tcmodules.pas 874 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FPasLibrary: TPasLibrary;
  110. FHintMsgs: TObjectList; // list of TTestHintMessage
  111. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  112. FJSRegModuleCall: TJSCallExpression;
  113. FScanner: TPas2jsPasScanner;
  114. FSkipTests: boolean;
  115. FSource: TStringList;
  116. FFirstPasStatement: TPasImplBlock;
  117. FWithTypeInfo: boolean;
  118. {$IFDEF EnablePasTreeGlobalRefCount}
  119. FElementRefCountAtSetup: int64;
  120. {$ENDIF}
  121. function GetMsgCount: integer;
  122. function GetMsgs(Index: integer): TTestHintMessage;
  123. function GetResolverCount: integer;
  124. function GetResolvers(Index: integer): TTestEnginePasResolver;
  125. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  126. procedure OnParserLog(Sender: TObject; const Msg: String);
  127. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  128. procedure OnScannerLog(Sender: TObject; const Msg: String);
  129. procedure SetWithTypeInfo(const AValue: boolean);
  130. protected
  131. procedure SetUp; override;
  132. function CreateConverter: TPasToJSConverter; virtual;
  133. function LoadUnit(const aUnitName: String): TPasModule;
  134. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  135. procedure TearDown; override;
  136. Procedure Add(Line: string); virtual;
  137. Procedure Add(const Lines: array of string);
  138. Procedure StartParsing; virtual;
  139. procedure ParseModuleQueue; virtual;
  140. procedure ParseModule; virtual;
  141. procedure ParseProgram; virtual;
  142. procedure ParseLibrary; virtual;
  143. procedure ParseUnit; virtual;
  144. protected
  145. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  146. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  149. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  150. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  151. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  152. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure ConvertModule; virtual;
  155. procedure ConvertProgram; virtual;
  156. procedure ConvertLibrary; virtual;
  157. procedure ConvertUnit; virtual;
  158. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  159. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  160. function GetDottedIdentifier(El: TJSElement): string;
  161. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  162. ImplStatements: string = ''); virtual;
  163. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  164. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  165. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  166. Msg: string; Marker: PSrcMarker = nil); virtual;
  167. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  168. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  169. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  172. function IsErrorExpected(E: Exception): boolean;
  173. procedure HandleScannerError(E: EScannerError);
  174. procedure HandleParserError(E: EParserError);
  175. procedure HandlePasResolveError(E: EPasResolve);
  176. procedure HandlePas2JSError(E: EPas2JS);
  177. procedure HandleException(E: Exception);
  178. procedure FailException(E: Exception);
  179. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  180. function IndexOfResolver(const Filename: string): integer;
  181. function GetResolver(const Filename: string): TTestEnginePasResolver;
  182. function GetDefaultNamespace: string;
  183. property PasProgram: TPasProgram Read FPasProgram;
  184. property PasLibrary: TPasLibrary Read FPasLibrary;
  185. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  186. property ResolverCount: integer read GetResolverCount;
  187. property Engine: TTestEnginePasResolver read FEngine;
  188. property Filename: string read FFilename;
  189. Property Module: TPasModule Read FModule;
  190. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  191. property Converter: TPasToJSConverter read FConverter;
  192. property JSSource: TStringList read FJSSource;
  193. property JSModule: TJSSourceElements read FJSModule;
  194. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  195. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  196. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  197. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  198. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  199. property JSInitBody: TJSFunctionBody read FJSInitBody;
  200. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  201. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  202. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  203. property SkipTests: boolean read FSkipTests write FSkipTests;
  204. public
  205. constructor Create; override;
  206. destructor Destroy; override;
  207. property Hub: TPas2JSResolverHub read FHub;
  208. property Source: TStringList read FSource;
  209. property FileResolver: TStreamResolver read FFileResolver;
  210. property Scanner: TPas2jsPasScanner read FScanner;
  211. property Parser: TTestPasParser read FParser;
  212. property MsgCount: integer read GetMsgCount;
  213. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  214. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  215. end;
  216. { TTestModule }
  217. TTestModule = class(TCustomTestModule)
  218. Published
  219. Procedure TestReservedWords;
  220. // program, units, includes
  221. Procedure TestEmptyProgram;
  222. Procedure TestEmptyProgramUseStrict;
  223. Procedure TestEmptyUnit;
  224. Procedure TestEmptyUnitUseStrict;
  225. Procedure TestDottedUnitNames;
  226. Procedure TestDottedUnitNameImpl;
  227. Procedure TestDottedUnitExpr;
  228. Procedure Test_ModeFPCFail;
  229. Procedure Test_ModeSwitchCBlocksFail;
  230. Procedure TestUnit_UseSystem;
  231. Procedure TestUnit_Intf1Impl2Intf1;
  232. Procedure TestIncludeVersion;
  233. // vars/const
  234. Procedure TestVarInt;
  235. Procedure TestVarBaseTypes;
  236. Procedure TestBaseTypeSingleFail;
  237. Procedure TestBaseTypeExtendedFail;
  238. Procedure TestConstBaseTypes;
  239. Procedure TestUnitImplVars;
  240. Procedure TestUnitImplConsts;
  241. Procedure TestUnitImplRecord;
  242. Procedure TestRenameJSNameConflict;
  243. Procedure TestLocalConst;
  244. Procedure TestVarExternal;
  245. Procedure TestVarExternalOtherUnit;
  246. Procedure TestVarAbsoluteFail;
  247. Procedure TestConstExternal;
  248. // numbers
  249. Procedure TestDouble;
  250. Procedure TestInteger;
  251. Procedure TestIntegerRange;
  252. Procedure TestIntegerTypecasts;
  253. Procedure TestInteger_BitwiseShrNativeInt;
  254. Procedure TestInteger_BitwiseShlNativeInt;
  255. Procedure TestInteger_SystemFunc;
  256. Procedure TestCurrency;
  257. Procedure TestForBoolDo;
  258. Procedure TestForIntDo;
  259. Procedure TestForIntInDo;
  260. // strings
  261. Procedure TestCharConst;
  262. Procedure TestChar_Compare;
  263. Procedure TestChar_BuiltInProcs;
  264. Procedure TestStringConst;
  265. Procedure TestStringConst_InvalidUTF16;
  266. Procedure TestStringConstSurrogate;
  267. Procedure TestString_Length;
  268. Procedure TestString_Compare;
  269. Procedure TestString_SetLength;
  270. Procedure TestString_CharAt;
  271. Procedure TestStringHMinusFail;
  272. Procedure TestStr;
  273. Procedure TestBaseType_AnsiStringFail;
  274. Procedure TestBaseType_WideStringFail;
  275. Procedure TestBaseType_ShortStringFail;
  276. Procedure TestBaseType_RawByteStringFail;
  277. Procedure TestTypeShortstring_Fail;
  278. Procedure TestCharSet_Custom;
  279. Procedure TestWideChar;
  280. Procedure TestForCharDo;
  281. Procedure TestForCharInDo;
  282. // alias types
  283. Procedure TestAliasTypeRef;
  284. Procedure TestTypeCast_BaseTypes;
  285. Procedure TestTypeCast_AliasBaseTypes;
  286. // functions
  287. Procedure TestEmptyProc;
  288. Procedure TestProcOneParam;
  289. Procedure TestFunctionWithoutParams;
  290. Procedure TestProcedureWithoutParams;
  291. Procedure TestPrgProcVar;
  292. Procedure TestProcTwoArgs;
  293. Procedure TestProc_DefaultValue;
  294. Procedure TestUnitProcVar;
  295. Procedure TestImplProc;
  296. Procedure TestFunctionResult;
  297. Procedure TestNestedProc;
  298. Procedure TestNestedProc_ResultString;
  299. Procedure TestForwardProc;
  300. Procedure TestNestedForwardProc;
  301. Procedure TestAssignFunctionResult;
  302. Procedure TestFunctionResultInCondition;
  303. Procedure TestFunctionResultInForLoop;
  304. Procedure TestFunctionResultInTypeCast;
  305. Procedure TestExit;
  306. Procedure TestExit_ResultInFinally;
  307. Procedure TestBreak;
  308. Procedure TestBreakAsVar;
  309. Procedure TestContinue;
  310. Procedure TestProc_External;
  311. Procedure TestProc_ExternalOtherUnit;
  312. Procedure TestProc_Asm;
  313. Procedure TestProc_AsmSubBlock;
  314. Procedure TestProc_Assembler;
  315. Procedure TestProc_VarParam;
  316. Procedure TestProc_VarParamString;
  317. Procedure TestProc_VarParamV;
  318. Procedure TestProc_Overload;
  319. Procedure TestProc_OverloadForward;
  320. Procedure TestProc_OverloadIntfImpl;
  321. Procedure TestProc_OverloadNested;
  322. Procedure TestProc_OverloadNestedForward;
  323. Procedure TestProc_OverloadUnitCycle;
  324. Procedure TestProc_Varargs;
  325. Procedure TestProc_ConstOrder;
  326. Procedure TestProc_DuplicateConst;
  327. Procedure TestProc_LocalVarAbsolute;
  328. Procedure TestProc_LocalVarInit;
  329. Procedure TestProc_ReservedWords;
  330. Procedure TestProc_ConstRefWord;
  331. // anonymous functions
  332. Procedure TestAnonymousProc_Assign_ObjFPC;
  333. Procedure TestAnonymousProc_Assign_Delphi;
  334. Procedure TestAnonymousProc_Arg;
  335. Procedure TestAnonymousProc_Typecast;
  336. Procedure TestAnonymousProc_With;
  337. Procedure TestAnonymousProc_ExceptOn;
  338. Procedure TestAnonymousProc_Nested;
  339. Procedure TestAnonymousProc_NestedAssignResult;
  340. Procedure TestAnonymousProc_Class;
  341. Procedure TestAnonymousProc_ForLoop;
  342. Procedure TestAnonymousProc_AsmDelphi;
  343. // enums, sets
  344. Procedure TestEnum_Name;
  345. Procedure TestEnum_Number;
  346. Procedure TestEnum_ConstFail;
  347. Procedure TestEnum_Functions;
  348. Procedure TestEnumRg_Functions;
  349. Procedure TestEnum_AsParams;
  350. Procedure TestEnumRange_Array;
  351. Procedure TestEnum_ForIn;
  352. Procedure TestEnum_ScopedNumber;
  353. Procedure TestEnum_InFunction;
  354. Procedure TestSet_Enum;
  355. Procedure TestSet_Operators;
  356. Procedure TestSet_Operator_In;
  357. Procedure TestSet_Functions;
  358. Procedure TestSet_PassAsArgClone;
  359. Procedure TestSet_AsParams;
  360. Procedure TestSet_Property;
  361. Procedure TestSet_EnumConst;
  362. Procedure TestSet_IntConst;
  363. Procedure TestSet_IntRange;
  364. Procedure TestSet_AnonymousEnumType;
  365. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  366. Procedure TestSet_ConstEnum;
  367. Procedure TestSet_ConstChar;
  368. Procedure TestSet_ConstInt;
  369. Procedure TestSet_InFunction;
  370. Procedure TestSet_ForIn;
  371. // statements
  372. Procedure TestNestBegin;
  373. Procedure TestIncDec;
  374. Procedure TestLoHiFpcMode;
  375. Procedure TestLoHiDelphiMode;
  376. Procedure TestAssignments;
  377. Procedure TestArithmeticOperators1;
  378. Procedure TestLogicalOperators;
  379. Procedure TestBitwiseOperators;
  380. Procedure TestBitwiseOperatorsLongword;
  381. Procedure TestFunctionInt;
  382. Procedure TestFunctionString;
  383. Procedure TestIfThen;
  384. Procedure TestForLoop;
  385. Procedure TestForLoopInsideFunction;
  386. Procedure TestForLoop_ReadVarAfter;
  387. Procedure TestForLoop_Nested;
  388. Procedure TestRepeatUntil;
  389. Procedure TestAsmBlock;
  390. Procedure TestAsmPas_Impl; // ToDo
  391. Procedure TestTryFinally;
  392. Procedure TestTryExcept;
  393. Procedure TestTryExcept_ReservedWords;
  394. Procedure TestIfThenRaiseElse;
  395. Procedure TestCaseOf;
  396. Procedure TestCaseOf_UseSwitch;
  397. Procedure TestCaseOfNoElse;
  398. Procedure TestCaseOfNoElse_UseSwitch;
  399. Procedure TestCaseOfRange;
  400. Procedure TestCaseOfString;
  401. Procedure TestCaseOfChar;
  402. Procedure TestCaseOfExternalClassConst;
  403. Procedure TestDebugger;
  404. // arrays
  405. Procedure TestArray_Dynamic;
  406. Procedure TestArray_Dynamic_Nil;
  407. Procedure TestArray_DynMultiDimensional;
  408. Procedure TestArray_DynamicAssign;
  409. Procedure TestArray_StaticInt;
  410. Procedure TestArray_StaticBool;
  411. Procedure TestArray_StaticChar;
  412. Procedure TestArray_StaticMultiDim;
  413. Procedure TestArray_StaticInFunction;
  414. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  415. Procedure TestArrayOfRecord;
  416. Procedure TestArray_StaticRecord;
  417. Procedure TestArrayOfSet;
  418. Procedure TestArray_DynAsParam;
  419. Procedure TestArray_StaticAsParam;
  420. Procedure TestArrayElement_AsParams;
  421. Procedure TestArrayElementFromFuncResult_AsParams;
  422. Procedure TestArrayEnumTypeRange;
  423. Procedure TestArray_SetLengthOutArg;
  424. Procedure TestArray_SetLengthProperty;
  425. Procedure TestArray_SetLengthMultiDim;
  426. Procedure TestArray_SetLengthDynOfStatic;
  427. Procedure TestArray_OpenArrayOfString;
  428. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  429. Procedure TestArray_ConstRef;
  430. Procedure TestArray_Concat;
  431. Procedure TestArray_Copy;
  432. Procedure TestArray_InsertDelete;
  433. Procedure TestArray_DynArrayConstObjFPC;
  434. Procedure TestArray_DynArrayConstDelphi;
  435. Procedure TestArray_ArrayLitAsParam;
  436. Procedure TestArray_ArrayLitMultiDimAsParam;
  437. Procedure TestArray_ArrayLitStaticAsParam;
  438. Procedure TestArray_ForInArrOfString;
  439. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  440. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  441. Procedure TestArrayOfConst_TVarRec;
  442. Procedure TestArrayOfConst_PassBaseTypes;
  443. Procedure TestArrayOfConst_PassObj;
  444. // record
  445. Procedure TestRecord_Empty;
  446. Procedure TestRecord_Var;
  447. Procedure TestRecord_VarExternal;
  448. Procedure TestRecord_WithDo;
  449. Procedure TestRecord_Assign;
  450. Procedure TestRecord_AsParams;
  451. Procedure TestRecord_ConstRef;
  452. Procedure TestRecordElement_AsParams;
  453. Procedure TestRecordElementFromFuncResult_AsParams;
  454. Procedure TestRecordElementFromWith_AsParams;
  455. Procedure TestRecord_Equal;
  456. Procedure TestRecord_JSValue;
  457. Procedure TestRecord_VariantFail;
  458. Procedure TestRecord_FieldArray;
  459. Procedure TestRecord_Const;
  460. Procedure TestRecord_TypecastFail;
  461. Procedure TestRecord_InFunction;
  462. Procedure TestRecord_AnonymousFail;
  463. // advanced record
  464. Procedure TestAdvRecord_Function;
  465. Procedure TestAdvRecord_Property;
  466. Procedure TestAdvRecord_PropertyDefault;
  467. Procedure TestAdvRecord_Property_ClassMethod;
  468. Procedure TestAdvRecord_Const;
  469. Procedure TestAdvRecord_ExternalField;
  470. Procedure TestAdvRecord_SubRecord;
  471. Procedure TestAdvRecord_SubClass;
  472. Procedure TestAdvRecord_SubInterfaceFail;
  473. Procedure TestAdvRecord_Constructor;
  474. Procedure TestAdvRecord_ClassConstructor_Program;
  475. Procedure TestAdvRecord_ClassConstructor_Unit;
  476. // classes
  477. Procedure TestClass_TObjectDefaultConstructor;
  478. Procedure TestClass_TObjectConstructorWithParams;
  479. Procedure TestClass_TObjectConstructorWithDefaultParam;
  480. Procedure TestClass_Var;
  481. Procedure TestClass_Method;
  482. Procedure TestClass_Implementation;
  483. Procedure TestClass_Inheritance;
  484. Procedure TestClass_TypeAlias;
  485. Procedure TestClass_AbstractMethod;
  486. Procedure TestClass_CallInherited_ProcNoParams;
  487. Procedure TestClass_CallInherited_WithParams;
  488. Procedure TestClasS_CallInheritedConstructor;
  489. Procedure TestClass_ClassVar_Assign;
  490. Procedure TestClass_CallClassMethod;
  491. Procedure TestClass_Property;
  492. Procedure TestClass_Property_ClassMethod;
  493. Procedure TestClass_Property_Indexed;
  494. Procedure TestClass_Property_IndexSpec;
  495. Procedure TestClass_PropertyOfTypeArray;
  496. Procedure TestClass_PropertyDefault;
  497. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  498. //Procedure TestClass_PropertyDefault;
  499. Procedure TestClass_PropertyOverride;
  500. Procedure TestClass_PropertyIncVisibility;
  501. Procedure TestClass_Assigned;
  502. Procedure TestClass_WithClassDoCreate;
  503. Procedure TestClass_WithClassInstDoProperty;
  504. Procedure TestClass_WithClassInstDoPropertyWithParams;
  505. Procedure TestClass_WithClassInstDoFunc;
  506. Procedure TestClass_TypeCast;
  507. Procedure TestClass_TypeCastUntypedParam;
  508. Procedure TestClass_Overloads;
  509. Procedure TestClass_OverloadsAncestor;
  510. Procedure TestClass_OverloadConstructor;
  511. Procedure TestClass_OverloadDelphiOverride;
  512. Procedure TestClass_ReintroduceVarDelphi;
  513. Procedure TestClass_ReintroducedVar;
  514. Procedure TestClass_RaiseDescendant;
  515. Procedure TestClass_ExternalMethod;
  516. Procedure TestClass_ExternalVirtualNameMismatchFail;
  517. Procedure TestClass_ExternalOverrideFail;
  518. Procedure TestClass_ExternalVar;
  519. Procedure TestClass_Const;
  520. Procedure TestClass_ConstEnum;
  521. Procedure TestClass_LocalConstDuplicate_Prg;
  522. Procedure TestClass_LocalConstDuplicate_Unit;
  523. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  524. Procedure TestClass_LocalVarSelfFail;
  525. Procedure TestClass_ArgSelfFail;
  526. Procedure TestClass_NestedProcSelf;
  527. Procedure TestClass_NestedProcSelf2;
  528. Procedure TestClass_NestedProcClassSelf;
  529. Procedure TestClass_NestedProcCallInherited;
  530. Procedure TestClass_TObjectFree;
  531. Procedure TestClass_TObjectFree_VarArg;
  532. Procedure TestClass_TObjectFreeNewInstance;
  533. Procedure TestClass_TObjectFreeLowerCase;
  534. Procedure TestClass_TObjectFreeFunctionFail;
  535. Procedure TestClass_TObjectFreePropertyFail;
  536. Procedure TestClass_ForIn;
  537. Procedure TestClass_DispatchMessage;
  538. Procedure TestClass_Message_DuplicateIntFail;
  539. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  540. // class of
  541. Procedure TestClassOf_Create;
  542. Procedure TestClassOf_Call;
  543. Procedure TestClassOf_Assign;
  544. Procedure TestClassOf_Is;
  545. Procedure TestClassOf_Compare;
  546. Procedure TestClassOf_ClassVar;
  547. Procedure TestClassOf_ClassMethod;
  548. Procedure TestClassOf_ClassProperty;
  549. Procedure TestClassOf_ClassMethodSelf;
  550. Procedure TestClassOf_TypeCast;
  551. Procedure TestClassOf_ImplicitFunctionCall;
  552. Procedure TestClassOf_Const;
  553. // nested class
  554. Procedure TestNestedClass_Alias;
  555. Procedure TestNestedClass_Record;
  556. Procedure TestNestedClass_Class;
  557. // external class
  558. Procedure TestExternalClass_Var;
  559. Procedure TestExternalClass_Const;
  560. Procedure TestExternalClass_Dollar;
  561. Procedure TestExternalClass_DuplicateVarFail;
  562. Procedure TestExternalClass_Method;
  563. Procedure TestExternalClass_ClassMethod;
  564. Procedure TestExternalClass_ClassMethodStatic;
  565. Procedure TestExternalClass_FunctionResultInTypeCast;
  566. Procedure TestExternalClass_NonExternalOverride;
  567. Procedure TestExternalClass_OverloadHint;
  568. Procedure TestExternalClass_SameNamePublishedProperty;
  569. Procedure TestExternalClass_Property;
  570. Procedure TestExternalClass_PropertyDate;
  571. Procedure TestExternalClass_ClassProperty;
  572. Procedure TestExternalClass_ClassOf;
  573. Procedure TestExternalClass_ClassOtherUnit;
  574. Procedure TestExternalClass_Is;
  575. Procedure TestExternalClass_As;
  576. Procedure TestExternalClass_DestructorFail;
  577. Procedure TestExternalClass_New;
  578. Procedure TestExternalClass_ClassOf_New;
  579. Procedure TestExternalClass_FuncClassOf_New;
  580. Procedure TestExternalClass_New_PasClassFail;
  581. Procedure TestExternalClass_New_PasClassBracketsFail;
  582. Procedure TestExternalClass_NewExtName;
  583. Procedure TestExternalClass_Constructor;
  584. Procedure TestExternalClass_ConstructorBrackets;
  585. Procedure TestExternalClass_LocalConstSameName;
  586. Procedure TestExternalClass_ReintroduceOverload;
  587. Procedure TestExternalClass_Inherited;
  588. Procedure TestExternalClass_PascalAncestorFail;
  589. Procedure TestExternalClass_NewInstance;
  590. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  591. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  592. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  593. Procedure TestExternalClass_JSFunctionPasDescendant;
  594. Procedure TestExternalClass_PascalProperty;
  595. Procedure TestExternalClass_TypeCastToRootClass;
  596. Procedure TestExternalClass_TypeCastToJSObject;
  597. Procedure TestExternalClass_TypeCastStringToExternalString;
  598. Procedure TestExternalClass_TypeCastToJSFunction;
  599. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  600. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  601. Procedure TestExternalClass_BracketAccessor;
  602. Procedure TestExternalClass_BracketAccessor_Call;
  603. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  604. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  605. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  606. Procedure TestExternalClass_BracketAccessor_MultiType;
  607. Procedure TestExternalClass_BracketAccessor_Index;
  608. Procedure TestExternalClass_ForInJSObject;
  609. Procedure TestExternalClass_ForInJSArray;
  610. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  611. // class interfaces
  612. Procedure TestClassInterface_Corba;
  613. Procedure TestClassInterface_ProcExternalFail;
  614. Procedure TestClassInterface_Overloads;
  615. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  616. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  617. Procedure TestClassInterface_AncestorImpl;
  618. Procedure TestClassInterface_ImplReintroduce;
  619. Procedure TestClassInterface_MethodResolution;
  620. Procedure TestClassInterface_AncestorMoreInterfaces;
  621. Procedure TestClassInterface_MethodOverride;
  622. Procedure TestClassInterface_Corba_Delegation;
  623. Procedure TestClassInterface_Corba_DelegationStatic;
  624. Procedure TestClassInterface_Corba_Operators;
  625. Procedure TestClassInterface_Corba_Args;
  626. Procedure TestClassInterface_Corba_ForIn;
  627. Procedure TestClassInterface_COM_AssignVar;
  628. Procedure TestClassInterface_COM_AssignArg;
  629. Procedure TestClassInterface_COM_FunctionResult;
  630. Procedure TestClassInterface_COM_InheritedFuncResult;
  631. Procedure TestClassInterface_COM_IsAsTypeCasts;
  632. Procedure TestClassInterface_COM_PassAsArg;
  633. Procedure TestClassInterface_COM_PassToUntypedParam;
  634. Procedure TestClassInterface_COM_FunctionInExpr;
  635. Procedure TestClassInterface_COM_Property;
  636. Procedure TestClassInterface_COM_IntfProperty;
  637. Procedure TestClassInterface_COM_Delegation;
  638. Procedure TestClassInterface_COM_With;
  639. Procedure TestClassInterface_COM_ForIn;
  640. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  641. Procedure TestClassInterface_COM_RecordIntfFail;
  642. Procedure TestClassInterface_COM_UnitInitialization;
  643. Procedure TestClassInterface_GUID;
  644. Procedure TestClassInterface_GUIDProperty;
  645. // helpers
  646. Procedure TestClassHelper_ClassVar;
  647. Procedure TestClassHelper_Method_AccessInstanceFields;
  648. Procedure TestClassHelper_Method_Call;
  649. Procedure TestClassHelper_Method_Nested_Call;
  650. Procedure TestClassHelper_ClassMethod_Call;
  651. Procedure TestClassHelper_ClassOf;
  652. Procedure TestClassHelper_MethodRefObjFPC;
  653. Procedure TestClassHelper_Constructor;
  654. Procedure TestClassHelper_InheritedObjFPC;
  655. Procedure TestClassHelper_Property;
  656. Procedure TestClassHelper_Property_Array;
  657. Procedure TestClassHelper_Property_Array_Default;
  658. Procedure TestClassHelper_Property_Array_DefaultDefault;
  659. Procedure TestClassHelper_ClassProperty;
  660. Procedure TestClassHelper_ClassPropertyStatic;
  661. Procedure TestClassHelper_ClassProperty_Array;
  662. Procedure TestClassHelper_ForIn;
  663. Procedure TestClassHelper_PassProperty;
  664. Procedure TestExtClassHelper_ClassVar;
  665. Procedure TestExtClassHelper_Method_Call;
  666. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  667. Procedure TestRecordHelper_ClassVar;
  668. Procedure TestRecordHelper_Method_Call;
  669. Procedure TestRecordHelper_Constructor;
  670. Procedure TestTypeHelper_ClassVar;
  671. Procedure TestTypeHelper_PassResultElement;
  672. Procedure TestTypeHelper_PassArgs;
  673. Procedure TestTypeHelper_PassVarConst;
  674. Procedure TestTypeHelper_PassFuncResult;
  675. Procedure TestTypeHelper_PassPropertyField;
  676. Procedure TestTypeHelper_PassPropertyGetter;
  677. Procedure TestTypeHelper_PassClassPropertyField;
  678. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  679. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  680. Procedure TestTypeHelper_Property;
  681. Procedure TestTypeHelper_Property_Array;
  682. Procedure TestTypeHelper_ClassProperty;
  683. Procedure TestTypeHelper_ClassProperty_Array;
  684. Procedure TestTypeHelper_ClassMethod;
  685. Procedure TestTypeHelper_ExtClassMethodFail;
  686. Procedure TestTypeHelper_Constructor;
  687. Procedure TestTypeHelper_Word;
  688. Procedure TestTypeHelper_Boolean;
  689. Procedure TestTypeHelper_WordBool;
  690. Procedure TestTypeHelper_Double;
  691. Procedure TestTypeHelper_NativeInt;
  692. Procedure TestTypeHelper_StringChar;
  693. Procedure TestTypeHelper_JSValue;
  694. Procedure TestTypeHelper_Array;
  695. Procedure TestTypeHelper_EnumType;
  696. Procedure TestTypeHelper_SetType;
  697. Procedure TestTypeHelper_InterfaceType;
  698. Procedure TestTypeHelper_NestedSelf;
  699. // proc types
  700. Procedure TestProcType;
  701. Procedure TestProcType_Arg;
  702. Procedure TestProcType_FunctionFPC;
  703. Procedure TestProcType_FunctionDelphi;
  704. Procedure TestProcType_ProcedureDelphi;
  705. Procedure TestProcType_AsParam;
  706. Procedure TestProcType_MethodFPC;
  707. Procedure TestProcType_MethodDelphi;
  708. Procedure TestProcType_PropertyFPC;
  709. Procedure TestProcType_PropertyDelphi;
  710. Procedure TestProcType_WithClassInstDoPropertyFPC;
  711. Procedure TestProcType_Nested;
  712. Procedure TestProcType_NestedOfObject;
  713. Procedure TestProcType_ReferenceToProc;
  714. Procedure TestProcType_ReferenceToMethod;
  715. Procedure TestProcType_Typecast;
  716. Procedure TestProcType_PassProcToUntyped;
  717. Procedure TestProcType_PassProcToArray;
  718. Procedure TestProcType_SafeCallObjFPC;
  719. Procedure TestProcType_SafeCallDelphi;
  720. // pointer
  721. Procedure TestPointer;
  722. Procedure TestPointer_Proc;
  723. Procedure TestPointer_AssignRecordFail;
  724. Procedure TestPointer_AssignStaticArrayFail;
  725. Procedure TestPointer_TypeCastJSValueToPointer;
  726. Procedure TestPointer_NonRecordFail;
  727. Procedure TestPointer_AnonymousArgTypeFail;
  728. Procedure TestPointer_AnonymousVarTypeFail;
  729. Procedure TestPointer_AnonymousResultTypeFail;
  730. Procedure TestPointer_AddrOperatorFail;
  731. Procedure TestPointer_ArrayParamsFail;
  732. Procedure TestPointer_PointerAddFail;
  733. Procedure TestPointer_IncPointerFail;
  734. Procedure TestPointer_Record;
  735. Procedure TestPointer_RecordArg;
  736. // jsvalue
  737. Procedure TestJSValue_AssignToJSValue;
  738. Procedure TestJSValue_TypeCastToBaseType;
  739. Procedure TestJSValue_TypecastToJSValue;
  740. Procedure TestJSValue_Equal;
  741. Procedure TestJSValue_If;
  742. Procedure TestJSValue_Not;
  743. Procedure TestJSValue_Enum;
  744. Procedure TestJSValue_ClassInstance;
  745. Procedure TestJSValue_ClassOf;
  746. Procedure TestJSValue_ArrayOfJSValue;
  747. Procedure TestJSValue_ArrayLit;
  748. Procedure TestJSValue_Params;
  749. Procedure TestJSValue_UntypedParam;
  750. Procedure TestJSValue_FuncResultType;
  751. Procedure TestJSValue_ProcType_Assign;
  752. Procedure TestJSValue_ProcType_Equal;
  753. Procedure TestJSValue_ProcType_Param;
  754. Procedure TestJSValue_AssignToPointerFail;
  755. Procedure TestJSValue_OverloadDouble;
  756. Procedure TestJSValue_OverloadNativeInt;
  757. Procedure TestJSValue_OverloadWord;
  758. Procedure TestJSValue_OverloadString;
  759. Procedure TestJSValue_OverloadChar;
  760. Procedure TestJSValue_OverloadPointer;
  761. Procedure TestJSValue_ForIn;
  762. // RTTI
  763. Procedure TestRTTI_IntRange;
  764. Procedure TestRTTI_Double;
  765. Procedure TestRTTI_ProcType;
  766. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  767. Procedure TestRTTI_EnumAndSetType;
  768. Procedure TestRTTI_EnumRange;
  769. Procedure TestRTTI_AnonymousEnumType;
  770. Procedure TestRTTI_StaticArray;
  771. Procedure TestRTTI_DynArray;
  772. Procedure TestRTTI_ArrayNestedAnonymous;
  773. Procedure TestRTTI_PublishedMethodOverloadFail;
  774. Procedure TestRTTI_PublishedMethodExternalFail;
  775. Procedure TestRTTI_PublishedClassPropertyFail;
  776. Procedure TestRTTI_PublishedClassFieldFail;
  777. Procedure TestRTTI_PublishedFieldExternalFail;
  778. Procedure TestRTTI_Class_Field;
  779. Procedure TestRTTI_Class_Method;
  780. Procedure TestRTTI_Class_MethodArgFlags;
  781. Procedure TestRTTI_Class_Property;
  782. Procedure TestRTTI_Class_PropertyParams;
  783. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  784. Procedure TestRTTI_Class_OmitRTTI;
  785. Procedure TestRTTI_IndexModifier;
  786. Procedure TestRTTI_StoredModifier;
  787. Procedure TestRTTI_DefaultValue;
  788. Procedure TestRTTI_DefaultValueSet;
  789. Procedure TestRTTI_DefaultValueRangeType;
  790. Procedure TestRTTI_DefaultValueInherit;
  791. Procedure TestRTTI_OverrideMethod;
  792. Procedure TestRTTI_ReintroduceMethod;
  793. Procedure TestRTTI_OverloadProperty;
  794. // ToDo: array argument
  795. Procedure TestRTTI_ClassForward;
  796. Procedure TestRTTI_ClassOf;
  797. Procedure TestRTTI_Record;
  798. Procedure TestRTTI_RecordAnonymousArray;
  799. Procedure TestRTTI_Record_ClassVarType;
  800. Procedure TestRTTI_LocalTypes;
  801. Procedure TestRTTI_TypeInfo_BaseTypes;
  802. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  803. Procedure TestRTTI_TypeInfo_LocalFail;
  804. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  805. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  806. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  807. Procedure TestRTTI_TypeInfo_FunctionClassType;
  808. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  809. Procedure TestRTTI_Interface_Corba;
  810. Procedure TestRTTI_Interface_COM;
  811. Procedure TestRTTI_ClassHelper;
  812. Procedure TestRTTI_ExternalClass;
  813. Procedure TestRTTI_Unit;
  814. // Resourcestring
  815. Procedure TestResourcestringProgram;
  816. Procedure TestResourcestringUnit;
  817. Procedure TestResourcestringImplementation;
  818. // Attributes
  819. Procedure TestAttributes_Members;
  820. Procedure TestAttributes_Types;
  821. Procedure TestAttributes_HelperConstructor_Fail;
  822. // Assertions, checks
  823. procedure TestAssert;
  824. procedure TestAssert_SysUtils;
  825. procedure TestObjectChecks;
  826. procedure TestOverflowChecks_Int;
  827. procedure TestRangeChecks_AssignInt;
  828. procedure TestRangeChecks_AssignIntRange;
  829. procedure TestRangeChecks_AssignEnum;
  830. procedure TestRangeChecks_AssignEnumRange;
  831. procedure TestRangeChecks_AssignChar;
  832. procedure TestRangeChecks_AssignCharRange;
  833. procedure TestRangeChecks_ArrayIndex;
  834. procedure TestRangeChecks_ArrayOfRecIndex;
  835. procedure TestRangeChecks_StringIndex;
  836. procedure TestRangeChecks_TypecastInt;
  837. procedure TestRangeChecks_TypeHelperInt;
  838. // Async/AWait
  839. Procedure TestAsync_Proc;
  840. Procedure TestAsync_CallResultIsPromise;
  841. Procedure TestAsync_ConstructorFail;
  842. Procedure TestAsync_PropertyGetterFail;
  843. Procedure TestAwait_NonPromiseWithTypeFail;
  844. Procedure TestAwait_AsyncCallTypeMismatch;
  845. Procedure TestAWait_OutsideAsyncFail;
  846. Procedure TestAWait_IntegerFail;
  847. Procedure TestAWait_ExternalClassPromise;
  848. Procedure TestAWait_JSValue;
  849. Procedure TestAWait_Result;
  850. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  851. Procedure TestAsync_AnonymousProc;
  852. Procedure TestAsync_ProcType;
  853. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  854. Procedure TestAsync_Inherited;
  855. Procedure TestAsync_ClassInterface;
  856. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  857. // Library
  858. Procedure TestLibrary_Empty;
  859. Procedure TestLibrary_ExportFunc; // ToDo
  860. // ToDo: test delayed specialization init
  861. // ToDO: analyzer
  862. end;
  863. function LinesToStr(Args: array of const): string;
  864. function ExtractFileUnitName(aFilename: string): string;
  865. function JSToStr(El: TJSElement): string;
  866. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  867. implementation
  868. function LinesToStr(Args: array of const): string;
  869. var
  870. s: String;
  871. i: Integer;
  872. begin
  873. s:='';
  874. for i:=Low(Args) to High(Args) do
  875. case Args[i].VType of
  876. vtChar: s += Args[i].VChar+LineEnding;
  877. vtString: s += Args[i].VString^+LineEnding;
  878. vtPChar: s += Args[i].VPChar+LineEnding;
  879. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  880. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  881. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  882. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  883. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  884. end;
  885. Result:=s;
  886. end;
  887. function ExtractFileUnitName(aFilename: string): string;
  888. var
  889. p: Integer;
  890. begin
  891. Result:=ExtractFileName(aFilename);
  892. if Result='' then exit;
  893. for p:=length(Result) downto 1 do
  894. case Result[p] of
  895. '/','\': exit;
  896. '.':
  897. begin
  898. Delete(Result,p,length(Result));
  899. exit;
  900. end;
  901. end;
  902. end;
  903. function JSToStr(El: TJSElement): string;
  904. var
  905. aWriter: TBufferWriter;
  906. aJSWriter: TJSWriter;
  907. begin
  908. aJSWriter:=nil;
  909. aWriter:=TBufferWriter.Create(1000);
  910. try
  911. aJSWriter:=TJSWriter.Create(aWriter);
  912. aJSWriter.IndentSize:=2;
  913. aJSWriter.WriteJS(El);
  914. Result:=aWriter.AsString;
  915. finally
  916. aJSWriter.Free;
  917. aWriter.Free;
  918. end;
  919. end;
  920. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  921. // search diff, ignore changes in spaces
  922. const
  923. SpaceChars = [#9,#10,#13,' '];
  924. var
  925. ExpectedP, ActualP: PChar;
  926. function FindLineEnd(p: PChar): PChar;
  927. begin
  928. Result:=p;
  929. while not (Result^ in [#0,#10,#13]) do inc(Result);
  930. end;
  931. function FindLineStart(p, MinP: PChar): PChar;
  932. begin
  933. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  934. Result:=p;
  935. end;
  936. procedure SkipLineEnd(var p: PChar);
  937. begin
  938. if p^ in [#10,#13] then
  939. begin
  940. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  941. inc(p,2)
  942. else
  943. inc(p);
  944. end;
  945. end;
  946. function HasSpecialChar(s: string): boolean;
  947. var
  948. i: Integer;
  949. begin
  950. for i:=1 to length(s) do
  951. if s[i] in [#0..#31,#127..#255] then
  952. exit(true);
  953. Result:=false;
  954. end;
  955. function HashSpecialChars(s: string): string;
  956. var
  957. i: Integer;
  958. begin
  959. Result:='';
  960. for i:=1 to length(s) do
  961. if s[i] in [#0..#31,#127..#255] then
  962. Result:=Result+'#'+hexstr(ord(s[i]),2)
  963. else
  964. Result:=Result+s[i];
  965. end;
  966. procedure DiffFound;
  967. var
  968. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  969. ExpLine, ActLine: String;
  970. i, LineNo, DiffLineNo: Integer;
  971. begin
  972. writeln('Diff found "',Msg,'". Lines:');
  973. // write correct lines
  974. p:=PChar(Expected);
  975. LineNo:=0;
  976. DiffLineNo:=0;
  977. repeat
  978. StartPos:=p;
  979. while not (p^ in [#0,#10,#13]) do inc(p);
  980. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  981. SkipLineEnd(p);
  982. inc(LineNo);
  983. if (p<=ExpectedP) and (p^<>#0) then
  984. begin
  985. writeln('= ',ExpLine);
  986. end else begin
  987. // diff line
  988. if DiffLineNo=0 then DiffLineNo:=LineNo;
  989. // write actual line
  990. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  991. ActLineEndP:=FindLineEnd(ActualP);
  992. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  993. writeln('- ',ActLine);
  994. if HasSpecialChar(ActLine) then
  995. writeln('- ',HashSpecialChars(ActLine));
  996. // write expected line
  997. writeln('+ ',ExpLine);
  998. if HasSpecialChar(ExpLine) then
  999. writeln('- ',HashSpecialChars(ExpLine));
  1000. // write empty line with pointer ^
  1001. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1002. writeln('^');
  1003. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1004. CheckSrcDiff:=false;
  1005. // write up to three following actual lines to get some context
  1006. for i:=1 to 3 do begin
  1007. ActLineStartP:=ActLineEndP;
  1008. SkipLineEnd(ActLineStartP);
  1009. if ActLineStartP^=#0 then break;
  1010. ActLineEndP:=FindLineEnd(ActLineStartP);
  1011. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1012. writeln('~ ',ActLine);
  1013. end;
  1014. exit;
  1015. end;
  1016. until p^=#0;
  1017. writeln('DiffFound Actual:-----------------------');
  1018. writeln(Actual);
  1019. writeln('DiffFound Expected:---------------------');
  1020. writeln(Expected);
  1021. writeln('DiffFound ------------------------------');
  1022. Msg:='diff found, but lines are the same, internal error';
  1023. CheckSrcDiff:=false;
  1024. end;
  1025. var
  1026. IsSpaceNeeded: Boolean;
  1027. LastChar, Quote: Char;
  1028. begin
  1029. Result:=true;
  1030. Msg:='';
  1031. if Expected='' then Expected:=' ';
  1032. if Actual='' then Actual:=' ';
  1033. ExpectedP:=PChar(Expected);
  1034. ActualP:=PChar(Actual);
  1035. repeat
  1036. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1037. case ExpectedP^ of
  1038. #0:
  1039. begin
  1040. // check that rest of Actual has only spaces
  1041. while ActualP^ in SpaceChars do inc(ActualP);
  1042. if ActualP^<>#0 then
  1043. begin
  1044. DiffFound;
  1045. exit;
  1046. end;
  1047. exit(true);
  1048. end;
  1049. ' ',#9,#10,#13:
  1050. begin
  1051. // skip space in Expected
  1052. IsSpaceNeeded:=false;
  1053. if ExpectedP>PChar(Expected) then
  1054. LastChar:=ExpectedP[-1]
  1055. else
  1056. LastChar:=#0;
  1057. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1058. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1059. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1060. IsSpaceNeeded:=true;
  1061. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1062. begin
  1063. DiffFound;
  1064. exit;
  1065. end;
  1066. while ActualP^ in SpaceChars do inc(ActualP);
  1067. end;
  1068. '''','"':
  1069. begin
  1070. while ActualP^ in SpaceChars do inc(ActualP);
  1071. if ExpectedP^<>ActualP^ then
  1072. begin
  1073. DiffFound;
  1074. exit;
  1075. end;
  1076. Quote:=ExpectedP^;
  1077. repeat
  1078. inc(ExpectedP);
  1079. inc(ActualP);
  1080. if ExpectedP^<>ActualP^ then
  1081. begin
  1082. DiffFound;
  1083. exit;
  1084. end;
  1085. if (ExpectedP^ in [#0,#10,#13]) then
  1086. break
  1087. else if (ExpectedP^=Quote) then
  1088. begin
  1089. inc(ExpectedP);
  1090. inc(ActualP);
  1091. break;
  1092. end;
  1093. until false;
  1094. end;
  1095. else
  1096. while ActualP^ in SpaceChars do inc(ActualP);
  1097. if ExpectedP^<>ActualP^ then
  1098. begin
  1099. DiffFound;
  1100. exit;
  1101. end;
  1102. inc(ExpectedP);
  1103. inc(ActualP);
  1104. end;
  1105. until false;
  1106. end;
  1107. { TTestEnginePasResolver }
  1108. destructor TTestEnginePasResolver.Destroy;
  1109. begin
  1110. FreeAndNil(FStreamResolver);
  1111. FreeAndNil(FParser);
  1112. FreeAndNil(FScanner);
  1113. FreeAndNil(FStreamResolver);
  1114. if Module<>nil then
  1115. begin
  1116. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1117. FModule:=nil;
  1118. end;
  1119. inherited Destroy;
  1120. end;
  1121. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1122. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1123. begin
  1124. Result:=nil;
  1125. if InFilename<>'' then
  1126. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1127. if Assigned(OnFindUnit) then
  1128. Result:=OnFindUnit(AName);
  1129. if NameExpr=nil then ;
  1130. end;
  1131. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1132. begin
  1133. // do not parse recursively
  1134. // parse via the queue
  1135. if Section=nil then ;
  1136. end;
  1137. { TCustomTestModule }
  1138. function TCustomTestModule.GetMsgCount: integer;
  1139. begin
  1140. Result:=FHintMsgs.Count;
  1141. end;
  1142. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1143. begin
  1144. Result:=TTestHintMessage(FHintMsgs[Index]);
  1145. end;
  1146. function TCustomTestModule.GetResolverCount: integer;
  1147. begin
  1148. Result:=FModules.Count;
  1149. end;
  1150. function TCustomTestModule.GetResolvers(Index: integer
  1151. ): TTestEnginePasResolver;
  1152. begin
  1153. Result:=TTestEnginePasResolver(FModules[Index]);
  1154. end;
  1155. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1156. ): TPasModule;
  1157. var
  1158. DefNamespace: String;
  1159. begin
  1160. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1161. if (Pos('.',aUnitName)<1) then
  1162. begin
  1163. DefNamespace:=GetDefaultNamespace;
  1164. if DefNamespace<>'' then
  1165. begin
  1166. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1167. if Result<>nil then exit;
  1168. end;
  1169. end;
  1170. Result:=LoadUnit(aUnitName);
  1171. if Result<>nil then exit;
  1172. {$IFDEF VerbosePas2JS}
  1173. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1174. {$ENDIF}
  1175. Fail('can''t find unit "'+aUnitName+'"');
  1176. end;
  1177. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1178. var
  1179. aParser: TPasParser;
  1180. Item: TTestHintMessage;
  1181. begin
  1182. aParser:=Sender as TPasParser;
  1183. Item:=TTestHintMessage.Create;
  1184. Item.Id:=aParser.LastMsgNumber;
  1185. Item.MsgType:=aParser.LastMsgType;
  1186. Item.MsgNumber:=aParser.LastMsgNumber;
  1187. Item.Msg:=Msg;
  1188. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1189. {$IFDEF VerbosePas2JS}
  1190. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1191. {$ENDIF}
  1192. FHintMsgs.Add(Item);
  1193. end;
  1194. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1195. );
  1196. var
  1197. aResolver: TTestEnginePasResolver;
  1198. Item: TTestHintMessage;
  1199. begin
  1200. aResolver:=Sender as TTestEnginePasResolver;
  1201. Item:=TTestHintMessage.Create;
  1202. Item.Id:=aResolver.LastMsgId;
  1203. Item.MsgType:=aResolver.LastMsgType;
  1204. Item.MsgNumber:=aResolver.LastMsgNumber;
  1205. Item.Msg:=Msg;
  1206. Item.SourcePos:=aResolver.LastSourcePos;
  1207. {$IFDEF VerbosePas2JS}
  1208. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1209. {$ENDIF}
  1210. FHintMsgs.Add(Item);
  1211. end;
  1212. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1213. var
  1214. Item: TTestHintMessage;
  1215. aScanner: TPas2jsPasScanner;
  1216. begin
  1217. aScanner:=Sender as TPas2jsPasScanner;
  1218. Item:=TTestHintMessage.Create;
  1219. Item.Id:=aScanner.LastMsgNumber;
  1220. Item.MsgType:=aScanner.LastMsgType;
  1221. Item.MsgNumber:=aScanner.LastMsgNumber;
  1222. Item.Msg:=Msg;
  1223. Item.SourcePos:=aScanner.CurSourcePos;
  1224. {$IFDEF VerbosePas2JS}
  1225. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1226. {$ENDIF}
  1227. FHintMsgs.Add(Item);
  1228. end;
  1229. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1230. begin
  1231. if FWithTypeInfo=AValue then Exit;
  1232. FWithTypeInfo:=AValue;
  1233. if AValue then
  1234. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1235. else
  1236. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1237. end;
  1238. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1239. var
  1240. i: Integer;
  1241. CurEngine: TTestEnginePasResolver;
  1242. CurUnitName: String;
  1243. begin
  1244. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1245. Result:=nil;
  1246. if (Module.ClassType=TPasModule)
  1247. and (CompareText(Module.Name,aUnitName)=0) then
  1248. exit(Module);
  1249. for i:=0 to ResolverCount-1 do
  1250. begin
  1251. CurEngine:=Resolvers[i];
  1252. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1253. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1254. if CompareText(aUnitName,CurUnitName)=0 then
  1255. begin
  1256. Result:=CurEngine.Module;
  1257. if Result<>nil then exit;
  1258. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1259. FileResolver.FindSourceFile(aUnitName);
  1260. CurEngine.StreamResolver:=TStreamResolver.Create;
  1261. CurEngine.StreamResolver.OwnsStreams:=True;
  1262. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1263. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1264. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1265. InitScanner(CurEngine.Scanner);
  1266. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1267. CurEngine.Parser.Options:=po_tcmodules;
  1268. if CompareText(CurUnitName,'System')=0 then
  1269. CurEngine.Parser.ImplicitUses.Clear;
  1270. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1271. try
  1272. CurEngine.Parser.NextToken;
  1273. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1274. except
  1275. on E: Exception do
  1276. HandleException(E);
  1277. end;
  1278. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1279. Result:=CurEngine.Module;
  1280. exit;
  1281. end;
  1282. end;
  1283. end;
  1284. procedure TCustomTestModule.SetUp;
  1285. begin
  1286. {$IFDEF EnablePasTreeGlobalRefCount}
  1287. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1288. {$ENDIF}
  1289. if FModules<>nil then
  1290. begin
  1291. writeln('TCustomTestModule.SetUp FModules<>nil');
  1292. Halt;
  1293. end;
  1294. inherited SetUp;
  1295. FSkipTests:=false;
  1296. FWithTypeInfo:=false;
  1297. FSource:=TStringList.Create;
  1298. FHub:=TPas2JSResolverHub.Create(Self);
  1299. FModules:=TObjectList.Create(true);
  1300. FFilename:='test1.pp';
  1301. FFileResolver:=TStreamResolver.Create;
  1302. FFileResolver.OwnsStreams:=True;
  1303. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1304. InitScanner(FScanner);
  1305. FEngine:=AddModule(Filename);
  1306. FEngine.Scanner:=FScanner;
  1307. FScanner.Resolver:=FEngine;
  1308. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1309. FParser.OnLog:=@OnParserLog;
  1310. FEngine.Parser:=FParser;
  1311. Parser.Options:=po_tcmodules;
  1312. FModule:=Nil;
  1313. FConverter:=CreateConverter;
  1314. FExpectedErrorClass:=nil;
  1315. end;
  1316. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1317. var
  1318. Options: TPasToJsConverterOptions;
  1319. begin
  1320. Result:=TPasToJSConverter.Create;
  1321. Options:=co_tcmodules;
  1322. if WithTypeInfo then
  1323. Exclude(Options,coNoTypeInfo)
  1324. else
  1325. Include(Options,coNoTypeInfo);
  1326. Result.Options:=Options;
  1327. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1328. end;
  1329. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1330. begin
  1331. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1332. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1333. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1334. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1335. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1336. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1337. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1338. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1339. aScanner.OnLog:=@OnScannerLog;
  1340. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1341. end;
  1342. procedure TCustomTestModule.TearDown;
  1343. {$IFDEF CheckPasTreeRefCount}
  1344. var
  1345. El: TPasElement;
  1346. {$ENDIF}
  1347. var
  1348. i: Integer;
  1349. CurModule: TPasModule;
  1350. begin
  1351. FHintMsgs.Clear;
  1352. FHintMsgsGood.Clear;
  1353. FSkipTests:=false;
  1354. FWithTypeInfo:=false;
  1355. FJSRegModuleCall:=nil;
  1356. FJSModuleCallArgs:=nil;
  1357. FJSImplentationUses:=nil;
  1358. FJSInterfaceUses:=nil;
  1359. FJSModuleSrc:=nil;
  1360. FJSInitBody:=nil;
  1361. FreeAndNil(FJSSource);
  1362. FreeAndNil(FJSModule);
  1363. FreeAndNil(FConverter);
  1364. Engine.Clear;
  1365. FreeAndNil(FSource);
  1366. FreeAndNil(FFileResolver);
  1367. if FModules<>nil then
  1368. begin
  1369. for i:=0 to FModules.Count-1 do
  1370. begin
  1371. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1372. if CurModule=nil then continue;
  1373. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1374. CurModule.ReleaseUsedUnits;
  1375. end;
  1376. if FModule<>nil then
  1377. FModule.ReleaseUsedUnits;
  1378. for i:=0 to FModules.Count-1 do
  1379. begin
  1380. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1381. if CurModule=nil then continue;
  1382. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1383. end;
  1384. FreeAndNil(FModules);
  1385. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1386. FEngine:=nil;
  1387. end;
  1388. FreeAndNil(FHub);
  1389. inherited TearDown;
  1390. {$IFDEF EnablePasTreeGlobalRefCount}
  1391. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1392. begin
  1393. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1394. {$IFDEF CheckPasTreeRefCount}
  1395. El:=TPasElement.FirstRefEl;
  1396. while El<>nil do
  1397. begin
  1398. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1399. for i:=0 to El.RefIds.Count-1 do
  1400. writeln(' ',El.RefIds[i]);
  1401. El:=El.NextRefEl;
  1402. end;
  1403. {$ENDIF}
  1404. Halt;
  1405. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1406. end;
  1407. {$ENDIF}
  1408. end;
  1409. procedure TCustomTestModule.Add(Line: string);
  1410. begin
  1411. Source.Add(Line);
  1412. end;
  1413. procedure TCustomTestModule.Add(const Lines: array of string);
  1414. var
  1415. i: Integer;
  1416. begin
  1417. for i:=low(Lines) to high(Lines) do
  1418. Add(Lines[i]);
  1419. end;
  1420. procedure TCustomTestModule.StartParsing;
  1421. var
  1422. Src: String;
  1423. begin
  1424. Src:=Source.Text;
  1425. FEngine.Source:=Src;
  1426. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1427. Scanner.OpenFile(FileName);
  1428. Writeln('// Test : ',Self.TestName);
  1429. Writeln(Src);
  1430. end;
  1431. procedure TCustomTestModule.ParseModuleQueue;
  1432. var
  1433. i: Integer;
  1434. CurResolver: TTestEnginePasResolver;
  1435. Found: Boolean;
  1436. Section: TPasSection;
  1437. begin
  1438. // parse til exception or all modules finished
  1439. while not SkipTests do
  1440. begin
  1441. Found:=false;
  1442. for i:=0 to ResolverCount-1 do
  1443. begin
  1444. CurResolver:=Resolvers[i];
  1445. if CurResolver.CurrentParser=nil then continue;
  1446. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1447. continue;
  1448. CurResolver.Parser.ParseContinue;
  1449. Found:=true;
  1450. break;
  1451. end;
  1452. if not Found then break;
  1453. end;
  1454. for i:=0 to ResolverCount-1 do
  1455. begin
  1456. CurResolver:=Resolvers[i];
  1457. if CurResolver.Parser=nil then
  1458. begin
  1459. if CurResolver.CurrentParser<>nil then
  1460. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1461. continue;
  1462. end;
  1463. if CurResolver.Parser.CurModule<>nil then
  1464. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1465. end;
  1466. end;
  1467. procedure TCustomTestModule.ParseModule;
  1468. begin
  1469. if SkipTests then exit;
  1470. FFirstPasStatement:=nil;
  1471. try
  1472. StartParsing;
  1473. Parser.ParseMain(FModule);
  1474. ParseModuleQueue;
  1475. except
  1476. on E: Exception do
  1477. HandleException(E);
  1478. end;
  1479. if SkipTests then exit;
  1480. AssertNotNull('Module resulted in Module',Module);
  1481. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1482. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1483. end;
  1484. procedure TCustomTestModule.ParseProgram;
  1485. begin
  1486. if SkipTests then exit;
  1487. ParseModule;
  1488. if SkipTests then exit;
  1489. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1490. FPasProgram:=TPasProgram(Module);
  1491. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1492. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1493. if (PasProgram.InitializationSection.Elements.Count>0) then
  1494. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1495. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1496. end;
  1497. procedure TCustomTestModule.ParseLibrary;
  1498. var
  1499. Init: TInitializationSection;
  1500. begin
  1501. if SkipTests then exit;
  1502. ParseModule;
  1503. if SkipTests then exit;
  1504. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1505. FPasLibrary:=TPasLibrary(Module);
  1506. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1507. Init:=PasLibrary.InitializationSection;
  1508. if (Init<>nil) and (Init.Elements.Count>0) then
  1509. if TObject(Init.Elements[0]) is TPasImplBlock then
  1510. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1511. end;
  1512. procedure TCustomTestModule.ParseUnit;
  1513. begin
  1514. if SkipTests then exit;
  1515. ParseModule;
  1516. if SkipTests then exit;
  1517. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1518. AssertNotNull('Has interface section',Module.InterfaceSection);
  1519. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1520. if (Module.InitializationSection<>nil)
  1521. and (Module.InitializationSection.Elements.Count>0)
  1522. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1523. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1524. end;
  1525. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1526. ): TTestEnginePasResolver;
  1527. var
  1528. i: Integer;
  1529. begin
  1530. for i:=0 to ResolverCount-1 do
  1531. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1532. exit(Resolvers[i]);
  1533. Result:=nil;
  1534. end;
  1535. function TCustomTestModule.AddModule(aFilename: string
  1536. ): TTestEnginePasResolver;
  1537. begin
  1538. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1539. if FindModuleWithFilename(aFilename)<>nil then
  1540. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1541. Result:=TTestEnginePasResolver.Create;
  1542. Result.Filename:=aFilename;
  1543. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1544. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1545. Result.OnLog:=@OnPasResolverLog;
  1546. Result.Hub:=Hub;
  1547. FModules.Add(Result);
  1548. end;
  1549. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1550. ): TTestEnginePasResolver;
  1551. begin
  1552. Result:=AddModule(aFilename);
  1553. Result.Source:=Src;
  1554. end;
  1555. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1556. ImplementationSrc: string): TTestEnginePasResolver;
  1557. var
  1558. Src: String;
  1559. begin
  1560. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1561. Src+=LineEnding;
  1562. Src+='interface'+LineEnding;
  1563. Src+=LineEnding;
  1564. Src+=InterfaceSrc;
  1565. Src+='implementation'+LineEnding;
  1566. Src+=LineEnding;
  1567. Src+=ImplementationSrc;
  1568. Src+='end.'+LineEnding;
  1569. Result:=AddModuleWithSrc(aFilename,Src);
  1570. end;
  1571. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1572. var
  1573. Intf, Impl: TStringList;
  1574. begin
  1575. Intf:=TStringList.Create;
  1576. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1577. // unit interface
  1578. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1579. Intf.Add('{$modeswitch externalclass}');
  1580. Intf.Add('type');
  1581. Intf.Add(' integer=longint;');
  1582. Intf.Add(' sizeint=nativeint;');
  1583. //'const',
  1584. //' LineEnding = #10;',
  1585. //' DirectorySeparator = ''/'';',
  1586. //' DriveSeparator = '''';',
  1587. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1588. //' AllowDriveSeparators : set of char = [];',
  1589. if supTObject in Parts then
  1590. Intf.AddStrings([
  1591. 'type',
  1592. ' TClass = class of TObject;',
  1593. ' TObject = class',
  1594. ' constructor Create;',
  1595. ' destructor Destroy; virtual;',
  1596. ' class function ClassType: TClass; assembler;',
  1597. ' class function ClassName: String; assembler;',
  1598. ' class function ClassNameIs(const Name: string): boolean;',
  1599. ' class function ClassParent: TClass; assembler;',
  1600. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1601. ' class function UnitName: String; assembler;',
  1602. ' procedure AfterConstruction; virtual;',
  1603. ' procedure BeforeDestruction;virtual;',
  1604. ' function Equals(Obj: TObject): boolean; virtual;',
  1605. ' function ToString: String; virtual;',
  1606. ' end;']);
  1607. if supTInterfacedObject in Parts then
  1608. Intf.AddStrings([
  1609. ' {$Interfaces COM}',
  1610. ' IUnknown = interface',
  1611. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1612. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1613. ' function _AddRef: Integer;',
  1614. ' function _Release: Integer;',
  1615. ' end;',
  1616. ' IInterface = IUnknown;',
  1617. ' TInterfacedObject = class(TObject,IUnknown)',
  1618. ' protected',
  1619. ' fRefCount: Integer;',
  1620. ' { implement methods of IUnknown }',
  1621. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1622. ' function _AddRef: Integer; virtual;',
  1623. ' function _Release: Integer; virtual;',
  1624. ' end;',
  1625. ' TInterfacedClass = class of TInterfacedObject;',
  1626. '',
  1627. '']);
  1628. if supTVarRec in Parts then
  1629. Intf.AddStrings([
  1630. 'const',
  1631. ' vtInteger = 0;',
  1632. ' vtBoolean = 1;',
  1633. ' vtJSValue = 19;',
  1634. 'type',
  1635. ' PVarRec = ^TVarRec;',
  1636. ' TVarRec = record',
  1637. ' VType : byte;',
  1638. ' VJSValue: JSValue;',
  1639. ' vInteger: longint external name ''VJSValue'';',
  1640. ' vBoolean: boolean external name ''VJSValue'';',
  1641. ' end;',
  1642. ' TVarRecArray = array of TVarRec;',
  1643. 'function VarRecs: TVarRecArray; varargs;',
  1644. '']);
  1645. if supTypeInfo in Parts then
  1646. begin
  1647. Intf.AddStrings([
  1648. 'type',
  1649. ' TTypeKind = (',
  1650. ' tkUnknown, // 0',
  1651. ' tkInteger, // 1',
  1652. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1653. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1654. ' tkEnumeration, // 4',
  1655. ' tkSet, // 5',
  1656. ' tkDouble, // 6',
  1657. ' tkBool, // 7',
  1658. ' tkProcVar, // 8 function or procedure',
  1659. ' tkMethod, // 9 proc var of object',
  1660. ' tkArray, // 10 static array',
  1661. ' tkDynArray, // 11',
  1662. ' tkRecord, // 12',
  1663. ' tkClass, // 13',
  1664. ' tkClassRef, // 14',
  1665. ' tkPointer, // 15',
  1666. ' tkJSValue, // 16',
  1667. ' tkRefToProcVar, // 17 variable of procedure type',
  1668. ' tkInterface, // 18',
  1669. ' //tkObject,',
  1670. ' //tkSString,tkLString,tkAString,tkWString,',
  1671. ' //tkVariant,',
  1672. ' //tkWChar,',
  1673. ' //tkInt64,',
  1674. ' //tkQWord,',
  1675. ' //tkInterfaceRaw,',
  1676. ' //tkUString,tkUChar,',
  1677. ' tkHelper, // 19',
  1678. ' //tkFile,',
  1679. ' tkExtClass // 20',
  1680. ' );',
  1681. ' TTypeKinds = set of TTypeKind;',
  1682. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1683. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1684. ' end;',
  1685. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1686. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1687. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1688. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1689. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1690. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1691. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1692. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1693. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1694. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1695. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1696. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1697. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1698. '']);
  1699. end;
  1700. if supWriteln in Parts then
  1701. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1702. Intf.Add('var');
  1703. Intf.Add(' ExitCode: Longint = 0;');
  1704. // unit implementation
  1705. Impl:=TStringList.Create;
  1706. if supTObject in Parts then
  1707. Impl.AddStrings([
  1708. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1709. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1710. 'asm',
  1711. 'end;',
  1712. 'constructor TObject.Create; begin end;',
  1713. 'destructor TObject.Destroy; begin end;',
  1714. 'class function TObject.ClassType: TClass; assembler;',
  1715. 'asm',
  1716. 'end;',
  1717. 'class function TObject.ClassName: String; assembler;',
  1718. 'asm',
  1719. 'end;',
  1720. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1721. 'begin',
  1722. ' Result:=SameText(Name,ClassName);',
  1723. 'end;',
  1724. 'class function TObject.ClassParent: TClass; assembler;',
  1725. 'asm',
  1726. 'end;',
  1727. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1728. 'asm',
  1729. 'end;',
  1730. 'class function TObject.UnitName: String; assembler;',
  1731. 'asm',
  1732. 'end;',
  1733. 'procedure TObject.AfterConstruction; begin end;',
  1734. 'procedure TObject.BeforeDestruction; begin end;',
  1735. 'function TObject.Equals(Obj: TObject): boolean;',
  1736. 'begin',
  1737. ' Result:=Obj=Self;',
  1738. 'end;',
  1739. 'function TObject.ToString: String;',
  1740. 'begin',
  1741. ' Result:=ClassName;',
  1742. 'end;'
  1743. ]);
  1744. if supTInterfacedObject in Parts then
  1745. Impl.AddStrings([
  1746. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1747. //'begin',
  1748. //'end;',
  1749. 'function TInterfacedObject._AddRef: Integer;',
  1750. 'begin',
  1751. 'end;',
  1752. 'function TInterfacedObject._Release: Integer;',
  1753. 'begin',
  1754. 'end;',
  1755. '']);
  1756. if supTVarRec in Parts then
  1757. Impl.AddStrings([
  1758. 'function VarRecs: TVarRecArray; varargs;',
  1759. 'var',
  1760. ' v: PVarRec;',
  1761. 'begin',
  1762. ' v^.VType:=1;',
  1763. ' v^.VJSValue:=2;',
  1764. 'end;',
  1765. '']);
  1766. try
  1767. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1768. finally
  1769. Intf.Free;
  1770. Impl.Free;
  1771. end;
  1772. end;
  1773. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1774. SystemUnitParts: TSystemUnitParts);
  1775. begin
  1776. if NeedSystemUnit then
  1777. AddSystemUnit(SystemUnitParts)
  1778. else
  1779. Parser.ImplicitUses.Clear;
  1780. Add('program '+ExtractFileUnitName(Filename)+';');
  1781. Add('');
  1782. end;
  1783. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1784. SystemUnitParts: TSystemUnitParts);
  1785. begin
  1786. if NeedSystemUnit then
  1787. AddSystemUnit(SystemUnitParts)
  1788. else
  1789. Parser.ImplicitUses.Clear;
  1790. Add('library '+ExtractFileUnitName(Filename)+';');
  1791. Add('');
  1792. end;
  1793. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1794. SystemUnitParts: TSystemUnitParts);
  1795. begin
  1796. if NeedSystemUnit then
  1797. AddSystemUnit(SystemUnitParts)
  1798. else
  1799. Parser.ImplicitUses.Clear;
  1800. Add('unit Test1;');
  1801. Add('');
  1802. end;
  1803. procedure TCustomTestModule.ConvertModule;
  1804. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1805. out UsesLit: TJSArrayLiteral);
  1806. var
  1807. i: Integer;
  1808. Item: TJSElement;
  1809. Lit: TJSLiteral;
  1810. begin
  1811. UsesLit:=nil;
  1812. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1813. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1814. exit; // null is ok
  1815. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1816. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1817. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1818. begin
  1819. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1820. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1821. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1822. Lit:=TJSLiteral(Item);
  1823. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1824. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1825. end;
  1826. end;
  1827. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1828. out Src: TJSSourceElements);
  1829. var
  1830. FunDecl: TJSFunctionDeclarationStatement;
  1831. FunDef: TJSFuncDef;
  1832. FunBody: TJSFunctionBody;
  1833. begin
  1834. Src:=nil;
  1835. AssertNotNull(ParamName,Arg.Expr);
  1836. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1837. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1838. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1839. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1840. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1841. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1842. AssertNotNull(ParamName+' body',FunDef.Body);
  1843. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1844. FunBody:=FunDef.Body as TJSFunctionBody;
  1845. AssertNotNull(ParamName+' body.A',FunBody.A);
  1846. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1847. Src:=FunBody.A as TJSSourceElements;
  1848. end;
  1849. var
  1850. ModuleNameExpr: TJSLiteral;
  1851. InitFunction: TJSFunctionDeclarationStatement;
  1852. InitAssign: TJSSimpleAssignStatement;
  1853. InitName: String;
  1854. LastNode: TJSElement;
  1855. Arg: TJSArrayLiteralElement;
  1856. begin
  1857. if SkipTests then exit;
  1858. try
  1859. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1860. except
  1861. on E: Exception do
  1862. HandleException(E);
  1863. end;
  1864. if SkipTests then exit;
  1865. if ExpectedErrorClass<>nil then
  1866. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1867. FJSSource:=TStringList.Create;
  1868. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1869. {$IFDEF VerbosePas2JS}
  1870. writeln('TTestModule.ConvertModule JS:');
  1871. write(FJSSource.Text);
  1872. {$ENDIF}
  1873. // rtl.module(...
  1874. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1875. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1876. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1877. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1878. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1879. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1880. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1881. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1882. // parameter 'unitname'
  1883. if JSModuleCallArgs.Elements.Count<1 then
  1884. Fail('rtl.module first param unit missing');
  1885. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1886. AssertNotNull('module name param',Arg.Expr);
  1887. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1888. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1889. if Module is TPasProgram then
  1890. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1891. else if Module is TPasLibrary then
  1892. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1893. else
  1894. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1895. // main uses section
  1896. if JSModuleCallArgs.Elements.Count<2 then
  1897. Fail('rtl.module second param main uses missing');
  1898. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1899. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1900. // program/library/interface function()
  1901. if JSModuleCallArgs.Elements.Count<3 then
  1902. Fail('rtl.module third param intf-function missing');
  1903. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1904. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1905. // search for $mod.$init or $mod.$main - the last statement
  1906. if (Module is TPasProgram) or (Module is TPasLibrary) then
  1907. begin
  1908. InitName:='$main';
  1909. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1910. end
  1911. else
  1912. InitName:='$init';
  1913. FJSInitBody:=nil;
  1914. if JSModuleSrc.Statements.Count>0 then
  1915. begin
  1916. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1917. if LastNode is TJSSimpleAssignStatement then
  1918. begin
  1919. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1920. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1921. begin
  1922. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1923. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1924. end
  1925. else if (Module is TPasProgram) or (Module is TPasLibrary) then
  1926. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1927. end;
  1928. end;
  1929. // optional: implementation uses section
  1930. if JSModuleCallArgs.Elements.Count<4 then
  1931. exit;
  1932. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1933. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1934. end;
  1935. procedure TCustomTestModule.ConvertProgram;
  1936. begin
  1937. Add('end.');
  1938. ParseProgram;
  1939. ConvertModule;
  1940. end;
  1941. procedure TCustomTestModule.ConvertLibrary;
  1942. begin
  1943. Add('end.');
  1944. ParseLibrary;
  1945. ConvertModule;
  1946. end;
  1947. procedure TCustomTestModule.ConvertUnit;
  1948. begin
  1949. Add('end.');
  1950. ParseUnit;
  1951. ConvertModule;
  1952. end;
  1953. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1954. begin
  1955. Result:=tcmodules.JSToStr(El);
  1956. end;
  1957. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1958. DottedName: string);
  1959. begin
  1960. if DottedName='' then
  1961. begin
  1962. AssertNull(Msg,El);
  1963. end
  1964. else
  1965. begin
  1966. AssertNotNull(Msg,El);
  1967. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1968. end;
  1969. end;
  1970. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1971. begin
  1972. if El=nil then
  1973. Result:=''
  1974. else if El is TJSPrimaryExpressionIdent then
  1975. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1976. else if El is TJSDotMemberExpression then
  1977. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1978. else
  1979. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1980. end;
  1981. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1982. InitStatements: string; ImplStatements: string);
  1983. var
  1984. ActualSrc, ExpectedSrc, InitName: String;
  1985. begin
  1986. ActualSrc:=JSToStr(JSModuleSrc);
  1987. if coUseStrict in Converter.Options then
  1988. ExpectedSrc:='"use strict";'+LineEnding
  1989. else
  1990. ExpectedSrc:='';
  1991. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1992. ExpectedSrc:=ExpectedSrc+Statements;
  1993. // unit implementation
  1994. if (Trim(ImplStatements)<>'') then
  1995. ExpectedSrc:=ExpectedSrc+LineEnding
  1996. +'$mod.$implcode = function () {'+LineEnding
  1997. +ImplStatements
  1998. +'};'+LineEnding;
  1999. // program main or unit initialization
  2000. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  2001. begin
  2002. if (Module is TPasProgram) or (Module is TPasLibrary) then
  2003. InitName:='$main'
  2004. else
  2005. InitName:='$init';
  2006. ExpectedSrc:=ExpectedSrc+LineEnding
  2007. +'$mod.'+InitName+' = function () {'+LineEnding
  2008. +InitStatements
  2009. +'};'+LineEnding;
  2010. end;
  2011. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2012. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2013. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2014. end;
  2015. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2016. // search diff, ignore changes in spaces
  2017. var
  2018. s: string;
  2019. begin
  2020. if CheckSrcDiff(Expected,Actual,s) then exit;
  2021. Fail(Msg+': '+s);
  2022. end;
  2023. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2024. var
  2025. aResolver: TTestEnginePasResolver;
  2026. aConverter: TPasToJSConverter;
  2027. aJSModule: TJSSourceElements;
  2028. ActualSrc: String;
  2029. begin
  2030. aResolver:=GetResolver(Filename);
  2031. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2032. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2033. {$IFDEF VerbosePas2JS}
  2034. writeln('CheckUnit '+Filename+' converting ...');
  2035. {$ENDIF}
  2036. aConverter:=CreateConverter;
  2037. aJSModule:=nil;
  2038. try
  2039. try
  2040. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2041. except
  2042. on E: Exception do
  2043. HandleException(E);
  2044. end;
  2045. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2046. {$IFDEF VerbosePas2JS}
  2047. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2048. write(aResolver.Source);
  2049. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2050. write(ActualSrc);
  2051. {$ENDIF}
  2052. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2053. finally
  2054. aJSModule.Free;
  2055. aConverter.Free;
  2056. end;
  2057. end;
  2058. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2059. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2060. var
  2061. i: Integer;
  2062. Item: TTestHintMessage;
  2063. Expected,Actual: string;
  2064. begin
  2065. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2066. for i:=0 to MsgCount-1 do
  2067. begin
  2068. Item:=Msgs[i];
  2069. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2070. if (Marker<>nil) then
  2071. begin
  2072. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2073. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2074. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2075. end;
  2076. // found
  2077. FHintMsgsGood.Add(Item);
  2078. str(Item.MsgType,Actual);
  2079. str(MsgType,Expected);
  2080. AssertEquals('MsgType',Expected,Actual);
  2081. exit;
  2082. end;
  2083. // needed message missing -> show emitted messages
  2084. WriteSources('',0,0);
  2085. for i:=0 to MsgCount-1 do
  2086. begin
  2087. Item:=Msgs[i];
  2088. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2089. ' ('+IntToStr(Item.MsgNumber),')');
  2090. if Marker<>nil then
  2091. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2092. writeln(' {',Item.Msg,'}');
  2093. end;
  2094. str(MsgType,Expected);
  2095. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2096. if Marker<>nil then
  2097. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2098. Actual:=Actual+' '+Msg;
  2099. Fail(Actual);
  2100. end;
  2101. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2102. );
  2103. var
  2104. i: Integer;
  2105. s, Txt: String;
  2106. Msg: TTestHintMessage;
  2107. begin
  2108. for i:=0 to MsgCount-1 do
  2109. begin
  2110. Msg:=Msgs[i];
  2111. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2112. s:='';
  2113. str(Msg.MsgType,s);
  2114. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2115. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2116. if WithSourcePos then
  2117. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2118. Txt:=Txt+' {'+Msg.Msg+'}';
  2119. Fail(Txt);
  2120. end;
  2121. end;
  2122. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2123. MsgNumber: integer);
  2124. begin
  2125. ExpectedErrorClass:=EScannerError;
  2126. ExpectedErrorMsg:=Msg;
  2127. ExpectedErrorNumber:=MsgNumber;
  2128. end;
  2129. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2130. MsgNumber: integer);
  2131. begin
  2132. ExpectedErrorClass:=EParserError;
  2133. ExpectedErrorMsg:=Msg;
  2134. ExpectedErrorNumber:=MsgNumber;
  2135. end;
  2136. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2137. MsgNumber: integer);
  2138. begin
  2139. ExpectedErrorClass:=EPasResolve;
  2140. ExpectedErrorMsg:=Msg;
  2141. ExpectedErrorNumber:=MsgNumber;
  2142. end;
  2143. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2144. MsgNumber: integer);
  2145. begin
  2146. ExpectedErrorClass:=EPas2JS;
  2147. ExpectedErrorMsg:=Msg;
  2148. ExpectedErrorNumber:=MsgNumber;
  2149. end;
  2150. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2151. var
  2152. MsgNumber: Integer;
  2153. Msg: String;
  2154. begin
  2155. Result:=false;
  2156. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2157. Msg:=E.Message;
  2158. if E is EPas2JS then
  2159. MsgNumber:=EPas2JS(E).MsgNumber
  2160. else if E is EPasResolve then
  2161. MsgNumber:=EPasResolve(E).MsgNumber
  2162. else if E is EParserError then
  2163. MsgNumber:=Parser.LastMsgNumber
  2164. else if E is EScannerError then
  2165. begin
  2166. MsgNumber:=Scanner.LastMsgNumber;
  2167. Msg:=Scanner.LastMsg;
  2168. end
  2169. else
  2170. MsgNumber:=0;
  2171. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2172. if Result then
  2173. SkipTests:=true;
  2174. end;
  2175. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2176. begin
  2177. if IsErrorExpected(E) then exit;
  2178. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2179. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2180. +' '+Scanner.CurFilename
  2181. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2182. FailException(E);
  2183. end;
  2184. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2185. begin
  2186. if IsErrorExpected(E) then exit;
  2187. WriteSources(E.Filename,E.Row,E.Column);
  2188. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2189. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2190. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2191. );
  2192. FailException(E);
  2193. end;
  2194. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2195. var
  2196. P: TPasSourcePos;
  2197. begin
  2198. if IsErrorExpected(E) then exit;
  2199. P:=E.SourcePos;
  2200. WriteSources(P.FileName,P.Row,P.Column);
  2201. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2202. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2203. FailException(E);
  2204. end;
  2205. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2206. var
  2207. Row, Col: integer;
  2208. begin
  2209. if IsErrorExpected(E) then exit;
  2210. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2211. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2212. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2213. +' '+E.PasElement.SourceFilename
  2214. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2215. FailException(E);
  2216. end;
  2217. procedure TCustomTestModule.HandleException(E: Exception);
  2218. begin
  2219. if E is EScannerError then
  2220. HandleScannerError(EScannerError(E))
  2221. else if E is EParserError then
  2222. HandleParserError(EParserError(E))
  2223. else if E is EPasResolve then
  2224. HandlePasResolveError(EPasResolve(E))
  2225. else if E is EPas2JS then
  2226. HandlePas2JSError(EPas2JS(E))
  2227. else
  2228. begin
  2229. if IsErrorExpected(E) then exit;
  2230. if not (E is EAssertionFailedError) then
  2231. begin
  2232. WriteSources('',0,0);
  2233. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2234. end;
  2235. FailException(E);
  2236. end;
  2237. end;
  2238. procedure TCustomTestModule.FailException(E: Exception);
  2239. var
  2240. MsgNumber: Integer;
  2241. begin
  2242. if ExpectedErrorClass<>nil then
  2243. begin
  2244. if FExpectedErrorClass=E.ClassType then
  2245. begin
  2246. if E is EPas2JS then
  2247. MsgNumber:=EPas2JS(E).MsgNumber
  2248. else if E is EPasResolve then
  2249. MsgNumber:=EPasResolve(E).MsgNumber
  2250. else if E is EParserError then
  2251. MsgNumber:=Parser.LastMsgNumber
  2252. else if E is EScannerError then
  2253. MsgNumber:=Scanner.LastMsgNumber
  2254. else
  2255. MsgNumber:=0;
  2256. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2257. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2258. ExpectedErrorNumber,MsgNumber);
  2259. end else begin
  2260. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2261. end;
  2262. end;
  2263. Fail(E.Message);
  2264. end;
  2265. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2266. aCol: integer);
  2267. var
  2268. IsSrc: Boolean;
  2269. i, j: Integer;
  2270. SrcLines: TStringList;
  2271. Line: string;
  2272. aModule: TTestEnginePasResolver;
  2273. begin
  2274. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2275. for i:=0 to ResolverCount-1 do
  2276. begin
  2277. aModule:=Resolvers[i];
  2278. SrcLines:=TStringList.Create;
  2279. try
  2280. SrcLines.Text:=aModule.Source;
  2281. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2282. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2283. for j:=1 to SrcLines.Count do
  2284. begin
  2285. Line:=SrcLines[j-1];
  2286. if IsSrc and (j=aRow) then
  2287. begin
  2288. write('*');
  2289. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2290. end;
  2291. writeln(Format('%:4d: ',[j]),Line);
  2292. end;
  2293. finally
  2294. SrcLines.Free;
  2295. end;
  2296. end;
  2297. end;
  2298. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2299. var
  2300. i: Integer;
  2301. begin
  2302. for i:=0 to ResolverCount-1 do
  2303. if Filename=Resolvers[i].Filename then exit(i);
  2304. Result:=-1;
  2305. end;
  2306. function TCustomTestModule.GetResolver(const Filename: string
  2307. ): TTestEnginePasResolver;
  2308. var
  2309. i: Integer;
  2310. begin
  2311. i:=IndexOfResolver(Filename);
  2312. if i<0 then exit(nil);
  2313. Result:=Resolvers[i];
  2314. end;
  2315. function TCustomTestModule.GetDefaultNamespace: string;
  2316. var
  2317. C: TClass;
  2318. begin
  2319. Result:='';
  2320. if FModule=nil then exit;
  2321. C:=FModule.ClassType;
  2322. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2323. Result:=Engine.DefaultNameSpace;
  2324. end;
  2325. constructor TCustomTestModule.Create;
  2326. begin
  2327. inherited Create;
  2328. FHintMsgs:=TObjectList.Create(true);
  2329. FHintMsgsGood:=TFPList.Create;
  2330. end;
  2331. destructor TCustomTestModule.Destroy;
  2332. begin
  2333. FreeAndNil(FHintMsgs);
  2334. FreeAndNil(FHintMsgsGood);
  2335. inherited Destroy;
  2336. end;
  2337. { TTestModule }
  2338. procedure TTestModule.TestReservedWords;
  2339. var
  2340. i: integer;
  2341. begin
  2342. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2343. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2344. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2345. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2346. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2347. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2348. end;
  2349. procedure TTestModule.TestEmptyProgram;
  2350. begin
  2351. StartProgram(false);
  2352. Add('begin');
  2353. ConvertProgram;
  2354. CheckSource('TestEmptyProgram','','');
  2355. end;
  2356. procedure TTestModule.TestEmptyProgramUseStrict;
  2357. begin
  2358. Converter.Options:=Converter.Options+[coUseStrict];
  2359. StartProgram(false);
  2360. Add('begin');
  2361. ConvertProgram;
  2362. CheckSource('TestEmptyProgramUseStrict','','');
  2363. end;
  2364. procedure TTestModule.TestEmptyUnit;
  2365. begin
  2366. StartUnit(false);
  2367. Add('interface');
  2368. Add('implementation');
  2369. ConvertUnit;
  2370. CheckSource('TestEmptyUnit',
  2371. LinesToStr([
  2372. ]),
  2373. '');
  2374. end;
  2375. procedure TTestModule.TestEmptyUnitUseStrict;
  2376. begin
  2377. Converter.Options:=Converter.Options+[coUseStrict];
  2378. StartUnit(false);
  2379. Add('interface');
  2380. Add('implementation');
  2381. ConvertUnit;
  2382. CheckSource('TestEmptyUnitUseStrict',
  2383. LinesToStr([
  2384. ''
  2385. ]),
  2386. '');
  2387. end;
  2388. procedure TTestModule.TestDottedUnitNames;
  2389. begin
  2390. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2391. LinesToStr([
  2392. 'var iV: longint;'
  2393. ]),
  2394. '');
  2395. FFilename:='ns1.test1.pp';
  2396. StartProgram(true);
  2397. Add('uses unIt2;');
  2398. Add('var');
  2399. Add(' i: longint;');
  2400. Add('begin');
  2401. Add(' i:=iv;');
  2402. Add(' i:=uNit2.iv;');
  2403. Add(' i:=Ns1.TEst1.i;');
  2404. ConvertProgram;
  2405. CheckSource('TestDottedUnitNames',
  2406. LinesToStr([
  2407. 'this.i = 0;',
  2408. '']),
  2409. LinesToStr([ // this.$init
  2410. '$mod.i = pas["NS1.Unit2"].iV;',
  2411. '$mod.i = pas["NS1.Unit2"].iV;',
  2412. '$mod.i = $mod.i;',
  2413. '']) );
  2414. end;
  2415. procedure TTestModule.TestDottedUnitNameImpl;
  2416. begin
  2417. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2418. LinesToStr([
  2419. 'type',
  2420. ' TObject = class end;',
  2421. ' TTestA = class',
  2422. ' end;'
  2423. ]),
  2424. LinesToStr(['uses TEST.UnitB;'])
  2425. );
  2426. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2427. LinesToStr([
  2428. 'uses TEST.UnitA;',
  2429. 'type TTestB = class(TTestA);'
  2430. ]),
  2431. ''
  2432. );
  2433. StartProgram(true);
  2434. Add('uses TEST.UnitA;');
  2435. Add('begin');
  2436. ConvertProgram;
  2437. CheckSource('TestDottedUnitNameImpl',
  2438. LinesToStr([
  2439. '']),
  2440. LinesToStr([ // this.$init
  2441. '']) );
  2442. CheckUnit('TEST.UnitA.pas',
  2443. LinesToStr([
  2444. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2445. ' var $mod = this;',
  2446. ' rtl.createClass(this, "TObject", null, function () {',
  2447. ' this.$init = function () {',
  2448. ' };',
  2449. ' this.$final = function () {',
  2450. ' };',
  2451. ' });',
  2452. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2453. ' });',
  2454. '}, ["TEST.UnitB"]);'
  2455. ]));
  2456. CheckUnit('TEST.UnitB.pas',
  2457. LinesToStr([
  2458. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2459. ' var $mod = this;',
  2460. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2461. ' });',
  2462. '});'
  2463. ]));
  2464. end;
  2465. procedure TTestModule.TestDottedUnitExpr;
  2466. begin
  2467. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2468. LinesToStr([
  2469. 'procedure DoIt;'
  2470. ]),
  2471. 'procedure DoIt; begin end;');
  2472. FFilename:='Ns1.SubNs1.Test1.pp';
  2473. StartProgram(true);
  2474. Add('uses Ns2.sUbnS2.unIt2;');
  2475. Add('var');
  2476. Add(' i: longint;');
  2477. Add('begin');
  2478. Add(' ns2.subns2.unit2.doit;');
  2479. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2480. ConvertProgram;
  2481. CheckSource('TestDottedUnitExpr',
  2482. LinesToStr([
  2483. 'this.i = 0;',
  2484. '']),
  2485. LinesToStr([ // this.$init
  2486. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2487. '$mod.i = $mod.i;',
  2488. '']) );
  2489. end;
  2490. procedure TTestModule.Test_ModeFPCFail;
  2491. begin
  2492. StartProgram(false);
  2493. Add('{$mode FPC}');
  2494. Add('begin');
  2495. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2496. ConvertProgram;
  2497. end;
  2498. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2499. begin
  2500. StartProgram(false);
  2501. Add('{$modeswitch cblocks-}');
  2502. Add('begin');
  2503. ConvertProgram;
  2504. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2505. CheckResolverUnexpectedHints();
  2506. end;
  2507. procedure TTestModule.TestUnit_UseSystem;
  2508. begin
  2509. StartUnit(true);
  2510. Add([
  2511. 'interface',
  2512. 'var i: integer;',
  2513. 'implementation']);
  2514. ConvertUnit;
  2515. CheckSource('TestUnit_UseSystem',
  2516. LinesToStr([
  2517. 'this.i = 0;',
  2518. '']),
  2519. LinesToStr([
  2520. '']) );
  2521. end;
  2522. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2523. begin
  2524. AddModuleWithIntfImplSrc('unit1.pp',
  2525. LinesToStr([
  2526. 'type number = longint;']),
  2527. LinesToStr([
  2528. 'uses test1;',
  2529. 'procedure DoIt;',
  2530. 'begin',
  2531. ' i:=3;',
  2532. 'end;']));
  2533. StartUnit(true);
  2534. Add([
  2535. 'interface',
  2536. 'uses unit1;',
  2537. 'var i: number;',
  2538. 'implementation']);
  2539. ConvertUnit;
  2540. CheckSource('TestUnit_Intf1Impl2Intf1',
  2541. LinesToStr([
  2542. 'this.i = 0;',
  2543. '']),
  2544. LinesToStr([
  2545. '']) );
  2546. end;
  2547. procedure TTestModule.TestIncludeVersion;
  2548. begin
  2549. StartProgram(false);
  2550. Add([
  2551. 'var',
  2552. ' s: string;',
  2553. ' i: word;',
  2554. 'begin',
  2555. ' s:={$I %line%};',
  2556. ' i:={$I %linenum%};',
  2557. ' s:={$I %currentroutine%};',
  2558. ' s:={$I %pas2jsversion%};',
  2559. ' s:={$I %pas2jstarget%};',
  2560. ' s:={$I %pas2jstargetos%};',
  2561. ' s:={$I %pas2jstargetcpu%};',
  2562. ' s:={$I %file%};',
  2563. '']);
  2564. ConvertProgram;
  2565. CheckSource('TestIncludeVersion',
  2566. LinesToStr([
  2567. 'this.s="";',
  2568. 'this.i = 0;']),
  2569. LinesToStr([
  2570. '$mod.s = "7";',
  2571. '$mod.i = 8;',
  2572. '$mod.s = "<anonymous>";',
  2573. '$mod.s = "Comp.Ver.tcmodules";',
  2574. '$mod.s = "Browser";',
  2575. '$mod.s = "Browser";',
  2576. '$mod.s = "ECMAScript5";',
  2577. '$mod.s = "test1.pp";',
  2578. '']));
  2579. end;
  2580. procedure TTestModule.TestVarInt;
  2581. begin
  2582. StartProgram(false);
  2583. Add('var MyI: longint;');
  2584. Add('begin');
  2585. ConvertProgram;
  2586. CheckSource('TestVarInt','this.MyI=0;','');
  2587. end;
  2588. procedure TTestModule.TestVarBaseTypes;
  2589. begin
  2590. StartProgram(false);
  2591. Add('var');
  2592. Add(' i: longint;');
  2593. Add(' s: string;');
  2594. Add(' c: char;');
  2595. Add(' b: boolean;');
  2596. Add(' d: double;');
  2597. Add(' i2: longint = 3;');
  2598. Add(' s2: string = ''foo'';');
  2599. Add(' c2: char = ''4'';');
  2600. Add(' b2: boolean = true;');
  2601. Add(' d2: double = 5.6;');
  2602. Add(' i3: longint = $707;');
  2603. Add(' i4: nativeint = 9007199254740991;');
  2604. Add(' i5: nativeint = -9007199254740991-1;');
  2605. Add(' i6: nativeint = $fffffffffffff;');
  2606. Add(' i7: nativeint = -$fffffffffffff-1;');
  2607. Add(' i8: byte = 00;');
  2608. Add(' u8: nativeuint = $fffffffffffff;');
  2609. Add(' u9: nativeuint = $0000000000000;');
  2610. Add(' u10: nativeuint = $00ff00;');
  2611. Add('begin');
  2612. ConvertProgram;
  2613. CheckSource('TestVarBaseTypes',
  2614. LinesToStr([
  2615. 'this.i = 0;',
  2616. 'this.s = "";',
  2617. 'this.c = "";',
  2618. 'this.b = false;',
  2619. 'this.d = 0.0;',
  2620. 'this.i2 = 3;',
  2621. 'this.s2 = "foo";',
  2622. 'this.c2 = "4";',
  2623. 'this.b2 = true;',
  2624. 'this.d2 = 5.6;',
  2625. 'this.i3 = 0x707;',
  2626. 'this.i4 = 9007199254740991;',
  2627. 'this.i5 = -9007199254740991-1;',
  2628. 'this.i6 = 0xfffffffffffff;',
  2629. 'this.i7 =-0xfffffffffffff-1;',
  2630. 'this.i8 = 0;',
  2631. 'this.u8 = 0xfffffffffffff;',
  2632. 'this.u9 = 0x0;',
  2633. 'this.u10 = 0xff00;'
  2634. ]),
  2635. '');
  2636. end;
  2637. procedure TTestModule.TestBaseTypeSingleFail;
  2638. begin
  2639. StartProgram(false);
  2640. Add('var s: single;');
  2641. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2642. ConvertProgram;
  2643. end;
  2644. procedure TTestModule.TestBaseTypeExtendedFail;
  2645. begin
  2646. StartProgram(false);
  2647. Add('var e: extended;');
  2648. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2649. ConvertProgram;
  2650. end;
  2651. procedure TTestModule.TestConstBaseTypes;
  2652. begin
  2653. StartProgram(false);
  2654. Add('const');
  2655. Add(' i: longint = 3;');
  2656. Add(' s: string = ''foo'';');
  2657. Add(' c: char = ''4'';');
  2658. Add(' b: boolean = true;');
  2659. Add(' d: double = 5.6;');
  2660. Add(' e = low(word);');
  2661. Add(' f = high(word);');
  2662. Add('begin');
  2663. ConvertProgram;
  2664. CheckSource('TestVarBaseTypes',
  2665. LinesToStr([
  2666. 'this.i=3;',
  2667. 'this.s="foo";',
  2668. 'this.c="4";',
  2669. 'this.b=true;',
  2670. 'this.d=5.6;',
  2671. 'this.e = 0;',
  2672. 'this.f = 65535;'
  2673. ]),
  2674. '');
  2675. end;
  2676. procedure TTestModule.TestAliasTypeRef;
  2677. begin
  2678. StartProgram(false);
  2679. Add('type');
  2680. Add(' a=longint;');
  2681. Add(' b=a;');
  2682. Add('var');
  2683. Add(' c: A;');
  2684. Add(' d: B;');
  2685. Add('begin');
  2686. ConvertProgram;
  2687. CheckSource('TestAliasTypeRef',
  2688. LinesToStr([ // statements
  2689. 'this.c = 0;',
  2690. 'this.d = 0;'
  2691. ]),
  2692. LinesToStr([ // this.$main
  2693. ''
  2694. ]));
  2695. end;
  2696. procedure TTestModule.TestTypeCast_BaseTypes;
  2697. begin
  2698. StartProgram(false);
  2699. Add([
  2700. 'var',
  2701. ' i: longint;',
  2702. ' b: boolean;',
  2703. ' d: double;',
  2704. ' s: string;',
  2705. ' c: char;',
  2706. 'begin',
  2707. ' i:=longint(i);',
  2708. ' i:=longint(b);',
  2709. ' b:=boolean(b);',
  2710. ' b:=boolean(i);',
  2711. ' d:=double(d);',
  2712. ' d:=double(i);',
  2713. ' s:=string(s);',
  2714. ' s:=string(c);',
  2715. ' c:=char(c);',
  2716. ' c:=char(i);',
  2717. ' c:=char(65);',
  2718. ' c:=char(#10);',
  2719. ' c:=char(#$E000);',
  2720. '']);
  2721. ConvertProgram;
  2722. CheckSource('TestAliasTypeRef',
  2723. LinesToStr([ // statements
  2724. 'this.i = 0;',
  2725. 'this.b = false;',
  2726. 'this.d = 0.0;',
  2727. 'this.s = "";',
  2728. 'this.c = "";',
  2729. '']),
  2730. LinesToStr([ // this.$main
  2731. '$mod.i = $mod.i;',
  2732. '$mod.i = ($mod.b ? 1 : 0);',
  2733. '$mod.b = $mod.b;',
  2734. '$mod.b = $mod.i != 0;',
  2735. '$mod.d = $mod.d;',
  2736. '$mod.d = $mod.i;',
  2737. '$mod.s = $mod.s;',
  2738. '$mod.s = $mod.c;',
  2739. '$mod.c = $mod.c;',
  2740. '$mod.c = String.fromCharCode($mod.i);',
  2741. '$mod.c = "A";',
  2742. '$mod.c = "\n";',
  2743. '$mod.c = "";',
  2744. '']));
  2745. end;
  2746. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2747. begin
  2748. StartProgram(false);
  2749. Add('type');
  2750. Add(' integer = longint;');
  2751. Add(' TYesNo = boolean;');
  2752. Add(' TFloat = double;');
  2753. Add(' TCaption = string;');
  2754. Add(' TChar = char;');
  2755. Add('var');
  2756. Add(' i: integer;');
  2757. Add(' b: TYesNo;');
  2758. Add(' d: TFloat;');
  2759. Add(' s: TCaption;');
  2760. Add(' c: TChar;');
  2761. Add('begin');
  2762. Add(' i:=integer(i);');
  2763. Add(' i:=integer(b);');
  2764. Add(' b:=TYesNo(b);');
  2765. Add(' b:=TYesNo(i);');
  2766. Add(' d:=TFloat(d);');
  2767. Add(' d:=TFloat(i);');
  2768. Add(' s:=TCaption(s);');
  2769. Add(' s:=TCaption(c);');
  2770. Add(' c:=TChar(c);');
  2771. ConvertProgram;
  2772. CheckSource('TestAliasTypeRef',
  2773. LinesToStr([ // statements
  2774. 'this.i = 0;',
  2775. 'this.b = false;',
  2776. 'this.d = 0.0;',
  2777. 'this.s = "";',
  2778. 'this.c = "";',
  2779. '']),
  2780. LinesToStr([ // this.$main
  2781. '$mod.i = $mod.i;',
  2782. '$mod.i = ($mod.b ? 1 : 0);',
  2783. '$mod.b = $mod.b;',
  2784. '$mod.b = $mod.i != 0;',
  2785. '$mod.d = $mod.d;',
  2786. '$mod.d = $mod.i;',
  2787. '$mod.s = $mod.s;',
  2788. '$mod.s = $mod.c;',
  2789. '$mod.c = $mod.c;',
  2790. '']));
  2791. end;
  2792. procedure TTestModule.TestEmptyProc;
  2793. begin
  2794. StartProgram(false);
  2795. Add('procedure Test;');
  2796. Add('begin');
  2797. Add('end;');
  2798. Add('begin');
  2799. ConvertProgram;
  2800. CheckSource('TestEmptyProc',
  2801. LinesToStr([ // statements
  2802. 'this.Test = function () {',
  2803. '};'
  2804. ]),
  2805. LinesToStr([ // this.$main
  2806. ''
  2807. ]));
  2808. end;
  2809. procedure TTestModule.TestProcOneParam;
  2810. begin
  2811. StartProgram(false);
  2812. Add('procedure ProcA(i: longint);');
  2813. Add('begin');
  2814. Add('end;');
  2815. Add('begin');
  2816. Add(' PROCA(3);');
  2817. ConvertProgram;
  2818. CheckSource('TestProcOneParam',
  2819. LinesToStr([ // statements
  2820. 'this.ProcA = function (i) {',
  2821. '};'
  2822. ]),
  2823. LinesToStr([ // this.$main
  2824. '$mod.ProcA(3);'
  2825. ]));
  2826. end;
  2827. procedure TTestModule.TestFunctionWithoutParams;
  2828. begin
  2829. StartProgram(false);
  2830. Add('function FuncA: longint;');
  2831. Add('begin');
  2832. Add('end;');
  2833. Add('var i: longint;');
  2834. Add('begin');
  2835. Add(' I:=FUNCA();');
  2836. Add(' I:=FUNCA;');
  2837. Add(' FUNCA();');
  2838. Add(' FUNCA;');
  2839. ConvertProgram;
  2840. CheckSource('TestProcWithoutParams',
  2841. LinesToStr([ // statements
  2842. 'this.FuncA = function () {',
  2843. ' var Result = 0;',
  2844. ' return Result;',
  2845. '};',
  2846. 'this.i=0;'
  2847. ]),
  2848. LinesToStr([ // this.$main
  2849. '$mod.i=$mod.FuncA();',
  2850. '$mod.i=$mod.FuncA();',
  2851. '$mod.FuncA();',
  2852. '$mod.FuncA();'
  2853. ]));
  2854. end;
  2855. procedure TTestModule.TestProcedureWithoutParams;
  2856. begin
  2857. StartProgram(false);
  2858. Add('procedure ProcA;');
  2859. Add('begin');
  2860. Add('end;');
  2861. Add('begin');
  2862. Add(' PROCA();');
  2863. Add(' PROCA;');
  2864. ConvertProgram;
  2865. CheckSource('TestProcWithoutParams',
  2866. LinesToStr([ // statements
  2867. 'this.ProcA = function () {',
  2868. '};'
  2869. ]),
  2870. LinesToStr([ // this.$main
  2871. '$mod.ProcA();',
  2872. '$mod.ProcA();'
  2873. ]));
  2874. end;
  2875. procedure TTestModule.TestIncDec;
  2876. begin
  2877. StartProgram(false);
  2878. Add([
  2879. 'procedure DoIt(var i: longint);',
  2880. 'begin',
  2881. ' inc(i);',
  2882. ' inc(i,2);',
  2883. 'end;',
  2884. 'var',
  2885. ' Bar: longint;',
  2886. 'begin',
  2887. ' inc(bar);',
  2888. ' inc(bar,2);',
  2889. ' dec(bar);',
  2890. ' dec(bar,3);',
  2891. '']);
  2892. ConvertProgram;
  2893. CheckSource('TestIncDec',
  2894. LinesToStr([ // statements
  2895. 'this.DoIt = function (i) {',
  2896. ' i.set(i.get()+1);',
  2897. ' i.set(i.get()+2);',
  2898. '};',
  2899. 'this.Bar = 0;'
  2900. ]),
  2901. LinesToStr([ // this.$main
  2902. '$mod.Bar+=1;',
  2903. '$mod.Bar+=2;',
  2904. '$mod.Bar-=1;',
  2905. '$mod.Bar-=3;'
  2906. ]));
  2907. end;
  2908. procedure TTestModule.TestLoHiFpcMode;
  2909. begin
  2910. StartProgram(false);
  2911. Add([
  2912. '{$mode objfpc}',
  2913. 'const',
  2914. ' LoByte1 = Lo(Word($1234));',
  2915. ' HiByte1 = Hi(Word($1234));',
  2916. ' LoByte2 = Lo(SmallInt($1234));',
  2917. ' HiByte2 = Hi(SmallInt($1234));',
  2918. ' LoWord1 = Lo($1234CDEF);',
  2919. ' HiWord1 = Hi($1234CDEF);',
  2920. ' LoWord2 = Lo(-$1234CDEF);',
  2921. ' HiWord2 = Hi(-$1234CDEF);',
  2922. ' lo4:byte=lo(byte($34));',
  2923. ' hi4:byte=hi(byte($34));',
  2924. ' lo5:byte=lo(shortint(-$34));',
  2925. ' hi5:byte=hi(shortint(-$34));',
  2926. ' lo6:longword=lo($123456789ABCD);',
  2927. ' hi6:longword=hi($123456789ABCD);',
  2928. ' lo7:longword=lo(-$123456789ABCD);',
  2929. ' hi7:longword=hi(-$123456789ABCD);',
  2930. 'var',
  2931. ' b: Byte;',
  2932. ' ss: shortint;',
  2933. ' w: Word;',
  2934. ' si: SmallInt;',
  2935. ' lw: LongWord;',
  2936. ' li: LongInt;',
  2937. ' b2: Byte;',
  2938. ' ni: nativeint;',
  2939. 'begin',
  2940. ' w := $1234;',
  2941. ' ss := -$12;',
  2942. ' b := lo(ss);',
  2943. ' b := HI(ss);',
  2944. ' b := lo(w);',
  2945. ' b := HI(w);',
  2946. ' b2 := lo(b);',
  2947. ' b2 := hi(b);',
  2948. ' lw := $1234CDEF;',
  2949. ' w := lo(lw);',
  2950. ' w := hi(lw);',
  2951. ' ni := $123456789ABCD;',
  2952. ' lw := lo(ni);',
  2953. ' lw := hi(ni);',
  2954. '']);
  2955. ConvertProgram;
  2956. CheckSource('TestLoHiFpcMode',
  2957. LinesToStr([ // statements
  2958. 'this.LoByte1 = 0x1234 & 0xFF;',
  2959. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2960. 'this.LoByte2 = 0x1234 & 0xFF;',
  2961. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2962. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2963. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2964. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2965. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2966. 'this.lo4 = 0x34 & 0xF;',
  2967. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2968. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2969. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2970. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2971. 'this.hi6 = 74565 >>> 0;',
  2972. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2973. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2974. 'this.b = 0;',
  2975. 'this.ss = 0;',
  2976. 'this.w = 0;',
  2977. 'this.si = 0;',
  2978. 'this.lw = 0;',
  2979. 'this.li = 0;',
  2980. 'this.b2 = 0;',
  2981. 'this.ni = 0;',
  2982. '']),
  2983. LinesToStr([ // this.$main
  2984. '$mod.w = 0x1234;',
  2985. '$mod.ss = -0x12;',
  2986. '$mod.b = $mod.ss & 0xFF;',
  2987. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2988. '$mod.b = $mod.w & 0xFF;',
  2989. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2990. '$mod.b2 = $mod.b & 0xF;',
  2991. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2992. '$mod.lw = 0x1234CDEF;',
  2993. '$mod.w = $mod.lw & 0xFFFF;',
  2994. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2995. '$mod.ni = 0x123456789ABCD;',
  2996. '$mod.lw = $mod.ni >>> 0;',
  2997. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2998. '']));
  2999. end;
  3000. procedure TTestModule.TestLoHiDelphiMode;
  3001. begin
  3002. StartProgram(false);
  3003. Add([
  3004. '{$mode delphi}',
  3005. 'const',
  3006. ' LoByte1 = Lo(Word($1234));',
  3007. ' HiByte1 = Hi(Word($1234));',
  3008. ' LoByte2 = Lo(SmallInt($1234));',
  3009. ' HiByte2 = Hi(SmallInt($1234));',
  3010. ' LoByte3 = Lo($1234CDEF);',
  3011. ' HiByte3 = Hi($1234CDEF);',
  3012. ' LoByte4 = Lo(-$1234CDEF);',
  3013. ' HiByte4 = Hi(-$1234CDEF);',
  3014. 'var',
  3015. ' b: Byte;',
  3016. ' w: Word;',
  3017. ' si: SmallInt;',
  3018. ' lw: LongWord;',
  3019. ' li: LongInt;',
  3020. 'begin',
  3021. ' w := $1234;',
  3022. ' b := lo(w);',
  3023. ' b := HI(w);',
  3024. ' lw := $1234CDEF;',
  3025. ' b := lo(lw);',
  3026. ' b := hi(lw);',
  3027. '']);
  3028. ConvertProgram;
  3029. CheckSource('TestLoHiDelphiMode',
  3030. LinesToStr([ // statements
  3031. 'this.LoByte1 = 0x1234 & 0xFF;',
  3032. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3033. 'this.LoByte2 = 0x1234 & 0xFF;',
  3034. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3035. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3036. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3037. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3038. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3039. 'this.b = 0;',
  3040. 'this.w = 0;',
  3041. 'this.si = 0;',
  3042. 'this.lw = 0;',
  3043. 'this.li = 0;'
  3044. ]),
  3045. LinesToStr([ // this.$main
  3046. '$mod.w = 0x1234;',
  3047. '$mod.b = $mod.w & 0xFF;',
  3048. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3049. '$mod.lw = 0x1234CDEF;',
  3050. '$mod.b = $mod.lw & 0xFF;',
  3051. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3052. ]));
  3053. end;
  3054. procedure TTestModule.TestAssignments;
  3055. begin
  3056. StartProgram(false);
  3057. Parser.Options:=Parser.Options+[po_cassignments];
  3058. Add('var');
  3059. Add(' Bar:longint;');
  3060. Add('begin');
  3061. Add(' bar:=3;');
  3062. Add(' bar+=4;');
  3063. Add(' bar-=5;');
  3064. Add(' bar*=6;');
  3065. ConvertProgram;
  3066. CheckSource('TestAssignments',
  3067. LinesToStr([ // statements
  3068. 'this.Bar = 0;'
  3069. ]),
  3070. LinesToStr([ // this.$main
  3071. '$mod.Bar=3;',
  3072. '$mod.Bar+=4;',
  3073. '$mod.Bar-=5;',
  3074. '$mod.Bar*=6;'
  3075. ]));
  3076. end;
  3077. procedure TTestModule.TestArithmeticOperators1;
  3078. begin
  3079. StartProgram(false);
  3080. Add('var');
  3081. Add(' vA,vB,vC:longint;');
  3082. Add('begin');
  3083. Add(' va:=1;');
  3084. Add(' vb:=va+va;');
  3085. Add(' vb:=va div vb;');
  3086. Add(' vb:=va mod vb;');
  3087. Add(' vb:=va+va*vb+va div vb;');
  3088. Add(' vc:=-va;');
  3089. Add(' va:=va-vb;');
  3090. Add(' vb:=va;');
  3091. Add(' if va<vb then vc:=va else vc:=vb;');
  3092. ConvertProgram;
  3093. CheckSource('TestArithmeticOperators1',
  3094. LinesToStr([ // statements
  3095. 'this.vA = 0;',
  3096. 'this.vB = 0;',
  3097. 'this.vC = 0;'
  3098. ]),
  3099. LinesToStr([ // this.$main
  3100. '$mod.vA = 1;',
  3101. '$mod.vB = $mod.vA + $mod.vA;',
  3102. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3103. '$mod.vB = $mod.vA % $mod.vB;',
  3104. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3105. '$mod.vC = -$mod.vA;',
  3106. '$mod.vA = $mod.vA - $mod.vB;',
  3107. '$mod.vB = $mod.vA;',
  3108. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3109. ]));
  3110. end;
  3111. procedure TTestModule.TestLogicalOperators;
  3112. begin
  3113. StartProgram(false);
  3114. Add('var');
  3115. Add(' vA,vB,vC:boolean;');
  3116. Add('begin');
  3117. Add(' va:=vb and vc;');
  3118. Add(' va:=vb or vc;');
  3119. Add(' va:=vb xor vc;');
  3120. Add(' va:=true and vc;');
  3121. Add(' va:=(vb and vc) or (va and vb);');
  3122. Add(' va:=not vb;');
  3123. ConvertProgram;
  3124. CheckSource('TestLogicalOperators',
  3125. LinesToStr([ // statements
  3126. 'this.vA = false;',
  3127. 'this.vB = false;',
  3128. 'this.vC = false;'
  3129. ]),
  3130. LinesToStr([ // this.$main
  3131. '$mod.vA = $mod.vB && $mod.vC;',
  3132. '$mod.vA = $mod.vB || $mod.vC;',
  3133. '$mod.vA = $mod.vB ^ $mod.vC;',
  3134. '$mod.vA = true && $mod.vC;',
  3135. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3136. '$mod.vA = !$mod.vB;'
  3137. ]));
  3138. end;
  3139. procedure TTestModule.TestBitwiseOperators;
  3140. begin
  3141. StartProgram(false);
  3142. Add([
  3143. 'var',
  3144. ' vA,vB,vC:longint;',
  3145. ' X,Y,Z: nativeint;',
  3146. 'begin',
  3147. ' va:=vb and vc;',
  3148. ' va:=vb or vc;',
  3149. ' va:=vb xor vc;',
  3150. ' va:=vb shl vc;',
  3151. ' va:=vb shr vc;',
  3152. ' va:=3 and vc;',
  3153. ' va:=(vb and vc) or (va and vb);',
  3154. ' va:=not vb;',
  3155. ' X:=Y and Z;',
  3156. ' X:=Y and va;',
  3157. ' X:=Y or Z;',
  3158. ' X:=Y or va;',
  3159. ' X:=Y xor Z;',
  3160. ' X:=Y xor va;',
  3161. '']);
  3162. ConvertProgram;
  3163. CheckSource('TestBitwiseOperators',
  3164. LinesToStr([ // statements
  3165. 'this.vA = 0;',
  3166. 'this.vB = 0;',
  3167. 'this.vC = 0;',
  3168. 'this.X = 0;',
  3169. 'this.Y = 0;',
  3170. 'this.Z = 0;',
  3171. '']),
  3172. LinesToStr([ // this.$main
  3173. '$mod.vA = $mod.vB & $mod.vC;',
  3174. '$mod.vA = $mod.vB | $mod.vC;',
  3175. '$mod.vA = $mod.vB ^ $mod.vC;',
  3176. '$mod.vA = $mod.vB << $mod.vC;',
  3177. '$mod.vA = $mod.vB >>> $mod.vC;',
  3178. '$mod.vA = 3 & $mod.vC;',
  3179. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3180. '$mod.vA = ~$mod.vB;',
  3181. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3182. '$mod.X = $mod.Y & $mod.vA;',
  3183. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3184. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3185. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3186. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3187. '']));
  3188. end;
  3189. procedure TTestModule.TestBitwiseOperatorsLongword;
  3190. begin
  3191. StartProgram(false);
  3192. Add([
  3193. 'var',
  3194. ' a,b,c:longword;',
  3195. ' i: longint;',
  3196. 'begin',
  3197. ' a:=$12345678;',
  3198. ' b:=$EDCBA987;',
  3199. ' c:=not a;',
  3200. ' c:=a and b;',
  3201. ' c:=a and $ffff0000;',
  3202. ' c:=a or b;',
  3203. ' c:=a or $ff00ff00;',
  3204. ' c:=a xor b;',
  3205. ' c:=a xor $f0f0f0f0;',
  3206. ' c:=a shl 1;',
  3207. ' c:=a shl 16;',
  3208. ' c:=a shl 24;',
  3209. ' c:=a shl b;',
  3210. ' c:=a shr 1;',
  3211. ' c:=a shr 16;',
  3212. ' c:=a shr 24;',
  3213. ' c:=a shr b;',
  3214. ' c:=(b and c) or (a and b);',
  3215. ' c:=i and a;',
  3216. ' c:=i or a;',
  3217. ' c:=i xor a;',
  3218. '']);
  3219. ConvertProgram;
  3220. CheckSource('TestBitwiseOperatorsLongword',
  3221. LinesToStr([ // statements
  3222. 'this.a = 0;',
  3223. 'this.b = 0;',
  3224. 'this.c = 0;',
  3225. 'this.i = 0;',
  3226. '']),
  3227. LinesToStr([ // this.$main
  3228. '$mod.a = 0x12345678;',
  3229. '$mod.b = 0xEDCBA987;',
  3230. '$mod.c = rtl.lw(~$mod.a);',
  3231. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3232. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3233. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3234. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3235. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3236. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3237. '$mod.c = rtl.lw($mod.a << 1);',
  3238. '$mod.c = rtl.lw($mod.a << 16);',
  3239. '$mod.c = rtl.lw($mod.a << 24);',
  3240. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3241. '$mod.c = rtl.lw($mod.a >>> 1);',
  3242. '$mod.c = rtl.lw($mod.a >>> 16);',
  3243. '$mod.c = rtl.lw($mod.a >>> 24);',
  3244. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3245. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3246. '$mod.c = $mod.i & $mod.a;',
  3247. '$mod.c = $mod.i | $mod.a;',
  3248. '$mod.c = $mod.i ^ $mod.a;',
  3249. '']));
  3250. end;
  3251. procedure TTestModule.TestPrgProcVar;
  3252. begin
  3253. StartProgram(false);
  3254. Add('procedure Proc1;');
  3255. Add('type');
  3256. Add(' t1=longint;');
  3257. Add('var');
  3258. Add(' vA:t1;');
  3259. Add('begin');
  3260. Add('end;');
  3261. Add('begin');
  3262. ConvertProgram;
  3263. CheckSource('TestPrgProcVar',
  3264. LinesToStr([ // statements
  3265. 'this.Proc1 = function () {',
  3266. ' var vA=0;',
  3267. '};'
  3268. ]),
  3269. LinesToStr([ // this.$main
  3270. ''
  3271. ]));
  3272. end;
  3273. procedure TTestModule.TestUnitProcVar;
  3274. begin
  3275. StartUnit(false);
  3276. Add('interface');
  3277. Add('');
  3278. Add('type tA=string; // unit scope');
  3279. Add('procedure Proc1;');
  3280. Add('');
  3281. Add('implementation');
  3282. Add('');
  3283. Add('procedure Proc1;');
  3284. Add('type tA=longint; // local proc scope');
  3285. Add('var v1:tA; // using local tA');
  3286. Add('begin');
  3287. Add('end;');
  3288. Add('var v2:tA; // using interface tA');
  3289. ConvertUnit;
  3290. CheckSource('TestUnitProcVar',
  3291. LinesToStr([ // statements
  3292. 'var $impl = $mod.$impl;',
  3293. 'this.Proc1 = function () {',
  3294. ' var v1 = 0;',
  3295. '};',
  3296. '']),
  3297. // this.$init
  3298. '',
  3299. // implementation
  3300. LinesToStr([
  3301. '$impl.v2 = "";',
  3302. '']));
  3303. end;
  3304. procedure TTestModule.TestImplProc;
  3305. begin
  3306. StartUnit(false);
  3307. Add('interface');
  3308. Add('');
  3309. Add('procedure Proc1;');
  3310. Add('');
  3311. Add('implementation');
  3312. Add('');
  3313. Add('procedure Proc1; begin end;');
  3314. Add('procedure Proc2; begin end;');
  3315. Add('initialization');
  3316. Add(' Proc1;');
  3317. Add(' Proc2;');
  3318. ConvertUnit;
  3319. CheckSource('TestImplProc',
  3320. LinesToStr([ // statements
  3321. 'var $impl = $mod.$impl;',
  3322. 'this.Proc1 = function () {',
  3323. '};',
  3324. '']),
  3325. LinesToStr([ // this.$init
  3326. '$mod.Proc1();',
  3327. '$impl.Proc2();',
  3328. '']),
  3329. LinesToStr([ // implementation
  3330. '$impl.Proc2 = function () {',
  3331. '};',
  3332. ''])
  3333. );
  3334. end;
  3335. procedure TTestModule.TestFunctionResult;
  3336. begin
  3337. StartProgram(false);
  3338. Add('function Func1: longint;');
  3339. Add('begin');
  3340. Add(' Result:=3;');
  3341. Add(' Func1:=4;');
  3342. Add('end;');
  3343. Add('begin');
  3344. ConvertProgram;
  3345. CheckSource('TestFunctionResult',
  3346. LinesToStr([ // statements
  3347. 'this.Func1 = function () {',
  3348. ' var Result = 0;',
  3349. ' Result = 3;',
  3350. ' Result = 4;',
  3351. ' return Result;',
  3352. '};'
  3353. ]),
  3354. '');
  3355. end;
  3356. procedure TTestModule.TestNestedProc;
  3357. begin
  3358. StartProgram(false);
  3359. Add([
  3360. 'var vInUnit: longint;',
  3361. 'function DoIt(pA,pD: longint): longint;',
  3362. 'var',
  3363. ' vB: longint;',
  3364. ' vC: longint;',
  3365. ' function Nesty(pA: longint): longint; ',
  3366. ' var vB: longint;',
  3367. ' begin',
  3368. ' Result:=pa+vb+vc+pd+vInUnit;',
  3369. ' nesty:=3;',
  3370. ' doit:=4;',
  3371. ' exit;',
  3372. ' end;',
  3373. 'begin',
  3374. ' Result:=pa+vb+vc;',
  3375. ' doit:=6;',
  3376. ' exit;',
  3377. 'end;',
  3378. 'begin']);
  3379. ConvertProgram;
  3380. CheckSource('TestNestedProc',
  3381. LinesToStr([ // statements
  3382. 'this.vInUnit = 0;',
  3383. 'this.DoIt = function (pA, pD) {',
  3384. ' var Result = 0;',
  3385. ' var vB = 0;',
  3386. ' var vC = 0;',
  3387. ' function Nesty(pA) {',
  3388. ' var Result$1 = 0;',
  3389. ' var vB = 0;',
  3390. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3391. ' Result$1 = 3;',
  3392. ' Result = 4;',
  3393. ' return Result$1;',
  3394. ' return Result$1;',
  3395. ' };',
  3396. ' Result = pA + vB + vC;',
  3397. ' Result = 6;',
  3398. ' return Result;',
  3399. ' return Result;',
  3400. '};'
  3401. ]),
  3402. '');
  3403. end;
  3404. procedure TTestModule.TestNestedProc_ResultString;
  3405. begin
  3406. StartProgram(false);
  3407. Add([
  3408. 'function DoIt: string;',
  3409. ' function Nesty: string; ',
  3410. ' begin',
  3411. ' nesty:=#65#66;',
  3412. ' nesty[1]:=#67;',
  3413. ' doit:=#68;',
  3414. ' doit[2]:=#69;',
  3415. ' end;',
  3416. 'begin',
  3417. ' doit:=#70;',
  3418. ' doit[3]:=#71;',
  3419. 'end;',
  3420. 'begin']);
  3421. ConvertProgram;
  3422. CheckSource('TestNestedProc_ResultString',
  3423. LinesToStr([ // statements
  3424. 'this.DoIt = function () {',
  3425. ' var Result = "";',
  3426. ' function Nesty() {',
  3427. ' var Result$1 = "";',
  3428. ' Result$1 = "AB";',
  3429. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3430. ' Result = "D";',
  3431. ' Result = rtl.setCharAt(Result, 1, "E");',
  3432. ' return Result$1;',
  3433. ' };',
  3434. ' Result = "F";',
  3435. ' Result = rtl.setCharAt(Result, 2, "G");',
  3436. ' return Result;',
  3437. '};'
  3438. ]),
  3439. '');
  3440. end;
  3441. procedure TTestModule.TestForwardProc;
  3442. begin
  3443. StartProgram(false);
  3444. Add('procedure FuncA(Bar: longint); forward;');
  3445. Add('procedure FuncB(Bar: longint);');
  3446. Add('begin');
  3447. Add(' funca(bar);');
  3448. Add('end;');
  3449. Add('procedure funca(bar: longint);');
  3450. Add('begin');
  3451. Add(' if bar=3 then ;');
  3452. Add('end;');
  3453. Add('begin');
  3454. Add(' funca(4);');
  3455. Add(' funcb(5);');
  3456. ConvertProgram;
  3457. CheckSource('TestForwardProc',
  3458. LinesToStr([ // statements'
  3459. 'this.FuncB = function (Bar) {',
  3460. ' $mod.FuncA(Bar);',
  3461. '};',
  3462. 'this.FuncA = function (Bar) {',
  3463. ' if (Bar === 3);',
  3464. '};'
  3465. ]),
  3466. LinesToStr([
  3467. '$mod.FuncA(4);',
  3468. '$mod.FuncB(5);'
  3469. ])
  3470. );
  3471. end;
  3472. procedure TTestModule.TestNestedForwardProc;
  3473. begin
  3474. StartProgram(false);
  3475. Add('procedure FuncA;');
  3476. Add(' procedure FuncB(i: longint); forward;');
  3477. Add(' procedure FuncC(i: longint);');
  3478. Add(' begin');
  3479. Add(' funcb(i);');
  3480. Add(' end;');
  3481. Add(' procedure FuncB(i: longint);');
  3482. Add(' begin');
  3483. Add(' if i=3 then ;');
  3484. Add(' end;');
  3485. Add('begin');
  3486. Add(' funcc(4)');
  3487. Add('end;');
  3488. Add('begin');
  3489. Add(' funca;');
  3490. ConvertProgram;
  3491. CheckSource('TestNestedForwardProc',
  3492. LinesToStr([ // statements'
  3493. 'this.FuncA = function () {',
  3494. ' function FuncC(i) {',
  3495. ' FuncB(i);',
  3496. ' };',
  3497. ' function FuncB(i) {',
  3498. ' if (i === 3);',
  3499. ' };',
  3500. ' FuncC(4);',
  3501. '};'
  3502. ]),
  3503. LinesToStr([
  3504. '$mod.FuncA();'
  3505. ])
  3506. );
  3507. end;
  3508. procedure TTestModule.TestAssignFunctionResult;
  3509. begin
  3510. StartProgram(false);
  3511. Add('function Func1: longint;');
  3512. Add('begin');
  3513. Add('end;');
  3514. Add('var i: longint;');
  3515. Add('begin');
  3516. Add(' i:=func1();');
  3517. Add(' i:=func1()+func1();');
  3518. ConvertProgram;
  3519. CheckSource('TestAssignFunctionResult',
  3520. LinesToStr([ // statements
  3521. 'this.Func1 = function () {',
  3522. ' var Result = 0;',
  3523. ' return Result;',
  3524. '};',
  3525. 'this.i = 0;'
  3526. ]),
  3527. LinesToStr([
  3528. '$mod.i = $mod.Func1();',
  3529. '$mod.i = $mod.Func1() + $mod.Func1();'
  3530. ]));
  3531. end;
  3532. procedure TTestModule.TestFunctionResultInCondition;
  3533. begin
  3534. StartProgram(false);
  3535. Add('function Func1: longint;');
  3536. Add('begin');
  3537. Add('end;');
  3538. Add('function Func2: boolean;');
  3539. Add('begin');
  3540. Add('end;');
  3541. Add('var i: longint;');
  3542. Add('begin');
  3543. Add(' if func2 then ;');
  3544. Add(' if i=func1() then ;');
  3545. Add(' if i=func1 then ;');
  3546. ConvertProgram;
  3547. CheckSource('TestFunctionResultInCondition',
  3548. LinesToStr([ // statements
  3549. 'this.Func1 = function () {',
  3550. ' var Result = 0;',
  3551. ' return Result;',
  3552. '};',
  3553. 'this.Func2 = function () {',
  3554. ' var Result = false;',
  3555. ' return Result;',
  3556. '};',
  3557. 'this.i = 0;'
  3558. ]),
  3559. LinesToStr([
  3560. 'if ($mod.Func2());',
  3561. 'if ($mod.i === $mod.Func1());',
  3562. 'if ($mod.i === $mod.Func1());'
  3563. ]));
  3564. end;
  3565. procedure TTestModule.TestFunctionResultInForLoop;
  3566. begin
  3567. StartProgram(false);
  3568. Add([
  3569. 'function Func1(a: array of longint): longint;',
  3570. 'begin',
  3571. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3572. ' for Result in a do if a[Result]=0 then exit;',
  3573. 'end;',
  3574. 'begin',
  3575. ' Func1([1,2,3])']);
  3576. ConvertProgram;
  3577. CheckSource('TestFunctionResultInForLoop',
  3578. LinesToStr([ // statements
  3579. 'this.Func1 = function (a) {',
  3580. ' var Result = 0;',
  3581. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3582. ' Result = $l;',
  3583. ' if (a[Result] === 0) return Result;',
  3584. ' };',
  3585. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3586. ' Result = $in[$l1];',
  3587. ' if (a[Result] === 0) return Result;',
  3588. ' };',
  3589. ' return Result;',
  3590. '};',
  3591. '']),
  3592. LinesToStr([
  3593. '$mod.Func1([1, 2, 3]);'
  3594. ]));
  3595. end;
  3596. procedure TTestModule.TestFunctionResultInTypeCast;
  3597. begin
  3598. StartProgram(false);
  3599. Add([
  3600. 'function GetInt: longint;',
  3601. 'begin',
  3602. 'end;',
  3603. 'begin',
  3604. ' if Byte(GetInt)=0 then ;',
  3605. '']);
  3606. ConvertProgram;
  3607. CheckSource('TestFunctionResultInTypeCast',
  3608. LinesToStr([ // statements
  3609. 'this.GetInt = function () {',
  3610. ' var Result = 0;',
  3611. ' return Result;',
  3612. '};',
  3613. '']),
  3614. LinesToStr([
  3615. 'if (($mod.GetInt() & 255) === 0) ;'
  3616. ]));
  3617. end;
  3618. procedure TTestModule.TestExit;
  3619. begin
  3620. StartProgram(false);
  3621. Add('procedure ProcA;');
  3622. Add('begin');
  3623. Add(' exit;');
  3624. Add('end;');
  3625. Add('function FuncB: longint;');
  3626. Add('begin');
  3627. Add(' exit;');
  3628. Add(' exit(3);');
  3629. Add('end;');
  3630. Add('function FuncC: string;');
  3631. Add('begin');
  3632. Add(' exit;');
  3633. Add(' exit(''a'');');
  3634. Add(' exit(''abc'');');
  3635. Add('end;');
  3636. Add('begin');
  3637. Add(' exit;');
  3638. Add(' exit(1);');
  3639. ConvertProgram;
  3640. CheckSource('TestExit',
  3641. LinesToStr([ // statements
  3642. 'this.ProcA = function () {',
  3643. ' return;',
  3644. '};',
  3645. 'this.FuncB = function () {',
  3646. ' var Result = 0;',
  3647. ' return Result;',
  3648. ' return 3;',
  3649. ' return Result;',
  3650. '};',
  3651. 'this.FuncC = function () {',
  3652. ' var Result = "";',
  3653. ' return Result;',
  3654. ' return "a";',
  3655. ' return "abc";',
  3656. ' return Result;',
  3657. '};'
  3658. ]),
  3659. LinesToStr([
  3660. 'return;',
  3661. 'return 1;',
  3662. '']));
  3663. end;
  3664. procedure TTestModule.TestExit_ResultInFinally;
  3665. begin
  3666. StartProgram(false);
  3667. Add([
  3668. 'function Run: word;',
  3669. 'begin',
  3670. ' try',
  3671. ' exit(3);', // no Result in finally -> use return 3
  3672. ' finally',
  3673. ' end;',
  3674. 'end;',
  3675. 'function Fly: word;',
  3676. 'begin',
  3677. ' try',
  3678. ' exit(3);',
  3679. ' finally',
  3680. ' if Result>0 then ;',
  3681. ' end;',
  3682. 'end;',
  3683. 'function Jump: word;',
  3684. 'begin',
  3685. ' try',
  3686. ' try',
  3687. ' exit(4);',
  3688. ' finally',
  3689. ' end;',
  3690. ' finally',
  3691. ' if Result>0 then ;',
  3692. ' end;',
  3693. 'end;',
  3694. 'begin',
  3695. '']);
  3696. ConvertProgram;
  3697. CheckSource('TestExit_ResultInFinally',
  3698. LinesToStr([ // statements
  3699. 'this.Run = function () {',
  3700. ' var Result = 0;',
  3701. ' try {',
  3702. ' return 3;',
  3703. ' } finally {',
  3704. ' };',
  3705. ' return Result;',
  3706. '};',
  3707. 'this.Fly = function () {',
  3708. ' var Result = 0;',
  3709. ' try {',
  3710. ' Result = 3;',
  3711. ' return Result;',
  3712. ' } finally {',
  3713. ' if (Result > 0) ;',
  3714. ' };',
  3715. ' return Result;',
  3716. '};',
  3717. 'this.Jump = function () {',
  3718. ' var Result = 0;',
  3719. ' try {',
  3720. ' try {',
  3721. ' Result = 4;',
  3722. ' return Result;',
  3723. ' } finally {',
  3724. ' };',
  3725. ' } finally {',
  3726. ' if (Result > 0) ;',
  3727. ' };',
  3728. ' return Result;',
  3729. '};',
  3730. '']),
  3731. LinesToStr([
  3732. '']));
  3733. end;
  3734. procedure TTestModule.TestBreak;
  3735. begin
  3736. StartProgram(false);
  3737. Add([
  3738. 'var',
  3739. ' i: longint;',
  3740. 'begin',
  3741. ' repeat',
  3742. ' break;',
  3743. ' until true;',
  3744. ' while true do',
  3745. ' break;',
  3746. ' for i:=1 to 2 do',
  3747. ' break;']);
  3748. ConvertProgram;
  3749. CheckSource('TestBreak',
  3750. LinesToStr([ // statements
  3751. 'this.i = 0;'
  3752. ]),
  3753. LinesToStr([
  3754. 'do {',
  3755. ' break;',
  3756. '} while (!true);',
  3757. 'while (true) break;',
  3758. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3759. '']));
  3760. end;
  3761. procedure TTestModule.TestBreakAsVar;
  3762. begin
  3763. StartProgram(false);
  3764. Add([
  3765. 'procedure DoIt(break: boolean);',
  3766. 'begin',
  3767. ' if break then ;',
  3768. 'end;',
  3769. 'var',
  3770. ' break: boolean;',
  3771. 'begin',
  3772. ' if break then ;']);
  3773. ConvertProgram;
  3774. CheckSource('TestBreakAsVar',
  3775. LinesToStr([ // statements
  3776. 'this.DoIt = function (Break) {',
  3777. ' if (Break) ;',
  3778. '};',
  3779. 'this.Break = false;',
  3780. '']),
  3781. LinesToStr([
  3782. 'if($mod.Break) ;',
  3783. '']));
  3784. end;
  3785. procedure TTestModule.TestContinue;
  3786. begin
  3787. StartProgram(false);
  3788. Add('var i: longint;');
  3789. Add('begin');
  3790. Add(' repeat');
  3791. Add(' continue;');
  3792. Add(' until true;');
  3793. Add(' while true do');
  3794. Add(' continue;');
  3795. Add(' for i:=1 to 2 do');
  3796. Add(' continue;');
  3797. ConvertProgram;
  3798. CheckSource('TestContinue',
  3799. LinesToStr([ // statements
  3800. 'this.i = 0;'
  3801. ]),
  3802. LinesToStr([
  3803. 'do {',
  3804. ' continue;',
  3805. '} while (!true);',
  3806. 'while (true) continue;',
  3807. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3808. '']));
  3809. end;
  3810. procedure TTestModule.TestProc_External;
  3811. begin
  3812. StartProgram(false);
  3813. Add('procedure Foo; external name ''console.log'';');
  3814. Add('function Bar: longint; external name ''get.item'';');
  3815. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3816. Add('var');
  3817. Add(' i: longint;');
  3818. Add('begin');
  3819. Add(' Foo;');
  3820. Add(' i:=Bar;');
  3821. Add(' i:=Bla(''abc'');');
  3822. ConvertProgram;
  3823. CheckSource('TestProc_External',
  3824. LinesToStr([ // statements
  3825. 'this.i = 0;'
  3826. ]),
  3827. LinesToStr([
  3828. 'console.log();',
  3829. '$mod.i = get.item();',
  3830. '$mod.i = apply.something("abc");'
  3831. ]));
  3832. end;
  3833. procedure TTestModule.TestProc_ExternalOtherUnit;
  3834. begin
  3835. AddModuleWithIntfImplSrc('unit2.pas',
  3836. LinesToStr([
  3837. 'procedure Now; external name ''Date.now'';',
  3838. 'procedure DoIt;'
  3839. ]),
  3840. 'procedure doit; begin end;');
  3841. StartUnit(true);
  3842. Add('interface');
  3843. Add('uses unit2;');
  3844. Add('implementation');
  3845. Add('begin');
  3846. Add(' now;');
  3847. Add(' now();');
  3848. Add(' uNit2.now;');
  3849. Add(' uNit2.now();');
  3850. Add(' doit;');
  3851. Add(' uNit2.doit;');
  3852. ConvertUnit;
  3853. CheckSource('TestProc_ExternalOtherUnit',
  3854. LinesToStr([
  3855. '']),
  3856. LinesToStr([
  3857. 'Date.now();',
  3858. 'Date.now();',
  3859. 'Date.now();',
  3860. 'Date.now();',
  3861. 'pas.unit2.DoIt();',
  3862. 'pas.unit2.DoIt();',
  3863. '']));
  3864. end;
  3865. procedure TTestModule.TestProc_Asm;
  3866. begin
  3867. StartProgram(false);
  3868. Add([
  3869. '{$mode delphi}',
  3870. 'function DoIt: longint;',
  3871. 'begin;',
  3872. ' asm',
  3873. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3874. ' end;',
  3875. ' asm console.log(); end;',
  3876. ' asm',
  3877. ' s = "'' ";',
  3878. ' s = ''" '';',
  3879. ' s = s + "world" + "''";',
  3880. ' // end',
  3881. ' s = ''end'';',
  3882. ' s = "end";',
  3883. ' s = "foo\"bar";',
  3884. ' s = ''a\''b'';',
  3885. ' s = `${expr}\`-"-''-`;',
  3886. ' s = `multi',
  3887. 'line`;',
  3888. ' end;',
  3889. 'end;',
  3890. 'procedure Fly;',
  3891. 'asm',
  3892. ' return;',
  3893. 'end;',
  3894. 'begin']);
  3895. ConvertProgram;
  3896. CheckSource('TestProc_Asm',
  3897. LinesToStr([ // statements
  3898. 'this.DoIt = function () {',
  3899. ' var Result = 0;',
  3900. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3901. ' console.log();',
  3902. ' s = "'' ";',
  3903. ' s = ''" '';',
  3904. ' s = s + "world" + "''";',
  3905. ' // end',
  3906. ' s = ''end'';',
  3907. ' s = "end";',
  3908. ' s = "foo\"bar";',
  3909. ' s = ''a\''b'';',
  3910. ' s = `${expr}\`-"-''-`;',
  3911. ' s = `multi',
  3912. 'line`;',
  3913. ' return Result;',
  3914. '};',
  3915. 'this.Fly = function () {',
  3916. ' return;',
  3917. '};',
  3918. '']),
  3919. LinesToStr([
  3920. ''
  3921. ]));
  3922. end;
  3923. procedure TTestModule.TestProc_AsmSubBlock;
  3924. begin
  3925. StartProgram(true,[supTObject]);
  3926. Add([
  3927. '{$mode delphi}',
  3928. 'type',
  3929. ' TBird = class end;',
  3930. 'procedure Run(w: word);',
  3931. 'begin;',
  3932. ' if true then asm console.log(); end;',
  3933. ' if w>3 then asm',
  3934. ' var a = w+1;',
  3935. ' w = a+3;',
  3936. ' end;',
  3937. ' while (w>7) do asm',
  3938. ' w+=3; w*=2;',
  3939. ' end;',
  3940. ' try',
  3941. ' except',
  3942. ' on E: TBird do',
  3943. ' asm console.log(E); end;',
  3944. ' on E: TObject do',
  3945. ' asm var i=3; i--; end;',
  3946. ' else asm Fly; High; end;',
  3947. ' end;',
  3948. 'end;',
  3949. 'begin']);
  3950. ConvertProgram;
  3951. CheckSource('TestProc_AsmSubBlock',
  3952. LinesToStr([ // statements
  3953. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3954. '});',
  3955. 'this.Run = function (w) {',
  3956. ' if (true) console.log();',
  3957. ' if (w > 3) {',
  3958. ' var a = w+1;',
  3959. ' w = a+3;',
  3960. ' };',
  3961. ' while (w > 7) {',
  3962. ' w+=3; w*=2;',
  3963. ' };',
  3964. ' try {} catch ($e) {',
  3965. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3966. ' var E = $e;',
  3967. ' console.log(E);',
  3968. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3969. ' var E = $e;',
  3970. ' var i=3; i--;',
  3971. ' } else {',
  3972. ' Fly; High;',
  3973. ' }',
  3974. ' };',
  3975. '};',
  3976. '']),
  3977. LinesToStr([
  3978. ''
  3979. ]));
  3980. end;
  3981. procedure TTestModule.TestProc_Assembler;
  3982. begin
  3983. StartProgram(false);
  3984. Add('function DoIt: longint; assembler;');
  3985. Add('asm');
  3986. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3987. Add('end;');
  3988. Add('begin');
  3989. ConvertProgram;
  3990. CheckSource('TestProc_Assembler',
  3991. LinesToStr([ // statements
  3992. 'this.DoIt = function () {',
  3993. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3994. '};'
  3995. ]),
  3996. LinesToStr([
  3997. ''
  3998. ]));
  3999. end;
  4000. procedure TTestModule.TestProc_VarParam;
  4001. begin
  4002. StartProgram(false);
  4003. Add('type integer = longint;');
  4004. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4005. Add('var vJ: integer;');
  4006. Add('begin');
  4007. Add(' vg:=vg+1;');
  4008. Add(' vj:=vh+2;');
  4009. Add(' vi:=vi+3;');
  4010. Add(' doit(vg,vg,vg);');
  4011. Add(' doit(vh,vh,vj);');
  4012. Add(' doit(vi,vi,vi);');
  4013. Add(' doit(vj,vj,vj);');
  4014. Add('end;');
  4015. Add('var i: integer;');
  4016. Add('begin');
  4017. Add(' doit(i,i,i);');
  4018. ConvertProgram;
  4019. CheckSource('TestProc_VarParam',
  4020. LinesToStr([ // statements
  4021. 'this.DoIt = function (vG,vH,vI) {',
  4022. ' var vJ = 0;',
  4023. ' vG = vG + 1;',
  4024. ' vJ = vH + 2;',
  4025. ' vI.set(vI.get()+3);',
  4026. ' $mod.DoIt(vG, vG, {',
  4027. ' get: function () {',
  4028. ' return vG;',
  4029. ' },',
  4030. ' set: function (v) {',
  4031. ' vG = v;',
  4032. ' }',
  4033. ' });',
  4034. ' $mod.DoIt(vH, vH, {',
  4035. ' get: function () {',
  4036. ' return vJ;',
  4037. ' },',
  4038. ' set: function (v) {',
  4039. ' vJ = v;',
  4040. ' }',
  4041. ' });',
  4042. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4043. ' $mod.DoIt(vJ, vJ, {',
  4044. ' get: function () {',
  4045. ' return vJ;',
  4046. ' },',
  4047. ' set: function (v) {',
  4048. ' vJ = v;',
  4049. ' }',
  4050. ' });',
  4051. '};',
  4052. 'this.i = 0;'
  4053. ]),
  4054. LinesToStr([
  4055. '$mod.DoIt($mod.i,$mod.i,{',
  4056. ' p: $mod,',
  4057. ' get: function () {',
  4058. ' return this.p.i;',
  4059. ' },',
  4060. ' set: function (v) {',
  4061. ' this.p.i = v;',
  4062. ' }',
  4063. '});'
  4064. ]));
  4065. end;
  4066. procedure TTestModule.TestProc_VarParamString;
  4067. begin
  4068. StartProgram(false);
  4069. Add(['type TCaption = string;',
  4070. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4071. 'var c: char;',
  4072. 'begin',
  4073. ' va[1]:=c;',
  4074. ' vb[2]:=c;',
  4075. ' vc[3]:=c;',
  4076. 'end;',
  4077. 'begin']);
  4078. ConvertProgram;
  4079. CheckSource('TestProc_VarParamString',
  4080. LinesToStr([ // statements
  4081. 'this.DoIt = function (vA,vB,vC) {',
  4082. ' var c = "";',
  4083. ' vA = rtl.setCharAt(vA, 0, c);',
  4084. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4085. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4086. '};',
  4087. '']),
  4088. LinesToStr([
  4089. ]));
  4090. end;
  4091. procedure TTestModule.TestProc_VarParamV;
  4092. begin
  4093. StartProgram(false);
  4094. Add([
  4095. 'procedure Inc2(var i: longint);',
  4096. 'begin',
  4097. ' i:=i+2;',
  4098. 'end;',
  4099. 'procedure DoIt(v: longint);',
  4100. 'var p: array of longint;',
  4101. 'begin',
  4102. ' Inc2(v);',
  4103. ' Inc2(p[v]);',
  4104. 'end;',
  4105. 'begin']);
  4106. ConvertProgram;
  4107. CheckSource('TestProc_VarParamV',
  4108. LinesToStr([ // statements
  4109. 'this.Inc2 = function (i) {',
  4110. ' i.set(i.get()+2);',
  4111. '};',
  4112. 'this.DoIt = function (v) {',
  4113. ' var p = [];',
  4114. ' $mod.Inc2({get: function () {',
  4115. ' return v;',
  4116. ' }, set: function (w) {',
  4117. ' v = w;',
  4118. ' }});',
  4119. ' $mod.Inc2({',
  4120. ' a: v,',
  4121. ' p: p,',
  4122. ' get: function () {',
  4123. ' return this.p[this.a];',
  4124. ' },',
  4125. ' set: function (v) {',
  4126. ' this.p[this.a] = v;',
  4127. ' }',
  4128. ' });',
  4129. '};',
  4130. '']),
  4131. LinesToStr([
  4132. '']));
  4133. end;
  4134. procedure TTestModule.TestProc_Overload;
  4135. begin
  4136. StartProgram(false);
  4137. Add('procedure DoIt(vI: longint); begin end;');
  4138. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4139. Add('procedure DoIt(vD: double); begin end;');
  4140. Add('begin');
  4141. Add(' DoIt(1);');
  4142. Add(' DoIt(2,3);');
  4143. Add(' DoIt(4.5);');
  4144. ConvertProgram;
  4145. CheckSource('TestProcedureOverload',
  4146. LinesToStr([ // statements
  4147. 'this.DoIt = function (vI) {',
  4148. '};',
  4149. 'this.DoIt$1 = function (vI, vJ) {',
  4150. '};',
  4151. 'this.DoIt$2 = function (vD) {',
  4152. '};',
  4153. '']),
  4154. LinesToStr([
  4155. '$mod.DoIt(1);',
  4156. '$mod.DoIt$1(2, 3);',
  4157. '$mod.DoIt$2(4.5);',
  4158. '']));
  4159. end;
  4160. procedure TTestModule.TestProc_OverloadForward;
  4161. begin
  4162. StartProgram(false);
  4163. Add('procedure DoIt(vI: longint); forward;');
  4164. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4165. Add('procedure doit(vi: longint); begin end;');
  4166. Add('begin');
  4167. Add(' doit(1);');
  4168. Add(' doit(2,3);');
  4169. ConvertProgram;
  4170. CheckSource('TestProcedureOverloadForward',
  4171. LinesToStr([ // statements
  4172. 'this.DoIt$1 = function (vI, vJ) {',
  4173. '};',
  4174. 'this.DoIt = function (vI) {',
  4175. '};',
  4176. '']),
  4177. LinesToStr([
  4178. '$mod.DoIt(1);',
  4179. '$mod.DoIt$1(2, 3);',
  4180. '']));
  4181. end;
  4182. procedure TTestModule.TestProc_OverloadIntfImpl;
  4183. begin
  4184. StartUnit(false);
  4185. Add('interface');
  4186. Add('procedure DoIt(vI: longint);');
  4187. Add('procedure DoIt(vI, vJ: longint);');
  4188. Add('implementation');
  4189. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4190. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4191. Add('procedure DoIt(vi: longint); begin end;');
  4192. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4193. Add('procedure DoIt(vi, vj: longint); begin end;');
  4194. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4195. Add('begin');
  4196. Add(' doit(1);');
  4197. Add(' doit(2,3);');
  4198. Add(' doit(4,5,6);');
  4199. Add(' doit(7,8,9,10);');
  4200. Add(' doit(11,12,13,14,15);');
  4201. ConvertUnit;
  4202. CheckSource('TestProcedureOverloadUnit',
  4203. LinesToStr([ // statements
  4204. 'var $impl = $mod.$impl;',
  4205. 'this.DoIt = function (vI) {',
  4206. '};',
  4207. 'this.DoIt$1 = function (vI, vJ) {',
  4208. '};',
  4209. '']),
  4210. LinesToStr([ // this.$init
  4211. '$mod.DoIt(1);',
  4212. '$mod.DoIt$1(2, 3);',
  4213. '$impl.DoIt$3(4,5,6);',
  4214. '$impl.DoIt$4(7,8,9,10);',
  4215. '$impl.DoIt$2(11,12,13,14,15);',
  4216. '']),
  4217. LinesToStr([ // implementation
  4218. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4219. '};',
  4220. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4221. '};',
  4222. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4223. '};',
  4224. '']));
  4225. end;
  4226. procedure TTestModule.TestProc_OverloadNested;
  4227. begin
  4228. StartProgram(false);
  4229. Add([
  4230. 'procedure doit(vA: longint);',
  4231. ' procedure DoIt(vA, vB: longint); overload;',
  4232. ' begin',
  4233. ' doit(1);',
  4234. ' doit(1,2);',
  4235. ' end;',
  4236. ' procedure doit(vA, vB, vC: longint);',
  4237. ' begin',
  4238. ' doit(1);',
  4239. ' doit(1,2);',
  4240. ' doit(1,2,3);',
  4241. ' end;',
  4242. 'begin',
  4243. ' doit(1);',
  4244. ' doit(1,2);',
  4245. ' doit(1,2,3);',
  4246. 'end;',
  4247. 'begin // main',
  4248. ' doit(1);']);
  4249. ConvertProgram;
  4250. CheckSource('TestProcedureOverloadNested',
  4251. LinesToStr([ // statements
  4252. 'this.doit = function (vA) {',
  4253. ' function DoIt$1(vA, vB) {',
  4254. ' $mod.doit(1);',
  4255. ' DoIt$1(1, 2);',
  4256. ' };',
  4257. ' function doit$2(vA, vB, vC) {',
  4258. ' $mod.doit(1);',
  4259. ' DoIt$1(1, 2);',
  4260. ' doit$2(1, 2, 3);',
  4261. ' };',
  4262. ' $mod.doit(1);',
  4263. ' DoIt$1(1, 2);',
  4264. ' doit$2(1, 2, 3);',
  4265. '};',
  4266. '']),
  4267. LinesToStr([
  4268. '$mod.doit(1);',
  4269. '']));
  4270. end;
  4271. procedure TTestModule.TestProc_OverloadNestedForward;
  4272. begin
  4273. StartProgram(false);
  4274. Add([
  4275. 'procedure DoIt(vA: longint); overload; forward;',
  4276. 'procedure DoIt(vB, vC: longint); overload;',
  4277. 'begin // 2 param overload',
  4278. ' doit(1);',
  4279. ' doit(1,2);',
  4280. 'end;',
  4281. 'procedure doit(vA: longint);',
  4282. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4283. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4284. ' begin // 4 param overload',
  4285. ' doit(1);',
  4286. ' doit(1,2);',
  4287. ' doit(1,2,3);',
  4288. ' doit(1,2,3,4);',
  4289. ' end;',
  4290. ' procedure doit(vA, vB, vC: longint);',
  4291. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4292. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4293. ' begin // 6 param overload',
  4294. ' doit(1);',
  4295. ' doit(1,2);',
  4296. ' doit(1,2,3);',
  4297. ' doit(1,2,3,4);',
  4298. ' doit(1,2,3,4,5);',
  4299. ' doit(1,2,3,4,5,6);',
  4300. ' end;',
  4301. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4302. ' begin // 5 param overload',
  4303. ' doit(1);',
  4304. ' doit(1,2);',
  4305. ' doit(1,2,3);',
  4306. ' doit(1,2,3,4);',
  4307. ' doit(1,2,3,4,5);',
  4308. ' doit(1,2,3,4,5,6);',
  4309. ' end;',
  4310. ' begin // 3 param overload',
  4311. ' doit(1);',
  4312. ' doit(1,2);',
  4313. ' doit(1,2,3);',
  4314. ' doit(1,2,3,4);',
  4315. ' doit(1,2,3,4,5);',
  4316. ' doit(1,2,3,4,5,6);',
  4317. ' end;',
  4318. 'begin // 1 param overload',
  4319. ' doit(1);',
  4320. ' doit(1,2);',
  4321. ' doit(1,2,3);',
  4322. ' doit(1,2,3,4);',
  4323. 'end;',
  4324. 'begin // main',
  4325. ' doit(1);',
  4326. ' doit(1,2);']);
  4327. ConvertProgram;
  4328. CheckSource('TestProc_OverloadNestedForward',
  4329. LinesToStr([ // statements
  4330. 'this.DoIt$1 = function (vB, vC) {',
  4331. ' $mod.DoIt(1);',
  4332. ' $mod.DoIt$1(1, 2);',
  4333. '};',
  4334. 'this.DoIt = function (vA) {',
  4335. ' function DoIt$3(vA, vB, vC, vD) {',
  4336. ' $mod.DoIt(1);',
  4337. ' $mod.DoIt$1(1, 2);',
  4338. ' DoIt$2(1, 2, 3);',
  4339. ' DoIt$3(1, 2, 3, 4);',
  4340. ' };',
  4341. ' function DoIt$2(vA, vB, vC) {',
  4342. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4343. ' $mod.DoIt(1);',
  4344. ' $mod.DoIt$1(1, 2);',
  4345. ' DoIt$2(1, 2, 3);',
  4346. ' DoIt$3(1, 2, 3, 4);',
  4347. ' DoIt$4(1, 2, 3, 4, 5);',
  4348. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4349. ' };',
  4350. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4351. ' $mod.DoIt(1);',
  4352. ' $mod.DoIt$1(1, 2);',
  4353. ' DoIt$2(1, 2, 3);',
  4354. ' DoIt$3(1, 2, 3, 4);',
  4355. ' DoIt$4(1, 2, 3, 4, 5);',
  4356. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4357. ' };',
  4358. ' $mod.DoIt(1);',
  4359. ' $mod.DoIt$1(1, 2);',
  4360. ' DoIt$2(1, 2, 3);',
  4361. ' DoIt$3(1, 2, 3, 4);',
  4362. ' DoIt$4(1, 2, 3, 4, 5);',
  4363. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4364. ' };',
  4365. ' $mod.DoIt(1);',
  4366. ' $mod.DoIt$1(1, 2);',
  4367. ' DoIt$2(1, 2, 3);',
  4368. ' DoIt$3(1, 2, 3, 4);',
  4369. '};',
  4370. '']),
  4371. LinesToStr([
  4372. '$mod.DoIt(1);',
  4373. '$mod.DoIt$1(1, 2);',
  4374. '']));
  4375. end;
  4376. procedure TTestModule.TestProc_OverloadUnitCycle;
  4377. begin
  4378. AddModuleWithIntfImplSrc('Unit2.pas',
  4379. LinesToStr([
  4380. 'type',
  4381. ' TObject = class',
  4382. ' procedure DoIt(b: boolean); virtual; abstract;',
  4383. ' procedure DoIt(i: longint); virtual; abstract;',
  4384. ' end;',
  4385. '']),
  4386. 'uses test1;');
  4387. StartUnit(true);
  4388. Add([
  4389. 'interface',
  4390. 'uses unit2;',
  4391. 'type',
  4392. ' TEagle = class(TObject)',
  4393. ' procedure DoIt(b: boolean); override;',
  4394. ' procedure DoIt(i: longint); override;',
  4395. ' end;',
  4396. 'implementation',
  4397. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4398. 'procedure TEagle.DoIt(i: longint); begin end;',
  4399. '']);
  4400. ConvertUnit;
  4401. CheckSource('TestProc_OverloadUnitCycle',
  4402. LinesToStr([ // statements
  4403. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4404. ' this.DoIt = function (b) {',
  4405. ' };',
  4406. ' this.DoIt$1 = function (i) {',
  4407. ' };',
  4408. '});',
  4409. '']),
  4410. '',
  4411. LinesToStr([
  4412. '']));
  4413. end;
  4414. procedure TTestModule.TestProc_Varargs;
  4415. begin
  4416. StartProgram(false);
  4417. Add([
  4418. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4419. 'procedure ProcB; varargs; external name ''ProcB'';',
  4420. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4421. 'function GetIt: longint; begin end;',
  4422. 'begin',
  4423. ' ProcA(1);',
  4424. ' ProcA(1,2);',
  4425. ' ProcA(1,2.0);',
  4426. ' ProcA(1,2,3);',
  4427. ' ProcA(1,''2'');',
  4428. ' ProcA(2,'''');',
  4429. ' ProcA(3,false);',
  4430. ' ProcB;',
  4431. ' ProcB();',
  4432. ' ProcB(4);',
  4433. ' ProcB(''foo'');',
  4434. ' ProcC;',
  4435. ' ProcC();',
  4436. ' ProcC(4);',
  4437. ' ProcC(5,''foo'');',
  4438. ' ProcB(GetIt);',
  4439. ' ProcB(GetIt());',
  4440. ' ProcB(GetIt,GetIt());']);
  4441. ConvertProgram;
  4442. CheckSource('TestProc_Varargs',
  4443. LinesToStr([ // statements
  4444. 'this.GetIt = function () {',
  4445. ' var Result = 0;',
  4446. ' return Result;',
  4447. '};',
  4448. '']),
  4449. LinesToStr([
  4450. 'ProcA(1);',
  4451. 'ProcA(1, 2);',
  4452. 'ProcA(1, 2.0);',
  4453. 'ProcA(1, 2, 3);',
  4454. 'ProcA(1, "2");',
  4455. 'ProcA(2, "");',
  4456. 'ProcA(3, false);',
  4457. 'ProcB();',
  4458. 'ProcB();',
  4459. 'ProcB(4);',
  4460. 'ProcB("foo");',
  4461. 'ProcC(17);',
  4462. 'ProcC(17);',
  4463. 'ProcC(4);',
  4464. 'ProcC(5, "foo");',
  4465. 'ProcB($mod.GetIt());',
  4466. 'ProcB($mod.GetIt());',
  4467. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4468. '']));
  4469. end;
  4470. procedure TTestModule.TestProc_ConstOrder;
  4471. begin
  4472. StartProgram(false);
  4473. Add([
  4474. 'const A = 3;',
  4475. 'const B = A+1;',
  4476. 'procedure DoIt;',
  4477. 'const C = A+1;',
  4478. 'const D = B+1;',
  4479. 'const E = D+C+B+A;',
  4480. 'begin',
  4481. 'end;',
  4482. 'begin'
  4483. ]);
  4484. ConvertProgram;
  4485. CheckSource('TestProc_ConstOrder',
  4486. LinesToStr([ // statements
  4487. 'this.A = 3;',
  4488. 'this.B = 3 + 1;',
  4489. 'var C = 3 + 1;',
  4490. 'var D = 4 + 1;',
  4491. 'var E = 5 + 4 + 4 + 3;',
  4492. 'this.DoIt = function () {',
  4493. '};',
  4494. '']),
  4495. LinesToStr([
  4496. ''
  4497. ]));
  4498. end;
  4499. procedure TTestModule.TestProc_DuplicateConst;
  4500. begin
  4501. StartProgram(false);
  4502. Add([
  4503. 'const A = 1;',
  4504. 'procedure DoIt;',
  4505. 'const A = 2;',
  4506. ' procedure SubIt;',
  4507. ' const A = 21;',
  4508. ' begin',
  4509. ' end;',
  4510. 'begin',
  4511. 'end;',
  4512. 'procedure DoSome;',
  4513. 'const A = 3;',
  4514. 'begin',
  4515. 'end;',
  4516. 'begin'
  4517. ]);
  4518. ConvertProgram;
  4519. CheckSource('TestProc_DuplicateConst',
  4520. LinesToStr([ // statements
  4521. 'this.A = 1;',
  4522. 'var A$1 = 2;',
  4523. 'var A$2 = 21;',
  4524. 'this.DoIt = function () {',
  4525. ' function SubIt() {',
  4526. ' };',
  4527. '};',
  4528. 'var A$3 = 3;',
  4529. 'this.DoSome = function () {',
  4530. '};',
  4531. '']),
  4532. LinesToStr([
  4533. ''
  4534. ]));
  4535. end;
  4536. procedure TTestModule.TestProc_LocalVarAbsolute;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. 'type',
  4541. ' TObject = class',
  4542. ' Index: longint;',
  4543. ' procedure DoAbs(Item: pointer);',
  4544. ' end;',
  4545. 'procedure TObject.DoAbs(Item: pointer);',
  4546. 'var',
  4547. ' o: TObject absolute Item;',
  4548. 'begin',
  4549. ' if o.Index<o.Index then o.Index:=o.Index;',
  4550. 'end;',
  4551. 'procedure DoIt(i: longint; p: pointer);',
  4552. 'var',
  4553. ' d: double absolute i;',
  4554. ' s: string absolute d;',
  4555. ' oi: TObject absolute i;',
  4556. ' op: TObject absolute p;',
  4557. 'begin',
  4558. ' if d=d then d:=d;',
  4559. ' if s=s then s:=s;',
  4560. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4561. ' if op.Index=op.Index then op.Index:=op.Index;',
  4562. 'end;',
  4563. 'begin']);
  4564. ConvertProgram;
  4565. CheckSource('TestProc_LocalVarAbsolute',
  4566. LinesToStr([ // statements
  4567. 'rtl.createClass(this, "TObject", null, function () {',
  4568. ' this.$init = function () {',
  4569. ' this.Index = 0;',
  4570. ' };',
  4571. ' this.$final = function () {',
  4572. ' };',
  4573. ' this.DoAbs = function (Item) {',
  4574. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4575. ' };',
  4576. '});',
  4577. 'this.DoIt = function (i, p) {',
  4578. ' if (i === i) i = i;',
  4579. ' if (i === i) i = i;',
  4580. ' if (i.Index < i.Index) i.Index = i.Index;',
  4581. ' if (p.Index === p.Index) p.Index = p.Index;',
  4582. '};'
  4583. ]),
  4584. LinesToStr([
  4585. ]));
  4586. end;
  4587. procedure TTestModule.TestProc_LocalVarInit;
  4588. begin
  4589. StartProgram(false);
  4590. Add([
  4591. 'type TBytes = array of byte;',
  4592. 'procedure DoIt;',
  4593. 'const c = 4;',
  4594. 'var',
  4595. ' b: byte = 1;',
  4596. ' w: word = 2+c;',
  4597. ' p: pointer = nil;',
  4598. ' Buffer: TBytes = nil;',
  4599. 'begin',
  4600. 'end;',
  4601. 'begin']);
  4602. ConvertProgram;
  4603. CheckSource('TestProc_LocalVarInit',
  4604. LinesToStr([ // statements
  4605. 'var c = 4;',
  4606. 'this.DoIt = function () {',
  4607. ' var b = 1;',
  4608. ' var w = 2 + 4;',
  4609. ' var p = null;',
  4610. ' var Buffer = [];',
  4611. '};',
  4612. '']),
  4613. LinesToStr([
  4614. ]));
  4615. end;
  4616. procedure TTestModule.TestProc_ReservedWords;
  4617. begin
  4618. StartProgram(false);
  4619. Add([
  4620. 'procedure Date(ArrayBuffer: longint);',
  4621. 'const',
  4622. ' NaN: longint = 3;',
  4623. 'var',
  4624. ' &Boolean: longint;',
  4625. ' procedure Error(ArrayBuffer: longint);',
  4626. ' begin',
  4627. ' end;',
  4628. 'begin',
  4629. ' Nan:=&bOolean;',
  4630. 'end;',
  4631. 'begin',
  4632. ' Date(1);']);
  4633. ConvertProgram;
  4634. CheckSource('TestProc_ReservedWords',
  4635. LinesToStr([ // statements
  4636. 'var naN = 3;',
  4637. 'this.Date = function (arrayBuffer) {',
  4638. ' var boolean = 0;',
  4639. ' function error(arrayBuffer) {',
  4640. ' };',
  4641. ' naN = boolean;',
  4642. '};',
  4643. '']),
  4644. LinesToStr([
  4645. ' $mod.Date(1);'
  4646. ]));
  4647. end;
  4648. procedure TTestModule.TestProc_ConstRefWord;
  4649. begin
  4650. StartProgram(false);
  4651. Add([
  4652. 'procedure Run(constref w: word);',
  4653. 'var l: word;',
  4654. 'begin',
  4655. ' l:=w;',
  4656. ' Run(w);',
  4657. ' Run(l);',
  4658. 'end;',
  4659. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4660. 'begin',
  4661. ' Run(a);',
  4662. ' Run(b);',
  4663. ' Run(c);',
  4664. ' Run(d);',
  4665. ' Run(e);',
  4666. 'end;',
  4667. 'begin',
  4668. ' Run(1);']);
  4669. ConvertProgram;
  4670. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4671. CheckSource('TestProc_ConstRefWord',
  4672. LinesToStr([ // statements
  4673. 'this.Run = function (w) {',
  4674. ' var l = 0;',
  4675. ' l = w;',
  4676. ' $mod.Run(w);',
  4677. ' $mod.Run(l);',
  4678. '};',
  4679. 'this.Fly = function (a, b, c, d, e) {',
  4680. ' $mod.Run(a);',
  4681. ' $mod.Run(b.get());',
  4682. ' $mod.Run(c.get());',
  4683. ' $mod.Run(d);',
  4684. ' $mod.Run(e);',
  4685. '};',
  4686. '']),
  4687. LinesToStr([
  4688. '$mod.Run(1);'
  4689. ]));
  4690. end;
  4691. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4692. begin
  4693. StartProgram(false);
  4694. Add([
  4695. '{$mode objfpc}',
  4696. 'type',
  4697. ' TFunc = reference to function(x: word): word;',
  4698. 'var Func: TFunc;',
  4699. 'procedure DoIt(a: word);',
  4700. 'begin',
  4701. ' Func:=function(b:word): word',
  4702. ' begin',
  4703. ' Result:=a+b;',
  4704. ' exit(b);',
  4705. ' exit(Result);',
  4706. ' end;',// test semicolon
  4707. ' a:=3;',
  4708. 'end;',
  4709. 'begin',
  4710. ' Func:=function(c:word):word begin',
  4711. ' Result:=3+c;',
  4712. ' exit(c);',
  4713. ' exit(Result);',
  4714. ' end;']);
  4715. ConvertProgram;
  4716. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4717. LinesToStr([ // statements
  4718. 'this.Func = null;',
  4719. 'this.DoIt = function (a) {',
  4720. ' $mod.Func = function (b) {',
  4721. ' var Result = 0;',
  4722. ' Result = a + b;',
  4723. ' return b;',
  4724. ' return Result;',
  4725. ' return Result;',
  4726. ' };',
  4727. ' a = 3;',
  4728. '};',
  4729. '']),
  4730. LinesToStr([
  4731. '$mod.Func = function (c) {',
  4732. ' var Result = 0;',
  4733. ' Result = 3 + c;',
  4734. ' return c;',
  4735. ' return Result;',
  4736. ' return Result;',
  4737. '};',
  4738. '']));
  4739. end;
  4740. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4741. begin
  4742. StartProgram(false);
  4743. Add([
  4744. '{$mode delphi}',
  4745. 'type',
  4746. ' TProc = reference to procedure(x: word);',
  4747. 'procedure DoIt(a: word);',
  4748. 'var Proc: TProc;',
  4749. 'begin',
  4750. ' Proc:=procedure(b:word) begin end;',
  4751. 'end;',
  4752. 'var Proc: TProc;',
  4753. 'begin',
  4754. ' Proc:=procedure(c:word) begin end;',
  4755. '']);
  4756. ConvertProgram;
  4757. CheckSource('TestAnonymousProc_Assign_Delphi',
  4758. LinesToStr([ // statements
  4759. 'this.DoIt = function (a) {',
  4760. ' var Proc = null;',
  4761. ' Proc = function (b) {',
  4762. ' };',
  4763. '};',
  4764. 'this.Proc = null;',
  4765. '']),
  4766. LinesToStr([
  4767. '$mod.Proc = function (c) {',
  4768. '};',
  4769. '']));
  4770. end;
  4771. procedure TTestModule.TestAnonymousProc_Arg;
  4772. begin
  4773. StartProgram(false);
  4774. Add([
  4775. 'type',
  4776. ' TProc = reference to procedure;',
  4777. ' TFunc = reference to function(x: word): word;',
  4778. 'procedure DoMore(f,g: TProc);',
  4779. 'begin',
  4780. 'end;',
  4781. 'procedure DoOdd(v: jsvalue);',
  4782. 'begin',
  4783. 'end;',
  4784. 'procedure DoIt(f: TFunc);',
  4785. 'begin',
  4786. ' DoIt(function(b:word): word',
  4787. ' begin',
  4788. ' Result:=1+b;',
  4789. ' end);',
  4790. ' DoMore(procedure begin end, procedure begin end);',
  4791. ' DoOdd(procedure begin end);',
  4792. 'end;',
  4793. 'begin',
  4794. ' DoMore(procedure begin end,',
  4795. ' procedure assembler asm',
  4796. ' console.log("c");',
  4797. ' end);',
  4798. '']);
  4799. ConvertProgram;
  4800. CheckSource('TestAnonymousProc_Arg',
  4801. LinesToStr([ // statements
  4802. 'this.DoMore = function (f, g) {',
  4803. '};',
  4804. 'this.DoOdd = function (v) {',
  4805. '};',
  4806. 'this.DoIt = function (f) {',
  4807. ' $mod.DoIt(function (b) {',
  4808. ' var Result = 0;',
  4809. ' Result = 1 + b;',
  4810. ' return Result;',
  4811. ' });',
  4812. ' $mod.DoMore(function () {',
  4813. ' }, function () {',
  4814. ' });',
  4815. ' $mod.DoOdd(function () {',
  4816. ' });',
  4817. '};',
  4818. '']),
  4819. LinesToStr([
  4820. '$mod.DoMore(function () {',
  4821. '}, function () {',
  4822. ' console.log("c");',
  4823. '});',
  4824. '']));
  4825. end;
  4826. procedure TTestModule.TestAnonymousProc_Typecast;
  4827. begin
  4828. StartProgram(false);
  4829. Add([
  4830. 'type',
  4831. ' TProc = reference to procedure(w: word);',
  4832. ' TArr = array of word;',
  4833. ' TFuncArr = reference to function: TArr;',
  4834. 'procedure DoIt(p: TProc);',
  4835. 'var',
  4836. ' w: word;',
  4837. ' a: TArr;',
  4838. 'begin',
  4839. ' p:=TProc(procedure(b: smallint) begin end);',
  4840. ' a:=TFuncArr(function: TArr begin end)();',
  4841. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4842. 'end;',
  4843. 'begin']);
  4844. ConvertProgram;
  4845. CheckSource('TestAnonymousProc_Typecast',
  4846. LinesToStr([ // statements
  4847. 'this.DoIt = function (p) {',
  4848. ' var w = 0;',
  4849. ' var a = [];',
  4850. ' p = function (b) {',
  4851. ' };',
  4852. ' a = function () {',
  4853. ' var Result = [];',
  4854. ' return Result;',
  4855. ' }();',
  4856. ' w = function () {',
  4857. ' var Result = [];',
  4858. ' return Result;',
  4859. ' }()[3];',
  4860. '};',
  4861. '']),
  4862. LinesToStr([
  4863. '']));
  4864. end;
  4865. procedure TTestModule.TestAnonymousProc_With;
  4866. begin
  4867. StartProgram(false);
  4868. Add([
  4869. 'type',
  4870. ' TProc = reference to procedure(w: word);',
  4871. ' TObject = class',
  4872. ' b: boolean;',
  4873. ' end;',
  4874. 'var',
  4875. ' p: TProc;',
  4876. ' bird: TObject;',
  4877. 'begin',
  4878. ' with bird do',
  4879. ' p:=procedure(w: word)',
  4880. ' begin',
  4881. ' b:=w>2;',
  4882. ' end;',
  4883. '']);
  4884. ConvertProgram;
  4885. CheckSource('TestAnonymousProc_With',
  4886. LinesToStr([ // statements
  4887. 'rtl.createClass(this, "TObject", null, function () {',
  4888. ' this.$init = function () {',
  4889. ' this.b = false;',
  4890. ' };',
  4891. ' this.$final = function () {',
  4892. ' };',
  4893. '});',
  4894. 'this.p = null;',
  4895. 'this.bird = null;',
  4896. '']),
  4897. LinesToStr([
  4898. 'var $with = $mod.bird;',
  4899. '$mod.p = function (w) {',
  4900. ' $with.b = w > 2;',
  4901. '};',
  4902. '']));
  4903. end;
  4904. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4905. begin
  4906. StartProgram(false);
  4907. Add([
  4908. 'type',
  4909. ' TProc = reference to procedure;',
  4910. ' TObject = class',
  4911. ' b: boolean;',
  4912. ' end;',
  4913. 'procedure DoIt;',
  4914. 'var',
  4915. ' p: TProc;',
  4916. 'begin',
  4917. ' try',
  4918. ' except',
  4919. ' on E: TObject do',
  4920. ' p:=procedure',
  4921. ' begin',
  4922. ' E.b:=true;',
  4923. ' end;',
  4924. ' end;',
  4925. 'end;',
  4926. 'begin']);
  4927. ConvertProgram;
  4928. CheckSource('TestAnonymousProc_ExceptOn',
  4929. LinesToStr([ // statements
  4930. 'rtl.createClass(this, "TObject", null, function () {',
  4931. ' this.$init = function () {',
  4932. ' this.b = false;',
  4933. ' };',
  4934. ' this.$final = function () {',
  4935. ' };',
  4936. '});',
  4937. 'this.DoIt = function () {',
  4938. ' var p = null;',
  4939. ' try {} catch ($e) {',
  4940. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4941. ' var E = $e;',
  4942. ' p = function () {',
  4943. ' E.b = true;',
  4944. ' };',
  4945. ' } else throw $e',
  4946. ' };',
  4947. '};',
  4948. '']),
  4949. LinesToStr([
  4950. '']));
  4951. end;
  4952. procedure TTestModule.TestAnonymousProc_Nested;
  4953. begin
  4954. StartProgram(false);
  4955. Add([
  4956. 'type',
  4957. ' TProc = reference to procedure;',
  4958. ' TObject = class',
  4959. ' i: byte;',
  4960. ' procedure DoIt;',
  4961. ' end;',
  4962. 'procedure TObject.DoIt;',
  4963. 'var',
  4964. ' p: TProc;',
  4965. ' procedure Sub;',
  4966. ' begin',
  4967. ' p:=procedure',
  4968. ' begin',
  4969. ' i:=3;',
  4970. ' Self.i:=4;',
  4971. ' p:=procedure',
  4972. ' procedure SubSub;',
  4973. ' begin',
  4974. ' i:=13;',
  4975. ' Self.i:=14;',
  4976. ' end;',
  4977. ' begin',
  4978. ' i:=13;',
  4979. ' Self.i:=14;',
  4980. ' end;',
  4981. ' end;',
  4982. ' end;',
  4983. 'begin',
  4984. 'end;',
  4985. 'begin']);
  4986. ConvertProgram;
  4987. CheckSource('TestAnonymousProc_Nested',
  4988. LinesToStr([ // statements
  4989. 'rtl.createClass(this, "TObject", null, function () {',
  4990. ' this.$init = function () {',
  4991. ' this.i = 0;',
  4992. ' };',
  4993. ' this.$final = function () {',
  4994. ' };',
  4995. ' this.DoIt = function () {',
  4996. ' var $Self = this;',
  4997. ' var p = null;',
  4998. ' function Sub() {',
  4999. ' p = function () {',
  5000. ' $Self.i = 3;',
  5001. ' $Self.i = 4;',
  5002. ' p = function () {',
  5003. ' function SubSub() {',
  5004. ' $Self.i = 13;',
  5005. ' $Self.i = 14;',
  5006. ' };',
  5007. ' $Self.i = 13;',
  5008. ' $Self.i = 14;',
  5009. ' };',
  5010. ' };',
  5011. ' };',
  5012. ' };',
  5013. '});',
  5014. '']),
  5015. LinesToStr([
  5016. '']));
  5017. end;
  5018. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5019. begin
  5020. StartProgram(false);
  5021. Add([
  5022. 'type',
  5023. ' TProc = reference to procedure;',
  5024. 'function DoIt: TProc;',
  5025. ' function Sub: TProc;',
  5026. ' begin',
  5027. ' Result:=procedure',
  5028. ' begin',
  5029. ' Sub:=procedure',
  5030. ' procedure SubSub;',
  5031. ' begin',
  5032. ' Result:=nil;',
  5033. ' Sub:=nil;',
  5034. ' DoIt:=nil;',
  5035. ' end;',
  5036. ' begin',
  5037. ' Result:=nil;',
  5038. ' Sub:=nil;',
  5039. ' DoIt:=nil;',
  5040. ' end;',
  5041. ' end;',
  5042. ' end;',
  5043. 'begin',
  5044. 'end;',
  5045. 'begin']);
  5046. ConvertProgram;
  5047. CheckSource('TestAnonymousProc_NestedAssignResult',
  5048. LinesToStr([ // statements
  5049. 'this.DoIt = function () {',
  5050. ' var Result = null;',
  5051. ' function Sub() {',
  5052. ' var Result$1 = null;',
  5053. ' Result$1 = function () {',
  5054. ' Result$1 = function () {',
  5055. ' function SubSub() {',
  5056. ' Result$1 = null;',
  5057. ' Result$1 = null;',
  5058. ' Result = null;',
  5059. ' };',
  5060. ' Result$1 = null;',
  5061. ' Result$1 = null;',
  5062. ' Result = null;',
  5063. ' };',
  5064. ' };',
  5065. ' return Result$1;',
  5066. ' };',
  5067. ' return Result;',
  5068. '};',
  5069. '']),
  5070. LinesToStr([
  5071. '']));
  5072. end;
  5073. procedure TTestModule.TestAnonymousProc_Class;
  5074. begin
  5075. StartProgram(false);
  5076. Add([
  5077. 'type',
  5078. ' TProc = reference to procedure;',
  5079. ' TEvent = procedure of object;',
  5080. ' TObject = class',
  5081. ' Size: word;',
  5082. ' function GetIt: TProc;',
  5083. ' procedure DoIt; virtual; abstract;',
  5084. ' end;',
  5085. 'function TObject.GetIt: TProc;',
  5086. 'begin',
  5087. ' Result:=procedure',
  5088. ' var p: TEvent;',
  5089. ' begin',
  5090. ' Size:=Size;',
  5091. ' Size:=Self.Size;',
  5092. ' p:=@DoIt;',
  5093. ' p:[email protected];',
  5094. ' end;',
  5095. 'end;',
  5096. 'begin']);
  5097. ConvertProgram;
  5098. CheckSource('TestAnonymousProc_Class',
  5099. LinesToStr([ // statements
  5100. 'rtl.createClass(this, "TObject", null, function () {',
  5101. ' this.$init = function () {',
  5102. ' this.Size = 0;',
  5103. ' };',
  5104. ' this.$final = function () {',
  5105. ' };',
  5106. ' this.GetIt = function () {',
  5107. ' var $Self = this;',
  5108. ' var Result = null;',
  5109. ' Result = function () {',
  5110. ' var p = null;',
  5111. ' $Self.Size = $Self.Size;',
  5112. ' $Self.Size = $Self.Size;',
  5113. ' p = rtl.createCallback($Self, "DoIt");',
  5114. ' p = rtl.createCallback($Self, "DoIt");',
  5115. ' };',
  5116. ' return Result;',
  5117. ' };',
  5118. '});',
  5119. '']),
  5120. LinesToStr([
  5121. '']));
  5122. end;
  5123. procedure TTestModule.TestAnonymousProc_ForLoop;
  5124. begin
  5125. StartProgram(false);
  5126. Add([
  5127. 'type TProc = reference to procedure;',
  5128. 'procedure Foo(p: TProc);',
  5129. 'begin',
  5130. 'end;',
  5131. 'procedure DoIt;',
  5132. 'var i: word;',
  5133. ' a: word;',
  5134. 'begin',
  5135. ' for i:=1 to 10 do begin',
  5136. ' Foo(procedure begin a:=3; end);',
  5137. ' end;',
  5138. 'end;',
  5139. 'begin',
  5140. ' DoIt;']);
  5141. ConvertProgram;
  5142. CheckSource('TestAnonymousProc_ForLoop',
  5143. LinesToStr([ // statements
  5144. 'this.Foo = function (p) {',
  5145. '};',
  5146. 'this.DoIt = function () {',
  5147. ' var i = 0;',
  5148. ' var a = 0;',
  5149. ' for (i = 1; i <= 10; i++) {',
  5150. ' $mod.Foo(function () {',
  5151. ' a = 3;',
  5152. ' });',
  5153. ' };',
  5154. '};',
  5155. '']),
  5156. LinesToStr([
  5157. '$mod.DoIt();'
  5158. ]));
  5159. end;
  5160. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5161. begin
  5162. StartProgram(false);
  5163. Add([
  5164. '{$mode delphi}',
  5165. 'type',
  5166. ' TProc = reference to procedure;',
  5167. ' TFunc = reference to function(x: word): word;',
  5168. 'procedure Run;',
  5169. 'asm',
  5170. 'end;',
  5171. 'procedure Walk(p: TProc; f: TFunc);',
  5172. 'begin',
  5173. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5174. 'end;',
  5175. 'begin',
  5176. ' Walk(procedure',
  5177. ' asm',
  5178. ' console.log("a");',
  5179. ' end,',
  5180. ' function(x: word): word asm',
  5181. ' console.log("c");',
  5182. ' end);',
  5183. '']);
  5184. ConvertProgram;
  5185. CheckSource('TestAnonymousProc_AsmDelphi',
  5186. LinesToStr([ // statements
  5187. 'this.Run = function () {',
  5188. '};',
  5189. 'this.Walk = function (p, f) {',
  5190. ' $mod.Walk(function () {',
  5191. ' }, function (b) {',
  5192. ' return 1+b;',
  5193. ' });',
  5194. '};',
  5195. '']),
  5196. LinesToStr([
  5197. '$mod.Walk(function () {',
  5198. ' console.log("a");',
  5199. '}, function (x) {',
  5200. ' console.log("c");',
  5201. '});',
  5202. '']));
  5203. end;
  5204. procedure TTestModule.TestEnum_Name;
  5205. begin
  5206. StartProgram(false);
  5207. Add('type TMyEnum = (Red, Green, Blue);');
  5208. Add('var e: TMyEnum;');
  5209. Add('var f: TMyEnum = Blue;');
  5210. Add('begin');
  5211. Add(' e:=green;');
  5212. Add(' e:=default(TMyEnum);');
  5213. ConvertProgram;
  5214. CheckSource('TestEnum_Name',
  5215. LinesToStr([ // statements
  5216. 'this.TMyEnum = {',
  5217. ' "0":"Red",',
  5218. ' Red:0,',
  5219. ' "1":"Green",',
  5220. ' Green:1,',
  5221. ' "2":"Blue",',
  5222. ' Blue:2',
  5223. ' };',
  5224. 'this.e = 0;',
  5225. 'this.f = this.TMyEnum.Blue;'
  5226. ]),
  5227. LinesToStr([
  5228. '$mod.e=$mod.TMyEnum.Green;',
  5229. '$mod.e=$mod.TMyEnum.Red;'
  5230. ]));
  5231. end;
  5232. procedure TTestModule.TestEnum_Number;
  5233. begin
  5234. Converter.Options:=Converter.Options+[coEnumNumbers];
  5235. StartProgram(false);
  5236. Add('type TMyEnum = (Red, Green);');
  5237. Add('var');
  5238. Add(' e: TMyEnum;');
  5239. Add(' f: TMyEnum = Green;');
  5240. Add(' i: longint;');
  5241. Add('begin');
  5242. Add(' e:=green;');
  5243. Add(' i:=longint(e);');
  5244. ConvertProgram;
  5245. CheckSource('TestEnumNumber',
  5246. LinesToStr([ // statements
  5247. 'this.TMyEnum = {',
  5248. ' "0":"Red",',
  5249. ' Red:0,',
  5250. ' "1":"Green",',
  5251. ' Green:1',
  5252. ' };',
  5253. 'this.e = 0;',
  5254. 'this.f = 1;',
  5255. 'this.i = 0;'
  5256. ]),
  5257. LinesToStr([
  5258. '$mod.e=1;',
  5259. '$mod.i=$mod.e;'
  5260. ]));
  5261. end;
  5262. procedure TTestModule.TestEnum_ConstFail;
  5263. begin
  5264. StartProgram(false);
  5265. Add([
  5266. 'type TMyEnum = (Red = 100, Green = 101);',
  5267. 'var',
  5268. ' e: TMyEnum;',
  5269. ' f: TMyEnum = Green;',
  5270. 'begin',
  5271. ' e:=green;']);
  5272. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5273. ConvertProgram;
  5274. end;
  5275. procedure TTestModule.TestEnum_Functions;
  5276. begin
  5277. StartProgram(false);
  5278. Add([
  5279. 'type TMyEnum = (Red, Green);',
  5280. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5281. 'var',
  5282. ' v: longint;',
  5283. ' s: string;',
  5284. 'begin',
  5285. ' val(s,e,v);',
  5286. ' val(s,e,i);',
  5287. 'end;',
  5288. 'var',
  5289. ' e: TMyEnum;',
  5290. ' i: longint;',
  5291. ' s: string;',
  5292. ' b: boolean;',
  5293. 'begin',
  5294. ' i:=ord(red);',
  5295. ' i:=ord(green);',
  5296. ' i:=ord(e);',
  5297. ' i:=ord(b);',
  5298. ' e:=low(tmyenum);',
  5299. ' e:=low(e);',
  5300. ' b:=low(boolean);',
  5301. ' e:=high(tmyenum);',
  5302. ' e:=high(e);',
  5303. ' b:=high(boolean);',
  5304. ' e:=pred(green);',
  5305. ' e:=pred(e);',
  5306. ' b:=pred(b);',
  5307. ' e:=succ(red);',
  5308. ' e:=succ(e);',
  5309. ' b:=succ(b);',
  5310. ' e:=tmyenum(1);',
  5311. ' e:=tmyenum(i);',
  5312. ' s:=str(e);',
  5313. ' str(e,s);',
  5314. ' str(red,s);',
  5315. ' s:=str(e:3);',
  5316. ' writestr(s,e:3,red);',
  5317. ' val(s,e,i);',
  5318. ' i:=longint(e);']);
  5319. ConvertProgram;
  5320. CheckSource('TestEnum_Functions',
  5321. LinesToStr([ // statements
  5322. 'this.TMyEnum = {',
  5323. ' "0":"Red",',
  5324. ' Red:0,',
  5325. ' "1":"Green",',
  5326. ' Green:1',
  5327. ' };',
  5328. 'this.DoIt = function (e, i) {',
  5329. ' var v = 0;',
  5330. ' var s = "";',
  5331. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5332. ' v = w;',
  5333. ' }));',
  5334. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5335. '};',
  5336. 'this.e = 0;',
  5337. 'this.i = 0;',
  5338. 'this.s = "";',
  5339. 'this.b = false;',
  5340. '']),
  5341. LinesToStr([
  5342. '$mod.i=$mod.TMyEnum.Red;',
  5343. '$mod.i=$mod.TMyEnum.Green;',
  5344. '$mod.i=$mod.e;',
  5345. '$mod.i=$mod.b+0;',
  5346. '$mod.e=$mod.TMyEnum.Red;',
  5347. '$mod.e=$mod.TMyEnum.Red;',
  5348. '$mod.b=false;',
  5349. '$mod.e=$mod.TMyEnum.Green;',
  5350. '$mod.e=$mod.TMyEnum.Green;',
  5351. '$mod.b=true;',
  5352. '$mod.e=$mod.TMyEnum.Green-1;',
  5353. '$mod.e=$mod.e-1;',
  5354. '$mod.b=false;',
  5355. '$mod.e=$mod.TMyEnum.Red+1;',
  5356. '$mod.e=$mod.e+1;',
  5357. '$mod.b=true;',
  5358. '$mod.e=1;',
  5359. '$mod.e=$mod.i;',
  5360. '$mod.s = $mod.TMyEnum[$mod.e];',
  5361. '$mod.s = $mod.TMyEnum[$mod.e];',
  5362. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5363. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5364. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5365. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5366. ' $mod.i = v;',
  5367. '});',
  5368. '$mod.i=$mod.e;',
  5369. '']));
  5370. end;
  5371. procedure TTestModule.TestEnumRg_Functions;
  5372. begin
  5373. StartProgram(false);
  5374. Add([
  5375. 'type',
  5376. ' TEnum = (Red, Green, Blue);',
  5377. ' TEnumRg = Green..Blue;',
  5378. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5379. 'var',
  5380. ' v: longint;',
  5381. ' s: string;',
  5382. 'begin',
  5383. ' val(s,e,v);',
  5384. ' val(s,e,i);',
  5385. 'end;',
  5386. 'var',
  5387. ' e: TEnumRg;',
  5388. ' i: longint;',
  5389. ' s: string;',
  5390. 'begin',
  5391. ' i:=ord(green);',
  5392. ' i:=ord(e);',
  5393. ' e:=low(tenumrg);',
  5394. ' e:=low(e);',
  5395. ' e:=high(tenumrg);',
  5396. ' e:=high(e);',
  5397. ' e:=pred(blue);',
  5398. ' e:=pred(e);',
  5399. ' e:=succ(green);',
  5400. ' e:=succ(e);',
  5401. ' e:=tenumrg(1);',
  5402. ' e:=tenumrg(i);',
  5403. ' s:=str(e);',
  5404. ' str(e,s);',
  5405. ' str(red,s);',
  5406. ' s:=str(e:3);',
  5407. ' writestr(s,e:3,blue);',
  5408. ' val(s,e,i);',
  5409. ' i:=longint(e);']);
  5410. ConvertProgram;
  5411. CheckSource('TestEnumRg_Functions',
  5412. LinesToStr([ // statements
  5413. 'this.TEnum = {',
  5414. ' "0":"Red",',
  5415. ' Red:0,',
  5416. ' "1":"Green",',
  5417. ' Green:1,',
  5418. ' "2":"Blue",',
  5419. ' Blue:2',
  5420. ' };',
  5421. 'this.DoIt = function (e, i) {',
  5422. ' var v = 0;',
  5423. ' var s = "";',
  5424. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5425. ' v = w;',
  5426. ' }));',
  5427. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5428. '};',
  5429. 'this.e = this.TEnum.Green;',
  5430. 'this.i = 0;',
  5431. 'this.s = "";',
  5432. '']),
  5433. LinesToStr([
  5434. '$mod.i=$mod.TEnum.Green;',
  5435. '$mod.i=$mod.e;',
  5436. '$mod.e=$mod.TEnum.Green;',
  5437. '$mod.e=$mod.TEnum.Green;',
  5438. '$mod.e=$mod.TEnum.Blue;',
  5439. '$mod.e=$mod.TEnum.Blue;',
  5440. '$mod.e=$mod.TEnum.Blue-1;',
  5441. '$mod.e=$mod.e-1;',
  5442. '$mod.e=$mod.TEnum.Green+1;',
  5443. '$mod.e=$mod.e+1;',
  5444. '$mod.e=1;',
  5445. '$mod.e=$mod.i;',
  5446. '$mod.s = $mod.TEnum[$mod.e];',
  5447. '$mod.s = $mod.TEnum[$mod.e];',
  5448. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5449. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5450. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5451. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5452. ' $mod.i = v;',
  5453. '});',
  5454. '$mod.i=$mod.e;',
  5455. '']));
  5456. end;
  5457. procedure TTestModule.TestEnum_AsParams;
  5458. begin
  5459. StartProgram(false);
  5460. Add('type TEnum = (Red,Blue);');
  5461. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5462. Add('var vJ: TEnum;');
  5463. Add('begin');
  5464. Add(' vg:=vg;');
  5465. Add(' vj:=vh;');
  5466. Add(' vi:=vi;');
  5467. Add(' doit(vg,vg,vg);');
  5468. Add(' doit(vh,vh,vj);');
  5469. Add(' doit(vi,vi,vi);');
  5470. Add(' doit(vj,vj,vj);');
  5471. Add('end;');
  5472. Add('var i: TEnum;');
  5473. Add('begin');
  5474. Add(' doit(i,i,i);');
  5475. ConvertProgram;
  5476. CheckSource('TestEnum_AsParams',
  5477. LinesToStr([ // statements
  5478. 'this.TEnum = {',
  5479. ' "0": "Red",',
  5480. ' Red: 0,',
  5481. ' "1": "Blue",',
  5482. ' Blue: 1',
  5483. '};',
  5484. 'this.DoIt = function (vG,vH,vI) {',
  5485. ' var vJ = 0;',
  5486. ' vG = vG;',
  5487. ' vJ = vH;',
  5488. ' vI.set(vI.get());',
  5489. ' $mod.DoIt(vG, vG, {',
  5490. ' get: function () {',
  5491. ' return vG;',
  5492. ' },',
  5493. ' set: function (v) {',
  5494. ' vG = v;',
  5495. ' }',
  5496. ' });',
  5497. ' $mod.DoIt(vH, vH, {',
  5498. ' get: function () {',
  5499. ' return vJ;',
  5500. ' },',
  5501. ' set: function (v) {',
  5502. ' vJ = v;',
  5503. ' }',
  5504. ' });',
  5505. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5506. ' $mod.DoIt(vJ, vJ, {',
  5507. ' get: function () {',
  5508. ' return vJ;',
  5509. ' },',
  5510. ' set: function (v) {',
  5511. ' vJ = v;',
  5512. ' }',
  5513. ' });',
  5514. '};',
  5515. 'this.i = 0;'
  5516. ]),
  5517. LinesToStr([
  5518. '$mod.DoIt($mod.i,$mod.i,{',
  5519. ' p: $mod,',
  5520. ' get: function () {',
  5521. ' return this.p.i;',
  5522. ' },',
  5523. ' set: function (v) {',
  5524. ' this.p.i = v;',
  5525. ' }',
  5526. '});'
  5527. ]));
  5528. end;
  5529. procedure TTestModule.TestEnumRange_Array;
  5530. begin
  5531. StartProgram(false);
  5532. Add([
  5533. 'type',
  5534. ' TEnum = (Red, Green, Blue);',
  5535. ' TEnumRg = green..blue;',
  5536. ' TArr = array[TEnumRg] of byte;',
  5537. ' TArr2 = array[green..blue] of byte;',
  5538. 'var',
  5539. ' a: TArr;',
  5540. ' b: TArr = (3,4);',
  5541. ' c: TArr2 = (5,6);',
  5542. 'begin',
  5543. ' a[green] := b[blue];',
  5544. ' c[green] := c[blue];',
  5545. '']);
  5546. ConvertProgram;
  5547. CheckSource('TestEnumRange_Array',
  5548. LinesToStr([ // statements
  5549. 'this.TEnum = {',
  5550. ' "0": "Red",',
  5551. ' Red: 0,',
  5552. ' "1": "Green",',
  5553. ' Green: 1,',
  5554. ' "2": "Blue",',
  5555. ' Blue: 2',
  5556. '};',
  5557. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5558. 'this.b = [3, 4];',
  5559. 'this.c = [5, 6];',
  5560. '']),
  5561. LinesToStr([
  5562. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5563. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5564. '']));
  5565. end;
  5566. procedure TTestModule.TestEnum_ForIn;
  5567. begin
  5568. StartProgram(false);
  5569. Add([
  5570. 'type',
  5571. ' TEnum = (Red, Green, Blue);',
  5572. ' TEnumRg = green..blue;',
  5573. ' TArr = array[TEnum] of byte;',
  5574. ' TArrRg = array[TEnumRg] of byte;',
  5575. 'var',
  5576. ' e: TEnum;',
  5577. ' a1: TArr = (3,4,5);',
  5578. ' a2: TArrRg = (11,12);',
  5579. ' b: byte;',
  5580. 'begin',
  5581. ' for e in TEnum do ;',
  5582. ' for e in TEnumRg do ;',
  5583. ' for e in TArr do ;',
  5584. ' for e in TArrRg do ;',
  5585. ' for b in a1 do ;',
  5586. ' for b in a2 do ;',
  5587. '']);
  5588. ConvertProgram;
  5589. CheckSource('TestEnum_ForIn',
  5590. LinesToStr([ // statements
  5591. 'this.TEnum = {',
  5592. ' "0": "Red",',
  5593. ' Red: 0,',
  5594. ' "1": "Green",',
  5595. ' Green: 1,',
  5596. ' "2": "Blue",',
  5597. ' Blue: 2',
  5598. '};',
  5599. 'this.e = 0;',
  5600. 'this.a1 = [3, 4, 5];',
  5601. 'this.a2 = [11, 12];',
  5602. 'this.b = 0;',
  5603. '']),
  5604. LinesToStr([
  5605. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5606. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5607. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5608. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5609. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5610. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5611. '']));
  5612. end;
  5613. procedure TTestModule.TestEnum_ScopedNumber;
  5614. begin
  5615. Converter.Options:=Converter.Options+[coEnumNumbers];
  5616. StartProgram(false);
  5617. Add([
  5618. 'type',
  5619. ' TEnum = (Red, Green);',
  5620. 'var',
  5621. ' e: TEnum;',
  5622. 'begin',
  5623. ' e:=TEnum.Green;',
  5624. '']);
  5625. ConvertProgram;
  5626. CheckSource('TestEnum_ScopedNumber',
  5627. LinesToStr([ // statements
  5628. 'this.TEnum = {',
  5629. ' "0": "Red",',
  5630. ' Red: 0,',
  5631. ' "1": "Green",',
  5632. ' Green: 1',
  5633. '};',
  5634. 'this.e = 0;',
  5635. '']),
  5636. LinesToStr([
  5637. '$mod.e = 1;']));
  5638. end;
  5639. procedure TTestModule.TestEnum_InFunction;
  5640. begin
  5641. StartProgram(false);
  5642. Add([
  5643. 'const TEnum = 3;',
  5644. 'procedure DoIt;',
  5645. 'type',
  5646. ' TEnum = (Red, Green, Blue);',
  5647. ' procedure Sub;',
  5648. ' type',
  5649. ' TEnumSub = (Left, Right);',
  5650. ' var',
  5651. ' es: TEnumSub;',
  5652. ' begin',
  5653. ' es:=Left;',
  5654. ' end;',
  5655. 'var',
  5656. ' e, e2: TEnum;',
  5657. 'begin',
  5658. ' if e in [red,blue] then e2:=e;',
  5659. 'end;',
  5660. 'begin']);
  5661. ConvertProgram;
  5662. CheckSource('TestEnum_InFunction',
  5663. LinesToStr([ // statements
  5664. 'this.TEnum = 3;',
  5665. 'var TEnum$1 = {',
  5666. ' "0":"Red",',
  5667. ' Red:0,',
  5668. ' "1":"Green",',
  5669. ' Green:1,',
  5670. ' "2":"Blue",',
  5671. ' Blue:2',
  5672. ' };',
  5673. 'var TEnumSub = {',
  5674. ' "0": "Left",',
  5675. ' Left: 0,',
  5676. ' "1": "Right",',
  5677. ' Right: 1',
  5678. '};',
  5679. 'this.DoIt = function () {',
  5680. ' function Sub() {',
  5681. ' var es = 0;',
  5682. ' es = TEnumSub.Left;',
  5683. ' };',
  5684. ' var e = 0;',
  5685. ' var e2 = 0;',
  5686. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5687. '};',
  5688. '']),
  5689. LinesToStr([
  5690. '']));
  5691. end;
  5692. procedure TTestModule.TestSet_Enum;
  5693. begin
  5694. StartProgram(false);
  5695. Add([
  5696. 'type',
  5697. ' TColor = (Red, Green, Blue);',
  5698. ' TColors = set of TColor;',
  5699. 'var',
  5700. ' c: TColor;',
  5701. ' s: TColors;',
  5702. ' t: TColors = [];',
  5703. ' u: TColors = [Red];',
  5704. 'begin',
  5705. ' s:=[];',
  5706. ' s:=[Green];',
  5707. ' s:=[Green,Blue];',
  5708. ' s:=[Red..Blue];',
  5709. ' s:=[Red,Green..Blue];',
  5710. ' s:=[Red,c];',
  5711. ' s:=t;',
  5712. ' s:=default(TColors);',
  5713. '']);
  5714. ConvertProgram;
  5715. CheckSource('TestSet',
  5716. LinesToStr([ // statements
  5717. 'this.TColor = {',
  5718. ' "0":"Red",',
  5719. ' Red:0,',
  5720. ' "1":"Green",',
  5721. ' Green:1,',
  5722. ' "2":"Blue",',
  5723. ' Blue:2',
  5724. ' };',
  5725. 'this.c = 0;',
  5726. 'this.s = {};',
  5727. 'this.t = {};',
  5728. 'this.u = rtl.createSet(this.TColor.Red);'
  5729. ]),
  5730. LinesToStr([
  5731. '$mod.s={};',
  5732. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5733. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5734. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5735. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5736. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5737. '$mod.s=rtl.refSet($mod.t);',
  5738. '$mod.s={};',
  5739. '']));
  5740. end;
  5741. procedure TTestModule.TestSet_Operators;
  5742. begin
  5743. StartProgram(false);
  5744. Add('type');
  5745. Add(' TColor = (Red, Green, Blue);');
  5746. Add(' TColors = set of tcolor;');
  5747. Add('var');
  5748. Add(' vC: TColor;');
  5749. Add(' vS: TColors;');
  5750. Add(' vT: TColors;');
  5751. Add(' vU: TColors;');
  5752. Add(' B: boolean;');
  5753. Add('begin');
  5754. Add(' include(vs,green);');
  5755. Add(' exclude(vs,vc);');
  5756. Add(' vs:=vt+vu;');
  5757. Add(' vs:=vt+[red];');
  5758. Add(' vs:=[red]+vt;');
  5759. Add(' vs:=[red]+[green];');
  5760. Add(' vs:=vt-vu;');
  5761. Add(' vs:=vt-[red];');
  5762. Add(' vs:=[red]-vt;');
  5763. Add(' vs:=[red]-[green];');
  5764. Add(' vs:=vt*vu;');
  5765. Add(' vs:=vt*[red];');
  5766. Add(' vs:=[red]*vt;');
  5767. Add(' vs:=[red]*[green];');
  5768. Add(' vs:=vt><vu;');
  5769. Add(' vs:=vt><[red];');
  5770. Add(' vs:=[red]><vt;');
  5771. Add(' vs:=[red]><[green];');
  5772. Add(' b:=vt=vu;');
  5773. Add(' b:=vt=[red];');
  5774. Add(' b:=[red]=vt;');
  5775. Add(' b:=[red]=[green];');
  5776. Add(' b:=vt<>vu;');
  5777. Add(' b:=vt<>[red];');
  5778. Add(' b:=[red]<>vt;');
  5779. Add(' b:=[red]<>[green];');
  5780. Add(' b:=vt<=vu;');
  5781. Add(' b:=vt<=[red];');
  5782. Add(' b:=[red]<=vt;');
  5783. Add(' b:=[red]<=[green];');
  5784. Add(' b:=vt>=vu;');
  5785. Add(' b:=vt>=[red];');
  5786. Add(' b:=[red]>=vt;');
  5787. Add(' b:=[red]>=[green];');
  5788. ConvertProgram;
  5789. CheckSource('TestSet_Operators',
  5790. LinesToStr([ // statements
  5791. 'this.TColor = {',
  5792. ' "0":"Red",',
  5793. ' Red:0,',
  5794. ' "1":"Green",',
  5795. ' Green:1,',
  5796. ' "2":"Blue",',
  5797. ' Blue:2',
  5798. ' };',
  5799. 'this.vC = 0;',
  5800. 'this.vS = {};',
  5801. 'this.vT = {};',
  5802. 'this.vU = {};',
  5803. 'this.B = false;'
  5804. ]),
  5805. LinesToStr([
  5806. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5807. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5808. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5809. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5810. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5811. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5812. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5813. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5814. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5815. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5816. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5817. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5818. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5819. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5820. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5821. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5822. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5823. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5824. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5825. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5826. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5827. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5828. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5829. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5830. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5831. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5832. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5833. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5834. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5835. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5836. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5837. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5838. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5839. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5840. '']));
  5841. end;
  5842. procedure TTestModule.TestSet_Operator_In;
  5843. begin
  5844. StartProgram(false);
  5845. Add([
  5846. 'type',
  5847. ' TColor = (Red, Green, Blue);',
  5848. ' TColors = set of tcolor;',
  5849. ' TColorRg = green..blue;',
  5850. 'var',
  5851. ' vC: tcolor;',
  5852. ' vT: tcolors;',
  5853. ' B: boolean;',
  5854. ' rg: TColorRg;',
  5855. 'begin',
  5856. ' b:=red in vt;',
  5857. ' b:=vc in vt;',
  5858. ' b:=green in [red..blue];',
  5859. ' b:=vc in [red..blue];',
  5860. ' ',
  5861. ' if red in vt then ;',
  5862. ' while vC in vt do ;',
  5863. ' repeat',
  5864. ' until vC in vt;',
  5865. ' if rg in [green..blue] then ;',
  5866. '']);
  5867. ConvertProgram;
  5868. CheckSource('TestSet_Operator_In',
  5869. LinesToStr([ // statements
  5870. 'this.TColor = {',
  5871. ' "0":"Red",',
  5872. ' Red:0,',
  5873. ' "1":"Green",',
  5874. ' Green:1,',
  5875. ' "2":"Blue",',
  5876. ' Blue:2',
  5877. ' };',
  5878. 'this.vC = 0;',
  5879. 'this.vT = {};',
  5880. 'this.B = false;',
  5881. 'this.rg = this.TColor.Green;',
  5882. '']),
  5883. LinesToStr([
  5884. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5885. '$mod.B = $mod.vC in $mod.vT;',
  5886. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5887. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5888. 'if ($mod.TColor.Red in $mod.vT) ;',
  5889. 'while ($mod.vC in $mod.vT) {',
  5890. '};',
  5891. 'do {',
  5892. '} while (!($mod.vC in $mod.vT));',
  5893. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5894. '']));
  5895. end;
  5896. procedure TTestModule.TestSet_Functions;
  5897. begin
  5898. StartProgram(false);
  5899. Add('type');
  5900. Add(' TMyEnum = (Red, Green);');
  5901. Add(' TMyEnums = set of TMyEnum;');
  5902. Add('var');
  5903. Add(' e: TMyEnum;');
  5904. Add(' s: TMyEnums;');
  5905. Add('begin');
  5906. Add(' e:=Low(TMyEnums);');
  5907. Add(' e:=Low(s);');
  5908. Add(' e:=High(TMyEnums);');
  5909. Add(' e:=High(s);');
  5910. ConvertProgram;
  5911. CheckSource('TestSetFunctions',
  5912. LinesToStr([ // statements
  5913. 'this.TMyEnum = {',
  5914. ' "0":"Red",',
  5915. ' Red:0,',
  5916. ' "1":"Green",',
  5917. ' Green:1',
  5918. ' };',
  5919. 'this.e = 0;',
  5920. 'this.s = {};'
  5921. ]),
  5922. LinesToStr([
  5923. '$mod.e=$mod.TMyEnum.Red;',
  5924. '$mod.e=$mod.TMyEnum.Red;',
  5925. '$mod.e=$mod.TMyEnum.Green;',
  5926. '$mod.e=$mod.TMyEnum.Green;',
  5927. '']));
  5928. end;
  5929. procedure TTestModule.TestSet_PassAsArgClone;
  5930. begin
  5931. StartProgram(false);
  5932. Add('type');
  5933. Add(' TMyEnum = (Red, Green);');
  5934. Add(' TMyEnums = set of TMyEnum;');
  5935. Add('procedure DoDefault(s: tmyenums); begin end;');
  5936. Add('procedure DoConst(const s: tmyenums); begin end;');
  5937. Add('var');
  5938. Add(' aSet: tmyenums;');
  5939. Add('begin');
  5940. Add(' dodefault(aset);');
  5941. Add(' doconst(aset);');
  5942. ConvertProgram;
  5943. CheckSource('TestSetFunctions',
  5944. LinesToStr([ // statements
  5945. 'this.TMyEnum = {',
  5946. ' "0":"Red",',
  5947. ' Red:0,',
  5948. ' "1":"Green",',
  5949. ' Green:1',
  5950. ' };',
  5951. 'this.DoDefault = function (s) {',
  5952. '};',
  5953. 'this.DoConst = function (s) {',
  5954. '};',
  5955. 'this.aSet = {};'
  5956. ]),
  5957. LinesToStr([
  5958. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5959. '$mod.DoConst($mod.aSet);',
  5960. '']));
  5961. end;
  5962. procedure TTestModule.TestSet_AsParams;
  5963. begin
  5964. StartProgram(false);
  5965. Add([
  5966. 'type TEnum = (Red,Blue);',
  5967. 'type TEnums = set of TEnum;',
  5968. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5969. 'var vJ: TEnums;',
  5970. 'begin',
  5971. ' Include(vg,red);',
  5972. ' Include(result,blue);',
  5973. ' vg:=vg;',
  5974. ' vj:=vh;',
  5975. ' vi:=vi;',
  5976. ' doit(vg,vg,vg);',
  5977. ' doit(vh,vh,vj);',
  5978. ' doit(vi,vi,vi);',
  5979. ' doit(vj,vj,vj);',
  5980. 'end;',
  5981. 'var i: TEnums;',
  5982. 'begin',
  5983. ' doit(i,i,i);']);
  5984. ConvertProgram;
  5985. CheckSource('TestSet_AsParams',
  5986. LinesToStr([ // statements
  5987. 'this.TEnum = {',
  5988. ' "0": "Red",',
  5989. ' Red: 0,',
  5990. ' "1": "Blue",',
  5991. ' Blue: 1',
  5992. '};',
  5993. 'this.DoIt = function (vG,vH,vI) {',
  5994. ' var Result = {};',
  5995. ' var vJ = {};',
  5996. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5997. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5998. ' vG = rtl.refSet(vG);',
  5999. ' vJ = rtl.refSet(vH);',
  6000. ' vI.set(rtl.refSet(vI.get()));',
  6001. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6002. ' get: function () {',
  6003. ' return vG;',
  6004. ' },',
  6005. ' set: function (v) {',
  6006. ' vG = v;',
  6007. ' }',
  6008. ' });',
  6009. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6010. ' get: function () {',
  6011. ' return vJ;',
  6012. ' },',
  6013. ' set: function (v) {',
  6014. ' vJ = v;',
  6015. ' }',
  6016. ' });',
  6017. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6018. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6019. ' get: function () {',
  6020. ' return vJ;',
  6021. ' },',
  6022. ' set: function (v) {',
  6023. ' vJ = v;',
  6024. ' }',
  6025. ' });',
  6026. ' return Result;',
  6027. '};',
  6028. 'this.i = {};'
  6029. ]),
  6030. LinesToStr([
  6031. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6032. ' p: $mod,',
  6033. ' get: function () {',
  6034. ' return this.p.i;',
  6035. ' },',
  6036. ' set: function (v) {',
  6037. ' this.p.i = v;',
  6038. ' }',
  6039. '});'
  6040. ]));
  6041. end;
  6042. procedure TTestModule.TestSet_Property;
  6043. begin
  6044. StartProgram(false);
  6045. Add('type');
  6046. Add(' TEnum = (Red,Blue);');
  6047. Add(' TEnums = set of TEnum;');
  6048. Add(' TObject = class');
  6049. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6050. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6051. Add(' property Colors: TEnums read GetColors write SetColors;');
  6052. Add(' end;');
  6053. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6054. Add('begin end;');
  6055. Add('var Obj: TObject;');
  6056. Add('begin');
  6057. Add(' Include(Obj.Colors,Red);');
  6058. Add(' Exclude(Obj.Colors,Red);');
  6059. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6060. ConvertProgram;
  6061. CheckSource('TestSet_Property',
  6062. LinesToStr([ // statements
  6063. 'this.TEnum = {',
  6064. ' "0": "Red",',
  6065. ' Red: 0,',
  6066. ' "1": "Blue",',
  6067. ' Blue: 1',
  6068. '};',
  6069. 'rtl.createClass(this, "TObject", null, function () {',
  6070. ' this.$init = function () {',
  6071. ' };',
  6072. ' this.$final = function () {',
  6073. ' };',
  6074. '});',
  6075. 'this.DoIt = function (i, j, k, l) {',
  6076. '};',
  6077. 'this.Obj = null;',
  6078. '']),
  6079. LinesToStr([
  6080. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6081. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6082. '']));
  6083. end;
  6084. procedure TTestModule.TestSet_EnumConst;
  6085. begin
  6086. StartProgram(false);
  6087. Add([
  6088. 'type',
  6089. ' TEnum = (Red,Blue);',
  6090. ' TEnums = set of TEnum;',
  6091. 'const',
  6092. ' Orange = red;',
  6093. 'var',
  6094. ' Enum: tenum;',
  6095. ' Enums: tenums;',
  6096. 'begin',
  6097. ' Include(enums,orange);',
  6098. ' Exclude(enums,orange);',
  6099. ' if orange in enums then;',
  6100. ' if orange in [orange,red] then;']);
  6101. ConvertProgram;
  6102. CheckSource('TestSet_EnumConst',
  6103. LinesToStr([ // statements
  6104. 'this.TEnum = {',
  6105. ' "0": "Red",',
  6106. ' Red: 0,',
  6107. ' "1": "Blue",',
  6108. ' Blue: 1',
  6109. '};',
  6110. 'this.Orange = this.TEnum.Red;',
  6111. 'this.Enum = 0;',
  6112. 'this.Enums = {};',
  6113. '']),
  6114. LinesToStr([
  6115. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6116. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6117. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6118. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6119. '']));
  6120. end;
  6121. procedure TTestModule.TestSet_IntConst;
  6122. begin
  6123. StartProgram(false);
  6124. Add([
  6125. 'type',
  6126. ' TEnums = set of Byte;',
  6127. 'const',
  6128. ' Orange = 0;',
  6129. 'var',
  6130. ' Enum: byte;',
  6131. ' Enums: tenums;',
  6132. 'begin',
  6133. ' Enums:=[];',
  6134. ' Enums:=[0];',
  6135. ' Enums:=[1..2];',
  6136. //' Include(enums,orange);',
  6137. //' Exclude(enums,orange);',
  6138. ' if orange in enums then;',
  6139. ' if orange in [orange,1] then;']);
  6140. ConvertProgram;
  6141. CheckSource('TestSet_IntConst',
  6142. LinesToStr([ // statements
  6143. 'this.Orange = 0;',
  6144. 'this.Enum = 0;',
  6145. 'this.Enums = {};',
  6146. '']),
  6147. LinesToStr([
  6148. '$mod.Enums = {};',
  6149. '$mod.Enums = rtl.createSet(0);',
  6150. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6151. 'if (0 in $mod.Enums) ;',
  6152. 'if (0 in rtl.createSet(0, 1)) ;',
  6153. '']));
  6154. end;
  6155. procedure TTestModule.TestSet_IntRange;
  6156. begin
  6157. StartProgram(false);
  6158. Add([
  6159. 'type',
  6160. ' TRange = 1..3;',
  6161. ' TEnums = set of TRange;',
  6162. 'const',
  6163. ' Orange = 2;',
  6164. 'var',
  6165. ' Enum: byte;',
  6166. ' Enums: TEnums;',
  6167. 'begin',
  6168. ' Enums:=[];',
  6169. ' Enums:=[1];',
  6170. ' Enums:=[2..3];',
  6171. ' Include(enums,orange);',
  6172. ' Exclude(enums,orange);',
  6173. ' if orange in enums then;',
  6174. ' if orange in [orange,1] then;']);
  6175. ConvertProgram;
  6176. CheckSource('TestSet_IntRange',
  6177. LinesToStr([ // statements
  6178. 'this.Orange = 2;',
  6179. 'this.Enum = 0;',
  6180. 'this.Enums = {};',
  6181. '']),
  6182. LinesToStr([
  6183. '$mod.Enums = {};',
  6184. '$mod.Enums = rtl.createSet(1);',
  6185. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6186. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6187. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6188. 'if (2 in $mod.Enums) ;',
  6189. 'if (2 in rtl.createSet(2, 1)) ;',
  6190. '']));
  6191. end;
  6192. procedure TTestModule.TestSet_AnonymousEnumType;
  6193. begin
  6194. StartProgram(false);
  6195. Add('type');
  6196. Add(' TFlags = set of (red, green);');
  6197. Add('const');
  6198. Add(' favorite = red;');
  6199. Add('var');
  6200. Add(' f: TFlags;');
  6201. Add(' i: longint;');
  6202. Add('begin');
  6203. Add(' Include(f,red);');
  6204. Add(' Include(f,favorite);');
  6205. Add(' i:=ord(red);');
  6206. Add(' i:=ord(favorite);');
  6207. Add(' i:=ord(low(TFlags));');
  6208. Add(' i:=ord(low(f));');
  6209. Add(' i:=ord(low(favorite));');
  6210. Add(' i:=ord(high(TFlags));');
  6211. Add(' i:=ord(high(f));');
  6212. Add(' i:=ord(high(favorite));');
  6213. Add(' f:=[green,favorite];');
  6214. ConvertProgram;
  6215. CheckSource('TestSet_AnonymousEnumType',
  6216. LinesToStr([ // statements
  6217. 'this.TFlags$a = {',
  6218. ' "0": "red",',
  6219. ' red: 0,',
  6220. ' "1": "green",',
  6221. ' green: 1',
  6222. '};',
  6223. 'this.favorite = this.TFlags$a.red;',
  6224. 'this.f = {};',
  6225. 'this.i = 0;',
  6226. '']),
  6227. LinesToStr([
  6228. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6229. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6230. '$mod.i = $mod.TFlags$a.red;',
  6231. '$mod.i = $mod.TFlags$a.red;',
  6232. '$mod.i = $mod.TFlags$a.red;',
  6233. '$mod.i = $mod.TFlags$a.red;',
  6234. '$mod.i = $mod.TFlags$a.red;',
  6235. '$mod.i = $mod.TFlags$a.green;',
  6236. '$mod.i = $mod.TFlags$a.green;',
  6237. '$mod.i = $mod.TFlags$a.green;',
  6238. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6239. '']));
  6240. end;
  6241. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6242. begin
  6243. exit;
  6244. StartProgram(false);
  6245. Add([
  6246. 'type',
  6247. ' TAtoZ = ''A''..''Z'';',
  6248. ' TSetOfAZ = set of TAtoZ;',
  6249. 'var',
  6250. ' c: char;',
  6251. ' a: TAtoZ;',
  6252. ' s: TSetOfAZ = [''P'',''A''];',
  6253. ' i: longint;',
  6254. 'begin',
  6255. ' Include(s,''S'');',
  6256. ' Include(s,c);',
  6257. ' Include(s,a);',
  6258. ' c:=low(TAtoZ);',
  6259. ' i:=ord(low(TAtoZ));',
  6260. ' a:=high(TAtoZ);',
  6261. ' a:=high(TSetOfAtoZ);',
  6262. ' s:=[a,c,''M''];',
  6263. '']);
  6264. ConvertProgram;
  6265. CheckSource('TestSet_AnonymousEnumTypeChar',
  6266. LinesToStr([ // statements
  6267. '']),
  6268. LinesToStr([
  6269. '']));
  6270. end;
  6271. procedure TTestModule.TestSet_ConstEnum;
  6272. begin
  6273. StartProgram(false);
  6274. Add([
  6275. 'type',
  6276. ' TEnum = (red,blue,green);',
  6277. ' TEnums = set of TEnum;',
  6278. 'const',
  6279. ' teAny = [low(TEnum)..high(TEnum)];',
  6280. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6281. 'var',
  6282. ' e: TEnum;',
  6283. ' s: TEnums;',
  6284. 'begin',
  6285. ' if blue in teAny then;',
  6286. ' if blue in teAny+[e] then;',
  6287. ' if blue in teAny+teRedBlue then;',
  6288. ' if e in [red,blue] then;',
  6289. ' s:=teAny;',
  6290. ' s:=teAny+[e];',
  6291. ' s:=[e]+teAny;',
  6292. ' s:=teAny+teRedBlue;',
  6293. ' s:=teAny+teRedBlue+[e];',
  6294. '']);
  6295. ConvertProgram;
  6296. CheckSource('TestSet_ConstEnum',
  6297. LinesToStr([ // statements
  6298. 'this.TEnum = {',
  6299. ' "0": "red",',
  6300. ' red: 0,',
  6301. ' "1": "blue",',
  6302. ' blue: 1,',
  6303. ' "2": "green",',
  6304. ' green: 2',
  6305. '};',
  6306. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6307. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6308. 'this.e = 0;',
  6309. 'this.s = {};',
  6310. '']),
  6311. LinesToStr([
  6312. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6313. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6314. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6315. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6316. '$mod.s = rtl.refSet($mod.teAny);',
  6317. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6318. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6319. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6320. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6321. '']));
  6322. end;
  6323. procedure TTestModule.TestSet_ConstChar;
  6324. begin
  6325. StartProgram(false);
  6326. Add([
  6327. 'const',
  6328. ' LowChars = [''a''..''z''];',
  6329. ' Chars = LowChars+[''A''..''Z''];',
  6330. ' sc = [''А'', ''Я''];',
  6331. 'var',
  6332. ' c: char;',
  6333. ' s: string;',
  6334. 'begin',
  6335. ' if c in lowchars then ;',
  6336. ' if ''a'' in lowchars then ;',
  6337. ' if s[1] in lowchars then ;',
  6338. ' if c in chars then ;',
  6339. ' if c in [''a''..''z'',''_''] then ;',
  6340. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6341. ' if ''Я'' in sc then ;',
  6342. ' if 3=ord('' '') then ;',
  6343. '']);
  6344. ConvertProgram;
  6345. CheckSource('TestSet_ConstChar',
  6346. LinesToStr([ // statements
  6347. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6348. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6349. 'this.sc = rtl.createSet(1040, 1071);',
  6350. 'this.c = "";',
  6351. 'this.s = "";',
  6352. '']),
  6353. LinesToStr([
  6354. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6355. 'if (97 in $mod.LowChars) ;',
  6356. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6357. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6358. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6359. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6360. 'if (1071 in $mod.sc) ;',
  6361. 'if (3 === 32) ;',
  6362. '']));
  6363. end;
  6364. procedure TTestModule.TestSet_ConstInt;
  6365. begin
  6366. StartProgram(false);
  6367. Add([
  6368. 'const',
  6369. ' Months = [1..12];',
  6370. ' Mirror = [-12..-1]+Months;',
  6371. 'var',
  6372. ' i: smallint;',
  6373. 'begin',
  6374. ' if 3 in Months then;',
  6375. ' if i in Months+[i] then;',
  6376. ' if i in Months+Mirror then;',
  6377. ' if i in [4..6,8] then;',
  6378. '']);
  6379. ConvertProgram;
  6380. CheckSource('TestSet_ConstInt',
  6381. LinesToStr([ // statements
  6382. 'this.Months = rtl.createSet(null, 1, 12);',
  6383. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6384. 'this.i = 0;',
  6385. '']),
  6386. LinesToStr([
  6387. 'if (3 in $mod.Months) ;',
  6388. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6389. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6390. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6391. '']));
  6392. end;
  6393. procedure TTestModule.TestSet_InFunction;
  6394. begin
  6395. StartProgram(false);
  6396. Add([
  6397. 'const',
  6398. ' TEnum = 3;',
  6399. ' TSetOfEnum = 4;',
  6400. ' TSetOfAno = 5;',
  6401. 'procedure DoIt;',
  6402. 'type',
  6403. ' TEnum = (red, blue);',
  6404. ' TSetOfEnum = set of TEnum;',
  6405. ' TSetOfAno = set of (up,down);',
  6406. 'var',
  6407. ' e: TEnum;',
  6408. ' se: TSetOfEnum;',
  6409. ' sa: TSetOfAno;',
  6410. 'begin',
  6411. ' se:=[e];',
  6412. ' sa:=[up];',
  6413. 'end;',
  6414. 'begin',
  6415. '']);
  6416. ConvertProgram;
  6417. CheckSource('TestSet_InFunction',
  6418. LinesToStr([ // statements
  6419. 'this.TEnum = 3;',
  6420. 'this.TSetOfEnum = 4;',
  6421. 'this.TSetOfAno = 5;',
  6422. 'var TEnum$1 = {',
  6423. ' "0": "red",',
  6424. ' red: 0,',
  6425. ' "1": "blue",',
  6426. ' blue: 1',
  6427. '};',
  6428. 'var TSetOfAno$a = {',
  6429. ' "0": "up",',
  6430. ' up: 0,',
  6431. ' "1": "down",',
  6432. ' down: 1',
  6433. '};',
  6434. 'this.DoIt = function () {',
  6435. ' var e = 0;',
  6436. ' var se = {};',
  6437. ' var sa = {};',
  6438. ' se = rtl.createSet(e);',
  6439. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6440. '};',
  6441. '']),
  6442. LinesToStr([
  6443. '']));
  6444. end;
  6445. procedure TTestModule.TestSet_ForIn;
  6446. begin
  6447. StartProgram(false);
  6448. Add([
  6449. 'type',
  6450. ' TEnum = (Red, Green, Blue);',
  6451. ' TEnumRg = green..blue;',
  6452. ' TSetOfEnum = set of TEnum;',
  6453. ' TSetOfEnumRg = set of TEnumRg;',
  6454. 'var',
  6455. ' e, e2: TEnum;',
  6456. ' er: TEnum;',
  6457. ' s: TSetOfEnum;',
  6458. 'begin',
  6459. ' for e in TSetOfEnum do ;',
  6460. ' for e in TSetOfEnumRg do ;',
  6461. ' for e in [] do e2:=e;',
  6462. ' for e in [red..green] do e2:=e;',
  6463. ' for e in [green,blue] do e2:=e;',
  6464. ' for e in [red,blue] do e2:=e;',
  6465. ' for e in s do e2:=e;',
  6466. ' for er in TSetOfEnumRg do ;',
  6467. '']);
  6468. ConvertProgram;
  6469. CheckSource('TestSet_ForIn',
  6470. LinesToStr([ // statements
  6471. 'this.TEnum = {',
  6472. ' "0":"Red",',
  6473. ' Red:0,',
  6474. ' "1":"Green",',
  6475. ' Green:1,',
  6476. ' "2":"Blue",',
  6477. ' Blue:2',
  6478. ' };',
  6479. 'this.e = 0;',
  6480. 'this.e2 = 0;',
  6481. 'this.er = 0;',
  6482. 'this.s = {};',
  6483. '']),
  6484. LinesToStr([
  6485. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6486. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6487. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6488. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6489. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6490. 'for (var $l in $mod.s){',
  6491. ' $mod.e = +$l;',
  6492. ' $mod.e2 = $mod.e;',
  6493. '};',
  6494. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6495. '']));
  6496. end;
  6497. procedure TTestModule.TestNestBegin;
  6498. begin
  6499. StartProgram(false);
  6500. Add('begin');
  6501. Add(' begin');
  6502. Add(' begin');
  6503. Add(' end;');
  6504. Add(' begin');
  6505. Add(' if true then ;');
  6506. Add(' end;');
  6507. Add(' end;');
  6508. ConvertProgram;
  6509. CheckSource('TestNestBegin',
  6510. '',
  6511. 'if (true) ;');
  6512. end;
  6513. procedure TTestModule.TestUnitImplVars;
  6514. begin
  6515. StartUnit(false);
  6516. Add('interface');
  6517. Add('implementation');
  6518. Add('var');
  6519. Add(' V1:longint;');
  6520. Add(' V2:longint = 3;');
  6521. Add(' V3:string = ''abc'';');
  6522. ConvertUnit;
  6523. CheckSource('TestUnitImplVars',
  6524. LinesToStr([ // statements
  6525. 'var $impl = $mod.$impl;',
  6526. '']),
  6527. '', // this.$init
  6528. LinesToStr([ // implementation
  6529. '$impl.V1 = 0;',
  6530. '$impl.V2 = 3;',
  6531. '$impl.V3 = "abc";',
  6532. '']) );
  6533. end;
  6534. procedure TTestModule.TestUnitImplConsts;
  6535. begin
  6536. StartUnit(false);
  6537. Add('interface');
  6538. Add('implementation');
  6539. Add('const');
  6540. Add(' v1 = 3;');
  6541. Add(' v2:longint = 4;');
  6542. Add(' v3:string = ''abc'';');
  6543. ConvertUnit;
  6544. CheckSource('TestUnitImplConsts',
  6545. LinesToStr([ // statements
  6546. 'var $impl = $mod.$impl;',
  6547. '']),
  6548. '', // this.$init
  6549. LinesToStr([ // implementation
  6550. '$impl.v1 = 3;',
  6551. '$impl.v2 = 4;',
  6552. '$impl.v3 = "abc";',
  6553. '']) );
  6554. end;
  6555. procedure TTestModule.TestUnitImplRecord;
  6556. begin
  6557. StartUnit(false);
  6558. Add('interface');
  6559. Add('implementation');
  6560. Add('type');
  6561. Add(' TMyRecord = record');
  6562. Add(' i: longint;');
  6563. Add(' end;');
  6564. Add('var aRec: TMyRecord;');
  6565. Add('initialization');
  6566. Add(' arec.i:=3;');
  6567. ConvertUnit;
  6568. CheckSource('TestUnitImplRecord',
  6569. LinesToStr([ // statements
  6570. 'var $impl = $mod.$impl;',
  6571. '']),
  6572. // this.$init
  6573. '$impl.aRec.i = 3;',
  6574. LinesToStr([ // implementation
  6575. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6576. ' this.i = 0;',
  6577. ' this.$eq = function (b) {',
  6578. ' return this.i === b.i;',
  6579. ' };',
  6580. ' this.$assign = function (s) {',
  6581. ' this.i = s.i;',
  6582. ' return this;',
  6583. ' };',
  6584. '});',
  6585. '$impl.aRec = $impl.TMyRecord.$new();',
  6586. '']) );
  6587. end;
  6588. procedure TTestModule.TestRenameJSNameConflict;
  6589. begin
  6590. StartProgram(false);
  6591. Add('var apply: longint;');
  6592. Add('var bind: longint;');
  6593. Add('var call: longint;');
  6594. Add('begin');
  6595. ConvertProgram;
  6596. CheckSource('TestRenameJSNameConflict',
  6597. LinesToStr([ // statements
  6598. 'this.Apply = 0;',
  6599. 'this.Bind = 0;',
  6600. 'this.Call = 0;'
  6601. ]),
  6602. LinesToStr([ // this.$main
  6603. ''
  6604. ]));
  6605. end;
  6606. procedure TTestModule.TestLocalConst;
  6607. begin
  6608. StartProgram(false);
  6609. Add('procedure DoIt;');
  6610. Add('const');
  6611. Add(' cA: longint = 1;');
  6612. Add(' cB = 2;');
  6613. Add(' procedure Sub;');
  6614. Add(' const');
  6615. Add(' csA = 3;');
  6616. Add(' cB: double = 4;');
  6617. Add(' begin');
  6618. Add(' cb:=cb+csa;');
  6619. Add(' ca:=ca+csa+5;');
  6620. Add(' end;');
  6621. Add('begin');
  6622. Add(' ca:=ca+cb+6;');
  6623. Add('end;');
  6624. Add('begin');
  6625. ConvertProgram;
  6626. CheckSource('TestLocalConst',
  6627. LinesToStr([
  6628. 'var cA = 1;',
  6629. 'var cB = 2;',
  6630. 'var csA = 3;',
  6631. 'var cB$1 = 4;',
  6632. 'this.DoIt = function () {',
  6633. ' function Sub() {',
  6634. ' cB$1 = cB$1 + 3;',
  6635. ' cA = cA + 3 + 5;',
  6636. ' };',
  6637. ' cA = cA + 2 + 6;',
  6638. '};'
  6639. ]),
  6640. LinesToStr([
  6641. ]));
  6642. end;
  6643. procedure TTestModule.TestVarExternal;
  6644. begin
  6645. StartProgram(false);
  6646. Add('var');
  6647. Add(' NaN: double; external name ''Global.NaN'';');
  6648. Add(' d: double;');
  6649. Add('begin');
  6650. Add(' d:=NaN;');
  6651. ConvertProgram;
  6652. CheckSource('TestVarExternal',
  6653. LinesToStr([
  6654. 'this.d = 0.0;'
  6655. ]),
  6656. LinesToStr([
  6657. '$mod.d = Global.NaN;'
  6658. ]));
  6659. end;
  6660. procedure TTestModule.TestVarExternalOtherUnit;
  6661. begin
  6662. AddModuleWithIntfImplSrc('unit2.pas',
  6663. LinesToStr([
  6664. 'var NaN: double; external name ''Global.NaN'';',
  6665. 'var iV: longint;'
  6666. ]),
  6667. '');
  6668. StartUnit(true);
  6669. Add('interface');
  6670. Add('uses unit2;');
  6671. Add('implementation');
  6672. Add('var');
  6673. Add(' d: double;');
  6674. Add(' i: longint; external name ''$i'';');
  6675. Add('begin');
  6676. Add(' d:=nan;');
  6677. Add(' d:=uNit2.nan;');
  6678. Add(' d:=test1.d;');
  6679. Add(' i:=iv;');
  6680. Add(' i:=uNit2.iv;');
  6681. Add(' i:=test1.i;');
  6682. ConvertUnit;
  6683. CheckSource('TestVarExternalOtherUnit',
  6684. LinesToStr([
  6685. 'var $impl = $mod.$impl;',
  6686. '']),
  6687. LinesToStr([ // this.$init
  6688. '$impl.d = Global.NaN;',
  6689. '$impl.d = Global.NaN;',
  6690. '$impl.d = $impl.d;',
  6691. '$i = pas.unit2.iV;',
  6692. '$i = pas.unit2.iV;',
  6693. '$i = $i;',
  6694. '']),
  6695. LinesToStr([ // implementation
  6696. '$impl.d = 0.0;',
  6697. '']) );
  6698. end;
  6699. procedure TTestModule.TestVarAbsoluteFail;
  6700. begin
  6701. StartProgram(false);
  6702. Add([
  6703. 'var',
  6704. ' a: longint;',
  6705. ' b: longword absolute a;',
  6706. 'begin']);
  6707. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6708. ConvertProgram;
  6709. end;
  6710. procedure TTestModule.TestConstExternal;
  6711. begin
  6712. StartProgram(false);
  6713. Add([
  6714. 'const',
  6715. ' PI: double; external name ''Global.PI'';',
  6716. ' Tau = 2*pi;',
  6717. 'var d: double;',
  6718. 'begin',
  6719. ' d:=pi;',
  6720. ' d:=tau+pi;']);
  6721. ConvertProgram;
  6722. CheckSource('TestConstExternal',
  6723. LinesToStr([
  6724. 'this.Tau = 2*Global.PI;',
  6725. 'this.d = 0.0;'
  6726. ]),
  6727. LinesToStr([
  6728. '$mod.d = Global.PI;',
  6729. '$mod.d = $mod.Tau + Global.PI;'
  6730. ]));
  6731. end;
  6732. procedure TTestModule.TestDouble;
  6733. begin
  6734. StartProgram(false);
  6735. Add([
  6736. 'type',
  6737. ' TDateTime = double;',
  6738. 'const',
  6739. ' a = TDateTime(2.7);',
  6740. ' b = a + TDateTime(1.7);',
  6741. ' c = 0.9 + 0.1;',
  6742. ' f0_1 = 0.1;',
  6743. ' f0_3 = 0.3;',
  6744. ' fn0_1 = -0.1;',
  6745. ' fn0_3 = -0.3;',
  6746. ' fn0_003 = -0.003;',
  6747. ' fn0_123456789 = -0.123456789;',
  6748. ' fn300_0 = -300.0;',
  6749. ' fn123456_0 = -123456.0;',
  6750. ' fn1234567_8 = -1234567.8;',
  6751. ' fn12345678_9 = -12345678.9;',
  6752. ' f1_0En12 = 1E-12;',
  6753. ' fn1_0En12 = -1E-12;',
  6754. ' maxdouble = 1.7e+308;',
  6755. ' mindouble = -1.7e+308;',
  6756. ' MinSafeIntDouble = -$1fffffffffffff;',
  6757. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6758. ' MaxSafeIntDouble = $1fffffffffffff;',
  6759. ' DZeroResolution = 1E-12;',
  6760. ' Minus1 = -1E-12;',
  6761. ' EPS = 1E-9;',
  6762. ' DELTA = 0.001;',
  6763. ' Big = 129.789E+100;',
  6764. ' Test0_15 = 0.15;',
  6765. ' Test999 = 2.9999999999999;',
  6766. ' Test111999 = 211199999999999000.0;',
  6767. ' TestMinus111999 = -211199999999999000.0;',
  6768. 'var',
  6769. ' d: double = b;',
  6770. 'begin',
  6771. ' d:=1.0;',
  6772. ' d:=1.0/3.0;',
  6773. ' d:=1/3;',
  6774. ' d:=5.0E-324;',
  6775. ' d:=1.7E308;',
  6776. ' d:=001.00E00;',
  6777. ' d:=002.00E001;',
  6778. ' d:=003.000E000;',
  6779. ' d:=-004.00E-00;',
  6780. ' d:=-005.00E-001;',
  6781. ' d:=10**3;',
  6782. ' d:=10 mod 3;',
  6783. ' d:=10 div 3;',
  6784. ' d:=c;',
  6785. ' d:=f0_1;',
  6786. ' d:=f0_3;',
  6787. ' d:=fn0_1;',
  6788. ' d:=fn0_3;',
  6789. ' d:=fn0_003;',
  6790. ' d:=fn0_123456789;',
  6791. ' d:=fn300_0;',
  6792. ' d:=fn123456_0;',
  6793. ' d:=fn1234567_8;',
  6794. ' d:=fn12345678_9;',
  6795. ' d:=f1_0En12;',
  6796. ' d:=fn1_0En12;',
  6797. ' d:=maxdouble;',
  6798. ' d:=mindouble;',
  6799. ' d:=MinSafeIntDouble;',
  6800. ' d:=double(MinSafeIntDouble);',
  6801. ' d:=MinSafeIntDouble2;',
  6802. ' d:=double(MinSafeIntDouble2);',
  6803. ' d:=MaxSafeIntDouble;',
  6804. ' d:=default(double);',
  6805. '']);
  6806. ConvertProgram;
  6807. CheckSource('TestDouble',
  6808. LinesToStr([
  6809. 'this.a = 2.7;',
  6810. 'this.b = 2.7 + 1.7;',
  6811. 'this.c = 0.9 + 0.1;',
  6812. 'this.f0_1 = 0.1;',
  6813. 'this.f0_3 = 0.3;',
  6814. 'this.fn0_1 = -0.1;',
  6815. 'this.fn0_3 = -0.3;',
  6816. 'this.fn0_003 = -0.003;',
  6817. 'this.fn0_123456789 = -0.123456789;',
  6818. 'this.fn300_0 = -300.0;',
  6819. 'this.fn123456_0 = -123456.0;',
  6820. 'this.fn1234567_8 = -1234567.8;',
  6821. 'this.fn12345678_9 = -12345678.9;',
  6822. 'this.f1_0En12 = 1E-12;',
  6823. 'this.fn1_0En12 = -1E-12;',
  6824. 'this.maxdouble = 1.7e+308;',
  6825. 'this.mindouble = -1.7e+308;',
  6826. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6827. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6828. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6829. 'this.DZeroResolution = 1E-12;',
  6830. 'this.Minus1 = -1E-12;',
  6831. 'this.EPS = 1E-9;',
  6832. 'this.DELTA = 0.001;',
  6833. 'this.Big = 129.789E+100;',
  6834. 'this.Test0_15 = 0.15;',
  6835. 'this.Test999 = 2.9999999999999;',
  6836. 'this.Test111999 = 211199999999999000.0;',
  6837. 'this.TestMinus111999 = -211199999999999000.0;',
  6838. 'this.d = 4.4;'
  6839. ]),
  6840. LinesToStr([
  6841. '$mod.d = 1.0;',
  6842. '$mod.d = 1.0 / 3.0;',
  6843. '$mod.d = 1 / 3;',
  6844. '$mod.d = 5.0E-324;',
  6845. '$mod.d = 1.7E308;',
  6846. '$mod.d = 1.00E0;',
  6847. '$mod.d = 2.00E1;',
  6848. '$mod.d = 3.000E0;',
  6849. '$mod.d = -4.00E-0;',
  6850. '$mod.d = -5.00E-1;',
  6851. '$mod.d = Math.pow(10, 3);',
  6852. '$mod.d = 10 % 3;',
  6853. '$mod.d = rtl.trunc(10 / 3);',
  6854. '$mod.d = 1;',
  6855. '$mod.d = 0.1;',
  6856. '$mod.d = 0.3;',
  6857. '$mod.d = -0.1;',
  6858. '$mod.d = -0.3;',
  6859. '$mod.d = -0.003;',
  6860. '$mod.d = -0.123456789;',
  6861. '$mod.d = -300;',
  6862. '$mod.d = -123456;',
  6863. '$mod.d = -1234567.8;',
  6864. '$mod.d = -1.23456789E7;',
  6865. '$mod.d = 1E-12;',
  6866. '$mod.d = -1E-12;',
  6867. '$mod.d = 1.7E308;',
  6868. '$mod.d = -1.7E308;',
  6869. '$mod.d = -9007199254740991;',
  6870. '$mod.d = -9007199254740991;',
  6871. '$mod.d = -9.007199254740992E15;',
  6872. '$mod.d = -9.007199254740992E15;',
  6873. '$mod.d = 9007199254740991;',
  6874. '$mod.d = 0.0;',
  6875. '']));
  6876. end;
  6877. procedure TTestModule.TestInteger;
  6878. begin
  6879. StartProgram(false);
  6880. Add([
  6881. 'const',
  6882. ' MinInt = low(NativeInt);',
  6883. ' MaxInt = high(NativeInt);',
  6884. 'type',
  6885. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6886. 'const',
  6887. ' a = low(TMyInt)+High(TMyInt);',
  6888. 'var',
  6889. ' i: TMyInt;',
  6890. 'begin',
  6891. ' i:=-MinInt;',
  6892. ' i:=default(TMyInt);',
  6893. ' i:=low(i)+high(i);',
  6894. '']);
  6895. ConvertProgram;
  6896. CheckSource('TestIntegerRange',
  6897. LinesToStr([
  6898. 'this.MinInt = -9007199254740991;',
  6899. 'this.MaxInt = 9007199254740991;',
  6900. 'this.a = -9007199254740991 + 9007199254740991;',
  6901. 'this.i = 0;',
  6902. '']),
  6903. LinesToStr([
  6904. '$mod.i = - -9007199254740991;',
  6905. '$mod.i = -9007199254740991;',
  6906. '$mod.i = -9007199254740991 + 9007199254740991;',
  6907. '']));
  6908. end;
  6909. procedure TTestModule.TestIntegerRange;
  6910. begin
  6911. StartProgram(false);
  6912. Add([
  6913. 'const',
  6914. ' MinInt = -1;',
  6915. ' MaxInt = +1;',
  6916. 'type',
  6917. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6918. ' TInt2 = 1..3;',
  6919. 'const',
  6920. ' a = low(TMyInt)+High(TMyInt);',
  6921. ' b = low(TInt2)+High(TInt2);',
  6922. ' s1 = [1];',
  6923. ' s2 = [1,2];',
  6924. ' s3 = [1..3];',
  6925. ' s4 = [low(shortint)..high(shortint)];',
  6926. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6927. ' s6 = 1 in s2;',
  6928. 'var',
  6929. ' i: TMyInt;',
  6930. ' i2: TInt2;',
  6931. 'begin',
  6932. ' i:=i2;',
  6933. ' i:=default(TMyInt);',
  6934. ' if i=i2 then ;',
  6935. ' i:=ord(i2);',
  6936. '']);
  6937. ConvertProgram;
  6938. CheckSource('TestIntegerRange',
  6939. LinesToStr([
  6940. 'this.MinInt = -1;',
  6941. 'this.MaxInt = +1;',
  6942. 'this.a = -1 + 1;',
  6943. 'this.b = 1 + 3;',
  6944. 'this.s1 = rtl.createSet(1);',
  6945. 'this.s2 = rtl.createSet(1, 2);',
  6946. 'this.s3 = rtl.createSet(null, 1, 3);',
  6947. 'this.s4 = rtl.createSet(null, -128, 127);',
  6948. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6949. 'this.s6 = 1 in this.s2;',
  6950. 'this.i = 0;',
  6951. 'this.i2 = 0;',
  6952. '']),
  6953. LinesToStr([
  6954. '$mod.i = $mod.i2;',
  6955. '$mod.i = -1;',
  6956. 'if ($mod.i === $mod.i2) ;',
  6957. '$mod.i = $mod.i2;',
  6958. '']));
  6959. end;
  6960. procedure TTestModule.TestIntegerTypecasts;
  6961. begin
  6962. StartProgram(false);
  6963. Add([
  6964. 'var',
  6965. ' i: nativeint;',
  6966. ' b: byte;',
  6967. ' sh: shortint;',
  6968. ' w: word;',
  6969. ' sm: smallint;',
  6970. ' lw: longword;',
  6971. ' li: longint;',
  6972. 'begin',
  6973. ' b:=byte(i);',
  6974. ' sh:=shortint(i);',
  6975. ' w:=word(i);',
  6976. ' sm:=smallint(i);',
  6977. ' lw:=longword(i);',
  6978. ' li:=longint(i);',
  6979. '']);
  6980. ConvertProgram;
  6981. CheckSource('TestIntegerTypecasts',
  6982. LinesToStr([
  6983. 'this.i = 0;',
  6984. 'this.b = 0;',
  6985. 'this.sh = 0;',
  6986. 'this.w = 0;',
  6987. 'this.sm = 0;',
  6988. 'this.lw = 0;',
  6989. 'this.li = 0;',
  6990. '']),
  6991. LinesToStr([
  6992. '$mod.b = $mod.i & 255;',
  6993. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6994. '$mod.w = $mod.i & 65535;',
  6995. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6996. '$mod.lw = $mod.i >>> 0;',
  6997. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6998. '']));
  6999. end;
  7000. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7001. begin
  7002. StartProgram(false);
  7003. Add([
  7004. 'var',
  7005. ' i,j: nativeint;',
  7006. 'begin',
  7007. ' i:=i shr 0;',
  7008. ' i:=i shr 1;',
  7009. ' i:=i shr 3;',
  7010. ' i:=i shr 54;',
  7011. ' i:=j shr i;',
  7012. '']);
  7013. ConvertProgram;
  7014. CheckResolverUnexpectedHints;
  7015. CheckSource('TestInteger_BitwiseShrNativeInt',
  7016. LinesToStr([
  7017. 'this.i = 0;',
  7018. 'this.j = 0;',
  7019. '']),
  7020. LinesToStr([
  7021. '$mod.i = $mod.i;',
  7022. '$mod.i = Math.floor($mod.i / 2);',
  7023. '$mod.i = Math.floor($mod.i / 8);',
  7024. '$mod.i = 0;',
  7025. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7026. '']));
  7027. end;
  7028. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7029. begin
  7030. StartProgram(false);
  7031. Add([
  7032. 'var',
  7033. ' i: nativeint;',
  7034. 'begin',
  7035. ' i:=i shl 0;',
  7036. ' i:=i shl 54;',
  7037. ' i:=123456789012 shl 1;',
  7038. ' i:=i shl 1;',
  7039. '']);
  7040. ConvertProgram;
  7041. CheckResolverUnexpectedHints;
  7042. CheckSource('TestInteger_BitwiseShrNativeInt',
  7043. LinesToStr([
  7044. 'this.i = 0;',
  7045. '']),
  7046. LinesToStr([
  7047. '$mod.i = $mod.i;',
  7048. '$mod.i = 0;',
  7049. '$mod.i = 246913578024;',
  7050. '$mod.i = rtl.shl($mod.i, 1);',
  7051. '']));
  7052. end;
  7053. procedure TTestModule.TestInteger_SystemFunc;
  7054. begin
  7055. StartProgram(true);
  7056. Add([
  7057. 'var',
  7058. ' i: byte;',
  7059. ' s: string;',
  7060. 'begin',
  7061. ' system.inc(i);',
  7062. ' system.str(i,s);',
  7063. ' s:=system.str(i);',
  7064. ' i:=system.low(i);',
  7065. ' i:=system.high(i);',
  7066. ' i:=system.pred(i);',
  7067. ' i:=system.succ(i);',
  7068. ' i:=system.ord(i);',
  7069. '']);
  7070. ConvertProgram;
  7071. CheckResolverUnexpectedHints;
  7072. CheckSource('TestInteger_SystemFunc',
  7073. LinesToStr([
  7074. 'this.i = 0;',
  7075. 'this.s = "";',
  7076. '']),
  7077. LinesToStr([
  7078. '$mod.i += 1;',
  7079. '$mod.s = "" + $mod.i;',
  7080. '$mod.s = "" + $mod.i;',
  7081. '$mod.i = 0;',
  7082. '$mod.i = 255;',
  7083. '$mod.i = $mod.i - 1;',
  7084. '$mod.i = $mod.i + 1;',
  7085. '$mod.i = $mod.i;',
  7086. '']));
  7087. end;
  7088. procedure TTestModule.TestCurrency;
  7089. begin
  7090. StartProgram(false);
  7091. Add([
  7092. 'type',
  7093. ' TCoin = currency;',
  7094. 'const',
  7095. ' a = TCoin(2.7);',
  7096. ' b = a + TCoin(1.7);',
  7097. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7098. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7099. 'var',
  7100. ' c: TCoin = b;',
  7101. ' i: nativeint;',
  7102. ' d: double;',
  7103. ' j: jsvalue;',
  7104. 'function DoIt(c: currency): currency; begin end;',
  7105. 'function GetIt(d: double): double; begin end;',
  7106. 'procedure Write(v: jsvalue); begin end;',
  7107. 'begin',
  7108. ' c:=1.0;',
  7109. ' c:=0.1;',
  7110. ' c:=1.0/3.0;',
  7111. ' c:=1/3;',
  7112. ' c:=a;',
  7113. ' d:=c;',
  7114. ' c:=d;',
  7115. ' c:=currency(c);',
  7116. ' c:=currency(d);',
  7117. ' d:=double(c);',
  7118. ' c:=i;',
  7119. ' c:=currency(i);',
  7120. //' i:=c;', not allowed
  7121. ' i:=nativeint(c);',
  7122. ' c:=c+a;',
  7123. ' c:=-c-a;',
  7124. ' c:=d+c;',
  7125. ' c:=c+d;',
  7126. ' c:=d-c;',
  7127. ' c:=c-d;',
  7128. ' c:=c*a;',
  7129. ' c:=a*c;',
  7130. ' c:=d*c;',
  7131. ' c:=c*d;',
  7132. ' c:=c/a;',
  7133. ' c:=a/c;',
  7134. ' c:=d/c;',
  7135. ' c:=c/d;',
  7136. ' c:=c**a;',
  7137. ' c:=a**c;',
  7138. ' c:=d**c;',
  7139. ' c:=c**d;',
  7140. ' if c=c then ;',
  7141. ' if c=a then ;',
  7142. ' if a=c then ;',
  7143. ' if d=c then ;',
  7144. ' if c=d then ;',
  7145. ' c:=DoIt(c);',
  7146. ' c:=DoIt(i);',
  7147. ' c:=DoIt(d);',
  7148. ' c:=GetIt(c);',
  7149. ' j:=c;',
  7150. ' Write(c);',
  7151. ' c:=default(currency);',
  7152. ' j:=str(c);',
  7153. ' j:=str(c:0:3);',
  7154. '']);
  7155. ConvertProgram;
  7156. CheckSource('TestCurrency',
  7157. LinesToStr([
  7158. 'this.a = 27000;',
  7159. 'this.b = this.a + 17000;',
  7160. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7161. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7162. 'this.c = this.b;',
  7163. 'this.i = 0;',
  7164. 'this.d = 0.0;',
  7165. 'this.j = undefined;',
  7166. 'this.DoIt = function (c) {',
  7167. ' var Result = 0;',
  7168. ' return Result;',
  7169. '};',
  7170. 'this.GetIt = function (d) {',
  7171. ' var Result = 0.0;',
  7172. ' return Result;',
  7173. '};',
  7174. 'this.Write = function (v) {',
  7175. '};',
  7176. '']),
  7177. LinesToStr([
  7178. '$mod.c = 10000;',
  7179. '$mod.c = 1000;',
  7180. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7181. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7182. '$mod.c = $mod.a;',
  7183. '$mod.d = $mod.c / 10000;',
  7184. '$mod.c = rtl.trunc($mod.d * 10000);',
  7185. '$mod.c = $mod.c;',
  7186. '$mod.c = $mod.d * 10000;',
  7187. '$mod.d = $mod.c / 10000;',
  7188. '$mod.c = $mod.i * 10000;',
  7189. '$mod.c = $mod.i * 10000;',
  7190. '$mod.i = rtl.trunc($mod.c / 10000);',
  7191. '$mod.c = $mod.c + $mod.a;',
  7192. '$mod.c = -$mod.c - $mod.a;',
  7193. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7194. '$mod.c = $mod.c + ($mod.d * 10000);',
  7195. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7196. '$mod.c = $mod.c - ($mod.d * 10000);',
  7197. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7198. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7199. '$mod.c = $mod.d * $mod.c;',
  7200. '$mod.c = $mod.c * $mod.d;',
  7201. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7202. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7203. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7204. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7205. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7206. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7207. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7208. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7209. 'if ($mod.c === $mod.c) ;',
  7210. 'if ($mod.c === $mod.a) ;',
  7211. 'if ($mod.a === $mod.c) ;',
  7212. 'if (($mod.d * 10000) === $mod.c) ;',
  7213. 'if ($mod.c === ($mod.d * 10000)) ;',
  7214. '$mod.c = $mod.DoIt($mod.c);',
  7215. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7216. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7217. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7218. '$mod.j = $mod.c / 10000;',
  7219. '$mod.Write($mod.c / 10000);',
  7220. '$mod.c = 0;',
  7221. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7222. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7223. '']));
  7224. end;
  7225. procedure TTestModule.TestForBoolDo;
  7226. begin
  7227. StartProgram(false);
  7228. Add([
  7229. 'var b: boolean;',
  7230. 'begin',
  7231. ' for b:=false to true do ;',
  7232. ' for b:=b downto false do ;',
  7233. ' for b in boolean do ;',
  7234. '']);
  7235. ConvertProgram;
  7236. CheckSource('TestForBoolDo',
  7237. LinesToStr([ // statements
  7238. 'this.b = false;']),
  7239. LinesToStr([ // this.$main
  7240. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7241. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7242. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7243. '']));
  7244. end;
  7245. procedure TTestModule.TestForIntDo;
  7246. begin
  7247. StartProgram(false);
  7248. Add([
  7249. 'var i: longint;',
  7250. 'begin',
  7251. ' for i:=3 to 5 do ;',
  7252. ' for i:=i downto 2 do ;',
  7253. ' for i in byte do ;',
  7254. '']);
  7255. ConvertProgram;
  7256. CheckSource('TestForIntDo',
  7257. LinesToStr([ // statements
  7258. 'this.i = 0;']),
  7259. LinesToStr([ // this.$main
  7260. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7261. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7262. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7263. '']));
  7264. end;
  7265. procedure TTestModule.TestForIntInDo;
  7266. begin
  7267. StartProgram(false);
  7268. Add([
  7269. 'type',
  7270. ' TSetOfInt = set of byte;',
  7271. ' TIntRg = 3..7;',
  7272. ' TSetOfIntRg = set of TIntRg;',
  7273. 'var',
  7274. ' i,i2: longint;',
  7275. ' a1: array of byte;',
  7276. ' a2: array[1..3] of byte;',
  7277. ' soi: TSetOfInt;',
  7278. ' soir: TSetOfIntRg;',
  7279. ' ir: TIntRg;',
  7280. 'begin',
  7281. ' for i in byte do ;',
  7282. ' for i in a1 do ;',
  7283. ' for i in a2 do ;',
  7284. ' for i in [11..13] do ;',
  7285. ' for i in TSetOfInt do ;',
  7286. ' for i in TIntRg do ;',
  7287. ' for i in soi do i2:=i;',
  7288. ' for i in TSetOfIntRg do ;',
  7289. ' for i in soir do ;',
  7290. ' for ir in TIntRg do ;',
  7291. ' for ir in TSetOfIntRg do ;',
  7292. ' for ir in soir do ;',
  7293. '']);
  7294. ConvertProgram;
  7295. CheckSource('TestForIntInDo',
  7296. LinesToStr([ // statements
  7297. 'this.i = 0;',
  7298. 'this.i2 = 0;',
  7299. 'this.a1 = [];',
  7300. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7301. 'this.soi = {};',
  7302. 'this.soir = {};',
  7303. 'this.ir = 0;',
  7304. '']),
  7305. LinesToStr([ // this.$main
  7306. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7307. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7308. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7309. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7310. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7311. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7312. 'for (var $l6 in $mod.soi) {',
  7313. ' $mod.i = +$l6;',
  7314. ' $mod.i2 = $mod.i;',
  7315. '};',
  7316. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7317. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7318. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7319. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7320. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7321. '']));
  7322. end;
  7323. procedure TTestModule.TestCharConst;
  7324. begin
  7325. StartProgram(false);
  7326. Add([
  7327. 'const',
  7328. ' a = #$00F3;',
  7329. ' c: char = ''1'';',
  7330. ' wc: widechar = ''ä'';',
  7331. 'begin',
  7332. ' c:=#0;',
  7333. ' c:=#1;',
  7334. ' c:=#9;',
  7335. ' c:=#10;',
  7336. ' c:=#13;',
  7337. ' c:=#31;',
  7338. ' c:=#32;',
  7339. ' c:=#$A;',
  7340. ' c:=#$0A;',
  7341. ' c:=#$b;',
  7342. ' c:=#$0b;',
  7343. ' c:=^A;',
  7344. ' c:=''"'';',
  7345. ' c:=default(char);',
  7346. ' c:=#$00E4;', // ä
  7347. ' c:=''ä'';',
  7348. ' c:=#$E4;', // ä
  7349. ' c:=#$D800;', // invalid UTF-16
  7350. ' c:=#$DFFF;', // invalid UTF-16
  7351. ' c:=#$FFFF;', // last UCS-2
  7352. ' c:=high(c);', // last UCS-2
  7353. ' c:=#269;',
  7354. '']);
  7355. ConvertProgram;
  7356. CheckSource('TestCharConst',
  7357. LinesToStr([
  7358. 'this.a="ó";',
  7359. 'this.c="1";',
  7360. 'this.wc="ä";'
  7361. ]),
  7362. LinesToStr([
  7363. '$mod.c="\x00";',
  7364. '$mod.c="\x01";',
  7365. '$mod.c="\t";',
  7366. '$mod.c="\n";',
  7367. '$mod.c="\r";',
  7368. '$mod.c="\x1F";',
  7369. '$mod.c=" ";',
  7370. '$mod.c="\n";',
  7371. '$mod.c="\n";',
  7372. '$mod.c="\x0B";',
  7373. '$mod.c="\x0B";',
  7374. '$mod.c="\x01";',
  7375. '$mod.c=''"'';',
  7376. '$mod.c="\x00";',
  7377. '$mod.c = "ä";',
  7378. '$mod.c = "ä";',
  7379. '$mod.c = "ä";',
  7380. '$mod.c="\uD800";',
  7381. '$mod.c="\uDFFF";',
  7382. '$mod.c="\uFFFF";',
  7383. '$mod.c="\uFFFF";',
  7384. '$mod.c = "č";',
  7385. '']));
  7386. end;
  7387. procedure TTestModule.TestChar_Compare;
  7388. begin
  7389. StartProgram(false);
  7390. Add('var');
  7391. Add(' c: char;');
  7392. Add(' b: boolean;');
  7393. Add('begin');
  7394. Add(' b:=c=''1'';');
  7395. Add(' b:=''2''=c;');
  7396. Add(' b:=''3''=''4'';');
  7397. Add(' b:=c<>''5'';');
  7398. Add(' b:=''6''<>c;');
  7399. Add(' b:=c>''7'';');
  7400. Add(' b:=''8''>c;');
  7401. Add(' b:=c>=''9'';');
  7402. Add(' b:=''A''>=c;');
  7403. Add(' b:=c<''B'';');
  7404. Add(' b:=''C''<c;');
  7405. Add(' b:=c<=''D'';');
  7406. Add(' b:=''E''<=c;');
  7407. ConvertProgram;
  7408. CheckSource('TestChar_Compare',
  7409. LinesToStr([
  7410. 'this.c="";',
  7411. 'this.b = false;'
  7412. ]),
  7413. LinesToStr([
  7414. '$mod.b = $mod.c === "1";',
  7415. '$mod.b = "2" === $mod.c;',
  7416. '$mod.b = "3" === "4";',
  7417. '$mod.b = $mod.c !== "5";',
  7418. '$mod.b = "6" !== $mod.c;',
  7419. '$mod.b = $mod.c > "7";',
  7420. '$mod.b = "8" > $mod.c;',
  7421. '$mod.b = $mod.c >= "9";',
  7422. '$mod.b = "A" >= $mod.c;',
  7423. '$mod.b = $mod.c < "B";',
  7424. '$mod.b = "C" < $mod.c;',
  7425. '$mod.b = $mod.c <= "D";',
  7426. '$mod.b = "E" <= $mod.c;',
  7427. '']));
  7428. end;
  7429. procedure TTestModule.TestChar_BuiltInProcs;
  7430. begin
  7431. StartProgram(false);
  7432. Add([
  7433. 'var',
  7434. ' c: char;',
  7435. ' i: longint;',
  7436. ' s: string;',
  7437. 'begin',
  7438. ' i:=ord(c);',
  7439. ' i:=ord(s[i]);',
  7440. ' c:=chr(i);',
  7441. ' c:=pred(c);',
  7442. ' c:=succ(c);',
  7443. ' c:=low(c);',
  7444. ' c:=high(c);',
  7445. ' i:=byte(c);',
  7446. ' i:=word(c);',
  7447. ' i:=longint(c);',
  7448. '']);
  7449. ConvertProgram;
  7450. CheckSource('TestChar_BuiltInProcs',
  7451. LinesToStr([
  7452. 'this.c = "";',
  7453. 'this.i = 0;',
  7454. 'this.s = "";'
  7455. ]),
  7456. LinesToStr([
  7457. '$mod.i = $mod.c.charCodeAt();',
  7458. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7459. '$mod.c = String.fromCharCode($mod.i);',
  7460. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7461. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7462. '$mod.c = "\x00";',
  7463. '$mod.c = "\uFFFF";',
  7464. '$mod.i = $mod.c.charCodeAt() & 255;',
  7465. '$mod.i = $mod.c.charCodeAt();',
  7466. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7467. '']));
  7468. end;
  7469. procedure TTestModule.TestStringConst;
  7470. begin
  7471. StartProgram(false);
  7472. Add([
  7473. '{$H+}',
  7474. 'const',
  7475. ' a = #$00F3#$017C;', // first <256, then >=256
  7476. ' b = string(''a'');',
  7477. ' c = string(''ä'');',
  7478. ' d = UnicodeString(''b'');',
  7479. ' e = UnicodeString(''ö'');',
  7480. 'var',
  7481. ' s: string = ''abc'';',
  7482. 'begin',
  7483. ' s:='''';',
  7484. ' s:=#13#10;',
  7485. ' s:=#9''foo'';',
  7486. ' s:=#$A9;',
  7487. ' s:=''foo''#13''bar'';',
  7488. ' s:=''"'';',
  7489. ' s:=''"''''"'';',
  7490. ' s:=#$20AC;', // euro
  7491. ' s:=#$10437;', // outside BMP
  7492. ' s:=''abc''#$20AC;', // ascii,#
  7493. ' s:=''ä''#$20AC;', // non ascii,#
  7494. ' s:=#$20AC''abc'';', // #, ascii
  7495. ' s:=#$20AC''ä'';', // #, non ascii
  7496. ' s:=default(string);',
  7497. ' s:=concat(s);',
  7498. ' s:=concat(s,''a'',s);',
  7499. ' s:=#250#269;',
  7500. //' s:=#$2F804;',
  7501. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7502. '']);
  7503. ConvertProgram;
  7504. CheckSource('TestStringConst',
  7505. LinesToStr([
  7506. 'this.a = "óż";',
  7507. 'this.b = "a";',
  7508. 'this.c = "ä";',
  7509. 'this.d = "b";',
  7510. 'this.e = "ö";',
  7511. 'this.s="abc";',
  7512. '']),
  7513. LinesToStr([
  7514. '$mod.s="";',
  7515. '$mod.s="\r\n";',
  7516. '$mod.s="\tfoo";',
  7517. '$mod.s="©";',
  7518. '$mod.s="foo\rbar";',
  7519. '$mod.s=''"'';',
  7520. '$mod.s=''"\''"'';',
  7521. '$mod.s="€";',
  7522. '$mod.s="'#$F0#$90#$90#$B7'";',
  7523. '$mod.s = "abc€";',
  7524. '$mod.s = "ä€";',
  7525. '$mod.s = "€abc";',
  7526. '$mod.s = "ۊ";',
  7527. '$mod.s="";',
  7528. '$mod.s = $mod.s;',
  7529. '$mod.s = $mod.s.concat("a", $mod.s);',
  7530. '$mod.s = "úč";',
  7531. '']));
  7532. end;
  7533. procedure TTestModule.TestStringConst_InvalidUTF16;
  7534. begin
  7535. StartProgram(false);
  7536. Add([
  7537. 'const',
  7538. ' a: char = #$D87E;',
  7539. ' b: string = #$D87E;',
  7540. ' c: string = #$D87E#43;',
  7541. 'begin',
  7542. ' c:=''abc''#$D87E;',
  7543. ' c:=#0#1#2;',
  7544. ' c:=#127;',
  7545. ' c:=#128;',
  7546. ' c:=#255;',
  7547. ' c:=#256;',
  7548. '']);
  7549. ConvertProgram;
  7550. CheckSource('TestStringConst',
  7551. LinesToStr([
  7552. 'this.a = "\uD87E";',
  7553. 'this.b = "\uD87E";',
  7554. 'this.c = "\uD87E+";',
  7555. '']),
  7556. LinesToStr([
  7557. '$mod.c = "abc\uD87E";',
  7558. '$mod.c = "\x00\x01\x02";',
  7559. '$mod.c = "'#127'";',
  7560. '$mod.c = "'#$c2#$80'";',
  7561. '$mod.c = "'#$c3#$BF'";',
  7562. '$mod.c = "'#$c4#$80'";',
  7563. '']));
  7564. end;
  7565. procedure TTestModule.TestStringConstSurrogate;
  7566. begin
  7567. StartProgram(false);
  7568. Add([
  7569. 'var',
  7570. ' s: string;',
  7571. 'begin',
  7572. ' s:=''😊'';', // 1F60A
  7573. '']);
  7574. ConvertProgram;
  7575. CheckSource('TestStringConstSurrogate',
  7576. LinesToStr([
  7577. 'this.s="";'
  7578. ]),
  7579. LinesToStr([
  7580. '$mod.s="😊";'
  7581. ]));
  7582. end;
  7583. procedure TTestModule.TestString_Length;
  7584. begin
  7585. StartProgram(false);
  7586. Add('const c = ''foo'';');
  7587. Add('var');
  7588. Add(' s: string;');
  7589. Add(' i: longint;');
  7590. Add('begin');
  7591. Add(' i:=length(s);');
  7592. Add(' i:=length(s+s);');
  7593. Add(' i:=length(''abc'');');
  7594. Add(' i:=length(c);');
  7595. ConvertProgram;
  7596. CheckSource('TestString_Length',
  7597. LinesToStr([
  7598. 'this.c = "foo";',
  7599. 'this.s = "";',
  7600. 'this.i = 0;',
  7601. '']),
  7602. LinesToStr([
  7603. '$mod.i = $mod.s.length;',
  7604. '$mod.i = ($mod.s+$mod.s).length;',
  7605. '$mod.i = "abc".length;',
  7606. '$mod.i = $mod.c.length;',
  7607. '']));
  7608. end;
  7609. procedure TTestModule.TestString_Compare;
  7610. begin
  7611. StartProgram(false);
  7612. Add('var');
  7613. Add(' s, t: string;');
  7614. Add(' b: boolean;');
  7615. Add('begin');
  7616. Add(' b:=s=t;');
  7617. Add(' b:=s<>t;');
  7618. Add(' b:=s>t;');
  7619. Add(' b:=s>=t;');
  7620. Add(' b:=s<t;');
  7621. Add(' b:=s<=t;');
  7622. ConvertProgram;
  7623. CheckSource('TestString_Compare',
  7624. LinesToStr([ // statements
  7625. 'this.s = "";',
  7626. 'this.t = "";',
  7627. 'this.b =false;'
  7628. ]),
  7629. LinesToStr([ // this.$main
  7630. '$mod.b = $mod.s === $mod.t;',
  7631. '$mod.b = $mod.s !== $mod.t;',
  7632. '$mod.b = $mod.s > $mod.t;',
  7633. '$mod.b = $mod.s >= $mod.t;',
  7634. '$mod.b = $mod.s < $mod.t;',
  7635. '$mod.b = $mod.s <= $mod.t;',
  7636. '']));
  7637. end;
  7638. procedure TTestModule.TestString_SetLength;
  7639. begin
  7640. StartProgram(false);
  7641. Add([
  7642. 'procedure DoIt(var s: string);',
  7643. 'begin',
  7644. ' SetLength(s,2);',
  7645. 'end;',
  7646. 'var s: string;',
  7647. 'begin',
  7648. ' SetLength(s,3);',
  7649. '']);
  7650. ConvertProgram;
  7651. CheckSource('TestString_SetLength',
  7652. LinesToStr([ // statements
  7653. 'this.DoIt = function (s) {',
  7654. ' s.set(rtl.strSetLength(s.get(), 2));',
  7655. '};',
  7656. 'this.s = "";',
  7657. '']),
  7658. LinesToStr([ // this.$main
  7659. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7660. ]));
  7661. end;
  7662. procedure TTestModule.TestString_CharAt;
  7663. begin
  7664. StartProgram(false);
  7665. Add([
  7666. 'var',
  7667. ' s: string;',
  7668. ' c: char;',
  7669. ' b: boolean;',
  7670. 'begin',
  7671. ' b:= s[1] = c;',
  7672. ' b:= c = s[1];',
  7673. ' b:= c <> s[1];',
  7674. ' b:= c > s[1];',
  7675. ' b:= c >= s[1];',
  7676. ' b:= c < s[2];',
  7677. ' b:= c <= s[1];',
  7678. ' s[1] := c;',
  7679. ' s[2+3] := c;']);
  7680. ConvertProgram;
  7681. CheckSource('TestString_CharAt',
  7682. LinesToStr([ // statements
  7683. 'this.s = "";',
  7684. 'this.c = "";',
  7685. 'this.b = false;'
  7686. ]),
  7687. LinesToStr([ // this.$main
  7688. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7689. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7690. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7691. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7692. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7693. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7694. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7695. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7696. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7697. '']));
  7698. end;
  7699. procedure TTestModule.TestStringHMinusFail;
  7700. begin
  7701. StartProgram(false);
  7702. Add([
  7703. '{$H-}',
  7704. 'var s: string;',
  7705. 'begin']);
  7706. ConvertProgram;
  7707. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7708. end;
  7709. procedure TTestModule.TestStr;
  7710. begin
  7711. StartProgram(false);
  7712. Add('var');
  7713. Add(' b: boolean;');
  7714. Add(' i: longint;');
  7715. Add(' d: double;');
  7716. Add(' s: string;');
  7717. Add('begin');
  7718. Add(' str(b,s);');
  7719. Add(' str(i,s);');
  7720. Add(' str(d,s);');
  7721. Add(' str(i:3,s);');
  7722. Add(' str(d:3:2,s);');
  7723. Add(' Str(12.456:12:1,s);');
  7724. Add(' Str(12.456:12,s);');
  7725. Add(' s:=str(b);');
  7726. Add(' s:=str(i);');
  7727. Add(' s:=str(d);');
  7728. Add(' s:=str(i,i);');
  7729. Add(' s:=str(i:3);');
  7730. Add(' s:=str(d:3:2);');
  7731. Add(' s:=str(i:4,i);');
  7732. Add(' s:=str(i,i:5);');
  7733. Add(' s:=str(i:4,i:5);');
  7734. Add(' s:=str(s,s);');
  7735. Add(' s:=str(s,''foo'');');
  7736. ConvertProgram;
  7737. CheckSource('TestStr',
  7738. LinesToStr([ // statements
  7739. 'this.b = false;',
  7740. 'this.i = 0;',
  7741. 'this.d = 0.0;',
  7742. 'this.s = "";',
  7743. '']),
  7744. LinesToStr([ // this.$main
  7745. '$mod.s = ""+$mod.b;',
  7746. '$mod.s = ""+$mod.i;',
  7747. '$mod.s = rtl.floatToStr($mod.d);',
  7748. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7749. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7750. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7751. '$mod.s = rtl.floatToStr(12.456,12);',
  7752. '$mod.s = ""+$mod.b;',
  7753. '$mod.s = ""+$mod.i;',
  7754. '$mod.s = rtl.floatToStr($mod.d);',
  7755. '$mod.s = ""+$mod.i+$mod.i;',
  7756. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7757. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7758. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7759. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7760. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7761. '$mod.s = $mod.s + $mod.s;',
  7762. '$mod.s = $mod.s + "foo";',
  7763. '']));
  7764. end;
  7765. procedure TTestModule.TestBaseType_AnsiStringFail;
  7766. begin
  7767. StartProgram(false);
  7768. Add('var s: AnsiString');
  7769. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7770. ConvertProgram;
  7771. end;
  7772. procedure TTestModule.TestBaseType_WideStringFail;
  7773. begin
  7774. StartProgram(false);
  7775. Add('var s: WideString');
  7776. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7777. ConvertProgram;
  7778. end;
  7779. procedure TTestModule.TestBaseType_ShortStringFail;
  7780. begin
  7781. StartProgram(false);
  7782. Add('var s: ShortString');
  7783. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7784. ConvertProgram;
  7785. end;
  7786. procedure TTestModule.TestBaseType_RawByteStringFail;
  7787. begin
  7788. StartProgram(false);
  7789. Add('var s: RawByteString');
  7790. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7791. ConvertProgram;
  7792. end;
  7793. procedure TTestModule.TestTypeShortstring_Fail;
  7794. begin
  7795. StartProgram(false);
  7796. Add('type t = string[12];');
  7797. Add('var s: t;');
  7798. Add('begin');
  7799. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7800. ConvertProgram;
  7801. end;
  7802. procedure TTestModule.TestCharSet_Custom;
  7803. begin
  7804. StartProgram(false);
  7805. Add([
  7806. 'type',
  7807. ' TCharRg = ''a''..''z'';',
  7808. ' TSetOfCharRg = set of TCharRg;',
  7809. ' TCharRg2 = ''m''..''p'';',
  7810. 'const',
  7811. ' crg: TCharRg = ''b'';',
  7812. 'var',
  7813. ' c: char;',
  7814. ' crg2: TCharRg2;',
  7815. ' s: TSetOfCharRg;',
  7816. 'begin',
  7817. ' c:=crg;',
  7818. ' crg:=c;',
  7819. ' crg2:=crg;',
  7820. ' if c=crg then ;',
  7821. ' if crg=c then ;',
  7822. ' if crg=crg2 then ;',
  7823. ' if c in s then ;',
  7824. ' if crg2 in s then ;',
  7825. ' c:=default(TCharRg);',
  7826. '']);
  7827. ConvertProgram;
  7828. CheckSource('TestCharSet_Custom',
  7829. LinesToStr([ // statements
  7830. 'this.crg = "b";',
  7831. 'this.c = "";',
  7832. 'this.crg2 = "m";',
  7833. 'this.s = {};',
  7834. '']),
  7835. LinesToStr([ // this.$main
  7836. '$mod.c = $mod.crg;',
  7837. '$mod.crg = $mod.c;',
  7838. '$mod.crg2 = $mod.crg;',
  7839. 'if ($mod.c === $mod.crg) ;',
  7840. 'if ($mod.crg === $mod.c) ;',
  7841. 'if ($mod.crg === $mod.crg2) ;',
  7842. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7843. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7844. '$mod.c = "a";',
  7845. '']));
  7846. end;
  7847. procedure TTestModule.TestWideChar;
  7848. begin
  7849. StartProgram(false);
  7850. Add([
  7851. 'procedure Fly(var c: char);',
  7852. 'begin',
  7853. 'end;',
  7854. 'procedure Run(var c: widechar);',
  7855. 'begin',
  7856. 'end;',
  7857. 'var',
  7858. ' c: char;',
  7859. ' wc: widechar;',
  7860. ' w: word;',
  7861. 'begin',
  7862. ' Fly(wc);',
  7863. ' Run(c);',
  7864. ' wc:=WideChar(w);',
  7865. ' w:=ord(wc);',
  7866. '']);
  7867. ConvertProgram;
  7868. CheckSource('TestWideChar_VarArg',
  7869. LinesToStr([ // statements
  7870. 'this.Fly = function (c) {',
  7871. '};',
  7872. 'this.Run = function (c) {',
  7873. '};',
  7874. 'this.c = "";',
  7875. 'this.wc = "";',
  7876. 'this.w = 0;',
  7877. '']),
  7878. LinesToStr([ // this.$main
  7879. '$mod.Fly({',
  7880. ' p: $mod,',
  7881. ' get: function () {',
  7882. ' return this.p.wc;',
  7883. ' },',
  7884. ' set: function (v) {',
  7885. ' this.p.wc = v;',
  7886. ' }',
  7887. '});',
  7888. '$mod.Run({',
  7889. ' p: $mod,',
  7890. ' get: function () {',
  7891. ' return this.p.c;',
  7892. ' },',
  7893. ' set: function (v) {',
  7894. ' this.p.c = v;',
  7895. ' }',
  7896. '});',
  7897. '$mod.wc = String.fromCharCode($mod.w);',
  7898. '$mod.w = $mod.wc.charCodeAt();',
  7899. '',
  7900. '']));
  7901. end;
  7902. procedure TTestModule.TestForCharDo;
  7903. begin
  7904. StartProgram(false);
  7905. Add([
  7906. 'var c: char;',
  7907. 'begin',
  7908. ' for c:=''a'' to ''c'' do ;',
  7909. ' for c:=c downto ''a'' do ;',
  7910. ' for c:=''Б'' to ''Я'' do ;',
  7911. '']);
  7912. ConvertProgram;
  7913. CheckSource('TestForCharDo',
  7914. LinesToStr([ // statements
  7915. 'this.c = "";']),
  7916. LinesToStr([ // this.$main
  7917. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7918. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7919. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7920. '']));
  7921. end;
  7922. procedure TTestModule.TestForCharInDo;
  7923. begin
  7924. StartProgram(false);
  7925. Add([
  7926. 'type',
  7927. ' TSetOfChar = set of char;',
  7928. ' TCharRg = ''a''..''z'';',
  7929. ' TSetOfCharRg = set of TCharRg;',
  7930. 'const Foo = ''foo'';',
  7931. 'var',
  7932. ' c,c2: char;',
  7933. ' s: string;',
  7934. ' a1: array of char;',
  7935. ' a2: array[1..3] of char;',
  7936. ' soc: TSetOfChar;',
  7937. ' socr: TSetOfCharRg;',
  7938. ' cr: TCharRg;',
  7939. 'begin',
  7940. ' for c in foo do ;',
  7941. ' for c in s do ;',
  7942. ' for c in char do ;',
  7943. ' for c in a1 do ;',
  7944. ' for c in a2 do ;',
  7945. ' for c in [''1''..''3''] do ;',
  7946. ' for c in TSetOfChar do ;',
  7947. ' for c in TCharRg do ;',
  7948. ' for c in soc do c2:=c;',
  7949. ' for c in TSetOfCharRg do ;',
  7950. ' for c in socr do ;',
  7951. ' for cr in TCharRg do ;',
  7952. ' for cr in TSetOfCharRg do ;',
  7953. ' for cr in socr do ;',
  7954. '']);
  7955. ConvertProgram;
  7956. CheckSource('TestForCharInDo',
  7957. LinesToStr([ // statements
  7958. 'this.Foo = "foo";',
  7959. 'this.c = "";',
  7960. 'this.c2 = "";',
  7961. 'this.s = "";',
  7962. 'this.a1 = [];',
  7963. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7964. 'this.soc = {};',
  7965. 'this.socr = {};',
  7966. 'this.cr = "a";',
  7967. '']),
  7968. LinesToStr([ // this.$main
  7969. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7970. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7971. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7972. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7973. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7974. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7975. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7976. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7977. 'for (var $l8 in $mod.soc) {',
  7978. ' $mod.c = String.fromCharCode($l8);',
  7979. ' $mod.c2 = $mod.c;',
  7980. '};',
  7981. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7982. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7983. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7984. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7985. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7986. '']));
  7987. end;
  7988. procedure TTestModule.TestProcTwoArgs;
  7989. begin
  7990. StartProgram(false);
  7991. Add('procedure Test(a,b: longint);');
  7992. Add('begin');
  7993. Add('end;');
  7994. Add('begin');
  7995. ConvertProgram;
  7996. CheckSource('TestProcTwoArgs',
  7997. LinesToStr([ // statements
  7998. 'this.Test = function (a,b) {',
  7999. '};'
  8000. ]),
  8001. LinesToStr([ // this.$main
  8002. ''
  8003. ]));
  8004. end;
  8005. procedure TTestModule.TestProc_DefaultValue;
  8006. begin
  8007. StartProgram(false);
  8008. Add('procedure p1(i: longint = 1);');
  8009. Add('begin');
  8010. Add('end;');
  8011. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8012. Add('begin');
  8013. Add('end;');
  8014. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8015. Add('begin');
  8016. Add('end;');
  8017. Add('begin');
  8018. Add(' p1;');
  8019. Add(' p1();');
  8020. Add(' p1(11);');
  8021. Add(' p2;');
  8022. Add(' p2();');
  8023. Add(' p2(12);');
  8024. Add(' p2(13,''b'');');
  8025. Add(' p3();');
  8026. ConvertProgram;
  8027. CheckSource('TestProc_DefaultValue',
  8028. LinesToStr([ // statements
  8029. 'this.p1 = function (i) {',
  8030. '};',
  8031. 'this.p2 = function (i,c) {',
  8032. '};',
  8033. 'this.p3 = function (d,b,s) {',
  8034. '};'
  8035. ]),
  8036. LinesToStr([ // this.$main
  8037. ' $mod.p1(1);',
  8038. ' $mod.p1(1);',
  8039. ' $mod.p1(11);',
  8040. ' $mod.p2(1,"a");',
  8041. ' $mod.p2(1,"a");',
  8042. ' $mod.p2(12,"a");',
  8043. ' $mod.p2(13,"b");',
  8044. ' $mod.p3(1.0,false,"abc");'
  8045. ]));
  8046. end;
  8047. procedure TTestModule.TestFunctionInt;
  8048. begin
  8049. StartProgram(false);
  8050. Add('function MyTest(Bar: longint): longint;');
  8051. Add('begin');
  8052. Add(' Result:=2*bar');
  8053. Add('end;');
  8054. Add('begin');
  8055. ConvertProgram;
  8056. CheckSource('TestFunctionInt',
  8057. LinesToStr([ // statements
  8058. 'this.MyTest = function (Bar) {',
  8059. ' var Result = 0;',
  8060. ' Result = 2*Bar;',
  8061. ' return Result;',
  8062. '};'
  8063. ]),
  8064. LinesToStr([ // this.$main
  8065. ''
  8066. ]));
  8067. end;
  8068. procedure TTestModule.TestFunctionString;
  8069. begin
  8070. StartProgram(false);
  8071. Add('function Test(Bar: string): string;');
  8072. Add('begin');
  8073. Add(' Result:=bar+BAR');
  8074. Add('end;');
  8075. Add('begin');
  8076. ConvertProgram;
  8077. CheckSource('TestFunctionString',
  8078. LinesToStr([ // statements
  8079. 'this.Test = function (Bar) {',
  8080. ' var Result = "";',
  8081. ' Result = Bar+Bar;',
  8082. ' return Result;',
  8083. '};'
  8084. ]),
  8085. LinesToStr([ // this.$main
  8086. ''
  8087. ]));
  8088. end;
  8089. procedure TTestModule.TestIfThen;
  8090. begin
  8091. StartProgram(false);
  8092. Add([
  8093. 'var b: boolean;',
  8094. 'begin',
  8095. ' if b then ;',
  8096. ' if b then else ;']);
  8097. ConvertProgram;
  8098. CheckSource('TestIfThen',
  8099. LinesToStr([ // statements
  8100. 'this.b = false;',
  8101. '']),
  8102. LinesToStr([ // this.$main
  8103. 'if ($mod.b) ;',
  8104. 'if ($mod.b) ;',
  8105. '']));
  8106. end;
  8107. procedure TTestModule.TestForLoop;
  8108. begin
  8109. StartProgram(false);
  8110. Add('var');
  8111. Add(' vI, vJ, vN: longint;');
  8112. Add('begin');
  8113. Add(' VJ:=0;');
  8114. Add(' VN:=3;');
  8115. Add(' for VI:=1 to VN do');
  8116. Add(' begin');
  8117. Add(' VJ:=VJ+VI;');
  8118. Add(' end;');
  8119. ConvertProgram;
  8120. CheckSource('TestForLoop',
  8121. LinesToStr([ // statements
  8122. 'this.vI = 0;',
  8123. 'this.vJ = 0;',
  8124. 'this.vN = 0;'
  8125. ]),
  8126. LinesToStr([ // this.$main
  8127. ' $mod.vJ = 0;',
  8128. ' $mod.vN = 3;',
  8129. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8130. ' $mod.vI = $l;',
  8131. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8132. ' };',
  8133. '']));
  8134. end;
  8135. procedure TTestModule.TestForLoopInsideFunction;
  8136. begin
  8137. StartProgram(false);
  8138. Add('function SumNumbers(Count: longint): longint;');
  8139. Add('var');
  8140. Add(' vI, vJ: longint;');
  8141. Add('begin');
  8142. Add(' vj:=0;');
  8143. Add(' for vi:=1 to count do');
  8144. Add(' begin');
  8145. Add(' vj:=vj+vi;');
  8146. Add(' end;');
  8147. Add('end;');
  8148. Add('begin');
  8149. Add(' sumnumbers(3);');
  8150. ConvertProgram;
  8151. CheckSource('TestForLoopInsideFunction',
  8152. LinesToStr([ // statements
  8153. 'this.SumNumbers = function (Count) {',
  8154. ' var Result = 0;',
  8155. ' var vI = 0;',
  8156. ' var vJ = 0;',
  8157. ' vJ = 0;',
  8158. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8159. ' vI = $l;',
  8160. ' vJ = vJ + vI;',
  8161. ' };',
  8162. ' return Result;',
  8163. '};'
  8164. ]),
  8165. LinesToStr([ // $mod.$main
  8166. ' $mod.SumNumbers(3);'
  8167. ]));
  8168. end;
  8169. procedure TTestModule.TestForLoop_ReadVarAfter;
  8170. begin
  8171. StartProgram(false);
  8172. Add('var');
  8173. Add(' vI: longint;');
  8174. Add('begin');
  8175. Add(' for vi:=1 to 2 do ;');
  8176. Add(' if vi=3 then ;');
  8177. ConvertProgram;
  8178. CheckSource('TestForLoop',
  8179. LinesToStr([ // statements
  8180. 'this.vI = 0;'
  8181. ]),
  8182. LinesToStr([ // this.$main
  8183. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8184. ' if ($mod.vI===3) ;'
  8185. ]));
  8186. end;
  8187. procedure TTestModule.TestForLoop_Nested;
  8188. begin
  8189. StartProgram(false);
  8190. Add('function SumNumbers(Count: longint): longint;');
  8191. Add('var');
  8192. Add(' vI, vJ, vK: longint;');
  8193. Add('begin');
  8194. Add(' VK:=0;');
  8195. Add(' for VI:=1 to count do');
  8196. Add(' begin');
  8197. Add(' for vj:=1 to vi do');
  8198. Add(' begin');
  8199. Add(' vk:=VK+VI;');
  8200. Add(' end;');
  8201. Add(' end;');
  8202. Add('end;');
  8203. Add('begin');
  8204. Add(' sumnumbers(3);');
  8205. ConvertProgram;
  8206. CheckSource('TestForLoopInFunction',
  8207. LinesToStr([ // statements
  8208. 'this.SumNumbers = function (Count) {',
  8209. ' var Result = 0;',
  8210. ' var vI = 0;',
  8211. ' var vJ = 0;',
  8212. ' var vK = 0;',
  8213. ' vK = 0;',
  8214. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8215. ' vI = $l;',
  8216. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8217. ' vJ = $l1;',
  8218. ' vK = vK + vI;',
  8219. ' };',
  8220. ' };',
  8221. ' return Result;',
  8222. '};'
  8223. ]),
  8224. LinesToStr([ // $mod.$main
  8225. ' $mod.SumNumbers(3);'
  8226. ]));
  8227. end;
  8228. procedure TTestModule.TestRepeatUntil;
  8229. begin
  8230. StartProgram(false);
  8231. Add('var');
  8232. Add(' vI, vJ, vN: longint;');
  8233. Add('begin');
  8234. Add(' vn:=3;');
  8235. Add(' vj:=0;');
  8236. Add(' VI:=0;');
  8237. Add(' repeat');
  8238. Add(' VI:=vi+1;');
  8239. Add(' vj:=VJ+vI;');
  8240. Add(' until vi>=vn');
  8241. ConvertProgram;
  8242. CheckSource('TestRepeatUntil',
  8243. LinesToStr([ // statements
  8244. 'this.vI = 0;',
  8245. 'this.vJ = 0;',
  8246. 'this.vN = 0;'
  8247. ]),
  8248. LinesToStr([ // $mod.$main
  8249. ' $mod.vN = 3;',
  8250. ' $mod.vJ = 0;',
  8251. ' $mod.vI = 0;',
  8252. ' do{',
  8253. ' $mod.vI = $mod.vI + 1;',
  8254. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8255. ' }while(!($mod.vI>=$mod.vN));'
  8256. ]));
  8257. end;
  8258. procedure TTestModule.TestAsmBlock;
  8259. begin
  8260. StartProgram(false);
  8261. Add([
  8262. 'var',
  8263. ' vI: longint;',
  8264. 'begin',
  8265. ' vi:=1;',
  8266. ' asm',
  8267. ' if (vI===1) {',
  8268. ' vI=2;',
  8269. //' console.log(''end;'');', ToDo
  8270. ' }',
  8271. ' if (vI===2){ vI=3; }',
  8272. ' end;',
  8273. ' VI:=4;']);
  8274. ConvertProgram;
  8275. CheckSource('TestAsmBlock',
  8276. LinesToStr([ // statements
  8277. 'this.vI = 0;'
  8278. ]),
  8279. LinesToStr([ // $mod.$main
  8280. '$mod.vI = 1;',
  8281. 'if (vI===1) {',
  8282. ' vI=2;',
  8283. '}',
  8284. 'if (vI===2){ vI=3; }',
  8285. ';',
  8286. '$mod.vI = 4;'
  8287. ]));
  8288. end;
  8289. procedure TTestModule.TestAsmPas_Impl;
  8290. begin
  8291. StartUnit(false);
  8292. Add('interface');
  8293. Add('const cIntf: longint = 1;');
  8294. Add('var vIntf: longint;');
  8295. Add('implementation');
  8296. Add('const cImpl: longint = 2;');
  8297. Add('var vImpl: longint;');
  8298. Add('procedure DoIt;');
  8299. Add('const cLoc: longint = 3;');
  8300. Add('var vLoc: longint;');
  8301. Add('begin;');
  8302. Add(' asm');
  8303. //Add(' pas(vIntf)=pas(cIntf);');
  8304. //Add(' pas(vImpl)=pas(cImpl);');
  8305. //Add(' pas(vLoc)=pas(cLoc);');
  8306. Add(' end;');
  8307. Add('end;');
  8308. ConvertUnit;
  8309. CheckSource('TestAsmPas_Impl',
  8310. LinesToStr([
  8311. 'var $impl = $mod.$impl;',
  8312. 'this.cIntf = 1;',
  8313. 'this.vIntf = 0;',
  8314. '']),
  8315. '', // this.$init
  8316. LinesToStr([ // implementation
  8317. '$impl.cImpl = 2;',
  8318. '$impl.vImpl = 0;',
  8319. 'var cLoc = 3;',
  8320. '$impl.DoIt = function () {',
  8321. ' var vLoc = 0;',
  8322. '};',
  8323. '']) );
  8324. end;
  8325. procedure TTestModule.TestTryFinally;
  8326. begin
  8327. StartProgram(false);
  8328. Add('var i: longint;');
  8329. Add('begin');
  8330. Add(' try');
  8331. Add(' i:=0; i:=2 div i;');
  8332. Add(' finally');
  8333. Add(' i:=3');
  8334. Add(' end;');
  8335. ConvertProgram;
  8336. CheckSource('TestTryFinally',
  8337. LinesToStr([ // statements
  8338. 'this.i = 0;'
  8339. ]),
  8340. LinesToStr([ // $mod.$main
  8341. 'try {',
  8342. ' $mod.i = 0;',
  8343. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8344. '} finally {',
  8345. ' $mod.i = 3;',
  8346. '};'
  8347. ]));
  8348. end;
  8349. procedure TTestModule.TestTryExcept;
  8350. begin
  8351. StartProgram(false);
  8352. Add([
  8353. 'type',
  8354. ' TObject = class end;',
  8355. ' Exception = class Msg: string; end;',
  8356. ' EInvalidCast = class(Exception) end;',
  8357. 'var vI: longint;',
  8358. 'begin',
  8359. ' try',
  8360. ' vi:=1;',
  8361. ' except',
  8362. ' vi:=2',
  8363. ' end;',
  8364. ' try',
  8365. ' vi:=3;',
  8366. ' except',
  8367. ' raise;',
  8368. ' end;',
  8369. ' try',
  8370. ' VI:=4;',
  8371. ' except',
  8372. ' on einvalidcast do',
  8373. ' raise;',
  8374. ' on E: exception do',
  8375. ' if e.msg='''' then',
  8376. ' raise e;',
  8377. ' else',
  8378. ' vi:=5',
  8379. ' end;',
  8380. ' try',
  8381. ' VI:=6;',
  8382. ' except',
  8383. ' on einvalidcast do ;',
  8384. ' end;',
  8385. '']);
  8386. ConvertProgram;
  8387. CheckSource('TestTryExcept',
  8388. LinesToStr([ // statements
  8389. 'rtl.createClass(this, "TObject", null, function () {',
  8390. ' this.$init = function () {',
  8391. ' };',
  8392. ' this.$final = function () {',
  8393. ' };',
  8394. '});',
  8395. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8396. ' this.$init = function () {',
  8397. ' $mod.TObject.$init.call(this);',
  8398. ' this.Msg = "";',
  8399. ' };',
  8400. '});',
  8401. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8402. '});',
  8403. 'this.vI = 0;'
  8404. ]),
  8405. LinesToStr([ // $mod.$main
  8406. 'try {',
  8407. ' $mod.vI = 1;',
  8408. '} catch ($e) {',
  8409. ' $mod.vI = 2;',
  8410. '};',
  8411. 'try {',
  8412. ' $mod.vI = 3;',
  8413. '} catch ($e) {',
  8414. ' throw $e;',
  8415. '};',
  8416. 'try {',
  8417. ' $mod.vI = 4;',
  8418. '} catch ($e) {',
  8419. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8420. ' throw $e',
  8421. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8422. ' var E = $e;',
  8423. ' if (E.Msg === "") throw E;',
  8424. ' } else {',
  8425. ' $mod.vI = 5;',
  8426. ' }',
  8427. '};',
  8428. 'try {',
  8429. ' $mod.vI = 6;',
  8430. '} catch ($e) {',
  8431. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8432. ' } else throw $e',
  8433. '};',
  8434. '']));
  8435. end;
  8436. procedure TTestModule.TestTryExcept_ReservedWords;
  8437. begin
  8438. StartProgram(false);
  8439. Add([
  8440. 'type',
  8441. ' TObject = class end;',
  8442. ' Exception = class',
  8443. ' Symbol: string;',
  8444. ' end;',
  8445. 'var &try: longint;',
  8446. 'begin',
  8447. ' try',
  8448. ' &try:=4;',
  8449. ' except',
  8450. ' on Error: exception do',
  8451. ' if errOR.symBol='''' then',
  8452. ' raise ERRor;',
  8453. ' end;',
  8454. '']);
  8455. ConvertProgram;
  8456. CheckSource('TestTryExcept_ReservedWords',
  8457. LinesToStr([ // statements
  8458. 'rtl.createClass(this, "TObject", null, function () {',
  8459. ' this.$init = function () {',
  8460. ' };',
  8461. ' this.$final = function () {',
  8462. ' };',
  8463. '});',
  8464. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8465. ' this.$init = function () {',
  8466. ' $mod.TObject.$init.call(this);',
  8467. ' this.Symbol = "";',
  8468. ' };',
  8469. '});',
  8470. 'this.Try = 0;',
  8471. '']),
  8472. LinesToStr([ // $mod.$main
  8473. 'try {',
  8474. ' $mod.Try = 4;',
  8475. '} catch ($e) {',
  8476. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8477. ' var error = $e;',
  8478. ' if (error.Symbol === "") throw error;',
  8479. ' } else throw $e',
  8480. '};',
  8481. '']));
  8482. end;
  8483. procedure TTestModule.TestIfThenRaiseElse;
  8484. begin
  8485. StartProgram(false);
  8486. Add([
  8487. 'type',
  8488. ' TObject = class',
  8489. ' constructor Create;',
  8490. ' end;',
  8491. 'constructor TObject.Create;',
  8492. 'begin',
  8493. 'end;',
  8494. 'var b: boolean;',
  8495. 'begin',
  8496. ' if b then',
  8497. ' raise TObject.Create',
  8498. ' else',
  8499. ' b:=false;',
  8500. '']);
  8501. ConvertProgram;
  8502. CheckSource('TestIfThenRaiseElse',
  8503. LinesToStr([ // statements
  8504. 'rtl.createClass(this, "TObject", null, function () {',
  8505. ' this.$init = function () {',
  8506. ' };',
  8507. ' this.$final = function () {',
  8508. ' };',
  8509. ' this.Create = function () {',
  8510. ' return this;',
  8511. ' };',
  8512. '});',
  8513. 'this.b = false;',
  8514. '']),
  8515. LinesToStr([ // $mod.$main
  8516. 'if ($mod.b) {',
  8517. ' throw $mod.TObject.$create("Create")}',
  8518. ' else $mod.b = false;',
  8519. '']));
  8520. end;
  8521. procedure TTestModule.TestCaseOf;
  8522. begin
  8523. StartProgram(false);
  8524. Add([
  8525. 'const e: longint; external name ''$e'';',
  8526. 'var vI: longint;',
  8527. 'begin',
  8528. ' case vi of',
  8529. ' 1: ;',
  8530. ' 2: vi:=3;',
  8531. ' e: ;',
  8532. ' else',
  8533. ' VI:=4',
  8534. ' end;']);
  8535. ConvertProgram;
  8536. CheckSource('TestCaseOf',
  8537. LinesToStr([ // statements
  8538. 'this.vI = 0;'
  8539. ]),
  8540. LinesToStr([ // $mod.$main
  8541. 'var $tmp = $mod.vI;',
  8542. 'if ($tmp === 1) {}',
  8543. 'else if ($tmp === 2) {',
  8544. ' $mod.vI = 3}',
  8545. ' else if ($tmp === $e) {}',
  8546. 'else {',
  8547. ' $mod.vI = 4;',
  8548. '};'
  8549. ]));
  8550. end;
  8551. procedure TTestModule.TestCaseOf_UseSwitch;
  8552. begin
  8553. StartProgram(false);
  8554. Converter.UseSwitchStatement:=true;
  8555. Add('var Vi: longint;');
  8556. Add('begin');
  8557. Add(' case vi of');
  8558. Add(' 1: ;');
  8559. Add(' 2: VI:=3;');
  8560. Add(' else');
  8561. Add(' vi:=4');
  8562. Add(' end;');
  8563. ConvertProgram;
  8564. CheckSource('TestCaseOf_UseSwitch',
  8565. LinesToStr([ // statements
  8566. 'this.Vi = 0;'
  8567. ]),
  8568. LinesToStr([ // $mod.$main
  8569. 'switch ($mod.Vi) {',
  8570. 'case 1:',
  8571. ' break;',
  8572. 'case 2:',
  8573. ' $mod.Vi = 3;',
  8574. ' break;',
  8575. 'default:',
  8576. ' $mod.Vi = 4;',
  8577. '};'
  8578. ]));
  8579. end;
  8580. procedure TTestModule.TestCaseOfNoElse;
  8581. begin
  8582. StartProgram(false);
  8583. Add('var Vi: longint;');
  8584. Add('begin');
  8585. Add(' case vi of');
  8586. Add(' 1: begin vi:=2; VI:=3; end;');
  8587. Add(' end;');
  8588. ConvertProgram;
  8589. CheckSource('TestCaseOfNoElse',
  8590. LinesToStr([ // statements
  8591. 'this.Vi = 0;'
  8592. ]),
  8593. LinesToStr([ // $mod.$main
  8594. 'var $tmp = $mod.Vi;',
  8595. 'if ($tmp === 1) {',
  8596. ' $mod.Vi = 2;',
  8597. ' $mod.Vi = 3;',
  8598. '};'
  8599. ]));
  8600. end;
  8601. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8602. begin
  8603. StartProgram(false);
  8604. Converter.UseSwitchStatement:=true;
  8605. Add('var vI: longint;');
  8606. Add('begin');
  8607. Add(' case vi of');
  8608. Add(' 1: begin VI:=2; vi:=3; end;');
  8609. Add(' end;');
  8610. ConvertProgram;
  8611. CheckSource('TestCaseOfNoElse_UseSwitch',
  8612. LinesToStr([ // statements
  8613. 'this.vI = 0;'
  8614. ]),
  8615. LinesToStr([ // $mod.$main
  8616. 'switch ($mod.vI) {',
  8617. 'case 1:',
  8618. ' $mod.vI = 2;',
  8619. ' $mod.vI = 3;',
  8620. ' break;',
  8621. '};'
  8622. ]));
  8623. end;
  8624. procedure TTestModule.TestCaseOfRange;
  8625. begin
  8626. StartProgram(false);
  8627. Add('var vI: longint;');
  8628. Add('begin');
  8629. Add(' case vi of');
  8630. Add(' 1..3: vi:=14;');
  8631. Add(' 4,5: vi:=16;');
  8632. Add(' 6..7,9..10: ;');
  8633. Add(' else ;');
  8634. Add(' end;');
  8635. ConvertProgram;
  8636. CheckSource('TestCaseOfRange',
  8637. LinesToStr([ // statements
  8638. 'this.vI = 0;'
  8639. ]),
  8640. LinesToStr([ // $mod.$main
  8641. 'var $tmp = $mod.vI;',
  8642. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8643. ' $mod.vI = 14',
  8644. '} else if (($tmp === 4) || ($tmp === 5)){',
  8645. ' $mod.vI = 16',
  8646. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8647. ]));
  8648. end;
  8649. procedure TTestModule.TestCaseOfString;
  8650. begin
  8651. StartProgram(false);
  8652. Add([
  8653. 'var s,h: string;',
  8654. 'begin',
  8655. ' case s of',
  8656. ' ''foo'': s:=h;',
  8657. ' ''a''..''z'': h:=s;',
  8658. ' ''ў'', ''ё'': ;',
  8659. ' ''Б''..''Я'': ;',
  8660. ' end;',
  8661. '']);
  8662. ConvertProgram;
  8663. CheckSource('TestCaseOfString',
  8664. LinesToStr([ // statements
  8665. 'this.s = "";',
  8666. 'this.h = "";',
  8667. '']),
  8668. LinesToStr([ // $mod.$main
  8669. 'var $tmp = $mod.s;',
  8670. 'if ($tmp === "foo") {',
  8671. ' $mod.s = $mod.h}',
  8672. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8673. ' $mod.h = $mod.s}',
  8674. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8675. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8676. '']));
  8677. end;
  8678. procedure TTestModule.TestCaseOfChar;
  8679. begin
  8680. StartProgram(false);
  8681. Add([
  8682. 'var s,h: char;',
  8683. 'begin',
  8684. ' case s of',
  8685. ' ''a''..''z'': h:=s;',
  8686. ' ''ä'': ;',
  8687. ' ''ў'', ''ё'': ;',
  8688. ' ''Б''..''Я'': ;',
  8689. ' end;',
  8690. '']);
  8691. ConvertProgram;
  8692. CheckSource('TestCaseOfString',
  8693. LinesToStr([ // statements
  8694. 'this.s = "";',
  8695. 'this.h = "";',
  8696. '']),
  8697. LinesToStr([ // $mod.$main
  8698. 'var $tmp = $mod.s;',
  8699. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8700. ' $mod.h = $mod.s}',
  8701. ' else if ($tmp === "ä") {}',
  8702. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8703. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8704. '']));
  8705. end;
  8706. procedure TTestModule.TestCaseOfExternalClassConst;
  8707. begin
  8708. StartProgram(false);
  8709. Add([
  8710. '{$modeswitch externalclass}',
  8711. 'type',
  8712. ' TBird = class external name ''Bird''',
  8713. ' const e: longint;',
  8714. ' end;',
  8715. 'var vI: longint;',
  8716. 'begin',
  8717. ' case vi of',
  8718. ' 1: vi:=3;',
  8719. ' TBird.e: ;',
  8720. ' end;']);
  8721. ConvertProgram;
  8722. CheckSource('TestCaseOfExternalClassConst',
  8723. LinesToStr([ // statements
  8724. 'this.vI = 0;'
  8725. ]),
  8726. LinesToStr([ // $mod.$main
  8727. 'var $tmp = $mod.vI;',
  8728. 'if ($tmp === 1) {',
  8729. ' $mod.vI = 3}',
  8730. ' else if ($tmp === Bird.e) ;'
  8731. ]));
  8732. end;
  8733. procedure TTestModule.TestDebugger;
  8734. begin
  8735. StartProgram(false);
  8736. Add([
  8737. 'procedure DoIt;',
  8738. 'begin',
  8739. ' deBugger;',
  8740. ' DeBugger();',
  8741. 'end;',
  8742. 'begin',
  8743. ' Debugger;']);
  8744. ConvertProgram;
  8745. CheckSource('TestDebugger',
  8746. LinesToStr([ // statements
  8747. 'this.DoIt = function () {',
  8748. ' debugger;',
  8749. ' debugger;',
  8750. '};',
  8751. '']),
  8752. LinesToStr([ // $mod.$main
  8753. 'debugger;',
  8754. '']));
  8755. end;
  8756. procedure TTestModule.TestArray_Dynamic;
  8757. begin
  8758. StartProgram(false);
  8759. Add([
  8760. 'type',
  8761. ' TArrayInt = array of longint;',
  8762. 'var',
  8763. ' Arr: TArrayInt;',
  8764. ' i: longint;',
  8765. ' b: boolean;',
  8766. 'begin',
  8767. ' SetLength(arr,3);',
  8768. ' arr[0]:=4;',
  8769. ' arr[1]:=length(arr)+arr[0];',
  8770. ' arr[i]:=5;',
  8771. ' arr[arr[i]]:=arr[6];',
  8772. ' i:=low(arr);',
  8773. ' i:=high(arr);',
  8774. ' b:=Assigned(arr);',
  8775. ' Arr:=default(TArrayInt);']);
  8776. ConvertProgram;
  8777. CheckSource('TestArray_Dynamic',
  8778. LinesToStr([ // statements
  8779. 'this.Arr = [];',
  8780. 'this.i = 0;',
  8781. 'this.b = false;'
  8782. ]),
  8783. LinesToStr([ // $mod.$main
  8784. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8785. '$mod.Arr[0] = 4;',
  8786. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8787. '$mod.Arr[$mod.i] = 5;',
  8788. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8789. '$mod.i = 0;',
  8790. '$mod.i = rtl.length($mod.Arr) - 1;',
  8791. '$mod.b = rtl.length($mod.Arr) > 0;',
  8792. '$mod.Arr = [];',
  8793. '']));
  8794. end;
  8795. procedure TTestModule.TestArray_Dynamic_Nil;
  8796. begin
  8797. StartProgram(false);
  8798. Add('type');
  8799. Add(' TArrayInt = array of longint;');
  8800. Add('var');
  8801. Add(' Arr: TArrayInt;');
  8802. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8803. Add('begin');
  8804. Add(' arr:=nil;');
  8805. Add(' if arr=nil then;');
  8806. Add(' if nil=arr then;');
  8807. Add(' if arr<>nil then;');
  8808. Add(' if nil<>arr then;');
  8809. Add(' DoIt(nil,nil);');
  8810. ConvertProgram;
  8811. CheckSource('TestArray_Dynamic',
  8812. LinesToStr([ // statements
  8813. 'this.Arr = [];',
  8814. 'this.DoIt = function(i,j){',
  8815. '};'
  8816. ]),
  8817. LinesToStr([ // $mod.$main
  8818. '$mod.Arr = [];',
  8819. 'if (rtl.length($mod.Arr) === 0) ;',
  8820. 'if (rtl.length($mod.Arr) === 0) ;',
  8821. 'if (rtl.length($mod.Arr) > 0) ;',
  8822. 'if (rtl.length($mod.Arr) > 0) ;',
  8823. '$mod.DoIt([],[]);',
  8824. '']));
  8825. end;
  8826. procedure TTestModule.TestArray_DynMultiDimensional;
  8827. begin
  8828. StartProgram(false);
  8829. Add([
  8830. 'type',
  8831. ' TArrayInt = array of longint;',
  8832. ' TArrayArrayInt = array of TArrayInt;',
  8833. 'var',
  8834. ' Arr: TArrayInt;',
  8835. ' Arr2: TArrayArrayInt;',
  8836. ' i: longint;',
  8837. 'begin',
  8838. ' arr2:=nil;',
  8839. ' if arr2=nil then;',
  8840. ' if nil=arr2 then;',
  8841. ' i:=low(arr2);',
  8842. ' i:=low(arr2[1]);',
  8843. ' i:=high(arr2);',
  8844. ' i:=high(arr2[2]);',
  8845. ' arr2[3]:=arr;',
  8846. ' arr2[4][5]:=i;',
  8847. ' i:=arr2[6][7];',
  8848. ' arr2[8,9]:=i;',
  8849. ' i:=arr2[10,11];',
  8850. ' SetLength(arr2,14);',
  8851. ' SetLength(arr2[15],16);']);
  8852. ConvertProgram;
  8853. CheckSource('TestArray_Dynamic',
  8854. LinesToStr([ // statements
  8855. 'this.Arr = [];',
  8856. 'this.Arr2 = [];',
  8857. 'this.i = 0;'
  8858. ]),
  8859. LinesToStr([ // $mod.$main
  8860. '$mod.Arr2 = [];',
  8861. 'if (rtl.length($mod.Arr2) === 0) ;',
  8862. 'if (rtl.length($mod.Arr2) === 0) ;',
  8863. '$mod.i = 0;',
  8864. '$mod.i = 0;',
  8865. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8866. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8867. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8868. '$mod.Arr2[4][5] = $mod.i;',
  8869. '$mod.i = $mod.Arr2[6][7];',
  8870. '$mod.Arr2[8][9] = $mod.i;',
  8871. '$mod.i = $mod.Arr2[10][11];',
  8872. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8873. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8874. '']));
  8875. end;
  8876. procedure TTestModule.TestArray_DynamicAssign;
  8877. begin
  8878. StartProgram(false);
  8879. Add([
  8880. 'type',
  8881. ' TArrayInt = array of longint;',
  8882. ' TArrayArrayInt = array of TArrayInt;',
  8883. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8884. 'begin',
  8885. 'end;',
  8886. 'procedure Fly(var a: TArrayInt);',
  8887. 'begin',
  8888. 'end;',
  8889. 'var',
  8890. ' Arr: TArrayInt;',
  8891. ' Arr2: TArrayArrayInt;',
  8892. 'begin',
  8893. ' arr:=nil;',
  8894. ' arr2:=nil;',
  8895. ' arr2[1]:=nil;',
  8896. ' arr2[2]:=arr;',
  8897. ' Run(arr,arr,arr);',
  8898. ' Fly(arr);',
  8899. ' Run(arr2[4],arr2[5],arr2[6]);',
  8900. ' Fly(arr2[7]);',
  8901. '']);
  8902. ConvertProgram;
  8903. CheckSource('TestArray_DynamicAssign',
  8904. LinesToStr([ // statements
  8905. 'this.Run = function (a, b, c) {',
  8906. '};',
  8907. 'this.Fly = function (a) {',
  8908. '};',
  8909. 'this.Arr = [];',
  8910. 'this.Arr2 = [];',
  8911. '']),
  8912. LinesToStr([ // $mod.$main
  8913. '$mod.Arr = [];',
  8914. '$mod.Arr2 = [];',
  8915. '$mod.Arr2[1] = [];',
  8916. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8917. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8918. '$mod.Fly({',
  8919. ' p: $mod,',
  8920. ' get: function () {',
  8921. ' return this.p.Arr;',
  8922. ' },',
  8923. ' set: function (v) {',
  8924. ' this.p.Arr = v;',
  8925. ' }',
  8926. '});',
  8927. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8928. '$mod.Fly({',
  8929. ' a: 7,',
  8930. ' p: $mod.Arr2,',
  8931. ' get: function () {',
  8932. ' return this.p[this.a];',
  8933. ' },',
  8934. ' set: function (v) {',
  8935. ' this.p[this.a] = v;',
  8936. ' }',
  8937. '});',
  8938. '']));
  8939. end;
  8940. procedure TTestModule.TestArray_StaticInt;
  8941. begin
  8942. StartProgram(false);
  8943. Add('type');
  8944. Add(' TArrayInt = array[2..4] of longint;');
  8945. Add('var');
  8946. Add(' Arr: TArrayInt;');
  8947. Add(' Arr2: TArrayInt = (5,6,7);');
  8948. Add(' i: longint;');
  8949. Add(' b: boolean;');
  8950. Add('begin');
  8951. Add(' arr[2]:=4;');
  8952. Add(' arr[3]:=arr[2]+arr[3];');
  8953. Add(' arr[i]:=5;');
  8954. Add(' arr[arr[i]]:=arr[high(arr)];');
  8955. Add(' i:=low(arr);');
  8956. Add(' i:=high(arr);');
  8957. Add(' b:=arr[2]=arr[3];');
  8958. Add(' arr:=default(TArrayInt);');
  8959. ConvertProgram;
  8960. CheckSource('TestArray_StaticInt',
  8961. LinesToStr([ // statements
  8962. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8963. 'this.Arr2 = [5, 6, 7];',
  8964. 'this.i = 0;',
  8965. 'this.b = false;'
  8966. ]),
  8967. LinesToStr([ // $mod.$main
  8968. '$mod.Arr[0] = 4;',
  8969. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8970. '$mod.Arr[$mod.i-2] = 5;',
  8971. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8972. '$mod.i = 2;',
  8973. '$mod.i = 4;',
  8974. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8975. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8976. '']));
  8977. end;
  8978. procedure TTestModule.TestArray_StaticBool;
  8979. begin
  8980. StartProgram(false);
  8981. Add('type');
  8982. Add(' TBools = array[boolean] of boolean;');
  8983. Add(' TBool2 = array[true..true] of boolean;');
  8984. Add('var');
  8985. Add(' Arr: TBools;');
  8986. Add(' Arr2: TBool2;');
  8987. Add(' Arr3: TBools = (true,false);');
  8988. Add(' b: boolean;');
  8989. Add('begin');
  8990. Add(' b:=low(arr);');
  8991. Add(' b:=high(arr);');
  8992. Add(' arr[true]:=false;');
  8993. Add(' arr[false]:=arr[b] or arr[true];');
  8994. Add(' arr[b]:=true;');
  8995. Add(' arr[arr[b]]:=arr[high(arr)];');
  8996. Add(' b:=arr[false]=arr[true];');
  8997. Add(' b:=low(arr2);');
  8998. Add(' b:=high(arr2);');
  8999. Add(' arr2[true]:=true;');
  9000. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9001. Add(' arr2[b]:=false;');
  9002. ConvertProgram;
  9003. CheckSource('TestArray_StaticBool',
  9004. LinesToStr([ // statements
  9005. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9006. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9007. 'this.Arr3 = [true, false];',
  9008. 'this.b = false;'
  9009. ]),
  9010. LinesToStr([ // $mod.$main
  9011. '$mod.b = false;',
  9012. '$mod.b = true;',
  9013. '$mod.Arr[1] = false;',
  9014. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9015. '$mod.Arr[+$mod.b] = true;',
  9016. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9017. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9018. '$mod.b = true;',
  9019. '$mod.b = true;',
  9020. '$mod.Arr2[0] = true;',
  9021. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9022. '$mod.Arr2[1-$mod.b] = false;',
  9023. '']));
  9024. end;
  9025. procedure TTestModule.TestArray_StaticChar;
  9026. begin
  9027. StartProgram(false);
  9028. Add([
  9029. 'type',
  9030. ' TChars = array[char] of char;',
  9031. ' TChars2 = array[''a''..''z''] of char;',
  9032. 'var',
  9033. ' Arr: TChars;',
  9034. ' Arr2: TChars2;',
  9035. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9036. ' Arr4: array[11..13] of char = ''pas'';',
  9037. ' Arr5: array[21..22] of char = ''äö'';',
  9038. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9039. ' c: char;',
  9040. ' b: boolean;',
  9041. 'begin',
  9042. ' c:=low(arr);',
  9043. ' c:=high(arr);',
  9044. ' arr[''B'']:=''a'';',
  9045. ' arr[''D'']:=arr[c];',
  9046. ' arr[c]:=arr[''d''];',
  9047. ' arr[arr[c]]:=arr[high(arr)];',
  9048. ' b:=arr[low(arr)]=arr[''e''];',
  9049. ' c:=low(arr2);',
  9050. ' c:=high(arr2);',
  9051. ' arr2[''b'']:=''f'';',
  9052. ' arr2[''a'']:=arr2[c];',
  9053. ' arr2[c]:=arr2[''g''];']);
  9054. ConvertProgram;
  9055. CheckSource('TestArray_StaticChar',
  9056. LinesToStr([ // statements
  9057. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9058. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9059. 'this.Arr3 = ["p", "a", "s"];',
  9060. 'this.Arr4 = ["p", "a", "s"];',
  9061. 'this.Arr5 = ["ä", "ö"];',
  9062. 'this.Arr6 = ["ä", "ö"];',
  9063. 'this.c = "";',
  9064. 'this.b = false;',
  9065. '']),
  9066. LinesToStr([ // $mod.$main
  9067. '$mod.c = "\x00";',
  9068. '$mod.c = "\uFFFF";',
  9069. '$mod.Arr[66] = "a";',
  9070. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9071. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9072. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9073. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9074. '$mod.c = "a";',
  9075. '$mod.c = "z";',
  9076. '$mod.Arr2[1] = "f";',
  9077. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9078. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9079. '']));
  9080. end;
  9081. procedure TTestModule.TestArray_StaticMultiDim;
  9082. begin
  9083. StartProgram(false);
  9084. Add([
  9085. 'type',
  9086. ' TArrayInt = array[1..3] of longint;',
  9087. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9088. 'var',
  9089. ' Arr: TArrayInt;',
  9090. ' Arr2: TArrayArrayInt;',
  9091. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9092. ' i: longint;',
  9093. 'begin',
  9094. ' i:=low(arr);',
  9095. ' i:=low(arr2);',
  9096. ' i:=low(arr2[5]);',
  9097. ' i:=high(arr);',
  9098. ' i:=high(arr2);',
  9099. ' i:=high(arr2[6]);',
  9100. ' arr2[5]:=arr;',
  9101. ' arr2[6][2]:=i;',
  9102. ' i:=arr2[6][3];',
  9103. ' arr2[6,3]:=i;',
  9104. ' i:=arr2[5,2];',
  9105. ' arr2:=arr2;',// clone multi dim static array
  9106. //' arr3:=arr3;',// clone anonymous multi dim static array
  9107. '']);
  9108. ConvertProgram;
  9109. CheckSource('TestArray_StaticMultiDim',
  9110. LinesToStr([ // statements
  9111. 'this.TArrayArrayInt$clone = function (a) {',
  9112. ' var r = [];',
  9113. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9114. ' return r;',
  9115. '};',
  9116. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9117. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9118. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9119. 'this.i = 0;'
  9120. ]),
  9121. LinesToStr([ // $mod.$main
  9122. '$mod.i = 1;',
  9123. '$mod.i = 5;',
  9124. '$mod.i = 1;',
  9125. '$mod.i = 3;',
  9126. '$mod.i = 6;',
  9127. '$mod.i = 3;',
  9128. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9129. '$mod.Arr2[1][1] = $mod.i;',
  9130. '$mod.i = $mod.Arr2[1][2];',
  9131. '$mod.Arr2[1][2] = $mod.i;',
  9132. '$mod.i = $mod.Arr2[0][1];',
  9133. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9134. '']));
  9135. end;
  9136. procedure TTestModule.TestArray_StaticInFunction;
  9137. begin
  9138. StartProgram(false);
  9139. Add([
  9140. 'const TArrayInt = 3;',
  9141. 'const TArrayArrayInt = 4;',
  9142. 'procedure DoIt;',
  9143. 'type',
  9144. ' TArrayInt = array[1..3] of longint;',
  9145. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9146. 'var',
  9147. ' Arr: TArrayInt;',
  9148. ' Arr2: TArrayArrayInt;',
  9149. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9150. ' i: longint;',
  9151. 'begin',
  9152. ' arr2[5]:=arr;',
  9153. ' arr2:=arr2;',// clone multi dim static array
  9154. 'end;',
  9155. 'begin',
  9156. '']);
  9157. ConvertProgram;
  9158. CheckSource('TestArray_StaticInFunction',
  9159. LinesToStr([ // statements
  9160. 'this.TArrayInt = 3;',
  9161. 'this.TArrayArrayInt = 4;',
  9162. 'var TArrayArrayInt$1$clone = function (a) {',
  9163. ' var r = [];',
  9164. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9165. ' return r;',
  9166. '};',
  9167. 'this.DoIt = function () {',
  9168. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9169. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9170. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9171. ' var i = 0;',
  9172. ' Arr2[0] = Arr.slice(0);',
  9173. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9174. '};',
  9175. '']),
  9176. LinesToStr([ // $mod.$main
  9177. '']));
  9178. end;
  9179. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9180. begin
  9181. StartProgram(false);
  9182. Add([
  9183. 'type',
  9184. ' TArrayInt = array[1..3,1..2] of longint;',
  9185. 'var',
  9186. ' a,b: TArrayInt;',
  9187. 'begin',
  9188. ' if a=b then ;',
  9189. '']);
  9190. SetExpectedPasResolverError('compare static array is not supported',
  9191. nXIsNotSupported);
  9192. ConvertProgram;
  9193. end;
  9194. procedure TTestModule.TestArrayOfRecord;
  9195. begin
  9196. StartProgram(false);
  9197. Add([
  9198. 'type',
  9199. ' TRec = record',
  9200. ' Int: longint;',
  9201. ' end;',
  9202. ' TArrayRec = array of TRec;',
  9203. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9204. 'begin',
  9205. 'end;',
  9206. 'var',
  9207. ' Arr: TArrayRec;',
  9208. ' r: TRec;',
  9209. ' i: longint;',
  9210. 'begin',
  9211. ' SetLength(arr,3);',
  9212. ' arr[0].int:=4;',
  9213. ' arr[1].int:=length(arr)+arr[2].int;',
  9214. ' arr[arr[i].int].int:=arr[5].int;',
  9215. ' arr[7]:=r;',
  9216. ' r:=arr[8];',
  9217. ' i:=low(arr);',
  9218. ' i:=high(arr);',
  9219. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9220. ConvertProgram;
  9221. CheckSource('TestArrayOfRecord',
  9222. LinesToStr([ // statements
  9223. 'rtl.recNewT(this, "TRec", function () {',
  9224. ' this.Int = 0;',
  9225. ' this.$eq = function (b) {',
  9226. ' return this.Int === b.Int;',
  9227. ' };',
  9228. ' this.$assign = function (s) {',
  9229. ' this.Int = s.Int;',
  9230. ' return this;',
  9231. ' };',
  9232. '});',
  9233. 'this.DoIt = function (vd, vc, vv) {',
  9234. '};',
  9235. 'this.Arr = [];',
  9236. 'this.r = this.TRec.$new();',
  9237. 'this.i = 0;'
  9238. ]),
  9239. LinesToStr([ // $mod.$main
  9240. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9241. '$mod.Arr[0].Int = 4;',
  9242. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9243. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9244. '$mod.Arr[7].$assign($mod.r);',
  9245. '$mod.r.$assign($mod.Arr[8]);',
  9246. '$mod.i = 0;',
  9247. '$mod.i = rtl.length($mod.Arr)-1;',
  9248. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9249. '']));
  9250. end;
  9251. procedure TTestModule.TestArray_StaticRecord;
  9252. begin
  9253. StartProgram(false);
  9254. Add([
  9255. 'type',
  9256. ' TRec = record',
  9257. ' Int: longint;',
  9258. ' end;',
  9259. ' TArrayRec = array[1..2] of TRec;',
  9260. 'var',
  9261. ' Arr: TArrayRec;',
  9262. 'begin',
  9263. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9264. '']);
  9265. ConvertProgram;
  9266. CheckSource('TestArray_StaticRecord',
  9267. LinesToStr([ // statements
  9268. 'rtl.recNewT(this, "TRec", function () {',
  9269. ' this.Int = 0;',
  9270. ' this.$eq = function (b) {',
  9271. ' return this.Int === b.Int;',
  9272. ' };',
  9273. ' this.$assign = function (s) {',
  9274. ' this.Int = s.Int;',
  9275. ' return this;',
  9276. ' };',
  9277. '});',
  9278. 'this.TArrayRec$clone = function (a) {',
  9279. ' var r = [];',
  9280. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9281. ' return r;',
  9282. '};',
  9283. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9284. '']),
  9285. LinesToStr([ // $mod.$main
  9286. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9287. end;
  9288. procedure TTestModule.TestArrayOfSet;
  9289. begin
  9290. StartProgram(false);
  9291. Add([
  9292. 'type',
  9293. ' TFlag = (big,small);',
  9294. ' TSetOfFlag = set of tflag;',
  9295. ' TArrayFlag = array of TSetOfFlag;',
  9296. 'procedure DoIt(const a: Tarrayflag);',
  9297. 'begin',
  9298. 'end;',
  9299. 'var',
  9300. ' f: TFlag;',
  9301. ' s: TSetOfFlag;',
  9302. ' Arr: TArrayFlag;',
  9303. ' i: longint;',
  9304. 'begin',
  9305. ' SetLength(arr,3);',
  9306. ' arr[0]:=s;',
  9307. ' arr[1]:=[big];',
  9308. ' arr[2]:=[big]+s;',
  9309. ' arr[3]:=s+[big];',
  9310. ' arr[4]:=arr[5];',
  9311. ' s:=arr[6];',
  9312. ' i:=low(arr);',
  9313. ' i:=high(arr);',
  9314. ' DoIt(arr);',
  9315. ' DoIt([s]);',
  9316. ' DoIt([[],s]);',
  9317. ' DoIt([s,[]]);',
  9318. '']);
  9319. ConvertProgram;
  9320. CheckSource('TestArrayOfSet',
  9321. LinesToStr([ // statements
  9322. 'this.TFlag = {',
  9323. ' "0": "big",',
  9324. ' big: 0,',
  9325. ' "1": "small",',
  9326. ' small: 1',
  9327. '};',
  9328. 'this.DoIt = function (a) {',
  9329. '};',
  9330. 'this.f = 0;',
  9331. 'this.s = {};',
  9332. 'this.Arr = [];',
  9333. 'this.i = 0;',
  9334. '']),
  9335. LinesToStr([ // $mod.$main
  9336. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9337. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9338. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9339. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9340. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9341. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9342. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9343. '$mod.i = 0;',
  9344. '$mod.i = rtl.length($mod.Arr) - 1;',
  9345. '$mod.DoIt($mod.Arr);',
  9346. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9347. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9348. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9349. '']));
  9350. end;
  9351. procedure TTestModule.TestArray_DynAsParam;
  9352. begin
  9353. StartProgram(false);
  9354. Add([
  9355. 'type integer = longint;',
  9356. 'type TArrInt = array of integer;',
  9357. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9358. 'var vJ: TArrInt;',
  9359. 'begin',
  9360. ' vg:=vg;',
  9361. ' vj:=vh;',
  9362. ' vi:=vi;',
  9363. ' doit(vg,vg,vg);',
  9364. ' doit(vh,vh,vj);',
  9365. ' doit(vi,vi,vi);',
  9366. ' doit(vj,vj,vj);',
  9367. 'end;',
  9368. 'var i: TArrInt;',
  9369. 'begin',
  9370. ' doit(i,i,i);']);
  9371. ConvertProgram;
  9372. CheckSource('TestArray_DynAsParams',
  9373. LinesToStr([ // statements
  9374. 'this.DoIt = function (vG,vH,vI) {',
  9375. ' var vJ = [];',
  9376. ' vG = rtl.arrayRef(vG);',
  9377. ' vJ = rtl.arrayRef(vH);',
  9378. ' vI.set(rtl.arrayRef(vI.get()));',
  9379. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9380. ' get: function () {',
  9381. ' return vG;',
  9382. ' },',
  9383. ' set: function (v) {',
  9384. ' vG = v;',
  9385. ' }',
  9386. ' });',
  9387. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9388. ' get: function () {',
  9389. ' return vJ;',
  9390. ' },',
  9391. ' set: function (v) {',
  9392. ' vJ = v;',
  9393. ' }',
  9394. ' });',
  9395. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9396. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9397. ' get: function () {',
  9398. ' return vJ;',
  9399. ' },',
  9400. ' set: function (v) {',
  9401. ' vJ = v;',
  9402. ' }',
  9403. ' });',
  9404. '};',
  9405. 'this.i = [];'
  9406. ]),
  9407. LinesToStr([
  9408. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9409. ' p: $mod,',
  9410. ' get: function () {',
  9411. ' return this.p.i;',
  9412. ' },',
  9413. ' set: function (v) {',
  9414. ' this.p.i = v;',
  9415. ' }',
  9416. '});'
  9417. ]));
  9418. end;
  9419. procedure TTestModule.TestArray_StaticAsParam;
  9420. begin
  9421. StartProgram(false);
  9422. Add([
  9423. 'type integer = longint;',
  9424. 'type TArrInt = array[1..2] of integer;',
  9425. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9426. 'var vJ: TArrInt;',
  9427. 'begin',
  9428. ' vg:=vg;',
  9429. ' vj:=vh;',
  9430. ' vi:=vi;',
  9431. ' doit(vg,vg,vg);',
  9432. ' doit(vh,vh,vj);',
  9433. ' doit(vi,vi,vi);',
  9434. ' doit(vj,vj,vj);',
  9435. 'end;',
  9436. 'var i: TArrInt;',
  9437. 'begin',
  9438. ' doit(i,i,i);']);
  9439. ConvertProgram;
  9440. CheckSource('TestArray_StaticAsParams',
  9441. LinesToStr([ // statements
  9442. 'this.DoIt = function (vG,vH,vI) {',
  9443. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9444. ' vG = vG.slice(0);',
  9445. ' vJ = vH.slice(0);',
  9446. ' vI.set(vI.get().slice(0));',
  9447. ' $mod.DoIt(vG.slice(0), vG, {',
  9448. ' get: function () {',
  9449. ' return vG;',
  9450. ' },',
  9451. ' set: function (v) {',
  9452. ' vG = v;',
  9453. ' }',
  9454. ' });',
  9455. ' $mod.DoIt(vH.slice(0), vH, {',
  9456. ' get: function () {',
  9457. ' return vJ;',
  9458. ' },',
  9459. ' set: function (v) {',
  9460. ' vJ = v;',
  9461. ' }',
  9462. ' });',
  9463. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9464. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9465. ' get: function () {',
  9466. ' return vJ;',
  9467. ' },',
  9468. ' set: function (v) {',
  9469. ' vJ = v;',
  9470. ' }',
  9471. ' });',
  9472. '};',
  9473. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9474. ]),
  9475. LinesToStr([
  9476. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9477. ' p: $mod,',
  9478. ' get: function () {',
  9479. ' return this.p.i;',
  9480. ' },',
  9481. ' set: function (v) {',
  9482. ' this.p.i = v;',
  9483. ' }',
  9484. '});'
  9485. ]));
  9486. end;
  9487. procedure TTestModule.TestArrayElement_AsParams;
  9488. begin
  9489. StartProgram(false);
  9490. Add('type integer = longint;');
  9491. Add('type TArrayInt = array of integer;');
  9492. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9493. Add('var vJ: tarrayint;');
  9494. Add('begin');
  9495. Add(' vi:=vi;');
  9496. Add(' doit(vi,vi,vi);');
  9497. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9498. Add('end;');
  9499. Add('var a: TArrayInt;');
  9500. Add('begin');
  9501. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9502. ConvertProgram;
  9503. CheckSource('TestArrayElement_AsParams',
  9504. LinesToStr([ // statements
  9505. 'this.DoIt = function (vG,vH,vI) {',
  9506. ' var vJ = [];',
  9507. ' vI.set(vI.get());',
  9508. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9509. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9510. ' a:1+3,',
  9511. ' p:vJ,',
  9512. ' get: function () {',
  9513. ' return this.p[this.a];',
  9514. ' },',
  9515. ' set: function (v) {',
  9516. ' this.p[this.a] = v;',
  9517. ' }',
  9518. ' });',
  9519. '};',
  9520. 'this.a = [];'
  9521. ]),
  9522. LinesToStr([
  9523. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9524. ' a: 1+6,',
  9525. ' p: $mod.a,',
  9526. ' get: function () {',
  9527. ' return this.p[this.a];',
  9528. ' },',
  9529. ' set: function (v) {',
  9530. ' this.p[this.a] = v;',
  9531. ' }',
  9532. '});'
  9533. ]));
  9534. end;
  9535. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9536. begin
  9537. StartProgram(false);
  9538. Add('type Integer = longint;');
  9539. Add('type TArrayInt = array of integer;');
  9540. Add('function GetArr(vB: integer = 0): tarrayint;');
  9541. Add('begin');
  9542. Add('end;');
  9543. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9544. Add('begin');
  9545. Add('end;');
  9546. Add('begin');
  9547. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9548. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9549. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9550. ConvertProgram;
  9551. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9552. LinesToStr([ // statements
  9553. 'this.GetArr = function (vB) {',
  9554. ' var Result = [];',
  9555. ' return Result;',
  9556. '};',
  9557. 'this.DoIt = function (vG,vH,vI) {',
  9558. '};'
  9559. ]),
  9560. LinesToStr([
  9561. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9562. ' a: 1+3,',
  9563. ' p: $mod.GetArr(0),',
  9564. ' get: function () {',
  9565. ' return this.p[this.a];',
  9566. ' },',
  9567. ' set: function (v) {',
  9568. ' this.p[this.a] = v;',
  9569. ' }',
  9570. '});',
  9571. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9572. ' a: 2+3,',
  9573. ' p: $mod.GetArr(0),',
  9574. ' get: function () {',
  9575. ' return this.p[this.a];',
  9576. ' },',
  9577. ' set: function (v) {',
  9578. ' this.p[this.a] = v;',
  9579. ' }',
  9580. '});',
  9581. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9582. ' a: 3+3,',
  9583. ' p: $mod.GetArr(9),',
  9584. ' get: function () {',
  9585. ' return this.p[this.a];',
  9586. ' },',
  9587. ' set: function (v) {',
  9588. ' this.p[this.a] = v;',
  9589. ' }',
  9590. '});',
  9591. '']));
  9592. end;
  9593. procedure TTestModule.TestArrayEnumTypeRange;
  9594. begin
  9595. StartProgram(false);
  9596. Add([
  9597. 'type',
  9598. ' TEnum = (red,blue);',
  9599. ' TEnumArray = array[TEnum] of longint;',
  9600. 'var',
  9601. ' e: TEnum;',
  9602. ' i: longint;',
  9603. ' a: TEnumArray;',
  9604. ' numbers: TEnumArray = (1,2);',
  9605. ' names: array[TEnum] of string = (''red'',''blue'');',
  9606. 'begin',
  9607. ' e:=low(a);',
  9608. ' e:=high(a);',
  9609. ' i:=a[red];',
  9610. ' a[e]:=a[e];']);
  9611. ConvertProgram;
  9612. CheckSource('TestArrayEnumTypeRange',
  9613. LinesToStr([ // statements
  9614. ' this.TEnum = {',
  9615. ' "0": "red",',
  9616. ' red: 0,',
  9617. ' "1": "blue",',
  9618. ' blue: 1',
  9619. '};',
  9620. 'this.e = 0;',
  9621. 'this.i = 0;',
  9622. 'this.a = rtl.arraySetLength(null,0,2);',
  9623. 'this.numbers = [1, 2];',
  9624. 'this.names = ["red", "blue"];',
  9625. '']),
  9626. LinesToStr([ // $mod.$main
  9627. '$mod.e = $mod.TEnum.red;',
  9628. '$mod.e = $mod.TEnum.blue;',
  9629. '$mod.i = $mod.a[$mod.TEnum.red];',
  9630. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9631. '']));
  9632. end;
  9633. procedure TTestModule.TestArray_SetLengthOutArg;
  9634. begin
  9635. StartProgram(false);
  9636. Add([
  9637. 'type TArrInt = array of longint;',
  9638. 'procedure DoIt(out a: TArrInt);',
  9639. 'begin',
  9640. ' SetLength(a,2);',
  9641. 'end;',
  9642. 'begin',
  9643. '']);
  9644. ConvertProgram;
  9645. CheckSource('TestArray_SetLengthOutArg',
  9646. LinesToStr([ // statements
  9647. 'this.DoIt = function (a) {',
  9648. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9649. '};',
  9650. '']),
  9651. LinesToStr([
  9652. '']));
  9653. end;
  9654. procedure TTestModule.TestArray_SetLengthProperty;
  9655. begin
  9656. StartProgram(false);
  9657. Add('type');
  9658. Add(' TArrInt = array of longint;');
  9659. Add(' TObject = class');
  9660. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9661. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9662. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9663. Add(' end;');
  9664. Add('var Obj: TObject;');
  9665. Add('begin');
  9666. Add(' SetLength(Obj.Colors,2);');
  9667. ConvertProgram;
  9668. CheckSource('TestArray_SetLengthProperty',
  9669. LinesToStr([ // statements
  9670. 'rtl.createClass(this, "TObject", null, function () {',
  9671. ' this.$init = function () {',
  9672. ' };',
  9673. ' this.$final = function () {',
  9674. ' };',
  9675. '});',
  9676. 'this.Obj = null;',
  9677. '']),
  9678. LinesToStr([
  9679. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9680. '']));
  9681. end;
  9682. procedure TTestModule.TestArray_SetLengthMultiDim;
  9683. begin
  9684. StartProgram(false);
  9685. Add([
  9686. 'type',
  9687. ' TArrArrInt = array of array of longint;',
  9688. ' TArrStaInt = array of array[1..2] of longint;',
  9689. 'var',
  9690. ' a: TArrArrInt;',
  9691. ' b: TArrStaInt;',
  9692. 'begin',
  9693. ' SetLength(a,2);',
  9694. ' SetLength(a,3,4);',
  9695. ' SetLength(b,5);',
  9696. '']);
  9697. ConvertProgram;
  9698. CheckSource('TestArray_SetLengthMultiDim',
  9699. LinesToStr([ // statements
  9700. 'this.a = [];',
  9701. 'this.b = [];',
  9702. '']),
  9703. LinesToStr([
  9704. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9705. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9706. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9707. '']));
  9708. end;
  9709. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9710. begin
  9711. StartProgram(false);
  9712. Add([
  9713. 'type',
  9714. ' TStaArr1 = array[1..3] of boolean;',
  9715. //' TStaArr2 = array[5..6] of TStaArr1;',
  9716. ' TDynArr1StaArr1 = array of TStaArr1;',
  9717. //' TDynArr1StaArr2 = array of TStaArr2;',
  9718. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9719. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9720. 'var',
  9721. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9722. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9723. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9724. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9725. 'begin',
  9726. ' SetLength(DynArr1StaArr1,11);',
  9727. ' SetLength(DynArr2StaArr1,12);',
  9728. ' SetLength(DynArr2StaArr1[13],14);',
  9729. ' SetLength(DynArr2StaArr1,15,16);',
  9730. //' SetLength(DynArr1StaArr2,21);',
  9731. //' SetLength(DynArr2StaArr2,22);',
  9732. //' SetLength(DynArr2StaArr2[23],24);',
  9733. //' SetLength(DynArr2StaArr2,25,26);',
  9734. '']);
  9735. ConvertProgram;
  9736. CheckSource('TestArray_DynOfStatic',
  9737. LinesToStr([ // statements
  9738. 'this.DynArr1StaArr1 = [];',
  9739. 'this.DynArr2StaArr1 = [];',
  9740. '']),
  9741. LinesToStr([ // $mod.$main
  9742. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9743. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9744. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9745. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9746. ' $mod.DynArr2StaArr1,',
  9747. ' false,',
  9748. ' 15,',
  9749. ' 16,',
  9750. ' "s",',
  9751. ' 3',
  9752. ');',
  9753. '']));
  9754. end;
  9755. procedure TTestModule.TestArray_OpenArrayOfString;
  9756. begin
  9757. StartProgram(false);
  9758. Add('procedure DoIt(const a: array of String);');
  9759. Add('var');
  9760. Add(' i: longint;');
  9761. Add(' s: string;');
  9762. Add('begin');
  9763. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9764. Add('end;');
  9765. Add('var s: string;');
  9766. Add('begin');
  9767. Add(' DoIt([]);');
  9768. Add(' DoIt([s,''foo'','''',s+s]);');
  9769. ConvertProgram;
  9770. CheckSource('TestArray_OpenArrayOfString',
  9771. LinesToStr([ // statements
  9772. 'this.DoIt = function (a) {',
  9773. ' var i = 0;',
  9774. ' var s = "";',
  9775. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9776. ' i = $l;',
  9777. ' s = a[rtl.length(a) - i - 1];',
  9778. ' };',
  9779. '};',
  9780. 'this.s = "";',
  9781. '']),
  9782. LinesToStr([
  9783. '$mod.DoIt([]);',
  9784. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9785. '']));
  9786. end;
  9787. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9788. begin
  9789. StartProgram(false);
  9790. Add([
  9791. 'type TArr = array of char;',
  9792. 'var',
  9793. ' c: char;',
  9794. ' s: string;',
  9795. ' a: TArr;',
  9796. 'procedure Run(const a: array of char);',
  9797. 'begin',
  9798. ' Run(c);',
  9799. ' Run(s);',
  9800. 'end;',
  9801. 'begin',
  9802. ' a:=c;',
  9803. ' a:=s;',
  9804. ' a:=#13;',
  9805. ' a:=''Foo'';',
  9806. ' Run(c);',
  9807. ' Run(s);',
  9808. '']);
  9809. ConvertProgram;
  9810. CheckSource('TestArray_ArrayOfCharAssignString',
  9811. LinesToStr([ // statements
  9812. 'this.c = "";',
  9813. 'this.s = "";',
  9814. 'this.a = [];',
  9815. 'this.Run = function (a) {',
  9816. ' $mod.Run($mod.c.split(""));',
  9817. ' $mod.Run($mod.s.split(""));',
  9818. '};',
  9819. '']),
  9820. LinesToStr([
  9821. '$mod.a = $mod.c.split("");',
  9822. '$mod.a = $mod.s.split("");',
  9823. '$mod.a = "\r".split("");',
  9824. '$mod.a = "Foo".split("");',
  9825. '$mod.Run($mod.c.split(""));',
  9826. '$mod.Run($mod.s.split(""));',
  9827. '']));
  9828. end;
  9829. procedure TTestModule.TestArray_ConstRef;
  9830. begin
  9831. StartProgram(false);
  9832. Add([
  9833. 'type TArr = array of word;',
  9834. 'procedure Run(constref a: TArr);',
  9835. 'begin',
  9836. 'end;',
  9837. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9838. 'var l: TArr;',
  9839. 'begin',
  9840. ' Run(l);',
  9841. ' Run(a);',
  9842. ' Run(b);',
  9843. ' Run(c);',
  9844. ' Run(d);',
  9845. ' Run(e);',
  9846. 'end;',
  9847. 'begin',
  9848. '']);
  9849. ConvertProgram;
  9850. CheckResolverUnexpectedHints();
  9851. CheckSource('TestArray_ConstRef',
  9852. LinesToStr([ // statements
  9853. 'this.Run = function (a) {',
  9854. '};',
  9855. 'this.Fly = function (a, b, c, d, e) {',
  9856. ' var l = [];',
  9857. ' $mod.Run(l);',
  9858. ' $mod.Run(a);',
  9859. ' $mod.Run(b.get());',
  9860. ' $mod.Run(c.get());',
  9861. ' $mod.Run(d);',
  9862. ' $mod.Run(e);',
  9863. '};',
  9864. '']),
  9865. LinesToStr([
  9866. '']));
  9867. end;
  9868. procedure TTestModule.TestArray_Concat;
  9869. begin
  9870. StartProgram(false);
  9871. Add([
  9872. 'type',
  9873. ' integer = longint;',
  9874. ' TFlag = (big,small);',
  9875. ' TFlags = set of TFlag;',
  9876. ' TRec = record',
  9877. ' i: integer;',
  9878. ' end;',
  9879. ' TArrInt = array of integer;',
  9880. ' TArrRec = array of TRec;',
  9881. ' TArrFlag = array of TFlag;',
  9882. ' TArrSet = array of TFlags;',
  9883. ' TArrJSValue = array of jsvalue;',
  9884. 'var',
  9885. ' ArrInt: tarrint;',
  9886. ' ArrRec: tarrrec;',
  9887. ' ArrFlag: tarrflag;',
  9888. ' ArrSet: tarrset;',
  9889. ' ArrJSValue: tarrjsvalue;',
  9890. 'begin',
  9891. ' arrint:=concat(arrint);',
  9892. ' arrint:=concat(arrint,arrint);',
  9893. ' arrint:=concat(arrint,arrint,arrint);',
  9894. ' arrrec:=concat(arrrec);',
  9895. ' arrrec:=concat(arrrec,arrrec);',
  9896. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9897. ' arrset:=concat(arrset);',
  9898. ' arrset:=concat(arrset,arrset);',
  9899. ' arrset:=concat(arrset,arrset,arrset);',
  9900. ' arrjsvalue:=concat(arrjsvalue);',
  9901. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9902. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9903. ' arrint:=concat([1],arrint);',
  9904. ' arrflag:=concat([big]);',
  9905. ' arrflag:=concat([big],arrflag);',
  9906. ' arrflag:=concat(arrflag,[small]);',
  9907. '']);
  9908. ConvertProgram;
  9909. CheckSource('TestArray_Concat',
  9910. LinesToStr([ // statements
  9911. 'this.TFlag = {',
  9912. ' "0": "big",',
  9913. ' big: 0,',
  9914. ' "1": "small",',
  9915. ' small: 1',
  9916. '};',
  9917. 'rtl.recNewT(this, "TRec", function () {',
  9918. ' this.i = 0;',
  9919. ' this.$eq = function (b) {',
  9920. ' return this.i === b.i;',
  9921. ' };',
  9922. ' this.$assign = function (s) {',
  9923. ' this.i = s.i;',
  9924. ' return this;',
  9925. ' };',
  9926. '});',
  9927. 'this.ArrInt = [];',
  9928. 'this.ArrRec = [];',
  9929. 'this.ArrFlag = [];',
  9930. 'this.ArrSet = [];',
  9931. 'this.ArrJSValue = [];',
  9932. '']),
  9933. LinesToStr([ // $mod.$main
  9934. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9935. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9936. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9937. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9938. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9939. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9940. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9941. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9942. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9943. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9944. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9945. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9946. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9947. '$mod.ArrFlag = [$mod.TFlag.big];',
  9948. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9949. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9950. '']));
  9951. end;
  9952. procedure TTestModule.TestArray_Copy;
  9953. begin
  9954. StartProgram(false);
  9955. Add([
  9956. 'type',
  9957. ' integer = longint;',
  9958. ' TFlag = (big,small);',
  9959. ' TFlags = set of TFlag;',
  9960. ' TRec = record',
  9961. ' i: integer;',
  9962. ' end;',
  9963. ' TArrInt = array of integer;',
  9964. ' TArrRec = array of TRec;',
  9965. ' TArrSet = array of TFlags;',
  9966. ' TArrJSValue = array of jsvalue;',
  9967. 'var',
  9968. ' ArrInt: tarrint;',
  9969. ' ArrRec: tarrrec;',
  9970. ' ArrSet: tarrset;',
  9971. ' ArrJSValue: tarrjsvalue;',
  9972. 'begin',
  9973. ' arrint:=copy(arrint);',
  9974. ' arrint:=copy(arrint,2);',
  9975. ' arrint:=copy(arrint,3,4);',
  9976. ' arrint:=copy([1,1],1,2);',
  9977. ' arrrec:=copy(arrrec);',
  9978. ' arrrec:=copy(arrrec,5);',
  9979. ' arrrec:=copy(arrrec,6,7);',
  9980. ' arrset:=copy(arrset);',
  9981. ' arrset:=copy(arrset,8);',
  9982. ' arrset:=copy(arrset,9,10);',
  9983. ' arrjsvalue:=copy(arrjsvalue);',
  9984. ' arrjsvalue:=copy(arrjsvalue,11);',
  9985. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9986. ' ']);
  9987. ConvertProgram;
  9988. CheckSource('TestArray_Copy',
  9989. LinesToStr([ // statements
  9990. 'this.TFlag = {',
  9991. ' "0": "big",',
  9992. ' big: 0,',
  9993. ' "1": "small",',
  9994. ' small: 1',
  9995. '};',
  9996. 'rtl.recNewT(this, "TRec", function () {',
  9997. ' this.i = 0;',
  9998. ' this.$eq = function (b) {',
  9999. ' return this.i === b.i;',
  10000. ' };',
  10001. ' this.$assign = function (s) {',
  10002. ' this.i = s.i;',
  10003. ' return this;',
  10004. ' };',
  10005. '});',
  10006. 'this.ArrInt = [];',
  10007. 'this.ArrRec = [];',
  10008. 'this.ArrSet = [];',
  10009. 'this.ArrJSValue = [];',
  10010. '']),
  10011. LinesToStr([ // $mod.$main
  10012. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10013. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10014. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10015. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10016. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10017. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10018. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10019. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10020. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10021. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10022. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10023. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10024. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10025. '']));
  10026. end;
  10027. procedure TTestModule.TestArray_InsertDelete;
  10028. begin
  10029. StartProgram(false);
  10030. Add([
  10031. 'type',
  10032. ' integer = longint;',
  10033. ' TFlag = (big,small);',
  10034. ' TFlags = set of TFlag;',
  10035. ' TRec = record',
  10036. ' i: integer;',
  10037. ' end;',
  10038. ' TArrInt = array of integer;',
  10039. ' TArrRec = array of TRec;',
  10040. ' TArrSet = array of TFlags;',
  10041. ' TArrJSValue = array of jsvalue;',
  10042. ' TArrArrInt = array of TArrInt;',
  10043. 'var',
  10044. ' ArrInt: tarrint;',
  10045. ' ArrRec: tarrrec;',
  10046. ' ArrSet: tarrset;',
  10047. ' ArrJSValue: tarrjsvalue;',
  10048. ' ArrArrInt: TArrArrInt;',
  10049. 'begin',
  10050. ' Insert(1,arrint,2);',
  10051. ' Insert(arrint[3],arrint,4);',
  10052. ' Insert(arrrec[5],arrrec,6);',
  10053. ' Insert(arrset[7],arrset,7);',
  10054. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10055. ' Insert(10,arrjsvalue,11);',
  10056. ' Insert([23],arrarrint,22);',
  10057. ' Delete(arrint,12,13);',
  10058. ' Delete(arrrec,14,15);',
  10059. ' Delete(arrset,17,18);',
  10060. ' Delete(arrjsvalue,19,10);']);
  10061. ConvertProgram;
  10062. CheckSource('TestArray_InsertDelete',
  10063. LinesToStr([ // statements
  10064. 'this.TFlag = {',
  10065. ' "0": "big",',
  10066. ' big: 0,',
  10067. ' "1": "small",',
  10068. ' small: 1',
  10069. '};',
  10070. 'rtl.recNewT(this, "TRec", function () {',
  10071. ' this.i = 0;',
  10072. ' this.$eq = function (b) {',
  10073. ' return this.i === b.i;',
  10074. ' };',
  10075. ' this.$assign = function (s) {',
  10076. ' this.i = s.i;',
  10077. ' return this;',
  10078. ' };',
  10079. '});',
  10080. 'this.ArrInt = [];',
  10081. 'this.ArrRec = [];',
  10082. 'this.ArrSet = [];',
  10083. 'this.ArrJSValue = [];',
  10084. 'this.ArrArrInt = [];',
  10085. '']),
  10086. LinesToStr([ // $mod.$main
  10087. '$mod.ArrInt.splice(2, 0, 1);',
  10088. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  10089. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  10090. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  10091. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  10092. '$mod.ArrJSValue.splice(11, 0, 10);',
  10093. '$mod.ArrArrInt.splice(22, 0, [23]);',
  10094. '$mod.ArrInt.splice(12, 13);',
  10095. '$mod.ArrRec.splice(14, 15);',
  10096. '$mod.ArrSet.splice(17, 18);',
  10097. '$mod.ArrJSValue.splice(19, 10);',
  10098. '']));
  10099. end;
  10100. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10101. begin
  10102. Parser.Options:=Parser.Options+[po_cassignments];
  10103. StartProgram(false);
  10104. Add([
  10105. '{$modeswitch arrayoperators}',
  10106. 'type',
  10107. ' integer = longint;',
  10108. ' TArrInt = array of integer;',
  10109. ' TArrStr = array of string;',
  10110. 'const',
  10111. ' Ints: TArrInt = (1,2,3);',
  10112. ' Aliases: TarrStr = (''foo'',''b'');',
  10113. ' OneInt: TArrInt = (7);',
  10114. ' OneStr: array of integer = (7);',
  10115. ' Chars: array of char = ''aoc'';',
  10116. ' Names: array of string = (''a'',''foo'');',
  10117. ' NameCount = low(Names)+high(Names)+length(Names);',
  10118. 'var i: integer;',
  10119. 'begin',
  10120. ' Ints:=[];',
  10121. ' Ints:=[1,1];',
  10122. ' Ints:=[1]+[2];',
  10123. ' Ints:=[2];',
  10124. ' Ints:=[]+ints;',
  10125. ' Ints:=Ints+[];',
  10126. ' Ints:=Ints+OneInt;',
  10127. ' Ints:=Ints+[1,1];',
  10128. ' Ints:=[i,i]+Ints;',
  10129. ' Ints:=[1]+[i]+[3];',
  10130. '']);
  10131. ConvertProgram;
  10132. CheckSource('TestArray_DynArrayConstObjFPC',
  10133. LinesToStr([ // statements
  10134. 'this.Ints = [1, 2, 3];',
  10135. 'this.Aliases = ["foo", "b"];',
  10136. 'this.OneInt = [7];',
  10137. 'this.OneStr = [7];',
  10138. 'this.Chars = ["a", "o", "c"];',
  10139. 'this.Names = ["a", "foo"];',
  10140. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10141. 'this.i = 0;',
  10142. '']),
  10143. LinesToStr([ // $mod.$main
  10144. '$mod.Ints = [];',
  10145. '$mod.Ints = [1, 1];',
  10146. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10147. '$mod.Ints = [2];',
  10148. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10149. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10150. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10151. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10152. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10153. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10154. '']));
  10155. end;
  10156. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10157. begin
  10158. StartProgram(false);
  10159. // Note: const c = [1,1]; defines a set!
  10160. Add([
  10161. '{$mode delphi}',
  10162. 'type',
  10163. ' integer = longint;',
  10164. ' TArrInt = array of integer;',
  10165. ' TArrStr = array of string;',
  10166. 'const',
  10167. ' Ints: TArrInt = [1,1,2];',
  10168. ' Aliases: TarrStr = [''foo'',''b''];',
  10169. ' OneInt: TArrInt = [7];',
  10170. ' OneStr: array of integer = [7]+[8];',
  10171. ' Chars: array of char = ''aoc'';',
  10172. ' Names: array of string = [''a'',''a''];',
  10173. ' NameCount = low(Names)+high(Names)+length(Names);',
  10174. 'begin',
  10175. '']);
  10176. ConvertProgram;
  10177. CheckSource('TestArray_DynArrayConstDelphi',
  10178. LinesToStr([ // statements
  10179. 'this.Ints = [1, 1, 2];',
  10180. 'this.Aliases = ["foo", "b"];',
  10181. 'this.OneInt = [7];',
  10182. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10183. 'this.Chars = ["a", "o", "c"];',
  10184. 'this.Names = ["a", "a"];',
  10185. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10186. '']),
  10187. LinesToStr([ // $mod.$main
  10188. '']));
  10189. end;
  10190. procedure TTestModule.TestArray_ArrayLitAsParam;
  10191. begin
  10192. StartProgram(false);
  10193. Add([
  10194. '{$modeswitch arrayoperators}',
  10195. 'type',
  10196. ' integer = longint;',
  10197. ' TArrInt = array of integer;',
  10198. ' TArrSet = array of (red,green,blue);',
  10199. 'procedure DoOpenInt(const a: array of integer); forward;',
  10200. 'procedure DoInt(const a: TArrInt);',
  10201. 'begin',
  10202. ' DoInt(a+[1]);',
  10203. ' DoInt([1]+a);',
  10204. ' DoOpenInt(a);',
  10205. ' DoOpenInt(a+[1]);',
  10206. ' DoOpenInt([1]+a);',
  10207. 'end;',
  10208. 'procedure DoOpenInt(const a: array of integer);',
  10209. 'begin',
  10210. ' DoOpenInt(a+[1]);',
  10211. ' DoOpenInt([1]+a);',
  10212. ' DoInt(a);',
  10213. ' DoInt(a+[1]);',
  10214. ' DoInt([1]+a);',
  10215. 'end;',
  10216. 'procedure DoSet(const a: TArrSet);',
  10217. 'begin',
  10218. ' DoSet(a+[red]);',
  10219. ' DoSet([blue]+a);',
  10220. 'end;',
  10221. 'var',
  10222. ' i: TArrInt;',
  10223. ' s: TArrSet;',
  10224. 'begin',
  10225. ' DoInt([1]);',
  10226. ' DoInt([1]+[2]);',
  10227. ' DoInt(i+[1]);',
  10228. ' DoInt([1]+i);',
  10229. ' DoOpenInt([1]);',
  10230. ' DoOpenInt([1]+[2]);',
  10231. ' DoOpenInt(i+[1]);',
  10232. ' DoOpenInt([1]+i);',
  10233. ' DoSet([red]);',
  10234. ' DoSet([blue]+[green]);',
  10235. ' DoSet(s+[blue]);',
  10236. ' DoSet([red]+s);',
  10237. '']);
  10238. ConvertProgram;
  10239. CheckSource('TestArray_ArrayLitAsParam',
  10240. LinesToStr([ // statements
  10241. 'this.TArrSet$a = {',
  10242. ' "0": "red",',
  10243. ' red: 0,',
  10244. ' "1": "green",',
  10245. ' green: 1,',
  10246. ' "2": "blue",',
  10247. ' blue: 2',
  10248. '};',
  10249. 'this.DoInt = function (a) {',
  10250. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10251. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10252. ' $mod.DoOpenInt(a);',
  10253. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10254. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10255. '};',
  10256. 'this.DoOpenInt = function (a) {',
  10257. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10258. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10259. ' $mod.DoInt(a);',
  10260. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10261. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10262. '};',
  10263. 'this.DoSet = function (a) {',
  10264. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10265. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10266. '};',
  10267. 'this.i = [];',
  10268. 'this.s = [];',
  10269. '']),
  10270. LinesToStr([ // $mod.$main
  10271. '$mod.DoInt([1]);',
  10272. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10273. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10274. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10275. '$mod.DoOpenInt([1]);',
  10276. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10277. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10278. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10279. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10280. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10281. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10282. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10283. '']));
  10284. end;
  10285. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10286. begin
  10287. StartProgram(false);
  10288. Add([
  10289. '{$modeswitch arrayoperators}',
  10290. 'type',
  10291. ' integer = longint;',
  10292. ' TArrInt = array of integer;',
  10293. ' TArrArrInt = array of TArrInt;',
  10294. 'procedure DoInt(const a: TArrArrInt);',
  10295. 'begin',
  10296. ' DoInt(a+[[1]]);',
  10297. ' DoInt([[1]]+a);',
  10298. ' DoInt(a);',
  10299. 'end;',
  10300. 'var',
  10301. ' i: TArrInt;',
  10302. ' a: TArrArrInt;',
  10303. 'begin',
  10304. ' a:=[[1]];',
  10305. ' a:=[i];',
  10306. ' a:=a+[i];',
  10307. ' a:=[i]+a;',
  10308. ' a:=[[1]+i];',
  10309. ' a:=[[1]+[2]];',
  10310. ' a:=[i+[2]];',
  10311. ' DoInt([[1]]);',
  10312. ' DoInt([[1]+[2],[3,4],[5]]);',
  10313. ' DoInt([i+[1]]+a);',
  10314. ' DoInt([i]+a);',
  10315. '']);
  10316. ConvertProgram;
  10317. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10318. LinesToStr([ // statements
  10319. 'this.DoInt = function (a) {',
  10320. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10321. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10322. ' $mod.DoInt(a);',
  10323. '};',
  10324. 'this.i = [];',
  10325. 'this.a = [];',
  10326. '']),
  10327. LinesToStr([ // $mod.$main
  10328. '$mod.a = [[1]];',
  10329. '$mod.a = [$mod.i];',
  10330. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10331. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10332. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10333. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10334. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10335. '$mod.DoInt([[1]]);',
  10336. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10337. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10338. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10339. '']));
  10340. end;
  10341. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10342. begin
  10343. StartProgram(false);
  10344. Add([
  10345. '{$modeswitch arrayoperators}',
  10346. 'type',
  10347. ' integer = longint;',
  10348. ' TArrInt = array[1..2] of integer;',
  10349. ' TArrArrInt = array of TArrInt;',
  10350. 'procedure DoInt(const a: TArrArrInt);',
  10351. 'begin',
  10352. ' DoInt(a+[[1,2]]);',
  10353. ' DoInt([[1,2]]+a);',
  10354. ' DoInt(a);',
  10355. 'end;',
  10356. 'var',
  10357. ' i: TArrInt;',
  10358. ' a: TArrArrInt;',
  10359. 'begin',
  10360. ' a:=[[1,1]];',
  10361. ' a:=[i];',
  10362. ' a:=a+[i];',
  10363. ' a:=[i]+a;',
  10364. ' DoInt([[1,1]]);',
  10365. ' DoInt([[1,2],[3,4]]);',
  10366. '']);
  10367. ConvertProgram;
  10368. CheckSource('TestArray_ArrayLitStaticAsParam',
  10369. LinesToStr([ // statements
  10370. 'this.DoInt = function (a) {',
  10371. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10372. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10373. ' $mod.DoInt(a);',
  10374. '};',
  10375. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10376. 'this.a = [];',
  10377. '']),
  10378. LinesToStr([ // $mod.$main
  10379. '$mod.a = [[1, 1]];',
  10380. '$mod.a = [$mod.i.slice(0)];',
  10381. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10382. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10383. '$mod.DoInt([[1, 1]]);',
  10384. '$mod.DoInt([[1, 2], [3, 4]]);',
  10385. '']));
  10386. end;
  10387. procedure TTestModule.TestArray_ForInArrOfString;
  10388. begin
  10389. StartProgram(false);
  10390. Add([
  10391. 'type',
  10392. 'type',
  10393. ' TMonthNameArray = array [1..12] of string;',
  10394. ' TMonthNames = TMonthNameArray;',
  10395. ' TObject = class',
  10396. ' private',
  10397. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10398. ' public',
  10399. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10400. ' end;',
  10401. 'var',
  10402. ' f: TObject;',
  10403. ' Month: string;',
  10404. ' Names: array of string = (''a'',''foo'',''bar'');',
  10405. ' i: longint;',
  10406. 'begin',
  10407. ' for Month in f.LongMonthNames do ;',
  10408. ' for Month in Names do ;',
  10409. ' for i:=low(Names) to high(Names) do ;',
  10410. '']);
  10411. ConvertProgram;
  10412. CheckSource('TestArray_ForInArrOfString',
  10413. LinesToStr([ // statements
  10414. 'rtl.createClass(this, "TObject", null, function () {',
  10415. ' this.$init = function () {',
  10416. ' };',
  10417. ' this.$final = function () {',
  10418. ' };',
  10419. '});',
  10420. 'this.f = null;',
  10421. 'this.Month = "";',
  10422. 'this.Names = ["a", "foo", "bar"];',
  10423. 'this.i = 0;',
  10424. '']),
  10425. LinesToStr([ // $mod.$main
  10426. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10427. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10428. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10429. '']));
  10430. end;
  10431. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10432. begin
  10433. StartProgram(false);
  10434. Add([
  10435. '{$modeswitch externalclass}',
  10436. 'type',
  10437. ' TJSObject = class external name ''Object''',
  10438. ' end;',
  10439. ' TJSArray = class external name ''Array''',
  10440. ' class function isArray(Value: JSValue) : boolean;',
  10441. ' function concat() : TJSArray; varargs;',
  10442. ' end;',
  10443. 'var',
  10444. ' aObj: TJSArray;',
  10445. ' a: array of longint;',
  10446. ' o: TJSObject;',
  10447. 'begin',
  10448. ' if TJSArray.isArray(65) then ;',
  10449. ' aObj:=TJSArray(a).concat(a);',
  10450. ' o:=TJSObject(a);']);
  10451. ConvertProgram;
  10452. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10453. LinesToStr([ // statements
  10454. 'this.aObj = null;',
  10455. 'this.a = [];',
  10456. 'this.o = null;',
  10457. '']),
  10458. LinesToStr([ // $mod.$main
  10459. 'if (Array.isArray(65)) ;',
  10460. '$mod.aObj = $mod.a.concat($mod.a);',
  10461. '$mod.o = $mod.a;',
  10462. '']));
  10463. end;
  10464. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10465. begin
  10466. StartProgram(false);
  10467. Add([
  10468. '{$modeswitch externalclass}',
  10469. 'type',
  10470. ' TArrStr = array of string;',
  10471. ' TJSArray = class external name ''Array''',
  10472. ' end;',
  10473. ' TJSObject = class external name ''Object''',
  10474. ' end;',
  10475. 'var',
  10476. ' aObj: TJSArray;',
  10477. ' a: TArrStr;',
  10478. ' jo: TJSObject;',
  10479. 'begin',
  10480. ' a:=TArrStr(aObj);',
  10481. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10482. ' a:=TarrStr(jo);',
  10483. '']);
  10484. ConvertProgram;
  10485. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10486. LinesToStr([ // statements
  10487. 'this.aObj = null;',
  10488. 'this.a = [];',
  10489. 'this.jo = null;',
  10490. '']),
  10491. LinesToStr([ // $mod.$main
  10492. '$mod.a = $mod.aObj;',
  10493. '$mod.aObj[1] = $mod.aObj[2];',
  10494. '$mod.a = $mod.jo;',
  10495. '']));
  10496. end;
  10497. procedure TTestModule.TestArrayOfConst_TVarRec;
  10498. begin
  10499. StartProgram(true,[supTVarRec]);
  10500. Add([
  10501. 'procedure Say(args: array of const);',
  10502. 'var',
  10503. ' i: longint;',
  10504. ' v: TVarRec;',
  10505. 'begin',
  10506. ' for i:=low(args) to high(args) do begin',
  10507. ' v:=args[i];',
  10508. ' case v.vtype of',
  10509. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10510. ' end;',
  10511. ' end;',
  10512. ' for v in args do ;',
  10513. ' args:=nil;',
  10514. ' SetLength(args,2);',
  10515. 'end;',
  10516. 'begin']);
  10517. ConvertProgram;
  10518. CheckSource('TestArrayOfConst_TVarRec',
  10519. LinesToStr([ // statements
  10520. 'this.Say = function (args) {',
  10521. ' var i = 0;',
  10522. ' var v = pas.system.TVarRec.$new();',
  10523. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10524. ' i = $l;',
  10525. ' v.$assign(args[i]);',
  10526. ' var $tmp = v.VType;',
  10527. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10528. ' };',
  10529. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10530. ' args = [];',
  10531. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10532. '};',
  10533. '']),
  10534. LinesToStr([ // $mod.$main
  10535. ]));
  10536. end;
  10537. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10538. begin
  10539. StartProgram(true,[supTVarRec]);
  10540. Add([
  10541. 'procedure Say(args: array of const);',
  10542. 'begin',
  10543. ' Say(args);',
  10544. 'end;',
  10545. 'var',
  10546. ' p: Pointer;',
  10547. ' j: jsvalue;',
  10548. ' c: currency;',
  10549. 'begin',
  10550. ' Say([]);',
  10551. ' Say([1]);',
  10552. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10553. '']);
  10554. ConvertProgram;
  10555. CheckSource('TestArrayOfConst_PassBaseTypes',
  10556. LinesToStr([ // statements
  10557. 'this.Say = function (args) {',
  10558. ' $mod.Say(args);',
  10559. '};',
  10560. 'this.p = null;',
  10561. 'this.j = undefined;',
  10562. 'this.c = 0;',
  10563. '']),
  10564. LinesToStr([ // $mod.$main
  10565. '$mod.Say([]);',
  10566. '$mod.Say(pas.system.VarRecs(0, 1));',
  10567. '$mod.Say(pas.system.VarRecs(',
  10568. ' 9,',
  10569. ' "c",',
  10570. ' 18,',
  10571. ' "foo",',
  10572. ' 5,',
  10573. ' null,',
  10574. ' 1,',
  10575. ' true,',
  10576. ' 3,',
  10577. ' 1.3,',
  10578. ' 5,',
  10579. ' $mod.p,',
  10580. ' 20,',
  10581. ' $mod.j,',
  10582. ' 12,',
  10583. ' $mod.c',
  10584. ' ));',
  10585. '']));
  10586. end;
  10587. procedure TTestModule.TestArrayOfConst_PassObj;
  10588. begin
  10589. StartProgram(true,[supTVarRec]);
  10590. Add([
  10591. '{$interfaces corba}',
  10592. 'type',
  10593. ' TObject = class',
  10594. ' end;',
  10595. ' TClass = class of TObject;',
  10596. ' IUnknown = interface',
  10597. ' end;',
  10598. 'procedure Say(args: array of const);',
  10599. 'begin',
  10600. 'end;',
  10601. 'var',
  10602. ' o: TObject;',
  10603. ' c: TClass;',
  10604. ' i: IUnknown;',
  10605. 'begin',
  10606. ' Say([o,c,TObject]);',
  10607. ' Say([nil,i]);',
  10608. '']);
  10609. ConvertProgram;
  10610. CheckSource('TestArrayOfConst_PassObj',
  10611. LinesToStr([ // statements
  10612. 'rtl.createClass(this, "TObject", null, function () {',
  10613. ' this.$init = function () {',
  10614. ' };',
  10615. ' this.$final = function () {',
  10616. ' };',
  10617. '});',
  10618. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10619. 'this.Say = function (args) {',
  10620. '};',
  10621. 'this.o = null;',
  10622. 'this.c = null;',
  10623. 'this.i = null;',
  10624. '']),
  10625. LinesToStr([ // $mod.$main
  10626. '$mod.Say(pas.system.VarRecs(',
  10627. ' 7,',
  10628. ' $mod.o,',
  10629. ' 8,',
  10630. ' $mod.c,',
  10631. ' 8,',
  10632. ' $mod.TObject',
  10633. '));',
  10634. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10635. '']));
  10636. end;
  10637. procedure TTestModule.TestRecord_Empty;
  10638. begin
  10639. StartProgram(false);
  10640. Add([
  10641. 'type',
  10642. ' TRecA = record',
  10643. ' end;',
  10644. 'var a,b: TRecA;',
  10645. 'begin',
  10646. ' if a=b then ;']);
  10647. ConvertProgram;
  10648. CheckSource('TestRecord_Empty',
  10649. LinesToStr([ // statements
  10650. 'rtl.recNewT(this, "TRecA", function () {',
  10651. ' this.$eq = function (b) {',
  10652. ' return true;',
  10653. ' };',
  10654. ' this.$assign = function (s) {',
  10655. ' return this;',
  10656. ' };',
  10657. '});',
  10658. 'this.a = this.TRecA.$new();',
  10659. 'this.b = this.TRecA.$new();',
  10660. '']),
  10661. LinesToStr([ // $mod.$main
  10662. 'if ($mod.a.$eq($mod.b)) ;'
  10663. ]));
  10664. end;
  10665. procedure TTestModule.TestRecord_Var;
  10666. begin
  10667. StartProgram(false);
  10668. Add('type');
  10669. Add(' TRecA = record');
  10670. Add(' Bold: longint;');
  10671. Add(' end;');
  10672. Add('var Rec: TRecA;');
  10673. Add('begin');
  10674. Add(' rec.bold:=123');
  10675. ConvertProgram;
  10676. CheckSource('TestRecord_Var',
  10677. LinesToStr([ // statements
  10678. 'rtl.recNewT(this, "TRecA", function () {',
  10679. ' this.Bold = 0;',
  10680. ' this.$eq = function (b) {',
  10681. ' return this.Bold === b.Bold;',
  10682. ' };',
  10683. ' this.$assign = function (s) {',
  10684. ' this.Bold = s.Bold;',
  10685. ' return this;',
  10686. ' };',
  10687. '});',
  10688. 'this.Rec = this.TRecA.$new();',
  10689. '']),
  10690. LinesToStr([ // $mod.$main
  10691. '$mod.Rec.Bold = 123;'
  10692. ]));
  10693. end;
  10694. procedure TTestModule.TestRecord_VarExternal;
  10695. begin
  10696. StartProgram(false);
  10697. Add([
  10698. '{$modeswitch externalclass}',
  10699. 'type',
  10700. ' TRecA = record',
  10701. ' i: byte;',
  10702. ' length_: longint external name ''length'';',
  10703. ' end;',
  10704. 'var Rec: TRecA;',
  10705. 'begin',
  10706. ' rec.length_ := rec.length_',
  10707. '']);
  10708. ConvertProgram;
  10709. CheckSource('TestRecord_VarExternal',
  10710. LinesToStr([ // statements
  10711. 'rtl.recNewT(this, "TRecA", function () {',
  10712. ' this.i = 0;',
  10713. ' this.$eq = function (b) {',
  10714. ' return (this.i === b.i) && (this.length === b.length);',
  10715. ' };',
  10716. ' this.$assign = function (s) {',
  10717. ' this.i = s.i;',
  10718. ' this.length = s.length;',
  10719. ' return this;',
  10720. ' };',
  10721. '});',
  10722. 'this.Rec = this.TRecA.$new();',
  10723. '']),
  10724. LinesToStr([ // $mod.$main
  10725. '$mod.Rec.length = $mod.Rec.length;'
  10726. ]));
  10727. end;
  10728. procedure TTestModule.TestRecord_WithDo;
  10729. begin
  10730. StartProgram(false);
  10731. Add('type');
  10732. Add(' TRec = record');
  10733. Add(' vI: longint;');
  10734. Add(' end;');
  10735. Add('var');
  10736. Add(' Int: longint;');
  10737. Add(' r: TRec;');
  10738. Add('begin');
  10739. Add(' with r do');
  10740. Add(' int:=vi;');
  10741. Add(' with r do begin');
  10742. Add(' int:=vi;');
  10743. Add(' vi:=int;');
  10744. Add(' end;');
  10745. ConvertProgram;
  10746. CheckSource('TestWithRecordDo',
  10747. LinesToStr([ // statements
  10748. 'rtl.recNewT(this, "TRec", function () {',
  10749. ' this.vI = 0;',
  10750. ' this.$eq = function (b) {',
  10751. ' return this.vI === b.vI;',
  10752. ' };',
  10753. ' this.$assign = function (s) {',
  10754. ' this.vI = s.vI;',
  10755. ' return this;',
  10756. ' };',
  10757. '});',
  10758. 'this.Int = 0;',
  10759. 'this.r = this.TRec.$new();',
  10760. '']),
  10761. LinesToStr([ // $mod.$main
  10762. 'var $with = $mod.r;',
  10763. '$mod.Int = $with.vI;',
  10764. 'var $with1 = $mod.r;',
  10765. '$mod.Int = $with1.vI;',
  10766. '$with1.vI = $mod.Int;'
  10767. ]));
  10768. end;
  10769. procedure TTestModule.TestRecord_Assign;
  10770. begin
  10771. StartProgram(false);
  10772. Add('type');
  10773. Add(' TEnum = (red,green);');
  10774. Add(' TEnums = set of TEnum;');
  10775. Add(' TSmallRec = record');
  10776. Add(' N: longint;');
  10777. Add(' end;');
  10778. Add(' TBigRec = record');
  10779. Add(' Int: longint;');
  10780. Add(' D: double;');
  10781. Add(' Arr: array of longint;');
  10782. Add(' Arr2: array[1..2] of longint;');
  10783. Add(' Small: TSmallRec;');
  10784. Add(' Enums: TEnums;');
  10785. Add(' end;');
  10786. Add('var');
  10787. Add(' r, s: TBigRec;');
  10788. Add('begin');
  10789. Add(' r:=s;');
  10790. Add(' r:=default(TBigRec);');
  10791. Add(' r:=default(s);');
  10792. ConvertProgram;
  10793. CheckSource('TestRecord_Assign',
  10794. LinesToStr([ // statements
  10795. 'this.TEnum = {',
  10796. ' "0": "red",',
  10797. ' red: 0,',
  10798. ' "1": "green",',
  10799. ' green: 1',
  10800. '};',
  10801. 'rtl.recNewT(this, "TSmallRec", function () {',
  10802. ' this.N = 0;',
  10803. ' this.$eq = function (b) {',
  10804. ' return this.N === b.N;',
  10805. ' };',
  10806. ' this.$assign = function (s) {',
  10807. ' this.N = s.N;',
  10808. ' return this;',
  10809. ' };',
  10810. '});',
  10811. 'rtl.recNewT(this, "TBigRec", function () {',
  10812. ' this.Int = 0;',
  10813. ' this.D = 0.0;',
  10814. ' this.$new = function () {',
  10815. ' var r = Object.create(this);',
  10816. ' r.Arr = [];',
  10817. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10818. ' r.Small = $mod.TSmallRec.$new();',
  10819. ' r.Enums = {};',
  10820. ' return r;',
  10821. ' };',
  10822. ' this.$eq = function (b) {',
  10823. ' 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);',
  10824. ' };',
  10825. ' this.$assign = function (s) {',
  10826. ' this.Int = s.Int;',
  10827. ' this.D = s.D;',
  10828. ' this.Arr = rtl.arrayRef(s.Arr);',
  10829. ' this.Arr2 = s.Arr2.slice(0);',
  10830. ' this.Small.$assign(s.Small);',
  10831. ' this.Enums = rtl.refSet(s.Enums);',
  10832. ' return this;',
  10833. ' };',
  10834. '});',
  10835. 'this.r = this.TBigRec.$new();',
  10836. 'this.s = this.TBigRec.$new();',
  10837. '']),
  10838. LinesToStr([ // $mod.$main
  10839. '$mod.r.$assign($mod.s);',
  10840. '$mod.r.$assign($mod.TBigRec.$new());',
  10841. '$mod.r.$assign($mod.TBigRec.$new());',
  10842. '']));
  10843. end;
  10844. procedure TTestModule.TestRecord_AsParams;
  10845. begin
  10846. StartProgram(false);
  10847. Add([
  10848. 'type',
  10849. ' integer = longint;',
  10850. ' TRecord = record',
  10851. ' i: integer;',
  10852. ' end;',
  10853. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10854. 'var vL: TRecord;',
  10855. 'begin',
  10856. ' vd:=vd;',
  10857. ' vd.i:=vd.i;',
  10858. ' vl:=vc;',
  10859. ' vv:=vv;',
  10860. ' vv.i:=vv.i;',
  10861. ' U:=vl;',
  10862. ' U:=vd;',
  10863. ' U:=vc;',
  10864. ' U:=vv;',
  10865. ' vl:=TRecord(U);',
  10866. ' vd:=TRecord(U);',
  10867. ' vv:=TRecord(U);',
  10868. ' doit(vd,vd,vd,vd);',
  10869. ' doit(vc,vc,vl,vl);',
  10870. ' doit(vv,vv,vv,vv);',
  10871. ' doit(vl,vl,vl,vl);',
  10872. ' TRecord(U).i:=3;',
  10873. 'end;',
  10874. 'var i: TRecord;',
  10875. 'begin',
  10876. ' doit(i,i,i,i);',
  10877. '']);
  10878. ConvertProgram;
  10879. CheckSource('TestRecord_AsParams',
  10880. LinesToStr([ // statements
  10881. 'rtl.recNewT(this, "TRecord", function () {',
  10882. ' this.i = 0;',
  10883. ' this.$eq = function (b) {',
  10884. ' return this.i === b.i;',
  10885. ' };',
  10886. ' this.$assign = function (s) {',
  10887. ' this.i = s.i;',
  10888. ' return this;',
  10889. ' };',
  10890. '});',
  10891. 'this.DoIt = function (vD, vC, vV, U) {',
  10892. ' var vL = $mod.TRecord.$new();',
  10893. ' vD.$assign(vD);',
  10894. ' vD.i = vD.i;',
  10895. ' vL.$assign(vC);',
  10896. ' vV.$assign(vV);',
  10897. ' vV.i = vV.i;',
  10898. ' U.$assign(vL);',
  10899. ' U.$assign(vD);',
  10900. ' U.$assign(vC);',
  10901. ' U.$assign(vV);',
  10902. ' vL.$assign(U);',
  10903. ' vD.$assign(U);',
  10904. ' vV.$assign(U);',
  10905. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10906. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10907. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10908. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10909. ' U.i = 3;',
  10910. '};',
  10911. 'this.i = this.TRecord.$new();'
  10912. ]),
  10913. LinesToStr([
  10914. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10915. '']));
  10916. end;
  10917. procedure TTestModule.TestRecord_ConstRef;
  10918. begin
  10919. StartProgram(false);
  10920. Add([
  10921. 'type TRec = record i: word; end;',
  10922. 'procedure Run(constref a: TRec);',
  10923. 'begin',
  10924. 'end;',
  10925. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10926. 'var l: TRec;',
  10927. 'begin',
  10928. ' Run(l);',
  10929. ' Run(a);',
  10930. ' Run(b);',
  10931. ' Run(c);',
  10932. ' Run(d);',
  10933. ' Run(e);',
  10934. 'end;',
  10935. 'begin',
  10936. '']);
  10937. ConvertProgram;
  10938. CheckResolverUnexpectedHints();
  10939. CheckSource('TestRecord_ConstRef',
  10940. LinesToStr([ // statements
  10941. 'rtl.recNewT(this, "TRec", function () {',
  10942. ' this.i = 0;',
  10943. ' this.$eq = function (b) {',
  10944. ' return this.i === b.i;',
  10945. ' };',
  10946. ' this.$assign = function (s) {',
  10947. ' this.i = s.i;',
  10948. ' return this;',
  10949. ' };',
  10950. '});',
  10951. 'this.Run = function (a) {',
  10952. '};',
  10953. 'this.Fly = function (a, b, c, d, e) {',
  10954. ' var l = $mod.TRec.$new();',
  10955. ' $mod.Run(l);',
  10956. ' $mod.Run(a);',
  10957. ' $mod.Run(b);',
  10958. ' $mod.Run(c);',
  10959. ' $mod.Run(d);',
  10960. ' $mod.Run(e);',
  10961. '};',
  10962. '']),
  10963. LinesToStr([
  10964. '']));
  10965. end;
  10966. procedure TTestModule.TestRecordElement_AsParams;
  10967. begin
  10968. StartProgram(false);
  10969. Add('type');
  10970. Add(' integer = longint;');
  10971. Add(' TRecord = record');
  10972. Add(' i: integer;');
  10973. Add(' end;');
  10974. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10975. Add('var vJ: TRecord;');
  10976. Add('begin');
  10977. Add(' doit(vj.i,vj.i,vj.i);');
  10978. Add('end;');
  10979. Add('var r: TRecord;');
  10980. Add('begin');
  10981. Add(' doit(r.i,r.i,r.i);');
  10982. ConvertProgram;
  10983. CheckSource('TestRecordElement_AsParams',
  10984. LinesToStr([ // statements
  10985. 'rtl.recNewT(this, "TRecord", function () {',
  10986. ' this.i = 0;',
  10987. ' this.$eq = function (b) {',
  10988. ' return this.i === b.i;',
  10989. ' };',
  10990. ' this.$assign = function (s) {',
  10991. ' this.i = s.i;',
  10992. ' return this;',
  10993. ' };',
  10994. '});',
  10995. 'this.DoIt = function (vG,vH,vI) {',
  10996. ' var vJ = $mod.TRecord.$new();',
  10997. ' $mod.DoIt(vJ.i, vJ.i, {',
  10998. ' p: vJ,',
  10999. ' get: function () {',
  11000. ' return this.p.i;',
  11001. ' },',
  11002. ' set: function (v) {',
  11003. ' this.p.i = v;',
  11004. ' }',
  11005. ' });',
  11006. '};',
  11007. 'this.r = this.TRecord.$new();'
  11008. ]),
  11009. LinesToStr([
  11010. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11011. ' p: $mod.r,',
  11012. ' get: function () {',
  11013. ' return this.p.i;',
  11014. ' },',
  11015. ' set: function (v) {',
  11016. ' this.p.i = v;',
  11017. ' }',
  11018. '});'
  11019. ]));
  11020. end;
  11021. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11022. begin
  11023. StartProgram(false);
  11024. Add('type');
  11025. Add(' integer = longint;');
  11026. Add(' TRecord = record');
  11027. Add(' i: integer;');
  11028. Add(' end;');
  11029. Add('function GetRec(vB: integer = 0): TRecord;');
  11030. Add('begin');
  11031. Add('end;');
  11032. Add('procedure DoIt(vG: integer; const vH: integer);');
  11033. Add('begin');
  11034. Add('end;');
  11035. Add('begin');
  11036. Add(' doit(getrec.i,getrec.i);');
  11037. Add(' doit(getrec().i,getrec().i);');
  11038. Add(' doit(getrec(1).i,getrec(2).i);');
  11039. ConvertProgram;
  11040. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11041. LinesToStr([ // statements
  11042. 'rtl.recNewT(this, "TRecord", function () {',
  11043. ' this.i = 0;',
  11044. ' this.$eq = function (b) {',
  11045. ' return this.i === b.i;',
  11046. ' };',
  11047. ' this.$assign = function (s) {',
  11048. ' this.i = s.i;',
  11049. ' return this;',
  11050. ' };',
  11051. '});',
  11052. 'this.GetRec = function (vB) {',
  11053. ' var Result = $mod.TRecord.$new();',
  11054. ' return Result;',
  11055. '};',
  11056. 'this.DoIt = function (vG, vH) {',
  11057. '};',
  11058. '']),
  11059. LinesToStr([
  11060. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11061. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11062. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11063. '']));
  11064. end;
  11065. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11066. begin
  11067. StartProgram(false);
  11068. Add('type');
  11069. Add(' integer = longint;');
  11070. Add(' TRecord = record');
  11071. Add(' i: integer;');
  11072. Add(' end;');
  11073. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11074. Add('begin');
  11075. Add('end;');
  11076. Add('var r: trecord;');
  11077. Add('begin');
  11078. Add(' with r do ');
  11079. Add(' doit(i,i,i);');
  11080. ConvertProgram;
  11081. CheckSource('TestRecordElementFromWith_AsParams',
  11082. LinesToStr([ // statements
  11083. 'rtl.recNewT(this, "TRecord", function () {',
  11084. ' this.i = 0;',
  11085. ' this.$eq = function (b) {',
  11086. ' return this.i === b.i;',
  11087. ' };',
  11088. ' this.$assign = function (s) {',
  11089. ' this.i = s.i;',
  11090. ' return this;',
  11091. ' };',
  11092. '});',
  11093. 'this.DoIt = function (vG,vH,vI) {',
  11094. '};',
  11095. 'this.r = this.TRecord.$new();'
  11096. ]),
  11097. LinesToStr([
  11098. 'var $with = $mod.r;',
  11099. '$mod.DoIt($with.i,$with.i,{',
  11100. ' p: $with,',
  11101. ' get: function () {',
  11102. ' return this.p.i;',
  11103. ' },',
  11104. ' set: function (v) {',
  11105. ' this.p.i = v;',
  11106. ' }',
  11107. '});',
  11108. '']));
  11109. end;
  11110. procedure TTestModule.TestRecord_Equal;
  11111. begin
  11112. StartProgram(false);
  11113. Add('type');
  11114. Add(' integer = longint;');
  11115. Add(' TFlag = (red,blue);');
  11116. Add(' TFlags = set of TFlag;');
  11117. Add(' TProc = procedure;');
  11118. Add(' TRecord = record');
  11119. Add(' i: integer;');
  11120. Add(' Event: TProc;');
  11121. Add(' f: TFlags;');
  11122. Add(' end;');
  11123. Add(' TNested = record');
  11124. Add(' r: TRecord;');
  11125. Add(' end;');
  11126. Add('var');
  11127. Add(' b: boolean;');
  11128. Add(' r,s: trecord;');
  11129. Add('begin');
  11130. Add(' b:=r=s;');
  11131. Add(' b:=r<>s;');
  11132. ConvertProgram;
  11133. CheckSource('TestRecord_Equal',
  11134. LinesToStr([ // statements
  11135. 'this.TFlag = {',
  11136. ' "0": "red",',
  11137. ' red: 0,',
  11138. ' "1": "blue",',
  11139. ' blue: 1',
  11140. '};',
  11141. 'rtl.recNewT(this, "TRecord", function () {',
  11142. ' this.i = 0;',
  11143. ' this.Event = null;',
  11144. ' this.$new = function () {',
  11145. ' var r = Object.create(this);',
  11146. ' r.f = {};',
  11147. ' return r;',
  11148. ' };',
  11149. ' this.$eq = function (b) {',
  11150. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11151. ' };',
  11152. ' this.$assign = function (s) {',
  11153. ' this.i = s.i;',
  11154. ' this.Event = s.Event;',
  11155. ' this.f = rtl.refSet(s.f);',
  11156. ' return this;',
  11157. ' };',
  11158. '});',
  11159. 'rtl.recNewT(this, "TNested", function () {',
  11160. ' this.$new = function () {',
  11161. ' var r = Object.create(this);',
  11162. ' r.r = $mod.TRecord.$new();',
  11163. ' return r;',
  11164. ' };',
  11165. ' this.$eq = function (b) {',
  11166. ' return this.r.$eq(b.r);',
  11167. ' };',
  11168. ' this.$assign = function (s) {',
  11169. ' this.r.$assign(s.r);',
  11170. ' return this;',
  11171. ' };',
  11172. '});',
  11173. 'this.b = false;',
  11174. 'this.r = this.TRecord.$new();',
  11175. 'this.s = this.TRecord.$new();',
  11176. '']),
  11177. LinesToStr([
  11178. '$mod.b = $mod.r.$eq($mod.s);',
  11179. '$mod.b = !$mod.r.$eq($mod.s);',
  11180. '']));
  11181. end;
  11182. procedure TTestModule.TestRecord_JSValue;
  11183. begin
  11184. StartProgram(false);
  11185. Add([
  11186. 'type',
  11187. ' TRecord = record',
  11188. ' i: longint;',
  11189. ' end;',
  11190. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11191. 'begin',
  11192. 'end;',
  11193. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11194. 'begin',
  11195. ' if jsvalue(d) then ;',
  11196. ' if jsvalue(c) then ;',
  11197. ' if jsvalue(v) then ;',
  11198. 'end;',
  11199. 'var',
  11200. ' Jv: jsvalue;',
  11201. ' Rec: trecord;',
  11202. 'begin',
  11203. ' rec:=trecord(jv);',
  11204. ' jv:=rec;',
  11205. ' Fly(rec,rec);',
  11206. ' Fly(@rec,@rec);',
  11207. ' if jsvalue(Rec) then ;',
  11208. ' Run(trecord(jv),trecord(jv),rec);',
  11209. '']);
  11210. ConvertProgram;
  11211. CheckSource('TestRecord_JSValue',
  11212. LinesToStr([ // statements
  11213. 'rtl.recNewT(this, "TRecord", function () {',
  11214. ' this.i = 0;',
  11215. ' this.$eq = function (b) {',
  11216. ' return this.i === b.i;',
  11217. ' };',
  11218. ' this.$assign = function (s) {',
  11219. ' this.i = s.i;',
  11220. ' return this;',
  11221. ' };',
  11222. '});',
  11223. 'this.Fly = function (d, c) {',
  11224. '};',
  11225. 'this.Run = function (d, c, v) {',
  11226. ' if (d) ;',
  11227. ' if (c) ;',
  11228. ' if (v) ;',
  11229. '};',
  11230. 'this.Jv = undefined;',
  11231. 'this.Rec = this.TRecord.$new();',
  11232. '']),
  11233. LinesToStr([
  11234. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11235. '$mod.Jv = $mod.Rec;',
  11236. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11237. '$mod.Fly($mod.Rec, $mod.Rec);',
  11238. 'if ($mod.Rec) ;',
  11239. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11240. '']));
  11241. end;
  11242. procedure TTestModule.TestRecord_VariantFail;
  11243. begin
  11244. StartProgram(false);
  11245. Add([
  11246. 'type',
  11247. ' TRec = record',
  11248. ' case word of',
  11249. ' 0: (b0, b1: Byte);',
  11250. ' 1: (i: word);',
  11251. ' end;',
  11252. 'begin']);
  11253. SetExpectedPasResolverError('variant record is not supported',
  11254. nXIsNotSupported);
  11255. ConvertProgram;
  11256. end;
  11257. procedure TTestModule.TestRecord_FieldArray;
  11258. begin
  11259. StartProgram(false);
  11260. Add([
  11261. 'type',
  11262. ' TArrInt = array[3..4] of longint;',
  11263. ' TArrArrInt = array[3..4] of longint;',
  11264. ' TRec = record',
  11265. ' a: array of longint;',
  11266. ' s: array[1..2] of longint;',
  11267. ' m: array[1..2,3..4] of longint;',
  11268. ' o: TArrArrInt;',
  11269. ' end;',
  11270. 'begin']);
  11271. ConvertProgram;
  11272. CheckSource('TestRecord_FieldArray',
  11273. LinesToStr([ // statements
  11274. 'rtl.recNewT(this, "TRec", function () {',
  11275. ' this.$new = function () {',
  11276. ' var r = Object.create(this);',
  11277. ' r.a = [];',
  11278. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11279. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11280. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11281. ' return r;',
  11282. ' };',
  11283. ' this.$eq = function (b) {',
  11284. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11285. ' };',
  11286. ' this.$assign = function (s) {',
  11287. ' this.a = rtl.arrayRef(s.a);',
  11288. ' this.s = s.s.slice(0);',
  11289. ' this.m = s.m.slice(0);',
  11290. ' this.o = s.o.slice(0);',
  11291. ' return this;',
  11292. ' };',
  11293. '});',
  11294. '']),
  11295. LinesToStr([ // $mod.$main
  11296. '']));
  11297. end;
  11298. procedure TTestModule.TestRecord_Const;
  11299. begin
  11300. StartProgram(false);
  11301. Add([
  11302. 'type',
  11303. ' TArrInt = array[3..4] of longint;',
  11304. ' TPoint = record x,y: longint; end;',
  11305. ' TRec = record',
  11306. ' i: longint;',
  11307. ' a: array of longint;',
  11308. ' s: array[1..2] of longint;',
  11309. ' m: array[1..2,3..4] of longint;',
  11310. ' p: TPoint;',
  11311. ' end;',
  11312. ' TPoints = array of TPoint;',
  11313. 'const',
  11314. ' r: TRec = (',
  11315. ' i:1;',
  11316. ' a:(2,3);',
  11317. ' s:(4,5);',
  11318. ' m:( (11,12), (13,14) );',
  11319. ' p: (x:21; y:22)',
  11320. ' );',
  11321. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11322. 'begin']);
  11323. ConvertProgram;
  11324. CheckSource('TestRecord_Const',
  11325. LinesToStr([ // statements
  11326. 'rtl.recNewT(this, "TPoint", function () {',
  11327. ' this.x = 0;',
  11328. ' this.y = 0;',
  11329. ' this.$eq = function (b) {',
  11330. ' return (this.x === b.x) && (this.y === b.y);',
  11331. ' };',
  11332. ' this.$assign = function (s) {',
  11333. ' this.x = s.x;',
  11334. ' this.y = s.y;',
  11335. ' return this;',
  11336. ' };',
  11337. '});',
  11338. 'rtl.recNewT(this, "TRec", function () {',
  11339. ' this.i = 0;',
  11340. ' this.$new = function () {',
  11341. ' var r = Object.create(this);',
  11342. ' r.a = [];',
  11343. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11344. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11345. ' r.p = $mod.TPoint.$new();',
  11346. ' return r;',
  11347. ' };',
  11348. ' this.$eq = function (b) {',
  11349. ' 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);',
  11350. ' };',
  11351. ' this.$assign = function (s) {',
  11352. ' this.i = s.i;',
  11353. ' this.a = rtl.arrayRef(s.a);',
  11354. ' this.s = s.s.slice(0);',
  11355. ' this.m = s.m.slice(0);',
  11356. ' this.p.$assign(s.p);',
  11357. ' return this;',
  11358. ' };',
  11359. '});',
  11360. 'this.r = this.TRec.$clone({',
  11361. ' i: 1,',
  11362. ' a: [2, 3],',
  11363. ' s: [4, 5],',
  11364. ' m: [[11, 12], [13, 14]],',
  11365. ' p: this.TPoint.$clone({',
  11366. ' x: 21,',
  11367. ' y: 22',
  11368. ' })',
  11369. '});',
  11370. 'this.p = [this.TPoint.$clone({',
  11371. ' x: 1,',
  11372. ' y: 2',
  11373. '}), this.TPoint.$clone({',
  11374. ' x: 3,',
  11375. ' y: 4',
  11376. '})];',
  11377. '']),
  11378. LinesToStr([ // $mod.$main
  11379. '']));
  11380. end;
  11381. procedure TTestModule.TestRecord_TypecastFail;
  11382. begin
  11383. StartProgram(false);
  11384. Add([
  11385. 'type',
  11386. ' TPoint = record x,y: longint; end;',
  11387. ' TRec = record l: longint end;',
  11388. 'var p: TPoint;',
  11389. 'begin',
  11390. ' if TRec(p).l=2 then ;']);
  11391. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11392. nIllegalTypeConversionTo);
  11393. ConvertProgram;
  11394. end;
  11395. procedure TTestModule.TestRecord_InFunction;
  11396. begin
  11397. StartProgram(false);
  11398. Add([
  11399. 'var TPoint: longint = 3;',
  11400. 'procedure DoIt;',
  11401. 'type',
  11402. ' TPoint = record x,y: longint; end;',
  11403. ' TPoints = array of TPoint;',
  11404. 'var',
  11405. ' r: TPoint;',
  11406. ' p: TPoints;',
  11407. 'begin',
  11408. ' SetLength(p,2);',
  11409. 'end;',
  11410. 'begin']);
  11411. ConvertProgram;
  11412. CheckSource('TestRecord_InFunction',
  11413. LinesToStr([ // statements
  11414. 'this.TPoint = 3;',
  11415. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11416. ' this.x = 0;',
  11417. ' this.y = 0;',
  11418. ' this.$eq = function (b) {',
  11419. ' return (this.x === b.x) && (this.y === b.y);',
  11420. ' };',
  11421. ' this.$assign = function (s) {',
  11422. ' this.x = s.x;',
  11423. ' this.y = s.y;',
  11424. ' return this;',
  11425. ' };',
  11426. '});',
  11427. 'this.DoIt = function () {',
  11428. ' var r = TPoint$1.$new();',
  11429. ' var p = [];',
  11430. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11431. '};',
  11432. '']),
  11433. LinesToStr([ // $mod.$main
  11434. '']));
  11435. end;
  11436. procedure TTestModule.TestRecord_AnonymousFail;
  11437. begin
  11438. StartProgram(false);
  11439. Add([
  11440. 'var',
  11441. ' r: record x: word end;',
  11442. 'begin']);
  11443. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11444. nNotYetImplemented);
  11445. ConvertProgram;
  11446. end;
  11447. procedure TTestModule.TestAdvRecord_Function;
  11448. begin
  11449. StartProgram(false);
  11450. Parser.Options:=Parser.Options+[po_cassignments];
  11451. Add([
  11452. '{$modeswitch AdvancedRecords}',
  11453. 'type',
  11454. ' TPoint = record',
  11455. ' x,y: word;',
  11456. ' function Add(const apt: TPoint): TPoint;',
  11457. ' end;',
  11458. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11459. 'begin',
  11460. ' Result:=Self;',
  11461. ' Result.x+=apt.x;',
  11462. ' Result.y:=Result.y+apt.y;',
  11463. ' Self:=apt;',
  11464. 'end;',
  11465. 'var p,q: TPoint;',
  11466. 'begin',
  11467. ' p.add(q);',
  11468. ' p:=default(TPoint);',
  11469. ' p:=q;',
  11470. '']);
  11471. ConvertProgram;
  11472. CheckSource('TestAdvRecord_Function',
  11473. LinesToStr([ // statements
  11474. 'rtl.recNewT(this, "TPoint", function () {',
  11475. ' this.x = 0;',
  11476. ' this.y = 0;',
  11477. ' this.$eq = function (b) {',
  11478. ' return (this.x === b.x) && (this.y === b.y);',
  11479. ' };',
  11480. ' this.$assign = function (s) {',
  11481. ' this.x = s.x;',
  11482. ' this.y = s.y;',
  11483. ' return this;',
  11484. ' };',
  11485. ' this.Add = function (apt) {',
  11486. ' var Result = $mod.TPoint.$new();',
  11487. ' Result.$assign(this);',
  11488. ' Result.x += apt.x;',
  11489. ' Result.y = Result.y + apt.y;',
  11490. ' this.$assign(apt);',
  11491. ' return Result;',
  11492. ' };',
  11493. '});',
  11494. 'this.p = this.TPoint.$new();',
  11495. 'this.q = this.TPoint.$new();',
  11496. '']),
  11497. LinesToStr([ // $mod.$main
  11498. '$mod.p.Add($mod.q);',
  11499. '$mod.p.$assign($mod.TPoint.$new());',
  11500. '$mod.p.$assign($mod.q);',
  11501. '']));
  11502. end;
  11503. procedure TTestModule.TestAdvRecord_Property;
  11504. begin
  11505. StartProgram(false);
  11506. Add([
  11507. '{$modeswitch AdvancedRecords}',
  11508. 'type',
  11509. ' TPoint = record',
  11510. ' x,y: word;',
  11511. ' strict private',
  11512. ' function GetSize: longword;',
  11513. ' procedure SetSize(Value: longword);',
  11514. ' public',
  11515. ' property Size: longword read GetSize write SetSize;',
  11516. ' property Left: word read x write y;',
  11517. ' end;',
  11518. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11519. 'function TPoint.GetSize: longword;',
  11520. 'begin',
  11521. ' x:=y;',
  11522. ' Size:=Size;',
  11523. ' Left:=Left;',
  11524. 'end;',
  11525. 'procedure TPoint.SetSize(Value: longword);',
  11526. 'begin',
  11527. 'end;',
  11528. 'var p,q: TPoint;',
  11529. 'begin',
  11530. ' p.Size:=q.Size;',
  11531. ' p.Left:=q.Left;',
  11532. '']);
  11533. ConvertProgram;
  11534. CheckSource('TestAdvRecord_Property',
  11535. LinesToStr([ // statements
  11536. 'rtl.recNewT(this, "TPoint", function () {',
  11537. ' this.x = 0;',
  11538. ' this.y = 0;',
  11539. ' this.$eq = function (b) {',
  11540. ' return (this.x === b.x) && (this.y === b.y);',
  11541. ' };',
  11542. ' this.$assign = function (s) {',
  11543. ' this.x = s.x;',
  11544. ' this.y = s.y;',
  11545. ' return this;',
  11546. ' };',
  11547. ' this.GetSize = function () {',
  11548. ' var Result = 0;',
  11549. ' this.x = this.y;',
  11550. ' this.SetSize(this.GetSize());',
  11551. ' this.y = this.x;',
  11552. ' return Result;',
  11553. ' };',
  11554. ' this.SetSize = function (Value) {',
  11555. ' };',
  11556. '});',
  11557. 'this.SetSize = function (Value) {',
  11558. '};',
  11559. 'this.p = this.TPoint.$new();',
  11560. 'this.q = this.TPoint.$new();',
  11561. '']),
  11562. LinesToStr([ // $mod.$main
  11563. '$mod.p.SetSize($mod.q.GetSize());',
  11564. '$mod.p.y = $mod.q.x;',
  11565. '']));
  11566. end;
  11567. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11568. begin
  11569. StartProgram(false);
  11570. Add([
  11571. '{$modeswitch AdvancedRecords}',
  11572. 'type',
  11573. ' TPoint = record',
  11574. ' strict private',
  11575. ' function GetItems(Index: word): word;',
  11576. ' procedure SetItems(Index: word; Value: word);',
  11577. ' public',
  11578. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11579. ' end;',
  11580. 'function TPoint.GetItems(Index: word): word;',
  11581. 'begin',
  11582. ' Items[index]:=Items[index];',
  11583. ' self.Items[index]:=self.Items[index];',
  11584. 'end;',
  11585. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11586. 'begin',
  11587. 'end;',
  11588. 'var p: TPoint;',
  11589. 'begin',
  11590. ' p[1]:=p[2];',
  11591. ' p.Items[3]:=p.Items[4];',
  11592. '']);
  11593. ConvertProgram;
  11594. CheckSource('TestAdvRecord_PropertyDefault',
  11595. LinesToStr([ // statements
  11596. 'rtl.recNewT(this, "TPoint", function () {',
  11597. ' this.$eq = function (b) {',
  11598. ' return true;',
  11599. ' };',
  11600. ' this.$assign = function (s) {',
  11601. ' return this;',
  11602. ' };',
  11603. ' this.GetItems = function (Index) {',
  11604. ' var Result = 0;',
  11605. ' this.SetItems(Index, this.GetItems(Index));',
  11606. ' this.SetItems(Index, this.GetItems(Index));',
  11607. ' return Result;',
  11608. ' };',
  11609. ' this.SetItems = function (Index, Value) {',
  11610. ' };',
  11611. '});',
  11612. 'this.p = this.TPoint.$new();',
  11613. '']),
  11614. LinesToStr([ // $mod.$main
  11615. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11616. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11617. '']));
  11618. end;
  11619. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11620. begin
  11621. StartProgram(false);
  11622. Add([
  11623. '{$modeswitch AdvancedRecords}',
  11624. 'type',
  11625. ' TRec = record',
  11626. ' class var',
  11627. ' Fx: longint;',
  11628. ' Fy: longint;',
  11629. ' class function GetInt: longint; static;',
  11630. ' class procedure SetInt(Value: longint); static;',
  11631. ' class procedure DoIt; static;',
  11632. ' class property IntA: longint read Fx write Fy;',
  11633. ' class property IntB: longint read GetInt write SetInt;',
  11634. ' end;',
  11635. 'class function trec.getint: longint;',
  11636. 'begin',
  11637. ' result:=fx;',
  11638. 'end;',
  11639. 'class procedure trec.setint(value: longint);',
  11640. 'begin',
  11641. 'end;',
  11642. 'class procedure trec.doit;',
  11643. 'begin',
  11644. ' IntA:=IntA+1;',
  11645. ' IntB:=IntB+1;',
  11646. 'end;',
  11647. 'var r: trec;',
  11648. 'begin',
  11649. ' trec.inta:=trec.inta+1;',
  11650. ' if trec.intb=2 then;',
  11651. ' trec.intb:=trec.intb+2;',
  11652. ' trec.setint(trec.inta);',
  11653. ' r.inta:=r.inta+1;',
  11654. ' if r.intb=2 then;',
  11655. ' r.intb:=r.intb+2;',
  11656. ' r.setint(r.inta);']);
  11657. ConvertProgram;
  11658. CheckSource('TestAdvRecord_Property_ClassMethod',
  11659. LinesToStr([ // statements
  11660. 'rtl.recNewT(this, "TRec", function () {',
  11661. ' this.Fx = 0;',
  11662. ' this.Fy = 0;',
  11663. ' this.$eq = function (b) {',
  11664. ' return true;',
  11665. ' };',
  11666. ' this.$assign = function (s) {',
  11667. ' return this;',
  11668. ' };',
  11669. ' this.GetInt = function () {',
  11670. ' var Result = 0;',
  11671. ' Result = $mod.TRec.Fx;',
  11672. ' return Result;',
  11673. ' };',
  11674. ' this.SetInt = function (Value) {',
  11675. ' };',
  11676. ' this.DoIt = function () {',
  11677. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11678. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11679. ' };',
  11680. '}, true);',
  11681. 'this.r = this.TRec.$new();',
  11682. '']),
  11683. LinesToStr([ // $mod.$main
  11684. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11685. 'if ($mod.TRec.GetInt() === 2) ;',
  11686. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11687. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11688. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11689. 'if ($mod.r.GetInt() === 2) ;',
  11690. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11691. '$mod.r.SetInt($mod.r.Fx);',
  11692. '']));
  11693. end;
  11694. procedure TTestModule.TestAdvRecord_Const;
  11695. begin
  11696. StartProgram(false);
  11697. Add([
  11698. '{$modeswitch AdvancedRecords}',
  11699. 'type',
  11700. ' TArrInt = array[3..4] of longint;',
  11701. ' TPoint = record',
  11702. ' x,y: longint;',
  11703. ' class var Count: nativeint;',
  11704. ' end;',
  11705. ' TRec = record',
  11706. ' i: longint;',
  11707. ' a: array of longint;',
  11708. ' s: array[1..2] of longint;',
  11709. ' m: array[1..2,3..4] of longint;',
  11710. ' p: TPoint;',
  11711. ' end;',
  11712. ' TPoints = array of TPoint;',
  11713. 'const',
  11714. ' r: TRec = (',
  11715. ' i:1;',
  11716. ' a:(2,3);',
  11717. ' s:(4,5);',
  11718. ' m:( (11,12), (13,14) );',
  11719. ' p: (x:21)',
  11720. ' );',
  11721. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11722. 'begin']);
  11723. ConvertProgram;
  11724. CheckSource('TestAdvRecord_Const',
  11725. LinesToStr([ // statements
  11726. 'rtl.recNewT(this, "TPoint", function () {',
  11727. ' this.x = 0;',
  11728. ' this.y = 0;',
  11729. ' this.Count = 0;',
  11730. ' this.$eq = function (b) {',
  11731. ' return (this.x === b.x) && (this.y === b.y);',
  11732. ' };',
  11733. ' this.$assign = function (s) {',
  11734. ' this.x = s.x;',
  11735. ' this.y = s.y;',
  11736. ' return this;',
  11737. ' };',
  11738. '}, true);',
  11739. 'rtl.recNewT(this, "TRec", function () {',
  11740. ' this.i = 0;',
  11741. ' this.$new = function () {',
  11742. ' var r = Object.create(this);',
  11743. ' r.a = [];',
  11744. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11745. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11746. ' r.p = $mod.TPoint.$new();',
  11747. ' return r;',
  11748. ' };',
  11749. ' this.$eq = function (b) {',
  11750. ' 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);',
  11751. ' };',
  11752. ' this.$assign = function (s) {',
  11753. ' this.i = s.i;',
  11754. ' this.a = rtl.arrayRef(s.a);',
  11755. ' this.s = s.s.slice(0);',
  11756. ' this.m = s.m.slice(0);',
  11757. ' this.p.$assign(s.p);',
  11758. ' return this;',
  11759. ' };',
  11760. '});',
  11761. 'this.r = this.TRec.$clone({',
  11762. ' i: 1,',
  11763. ' a: [2, 3],',
  11764. ' s: [4, 5],',
  11765. ' m: [[11, 12], [13, 14]],',
  11766. ' p: this.TPoint.$clone({',
  11767. ' x: 21,',
  11768. ' y: 0',
  11769. ' })',
  11770. '});',
  11771. 'this.p = [this.TPoint.$clone({',
  11772. ' x: 1,',
  11773. ' y: 2',
  11774. '}), this.TPoint.$clone({',
  11775. ' x: 3,',
  11776. ' y: 4',
  11777. '})];',
  11778. '']),
  11779. LinesToStr([ // $mod.$main
  11780. '']));
  11781. end;
  11782. procedure TTestModule.TestAdvRecord_ExternalField;
  11783. begin
  11784. StartProgram(false);
  11785. Add([
  11786. '{$modeswitch AdvancedRecords}',
  11787. '{$modeswitch externalclass}',
  11788. 'type',
  11789. ' TCar = record',
  11790. ' public',
  11791. ' Intern: longint external name ''$Intern'';',
  11792. ' Intern2: longint external name ''$Intern2'';',
  11793. ' Bracket: longint external name ''["A B"]'';',
  11794. ' procedure DoIt;',
  11795. ' end;',
  11796. 'procedure tcar.doit;',
  11797. 'begin',
  11798. ' Intern:=Intern+1;',
  11799. ' Intern2:=Intern2+2;',
  11800. ' Bracket:=Bracket+3;',
  11801. 'end;',
  11802. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11803. 'begin',
  11804. ' Rec.intern:=Rec.intern+1;',
  11805. ' Rec.intern2:=Rec.intern2+2;',
  11806. ' Rec.Bracket:=Rec.Bracket+3;',
  11807. ' with Rec do begin',
  11808. ' intern:=intern+1;',
  11809. ' intern2:=intern2+2;',
  11810. ' Bracket:=Bracket+3;',
  11811. ' end;']);
  11812. ConvertProgram;
  11813. CheckSource('TestAdvRecord_ExternalField',
  11814. LinesToStr([ // statements
  11815. 'rtl.recNewT(this, "TCar", function () {',
  11816. ' this.$eq = function (b) {',
  11817. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11818. ' };',
  11819. ' this.$assign = function (s) {',
  11820. ' this.$Intern = s.$Intern;',
  11821. ' this.$Intern2 = s.$Intern2;',
  11822. ' this["A B"] = s["A B"];',
  11823. ' return this;',
  11824. ' };',
  11825. ' this.DoIt = function () {',
  11826. ' this.$Intern = this.$Intern + 1;',
  11827. ' this.$Intern2 = this.$Intern2 + 2;',
  11828. ' this["A B"] = this["A B"] + 3;',
  11829. ' };',
  11830. '});',
  11831. 'this.Rec = this.TCar.$clone({',
  11832. ' $Intern: 11,',
  11833. ' $Intern2: 12,',
  11834. ' "A B": 13',
  11835. '});',
  11836. '']),
  11837. LinesToStr([ // $mod.$main
  11838. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11839. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11840. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11841. 'var $with = $mod.Rec;',
  11842. '$with.$Intern = $with.$Intern + 1;',
  11843. '$with.$Intern2 = $with.$Intern2 + 2;',
  11844. '$with["A B"] = $with["A B"] + 3;',
  11845. '']));
  11846. end;
  11847. procedure TTestModule.TestAdvRecord_SubRecord;
  11848. begin
  11849. StartProgram(false);
  11850. Add([
  11851. '{$modeswitch AdvancedRecords}',
  11852. 'type',
  11853. ' TRec = record',
  11854. ' type',
  11855. ' TPoint = record',
  11856. ' x,y: longint;',
  11857. ' class var Count: nativeint;',
  11858. ' procedure DoIt;',
  11859. ' class procedure DoThat; static;',
  11860. ' end;',
  11861. ' var',
  11862. ' i: longint;',
  11863. ' p: TPoint;',
  11864. ' procedure DoSome;',
  11865. ' end;',
  11866. 'const',
  11867. ' r: TRec = (',
  11868. ' i:1;',
  11869. ' p: (x:21;y:22)',
  11870. ' );',
  11871. 'procedure TRec.DoSome;',
  11872. 'begin',
  11873. ' p.x:=p.y+1;',
  11874. ' p.Count:=p.Count+2;',
  11875. 'end;',
  11876. 'procedure TRec.TPoint.DoIt;',
  11877. 'begin',
  11878. ' Count:=Count+3;',
  11879. 'end;',
  11880. 'class procedure TRec.TPoint.DoThat;',
  11881. 'begin',
  11882. ' Count:=Count+4;',
  11883. 'end;',
  11884. 'begin']);
  11885. ConvertProgram;
  11886. CheckSource('TestAdvRecord_SubRecord',
  11887. LinesToStr([ // statements
  11888. 'rtl.recNewT(this, "TRec", function () {',
  11889. ' rtl.recNewT(this, "TPoint", function () {',
  11890. ' this.x = 0;',
  11891. ' this.y = 0;',
  11892. ' this.Count = 0;',
  11893. ' this.$eq = function (b) {',
  11894. ' return (this.x === b.x) && (this.y === b.y);',
  11895. ' };',
  11896. ' this.$assign = function (s) {',
  11897. ' this.x = s.x;',
  11898. ' this.y = s.y;',
  11899. ' return this;',
  11900. ' };',
  11901. ' this.DoIt = function () {',
  11902. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11903. ' };',
  11904. ' this.DoThat = function () {',
  11905. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11906. ' };',
  11907. ' }, true);',
  11908. ' this.i = 0;',
  11909. ' this.$new = function () {',
  11910. ' var r = Object.create(this);',
  11911. ' r.p = this.TPoint.$new();',
  11912. ' return r;',
  11913. ' };',
  11914. ' this.$eq = function (b) {',
  11915. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11916. ' };',
  11917. ' this.$assign = function (s) {',
  11918. ' this.i = s.i;',
  11919. ' this.p.$assign(s.p);',
  11920. ' return this;',
  11921. ' };',
  11922. ' this.DoSome = function () {',
  11923. ' this.p.x = this.p.y + 1;',
  11924. ' this.TPoint.Count = this.p.Count + 2;',
  11925. ' };',
  11926. '}, true);',
  11927. 'this.r = this.TRec.$clone({',
  11928. ' i: 1,',
  11929. ' p: this.TRec.TPoint.$clone({',
  11930. ' x: 21,',
  11931. ' y: 22',
  11932. ' })',
  11933. '});',
  11934. '']),
  11935. LinesToStr([ // $mod.$main
  11936. '']));
  11937. end;
  11938. procedure TTestModule.TestAdvRecord_SubClass;
  11939. begin
  11940. StartProgram(false);
  11941. Add([
  11942. '{$modeswitch AdvancedRecords}',
  11943. 'type',
  11944. ' TObject = class end;',
  11945. ' TPoint = record',
  11946. ' type',
  11947. ' TBird = class',
  11948. ' procedure DoIt;',
  11949. ' class procedure Glob;',
  11950. ' end;',
  11951. ' procedure DoIt(b: TBird);',
  11952. ' end;',
  11953. 'procedure TPoint.TBird.DoIt;',
  11954. 'begin',
  11955. ' doit;',
  11956. ' self.doit;',
  11957. ' glob;',
  11958. ' self.glob;',
  11959. 'end;',
  11960. 'class procedure TPoint.TBird.Glob;',
  11961. 'begin',
  11962. ' glob;',
  11963. ' self.glob;',
  11964. 'end;',
  11965. 'procedure TPoint.DoIt(b: TBird);',
  11966. 'begin',
  11967. ' b.doit;',
  11968. ' b.glob;',
  11969. ' TBird.glob;',
  11970. 'end;',
  11971. 'begin',
  11972. '']);
  11973. ConvertProgram;
  11974. CheckSource('TestAdvRecord_SubClass',
  11975. LinesToStr([ // statements
  11976. 'rtl.createClass(this, "TObject", null, function () {',
  11977. ' this.$init = function () {',
  11978. ' };',
  11979. ' this.$final = function () {',
  11980. ' };',
  11981. '});',
  11982. 'rtl.recNewT(this, "TPoint", function () {',
  11983. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11984. ' this.DoIt = function () {',
  11985. ' this.DoIt();',
  11986. ' this.DoIt();',
  11987. ' this.$class.Glob();',
  11988. ' this.$class.Glob();',
  11989. ' };',
  11990. ' this.Glob = function () {',
  11991. ' this.Glob();',
  11992. ' this.Glob();',
  11993. ' };',
  11994. ' }, "TPoint.TBird");',
  11995. ' this.$eq = function (b) {',
  11996. ' return true;',
  11997. ' };',
  11998. ' this.$assign = function (s) {',
  11999. ' return this;',
  12000. ' };',
  12001. ' this.DoIt = function (b) {',
  12002. ' b.DoIt();',
  12003. ' b.$class.Glob();',
  12004. ' this.TBird.Glob();',
  12005. ' };',
  12006. '}, true);',
  12007. '']),
  12008. LinesToStr([ // $mod.$main
  12009. '']));
  12010. end;
  12011. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12012. begin
  12013. StartProgram(false);
  12014. Add([
  12015. '{$modeswitch AdvancedRecords}',
  12016. 'type',
  12017. ' IUnknown = interface end;',
  12018. ' TPoint = record',
  12019. ' type IBird = interface end;',
  12020. ' end;',
  12021. 'begin',
  12022. '']);
  12023. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12024. nNotYetImplemented);
  12025. ParseProgram;
  12026. end;
  12027. procedure TTestModule.TestAdvRecord_Constructor;
  12028. begin
  12029. StartProgram(false);
  12030. Add([
  12031. '{$modeswitch AdvancedRecords}',
  12032. 'type',
  12033. ' TPoint = record',
  12034. ' x,y: longint;',
  12035. ' class procedure Run(w: longint = 13); static;',
  12036. ' constructor Create(ax: longint; ay: longint = -1);',
  12037. ' end;',
  12038. 'class procedure tpoint.run(w: longint);',
  12039. 'begin',
  12040. ' run;',
  12041. ' run();',
  12042. 'end;',
  12043. 'constructor tpoint.create(ax,ay: longint);',
  12044. 'begin',
  12045. ' x:=ax;',
  12046. ' self.y:=ay;',
  12047. ' run;',
  12048. ' run(ax);',
  12049. 'end;',
  12050. 'var r: TPoint;',
  12051. 'begin',
  12052. ' r:=TPoint.Create(1,2);',
  12053. ' with TPoint do r:=Create(1,2);',
  12054. ' r.Create(3);',
  12055. ' r:=r.Create(4);',
  12056. '']);
  12057. ConvertProgram;
  12058. CheckSource('TestAdvRecord_Constructor',
  12059. LinesToStr([ // statements
  12060. 'rtl.recNewT(this, "TPoint", function () {',
  12061. ' this.x = 0;',
  12062. ' this.y = 0;',
  12063. ' this.$eq = function (b) {',
  12064. ' return (this.x === b.x) && (this.y === b.y);',
  12065. ' };',
  12066. ' this.$assign = function (s) {',
  12067. ' this.x = s.x;',
  12068. ' this.y = s.y;',
  12069. ' return this;',
  12070. ' };',
  12071. ' this.Run = function (w) {',
  12072. ' $mod.TPoint.Run(13);',
  12073. ' $mod.TPoint.Run(13);',
  12074. ' };',
  12075. ' this.Create = function (ax, ay) {',
  12076. ' this.x = ax;',
  12077. ' this.y = ay;',
  12078. ' this.Run(13);',
  12079. ' this.Run(ax);',
  12080. ' return this;',
  12081. ' };',
  12082. '});',
  12083. 'this.r = this.TPoint.$new();',
  12084. '']),
  12085. LinesToStr([ // $mod.$main
  12086. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12087. 'var $with = $mod.TPoint;',
  12088. '$mod.r.$assign($with.$new().Create(1, 2));',
  12089. '$mod.r.Create(3, -1);',
  12090. '$mod.r.$assign($mod.r.Create(4, -1));',
  12091. '']));
  12092. end;
  12093. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12094. begin
  12095. StartProgram(false);
  12096. Add([
  12097. '{$modeswitch AdvancedRecords}',
  12098. 'type',
  12099. ' TPoint = record',
  12100. ' class var x: longint;',
  12101. ' class procedure Fly; static;',
  12102. ' class constructor Init;',
  12103. ' end;',
  12104. 'var count: word;',
  12105. 'class procedure Tpoint.Fly;',
  12106. 'begin',
  12107. 'end;',
  12108. 'class constructor tpoint.init;',
  12109. 'begin',
  12110. ' count:=count+1;',
  12111. ' x:=x+3;',
  12112. ' tpoint.x:=tpoint.x+4;',
  12113. ' fly;',
  12114. ' tpoint.fly;',
  12115. 'end;',
  12116. 'var r: TPoint;',
  12117. 'begin',
  12118. ' r.x:=r.x+10;',
  12119. ' r.Fly;',
  12120. ' r.Fly();',
  12121. '']);
  12122. ConvertProgram;
  12123. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12124. LinesToStr([ // statements
  12125. 'rtl.recNewT(this, "TPoint", function () {',
  12126. ' this.x = 0;',
  12127. ' this.$eq = function (b) {',
  12128. ' return true;',
  12129. ' };',
  12130. ' this.$assign = function (s) {',
  12131. ' return this;',
  12132. ' };',
  12133. ' this.Fly = function () {',
  12134. ' };',
  12135. '}, true);',
  12136. 'this.count = 0;',
  12137. 'this.r = this.TPoint.$new();',
  12138. '']),
  12139. LinesToStr([ // $mod.$main
  12140. '(function () {',
  12141. ' $mod.count = $mod.count + 1;',
  12142. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12143. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12144. ' $mod.TPoint.Fly();',
  12145. ' $mod.TPoint.Fly();',
  12146. '})();',
  12147. '$mod.TPoint.x = $mod.r.x + 10;',
  12148. '$mod.r.Fly();',
  12149. '$mod.r.Fly();',
  12150. '']));
  12151. end;
  12152. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12153. begin
  12154. StartUnit(false);
  12155. Add([
  12156. 'interface',
  12157. '{$modeswitch AdvancedRecords}',
  12158. 'type',
  12159. ' TPoint = record',
  12160. ' class var x: longint;',
  12161. ' class procedure Fly; static;',
  12162. ' class constructor Init;',
  12163. ' end;',
  12164. 'implementation',
  12165. 'var count: word;',
  12166. 'class procedure Tpoint.Fly;',
  12167. 'begin',
  12168. 'end;',
  12169. 'class constructor tpoint.init;',
  12170. 'begin',
  12171. ' count:=count+1;',
  12172. ' x:=3;',
  12173. ' tpoint.x:=4;',
  12174. ' fly;',
  12175. ' tpoint.fly;',
  12176. 'end;',
  12177. '']);
  12178. ConvertUnit;
  12179. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12180. LinesToStr([ // statements
  12181. 'var $impl = $mod.$impl;',
  12182. 'rtl.recNewT(this, "TPoint", function () {',
  12183. ' this.x = 0;',
  12184. ' this.$eq = function (b) {',
  12185. ' return true;',
  12186. ' };',
  12187. ' this.$assign = function (s) {',
  12188. ' return this;',
  12189. ' };',
  12190. ' this.Fly = function () {',
  12191. ' };',
  12192. '}, true);',
  12193. '']),
  12194. LinesToStr([ // $mod.$init
  12195. '(function () {',
  12196. ' $impl.count = $impl.count + 1;',
  12197. ' $mod.TPoint.x = 3;',
  12198. ' $mod.TPoint.x = 4;',
  12199. ' $mod.TPoint.Fly();',
  12200. ' $mod.TPoint.Fly();',
  12201. '})();',
  12202. '']),
  12203. LinesToStr([ // $mod.$main
  12204. '$impl.count = 0;',
  12205. '']));
  12206. end;
  12207. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12208. begin
  12209. StartProgram(false);
  12210. Add(['type',
  12211. ' TObject = class',
  12212. ' public',
  12213. ' constructor Create;',
  12214. ' destructor Destroy;',
  12215. ' end;',
  12216. ' TBird = TObject;',
  12217. 'constructor tobject.create;',
  12218. 'begin end;',
  12219. 'destructor tobject.destroy;',
  12220. 'begin end;',
  12221. 'var Obj: tobject;',
  12222. 'begin',
  12223. ' obj:=tobject.create;',
  12224. ' obj:=tobject.create();',
  12225. ' obj:=tbird.create;',
  12226. ' obj:=tbird.create();',
  12227. ' obj:=obj.create();',
  12228. ' obj.destroy;',
  12229. '']);
  12230. ConvertProgram;
  12231. CheckSource('TestClass_TObjectDefaultConstructor',
  12232. LinesToStr([ // statements
  12233. 'rtl.createClass(this,"TObject",null,function(){',
  12234. ' this.$init = function () {',
  12235. ' };',
  12236. ' this.$final = function () {',
  12237. ' };',
  12238. ' this.Create = function(){',
  12239. ' return this;',
  12240. ' };',
  12241. ' this.Destroy = function(){',
  12242. ' };',
  12243. '});',
  12244. 'this.Obj = null;'
  12245. ]),
  12246. LinesToStr([ // $mod.$main
  12247. '$mod.Obj = $mod.TObject.$create("Create");',
  12248. '$mod.Obj = $mod.TObject.$create("Create");',
  12249. '$mod.Obj = $mod.TObject.$create("Create");',
  12250. '$mod.Obj = $mod.TObject.$create("Create");',
  12251. '$mod.Obj = $mod.Obj.Create();',
  12252. '$mod.Obj.$destroy("Destroy");',
  12253. '']));
  12254. end;
  12255. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12256. begin
  12257. StartProgram(false);
  12258. Add('type');
  12259. Add(' TObject = class');
  12260. Add(' public');
  12261. Add(' constructor Create(Par: longint);');
  12262. Add(' end;');
  12263. Add('constructor tobject.create(par: longint);');
  12264. Add('begin end;');
  12265. Add('var Obj: tobject;');
  12266. Add('begin');
  12267. Add(' obj:=tobject.create(3);');
  12268. ConvertProgram;
  12269. CheckSource('TestClass_TObjectConstructorWithParams',
  12270. LinesToStr([ // statements
  12271. 'rtl.createClass(this,"TObject",null,function(){',
  12272. ' this.$init = function () {',
  12273. ' };',
  12274. ' this.$final = function () {',
  12275. ' };',
  12276. ' this.Create = function(Par){',
  12277. ' return this;',
  12278. ' };',
  12279. '});',
  12280. 'this.Obj = null;'
  12281. ]),
  12282. LinesToStr([ // $mod.$main
  12283. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12284. ]));
  12285. end;
  12286. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12287. begin
  12288. StartProgram(false);
  12289. Add('type');
  12290. Add(' TObject = class');
  12291. Add(' public');
  12292. Add(' constructor Create;');
  12293. Add(' end;');
  12294. Add(' TTest = class(TObject)');
  12295. Add(' public');
  12296. Add(' constructor Create(const Par: longint = 1);');
  12297. Add(' end;');
  12298. Add('constructor tobject.create;');
  12299. Add('begin end;');
  12300. Add('constructor ttest.create(const par: longint);');
  12301. Add('begin end;');
  12302. Add('var t: ttest;');
  12303. Add('begin');
  12304. Add(' t:=ttest.create;');
  12305. Add(' t:=ttest.create(2);');
  12306. ConvertProgram;
  12307. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12308. LinesToStr([ // statements
  12309. 'rtl.createClass(this,"TObject",null,function(){',
  12310. ' this.$init = function () {',
  12311. ' };',
  12312. ' this.$final = function () {',
  12313. ' };',
  12314. ' this.Create = function(){',
  12315. ' return this;',
  12316. ' };',
  12317. '});',
  12318. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12319. ' this.Create$1 = function (Par) {',
  12320. ' return this;',
  12321. ' };',
  12322. '});',
  12323. 'this.t = null;'
  12324. ]),
  12325. LinesToStr([ // $mod.$main
  12326. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12327. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12328. ]));
  12329. end;
  12330. procedure TTestModule.TestClass_Var;
  12331. begin
  12332. StartProgram(false);
  12333. Add([
  12334. 'type',
  12335. ' TObject = class',
  12336. ' public',
  12337. ' vI: longint;',
  12338. ' constructor Create(Par: longint);',
  12339. ' end;',
  12340. 'constructor tobject.create(par: longint);',
  12341. 'begin',
  12342. ' vi:=par+3',
  12343. 'end;',
  12344. 'var Obj: tobject;',
  12345. 'begin',
  12346. ' obj:=tobject.create(4);',
  12347. ' obj.vi:=obj.VI+5;']);
  12348. ConvertProgram;
  12349. CheckSource('TestClass_Var',
  12350. LinesToStr([ // statements
  12351. 'rtl.createClass(this,"TObject",null,function(){',
  12352. ' this.$init = function () {',
  12353. ' this.vI = 0;',
  12354. ' };',
  12355. ' this.$final = function () {',
  12356. ' };',
  12357. ' this.Create = function(Par){',
  12358. ' this.vI = Par+3;',
  12359. ' return this;',
  12360. ' };',
  12361. '});',
  12362. 'this.Obj = null;'
  12363. ]),
  12364. LinesToStr([ // $mod.$main
  12365. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12366. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12367. ]));
  12368. end;
  12369. procedure TTestModule.TestClass_Method;
  12370. begin
  12371. StartProgram(false);
  12372. Add('type');
  12373. Add(' TObject = class');
  12374. Add(' public');
  12375. Add(' vI: longint;');
  12376. Add(' Sub: TObject;');
  12377. Add(' constructor Create;');
  12378. Add(' function GetIt(Par: longint): tobject;');
  12379. Add(' end;');
  12380. Add('constructor tobject.create; begin end;');
  12381. Add('function tobject.getit(par: longint): tobject;');
  12382. Add('begin');
  12383. Add(' Self.vi:=par+3;');
  12384. Add(' Result:=self.sub;');
  12385. Add('end;');
  12386. Add('var Obj: tobject;');
  12387. Add('begin');
  12388. Add(' obj:=tobject.create;');
  12389. Add(' obj.getit(4);');
  12390. Add(' obj.sub.sub:=nil;');
  12391. Add(' obj.sub.getit(5);');
  12392. Add(' obj.sub.getit(6).SUB:=nil;');
  12393. Add(' obj.sub.getit(7).GETIT(8);');
  12394. Add(' obj.sub.getit(9).SuB.getit(10);');
  12395. ConvertProgram;
  12396. CheckSource('TestClass_Method',
  12397. LinesToStr([ // statements
  12398. 'rtl.createClass(this,"TObject",null,function(){',
  12399. ' this.$init = function () {',
  12400. ' this.vI = 0;',
  12401. ' this.Sub = null;',
  12402. ' };',
  12403. ' this.$final = function () {',
  12404. ' this.Sub = undefined;',
  12405. ' };',
  12406. ' this.Create = function(){',
  12407. ' return this;',
  12408. ' };',
  12409. ' this.GetIt = function(Par){',
  12410. ' var Result = null;',
  12411. ' this.vI = Par + 3;',
  12412. ' Result = this.Sub;',
  12413. ' return Result;',
  12414. ' };',
  12415. '});',
  12416. 'this.Obj = null;'
  12417. ]),
  12418. LinesToStr([ // $mod.$main
  12419. '$mod.Obj = $mod.TObject.$create("Create");',
  12420. '$mod.Obj.GetIt(4);',
  12421. '$mod.Obj.Sub.Sub=null;',
  12422. '$mod.Obj.Sub.GetIt(5);',
  12423. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12424. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12425. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12426. ]));
  12427. end;
  12428. procedure TTestModule.TestClass_Implementation;
  12429. begin
  12430. StartUnit(false);
  12431. Add([
  12432. 'interface',
  12433. 'type',
  12434. ' TObject = class',
  12435. ' constructor Create;',
  12436. ' end;',
  12437. 'implementation',
  12438. 'type',
  12439. ' TIntClass = class',
  12440. ' constructor Create; reintroduce;',
  12441. ' class procedure DoGlob;',
  12442. ' end;',
  12443. 'constructor tintclass.create;',
  12444. 'begin',
  12445. ' inherited;',
  12446. ' inherited create;',
  12447. ' doglob;',
  12448. 'end;',
  12449. 'class procedure tintclass.doglob;',
  12450. 'begin',
  12451. 'end;',
  12452. 'constructor tobject.create;',
  12453. 'var',
  12454. ' iC: tintclass;',
  12455. 'begin',
  12456. ' ic:=tintclass.create;',
  12457. ' tintclass.doglob;',
  12458. ' ic.doglob;',
  12459. 'end;',
  12460. 'initialization',
  12461. ' tintclass.doglob;',
  12462. '']);
  12463. ConvertUnit;
  12464. CheckSource('TestClass_Implementation',
  12465. LinesToStr([ // statements
  12466. 'var $impl = $mod.$impl;',
  12467. 'rtl.createClass(this, "TObject", null, function () {',
  12468. ' this.$init = function () {',
  12469. ' };',
  12470. ' this.$final = function () {',
  12471. ' };',
  12472. ' this.Create = function () {',
  12473. ' var iC = null;',
  12474. ' iC = $impl.TIntClass.$create("Create$1");',
  12475. ' $impl.TIntClass.DoGlob();',
  12476. ' iC.$class.DoGlob();',
  12477. ' return this;',
  12478. ' };',
  12479. '});',
  12480. '']),
  12481. LinesToStr([ // $mod.$main
  12482. '$impl.TIntClass.DoGlob();',
  12483. '']),
  12484. LinesToStr([
  12485. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12486. ' this.Create$1 = function () {',
  12487. ' $mod.TObject.Create.call(this);',
  12488. ' $mod.TObject.Create.call(this);',
  12489. ' this.$class.DoGlob();',
  12490. ' return this;',
  12491. ' };',
  12492. ' this.DoGlob = function () {',
  12493. ' };',
  12494. '});',
  12495. '']));
  12496. end;
  12497. procedure TTestModule.TestClass_Inheritance;
  12498. begin
  12499. StartProgram(false);
  12500. Add('type');
  12501. Add(' TObject = class');
  12502. Add(' public');
  12503. Add(' constructor Create;');
  12504. Add(' end;');
  12505. Add(' TClassA = class');
  12506. Add(' end;');
  12507. Add(' TClassB = class(TObject)');
  12508. Add(' procedure ProcB;');
  12509. Add(' end;');
  12510. Add('constructor tobject.create; begin end;');
  12511. Add('procedure tclassb.procb; begin end;');
  12512. Add('var');
  12513. Add(' oO: TObject;');
  12514. Add(' oA: TClassA;');
  12515. Add(' oB: TClassB;');
  12516. Add('begin');
  12517. Add(' oO:=tobject.Create;');
  12518. Add(' oA:=tclassa.Create;');
  12519. Add(' ob:=tclassb.Create;');
  12520. Add(' if oo is tclassa then ;');
  12521. Add(' ob:=oo as tclassb;');
  12522. Add(' (oo as tclassb).procb;');
  12523. ConvertProgram;
  12524. CheckSource('TestClass_Inheritance',
  12525. LinesToStr([ // statements
  12526. 'rtl.createClass(this,"TObject",null,function(){',
  12527. ' this.$init = function () {',
  12528. ' };',
  12529. ' this.$final = function () {',
  12530. ' };',
  12531. ' this.Create = function () {',
  12532. ' return this;',
  12533. ' };',
  12534. '});',
  12535. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12536. '});',
  12537. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12538. ' this.ProcB = function () {',
  12539. ' };',
  12540. '});',
  12541. 'this.oO = null;',
  12542. 'this.oA = null;',
  12543. 'this.oB = null;'
  12544. ]),
  12545. LinesToStr([ // $mod.$main
  12546. '$mod.oO = $mod.TObject.$create("Create");',
  12547. '$mod.oA = $mod.TClassA.$create("Create");',
  12548. '$mod.oB = $mod.TClassB.$create("Create");',
  12549. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12550. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12551. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12552. ]));
  12553. end;
  12554. procedure TTestModule.TestClass_TypeAlias;
  12555. begin
  12556. StartProgram(false);
  12557. Add([
  12558. '{$interfaces corba}',
  12559. 'type',
  12560. ' IObject = interface',
  12561. ' end;',
  12562. ' IBird = type IObject;',
  12563. ' TObject = class',
  12564. ' end;',
  12565. ' TBird = type TObject;',
  12566. 'var',
  12567. ' oObj: TObject;',
  12568. ' oBird: TBird;',
  12569. ' IntfObj: IObject;',
  12570. ' IntfBird: IBird;',
  12571. 'begin',
  12572. ' oObj:=oBird;',
  12573. '']);
  12574. ConvertProgram;
  12575. CheckSource('TestClass_TypeAlias',
  12576. LinesToStr([ // statements
  12577. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12578. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12579. 'rtl.createClass(this, "TObject", null, function () {',
  12580. ' this.$init = function () {',
  12581. ' };',
  12582. ' this.$final = function () {',
  12583. ' };',
  12584. '});',
  12585. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12586. '});',
  12587. 'this.oObj = null;',
  12588. 'this.oBird = null;',
  12589. 'this.IntfObj = null;',
  12590. 'this.IntfBird = null;',
  12591. '']),
  12592. LinesToStr([ // $mod.$main
  12593. '$mod.oObj = $mod.oBird;',
  12594. '']));
  12595. end;
  12596. procedure TTestModule.TestClass_AbstractMethod;
  12597. begin
  12598. StartProgram(false);
  12599. Add('type');
  12600. Add(' TObject = class');
  12601. Add(' public');
  12602. Add(' procedure DoIt; virtual; abstract;');
  12603. Add(' end;');
  12604. Add('begin');
  12605. ConvertProgram;
  12606. CheckSource('TestClass_AbstractMethod',
  12607. LinesToStr([ // statements
  12608. 'rtl.createClass(this,"TObject",null,function(){',
  12609. ' this.$init = function () {',
  12610. ' };',
  12611. ' this.$final = function () {',
  12612. ' };',
  12613. '});'
  12614. ]),
  12615. LinesToStr([ // this.$main
  12616. ''
  12617. ]));
  12618. end;
  12619. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12620. begin
  12621. StartProgram(false);
  12622. Add([
  12623. 'type',
  12624. ' TObject = class',
  12625. ' procedure DoAbstract; virtual; abstract;',
  12626. ' procedure DoVirtual; virtual;',
  12627. ' procedure DoIt;',
  12628. ' end;',
  12629. ' TA = class',
  12630. ' procedure doabstract; override;',
  12631. ' procedure dovirtual; override;',
  12632. ' procedure DoSome;',
  12633. ' end;',
  12634. 'procedure tobject.dovirtual;',
  12635. 'begin',
  12636. ' inherited; // call non existing ancestor -> ignore silently',
  12637. 'end;',
  12638. 'procedure tobject.doit;',
  12639. 'begin',
  12640. 'end;',
  12641. 'procedure ta.doabstract;',
  12642. 'begin',
  12643. ' inherited dovirtual; // call TObject.DoVirtual',
  12644. 'end;',
  12645. 'procedure ta.dovirtual;',
  12646. 'begin',
  12647. ' inherited; // call TObject.DoVirtual',
  12648. ' inherited dovirtual; // call TObject.DoVirtual',
  12649. ' inherited dovirtual(); // call TObject.DoVirtual',
  12650. ' doit;',
  12651. ' doit();',
  12652. 'end;',
  12653. 'procedure ta.dosome;',
  12654. 'begin',
  12655. ' inherited; // call non existing ancestor method -> silently ignore',
  12656. 'end;',
  12657. 'begin']);
  12658. ConvertProgram;
  12659. CheckSource('TestClass_CallInherited_ProcNoParams',
  12660. LinesToStr([ // statements
  12661. 'rtl.createClass(this,"TObject",null,function(){',
  12662. ' this.$init = function () {',
  12663. ' };',
  12664. ' this.$final = function () {',
  12665. ' };',
  12666. ' this.DoVirtual = function () {',
  12667. ' };',
  12668. ' this.DoIt = function () {',
  12669. ' };',
  12670. '});',
  12671. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12672. ' this.DoAbstract = function () {',
  12673. ' $mod.TObject.DoVirtual.call(this);',
  12674. ' };',
  12675. ' this.DoVirtual = function () {',
  12676. ' $mod.TObject.DoVirtual.call(this);',
  12677. ' $mod.TObject.DoVirtual.call(this);',
  12678. ' $mod.TObject.DoVirtual.call(this);',
  12679. ' this.DoIt();',
  12680. ' this.DoIt();',
  12681. ' };',
  12682. ' this.DoSome = function () {',
  12683. ' };',
  12684. '});'
  12685. ]),
  12686. LinesToStr([ // this.$main
  12687. ''
  12688. ]));
  12689. end;
  12690. procedure TTestModule.TestClass_CallInherited_WithParams;
  12691. begin
  12692. StartProgram(false);
  12693. Add([
  12694. 'type',
  12695. ' TObject = class',
  12696. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12697. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12698. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12699. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12700. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12701. ' end;',
  12702. ' TClassA = class',
  12703. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12704. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12705. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12706. ' end;',
  12707. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12708. 'begin',
  12709. 'end;',
  12710. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12711. 'begin',
  12712. 'end;',
  12713. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12714. 'begin',
  12715. 'end;',
  12716. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12717. 'begin',
  12718. 'end;',
  12719. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12720. 'begin',
  12721. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12722. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12723. 'end;',
  12724. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12725. 'begin',
  12726. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12727. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12728. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12729. ' doit(pa,pb);',
  12730. ' doit(pa);',
  12731. ' doit2(pa);',
  12732. ' doit2;',
  12733. 'end;',
  12734. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12735. 'begin',
  12736. ' pa:=inherited;',
  12737. 'end;',
  12738. 'begin']);
  12739. ConvertProgram;
  12740. CheckSource('TestClass_CallInherited_WithParams',
  12741. LinesToStr([ // statements
  12742. 'rtl.createClass(this,"TObject",null,function(){',
  12743. ' this.$init = function () {',
  12744. ' };',
  12745. ' this.$final = function () {',
  12746. ' };',
  12747. ' this.DoVirtual = function (pA,pB) {',
  12748. ' };',
  12749. ' this.DoIt = function (pA,pB) {',
  12750. ' };',
  12751. ' this.DoIt2 = function (pA,pB) {',
  12752. ' };',
  12753. ' this.GetIt = function (pA, pB) {',
  12754. ' var Result = 0;',
  12755. ' return Result;',
  12756. ' };',
  12757. '});',
  12758. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12759. ' this.DoAbstract = function (pA,pB) {',
  12760. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12761. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12762. ' };',
  12763. ' this.DoVirtual = function (pA,pB) {',
  12764. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12765. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12766. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12767. ' this.DoIt(pA,pB);',
  12768. ' this.DoIt(pA,0);',
  12769. ' this.DoIt2(pA,2);',
  12770. ' this.DoIt2(1,2);',
  12771. ' };',
  12772. ' this.GetIt$1 = function (pA, pB) {',
  12773. ' var Result = 0;',
  12774. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12775. ' return Result;',
  12776. ' };',
  12777. '});'
  12778. ]),
  12779. LinesToStr([ // this.$main
  12780. ''
  12781. ]));
  12782. end;
  12783. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12784. begin
  12785. StartProgram(false);
  12786. Add('type');
  12787. Add(' TObject = class');
  12788. Add(' constructor Create; virtual;');
  12789. Add(' constructor CreateWithB(b: boolean);');
  12790. Add(' end;');
  12791. Add(' TA = class');
  12792. Add(' constructor Create; override;');
  12793. Add(' constructor CreateWithC(c: char);');
  12794. Add(' procedure DoIt;');
  12795. Add(' class function DoSome: TObject;');
  12796. Add(' end;');
  12797. Add('constructor tobject.create;');
  12798. Add('begin');
  12799. Add(' inherited; // call non existing ancestor -> ignore silently');
  12800. Add('end;');
  12801. Add('constructor tobject.createwithb(b: boolean);');
  12802. Add('begin');
  12803. Add(' inherited; // call non existing ancestor -> ignore silently');
  12804. Add(' create; // normal call');
  12805. Add('end;');
  12806. Add('constructor ta.create;');
  12807. Add('begin');
  12808. Add(' inherited; // normal call TObject.Create');
  12809. Add(' inherited create; // normal call TObject.Create');
  12810. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12811. Add('end;');
  12812. Add('constructor ta.createwithc(c: char);');
  12813. Add('begin');
  12814. Add(' inherited create; // call TObject.Create');
  12815. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12816. Add(' doit;');
  12817. Add(' doit();');
  12818. Add(' dosome;');
  12819. Add('end;');
  12820. Add('procedure ta.doit;');
  12821. Add('begin');
  12822. Add(' create; // normal call');
  12823. Add(' createwithb(false); // normal call');
  12824. Add(' createwithc(''c''); // normal call');
  12825. Add('end;');
  12826. Add('class function ta.dosome: TObject;');
  12827. Add('begin');
  12828. Add(' Result:=create; // constructor');
  12829. Add(' Result:=createwithb(true); // constructor');
  12830. Add(' Result:=createwithc(''c''); // constructor');
  12831. Add('end;');
  12832. Add('begin');
  12833. ConvertProgram;
  12834. CheckSource('TestClass_CallInheritedConstructor',
  12835. LinesToStr([ // statements
  12836. 'rtl.createClass(this,"TObject",null,function(){',
  12837. ' this.$init = function () {',
  12838. ' };',
  12839. ' this.$final = function () {',
  12840. ' };',
  12841. ' this.Create = function () {',
  12842. ' return this;',
  12843. ' };',
  12844. ' this.CreateWithB = function (b) {',
  12845. ' this.Create();',
  12846. ' return this;',
  12847. ' };',
  12848. '});',
  12849. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12850. ' this.Create = function () {',
  12851. ' $mod.TObject.Create.call(this);',
  12852. ' $mod.TObject.Create.call(this);',
  12853. ' $mod.TObject.CreateWithB.call(this, false);',
  12854. ' return this;',
  12855. ' };',
  12856. ' this.CreateWithC = function (c) {',
  12857. ' $mod.TObject.Create.call(this);',
  12858. ' $mod.TObject.CreateWithB.call(this, true);',
  12859. ' this.DoIt();',
  12860. ' this.DoIt();',
  12861. ' this.$class.DoSome();',
  12862. ' return this;',
  12863. ' };',
  12864. ' this.DoIt = function () {',
  12865. ' this.Create();',
  12866. ' this.CreateWithB(false);',
  12867. ' this.CreateWithC("c");',
  12868. ' };',
  12869. ' this.DoSome = function () {',
  12870. ' var Result = null;',
  12871. ' Result = this.$create("Create");',
  12872. ' Result = this.$create("CreateWithB", [true]);',
  12873. ' Result = this.$create("CreateWithC", ["c"]);',
  12874. ' return Result;',
  12875. ' };',
  12876. '});'
  12877. ]),
  12878. LinesToStr([ // this.$main
  12879. ''
  12880. ]));
  12881. end;
  12882. procedure TTestModule.TestClass_ClassVar_Assign;
  12883. begin
  12884. StartProgram(false);
  12885. Add([
  12886. 'type',
  12887. ' TObject = class',
  12888. ' public',
  12889. ' class var vI: longint;',
  12890. ' class var Sub: TObject;',
  12891. ' constructor Create;',
  12892. ' class function GetIt(var Par: longint): tobject;',
  12893. ' end;',
  12894. 'constructor tobject.create;',
  12895. 'begin',
  12896. ' vi:=vi+1;',
  12897. ' Self.vi:=Self.vi+1;',
  12898. ' inc(vi);',
  12899. 'end;',
  12900. 'class function tobject.getit(var par: longint): tobject;',
  12901. 'begin',
  12902. ' vi:=vi+3;',
  12903. ' Self.vi:=Self.vi+4;',
  12904. ' inc(vi);',
  12905. ' Result:=self.sub;',
  12906. ' GetIt(vi);',
  12907. 'end;',
  12908. 'var Obj: tobject;',
  12909. 'begin',
  12910. ' obj:=tobject.create;',
  12911. ' tobject.vi:=3;',
  12912. ' if tobject.vi=4 then ;',
  12913. ' tobject.sub:=nil;',
  12914. ' obj.sub:=nil;',
  12915. ' obj.sub.sub:=nil;']);
  12916. ConvertProgram;
  12917. CheckSource('TestClass_ClassVar_Assign',
  12918. LinesToStr([ // statements
  12919. 'rtl.createClass(this,"TObject",null,function(){',
  12920. ' this.vI = 0;',
  12921. ' this.Sub = null;',
  12922. ' this.$init = function () {',
  12923. ' };',
  12924. ' this.$final = function () {',
  12925. ' };',
  12926. ' this.Create = function(){',
  12927. ' $mod.TObject.vI = this.vI+1;',
  12928. ' $mod.TObject.vI = this.vI+1;',
  12929. ' $mod.TObject.vI += 1;',
  12930. ' return this;',
  12931. ' };',
  12932. ' this.GetIt = function(Par){',
  12933. ' var Result = null;',
  12934. ' $mod.TObject.vI = this.vI + 3;',
  12935. ' $mod.TObject.vI = this.vI + 4;',
  12936. ' $mod.TObject.vI += 1;',
  12937. ' Result = this.Sub;',
  12938. ' this.GetIt({',
  12939. ' p: $mod.TObject,',
  12940. ' get: function () {',
  12941. ' return this.p.vI;',
  12942. ' },',
  12943. ' set: function (v) {',
  12944. ' this.p.vI = v;',
  12945. ' }',
  12946. ' });',
  12947. ' return Result;',
  12948. ' };',
  12949. '});',
  12950. 'this.Obj = null;'
  12951. ]),
  12952. LinesToStr([ // $mod.$main
  12953. '$mod.Obj = $mod.TObject.$create("Create");',
  12954. '$mod.TObject.vI = 3;',
  12955. 'if ($mod.TObject.vI === 4);',
  12956. '$mod.TObject.Sub=null;',
  12957. '$mod.TObject.Sub=null;',
  12958. '$mod.TObject.Sub=null;',
  12959. '']));
  12960. end;
  12961. procedure TTestModule.TestClass_CallClassMethod;
  12962. begin
  12963. StartProgram(false);
  12964. Add('type');
  12965. Add(' TObject = class');
  12966. Add(' public');
  12967. Add(' class var vI: longint;');
  12968. Add(' class var Sub: TObject;');
  12969. Add(' constructor Create;');
  12970. Add(' function GetMore(Par: longint): longint;');
  12971. Add(' class function GetIt(Par: longint): tobject;');
  12972. Add(' end;');
  12973. Add('constructor tobject.create;');
  12974. Add('begin');
  12975. Add(' sub:=getit(3);');
  12976. Add(' vi:=getmore(4);');
  12977. Add(' sub:=Self.getit(5);');
  12978. Add(' vi:=Self.getmore(6);');
  12979. Add('end;');
  12980. Add('function tobject.getmore(par: longint): longint;');
  12981. Add('begin');
  12982. Add(' sub:=getit(11);');
  12983. Add(' vi:=getmore(12);');
  12984. Add(' sub:=self.getit(13);');
  12985. Add(' vi:=self.getmore(14);');
  12986. Add('end;');
  12987. Add('class function tobject.getit(par: longint): tobject;');
  12988. Add('begin');
  12989. Add(' sub:=getit(21);');
  12990. Add(' vi:=sub.getmore(22);');
  12991. Add(' sub:=self.getit(23);');
  12992. Add(' vi:=self.sub.getmore(24);');
  12993. Add('end;');
  12994. Add('var Obj: tobject;');
  12995. Add('begin');
  12996. Add(' obj:=tobject.create;');
  12997. Add(' tobject.getit(5);');
  12998. Add(' obj.getit(6);');
  12999. Add(' obj.sub.getit(7);');
  13000. Add(' obj.sub.getit(8).SUB:=nil;');
  13001. Add(' obj.sub.getit(9).GETIT(10);');
  13002. Add(' obj.sub.getit(11).SuB.getit(12);');
  13003. ConvertProgram;
  13004. CheckSource('TestClass_CallClassMethod',
  13005. LinesToStr([ // statements
  13006. 'rtl.createClass(this,"TObject",null,function(){',
  13007. ' this.vI = 0;',
  13008. ' this.Sub = null;',
  13009. ' this.$init = function () {',
  13010. ' };',
  13011. ' this.$final = function () {',
  13012. ' };',
  13013. ' this.Create = function(){',
  13014. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13015. ' $mod.TObject.vI = this.GetMore(4);',
  13016. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13017. ' $mod.TObject.vI = this.GetMore(6);',
  13018. ' return this;',
  13019. ' };',
  13020. ' this.GetMore = function(Par){',
  13021. ' var Result = 0;',
  13022. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13023. ' $mod.TObject.vI = this.GetMore(12);',
  13024. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13025. ' $mod.TObject.vI = this.GetMore(14);',
  13026. ' return Result;',
  13027. ' };',
  13028. ' this.GetIt = function(Par){',
  13029. ' var Result = null;',
  13030. ' $mod.TObject.Sub = this.GetIt(21);',
  13031. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13032. ' $mod.TObject.Sub = this.GetIt(23);',
  13033. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13034. ' return Result;',
  13035. ' };',
  13036. '});',
  13037. 'this.Obj = null;'
  13038. ]),
  13039. LinesToStr([ // $mod.$main
  13040. '$mod.Obj = $mod.TObject.$create("Create");',
  13041. '$mod.TObject.GetIt(5);',
  13042. '$mod.Obj.$class.GetIt(6);',
  13043. '$mod.Obj.Sub.$class.GetIt(7);',
  13044. '$mod.TObject.Sub=null;',
  13045. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13046. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13047. '']));
  13048. end;
  13049. procedure TTestModule.TestClass_Property;
  13050. begin
  13051. StartProgram(false);
  13052. Add('type');
  13053. Add(' TObject = class');
  13054. Add(' Fx: longint;');
  13055. Add(' Fy: longint;');
  13056. Add(' function GetInt: longint;');
  13057. Add(' procedure SetInt(Value: longint);');
  13058. Add(' procedure DoIt;');
  13059. Add(' property IntA: longint read Fx write Fy;');
  13060. Add(' property IntB: longint read GetInt write SetInt;');
  13061. Add(' end;');
  13062. Add('function tobject.getint: longint;');
  13063. Add('begin');
  13064. Add(' result:=fx;');
  13065. Add('end;');
  13066. Add('procedure tobject.setint(value: longint);');
  13067. Add('begin');
  13068. Add(' if value=fy then exit;');
  13069. Add(' fy:=value;');
  13070. Add('end;');
  13071. Add('procedure tobject.doit;');
  13072. Add('begin');
  13073. Add(' IntA:=IntA+1;');
  13074. Add(' Self.IntA:=Self.IntA+1;');
  13075. Add(' IntB:=IntB+1;');
  13076. Add(' Self.IntB:=Self.IntB+1;');
  13077. Add('end;');
  13078. Add('var Obj: tobject;');
  13079. Add('begin');
  13080. Add(' obj.inta:=obj.inta+1;');
  13081. Add(' if obj.intb=2 then;');
  13082. Add(' obj.intb:=obj.intb+2;');
  13083. Add(' obj.setint(obj.inta);');
  13084. ConvertProgram;
  13085. CheckSource('TestClass_Property',
  13086. LinesToStr([ // statements
  13087. 'rtl.createClass(this, "TObject", null, function () {',
  13088. ' this.$init = function () {',
  13089. ' this.Fx = 0;',
  13090. ' this.Fy = 0;',
  13091. ' };',
  13092. ' this.$final = function () {',
  13093. ' };',
  13094. ' this.GetInt = function () {',
  13095. ' var Result = 0;',
  13096. ' Result = this.Fx;',
  13097. ' return Result;',
  13098. ' };',
  13099. ' this.SetInt = function (Value) {',
  13100. ' if (Value === this.Fy) return;',
  13101. ' this.Fy = Value;',
  13102. ' };',
  13103. ' this.DoIt = function () {',
  13104. ' this.Fy = this.Fx + 1;',
  13105. ' this.Fy = this.Fx + 1;',
  13106. ' this.SetInt(this.GetInt() + 1);',
  13107. ' this.SetInt(this.GetInt() + 1);',
  13108. ' };',
  13109. '});',
  13110. 'this.Obj = null;'
  13111. ]),
  13112. LinesToStr([ // $mod.$main
  13113. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13114. 'if ($mod.Obj.GetInt() === 2);',
  13115. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13116. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13117. ]));
  13118. end;
  13119. procedure TTestModule.TestClass_Property_ClassMethod;
  13120. begin
  13121. StartProgram(false);
  13122. Add([
  13123. 'type',
  13124. ' TObject = class',
  13125. ' class var Fx: longint;',
  13126. ' class var Fy: longint;',
  13127. ' class function GetInt: longint;',
  13128. ' class procedure SetInt(Value: longint);',
  13129. ' end;',
  13130. ' TBird = class',
  13131. ' class procedure DoIt;',
  13132. ' class property IntA: longint read Fx write Fy;',
  13133. ' class property IntB: longint read GetInt write SetInt;',
  13134. ' end;',
  13135. 'class function tobject.getint: longint;',
  13136. 'begin',
  13137. ' result:=fx;',
  13138. 'end;',
  13139. 'class procedure tobject.setint(value: longint);',
  13140. 'begin',
  13141. 'end;',
  13142. 'class procedure tbird.doit;',
  13143. 'begin',
  13144. ' FX:=3;',
  13145. ' IntA:=IntA+1;',
  13146. ' Self.IntA:=Self.IntA+1;',
  13147. ' IntB:=IntB+1;',
  13148. ' Self.IntB:=Self.IntB+1;',
  13149. ' with Self do begin',
  13150. ' FX:=11;',
  13151. ' IntA:=IntA+12;',
  13152. ' IntB:=IntB+13;',
  13153. ' end;',
  13154. 'end;',
  13155. 'var Obj: tbird;',
  13156. 'begin',
  13157. ' tbird.fx:=tbird.fx+1;',
  13158. ' tbird.inta:=tbird.inta+1;',
  13159. ' if tbird.intb=2 then;',
  13160. ' tbird.intb:=tbird.intb+2;',
  13161. ' tbird.setint(tbird.inta);',
  13162. ' obj.inta:=obj.inta+1;',
  13163. ' if obj.intb=2 then;',
  13164. ' obj.intb:=obj.intb+2;',
  13165. ' obj.setint(obj.inta);',
  13166. ' with Tbird do begin',
  13167. ' FX:=FY+1;',
  13168. ' inta:=inta+2;',
  13169. ' intb:=intb+3;',
  13170. ' end;',
  13171. ' with Obj do begin',
  13172. ' FX:=FY+1;',
  13173. ' inta:=inta+2;',
  13174. ' intb:=intb+3;',
  13175. ' end;',
  13176. '']);
  13177. ConvertProgram;
  13178. CheckSource('TestClass_Property_ClassMethod',
  13179. LinesToStr([ // statements
  13180. 'rtl.createClass(this, "TObject", null, function () {',
  13181. ' this.Fx = 0;',
  13182. ' this.Fy = 0;',
  13183. ' this.$init = function () {',
  13184. ' };',
  13185. ' this.$final = function () {',
  13186. ' };',
  13187. ' this.GetInt = function () {',
  13188. ' var Result = 0;',
  13189. ' Result = this.Fx;',
  13190. ' return Result;',
  13191. ' };',
  13192. ' this.SetInt = function (Value) {',
  13193. ' };',
  13194. '});',
  13195. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13196. ' this.DoIt = function () {',
  13197. ' $mod.TObject.Fx = 3;',
  13198. ' $mod.TObject.Fy = this.Fx + 1;',
  13199. ' $mod.TObject.Fy = this.Fx + 1;',
  13200. ' this.SetInt(this.GetInt() + 1);',
  13201. ' this.SetInt(this.GetInt() + 1);',
  13202. ' $mod.TObject.Fx = 11;',
  13203. ' $mod.TObject.Fy = this.Fx + 12;',
  13204. ' this.SetInt(this.GetInt() + 13);',
  13205. ' };',
  13206. '});',
  13207. 'this.Obj = null;'
  13208. ]),
  13209. LinesToStr([ // $mod.$main
  13210. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13211. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13212. 'if ($mod.TBird.GetInt() === 2);',
  13213. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13214. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13215. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13216. 'if ($mod.Obj.$class.GetInt() === 2);',
  13217. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13218. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13219. 'var $with = $mod.TBird;',
  13220. '$mod.TObject.Fx = $with.Fy + 1;',
  13221. '$mod.TObject.Fy = $with.Fx + 2;',
  13222. '$with.SetInt($with.GetInt() + 3);',
  13223. 'var $with1 = $mod.Obj;',
  13224. '$mod.TObject.Fx = $with1.Fy + 1;',
  13225. '$mod.TObject.Fy = $with1.Fx + 2;',
  13226. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13227. '']));
  13228. end;
  13229. procedure TTestModule.TestClass_Property_Indexed;
  13230. begin
  13231. StartProgram(false);
  13232. Add([
  13233. 'type',
  13234. ' TObject = class',
  13235. ' FItems: array of longint;',
  13236. ' function GetItems(Index: longint): longint;',
  13237. ' procedure SetItems(Index: longint; Value: longint);',
  13238. ' procedure DoIt;',
  13239. ' property Items[Index: longint]: longint read getitems write setitems;',
  13240. ' end;',
  13241. 'function tobject.getitems(index: longint): longint;',
  13242. 'begin',
  13243. ' Result:=fitems[index];',
  13244. 'end;',
  13245. 'procedure tobject.setitems(index: longint; value: longint);',
  13246. 'begin',
  13247. ' fitems[index]:=value;',
  13248. 'end;',
  13249. 'procedure tobject.doit;',
  13250. 'begin',
  13251. ' items[1]:=2;',
  13252. ' items[3]:=items[4];',
  13253. ' self.items[5]:=self.items[6];',
  13254. ' items[items[7]]:=items[items[8]];',
  13255. 'end;',
  13256. 'var Obj: tobject;',
  13257. 'begin',
  13258. ' obj.Items[11]:=obj.Items[12];',
  13259. '']);
  13260. ConvertProgram;
  13261. CheckSource('TestClass_Property_Indexed',
  13262. LinesToStr([ // statements
  13263. 'rtl.createClass(this, "TObject", null, function () {',
  13264. ' this.$init = function () {',
  13265. ' this.FItems = [];',
  13266. ' };',
  13267. ' this.$final = function () {',
  13268. ' this.FItems = undefined;',
  13269. ' };',
  13270. ' this.GetItems = function (Index) {',
  13271. ' var Result = 0;',
  13272. ' Result = this.FItems[Index];',
  13273. ' return Result;',
  13274. ' };',
  13275. ' this.SetItems = function (Index, Value) {',
  13276. ' this.FItems[Index] = Value;',
  13277. ' };',
  13278. ' this.DoIt = function () {',
  13279. ' this.SetItems(1, 2);',
  13280. ' this.SetItems(3,this.GetItems(4));',
  13281. ' this.SetItems(5,this.GetItems(6));',
  13282. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13283. ' };',
  13284. '});',
  13285. 'this.Obj = null;'
  13286. ]),
  13287. LinesToStr([ // $mod.$main
  13288. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13289. ]));
  13290. end;
  13291. procedure TTestModule.TestClass_Property_IndexSpec;
  13292. begin
  13293. StartProgram(false);
  13294. Add([
  13295. 'type',
  13296. ' TEnum = (red, blue);',
  13297. ' TObject = class',
  13298. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13299. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13300. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13301. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13302. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13303. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13304. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13305. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13306. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13307. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13308. ' end;',
  13309. 'procedure DoIt(b: boolean); begin end;',
  13310. 'var',
  13311. ' o: TObject;',
  13312. 'begin',
  13313. ' o.B1:=o.B1;',
  13314. ' o.B2:=o.B2;',
  13315. ' o.B3:=o.B3;',
  13316. ' o.I1[''a'']:=o.I1[''b''];',
  13317. ' doit(o.b1);',
  13318. ' doit(o.b2);',
  13319. ' doit(o.i1[''c'']);',
  13320. '']);
  13321. ConvertProgram;
  13322. CheckSource('TestClass_Property_IndexSpec',
  13323. LinesToStr([ // statements
  13324. 'this.TEnum = {',
  13325. ' "0": "red",',
  13326. ' red: 0,',
  13327. ' "1": "blue",',
  13328. ' blue: 1',
  13329. '};',
  13330. 'rtl.createClass(this, "TObject", null, function () {',
  13331. ' this.$init = function () {',
  13332. ' };',
  13333. ' this.$final = function () {',
  13334. ' };',
  13335. '});',
  13336. 'this.DoIt = function (b) {',
  13337. '};',
  13338. 'this.o = null;',
  13339. '']),
  13340. LinesToStr([ // $mod.$main
  13341. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13342. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13343. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13344. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13345. '$mod.DoIt($mod.o.GetIntBool(1));',
  13346. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13347. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13348. '']));
  13349. end;
  13350. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13351. begin
  13352. StartProgram(false);
  13353. Add('type');
  13354. Add(' TArray = array of longint;');
  13355. Add(' TObject = class');
  13356. Add(' FItems: TArray;');
  13357. Add(' function GetItems: tarray;');
  13358. Add(' procedure SetItems(Value: tarray);');
  13359. Add(' property Items: tarray read getitems write setitems;');
  13360. Add(' procedure SetNumbers(const Value: tarray);');
  13361. Add(' property Numbers: tarray write setnumbers;');
  13362. Add(' end;');
  13363. Add('function tobject.getitems: tarray;');
  13364. Add('begin');
  13365. Add(' Result:=fitems;');
  13366. Add('end;');
  13367. Add('procedure tobject.setitems(value: tarray);');
  13368. Add('begin');
  13369. Add(' fitems:=value;');
  13370. Add(' fitems:=nil;');
  13371. Add(' Items:=nil;');
  13372. Add(' Items:=Items;');
  13373. Add(' Items[1]:=2;');
  13374. Add(' fitems[3]:=Items[4];');
  13375. Add(' Items[5]:=Items[6];');
  13376. Add(' Self.Items[7]:=8;');
  13377. Add(' Self.Items[9]:=Self.Items[10];');
  13378. Add(' Items[Items[11]]:=Items[Items[12]];');
  13379. Add('end;');
  13380. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13381. Add('begin;');
  13382. Add(' Numbers:=nil;');
  13383. Add(' Numbers:=Value;');
  13384. Add(' Self.Numbers:=Value;');
  13385. Add('end;');
  13386. Add('var Obj: tobject;');
  13387. Add('begin');
  13388. Add(' obj.items:=nil;');
  13389. Add(' obj.items:=obj.items;');
  13390. Add(' obj.items[11]:=obj.items[12];');
  13391. ConvertProgram;
  13392. CheckSource('TestClass_PropertyOfTypeArray',
  13393. LinesToStr([ // statements
  13394. 'rtl.createClass(this, "TObject", null, function () {',
  13395. ' this.$init = function () {',
  13396. ' this.FItems = [];',
  13397. ' };',
  13398. ' this.$final = function () {',
  13399. ' this.FItems = undefined;',
  13400. ' };',
  13401. ' this.GetItems = function () {',
  13402. ' var Result = [];',
  13403. ' Result = rtl.arrayRef(this.FItems);',
  13404. ' return Result;',
  13405. ' };',
  13406. ' this.SetItems = function (Value) {',
  13407. ' this.FItems = rtl.arrayRef(Value);',
  13408. ' this.FItems = [];',
  13409. ' this.SetItems([]);',
  13410. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13411. ' this.GetItems()[1] = 2;',
  13412. ' this.FItems[3] = this.GetItems()[4];',
  13413. ' this.GetItems()[5] = this.GetItems()[6];',
  13414. ' this.GetItems()[7] = 8;',
  13415. ' this.GetItems()[9] = this.GetItems()[10];',
  13416. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13417. ' };',
  13418. ' this.SetNumbers = function (Value) {',
  13419. ' this.SetNumbers([]);',
  13420. ' this.SetNumbers(Value);',
  13421. ' this.SetNumbers(Value);',
  13422. ' };',
  13423. '});',
  13424. 'this.Obj = null;'
  13425. ]),
  13426. LinesToStr([ // $mod.$main
  13427. '$mod.Obj.SetItems([]);',
  13428. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13429. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13430. ]));
  13431. end;
  13432. procedure TTestModule.TestClass_PropertyDefault;
  13433. begin
  13434. StartProgram(false);
  13435. Add([
  13436. 'type',
  13437. ' TArray = array of longint;',
  13438. ' TObject = class',
  13439. ' end;',
  13440. ' TBird = class',
  13441. ' FItems: TArray;',
  13442. ' function GetItems(Index: longint): longint;',
  13443. ' procedure SetItems(Index, Value: longint);',
  13444. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13445. ' end;',
  13446. 'function TBird.getitems(index: longint): longint;',
  13447. 'begin',
  13448. 'end;',
  13449. 'procedure TBird.setitems(index, value: longint);',
  13450. 'begin',
  13451. ' Self[1]:=2;',
  13452. ' Self[3]:=Self[index];',
  13453. ' Self[index]:=Self[Self[value]];',
  13454. ' Self[Self[4]]:=value;',
  13455. 'end;',
  13456. 'var',
  13457. ' Bird: TBird;',
  13458. ' Obj: TObject;',
  13459. 'begin',
  13460. ' bird[11]:=12;',
  13461. ' bird[13]:=bird[14];',
  13462. ' bird[Bird[15]]:=bird[Bird[15]];',
  13463. ' TBird(obj)[16]:=TBird(obj)[17];',
  13464. ' (obj as tbird)[18]:=19;',
  13465. '']);
  13466. ConvertProgram;
  13467. CheckSource('TestClass_PropertyDefault',
  13468. LinesToStr([ // statements
  13469. 'rtl.createClass(this, "TObject", null, function () {',
  13470. ' this.$init = function () {',
  13471. ' };',
  13472. ' this.$final = function () {',
  13473. ' };',
  13474. '});',
  13475. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13476. ' this.$init = function () {',
  13477. ' $mod.TObject.$init.call(this);',
  13478. ' this.FItems = [];',
  13479. ' };',
  13480. ' this.$final = function () {',
  13481. ' this.FItems = undefined;',
  13482. ' $mod.TObject.$final.call(this);',
  13483. ' };',
  13484. ' this.GetItems = function (Index) {',
  13485. ' var Result = 0;',
  13486. ' return Result;',
  13487. ' };',
  13488. ' this.SetItems = function (Index, Value) {',
  13489. ' this.SetItems(1, 2);',
  13490. ' this.SetItems(3, this.GetItems(Index));',
  13491. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13492. ' this.SetItems(this.GetItems(4), Value);',
  13493. ' };',
  13494. '});',
  13495. 'this.Bird = null;',
  13496. 'this.Obj = null;',
  13497. '']),
  13498. LinesToStr([ // $mod.$main
  13499. '$mod.Bird.SetItems(11, 12);',
  13500. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13501. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13502. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13503. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13504. '']));
  13505. end;
  13506. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13507. begin
  13508. StartProgram(false);
  13509. Add([
  13510. 'type',
  13511. ' TObject = class end;',
  13512. ' TAlphaList = class',
  13513. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13514. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13515. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13516. ' end;',
  13517. ' TBetaList = class',
  13518. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13519. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13520. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13521. ' end;',
  13522. ' TBird = class',
  13523. ' procedure DoIt;',
  13524. ' end;',
  13525. 'procedure TBird.DoIt;',
  13526. 'var',
  13527. ' List: TAlphaList;',
  13528. 'begin',
  13529. ' if TBetaList(List[true])[3]=nil then ;',
  13530. ' TBetaList(List[false])[5]:=nil;',
  13531. 'end;',
  13532. 'var',
  13533. ' List: TAlphaList;',
  13534. 'begin',
  13535. ' if TBetaList(List[true])[3]=nil then ;',
  13536. ' TBetaList(List[false])[5]:=nil;',
  13537. '']);
  13538. ConvertProgram;
  13539. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13540. LinesToStr([ // statements
  13541. 'rtl.createClass(this, "TObject", null, function () {',
  13542. ' this.$init = function () {',
  13543. ' };',
  13544. ' this.$final = function () {',
  13545. ' };',
  13546. '});',
  13547. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13548. '});',
  13549. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13550. '});',
  13551. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13552. ' this.DoIt = function () {',
  13553. ' var List = null;',
  13554. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13555. ' List.GetAlphas(false).SetBetas(5, null);',
  13556. ' };',
  13557. '});',
  13558. 'this.List = null;',
  13559. '']),
  13560. LinesToStr([ // $mod.$main
  13561. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13562. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13563. '']));
  13564. end;
  13565. procedure TTestModule.TestClass_PropertyOverride;
  13566. begin
  13567. StartProgram(false);
  13568. Add('type');
  13569. Add(' integer = longint;');
  13570. Add(' TObject = class');
  13571. Add(' FItem: integer;');
  13572. Add(' function GetItem: integer; external name ''GetItem'';');
  13573. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13574. Add(' property Item: integer read getitem write setitem;');
  13575. Add(' end;');
  13576. Add(' TCar = class');
  13577. Add(' FBag: integer;');
  13578. Add(' function GetBag: integer; external name ''GetBag'';');
  13579. Add(' property Item read getbag;');
  13580. Add(' end;');
  13581. Add('var');
  13582. Add(' Obj: tobject;');
  13583. Add(' Car: tcar;');
  13584. Add('begin');
  13585. Add(' Obj.Item:=Obj.Item;');
  13586. Add(' Car.Item:=Car.Item;');
  13587. ConvertProgram;
  13588. CheckSource('TestClass_PropertyOverride',
  13589. LinesToStr([ // statements
  13590. 'rtl.createClass(this, "TObject", null, function () {',
  13591. ' this.$init = function () {',
  13592. ' this.FItem = 0;',
  13593. ' };',
  13594. ' this.$final = function () {',
  13595. ' };',
  13596. '});',
  13597. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13598. ' this.$init = function () {',
  13599. ' $mod.TObject.$init.call(this);',
  13600. ' this.FBag = 0;',
  13601. ' };',
  13602. '});',
  13603. 'this.Obj = null;',
  13604. 'this.Car = null;',
  13605. '']),
  13606. LinesToStr([ // $mod.$main
  13607. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13608. '$mod.Car.SetItem($mod.Car.GetBag());',
  13609. '']));
  13610. end;
  13611. procedure TTestModule.TestClass_PropertyIncVisibility;
  13612. begin
  13613. AddModuleWithIntfImplSrc('unit1.pp',
  13614. LinesToStr([
  13615. 'type',
  13616. ' TNumber = longint;',
  13617. ' TInteger = longint;',
  13618. ' TObject = class',
  13619. ' private',
  13620. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13621. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13622. ' protected',
  13623. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13624. ' end;']),
  13625. LinesToStr([
  13626. '']));
  13627. StartProgram(true);
  13628. Add([
  13629. 'uses unit1;',
  13630. 'type',
  13631. ' TBird = class',
  13632. ' public',
  13633. ' property Items;',
  13634. ' end;',
  13635. 'procedure DoIt(i: TInteger);',
  13636. 'begin',
  13637. 'end;',
  13638. 'var b: TBird;',
  13639. 'begin',
  13640. ' b.Items[1]:=2;',
  13641. ' b.Items[3]:=b.Items[4];',
  13642. ' DoIt(b.Items[5]);',
  13643. '']);
  13644. ConvertProgram;
  13645. CheckSource('TestClass_PropertyIncVisibility',
  13646. LinesToStr([ // statements
  13647. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13648. '});',
  13649. 'this.DoIt = function (i) {',
  13650. '};',
  13651. 'this.b = null;'
  13652. ]),
  13653. LinesToStr([ // $mod.$main
  13654. '$mod.b.SetItems(1, 2);',
  13655. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13656. '$mod.DoIt($mod.b.GetItems(5));'
  13657. ]));
  13658. end;
  13659. procedure TTestModule.TestClass_Assigned;
  13660. begin
  13661. StartProgram(false);
  13662. Add('type');
  13663. Add(' TObject = class');
  13664. Add(' end;');
  13665. Add('var');
  13666. Add(' Obj: tobject;');
  13667. Add(' b: boolean;');
  13668. Add('begin');
  13669. Add(' if Assigned(obj) then ;');
  13670. Add(' b:=Assigned(obj) or false;');
  13671. ConvertProgram;
  13672. CheckSource('TestClass_Assigned',
  13673. LinesToStr([ // statements
  13674. 'rtl.createClass(this, "TObject", null, function () {',
  13675. ' this.$init = function () {',
  13676. ' };',
  13677. ' this.$final = function () {',
  13678. ' };',
  13679. '});',
  13680. 'this.Obj = null;',
  13681. 'this.b = false;'
  13682. ]),
  13683. LinesToStr([ // $mod.$main
  13684. 'if ($mod.Obj != null);',
  13685. '$mod.b = ($mod.Obj != null) || false;'
  13686. ]));
  13687. end;
  13688. procedure TTestModule.TestClass_WithClassDoCreate;
  13689. begin
  13690. StartProgram(false);
  13691. Add('type');
  13692. Add(' TObject = class');
  13693. Add(' aBool: boolean;');
  13694. Add(' Arr: array of boolean;');
  13695. Add(' constructor Create;');
  13696. Add(' end;');
  13697. Add('constructor TObject.Create; begin end;');
  13698. Add('var');
  13699. Add(' Obj: tobject;');
  13700. Add(' b: boolean;');
  13701. Add('begin');
  13702. Add(' with tobject.create do begin');
  13703. Add(' b:=abool;');
  13704. Add(' abool:=b;');
  13705. Add(' b:=arr[1];');
  13706. Add(' arr[2]:=b;');
  13707. Add(' end;');
  13708. Add(' with tobject do');
  13709. Add(' obj:=create;');
  13710. Add(' with obj do begin');
  13711. Add(' create;');
  13712. Add(' b:=abool;');
  13713. Add(' abool:=b;');
  13714. Add(' b:=arr[3];');
  13715. Add(' arr[4]:=b;');
  13716. Add(' end;');
  13717. ConvertProgram;
  13718. CheckSource('TestClass_WithClassDoCreate',
  13719. LinesToStr([ // statements
  13720. 'rtl.createClass(this, "TObject", null, function () {',
  13721. ' this.$init = function () {',
  13722. ' this.aBool = false;',
  13723. ' this.Arr = [];',
  13724. ' };',
  13725. ' this.$final = function () {',
  13726. ' this.Arr = undefined;',
  13727. ' };',
  13728. ' this.Create = function () {',
  13729. ' return this;',
  13730. ' };',
  13731. '});',
  13732. 'this.Obj = null;',
  13733. 'this.b = false;'
  13734. ]),
  13735. LinesToStr([ // $mod.$main
  13736. 'var $with = $mod.TObject.$create("Create");',
  13737. '$mod.b = $with.aBool;',
  13738. '$with.aBool = $mod.b;',
  13739. '$mod.b = $with.Arr[1];',
  13740. '$with.Arr[2] = $mod.b;',
  13741. 'var $with1 = $mod.TObject;',
  13742. '$mod.Obj = $with1.$create("Create");',
  13743. 'var $with2 = $mod.Obj;',
  13744. '$with2.Create();',
  13745. '$mod.b = $with2.aBool;',
  13746. '$with2.aBool = $mod.b;',
  13747. '$mod.b = $with2.Arr[3];',
  13748. '$with2.Arr[4] = $mod.b;',
  13749. '']));
  13750. end;
  13751. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13752. begin
  13753. StartProgram(false);
  13754. Add('type');
  13755. Add(' TObject = class');
  13756. Add(' FInt: longint;');
  13757. Add(' constructor Create;');
  13758. Add(' function GetSize: longint;');
  13759. Add(' procedure SetSize(Value: longint);');
  13760. Add(' property Int: longint read FInt write FInt;');
  13761. Add(' property Size: longint read GetSize write SetSize;');
  13762. Add(' end;');
  13763. Add('constructor TObject.Create; begin end;');
  13764. Add('function TObject.GetSize: longint; begin; end;');
  13765. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13766. Add('var');
  13767. Add(' Obj: tobject;');
  13768. Add(' i: longint;');
  13769. Add('begin');
  13770. Add(' with TObject.Create do begin');
  13771. Add(' i:=int;');
  13772. Add(' int:=i;');
  13773. Add(' i:=size;');
  13774. Add(' size:=i;');
  13775. Add(' end;');
  13776. Add(' with obj do begin');
  13777. Add(' i:=int;');
  13778. Add(' int:=i;');
  13779. Add(' i:=size;');
  13780. Add(' size:=i;');
  13781. Add(' end;');
  13782. ConvertProgram;
  13783. CheckSource('TestClass_WithClassInstDoProperty',
  13784. LinesToStr([ // statements
  13785. 'rtl.createClass(this, "TObject", null, function () {',
  13786. ' this.$init = function () {',
  13787. ' this.FInt = 0;',
  13788. ' };',
  13789. ' this.$final = function () {',
  13790. ' };',
  13791. ' this.Create = function () {',
  13792. ' return this;',
  13793. ' };',
  13794. ' this.GetSize = function () {',
  13795. ' var Result = 0;',
  13796. ' return Result;',
  13797. ' };',
  13798. ' this.SetSize = function (Value) {',
  13799. ' };',
  13800. '});',
  13801. 'this.Obj = null;',
  13802. 'this.i = 0;'
  13803. ]),
  13804. LinesToStr([ // $mod.$main
  13805. 'var $with = $mod.TObject.$create("Create");',
  13806. '$mod.i = $with.FInt;',
  13807. '$with.FInt = $mod.i;',
  13808. '$mod.i = $with.GetSize();',
  13809. '$with.SetSize($mod.i);',
  13810. 'var $with1 = $mod.Obj;',
  13811. '$mod.i = $with1.FInt;',
  13812. '$with1.FInt = $mod.i;',
  13813. '$mod.i = $with1.GetSize();',
  13814. '$with1.SetSize($mod.i);',
  13815. '']));
  13816. end;
  13817. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13818. begin
  13819. StartProgram(false);
  13820. Add('type');
  13821. Add(' TObject = class');
  13822. Add(' constructor Create;');
  13823. Add(' function GetItems(Index: longint): longint;');
  13824. Add(' procedure SetItems(Index, Value: longint);');
  13825. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13826. Add(' end;');
  13827. Add('constructor TObject.Create; begin end;');
  13828. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13829. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13830. Add('var');
  13831. Add(' Obj: tobject;');
  13832. Add(' i: longint;');
  13833. Add('begin');
  13834. Add(' with TObject.Create do begin');
  13835. Add(' i:=Items[1];');
  13836. Add(' Items[2]:=i;');
  13837. Add(' end;');
  13838. Add(' with obj do begin');
  13839. Add(' i:=Items[3];');
  13840. Add(' Items[4]:=i;');
  13841. Add(' end;');
  13842. ConvertProgram;
  13843. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13844. LinesToStr([ // statements
  13845. 'rtl.createClass(this, "TObject", null, function () {',
  13846. ' this.$init = function () {',
  13847. ' };',
  13848. ' this.$final = function () {',
  13849. ' };',
  13850. ' this.Create = function () {',
  13851. ' return this;',
  13852. ' };',
  13853. ' this.GetItems = function (Index) {',
  13854. ' var Result = 0;',
  13855. ' return Result;',
  13856. ' };',
  13857. ' this.SetItems = function (Index, Value) {',
  13858. ' };',
  13859. '});',
  13860. 'this.Obj = null;',
  13861. 'this.i = 0;'
  13862. ]),
  13863. LinesToStr([ // $mod.$main
  13864. 'var $with = $mod.TObject.$create("Create");',
  13865. '$mod.i = $with.GetItems(1);',
  13866. '$with.SetItems(2, $mod.i);',
  13867. 'var $with1 = $mod.Obj;',
  13868. '$mod.i = $with1.GetItems(3);',
  13869. '$with1.SetItems(4, $mod.i);',
  13870. '']));
  13871. end;
  13872. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13873. begin
  13874. StartProgram(false);
  13875. Add('type');
  13876. Add(' TObject = class');
  13877. Add(' constructor Create;');
  13878. Add(' function GetSize: longint;');
  13879. Add(' procedure SetSize(Value: longint);');
  13880. Add(' end;');
  13881. Add('constructor TObject.Create; begin end;');
  13882. Add('function TObject.GetSize: longint; begin; end;');
  13883. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13884. Add('var');
  13885. Add(' Obj: tobject;');
  13886. Add(' i: longint;');
  13887. Add('begin');
  13888. Add(' with TObject.Create do begin');
  13889. Add(' i:=GetSize;');
  13890. Add(' i:=GetSize();');
  13891. Add(' SetSize(i);');
  13892. Add(' end;');
  13893. Add(' with obj do begin');
  13894. Add(' i:=GetSize;');
  13895. Add(' i:=GetSize();');
  13896. Add(' SetSize(i);');
  13897. Add(' end;');
  13898. ConvertProgram;
  13899. CheckSource('TestClass_WithClassInstDoFunc',
  13900. LinesToStr([ // statements
  13901. 'rtl.createClass(this, "TObject", null, function () {',
  13902. ' this.$init = function () {',
  13903. ' };',
  13904. ' this.$final = function () {',
  13905. ' };',
  13906. ' this.Create = function () {',
  13907. ' return this;',
  13908. ' };',
  13909. ' this.GetSize = function () {',
  13910. ' var Result = 0;',
  13911. ' return Result;',
  13912. ' };',
  13913. ' this.SetSize = function (Value) {',
  13914. ' };',
  13915. '});',
  13916. 'this.Obj = null;',
  13917. 'this.i = 0;'
  13918. ]),
  13919. LinesToStr([ // $mod.$main
  13920. 'var $with = $mod.TObject.$create("Create");',
  13921. '$mod.i = $with.GetSize();',
  13922. '$mod.i = $with.GetSize();',
  13923. '$with.SetSize($mod.i);',
  13924. 'var $with1 = $mod.Obj;',
  13925. '$mod.i = $with1.GetSize();',
  13926. '$mod.i = $with1.GetSize();',
  13927. '$with1.SetSize($mod.i);',
  13928. '']));
  13929. end;
  13930. procedure TTestModule.TestClass_TypeCast;
  13931. begin
  13932. StartProgram(false);
  13933. Add('type');
  13934. Add(' TObject = class');
  13935. Add(' Next: TObject;');
  13936. Add(' constructor Create;');
  13937. Add(' end;');
  13938. Add(' TControl = class(TObject)');
  13939. Add(' Arr: array of TObject;');
  13940. Add(' function GetIt(vI: longint = 0): TObject;');
  13941. Add(' end;');
  13942. Add('constructor tobject.create; begin end;');
  13943. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13944. Add('var');
  13945. Add(' Obj: tobject;');
  13946. Add('begin');
  13947. Add(' obj:=tcontrol(obj).next;');
  13948. Add(' tcontrol(obj):=nil;');
  13949. Add(' obj:=tcontrol(obj);');
  13950. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13951. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13952. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13953. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13954. Add(' obj:=tcontrol(nil);');
  13955. ConvertProgram;
  13956. CheckSource('TestClass_TypeCast',
  13957. LinesToStr([ // statements
  13958. 'rtl.createClass(this, "TObject", null, function () {',
  13959. ' this.$init = function () {',
  13960. ' this.Next = null;',
  13961. ' };',
  13962. ' this.$final = function () {',
  13963. ' this.Next = undefined;',
  13964. ' };',
  13965. ' this.Create = function () {',
  13966. ' return this;',
  13967. ' };',
  13968. '});',
  13969. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  13970. ' this.$init = function () {',
  13971. ' $mod.TObject.$init.call(this);',
  13972. ' this.Arr = [];',
  13973. ' };',
  13974. ' this.$final = function () {',
  13975. ' this.Arr = undefined;',
  13976. ' $mod.TObject.$final.call(this);',
  13977. ' };',
  13978. ' this.GetIt = function (vI) {',
  13979. ' var Result = null;',
  13980. ' return Result;',
  13981. ' };',
  13982. '});',
  13983. 'this.Obj = null;'
  13984. ]),
  13985. LinesToStr([ // $mod.$main
  13986. '$mod.Obj = $mod.Obj.Next;',
  13987. '$mod.Obj = null;',
  13988. '$mod.Obj = $mod.Obj;',
  13989. '$mod.Obj = $mod.Obj.GetIt(0);',
  13990. '$mod.Obj = $mod.Obj.GetIt(0);',
  13991. '$mod.Obj = $mod.Obj.GetIt(1);',
  13992. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13993. '$mod.Obj = null;',
  13994. '']));
  13995. end;
  13996. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13997. begin
  13998. StartProgram(false);
  13999. Add('type');
  14000. Add(' TObject = class end;');
  14001. Add('procedure ProcA(var A);');
  14002. Add('begin');
  14003. Add(' TObject(A):=nil;');
  14004. Add(' TObject(A):=TObject(A);');
  14005. Add(' if TObject(A)=nil then ;');
  14006. Add(' if nil=TObject(A) then ;');
  14007. Add('end;');
  14008. Add('procedure ProcB(out A);');
  14009. Add('begin');
  14010. Add(' TObject(A):=nil;');
  14011. Add(' TObject(A):=TObject(A);');
  14012. Add(' if TObject(A)=nil then ;');
  14013. Add(' if nil=TObject(A) then ;');
  14014. Add('end;');
  14015. Add('procedure ProcC(const A);');
  14016. Add('begin');
  14017. Add(' if TObject(A)=nil then ;');
  14018. Add(' if nil=TObject(A) then ;');
  14019. Add('end;');
  14020. Add('var o: TObject;');
  14021. Add('begin');
  14022. Add(' ProcA(o);');
  14023. Add(' ProcB(o);');
  14024. Add(' ProcC(o);');
  14025. ConvertProgram;
  14026. CheckSource('TestClass_TypeCastUntypedParam',
  14027. LinesToStr([ // statements
  14028. 'rtl.createClass(this, "TObject", null, function () {',
  14029. ' this.$init = function () {',
  14030. ' };',
  14031. ' this.$final = function () {',
  14032. ' };',
  14033. '});',
  14034. 'this.ProcA = function (A) {',
  14035. ' A.set(null);',
  14036. ' A.set(A.get());',
  14037. ' if (A.get() === null);',
  14038. ' if (null === A.get());',
  14039. '};',
  14040. 'this.ProcB = function (A) {',
  14041. ' A.set(null);',
  14042. ' A.set(A.get());',
  14043. ' if (A.get() === null);',
  14044. ' if (null === A.get());',
  14045. '};',
  14046. 'this.ProcC = function (A) {',
  14047. ' if (A === null);',
  14048. ' if (null === A);',
  14049. '};',
  14050. 'this.o = null;',
  14051. '']),
  14052. LinesToStr([ // $mod.$main
  14053. '$mod.ProcA({',
  14054. ' p: $mod,',
  14055. ' get: function () {',
  14056. ' return this.p.o;',
  14057. ' },',
  14058. ' set: function (v) {',
  14059. ' this.p.o = v;',
  14060. ' }',
  14061. '});',
  14062. '$mod.ProcB({',
  14063. ' p: $mod,',
  14064. ' get: function () {',
  14065. ' return this.p.o;',
  14066. ' },',
  14067. ' set: function (v) {',
  14068. ' this.p.o = v;',
  14069. ' }',
  14070. '});',
  14071. '$mod.ProcC($mod.o);',
  14072. '']));
  14073. end;
  14074. procedure TTestModule.TestClass_Overloads;
  14075. begin
  14076. StartProgram(false);
  14077. Add('type');
  14078. Add(' TObject = class');
  14079. Add(' procedure DoIt;');
  14080. Add(' procedure DoIt(vI: longint);');
  14081. Add(' end;');
  14082. Add('procedure TObject.DoIt;');
  14083. Add('begin');
  14084. Add(' DoIt;');
  14085. Add(' DoIt(1);');
  14086. Add('end;');
  14087. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14088. Add('begin');
  14089. ConvertProgram;
  14090. CheckSource('TestClass_Overloads',
  14091. LinesToStr([ // statements
  14092. 'rtl.createClass(this, "TObject", null, function () {',
  14093. ' this.$init = function () {',
  14094. ' };',
  14095. ' this.$final = function () {',
  14096. ' };',
  14097. ' this.DoIt = function () {',
  14098. ' this.DoIt();',
  14099. ' this.DoIt$1(1);',
  14100. ' };',
  14101. ' this.DoIt$1 = function (vI) {',
  14102. ' };',
  14103. '});',
  14104. '']),
  14105. LinesToStr([ // $mod.$main
  14106. '']));
  14107. end;
  14108. procedure TTestModule.TestClass_OverloadsAncestor;
  14109. begin
  14110. StartProgram(false);
  14111. Add('type');
  14112. Add(' TObject = class;');
  14113. Add(' TObject = class');
  14114. Add(' procedure DoIt(vA: longint);');
  14115. Add(' procedure DoIt(vA, vB: longint);');
  14116. Add(' end;');
  14117. Add(' TCar = class;');
  14118. Add(' TCar = class');
  14119. Add(' procedure DoIt(vA: longint);');
  14120. Add(' procedure DoIt(vA, vB: longint);');
  14121. Add(' end;');
  14122. Add('procedure tobject.doit(va: longint);');
  14123. Add('begin');
  14124. Add(' doit(1);');
  14125. Add(' doit(1,2);');
  14126. Add('end;');
  14127. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14128. Add('procedure tcar.doit(va: longint);');
  14129. Add('begin');
  14130. Add(' doit(1);');
  14131. Add(' doit(1,2);');
  14132. Add(' inherited doit(1);');
  14133. Add(' inherited doit(1,2);');
  14134. Add('end;');
  14135. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14136. Add('begin');
  14137. ConvertProgram;
  14138. CheckSource('TestClass_OverloadsAncestor',
  14139. LinesToStr([ // statements
  14140. 'rtl.createClass(this, "TObject", null, function () {',
  14141. ' this.$init = function () {',
  14142. ' };',
  14143. ' this.$final = function () {',
  14144. ' };',
  14145. ' this.DoIt = function (vA) {',
  14146. ' this.DoIt(1);',
  14147. ' this.DoIt$1(1,2);',
  14148. ' };',
  14149. ' this.DoIt$1 = function (vA, vB) {',
  14150. ' };',
  14151. '});',
  14152. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14153. ' this.DoIt$2 = function (vA) {',
  14154. ' this.DoIt$2(1);',
  14155. ' this.DoIt$3(1, 2);',
  14156. ' $mod.TObject.DoIt.call(this, 1);',
  14157. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14158. ' };',
  14159. ' this.DoIt$3 = function (vA, vB) {',
  14160. ' };',
  14161. '});',
  14162. '']),
  14163. LinesToStr([ // $mod.$main
  14164. '']));
  14165. end;
  14166. procedure TTestModule.TestClass_OverloadConstructor;
  14167. begin
  14168. StartProgram(false);
  14169. Add('type');
  14170. Add(' TObject = class');
  14171. Add(' constructor Create(vA: longint);');
  14172. Add(' constructor Create(vA, vB: longint);');
  14173. Add(' end;');
  14174. Add(' TCar = class');
  14175. Add(' constructor Create(vA: longint);');
  14176. Add(' constructor Create(vA, vB: longint);');
  14177. Add(' end;');
  14178. Add('constructor tobject.create(va: longint);');
  14179. Add('begin');
  14180. Add(' create(1);');
  14181. Add(' create(1,2);');
  14182. Add('end;');
  14183. Add('constructor tobject.create(va, vb: longint); begin end;');
  14184. Add('constructor tcar.create(va: longint);');
  14185. Add('begin');
  14186. Add(' create(1);');
  14187. Add(' create(1,2);');
  14188. Add(' inherited create(1);');
  14189. Add(' inherited create(1,2);');
  14190. Add('end;');
  14191. Add('constructor tcar.create(va, vb: longint); begin end;');
  14192. Add('begin');
  14193. Add(' tobject.create(1);');
  14194. Add(' tobject.create(1,2);');
  14195. Add(' tcar.create(1);');
  14196. Add(' tcar.create(1,2);');
  14197. ConvertProgram;
  14198. CheckSource('TestClass_OverloadConstructor',
  14199. LinesToStr([ // statements
  14200. 'rtl.createClass(this, "TObject", null, function () {',
  14201. ' this.$init = function () {',
  14202. ' };',
  14203. ' this.$final = function () {',
  14204. ' };',
  14205. ' this.Create = function (vA) {',
  14206. ' this.Create(1);',
  14207. ' this.Create$1(1,2);',
  14208. ' return this;',
  14209. ' };',
  14210. ' this.Create$1 = function (vA, vB) {',
  14211. ' return this;',
  14212. ' };',
  14213. '});',
  14214. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14215. ' this.Create$2 = function (vA) {',
  14216. ' this.Create$2(1);',
  14217. ' this.Create$3(1, 2);',
  14218. ' $mod.TObject.Create.call(this, 1);',
  14219. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14220. ' return this;',
  14221. ' };',
  14222. ' this.Create$3 = function (vA, vB) {',
  14223. ' return this;',
  14224. ' };',
  14225. '});',
  14226. '']),
  14227. LinesToStr([ // $mod.$main
  14228. '$mod.TObject.$create("Create", [1]);',
  14229. '$mod.TObject.$create("Create$1", [1, 2]);',
  14230. '$mod.TCar.$create("Create$2", [1]);',
  14231. '$mod.TCar.$create("Create$3", [1, 2]);',
  14232. '']));
  14233. end;
  14234. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14235. begin
  14236. StartProgram(false);
  14237. Add([
  14238. '{$mode delphi}',
  14239. 'type',
  14240. ' TObject = class end;',
  14241. ' TBird = class',
  14242. ' function {#a}GetValue: longint; overload; virtual;',
  14243. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14244. ' end;',
  14245. ' TEagle = class(TBird)',
  14246. ' function {#c}GetValue: longint; overload; override;',
  14247. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14248. ' end;',
  14249. 'function TBird.GetValue: longint;',
  14250. 'begin',
  14251. ' if 3={@a}GetValue then ;',
  14252. ' if 4={@b}GetValue(5) then ;',
  14253. 'end;',
  14254. 'function TBird.GetValue(AValue: longint): longint;',
  14255. 'begin',
  14256. 'end;',
  14257. 'function TEagle.GetValue: longint;',
  14258. 'begin',
  14259. ' if 13={@c}GetValue then ;',
  14260. ' if 14={@d}GetValue(15) then ;',
  14261. ' if 15=inherited {@a}GetValue then ;',
  14262. ' if 16=inherited {@b}GetValue(17) then ;',
  14263. 'end;',
  14264. 'function TEagle.GetValue(AValue: longint): longint;',
  14265. 'begin',
  14266. 'end;',
  14267. 'var',
  14268. ' e: TEagle;',
  14269. 'begin',
  14270. ' if 23=e.{@c}GetValue then ;',
  14271. ' if 24=e.{@d}GetValue(25) then ;']);
  14272. ConvertProgram;
  14273. CheckSource('TestClass_OverloadDelphiOverride',
  14274. LinesToStr([ // statements
  14275. 'rtl.createClass(this, "TObject", null, function () {',
  14276. ' this.$init = function () {',
  14277. ' };',
  14278. ' this.$final = function () {',
  14279. ' };',
  14280. '});',
  14281. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14282. ' this.GetValue = function () {',
  14283. ' var Result = 0;',
  14284. ' if (3 === this.GetValue()) ;',
  14285. ' if (4 === this.GetValue$1(5)) ;',
  14286. ' return Result;',
  14287. ' };',
  14288. ' this.GetValue$1 = function (AValue) {',
  14289. ' var Result = 0;',
  14290. ' return Result;',
  14291. ' };',
  14292. '});',
  14293. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14294. ' this.GetValue = function () {',
  14295. ' var Result = 0;',
  14296. ' if (13 === this.GetValue()) ;',
  14297. ' if (14 === this.GetValue$1(15)) ;',
  14298. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14299. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14300. ' return Result;',
  14301. ' };',
  14302. ' this.GetValue$1 = function (AValue) {',
  14303. ' var Result = 0;',
  14304. ' return Result;',
  14305. ' };',
  14306. '});',
  14307. 'this.e = null;',
  14308. '']),
  14309. LinesToStr([ // $mod.$main
  14310. 'if (23 === $mod.e.GetValue()) ;',
  14311. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14312. '']));
  14313. end;
  14314. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14315. begin
  14316. StartProgram(false);
  14317. Add([
  14318. '{$mode delphi}',
  14319. 'type',
  14320. ' TObject = class end;',
  14321. ' TAnimal = class',
  14322. ' public',
  14323. ' {#animal_a}A: longint;',
  14324. ' function {#animal_b}B: longint;',
  14325. ' end;',
  14326. ' TBird = class(TAnimal)',
  14327. ' public',
  14328. ' {#bird_a}A: double;',
  14329. ' {#bird_b}B: boolean;',
  14330. ' end;',
  14331. ' TEagle = class(TBird)',
  14332. ' public',
  14333. ' function {#eagle_a}A: boolean;',
  14334. ' {#eagle_b}B: double;',
  14335. ' end;',
  14336. 'function TAnimal.B: longint;',
  14337. 'begin',
  14338. 'end;',
  14339. 'function TEagle.A: boolean;',
  14340. 'begin',
  14341. ' {@eagle_b}B:=3.3;',
  14342. ' {@eagle_a}A();',
  14343. ' TBird(Self).{@bird_b}B:=true;',
  14344. ' TAnimal(Self).{@animal_a}A:=17;',
  14345. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14346. 'end;',
  14347. 'var',
  14348. ' e: TEagle;',
  14349. 'begin',
  14350. ' e.{@eagle_b}B:=5.3;',
  14351. ' if e.{@eagle_a}A then ;',
  14352. '']);
  14353. ConvertProgram;
  14354. CheckSource('TestClass_ReintroduceVarDelphi',
  14355. LinesToStr([ // statements
  14356. 'rtl.createClass(this, "TObject", null, function () {',
  14357. ' this.$init = function () {',
  14358. ' };',
  14359. ' this.$final = function () {',
  14360. ' };',
  14361. '});',
  14362. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14363. ' this.$init = function () {',
  14364. ' $mod.TObject.$init.call(this);',
  14365. ' this.A = 0;',
  14366. ' };',
  14367. ' this.B = function () {',
  14368. ' var Result = 0;',
  14369. ' return Result;',
  14370. ' };',
  14371. '});',
  14372. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14373. ' this.$init = function () {',
  14374. ' $mod.TAnimal.$init.call(this);',
  14375. ' this.A$1 = 0.0;',
  14376. ' this.B$1 = false;',
  14377. ' };',
  14378. '});',
  14379. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14380. ' this.$init = function () {',
  14381. ' $mod.TBird.$init.call(this);',
  14382. ' this.B$2 = 0.0;',
  14383. ' };',
  14384. ' this.A$2 = function () {',
  14385. ' var Result = false;',
  14386. ' this.B$2 = 3.3;',
  14387. ' this.A$2();',
  14388. ' this.B$1 = true;',
  14389. ' this.A = 17;',
  14390. ' this.B$1 = this.A$1 > 1;',
  14391. ' return Result;',
  14392. ' };',
  14393. '});',
  14394. 'this.e = null;',
  14395. '']),
  14396. LinesToStr([ // $mod.$main
  14397. '$mod.e.B$2 = 5.3;',
  14398. 'if ($mod.e.A$2()) ;',
  14399. '']));
  14400. end;
  14401. procedure TTestModule.TestClass_ReintroducedVar;
  14402. begin
  14403. StartProgram(false);
  14404. Add('type');
  14405. Add(' TObject = class');
  14406. Add(' strict private');
  14407. Add(' Some: longint;');
  14408. Add(' end;');
  14409. Add(' TMobile = class');
  14410. Add(' strict private');
  14411. Add(' Some: string;');
  14412. Add(' end;');
  14413. Add(' TCar = class(tmobile)');
  14414. Add(' procedure Some;');
  14415. Add(' procedure Some(vA: longint);');
  14416. Add(' end;');
  14417. Add('procedure tcar.some;');
  14418. Add('begin');
  14419. Add(' Some;');
  14420. Add(' Some(1);');
  14421. Add('end;');
  14422. Add('procedure tcar.some(va: longint); begin end;');
  14423. Add('begin');
  14424. ConvertProgram;
  14425. CheckSource('TestClass_ReintroducedVar',
  14426. LinesToStr([ // statements
  14427. 'rtl.createClass(this, "TObject", null, function () {',
  14428. ' this.$init = function () {',
  14429. ' this.Some = 0;',
  14430. ' };',
  14431. ' this.$final = function () {',
  14432. ' };',
  14433. '});',
  14434. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14435. ' this.$init = function () {',
  14436. ' $mod.TObject.$init.call(this);',
  14437. ' this.Some$1 = "";',
  14438. ' };',
  14439. '});',
  14440. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14441. ' this.Some$2 = function () {',
  14442. ' this.Some$2();',
  14443. ' this.Some$3(1);',
  14444. ' };',
  14445. ' this.Some$3 = function (vA) {',
  14446. ' };',
  14447. '});',
  14448. '']),
  14449. LinesToStr([ // $mod.$main
  14450. '']));
  14451. end;
  14452. procedure TTestModule.TestClass_RaiseDescendant;
  14453. begin
  14454. StartProgram(false);
  14455. Add([
  14456. 'type',
  14457. ' TObject = class',
  14458. ' constructor Create(Msg: string);',
  14459. ' end;',
  14460. ' Exception = class',
  14461. ' end;',
  14462. ' EConvertError = class(Exception)',
  14463. ' end;',
  14464. 'constructor TObject.Create(Msg: string); begin end;',
  14465. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14466. 'begin',
  14467. ' raise Exception.Create(''Bar1'');',
  14468. ' raise EConvertError.Create(''Bar2'');',
  14469. ' raise AssertConv(''Bar2'');',
  14470. ' raise AssertConv;',
  14471. '']);
  14472. ConvertProgram;
  14473. CheckSource('TestClass_RaiseDescendant',
  14474. LinesToStr([ // statements
  14475. 'rtl.createClass(this, "TObject", null, function () {',
  14476. ' this.$init = function () {',
  14477. ' };',
  14478. ' this.$final = function () {',
  14479. ' };',
  14480. ' this.Create = function (Msg) {',
  14481. ' return this;',
  14482. ' };',
  14483. '});',
  14484. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14485. '});',
  14486. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14487. '});',
  14488. 'this.AssertConv = function (Msg) {',
  14489. ' var Result = null;',
  14490. ' return Result;',
  14491. '};',
  14492. '']),
  14493. LinesToStr([ // $mod.$main
  14494. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14495. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14496. 'throw $mod.AssertConv("Bar2");',
  14497. 'throw $mod.AssertConv("def");',
  14498. '']));
  14499. end;
  14500. procedure TTestModule.TestClass_ExternalMethod;
  14501. begin
  14502. AddModuleWithIntfImplSrc('unit2.pas',
  14503. LinesToStr([
  14504. 'type',
  14505. ' TObject = class',
  14506. ' public',
  14507. ' procedure Intern; external name ''$DoIntern'';',
  14508. ' end;',
  14509. '']),
  14510. LinesToStr([
  14511. '']));
  14512. StartUnit(true);
  14513. Add('interface');
  14514. Add('uses unit2;');
  14515. Add('type');
  14516. Add(' TCar = class(TObject)');
  14517. Add(' public');
  14518. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14519. Add(' procedure DoIt;');
  14520. Add(' end;');
  14521. Add('implementation');
  14522. Add('procedure tcar.doit;');
  14523. Add('begin');
  14524. Add(' Intern;');
  14525. Add(' Intern();');
  14526. Add(' Intern2;');
  14527. Add(' Intern2();');
  14528. Add('end;');
  14529. Add('var Obj: TCar;');
  14530. Add('begin');
  14531. Add(' obj.intern;');
  14532. Add(' obj.intern();');
  14533. Add(' obj.intern2;');
  14534. Add(' obj.intern2();');
  14535. Add(' obj.doit;');
  14536. Add(' obj.doit();');
  14537. Add(' with obj do begin');
  14538. Add(' Intern;');
  14539. Add(' Intern();');
  14540. Add(' Intern2;');
  14541. Add(' Intern2();');
  14542. Add(' end;');
  14543. ConvertUnit;
  14544. CheckSource('TestClass_ExternalMethod',
  14545. LinesToStr([
  14546. 'var $impl = $mod.$impl;',
  14547. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14548. ' this.DoIt = function () {',
  14549. ' this.$DoIntern();',
  14550. ' this.$DoIntern();',
  14551. ' this.$DoIntern2();',
  14552. ' this.$DoIntern2();',
  14553. ' };',
  14554. ' });',
  14555. '']),
  14556. LinesToStr([ // this.$init
  14557. '$impl.Obj.$DoIntern();',
  14558. '$impl.Obj.$DoIntern();',
  14559. '$impl.Obj.$DoIntern2();',
  14560. '$impl.Obj.$DoIntern2();',
  14561. '$impl.Obj.DoIt();',
  14562. '$impl.Obj.DoIt();',
  14563. 'var $with = $impl.Obj;',
  14564. '$with.$DoIntern();',
  14565. '$with.$DoIntern();',
  14566. '$with.$DoIntern2();',
  14567. '$with.$DoIntern2();',
  14568. '']),
  14569. LinesToStr([ // implementation
  14570. '$impl.Obj = null;',
  14571. '']) );
  14572. end;
  14573. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14574. begin
  14575. StartProgram(false);
  14576. Add('type');
  14577. Add(' TObject = class');
  14578. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14579. Add(' end;');
  14580. Add('begin');
  14581. SetExpectedPasResolverError('Virtual method name must match external',
  14582. nVirtualMethodNameMustMatchExternal);
  14583. ConvertProgram;
  14584. end;
  14585. procedure TTestModule.TestClass_ExternalOverrideFail;
  14586. begin
  14587. StartProgram(false);
  14588. Add('type');
  14589. Add(' TObject = class');
  14590. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14591. Add(' end;');
  14592. Add(' TCar = class');
  14593. Add(' procedure DoIt; override; external name ''DoIt'';');
  14594. Add(' end;');
  14595. Add('begin');
  14596. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14597. nInvalidXModifierY);
  14598. ConvertProgram;
  14599. end;
  14600. procedure TTestModule.TestClass_ExternalVar;
  14601. begin
  14602. AddModuleWithIntfImplSrc('unit2.pas',
  14603. LinesToStr([
  14604. '{$modeswitch externalclass}',
  14605. 'type',
  14606. ' TObject = class',
  14607. ' public',
  14608. ' Intern: longint external name ''$Intern'';',
  14609. ' Bracket: longint external name ''["A B"]'';',
  14610. ' end;',
  14611. '']),
  14612. LinesToStr([
  14613. '']));
  14614. StartUnit(true);
  14615. Add([
  14616. 'interface',
  14617. 'uses unit2;',
  14618. '{$modeswitch externalclass}',
  14619. 'type',
  14620. ' TCar = class(tobject)',
  14621. ' public',
  14622. ' Intern2: longint external name ''$Intern2'';',
  14623. ' procedure DoIt;',
  14624. ' end;',
  14625. 'implementation',
  14626. 'procedure tcar.doit;',
  14627. 'begin',
  14628. ' Intern:=Intern+1;',
  14629. ' Intern2:=Intern2+2;',
  14630. ' Bracket:=Bracket+3;',
  14631. 'end;',
  14632. 'var Obj: TCar;',
  14633. 'begin',
  14634. ' obj.intern:=obj.intern+1;',
  14635. ' obj.intern2:=obj.intern2+2;',
  14636. ' obj.Bracket:=obj.Bracket+3;',
  14637. ' with obj do begin',
  14638. ' intern:=intern+1;',
  14639. ' intern2:=intern2+2;',
  14640. ' Bracket:=Bracket+3;',
  14641. ' end;']);
  14642. ConvertUnit;
  14643. CheckSource('TestClass_ExternalVar',
  14644. LinesToStr([
  14645. 'var $impl = $mod.$impl;',
  14646. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14647. ' this.DoIt = function () {',
  14648. ' this.$Intern = this.$Intern + 1;',
  14649. ' this.$Intern2 = this.$Intern2 + 2;',
  14650. ' this["A B"] = this["A B"] + 3;',
  14651. ' };',
  14652. ' });',
  14653. '']),
  14654. LinesToStr([
  14655. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14656. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14657. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14658. 'var $with = $impl.Obj;',
  14659. '$with.$Intern = $with.$Intern + 1;',
  14660. '$with.$Intern2 = $with.$Intern2 + 2;',
  14661. '$with["A B"] = $with["A B"] + 3;',
  14662. '']),
  14663. LinesToStr([ // implementation
  14664. '$impl.Obj = null;',
  14665. '']));
  14666. end;
  14667. procedure TTestModule.TestClass_Const;
  14668. begin
  14669. StartProgram(false);
  14670. Add([
  14671. 'type',
  14672. ' integer = longint;',
  14673. ' TClass = class of TObject;',
  14674. ' TObject = class',
  14675. ' public',
  14676. ' const cI: integer = 3;',
  14677. ' procedure DoIt;',
  14678. ' class procedure DoMore;',
  14679. ' end;',
  14680. 'procedure tobject.doit;',
  14681. 'begin',
  14682. ' if cI=4 then;',
  14683. ' if 5=cI then;',
  14684. ' if Self.cI=6 then;',
  14685. ' if 7=Self.cI then;',
  14686. ' with Self do begin',
  14687. ' if cI=11 then;',
  14688. ' if 12=cI then;',
  14689. ' end;',
  14690. 'end;',
  14691. 'class procedure tobject.domore;',
  14692. 'begin',
  14693. ' if cI=8 then;',
  14694. ' if Self.cI=9 then;',
  14695. ' if 10=cI then;',
  14696. ' if 11=Self.cI then;',
  14697. ' with Self do begin',
  14698. ' if cI=13 then;',
  14699. ' if 14=cI then;',
  14700. ' end;',
  14701. 'end;',
  14702. 'var',
  14703. ' Obj: TObject;',
  14704. ' Cla: TClass;',
  14705. 'begin',
  14706. ' if TObject.cI=21 then ;',
  14707. ' if Obj.cI=22 then ;',
  14708. ' if Cla.cI=23 then ;',
  14709. ' with obj do if ci=24 then;',
  14710. ' with TObject do if ci=25 then;',
  14711. ' with Cla do if ci=26 then;']);
  14712. ConvertProgram;
  14713. CheckSource('TestClass_Const',
  14714. LinesToStr([
  14715. 'rtl.createClass(this, "TObject", null, function () {',
  14716. ' this.cI = 3;',
  14717. ' this.$init = function () {',
  14718. ' };',
  14719. ' this.$final = function () {',
  14720. ' };',
  14721. ' this.DoIt = function () {',
  14722. ' if (this.cI === 4) ;',
  14723. ' if (5 === this.cI) ;',
  14724. ' if (this.cI === 6) ;',
  14725. ' if (7 === this.cI) ;',
  14726. ' if (this.cI === 11) ;',
  14727. ' if (12 === this.cI) ;',
  14728. ' };',
  14729. ' this.DoMore = function () {',
  14730. ' if (this.cI === 8) ;',
  14731. ' if (this.cI === 9) ;',
  14732. ' if (10 === this.cI) ;',
  14733. ' if (11 === this.cI) ;',
  14734. ' if (this.cI === 13) ;',
  14735. ' if (14 === this.cI) ;',
  14736. ' };',
  14737. '});',
  14738. 'this.Obj = null;',
  14739. 'this.Cla = null;',
  14740. '']),
  14741. LinesToStr([
  14742. 'if ($mod.TObject.cI === 21) ;',
  14743. 'if ($mod.Obj.cI === 22) ;',
  14744. 'if ($mod.Cla.cI === 23) ;',
  14745. 'var $with = $mod.Obj;',
  14746. 'if ($with.cI === 24) ;',
  14747. 'var $with1 = $mod.TObject;',
  14748. 'if ($with1.cI === 25) ;',
  14749. 'var $with2 = $mod.Cla;',
  14750. 'if ($with2.cI === 26) ;',
  14751. '']));
  14752. end;
  14753. procedure TTestModule.TestClass_ConstEnum;
  14754. begin
  14755. StartProgram(false);
  14756. Add([
  14757. 'type',
  14758. ' TEnum = (red,blue);',
  14759. ' TObject = class',
  14760. ' end;',
  14761. ' TAnimal = class',
  14762. ' public',
  14763. ' type TSubEnum = (light,dark);',
  14764. ' const a = high(TEnum);',
  14765. ' const b = high(TSubEnum);',
  14766. ' end;',
  14767. ' TBird = class(TAnimal)',
  14768. ' public',
  14769. ' const c = high(TEnum);',
  14770. ' const d = high(TSubEnum);',
  14771. ' end;',
  14772. ' TAnt = class',
  14773. ' public',
  14774. ' const e = high(TEnum);',
  14775. ' const f = high(TBird.TSubEnum);',
  14776. ' end;',
  14777. 'begin',
  14778. '']);
  14779. ConvertProgram;
  14780. CheckSource('TestClass_ConstEnum',
  14781. LinesToStr([
  14782. 'this.TEnum = {',
  14783. ' "0": "red",',
  14784. ' red: 0,',
  14785. ' "1": "blue",',
  14786. ' blue: 1',
  14787. '};',
  14788. 'rtl.createClass(this, "TObject", null, function () {',
  14789. ' this.$init = function () {',
  14790. ' };',
  14791. ' this.$final = function () {',
  14792. ' };',
  14793. '});',
  14794. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14795. ' this.TSubEnum = {',
  14796. ' "0": "light",',
  14797. ' light: 0,',
  14798. ' "1": "dark",',
  14799. ' dark: 1',
  14800. ' };',
  14801. ' this.a = $mod.TEnum.blue;',
  14802. ' this.b = this.TSubEnum.dark;',
  14803. '});',
  14804. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14805. ' this.c = $mod.TEnum.blue;',
  14806. ' this.d = this.TSubEnum.dark;',
  14807. '});',
  14808. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14809. ' this.e = $mod.TEnum.blue;',
  14810. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14811. '});',
  14812. '']),
  14813. LinesToStr([
  14814. '']));
  14815. end;
  14816. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14817. begin
  14818. StartProgram(false);
  14819. Add([
  14820. 'type',
  14821. ' TObject = class',
  14822. ' const cI: longint = 3;',
  14823. ' procedure Fly;',
  14824. ' procedure Run;',
  14825. ' end;',
  14826. ' TBird = class',
  14827. ' procedure Go;',
  14828. ' end;',
  14829. 'procedure tobject.fly;',
  14830. 'const cI: word = 4;',
  14831. 'begin',
  14832. ' if cI=Self.cI then ;',
  14833. 'end;',
  14834. 'procedure tobject.run;',
  14835. 'const cI: word = 5;',
  14836. 'begin',
  14837. ' if cI=Self.cI then ;',
  14838. 'end;',
  14839. 'procedure tbird.go;',
  14840. 'const cI: word = 6;',
  14841. 'begin',
  14842. ' if cI=Self.cI then ;',
  14843. 'end;',
  14844. 'begin',
  14845. '']);
  14846. ConvertProgram;
  14847. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14848. LinesToStr([
  14849. 'rtl.createClass(this, "TObject", null, function () {',
  14850. ' this.cI = 3;',
  14851. ' this.$init = function () {',
  14852. ' };',
  14853. ' this.$final = function () {',
  14854. ' };',
  14855. ' var cI$1 = 4;',
  14856. ' this.Fly = function () {',
  14857. ' if (cI$1 === this.cI) ;',
  14858. ' };',
  14859. ' var cI$2 = 5;',
  14860. ' this.Run = function () {',
  14861. ' if (cI$2 === this.cI) ;',
  14862. ' };',
  14863. '});',
  14864. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14865. ' var cI$3 = 6;',
  14866. ' this.Go = function () {',
  14867. ' if (cI$3 === this.cI) ;',
  14868. ' };',
  14869. '});',
  14870. '']),
  14871. LinesToStr([
  14872. '']));
  14873. end;
  14874. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14875. begin
  14876. StartUnit(false);
  14877. Add([
  14878. 'interface',
  14879. 'type',
  14880. ' TObject = class',
  14881. ' const cI: longint = 3;',
  14882. ' procedure Fly;',
  14883. ' procedure Run;',
  14884. ' end;',
  14885. ' TBird = class',
  14886. ' procedure Go;',
  14887. ' end;',
  14888. 'implementation',
  14889. 'procedure tobject.fly;',
  14890. 'const cI: word = 4;',
  14891. 'begin',
  14892. ' if cI=Self.cI then ;',
  14893. 'end;',
  14894. 'procedure tobject.run;',
  14895. 'const cI: word = 5;',
  14896. 'begin',
  14897. ' if cI=Self.cI then ;',
  14898. 'end;',
  14899. 'procedure tbird.go;',
  14900. 'const cI: word = 6;',
  14901. 'begin',
  14902. ' if cI=Self.cI then ;',
  14903. 'end;',
  14904. '']);
  14905. ConvertUnit;
  14906. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14907. LinesToStr([
  14908. 'rtl.createClass(this, "TObject", null, function () {',
  14909. ' this.cI = 3;',
  14910. ' this.$init = function () {',
  14911. ' };',
  14912. ' this.$final = function () {',
  14913. ' };',
  14914. ' var cI$1 = 4;',
  14915. ' this.Fly = function () {',
  14916. ' if (cI$1 === this.cI) ;',
  14917. ' };',
  14918. ' var cI$2 = 5;',
  14919. ' this.Run = function () {',
  14920. ' if (cI$2 === this.cI) ;',
  14921. ' };',
  14922. '});',
  14923. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14924. ' var cI$3 = 6;',
  14925. ' this.Go = function () {',
  14926. ' if (cI$3 === this.cI) ;',
  14927. ' };',
  14928. '});',
  14929. '']),
  14930. '',
  14931. '');
  14932. end;
  14933. procedure TTestModule.TestClass_LocalVarSelfFail;
  14934. begin
  14935. StartProgram(false);
  14936. Add([
  14937. 'type',
  14938. ' TObject = class',
  14939. ' constructor Create;',
  14940. ' end;',
  14941. 'constructor tobject.create;',
  14942. 'var self: longint;',
  14943. 'begin',
  14944. 'end',
  14945. 'begin',
  14946. '']);
  14947. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14948. ConvertProgram;
  14949. end;
  14950. procedure TTestModule.TestClass_ArgSelfFail;
  14951. begin
  14952. StartProgram(false);
  14953. Add([
  14954. 'type',
  14955. ' TObject = class',
  14956. ' procedure DoIt(Self: longint);',
  14957. ' end;',
  14958. 'procedure tobject.doit(self: longint);',
  14959. 'begin',
  14960. 'end',
  14961. 'begin',
  14962. '']);
  14963. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14964. ConvertProgram;
  14965. end;
  14966. procedure TTestModule.TestClass_NestedProcSelf;
  14967. begin
  14968. StartProgram(false);
  14969. Add([
  14970. 'type',
  14971. ' TObject = class',
  14972. ' Key: longint;',
  14973. ' class var State: longint;',
  14974. ' procedure DoIt;',
  14975. ' function GetSize: longint; virtual; abstract;',
  14976. ' procedure SetSize(Value: longint); virtual; abstract;',
  14977. ' property Size: longint read GetSize write SetSize;',
  14978. ' end;',
  14979. 'procedure tobject.doit;',
  14980. ' procedure Sub;',
  14981. ' begin',
  14982. ' key:=key+2;',
  14983. ' self.key:=self.key+3;',
  14984. ' state:=state+4;',
  14985. ' self.state:=self.state+5;',
  14986. ' tobject.state:=tobject.state+6;',
  14987. ' size:=size+7;',
  14988. ' self.size:=self.size+8;',
  14989. ' end;',
  14990. 'begin',
  14991. ' sub;',
  14992. ' key:=key+12;',
  14993. ' self.key:=self.key+13;',
  14994. ' state:=state+14;',
  14995. ' self.state:=self.state+15;',
  14996. ' tobject.state:=tobject.state+16;',
  14997. ' size:=size+17;',
  14998. ' self.size:=self.size+18;',
  14999. 'end;',
  15000. 'begin',
  15001. '']);
  15002. ConvertProgram;
  15003. CheckSource('TestClass_NestedProcSelf',
  15004. LinesToStr([ // statements
  15005. 'rtl.createClass(this, "TObject", null, function () {',
  15006. ' this.State = 0;',
  15007. ' this.$init = function () {',
  15008. ' this.Key = 0;',
  15009. ' };',
  15010. ' this.$final = function () {',
  15011. ' };',
  15012. ' this.DoIt = function () {',
  15013. ' var $Self = this;',
  15014. ' function Sub() {',
  15015. ' $Self.Key = $Self.Key + 2;',
  15016. ' $Self.Key = $Self.Key + 3;',
  15017. ' $mod.TObject.State = $Self.State + 4;',
  15018. ' $mod.TObject.State = $Self.State + 5;',
  15019. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15020. ' $Self.SetSize($Self.GetSize() + 7);',
  15021. ' $Self.SetSize($Self.GetSize() + 8);',
  15022. ' };',
  15023. ' Sub();',
  15024. ' this.Key = this.Key + 12;',
  15025. ' $Self.Key = $Self.Key + 13;',
  15026. ' $mod.TObject.State = this.State + 14;',
  15027. ' $mod.TObject.State = $Self.State + 15;',
  15028. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15029. ' this.SetSize(this.GetSize() + 17);',
  15030. ' $Self.SetSize($Self.GetSize() + 18);',
  15031. ' };',
  15032. '});',
  15033. '']),
  15034. LinesToStr([ // $mod.$main
  15035. '']));
  15036. end;
  15037. procedure TTestModule.TestClass_NestedProcSelf2;
  15038. begin
  15039. StartProgram(false);
  15040. Add([
  15041. 'type',
  15042. ' TObject = class',
  15043. ' Key: longint;',
  15044. ' class var State: longint;',
  15045. ' function GetSize: longint; virtual; abstract;',
  15046. ' procedure SetSize(Value: longint); virtual; abstract;',
  15047. ' property Size: longint read GetSize write SetSize;',
  15048. ' end;',
  15049. ' TBird = class',
  15050. ' procedure DoIt;',
  15051. ' end;',
  15052. 'procedure tbird.doit;',
  15053. ' procedure Sub;',
  15054. ' begin',
  15055. ' key:=key+2;',
  15056. ' self.key:=self.key+3;',
  15057. ' state:=state+4;',
  15058. ' self.state:=self.state+5;',
  15059. ' tobject.state:=tobject.state+6;',
  15060. ' size:=size+7;',
  15061. ' self.size:=self.size+8;',
  15062. ' end;',
  15063. 'begin',
  15064. ' sub;',
  15065. ' key:=key+12;',
  15066. ' self.key:=self.key+13;',
  15067. ' state:=state+14;',
  15068. ' self.state:=self.state+15;',
  15069. ' tobject.state:=tobject.state+16;',
  15070. ' size:=size+17;',
  15071. ' self.size:=self.size+18;',
  15072. 'end;',
  15073. 'begin',
  15074. '']);
  15075. ConvertProgram;
  15076. CheckSource('TestClass_NestedProcSelf2',
  15077. LinesToStr([ // statements
  15078. 'rtl.createClass(this, "TObject", null, function () {',
  15079. ' this.State = 0;',
  15080. ' this.$init = function () {',
  15081. ' this.Key = 0;',
  15082. ' };',
  15083. ' this.$final = function () {',
  15084. ' };',
  15085. '});',
  15086. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15087. ' this.DoIt = function () {',
  15088. ' var $Self = this;',
  15089. ' function Sub() {',
  15090. ' $Self.Key = $Self.Key + 2;',
  15091. ' $Self.Key = $Self.Key + 3;',
  15092. ' $mod.TObject.State = $Self.State + 4;',
  15093. ' $mod.TObject.State = $Self.State + 5;',
  15094. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15095. ' $Self.SetSize($Self.GetSize() + 7);',
  15096. ' $Self.SetSize($Self.GetSize() + 8);',
  15097. ' };',
  15098. ' Sub();',
  15099. ' this.Key = this.Key + 12;',
  15100. ' $Self.Key = $Self.Key + 13;',
  15101. ' $mod.TObject.State = this.State + 14;',
  15102. ' $mod.TObject.State = $Self.State + 15;',
  15103. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15104. ' this.SetSize(this.GetSize() + 17);',
  15105. ' $Self.SetSize($Self.GetSize() + 18);',
  15106. ' };',
  15107. '});',
  15108. '']),
  15109. LinesToStr([ // $mod.$main
  15110. '']));
  15111. end;
  15112. procedure TTestModule.TestClass_NestedProcClassSelf;
  15113. begin
  15114. StartProgram(false);
  15115. Add([
  15116. 'type',
  15117. ' TObject = class',
  15118. ' class var State: longint;',
  15119. ' class procedure DoIt;',
  15120. ' class function GetSize: longint; virtual; abstract;',
  15121. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15122. ' class property Size: longint read GetSize write SetSize;',
  15123. ' end;',
  15124. 'class procedure tobject.doit;',
  15125. ' procedure Sub;',
  15126. ' begin',
  15127. ' state:=state+2;',
  15128. ' self.state:=self.state+3;',
  15129. ' tobject.state:=tobject.state+4;',
  15130. ' size:=size+5;',
  15131. ' self.size:=self.size+6;',
  15132. ' tobject.size:=tobject.size+7;',
  15133. ' end;',
  15134. 'begin',
  15135. ' sub;',
  15136. ' state:=state+12;',
  15137. ' self.state:=self.state+13;',
  15138. ' tobject.state:=tobject.state+14;',
  15139. ' size:=size+15;',
  15140. ' self.size:=self.size+16;',
  15141. ' tobject.size:=tobject.size+17;',
  15142. 'end;',
  15143. 'begin',
  15144. '']);
  15145. ConvertProgram;
  15146. CheckSource('TestClass_NestedProcClassSelf',
  15147. LinesToStr([ // statements
  15148. 'rtl.createClass(this, "TObject", null, function () {',
  15149. ' this.State = 0;',
  15150. ' this.$init = function () {',
  15151. ' };',
  15152. ' this.$final = function () {',
  15153. ' };',
  15154. ' this.DoIt = function () {',
  15155. ' var $Self = this;',
  15156. ' function Sub() {',
  15157. ' $mod.TObject.State = $Self.State + 2;',
  15158. ' $mod.TObject.State = $Self.State + 3;',
  15159. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15160. ' $Self.SetSize($Self.GetSize() + 5);',
  15161. ' $Self.SetSize($Self.GetSize() + 6);',
  15162. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15163. ' };',
  15164. ' Sub();',
  15165. ' $mod.TObject.State = this.State + 12;',
  15166. ' $mod.TObject.State = $Self.State + 13;',
  15167. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15168. ' this.SetSize(this.GetSize() + 15);',
  15169. ' $Self.SetSize($Self.GetSize() + 16);',
  15170. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15171. ' };',
  15172. '});',
  15173. '']),
  15174. LinesToStr([ // $mod.$main
  15175. '']));
  15176. end;
  15177. procedure TTestModule.TestClass_NestedProcCallInherited;
  15178. begin
  15179. StartProgram(false);
  15180. Add([
  15181. 'type',
  15182. ' TObject = class',
  15183. ' function DoIt(k: boolean): longint; virtual;',
  15184. ' end;',
  15185. ' TBird = class',
  15186. ' function DoIt(k: boolean): longint; override;',
  15187. ' end;',
  15188. 'function tobject.doit(k: boolean): longint;',
  15189. 'begin',
  15190. 'end;',
  15191. 'function tbird.doit(k: boolean): longint;',
  15192. ' procedure Sub;',
  15193. ' begin',
  15194. ' inherited DoIt(true);',
  15195. //' if inherited DoIt(false)=4 then ;',
  15196. ' end;',
  15197. 'begin',
  15198. ' Sub;',
  15199. ' inherited;',
  15200. ' inherited DoIt(true);',
  15201. //' if inherited DoIt(false)=14 then ;',
  15202. 'end;',
  15203. 'begin',
  15204. '']);
  15205. ConvertProgram;
  15206. CheckSource('TestClass_NestedProcCallInherited',
  15207. LinesToStr([ // statements
  15208. 'rtl.createClass(this, "TObject", null, function () {',
  15209. ' this.$init = function () {',
  15210. ' };',
  15211. ' this.$final = function () {',
  15212. ' };',
  15213. ' this.DoIt = function (k) {',
  15214. ' var Result = 0;',
  15215. ' return Result;',
  15216. ' };',
  15217. '});',
  15218. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15219. ' this.DoIt = function (k) {',
  15220. ' var $Self = this;',
  15221. ' var Result = 0;',
  15222. ' function Sub() {',
  15223. ' $mod.TObject.DoIt.call($Self, true);',
  15224. ' };',
  15225. ' Sub();',
  15226. ' $mod.TObject.DoIt.apply(this, arguments);',
  15227. ' $mod.TObject.DoIt.call(this, true);',
  15228. ' return Result;',
  15229. ' };',
  15230. '});',
  15231. '']),
  15232. LinesToStr([ // $mod.$main
  15233. '']));
  15234. end;
  15235. procedure TTestModule.TestClass_TObjectFree;
  15236. begin
  15237. StartProgram(false);
  15238. Add([
  15239. 'type',
  15240. ' TObject = class',
  15241. ' Obj: tobject;',
  15242. ' procedure Free;',
  15243. ' procedure Release;',
  15244. ' end;',
  15245. 'procedure tobject.free;',
  15246. 'begin',
  15247. 'end;',
  15248. 'procedure tobject.release;',
  15249. 'begin',
  15250. ' free;',
  15251. ' if true then free;',
  15252. 'end;',
  15253. 'function DoIt(o: tobject): tobject;',
  15254. 'var l: tobject;',
  15255. 'begin',
  15256. ' o.free;',
  15257. ' o.free();',
  15258. ' l.free;',
  15259. ' l.free();',
  15260. ' o.obj.free;',
  15261. ' o.obj.free();',
  15262. ' with o do obj.free;',
  15263. ' with o do obj.free();',
  15264. ' result.Free;',
  15265. ' result.Free();',
  15266. 'end;',
  15267. 'var o: tobject;',
  15268. ' a: array of tobject;',
  15269. 'begin',
  15270. ' o.free;',
  15271. ' o.obj.free;',
  15272. ' a[1+2].free;',
  15273. '']);
  15274. ConvertProgram;
  15275. CheckSource('TestClass_TObjectFree',
  15276. LinesToStr([ // statements
  15277. 'rtl.createClass(this, "TObject", null, function () {',
  15278. ' this.$init = function () {',
  15279. ' this.Obj = null;',
  15280. ' };',
  15281. ' this.$final = function () {',
  15282. ' this.Obj = undefined;',
  15283. ' };',
  15284. ' this.Free = function () {',
  15285. ' };',
  15286. ' this.Release = function () {',
  15287. ' this.Free();',
  15288. ' if (true) this.Free();',
  15289. ' };',
  15290. '});',
  15291. 'this.DoIt = function (o) {',
  15292. ' var Result = null;',
  15293. ' var l = null;',
  15294. ' o = rtl.freeLoc(o);',
  15295. ' o = rtl.freeLoc(o);',
  15296. ' l = rtl.freeLoc(l);',
  15297. ' l = rtl.freeLoc(l);',
  15298. ' rtl.free(o, "Obj");',
  15299. ' rtl.free(o, "Obj");',
  15300. ' rtl.free(o, "Obj");',
  15301. ' rtl.free(o, "Obj");',
  15302. ' Result = rtl.freeLoc(Result);',
  15303. ' Result = rtl.freeLoc(Result);',
  15304. ' return Result;',
  15305. '};',
  15306. 'this.o = null;',
  15307. 'this.a = [];',
  15308. '']),
  15309. LinesToStr([ // $mod.$main
  15310. 'rtl.free($mod, "o");',
  15311. 'rtl.free($mod.o, "Obj");',
  15312. 'rtl.free($mod.a, 1 + 2);',
  15313. '']));
  15314. end;
  15315. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15316. begin
  15317. StartProgram(false);
  15318. Add([
  15319. 'type',
  15320. ' TObject = class',
  15321. ' Obj: tobject;',
  15322. ' procedure Free;',
  15323. ' end;',
  15324. 'procedure tobject.free;',
  15325. 'begin',
  15326. 'end;',
  15327. 'procedure DoIt(var o: tobject);',
  15328. 'begin',
  15329. ' o.free;',
  15330. ' o.free();',
  15331. 'end;',
  15332. 'begin',
  15333. '']);
  15334. ConvertProgram;
  15335. CheckSource('TestClass_TObjectFree_VarArg',
  15336. LinesToStr([ // statements
  15337. 'rtl.createClass(this, "TObject", null, function () {',
  15338. ' this.$init = function () {',
  15339. ' this.Obj = null;',
  15340. ' };',
  15341. ' this.$final = function () {',
  15342. ' this.Obj = undefined;',
  15343. ' };',
  15344. ' this.Free = function () {',
  15345. ' };',
  15346. '});',
  15347. 'this.DoIt = function (o) {',
  15348. ' o.set(rtl.freeLoc(o.get()));',
  15349. ' o.set(rtl.freeLoc(o.get()));',
  15350. '};',
  15351. '']),
  15352. LinesToStr([ // $mod.$main
  15353. '']));
  15354. end;
  15355. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15356. begin
  15357. StartProgram(false);
  15358. Add([
  15359. 'type',
  15360. ' TObject = class',
  15361. ' constructor Create;',
  15362. ' procedure Free;',
  15363. ' end;',
  15364. 'constructor TObject.Create; begin end;',
  15365. 'procedure tobject.free; begin end;',
  15366. 'begin',
  15367. ' with tobject.create do free;',
  15368. '']);
  15369. ConvertProgram;
  15370. CheckSource('TestClass_TObjectFreeNewInstance',
  15371. LinesToStr([ // statements
  15372. 'rtl.createClass(this, "TObject", null, function () {',
  15373. ' this.$init = function () {',
  15374. ' };',
  15375. ' this.$final = function () {',
  15376. ' };',
  15377. ' this.Create = function () {',
  15378. ' return this;',
  15379. ' };',
  15380. ' this.Free = function () {',
  15381. ' };',
  15382. '});',
  15383. '']),
  15384. LinesToStr([ // $mod.$main
  15385. 'var $with = $mod.TObject.$create("Create");',
  15386. '$with=rtl.freeLoc($with);',
  15387. '']));
  15388. end;
  15389. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15390. begin
  15391. StartProgram(false);
  15392. Add([
  15393. 'type',
  15394. ' TObject = class',
  15395. ' destructor Destroy;',
  15396. ' procedure Free;',
  15397. ' end;',
  15398. 'destructor TObject.Destroy; begin end;',
  15399. 'procedure tobject.free; begin end;',
  15400. 'var o: tobject;',
  15401. 'begin',
  15402. ' o.free;',
  15403. '']);
  15404. Converter.UseLowerCase:=true;
  15405. ConvertProgram;
  15406. CheckSource('TestClass_TObjectFreeLowerCase',
  15407. LinesToStr([ // statements
  15408. 'rtl.createClass(this, "tobject", null, function () {',
  15409. ' this.$init = function () {',
  15410. ' };',
  15411. ' this.$final = function () {',
  15412. ' };',
  15413. ' rtl.tObjectDestroy = "destroy";',
  15414. ' this.destroy = function () {',
  15415. ' };',
  15416. ' this.free = function () {',
  15417. ' };',
  15418. '});',
  15419. 'this.o = null;',
  15420. '']),
  15421. LinesToStr([ // $mod.$main
  15422. 'rtl.free($mod, "o");',
  15423. '']));
  15424. end;
  15425. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15426. begin
  15427. StartProgram(false);
  15428. Add([
  15429. 'type',
  15430. ' TObject = class',
  15431. ' procedure Free;',
  15432. ' function GetObj: tobject; virtual; abstract;',
  15433. ' end;',
  15434. 'procedure tobject.free;',
  15435. 'begin',
  15436. 'end;',
  15437. 'var o: tobject;',
  15438. 'begin',
  15439. ' o.getobj.free;',
  15440. '']);
  15441. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15442. ConvertProgram;
  15443. end;
  15444. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15445. begin
  15446. StartProgram(false);
  15447. Add([
  15448. 'type',
  15449. ' TObject = class',
  15450. ' procedure Free;',
  15451. ' FObj: TObject;',
  15452. ' property Obj: tobject read FObj write FObj;',
  15453. ' end;',
  15454. 'procedure tobject.free;',
  15455. 'begin',
  15456. 'end;',
  15457. 'var o: tobject;',
  15458. 'begin',
  15459. ' o.obj.free;',
  15460. '']);
  15461. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15462. ConvertProgram;
  15463. end;
  15464. procedure TTestModule.TestClass_ForIn;
  15465. begin
  15466. StartProgram(false);
  15467. Add([
  15468. 'type',
  15469. ' TObject = class end;',
  15470. ' TItem = TObject;',
  15471. ' TEnumerator = class',
  15472. ' FCurrent: TItem;',
  15473. ' property Current: TItem read FCurrent;',
  15474. ' function MoveNext: boolean;',
  15475. ' end;',
  15476. ' TBird = class',
  15477. ' function GetEnumerator: TEnumerator;',
  15478. ' end;',
  15479. 'function TEnumerator.MoveNext: boolean;',
  15480. 'begin',
  15481. 'end;',
  15482. 'function TBird.GetEnumerator: TEnumerator;',
  15483. 'begin',
  15484. 'end;',
  15485. 'var',
  15486. ' b: TBird;',
  15487. ' i, i2: TItem;',
  15488. 'begin',
  15489. ' for i in b do i2:=i;']);
  15490. ConvertProgram;
  15491. CheckSource('TestClass_ForIn',
  15492. LinesToStr([ // statements
  15493. 'rtl.createClass(this, "TObject", null, function () {',
  15494. ' this.$init = function () {',
  15495. ' };',
  15496. ' this.$final = function () {',
  15497. ' };',
  15498. '});',
  15499. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15500. ' this.$init = function () {',
  15501. ' $mod.TObject.$init.call(this);',
  15502. ' this.FCurrent = null;',
  15503. ' };',
  15504. ' this.$final = function () {',
  15505. ' this.FCurrent = undefined;',
  15506. ' $mod.TObject.$final.call(this);',
  15507. ' };',
  15508. ' this.MoveNext = function () {',
  15509. ' var Result = false;',
  15510. ' return Result;',
  15511. ' };',
  15512. '});',
  15513. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15514. ' this.GetEnumerator = function () {',
  15515. ' var Result = null;',
  15516. ' return Result;',
  15517. ' };',
  15518. '});',
  15519. 'this.b = null;',
  15520. 'this.i = null;',
  15521. 'this.i2 = null;'
  15522. ]),
  15523. LinesToStr([ // $mod.$main
  15524. 'var $in = $mod.b.GetEnumerator();',
  15525. 'try {',
  15526. ' while ($in.MoveNext()){',
  15527. ' $mod.i = $in.FCurrent;',
  15528. ' $mod.i2 = $mod.i;',
  15529. ' }',
  15530. '} finally {',
  15531. ' $in = rtl.freeLoc($in)',
  15532. '};',
  15533. '']));
  15534. end;
  15535. procedure TTestModule.TestClass_DispatchMessage;
  15536. begin
  15537. StartProgram(false);
  15538. Add([
  15539. 'type',
  15540. ' TObject = class',
  15541. ' {$DispatchField DispInt}',
  15542. ' procedure Dispatch(var Msg); virtual; abstract;',
  15543. ' {$DispatchStrField DispStr}',
  15544. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15545. ' end;',
  15546. ' THopMsg = record',
  15547. ' DispInt: longint;',
  15548. ' end;',
  15549. ' TPutMsg = record',
  15550. ' DispStr: string;',
  15551. ' end;',
  15552. ' TBird = class',
  15553. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15554. ' procedure Run; overload; virtual; abstract;',
  15555. ' procedure Run(var Msg); overload; message ''Fast'';',
  15556. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15557. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15558. ' end;',
  15559. 'procedure TBird.Run(var Msg);',
  15560. 'begin',
  15561. 'end;',
  15562. 'begin',
  15563. '']);
  15564. ConvertProgram;
  15565. CheckSource('TestClass_Message',
  15566. LinesToStr([ // statements
  15567. 'rtl.createClass(this, "TObject", null, function () {',
  15568. ' this.$init = function () {',
  15569. ' };',
  15570. ' this.$final = function () {',
  15571. ' };',
  15572. '});',
  15573. 'rtl.recNewT(this, "THopMsg", function () {',
  15574. ' this.DispInt = 0;',
  15575. ' this.$eq = function (b) {',
  15576. ' return this.DispInt === b.DispInt;',
  15577. ' };',
  15578. ' this.$assign = function (s) {',
  15579. ' this.DispInt = s.DispInt;',
  15580. ' return this;',
  15581. ' };',
  15582. '});',
  15583. 'rtl.recNewT(this, "TPutMsg", function () {',
  15584. ' this.DispStr = "";',
  15585. ' this.$eq = function (b) {',
  15586. ' return this.DispStr === b.DispStr;',
  15587. ' };',
  15588. ' this.$assign = function (s) {',
  15589. ' this.DispStr = s.DispStr;',
  15590. ' return this;',
  15591. ' };',
  15592. '});',
  15593. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15594. ' this.Run$1 = function (Msg) {',
  15595. ' };',
  15596. ' this.$msgint = {',
  15597. ' "2": "Fly",',
  15598. ' "3": "Hop"',
  15599. ' };',
  15600. ' this.$msgstr = {',
  15601. ' Fast: "Run$1",',
  15602. ' foo: "Put"',
  15603. ' };',
  15604. '});',
  15605. '']),
  15606. LinesToStr([ // $mod.$main
  15607. '']));
  15608. end;
  15609. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15610. begin
  15611. StartProgram(false);
  15612. Add([
  15613. 'type',
  15614. ' TObject = class',
  15615. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15616. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15617. ' end;',
  15618. 'begin',
  15619. '']);
  15620. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15621. ConvertProgram;
  15622. end;
  15623. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15624. begin
  15625. StartProgram(false);
  15626. Add([
  15627. 'type',
  15628. ' TObject = class',
  15629. ' {$dispatchfield Msg}',
  15630. ' procedure Dispatch(var Msg); virtual; abstract;',
  15631. ' end;',
  15632. ' TFlyMsg = record',
  15633. ' FlyId: longint;',
  15634. ' end;',
  15635. ' TBird = class',
  15636. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15637. ' end;',
  15638. 'begin',
  15639. '']);
  15640. ConvertProgram;
  15641. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15642. end;
  15643. procedure TTestModule.TestClassOf_Create;
  15644. begin
  15645. StartProgram(false);
  15646. Add('type');
  15647. Add(' TObject = class');
  15648. Add(' constructor Create;');
  15649. Add(' end;');
  15650. Add(' TClass = class of TObject;');
  15651. Add('constructor tobject.create; begin end;');
  15652. Add('var');
  15653. Add(' Obj: tobject;');
  15654. Add(' C: tclass;');
  15655. Add('begin');
  15656. Add(' obj:=C.create;');
  15657. Add(' with c do obj:=create;');
  15658. ConvertProgram;
  15659. CheckSource('TestClassOf_Create',
  15660. LinesToStr([ // statements
  15661. 'rtl.createClass(this, "TObject", null, function () {',
  15662. ' this.$init = function () {',
  15663. ' };',
  15664. ' this.$final = function () {',
  15665. ' };',
  15666. ' this.Create = function () {',
  15667. ' return this;',
  15668. ' };',
  15669. '});',
  15670. 'this.Obj = null;',
  15671. 'this.C = null;'
  15672. ]),
  15673. LinesToStr([ // $mod.$main
  15674. '$mod.Obj = $mod.C.$create("Create");',
  15675. 'var $with = $mod.C;',
  15676. '$mod.Obj = $with.$create("Create");',
  15677. '']));
  15678. end;
  15679. procedure TTestModule.TestClassOf_Call;
  15680. begin
  15681. StartProgram(false);
  15682. Add('type');
  15683. Add(' TObject = class');
  15684. Add(' class procedure DoIt;');
  15685. Add(' end;');
  15686. Add(' TClass = class of TObject;');
  15687. Add('class procedure tobject.doit; begin end;');
  15688. Add('var');
  15689. Add(' C: tclass;');
  15690. Add('begin');
  15691. Add(' c.doit;');
  15692. Add(' with c do doit;');
  15693. ConvertProgram;
  15694. CheckSource('TestClassOf_Call',
  15695. LinesToStr([ // statements
  15696. 'rtl.createClass(this, "TObject", null, function () {',
  15697. ' this.$init = function () {',
  15698. ' };',
  15699. ' this.$final = function () {',
  15700. ' };',
  15701. ' this.DoIt = function () {',
  15702. ' };',
  15703. '});',
  15704. 'this.C = null;'
  15705. ]),
  15706. LinesToStr([ // $mod.$main
  15707. '$mod.C.DoIt();',
  15708. 'var $with = $mod.C;',
  15709. '$with.DoIt();',
  15710. '']));
  15711. end;
  15712. procedure TTestModule.TestClassOf_Assign;
  15713. begin
  15714. StartProgram(false);
  15715. Add('type');
  15716. Add(' TClass = class of TObject;');
  15717. Add(' TObject = class');
  15718. Add(' ClassType: TClass; ');
  15719. Add(' end;');
  15720. Add('var');
  15721. Add(' Obj: tobject;');
  15722. Add(' C: tclass;');
  15723. Add('begin');
  15724. Add(' c:=nil;');
  15725. Add(' c:=obj.classtype;');
  15726. ConvertProgram;
  15727. CheckSource('TestClassOf_Assign',
  15728. LinesToStr([ // statements
  15729. 'rtl.createClass(this, "TObject", null, function () {',
  15730. ' this.$init = function () {',
  15731. ' this.ClassType = null;',
  15732. ' };',
  15733. ' this.$final = function () {',
  15734. ' this.ClassType = undefined;',
  15735. ' };',
  15736. '});',
  15737. 'this.Obj = null;',
  15738. 'this.C = null;'
  15739. ]),
  15740. LinesToStr([ // $mod.$main
  15741. '$mod.C = null;',
  15742. '$mod.C = $mod.Obj.ClassType;',
  15743. '']));
  15744. end;
  15745. procedure TTestModule.TestClassOf_Is;
  15746. begin
  15747. StartProgram(false);
  15748. Add('type');
  15749. Add(' TClass = class of TObject;');
  15750. Add(' TObject = class');
  15751. Add(' end;');
  15752. Add(' TCar = class');
  15753. Add(' end;');
  15754. Add(' TCars = class of TCar;');
  15755. Add('var');
  15756. Add(' Obj: tobject;');
  15757. Add(' C: tclass;');
  15758. Add(' Cars: tcars;');
  15759. Add('begin');
  15760. Add(' if c is tcar then ;');
  15761. Add(' if c is tcars then ;');
  15762. ConvertProgram;
  15763. CheckSource('TestClassOf_Is',
  15764. LinesToStr([ // statements
  15765. 'rtl.createClass(this, "TObject", null, function () {',
  15766. ' this.$init = function () {',
  15767. ' };',
  15768. ' this.$final = function () {',
  15769. ' };',
  15770. '});',
  15771. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15772. '});',
  15773. 'this.Obj = null;',
  15774. 'this.C = null;',
  15775. 'this.Cars = null;'
  15776. ]),
  15777. LinesToStr([ // $mod.$main
  15778. 'if(rtl.is($mod.C,$mod.TCar));',
  15779. 'if(rtl.is($mod.C,$mod.TCar));',
  15780. '']));
  15781. end;
  15782. procedure TTestModule.TestClassOf_Compare;
  15783. begin
  15784. StartProgram(false);
  15785. Add('type');
  15786. Add(' TClass = class of TObject;');
  15787. Add(' TObject = class');
  15788. Add(' ClassType: TClass; ');
  15789. Add(' end;');
  15790. Add('var');
  15791. Add(' b: boolean;');
  15792. Add(' Obj: tobject;');
  15793. Add(' C: tclass;');
  15794. Add('begin');
  15795. Add(' b:=c=nil;');
  15796. Add(' b:=nil=c;');
  15797. Add(' b:=c=obj.classtype;');
  15798. Add(' b:=obj.classtype=c;');
  15799. Add(' b:=c=TObject;');
  15800. Add(' b:=TObject=c;');
  15801. Add(' b:=c<>nil;');
  15802. Add(' b:=nil<>c;');
  15803. Add(' b:=c<>obj.classtype;');
  15804. Add(' b:=obj.classtype<>c;');
  15805. Add(' b:=c<>TObject;');
  15806. Add(' b:=TObject<>c;');
  15807. ConvertProgram;
  15808. CheckSource('TestClassOf_Compare',
  15809. LinesToStr([ // statements
  15810. 'rtl.createClass(this, "TObject", null, function () {',
  15811. ' this.$init = function () {',
  15812. ' this.ClassType = null;',
  15813. ' };',
  15814. ' this.$final = function () {',
  15815. ' this.ClassType = undefined;',
  15816. ' };',
  15817. '});',
  15818. 'this.b = false;',
  15819. 'this.Obj = null;',
  15820. 'this.C = null;'
  15821. ]),
  15822. LinesToStr([ // $mod.$main
  15823. '$mod.b = $mod.C === null;',
  15824. '$mod.b = null === $mod.C;',
  15825. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15826. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15827. '$mod.b = $mod.C === $mod.TObject;',
  15828. '$mod.b = $mod.TObject === $mod.C;',
  15829. '$mod.b = $mod.C !== null;',
  15830. '$mod.b = null !== $mod.C;',
  15831. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15832. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15833. '$mod.b = $mod.C !== $mod.TObject;',
  15834. '$mod.b = $mod.TObject !== $mod.C;',
  15835. '']));
  15836. end;
  15837. procedure TTestModule.TestClassOf_ClassVar;
  15838. begin
  15839. StartProgram(false);
  15840. Add('type');
  15841. Add(' TObject = class');
  15842. Add(' class var id: longint;');
  15843. Add(' end;');
  15844. Add(' TClass = class of TObject;');
  15845. Add('var');
  15846. Add(' C: tclass;');
  15847. Add('begin');
  15848. Add(' C.id:=C.id;');
  15849. ConvertProgram;
  15850. CheckSource('TestClassOf_ClassVar',
  15851. LinesToStr([ // statements
  15852. 'rtl.createClass(this, "TObject", null, function () {',
  15853. ' this.id = 0;',
  15854. ' this.$init = function () {',
  15855. ' };',
  15856. ' this.$final = function () {',
  15857. ' };',
  15858. '});',
  15859. 'this.C = null;'
  15860. ]),
  15861. LinesToStr([ // $mod.$main
  15862. '$mod.TObject.id = $mod.C.id;',
  15863. '']));
  15864. end;
  15865. procedure TTestModule.TestClassOf_ClassMethod;
  15866. begin
  15867. StartProgram(false);
  15868. Add('type');
  15869. Add(' TObject = class');
  15870. Add(' class function DoIt(i: longint = 0): longint;');
  15871. Add(' end;');
  15872. Add(' TClass = class of TObject;');
  15873. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15874. Add('var');
  15875. Add(' i: longint;');
  15876. Add(' C: tclass;');
  15877. Add('begin');
  15878. Add(' C.DoIt;');
  15879. Add(' C.DoIt();');
  15880. Add(' i:=C.DoIt;');
  15881. Add(' i:=C.DoIt();');
  15882. ConvertProgram;
  15883. CheckSource('TestClassOf_ClassMethod',
  15884. LinesToStr([ // statements
  15885. 'rtl.createClass(this, "TObject", null, function () {',
  15886. ' this.$init = function () {',
  15887. ' };',
  15888. ' this.$final = function () {',
  15889. ' };',
  15890. ' this.DoIt = function (i) {',
  15891. ' var Result = 0;',
  15892. ' return Result;',
  15893. ' };',
  15894. '});',
  15895. 'this.i = 0;',
  15896. 'this.C = null;'
  15897. ]),
  15898. LinesToStr([ // $mod.$main
  15899. '$mod.C.DoIt(0);',
  15900. '$mod.C.DoIt(0);',
  15901. '$mod.i = $mod.C.DoIt(0);',
  15902. '$mod.i = $mod.C.DoIt(0);',
  15903. '']));
  15904. end;
  15905. procedure TTestModule.TestClassOf_ClassProperty;
  15906. begin
  15907. StartProgram(false);
  15908. Add([
  15909. 'type',
  15910. ' TObject = class',
  15911. ' class var FA: longint;',
  15912. ' class function GetA: longint;',
  15913. ' class procedure SetA(Value: longint);',
  15914. ' class property pA: longint read fa write fa;',
  15915. ' class property pB: longint read geta write seta;',
  15916. ' end;',
  15917. ' TObjectClass = class of tobject;',
  15918. 'class function tobject.geta: longint; begin end;',
  15919. 'class procedure tobject.seta(value: longint); begin end;',
  15920. 'var',
  15921. ' b: boolean;',
  15922. ' Obj: tobject;',
  15923. ' Cla: tobjectclass;',
  15924. 'begin',
  15925. ' obj.pa:=obj.pa;',
  15926. ' obj.pb:=obj.pb;',
  15927. ' b:=obj.pa=4;',
  15928. ' b:=obj.pb=obj.pb;',
  15929. ' b:=5=obj.pa;',
  15930. ' cla.pa:=6;',
  15931. ' cla.pa:=cla.pa;',
  15932. ' cla.pb:=cla.pb;',
  15933. ' b:=cla.pa=7;',
  15934. ' b:=cla.pb=cla.pb;',
  15935. ' b:=8=cla.pa;',
  15936. ' tobject.pa:=9;',
  15937. ' tobject.pb:=tobject.pb;',
  15938. ' b:=tobject.pa=10;',
  15939. ' b:=11=tobject.pa;',
  15940. '']);
  15941. ConvertProgram;
  15942. CheckSource('TestClassOf_ClassProperty',
  15943. LinesToStr([ // statements
  15944. 'rtl.createClass(this, "TObject", null, function () {',
  15945. ' this.FA = 0;',
  15946. ' this.$init = function () {',
  15947. ' };',
  15948. ' this.$final = function () {',
  15949. ' };',
  15950. ' this.GetA = function () {',
  15951. ' var Result = 0;',
  15952. ' return Result;',
  15953. ' };',
  15954. ' this.SetA = function (Value) {',
  15955. ' };',
  15956. '});',
  15957. 'this.b = false;',
  15958. 'this.Obj = null;',
  15959. 'this.Cla = null;'
  15960. ]),
  15961. LinesToStr([ // $mod.$main
  15962. '$mod.TObject.FA = $mod.Obj.FA;',
  15963. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15964. '$mod.b = $mod.Obj.FA === 4;',
  15965. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15966. '$mod.b = 5 === $mod.Obj.FA;',
  15967. '$mod.TObject.FA = 6;',
  15968. '$mod.TObject.FA = $mod.Cla.FA;',
  15969. '$mod.Cla.SetA($mod.Cla.GetA());',
  15970. '$mod.b = $mod.Cla.FA === 7;',
  15971. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15972. '$mod.b = 8 === $mod.Cla.FA;',
  15973. '$mod.TObject.FA = 9;',
  15974. '$mod.TObject.SetA($mod.TObject.GetA());',
  15975. '$mod.b = $mod.TObject.FA === 10;',
  15976. '$mod.b = 11 === $mod.TObject.FA;',
  15977. '']));
  15978. end;
  15979. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15980. begin
  15981. StartProgram(false);
  15982. Add('type');
  15983. Add(' TObject = class');
  15984. Add(' class var GlobalId: longint;');
  15985. Add(' class procedure ProcA;');
  15986. Add(' end;');
  15987. Add('class procedure tobject.proca;');
  15988. Add('var b: boolean;');
  15989. Add('begin');
  15990. Add(' b:=self=nil;');
  15991. Add(' b:=self.globalid=3;');
  15992. Add(' b:=4=self.globalid;');
  15993. Add(' self.globalid:=5;');
  15994. Add(' self.proca;');
  15995. Add('end;');
  15996. Add('begin');
  15997. ConvertProgram;
  15998. CheckSource('TestClassOf_ClassMethodSelf',
  15999. LinesToStr([ // statements
  16000. 'rtl.createClass(this, "TObject", null, function () {',
  16001. ' this.GlobalId = 0;',
  16002. ' this.$init = function () {',
  16003. ' };',
  16004. ' this.$final = function () {',
  16005. ' };',
  16006. ' this.ProcA = function () {',
  16007. ' var b = false;',
  16008. ' b = this === null;',
  16009. ' b = this.GlobalId === 3;',
  16010. ' b = 4 === this.GlobalId;',
  16011. ' $mod.TObject.GlobalId = 5;',
  16012. ' this.ProcA();',
  16013. ' };',
  16014. '});'
  16015. ]),
  16016. LinesToStr([ // $mod.$main
  16017. '']));
  16018. end;
  16019. procedure TTestModule.TestClassOf_TypeCast;
  16020. begin
  16021. StartProgram(false);
  16022. Add('type');
  16023. Add(' TObject = class');
  16024. Add(' class procedure {#TObject_DoIt}DoIt;');
  16025. Add(' end;');
  16026. Add(' TClass = class of TObject;');
  16027. Add(' TMobile = class');
  16028. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16029. Add(' end;');
  16030. Add(' TMobileClass = class of TMobile;');
  16031. Add(' TCar = class(TMobile)');
  16032. Add(' class procedure {#TCar_DoIt}DoIt;');
  16033. Add(' end;');
  16034. Add(' TCarClass = class of TCar;');
  16035. Add('class procedure TObject.DoIt;');
  16036. Add('begin');
  16037. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16038. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16039. Add('end;');
  16040. Add('class procedure TMobile.DoIt;');
  16041. Add('begin');
  16042. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16043. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16044. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16045. Add('end;');
  16046. Add('class procedure TCar.DoIt; begin end;');
  16047. Add('var');
  16048. Add(' ObjC: TClass;');
  16049. Add(' MobileC: TMobileClass;');
  16050. Add(' CarC: TCarClass;');
  16051. Add('begin');
  16052. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16053. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16054. Add(' CarC.{@TCar_DoIt}DoIt;');
  16055. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16056. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16057. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16058. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16059. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16060. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16061. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16062. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16063. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16064. ConvertProgram;
  16065. CheckSource('TestClassOf_TypeCast',
  16066. LinesToStr([ // statements
  16067. 'rtl.createClass(this, "TObject", null, function () {',
  16068. ' this.$init = function () {',
  16069. ' };',
  16070. ' this.$final = function () {',
  16071. ' };',
  16072. ' this.DoIt = function () {',
  16073. ' this.DoIt();',
  16074. ' this.DoIt$1();',
  16075. ' };',
  16076. '});',
  16077. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16078. ' this.DoIt$1 = function () {',
  16079. ' this.DoIt();',
  16080. ' this.DoIt$1();',
  16081. ' this.DoIt$2();',
  16082. ' };',
  16083. '});',
  16084. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16085. ' this.DoIt$2 = function () {',
  16086. ' };',
  16087. '});',
  16088. 'this.ObjC = null;',
  16089. 'this.MobileC = null;',
  16090. 'this.CarC = null;',
  16091. '']),
  16092. LinesToStr([ // $mod.$main
  16093. '$mod.ObjC.DoIt();',
  16094. '$mod.MobileC.DoIt$1();',
  16095. '$mod.CarC.DoIt$2();',
  16096. '$mod.ObjC.DoIt();',
  16097. '$mod.ObjC.DoIt$1();',
  16098. '$mod.ObjC.DoIt$2();',
  16099. '$mod.MobileC.DoIt();',
  16100. '$mod.MobileC.DoIt$1();',
  16101. '$mod.MobileC.DoIt$2();',
  16102. '$mod.CarC.DoIt();',
  16103. '$mod.CarC.DoIt$1();',
  16104. '$mod.CarC.DoIt$2();',
  16105. '']));
  16106. end;
  16107. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16108. begin
  16109. StartProgram(false);
  16110. Add('type');
  16111. Add(' TObject = class');
  16112. Add(' function CurNow: longint; ');
  16113. Add(' class function Now: longint; ');
  16114. Add(' end;');
  16115. Add('function TObject.CurNow: longint; begin end;');
  16116. Add('class function TObject.Now: longint; begin end;');
  16117. Add('var');
  16118. Add(' Obj: tobject;');
  16119. Add(' vI: longint;');
  16120. Add('begin');
  16121. Add(' obj.curnow;');
  16122. Add(' vi:=obj.curnow;');
  16123. Add(' tobject.now;');
  16124. Add(' vi:=tobject.now;');
  16125. ConvertProgram;
  16126. CheckSource('TestClassOf_ImplicitFunctionCall',
  16127. LinesToStr([ // statements
  16128. 'rtl.createClass(this, "TObject", null, function () {',
  16129. ' this.$init = function () {',
  16130. ' };',
  16131. ' this.$final = function () {',
  16132. ' };',
  16133. ' this.CurNow = function () {',
  16134. ' var Result = 0;',
  16135. ' return Result;',
  16136. ' };',
  16137. ' this.Now = function () {',
  16138. ' var Result = 0;',
  16139. ' return Result;',
  16140. ' };',
  16141. '});',
  16142. 'this.Obj = null;',
  16143. 'this.vI = 0;',
  16144. '']),
  16145. LinesToStr([ // $mod.$main
  16146. '$mod.Obj.CurNow();',
  16147. '$mod.vI = $mod.Obj.CurNow();',
  16148. '$mod.TObject.Now();',
  16149. '$mod.vI = $mod.TObject.Now();',
  16150. '']));
  16151. end;
  16152. procedure TTestModule.TestClassOf_Const;
  16153. begin
  16154. StartProgram(false);
  16155. Add([
  16156. 'type',
  16157. ' TObject = class',
  16158. ' end;',
  16159. ' TBird = TObject;',
  16160. ' TBirds = class of TBird;',
  16161. ' TEagles = TBirds;',
  16162. ' THawk = class(TBird);',
  16163. 'const',
  16164. ' Hawk: TEagles = THawk;',
  16165. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16166. ' TBird,',
  16167. ' THawk',
  16168. ' );',
  16169. 'begin']);
  16170. ConvertProgram;
  16171. CheckSource('TestClassOf_Const',
  16172. LinesToStr([ // statements
  16173. 'rtl.createClass(this, "TObject", null, function () {',
  16174. ' this.$init = function () {',
  16175. ' };',
  16176. ' this.$final = function () {',
  16177. ' };',
  16178. '});',
  16179. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16180. '});',
  16181. 'this.Hawk = this.THawk;',
  16182. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16183. '']),
  16184. LinesToStr([ // $mod.$main
  16185. '']));
  16186. end;
  16187. procedure TTestModule.TestNestedClass_Alias;
  16188. begin
  16189. WithTypeInfo:=true;
  16190. StartProgram(false);
  16191. Add([
  16192. 'type',
  16193. ' TObject = class',
  16194. ' type TNested = type longint;',
  16195. ' end;',
  16196. 'type TAlias = type tobject.tnested;',
  16197. 'var i: tobject.tnested = 3;',
  16198. 'var j: TAlias = 4;',
  16199. 'begin',
  16200. ' if typeinfo(TAlias)=nil then ;',
  16201. ' if typeinfo(tobject.tnested)=nil then ;',
  16202. '']);
  16203. ConvertProgram;
  16204. CheckSource('TestNestedClass_Alias',
  16205. LinesToStr([ // statements
  16206. 'rtl.createClass(this, "TObject", null, function () {',
  16207. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16208. ' this.$init = function () {',
  16209. ' };',
  16210. ' this.$final = function () {',
  16211. ' };',
  16212. '});',
  16213. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16214. 'this.i = 3;',
  16215. 'this.j = 4;',
  16216. '']),
  16217. LinesToStr([ // $mod.$main
  16218. 'if ($mod.$rtti["TAlias"] === null) ;',
  16219. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16220. '']));
  16221. end;
  16222. procedure TTestModule.TestNestedClass_Record;
  16223. begin
  16224. WithTypeInfo:=true;
  16225. StartProgram(false);
  16226. Add([
  16227. 'type',
  16228. ' TObject = class',
  16229. ' type TPoint = record',
  16230. ' x,y: byte;',
  16231. ' end;',
  16232. ' procedure DoIt(t: TPoint);',
  16233. ' end;',
  16234. 'procedure tobject.DoIt(t: TPoint);',
  16235. 'var p: TPoint;',
  16236. 'begin',
  16237. ' t.x:=t.y;',
  16238. ' p:=t;',
  16239. 'end;',
  16240. 'var',
  16241. ' p: tobject.tpoint = (x:2; y:4);',
  16242. ' o: TObject;',
  16243. 'begin',
  16244. ' p:=p;',
  16245. ' o.doit(p);',
  16246. '']);
  16247. ConvertProgram;
  16248. CheckSource('TestNestedClass_Record',
  16249. LinesToStr([ // statements
  16250. 'rtl.createClass(this, "TObject", null, function () {',
  16251. ' rtl.recNewT(this, "TPoint", function () {',
  16252. ' this.x = 0;',
  16253. ' this.y = 0;',
  16254. ' this.$eq = function (b) {',
  16255. ' return (this.x === b.x) && (this.y === b.y);',
  16256. ' };',
  16257. ' this.$assign = function (s) {',
  16258. ' this.x = s.x;',
  16259. ' this.y = s.y;',
  16260. ' return this;',
  16261. ' };',
  16262. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16263. ' $r.addField("x", rtl.byte);',
  16264. ' $r.addField("y", rtl.byte);',
  16265. ' });',
  16266. ' this.$init = function () {',
  16267. ' };',
  16268. ' this.$final = function () {',
  16269. ' };',
  16270. ' this.DoIt = function (t) {',
  16271. ' var p = this.TPoint.$new();',
  16272. ' t.x = t.y;',
  16273. ' p.$assign(t);',
  16274. ' };',
  16275. '});',
  16276. 'this.p = this.TObject.TPoint.$clone({',
  16277. ' x: 2,',
  16278. ' y: 4',
  16279. '});',
  16280. 'this.o = null;',
  16281. '']),
  16282. LinesToStr([ // $mod.$main
  16283. '$mod.p.$assign($mod.p);',
  16284. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16285. '']));
  16286. end;
  16287. procedure TTestModule.TestNestedClass_Class;
  16288. begin
  16289. WithTypeInfo:=true;
  16290. StartProgram(false);
  16291. Add([
  16292. 'type',
  16293. ' TObject = class end;',
  16294. ' TBird = class',
  16295. ' type TLeg = class',
  16296. ' FId: longint;',
  16297. ' constructor Create;',
  16298. ' function Create(i: longint): TLeg;',
  16299. ' end;',
  16300. ' function DoIt(b: TBird): Tleg;',
  16301. ' end;',
  16302. 'constructor tbird.tleg.create;',
  16303. 'begin',
  16304. ' FId:=3;',
  16305. 'end;',
  16306. 'function tbird.tleg.Create(i: longint): TLeg;',
  16307. 'begin',
  16308. ' Create;',
  16309. ' Result:=TLeg.Create;',
  16310. ' Result:=TBird.TLeg.Create;',
  16311. ' Result:=Create(3);',
  16312. ' FId:=i;',
  16313. 'end;',
  16314. 'function tbird.DoIt(b: tbird): tleg;',
  16315. 'begin',
  16316. ' Result.Create;',
  16317. ' Result:=TLeg.Create;',
  16318. ' Result:=TBird.TLeg.Create;',
  16319. ' Result:=Result.Create(3);',
  16320. 'end;',
  16321. 'var',
  16322. ' b: Tbird.tleg;',
  16323. 'begin',
  16324. ' b.Create;',
  16325. ' b:=TBird.TLeg.Create;',
  16326. ' b:=b.Create(3);',
  16327. '']);
  16328. ConvertProgram;
  16329. CheckSource('TestNestedClass_Class',
  16330. LinesToStr([ // statements
  16331. 'rtl.createClass(this, "TObject", null, function () {',
  16332. ' this.$init = function () {',
  16333. ' };',
  16334. ' this.$final = function () {',
  16335. ' };',
  16336. '});',
  16337. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16338. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16339. ' this.$init = function () {',
  16340. ' $mod.TObject.$init.call(this);',
  16341. ' this.FId = 0;',
  16342. ' };',
  16343. ' this.Create = function () {',
  16344. ' this.FId = 3;',
  16345. ' return this;',
  16346. ' };',
  16347. ' this.Create$1 = function (i) {',
  16348. ' var Result = null;',
  16349. ' this.Create();',
  16350. ' Result = $mod.TBird.TLeg.$create("Create");',
  16351. ' Result = $mod.TBird.TLeg.$create("Create");',
  16352. ' Result = this.Create$1(3);',
  16353. ' this.FId = i;',
  16354. ' return Result;',
  16355. ' };',
  16356. ' }, "TBird.TLeg");',
  16357. ' this.DoIt = function (b) {',
  16358. ' var Result = null;',
  16359. ' Result.Create();',
  16360. ' Result = this.TLeg.$create("Create");',
  16361. ' Result = $mod.TBird.TLeg.$create("Create");',
  16362. ' Result = Result.Create$1(3);',
  16363. ' return Result;',
  16364. ' };',
  16365. '});',
  16366. 'this.b = null;',
  16367. '']),
  16368. LinesToStr([ // $mod.$main
  16369. '$mod.b.Create();',
  16370. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16371. '$mod.b = $mod.b.Create$1(3);',
  16372. '']));
  16373. end;
  16374. procedure TTestModule.TestExternalClass_Var;
  16375. begin
  16376. StartProgram(false);
  16377. Add([
  16378. '{$modeswitch externalclass}',
  16379. 'type',
  16380. ' TExtA = class external name ''ExtObj''',
  16381. ' Id: longint external name ''$Id'';',
  16382. ' B: longint;',
  16383. ' end;',
  16384. 'var Obj: TExtA;',
  16385. 'begin',
  16386. ' obj.id:=obj.id+1;',
  16387. ' obj.B:=obj.B+1;']);
  16388. ConvertProgram;
  16389. CheckSource('TestExternalClass_Var',
  16390. LinesToStr([ // statements
  16391. 'this.Obj = null;',
  16392. '']),
  16393. LinesToStr([ // $mod.$main
  16394. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16395. '$mod.Obj.B = $mod.Obj.B + 1;',
  16396. '']));
  16397. end;
  16398. procedure TTestModule.TestExternalClass_Const;
  16399. begin
  16400. StartProgram(false);
  16401. Add([
  16402. '{$modeswitch externalclass}',
  16403. 'type',
  16404. ' TExtA = class external name ''ExtObj''',
  16405. ' const Two: longint = 2;',
  16406. ' const Three = 3;',
  16407. ' const Id: longint;',
  16408. ' end;',
  16409. ' TExtB = class external name ''ExtB''',
  16410. ' A: TExtA;',
  16411. ' end;',
  16412. 'var',
  16413. ' A: texta;',
  16414. ' B: textb;',
  16415. ' i: longint;',
  16416. 'begin',
  16417. ' i:=a.two;',
  16418. ' i:=texta.two;',
  16419. ' i:=a.three;',
  16420. ' i:=texta.three;',
  16421. ' i:=a.id;',
  16422. ' i:=texta.id;',
  16423. '']);
  16424. ConvertProgram;
  16425. CheckSource('TestExternalClass_Const',
  16426. LinesToStr([ // statements
  16427. 'this.A = null;',
  16428. 'this.B = null;',
  16429. 'this.i = 0;',
  16430. '']),
  16431. LinesToStr([ // $mod.$main
  16432. '$mod.i = 2;',
  16433. '$mod.i = 2;',
  16434. '$mod.i = 3;',
  16435. '$mod.i = 3;',
  16436. '$mod.i = $mod.A.Id;',
  16437. '$mod.i = ExtObj.Id;',
  16438. '']));
  16439. end;
  16440. procedure TTestModule.TestExternalClass_Dollar;
  16441. begin
  16442. StartProgram(false);
  16443. Add([
  16444. '{$modeswitch externalclass}',
  16445. 'type',
  16446. ' TExtA = class external name ''$''',
  16447. ' Id: longint external name ''$'';',
  16448. ' function Bla(i: longint): longint; external name ''$'';',
  16449. ' end;',
  16450. 'function dollar(k: longint): longint; external name ''$'';',
  16451. 'var Obj: TExtA;',
  16452. 'begin',
  16453. ' dollar(1);',
  16454. ' obj.id:=obj.id+2;',
  16455. ' obj.Bla(3);',
  16456. '']);
  16457. ConvertProgram;
  16458. CheckSource('TestExternalClass_Dollar',
  16459. LinesToStr([ // statements
  16460. 'this.Obj = null;',
  16461. '']),
  16462. LinesToStr([ // $mod.$main
  16463. '$(1);',
  16464. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16465. '$mod.Obj.$(3);',
  16466. '']));
  16467. end;
  16468. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16469. begin
  16470. StartProgram(false);
  16471. Add('{$modeswitch externalclass}');
  16472. Add('type');
  16473. Add(' TExtA = class external name ''ExtA''');
  16474. Add(' Id: longint external name ''$Id'';');
  16475. Add(' end;');
  16476. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16477. Add(' Id: longint;');
  16478. Add(' end;');
  16479. Add('begin');
  16480. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16481. ConvertProgram;
  16482. end;
  16483. procedure TTestModule.TestExternalClass_Method;
  16484. begin
  16485. StartProgram(false);
  16486. Add(['{$modeswitch externalclass}',
  16487. 'type',
  16488. ' TExtA = class external name ''ExtObj''',
  16489. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16490. ' procedure DoSome(Id: longint = 1);',
  16491. ' end;',
  16492. 'var Obj: texta;',
  16493. 'begin',
  16494. ' obj.doit;',
  16495. ' obj.doit();',
  16496. ' obj.doit(2);',
  16497. ' with obj do begin',
  16498. ' doit;',
  16499. ' doit();',
  16500. ' doit(3);',
  16501. ' end;']);
  16502. ConvertProgram;
  16503. CheckSource('TestExternalClass_Method',
  16504. LinesToStr([ // statements
  16505. 'this.Obj = null;',
  16506. '']),
  16507. LinesToStr([ // $mod.$main
  16508. '$mod.Obj.$Execute(1);',
  16509. '$mod.Obj.$Execute(1);',
  16510. '$mod.Obj.$Execute(2);',
  16511. 'var $with = $mod.Obj;',
  16512. '$with.$Execute(1);',
  16513. '$with.$Execute(1);',
  16514. '$with.$Execute(3);',
  16515. '']));
  16516. end;
  16517. procedure TTestModule.TestExternalClass_ClassMethod;
  16518. begin
  16519. StartProgram(false);
  16520. Add([
  16521. '{$modeswitch externalclass}',
  16522. 'type',
  16523. ' TExtA = class external name ''ExtObj''',
  16524. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16525. ' end;',
  16526. ' TExtB = TExtA;',
  16527. 'var p: Pointer;',
  16528. 'begin',
  16529. ' texta.doit;',
  16530. ' texta.doit();',
  16531. ' texta.doit(2);',
  16532. ' p:[email protected];',
  16533. ' with texta do begin',
  16534. ' doit;',
  16535. ' doit();',
  16536. ' doit(3);',
  16537. ' p:=@DoIt;',
  16538. ' end;',
  16539. ' textb.doit;',
  16540. ' textb.doit();',
  16541. ' textb.doit(4);',
  16542. ' with textb do begin',
  16543. ' doit;',
  16544. ' doit();',
  16545. ' doit(5);',
  16546. ' end;',
  16547. '']);
  16548. ConvertProgram;
  16549. CheckSource('TestExternalClass_ClassMethod',
  16550. LinesToStr([ // statements
  16551. 'this.p = null;',
  16552. '']),
  16553. LinesToStr([ // $mod.$main
  16554. 'ExtObj.$Execute(1);',
  16555. 'ExtObj.$Execute(1);',
  16556. 'ExtObj.$Execute(2);',
  16557. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16558. 'ExtObj.$Execute(1);',
  16559. 'ExtObj.$Execute(1);',
  16560. 'ExtObj.$Execute(3);',
  16561. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16562. 'ExtObj.$Execute(1);',
  16563. 'ExtObj.$Execute(1);',
  16564. 'ExtObj.$Execute(4);',
  16565. 'ExtObj.$Execute(1);',
  16566. 'ExtObj.$Execute(1);',
  16567. 'ExtObj.$Execute(5);',
  16568. '']));
  16569. end;
  16570. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16571. begin
  16572. StartProgram(false);
  16573. Add([
  16574. '{$modeswitch externalclass}',
  16575. 'type',
  16576. ' TExtA = class external name ''ExtObj''',
  16577. ' class procedure DoIt(Id: longint = 1); static;',
  16578. ' end;',
  16579. 'var p: Pointer;',
  16580. 'begin',
  16581. ' texta.doit;',
  16582. ' texta.doit();',
  16583. ' texta.doit(2);',
  16584. ' p:[email protected];',
  16585. ' with texta do begin',
  16586. ' doit;',
  16587. ' doit();',
  16588. ' doit(3);',
  16589. ' p:=@DoIt;',
  16590. ' end;',
  16591. '']);
  16592. ConvertProgram;
  16593. CheckSource('TestExternalClass_ClassMethodStatic',
  16594. LinesToStr([ // statements
  16595. 'this.p = null;',
  16596. '']),
  16597. LinesToStr([ // $mod.$main
  16598. 'ExtObj.DoIt(1);',
  16599. 'ExtObj.DoIt(1);',
  16600. 'ExtObj.DoIt(2);',
  16601. '$mod.p = ExtObj.DoIt;',
  16602. 'ExtObj.DoIt(1);',
  16603. 'ExtObj.DoIt(1);',
  16604. 'ExtObj.DoIt(3);',
  16605. '$mod.p = ExtObj.DoIt;',
  16606. '']));
  16607. end;
  16608. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16609. begin
  16610. StartProgram(false);
  16611. Add([
  16612. '{$modeswitch externalclass}',
  16613. 'type',
  16614. ' TBird = class external name ''Array''',
  16615. ' end;',
  16616. 'function GetPtr: Pointer;',
  16617. 'begin',
  16618. 'end;',
  16619. 'procedure Write(const p);',
  16620. 'begin',
  16621. 'end;',
  16622. 'procedure WriteLn; varargs;',
  16623. 'begin',
  16624. 'end;',
  16625. 'begin',
  16626. ' if TBird(GetPtr)=nil then ;',
  16627. ' Write(GetPtr);',
  16628. ' WriteLn(GetPtr);',
  16629. ' Write(TBird(GetPtr));',
  16630. ' WriteLn(TBird(GetPtr));',
  16631. '']);
  16632. ConvertProgram;
  16633. CheckSource('TestFunctionResultInTypeCast',
  16634. LinesToStr([ // statements
  16635. 'this.GetPtr = function () {',
  16636. ' var Result = null;',
  16637. ' return Result;',
  16638. '};',
  16639. 'this.Write = function (p) {',
  16640. '};',
  16641. 'this.WriteLn = function () {',
  16642. '};',
  16643. '']),
  16644. LinesToStr([
  16645. 'if ($mod.GetPtr() === null) ;',
  16646. '$mod.Write($mod.GetPtr());',
  16647. '$mod.WriteLn($mod.GetPtr());',
  16648. '$mod.Write($mod.GetPtr());',
  16649. '$mod.WriteLn($mod.GetPtr());',
  16650. '']));
  16651. end;
  16652. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16653. begin
  16654. StartProgram(false);
  16655. Add([
  16656. '{$modeswitch externalclass}',
  16657. 'type',
  16658. ' TExtA = class external name ''ExtObjA''',
  16659. ' procedure ProcA; virtual;',
  16660. ' procedure ProcB; virtual;',
  16661. ' end;',
  16662. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16663. ' end;',
  16664. ' TExtC = class (TExtB)',
  16665. ' procedure ProcA; override;',
  16666. ' end;',
  16667. 'procedure TExtC.ProcA;',
  16668. 'begin',
  16669. ' ProcA;',
  16670. ' Self.ProcA;',
  16671. ' ProcB;',
  16672. ' Self.ProcB;',
  16673. 'end;',
  16674. 'var',
  16675. ' A: texta;',
  16676. ' B: textb;',
  16677. ' C: textc;',
  16678. 'begin',
  16679. ' a.proca;',
  16680. ' b.proca;',
  16681. ' c.proca;']);
  16682. ConvertProgram;
  16683. CheckSource('TestExternalClass_NonExternalOverride',
  16684. LinesToStr([ // statements
  16685. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16686. ' this.$init = function () {',
  16687. ' };',
  16688. ' this.$final = function () {',
  16689. ' };',
  16690. ' this.ProcA = function () {',
  16691. ' this.ProcA();',
  16692. ' this.ProcA();',
  16693. ' this.ProcB();',
  16694. ' this.ProcB();',
  16695. ' };',
  16696. '});',
  16697. 'this.A = null;',
  16698. 'this.B = null;',
  16699. 'this.C = null;',
  16700. '']),
  16701. LinesToStr([ // $mod.$main
  16702. '$mod.A.ProcA();',
  16703. '$mod.B.ProcA();',
  16704. '$mod.C.ProcA();',
  16705. '']));
  16706. end;
  16707. procedure TTestModule.TestExternalClass_OverloadHint;
  16708. begin
  16709. StartProgram(false);
  16710. Add([
  16711. '{$modeswitch externalclass}',
  16712. 'type',
  16713. ' TExtA = class external name ''ExtObjA''',
  16714. ' procedure DoIt;',
  16715. ' procedure DoIt(i: longint);',
  16716. ' end;',
  16717. 'begin',
  16718. '']);
  16719. ConvertProgram;
  16720. CheckResolverUnexpectedHints(true);
  16721. CheckSource('TestExternalClass_OverloadHint',
  16722. LinesToStr([ // statements
  16723. '']),
  16724. LinesToStr([ // $mod.$main
  16725. '']));
  16726. end;
  16727. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16728. begin
  16729. WithTypeInfo:=true;
  16730. StartProgram(false);
  16731. Add([
  16732. '{$modeswitch externalclass}',
  16733. 'type',
  16734. ' JSwiper = class external name ''Swiper''',
  16735. ' constructor New;',
  16736. ' end;',
  16737. ' TObject = class',
  16738. ' private',
  16739. ' FSwiper: JSwiper;',
  16740. ' published',
  16741. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16742. ' end;',
  16743. 'begin',
  16744. ' JSwiper.new;',
  16745. '']);
  16746. ConvertProgram;
  16747. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16748. LinesToStr([ // statements
  16749. 'this.$rtti.$ExtClass("JSwiper", {',
  16750. ' jsclass: "Swiper"',
  16751. '});',
  16752. 'rtl.createClass(this, "TObject", null, function () {',
  16753. ' this.$init = function () {',
  16754. ' this.FSwiper = null;',
  16755. ' };',
  16756. ' this.$final = function () {',
  16757. ' this.FSwiper = undefined;',
  16758. ' };',
  16759. ' var $r = this.$rtti;',
  16760. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16761. '});',
  16762. '']),
  16763. LinesToStr([ // $mod.$main
  16764. 'new Swiper();',
  16765. '']));
  16766. end;
  16767. procedure TTestModule.TestExternalClass_Property;
  16768. begin
  16769. StartProgram(false);
  16770. Add([
  16771. '{$modeswitch externalclass}',
  16772. 'type',
  16773. ' TExtA = class external name ''ExtA''',
  16774. ' function getYear: longint;',
  16775. ' procedure setYear(Value: longint);',
  16776. ' property Year: longint read getyear write setyear;',
  16777. ' end;',
  16778. ' TExtB = class (TExtA)',
  16779. ' procedure OtherSetYear(Value: longint);',
  16780. ' property year write othersetyear;',
  16781. ' end;',
  16782. 'procedure textb.othersetyear(value: longint);',
  16783. 'begin',
  16784. ' setYear(Value+4);',
  16785. 'end;',
  16786. 'var',
  16787. ' A: texta;',
  16788. ' B: textb;',
  16789. 'begin',
  16790. ' a.year:=a.year+1;',
  16791. ' b.year:=b.year+2;']);
  16792. ConvertProgram;
  16793. CheckSource('TestExternalClass_NonExternalOverride',
  16794. LinesToStr([ // statements
  16795. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16796. ' this.$init = function () {',
  16797. ' };',
  16798. ' this.$final = function () {',
  16799. ' };',
  16800. ' this.OtherSetYear = function (Value) {',
  16801. ' this.setYear(Value+4);',
  16802. ' };',
  16803. '});',
  16804. 'this.A = null;',
  16805. 'this.B = null;',
  16806. '']),
  16807. LinesToStr([ // $mod.$main
  16808. '$mod.A.setYear($mod.A.getYear()+1);',
  16809. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16810. '']));
  16811. end;
  16812. procedure TTestModule.TestExternalClass_PropertyDate;
  16813. begin
  16814. StartProgram(false);
  16815. Add([
  16816. '{$modeswitch externalclass}',
  16817. 'type',
  16818. ' TExtA = class external name ''ExtA''',
  16819. ' end;',
  16820. ' TExtB = class (TExtA)',
  16821. ' FDate: string;',
  16822. ' property Date: string read FDate write FDate;',
  16823. ' property ExtA: string read FDate write FDate;',
  16824. ' end;',
  16825. ' {$M+}',
  16826. ' TObject = class',
  16827. ' FDate: string;',
  16828. ' published',
  16829. ' property Date: string read FDate write FDate;',
  16830. ' property ExtA: string read FDate write FDate;',
  16831. ' end;',
  16832. 'var',
  16833. ' B: textb;',
  16834. ' o: TObject;',
  16835. 'begin',
  16836. ' b.date:=b.exta;',
  16837. ' o.date:=o.exta;']);
  16838. ConvertProgram;
  16839. CheckSource('TestExternalClass_PropertyDate',
  16840. LinesToStr([ // statements
  16841. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16842. ' this.$init = function () {',
  16843. ' this.FDate = "";',
  16844. ' };',
  16845. ' this.$final = function () {',
  16846. ' };',
  16847. '});',
  16848. 'rtl.createClass(this, "TObject", null, function () {',
  16849. ' this.$init = function () {',
  16850. ' this.FDate = "";',
  16851. ' };',
  16852. ' this.$final = function () {',
  16853. ' };',
  16854. ' var $r = this.$rtti;',
  16855. ' $r.addField("FDate", rtl.string);',
  16856. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16857. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16858. '});',
  16859. 'this.B = null;',
  16860. 'this.o = null;',
  16861. '']),
  16862. LinesToStr([ // $mod.$main
  16863. '$mod.B.FDate = $mod.B.FDate;',
  16864. '$mod.o.FDate = $mod.o.FDate;',
  16865. '']));
  16866. end;
  16867. procedure TTestModule.TestExternalClass_ClassProperty;
  16868. begin
  16869. StartProgram(false);
  16870. Add('{$modeswitch externalclass}');
  16871. Add('type');
  16872. Add(' TExtA = class external name ''ExtA''');
  16873. Add(' class function getYear: longint;');
  16874. Add(' class procedure setYear(Value: longint);');
  16875. Add(' class property Year: longint read getyear write setyear;');
  16876. Add(' end;');
  16877. Add(' TExtB = class (TExtA)');
  16878. Add(' class function GetCentury: longint;');
  16879. Add(' class procedure SetCentury(Value: longint);');
  16880. Add(' class property Century: longint read getcentury write setcentury;');
  16881. Add(' end;');
  16882. Add('class function textb.getcentury: longint;');
  16883. Add('begin');
  16884. Add('end;');
  16885. Add('class procedure textb.setcentury(value: longint);');
  16886. Add('begin');
  16887. Add(' setyear(value+11);');
  16888. Add(' texta.year:=texta.year+12;');
  16889. Add(' year:=year+13;');
  16890. Add(' textb.century:=textb.century+14;');
  16891. Add(' century:=century+15;');
  16892. Add('end;');
  16893. Add('var');
  16894. Add(' A: texta;');
  16895. Add(' B: textb;');
  16896. Add('begin');
  16897. Add(' texta.year:=texta.year+1;');
  16898. Add(' textb.year:=textb.year+2;');
  16899. Add(' TextA.year:=TextA.year+3;');
  16900. Add(' b.year:=b.year+4;');
  16901. Add(' textb.century:=textb.century+5;');
  16902. Add(' b.century:=b.century+6;');
  16903. ConvertProgram;
  16904. CheckSource('TestExternalClass_ClassProperty',
  16905. LinesToStr([ // statements
  16906. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16907. ' this.$init = function () {',
  16908. ' };',
  16909. ' this.$final = function () {',
  16910. ' };',
  16911. ' this.GetCentury = function () {',
  16912. ' var Result = 0;',
  16913. ' return Result;',
  16914. ' };',
  16915. ' this.SetCentury = function (Value) {',
  16916. ' this.setYear(Value + 11);',
  16917. ' ExtA.setYear(ExtA.getYear() + 12);',
  16918. ' this.setYear(this.getYear() + 13);',
  16919. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16920. ' this.SetCentury(this.GetCentury() + 15);',
  16921. ' };',
  16922. '});',
  16923. 'this.A = null;',
  16924. 'this.B = null;',
  16925. '']),
  16926. LinesToStr([ // $mod.$main
  16927. 'ExtA.setYear(ExtA.getYear() + 1);',
  16928. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16929. 'ExtA.setYear(ExtA.getYear() + 3);',
  16930. '$mod.B.setYear($mod.B.getYear() + 4);',
  16931. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16932. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16933. '']));
  16934. end;
  16935. procedure TTestModule.TestExternalClass_ClassOf;
  16936. begin
  16937. StartProgram(false);
  16938. Add('{$modeswitch externalclass}');
  16939. Add('type');
  16940. Add(' TExtA = class external name ''ExtA''');
  16941. Add(' procedure ProcA; virtual;');
  16942. Add(' procedure ProcB; virtual;');
  16943. Add(' end;');
  16944. Add(' TExtAClass = class of TExtA;');
  16945. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16946. Add(' end;');
  16947. Add(' TExtBClass = class of TExtB;');
  16948. Add(' TExtC = class (TExtB)');
  16949. Add(' procedure ProcA; override;');
  16950. Add(' end;');
  16951. Add(' TExtCClass = class of TExtC;');
  16952. Add('procedure TExtC.ProcA; begin end;');
  16953. Add('var');
  16954. Add(' A: texta; ClA: TExtAClass;');
  16955. Add(' B: textb; ClB: TExtBClass;');
  16956. Add(' C: textc; ClC: TExtCClass;');
  16957. Add('begin');
  16958. Add(' ClA:=texta;');
  16959. Add(' ClA:=textb;');
  16960. Add(' ClA:=textc;');
  16961. Add(' ClB:=textb;');
  16962. Add(' ClB:=textc;');
  16963. Add(' ClC:=textc;');
  16964. ConvertProgram;
  16965. CheckSource('TestExternalClass_ClassOf',
  16966. LinesToStr([ // statements
  16967. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16968. ' this.$init = function () {',
  16969. ' };',
  16970. ' this.$final = function () {',
  16971. ' };',
  16972. ' this.ProcA = function () {',
  16973. ' };',
  16974. '});',
  16975. 'this.A = null;',
  16976. 'this.ClA = null;',
  16977. 'this.B = null;',
  16978. 'this.ClB = null;',
  16979. 'this.C = null;',
  16980. 'this.ClC = null;',
  16981. '']),
  16982. LinesToStr([ // $mod.$main
  16983. '$mod.ClA = ExtA;',
  16984. '$mod.ClA = ExtB;',
  16985. '$mod.ClA = $mod.TExtC;',
  16986. '$mod.ClB = ExtB;',
  16987. '$mod.ClB = $mod.TExtC;',
  16988. '$mod.ClC = $mod.TExtC;',
  16989. '']));
  16990. end;
  16991. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16992. begin
  16993. AddModuleWithIntfImplSrc('unit2.pas',
  16994. LinesToStr([
  16995. '{$modeswitch externalclass}',
  16996. 'type',
  16997. ' TExtA = class external name ''ExtA''',
  16998. ' class var Id: longint;',
  16999. ' end;',
  17000. '']),
  17001. '');
  17002. StartUnit(true);
  17003. Add('interface');
  17004. Add('uses unit2;');
  17005. Add('implementation');
  17006. Add('begin');
  17007. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17008. ConvertUnit;
  17009. CheckSource('TestExternalClass_ClassOtherUnit',
  17010. LinesToStr([
  17011. '']),
  17012. LinesToStr([
  17013. 'ExtA.Id = ExtA.Id + 1;',
  17014. '']));
  17015. end;
  17016. procedure TTestModule.TestExternalClass_Is;
  17017. begin
  17018. StartProgram(false);
  17019. Add([
  17020. '{$modeswitch externalclass}',
  17021. 'type',
  17022. ' TExtA = class external name ''ExtA''',
  17023. ' end;',
  17024. ' TExtAClass = class of TExtA;',
  17025. ' TExtB = class external name ''ExtB'' (TExtA)',
  17026. ' end;',
  17027. ' TExtBClass = class of TExtB;',
  17028. ' TExtC = class (TExtB)',
  17029. ' end;',
  17030. ' TExtCClass = class of TExtC;',
  17031. 'var',
  17032. ' A: texta; ClA: TExtAClass;',
  17033. ' B: textb; ClB: TExtBClass;',
  17034. ' C: textc; ClC: TExtCClass;',
  17035. 'begin',
  17036. ' if a is textb then ;',
  17037. ' if a is textc then ;',
  17038. ' if b is textc then ;',
  17039. ' if cla is textb then ;',
  17040. ' if cla is textc then ;',
  17041. ' if clb is textc then ;',
  17042. ' try',
  17043. ' except',
  17044. ' on TExtA do ;',
  17045. ' on e: TExtB do ;',
  17046. ' end;',
  17047. '']);
  17048. ConvertProgram;
  17049. CheckSource('TestExternalClass_Is',
  17050. LinesToStr([ // statements
  17051. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17052. ' this.$init = function () {',
  17053. ' };',
  17054. ' this.$final = function () {',
  17055. ' };',
  17056. '});',
  17057. 'this.A = null;',
  17058. 'this.ClA = null;',
  17059. 'this.B = null;',
  17060. 'this.ClB = null;',
  17061. 'this.C = null;',
  17062. 'this.ClC = null;',
  17063. '']),
  17064. LinesToStr([ // $mod.$main
  17065. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17066. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17067. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17068. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17069. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17070. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17071. 'try {} catch ($e) {',
  17072. ' if (rtl.isExt($e,ExtA)) {}',
  17073. ' else if (rtl.isExt($e,ExtB)) {',
  17074. ' var e = $e;',
  17075. ' } else throw $e',
  17076. '};',
  17077. '']));
  17078. end;
  17079. procedure TTestModule.TestExternalClass_As;
  17080. begin
  17081. StartProgram(false);
  17082. Add('{$modeswitch externalclass}');
  17083. Add('type');
  17084. Add(' TExtA = class external name ''ExtA''');
  17085. Add(' end;');
  17086. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17087. Add(' end;');
  17088. Add(' TExtC = class (TExtB)');
  17089. Add(' end;');
  17090. Add('var');
  17091. Add(' A: texta;');
  17092. Add(' B: textb;');
  17093. Add(' C: textc;');
  17094. Add('begin');
  17095. Add(' b:=a as textb;');
  17096. Add(' c:=a as textc;');
  17097. Add(' c:=b as textc;');
  17098. ConvertProgram;
  17099. CheckSource('TestExternalClass_Is',
  17100. LinesToStr([ // statements
  17101. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17102. ' this.$init = function () {',
  17103. ' };',
  17104. ' this.$final = function () {',
  17105. ' };',
  17106. '});',
  17107. 'this.A = null;',
  17108. 'this.B = null;',
  17109. 'this.C = null;',
  17110. '']),
  17111. LinesToStr([ // $mod.$main
  17112. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17113. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17114. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17115. '']));
  17116. end;
  17117. procedure TTestModule.TestExternalClass_DestructorFail;
  17118. begin
  17119. StartProgram(false);
  17120. Add('{$modeswitch externalclass}');
  17121. Add('type');
  17122. Add(' TExtA = class external name ''ExtA''');
  17123. Add(' destructor Free;');
  17124. Add(' end;');
  17125. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17126. nPasElementNotSupported);
  17127. ConvertProgram;
  17128. end;
  17129. procedure TTestModule.TestExternalClass_New;
  17130. begin
  17131. StartProgram(false);
  17132. Add([
  17133. '{$modeswitch externalclass}',
  17134. 'type',
  17135. ' TExtA = class external name ''ExtA''',
  17136. ' constructor New;',
  17137. ' constructor New(i: longint; j: longint = 2);',
  17138. ' end;',
  17139. 'var',
  17140. ' A: texta;',
  17141. 'begin',
  17142. ' a:=texta.new;',
  17143. ' a:=texta(texta.new);',
  17144. ' a:=texta.new();',
  17145. ' a:=texta.new(1);',
  17146. ' with texta do begin',
  17147. ' a:=new;',
  17148. ' a:=new();',
  17149. ' a:=new(2);',
  17150. ' end;',
  17151. ' a:=test1.texta.new;',
  17152. ' a:=test1.texta.new();',
  17153. ' a:=test1.texta.new(3);',
  17154. '']);
  17155. ConvertProgram;
  17156. CheckSource('TestExternalClass_New',
  17157. LinesToStr([ // statements
  17158. 'this.A = null;',
  17159. '']),
  17160. LinesToStr([ // $mod.$main
  17161. '$mod.A = new ExtA();',
  17162. '$mod.A = new ExtA();',
  17163. '$mod.A = new ExtA();',
  17164. '$mod.A = new ExtA(1,2);',
  17165. '$mod.A = new ExtA();',
  17166. '$mod.A = new ExtA();',
  17167. '$mod.A = new ExtA(2,2);',
  17168. '$mod.A = new ExtA();',
  17169. '$mod.A = new ExtA();',
  17170. '$mod.A = new ExtA(3,2);',
  17171. '']));
  17172. end;
  17173. procedure TTestModule.TestExternalClass_ClassOf_New;
  17174. begin
  17175. StartProgram(false);
  17176. Add('{$modeswitch externalclass}');
  17177. Add('type');
  17178. Add(' TExtAClass = class of TExtA;');
  17179. Add(' TExtA = class external name ''ExtA''');
  17180. Add(' C: TExtAClass;');
  17181. Add(' constructor New;');
  17182. Add(' end;');
  17183. Add('var');
  17184. Add(' A: texta;');
  17185. Add(' C: textaclass;');
  17186. Add('begin');
  17187. Add(' a:=c.new;');
  17188. Add(' a:=c.new();');
  17189. Add(' with C do begin');
  17190. Add(' a:=new;');
  17191. Add(' a:=new();');
  17192. Add(' end;');
  17193. Add(' a:=test1.c.new;');
  17194. Add(' a:=test1.c.new();');
  17195. Add(' a:=A.c.new();');
  17196. ConvertProgram;
  17197. CheckSource('TestExternalClass_ClassOf_New',
  17198. LinesToStr([ // statements
  17199. 'this.A = null;',
  17200. 'this.C = null;',
  17201. '']),
  17202. LinesToStr([ // $mod.$main
  17203. '$mod.A = new $mod.C();',
  17204. '$mod.A = new $mod.C();',
  17205. 'var $with = $mod.C;',
  17206. '$mod.A = new $with();',
  17207. '$mod.A = new $with();',
  17208. '$mod.A = new $mod.C();',
  17209. '$mod.A = new $mod.C();',
  17210. '$mod.A = new $mod.A.C();',
  17211. '']));
  17212. end;
  17213. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17214. begin
  17215. StartProgram(false);
  17216. Add([
  17217. '{$modeswitch externalclass}',
  17218. 'type',
  17219. ' TExtAClass = class of TExtA;',
  17220. ' TExtA = class external name ''ExtA''',
  17221. ' constructor New;',
  17222. ' end;',
  17223. 'function GetCreator: TExtAClass;',
  17224. 'begin',
  17225. ' Result:=TExtA;',
  17226. 'end;',
  17227. 'var',
  17228. ' A: texta;',
  17229. 'begin',
  17230. ' a:=getcreator.new;',
  17231. ' a:=getcreator().new;',
  17232. ' a:=getcreator().new();',
  17233. ' a:=getcreator.new();',
  17234. ' with getcreator do begin',
  17235. ' a:=new;',
  17236. ' a:=new();',
  17237. ' end;']);
  17238. ConvertProgram;
  17239. CheckSource('TestExternalClass_FuncClassOf_New',
  17240. LinesToStr([ // statements
  17241. 'this.GetCreator = function () {',
  17242. ' var Result = null;',
  17243. ' Result = ExtA;',
  17244. ' return Result;',
  17245. '};',
  17246. 'this.A = null;',
  17247. '']),
  17248. LinesToStr([ // $mod.$main
  17249. '$mod.A = new ($mod.GetCreator())();',
  17250. '$mod.A = new ($mod.GetCreator())();',
  17251. '$mod.A = new ($mod.GetCreator())();',
  17252. '$mod.A = new ($mod.GetCreator())();',
  17253. 'var $with = $mod.GetCreator();',
  17254. '$mod.A = new $with();',
  17255. '$mod.A = new $with();',
  17256. '']));
  17257. end;
  17258. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17259. begin
  17260. StartProgram(false);
  17261. Add([
  17262. '{$modeswitch externalclass}',
  17263. 'type',
  17264. ' TExtA = class external name ''ExtA''',
  17265. ' constructor New;',
  17266. ' end;',
  17267. ' TBird = class(TExtA)',
  17268. ' end;',
  17269. 'begin',
  17270. ' TBird.new;',
  17271. '']);
  17272. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17273. ConvertProgram;
  17274. end;
  17275. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17276. begin
  17277. StartProgram(false);
  17278. Add([
  17279. '{$modeswitch externalclass}',
  17280. 'type',
  17281. ' TExtA = class external name ''ExtA''',
  17282. ' constructor New;',
  17283. ' end;',
  17284. ' TBird = class(TExtA)',
  17285. ' end;',
  17286. 'begin',
  17287. ' TBird.new();',
  17288. '']);
  17289. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17290. ConvertProgram;
  17291. end;
  17292. procedure TTestModule.TestExternalClass_NewExtName;
  17293. begin
  17294. StartProgram(false);
  17295. Add([
  17296. '{$modeswitch externalclass}',
  17297. 'type',
  17298. ' TExtA = class external name ''ExtA''',
  17299. ' constructor New; external name ''Other'';',
  17300. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17301. ' end;',
  17302. 'var',
  17303. ' A: texta;',
  17304. 'begin',
  17305. ' a:=texta.new;',
  17306. ' a:=texta(texta.new);',
  17307. ' a:=texta.new();',
  17308. ' a:=texta.new(1);',
  17309. ' with texta do begin',
  17310. ' a:=new;',
  17311. ' a:=new();',
  17312. ' a:=new(2);',
  17313. ' end;',
  17314. ' a:=test1.texta.new;',
  17315. ' a:=test1.texta.new();',
  17316. ' a:=test1.texta.new(3);',
  17317. '']);
  17318. ConvertProgram;
  17319. CheckSource('TestExternalClass_NewExtName',
  17320. LinesToStr([ // statements
  17321. 'this.A = null;',
  17322. '']),
  17323. LinesToStr([ // $mod.$main
  17324. '$mod.A = new Other();',
  17325. '$mod.A = new Other();',
  17326. '$mod.A = new Other();',
  17327. '$mod.A = new A.B(1,2);',
  17328. '$mod.A = new Other();',
  17329. '$mod.A = new Other();',
  17330. '$mod.A = new A.B(2,2);',
  17331. '$mod.A = new Other();',
  17332. '$mod.A = new Other();',
  17333. '$mod.A = new A.B(3,2);',
  17334. '']));
  17335. end;
  17336. procedure TTestModule.TestExternalClass_Constructor;
  17337. begin
  17338. StartProgram(false);
  17339. Add([
  17340. '{$modeswitch externalclass}',
  17341. 'type',
  17342. ' TExtA = class external name ''ExtA''',
  17343. ' constructor Create;',
  17344. ' constructor Create(i: longint; j: longint = 2);',
  17345. ' end;',
  17346. 'var',
  17347. ' A: texta;',
  17348. 'begin',
  17349. ' a:=texta.create;',
  17350. ' a:=texta(texta.create);',
  17351. ' a:=texta.create();',
  17352. ' a:=texta.create(1);',
  17353. ' with texta do begin',
  17354. ' a:=create;',
  17355. ' a:=create();',
  17356. ' a:=create(2);',
  17357. ' end;',
  17358. ' a:=test1.texta.create;',
  17359. ' a:=test1.texta.create();',
  17360. ' a:=test1.texta.create(3);',
  17361. '']);
  17362. ConvertProgram;
  17363. CheckSource('TestExternalClass_Constructor',
  17364. LinesToStr([ // statements
  17365. 'this.A = null;',
  17366. '']),
  17367. LinesToStr([ // $mod.$main
  17368. '$mod.A = new ExtA.Create();',
  17369. '$mod.A = new ExtA.Create();',
  17370. '$mod.A = new ExtA.Create();',
  17371. '$mod.A = new ExtA.Create(1,2);',
  17372. '$mod.A = new ExtA.Create();',
  17373. '$mod.A = new ExtA.Create();',
  17374. '$mod.A = new ExtA.Create(2,2);',
  17375. '$mod.A = new ExtA.Create();',
  17376. '$mod.A = new ExtA.Create();',
  17377. '$mod.A = new ExtA.Create(3,2);',
  17378. '']));
  17379. end;
  17380. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17381. begin
  17382. StartProgram(false);
  17383. Add([
  17384. '{$modeswitch externalclass}',
  17385. 'type',
  17386. ' TExtA = class external name ''ExtA''',
  17387. ' constructor Create; external name ''{}'';',
  17388. ' end;',
  17389. 'var',
  17390. ' A: texta;',
  17391. 'begin',
  17392. ' a:=texta.create;',
  17393. ' a:=texta(texta.create);',
  17394. ' a:=texta.create();',
  17395. ' with texta do begin',
  17396. ' a:=create;',
  17397. ' a:=create();',
  17398. ' end;',
  17399. ' a:=test1.texta.create;',
  17400. ' a:=test1.texta.create();',
  17401. '']);
  17402. ConvertProgram;
  17403. CheckSource('TestExternalClass_ConstructorBrackets',
  17404. LinesToStr([ // statements
  17405. 'this.A = null;',
  17406. '']),
  17407. LinesToStr([ // $mod.$main
  17408. '$mod.A = {};',
  17409. '$mod.A = {};',
  17410. '$mod.A = {};',
  17411. '$mod.A = {};',
  17412. '$mod.A = {};',
  17413. '$mod.A = {};',
  17414. '$mod.A = {};',
  17415. '']));
  17416. end;
  17417. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17418. begin
  17419. StartProgram(false);
  17420. Add('{$modeswitch externalclass}');
  17421. Add('type');
  17422. Add(' TExtA = class external name ''ExtA''');
  17423. Add(' constructor New;');
  17424. Add(' end;');
  17425. Add('function DoIt: longint;');
  17426. Add('const ExtA: longint = 3;');
  17427. Add('begin');
  17428. Add(' Result:=ExtA;');
  17429. Add('end;');
  17430. Add('var');
  17431. Add(' A: texta;');
  17432. Add('begin');
  17433. Add(' a:=texta.new;');
  17434. ConvertProgram;
  17435. CheckSource('TestExternalClass_LocalConstSameName',
  17436. LinesToStr([ // statements
  17437. 'var ExtA$1 = 3;',
  17438. 'this.DoIt = function () {',
  17439. ' var Result = 0;',
  17440. ' Result = ExtA$1;',
  17441. ' return Result;',
  17442. '};',
  17443. 'this.A = null;',
  17444. '']),
  17445. LinesToStr([ // $mod.$main
  17446. '$mod.A = new ExtA();',
  17447. '']));
  17448. end;
  17449. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17450. begin
  17451. StartProgram(false);
  17452. Add('{$modeswitch externalclass}');
  17453. Add('type');
  17454. Add(' TExtA = class external name ''ExtA''');
  17455. Add(' procedure DoIt;');
  17456. Add(' end;');
  17457. Add(' TMyA = class(TExtA)');
  17458. Add(' procedure DoIt;');
  17459. Add(' end;');
  17460. Add('procedure TMyA.DoIt; begin end;');
  17461. Add('begin');
  17462. ConvertProgram;
  17463. CheckSource('TestExternalClass_ReintroduceOverload',
  17464. LinesToStr([ // statements
  17465. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17466. ' this.$init = function () {',
  17467. ' };',
  17468. ' this.$final = function () {',
  17469. ' };',
  17470. ' this.DoIt$1 = function () {',
  17471. ' };',
  17472. '});',
  17473. '']),
  17474. LinesToStr([ // $mod.$main
  17475. '']));
  17476. end;
  17477. procedure TTestModule.TestExternalClass_Inherited;
  17478. begin
  17479. StartProgram(false);
  17480. Add('{$modeswitch externalclass}');
  17481. Add('type');
  17482. Add(' TExtA = class external name ''ExtA''');
  17483. Add(' procedure DoIt(i: longint = 1); virtual;');
  17484. Add(' procedure DoSome(j: longint = 2);');
  17485. Add(' end;');
  17486. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17487. Add(' end;');
  17488. Add(' TMyC = class(TExtB)');
  17489. Add(' procedure DoIt(i: longint = 1); override;');
  17490. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17491. Add(' end;');
  17492. Add('procedure TMyC.DoIt(i: longint);');
  17493. Add('begin');
  17494. Add(' inherited;');
  17495. Add(' inherited DoIt;');
  17496. Add(' inherited DoIt();');
  17497. Add(' inherited DoIt(3);');
  17498. Add(' inherited DoSome;');
  17499. Add(' inherited DoSome();');
  17500. Add(' inherited DoSome(4);');
  17501. Add('end;');
  17502. Add('procedure TMyC.DoSome(j: longint);');
  17503. Add('begin');
  17504. Add(' inherited;');
  17505. Add('end;');
  17506. Add('begin');
  17507. ConvertProgram;
  17508. CheckSource('TestExternalClass_ReintroduceOverload',
  17509. LinesToStr([ // statements
  17510. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17511. ' this.$init = function () {',
  17512. ' };',
  17513. ' this.$final = function () {',
  17514. ' };',
  17515. ' this.DoIt = function (i) {',
  17516. ' ExtB.DoIt.apply(this, arguments);',
  17517. ' ExtB.DoIt.call(this, 1);',
  17518. ' ExtB.DoIt.call(this, 1);',
  17519. ' ExtB.DoIt.call(this, 3);',
  17520. ' ExtB.DoSome.call(this, 2);',
  17521. ' ExtB.DoSome.call(this, 2);',
  17522. ' ExtB.DoSome.call(this, 4);',
  17523. ' };',
  17524. ' this.DoSome$1 = function (j) {',
  17525. ' ExtB.DoSome.apply(this, arguments);',
  17526. ' };',
  17527. '});',
  17528. '']),
  17529. LinesToStr([ // $mod.$main
  17530. '']));
  17531. end;
  17532. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17533. begin
  17534. StartProgram(false);
  17535. Add('{$modeswitch externalclass}');
  17536. Add('type');
  17537. Add(' TObject = class');
  17538. Add(' end;');
  17539. Add(' TExtA = class external name ''ExtA''(TObject)');
  17540. Add(' end;');
  17541. Add('begin');
  17542. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17543. ConvertProgram;
  17544. end;
  17545. procedure TTestModule.TestExternalClass_NewInstance;
  17546. begin
  17547. StartProgram(false);
  17548. Add('{$modeswitch externalclass}');
  17549. Add('type');
  17550. Add(' TExtA = class external name ''ExtA''');
  17551. Add(' end;');
  17552. Add(' TMyB = class(TExtA)');
  17553. Add(' protected');
  17554. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17555. Add(' end;');
  17556. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17557. Add('begin end;');
  17558. Add('begin');
  17559. ConvertProgram;
  17560. CheckSource('TestExternalClass_NewInstance',
  17561. LinesToStr([ // statements
  17562. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17563. ' this.$init = function () {',
  17564. ' };',
  17565. ' this.$final = function () {',
  17566. ' };',
  17567. ' this.NewInstance = function (fnname, paramarray) {',
  17568. ' var Result = null;',
  17569. ' return Result;',
  17570. ' };',
  17571. '});',
  17572. '']),
  17573. LinesToStr([ // $mod.$main
  17574. '']));
  17575. end;
  17576. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17577. begin
  17578. StartProgram(false);
  17579. Add('{$modeswitch externalclass}');
  17580. Add('type');
  17581. Add(' TExtA = class external name ''ExtA''');
  17582. Add(' end;');
  17583. Add(' TMyB = class(TExtA)');
  17584. Add(' protected');
  17585. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17586. Add(' end;');
  17587. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17588. Add('begin end;');
  17589. Add('begin');
  17590. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17591. ConvertProgram;
  17592. end;
  17593. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17594. begin
  17595. StartProgram(false);
  17596. Add('{$modeswitch externalclass}');
  17597. Add('type');
  17598. Add(' TExtA = class external name ''ExtA''');
  17599. Add(' end;');
  17600. Add(' TMyB = class(TExtA)');
  17601. Add(' protected');
  17602. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17603. Add(' end;');
  17604. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17605. Add('begin end;');
  17606. Add('begin');
  17607. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17608. nIncompatibleTypeArgNo);
  17609. ConvertProgram;
  17610. end;
  17611. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17612. begin
  17613. StartProgram(false);
  17614. Add('{$modeswitch externalclass}');
  17615. Add('type');
  17616. Add(' TExtA = class external name ''ExtA''');
  17617. Add(' end;');
  17618. Add(' TMyB = class(TExtA)');
  17619. Add(' protected');
  17620. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17621. Add(' end;');
  17622. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17623. Add('begin end;');
  17624. Add('begin');
  17625. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17626. nIncompatibleTypeArgNo);
  17627. ConvertProgram;
  17628. end;
  17629. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17630. begin
  17631. StartProgram(false);
  17632. Add([
  17633. '{$modeswitch externalclass}',
  17634. 'type',
  17635. ' TJSFunction = class external name ''Function''',
  17636. ' end;',
  17637. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17638. ' constructor New(w: word);',
  17639. ' end;',
  17640. ' TBird = class (TExtA)',
  17641. ' public',
  17642. ' Size: word;',
  17643. ' class var Legs: word;',
  17644. ' constructor Create(a: word);',
  17645. ' end;',
  17646. ' TEagle = class (TBird)',
  17647. ' public',
  17648. ' constructor Create(b: word); reintroduce;',
  17649. ' end;',
  17650. 'constructor TBird.Create(a: word);',
  17651. 'begin',
  17652. ' inherited;', // silently ignored
  17653. ' inherited New(a);', // this.$func(a)
  17654. 'end;',
  17655. 'constructor TEagle.Create(b: word);',
  17656. 'begin',
  17657. ' inherited Create(b);',
  17658. 'end;',
  17659. 'var',
  17660. ' Bird: TBird;',
  17661. ' Eagle: TEagle;',
  17662. 'begin',
  17663. ' Bird:=TBird.Create(3);',
  17664. ' Eagle:=TEagle.Create(4);',
  17665. ' Bird.Size:=Bird.Size+5;',
  17666. ' Bird.Legs:=Bird.Legs+6;',
  17667. ' Eagle.Size:=Eagle.Size+5;',
  17668. ' Eagle.Legs:=Eagle.Legs+6;',
  17669. '']);
  17670. ConvertProgram;
  17671. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17672. LinesToStr([ // statements
  17673. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17674. ' this.Legs = 0;',
  17675. ' this.$init = function () {',
  17676. ' this.Size = 0;',
  17677. ' };',
  17678. ' this.$final = function () {',
  17679. ' };',
  17680. ' this.Create = function (a) {',
  17681. ' this.$ancestorfunc(a);',
  17682. ' return this;',
  17683. ' };',
  17684. '});',
  17685. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17686. ' this.Create$1 = function (b) {',
  17687. ' $mod.TBird.Create.call(this, b);',
  17688. ' return this;',
  17689. ' };',
  17690. '});',
  17691. 'this.Bird = null;',
  17692. 'this.Eagle = null;',
  17693. '']),
  17694. LinesToStr([ // $mod.$main
  17695. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17696. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17697. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17698. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17699. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17700. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17701. '']));
  17702. end;
  17703. procedure TTestModule.TestExternalClass_PascalProperty;
  17704. begin
  17705. StartProgram(false);
  17706. Add('{$modeswitch externalclass}');
  17707. Add('type');
  17708. Add(' TJSElement = class;');
  17709. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17710. Add(' TJSElement = class external name ''ExtA''');
  17711. Add(' end;');
  17712. Add(' TControl = class(TJSElement)');
  17713. Add(' private');
  17714. Add(' FOnClick: TJSNotifyEvent;');
  17715. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17716. Add(' procedure Click(Sender: TJSElement);');
  17717. Add(' end;');
  17718. Add('procedure TControl.Click(Sender: TJSElement);');
  17719. Add('begin');
  17720. Add(' OnClick(Self);');
  17721. Add('end;');
  17722. Add('var');
  17723. Add(' Ctrl: TControl;');
  17724. Add('begin');
  17725. Add(' Ctrl.OnClick:[email protected];');
  17726. Add(' Ctrl.OnClick(Ctrl);');
  17727. ConvertProgram;
  17728. CheckSource('TestExternalClass_PascalProperty',
  17729. LinesToStr([ // statements
  17730. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17731. ' this.$init = function () {',
  17732. ' this.FOnClick = null;',
  17733. ' };',
  17734. ' this.$final = function () {',
  17735. ' this.FOnClick = undefined;',
  17736. ' };',
  17737. ' this.Click = function (Sender) {',
  17738. ' this.FOnClick(this);',
  17739. ' };',
  17740. '});',
  17741. 'this.Ctrl = null;',
  17742. '']),
  17743. LinesToStr([ // $mod.$main
  17744. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17745. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17746. '']));
  17747. end;
  17748. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17749. begin
  17750. StartProgram(false);
  17751. Add([
  17752. '{$modeswitch externalclass}',
  17753. 'type',
  17754. ' IUnknown = interface end;',
  17755. ' TObject = class',
  17756. ' end;',
  17757. ' TChild = class',
  17758. ' end;',
  17759. ' TExtRootA = class external name ''ExtRootA''',
  17760. ' end;',
  17761. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17762. ' end;',
  17763. ' TExtRootB = class external name ''ExtRootB''',
  17764. ' end;',
  17765. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17766. ' end;',
  17767. 'var',
  17768. ' Obj: TObject;',
  17769. ' Child: TChild;',
  17770. ' RootA: TExtRootA;',
  17771. ' ChildA: TExtChildA;',
  17772. ' RootB: TExtRootB;',
  17773. ' ChildB: TExtChildB;',
  17774. ' i: IUnknown;',
  17775. 'begin',
  17776. ' obj:=tobject(roota);',
  17777. ' obj:=tobject(childa);',
  17778. ' child:=tchild(tobject(roota));',
  17779. ' roota:=textroota(obj);',
  17780. ' roota:=textroota(child);',
  17781. ' roota:=textroota(rootb);',
  17782. ' roota:=textroota(childb);',
  17783. ' childa:=textchilda(textroota(obj));',
  17784. ' roota:=TExtRootA(i)',
  17785. '']);
  17786. ConvertProgram;
  17787. CheckSource('TestExternalClass_TypeCastToRootClass',
  17788. LinesToStr([ // statements
  17789. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17790. 'rtl.createClass(this, "TObject", null, function () {',
  17791. ' this.$init = function () {',
  17792. ' };',
  17793. ' this.$final = function () {',
  17794. ' };',
  17795. '});',
  17796. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17797. '});',
  17798. 'this.Obj = null;',
  17799. 'this.Child = null;',
  17800. 'this.RootA = null;',
  17801. 'this.ChildA = null;',
  17802. 'this.RootB = null;',
  17803. 'this.ChildB = null;',
  17804. 'this.i = null;',
  17805. '']),
  17806. LinesToStr([ // $mod.$main
  17807. '$mod.Obj = $mod.RootA;',
  17808. '$mod.Obj = $mod.ChildA;',
  17809. '$mod.Child = $mod.RootA;',
  17810. '$mod.RootA = $mod.Obj;',
  17811. '$mod.RootA = $mod.Child;',
  17812. '$mod.RootA = $mod.RootB;',
  17813. '$mod.RootA = $mod.ChildB;',
  17814. '$mod.ChildA = $mod.Obj;',
  17815. '$mod.RootA = $mod.i;',
  17816. '']));
  17817. end;
  17818. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17819. begin
  17820. StartProgram(false);
  17821. Add([
  17822. '{$modeswitch externalclass}',
  17823. 'type',
  17824. ' IUnknown = interface end;',
  17825. ' IBird = interface(IUnknown) end;',
  17826. ' TClass = class of TObject;',
  17827. ' TObject = class',
  17828. ' end;',
  17829. ' TChild = class',
  17830. ' end;',
  17831. ' TJSObject = class external name ''Object''',
  17832. ' end;',
  17833. ' TRec = record end;',
  17834. 'var',
  17835. ' Obj: TObject;',
  17836. ' Child: TChild;',
  17837. ' i: IUnknown;',
  17838. ' Bird: IBird;',
  17839. ' j: TJSObject;',
  17840. ' r: TRec;',
  17841. ' c: TClass;',
  17842. 'begin',
  17843. ' j:=tjsobject(IUnknown);',
  17844. ' j:=tjsobject(IBird);',
  17845. ' j:=tjsobject(TObject);',
  17846. ' j:=tjsobject(TChild);',
  17847. ' j:=tjsobject(TRec);',
  17848. ' j:=tjsobject(Obj);',
  17849. ' j:=tjsobject(Child);',
  17850. ' j:=tjsobject(i);',
  17851. ' j:=tjsobject(Bird);',
  17852. ' j:=tjsobject(r);',
  17853. ' j:=tjsobject(c);',
  17854. '']);
  17855. ConvertProgram;
  17856. CheckSource('TestExternalClass_TypeCastToJSObject',
  17857. LinesToStr([ // statements
  17858. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17859. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17860. 'rtl.createClass(this, "TObject", null, function () {',
  17861. ' this.$init = function () {',
  17862. ' };',
  17863. ' this.$final = function () {',
  17864. ' };',
  17865. '});',
  17866. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17867. '});',
  17868. 'rtl.recNewT(this, "TRec", function () {',
  17869. ' this.$eq = function (b) {',
  17870. ' return true;',
  17871. ' };',
  17872. ' this.$assign = function (s) {',
  17873. ' return this;',
  17874. ' };',
  17875. '});',
  17876. 'this.Obj = null;',
  17877. 'this.Child = null;',
  17878. 'this.i = null;',
  17879. 'this.Bird = null;',
  17880. 'this.j = null;',
  17881. 'this.r = this.TRec.$new();',
  17882. 'this.c = null;',
  17883. '']),
  17884. LinesToStr([ // $mod.$main
  17885. '$mod.j = $mod.IUnknown;',
  17886. '$mod.j = $mod.IBird;',
  17887. '$mod.j = $mod.TObject;',
  17888. '$mod.j = $mod.TChild;',
  17889. '$mod.j = $mod.TRec;',
  17890. '$mod.j = $mod.Obj;',
  17891. '$mod.j = $mod.Child;',
  17892. '$mod.j = $mod.i;',
  17893. '$mod.j = $mod.Bird;',
  17894. '$mod.j = $mod.r;',
  17895. '$mod.j = $mod.c;',
  17896. '']));
  17897. end;
  17898. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17899. begin
  17900. StartProgram(false);
  17901. Add('{$modeswitch externalclass}');
  17902. Add('type');
  17903. Add(' TJSString = class external name ''String''');
  17904. Add(' class function fromCharCode() : string; varargs;');
  17905. Add(' function anchor(const aName : string) : string;');
  17906. Add(' end;');
  17907. Add('var');
  17908. Add(' s: string;');
  17909. Add('begin');
  17910. Add(' s:=TJSString.fromCharCode(65,66);');
  17911. Add(' s:=TJSString(s).anchor(s);');
  17912. Add(' s:=TJSString(''foo'').anchor(s);');
  17913. ConvertProgram;
  17914. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17915. LinesToStr([ // statements
  17916. 'this.s = "";',
  17917. '']),
  17918. LinesToStr([ // $mod.$main
  17919. '$mod.s = String.fromCharCode(65, 66);',
  17920. '$mod.s = $mod.s.anchor($mod.s);',
  17921. '$mod.s = "foo".anchor($mod.s);',
  17922. '']));
  17923. end;
  17924. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17925. begin
  17926. StartProgram(false);
  17927. Add([
  17928. '{$modeswitch externalclass}',
  17929. 'type',
  17930. ' TJSObject = class external name ''Object'' end;',
  17931. ' TJSFunction = class external name ''Function''',
  17932. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17933. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17934. ' end;',
  17935. ' TObject = class',
  17936. ' procedure DoIt(i: longint);',
  17937. ' end;',
  17938. ' TFuncInt = function(o: TObject): longint;',
  17939. 'function GetIt(o: TObject): longint;',
  17940. ' procedure Sub; begin end;',
  17941. 'var',
  17942. ' f: TJSFunction;',
  17943. ' fi: TFuncInt;',
  17944. 'begin',
  17945. ' fi:=TFuncInt(f);',
  17946. ' f:=TJSFunction(fi);',
  17947. ' f:=TJSFunction(@GetIt);',
  17948. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17949. ' f:=TJSFunction(@Sub);',
  17950. ' f:=TJSFunction(@o.doit);',
  17951. ' f:=TJSFunction(fi).bind(nil,4)',
  17952. 'end;',
  17953. 'procedure TObject.DoIt(i: longint);',
  17954. ' procedure Sub; begin end;',
  17955. 'var f: TJSFunction;',
  17956. 'begin',
  17957. ' f:=TJSFunction(@DoIt);',
  17958. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17959. ' f:=TJSFunction(@Sub);',
  17960. ' f:=TJSFunction(@GetIt);',
  17961. 'end;',
  17962. 'begin']);
  17963. ConvertProgram;
  17964. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17965. LinesToStr([ // statements
  17966. 'rtl.createClass(this, "TObject", null, function () {',
  17967. ' this.$init = function () {',
  17968. ' };',
  17969. ' this.$final = function () {',
  17970. ' };',
  17971. ' this.DoIt = function (i) {',
  17972. ' var $Self = this;',
  17973. ' function Sub() {',
  17974. ' };',
  17975. ' var f = null;',
  17976. ' f = this.DoIt;',
  17977. ' f = this.DoIt.bind(null, 13);',
  17978. ' f = Sub;',
  17979. ' f = $mod.GetIt;',
  17980. ' };',
  17981. '});',
  17982. 'this.GetIt = function (o) {',
  17983. ' var Result = 0;',
  17984. ' function Sub() {',
  17985. ' };',
  17986. ' var f = null;',
  17987. ' var fi = null;',
  17988. ' fi = f;',
  17989. ' f = fi;',
  17990. ' f = $mod.GetIt;',
  17991. ' f = $mod.GetIt.bind(null, 3);',
  17992. ' f = Sub;',
  17993. ' f = $mod.TObject.DoIt;',
  17994. ' f = fi.bind(null, 4);',
  17995. ' return Result;',
  17996. '};',
  17997. '']),
  17998. LinesToStr([ // $mod.$main
  17999. '']));
  18000. end;
  18001. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18002. begin
  18003. StartProgram(false);
  18004. Add([
  18005. '{$mode delphi}',
  18006. '{$modeswitch externalclass}',
  18007. 'type',
  18008. ' TJSObject = class external name ''Object'' end;',
  18009. ' TJSWindow = class external name ''Window''(TJSObject)',
  18010. ' procedure Open;',
  18011. ' end;',
  18012. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18013. ' procedure Execute;',
  18014. ' end;',
  18015. 'procedure Fly;',
  18016. 'var',
  18017. ' w: TJSWindow;',
  18018. ' e: TJSEventTarget;',
  18019. 'begin',
  18020. ' w:=TJSWindow(e);',
  18021. ' e:=TJSEventTarget(w);',
  18022. 'end;',
  18023. 'begin']);
  18024. ConvertProgram;
  18025. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18026. LinesToStr([ // statements
  18027. 'this.Fly = function () {',
  18028. ' var w = null;',
  18029. ' var e = null;',
  18030. ' w = e;',
  18031. ' e = w;',
  18032. '};',
  18033. '']),
  18034. LinesToStr([ // $mod.$main
  18035. '']));
  18036. end;
  18037. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18038. begin
  18039. StartProgram(false);
  18040. Add('{$modeswitch externalclass}');
  18041. Add('type');
  18042. Add(' TJSString = class external name ''String''');
  18043. Add(' class function fromCharCode() : string; varargs;');
  18044. Add(' end;');
  18045. Add('var');
  18046. Add(' s: string;');
  18047. Add(' sObj: TJSString;');
  18048. Add('begin');
  18049. Add(' s:=sObj.fromCharCode(65,66);');
  18050. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18051. nExternalClassInstanceCannotAccessStaticX);
  18052. ConvertProgram;
  18053. end;
  18054. procedure TTestModule.TestExternalClass_BracketAccessor;
  18055. begin
  18056. StartProgram(false);
  18057. Add([
  18058. '{$modeswitch externalclass}',
  18059. 'type',
  18060. ' TJSArray = class external name ''Array2''',
  18061. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18062. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18063. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18064. ' end;',
  18065. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18066. 'begin end;',
  18067. 'var',
  18068. ' Arr: tjsarray;',
  18069. ' s: string;',
  18070. ' i: longint;',
  18071. ' v: jsvalue;',
  18072. 'begin',
  18073. ' v:=arr[0];',
  18074. ' v:=arr.items[1];',
  18075. ' arr[2]:=s;',
  18076. ' arr.items[3]:=s;',
  18077. ' arr[4]:=i;',
  18078. ' arr[5]:=arr[6];',
  18079. ' arr.items[7]:=arr.items[8];',
  18080. ' with arr do items[9]:=items[10];',
  18081. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18082. ' with arr do begin',
  18083. ' v:=GetItems(14);',
  18084. ' setitems(15,16);',
  18085. ' end;',
  18086. ' v:=test1.arr.items[17];',
  18087. ' test1.arr.items[18]:=v;',
  18088. '']);
  18089. ConvertProgram;
  18090. CheckSource('TestExternalClass_BracketAccessor',
  18091. LinesToStr([ // statements
  18092. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18093. '};',
  18094. 'this.Arr = null;',
  18095. 'this.s = "";',
  18096. 'this.i = 0;',
  18097. 'this.v = undefined;',
  18098. '']),
  18099. LinesToStr([ // $mod.$main
  18100. '$mod.v = $mod.Arr[0];',
  18101. '$mod.v = $mod.Arr[1];',
  18102. '$mod.Arr[2] = $mod.s;',
  18103. '$mod.Arr[3] = $mod.s;',
  18104. '$mod.Arr[4] = $mod.i;',
  18105. '$mod.Arr[5] = $mod.Arr[6];',
  18106. '$mod.Arr[7] = $mod.Arr[8];',
  18107. 'var $with = $mod.Arr;',
  18108. '$with[9] = $with[10];',
  18109. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18110. ' a: 9,',
  18111. ' p: $mod.Arr,',
  18112. ' get: function () {',
  18113. ' return this.p[this.a];',
  18114. ' },',
  18115. ' set: function (v) {',
  18116. ' this.p[this.a] = v;',
  18117. ' }',
  18118. '}, {',
  18119. ' a: 10,',
  18120. ' p: $mod.Arr,',
  18121. ' get: function () {',
  18122. ' return this.p[this.a];',
  18123. ' },',
  18124. ' set: function (v) {',
  18125. ' this.p[this.a] = v;',
  18126. ' }',
  18127. '});',
  18128. 'var $with1 = $mod.Arr;',
  18129. '$mod.v = $with1[14];',
  18130. '$with1[15] = 16;',
  18131. '$mod.v = $mod.Arr[17];',
  18132. '$mod.Arr[18] = $mod.v;',
  18133. '']));
  18134. end;
  18135. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18136. begin
  18137. StartProgram(false);
  18138. Add([
  18139. '{$modeswitch externalclass}',
  18140. 'type',
  18141. ' TJSArray = class external name ''Array2''',
  18142. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18143. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18144. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18145. ' end;',
  18146. ' TMyArr = class(TJSArray)',
  18147. ' procedure DoIt;',
  18148. ' end;',
  18149. 'procedure tmyarr.DoIt;',
  18150. 'begin',
  18151. ' Items[1]:=Items[2];',
  18152. ' SetItems(3,getItems(4));',
  18153. 'end;',
  18154. 'var',
  18155. ' Arr: tmyarr;',
  18156. ' s: string;',
  18157. ' i: longint;',
  18158. ' v: jsvalue;',
  18159. 'begin',
  18160. ' v:=arr[0];',
  18161. ' v:=arr.items[1];',
  18162. ' arr[2]:=s;',
  18163. ' arr.items[3]:=s;',
  18164. ' arr[4]:=i;',
  18165. ' arr[5]:=arr[6];',
  18166. ' arr.items[7]:=arr.items[8];',
  18167. ' with arr do items[9]:=items[10];',
  18168. ' with arr do begin',
  18169. ' v:=GetItems(14);',
  18170. ' setitems(15,16);',
  18171. ' end;',
  18172. '']);
  18173. ConvertProgram;
  18174. CheckSource('TestExternalClass_BracketAccessor_Call',
  18175. LinesToStr([ // statements
  18176. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18177. ' this.$init = function () {',
  18178. ' };',
  18179. ' this.$final = function () {',
  18180. ' };',
  18181. ' this.DoIt = function () {',
  18182. ' this[1] = this[2];',
  18183. ' this[3] = this[4];',
  18184. ' };',
  18185. '});',
  18186. 'this.Arr = null;',
  18187. 'this.s = "";',
  18188. 'this.i = 0;',
  18189. 'this.v = undefined;',
  18190. '']),
  18191. LinesToStr([ // $mod.$main
  18192. '$mod.v = $mod.Arr[0];',
  18193. '$mod.v = $mod.Arr[1];',
  18194. '$mod.Arr[2] = $mod.s;',
  18195. '$mod.Arr[3] = $mod.s;',
  18196. '$mod.Arr[4] = $mod.i;',
  18197. '$mod.Arr[5] = $mod.Arr[6];',
  18198. '$mod.Arr[7] = $mod.Arr[8];',
  18199. 'var $with = $mod.Arr;',
  18200. '$with[9] = $with[10];',
  18201. 'var $with1 = $mod.Arr;',
  18202. '$mod.v = $with1[14];',
  18203. '$with1[15] = 16;',
  18204. '']));
  18205. end;
  18206. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18207. begin
  18208. StartProgram(false);
  18209. Add('{$modeswitch externalclass}');
  18210. Add('type');
  18211. Add(' TJSArray = class external name ''Array2''');
  18212. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18213. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18214. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18215. Add(' end;');
  18216. Add('begin');
  18217. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18218. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18219. ConvertProgram;
  18220. end;
  18221. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18222. begin
  18223. StartProgram(false);
  18224. Add('{$modeswitch externalclass}');
  18225. Add('type');
  18226. Add(' TJSArray = class external name ''Array2''');
  18227. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18228. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18229. Add(' end;');
  18230. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18231. Add('begin end;');
  18232. Add('var');
  18233. Add(' Arr: tjsarray;');
  18234. Add(' v: jsvalue;');
  18235. Add('begin');
  18236. Add(' v:=arr[0];');
  18237. Add(' v:=arr.items[1];');
  18238. Add(' with arr do v:=items[2];');
  18239. Add(' doit(arr[3],arr[4]);');
  18240. ConvertProgram;
  18241. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18242. LinesToStr([ // statements
  18243. 'this.DoIt = function (vI, vJ) {',
  18244. '};',
  18245. 'this.Arr = null;',
  18246. 'this.v = undefined;',
  18247. '']),
  18248. LinesToStr([ // $mod.$main
  18249. '$mod.v = $mod.Arr[0];',
  18250. '$mod.v = $mod.Arr[1];',
  18251. 'var $with = $mod.Arr;',
  18252. '$mod.v = $with[2];',
  18253. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18254. '']));
  18255. end;
  18256. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18257. begin
  18258. StartProgram(false);
  18259. Add('{$modeswitch externalclass}');
  18260. Add('type');
  18261. Add(' TJSArray = class external name ''Array2''');
  18262. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18263. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18264. Add(' end;');
  18265. Add('var');
  18266. Add(' Arr: tjsarray;');
  18267. Add(' s: string;');
  18268. Add(' i: longint;');
  18269. Add(' v: jsvalue;');
  18270. Add('begin');
  18271. Add(' arr[2]:=s;');
  18272. Add(' arr.items[3]:=s;');
  18273. Add(' arr[4]:=i;');
  18274. Add(' with arr do items[5]:=i;');
  18275. ConvertProgram;
  18276. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18277. LinesToStr([ // statements
  18278. 'this.Arr = null;',
  18279. 'this.s = "";',
  18280. 'this.i = 0;',
  18281. 'this.v = undefined;',
  18282. '']),
  18283. LinesToStr([ // $mod.$main
  18284. '$mod.Arr[2] = $mod.s;',
  18285. '$mod.Arr[3] = $mod.s;',
  18286. '$mod.Arr[4] = $mod.i;',
  18287. 'var $with = $mod.Arr;',
  18288. '$with[5] = $mod.i;',
  18289. '']));
  18290. end;
  18291. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18292. begin
  18293. StartProgram(false);
  18294. Add('{$modeswitch externalclass}');
  18295. Add('type');
  18296. Add(' TJSArray = class external name ''Array2''');
  18297. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18298. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18299. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18300. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18301. Add(' end;');
  18302. Add('var');
  18303. Add(' Arr: tjsarray;');
  18304. Add(' s: string;');
  18305. Add(' i: longint;');
  18306. Add(' v: jsvalue;');
  18307. Add('begin');
  18308. Add(' arr[2]:=s;');
  18309. Add(' arr.items[3]:=s;');
  18310. Add(' arr.numbers[4]:=i;');
  18311. Add(' with arr do items[5]:=i;');
  18312. Add(' with arr do numbers[6]:=i;');
  18313. ConvertProgram;
  18314. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18315. LinesToStr([ // statements
  18316. 'this.Arr = null;',
  18317. 'this.s = "";',
  18318. 'this.i = 0;',
  18319. 'this.v = undefined;',
  18320. '']),
  18321. LinesToStr([ // $mod.$main
  18322. '$mod.Arr[2] = $mod.s;',
  18323. '$mod.Arr[3] = $mod.s;',
  18324. '$mod.Arr[4] = $mod.i;',
  18325. 'var $with = $mod.Arr;',
  18326. '$with[5] = $mod.i;',
  18327. 'var $with1 = $mod.Arr;',
  18328. '$with1[6] = $mod.i;',
  18329. '']));
  18330. end;
  18331. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18332. begin
  18333. StartProgram(false);
  18334. Add('{$modeswitch externalclass}');
  18335. Add('type');
  18336. Add(' TJSArray = class external name ''Array2''');
  18337. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18338. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18339. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18340. Add(' end;');
  18341. Add('var');
  18342. Add(' Arr: tjsarray;');
  18343. Add(' i: longint;');
  18344. Add(' IntArr: array of longint;');
  18345. Add(' v: jsvalue;');
  18346. Add('begin');
  18347. Add(' v:=arr.items[i];');
  18348. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18349. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18350. ConvertProgram;
  18351. CheckSource('TestExternalClass_BracketAccessor_Index',
  18352. LinesToStr([ // statements
  18353. 'this.Arr = null;',
  18354. 'this.i = 0;',
  18355. 'this.IntArr = [];',
  18356. 'this.v = undefined;',
  18357. '']),
  18358. LinesToStr([ // $mod.$main
  18359. '$mod.v = $mod.Arr[$mod.i];',
  18360. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18361. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18362. '']));
  18363. end;
  18364. procedure TTestModule.TestExternalClass_ForInJSObject;
  18365. begin
  18366. StartProgram(false);
  18367. Add([
  18368. '{$modeswitch externalclass}',
  18369. 'type',
  18370. ' TJSObject = class external name ''Object''',
  18371. ' end;',
  18372. 'var',
  18373. ' o: TJSObject;',
  18374. ' key: string;',
  18375. 'begin',
  18376. ' for key in o do',
  18377. ' if key=''abc'' then ;',
  18378. '']);
  18379. ConvertProgram;
  18380. CheckSource('TestExternalClass_ForInJSObject',
  18381. LinesToStr([ // statements
  18382. 'this.o = null;',
  18383. 'this.key = "";',
  18384. '']),
  18385. LinesToStr([ // $mod.$main
  18386. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18387. '']));
  18388. end;
  18389. procedure TTestModule.TestExternalClass_ForInJSArray;
  18390. begin
  18391. StartProgram(false);
  18392. Add([
  18393. '{$modeswitch externalclass}',
  18394. 'type',
  18395. ' TJSInt8Array = class external name ''Int8Array''',
  18396. ' private',
  18397. ' flength: NativeInt external name ''length'';',
  18398. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18399. ' public',
  18400. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18401. ' property Length: NativeInt read flength;',
  18402. ' end;',
  18403. 'var',
  18404. ' a: TJSInt8Array;',
  18405. ' value: shortint;',
  18406. 'begin',
  18407. ' for value in a do',
  18408. ' if value=3 then ;',
  18409. '']);
  18410. ConvertProgram;
  18411. CheckSource('TestExternalClass_ForInJSArray',
  18412. LinesToStr([ // statements
  18413. 'this.a = null;',
  18414. 'this.value = 0;',
  18415. '']),
  18416. LinesToStr([ // $mod.$main
  18417. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18418. ' $mod.value = $in[$l];',
  18419. ' if ($mod.value === 3) ;',
  18420. '};',
  18421. '']));
  18422. end;
  18423. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18424. begin
  18425. AddModuleWithIntfImplSrc('unit2.pas',
  18426. LinesToStr([
  18427. '{$modeswitch externalclass}',
  18428. 'type',
  18429. ' TJSBufferSource = class external name ''BufferSource''',
  18430. ' end;',
  18431. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18432. '']),
  18433. '');
  18434. AddModuleWithIntfImplSrc('unit3.pas',
  18435. LinesToStr([
  18436. '{$modeswitch externalclass}',
  18437. 'type',
  18438. ' TJSBufferSource = class external name ''BufferSource''',
  18439. ' end;',
  18440. '']),
  18441. '');
  18442. StartUnit(true);
  18443. Add([
  18444. 'interface',
  18445. 'uses unit2, unit3;',
  18446. 'procedure DoSome(s: TJSBufferSource);',
  18447. 'implementation',
  18448. 'procedure DoSome(s: TJSBufferSource);',
  18449. 'begin',
  18450. ' DoIt(s);',
  18451. 'end;',
  18452. '']);
  18453. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18454. nIncompatibleTypeArgNo);
  18455. ConvertUnit;
  18456. end;
  18457. procedure TTestModule.TestClassInterface_Corba;
  18458. begin
  18459. StartProgram(false);
  18460. Add([
  18461. '{$interfaces corba}',
  18462. 'type',
  18463. ' IUnknown = interface;',
  18464. ' IUnknown = interface',
  18465. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18466. ' end;',
  18467. ' IInterface = IUnknown;',
  18468. ' IBird = interface(IInterface)',
  18469. ' function GetSize: longint;',
  18470. ' procedure SetSize(i: longint);',
  18471. ' property Size: longint read GetSize write SetSize;',
  18472. ' procedure DoIt(i: longint);',
  18473. ' end;',
  18474. ' TObject = class',
  18475. ' end;',
  18476. ' TBird = class(TObject,IBird)',
  18477. ' function GetSize: longint; virtual; abstract;',
  18478. ' procedure SetSize(i: longint); virtual; abstract;',
  18479. ' procedure DoIt(i: longint); virtual; abstract;',
  18480. ' end;',
  18481. 'var',
  18482. ' BirdIntf: IBird;',
  18483. 'begin',
  18484. ' BirdIntf.Size:=BirdIntf.Size;',
  18485. '']);
  18486. ConvertProgram;
  18487. CheckSource('TestClassInterface_Corba',
  18488. LinesToStr([ // statements
  18489. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18490. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18491. 'rtl.createClass(this, "TObject", null, function () {',
  18492. ' this.$init = function () {',
  18493. ' };',
  18494. ' this.$final = function () {',
  18495. ' };',
  18496. '});',
  18497. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18498. ' rtl.addIntf(this, $mod.IBird);',
  18499. '});',
  18500. 'this.BirdIntf = null;',
  18501. '']),
  18502. LinesToStr([ // $mod.$main
  18503. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18504. '']));
  18505. end;
  18506. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18507. begin
  18508. StartProgram(false);
  18509. Add([
  18510. '{$interfaces corba}',
  18511. 'type',
  18512. ' IUnknown = interface',
  18513. ' procedure DoIt; external name ''foo'';',
  18514. ' end;',
  18515. 'begin']);
  18516. SetExpectedParserError(
  18517. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18518. nParserNoFieldsAllowed);
  18519. ConvertProgram;
  18520. end;
  18521. procedure TTestModule.TestClassInterface_Overloads;
  18522. begin
  18523. StartProgram(false);
  18524. Add([
  18525. '{$interfaces corba}',
  18526. 'type',
  18527. ' integer = longint;',
  18528. ' IUnknown = interface',
  18529. ' procedure DoIt(i: integer);',
  18530. ' procedure DoIt(s: string);',
  18531. ' end;',
  18532. ' IBird = interface(IUnknown)',
  18533. ' procedure DoIt(b: boolean); overload;',
  18534. ' end;',
  18535. ' TObject = class',
  18536. ' end;',
  18537. ' TBird = class(TObject,IBird)',
  18538. ' procedure DoIt(o: TObject);',
  18539. ' procedure DoIt(s: string);',
  18540. ' procedure DoIt(i: integer);',
  18541. ' procedure DoIt(b: boolean);',
  18542. ' end;',
  18543. 'procedure TBird.DoIt(o: TObject); begin end;',
  18544. 'procedure TBird.DoIt(s: string); begin end;',
  18545. 'procedure TBird.DoIt(i: integer); begin end;',
  18546. 'procedure TBird.DoIt(b: boolean); begin end;',
  18547. 'var',
  18548. ' BirdIntf: IBird;',
  18549. 'begin',
  18550. ' BirdIntf.DoIt(3);',
  18551. ' BirdIntf.DoIt(''abc'');',
  18552. ' BirdIntf.DoIt(true);',
  18553. '']);
  18554. ConvertProgram;
  18555. CheckSource('TestClassInterface_Overloads',
  18556. LinesToStr([ // statements
  18557. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18558. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18559. 'rtl.createClass(this, "TObject", null, function () {',
  18560. ' this.$init = function () {',
  18561. ' };',
  18562. ' this.$final = function () {',
  18563. ' };',
  18564. '});',
  18565. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18566. ' this.DoIt = function (o) {',
  18567. ' };',
  18568. ' this.DoIt$1 = function (s) {',
  18569. ' };',
  18570. ' this.DoIt$2 = function (i) {',
  18571. ' };',
  18572. ' this.DoIt$3 = function (b) {',
  18573. ' };',
  18574. ' rtl.addIntf(this, $mod.IBird, {',
  18575. ' DoIt$2: "DoIt$3",',
  18576. ' DoIt: "DoIt$2"',
  18577. ' });',
  18578. '});',
  18579. 'this.BirdIntf = null;',
  18580. '']),
  18581. LinesToStr([ // $mod.$main
  18582. '$mod.BirdIntf.DoIt(3);',
  18583. '$mod.BirdIntf.DoIt$1("abc");',
  18584. '$mod.BirdIntf.DoIt$2(true);',
  18585. '']));
  18586. end;
  18587. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18588. begin
  18589. StartProgram(false);
  18590. Add([
  18591. '{$interfaces corba}',
  18592. 'type',
  18593. ' IBird = interface',
  18594. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18595. ' end;',
  18596. ' IDog = interface',
  18597. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18598. ' end;',
  18599. ' TObject = class(IBird,IDog)',
  18600. ' end;',
  18601. 'begin']);
  18602. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18603. nDuplicateGUIDXInYZ);
  18604. ConvertProgram;
  18605. end;
  18606. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18607. begin
  18608. StartProgram(false);
  18609. Add([
  18610. '{$interfaces corba}',
  18611. 'type',
  18612. ' IAnimal = interface',
  18613. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18614. ' end;',
  18615. ' IBird = interface(IAnimal)',
  18616. ' end;',
  18617. ' IHawk = interface(IBird)',
  18618. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18619. ' end;',
  18620. 'begin']);
  18621. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18622. nDuplicateGUIDXInYZ);
  18623. ConvertProgram;
  18624. end;
  18625. procedure TTestModule.TestClassInterface_AncestorImpl;
  18626. begin
  18627. StartProgram(false);
  18628. Add([
  18629. '{$interfaces corba}',
  18630. 'type',
  18631. ' integer = longint;',
  18632. ' IUnknown = interface',
  18633. ' procedure DoIt(i: integer);',
  18634. ' end;',
  18635. ' IBird = interface',
  18636. ' procedure Fly(i: integer);',
  18637. ' end;',
  18638. ' TObject = class(IUnknown)',
  18639. ' procedure DoIt(i: integer);',
  18640. ' end;',
  18641. ' TBird = class(IBird)',
  18642. ' procedure Fly(i: integer);',
  18643. ' end;',
  18644. 'procedure TObject.DoIt(i: integer); begin end;',
  18645. 'procedure TBird.Fly(i: integer); begin end;',
  18646. 'begin',
  18647. '']);
  18648. ConvertProgram;
  18649. CheckSource('TestClassInterface_AncestorIntf',
  18650. LinesToStr([ // statements
  18651. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18652. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18653. 'rtl.createClass(this, "TObject", null, function () {',
  18654. ' this.$init = function () {',
  18655. ' };',
  18656. ' this.$final = function () {',
  18657. ' };',
  18658. ' this.DoIt = function (i) {',
  18659. ' };',
  18660. ' rtl.addIntf(this, $mod.IUnknown);',
  18661. '});',
  18662. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18663. ' this.Fly = function (i) {',
  18664. ' };',
  18665. ' rtl.addIntf(this, $mod.IBird);',
  18666. ' rtl.addIntf(this, $mod.IUnknown);',
  18667. '});',
  18668. '']),
  18669. LinesToStr([ // $mod.$main
  18670. '']));
  18671. end;
  18672. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18673. begin
  18674. StartProgram(false);
  18675. Add([
  18676. '{$interfaces corba}',
  18677. 'type',
  18678. ' integer = longint;',
  18679. ' IBird = interface',
  18680. ' procedure DoIt(i: integer);',
  18681. ' end;',
  18682. ' TObject = class',
  18683. ' procedure DoIt(i: integer);',
  18684. ' end;',
  18685. ' TBird = class(IBird)',
  18686. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18687. ' end;',
  18688. 'procedure TObject.DoIt(i: integer); begin end;',
  18689. 'procedure TBird.DoIt(i: integer); begin end;',
  18690. 'begin',
  18691. '']);
  18692. ConvertProgram;
  18693. CheckSource('TestClassInterface_ImplReintroduce',
  18694. LinesToStr([ // statements
  18695. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18696. 'rtl.createClass(this, "TObject", null, function () {',
  18697. ' this.$init = function () {',
  18698. ' };',
  18699. ' this.$final = function () {',
  18700. ' };',
  18701. ' this.DoIt = function (i) {',
  18702. ' };',
  18703. '});',
  18704. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18705. ' this.DoIt$1 = function (i) {',
  18706. ' };',
  18707. ' rtl.addIntf(this, $mod.IBird, {',
  18708. ' DoIt: "DoIt$1"',
  18709. ' });',
  18710. '});',
  18711. '']),
  18712. LinesToStr([ // $mod.$main
  18713. '']));
  18714. end;
  18715. procedure TTestModule.TestClassInterface_MethodResolution;
  18716. begin
  18717. StartProgram(false);
  18718. Add([
  18719. '{$interfaces corba}',
  18720. 'type',
  18721. ' IUnknown = interface',
  18722. ' procedure Walk(i: longint);',
  18723. ' end;',
  18724. ' IBird = interface(IUnknown)',
  18725. ' procedure Walk(b: boolean); overload;',
  18726. ' procedure Fly(s: string);',
  18727. ' end;',
  18728. ' TObject = class',
  18729. ' end;',
  18730. ' TBird = class(TObject,IBird)',
  18731. ' procedure IBird.Fly = Move;',
  18732. ' procedure IBird.Walk = Hop;',
  18733. ' procedure Hop(i: longint);',
  18734. ' procedure Move(s: string);',
  18735. ' procedure Hop(b: boolean);',
  18736. ' end;',
  18737. 'procedure TBird.Move(s: string); begin end;',
  18738. 'procedure TBird.Hop(i: longint); begin end;',
  18739. 'procedure TBird.Hop(b: boolean); begin end;',
  18740. 'var',
  18741. ' BirdIntf: IBird;',
  18742. 'begin',
  18743. ' BirdIntf.Walk(3);',
  18744. ' BirdIntf.Walk(true);',
  18745. ' BirdIntf.Fly(''abc'');',
  18746. '']);
  18747. ConvertProgram;
  18748. CheckSource('TestClassInterface_MethodResolution',
  18749. LinesToStr([ // statements
  18750. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18751. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18752. 'rtl.createClass(this, "TObject", null, function () {',
  18753. ' this.$init = function () {',
  18754. ' };',
  18755. ' this.$final = function () {',
  18756. ' };',
  18757. '});',
  18758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18759. ' this.Hop = function (i) {',
  18760. ' };',
  18761. ' this.Move = function (s) {',
  18762. ' };',
  18763. ' this.Hop$1 = function (b) {',
  18764. ' };',
  18765. ' rtl.addIntf(this, $mod.IBird, {',
  18766. ' Walk$1: "Hop$1",',
  18767. ' Fly: "Move",',
  18768. ' Walk: "Hop"',
  18769. ' });',
  18770. '});',
  18771. 'this.BirdIntf = null;',
  18772. '']),
  18773. LinesToStr([ // $mod.$main
  18774. '$mod.BirdIntf.Walk(3);',
  18775. '$mod.BirdIntf.Walk$1(true);',
  18776. '$mod.BirdIntf.Fly("abc");',
  18777. '']));
  18778. end;
  18779. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18780. begin
  18781. StartProgram(false);
  18782. Add([
  18783. '{$interfaces com}',
  18784. 'type',
  18785. ' IUnknown = interface',
  18786. ' function _AddRef: longint;',
  18787. ' procedure Walk;',
  18788. ' end;',
  18789. ' IBird = interface end;',
  18790. ' IDog = interface end;',
  18791. ' TObject = class(IBird,IDog)',
  18792. ' function _AddRef: longint; virtual; abstract;',
  18793. ' procedure Walk; virtual; abstract;',
  18794. ' end;',
  18795. ' TBird = class(IUnknown)',
  18796. ' end;',
  18797. 'begin',
  18798. '']);
  18799. ConvertProgram;
  18800. CheckSource('TestClassInterface_COM_AncestorLess',
  18801. LinesToStr([ // statements
  18802. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18803. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18804. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18805. 'rtl.createClass(this, "TObject", null, function () {',
  18806. ' this.$init = function () {',
  18807. ' };',
  18808. ' this.$final = function () {',
  18809. ' };',
  18810. ' rtl.addIntf(this, $mod.IBird);',
  18811. ' rtl.addIntf(this, $mod.IDog);',
  18812. '});',
  18813. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18814. ' rtl.addIntf(this, $mod.IUnknown);',
  18815. ' rtl.addIntf(this, $mod.IBird);',
  18816. ' rtl.addIntf(this, $mod.IDog);',
  18817. '});',
  18818. '']),
  18819. LinesToStr([ // $mod.$main
  18820. '']));
  18821. end;
  18822. procedure TTestModule.TestClassInterface_MethodOverride;
  18823. begin
  18824. StartProgram(false);
  18825. Add([
  18826. '{$interfaces corba}',
  18827. 'type',
  18828. ' IUnknown = interface',
  18829. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18830. ' procedure Go;',
  18831. ' end;',
  18832. ' TObject = class(IUnknown)',
  18833. ' procedure Go; virtual; abstract;',
  18834. ' end;',
  18835. ' TBird = class',
  18836. ' procedure Go; override;',
  18837. ' end;',
  18838. ' TCat = class(TObject)',
  18839. ' procedure Go; override;',
  18840. ' end;',
  18841. ' TDog = class(TObject, IUnknown)',
  18842. ' procedure Go; override;',
  18843. ' end;',
  18844. 'procedure TBird.Go; begin end;',
  18845. 'procedure TCat.Go; begin end;',
  18846. 'procedure TDog.Go; begin end;',
  18847. 'begin',
  18848. '']);
  18849. ConvertProgram;
  18850. CheckSource('TestClassInterface_MethodOverride',
  18851. LinesToStr([ // statements
  18852. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18853. 'rtl.createClass(this, "TObject", null, function () {',
  18854. ' this.$init = function () {',
  18855. ' };',
  18856. ' this.$final = function () {',
  18857. ' };',
  18858. ' rtl.addIntf(this, $mod.IUnknown);',
  18859. '});',
  18860. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18861. ' this.Go = function () {',
  18862. ' };',
  18863. ' rtl.addIntf(this, $mod.IUnknown);',
  18864. '});',
  18865. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18866. ' this.Go = function () {',
  18867. ' };',
  18868. ' rtl.addIntf(this, $mod.IUnknown);',
  18869. '});',
  18870. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18871. ' this.Go = function () {',
  18872. ' };',
  18873. ' rtl.addIntf(this, $mod.IUnknown);',
  18874. '});',
  18875. '']),
  18876. LinesToStr([ // $mod.$main
  18877. '']));
  18878. end;
  18879. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18880. begin
  18881. StartProgram(false);
  18882. Add([
  18883. '{$interfaces corba}',
  18884. 'type',
  18885. ' IUnknown = interface',
  18886. ' end;',
  18887. ' IBird = interface(IUnknown)',
  18888. ' procedure Fly(s: string);',
  18889. ' end;',
  18890. ' IEagle = interface(IBird)',
  18891. ' end;',
  18892. ' IDove = interface(IBird)',
  18893. ' end;',
  18894. ' ISwallow = interface(IBird)',
  18895. ' end;',
  18896. ' TObject = class',
  18897. ' end;',
  18898. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18899. ' procedure Fly(s: string); virtual; abstract;',
  18900. ' end;',
  18901. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18902. ' FBirdIntf: IBird;',
  18903. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18904. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18905. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18906. ' FDoveObj: TBird;',
  18907. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18908. ' function GetSwallowObj: TBird; virtual; abstract;',
  18909. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18910. ' end;',
  18911. 'begin',
  18912. '']);
  18913. ConvertProgram;
  18914. CheckSource('TestClassInterface_Corba_Delegation',
  18915. LinesToStr([ // statements
  18916. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18917. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18918. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18919. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18920. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18921. 'rtl.createClass(this, "TObject", null, function () {',
  18922. ' this.$init = function () {',
  18923. ' };',
  18924. ' this.$final = function () {',
  18925. ' };',
  18926. '});',
  18927. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18928. ' rtl.addIntf(this, $mod.IBird);',
  18929. ' rtl.addIntf(this, $mod.IEagle);',
  18930. ' rtl.addIntf(this, $mod.IDove);',
  18931. ' rtl.addIntf(this, $mod.ISwallow);',
  18932. '});',
  18933. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18934. ' this.$init = function () {',
  18935. ' $mod.TObject.$init.call(this);',
  18936. ' this.FBirdIntf = null;',
  18937. ' this.FDoveObj = null;',
  18938. ' };',
  18939. ' this.$final = function () {',
  18940. ' this.FBirdIntf = undefined;',
  18941. ' this.FDoveObj = undefined;',
  18942. ' $mod.TObject.$final.call(this);',
  18943. ' };',
  18944. ' this.$intfmaps = {',
  18945. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18946. ' return this.FBirdIntf;',
  18947. ' },',
  18948. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18949. ' return this.GetEagleIntf();',
  18950. ' },',
  18951. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18952. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18953. ' },',
  18954. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18955. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18956. ' }',
  18957. ' };',
  18958. '});',
  18959. '']),
  18960. LinesToStr([ // $mod.$main
  18961. '']));
  18962. end;
  18963. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18964. begin
  18965. StartProgram(false);
  18966. Add([
  18967. '{$interfaces corba}',
  18968. 'type',
  18969. ' IUnknown = interface',
  18970. ' end;',
  18971. ' IBird = interface(IUnknown)',
  18972. ' procedure Fly(s: string);',
  18973. ' end;',
  18974. ' IEagle = interface(IBird)',
  18975. ' end;',
  18976. ' IDove = interface(IBird)',
  18977. ' end;',
  18978. ' ISwallow = interface(IBird)',
  18979. ' end;',
  18980. ' TObject = class',
  18981. ' end;',
  18982. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18983. ' procedure Fly(s: string); virtual; abstract;',
  18984. ' end;',
  18985. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18986. ' private',
  18987. ' class var FBirdIntf: IBird;',
  18988. ' class var FDoveObj: TBird;',
  18989. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18990. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18991. ' protected',
  18992. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18993. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18994. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18995. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18996. ' end;',
  18997. 'begin',
  18998. '']);
  18999. ConvertProgram;
  19000. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19001. LinesToStr([ // statements
  19002. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19003. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19004. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19005. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19006. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19007. 'rtl.createClass(this, "TObject", null, function () {',
  19008. ' this.$init = function () {',
  19009. ' };',
  19010. ' this.$final = function () {',
  19011. ' };',
  19012. '});',
  19013. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19014. ' rtl.addIntf(this, $mod.IBird);',
  19015. ' rtl.addIntf(this, $mod.IEagle);',
  19016. ' rtl.addIntf(this, $mod.IDove);',
  19017. ' rtl.addIntf(this, $mod.ISwallow);',
  19018. '});',
  19019. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19020. ' this.FBirdIntf = null;',
  19021. ' this.FDoveObj = null;',
  19022. ' this.$intfmaps = {',
  19023. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19024. ' return this.FBirdIntf;',
  19025. ' },',
  19026. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19027. ' return this.GetEagleIntf();',
  19028. ' },',
  19029. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19030. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19031. ' },',
  19032. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19033. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19034. ' }',
  19035. ' };',
  19036. '});',
  19037. '']),
  19038. LinesToStr([ // $mod.$main
  19039. '']));
  19040. end;
  19041. procedure TTestModule.TestClassInterface_Corba_Operators;
  19042. begin
  19043. StartProgram(false);
  19044. Add([
  19045. '{$interfaces corba}',
  19046. 'type',
  19047. ' IUnknown = interface',
  19048. ' end;',
  19049. ' IBird = interface(IUnknown)',
  19050. ' function GetItems(Index: longint): longint;',
  19051. ' procedure SetItems(Index: longint; Value: longint);',
  19052. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19053. ' end;',
  19054. ' TObject = class',
  19055. ' end;',
  19056. ' TBird = class(TObject,IBird)',
  19057. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19058. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19059. ' end;',
  19060. 'var',
  19061. ' IntfVar: IBird = nil;',
  19062. ' IntfVar2: IBird;',
  19063. ' ObjVar: TBird;',
  19064. ' v: JSValue;',
  19065. 'begin',
  19066. ' IntfVar:=nil;',
  19067. ' IntfVar[3]:=IntfVar[4];',
  19068. ' if Assigned(IntfVar) then ;',
  19069. ' IntfVar:=IntfVar2;',
  19070. ' IntfVar:=ObjVar;',
  19071. ' if IntfVar=IntfVar2 then ;',
  19072. ' if IntfVar<>IntfVar2 then ;',
  19073. ' if IntfVar is IBird then ;',
  19074. ' if IntfVar is TBird then ;',
  19075. ' if ObjVar is IBird then ;',
  19076. ' IntfVar:=IntfVar2 as IBird;',
  19077. ' ObjVar:=IntfVar2 as TBird;',
  19078. ' IntfVar:=ObjVar as IBird;',
  19079. ' IntfVar:=IBird(IntfVar2);',
  19080. ' ObjVar:=TBird(IntfVar);',
  19081. ' IntfVar:=IBird(ObjVar);',
  19082. ' v:=IntfVar;',
  19083. ' IntfVar:=IBird(v);',
  19084. ' if v is IBird then ;',
  19085. ' v:=JSValue(IntfVar);',
  19086. ' v:=IBird;',
  19087. '']);
  19088. ConvertProgram;
  19089. CheckSource('TestClassInterface_Corba_Operators',
  19090. LinesToStr([ // statements
  19091. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19092. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19093. 'rtl.createClass(this, "TObject", null, function () {',
  19094. ' this.$init = function () {',
  19095. ' };',
  19096. ' this.$final = function () {',
  19097. ' };',
  19098. '});',
  19099. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19100. ' rtl.addIntf(this, $mod.IBird);',
  19101. '});',
  19102. 'this.IntfVar = null;',
  19103. 'this.IntfVar2 = null;',
  19104. 'this.ObjVar = null;',
  19105. 'this.v = undefined;',
  19106. '']),
  19107. LinesToStr([ // $mod.$main
  19108. '$mod.IntfVar = null;',
  19109. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19110. 'if ($mod.IntfVar != null) ;',
  19111. '$mod.IntfVar = $mod.IntfVar2;',
  19112. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19113. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19114. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19115. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19116. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19117. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19118. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19119. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19120. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19121. '$mod.IntfVar = $mod.IntfVar2;',
  19122. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19123. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19124. '$mod.v = $mod.IntfVar;',
  19125. '$mod.IntfVar = rtl.getObject($mod.v);',
  19126. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19127. '$mod.v = $mod.IntfVar;',
  19128. '$mod.v = $mod.IBird;',
  19129. '']));
  19130. end;
  19131. procedure TTestModule.TestClassInterface_Corba_Args;
  19132. begin
  19133. StartProgram(false);
  19134. Add([
  19135. '{$interfaces corba}',
  19136. 'type',
  19137. ' IUnknown = interface',
  19138. ' end;',
  19139. ' IBird = interface(IUnknown)',
  19140. ' end;',
  19141. ' TObject = class',
  19142. ' end;',
  19143. ' TBird = class(TObject,IBird)',
  19144. ' end;',
  19145. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19146. 'begin',
  19147. ' DoIt(i,i,i);',
  19148. 'end;',
  19149. 'procedure Change(var i: IBird; out j: IBird);',
  19150. 'begin',
  19151. ' DoIt(i,i,i);',
  19152. ' Change(i,i);',
  19153. 'end;',
  19154. 'var',
  19155. ' i: IBird;',
  19156. ' o: TBird;',
  19157. 'begin',
  19158. ' DoIt(i,i,i);',
  19159. ' Change(i,i);',
  19160. ' DoIt(o,o,o);',
  19161. '']);
  19162. ConvertProgram;
  19163. CheckSource('TestClassInterface_Corba_Args',
  19164. LinesToStr([ // statements
  19165. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19166. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19167. 'rtl.createClass(this, "TObject", null, function () {',
  19168. ' this.$init = function () {',
  19169. ' };',
  19170. ' this.$final = function () {',
  19171. ' };',
  19172. '});',
  19173. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19174. ' rtl.addIntf(this, $mod.IBird);',
  19175. '});',
  19176. 'this.DoIt = function (u, i, j) {',
  19177. ' $mod.DoIt({',
  19178. ' get: function () {',
  19179. ' return i;',
  19180. ' },',
  19181. ' set: function (v) {',
  19182. ' i = v;',
  19183. ' }',
  19184. ' }, i, i);',
  19185. '};',
  19186. 'this.Change = function (i, j) {',
  19187. ' $mod.DoIt(i, i.get(), i.get());',
  19188. ' $mod.Change(i, i);',
  19189. '};',
  19190. 'this.i = null;',
  19191. 'this.o = null;',
  19192. '']),
  19193. LinesToStr([ // $mod.$main
  19194. '$mod.DoIt({',
  19195. ' p: $mod,',
  19196. ' get: function () {',
  19197. ' return this.p.i;',
  19198. ' },',
  19199. ' set: function (v) {',
  19200. ' this.p.i = v;',
  19201. ' }',
  19202. '}, $mod.i, $mod.i);',
  19203. '$mod.Change({',
  19204. ' p: $mod,',
  19205. ' get: function () {',
  19206. ' return this.p.i;',
  19207. ' },',
  19208. ' set: function (v) {',
  19209. ' this.p.i = v;',
  19210. ' }',
  19211. '}, {',
  19212. ' p: $mod,',
  19213. ' get: function () {',
  19214. ' return this.p.i;',
  19215. ' },',
  19216. ' set: function (v) {',
  19217. ' this.p.i = v;',
  19218. ' }',
  19219. '});',
  19220. '$mod.DoIt({',
  19221. ' p: $mod,',
  19222. ' get: function () {',
  19223. ' return this.p.o;',
  19224. ' },',
  19225. ' set: function (v) {',
  19226. ' this.p.o = v;',
  19227. ' }',
  19228. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19229. '']));
  19230. end;
  19231. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19232. begin
  19233. StartProgram(false);
  19234. Add([
  19235. '{$interfaces corba}',
  19236. 'type',
  19237. ' IUnknown = interface end;',
  19238. ' TObject = class',
  19239. ' Id: longint;',
  19240. ' end;',
  19241. ' IEnumerator = interface(IUnknown)',
  19242. ' function GetCurrent: TObject;',
  19243. ' function MoveNext: Boolean;',
  19244. ' property Current: TObject read GetCurrent;',
  19245. ' end;',
  19246. ' IEnumerable = interface(IUnknown)',
  19247. ' function GetEnumerator: IEnumerator;',
  19248. ' end;',
  19249. 'var',
  19250. ' o: TObject;',
  19251. ' i: IEnumerable;',
  19252. 'begin',
  19253. ' for o in i do o.Id:=3;',
  19254. '']);
  19255. ConvertProgram;
  19256. CheckSource('TestClassInterface_Corba_ForIn',
  19257. LinesToStr([ // statements
  19258. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19259. 'rtl.createClass(this, "TObject", null, function () {',
  19260. ' this.$init = function () {',
  19261. ' this.Id = 0;',
  19262. ' };',
  19263. ' this.$final = function () {',
  19264. ' };',
  19265. '});',
  19266. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19267. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19268. 'this.o = null;',
  19269. 'this.i = null;',
  19270. '']),
  19271. LinesToStr([ // $mod.$main
  19272. 'var $in = $mod.i.GetEnumerator();',
  19273. 'while ($in.MoveNext()) {',
  19274. ' $mod.o = $in.GetCurrent();',
  19275. ' $mod.o.Id = 3;',
  19276. '};',
  19277. '']));
  19278. end;
  19279. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19280. begin
  19281. StartProgram(false);
  19282. Add([
  19283. '{$interfaces com}',
  19284. 'type',
  19285. ' IUnknown = interface',
  19286. ' function _AddRef: longint;',
  19287. ' function _Release: longint;',
  19288. ' end;',
  19289. ' TObject = class(IUnknown)',
  19290. ' function _AddRef: longint; virtual; abstract;',
  19291. ' function _Release: longint; virtual; abstract;',
  19292. ' end;',
  19293. 'var',
  19294. ' i: IUnknown;',
  19295. 'procedure DoGlobal(o: TObject);',
  19296. 'begin',
  19297. ' i:=nil;',
  19298. ' i:=o;',
  19299. ' i:=i;',
  19300. 'end;',
  19301. 'procedure DoLocal(o: TObject);',
  19302. 'const k: IUnknown = nil;',
  19303. 'var j: IUnknown;',
  19304. 'begin',
  19305. ' k:=o;',
  19306. ' k:=i;',
  19307. ' j:=o;',
  19308. ' j:=i;',
  19309. 'end;',
  19310. 'var o: TObject;',
  19311. 'begin',
  19312. ' i:=nil;',
  19313. ' i:=o;',
  19314. '']);
  19315. ConvertProgram;
  19316. CheckSource('TestClassInterface_COM_AssignVar',
  19317. LinesToStr([ // statements
  19318. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19319. 'rtl.createClass(this, "TObject", null, function () {',
  19320. ' this.$init = function () {',
  19321. ' };',
  19322. ' this.$final = function () {',
  19323. ' };',
  19324. ' rtl.addIntf(this, $mod.IUnknown);',
  19325. '});',
  19326. 'this.i = null;',
  19327. 'this.DoGlobal = function (o) {',
  19328. ' rtl.setIntfP($mod, "i", null);',
  19329. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19330. ' rtl.setIntfP($mod, "i", $mod.i);',
  19331. '};',
  19332. 'var k = null;',
  19333. 'this.DoLocal = function (o) {',
  19334. ' var j = null;',
  19335. ' try{',
  19336. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19337. ' k = rtl.setIntfL(k, $mod.i);',
  19338. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19339. ' j = rtl.setIntfL(j, $mod.i);',
  19340. ' }finally{',
  19341. ' rtl._Release(j);',
  19342. ' };',
  19343. '};',
  19344. 'this.o = null;',
  19345. '']),
  19346. LinesToStr([ // $mod.$main
  19347. 'rtl.setIntfP($mod, "i", null);',
  19348. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19349. '']));
  19350. end;
  19351. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19352. begin
  19353. StartProgram(false);
  19354. Add([
  19355. '{$interfaces com}',
  19356. 'type',
  19357. ' IUnknown = interface',
  19358. ' function _AddRef: longint;',
  19359. ' function _Release: longint;',
  19360. ' end;',
  19361. ' TObject = class(IUnknown)',
  19362. ' function _AddRef: longint; virtual; abstract;',
  19363. ' function _Release: longint; virtual; abstract;',
  19364. ' end;',
  19365. 'procedure DoDefault(i, j: IUnknown);',
  19366. 'begin',
  19367. ' i:=nil;',
  19368. ' i:=j;',
  19369. 'end;',
  19370. 'begin',
  19371. '']);
  19372. ConvertProgram;
  19373. CheckSource('TestClassInterface_COM_AssignArg',
  19374. LinesToStr([ // statements
  19375. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19376. 'rtl.createClass(this, "TObject", null, function () {',
  19377. ' this.$init = function () {',
  19378. ' };',
  19379. ' this.$final = function () {',
  19380. ' };',
  19381. ' rtl.addIntf(this, $mod.IUnknown);',
  19382. '});',
  19383. 'this.DoDefault = function (i, j) {',
  19384. ' rtl._AddRef(i);',
  19385. ' try {',
  19386. ' i = rtl.setIntfL(i, null);',
  19387. ' i = rtl.setIntfL(i, j);',
  19388. ' } finally {',
  19389. ' rtl._Release(i);',
  19390. ' };',
  19391. '};',
  19392. '']),
  19393. LinesToStr([ // $mod.$main
  19394. '']));
  19395. end;
  19396. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19397. begin
  19398. StartProgram(false);
  19399. Add([
  19400. '{$interfaces com}',
  19401. 'type',
  19402. ' IUnknown = interface',
  19403. ' function _AddRef: longint;',
  19404. ' function _Release: longint;',
  19405. ' end;',
  19406. ' TObject = class(IUnknown)',
  19407. ' function _AddRef: longint; virtual; abstract;',
  19408. ' function _Release: longint; virtual; abstract;',
  19409. ' end;',
  19410. 'function DoDefault(i: IUnknown): IUnknown;',
  19411. 'begin',
  19412. ' Result:=i;',
  19413. ' if Result<>nil then exit;',
  19414. 'end;',
  19415. 'begin',
  19416. '']);
  19417. ConvertProgram;
  19418. CheckSource('TestClassInterface_COM_FunctionResult',
  19419. LinesToStr([ // statements
  19420. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19421. 'rtl.createClass(this, "TObject", null, function () {',
  19422. ' this.$init = function () {',
  19423. ' };',
  19424. ' this.$final = function () {',
  19425. ' };',
  19426. ' rtl.addIntf(this, $mod.IUnknown);',
  19427. '});',
  19428. 'this.DoDefault = function (i) {',
  19429. ' var Result = null;',
  19430. ' var $ok = false;',
  19431. ' try {',
  19432. ' Result = rtl.setIntfL(Result, i);',
  19433. ' if(Result !== null){',
  19434. ' $ok = true;',
  19435. ' return Result;',
  19436. ' };',
  19437. ' $ok = true;',
  19438. ' } finally {',
  19439. ' if(!$ok) rtl._Release(Result);',
  19440. ' };',
  19441. ' return Result;',
  19442. '};',
  19443. '']),
  19444. LinesToStr([ // $mod.$main
  19445. '']));
  19446. end;
  19447. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19448. begin
  19449. StartProgram(false);
  19450. Add([
  19451. '{$interfaces com}',
  19452. 'type',
  19453. ' IUnknown = interface',
  19454. ' function _AddRef: longint;',
  19455. ' function _Release: longint;',
  19456. ' end;',
  19457. ' TObject = class(IUnknown)',
  19458. ' function _AddRef: longint; virtual; abstract;',
  19459. ' function _Release: longint; virtual; abstract;',
  19460. ' function GetIntf: IUnknown; virtual;',
  19461. ' end;',
  19462. ' TMouse = class',
  19463. ' function GetIntf: IUnknown; override;',
  19464. ' end;',
  19465. 'function TObject.GetIntf: IUnknown; begin end;',
  19466. 'function TMouse.GetIntf: IUnknown;',
  19467. 'var i: IUnknown;',
  19468. 'begin',
  19469. ' inherited;',
  19470. ' inherited GetIntf;',
  19471. ' inherited GetIntf();',
  19472. ' Result:=inherited GetIntf;',
  19473. ' Result:=inherited GetIntf();',
  19474. ' i:=inherited GetIntf;',
  19475. ' i:=inherited GetIntf();',
  19476. 'end;',
  19477. 'begin',
  19478. '']);
  19479. ConvertProgram;
  19480. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19481. LinesToStr([ // statements
  19482. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19483. 'rtl.createClass(this, "TObject", null, function () {',
  19484. ' this.$init = function () {',
  19485. ' };',
  19486. ' this.$final = function () {',
  19487. ' };',
  19488. ' this.GetIntf = function () {',
  19489. ' var Result = null;',
  19490. ' return Result;',
  19491. ' };',
  19492. ' rtl.addIntf(this, $mod.IUnknown);',
  19493. '});',
  19494. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19495. ' this.GetIntf = function () {',
  19496. ' var Result = null;',
  19497. ' var i = null;',
  19498. ' var $ir = rtl.createIntfRefs();',
  19499. ' var $ok = false;',
  19500. ' try {',
  19501. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19502. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19503. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19504. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19505. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19506. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19507. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19508. ' $ok = true;',
  19509. ' } finally {',
  19510. ' $ir.free();',
  19511. ' rtl._Release(i);',
  19512. ' if (!$ok) rtl._Release(Result);',
  19513. ' };',
  19514. ' return Result;',
  19515. ' };',
  19516. ' rtl.addIntf(this, $mod.IUnknown);',
  19517. '});',
  19518. '']),
  19519. LinesToStr([ // $mod.$main
  19520. '']));
  19521. end;
  19522. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19523. begin
  19524. StartProgram(false);
  19525. Add([
  19526. '{$interfaces com}',
  19527. 'type',
  19528. ' IUnknown = interface',
  19529. ' function _AddRef: longint;',
  19530. ' function _Release: longint;',
  19531. ' end;',
  19532. ' TObject = class(IUnknown)',
  19533. ' function _AddRef: longint; virtual; abstract;',
  19534. ' function _Release: longint; virtual; abstract;',
  19535. ' end;',
  19536. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19537. 'begin',
  19538. ' if i is IUnknown then ;',
  19539. ' if o is IUnknown then ;',
  19540. ' if i is TObject then ;',
  19541. ' i:=j as IUnknown;',
  19542. ' i:=o as IUnknown;',
  19543. ' o:=j as TObject;',
  19544. ' i:=IUnknown(j);',
  19545. ' i:=IUnknown(o);',
  19546. ' o:=TObject(i);',
  19547. 'end;',
  19548. 'begin',
  19549. '']);
  19550. ConvertProgram;
  19551. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19552. LinesToStr([ // statements
  19553. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19554. 'rtl.createClass(this, "TObject", null, function () {',
  19555. ' this.$init = function () {',
  19556. ' };',
  19557. ' this.$final = function () {',
  19558. ' };',
  19559. ' rtl.addIntf(this, $mod.IUnknown);',
  19560. '});',
  19561. 'this.DoDefault = function (i, j, o) {',
  19562. ' rtl._AddRef(i);',
  19563. ' try {',
  19564. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19565. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19566. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19567. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19568. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19569. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19570. ' i = rtl.setIntfL(i, j);',
  19571. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19572. ' o = rtl.intfToClass(i, $mod.TObject);',
  19573. ' } finally {',
  19574. ' rtl._Release(i);',
  19575. ' };',
  19576. '};',
  19577. '']),
  19578. LinesToStr([ // $mod.$main
  19579. '']));
  19580. end;
  19581. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19582. begin
  19583. StartProgram(false);
  19584. Add([
  19585. '{$interfaces com}',
  19586. 'type',
  19587. ' IUnknown = interface',
  19588. ' function _AddRef: longint;',
  19589. ' function _Release: longint;',
  19590. ' end;',
  19591. ' TObject = class(IUnknown)',
  19592. ' function _AddRef: longint; virtual; abstract;',
  19593. ' function _Release: longint; virtual; abstract;',
  19594. ' end;',
  19595. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19596. 'var o: TObject;',
  19597. 'begin',
  19598. ' DoIt(v,v,v,v);',
  19599. ' DoIt(o,o,k,k);',
  19600. 'end;',
  19601. 'procedure DoSome;',
  19602. 'var v: IUnknown;',
  19603. 'begin',
  19604. ' DoIt(v,v,v,v);',
  19605. 'end;',
  19606. 'var i: IUnknown;',
  19607. 'begin',
  19608. ' DoIt(i,i,i,i);',
  19609. '']);
  19610. ConvertProgram;
  19611. CheckSource('TestClassInterface_COM_PassAsArg',
  19612. LinesToStr([ // statements
  19613. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19614. 'rtl.createClass(this, "TObject", null, function () {',
  19615. ' this.$init = function () {',
  19616. ' };',
  19617. ' this.$final = function () {',
  19618. ' };',
  19619. ' rtl.addIntf(this, $mod.IUnknown);',
  19620. '});',
  19621. 'this.DoIt = function (v, j, k, l) {',
  19622. ' var o = null;',
  19623. ' var $ir = rtl.createIntfRefs();',
  19624. ' rtl._AddRef(v);',
  19625. ' try {',
  19626. ' $mod.DoIt(v, v, {',
  19627. ' get: function () {',
  19628. ' return v;',
  19629. ' },',
  19630. ' set: function (w) {',
  19631. ' v = rtl.setIntfL(v, w);',
  19632. ' }',
  19633. ' }, {',
  19634. ' get: function () {',
  19635. ' return v;',
  19636. ' },',
  19637. ' set: function (w) {',
  19638. ' v = rtl.setIntfL(v, w);',
  19639. ' }',
  19640. ' });',
  19641. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19642. ' } finally {',
  19643. ' $ir.free();',
  19644. ' rtl._Release(v);',
  19645. ' };',
  19646. '};',
  19647. 'this.DoSome = function () {',
  19648. ' var v = null;',
  19649. ' try {',
  19650. ' $mod.DoIt(v, v, {',
  19651. ' get: function () {',
  19652. ' return v;',
  19653. ' },',
  19654. ' set: function (w) {',
  19655. ' v = rtl.setIntfL(v, w);',
  19656. ' }',
  19657. ' }, {',
  19658. ' get: function () {',
  19659. ' return v;',
  19660. ' },',
  19661. ' set: function (w) {',
  19662. ' v = rtl.setIntfL(v, w);',
  19663. ' }',
  19664. ' });',
  19665. ' } finally {',
  19666. ' rtl._Release(v);',
  19667. ' };',
  19668. '};',
  19669. 'this.i = null;',
  19670. '']),
  19671. LinesToStr([ // $mod.$main
  19672. '$mod.DoIt($mod.i, $mod.i, {',
  19673. ' p: $mod,',
  19674. ' get: function () {',
  19675. ' return this.p.i;',
  19676. ' },',
  19677. ' set: function (v) {',
  19678. ' rtl.setIntfP(this.p, "i", v);',
  19679. ' }',
  19680. '}, {',
  19681. ' p: $mod,',
  19682. ' get: function () {',
  19683. ' return this.p.i;',
  19684. ' },',
  19685. ' set: function (v) {',
  19686. ' rtl.setIntfP(this.p, "i", v);',
  19687. ' }',
  19688. '});',
  19689. '']));
  19690. end;
  19691. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19692. begin
  19693. StartProgram(false);
  19694. Add([
  19695. '{$interfaces com}',
  19696. 'type',
  19697. ' IUnknown = interface',
  19698. ' function _AddRef: longint;',
  19699. ' function _Release: longint;',
  19700. ' end;',
  19701. ' TObject = class(IUnknown)',
  19702. ' function _AddRef: longint; virtual; abstract;',
  19703. ' function _Release: longint; virtual; abstract;',
  19704. ' end;',
  19705. 'procedure DoIt(out i);',
  19706. 'begin end;',
  19707. 'procedure DoSome;',
  19708. 'var v: IUnknown;',
  19709. 'begin',
  19710. ' DoIt(v);',
  19711. 'end;',
  19712. 'function GetIt: IUnknown;',
  19713. 'begin',
  19714. ' DoIt(Result);',
  19715. 'end;',
  19716. 'var i: IUnknown;',
  19717. 'begin',
  19718. ' DoIt(i);',
  19719. '']);
  19720. ConvertProgram;
  19721. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19722. LinesToStr([ // statements
  19723. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19724. 'rtl.createClass(this, "TObject", null, function () {',
  19725. ' this.$init = function () {',
  19726. ' };',
  19727. ' this.$final = function () {',
  19728. ' };',
  19729. ' rtl.addIntf(this, $mod.IUnknown);',
  19730. '});',
  19731. 'this.DoIt = function (i) {',
  19732. '};',
  19733. 'this.DoSome = function () {',
  19734. ' var v = null;',
  19735. ' try {',
  19736. ' $mod.DoIt({',
  19737. ' get: function () {',
  19738. ' return v;',
  19739. ' },',
  19740. ' set: function (w) {',
  19741. ' v = w;',
  19742. ' }',
  19743. ' });',
  19744. ' } finally {',
  19745. ' rtl._Release(v);',
  19746. ' };',
  19747. '};',
  19748. 'this.GetIt = function () {',
  19749. ' var Result = null;',
  19750. ' var $ok = false;',
  19751. ' try {',
  19752. ' $mod.DoIt({',
  19753. ' get: function () {',
  19754. ' return Result;',
  19755. ' },',
  19756. ' set: function (v) {',
  19757. ' Result = v;',
  19758. ' }',
  19759. ' });',
  19760. ' $ok = true;',
  19761. ' } finally {',
  19762. ' if (!$ok) rtl._Release(Result);',
  19763. ' };',
  19764. ' return Result;',
  19765. '};',
  19766. 'this.i = null;',
  19767. '']),
  19768. LinesToStr([ // $mod.$main
  19769. 'try {',
  19770. ' $mod.DoIt({',
  19771. ' p: $mod,',
  19772. ' get: function () {',
  19773. ' return this.p.i;',
  19774. ' },',
  19775. ' set: function (v) {',
  19776. ' this.p.i = v;',
  19777. ' }',
  19778. ' });',
  19779. '} finally {',
  19780. ' rtl._Release($mod.i);',
  19781. '};',
  19782. '']));
  19783. end;
  19784. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19785. begin
  19786. StartProgram(false);
  19787. Add([
  19788. '{$interfaces com}',
  19789. 'type',
  19790. ' IUnknown = interface',
  19791. ' function _AddRef: longint;',
  19792. ' function _Release: longint;',
  19793. ' end;',
  19794. ' TObject = class(IUnknown)',
  19795. ' function _AddRef: longint; virtual; abstract;',
  19796. ' function _Release: longint; virtual; abstract;',
  19797. ' end;',
  19798. 'function GetIt: IUnknown;',
  19799. 'begin',
  19800. 'end;',
  19801. 'procedure DoSome;',
  19802. 'var v: IUnknown;',
  19803. ' i: longint;',
  19804. 'begin',
  19805. ' v:=GetIt;',
  19806. ' v:=GetIt();',
  19807. ' GetIt()._AddRef;',
  19808. ' i:=GetIt()._AddRef;',
  19809. 'end;',
  19810. 'var v: IUnknown;',
  19811. ' i: longint;',
  19812. 'begin',
  19813. ' v:=GetIt;',
  19814. ' v:=GetIt();',
  19815. ' GetIt()._AddRef;',
  19816. ' i:=GetIt()._AddRef;',
  19817. '']);
  19818. ConvertProgram;
  19819. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19820. LinesToStr([ // statements
  19821. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19822. 'rtl.createClass(this, "TObject", null, function () {',
  19823. ' this.$init = function () {',
  19824. ' };',
  19825. ' this.$final = function () {',
  19826. ' };',
  19827. ' rtl.addIntf(this, $mod.IUnknown);',
  19828. '});',
  19829. 'this.GetIt = function () {',
  19830. ' var Result = null;',
  19831. ' return Result;',
  19832. '};',
  19833. 'this.DoSome = function () {',
  19834. ' var v = null;',
  19835. ' var i = 0;',
  19836. ' var $ir = rtl.createIntfRefs();',
  19837. ' try {',
  19838. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19839. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19840. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19841. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19842. ' } finally {',
  19843. ' $ir.free();',
  19844. ' rtl._Release(v);',
  19845. ' };',
  19846. '};',
  19847. 'this.v = null;',
  19848. 'this.i = 0;',
  19849. '']),
  19850. LinesToStr([ // $mod.$main
  19851. 'var $ir = rtl.createIntfRefs();',
  19852. 'try {',
  19853. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19854. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19855. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19856. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19857. '} finally {',
  19858. ' $ir.free();',
  19859. '};',
  19860. '']));
  19861. end;
  19862. procedure TTestModule.TestClassInterface_COM_Property;
  19863. begin
  19864. StartProgram(false);
  19865. Add([
  19866. '{$interfaces com}',
  19867. 'type',
  19868. ' IUnknown = interface',
  19869. ' function _AddRef: longint;',
  19870. ' function _Release: longint;',
  19871. ' end;',
  19872. ' TObject = class(IUnknown)',
  19873. ' FAnt: IUnknown;',
  19874. ' function _AddRef: longint; virtual; abstract;',
  19875. ' function _Release: longint; virtual; abstract;',
  19876. ' function GetBird: IUnknown; virtual; abstract;',
  19877. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19878. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19879. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19880. ' property Ant: IUnknown read FAnt write FAnt;',
  19881. ' property Bird: IUnknown read GetBird write SetBird;',
  19882. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19883. ' end;',
  19884. 'procedure DoIt;',
  19885. 'var',
  19886. ' o: TObject;',
  19887. ' v: IUnknown;',
  19888. 'begin',
  19889. ' v:=o.Ant;',
  19890. ' o.Ant:=v;',
  19891. ' o.Ant:=o.Ant;',
  19892. ' v:=o.Bird;',
  19893. ' o.Bird:=v;',
  19894. ' o.Bird:=o.Bird;',
  19895. ' v:=o.Items[1];',
  19896. ' o.Items[2]:=v;',
  19897. ' o.Items[3]:=o.Items[4];',
  19898. ' v:=o[5];',
  19899. ' o[6]:=v;',
  19900. ' o[7]:=o[8];',
  19901. 'end;',
  19902. 'begin',
  19903. '']);
  19904. ConvertProgram;
  19905. CheckSource('TestClassInterface_COM_Property',
  19906. LinesToStr([ // statements
  19907. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19908. 'rtl.createClass(this, "TObject", null, function () {',
  19909. ' this.$init = function () {',
  19910. ' this.FAnt = null;',
  19911. ' };',
  19912. ' this.$final = function () {',
  19913. ' this.FAnt = undefined;',
  19914. ' };',
  19915. ' rtl.addIntf(this, $mod.IUnknown);',
  19916. '});',
  19917. 'this.DoIt = function () {',
  19918. ' var o = null;',
  19919. ' var v = null;',
  19920. ' var $ir = rtl.createIntfRefs();',
  19921. ' try {',
  19922. ' v = rtl.setIntfL(v, o.FAnt);',
  19923. ' rtl.setIntfP(o, "FAnt", v);',
  19924. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19925. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19926. ' o.SetBird(v);',
  19927. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19928. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19929. ' o.SetItems(2, v);',
  19930. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19931. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19932. ' o.SetItems(6, v);',
  19933. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19934. ' } finally {',
  19935. ' $ir.free();',
  19936. ' rtl._Release(v);',
  19937. ' };',
  19938. '};',
  19939. '']),
  19940. LinesToStr([ // $mod.$main
  19941. '']));
  19942. end;
  19943. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19944. begin
  19945. StartProgram(false);
  19946. Add([
  19947. '{$interfaces com}',
  19948. 'type',
  19949. ' IUnknown = interface',
  19950. ' function _AddRef: longint;',
  19951. ' function _Release: longint;',
  19952. ' function GetBird: IUnknown;',
  19953. ' procedure SetBird(Value: IUnknown);',
  19954. ' function GetItems(Index: longint): IUnknown;',
  19955. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19956. ' property Bird: IUnknown read GetBird write SetBird;',
  19957. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19958. ' end;',
  19959. ' TObject = class(IUnknown)',
  19960. ' function _AddRef: longint; virtual; abstract;',
  19961. ' function _Release: longint; virtual; abstract;',
  19962. ' function GetBird: IUnknown; virtual; abstract;',
  19963. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19964. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19965. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19966. ' end;',
  19967. 'procedure DoIt;',
  19968. 'var',
  19969. ' o: TObject;',
  19970. ' v: IUnknown;',
  19971. 'begin',
  19972. ' v:=v.Items[1];',
  19973. ' v.Items[2]:=v;',
  19974. ' v.Items[3]:=v.Items[4];',
  19975. ' v:=v[5];',
  19976. ' v[6]:=v;',
  19977. ' v[7]:=v[8];',
  19978. ' v[9].Bird.Bird:=v;',
  19979. ' v:=v.Bird[10].Bird',
  19980. 'end;',
  19981. 'begin',
  19982. '']);
  19983. ConvertProgram;
  19984. CheckSource('TestClassInterface_COM_IntfProperty',
  19985. LinesToStr([ // statements
  19986. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19987. ' "_AddRef",',
  19988. ' "_Release",',
  19989. ' "GetBird",',
  19990. ' "SetBird",',
  19991. ' "GetItems",',
  19992. ' "SetItems"',
  19993. '], null);',
  19994. 'rtl.createClass(this, "TObject", null, function () {',
  19995. ' this.$init = function () {',
  19996. ' };',
  19997. ' this.$final = function () {',
  19998. ' };',
  19999. ' rtl.addIntf(this, $mod.IUnknown);',
  20000. '});',
  20001. 'this.DoIt = function () {',
  20002. ' var o = null;',
  20003. ' var v = null;',
  20004. ' var $ir = rtl.createIntfRefs();',
  20005. ' try {',
  20006. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20007. ' v.SetItems(2, v);',
  20008. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20009. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20010. ' v.SetItems(6, v);',
  20011. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20012. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20013. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20014. ' } finally {',
  20015. ' $ir.free();',
  20016. ' rtl._Release(v);',
  20017. ' };',
  20018. '};',
  20019. '']),
  20020. LinesToStr([ // $mod.$main
  20021. '']));
  20022. end;
  20023. procedure TTestModule.TestClassInterface_COM_Delegation;
  20024. begin
  20025. StartProgram(false);
  20026. Add([
  20027. '{$interfaces com}',
  20028. 'type',
  20029. ' IUnknown = interface',
  20030. ' function _AddRef: longint;',
  20031. ' function _Release: longint;',
  20032. ' end;',
  20033. ' IBird = interface(IUnknown)',
  20034. ' procedure Fly(s: string);',
  20035. ' end;',
  20036. ' IEagle = interface(IBird) end;',
  20037. ' IDove = interface(IBird) end;',
  20038. ' ISwallow = interface(IBird) end;',
  20039. ' TObject = class',
  20040. ' end;',
  20041. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20042. ' function _AddRef: longint; virtual; abstract;',
  20043. ' function _Release: longint; virtual; abstract;',
  20044. ' procedure Fly(s: string); virtual; abstract;',
  20045. ' end;',
  20046. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20047. ' function _AddRef: longint; virtual; abstract;',
  20048. ' function _Release: longint; virtual; abstract;',
  20049. ' FBirdIntf: IBird;',
  20050. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20051. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20052. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20053. ' FDoveObj: TBird;',
  20054. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20055. ' function GetSwallowObj: TBird; virtual; abstract;',
  20056. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20057. ' end;',
  20058. 'begin',
  20059. '']);
  20060. ConvertProgram;
  20061. CheckSource('TestClassInterface_COM_Delegation',
  20062. LinesToStr([ // statements
  20063. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20064. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20065. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20066. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20067. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20068. 'rtl.createClass(this, "TObject", null, function () {',
  20069. ' this.$init = function () {',
  20070. ' };',
  20071. ' this.$final = function () {',
  20072. ' };',
  20073. '});',
  20074. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20075. ' rtl.addIntf(this, $mod.IBird);',
  20076. ' rtl.addIntf(this, $mod.IEagle);',
  20077. ' rtl.addIntf(this, $mod.IDove);',
  20078. ' rtl.addIntf(this, $mod.ISwallow);',
  20079. '});',
  20080. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20081. ' this.$init = function () {',
  20082. ' $mod.TObject.$init.call(this);',
  20083. ' this.FBirdIntf = null;',
  20084. ' this.FDoveObj = null;',
  20085. ' };',
  20086. ' this.$final = function () {',
  20087. ' this.FBirdIntf = undefined;',
  20088. ' this.FDoveObj = undefined;',
  20089. ' $mod.TObject.$final.call(this);',
  20090. ' };',
  20091. ' this.$intfmaps = {',
  20092. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20093. ' return rtl._AddRef(this.FBirdIntf);',
  20094. ' },',
  20095. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20096. ' return this.GetEagleIntf();',
  20097. ' },',
  20098. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20099. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20100. ' },',
  20101. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20102. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20103. ' }',
  20104. ' };',
  20105. '});',
  20106. '']),
  20107. LinesToStr([ // $mod.$main
  20108. '']));
  20109. end;
  20110. procedure TTestModule.TestClassInterface_COM_With;
  20111. begin
  20112. StartProgram(false);
  20113. Add([
  20114. '{$interfaces com}',
  20115. 'type',
  20116. ' IUnknown = interface',
  20117. ' function _AddRef: longint;',
  20118. ' function _Release: longint;',
  20119. ' function GetAnt: IUnknown;',
  20120. ' property Ant: IUnknown read GetAnt;',
  20121. ' end;',
  20122. ' TObject = class(IUnknown)',
  20123. ' function _AddRef: longint; virtual; abstract;',
  20124. ' function _Release: longint; virtual; abstract;',
  20125. ' function GetAnt: IUnknown; virtual; abstract;',
  20126. ' property Ant: IUnknown read GetAnt;',
  20127. ' end;',
  20128. 'procedure DoIt;',
  20129. 'var',
  20130. ' i: IUnknown;',
  20131. 'begin',
  20132. ' with i do ',
  20133. ' GetAnt;',
  20134. ' with i.Ant, Ant do ',
  20135. ' GetAnt;',
  20136. 'end;',
  20137. 'begin',
  20138. '']);
  20139. ConvertProgram;
  20140. CheckSource('TestClassInterface_COM_With',
  20141. LinesToStr([ // statements
  20142. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20143. 'rtl.createClass(this, "TObject", null, function () {',
  20144. ' this.$init = function () {',
  20145. ' };',
  20146. ' this.$final = function () {',
  20147. ' };',
  20148. ' rtl.addIntf(this, $mod.IUnknown);',
  20149. '});',
  20150. 'this.DoIt = function () {',
  20151. ' var i = null;',
  20152. ' var $ir = rtl.createIntfRefs();',
  20153. ' try {',
  20154. ' $ir.ref(1, i.GetAnt());',
  20155. ' var $with = $ir.ref(2, i.GetAnt());',
  20156. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20157. ' $ir.ref(4, $with1.GetAnt());',
  20158. ' } finally {',
  20159. ' $ir.free();',
  20160. ' };',
  20161. '};',
  20162. '']),
  20163. LinesToStr([ // $mod.$main
  20164. '']));
  20165. end;
  20166. procedure TTestModule.TestClassInterface_COM_ForIn;
  20167. begin
  20168. StartProgram(false);
  20169. Add([
  20170. '{$interfaces com}',
  20171. 'type',
  20172. ' IUnknown = interface end;',
  20173. ' TObject = class',
  20174. ' Id: longint;',
  20175. ' end;',
  20176. ' IEnumerator = interface(IUnknown)',
  20177. ' function GetCurrent: TObject;',
  20178. ' function MoveNext: Boolean;',
  20179. ' property Current: TObject read GetCurrent;',
  20180. ' end;',
  20181. ' IEnumerable = interface(IUnknown)',
  20182. ' function GetEnumerator: IEnumerator;',
  20183. ' end;',
  20184. 'var',
  20185. ' o: TObject;',
  20186. ' i: IEnumerable;',
  20187. 'begin',
  20188. ' for o in i do o.Id:=3;',
  20189. '']);
  20190. ConvertProgram;
  20191. CheckSource('TestClassInterface_COM_ForIn',
  20192. LinesToStr([ // statements
  20193. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20194. 'rtl.createClass(this, "TObject", null, function () {',
  20195. ' this.$init = function () {',
  20196. ' this.Id = 0;',
  20197. ' };',
  20198. ' this.$final = function () {',
  20199. ' };',
  20200. '});',
  20201. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20202. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20203. 'this.o = null;',
  20204. 'this.i = null;',
  20205. '']),
  20206. LinesToStr([ // $mod.$main
  20207. 'var $in = $mod.i.GetEnumerator();',
  20208. 'try {',
  20209. ' while ($in.MoveNext()) {',
  20210. ' $mod.o = $in.GetCurrent();',
  20211. ' $mod.o.Id = 3;',
  20212. ' }',
  20213. '} finally {',
  20214. ' rtl._Release($in)',
  20215. '};',
  20216. '']));
  20217. end;
  20218. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20219. begin
  20220. StartProgram(false);
  20221. Add([
  20222. '{$interfaces com}',
  20223. 'type',
  20224. ' IUnknown = interface',
  20225. ' function _AddRef: longint;',
  20226. ' function _Release: longint;',
  20227. ' end;',
  20228. ' TObject = class',
  20229. ' end;',
  20230. ' TArrOfIntf = array of IUnknown;',
  20231. 'begin',
  20232. '']);
  20233. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20234. ConvertProgram;
  20235. end;
  20236. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20237. begin
  20238. StartProgram(false);
  20239. Add([
  20240. '{$interfaces com}',
  20241. 'type',
  20242. ' IUnknown = interface',
  20243. ' function _AddRef: longint;',
  20244. ' function _Release: longint;',
  20245. ' end;',
  20246. ' TRec = record',
  20247. ' i: IUnknown;',
  20248. ' end;',
  20249. 'begin',
  20250. '']);
  20251. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20252. ConvertProgram;
  20253. end;
  20254. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20255. begin
  20256. StartUnit(false);
  20257. Add([
  20258. '{$interfaces com}',
  20259. 'interface',
  20260. 'implementation',
  20261. 'type',
  20262. ' IUnknown = interface',
  20263. ' function _AddRef: longint;',
  20264. ' end;',
  20265. ' TObject = class(IUnknown)',
  20266. ' function _AddRef: longint;',
  20267. ' end;',
  20268. 'function TObject._AddRef: longint; begin end;',
  20269. 'var i: IUnknown;',
  20270. ' o: TObject;',
  20271. 'initialization',
  20272. ' i:=nil;',
  20273. ' i:=i;',
  20274. ' i:=o;',
  20275. ' if (o as IUnknown)=nil then ;',
  20276. '']);
  20277. ConvertUnit;
  20278. CheckSource('TestClassInterface_COM_UnitInitialization',
  20279. LinesToStr([ // statements
  20280. 'var $impl = $mod.$impl;',
  20281. '']),
  20282. LinesToStr([ // this.$init
  20283. 'var $ir = rtl.createIntfRefs();',
  20284. 'try {',
  20285. ' rtl.setIntfP($impl, "i", null);',
  20286. ' rtl.setIntfP($impl, "i", $impl.i);',
  20287. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20288. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20289. '} finally {',
  20290. ' $ir.free();',
  20291. '};',
  20292. '']),
  20293. LinesToStr([ // implementation
  20294. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20295. 'rtl.createClass($impl, "TObject", null, function () {',
  20296. ' this.$init = function () {',
  20297. ' };',
  20298. ' this.$final = function () {',
  20299. ' };',
  20300. ' this._AddRef = function () {',
  20301. ' var Result = 0;',
  20302. ' return Result;',
  20303. ' };',
  20304. ' rtl.addIntf(this, $impl.IUnknown);',
  20305. '});',
  20306. '$impl.i = null;',
  20307. '$impl.o = null;',
  20308. ''])
  20309. );
  20310. end;
  20311. procedure TTestModule.TestClassInterface_GUID;
  20312. begin
  20313. StartProgram(false);
  20314. Add([
  20315. '{$interfaces corba}',
  20316. 'type',
  20317. ' IUnknown = interface',
  20318. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20319. ' end;',
  20320. ' TObject = class end;',
  20321. ' TGUID = record D1, D2, D3, D4: word; end;',
  20322. ' TAliasGUID = TGUID;',
  20323. ' TGUIDString = type string;',
  20324. ' TAliasGUIDString = TGUIDString;',
  20325. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20326. 'begin end;',
  20327. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20328. 'begin end;',
  20329. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20330. 'begin end;',
  20331. 'var',
  20332. ' i: IUnknown;',
  20333. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20334. ' s: TAliasGUIDString;',
  20335. 'begin',
  20336. ' DoConstGUIDIt(IUnknown);',
  20337. ' DoDefGUID(IUnknown);',
  20338. ' DoStr(IUnknown);',
  20339. ' DoConstGUIDIt(i);',
  20340. ' DoDefGUID(i);',
  20341. ' DoStr(i);',
  20342. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20343. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20344. ' DoStr(g);',
  20345. ' g:=i;',
  20346. ' g:=IUnknown;',
  20347. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20348. ' s:=i;',
  20349. ' s:=IUnknown;',
  20350. ' s:=g;',
  20351. ' if g=i then ;',
  20352. ' if i=g then ;',
  20353. ' if g=IUnknown then ;',
  20354. ' if IUnknown=g then ;',
  20355. ' if s=i then ;',
  20356. ' if i=s then ;',
  20357. ' if s=IUnknown then ;',
  20358. ' if IUnknown=s then ;',
  20359. ' if s=g then ;',
  20360. ' if g=s then ;',
  20361. '']);
  20362. ConvertProgram;
  20363. CheckSource('TestClassInterface_GUID',
  20364. LinesToStr([ // statements
  20365. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20366. 'rtl.createClass(this, "TObject", null, function () {',
  20367. ' this.$init = function () {',
  20368. ' };',
  20369. ' this.$final = function () {',
  20370. ' };',
  20371. '});',
  20372. 'rtl.recNewT(this, "TGUID", function () {',
  20373. ' this.D1 = 0;',
  20374. ' this.D2 = 0;',
  20375. ' this.D3 = 0;',
  20376. ' this.D4 = 0;',
  20377. ' this.$eq = function (b) {',
  20378. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20379. ' };',
  20380. ' this.$assign = function (s) {',
  20381. ' this.D1 = s.D1;',
  20382. ' this.D2 = s.D2;',
  20383. ' this.D3 = s.D3;',
  20384. ' this.D4 = s.D4;',
  20385. ' return this;',
  20386. ' };',
  20387. '});',
  20388. 'this.DoConstGUIDIt = function (g) {',
  20389. '};',
  20390. 'this.DoDefGUID = function (g) {',
  20391. '};',
  20392. 'this.DoStr = function (s) {',
  20393. '};',
  20394. 'this.i = null;',
  20395. 'this.g = this.TGUID.$clone({',
  20396. ' D1: 0xD91C9AF4,',
  20397. ' D2: 0x3C93,',
  20398. ' D3: 0x420F,',
  20399. ' D4: [',
  20400. ' 0xA3,',
  20401. ' 0x03,',
  20402. ' 0xBF,',
  20403. ' 0x5B,',
  20404. ' 0xA8,',
  20405. ' 0x2B,',
  20406. ' 0xFD,',
  20407. ' 0x23',
  20408. ' ]',
  20409. '});',
  20410. 'this.s = "";',
  20411. '']),
  20412. LinesToStr([ // $mod.$main
  20413. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20414. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20415. '$mod.DoStr($mod.IUnknown.$guid);',
  20416. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20417. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20418. '$mod.DoStr($mod.i.$guid);',
  20419. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20420. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20421. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20422. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20423. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20424. '$mod.g.$assign({',
  20425. ' D1: 0xD91C9AF4,',
  20426. ' D2: 0x3C93,',
  20427. ' D3: 0x420F,',
  20428. ' D4: [',
  20429. ' 0xA3,',
  20430. ' 0x03,',
  20431. ' 0xBF,',
  20432. ' 0x5B,',
  20433. ' 0xA8,',
  20434. ' 0x2B,',
  20435. ' 0xFD,',
  20436. ' 0x23',
  20437. ' ]',
  20438. '});',
  20439. '$mod.s = $mod.i.$guid;',
  20440. '$mod.s = $mod.IUnknown.$guid;',
  20441. '$mod.s = rtl.guidrToStr($mod.g);',
  20442. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20443. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20444. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20445. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20446. 'if ($mod.s === $mod.i.$guid) ;',
  20447. 'if ($mod.i.$guid === $mod.s) ;',
  20448. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20449. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20450. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20451. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20452. '']));
  20453. end;
  20454. procedure TTestModule.TestClassInterface_GUIDProperty;
  20455. begin
  20456. StartProgram(false);
  20457. Add([
  20458. '{$interfaces corba}',
  20459. 'type',
  20460. ' IUnknown = interface',
  20461. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20462. ' end;',
  20463. ' TGUID = record D1, D2, D3, D4: word; end;',
  20464. ' TAliasGUID = TGUID;',
  20465. ' TGUIDString = type string;',
  20466. ' TAliasGUIDString = TGUIDString;',
  20467. ' TObject = class',
  20468. ' function GetG: TAliasGUID; virtual; abstract;',
  20469. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20470. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20471. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20472. ' property g: TAliasGUID read GetG write SetG;',
  20473. ' property s: TAliasGUIDString read GetS write SetS;',
  20474. ' end;',
  20475. 'var o: TObject;',
  20476. 'begin',
  20477. ' o.g:=IUnknown;',
  20478. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20479. ' o.s:=IUnknown;',
  20480. ' o.s:=o.g;',
  20481. '']);
  20482. ConvertProgram;
  20483. CheckSource('TestClassInterface_GUIDProperty',
  20484. LinesToStr([ // statements
  20485. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20486. 'rtl.recNewT(this, "TGUID", function () {',
  20487. ' this.D1 = 0;',
  20488. ' this.D2 = 0;',
  20489. ' this.D3 = 0;',
  20490. ' this.D4 = 0;',
  20491. ' this.$eq = function (b) {',
  20492. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20493. ' };',
  20494. ' this.$assign = function (s) {',
  20495. ' this.D1 = s.D1;',
  20496. ' this.D2 = s.D2;',
  20497. ' this.D3 = s.D3;',
  20498. ' this.D4 = s.D4;',
  20499. ' return this;',
  20500. ' };',
  20501. '});',
  20502. 'rtl.createClass(this, "TObject", null, function () {',
  20503. ' this.$init = function () {',
  20504. ' };',
  20505. ' this.$final = function () {',
  20506. ' };',
  20507. '});',
  20508. 'this.o = null;',
  20509. '']),
  20510. LinesToStr([ // $mod.$main
  20511. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20512. '$mod.o.SetG({',
  20513. ' D1: 0xD91C9AF4,',
  20514. ' D2: 0x3C93,',
  20515. ' D3: 0x420F,',
  20516. ' D4: [',
  20517. ' 0xA3,',
  20518. ' 0x03,',
  20519. ' 0xBF,',
  20520. ' 0x5B,',
  20521. ' 0xA8,',
  20522. ' 0x2B,',
  20523. ' 0xFD,',
  20524. ' 0x23',
  20525. ' ]',
  20526. '});',
  20527. '$mod.o.SetS($mod.IUnknown.$guid);',
  20528. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20529. '']));
  20530. end;
  20531. procedure TTestModule.TestClassHelper_ClassVar;
  20532. begin
  20533. StartProgram(false);
  20534. Add([
  20535. 'type',
  20536. ' TObject = class',
  20537. ' end;',
  20538. ' THelper = class helper for TObject',
  20539. ' const',
  20540. ' One = 1;',
  20541. ' Two: word = 2;',
  20542. ' class var',
  20543. ' Glob: word;',
  20544. ' function Foo(w: word): word;',
  20545. ' class function Bar(w: word): word;',
  20546. ' end;',
  20547. 'function THelper.foo(w: word): word;',
  20548. 'begin',
  20549. ' Result:=w;',
  20550. ' Two:=One+w;',
  20551. ' Glob:=Glob;',
  20552. ' Result:=Self.Glob;',
  20553. ' Self.Glob:=Self.Glob;',
  20554. ' with Self do Glob:=Glob;',
  20555. 'end;',
  20556. 'class function THelper.bar(w: word): word;',
  20557. 'begin',
  20558. ' Result:=w;',
  20559. ' Two:=One;',
  20560. ' Glob:=Glob;',
  20561. ' Self.Glob:=Self.Glob;',
  20562. ' with Self do Glob:=Glob;',
  20563. 'end;',
  20564. 'var o: TObject;',
  20565. 'begin',
  20566. ' tobject.two:=tobject.one;',
  20567. ' tobject.Glob:=tobject.Glob;',
  20568. ' with tobject do begin',
  20569. ' two:=one;',
  20570. ' Glob:=Glob;',
  20571. ' end;',
  20572. ' o.two:=o.one;',
  20573. ' o.Glob:=o.Glob;',
  20574. ' with o do begin',
  20575. ' two:=one;',
  20576. ' Glob:=Glob;',
  20577. ' end;',
  20578. '']);
  20579. ConvertProgram;
  20580. CheckSource('TestClassHelper_ClassVar',
  20581. LinesToStr([ // statements
  20582. 'rtl.createClass(this, "TObject", null, function () {',
  20583. ' this.$init = function () {',
  20584. ' };',
  20585. ' this.$final = function () {',
  20586. ' };',
  20587. '});',
  20588. 'rtl.createHelper(this, "THelper", null, function () {',
  20589. ' this.One = 1;',
  20590. ' this.Two = 2;',
  20591. ' this.Glob = 0;',
  20592. ' this.Foo = function (w) {',
  20593. ' var Result = 0;',
  20594. ' Result = w;',
  20595. ' $mod.THelper.Two = 1 + w;',
  20596. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20597. ' Result = $mod.THelper.Glob;',
  20598. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20599. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20600. ' return Result;',
  20601. ' };',
  20602. ' this.Bar = function (w) {',
  20603. ' var Result = 0;',
  20604. ' Result = w;',
  20605. ' $mod.THelper.Two = 1;',
  20606. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20607. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20608. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20609. ' return Result;',
  20610. ' };',
  20611. '});',
  20612. 'this.o = null;',
  20613. '']),
  20614. LinesToStr([ // $mod.$main
  20615. '$mod.THelper.Two = 1;',
  20616. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20617. 'var $with = $mod.TObject;',
  20618. '$mod.THelper.Two = 1;',
  20619. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20620. '$mod.THelper.Two = 1;',
  20621. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20622. 'var $with1 = $mod.o;',
  20623. '$mod.THelper.Two = 1;',
  20624. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20625. '']));
  20626. end;
  20627. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20628. begin
  20629. StartProgram(false);
  20630. Add([
  20631. 'type',
  20632. ' TObject = class',
  20633. ' FSize: word;',
  20634. ' property Size: word read FSize write FSize;',
  20635. ' end;',
  20636. ' THelper = class helper for TObject',
  20637. ' function Foo(w: word = 1): word;',
  20638. ' end;',
  20639. 'function THelper.foo(w: word): word;',
  20640. 'begin',
  20641. ' Result:=Size;',
  20642. ' Size:=Size+2;',
  20643. ' Self.Size:=Self.Size+3;',
  20644. ' FSize:=FSize+4;',
  20645. ' Self.FSize:=Self.FSize+5;',
  20646. ' with Self do begin',
  20647. ' Size:=Size+6;',
  20648. ' FSize:=FSize+7;',
  20649. ' FSize:=FSize+8;',
  20650. ' end;',
  20651. 'end;',
  20652. 'begin',
  20653. '']);
  20654. ConvertProgram;
  20655. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20656. LinesToStr([ // statements
  20657. 'rtl.createClass(this, "TObject", null, function () {',
  20658. ' this.$init = function () {',
  20659. ' this.FSize = 0;',
  20660. ' };',
  20661. ' this.$final = function () {',
  20662. ' };',
  20663. '});',
  20664. 'rtl.createHelper(this, "THelper", null, function () {',
  20665. ' this.Foo = function (w) {',
  20666. ' var Result = 0;',
  20667. ' Result = this.FSize;',
  20668. ' this.FSize = this.FSize + 2;',
  20669. ' this.FSize = this.FSize + 3;',
  20670. ' this.FSize = this.FSize + 4;',
  20671. ' this.FSize = this.FSize + 5;',
  20672. ' this.FSize = this.FSize + 6;',
  20673. ' this.FSize = this.FSize + 7;',
  20674. ' this.FSize = this.FSize + 8;',
  20675. ' return Result;',
  20676. ' };',
  20677. '});',
  20678. '']),
  20679. LinesToStr([ // $mod.$main
  20680. '']));
  20681. end;
  20682. procedure TTestModule.TestClassHelper_Method_Call;
  20683. begin
  20684. StartProgram(false);
  20685. Add([
  20686. 'type',
  20687. ' TObject = class',
  20688. ' procedure Run(w: word = 10);',
  20689. ' end;',
  20690. ' THelper = class helper for TObject',
  20691. ' function Foo(w: word = 1): word;',
  20692. ' end;',
  20693. 'procedure TObject.Run(w: word);',
  20694. 'var o: TObject;',
  20695. 'begin',
  20696. ' Foo;',
  20697. ' Foo();',
  20698. ' Foo(2);',
  20699. ' Self.Foo;',
  20700. ' Self.Foo();',
  20701. ' Self.Foo(3);',
  20702. ' with Self do begin',
  20703. ' Foo;',
  20704. ' Foo();',
  20705. ' Foo(4);',
  20706. ' end;',
  20707. ' with o do Foo(5);',
  20708. 'end;',
  20709. 'function THelper.foo(w: word): word;',
  20710. 'begin',
  20711. ' Run;',
  20712. ' Run();',
  20713. ' Run(11);',
  20714. ' Foo;',
  20715. ' Foo();',
  20716. ' Foo(12);',
  20717. ' Self.Foo;',
  20718. ' Self.Foo();',
  20719. ' Self.Foo(13);',
  20720. ' with Self do begin',
  20721. ' Foo;',
  20722. ' Foo();',
  20723. ' Foo(14);',
  20724. ' end;',
  20725. 'end;',
  20726. 'var Obj: TObject;',
  20727. 'begin',
  20728. ' obj.Foo;',
  20729. ' obj.Foo();',
  20730. ' obj.Foo(21);',
  20731. ' with obj do begin',
  20732. ' Foo;',
  20733. ' Foo();',
  20734. ' Foo(22);',
  20735. ' end;',
  20736. '']);
  20737. ConvertProgram;
  20738. CheckSource('TestClassHelper_Method_Call',
  20739. LinesToStr([ // statements
  20740. 'rtl.createClass(this, "TObject", null, function () {',
  20741. ' this.$init = function () {',
  20742. ' };',
  20743. ' this.$final = function () {',
  20744. ' };',
  20745. ' this.Run = function (w) {',
  20746. ' var o = null;',
  20747. ' $mod.THelper.Foo.call(this, 1);',
  20748. ' $mod.THelper.Foo.call(this, 1);',
  20749. ' $mod.THelper.Foo.call(this, 2);',
  20750. ' $mod.THelper.Foo.call(this, 1);',
  20751. ' $mod.THelper.Foo.call(this, 1);',
  20752. ' $mod.THelper.Foo.call(this, 3);',
  20753. ' $mod.THelper.Foo.call(this, 1);',
  20754. ' $mod.THelper.Foo.call(this, 1);',
  20755. ' $mod.THelper.Foo.call(this, 4);',
  20756. ' $mod.THelper.Foo.call(o, 5);',
  20757. ' };',
  20758. '});',
  20759. 'rtl.createHelper(this, "THelper", null, function () {',
  20760. ' this.Foo = function (w) {',
  20761. ' var Result = 0;',
  20762. ' this.Run(10);',
  20763. ' this.Run(10);',
  20764. ' this.Run(11);',
  20765. ' $mod.THelper.Foo.call(this, 1);',
  20766. ' $mod.THelper.Foo.call(this, 1);',
  20767. ' $mod.THelper.Foo.call(this, 12);',
  20768. ' $mod.THelper.Foo.call(this, 1);',
  20769. ' $mod.THelper.Foo.call(this, 1);',
  20770. ' $mod.THelper.Foo.call(this, 13);',
  20771. ' $mod.THelper.Foo.call(this, 1);',
  20772. ' $mod.THelper.Foo.call(this, 1);',
  20773. ' $mod.THelper.Foo.call(this, 14);',
  20774. ' return Result;',
  20775. ' };',
  20776. '});',
  20777. 'this.Obj = null;',
  20778. '']),
  20779. LinesToStr([ // $mod.$main
  20780. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20781. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20782. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20783. 'var $with = $mod.Obj;',
  20784. '$mod.THelper.Foo.call($with, 1);',
  20785. '$mod.THelper.Foo.call($with, 1);',
  20786. '$mod.THelper.Foo.call($with, 22);',
  20787. '']));
  20788. end;
  20789. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20790. begin
  20791. StartProgram(false);
  20792. Add([
  20793. 'type',
  20794. ' TObject = class',
  20795. ' procedure Run(w: word = 10);',
  20796. ' end;',
  20797. ' THelper = class helper for TObject',
  20798. ' function Foo(w: word = 1): word;',
  20799. ' end;',
  20800. 'procedure TObject.Run(w: word);',
  20801. ' procedure Sub(Self: TObject);',
  20802. ' begin',
  20803. ' Foo;',
  20804. ' Foo();',
  20805. ' Self.Foo;',
  20806. ' Self.Foo();',
  20807. ' with Self do begin',
  20808. ' Foo;',
  20809. ' Foo();',
  20810. ' end;',
  20811. ' end;',
  20812. 'begin',
  20813. 'end;',
  20814. 'function THelper.foo(w: word): word;',
  20815. ' procedure Sub(Self: TObject);',
  20816. ' begin',
  20817. ' Run;',
  20818. ' Run();',
  20819. ' Foo;',
  20820. ' Foo();',
  20821. ' Self.Foo;',
  20822. ' Self.Foo();',
  20823. ' with Self do begin',
  20824. ' Foo;',
  20825. ' Foo();',
  20826. ' end;',
  20827. ' end;',
  20828. 'begin',
  20829. 'end;',
  20830. 'begin',
  20831. '']);
  20832. ConvertProgram;
  20833. CheckSource('TestClassHelper_Method_Nested_Call',
  20834. LinesToStr([ // statements
  20835. 'rtl.createClass(this, "TObject", null, function () {',
  20836. ' this.$init = function () {',
  20837. ' };',
  20838. ' this.$final = function () {',
  20839. ' };',
  20840. ' this.Run = function (w) {',
  20841. ' var $Self = this;',
  20842. ' function Sub(Self) {',
  20843. ' $mod.THelper.Foo.call($Self, 1);',
  20844. ' $mod.THelper.Foo.call($Self, 1);',
  20845. ' $mod.THelper.Foo.call(Self, 1);',
  20846. ' $mod.THelper.Foo.call(Self, 1);',
  20847. ' $mod.THelper.Foo.call(Self, 1);',
  20848. ' $mod.THelper.Foo.call(Self, 1);',
  20849. ' };',
  20850. ' };',
  20851. '});',
  20852. 'rtl.createHelper(this, "THelper", null, function () {',
  20853. ' this.Foo = function (w) {',
  20854. ' var $Self = this;',
  20855. ' var Result = 0;',
  20856. ' function Sub(Self) {',
  20857. ' $Self.Run(10);',
  20858. ' $Self.Run(10);',
  20859. ' $mod.THelper.Foo.call($Self, 1);',
  20860. ' $mod.THelper.Foo.call($Self, 1);',
  20861. ' $mod.THelper.Foo.call(Self, 1);',
  20862. ' $mod.THelper.Foo.call(Self, 1);',
  20863. ' $mod.THelper.Foo.call(Self, 1);',
  20864. ' $mod.THelper.Foo.call(Self, 1);',
  20865. ' };',
  20866. ' return Result;',
  20867. ' };',
  20868. '});',
  20869. '']),
  20870. LinesToStr([ // $mod.$main
  20871. '']));
  20872. end;
  20873. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20874. begin
  20875. StartProgram(false);
  20876. Add([
  20877. 'type',
  20878. ' TObject = class',
  20879. ' class procedure Run(w: word = 10);',
  20880. ' end;',
  20881. ' THelper = class helper for TObject',
  20882. ' class function Foo(w: word = 1): word;',
  20883. ' end;',
  20884. 'class procedure TObject.Run(w: word);',
  20885. 'begin',
  20886. ' Foo;',
  20887. ' Foo();',
  20888. ' Self.Foo;',
  20889. ' Self.Foo();',
  20890. ' with Self do begin',
  20891. ' Foo;',
  20892. ' Foo();',
  20893. ' end;',
  20894. 'end;',
  20895. 'class function THelper.foo(w: word): word;',
  20896. 'begin',
  20897. ' Run;',
  20898. ' Run();',
  20899. ' Foo;',
  20900. ' Foo();',
  20901. ' Self.Foo;',
  20902. ' Self.Foo();',
  20903. ' with Self do begin',
  20904. ' Foo;',
  20905. ' Foo();',
  20906. ' end;',
  20907. 'end;',
  20908. 'var',
  20909. ' Obj: TObject;',
  20910. 'begin',
  20911. ' obj.Foo;',
  20912. ' obj.Foo();',
  20913. ' with obj do begin',
  20914. ' Foo;',
  20915. ' Foo();',
  20916. ' end;',
  20917. ' tobject.Foo;',
  20918. ' tobject.Foo();',
  20919. ' with tobject do begin',
  20920. ' Foo;',
  20921. ' Foo();',
  20922. ' end;',
  20923. '']);
  20924. ConvertProgram;
  20925. CheckSource('TestClassHelper_ClassMethod_Call',
  20926. LinesToStr([ // statements
  20927. 'rtl.createClass(this, "TObject", null, function () {',
  20928. ' this.$init = function () {',
  20929. ' };',
  20930. ' this.$final = function () {',
  20931. ' };',
  20932. ' this.Run = function (w) {',
  20933. ' $mod.THelper.Foo.call(this, 1);',
  20934. ' $mod.THelper.Foo.call(this, 1);',
  20935. ' $mod.THelper.Foo.call(this, 1);',
  20936. ' $mod.THelper.Foo.call(this, 1);',
  20937. ' $mod.THelper.Foo.call(this, 1);',
  20938. ' $mod.THelper.Foo.call(this, 1);',
  20939. ' };',
  20940. '});',
  20941. 'rtl.createHelper(this, "THelper", null, function () {',
  20942. ' this.Foo = function (w) {',
  20943. ' var Result = 0;',
  20944. ' this.Run(10);',
  20945. ' this.Run(10);',
  20946. ' $mod.THelper.Foo.call(this, 1);',
  20947. ' $mod.THelper.Foo.call(this, 1);',
  20948. ' $mod.THelper.Foo.call(this, 1);',
  20949. ' $mod.THelper.Foo.call(this, 1);',
  20950. ' $mod.THelper.Foo.call(this, 1);',
  20951. ' $mod.THelper.Foo.call(this, 1);',
  20952. ' return Result;',
  20953. ' };',
  20954. '});',
  20955. 'this.Obj = null;',
  20956. '']),
  20957. LinesToStr([ // $mod.$main
  20958. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20959. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20960. 'var $with = $mod.Obj;',
  20961. '$mod.THelper.Foo.call($with.$class, 1);',
  20962. '$mod.THelper.Foo.call($with.$class, 1);',
  20963. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20964. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20965. 'var $with1 = $mod.TObject;',
  20966. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20967. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20968. '']));
  20969. end;
  20970. procedure TTestModule.TestClassHelper_ClassOf;
  20971. begin
  20972. StartProgram(false);
  20973. Add([
  20974. 'type',
  20975. ' TObject = class',
  20976. ' end;',
  20977. ' TClass = class of TObject;',
  20978. ' THelper = class helper for TObject',
  20979. ' class function Foo(w: word = 1): word;',
  20980. ' end;',
  20981. 'class function THelper.foo(w: word): word;',
  20982. 'begin',
  20983. 'end;',
  20984. 'var',
  20985. ' c: TClass;',
  20986. 'begin',
  20987. ' c.Foo;',
  20988. ' c.Foo();',
  20989. ' with c do begin',
  20990. ' Foo;',
  20991. ' Foo();',
  20992. ' end;',
  20993. '']);
  20994. ConvertProgram;
  20995. CheckSource('TestClassHelper_ClassOf',
  20996. LinesToStr([ // statements
  20997. 'rtl.createClass(this, "TObject", null, function () {',
  20998. ' this.$init = function () {',
  20999. ' };',
  21000. ' this.$final = function () {',
  21001. ' };',
  21002. '});',
  21003. 'rtl.createHelper(this, "THelper", null, function () {',
  21004. ' this.Foo = function (w) {',
  21005. ' var Result = 0;',
  21006. ' return Result;',
  21007. ' };',
  21008. '});',
  21009. 'this.c = null;',
  21010. '']),
  21011. LinesToStr([ // $mod.$main
  21012. '$mod.THelper.Foo.call($mod.c, 1);',
  21013. '$mod.THelper.Foo.call($mod.c, 1);',
  21014. 'var $with = $mod.c;',
  21015. '$mod.THelper.Foo.call($with, 1);',
  21016. '$mod.THelper.Foo.call($with, 1);',
  21017. '']));
  21018. end;
  21019. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21020. begin
  21021. StartProgram(false);
  21022. Add([
  21023. '{$mode objfpc}',
  21024. 'type',
  21025. ' TObject = class',
  21026. ' procedure DoIt;',
  21027. ' end;',
  21028. ' THelper = class helper for TObject',
  21029. ' procedure Fly(w: word = 1);',
  21030. ' class procedure Glide(w: word = 1);',
  21031. ' class procedure Run(w: word = 1); static;',
  21032. ' end;',
  21033. ' TFly = procedure(w: word) of object;',
  21034. ' TGlide = TFly;',
  21035. ' TRun = procedure(w: word);',
  21036. 'var',
  21037. ' f: TFly;',
  21038. ' g: TGlide;',
  21039. ' r: TRun;',
  21040. 'procedure TObject.DoIt;',
  21041. 'begin',
  21042. ' f:=@fly;',
  21043. ' g:=@glide;',
  21044. ' r:=@run;',
  21045. ' f:[email protected];',
  21046. ' g:[email protected];',
  21047. ' r:[email protected];',
  21048. ' with self do begin',
  21049. ' f:=@fly;',
  21050. ' g:=@glide;',
  21051. ' r:=@run;',
  21052. ' end;',
  21053. 'end;',
  21054. 'procedure THelper.fly(w: word);',
  21055. 'begin',
  21056. ' f:=@fly;',
  21057. ' g:=@glide;',
  21058. ' r:=@run;',
  21059. 'end;',
  21060. 'class procedure THelper.glide(w: word);',
  21061. 'begin',
  21062. ' g:=@glide;',
  21063. ' r:=@run;',
  21064. 'end;',
  21065. 'class procedure THelper.run(w: word);',
  21066. 'begin',
  21067. ' g:=@glide;',
  21068. ' r:=@run;',
  21069. 'end;',
  21070. 'var',
  21071. ' Obj: TObject;',
  21072. 'begin',
  21073. ' f:[email protected];',
  21074. ' g:[email protected];',
  21075. ' r:[email protected];',
  21076. ' with obj do begin',
  21077. ' f:=@fly;',
  21078. ' g:=@glide;',
  21079. ' r:=@run;',
  21080. ' end;',
  21081. ' g:[email protected];',
  21082. ' r:[email protected];',
  21083. ' with tobject do begin',
  21084. ' g:=@glide;',
  21085. ' r:=@run;',
  21086. ' end;',
  21087. '']);
  21088. ConvertProgram;
  21089. CheckSource('TestClassHelper_MethodRefObjFPC',
  21090. LinesToStr([ // statements
  21091. 'rtl.createClass(this, "TObject", null, function () {',
  21092. ' this.$init = function () {',
  21093. ' };',
  21094. ' this.$final = function () {',
  21095. ' };',
  21096. ' this.DoIt = function () {',
  21097. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21098. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21099. ' $mod.r = $mod.THelper.Run;',
  21100. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21101. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21102. ' $mod.r = $mod.THelper.Run;',
  21103. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21104. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21105. ' $mod.r = $mod.THelper.Run;',
  21106. ' };',
  21107. '});',
  21108. 'rtl.createHelper(this, "THelper", null, function () {',
  21109. ' this.Fly = function (w) {',
  21110. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21111. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21112. ' $mod.r = $mod.THelper.Run;',
  21113. ' };',
  21114. ' this.Glide = function (w) {',
  21115. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21116. ' $mod.r = $mod.THelper.Run;',
  21117. ' };',
  21118. ' this.Run = function (w) {',
  21119. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21120. ' $mod.r = $mod.THelper.Run;',
  21121. ' };',
  21122. '});',
  21123. 'this.f = null;',
  21124. 'this.g = null;',
  21125. 'this.r = null;',
  21126. 'this.Obj = null;',
  21127. '']),
  21128. LinesToStr([ // $mod.$main
  21129. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21130. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21131. '$mod.r = $mod.THelper.Run;',
  21132. 'var $with = $mod.Obj;',
  21133. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21134. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21135. '$mod.r = $mod.THelper.Run;',
  21136. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21137. '$mod.r = $mod.THelper.Run;',
  21138. 'var $with1 = $mod.TObject;',
  21139. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21140. '$mod.r = $mod.THelper.Run;',
  21141. '']));
  21142. end;
  21143. procedure TTestModule.TestClassHelper_Constructor;
  21144. begin
  21145. StartProgram(false);
  21146. Add([
  21147. 'type',
  21148. ' TObject = class',
  21149. ' constructor Create;',
  21150. ' end;',
  21151. ' TClass = class of TObject;',
  21152. ' THelper = class helper for TObject',
  21153. ' constructor NewHlp(w: word);',
  21154. ' end;',
  21155. 'var',
  21156. ' obj: TObject;',
  21157. ' c: TClass;',
  21158. 'constructor TObject.Create;',
  21159. 'begin',
  21160. ' NewHlp(2);', // normal call
  21161. ' tobject.NewHlp(3);', // new instance
  21162. ' c.newhlp(4);', // new instance
  21163. 'end;',
  21164. 'constructor THelper.NewHlp(w: word);',
  21165. 'begin',
  21166. ' create;', // normal call
  21167. ' tobject.create;', // new instance
  21168. ' NewHlp(2);', // normal call
  21169. ' tobject.NewHlp(3);', // new instance
  21170. ' c.newhlp(4);', // new instance
  21171. 'end;',
  21172. 'begin',
  21173. ' obj.newhlp(2);', // normal call
  21174. ' with Obj do newhlp(12);', // normal call
  21175. ' tobject.newhlp(3);', // new instance
  21176. ' with tobject do newhlp(13);', // new instance
  21177. ' c.newhlp(4);', // new instance
  21178. ' with c do newhlp(14);', // new instance
  21179. '']);
  21180. ConvertProgram;
  21181. CheckSource('TestClassHelper_Constructor',
  21182. LinesToStr([ // statements
  21183. 'rtl.createClass(this, "TObject", null, function () {',
  21184. ' this.$init = function () {',
  21185. ' };',
  21186. ' this.$final = function () {',
  21187. ' };',
  21188. ' this.Create = function () {',
  21189. ' $mod.THelper.NewHlp.call(this, 2);',
  21190. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21191. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21192. ' return this;',
  21193. ' };',
  21194. '});',
  21195. 'rtl.createHelper(this, "THelper", null, function () {',
  21196. ' this.NewHlp = function (w) {',
  21197. ' this.Create();',
  21198. ' $mod.TObject.$create("Create");',
  21199. ' $mod.THelper.NewHlp.call(this, 2);',
  21200. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21201. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21202. ' return this;',
  21203. ' };',
  21204. '});',
  21205. 'this.obj = null;',
  21206. 'this.c = null;',
  21207. '']),
  21208. LinesToStr([ // $mod.$main
  21209. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21210. 'var $with = $mod.obj;',
  21211. '$mod.THelper.NewHlp.call($with, 12);',
  21212. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21213. 'var $with1 = $mod.TObject;',
  21214. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21215. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21216. 'var $with2 = $mod.c;',
  21217. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21218. '']));
  21219. end;
  21220. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21221. begin
  21222. StartProgram(false);
  21223. Add([
  21224. 'type',
  21225. ' TObject = class',
  21226. ' procedure Fly;',
  21227. ' end;',
  21228. ' TObjHelper = class helper for TObject',
  21229. ' procedure Fly;',
  21230. ' end;',
  21231. ' TBird = class',
  21232. ' procedure Fly;',
  21233. ' end;',
  21234. ' TBirdHelper = class helper for TBird',
  21235. ' procedure Fly;',
  21236. ' procedure Walk(w: word);',
  21237. ' end;',
  21238. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21239. ' procedure Fly;',
  21240. ' procedure Walk(w: word);',
  21241. ' end;',
  21242. 'procedure Tobject.fly;',
  21243. 'begin',
  21244. ' inherited;', // ignore
  21245. 'end;',
  21246. 'procedure Tobjhelper.fly;',
  21247. 'begin',
  21248. ' {@TObject_Fly}inherited;',
  21249. ' inherited {@TObject_Fly}Fly;',
  21250. 'end;',
  21251. 'procedure Tbird.fly;',
  21252. 'begin',
  21253. ' {@TObjHelper_Fly}inherited;',
  21254. ' inherited {@TObjHelper_Fly}Fly;',
  21255. 'end;',
  21256. 'procedure Tbirdhelper.fly;',
  21257. 'begin',
  21258. ' {@TBird_Fly}inherited;',
  21259. ' inherited {@TBird_Fly}Fly;',
  21260. 'end;',
  21261. 'procedure Tbirdhelper.walk(w: word);',
  21262. 'begin',
  21263. 'end;',
  21264. 'procedure teagleHelper.fly;',
  21265. 'begin',
  21266. ' {@TBird_Fly}inherited;',
  21267. ' inherited {@TBird_Fly}Fly;',
  21268. 'end;',
  21269. 'procedure teagleHelper.walk(w: word);',
  21270. 'begin',
  21271. ' {@TBirdHelper_Walk}inherited;',
  21272. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21273. 'end;',
  21274. 'begin',
  21275. '']);
  21276. ConvertProgram;
  21277. CheckSource('TestClassHelper_InheritedObjFPC',
  21278. LinesToStr([ // statements
  21279. 'rtl.createClass(this, "TObject", null, function () {',
  21280. ' this.$init = function () {',
  21281. ' };',
  21282. ' this.$final = function () {',
  21283. ' };',
  21284. ' this.Fly = function () {',
  21285. ' };',
  21286. '});',
  21287. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21288. ' this.Fly = function () {',
  21289. ' $mod.TObject.Fly.call(this);',
  21290. ' $mod.TObject.Fly.call(this);',
  21291. ' };',
  21292. '});',
  21293. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21294. ' this.Fly$1 = function () {',
  21295. ' $mod.TObjHelper.Fly.call(this);',
  21296. ' $mod.TObjHelper.Fly.call(this);',
  21297. ' };',
  21298. '});',
  21299. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21300. ' this.Fly = function () {',
  21301. ' $mod.TBird.Fly$1.call(this);',
  21302. ' $mod.TBird.Fly$1.call(this);',
  21303. ' };',
  21304. ' this.Walk = function (w) {',
  21305. ' };',
  21306. '});',
  21307. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21308. ' this.Fly$1 = function () {',
  21309. ' $mod.TBird.Fly$1.call(this);',
  21310. ' $mod.TBird.Fly$1.call(this);',
  21311. ' };',
  21312. ' this.Walk$1 = function (w) {',
  21313. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21314. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21315. ' };',
  21316. '});',
  21317. '']),
  21318. LinesToStr([ // $mod.$main
  21319. '']));
  21320. end;
  21321. procedure TTestModule.TestClassHelper_Property;
  21322. begin
  21323. StartProgram(false);
  21324. Add([
  21325. 'type',
  21326. ' TObject = class',
  21327. ' FSize: word;',
  21328. ' function GetSpeed: word;',
  21329. ' procedure SetSpeed(Value: word);',
  21330. ' end;',
  21331. ' TObjHelper = class helper for TObject',
  21332. ' function GetLeft: word;',
  21333. ' procedure SetLeft(Value: word);',
  21334. ' property Size: word read FSize write FSize;',
  21335. ' property Speed: word read GetSpeed write SetSpeed;',
  21336. ' property Left: word read GetLeft write SetLeft;',
  21337. ' end;',
  21338. ' TBird = class',
  21339. ' property NotRight: word read GetLeft write SetLeft;',
  21340. ' procedure DoIt;',
  21341. ' end;',
  21342. 'var',
  21343. ' b: TBird;',
  21344. 'function Tobject.GetSpeed: word;',
  21345. 'begin',
  21346. ' Size:=Size+11;',
  21347. ' Speed:=Speed+12;',
  21348. ' Result:=Left+13;',
  21349. ' Left:=13;',
  21350. ' Left:=Left+13;',
  21351. ' Self.Size:=Self.Size+21;',
  21352. ' Self.Speed:=Self.Speed+22;',
  21353. ' Self.Left:=Self.Left+23;',
  21354. ' with Self do begin',
  21355. ' Size:=Size+31;',
  21356. ' Speed:=Speed+32;',
  21357. ' Left:=Left+33;',
  21358. ' end;',
  21359. 'end;',
  21360. 'procedure Tobject.SetSpeed(Value: word);',
  21361. 'begin',
  21362. 'end;',
  21363. 'function TObjHelper.GetLeft: word;',
  21364. 'begin',
  21365. ' Size:=Size+11;',
  21366. ' Speed:=Speed+12;',
  21367. ' Left:=Left+13;',
  21368. ' Self.Size:=Self.Size+21;',
  21369. ' Self.Speed:=Self.Speed+22;',
  21370. ' Self.Left:=Self.Left+23;',
  21371. ' with Self do begin',
  21372. ' Size:=Size+31;',
  21373. ' Speed:=Speed+32;',
  21374. ' Left:=Left+33;',
  21375. ' end;',
  21376. 'end;',
  21377. 'procedure TObjHelper.SetLeft(Value: word);',
  21378. 'begin',
  21379. 'end;',
  21380. 'procedure TBird.DoIt;',
  21381. 'begin',
  21382. ' NotRight:=NotRight+11;',
  21383. ' Self.NotRight:=Self.NotRight+21;',
  21384. ' with Self do begin',
  21385. ' NotRight:=NotRight+31;',
  21386. ' end;',
  21387. 'end;',
  21388. 'begin',
  21389. ' b.Size:=b.Size+11;',
  21390. ' b.Speed:=b.Speed+12;',
  21391. ' b.Left:=b.Left+13;',
  21392. ' b.NotRight:=b.NotRight+14;',
  21393. ' with b do begin',
  21394. ' Size:=Size+31;',
  21395. ' Speed:=Speed+32;',
  21396. ' Left:=Left+33;',
  21397. ' NotRight:=NotRight+34;',
  21398. ' end;',
  21399. '']);
  21400. ConvertProgram;
  21401. CheckSource('TestClassHelper_Property',
  21402. LinesToStr([ // statements
  21403. 'rtl.createClass(this, "TObject", null, function () {',
  21404. ' this.$init = function () {',
  21405. ' this.FSize = 0;',
  21406. ' };',
  21407. ' this.$final = function () {',
  21408. ' };',
  21409. ' this.GetSpeed = function () {',
  21410. ' var Result = 0;',
  21411. ' this.FSize = this.FSize + 11;',
  21412. ' this.SetSpeed(this.GetSpeed() + 12);',
  21413. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21414. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21415. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21416. ' this.FSize = this.FSize + 21;',
  21417. ' this.SetSpeed(this.GetSpeed() + 22);',
  21418. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21419. ' this.FSize = this.FSize + 31;',
  21420. ' this.SetSpeed(this.GetSpeed() + 32);',
  21421. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21422. ' return Result;',
  21423. ' };',
  21424. ' this.SetSpeed = function (Value) {',
  21425. ' };',
  21426. '});',
  21427. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21428. ' this.GetLeft = function () {',
  21429. ' var Result = 0;',
  21430. ' this.FSize = this.FSize + 11;',
  21431. ' this.SetSpeed(this.GetSpeed() + 12);',
  21432. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21433. ' this.FSize = this.FSize + 21;',
  21434. ' this.SetSpeed(this.GetSpeed() + 22);',
  21435. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21436. ' this.FSize = this.FSize + 31;',
  21437. ' this.SetSpeed(this.GetSpeed() + 32);',
  21438. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21439. ' return Result;',
  21440. ' };',
  21441. ' this.SetLeft = function (Value) {',
  21442. ' };',
  21443. '});',
  21444. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21445. ' this.DoIt = function () {',
  21446. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21447. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21448. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21449. ' };',
  21450. '});',
  21451. 'this.b = null;',
  21452. '']),
  21453. LinesToStr([ // $mod.$main
  21454. '$mod.b.FSize = $mod.b.FSize + 11;',
  21455. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21456. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21457. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21458. 'var $with = $mod.b;',
  21459. '$with.FSize = $with.FSize + 31;',
  21460. '$with.SetSpeed($with.GetSpeed() + 32);',
  21461. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21462. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21463. '']));
  21464. end;
  21465. procedure TTestModule.TestClassHelper_Property_Array;
  21466. begin
  21467. StartProgram(false);
  21468. Add([
  21469. 'type',
  21470. ' TObject = class',
  21471. ' function GetSpeed(Index: boolean): word;',
  21472. ' procedure SetSpeed(Index: boolean; Value: word);',
  21473. ' end;',
  21474. ' TObjHelper = class helper for TObject',
  21475. ' function GetSize(Index: boolean): word;',
  21476. ' procedure SetSize(Index: boolean; Value: word);',
  21477. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21478. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21479. ' end;',
  21480. ' TBird = class',
  21481. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21482. ' procedure DoIt;',
  21483. ' end;',
  21484. 'var',
  21485. ' b: TBird;',
  21486. 'function Tobject.GetSpeed(Index: boolean): word;',
  21487. 'begin',
  21488. ' Result:=Size[false];',
  21489. ' Size[true]:=Size[false]+11;',
  21490. ' Speed[true]:=Speed[false]+12;',
  21491. ' Self.Size[true]:=Self.Size[false]+21;',
  21492. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21493. ' with Self do begin',
  21494. ' Size[true]:=Size[false]+31;',
  21495. ' Speed[true]:=Speed[false]+32;',
  21496. ' end;',
  21497. 'end;',
  21498. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21499. 'begin',
  21500. 'end;',
  21501. 'function TObjHelper.GetSize(Index: boolean): word;',
  21502. 'begin',
  21503. ' Size[true]:=Size[false]+11;',
  21504. ' Speed[true]:=Speed[false]+12;',
  21505. ' Self.Size[true]:=Self.Size[false]+21;',
  21506. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21507. ' with Self do begin',
  21508. ' Size[true]:=Size[false]+31;',
  21509. ' Speed[true]:=Speed[false]+32;',
  21510. ' end;',
  21511. 'end;',
  21512. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21513. 'begin',
  21514. 'end;',
  21515. 'procedure TBird.DoIt;',
  21516. 'begin',
  21517. ' Items[true]:=Items[false]+11;',
  21518. ' Self.Items[true]:=Self.Items[false]+21;',
  21519. ' with Self do Items[true]:=Items[false]+31;',
  21520. 'end;',
  21521. 'begin',
  21522. ' b.Size[true]:=b.Size[false]+11;',
  21523. ' b.Speed[true]:=b.Speed[false]+12;',
  21524. ' b.Items[true]:=b.Items[false]+13;',
  21525. ' with b do begin',
  21526. ' Size[true]:=Size[false]+21;',
  21527. ' Speed[true]:=Speed[false]+22;',
  21528. ' Items[true]:=Items[false]+23;',
  21529. ' end;',
  21530. '']);
  21531. ConvertProgram;
  21532. CheckSource('TestClassHelper_Property_Array',
  21533. LinesToStr([ // statements
  21534. 'rtl.createClass(this, "TObject", null, function () {',
  21535. ' this.$init = function () {',
  21536. ' };',
  21537. ' this.$final = function () {',
  21538. ' };',
  21539. ' this.GetSpeed = function (Index) {',
  21540. ' var Result = 0;',
  21541. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21542. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21543. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21544. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21545. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21546. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21547. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21548. ' return Result;',
  21549. ' };',
  21550. ' this.SetSpeed = function (Index, Value) {',
  21551. ' };',
  21552. '});',
  21553. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21554. ' this.GetSize = function (Index) {',
  21555. ' var Result = 0;',
  21556. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21557. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21558. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21559. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21560. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21561. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21562. ' return Result;',
  21563. ' };',
  21564. ' this.SetSize = function (Index, Value) {',
  21565. ' };',
  21566. '});',
  21567. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21568. ' this.DoIt = function () {',
  21569. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21570. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21571. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21572. ' };',
  21573. '});',
  21574. 'this.b = null;',
  21575. '']),
  21576. LinesToStr([ // $mod.$main
  21577. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21578. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21579. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21580. 'var $with = $mod.b;',
  21581. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21582. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21583. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21584. '']));
  21585. end;
  21586. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21587. begin
  21588. StartProgram(false);
  21589. Add([
  21590. 'type',
  21591. ' TObject = class',
  21592. ' function GetSpeed(Index: boolean): word;',
  21593. ' procedure SetSpeed(Index: boolean; Value: word);',
  21594. ' end;',
  21595. ' TObjHelper = class helper for TObject',
  21596. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21597. ' end;',
  21598. ' TBird = class',
  21599. ' end;',
  21600. ' TBirdHelper = class helper for TBird',
  21601. ' function GetSize(Index: word): boolean;',
  21602. ' procedure SetSize(Index: word; Value: boolean);',
  21603. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21604. ' end;',
  21605. 'function Tobject.GetSpeed(Index: boolean): word;',
  21606. 'begin',
  21607. ' Self[true]:=Self[false]+1;',
  21608. 'end;',
  21609. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21610. 'begin',
  21611. 'end;',
  21612. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21613. 'begin',
  21614. ' Self[1]:=not Self[2];',
  21615. 'end;',
  21616. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21617. 'begin',
  21618. 'end;',
  21619. 'var',
  21620. ' o: TObject;',
  21621. ' b: TBird;',
  21622. 'begin',
  21623. ' o[true]:=o[false]+1;',
  21624. ' b[3]:=not b[4];',
  21625. '']);
  21626. ConvertProgram;
  21627. CheckSource('TestClassHelper_Property_Array_Default',
  21628. LinesToStr([ // statements
  21629. 'rtl.createClass(this, "TObject", null, function () {',
  21630. ' this.$init = function () {',
  21631. ' };',
  21632. ' this.$final = function () {',
  21633. ' };',
  21634. ' this.GetSpeed = function (Index) {',
  21635. ' var Result = 0;',
  21636. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21637. ' return Result;',
  21638. ' };',
  21639. ' this.SetSpeed = function (Index, Value) {',
  21640. ' };',
  21641. '});',
  21642. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21643. '});',
  21644. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21645. '});',
  21646. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21647. ' this.GetSize = function (Index) {',
  21648. ' var Result = false;',
  21649. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21650. ' return Result;',
  21651. ' };',
  21652. ' this.SetSize = function (Index, Value) {',
  21653. ' };',
  21654. '});',
  21655. 'this.o = null;',
  21656. 'this.b = null;',
  21657. '']),
  21658. LinesToStr([ // $mod.$main
  21659. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21660. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21661. '']));
  21662. end;
  21663. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21664. begin
  21665. StartProgram(false);
  21666. Add([
  21667. 'type',
  21668. ' TObject = class',
  21669. ' end;',
  21670. ' TObjHelper = class helper for TObject',
  21671. ' function GetItems(Index: word): TObject;',
  21672. ' procedure SetItems(Index: word; Value: TObject);',
  21673. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21674. ' end;',
  21675. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21676. 'begin',
  21677. ' Self[1][2]:=Self[3][4];',
  21678. 'end;',
  21679. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21680. 'begin',
  21681. 'end;',
  21682. 'var',
  21683. ' o: TObject;',
  21684. 'begin',
  21685. ' o[1][2]:=o[3][4];',
  21686. '']);
  21687. ConvertProgram;
  21688. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21689. LinesToStr([ // statements
  21690. 'rtl.createClass(this, "TObject", null, function () {',
  21691. ' this.$init = function () {',
  21692. ' };',
  21693. ' this.$final = function () {',
  21694. ' };',
  21695. '});',
  21696. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21697. ' this.GetItems = function (Index) {',
  21698. ' var Result = null;',
  21699. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21700. ' return Result;',
  21701. ' };',
  21702. ' this.SetItems = function (Index, Value) {',
  21703. ' };',
  21704. '});',
  21705. 'this.o = null;',
  21706. '']),
  21707. LinesToStr([ // $mod.$main
  21708. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21709. '']));
  21710. end;
  21711. procedure TTestModule.TestClassHelper_ClassProperty;
  21712. begin
  21713. StartProgram(false);
  21714. Add([
  21715. 'type',
  21716. ' TObject = class',
  21717. ' class var FSize: word;',
  21718. ' class function GetSpeed: word;',
  21719. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21720. ' end;',
  21721. ' TObjHelper = class helper for TObject',
  21722. ' class function GetLeft: word;',
  21723. ' class procedure SetLeft(Value: word);',
  21724. ' class property Size: word read FSize write FSize;',
  21725. ' class property Speed: word read GetSpeed write SetSpeed;',
  21726. ' class property Left: word read GetLeft write SetLeft;',
  21727. ' end;',
  21728. ' TBird = class',
  21729. ' class property NotRight: word read GetLeft write SetLeft;',
  21730. ' class procedure DoIt;',
  21731. ' end;',
  21732. ' TBirdClass = class of TBird;',
  21733. 'class function Tobject.GetSpeed: word;',
  21734. 'begin',
  21735. ' Size:=Size+11;',
  21736. ' Speed:=Speed+12;',
  21737. ' Left:=Left+13;',
  21738. ' Self.Size:=Self.Size+21;',
  21739. ' Self.Speed:=Self.Speed+22;',
  21740. ' Self.Left:=Self.Left+23;',
  21741. ' with Self do begin',
  21742. ' Size:=Size+31;',
  21743. ' Speed:=Speed+32;',
  21744. ' Left:=Left+33;',
  21745. ' end;',
  21746. 'end;',
  21747. 'class function TObjHelper.GetLeft: word;',
  21748. 'begin',
  21749. ' Size:=Size+11;',
  21750. ' Speed:=Speed+12;',
  21751. ' Left:=Left+13;',
  21752. ' Self.Size:=Self.Size+21;',
  21753. ' Self.Speed:=Self.Speed+22;',
  21754. ' Self.Left:=Self.Left+23;',
  21755. ' with Self do begin',
  21756. ' Size:=Size+31;',
  21757. ' Speed:=Speed+32;',
  21758. ' Left:=Left+33;',
  21759. ' end;',
  21760. 'end;',
  21761. 'class procedure TObjHelper.SetLeft(Value: word);',
  21762. 'begin',
  21763. 'end;',
  21764. 'class procedure TBird.DoIt;',
  21765. 'begin',
  21766. ' NotRight:=NotRight+11;',
  21767. ' Self.NotRight:=Self.NotRight+21;',
  21768. ' with Self do NotRight:=NotRight+31;',
  21769. 'end;',
  21770. 'var',
  21771. ' b: TBird;',
  21772. ' c: TBirdClass;',
  21773. 'begin',
  21774. ' b.Size:=b.Size+11;',
  21775. ' b.Speed:=b.Speed+12;',
  21776. ' b.Left:=b.Left+13;',
  21777. ' b.NotRight:=b.NotRight+14;',
  21778. ' with b do begin',
  21779. ' Size:=Size+31;',
  21780. ' Speed:=Speed+32;',
  21781. ' Left:=Left+33;',
  21782. ' NotRight:=NotRight+34;',
  21783. ' end;',
  21784. ' c.Size:=c.Size+11;',
  21785. ' c.Speed:=c.Speed+12;',
  21786. ' c.Left:=c.Left+13;',
  21787. ' c.NotRight:=c.NotRight+14;',
  21788. ' with c do begin',
  21789. ' Size:=Size+31;',
  21790. ' Speed:=Speed+32;',
  21791. ' Left:=Left+33;',
  21792. ' NotRight:=NotRight+34;',
  21793. ' end;',
  21794. ' tbird.Size:=tbird.Size+11;',
  21795. ' tbird.Speed:=tbird.Speed+12;',
  21796. ' tbird.Left:=tbird.Left+13;',
  21797. ' tbird.NotRight:=tbird.NotRight+14;',
  21798. ' with tbird do begin',
  21799. ' Size:=Size+31;',
  21800. ' Speed:=Speed+32;',
  21801. ' Left:=Left+33;',
  21802. ' NotRight:=NotRight+34;',
  21803. ' end;',
  21804. '']);
  21805. ConvertProgram;
  21806. CheckSource('TestClassHelper_ClassProperty',
  21807. LinesToStr([ // statements
  21808. 'rtl.createClass(this, "TObject", null, function () {',
  21809. ' this.FSize = 0;',
  21810. ' this.$init = function () {',
  21811. ' };',
  21812. ' this.$final = function () {',
  21813. ' };',
  21814. ' this.GetSpeed = function () {',
  21815. ' var Result = 0;',
  21816. ' $mod.TObject.FSize = this.FSize + 11;',
  21817. ' this.SetSpeed(this.GetSpeed() + 12);',
  21818. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21819. ' $mod.TObject.FSize = this.FSize + 21;',
  21820. ' this.SetSpeed(this.GetSpeed() + 22);',
  21821. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21822. ' $mod.TObject.FSize = this.FSize + 31;',
  21823. ' this.SetSpeed(this.GetSpeed() + 32);',
  21824. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21825. ' return Result;',
  21826. ' };',
  21827. '});',
  21828. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21829. ' this.GetLeft = function () {',
  21830. ' var Result = 0;',
  21831. ' $mod.TObject.FSize = this.FSize + 11;',
  21832. ' this.SetSpeed(this.GetSpeed() + 12);',
  21833. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21834. ' $mod.TObject.FSize = this.FSize + 21;',
  21835. ' this.SetSpeed(this.GetSpeed() + 22);',
  21836. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21837. ' $mod.TObject.FSize = this.FSize + 31;',
  21838. ' this.SetSpeed(this.GetSpeed() + 32);',
  21839. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21840. ' return Result;',
  21841. ' };',
  21842. ' this.SetLeft = function (Value) {',
  21843. ' };',
  21844. '});',
  21845. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21846. ' this.DoIt = function () {',
  21847. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21848. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21849. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21850. ' };',
  21851. '});',
  21852. 'this.b = null;',
  21853. 'this.c = null;',
  21854. '']),
  21855. LinesToStr([ // $mod.$main
  21856. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21857. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21858. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21859. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21860. 'var $with = $mod.b;',
  21861. '$mod.TObject.FSize = $with.FSize + 31;',
  21862. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21863. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21864. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21865. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21866. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21867. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21868. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21869. 'var $with1 = $mod.c;',
  21870. '$mod.TObject.FSize = $with1.FSize + 31;',
  21871. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21872. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21873. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21874. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21875. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21876. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21877. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21878. 'var $with2 = $mod.TBird;',
  21879. '$mod.TObject.FSize = $with2.FSize + 31;',
  21880. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21881. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21882. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21883. '']));
  21884. end;
  21885. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21886. begin
  21887. StartProgram(false);
  21888. Add([
  21889. 'type',
  21890. ' TObject = class',
  21891. ' class function GetSpeed: word; static;',
  21892. ' class procedure SetSpeed(Value: word); static;',
  21893. ' end;',
  21894. ' TObjHelper = class helper for TObject',
  21895. ' class function GetLeft: word; static;',
  21896. ' class procedure SetLeft(Value: word); static;',
  21897. ' class property Speed: word read GetSpeed write SetSpeed;',
  21898. ' class property Left: word read GetLeft write SetLeft;',
  21899. ' end;',
  21900. ' TBird = class',
  21901. ' class property NotRight: word read GetLeft write SetLeft;',
  21902. ' class procedure DoIt; static;',
  21903. ' class procedure DoSome;',
  21904. ' end;',
  21905. ' TBirdClass = class of TBird;',
  21906. 'class function Tobject.GetSpeed: word;',
  21907. 'begin',
  21908. ' Speed:=Speed+12;',
  21909. ' Left:=Left+13;',
  21910. 'end;',
  21911. 'class procedure TObject.SetSpeed(Value: word);',
  21912. 'begin',
  21913. 'end;',
  21914. 'class function TObjHelper.GetLeft: word;',
  21915. 'begin',
  21916. ' Speed:=Speed+12;',
  21917. ' Left:=Left+13;',
  21918. 'end;',
  21919. 'class procedure TObjHelper.SetLeft(Value: word);',
  21920. 'begin',
  21921. 'end;',
  21922. 'class procedure TBird.DoIt;',
  21923. 'begin',
  21924. ' NotRight:=NotRight+11;',
  21925. 'end;',
  21926. 'class procedure TBird.DoSome;',
  21927. 'begin',
  21928. ' Speed:=Speed+12;',
  21929. ' Left:=Left+13;',
  21930. ' Self.Speed:=Self.Speed+22;',
  21931. ' Self.Left:=Self.Left+23;',
  21932. ' with Self do begin',
  21933. ' Speed:=Speed+32;',
  21934. ' Left:=Left+33;',
  21935. ' end;',
  21936. ' NotRight:=NotRight+11;',
  21937. ' Self.NotRight:=Self.NotRight+21;',
  21938. ' with Self do NotRight:=NotRight+31;',
  21939. 'end;',
  21940. 'var',
  21941. ' b: TBird;',
  21942. ' c: TBirdClass;',
  21943. 'begin',
  21944. ' b.Speed:=b.Speed+12;',
  21945. ' b.Left:=b.Left+13;',
  21946. ' b.NotRight:=b.NotRight+14;',
  21947. ' with b do begin',
  21948. ' Speed:=Speed+32;',
  21949. ' Left:=Left+33;',
  21950. ' NotRight:=NotRight+34;',
  21951. ' end;',
  21952. ' c.Speed:=c.Speed+12;',
  21953. ' c.Left:=c.Left+13;',
  21954. ' c.NotRight:=c.NotRight+14;',
  21955. ' with c do begin',
  21956. ' Speed:=Speed+32;',
  21957. ' Left:=Left+33;',
  21958. ' NotRight:=NotRight+34;',
  21959. ' end;',
  21960. ' tbird.Speed:=tbird.Speed+12;',
  21961. ' tbird.Left:=tbird.Left+13;',
  21962. ' tbird.NotRight:=tbird.NotRight+14;',
  21963. ' with tbird do begin',
  21964. ' Speed:=Speed+32;',
  21965. ' Left:=Left+33;',
  21966. ' NotRight:=NotRight+34;',
  21967. ' end;',
  21968. '']);
  21969. ConvertProgram;
  21970. CheckSource('TestClassHelper_ClassPropertyStatic',
  21971. LinesToStr([ // statements
  21972. 'rtl.createClass(this, "TObject", null, function () {',
  21973. ' this.$init = function () {',
  21974. ' };',
  21975. ' this.$final = function () {',
  21976. ' };',
  21977. ' this.GetSpeed = function () {',
  21978. ' var Result = 0;',
  21979. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21980. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21981. ' return Result;',
  21982. ' };',
  21983. ' this.SetSpeed = function (Value) {',
  21984. ' };',
  21985. '});',
  21986. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21987. ' this.GetLeft = function () {',
  21988. ' var Result = 0;',
  21989. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21990. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21991. ' return Result;',
  21992. ' };',
  21993. ' this.SetLeft = function (Value) {',
  21994. ' };',
  21995. '});',
  21996. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21997. ' this.DoIt = function () {',
  21998. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21999. ' };',
  22000. ' this.DoSome = function () {',
  22001. ' this.SetSpeed(this.GetSpeed() + 12);',
  22002. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22003. ' this.SetSpeed(this.GetSpeed() + 22);',
  22004. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22005. ' this.SetSpeed(this.GetSpeed() + 32);',
  22006. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22007. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22008. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22009. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22010. ' };',
  22011. '});',
  22012. 'this.b = null;',
  22013. 'this.c = null;',
  22014. '']),
  22015. LinesToStr([ // $mod.$main
  22016. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  22017. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22018. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22019. 'var $with = $mod.b;',
  22020. '$with.SetSpeed($with.GetSpeed() + 32);',
  22021. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22022. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22023. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22024. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22025. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22026. 'var $with1 = $mod.c;',
  22027. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22028. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22029. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22030. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22031. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22032. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22033. 'var $with2 = $mod.TBird;',
  22034. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22035. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22036. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22037. '']));
  22038. end;
  22039. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22040. begin
  22041. StartProgram(false);
  22042. Add([
  22043. 'type',
  22044. ' TObject = class',
  22045. ' class function GetSpeed(Index: boolean): word;',
  22046. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22047. ' end;',
  22048. ' TObjHelper = class helper for TObject',
  22049. ' class function GetSize(Index: boolean): word;',
  22050. ' class procedure SetSize(Index: boolean; Value: word);',
  22051. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22052. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22053. ' end;',
  22054. ' TBird = class',
  22055. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22056. ' class procedure DoIt;',
  22057. ' end;',
  22058. ' TBirdClass = class of TBird;',
  22059. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22060. 'begin',
  22061. ' Size[true]:=Size[false]+11;',
  22062. ' Speed[true]:=Speed[false]+12;',
  22063. ' Self.Size[true]:=Self.Size[false]+21;',
  22064. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22065. ' with Self do begin',
  22066. ' Size[true]:=Size[false]+31;',
  22067. ' Speed[true]:=Speed[false]+32;',
  22068. ' end;',
  22069. 'end;',
  22070. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22071. 'begin',
  22072. ' Size[true]:=Size[false]+11;',
  22073. ' Speed[true]:=Speed[false]+12;',
  22074. ' Self.Size[true]:=Self.Size[false]+21;',
  22075. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22076. ' with Self do begin',
  22077. ' Size[true]:=Size[false]+31;',
  22078. ' Speed[true]:=Speed[false]+32;',
  22079. ' end;',
  22080. 'end;',
  22081. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22082. 'begin',
  22083. 'end;',
  22084. 'class procedure TBird.DoIt;',
  22085. 'begin',
  22086. ' Items[true]:=Items[false]+11;',
  22087. ' Self.Items[true]:=Self.Items[false]+21;',
  22088. ' with Self do Items[true]:=Items[false]+31;',
  22089. 'end;',
  22090. 'var',
  22091. ' b: TBird;',
  22092. ' c: TBirdClass;',
  22093. 'begin',
  22094. ' b.Size[true]:=b.Size[false]+11;',
  22095. ' b.Speed[true]:=b.Speed[false]+12;',
  22096. ' b.Items[true]:=b.Items[false]+13;',
  22097. ' with b do begin',
  22098. ' Size[true]:=Size[false]+21;',
  22099. ' Speed[true]:=Speed[false]+22;',
  22100. ' Items[true]:=Items[false]+23;',
  22101. ' end;',
  22102. ' c.Size[true]:=c.Size[false]+11;',
  22103. ' c.Speed[true]:=c.Speed[false]+12;',
  22104. ' c.Items[true]:=c.Items[false]+13;',
  22105. ' with c do begin',
  22106. ' Size[true]:=Size[false]+21;',
  22107. ' Speed[true]:=Speed[false]+22;',
  22108. ' Items[true]:=Items[false]+23;',
  22109. ' end;',
  22110. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22111. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22112. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22113. ' with TBird do begin',
  22114. ' Size[true]:=Size[false]+21;',
  22115. ' Speed[true]:=Speed[false]+22;',
  22116. ' Items[true]:=Items[false]+23;',
  22117. ' end;',
  22118. '']);
  22119. ConvertProgram;
  22120. CheckSource('TestClassHelper_ClassProperty_Array',
  22121. LinesToStr([ // statements
  22122. 'rtl.createClass(this, "TObject", null, function () {',
  22123. ' this.$init = function () {',
  22124. ' };',
  22125. ' this.$final = function () {',
  22126. ' };',
  22127. ' this.GetSpeed = function (Index) {',
  22128. ' var Result = 0;',
  22129. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22130. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22131. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22132. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22133. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22134. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22135. ' return Result;',
  22136. ' };',
  22137. '});',
  22138. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22139. ' this.GetSize = function (Index) {',
  22140. ' var Result = 0;',
  22141. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22142. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22143. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22144. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22145. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22146. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22147. ' return Result;',
  22148. ' };',
  22149. ' this.SetSize = function (Index, Value) {',
  22150. ' };',
  22151. '});',
  22152. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22153. ' this.DoIt = function () {',
  22154. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22155. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22156. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22157. ' };',
  22158. '});',
  22159. 'this.b = null;',
  22160. 'this.c = null;',
  22161. '']),
  22162. LinesToStr([ // $mod.$main
  22163. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22164. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22165. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22166. 'var $with = $mod.b;',
  22167. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22168. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22169. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22170. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22171. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22172. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22173. 'var $with1 = $mod.c;',
  22174. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22175. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22176. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22177. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22178. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22179. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22180. 'var $with2 = $mod.TBird;',
  22181. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22182. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22183. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22184. '']));
  22185. end;
  22186. procedure TTestModule.TestClassHelper_ForIn;
  22187. begin
  22188. StartProgram(false);
  22189. Add([
  22190. 'type',
  22191. ' TObject = class end;',
  22192. ' TItem = TObject;',
  22193. ' TEnumerator = class',
  22194. ' FCurrent: TItem;',
  22195. ' property Current: TItem read FCurrent;',
  22196. ' function MoveNext: boolean;',
  22197. ' end;',
  22198. ' TBird = class',
  22199. ' end;',
  22200. ' TBirdHelper = class helper for TBird',
  22201. ' function GetEnumerator: TEnumerator;',
  22202. ' end;',
  22203. 'function TEnumerator.MoveNext: boolean;',
  22204. 'begin',
  22205. 'end;',
  22206. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22207. 'begin',
  22208. 'end;',
  22209. 'var',
  22210. ' b: TBird;',
  22211. ' i, i2: TItem;',
  22212. 'begin',
  22213. ' for i in b do i2:=i;']);
  22214. ConvertProgram;
  22215. CheckSource('TestClassHelper_ForIn',
  22216. LinesToStr([ // statements
  22217. 'rtl.createClass(this, "TObject", null, function () {',
  22218. ' this.$init = function () {',
  22219. ' };',
  22220. ' this.$final = function () {',
  22221. ' };',
  22222. '});',
  22223. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22224. ' this.$init = function () {',
  22225. ' $mod.TObject.$init.call(this);',
  22226. ' this.FCurrent = null;',
  22227. ' };',
  22228. ' this.$final = function () {',
  22229. ' this.FCurrent = undefined;',
  22230. ' $mod.TObject.$final.call(this);',
  22231. ' };',
  22232. ' this.MoveNext = function () {',
  22233. ' var Result = false;',
  22234. ' return Result;',
  22235. ' };',
  22236. '});',
  22237. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22238. '});',
  22239. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22240. ' this.GetEnumerator = function () {',
  22241. ' var Result = null;',
  22242. ' return Result;',
  22243. ' };',
  22244. '});',
  22245. 'this.b = null;',
  22246. 'this.i = null;',
  22247. 'this.i2 = null;'
  22248. ]),
  22249. LinesToStr([ // $mod.$main
  22250. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22251. 'try {',
  22252. ' while ($in.MoveNext()){',
  22253. ' $mod.i = $in.FCurrent;',
  22254. ' $mod.i2 = $mod.i;',
  22255. ' }',
  22256. '} finally {',
  22257. ' $in = rtl.freeLoc($in)',
  22258. '};',
  22259. '']));
  22260. end;
  22261. procedure TTestModule.TestClassHelper_PassProperty;
  22262. begin
  22263. StartProgram(false);
  22264. Add([
  22265. 'type',
  22266. ' TObject = class',
  22267. ' FField: TObject;',
  22268. ' property Field: TObject read FField write FField;',
  22269. ' end;',
  22270. ' THelper = class helper for TObject',
  22271. ' procedure Fly;',
  22272. ' class procedure Run;',
  22273. ' class procedure Jump; static;',
  22274. ' end;',
  22275. 'procedure THelper.Fly;',
  22276. 'begin',
  22277. ' Field.Fly;',
  22278. ' Field.Run;',
  22279. ' Field.Jump;',
  22280. ' with Field do begin',
  22281. ' Fly;',
  22282. ' Run;',
  22283. ' Jump;',
  22284. ' end;',
  22285. 'end;',
  22286. 'class procedure THelper.Run;',
  22287. 'begin',
  22288. 'end;',
  22289. 'class procedure THelper.Jump;',
  22290. 'begin',
  22291. 'end;',
  22292. 'var',
  22293. ' b: TObject;',
  22294. 'begin',
  22295. ' b.Field.Fly;',
  22296. ' b.Field.Run;',
  22297. ' b.Field.Jump;',
  22298. ' with b do begin',
  22299. ' Field.Run;',
  22300. ' Field.Fly;',
  22301. ' Field.Jump;',
  22302. ' end;',
  22303. ' with b.Field do begin',
  22304. ' Run;',
  22305. ' Fly;',
  22306. ' Jump;',
  22307. ' end;',
  22308. '']);
  22309. ConvertProgram;
  22310. CheckSource('TestClassHelper_PassProperty',
  22311. LinesToStr([ // statements
  22312. 'rtl.createClass(this, "TObject", null, function () {',
  22313. ' this.$init = function () {',
  22314. ' this.FField = null;',
  22315. ' };',
  22316. ' this.$final = function () {',
  22317. ' this.FField = undefined;',
  22318. ' };',
  22319. '});',
  22320. 'rtl.createHelper(this, "THelper", null, function () {',
  22321. ' this.Fly = function () {',
  22322. ' $mod.THelper.Fly.call(this.FField);',
  22323. ' $mod.THelper.Run.call(this.FField.$class);',
  22324. ' $mod.THelper.Jump();',
  22325. ' var $with = this.FField;',
  22326. ' $mod.THelper.Fly.call($with);',
  22327. ' $mod.THelper.Run.call($with.$class);',
  22328. ' $mod.THelper.Jump();',
  22329. ' };',
  22330. ' this.Run = function () {',
  22331. ' };',
  22332. ' this.Jump = function () {',
  22333. ' };',
  22334. '});',
  22335. 'this.b = null;',
  22336. '']),
  22337. LinesToStr([ // $mod.$main
  22338. '$mod.THelper.Fly.call($mod.b.FField);',
  22339. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22340. '$mod.THelper.Jump();',
  22341. 'var $with = $mod.b;',
  22342. '$mod.THelper.Run.call($with.FField.$class);',
  22343. '$mod.THelper.Fly.call($with.FField);',
  22344. '$mod.THelper.Jump();',
  22345. 'var $with1 = $mod.b.FField;',
  22346. '$mod.THelper.Run.call($with1.$class);',
  22347. '$mod.THelper.Fly.call($with1);',
  22348. '$mod.THelper.Jump();',
  22349. '']));
  22350. end;
  22351. procedure TTestModule.TestExtClassHelper_ClassVar;
  22352. begin
  22353. StartProgram(false);
  22354. Add([
  22355. '{$modeswitch externalclass}',
  22356. 'type',
  22357. ' TExtA = class external name ''ExtObj''',
  22358. ' end;',
  22359. ' THelper = class helper for TExtA',
  22360. ' const',
  22361. ' One = 1;',
  22362. ' Two: word = 2;',
  22363. ' class var',
  22364. ' Glob: word;',
  22365. ' function Foo(w: word): word;',
  22366. ' class function Bar(w: word): word; static;',
  22367. ' end;',
  22368. 'function THelper.foo(w: word): word;',
  22369. 'begin',
  22370. ' Result:=w;',
  22371. ' Two:=One+w;',
  22372. ' Glob:=Glob;',
  22373. ' Result:=Self.Glob;',
  22374. ' Self.Glob:=Self.Glob;',
  22375. ' with Self do Glob:=Glob;',
  22376. 'end;',
  22377. 'class function THelper.bar(w: word): word;',
  22378. 'begin',
  22379. ' Result:=w;',
  22380. ' Two:=One;',
  22381. ' Glob:=Glob;',
  22382. 'end;',
  22383. 'var o: TExtA;',
  22384. 'begin',
  22385. ' texta.two:=texta.one;',
  22386. ' texta.Glob:=texta.Glob;',
  22387. ' with texta do begin',
  22388. ' two:=one;',
  22389. ' Glob:=Glob;',
  22390. ' end;',
  22391. ' o.two:=o.one;',
  22392. ' o.Glob:=o.Glob;',
  22393. ' with o do begin',
  22394. ' two:=one;',
  22395. ' Glob:=Glob;',
  22396. ' end;',
  22397. '']);
  22398. ConvertProgram;
  22399. CheckSource('TestExtClassHelper_ClassVar',
  22400. LinesToStr([ // statements
  22401. 'rtl.createHelper(this, "THelper", null, function () {',
  22402. ' this.One = 1;',
  22403. ' this.Two = 2;',
  22404. ' this.Glob = 0;',
  22405. ' this.Foo = function (w) {',
  22406. ' var Result = 0;',
  22407. ' Result = w;',
  22408. ' $mod.THelper.Two = 1 + w;',
  22409. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22410. ' Result = $mod.THelper.Glob;',
  22411. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22412. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22413. ' return Result;',
  22414. ' };',
  22415. ' this.Bar = function (w) {',
  22416. ' var Result = 0;',
  22417. ' Result = w;',
  22418. ' $mod.THelper.Two = 1;',
  22419. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22420. ' return Result;',
  22421. ' };',
  22422. '});',
  22423. 'this.o = null;',
  22424. '']),
  22425. LinesToStr([ // $mod.$main
  22426. '$mod.THelper.Two = 1;',
  22427. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22428. '$mod.THelper.Two = 1;',
  22429. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22430. '$mod.THelper.Two = 1;',
  22431. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22432. 'var $with = $mod.o;',
  22433. '$mod.THelper.Two = 1;',
  22434. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22435. '']));
  22436. end;
  22437. procedure TTestModule.TestExtClassHelper_Method_Call;
  22438. begin
  22439. StartProgram(false);
  22440. Add([
  22441. '{$modeswitch externalclass}',
  22442. 'type',
  22443. ' TFly = function(w: word): word of object;',
  22444. ' TExtA = class external name ''ExtObj''',
  22445. ' procedure Run(w: word = 10);',
  22446. ' end;',
  22447. ' THelper = class helper for TExtA',
  22448. ' function Foo(w: word = 1): word;',
  22449. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22450. ' end;',
  22451. 'var p: TFly;',
  22452. 'function THelper.foo(w: word): word;',
  22453. 'begin',
  22454. ' Run;',
  22455. ' Run();',
  22456. ' Run(11);',
  22457. ' Foo;',
  22458. ' Foo();',
  22459. ' Foo(12);',
  22460. ' Self.Foo;',
  22461. ' Self.Foo();',
  22462. ' Self.Foo(13);',
  22463. ' Fly;',
  22464. ' Fly();',
  22465. ' with Self do begin',
  22466. ' Foo;',
  22467. ' Foo();',
  22468. ' Foo(14);',
  22469. ' Fly;',
  22470. ' Fly();',
  22471. ' end;',
  22472. ' p:=@Fly;',
  22473. 'end;',
  22474. 'var Obj: TExtA;',
  22475. 'begin',
  22476. ' obj.Foo;',
  22477. ' obj.Foo();',
  22478. ' obj.Foo(21);',
  22479. ' obj.Fly;',
  22480. ' obj.Fly();',
  22481. ' with obj do begin',
  22482. ' Foo;',
  22483. ' Foo();',
  22484. ' Foo(22);',
  22485. ' Fly;',
  22486. ' Fly();',
  22487. ' end;',
  22488. ' p:[email protected];',
  22489. '']);
  22490. ConvertProgram;
  22491. CheckSource('TestExtClassHelper_Method_Call',
  22492. LinesToStr([ // statements
  22493. 'rtl.createHelper(this, "THelper", null, function () {',
  22494. ' this.Foo = function (w) {',
  22495. ' var Result = 0;',
  22496. ' this.Run(10);',
  22497. ' this.Run(10);',
  22498. ' this.Run(11);',
  22499. ' $mod.THelper.Foo.call(this, 1);',
  22500. ' $mod.THelper.Foo.call(this, 1);',
  22501. ' $mod.THelper.Foo.call(this, 12);',
  22502. ' $mod.THelper.Foo.call(this, 1);',
  22503. ' $mod.THelper.Foo.call(this, 1);',
  22504. ' $mod.THelper.Foo.call(this, 13);',
  22505. ' this.Fly(2);',
  22506. ' this.Fly(2);',
  22507. ' $mod.THelper.Foo.call(this, 1);',
  22508. ' $mod.THelper.Foo.call(this, 1);',
  22509. ' $mod.THelper.Foo.call(this, 14);',
  22510. ' this.Fly(2);',
  22511. ' this.Fly(2);',
  22512. ' $mod.p = rtl.createCallback(this, "Fly");',
  22513. ' return Result;',
  22514. ' };',
  22515. '});',
  22516. 'this.p = null;',
  22517. 'this.Obj = null;',
  22518. '']),
  22519. LinesToStr([ // $mod.$main
  22520. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22521. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22522. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22523. '$mod.Obj.Fly(2);',
  22524. '$mod.Obj.Fly(2);',
  22525. 'var $with = $mod.Obj;',
  22526. '$mod.THelper.Foo.call($with, 1);',
  22527. '$mod.THelper.Foo.call($with, 1);',
  22528. '$mod.THelper.Foo.call($with, 22);',
  22529. '$with.Fly(2);',
  22530. '$with.Fly(2);',
  22531. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22532. '']));
  22533. end;
  22534. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22535. begin
  22536. StartProgram(false);
  22537. Add([
  22538. '{$modeswitch externalclass}',
  22539. 'type',
  22540. ' TExtA = class external name ''ExtObj''',
  22541. ' procedure Run(w: word = 10);',
  22542. ' end;',
  22543. ' THelper = class helper for TExtA',
  22544. ' class procedure Fly;',
  22545. ' end;',
  22546. 'class procedure THelper.Fly;',
  22547. 'begin end;',
  22548. 'begin',
  22549. '']);
  22550. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22551. nHelperClassMethodForExtClassMustBeStatic);
  22552. ConvertProgram;
  22553. end;
  22554. procedure TTestModule.TestRecordHelper_ClassVar;
  22555. begin
  22556. StartProgram(false);
  22557. Add([
  22558. 'type',
  22559. ' TRec = record',
  22560. ' end;',
  22561. ' THelper = record helper for TRec',
  22562. ' const',
  22563. ' One = 1;',
  22564. ' Two: word = 2;',
  22565. ' class var',
  22566. ' Glob: word;',
  22567. ' function Foo(w: word): word;',
  22568. ' class function Bar(w: word): word; static;',
  22569. ' end;',
  22570. 'function THelper.foo(w: word): word;',
  22571. 'begin',
  22572. ' Result:=w;',
  22573. ' Two:=One+w;',
  22574. ' Glob:=Glob;',
  22575. ' Result:=Self.Glob;',
  22576. ' Self.Glob:=Self.Glob;',
  22577. ' with Self do Glob:=Glob;',
  22578. ' Self:=Self;',
  22579. 'end;',
  22580. 'class function THelper.bar(w: word): word;',
  22581. 'begin',
  22582. ' Result:=w;',
  22583. ' Two:=One;',
  22584. ' Glob:=Glob;',
  22585. 'end;',
  22586. 'var r: TRec;',
  22587. 'begin',
  22588. ' trec.two:=trec.one;',
  22589. ' trec.Glob:=trec.Glob;',
  22590. ' with trec do begin',
  22591. ' two:=one;',
  22592. ' Glob:=Glob;',
  22593. ' end;',
  22594. ' r.two:=r.one;',
  22595. ' r.Glob:=r.Glob;',
  22596. ' with r do begin',
  22597. ' two:=one;',
  22598. ' Glob:=Glob;',
  22599. ' end;',
  22600. '']);
  22601. ConvertProgram;
  22602. CheckSource('TestRecordHelper_ClassVar',
  22603. LinesToStr([ // statements
  22604. 'rtl.recNewT(this, "TRec", function () {',
  22605. ' this.$eq = function (b) {',
  22606. ' return true;',
  22607. ' };',
  22608. ' this.$assign = function (s) {',
  22609. ' return this;',
  22610. ' };',
  22611. '});',
  22612. 'rtl.createHelper(this, "THelper", null, function () {',
  22613. ' this.One = 1;',
  22614. ' this.Two = 2;',
  22615. ' this.Glob = 0;',
  22616. ' this.Foo = function (w) {',
  22617. ' var Result = 0;',
  22618. ' Result = w;',
  22619. ' $mod.THelper.Two = 1 + w;',
  22620. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22621. ' Result = $mod.THelper.Glob;',
  22622. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22623. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22624. ' this.$assign(this);',
  22625. ' return Result;',
  22626. ' };',
  22627. ' this.Bar = function (w) {',
  22628. ' var Result = 0;',
  22629. ' Result = w;',
  22630. ' $mod.THelper.Two = 1;',
  22631. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22632. ' return Result;',
  22633. ' };',
  22634. '});',
  22635. 'this.r = this.TRec.$new();',
  22636. '']),
  22637. LinesToStr([ // $mod.$main
  22638. '$mod.THelper.Two = 1;',
  22639. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22640. 'var $with = $mod.TRec;',
  22641. '$mod.THelper.Two = 1;',
  22642. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22643. '$mod.THelper.Two = 1;',
  22644. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22645. 'var $with1 = $mod.r;',
  22646. '$mod.THelper.Two = 1;',
  22647. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22648. '']));
  22649. end;
  22650. procedure TTestModule.TestRecordHelper_Method_Call;
  22651. begin
  22652. StartProgram(false);
  22653. Add([
  22654. '{$modeswitch AdvancedRecords}',
  22655. 'type',
  22656. ' TRec = record',
  22657. ' procedure Run(w: word = 10);',
  22658. ' end;',
  22659. ' THelper = record helper for TRec',
  22660. ' function Foo(w: word = 1): word;',
  22661. ' end;',
  22662. 'procedure TRec.Run(w: word);',
  22663. 'begin',
  22664. ' Foo;',
  22665. ' Foo();',
  22666. ' Foo(2);',
  22667. ' Self.Foo;',
  22668. ' Self.Foo();',
  22669. ' Self.Foo(3);',
  22670. ' with Self do begin',
  22671. ' Foo;',
  22672. ' Foo();',
  22673. ' Foo(4);',
  22674. ' end;',
  22675. 'end;',
  22676. 'function THelper.foo(w: word): word;',
  22677. 'begin',
  22678. ' Run;',
  22679. ' Run();',
  22680. ' Run(11);',
  22681. ' Foo;',
  22682. ' Foo();',
  22683. ' Foo(12);',
  22684. ' Self.Foo;',
  22685. ' Self.Foo();',
  22686. ' Self.Foo(13);',
  22687. ' with Self do begin',
  22688. ' Foo;',
  22689. ' Foo();',
  22690. ' Foo(14);',
  22691. ' end;',
  22692. 'end;',
  22693. 'var Rec: TRec;',
  22694. 'begin',
  22695. ' Rec.Foo;',
  22696. ' Rec.Foo();',
  22697. ' Rec.Foo(21);',
  22698. ' with Rec do begin',
  22699. ' Foo;',
  22700. ' Foo();',
  22701. ' Foo(22);',
  22702. ' end;',
  22703. '']);
  22704. ConvertProgram;
  22705. CheckSource('TestRecordHelper_Method_Call',
  22706. LinesToStr([ // statements
  22707. 'rtl.recNewT(this, "TRec", function () {',
  22708. ' this.$eq = function (b) {',
  22709. ' return true;',
  22710. ' };',
  22711. ' this.$assign = function (s) {',
  22712. ' return this;',
  22713. ' };',
  22714. ' this.Run = function (w) {',
  22715. ' $mod.THelper.Foo.call(this, 1);',
  22716. ' $mod.THelper.Foo.call(this, 1);',
  22717. ' $mod.THelper.Foo.call(this, 2);',
  22718. ' $mod.THelper.Foo.call(this, 1);',
  22719. ' $mod.THelper.Foo.call(this, 1);',
  22720. ' $mod.THelper.Foo.call(this, 3);',
  22721. ' $mod.THelper.Foo.call(this, 1);',
  22722. ' $mod.THelper.Foo.call(this, 1);',
  22723. ' $mod.THelper.Foo.call(this, 4);',
  22724. ' };',
  22725. '});',
  22726. 'rtl.createHelper(this, "THelper", null, function () {',
  22727. ' this.Foo = function (w) {',
  22728. ' var Result = 0;',
  22729. ' this.Run(10);',
  22730. ' this.Run(10);',
  22731. ' this.Run(11);',
  22732. ' $mod.THelper.Foo.call(this, 1);',
  22733. ' $mod.THelper.Foo.call(this, 1);',
  22734. ' $mod.THelper.Foo.call(this, 12);',
  22735. ' $mod.THelper.Foo.call(this, 1);',
  22736. ' $mod.THelper.Foo.call(this, 1);',
  22737. ' $mod.THelper.Foo.call(this, 13);',
  22738. ' $mod.THelper.Foo.call(this, 1);',
  22739. ' $mod.THelper.Foo.call(this, 1);',
  22740. ' $mod.THelper.Foo.call(this, 14);',
  22741. ' return Result;',
  22742. ' };',
  22743. '});',
  22744. 'this.Rec = this.TRec.$new();',
  22745. '']),
  22746. LinesToStr([ // $mod.$main
  22747. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22748. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22749. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22750. 'var $with = $mod.Rec;',
  22751. '$mod.THelper.Foo.call($with, 1);',
  22752. '$mod.THelper.Foo.call($with, 1);',
  22753. '$mod.THelper.Foo.call($with, 22);',
  22754. '']));
  22755. end;
  22756. procedure TTestModule.TestRecordHelper_Constructor;
  22757. begin
  22758. StartProgram(false);
  22759. Add([
  22760. '{$modeswitch AdvancedRecords}',
  22761. 'type',
  22762. ' TRec = record',
  22763. ' constructor Create(w: word);',
  22764. ' end;',
  22765. ' THelper = record helper for TRec',
  22766. ' constructor NewHlp(w: word);',
  22767. ' end;',
  22768. 'var',
  22769. ' Rec: TRec;',
  22770. 'constructor TRec.Create(w: word);',
  22771. 'begin',
  22772. ' NewHlp(2);', // normal call
  22773. ' trec.NewHlp(3);', // new instance
  22774. 'end;',
  22775. 'constructor THelper.NewHlp(w: word);',
  22776. 'begin',
  22777. ' create(2);', // normal call
  22778. ' trec.create(3);', // new instance
  22779. ' NewHlp(4);', // normal call
  22780. ' trec.NewHlp(5);', // new instance
  22781. 'end;',
  22782. 'begin',
  22783. ' rec.newhlp(2);', // normal call
  22784. ' with rec do newhlp(12);', // normal call
  22785. ' trec.newhlp(3);', // new instance
  22786. ' with trec do newhlp(13);', // new instance
  22787. '']);
  22788. ConvertProgram;
  22789. CheckSource('TestRecordHelper_Constructor',
  22790. LinesToStr([ // statements
  22791. 'rtl.recNewT(this, "TRec", function () {',
  22792. ' this.$eq = function (b) {',
  22793. ' return true;',
  22794. ' };',
  22795. ' this.$assign = function (s) {',
  22796. ' return this;',
  22797. ' };',
  22798. ' this.Create = function (w) {',
  22799. ' $mod.THelper.NewHlp.call(this, 2);',
  22800. ' $mod.THelper.$new("NewHlp", [3]);',
  22801. ' return this;',
  22802. ' };',
  22803. '});',
  22804. 'rtl.createHelper(this, "THelper", null, function () {',
  22805. ' this.NewHlp = function (w) {',
  22806. ' this.Create(2);',
  22807. ' $mod.TRec.$new().Create(3);',
  22808. ' $mod.THelper.NewHlp.call(this, 4);',
  22809. ' $mod.THelper.$new("NewHlp", [5]);',
  22810. ' return this;',
  22811. ' };',
  22812. ' this.$new = function (fn, args) {',
  22813. ' return this[fn].apply($mod.TRec.$new(), args);',
  22814. ' };',
  22815. '});',
  22816. 'this.Rec = this.TRec.$new();',
  22817. '']),
  22818. LinesToStr([ // $mod.$main
  22819. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22820. 'var $with = $mod.Rec;',
  22821. '$mod.THelper.NewHlp.call($with, 12);',
  22822. '$mod.THelper.$new("NewHlp", [3]);',
  22823. 'var $with1 = $mod.TRec;',
  22824. '$mod.THelper.$new("NewHlp", [13]);',
  22825. '']));
  22826. end;
  22827. procedure TTestModule.TestTypeHelper_ClassVar;
  22828. begin
  22829. StartProgram(false);
  22830. Add([
  22831. '{$modeswitch typehelpers}',
  22832. 'type',
  22833. ' THelper = type helper for byte',
  22834. ' const',
  22835. ' One = 1;',
  22836. ' Two: word = 2;',
  22837. ' class var',
  22838. ' Glob: word;',
  22839. ' function Foo(w: word): word;',
  22840. ' class function Bar(w: word): word; static;',
  22841. ' end;',
  22842. 'function THelper.foo(w: word): word;',
  22843. 'begin',
  22844. ' Result:=w;',
  22845. ' Two:=One+w;',
  22846. ' Glob:=Glob;',
  22847. ' Result:=Self.Glob;',
  22848. ' Self.Glob:=Self.Glob;',
  22849. ' with Self do Glob:=Glob;',
  22850. 'end;',
  22851. 'class function THelper.bar(w: word): word;',
  22852. 'begin',
  22853. ' Result:=w;',
  22854. ' Two:=One;',
  22855. ' Glob:=Glob;',
  22856. 'end;',
  22857. 'var b: byte;',
  22858. 'begin',
  22859. ' byte.two:=byte.one;',
  22860. ' byte.Glob:=byte.Glob;',
  22861. ' with byte do begin',
  22862. ' two:=one;',
  22863. ' Glob:=Glob;',
  22864. ' end;',
  22865. ' b.two:=b.one;',
  22866. ' b.Glob:=b.Glob;',
  22867. ' with b do begin',
  22868. ' two:=one;',
  22869. ' Glob:=Glob;',
  22870. ' end;',
  22871. '']);
  22872. ConvertProgram;
  22873. CheckSource('TestTypeHelper_ClassVar',
  22874. LinesToStr([ // statements
  22875. 'rtl.createHelper(this, "THelper", null, function () {',
  22876. ' this.One = 1;',
  22877. ' this.Two = 2;',
  22878. ' this.Glob = 0;',
  22879. ' this.Foo = function (w) {',
  22880. ' var Result = 0;',
  22881. ' Result = w;',
  22882. ' $mod.THelper.Two = 1 + w;',
  22883. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22884. ' Result = $mod.THelper.Glob;',
  22885. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22886. ' var $with = this.get();',
  22887. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22888. ' return Result;',
  22889. ' };',
  22890. ' this.Bar = function (w) {',
  22891. ' var Result = 0;',
  22892. ' Result = w;',
  22893. ' $mod.THelper.Two = 1;',
  22894. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22895. ' return Result;',
  22896. ' };',
  22897. '});',
  22898. 'this.b = 0;',
  22899. '']),
  22900. LinesToStr([ // $mod.$main
  22901. '$mod.THelper.Two = 1;',
  22902. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22903. '$mod.THelper.Two = 1;',
  22904. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22905. '$mod.THelper.Two = 1;',
  22906. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22907. 'var $with = $mod.b;',
  22908. '$mod.THelper.Two = 1;',
  22909. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22910. '']));
  22911. end;
  22912. procedure TTestModule.TestTypeHelper_PassResultElement;
  22913. begin
  22914. StartProgram(false);
  22915. Add([
  22916. '{$modeswitch typehelpers}',
  22917. 'type',
  22918. ' THelper = type helper for word',
  22919. ' procedure DoIt(e: byte = 123);',
  22920. ' class procedure DoSome(e: byte = 456); static;',
  22921. ' end;',
  22922. 'procedure THelper.DoIt(e: byte);',
  22923. 'begin',
  22924. 'end;',
  22925. 'class procedure THelper.DoSome(e: byte);',
  22926. 'begin',
  22927. 'end;',
  22928. 'function Foo(w: word): word;',
  22929. 'begin',
  22930. ' Result.DoIt;',
  22931. ' Result.DoIt();',
  22932. ' Result.DoSome;',
  22933. ' Result.DoSome();',
  22934. ' with Result do begin',
  22935. ' DoIt;',
  22936. ' DoIt();',
  22937. ' DoSome;',
  22938. ' DoSome();',
  22939. ' end;',
  22940. 'end;',
  22941. 'begin',
  22942. '']);
  22943. ConvertProgram;
  22944. CheckSource('TestTypeHelper_PassResultElement',
  22945. LinesToStr([ // statements
  22946. 'rtl.createHelper(this, "THelper", null, function () {',
  22947. ' this.DoIt = function (e) {',
  22948. ' };',
  22949. ' this.DoSome = function (e) {',
  22950. ' };',
  22951. '});',
  22952. 'this.Foo = function (w) {',
  22953. ' var Result = 0;',
  22954. ' $mod.THelper.DoIt.call({',
  22955. ' get: function () {',
  22956. ' return Result;',
  22957. ' },',
  22958. ' set: function (v) {',
  22959. ' Result = v;',
  22960. ' }',
  22961. ' }, 123);',
  22962. ' $mod.THelper.DoIt.call({',
  22963. ' get: function () {',
  22964. ' return Result;',
  22965. ' },',
  22966. ' set: function (v) {',
  22967. ' Result = v;',
  22968. ' }',
  22969. ' }, 123);',
  22970. ' $mod.THelper.DoSome(456);',
  22971. ' $mod.THelper.DoSome(456);',
  22972. ' $mod.THelper.DoIt.call({',
  22973. ' get: function () {',
  22974. ' return Result;',
  22975. ' },',
  22976. ' set: function (v) {',
  22977. ' Result = v;',
  22978. ' }',
  22979. ' }, 123);',
  22980. ' $mod.THelper.DoIt.call({',
  22981. ' get: function () {',
  22982. ' return Result;',
  22983. ' },',
  22984. ' set: function (v) {',
  22985. ' Result = v;',
  22986. ' }',
  22987. ' }, 123);',
  22988. ' $mod.THelper.DoSome(456);',
  22989. ' $mod.THelper.DoSome(456);',
  22990. ' return Result;',
  22991. '};',
  22992. '']),
  22993. LinesToStr([ // $mod.$main
  22994. '']));
  22995. end;
  22996. procedure TTestModule.TestTypeHelper_PassArgs;
  22997. begin
  22998. StartProgram(false);
  22999. Add([
  23000. '{$modeswitch typehelpers}',
  23001. 'type',
  23002. ' THelper = type helper for word',
  23003. ' procedure DoIt(e: byte = 123);',
  23004. ' end;',
  23005. 'procedure THelper.DoIt(e: byte);',
  23006. 'begin',
  23007. 'end;',
  23008. 'procedure FooDefault(a: word);',
  23009. 'begin',
  23010. ' a.DoIt;',
  23011. ' with a do DoIt;',
  23012. 'end;',
  23013. 'procedure FooConst(const a: word);',
  23014. 'begin',
  23015. ' a.DoIt;',
  23016. ' with a do DoIt;',
  23017. 'end;',
  23018. 'procedure FooVar(var a: word);',
  23019. 'begin',
  23020. ' a.DoIt;',
  23021. ' with a do DoIt;',
  23022. 'end;',
  23023. 'begin',
  23024. '']);
  23025. ConvertProgram;
  23026. CheckSource('TestTypeHelper_PassArgs',
  23027. LinesToStr([ // statements
  23028. 'rtl.createHelper(this, "THelper", null, function () {',
  23029. ' this.DoIt = function (e) {',
  23030. ' };',
  23031. '});',
  23032. 'this.FooDefault = function (a) {',
  23033. ' $mod.THelper.DoIt.call({',
  23034. ' get: function () {',
  23035. ' return a;',
  23036. ' },',
  23037. ' set: function (v) {',
  23038. ' a = v;',
  23039. ' }',
  23040. ' }, 123);',
  23041. ' $mod.THelper.DoIt.call({',
  23042. ' get: function () {',
  23043. ' return a;',
  23044. ' },',
  23045. ' set: function (v) {',
  23046. ' a = v;',
  23047. ' }',
  23048. ' }, 123);',
  23049. '};',
  23050. 'this.FooConst = function (a) {',
  23051. ' $mod.THelper.DoIt.call({',
  23052. ' get: function () {',
  23053. ' return a;',
  23054. ' },',
  23055. ' set: function (v) {',
  23056. ' rtl.raiseE("EPropReadOnly");',
  23057. ' }',
  23058. ' }, 123);',
  23059. ' $mod.THelper.DoIt.call({',
  23060. ' get: function () {',
  23061. ' return a;',
  23062. ' },',
  23063. ' set: function () {',
  23064. ' rtl.raiseE("EPropReadOnly");',
  23065. ' }',
  23066. ' }, 123);',
  23067. '};',
  23068. 'this.FooVar = function (a) {',
  23069. ' $mod.THelper.DoIt.call(a, 123);',
  23070. ' var $with = a.get();',
  23071. ' $mod.THelper.DoIt.call(a, 123);',
  23072. '};',
  23073. '']),
  23074. LinesToStr([ // $mod.$main
  23075. '']));
  23076. end;
  23077. procedure TTestModule.TestTypeHelper_PassVarConst;
  23078. begin
  23079. StartProgram(false);
  23080. Add([
  23081. '{$modeswitch typehelpers}',
  23082. 'type',
  23083. ' THelper = type helper for word',
  23084. ' procedure DoIt(e: byte = 123);',
  23085. ' end;',
  23086. 'procedure THelper.DoIt(e: byte);',
  23087. 'begin',
  23088. 'end;',
  23089. 'var a: word;',
  23090. 'const c: word = 2;',
  23091. '{$writeableconst off}',
  23092. 'const r: word = 3;',
  23093. 'begin',
  23094. ' a.DoIt;',
  23095. ' with a do DoIt;',
  23096. ' c.DoIt;',
  23097. ' with c do DoIt;',
  23098. ' r.DoIt;',
  23099. ' with r do DoIt;',
  23100. '']);
  23101. ConvertProgram;
  23102. CheckSource('TestTypeHelper_PassVarConst',
  23103. LinesToStr([ // statements
  23104. 'rtl.createHelper(this, "THelper", null, function () {',
  23105. ' this.DoIt = function (e) {',
  23106. ' };',
  23107. '});',
  23108. 'this.a = 0;',
  23109. 'this.c = 2;',
  23110. 'this.r = 3;',
  23111. '']),
  23112. LinesToStr([ // $mod.$main
  23113. '$mod.THelper.DoIt.call({',
  23114. ' p: $mod,',
  23115. ' get: function () {',
  23116. ' return this.p.a;',
  23117. ' },',
  23118. ' set: function (v) {',
  23119. ' this.p.a = v;',
  23120. ' }',
  23121. '}, 123);',
  23122. 'var $with = $mod.a;',
  23123. '$mod.THelper.DoIt.call({',
  23124. ' get: function () {',
  23125. ' return $with;',
  23126. ' },',
  23127. ' set: function (v) {',
  23128. ' $with = v;',
  23129. ' }',
  23130. '}, 123);',
  23131. '$mod.THelper.DoIt.call({',
  23132. ' p: $mod,',
  23133. ' get: function () {',
  23134. ' return this.p.c;',
  23135. ' },',
  23136. ' set: function (v) {',
  23137. ' this.p.c = v;',
  23138. ' }',
  23139. '}, 123);',
  23140. 'var $with1 = $mod.c;',
  23141. '$mod.THelper.DoIt.call({',
  23142. ' get: function () {',
  23143. ' return $with1;',
  23144. ' },',
  23145. ' set: function (v) {',
  23146. ' $with1 = v;',
  23147. ' }',
  23148. '}, 123);',
  23149. '$mod.THelper.DoIt.call({',
  23150. ' get: function () {',
  23151. ' return 3;',
  23152. ' },',
  23153. ' set: function (v) {',
  23154. ' rtl.raiseE("EPropReadOnly");',
  23155. ' }',
  23156. '}, 123);',
  23157. 'var $with2 = 3;',
  23158. ' $mod.THelper.DoIt.call({',
  23159. ' get: function () {',
  23160. ' return $with2;',
  23161. ' },',
  23162. ' set: function () {',
  23163. ' rtl.raiseE("EPropReadOnly");',
  23164. ' }',
  23165. ' }, 123);',
  23166. '']));
  23167. end;
  23168. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23169. begin
  23170. StartProgram(false);
  23171. Add([
  23172. '{$modeswitch typehelpers}',
  23173. 'type',
  23174. ' THelper = type helper for word',
  23175. ' procedure DoIt(e: byte = 123);',
  23176. ' end;',
  23177. 'procedure THelper.DoIt(e: byte);',
  23178. 'begin',
  23179. 'end;',
  23180. 'function Foo(b: byte = 1): word;',
  23181. 'begin',
  23182. 'end;',
  23183. 'begin',
  23184. ' Foo.DoIt;',
  23185. ' Foo().DoIt;',
  23186. ' with Foo do DoIt;',
  23187. ' with Foo() do DoIt;',
  23188. '']);
  23189. ConvertProgram;
  23190. CheckSource('TestTypeHelper_PassFuncResult',
  23191. LinesToStr([ // statements
  23192. 'rtl.createHelper(this, "THelper", null, function () {',
  23193. ' this.DoIt = function (e) {',
  23194. ' };',
  23195. '});',
  23196. 'this.Foo = function (b) {',
  23197. ' var Result = 0;',
  23198. ' return Result;',
  23199. '};',
  23200. '']),
  23201. LinesToStr([ // $mod.$main
  23202. '$mod.THelper.DoIt.call({',
  23203. ' a: $mod.Foo(1),',
  23204. ' get: function () {',
  23205. ' return this.a;',
  23206. ' },',
  23207. ' set: function (v) {',
  23208. ' this.a = v;',
  23209. ' }',
  23210. '}, 123);',
  23211. '$mod.THelper.DoIt.call({',
  23212. ' a: $mod.Foo(1),',
  23213. ' get: function () {',
  23214. ' return this.a;',
  23215. ' },',
  23216. ' set: function (v) {',
  23217. ' this.a = v;',
  23218. ' }',
  23219. '}, 123);',
  23220. 'var $with = $mod.Foo(1);',
  23221. '$mod.THelper.DoIt.call({',
  23222. ' get: function () {',
  23223. ' return $with;',
  23224. ' },',
  23225. ' set: function (v) {',
  23226. ' $with = v;',
  23227. ' }',
  23228. '}, 123);',
  23229. 'var $with1 = $mod.Foo(1);',
  23230. '$mod.THelper.DoIt.call({',
  23231. ' get: function () {',
  23232. ' return $with1;',
  23233. ' },',
  23234. ' set: function (v) {',
  23235. ' $with1 = v;',
  23236. ' }',
  23237. '}, 123);',
  23238. '']));
  23239. end;
  23240. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23241. begin
  23242. StartProgram(false);
  23243. Add([
  23244. '{$modeswitch typehelpers}',
  23245. 'type',
  23246. ' TObject = class',
  23247. ' FField: word;',
  23248. ' procedure SetField(Value: word);',
  23249. ' property Field: word read FField write SetField;',
  23250. ' end;',
  23251. ' THelper = type helper for word',
  23252. ' procedure Fly;',
  23253. ' class procedure Run; static;',
  23254. ' end;',
  23255. 'procedure TObject.SetField(Value: word);',
  23256. 'begin',
  23257. ' Field.Fly;',
  23258. ' Field.Run;',
  23259. ' Self.Field.Fly;',
  23260. ' Self.Field.Run;',
  23261. ' with Self do begin',
  23262. ' Field.Fly;',
  23263. ' Field.Run;',
  23264. ' end;',
  23265. ' with Self.Field do begin',
  23266. ' Fly;',
  23267. ' Run;',
  23268. ' end;',
  23269. 'end;',
  23270. 'procedure THelper.Fly;',
  23271. 'begin',
  23272. 'end;',
  23273. 'class procedure THelper.Run;',
  23274. 'begin',
  23275. 'end;',
  23276. 'var',
  23277. ' o: TObject;',
  23278. 'begin',
  23279. ' o.Field.Fly;',
  23280. ' o.Field.Run;',
  23281. ' with o do begin',
  23282. ' Field.Fly;',
  23283. ' Field.Run;',
  23284. ' end;',
  23285. ' with o.Field do begin',
  23286. ' Fly;',
  23287. ' Run;',
  23288. ' end;',
  23289. '']);
  23290. ConvertProgram;
  23291. CheckSource('TestTypeHelper_PassPropertyField',
  23292. LinesToStr([ // statements
  23293. 'rtl.createClass(this, "TObject", null, function () {',
  23294. ' this.$init = function () {',
  23295. ' this.FField = 0;',
  23296. ' };',
  23297. ' this.$final = function () {',
  23298. ' };',
  23299. ' this.SetField = function (Value) {',
  23300. ' $mod.THelper.Fly.call({',
  23301. ' p: this,',
  23302. ' get: function () {',
  23303. ' return this.p.FField;',
  23304. ' },',
  23305. ' set: function (v) {',
  23306. ' this.p.FField = v;',
  23307. ' }',
  23308. ' });',
  23309. ' $mod.THelper.Run();',
  23310. ' $mod.THelper.Fly.call({',
  23311. ' p: this,',
  23312. ' get: function () {',
  23313. ' return this.p.FField;',
  23314. ' },',
  23315. ' set: function (v) {',
  23316. ' this.p.FField = v;',
  23317. ' }',
  23318. ' });',
  23319. ' $mod.THelper.Run();',
  23320. ' $mod.THelper.Fly.call({',
  23321. ' p: this,',
  23322. ' get: function () {',
  23323. ' return this.p.FField;',
  23324. ' },',
  23325. ' set: function (v) {',
  23326. ' this.p.FField = v;',
  23327. ' }',
  23328. ' });',
  23329. ' $mod.THelper.Run();',
  23330. ' var $with = this.FField;',
  23331. ' $mod.THelper.Fly.call({',
  23332. ' get: function () {',
  23333. ' return $with;',
  23334. ' },',
  23335. ' set: function (v) {',
  23336. ' $with = v;',
  23337. ' }',
  23338. ' });',
  23339. ' $mod.THelper.Run();',
  23340. ' };',
  23341. '});',
  23342. 'rtl.createHelper(this, "THelper", null, function () {',
  23343. ' this.Fly = function () {',
  23344. ' };',
  23345. ' this.Run = function () {',
  23346. ' };',
  23347. '});',
  23348. 'this.o = null;',
  23349. '']),
  23350. LinesToStr([ // $mod.$main
  23351. '$mod.THelper.Fly.call({',
  23352. ' p: $mod.o,',
  23353. ' get: function () {',
  23354. ' return this.p.FField;',
  23355. ' },',
  23356. ' set: function (v) {',
  23357. ' this.p.FField = v;',
  23358. ' }',
  23359. '});',
  23360. '$mod.THelper.Run();',
  23361. 'var $with = $mod.o;',
  23362. '$mod.THelper.Fly.call({',
  23363. ' p: $with,',
  23364. ' get: function () {',
  23365. ' return this.p.FField;',
  23366. ' },',
  23367. ' set: function (v) {',
  23368. ' this.p.FField = v;',
  23369. ' }',
  23370. '});',
  23371. '$mod.THelper.Run();',
  23372. 'var $with1 = $mod.o.FField;',
  23373. '$mod.THelper.Fly.call({',
  23374. ' get: function () {',
  23375. ' return $with1;',
  23376. ' },',
  23377. ' set: function (v) {',
  23378. ' $with1 = v;',
  23379. ' }',
  23380. '});',
  23381. '$mod.THelper.Run();',
  23382. '']));
  23383. end;
  23384. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23385. begin
  23386. StartProgram(false);
  23387. Add([
  23388. '{$modeswitch typehelpers}',
  23389. 'type',
  23390. ' TObject = class',
  23391. ' FField: word;',
  23392. ' function GetField: word;',
  23393. ' property Field: word read GetField write FField;',
  23394. ' end;',
  23395. ' THelper = type helper for word',
  23396. ' procedure Fly;',
  23397. ' class procedure Run; static;',
  23398. ' end;',
  23399. 'function TObject.GetField: word;',
  23400. 'begin',
  23401. ' Field.Fly;',
  23402. ' Field.Run;',
  23403. ' Self.Field.Fly;',
  23404. ' Self.Field.Run;',
  23405. ' with Self do begin',
  23406. ' Field.Fly;',
  23407. ' Field.Run;',
  23408. ' end;',
  23409. ' with Self.Field do begin',
  23410. ' Fly;',
  23411. ' Run;',
  23412. ' end;',
  23413. 'end;',
  23414. 'procedure THelper.Fly;',
  23415. 'begin',
  23416. 'end;',
  23417. 'class procedure THelper.Run;',
  23418. 'begin',
  23419. 'end;',
  23420. 'var',
  23421. ' o: TObject;',
  23422. 'begin',
  23423. ' o.Field.Fly;',
  23424. ' o.Field.Run;',
  23425. ' with o do begin',
  23426. ' Field.Fly;',
  23427. ' Field.Run;',
  23428. ' end;',
  23429. ' with o.Field do begin',
  23430. ' Fly;',
  23431. ' Run;',
  23432. ' end;',
  23433. '']);
  23434. ConvertProgram;
  23435. CheckSource('TestTypeHelper_PassPropertyGetter',
  23436. LinesToStr([ // statements
  23437. 'rtl.createClass(this, "TObject", null, function () {',
  23438. ' this.$init = function () {',
  23439. ' this.FField = 0;',
  23440. ' };',
  23441. ' this.$final = function () {',
  23442. ' };',
  23443. ' this.GetField = function () {',
  23444. ' var Result = 0;',
  23445. ' $mod.THelper.Fly.call({',
  23446. ' p: this.GetField(),',
  23447. ' get: function () {',
  23448. ' return this.p;',
  23449. ' },',
  23450. ' set: function (v) {',
  23451. ' this.p = v;',
  23452. ' }',
  23453. ' });',
  23454. ' $mod.THelper.Run();',
  23455. ' $mod.THelper.Fly.call({',
  23456. ' p: this.GetField(),',
  23457. ' get: function () {',
  23458. ' return this.p;',
  23459. ' },',
  23460. ' set: function (v) {',
  23461. ' this.p = v;',
  23462. ' }',
  23463. ' });',
  23464. ' $mod.THelper.Run();',
  23465. ' $mod.THelper.Fly.call({',
  23466. ' p: this.GetField(),',
  23467. ' get: function () {',
  23468. ' return this.p;',
  23469. ' },',
  23470. ' set: function (v) {',
  23471. ' this.p = v;',
  23472. ' }',
  23473. ' });',
  23474. ' $mod.THelper.Run();',
  23475. ' var $with = this.GetField();',
  23476. ' $mod.THelper.Fly.call({',
  23477. ' get: function () {',
  23478. ' return $with;',
  23479. ' },',
  23480. ' set: function (v) {',
  23481. ' $with = v;',
  23482. ' }',
  23483. ' });',
  23484. ' $mod.THelper.Run();',
  23485. ' return Result;',
  23486. ' };',
  23487. '});',
  23488. 'rtl.createHelper(this, "THelper", null, function () {',
  23489. ' this.Fly = function () {',
  23490. ' };',
  23491. ' this.Run = function () {',
  23492. ' };',
  23493. '});',
  23494. 'this.o = null;',
  23495. '']),
  23496. LinesToStr([ // $mod.$main
  23497. '$mod.THelper.Fly.call({',
  23498. ' p: $mod.o.GetField(),',
  23499. ' get: function () {',
  23500. ' return this.p;',
  23501. ' },',
  23502. ' set: function (v) {',
  23503. ' this.p = v;',
  23504. ' }',
  23505. '});',
  23506. '$mod.THelper.Run();',
  23507. 'var $with = $mod.o;',
  23508. '$mod.THelper.Fly.call({',
  23509. ' p: $with.GetField(),',
  23510. ' get: function () {',
  23511. ' return this.p;',
  23512. ' },',
  23513. ' set: function (v) {',
  23514. ' this.p = v;',
  23515. ' }',
  23516. '});',
  23517. '$mod.THelper.Run();',
  23518. 'var $with1 = $mod.o.GetField();',
  23519. '$mod.THelper.Fly.call({',
  23520. ' get: function () {',
  23521. ' return $with1;',
  23522. ' },',
  23523. ' set: function (v) {',
  23524. ' $with1 = v;',
  23525. ' }',
  23526. '});',
  23527. '$mod.THelper.Run();',
  23528. '']));
  23529. end;
  23530. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23531. begin
  23532. StartProgram(false);
  23533. Add([
  23534. '{$modeswitch typehelpers}',
  23535. 'type',
  23536. ' TObject = class',
  23537. ' class var FField: word;',
  23538. ' class procedure SetField(Value: word);',
  23539. ' class property Field: word read FField write SetField;',
  23540. ' end;',
  23541. ' THelper = type helper for word',
  23542. ' procedure Fly(n: byte);',
  23543. ' end;',
  23544. 'class procedure TObject.SetField(Value: word);',
  23545. 'begin',
  23546. ' Field.Fly(1);',
  23547. ' Self.Field.Fly(2);',
  23548. ' with Self do Field.Fly(3);',
  23549. ' with Self.Field do Fly(4);',
  23550. ' TObject.Field.Fly(5);',
  23551. ' with TObject do Field.Fly(6);',
  23552. ' with TObject.Field do Fly(7);',
  23553. 'end;',
  23554. 'procedure THelper.Fly(n: byte);',
  23555. 'begin',
  23556. 'end;',
  23557. 'var',
  23558. ' o: TObject;',
  23559. 'begin',
  23560. ' o.Field.Fly(11);',
  23561. ' with o do Field.Fly(12);',
  23562. ' with o.Field do Fly(13);',
  23563. ' TObject.Field.Fly(14);',
  23564. ' with TObject do Field.Fly(15);',
  23565. ' with TObject.Field do Fly(16);',
  23566. '']);
  23567. ConvertProgram;
  23568. CheckSource('TestTypeHelper_PassClassPropertyField',
  23569. LinesToStr([ // statements
  23570. 'rtl.createClass(this, "TObject", null, function () {',
  23571. ' this.FField = 0;',
  23572. ' this.$init = function () {',
  23573. ' };',
  23574. ' this.$final = function () {',
  23575. ' };',
  23576. ' this.SetField = function (Value) {',
  23577. ' $mod.THelper.Fly.call({',
  23578. ' p: this,',
  23579. ' get: function () {',
  23580. ' return this.p.FField;',
  23581. ' },',
  23582. ' set: function (v) {',
  23583. ' $mod.TObject.FField = v;',
  23584. ' }',
  23585. ' }, 1);',
  23586. ' $mod.THelper.Fly.call({',
  23587. ' p: this,',
  23588. ' get: function () {',
  23589. ' return this.p.FField;',
  23590. ' },',
  23591. ' set: function (v) {',
  23592. ' $mod.TObject.FField = v;',
  23593. ' }',
  23594. ' }, 2);',
  23595. ' $mod.THelper.Fly.call({',
  23596. ' p: this,',
  23597. ' get: function () {',
  23598. ' return this.p.FField;',
  23599. ' },',
  23600. ' set: function (v) {',
  23601. ' $mod.TObject.FField = v;',
  23602. ' }',
  23603. ' }, 3);',
  23604. ' var $with = this.FField;',
  23605. ' $mod.THelper.Fly.call({',
  23606. ' get: function () {',
  23607. ' return $with;',
  23608. ' },',
  23609. ' set: function (v) {',
  23610. ' $with = v;',
  23611. ' }',
  23612. ' }, 4);',
  23613. ' $mod.THelper.Fly.call({',
  23614. ' p: $mod.TObject,',
  23615. ' get: function () {',
  23616. ' return this.p.FField;',
  23617. ' },',
  23618. ' set: function (v) {',
  23619. ' $mod.TObject.FField = v;',
  23620. ' }',
  23621. ' }, 5);',
  23622. ' var $with1 = $mod.TObject;',
  23623. ' $mod.THelper.Fly.call({',
  23624. ' p: $with1,',
  23625. ' get: function () {',
  23626. ' return this.p.FField;',
  23627. ' },',
  23628. ' set: function (v) {',
  23629. ' $mod.TObject.FField = v;',
  23630. ' }',
  23631. ' }, 6);',
  23632. ' var $with2 = $mod.TObject.FField;',
  23633. ' $mod.THelper.Fly.call({',
  23634. ' get: function () {',
  23635. ' return $with2;',
  23636. ' },',
  23637. ' set: function (v) {',
  23638. ' $with2 = v;',
  23639. ' }',
  23640. ' }, 7);',
  23641. ' };',
  23642. '});',
  23643. 'rtl.createHelper(this, "THelper", null, function () {',
  23644. ' this.Fly = function (n) {',
  23645. ' };',
  23646. '});',
  23647. 'this.o = null;',
  23648. '']),
  23649. LinesToStr([ // $mod.$main
  23650. '$mod.THelper.Fly.call({',
  23651. ' p: $mod.o,',
  23652. ' get: function () {',
  23653. ' return this.p.FField;',
  23654. ' },',
  23655. ' set: function (v) {',
  23656. ' $mod.TObject.FField = v;',
  23657. ' }',
  23658. '}, 11);',
  23659. 'var $with = $mod.o;',
  23660. '$mod.THelper.Fly.call({',
  23661. ' p: $with,',
  23662. ' get: function () {',
  23663. ' return this.p.FField;',
  23664. ' },',
  23665. ' set: function (v) {',
  23666. ' $mod.TObject.FField = v;',
  23667. ' }',
  23668. '}, 12);',
  23669. 'var $with1 = $mod.o.FField;',
  23670. '$mod.THelper.Fly.call({',
  23671. ' get: function () {',
  23672. ' return $with1;',
  23673. ' },',
  23674. ' set: function (v) {',
  23675. ' $with1 = v;',
  23676. ' }',
  23677. '}, 13);',
  23678. '$mod.THelper.Fly.call({',
  23679. ' p: $mod.TObject,',
  23680. ' get: function () {',
  23681. ' return this.p.FField;',
  23682. ' },',
  23683. ' set: function (v) {',
  23684. ' $mod.TObject.FField = v;',
  23685. ' }',
  23686. '}, 14);',
  23687. 'var $with2 = $mod.TObject;',
  23688. '$mod.THelper.Fly.call({',
  23689. ' p: $with2,',
  23690. ' get: function () {',
  23691. ' return this.p.FField;',
  23692. ' },',
  23693. ' set: function (v) {',
  23694. ' $mod.TObject.FField = v;',
  23695. ' }',
  23696. '}, 15);',
  23697. 'var $with3 = $mod.TObject.FField;',
  23698. '$mod.THelper.Fly.call({',
  23699. ' get: function () {',
  23700. ' return $with3;',
  23701. ' },',
  23702. ' set: function (v) {',
  23703. ' $with3 = v;',
  23704. ' }',
  23705. '}, 16);',
  23706. '']));
  23707. end;
  23708. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23709. begin
  23710. StartProgram(false);
  23711. Add([
  23712. '{$modeswitch typehelpers}',
  23713. 'type',
  23714. ' TObject = class',
  23715. ' class var FField: word;',
  23716. ' class function GetField: word; static;',
  23717. ' class property Field: word read GetField write FField;',
  23718. ' end;',
  23719. ' THelper = type helper for word',
  23720. ' procedure Fly(n: byte);',
  23721. ' end;',
  23722. 'class function TObject.GetField: word;',
  23723. 'begin',
  23724. ' Field.Fly(1);',
  23725. ' TObject.Field.Fly(5);',
  23726. ' with TObject do Field.Fly(6);',
  23727. ' with TObject.Field do Fly(7);',
  23728. 'end;',
  23729. 'procedure THelper.Fly(n: byte);',
  23730. 'begin',
  23731. 'end;',
  23732. 'var',
  23733. ' o: TObject;',
  23734. 'begin',
  23735. ' o.Field.Fly(11);',
  23736. ' with o do Field.Fly(12);',
  23737. ' with o.Field do Fly(13);',
  23738. '']);
  23739. ConvertProgram;
  23740. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23741. LinesToStr([ // statements
  23742. 'rtl.createClass(this, "TObject", null, function () {',
  23743. ' this.FField = 0;',
  23744. ' this.$init = function () {',
  23745. ' };',
  23746. ' this.$final = function () {',
  23747. ' };',
  23748. ' this.GetField = function () {',
  23749. ' var Result = 0;',
  23750. ' $mod.THelper.Fly.call({',
  23751. ' p: $mod.TObject.GetField(),',
  23752. ' get: function () {',
  23753. ' return this.p;',
  23754. ' },',
  23755. ' set: function (v) {',
  23756. ' this.p = v;',
  23757. ' }',
  23758. ' }, 1);',
  23759. ' $mod.THelper.Fly.call({',
  23760. ' p: $mod.TObject.GetField(),',
  23761. ' get: function () {',
  23762. ' return this.p;',
  23763. ' },',
  23764. ' set: function (v) {',
  23765. ' this.p = v;',
  23766. ' }',
  23767. ' }, 5);',
  23768. ' var $with = $mod.TObject;',
  23769. ' $mod.THelper.Fly.call({',
  23770. ' p: $with.GetField(),',
  23771. ' get: function () {',
  23772. ' return this.p;',
  23773. ' },',
  23774. ' set: function (v) {',
  23775. ' this.p = v;',
  23776. ' }',
  23777. ' }, 6);',
  23778. ' var $with1 = $mod.TObject.GetField();',
  23779. ' $mod.THelper.Fly.call({',
  23780. ' get: function () {',
  23781. ' return $with1;',
  23782. ' },',
  23783. ' set: function (v) {',
  23784. ' $with1 = v;',
  23785. ' }',
  23786. ' }, 7);',
  23787. ' return Result;',
  23788. ' };',
  23789. '});',
  23790. 'rtl.createHelper(this, "THelper", null, function () {',
  23791. ' this.Fly = function (n) {',
  23792. ' };',
  23793. '});',
  23794. 'this.o = null;',
  23795. '']),
  23796. LinesToStr([ // $mod.$main
  23797. '$mod.THelper.Fly.call({',
  23798. ' p: $mod.o.GetField(),',
  23799. ' get: function () {',
  23800. ' return this.p;',
  23801. ' },',
  23802. ' set: function (v) {',
  23803. ' this.p = v;',
  23804. ' }',
  23805. '}, 11);',
  23806. 'var $with = $mod.o;',
  23807. '$mod.THelper.Fly.call({',
  23808. ' p: $with.GetField(),',
  23809. ' get: function () {',
  23810. ' return this.p;',
  23811. ' },',
  23812. ' set: function (v) {',
  23813. ' this.p = v;',
  23814. ' }',
  23815. '}, 12);',
  23816. 'var $with1 = $mod.o.GetField();',
  23817. '$mod.THelper.Fly.call({',
  23818. ' get: function () {',
  23819. ' return $with1;',
  23820. ' },',
  23821. ' set: function (v) {',
  23822. ' $with1 = v;',
  23823. ' }',
  23824. '}, 13);',
  23825. '']));
  23826. end;
  23827. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23828. begin
  23829. StartProgram(false);
  23830. Add([
  23831. '{$modeswitch typehelpers}',
  23832. 'type',
  23833. ' TObject = class',
  23834. ' class var FField: word;',
  23835. ' class function GetField: word;',
  23836. ' class property Field: word read GetField write FField;',
  23837. ' end;',
  23838. ' TClass = class of TObject;',
  23839. ' THelper = type helper for word',
  23840. ' procedure Fly(n: byte);',
  23841. ' end;',
  23842. 'class function TObject.GetField: word;',
  23843. 'begin',
  23844. ' Field.Fly(1);',
  23845. ' Self.Field.Fly(5);',
  23846. ' with Self do Field.Fly(6);',
  23847. ' with Self.Field do Fly(7);',
  23848. 'end;',
  23849. 'procedure THelper.Fly(n: byte);',
  23850. 'begin',
  23851. 'end;',
  23852. 'var',
  23853. ' o: TObject;',
  23854. ' c: TClass;',
  23855. 'begin',
  23856. ' o.Field.Fly(11);',
  23857. ' with o do Field.Fly(12);',
  23858. ' with o.Field do Fly(13);',
  23859. ' c.Field.Fly(14);',
  23860. ' with c do Field.Fly(15);',
  23861. ' with c.Field do Fly(16);',
  23862. '']);
  23863. ConvertProgram;
  23864. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23865. LinesToStr([ // statements
  23866. 'rtl.createClass(this, "TObject", null, function () {',
  23867. ' this.FField = 0;',
  23868. ' this.$init = function () {',
  23869. ' };',
  23870. ' this.$final = function () {',
  23871. ' };',
  23872. ' this.GetField = function () {',
  23873. ' var Result = 0;',
  23874. ' $mod.THelper.Fly.call({',
  23875. ' p: this.GetField(),',
  23876. ' get: function () {',
  23877. ' return this.p;',
  23878. ' },',
  23879. ' set: function (v) {',
  23880. ' this.p = v;',
  23881. ' }',
  23882. ' }, 1);',
  23883. ' $mod.THelper.Fly.call({',
  23884. ' p: this.GetField(),',
  23885. ' get: function () {',
  23886. ' return this.p;',
  23887. ' },',
  23888. ' set: function (v) {',
  23889. ' this.p = v;',
  23890. ' }',
  23891. ' }, 5);',
  23892. ' $mod.THelper.Fly.call({',
  23893. ' p: this.GetField(),',
  23894. ' get: function () {',
  23895. ' return this.p;',
  23896. ' },',
  23897. ' set: function (v) {',
  23898. ' this.p = v;',
  23899. ' }',
  23900. ' }, 6);',
  23901. ' var $with = this.GetField();',
  23902. ' $mod.THelper.Fly.call({',
  23903. ' get: function () {',
  23904. ' return $with;',
  23905. ' },',
  23906. ' set: function (v) {',
  23907. ' $with = v;',
  23908. ' }',
  23909. ' }, 7);',
  23910. ' return Result;',
  23911. ' };',
  23912. '});',
  23913. 'rtl.createHelper(this, "THelper", null, function () {',
  23914. ' this.Fly = function (n) {',
  23915. ' };',
  23916. '});',
  23917. 'this.o = null;',
  23918. 'this.c = null;',
  23919. '']),
  23920. LinesToStr([ // $mod.$main
  23921. '$mod.THelper.Fly.call({',
  23922. ' p: $mod.o.$class.GetField(),',
  23923. ' get: function () {',
  23924. ' return this.p;',
  23925. ' },',
  23926. ' set: function (v) {',
  23927. ' this.p = v;',
  23928. ' }',
  23929. '}, 11);',
  23930. 'var $with = $mod.o;',
  23931. '$mod.THelper.Fly.call({',
  23932. ' p: $with.$class.GetField(),',
  23933. ' get: function () {',
  23934. ' return this.p;',
  23935. ' },',
  23936. ' set: function (v) {',
  23937. ' this.p = v;',
  23938. ' }',
  23939. '}, 12);',
  23940. 'var $with1 = $mod.o.$class.GetField();',
  23941. '$mod.THelper.Fly.call({',
  23942. ' get: function () {',
  23943. ' return $with1;',
  23944. ' },',
  23945. ' set: function (v) {',
  23946. ' $with1 = v;',
  23947. ' }',
  23948. '}, 13);',
  23949. '$mod.THelper.Fly.call({',
  23950. ' p: $mod.c.GetField(),',
  23951. ' get: function () {',
  23952. ' return this.p;',
  23953. ' },',
  23954. ' set: function (v) {',
  23955. ' this.p = v;',
  23956. ' }',
  23957. '}, 14);',
  23958. 'var $with2 = $mod.c;',
  23959. '$mod.THelper.Fly.call({',
  23960. ' p: $with2.GetField(),',
  23961. ' get: function () {',
  23962. ' return this.p;',
  23963. ' },',
  23964. ' set: function (v) {',
  23965. ' this.p = v;',
  23966. ' }',
  23967. '}, 15);',
  23968. 'var $with3 = $mod.c.GetField();',
  23969. '$mod.THelper.Fly.call({',
  23970. ' get: function () {',
  23971. ' return $with3;',
  23972. ' },',
  23973. ' set: function (v) {',
  23974. ' $with3 = v;',
  23975. ' }',
  23976. '}, 16);',
  23977. '']));
  23978. end;
  23979. procedure TTestModule.TestTypeHelper_Property;
  23980. begin
  23981. StartProgram(false);
  23982. Add([
  23983. '{$modeswitch typehelpers}',
  23984. 'type',
  23985. ' THelper = type helper for word',
  23986. ' function GetSize: longint;',
  23987. ' procedure SetSize(Value: longint);',
  23988. ' property Size: longint read GetSize write SetSize;',
  23989. ' end;',
  23990. 'function THelper.GetSize: longint;',
  23991. 'begin',
  23992. ' Result:=Size+1;',
  23993. ' Size:=2;',
  23994. ' Result:=Self.Size+3;',
  23995. ' Self.Size:=4;',
  23996. ' with Self do begin',
  23997. ' Result:=Size+5;',
  23998. ' Size:=6;',
  23999. ' end;',
  24000. 'end;',
  24001. 'procedure THelper.SetSize(Value: longint);',
  24002. 'begin',
  24003. 'end;',
  24004. 'var w: word;',
  24005. 'begin',
  24006. ' w:=w.Size+7;',
  24007. ' w.Size:=w+8;',
  24008. ' with w do begin',
  24009. ' w:=Size+9;',
  24010. ' Size:=w+10;',
  24011. ' end;',
  24012. '']);
  24013. ConvertProgram;
  24014. CheckSource('TestTypeHelper_Property',
  24015. LinesToStr([ // statements
  24016. 'rtl.createHelper(this, "THelper", null, function () {',
  24017. ' this.GetSize = function () {',
  24018. ' var Result = 0;',
  24019. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24020. ' $mod.THelper.SetSize.call(this, 2);',
  24021. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24022. ' $mod.THelper.SetSize.call(this, 4);',
  24023. ' var $with = this.get();',
  24024. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24025. ' $mod.THelper.SetSize.call(this, 6);',
  24026. ' return Result;',
  24027. ' };',
  24028. ' this.SetSize = function (Value) {',
  24029. ' };',
  24030. '});',
  24031. 'this.w = 0;',
  24032. '']),
  24033. LinesToStr([ // $mod.$main
  24034. '$mod.w = $mod.THelper.GetSize.call({',
  24035. ' p: $mod,',
  24036. ' get: function () {',
  24037. ' return this.p.w;',
  24038. ' },',
  24039. ' set: function (v) {',
  24040. ' this.p.w = v;',
  24041. ' }',
  24042. '}) + 7;',
  24043. '$mod.THelper.SetSize.call({',
  24044. ' p: $mod,',
  24045. ' get: function () {',
  24046. ' return this.p.w;',
  24047. ' },',
  24048. ' set: function (v) {',
  24049. ' this.p.w = v;',
  24050. ' }',
  24051. '}, $mod.w + 8);',
  24052. 'var $with = $mod.w;',
  24053. '$mod.w = $mod.THelper.GetSize.call({',
  24054. ' get: function () {',
  24055. ' return $with;',
  24056. ' },',
  24057. ' set: function (v) {',
  24058. ' $with = v;',
  24059. ' }',
  24060. '}) + 9;',
  24061. '$mod.THelper.SetSize.call({',
  24062. ' get: function () {',
  24063. ' return $with;',
  24064. ' },',
  24065. ' set: function (v) {',
  24066. ' $with = v;',
  24067. ' }',
  24068. '}, $mod.w + 10);',
  24069. '']));
  24070. end;
  24071. procedure TTestModule.TestTypeHelper_Property_Array;
  24072. begin
  24073. StartProgram(false);
  24074. Add([
  24075. '{$modeswitch typehelpers}',
  24076. 'type',
  24077. ' THelper = type helper for word',
  24078. ' function GetItems(Index: byte): boolean;',
  24079. ' procedure SetItems(Index: byte; Value: boolean);',
  24080. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24081. ' end;',
  24082. 'function THelper.GetItems(Index: byte): boolean;',
  24083. 'begin',
  24084. ' Result:=Items[1];',
  24085. ' Items[2]:=false;',
  24086. ' Result:=Self.Items[3];',
  24087. ' Self.Items[4]:=true;',
  24088. ' with Self do begin',
  24089. ' Result:=Items[5];',
  24090. ' Items[6]:=false;',
  24091. ' end;',
  24092. 'end;',
  24093. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24094. 'begin',
  24095. 'end;',
  24096. 'var',
  24097. ' w: word;',
  24098. ' b: boolean;',
  24099. 'begin',
  24100. ' b:=w.Items[1];',
  24101. ' w.Items[2]:=b;',
  24102. ' with w do begin',
  24103. ' b:=Items[3];',
  24104. ' Items[4]:=b;',
  24105. ' end;',
  24106. '']);
  24107. ConvertProgram;
  24108. CheckSource('TestTypeHelper_Property_Array',
  24109. LinesToStr([ // statements
  24110. 'rtl.createHelper(this, "THelper", null, function () {',
  24111. ' this.GetItems = function (Index) {',
  24112. ' var Result = false;',
  24113. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24114. ' $mod.THelper.SetItems.call(this, 2, false);',
  24115. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24116. ' $mod.THelper.SetItems.call(this, 4, true);',
  24117. ' var $with = this.get();',
  24118. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24119. ' $mod.THelper.SetItems.call(this, 6, false);',
  24120. ' return Result;',
  24121. ' };',
  24122. ' this.SetItems = function (Index, Value) {',
  24123. ' };',
  24124. '});',
  24125. 'this.w = 0;',
  24126. 'this.b = false;',
  24127. '']),
  24128. LinesToStr([ // $mod.$main
  24129. '$mod.b = $mod.THelper.GetItems.call({',
  24130. ' p: $mod,',
  24131. ' get: function () {',
  24132. ' return this.p.w;',
  24133. ' },',
  24134. ' set: function (v) {',
  24135. ' this.p.w = v;',
  24136. ' }',
  24137. '}, 1);',
  24138. '$mod.THelper.SetItems.call({',
  24139. ' p: $mod,',
  24140. ' get: function () {',
  24141. ' return this.p.w;',
  24142. ' },',
  24143. ' set: function (v) {',
  24144. ' this.p.w = v;',
  24145. ' }',
  24146. '}, 2, $mod.b);',
  24147. 'var $with = $mod.w;',
  24148. '$mod.b = $mod.THelper.GetItems.call({',
  24149. ' get: function () {',
  24150. ' return $with;',
  24151. ' },',
  24152. ' set: function (v) {',
  24153. ' $with = v;',
  24154. ' }',
  24155. '}, 3);',
  24156. '$mod.THelper.SetItems.call({',
  24157. ' get: function () {',
  24158. ' return $with;',
  24159. ' },',
  24160. ' set: function (v) {',
  24161. ' $with = v;',
  24162. ' }',
  24163. '}, 4, $mod.b);',
  24164. '']));
  24165. end;
  24166. procedure TTestModule.TestTypeHelper_ClassProperty;
  24167. begin
  24168. StartProgram(false);
  24169. Add([
  24170. '{$modeswitch typehelpers}',
  24171. 'type',
  24172. ' THelper = type helper for word',
  24173. ' class function GetSize: longint; static;',
  24174. ' class procedure SetSize(Value: longint); static;',
  24175. ' class property Size: longint read GetSize write SetSize;',
  24176. ' end;',
  24177. 'class function THelper.GetSize: longint;',
  24178. 'begin',
  24179. ' Result:=Size+1;',
  24180. ' Size:=2;',
  24181. 'end;',
  24182. 'class procedure THelper.SetSize(Value: longint);',
  24183. 'begin',
  24184. 'end;',
  24185. 'begin',
  24186. '']);
  24187. ConvertProgram;
  24188. CheckSource('TestTypeHelper_ClassProperty',
  24189. LinesToStr([ // statements
  24190. 'rtl.createHelper(this, "THelper", null, function () {',
  24191. ' this.GetSize = function () {',
  24192. ' var Result = 0;',
  24193. ' Result = $mod.THelper.GetSize() + 1;',
  24194. ' $mod.THelper.SetSize(2);',
  24195. ' return Result;',
  24196. ' };',
  24197. ' this.SetSize = function (Value) {',
  24198. ' };',
  24199. '});',
  24200. '']),
  24201. LinesToStr([ // $mod.$main
  24202. '']));
  24203. end;
  24204. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24205. begin
  24206. StartProgram(false);
  24207. Add([
  24208. '{$modeswitch typehelpers}',
  24209. 'type',
  24210. ' THelper = type helper for word',
  24211. ' class function GetItems(Index: byte): boolean; static;',
  24212. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24213. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24214. ' end;',
  24215. 'class function THelper.GetItems(Index: byte): boolean;',
  24216. 'begin',
  24217. ' Result:=Items[1];',
  24218. ' Items[2]:=false;',
  24219. 'end;',
  24220. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24221. 'begin',
  24222. 'end;',
  24223. 'var',
  24224. ' w: word;',
  24225. ' b: boolean;',
  24226. 'begin',
  24227. ' b:=w.Items[1];',
  24228. ' w.Items[2]:=b;',
  24229. ' with w do begin',
  24230. ' b:=Items[3];',
  24231. ' Items[4]:=b;',
  24232. ' end;',
  24233. '']);
  24234. ConvertProgram;
  24235. CheckSource('TestTypeHelper_ClassProperty_Array',
  24236. LinesToStr([ // statements
  24237. 'rtl.createHelper(this, "THelper", null, function () {',
  24238. ' this.GetItems = function (Index) {',
  24239. ' var Result = false;',
  24240. ' Result = $mod.THelper.GetItems(1);',
  24241. ' $mod.THelper.SetItems(2, false);',
  24242. ' return Result;',
  24243. ' };',
  24244. ' this.SetItems = function (Index, Value) {',
  24245. ' };',
  24246. '});',
  24247. 'this.w = 0;',
  24248. 'this.b = false;',
  24249. '']),
  24250. LinesToStr([ // $mod.$main
  24251. '$mod.b = $mod.THelper.GetItems(1);',
  24252. '$mod.THelper.SetItems(2, $mod.b);',
  24253. 'var $with = $mod.w;',
  24254. '$mod.b = $mod.THelper.GetItems(3);',
  24255. '$mod.THelper.SetItems(4, $mod.b);',
  24256. '']));
  24257. end;
  24258. procedure TTestModule.TestTypeHelper_ClassMethod;
  24259. begin
  24260. StartProgram(false);
  24261. Add([
  24262. '{$modeswitch typehelpers}',
  24263. 'type',
  24264. ' THelper = type helper for word',
  24265. ' class procedure DoStatic; static;',
  24266. ' end;',
  24267. 'class procedure THelper.DoStatic;',
  24268. 'begin',
  24269. ' DoStatic;',
  24270. ' DoStatic();',
  24271. 'end;',
  24272. 'var w: word;',
  24273. 'begin',
  24274. ' w.DoStatic;',
  24275. ' w.DoStatic();',
  24276. '']);
  24277. ConvertProgram;
  24278. CheckSource('TestTypeHelper_ClassMethod',
  24279. LinesToStr([ // statements
  24280. 'rtl.createHelper(this, "THelper", null, function () {',
  24281. ' this.DoStatic = function () {',
  24282. ' $mod.THelper.DoStatic();',
  24283. ' $mod.THelper.DoStatic();',
  24284. ' };',
  24285. '});',
  24286. 'this.w = 0;',
  24287. '']),
  24288. LinesToStr([ // $mod.$main
  24289. '$mod.THelper.DoStatic();',
  24290. '$mod.THelper.DoStatic();',
  24291. '']));
  24292. end;
  24293. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24294. begin
  24295. StartProgram(false);
  24296. Add([
  24297. '{$modeswitch typehelpers}',
  24298. 'type',
  24299. ' THelper = type helper for word',
  24300. ' procedure Run; external name ''Run'';',
  24301. ' end;',
  24302. 'var w: word;',
  24303. 'begin',
  24304. ' w.Run;',
  24305. '']);
  24306. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24307. ConvertProgram;
  24308. end;
  24309. procedure TTestModule.TestTypeHelper_Constructor;
  24310. begin
  24311. StartProgram(false);
  24312. Add([
  24313. '{$modeswitch typehelpers}',
  24314. 'type',
  24315. ' THelper = type helper for word',
  24316. ' constructor Init(e: longint);',
  24317. ' end;',
  24318. 'constructor THelper.Init(e: longint);',
  24319. 'begin',
  24320. ' Self:=e;',
  24321. ' Init(e+1);',
  24322. 'end;',
  24323. 'var w: word;',
  24324. 'begin',
  24325. ' w:=word.Init(2);',
  24326. ' w:=w.Init(3);',
  24327. ' with word do w:=Init(4);',
  24328. ' with w do w:=Init(5);',
  24329. '']);
  24330. ConvertProgram;
  24331. CheckSource('TestTypeHelper_Constructor',
  24332. LinesToStr([ // statements
  24333. 'rtl.createHelper(this, "THelper", null, function () {',
  24334. ' this.Init = function (e) {',
  24335. ' this.set(e);',
  24336. ' $mod.THelper.Init.call(this, e + 1);',
  24337. ' return this.get();',
  24338. ' };',
  24339. ' this.$new = function (fn, args) {',
  24340. ' return this[fn].apply({',
  24341. ' p: 0,',
  24342. ' get: function () {',
  24343. ' return this.p;',
  24344. ' },',
  24345. ' set: function (v) {',
  24346. ' this.p = v;',
  24347. ' }',
  24348. ' }, args);',
  24349. ' };',
  24350. '});',
  24351. 'this.w = 0;',
  24352. '']),
  24353. LinesToStr([ // $mod.$main
  24354. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24355. '$mod.w = $mod.THelper.Init.call({',
  24356. ' p: $mod,',
  24357. ' get: function () {',
  24358. ' return this.p.w;',
  24359. ' },',
  24360. ' set: function (v) {',
  24361. ' this.p.w = v;',
  24362. ' }',
  24363. '}, 3);',
  24364. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24365. 'var $with = $mod.w;',
  24366. '$mod.w = $mod.THelper.Init.call({',
  24367. ' get: function () {',
  24368. ' return $with;',
  24369. ' },',
  24370. ' set: function (v) {',
  24371. ' $with = v;',
  24372. ' }',
  24373. '}, 5);',
  24374. '']));
  24375. end;
  24376. procedure TTestModule.TestTypeHelper_Word;
  24377. begin
  24378. StartProgram(false);
  24379. Add([
  24380. '{$modeswitch typehelpers}',
  24381. 'type',
  24382. ' THelper = type helper for word',
  24383. ' procedure DoIt(e: byte = 123);',
  24384. ' end;',
  24385. 'procedure THelper.DoIt(e: byte);',
  24386. 'begin',
  24387. ' Self:=e;',
  24388. ' Self:=Self+1;',
  24389. ' with Self do Doit;',
  24390. 'end;',
  24391. 'begin',
  24392. ' word(3).DoIt;',
  24393. '']);
  24394. ConvertProgram;
  24395. CheckSource('TestTypeHelper_Word',
  24396. LinesToStr([ // statements
  24397. 'rtl.createHelper(this, "THelper", null, function () {',
  24398. ' this.DoIt = function (e) {',
  24399. ' this.set(e);',
  24400. ' this.set(this.get() + 1);',
  24401. ' var $with = this.get();',
  24402. ' $mod.THelper.DoIt.call(this, 123);',
  24403. ' };',
  24404. '});',
  24405. '']),
  24406. LinesToStr([ // $mod.$main
  24407. '$mod.THelper.DoIt.call({',
  24408. ' get: function () {',
  24409. ' return 3;',
  24410. ' },',
  24411. ' set: function (v) {',
  24412. ' rtl.raiseE("EPropReadOnly");',
  24413. ' }',
  24414. '}, 123);',
  24415. '']));
  24416. end;
  24417. procedure TTestModule.TestTypeHelper_Boolean;
  24418. begin
  24419. StartProgram(false);
  24420. Add([
  24421. '{$modeswitch typehelpers}',
  24422. 'type',
  24423. ' Integer = longint;',
  24424. ' THelper = type helper for boolean',
  24425. ' procedure Run(e: wordbool = true);',
  24426. ' end;',
  24427. 'procedure THelper.Run(e: wordbool);',
  24428. 'begin',
  24429. ' Self:=e;',
  24430. ' Self:=not Self;',
  24431. ' with Self do Run;',
  24432. ' if Integer(Self)=0 then ;',
  24433. 'end;',
  24434. 'begin',
  24435. ' boolean(3).Run;',
  24436. '']);
  24437. ConvertProgram;
  24438. CheckSource('TestTypeHelper_Boolean',
  24439. LinesToStr([ // statements
  24440. 'rtl.createHelper(this, "THelper", null, function () {',
  24441. ' this.Run = function (e) {',
  24442. ' this.set(e);',
  24443. ' this.set(!this.get());',
  24444. ' var $with = this.get();',
  24445. ' $mod.THelper.Run.call(this, true);',
  24446. ' if ((this.get() ? 1 : 0) === 0) ;',
  24447. ' };',
  24448. '});',
  24449. '']),
  24450. LinesToStr([ // $mod.$main
  24451. '$mod.THelper.Run.call({',
  24452. ' a: 3 != 0,',
  24453. ' get: function () {',
  24454. ' return this.a;',
  24455. ' },',
  24456. ' set: function (v) {',
  24457. ' rtl.raiseE("EPropReadOnly");',
  24458. ' }',
  24459. '}, true);',
  24460. '']));
  24461. end;
  24462. procedure TTestModule.TestTypeHelper_WordBool;
  24463. begin
  24464. StartProgram(false);
  24465. Add([
  24466. '{$modeswitch typehelpers}',
  24467. 'type',
  24468. ' Integer = longint;',
  24469. ' THelper = type helper for WordBool',
  24470. ' procedure Run(e: wordbool = true);',
  24471. ' end;',
  24472. 'procedure THelper.Run(e: wordbool);',
  24473. 'var i: integer;',
  24474. 'begin',
  24475. ' i:=Integer(Self);',
  24476. 'end;',
  24477. 'var w: wordbool;',
  24478. 'begin',
  24479. ' w.Run;',
  24480. ' wordbool(3).Run;',
  24481. '']);
  24482. ConvertProgram;
  24483. CheckSource('TestTypeHelper_WordBool',
  24484. LinesToStr([ // statements
  24485. 'rtl.createHelper(this, "THelper", null, function () {',
  24486. ' this.Run = function (e) {',
  24487. ' var i = 0;',
  24488. ' i = (this.get() ? 1 : 0);',
  24489. ' };',
  24490. '});',
  24491. 'this.w = false;',
  24492. '']),
  24493. LinesToStr([ // $mod.$main
  24494. '$mod.THelper.Run.call({',
  24495. ' p: $mod,',
  24496. ' get: function () {',
  24497. ' return this.p.w;',
  24498. ' },',
  24499. ' set: function (v) {',
  24500. ' this.p.w = v;',
  24501. ' }',
  24502. '}, true);',
  24503. '$mod.THelper.Run.call({',
  24504. ' a: 3 != 0,',
  24505. ' get: function () {',
  24506. ' return this.a;',
  24507. ' },',
  24508. ' set: function (v) {',
  24509. ' rtl.raiseE("EPropReadOnly");',
  24510. ' }',
  24511. '}, true);',
  24512. '']));
  24513. end;
  24514. procedure TTestModule.TestTypeHelper_Double;
  24515. begin
  24516. StartProgram(false);
  24517. Add([
  24518. '{$modeswitch typehelpers}',
  24519. 'type',
  24520. ' Float = type double;',
  24521. ' THelper = type helper for Float',
  24522. ' const NPI = 3.141592;',
  24523. ' function ToStr: String;',
  24524. ' end;',
  24525. 'function THelper.ToStr: String;',
  24526. 'begin',
  24527. 'end;',
  24528. 'procedure DoIt(s: string);',
  24529. 'begin',
  24530. 'end;',
  24531. 'var f: Float;',
  24532. 'begin',
  24533. ' DoIt(f.toStr);',
  24534. ' DoIt(f.toStr());',
  24535. ' (f*f).toStr;',
  24536. ' DoIt((f*f).toStr);',
  24537. '']);
  24538. ConvertProgram;
  24539. CheckSource('TestTypeHelper_Double',
  24540. LinesToStr([ // statements
  24541. 'rtl.createHelper(this, "THelper", null, function () {',
  24542. ' this.NPI = 3.141592;',
  24543. ' this.ToStr = function () {',
  24544. ' var Result = "";',
  24545. ' return Result;',
  24546. ' };',
  24547. '});',
  24548. 'this.DoIt = function (s) {',
  24549. '};',
  24550. 'this.f = 0.0;',
  24551. '']),
  24552. LinesToStr([ // $mod.$main
  24553. '$mod.DoIt($mod.THelper.ToStr.call({',
  24554. ' p: $mod,',
  24555. ' get: function () {',
  24556. ' return this.p.f;',
  24557. ' },',
  24558. ' set: function (v) {',
  24559. ' this.p.f = v;',
  24560. ' }',
  24561. '}));',
  24562. '$mod.DoIt($mod.THelper.ToStr.call({',
  24563. ' p: $mod,',
  24564. ' get: function () {',
  24565. ' return this.p.f;',
  24566. ' },',
  24567. ' set: function (v) {',
  24568. ' this.p.f = v;',
  24569. ' }',
  24570. '}));',
  24571. '$mod.THelper.ToStr.call({',
  24572. ' a: $mod.f * $mod.f,',
  24573. ' get: function () {',
  24574. ' return this.a;',
  24575. ' },',
  24576. ' set: function (v) {',
  24577. ' rtl.raiseE("EPropReadOnly");',
  24578. ' }',
  24579. '});',
  24580. '$mod.DoIt($mod.THelper.ToStr.call({',
  24581. ' a: $mod.f * $mod.f,',
  24582. ' get: function () {',
  24583. ' return this.a;',
  24584. ' },',
  24585. ' set: function (v) {',
  24586. ' rtl.raiseE("EPropReadOnly");',
  24587. ' }',
  24588. '}));',
  24589. '']));
  24590. end;
  24591. procedure TTestModule.TestTypeHelper_NativeInt;
  24592. begin
  24593. StartProgram(false);
  24594. Add([
  24595. '{$modeswitch typehelpers}',
  24596. 'type',
  24597. ' MaxInt = type nativeint;',
  24598. ' THelperI = type helper for MaxInt',
  24599. ' function ToStr: String;',
  24600. ' end;',
  24601. ' MaxUInt = type nativeuint;',
  24602. ' THelperU = type helper for MaxUInt',
  24603. ' function ToStr: String;',
  24604. ' end;',
  24605. 'function THelperI.ToStr: String;',
  24606. 'begin',
  24607. ' Result:=str(Self);',
  24608. 'end;',
  24609. 'function THelperU.ToStr: String;',
  24610. 'begin',
  24611. ' Result:=str(Self);',
  24612. 'end;',
  24613. 'procedure DoIt(s: string);',
  24614. 'begin',
  24615. 'end;',
  24616. 'var i: MaxInt;',
  24617. 'begin',
  24618. ' DoIt(i.toStr);',
  24619. ' DoIt(i.toStr());',
  24620. ' (i*i).toStr;',
  24621. ' DoIt((i*i).toStr);',
  24622. '']);
  24623. ConvertProgram;
  24624. CheckSource('TestTypeHelper_NativeInt',
  24625. LinesToStr([ // statements
  24626. 'rtl.createHelper(this, "THelperI", null, function () {',
  24627. ' this.ToStr = function () {',
  24628. ' var Result = "";',
  24629. ' Result = "" + this.get();',
  24630. ' return Result;',
  24631. ' };',
  24632. '});',
  24633. 'rtl.createHelper(this, "THelperU", null, function () {',
  24634. ' this.ToStr = function () {',
  24635. ' var Result = "";',
  24636. ' Result = "" + this.get();',
  24637. ' return Result;',
  24638. ' };',
  24639. '});',
  24640. 'this.DoIt = function (s) {',
  24641. '};',
  24642. 'this.i = 0;',
  24643. '']),
  24644. LinesToStr([ // $mod.$main
  24645. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24646. ' p: $mod,',
  24647. ' get: function () {',
  24648. ' return this.p.i;',
  24649. ' },',
  24650. ' set: function (v) {',
  24651. ' this.p.i = v;',
  24652. ' }',
  24653. '}));',
  24654. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24655. ' p: $mod,',
  24656. ' get: function () {',
  24657. ' return this.p.i;',
  24658. ' },',
  24659. ' set: function (v) {',
  24660. ' this.p.i = v;',
  24661. ' }',
  24662. '}));',
  24663. '$mod.THelperI.ToStr.call({',
  24664. ' a: $mod.i * $mod.i,',
  24665. ' get: function () {',
  24666. ' return this.a;',
  24667. ' },',
  24668. ' set: function (v) {',
  24669. ' rtl.raiseE("EPropReadOnly");',
  24670. ' }',
  24671. '});',
  24672. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24673. ' a: $mod.i * $mod.i,',
  24674. ' get: function () {',
  24675. ' return this.a;',
  24676. ' },',
  24677. ' set: function (v) {',
  24678. ' rtl.raiseE("EPropReadOnly");',
  24679. ' }',
  24680. '}));',
  24681. '']));
  24682. end;
  24683. procedure TTestModule.TestTypeHelper_StringChar;
  24684. begin
  24685. StartProgram(false);
  24686. Add([
  24687. '{$modeswitch typehelpers}',
  24688. 'type',
  24689. ' TStringHelper = type helper for string',
  24690. ' procedure DoIt(e: byte = 123);',
  24691. ' end;',
  24692. ' TCharHelper = type helper for char',
  24693. ' procedure Fly;',
  24694. ' end;',
  24695. 'procedure TStringHelper.DoIt(e: byte);',
  24696. 'begin',
  24697. ' Self[1]:=''c'';',
  24698. ' Self[2]:=Self[3];',
  24699. 'end;',
  24700. 'procedure TCharHelper.Fly;',
  24701. 'begin',
  24702. ' Self:=''c'';',
  24703. 'end;',
  24704. 'begin',
  24705. ' ''abc''.DoIt;',
  24706. ' ''xyz''.DoIt();',
  24707. ' ''c''.Fly();',
  24708. '']);
  24709. ConvertProgram;
  24710. CheckSource('TestTypeHelper_StringChar',
  24711. LinesToStr([ // statements
  24712. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24713. ' this.DoIt = function (e) {',
  24714. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24715. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24716. ' };',
  24717. '});',
  24718. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24719. ' this.Fly = function () {',
  24720. ' this.set("c");',
  24721. ' };',
  24722. '});',
  24723. '']),
  24724. LinesToStr([ // $mod.$main
  24725. '$mod.TStringHelper.DoIt.call({',
  24726. ' get: function () {',
  24727. ' return "abc";',
  24728. ' },',
  24729. ' set: function (v) {',
  24730. ' rtl.raiseE("EPropReadOnly");',
  24731. ' }',
  24732. '}, 123);',
  24733. '$mod.TStringHelper.DoIt.call({',
  24734. ' get: function () {',
  24735. ' return "xyz";',
  24736. ' },',
  24737. ' set: function (v) {',
  24738. ' rtl.raiseE("EPropReadOnly");',
  24739. ' }',
  24740. '}, 123);',
  24741. '$mod.TCharHelper.Fly.call({',
  24742. ' get: function () {',
  24743. ' return "c";',
  24744. ' },',
  24745. ' set: function (v) {',
  24746. ' rtl.raiseE("EPropReadOnly");',
  24747. ' }',
  24748. '});',
  24749. '']));
  24750. end;
  24751. procedure TTestModule.TestTypeHelper_JSValue;
  24752. begin
  24753. StartProgram(false);
  24754. Add([
  24755. '{$modeswitch typehelpers}',
  24756. 'type',
  24757. ' TExtValue = type jsvalue;',
  24758. ' THelper = type helper for TExtValue',
  24759. ' function ToStr: String;',
  24760. ' end;',
  24761. 'function THelper.ToStr: String;',
  24762. 'begin',
  24763. 'end;',
  24764. 'var',
  24765. ' s: string;',
  24766. ' v: TExtValue;',
  24767. 'begin',
  24768. ' s:=v.toStr;',
  24769. ' s:=v.toStr();',
  24770. ' TExtValue(s).toStr;',
  24771. '']);
  24772. ConvertProgram;
  24773. CheckSource('TestTypeHelper_JSValue',
  24774. LinesToStr([ // statements
  24775. 'rtl.createHelper(this, "THelper", null, function () {',
  24776. ' this.ToStr = function () {',
  24777. ' var Result = "";',
  24778. ' return Result;',
  24779. ' };',
  24780. '});',
  24781. 'this.s = "";',
  24782. 'this.v = undefined;',
  24783. '']),
  24784. LinesToStr([ // $mod.$main
  24785. '$mod.s = $mod.THelper.ToStr.call({',
  24786. ' p: $mod,',
  24787. ' get: function () {',
  24788. ' return this.p.v;',
  24789. ' },',
  24790. ' set: function (v) {',
  24791. ' this.p.v = v;',
  24792. ' }',
  24793. '});',
  24794. '$mod.s = $mod.THelper.ToStr.call({',
  24795. ' p: $mod,',
  24796. ' get: function () {',
  24797. ' return this.p.v;',
  24798. ' },',
  24799. ' set: function (v) {',
  24800. ' this.p.v = v;',
  24801. ' }',
  24802. '});',
  24803. '$mod.THelper.ToStr.call({',
  24804. ' p: $mod,',
  24805. ' get: function () {',
  24806. ' return this.p.s;',
  24807. ' },',
  24808. ' set: function (v) {',
  24809. ' rtl.raiseE("EPropReadOnly");',
  24810. ' }',
  24811. '});',
  24812. '']));
  24813. end;
  24814. procedure TTestModule.TestTypeHelper_Array;
  24815. begin
  24816. StartProgram(false);
  24817. Add([
  24818. '{$modeswitch typehelpers}',
  24819. 'type',
  24820. ' TArrOfBool = array of boolean;',
  24821. ' TArrOfJS = array of jsvalue;',
  24822. ' THelper = type helper for TArrOfBool',
  24823. ' procedure DoIt(e: byte = 123);',
  24824. ' end;',
  24825. 'procedure THelper.DoIt(e: byte);',
  24826. 'begin',
  24827. ' Self[1]:=true;',
  24828. ' Self[2]:=not Self[3];',
  24829. ' SetLength(Self,4);',
  24830. 'end;',
  24831. 'var',
  24832. ' b: TArrOfBool;',
  24833. ' j: TArrOfJS;',
  24834. 'begin',
  24835. ' b.DoIt;',
  24836. ' TArrOfBool(j).DoIt();',
  24837. '']);
  24838. ConvertProgram;
  24839. CheckSource('TestTypeHelper_Array',
  24840. LinesToStr([ // statements
  24841. 'rtl.createHelper(this, "THelper", null, function () {',
  24842. ' this.DoIt = function (e) {',
  24843. ' this.get()[1] = true;',
  24844. ' this.get()[2] = !this.get()[3];',
  24845. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24846. ' };',
  24847. '});',
  24848. 'this.b = [];',
  24849. 'this.j = [];',
  24850. '']),
  24851. LinesToStr([ // $mod.$main
  24852. '$mod.THelper.DoIt.call({',
  24853. ' p: $mod,',
  24854. ' get: function () {',
  24855. ' return this.p.b;',
  24856. ' },',
  24857. ' set: function (v) {',
  24858. ' this.p.b = v;',
  24859. ' }',
  24860. '}, 123);',
  24861. '$mod.THelper.DoIt.call({',
  24862. ' p: $mod,',
  24863. ' get: function () {',
  24864. ' return this.p.j;',
  24865. ' },',
  24866. ' set: function (v) {',
  24867. ' this.p.j = v;',
  24868. ' }',
  24869. '}, 123);',
  24870. '']));
  24871. end;
  24872. procedure TTestModule.TestTypeHelper_EnumType;
  24873. begin
  24874. StartProgram(false);
  24875. Add([
  24876. '{$modeswitch typehelpers}',
  24877. 'type',
  24878. ' TEnum = (red,blue);',
  24879. ' THelper = type helper for TEnum',
  24880. ' procedure DoIt(e: byte = 123);',
  24881. ' class procedure Swing(w: word); static;',
  24882. ' end;',
  24883. 'procedure THelper.DoIt(e: byte);',
  24884. 'begin',
  24885. ' Self:=red;',
  24886. ' Self:=succ(Self);',
  24887. ' with Self do Doit;',
  24888. 'end;',
  24889. 'class procedure THelper.Swing(w: word);',
  24890. 'begin',
  24891. 'end;',
  24892. 'var e: TEnum;',
  24893. 'begin',
  24894. ' e.DoIt;',
  24895. ' red.DoIt;',
  24896. ' TEnum.blue.DoIt;',
  24897. ' TEnum(1).DoIt;',
  24898. ' TEnum.Swing(3);',
  24899. '']);
  24900. ConvertProgram;
  24901. CheckSource('TestTypeHelper_EnumType',
  24902. LinesToStr([ // statements
  24903. 'this.TEnum = {',
  24904. ' "0": "red",',
  24905. ' red: 0,',
  24906. ' "1": "blue",',
  24907. ' blue: 1',
  24908. '};',
  24909. 'rtl.createHelper(this, "THelper", null, function () {',
  24910. ' this.DoIt = function (e) {',
  24911. ' this.set($mod.TEnum.red);',
  24912. ' this.set(this.get() + 1);',
  24913. ' var $with = this.get();',
  24914. ' $mod.THelper.DoIt.call(this, 123);',
  24915. ' };',
  24916. ' this.Swing = function (w) {',
  24917. ' };',
  24918. '});',
  24919. 'this.e = 0;',
  24920. '']),
  24921. LinesToStr([ // $mod.$main
  24922. '$mod.THelper.DoIt.call({',
  24923. ' p: $mod,',
  24924. ' get: function () {',
  24925. ' return this.p.e;',
  24926. ' },',
  24927. ' set: function (v) {',
  24928. ' this.p.e = v;',
  24929. ' }',
  24930. '}, 123);',
  24931. '$mod.THelper.DoIt.call({',
  24932. ' p: $mod.TEnum,',
  24933. ' get: function () {',
  24934. ' return this.p.red;',
  24935. ' },',
  24936. ' set: function (v) {',
  24937. ' rtl.raiseE("EPropReadOnly");',
  24938. ' }',
  24939. '}, 123);',
  24940. '$mod.THelper.DoIt.call({',
  24941. ' p: $mod.TEnum,',
  24942. ' get: function () {',
  24943. ' return this.p.blue;',
  24944. ' },',
  24945. ' set: function (v) {',
  24946. ' rtl.raiseE("EPropReadOnly");',
  24947. ' }',
  24948. '}, 123);',
  24949. '$mod.THelper.DoIt.call({',
  24950. ' get: function () {',
  24951. ' return 1;',
  24952. ' },',
  24953. ' set: function (v) {',
  24954. ' rtl.raiseE("EPropReadOnly");',
  24955. ' }',
  24956. '}, 123);',
  24957. '$mod.THelper.Swing(3);',
  24958. '']));
  24959. end;
  24960. procedure TTestModule.TestTypeHelper_SetType;
  24961. begin
  24962. StartProgram(false);
  24963. Add([
  24964. '{$modeswitch typehelpers}',
  24965. 'type',
  24966. ' TEnum = (red,blue);',
  24967. ' TSetOfEnum = set of TEnum;',
  24968. ' THelper = type helper for TSetOfEnum',
  24969. ' procedure DoIt(e: byte = 123);',
  24970. ' constructor Init(e: TEnum);',
  24971. ' constructor InitEmpty;',
  24972. ' end;',
  24973. 'procedure THelper.DoIt(e: byte);',
  24974. 'begin',
  24975. ' Self:=[];',
  24976. ' Self:=[red];',
  24977. ' Include(Self,blue);',
  24978. 'end;',
  24979. 'constructor THelper.Init(e: TEnum);',
  24980. 'begin',
  24981. ' Self:=[];',
  24982. ' Self:=[e];',
  24983. ' Include(Self,blue);',
  24984. 'end;',
  24985. 'constructor THelper.InitEmpty;',
  24986. 'begin',
  24987. 'end;',
  24988. 'var s: TSetOfEnum;',
  24989. 'begin',
  24990. ' s.DoIt;',
  24991. //' [red].DoIt;',
  24992. //' with s do DoIt;',
  24993. //' with [red,blue] do DoIt;',
  24994. ' s:=TSetOfEnum.Init(blue);',
  24995. ' s:=s.Init(blue);',
  24996. '']);
  24997. ConvertProgram;
  24998. CheckSource('TestTypeHelper_SetType',
  24999. LinesToStr([ // statements
  25000. 'this.TEnum = {',
  25001. ' "0": "red",',
  25002. ' red: 0,',
  25003. ' "1": "blue",',
  25004. ' blue: 1',
  25005. '};',
  25006. 'rtl.createHelper(this, "THelper", null, function () {',
  25007. ' this.DoIt = function (e) {',
  25008. ' this.set({});',
  25009. ' this.set(rtl.createSet($mod.TEnum.red));',
  25010. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25011. ' };',
  25012. ' this.Init = function (e) {',
  25013. ' this.set({});',
  25014. ' this.set(rtl.createSet(e));',
  25015. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25016. ' return this.get();',
  25017. ' };',
  25018. ' this.InitEmpty = function () {',
  25019. ' return this.get();',
  25020. ' };',
  25021. ' this.$new = function (fn, args) {',
  25022. ' return this[fn].apply({',
  25023. ' p: {},',
  25024. ' get: function () {',
  25025. ' return this.p;',
  25026. ' },',
  25027. ' set: function (v) {',
  25028. ' this.p = v;',
  25029. ' }',
  25030. ' }, args);',
  25031. ' };',
  25032. '});',
  25033. 'this.s = {};',
  25034. '']),
  25035. LinesToStr([ // $mod.$main
  25036. '$mod.THelper.DoIt.call({',
  25037. ' p: $mod,',
  25038. ' get: function () {',
  25039. ' return this.p.s;',
  25040. ' },',
  25041. ' set: function (v) {',
  25042. ' this.p.s = v;',
  25043. ' }',
  25044. '}, 123);',
  25045. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25046. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25047. ' p: $mod,',
  25048. ' get: function () {',
  25049. ' return this.p.s;',
  25050. ' },',
  25051. ' set: function (v) {',
  25052. ' this.p.s = v;',
  25053. ' }',
  25054. '}, $mod.TEnum.blue));',
  25055. '']));
  25056. end;
  25057. procedure TTestModule.TestTypeHelper_InterfaceType;
  25058. begin
  25059. StartProgram(false);
  25060. Add([
  25061. '{$interfaces com}',
  25062. '{$modeswitch typehelpers}',
  25063. 'type',
  25064. ' IUnknown = interface',
  25065. ' function _AddRef: longint;',
  25066. ' function _Release: longint;',
  25067. ' end;',
  25068. ' TObject = class(IUnknown)',
  25069. ' function _AddRef: longint; virtual; abstract;',
  25070. ' function _Release: longint; virtual; abstract;',
  25071. ' end;',
  25072. ' THelper = type helper for IUnknown',
  25073. ' procedure Fly(e: byte = 123);',
  25074. ' class procedure Run; static;',
  25075. ' end;',
  25076. 'var',
  25077. ' i: IUnknown;',
  25078. ' o: TObject;',
  25079. 'procedure THelper.Fly(e: byte);',
  25080. 'begin',
  25081. ' i:=Self;',
  25082. ' o:=Self as TObject;',
  25083. ' Self:=nil;',
  25084. ' Self:=i;',
  25085. ' Self:=o;',
  25086. ' with Self do begin',
  25087. ' Fly;',
  25088. ' Fly();',
  25089. ' end;',
  25090. 'end;',
  25091. 'class procedure THelper.Run;',
  25092. 'var l: IUnknown;',
  25093. 'begin',
  25094. ' l.Fly;',
  25095. ' l.Fly();',
  25096. 'end;',
  25097. 'begin',
  25098. ' i.Fly;',
  25099. ' i.Fly();',
  25100. ' i.Run;',
  25101. ' i.Run();',
  25102. ' IUnknown.Run;',
  25103. ' IUnknown.Run();',
  25104. '']);
  25105. ConvertProgram;
  25106. CheckSource('TestTypeHelper_InterfaceType',
  25107. LinesToStr([ // statements
  25108. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25109. 'rtl.createClass(this, "TObject", null, function () {',
  25110. ' this.$init = function () {',
  25111. ' };',
  25112. ' this.$final = function () {',
  25113. ' };',
  25114. ' rtl.addIntf(this, $mod.IUnknown);',
  25115. '});',
  25116. 'rtl.createHelper(this, "THelper", null, function () {',
  25117. ' this.Fly = function (e) {',
  25118. ' var $ir = rtl.createIntfRefs();',
  25119. ' try {',
  25120. ' rtl.setIntfP($mod, "i", this.get());',
  25121. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25122. ' this.set(null);',
  25123. ' this.set($mod.i);',
  25124. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25125. ' var $with = this.get();',
  25126. ' $mod.THelper.Fly.call(this, 123);',
  25127. ' $mod.THelper.Fly.call(this, 123);',
  25128. ' } finally {',
  25129. ' $ir.free();',
  25130. ' };',
  25131. ' };',
  25132. ' this.Run = function () {',
  25133. ' var l = null;',
  25134. ' try {',
  25135. ' $mod.THelper.Fly.call({',
  25136. ' get: function () {',
  25137. ' return l;',
  25138. ' },',
  25139. ' set: function (v) {',
  25140. ' l = rtl.setIntfL(l, v);',
  25141. ' }',
  25142. ' }, 123);',
  25143. ' $mod.THelper.Fly.call({',
  25144. ' get: function () {',
  25145. ' return l;',
  25146. ' },',
  25147. ' set: function (v) {',
  25148. ' l = rtl.setIntfL(l, v);',
  25149. ' }',
  25150. ' }, 123);',
  25151. ' } finally {',
  25152. ' rtl._Release(l);',
  25153. ' };',
  25154. ' };',
  25155. '});',
  25156. 'this.i = null;',
  25157. 'this.o = null;',
  25158. '']),
  25159. LinesToStr([ // $mod.$main
  25160. '$mod.THelper.Fly.call({',
  25161. ' p: $mod,',
  25162. ' get: function () {',
  25163. ' return this.p.i;',
  25164. ' },',
  25165. ' set: function (v) {',
  25166. ' rtl.setIntfP(this.p, "i", v);',
  25167. ' }',
  25168. '}, 123);',
  25169. '$mod.THelper.Fly.call({',
  25170. ' p: $mod,',
  25171. ' get: function () {',
  25172. ' return this.p.i;',
  25173. ' },',
  25174. ' set: function (v) {',
  25175. ' rtl.setIntfP(this.p, "i", v);',
  25176. ' }',
  25177. '}, 123);',
  25178. '$mod.THelper.Run();',
  25179. '$mod.THelper.Run();',
  25180. '$mod.THelper.Run();',
  25181. '$mod.THelper.Run();',
  25182. '']));
  25183. end;
  25184. procedure TTestModule.TestTypeHelper_NestedSelf;
  25185. begin
  25186. StartProgram(false);
  25187. Add([
  25188. '{$modeswitch typehelpers}',
  25189. 'type',
  25190. ' THelper = type helper for string',
  25191. ' procedure Run(Value: string);',
  25192. ' end;',
  25193. 'procedure THelper.Run(Value: string);',
  25194. ' function Sub(i: nativeint): boolean;',
  25195. ' begin',
  25196. ' Result:=Self[i+1]=Value[i];',
  25197. ' end;',
  25198. 'begin',
  25199. ' if Self[3]=Value[4] then ;',
  25200. 'end;',
  25201. 'begin',
  25202. '']);
  25203. ConvertProgram;
  25204. CheckSource('TestTypeHelper_NestedSelf',
  25205. LinesToStr([ // statements
  25206. 'rtl.createHelper(this, "THelper", null, function () {',
  25207. ' this.Run = function (Value) {',
  25208. ' var $Self = this;',
  25209. ' function Sub(i) {',
  25210. ' var Result = false;',
  25211. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25212. ' return Result;',
  25213. ' };',
  25214. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25215. ' };',
  25216. '});',
  25217. '']),
  25218. LinesToStr([ // $mod.$main
  25219. '']));
  25220. end;
  25221. procedure TTestModule.TestProcType;
  25222. begin
  25223. StartProgram(false);
  25224. Add([
  25225. 'type',
  25226. ' TProcInt = procedure(vI: longint = 1);',
  25227. 'procedure DoIt(vJ: longint);',
  25228. 'begin end;',
  25229. 'var',
  25230. ' b: boolean;',
  25231. ' vP, vQ: tprocint;',
  25232. 'begin',
  25233. ' vp:=nil;',
  25234. ' vp:=vp;',
  25235. ' vp:=@doit;',
  25236. ' vp;',
  25237. ' vp();',
  25238. ' vp(2);',
  25239. ' b:=vp=nil;',
  25240. ' b:=nil=vp;',
  25241. ' b:=vp=vq;',
  25242. ' b:=vp=@doit;',
  25243. ' b:=@doit=vp;',
  25244. ' b:=vp<>nil;',
  25245. ' b:=nil<>vp;',
  25246. ' b:=vp<>vq;',
  25247. ' b:=vp<>@doit;',
  25248. ' b:=@doit<>vp;',
  25249. ' b:=Assigned(vp);',
  25250. ' if Assigned(vp) then ;']);
  25251. ConvertProgram;
  25252. CheckSource('TestProcType',
  25253. LinesToStr([ // statements
  25254. 'this.DoIt = function(vJ) {',
  25255. '};',
  25256. 'this.b = false;',
  25257. 'this.vP = null;',
  25258. 'this.vQ = null;'
  25259. ]),
  25260. LinesToStr([ // $mod.$main
  25261. '$mod.vP = null;',
  25262. '$mod.vP = $mod.vP;',
  25263. '$mod.vP = $mod.DoIt;',
  25264. '$mod.vP(1);',
  25265. '$mod.vP(1);',
  25266. '$mod.vP(2);',
  25267. '$mod.b = $mod.vP === null;',
  25268. '$mod.b = null === $mod.vP;',
  25269. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25270. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25271. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25272. '$mod.b = $mod.vP !== null;',
  25273. '$mod.b = null !== $mod.vP;',
  25274. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25275. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25276. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25277. '$mod.b = $mod.vP != null;',
  25278. 'if ($mod.vP != null) ;',
  25279. '']));
  25280. end;
  25281. procedure TTestModule.TestProcType_Arg;
  25282. begin
  25283. StartProgram(false);
  25284. Add([
  25285. 'type',
  25286. ' TProcInt = procedure(vI: longint = 1);',
  25287. 'procedure DoIt(vJ: longint); begin end;',
  25288. 'procedure DoSome(vP, vQ: TProcInt);',
  25289. 'var',
  25290. ' b: boolean;',
  25291. 'begin',
  25292. ' vp:=nil;',
  25293. ' vp:=vp;',
  25294. ' vp:=@doit;',
  25295. ' vp;',
  25296. ' vp();',
  25297. ' vp(2);',
  25298. ' b:=vp=nil;',
  25299. ' b:=nil=vp;',
  25300. ' b:=vp=vq;',
  25301. ' b:=vp=@doit;',
  25302. ' b:=@doit=vp;',
  25303. ' b:=vp<>nil;',
  25304. ' b:=nil<>vp;',
  25305. ' b:=vp<>vq;',
  25306. ' b:=vp<>@doit;',
  25307. ' b:=@doit<>vp;',
  25308. ' b:=Assigned(vp);',
  25309. ' if Assigned(vp) then ;',
  25310. 'end;',
  25311. 'begin',
  25312. ' DoSome(@DoIt,nil);']);
  25313. ConvertProgram;
  25314. CheckSource('TestProcType_Arg',
  25315. LinesToStr([ // statements
  25316. 'this.DoIt = function(vJ) {',
  25317. '};',
  25318. 'this.DoSome = function(vP, vQ) {',
  25319. ' var b = false;',
  25320. ' vP = null;',
  25321. ' vP = vP;',
  25322. ' vP = $mod.DoIt;',
  25323. ' vP(1);',
  25324. ' vP(1);',
  25325. ' vP(2);',
  25326. ' b = vP === null;',
  25327. ' b = null === vP;',
  25328. ' b = rtl.eqCallback(vP,vQ);',
  25329. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25330. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25331. ' b = vP !== null;',
  25332. ' b = null !== vP;',
  25333. ' b = !rtl.eqCallback(vP, vQ);',
  25334. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25335. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25336. ' b = vP != null;',
  25337. ' if (vP != null) ;',
  25338. '};',
  25339. '']),
  25340. LinesToStr([ // $mod.$main
  25341. '$mod.DoSome($mod.DoIt,null);',
  25342. '']));
  25343. end;
  25344. procedure TTestModule.TestProcType_FunctionFPC;
  25345. begin
  25346. StartProgram(false);
  25347. Add('type');
  25348. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25349. Add('function DoIt(vI: longint): longint;');
  25350. Add('begin end;');
  25351. Add('var');
  25352. Add(' b: boolean;');
  25353. Add(' vP, vQ: tfuncint;');
  25354. Add('begin');
  25355. Add(' vp:=nil;');
  25356. Add(' vp:=vp;');
  25357. Add(' vp:=@doit;'); // ok in fpc and delphi
  25358. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25359. Add(' vp;'); // ok in fpc and delphi
  25360. Add(' vp();');
  25361. Add(' vp(2);');
  25362. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25363. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25364. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25365. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25366. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25367. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25368. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25369. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25370. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25371. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25372. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25373. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25374. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25375. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25376. Add(' b:=Assigned(vp);');
  25377. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25378. Add(' doit(vp());'); // ok in fpc and delphi
  25379. Add(' doit(vp(2));'); // ok in fpc and delphi
  25380. ConvertProgram;
  25381. CheckSource('TestProcType_FunctionFPC',
  25382. LinesToStr([ // statements
  25383. 'this.DoIt = function(vI) {',
  25384. ' var Result = 0;',
  25385. ' return Result;',
  25386. '};',
  25387. 'this.b = false;',
  25388. 'this.vP = null;',
  25389. 'this.vQ = null;'
  25390. ]),
  25391. LinesToStr([ // $mod.$main
  25392. '$mod.vP = null;',
  25393. '$mod.vP = $mod.vP;',
  25394. '$mod.vP = $mod.DoIt;',
  25395. '$mod.vP(1);',
  25396. '$mod.vP(1);',
  25397. '$mod.vP(2);',
  25398. '$mod.b = $mod.vP === null;',
  25399. '$mod.b = null === $mod.vP;',
  25400. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25401. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25402. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25403. '$mod.b = 4 === $mod.vP(1);',
  25404. '$mod.b = $mod.vP !== null;',
  25405. '$mod.b = null !== $mod.vP;',
  25406. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25407. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25408. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25409. '$mod.b = 6 !== $mod.vP(1);',
  25410. '$mod.b = $mod.vP != null;',
  25411. '$mod.DoIt($mod.vP(1));',
  25412. '$mod.DoIt($mod.vP(2));',
  25413. '']));
  25414. end;
  25415. procedure TTestModule.TestProcType_FunctionDelphi;
  25416. begin
  25417. StartProgram(false);
  25418. Add('{$mode Delphi}');
  25419. Add('type');
  25420. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25421. Add('function DoIt(vI: longint): longint;');
  25422. Add('begin end;');
  25423. Add('var');
  25424. Add(' b: boolean;');
  25425. Add(' vP, vQ: tfuncint;');
  25426. Add('begin');
  25427. Add(' vp:=nil;');
  25428. Add(' vp:=vp;');
  25429. Add(' vp:=@doit;'); // ok in fpc and delphi
  25430. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25431. Add(' vp;'); // ok in fpc and delphi
  25432. Add(' vp();');
  25433. Add(' vp(2);');
  25434. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25435. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25436. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25437. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25438. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25439. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25440. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25441. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25442. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25443. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25444. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25445. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25446. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25447. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25448. Add(' b:=Assigned(vp);');
  25449. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25450. Add(' doit(vp());'); // ok in fpc and delphi
  25451. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25452. ConvertProgram;
  25453. CheckSource('TestProcType_FunctionDelphi',
  25454. LinesToStr([ // statements
  25455. 'this.DoIt = function(vI) {',
  25456. ' var Result = 0;',
  25457. ' return Result;',
  25458. '};',
  25459. 'this.b = false;',
  25460. 'this.vP = null;',
  25461. 'this.vQ = null;'
  25462. ]),
  25463. LinesToStr([ // $mod.$main
  25464. '$mod.vP = null;',
  25465. '$mod.vP = $mod.vP;',
  25466. '$mod.vP = $mod.DoIt;',
  25467. '$mod.vP = $mod.DoIt;',
  25468. '$mod.vP(1);',
  25469. '$mod.vP(1);',
  25470. '$mod.vP(2);',
  25471. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25472. '$mod.b = $mod.vP(1) === 3;',
  25473. '$mod.b = 4 === $mod.vP(1);',
  25474. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25475. '$mod.b = $mod.vP(1) !== 5;',
  25476. '$mod.b = 6 !== $mod.vP(1);',
  25477. '$mod.b = $mod.vP != null;',
  25478. '$mod.DoIt($mod.vP(1));',
  25479. '$mod.DoIt($mod.vP(1));',
  25480. '$mod.DoIt($mod.vP(2));',
  25481. '']));
  25482. end;
  25483. procedure TTestModule.TestProcType_ProcedureDelphi;
  25484. begin
  25485. StartProgram(false);
  25486. Add('{$mode Delphi}');
  25487. Add('type');
  25488. Add(' TProc = procedure;');
  25489. Add('procedure DoIt;');
  25490. Add('begin end;');
  25491. Add('var');
  25492. Add(' b: boolean;');
  25493. Add(' vP, vQ: tproc;');
  25494. Add('begin');
  25495. Add(' vp:=nil;');
  25496. Add(' vp:=vp;');
  25497. Add(' vp:=vq;');
  25498. 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
  25499. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25500. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25501. Add(' vp;'); // ok in fpc and delphi
  25502. Add(' vp();');
  25503. // equal
  25504. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25505. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25506. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25507. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25508. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25509. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25510. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25511. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25512. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25513. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25514. // unequal
  25515. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25516. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25517. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25518. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25519. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25520. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25521. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25522. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25523. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25524. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25525. Add(' b:=Assigned(vp);');
  25526. ConvertProgram;
  25527. CheckSource('TestProcType_ProcedureDelphi',
  25528. LinesToStr([ // statements
  25529. 'this.DoIt = function() {',
  25530. '};',
  25531. 'this.b = false;',
  25532. 'this.vP = null;',
  25533. 'this.vQ = null;'
  25534. ]),
  25535. LinesToStr([ // $mod.$main
  25536. '$mod.vP = null;',
  25537. '$mod.vP = $mod.vP;',
  25538. '$mod.vP = $mod.vQ;',
  25539. '$mod.vP = $mod.DoIt;',
  25540. '$mod.vP = $mod.DoIt;',
  25541. '$mod.vP();',
  25542. '$mod.vP();',
  25543. '$mod.b = $mod.vP === null;',
  25544. '$mod.b = null === $mod.vP;',
  25545. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25546. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25547. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25548. '$mod.b = $mod.vP !== null;',
  25549. '$mod.b = null !== $mod.vP;',
  25550. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25551. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25552. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25553. '$mod.b = $mod.vP != null;',
  25554. '']));
  25555. end;
  25556. procedure TTestModule.TestProcType_AsParam;
  25557. begin
  25558. StartProgram(false);
  25559. Add('type');
  25560. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25561. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25562. Add('var vJ: tfuncint;');
  25563. Add('begin');
  25564. Add(' vg:=vg;');
  25565. Add(' vj:=vh;');
  25566. Add(' vi:=vi;');
  25567. Add(' doit(vg,vg,vg);');
  25568. Add(' doit(vh,vh,vj);');
  25569. Add(' doit(vi,vi,vi);');
  25570. Add(' doit(vj,vj,vj);');
  25571. Add('end;');
  25572. Add('var i: tfuncint;');
  25573. Add('begin');
  25574. Add(' doit(i,i,i);');
  25575. ConvertProgram;
  25576. CheckSource('TestProcType_AsParam',
  25577. LinesToStr([ // statements
  25578. 'this.DoIt = function (vG,vH,vI) {',
  25579. ' var vJ = null;',
  25580. ' vG = vG;',
  25581. ' vJ = vH;',
  25582. ' vI.set(vI.get());',
  25583. ' $mod.DoIt(vG, vG, {',
  25584. ' get: function () {',
  25585. ' return vG;',
  25586. ' },',
  25587. ' set: function (v) {',
  25588. ' vG = v;',
  25589. ' }',
  25590. ' });',
  25591. ' $mod.DoIt(vH, vH, {',
  25592. ' get: function () {',
  25593. ' return vJ;',
  25594. ' },',
  25595. ' set: function (v) {',
  25596. ' vJ = v;',
  25597. ' }',
  25598. ' });',
  25599. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25600. ' $mod.DoIt(vJ, vJ, {',
  25601. ' get: function () {',
  25602. ' return vJ;',
  25603. ' },',
  25604. ' set: function (v) {',
  25605. ' vJ = v;',
  25606. ' }',
  25607. ' });',
  25608. '};',
  25609. 'this.i = null;'
  25610. ]),
  25611. LinesToStr([
  25612. '$mod.DoIt($mod.i,$mod.i,{',
  25613. ' p: $mod,',
  25614. ' get: function () {',
  25615. ' return this.p.i;',
  25616. ' },',
  25617. ' set: function (v) {',
  25618. ' this.p.i = v;',
  25619. ' }',
  25620. '});'
  25621. ]));
  25622. end;
  25623. procedure TTestModule.TestProcType_MethodFPC;
  25624. begin
  25625. StartProgram(false);
  25626. Add('type');
  25627. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25628. Add(' TObject = class');
  25629. Add(' function DoIt(vA: longint = 1): longint;');
  25630. Add(' end;');
  25631. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25632. Add('begin');
  25633. Add('end;');
  25634. Add('var');
  25635. Add(' Obj: TObject;');
  25636. Add(' vP: tfuncint;');
  25637. Add(' b: boolean;');
  25638. Add('begin');
  25639. Add(' vp:[email protected];'); // ok in fpc and delphi
  25640. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25641. Add(' vp;'); // ok in fpc and delphi
  25642. Add(' vp();');
  25643. Add(' vp(2);');
  25644. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25645. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25646. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25647. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25648. ConvertProgram;
  25649. CheckSource('TestProcType_MethodFPC',
  25650. LinesToStr([ // statements
  25651. 'rtl.createClass(this, "TObject", null, function () {',
  25652. ' this.$init = function () {',
  25653. ' };',
  25654. ' this.$final = function () {',
  25655. ' };',
  25656. ' this.DoIt = function (vA) {',
  25657. ' var Result = 0;',
  25658. ' return Result;',
  25659. ' };',
  25660. '});',
  25661. 'this.Obj = null;',
  25662. 'this.vP = null;',
  25663. 'this.b = false;'
  25664. ]),
  25665. LinesToStr([
  25666. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25667. '$mod.vP(1);',
  25668. '$mod.vP(1);',
  25669. '$mod.vP(2);',
  25670. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25671. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25672. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25673. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25674. '']));
  25675. end;
  25676. procedure TTestModule.TestProcType_MethodDelphi;
  25677. begin
  25678. StartProgram(false);
  25679. Add([
  25680. '{$mode delphi}',
  25681. 'type',
  25682. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25683. ' TObject = class',
  25684. ' function DoIt(vA: longint = 1): longint;',
  25685. ' end;',
  25686. 'function TObject.DoIt(vA: longint = 1): longint;',
  25687. 'begin',
  25688. 'end;',
  25689. 'var',
  25690. ' Obj: TObject;',
  25691. ' vP: tfuncint;',
  25692. ' b: boolean;',
  25693. 'begin',
  25694. ' vp:[email protected];', // ok in fpc and delphi
  25695. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25696. ' vp;', // ok in fpc and delphi
  25697. ' vp();',
  25698. ' vp(2);',
  25699. //' b:[email protected];', // ok in fpc, illegal in delphi
  25700. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25701. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25702. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25703. '']);
  25704. ConvertProgram;
  25705. CheckSource('TestProcType_MethodDelphi',
  25706. LinesToStr([ // statements
  25707. 'rtl.createClass(this, "TObject", null, function () {',
  25708. ' this.$init = function () {',
  25709. ' };',
  25710. ' this.$final = function () {',
  25711. ' };',
  25712. ' this.DoIt = function (vA) {',
  25713. ' var Result = 0;',
  25714. ' return Result;',
  25715. ' };',
  25716. '});',
  25717. 'this.Obj = null;',
  25718. 'this.vP = null;',
  25719. 'this.b = false;'
  25720. ]),
  25721. LinesToStr([
  25722. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25723. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25724. '$mod.vP(1);',
  25725. '$mod.vP(1);',
  25726. '$mod.vP(2);',
  25727. '']));
  25728. end;
  25729. procedure TTestModule.TestProcType_PropertyFPC;
  25730. begin
  25731. StartProgram(false);
  25732. Add('type');
  25733. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25734. Add(' TObject = class');
  25735. Add(' FOnFoo: TFuncInt;');
  25736. Add(' function DoIt(vA: longint = 1): longint;');
  25737. Add(' function GetFoo: TFuncInt;');
  25738. Add(' procedure SetFoo(const Value: TFuncInt);');
  25739. Add(' function GetEvents(Index: longint): TFuncInt;');
  25740. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25741. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25742. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25743. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25744. Add(' end;');
  25745. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25746. Add('function tobject.getfoo: tfuncint; begin end;');
  25747. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25748. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25749. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25750. Add('var');
  25751. Add(' Obj: TObject;');
  25752. Add(' vP: tfuncint;');
  25753. Add(' b: boolean;');
  25754. Add('begin');
  25755. Add(' obj.onfoo:=nil;');
  25756. Add(' obj.onbar:=nil;');
  25757. Add(' obj.events[1]:=nil;');
  25758. Add(' obj.onfoo:=obj.onfoo;');
  25759. Add(' obj.onbar:=obj.onbar;');
  25760. Add(' obj.events[2]:=obj.events[3];');
  25761. Add(' obj.onfoo:[email protected];');
  25762. Add(' obj.onbar:[email protected];');
  25763. Add(' obj.events[4]:[email protected];');
  25764. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25765. //Add(' obj.onbar:=obj.doit;'); // delphi
  25766. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25767. Add(' obj.onfoo;');
  25768. Add(' obj.onbar;');
  25769. //Add(' obj.events[5];'); ToDo in pasresolver
  25770. Add(' obj.onfoo();');
  25771. Add(' obj.onbar();');
  25772. Add(' obj.events[6]();');
  25773. Add(' b:=obj.onfoo=nil;');
  25774. Add(' b:=obj.onbar=nil;');
  25775. Add(' b:=obj.events[7]=nil;');
  25776. Add(' b:=obj.onfoo<>nil;');
  25777. Add(' b:=obj.onbar<>nil;');
  25778. Add(' b:=obj.events[8]<>nil;');
  25779. Add(' b:=obj.onfoo=vp;');
  25780. Add(' b:=obj.onbar=vp;');
  25781. Add(' b:=obj.events[9]=vp;');
  25782. Add(' b:=obj.onfoo=obj.onfoo;');
  25783. Add(' b:=obj.onbar=obj.onfoo;');
  25784. Add(' b:=obj.events[10]=obj.onfoo;');
  25785. Add(' b:=obj.onfoo<>obj.onfoo;');
  25786. Add(' b:=obj.onbar<>obj.onfoo;');
  25787. Add(' b:=obj.events[11]<>obj.onfoo;');
  25788. Add(' b:[email protected];');
  25789. Add(' b:[email protected];');
  25790. Add(' b:=obj.events[12][email protected];');
  25791. Add(' b:=obj.onfoo<>@obj.doit;');
  25792. Add(' b:=obj.onbar<>@obj.doit;');
  25793. Add(' b:=obj.events[12]<>@obj.doit;');
  25794. Add(' b:=Assigned(obj.onfoo);');
  25795. Add(' b:=Assigned(obj.onbar);');
  25796. Add(' b:=Assigned(obj.events[13]);');
  25797. ConvertProgram;
  25798. CheckSource('TestProcType_PropertyFPC',
  25799. LinesToStr([ // statements
  25800. 'rtl.createClass(this, "TObject", null, function () {',
  25801. ' this.$init = function () {',
  25802. ' this.FOnFoo = null;',
  25803. ' };',
  25804. ' this.$final = function () {',
  25805. ' this.FOnFoo = undefined;',
  25806. ' };',
  25807. ' this.DoIt = function (vA) {',
  25808. ' var Result = 0;',
  25809. ' return Result;',
  25810. ' };',
  25811. 'this.GetFoo = function () {',
  25812. ' var Result = null;',
  25813. ' return Result;',
  25814. '};',
  25815. 'this.SetFoo = function (Value) {',
  25816. '};',
  25817. 'this.GetEvents = function (Index) {',
  25818. ' var Result = null;',
  25819. ' return Result;',
  25820. '};',
  25821. 'this.SetEvents = function (Index, Value) {',
  25822. '};',
  25823. '});',
  25824. 'this.Obj = null;',
  25825. 'this.vP = null;',
  25826. 'this.b = false;'
  25827. ]),
  25828. LinesToStr([
  25829. '$mod.Obj.FOnFoo = null;',
  25830. '$mod.Obj.SetFoo(null);',
  25831. '$mod.Obj.SetEvents(1, null);',
  25832. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25833. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25834. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25835. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25836. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25837. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25838. '$mod.Obj.FOnFoo(1);',
  25839. '$mod.Obj.GetFoo();',
  25840. '$mod.Obj.FOnFoo(1);',
  25841. '$mod.Obj.GetFoo()(1);',
  25842. '$mod.Obj.GetEvents(6)(1);',
  25843. '$mod.b = $mod.Obj.FOnFoo === null;',
  25844. '$mod.b = $mod.Obj.GetFoo() === null;',
  25845. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25846. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25847. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25848. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25849. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25850. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25851. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25852. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25853. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25854. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25855. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25856. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25857. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25858. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25859. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25860. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25861. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25862. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25863. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25864. '$mod.b = $mod.Obj.FOnFoo != null;',
  25865. '$mod.b = $mod.Obj.GetFoo() != null;',
  25866. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25867. '']));
  25868. end;
  25869. procedure TTestModule.TestProcType_PropertyDelphi;
  25870. begin
  25871. StartProgram(false);
  25872. Add('{$mode delphi}');
  25873. Add('type');
  25874. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25875. Add(' TObject = class');
  25876. Add(' FOnFoo: TFuncInt;');
  25877. Add(' function DoIt(vA: longint = 1): longint;');
  25878. Add(' function GetFoo: TFuncInt;');
  25879. Add(' procedure SetFoo(const Value: TFuncInt);');
  25880. Add(' function GetEvents(Index: longint): TFuncInt;');
  25881. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25882. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25883. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25884. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25885. Add(' end;');
  25886. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25887. Add('function tobject.getfoo: tfuncint; begin end;');
  25888. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25889. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25890. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25891. Add('var');
  25892. Add(' Obj: TObject;');
  25893. Add(' vP: tfuncint;');
  25894. Add(' b: boolean;');
  25895. Add('begin');
  25896. Add(' obj.onfoo:=nil;');
  25897. Add(' obj.onbar:=nil;');
  25898. Add(' obj.events[1]:=nil;');
  25899. Add(' obj.onfoo:=obj.onfoo;');
  25900. Add(' obj.onbar:=obj.onbar;');
  25901. Add(' obj.events[2]:=obj.events[3];');
  25902. Add(' obj.onfoo:[email protected];');
  25903. Add(' obj.onbar:[email protected];');
  25904. Add(' obj.events[4]:[email protected];');
  25905. Add(' obj.onfoo:=obj.doit;'); // delphi
  25906. Add(' obj.onbar:=obj.doit;'); // delphi
  25907. Add(' obj.events[4]:=obj.doit;'); // delphi
  25908. Add(' obj.onfoo;');
  25909. Add(' obj.onbar;');
  25910. //Add(' obj.events[5];'); ToDo in pasresolver
  25911. Add(' obj.onfoo();');
  25912. Add(' obj.onbar();');
  25913. Add(' obj.events[6]();');
  25914. //Add(' b:=obj.onfoo=nil;'); // fpc
  25915. //Add(' b:=obj.onbar=nil;'); // fpc
  25916. //Add(' b:=obj.events[7]=nil;'); // fpc
  25917. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25918. //Add(' b:=obj.onbar<>nil;'); // fpc
  25919. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25920. Add(' b:=obj.onfoo=vp;');
  25921. Add(' b:=obj.onbar=vp;');
  25922. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25923. Add(' b:=obj.onfoo=obj.onfoo;');
  25924. Add(' b:=obj.onbar=obj.onfoo;');
  25925. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25926. Add(' b:=obj.onfoo<>obj.onfoo;');
  25927. Add(' b:=obj.onbar<>obj.onfoo;');
  25928. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25929. //Add(' b:[email protected];'); // fpc
  25930. //Add(' b:[email protected];'); // fpc
  25931. //Add(' b:=obj.events[12][email protected];'); // fpc
  25932. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25933. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25934. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25935. Add(' b:=Assigned(obj.onfoo);');
  25936. Add(' b:=Assigned(obj.onbar);');
  25937. Add(' b:=Assigned(obj.events[13]);');
  25938. ConvertProgram;
  25939. CheckSource('TestProcType_PropertyDelphi',
  25940. LinesToStr([ // statements
  25941. 'rtl.createClass(this, "TObject", null, function () {',
  25942. ' this.$init = function () {',
  25943. ' this.FOnFoo = null;',
  25944. ' };',
  25945. ' this.$final = function () {',
  25946. ' this.FOnFoo = undefined;',
  25947. ' };',
  25948. ' this.DoIt = function (vA) {',
  25949. ' var Result = 0;',
  25950. ' return Result;',
  25951. ' };',
  25952. 'this.GetFoo = function () {',
  25953. ' var Result = null;',
  25954. ' return Result;',
  25955. '};',
  25956. 'this.SetFoo = function (Value) {',
  25957. '};',
  25958. 'this.GetEvents = function (Index) {',
  25959. ' var Result = null;',
  25960. ' return Result;',
  25961. '};',
  25962. 'this.SetEvents = function (Index, Value) {',
  25963. '};',
  25964. '});',
  25965. 'this.Obj = null;',
  25966. 'this.vP = null;',
  25967. 'this.b = false;'
  25968. ]),
  25969. LinesToStr([
  25970. '$mod.Obj.FOnFoo = null;',
  25971. '$mod.Obj.SetFoo(null);',
  25972. '$mod.Obj.SetEvents(1, null);',
  25973. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25974. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25975. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25976. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25977. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25978. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25979. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25980. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25981. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25982. '$mod.Obj.FOnFoo(1);',
  25983. '$mod.Obj.GetFoo();',
  25984. '$mod.Obj.FOnFoo(1);',
  25985. '$mod.Obj.GetFoo()(1);',
  25986. '$mod.Obj.GetEvents(6)(1);',
  25987. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25988. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25989. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25990. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25991. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25992. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25993. '$mod.b = $mod.Obj.FOnFoo != null;',
  25994. '$mod.b = $mod.Obj.GetFoo() != null;',
  25995. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25996. '']));
  25997. end;
  25998. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25999. begin
  26000. StartProgram(false);
  26001. Add('type');
  26002. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26003. Add(' TObject = class');
  26004. Add(' FOnFoo: TFuncInt;');
  26005. Add(' function DoIt(vA: longint = 1): longint;');
  26006. Add(' function GetFoo: TFuncInt;');
  26007. Add(' procedure SetFoo(const Value: TFuncInt);');
  26008. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26009. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26010. Add(' end;');
  26011. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26012. Add('function tobject.getfoo: tfuncint; begin end;');
  26013. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26014. Add('var');
  26015. Add(' Obj: TObject;');
  26016. Add(' vP: tfuncint;');
  26017. Add(' b: boolean;');
  26018. Add('begin');
  26019. Add('with obj do begin');
  26020. Add(' fonfoo:=nil;');
  26021. Add(' onfoo:=nil;');
  26022. Add(' onbar:=nil;');
  26023. Add(' fonfoo:=fonfoo;');
  26024. Add(' onfoo:=onfoo;');
  26025. Add(' onbar:=onbar;');
  26026. Add(' fonfoo:=@doit;');
  26027. Add(' onfoo:=@doit;');
  26028. Add(' onbar:=@doit;');
  26029. //Add(' fonfoo:=doit;'); // delphi
  26030. //Add(' onfoo:=doit;'); // delphi
  26031. //Add(' onbar:=doit;'); // delphi
  26032. Add(' fonfoo;');
  26033. Add(' onfoo;');
  26034. Add(' onbar;');
  26035. Add(' fonfoo();');
  26036. Add(' onfoo();');
  26037. Add(' onbar();');
  26038. Add(' b:=fonfoo=nil;');
  26039. Add(' b:=onfoo=nil;');
  26040. Add(' b:=onbar=nil;');
  26041. Add(' b:=fonfoo<>nil;');
  26042. Add(' b:=onfoo<>nil;');
  26043. Add(' b:=onbar<>nil;');
  26044. Add(' b:=fonfoo=vp;');
  26045. Add(' b:=onfoo=vp;');
  26046. Add(' b:=onbar=vp;');
  26047. Add(' b:=fonfoo=fonfoo;');
  26048. Add(' b:=onfoo=onfoo;');
  26049. Add(' b:=onbar=onfoo;');
  26050. Add(' b:=fonfoo<>fonfoo;');
  26051. Add(' b:=onfoo<>onfoo;');
  26052. Add(' b:=onbar<>onfoo;');
  26053. Add(' b:=fonfoo=@doit;');
  26054. Add(' b:=onfoo=@doit;');
  26055. Add(' b:=onbar=@doit;');
  26056. Add(' b:=fonfoo<>@doit;');
  26057. Add(' b:=onfoo<>@doit;');
  26058. Add(' b:=onbar<>@doit;');
  26059. Add(' b:=Assigned(fonfoo);');
  26060. Add(' b:=Assigned(onfoo);');
  26061. Add(' b:=Assigned(onbar);');
  26062. Add('end;');
  26063. ConvertProgram;
  26064. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26065. LinesToStr([ // statements
  26066. 'rtl.createClass(this, "TObject", null, function () {',
  26067. ' this.$init = function () {',
  26068. ' this.FOnFoo = null;',
  26069. ' };',
  26070. ' this.$final = function () {',
  26071. ' this.FOnFoo = undefined;',
  26072. ' };',
  26073. ' this.DoIt = function (vA) {',
  26074. ' var Result = 0;',
  26075. ' return Result;',
  26076. ' };',
  26077. ' this.GetFoo = function () {',
  26078. ' var Result = null;',
  26079. ' return Result;',
  26080. ' };',
  26081. ' this.SetFoo = function (Value) {',
  26082. ' };',
  26083. '});',
  26084. 'this.Obj = null;',
  26085. 'this.vP = null;',
  26086. 'this.b = false;'
  26087. ]),
  26088. LinesToStr([
  26089. 'var $with = $mod.Obj;',
  26090. '$with.FOnFoo = null;',
  26091. '$with.FOnFoo = null;',
  26092. '$with.SetFoo(null);',
  26093. '$with.FOnFoo = $with.FOnFoo;',
  26094. '$with.FOnFoo = $with.FOnFoo;',
  26095. '$with.SetFoo($with.GetFoo());',
  26096. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26097. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26098. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26099. '$with.FOnFoo(1);',
  26100. '$with.FOnFoo(1);',
  26101. '$with.GetFoo();',
  26102. '$with.FOnFoo(1);',
  26103. '$with.FOnFoo(1);',
  26104. '$with.GetFoo()(1);',
  26105. '$mod.b = $with.FOnFoo === null;',
  26106. '$mod.b = $with.FOnFoo === null;',
  26107. '$mod.b = $with.GetFoo() === null;',
  26108. '$mod.b = $with.FOnFoo !== null;',
  26109. '$mod.b = $with.FOnFoo !== null;',
  26110. '$mod.b = $with.GetFoo() !== null;',
  26111. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26112. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26113. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26114. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26115. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26116. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26117. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26118. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26119. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26120. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26121. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26122. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26123. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26124. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26125. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26126. '$mod.b = $with.FOnFoo != null;',
  26127. '$mod.b = $with.FOnFoo != null;',
  26128. '$mod.b = $with.GetFoo() != null;',
  26129. '']));
  26130. end;
  26131. procedure TTestModule.TestProcType_Nested;
  26132. begin
  26133. StartProgram(false);
  26134. Add([
  26135. 'type',
  26136. ' TProcInt = procedure(vI: longint = 1);',
  26137. 'procedure DoIt(vJ: longint);',
  26138. 'var aProc: TProcInt;',
  26139. ' b: boolean;',
  26140. ' procedure Sub(vK: longint);',
  26141. ' var aSub: TProcInt;',
  26142. ' procedure SubSub(vK: longint);',
  26143. ' var aSubSub: TProcInt;',
  26144. ' begin;',
  26145. ' aProc:=@DoIt;',
  26146. ' aSub:=@DoIt;',
  26147. ' aSubSub:=@DoIt;',
  26148. ' aProc:=@Sub;',
  26149. ' aSub:=@Sub;',
  26150. ' aSubSub:=@Sub;',
  26151. ' aProc:=@SubSub;',
  26152. ' aSub:=@SubSub;',
  26153. ' aSubSub:=@SubSub;',
  26154. ' end;',
  26155. ' begin;',
  26156. ' end;',
  26157. 'begin;',
  26158. ' aProc:=@Sub;',
  26159. ' b:=aProc=@Sub;',
  26160. ' b:=@Sub=aProc;',
  26161. 'end;',
  26162. 'begin',
  26163. '']);
  26164. ConvertProgram;
  26165. CheckSource('TestProcType_Nested',
  26166. LinesToStr([ // statements
  26167. 'this.DoIt = function (vJ) {',
  26168. ' var aProc = null;',
  26169. ' var b = false;',
  26170. ' function Sub(vK) {',
  26171. ' var aSub = null;',
  26172. ' function SubSub(vK) {',
  26173. ' var aSubSub = null;',
  26174. ' aProc = $mod.DoIt;',
  26175. ' aSub = $mod.DoIt;',
  26176. ' aSubSub = $mod.DoIt;',
  26177. ' aProc = Sub;',
  26178. ' aSub = Sub;',
  26179. ' aSubSub = Sub;',
  26180. ' aProc = SubSub;',
  26181. ' aSub = SubSub;',
  26182. ' aSubSub = SubSub;',
  26183. ' };',
  26184. ' };',
  26185. ' aProc = Sub;',
  26186. ' b = rtl.eqCallback(aProc, Sub);',
  26187. ' b = rtl.eqCallback(Sub, aProc);',
  26188. '};',
  26189. '']),
  26190. LinesToStr([ // $mod.$main
  26191. '']));
  26192. end;
  26193. procedure TTestModule.TestProcType_NestedOfObject;
  26194. begin
  26195. StartProgram(false);
  26196. Add([
  26197. 'type',
  26198. ' TProcInt = procedure(vI: longint = 1) of object;',
  26199. ' TObject = class',
  26200. ' procedure DoIt(vJ: longint);',
  26201. ' end;',
  26202. 'procedure TObject.DoIt(vJ: longint);',
  26203. 'var aProc: TProcInt;',
  26204. ' b: boolean;',
  26205. ' procedure Sub(vK: longint);',
  26206. ' var aSub: TProcInt;',
  26207. ' procedure SubSub(vK: longint);',
  26208. ' var aSubSub: TProcInt;',
  26209. ' begin;',
  26210. ' aProc:=@DoIt;',
  26211. ' aSub:=@DoIt;',
  26212. ' aSubSub:=@DoIt;',
  26213. ' aProc:=@Sub;',
  26214. ' aSub:=@Sub;',
  26215. ' aSubSub:=@Sub;',
  26216. ' aProc:=@SubSub;',
  26217. ' aSub:=@SubSub;',
  26218. ' aSubSub:=@SubSub;',
  26219. ' end;',
  26220. ' begin;',
  26221. ' end;',
  26222. 'begin;',
  26223. ' aProc:=@Sub;',
  26224. ' b:=aProc=@Sub;',
  26225. ' b:=@Sub=aProc;',
  26226. 'end;',
  26227. 'begin',
  26228. '']);
  26229. ConvertProgram;
  26230. CheckSource('TestProcType_Nested',
  26231. LinesToStr([ // statements
  26232. 'rtl.createClass(this, "TObject", null, function () {',
  26233. ' this.$init = function () {',
  26234. ' };',
  26235. ' this.$final = function () {',
  26236. ' };',
  26237. ' this.DoIt = function (vJ) {',
  26238. ' var $Self = this;',
  26239. ' var aProc = null;',
  26240. ' var b = false;',
  26241. ' function Sub(vK) {',
  26242. ' var aSub = null;',
  26243. ' function SubSub(vK) {',
  26244. ' var aSubSub = null;',
  26245. ' aProc = rtl.createCallback($Self, "DoIt");',
  26246. ' aSub = rtl.createCallback($Self, "DoIt");',
  26247. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26248. ' aProc = Sub;',
  26249. ' aSub = Sub;',
  26250. ' aSubSub = Sub;',
  26251. ' aProc = SubSub;',
  26252. ' aSub = SubSub;',
  26253. ' aSubSub = SubSub;',
  26254. ' };',
  26255. ' };',
  26256. ' aProc = Sub;',
  26257. ' b = rtl.eqCallback(aProc, Sub);',
  26258. ' b = rtl.eqCallback(Sub, aProc);',
  26259. ' };',
  26260. '});',
  26261. '']),
  26262. LinesToStr([ // $mod.$main
  26263. '']));
  26264. end;
  26265. procedure TTestModule.TestProcType_ReferenceToProc;
  26266. begin
  26267. StartProgram(false);
  26268. Add([
  26269. 'type',
  26270. ' TProcRef = reference to procedure(i: longint = 0);',
  26271. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26272. 'var',
  26273. ' p: TProcRef;',
  26274. ' f: TFuncRef;',
  26275. 'procedure DoIt(i: longint);',
  26276. 'begin',
  26277. 'end;',
  26278. 'function GetIt(i: longint): longint;',
  26279. 'begin',
  26280. ' p:=@DoIt;',
  26281. ' f:=@GetIt;',
  26282. ' f;',
  26283. ' f();',
  26284. ' f(1);',
  26285. 'end;',
  26286. 'begin',
  26287. ' p:=@DoIt;',
  26288. ' f:=@GetIt;',
  26289. ' f;',
  26290. ' f();',
  26291. ' f(1);',
  26292. ' p:=TProcRef(f);',
  26293. '']);
  26294. ConvertProgram;
  26295. CheckSource('TestProcType_ReferenceToProc',
  26296. LinesToStr([ // statements
  26297. 'this.p = null;',
  26298. 'this.f = null;',
  26299. 'this.DoIt = function (i) {',
  26300. '};',
  26301. 'this.GetIt = function (i) {',
  26302. ' var Result = 0;',
  26303. ' $mod.p = $mod.DoIt;',
  26304. ' $mod.f = $mod.GetIt;',
  26305. ' $mod.f(0);',
  26306. ' $mod.f(0);',
  26307. ' $mod.f(1);',
  26308. ' return Result;',
  26309. '};',
  26310. '']),
  26311. LinesToStr([ // $mod.$main
  26312. '$mod.p = $mod.DoIt;',
  26313. '$mod.f = $mod.GetIt;',
  26314. '$mod.f(0);',
  26315. '$mod.f(0);',
  26316. '$mod.f(1);',
  26317. '$mod.p = $mod.f;',
  26318. '']));
  26319. end;
  26320. procedure TTestModule.TestProcType_ReferenceToMethod;
  26321. begin
  26322. StartProgram(false);
  26323. Add([
  26324. 'type',
  26325. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26326. ' TObject = class',
  26327. ' function Grow(s: longint): longint;',
  26328. ' end;',
  26329. 'var',
  26330. ' f: tfuncref;',
  26331. 'function tobject.grow(s: longint): longint;',
  26332. ' function GrowSub(i: longint): longint;',
  26333. ' begin',
  26334. ' f:=@grow;',
  26335. ' f:=@growsub;',
  26336. ' end;',
  26337. 'begin',
  26338. ' f:=@grow;',
  26339. ' f:=@growsub;',
  26340. 'end;',
  26341. 'begin',
  26342. '']);
  26343. ConvertProgram;
  26344. CheckSource('TestProcType_ReferenceToMethod',
  26345. LinesToStr([ // statements
  26346. 'rtl.createClass(this, "TObject", null, function () {',
  26347. ' this.$init = function () {',
  26348. ' };',
  26349. ' this.$final = function () {',
  26350. ' };',
  26351. ' this.Grow = function (s) {',
  26352. ' var $Self = this;',
  26353. ' var Result = 0;',
  26354. ' function GrowSub(i) {',
  26355. ' var Result = 0;',
  26356. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26357. ' $mod.f = GrowSub;',
  26358. ' return Result;',
  26359. ' };',
  26360. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26361. ' $mod.f = GrowSub;',
  26362. ' return Result;',
  26363. ' };',
  26364. '});',
  26365. 'this.f = null;',
  26366. '']),
  26367. LinesToStr([ // $mod.$main
  26368. '']));
  26369. end;
  26370. procedure TTestModule.TestProcType_Typecast;
  26371. begin
  26372. StartProgram(false);
  26373. Add([
  26374. 'type',
  26375. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26376. ' TEvent = procedure of object;',
  26377. ' TGetter = function:longint of object;',
  26378. ' TProcA = procedure(i: longint);',
  26379. ' TFuncB = function(i, j: longint): longint;',
  26380. 'procedure DoIt(); varargs; begin end;',
  26381. 'var',
  26382. ' Notify: tnotifyevent;',
  26383. ' Event: tevent;',
  26384. ' Getter: tgetter;',
  26385. ' ProcA: tproca;',
  26386. ' FuncB: tfuncb;',
  26387. ' p: pointer;',
  26388. 'begin',
  26389. ' notify:=tnotifyevent(event);',
  26390. ' event:=tevent(event);',
  26391. ' event:=tevent(notify);',
  26392. ' event:=tevent(getter);',
  26393. ' event:=tevent(proca);',
  26394. ' proca:=tproca(funcb);',
  26395. ' funcb:=tfuncb(funcb);',
  26396. ' funcb:=tfuncb(proca);',
  26397. ' funcb:=tfuncb(getter);',
  26398. ' proca:=tproca(p);',
  26399. ' funcb:=tfuncb(p);',
  26400. ' getter:=tgetter(p);',
  26401. ' p:=pointer(notify);',
  26402. ' p:=notify;',
  26403. ' p:=pointer(proca);',
  26404. ' p:=proca;',
  26405. ' p:=pointer(funcb);',
  26406. ' p:=funcb;',
  26407. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26408. '']);
  26409. ConvertProgram;
  26410. CheckSource('TestProcType_Typecast',
  26411. LinesToStr([ // statements
  26412. 'this.DoIt = function () {',
  26413. '};',
  26414. 'this.Notify = null;',
  26415. 'this.Event = null;',
  26416. 'this.Getter = null;',
  26417. 'this.ProcA = null;',
  26418. 'this.FuncB = null;',
  26419. 'this.p = null;',
  26420. '']),
  26421. LinesToStr([ // $mod.$main
  26422. '$mod.Notify = $mod.Event;',
  26423. '$mod.Event = $mod.Event;',
  26424. '$mod.Event = $mod.Notify;',
  26425. '$mod.Event = $mod.Getter;',
  26426. '$mod.Event = $mod.ProcA;',
  26427. '$mod.ProcA = $mod.FuncB;',
  26428. '$mod.FuncB = $mod.FuncB;',
  26429. '$mod.FuncB = $mod.ProcA;',
  26430. '$mod.FuncB = $mod.Getter;',
  26431. '$mod.ProcA = $mod.p;',
  26432. '$mod.FuncB = $mod.p;',
  26433. '$mod.Getter = $mod.p;',
  26434. '$mod.p = $mod.Notify;',
  26435. '$mod.p = $mod.Notify;',
  26436. '$mod.p = $mod.ProcA;',
  26437. '$mod.p = $mod.ProcA;',
  26438. '$mod.p = $mod.FuncB;',
  26439. '$mod.p = $mod.FuncB;',
  26440. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26441. '']));
  26442. end;
  26443. procedure TTestModule.TestProcType_PassProcToUntyped;
  26444. begin
  26445. StartProgram(false);
  26446. Add([
  26447. 'type',
  26448. ' TEvent = procedure of object;',
  26449. ' TFunc = function: longint;',
  26450. 'procedure DoIt(); varargs; begin end;',
  26451. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26452. 'var',
  26453. ' Event: tevent;',
  26454. ' Func: TFunc;',
  26455. 'begin',
  26456. ' doit(event,func);',
  26457. ' dosome(event,event,event);',
  26458. ' dosome(func,func,func);',
  26459. '']);
  26460. ConvertProgram;
  26461. CheckSource('TestProcType_PassProcToUntyped',
  26462. LinesToStr([ // statements
  26463. 'this.DoIt = function () {',
  26464. '};',
  26465. 'this.DoSome = function (a, b, p) {',
  26466. '};',
  26467. 'this.Event = null;',
  26468. 'this.Func = null;',
  26469. '']),
  26470. LinesToStr([ // $mod.$main
  26471. '$mod.DoIt($mod.Event, $mod.Func);',
  26472. '$mod.DoSome($mod.Event, {',
  26473. ' p: $mod,',
  26474. ' get: function () {',
  26475. ' return this.p.Event;',
  26476. ' },',
  26477. ' set: function (v) {',
  26478. ' this.p.Event = v;',
  26479. ' }',
  26480. '}, $mod.Event);',
  26481. '$mod.DoSome($mod.Func, {',
  26482. ' p: $mod,',
  26483. ' get: function () {',
  26484. ' return this.p.Func;',
  26485. ' },',
  26486. ' set: function (v) {',
  26487. ' this.p.Func = v;',
  26488. ' }',
  26489. '}, $mod.Func);',
  26490. '']));
  26491. end;
  26492. procedure TTestModule.TestProcType_PassProcToArray;
  26493. begin
  26494. StartProgram(false);
  26495. Add([
  26496. 'type',
  26497. ' TFunc = function: longint;',
  26498. ' TArrFunc = array of TFunc;',
  26499. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26500. 'function GetIt: longint; begin end;',
  26501. 'var',
  26502. ' Func: tfunc;',
  26503. 'begin',
  26504. ' doit([]);',
  26505. ' doit([@GetIt]);',
  26506. ' doit([Func]);',
  26507. '']);
  26508. ConvertProgram;
  26509. CheckSource('TestProcType_PassProcToArray',
  26510. LinesToStr([ // statements
  26511. 'this.DoIt = function (Arr) {',
  26512. '};',
  26513. 'this.GetIt = function () {',
  26514. ' var Result = 0;',
  26515. ' return Result;',
  26516. '};',
  26517. 'this.Func = null;',
  26518. '']),
  26519. LinesToStr([ // $mod.$main
  26520. '$mod.DoIt([]);',
  26521. '$mod.DoIt([$mod.GetIt]);',
  26522. '$mod.DoIt([$mod.Func]);',
  26523. '']));
  26524. end;
  26525. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26526. begin
  26527. StartProgram(false);
  26528. Add([
  26529. '{$modeswitch externalclass}',
  26530. 'type',
  26531. ' TProc = reference to procedure(i: longint); safecall;',
  26532. ' TEvent = procedure(i: longint) of object; safecall;',
  26533. ' TExtA = class external name ''ExtObj''',
  26534. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26535. ' procedure DoSome(Id: longint = 1);',
  26536. ' procedure SetOnClick(const e: TEvent);',
  26537. ' property OnClick: TEvent write SetOnClick;',
  26538. ' class procedure Fly(Id: longint = 1); static;',
  26539. ' procedure SetOnShow(const p: TProc);',
  26540. ' property OnShow: TProc write SetOnShow;',
  26541. ' end;',
  26542. 'procedure Run(i: longint = 1);',
  26543. 'begin',
  26544. 'end;',
  26545. 'var',
  26546. ' Obj: texta;',
  26547. ' e: TEvent;',
  26548. ' p: TProc;',
  26549. 'begin',
  26550. ' e:=e;',
  26551. ' e:[email protected];',
  26552. ' e:[email protected];',
  26553. ' e:=TEvent(@obj.dosome);', // no safecall
  26554. ' obj.OnClick:[email protected];',
  26555. ' obj.OnClick:[email protected];',
  26556. ' obj.setonclick(@obj.doit);',
  26557. ' obj.setonclick(@obj.dosome);',
  26558. ' p:=@Run;',
  26559. ' p:[email protected];',
  26560. ' obj.OnShow:=@Run;',
  26561. ' obj.OnShow:[email protected];',
  26562. ' obj.setOnShow(@Run);',
  26563. ' obj.setOnShow(@TExtA.Fly);',
  26564. ' with obj do begin',
  26565. ' e:=@doit;',
  26566. ' e:=@dosome;',
  26567. ' OnClick:=@doit;',
  26568. ' OnClick:=@dosome;',
  26569. ' setonclick(@doit);',
  26570. ' setonclick(@dosome);',
  26571. ' OnShow:=@Run;',
  26572. ' setOnShow(@Run);',
  26573. ' end;']);
  26574. ConvertProgram;
  26575. CheckSource('TestProcType_SafeCallObjFPC',
  26576. LinesToStr([ // statements
  26577. 'this.Run = function (i) {',
  26578. '};',
  26579. 'this.Obj = null;',
  26580. 'this.e = null;',
  26581. 'this.p = null;',
  26582. '']),
  26583. LinesToStr([ // $mod.$main
  26584. '$mod.e = $mod.e;',
  26585. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26586. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26587. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26588. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26589. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26590. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26591. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26592. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26593. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26594. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26595. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26596. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26597. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26598. 'var $with = $mod.Obj;',
  26599. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26600. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26601. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26602. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26603. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26604. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26605. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26606. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26607. '']));
  26608. end;
  26609. procedure TTestModule.TestProcType_SafeCallDelphi;
  26610. begin
  26611. StartProgram(false);
  26612. Add([
  26613. '{$mode delphi}',
  26614. '{$modeswitch externalclass}',
  26615. 'type',
  26616. ' TProc = reference to procedure(i: longint); safecall;',
  26617. ' TEvent = procedure(i: longint) of object; safecall;',
  26618. ' TExtA = class external name ''ExtObj''',
  26619. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26620. ' procedure DoSome(Id: longint = 1);',
  26621. ' procedure SetOnClick(const e: TEvent);',
  26622. ' property OnClick: TEvent write SetOnClick;',
  26623. ' class procedure Fly(Id: longint = 1); static;',
  26624. ' procedure SetOnShow(const p: TProc);',
  26625. ' property OnShow: TProc write SetOnShow;',
  26626. ' end;',
  26627. 'procedure Run(i: longint = 1);',
  26628. 'begin',
  26629. 'end;',
  26630. 'var',
  26631. ' Obj: texta;',
  26632. ' e: TEvent;',
  26633. ' p: TProc;',
  26634. 'begin',
  26635. ' e:=e;',
  26636. ' e:=obj.doit;',
  26637. ' e:=obj.dosome;',
  26638. ' e:=TEvent(@obj.dosome);', // no safecall
  26639. ' obj.OnClick:=obj.doit;',
  26640. ' obj.OnClick:=obj.dosome;',
  26641. ' obj.setonclick(obj.doit);',
  26642. ' obj.setonclick(obj.dosome);',
  26643. ' p:=Run;',
  26644. ' p:=TExtA.Fly;',
  26645. ' obj.OnShow:=Run;',
  26646. ' obj.OnShow:=TExtA.Fly;',
  26647. ' obj.setOnShow(Run);',
  26648. ' obj.setOnShow(TExtA.Fly);',
  26649. ' with obj do begin',
  26650. ' e:=doit;',
  26651. ' e:=dosome;',
  26652. ' OnClick:=doit;',
  26653. ' OnClick:=dosome;',
  26654. ' setonclick(doit);',
  26655. ' setonclick(dosome);',
  26656. ' OnShow:=@Run;',
  26657. ' setOnShow(@Run);',
  26658. ' end;']);
  26659. ConvertProgram;
  26660. CheckSource('TestProcType_SafeCallDelphi',
  26661. LinesToStr([ // statements
  26662. 'this.Run = function (i) {',
  26663. '};',
  26664. 'this.Obj = null;',
  26665. 'this.e = null;',
  26666. 'this.p = null;',
  26667. '']),
  26668. LinesToStr([ // $mod.$main
  26669. '$mod.e = $mod.e;',
  26670. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26671. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26672. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26673. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26674. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26675. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26676. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26677. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26678. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26679. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26680. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26681. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26682. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26683. 'var $with = $mod.Obj;',
  26684. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26685. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26686. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26687. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26688. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26689. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26690. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26691. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26692. '']));
  26693. end;
  26694. procedure TTestModule.TestPointer;
  26695. begin
  26696. StartProgram(false);
  26697. Add(['type',
  26698. ' TObject = class end;',
  26699. ' TClass = class of TObject;',
  26700. ' TArrInt = array of longint;',
  26701. 'const',
  26702. ' n = nil;',
  26703. 'var',
  26704. ' v: jsvalue;',
  26705. ' Obj: tobject;',
  26706. ' C: tclass;',
  26707. ' a: tarrint;',
  26708. ' p: Pointer = nil;',
  26709. ' s: string;',
  26710. 'begin',
  26711. ' p:=p;',
  26712. ' p:=nil;',
  26713. ' if p=nil then;',
  26714. ' if nil=p then;',
  26715. ' if Assigned(p) then;',
  26716. ' p:=Pointer(v);',
  26717. ' p:=obj;',
  26718. ' p:=c;',
  26719. ' p:=a;',
  26720. ' p:=tobject;',
  26721. ' obj:=TObject(p);',
  26722. ' c:=TClass(p);',
  26723. ' a:=TArrInt(p);',
  26724. ' p:=n;',
  26725. ' p:=Pointer(a);',
  26726. ' p:=pointer(s);',
  26727. ' s:=string(p);',
  26728. '']);
  26729. ConvertProgram;
  26730. CheckSource('TestPointer',
  26731. LinesToStr([ // statements
  26732. 'rtl.createClass(this, "TObject", null, function () {',
  26733. ' this.$init = function () {',
  26734. ' };',
  26735. ' this.$final = function () {',
  26736. ' };',
  26737. '});',
  26738. 'this.n = null;',
  26739. 'this.v = undefined;',
  26740. 'this.Obj = null;',
  26741. 'this.C = null;',
  26742. 'this.a = [];',
  26743. 'this.p = null;',
  26744. 'this.s = "";',
  26745. '']),
  26746. LinesToStr([ // $mod.$main
  26747. '$mod.p = $mod.p;',
  26748. '$mod.p = null;',
  26749. 'if ($mod.p === null) ;',
  26750. 'if (null === $mod.p) ;',
  26751. 'if ($mod.p != null) ;',
  26752. '$mod.p = $mod.v;',
  26753. '$mod.p = $mod.Obj;',
  26754. '$mod.p = $mod.C;',
  26755. '$mod.p = $mod.a;',
  26756. '$mod.p = $mod.TObject;',
  26757. '$mod.Obj = $mod.p;',
  26758. '$mod.C = $mod.p;',
  26759. '$mod.a = $mod.p;',
  26760. '$mod.p = null;',
  26761. '$mod.p = $mod.a;',
  26762. '$mod.p = $mod.s;',
  26763. '$mod.s = $mod.p;',
  26764. '']));
  26765. end;
  26766. procedure TTestModule.TestPointer_Proc;
  26767. begin
  26768. StartProgram(false);
  26769. Add('type');
  26770. Add(' TObject = class');
  26771. Add(' procedure DoIt; virtual; abstract;');
  26772. Add(' end;');
  26773. Add('procedure DoSome; begin end;');
  26774. Add('var');
  26775. Add(' o: TObject;');
  26776. Add(' p: Pointer;');
  26777. Add('begin');
  26778. Add(' p:=@DoSome;');
  26779. Add(' p:[email protected];');
  26780. ConvertProgram;
  26781. CheckSource('TestPointer_Proc',
  26782. LinesToStr([ // statements
  26783. 'rtl.createClass(this, "TObject", null, function () {',
  26784. ' this.$init = function () {',
  26785. ' };',
  26786. ' this.$final = function () {',
  26787. ' };',
  26788. '});',
  26789. 'this.DoSome = function () {',
  26790. '};',
  26791. 'this.o = null;',
  26792. 'this.p = null;',
  26793. '']),
  26794. LinesToStr([ // $mod.$main
  26795. '$mod.p = $mod.DoSome;',
  26796. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26797. '']));
  26798. end;
  26799. procedure TTestModule.TestPointer_AssignRecordFail;
  26800. begin
  26801. StartProgram(false);
  26802. Add('type');
  26803. Add(' TRec = record end;');
  26804. Add('var');
  26805. Add(' p: Pointer;');
  26806. Add(' r: TRec;');
  26807. Add('begin');
  26808. Add(' p:=r;');
  26809. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26810. nIncompatibleTypesGotExpected);
  26811. ConvertProgram;
  26812. end;
  26813. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26814. begin
  26815. StartProgram(false);
  26816. Add('type');
  26817. Add(' TArr = array[boolean] of longint;');
  26818. Add('var');
  26819. Add(' p: Pointer;');
  26820. Add(' a: TArr;');
  26821. Add('begin');
  26822. Add(' p:=a;');
  26823. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26824. nIncompatibleTypesGotExpected);
  26825. ConvertProgram;
  26826. end;
  26827. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26828. begin
  26829. StartProgram(false);
  26830. Add([
  26831. 'procedure DoIt(args: array of jsvalue); begin end;',
  26832. 'procedure DoAll; varargs; begin end;',
  26833. 'var',
  26834. ' v: jsvalue;',
  26835. 'begin',
  26836. ' DoIt([pointer(v)]);',
  26837. ' DoAll(pointer(v));',
  26838. '']);
  26839. ConvertProgram;
  26840. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26841. LinesToStr([ // statements
  26842. 'this.DoIt = function (args) {',
  26843. '};',
  26844. 'this.DoAll = function () {',
  26845. '};',
  26846. 'this.v = undefined;',
  26847. '']),
  26848. LinesToStr([ // $mod.$main
  26849. '$mod.DoIt([$mod.v]);',
  26850. '$mod.DoAll($mod.v);',
  26851. '']));
  26852. end;
  26853. procedure TTestModule.TestPointer_NonRecordFail;
  26854. begin
  26855. StartProgram(false);
  26856. Add([
  26857. 'type',
  26858. ' p = ^longint;',
  26859. 'begin',
  26860. '']);
  26861. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26862. ConvertProgram;
  26863. end;
  26864. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26865. begin
  26866. StartProgram(false);
  26867. Add([
  26868. 'procedure DoIt(p: ^longint); begin end;',
  26869. 'begin',
  26870. '']);
  26871. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26872. ConvertProgram;
  26873. end;
  26874. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26875. begin
  26876. StartProgram(false);
  26877. Add([
  26878. 'var p: ^longint;',
  26879. 'begin',
  26880. '']);
  26881. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26882. ConvertProgram;
  26883. end;
  26884. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26885. begin
  26886. StartProgram(false);
  26887. Add([
  26888. 'function DoIt: ^longint; begin end;',
  26889. 'begin',
  26890. '']);
  26891. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26892. ConvertProgram;
  26893. end;
  26894. procedure TTestModule.TestPointer_AddrOperatorFail;
  26895. begin
  26896. StartProgram(false);
  26897. Add([
  26898. 'var i: longint;',
  26899. 'begin',
  26900. ' if @i=nil then ;',
  26901. '']);
  26902. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26903. ConvertProgram;
  26904. end;
  26905. procedure TTestModule.TestPointer_ArrayParamsFail;
  26906. begin
  26907. StartProgram(false);
  26908. Add([
  26909. 'var',
  26910. ' p: Pointer;',
  26911. 'begin',
  26912. ' p:=p[1];',
  26913. '']);
  26914. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26915. ConvertProgram;
  26916. end;
  26917. procedure TTestModule.TestPointer_PointerAddFail;
  26918. begin
  26919. StartProgram(false);
  26920. Add([
  26921. 'var',
  26922. ' p: Pointer;',
  26923. 'begin',
  26924. ' p:=p+1;',
  26925. '']);
  26926. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26927. ConvertProgram;
  26928. end;
  26929. procedure TTestModule.TestPointer_IncPointerFail;
  26930. begin
  26931. StartProgram(false);
  26932. Add([
  26933. 'var',
  26934. ' p: Pointer;',
  26935. 'begin',
  26936. ' inc(p,1);',
  26937. '']);
  26938. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26939. nIncompatibleTypeArgNo);
  26940. ConvertProgram;
  26941. end;
  26942. procedure TTestModule.TestPointer_Record;
  26943. begin
  26944. StartProgram(false);
  26945. Add([
  26946. 'type',
  26947. ' TRec = record x: longint; end;',
  26948. ' PRec = ^TRec;',
  26949. 'var',
  26950. ' r: TRec;',
  26951. ' p: PRec;',
  26952. ' q: ^TRec;',
  26953. ' Ptr: pointer;',
  26954. 'begin',
  26955. ' new(p);',
  26956. ' p:=@r;',
  26957. ' r:=p^;',
  26958. ' r.x:=p^.x;',
  26959. ' p^.x:=r.x;',
  26960. ' if p^.x=3 then ;',
  26961. ' if 4=p^.x then ;',
  26962. ' dispose(p);',
  26963. ' new(q);',
  26964. ' dispose(q);',
  26965. ' Ptr:=p;',
  26966. ' p:=PRec(ptr);',
  26967. '']);
  26968. ConvertProgram;
  26969. CheckSource('TestPointer_Record',
  26970. LinesToStr([ // statements
  26971. 'rtl.recNewT(this, "TRec", function () {',
  26972. ' this.x = 0;',
  26973. ' this.$eq = function (b) {',
  26974. ' return this.x === b.x;',
  26975. ' };',
  26976. ' this.$assign = function (s) {',
  26977. ' this.x = s.x;',
  26978. ' return this;',
  26979. ' };',
  26980. '});',
  26981. 'this.r = this.TRec.$new();',
  26982. 'this.p = null;',
  26983. 'this.q = null;',
  26984. 'this.Ptr = null;',
  26985. '']),
  26986. LinesToStr([ // $mod.$main
  26987. '$mod.p = $mod.TRec.$new();',
  26988. '$mod.p = $mod.r;',
  26989. '$mod.r.$assign($mod.p);',
  26990. '$mod.r.x = $mod.p.x;',
  26991. '$mod.p.x = $mod.r.x;',
  26992. 'if ($mod.p.x === 3) ;',
  26993. 'if (4 === $mod.p.x) ;',
  26994. '$mod.p = null;',
  26995. '$mod.q = $mod.TRec.$new();',
  26996. '$mod.q = null;',
  26997. '$mod.Ptr = $mod.p;',
  26998. '$mod.p = $mod.Ptr;',
  26999. '']));
  27000. end;
  27001. procedure TTestModule.TestPointer_RecordArg;
  27002. begin
  27003. StartProgram(false);
  27004. Add([
  27005. '{$modeswitch autoderef}',
  27006. 'type',
  27007. ' TRec = record x: longint; end;',
  27008. ' PRec = ^TRec;',
  27009. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27010. 'begin',
  27011. ' a.x:=a.x;',
  27012. ' a^.x:=a^.x;',
  27013. ' with a^ do',
  27014. ' x:=x;',
  27015. 'end;',
  27016. 'function GetIt(p: PRec): PRec;',
  27017. 'begin',
  27018. ' p.x:=p.x;',
  27019. ' p^.x:=p^.x;',
  27020. ' with p^ do',
  27021. ' x:=x;',
  27022. 'end;',
  27023. 'var',
  27024. ' r: TRec;',
  27025. ' p: PRec;',
  27026. 'begin',
  27027. ' p:=GetIt(p);',
  27028. ' p^:=GetIt(@r)^;',
  27029. ' DoIt(p,p,p);',
  27030. ' DoIt(@r,p,p);',
  27031. '']);
  27032. ConvertProgram;
  27033. CheckSource('TestPointer_RecordArg',
  27034. LinesToStr([ // statements
  27035. 'rtl.recNewT(this, "TRec", function () {',
  27036. ' this.x = 0;',
  27037. ' this.$eq = function (b) {',
  27038. ' return this.x === b.x;',
  27039. ' };',
  27040. ' this.$assign = function (s) {',
  27041. ' this.x = s.x;',
  27042. ' return this;',
  27043. ' };',
  27044. '});',
  27045. 'this.DoIt = function (a, b, c) {',
  27046. ' var Result = $mod.TRec.$new();',
  27047. ' a.x = a.x;',
  27048. ' a.x = a.x;',
  27049. ' a.x = a.x;',
  27050. ' return Result;',
  27051. '};',
  27052. 'this.GetIt = function (p) {',
  27053. ' var Result = null;',
  27054. ' p.x = p.x;',
  27055. ' p.x = p.x;',
  27056. ' p.x = p.x;',
  27057. ' return Result;',
  27058. '};',
  27059. 'this.r = this.TRec.$new();',
  27060. 'this.p = null;',
  27061. '']),
  27062. LinesToStr([ // $mod.$main
  27063. '$mod.p = $mod.GetIt($mod.p);',
  27064. '$mod.p.$assign($mod.GetIt($mod.r));',
  27065. '$mod.DoIt($mod.p, {',
  27066. ' p: $mod,',
  27067. ' get: function () {',
  27068. ' return this.p.p;',
  27069. ' },',
  27070. ' set: function (v) {',
  27071. ' this.p.p = v;',
  27072. ' }',
  27073. '}, {',
  27074. ' p: $mod,',
  27075. ' get: function () {',
  27076. ' return this.p.p;',
  27077. ' },',
  27078. ' set: function (v) {',
  27079. ' this.p.p = v;',
  27080. ' }',
  27081. '});',
  27082. '$mod.DoIt($mod.r, {',
  27083. ' p: $mod,',
  27084. ' get: function () {',
  27085. ' return this.p.p;',
  27086. ' },',
  27087. ' set: function (v) {',
  27088. ' this.p.p = v;',
  27089. ' }',
  27090. '}, {',
  27091. ' p: $mod,',
  27092. ' get: function () {',
  27093. ' return this.p.p;',
  27094. ' },',
  27095. ' set: function (v) {',
  27096. ' this.p.p = v;',
  27097. ' }',
  27098. '});',
  27099. '']));
  27100. end;
  27101. procedure TTestModule.TestJSValue_AssignToJSValue;
  27102. begin
  27103. StartProgram(false);
  27104. Add('var');
  27105. Add(' v: jsvalue;');
  27106. Add(' i: longint;');
  27107. Add(' s: string;');
  27108. Add(' b: boolean;');
  27109. Add(' d: double;');
  27110. Add(' p: pointer;');
  27111. Add('begin');
  27112. Add(' v:=v;');
  27113. Add(' v:=1;');
  27114. Add(' v:=i;');
  27115. Add(' v:='''';');
  27116. Add(' v:=''c'';');
  27117. Add(' v:=''foo'';');
  27118. Add(' v:=s;');
  27119. Add(' v:=false;');
  27120. Add(' v:=true;');
  27121. Add(' v:=b;');
  27122. Add(' v:=0.1;');
  27123. Add(' v:=d;');
  27124. Add(' v:=nil;');
  27125. Add(' v:=p;');
  27126. ConvertProgram;
  27127. CheckSource('TestJSValue_AssignToJSValue',
  27128. LinesToStr([ // statements
  27129. 'this.v = undefined;',
  27130. 'this.i = 0;',
  27131. 'this.s = "";',
  27132. 'this.b = false;',
  27133. 'this.d = 0.0;',
  27134. 'this.p = null;',
  27135. '']),
  27136. LinesToStr([ // $mod.$main
  27137. '$mod.v = $mod.v;',
  27138. '$mod.v = 1;',
  27139. '$mod.v = $mod.i;',
  27140. '$mod.v = "";',
  27141. '$mod.v = "c";',
  27142. '$mod.v = "foo";',
  27143. '$mod.v = $mod.s;',
  27144. '$mod.v = false;',
  27145. '$mod.v = true;',
  27146. '$mod.v = $mod.b;',
  27147. '$mod.v = 0.1;',
  27148. '$mod.v = $mod.d;',
  27149. '$mod.v = null;',
  27150. '$mod.v = $mod.p;',
  27151. '']));
  27152. end;
  27153. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27154. begin
  27155. StartProgram(false);
  27156. Add('type');
  27157. Add(' integer = longint;');
  27158. Add(' TYesNo = boolean;');
  27159. Add(' TFloat = double;');
  27160. Add(' TCaption = string;');
  27161. Add(' TChar = char;');
  27162. Add('var');
  27163. Add(' v: jsvalue;');
  27164. Add(' i: integer;');
  27165. Add(' s: TCaption;');
  27166. Add(' b: TYesNo;');
  27167. Add(' d: TFloat;');
  27168. Add(' c: char;');
  27169. Add('begin');
  27170. Add(' i:=longint(v);');
  27171. Add(' i:=integer(v);');
  27172. Add(' s:=string(v);');
  27173. Add(' s:=TCaption(v);');
  27174. Add(' b:=boolean(v);');
  27175. Add(' b:=TYesNo(v);');
  27176. Add(' d:=double(v);');
  27177. Add(' d:=TFloat(v);');
  27178. Add(' c:=char(v);');
  27179. Add(' c:=TChar(v);');
  27180. ConvertProgram;
  27181. CheckSource('TestJSValue_TypeCastToBaseType',
  27182. LinesToStr([ // statements
  27183. 'this.v = undefined;',
  27184. 'this.i = 0;',
  27185. 'this.s = "";',
  27186. 'this.b = false;',
  27187. 'this.d = 0.0;',
  27188. 'this.c = "";',
  27189. '']),
  27190. LinesToStr([ // $mod.$main
  27191. '$mod.i = rtl.trunc($mod.v);',
  27192. '$mod.i = rtl.trunc($mod.v);',
  27193. '$mod.s = "" + $mod.v;',
  27194. '$mod.s = "" + $mod.v;',
  27195. '$mod.b = !($mod.v == false);',
  27196. '$mod.b = !($mod.v == false);',
  27197. '$mod.d = rtl.getNumber($mod.v);',
  27198. '$mod.d = rtl.getNumber($mod.v);',
  27199. '$mod.c = rtl.getChar($mod.v);',
  27200. '$mod.c = rtl.getChar($mod.v);',
  27201. '']));
  27202. end;
  27203. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27204. begin
  27205. StartProgram(false);
  27206. Add([
  27207. 'type',
  27208. ' TArr = array of word;',
  27209. ' TRec = record end;',
  27210. ' TSet = set of boolean;',
  27211. 'procedure Fly(v: jsvalue);',
  27212. 'begin',
  27213. 'end;',
  27214. 'var',
  27215. ' a: TArr;',
  27216. ' r: TRec;',
  27217. ' s: TSet;',
  27218. 'begin',
  27219. ' Fly(jsvalue(a));',
  27220. ' Fly(jsvalue(r));',
  27221. ' Fly(jsvalue(s));',
  27222. '']);
  27223. ConvertProgram;
  27224. CheckSource('TestJSValue_TypecastToJSValue',
  27225. LinesToStr([ // statements
  27226. 'rtl.recNewT(this, "TRec", function () {',
  27227. ' this.$eq = function (b) {',
  27228. ' return true;',
  27229. ' };',
  27230. ' this.$assign = function (s) {',
  27231. ' return this;',
  27232. ' };',
  27233. '});',
  27234. 'this.Fly = function (v) {',
  27235. '};',
  27236. 'this.a = [];',
  27237. 'this.r = this.TRec.$new();',
  27238. 'this.s = {};',
  27239. '']),
  27240. LinesToStr([ // $mod.$main
  27241. '$mod.Fly($mod.a);',
  27242. '$mod.Fly($mod.r);',
  27243. '$mod.Fly($mod.s);',
  27244. '']));
  27245. end;
  27246. procedure TTestModule.TestJSValue_Equal;
  27247. begin
  27248. StartProgram(false);
  27249. Add('type');
  27250. Add(' integer = longint;');
  27251. Add(' TYesNo = boolean;');
  27252. Add(' TFloat = double;');
  27253. Add(' TCaption = string;');
  27254. Add(' TChar = char;');
  27255. Add(' TMulti = JSValue;');
  27256. Add('var');
  27257. Add(' v: jsvalue;');
  27258. Add(' i: integer;');
  27259. Add(' s: TCaption;');
  27260. Add(' b: TYesNo;');
  27261. Add(' d: TFloat;');
  27262. Add(' c: char;');
  27263. Add(' m: TMulti;');
  27264. Add('begin');
  27265. Add(' b:=v=v;');
  27266. Add(' b:=v<>v;');
  27267. Add(' b:=v=1;');
  27268. Add(' b:=v<>1;');
  27269. Add(' b:=2=v;');
  27270. Add(' b:=2<>v;');
  27271. Add(' b:=v=i;');
  27272. Add(' b:=i=v;');
  27273. Add(' b:=v=nil;');
  27274. Add(' b:=nil=v;');
  27275. Add(' b:=v=false;');
  27276. Add(' b:=true=v;');
  27277. Add(' b:=v=b;');
  27278. Add(' b:=b=v;');
  27279. Add(' b:=v=s;');
  27280. Add(' b:=s=v;');
  27281. Add(' b:=v=''foo'';');
  27282. Add(' b:=''''=v;');
  27283. Add(' b:=v=d;');
  27284. Add(' b:=d=v;');
  27285. Add(' b:=v=3.4;');
  27286. Add(' b:=5.6=v;');
  27287. Add(' b:=v=c;');
  27288. Add(' b:=c=v;');
  27289. Add(' b:=m=m;');
  27290. Add(' b:=v=m;');
  27291. Add(' b:=m=v;');
  27292. ConvertProgram;
  27293. CheckSource('TestJSValue_Equal',
  27294. LinesToStr([ // statements
  27295. 'this.v = undefined;',
  27296. 'this.i = 0;',
  27297. 'this.s = "";',
  27298. 'this.b = false;',
  27299. 'this.d = 0.0;',
  27300. 'this.c = "";',
  27301. 'this.m = undefined;',
  27302. '']),
  27303. LinesToStr([ // $mod.$main
  27304. '$mod.b = $mod.v == $mod.v;',
  27305. '$mod.b = $mod.v != $mod.v;',
  27306. '$mod.b = $mod.v == 1;',
  27307. '$mod.b = $mod.v != 1;',
  27308. '$mod.b = 2 == $mod.v;',
  27309. '$mod.b = 2 != $mod.v;',
  27310. '$mod.b = $mod.v == $mod.i;',
  27311. '$mod.b = $mod.i == $mod.v;',
  27312. '$mod.b = $mod.v == null;',
  27313. '$mod.b = null == $mod.v;',
  27314. '$mod.b = $mod.v == false;',
  27315. '$mod.b = true == $mod.v;',
  27316. '$mod.b = $mod.v == $mod.b;',
  27317. '$mod.b = $mod.b == $mod.v;',
  27318. '$mod.b = $mod.v == $mod.s;',
  27319. '$mod.b = $mod.s == $mod.v;',
  27320. '$mod.b = $mod.v == "foo";',
  27321. '$mod.b = "" == $mod.v;',
  27322. '$mod.b = $mod.v == $mod.d;',
  27323. '$mod.b = $mod.d == $mod.v;',
  27324. '$mod.b = $mod.v == 3.4;',
  27325. '$mod.b = 5.6 == $mod.v;',
  27326. '$mod.b = $mod.v == $mod.c;',
  27327. '$mod.b = $mod.c == $mod.v;',
  27328. '$mod.b = $mod.m == $mod.m;',
  27329. '$mod.b = $mod.v == $mod.m;',
  27330. '$mod.b = $mod.m == $mod.v;',
  27331. '']));
  27332. end;
  27333. procedure TTestModule.TestJSValue_If;
  27334. begin
  27335. StartProgram(false);
  27336. Add([
  27337. 'procedure Fly(var u);',
  27338. 'begin',
  27339. ' if jsvalue(u) then ;',
  27340. 'end;',
  27341. 'var',
  27342. ' v: jsvalue;',
  27343. 'begin',
  27344. ' if v then ;',
  27345. ' while v do ;',
  27346. ' repeat until v;',
  27347. '']);
  27348. ConvertProgram;
  27349. CheckSource('TestJSValue_If',
  27350. LinesToStr([ // statements
  27351. 'this.Fly = function (u) {',
  27352. ' if (u.get()) ;',
  27353. '};',
  27354. 'this.v = undefined;',
  27355. '']),
  27356. LinesToStr([ // $mod.$main
  27357. 'if ($mod.v) ;',
  27358. 'while($mod.v){',
  27359. '};',
  27360. 'do{',
  27361. '} while(!$mod.v);',
  27362. '']));
  27363. end;
  27364. procedure TTestModule.TestJSValue_Not;
  27365. begin
  27366. StartProgram(false);
  27367. Add([
  27368. 'var',
  27369. ' v: jsvalue;',
  27370. ' b: boolean;',
  27371. 'begin',
  27372. ' b:=not v;',
  27373. ' if not v then ;',
  27374. ' while not v do ;',
  27375. ' repeat until not v;',
  27376. '']);
  27377. ConvertProgram;
  27378. CheckSource('TestJSValue_If',
  27379. LinesToStr([ // statements
  27380. 'this.v = undefined;',
  27381. 'this.b = false;',
  27382. '']),
  27383. LinesToStr([ // $mod.$main
  27384. '$mod.b=!$mod.v;',
  27385. 'if (!$mod.v) ;',
  27386. 'while(!$mod.v){',
  27387. '};',
  27388. 'do{',
  27389. '} while($mod.v);',
  27390. '']));
  27391. end;
  27392. procedure TTestModule.TestJSValue_Enum;
  27393. begin
  27394. StartProgram(false);
  27395. Add('type');
  27396. Add(' TColor = (red, blue);');
  27397. Add(' TRedBlue = TColor;');
  27398. Add('var');
  27399. Add(' v: jsvalue;');
  27400. Add(' e: TColor;');
  27401. Add('begin');
  27402. Add(' v:=e;');
  27403. Add(' v:=TColor(e);');
  27404. Add(' v:=TRedBlue(e);');
  27405. Add(' e:=TColor(v);');
  27406. Add(' e:=TRedBlue(v);');
  27407. ConvertProgram;
  27408. CheckSource('TestJSValue_Enum',
  27409. LinesToStr([ // statements
  27410. 'this.TColor = {',
  27411. ' "0": "red",',
  27412. ' red: 0,',
  27413. ' "1": "blue",',
  27414. ' blue: 1',
  27415. '};',
  27416. 'this.v = undefined;',
  27417. 'this.e = 0;',
  27418. '']),
  27419. LinesToStr([ // $mod.$main
  27420. '$mod.v = $mod.e;',
  27421. '$mod.v = $mod.e;',
  27422. '$mod.v = $mod.e;',
  27423. '$mod.e = $mod.v;',
  27424. '$mod.e = $mod.v;',
  27425. '']));
  27426. end;
  27427. procedure TTestModule.TestJSValue_ClassInstance;
  27428. begin
  27429. StartProgram(false);
  27430. Add([
  27431. 'type',
  27432. ' TObject = class',
  27433. ' end;',
  27434. ' TBirdObject = TObject;',
  27435. 'var',
  27436. ' v: jsvalue;',
  27437. ' o: TObject;',
  27438. 'begin',
  27439. ' v:=o;',
  27440. ' v:=TObject(o);',
  27441. ' v:=TBirdObject(o);',
  27442. ' o:=TObject(v);',
  27443. ' o:=TBirdObject(v);',
  27444. ' if v is TObject then ;',
  27445. '']);
  27446. ConvertProgram;
  27447. CheckSource('TestJSValue_ClassInstance',
  27448. LinesToStr([ // statements
  27449. 'rtl.createClass(this, "TObject", null, function () {',
  27450. ' this.$init = function () {',
  27451. ' };',
  27452. ' this.$final = function () {',
  27453. ' };',
  27454. '});',
  27455. 'this.v = undefined;',
  27456. 'this.o = null;',
  27457. '']),
  27458. LinesToStr([ // $mod.$main
  27459. '$mod.v = $mod.o;',
  27460. '$mod.v = $mod.o;',
  27461. '$mod.v = $mod.o;',
  27462. '$mod.o = rtl.getObject($mod.v);',
  27463. '$mod.o = rtl.getObject($mod.v);',
  27464. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27465. '']));
  27466. end;
  27467. procedure TTestModule.TestJSValue_ClassOf;
  27468. begin
  27469. StartProgram(false);
  27470. Add([
  27471. 'type',
  27472. ' TClass = class of TObject;',
  27473. ' TObject = class',
  27474. ' end;',
  27475. ' TBirds = class of TBird;',
  27476. ' TBird = class(TObject) end;',
  27477. 'var',
  27478. ' v: jsvalue;',
  27479. ' c: TClass;',
  27480. 'begin',
  27481. ' v:=c;',
  27482. ' v:=TObject;',
  27483. ' v:=TClass(c);',
  27484. ' v:=TBirds(c);',
  27485. ' c:=TClass(v);',
  27486. ' c:=TBirds(v);',
  27487. ' if v is TClass then ;',
  27488. '']);
  27489. ConvertProgram;
  27490. CheckSource('TestJSValue_ClassOf',
  27491. LinesToStr([ // statements
  27492. 'rtl.createClass(this, "TObject", null, function () {',
  27493. ' this.$init = function () {',
  27494. ' };',
  27495. ' this.$final = function () {',
  27496. ' };',
  27497. '});',
  27498. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27499. '});',
  27500. 'this.v = undefined;',
  27501. 'this.c = null;',
  27502. '']),
  27503. LinesToStr([ // $mod.$main
  27504. '$mod.v = $mod.c;',
  27505. '$mod.v = $mod.TObject;',
  27506. '$mod.v = $mod.c;',
  27507. '$mod.v = $mod.c;',
  27508. '$mod.c = rtl.getObject($mod.v);',
  27509. '$mod.c = rtl.getObject($mod.v);',
  27510. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27511. '']));
  27512. end;
  27513. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27514. begin
  27515. StartProgram(false);
  27516. Add([
  27517. 'type',
  27518. ' integer = longint;',
  27519. ' TArray = array of JSValue;',
  27520. ' TArrgh = tarray;',
  27521. ' TArrInt = array of integer;',
  27522. 'var',
  27523. ' v: jsvalue;',
  27524. ' TheArray: tarray = (1,''2'');',
  27525. ' Arr: tarrgh;',
  27526. ' i: integer;',
  27527. ' ArrInt: tarrint;',
  27528. 'begin',
  27529. ' arr:=thearray;',
  27530. ' thearray:=arr;',
  27531. ' setlength(arr,2);',
  27532. ' setlength(thearray,3);',
  27533. ' arr[4]:=v;',
  27534. ' arr[5]:=length(thearray);',
  27535. ' arr[6]:=nil;',
  27536. ' arr[7]:=thearray[8];',
  27537. ' arr[low(arr)]:=high(thearray);',
  27538. ' arr:=arrint;',
  27539. ' arrInt:=tarrint(arr);',
  27540. ' if TheArray = nil then ;',
  27541. ' if nil = TheArray then ;',
  27542. ' if TheArray <> nil then ;',
  27543. ' if nil <> TheArray then ;',
  27544. '']);
  27545. ConvertProgram;
  27546. CheckSource('TestJSValue_ArrayOfJSValue',
  27547. LinesToStr([ // statements
  27548. 'this.v = undefined;',
  27549. 'this.TheArray = [1, "2"];',
  27550. 'this.Arr = [];',
  27551. 'this.i = 0;',
  27552. 'this.ArrInt = [];',
  27553. '']),
  27554. LinesToStr([ // $mod.$main
  27555. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27556. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27557. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27558. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27559. '$mod.Arr[4] = $mod.v;',
  27560. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27561. '$mod.Arr[6] = null;',
  27562. '$mod.Arr[7] = $mod.TheArray[8];',
  27563. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27564. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27565. '$mod.ArrInt = $mod.Arr;',
  27566. 'if (rtl.length($mod.TheArray) === 0) ;',
  27567. 'if (rtl.length($mod.TheArray) === 0) ;',
  27568. 'if (rtl.length($mod.TheArray) > 0) ;',
  27569. 'if (rtl.length($mod.TheArray) > 0) ;',
  27570. '']));
  27571. end;
  27572. procedure TTestModule.TestJSValue_ArrayLit;
  27573. begin
  27574. StartProgram(false);
  27575. Add([
  27576. 'type',
  27577. ' TFlag = (big,small);',
  27578. ' TArray = array of JSValue;',
  27579. ' TObject = class end;',
  27580. ' TClass = class of TObject;',
  27581. 'var',
  27582. ' v: jsvalue;',
  27583. ' a: TArray;',
  27584. ' o: TObject;',
  27585. 'begin',
  27586. ' a:=[];',
  27587. ' a:=[1];',
  27588. ' a:=[1,2];',
  27589. ' a:=[big];',
  27590. ' a:=[1,big];',
  27591. ' a:=[o,nil];',
  27592. '']);
  27593. ConvertProgram;
  27594. CheckSource('TestJSValue_ArrayLit',
  27595. LinesToStr([ // statements
  27596. 'this.TFlag = {',
  27597. ' "0": "big",',
  27598. ' big: 0,',
  27599. ' "1": "small",',
  27600. ' small: 1',
  27601. '};',
  27602. 'rtl.createClass(this, "TObject", null, function () {',
  27603. ' this.$init = function () {',
  27604. ' };',
  27605. ' this.$final = function () {',
  27606. ' };',
  27607. '});',
  27608. 'this.v = undefined;',
  27609. 'this.a = [];',
  27610. 'this.o = null;',
  27611. '']),
  27612. LinesToStr([ // $mod.$main
  27613. '$mod.a = [];',
  27614. '$mod.a = [1];',
  27615. '$mod.a = [1, 2];',
  27616. '$mod.a = [$mod.TFlag.big];',
  27617. '$mod.a = [1, $mod.TFlag.big];',
  27618. '$mod.a = [$mod.o, null];',
  27619. '']));
  27620. end;
  27621. procedure TTestModule.TestJSValue_Params;
  27622. begin
  27623. StartProgram(false);
  27624. Add('type');
  27625. Add(' integer = longint;');
  27626. Add(' TYesNo = boolean;');
  27627. Add(' TFloat = double;');
  27628. Add(' TCaption = string;');
  27629. Add(' TChar = char;');
  27630. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27631. Add('var');
  27632. Add(' l: jsvalue;');
  27633. Add('begin');
  27634. Add(' a:=a;');
  27635. Add(' l:=b;');
  27636. Add(' c:=c;');
  27637. Add(' d:=d;');
  27638. Add(' Result:=l;');
  27639. Add('end;');
  27640. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27641. Add('var');
  27642. Add(' v: jsvalue;');
  27643. Add(' i: integer;');
  27644. Add(' b: TYesNo;');
  27645. Add(' d: TFloat;');
  27646. Add(' s: TCaption;');
  27647. Add(' c: TChar;');
  27648. Add('begin');
  27649. Add(' v:=doit(v,v,v,v);');
  27650. Add(' i:=integer(dosome(i,i));');
  27651. Add(' b:=TYesNo(dosome(b,b));');
  27652. Add(' d:=TFloat(dosome(d,d));');
  27653. Add(' s:=TCaption(dosome(s,s));');
  27654. Add(' c:=TChar(dosome(c,c));');
  27655. ConvertProgram;
  27656. CheckSource('TestJSValue_Params',
  27657. LinesToStr([ // statements
  27658. 'this.DoIt = function (a, b, c, d) {',
  27659. ' var Result = undefined;',
  27660. ' var l = undefined;',
  27661. ' a = a;',
  27662. ' l = b;',
  27663. ' c.set(c.get());',
  27664. ' d.set(d.get());',
  27665. ' Result = l;',
  27666. ' return Result;',
  27667. '};',
  27668. 'this.DoSome = function (a, b) {',
  27669. ' var Result = undefined;',
  27670. ' return Result;',
  27671. '};',
  27672. 'this.v = undefined;',
  27673. 'this.i = 0;',
  27674. 'this.b = false;',
  27675. 'this.d = 0.0;',
  27676. 'this.s = "";',
  27677. 'this.c = "";',
  27678. '']),
  27679. LinesToStr([ // $mod.$main
  27680. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27681. ' p: $mod,',
  27682. ' get: function () {',
  27683. ' return this.p.v;',
  27684. ' },',
  27685. ' set: function (v) {',
  27686. ' this.p.v = v;',
  27687. ' }',
  27688. '}, {',
  27689. ' p: $mod,',
  27690. ' get: function () {',
  27691. ' return this.p.v;',
  27692. ' },',
  27693. ' set: function (v) {',
  27694. ' this.p.v = v;',
  27695. ' }',
  27696. '});',
  27697. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27698. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27699. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27700. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27701. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27702. '']));
  27703. end;
  27704. procedure TTestModule.TestJSValue_UntypedParam;
  27705. begin
  27706. StartProgram(false);
  27707. Add('function DoIt(const a; var b; out c): jsvalue;');
  27708. Add('begin');
  27709. Add(' Result:=a;');
  27710. Add(' Result:=b;');
  27711. Add(' Result:=c;');
  27712. Add(' b:=Result;');
  27713. Add(' c:=Result;');
  27714. Add('end;');
  27715. Add('var i: longint;');
  27716. Add('begin');
  27717. Add(' doit(i,i,i);');
  27718. ConvertProgram;
  27719. CheckSource('TestJSValue_UntypedParam',
  27720. LinesToStr([ // statements
  27721. 'this.DoIt = function (a, b, c) {',
  27722. ' var Result = undefined;',
  27723. ' Result = a;',
  27724. ' Result = b.get();',
  27725. ' Result = c.get();',
  27726. ' b.set(Result);',
  27727. ' c.set(Result);',
  27728. ' return Result;',
  27729. '};',
  27730. 'this.i = 0;',
  27731. '']),
  27732. LinesToStr([ // $mod.$main
  27733. '$mod.DoIt($mod.i, {',
  27734. ' p: $mod,',
  27735. ' get: function () {',
  27736. ' return this.p.i;',
  27737. ' },',
  27738. ' set: function (v) {',
  27739. ' this.p.i = v;',
  27740. ' }',
  27741. '}, {',
  27742. ' p: $mod,',
  27743. ' get: function () {',
  27744. ' return this.p.i;',
  27745. ' },',
  27746. ' set: function (v) {',
  27747. ' this.p.i = v;',
  27748. ' }',
  27749. '});',
  27750. '']));
  27751. end;
  27752. procedure TTestModule.TestJSValue_FuncResultType;
  27753. begin
  27754. StartProgram(false);
  27755. Add('type');
  27756. Add(' integer = longint;');
  27757. Add(' TJSValueArray = array of JSValue;');
  27758. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27759. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27760. Add('begin');
  27761. Add(' while Compare(P,aList[0])>0 do ;');
  27762. Add('end;');
  27763. Add('var');
  27764. Add(' Compare: TListSortCompare;');
  27765. Add(' V: JSValue;');
  27766. Add(' i: integer;');
  27767. Add('begin');
  27768. Add(' if Compare(V,V)>0 then ;');
  27769. Add(' if Compare(i,i)>1 then ;');
  27770. Add(' if Compare(nil,false)>2 then ;');
  27771. Add(' if Compare(1,true)>3 then ;');
  27772. ConvertProgram;
  27773. CheckSource('TestJSValue_UntypedParam',
  27774. LinesToStr([ // statements
  27775. 'this.Sort = function (P, aList, Compare) {',
  27776. ' while (Compare(P, aList[0]) > 0) {',
  27777. ' };',
  27778. '};',
  27779. 'this.Compare = null;',
  27780. 'this.V = undefined;',
  27781. 'this.i = 0;',
  27782. '']),
  27783. LinesToStr([ // $mod.$main
  27784. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27785. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27786. 'if ($mod.Compare(null, false) > 2) ;',
  27787. 'if ($mod.Compare(1, true) > 3) ;',
  27788. '']));
  27789. end;
  27790. procedure TTestModule.TestJSValue_ProcType_Assign;
  27791. begin
  27792. StartProgram(false);
  27793. Add('type');
  27794. Add(' integer = longint;');
  27795. Add(' TObject = class');
  27796. Add(' class function GetGlob: integer;');
  27797. Add(' function Getter: integer;');
  27798. Add(' end;');
  27799. Add('class function TObject.GetGlob: integer;');
  27800. Add('var v1: jsvalue;');
  27801. Add('begin');
  27802. Add(' v1:=@GetGlob;');
  27803. Add(' v1:[email protected];');
  27804. Add('end;');
  27805. Add('function TObject.Getter: integer;');
  27806. Add('var v2: jsvalue;');
  27807. Add('begin');
  27808. Add(' v2:=@Getter;');
  27809. Add(' v2:[email protected];');
  27810. Add(' v2:=@GetGlob;');
  27811. Add(' v2:[email protected];');
  27812. Add('end;');
  27813. Add('function GetIt(i: integer): integer;');
  27814. Add('var v3: jsvalue;');
  27815. Add('begin');
  27816. Add(' v3:=@GetIt;');
  27817. Add('end;');
  27818. Add('var');
  27819. Add(' V: JSValue;');
  27820. Add(' o: TObject;');
  27821. Add('begin');
  27822. Add(' v:=@GetIt;');
  27823. Add(' v:[email protected];');
  27824. Add(' v:[email protected];');
  27825. ConvertProgram;
  27826. CheckSource('TestJSValue_ProcType_Assign',
  27827. LinesToStr([ // statements
  27828. 'rtl.createClass(this, "TObject", null, function () {',
  27829. ' this.$init = function () {',
  27830. ' };',
  27831. ' this.$final = function () {',
  27832. ' };',
  27833. ' this.GetGlob = function () {',
  27834. ' var Result = 0;',
  27835. ' var v1 = undefined;',
  27836. ' v1 = rtl.createCallback(this, "GetGlob");',
  27837. ' v1 = rtl.createCallback(this, "GetGlob");',
  27838. ' return Result;',
  27839. ' };',
  27840. ' this.Getter = function () {',
  27841. ' var Result = 0;',
  27842. ' var v2 = undefined;',
  27843. ' v2 = rtl.createCallback(this, "Getter");',
  27844. ' v2 = rtl.createCallback(this, "Getter");',
  27845. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27846. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27847. ' return Result;',
  27848. ' };',
  27849. '});',
  27850. 'this.GetIt = function (i) {',
  27851. ' var Result = 0;',
  27852. ' var v3 = undefined;',
  27853. ' v3 = $mod.GetIt;',
  27854. ' return Result;',
  27855. '};',
  27856. 'this.V = undefined;',
  27857. 'this.o = null;',
  27858. '']),
  27859. LinesToStr([ // $mod.$main
  27860. '$mod.V = $mod.GetIt;',
  27861. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27862. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27863. '']));
  27864. end;
  27865. procedure TTestModule.TestJSValue_ProcType_Equal;
  27866. begin
  27867. StartProgram(false);
  27868. Add('type');
  27869. Add(' integer = longint;');
  27870. Add(' TObject = class');
  27871. Add(' class function GetGlob: integer;');
  27872. Add(' function Getter: integer;');
  27873. Add(' end;');
  27874. Add('class function TObject.GetGlob: integer;');
  27875. Add('var v1: jsvalue;');
  27876. Add('begin');
  27877. Add(' if v1=@GetGlob then;');
  27878. Add(' if [email protected] then ;');
  27879. Add('end;');
  27880. Add('function TObject.Getter: integer;');
  27881. Add('var v2: jsvalue;');
  27882. Add('begin');
  27883. Add(' if v2=@Getter then;');
  27884. Add(' if [email protected] then ;');
  27885. Add(' if v2=@GetGlob then;');
  27886. Add(' if [email protected] then;');
  27887. Add('end;');
  27888. Add('function GetIt(i: integer): integer;');
  27889. Add('var v3: jsvalue;');
  27890. Add('begin');
  27891. Add(' if v3=@GetIt then;');
  27892. Add('end;');
  27893. Add('var');
  27894. Add(' V: JSValue;');
  27895. Add(' o: TObject;');
  27896. Add('begin');
  27897. Add(' if v=@GetIt then;');
  27898. Add(' if [email protected] then;');
  27899. Add(' if [email protected] then;');
  27900. Add(' if @GetIt=v then;');
  27901. Add(' if @o.Getter=v then;');
  27902. Add(' if @o.GetGlob=v then;');
  27903. ConvertProgram;
  27904. CheckSource('TestJSValue_ProcType_Equal',
  27905. LinesToStr([ // statements
  27906. 'rtl.createClass(this, "TObject", null, function () {',
  27907. ' this.$init = function () {',
  27908. ' };',
  27909. ' this.$final = function () {',
  27910. ' };',
  27911. ' this.GetGlob = function () {',
  27912. ' var Result = 0;',
  27913. ' var v1 = undefined;',
  27914. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27915. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27916. ' return Result;',
  27917. ' };',
  27918. ' this.Getter = function () {',
  27919. ' var Result = 0;',
  27920. ' var v2 = undefined;',
  27921. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27922. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27923. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27924. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27925. ' return Result;',
  27926. ' };',
  27927. '});',
  27928. 'this.GetIt = function (i) {',
  27929. ' var Result = 0;',
  27930. ' var v3 = undefined;',
  27931. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27932. ' return Result;',
  27933. '};',
  27934. 'this.V = undefined;',
  27935. 'this.o = null;',
  27936. '']),
  27937. LinesToStr([ // $mod.$main
  27938. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27939. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27940. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27941. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27942. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27943. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27944. '']));
  27945. end;
  27946. procedure TTestModule.TestJSValue_ProcType_Param;
  27947. begin
  27948. StartProgram(false);
  27949. Add([
  27950. 'type',
  27951. ' variant = jsvalue;',
  27952. ' TArrVariant = array of variant;',
  27953. ' TArrVar2 = TArrVariant;',
  27954. ' TFuncInt = function: longint;',
  27955. 'function GetIt: longint;',
  27956. 'begin',
  27957. 'end;',
  27958. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27959. 'var v: variant;',
  27960. 'begin',
  27961. ' v:=arr[1];',
  27962. 'end;',
  27963. 'var s: string;',
  27964. 'begin',
  27965. ' DoIt(GetIt,[]);',
  27966. ' DoIt(@GetIt,[]);',
  27967. ' DoIt(1,[s,GetIt]);',
  27968. ' DoIt(1,[s,@GetIt]);',
  27969. '']);
  27970. ConvertProgram;
  27971. CheckSource('TestJSValue_ProcType_Param',
  27972. LinesToStr([ // statements
  27973. 'this.GetIt = function () {',
  27974. ' var Result = 0;',
  27975. ' return Result;',
  27976. '};',
  27977. 'this.DoIt = function (p, Arr) {',
  27978. ' var v = undefined;',
  27979. ' v = Arr[1];',
  27980. '};',
  27981. 'this.s = "";',
  27982. '']),
  27983. LinesToStr([ // $mod.$main
  27984. '$mod.DoIt($mod.GetIt(), []);',
  27985. '$mod.DoIt($mod.GetIt, []);',
  27986. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27987. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27988. '']));
  27989. end;
  27990. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27991. begin
  27992. StartProgram(false);
  27993. Add([
  27994. 'var',
  27995. ' v: JSValue;',
  27996. ' p: Pointer;',
  27997. 'begin',
  27998. ' p:=v;',
  27999. '']);
  28000. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28001. nIncompatibleTypesGotExpected);
  28002. ConvertProgram;
  28003. end;
  28004. procedure TTestModule.TestJSValue_OverloadDouble;
  28005. begin
  28006. StartProgram(false);
  28007. Add([
  28008. 'type',
  28009. ' integer = longint;',
  28010. ' tdatetime = double;',
  28011. 'procedure DoIt(d: double); begin end;',
  28012. 'procedure DoIt(v: jsvalue); begin end;',
  28013. 'var',
  28014. ' d: double;',
  28015. ' dt: tdatetime;',
  28016. ' i: integer;',
  28017. ' b: byte;',
  28018. ' shi: shortint;',
  28019. ' w: word;',
  28020. ' smi: smallint;',
  28021. ' lw: longword;',
  28022. ' li: longint;',
  28023. ' ni: nativeint;',
  28024. ' nu: nativeuint;',
  28025. 'begin',
  28026. ' DoIt(d);',
  28027. ' DoIt(dt);',
  28028. ' DoIt(i);',
  28029. ' DoIt(b);',
  28030. ' DoIt(shi);',
  28031. ' DoIt(w);',
  28032. ' DoIt(smi);',
  28033. ' DoIt(lw);',
  28034. ' DoIt(li);',
  28035. ' DoIt(ni);',
  28036. ' DoIt(nu);',
  28037. '']);
  28038. ConvertProgram;
  28039. CheckSource('TestJSValue_OverloadDouble',
  28040. LinesToStr([ // statements
  28041. 'this.DoIt = function (d) {',
  28042. '};',
  28043. 'this.DoIt$1 = function (v) {',
  28044. '};',
  28045. 'this.d = 0.0;',
  28046. 'this.dt = 0.0;',
  28047. 'this.i = 0;',
  28048. 'this.b = 0;',
  28049. 'this.shi = 0;',
  28050. 'this.w = 0;',
  28051. 'this.smi = 0;',
  28052. 'this.lw = 0;',
  28053. 'this.li = 0;',
  28054. 'this.ni = 0;',
  28055. 'this.nu = 0;',
  28056. '']),
  28057. LinesToStr([ // $mod.$main
  28058. '$mod.DoIt($mod.d);',
  28059. '$mod.DoIt($mod.dt);',
  28060. '$mod.DoIt$1($mod.i);',
  28061. '$mod.DoIt$1($mod.b);',
  28062. '$mod.DoIt$1($mod.shi);',
  28063. '$mod.DoIt$1($mod.w);',
  28064. '$mod.DoIt$1($mod.smi);',
  28065. '$mod.DoIt$1($mod.lw);',
  28066. '$mod.DoIt$1($mod.li);',
  28067. '$mod.DoIt$1($mod.ni);',
  28068. '$mod.DoIt$1($mod.nu);',
  28069. '']));
  28070. end;
  28071. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28072. begin
  28073. StartProgram(false);
  28074. Add([
  28075. 'type',
  28076. ' integer = longint;',
  28077. ' int53 = nativeint;',
  28078. ' tdatetime = double;',
  28079. 'procedure DoIt(n: nativeint); begin end;',
  28080. 'procedure DoIt(v: jsvalue); begin end;',
  28081. 'var',
  28082. ' d: double;',
  28083. ' dt: tdatetime;',
  28084. ' i: integer;',
  28085. ' b: byte;',
  28086. ' shi: shortint;',
  28087. ' w: word;',
  28088. ' smi: smallint;',
  28089. ' lw: longword;',
  28090. ' li: longint;',
  28091. ' ni: nativeint;',
  28092. ' nu: nativeuint;',
  28093. 'begin',
  28094. ' DoIt(d);',
  28095. ' DoIt(dt);',
  28096. ' DoIt(i);',
  28097. ' DoIt(b);',
  28098. ' DoIt(shi);',
  28099. ' DoIt(w);',
  28100. ' DoIt(smi);',
  28101. ' DoIt(lw);',
  28102. ' DoIt(li);',
  28103. ' DoIt(ni);',
  28104. ' DoIt(nu);',
  28105. '']);
  28106. ConvertProgram;
  28107. CheckSource('TestJSValue_OverloadNativeInt',
  28108. LinesToStr([ // statements
  28109. 'this.DoIt = function (n) {',
  28110. '};',
  28111. 'this.DoIt$1 = function (v) {',
  28112. '};',
  28113. 'this.d = 0.0;',
  28114. 'this.dt = 0.0;',
  28115. 'this.i = 0;',
  28116. 'this.b = 0;',
  28117. 'this.shi = 0;',
  28118. 'this.w = 0;',
  28119. 'this.smi = 0;',
  28120. 'this.lw = 0;',
  28121. 'this.li = 0;',
  28122. 'this.ni = 0;',
  28123. 'this.nu = 0;',
  28124. '']),
  28125. LinesToStr([ // $mod.$main
  28126. '$mod.DoIt$1($mod.d);',
  28127. '$mod.DoIt$1($mod.dt);',
  28128. '$mod.DoIt($mod.i);',
  28129. '$mod.DoIt($mod.b);',
  28130. '$mod.DoIt($mod.shi);',
  28131. '$mod.DoIt($mod.w);',
  28132. '$mod.DoIt($mod.smi);',
  28133. '$mod.DoIt($mod.lw);',
  28134. '$mod.DoIt($mod.li);',
  28135. '$mod.DoIt($mod.ni);',
  28136. '$mod.DoIt($mod.nu);',
  28137. '']));
  28138. end;
  28139. procedure TTestModule.TestJSValue_OverloadWord;
  28140. begin
  28141. StartProgram(false);
  28142. Add([
  28143. 'type',
  28144. ' integer = longint;',
  28145. ' int53 = nativeint;',
  28146. ' tdatetime = double;',
  28147. 'procedure DoIt(w: word); begin end;',
  28148. 'procedure DoIt(v: jsvalue); begin end;',
  28149. 'var',
  28150. ' d: double;',
  28151. ' dt: tdatetime;',
  28152. ' i: integer;',
  28153. ' b: byte;',
  28154. ' shi: shortint;',
  28155. ' w: word;',
  28156. ' smi: smallint;',
  28157. ' lw: longword;',
  28158. ' li: longint;',
  28159. ' ni: nativeint;',
  28160. ' nu: nativeuint;',
  28161. 'begin',
  28162. ' DoIt(d);',
  28163. ' DoIt(dt);',
  28164. ' DoIt(i);',
  28165. ' DoIt(b);',
  28166. ' DoIt(shi);',
  28167. ' DoIt(w);',
  28168. ' DoIt(smi);',
  28169. ' DoIt(lw);',
  28170. ' DoIt(li);',
  28171. ' DoIt(ni);',
  28172. ' DoIt(nu);',
  28173. '']);
  28174. ConvertProgram;
  28175. CheckSource('TestJSValue_OverloadWord',
  28176. LinesToStr([ // statements
  28177. 'this.DoIt = function (w) {',
  28178. '};',
  28179. 'this.DoIt$1 = function (v) {',
  28180. '};',
  28181. 'this.d = 0.0;',
  28182. 'this.dt = 0.0;',
  28183. 'this.i = 0;',
  28184. 'this.b = 0;',
  28185. 'this.shi = 0;',
  28186. 'this.w = 0;',
  28187. 'this.smi = 0;',
  28188. 'this.lw = 0;',
  28189. 'this.li = 0;',
  28190. 'this.ni = 0;',
  28191. 'this.nu = 0;',
  28192. '']),
  28193. LinesToStr([ // $mod.$main
  28194. '$mod.DoIt$1($mod.d);',
  28195. '$mod.DoIt$1($mod.dt);',
  28196. '$mod.DoIt$1($mod.i);',
  28197. '$mod.DoIt($mod.b);',
  28198. '$mod.DoIt($mod.shi);',
  28199. '$mod.DoIt($mod.w);',
  28200. '$mod.DoIt$1($mod.smi);',
  28201. '$mod.DoIt$1($mod.lw);',
  28202. '$mod.DoIt$1($mod.li);',
  28203. '$mod.DoIt$1($mod.ni);',
  28204. '$mod.DoIt$1($mod.nu);',
  28205. '']));
  28206. end;
  28207. procedure TTestModule.TestJSValue_OverloadString;
  28208. begin
  28209. StartProgram(false);
  28210. Add([
  28211. 'type',
  28212. ' uni = string;',
  28213. ' WChar = char;',
  28214. 'procedure DoIt(s: string); begin end;',
  28215. 'procedure DoIt(v: jsvalue); begin end;',
  28216. 'var',
  28217. ' s: string;',
  28218. ' c: char;',
  28219. ' u: uni;',
  28220. 'begin',
  28221. ' DoIt(s);',
  28222. ' DoIt(c);',
  28223. ' DoIt(u);',
  28224. '']);
  28225. ConvertProgram;
  28226. CheckSource('TestJSValue_OverloadString',
  28227. LinesToStr([ // statements
  28228. 'this.DoIt = function (s) {',
  28229. '};',
  28230. 'this.DoIt$1 = function (v) {',
  28231. '};',
  28232. 'this.s = "";',
  28233. 'this.c = "";',
  28234. 'this.u = "";',
  28235. '']),
  28236. LinesToStr([ // $mod.$main
  28237. '$mod.DoIt($mod.s);',
  28238. '$mod.DoIt($mod.c);',
  28239. '$mod.DoIt($mod.u);',
  28240. '']));
  28241. end;
  28242. procedure TTestModule.TestJSValue_OverloadChar;
  28243. begin
  28244. StartProgram(false);
  28245. Add([
  28246. 'type',
  28247. ' uni = string;',
  28248. ' WChar = char;',
  28249. 'procedure DoIt(c: char); begin end;',
  28250. 'procedure DoIt(v: jsvalue); begin end;',
  28251. 'var',
  28252. ' s: string;',
  28253. ' c: char;',
  28254. ' u: uni;',
  28255. 'begin',
  28256. ' DoIt(s);',
  28257. ' DoIt(c);',
  28258. ' DoIt(u);',
  28259. '']);
  28260. ConvertProgram;
  28261. CheckSource('TestJSValue_OverloadChar',
  28262. LinesToStr([ // statements
  28263. 'this.DoIt = function (c) {',
  28264. '};',
  28265. 'this.DoIt$1 = function (v) {',
  28266. '};',
  28267. 'this.s = "";',
  28268. 'this.c = "";',
  28269. 'this.u = "";',
  28270. '']),
  28271. LinesToStr([ // $mod.$main
  28272. '$mod.DoIt$1($mod.s);',
  28273. '$mod.DoIt($mod.c);',
  28274. '$mod.DoIt$1($mod.u);',
  28275. '']));
  28276. end;
  28277. procedure TTestModule.TestJSValue_OverloadPointer;
  28278. begin
  28279. StartProgram(false);
  28280. Add([
  28281. 'type',
  28282. ' TObject = class end;',
  28283. 'procedure DoIt(p: pointer); begin end;',
  28284. 'procedure DoIt(v: jsvalue); begin end;',
  28285. 'var',
  28286. ' o: TObject;',
  28287. 'begin',
  28288. ' DoIt(o);',
  28289. '']);
  28290. ConvertProgram;
  28291. CheckSource('TestJSValue_OverloadPointer',
  28292. LinesToStr([ // statements
  28293. 'rtl.createClass(this, "TObject", null, function () {',
  28294. ' this.$init = function () {',
  28295. ' };',
  28296. ' this.$final = function () {',
  28297. ' };',
  28298. '});',
  28299. 'this.DoIt = function (p) {',
  28300. '};',
  28301. 'this.DoIt$1 = function (v) {',
  28302. '};',
  28303. 'this.o = null;',
  28304. '']),
  28305. LinesToStr([ // $mod.$main
  28306. '$mod.DoIt($mod.o);',
  28307. '']));
  28308. end;
  28309. procedure TTestModule.TestJSValue_ForIn;
  28310. begin
  28311. StartProgram(false);
  28312. Add([
  28313. 'var',
  28314. ' v: JSValue;',
  28315. ' key: string;',
  28316. 'begin',
  28317. ' for key in v do begin',
  28318. ' if key=''abc'' then ;',
  28319. ' end;',
  28320. '']);
  28321. ConvertProgram;
  28322. CheckSource('TestJSValue_ForIn',
  28323. LinesToStr([ // statements
  28324. 'this.v = undefined;',
  28325. 'this.key = "";',
  28326. '']),
  28327. LinesToStr([ // $mod.$main
  28328. 'for ($mod.key in $mod.v) {',
  28329. ' if ($mod.key === "abc") ;',
  28330. '};',
  28331. '']));
  28332. end;
  28333. procedure TTestModule.TestRTTI_IntRange;
  28334. begin
  28335. WithTypeInfo:=true;
  28336. StartProgram(true,[supTypeInfo]);
  28337. Add([
  28338. '{$modeswitch externalclass}',
  28339. 'type',
  28340. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28341. ' TColor = type TGraphicsColor;',
  28342. 'var',
  28343. ' p: TTypeInfo;',
  28344. ' k: TTypeKind;',
  28345. 'begin',
  28346. ' p:=typeinfo(TGraphicsColor);',
  28347. ' p:=typeinfo(TColor);',
  28348. ' k:=GetTypeKind(TGraphicsColor);',
  28349. ' k:=GetTypeKind(TColor);',
  28350. '']);
  28351. ConvertProgram;
  28352. CheckSource('TestRTTI_IntRange',
  28353. LinesToStr([ // statements
  28354. 'this.$rtti.$Int("TGraphicsColor", {',
  28355. ' minvalue: -2147483648,',
  28356. ' maxvalue: 2147483647,',
  28357. ' ordtype: 4',
  28358. '});',
  28359. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28360. 'this.p = null;',
  28361. 'this.k = 0;',
  28362. '']),
  28363. LinesToStr([ // $mod.$main
  28364. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28365. '$mod.p = $mod.$rtti["TColor"];',
  28366. '$mod.k = 1;',
  28367. '$mod.k = 1;',
  28368. '']));
  28369. end;
  28370. procedure TTestModule.TestRTTI_Double;
  28371. begin
  28372. WithTypeInfo:=true;
  28373. StartProgram(true,[supTypeInfo]);
  28374. Add([
  28375. '{$modeswitch externalclass}',
  28376. 'type',
  28377. ' TFloat = type double;',
  28378. 'var',
  28379. ' p: TTypeInfo;',
  28380. 'begin',
  28381. ' p:=typeinfo(double);',
  28382. ' p:=typeinfo(TFloat);',
  28383. '']);
  28384. ConvertProgram;
  28385. CheckSource('TestRTTI_Double',
  28386. LinesToStr([ // statements
  28387. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28388. 'this.p = null;',
  28389. '']),
  28390. LinesToStr([ // $mod.$main
  28391. '$mod.p = rtl.double;',
  28392. '$mod.p = $mod.$rtti["TFloat"];',
  28393. '']));
  28394. end;
  28395. procedure TTestModule.TestRTTI_ProcType;
  28396. begin
  28397. WithTypeInfo:=true;
  28398. StartProgram(false);
  28399. Add('type');
  28400. Add(' TProcA = procedure;');
  28401. Add(' TMethodB = procedure of object;');
  28402. Add(' TProcC = procedure; varargs;');
  28403. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28404. Add(' TProcE = function: nativeint;');
  28405. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28406. Add('var p: pointer;');
  28407. Add('begin');
  28408. Add(' p:=typeinfo(tproca);');
  28409. ConvertProgram;
  28410. CheckSource('TestRTTI_ProcType',
  28411. LinesToStr([ // statements
  28412. 'this.$rtti.$ProcVar("TProcA", {',
  28413. ' procsig: rtl.newTIProcSig(null)',
  28414. '});',
  28415. 'this.$rtti.$MethodVar("TMethodB", {',
  28416. ' procsig: rtl.newTIProcSig(null),',
  28417. ' methodkind: 0',
  28418. '});',
  28419. 'this.$rtti.$ProcVar("TProcC", {',
  28420. ' procsig: rtl.newTIProcSig(null, 2)',
  28421. '});',
  28422. 'this.$rtti.$ProcVar("TProcD", {',
  28423. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28424. '});',
  28425. 'this.$rtti.$ProcVar("TProcE", {',
  28426. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28427. '});',
  28428. 'this.$rtti.$ProcVar("TProcF", {',
  28429. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28430. '});',
  28431. 'this.p = null;',
  28432. '']),
  28433. LinesToStr([ // $mod.$main
  28434. '$mod.p = $mod.$rtti["TProcA"];',
  28435. '']));
  28436. end;
  28437. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28438. begin
  28439. WithTypeInfo:=true;
  28440. AddModuleWithIntfImplSrc('unit2.pas',
  28441. LinesToStr([
  28442. 'type',
  28443. ' TObject = class end;'
  28444. ]),
  28445. '');
  28446. StartUnit(true);
  28447. Add('interface');
  28448. Add('uses unit2;');
  28449. Add('type');
  28450. Add(' TProcA = function(o: tobject): tobject;');
  28451. Add('implementation');
  28452. Add('type');
  28453. Add(' TProcB = function(o: tobject): tobject;');
  28454. Add('var p: Pointer;');
  28455. Add('initialization');
  28456. Add(' p:=typeinfo(tproca);');
  28457. Add(' p:=typeinfo(tprocb);');
  28458. ConvertUnit;
  28459. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28460. LinesToStr([ // statements
  28461. 'var $impl = $mod.$impl;',
  28462. 'this.$rtti.$ProcVar("TProcA", {',
  28463. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28464. '});',
  28465. '']),
  28466. LinesToStr([ // this.$init
  28467. '$impl.p = $mod.$rtti["TProcA"];',
  28468. '$impl.p = $mod.$rtti["TProcB"];',
  28469. '']),
  28470. LinesToStr([ // implementation
  28471. '$mod.$rtti.$ProcVar("TProcB", {',
  28472. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28473. '});',
  28474. '$impl.p = null;',
  28475. '']) );
  28476. end;
  28477. procedure TTestModule.TestRTTI_EnumAndSetType;
  28478. begin
  28479. WithTypeInfo:=true;
  28480. StartProgram(false);
  28481. Add('type');
  28482. Add(' TFlag = (light,dark);');
  28483. Add(' TFlags = set of TFlag;');
  28484. Add(' TProc = function(f: TFlags): TFlag;');
  28485. Add('var p: pointer;');
  28486. Add('begin');
  28487. Add(' p:=typeinfo(tflag);');
  28488. Add(' p:=typeinfo(tflags);');
  28489. ConvertProgram;
  28490. CheckSource('TestRTTI_EnumAndType',
  28491. LinesToStr([ // statements
  28492. 'this.TFlag = {',
  28493. ' "0": "light",',
  28494. ' light: 0,',
  28495. ' "1": "dark",',
  28496. ' dark: 1',
  28497. '};',
  28498. 'this.$rtti.$Enum("TFlag", {',
  28499. ' minvalue: 0,',
  28500. ' maxvalue: 1,',
  28501. ' ordtype: 1,',
  28502. ' enumtype: this.TFlag',
  28503. '});',
  28504. 'this.$rtti.$Set("TFlags", {',
  28505. ' comptype: this.$rtti["TFlag"]',
  28506. '});',
  28507. 'this.$rtti.$ProcVar("TProc", {',
  28508. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28509. '});',
  28510. 'this.p = null;',
  28511. '']),
  28512. LinesToStr([ // $mod.$main
  28513. '$mod.p = $mod.$rtti["TFlag"];',
  28514. '$mod.p = $mod.$rtti["TFlags"];',
  28515. '']));
  28516. end;
  28517. procedure TTestModule.TestRTTI_EnumRange;
  28518. begin
  28519. WithTypeInfo:=true;
  28520. StartProgram(false);
  28521. Add([
  28522. 'type',
  28523. ' TCol = (red,green,blue);',
  28524. ' TColRg = green..blue;',
  28525. ' TSetOfColRg = set of TColRg;',
  28526. 'var p: pointer;',
  28527. 'begin',
  28528. ' p:=typeinfo(tcolrg);',
  28529. ' p:=typeinfo(tsetofcolrg);',
  28530. '']);
  28531. ConvertProgram;
  28532. end;
  28533. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28534. begin
  28535. WithTypeInfo:=true;
  28536. StartProgram(false);
  28537. Add('type');
  28538. Add(' TFlags = set of (red, green);');
  28539. Add('var');
  28540. Add(' f: TFlags;');
  28541. Add('begin');
  28542. Add(' Include(f,red);');
  28543. ConvertProgram;
  28544. CheckSource('TestRTTI_AnonymousEnumType',
  28545. LinesToStr([ // statements
  28546. 'this.TFlags$a = {',
  28547. ' "0": "red",',
  28548. ' red: 0,',
  28549. ' "1": "green",',
  28550. ' green: 1',
  28551. '};',
  28552. 'this.$rtti.$Enum("TFlags$a", {',
  28553. ' minvalue: 0,',
  28554. ' maxvalue: 1,',
  28555. ' ordtype: 1,',
  28556. ' enumtype: this.TFlags$a',
  28557. '});',
  28558. 'this.$rtti.$Set("TFlags", {',
  28559. ' comptype: this.$rtti["TFlags$a"]',
  28560. '});',
  28561. 'this.f = {};',
  28562. '']),
  28563. LinesToStr([
  28564. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28565. '']));
  28566. end;
  28567. procedure TTestModule.TestRTTI_StaticArray;
  28568. begin
  28569. WithTypeInfo:=true;
  28570. StartProgram(false);
  28571. Add('type');
  28572. Add(' TFlag = (light,dark);');
  28573. Add(' TFlagNames = array[TFlag] of string;');
  28574. Add(' TBoolNames = array[boolean] of string;');
  28575. Add(' TByteArray = array[1..32768] of byte;');
  28576. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28577. Add('var p: pointer;');
  28578. Add('begin');
  28579. Add(' p:=typeinfo(TFlagNames);');
  28580. Add(' p:=typeinfo(TBoolNames);');
  28581. ConvertProgram;
  28582. CheckSource('TestRTTI_StaticArray',
  28583. LinesToStr([ // statements
  28584. 'this.TFlag = {',
  28585. ' "0": "light",',
  28586. ' light: 0,',
  28587. ' "1": "dark",',
  28588. ' dark: 1',
  28589. '};',
  28590. 'this.$rtti.$Enum("TFlag", {',
  28591. ' minvalue: 0,',
  28592. ' maxvalue: 1,',
  28593. ' ordtype: 1,',
  28594. ' enumtype: this.TFlag',
  28595. '});',
  28596. 'this.$rtti.$StaticArray("TFlagNames", {',
  28597. ' dims: [2],',
  28598. ' eltype: rtl.string',
  28599. '});',
  28600. 'this.$rtti.$StaticArray("TBoolNames", {',
  28601. ' dims: [2],',
  28602. ' eltype: rtl.string',
  28603. '});',
  28604. 'this.$rtti.$StaticArray("TByteArray", {',
  28605. ' dims: [32768],',
  28606. ' eltype: rtl.byte',
  28607. '});',
  28608. 'this.$rtti.$ProcVar("TProc", {',
  28609. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28610. '});',
  28611. 'this.p = null;',
  28612. '']),
  28613. LinesToStr([ // $mod.$main
  28614. '$mod.p = $mod.$rtti["TFlagNames"];',
  28615. '$mod.p = $mod.$rtti["TBoolNames"];',
  28616. '']));
  28617. end;
  28618. procedure TTestModule.TestRTTI_DynArray;
  28619. begin
  28620. WithTypeInfo:=true;
  28621. StartProgram(false);
  28622. Add('type');
  28623. Add(' TArrStr = array of string;');
  28624. Add(' TArr2Dim = array of tarrstr;');
  28625. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28626. Add('var p: pointer;');
  28627. Add('begin');
  28628. Add(' p:=typeinfo(tarrstr);');
  28629. Add(' p:=typeinfo(tarr2dim);');
  28630. ConvertProgram;
  28631. CheckSource('TestRTTI_DynArray',
  28632. LinesToStr([ // statements
  28633. 'this.$rtti.$DynArray("TArrStr", {',
  28634. ' eltype: rtl.string',
  28635. '});',
  28636. 'this.$rtti.$DynArray("TArr2Dim", {',
  28637. ' eltype: this.$rtti["TArrStr"]',
  28638. '});',
  28639. 'this.$rtti.$ProcVar("TProc", {',
  28640. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28641. '});',
  28642. 'this.p = null;',
  28643. '']),
  28644. LinesToStr([ // $mod.$main
  28645. '$mod.p = $mod.$rtti["TArrStr"];',
  28646. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28647. '']));
  28648. end;
  28649. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28650. begin
  28651. WithTypeInfo:=true;
  28652. StartProgram(false);
  28653. Add('type');
  28654. Add(' TArr = array of array of longint;');
  28655. Add('var a: TArr;');
  28656. Add('begin');
  28657. ConvertProgram;
  28658. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28659. LinesToStr([ // statements
  28660. 'this.$rtti.$DynArray("TArr$a", {',
  28661. ' eltype: rtl.longint',
  28662. '});',
  28663. 'this.$rtti.$DynArray("TArr", {',
  28664. ' eltype: this.$rtti["TArr$a"]',
  28665. '});',
  28666. 'this.a = [];',
  28667. '']),
  28668. LinesToStr([ // $mod.$main
  28669. ]));
  28670. end;
  28671. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28672. begin
  28673. WithTypeInfo:=true;
  28674. StartProgram(false);
  28675. Add('type');
  28676. Add(' TObject = class');
  28677. Add(' published');
  28678. Add(' procedure Proc; virtual; abstract;');
  28679. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28680. Add(' end;');
  28681. Add('begin');
  28682. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28683. nDuplicatePublishedMethodXAtY);
  28684. ConvertProgram;
  28685. end;
  28686. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28687. begin
  28688. WithTypeInfo:=true;
  28689. StartProgram(false);
  28690. Add('type');
  28691. Add(' TObject = class');
  28692. Add(' published');
  28693. Add(' procedure Proc; external name ''foo'';');
  28694. Add(' end;');
  28695. Add('begin');
  28696. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28697. nPublishedNameMustMatchExternal);
  28698. ConvertProgram;
  28699. end;
  28700. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28701. begin
  28702. WithTypeInfo:=true;
  28703. StartProgram(false);
  28704. Add('type');
  28705. Add(' TObject = class');
  28706. Add(' class var FA: longint;');
  28707. Add(' published');
  28708. Add(' class property A: longint read FA;');
  28709. Add(' end;');
  28710. Add('begin');
  28711. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28712. nInvalidXModifierY);
  28713. ConvertProgram;
  28714. end;
  28715. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28716. begin
  28717. WithTypeInfo:=true;
  28718. StartProgram(false);
  28719. Add('type');
  28720. Add(' TObject = class');
  28721. Add(' published');
  28722. Add(' class var FA: longint;');
  28723. Add(' end;');
  28724. Add('begin');
  28725. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28726. nSymbolCannotBePublished);
  28727. ConvertProgram;
  28728. end;
  28729. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28730. begin
  28731. WithTypeInfo:=true;
  28732. StartProgram(false);
  28733. Add('{$modeswitch externalclass}');
  28734. Add('type');
  28735. Add(' TObject = class');
  28736. Add(' published');
  28737. Add(' V: longint; external name ''foo'';');
  28738. Add(' end;');
  28739. Add('begin');
  28740. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28741. nPublishedNameMustMatchExternal);
  28742. ConvertProgram;
  28743. end;
  28744. procedure TTestModule.TestRTTI_Class_Field;
  28745. begin
  28746. WithTypeInfo:=true;
  28747. StartProgram(false);
  28748. Add('{$modeswitch externalclass}');
  28749. Add('type');
  28750. Add(' TObject = class');
  28751. Add(' private');
  28752. Add(' FPropA: string;');
  28753. Add(' published');
  28754. Add(' VarLI: longint;');
  28755. Add(' VarC: char;');
  28756. Add(' VarS: string;');
  28757. Add(' VarD: double;');
  28758. Add(' VarB: boolean;');
  28759. Add(' VarLW: longword;');
  28760. Add(' VarSmI: smallint;');
  28761. Add(' VarW: word;');
  28762. Add(' VarShI: shortint;');
  28763. Add(' VarBy: byte;');
  28764. Add(' VarExt: longint external name ''VarExt'';');
  28765. Add(' ArrA, ArrB: array of byte;');
  28766. Add(' end;');
  28767. Add('var p: pointer;');
  28768. Add(' Obj: tobject;');
  28769. Add('begin');
  28770. Add(' p:=typeinfo(tobject);');
  28771. Add(' p:=typeinfo(p);');
  28772. Add(' p:=typeinfo(obj);');
  28773. ConvertProgram;
  28774. CheckSource('TestRTTI_Class_Field',
  28775. LinesToStr([ // statements
  28776. 'rtl.createClass(this, "TObject", null, function () {',
  28777. ' this.$init = function () {',
  28778. ' this.FPropA = "";',
  28779. ' this.VarLI = 0;',
  28780. ' this.VarC = "";',
  28781. ' this.VarS = "";',
  28782. ' this.VarD = 0.0;',
  28783. ' this.VarB = false;',
  28784. ' this.VarLW = 0;',
  28785. ' this.VarSmI = 0;',
  28786. ' this.VarW = 0;',
  28787. ' this.VarShI = 0;',
  28788. ' this.VarBy = 0;',
  28789. ' this.ArrA = [];',
  28790. ' this.ArrB = [];',
  28791. ' };',
  28792. ' this.$final = function () {',
  28793. ' this.ArrA = undefined;',
  28794. ' this.ArrB = undefined;',
  28795. ' };',
  28796. ' var $r = this.$rtti;',
  28797. ' $r.addField("VarLI", rtl.longint);',
  28798. ' $r.addField("VarC", rtl.char);',
  28799. ' $r.addField("VarS", rtl.string);',
  28800. ' $r.addField("VarD", rtl.double);',
  28801. ' $r.addField("VarB", rtl.boolean);',
  28802. ' $r.addField("VarLW", rtl.longword);',
  28803. ' $r.addField("VarSmI", rtl.smallint);',
  28804. ' $r.addField("VarW", rtl.word);',
  28805. ' $r.addField("VarShI", rtl.shortint);',
  28806. ' $r.addField("VarBy", rtl.byte);',
  28807. ' $r.addField("VarExt", rtl.longint);',
  28808. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28809. ' eltype: rtl.byte',
  28810. ' });',
  28811. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28812. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28813. '});',
  28814. 'this.p = null;',
  28815. 'this.Obj = null;',
  28816. '']),
  28817. LinesToStr([ // $mod.$main
  28818. '$mod.p = $mod.$rtti["TObject"];',
  28819. '$mod.p = rtl.pointer;',
  28820. '$mod.p = $mod.Obj.$rtti;',
  28821. '']));
  28822. end;
  28823. procedure TTestModule.TestRTTI_Class_Method;
  28824. begin
  28825. WithTypeInfo:=true;
  28826. StartProgram(false);
  28827. Add('type');
  28828. Add(' TObject = class');
  28829. Add(' private');
  28830. Add(' procedure Internal; external name ''$intern'';');
  28831. Add(' published');
  28832. Add(' procedure Click; virtual; abstract;');
  28833. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28834. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28835. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28836. Add(' end;');
  28837. Add('begin');
  28838. ConvertProgram;
  28839. CheckSource('TestRTTI_Class_Method',
  28840. LinesToStr([ // statements
  28841. 'rtl.createClass(this, "TObject", null, function () {',
  28842. ' this.$init = function () {',
  28843. ' };',
  28844. ' this.$final = function () {',
  28845. ' };',
  28846. ' var $r = this.$rtti;',
  28847. ' $r.addMethod("Click", 0, null);',
  28848. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28849. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28850. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28851. ' flags: 2',
  28852. ' });',
  28853. '});',
  28854. '']),
  28855. LinesToStr([ // $mod.$main
  28856. '']));
  28857. end;
  28858. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28859. begin
  28860. WithTypeInfo:=true;
  28861. StartProgram(false);
  28862. Add('type');
  28863. Add(' TObject = class');
  28864. Add(' published');
  28865. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28866. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28867. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28868. Add(' end;');
  28869. Add('begin');
  28870. ConvertProgram;
  28871. CheckSource('TestRTTI_Class_MethodOpenArray',
  28872. LinesToStr([ // statements
  28873. 'rtl.createClass(this, "TObject", null, function () {',
  28874. ' this.$init = function () {',
  28875. ' };',
  28876. ' this.$final = function () {',
  28877. ' };',
  28878. ' var $r = this.$rtti;',
  28879. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28880. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28881. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28882. '});',
  28883. '']),
  28884. LinesToStr([ // $mod.$main
  28885. '']));
  28886. end;
  28887. procedure TTestModule.TestRTTI_Class_Property;
  28888. begin
  28889. WithTypeInfo:=true;
  28890. StartProgram(false);
  28891. Add('{$modeswitch externalclass}');
  28892. Add('type');
  28893. Add(' TObject = class');
  28894. Add(' private');
  28895. Add(' FColor: longint;');
  28896. Add(' FColorStored: boolean;');
  28897. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28898. Add(' function GetColor: longint; virtual; abstract;');
  28899. Add(' function GetColorStored: boolean; virtual; abstract;');
  28900. Add(' FExtSize: longint external name ''$extSize'';');
  28901. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28902. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28903. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28904. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28905. Add(' published');
  28906. Add(' property ColorA: longint read FColor;');
  28907. Add(' property ColorB: longint write FColor;');
  28908. Add(' property ColorC: longint read GetColor write SetColor;');
  28909. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28910. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28911. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28912. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28913. Add(' end;');
  28914. Add('begin');
  28915. ConvertProgram;
  28916. CheckSource('TestRTTI_Class_Property',
  28917. LinesToStr([ // statements
  28918. 'rtl.createClass(this, "TObject", null, function () {',
  28919. ' this.$init = function () {',
  28920. ' this.FColor = 0;',
  28921. ' this.FColorStored = false;',
  28922. ' };',
  28923. ' this.$final = function () {',
  28924. ' };',
  28925. ' var $r = this.$rtti;',
  28926. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28927. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28928. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28929. ' $r.addProperty(',
  28930. ' "ColorD",',
  28931. ' 8,',
  28932. ' rtl.longint,',
  28933. ' "FColor",',
  28934. ' "FColor",',
  28935. ' {',
  28936. ' stored: "FColorStored"',
  28937. ' }',
  28938. ' );',
  28939. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28940. ' $r.addProperty(',
  28941. ' "ExtSizeB",',
  28942. ' 11,',
  28943. ' rtl.longint,',
  28944. ' "$getSize",',
  28945. ' "$setSize",',
  28946. ' {',
  28947. ' stored: "$extSizeStored"',
  28948. ' }',
  28949. ' );',
  28950. ' $r.addProperty(',
  28951. ' "ExtSizeC",',
  28952. ' 12,',
  28953. ' rtl.longint,',
  28954. ' "$extSize",',
  28955. ' "$extSize",',
  28956. ' {',
  28957. ' stored: "$getExtSizeStored"',
  28958. ' }',
  28959. ' );',
  28960. '});',
  28961. '']),
  28962. LinesToStr([ // $mod.$main
  28963. '']));
  28964. end;
  28965. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28966. begin
  28967. WithTypeInfo:=true;
  28968. StartProgram(false);
  28969. Add('{$modeswitch externalclass}');
  28970. Add('type');
  28971. Add(' integer = longint;');
  28972. Add(' TObject = class');
  28973. Add(' private');
  28974. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28975. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28976. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28977. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28978. Add(' published');
  28979. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28980. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28981. Add(' end;');
  28982. Add('begin');
  28983. ConvertProgram;
  28984. CheckSource('TestRTTI_Class_PropertyParams',
  28985. LinesToStr([ // statements
  28986. 'rtl.createClass(this, "TObject", null, function () {',
  28987. ' this.$init = function () {',
  28988. ' };',
  28989. ' this.$final = function () {',
  28990. ' };',
  28991. ' var $r = this.$rtti;',
  28992. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28993. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28994. '});',
  28995. '']),
  28996. LinesToStr([ // $mod.$main
  28997. '']));
  28998. end;
  28999. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29000. begin
  29001. WithTypeInfo:=true;
  29002. AddModuleWithIntfImplSrc('unit1.pas',
  29003. 'type TColor = -5..5;',
  29004. '');
  29005. StartProgram(true);
  29006. Add([
  29007. 'uses unit1;',
  29008. 'type',
  29009. ' TColorAlias = TColor;',
  29010. ' TColorTypeAlias = type TColor;',
  29011. ' TObject = class',
  29012. ' private',
  29013. ' fColor: TColor;',
  29014. ' fAlias: TColorAlias;',
  29015. ' fTypeAlias: TColorTypeAlias;',
  29016. ' published',
  29017. ' property Color: TColor read fcolor;',
  29018. ' property Alias: TColorAlias read falias;',
  29019. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29020. ' end;',
  29021. 'begin',
  29022. '']);
  29023. ConvertProgram;
  29024. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29025. LinesToStr([ // statements
  29026. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29027. 'rtl.createClass(this, "TObject", null, function () {',
  29028. ' this.$init = function () {',
  29029. ' this.fColor = 0;',
  29030. ' this.fAlias = 0;',
  29031. ' this.fTypeAlias = 0;',
  29032. ' };',
  29033. ' this.$final = function () {',
  29034. ' };',
  29035. ' var $r = this.$rtti;',
  29036. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29037. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29038. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29039. '});',
  29040. '']),
  29041. LinesToStr([ // $mod.$main
  29042. '']));
  29043. end;
  29044. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29045. begin
  29046. WithTypeInfo:=true;
  29047. StartProgram(false);
  29048. Add([
  29049. '{$modeswitch omitrtti}',
  29050. 'type',
  29051. ' TObject = class',
  29052. ' private',
  29053. ' FA: byte;',
  29054. ' published',
  29055. ' property A: byte read FA write FA;',
  29056. ' end;',
  29057. 'begin']);
  29058. ConvertProgram;
  29059. CheckSource('TestRTTI_Class_OmitRTTI',
  29060. LinesToStr([ // statements
  29061. 'rtl.createClass(this, "TObject", null, function () {',
  29062. ' this.$init = function () {',
  29063. ' this.FA = 0;',
  29064. ' };',
  29065. ' this.$final = function () {',
  29066. ' };',
  29067. '});',
  29068. '']),
  29069. LinesToStr([ // $mod.$main
  29070. '']));
  29071. end;
  29072. procedure TTestModule.TestRTTI_IndexModifier;
  29073. begin
  29074. WithTypeInfo:=true;
  29075. StartProgram(false);
  29076. Add([
  29077. 'type',
  29078. ' TEnum = (red, blue);',
  29079. ' TObject = class',
  29080. ' FB: boolean;',
  29081. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29082. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29083. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29084. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29085. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29086. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29087. ' published',
  29088. ' property B1: boolean index 1 read FB write SetIntBool;',
  29089. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29090. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29091. ' end;',
  29092. 'begin']);
  29093. ConvertProgram;
  29094. CheckSource('TestRTTI_IndexModifier',
  29095. LinesToStr([ // statements
  29096. 'this.TEnum = {',
  29097. ' "0": "red",',
  29098. ' red: 0,',
  29099. ' "1": "blue",',
  29100. ' blue: 1',
  29101. '};',
  29102. 'this.$rtti.$Enum("TEnum", {',
  29103. ' minvalue: 0,',
  29104. ' maxvalue: 1,',
  29105. ' ordtype: 1,',
  29106. ' enumtype: this.TEnum',
  29107. '});',
  29108. 'rtl.createClass(this, "TObject", null, function () {',
  29109. ' this.$init = function () {',
  29110. ' this.FB = false;',
  29111. ' };',
  29112. ' this.$final = function () {',
  29113. ' };',
  29114. ' var $r = this.$rtti;',
  29115. ' $r.addProperty(',
  29116. ' "B1",',
  29117. ' 18,',
  29118. ' rtl.boolean,',
  29119. ' "FB",',
  29120. ' "SetIntBool",',
  29121. ' {',
  29122. ' index: 1',
  29123. ' }',
  29124. ' );',
  29125. ' $r.addProperty(',
  29126. ' "B2",',
  29127. ' 17,',
  29128. ' rtl.boolean,',
  29129. ' "GetEnumBool",',
  29130. ' "FB",',
  29131. ' {',
  29132. ' index: $mod.TEnum.blue',
  29133. ' }',
  29134. ' );',
  29135. ' $r.addProperty(',
  29136. ' "I1",',
  29137. ' 19,',
  29138. ' rtl.boolean,',
  29139. ' "GetStrIntBool",',
  29140. ' "SetStrIntBool",',
  29141. ' {',
  29142. ' index: 2',
  29143. ' }',
  29144. ' );',
  29145. '});',
  29146. '']),
  29147. LinesToStr([ // $mod.$main
  29148. '']));
  29149. end;
  29150. procedure TTestModule.TestRTTI_StoredModifier;
  29151. begin
  29152. WithTypeInfo:=true;
  29153. StartProgram(false);
  29154. Add([
  29155. 'const',
  29156. ' ConstB = true;',
  29157. 'type',
  29158. ' TObject = class',
  29159. ' private',
  29160. ' FB: boolean;',
  29161. ' function IsBStored: boolean; virtual; abstract;',
  29162. ' published',
  29163. ' property BoolA: boolean read FB stored true;',
  29164. ' property BoolB: boolean read FB stored false;',
  29165. ' property BoolC: boolean read FB stored FB;',
  29166. ' property BoolD: boolean read FB stored ConstB;',
  29167. ' property BoolE: boolean read FB stored IsBStored;',
  29168. ' end;',
  29169. 'begin']);
  29170. ConvertProgram;
  29171. CheckSource('TestRTTI_StoredModifier',
  29172. LinesToStr([ // statements
  29173. 'this.ConstB = true;',
  29174. 'rtl.createClass(this, "TObject", null, function () {',
  29175. ' this.$init = function () {',
  29176. ' this.FB = false;',
  29177. ' };',
  29178. ' this.$final = function () {',
  29179. ' };',
  29180. ' var $r = this.$rtti;',
  29181. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29182. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29183. ' $r.addProperty(',
  29184. ' "BoolC",',
  29185. ' 8,',
  29186. ' rtl.boolean,',
  29187. ' "FB",',
  29188. ' "",',
  29189. ' {',
  29190. ' stored: "FB"',
  29191. ' }',
  29192. ' );',
  29193. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29194. ' $r.addProperty(',
  29195. ' "BoolE",',
  29196. ' 12,',
  29197. ' rtl.boolean,',
  29198. ' "FB",',
  29199. ' "",',
  29200. ' {',
  29201. ' stored: "IsBStored"',
  29202. ' }',
  29203. ' );',
  29204. '});',
  29205. '']),
  29206. LinesToStr([ // $mod.$main
  29207. '']));
  29208. end;
  29209. procedure TTestModule.TestRTTI_DefaultValue;
  29210. begin
  29211. WithTypeInfo:=true;
  29212. StartProgram(false);
  29213. Add([
  29214. 'type',
  29215. ' TEnum = (red, blue);',
  29216. 'const',
  29217. ' CB = true or false;',
  29218. ' CI = 1+2;',
  29219. 'type',
  29220. ' TObject = class',
  29221. ' FB: boolean;',
  29222. ' FI: longint;',
  29223. ' FE: TEnum;',
  29224. ' published',
  29225. ' property B1: boolean read FB default true;',
  29226. ' property B2: boolean read FB default CB;',
  29227. ' property B3: boolean read FB default test1.cb;',
  29228. ' property I1: longint read FI default 2;',
  29229. ' property I2: longint read FI default CI;',
  29230. ' property E1: TEnum read FE default red;',
  29231. ' property E2: TEnum read FE default TEnum.blue;',
  29232. ' end;',
  29233. 'begin']);
  29234. ConvertProgram;
  29235. CheckSource('TestRTTI_DefaultValue',
  29236. LinesToStr([ // statements
  29237. 'this.TEnum = {',
  29238. ' "0": "red",',
  29239. ' red: 0,',
  29240. ' "1": "blue",',
  29241. ' blue: 1',
  29242. '};',
  29243. 'this.$rtti.$Enum("TEnum", {',
  29244. ' minvalue: 0,',
  29245. ' maxvalue: 1,',
  29246. ' ordtype: 1,',
  29247. ' enumtype: this.TEnum',
  29248. '});',
  29249. 'this.CB = true || false;',
  29250. 'this.CI = 1 + 2;',
  29251. 'rtl.createClass(this, "TObject", null, function () {',
  29252. ' this.$init = function () {',
  29253. ' this.FB = false;',
  29254. ' this.FI = 0;',
  29255. ' this.FE = 0;',
  29256. ' };',
  29257. ' this.$final = function () {',
  29258. ' };',
  29259. ' var $r = this.$rtti;',
  29260. ' $r.addProperty(',
  29261. ' "B1",',
  29262. ' 0,',
  29263. ' rtl.boolean,',
  29264. ' "FB",',
  29265. ' "",',
  29266. ' {',
  29267. ' Default: true',
  29268. ' }',
  29269. ' );',
  29270. ' $r.addProperty(',
  29271. ' "B2",',
  29272. ' 0,',
  29273. ' rtl.boolean,',
  29274. ' "FB",',
  29275. ' "",',
  29276. ' {',
  29277. ' Default: true',
  29278. ' }',
  29279. ' );',
  29280. ' $r.addProperty(',
  29281. ' "B3",',
  29282. ' 0,',
  29283. ' rtl.boolean,',
  29284. ' "FB",',
  29285. ' "",',
  29286. ' {',
  29287. ' Default: true',
  29288. ' }',
  29289. ' );',
  29290. ' $r.addProperty(',
  29291. ' "I1",',
  29292. ' 0,',
  29293. ' rtl.longint,',
  29294. ' "FI",',
  29295. ' "",',
  29296. ' {',
  29297. ' Default: 2',
  29298. ' }',
  29299. ' );',
  29300. ' $r.addProperty(',
  29301. ' "I2",',
  29302. ' 0,',
  29303. ' rtl.longint,',
  29304. ' "FI",',
  29305. ' "",',
  29306. ' {',
  29307. ' Default: 3',
  29308. ' }',
  29309. ' );',
  29310. ' $r.addProperty(',
  29311. ' "E1",',
  29312. ' 0,',
  29313. ' $mod.$rtti["TEnum"],',
  29314. ' "FE",',
  29315. ' "",',
  29316. ' {',
  29317. ' Default: $mod.TEnum.red',
  29318. ' }',
  29319. ' );',
  29320. ' $r.addProperty(',
  29321. ' "E2",',
  29322. ' 0,',
  29323. ' $mod.$rtti["TEnum"],',
  29324. ' "FE",',
  29325. ' "",',
  29326. ' {',
  29327. ' Default: $mod.TEnum.blue',
  29328. ' }',
  29329. ' );',
  29330. '});',
  29331. '']),
  29332. LinesToStr([ // $mod.$main
  29333. '']));
  29334. end;
  29335. procedure TTestModule.TestRTTI_DefaultValueSet;
  29336. begin
  29337. WithTypeInfo:=true;
  29338. StartProgram(false);
  29339. Add([
  29340. 'type',
  29341. ' TEnum = (red, blue);',
  29342. ' TSet = set of TEnum;',
  29343. 'const',
  29344. ' CSet = [red,blue];',
  29345. 'type',
  29346. ' TObject = class',
  29347. ' FSet: TSet;',
  29348. ' published',
  29349. ' property Set1: TSet read FSet default [];',
  29350. ' property Set2: TSet read FSet default [red];',
  29351. ' property Set3: TSet read FSet default [red,blue];',
  29352. ' property Set4: TSet read FSet default CSet;',
  29353. ' end;',
  29354. 'begin']);
  29355. ConvertProgram;
  29356. CheckSource('TestRTTI_DefaultValueSet',
  29357. LinesToStr([ // statements
  29358. 'this.TEnum = {',
  29359. ' "0": "red",',
  29360. ' red: 0,',
  29361. ' "1": "blue",',
  29362. ' blue: 1',
  29363. '};',
  29364. 'this.$rtti.$Enum("TEnum", {',
  29365. ' minvalue: 0,',
  29366. ' maxvalue: 1,',
  29367. ' ordtype: 1,',
  29368. ' enumtype: this.TEnum',
  29369. '});',
  29370. 'this.$rtti.$Set("TSet", {',
  29371. ' comptype: this.$rtti["TEnum"]',
  29372. '});',
  29373. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29374. 'rtl.createClass(this, "TObject", null, function () {',
  29375. ' this.$init = function () {',
  29376. ' this.FSet = {};',
  29377. ' };',
  29378. ' this.$final = function () {',
  29379. ' this.FSet = undefined;',
  29380. ' };',
  29381. ' var $r = this.$rtti;',
  29382. ' $r.addProperty(',
  29383. ' "Set1",',
  29384. ' 0,',
  29385. ' $mod.$rtti["TSet"],',
  29386. ' "FSet",',
  29387. ' "",',
  29388. ' {',
  29389. ' Default: {}',
  29390. ' }',
  29391. ' );',
  29392. ' $r.addProperty(',
  29393. ' "Set2",',
  29394. ' 0,',
  29395. ' $mod.$rtti["TSet"],',
  29396. ' "FSet",',
  29397. ' "",',
  29398. ' {',
  29399. ' Default: rtl.createSet($mod.TEnum.red)',
  29400. ' }',
  29401. ' );',
  29402. ' $r.addProperty(',
  29403. ' "Set3",',
  29404. ' 0,',
  29405. ' $mod.$rtti["TSet"],',
  29406. ' "FSet",',
  29407. ' "",',
  29408. ' {',
  29409. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29410. ' }',
  29411. ' );',
  29412. ' $r.addProperty(',
  29413. ' "Set4",',
  29414. ' 0,',
  29415. ' $mod.$rtti["TSet"],',
  29416. ' "FSet",',
  29417. ' "",',
  29418. ' {',
  29419. ' Default: $mod.CSet',
  29420. ' }',
  29421. ' );',
  29422. '});',
  29423. '']),
  29424. LinesToStr([ // $mod.$main
  29425. '']));
  29426. end;
  29427. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29428. begin
  29429. WithTypeInfo:=true;
  29430. StartProgram(false);
  29431. Add([
  29432. 'type',
  29433. ' TRg = -1..1;',
  29434. 'const',
  29435. ' l = low(TRg);',
  29436. ' h = high(TRg);',
  29437. 'type',
  29438. ' TObject = class',
  29439. ' FV: TRg;',
  29440. ' published',
  29441. ' property V1: TRg read FV default -1;',
  29442. ' end;',
  29443. 'begin']);
  29444. ConvertProgram;
  29445. CheckSource('TestRTTI_DefaultValueRangeType',
  29446. LinesToStr([ // statements
  29447. 'this.$rtti.$Int("TRg", {',
  29448. ' minvalue: -1,',
  29449. ' maxvalue: 1,',
  29450. ' ordtype: 0',
  29451. '});',
  29452. 'this.l = -1;',
  29453. 'this.h = 1;',
  29454. 'rtl.createClass(this, "TObject", null, function () {',
  29455. ' this.$init = function () {',
  29456. ' this.FV = 0;',
  29457. ' };',
  29458. ' this.$final = function () {',
  29459. ' };',
  29460. ' var $r = this.$rtti;',
  29461. ' $r.addProperty(',
  29462. ' "V1",',
  29463. ' 0,',
  29464. ' $mod.$rtti["TRg"],',
  29465. ' "FV",',
  29466. ' "",',
  29467. ' {',
  29468. ' Default: -1',
  29469. ' }',
  29470. ' );',
  29471. '});',
  29472. '']),
  29473. LinesToStr([ // $mod.$main
  29474. '']));
  29475. end;
  29476. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29477. begin
  29478. WithTypeInfo:=true;
  29479. StartProgram(false);
  29480. Add([
  29481. 'type',
  29482. ' TObject = class',
  29483. ' FA, FB: byte;',
  29484. ' property A: byte read FA default 1;',
  29485. ' property B: byte read FB default 2;',
  29486. ' end;',
  29487. ' TBird = class',
  29488. ' published',
  29489. ' property A;',
  29490. ' property B nodefault;',
  29491. ' end;',
  29492. 'begin']);
  29493. ConvertProgram;
  29494. CheckSource('TestRTTI_DefaultValueInherit',
  29495. LinesToStr([ // statements
  29496. 'rtl.createClass(this, "TObject", null, function () {',
  29497. ' this.$init = function () {',
  29498. ' this.FA = 0;',
  29499. ' this.FB = 0;',
  29500. ' };',
  29501. ' this.$final = function () {',
  29502. ' };',
  29503. '});',
  29504. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29505. ' var $r = this.$rtti;',
  29506. ' $r.addProperty(',
  29507. ' "A",',
  29508. ' 0,',
  29509. ' rtl.byte,',
  29510. ' "FA",',
  29511. ' "",',
  29512. ' {',
  29513. ' Default: 1',
  29514. ' }',
  29515. ' );',
  29516. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29517. '});',
  29518. '']),
  29519. LinesToStr([ // $mod.$main
  29520. '']));
  29521. end;
  29522. procedure TTestModule.TestRTTI_OverrideMethod;
  29523. begin
  29524. WithTypeInfo:=true;
  29525. StartProgram(false);
  29526. Add('type');
  29527. Add(' TObject = class');
  29528. Add(' published');
  29529. Add(' procedure DoIt; virtual; abstract;');
  29530. Add(' end;');
  29531. Add(' TSky = class');
  29532. Add(' published');
  29533. Add(' procedure DoIt; override;');
  29534. Add(' end;');
  29535. Add('procedure TSky.DoIt; begin end;');
  29536. Add('begin');
  29537. ConvertProgram;
  29538. CheckSource('TestRTTI_OverrideMethod',
  29539. LinesToStr([ // statements
  29540. 'rtl.createClass(this, "TObject", null, function () {',
  29541. ' this.$init = function () {',
  29542. ' };',
  29543. ' this.$final = function () {',
  29544. ' };',
  29545. ' var $r = this.$rtti;',
  29546. ' $r.addMethod("DoIt", 0, null);',
  29547. '});',
  29548. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29549. ' this.DoIt = function () {',
  29550. ' };',
  29551. '});',
  29552. '']),
  29553. LinesToStr([ // $mod.$main
  29554. '']));
  29555. end;
  29556. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29557. begin
  29558. WithTypeInfo:=true;
  29559. StartProgram(false);
  29560. Add([
  29561. 'type',
  29562. ' TObject = class',
  29563. ' published',
  29564. ' procedure DoIt;',
  29565. ' end;',
  29566. ' TSky = class',
  29567. ' published',
  29568. ' procedure DoIt; reintroduce;',
  29569. ' end;',
  29570. 'procedure TObject.DoIt; begin end;',
  29571. 'procedure TSky.DoIt;',
  29572. 'begin',
  29573. ' inherited DoIt;',
  29574. 'end;',
  29575. 'begin']);
  29576. ConvertProgram;
  29577. CheckSource('TestRTTI_ReintroduceMethod',
  29578. LinesToStr([ // statements
  29579. 'rtl.createClass(this, "TObject", null, function () {',
  29580. ' this.$init = function () {',
  29581. ' };',
  29582. ' this.$final = function () {',
  29583. ' };',
  29584. ' this.DoIt = function () {',
  29585. ' };',
  29586. ' var $r = this.$rtti;',
  29587. ' $r.addMethod("DoIt", 0, null);',
  29588. '});',
  29589. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29590. ' this.DoIt = function () {',
  29591. ' $mod.TObject.DoIt.call(this);',
  29592. ' };',
  29593. ' var $r = this.$rtti;',
  29594. ' $r.addMethod("DoIt", 0, null);',
  29595. '});',
  29596. '']),
  29597. LinesToStr([ // $mod.$main
  29598. '']));
  29599. end;
  29600. procedure TTestModule.TestRTTI_OverloadProperty;
  29601. begin
  29602. WithTypeInfo:=true;
  29603. StartProgram(false);
  29604. Add('type');
  29605. Add(' TObject = class');
  29606. Add(' protected');
  29607. Add(' FFlag: longint;');
  29608. Add(' published');
  29609. Add(' property Flag: longint read fflag;');
  29610. Add(' end;');
  29611. Add(' TSky = class');
  29612. Add(' published');
  29613. Add(' property FLAG: longint write fflag;');
  29614. Add(' end;');
  29615. Add('begin');
  29616. ConvertProgram;
  29617. CheckSource('TestRTTI_OverrideMethod',
  29618. LinesToStr([ // statements
  29619. 'rtl.createClass(this, "TObject", null, function () {',
  29620. ' this.$init = function () {',
  29621. ' this.FFlag = 0;',
  29622. ' };',
  29623. ' this.$final = function () {',
  29624. ' };',
  29625. ' var $r = this.$rtti;',
  29626. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29627. '});',
  29628. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29629. ' var $r = this.$rtti;',
  29630. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29631. '});',
  29632. '']),
  29633. LinesToStr([ // $mod.$main
  29634. '']));
  29635. end;
  29636. procedure TTestModule.TestRTTI_ClassForward;
  29637. begin
  29638. WithTypeInfo:=true;
  29639. StartProgram(false);
  29640. Add('type');
  29641. Add(' TObject = class end;');
  29642. Add(' tbridge = class;');
  29643. Add(' TProc = function: tbridge;');
  29644. Add(' TOger = class');
  29645. Add(' published');
  29646. Add(' FBridge: tbridge;');
  29647. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29648. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29649. Add(' end;');
  29650. Add(' TBridge = class');
  29651. Add(' FOger: toger;');
  29652. Add(' end;');
  29653. Add('var p: Pointer;');
  29654. Add(' b: tbridge;');
  29655. Add('begin');
  29656. Add(' p:=typeinfo(tbridge);');
  29657. Add(' p:=typeinfo(b);');
  29658. ConvertProgram;
  29659. CheckSource('TestRTTI_ClassForward',
  29660. LinesToStr([ // statements
  29661. 'rtl.createClass(this, "TObject", null, function () {',
  29662. ' this.$init = function () {',
  29663. ' };',
  29664. ' this.$final = function () {',
  29665. ' };',
  29666. '});',
  29667. 'this.$rtti.$Class("TBridge");',
  29668. 'this.$rtti.$ProcVar("TProc", {',
  29669. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29670. '});',
  29671. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29672. ' this.$init = function () {',
  29673. ' $mod.TObject.$init.call(this);',
  29674. ' this.FBridge = null;',
  29675. ' };',
  29676. ' this.$final = function () {',
  29677. ' this.FBridge = undefined;',
  29678. ' $mod.TObject.$final.call(this);',
  29679. ' };',
  29680. ' var $r = this.$rtti;',
  29681. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29682. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29683. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29684. '});',
  29685. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29686. ' this.$init = function () {',
  29687. ' $mod.TObject.$init.call(this);',
  29688. ' this.FOger = null;',
  29689. ' };',
  29690. ' this.$final = function () {',
  29691. ' this.FOger = undefined;',
  29692. ' $mod.TObject.$final.call(this);',
  29693. ' };',
  29694. '});',
  29695. 'this.p = null;',
  29696. 'this.b = null;',
  29697. '']),
  29698. LinesToStr([ // $mod.$main
  29699. '$mod.p = $mod.$rtti["TBridge"];',
  29700. '$mod.p = $mod.b.$rtti;',
  29701. '']));
  29702. end;
  29703. procedure TTestModule.TestRTTI_ClassOf;
  29704. begin
  29705. WithTypeInfo:=true;
  29706. StartProgram(false);
  29707. Add('type');
  29708. Add(' TClass = class of tobject;');
  29709. Add(' TProcA = function: TClass;');
  29710. Add(' TObject = class');
  29711. Add(' published');
  29712. Add(' C: tclass;');
  29713. Add(' end;');
  29714. Add(' tfox = class;');
  29715. Add(' TBird = class end;');
  29716. Add(' TBirds = class of tbird;');
  29717. Add(' TFox = class end;');
  29718. Add(' TFoxes = class of tfox;');
  29719. Add(' TCows = class of TCow;');
  29720. Add(' TCow = class;');
  29721. Add(' TCow = class end;');
  29722. Add('begin');
  29723. ConvertProgram;
  29724. CheckSource('TestRTTI_ClassOf',
  29725. LinesToStr([ // statements
  29726. 'this.$rtti.$Class("TObject");',
  29727. 'this.$rtti.$ClassRef("TClass", {',
  29728. ' instancetype: this.$rtti["TObject"]',
  29729. '});',
  29730. 'this.$rtti.$ProcVar("TProcA", {',
  29731. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29732. '});',
  29733. 'rtl.createClass(this, "TObject", null, function () {',
  29734. ' this.$init = function () {',
  29735. ' this.C = null;',
  29736. ' };',
  29737. ' this.$final = function () {',
  29738. ' this.C = undefined;',
  29739. ' };',
  29740. ' var $r = this.$rtti;',
  29741. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29742. '});',
  29743. 'this.$rtti.$Class("TFox");',
  29744. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29745. '});',
  29746. 'this.$rtti.$ClassRef("TBirds", {',
  29747. ' instancetype: this.$rtti["TBird"]',
  29748. '});',
  29749. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29750. '});',
  29751. 'this.$rtti.$ClassRef("TFoxes", {',
  29752. ' instancetype: this.$rtti["TFox"]',
  29753. '});',
  29754. 'this.$rtti.$Class("TCow");',
  29755. 'this.$rtti.$ClassRef("TCows", {',
  29756. ' instancetype: this.$rtti["TCow"]',
  29757. '});',
  29758. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29759. '});',
  29760. '']),
  29761. LinesToStr([ // $mod.$main
  29762. '']));
  29763. end;
  29764. procedure TTestModule.TestRTTI_Record;
  29765. begin
  29766. WithTypeInfo:=true;
  29767. StartProgram(false);
  29768. Add('type');
  29769. Add(' integer = longint;');
  29770. Add(' TPoint = record');
  29771. Add(' x,y: integer;');
  29772. Add(' end;');
  29773. Add('var p: pointer;');
  29774. Add(' r: tpoint;');
  29775. Add('begin');
  29776. Add(' p:=typeinfo(tpoint);');
  29777. Add(' p:=typeinfo(r);');
  29778. Add(' p:=typeinfo(r.x);');
  29779. ConvertProgram;
  29780. CheckSource('TestRTTI_Record',
  29781. LinesToStr([ // statements
  29782. 'rtl.recNewT(this, "TPoint", function () {',
  29783. ' this.x = 0;',
  29784. ' this.y = 0;',
  29785. ' this.$eq = function (b) {',
  29786. ' return (this.x === b.x) && (this.y === b.y);',
  29787. ' };',
  29788. ' this.$assign = function (s) {',
  29789. ' this.x = s.x;',
  29790. ' this.y = s.y;',
  29791. ' return this;',
  29792. ' };',
  29793. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29794. ' $r.addField("x", rtl.longint);',
  29795. ' $r.addField("y", rtl.longint);',
  29796. '});',
  29797. 'this.p = null;',
  29798. 'this.r = this.TPoint.$new();',
  29799. '']),
  29800. LinesToStr([ // $mod.$main
  29801. '$mod.p = $mod.$rtti["TPoint"];',
  29802. '$mod.p = $mod.$rtti["TPoint"];',
  29803. '$mod.p = rtl.longint;',
  29804. '']));
  29805. end;
  29806. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29807. begin
  29808. WithTypeInfo:=true;
  29809. StartProgram(false);
  29810. Add('type');
  29811. Add(' TFloatRec = record');
  29812. Add(' c,d: array of char;');
  29813. // Add(' i: array of array of longint;');
  29814. Add(' end;');
  29815. Add('var p: pointer;');
  29816. Add(' r: tfloatrec;');
  29817. Add('begin');
  29818. Add(' p:=typeinfo(tfloatrec);');
  29819. Add(' p:=typeinfo(r);');
  29820. Add(' p:=typeinfo(r.d);');
  29821. ConvertProgram;
  29822. CheckSource('TestRTTI_Record',
  29823. LinesToStr([ // statements
  29824. 'rtl.recNewT(this, "TFloatRec", function () {',
  29825. ' this.$new = function () {',
  29826. ' var r = Object.create(this);',
  29827. ' r.c = [];',
  29828. ' r.d = [];',
  29829. ' return r;',
  29830. ' };',
  29831. ' this.$eq = function (b) {',
  29832. ' return (this.c === b.c) && (this.d === b.d);',
  29833. ' };',
  29834. ' this.$assign = function (s) {',
  29835. ' this.c = rtl.arrayRef(s.c);',
  29836. ' this.d = rtl.arrayRef(s.d);',
  29837. ' return this;',
  29838. ' };',
  29839. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29840. ' eltype: rtl.char',
  29841. ' });',
  29842. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29843. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29844. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29845. '});',
  29846. 'this.p = null;',
  29847. 'this.r = this.TFloatRec.$new();',
  29848. '']),
  29849. LinesToStr([ // $mod.$main
  29850. '$mod.p = $mod.$rtti["TFloatRec"];',
  29851. '$mod.p = $mod.$rtti["TFloatRec"];',
  29852. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29853. '']));
  29854. end;
  29855. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29856. begin
  29857. WithTypeInfo:=true;
  29858. StartProgram(false);
  29859. Add([
  29860. '{$modeswitch AdvancedRecords}',
  29861. 'type',
  29862. ' TPoint = record',
  29863. ' type TProc = procedure(w: word);',
  29864. ' class var p: TProc;',
  29865. ' end;',
  29866. 'begin',
  29867. '']);
  29868. ConvertProgram;
  29869. CheckSource('TestRTTI_Record_ClassVarType',
  29870. LinesToStr([ // statements
  29871. 'rtl.recNewT(this, "TPoint", function () {',
  29872. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  29873. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  29874. ' });',
  29875. ' this.p = null;',
  29876. ' this.$eq = function (b) {',
  29877. ' return true;',
  29878. ' };',
  29879. ' this.$assign = function (s) {',
  29880. ' return this;',
  29881. ' };',
  29882. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29883. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  29884. '}, true);',
  29885. '']),
  29886. LinesToStr([ // $mod.$main
  29887. '']));
  29888. end;
  29889. procedure TTestModule.TestRTTI_LocalTypes;
  29890. begin
  29891. WithTypeInfo:=true;
  29892. StartProgram(false);
  29893. Add([
  29894. 'procedure DoIt;',
  29895. 'type',
  29896. ' integer = longint;',
  29897. ' TPoint = record',
  29898. ' x,y: integer;',
  29899. ' end;',
  29900. 'var p: TPoint;',
  29901. 'begin',
  29902. 'end;',
  29903. 'begin']);
  29904. ConvertProgram;
  29905. CheckSource('TestRTTI_LocalTypes',
  29906. LinesToStr([ // statements
  29907. 'var TPoint = rtl.recNewT(null, "", function () {',
  29908. ' this.x = 0;',
  29909. ' this.y = 0;',
  29910. ' this.$eq = function (b) {',
  29911. ' return (this.x === b.x) && (this.y === b.y);',
  29912. ' };',
  29913. ' this.$assign = function (s) {',
  29914. ' this.x = s.x;',
  29915. ' this.y = s.y;',
  29916. ' return this;',
  29917. ' };',
  29918. '});',
  29919. 'this.DoIt = function () {',
  29920. ' var p = TPoint.$new();',
  29921. '};',
  29922. '']),
  29923. LinesToStr([ // $mod.$main
  29924. '']));
  29925. end;
  29926. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29927. begin
  29928. WithTypeInfo:=true;
  29929. StartProgram(false);
  29930. Add([
  29931. 'type',
  29932. ' TCaption = string;',
  29933. ' TYesNo = boolean;',
  29934. ' TLetter = char;',
  29935. ' TFloat = double;',
  29936. ' TPtr = pointer;',
  29937. ' TShortInt = shortint;',
  29938. ' TByte = byte;',
  29939. ' TSmallInt = smallint;',
  29940. ' TWord = word;',
  29941. ' TInt32 = longint;',
  29942. ' TDWord = longword;',
  29943. ' TValue = jsvalue;',
  29944. 'var p: TPtr;',
  29945. 'begin',
  29946. ' p:=typeinfo(string);',
  29947. ' p:=typeinfo(tcaption);',
  29948. ' p:=typeinfo(boolean);',
  29949. ' p:=typeinfo(tyesno);',
  29950. ' p:=typeinfo(char);',
  29951. ' p:=typeinfo(tletter);',
  29952. ' p:=typeinfo(double);',
  29953. ' p:=typeinfo(tfloat);',
  29954. ' p:=typeinfo(pointer);',
  29955. ' p:=typeinfo(tptr);',
  29956. ' p:=typeinfo(shortint);',
  29957. ' p:=typeinfo(tshortint);',
  29958. ' p:=typeinfo(byte);',
  29959. ' p:=typeinfo(tbyte);',
  29960. ' p:=typeinfo(smallint);',
  29961. ' p:=typeinfo(tsmallint);',
  29962. ' p:=typeinfo(word);',
  29963. ' p:=typeinfo(tword);',
  29964. ' p:=typeinfo(longword);',
  29965. ' p:=typeinfo(tdword);',
  29966. ' p:=typeinfo(jsvalue);',
  29967. ' p:=typeinfo(tvalue);',
  29968. '']);
  29969. ConvertProgram;
  29970. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29971. LinesToStr([ // statements
  29972. 'this.p = null;',
  29973. '']),
  29974. LinesToStr([ // $mod.$main
  29975. '$mod.p = rtl.string;',
  29976. '$mod.p = rtl.string;',
  29977. '$mod.p = rtl.boolean;',
  29978. '$mod.p = rtl.boolean;',
  29979. '$mod.p = rtl.char;',
  29980. '$mod.p = rtl.char;',
  29981. '$mod.p = rtl.double;',
  29982. '$mod.p = rtl.double;',
  29983. '$mod.p = rtl.pointer;',
  29984. '$mod.p = rtl.pointer;',
  29985. '$mod.p = rtl.shortint;',
  29986. '$mod.p = rtl.shortint;',
  29987. '$mod.p = rtl.byte;',
  29988. '$mod.p = rtl.byte;',
  29989. '$mod.p = rtl.smallint;',
  29990. '$mod.p = rtl.smallint;',
  29991. '$mod.p = rtl.word;',
  29992. '$mod.p = rtl.word;',
  29993. '$mod.p = rtl.longword;',
  29994. '$mod.p = rtl.longword;',
  29995. '$mod.p = rtl.jsvalue;',
  29996. '$mod.p = rtl.jsvalue;',
  29997. '']));
  29998. end;
  29999. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30000. begin
  30001. WithTypeInfo:=true;
  30002. StartProgram(false);
  30003. Add([
  30004. 'type',
  30005. ' TCaption = type string;',
  30006. ' TYesNo = type boolean;',
  30007. ' TLetter = type char;',
  30008. ' TFloat = type double;',
  30009. ' TPtr = type pointer;',
  30010. ' TShortInt = type shortint;',
  30011. ' TByte = type byte;',
  30012. ' TSmallInt = type smallint;',
  30013. ' TWord = type word;',
  30014. ' TInt32 = type longint;',
  30015. ' TDWord = type longword;',
  30016. ' TValue = type jsvalue;',
  30017. ' TAliasValue = type TValue;',
  30018. 'var',
  30019. ' p: TPtr;',
  30020. ' a: TAliasValue;',
  30021. 'begin',
  30022. ' p:=typeinfo(tcaption);',
  30023. ' p:=typeinfo(tyesno);',
  30024. ' p:=typeinfo(tletter);',
  30025. ' p:=typeinfo(tfloat);',
  30026. ' p:=typeinfo(tptr);',
  30027. ' p:=typeinfo(tshortint);',
  30028. ' p:=typeinfo(tbyte);',
  30029. ' p:=typeinfo(tsmallint);',
  30030. ' p:=typeinfo(tword);',
  30031. ' p:=typeinfo(tdword);',
  30032. ' p:=typeinfo(tvalue);',
  30033. ' p:=typeinfo(taliasvalue);',
  30034. ' p:=typeinfo(a);',
  30035. '']);
  30036. ConvertProgram;
  30037. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30038. LinesToStr([ // statements
  30039. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30040. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30041. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30042. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30043. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30044. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30045. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30046. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30047. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30048. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30049. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30050. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30051. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30052. 'this.p = null;',
  30053. 'this.a = undefined;',
  30054. '']),
  30055. LinesToStr([ // $mod.$main
  30056. '$mod.p = $mod.$rtti["TCaption"];',
  30057. '$mod.p = $mod.$rtti["TYesNo"];',
  30058. '$mod.p = $mod.$rtti["TLetter"];',
  30059. '$mod.p = $mod.$rtti["TFloat"];',
  30060. '$mod.p = $mod.$rtti["TPtr"];',
  30061. '$mod.p = $mod.$rtti["TShortInt"];',
  30062. '$mod.p = $mod.$rtti["TByte"];',
  30063. '$mod.p = $mod.$rtti["TSmallInt"];',
  30064. '$mod.p = $mod.$rtti["TWord"];',
  30065. '$mod.p = $mod.$rtti["TDWord"];',
  30066. '$mod.p = $mod.$rtti["TValue"];',
  30067. '$mod.p = $mod.$rtti["TAliasValue"];',
  30068. '$mod.p = $mod.$rtti["TAliasValue"];',
  30069. '']));
  30070. end;
  30071. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30072. begin
  30073. WithTypeInfo:=true;
  30074. StartProgram(false);
  30075. Add('procedure DoIt;');
  30076. Add('type');
  30077. Add(' integer = longint;');
  30078. Add(' TPoint = record');
  30079. Add(' x,y: integer;');
  30080. Add(' end;');
  30081. Add('var p: pointer;');
  30082. Add('begin');
  30083. Add(' p:=typeinfo(tpoint);');
  30084. Add('end;');
  30085. Add('begin');
  30086. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30087. ConvertProgram;
  30088. end;
  30089. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30090. begin
  30091. WithTypeInfo:=true;
  30092. StartProgram(true,[supTypeInfo]);
  30093. Add([
  30094. '{$modeswitch externalclass}',
  30095. 'type',
  30096. ' TFlag = (up,down);',
  30097. ' TFlags = set of TFlag;',
  30098. 'var',
  30099. ' ti: TTypeInfo;',
  30100. ' tiInt: TTypeInfoInteger;',
  30101. ' tiEnum: TTypeInfoEnum;',
  30102. ' tiSet: TTypeInfoSet;',
  30103. 'begin',
  30104. ' ti:=typeinfo(string);',
  30105. ' ti:=typeinfo(boolean);',
  30106. ' ti:=typeinfo(char);',
  30107. ' ti:=typeinfo(double);',
  30108. ' tiInt:=typeinfo(shortint);',
  30109. ' tiInt:=typeinfo(byte);',
  30110. ' tiInt:=typeinfo(smallint);',
  30111. ' tiInt:=typeinfo(word);',
  30112. ' tiInt:=typeinfo(longint);',
  30113. ' tiInt:=typeinfo(longword);',
  30114. ' ti:=typeinfo(jsvalue);',
  30115. ' tiEnum:=typeinfo(tflag);',
  30116. ' tiSet:=typeinfo(tflags);']);
  30117. ConvertProgram;
  30118. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30119. LinesToStr([ // statements
  30120. 'this.TFlag = {',
  30121. ' "0": "up",',
  30122. ' up: 0,',
  30123. ' "1": "down",',
  30124. ' down: 1',
  30125. '};',
  30126. 'this.$rtti.$Enum("TFlag", {',
  30127. ' minvalue: 0,',
  30128. ' maxvalue: 1,',
  30129. ' ordtype: 1,',
  30130. ' enumtype: this.TFlag',
  30131. '});',
  30132. 'this.$rtti.$Set("TFlags", {',
  30133. ' comptype: this.$rtti["TFlag"]',
  30134. '});',
  30135. 'this.ti = null;',
  30136. 'this.tiInt = null;',
  30137. 'this.tiEnum = null;',
  30138. 'this.tiSet = null;',
  30139. '']),
  30140. LinesToStr([ // $mod.$main
  30141. '$mod.ti = rtl.string;',
  30142. '$mod.ti = rtl.boolean;',
  30143. '$mod.ti = rtl.char;',
  30144. '$mod.ti = rtl.double;',
  30145. '$mod.tiInt = rtl.shortint;',
  30146. '$mod.tiInt = rtl.byte;',
  30147. '$mod.tiInt = rtl.smallint;',
  30148. '$mod.tiInt = rtl.word;',
  30149. '$mod.tiInt = rtl.longint;',
  30150. '$mod.tiInt = rtl.longword;',
  30151. '$mod.ti = rtl.jsvalue;',
  30152. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30153. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30154. '']));
  30155. end;
  30156. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30157. begin
  30158. WithTypeInfo:=true;
  30159. StartProgram(true,[supTypeInfo]);
  30160. Add('{$modeswitch externalclass}');
  30161. Add('type');
  30162. Add(' TStaticArr = array[boolean] of string;');
  30163. Add(' TDynArr = array of string;');
  30164. Add(' TProc = procedure;');
  30165. Add(' TMethod = procedure of object;');
  30166. Add('var');
  30167. Add(' StaticArray: TStaticArr;');
  30168. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30169. Add(' DynArray: TDynArr;');
  30170. Add(' tiDynArray: TTypeInfoDynArray;');
  30171. Add(' ProcVar: TProc;');
  30172. Add(' tiProcVar: TTypeInfoProcVar;');
  30173. Add(' MethodVar: TMethod;');
  30174. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30175. Add('begin');
  30176. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30177. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30178. Add(' tiDynArray:=typeinfo(DynArray);');
  30179. Add(' tiDynArray:=typeinfo(TDynArr);');
  30180. Add(' tiProcVar:=typeinfo(ProcVar);');
  30181. Add(' tiProcVar:=typeinfo(TProc);');
  30182. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30183. Add(' tiMethodVar:=typeinfo(TMethod);');
  30184. ConvertProgram;
  30185. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30186. LinesToStr([ // statements
  30187. 'this.$rtti.$StaticArray("TStaticArr", {',
  30188. ' dims: [2],',
  30189. ' eltype: rtl.string',
  30190. '});',
  30191. 'this.$rtti.$DynArray("TDynArr", {',
  30192. ' eltype: rtl.string',
  30193. '});',
  30194. 'this.$rtti.$ProcVar("TProc", {',
  30195. ' procsig: rtl.newTIProcSig(null)',
  30196. '});',
  30197. 'this.$rtti.$MethodVar("TMethod", {',
  30198. ' procsig: rtl.newTIProcSig(null),',
  30199. ' methodkind: 0',
  30200. '});',
  30201. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30202. 'this.tiStaticArray = null;',
  30203. 'this.DynArray = [];',
  30204. 'this.tiDynArray = null;',
  30205. 'this.ProcVar = null;',
  30206. 'this.tiProcVar = null;',
  30207. 'this.MethodVar = null;',
  30208. 'this.tiMethodVar = null;',
  30209. '']),
  30210. LinesToStr([ // $mod.$main
  30211. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30212. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30213. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30214. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30215. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30216. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30217. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30218. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30219. '']));
  30220. end;
  30221. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30222. begin
  30223. WithTypeInfo:=true;
  30224. StartProgram(true,[supTypeInfo]);
  30225. Add('{$modeswitch externalclass}');
  30226. Add('type');
  30227. Add(' TRec = record end;');
  30228. // ToDo: ^TRec
  30229. Add(' TObject = class end;');
  30230. Add(' TClass = class of tobject;');
  30231. Add('var');
  30232. Add(' Rec: trec;');
  30233. Add(' tiRecord: ttypeinforecord;');
  30234. Add(' Obj: tobject;');
  30235. Add(' tiClass: ttypeinfoclass;');
  30236. Add(' aClass: tclass;');
  30237. Add(' tiClassRef: ttypeinfoclassref;');
  30238. // ToDo: ^TRec
  30239. Add(' tiPointer: ttypeinfopointer;');
  30240. Add('begin');
  30241. Add(' tirecord:=typeinfo(trec);');
  30242. Add(' tirecord:=typeinfo(trec);');
  30243. Add(' ticlass:=typeinfo(obj);');
  30244. Add(' ticlass:=typeinfo(tobject);');
  30245. Add(' ticlass:=typeinfo(aclass);');
  30246. Add(' ticlassref:=typeinfo(tclass);');
  30247. ConvertProgram;
  30248. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30249. LinesToStr([ // statements
  30250. 'rtl.recNewT(this, "TRec", function () {',
  30251. ' this.$eq = function (b) {',
  30252. ' return true;',
  30253. ' };',
  30254. ' this.$assign = function (s) {',
  30255. ' return this;',
  30256. ' };',
  30257. ' $mod.$rtti.$Record("TRec", {});',
  30258. '});',
  30259. 'rtl.createClass(this, "TObject", null, function () {',
  30260. ' this.$init = function () {',
  30261. ' };',
  30262. ' this.$final = function () {',
  30263. ' };',
  30264. '});',
  30265. 'this.$rtti.$ClassRef("TClass", {',
  30266. ' instancetype: this.$rtti["TObject"]',
  30267. '});',
  30268. 'this.Rec = this.TRec.$new();',
  30269. 'this.tiRecord = null;',
  30270. 'this.Obj = null;',
  30271. 'this.tiClass = null;',
  30272. 'this.aClass = null;',
  30273. 'this.tiClassRef = null;',
  30274. 'this.tiPointer = null;',
  30275. '']),
  30276. LinesToStr([ // $mod.$main
  30277. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30278. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30279. '$mod.tiClass = $mod.Obj.$rtti;',
  30280. '$mod.tiClass = $mod.$rtti["TObject"];',
  30281. '$mod.tiClass = $mod.aClass.$rtti;',
  30282. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30283. '']));
  30284. end;
  30285. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30286. begin
  30287. WithTypeInfo:=true;
  30288. StartProgram(true,[supTypeInfo]);
  30289. Add([
  30290. '{$modeswitch externalclass}',
  30291. 'type',
  30292. ' TClass = class of tobject;',
  30293. ' TObject = class',
  30294. ' function MyClass: TClass;',
  30295. ' class function ClassType: TClass;',
  30296. ' end;',
  30297. 'function TObject.MyClass: TClass;',
  30298. 'var t: TTypeInfoClass;',
  30299. 'begin',
  30300. ' t:=TypeInfo(Self);',
  30301. ' t:=TypeInfo(Result);',
  30302. ' t:=TypeInfo(TObject);',
  30303. 'end;',
  30304. 'class function TObject.ClassType: TClass;',
  30305. 'var t: TTypeInfoClass;',
  30306. 'begin',
  30307. ' t:=TypeInfo(Self);',
  30308. ' t:=TypeInfo(Result);',
  30309. 'end;',
  30310. 'var',
  30311. ' Obj: TObject;',
  30312. ' t: TTypeInfoClass;',
  30313. 'begin',
  30314. ' t:=TypeInfo(TObject.ClassType);',
  30315. ' t:=TypeInfo(Obj.ClassType);',
  30316. ' t:=TypeInfo(Obj.MyClass);',
  30317. '']);
  30318. ConvertProgram;
  30319. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30320. LinesToStr([ // statements
  30321. 'this.$rtti.$Class("TObject");',
  30322. 'this.$rtti.$ClassRef("TClass", {',
  30323. ' instancetype: this.$rtti["TObject"]',
  30324. '});',
  30325. 'rtl.createClass(this, "TObject", null, function () {',
  30326. ' this.$init = function () {',
  30327. ' };',
  30328. ' this.$final = function () {',
  30329. ' };',
  30330. ' this.MyClass = function () {',
  30331. ' var Result = null;',
  30332. ' var t = null;',
  30333. ' t = this.$rtti;',
  30334. ' t = Result.$rtti;',
  30335. ' t = $mod.$rtti["TObject"];',
  30336. ' return Result;',
  30337. ' };',
  30338. ' this.ClassType = function () {',
  30339. ' var Result = null;',
  30340. ' var t = null;',
  30341. ' t = this.$rtti;',
  30342. ' t = Result.$rtti;',
  30343. ' return Result;',
  30344. ' };',
  30345. '});',
  30346. 'this.Obj = null;',
  30347. 'this.t = null;',
  30348. '']),
  30349. LinesToStr([ // $mod.$main
  30350. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30351. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30352. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30353. '']));
  30354. end;
  30355. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30356. begin
  30357. WithTypeInfo:=true;
  30358. AddModuleWithIntfImplSrc('typinfo.pas',
  30359. LinesToStr([
  30360. '{$modeswitch externalclass}',
  30361. 'type',
  30362. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30363. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30364. '']),
  30365. '');
  30366. AddModuleWithIntfImplSrc('unit2.pas',
  30367. LinesToStr([
  30368. 'uses typinfo;',
  30369. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30370. 'procedure DoPtr(p: PTypeInfo);',
  30371. 'procedure DoInfo(t: TTypeInfo);',
  30372. 'procedure DoInt(t: TTypeInfoInteger);',
  30373. '']),
  30374. LinesToStr([
  30375. 'procedure DoPtr(p: PTypeInfo);',
  30376. 'begin end;',
  30377. 'procedure DoInfo(t: TTypeInfo);',
  30378. 'begin end;',
  30379. 'procedure DoInt(t: TTypeInfoInteger);',
  30380. 'begin end;',
  30381. '']));
  30382. StartUnit(true);
  30383. Add([
  30384. 'interface',
  30385. 'uses unit2;', // does not use unit typinfo
  30386. 'implementation',
  30387. 'var',
  30388. ' i: byte;',
  30389. ' p: pointer;',
  30390. ' t: PTypeInfo;',
  30391. 'initialization',
  30392. ' p:=typeinfo(i);',
  30393. ' t:=typeinfo(i);',
  30394. ' if p=t then ;',
  30395. ' if p=typeinfo(i) then ;',
  30396. ' if typeinfo(i)=p then ;',
  30397. ' if t=typeinfo(i) then ;',
  30398. ' if typeinfo(i)=t then ;',
  30399. ' DoPtr(p);',
  30400. ' DoPtr(t);',
  30401. ' DoPtr(typeinfo(i));',
  30402. ' DoInfo(p);',
  30403. ' DoInfo(t);',
  30404. ' DoInfo(typeinfo(i));',
  30405. ' DoInt(typeinfo(i));',
  30406. '']);
  30407. ConvertUnit;
  30408. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30409. LinesToStr([ // statements
  30410. 'var $impl = $mod.$impl;',
  30411. '']),
  30412. LinesToStr([ // this.$init
  30413. '$impl.p = rtl.byte;',
  30414. '$impl.t = rtl.byte;',
  30415. 'if ($impl.p === $impl.t) ;',
  30416. 'if ($impl.p === rtl.byte) ;',
  30417. 'if (rtl.byte === $impl.p) ;',
  30418. 'if ($impl.t === rtl.byte) ;',
  30419. 'if (rtl.byte === $impl.t) ;',
  30420. 'pas.unit2.DoPtr($impl.p);',
  30421. 'pas.unit2.DoPtr($impl.t);',
  30422. 'pas.unit2.DoPtr(rtl.byte);',
  30423. 'pas.unit2.DoInfo($impl.p);',
  30424. 'pas.unit2.DoInfo($impl.t);',
  30425. 'pas.unit2.DoInfo(rtl.byte);',
  30426. 'pas.unit2.DoInt(rtl.byte);',
  30427. '']),
  30428. LinesToStr([ // implementation
  30429. '$impl.i = 0;',
  30430. '$impl.p = null;',
  30431. '$impl.t = null;',
  30432. '']) );
  30433. end;
  30434. procedure TTestModule.TestRTTI_Interface_Corba;
  30435. begin
  30436. WithTypeInfo:=true;
  30437. StartProgram(true,[supTypeInfo]);
  30438. Add([
  30439. '{$interfaces corba}',
  30440. '{$modeswitch externalclass}',
  30441. 'type',
  30442. ' IUnknown = interface',
  30443. ' end;',
  30444. ' IBird = interface',
  30445. ' function GetItem: longint;',
  30446. ' procedure SetItem(Value: longint);',
  30447. ' property Item: longint read GetItem write SetItem;',
  30448. ' end;',
  30449. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30450. 'var',
  30451. ' i: IBird;',
  30452. ' t: TTypeInfoInterface;',
  30453. 'begin',
  30454. ' t:=TypeInfo(IBird);',
  30455. ' t:=TypeInfo(i);',
  30456. ' DoIt(t);',
  30457. ' DoIt(TypeInfo(IBird));',
  30458. '']);
  30459. ConvertProgram;
  30460. CheckSource('TestRTTI_Interface_Corba',
  30461. LinesToStr([ // statements
  30462. 'rtl.createInterface(',
  30463. ' this,',
  30464. ' "IUnknown",',
  30465. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30466. ' [],',
  30467. ' null,',
  30468. ' function () {',
  30469. ' }',
  30470. ');',
  30471. 'rtl.createInterface(',
  30472. ' this,',
  30473. ' "IBird",',
  30474. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30475. ' ["GetItem", "SetItem"],',
  30476. ' null,',
  30477. ' function () {',
  30478. ' var $r = this.$rtti;',
  30479. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30480. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30481. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30482. ' }',
  30483. ');',
  30484. 'this.DoIt = function (t) {',
  30485. '}; ',
  30486. 'this.i = null;',
  30487. 'this.t = null;',
  30488. '']),
  30489. LinesToStr([ // $mod.$main
  30490. '$mod.t = $mod.$rtti["IBird"];',
  30491. '$mod.t = $mod.i.$rtti;',
  30492. '$mod.DoIt($mod.t);',
  30493. '$mod.DoIt($mod.$rtti["IBird"]);',
  30494. '']));
  30495. end;
  30496. procedure TTestModule.TestRTTI_Interface_COM;
  30497. begin
  30498. WithTypeInfo:=true;
  30499. StartProgram(true,[supTypeInfo]);
  30500. Add([
  30501. '{$interfaces com}',
  30502. '{$modeswitch externalclass}',
  30503. 'type',
  30504. ' TGuid = record end;',
  30505. ' integer = longint;',
  30506. ' IUnknown = interface',
  30507. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30508. ' function _AddRef: Integer;',
  30509. ' function _Release: Integer;',
  30510. ' end;',
  30511. ' IBird = interface',
  30512. ' function GetItem: longint;',
  30513. ' procedure SetItem(Value: longint);',
  30514. ' property Item: longint read GetItem write SetItem;',
  30515. ' end;',
  30516. 'var',
  30517. ' i: IBird;',
  30518. ' t: TTypeInfoInterface;',
  30519. 'begin',
  30520. ' t:=TypeInfo(IBird);',
  30521. ' t:=TypeInfo(i);',
  30522. '']);
  30523. ConvertProgram;
  30524. CheckSource('TestRTTI_Interface_COM',
  30525. LinesToStr([ // statements
  30526. 'rtl.recNewT(this, "TGuid", function () {',
  30527. ' this.$eq = function (b) {',
  30528. ' return true;',
  30529. ' };',
  30530. ' this.$assign = function (s) {',
  30531. ' return this;',
  30532. ' };',
  30533. ' $mod.$rtti.$Record("TGuid", {});',
  30534. '});',
  30535. 'rtl.createInterface(',
  30536. ' this,',
  30537. ' "IUnknown",',
  30538. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30539. ' ["QueryInterface", "_AddRef", "_Release"],',
  30540. ' null,',
  30541. ' function () {',
  30542. ' this.$kind = "com";',
  30543. ' var $r = this.$rtti;',
  30544. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30545. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30546. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30547. ' }',
  30548. ');',
  30549. 'rtl.createInterface(',
  30550. ' this,',
  30551. ' "IBird",',
  30552. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30553. ' ["GetItem", "SetItem"],',
  30554. ' this.IUnknown,',
  30555. ' function () {',
  30556. ' var $r = this.$rtti;',
  30557. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30558. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30559. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30560. ' }',
  30561. ');',
  30562. 'this.i = null;',
  30563. 'this.t = null;',
  30564. '']),
  30565. LinesToStr([ // $mod.$main
  30566. '$mod.t = $mod.$rtti["IBird"];',
  30567. '$mod.t = $mod.i.$rtti;',
  30568. '']));
  30569. end;
  30570. procedure TTestModule.TestRTTI_ClassHelper;
  30571. begin
  30572. WithTypeInfo:=true;
  30573. StartProgram(true,[supTypeInfo]);
  30574. Add([
  30575. '{$interfaces com}',
  30576. '{$modeswitch externalclass}',
  30577. 'type',
  30578. ' TObject = class',
  30579. ' end;',
  30580. ' THelper = class helper for TObject',
  30581. ' published',
  30582. ' function GetItem: longint;',
  30583. ' property Item: longint read GetItem;',
  30584. ' end;',
  30585. 'function THelper.GetItem: longint;',
  30586. 'begin',
  30587. 'end;',
  30588. 'var',
  30589. ' t: TTypeInfoHelper;',
  30590. 'begin',
  30591. ' t:=TypeInfo(THelper);',
  30592. '']);
  30593. ConvertProgram;
  30594. CheckSource('TestRTTI_ClassHelper',
  30595. LinesToStr([ // statements
  30596. 'rtl.createClass(this, "TObject", null, function () {',
  30597. ' this.$init = function () {',
  30598. ' };',
  30599. ' this.$final = function () {',
  30600. ' };',
  30601. '});',
  30602. 'rtl.createHelper(this, "THelper", null, function () {',
  30603. ' this.GetItem = function () {',
  30604. ' var Result = 0;',
  30605. ' return Result;',
  30606. ' };',
  30607. ' var $r = this.$rtti;',
  30608. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30609. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30610. '});',
  30611. 'this.t = null;',
  30612. '']),
  30613. LinesToStr([ // $mod.$main
  30614. '$mod.t = $mod.$rtti["THelper"];',
  30615. '']));
  30616. end;
  30617. procedure TTestModule.TestRTTI_ExternalClass;
  30618. begin
  30619. WithTypeInfo:=true;
  30620. StartProgram(true,[supTypeInfo]);
  30621. Add([
  30622. '{$modeswitch externalclass}',
  30623. 'type',
  30624. ' TJSObject = class external name ''Object''',
  30625. ' end;',
  30626. ' TJSArray = class external name ''Array'' (TJSObject)',
  30627. ' end;',
  30628. 'var',
  30629. ' p: Pointer;',
  30630. ' tc: TTypeInfoExtClass;',
  30631. 'begin',
  30632. ' p:=typeinfo(TJSArray);']);
  30633. ConvertProgram;
  30634. CheckSource('TestRTTI_ExternalClass',
  30635. LinesToStr([ // statements
  30636. 'this.$rtti.$ExtClass("TJSObject", {',
  30637. ' jsclass: "Object"',
  30638. '});',
  30639. 'this.$rtti.$ExtClass("TJSArray", {',
  30640. ' ancestor: this.$rtti["TJSObject"],',
  30641. ' jsclass: "Array"',
  30642. '});',
  30643. 'this.p = null;',
  30644. 'this.tc = null;',
  30645. '']),
  30646. LinesToStr([ // $mod.$main
  30647. '$mod.p = $mod.$rtti["TJSArray"];',
  30648. '']));
  30649. end;
  30650. procedure TTestModule.TestRTTI_Unit;
  30651. begin
  30652. WithTypeInfo:=true;
  30653. AddModuleWithIntfImplSrc('unit2.pas',
  30654. LinesToStr([
  30655. '{$mode delphi}',
  30656. 'type',
  30657. ' TWordArray = array of word;',
  30658. ' TArray<T> = array of T;',
  30659. '']),
  30660. '');
  30661. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30662. Add([
  30663. '{$mode delphi}',
  30664. 'interface',
  30665. 'uses unit2;',
  30666. 'type',
  30667. ' IBird = interface',
  30668. ' function Swoop: TWordArray;',
  30669. ' function Glide: TArray<word>;',
  30670. ' end;',
  30671. 'procedure Fly;',
  30672. 'implementation',
  30673. 'procedure Fly;',
  30674. 'var',
  30675. ' ta: tTypeInfoDynArray;',
  30676. ' ti: tTypeInfoInterface;',
  30677. 'begin',
  30678. ' ta:=typeinfo(TWordArray);',
  30679. ' ta:=typeinfo(TArray<word>);',
  30680. ' ti:=typeinfo(IBird);',
  30681. 'end;',
  30682. '']);
  30683. ConvertUnit;
  30684. CheckSource('TestRTTI_ExternalClass',
  30685. LinesToStr([ // statements
  30686. 'rtl.createInterface(',
  30687. ' this,',
  30688. ' "IBird",',
  30689. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30690. ' ["Swoop", "Glide"],',
  30691. ' pas.system.IUnknown,',
  30692. ' function () {',
  30693. ' var $r = this.$rtti;',
  30694. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30695. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30696. ' }',
  30697. ');',
  30698. 'this.Fly = function () {',
  30699. ' var ta = null;',
  30700. ' var ti = null;',
  30701. ' ta = pas.unit2.$rtti["TWordArray"];',
  30702. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30703. ' ti = $mod.$rtti["IBird"];',
  30704. '};',
  30705. '']),
  30706. LinesToStr([ // $mod.$main
  30707. '']));
  30708. end;
  30709. procedure TTestModule.TestResourcestringProgram;
  30710. begin
  30711. AddModuleWithIntfImplSrc('unit2.pas',
  30712. LinesToStr([
  30713. 'resourcestring Title = ''Nice'';',
  30714. '']),
  30715. '');
  30716. StartProgram(true);
  30717. Add([
  30718. 'uses unit2;',
  30719. 'const Bar = ''bar'';',
  30720. 'resourcestring',
  30721. ' Red = ''red'';',
  30722. ' Foobar = ''fOo''+bar;',
  30723. 'var s: string;',
  30724. ' c: char;',
  30725. 'begin',
  30726. ' s:=red;',
  30727. ' s:=test1.red;',
  30728. ' s:=Title;',
  30729. ' c:=red[1];',
  30730. ' c:=test1.red[2];',
  30731. ' if red=foobar then ;',
  30732. ' if red[3]=red[4] then ;']);
  30733. ConvertProgram;
  30734. CheckSource('TestResourcestringProgram',
  30735. LinesToStr([ // statements
  30736. 'this.Bar = "bar";',
  30737. 'this.s = "";',
  30738. 'this.c = "";',
  30739. '$mod.$resourcestrings = {',
  30740. ' Red: {',
  30741. ' org: "red"',
  30742. ' },',
  30743. ' Foobar: {',
  30744. ' org: "fOobar"',
  30745. ' }',
  30746. '};',
  30747. '']),
  30748. LinesToStr([ // $mod.$main
  30749. '$mod.s = rtl.getResStr($mod, "Red");',
  30750. '$mod.s = rtl.getResStr($mod, "Red");',
  30751. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30752. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30753. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30754. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30755. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30756. '']));
  30757. end;
  30758. procedure TTestModule.TestResourcestringUnit;
  30759. begin
  30760. AddModuleWithIntfImplSrc('unit2.pas',
  30761. LinesToStr([
  30762. 'resourcestring Title = ''Nice'';',
  30763. '']),
  30764. '');
  30765. StartUnit(true);
  30766. Add([
  30767. 'interface',
  30768. 'uses unit2;',
  30769. 'const Red = ''rEd'';',
  30770. 'resourcestring',
  30771. ' Blue = ''blue'';',
  30772. ' NotRed = ''not''+Red;',
  30773. 'var s: string;',
  30774. 'implementation',
  30775. 'resourcestring',
  30776. ' ImplGreen = ''green'';',
  30777. 'initialization',
  30778. ' s:=blue+ImplGreen;',
  30779. ' s:=test1.blue+test1.implgreen;',
  30780. ' s:=blue[1]+implgreen[2];',
  30781. ' s:=Title;',
  30782. '']);
  30783. ConvertUnit;
  30784. CheckSource('TestResourcestringUnit',
  30785. LinesToStr([ // statements
  30786. 'this.Red = "rEd";',
  30787. 'this.s = "";',
  30788. '$mod.$resourcestrings = {',
  30789. ' Blue: {',
  30790. ' org: "blue"',
  30791. ' },',
  30792. ' NotRed: {',
  30793. ' org: "notrEd"',
  30794. ' },',
  30795. ' ImplGreen: {',
  30796. ' org: "green"',
  30797. ' }',
  30798. '};',
  30799. '']),
  30800. LinesToStr([ // $mod.$main
  30801. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30802. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30803. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30804. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30805. '']));
  30806. end;
  30807. procedure TTestModule.TestResourcestringImplementation;
  30808. begin
  30809. StartUnit(false);
  30810. Add([
  30811. 'interface',
  30812. 'implementation',
  30813. 'resourcestring',
  30814. ' ImplRed = ''red'';']);
  30815. ConvertUnit;
  30816. CheckSource('TestResourcestringImplementation',
  30817. LinesToStr([ // intf statements
  30818. 'var $impl = $mod.$impl;']),
  30819. LinesToStr([ // $mod.$init
  30820. '']),
  30821. LinesToStr([ // impl statements
  30822. '$mod.$resourcestrings = {',
  30823. ' ImplRed: {',
  30824. ' org: "red"',
  30825. ' }',
  30826. '};',
  30827. '']));
  30828. end;
  30829. procedure TTestModule.TestAttributes_Members;
  30830. begin
  30831. WithTypeInfo:=true;
  30832. StartProgram(false);
  30833. Add([
  30834. '{$modeswitch PrefixedAttributes}',
  30835. 'type',
  30836. ' TObject = class',
  30837. ' constructor Create;',
  30838. ' end;',
  30839. ' TCustomAttribute = class',
  30840. ' constructor Create(Id: word);',
  30841. ' end;',
  30842. ' [Missing]',
  30843. ' TBird = class',
  30844. ' published',
  30845. ' [Tcustom]',
  30846. ' FField: word;',
  30847. ' [tcustom(14)]',
  30848. ' property Size: word read FField;',
  30849. ' [Tcustom(15)]',
  30850. ' procedure Fly; virtual; abstract;',
  30851. ' end;',
  30852. ' TRec = record',
  30853. ' [Tcustom,tcustom(14)]',
  30854. ' Size: word;',
  30855. ' end;',
  30856. 'constructor TObject.Create; begin end;',
  30857. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30858. 'begin',
  30859. '']);
  30860. ConvertProgram;
  30861. CheckSource('TestAttributes_Members',
  30862. LinesToStr([ // statements
  30863. 'rtl.createClass(this, "TObject", null, function () {',
  30864. ' this.$init = function () {',
  30865. ' };',
  30866. ' this.$final = function () {',
  30867. ' };',
  30868. ' this.Create = function () {',
  30869. ' return this;',
  30870. ' };',
  30871. '});',
  30872. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30873. ' this.Create$1 = function (Id) {',
  30874. ' return this;',
  30875. ' };',
  30876. '});',
  30877. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30878. ' this.$init = function () {',
  30879. ' $mod.TObject.$init.call(this);',
  30880. ' this.FField = 0;',
  30881. ' };',
  30882. ' var $r = this.$rtti;',
  30883. ' $r.addField("FField", rtl.word, {',
  30884. ' attr: [$mod.TCustomAttribute, "Create"]',
  30885. ' });',
  30886. ' $r.addProperty(',
  30887. ' "Size",',
  30888. ' 0,',
  30889. ' rtl.word,',
  30890. ' "FField",',
  30891. ' "",',
  30892. ' {',
  30893. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30894. ' }',
  30895. ' );',
  30896. ' $r.addMethod("Fly", 0, null, null, {',
  30897. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30898. ' });',
  30899. '});',
  30900. 'rtl.recNewT(this, "TRec", function () {',
  30901. ' this.Size = 0;',
  30902. ' this.$eq = function (b) {',
  30903. ' return this.Size === b.Size;',
  30904. ' };',
  30905. ' this.$assign = function (s) {',
  30906. ' this.Size = s.Size;',
  30907. ' return this;',
  30908. ' };',
  30909. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30910. ' $r.addField("Size", rtl.word, {',
  30911. ' attr: [',
  30912. ' $mod.TCustomAttribute,',
  30913. ' "Create",',
  30914. ' $mod.TCustomAttribute,',
  30915. ' "Create$1",',
  30916. ' [14]',
  30917. ' ]',
  30918. ' });',
  30919. '});',
  30920. '']),
  30921. LinesToStr([ // $mod.$main
  30922. '']));
  30923. end;
  30924. procedure TTestModule.TestAttributes_Types;
  30925. begin
  30926. WithTypeInfo:=true;
  30927. StartProgram(false);
  30928. Add([
  30929. '{$modeswitch PrefixedAttributes}',
  30930. 'type',
  30931. ' TObject = class',
  30932. ' constructor Create(Id: word);',
  30933. ' end;',
  30934. ' TCustomAttribute = class',
  30935. ' end;',
  30936. ' [TCustom(1)]',
  30937. ' TMyClass = class',
  30938. ' end;',
  30939. ' [TCustom(2)]',
  30940. ' TRec = record',
  30941. ' end;',
  30942. ' [TCustom(3)]',
  30943. ' TInt = type word;',
  30944. 'constructor TObject.Create(Id: word);',
  30945. 'begin',
  30946. 'end;',
  30947. 'var p: pointer;',
  30948. 'begin',
  30949. ' p:=typeinfo(TMyClass);',
  30950. ' p:=typeinfo(TRec);',
  30951. ' p:=typeinfo(TInt);',
  30952. '']);
  30953. ConvertProgram;
  30954. CheckSource('TestAttributes_Types',
  30955. LinesToStr([ // statements
  30956. 'rtl.createClass(this, "TObject", null, function () {',
  30957. ' this.$init = function () {',
  30958. ' };',
  30959. ' this.$final = function () {',
  30960. ' };',
  30961. ' this.Create = function (Id) {',
  30962. ' return this;',
  30963. ' };',
  30964. '});',
  30965. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30966. '});',
  30967. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  30968. ' var $r = this.$rtti;',
  30969. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30970. '});',
  30971. 'rtl.recNewT(this, "TRec", function () {',
  30972. ' this.$eq = function (b) {',
  30973. ' return true;',
  30974. ' };',
  30975. ' this.$assign = function (s) {',
  30976. ' return this;',
  30977. ' };',
  30978. ' $mod.$rtti.$Record("TRec", {',
  30979. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30980. ' });',
  30981. '});',
  30982. 'this.$rtti.$inherited("TInt", rtl.word, {',
  30983. ' attr: [this.TCustomAttribute, "Create", [3]]',
  30984. '});',
  30985. 'this.p = null;',
  30986. '']),
  30987. LinesToStr([ // $mod.$main
  30988. '$mod.p = $mod.$rtti["TMyClass"];',
  30989. '$mod.p = $mod.$rtti["TRec"];',
  30990. '$mod.p = $mod.$rtti["TInt"];',
  30991. '']));
  30992. end;
  30993. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30994. begin
  30995. WithTypeInfo:=true;
  30996. StartProgram(false);
  30997. Add([
  30998. '{$modeswitch PrefixedAttributes}',
  30999. 'type',
  31000. ' TObject = class',
  31001. ' constructor Create;',
  31002. ' end;',
  31003. ' TCustomAttribute = class',
  31004. ' end;',
  31005. ' THelper = class helper for TCustomAttribute',
  31006. ' constructor Create(Id: word);',
  31007. ' end;',
  31008. ' [TCustom(3)]',
  31009. ' TMyInt = word;',
  31010. 'constructor TObject.Create; begin end;',
  31011. 'constructor THelper.Create(Id: word); begin end;',
  31012. 'begin',
  31013. ' if typeinfo(TMyInt)=nil then ;']);
  31014. ConvertProgram;
  31015. end;
  31016. procedure TTestModule.TestAssert;
  31017. begin
  31018. StartProgram(false);
  31019. Add([
  31020. 'procedure DoIt;',
  31021. 'var',
  31022. ' b: boolean;',
  31023. ' s: string;',
  31024. 'begin',
  31025. ' {$Assertions on}',
  31026. ' Assert(b);',
  31027. 'end;',
  31028. 'begin',
  31029. ' DoIt;',
  31030. '']);
  31031. ConvertProgram;
  31032. CheckSource('TestAssert',
  31033. LinesToStr([ // statements
  31034. 'this.DoIt = function () {',
  31035. ' var b = false;',
  31036. ' var s = "";',
  31037. ' if (!b) throw "assert failed";',
  31038. '};',
  31039. '']),
  31040. LinesToStr([ // $mod.$main
  31041. '$mod.DoIt();',
  31042. '']));
  31043. end;
  31044. procedure TTestModule.TestAssert_SysUtils;
  31045. begin
  31046. AddModuleWithIntfImplSrc('SysUtils.pas',
  31047. LinesToStr([
  31048. 'type',
  31049. ' TObject = class',
  31050. ' constructor Create;',
  31051. ' end;',
  31052. ' EAssertionFailed = class',
  31053. ' constructor Create(s: string);',
  31054. ' end;',
  31055. '']),
  31056. LinesToStr([
  31057. 'constructor TObject.Create;',
  31058. 'begin end;',
  31059. 'constructor EAssertionFailed.Create(s: string);',
  31060. 'begin end;',
  31061. '']) );
  31062. StartProgram(true);
  31063. Add([
  31064. 'uses sysutils;',
  31065. 'procedure DoIt;',
  31066. 'var',
  31067. ' b: boolean;',
  31068. ' s: string;',
  31069. 'begin',
  31070. ' {$Assertions on}',
  31071. ' Assert(b);',
  31072. ' Assert(b,''msg'');',
  31073. 'end;',
  31074. 'begin',
  31075. ' DoIt;',
  31076. '']);
  31077. ConvertProgram;
  31078. CheckSource('TestAssert_SysUtils',
  31079. LinesToStr([ // statements
  31080. 'this.DoIt = function () {',
  31081. ' var b = false;',
  31082. ' var s = "";',
  31083. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31084. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31085. '};',
  31086. '']),
  31087. LinesToStr([ // $mod.$main
  31088. '$mod.DoIt();',
  31089. '']));
  31090. end;
  31091. procedure TTestModule.TestObjectChecks;
  31092. begin
  31093. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31094. StartProgram(false);
  31095. Add([
  31096. 'type',
  31097. ' TObject = class',
  31098. ' procedure DoIt;',
  31099. ' end;',
  31100. ' TClass = class of tobject;',
  31101. ' TBird = class',
  31102. ' end;',
  31103. ' TBirdClass = class of TBird;',
  31104. 'var',
  31105. ' o : TObject;',
  31106. ' c: TClass;',
  31107. ' b: TBird;',
  31108. ' bc: TBirdClass;',
  31109. 'procedure TObject.DoIt;',
  31110. 'begin',
  31111. ' b:=TBird(o);',
  31112. 'end;',
  31113. 'begin',
  31114. ' o.DoIt;',
  31115. ' b:=TBird(o);',
  31116. ' bc:=TBirdClass(c);',
  31117. '']);
  31118. ConvertProgram;
  31119. CheckSource('TestCheckMethodCall',
  31120. LinesToStr([ // statements
  31121. 'rtl.createClass(this, "TObject", null, function () {',
  31122. ' this.$init = function () {',
  31123. ' };',
  31124. ' this.$final = function () {',
  31125. ' };',
  31126. ' this.DoIt = function () {',
  31127. ' rtl.checkMethodCall(this,$mod.TObject);',
  31128. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31129. ' };',
  31130. '});',
  31131. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31132. '});',
  31133. 'this.o = null;',
  31134. 'this.c = null;',
  31135. 'this.b = null;',
  31136. 'this.bc = null;',
  31137. '']),
  31138. LinesToStr([ // $mod.$main
  31139. '$mod.o.DoIt();',
  31140. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31141. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31142. '']));
  31143. end;
  31144. procedure TTestModule.TestOverflowChecks_Int;
  31145. begin
  31146. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31147. StartProgram(false);
  31148. Add([
  31149. 'procedure DoIt;',
  31150. 'var',
  31151. ' b: byte;',
  31152. ' n: nativeint;',
  31153. ' u: nativeuint;',
  31154. ' c: currency;',
  31155. 'begin',
  31156. ' n:=n+n;',
  31157. ' n:=n-n;',
  31158. ' n:=n+b;',
  31159. ' n:=b-n;',
  31160. ' n:=n*n;',
  31161. ' n:=n*u;',
  31162. ' c:=c+b;',
  31163. ' c:=b+c;',
  31164. ' c:=c*b;',
  31165. ' c:=b*c;',
  31166. 'end;',
  31167. 'begin',
  31168. '']);
  31169. ConvertProgram;
  31170. CheckSource('TestOverflowChecks_Int',
  31171. LinesToStr([ // statements
  31172. 'this.DoIt = function () {',
  31173. ' var b = 0;',
  31174. ' var n = 0;',
  31175. ' var u = 0;',
  31176. ' var c = 0;',
  31177. ' n = rtl.oc(n + n);',
  31178. ' n = rtl.oc(n - n);',
  31179. ' n = rtl.oc(n + b);',
  31180. ' n = rtl.oc(b - n);',
  31181. ' n = rtl.oc(n * n);',
  31182. ' n = rtl.oc(n * u);',
  31183. ' c = rtl.oc(c + (b * 10000));',
  31184. ' c = rtl.oc((b * 10000) + c);',
  31185. ' c = rtl.oc(c * b);',
  31186. ' c = rtl.oc(b * c);',
  31187. '};',
  31188. '']),
  31189. LinesToStr([ // $mod.$main
  31190. '']));
  31191. end;
  31192. procedure TTestModule.TestRangeChecks_AssignInt;
  31193. begin
  31194. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31195. StartProgram(false);
  31196. Add([
  31197. '{$R+}',
  31198. 'var',
  31199. ' b: byte = 2;',
  31200. ' w: word = 3;',
  31201. 'procedure DoIt(p: byte);',
  31202. 'begin',
  31203. ' b:=w;',
  31204. ' b+=w;',
  31205. ' b:=1;',
  31206. 'end;',
  31207. '{$R-}',
  31208. 'procedure DoSome;',
  31209. 'begin',
  31210. ' DoIt(w);',
  31211. ' b:=w;',
  31212. ' b:=2;',
  31213. 'end;',
  31214. 'begin',
  31215. '{$R+}',
  31216. '']);
  31217. ConvertProgram;
  31218. CheckSource('TestRangeChecks_AssignInt',
  31219. LinesToStr([ // statements
  31220. 'this.b = 2;',
  31221. 'this.w = 3;',
  31222. 'this.DoIt = function (p) {',
  31223. ' rtl.rc(p, 0, 255);',
  31224. ' $mod.b = rtl.rc($mod.w,0,255);',
  31225. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31226. ' $mod.b = 1;',
  31227. '};',
  31228. 'this.DoSome = function () {',
  31229. ' $mod.DoIt($mod.w);',
  31230. ' $mod.b = $mod.w;',
  31231. ' $mod.b = 2;',
  31232. '};',
  31233. '']),
  31234. LinesToStr([ // $mod.$main
  31235. '']));
  31236. end;
  31237. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31238. begin
  31239. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31240. StartProgram(false);
  31241. Add([
  31242. '{$R+}',
  31243. 'type Ten = 1..10;',
  31244. 'var',
  31245. ' b: Ten = 2;',
  31246. ' w: Ten = 3;',
  31247. 'procedure DoIt(p: Ten);',
  31248. 'begin',
  31249. ' b:=w;',
  31250. ' b+=w;',
  31251. ' b:=1;',
  31252. 'end;',
  31253. '{$R-}',
  31254. 'procedure DoSome;',
  31255. 'begin',
  31256. ' DoIt(w);',
  31257. ' b:=w;',
  31258. ' b:=2;',
  31259. 'end;',
  31260. 'begin',
  31261. '{$R+}',
  31262. '']);
  31263. ConvertProgram;
  31264. CheckSource('TestRangeChecks_AssignIntRange',
  31265. LinesToStr([ // statements
  31266. 'this.b = 2;',
  31267. 'this.w = 3;',
  31268. 'this.DoIt = function (p) {',
  31269. ' rtl.rc(p, 1, 10);',
  31270. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31271. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31272. ' $mod.b = 1;',
  31273. '};',
  31274. 'this.DoSome = function () {',
  31275. ' $mod.DoIt($mod.w);',
  31276. ' $mod.b = $mod.w;',
  31277. ' $mod.b = 2;',
  31278. '};',
  31279. '']),
  31280. LinesToStr([ // $mod.$main
  31281. '']));
  31282. end;
  31283. procedure TTestModule.TestRangeChecks_AssignEnum;
  31284. begin
  31285. StartProgram(false);
  31286. Add([
  31287. '{$R+}',
  31288. 'type TEnum = (red,green);',
  31289. 'var',
  31290. ' e: TEnum = red;',
  31291. 'procedure DoIt(p: TEnum);',
  31292. 'begin',
  31293. ' e:=p;',
  31294. ' p:=TEnum(0);',
  31295. ' p:=succ(e);',
  31296. 'end;',
  31297. '{$R-}',
  31298. 'procedure DoSome;',
  31299. 'begin',
  31300. ' DoIt(e);',
  31301. ' e:=TEnum(1);',
  31302. ' e:=pred(e);',
  31303. 'end;',
  31304. 'begin',
  31305. '{$R+}',
  31306. '']);
  31307. ConvertProgram;
  31308. CheckSource('TestRangeChecks_AssignEnum',
  31309. LinesToStr([ // statements
  31310. 'this.TEnum = {',
  31311. ' "0": "red",',
  31312. ' red: 0,',
  31313. ' "1": "green",',
  31314. ' green: 1',
  31315. '};',
  31316. 'this.e = this.TEnum.red;',
  31317. 'this.DoIt = function (p) {',
  31318. ' rtl.rc(p, 0, 1);',
  31319. ' $mod.e = rtl.rc(p, 0, 1);',
  31320. ' p = 0;',
  31321. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31322. '};',
  31323. 'this.DoSome = function () {',
  31324. ' $mod.DoIt($mod.e);',
  31325. ' $mod.e = 1;',
  31326. ' $mod.e = $mod.e - 1;',
  31327. '};',
  31328. '']),
  31329. LinesToStr([ // $mod.$main
  31330. '']));
  31331. end;
  31332. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31333. begin
  31334. StartProgram(false);
  31335. Add([
  31336. '{$R+}',
  31337. 'type',
  31338. ' TEnum = (red,green);',
  31339. ' TEnumRg = red..green;',
  31340. 'var',
  31341. ' e: TEnumRg = red;',
  31342. 'procedure DoIt(p: TEnumRg);',
  31343. 'begin',
  31344. ' e:=p;',
  31345. ' p:=TEnumRg(0);',
  31346. ' p:=succ(e);',
  31347. 'end;',
  31348. '{$R-}',
  31349. 'procedure DoSome;',
  31350. 'begin',
  31351. ' DoIt(e);',
  31352. ' e:=TEnum(1);',
  31353. ' e:=pred(e);',
  31354. 'end;',
  31355. 'begin',
  31356. '{$R+}',
  31357. '']);
  31358. ConvertProgram;
  31359. CheckSource('TestRangeChecks_AssignEnumRange',
  31360. LinesToStr([ // statements
  31361. 'this.TEnum = {',
  31362. ' "0": "red",',
  31363. ' red: 0,',
  31364. ' "1": "green",',
  31365. ' green: 1',
  31366. '};',
  31367. 'this.e = this.TEnum.red;',
  31368. 'this.DoIt = function (p) {',
  31369. ' rtl.rc(p, 0, 1);',
  31370. ' $mod.e = rtl.rc(p, 0, 1);',
  31371. ' p = 0;',
  31372. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31373. '};',
  31374. 'this.DoSome = function () {',
  31375. ' $mod.DoIt($mod.e);',
  31376. ' $mod.e = 1;',
  31377. ' $mod.e = $mod.e - 1;',
  31378. '};',
  31379. '']),
  31380. LinesToStr([ // $mod.$main
  31381. '']));
  31382. end;
  31383. procedure TTestModule.TestRangeChecks_AssignChar;
  31384. begin
  31385. StartProgram(false);
  31386. Add([
  31387. '{$R+}',
  31388. 'type',
  31389. ' TLetter = char;',
  31390. 'var',
  31391. ' b: TLetter = ''2'';',
  31392. ' w: TLetter = ''3'';',
  31393. 'procedure DoIt(p: TLetter);',
  31394. 'begin',
  31395. ' b:=w;',
  31396. ' b:=''1'';',
  31397. 'end;',
  31398. '{$R-}',
  31399. 'procedure DoSome;',
  31400. 'begin',
  31401. ' DoIt(w);',
  31402. ' b:=w;',
  31403. ' b:=''2'';',
  31404. 'end;',
  31405. 'begin',
  31406. '{$R+}',
  31407. '']);
  31408. ConvertProgram;
  31409. CheckSource('TestRangeChecks_AssignChar',
  31410. LinesToStr([ // statements
  31411. 'this.b = "2";',
  31412. 'this.w = "3";',
  31413. 'this.DoIt = function (p) {',
  31414. ' rtl.rcc(p, 0, 65535);',
  31415. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31416. ' $mod.b = "1";',
  31417. '};',
  31418. 'this.DoSome = function () {',
  31419. ' $mod.DoIt($mod.w);',
  31420. ' $mod.b = $mod.w;',
  31421. ' $mod.b = "2";',
  31422. '};',
  31423. '']),
  31424. LinesToStr([ // $mod.$main
  31425. '']));
  31426. end;
  31427. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31428. begin
  31429. StartProgram(false);
  31430. Add([
  31431. '{$R+}',
  31432. 'type TDigit = ''0''..''9'';',
  31433. 'var',
  31434. ' b: TDigit = ''2'';',
  31435. ' w: TDigit = ''3'';',
  31436. 'procedure DoIt(p: TDigit);',
  31437. 'begin',
  31438. ' b:=w;',
  31439. ' b:=''1'';',
  31440. 'end;',
  31441. '{$R-}',
  31442. 'procedure DoSome;',
  31443. 'begin',
  31444. ' DoIt(w);',
  31445. ' b:=w;',
  31446. ' b:=''2'';',
  31447. 'end;',
  31448. 'begin',
  31449. '{$R+}',
  31450. '']);
  31451. ConvertProgram;
  31452. CheckSource('TestRangeChecks_AssignCharRange',
  31453. LinesToStr([ // statements
  31454. 'this.b = "2";',
  31455. 'this.w = "3";',
  31456. 'this.DoIt = function (p) {',
  31457. ' rtl.rcc(p, 48, 57);',
  31458. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31459. ' $mod.b = "1";',
  31460. '};',
  31461. 'this.DoSome = function () {',
  31462. ' $mod.DoIt($mod.w);',
  31463. ' $mod.b = $mod.w;',
  31464. ' $mod.b = "2";',
  31465. '};',
  31466. '']),
  31467. LinesToStr([ // $mod.$main
  31468. '']));
  31469. end;
  31470. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31471. begin
  31472. StartProgram(false);
  31473. Add([
  31474. '{$R+}',
  31475. 'type',
  31476. ' Ten = 1..10;',
  31477. ' TArr = array of Ten;',
  31478. ' TArrArr = array of TArr;',
  31479. ' TArrByte = array[byte] of Ten;',
  31480. ' TArrChar = array[''0''..''9''] of Ten;',
  31481. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31482. ' TObject = class',
  31483. ' A: TArr;',
  31484. ' end;',
  31485. 'procedure DoIt;',
  31486. 'var',
  31487. ' Arr: TArr;',
  31488. ' ArrArr: TArrArr;',
  31489. ' ArrByte: TArrByte;',
  31490. ' ArrChar: TArrChar;',
  31491. ' ArrByteChar: TArrByteChar;',
  31492. ' i: Ten;',
  31493. ' c: char;',
  31494. ' o: tobject;',
  31495. 'begin',
  31496. ' i:=Arr[1];',
  31497. ' i:=ArrByteChar[1,''2''];',
  31498. ' Arr[1]:=Arr[1];',
  31499. ' Arr[i]:=Arr[i];',
  31500. ' ArrByte[3]:=ArrByte[3];',
  31501. ' ArrByte[i]:=ArrByte[i];',
  31502. ' ArrChar[''5'']:=ArrChar[''5''];',
  31503. ' ArrChar[c]:=ArrChar[c];',
  31504. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31505. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31506. ' o.a[i]:=o.a[i];',
  31507. 'end;',
  31508. 'begin',
  31509. '']);
  31510. ConvertProgram;
  31511. CheckSource('TestRangeChecks_ArrayIndex',
  31512. LinesToStr([ // statements
  31513. 'rtl.createClass(this, "TObject", null, function () {',
  31514. ' this.$init = function () {',
  31515. ' this.A = [];',
  31516. ' };',
  31517. ' this.$final = function () {',
  31518. ' this.A = undefined;',
  31519. ' };',
  31520. '});',
  31521. 'this.DoIt = function () {',
  31522. ' var Arr = [];',
  31523. ' var ArrArr = [];',
  31524. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31525. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31526. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31527. ' var i = 0;',
  31528. ' var c = "";',
  31529. ' var o = null;',
  31530. ' i = rtl.rc(Arr[1], 1, 10);',
  31531. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31532. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31533. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31534. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31535. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31536. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31537. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31538. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31539. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31540. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31541. '};',
  31542. '']),
  31543. LinesToStr([ // $mod.$main
  31544. '']));
  31545. end;
  31546. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31547. begin
  31548. StartProgram(false);
  31549. Add([
  31550. '{$R+}',
  31551. 'type',
  31552. ' Ten = 1..10;',
  31553. ' TRec = record x: Ten end;',
  31554. ' TArr = array of TRec;',
  31555. ' TArrArr = array of TArr;',
  31556. ' TObject = class',
  31557. ' A: TArr;',
  31558. ' end;',
  31559. 'procedure DoIt;',
  31560. 'var',
  31561. ' Arr: TArr;',
  31562. ' ArrArr: TArrArr;',
  31563. ' i: Ten;',
  31564. ' o: tobject;',
  31565. 'begin',
  31566. ' Arr[1]:=Arr[1];',
  31567. ' Arr[i]:=Arr[i+1];',
  31568. ' o.a[i]:=o.a[i+2];',
  31569. 'end;',
  31570. 'begin',
  31571. '']);
  31572. ConvertProgram;
  31573. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31574. LinesToStr([ // statements
  31575. 'rtl.recNewT(this, "TRec", function () {',
  31576. ' this.x = 0;',
  31577. ' this.$eq = function (b) {',
  31578. ' return this.x === b.x;',
  31579. ' };',
  31580. ' this.$assign = function (s) {',
  31581. ' this.x = s.x;',
  31582. ' return this;',
  31583. ' };',
  31584. '});',
  31585. 'rtl.createClass(this, "TObject", null, function () {',
  31586. ' this.$init = function () {',
  31587. ' this.A = [];',
  31588. ' };',
  31589. ' this.$final = function () {',
  31590. ' this.A = undefined;',
  31591. ' };',
  31592. '});',
  31593. 'this.DoIt = function () {',
  31594. ' var Arr = [];',
  31595. ' var ArrArr = [];',
  31596. ' var i = 0;',
  31597. ' var o = null;',
  31598. ' Arr[1].$assign(Arr[1]);',
  31599. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31600. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31601. '};',
  31602. '']),
  31603. LinesToStr([ // $mod.$main
  31604. '']));
  31605. end;
  31606. procedure TTestModule.TestRangeChecks_StringIndex;
  31607. begin
  31608. StartProgram(false);
  31609. Add([
  31610. 'type',
  31611. ' TObject = class',
  31612. ' S: string;',
  31613. ' end;',
  31614. '{$R+}',
  31615. 'procedure DoIt(var h: string);',
  31616. 'var',
  31617. ' s: string;',
  31618. ' i: longint;',
  31619. ' c: char;',
  31620. ' o: tobject;',
  31621. 'begin',
  31622. ' c:=s[1];',
  31623. ' s[i]:=s[i];',
  31624. ' h[i]:=h[i];',
  31625. ' c:=o.s[i];',
  31626. ' o.s[i]:=c;',
  31627. 'end;',
  31628. 'begin',
  31629. '']);
  31630. ConvertProgram;
  31631. CheckSource('TestRangeChecks_StringIndex',
  31632. LinesToStr([ // statements
  31633. 'rtl.createClass(this, "TObject", null, function () {',
  31634. ' this.$init = function () {',
  31635. ' this.S = "";',
  31636. ' };',
  31637. ' this.$final = function () {',
  31638. ' };',
  31639. '});',
  31640. 'this.DoIt = function (h) {',
  31641. ' var s = "";',
  31642. ' var i = 0;',
  31643. ' var c = "";',
  31644. ' var o = null;',
  31645. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31646. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31647. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31648. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31649. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31650. '};',
  31651. '']),
  31652. LinesToStr([ // $mod.$main
  31653. '']));
  31654. end;
  31655. procedure TTestModule.TestRangeChecks_TypecastInt;
  31656. begin
  31657. StartProgram(false);
  31658. Add([
  31659. '{$R+}',
  31660. 'var',
  31661. ' i: nativeint;',
  31662. ' b: byte;',
  31663. ' sh: shortint;',
  31664. ' w: word;',
  31665. ' sm: smallint;',
  31666. ' lw: longword;',
  31667. ' li: longint;',
  31668. 'begin',
  31669. ' b:=12+byte(i);',
  31670. ' sh:=12+shortint(i);',
  31671. ' w:=12+word(i);',
  31672. ' sm:=12+smallint(i);',
  31673. ' lw:=12+longword(i);',
  31674. ' li:=12+longint(i);',
  31675. '']);
  31676. ConvertProgram;
  31677. CheckSource('TestRangeChecks_TypecastInt',
  31678. LinesToStr([
  31679. 'this.i = 0;',
  31680. 'this.b = 0;',
  31681. 'this.sh = 0;',
  31682. 'this.w = 0;',
  31683. 'this.sm = 0;',
  31684. 'this.lw = 0;',
  31685. 'this.li = 0;',
  31686. '']),
  31687. LinesToStr([
  31688. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31689. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31690. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31691. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31692. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31693. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31694. '']));
  31695. end;
  31696. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31697. begin
  31698. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31699. StartProgram(false);
  31700. Add([
  31701. '{$modeswitch typehelpers}',
  31702. '{$R+}',
  31703. 'type',
  31704. ' TObject = class',
  31705. ' FSize: byte;',
  31706. ' property Size: byte read FSize;',
  31707. ' end;',
  31708. ' THelper = type helper for byte',
  31709. ' procedure SetIt(w: word);',
  31710. ' end;',
  31711. 'procedure THelper.SetIt(w: word);',
  31712. 'begin',
  31713. ' Self:=w;',
  31714. 'end;',
  31715. 'function GetIt: byte;',
  31716. 'begin',
  31717. ' Result.SetIt(2);',
  31718. 'end;',
  31719. 'var',
  31720. ' b: byte = 3;',
  31721. ' o: TObject;',
  31722. 'begin',
  31723. ' b.SetIt(14);',
  31724. ' with b do SetIt(15);',
  31725. ' o.Size.SetIt(16);',
  31726. '']);
  31727. ConvertProgram;
  31728. CheckSource('TestRangeChecks_AssignInt',
  31729. LinesToStr([ // statements
  31730. 'rtl.createClass(this, "TObject", null, function () {',
  31731. ' this.$init = function () {',
  31732. ' this.FSize = 0;',
  31733. ' };',
  31734. ' this.$final = function () {',
  31735. ' };',
  31736. '});',
  31737. 'rtl.createHelper(this, "THelper", null, function () {',
  31738. ' this.SetIt = function (w) {',
  31739. ' rtl.rc(w, 0, 65535);',
  31740. ' this.set(w);',
  31741. ' };',
  31742. '});',
  31743. 'this.GetIt = function () {',
  31744. ' var Result = 0;',
  31745. ' $mod.THelper.SetIt.call({',
  31746. ' get: function () {',
  31747. ' return Result;',
  31748. ' },',
  31749. ' set: function (v) {',
  31750. ' rtl.rc(v, 0, 255);',
  31751. ' Result = v;',
  31752. ' }',
  31753. ' }, 2);',
  31754. ' return Result;',
  31755. '};',
  31756. 'this.b = 3;',
  31757. 'this.o = null;',
  31758. '']),
  31759. LinesToStr([ // $mod.$main
  31760. '$mod.THelper.SetIt.call({',
  31761. ' p: $mod,',
  31762. ' get: function () {',
  31763. ' return this.p.b;',
  31764. ' },',
  31765. ' set: function (v) {',
  31766. ' rtl.rc(v, 0, 255);',
  31767. ' this.p.b = v;',
  31768. ' }',
  31769. '}, 14);',
  31770. 'var $with = $mod.b;',
  31771. '$mod.THelper.SetIt.call({',
  31772. ' get: function () {',
  31773. ' return $with;',
  31774. ' },',
  31775. ' set: function (v) {',
  31776. ' rtl.rc(v, 0, 255);',
  31777. ' $with = v;',
  31778. ' }',
  31779. '}, 15);',
  31780. '$mod.THelper.SetIt.call({',
  31781. ' p: $mod.o,',
  31782. ' get: function () {',
  31783. ' return this.p.FSize;',
  31784. ' },',
  31785. ' set: function (v) {',
  31786. ' rtl.rc(v, 0, 255);',
  31787. ' this.p.FSize = v;',
  31788. ' }',
  31789. '}, 16);',
  31790. '']));
  31791. end;
  31792. procedure TTestModule.TestAsync_Proc;
  31793. begin
  31794. StartProgram(false);
  31795. Add([
  31796. 'procedure Fly(w: word = 1); async; forward;',
  31797. 'procedure Run(w: word = 2); async;',
  31798. 'begin',
  31799. ' Fly(w);',
  31800. ' Fly;',
  31801. ' await(Fly(w));',
  31802. ' await(Fly);',
  31803. 'end;',
  31804. 'procedure Fly(w: word); ',
  31805. 'begin',
  31806. 'end;',
  31807. 'begin',
  31808. ' Run;',
  31809. ' Run(3);',
  31810. '']);
  31811. CheckResolverUnexpectedHints();
  31812. ConvertProgram;
  31813. CheckSource('TestAsync_Proc',
  31814. LinesToStr([ // statements
  31815. 'this.Run = async function (w) {',
  31816. ' $mod.Fly(w);',
  31817. ' $mod.Fly(1);',
  31818. ' await $mod.Fly(w);',
  31819. ' await $mod.Fly(1);',
  31820. '};',
  31821. 'this.Fly = async function (w) {',
  31822. '};',
  31823. '']),
  31824. LinesToStr([
  31825. '$mod.Run(2);',
  31826. '$mod.Run(3);',
  31827. '']));
  31828. end;
  31829. procedure TTestModule.TestAsync_CallResultIsPromise;
  31830. begin
  31831. StartProgram(false);
  31832. Add([
  31833. '{$modeswitch externalclass}',
  31834. 'type',
  31835. ' TObject = class',
  31836. ' end;',
  31837. ' TJSPromise = class external name ''Promise''',
  31838. ' end;',
  31839. ' TBird = class',
  31840. ' function Fly: word; async; ',
  31841. ' end;',
  31842. 'function TBird.Fly: word; async; ',
  31843. 'begin',
  31844. ' Result:=3;',
  31845. ' Fly:=4+Result;',
  31846. ' if Result=5 then ;',
  31847. ' exit(6);',
  31848. 'end;',
  31849. 'function Run: word; async;',
  31850. 'begin',
  31851. ' Result:=11+Result;',
  31852. ' inc(Result);',
  31853. 'end;',
  31854. 'var',
  31855. ' p: TJSPromise;',
  31856. ' o: TBird;',
  31857. 'begin',
  31858. ' p:=Run;',
  31859. ' p:=Run();',
  31860. ' if Run=p then ;',
  31861. ' if p=Run then ;',
  31862. ' if Run()=p then ;',
  31863. ' if p=Run() then ;',
  31864. ' p:=o.Fly;',
  31865. ' p:=o.Fly();',
  31866. ' if o.Fly=p then ;',
  31867. ' if o.Fly()=p then ;',
  31868. ' with o do begin',
  31869. ' p:=Fly;',
  31870. ' p:=Fly();',
  31871. ' if Fly=p then ;',
  31872. ' if Fly()=p then ;',
  31873. ' end;',
  31874. '']);
  31875. CheckResolverUnexpectedHints();
  31876. ConvertProgram;
  31877. CheckSource('TestAsync_CallResultIsPromise',
  31878. LinesToStr([ // statements
  31879. 'rtl.createClass(this, "TObject", null, function () {',
  31880. ' this.$init = function () {',
  31881. ' };',
  31882. ' this.$final = function () {',
  31883. ' };',
  31884. '});',
  31885. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31886. ' this.Fly = async function () {',
  31887. ' var Result = 0;',
  31888. ' Result = 3;',
  31889. ' Result = 4 + Result;',
  31890. ' if (Result === 5) ;',
  31891. ' return 6;',
  31892. ' return Result;',
  31893. ' };',
  31894. '});',
  31895. 'this.Run = async function () {',
  31896. ' var Result = 0;',
  31897. ' Result = 11 + Result;',
  31898. ' Result += 1;',
  31899. ' return Result;',
  31900. '};',
  31901. 'this.p = null;',
  31902. 'this.o = null;',
  31903. '']),
  31904. LinesToStr([
  31905. '$mod.p = $mod.Run();',
  31906. '$mod.p = $mod.Run();',
  31907. 'if ($mod.Run() === $mod.p) ;',
  31908. 'if ($mod.p === $mod.Run()) ;',
  31909. 'if ($mod.Run() === $mod.p) ;',
  31910. 'if ($mod.p === $mod.Run()) ;',
  31911. '$mod.p = $mod.o.Fly();',
  31912. '$mod.p = $mod.o.Fly();',
  31913. 'if ($mod.o.Fly() === $mod.p) ;',
  31914. 'if ($mod.o.Fly() === $mod.p) ;',
  31915. 'var $with = $mod.o;',
  31916. '$mod.p = $with.Fly();',
  31917. '$mod.p = $with.Fly();',
  31918. 'if ($with.Fly() === $mod.p) ;',
  31919. 'if ($with.Fly() === $mod.p) ;',
  31920. '']));
  31921. end;
  31922. procedure TTestModule.TestAsync_ConstructorFail;
  31923. begin
  31924. StartProgram(false);
  31925. Add([
  31926. 'type',
  31927. ' TObject = class',
  31928. ' end;',
  31929. ' TBird = class',
  31930. ' constructor Create; async;',
  31931. ' end;',
  31932. 'constructor TBird.Create; async;',
  31933. 'begin',
  31934. 'end;',
  31935. 'begin',
  31936. '']);
  31937. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31938. ConvertProgram;
  31939. end;
  31940. procedure TTestModule.TestAsync_PropertyGetterFail;
  31941. begin
  31942. StartProgram(false);
  31943. Add([
  31944. 'type',
  31945. ' TObject = class',
  31946. ' end;',
  31947. ' TBird = class',
  31948. ' function GetSize: word; async;',
  31949. ' property Size: word read GetSize;',
  31950. ' end;',
  31951. 'function TBird.GetSize: word; async;',
  31952. 'begin',
  31953. 'end;',
  31954. 'begin',
  31955. '']);
  31956. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31957. ConvertProgram;
  31958. end;
  31959. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31960. begin
  31961. StartProgram(false);
  31962. Add([
  31963. 'procedure Run; async;',
  31964. 'begin',
  31965. ' await(word,1);',
  31966. 'end;',
  31967. 'begin',
  31968. '']);
  31969. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31970. ConvertProgram;
  31971. end;
  31972. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  31973. begin
  31974. StartProgram(false);
  31975. Add([
  31976. 'type',
  31977. ' TObject = class',
  31978. ' end;',
  31979. ' TBird = class',
  31980. ' end;',
  31981. 'function Fly: TObject; async;',
  31982. 'begin',
  31983. 'end;',
  31984. 'procedure Run; async;',
  31985. 'begin',
  31986. ' await(TBird,Fly);',
  31987. 'end;',
  31988. 'begin',
  31989. '']);
  31990. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  31991. ConvertProgram;
  31992. end;
  31993. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31994. begin
  31995. StartProgram(false);
  31996. Add([
  31997. 'procedure Crawl(w: double); ',
  31998. 'begin',
  31999. 'end;',
  32000. 'procedure Run(w: double);',
  32001. 'begin',
  32002. ' await(Crawl(w));',
  32003. 'end;',
  32004. 'begin',
  32005. ' Run(1);']);
  32006. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32007. ConvertProgram;
  32008. end;
  32009. procedure TTestModule.TestAWait_IntegerFail;
  32010. begin
  32011. StartProgram(false);
  32012. Add([
  32013. 'function Run: word;',
  32014. 'begin',
  32015. 'end;',
  32016. 'procedure Fly(w: word); async;',
  32017. 'begin',
  32018. ' await(Run());',
  32019. 'end;',
  32020. 'begin',
  32021. ' Fly(1);']);
  32022. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32023. ConvertProgram;
  32024. end;
  32025. procedure TTestModule.TestAWait_ExternalClassPromise;
  32026. begin
  32027. StartProgram(false);
  32028. Add([
  32029. '{$modeswitch externalclass}',
  32030. 'type',
  32031. ' TJSPromise = class external name ''Promise''',
  32032. ' end;',
  32033. ' TJSThenable = class external name ''Thenable''',
  32034. ' end;',
  32035. 'function Fly(w: word): TJSPromise;',
  32036. 'begin',
  32037. 'end;',
  32038. 'function Jump(w: word): word; async;',
  32039. 'begin',
  32040. 'end;',
  32041. 'function Eat(w: word): TJSPromise; async;',
  32042. 'begin',
  32043. 'end;',
  32044. 'function Run(d: double): word; async;',
  32045. 'var',
  32046. ' p: TJSPromise;',
  32047. 'begin',
  32048. ' Result:=await(word,p);', // promise needs type
  32049. ' Result:=await(word,Fly(3));', // promise needs type
  32050. ' Result:=await(Jump(4));', // async non promise must omit the type
  32051. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32052. ' Result:=await(word,Eat(6));', // promise needs type
  32053. 'end;',
  32054. 'begin',
  32055. '']);
  32056. ConvertProgram;
  32057. CheckSource('TestAWait_ExternalClassPromise',
  32058. LinesToStr([ // statements
  32059. 'this.Fly = function (w) {',
  32060. ' var Result = null;',
  32061. ' return Result;',
  32062. '};',
  32063. 'this.Jump = async function (w) {',
  32064. ' var Result = 0;',
  32065. ' return Result;',
  32066. '};',
  32067. 'this.Eat = async function (w) {',
  32068. ' var Result = null;',
  32069. ' return Result;',
  32070. '};',
  32071. 'this.Run = async function (d) {',
  32072. ' var Result = 0;',
  32073. ' var p = null;',
  32074. ' Result = await p;',
  32075. ' Result = await $mod.Fly(3);',
  32076. ' Result = await $mod.Jump(4);',
  32077. ' Result = await $mod.Jump(5);',
  32078. ' Result = await $mod.Eat(6);',
  32079. ' return Result;',
  32080. '};',
  32081. '']),
  32082. LinesToStr([
  32083. ]));
  32084. CheckResolverUnexpectedHints();
  32085. end;
  32086. procedure TTestModule.TestAWait_JSValue;
  32087. begin
  32088. StartProgram(false);
  32089. Add([
  32090. '{$modeswitch externalclass}',
  32091. 'type',
  32092. ' TJSPromise = class external name ''Promise''',
  32093. ' end;',
  32094. 'function Fly(w: word): jsvalue; async;',
  32095. 'begin',
  32096. 'end;',
  32097. 'function Run(d: jsvalue; var e): word; async;',
  32098. 'begin',
  32099. ' Result:=await(word,d);', // promise needs type
  32100. ' d:=await(Fly(4));', // async non promise must omit the type
  32101. ' Result:=await(word,e);', // promise needs type
  32102. 'end;',
  32103. 'begin',
  32104. '']);
  32105. ConvertProgram;
  32106. CheckSource('TestAWait_JSValue',
  32107. LinesToStr([ // statements
  32108. 'this.Fly = async function (w) {',
  32109. ' var Result = undefined;',
  32110. ' return Result;',
  32111. '};',
  32112. 'this.Run = async function (d, e) {',
  32113. ' var Result = 0;',
  32114. ' Result = await d;',
  32115. ' d = await $mod.Fly(4);',
  32116. ' Result = await e.get();',
  32117. ' return Result;',
  32118. '};',
  32119. '']),
  32120. LinesToStr([
  32121. ]));
  32122. CheckResolverUnexpectedHints();
  32123. end;
  32124. procedure TTestModule.TestAWait_Result;
  32125. begin
  32126. StartProgram(false);
  32127. Add([
  32128. '{$modeswitch externalclass}',
  32129. 'type',
  32130. ' TJSPromise = class external name ''Promise''',
  32131. ' end;',
  32132. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32133. 'begin',
  32134. 'end;',
  32135. 'function Run(d: double = 1.6): word; async;',
  32136. 'begin',
  32137. ' Result:=await(word,Crawl);',
  32138. ' Result:=await(word,Crawl(4.5));',
  32139. ' Result:=await(Run);',
  32140. ' Result:=await(Run(6.7));',
  32141. 'end;',
  32142. 'begin',
  32143. ' Run(1);']);
  32144. ConvertProgram;
  32145. CheckSource('TestAWait_Result',
  32146. LinesToStr([ // statements
  32147. 'this.Crawl = function (d) {',
  32148. ' var Result = null;',
  32149. ' return Result;',
  32150. '};',
  32151. 'this.Run = async function (d) {',
  32152. ' var Result = 0;',
  32153. ' Result = await $mod.Crawl(1.3);',
  32154. ' Result = await $mod.Crawl(4.5);',
  32155. ' Result = await $mod.Run(1.6);',
  32156. ' Result = await $mod.Run(6.7);',
  32157. ' return Result;',
  32158. '};',
  32159. '']),
  32160. LinesToStr([
  32161. '$mod.Run(1);'
  32162. ]));
  32163. CheckResolverUnexpectedHints();
  32164. end;
  32165. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32166. begin
  32167. StartProgram(false);
  32168. Add([
  32169. '{$mode objfpc}',
  32170. '{$modeswitch externalclass}',
  32171. 'type',
  32172. ' TJSPromise = class external name ''Promise''',
  32173. ' end;',
  32174. 'function Run: TJSPromise; async;',
  32175. 'begin',
  32176. 'end;',
  32177. 'procedure Fly(w: word); async;',
  32178. 'begin',
  32179. ' await(Run());',
  32180. 'end;',
  32181. 'begin',
  32182. ' Fly(1);']);
  32183. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32184. nWrongNumberOfParametersForCallTo);
  32185. ConvertProgram;
  32186. end;
  32187. procedure TTestModule.TestAsync_AnonymousProc;
  32188. begin
  32189. StartProgram(false);
  32190. Add([
  32191. '{$mode objfpc}',
  32192. '{$modeswitch externalclass}',
  32193. 'type',
  32194. ' TJSPromise = class external name ''Promise''',
  32195. ' end;',
  32196. 'type',
  32197. ' TFunc = reference to function(x: double): word; async;',
  32198. 'function Crawl(d: double = 1.3): word; async;',
  32199. 'begin',
  32200. 'end;',
  32201. 'var Func: TFunc;',
  32202. 'begin',
  32203. ' Func:=function(c:double):word async begin',
  32204. ' Result:=await(Crawl(c));',
  32205. ' end;',
  32206. ' Func:=function(c:double):word async assembler asm',
  32207. ' end;',
  32208. ' ']);
  32209. ConvertProgram;
  32210. CheckSource('TestAsync_AnonymousProc',
  32211. LinesToStr([ // statements
  32212. 'this.Crawl = async function (d) {',
  32213. ' var Result = 0;',
  32214. ' return Result;',
  32215. '};',
  32216. 'this.Func = null;',
  32217. '']),
  32218. LinesToStr([
  32219. '$mod.Func = async function (c) {',
  32220. ' var Result = 0;',
  32221. ' Result = await $mod.Crawl(c);',
  32222. ' return Result;',
  32223. '};',
  32224. '$mod.Func = async function (c) {',
  32225. '};',
  32226. '']));
  32227. CheckResolverUnexpectedHints();
  32228. end;
  32229. procedure TTestModule.TestAsync_ProcType;
  32230. begin
  32231. StartProgram(false);
  32232. Add([
  32233. '{$mode objfpc}',
  32234. 'type',
  32235. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32236. ' TFunc = function(x: double = 1.1): word; async;',
  32237. ' TProc = procedure(x: longint = 7); async;',
  32238. 'function Crawl(d: double): word; async;',
  32239. 'begin',
  32240. 'end;',
  32241. 'procedure Run(e:longint); async;',
  32242. 'begin',
  32243. 'end;',
  32244. 'procedure Fly(p: TProc); async;',
  32245. 'begin',
  32246. ' await(p);',
  32247. ' await(p());',
  32248. 'end;',
  32249. 'var',
  32250. ' RefFunc: TRefFunc;',
  32251. ' Func: TFunc;',
  32252. ' Proc, ProcB: TProc;',
  32253. 'begin',
  32254. ' Func:=@Crawl;',
  32255. ' RefFunc:=@Crawl;',
  32256. ' RefFunc:=function(c:double):word async begin',
  32257. ' Result:=await(RefFunc);',
  32258. ' Result:=await(RefFunc());',
  32259. ' Result:=await(Func);',
  32260. ' Result:=await(Func());',
  32261. ' await(Proc);',
  32262. ' await(Proc());',
  32263. ' await(Proc(13));',
  32264. ' end;',
  32265. ' Proc:=@Run;',
  32266. ' if Proc=ProcB then ;',
  32267. ' ']);
  32268. ConvertProgram;
  32269. CheckResolverUnexpectedHints();
  32270. CheckSource('TestAsync_ProcType',
  32271. LinesToStr([ // statements
  32272. 'this.Crawl = async function (d) {',
  32273. ' var Result = 0;',
  32274. ' return Result;',
  32275. '};',
  32276. 'this.Run = async function (e) {',
  32277. '};',
  32278. 'this.Fly = async function (p) {',
  32279. ' await p(7);',
  32280. ' await p(7);',
  32281. '};',
  32282. 'this.RefFunc = null;',
  32283. 'this.Func = null;',
  32284. 'this.Proc = null;',
  32285. 'this.ProcB = null;',
  32286. '']),
  32287. LinesToStr([
  32288. '$mod.Func = $mod.Crawl;',
  32289. '$mod.RefFunc = $mod.Crawl;',
  32290. '$mod.RefFunc = async function (c) {',
  32291. ' var Result = 0;',
  32292. ' Result = await $mod.RefFunc(1.3);',
  32293. ' Result = await $mod.RefFunc(1.3);',
  32294. ' Result = await $mod.Func(1.1);',
  32295. ' Result = await $mod.Func(1.1);',
  32296. ' await $mod.Proc(7);',
  32297. ' await $mod.Proc(7);',
  32298. ' await $mod.Proc(13);',
  32299. ' return Result;',
  32300. '};',
  32301. '$mod.Proc = $mod.Run;',
  32302. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32303. '']));
  32304. end;
  32305. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32306. begin
  32307. StartProgram(false);
  32308. Add([
  32309. '{$mode objfpc}',
  32310. 'type',
  32311. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32312. 'function Crawl(d: double): word; async;',
  32313. 'begin',
  32314. 'end;',
  32315. 'var',
  32316. ' RefFunc: TRefFunc;',
  32317. 'begin',
  32318. ' RefFunc:=@Crawl;',
  32319. ' ']);
  32320. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32321. ConvertProgram;
  32322. end;
  32323. procedure TTestModule.TestAsync_Inherited;
  32324. begin
  32325. StartProgram(false);
  32326. Add([
  32327. '{$mode objfpc}',
  32328. '{$modeswitch externalclass}',
  32329. 'type',
  32330. ' TJSPromise = class external name ''Promise''',
  32331. ' end;',
  32332. ' TObject = class',
  32333. ' function Run(w: word = 3): word; async; virtual;',
  32334. ' end;',
  32335. ' TBird = class',
  32336. ' function Run(w: word = 3): word; async; override;',
  32337. ' end;',
  32338. 'function TObject.Run(w: word = 3): word; async;',
  32339. 'begin',
  32340. 'end;',
  32341. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32342. 'var p: TJSPromise;',
  32343. 'begin',
  32344. ' p:=inherited;',
  32345. ' p:=inherited Run;',
  32346. ' p:=inherited Run();',
  32347. ' p:=inherited Run(4);',
  32348. ' exit(p);',
  32349. ' exit(inherited);',
  32350. ' exit(inherited Run);',
  32351. ' exit(inherited Run(5));',
  32352. ' exit(6);',
  32353. 'end;',
  32354. 'begin',
  32355. ' ']);
  32356. ConvertProgram;
  32357. CheckSource('TestAsync_Inherited',
  32358. LinesToStr([ // statements
  32359. 'rtl.createClass(this, "TObject", null, function () {',
  32360. ' this.$init = function () {',
  32361. ' };',
  32362. ' this.$final = function () {',
  32363. ' };',
  32364. ' this.Run = async function (w) {',
  32365. ' var Result = 0;',
  32366. ' return Result;',
  32367. ' };',
  32368. '});',
  32369. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32370. ' this.Run = async function (w) {',
  32371. ' var Result = 0;',
  32372. ' var p = null;',
  32373. ' p = $mod.TObject.Run.apply(this, arguments);',
  32374. ' p = $mod.TObject.Run.call(this, 3);',
  32375. ' p = $mod.TObject.Run.call(this, 3);',
  32376. ' p = $mod.TObject.Run.call(this, 4);',
  32377. ' return p;',
  32378. ' return $mod.TObject.Run.apply(this, arguments);',
  32379. ' return $mod.TObject.Run.call(this, 3);',
  32380. ' return $mod.TObject.Run.call(this, 5);',
  32381. ' return 6;',
  32382. ' return Result;',
  32383. ' };',
  32384. '});',
  32385. '']),
  32386. LinesToStr([
  32387. '']));
  32388. CheckResolverUnexpectedHints();
  32389. end;
  32390. procedure TTestModule.TestAsync_ClassInterface;
  32391. begin
  32392. StartProgram(false);
  32393. Add([
  32394. '{$mode objfpc}',
  32395. '{$modeswitch externalclass}',
  32396. 'type',
  32397. ' TJSPromise = class external name ''Promise''',
  32398. ' end;',
  32399. ' IUnknown = interface',
  32400. ' function _AddRef: longint;',
  32401. ' function _Release: longint;',
  32402. ' end;',
  32403. 'function Say(i: IUnknown): IUnknown; async;',
  32404. 'begin',
  32405. 'end;',
  32406. 'function Run: IUnknown; async;',
  32407. 'begin',
  32408. ' Result:=await(Run);',
  32409. ' Result:=await(Run());',
  32410. ' Result:=await(Run) as IUnknown;',
  32411. ' Result:=await(Say(nil));',
  32412. ' Result:=await(Say(await(Run())));',
  32413. ' Result:=await(Say(await(Run()) as IUnknown));',
  32414. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32415. 'end;',
  32416. 'procedure Fly;',
  32417. 'var p: TJSPromise;',
  32418. 'begin',
  32419. ' Run;',
  32420. ' Run();',
  32421. ' p:=Run;',
  32422. ' p:=Run();',
  32423. 'end;',
  32424. 'begin',
  32425. ' ']);
  32426. ConvertProgram;
  32427. CheckSource('TestAsync_ClassInterface',
  32428. LinesToStr([ // statements
  32429. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32430. 'this.Say = async function (i) {',
  32431. ' var Result = null;',
  32432. ' return Result;',
  32433. '};',
  32434. 'this.Run = async function () {',
  32435. ' var Result = null;',
  32436. ' var $ok = false;',
  32437. ' try {',
  32438. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32439. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32440. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32441. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32442. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32443. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32444. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32445. ' $ok = true;',
  32446. ' } finally {',
  32447. ' if (!$ok) rtl._Release(Result);',
  32448. ' };',
  32449. ' return Result;',
  32450. '};',
  32451. 'this.Fly = function () {',
  32452. ' var p = null;',
  32453. ' $mod.Run();',
  32454. ' $mod.Run();',
  32455. ' p = $mod.Run();',
  32456. ' p = $mod.Run();',
  32457. '};',
  32458. '']),
  32459. LinesToStr([
  32460. '']));
  32461. CheckResolverUnexpectedHints();
  32462. end;
  32463. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32464. begin
  32465. StartProgram(true,[supTInterfacedObject]);
  32466. Add([
  32467. '{$mode objfpc}',
  32468. '{$modeswitch externalclass}',
  32469. 'type',
  32470. ' TJSPromise = class external name ''Promise''',
  32471. ' end;',
  32472. ' IBird = interface',
  32473. ' procedure Run;',
  32474. ' end;',
  32475. ' TBird = class(TInterfacedObject,IBird)',
  32476. ' procedure Run; async;',
  32477. ' end;',
  32478. 'procedure TBird.Run;',
  32479. 'begin',
  32480. 'end;',
  32481. 'begin',
  32482. ' ']);
  32483. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32484. ConvertProgram;
  32485. end;
  32486. procedure TTestModule.TestLibrary_Empty;
  32487. begin
  32488. StartLibrary(false);
  32489. Add([
  32490. '']);
  32491. ConvertLibrary;
  32492. CheckSource('TestLibrary_Empty',
  32493. LinesToStr([ // statements
  32494. '']),
  32495. LinesToStr([
  32496. '']));
  32497. CheckResolverUnexpectedHints();
  32498. end;
  32499. procedure TTestModule.TestLibrary_ExportFunc;
  32500. begin
  32501. exit;
  32502. StartLibrary(false);
  32503. Add([
  32504. 'procedure Run(w: word);',
  32505. 'begin',
  32506. 'end;',
  32507. 'exports',
  32508. ' Run,',
  32509. ' run name ''Foo'';',
  32510. '']);
  32511. ConvertLibrary;
  32512. CheckSource('TestLibrary_ExportFunc',
  32513. LinesToStr([ // statements
  32514. '']),
  32515. LinesToStr([
  32516. '']));
  32517. CheckResolverUnexpectedHints();
  32518. end;
  32519. Initialization
  32520. RegisterTests([TTestModule]);
  32521. end.