tcmodules.pas 876 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426
  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 TestEnum_Name_Anonymous_Unit;
  355. Procedure TestSet_Enum;
  356. Procedure TestSet_Operators;
  357. Procedure TestSet_Operator_In;
  358. Procedure TestSet_Functions;
  359. Procedure TestSet_PassAsArgClone;
  360. Procedure TestSet_AsParams;
  361. Procedure TestSet_Property;
  362. Procedure TestSet_EnumConst;
  363. Procedure TestSet_IntConst;
  364. Procedure TestSet_IntRange;
  365. Procedure TestSet_AnonymousEnumType;
  366. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  367. Procedure TestSet_ConstEnum;
  368. Procedure TestSet_ConstChar;
  369. Procedure TestSet_ConstInt;
  370. Procedure TestSet_InFunction;
  371. Procedure TestSet_ForIn;
  372. // statements
  373. Procedure TestNestBegin;
  374. Procedure TestIncDec;
  375. Procedure TestLoHiFpcMode;
  376. Procedure TestLoHiDelphiMode;
  377. Procedure TestAssignments;
  378. Procedure TestArithmeticOperators1;
  379. Procedure TestLogicalOperators;
  380. Procedure TestBitwiseOperators;
  381. Procedure TestBitwiseOperatorsLongword;
  382. Procedure TestFunctionInt;
  383. Procedure TestFunctionString;
  384. Procedure TestIfThen;
  385. Procedure TestForLoop;
  386. Procedure TestForLoopInsideFunction;
  387. Procedure TestForLoop_ReadVarAfter;
  388. Procedure TestForLoop_Nested;
  389. Procedure TestRepeatUntil;
  390. Procedure TestAsmBlock;
  391. Procedure TestAsmPas_Impl; // ToDo
  392. Procedure TestTryFinally;
  393. Procedure TestTryExcept;
  394. Procedure TestTryExcept_ReservedWords;
  395. Procedure TestIfThenRaiseElse;
  396. Procedure TestCaseOf;
  397. Procedure TestCaseOf_UseSwitch;
  398. Procedure TestCaseOfNoElse;
  399. Procedure TestCaseOfNoElse_UseSwitch;
  400. Procedure TestCaseOfRange;
  401. Procedure TestCaseOfString;
  402. Procedure TestCaseOfChar;
  403. Procedure TestCaseOfExternalClassConst;
  404. Procedure TestDebugger;
  405. // arrays
  406. Procedure TestArray_Dynamic;
  407. Procedure TestArray_Dynamic_Nil;
  408. Procedure TestArray_DynMultiDimensional;
  409. Procedure TestArray_DynamicAssign;
  410. Procedure TestArray_StaticInt;
  411. Procedure TestArray_StaticBool;
  412. Procedure TestArray_StaticChar;
  413. Procedure TestArray_StaticMultiDim;
  414. Procedure TestArray_StaticInFunction;
  415. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  416. Procedure TestArrayOfRecord;
  417. Procedure TestArray_StaticRecord;
  418. Procedure TestArrayOfSet;
  419. Procedure TestArray_DynAsParam;
  420. Procedure TestArray_StaticAsParam;
  421. Procedure TestArrayElement_AsParams;
  422. Procedure TestArrayElementFromFuncResult_AsParams;
  423. Procedure TestArrayEnumTypeRange;
  424. Procedure TestArray_SetLengthOutArg;
  425. Procedure TestArray_SetLengthProperty;
  426. Procedure TestArray_SetLengthMultiDim;
  427. Procedure TestArray_SetLengthDynOfStatic;
  428. Procedure TestArray_OpenArrayOfString;
  429. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  430. Procedure TestArray_ConstRef;
  431. Procedure TestArray_Concat;
  432. Procedure TestArray_Copy;
  433. Procedure TestArray_InsertDelete;
  434. Procedure TestArray_DynArrayConstObjFPC;
  435. Procedure TestArray_DynArrayConstDelphi;
  436. Procedure TestArray_ArrayLitAsParam;
  437. Procedure TestArray_ArrayLitMultiDimAsParam;
  438. Procedure TestArray_ArrayLitStaticAsParam;
  439. Procedure TestArray_ForInArrOfString;
  440. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  441. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  442. Procedure TestArrayOfConst_TVarRec;
  443. Procedure TestArrayOfConst_PassBaseTypes;
  444. Procedure TestArrayOfConst_PassObj;
  445. // record
  446. Procedure TestRecord_Empty;
  447. Procedure TestRecord_Var;
  448. Procedure TestRecord_VarExternal;
  449. Procedure TestRecord_WithDo;
  450. Procedure TestRecord_Assign;
  451. Procedure TestRecord_AsParams;
  452. Procedure TestRecord_ConstRef;
  453. Procedure TestRecordElement_AsParams;
  454. Procedure TestRecordElementFromFuncResult_AsParams;
  455. Procedure TestRecordElementFromWith_AsParams;
  456. Procedure TestRecord_Equal;
  457. Procedure TestRecord_JSValue;
  458. Procedure TestRecord_VariantFail;
  459. Procedure TestRecord_FieldArray;
  460. Procedure TestRecord_Const;
  461. Procedure TestRecord_TypecastFail;
  462. Procedure TestRecord_InFunction;
  463. Procedure TestRecord_AnonymousFail;
  464. // advanced record
  465. Procedure TestAdvRecord_Function;
  466. Procedure TestAdvRecord_Property;
  467. Procedure TestAdvRecord_PropertyDefault;
  468. Procedure TestAdvRecord_Property_ClassMethod;
  469. Procedure TestAdvRecord_Const;
  470. Procedure TestAdvRecord_ExternalField;
  471. Procedure TestAdvRecord_SubRecord;
  472. Procedure TestAdvRecord_SubClass;
  473. Procedure TestAdvRecord_SubInterfaceFail;
  474. Procedure TestAdvRecord_Constructor;
  475. Procedure TestAdvRecord_ClassConstructor_Program;
  476. Procedure TestAdvRecord_ClassConstructor_Unit;
  477. // classes
  478. Procedure TestClass_TObjectDefaultConstructor;
  479. Procedure TestClass_TObjectConstructorWithParams;
  480. Procedure TestClass_TObjectConstructorWithDefaultParam;
  481. Procedure TestClass_Var;
  482. Procedure TestClass_Method;
  483. Procedure TestClass_Implementation;
  484. Procedure TestClass_Inheritance;
  485. Procedure TestClass_TypeAlias;
  486. Procedure TestClass_AbstractMethod;
  487. Procedure TestClass_CallInherited_ProcNoParams;
  488. Procedure TestClass_CallInherited_WithParams;
  489. Procedure TestClasS_CallInheritedConstructor;
  490. Procedure TestClass_ClassVar_Assign;
  491. Procedure TestClass_CallClassMethod;
  492. Procedure TestClass_CallClassMethodStatic; // ToDo
  493. Procedure TestClass_Property;
  494. Procedure TestClass_Property_ClassMethod;
  495. Procedure TestClass_Property_Indexed;
  496. Procedure TestClass_Property_IndexSpec;
  497. Procedure TestClass_PropertyOfTypeArray;
  498. Procedure TestClass_PropertyDefault;
  499. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  500. //Procedure TestClass_PropertyDefault;
  501. Procedure TestClass_PropertyOverride;
  502. Procedure TestClass_PropertyIncVisibility;
  503. Procedure TestClass_Assigned;
  504. Procedure TestClass_WithClassDoCreate;
  505. Procedure TestClass_WithClassInstDoProperty;
  506. Procedure TestClass_WithClassInstDoPropertyWithParams;
  507. Procedure TestClass_WithClassInstDoFunc;
  508. Procedure TestClass_TypeCast;
  509. Procedure TestClass_TypeCastUntypedParam;
  510. Procedure TestClass_Overloads;
  511. Procedure TestClass_OverloadsAncestor;
  512. Procedure TestClass_OverloadConstructor;
  513. Procedure TestClass_OverloadDelphiOverride;
  514. Procedure TestClass_ReintroduceVarDelphi;
  515. Procedure TestClass_ReintroducedVar;
  516. Procedure TestClass_RaiseDescendant;
  517. Procedure TestClass_ExternalMethod;
  518. Procedure TestClass_ExternalVirtualNameMismatchFail;
  519. Procedure TestClass_ExternalOverrideFail;
  520. Procedure TestClass_ExternalVar;
  521. Procedure TestClass_Const;
  522. Procedure TestClass_ConstEnum;
  523. Procedure TestClass_LocalConstDuplicate_Prg;
  524. Procedure TestClass_LocalConstDuplicate_Unit;
  525. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  526. Procedure TestClass_LocalVarSelfFail;
  527. Procedure TestClass_ArgSelfFail;
  528. Procedure TestClass_NestedProcSelf;
  529. Procedure TestClass_NestedProcSelf2;
  530. Procedure TestClass_NestedProcClassSelf;
  531. Procedure TestClass_NestedProcCallInherited;
  532. Procedure TestClass_TObjectFree;
  533. Procedure TestClass_TObjectFree_VarArg;
  534. Procedure TestClass_TObjectFreeNewInstance;
  535. Procedure TestClass_TObjectFreeLowerCase;
  536. Procedure TestClass_TObjectFreeFunctionFail;
  537. Procedure TestClass_TObjectFreePropertyFail;
  538. Procedure TestClass_ForIn;
  539. Procedure TestClass_DispatchMessage;
  540. Procedure TestClass_Message_DuplicateIntFail;
  541. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  542. // class of
  543. Procedure TestClassOf_Create;
  544. Procedure TestClassOf_Call;
  545. Procedure TestClassOf_Assign;
  546. Procedure TestClassOf_Is;
  547. Procedure TestClassOf_Compare;
  548. Procedure TestClassOf_ClassVar;
  549. Procedure TestClassOf_ClassMethod;
  550. Procedure TestClassOf_ClassProperty;
  551. Procedure TestClassOf_ClassMethodSelf;
  552. Procedure TestClassOf_TypeCast;
  553. Procedure TestClassOf_ImplicitFunctionCall;
  554. Procedure TestClassOf_Const;
  555. // nested class
  556. Procedure TestNestedClass_Alias;
  557. Procedure TestNestedClass_Record;
  558. Procedure TestNestedClass_Class;
  559. // external class
  560. Procedure TestExternalClass_Var;
  561. Procedure TestExternalClass_Const;
  562. Procedure TestExternalClass_Dollar;
  563. Procedure TestExternalClass_DuplicateVarFail;
  564. Procedure TestExternalClass_Method;
  565. Procedure TestExternalClass_ClassMethod;
  566. Procedure TestExternalClass_ClassMethodStatic;
  567. Procedure TestExternalClass_FunctionResultInTypeCast;
  568. Procedure TestExternalClass_NonExternalOverride;
  569. Procedure TestExternalClass_OverloadHint;
  570. Procedure TestExternalClass_SameNamePublishedProperty;
  571. Procedure TestExternalClass_Property;
  572. Procedure TestExternalClass_PropertyDate;
  573. Procedure TestExternalClass_ClassProperty;
  574. Procedure TestExternalClass_ClassOf;
  575. Procedure TestExternalClass_ClassOtherUnit;
  576. Procedure TestExternalClass_Is;
  577. Procedure TestExternalClass_As;
  578. Procedure TestExternalClass_DestructorFail;
  579. Procedure TestExternalClass_New;
  580. Procedure TestExternalClass_ClassOf_New;
  581. Procedure TestExternalClass_FuncClassOf_New;
  582. Procedure TestExternalClass_New_PasClassFail;
  583. Procedure TestExternalClass_New_PasClassBracketsFail;
  584. Procedure TestExternalClass_NewExtName;
  585. Procedure TestExternalClass_Constructor;
  586. Procedure TestExternalClass_ConstructorBrackets;
  587. Procedure TestExternalClass_LocalConstSameName;
  588. Procedure TestExternalClass_ReintroduceOverload;
  589. Procedure TestExternalClass_Inherited;
  590. Procedure TestExternalClass_PascalAncestorFail;
  591. Procedure TestExternalClass_NewInstance;
  592. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  593. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  594. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  595. Procedure TestExternalClass_JSFunctionPasDescendant;
  596. Procedure TestExternalClass_PascalProperty;
  597. Procedure TestExternalClass_TypeCastToRootClass;
  598. Procedure TestExternalClass_TypeCastToJSObject;
  599. Procedure TestExternalClass_TypeCastStringToExternalString;
  600. Procedure TestExternalClass_TypeCastToJSFunction;
  601. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  602. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  603. Procedure TestExternalClass_BracketAccessor;
  604. Procedure TestExternalClass_BracketAccessor_Call;
  605. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  606. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  607. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  608. Procedure TestExternalClass_BracketAccessor_MultiType;
  609. Procedure TestExternalClass_BracketAccessor_Index;
  610. Procedure TestExternalClass_ForInJSObject;
  611. Procedure TestExternalClass_ForInJSArray;
  612. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  613. // class interfaces
  614. Procedure TestClassInterface_Corba;
  615. Procedure TestClassInterface_ProcExternalFail;
  616. Procedure TestClassInterface_Overloads;
  617. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  618. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  619. Procedure TestClassInterface_AncestorImpl;
  620. Procedure TestClassInterface_ImplReintroduce;
  621. Procedure TestClassInterface_MethodResolution;
  622. Procedure TestClassInterface_AncestorMoreInterfaces;
  623. Procedure TestClassInterface_MethodOverride;
  624. Procedure TestClassInterface_Corba_Delegation;
  625. Procedure TestClassInterface_Corba_DelegationStatic;
  626. Procedure TestClassInterface_Corba_Operators;
  627. Procedure TestClassInterface_Corba_Args;
  628. Procedure TestClassInterface_Corba_ForIn;
  629. Procedure TestClassInterface_COM_AssignVar;
  630. Procedure TestClassInterface_COM_AssignArg;
  631. Procedure TestClassInterface_COM_FunctionResult;
  632. Procedure TestClassInterface_COM_InheritedFuncResult;
  633. Procedure TestClassInterface_COM_IsAsTypeCasts;
  634. Procedure TestClassInterface_COM_PassAsArg;
  635. Procedure TestClassInterface_COM_PassToUntypedParam;
  636. Procedure TestClassInterface_COM_FunctionInExpr;
  637. Procedure TestClassInterface_COM_Property;
  638. Procedure TestClassInterface_COM_IntfProperty;
  639. Procedure TestClassInterface_COM_Delegation;
  640. Procedure TestClassInterface_COM_With;
  641. Procedure TestClassInterface_COM_ForIn;
  642. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  643. Procedure TestClassInterface_COM_RecordIntfFail;
  644. Procedure TestClassInterface_COM_UnitInitialization;
  645. Procedure TestClassInterface_GUID;
  646. Procedure TestClassInterface_GUIDProperty;
  647. // helpers
  648. Procedure TestClassHelper_ClassVar;
  649. Procedure TestClassHelper_Method_AccessInstanceFields;
  650. Procedure TestClassHelper_Method_Call;
  651. Procedure TestClassHelper_Method_Nested_Call;
  652. Procedure TestClassHelper_ClassMethod_Call;
  653. Procedure TestClassHelper_ClassOf;
  654. Procedure TestClassHelper_MethodRefObjFPC;
  655. Procedure TestClassHelper_Constructor;
  656. Procedure TestClassHelper_InheritedObjFPC;
  657. Procedure TestClassHelper_Property;
  658. Procedure TestClassHelper_Property_Array;
  659. Procedure TestClassHelper_Property_Array_Default;
  660. Procedure TestClassHelper_Property_Array_DefaultDefault;
  661. Procedure TestClassHelper_ClassProperty;
  662. Procedure TestClassHelper_ClassPropertyStatic;
  663. Procedure TestClassHelper_ClassProperty_Array;
  664. Procedure TestClassHelper_ForIn;
  665. Procedure TestClassHelper_PassProperty;
  666. Procedure TestExtClassHelper_ClassVar;
  667. Procedure TestExtClassHelper_Method_Call;
  668. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  669. Procedure TestRecordHelper_ClassVar;
  670. Procedure TestRecordHelper_Method_Call;
  671. Procedure TestRecordHelper_Constructor;
  672. Procedure TestTypeHelper_ClassVar;
  673. Procedure TestTypeHelper_PassResultElement;
  674. Procedure TestTypeHelper_PassArgs;
  675. Procedure TestTypeHelper_PassVarConst;
  676. Procedure TestTypeHelper_PassFuncResult;
  677. Procedure TestTypeHelper_PassPropertyField;
  678. Procedure TestTypeHelper_PassPropertyGetter;
  679. Procedure TestTypeHelper_PassClassPropertyField;
  680. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  681. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  682. Procedure TestTypeHelper_Property;
  683. Procedure TestTypeHelper_Property_Array;
  684. Procedure TestTypeHelper_ClassProperty;
  685. Procedure TestTypeHelper_ClassProperty_Array;
  686. Procedure TestTypeHelper_ClassMethod;
  687. Procedure TestTypeHelper_ExtClassMethodFail;
  688. Procedure TestTypeHelper_Constructor;
  689. Procedure TestTypeHelper_Word;
  690. Procedure TestTypeHelper_Boolean;
  691. Procedure TestTypeHelper_WordBool;
  692. Procedure TestTypeHelper_Double;
  693. Procedure TestTypeHelper_NativeInt;
  694. Procedure TestTypeHelper_StringChar;
  695. Procedure TestTypeHelper_JSValue;
  696. Procedure TestTypeHelper_Array;
  697. Procedure TestTypeHelper_EnumType;
  698. Procedure TestTypeHelper_SetType;
  699. Procedure TestTypeHelper_InterfaceType;
  700. Procedure TestTypeHelper_NestedSelf;
  701. // proc types
  702. Procedure TestProcType;
  703. Procedure TestProcType_Arg;
  704. Procedure TestProcType_FunctionFPC;
  705. Procedure TestProcType_FunctionDelphi;
  706. Procedure TestProcType_ProcedureDelphi;
  707. Procedure TestProcType_AsParam;
  708. Procedure TestProcType_MethodFPC;
  709. Procedure TestProcType_MethodDelphi;
  710. Procedure TestProcType_PropertyFPC;
  711. Procedure TestProcType_PropertyDelphi;
  712. Procedure TestProcType_WithClassInstDoPropertyFPC;
  713. Procedure TestProcType_Nested;
  714. Procedure TestProcType_NestedOfObject;
  715. Procedure TestProcType_ReferenceToProc;
  716. Procedure TestProcType_ReferenceToMethod;
  717. Procedure TestProcType_Typecast;
  718. Procedure TestProcType_PassProcToUntyped;
  719. Procedure TestProcType_PassProcToArray;
  720. Procedure TestProcType_SafeCallObjFPC;
  721. Procedure TestProcType_SafeCallDelphi;
  722. // pointer
  723. Procedure TestPointer;
  724. Procedure TestPointer_Proc;
  725. Procedure TestPointer_AssignRecordFail;
  726. Procedure TestPointer_AssignStaticArrayFail;
  727. Procedure TestPointer_TypeCastJSValueToPointer;
  728. Procedure TestPointer_NonRecordFail;
  729. Procedure TestPointer_AnonymousArgTypeFail;
  730. Procedure TestPointer_AnonymousVarTypeFail;
  731. Procedure TestPointer_AnonymousResultTypeFail;
  732. Procedure TestPointer_AddrOperatorFail;
  733. Procedure TestPointer_ArrayParamsFail;
  734. Procedure TestPointer_PointerAddFail;
  735. Procedure TestPointer_IncPointerFail;
  736. Procedure TestPointer_Record;
  737. Procedure TestPointer_RecordArg;
  738. // jsvalue
  739. Procedure TestJSValue_AssignToJSValue;
  740. Procedure TestJSValue_TypeCastToBaseType;
  741. Procedure TestJSValue_TypecastToJSValue;
  742. Procedure TestJSValue_Equal;
  743. Procedure TestJSValue_If;
  744. Procedure TestJSValue_Not;
  745. Procedure TestJSValue_Enum;
  746. Procedure TestJSValue_ClassInstance;
  747. Procedure TestJSValue_ClassOf;
  748. Procedure TestJSValue_ArrayOfJSValue;
  749. Procedure TestJSValue_ArrayLit;
  750. Procedure TestJSValue_Params;
  751. Procedure TestJSValue_UntypedParam;
  752. Procedure TestJSValue_FuncResultType;
  753. Procedure TestJSValue_ProcType_Assign;
  754. Procedure TestJSValue_ProcType_Equal;
  755. Procedure TestJSValue_ProcType_Param;
  756. Procedure TestJSValue_AssignToPointerFail;
  757. Procedure TestJSValue_OverloadDouble;
  758. Procedure TestJSValue_OverloadNativeInt;
  759. Procedure TestJSValue_OverloadWord;
  760. Procedure TestJSValue_OverloadString;
  761. Procedure TestJSValue_OverloadChar;
  762. Procedure TestJSValue_OverloadPointer;
  763. Procedure TestJSValue_ForIn;
  764. // RTTI
  765. Procedure TestRTTI_IntRange;
  766. Procedure TestRTTI_Double;
  767. Procedure TestRTTI_ProcType;
  768. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  769. Procedure TestRTTI_EnumAndSetType;
  770. Procedure TestRTTI_EnumRange;
  771. Procedure TestRTTI_AnonymousEnumType;
  772. Procedure TestRTTI_StaticArray;
  773. Procedure TestRTTI_DynArray;
  774. Procedure TestRTTI_ArrayNestedAnonymous;
  775. Procedure TestRTTI_PublishedMethodOverloadFail;
  776. Procedure TestRTTI_PublishedMethodExternalFail;
  777. Procedure TestRTTI_PublishedClassPropertyFail;
  778. Procedure TestRTTI_PublishedClassFieldFail;
  779. Procedure TestRTTI_PublishedFieldExternalFail;
  780. Procedure TestRTTI_Class_Field;
  781. Procedure TestRTTI_Class_Method;
  782. Procedure TestRTTI_Class_MethodArgFlags;
  783. Procedure TestRTTI_Class_Property;
  784. Procedure TestRTTI_Class_PropertyParams;
  785. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  786. Procedure TestRTTI_Class_OmitRTTI;
  787. Procedure TestRTTI_IndexModifier;
  788. Procedure TestRTTI_StoredModifier;
  789. Procedure TestRTTI_DefaultValue;
  790. Procedure TestRTTI_DefaultValueSet;
  791. Procedure TestRTTI_DefaultValueRangeType;
  792. Procedure TestRTTI_DefaultValueInherit;
  793. Procedure TestRTTI_OverrideMethod;
  794. Procedure TestRTTI_ReintroduceMethod;
  795. Procedure TestRTTI_OverloadProperty;
  796. // ToDo: array argument
  797. Procedure TestRTTI_ClassForward;
  798. Procedure TestRTTI_ClassOf;
  799. Procedure TestRTTI_Record;
  800. Procedure TestRTTI_RecordAnonymousArray;
  801. Procedure TestRTTI_Record_ClassVarType;
  802. Procedure TestRTTI_LocalTypes;
  803. Procedure TestRTTI_TypeInfo_BaseTypes;
  804. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  805. Procedure TestRTTI_TypeInfo_LocalFail;
  806. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  807. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  808. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  809. Procedure TestRTTI_TypeInfo_FunctionClassType;
  810. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  811. Procedure TestRTTI_Interface_Corba;
  812. Procedure TestRTTI_Interface_COM;
  813. Procedure TestRTTI_ClassHelper;
  814. Procedure TestRTTI_ExternalClass;
  815. Procedure TestRTTI_Unit;
  816. // Resourcestring
  817. Procedure TestResourcestringProgram;
  818. Procedure TestResourcestringUnit;
  819. Procedure TestResourcestringImplementation;
  820. // Attributes
  821. Procedure TestAttributes_Members;
  822. Procedure TestAttributes_Types;
  823. Procedure TestAttributes_HelperConstructor_Fail;
  824. // Assertions, checks
  825. procedure TestAssert;
  826. procedure TestAssert_SysUtils;
  827. procedure TestObjectChecks;
  828. procedure TestOverflowChecks_Int;
  829. procedure TestRangeChecks_AssignInt;
  830. procedure TestRangeChecks_AssignIntRange;
  831. procedure TestRangeChecks_AssignEnum;
  832. procedure TestRangeChecks_AssignEnumRange;
  833. procedure TestRangeChecks_AssignChar;
  834. procedure TestRangeChecks_AssignCharRange;
  835. procedure TestRangeChecks_ArrayIndex;
  836. procedure TestRangeChecks_ArrayOfRecIndex;
  837. procedure TestRangeChecks_StringIndex;
  838. procedure TestRangeChecks_TypecastInt;
  839. procedure TestRangeChecks_TypeHelperInt;
  840. // Async/AWait
  841. Procedure TestAsync_Proc;
  842. Procedure TestAsync_CallResultIsPromise;
  843. Procedure TestAsync_ConstructorFail;
  844. Procedure TestAsync_PropertyGetterFail;
  845. Procedure TestAwait_NonPromiseWithTypeFail;
  846. Procedure TestAwait_AsyncCallTypeMismatch;
  847. Procedure TestAWait_OutsideAsyncFail;
  848. Procedure TestAWait_IntegerFail;
  849. Procedure TestAWait_ExternalClassPromise;
  850. Procedure TestAWait_JSValue;
  851. Procedure TestAWait_Result;
  852. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  853. Procedure TestAsync_AnonymousProc;
  854. Procedure TestAsync_ProcType;
  855. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  856. Procedure TestAsync_Inherited;
  857. Procedure TestAsync_ClassInterface;
  858. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  859. // Library
  860. Procedure TestLibrary_Empty;
  861. Procedure TestLibrary_ExportFunc; // ToDo
  862. // ToDo: test delayed specialization init
  863. // ToDO: analyzer
  864. end;
  865. function LinesToStr(Args: array of const): string;
  866. function ExtractFileUnitName(aFilename: string): string;
  867. function JSToStr(El: TJSElement): string;
  868. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  869. implementation
  870. function LinesToStr(Args: array of const): string;
  871. var
  872. s: String;
  873. i: Integer;
  874. begin
  875. s:='';
  876. for i:=Low(Args) to High(Args) do
  877. case Args[i].VType of
  878. vtChar: s += Args[i].VChar+LineEnding;
  879. vtString: s += Args[i].VString^+LineEnding;
  880. vtPChar: s += Args[i].VPChar+LineEnding;
  881. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  882. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  883. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  884. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  885. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  886. end;
  887. Result:=s;
  888. end;
  889. function ExtractFileUnitName(aFilename: string): string;
  890. var
  891. p: Integer;
  892. begin
  893. Result:=ExtractFileName(aFilename);
  894. if Result='' then exit;
  895. for p:=length(Result) downto 1 do
  896. case Result[p] of
  897. '/','\': exit;
  898. '.':
  899. begin
  900. Delete(Result,p,length(Result));
  901. exit;
  902. end;
  903. end;
  904. end;
  905. function JSToStr(El: TJSElement): string;
  906. var
  907. aWriter: TBufferWriter;
  908. aJSWriter: TJSWriter;
  909. begin
  910. aJSWriter:=nil;
  911. aWriter:=TBufferWriter.Create(1000);
  912. try
  913. aJSWriter:=TJSWriter.Create(aWriter);
  914. aJSWriter.IndentSize:=2;
  915. aJSWriter.WriteJS(El);
  916. Result:=aWriter.AsString;
  917. finally
  918. aJSWriter.Free;
  919. aWriter.Free;
  920. end;
  921. end;
  922. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  923. // search diff, ignore changes in spaces
  924. const
  925. SpaceChars = [#9,#10,#13,' '];
  926. var
  927. ExpectedP, ActualP: PChar;
  928. function FindLineEnd(p: PChar): PChar;
  929. begin
  930. Result:=p;
  931. while not (Result^ in [#0,#10,#13]) do inc(Result);
  932. end;
  933. function FindLineStart(p, MinP: PChar): PChar;
  934. begin
  935. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  936. Result:=p;
  937. end;
  938. procedure SkipLineEnd(var p: PChar);
  939. begin
  940. if p^ in [#10,#13] then
  941. begin
  942. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  943. inc(p,2)
  944. else
  945. inc(p);
  946. end;
  947. end;
  948. function HasSpecialChar(s: string): boolean;
  949. var
  950. i: Integer;
  951. begin
  952. for i:=1 to length(s) do
  953. if s[i] in [#0..#31,#127..#255] then
  954. exit(true);
  955. Result:=false;
  956. end;
  957. function HashSpecialChars(s: string): string;
  958. var
  959. i: Integer;
  960. begin
  961. Result:='';
  962. for i:=1 to length(s) do
  963. if s[i] in [#0..#31,#127..#255] then
  964. Result:=Result+'#'+hexstr(ord(s[i]),2)
  965. else
  966. Result:=Result+s[i];
  967. end;
  968. procedure DiffFound;
  969. var
  970. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  971. ExpLine, ActLine: String;
  972. i, LineNo, DiffLineNo: Integer;
  973. begin
  974. writeln('Diff found "',Msg,'". Lines:');
  975. // write correct lines
  976. p:=PChar(Expected);
  977. LineNo:=0;
  978. DiffLineNo:=0;
  979. repeat
  980. StartPos:=p;
  981. while not (p^ in [#0,#10,#13]) do inc(p);
  982. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  983. SkipLineEnd(p);
  984. inc(LineNo);
  985. if (p<=ExpectedP) and (p^<>#0) then
  986. begin
  987. writeln('= ',ExpLine);
  988. end else begin
  989. // diff line
  990. if DiffLineNo=0 then DiffLineNo:=LineNo;
  991. // write actual line
  992. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  993. ActLineEndP:=FindLineEnd(ActualP);
  994. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  995. writeln('- ',ActLine);
  996. if HasSpecialChar(ActLine) then
  997. writeln('- ',HashSpecialChars(ActLine));
  998. // write expected line
  999. writeln('+ ',ExpLine);
  1000. if HasSpecialChar(ExpLine) then
  1001. writeln('- ',HashSpecialChars(ExpLine));
  1002. // write empty line with pointer ^
  1003. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1004. writeln('^');
  1005. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1006. CheckSrcDiff:=false;
  1007. // write up to three following actual lines to get some context
  1008. for i:=1 to 3 do begin
  1009. ActLineStartP:=ActLineEndP;
  1010. SkipLineEnd(ActLineStartP);
  1011. if ActLineStartP^=#0 then break;
  1012. ActLineEndP:=FindLineEnd(ActLineStartP);
  1013. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1014. writeln('~ ',ActLine);
  1015. end;
  1016. exit;
  1017. end;
  1018. until p^=#0;
  1019. writeln('DiffFound Actual:-----------------------');
  1020. writeln(Actual);
  1021. writeln('DiffFound Expected:---------------------');
  1022. writeln(Expected);
  1023. writeln('DiffFound ------------------------------');
  1024. Msg:='diff found, but lines are the same, internal error';
  1025. CheckSrcDiff:=false;
  1026. end;
  1027. var
  1028. IsSpaceNeeded: Boolean;
  1029. LastChar, Quote: Char;
  1030. begin
  1031. Result:=true;
  1032. Msg:='';
  1033. if Expected='' then Expected:=' ';
  1034. if Actual='' then Actual:=' ';
  1035. ExpectedP:=PChar(Expected);
  1036. ActualP:=PChar(Actual);
  1037. repeat
  1038. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1039. case ExpectedP^ of
  1040. #0:
  1041. begin
  1042. // check that rest of Actual has only spaces
  1043. while ActualP^ in SpaceChars do inc(ActualP);
  1044. if ActualP^<>#0 then
  1045. begin
  1046. DiffFound;
  1047. exit;
  1048. end;
  1049. exit(true);
  1050. end;
  1051. ' ',#9,#10,#13:
  1052. begin
  1053. // skip space in Expected
  1054. IsSpaceNeeded:=false;
  1055. if ExpectedP>PChar(Expected) then
  1056. LastChar:=ExpectedP[-1]
  1057. else
  1058. LastChar:=#0;
  1059. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1060. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1061. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1062. IsSpaceNeeded:=true;
  1063. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1064. begin
  1065. DiffFound;
  1066. exit;
  1067. end;
  1068. while ActualP^ in SpaceChars do inc(ActualP);
  1069. end;
  1070. '''','"':
  1071. begin
  1072. while ActualP^ in SpaceChars do inc(ActualP);
  1073. if ExpectedP^<>ActualP^ then
  1074. begin
  1075. DiffFound;
  1076. exit;
  1077. end;
  1078. Quote:=ExpectedP^;
  1079. repeat
  1080. inc(ExpectedP);
  1081. inc(ActualP);
  1082. if ExpectedP^<>ActualP^ then
  1083. begin
  1084. DiffFound;
  1085. exit;
  1086. end;
  1087. if (ExpectedP^ in [#0,#10,#13]) then
  1088. break
  1089. else if (ExpectedP^=Quote) then
  1090. begin
  1091. inc(ExpectedP);
  1092. inc(ActualP);
  1093. break;
  1094. end;
  1095. until false;
  1096. end;
  1097. else
  1098. while ActualP^ in SpaceChars do inc(ActualP);
  1099. if ExpectedP^<>ActualP^ then
  1100. begin
  1101. DiffFound;
  1102. exit;
  1103. end;
  1104. inc(ExpectedP);
  1105. inc(ActualP);
  1106. end;
  1107. until false;
  1108. end;
  1109. { TTestEnginePasResolver }
  1110. destructor TTestEnginePasResolver.Destroy;
  1111. begin
  1112. FreeAndNil(FStreamResolver);
  1113. FreeAndNil(FParser);
  1114. FreeAndNil(FScanner);
  1115. FreeAndNil(FStreamResolver);
  1116. if Module<>nil then
  1117. begin
  1118. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1119. FModule:=nil;
  1120. end;
  1121. inherited Destroy;
  1122. end;
  1123. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1124. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1125. begin
  1126. Result:=nil;
  1127. if InFilename<>'' then
  1128. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1129. if Assigned(OnFindUnit) then
  1130. Result:=OnFindUnit(AName);
  1131. if NameExpr=nil then ;
  1132. end;
  1133. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1134. begin
  1135. // do not parse recursively
  1136. // parse via the queue
  1137. if Section=nil then ;
  1138. end;
  1139. { TCustomTestModule }
  1140. function TCustomTestModule.GetMsgCount: integer;
  1141. begin
  1142. Result:=FHintMsgs.Count;
  1143. end;
  1144. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1145. begin
  1146. Result:=TTestHintMessage(FHintMsgs[Index]);
  1147. end;
  1148. function TCustomTestModule.GetResolverCount: integer;
  1149. begin
  1150. Result:=FModules.Count;
  1151. end;
  1152. function TCustomTestModule.GetResolvers(Index: integer
  1153. ): TTestEnginePasResolver;
  1154. begin
  1155. Result:=TTestEnginePasResolver(FModules[Index]);
  1156. end;
  1157. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1158. ): TPasModule;
  1159. var
  1160. DefNamespace: String;
  1161. begin
  1162. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1163. if (Pos('.',aUnitName)<1) then
  1164. begin
  1165. DefNamespace:=GetDefaultNamespace;
  1166. if DefNamespace<>'' then
  1167. begin
  1168. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1169. if Result<>nil then exit;
  1170. end;
  1171. end;
  1172. Result:=LoadUnit(aUnitName);
  1173. if Result<>nil then exit;
  1174. {$IFDEF VerbosePas2JS}
  1175. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1176. {$ENDIF}
  1177. Fail('can''t find unit "'+aUnitName+'"');
  1178. end;
  1179. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1180. var
  1181. aParser: TPasParser;
  1182. Item: TTestHintMessage;
  1183. begin
  1184. aParser:=Sender as TPasParser;
  1185. Item:=TTestHintMessage.Create;
  1186. Item.Id:=aParser.LastMsgNumber;
  1187. Item.MsgType:=aParser.LastMsgType;
  1188. Item.MsgNumber:=aParser.LastMsgNumber;
  1189. Item.Msg:=Msg;
  1190. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1191. {$IFDEF VerbosePas2JS}
  1192. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1193. {$ENDIF}
  1194. FHintMsgs.Add(Item);
  1195. end;
  1196. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1197. );
  1198. var
  1199. aResolver: TTestEnginePasResolver;
  1200. Item: TTestHintMessage;
  1201. begin
  1202. aResolver:=Sender as TTestEnginePasResolver;
  1203. Item:=TTestHintMessage.Create;
  1204. Item.Id:=aResolver.LastMsgId;
  1205. Item.MsgType:=aResolver.LastMsgType;
  1206. Item.MsgNumber:=aResolver.LastMsgNumber;
  1207. Item.Msg:=Msg;
  1208. Item.SourcePos:=aResolver.LastSourcePos;
  1209. {$IFDEF VerbosePas2JS}
  1210. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1211. {$ENDIF}
  1212. FHintMsgs.Add(Item);
  1213. end;
  1214. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1215. var
  1216. Item: TTestHintMessage;
  1217. aScanner: TPas2jsPasScanner;
  1218. begin
  1219. aScanner:=Sender as TPas2jsPasScanner;
  1220. Item:=TTestHintMessage.Create;
  1221. Item.Id:=aScanner.LastMsgNumber;
  1222. Item.MsgType:=aScanner.LastMsgType;
  1223. Item.MsgNumber:=aScanner.LastMsgNumber;
  1224. Item.Msg:=Msg;
  1225. Item.SourcePos:=aScanner.CurSourcePos;
  1226. {$IFDEF VerbosePas2JS}
  1227. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1228. {$ENDIF}
  1229. FHintMsgs.Add(Item);
  1230. end;
  1231. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1232. begin
  1233. if FWithTypeInfo=AValue then Exit;
  1234. FWithTypeInfo:=AValue;
  1235. if AValue then
  1236. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1237. else
  1238. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1239. end;
  1240. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1241. var
  1242. i: Integer;
  1243. CurEngine: TTestEnginePasResolver;
  1244. CurUnitName: String;
  1245. begin
  1246. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1247. Result:=nil;
  1248. if (Module.ClassType=TPasModule)
  1249. and (CompareText(Module.Name,aUnitName)=0) then
  1250. exit(Module);
  1251. for i:=0 to ResolverCount-1 do
  1252. begin
  1253. CurEngine:=Resolvers[i];
  1254. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1255. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1256. if CompareText(aUnitName,CurUnitName)=0 then
  1257. begin
  1258. Result:=CurEngine.Module;
  1259. if Result<>nil then exit;
  1260. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1261. FileResolver.FindSourceFile(aUnitName);
  1262. CurEngine.StreamResolver:=TStreamResolver.Create;
  1263. CurEngine.StreamResolver.OwnsStreams:=True;
  1264. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1265. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1266. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1267. InitScanner(CurEngine.Scanner);
  1268. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1269. CurEngine.Parser.Options:=po_tcmodules;
  1270. if CompareText(CurUnitName,'System')=0 then
  1271. CurEngine.Parser.ImplicitUses.Clear;
  1272. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1273. try
  1274. CurEngine.Parser.NextToken;
  1275. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1276. except
  1277. on E: Exception do
  1278. HandleException(E);
  1279. end;
  1280. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1281. Result:=CurEngine.Module;
  1282. exit;
  1283. end;
  1284. end;
  1285. end;
  1286. procedure TCustomTestModule.SetUp;
  1287. begin
  1288. {$IFDEF EnablePasTreeGlobalRefCount}
  1289. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1290. {$ENDIF}
  1291. if FModules<>nil then
  1292. begin
  1293. writeln('TCustomTestModule.SetUp FModules<>nil');
  1294. Halt;
  1295. end;
  1296. inherited SetUp;
  1297. FSkipTests:=false;
  1298. FWithTypeInfo:=false;
  1299. FSource:=TStringList.Create;
  1300. FHub:=TPas2JSResolverHub.Create(Self);
  1301. FModules:=TObjectList.Create(true);
  1302. FFilename:='test1.pp';
  1303. FFileResolver:=TStreamResolver.Create;
  1304. FFileResolver.OwnsStreams:=True;
  1305. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1306. InitScanner(FScanner);
  1307. FEngine:=AddModule(Filename);
  1308. FEngine.Scanner:=FScanner;
  1309. FScanner.Resolver:=FEngine;
  1310. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1311. FParser.OnLog:=@OnParserLog;
  1312. FEngine.Parser:=FParser;
  1313. Parser.Options:=po_tcmodules;
  1314. FModule:=Nil;
  1315. FConverter:=CreateConverter;
  1316. FExpectedErrorClass:=nil;
  1317. end;
  1318. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1319. var
  1320. Options: TPasToJsConverterOptions;
  1321. begin
  1322. Result:=TPasToJSConverter.Create;
  1323. Options:=co_tcmodules;
  1324. if WithTypeInfo then
  1325. Exclude(Options,coNoTypeInfo)
  1326. else
  1327. Include(Options,coNoTypeInfo);
  1328. Result.Options:=Options;
  1329. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1330. end;
  1331. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1332. begin
  1333. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1334. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1335. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1336. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1337. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1338. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1339. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1340. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1341. aScanner.OnLog:=@OnScannerLog;
  1342. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1343. end;
  1344. procedure TCustomTestModule.TearDown;
  1345. {$IFDEF CheckPasTreeRefCount}
  1346. var
  1347. El: TPasElement;
  1348. {$ENDIF}
  1349. var
  1350. i: Integer;
  1351. CurModule: TPasModule;
  1352. begin
  1353. FHintMsgs.Clear;
  1354. FHintMsgsGood.Clear;
  1355. FSkipTests:=false;
  1356. FWithTypeInfo:=false;
  1357. FJSRegModuleCall:=nil;
  1358. FJSModuleCallArgs:=nil;
  1359. FJSImplentationUses:=nil;
  1360. FJSInterfaceUses:=nil;
  1361. FJSModuleSrc:=nil;
  1362. FJSInitBody:=nil;
  1363. FreeAndNil(FJSSource);
  1364. FreeAndNil(FJSModule);
  1365. FreeAndNil(FConverter);
  1366. Engine.Clear;
  1367. FreeAndNil(FSource);
  1368. FreeAndNil(FFileResolver);
  1369. if FModules<>nil then
  1370. begin
  1371. for i:=0 to FModules.Count-1 do
  1372. begin
  1373. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1374. if CurModule=nil then continue;
  1375. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1376. CurModule.ReleaseUsedUnits;
  1377. end;
  1378. if FModule<>nil then
  1379. FModule.ReleaseUsedUnits;
  1380. for i:=0 to FModules.Count-1 do
  1381. begin
  1382. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1383. if CurModule=nil then continue;
  1384. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1385. end;
  1386. FreeAndNil(FModules);
  1387. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1388. FEngine:=nil;
  1389. end;
  1390. FreeAndNil(FHub);
  1391. inherited TearDown;
  1392. {$IFDEF EnablePasTreeGlobalRefCount}
  1393. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1394. begin
  1395. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1396. {$IFDEF CheckPasTreeRefCount}
  1397. El:=TPasElement.FirstRefEl;
  1398. while El<>nil do
  1399. begin
  1400. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1401. for i:=0 to El.RefIds.Count-1 do
  1402. writeln(' ',El.RefIds[i]);
  1403. El:=El.NextRefEl;
  1404. end;
  1405. {$ENDIF}
  1406. Halt;
  1407. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1408. end;
  1409. {$ENDIF}
  1410. end;
  1411. procedure TCustomTestModule.Add(Line: string);
  1412. begin
  1413. Source.Add(Line);
  1414. end;
  1415. procedure TCustomTestModule.Add(const Lines: array of string);
  1416. var
  1417. i: Integer;
  1418. begin
  1419. for i:=low(Lines) to high(Lines) do
  1420. Add(Lines[i]);
  1421. end;
  1422. procedure TCustomTestModule.StartParsing;
  1423. var
  1424. Src: String;
  1425. begin
  1426. Src:=Source.Text;
  1427. FEngine.Source:=Src;
  1428. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1429. Scanner.OpenFile(FileName);
  1430. Writeln('// Test : ',Self.TestName);
  1431. Writeln(Src);
  1432. end;
  1433. procedure TCustomTestModule.ParseModuleQueue;
  1434. var
  1435. i: Integer;
  1436. CurResolver: TTestEnginePasResolver;
  1437. Found: Boolean;
  1438. Section: TPasSection;
  1439. begin
  1440. // parse til exception or all modules finished
  1441. while not SkipTests do
  1442. begin
  1443. Found:=false;
  1444. for i:=0 to ResolverCount-1 do
  1445. begin
  1446. CurResolver:=Resolvers[i];
  1447. if CurResolver.CurrentParser=nil then continue;
  1448. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1449. continue;
  1450. CurResolver.Parser.ParseContinue;
  1451. Found:=true;
  1452. break;
  1453. end;
  1454. if not Found then break;
  1455. end;
  1456. for i:=0 to ResolverCount-1 do
  1457. begin
  1458. CurResolver:=Resolvers[i];
  1459. if CurResolver.Parser=nil then
  1460. begin
  1461. if CurResolver.CurrentParser<>nil then
  1462. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1463. continue;
  1464. end;
  1465. if CurResolver.Parser.CurModule<>nil then
  1466. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1467. end;
  1468. end;
  1469. procedure TCustomTestModule.ParseModule;
  1470. begin
  1471. if SkipTests then exit;
  1472. FFirstPasStatement:=nil;
  1473. try
  1474. StartParsing;
  1475. Parser.ParseMain(FModule);
  1476. ParseModuleQueue;
  1477. except
  1478. on E: Exception do
  1479. HandleException(E);
  1480. end;
  1481. if SkipTests then exit;
  1482. AssertNotNull('Module resulted in Module',Module);
  1483. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1484. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1485. end;
  1486. procedure TCustomTestModule.ParseProgram;
  1487. begin
  1488. if SkipTests then exit;
  1489. ParseModule;
  1490. if SkipTests then exit;
  1491. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1492. FPasProgram:=TPasProgram(Module);
  1493. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1494. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1495. if (PasProgram.InitializationSection.Elements.Count>0) then
  1496. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1497. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1498. end;
  1499. procedure TCustomTestModule.ParseLibrary;
  1500. var
  1501. Init: TInitializationSection;
  1502. begin
  1503. if SkipTests then exit;
  1504. ParseModule;
  1505. if SkipTests then exit;
  1506. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1507. FPasLibrary:=TPasLibrary(Module);
  1508. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1509. Init:=PasLibrary.InitializationSection;
  1510. if (Init<>nil) and (Init.Elements.Count>0) then
  1511. if TObject(Init.Elements[0]) is TPasImplBlock then
  1512. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1513. end;
  1514. procedure TCustomTestModule.ParseUnit;
  1515. begin
  1516. if SkipTests then exit;
  1517. ParseModule;
  1518. if SkipTests then exit;
  1519. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1520. AssertNotNull('Has interface section',Module.InterfaceSection);
  1521. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1522. if (Module.InitializationSection<>nil)
  1523. and (Module.InitializationSection.Elements.Count>0)
  1524. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1525. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1526. end;
  1527. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1528. ): TTestEnginePasResolver;
  1529. var
  1530. i: Integer;
  1531. begin
  1532. for i:=0 to ResolverCount-1 do
  1533. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1534. exit(Resolvers[i]);
  1535. Result:=nil;
  1536. end;
  1537. function TCustomTestModule.AddModule(aFilename: string
  1538. ): TTestEnginePasResolver;
  1539. begin
  1540. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1541. if FindModuleWithFilename(aFilename)<>nil then
  1542. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1543. Result:=TTestEnginePasResolver.Create;
  1544. Result.Filename:=aFilename;
  1545. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1546. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1547. Result.OnLog:=@OnPasResolverLog;
  1548. Result.Hub:=Hub;
  1549. FModules.Add(Result);
  1550. end;
  1551. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1552. ): TTestEnginePasResolver;
  1553. begin
  1554. Result:=AddModule(aFilename);
  1555. Result.Source:=Src;
  1556. end;
  1557. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1558. ImplementationSrc: string): TTestEnginePasResolver;
  1559. var
  1560. Src: String;
  1561. begin
  1562. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1563. Src+=LineEnding;
  1564. Src+='interface'+LineEnding;
  1565. Src+=LineEnding;
  1566. Src+=InterfaceSrc;
  1567. Src+='implementation'+LineEnding;
  1568. Src+=LineEnding;
  1569. Src+=ImplementationSrc;
  1570. Src+='end.'+LineEnding;
  1571. Result:=AddModuleWithSrc(aFilename,Src);
  1572. end;
  1573. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1574. var
  1575. Intf, Impl: TStringList;
  1576. begin
  1577. Intf:=TStringList.Create;
  1578. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1579. // unit interface
  1580. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1581. Intf.Add('{$modeswitch externalclass}');
  1582. Intf.Add('type');
  1583. Intf.Add(' integer=longint;');
  1584. Intf.Add(' sizeint=nativeint;');
  1585. //'const',
  1586. //' LineEnding = #10;',
  1587. //' DirectorySeparator = ''/'';',
  1588. //' DriveSeparator = '''';',
  1589. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1590. //' AllowDriveSeparators : set of char = [];',
  1591. if supTObject in Parts then
  1592. Intf.AddStrings([
  1593. 'type',
  1594. ' TClass = class of TObject;',
  1595. ' TObject = class',
  1596. ' constructor Create;',
  1597. ' destructor Destroy; virtual;',
  1598. ' class function ClassType: TClass; assembler;',
  1599. ' class function ClassName: String; assembler;',
  1600. ' class function ClassNameIs(const Name: string): boolean;',
  1601. ' class function ClassParent: TClass; assembler;',
  1602. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1603. ' class function UnitName: String; assembler;',
  1604. ' procedure AfterConstruction; virtual;',
  1605. ' procedure BeforeDestruction;virtual;',
  1606. ' function Equals(Obj: TObject): boolean; virtual;',
  1607. ' function ToString: String; virtual;',
  1608. ' end;']);
  1609. if supTInterfacedObject in Parts then
  1610. Intf.AddStrings([
  1611. ' {$Interfaces COM}',
  1612. ' IUnknown = interface',
  1613. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1614. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1615. ' function _AddRef: Integer;',
  1616. ' function _Release: Integer;',
  1617. ' end;',
  1618. ' IInterface = IUnknown;',
  1619. ' TInterfacedObject = class(TObject,IUnknown)',
  1620. ' protected',
  1621. ' fRefCount: Integer;',
  1622. ' { implement methods of IUnknown }',
  1623. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1624. ' function _AddRef: Integer; virtual;',
  1625. ' function _Release: Integer; virtual;',
  1626. ' end;',
  1627. ' TInterfacedClass = class of TInterfacedObject;',
  1628. '',
  1629. '']);
  1630. if supTVarRec in Parts then
  1631. Intf.AddStrings([
  1632. 'const',
  1633. ' vtInteger = 0;',
  1634. ' vtBoolean = 1;',
  1635. ' vtJSValue = 19;',
  1636. 'type',
  1637. ' PVarRec = ^TVarRec;',
  1638. ' TVarRec = record',
  1639. ' VType : byte;',
  1640. ' VJSValue: JSValue;',
  1641. ' vInteger: longint external name ''VJSValue'';',
  1642. ' vBoolean: boolean external name ''VJSValue'';',
  1643. ' end;',
  1644. ' TVarRecArray = array of TVarRec;',
  1645. 'function VarRecs: TVarRecArray; varargs;',
  1646. '']);
  1647. if supTypeInfo in Parts then
  1648. begin
  1649. Intf.AddStrings([
  1650. 'type',
  1651. ' TTypeKind = (',
  1652. ' tkUnknown, // 0',
  1653. ' tkInteger, // 1',
  1654. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1655. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1656. ' tkEnumeration, // 4',
  1657. ' tkSet, // 5',
  1658. ' tkDouble, // 6',
  1659. ' tkBool, // 7',
  1660. ' tkProcVar, // 8 function or procedure',
  1661. ' tkMethod, // 9 proc var of object',
  1662. ' tkArray, // 10 static array',
  1663. ' tkDynArray, // 11',
  1664. ' tkRecord, // 12',
  1665. ' tkClass, // 13',
  1666. ' tkClassRef, // 14',
  1667. ' tkPointer, // 15',
  1668. ' tkJSValue, // 16',
  1669. ' tkRefToProcVar, // 17 variable of procedure type',
  1670. ' tkInterface, // 18',
  1671. ' //tkObject,',
  1672. ' //tkSString,tkLString,tkAString,tkWString,',
  1673. ' //tkVariant,',
  1674. ' //tkWChar,',
  1675. ' //tkInt64,',
  1676. ' //tkQWord,',
  1677. ' //tkInterfaceRaw,',
  1678. ' //tkUString,tkUChar,',
  1679. ' tkHelper, // 19',
  1680. ' //tkFile,',
  1681. ' tkExtClass // 20',
  1682. ' );',
  1683. ' TTypeKinds = set of TTypeKind;',
  1684. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1685. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1686. ' end;',
  1687. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1688. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1689. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1690. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1691. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1692. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1693. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1694. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1695. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1696. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1697. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1698. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1699. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1700. '']);
  1701. end;
  1702. if supWriteln in Parts then
  1703. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1704. Intf.Add('var');
  1705. Intf.Add(' ExitCode: Longint = 0;');
  1706. // unit implementation
  1707. Impl:=TStringList.Create;
  1708. if supTObject in Parts then
  1709. Impl.AddStrings([
  1710. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1711. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1712. 'asm',
  1713. 'end;',
  1714. 'constructor TObject.Create; begin end;',
  1715. 'destructor TObject.Destroy; begin end;',
  1716. 'class function TObject.ClassType: TClass; assembler;',
  1717. 'asm',
  1718. 'end;',
  1719. 'class function TObject.ClassName: String; assembler;',
  1720. 'asm',
  1721. 'end;',
  1722. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1723. 'begin',
  1724. ' Result:=SameText(Name,ClassName);',
  1725. 'end;',
  1726. 'class function TObject.ClassParent: TClass; assembler;',
  1727. 'asm',
  1728. 'end;',
  1729. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1730. 'asm',
  1731. 'end;',
  1732. 'class function TObject.UnitName: String; assembler;',
  1733. 'asm',
  1734. 'end;',
  1735. 'procedure TObject.AfterConstruction; begin end;',
  1736. 'procedure TObject.BeforeDestruction; begin end;',
  1737. 'function TObject.Equals(Obj: TObject): boolean;',
  1738. 'begin',
  1739. ' Result:=Obj=Self;',
  1740. 'end;',
  1741. 'function TObject.ToString: String;',
  1742. 'begin',
  1743. ' Result:=ClassName;',
  1744. 'end;'
  1745. ]);
  1746. if supTInterfacedObject in Parts then
  1747. Impl.AddStrings([
  1748. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1749. //'begin',
  1750. //'end;',
  1751. 'function TInterfacedObject._AddRef: Integer;',
  1752. 'begin',
  1753. 'end;',
  1754. 'function TInterfacedObject._Release: Integer;',
  1755. 'begin',
  1756. 'end;',
  1757. '']);
  1758. if supTVarRec in Parts then
  1759. Impl.AddStrings([
  1760. 'function VarRecs: TVarRecArray; varargs;',
  1761. 'var',
  1762. ' v: PVarRec;',
  1763. 'begin',
  1764. ' v^.VType:=1;',
  1765. ' v^.VJSValue:=2;',
  1766. 'end;',
  1767. '']);
  1768. try
  1769. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1770. finally
  1771. Intf.Free;
  1772. Impl.Free;
  1773. end;
  1774. end;
  1775. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1776. SystemUnitParts: TSystemUnitParts);
  1777. begin
  1778. if NeedSystemUnit then
  1779. AddSystemUnit(SystemUnitParts)
  1780. else
  1781. Parser.ImplicitUses.Clear;
  1782. Add('program '+ExtractFileUnitName(Filename)+';');
  1783. Add('');
  1784. end;
  1785. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1786. SystemUnitParts: TSystemUnitParts);
  1787. begin
  1788. if NeedSystemUnit then
  1789. AddSystemUnit(SystemUnitParts)
  1790. else
  1791. Parser.ImplicitUses.Clear;
  1792. Add('library '+ExtractFileUnitName(Filename)+';');
  1793. Add('');
  1794. end;
  1795. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1796. SystemUnitParts: TSystemUnitParts);
  1797. begin
  1798. if NeedSystemUnit then
  1799. AddSystemUnit(SystemUnitParts)
  1800. else
  1801. Parser.ImplicitUses.Clear;
  1802. Add('unit Test1;');
  1803. Add('');
  1804. end;
  1805. procedure TCustomTestModule.ConvertModule;
  1806. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1807. out UsesLit: TJSArrayLiteral);
  1808. var
  1809. i: Integer;
  1810. Item: TJSElement;
  1811. Lit: TJSLiteral;
  1812. begin
  1813. UsesLit:=nil;
  1814. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1815. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1816. exit; // null is ok
  1817. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1818. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1819. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1820. begin
  1821. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1822. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1823. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1824. Lit:=TJSLiteral(Item);
  1825. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1826. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1827. end;
  1828. end;
  1829. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1830. out Src: TJSSourceElements);
  1831. var
  1832. FunDecl: TJSFunctionDeclarationStatement;
  1833. FunDef: TJSFuncDef;
  1834. FunBody: TJSFunctionBody;
  1835. begin
  1836. Src:=nil;
  1837. AssertNotNull(ParamName,Arg.Expr);
  1838. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1839. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1840. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1841. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1842. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1843. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1844. AssertNotNull(ParamName+' body',FunDef.Body);
  1845. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1846. FunBody:=FunDef.Body as TJSFunctionBody;
  1847. AssertNotNull(ParamName+' body.A',FunBody.A);
  1848. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1849. Src:=FunBody.A as TJSSourceElements;
  1850. end;
  1851. var
  1852. ModuleNameExpr: TJSLiteral;
  1853. InitFunction: TJSFunctionDeclarationStatement;
  1854. InitAssign: TJSSimpleAssignStatement;
  1855. InitName: String;
  1856. LastNode: TJSElement;
  1857. Arg: TJSArrayLiteralElement;
  1858. begin
  1859. if SkipTests then exit;
  1860. try
  1861. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1862. except
  1863. on E: Exception do
  1864. HandleException(E);
  1865. end;
  1866. if SkipTests then exit;
  1867. if ExpectedErrorClass<>nil then
  1868. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1869. FJSSource:=TStringList.Create;
  1870. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1871. {$IFDEF VerbosePas2JS}
  1872. writeln('TTestModule.ConvertModule JS:');
  1873. write(FJSSource.Text);
  1874. {$ENDIF}
  1875. // rtl.module(...
  1876. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1877. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1878. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1879. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1880. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1881. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1882. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1883. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1884. // parameter 'unitname'
  1885. if JSModuleCallArgs.Elements.Count<1 then
  1886. Fail('rtl.module first param unit missing');
  1887. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1888. AssertNotNull('module name param',Arg.Expr);
  1889. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1890. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1891. if Module is TPasProgram then
  1892. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1893. else if Module is TPasLibrary then
  1894. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1895. else
  1896. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1897. // main uses section
  1898. if JSModuleCallArgs.Elements.Count<2 then
  1899. Fail('rtl.module second param main uses missing');
  1900. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1901. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1902. // program/library/interface function()
  1903. if JSModuleCallArgs.Elements.Count<3 then
  1904. Fail('rtl.module third param intf-function missing');
  1905. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1906. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1907. // search for $mod.$init or $mod.$main - the last statement
  1908. if (Module is TPasProgram) or (Module is TPasLibrary) then
  1909. begin
  1910. InitName:='$main';
  1911. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1912. end
  1913. else
  1914. InitName:='$init';
  1915. FJSInitBody:=nil;
  1916. if JSModuleSrc.Statements.Count>0 then
  1917. begin
  1918. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1919. if LastNode is TJSSimpleAssignStatement then
  1920. begin
  1921. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1922. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1923. begin
  1924. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1925. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1926. end
  1927. else if (Module is TPasProgram) or (Module is TPasLibrary) then
  1928. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1929. end;
  1930. end;
  1931. // optional: implementation uses section
  1932. if JSModuleCallArgs.Elements.Count<4 then
  1933. exit;
  1934. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1935. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1936. end;
  1937. procedure TCustomTestModule.ConvertProgram;
  1938. begin
  1939. Add('end.');
  1940. ParseProgram;
  1941. ConvertModule;
  1942. end;
  1943. procedure TCustomTestModule.ConvertLibrary;
  1944. begin
  1945. Add('end.');
  1946. ParseLibrary;
  1947. ConvertModule;
  1948. end;
  1949. procedure TCustomTestModule.ConvertUnit;
  1950. begin
  1951. Add('end.');
  1952. ParseUnit;
  1953. ConvertModule;
  1954. end;
  1955. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1956. begin
  1957. Result:=tcmodules.JSToStr(El);
  1958. end;
  1959. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1960. DottedName: string);
  1961. begin
  1962. if DottedName='' then
  1963. begin
  1964. AssertNull(Msg,El);
  1965. end
  1966. else
  1967. begin
  1968. AssertNotNull(Msg,El);
  1969. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1970. end;
  1971. end;
  1972. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1973. begin
  1974. if El=nil then
  1975. Result:=''
  1976. else if El is TJSPrimaryExpressionIdent then
  1977. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1978. else if El is TJSDotMemberExpression then
  1979. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1980. else
  1981. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1982. end;
  1983. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1984. InitStatements: string; ImplStatements: string);
  1985. var
  1986. ActualSrc, ExpectedSrc, InitName: String;
  1987. begin
  1988. ActualSrc:=JSToStr(JSModuleSrc);
  1989. if coUseStrict in Converter.Options then
  1990. ExpectedSrc:='"use strict";'+LineEnding
  1991. else
  1992. ExpectedSrc:='';
  1993. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1994. ExpectedSrc:=ExpectedSrc+Statements;
  1995. // unit implementation
  1996. if (Trim(ImplStatements)<>'') then
  1997. ExpectedSrc:=ExpectedSrc+LineEnding
  1998. +'$mod.$implcode = function () {'+LineEnding
  1999. +ImplStatements
  2000. +'};'+LineEnding;
  2001. // program main or unit initialization
  2002. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  2003. begin
  2004. if (Module is TPasProgram) or (Module is TPasLibrary) then
  2005. InitName:='$main'
  2006. else
  2007. InitName:='$init';
  2008. ExpectedSrc:=ExpectedSrc+LineEnding
  2009. +'$mod.'+InitName+' = function () {'+LineEnding
  2010. +InitStatements
  2011. +'};'+LineEnding;
  2012. end;
  2013. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2014. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2015. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2016. end;
  2017. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2018. // search diff, ignore changes in spaces
  2019. var
  2020. s: string;
  2021. begin
  2022. if CheckSrcDiff(Expected,Actual,s) then exit;
  2023. Fail(Msg+': '+s);
  2024. end;
  2025. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2026. var
  2027. aResolver: TTestEnginePasResolver;
  2028. aConverter: TPasToJSConverter;
  2029. aJSModule: TJSSourceElements;
  2030. ActualSrc: String;
  2031. begin
  2032. aResolver:=GetResolver(Filename);
  2033. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2034. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2035. {$IFDEF VerbosePas2JS}
  2036. writeln('CheckUnit '+Filename+' converting ...');
  2037. {$ENDIF}
  2038. aConverter:=CreateConverter;
  2039. aJSModule:=nil;
  2040. try
  2041. try
  2042. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2043. except
  2044. on E: Exception do
  2045. HandleException(E);
  2046. end;
  2047. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2048. {$IFDEF VerbosePas2JS}
  2049. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2050. write(aResolver.Source);
  2051. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2052. write(ActualSrc);
  2053. {$ENDIF}
  2054. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2055. finally
  2056. aJSModule.Free;
  2057. aConverter.Free;
  2058. end;
  2059. end;
  2060. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2061. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2062. var
  2063. i: Integer;
  2064. Item: TTestHintMessage;
  2065. Expected,Actual: string;
  2066. begin
  2067. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2068. for i:=0 to MsgCount-1 do
  2069. begin
  2070. Item:=Msgs[i];
  2071. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2072. if (Marker<>nil) then
  2073. begin
  2074. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2075. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2076. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2077. end;
  2078. // found
  2079. FHintMsgsGood.Add(Item);
  2080. str(Item.MsgType,Actual);
  2081. str(MsgType,Expected);
  2082. AssertEquals('MsgType',Expected,Actual);
  2083. exit;
  2084. end;
  2085. // needed message missing -> show emitted messages
  2086. WriteSources('',0,0);
  2087. for i:=0 to MsgCount-1 do
  2088. begin
  2089. Item:=Msgs[i];
  2090. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2091. ' ('+IntToStr(Item.MsgNumber),')');
  2092. if Marker<>nil then
  2093. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2094. writeln(' {',Item.Msg,'}');
  2095. end;
  2096. str(MsgType,Expected);
  2097. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2098. if Marker<>nil then
  2099. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2100. Actual:=Actual+' '+Msg;
  2101. Fail(Actual);
  2102. end;
  2103. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2104. );
  2105. var
  2106. i: Integer;
  2107. s, Txt: String;
  2108. Msg: TTestHintMessage;
  2109. begin
  2110. for i:=0 to MsgCount-1 do
  2111. begin
  2112. Msg:=Msgs[i];
  2113. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2114. s:='';
  2115. str(Msg.MsgType,s);
  2116. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2117. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2118. if WithSourcePos then
  2119. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2120. Txt:=Txt+' {'+Msg.Msg+'}';
  2121. Fail(Txt);
  2122. end;
  2123. end;
  2124. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2125. MsgNumber: integer);
  2126. begin
  2127. ExpectedErrorClass:=EScannerError;
  2128. ExpectedErrorMsg:=Msg;
  2129. ExpectedErrorNumber:=MsgNumber;
  2130. end;
  2131. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2132. MsgNumber: integer);
  2133. begin
  2134. ExpectedErrorClass:=EParserError;
  2135. ExpectedErrorMsg:=Msg;
  2136. ExpectedErrorNumber:=MsgNumber;
  2137. end;
  2138. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2139. MsgNumber: integer);
  2140. begin
  2141. ExpectedErrorClass:=EPasResolve;
  2142. ExpectedErrorMsg:=Msg;
  2143. ExpectedErrorNumber:=MsgNumber;
  2144. end;
  2145. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2146. MsgNumber: integer);
  2147. begin
  2148. ExpectedErrorClass:=EPas2JS;
  2149. ExpectedErrorMsg:=Msg;
  2150. ExpectedErrorNumber:=MsgNumber;
  2151. end;
  2152. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2153. var
  2154. MsgNumber: Integer;
  2155. Msg: String;
  2156. begin
  2157. Result:=false;
  2158. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2159. Msg:=E.Message;
  2160. if E is EPas2JS then
  2161. MsgNumber:=EPas2JS(E).MsgNumber
  2162. else if E is EPasResolve then
  2163. MsgNumber:=EPasResolve(E).MsgNumber
  2164. else if E is EParserError then
  2165. MsgNumber:=Parser.LastMsgNumber
  2166. else if E is EScannerError then
  2167. begin
  2168. MsgNumber:=Scanner.LastMsgNumber;
  2169. Msg:=Scanner.LastMsg;
  2170. end
  2171. else
  2172. MsgNumber:=0;
  2173. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2174. if Result then
  2175. SkipTests:=true;
  2176. end;
  2177. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2178. begin
  2179. if IsErrorExpected(E) then exit;
  2180. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2181. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2182. +' '+Scanner.CurFilename
  2183. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2184. FailException(E);
  2185. end;
  2186. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2187. begin
  2188. if IsErrorExpected(E) then exit;
  2189. WriteSources(E.Filename,E.Row,E.Column);
  2190. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2191. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2192. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2193. );
  2194. FailException(E);
  2195. end;
  2196. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2197. var
  2198. P: TPasSourcePos;
  2199. begin
  2200. if IsErrorExpected(E) then exit;
  2201. P:=E.SourcePos;
  2202. WriteSources(P.FileName,P.Row,P.Column);
  2203. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2204. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2205. FailException(E);
  2206. end;
  2207. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2208. var
  2209. Row, Col: integer;
  2210. begin
  2211. if IsErrorExpected(E) then exit;
  2212. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2213. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2214. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2215. +' '+E.PasElement.SourceFilename
  2216. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2217. FailException(E);
  2218. end;
  2219. procedure TCustomTestModule.HandleException(E: Exception);
  2220. begin
  2221. if E is EScannerError then
  2222. HandleScannerError(EScannerError(E))
  2223. else if E is EParserError then
  2224. HandleParserError(EParserError(E))
  2225. else if E is EPasResolve then
  2226. HandlePasResolveError(EPasResolve(E))
  2227. else if E is EPas2JS then
  2228. HandlePas2JSError(EPas2JS(E))
  2229. else
  2230. begin
  2231. if IsErrorExpected(E) then exit;
  2232. if not (E is EAssertionFailedError) then
  2233. begin
  2234. WriteSources('',0,0);
  2235. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2236. end;
  2237. FailException(E);
  2238. end;
  2239. end;
  2240. procedure TCustomTestModule.FailException(E: Exception);
  2241. var
  2242. MsgNumber: Integer;
  2243. begin
  2244. if ExpectedErrorClass<>nil then
  2245. begin
  2246. if FExpectedErrorClass=E.ClassType then
  2247. begin
  2248. if E is EPas2JS then
  2249. MsgNumber:=EPas2JS(E).MsgNumber
  2250. else if E is EPasResolve then
  2251. MsgNumber:=EPasResolve(E).MsgNumber
  2252. else if E is EParserError then
  2253. MsgNumber:=Parser.LastMsgNumber
  2254. else if E is EScannerError then
  2255. MsgNumber:=Scanner.LastMsgNumber
  2256. else
  2257. MsgNumber:=0;
  2258. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2259. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2260. ExpectedErrorNumber,MsgNumber);
  2261. end else begin
  2262. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2263. end;
  2264. end;
  2265. Fail(E.Message);
  2266. end;
  2267. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2268. aCol: integer);
  2269. var
  2270. IsSrc: Boolean;
  2271. i, j: Integer;
  2272. SrcLines: TStringList;
  2273. Line: string;
  2274. aModule: TTestEnginePasResolver;
  2275. begin
  2276. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2277. for i:=0 to ResolverCount-1 do
  2278. begin
  2279. aModule:=Resolvers[i];
  2280. SrcLines:=TStringList.Create;
  2281. try
  2282. SrcLines.Text:=aModule.Source;
  2283. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2284. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2285. for j:=1 to SrcLines.Count do
  2286. begin
  2287. Line:=SrcLines[j-1];
  2288. if IsSrc and (j=aRow) then
  2289. begin
  2290. write('*');
  2291. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2292. end;
  2293. writeln(Format('%:4d: ',[j]),Line);
  2294. end;
  2295. finally
  2296. SrcLines.Free;
  2297. end;
  2298. end;
  2299. end;
  2300. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2301. var
  2302. i: Integer;
  2303. begin
  2304. for i:=0 to ResolverCount-1 do
  2305. if Filename=Resolvers[i].Filename then exit(i);
  2306. Result:=-1;
  2307. end;
  2308. function TCustomTestModule.GetResolver(const Filename: string
  2309. ): TTestEnginePasResolver;
  2310. var
  2311. i: Integer;
  2312. begin
  2313. i:=IndexOfResolver(Filename);
  2314. if i<0 then exit(nil);
  2315. Result:=Resolvers[i];
  2316. end;
  2317. function TCustomTestModule.GetDefaultNamespace: string;
  2318. var
  2319. C: TClass;
  2320. begin
  2321. Result:='';
  2322. if FModule=nil then exit;
  2323. C:=FModule.ClassType;
  2324. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2325. Result:=Engine.DefaultNameSpace;
  2326. end;
  2327. constructor TCustomTestModule.Create;
  2328. begin
  2329. inherited Create;
  2330. FHintMsgs:=TObjectList.Create(true);
  2331. FHintMsgsGood:=TFPList.Create;
  2332. end;
  2333. destructor TCustomTestModule.Destroy;
  2334. begin
  2335. FreeAndNil(FHintMsgs);
  2336. FreeAndNil(FHintMsgsGood);
  2337. inherited Destroy;
  2338. end;
  2339. { TTestModule }
  2340. procedure TTestModule.TestReservedWords;
  2341. var
  2342. i: integer;
  2343. begin
  2344. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2345. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2346. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2347. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2348. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2349. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2350. end;
  2351. procedure TTestModule.TestEmptyProgram;
  2352. begin
  2353. StartProgram(false);
  2354. Add('begin');
  2355. ConvertProgram;
  2356. CheckSource('TestEmptyProgram','','');
  2357. end;
  2358. procedure TTestModule.TestEmptyProgramUseStrict;
  2359. begin
  2360. Converter.Options:=Converter.Options+[coUseStrict];
  2361. StartProgram(false);
  2362. Add('begin');
  2363. ConvertProgram;
  2364. CheckSource('TestEmptyProgramUseStrict','','');
  2365. end;
  2366. procedure TTestModule.TestEmptyUnit;
  2367. begin
  2368. StartUnit(false);
  2369. Add('interface');
  2370. Add('implementation');
  2371. ConvertUnit;
  2372. CheckSource('TestEmptyUnit',
  2373. LinesToStr([
  2374. ]),
  2375. '');
  2376. end;
  2377. procedure TTestModule.TestEmptyUnitUseStrict;
  2378. begin
  2379. Converter.Options:=Converter.Options+[coUseStrict];
  2380. StartUnit(false);
  2381. Add('interface');
  2382. Add('implementation');
  2383. ConvertUnit;
  2384. CheckSource('TestEmptyUnitUseStrict',
  2385. LinesToStr([
  2386. ''
  2387. ]),
  2388. '');
  2389. end;
  2390. procedure TTestModule.TestDottedUnitNames;
  2391. begin
  2392. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2393. LinesToStr([
  2394. 'var iV: longint;'
  2395. ]),
  2396. '');
  2397. FFilename:='ns1.test1.pp';
  2398. StartProgram(true);
  2399. Add('uses unIt2;');
  2400. Add('var');
  2401. Add(' i: longint;');
  2402. Add('begin');
  2403. Add(' i:=iv;');
  2404. Add(' i:=uNit2.iv;');
  2405. Add(' i:=Ns1.TEst1.i;');
  2406. ConvertProgram;
  2407. CheckSource('TestDottedUnitNames',
  2408. LinesToStr([
  2409. 'this.i = 0;',
  2410. '']),
  2411. LinesToStr([ // this.$init
  2412. '$mod.i = pas["NS1.Unit2"].iV;',
  2413. '$mod.i = pas["NS1.Unit2"].iV;',
  2414. '$mod.i = $mod.i;',
  2415. '']) );
  2416. end;
  2417. procedure TTestModule.TestDottedUnitNameImpl;
  2418. begin
  2419. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2420. LinesToStr([
  2421. 'type',
  2422. ' TObject = class end;',
  2423. ' TTestA = class',
  2424. ' end;'
  2425. ]),
  2426. LinesToStr(['uses TEST.UnitB;'])
  2427. );
  2428. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2429. LinesToStr([
  2430. 'uses TEST.UnitA;',
  2431. 'type TTestB = class(TTestA);'
  2432. ]),
  2433. ''
  2434. );
  2435. StartProgram(true);
  2436. Add('uses TEST.UnitA;');
  2437. Add('begin');
  2438. ConvertProgram;
  2439. CheckSource('TestDottedUnitNameImpl',
  2440. LinesToStr([
  2441. '']),
  2442. LinesToStr([ // this.$init
  2443. '']) );
  2444. CheckUnit('TEST.UnitA.pas',
  2445. LinesToStr([
  2446. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2447. ' var $mod = this;',
  2448. ' rtl.createClass(this, "TObject", null, function () {',
  2449. ' this.$init = function () {',
  2450. ' };',
  2451. ' this.$final = function () {',
  2452. ' };',
  2453. ' });',
  2454. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2455. ' });',
  2456. '}, ["TEST.UnitB"]);'
  2457. ]));
  2458. CheckUnit('TEST.UnitB.pas',
  2459. LinesToStr([
  2460. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2461. ' var $mod = this;',
  2462. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2463. ' });',
  2464. '});'
  2465. ]));
  2466. end;
  2467. procedure TTestModule.TestDottedUnitExpr;
  2468. begin
  2469. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2470. LinesToStr([
  2471. 'procedure DoIt;'
  2472. ]),
  2473. 'procedure DoIt; begin end;');
  2474. FFilename:='Ns1.SubNs1.Test1.pp';
  2475. StartProgram(true);
  2476. Add('uses Ns2.sUbnS2.unIt2;');
  2477. Add('var');
  2478. Add(' i: longint;');
  2479. Add('begin');
  2480. Add(' ns2.subns2.unit2.doit;');
  2481. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2482. ConvertProgram;
  2483. CheckSource('TestDottedUnitExpr',
  2484. LinesToStr([
  2485. 'this.i = 0;',
  2486. '']),
  2487. LinesToStr([ // this.$init
  2488. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2489. '$mod.i = $mod.i;',
  2490. '']) );
  2491. end;
  2492. procedure TTestModule.Test_ModeFPCFail;
  2493. begin
  2494. StartProgram(false);
  2495. Add('{$mode FPC}');
  2496. Add('begin');
  2497. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2498. ConvertProgram;
  2499. end;
  2500. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2501. begin
  2502. StartProgram(false);
  2503. Add('{$modeswitch cblocks-}');
  2504. Add('begin');
  2505. ConvertProgram;
  2506. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2507. CheckResolverUnexpectedHints();
  2508. end;
  2509. procedure TTestModule.TestUnit_UseSystem;
  2510. begin
  2511. StartUnit(true);
  2512. Add([
  2513. 'interface',
  2514. 'var i: integer;',
  2515. 'implementation']);
  2516. ConvertUnit;
  2517. CheckSource('TestUnit_UseSystem',
  2518. LinesToStr([
  2519. 'this.i = 0;',
  2520. '']),
  2521. LinesToStr([
  2522. '']) );
  2523. end;
  2524. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2525. begin
  2526. AddModuleWithIntfImplSrc('unit1.pp',
  2527. LinesToStr([
  2528. 'type number = longint;']),
  2529. LinesToStr([
  2530. 'uses test1;',
  2531. 'procedure DoIt;',
  2532. 'begin',
  2533. ' i:=3;',
  2534. 'end;']));
  2535. StartUnit(true);
  2536. Add([
  2537. 'interface',
  2538. 'uses unit1;',
  2539. 'var i: number;',
  2540. 'implementation']);
  2541. ConvertUnit;
  2542. CheckSource('TestUnit_Intf1Impl2Intf1',
  2543. LinesToStr([
  2544. 'this.i = 0;',
  2545. '']),
  2546. LinesToStr([
  2547. '']) );
  2548. end;
  2549. procedure TTestModule.TestIncludeVersion;
  2550. begin
  2551. StartProgram(false);
  2552. Add([
  2553. 'var',
  2554. ' s: string;',
  2555. ' i: word;',
  2556. 'begin',
  2557. ' s:={$I %line%};',
  2558. ' i:={$I %linenum%};',
  2559. ' s:={$I %currentroutine%};',
  2560. ' s:={$I %pas2jsversion%};',
  2561. ' s:={$I %pas2jstarget%};',
  2562. ' s:={$I %pas2jstargetos%};',
  2563. ' s:={$I %pas2jstargetcpu%};',
  2564. ' s:={$I %file%};',
  2565. '']);
  2566. ConvertProgram;
  2567. CheckSource('TestIncludeVersion',
  2568. LinesToStr([
  2569. 'this.s="";',
  2570. 'this.i = 0;']),
  2571. LinesToStr([
  2572. '$mod.s = "7";',
  2573. '$mod.i = 8;',
  2574. '$mod.s = "<anonymous>";',
  2575. '$mod.s = "Comp.Ver.tcmodules";',
  2576. '$mod.s = "Browser";',
  2577. '$mod.s = "Browser";',
  2578. '$mod.s = "ECMAScript5";',
  2579. '$mod.s = "test1.pp";',
  2580. '']));
  2581. end;
  2582. procedure TTestModule.TestVarInt;
  2583. begin
  2584. StartProgram(false);
  2585. Add('var MyI: longint;');
  2586. Add('begin');
  2587. ConvertProgram;
  2588. CheckSource('TestVarInt','this.MyI=0;','');
  2589. end;
  2590. procedure TTestModule.TestVarBaseTypes;
  2591. begin
  2592. StartProgram(false);
  2593. Add('var');
  2594. Add(' i: longint;');
  2595. Add(' s: string;');
  2596. Add(' c: char;');
  2597. Add(' b: boolean;');
  2598. Add(' d: double;');
  2599. Add(' i2: longint = 3;');
  2600. Add(' s2: string = ''foo'';');
  2601. Add(' c2: char = ''4'';');
  2602. Add(' b2: boolean = true;');
  2603. Add(' d2: double = 5.6;');
  2604. Add(' i3: longint = $707;');
  2605. Add(' i4: nativeint = 9007199254740991;');
  2606. Add(' i5: nativeint = -9007199254740991-1;');
  2607. Add(' i6: nativeint = $fffffffffffff;');
  2608. Add(' i7: nativeint = -$fffffffffffff-1;');
  2609. Add(' i8: byte = 00;');
  2610. Add(' u8: nativeuint = $fffffffffffff;');
  2611. Add(' u9: nativeuint = $0000000000000;');
  2612. Add(' u10: nativeuint = $00ff00;');
  2613. Add('begin');
  2614. ConvertProgram;
  2615. CheckSource('TestVarBaseTypes',
  2616. LinesToStr([
  2617. 'this.i = 0;',
  2618. 'this.s = "";',
  2619. 'this.c = "";',
  2620. 'this.b = false;',
  2621. 'this.d = 0.0;',
  2622. 'this.i2 = 3;',
  2623. 'this.s2 = "foo";',
  2624. 'this.c2 = "4";',
  2625. 'this.b2 = true;',
  2626. 'this.d2 = 5.6;',
  2627. 'this.i3 = 0x707;',
  2628. 'this.i4 = 9007199254740991;',
  2629. 'this.i5 = -9007199254740991-1;',
  2630. 'this.i6 = 0xfffffffffffff;',
  2631. 'this.i7 =-0xfffffffffffff-1;',
  2632. 'this.i8 = 0;',
  2633. 'this.u8 = 0xfffffffffffff;',
  2634. 'this.u9 = 0x0;',
  2635. 'this.u10 = 0xff00;'
  2636. ]),
  2637. '');
  2638. end;
  2639. procedure TTestModule.TestBaseTypeSingleFail;
  2640. begin
  2641. StartProgram(false);
  2642. Add('var s: single;');
  2643. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2644. ConvertProgram;
  2645. end;
  2646. procedure TTestModule.TestBaseTypeExtendedFail;
  2647. begin
  2648. StartProgram(false);
  2649. Add('var e: extended;');
  2650. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2651. ConvertProgram;
  2652. end;
  2653. procedure TTestModule.TestConstBaseTypes;
  2654. begin
  2655. StartProgram(false);
  2656. Add('const');
  2657. Add(' i: longint = 3;');
  2658. Add(' s: string = ''foo'';');
  2659. Add(' c: char = ''4'';');
  2660. Add(' b: boolean = true;');
  2661. Add(' d: double = 5.6;');
  2662. Add(' e = low(word);');
  2663. Add(' f = high(word);');
  2664. Add('begin');
  2665. ConvertProgram;
  2666. CheckSource('TestVarBaseTypes',
  2667. LinesToStr([
  2668. 'this.i=3;',
  2669. 'this.s="foo";',
  2670. 'this.c="4";',
  2671. 'this.b=true;',
  2672. 'this.d=5.6;',
  2673. 'this.e = 0;',
  2674. 'this.f = 65535;'
  2675. ]),
  2676. '');
  2677. end;
  2678. procedure TTestModule.TestAliasTypeRef;
  2679. begin
  2680. StartProgram(false);
  2681. Add('type');
  2682. Add(' a=longint;');
  2683. Add(' b=a;');
  2684. Add('var');
  2685. Add(' c: A;');
  2686. Add(' d: B;');
  2687. Add('begin');
  2688. ConvertProgram;
  2689. CheckSource('TestAliasTypeRef',
  2690. LinesToStr([ // statements
  2691. 'this.c = 0;',
  2692. 'this.d = 0;'
  2693. ]),
  2694. LinesToStr([ // this.$main
  2695. ''
  2696. ]));
  2697. end;
  2698. procedure TTestModule.TestTypeCast_BaseTypes;
  2699. begin
  2700. StartProgram(false);
  2701. Add([
  2702. 'var',
  2703. ' i: longint;',
  2704. ' b: boolean;',
  2705. ' d: double;',
  2706. ' s: string;',
  2707. ' c: char;',
  2708. 'begin',
  2709. ' i:=longint(i);',
  2710. ' i:=longint(b);',
  2711. ' b:=boolean(b);',
  2712. ' b:=boolean(i);',
  2713. ' d:=double(d);',
  2714. ' d:=double(i);',
  2715. ' s:=string(s);',
  2716. ' s:=string(c);',
  2717. ' c:=char(c);',
  2718. ' c:=char(i);',
  2719. ' c:=char(65);',
  2720. ' c:=char(#10);',
  2721. ' c:=char(#$E000);',
  2722. '']);
  2723. ConvertProgram;
  2724. CheckSource('TestAliasTypeRef',
  2725. LinesToStr([ // statements
  2726. 'this.i = 0;',
  2727. 'this.b = false;',
  2728. 'this.d = 0.0;',
  2729. 'this.s = "";',
  2730. 'this.c = "";',
  2731. '']),
  2732. LinesToStr([ // this.$main
  2733. '$mod.i = $mod.i;',
  2734. '$mod.i = ($mod.b ? 1 : 0);',
  2735. '$mod.b = $mod.b;',
  2736. '$mod.b = $mod.i != 0;',
  2737. '$mod.d = $mod.d;',
  2738. '$mod.d = $mod.i;',
  2739. '$mod.s = $mod.s;',
  2740. '$mod.s = $mod.c;',
  2741. '$mod.c = $mod.c;',
  2742. '$mod.c = String.fromCharCode($mod.i);',
  2743. '$mod.c = "A";',
  2744. '$mod.c = "\n";',
  2745. '$mod.c = "";',
  2746. '']));
  2747. end;
  2748. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2749. begin
  2750. StartProgram(false);
  2751. Add('type');
  2752. Add(' integer = longint;');
  2753. Add(' TYesNo = boolean;');
  2754. Add(' TFloat = double;');
  2755. Add(' TCaption = string;');
  2756. Add(' TChar = char;');
  2757. Add('var');
  2758. Add(' i: integer;');
  2759. Add(' b: TYesNo;');
  2760. Add(' d: TFloat;');
  2761. Add(' s: TCaption;');
  2762. Add(' c: TChar;');
  2763. Add('begin');
  2764. Add(' i:=integer(i);');
  2765. Add(' i:=integer(b);');
  2766. Add(' b:=TYesNo(b);');
  2767. Add(' b:=TYesNo(i);');
  2768. Add(' d:=TFloat(d);');
  2769. Add(' d:=TFloat(i);');
  2770. Add(' s:=TCaption(s);');
  2771. Add(' s:=TCaption(c);');
  2772. Add(' c:=TChar(c);');
  2773. ConvertProgram;
  2774. CheckSource('TestAliasTypeRef',
  2775. LinesToStr([ // statements
  2776. 'this.i = 0;',
  2777. 'this.b = false;',
  2778. 'this.d = 0.0;',
  2779. 'this.s = "";',
  2780. 'this.c = "";',
  2781. '']),
  2782. LinesToStr([ // this.$main
  2783. '$mod.i = $mod.i;',
  2784. '$mod.i = ($mod.b ? 1 : 0);',
  2785. '$mod.b = $mod.b;',
  2786. '$mod.b = $mod.i != 0;',
  2787. '$mod.d = $mod.d;',
  2788. '$mod.d = $mod.i;',
  2789. '$mod.s = $mod.s;',
  2790. '$mod.s = $mod.c;',
  2791. '$mod.c = $mod.c;',
  2792. '']));
  2793. end;
  2794. procedure TTestModule.TestEmptyProc;
  2795. begin
  2796. StartProgram(false);
  2797. Add('procedure Test;');
  2798. Add('begin');
  2799. Add('end;');
  2800. Add('begin');
  2801. ConvertProgram;
  2802. CheckSource('TestEmptyProc',
  2803. LinesToStr([ // statements
  2804. 'this.Test = function () {',
  2805. '};'
  2806. ]),
  2807. LinesToStr([ // this.$main
  2808. ''
  2809. ]));
  2810. end;
  2811. procedure TTestModule.TestProcOneParam;
  2812. begin
  2813. StartProgram(false);
  2814. Add('procedure ProcA(i: longint);');
  2815. Add('begin');
  2816. Add('end;');
  2817. Add('begin');
  2818. Add(' PROCA(3);');
  2819. ConvertProgram;
  2820. CheckSource('TestProcOneParam',
  2821. LinesToStr([ // statements
  2822. 'this.ProcA = function (i) {',
  2823. '};'
  2824. ]),
  2825. LinesToStr([ // this.$main
  2826. '$mod.ProcA(3);'
  2827. ]));
  2828. end;
  2829. procedure TTestModule.TestFunctionWithoutParams;
  2830. begin
  2831. StartProgram(false);
  2832. Add('function FuncA: longint;');
  2833. Add('begin');
  2834. Add('end;');
  2835. Add('var i: longint;');
  2836. Add('begin');
  2837. Add(' I:=FUNCA();');
  2838. Add(' I:=FUNCA;');
  2839. Add(' FUNCA();');
  2840. Add(' FUNCA;');
  2841. ConvertProgram;
  2842. CheckSource('TestProcWithoutParams',
  2843. LinesToStr([ // statements
  2844. 'this.FuncA = function () {',
  2845. ' var Result = 0;',
  2846. ' return Result;',
  2847. '};',
  2848. 'this.i=0;'
  2849. ]),
  2850. LinesToStr([ // this.$main
  2851. '$mod.i=$mod.FuncA();',
  2852. '$mod.i=$mod.FuncA();',
  2853. '$mod.FuncA();',
  2854. '$mod.FuncA();'
  2855. ]));
  2856. end;
  2857. procedure TTestModule.TestProcedureWithoutParams;
  2858. begin
  2859. StartProgram(false);
  2860. Add('procedure ProcA;');
  2861. Add('begin');
  2862. Add('end;');
  2863. Add('begin');
  2864. Add(' PROCA();');
  2865. Add(' PROCA;');
  2866. ConvertProgram;
  2867. CheckSource('TestProcWithoutParams',
  2868. LinesToStr([ // statements
  2869. 'this.ProcA = function () {',
  2870. '};'
  2871. ]),
  2872. LinesToStr([ // this.$main
  2873. '$mod.ProcA();',
  2874. '$mod.ProcA();'
  2875. ]));
  2876. end;
  2877. procedure TTestModule.TestIncDec;
  2878. begin
  2879. StartProgram(false);
  2880. Add([
  2881. 'procedure DoIt(var i: longint);',
  2882. 'begin',
  2883. ' inc(i);',
  2884. ' inc(i,2);',
  2885. 'end;',
  2886. 'var',
  2887. ' Bar: longint;',
  2888. 'begin',
  2889. ' inc(bar);',
  2890. ' inc(bar,2);',
  2891. ' dec(bar);',
  2892. ' dec(bar,3);',
  2893. '']);
  2894. ConvertProgram;
  2895. CheckSource('TestIncDec',
  2896. LinesToStr([ // statements
  2897. 'this.DoIt = function (i) {',
  2898. ' i.set(i.get()+1);',
  2899. ' i.set(i.get()+2);',
  2900. '};',
  2901. 'this.Bar = 0;'
  2902. ]),
  2903. LinesToStr([ // this.$main
  2904. '$mod.Bar+=1;',
  2905. '$mod.Bar+=2;',
  2906. '$mod.Bar-=1;',
  2907. '$mod.Bar-=3;'
  2908. ]));
  2909. end;
  2910. procedure TTestModule.TestLoHiFpcMode;
  2911. begin
  2912. StartProgram(false);
  2913. Add([
  2914. '{$mode objfpc}',
  2915. 'const',
  2916. ' LoByte1 = Lo(Word($1234));',
  2917. ' HiByte1 = Hi(Word($1234));',
  2918. ' LoByte2 = Lo(SmallInt($1234));',
  2919. ' HiByte2 = Hi(SmallInt($1234));',
  2920. ' LoWord1 = Lo($1234CDEF);',
  2921. ' HiWord1 = Hi($1234CDEF);',
  2922. ' LoWord2 = Lo(-$1234CDEF);',
  2923. ' HiWord2 = Hi(-$1234CDEF);',
  2924. ' lo4:byte=lo(byte($34));',
  2925. ' hi4:byte=hi(byte($34));',
  2926. ' lo5:byte=lo(shortint(-$34));',
  2927. ' hi5:byte=hi(shortint(-$34));',
  2928. ' lo6:longword=lo($123456789ABCD);',
  2929. ' hi6:longword=hi($123456789ABCD);',
  2930. ' lo7:longword=lo(-$123456789ABCD);',
  2931. ' hi7:longword=hi(-$123456789ABCD);',
  2932. 'var',
  2933. ' b: Byte;',
  2934. ' ss: shortint;',
  2935. ' w: Word;',
  2936. ' si: SmallInt;',
  2937. ' lw: LongWord;',
  2938. ' li: LongInt;',
  2939. ' b2: Byte;',
  2940. ' ni: nativeint;',
  2941. 'begin',
  2942. ' w := $1234;',
  2943. ' ss := -$12;',
  2944. ' b := lo(ss);',
  2945. ' b := HI(ss);',
  2946. ' b := lo(w);',
  2947. ' b := HI(w);',
  2948. ' b2 := lo(b);',
  2949. ' b2 := hi(b);',
  2950. ' lw := $1234CDEF;',
  2951. ' w := lo(lw);',
  2952. ' w := hi(lw);',
  2953. ' ni := $123456789ABCD;',
  2954. ' lw := lo(ni);',
  2955. ' lw := hi(ni);',
  2956. '']);
  2957. ConvertProgram;
  2958. CheckSource('TestLoHiFpcMode',
  2959. LinesToStr([ // statements
  2960. 'this.LoByte1 = 0x1234 & 0xFF;',
  2961. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2962. 'this.LoByte2 = 0x1234 & 0xFF;',
  2963. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2964. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2965. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2966. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2967. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2968. 'this.lo4 = 0x34 & 0xF;',
  2969. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2970. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2971. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2972. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2973. 'this.hi6 = 74565 >>> 0;',
  2974. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2975. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2976. 'this.b = 0;',
  2977. 'this.ss = 0;',
  2978. 'this.w = 0;',
  2979. 'this.si = 0;',
  2980. 'this.lw = 0;',
  2981. 'this.li = 0;',
  2982. 'this.b2 = 0;',
  2983. 'this.ni = 0;',
  2984. '']),
  2985. LinesToStr([ // this.$main
  2986. '$mod.w = 0x1234;',
  2987. '$mod.ss = -0x12;',
  2988. '$mod.b = $mod.ss & 0xFF;',
  2989. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2990. '$mod.b = $mod.w & 0xFF;',
  2991. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2992. '$mod.b2 = $mod.b & 0xF;',
  2993. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2994. '$mod.lw = 0x1234CDEF;',
  2995. '$mod.w = $mod.lw & 0xFFFF;',
  2996. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2997. '$mod.ni = 0x123456789ABCD;',
  2998. '$mod.lw = $mod.ni >>> 0;',
  2999. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3000. '']));
  3001. end;
  3002. procedure TTestModule.TestLoHiDelphiMode;
  3003. begin
  3004. StartProgram(false);
  3005. Add([
  3006. '{$mode delphi}',
  3007. 'const',
  3008. ' LoByte1 = Lo(Word($1234));',
  3009. ' HiByte1 = Hi(Word($1234));',
  3010. ' LoByte2 = Lo(SmallInt($1234));',
  3011. ' HiByte2 = Hi(SmallInt($1234));',
  3012. ' LoByte3 = Lo($1234CDEF);',
  3013. ' HiByte3 = Hi($1234CDEF);',
  3014. ' LoByte4 = Lo(-$1234CDEF);',
  3015. ' HiByte4 = Hi(-$1234CDEF);',
  3016. 'var',
  3017. ' b: Byte;',
  3018. ' w: Word;',
  3019. ' si: SmallInt;',
  3020. ' lw: LongWord;',
  3021. ' li: LongInt;',
  3022. 'begin',
  3023. ' w := $1234;',
  3024. ' b := lo(w);',
  3025. ' b := HI(w);',
  3026. ' lw := $1234CDEF;',
  3027. ' b := lo(lw);',
  3028. ' b := hi(lw);',
  3029. '']);
  3030. ConvertProgram;
  3031. CheckSource('TestLoHiDelphiMode',
  3032. LinesToStr([ // statements
  3033. 'this.LoByte1 = 0x1234 & 0xFF;',
  3034. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3035. 'this.LoByte2 = 0x1234 & 0xFF;',
  3036. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3037. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3038. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3039. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3040. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3041. 'this.b = 0;',
  3042. 'this.w = 0;',
  3043. 'this.si = 0;',
  3044. 'this.lw = 0;',
  3045. 'this.li = 0;'
  3046. ]),
  3047. LinesToStr([ // this.$main
  3048. '$mod.w = 0x1234;',
  3049. '$mod.b = $mod.w & 0xFF;',
  3050. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3051. '$mod.lw = 0x1234CDEF;',
  3052. '$mod.b = $mod.lw & 0xFF;',
  3053. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3054. ]));
  3055. end;
  3056. procedure TTestModule.TestAssignments;
  3057. begin
  3058. StartProgram(false);
  3059. Parser.Options:=Parser.Options+[po_cassignments];
  3060. Add('var');
  3061. Add(' Bar:longint;');
  3062. Add('begin');
  3063. Add(' bar:=3;');
  3064. Add(' bar+=4;');
  3065. Add(' bar-=5;');
  3066. Add(' bar*=6;');
  3067. ConvertProgram;
  3068. CheckSource('TestAssignments',
  3069. LinesToStr([ // statements
  3070. 'this.Bar = 0;'
  3071. ]),
  3072. LinesToStr([ // this.$main
  3073. '$mod.Bar=3;',
  3074. '$mod.Bar+=4;',
  3075. '$mod.Bar-=5;',
  3076. '$mod.Bar*=6;'
  3077. ]));
  3078. end;
  3079. procedure TTestModule.TestArithmeticOperators1;
  3080. begin
  3081. StartProgram(false);
  3082. Add('var');
  3083. Add(' vA,vB,vC:longint;');
  3084. Add('begin');
  3085. Add(' va:=1;');
  3086. Add(' vb:=va+va;');
  3087. Add(' vb:=va div vb;');
  3088. Add(' vb:=va mod vb;');
  3089. Add(' vb:=va+va*vb+va div vb;');
  3090. Add(' vc:=-va;');
  3091. Add(' va:=va-vb;');
  3092. Add(' vb:=va;');
  3093. Add(' if va<vb then vc:=va else vc:=vb;');
  3094. ConvertProgram;
  3095. CheckSource('TestArithmeticOperators1',
  3096. LinesToStr([ // statements
  3097. 'this.vA = 0;',
  3098. 'this.vB = 0;',
  3099. 'this.vC = 0;'
  3100. ]),
  3101. LinesToStr([ // this.$main
  3102. '$mod.vA = 1;',
  3103. '$mod.vB = $mod.vA + $mod.vA;',
  3104. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3105. '$mod.vB = $mod.vA % $mod.vB;',
  3106. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3107. '$mod.vC = -$mod.vA;',
  3108. '$mod.vA = $mod.vA - $mod.vB;',
  3109. '$mod.vB = $mod.vA;',
  3110. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3111. ]));
  3112. end;
  3113. procedure TTestModule.TestLogicalOperators;
  3114. begin
  3115. StartProgram(false);
  3116. Add('var');
  3117. Add(' vA,vB,vC:boolean;');
  3118. Add('begin');
  3119. Add(' va:=vb and vc;');
  3120. Add(' va:=vb or vc;');
  3121. Add(' va:=vb xor vc;');
  3122. Add(' va:=true and vc;');
  3123. Add(' va:=(vb and vc) or (va and vb);');
  3124. Add(' va:=not vb;');
  3125. ConvertProgram;
  3126. CheckSource('TestLogicalOperators',
  3127. LinesToStr([ // statements
  3128. 'this.vA = false;',
  3129. 'this.vB = false;',
  3130. 'this.vC = false;'
  3131. ]),
  3132. LinesToStr([ // this.$main
  3133. '$mod.vA = $mod.vB && $mod.vC;',
  3134. '$mod.vA = $mod.vB || $mod.vC;',
  3135. '$mod.vA = $mod.vB ^ $mod.vC;',
  3136. '$mod.vA = true && $mod.vC;',
  3137. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3138. '$mod.vA = !$mod.vB;'
  3139. ]));
  3140. end;
  3141. procedure TTestModule.TestBitwiseOperators;
  3142. begin
  3143. StartProgram(false);
  3144. Add([
  3145. 'var',
  3146. ' vA,vB,vC:longint;',
  3147. ' X,Y,Z: nativeint;',
  3148. 'begin',
  3149. ' va:=vb and vc;',
  3150. ' va:=vb or vc;',
  3151. ' va:=vb xor vc;',
  3152. ' va:=vb shl vc;',
  3153. ' va:=vb shr vc;',
  3154. ' va:=3 and vc;',
  3155. ' va:=(vb and vc) or (va and vb);',
  3156. ' va:=not vb;',
  3157. ' X:=Y and Z;',
  3158. ' X:=Y and va;',
  3159. ' X:=Y or Z;',
  3160. ' X:=Y or va;',
  3161. ' X:=Y xor Z;',
  3162. ' X:=Y xor va;',
  3163. '']);
  3164. ConvertProgram;
  3165. CheckSource('TestBitwiseOperators',
  3166. LinesToStr([ // statements
  3167. 'this.vA = 0;',
  3168. 'this.vB = 0;',
  3169. 'this.vC = 0;',
  3170. 'this.X = 0;',
  3171. 'this.Y = 0;',
  3172. 'this.Z = 0;',
  3173. '']),
  3174. LinesToStr([ // this.$main
  3175. '$mod.vA = $mod.vB & $mod.vC;',
  3176. '$mod.vA = $mod.vB | $mod.vC;',
  3177. '$mod.vA = $mod.vB ^ $mod.vC;',
  3178. '$mod.vA = $mod.vB << $mod.vC;',
  3179. '$mod.vA = $mod.vB >>> $mod.vC;',
  3180. '$mod.vA = 3 & $mod.vC;',
  3181. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3182. '$mod.vA = ~$mod.vB;',
  3183. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3184. '$mod.X = $mod.Y & $mod.vA;',
  3185. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3186. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3187. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3188. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3189. '']));
  3190. end;
  3191. procedure TTestModule.TestBitwiseOperatorsLongword;
  3192. begin
  3193. StartProgram(false);
  3194. Add([
  3195. 'var',
  3196. ' a,b,c:longword;',
  3197. ' i: longint;',
  3198. 'begin',
  3199. ' a:=$12345678;',
  3200. ' b:=$EDCBA987;',
  3201. ' c:=not a;',
  3202. ' c:=a and b;',
  3203. ' c:=a and $ffff0000;',
  3204. ' c:=a or b;',
  3205. ' c:=a or $ff00ff00;',
  3206. ' c:=a xor b;',
  3207. ' c:=a xor $f0f0f0f0;',
  3208. ' c:=a shl 1;',
  3209. ' c:=a shl 16;',
  3210. ' c:=a shl 24;',
  3211. ' c:=a shl b;',
  3212. ' c:=a shr 1;',
  3213. ' c:=a shr 16;',
  3214. ' c:=a shr 24;',
  3215. ' c:=a shr b;',
  3216. ' c:=(b and c) or (a and b);',
  3217. ' c:=i and a;',
  3218. ' c:=i or a;',
  3219. ' c:=i xor a;',
  3220. '']);
  3221. ConvertProgram;
  3222. CheckSource('TestBitwiseOperatorsLongword',
  3223. LinesToStr([ // statements
  3224. 'this.a = 0;',
  3225. 'this.b = 0;',
  3226. 'this.c = 0;',
  3227. 'this.i = 0;',
  3228. '']),
  3229. LinesToStr([ // this.$main
  3230. '$mod.a = 0x12345678;',
  3231. '$mod.b = 0xEDCBA987;',
  3232. '$mod.c = rtl.lw(~$mod.a);',
  3233. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3234. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3235. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3236. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3237. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3238. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3239. '$mod.c = rtl.lw($mod.a << 1);',
  3240. '$mod.c = rtl.lw($mod.a << 16);',
  3241. '$mod.c = rtl.lw($mod.a << 24);',
  3242. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3243. '$mod.c = rtl.lw($mod.a >>> 1);',
  3244. '$mod.c = rtl.lw($mod.a >>> 16);',
  3245. '$mod.c = rtl.lw($mod.a >>> 24);',
  3246. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3247. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3248. '$mod.c = $mod.i & $mod.a;',
  3249. '$mod.c = $mod.i | $mod.a;',
  3250. '$mod.c = $mod.i ^ $mod.a;',
  3251. '']));
  3252. end;
  3253. procedure TTestModule.TestPrgProcVar;
  3254. begin
  3255. StartProgram(false);
  3256. Add('procedure Proc1;');
  3257. Add('type');
  3258. Add(' t1=longint;');
  3259. Add('var');
  3260. Add(' vA:t1;');
  3261. Add('begin');
  3262. Add('end;');
  3263. Add('begin');
  3264. ConvertProgram;
  3265. CheckSource('TestPrgProcVar',
  3266. LinesToStr([ // statements
  3267. 'this.Proc1 = function () {',
  3268. ' var vA=0;',
  3269. '};'
  3270. ]),
  3271. LinesToStr([ // this.$main
  3272. ''
  3273. ]));
  3274. end;
  3275. procedure TTestModule.TestUnitProcVar;
  3276. begin
  3277. StartUnit(false);
  3278. Add('interface');
  3279. Add('');
  3280. Add('type tA=string; // unit scope');
  3281. Add('procedure Proc1;');
  3282. Add('');
  3283. Add('implementation');
  3284. Add('');
  3285. Add('procedure Proc1;');
  3286. Add('type tA=longint; // local proc scope');
  3287. Add('var v1:tA; // using local tA');
  3288. Add('begin');
  3289. Add('end;');
  3290. Add('var v2:tA; // using interface tA');
  3291. ConvertUnit;
  3292. CheckSource('TestUnitProcVar',
  3293. LinesToStr([ // statements
  3294. 'var $impl = $mod.$impl;',
  3295. 'this.Proc1 = function () {',
  3296. ' var v1 = 0;',
  3297. '};',
  3298. '']),
  3299. // this.$init
  3300. '',
  3301. // implementation
  3302. LinesToStr([
  3303. '$impl.v2 = "";',
  3304. '']));
  3305. end;
  3306. procedure TTestModule.TestImplProc;
  3307. begin
  3308. StartUnit(false);
  3309. Add('interface');
  3310. Add('');
  3311. Add('procedure Proc1;');
  3312. Add('');
  3313. Add('implementation');
  3314. Add('');
  3315. Add('procedure Proc1; begin end;');
  3316. Add('procedure Proc2; begin end;');
  3317. Add('initialization');
  3318. Add(' Proc1;');
  3319. Add(' Proc2;');
  3320. ConvertUnit;
  3321. CheckSource('TestImplProc',
  3322. LinesToStr([ // statements
  3323. 'var $impl = $mod.$impl;',
  3324. 'this.Proc1 = function () {',
  3325. '};',
  3326. '']),
  3327. LinesToStr([ // this.$init
  3328. '$mod.Proc1();',
  3329. '$impl.Proc2();',
  3330. '']),
  3331. LinesToStr([ // implementation
  3332. '$impl.Proc2 = function () {',
  3333. '};',
  3334. ''])
  3335. );
  3336. end;
  3337. procedure TTestModule.TestFunctionResult;
  3338. begin
  3339. StartProgram(false);
  3340. Add('function Func1: longint;');
  3341. Add('begin');
  3342. Add(' Result:=3;');
  3343. Add(' Func1:=4;');
  3344. Add('end;');
  3345. Add('begin');
  3346. ConvertProgram;
  3347. CheckSource('TestFunctionResult',
  3348. LinesToStr([ // statements
  3349. 'this.Func1 = function () {',
  3350. ' var Result = 0;',
  3351. ' Result = 3;',
  3352. ' Result = 4;',
  3353. ' return Result;',
  3354. '};'
  3355. ]),
  3356. '');
  3357. end;
  3358. procedure TTestModule.TestNestedProc;
  3359. begin
  3360. StartProgram(false);
  3361. Add([
  3362. 'var vInUnit: longint;',
  3363. 'function DoIt(pA,pD: longint): longint;',
  3364. 'var',
  3365. ' vB: longint;',
  3366. ' vC: longint;',
  3367. ' function Nesty(pA: longint): longint; ',
  3368. ' var vB: longint;',
  3369. ' begin',
  3370. ' Result:=pa+vb+vc+pd+vInUnit;',
  3371. ' nesty:=3;',
  3372. ' doit:=4;',
  3373. ' exit;',
  3374. ' end;',
  3375. 'begin',
  3376. ' Result:=pa+vb+vc;',
  3377. ' doit:=6;',
  3378. ' exit;',
  3379. 'end;',
  3380. 'begin']);
  3381. ConvertProgram;
  3382. CheckSource('TestNestedProc',
  3383. LinesToStr([ // statements
  3384. 'this.vInUnit = 0;',
  3385. 'this.DoIt = function (pA, pD) {',
  3386. ' var Result = 0;',
  3387. ' var vB = 0;',
  3388. ' var vC = 0;',
  3389. ' function Nesty(pA) {',
  3390. ' var Result$1 = 0;',
  3391. ' var vB = 0;',
  3392. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3393. ' Result$1 = 3;',
  3394. ' Result = 4;',
  3395. ' return Result$1;',
  3396. ' return Result$1;',
  3397. ' };',
  3398. ' Result = pA + vB + vC;',
  3399. ' Result = 6;',
  3400. ' return Result;',
  3401. ' return Result;',
  3402. '};'
  3403. ]),
  3404. '');
  3405. end;
  3406. procedure TTestModule.TestNestedProc_ResultString;
  3407. begin
  3408. StartProgram(false);
  3409. Add([
  3410. 'function DoIt: string;',
  3411. ' function Nesty: string; ',
  3412. ' begin',
  3413. ' nesty:=#65#66;',
  3414. ' nesty[1]:=#67;',
  3415. ' doit:=#68;',
  3416. ' doit[2]:=#69;',
  3417. ' end;',
  3418. 'begin',
  3419. ' doit:=#70;',
  3420. ' doit[3]:=#71;',
  3421. 'end;',
  3422. 'begin']);
  3423. ConvertProgram;
  3424. CheckSource('TestNestedProc_ResultString',
  3425. LinesToStr([ // statements
  3426. 'this.DoIt = function () {',
  3427. ' var Result = "";',
  3428. ' function Nesty() {',
  3429. ' var Result$1 = "";',
  3430. ' Result$1 = "AB";',
  3431. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3432. ' Result = "D";',
  3433. ' Result = rtl.setCharAt(Result, 1, "E");',
  3434. ' return Result$1;',
  3435. ' };',
  3436. ' Result = "F";',
  3437. ' Result = rtl.setCharAt(Result, 2, "G");',
  3438. ' return Result;',
  3439. '};'
  3440. ]),
  3441. '');
  3442. end;
  3443. procedure TTestModule.TestForwardProc;
  3444. begin
  3445. StartProgram(false);
  3446. Add('procedure FuncA(Bar: longint); forward;');
  3447. Add('procedure FuncB(Bar: longint);');
  3448. Add('begin');
  3449. Add(' funca(bar);');
  3450. Add('end;');
  3451. Add('procedure funca(bar: longint);');
  3452. Add('begin');
  3453. Add(' if bar=3 then ;');
  3454. Add('end;');
  3455. Add('begin');
  3456. Add(' funca(4);');
  3457. Add(' funcb(5);');
  3458. ConvertProgram;
  3459. CheckSource('TestForwardProc',
  3460. LinesToStr([ // statements'
  3461. 'this.FuncB = function (Bar) {',
  3462. ' $mod.FuncA(Bar);',
  3463. '};',
  3464. 'this.FuncA = function (Bar) {',
  3465. ' if (Bar === 3);',
  3466. '};'
  3467. ]),
  3468. LinesToStr([
  3469. '$mod.FuncA(4);',
  3470. '$mod.FuncB(5);'
  3471. ])
  3472. );
  3473. end;
  3474. procedure TTestModule.TestNestedForwardProc;
  3475. begin
  3476. StartProgram(false);
  3477. Add('procedure FuncA;');
  3478. Add(' procedure FuncB(i: longint); forward;');
  3479. Add(' procedure FuncC(i: longint);');
  3480. Add(' begin');
  3481. Add(' funcb(i);');
  3482. Add(' end;');
  3483. Add(' procedure FuncB(i: longint);');
  3484. Add(' begin');
  3485. Add(' if i=3 then ;');
  3486. Add(' end;');
  3487. Add('begin');
  3488. Add(' funcc(4)');
  3489. Add('end;');
  3490. Add('begin');
  3491. Add(' funca;');
  3492. ConvertProgram;
  3493. CheckSource('TestNestedForwardProc',
  3494. LinesToStr([ // statements'
  3495. 'this.FuncA = function () {',
  3496. ' function FuncC(i) {',
  3497. ' FuncB(i);',
  3498. ' };',
  3499. ' function FuncB(i) {',
  3500. ' if (i === 3);',
  3501. ' };',
  3502. ' FuncC(4);',
  3503. '};'
  3504. ]),
  3505. LinesToStr([
  3506. '$mod.FuncA();'
  3507. ])
  3508. );
  3509. end;
  3510. procedure TTestModule.TestAssignFunctionResult;
  3511. begin
  3512. StartProgram(false);
  3513. Add('function Func1: longint;');
  3514. Add('begin');
  3515. Add('end;');
  3516. Add('var i: longint;');
  3517. Add('begin');
  3518. Add(' i:=func1();');
  3519. Add(' i:=func1()+func1();');
  3520. ConvertProgram;
  3521. CheckSource('TestAssignFunctionResult',
  3522. LinesToStr([ // statements
  3523. 'this.Func1 = function () {',
  3524. ' var Result = 0;',
  3525. ' return Result;',
  3526. '};',
  3527. 'this.i = 0;'
  3528. ]),
  3529. LinesToStr([
  3530. '$mod.i = $mod.Func1();',
  3531. '$mod.i = $mod.Func1() + $mod.Func1();'
  3532. ]));
  3533. end;
  3534. procedure TTestModule.TestFunctionResultInCondition;
  3535. begin
  3536. StartProgram(false);
  3537. Add('function Func1: longint;');
  3538. Add('begin');
  3539. Add('end;');
  3540. Add('function Func2: boolean;');
  3541. Add('begin');
  3542. Add('end;');
  3543. Add('var i: longint;');
  3544. Add('begin');
  3545. Add(' if func2 then ;');
  3546. Add(' if i=func1() then ;');
  3547. Add(' if i=func1 then ;');
  3548. ConvertProgram;
  3549. CheckSource('TestFunctionResultInCondition',
  3550. LinesToStr([ // statements
  3551. 'this.Func1 = function () {',
  3552. ' var Result = 0;',
  3553. ' return Result;',
  3554. '};',
  3555. 'this.Func2 = function () {',
  3556. ' var Result = false;',
  3557. ' return Result;',
  3558. '};',
  3559. 'this.i = 0;'
  3560. ]),
  3561. LinesToStr([
  3562. 'if ($mod.Func2());',
  3563. 'if ($mod.i === $mod.Func1());',
  3564. 'if ($mod.i === $mod.Func1());'
  3565. ]));
  3566. end;
  3567. procedure TTestModule.TestFunctionResultInForLoop;
  3568. begin
  3569. StartProgram(false);
  3570. Add([
  3571. 'function Func1(a: array of longint): longint;',
  3572. 'begin',
  3573. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3574. ' for Result in a do if a[Result]=0 then exit;',
  3575. 'end;',
  3576. 'begin',
  3577. ' Func1([1,2,3])']);
  3578. ConvertProgram;
  3579. CheckSource('TestFunctionResultInForLoop',
  3580. LinesToStr([ // statements
  3581. 'this.Func1 = function (a) {',
  3582. ' var Result = 0;',
  3583. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3584. ' Result = $l;',
  3585. ' if (a[Result] === 0) return Result;',
  3586. ' };',
  3587. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3588. ' Result = $in[$l1];',
  3589. ' if (a[Result] === 0) return Result;',
  3590. ' };',
  3591. ' return Result;',
  3592. '};',
  3593. '']),
  3594. LinesToStr([
  3595. '$mod.Func1([1, 2, 3]);'
  3596. ]));
  3597. end;
  3598. procedure TTestModule.TestFunctionResultInTypeCast;
  3599. begin
  3600. StartProgram(false);
  3601. Add([
  3602. 'function GetInt: longint;',
  3603. 'begin',
  3604. 'end;',
  3605. 'begin',
  3606. ' if Byte(GetInt)=0 then ;',
  3607. '']);
  3608. ConvertProgram;
  3609. CheckSource('TestFunctionResultInTypeCast',
  3610. LinesToStr([ // statements
  3611. 'this.GetInt = function () {',
  3612. ' var Result = 0;',
  3613. ' return Result;',
  3614. '};',
  3615. '']),
  3616. LinesToStr([
  3617. 'if (($mod.GetInt() & 255) === 0) ;'
  3618. ]));
  3619. end;
  3620. procedure TTestModule.TestExit;
  3621. begin
  3622. StartProgram(false);
  3623. Add('procedure ProcA;');
  3624. Add('begin');
  3625. Add(' exit;');
  3626. Add('end;');
  3627. Add('function FuncB: longint;');
  3628. Add('begin');
  3629. Add(' exit;');
  3630. Add(' exit(3);');
  3631. Add('end;');
  3632. Add('function FuncC: string;');
  3633. Add('begin');
  3634. Add(' exit;');
  3635. Add(' exit(''a'');');
  3636. Add(' exit(''abc'');');
  3637. Add('end;');
  3638. Add('begin');
  3639. Add(' exit;');
  3640. Add(' exit(1);');
  3641. ConvertProgram;
  3642. CheckSource('TestExit',
  3643. LinesToStr([ // statements
  3644. 'this.ProcA = function () {',
  3645. ' return;',
  3646. '};',
  3647. 'this.FuncB = function () {',
  3648. ' var Result = 0;',
  3649. ' return Result;',
  3650. ' return 3;',
  3651. ' return Result;',
  3652. '};',
  3653. 'this.FuncC = function () {',
  3654. ' var Result = "";',
  3655. ' return Result;',
  3656. ' return "a";',
  3657. ' return "abc";',
  3658. ' return Result;',
  3659. '};'
  3660. ]),
  3661. LinesToStr([
  3662. 'return;',
  3663. 'return 1;',
  3664. '']));
  3665. end;
  3666. procedure TTestModule.TestExit_ResultInFinally;
  3667. begin
  3668. StartProgram(false);
  3669. Add([
  3670. 'function Run: word;',
  3671. 'begin',
  3672. ' try',
  3673. ' exit(3);', // no Result in finally -> use return 3
  3674. ' finally',
  3675. ' end;',
  3676. 'end;',
  3677. 'function Fly: word;',
  3678. 'begin',
  3679. ' try',
  3680. ' exit(3);',
  3681. ' finally',
  3682. ' if Result>0 then ;',
  3683. ' end;',
  3684. 'end;',
  3685. 'function Jump: word;',
  3686. 'begin',
  3687. ' try',
  3688. ' try',
  3689. ' exit(4);',
  3690. ' finally',
  3691. ' end;',
  3692. ' finally',
  3693. ' if Result>0 then ;',
  3694. ' end;',
  3695. 'end;',
  3696. 'begin',
  3697. '']);
  3698. ConvertProgram;
  3699. CheckSource('TestExit_ResultInFinally',
  3700. LinesToStr([ // statements
  3701. 'this.Run = function () {',
  3702. ' var Result = 0;',
  3703. ' try {',
  3704. ' return 3;',
  3705. ' } finally {',
  3706. ' };',
  3707. ' return Result;',
  3708. '};',
  3709. 'this.Fly = function () {',
  3710. ' var Result = 0;',
  3711. ' try {',
  3712. ' Result = 3;',
  3713. ' return Result;',
  3714. ' } finally {',
  3715. ' if (Result > 0) ;',
  3716. ' };',
  3717. ' return Result;',
  3718. '};',
  3719. 'this.Jump = function () {',
  3720. ' var Result = 0;',
  3721. ' try {',
  3722. ' try {',
  3723. ' Result = 4;',
  3724. ' return Result;',
  3725. ' } finally {',
  3726. ' };',
  3727. ' } finally {',
  3728. ' if (Result > 0) ;',
  3729. ' };',
  3730. ' return Result;',
  3731. '};',
  3732. '']),
  3733. LinesToStr([
  3734. '']));
  3735. end;
  3736. procedure TTestModule.TestBreak;
  3737. begin
  3738. StartProgram(false);
  3739. Add([
  3740. 'var',
  3741. ' i: longint;',
  3742. 'begin',
  3743. ' repeat',
  3744. ' break;',
  3745. ' until true;',
  3746. ' while true do',
  3747. ' break;',
  3748. ' for i:=1 to 2 do',
  3749. ' break;']);
  3750. ConvertProgram;
  3751. CheckSource('TestBreak',
  3752. LinesToStr([ // statements
  3753. 'this.i = 0;'
  3754. ]),
  3755. LinesToStr([
  3756. 'do {',
  3757. ' break;',
  3758. '} while (!true);',
  3759. 'while (true) break;',
  3760. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3761. '']));
  3762. end;
  3763. procedure TTestModule.TestBreakAsVar;
  3764. begin
  3765. StartProgram(false);
  3766. Add([
  3767. 'procedure DoIt(break: boolean);',
  3768. 'begin',
  3769. ' if break then ;',
  3770. 'end;',
  3771. 'var',
  3772. ' break: boolean;',
  3773. 'begin',
  3774. ' if break then ;']);
  3775. ConvertProgram;
  3776. CheckSource('TestBreakAsVar',
  3777. LinesToStr([ // statements
  3778. 'this.DoIt = function (Break) {',
  3779. ' if (Break) ;',
  3780. '};',
  3781. 'this.Break = false;',
  3782. '']),
  3783. LinesToStr([
  3784. 'if($mod.Break) ;',
  3785. '']));
  3786. end;
  3787. procedure TTestModule.TestContinue;
  3788. begin
  3789. StartProgram(false);
  3790. Add('var i: longint;');
  3791. Add('begin');
  3792. Add(' repeat');
  3793. Add(' continue;');
  3794. Add(' until true;');
  3795. Add(' while true do');
  3796. Add(' continue;');
  3797. Add(' for i:=1 to 2 do');
  3798. Add(' continue;');
  3799. ConvertProgram;
  3800. CheckSource('TestContinue',
  3801. LinesToStr([ // statements
  3802. 'this.i = 0;'
  3803. ]),
  3804. LinesToStr([
  3805. 'do {',
  3806. ' continue;',
  3807. '} while (!true);',
  3808. 'while (true) continue;',
  3809. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3810. '']));
  3811. end;
  3812. procedure TTestModule.TestProc_External;
  3813. begin
  3814. StartProgram(false);
  3815. Add('procedure Foo; external name ''console.log'';');
  3816. Add('function Bar: longint; external name ''get.item'';');
  3817. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3818. Add('var');
  3819. Add(' i: longint;');
  3820. Add('begin');
  3821. Add(' Foo;');
  3822. Add(' i:=Bar;');
  3823. Add(' i:=Bla(''abc'');');
  3824. ConvertProgram;
  3825. CheckSource('TestProc_External',
  3826. LinesToStr([ // statements
  3827. 'this.i = 0;'
  3828. ]),
  3829. LinesToStr([
  3830. 'console.log();',
  3831. '$mod.i = get.item();',
  3832. '$mod.i = apply.something("abc");'
  3833. ]));
  3834. end;
  3835. procedure TTestModule.TestProc_ExternalOtherUnit;
  3836. begin
  3837. AddModuleWithIntfImplSrc('unit2.pas',
  3838. LinesToStr([
  3839. 'procedure Now; external name ''Date.now'';',
  3840. 'procedure DoIt;'
  3841. ]),
  3842. 'procedure doit; begin end;');
  3843. StartUnit(true);
  3844. Add('interface');
  3845. Add('uses unit2;');
  3846. Add('implementation');
  3847. Add('begin');
  3848. Add(' now;');
  3849. Add(' now();');
  3850. Add(' uNit2.now;');
  3851. Add(' uNit2.now();');
  3852. Add(' doit;');
  3853. Add(' uNit2.doit;');
  3854. ConvertUnit;
  3855. CheckSource('TestProc_ExternalOtherUnit',
  3856. LinesToStr([
  3857. '']),
  3858. LinesToStr([
  3859. 'Date.now();',
  3860. 'Date.now();',
  3861. 'Date.now();',
  3862. 'Date.now();',
  3863. 'pas.unit2.DoIt();',
  3864. 'pas.unit2.DoIt();',
  3865. '']));
  3866. end;
  3867. procedure TTestModule.TestProc_Asm;
  3868. begin
  3869. StartProgram(false);
  3870. Add([
  3871. '{$mode delphi}',
  3872. 'function DoIt: longint;',
  3873. 'begin;',
  3874. ' asm',
  3875. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3876. ' end;',
  3877. ' asm console.log(); end;',
  3878. ' asm',
  3879. ' s = "'' ";',
  3880. ' s = ''" '';',
  3881. ' s = s + "world" + "''";',
  3882. ' // end',
  3883. ' s = ''end'';',
  3884. ' s = "end";',
  3885. ' s = "foo\"bar";',
  3886. ' s = ''a\''b'';',
  3887. ' s = `${expr}\`-"-''-`;',
  3888. ' s = `multi',
  3889. 'line`;',
  3890. ' end;',
  3891. 'end;',
  3892. 'procedure Fly;',
  3893. 'asm',
  3894. ' return;',
  3895. 'end;',
  3896. 'begin']);
  3897. ConvertProgram;
  3898. CheckSource('TestProc_Asm',
  3899. LinesToStr([ // statements
  3900. 'this.DoIt = function () {',
  3901. ' var Result = 0;',
  3902. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3903. ' console.log();',
  3904. ' s = "'' ";',
  3905. ' s = ''" '';',
  3906. ' s = s + "world" + "''";',
  3907. ' // end',
  3908. ' s = ''end'';',
  3909. ' s = "end";',
  3910. ' s = "foo\"bar";',
  3911. ' s = ''a\''b'';',
  3912. ' s = `${expr}\`-"-''-`;',
  3913. ' s = `multi',
  3914. 'line`;',
  3915. ' return Result;',
  3916. '};',
  3917. 'this.Fly = function () {',
  3918. ' return;',
  3919. '};',
  3920. '']),
  3921. LinesToStr([
  3922. ''
  3923. ]));
  3924. end;
  3925. procedure TTestModule.TestProc_AsmSubBlock;
  3926. begin
  3927. StartProgram(true,[supTObject]);
  3928. Add([
  3929. '{$mode delphi}',
  3930. 'type',
  3931. ' TBird = class end;',
  3932. 'procedure Run(w: word);',
  3933. 'begin;',
  3934. ' if true then asm console.log(); end;',
  3935. ' if w>3 then asm',
  3936. ' var a = w+1;',
  3937. ' w = a+3;',
  3938. ' end;',
  3939. ' while (w>7) do asm',
  3940. ' w+=3; w*=2;',
  3941. ' end;',
  3942. ' try',
  3943. ' except',
  3944. ' on E: TBird do',
  3945. ' asm console.log(E); end;',
  3946. ' on E: TObject do',
  3947. ' asm var i=3; i--; end;',
  3948. ' else asm Fly; High; end;',
  3949. ' end;',
  3950. 'end;',
  3951. 'begin']);
  3952. ConvertProgram;
  3953. CheckSource('TestProc_AsmSubBlock',
  3954. LinesToStr([ // statements
  3955. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3956. '});',
  3957. 'this.Run = function (w) {',
  3958. ' if (true) console.log();',
  3959. ' if (w > 3) {',
  3960. ' var a = w+1;',
  3961. ' w = a+3;',
  3962. ' };',
  3963. ' while (w > 7) {',
  3964. ' w+=3; w*=2;',
  3965. ' };',
  3966. ' try {} catch ($e) {',
  3967. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3968. ' var E = $e;',
  3969. ' console.log(E);',
  3970. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3971. ' var E = $e;',
  3972. ' var i=3; i--;',
  3973. ' } else {',
  3974. ' Fly; High;',
  3975. ' }',
  3976. ' };',
  3977. '};',
  3978. '']),
  3979. LinesToStr([
  3980. ''
  3981. ]));
  3982. end;
  3983. procedure TTestModule.TestProc_Assembler;
  3984. begin
  3985. StartProgram(false);
  3986. Add('function DoIt: longint; assembler;');
  3987. Add('asm');
  3988. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3989. Add('end;');
  3990. Add('begin');
  3991. ConvertProgram;
  3992. CheckSource('TestProc_Assembler',
  3993. LinesToStr([ // statements
  3994. 'this.DoIt = function () {',
  3995. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3996. '};'
  3997. ]),
  3998. LinesToStr([
  3999. ''
  4000. ]));
  4001. end;
  4002. procedure TTestModule.TestProc_VarParam;
  4003. begin
  4004. StartProgram(false);
  4005. Add('type integer = longint;');
  4006. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4007. Add('var vJ: integer;');
  4008. Add('begin');
  4009. Add(' vg:=vg+1;');
  4010. Add(' vj:=vh+2;');
  4011. Add(' vi:=vi+3;');
  4012. Add(' doit(vg,vg,vg);');
  4013. Add(' doit(vh,vh,vj);');
  4014. Add(' doit(vi,vi,vi);');
  4015. Add(' doit(vj,vj,vj);');
  4016. Add('end;');
  4017. Add('var i: integer;');
  4018. Add('begin');
  4019. Add(' doit(i,i,i);');
  4020. ConvertProgram;
  4021. CheckSource('TestProc_VarParam',
  4022. LinesToStr([ // statements
  4023. 'this.DoIt = function (vG,vH,vI) {',
  4024. ' var vJ = 0;',
  4025. ' vG = vG + 1;',
  4026. ' vJ = vH + 2;',
  4027. ' vI.set(vI.get()+3);',
  4028. ' $mod.DoIt(vG, vG, {',
  4029. ' get: function () {',
  4030. ' return vG;',
  4031. ' },',
  4032. ' set: function (v) {',
  4033. ' vG = v;',
  4034. ' }',
  4035. ' });',
  4036. ' $mod.DoIt(vH, vH, {',
  4037. ' get: function () {',
  4038. ' return vJ;',
  4039. ' },',
  4040. ' set: function (v) {',
  4041. ' vJ = v;',
  4042. ' }',
  4043. ' });',
  4044. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4045. ' $mod.DoIt(vJ, vJ, {',
  4046. ' get: function () {',
  4047. ' return vJ;',
  4048. ' },',
  4049. ' set: function (v) {',
  4050. ' vJ = v;',
  4051. ' }',
  4052. ' });',
  4053. '};',
  4054. 'this.i = 0;'
  4055. ]),
  4056. LinesToStr([
  4057. '$mod.DoIt($mod.i,$mod.i,{',
  4058. ' p: $mod,',
  4059. ' get: function () {',
  4060. ' return this.p.i;',
  4061. ' },',
  4062. ' set: function (v) {',
  4063. ' this.p.i = v;',
  4064. ' }',
  4065. '});'
  4066. ]));
  4067. end;
  4068. procedure TTestModule.TestProc_VarParamString;
  4069. begin
  4070. StartProgram(false);
  4071. Add(['type TCaption = string;',
  4072. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4073. 'var c: char;',
  4074. 'begin',
  4075. ' va[1]:=c;',
  4076. ' vb[2]:=c;',
  4077. ' vc[3]:=c;',
  4078. 'end;',
  4079. 'begin']);
  4080. ConvertProgram;
  4081. CheckSource('TestProc_VarParamString',
  4082. LinesToStr([ // statements
  4083. 'this.DoIt = function (vA,vB,vC) {',
  4084. ' var c = "";',
  4085. ' vA = rtl.setCharAt(vA, 0, c);',
  4086. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4087. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4088. '};',
  4089. '']),
  4090. LinesToStr([
  4091. ]));
  4092. end;
  4093. procedure TTestModule.TestProc_VarParamV;
  4094. begin
  4095. StartProgram(false);
  4096. Add([
  4097. 'procedure Inc2(var i: longint);',
  4098. 'begin',
  4099. ' i:=i+2;',
  4100. 'end;',
  4101. 'procedure DoIt(v: longint);',
  4102. 'var p: array of longint;',
  4103. 'begin',
  4104. ' Inc2(v);',
  4105. ' Inc2(p[v]);',
  4106. 'end;',
  4107. 'begin']);
  4108. ConvertProgram;
  4109. CheckSource('TestProc_VarParamV',
  4110. LinesToStr([ // statements
  4111. 'this.Inc2 = function (i) {',
  4112. ' i.set(i.get()+2);',
  4113. '};',
  4114. 'this.DoIt = function (v) {',
  4115. ' var p = [];',
  4116. ' $mod.Inc2({get: function () {',
  4117. ' return v;',
  4118. ' }, set: function (w) {',
  4119. ' v = w;',
  4120. ' }});',
  4121. ' $mod.Inc2({',
  4122. ' a: v,',
  4123. ' p: p,',
  4124. ' get: function () {',
  4125. ' return this.p[this.a];',
  4126. ' },',
  4127. ' set: function (v) {',
  4128. ' this.p[this.a] = v;',
  4129. ' }',
  4130. ' });',
  4131. '};',
  4132. '']),
  4133. LinesToStr([
  4134. '']));
  4135. end;
  4136. procedure TTestModule.TestProc_Overload;
  4137. begin
  4138. StartProgram(false);
  4139. Add('procedure DoIt(vI: longint); begin end;');
  4140. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4141. Add('procedure DoIt(vD: double); begin end;');
  4142. Add('begin');
  4143. Add(' DoIt(1);');
  4144. Add(' DoIt(2,3);');
  4145. Add(' DoIt(4.5);');
  4146. ConvertProgram;
  4147. CheckSource('TestProcedureOverload',
  4148. LinesToStr([ // statements
  4149. 'this.DoIt = function (vI) {',
  4150. '};',
  4151. 'this.DoIt$1 = function (vI, vJ) {',
  4152. '};',
  4153. 'this.DoIt$2 = function (vD) {',
  4154. '};',
  4155. '']),
  4156. LinesToStr([
  4157. '$mod.DoIt(1);',
  4158. '$mod.DoIt$1(2, 3);',
  4159. '$mod.DoIt$2(4.5);',
  4160. '']));
  4161. end;
  4162. procedure TTestModule.TestProc_OverloadForward;
  4163. begin
  4164. StartProgram(false);
  4165. Add('procedure DoIt(vI: longint); forward;');
  4166. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4167. Add('procedure doit(vi: longint); begin end;');
  4168. Add('begin');
  4169. Add(' doit(1);');
  4170. Add(' doit(2,3);');
  4171. ConvertProgram;
  4172. CheckSource('TestProcedureOverloadForward',
  4173. LinesToStr([ // statements
  4174. 'this.DoIt$1 = function (vI, vJ) {',
  4175. '};',
  4176. 'this.DoIt = function (vI) {',
  4177. '};',
  4178. '']),
  4179. LinesToStr([
  4180. '$mod.DoIt(1);',
  4181. '$mod.DoIt$1(2, 3);',
  4182. '']));
  4183. end;
  4184. procedure TTestModule.TestProc_OverloadIntfImpl;
  4185. begin
  4186. StartUnit(false);
  4187. Add('interface');
  4188. Add('procedure DoIt(vI: longint);');
  4189. Add('procedure DoIt(vI, vJ: longint);');
  4190. Add('implementation');
  4191. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4192. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4193. Add('procedure DoIt(vi: longint); begin end;');
  4194. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4195. Add('procedure DoIt(vi, vj: longint); begin end;');
  4196. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4197. Add('begin');
  4198. Add(' doit(1);');
  4199. Add(' doit(2,3);');
  4200. Add(' doit(4,5,6);');
  4201. Add(' doit(7,8,9,10);');
  4202. Add(' doit(11,12,13,14,15);');
  4203. ConvertUnit;
  4204. CheckSource('TestProcedureOverloadUnit',
  4205. LinesToStr([ // statements
  4206. 'var $impl = $mod.$impl;',
  4207. 'this.DoIt = function (vI) {',
  4208. '};',
  4209. 'this.DoIt$1 = function (vI, vJ) {',
  4210. '};',
  4211. '']),
  4212. LinesToStr([ // this.$init
  4213. '$mod.DoIt(1);',
  4214. '$mod.DoIt$1(2, 3);',
  4215. '$impl.DoIt$3(4,5,6);',
  4216. '$impl.DoIt$4(7,8,9,10);',
  4217. '$impl.DoIt$2(11,12,13,14,15);',
  4218. '']),
  4219. LinesToStr([ // implementation
  4220. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4221. '};',
  4222. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4223. '};',
  4224. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4225. '};',
  4226. '']));
  4227. end;
  4228. procedure TTestModule.TestProc_OverloadNested;
  4229. begin
  4230. StartProgram(false);
  4231. Add([
  4232. 'procedure doit(vA: longint);',
  4233. ' procedure DoIt(vA, vB: longint); overload;',
  4234. ' begin',
  4235. ' doit(1);',
  4236. ' doit(1,2);',
  4237. ' end;',
  4238. ' procedure doit(vA, vB, vC: longint);',
  4239. ' begin',
  4240. ' doit(1);',
  4241. ' doit(1,2);',
  4242. ' doit(1,2,3);',
  4243. ' end;',
  4244. 'begin',
  4245. ' doit(1);',
  4246. ' doit(1,2);',
  4247. ' doit(1,2,3);',
  4248. 'end;',
  4249. 'begin // main',
  4250. ' doit(1);']);
  4251. ConvertProgram;
  4252. CheckSource('TestProcedureOverloadNested',
  4253. LinesToStr([ // statements
  4254. 'this.doit = function (vA) {',
  4255. ' function DoIt$1(vA, vB) {',
  4256. ' $mod.doit(1);',
  4257. ' DoIt$1(1, 2);',
  4258. ' };',
  4259. ' function doit$2(vA, vB, vC) {',
  4260. ' $mod.doit(1);',
  4261. ' DoIt$1(1, 2);',
  4262. ' doit$2(1, 2, 3);',
  4263. ' };',
  4264. ' $mod.doit(1);',
  4265. ' DoIt$1(1, 2);',
  4266. ' doit$2(1, 2, 3);',
  4267. '};',
  4268. '']),
  4269. LinesToStr([
  4270. '$mod.doit(1);',
  4271. '']));
  4272. end;
  4273. procedure TTestModule.TestProc_OverloadNestedForward;
  4274. begin
  4275. StartProgram(false);
  4276. Add([
  4277. 'procedure DoIt(vA: longint); overload; forward;',
  4278. 'procedure DoIt(vB, vC: longint); overload;',
  4279. 'begin // 2 param overload',
  4280. ' doit(1);',
  4281. ' doit(1,2);',
  4282. 'end;',
  4283. 'procedure doit(vA: longint);',
  4284. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4285. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4286. ' begin // 4 param overload',
  4287. ' doit(1);',
  4288. ' doit(1,2);',
  4289. ' doit(1,2,3);',
  4290. ' doit(1,2,3,4);',
  4291. ' end;',
  4292. ' procedure doit(vA, vB, vC: longint);',
  4293. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4294. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4295. ' begin // 6 param overload',
  4296. ' doit(1);',
  4297. ' doit(1,2);',
  4298. ' doit(1,2,3);',
  4299. ' doit(1,2,3,4);',
  4300. ' doit(1,2,3,4,5);',
  4301. ' doit(1,2,3,4,5,6);',
  4302. ' end;',
  4303. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4304. ' begin // 5 param overload',
  4305. ' doit(1);',
  4306. ' doit(1,2);',
  4307. ' doit(1,2,3);',
  4308. ' doit(1,2,3,4);',
  4309. ' doit(1,2,3,4,5);',
  4310. ' doit(1,2,3,4,5,6);',
  4311. ' end;',
  4312. ' begin // 3 param overload',
  4313. ' doit(1);',
  4314. ' doit(1,2);',
  4315. ' doit(1,2,3);',
  4316. ' doit(1,2,3,4);',
  4317. ' doit(1,2,3,4,5);',
  4318. ' doit(1,2,3,4,5,6);',
  4319. ' end;',
  4320. 'begin // 1 param overload',
  4321. ' doit(1);',
  4322. ' doit(1,2);',
  4323. ' doit(1,2,3);',
  4324. ' doit(1,2,3,4);',
  4325. 'end;',
  4326. 'begin // main',
  4327. ' doit(1);',
  4328. ' doit(1,2);']);
  4329. ConvertProgram;
  4330. CheckSource('TestProc_OverloadNestedForward',
  4331. LinesToStr([ // statements
  4332. 'this.DoIt$1 = function (vB, vC) {',
  4333. ' $mod.DoIt(1);',
  4334. ' $mod.DoIt$1(1, 2);',
  4335. '};',
  4336. 'this.DoIt = function (vA) {',
  4337. ' function DoIt$3(vA, vB, vC, vD) {',
  4338. ' $mod.DoIt(1);',
  4339. ' $mod.DoIt$1(1, 2);',
  4340. ' DoIt$2(1, 2, 3);',
  4341. ' DoIt$3(1, 2, 3, 4);',
  4342. ' };',
  4343. ' function DoIt$2(vA, vB, vC) {',
  4344. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4345. ' $mod.DoIt(1);',
  4346. ' $mod.DoIt$1(1, 2);',
  4347. ' DoIt$2(1, 2, 3);',
  4348. ' DoIt$3(1, 2, 3, 4);',
  4349. ' DoIt$4(1, 2, 3, 4, 5);',
  4350. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4351. ' };',
  4352. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4353. ' $mod.DoIt(1);',
  4354. ' $mod.DoIt$1(1, 2);',
  4355. ' DoIt$2(1, 2, 3);',
  4356. ' DoIt$3(1, 2, 3, 4);',
  4357. ' DoIt$4(1, 2, 3, 4, 5);',
  4358. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4359. ' };',
  4360. ' $mod.DoIt(1);',
  4361. ' $mod.DoIt$1(1, 2);',
  4362. ' DoIt$2(1, 2, 3);',
  4363. ' DoIt$3(1, 2, 3, 4);',
  4364. ' DoIt$4(1, 2, 3, 4, 5);',
  4365. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4366. ' };',
  4367. ' $mod.DoIt(1);',
  4368. ' $mod.DoIt$1(1, 2);',
  4369. ' DoIt$2(1, 2, 3);',
  4370. ' DoIt$3(1, 2, 3, 4);',
  4371. '};',
  4372. '']),
  4373. LinesToStr([
  4374. '$mod.DoIt(1);',
  4375. '$mod.DoIt$1(1, 2);',
  4376. '']));
  4377. end;
  4378. procedure TTestModule.TestProc_OverloadUnitCycle;
  4379. begin
  4380. AddModuleWithIntfImplSrc('Unit2.pas',
  4381. LinesToStr([
  4382. 'type',
  4383. ' TObject = class',
  4384. ' procedure DoIt(b: boolean); virtual; abstract;',
  4385. ' procedure DoIt(i: longint); virtual; abstract;',
  4386. ' end;',
  4387. '']),
  4388. 'uses test1;');
  4389. StartUnit(true);
  4390. Add([
  4391. 'interface',
  4392. 'uses unit2;',
  4393. 'type',
  4394. ' TEagle = class(TObject)',
  4395. ' procedure DoIt(b: boolean); override;',
  4396. ' procedure DoIt(i: longint); override;',
  4397. ' end;',
  4398. 'implementation',
  4399. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4400. 'procedure TEagle.DoIt(i: longint); begin end;',
  4401. '']);
  4402. ConvertUnit;
  4403. CheckSource('TestProc_OverloadUnitCycle',
  4404. LinesToStr([ // statements
  4405. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4406. ' this.DoIt = function (b) {',
  4407. ' };',
  4408. ' this.DoIt$1 = function (i) {',
  4409. ' };',
  4410. '});',
  4411. '']),
  4412. '',
  4413. LinesToStr([
  4414. '']));
  4415. end;
  4416. procedure TTestModule.TestProc_Varargs;
  4417. begin
  4418. StartProgram(false);
  4419. Add([
  4420. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4421. 'procedure ProcB; varargs; external name ''ProcB'';',
  4422. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4423. 'function GetIt: longint; begin end;',
  4424. 'begin',
  4425. ' ProcA(1);',
  4426. ' ProcA(1,2);',
  4427. ' ProcA(1,2.0);',
  4428. ' ProcA(1,2,3);',
  4429. ' ProcA(1,''2'');',
  4430. ' ProcA(2,'''');',
  4431. ' ProcA(3,false);',
  4432. ' ProcB;',
  4433. ' ProcB();',
  4434. ' ProcB(4);',
  4435. ' ProcB(''foo'');',
  4436. ' ProcC;',
  4437. ' ProcC();',
  4438. ' ProcC(4);',
  4439. ' ProcC(5,''foo'');',
  4440. ' ProcB(GetIt);',
  4441. ' ProcB(GetIt());',
  4442. ' ProcB(GetIt,GetIt());']);
  4443. ConvertProgram;
  4444. CheckSource('TestProc_Varargs',
  4445. LinesToStr([ // statements
  4446. 'this.GetIt = function () {',
  4447. ' var Result = 0;',
  4448. ' return Result;',
  4449. '};',
  4450. '']),
  4451. LinesToStr([
  4452. 'ProcA(1);',
  4453. 'ProcA(1, 2);',
  4454. 'ProcA(1, 2.0);',
  4455. 'ProcA(1, 2, 3);',
  4456. 'ProcA(1, "2");',
  4457. 'ProcA(2, "");',
  4458. 'ProcA(3, false);',
  4459. 'ProcB();',
  4460. 'ProcB();',
  4461. 'ProcB(4);',
  4462. 'ProcB("foo");',
  4463. 'ProcC(17);',
  4464. 'ProcC(17);',
  4465. 'ProcC(4);',
  4466. 'ProcC(5, "foo");',
  4467. 'ProcB($mod.GetIt());',
  4468. 'ProcB($mod.GetIt());',
  4469. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4470. '']));
  4471. end;
  4472. procedure TTestModule.TestProc_ConstOrder;
  4473. begin
  4474. StartProgram(false);
  4475. Add([
  4476. 'const A = 3;',
  4477. 'const B = A+1;',
  4478. 'procedure DoIt;',
  4479. 'const C = A+1;',
  4480. 'const D = B+1;',
  4481. 'const E = D+C+B+A;',
  4482. 'begin',
  4483. 'end;',
  4484. 'begin'
  4485. ]);
  4486. ConvertProgram;
  4487. CheckSource('TestProc_ConstOrder',
  4488. LinesToStr([ // statements
  4489. 'this.A = 3;',
  4490. 'this.B = 3 + 1;',
  4491. 'var C = 3 + 1;',
  4492. 'var D = 4 + 1;',
  4493. 'var E = 5 + 4 + 4 + 3;',
  4494. 'this.DoIt = function () {',
  4495. '};',
  4496. '']),
  4497. LinesToStr([
  4498. ''
  4499. ]));
  4500. end;
  4501. procedure TTestModule.TestProc_DuplicateConst;
  4502. begin
  4503. StartProgram(false);
  4504. Add([
  4505. 'const A = 1;',
  4506. 'procedure DoIt;',
  4507. 'const A = 2;',
  4508. ' procedure SubIt;',
  4509. ' const A = 21;',
  4510. ' begin',
  4511. ' end;',
  4512. 'begin',
  4513. 'end;',
  4514. 'procedure DoSome;',
  4515. 'const A = 3;',
  4516. 'begin',
  4517. 'end;',
  4518. 'begin'
  4519. ]);
  4520. ConvertProgram;
  4521. CheckSource('TestProc_DuplicateConst',
  4522. LinesToStr([ // statements
  4523. 'this.A = 1;',
  4524. 'var A$1 = 2;',
  4525. 'var A$2 = 21;',
  4526. 'this.DoIt = function () {',
  4527. ' function SubIt() {',
  4528. ' };',
  4529. '};',
  4530. 'var A$3 = 3;',
  4531. 'this.DoSome = function () {',
  4532. '};',
  4533. '']),
  4534. LinesToStr([
  4535. ''
  4536. ]));
  4537. end;
  4538. procedure TTestModule.TestProc_LocalVarAbsolute;
  4539. begin
  4540. StartProgram(false);
  4541. Add([
  4542. 'type',
  4543. ' TObject = class',
  4544. ' Index: longint;',
  4545. ' procedure DoAbs(Item: pointer);',
  4546. ' end;',
  4547. 'procedure TObject.DoAbs(Item: pointer);',
  4548. 'var',
  4549. ' o: TObject absolute Item;',
  4550. 'begin',
  4551. ' if o.Index<o.Index then o.Index:=o.Index;',
  4552. 'end;',
  4553. 'procedure DoIt(i: longint; p: pointer);',
  4554. 'var',
  4555. ' d: double absolute i;',
  4556. ' s: string absolute d;',
  4557. ' oi: TObject absolute i;',
  4558. ' op: TObject absolute p;',
  4559. 'begin',
  4560. ' if d=d then d:=d;',
  4561. ' if s=s then s:=s;',
  4562. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4563. ' if op.Index=op.Index then op.Index:=op.Index;',
  4564. 'end;',
  4565. 'begin']);
  4566. ConvertProgram;
  4567. CheckSource('TestProc_LocalVarAbsolute',
  4568. LinesToStr([ // statements
  4569. 'rtl.createClass(this, "TObject", null, function () {',
  4570. ' this.$init = function () {',
  4571. ' this.Index = 0;',
  4572. ' };',
  4573. ' this.$final = function () {',
  4574. ' };',
  4575. ' this.DoAbs = function (Item) {',
  4576. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4577. ' };',
  4578. '});',
  4579. 'this.DoIt = function (i, p) {',
  4580. ' if (i === i) i = i;',
  4581. ' if (i === i) i = i;',
  4582. ' if (i.Index < i.Index) i.Index = i.Index;',
  4583. ' if (p.Index === p.Index) p.Index = p.Index;',
  4584. '};'
  4585. ]),
  4586. LinesToStr([
  4587. ]));
  4588. end;
  4589. procedure TTestModule.TestProc_LocalVarInit;
  4590. begin
  4591. StartProgram(false);
  4592. Add([
  4593. 'type TBytes = array of byte;',
  4594. 'procedure DoIt;',
  4595. 'const c = 4;',
  4596. 'var',
  4597. ' b: byte = 1;',
  4598. ' w: word = 2+c;',
  4599. ' p: pointer = nil;',
  4600. ' Buffer: TBytes = nil;',
  4601. 'begin',
  4602. 'end;',
  4603. 'begin']);
  4604. ConvertProgram;
  4605. CheckSource('TestProc_LocalVarInit',
  4606. LinesToStr([ // statements
  4607. 'var c = 4;',
  4608. 'this.DoIt = function () {',
  4609. ' var b = 1;',
  4610. ' var w = 2 + 4;',
  4611. ' var p = null;',
  4612. ' var Buffer = [];',
  4613. '};',
  4614. '']),
  4615. LinesToStr([
  4616. ]));
  4617. end;
  4618. procedure TTestModule.TestProc_ReservedWords;
  4619. begin
  4620. StartProgram(false);
  4621. Add([
  4622. 'procedure Date(ArrayBuffer: longint);',
  4623. 'const',
  4624. ' NaN: longint = 3;',
  4625. 'var',
  4626. ' &Boolean: longint;',
  4627. ' procedure Error(ArrayBuffer: longint);',
  4628. ' begin',
  4629. ' end;',
  4630. 'begin',
  4631. ' Nan:=&bOolean;',
  4632. 'end;',
  4633. 'begin',
  4634. ' Date(1);']);
  4635. ConvertProgram;
  4636. CheckSource('TestProc_ReservedWords',
  4637. LinesToStr([ // statements
  4638. 'var naN = 3;',
  4639. 'this.Date = function (arrayBuffer) {',
  4640. ' var boolean = 0;',
  4641. ' function error(arrayBuffer) {',
  4642. ' };',
  4643. ' naN = boolean;',
  4644. '};',
  4645. '']),
  4646. LinesToStr([
  4647. ' $mod.Date(1);'
  4648. ]));
  4649. end;
  4650. procedure TTestModule.TestProc_ConstRefWord;
  4651. begin
  4652. StartProgram(false);
  4653. Add([
  4654. 'procedure Run(constref w: word);',
  4655. 'var l: word;',
  4656. 'begin',
  4657. ' l:=w;',
  4658. ' Run(w);',
  4659. ' Run(l);',
  4660. 'end;',
  4661. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4662. 'begin',
  4663. ' Run(a);',
  4664. ' Run(b);',
  4665. ' Run(c);',
  4666. ' Run(d);',
  4667. ' Run(e);',
  4668. 'end;',
  4669. 'begin',
  4670. ' Run(1);']);
  4671. ConvertProgram;
  4672. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4673. CheckSource('TestProc_ConstRefWord',
  4674. LinesToStr([ // statements
  4675. 'this.Run = function (w) {',
  4676. ' var l = 0;',
  4677. ' l = w;',
  4678. ' $mod.Run(w);',
  4679. ' $mod.Run(l);',
  4680. '};',
  4681. 'this.Fly = function (a, b, c, d, e) {',
  4682. ' $mod.Run(a);',
  4683. ' $mod.Run(b.get());',
  4684. ' $mod.Run(c.get());',
  4685. ' $mod.Run(d);',
  4686. ' $mod.Run(e);',
  4687. '};',
  4688. '']),
  4689. LinesToStr([
  4690. '$mod.Run(1);'
  4691. ]));
  4692. end;
  4693. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4694. begin
  4695. StartProgram(false);
  4696. Add([
  4697. '{$mode objfpc}',
  4698. 'type',
  4699. ' TFunc = reference to function(x: word): word;',
  4700. 'var Func: TFunc;',
  4701. 'procedure DoIt(a: word);',
  4702. 'begin',
  4703. ' Func:=function(b:word): word',
  4704. ' begin',
  4705. ' Result:=a+b;',
  4706. ' exit(b);',
  4707. ' exit(Result);',
  4708. ' end;',// test semicolon
  4709. ' a:=3;',
  4710. 'end;',
  4711. 'begin',
  4712. ' Func:=function(c:word):word begin',
  4713. ' Result:=3+c;',
  4714. ' exit(c);',
  4715. ' exit(Result);',
  4716. ' end;']);
  4717. ConvertProgram;
  4718. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4719. LinesToStr([ // statements
  4720. 'this.Func = null;',
  4721. 'this.DoIt = function (a) {',
  4722. ' $mod.Func = function (b) {',
  4723. ' var Result = 0;',
  4724. ' Result = a + b;',
  4725. ' return b;',
  4726. ' return Result;',
  4727. ' return Result;',
  4728. ' };',
  4729. ' a = 3;',
  4730. '};',
  4731. '']),
  4732. LinesToStr([
  4733. '$mod.Func = function (c) {',
  4734. ' var Result = 0;',
  4735. ' Result = 3 + c;',
  4736. ' return c;',
  4737. ' return Result;',
  4738. ' return Result;',
  4739. '};',
  4740. '']));
  4741. end;
  4742. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4743. begin
  4744. StartProgram(false);
  4745. Add([
  4746. '{$mode delphi}',
  4747. 'type',
  4748. ' TProc = reference to procedure(x: word);',
  4749. 'procedure DoIt(a: word);',
  4750. 'var Proc: TProc;',
  4751. 'begin',
  4752. ' Proc:=procedure(b:word) begin end;',
  4753. 'end;',
  4754. 'var Proc: TProc;',
  4755. 'begin',
  4756. ' Proc:=procedure(c:word) begin end;',
  4757. '']);
  4758. ConvertProgram;
  4759. CheckSource('TestAnonymousProc_Assign_Delphi',
  4760. LinesToStr([ // statements
  4761. 'this.DoIt = function (a) {',
  4762. ' var Proc = null;',
  4763. ' Proc = function (b) {',
  4764. ' };',
  4765. '};',
  4766. 'this.Proc = null;',
  4767. '']),
  4768. LinesToStr([
  4769. '$mod.Proc = function (c) {',
  4770. '};',
  4771. '']));
  4772. end;
  4773. procedure TTestModule.TestAnonymousProc_Arg;
  4774. begin
  4775. StartProgram(false);
  4776. Add([
  4777. 'type',
  4778. ' TProc = reference to procedure;',
  4779. ' TFunc = reference to function(x: word): word;',
  4780. 'procedure DoMore(f,g: TProc);',
  4781. 'begin',
  4782. 'end;',
  4783. 'procedure DoOdd(v: jsvalue);',
  4784. 'begin',
  4785. 'end;',
  4786. 'procedure DoIt(f: TFunc);',
  4787. 'begin',
  4788. ' DoIt(function(b:word): word',
  4789. ' begin',
  4790. ' Result:=1+b;',
  4791. ' end);',
  4792. ' DoMore(procedure begin end, procedure begin end);',
  4793. ' DoOdd(procedure begin end);',
  4794. 'end;',
  4795. 'begin',
  4796. ' DoMore(procedure begin end,',
  4797. ' procedure assembler asm',
  4798. ' console.log("c");',
  4799. ' end);',
  4800. '']);
  4801. ConvertProgram;
  4802. CheckSource('TestAnonymousProc_Arg',
  4803. LinesToStr([ // statements
  4804. 'this.DoMore = function (f, g) {',
  4805. '};',
  4806. 'this.DoOdd = function (v) {',
  4807. '};',
  4808. 'this.DoIt = function (f) {',
  4809. ' $mod.DoIt(function (b) {',
  4810. ' var Result = 0;',
  4811. ' Result = 1 + b;',
  4812. ' return Result;',
  4813. ' });',
  4814. ' $mod.DoMore(function () {',
  4815. ' }, function () {',
  4816. ' });',
  4817. ' $mod.DoOdd(function () {',
  4818. ' });',
  4819. '};',
  4820. '']),
  4821. LinesToStr([
  4822. '$mod.DoMore(function () {',
  4823. '}, function () {',
  4824. ' console.log("c");',
  4825. '});',
  4826. '']));
  4827. end;
  4828. procedure TTestModule.TestAnonymousProc_Typecast;
  4829. begin
  4830. StartProgram(false);
  4831. Add([
  4832. 'type',
  4833. ' TProc = reference to procedure(w: word);',
  4834. ' TArr = array of word;',
  4835. ' TFuncArr = reference to function: TArr;',
  4836. 'procedure DoIt(p: TProc);',
  4837. 'var',
  4838. ' w: word;',
  4839. ' a: TArr;',
  4840. 'begin',
  4841. ' p:=TProc(procedure(b: smallint) begin end);',
  4842. ' a:=TFuncArr(function: TArr begin end)();',
  4843. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4844. 'end;',
  4845. 'begin']);
  4846. ConvertProgram;
  4847. CheckSource('TestAnonymousProc_Typecast',
  4848. LinesToStr([ // statements
  4849. 'this.DoIt = function (p) {',
  4850. ' var w = 0;',
  4851. ' var a = [];',
  4852. ' p = function (b) {',
  4853. ' };',
  4854. ' a = function () {',
  4855. ' var Result = [];',
  4856. ' return Result;',
  4857. ' }();',
  4858. ' w = function () {',
  4859. ' var Result = [];',
  4860. ' return Result;',
  4861. ' }()[3];',
  4862. '};',
  4863. '']),
  4864. LinesToStr([
  4865. '']));
  4866. end;
  4867. procedure TTestModule.TestAnonymousProc_With;
  4868. begin
  4869. StartProgram(false);
  4870. Add([
  4871. 'type',
  4872. ' TProc = reference to procedure(w: word);',
  4873. ' TObject = class',
  4874. ' b: boolean;',
  4875. ' end;',
  4876. 'var',
  4877. ' p: TProc;',
  4878. ' bird: TObject;',
  4879. 'begin',
  4880. ' with bird do',
  4881. ' p:=procedure(w: word)',
  4882. ' begin',
  4883. ' b:=w>2;',
  4884. ' end;',
  4885. '']);
  4886. ConvertProgram;
  4887. CheckSource('TestAnonymousProc_With',
  4888. LinesToStr([ // statements
  4889. 'rtl.createClass(this, "TObject", null, function () {',
  4890. ' this.$init = function () {',
  4891. ' this.b = false;',
  4892. ' };',
  4893. ' this.$final = function () {',
  4894. ' };',
  4895. '});',
  4896. 'this.p = null;',
  4897. 'this.bird = null;',
  4898. '']),
  4899. LinesToStr([
  4900. 'var $with = $mod.bird;',
  4901. '$mod.p = function (w) {',
  4902. ' $with.b = w > 2;',
  4903. '};',
  4904. '']));
  4905. end;
  4906. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4907. begin
  4908. StartProgram(false);
  4909. Add([
  4910. 'type',
  4911. ' TProc = reference to procedure;',
  4912. ' TObject = class',
  4913. ' b: boolean;',
  4914. ' end;',
  4915. 'procedure DoIt;',
  4916. 'var',
  4917. ' p: TProc;',
  4918. 'begin',
  4919. ' try',
  4920. ' except',
  4921. ' on E: TObject do',
  4922. ' p:=procedure',
  4923. ' begin',
  4924. ' E.b:=true;',
  4925. ' end;',
  4926. ' end;',
  4927. 'end;',
  4928. 'begin']);
  4929. ConvertProgram;
  4930. CheckSource('TestAnonymousProc_ExceptOn',
  4931. LinesToStr([ // statements
  4932. 'rtl.createClass(this, "TObject", null, function () {',
  4933. ' this.$init = function () {',
  4934. ' this.b = false;',
  4935. ' };',
  4936. ' this.$final = function () {',
  4937. ' };',
  4938. '});',
  4939. 'this.DoIt = function () {',
  4940. ' var p = null;',
  4941. ' try {} catch ($e) {',
  4942. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4943. ' var E = $e;',
  4944. ' p = function () {',
  4945. ' E.b = true;',
  4946. ' };',
  4947. ' } else throw $e',
  4948. ' };',
  4949. '};',
  4950. '']),
  4951. LinesToStr([
  4952. '']));
  4953. end;
  4954. procedure TTestModule.TestAnonymousProc_Nested;
  4955. begin
  4956. StartProgram(false);
  4957. Add([
  4958. 'type',
  4959. ' TProc = reference to procedure;',
  4960. ' TObject = class',
  4961. ' i: byte;',
  4962. ' procedure DoIt;',
  4963. ' end;',
  4964. 'procedure TObject.DoIt;',
  4965. 'var',
  4966. ' p: TProc;',
  4967. ' procedure Sub;',
  4968. ' begin',
  4969. ' p:=procedure',
  4970. ' begin',
  4971. ' i:=3;',
  4972. ' Self.i:=4;',
  4973. ' p:=procedure',
  4974. ' procedure SubSub;',
  4975. ' begin',
  4976. ' i:=13;',
  4977. ' Self.i:=14;',
  4978. ' end;',
  4979. ' begin',
  4980. ' i:=13;',
  4981. ' Self.i:=14;',
  4982. ' end;',
  4983. ' end;',
  4984. ' end;',
  4985. 'begin',
  4986. 'end;',
  4987. 'begin']);
  4988. ConvertProgram;
  4989. CheckSource('TestAnonymousProc_Nested',
  4990. LinesToStr([ // statements
  4991. 'rtl.createClass(this, "TObject", null, function () {',
  4992. ' this.$init = function () {',
  4993. ' this.i = 0;',
  4994. ' };',
  4995. ' this.$final = function () {',
  4996. ' };',
  4997. ' this.DoIt = function () {',
  4998. ' var $Self = this;',
  4999. ' var p = null;',
  5000. ' function Sub() {',
  5001. ' p = function () {',
  5002. ' $Self.i = 3;',
  5003. ' $Self.i = 4;',
  5004. ' p = function () {',
  5005. ' function SubSub() {',
  5006. ' $Self.i = 13;',
  5007. ' $Self.i = 14;',
  5008. ' };',
  5009. ' $Self.i = 13;',
  5010. ' $Self.i = 14;',
  5011. ' };',
  5012. ' };',
  5013. ' };',
  5014. ' };',
  5015. '});',
  5016. '']),
  5017. LinesToStr([
  5018. '']));
  5019. end;
  5020. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5021. begin
  5022. StartProgram(false);
  5023. Add([
  5024. 'type',
  5025. ' TProc = reference to procedure;',
  5026. 'function DoIt: TProc;',
  5027. ' function Sub: TProc;',
  5028. ' begin',
  5029. ' Result:=procedure',
  5030. ' begin',
  5031. ' Sub:=procedure',
  5032. ' procedure SubSub;',
  5033. ' begin',
  5034. ' Result:=nil;',
  5035. ' Sub:=nil;',
  5036. ' DoIt:=nil;',
  5037. ' end;',
  5038. ' begin',
  5039. ' Result:=nil;',
  5040. ' Sub:=nil;',
  5041. ' DoIt:=nil;',
  5042. ' end;',
  5043. ' end;',
  5044. ' end;',
  5045. 'begin',
  5046. 'end;',
  5047. 'begin']);
  5048. ConvertProgram;
  5049. CheckSource('TestAnonymousProc_NestedAssignResult',
  5050. LinesToStr([ // statements
  5051. 'this.DoIt = function () {',
  5052. ' var Result = null;',
  5053. ' function Sub() {',
  5054. ' var Result$1 = null;',
  5055. ' Result$1 = function () {',
  5056. ' Result$1 = function () {',
  5057. ' function SubSub() {',
  5058. ' Result$1 = null;',
  5059. ' Result$1 = null;',
  5060. ' Result = null;',
  5061. ' };',
  5062. ' Result$1 = null;',
  5063. ' Result$1 = null;',
  5064. ' Result = null;',
  5065. ' };',
  5066. ' };',
  5067. ' return Result$1;',
  5068. ' };',
  5069. ' return Result;',
  5070. '};',
  5071. '']),
  5072. LinesToStr([
  5073. '']));
  5074. end;
  5075. procedure TTestModule.TestAnonymousProc_Class;
  5076. begin
  5077. StartProgram(false);
  5078. Add([
  5079. 'type',
  5080. ' TProc = reference to procedure;',
  5081. ' TEvent = procedure of object;',
  5082. ' TObject = class',
  5083. ' Size: word;',
  5084. ' function GetIt: TProc;',
  5085. ' procedure DoIt; virtual; abstract;',
  5086. ' end;',
  5087. 'function TObject.GetIt: TProc;',
  5088. 'begin',
  5089. ' Result:=procedure',
  5090. ' var p: TEvent;',
  5091. ' begin',
  5092. ' Size:=Size;',
  5093. ' Size:=Self.Size;',
  5094. ' p:=@DoIt;',
  5095. ' p:[email protected];',
  5096. ' end;',
  5097. 'end;',
  5098. 'begin']);
  5099. ConvertProgram;
  5100. CheckSource('TestAnonymousProc_Class',
  5101. LinesToStr([ // statements
  5102. 'rtl.createClass(this, "TObject", null, function () {',
  5103. ' this.$init = function () {',
  5104. ' this.Size = 0;',
  5105. ' };',
  5106. ' this.$final = function () {',
  5107. ' };',
  5108. ' this.GetIt = function () {',
  5109. ' var $Self = this;',
  5110. ' var Result = null;',
  5111. ' Result = function () {',
  5112. ' var p = null;',
  5113. ' $Self.Size = $Self.Size;',
  5114. ' $Self.Size = $Self.Size;',
  5115. ' p = rtl.createCallback($Self, "DoIt");',
  5116. ' p = rtl.createCallback($Self, "DoIt");',
  5117. ' };',
  5118. ' return Result;',
  5119. ' };',
  5120. '});',
  5121. '']),
  5122. LinesToStr([
  5123. '']));
  5124. end;
  5125. procedure TTestModule.TestAnonymousProc_ForLoop;
  5126. begin
  5127. StartProgram(false);
  5128. Add([
  5129. 'type TProc = reference to procedure;',
  5130. 'procedure Foo(p: TProc);',
  5131. 'begin',
  5132. 'end;',
  5133. 'procedure DoIt;',
  5134. 'var i: word;',
  5135. ' a: word;',
  5136. 'begin',
  5137. ' for i:=1 to 10 do begin',
  5138. ' Foo(procedure begin a:=3; end);',
  5139. ' end;',
  5140. 'end;',
  5141. 'begin',
  5142. ' DoIt;']);
  5143. ConvertProgram;
  5144. CheckSource('TestAnonymousProc_ForLoop',
  5145. LinesToStr([ // statements
  5146. 'this.Foo = function (p) {',
  5147. '};',
  5148. 'this.DoIt = function () {',
  5149. ' var i = 0;',
  5150. ' var a = 0;',
  5151. ' for (i = 1; i <= 10; i++) {',
  5152. ' $mod.Foo(function () {',
  5153. ' a = 3;',
  5154. ' });',
  5155. ' };',
  5156. '};',
  5157. '']),
  5158. LinesToStr([
  5159. '$mod.DoIt();'
  5160. ]));
  5161. end;
  5162. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5163. begin
  5164. StartProgram(false);
  5165. Add([
  5166. '{$mode delphi}',
  5167. 'type',
  5168. ' TProc = reference to procedure;',
  5169. ' TFunc = reference to function(x: word): word;',
  5170. 'procedure Run;',
  5171. 'asm',
  5172. 'end;',
  5173. 'procedure Walk(p: TProc; f: TFunc);',
  5174. 'begin',
  5175. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5176. 'end;',
  5177. 'begin',
  5178. ' Walk(procedure',
  5179. ' asm',
  5180. ' console.log("a");',
  5181. ' end,',
  5182. ' function(x: word): word asm',
  5183. ' console.log("c");',
  5184. ' end);',
  5185. '']);
  5186. ConvertProgram;
  5187. CheckSource('TestAnonymousProc_AsmDelphi',
  5188. LinesToStr([ // statements
  5189. 'this.Run = function () {',
  5190. '};',
  5191. 'this.Walk = function (p, f) {',
  5192. ' $mod.Walk(function () {',
  5193. ' }, function (b) {',
  5194. ' return 1+b;',
  5195. ' });',
  5196. '};',
  5197. '']),
  5198. LinesToStr([
  5199. '$mod.Walk(function () {',
  5200. ' console.log("a");',
  5201. '}, function (x) {',
  5202. ' console.log("c");',
  5203. '});',
  5204. '']));
  5205. end;
  5206. procedure TTestModule.TestEnum_Name;
  5207. begin
  5208. StartProgram(false);
  5209. Add('type TMyEnum = (Red, Green, Blue);');
  5210. Add('var e: TMyEnum;');
  5211. Add('var f: TMyEnum = Blue;');
  5212. Add('begin');
  5213. Add(' e:=green;');
  5214. Add(' e:=default(TMyEnum);');
  5215. ConvertProgram;
  5216. CheckSource('TestEnum_Name',
  5217. LinesToStr([ // statements
  5218. 'this.TMyEnum = {',
  5219. ' "0":"Red",',
  5220. ' Red:0,',
  5221. ' "1":"Green",',
  5222. ' Green:1,',
  5223. ' "2":"Blue",',
  5224. ' Blue:2',
  5225. ' };',
  5226. 'this.e = 0;',
  5227. 'this.f = this.TMyEnum.Blue;'
  5228. ]),
  5229. LinesToStr([
  5230. '$mod.e=$mod.TMyEnum.Green;',
  5231. '$mod.e=$mod.TMyEnum.Red;'
  5232. ]));
  5233. end;
  5234. procedure TTestModule.TestEnum_Number;
  5235. begin
  5236. Converter.Options:=Converter.Options+[coEnumNumbers];
  5237. StartProgram(false);
  5238. Add('type TMyEnum = (Red, Green);');
  5239. Add('var');
  5240. Add(' e: TMyEnum;');
  5241. Add(' f: TMyEnum = Green;');
  5242. Add(' i: longint;');
  5243. Add('begin');
  5244. Add(' e:=green;');
  5245. Add(' i:=longint(e);');
  5246. ConvertProgram;
  5247. CheckSource('TestEnumNumber',
  5248. LinesToStr([ // statements
  5249. 'this.TMyEnum = {',
  5250. ' "0":"Red",',
  5251. ' Red:0,',
  5252. ' "1":"Green",',
  5253. ' Green:1',
  5254. ' };',
  5255. 'this.e = 0;',
  5256. 'this.f = 1;',
  5257. 'this.i = 0;'
  5258. ]),
  5259. LinesToStr([
  5260. '$mod.e=1;',
  5261. '$mod.i=$mod.e;'
  5262. ]));
  5263. end;
  5264. procedure TTestModule.TestEnum_ConstFail;
  5265. begin
  5266. StartProgram(false);
  5267. Add([
  5268. 'type TMyEnum = (Red = 100, Green = 101);',
  5269. 'var',
  5270. ' e: TMyEnum;',
  5271. ' f: TMyEnum = Green;',
  5272. 'begin',
  5273. ' e:=green;']);
  5274. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5275. ConvertProgram;
  5276. end;
  5277. procedure TTestModule.TestEnum_Functions;
  5278. begin
  5279. StartProgram(false);
  5280. Add([
  5281. 'type TMyEnum = (Red, Green);',
  5282. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5283. 'var',
  5284. ' v: longint;',
  5285. ' s: string;',
  5286. 'begin',
  5287. ' val(s,e,v);',
  5288. ' val(s,e,i);',
  5289. 'end;',
  5290. 'var',
  5291. ' e: TMyEnum;',
  5292. ' i: longint;',
  5293. ' s: string;',
  5294. ' b: boolean;',
  5295. 'begin',
  5296. ' i:=ord(red);',
  5297. ' i:=ord(green);',
  5298. ' i:=ord(e);',
  5299. ' i:=ord(b);',
  5300. ' e:=low(tmyenum);',
  5301. ' e:=low(e);',
  5302. ' b:=low(boolean);',
  5303. ' e:=high(tmyenum);',
  5304. ' e:=high(e);',
  5305. ' b:=high(boolean);',
  5306. ' e:=pred(green);',
  5307. ' e:=pred(e);',
  5308. ' b:=pred(b);',
  5309. ' e:=succ(red);',
  5310. ' e:=succ(e);',
  5311. ' b:=succ(b);',
  5312. ' e:=tmyenum(1);',
  5313. ' e:=tmyenum(i);',
  5314. ' s:=str(e);',
  5315. ' str(e,s);',
  5316. ' str(red,s);',
  5317. ' s:=str(e:3);',
  5318. ' writestr(s,e:3,red);',
  5319. ' val(s,e,i);',
  5320. ' i:=longint(e);']);
  5321. ConvertProgram;
  5322. CheckSource('TestEnum_Functions',
  5323. LinesToStr([ // statements
  5324. 'this.TMyEnum = {',
  5325. ' "0":"Red",',
  5326. ' Red:0,',
  5327. ' "1":"Green",',
  5328. ' Green:1',
  5329. ' };',
  5330. 'this.DoIt = function (e, i) {',
  5331. ' var v = 0;',
  5332. ' var s = "";',
  5333. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5334. ' v = w;',
  5335. ' }));',
  5336. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5337. '};',
  5338. 'this.e = 0;',
  5339. 'this.i = 0;',
  5340. 'this.s = "";',
  5341. 'this.b = false;',
  5342. '']),
  5343. LinesToStr([
  5344. '$mod.i=$mod.TMyEnum.Red;',
  5345. '$mod.i=$mod.TMyEnum.Green;',
  5346. '$mod.i=$mod.e;',
  5347. '$mod.i=$mod.b+0;',
  5348. '$mod.e=$mod.TMyEnum.Red;',
  5349. '$mod.e=$mod.TMyEnum.Red;',
  5350. '$mod.b=false;',
  5351. '$mod.e=$mod.TMyEnum.Green;',
  5352. '$mod.e=$mod.TMyEnum.Green;',
  5353. '$mod.b=true;',
  5354. '$mod.e=$mod.TMyEnum.Green-1;',
  5355. '$mod.e=$mod.e-1;',
  5356. '$mod.b=false;',
  5357. '$mod.e=$mod.TMyEnum.Red+1;',
  5358. '$mod.e=$mod.e+1;',
  5359. '$mod.b=true;',
  5360. '$mod.e=1;',
  5361. '$mod.e=$mod.i;',
  5362. '$mod.s = $mod.TMyEnum[$mod.e];',
  5363. '$mod.s = $mod.TMyEnum[$mod.e];',
  5364. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5365. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5366. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5367. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5368. ' $mod.i = v;',
  5369. '});',
  5370. '$mod.i=$mod.e;',
  5371. '']));
  5372. end;
  5373. procedure TTestModule.TestEnumRg_Functions;
  5374. begin
  5375. StartProgram(false);
  5376. Add([
  5377. 'type',
  5378. ' TEnum = (Red, Green, Blue);',
  5379. ' TEnumRg = Green..Blue;',
  5380. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5381. 'var',
  5382. ' v: longint;',
  5383. ' s: string;',
  5384. 'begin',
  5385. ' val(s,e,v);',
  5386. ' val(s,e,i);',
  5387. 'end;',
  5388. 'var',
  5389. ' e: TEnumRg;',
  5390. ' i: longint;',
  5391. ' s: string;',
  5392. 'begin',
  5393. ' i:=ord(green);',
  5394. ' i:=ord(e);',
  5395. ' e:=low(tenumrg);',
  5396. ' e:=low(e);',
  5397. ' e:=high(tenumrg);',
  5398. ' e:=high(e);',
  5399. ' e:=pred(blue);',
  5400. ' e:=pred(e);',
  5401. ' e:=succ(green);',
  5402. ' e:=succ(e);',
  5403. ' e:=tenumrg(1);',
  5404. ' e:=tenumrg(i);',
  5405. ' s:=str(e);',
  5406. ' str(e,s);',
  5407. ' str(red,s);',
  5408. ' s:=str(e:3);',
  5409. ' writestr(s,e:3,blue);',
  5410. ' val(s,e,i);',
  5411. ' i:=longint(e);']);
  5412. ConvertProgram;
  5413. CheckSource('TestEnumRg_Functions',
  5414. LinesToStr([ // statements
  5415. 'this.TEnum = {',
  5416. ' "0":"Red",',
  5417. ' Red:0,',
  5418. ' "1":"Green",',
  5419. ' Green:1,',
  5420. ' "2":"Blue",',
  5421. ' Blue:2',
  5422. ' };',
  5423. 'this.DoIt = function (e, i) {',
  5424. ' var v = 0;',
  5425. ' var s = "";',
  5426. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5427. ' v = w;',
  5428. ' }));',
  5429. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5430. '};',
  5431. 'this.e = this.TEnum.Green;',
  5432. 'this.i = 0;',
  5433. 'this.s = "";',
  5434. '']),
  5435. LinesToStr([
  5436. '$mod.i=$mod.TEnum.Green;',
  5437. '$mod.i=$mod.e;',
  5438. '$mod.e=$mod.TEnum.Green;',
  5439. '$mod.e=$mod.TEnum.Green;',
  5440. '$mod.e=$mod.TEnum.Blue;',
  5441. '$mod.e=$mod.TEnum.Blue;',
  5442. '$mod.e=$mod.TEnum.Blue-1;',
  5443. '$mod.e=$mod.e-1;',
  5444. '$mod.e=$mod.TEnum.Green+1;',
  5445. '$mod.e=$mod.e+1;',
  5446. '$mod.e=1;',
  5447. '$mod.e=$mod.i;',
  5448. '$mod.s = $mod.TEnum[$mod.e];',
  5449. '$mod.s = $mod.TEnum[$mod.e];',
  5450. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5451. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5452. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5453. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5454. ' $mod.i = v;',
  5455. '});',
  5456. '$mod.i=$mod.e;',
  5457. '']));
  5458. end;
  5459. procedure TTestModule.TestEnum_AsParams;
  5460. begin
  5461. StartProgram(false);
  5462. Add('type TEnum = (Red,Blue);');
  5463. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5464. Add('var vJ: TEnum;');
  5465. Add('begin');
  5466. Add(' vg:=vg;');
  5467. Add(' vj:=vh;');
  5468. Add(' vi:=vi;');
  5469. Add(' doit(vg,vg,vg);');
  5470. Add(' doit(vh,vh,vj);');
  5471. Add(' doit(vi,vi,vi);');
  5472. Add(' doit(vj,vj,vj);');
  5473. Add('end;');
  5474. Add('var i: TEnum;');
  5475. Add('begin');
  5476. Add(' doit(i,i,i);');
  5477. ConvertProgram;
  5478. CheckSource('TestEnum_AsParams',
  5479. LinesToStr([ // statements
  5480. 'this.TEnum = {',
  5481. ' "0": "Red",',
  5482. ' Red: 0,',
  5483. ' "1": "Blue",',
  5484. ' Blue: 1',
  5485. '};',
  5486. 'this.DoIt = function (vG,vH,vI) {',
  5487. ' var vJ = 0;',
  5488. ' vG = vG;',
  5489. ' vJ = vH;',
  5490. ' vI.set(vI.get());',
  5491. ' $mod.DoIt(vG, vG, {',
  5492. ' get: function () {',
  5493. ' return vG;',
  5494. ' },',
  5495. ' set: function (v) {',
  5496. ' vG = v;',
  5497. ' }',
  5498. ' });',
  5499. ' $mod.DoIt(vH, vH, {',
  5500. ' get: function () {',
  5501. ' return vJ;',
  5502. ' },',
  5503. ' set: function (v) {',
  5504. ' vJ = v;',
  5505. ' }',
  5506. ' });',
  5507. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5508. ' $mod.DoIt(vJ, vJ, {',
  5509. ' get: function () {',
  5510. ' return vJ;',
  5511. ' },',
  5512. ' set: function (v) {',
  5513. ' vJ = v;',
  5514. ' }',
  5515. ' });',
  5516. '};',
  5517. 'this.i = 0;'
  5518. ]),
  5519. LinesToStr([
  5520. '$mod.DoIt($mod.i,$mod.i,{',
  5521. ' p: $mod,',
  5522. ' get: function () {',
  5523. ' return this.p.i;',
  5524. ' },',
  5525. ' set: function (v) {',
  5526. ' this.p.i = v;',
  5527. ' }',
  5528. '});'
  5529. ]));
  5530. end;
  5531. procedure TTestModule.TestEnumRange_Array;
  5532. begin
  5533. StartProgram(false);
  5534. Add([
  5535. 'type',
  5536. ' TEnum = (Red, Green, Blue);',
  5537. ' TEnumRg = green..blue;',
  5538. ' TArr = array[TEnumRg] of byte;',
  5539. ' TArr2 = array[green..blue] of byte;',
  5540. 'var',
  5541. ' a: TArr;',
  5542. ' b: TArr = (3,4);',
  5543. ' c: TArr2 = (5,6);',
  5544. 'begin',
  5545. ' a[green] := b[blue];',
  5546. ' c[green] := c[blue];',
  5547. '']);
  5548. ConvertProgram;
  5549. CheckSource('TestEnumRange_Array',
  5550. LinesToStr([ // statements
  5551. 'this.TEnum = {',
  5552. ' "0": "Red",',
  5553. ' Red: 0,',
  5554. ' "1": "Green",',
  5555. ' Green: 1,',
  5556. ' "2": "Blue",',
  5557. ' Blue: 2',
  5558. '};',
  5559. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5560. 'this.b = [3, 4];',
  5561. 'this.c = [5, 6];',
  5562. '']),
  5563. LinesToStr([
  5564. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5565. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5566. '']));
  5567. end;
  5568. procedure TTestModule.TestEnum_ForIn;
  5569. begin
  5570. StartProgram(false);
  5571. Add([
  5572. 'type',
  5573. ' TEnum = (Red, Green, Blue);',
  5574. ' TEnumRg = green..blue;',
  5575. ' TArr = array[TEnum] of byte;',
  5576. ' TArrRg = array[TEnumRg] of byte;',
  5577. 'var',
  5578. ' e: TEnum;',
  5579. ' a1: TArr = (3,4,5);',
  5580. ' a2: TArrRg = (11,12);',
  5581. ' b: byte;',
  5582. 'begin',
  5583. ' for e in TEnum do ;',
  5584. ' for e in TEnumRg do ;',
  5585. ' for e in TArr do ;',
  5586. ' for e in TArrRg do ;',
  5587. ' for b in a1 do ;',
  5588. ' for b in a2 do ;',
  5589. '']);
  5590. ConvertProgram;
  5591. CheckSource('TestEnum_ForIn',
  5592. LinesToStr([ // statements
  5593. 'this.TEnum = {',
  5594. ' "0": "Red",',
  5595. ' Red: 0,',
  5596. ' "1": "Green",',
  5597. ' Green: 1,',
  5598. ' "2": "Blue",',
  5599. ' Blue: 2',
  5600. '};',
  5601. 'this.e = 0;',
  5602. 'this.a1 = [3, 4, 5];',
  5603. 'this.a2 = [11, 12];',
  5604. 'this.b = 0;',
  5605. '']),
  5606. LinesToStr([
  5607. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5608. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5609. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5610. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5611. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5612. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5613. '']));
  5614. end;
  5615. procedure TTestModule.TestEnum_ScopedNumber;
  5616. begin
  5617. Converter.Options:=Converter.Options+[coEnumNumbers];
  5618. StartProgram(false);
  5619. Add([
  5620. 'type',
  5621. ' TEnum = (Red, Green);',
  5622. 'var',
  5623. ' e: TEnum;',
  5624. 'begin',
  5625. ' e:=TEnum.Green;',
  5626. '']);
  5627. ConvertProgram;
  5628. CheckSource('TestEnum_ScopedNumber',
  5629. LinesToStr([ // statements
  5630. 'this.TEnum = {',
  5631. ' "0": "Red",',
  5632. ' Red: 0,',
  5633. ' "1": "Green",',
  5634. ' Green: 1',
  5635. '};',
  5636. 'this.e = 0;',
  5637. '']),
  5638. LinesToStr([
  5639. '$mod.e = 1;']));
  5640. end;
  5641. procedure TTestModule.TestEnum_InFunction;
  5642. begin
  5643. StartProgram(false);
  5644. Add([
  5645. 'const TEnum = 3;',
  5646. 'procedure DoIt;',
  5647. 'type',
  5648. ' TEnum = (Red, Green, Blue);',
  5649. ' procedure Sub;',
  5650. ' type',
  5651. ' TEnumSub = (Left, Right);',
  5652. ' var',
  5653. ' es: TEnumSub;',
  5654. ' begin',
  5655. ' es:=Left;',
  5656. ' end;',
  5657. 'var',
  5658. ' e, e2: TEnum;',
  5659. 'begin',
  5660. ' if e in [red,blue] then e2:=e;',
  5661. 'end;',
  5662. 'begin']);
  5663. ConvertProgram;
  5664. CheckSource('TestEnum_InFunction',
  5665. LinesToStr([ // statements
  5666. 'this.TEnum = 3;',
  5667. 'var TEnum$1 = {',
  5668. ' "0":"Red",',
  5669. ' Red:0,',
  5670. ' "1":"Green",',
  5671. ' Green:1,',
  5672. ' "2":"Blue",',
  5673. ' Blue:2',
  5674. ' };',
  5675. 'var TEnumSub = {',
  5676. ' "0": "Left",',
  5677. ' Left: 0,',
  5678. ' "1": "Right",',
  5679. ' Right: 1',
  5680. '};',
  5681. 'this.DoIt = function () {',
  5682. ' function Sub() {',
  5683. ' var es = 0;',
  5684. ' es = TEnumSub.Left;',
  5685. ' };',
  5686. ' var e = 0;',
  5687. ' var e2 = 0;',
  5688. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5689. '};',
  5690. '']),
  5691. LinesToStr([
  5692. '']));
  5693. end;
  5694. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5695. begin
  5696. StartUnit(true);
  5697. Add([
  5698. 'interface',
  5699. 'var color: (red, green);',
  5700. 'implementation',
  5701. 'initialization',
  5702. ' color:=green;',
  5703. '']);
  5704. ConvertUnit;
  5705. CheckSource('TestEnum_Name_Anonymous_Unit',
  5706. LinesToStr([
  5707. 'this.color$a = {',
  5708. ' "0": "red",',
  5709. ' red: 0,',
  5710. ' "1": "green",',
  5711. ' green: 1',
  5712. '};',
  5713. 'this.color = 0;',
  5714. '']),
  5715. LinesToStr([ // this.$init
  5716. '$mod.color = $mod.color$a.green;',
  5717. '']),
  5718. LinesToStr([ // implementation
  5719. '']) );
  5720. end;
  5721. procedure TTestModule.TestSet_Enum;
  5722. begin
  5723. StartProgram(false);
  5724. Add([
  5725. 'type',
  5726. ' TColor = (Red, Green, Blue);',
  5727. ' TColors = set of TColor;',
  5728. 'var',
  5729. ' c: TColor;',
  5730. ' s: TColors;',
  5731. ' t: TColors = [];',
  5732. ' u: TColors = [Red];',
  5733. 'begin',
  5734. ' s:=[];',
  5735. ' s:=[Green];',
  5736. ' s:=[Green,Blue];',
  5737. ' s:=[Red..Blue];',
  5738. ' s:=[Red,Green..Blue];',
  5739. ' s:=[Red,c];',
  5740. ' s:=t;',
  5741. ' s:=default(TColors);',
  5742. '']);
  5743. ConvertProgram;
  5744. CheckSource('TestSet',
  5745. LinesToStr([ // statements
  5746. 'this.TColor = {',
  5747. ' "0":"Red",',
  5748. ' Red:0,',
  5749. ' "1":"Green",',
  5750. ' Green:1,',
  5751. ' "2":"Blue",',
  5752. ' Blue:2',
  5753. ' };',
  5754. 'this.c = 0;',
  5755. 'this.s = {};',
  5756. 'this.t = {};',
  5757. 'this.u = rtl.createSet(this.TColor.Red);'
  5758. ]),
  5759. LinesToStr([
  5760. '$mod.s={};',
  5761. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5762. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5763. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5764. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5765. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5766. '$mod.s=rtl.refSet($mod.t);',
  5767. '$mod.s={};',
  5768. '']));
  5769. end;
  5770. procedure TTestModule.TestSet_Operators;
  5771. begin
  5772. StartProgram(false);
  5773. Add('type');
  5774. Add(' TColor = (Red, Green, Blue);');
  5775. Add(' TColors = set of tcolor;');
  5776. Add('var');
  5777. Add(' vC: TColor;');
  5778. Add(' vS: TColors;');
  5779. Add(' vT: TColors;');
  5780. Add(' vU: TColors;');
  5781. Add(' B: boolean;');
  5782. Add('begin');
  5783. Add(' include(vs,green);');
  5784. Add(' exclude(vs,vc);');
  5785. Add(' vs:=vt+vu;');
  5786. Add(' vs:=vt+[red];');
  5787. Add(' vs:=[red]+vt;');
  5788. Add(' vs:=[red]+[green];');
  5789. Add(' vs:=vt-vu;');
  5790. Add(' vs:=vt-[red];');
  5791. Add(' vs:=[red]-vt;');
  5792. Add(' vs:=[red]-[green];');
  5793. Add(' vs:=vt*vu;');
  5794. Add(' vs:=vt*[red];');
  5795. Add(' vs:=[red]*vt;');
  5796. Add(' vs:=[red]*[green];');
  5797. Add(' vs:=vt><vu;');
  5798. Add(' vs:=vt><[red];');
  5799. Add(' vs:=[red]><vt;');
  5800. Add(' vs:=[red]><[green];');
  5801. Add(' b:=vt=vu;');
  5802. Add(' b:=vt=[red];');
  5803. Add(' b:=[red]=vt;');
  5804. Add(' b:=[red]=[green];');
  5805. Add(' b:=vt<>vu;');
  5806. Add(' b:=vt<>[red];');
  5807. Add(' b:=[red]<>vt;');
  5808. Add(' b:=[red]<>[green];');
  5809. Add(' b:=vt<=vu;');
  5810. Add(' b:=vt<=[red];');
  5811. Add(' b:=[red]<=vt;');
  5812. Add(' b:=[red]<=[green];');
  5813. Add(' b:=vt>=vu;');
  5814. Add(' b:=vt>=[red];');
  5815. Add(' b:=[red]>=vt;');
  5816. Add(' b:=[red]>=[green];');
  5817. ConvertProgram;
  5818. CheckSource('TestSet_Operators',
  5819. LinesToStr([ // statements
  5820. 'this.TColor = {',
  5821. ' "0":"Red",',
  5822. ' Red:0,',
  5823. ' "1":"Green",',
  5824. ' Green:1,',
  5825. ' "2":"Blue",',
  5826. ' Blue:2',
  5827. ' };',
  5828. 'this.vC = 0;',
  5829. 'this.vS = {};',
  5830. 'this.vT = {};',
  5831. 'this.vU = {};',
  5832. 'this.B = false;'
  5833. ]),
  5834. LinesToStr([
  5835. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5836. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5837. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5838. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5839. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5840. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5841. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5842. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5843. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5844. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5845. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5846. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5847. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5848. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5849. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5850. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5851. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5852. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5853. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5854. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5855. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5856. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5857. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5858. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5859. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5860. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5861. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5862. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5863. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5864. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5865. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5866. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5867. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5868. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5869. '']));
  5870. end;
  5871. procedure TTestModule.TestSet_Operator_In;
  5872. begin
  5873. StartProgram(false);
  5874. Add([
  5875. 'type',
  5876. ' TColor = (Red, Green, Blue);',
  5877. ' TColors = set of tcolor;',
  5878. ' TColorRg = green..blue;',
  5879. 'var',
  5880. ' vC: tcolor;',
  5881. ' vT: tcolors;',
  5882. ' B: boolean;',
  5883. ' rg: TColorRg;',
  5884. 'begin',
  5885. ' b:=red in vt;',
  5886. ' b:=vc in vt;',
  5887. ' b:=green in [red..blue];',
  5888. ' b:=vc in [red..blue];',
  5889. ' ',
  5890. ' if red in vt then ;',
  5891. ' while vC in vt do ;',
  5892. ' repeat',
  5893. ' until vC in vt;',
  5894. ' if rg in [green..blue] then ;',
  5895. '']);
  5896. ConvertProgram;
  5897. CheckSource('TestSet_Operator_In',
  5898. LinesToStr([ // statements
  5899. 'this.TColor = {',
  5900. ' "0":"Red",',
  5901. ' Red:0,',
  5902. ' "1":"Green",',
  5903. ' Green:1,',
  5904. ' "2":"Blue",',
  5905. ' Blue:2',
  5906. ' };',
  5907. 'this.vC = 0;',
  5908. 'this.vT = {};',
  5909. 'this.B = false;',
  5910. 'this.rg = this.TColor.Green;',
  5911. '']),
  5912. LinesToStr([
  5913. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5914. '$mod.B = $mod.vC in $mod.vT;',
  5915. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5916. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5917. 'if ($mod.TColor.Red in $mod.vT) ;',
  5918. 'while ($mod.vC in $mod.vT) {',
  5919. '};',
  5920. 'do {',
  5921. '} while (!($mod.vC in $mod.vT));',
  5922. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5923. '']));
  5924. end;
  5925. procedure TTestModule.TestSet_Functions;
  5926. begin
  5927. StartProgram(false);
  5928. Add('type');
  5929. Add(' TMyEnum = (Red, Green);');
  5930. Add(' TMyEnums = set of TMyEnum;');
  5931. Add('var');
  5932. Add(' e: TMyEnum;');
  5933. Add(' s: TMyEnums;');
  5934. Add('begin');
  5935. Add(' e:=Low(TMyEnums);');
  5936. Add(' e:=Low(s);');
  5937. Add(' e:=High(TMyEnums);');
  5938. Add(' e:=High(s);');
  5939. ConvertProgram;
  5940. CheckSource('TestSetFunctions',
  5941. LinesToStr([ // statements
  5942. 'this.TMyEnum = {',
  5943. ' "0":"Red",',
  5944. ' Red:0,',
  5945. ' "1":"Green",',
  5946. ' Green:1',
  5947. ' };',
  5948. 'this.e = 0;',
  5949. 'this.s = {};'
  5950. ]),
  5951. LinesToStr([
  5952. '$mod.e=$mod.TMyEnum.Red;',
  5953. '$mod.e=$mod.TMyEnum.Red;',
  5954. '$mod.e=$mod.TMyEnum.Green;',
  5955. '$mod.e=$mod.TMyEnum.Green;',
  5956. '']));
  5957. end;
  5958. procedure TTestModule.TestSet_PassAsArgClone;
  5959. begin
  5960. StartProgram(false);
  5961. Add('type');
  5962. Add(' TMyEnum = (Red, Green);');
  5963. Add(' TMyEnums = set of TMyEnum;');
  5964. Add('procedure DoDefault(s: tmyenums); begin end;');
  5965. Add('procedure DoConst(const s: tmyenums); begin end;');
  5966. Add('var');
  5967. Add(' aSet: tmyenums;');
  5968. Add('begin');
  5969. Add(' dodefault(aset);');
  5970. Add(' doconst(aset);');
  5971. ConvertProgram;
  5972. CheckSource('TestSetFunctions',
  5973. LinesToStr([ // statements
  5974. 'this.TMyEnum = {',
  5975. ' "0":"Red",',
  5976. ' Red:0,',
  5977. ' "1":"Green",',
  5978. ' Green:1',
  5979. ' };',
  5980. 'this.DoDefault = function (s) {',
  5981. '};',
  5982. 'this.DoConst = function (s) {',
  5983. '};',
  5984. 'this.aSet = {};'
  5985. ]),
  5986. LinesToStr([
  5987. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5988. '$mod.DoConst($mod.aSet);',
  5989. '']));
  5990. end;
  5991. procedure TTestModule.TestSet_AsParams;
  5992. begin
  5993. StartProgram(false);
  5994. Add([
  5995. 'type TEnum = (Red,Blue);',
  5996. 'type TEnums = set of TEnum;',
  5997. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5998. 'var vJ: TEnums;',
  5999. 'begin',
  6000. ' Include(vg,red);',
  6001. ' Include(result,blue);',
  6002. ' vg:=vg;',
  6003. ' vj:=vh;',
  6004. ' vi:=vi;',
  6005. ' doit(vg,vg,vg);',
  6006. ' doit(vh,vh,vj);',
  6007. ' doit(vi,vi,vi);',
  6008. ' doit(vj,vj,vj);',
  6009. 'end;',
  6010. 'var i: TEnums;',
  6011. 'begin',
  6012. ' doit(i,i,i);']);
  6013. ConvertProgram;
  6014. CheckSource('TestSet_AsParams',
  6015. LinesToStr([ // statements
  6016. 'this.TEnum = {',
  6017. ' "0": "Red",',
  6018. ' Red: 0,',
  6019. ' "1": "Blue",',
  6020. ' Blue: 1',
  6021. '};',
  6022. 'this.DoIt = function (vG,vH,vI) {',
  6023. ' var Result = {};',
  6024. ' var vJ = {};',
  6025. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6026. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6027. ' vG = rtl.refSet(vG);',
  6028. ' vJ = rtl.refSet(vH);',
  6029. ' vI.set(rtl.refSet(vI.get()));',
  6030. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6031. ' get: function () {',
  6032. ' return vG;',
  6033. ' },',
  6034. ' set: function (v) {',
  6035. ' vG = v;',
  6036. ' }',
  6037. ' });',
  6038. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6039. ' get: function () {',
  6040. ' return vJ;',
  6041. ' },',
  6042. ' set: function (v) {',
  6043. ' vJ = v;',
  6044. ' }',
  6045. ' });',
  6046. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6047. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6048. ' get: function () {',
  6049. ' return vJ;',
  6050. ' },',
  6051. ' set: function (v) {',
  6052. ' vJ = v;',
  6053. ' }',
  6054. ' });',
  6055. ' return Result;',
  6056. '};',
  6057. 'this.i = {};'
  6058. ]),
  6059. LinesToStr([
  6060. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6061. ' p: $mod,',
  6062. ' get: function () {',
  6063. ' return this.p.i;',
  6064. ' },',
  6065. ' set: function (v) {',
  6066. ' this.p.i = v;',
  6067. ' }',
  6068. '});'
  6069. ]));
  6070. end;
  6071. procedure TTestModule.TestSet_Property;
  6072. begin
  6073. StartProgram(false);
  6074. Add('type');
  6075. Add(' TEnum = (Red,Blue);');
  6076. Add(' TEnums = set of TEnum;');
  6077. Add(' TObject = class');
  6078. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6079. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6080. Add(' property Colors: TEnums read GetColors write SetColors;');
  6081. Add(' end;');
  6082. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6083. Add('begin end;');
  6084. Add('var Obj: TObject;');
  6085. Add('begin');
  6086. Add(' Include(Obj.Colors,Red);');
  6087. Add(' Exclude(Obj.Colors,Red);');
  6088. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6089. ConvertProgram;
  6090. CheckSource('TestSet_Property',
  6091. LinesToStr([ // statements
  6092. 'this.TEnum = {',
  6093. ' "0": "Red",',
  6094. ' Red: 0,',
  6095. ' "1": "Blue",',
  6096. ' Blue: 1',
  6097. '};',
  6098. 'rtl.createClass(this, "TObject", null, function () {',
  6099. ' this.$init = function () {',
  6100. ' };',
  6101. ' this.$final = function () {',
  6102. ' };',
  6103. '});',
  6104. 'this.DoIt = function (i, j, k, l) {',
  6105. '};',
  6106. 'this.Obj = null;',
  6107. '']),
  6108. LinesToStr([
  6109. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6110. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6111. '']));
  6112. end;
  6113. procedure TTestModule.TestSet_EnumConst;
  6114. begin
  6115. StartProgram(false);
  6116. Add([
  6117. 'type',
  6118. ' TEnum = (Red,Blue);',
  6119. ' TEnums = set of TEnum;',
  6120. 'const',
  6121. ' Orange = red;',
  6122. 'var',
  6123. ' Enum: tenum;',
  6124. ' Enums: tenums;',
  6125. 'begin',
  6126. ' Include(enums,orange);',
  6127. ' Exclude(enums,orange);',
  6128. ' if orange in enums then;',
  6129. ' if orange in [orange,red] then;']);
  6130. ConvertProgram;
  6131. CheckSource('TestSet_EnumConst',
  6132. LinesToStr([ // statements
  6133. 'this.TEnum = {',
  6134. ' "0": "Red",',
  6135. ' Red: 0,',
  6136. ' "1": "Blue",',
  6137. ' Blue: 1',
  6138. '};',
  6139. 'this.Orange = this.TEnum.Red;',
  6140. 'this.Enum = 0;',
  6141. 'this.Enums = {};',
  6142. '']),
  6143. LinesToStr([
  6144. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6145. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6146. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6147. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6148. '']));
  6149. end;
  6150. procedure TTestModule.TestSet_IntConst;
  6151. begin
  6152. StartProgram(false);
  6153. Add([
  6154. 'type',
  6155. ' TEnums = set of Byte;',
  6156. 'const',
  6157. ' Orange = 0;',
  6158. 'var',
  6159. ' Enum: byte;',
  6160. ' Enums: tenums;',
  6161. 'begin',
  6162. ' Enums:=[];',
  6163. ' Enums:=[0];',
  6164. ' Enums:=[1..2];',
  6165. //' Include(enums,orange);',
  6166. //' Exclude(enums,orange);',
  6167. ' if orange in enums then;',
  6168. ' if orange in [orange,1] then;']);
  6169. ConvertProgram;
  6170. CheckSource('TestSet_IntConst',
  6171. LinesToStr([ // statements
  6172. 'this.Orange = 0;',
  6173. 'this.Enum = 0;',
  6174. 'this.Enums = {};',
  6175. '']),
  6176. LinesToStr([
  6177. '$mod.Enums = {};',
  6178. '$mod.Enums = rtl.createSet(0);',
  6179. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6180. 'if (0 in $mod.Enums) ;',
  6181. 'if (0 in rtl.createSet(0, 1)) ;',
  6182. '']));
  6183. end;
  6184. procedure TTestModule.TestSet_IntRange;
  6185. begin
  6186. StartProgram(false);
  6187. Add([
  6188. 'type',
  6189. ' TRange = 1..3;',
  6190. ' TEnums = set of TRange;',
  6191. 'const',
  6192. ' Orange = 2;',
  6193. 'var',
  6194. ' Enum: byte;',
  6195. ' Enums: TEnums;',
  6196. 'begin',
  6197. ' Enums:=[];',
  6198. ' Enums:=[1];',
  6199. ' Enums:=[2..3];',
  6200. ' Include(enums,orange);',
  6201. ' Exclude(enums,orange);',
  6202. ' if orange in enums then;',
  6203. ' if orange in [orange,1] then;']);
  6204. ConvertProgram;
  6205. CheckSource('TestSet_IntRange',
  6206. LinesToStr([ // statements
  6207. 'this.Orange = 2;',
  6208. 'this.Enum = 0;',
  6209. 'this.Enums = {};',
  6210. '']),
  6211. LinesToStr([
  6212. '$mod.Enums = {};',
  6213. '$mod.Enums = rtl.createSet(1);',
  6214. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6215. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6216. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6217. 'if (2 in $mod.Enums) ;',
  6218. 'if (2 in rtl.createSet(2, 1)) ;',
  6219. '']));
  6220. end;
  6221. procedure TTestModule.TestSet_AnonymousEnumType;
  6222. begin
  6223. StartProgram(false);
  6224. Add('type');
  6225. Add(' TFlags = set of (red, green);');
  6226. Add('const');
  6227. Add(' favorite = red;');
  6228. Add('var');
  6229. Add(' f: TFlags;');
  6230. Add(' i: longint;');
  6231. Add('begin');
  6232. Add(' Include(f,red);');
  6233. Add(' Include(f,favorite);');
  6234. Add(' i:=ord(red);');
  6235. Add(' i:=ord(favorite);');
  6236. Add(' i:=ord(low(TFlags));');
  6237. Add(' i:=ord(low(f));');
  6238. Add(' i:=ord(low(favorite));');
  6239. Add(' i:=ord(high(TFlags));');
  6240. Add(' i:=ord(high(f));');
  6241. Add(' i:=ord(high(favorite));');
  6242. Add(' f:=[green,favorite];');
  6243. ConvertProgram;
  6244. CheckSource('TestSet_AnonymousEnumType',
  6245. LinesToStr([ // statements
  6246. 'this.TFlags$a = {',
  6247. ' "0": "red",',
  6248. ' red: 0,',
  6249. ' "1": "green",',
  6250. ' green: 1',
  6251. '};',
  6252. 'this.favorite = this.TFlags$a.red;',
  6253. 'this.f = {};',
  6254. 'this.i = 0;',
  6255. '']),
  6256. LinesToStr([
  6257. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6258. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6259. '$mod.i = $mod.TFlags$a.red;',
  6260. '$mod.i = $mod.TFlags$a.red;',
  6261. '$mod.i = $mod.TFlags$a.red;',
  6262. '$mod.i = $mod.TFlags$a.red;',
  6263. '$mod.i = $mod.TFlags$a.red;',
  6264. '$mod.i = $mod.TFlags$a.green;',
  6265. '$mod.i = $mod.TFlags$a.green;',
  6266. '$mod.i = $mod.TFlags$a.green;',
  6267. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6268. '']));
  6269. end;
  6270. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6271. begin
  6272. exit;
  6273. StartProgram(false);
  6274. Add([
  6275. 'type',
  6276. ' TAtoZ = ''A''..''Z'';',
  6277. ' TSetOfAZ = set of TAtoZ;',
  6278. 'var',
  6279. ' c: char;',
  6280. ' a: TAtoZ;',
  6281. ' s: TSetOfAZ = [''P'',''A''];',
  6282. ' i: longint;',
  6283. 'begin',
  6284. ' Include(s,''S'');',
  6285. ' Include(s,c);',
  6286. ' Include(s,a);',
  6287. ' c:=low(TAtoZ);',
  6288. ' i:=ord(low(TAtoZ));',
  6289. ' a:=high(TAtoZ);',
  6290. ' a:=high(TSetOfAtoZ);',
  6291. ' s:=[a,c,''M''];',
  6292. '']);
  6293. ConvertProgram;
  6294. CheckSource('TestSet_AnonymousEnumTypeChar',
  6295. LinesToStr([ // statements
  6296. '']),
  6297. LinesToStr([
  6298. '']));
  6299. end;
  6300. procedure TTestModule.TestSet_ConstEnum;
  6301. begin
  6302. StartProgram(false);
  6303. Add([
  6304. 'type',
  6305. ' TEnum = (red,blue,green);',
  6306. ' TEnums = set of TEnum;',
  6307. 'const',
  6308. ' teAny = [low(TEnum)..high(TEnum)];',
  6309. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6310. 'var',
  6311. ' e: TEnum;',
  6312. ' s: TEnums;',
  6313. 'begin',
  6314. ' if blue in teAny then;',
  6315. ' if blue in teAny+[e] then;',
  6316. ' if blue in teAny+teRedBlue then;',
  6317. ' if e in [red,blue] then;',
  6318. ' s:=teAny;',
  6319. ' s:=teAny+[e];',
  6320. ' s:=[e]+teAny;',
  6321. ' s:=teAny+teRedBlue;',
  6322. ' s:=teAny+teRedBlue+[e];',
  6323. '']);
  6324. ConvertProgram;
  6325. CheckSource('TestSet_ConstEnum',
  6326. LinesToStr([ // statements
  6327. 'this.TEnum = {',
  6328. ' "0": "red",',
  6329. ' red: 0,',
  6330. ' "1": "blue",',
  6331. ' blue: 1,',
  6332. ' "2": "green",',
  6333. ' green: 2',
  6334. '};',
  6335. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6336. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6337. 'this.e = 0;',
  6338. 'this.s = {};',
  6339. '']),
  6340. LinesToStr([
  6341. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6342. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6343. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6344. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6345. '$mod.s = rtl.refSet($mod.teAny);',
  6346. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6347. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6348. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6349. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6350. '']));
  6351. end;
  6352. procedure TTestModule.TestSet_ConstChar;
  6353. begin
  6354. StartProgram(false);
  6355. Add([
  6356. 'const',
  6357. ' LowChars = [''a''..''z''];',
  6358. ' Chars = LowChars+[''A''..''Z''];',
  6359. ' sc = [''А'', ''Я''];',
  6360. 'var',
  6361. ' c: char;',
  6362. ' s: string;',
  6363. 'begin',
  6364. ' if c in lowchars then ;',
  6365. ' if ''a'' in lowchars then ;',
  6366. ' if s[1] in lowchars then ;',
  6367. ' if c in chars then ;',
  6368. ' if c in [''a''..''z'',''_''] then ;',
  6369. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6370. ' if ''Я'' in sc then ;',
  6371. ' if 3=ord('' '') then ;',
  6372. '']);
  6373. ConvertProgram;
  6374. CheckSource('TestSet_ConstChar',
  6375. LinesToStr([ // statements
  6376. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6377. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6378. 'this.sc = rtl.createSet(1040, 1071);',
  6379. 'this.c = "";',
  6380. 'this.s = "";',
  6381. '']),
  6382. LinesToStr([
  6383. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6384. 'if (97 in $mod.LowChars) ;',
  6385. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6386. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6387. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6388. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6389. 'if (1071 in $mod.sc) ;',
  6390. 'if (3 === 32) ;',
  6391. '']));
  6392. end;
  6393. procedure TTestModule.TestSet_ConstInt;
  6394. begin
  6395. StartProgram(false);
  6396. Add([
  6397. 'const',
  6398. ' Months = [1..12];',
  6399. ' Mirror = [-12..-1]+Months;',
  6400. 'var',
  6401. ' i: smallint;',
  6402. 'begin',
  6403. ' if 3 in Months then;',
  6404. ' if i in Months+[i] then;',
  6405. ' if i in Months+Mirror then;',
  6406. ' if i in [4..6,8] then;',
  6407. '']);
  6408. ConvertProgram;
  6409. CheckSource('TestSet_ConstInt',
  6410. LinesToStr([ // statements
  6411. 'this.Months = rtl.createSet(null, 1, 12);',
  6412. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6413. 'this.i = 0;',
  6414. '']),
  6415. LinesToStr([
  6416. 'if (3 in $mod.Months) ;',
  6417. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6418. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6419. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6420. '']));
  6421. end;
  6422. procedure TTestModule.TestSet_InFunction;
  6423. begin
  6424. StartProgram(false);
  6425. Add([
  6426. 'const',
  6427. ' TEnum = 3;',
  6428. ' TSetOfEnum = 4;',
  6429. ' TSetOfAno = 5;',
  6430. 'procedure DoIt;',
  6431. 'type',
  6432. ' TEnum = (red, blue);',
  6433. ' TSetOfEnum = set of TEnum;',
  6434. ' TSetOfAno = set of (up,down);',
  6435. 'var',
  6436. ' e: TEnum;',
  6437. ' se: TSetOfEnum;',
  6438. ' sa: TSetOfAno;',
  6439. 'begin',
  6440. ' se:=[e];',
  6441. ' sa:=[up];',
  6442. 'end;',
  6443. 'begin',
  6444. '']);
  6445. ConvertProgram;
  6446. CheckSource('TestSet_InFunction',
  6447. LinesToStr([ // statements
  6448. 'this.TEnum = 3;',
  6449. 'this.TSetOfEnum = 4;',
  6450. 'this.TSetOfAno = 5;',
  6451. 'var TEnum$1 = {',
  6452. ' "0": "red",',
  6453. ' red: 0,',
  6454. ' "1": "blue",',
  6455. ' blue: 1',
  6456. '};',
  6457. 'var TSetOfAno$a = {',
  6458. ' "0": "up",',
  6459. ' up: 0,',
  6460. ' "1": "down",',
  6461. ' down: 1',
  6462. '};',
  6463. 'this.DoIt = function () {',
  6464. ' var e = 0;',
  6465. ' var se = {};',
  6466. ' var sa = {};',
  6467. ' se = rtl.createSet(e);',
  6468. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6469. '};',
  6470. '']),
  6471. LinesToStr([
  6472. '']));
  6473. end;
  6474. procedure TTestModule.TestSet_ForIn;
  6475. begin
  6476. StartProgram(false);
  6477. Add([
  6478. 'type',
  6479. ' TEnum = (Red, Green, Blue);',
  6480. ' TEnumRg = green..blue;',
  6481. ' TSetOfEnum = set of TEnum;',
  6482. ' TSetOfEnumRg = set of TEnumRg;',
  6483. 'var',
  6484. ' e, e2: TEnum;',
  6485. ' er: TEnum;',
  6486. ' s: TSetOfEnum;',
  6487. 'begin',
  6488. ' for e in TSetOfEnum do ;',
  6489. ' for e in TSetOfEnumRg do ;',
  6490. ' for e in [] do e2:=e;',
  6491. ' for e in [red..green] do e2:=e;',
  6492. ' for e in [green,blue] do e2:=e;',
  6493. ' for e in [red,blue] do e2:=e;',
  6494. ' for e in s do e2:=e;',
  6495. ' for er in TSetOfEnumRg do ;',
  6496. '']);
  6497. ConvertProgram;
  6498. CheckSource('TestSet_ForIn',
  6499. LinesToStr([ // statements
  6500. 'this.TEnum = {',
  6501. ' "0":"Red",',
  6502. ' Red:0,',
  6503. ' "1":"Green",',
  6504. ' Green:1,',
  6505. ' "2":"Blue",',
  6506. ' Blue:2',
  6507. ' };',
  6508. 'this.e = 0;',
  6509. 'this.e2 = 0;',
  6510. 'this.er = 0;',
  6511. 'this.s = {};',
  6512. '']),
  6513. LinesToStr([
  6514. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6515. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6516. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6517. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6518. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6519. 'for (var $l in $mod.s){',
  6520. ' $mod.e = +$l;',
  6521. ' $mod.e2 = $mod.e;',
  6522. '};',
  6523. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6524. '']));
  6525. end;
  6526. procedure TTestModule.TestNestBegin;
  6527. begin
  6528. StartProgram(false);
  6529. Add('begin');
  6530. Add(' begin');
  6531. Add(' begin');
  6532. Add(' end;');
  6533. Add(' begin');
  6534. Add(' if true then ;');
  6535. Add(' end;');
  6536. Add(' end;');
  6537. ConvertProgram;
  6538. CheckSource('TestNestBegin',
  6539. '',
  6540. 'if (true) ;');
  6541. end;
  6542. procedure TTestModule.TestUnitImplVars;
  6543. begin
  6544. StartUnit(false);
  6545. Add('interface');
  6546. Add('implementation');
  6547. Add('var');
  6548. Add(' V1:longint;');
  6549. Add(' V2:longint = 3;');
  6550. Add(' V3:string = ''abc'';');
  6551. ConvertUnit;
  6552. CheckSource('TestUnitImplVars',
  6553. LinesToStr([ // statements
  6554. 'var $impl = $mod.$impl;',
  6555. '']),
  6556. '', // this.$init
  6557. LinesToStr([ // implementation
  6558. '$impl.V1 = 0;',
  6559. '$impl.V2 = 3;',
  6560. '$impl.V3 = "abc";',
  6561. '']) );
  6562. end;
  6563. procedure TTestModule.TestUnitImplConsts;
  6564. begin
  6565. StartUnit(false);
  6566. Add('interface');
  6567. Add('implementation');
  6568. Add('const');
  6569. Add(' v1 = 3;');
  6570. Add(' v2:longint = 4;');
  6571. Add(' v3:string = ''abc'';');
  6572. ConvertUnit;
  6573. CheckSource('TestUnitImplConsts',
  6574. LinesToStr([ // statements
  6575. 'var $impl = $mod.$impl;',
  6576. '']),
  6577. '', // this.$init
  6578. LinesToStr([ // implementation
  6579. '$impl.v1 = 3;',
  6580. '$impl.v2 = 4;',
  6581. '$impl.v3 = "abc";',
  6582. '']) );
  6583. end;
  6584. procedure TTestModule.TestUnitImplRecord;
  6585. begin
  6586. StartUnit(false);
  6587. Add('interface');
  6588. Add('implementation');
  6589. Add('type');
  6590. Add(' TMyRecord = record');
  6591. Add(' i: longint;');
  6592. Add(' end;');
  6593. Add('var aRec: TMyRecord;');
  6594. Add('initialization');
  6595. Add(' arec.i:=3;');
  6596. ConvertUnit;
  6597. CheckSource('TestUnitImplRecord',
  6598. LinesToStr([ // statements
  6599. 'var $impl = $mod.$impl;',
  6600. '']),
  6601. // this.$init
  6602. '$impl.aRec.i = 3;',
  6603. LinesToStr([ // implementation
  6604. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6605. ' this.i = 0;',
  6606. ' this.$eq = function (b) {',
  6607. ' return this.i === b.i;',
  6608. ' };',
  6609. ' this.$assign = function (s) {',
  6610. ' this.i = s.i;',
  6611. ' return this;',
  6612. ' };',
  6613. '});',
  6614. '$impl.aRec = $impl.TMyRecord.$new();',
  6615. '']) );
  6616. end;
  6617. procedure TTestModule.TestRenameJSNameConflict;
  6618. begin
  6619. StartProgram(false);
  6620. Add('var apply: longint;');
  6621. Add('var bind: longint;');
  6622. Add('var call: longint;');
  6623. Add('begin');
  6624. ConvertProgram;
  6625. CheckSource('TestRenameJSNameConflict',
  6626. LinesToStr([ // statements
  6627. 'this.Apply = 0;',
  6628. 'this.Bind = 0;',
  6629. 'this.Call = 0;'
  6630. ]),
  6631. LinesToStr([ // this.$main
  6632. ''
  6633. ]));
  6634. end;
  6635. procedure TTestModule.TestLocalConst;
  6636. begin
  6637. StartProgram(false);
  6638. Add('procedure DoIt;');
  6639. Add('const');
  6640. Add(' cA: longint = 1;');
  6641. Add(' cB = 2;');
  6642. Add(' procedure Sub;');
  6643. Add(' const');
  6644. Add(' csA = 3;');
  6645. Add(' cB: double = 4;');
  6646. Add(' begin');
  6647. Add(' cb:=cb+csa;');
  6648. Add(' ca:=ca+csa+5;');
  6649. Add(' end;');
  6650. Add('begin');
  6651. Add(' ca:=ca+cb+6;');
  6652. Add('end;');
  6653. Add('begin');
  6654. ConvertProgram;
  6655. CheckSource('TestLocalConst',
  6656. LinesToStr([
  6657. 'var cA = 1;',
  6658. 'var cB = 2;',
  6659. 'var csA = 3;',
  6660. 'var cB$1 = 4;',
  6661. 'this.DoIt = function () {',
  6662. ' function Sub() {',
  6663. ' cB$1 = cB$1 + 3;',
  6664. ' cA = cA + 3 + 5;',
  6665. ' };',
  6666. ' cA = cA + 2 + 6;',
  6667. '};'
  6668. ]),
  6669. LinesToStr([
  6670. ]));
  6671. end;
  6672. procedure TTestModule.TestVarExternal;
  6673. begin
  6674. StartProgram(false);
  6675. Add('var');
  6676. Add(' NaN: double; external name ''Global.NaN'';');
  6677. Add(' d: double;');
  6678. Add('begin');
  6679. Add(' d:=NaN;');
  6680. ConvertProgram;
  6681. CheckSource('TestVarExternal',
  6682. LinesToStr([
  6683. 'this.d = 0.0;'
  6684. ]),
  6685. LinesToStr([
  6686. '$mod.d = Global.NaN;'
  6687. ]));
  6688. end;
  6689. procedure TTestModule.TestVarExternalOtherUnit;
  6690. begin
  6691. AddModuleWithIntfImplSrc('unit2.pas',
  6692. LinesToStr([
  6693. 'var NaN: double; external name ''Global.NaN'';',
  6694. 'var iV: longint;'
  6695. ]),
  6696. '');
  6697. StartUnit(true);
  6698. Add('interface');
  6699. Add('uses unit2;');
  6700. Add('implementation');
  6701. Add('var');
  6702. Add(' d: double;');
  6703. Add(' i: longint; external name ''$i'';');
  6704. Add('begin');
  6705. Add(' d:=nan;');
  6706. Add(' d:=uNit2.nan;');
  6707. Add(' d:=test1.d;');
  6708. Add(' i:=iv;');
  6709. Add(' i:=uNit2.iv;');
  6710. Add(' i:=test1.i;');
  6711. ConvertUnit;
  6712. CheckSource('TestVarExternalOtherUnit',
  6713. LinesToStr([
  6714. 'var $impl = $mod.$impl;',
  6715. '']),
  6716. LinesToStr([ // this.$init
  6717. '$impl.d = Global.NaN;',
  6718. '$impl.d = Global.NaN;',
  6719. '$impl.d = $impl.d;',
  6720. '$i = pas.unit2.iV;',
  6721. '$i = pas.unit2.iV;',
  6722. '$i = $i;',
  6723. '']),
  6724. LinesToStr([ // implementation
  6725. '$impl.d = 0.0;',
  6726. '']) );
  6727. end;
  6728. procedure TTestModule.TestVarAbsoluteFail;
  6729. begin
  6730. StartProgram(false);
  6731. Add([
  6732. 'var',
  6733. ' a: longint;',
  6734. ' b: longword absolute a;',
  6735. 'begin']);
  6736. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6737. ConvertProgram;
  6738. end;
  6739. procedure TTestModule.TestConstExternal;
  6740. begin
  6741. StartProgram(false);
  6742. Add([
  6743. 'const',
  6744. ' PI: double; external name ''Global.PI'';',
  6745. ' Tau = 2*pi;',
  6746. 'var d: double;',
  6747. 'begin',
  6748. ' d:=pi;',
  6749. ' d:=tau+pi;']);
  6750. ConvertProgram;
  6751. CheckSource('TestConstExternal',
  6752. LinesToStr([
  6753. 'this.Tau = 2*Global.PI;',
  6754. 'this.d = 0.0;'
  6755. ]),
  6756. LinesToStr([
  6757. '$mod.d = Global.PI;',
  6758. '$mod.d = $mod.Tau + Global.PI;'
  6759. ]));
  6760. end;
  6761. procedure TTestModule.TestDouble;
  6762. begin
  6763. StartProgram(false);
  6764. Add([
  6765. 'type',
  6766. ' TDateTime = double;',
  6767. 'const',
  6768. ' a = TDateTime(2.7);',
  6769. ' b = a + TDateTime(1.7);',
  6770. ' c = 0.9 + 0.1;',
  6771. ' f0_1 = 0.1;',
  6772. ' f0_3 = 0.3;',
  6773. ' fn0_1 = -0.1;',
  6774. ' fn0_3 = -0.3;',
  6775. ' fn0_003 = -0.003;',
  6776. ' fn0_123456789 = -0.123456789;',
  6777. ' fn300_0 = -300.0;',
  6778. ' fn123456_0 = -123456.0;',
  6779. ' fn1234567_8 = -1234567.8;',
  6780. ' fn12345678_9 = -12345678.9;',
  6781. ' f1_0En12 = 1E-12;',
  6782. ' fn1_0En12 = -1E-12;',
  6783. ' maxdouble = 1.7e+308;',
  6784. ' mindouble = -1.7e+308;',
  6785. ' MinSafeIntDouble = -$1fffffffffffff;',
  6786. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6787. ' MaxSafeIntDouble = $1fffffffffffff;',
  6788. ' DZeroResolution = 1E-12;',
  6789. ' Minus1 = -1E-12;',
  6790. ' EPS = 1E-9;',
  6791. ' DELTA = 0.001;',
  6792. ' Big = 129.789E+100;',
  6793. ' Test0_15 = 0.15;',
  6794. ' Test999 = 2.9999999999999;',
  6795. ' Test111999 = 211199999999999000.0;',
  6796. ' TestMinus111999 = -211199999999999000.0;',
  6797. 'var',
  6798. ' d: double = b;',
  6799. 'begin',
  6800. ' d:=1.0;',
  6801. ' d:=1.0/3.0;',
  6802. ' d:=1/3;',
  6803. ' d:=5.0E-324;',
  6804. ' d:=1.7E308;',
  6805. ' d:=001.00E00;',
  6806. ' d:=002.00E001;',
  6807. ' d:=003.000E000;',
  6808. ' d:=-004.00E-00;',
  6809. ' d:=-005.00E-001;',
  6810. ' d:=10**3;',
  6811. ' d:=10 mod 3;',
  6812. ' d:=10 div 3;',
  6813. ' d:=c;',
  6814. ' d:=f0_1;',
  6815. ' d:=f0_3;',
  6816. ' d:=fn0_1;',
  6817. ' d:=fn0_3;',
  6818. ' d:=fn0_003;',
  6819. ' d:=fn0_123456789;',
  6820. ' d:=fn300_0;',
  6821. ' d:=fn123456_0;',
  6822. ' d:=fn1234567_8;',
  6823. ' d:=fn12345678_9;',
  6824. ' d:=f1_0En12;',
  6825. ' d:=fn1_0En12;',
  6826. ' d:=maxdouble;',
  6827. ' d:=mindouble;',
  6828. ' d:=MinSafeIntDouble;',
  6829. ' d:=double(MinSafeIntDouble);',
  6830. ' d:=MinSafeIntDouble2;',
  6831. ' d:=double(MinSafeIntDouble2);',
  6832. ' d:=MaxSafeIntDouble;',
  6833. ' d:=default(double);',
  6834. '']);
  6835. ConvertProgram;
  6836. CheckSource('TestDouble',
  6837. LinesToStr([
  6838. 'this.a = 2.7;',
  6839. 'this.b = 2.7 + 1.7;',
  6840. 'this.c = 0.9 + 0.1;',
  6841. 'this.f0_1 = 0.1;',
  6842. 'this.f0_3 = 0.3;',
  6843. 'this.fn0_1 = -0.1;',
  6844. 'this.fn0_3 = -0.3;',
  6845. 'this.fn0_003 = -0.003;',
  6846. 'this.fn0_123456789 = -0.123456789;',
  6847. 'this.fn300_0 = -300.0;',
  6848. 'this.fn123456_0 = -123456.0;',
  6849. 'this.fn1234567_8 = -1234567.8;',
  6850. 'this.fn12345678_9 = -12345678.9;',
  6851. 'this.f1_0En12 = 1E-12;',
  6852. 'this.fn1_0En12 = -1E-12;',
  6853. 'this.maxdouble = 1.7e+308;',
  6854. 'this.mindouble = -1.7e+308;',
  6855. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6856. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6857. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6858. 'this.DZeroResolution = 1E-12;',
  6859. 'this.Minus1 = -1E-12;',
  6860. 'this.EPS = 1E-9;',
  6861. 'this.DELTA = 0.001;',
  6862. 'this.Big = 129.789E+100;',
  6863. 'this.Test0_15 = 0.15;',
  6864. 'this.Test999 = 2.9999999999999;',
  6865. 'this.Test111999 = 211199999999999000.0;',
  6866. 'this.TestMinus111999 = -211199999999999000.0;',
  6867. 'this.d = 4.4;'
  6868. ]),
  6869. LinesToStr([
  6870. '$mod.d = 1.0;',
  6871. '$mod.d = 1.0 / 3.0;',
  6872. '$mod.d = 1 / 3;',
  6873. '$mod.d = 5.0E-324;',
  6874. '$mod.d = 1.7E308;',
  6875. '$mod.d = 1.00E0;',
  6876. '$mod.d = 2.00E1;',
  6877. '$mod.d = 3.000E0;',
  6878. '$mod.d = -4.00E-0;',
  6879. '$mod.d = -5.00E-1;',
  6880. '$mod.d = Math.pow(10, 3);',
  6881. '$mod.d = 10 % 3;',
  6882. '$mod.d = rtl.trunc(10 / 3);',
  6883. '$mod.d = 1;',
  6884. '$mod.d = 0.1;',
  6885. '$mod.d = 0.3;',
  6886. '$mod.d = -0.1;',
  6887. '$mod.d = -0.3;',
  6888. '$mod.d = -0.003;',
  6889. '$mod.d = -0.123456789;',
  6890. '$mod.d = -300;',
  6891. '$mod.d = -123456;',
  6892. '$mod.d = -1234567.8;',
  6893. '$mod.d = -1.23456789E7;',
  6894. '$mod.d = 1E-12;',
  6895. '$mod.d = -1E-12;',
  6896. '$mod.d = 1.7E308;',
  6897. '$mod.d = -1.7E308;',
  6898. '$mod.d = -9007199254740991;',
  6899. '$mod.d = -9007199254740991;',
  6900. '$mod.d = -9.007199254740992E15;',
  6901. '$mod.d = -9.007199254740992E15;',
  6902. '$mod.d = 9007199254740991;',
  6903. '$mod.d = 0.0;',
  6904. '']));
  6905. end;
  6906. procedure TTestModule.TestInteger;
  6907. begin
  6908. StartProgram(false);
  6909. Add([
  6910. 'const',
  6911. ' MinInt = low(NativeInt);',
  6912. ' MaxInt = high(NativeInt);',
  6913. 'type',
  6914. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6915. 'const',
  6916. ' a = low(TMyInt)+High(TMyInt);',
  6917. 'var',
  6918. ' i: TMyInt;',
  6919. 'begin',
  6920. ' i:=-MinInt;',
  6921. ' i:=default(TMyInt);',
  6922. ' i:=low(i)+high(i);',
  6923. '']);
  6924. ConvertProgram;
  6925. CheckSource('TestIntegerRange',
  6926. LinesToStr([
  6927. 'this.MinInt = -9007199254740991;',
  6928. 'this.MaxInt = 9007199254740991;',
  6929. 'this.a = -9007199254740991 + 9007199254740991;',
  6930. 'this.i = 0;',
  6931. '']),
  6932. LinesToStr([
  6933. '$mod.i = - -9007199254740991;',
  6934. '$mod.i = -9007199254740991;',
  6935. '$mod.i = -9007199254740991 + 9007199254740991;',
  6936. '']));
  6937. end;
  6938. procedure TTestModule.TestIntegerRange;
  6939. begin
  6940. StartProgram(false);
  6941. Add([
  6942. 'const',
  6943. ' MinInt = -1;',
  6944. ' MaxInt = +1;',
  6945. 'type',
  6946. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6947. ' TInt2 = 1..3;',
  6948. 'const',
  6949. ' a = low(TMyInt)+High(TMyInt);',
  6950. ' b = low(TInt2)+High(TInt2);',
  6951. ' s1 = [1];',
  6952. ' s2 = [1,2];',
  6953. ' s3 = [1..3];',
  6954. ' s4 = [low(shortint)..high(shortint)];',
  6955. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6956. ' s6 = 1 in s2;',
  6957. 'var',
  6958. ' i: TMyInt;',
  6959. ' i2: TInt2;',
  6960. 'begin',
  6961. ' i:=i2;',
  6962. ' i:=default(TMyInt);',
  6963. ' if i=i2 then ;',
  6964. ' i:=ord(i2);',
  6965. '']);
  6966. ConvertProgram;
  6967. CheckSource('TestIntegerRange',
  6968. LinesToStr([
  6969. 'this.MinInt = -1;',
  6970. 'this.MaxInt = +1;',
  6971. 'this.a = -1 + 1;',
  6972. 'this.b = 1 + 3;',
  6973. 'this.s1 = rtl.createSet(1);',
  6974. 'this.s2 = rtl.createSet(1, 2);',
  6975. 'this.s3 = rtl.createSet(null, 1, 3);',
  6976. 'this.s4 = rtl.createSet(null, -128, 127);',
  6977. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6978. 'this.s6 = 1 in this.s2;',
  6979. 'this.i = 0;',
  6980. 'this.i2 = 0;',
  6981. '']),
  6982. LinesToStr([
  6983. '$mod.i = $mod.i2;',
  6984. '$mod.i = -1;',
  6985. 'if ($mod.i === $mod.i2) ;',
  6986. '$mod.i = $mod.i2;',
  6987. '']));
  6988. end;
  6989. procedure TTestModule.TestIntegerTypecasts;
  6990. begin
  6991. StartProgram(false);
  6992. Add([
  6993. 'var',
  6994. ' i: nativeint;',
  6995. ' b: byte;',
  6996. ' sh: shortint;',
  6997. ' w: word;',
  6998. ' sm: smallint;',
  6999. ' lw: longword;',
  7000. ' li: longint;',
  7001. 'begin',
  7002. ' b:=byte(i);',
  7003. ' sh:=shortint(i);',
  7004. ' w:=word(i);',
  7005. ' sm:=smallint(i);',
  7006. ' lw:=longword(i);',
  7007. ' li:=longint(i);',
  7008. '']);
  7009. ConvertProgram;
  7010. CheckSource('TestIntegerTypecasts',
  7011. LinesToStr([
  7012. 'this.i = 0;',
  7013. 'this.b = 0;',
  7014. 'this.sh = 0;',
  7015. 'this.w = 0;',
  7016. 'this.sm = 0;',
  7017. 'this.lw = 0;',
  7018. 'this.li = 0;',
  7019. '']),
  7020. LinesToStr([
  7021. '$mod.b = $mod.i & 255;',
  7022. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7023. '$mod.w = $mod.i & 65535;',
  7024. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7025. '$mod.lw = $mod.i >>> 0;',
  7026. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7027. '']));
  7028. end;
  7029. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7030. begin
  7031. StartProgram(false);
  7032. Add([
  7033. 'var',
  7034. ' i,j: nativeint;',
  7035. 'begin',
  7036. ' i:=i shr 0;',
  7037. ' i:=i shr 1;',
  7038. ' i:=i shr 3;',
  7039. ' i:=i shr 54;',
  7040. ' i:=j shr i;',
  7041. '']);
  7042. ConvertProgram;
  7043. CheckResolverUnexpectedHints;
  7044. CheckSource('TestInteger_BitwiseShrNativeInt',
  7045. LinesToStr([
  7046. 'this.i = 0;',
  7047. 'this.j = 0;',
  7048. '']),
  7049. LinesToStr([
  7050. '$mod.i = $mod.i;',
  7051. '$mod.i = Math.floor($mod.i / 2);',
  7052. '$mod.i = Math.floor($mod.i / 8);',
  7053. '$mod.i = 0;',
  7054. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7055. '']));
  7056. end;
  7057. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7058. begin
  7059. StartProgram(false);
  7060. Add([
  7061. 'var',
  7062. ' i: nativeint;',
  7063. 'begin',
  7064. ' i:=i shl 0;',
  7065. ' i:=i shl 54;',
  7066. ' i:=123456789012 shl 1;',
  7067. ' i:=i shl 1;',
  7068. '']);
  7069. ConvertProgram;
  7070. CheckResolverUnexpectedHints;
  7071. CheckSource('TestInteger_BitwiseShrNativeInt',
  7072. LinesToStr([
  7073. 'this.i = 0;',
  7074. '']),
  7075. LinesToStr([
  7076. '$mod.i = $mod.i;',
  7077. '$mod.i = 0;',
  7078. '$mod.i = 246913578024;',
  7079. '$mod.i = rtl.shl($mod.i, 1);',
  7080. '']));
  7081. end;
  7082. procedure TTestModule.TestInteger_SystemFunc;
  7083. begin
  7084. StartProgram(true);
  7085. Add([
  7086. 'var',
  7087. ' i: byte;',
  7088. ' s: string;',
  7089. 'begin',
  7090. ' system.inc(i);',
  7091. ' system.str(i,s);',
  7092. ' s:=system.str(i);',
  7093. ' i:=system.low(i);',
  7094. ' i:=system.high(i);',
  7095. ' i:=system.pred(i);',
  7096. ' i:=system.succ(i);',
  7097. ' i:=system.ord(i);',
  7098. '']);
  7099. ConvertProgram;
  7100. CheckResolverUnexpectedHints;
  7101. CheckSource('TestInteger_SystemFunc',
  7102. LinesToStr([
  7103. 'this.i = 0;',
  7104. 'this.s = "";',
  7105. '']),
  7106. LinesToStr([
  7107. '$mod.i += 1;',
  7108. '$mod.s = "" + $mod.i;',
  7109. '$mod.s = "" + $mod.i;',
  7110. '$mod.i = 0;',
  7111. '$mod.i = 255;',
  7112. '$mod.i = $mod.i - 1;',
  7113. '$mod.i = $mod.i + 1;',
  7114. '$mod.i = $mod.i;',
  7115. '']));
  7116. end;
  7117. procedure TTestModule.TestCurrency;
  7118. begin
  7119. StartProgram(false);
  7120. Add([
  7121. 'type',
  7122. ' TCoin = currency;',
  7123. 'const',
  7124. ' a = TCoin(2.7);',
  7125. ' b = a + TCoin(1.7);',
  7126. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7127. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7128. 'var',
  7129. ' c: TCoin = b;',
  7130. ' i: nativeint;',
  7131. ' d: double;',
  7132. ' j: jsvalue;',
  7133. 'function DoIt(c: currency): currency; begin end;',
  7134. 'function GetIt(d: double): double; begin end;',
  7135. 'procedure Write(v: jsvalue); begin end;',
  7136. 'begin',
  7137. ' c:=1.0;',
  7138. ' c:=0.1;',
  7139. ' c:=1.0/3.0;',
  7140. ' c:=1/3;',
  7141. ' c:=a;',
  7142. ' d:=c;',
  7143. ' c:=d;',
  7144. ' c:=currency(c);',
  7145. ' c:=currency(d);',
  7146. ' d:=double(c);',
  7147. ' c:=i;',
  7148. ' c:=currency(i);',
  7149. //' i:=c;', not allowed
  7150. ' i:=nativeint(c);',
  7151. ' c:=c+a;',
  7152. ' c:=-c-a;',
  7153. ' c:=d+c;',
  7154. ' c:=c+d;',
  7155. ' c:=d-c;',
  7156. ' c:=c-d;',
  7157. ' c:=c*a;',
  7158. ' c:=a*c;',
  7159. ' c:=d*c;',
  7160. ' c:=c*d;',
  7161. ' c:=c/a;',
  7162. ' c:=a/c;',
  7163. ' c:=d/c;',
  7164. ' c:=c/d;',
  7165. ' c:=c**a;',
  7166. ' c:=a**c;',
  7167. ' c:=d**c;',
  7168. ' c:=c**d;',
  7169. ' if c=c then ;',
  7170. ' if c=a then ;',
  7171. ' if a=c then ;',
  7172. ' if d=c then ;',
  7173. ' if c=d then ;',
  7174. ' c:=DoIt(c);',
  7175. ' c:=DoIt(i);',
  7176. ' c:=DoIt(d);',
  7177. ' c:=GetIt(c);',
  7178. ' j:=c;',
  7179. ' Write(c);',
  7180. ' c:=default(currency);',
  7181. ' j:=str(c);',
  7182. ' j:=str(c:0:3);',
  7183. '']);
  7184. ConvertProgram;
  7185. CheckSource('TestCurrency',
  7186. LinesToStr([
  7187. 'this.a = 27000;',
  7188. 'this.b = this.a + 17000;',
  7189. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7190. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7191. 'this.c = this.b;',
  7192. 'this.i = 0;',
  7193. 'this.d = 0.0;',
  7194. 'this.j = undefined;',
  7195. 'this.DoIt = function (c) {',
  7196. ' var Result = 0;',
  7197. ' return Result;',
  7198. '};',
  7199. 'this.GetIt = function (d) {',
  7200. ' var Result = 0.0;',
  7201. ' return Result;',
  7202. '};',
  7203. 'this.Write = function (v) {',
  7204. '};',
  7205. '']),
  7206. LinesToStr([
  7207. '$mod.c = 10000;',
  7208. '$mod.c = 1000;',
  7209. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7210. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7211. '$mod.c = $mod.a;',
  7212. '$mod.d = $mod.c / 10000;',
  7213. '$mod.c = rtl.trunc($mod.d * 10000);',
  7214. '$mod.c = $mod.c;',
  7215. '$mod.c = $mod.d * 10000;',
  7216. '$mod.d = $mod.c / 10000;',
  7217. '$mod.c = $mod.i * 10000;',
  7218. '$mod.c = $mod.i * 10000;',
  7219. '$mod.i = rtl.trunc($mod.c / 10000);',
  7220. '$mod.c = $mod.c + $mod.a;',
  7221. '$mod.c = -$mod.c - $mod.a;',
  7222. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7223. '$mod.c = $mod.c + ($mod.d * 10000);',
  7224. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7225. '$mod.c = $mod.c - ($mod.d * 10000);',
  7226. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7227. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7228. '$mod.c = $mod.d * $mod.c;',
  7229. '$mod.c = $mod.c * $mod.d;',
  7230. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7231. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7232. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7233. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7234. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7235. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7236. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7237. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7238. 'if ($mod.c === $mod.c) ;',
  7239. 'if ($mod.c === $mod.a) ;',
  7240. 'if ($mod.a === $mod.c) ;',
  7241. 'if (($mod.d * 10000) === $mod.c) ;',
  7242. 'if ($mod.c === ($mod.d * 10000)) ;',
  7243. '$mod.c = $mod.DoIt($mod.c);',
  7244. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7245. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7246. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7247. '$mod.j = $mod.c / 10000;',
  7248. '$mod.Write($mod.c / 10000);',
  7249. '$mod.c = 0;',
  7250. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7251. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7252. '']));
  7253. end;
  7254. procedure TTestModule.TestForBoolDo;
  7255. begin
  7256. StartProgram(false);
  7257. Add([
  7258. 'var b: boolean;',
  7259. 'begin',
  7260. ' for b:=false to true do ;',
  7261. ' for b:=b downto false do ;',
  7262. ' for b in boolean do ;',
  7263. '']);
  7264. ConvertProgram;
  7265. CheckSource('TestForBoolDo',
  7266. LinesToStr([ // statements
  7267. 'this.b = false;']),
  7268. LinesToStr([ // this.$main
  7269. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7270. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7271. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7272. '']));
  7273. end;
  7274. procedure TTestModule.TestForIntDo;
  7275. begin
  7276. StartProgram(false);
  7277. Add([
  7278. 'var i: longint;',
  7279. 'begin',
  7280. ' for i:=3 to 5 do ;',
  7281. ' for i:=i downto 2 do ;',
  7282. ' for i in byte do ;',
  7283. '']);
  7284. ConvertProgram;
  7285. CheckSource('TestForIntDo',
  7286. LinesToStr([ // statements
  7287. 'this.i = 0;']),
  7288. LinesToStr([ // this.$main
  7289. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7290. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7291. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7292. '']));
  7293. end;
  7294. procedure TTestModule.TestForIntInDo;
  7295. begin
  7296. StartProgram(false);
  7297. Add([
  7298. 'type',
  7299. ' TSetOfInt = set of byte;',
  7300. ' TIntRg = 3..7;',
  7301. ' TSetOfIntRg = set of TIntRg;',
  7302. 'var',
  7303. ' i,i2: longint;',
  7304. ' a1: array of byte;',
  7305. ' a2: array[1..3] of byte;',
  7306. ' soi: TSetOfInt;',
  7307. ' soir: TSetOfIntRg;',
  7308. ' ir: TIntRg;',
  7309. 'begin',
  7310. ' for i in byte do ;',
  7311. ' for i in a1 do ;',
  7312. ' for i in a2 do ;',
  7313. ' for i in [11..13] do ;',
  7314. ' for i in TSetOfInt do ;',
  7315. ' for i in TIntRg do ;',
  7316. ' for i in soi do i2:=i;',
  7317. ' for i in TSetOfIntRg do ;',
  7318. ' for i in soir do ;',
  7319. ' for ir in TIntRg do ;',
  7320. ' for ir in TSetOfIntRg do ;',
  7321. ' for ir in soir do ;',
  7322. '']);
  7323. ConvertProgram;
  7324. CheckSource('TestForIntInDo',
  7325. LinesToStr([ // statements
  7326. 'this.i = 0;',
  7327. 'this.i2 = 0;',
  7328. 'this.a1 = [];',
  7329. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7330. 'this.soi = {};',
  7331. 'this.soir = {};',
  7332. 'this.ir = 0;',
  7333. '']),
  7334. LinesToStr([ // this.$main
  7335. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7336. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7337. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7338. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7339. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7340. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7341. 'for (var $l6 in $mod.soi) {',
  7342. ' $mod.i = +$l6;',
  7343. ' $mod.i2 = $mod.i;',
  7344. '};',
  7345. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7346. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7347. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7348. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7349. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7350. '']));
  7351. end;
  7352. procedure TTestModule.TestCharConst;
  7353. begin
  7354. StartProgram(false);
  7355. Add([
  7356. 'const',
  7357. ' a = #$00F3;',
  7358. ' c: char = ''1'';',
  7359. ' wc: widechar = ''ä'';',
  7360. 'begin',
  7361. ' c:=#0;',
  7362. ' c:=#1;',
  7363. ' c:=#9;',
  7364. ' c:=#10;',
  7365. ' c:=#13;',
  7366. ' c:=#31;',
  7367. ' c:=#32;',
  7368. ' c:=#$A;',
  7369. ' c:=#$0A;',
  7370. ' c:=#$b;',
  7371. ' c:=#$0b;',
  7372. ' c:=^A;',
  7373. ' c:=''"'';',
  7374. ' c:=default(char);',
  7375. ' c:=#$00E4;', // ä
  7376. ' c:=''ä'';',
  7377. ' c:=#$E4;', // ä
  7378. ' c:=#$D800;', // invalid UTF-16
  7379. ' c:=#$DFFF;', // invalid UTF-16
  7380. ' c:=#$FFFF;', // last UCS-2
  7381. ' c:=high(c);', // last UCS-2
  7382. ' c:=#269;',
  7383. '']);
  7384. ConvertProgram;
  7385. CheckSource('TestCharConst',
  7386. LinesToStr([
  7387. 'this.a="ó";',
  7388. 'this.c="1";',
  7389. 'this.wc="ä";'
  7390. ]),
  7391. LinesToStr([
  7392. '$mod.c="\x00";',
  7393. '$mod.c="\x01";',
  7394. '$mod.c="\t";',
  7395. '$mod.c="\n";',
  7396. '$mod.c="\r";',
  7397. '$mod.c="\x1F";',
  7398. '$mod.c=" ";',
  7399. '$mod.c="\n";',
  7400. '$mod.c="\n";',
  7401. '$mod.c="\x0B";',
  7402. '$mod.c="\x0B";',
  7403. '$mod.c="\x01";',
  7404. '$mod.c=''"'';',
  7405. '$mod.c="\x00";',
  7406. '$mod.c = "ä";',
  7407. '$mod.c = "ä";',
  7408. '$mod.c = "ä";',
  7409. '$mod.c="\uD800";',
  7410. '$mod.c="\uDFFF";',
  7411. '$mod.c="\uFFFF";',
  7412. '$mod.c="\uFFFF";',
  7413. '$mod.c = "č";',
  7414. '']));
  7415. end;
  7416. procedure TTestModule.TestChar_Compare;
  7417. begin
  7418. StartProgram(false);
  7419. Add('var');
  7420. Add(' c: char;');
  7421. Add(' b: boolean;');
  7422. Add('begin');
  7423. Add(' b:=c=''1'';');
  7424. Add(' b:=''2''=c;');
  7425. Add(' b:=''3''=''4'';');
  7426. Add(' b:=c<>''5'';');
  7427. Add(' b:=''6''<>c;');
  7428. Add(' b:=c>''7'';');
  7429. Add(' b:=''8''>c;');
  7430. Add(' b:=c>=''9'';');
  7431. Add(' b:=''A''>=c;');
  7432. Add(' b:=c<''B'';');
  7433. Add(' b:=''C''<c;');
  7434. Add(' b:=c<=''D'';');
  7435. Add(' b:=''E''<=c;');
  7436. ConvertProgram;
  7437. CheckSource('TestChar_Compare',
  7438. LinesToStr([
  7439. 'this.c="";',
  7440. 'this.b = false;'
  7441. ]),
  7442. LinesToStr([
  7443. '$mod.b = $mod.c === "1";',
  7444. '$mod.b = "2" === $mod.c;',
  7445. '$mod.b = "3" === "4";',
  7446. '$mod.b = $mod.c !== "5";',
  7447. '$mod.b = "6" !== $mod.c;',
  7448. '$mod.b = $mod.c > "7";',
  7449. '$mod.b = "8" > $mod.c;',
  7450. '$mod.b = $mod.c >= "9";',
  7451. '$mod.b = "A" >= $mod.c;',
  7452. '$mod.b = $mod.c < "B";',
  7453. '$mod.b = "C" < $mod.c;',
  7454. '$mod.b = $mod.c <= "D";',
  7455. '$mod.b = "E" <= $mod.c;',
  7456. '']));
  7457. end;
  7458. procedure TTestModule.TestChar_BuiltInProcs;
  7459. begin
  7460. StartProgram(false);
  7461. Add([
  7462. 'var',
  7463. ' c: char;',
  7464. ' i: longint;',
  7465. ' s: string;',
  7466. 'begin',
  7467. ' i:=ord(c);',
  7468. ' i:=ord(s[i]);',
  7469. ' c:=chr(i);',
  7470. ' c:=pred(c);',
  7471. ' c:=succ(c);',
  7472. ' c:=low(c);',
  7473. ' c:=high(c);',
  7474. ' i:=byte(c);',
  7475. ' i:=word(c);',
  7476. ' i:=longint(c);',
  7477. '']);
  7478. ConvertProgram;
  7479. CheckSource('TestChar_BuiltInProcs',
  7480. LinesToStr([
  7481. 'this.c = "";',
  7482. 'this.i = 0;',
  7483. 'this.s = "";'
  7484. ]),
  7485. LinesToStr([
  7486. '$mod.i = $mod.c.charCodeAt();',
  7487. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7488. '$mod.c = String.fromCharCode($mod.i);',
  7489. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7490. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7491. '$mod.c = "\x00";',
  7492. '$mod.c = "\uFFFF";',
  7493. '$mod.i = $mod.c.charCodeAt() & 255;',
  7494. '$mod.i = $mod.c.charCodeAt();',
  7495. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7496. '']));
  7497. end;
  7498. procedure TTestModule.TestStringConst;
  7499. begin
  7500. StartProgram(false);
  7501. Add([
  7502. '{$H+}',
  7503. 'const',
  7504. ' a = #$00F3#$017C;', // first <256, then >=256
  7505. ' b = string(''a'');',
  7506. ' c = string(''ä'');',
  7507. ' d = UnicodeString(''b'');',
  7508. ' e = UnicodeString(''ö'');',
  7509. 'var',
  7510. ' s: string = ''abc'';',
  7511. 'begin',
  7512. ' s:='''';',
  7513. ' s:=#13#10;',
  7514. ' s:=#9''foo'';',
  7515. ' s:=#$A9;',
  7516. ' s:=''foo''#13''bar'';',
  7517. ' s:=''"'';',
  7518. ' s:=''"''''"'';',
  7519. ' s:=#$20AC;', // euro
  7520. ' s:=#$10437;', // outside BMP
  7521. ' s:=''abc''#$20AC;', // ascii,#
  7522. ' s:=''ä''#$20AC;', // non ascii,#
  7523. ' s:=#$20AC''abc'';', // #, ascii
  7524. ' s:=#$20AC''ä'';', // #, non ascii
  7525. ' s:=default(string);',
  7526. ' s:=concat(s);',
  7527. ' s:=concat(s,''a'',s);',
  7528. ' s:=#250#269;',
  7529. //' s:=#$2F804;',
  7530. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7531. '']);
  7532. ConvertProgram;
  7533. CheckSource('TestStringConst',
  7534. LinesToStr([
  7535. 'this.a = "óż";',
  7536. 'this.b = "a";',
  7537. 'this.c = "ä";',
  7538. 'this.d = "b";',
  7539. 'this.e = "ö";',
  7540. 'this.s="abc";',
  7541. '']),
  7542. LinesToStr([
  7543. '$mod.s="";',
  7544. '$mod.s="\r\n";',
  7545. '$mod.s="\tfoo";',
  7546. '$mod.s="©";',
  7547. '$mod.s="foo\rbar";',
  7548. '$mod.s=''"'';',
  7549. '$mod.s=''"\''"'';',
  7550. '$mod.s="€";',
  7551. '$mod.s="'#$F0#$90#$90#$B7'";',
  7552. '$mod.s = "abc€";',
  7553. '$mod.s = "ä€";',
  7554. '$mod.s = "€abc";',
  7555. '$mod.s = "ۊ";',
  7556. '$mod.s="";',
  7557. '$mod.s = $mod.s;',
  7558. '$mod.s = $mod.s.concat("a", $mod.s);',
  7559. '$mod.s = "úč";',
  7560. '']));
  7561. end;
  7562. procedure TTestModule.TestStringConst_InvalidUTF16;
  7563. begin
  7564. StartProgram(false);
  7565. Add([
  7566. 'const',
  7567. ' a: char = #$D87E;',
  7568. ' b: string = #$D87E;',
  7569. ' c: string = #$D87E#43;',
  7570. 'begin',
  7571. ' c:=''abc''#$D87E;',
  7572. ' c:=#0#1#2;',
  7573. ' c:=#127;',
  7574. ' c:=#128;',
  7575. ' c:=#255;',
  7576. ' c:=#256;',
  7577. '']);
  7578. ConvertProgram;
  7579. CheckSource('TestStringConst',
  7580. LinesToStr([
  7581. 'this.a = "\uD87E";',
  7582. 'this.b = "\uD87E";',
  7583. 'this.c = "\uD87E+";',
  7584. '']),
  7585. LinesToStr([
  7586. '$mod.c = "abc\uD87E";',
  7587. '$mod.c = "\x00\x01\x02";',
  7588. '$mod.c = "'#127'";',
  7589. '$mod.c = "'#$c2#$80'";',
  7590. '$mod.c = "'#$c3#$BF'";',
  7591. '$mod.c = "'#$c4#$80'";',
  7592. '']));
  7593. end;
  7594. procedure TTestModule.TestStringConstSurrogate;
  7595. begin
  7596. StartProgram(false);
  7597. Add([
  7598. 'var',
  7599. ' s: string;',
  7600. 'begin',
  7601. ' s:=''😊'';', // 1F60A
  7602. '']);
  7603. ConvertProgram;
  7604. CheckSource('TestStringConstSurrogate',
  7605. LinesToStr([
  7606. 'this.s="";'
  7607. ]),
  7608. LinesToStr([
  7609. '$mod.s="😊";'
  7610. ]));
  7611. end;
  7612. procedure TTestModule.TestString_Length;
  7613. begin
  7614. StartProgram(false);
  7615. Add('const c = ''foo'';');
  7616. Add('var');
  7617. Add(' s: string;');
  7618. Add(' i: longint;');
  7619. Add('begin');
  7620. Add(' i:=length(s);');
  7621. Add(' i:=length(s+s);');
  7622. Add(' i:=length(''abc'');');
  7623. Add(' i:=length(c);');
  7624. ConvertProgram;
  7625. CheckSource('TestString_Length',
  7626. LinesToStr([
  7627. 'this.c = "foo";',
  7628. 'this.s = "";',
  7629. 'this.i = 0;',
  7630. '']),
  7631. LinesToStr([
  7632. '$mod.i = $mod.s.length;',
  7633. '$mod.i = ($mod.s+$mod.s).length;',
  7634. '$mod.i = "abc".length;',
  7635. '$mod.i = $mod.c.length;',
  7636. '']));
  7637. end;
  7638. procedure TTestModule.TestString_Compare;
  7639. begin
  7640. StartProgram(false);
  7641. Add('var');
  7642. Add(' s, t: string;');
  7643. Add(' b: boolean;');
  7644. Add('begin');
  7645. Add(' b:=s=t;');
  7646. Add(' b:=s<>t;');
  7647. Add(' b:=s>t;');
  7648. Add(' b:=s>=t;');
  7649. Add(' b:=s<t;');
  7650. Add(' b:=s<=t;');
  7651. ConvertProgram;
  7652. CheckSource('TestString_Compare',
  7653. LinesToStr([ // statements
  7654. 'this.s = "";',
  7655. 'this.t = "";',
  7656. 'this.b =false;'
  7657. ]),
  7658. LinesToStr([ // this.$main
  7659. '$mod.b = $mod.s === $mod.t;',
  7660. '$mod.b = $mod.s !== $mod.t;',
  7661. '$mod.b = $mod.s > $mod.t;',
  7662. '$mod.b = $mod.s >= $mod.t;',
  7663. '$mod.b = $mod.s < $mod.t;',
  7664. '$mod.b = $mod.s <= $mod.t;',
  7665. '']));
  7666. end;
  7667. procedure TTestModule.TestString_SetLength;
  7668. begin
  7669. StartProgram(false);
  7670. Add([
  7671. 'procedure DoIt(var s: string);',
  7672. 'begin',
  7673. ' SetLength(s,2);',
  7674. 'end;',
  7675. 'var s: string;',
  7676. 'begin',
  7677. ' SetLength(s,3);',
  7678. '']);
  7679. ConvertProgram;
  7680. CheckSource('TestString_SetLength',
  7681. LinesToStr([ // statements
  7682. 'this.DoIt = function (s) {',
  7683. ' s.set(rtl.strSetLength(s.get(), 2));',
  7684. '};',
  7685. 'this.s = "";',
  7686. '']),
  7687. LinesToStr([ // this.$main
  7688. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7689. ]));
  7690. end;
  7691. procedure TTestModule.TestString_CharAt;
  7692. begin
  7693. StartProgram(false);
  7694. Add([
  7695. 'var',
  7696. ' s: string;',
  7697. ' c: char;',
  7698. ' b: boolean;',
  7699. 'begin',
  7700. ' b:= s[1] = c;',
  7701. ' b:= c = s[1];',
  7702. ' b:= c <> s[1];',
  7703. ' b:= c > s[1];',
  7704. ' b:= c >= s[1];',
  7705. ' b:= c < s[2];',
  7706. ' b:= c <= s[1];',
  7707. ' s[1] := c;',
  7708. ' s[2+3] := c;']);
  7709. ConvertProgram;
  7710. CheckSource('TestString_CharAt',
  7711. LinesToStr([ // statements
  7712. 'this.s = "";',
  7713. 'this.c = "";',
  7714. 'this.b = false;'
  7715. ]),
  7716. LinesToStr([ // this.$main
  7717. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7718. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7719. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7720. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7721. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7722. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7723. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7724. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7725. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7726. '']));
  7727. end;
  7728. procedure TTestModule.TestStringHMinusFail;
  7729. begin
  7730. StartProgram(false);
  7731. Add([
  7732. '{$H-}',
  7733. 'var s: string;',
  7734. 'begin']);
  7735. ConvertProgram;
  7736. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7737. end;
  7738. procedure TTestModule.TestStr;
  7739. begin
  7740. StartProgram(false);
  7741. Add('var');
  7742. Add(' b: boolean;');
  7743. Add(' i: longint;');
  7744. Add(' d: double;');
  7745. Add(' s: string;');
  7746. Add('begin');
  7747. Add(' str(b,s);');
  7748. Add(' str(i,s);');
  7749. Add(' str(d,s);');
  7750. Add(' str(i:3,s);');
  7751. Add(' str(d:3:2,s);');
  7752. Add(' Str(12.456:12:1,s);');
  7753. Add(' Str(12.456:12,s);');
  7754. Add(' s:=str(b);');
  7755. Add(' s:=str(i);');
  7756. Add(' s:=str(d);');
  7757. Add(' s:=str(i,i);');
  7758. Add(' s:=str(i:3);');
  7759. Add(' s:=str(d:3:2);');
  7760. Add(' s:=str(i:4,i);');
  7761. Add(' s:=str(i,i:5);');
  7762. Add(' s:=str(i:4,i:5);');
  7763. Add(' s:=str(s,s);');
  7764. Add(' s:=str(s,''foo'');');
  7765. ConvertProgram;
  7766. CheckSource('TestStr',
  7767. LinesToStr([ // statements
  7768. 'this.b = false;',
  7769. 'this.i = 0;',
  7770. 'this.d = 0.0;',
  7771. 'this.s = "";',
  7772. '']),
  7773. LinesToStr([ // this.$main
  7774. '$mod.s = ""+$mod.b;',
  7775. '$mod.s = ""+$mod.i;',
  7776. '$mod.s = rtl.floatToStr($mod.d);',
  7777. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7778. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7779. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7780. '$mod.s = rtl.floatToStr(12.456,12);',
  7781. '$mod.s = ""+$mod.b;',
  7782. '$mod.s = ""+$mod.i;',
  7783. '$mod.s = rtl.floatToStr($mod.d);',
  7784. '$mod.s = ""+$mod.i+$mod.i;',
  7785. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7786. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7787. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7788. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7789. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7790. '$mod.s = $mod.s + $mod.s;',
  7791. '$mod.s = $mod.s + "foo";',
  7792. '']));
  7793. end;
  7794. procedure TTestModule.TestBaseType_AnsiStringFail;
  7795. begin
  7796. StartProgram(false);
  7797. Add('var s: AnsiString');
  7798. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7799. ConvertProgram;
  7800. end;
  7801. procedure TTestModule.TestBaseType_WideStringFail;
  7802. begin
  7803. StartProgram(false);
  7804. Add('var s: WideString');
  7805. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7806. ConvertProgram;
  7807. end;
  7808. procedure TTestModule.TestBaseType_ShortStringFail;
  7809. begin
  7810. StartProgram(false);
  7811. Add('var s: ShortString');
  7812. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7813. ConvertProgram;
  7814. end;
  7815. procedure TTestModule.TestBaseType_RawByteStringFail;
  7816. begin
  7817. StartProgram(false);
  7818. Add('var s: RawByteString');
  7819. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7820. ConvertProgram;
  7821. end;
  7822. procedure TTestModule.TestTypeShortstring_Fail;
  7823. begin
  7824. StartProgram(false);
  7825. Add('type t = string[12];');
  7826. Add('var s: t;');
  7827. Add('begin');
  7828. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7829. ConvertProgram;
  7830. end;
  7831. procedure TTestModule.TestCharSet_Custom;
  7832. begin
  7833. StartProgram(false);
  7834. Add([
  7835. 'type',
  7836. ' TCharRg = ''a''..''z'';',
  7837. ' TSetOfCharRg = set of TCharRg;',
  7838. ' TCharRg2 = ''m''..''p'';',
  7839. 'const',
  7840. ' crg: TCharRg = ''b'';',
  7841. 'var',
  7842. ' c: char;',
  7843. ' crg2: TCharRg2;',
  7844. ' s: TSetOfCharRg;',
  7845. 'begin',
  7846. ' c:=crg;',
  7847. ' crg:=c;',
  7848. ' crg2:=crg;',
  7849. ' if c=crg then ;',
  7850. ' if crg=c then ;',
  7851. ' if crg=crg2 then ;',
  7852. ' if c in s then ;',
  7853. ' if crg2 in s then ;',
  7854. ' c:=default(TCharRg);',
  7855. '']);
  7856. ConvertProgram;
  7857. CheckSource('TestCharSet_Custom',
  7858. LinesToStr([ // statements
  7859. 'this.crg = "b";',
  7860. 'this.c = "";',
  7861. 'this.crg2 = "m";',
  7862. 'this.s = {};',
  7863. '']),
  7864. LinesToStr([ // this.$main
  7865. '$mod.c = $mod.crg;',
  7866. '$mod.crg = $mod.c;',
  7867. '$mod.crg2 = $mod.crg;',
  7868. 'if ($mod.c === $mod.crg) ;',
  7869. 'if ($mod.crg === $mod.c) ;',
  7870. 'if ($mod.crg === $mod.crg2) ;',
  7871. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7872. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7873. '$mod.c = "a";',
  7874. '']));
  7875. end;
  7876. procedure TTestModule.TestWideChar;
  7877. begin
  7878. StartProgram(false);
  7879. Add([
  7880. 'procedure Fly(var c: char);',
  7881. 'begin',
  7882. 'end;',
  7883. 'procedure Run(var c: widechar);',
  7884. 'begin',
  7885. 'end;',
  7886. 'var',
  7887. ' c: char;',
  7888. ' wc: widechar;',
  7889. ' w: word;',
  7890. 'begin',
  7891. ' Fly(wc);',
  7892. ' Run(c);',
  7893. ' wc:=WideChar(w);',
  7894. ' w:=ord(wc);',
  7895. '']);
  7896. ConvertProgram;
  7897. CheckSource('TestWideChar_VarArg',
  7898. LinesToStr([ // statements
  7899. 'this.Fly = function (c) {',
  7900. '};',
  7901. 'this.Run = function (c) {',
  7902. '};',
  7903. 'this.c = "";',
  7904. 'this.wc = "";',
  7905. 'this.w = 0;',
  7906. '']),
  7907. LinesToStr([ // this.$main
  7908. '$mod.Fly({',
  7909. ' p: $mod,',
  7910. ' get: function () {',
  7911. ' return this.p.wc;',
  7912. ' },',
  7913. ' set: function (v) {',
  7914. ' this.p.wc = v;',
  7915. ' }',
  7916. '});',
  7917. '$mod.Run({',
  7918. ' p: $mod,',
  7919. ' get: function () {',
  7920. ' return this.p.c;',
  7921. ' },',
  7922. ' set: function (v) {',
  7923. ' this.p.c = v;',
  7924. ' }',
  7925. '});',
  7926. '$mod.wc = String.fromCharCode($mod.w);',
  7927. '$mod.w = $mod.wc.charCodeAt();',
  7928. '',
  7929. '']));
  7930. end;
  7931. procedure TTestModule.TestForCharDo;
  7932. begin
  7933. StartProgram(false);
  7934. Add([
  7935. 'var c: char;',
  7936. 'begin',
  7937. ' for c:=''a'' to ''c'' do ;',
  7938. ' for c:=c downto ''a'' do ;',
  7939. ' for c:=''Б'' to ''Я'' do ;',
  7940. '']);
  7941. ConvertProgram;
  7942. CheckSource('TestForCharDo',
  7943. LinesToStr([ // statements
  7944. 'this.c = "";']),
  7945. LinesToStr([ // this.$main
  7946. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7947. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7948. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7949. '']));
  7950. end;
  7951. procedure TTestModule.TestForCharInDo;
  7952. begin
  7953. StartProgram(false);
  7954. Add([
  7955. 'type',
  7956. ' TSetOfChar = set of char;',
  7957. ' TCharRg = ''a''..''z'';',
  7958. ' TSetOfCharRg = set of TCharRg;',
  7959. 'const Foo = ''foo'';',
  7960. 'var',
  7961. ' c,c2: char;',
  7962. ' s: string;',
  7963. ' a1: array of char;',
  7964. ' a2: array[1..3] of char;',
  7965. ' soc: TSetOfChar;',
  7966. ' socr: TSetOfCharRg;',
  7967. ' cr: TCharRg;',
  7968. 'begin',
  7969. ' for c in foo do ;',
  7970. ' for c in s do ;',
  7971. ' for c in char do ;',
  7972. ' for c in a1 do ;',
  7973. ' for c in a2 do ;',
  7974. ' for c in [''1''..''3''] do ;',
  7975. ' for c in TSetOfChar do ;',
  7976. ' for c in TCharRg do ;',
  7977. ' for c in soc do c2:=c;',
  7978. ' for c in TSetOfCharRg do ;',
  7979. ' for c in socr do ;',
  7980. ' for cr in TCharRg do ;',
  7981. ' for cr in TSetOfCharRg do ;',
  7982. ' for cr in socr do ;',
  7983. '']);
  7984. ConvertProgram;
  7985. CheckSource('TestForCharInDo',
  7986. LinesToStr([ // statements
  7987. 'this.Foo = "foo";',
  7988. 'this.c = "";',
  7989. 'this.c2 = "";',
  7990. 'this.s = "";',
  7991. 'this.a1 = [];',
  7992. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7993. 'this.soc = {};',
  7994. 'this.socr = {};',
  7995. 'this.cr = "a";',
  7996. '']),
  7997. LinesToStr([ // this.$main
  7998. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7999. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8000. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8001. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8002. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8003. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8004. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8005. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8006. 'for (var $l8 in $mod.soc) {',
  8007. ' $mod.c = String.fromCharCode($l8);',
  8008. ' $mod.c2 = $mod.c;',
  8009. '};',
  8010. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8011. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8012. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8013. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8014. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8015. '']));
  8016. end;
  8017. procedure TTestModule.TestProcTwoArgs;
  8018. begin
  8019. StartProgram(false);
  8020. Add('procedure Test(a,b: longint);');
  8021. Add('begin');
  8022. Add('end;');
  8023. Add('begin');
  8024. ConvertProgram;
  8025. CheckSource('TestProcTwoArgs',
  8026. LinesToStr([ // statements
  8027. 'this.Test = function (a,b) {',
  8028. '};'
  8029. ]),
  8030. LinesToStr([ // this.$main
  8031. ''
  8032. ]));
  8033. end;
  8034. procedure TTestModule.TestProc_DefaultValue;
  8035. begin
  8036. StartProgram(false);
  8037. Add('procedure p1(i: longint = 1);');
  8038. Add('begin');
  8039. Add('end;');
  8040. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8041. Add('begin');
  8042. Add('end;');
  8043. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8044. Add('begin');
  8045. Add('end;');
  8046. Add('begin');
  8047. Add(' p1;');
  8048. Add(' p1();');
  8049. Add(' p1(11);');
  8050. Add(' p2;');
  8051. Add(' p2();');
  8052. Add(' p2(12);');
  8053. Add(' p2(13,''b'');');
  8054. Add(' p3();');
  8055. ConvertProgram;
  8056. CheckSource('TestProc_DefaultValue',
  8057. LinesToStr([ // statements
  8058. 'this.p1 = function (i) {',
  8059. '};',
  8060. 'this.p2 = function (i,c) {',
  8061. '};',
  8062. 'this.p3 = function (d,b,s) {',
  8063. '};'
  8064. ]),
  8065. LinesToStr([ // this.$main
  8066. ' $mod.p1(1);',
  8067. ' $mod.p1(1);',
  8068. ' $mod.p1(11);',
  8069. ' $mod.p2(1,"a");',
  8070. ' $mod.p2(1,"a");',
  8071. ' $mod.p2(12,"a");',
  8072. ' $mod.p2(13,"b");',
  8073. ' $mod.p3(1.0,false,"abc");'
  8074. ]));
  8075. end;
  8076. procedure TTestModule.TestFunctionInt;
  8077. begin
  8078. StartProgram(false);
  8079. Add('function MyTest(Bar: longint): longint;');
  8080. Add('begin');
  8081. Add(' Result:=2*bar');
  8082. Add('end;');
  8083. Add('begin');
  8084. ConvertProgram;
  8085. CheckSource('TestFunctionInt',
  8086. LinesToStr([ // statements
  8087. 'this.MyTest = function (Bar) {',
  8088. ' var Result = 0;',
  8089. ' Result = 2*Bar;',
  8090. ' return Result;',
  8091. '};'
  8092. ]),
  8093. LinesToStr([ // this.$main
  8094. ''
  8095. ]));
  8096. end;
  8097. procedure TTestModule.TestFunctionString;
  8098. begin
  8099. StartProgram(false);
  8100. Add('function Test(Bar: string): string;');
  8101. Add('begin');
  8102. Add(' Result:=bar+BAR');
  8103. Add('end;');
  8104. Add('begin');
  8105. ConvertProgram;
  8106. CheckSource('TestFunctionString',
  8107. LinesToStr([ // statements
  8108. 'this.Test = function (Bar) {',
  8109. ' var Result = "";',
  8110. ' Result = Bar+Bar;',
  8111. ' return Result;',
  8112. '};'
  8113. ]),
  8114. LinesToStr([ // this.$main
  8115. ''
  8116. ]));
  8117. end;
  8118. procedure TTestModule.TestIfThen;
  8119. begin
  8120. StartProgram(false);
  8121. Add([
  8122. 'var b: boolean;',
  8123. 'begin',
  8124. ' if b then ;',
  8125. ' if b then else ;']);
  8126. ConvertProgram;
  8127. CheckSource('TestIfThen',
  8128. LinesToStr([ // statements
  8129. 'this.b = false;',
  8130. '']),
  8131. LinesToStr([ // this.$main
  8132. 'if ($mod.b) ;',
  8133. 'if ($mod.b) ;',
  8134. '']));
  8135. end;
  8136. procedure TTestModule.TestForLoop;
  8137. begin
  8138. StartProgram(false);
  8139. Add('var');
  8140. Add(' vI, vJ, vN: longint;');
  8141. Add('begin');
  8142. Add(' VJ:=0;');
  8143. Add(' VN:=3;');
  8144. Add(' for VI:=1 to VN do');
  8145. Add(' begin');
  8146. Add(' VJ:=VJ+VI;');
  8147. Add(' end;');
  8148. ConvertProgram;
  8149. CheckSource('TestForLoop',
  8150. LinesToStr([ // statements
  8151. 'this.vI = 0;',
  8152. 'this.vJ = 0;',
  8153. 'this.vN = 0;'
  8154. ]),
  8155. LinesToStr([ // this.$main
  8156. ' $mod.vJ = 0;',
  8157. ' $mod.vN = 3;',
  8158. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8159. ' $mod.vI = $l;',
  8160. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8161. ' };',
  8162. '']));
  8163. end;
  8164. procedure TTestModule.TestForLoopInsideFunction;
  8165. begin
  8166. StartProgram(false);
  8167. Add('function SumNumbers(Count: longint): longint;');
  8168. Add('var');
  8169. Add(' vI, vJ: longint;');
  8170. Add('begin');
  8171. Add(' vj:=0;');
  8172. Add(' for vi:=1 to count do');
  8173. Add(' begin');
  8174. Add(' vj:=vj+vi;');
  8175. Add(' end;');
  8176. Add('end;');
  8177. Add('begin');
  8178. Add(' sumnumbers(3);');
  8179. ConvertProgram;
  8180. CheckSource('TestForLoopInsideFunction',
  8181. LinesToStr([ // statements
  8182. 'this.SumNumbers = function (Count) {',
  8183. ' var Result = 0;',
  8184. ' var vI = 0;',
  8185. ' var vJ = 0;',
  8186. ' vJ = 0;',
  8187. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8188. ' vI = $l;',
  8189. ' vJ = vJ + vI;',
  8190. ' };',
  8191. ' return Result;',
  8192. '};'
  8193. ]),
  8194. LinesToStr([ // $mod.$main
  8195. ' $mod.SumNumbers(3);'
  8196. ]));
  8197. end;
  8198. procedure TTestModule.TestForLoop_ReadVarAfter;
  8199. begin
  8200. StartProgram(false);
  8201. Add('var');
  8202. Add(' vI: longint;');
  8203. Add('begin');
  8204. Add(' for vi:=1 to 2 do ;');
  8205. Add(' if vi=3 then ;');
  8206. ConvertProgram;
  8207. CheckSource('TestForLoop',
  8208. LinesToStr([ // statements
  8209. 'this.vI = 0;'
  8210. ]),
  8211. LinesToStr([ // this.$main
  8212. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8213. ' if ($mod.vI===3) ;'
  8214. ]));
  8215. end;
  8216. procedure TTestModule.TestForLoop_Nested;
  8217. begin
  8218. StartProgram(false);
  8219. Add('function SumNumbers(Count: longint): longint;');
  8220. Add('var');
  8221. Add(' vI, vJ, vK: longint;');
  8222. Add('begin');
  8223. Add(' VK:=0;');
  8224. Add(' for VI:=1 to count do');
  8225. Add(' begin');
  8226. Add(' for vj:=1 to vi do');
  8227. Add(' begin');
  8228. Add(' vk:=VK+VI;');
  8229. Add(' end;');
  8230. Add(' end;');
  8231. Add('end;');
  8232. Add('begin');
  8233. Add(' sumnumbers(3);');
  8234. ConvertProgram;
  8235. CheckSource('TestForLoopInFunction',
  8236. LinesToStr([ // statements
  8237. 'this.SumNumbers = function (Count) {',
  8238. ' var Result = 0;',
  8239. ' var vI = 0;',
  8240. ' var vJ = 0;',
  8241. ' var vK = 0;',
  8242. ' vK = 0;',
  8243. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8244. ' vI = $l;',
  8245. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8246. ' vJ = $l1;',
  8247. ' vK = vK + vI;',
  8248. ' };',
  8249. ' };',
  8250. ' return Result;',
  8251. '};'
  8252. ]),
  8253. LinesToStr([ // $mod.$main
  8254. ' $mod.SumNumbers(3);'
  8255. ]));
  8256. end;
  8257. procedure TTestModule.TestRepeatUntil;
  8258. begin
  8259. StartProgram(false);
  8260. Add('var');
  8261. Add(' vI, vJ, vN: longint;');
  8262. Add('begin');
  8263. Add(' vn:=3;');
  8264. Add(' vj:=0;');
  8265. Add(' VI:=0;');
  8266. Add(' repeat');
  8267. Add(' VI:=vi+1;');
  8268. Add(' vj:=VJ+vI;');
  8269. Add(' until vi>=vn');
  8270. ConvertProgram;
  8271. CheckSource('TestRepeatUntil',
  8272. LinesToStr([ // statements
  8273. 'this.vI = 0;',
  8274. 'this.vJ = 0;',
  8275. 'this.vN = 0;'
  8276. ]),
  8277. LinesToStr([ // $mod.$main
  8278. ' $mod.vN = 3;',
  8279. ' $mod.vJ = 0;',
  8280. ' $mod.vI = 0;',
  8281. ' do{',
  8282. ' $mod.vI = $mod.vI + 1;',
  8283. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8284. ' }while(!($mod.vI>=$mod.vN));'
  8285. ]));
  8286. end;
  8287. procedure TTestModule.TestAsmBlock;
  8288. begin
  8289. StartProgram(false);
  8290. Add([
  8291. 'var',
  8292. ' vI: longint;',
  8293. 'begin',
  8294. ' vi:=1;',
  8295. ' asm',
  8296. ' if (vI===1) {',
  8297. ' vI=2;',
  8298. //' console.log(''end;'');', ToDo
  8299. ' }',
  8300. ' if (vI===2){ vI=3; }',
  8301. ' end;',
  8302. ' VI:=4;']);
  8303. ConvertProgram;
  8304. CheckSource('TestAsmBlock',
  8305. LinesToStr([ // statements
  8306. 'this.vI = 0;'
  8307. ]),
  8308. LinesToStr([ // $mod.$main
  8309. '$mod.vI = 1;',
  8310. 'if (vI===1) {',
  8311. ' vI=2;',
  8312. '}',
  8313. 'if (vI===2){ vI=3; }',
  8314. ';',
  8315. '$mod.vI = 4;'
  8316. ]));
  8317. end;
  8318. procedure TTestModule.TestAsmPas_Impl;
  8319. begin
  8320. StartUnit(false);
  8321. Add('interface');
  8322. Add('const cIntf: longint = 1;');
  8323. Add('var vIntf: longint;');
  8324. Add('implementation');
  8325. Add('const cImpl: longint = 2;');
  8326. Add('var vImpl: longint;');
  8327. Add('procedure DoIt;');
  8328. Add('const cLoc: longint = 3;');
  8329. Add('var vLoc: longint;');
  8330. Add('begin;');
  8331. Add(' asm');
  8332. //Add(' pas(vIntf)=pas(cIntf);');
  8333. //Add(' pas(vImpl)=pas(cImpl);');
  8334. //Add(' pas(vLoc)=pas(cLoc);');
  8335. Add(' end;');
  8336. Add('end;');
  8337. ConvertUnit;
  8338. CheckSource('TestAsmPas_Impl',
  8339. LinesToStr([
  8340. 'var $impl = $mod.$impl;',
  8341. 'this.cIntf = 1;',
  8342. 'this.vIntf = 0;',
  8343. '']),
  8344. '', // this.$init
  8345. LinesToStr([ // implementation
  8346. '$impl.cImpl = 2;',
  8347. '$impl.vImpl = 0;',
  8348. 'var cLoc = 3;',
  8349. '$impl.DoIt = function () {',
  8350. ' var vLoc = 0;',
  8351. '};',
  8352. '']) );
  8353. end;
  8354. procedure TTestModule.TestTryFinally;
  8355. begin
  8356. StartProgram(false);
  8357. Add('var i: longint;');
  8358. Add('begin');
  8359. Add(' try');
  8360. Add(' i:=0; i:=2 div i;');
  8361. Add(' finally');
  8362. Add(' i:=3');
  8363. Add(' end;');
  8364. ConvertProgram;
  8365. CheckSource('TestTryFinally',
  8366. LinesToStr([ // statements
  8367. 'this.i = 0;'
  8368. ]),
  8369. LinesToStr([ // $mod.$main
  8370. 'try {',
  8371. ' $mod.i = 0;',
  8372. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8373. '} finally {',
  8374. ' $mod.i = 3;',
  8375. '};'
  8376. ]));
  8377. end;
  8378. procedure TTestModule.TestTryExcept;
  8379. begin
  8380. StartProgram(false);
  8381. Add([
  8382. 'type',
  8383. ' TObject = class end;',
  8384. ' Exception = class Msg: string; end;',
  8385. ' EInvalidCast = class(Exception) end;',
  8386. 'var vI: longint;',
  8387. 'begin',
  8388. ' try',
  8389. ' vi:=1;',
  8390. ' except',
  8391. ' vi:=2',
  8392. ' end;',
  8393. ' try',
  8394. ' vi:=3;',
  8395. ' except',
  8396. ' raise;',
  8397. ' end;',
  8398. ' try',
  8399. ' VI:=4;',
  8400. ' except',
  8401. ' on einvalidcast do',
  8402. ' raise;',
  8403. ' on E: exception do',
  8404. ' if e.msg='''' then',
  8405. ' raise e;',
  8406. ' else',
  8407. ' vi:=5',
  8408. ' end;',
  8409. ' try',
  8410. ' VI:=6;',
  8411. ' except',
  8412. ' on einvalidcast do ;',
  8413. ' end;',
  8414. '']);
  8415. ConvertProgram;
  8416. CheckSource('TestTryExcept',
  8417. LinesToStr([ // statements
  8418. 'rtl.createClass(this, "TObject", null, function () {',
  8419. ' this.$init = function () {',
  8420. ' };',
  8421. ' this.$final = function () {',
  8422. ' };',
  8423. '});',
  8424. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8425. ' this.$init = function () {',
  8426. ' $mod.TObject.$init.call(this);',
  8427. ' this.Msg = "";',
  8428. ' };',
  8429. '});',
  8430. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8431. '});',
  8432. 'this.vI = 0;'
  8433. ]),
  8434. LinesToStr([ // $mod.$main
  8435. 'try {',
  8436. ' $mod.vI = 1;',
  8437. '} catch ($e) {',
  8438. ' $mod.vI = 2;',
  8439. '};',
  8440. 'try {',
  8441. ' $mod.vI = 3;',
  8442. '} catch ($e) {',
  8443. ' throw $e;',
  8444. '};',
  8445. 'try {',
  8446. ' $mod.vI = 4;',
  8447. '} catch ($e) {',
  8448. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8449. ' throw $e',
  8450. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8451. ' var E = $e;',
  8452. ' if (E.Msg === "") throw E;',
  8453. ' } else {',
  8454. ' $mod.vI = 5;',
  8455. ' }',
  8456. '};',
  8457. 'try {',
  8458. ' $mod.vI = 6;',
  8459. '} catch ($e) {',
  8460. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8461. ' } else throw $e',
  8462. '};',
  8463. '']));
  8464. end;
  8465. procedure TTestModule.TestTryExcept_ReservedWords;
  8466. begin
  8467. StartProgram(false);
  8468. Add([
  8469. 'type',
  8470. ' TObject = class end;',
  8471. ' Exception = class',
  8472. ' Symbol: string;',
  8473. ' end;',
  8474. 'var &try: longint;',
  8475. 'begin',
  8476. ' try',
  8477. ' &try:=4;',
  8478. ' except',
  8479. ' on Error: exception do',
  8480. ' if errOR.symBol='''' then',
  8481. ' raise ERRor;',
  8482. ' end;',
  8483. '']);
  8484. ConvertProgram;
  8485. CheckSource('TestTryExcept_ReservedWords',
  8486. LinesToStr([ // statements
  8487. 'rtl.createClass(this, "TObject", null, function () {',
  8488. ' this.$init = function () {',
  8489. ' };',
  8490. ' this.$final = function () {',
  8491. ' };',
  8492. '});',
  8493. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8494. ' this.$init = function () {',
  8495. ' $mod.TObject.$init.call(this);',
  8496. ' this.Symbol = "";',
  8497. ' };',
  8498. '});',
  8499. 'this.Try = 0;',
  8500. '']),
  8501. LinesToStr([ // $mod.$main
  8502. 'try {',
  8503. ' $mod.Try = 4;',
  8504. '} catch ($e) {',
  8505. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8506. ' var error = $e;',
  8507. ' if (error.Symbol === "") throw error;',
  8508. ' } else throw $e',
  8509. '};',
  8510. '']));
  8511. end;
  8512. procedure TTestModule.TestIfThenRaiseElse;
  8513. begin
  8514. StartProgram(false);
  8515. Add([
  8516. 'type',
  8517. ' TObject = class',
  8518. ' constructor Create;',
  8519. ' end;',
  8520. 'constructor TObject.Create;',
  8521. 'begin',
  8522. 'end;',
  8523. 'var b: boolean;',
  8524. 'begin',
  8525. ' if b then',
  8526. ' raise TObject.Create',
  8527. ' else',
  8528. ' b:=false;',
  8529. '']);
  8530. ConvertProgram;
  8531. CheckSource('TestIfThenRaiseElse',
  8532. LinesToStr([ // statements
  8533. 'rtl.createClass(this, "TObject", null, function () {',
  8534. ' this.$init = function () {',
  8535. ' };',
  8536. ' this.$final = function () {',
  8537. ' };',
  8538. ' this.Create = function () {',
  8539. ' return this;',
  8540. ' };',
  8541. '});',
  8542. 'this.b = false;',
  8543. '']),
  8544. LinesToStr([ // $mod.$main
  8545. 'if ($mod.b) {',
  8546. ' throw $mod.TObject.$create("Create")}',
  8547. ' else $mod.b = false;',
  8548. '']));
  8549. end;
  8550. procedure TTestModule.TestCaseOf;
  8551. begin
  8552. StartProgram(false);
  8553. Add([
  8554. 'const e: longint; external name ''$e'';',
  8555. 'var vI: longint;',
  8556. 'begin',
  8557. ' case vi of',
  8558. ' 1: ;',
  8559. ' 2: vi:=3;',
  8560. ' e: ;',
  8561. ' else',
  8562. ' VI:=4',
  8563. ' end;']);
  8564. ConvertProgram;
  8565. CheckSource('TestCaseOf',
  8566. LinesToStr([ // statements
  8567. 'this.vI = 0;'
  8568. ]),
  8569. LinesToStr([ // $mod.$main
  8570. 'var $tmp = $mod.vI;',
  8571. 'if ($tmp === 1) {}',
  8572. 'else if ($tmp === 2) {',
  8573. ' $mod.vI = 3}',
  8574. ' else if ($tmp === $e) {}',
  8575. 'else {',
  8576. ' $mod.vI = 4;',
  8577. '};'
  8578. ]));
  8579. end;
  8580. procedure TTestModule.TestCaseOf_UseSwitch;
  8581. begin
  8582. StartProgram(false);
  8583. Converter.UseSwitchStatement:=true;
  8584. Add('var Vi: longint;');
  8585. Add('begin');
  8586. Add(' case vi of');
  8587. Add(' 1: ;');
  8588. Add(' 2: VI:=3;');
  8589. Add(' else');
  8590. Add(' vi:=4');
  8591. Add(' end;');
  8592. ConvertProgram;
  8593. CheckSource('TestCaseOf_UseSwitch',
  8594. LinesToStr([ // statements
  8595. 'this.Vi = 0;'
  8596. ]),
  8597. LinesToStr([ // $mod.$main
  8598. 'switch ($mod.Vi) {',
  8599. 'case 1:',
  8600. ' break;',
  8601. 'case 2:',
  8602. ' $mod.Vi = 3;',
  8603. ' break;',
  8604. 'default:',
  8605. ' $mod.Vi = 4;',
  8606. '};'
  8607. ]));
  8608. end;
  8609. procedure TTestModule.TestCaseOfNoElse;
  8610. begin
  8611. StartProgram(false);
  8612. Add('var Vi: longint;');
  8613. Add('begin');
  8614. Add(' case vi of');
  8615. Add(' 1: begin vi:=2; VI:=3; end;');
  8616. Add(' end;');
  8617. ConvertProgram;
  8618. CheckSource('TestCaseOfNoElse',
  8619. LinesToStr([ // statements
  8620. 'this.Vi = 0;'
  8621. ]),
  8622. LinesToStr([ // $mod.$main
  8623. 'var $tmp = $mod.Vi;',
  8624. 'if ($tmp === 1) {',
  8625. ' $mod.Vi = 2;',
  8626. ' $mod.Vi = 3;',
  8627. '};'
  8628. ]));
  8629. end;
  8630. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8631. begin
  8632. StartProgram(false);
  8633. Converter.UseSwitchStatement:=true;
  8634. Add('var vI: longint;');
  8635. Add('begin');
  8636. Add(' case vi of');
  8637. Add(' 1: begin VI:=2; vi:=3; end;');
  8638. Add(' end;');
  8639. ConvertProgram;
  8640. CheckSource('TestCaseOfNoElse_UseSwitch',
  8641. LinesToStr([ // statements
  8642. 'this.vI = 0;'
  8643. ]),
  8644. LinesToStr([ // $mod.$main
  8645. 'switch ($mod.vI) {',
  8646. 'case 1:',
  8647. ' $mod.vI = 2;',
  8648. ' $mod.vI = 3;',
  8649. ' break;',
  8650. '};'
  8651. ]));
  8652. end;
  8653. procedure TTestModule.TestCaseOfRange;
  8654. begin
  8655. StartProgram(false);
  8656. Add('var vI: longint;');
  8657. Add('begin');
  8658. Add(' case vi of');
  8659. Add(' 1..3: vi:=14;');
  8660. Add(' 4,5: vi:=16;');
  8661. Add(' 6..7,9..10: ;');
  8662. Add(' else ;');
  8663. Add(' end;');
  8664. ConvertProgram;
  8665. CheckSource('TestCaseOfRange',
  8666. LinesToStr([ // statements
  8667. 'this.vI = 0;'
  8668. ]),
  8669. LinesToStr([ // $mod.$main
  8670. 'var $tmp = $mod.vI;',
  8671. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8672. ' $mod.vI = 14',
  8673. '} else if (($tmp === 4) || ($tmp === 5)){',
  8674. ' $mod.vI = 16',
  8675. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8676. ]));
  8677. end;
  8678. procedure TTestModule.TestCaseOfString;
  8679. begin
  8680. StartProgram(false);
  8681. Add([
  8682. 'var s,h: string;',
  8683. 'begin',
  8684. ' case s of',
  8685. ' ''foo'': s:=h;',
  8686. ' ''a''..''z'': h:=s;',
  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 === "foo") {',
  8700. ' $mod.s = $mod.h}',
  8701. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8702. ' $mod.h = $mod.s}',
  8703. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8704. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8705. '']));
  8706. end;
  8707. procedure TTestModule.TestCaseOfChar;
  8708. begin
  8709. StartProgram(false);
  8710. Add([
  8711. 'var s,h: char;',
  8712. 'begin',
  8713. ' case s of',
  8714. ' ''a''..''z'': h:=s;',
  8715. ' ''ä'': ;',
  8716. ' ''ў'', ''ё'': ;',
  8717. ' ''Б''..''Я'': ;',
  8718. ' end;',
  8719. '']);
  8720. ConvertProgram;
  8721. CheckSource('TestCaseOfString',
  8722. LinesToStr([ // statements
  8723. 'this.s = "";',
  8724. 'this.h = "";',
  8725. '']),
  8726. LinesToStr([ // $mod.$main
  8727. 'var $tmp = $mod.s;',
  8728. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8729. ' $mod.h = $mod.s}',
  8730. ' else if ($tmp === "ä") {}',
  8731. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8732. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8733. '']));
  8734. end;
  8735. procedure TTestModule.TestCaseOfExternalClassConst;
  8736. begin
  8737. StartProgram(false);
  8738. Add([
  8739. '{$modeswitch externalclass}',
  8740. 'type',
  8741. ' TBird = class external name ''Bird''',
  8742. ' const e: longint;',
  8743. ' end;',
  8744. 'var vI: longint;',
  8745. 'begin',
  8746. ' case vi of',
  8747. ' 1: vi:=3;',
  8748. ' TBird.e: ;',
  8749. ' end;']);
  8750. ConvertProgram;
  8751. CheckSource('TestCaseOfExternalClassConst',
  8752. LinesToStr([ // statements
  8753. 'this.vI = 0;'
  8754. ]),
  8755. LinesToStr([ // $mod.$main
  8756. 'var $tmp = $mod.vI;',
  8757. 'if ($tmp === 1) {',
  8758. ' $mod.vI = 3}',
  8759. ' else if ($tmp === Bird.e) ;'
  8760. ]));
  8761. end;
  8762. procedure TTestModule.TestDebugger;
  8763. begin
  8764. StartProgram(false);
  8765. Add([
  8766. 'procedure DoIt;',
  8767. 'begin',
  8768. ' deBugger;',
  8769. ' DeBugger();',
  8770. 'end;',
  8771. 'begin',
  8772. ' Debugger;']);
  8773. ConvertProgram;
  8774. CheckSource('TestDebugger',
  8775. LinesToStr([ // statements
  8776. 'this.DoIt = function () {',
  8777. ' debugger;',
  8778. ' debugger;',
  8779. '};',
  8780. '']),
  8781. LinesToStr([ // $mod.$main
  8782. 'debugger;',
  8783. '']));
  8784. end;
  8785. procedure TTestModule.TestArray_Dynamic;
  8786. begin
  8787. StartProgram(false);
  8788. Add([
  8789. 'type',
  8790. ' TArrayInt = array of longint;',
  8791. 'var',
  8792. ' Arr: TArrayInt;',
  8793. ' i: longint;',
  8794. ' b: boolean;',
  8795. 'begin',
  8796. ' SetLength(arr,3);',
  8797. ' arr[0]:=4;',
  8798. ' arr[1]:=length(arr)+arr[0];',
  8799. ' arr[i]:=5;',
  8800. ' arr[arr[i]]:=arr[6];',
  8801. ' i:=low(arr);',
  8802. ' i:=high(arr);',
  8803. ' b:=Assigned(arr);',
  8804. ' Arr:=default(TArrayInt);']);
  8805. ConvertProgram;
  8806. CheckSource('TestArray_Dynamic',
  8807. LinesToStr([ // statements
  8808. 'this.Arr = [];',
  8809. 'this.i = 0;',
  8810. 'this.b = false;'
  8811. ]),
  8812. LinesToStr([ // $mod.$main
  8813. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8814. '$mod.Arr[0] = 4;',
  8815. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8816. '$mod.Arr[$mod.i] = 5;',
  8817. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8818. '$mod.i = 0;',
  8819. '$mod.i = rtl.length($mod.Arr) - 1;',
  8820. '$mod.b = rtl.length($mod.Arr) > 0;',
  8821. '$mod.Arr = [];',
  8822. '']));
  8823. end;
  8824. procedure TTestModule.TestArray_Dynamic_Nil;
  8825. begin
  8826. StartProgram(false);
  8827. Add('type');
  8828. Add(' TArrayInt = array of longint;');
  8829. Add('var');
  8830. Add(' Arr: TArrayInt;');
  8831. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8832. Add('begin');
  8833. Add(' arr:=nil;');
  8834. Add(' if arr=nil then;');
  8835. Add(' if nil=arr then;');
  8836. Add(' if arr<>nil then;');
  8837. Add(' if nil<>arr then;');
  8838. Add(' DoIt(nil,nil);');
  8839. ConvertProgram;
  8840. CheckSource('TestArray_Dynamic',
  8841. LinesToStr([ // statements
  8842. 'this.Arr = [];',
  8843. 'this.DoIt = function(i,j){',
  8844. '};'
  8845. ]),
  8846. LinesToStr([ // $mod.$main
  8847. '$mod.Arr = [];',
  8848. 'if (rtl.length($mod.Arr) === 0) ;',
  8849. 'if (rtl.length($mod.Arr) === 0) ;',
  8850. 'if (rtl.length($mod.Arr) > 0) ;',
  8851. 'if (rtl.length($mod.Arr) > 0) ;',
  8852. '$mod.DoIt([],[]);',
  8853. '']));
  8854. end;
  8855. procedure TTestModule.TestArray_DynMultiDimensional;
  8856. begin
  8857. StartProgram(false);
  8858. Add([
  8859. 'type',
  8860. ' TArrayInt = array of longint;',
  8861. ' TArrayArrayInt = array of TArrayInt;',
  8862. 'var',
  8863. ' Arr: TArrayInt;',
  8864. ' Arr2: TArrayArrayInt;',
  8865. ' i: longint;',
  8866. 'begin',
  8867. ' arr2:=nil;',
  8868. ' if arr2=nil then;',
  8869. ' if nil=arr2 then;',
  8870. ' i:=low(arr2);',
  8871. ' i:=low(arr2[1]);',
  8872. ' i:=high(arr2);',
  8873. ' i:=high(arr2[2]);',
  8874. ' arr2[3]:=arr;',
  8875. ' arr2[4][5]:=i;',
  8876. ' i:=arr2[6][7];',
  8877. ' arr2[8,9]:=i;',
  8878. ' i:=arr2[10,11];',
  8879. ' SetLength(arr2,14);',
  8880. ' SetLength(arr2[15],16);']);
  8881. ConvertProgram;
  8882. CheckSource('TestArray_Dynamic',
  8883. LinesToStr([ // statements
  8884. 'this.Arr = [];',
  8885. 'this.Arr2 = [];',
  8886. 'this.i = 0;'
  8887. ]),
  8888. LinesToStr([ // $mod.$main
  8889. '$mod.Arr2 = [];',
  8890. 'if (rtl.length($mod.Arr2) === 0) ;',
  8891. 'if (rtl.length($mod.Arr2) === 0) ;',
  8892. '$mod.i = 0;',
  8893. '$mod.i = 0;',
  8894. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8895. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8896. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8897. '$mod.Arr2[4][5] = $mod.i;',
  8898. '$mod.i = $mod.Arr2[6][7];',
  8899. '$mod.Arr2[8][9] = $mod.i;',
  8900. '$mod.i = $mod.Arr2[10][11];',
  8901. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8902. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8903. '']));
  8904. end;
  8905. procedure TTestModule.TestArray_DynamicAssign;
  8906. begin
  8907. StartProgram(false);
  8908. Add([
  8909. 'type',
  8910. ' TArrayInt = array of longint;',
  8911. ' TArrayArrayInt = array of TArrayInt;',
  8912. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8913. 'begin',
  8914. 'end;',
  8915. 'procedure Fly(var a: TArrayInt);',
  8916. 'begin',
  8917. 'end;',
  8918. 'var',
  8919. ' Arr: TArrayInt;',
  8920. ' Arr2: TArrayArrayInt;',
  8921. 'begin',
  8922. ' arr:=nil;',
  8923. ' arr2:=nil;',
  8924. ' arr2[1]:=nil;',
  8925. ' arr2[2]:=arr;',
  8926. ' Run(arr,arr,arr);',
  8927. ' Fly(arr);',
  8928. ' Run(arr2[4],arr2[5],arr2[6]);',
  8929. ' Fly(arr2[7]);',
  8930. '']);
  8931. ConvertProgram;
  8932. CheckSource('TestArray_DynamicAssign',
  8933. LinesToStr([ // statements
  8934. 'this.Run = function (a, b, c) {',
  8935. '};',
  8936. 'this.Fly = function (a) {',
  8937. '};',
  8938. 'this.Arr = [];',
  8939. 'this.Arr2 = [];',
  8940. '']),
  8941. LinesToStr([ // $mod.$main
  8942. '$mod.Arr = [];',
  8943. '$mod.Arr2 = [];',
  8944. '$mod.Arr2[1] = [];',
  8945. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8946. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8947. '$mod.Fly({',
  8948. ' p: $mod,',
  8949. ' get: function () {',
  8950. ' return this.p.Arr;',
  8951. ' },',
  8952. ' set: function (v) {',
  8953. ' this.p.Arr = v;',
  8954. ' }',
  8955. '});',
  8956. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8957. '$mod.Fly({',
  8958. ' a: 7,',
  8959. ' p: $mod.Arr2,',
  8960. ' get: function () {',
  8961. ' return this.p[this.a];',
  8962. ' },',
  8963. ' set: function (v) {',
  8964. ' this.p[this.a] = v;',
  8965. ' }',
  8966. '});',
  8967. '']));
  8968. end;
  8969. procedure TTestModule.TestArray_StaticInt;
  8970. begin
  8971. StartProgram(false);
  8972. Add('type');
  8973. Add(' TArrayInt = array[2..4] of longint;');
  8974. Add('var');
  8975. Add(' Arr: TArrayInt;');
  8976. Add(' Arr2: TArrayInt = (5,6,7);');
  8977. Add(' i: longint;');
  8978. Add(' b: boolean;');
  8979. Add('begin');
  8980. Add(' arr[2]:=4;');
  8981. Add(' arr[3]:=arr[2]+arr[3];');
  8982. Add(' arr[i]:=5;');
  8983. Add(' arr[arr[i]]:=arr[high(arr)];');
  8984. Add(' i:=low(arr);');
  8985. Add(' i:=high(arr);');
  8986. Add(' b:=arr[2]=arr[3];');
  8987. Add(' arr:=default(TArrayInt);');
  8988. ConvertProgram;
  8989. CheckSource('TestArray_StaticInt',
  8990. LinesToStr([ // statements
  8991. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8992. 'this.Arr2 = [5, 6, 7];',
  8993. 'this.i = 0;',
  8994. 'this.b = false;'
  8995. ]),
  8996. LinesToStr([ // $mod.$main
  8997. '$mod.Arr[0] = 4;',
  8998. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8999. '$mod.Arr[$mod.i-2] = 5;',
  9000. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9001. '$mod.i = 2;',
  9002. '$mod.i = 4;',
  9003. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9004. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9005. '']));
  9006. end;
  9007. procedure TTestModule.TestArray_StaticBool;
  9008. begin
  9009. StartProgram(false);
  9010. Add('type');
  9011. Add(' TBools = array[boolean] of boolean;');
  9012. Add(' TBool2 = array[true..true] of boolean;');
  9013. Add('var');
  9014. Add(' Arr: TBools;');
  9015. Add(' Arr2: TBool2;');
  9016. Add(' Arr3: TBools = (true,false);');
  9017. Add(' b: boolean;');
  9018. Add('begin');
  9019. Add(' b:=low(arr);');
  9020. Add(' b:=high(arr);');
  9021. Add(' arr[true]:=false;');
  9022. Add(' arr[false]:=arr[b] or arr[true];');
  9023. Add(' arr[b]:=true;');
  9024. Add(' arr[arr[b]]:=arr[high(arr)];');
  9025. Add(' b:=arr[false]=arr[true];');
  9026. Add(' b:=low(arr2);');
  9027. Add(' b:=high(arr2);');
  9028. Add(' arr2[true]:=true;');
  9029. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9030. Add(' arr2[b]:=false;');
  9031. ConvertProgram;
  9032. CheckSource('TestArray_StaticBool',
  9033. LinesToStr([ // statements
  9034. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9035. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9036. 'this.Arr3 = [true, false];',
  9037. 'this.b = false;'
  9038. ]),
  9039. LinesToStr([ // $mod.$main
  9040. '$mod.b = false;',
  9041. '$mod.b = true;',
  9042. '$mod.Arr[1] = false;',
  9043. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9044. '$mod.Arr[+$mod.b] = true;',
  9045. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9046. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9047. '$mod.b = true;',
  9048. '$mod.b = true;',
  9049. '$mod.Arr2[0] = true;',
  9050. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9051. '$mod.Arr2[1-$mod.b] = false;',
  9052. '']));
  9053. end;
  9054. procedure TTestModule.TestArray_StaticChar;
  9055. begin
  9056. StartProgram(false);
  9057. Add([
  9058. 'type',
  9059. ' TChars = array[char] of char;',
  9060. ' TChars2 = array[''a''..''z''] of char;',
  9061. 'var',
  9062. ' Arr: TChars;',
  9063. ' Arr2: TChars2;',
  9064. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9065. ' Arr4: array[11..13] of char = ''pas'';',
  9066. ' Arr5: array[21..22] of char = ''äö'';',
  9067. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9068. ' c: char;',
  9069. ' b: boolean;',
  9070. 'begin',
  9071. ' c:=low(arr);',
  9072. ' c:=high(arr);',
  9073. ' arr[''B'']:=''a'';',
  9074. ' arr[''D'']:=arr[c];',
  9075. ' arr[c]:=arr[''d''];',
  9076. ' arr[arr[c]]:=arr[high(arr)];',
  9077. ' b:=arr[low(arr)]=arr[''e''];',
  9078. ' c:=low(arr2);',
  9079. ' c:=high(arr2);',
  9080. ' arr2[''b'']:=''f'';',
  9081. ' arr2[''a'']:=arr2[c];',
  9082. ' arr2[c]:=arr2[''g''];']);
  9083. ConvertProgram;
  9084. CheckSource('TestArray_StaticChar',
  9085. LinesToStr([ // statements
  9086. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9087. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9088. 'this.Arr3 = ["p", "a", "s"];',
  9089. 'this.Arr4 = ["p", "a", "s"];',
  9090. 'this.Arr5 = ["ä", "ö"];',
  9091. 'this.Arr6 = ["ä", "ö"];',
  9092. 'this.c = "";',
  9093. 'this.b = false;',
  9094. '']),
  9095. LinesToStr([ // $mod.$main
  9096. '$mod.c = "\x00";',
  9097. '$mod.c = "\uFFFF";',
  9098. '$mod.Arr[66] = "a";',
  9099. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9100. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9101. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9102. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9103. '$mod.c = "a";',
  9104. '$mod.c = "z";',
  9105. '$mod.Arr2[1] = "f";',
  9106. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9107. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9108. '']));
  9109. end;
  9110. procedure TTestModule.TestArray_StaticMultiDim;
  9111. begin
  9112. StartProgram(false);
  9113. Add([
  9114. 'type',
  9115. ' TArrayInt = array[1..3] of longint;',
  9116. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9117. 'var',
  9118. ' Arr: TArrayInt;',
  9119. ' Arr2: TArrayArrayInt;',
  9120. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9121. ' i: longint;',
  9122. 'begin',
  9123. ' i:=low(arr);',
  9124. ' i:=low(arr2);',
  9125. ' i:=low(arr2[5]);',
  9126. ' i:=high(arr);',
  9127. ' i:=high(arr2);',
  9128. ' i:=high(arr2[6]);',
  9129. ' arr2[5]:=arr;',
  9130. ' arr2[6][2]:=i;',
  9131. ' i:=arr2[6][3];',
  9132. ' arr2[6,3]:=i;',
  9133. ' i:=arr2[5,2];',
  9134. ' arr2:=arr2;',// clone multi dim static array
  9135. ' arr3:=arr3;',// clone anonymous multi dim static array
  9136. '']);
  9137. ConvertProgram;
  9138. CheckSource('TestArray_StaticMultiDim',
  9139. LinesToStr([ // statements
  9140. 'this.TArrayArrayInt$clone = function (a) {',
  9141. ' var r = [];',
  9142. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9143. ' return r;',
  9144. '};',
  9145. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9146. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9147. 'this.Arr3$a$clone = function (a) {',
  9148. ' var r = [];',
  9149. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9150. ' return r;',
  9151. '};',
  9152. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9153. 'this.i = 0;'
  9154. ]),
  9155. LinesToStr([ // $mod.$main
  9156. '$mod.i = 1;',
  9157. '$mod.i = 5;',
  9158. '$mod.i = 1;',
  9159. '$mod.i = 3;',
  9160. '$mod.i = 6;',
  9161. '$mod.i = 3;',
  9162. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9163. '$mod.Arr2[1][1] = $mod.i;',
  9164. '$mod.i = $mod.Arr2[1][2];',
  9165. '$mod.Arr2[1][2] = $mod.i;',
  9166. '$mod.i = $mod.Arr2[0][1];',
  9167. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9168. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9169. '']));
  9170. end;
  9171. procedure TTestModule.TestArray_StaticInFunction;
  9172. begin
  9173. StartProgram(false);
  9174. Add([
  9175. 'const TArrayInt = 3;',
  9176. 'const TArrayArrayInt = 4;',
  9177. 'procedure DoIt;',
  9178. 'type',
  9179. ' TArrayInt = array[1..3] of longint;',
  9180. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9181. 'var',
  9182. ' Arr: TArrayInt;',
  9183. ' Arr2: TArrayArrayInt;',
  9184. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9185. ' i: longint;',
  9186. 'begin',
  9187. ' arr2[5]:=arr;',
  9188. ' arr2:=arr2;',// clone multi dim static array
  9189. ' arr3:=arr3;',// clone multi dim anonymous static array
  9190. 'end;',
  9191. 'begin',
  9192. '']);
  9193. ConvertProgram;
  9194. CheckSource('TestArray_StaticInFunction',
  9195. LinesToStr([ // statements
  9196. 'this.TArrayInt = 3;',
  9197. 'this.TArrayArrayInt = 4;',
  9198. 'var TArrayArrayInt$1$clone = function (a) {',
  9199. ' var r = [];',
  9200. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9201. ' return r;',
  9202. '};',
  9203. 'var Arr3$a$clone = function (a) {',
  9204. ' var r = [];',
  9205. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9206. ' return r;',
  9207. '};',
  9208. 'this.DoIt = function () {',
  9209. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9210. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9211. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9212. ' var i = 0;',
  9213. ' Arr2[0] = Arr.slice(0);',
  9214. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9215. ' Arr3 = Arr3$a$clone(Arr3);',
  9216. '};',
  9217. '']),
  9218. LinesToStr([ // $mod.$main
  9219. '']));
  9220. end;
  9221. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9222. begin
  9223. StartProgram(false);
  9224. Add([
  9225. 'type',
  9226. ' TArrayInt = array[1..3,1..2] of longint;',
  9227. 'var',
  9228. ' a,b: TArrayInt;',
  9229. 'begin',
  9230. ' if a=b then ;',
  9231. '']);
  9232. SetExpectedPasResolverError('compare static array is not supported',
  9233. nXIsNotSupported);
  9234. ConvertProgram;
  9235. end;
  9236. procedure TTestModule.TestArrayOfRecord;
  9237. begin
  9238. StartProgram(false);
  9239. Add([
  9240. 'type',
  9241. ' TRec = record',
  9242. ' Int: longint;',
  9243. ' end;',
  9244. ' TArrayRec = array of TRec;',
  9245. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9246. 'begin',
  9247. 'end;',
  9248. 'var',
  9249. ' Arr: TArrayRec;',
  9250. ' r: TRec;',
  9251. ' i: longint;',
  9252. 'begin',
  9253. ' SetLength(arr,3);',
  9254. ' arr[0].int:=4;',
  9255. ' arr[1].int:=length(arr)+arr[2].int;',
  9256. ' arr[arr[i].int].int:=arr[5].int;',
  9257. ' arr[7]:=r;',
  9258. ' r:=arr[8];',
  9259. ' i:=low(arr);',
  9260. ' i:=high(arr);',
  9261. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9262. ConvertProgram;
  9263. CheckSource('TestArrayOfRecord',
  9264. LinesToStr([ // statements
  9265. 'rtl.recNewT(this, "TRec", function () {',
  9266. ' this.Int = 0;',
  9267. ' this.$eq = function (b) {',
  9268. ' return this.Int === b.Int;',
  9269. ' };',
  9270. ' this.$assign = function (s) {',
  9271. ' this.Int = s.Int;',
  9272. ' return this;',
  9273. ' };',
  9274. '});',
  9275. 'this.DoIt = function (vd, vc, vv) {',
  9276. '};',
  9277. 'this.Arr = [];',
  9278. 'this.r = this.TRec.$new();',
  9279. 'this.i = 0;'
  9280. ]),
  9281. LinesToStr([ // $mod.$main
  9282. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9283. '$mod.Arr[0].Int = 4;',
  9284. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9285. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9286. '$mod.Arr[7].$assign($mod.r);',
  9287. '$mod.r.$assign($mod.Arr[8]);',
  9288. '$mod.i = 0;',
  9289. '$mod.i = rtl.length($mod.Arr)-1;',
  9290. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9291. '']));
  9292. end;
  9293. procedure TTestModule.TestArray_StaticRecord;
  9294. begin
  9295. StartProgram(false);
  9296. Add([
  9297. 'type',
  9298. ' TRec = record',
  9299. ' Int: longint;',
  9300. ' end;',
  9301. ' TArrayRec = array[1..2] of TRec;',
  9302. 'var',
  9303. ' Arr: TArrayRec;',
  9304. 'begin',
  9305. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9306. '']);
  9307. ConvertProgram;
  9308. CheckSource('TestArray_StaticRecord',
  9309. LinesToStr([ // statements
  9310. 'rtl.recNewT(this, "TRec", function () {',
  9311. ' this.Int = 0;',
  9312. ' this.$eq = function (b) {',
  9313. ' return this.Int === b.Int;',
  9314. ' };',
  9315. ' this.$assign = function (s) {',
  9316. ' this.Int = s.Int;',
  9317. ' return this;',
  9318. ' };',
  9319. '});',
  9320. 'this.TArrayRec$clone = function (a) {',
  9321. ' var r = [];',
  9322. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9323. ' return r;',
  9324. '};',
  9325. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9326. '']),
  9327. LinesToStr([ // $mod.$main
  9328. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9329. end;
  9330. procedure TTestModule.TestArrayOfSet;
  9331. begin
  9332. StartProgram(false);
  9333. Add([
  9334. 'type',
  9335. ' TFlag = (big,small);',
  9336. ' TSetOfFlag = set of tflag;',
  9337. ' TArrayFlag = array of TSetOfFlag;',
  9338. 'procedure DoIt(const a: Tarrayflag);',
  9339. 'begin',
  9340. 'end;',
  9341. 'var',
  9342. ' f: TFlag;',
  9343. ' s: TSetOfFlag;',
  9344. ' Arr: TArrayFlag;',
  9345. ' i: longint;',
  9346. 'begin',
  9347. ' SetLength(arr,3);',
  9348. ' arr[0]:=s;',
  9349. ' arr[1]:=[big];',
  9350. ' arr[2]:=[big]+s;',
  9351. ' arr[3]:=s+[big];',
  9352. ' arr[4]:=arr[5];',
  9353. ' s:=arr[6];',
  9354. ' i:=low(arr);',
  9355. ' i:=high(arr);',
  9356. ' DoIt(arr);',
  9357. ' DoIt([s]);',
  9358. ' DoIt([[],s]);',
  9359. ' DoIt([s,[]]);',
  9360. '']);
  9361. ConvertProgram;
  9362. CheckSource('TestArrayOfSet',
  9363. LinesToStr([ // statements
  9364. 'this.TFlag = {',
  9365. ' "0": "big",',
  9366. ' big: 0,',
  9367. ' "1": "small",',
  9368. ' small: 1',
  9369. '};',
  9370. 'this.DoIt = function (a) {',
  9371. '};',
  9372. 'this.f = 0;',
  9373. 'this.s = {};',
  9374. 'this.Arr = [];',
  9375. 'this.i = 0;',
  9376. '']),
  9377. LinesToStr([ // $mod.$main
  9378. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9379. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9380. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9381. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9382. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9383. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9384. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9385. '$mod.i = 0;',
  9386. '$mod.i = rtl.length($mod.Arr) - 1;',
  9387. '$mod.DoIt($mod.Arr);',
  9388. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9389. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9390. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9391. '']));
  9392. end;
  9393. procedure TTestModule.TestArray_DynAsParam;
  9394. begin
  9395. StartProgram(false);
  9396. Add([
  9397. 'type integer = longint;',
  9398. 'type TArrInt = array of integer;',
  9399. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9400. 'var vJ: TArrInt;',
  9401. 'begin',
  9402. ' vg:=vg;',
  9403. ' vj:=vh;',
  9404. ' vi:=vi;',
  9405. ' doit(vg,vg,vg);',
  9406. ' doit(vh,vh,vj);',
  9407. ' doit(vi,vi,vi);',
  9408. ' doit(vj,vj,vj);',
  9409. 'end;',
  9410. 'var i: TArrInt;',
  9411. 'begin',
  9412. ' doit(i,i,i);']);
  9413. ConvertProgram;
  9414. CheckSource('TestArray_DynAsParams',
  9415. LinesToStr([ // statements
  9416. 'this.DoIt = function (vG,vH,vI) {',
  9417. ' var vJ = [];',
  9418. ' vG = rtl.arrayRef(vG);',
  9419. ' vJ = rtl.arrayRef(vH);',
  9420. ' vI.set(rtl.arrayRef(vI.get()));',
  9421. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9422. ' get: function () {',
  9423. ' return vG;',
  9424. ' },',
  9425. ' set: function (v) {',
  9426. ' vG = v;',
  9427. ' }',
  9428. ' });',
  9429. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9430. ' get: function () {',
  9431. ' return vJ;',
  9432. ' },',
  9433. ' set: function (v) {',
  9434. ' vJ = v;',
  9435. ' }',
  9436. ' });',
  9437. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9438. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9439. ' get: function () {',
  9440. ' return vJ;',
  9441. ' },',
  9442. ' set: function (v) {',
  9443. ' vJ = v;',
  9444. ' }',
  9445. ' });',
  9446. '};',
  9447. 'this.i = [];'
  9448. ]),
  9449. LinesToStr([
  9450. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9451. ' p: $mod,',
  9452. ' get: function () {',
  9453. ' return this.p.i;',
  9454. ' },',
  9455. ' set: function (v) {',
  9456. ' this.p.i = v;',
  9457. ' }',
  9458. '});'
  9459. ]));
  9460. end;
  9461. procedure TTestModule.TestArray_StaticAsParam;
  9462. begin
  9463. StartProgram(false);
  9464. Add([
  9465. 'type integer = longint;',
  9466. 'type TArrInt = array[1..2] of integer;',
  9467. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9468. 'var vJ: TArrInt;',
  9469. 'begin',
  9470. ' vg:=vg;',
  9471. ' vj:=vh;',
  9472. ' vi:=vi;',
  9473. ' doit(vg,vg,vg);',
  9474. ' doit(vh,vh,vj);',
  9475. ' doit(vi,vi,vi);',
  9476. ' doit(vj,vj,vj);',
  9477. 'end;',
  9478. 'var i: TArrInt;',
  9479. 'begin',
  9480. ' doit(i,i,i);']);
  9481. ConvertProgram;
  9482. CheckSource('TestArray_StaticAsParams',
  9483. LinesToStr([ // statements
  9484. 'this.DoIt = function (vG,vH,vI) {',
  9485. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9486. ' vG = vG.slice(0);',
  9487. ' vJ = vH.slice(0);',
  9488. ' vI.set(vI.get().slice(0));',
  9489. ' $mod.DoIt(vG.slice(0), vG, {',
  9490. ' get: function () {',
  9491. ' return vG;',
  9492. ' },',
  9493. ' set: function (v) {',
  9494. ' vG = v;',
  9495. ' }',
  9496. ' });',
  9497. ' $mod.DoIt(vH.slice(0), vH, {',
  9498. ' get: function () {',
  9499. ' return vJ;',
  9500. ' },',
  9501. ' set: function (v) {',
  9502. ' vJ = v;',
  9503. ' }',
  9504. ' });',
  9505. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9506. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9507. ' get: function () {',
  9508. ' return vJ;',
  9509. ' },',
  9510. ' set: function (v) {',
  9511. ' vJ = v;',
  9512. ' }',
  9513. ' });',
  9514. '};',
  9515. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9516. ]),
  9517. LinesToStr([
  9518. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9519. ' p: $mod,',
  9520. ' get: function () {',
  9521. ' return this.p.i;',
  9522. ' },',
  9523. ' set: function (v) {',
  9524. ' this.p.i = v;',
  9525. ' }',
  9526. '});'
  9527. ]));
  9528. end;
  9529. procedure TTestModule.TestArrayElement_AsParams;
  9530. begin
  9531. StartProgram(false);
  9532. Add('type integer = longint;');
  9533. Add('type TArrayInt = array of integer;');
  9534. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9535. Add('var vJ: tarrayint;');
  9536. Add('begin');
  9537. Add(' vi:=vi;');
  9538. Add(' doit(vi,vi,vi);');
  9539. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9540. Add('end;');
  9541. Add('var a: TArrayInt;');
  9542. Add('begin');
  9543. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9544. ConvertProgram;
  9545. CheckSource('TestArrayElement_AsParams',
  9546. LinesToStr([ // statements
  9547. 'this.DoIt = function (vG,vH,vI) {',
  9548. ' var vJ = [];',
  9549. ' vI.set(vI.get());',
  9550. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9551. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9552. ' a:1+3,',
  9553. ' p:vJ,',
  9554. ' get: function () {',
  9555. ' return this.p[this.a];',
  9556. ' },',
  9557. ' set: function (v) {',
  9558. ' this.p[this.a] = v;',
  9559. ' }',
  9560. ' });',
  9561. '};',
  9562. 'this.a = [];'
  9563. ]),
  9564. LinesToStr([
  9565. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9566. ' a: 1+6,',
  9567. ' p: $mod.a,',
  9568. ' get: function () {',
  9569. ' return this.p[this.a];',
  9570. ' },',
  9571. ' set: function (v) {',
  9572. ' this.p[this.a] = v;',
  9573. ' }',
  9574. '});'
  9575. ]));
  9576. end;
  9577. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9578. begin
  9579. StartProgram(false);
  9580. Add('type Integer = longint;');
  9581. Add('type TArrayInt = array of integer;');
  9582. Add('function GetArr(vB: integer = 0): tarrayint;');
  9583. Add('begin');
  9584. Add('end;');
  9585. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9586. Add('begin');
  9587. Add('end;');
  9588. Add('begin');
  9589. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9590. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9591. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9592. ConvertProgram;
  9593. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9594. LinesToStr([ // statements
  9595. 'this.GetArr = function (vB) {',
  9596. ' var Result = [];',
  9597. ' return Result;',
  9598. '};',
  9599. 'this.DoIt = function (vG,vH,vI) {',
  9600. '};'
  9601. ]),
  9602. LinesToStr([
  9603. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9604. ' a: 1+3,',
  9605. ' p: $mod.GetArr(0),',
  9606. ' get: function () {',
  9607. ' return this.p[this.a];',
  9608. ' },',
  9609. ' set: function (v) {',
  9610. ' this.p[this.a] = v;',
  9611. ' }',
  9612. '});',
  9613. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9614. ' a: 2+3,',
  9615. ' p: $mod.GetArr(0),',
  9616. ' get: function () {',
  9617. ' return this.p[this.a];',
  9618. ' },',
  9619. ' set: function (v) {',
  9620. ' this.p[this.a] = v;',
  9621. ' }',
  9622. '});',
  9623. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9624. ' a: 3+3,',
  9625. ' p: $mod.GetArr(9),',
  9626. ' get: function () {',
  9627. ' return this.p[this.a];',
  9628. ' },',
  9629. ' set: function (v) {',
  9630. ' this.p[this.a] = v;',
  9631. ' }',
  9632. '});',
  9633. '']));
  9634. end;
  9635. procedure TTestModule.TestArrayEnumTypeRange;
  9636. begin
  9637. StartProgram(false);
  9638. Add([
  9639. 'type',
  9640. ' TEnum = (red,blue);',
  9641. ' TEnumArray = array[TEnum] of longint;',
  9642. 'var',
  9643. ' e: TEnum;',
  9644. ' i: longint;',
  9645. ' a: TEnumArray;',
  9646. ' numbers: TEnumArray = (1,2);',
  9647. ' names: array[TEnum] of string = (''red'',''blue'');',
  9648. 'begin',
  9649. ' e:=low(a);',
  9650. ' e:=high(a);',
  9651. ' i:=a[red];',
  9652. ' a[e]:=a[e];']);
  9653. ConvertProgram;
  9654. CheckSource('TestArrayEnumTypeRange',
  9655. LinesToStr([ // statements
  9656. ' this.TEnum = {',
  9657. ' "0": "red",',
  9658. ' red: 0,',
  9659. ' "1": "blue",',
  9660. ' blue: 1',
  9661. '};',
  9662. 'this.e = 0;',
  9663. 'this.i = 0;',
  9664. 'this.a = rtl.arraySetLength(null,0,2);',
  9665. 'this.numbers = [1, 2];',
  9666. 'this.names = ["red", "blue"];',
  9667. '']),
  9668. LinesToStr([ // $mod.$main
  9669. '$mod.e = $mod.TEnum.red;',
  9670. '$mod.e = $mod.TEnum.blue;',
  9671. '$mod.i = $mod.a[$mod.TEnum.red];',
  9672. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9673. '']));
  9674. end;
  9675. procedure TTestModule.TestArray_SetLengthOutArg;
  9676. begin
  9677. StartProgram(false);
  9678. Add([
  9679. 'type TArrInt = array of longint;',
  9680. 'procedure DoIt(out a: TArrInt);',
  9681. 'begin',
  9682. ' SetLength(a,2);',
  9683. 'end;',
  9684. 'begin',
  9685. '']);
  9686. ConvertProgram;
  9687. CheckSource('TestArray_SetLengthOutArg',
  9688. LinesToStr([ // statements
  9689. 'this.DoIt = function (a) {',
  9690. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9691. '};',
  9692. '']),
  9693. LinesToStr([
  9694. '']));
  9695. end;
  9696. procedure TTestModule.TestArray_SetLengthProperty;
  9697. begin
  9698. StartProgram(false);
  9699. Add('type');
  9700. Add(' TArrInt = array of longint;');
  9701. Add(' TObject = class');
  9702. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9703. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9704. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9705. Add(' end;');
  9706. Add('var Obj: TObject;');
  9707. Add('begin');
  9708. Add(' SetLength(Obj.Colors,2);');
  9709. ConvertProgram;
  9710. CheckSource('TestArray_SetLengthProperty',
  9711. LinesToStr([ // statements
  9712. 'rtl.createClass(this, "TObject", null, function () {',
  9713. ' this.$init = function () {',
  9714. ' };',
  9715. ' this.$final = function () {',
  9716. ' };',
  9717. '});',
  9718. 'this.Obj = null;',
  9719. '']),
  9720. LinesToStr([
  9721. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9722. '']));
  9723. end;
  9724. procedure TTestModule.TestArray_SetLengthMultiDim;
  9725. begin
  9726. StartProgram(false);
  9727. Add([
  9728. 'type',
  9729. ' TArrArrInt = array of array of longint;',
  9730. ' TArrStaInt = array of array[1..2] of longint;',
  9731. 'var',
  9732. ' a: TArrArrInt;',
  9733. ' b: TArrStaInt;',
  9734. 'begin',
  9735. ' SetLength(a,2);',
  9736. ' SetLength(a,3,4);',
  9737. ' SetLength(b,5);',
  9738. '']);
  9739. ConvertProgram;
  9740. CheckSource('TestArray_SetLengthMultiDim',
  9741. LinesToStr([ // statements
  9742. 'this.a = [];',
  9743. 'this.b = [];',
  9744. '']),
  9745. LinesToStr([
  9746. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9747. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9748. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9749. '']));
  9750. end;
  9751. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9752. begin
  9753. StartProgram(false);
  9754. Add([
  9755. 'type',
  9756. ' TStaArr1 = array[1..3] of boolean;',
  9757. //' TStaArr2 = array[5..6] of TStaArr1;',
  9758. ' TDynArr1StaArr1 = array of TStaArr1;',
  9759. //' TDynArr1StaArr2 = array of TStaArr2;',
  9760. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9761. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9762. 'var',
  9763. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9764. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9765. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9766. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9767. 'begin',
  9768. ' SetLength(DynArr1StaArr1,11);',
  9769. ' SetLength(DynArr2StaArr1,12);',
  9770. ' SetLength(DynArr2StaArr1[13],14);',
  9771. ' SetLength(DynArr2StaArr1,15,16);',
  9772. //' SetLength(DynArr1StaArr2,21);',
  9773. //' SetLength(DynArr2StaArr2,22);',
  9774. //' SetLength(DynArr2StaArr2[23],24);',
  9775. //' SetLength(DynArr2StaArr2,25,26);',
  9776. '']);
  9777. ConvertProgram;
  9778. CheckSource('TestArray_DynOfStatic',
  9779. LinesToStr([ // statements
  9780. 'this.DynArr1StaArr1 = [];',
  9781. 'this.DynArr2StaArr1 = [];',
  9782. '']),
  9783. LinesToStr([ // $mod.$main
  9784. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9785. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9786. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9787. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9788. ' $mod.DynArr2StaArr1,',
  9789. ' false,',
  9790. ' 15,',
  9791. ' 16,',
  9792. ' "s",',
  9793. ' 3',
  9794. ');',
  9795. '']));
  9796. end;
  9797. procedure TTestModule.TestArray_OpenArrayOfString;
  9798. begin
  9799. StartProgram(false);
  9800. Add('procedure DoIt(const a: array of String);');
  9801. Add('var');
  9802. Add(' i: longint;');
  9803. Add(' s: string;');
  9804. Add('begin');
  9805. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9806. Add('end;');
  9807. Add('var s: string;');
  9808. Add('begin');
  9809. Add(' DoIt([]);');
  9810. Add(' DoIt([s,''foo'','''',s+s]);');
  9811. ConvertProgram;
  9812. CheckSource('TestArray_OpenArrayOfString',
  9813. LinesToStr([ // statements
  9814. 'this.DoIt = function (a) {',
  9815. ' var i = 0;',
  9816. ' var s = "";',
  9817. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9818. ' i = $l;',
  9819. ' s = a[rtl.length(a) - i - 1];',
  9820. ' };',
  9821. '};',
  9822. 'this.s = "";',
  9823. '']),
  9824. LinesToStr([
  9825. '$mod.DoIt([]);',
  9826. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9827. '']));
  9828. end;
  9829. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9830. begin
  9831. StartProgram(false);
  9832. Add([
  9833. 'type TArr = array of char;',
  9834. 'var',
  9835. ' c: char;',
  9836. ' s: string;',
  9837. ' a: TArr;',
  9838. 'procedure Run(const a: array of char);',
  9839. 'begin',
  9840. ' Run(c);',
  9841. ' Run(s);',
  9842. 'end;',
  9843. 'begin',
  9844. ' a:=c;',
  9845. ' a:=s;',
  9846. ' a:=#13;',
  9847. ' a:=''Foo'';',
  9848. ' Run(c);',
  9849. ' Run(s);',
  9850. '']);
  9851. ConvertProgram;
  9852. CheckSource('TestArray_ArrayOfCharAssignString',
  9853. LinesToStr([ // statements
  9854. 'this.c = "";',
  9855. 'this.s = "";',
  9856. 'this.a = [];',
  9857. 'this.Run = function (a) {',
  9858. ' $mod.Run($mod.c.split(""));',
  9859. ' $mod.Run($mod.s.split(""));',
  9860. '};',
  9861. '']),
  9862. LinesToStr([
  9863. '$mod.a = $mod.c.split("");',
  9864. '$mod.a = $mod.s.split("");',
  9865. '$mod.a = "\r".split("");',
  9866. '$mod.a = "Foo".split("");',
  9867. '$mod.Run($mod.c.split(""));',
  9868. '$mod.Run($mod.s.split(""));',
  9869. '']));
  9870. end;
  9871. procedure TTestModule.TestArray_ConstRef;
  9872. begin
  9873. StartProgram(false);
  9874. Add([
  9875. 'type TArr = array of word;',
  9876. 'procedure Run(constref a: TArr);',
  9877. 'begin',
  9878. 'end;',
  9879. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9880. 'var l: TArr;',
  9881. 'begin',
  9882. ' Run(l);',
  9883. ' Run(a);',
  9884. ' Run(b);',
  9885. ' Run(c);',
  9886. ' Run(d);',
  9887. ' Run(e);',
  9888. 'end;',
  9889. 'begin',
  9890. '']);
  9891. ConvertProgram;
  9892. CheckResolverUnexpectedHints();
  9893. CheckSource('TestArray_ConstRef',
  9894. LinesToStr([ // statements
  9895. 'this.Run = function (a) {',
  9896. '};',
  9897. 'this.Fly = function (a, b, c, d, e) {',
  9898. ' var l = [];',
  9899. ' $mod.Run(l);',
  9900. ' $mod.Run(a);',
  9901. ' $mod.Run(b.get());',
  9902. ' $mod.Run(c.get());',
  9903. ' $mod.Run(d);',
  9904. ' $mod.Run(e);',
  9905. '};',
  9906. '']),
  9907. LinesToStr([
  9908. '']));
  9909. end;
  9910. procedure TTestModule.TestArray_Concat;
  9911. begin
  9912. StartProgram(false);
  9913. Add([
  9914. 'type',
  9915. ' integer = longint;',
  9916. ' TFlag = (big,small);',
  9917. ' TFlags = set of TFlag;',
  9918. ' TRec = record',
  9919. ' i: integer;',
  9920. ' end;',
  9921. ' TArrInt = array of integer;',
  9922. ' TArrRec = array of TRec;',
  9923. ' TArrFlag = array of TFlag;',
  9924. ' TArrSet = array of TFlags;',
  9925. ' TArrJSValue = array of jsvalue;',
  9926. 'var',
  9927. ' ArrInt: tarrint;',
  9928. ' ArrRec: tarrrec;',
  9929. ' ArrFlag: tarrflag;',
  9930. ' ArrSet: tarrset;',
  9931. ' ArrJSValue: tarrjsvalue;',
  9932. 'begin',
  9933. ' arrint:=concat(arrint);',
  9934. ' arrint:=concat(arrint,arrint);',
  9935. ' arrint:=concat(arrint,arrint,arrint);',
  9936. ' arrrec:=concat(arrrec);',
  9937. ' arrrec:=concat(arrrec,arrrec);',
  9938. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9939. ' arrset:=concat(arrset);',
  9940. ' arrset:=concat(arrset,arrset);',
  9941. ' arrset:=concat(arrset,arrset,arrset);',
  9942. ' arrjsvalue:=concat(arrjsvalue);',
  9943. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9944. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9945. ' arrint:=concat([1],arrint);',
  9946. ' arrflag:=concat([big]);',
  9947. ' arrflag:=concat([big],arrflag);',
  9948. ' arrflag:=concat(arrflag,[small]);',
  9949. '']);
  9950. ConvertProgram;
  9951. CheckSource('TestArray_Concat',
  9952. LinesToStr([ // statements
  9953. 'this.TFlag = {',
  9954. ' "0": "big",',
  9955. ' big: 0,',
  9956. ' "1": "small",',
  9957. ' small: 1',
  9958. '};',
  9959. 'rtl.recNewT(this, "TRec", function () {',
  9960. ' this.i = 0;',
  9961. ' this.$eq = function (b) {',
  9962. ' return this.i === b.i;',
  9963. ' };',
  9964. ' this.$assign = function (s) {',
  9965. ' this.i = s.i;',
  9966. ' return this;',
  9967. ' };',
  9968. '});',
  9969. 'this.ArrInt = [];',
  9970. 'this.ArrRec = [];',
  9971. 'this.ArrFlag = [];',
  9972. 'this.ArrSet = [];',
  9973. 'this.ArrJSValue = [];',
  9974. '']),
  9975. LinesToStr([ // $mod.$main
  9976. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9977. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9978. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9979. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9980. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9981. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9982. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9983. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9984. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9985. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9986. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9987. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9988. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9989. '$mod.ArrFlag = [$mod.TFlag.big];',
  9990. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9991. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9992. '']));
  9993. end;
  9994. procedure TTestModule.TestArray_Copy;
  9995. begin
  9996. StartProgram(false);
  9997. Add([
  9998. 'type',
  9999. ' integer = longint;',
  10000. ' TFlag = (big,small);',
  10001. ' TFlags = set of TFlag;',
  10002. ' TRec = record',
  10003. ' i: integer;',
  10004. ' end;',
  10005. ' TArrInt = array of integer;',
  10006. ' TArrRec = array of TRec;',
  10007. ' TArrSet = array of TFlags;',
  10008. ' TArrJSValue = array of jsvalue;',
  10009. 'var',
  10010. ' ArrInt: tarrint;',
  10011. ' ArrRec: tarrrec;',
  10012. ' ArrSet: tarrset;',
  10013. ' ArrJSValue: tarrjsvalue;',
  10014. 'begin',
  10015. ' arrint:=copy(arrint);',
  10016. ' arrint:=copy(arrint,2);',
  10017. ' arrint:=copy(arrint,3,4);',
  10018. ' arrint:=copy([1,1],1,2);',
  10019. ' arrrec:=copy(arrrec);',
  10020. ' arrrec:=copy(arrrec,5);',
  10021. ' arrrec:=copy(arrrec,6,7);',
  10022. ' arrset:=copy(arrset);',
  10023. ' arrset:=copy(arrset,8);',
  10024. ' arrset:=copy(arrset,9,10);',
  10025. ' arrjsvalue:=copy(arrjsvalue);',
  10026. ' arrjsvalue:=copy(arrjsvalue,11);',
  10027. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10028. ' ']);
  10029. ConvertProgram;
  10030. CheckSource('TestArray_Copy',
  10031. LinesToStr([ // statements
  10032. 'this.TFlag = {',
  10033. ' "0": "big",',
  10034. ' big: 0,',
  10035. ' "1": "small",',
  10036. ' small: 1',
  10037. '};',
  10038. 'rtl.recNewT(this, "TRec", function () {',
  10039. ' this.i = 0;',
  10040. ' this.$eq = function (b) {',
  10041. ' return this.i === b.i;',
  10042. ' };',
  10043. ' this.$assign = function (s) {',
  10044. ' this.i = s.i;',
  10045. ' return this;',
  10046. ' };',
  10047. '});',
  10048. 'this.ArrInt = [];',
  10049. 'this.ArrRec = [];',
  10050. 'this.ArrSet = [];',
  10051. 'this.ArrJSValue = [];',
  10052. '']),
  10053. LinesToStr([ // $mod.$main
  10054. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10055. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10056. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10057. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10058. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10059. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10060. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10061. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10062. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10063. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10064. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10065. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10066. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10067. '']));
  10068. end;
  10069. procedure TTestModule.TestArray_InsertDelete;
  10070. begin
  10071. StartProgram(false);
  10072. Add([
  10073. 'type',
  10074. ' integer = longint;',
  10075. ' TFlag = (big,small);',
  10076. ' TFlags = set of TFlag;',
  10077. ' TRec = record',
  10078. ' i: integer;',
  10079. ' end;',
  10080. ' TArrInt = array of integer;',
  10081. ' TArrRec = array of TRec;',
  10082. ' TArrSet = array of TFlags;',
  10083. ' TArrJSValue = array of jsvalue;',
  10084. ' TArrArrInt = array of TArrInt;',
  10085. 'var',
  10086. ' ArrInt: tarrint;',
  10087. ' ArrRec: tarrrec;',
  10088. ' ArrSet: tarrset;',
  10089. ' ArrJSValue: tarrjsvalue;',
  10090. ' ArrArrInt: TArrArrInt;',
  10091. 'begin',
  10092. ' Insert(1,arrint,2);',
  10093. ' Insert(arrint[3],arrint,4);',
  10094. ' Insert(arrrec[5],arrrec,6);',
  10095. ' Insert(arrset[7],arrset,7);',
  10096. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10097. ' Insert(10,arrjsvalue,11);',
  10098. ' Insert([23],arrarrint,22);',
  10099. ' Delete(arrint,12,13);',
  10100. ' Delete(arrrec,14,15);',
  10101. ' Delete(arrset,17,18);',
  10102. ' Delete(arrjsvalue,19,10);']);
  10103. ConvertProgram;
  10104. CheckSource('TestArray_InsertDelete',
  10105. LinesToStr([ // statements
  10106. 'this.TFlag = {',
  10107. ' "0": "big",',
  10108. ' big: 0,',
  10109. ' "1": "small",',
  10110. ' small: 1',
  10111. '};',
  10112. 'rtl.recNewT(this, "TRec", function () {',
  10113. ' this.i = 0;',
  10114. ' this.$eq = function (b) {',
  10115. ' return this.i === b.i;',
  10116. ' };',
  10117. ' this.$assign = function (s) {',
  10118. ' this.i = s.i;',
  10119. ' return this;',
  10120. ' };',
  10121. '});',
  10122. 'this.ArrInt = [];',
  10123. 'this.ArrRec = [];',
  10124. 'this.ArrSet = [];',
  10125. 'this.ArrJSValue = [];',
  10126. 'this.ArrArrInt = [];',
  10127. '']),
  10128. LinesToStr([ // $mod.$main
  10129. '$mod.ArrInt.splice(2, 0, 1);',
  10130. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  10131. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  10132. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  10133. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  10134. '$mod.ArrJSValue.splice(11, 0, 10);',
  10135. '$mod.ArrArrInt.splice(22, 0, [23]);',
  10136. '$mod.ArrInt.splice(12, 13);',
  10137. '$mod.ArrRec.splice(14, 15);',
  10138. '$mod.ArrSet.splice(17, 18);',
  10139. '$mod.ArrJSValue.splice(19, 10);',
  10140. '']));
  10141. end;
  10142. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10143. begin
  10144. Parser.Options:=Parser.Options+[po_cassignments];
  10145. StartProgram(false);
  10146. Add([
  10147. '{$modeswitch arrayoperators}',
  10148. 'type',
  10149. ' integer = longint;',
  10150. ' TArrInt = array of integer;',
  10151. ' TArrStr = array of string;',
  10152. 'const',
  10153. ' Ints: TArrInt = (1,2,3);',
  10154. ' Aliases: TarrStr = (''foo'',''b'');',
  10155. ' OneInt: TArrInt = (7);',
  10156. ' OneStr: array of integer = (7);',
  10157. ' Chars: array of char = ''aoc'';',
  10158. ' Names: array of string = (''a'',''foo'');',
  10159. ' NameCount = low(Names)+high(Names)+length(Names);',
  10160. 'var i: integer;',
  10161. 'begin',
  10162. ' Ints:=[];',
  10163. ' Ints:=[1,1];',
  10164. ' Ints:=[1]+[2];',
  10165. ' Ints:=[2];',
  10166. ' Ints:=[]+ints;',
  10167. ' Ints:=Ints+[];',
  10168. ' Ints:=Ints+OneInt;',
  10169. ' Ints:=Ints+[1,1];',
  10170. ' Ints:=[i,i]+Ints;',
  10171. ' Ints:=[1]+[i]+[3];',
  10172. '']);
  10173. ConvertProgram;
  10174. CheckSource('TestArray_DynArrayConstObjFPC',
  10175. LinesToStr([ // statements
  10176. 'this.Ints = [1, 2, 3];',
  10177. 'this.Aliases = ["foo", "b"];',
  10178. 'this.OneInt = [7];',
  10179. 'this.OneStr = [7];',
  10180. 'this.Chars = ["a", "o", "c"];',
  10181. 'this.Names = ["a", "foo"];',
  10182. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10183. 'this.i = 0;',
  10184. '']),
  10185. LinesToStr([ // $mod.$main
  10186. '$mod.Ints = [];',
  10187. '$mod.Ints = [1, 1];',
  10188. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10189. '$mod.Ints = [2];',
  10190. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10191. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10192. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10193. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10194. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10195. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10196. '']));
  10197. end;
  10198. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10199. begin
  10200. StartProgram(false);
  10201. // Note: const c = [1,1]; defines a set!
  10202. Add([
  10203. '{$mode delphi}',
  10204. 'type',
  10205. ' integer = longint;',
  10206. ' TArrInt = array of integer;',
  10207. ' TArrStr = array of string;',
  10208. 'const',
  10209. ' Ints: TArrInt = [1,1,2];',
  10210. ' Aliases: TarrStr = [''foo'',''b''];',
  10211. ' OneInt: TArrInt = [7];',
  10212. ' OneStr: array of integer = [7]+[8];',
  10213. ' Chars: array of char = ''aoc'';',
  10214. ' Names: array of string = [''a'',''a''];',
  10215. ' NameCount = low(Names)+high(Names)+length(Names);',
  10216. 'begin',
  10217. '']);
  10218. ConvertProgram;
  10219. CheckSource('TestArray_DynArrayConstDelphi',
  10220. LinesToStr([ // statements
  10221. 'this.Ints = [1, 1, 2];',
  10222. 'this.Aliases = ["foo", "b"];',
  10223. 'this.OneInt = [7];',
  10224. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10225. 'this.Chars = ["a", "o", "c"];',
  10226. 'this.Names = ["a", "a"];',
  10227. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10228. '']),
  10229. LinesToStr([ // $mod.$main
  10230. '']));
  10231. end;
  10232. procedure TTestModule.TestArray_ArrayLitAsParam;
  10233. begin
  10234. StartProgram(false);
  10235. Add([
  10236. '{$modeswitch arrayoperators}',
  10237. 'type',
  10238. ' integer = longint;',
  10239. ' TArrInt = array of integer;',
  10240. ' TArrSet = array of (red,green,blue);',
  10241. 'procedure DoOpenInt(const a: array of integer); forward;',
  10242. 'procedure DoInt(const a: TArrInt);',
  10243. 'begin',
  10244. ' DoInt(a+[1]);',
  10245. ' DoInt([1]+a);',
  10246. ' DoOpenInt(a);',
  10247. ' DoOpenInt(a+[1]);',
  10248. ' DoOpenInt([1]+a);',
  10249. 'end;',
  10250. 'procedure DoOpenInt(const a: array of integer);',
  10251. 'begin',
  10252. ' DoOpenInt(a+[1]);',
  10253. ' DoOpenInt([1]+a);',
  10254. ' DoInt(a);',
  10255. ' DoInt(a+[1]);',
  10256. ' DoInt([1]+a);',
  10257. 'end;',
  10258. 'procedure DoSet(const a: TArrSet);',
  10259. 'begin',
  10260. ' DoSet(a+[red]);',
  10261. ' DoSet([blue]+a);',
  10262. 'end;',
  10263. 'var',
  10264. ' i: TArrInt;',
  10265. ' s: TArrSet;',
  10266. 'begin',
  10267. ' DoInt([1]);',
  10268. ' DoInt([1]+[2]);',
  10269. ' DoInt(i+[1]);',
  10270. ' DoInt([1]+i);',
  10271. ' DoOpenInt([1]);',
  10272. ' DoOpenInt([1]+[2]);',
  10273. ' DoOpenInt(i+[1]);',
  10274. ' DoOpenInt([1]+i);',
  10275. ' DoSet([red]);',
  10276. ' DoSet([blue]+[green]);',
  10277. ' DoSet(s+[blue]);',
  10278. ' DoSet([red]+s);',
  10279. '']);
  10280. ConvertProgram;
  10281. CheckSource('TestArray_ArrayLitAsParam',
  10282. LinesToStr([ // statements
  10283. 'this.TArrSet$a = {',
  10284. ' "0": "red",',
  10285. ' red: 0,',
  10286. ' "1": "green",',
  10287. ' green: 1,',
  10288. ' "2": "blue",',
  10289. ' blue: 2',
  10290. '};',
  10291. 'this.DoInt = function (a) {',
  10292. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10293. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10294. ' $mod.DoOpenInt(a);',
  10295. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10296. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10297. '};',
  10298. 'this.DoOpenInt = function (a) {',
  10299. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10300. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10301. ' $mod.DoInt(a);',
  10302. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10303. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10304. '};',
  10305. 'this.DoSet = function (a) {',
  10306. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10307. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10308. '};',
  10309. 'this.i = [];',
  10310. 'this.s = [];',
  10311. '']),
  10312. LinesToStr([ // $mod.$main
  10313. '$mod.DoInt([1]);',
  10314. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10315. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10316. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10317. '$mod.DoOpenInt([1]);',
  10318. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10319. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10320. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10321. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10322. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10323. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10324. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10325. '']));
  10326. end;
  10327. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10328. begin
  10329. StartProgram(false);
  10330. Add([
  10331. '{$modeswitch arrayoperators}',
  10332. 'type',
  10333. ' integer = longint;',
  10334. ' TArrInt = array of integer;',
  10335. ' TArrArrInt = array of TArrInt;',
  10336. 'procedure DoInt(const a: TArrArrInt);',
  10337. 'begin',
  10338. ' DoInt(a+[[1]]);',
  10339. ' DoInt([[1]]+a);',
  10340. ' DoInt(a);',
  10341. 'end;',
  10342. 'var',
  10343. ' i: TArrInt;',
  10344. ' a: TArrArrInt;',
  10345. 'begin',
  10346. ' a:=[[1]];',
  10347. ' a:=[i];',
  10348. ' a:=a+[i];',
  10349. ' a:=[i]+a;',
  10350. ' a:=[[1]+i];',
  10351. ' a:=[[1]+[2]];',
  10352. ' a:=[i+[2]];',
  10353. ' DoInt([[1]]);',
  10354. ' DoInt([[1]+[2],[3,4],[5]]);',
  10355. ' DoInt([i+[1]]+a);',
  10356. ' DoInt([i]+a);',
  10357. '']);
  10358. ConvertProgram;
  10359. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10360. LinesToStr([ // statements
  10361. 'this.DoInt = function (a) {',
  10362. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10363. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10364. ' $mod.DoInt(a);',
  10365. '};',
  10366. 'this.i = [];',
  10367. 'this.a = [];',
  10368. '']),
  10369. LinesToStr([ // $mod.$main
  10370. '$mod.a = [[1]];',
  10371. '$mod.a = [$mod.i];',
  10372. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10373. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10374. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10375. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10376. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10377. '$mod.DoInt([[1]]);',
  10378. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10379. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10380. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10381. '']));
  10382. end;
  10383. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10384. begin
  10385. StartProgram(false);
  10386. Add([
  10387. '{$modeswitch arrayoperators}',
  10388. 'type',
  10389. ' integer = longint;',
  10390. ' TArrInt = array[1..2] of integer;',
  10391. ' TArrArrInt = array of TArrInt;',
  10392. 'procedure DoInt(const a: TArrArrInt);',
  10393. 'begin',
  10394. ' DoInt(a+[[1,2]]);',
  10395. ' DoInt([[1,2]]+a);',
  10396. ' DoInt(a);',
  10397. 'end;',
  10398. 'var',
  10399. ' i: TArrInt;',
  10400. ' a: TArrArrInt;',
  10401. 'begin',
  10402. ' a:=[[1,1]];',
  10403. ' a:=[i];',
  10404. ' a:=a+[i];',
  10405. ' a:=[i]+a;',
  10406. ' DoInt([[1,1]]);',
  10407. ' DoInt([[1,2],[3,4]]);',
  10408. '']);
  10409. ConvertProgram;
  10410. CheckSource('TestArray_ArrayLitStaticAsParam',
  10411. LinesToStr([ // statements
  10412. 'this.DoInt = function (a) {',
  10413. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10414. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10415. ' $mod.DoInt(a);',
  10416. '};',
  10417. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10418. 'this.a = [];',
  10419. '']),
  10420. LinesToStr([ // $mod.$main
  10421. '$mod.a = [[1, 1]];',
  10422. '$mod.a = [$mod.i.slice(0)];',
  10423. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10424. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10425. '$mod.DoInt([[1, 1]]);',
  10426. '$mod.DoInt([[1, 2], [3, 4]]);',
  10427. '']));
  10428. end;
  10429. procedure TTestModule.TestArray_ForInArrOfString;
  10430. begin
  10431. StartProgram(false);
  10432. Add([
  10433. 'type',
  10434. 'type',
  10435. ' TMonthNameArray = array [1..12] of string;',
  10436. ' TMonthNames = TMonthNameArray;',
  10437. ' TObject = class',
  10438. ' private',
  10439. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10440. ' public',
  10441. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10442. ' end;',
  10443. 'var',
  10444. ' f: TObject;',
  10445. ' Month: string;',
  10446. ' Names: array of string = (''a'',''foo'',''bar'');',
  10447. ' i: longint;',
  10448. 'begin',
  10449. ' for Month in f.LongMonthNames do ;',
  10450. ' for Month in Names do ;',
  10451. ' for i:=low(Names) to high(Names) do ;',
  10452. '']);
  10453. ConvertProgram;
  10454. CheckSource('TestArray_ForInArrOfString',
  10455. LinesToStr([ // statements
  10456. 'rtl.createClass(this, "TObject", null, function () {',
  10457. ' this.$init = function () {',
  10458. ' };',
  10459. ' this.$final = function () {',
  10460. ' };',
  10461. '});',
  10462. 'this.f = null;',
  10463. 'this.Month = "";',
  10464. 'this.Names = ["a", "foo", "bar"];',
  10465. 'this.i = 0;',
  10466. '']),
  10467. LinesToStr([ // $mod.$main
  10468. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10469. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10470. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10471. '']));
  10472. end;
  10473. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10474. begin
  10475. StartProgram(false);
  10476. Add([
  10477. '{$modeswitch externalclass}',
  10478. 'type',
  10479. ' TJSObject = class external name ''Object''',
  10480. ' end;',
  10481. ' TJSArray = class external name ''Array''',
  10482. ' class function isArray(Value: JSValue) : boolean;',
  10483. ' function concat() : TJSArray; varargs;',
  10484. ' end;',
  10485. 'var',
  10486. ' aObj: TJSArray;',
  10487. ' a: array of longint;',
  10488. ' o: TJSObject;',
  10489. 'begin',
  10490. ' if TJSArray.isArray(65) then ;',
  10491. ' aObj:=TJSArray(a).concat(a);',
  10492. ' o:=TJSObject(a);']);
  10493. ConvertProgram;
  10494. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10495. LinesToStr([ // statements
  10496. 'this.aObj = null;',
  10497. 'this.a = [];',
  10498. 'this.o = null;',
  10499. '']),
  10500. LinesToStr([ // $mod.$main
  10501. 'if (Array.isArray(65)) ;',
  10502. '$mod.aObj = $mod.a.concat($mod.a);',
  10503. '$mod.o = $mod.a;',
  10504. '']));
  10505. end;
  10506. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10507. begin
  10508. StartProgram(false);
  10509. Add([
  10510. '{$modeswitch externalclass}',
  10511. 'type',
  10512. ' TArrStr = array of string;',
  10513. ' TJSArray = class external name ''Array''',
  10514. ' end;',
  10515. ' TJSObject = class external name ''Object''',
  10516. ' end;',
  10517. 'var',
  10518. ' aObj: TJSArray;',
  10519. ' a: TArrStr;',
  10520. ' jo: TJSObject;',
  10521. 'begin',
  10522. ' a:=TArrStr(aObj);',
  10523. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10524. ' a:=TarrStr(jo);',
  10525. '']);
  10526. ConvertProgram;
  10527. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10528. LinesToStr([ // statements
  10529. 'this.aObj = null;',
  10530. 'this.a = [];',
  10531. 'this.jo = null;',
  10532. '']),
  10533. LinesToStr([ // $mod.$main
  10534. '$mod.a = $mod.aObj;',
  10535. '$mod.aObj[1] = $mod.aObj[2];',
  10536. '$mod.a = $mod.jo;',
  10537. '']));
  10538. end;
  10539. procedure TTestModule.TestArrayOfConst_TVarRec;
  10540. begin
  10541. StartProgram(true,[supTVarRec]);
  10542. Add([
  10543. 'procedure Say(args: array of const);',
  10544. 'var',
  10545. ' i: longint;',
  10546. ' v: TVarRec;',
  10547. 'begin',
  10548. ' for i:=low(args) to high(args) do begin',
  10549. ' v:=args[i];',
  10550. ' case v.vtype of',
  10551. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10552. ' end;',
  10553. ' end;',
  10554. ' for v in args do ;',
  10555. ' args:=nil;',
  10556. ' SetLength(args,2);',
  10557. 'end;',
  10558. 'begin']);
  10559. ConvertProgram;
  10560. CheckSource('TestArrayOfConst_TVarRec',
  10561. LinesToStr([ // statements
  10562. 'this.Say = function (args) {',
  10563. ' var i = 0;',
  10564. ' var v = pas.system.TVarRec.$new();',
  10565. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10566. ' i = $l;',
  10567. ' v.$assign(args[i]);',
  10568. ' var $tmp = v.VType;',
  10569. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10570. ' };',
  10571. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10572. ' args = [];',
  10573. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10574. '};',
  10575. '']),
  10576. LinesToStr([ // $mod.$main
  10577. ]));
  10578. end;
  10579. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10580. begin
  10581. StartProgram(true,[supTVarRec]);
  10582. Add([
  10583. 'procedure Say(args: array of const);',
  10584. 'begin',
  10585. ' Say(args);',
  10586. 'end;',
  10587. 'var',
  10588. ' p: Pointer;',
  10589. ' j: jsvalue;',
  10590. ' c: currency;',
  10591. 'begin',
  10592. ' Say([]);',
  10593. ' Say([1]);',
  10594. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10595. '']);
  10596. ConvertProgram;
  10597. CheckSource('TestArrayOfConst_PassBaseTypes',
  10598. LinesToStr([ // statements
  10599. 'this.Say = function (args) {',
  10600. ' $mod.Say(args);',
  10601. '};',
  10602. 'this.p = null;',
  10603. 'this.j = undefined;',
  10604. 'this.c = 0;',
  10605. '']),
  10606. LinesToStr([ // $mod.$main
  10607. '$mod.Say([]);',
  10608. '$mod.Say(pas.system.VarRecs(0, 1));',
  10609. '$mod.Say(pas.system.VarRecs(',
  10610. ' 9,',
  10611. ' "c",',
  10612. ' 18,',
  10613. ' "foo",',
  10614. ' 5,',
  10615. ' null,',
  10616. ' 1,',
  10617. ' true,',
  10618. ' 3,',
  10619. ' 1.3,',
  10620. ' 5,',
  10621. ' $mod.p,',
  10622. ' 20,',
  10623. ' $mod.j,',
  10624. ' 12,',
  10625. ' $mod.c',
  10626. ' ));',
  10627. '']));
  10628. end;
  10629. procedure TTestModule.TestArrayOfConst_PassObj;
  10630. begin
  10631. StartProgram(true,[supTVarRec]);
  10632. Add([
  10633. '{$interfaces corba}',
  10634. 'type',
  10635. ' TObject = class',
  10636. ' end;',
  10637. ' TClass = class of TObject;',
  10638. ' IUnknown = interface',
  10639. ' end;',
  10640. 'procedure Say(args: array of const);',
  10641. 'begin',
  10642. 'end;',
  10643. 'var',
  10644. ' o: TObject;',
  10645. ' c: TClass;',
  10646. ' i: IUnknown;',
  10647. 'begin',
  10648. ' Say([o,c,TObject]);',
  10649. ' Say([nil,i]);',
  10650. '']);
  10651. ConvertProgram;
  10652. CheckSource('TestArrayOfConst_PassObj',
  10653. LinesToStr([ // statements
  10654. 'rtl.createClass(this, "TObject", null, function () {',
  10655. ' this.$init = function () {',
  10656. ' };',
  10657. ' this.$final = function () {',
  10658. ' };',
  10659. '});',
  10660. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10661. 'this.Say = function (args) {',
  10662. '};',
  10663. 'this.o = null;',
  10664. 'this.c = null;',
  10665. 'this.i = null;',
  10666. '']),
  10667. LinesToStr([ // $mod.$main
  10668. '$mod.Say(pas.system.VarRecs(',
  10669. ' 7,',
  10670. ' $mod.o,',
  10671. ' 8,',
  10672. ' $mod.c,',
  10673. ' 8,',
  10674. ' $mod.TObject',
  10675. '));',
  10676. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10677. '']));
  10678. end;
  10679. procedure TTestModule.TestRecord_Empty;
  10680. begin
  10681. StartProgram(false);
  10682. Add([
  10683. 'type',
  10684. ' TRecA = record',
  10685. ' end;',
  10686. 'var a,b: TRecA;',
  10687. 'begin',
  10688. ' if a=b then ;']);
  10689. ConvertProgram;
  10690. CheckSource('TestRecord_Empty',
  10691. LinesToStr([ // statements
  10692. 'rtl.recNewT(this, "TRecA", function () {',
  10693. ' this.$eq = function (b) {',
  10694. ' return true;',
  10695. ' };',
  10696. ' this.$assign = function (s) {',
  10697. ' return this;',
  10698. ' };',
  10699. '});',
  10700. 'this.a = this.TRecA.$new();',
  10701. 'this.b = this.TRecA.$new();',
  10702. '']),
  10703. LinesToStr([ // $mod.$main
  10704. 'if ($mod.a.$eq($mod.b)) ;'
  10705. ]));
  10706. end;
  10707. procedure TTestModule.TestRecord_Var;
  10708. begin
  10709. StartProgram(false);
  10710. Add('type');
  10711. Add(' TRecA = record');
  10712. Add(' Bold: longint;');
  10713. Add(' end;');
  10714. Add('var Rec: TRecA;');
  10715. Add('begin');
  10716. Add(' rec.bold:=123');
  10717. ConvertProgram;
  10718. CheckSource('TestRecord_Var',
  10719. LinesToStr([ // statements
  10720. 'rtl.recNewT(this, "TRecA", function () {',
  10721. ' this.Bold = 0;',
  10722. ' this.$eq = function (b) {',
  10723. ' return this.Bold === b.Bold;',
  10724. ' };',
  10725. ' this.$assign = function (s) {',
  10726. ' this.Bold = s.Bold;',
  10727. ' return this;',
  10728. ' };',
  10729. '});',
  10730. 'this.Rec = this.TRecA.$new();',
  10731. '']),
  10732. LinesToStr([ // $mod.$main
  10733. '$mod.Rec.Bold = 123;'
  10734. ]));
  10735. end;
  10736. procedure TTestModule.TestRecord_VarExternal;
  10737. begin
  10738. StartProgram(false);
  10739. Add([
  10740. '{$modeswitch externalclass}',
  10741. 'type',
  10742. ' TRecA = record',
  10743. ' i: byte;',
  10744. ' length_: longint external name ''length'';',
  10745. ' end;',
  10746. 'var Rec: TRecA;',
  10747. 'begin',
  10748. ' rec.length_ := rec.length_',
  10749. '']);
  10750. ConvertProgram;
  10751. CheckSource('TestRecord_VarExternal',
  10752. LinesToStr([ // statements
  10753. 'rtl.recNewT(this, "TRecA", function () {',
  10754. ' this.i = 0;',
  10755. ' this.$eq = function (b) {',
  10756. ' return (this.i === b.i) && (this.length === b.length);',
  10757. ' };',
  10758. ' this.$assign = function (s) {',
  10759. ' this.i = s.i;',
  10760. ' this.length = s.length;',
  10761. ' return this;',
  10762. ' };',
  10763. '});',
  10764. 'this.Rec = this.TRecA.$new();',
  10765. '']),
  10766. LinesToStr([ // $mod.$main
  10767. '$mod.Rec.length = $mod.Rec.length;'
  10768. ]));
  10769. end;
  10770. procedure TTestModule.TestRecord_WithDo;
  10771. begin
  10772. StartProgram(false);
  10773. Add('type');
  10774. Add(' TRec = record');
  10775. Add(' vI: longint;');
  10776. Add(' end;');
  10777. Add('var');
  10778. Add(' Int: longint;');
  10779. Add(' r: TRec;');
  10780. Add('begin');
  10781. Add(' with r do');
  10782. Add(' int:=vi;');
  10783. Add(' with r do begin');
  10784. Add(' int:=vi;');
  10785. Add(' vi:=int;');
  10786. Add(' end;');
  10787. ConvertProgram;
  10788. CheckSource('TestWithRecordDo',
  10789. LinesToStr([ // statements
  10790. 'rtl.recNewT(this, "TRec", function () {',
  10791. ' this.vI = 0;',
  10792. ' this.$eq = function (b) {',
  10793. ' return this.vI === b.vI;',
  10794. ' };',
  10795. ' this.$assign = function (s) {',
  10796. ' this.vI = s.vI;',
  10797. ' return this;',
  10798. ' };',
  10799. '});',
  10800. 'this.Int = 0;',
  10801. 'this.r = this.TRec.$new();',
  10802. '']),
  10803. LinesToStr([ // $mod.$main
  10804. 'var $with = $mod.r;',
  10805. '$mod.Int = $with.vI;',
  10806. 'var $with1 = $mod.r;',
  10807. '$mod.Int = $with1.vI;',
  10808. '$with1.vI = $mod.Int;'
  10809. ]));
  10810. end;
  10811. procedure TTestModule.TestRecord_Assign;
  10812. begin
  10813. StartProgram(false);
  10814. Add([
  10815. 'type',
  10816. ' TEnum = (red,green);',
  10817. ' TEnums = set of TEnum;',
  10818. ' TSmallRec = record',
  10819. ' N: longint;',
  10820. ' end;',
  10821. ' TBigRec = record',
  10822. ' Int: longint;',
  10823. ' D: double;',
  10824. ' Arr: array of longint;',
  10825. ' Arr2: array[1..2] of longint;',
  10826. ' Small: TSmallRec;',
  10827. ' Enums: TEnums;',
  10828. ' end;',
  10829. 'var',
  10830. ' r, s: TBigRec;',
  10831. 'begin',
  10832. ' r:=s;',
  10833. ' r:=default(TBigRec);',
  10834. ' r:=default(s);',
  10835. '']);
  10836. ConvertProgram;
  10837. CheckSource('TestRecord_Assign',
  10838. LinesToStr([ // statements
  10839. 'this.TEnum = {',
  10840. ' "0": "red",',
  10841. ' red: 0,',
  10842. ' "1": "green",',
  10843. ' green: 1',
  10844. '};',
  10845. 'rtl.recNewT(this, "TSmallRec", function () {',
  10846. ' this.N = 0;',
  10847. ' this.$eq = function (b) {',
  10848. ' return this.N === b.N;',
  10849. ' };',
  10850. ' this.$assign = function (s) {',
  10851. ' this.N = s.N;',
  10852. ' return this;',
  10853. ' };',
  10854. '});',
  10855. 'rtl.recNewT(this, "TBigRec", function () {',
  10856. ' this.Int = 0;',
  10857. ' this.D = 0.0;',
  10858. ' this.$new = function () {',
  10859. ' var r = Object.create(this);',
  10860. ' r.Arr = [];',
  10861. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10862. ' r.Small = $mod.TSmallRec.$new();',
  10863. ' r.Enums = {};',
  10864. ' return r;',
  10865. ' };',
  10866. ' this.$eq = function (b) {',
  10867. ' 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);',
  10868. ' };',
  10869. ' this.$assign = function (s) {',
  10870. ' this.Int = s.Int;',
  10871. ' this.D = s.D;',
  10872. ' this.Arr = rtl.arrayRef(s.Arr);',
  10873. ' this.Arr2 = s.Arr2.slice(0);',
  10874. ' this.Small.$assign(s.Small);',
  10875. ' this.Enums = rtl.refSet(s.Enums);',
  10876. ' return this;',
  10877. ' };',
  10878. '});',
  10879. 'this.r = this.TBigRec.$new();',
  10880. 'this.s = this.TBigRec.$new();',
  10881. '']),
  10882. LinesToStr([ // $mod.$main
  10883. '$mod.r.$assign($mod.s);',
  10884. '$mod.r.$assign($mod.TBigRec.$new());',
  10885. '$mod.r.$assign($mod.TBigRec.$new());',
  10886. '']));
  10887. end;
  10888. procedure TTestModule.TestRecord_AsParams;
  10889. begin
  10890. StartProgram(false);
  10891. Add([
  10892. 'type',
  10893. ' integer = longint;',
  10894. ' TRecord = record',
  10895. ' i: integer;',
  10896. ' end;',
  10897. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10898. 'var vL: TRecord;',
  10899. 'begin',
  10900. ' vd:=vd;',
  10901. ' vd.i:=vd.i;',
  10902. ' vl:=vc;',
  10903. ' vv:=vv;',
  10904. ' vv.i:=vv.i;',
  10905. ' U:=vl;',
  10906. ' U:=vd;',
  10907. ' U:=vc;',
  10908. ' U:=vv;',
  10909. ' vl:=TRecord(U);',
  10910. ' vd:=TRecord(U);',
  10911. ' vv:=TRecord(U);',
  10912. ' doit(vd,vd,vd,vd);',
  10913. ' doit(vc,vc,vl,vl);',
  10914. ' doit(vv,vv,vv,vv);',
  10915. ' doit(vl,vl,vl,vl);',
  10916. ' TRecord(U).i:=3;',
  10917. 'end;',
  10918. 'var i: TRecord;',
  10919. 'begin',
  10920. ' doit(i,i,i,i);',
  10921. '']);
  10922. ConvertProgram;
  10923. CheckSource('TestRecord_AsParams',
  10924. LinesToStr([ // statements
  10925. 'rtl.recNewT(this, "TRecord", function () {',
  10926. ' this.i = 0;',
  10927. ' this.$eq = function (b) {',
  10928. ' return this.i === b.i;',
  10929. ' };',
  10930. ' this.$assign = function (s) {',
  10931. ' this.i = s.i;',
  10932. ' return this;',
  10933. ' };',
  10934. '});',
  10935. 'this.DoIt = function (vD, vC, vV, U) {',
  10936. ' var vL = $mod.TRecord.$new();',
  10937. ' vD.$assign(vD);',
  10938. ' vD.i = vD.i;',
  10939. ' vL.$assign(vC);',
  10940. ' vV.$assign(vV);',
  10941. ' vV.i = vV.i;',
  10942. ' U.$assign(vL);',
  10943. ' U.$assign(vD);',
  10944. ' U.$assign(vC);',
  10945. ' U.$assign(vV);',
  10946. ' vL.$assign(U);',
  10947. ' vD.$assign(U);',
  10948. ' vV.$assign(U);',
  10949. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10950. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10951. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10952. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10953. ' U.i = 3;',
  10954. '};',
  10955. 'this.i = this.TRecord.$new();'
  10956. ]),
  10957. LinesToStr([
  10958. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10959. '']));
  10960. end;
  10961. procedure TTestModule.TestRecord_ConstRef;
  10962. begin
  10963. StartProgram(false);
  10964. Add([
  10965. 'type TRec = record i: word; end;',
  10966. 'procedure Run(constref a: TRec);',
  10967. 'begin',
  10968. 'end;',
  10969. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10970. 'var l: TRec;',
  10971. 'begin',
  10972. ' Run(l);',
  10973. ' Run(a);',
  10974. ' Run(b);',
  10975. ' Run(c);',
  10976. ' Run(d);',
  10977. ' Run(e);',
  10978. 'end;',
  10979. 'begin',
  10980. '']);
  10981. ConvertProgram;
  10982. CheckResolverUnexpectedHints();
  10983. CheckSource('TestRecord_ConstRef',
  10984. LinesToStr([ // statements
  10985. 'rtl.recNewT(this, "TRec", 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.Run = function (a) {',
  10996. '};',
  10997. 'this.Fly = function (a, b, c, d, e) {',
  10998. ' var l = $mod.TRec.$new();',
  10999. ' $mod.Run(l);',
  11000. ' $mod.Run(a);',
  11001. ' $mod.Run(b);',
  11002. ' $mod.Run(c);',
  11003. ' $mod.Run(d);',
  11004. ' $mod.Run(e);',
  11005. '};',
  11006. '']),
  11007. LinesToStr([
  11008. '']));
  11009. end;
  11010. procedure TTestModule.TestRecordElement_AsParams;
  11011. begin
  11012. StartProgram(false);
  11013. Add('type');
  11014. Add(' integer = longint;');
  11015. Add(' TRecord = record');
  11016. Add(' i: integer;');
  11017. Add(' end;');
  11018. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11019. Add('var vJ: TRecord;');
  11020. Add('begin');
  11021. Add(' doit(vj.i,vj.i,vj.i);');
  11022. Add('end;');
  11023. Add('var r: TRecord;');
  11024. Add('begin');
  11025. Add(' doit(r.i,r.i,r.i);');
  11026. ConvertProgram;
  11027. CheckSource('TestRecordElement_AsParams',
  11028. LinesToStr([ // statements
  11029. 'rtl.recNewT(this, "TRecord", function () {',
  11030. ' this.i = 0;',
  11031. ' this.$eq = function (b) {',
  11032. ' return this.i === b.i;',
  11033. ' };',
  11034. ' this.$assign = function (s) {',
  11035. ' this.i = s.i;',
  11036. ' return this;',
  11037. ' };',
  11038. '});',
  11039. 'this.DoIt = function (vG,vH,vI) {',
  11040. ' var vJ = $mod.TRecord.$new();',
  11041. ' $mod.DoIt(vJ.i, vJ.i, {',
  11042. ' p: vJ,',
  11043. ' get: function () {',
  11044. ' return this.p.i;',
  11045. ' },',
  11046. ' set: function (v) {',
  11047. ' this.p.i = v;',
  11048. ' }',
  11049. ' });',
  11050. '};',
  11051. 'this.r = this.TRecord.$new();'
  11052. ]),
  11053. LinesToStr([
  11054. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11055. ' p: $mod.r,',
  11056. ' get: function () {',
  11057. ' return this.p.i;',
  11058. ' },',
  11059. ' set: function (v) {',
  11060. ' this.p.i = v;',
  11061. ' }',
  11062. '});'
  11063. ]));
  11064. end;
  11065. procedure TTestModule.TestRecordElementFromFuncResult_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('function GetRec(vB: integer = 0): TRecord;');
  11074. Add('begin');
  11075. Add('end;');
  11076. Add('procedure DoIt(vG: integer; const vH: integer);');
  11077. Add('begin');
  11078. Add('end;');
  11079. Add('begin');
  11080. Add(' doit(getrec.i,getrec.i);');
  11081. Add(' doit(getrec().i,getrec().i);');
  11082. Add(' doit(getrec(1).i,getrec(2).i);');
  11083. ConvertProgram;
  11084. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11085. LinesToStr([ // statements
  11086. 'rtl.recNewT(this, "TRecord", function () {',
  11087. ' this.i = 0;',
  11088. ' this.$eq = function (b) {',
  11089. ' return this.i === b.i;',
  11090. ' };',
  11091. ' this.$assign = function (s) {',
  11092. ' this.i = s.i;',
  11093. ' return this;',
  11094. ' };',
  11095. '});',
  11096. 'this.GetRec = function (vB) {',
  11097. ' var Result = $mod.TRecord.$new();',
  11098. ' return Result;',
  11099. '};',
  11100. 'this.DoIt = function (vG, vH) {',
  11101. '};',
  11102. '']),
  11103. LinesToStr([
  11104. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11105. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11106. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11107. '']));
  11108. end;
  11109. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11110. begin
  11111. StartProgram(false);
  11112. Add('type');
  11113. Add(' integer = longint;');
  11114. Add(' TRecord = record');
  11115. Add(' i: integer;');
  11116. Add(' end;');
  11117. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11118. Add('begin');
  11119. Add('end;');
  11120. Add('var r: trecord;');
  11121. Add('begin');
  11122. Add(' with r do ');
  11123. Add(' doit(i,i,i);');
  11124. ConvertProgram;
  11125. CheckSource('TestRecordElementFromWith_AsParams',
  11126. LinesToStr([ // statements
  11127. 'rtl.recNewT(this, "TRecord", function () {',
  11128. ' this.i = 0;',
  11129. ' this.$eq = function (b) {',
  11130. ' return this.i === b.i;',
  11131. ' };',
  11132. ' this.$assign = function (s) {',
  11133. ' this.i = s.i;',
  11134. ' return this;',
  11135. ' };',
  11136. '});',
  11137. 'this.DoIt = function (vG,vH,vI) {',
  11138. '};',
  11139. 'this.r = this.TRecord.$new();'
  11140. ]),
  11141. LinesToStr([
  11142. 'var $with = $mod.r;',
  11143. '$mod.DoIt($with.i,$with.i,{',
  11144. ' p: $with,',
  11145. ' get: function () {',
  11146. ' return this.p.i;',
  11147. ' },',
  11148. ' set: function (v) {',
  11149. ' this.p.i = v;',
  11150. ' }',
  11151. '});',
  11152. '']));
  11153. end;
  11154. procedure TTestModule.TestRecord_Equal;
  11155. begin
  11156. StartProgram(false);
  11157. Add('type');
  11158. Add(' integer = longint;');
  11159. Add(' TFlag = (red,blue);');
  11160. Add(' TFlags = set of TFlag;');
  11161. Add(' TProc = procedure;');
  11162. Add(' TRecord = record');
  11163. Add(' i: integer;');
  11164. Add(' Event: TProc;');
  11165. Add(' f: TFlags;');
  11166. Add(' end;');
  11167. Add(' TNested = record');
  11168. Add(' r: TRecord;');
  11169. Add(' end;');
  11170. Add('var');
  11171. Add(' b: boolean;');
  11172. Add(' r,s: trecord;');
  11173. Add('begin');
  11174. Add(' b:=r=s;');
  11175. Add(' b:=r<>s;');
  11176. ConvertProgram;
  11177. CheckSource('TestRecord_Equal',
  11178. LinesToStr([ // statements
  11179. 'this.TFlag = {',
  11180. ' "0": "red",',
  11181. ' red: 0,',
  11182. ' "1": "blue",',
  11183. ' blue: 1',
  11184. '};',
  11185. 'rtl.recNewT(this, "TRecord", function () {',
  11186. ' this.i = 0;',
  11187. ' this.Event = null;',
  11188. ' this.$new = function () {',
  11189. ' var r = Object.create(this);',
  11190. ' r.f = {};',
  11191. ' return r;',
  11192. ' };',
  11193. ' this.$eq = function (b) {',
  11194. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11195. ' };',
  11196. ' this.$assign = function (s) {',
  11197. ' this.i = s.i;',
  11198. ' this.Event = s.Event;',
  11199. ' this.f = rtl.refSet(s.f);',
  11200. ' return this;',
  11201. ' };',
  11202. '});',
  11203. 'rtl.recNewT(this, "TNested", function () {',
  11204. ' this.$new = function () {',
  11205. ' var r = Object.create(this);',
  11206. ' r.r = $mod.TRecord.$new();',
  11207. ' return r;',
  11208. ' };',
  11209. ' this.$eq = function (b) {',
  11210. ' return this.r.$eq(b.r);',
  11211. ' };',
  11212. ' this.$assign = function (s) {',
  11213. ' this.r.$assign(s.r);',
  11214. ' return this;',
  11215. ' };',
  11216. '});',
  11217. 'this.b = false;',
  11218. 'this.r = this.TRecord.$new();',
  11219. 'this.s = this.TRecord.$new();',
  11220. '']),
  11221. LinesToStr([
  11222. '$mod.b = $mod.r.$eq($mod.s);',
  11223. '$mod.b = !$mod.r.$eq($mod.s);',
  11224. '']));
  11225. end;
  11226. procedure TTestModule.TestRecord_JSValue;
  11227. begin
  11228. StartProgram(false);
  11229. Add([
  11230. 'type',
  11231. ' TRecord = record',
  11232. ' i: longint;',
  11233. ' end;',
  11234. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11235. 'begin',
  11236. 'end;',
  11237. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11238. 'begin',
  11239. ' if jsvalue(d) then ;',
  11240. ' if jsvalue(c) then ;',
  11241. ' if jsvalue(v) then ;',
  11242. 'end;',
  11243. 'var',
  11244. ' Jv: jsvalue;',
  11245. ' Rec: trecord;',
  11246. 'begin',
  11247. ' rec:=trecord(jv);',
  11248. ' jv:=rec;',
  11249. ' Fly(rec,rec);',
  11250. ' Fly(@rec,@rec);',
  11251. ' if jsvalue(Rec) then ;',
  11252. ' Run(trecord(jv),trecord(jv),rec);',
  11253. '']);
  11254. ConvertProgram;
  11255. CheckSource('TestRecord_JSValue',
  11256. LinesToStr([ // statements
  11257. 'rtl.recNewT(this, "TRecord", function () {',
  11258. ' this.i = 0;',
  11259. ' this.$eq = function (b) {',
  11260. ' return this.i === b.i;',
  11261. ' };',
  11262. ' this.$assign = function (s) {',
  11263. ' this.i = s.i;',
  11264. ' return this;',
  11265. ' };',
  11266. '});',
  11267. 'this.Fly = function (d, c) {',
  11268. '};',
  11269. 'this.Run = function (d, c, v) {',
  11270. ' if (d) ;',
  11271. ' if (c) ;',
  11272. ' if (v) ;',
  11273. '};',
  11274. 'this.Jv = undefined;',
  11275. 'this.Rec = this.TRecord.$new();',
  11276. '']),
  11277. LinesToStr([
  11278. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11279. '$mod.Jv = $mod.Rec;',
  11280. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11281. '$mod.Fly($mod.Rec, $mod.Rec);',
  11282. 'if ($mod.Rec) ;',
  11283. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11284. '']));
  11285. end;
  11286. procedure TTestModule.TestRecord_VariantFail;
  11287. begin
  11288. StartProgram(false);
  11289. Add([
  11290. 'type',
  11291. ' TRec = record',
  11292. ' case word of',
  11293. ' 0: (b0, b1: Byte);',
  11294. ' 1: (i: word);',
  11295. ' end;',
  11296. 'begin']);
  11297. SetExpectedPasResolverError('variant record is not supported',
  11298. nXIsNotSupported);
  11299. ConvertProgram;
  11300. end;
  11301. procedure TTestModule.TestRecord_FieldArray;
  11302. begin
  11303. StartProgram(false);
  11304. Add([
  11305. 'type',
  11306. ' TArrInt = array[3..4] of longint;',
  11307. ' TArrArrInt = array[3..4] of longint;',
  11308. ' TRec = record',
  11309. ' a: array of longint;',
  11310. ' s: array[1..2] of longint;',
  11311. ' m: array[1..2,3..4] of longint;',
  11312. ' o: TArrArrInt;',
  11313. ' end;',
  11314. 'begin']);
  11315. ConvertProgram;
  11316. CheckSource('TestRecord_FieldArray',
  11317. LinesToStr([ // statements
  11318. 'rtl.recNewT(this, "TRec", function () {',
  11319. ' this.$new = function () {',
  11320. ' var r = Object.create(this);',
  11321. ' r.a = [];',
  11322. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11323. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11324. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11325. ' return r;',
  11326. ' };',
  11327. ' this.$eq = function (b) {',
  11328. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11329. ' };',
  11330. ' this.$assign = function (s) {',
  11331. ' this.a = rtl.arrayRef(s.a);',
  11332. ' this.s = s.s.slice(0);',
  11333. ' this.m = s.m.slice(0);',
  11334. ' this.o = s.o.slice(0);',
  11335. ' return this;',
  11336. ' };',
  11337. '});',
  11338. '']),
  11339. LinesToStr([ // $mod.$main
  11340. '']));
  11341. end;
  11342. procedure TTestModule.TestRecord_Const;
  11343. begin
  11344. StartProgram(false);
  11345. Add([
  11346. 'type',
  11347. ' TArrInt = array[3..4] of longint;',
  11348. ' TPoint = record x,y: longint; end;',
  11349. ' TRec = record',
  11350. ' i: longint;',
  11351. ' a: array of longint;',
  11352. ' s: array[1..2] of longint;',
  11353. ' m: array[1..2,3..4] of longint;',
  11354. ' p: TPoint;',
  11355. ' end;',
  11356. ' TPoints = array of TPoint;',
  11357. 'const',
  11358. ' r: TRec = (',
  11359. ' i:1;',
  11360. ' a:(2,3);',
  11361. ' s:(4,5);',
  11362. ' m:( (11,12), (13,14) );',
  11363. ' p: (x:21; y:22)',
  11364. ' );',
  11365. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11366. 'begin']);
  11367. ConvertProgram;
  11368. CheckSource('TestRecord_Const',
  11369. LinesToStr([ // statements
  11370. 'rtl.recNewT(this, "TPoint", function () {',
  11371. ' this.x = 0;',
  11372. ' this.y = 0;',
  11373. ' this.$eq = function (b) {',
  11374. ' return (this.x === b.x) && (this.y === b.y);',
  11375. ' };',
  11376. ' this.$assign = function (s) {',
  11377. ' this.x = s.x;',
  11378. ' this.y = s.y;',
  11379. ' return this;',
  11380. ' };',
  11381. '});',
  11382. 'rtl.recNewT(this, "TRec", function () {',
  11383. ' this.i = 0;',
  11384. ' this.$new = function () {',
  11385. ' var r = Object.create(this);',
  11386. ' r.a = [];',
  11387. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11388. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11389. ' r.p = $mod.TPoint.$new();',
  11390. ' return r;',
  11391. ' };',
  11392. ' this.$eq = function (b) {',
  11393. ' 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);',
  11394. ' };',
  11395. ' this.$assign = function (s) {',
  11396. ' this.i = s.i;',
  11397. ' this.a = rtl.arrayRef(s.a);',
  11398. ' this.s = s.s.slice(0);',
  11399. ' this.m = s.m.slice(0);',
  11400. ' this.p.$assign(s.p);',
  11401. ' return this;',
  11402. ' };',
  11403. '});',
  11404. 'this.r = this.TRec.$clone({',
  11405. ' i: 1,',
  11406. ' a: [2, 3],',
  11407. ' s: [4, 5],',
  11408. ' m: [[11, 12], [13, 14]],',
  11409. ' p: this.TPoint.$clone({',
  11410. ' x: 21,',
  11411. ' y: 22',
  11412. ' })',
  11413. '});',
  11414. 'this.p = [this.TPoint.$clone({',
  11415. ' x: 1,',
  11416. ' y: 2',
  11417. '}), this.TPoint.$clone({',
  11418. ' x: 3,',
  11419. ' y: 4',
  11420. '})];',
  11421. '']),
  11422. LinesToStr([ // $mod.$main
  11423. '']));
  11424. end;
  11425. procedure TTestModule.TestRecord_TypecastFail;
  11426. begin
  11427. StartProgram(false);
  11428. Add([
  11429. 'type',
  11430. ' TPoint = record x,y: longint; end;',
  11431. ' TRec = record l: longint end;',
  11432. 'var p: TPoint;',
  11433. 'begin',
  11434. ' if TRec(p).l=2 then ;']);
  11435. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11436. nIllegalTypeConversionTo);
  11437. ConvertProgram;
  11438. end;
  11439. procedure TTestModule.TestRecord_InFunction;
  11440. begin
  11441. StartProgram(false);
  11442. Add([
  11443. 'var TPoint: longint = 3;',
  11444. 'procedure DoIt;',
  11445. 'type',
  11446. ' TPoint = record x,y: longint; end;',
  11447. ' TPoints = array of TPoint;',
  11448. 'var',
  11449. ' r: TPoint;',
  11450. ' p: TPoints;',
  11451. 'begin',
  11452. ' SetLength(p,2);',
  11453. 'end;',
  11454. 'begin']);
  11455. ConvertProgram;
  11456. CheckSource('TestRecord_InFunction',
  11457. LinesToStr([ // statements
  11458. 'this.TPoint = 3;',
  11459. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11460. ' this.x = 0;',
  11461. ' this.y = 0;',
  11462. ' this.$eq = function (b) {',
  11463. ' return (this.x === b.x) && (this.y === b.y);',
  11464. ' };',
  11465. ' this.$assign = function (s) {',
  11466. ' this.x = s.x;',
  11467. ' this.y = s.y;',
  11468. ' return this;',
  11469. ' };',
  11470. '});',
  11471. 'this.DoIt = function () {',
  11472. ' var r = TPoint$1.$new();',
  11473. ' var p = [];',
  11474. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11475. '};',
  11476. '']),
  11477. LinesToStr([ // $mod.$main
  11478. '']));
  11479. end;
  11480. procedure TTestModule.TestRecord_AnonymousFail;
  11481. begin
  11482. StartProgram(false);
  11483. Add([
  11484. 'var',
  11485. ' r: record x: word end;',
  11486. 'begin']);
  11487. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11488. nNotYetImplemented);
  11489. ConvertProgram;
  11490. end;
  11491. procedure TTestModule.TestAdvRecord_Function;
  11492. begin
  11493. StartProgram(false);
  11494. Parser.Options:=Parser.Options+[po_cassignments];
  11495. Add([
  11496. '{$modeswitch AdvancedRecords}',
  11497. 'type',
  11498. ' TPoint = record',
  11499. ' x,y: word;',
  11500. ' function Add(const apt: TPoint): TPoint;',
  11501. ' end;',
  11502. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11503. 'begin',
  11504. ' Result:=Self;',
  11505. ' Result.x+=apt.x;',
  11506. ' Result.y:=Result.y+apt.y;',
  11507. ' Self:=apt;',
  11508. 'end;',
  11509. 'var p,q: TPoint;',
  11510. 'begin',
  11511. ' p.add(q);',
  11512. ' p:=default(TPoint);',
  11513. ' p:=q;',
  11514. '']);
  11515. ConvertProgram;
  11516. CheckSource('TestAdvRecord_Function',
  11517. LinesToStr([ // statements
  11518. 'rtl.recNewT(this, "TPoint", function () {',
  11519. ' this.x = 0;',
  11520. ' this.y = 0;',
  11521. ' this.$eq = function (b) {',
  11522. ' return (this.x === b.x) && (this.y === b.y);',
  11523. ' };',
  11524. ' this.$assign = function (s) {',
  11525. ' this.x = s.x;',
  11526. ' this.y = s.y;',
  11527. ' return this;',
  11528. ' };',
  11529. ' this.Add = function (apt) {',
  11530. ' var Result = $mod.TPoint.$new();',
  11531. ' Result.$assign(this);',
  11532. ' Result.x += apt.x;',
  11533. ' Result.y = Result.y + apt.y;',
  11534. ' this.$assign(apt);',
  11535. ' return Result;',
  11536. ' };',
  11537. '});',
  11538. 'this.p = this.TPoint.$new();',
  11539. 'this.q = this.TPoint.$new();',
  11540. '']),
  11541. LinesToStr([ // $mod.$main
  11542. '$mod.p.Add($mod.q);',
  11543. '$mod.p.$assign($mod.TPoint.$new());',
  11544. '$mod.p.$assign($mod.q);',
  11545. '']));
  11546. end;
  11547. procedure TTestModule.TestAdvRecord_Property;
  11548. begin
  11549. StartProgram(false);
  11550. Add([
  11551. '{$modeswitch AdvancedRecords}',
  11552. 'type',
  11553. ' TPoint = record',
  11554. ' x,y: word;',
  11555. ' strict private',
  11556. ' function GetSize: longword;',
  11557. ' procedure SetSize(Value: longword);',
  11558. ' public',
  11559. ' property Size: longword read GetSize write SetSize;',
  11560. ' property Left: word read x write y;',
  11561. ' end;',
  11562. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11563. 'function TPoint.GetSize: longword;',
  11564. 'begin',
  11565. ' x:=y;',
  11566. ' Size:=Size;',
  11567. ' Left:=Left;',
  11568. 'end;',
  11569. 'procedure TPoint.SetSize(Value: longword);',
  11570. 'begin',
  11571. 'end;',
  11572. 'var p,q: TPoint;',
  11573. 'begin',
  11574. ' p.Size:=q.Size;',
  11575. ' p.Left:=q.Left;',
  11576. '']);
  11577. ConvertProgram;
  11578. CheckSource('TestAdvRecord_Property',
  11579. LinesToStr([ // statements
  11580. 'rtl.recNewT(this, "TPoint", function () {',
  11581. ' this.x = 0;',
  11582. ' this.y = 0;',
  11583. ' this.$eq = function (b) {',
  11584. ' return (this.x === b.x) && (this.y === b.y);',
  11585. ' };',
  11586. ' this.$assign = function (s) {',
  11587. ' this.x = s.x;',
  11588. ' this.y = s.y;',
  11589. ' return this;',
  11590. ' };',
  11591. ' this.GetSize = function () {',
  11592. ' var Result = 0;',
  11593. ' this.x = this.y;',
  11594. ' this.SetSize(this.GetSize());',
  11595. ' this.y = this.x;',
  11596. ' return Result;',
  11597. ' };',
  11598. ' this.SetSize = function (Value) {',
  11599. ' };',
  11600. '});',
  11601. 'this.SetSize = function (Value) {',
  11602. '};',
  11603. 'this.p = this.TPoint.$new();',
  11604. 'this.q = this.TPoint.$new();',
  11605. '']),
  11606. LinesToStr([ // $mod.$main
  11607. '$mod.p.SetSize($mod.q.GetSize());',
  11608. '$mod.p.y = $mod.q.x;',
  11609. '']));
  11610. end;
  11611. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11612. begin
  11613. StartProgram(false);
  11614. Add([
  11615. '{$modeswitch AdvancedRecords}',
  11616. 'type',
  11617. ' TPoint = record',
  11618. ' strict private',
  11619. ' function GetItems(Index: word): word;',
  11620. ' procedure SetItems(Index: word; Value: word);',
  11621. ' public',
  11622. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11623. ' end;',
  11624. 'function TPoint.GetItems(Index: word): word;',
  11625. 'begin',
  11626. ' Items[index]:=Items[index];',
  11627. ' self.Items[index]:=self.Items[index];',
  11628. 'end;',
  11629. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11630. 'begin',
  11631. 'end;',
  11632. 'var p: TPoint;',
  11633. 'begin',
  11634. ' p[1]:=p[2];',
  11635. ' p.Items[3]:=p.Items[4];',
  11636. '']);
  11637. ConvertProgram;
  11638. CheckSource('TestAdvRecord_PropertyDefault',
  11639. LinesToStr([ // statements
  11640. 'rtl.recNewT(this, "TPoint", function () {',
  11641. ' this.$eq = function (b) {',
  11642. ' return true;',
  11643. ' };',
  11644. ' this.$assign = function (s) {',
  11645. ' return this;',
  11646. ' };',
  11647. ' this.GetItems = function (Index) {',
  11648. ' var Result = 0;',
  11649. ' this.SetItems(Index, this.GetItems(Index));',
  11650. ' this.SetItems(Index, this.GetItems(Index));',
  11651. ' return Result;',
  11652. ' };',
  11653. ' this.SetItems = function (Index, Value) {',
  11654. ' };',
  11655. '});',
  11656. 'this.p = this.TPoint.$new();',
  11657. '']),
  11658. LinesToStr([ // $mod.$main
  11659. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11660. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11661. '']));
  11662. end;
  11663. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11664. begin
  11665. StartProgram(false);
  11666. Add([
  11667. '{$modeswitch AdvancedRecords}',
  11668. 'type',
  11669. ' TRec = record',
  11670. ' class var',
  11671. ' Fx: longint;',
  11672. ' Fy: longint;',
  11673. ' class function GetInt: longint; static;',
  11674. ' class procedure SetInt(Value: longint); static;',
  11675. ' class procedure DoIt; static;',
  11676. ' class property IntA: longint read Fx write Fy;',
  11677. ' class property IntB: longint read GetInt write SetInt;',
  11678. ' end;',
  11679. 'class function trec.getint: longint;',
  11680. 'begin',
  11681. ' result:=fx;',
  11682. 'end;',
  11683. 'class procedure trec.setint(value: longint);',
  11684. 'begin',
  11685. 'end;',
  11686. 'class procedure trec.doit;',
  11687. 'begin',
  11688. ' IntA:=IntA+1;',
  11689. ' IntB:=IntB+1;',
  11690. 'end;',
  11691. 'var r: trec;',
  11692. 'begin',
  11693. ' trec.inta:=trec.inta+1;',
  11694. ' if trec.intb=2 then;',
  11695. ' trec.intb:=trec.intb+2;',
  11696. ' trec.setint(trec.inta);',
  11697. ' r.inta:=r.inta+1;',
  11698. ' if r.intb=2 then;',
  11699. ' r.intb:=r.intb+2;',
  11700. ' r.setint(r.inta);']);
  11701. ConvertProgram;
  11702. CheckSource('TestAdvRecord_Property_ClassMethod',
  11703. LinesToStr([ // statements
  11704. 'rtl.recNewT(this, "TRec", function () {',
  11705. ' this.Fx = 0;',
  11706. ' this.Fy = 0;',
  11707. ' this.$eq = function (b) {',
  11708. ' return true;',
  11709. ' };',
  11710. ' this.$assign = function (s) {',
  11711. ' return this;',
  11712. ' };',
  11713. ' this.GetInt = function () {',
  11714. ' var Result = 0;',
  11715. ' Result = $mod.TRec.Fx;',
  11716. ' return Result;',
  11717. ' };',
  11718. ' this.SetInt = function (Value) {',
  11719. ' };',
  11720. ' this.DoIt = function () {',
  11721. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11722. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11723. ' };',
  11724. '}, true);',
  11725. 'this.r = this.TRec.$new();',
  11726. '']),
  11727. LinesToStr([ // $mod.$main
  11728. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11729. 'if ($mod.TRec.GetInt() === 2) ;',
  11730. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11731. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11732. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11733. 'if ($mod.TRec.GetInt() === 2) ;',
  11734. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11735. '$mod.TRec.SetInt($mod.r.Fx);',
  11736. '']));
  11737. end;
  11738. procedure TTestModule.TestAdvRecord_Const;
  11739. begin
  11740. StartProgram(false);
  11741. Add([
  11742. '{$modeswitch AdvancedRecords}',
  11743. 'type',
  11744. ' TArrInt = array[3..4] of longint;',
  11745. ' TPoint = record',
  11746. ' x,y: longint;',
  11747. ' class var Count: nativeint;',
  11748. ' end;',
  11749. ' TRec = record',
  11750. ' i: longint;',
  11751. ' a: array of longint;',
  11752. ' s: array[1..2] of longint;',
  11753. ' m: array[1..2,3..4] of longint;',
  11754. ' p: TPoint;',
  11755. ' end;',
  11756. ' TPoints = array of TPoint;',
  11757. 'const',
  11758. ' r: TRec = (',
  11759. ' i:1;',
  11760. ' a:(2,3);',
  11761. ' s:(4,5);',
  11762. ' m:( (11,12), (13,14) );',
  11763. ' p: (x:21)',
  11764. ' );',
  11765. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11766. 'begin']);
  11767. ConvertProgram;
  11768. CheckSource('TestAdvRecord_Const',
  11769. LinesToStr([ // statements
  11770. 'rtl.recNewT(this, "TPoint", function () {',
  11771. ' this.x = 0;',
  11772. ' this.y = 0;',
  11773. ' this.Count = 0;',
  11774. ' this.$eq = function (b) {',
  11775. ' return (this.x === b.x) && (this.y === b.y);',
  11776. ' };',
  11777. ' this.$assign = function (s) {',
  11778. ' this.x = s.x;',
  11779. ' this.y = s.y;',
  11780. ' return this;',
  11781. ' };',
  11782. '}, true);',
  11783. 'rtl.recNewT(this, "TRec", function () {',
  11784. ' this.i = 0;',
  11785. ' this.$new = function () {',
  11786. ' var r = Object.create(this);',
  11787. ' r.a = [];',
  11788. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11789. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11790. ' r.p = $mod.TPoint.$new();',
  11791. ' return r;',
  11792. ' };',
  11793. ' this.$eq = function (b) {',
  11794. ' 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);',
  11795. ' };',
  11796. ' this.$assign = function (s) {',
  11797. ' this.i = s.i;',
  11798. ' this.a = rtl.arrayRef(s.a);',
  11799. ' this.s = s.s.slice(0);',
  11800. ' this.m = s.m.slice(0);',
  11801. ' this.p.$assign(s.p);',
  11802. ' return this;',
  11803. ' };',
  11804. '});',
  11805. 'this.r = this.TRec.$clone({',
  11806. ' i: 1,',
  11807. ' a: [2, 3],',
  11808. ' s: [4, 5],',
  11809. ' m: [[11, 12], [13, 14]],',
  11810. ' p: this.TPoint.$clone({',
  11811. ' x: 21,',
  11812. ' y: 0',
  11813. ' })',
  11814. '});',
  11815. 'this.p = [this.TPoint.$clone({',
  11816. ' x: 1,',
  11817. ' y: 2',
  11818. '}), this.TPoint.$clone({',
  11819. ' x: 3,',
  11820. ' y: 4',
  11821. '})];',
  11822. '']),
  11823. LinesToStr([ // $mod.$main
  11824. '']));
  11825. end;
  11826. procedure TTestModule.TestAdvRecord_ExternalField;
  11827. begin
  11828. StartProgram(false);
  11829. Add([
  11830. '{$modeswitch AdvancedRecords}',
  11831. '{$modeswitch externalclass}',
  11832. 'type',
  11833. ' TCar = record',
  11834. ' public',
  11835. ' Intern: longint external name ''$Intern'';',
  11836. ' Intern2: longint external name ''$Intern2'';',
  11837. ' Bracket: longint external name ''["A B"]'';',
  11838. ' procedure DoIt;',
  11839. ' end;',
  11840. 'procedure tcar.doit;',
  11841. 'begin',
  11842. ' Intern:=Intern+1;',
  11843. ' Intern2:=Intern2+2;',
  11844. ' Bracket:=Bracket+3;',
  11845. 'end;',
  11846. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11847. 'begin',
  11848. ' Rec.intern:=Rec.intern+1;',
  11849. ' Rec.intern2:=Rec.intern2+2;',
  11850. ' Rec.Bracket:=Rec.Bracket+3;',
  11851. ' with Rec do begin',
  11852. ' intern:=intern+1;',
  11853. ' intern2:=intern2+2;',
  11854. ' Bracket:=Bracket+3;',
  11855. ' end;']);
  11856. ConvertProgram;
  11857. CheckSource('TestAdvRecord_ExternalField',
  11858. LinesToStr([ // statements
  11859. 'rtl.recNewT(this, "TCar", function () {',
  11860. ' this.$eq = function (b) {',
  11861. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11862. ' };',
  11863. ' this.$assign = function (s) {',
  11864. ' this.$Intern = s.$Intern;',
  11865. ' this.$Intern2 = s.$Intern2;',
  11866. ' this["A B"] = s["A B"];',
  11867. ' return this;',
  11868. ' };',
  11869. ' this.DoIt = function () {',
  11870. ' this.$Intern = this.$Intern + 1;',
  11871. ' this.$Intern2 = this.$Intern2 + 2;',
  11872. ' this["A B"] = this["A B"] + 3;',
  11873. ' };',
  11874. '});',
  11875. 'this.Rec = this.TCar.$clone({',
  11876. ' $Intern: 11,',
  11877. ' $Intern2: 12,',
  11878. ' "A B": 13',
  11879. '});',
  11880. '']),
  11881. LinesToStr([ // $mod.$main
  11882. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11883. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11884. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11885. 'var $with = $mod.Rec;',
  11886. '$with.$Intern = $with.$Intern + 1;',
  11887. '$with.$Intern2 = $with.$Intern2 + 2;',
  11888. '$with["A B"] = $with["A B"] + 3;',
  11889. '']));
  11890. end;
  11891. procedure TTestModule.TestAdvRecord_SubRecord;
  11892. begin
  11893. StartProgram(false);
  11894. Add([
  11895. '{$modeswitch AdvancedRecords}',
  11896. 'type',
  11897. ' TRec = record',
  11898. ' type',
  11899. ' TPoint = record',
  11900. ' x,y: longint;',
  11901. ' class var Count: nativeint;',
  11902. ' procedure DoIt;',
  11903. ' class procedure DoThat; static;',
  11904. ' end;',
  11905. ' var',
  11906. ' i: longint;',
  11907. ' p: TPoint;',
  11908. ' procedure DoSome;',
  11909. ' end;',
  11910. 'const',
  11911. ' r: TRec = (',
  11912. ' i:1;',
  11913. ' p: (x:21;y:22)',
  11914. ' );',
  11915. 'procedure TRec.DoSome;',
  11916. 'begin',
  11917. ' p.x:=p.y+1;',
  11918. ' p.Count:=p.Count+2;',
  11919. 'end;',
  11920. 'procedure TRec.TPoint.DoIt;',
  11921. 'begin',
  11922. ' Count:=Count+3;',
  11923. 'end;',
  11924. 'class procedure TRec.TPoint.DoThat;',
  11925. 'begin',
  11926. ' Count:=Count+4;',
  11927. 'end;',
  11928. 'begin']);
  11929. ConvertProgram;
  11930. CheckSource('TestAdvRecord_SubRecord',
  11931. LinesToStr([ // statements
  11932. 'rtl.recNewT(this, "TRec", function () {',
  11933. ' rtl.recNewT(this, "TPoint", function () {',
  11934. ' this.x = 0;',
  11935. ' this.y = 0;',
  11936. ' this.Count = 0;',
  11937. ' this.$eq = function (b) {',
  11938. ' return (this.x === b.x) && (this.y === b.y);',
  11939. ' };',
  11940. ' this.$assign = function (s) {',
  11941. ' this.x = s.x;',
  11942. ' this.y = s.y;',
  11943. ' return this;',
  11944. ' };',
  11945. ' this.DoIt = function () {',
  11946. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11947. ' };',
  11948. ' this.DoThat = function () {',
  11949. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11950. ' };',
  11951. ' }, true);',
  11952. ' this.i = 0;',
  11953. ' this.$new = function () {',
  11954. ' var r = Object.create(this);',
  11955. ' r.p = this.TPoint.$new();',
  11956. ' return r;',
  11957. ' };',
  11958. ' this.$eq = function (b) {',
  11959. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11960. ' };',
  11961. ' this.$assign = function (s) {',
  11962. ' this.i = s.i;',
  11963. ' this.p.$assign(s.p);',
  11964. ' return this;',
  11965. ' };',
  11966. ' this.DoSome = function () {',
  11967. ' this.p.x = this.p.y + 1;',
  11968. ' this.TPoint.Count = this.p.Count + 2;',
  11969. ' };',
  11970. '}, true);',
  11971. 'this.r = this.TRec.$clone({',
  11972. ' i: 1,',
  11973. ' p: this.TRec.TPoint.$clone({',
  11974. ' x: 21,',
  11975. ' y: 22',
  11976. ' })',
  11977. '});',
  11978. '']),
  11979. LinesToStr([ // $mod.$main
  11980. '']));
  11981. end;
  11982. procedure TTestModule.TestAdvRecord_SubClass;
  11983. begin
  11984. StartProgram(false);
  11985. Add([
  11986. '{$modeswitch AdvancedRecords}',
  11987. 'type',
  11988. ' TObject = class end;',
  11989. ' TPoint = record',
  11990. ' type',
  11991. ' TBird = class',
  11992. ' procedure DoIt;',
  11993. ' class procedure Glob;',
  11994. ' end;',
  11995. ' procedure DoIt(b: TBird);',
  11996. ' end;',
  11997. 'procedure TPoint.TBird.DoIt;',
  11998. 'begin',
  11999. ' doit;',
  12000. ' self.doit;',
  12001. ' glob;',
  12002. ' self.glob;',
  12003. 'end;',
  12004. 'class procedure TPoint.TBird.Glob;',
  12005. 'begin',
  12006. ' glob;',
  12007. ' self.glob;',
  12008. 'end;',
  12009. 'procedure TPoint.DoIt(b: TBird);',
  12010. 'begin',
  12011. ' b.doit;',
  12012. ' b.glob;',
  12013. ' TBird.glob;',
  12014. 'end;',
  12015. 'begin',
  12016. '']);
  12017. ConvertProgram;
  12018. CheckSource('TestAdvRecord_SubClass',
  12019. LinesToStr([ // statements
  12020. 'rtl.createClass(this, "TObject", null, function () {',
  12021. ' this.$init = function () {',
  12022. ' };',
  12023. ' this.$final = function () {',
  12024. ' };',
  12025. '});',
  12026. 'rtl.recNewT(this, "TPoint", function () {',
  12027. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12028. ' this.DoIt = function () {',
  12029. ' this.DoIt();',
  12030. ' this.DoIt();',
  12031. ' this.$class.Glob();',
  12032. ' this.$class.Glob();',
  12033. ' };',
  12034. ' this.Glob = function () {',
  12035. ' this.Glob();',
  12036. ' this.Glob();',
  12037. ' };',
  12038. ' }, "TPoint.TBird");',
  12039. ' this.$eq = function (b) {',
  12040. ' return true;',
  12041. ' };',
  12042. ' this.$assign = function (s) {',
  12043. ' return this;',
  12044. ' };',
  12045. ' this.DoIt = function (b) {',
  12046. ' b.DoIt();',
  12047. ' b.$class.Glob();',
  12048. ' this.TBird.Glob();',
  12049. ' };',
  12050. '}, true);',
  12051. '']),
  12052. LinesToStr([ // $mod.$main
  12053. '']));
  12054. end;
  12055. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12056. begin
  12057. StartProgram(false);
  12058. Add([
  12059. '{$modeswitch AdvancedRecords}',
  12060. 'type',
  12061. ' IUnknown = interface end;',
  12062. ' TPoint = record',
  12063. ' type IBird = interface end;',
  12064. ' end;',
  12065. 'begin',
  12066. '']);
  12067. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12068. nNotYetImplemented);
  12069. ParseProgram;
  12070. end;
  12071. procedure TTestModule.TestAdvRecord_Constructor;
  12072. begin
  12073. StartProgram(false);
  12074. Add([
  12075. '{$modeswitch AdvancedRecords}',
  12076. 'type',
  12077. ' TPoint = record',
  12078. ' x,y: longint;',
  12079. ' class procedure Run(w: longint = 13); static;',
  12080. ' constructor Create(ax: longint; ay: longint = -1);',
  12081. ' end;',
  12082. 'class procedure tpoint.run(w: longint);',
  12083. 'begin',
  12084. ' run;',
  12085. ' run();',
  12086. 'end;',
  12087. 'constructor tpoint.create(ax,ay: longint);',
  12088. 'begin',
  12089. ' x:=ax;',
  12090. ' self.y:=ay;',
  12091. ' run;',
  12092. ' run(ax);',
  12093. 'end;',
  12094. 'var r: TPoint;',
  12095. 'begin',
  12096. ' r:=TPoint.Create(1,2);',
  12097. ' with TPoint do r:=Create(1,2);',
  12098. ' r.Create(3);',
  12099. ' r:=r.Create(4);',
  12100. '']);
  12101. ConvertProgram;
  12102. CheckSource('TestAdvRecord_Constructor',
  12103. LinesToStr([ // statements
  12104. 'rtl.recNewT(this, "TPoint", function () {',
  12105. ' this.x = 0;',
  12106. ' this.y = 0;',
  12107. ' this.$eq = function (b) {',
  12108. ' return (this.x === b.x) && (this.y === b.y);',
  12109. ' };',
  12110. ' this.$assign = function (s) {',
  12111. ' this.x = s.x;',
  12112. ' this.y = s.y;',
  12113. ' return this;',
  12114. ' };',
  12115. ' this.Run = function (w) {',
  12116. ' $mod.TPoint.Run(13);',
  12117. ' $mod.TPoint.Run(13);',
  12118. ' };',
  12119. ' this.Create = function (ax, ay) {',
  12120. ' this.x = ax;',
  12121. ' this.y = ay;',
  12122. ' this.Run(13);',
  12123. ' this.Run(ax);',
  12124. ' return this;',
  12125. ' };',
  12126. '});',
  12127. 'this.r = this.TPoint.$new();',
  12128. '']),
  12129. LinesToStr([ // $mod.$main
  12130. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12131. 'var $with = $mod.TPoint;',
  12132. '$mod.r.$assign($with.$new().Create(1, 2));',
  12133. '$mod.r.Create(3, -1);',
  12134. '$mod.r.$assign($mod.r.Create(4, -1));',
  12135. '']));
  12136. end;
  12137. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12138. begin
  12139. StartProgram(false);
  12140. Add([
  12141. '{$modeswitch AdvancedRecords}',
  12142. 'type',
  12143. ' TPoint = record',
  12144. ' class var x: longint;',
  12145. ' class procedure Fly; static;',
  12146. ' class constructor Init;',
  12147. ' end;',
  12148. 'var count: word;',
  12149. 'class procedure Tpoint.Fly;',
  12150. 'begin',
  12151. 'end;',
  12152. 'class constructor tpoint.init;',
  12153. 'begin',
  12154. ' count:=count+1;',
  12155. ' x:=x+3;',
  12156. ' tpoint.x:=tpoint.x+4;',
  12157. ' fly;',
  12158. ' tpoint.fly;',
  12159. 'end;',
  12160. 'var r: TPoint;',
  12161. 'begin',
  12162. ' r.x:=r.x+10;',
  12163. ' r.Fly;',
  12164. ' r.Fly();',
  12165. '']);
  12166. ConvertProgram;
  12167. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12168. LinesToStr([ // statements
  12169. 'rtl.recNewT(this, "TPoint", function () {',
  12170. ' this.x = 0;',
  12171. ' this.$eq = function (b) {',
  12172. ' return true;',
  12173. ' };',
  12174. ' this.$assign = function (s) {',
  12175. ' return this;',
  12176. ' };',
  12177. ' this.Fly = function () {',
  12178. ' };',
  12179. '}, true);',
  12180. 'this.count = 0;',
  12181. 'this.r = this.TPoint.$new();',
  12182. '']),
  12183. LinesToStr([ // $mod.$main
  12184. '(function () {',
  12185. ' $mod.count = $mod.count + 1;',
  12186. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12187. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12188. ' $mod.TPoint.Fly();',
  12189. ' $mod.TPoint.Fly();',
  12190. '})();',
  12191. '$mod.TPoint.x = $mod.r.x + 10;',
  12192. '$mod.TPoint.Fly();',
  12193. '$mod.TPoint.Fly();',
  12194. '']));
  12195. end;
  12196. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12197. begin
  12198. StartUnit(false);
  12199. Add([
  12200. 'interface',
  12201. '{$modeswitch AdvancedRecords}',
  12202. 'type',
  12203. ' TPoint = record',
  12204. ' class var x: longint;',
  12205. ' class procedure Fly; static;',
  12206. ' class constructor Init;',
  12207. ' end;',
  12208. 'implementation',
  12209. 'var count: word;',
  12210. 'class procedure Tpoint.Fly;',
  12211. 'begin',
  12212. 'end;',
  12213. 'class constructor tpoint.init;',
  12214. 'begin',
  12215. ' count:=count+1;',
  12216. ' x:=3;',
  12217. ' tpoint.x:=4;',
  12218. ' fly;',
  12219. ' tpoint.fly;',
  12220. 'end;',
  12221. '']);
  12222. ConvertUnit;
  12223. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12224. LinesToStr([ // statements
  12225. 'var $impl = $mod.$impl;',
  12226. 'rtl.recNewT(this, "TPoint", function () {',
  12227. ' this.x = 0;',
  12228. ' this.$eq = function (b) {',
  12229. ' return true;',
  12230. ' };',
  12231. ' this.$assign = function (s) {',
  12232. ' return this;',
  12233. ' };',
  12234. ' this.Fly = function () {',
  12235. ' };',
  12236. '}, true);',
  12237. '']),
  12238. LinesToStr([ // $mod.$init
  12239. '(function () {',
  12240. ' $impl.count = $impl.count + 1;',
  12241. ' $mod.TPoint.x = 3;',
  12242. ' $mod.TPoint.x = 4;',
  12243. ' $mod.TPoint.Fly();',
  12244. ' $mod.TPoint.Fly();',
  12245. '})();',
  12246. '']),
  12247. LinesToStr([ // $mod.$main
  12248. '$impl.count = 0;',
  12249. '']));
  12250. end;
  12251. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12252. begin
  12253. StartProgram(false);
  12254. Add(['type',
  12255. ' TObject = class',
  12256. ' public',
  12257. ' constructor Create;',
  12258. ' destructor Destroy;',
  12259. ' end;',
  12260. ' TBird = TObject;',
  12261. 'constructor tobject.create;',
  12262. 'begin end;',
  12263. 'destructor tobject.destroy;',
  12264. 'begin end;',
  12265. 'var Obj: tobject;',
  12266. 'begin',
  12267. ' obj:=tobject.create;',
  12268. ' obj:=tobject.create();',
  12269. ' obj:=tbird.create;',
  12270. ' obj:=tbird.create();',
  12271. ' obj:=obj.create();',
  12272. ' obj.destroy;',
  12273. '']);
  12274. ConvertProgram;
  12275. CheckSource('TestClass_TObjectDefaultConstructor',
  12276. LinesToStr([ // statements
  12277. 'rtl.createClass(this,"TObject",null,function(){',
  12278. ' this.$init = function () {',
  12279. ' };',
  12280. ' this.$final = function () {',
  12281. ' };',
  12282. ' this.Create = function(){',
  12283. ' return this;',
  12284. ' };',
  12285. ' this.Destroy = function(){',
  12286. ' };',
  12287. '});',
  12288. 'this.Obj = null;'
  12289. ]),
  12290. LinesToStr([ // $mod.$main
  12291. '$mod.Obj = $mod.TObject.$create("Create");',
  12292. '$mod.Obj = $mod.TObject.$create("Create");',
  12293. '$mod.Obj = $mod.TObject.$create("Create");',
  12294. '$mod.Obj = $mod.TObject.$create("Create");',
  12295. '$mod.Obj = $mod.Obj.Create();',
  12296. '$mod.Obj.$destroy("Destroy");',
  12297. '']));
  12298. end;
  12299. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12300. begin
  12301. StartProgram(false);
  12302. Add('type');
  12303. Add(' TObject = class');
  12304. Add(' public');
  12305. Add(' constructor Create(Par: longint);');
  12306. Add(' end;');
  12307. Add('constructor tobject.create(par: longint);');
  12308. Add('begin end;');
  12309. Add('var Obj: tobject;');
  12310. Add('begin');
  12311. Add(' obj:=tobject.create(3);');
  12312. ConvertProgram;
  12313. CheckSource('TestClass_TObjectConstructorWithParams',
  12314. LinesToStr([ // statements
  12315. 'rtl.createClass(this,"TObject",null,function(){',
  12316. ' this.$init = function () {',
  12317. ' };',
  12318. ' this.$final = function () {',
  12319. ' };',
  12320. ' this.Create = function(Par){',
  12321. ' return this;',
  12322. ' };',
  12323. '});',
  12324. 'this.Obj = null;'
  12325. ]),
  12326. LinesToStr([ // $mod.$main
  12327. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12328. ]));
  12329. end;
  12330. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12331. begin
  12332. StartProgram(false);
  12333. Add('type');
  12334. Add(' TObject = class');
  12335. Add(' public');
  12336. Add(' constructor Create;');
  12337. Add(' end;');
  12338. Add(' TTest = class(TObject)');
  12339. Add(' public');
  12340. Add(' constructor Create(const Par: longint = 1);');
  12341. Add(' end;');
  12342. Add('constructor tobject.create;');
  12343. Add('begin end;');
  12344. Add('constructor ttest.create(const par: longint);');
  12345. Add('begin end;');
  12346. Add('var t: ttest;');
  12347. Add('begin');
  12348. Add(' t:=ttest.create;');
  12349. Add(' t:=ttest.create(2);');
  12350. ConvertProgram;
  12351. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12352. LinesToStr([ // statements
  12353. 'rtl.createClass(this,"TObject",null,function(){',
  12354. ' this.$init = function () {',
  12355. ' };',
  12356. ' this.$final = function () {',
  12357. ' };',
  12358. ' this.Create = function(){',
  12359. ' return this;',
  12360. ' };',
  12361. '});',
  12362. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12363. ' this.Create$1 = function (Par) {',
  12364. ' return this;',
  12365. ' };',
  12366. '});',
  12367. 'this.t = null;'
  12368. ]),
  12369. LinesToStr([ // $mod.$main
  12370. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12371. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12372. ]));
  12373. end;
  12374. procedure TTestModule.TestClass_Var;
  12375. begin
  12376. StartProgram(false);
  12377. Add([
  12378. 'type',
  12379. ' TObject = class',
  12380. ' public',
  12381. ' vI: longint;',
  12382. ' constructor Create(Par: longint);',
  12383. ' end;',
  12384. 'constructor tobject.create(par: longint);',
  12385. 'begin',
  12386. ' vi:=par+3',
  12387. 'end;',
  12388. 'var Obj: tobject;',
  12389. 'begin',
  12390. ' obj:=tobject.create(4);',
  12391. ' obj.vi:=obj.VI+5;']);
  12392. ConvertProgram;
  12393. CheckSource('TestClass_Var',
  12394. LinesToStr([ // statements
  12395. 'rtl.createClass(this,"TObject",null,function(){',
  12396. ' this.$init = function () {',
  12397. ' this.vI = 0;',
  12398. ' };',
  12399. ' this.$final = function () {',
  12400. ' };',
  12401. ' this.Create = function(Par){',
  12402. ' this.vI = Par+3;',
  12403. ' return this;',
  12404. ' };',
  12405. '});',
  12406. 'this.Obj = null;'
  12407. ]),
  12408. LinesToStr([ // $mod.$main
  12409. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12410. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12411. ]));
  12412. end;
  12413. procedure TTestModule.TestClass_Method;
  12414. begin
  12415. StartProgram(false);
  12416. Add('type');
  12417. Add(' TObject = class');
  12418. Add(' public');
  12419. Add(' vI: longint;');
  12420. Add(' Sub: TObject;');
  12421. Add(' constructor Create;');
  12422. Add(' function GetIt(Par: longint): tobject;');
  12423. Add(' end;');
  12424. Add('constructor tobject.create; begin end;');
  12425. Add('function tobject.getit(par: longint): tobject;');
  12426. Add('begin');
  12427. Add(' Self.vi:=par+3;');
  12428. Add(' Result:=self.sub;');
  12429. Add('end;');
  12430. Add('var Obj: tobject;');
  12431. Add('begin');
  12432. Add(' obj:=tobject.create;');
  12433. Add(' obj.getit(4);');
  12434. Add(' obj.sub.sub:=nil;');
  12435. Add(' obj.sub.getit(5);');
  12436. Add(' obj.sub.getit(6).SUB:=nil;');
  12437. Add(' obj.sub.getit(7).GETIT(8);');
  12438. Add(' obj.sub.getit(9).SuB.getit(10);');
  12439. ConvertProgram;
  12440. CheckSource('TestClass_Method',
  12441. LinesToStr([ // statements
  12442. 'rtl.createClass(this,"TObject",null,function(){',
  12443. ' this.$init = function () {',
  12444. ' this.vI = 0;',
  12445. ' this.Sub = null;',
  12446. ' };',
  12447. ' this.$final = function () {',
  12448. ' this.Sub = undefined;',
  12449. ' };',
  12450. ' this.Create = function(){',
  12451. ' return this;',
  12452. ' };',
  12453. ' this.GetIt = function(Par){',
  12454. ' var Result = null;',
  12455. ' this.vI = Par + 3;',
  12456. ' Result = this.Sub;',
  12457. ' return Result;',
  12458. ' };',
  12459. '});',
  12460. 'this.Obj = null;'
  12461. ]),
  12462. LinesToStr([ // $mod.$main
  12463. '$mod.Obj = $mod.TObject.$create("Create");',
  12464. '$mod.Obj.GetIt(4);',
  12465. '$mod.Obj.Sub.Sub=null;',
  12466. '$mod.Obj.Sub.GetIt(5);',
  12467. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12468. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12469. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12470. ]));
  12471. end;
  12472. procedure TTestModule.TestClass_Implementation;
  12473. begin
  12474. StartUnit(false);
  12475. Add([
  12476. 'interface',
  12477. 'type',
  12478. ' TObject = class',
  12479. ' constructor Create;',
  12480. ' end;',
  12481. 'implementation',
  12482. 'type',
  12483. ' TIntClass = class',
  12484. ' constructor Create; reintroduce;',
  12485. ' class procedure DoGlob;',
  12486. ' end;',
  12487. 'constructor tintclass.create;',
  12488. 'begin',
  12489. ' inherited;',
  12490. ' inherited create;',
  12491. ' doglob;',
  12492. 'end;',
  12493. 'class procedure tintclass.doglob;',
  12494. 'begin',
  12495. 'end;',
  12496. 'constructor tobject.create;',
  12497. 'var',
  12498. ' iC: tintclass;',
  12499. 'begin',
  12500. ' ic:=tintclass.create;',
  12501. ' tintclass.doglob;',
  12502. ' ic.doglob;',
  12503. 'end;',
  12504. 'initialization',
  12505. ' tintclass.doglob;',
  12506. '']);
  12507. ConvertUnit;
  12508. CheckSource('TestClass_Implementation',
  12509. LinesToStr([ // statements
  12510. 'var $impl = $mod.$impl;',
  12511. 'rtl.createClass(this, "TObject", null, function () {',
  12512. ' this.$init = function () {',
  12513. ' };',
  12514. ' this.$final = function () {',
  12515. ' };',
  12516. ' this.Create = function () {',
  12517. ' var iC = null;',
  12518. ' iC = $impl.TIntClass.$create("Create$1");',
  12519. ' $impl.TIntClass.DoGlob();',
  12520. ' iC.$class.DoGlob();',
  12521. ' return this;',
  12522. ' };',
  12523. '});',
  12524. '']),
  12525. LinesToStr([ // $mod.$main
  12526. '$impl.TIntClass.DoGlob();',
  12527. '']),
  12528. LinesToStr([
  12529. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12530. ' this.Create$1 = function () {',
  12531. ' $mod.TObject.Create.call(this);',
  12532. ' $mod.TObject.Create.call(this);',
  12533. ' this.$class.DoGlob();',
  12534. ' return this;',
  12535. ' };',
  12536. ' this.DoGlob = function () {',
  12537. ' };',
  12538. '});',
  12539. '']));
  12540. end;
  12541. procedure TTestModule.TestClass_Inheritance;
  12542. begin
  12543. StartProgram(false);
  12544. Add('type');
  12545. Add(' TObject = class');
  12546. Add(' public');
  12547. Add(' constructor Create;');
  12548. Add(' end;');
  12549. Add(' TClassA = class');
  12550. Add(' end;');
  12551. Add(' TClassB = class(TObject)');
  12552. Add(' procedure ProcB;');
  12553. Add(' end;');
  12554. Add('constructor tobject.create; begin end;');
  12555. Add('procedure tclassb.procb; begin end;');
  12556. Add('var');
  12557. Add(' oO: TObject;');
  12558. Add(' oA: TClassA;');
  12559. Add(' oB: TClassB;');
  12560. Add('begin');
  12561. Add(' oO:=tobject.Create;');
  12562. Add(' oA:=tclassa.Create;');
  12563. Add(' ob:=tclassb.Create;');
  12564. Add(' if oo is tclassa then ;');
  12565. Add(' ob:=oo as tclassb;');
  12566. Add(' (oo as tclassb).procb;');
  12567. ConvertProgram;
  12568. CheckSource('TestClass_Inheritance',
  12569. LinesToStr([ // statements
  12570. 'rtl.createClass(this,"TObject",null,function(){',
  12571. ' this.$init = function () {',
  12572. ' };',
  12573. ' this.$final = function () {',
  12574. ' };',
  12575. ' this.Create = function () {',
  12576. ' return this;',
  12577. ' };',
  12578. '});',
  12579. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12580. '});',
  12581. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12582. ' this.ProcB = function () {',
  12583. ' };',
  12584. '});',
  12585. 'this.oO = null;',
  12586. 'this.oA = null;',
  12587. 'this.oB = null;'
  12588. ]),
  12589. LinesToStr([ // $mod.$main
  12590. '$mod.oO = $mod.TObject.$create("Create");',
  12591. '$mod.oA = $mod.TClassA.$create("Create");',
  12592. '$mod.oB = $mod.TClassB.$create("Create");',
  12593. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12594. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12595. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12596. ]));
  12597. end;
  12598. procedure TTestModule.TestClass_TypeAlias;
  12599. begin
  12600. StartProgram(false);
  12601. Add([
  12602. '{$interfaces corba}',
  12603. 'type',
  12604. ' IObject = interface',
  12605. ' end;',
  12606. ' IBird = type IObject;',
  12607. ' TObject = class',
  12608. ' end;',
  12609. ' TBird = type TObject;',
  12610. 'var',
  12611. ' oObj: TObject;',
  12612. ' oBird: TBird;',
  12613. ' IntfObj: IObject;',
  12614. ' IntfBird: IBird;',
  12615. 'begin',
  12616. ' oObj:=oBird;',
  12617. '']);
  12618. ConvertProgram;
  12619. CheckSource('TestClass_TypeAlias',
  12620. LinesToStr([ // statements
  12621. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12622. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12623. 'rtl.createClass(this, "TObject", null, function () {',
  12624. ' this.$init = function () {',
  12625. ' };',
  12626. ' this.$final = function () {',
  12627. ' };',
  12628. '});',
  12629. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12630. '});',
  12631. 'this.oObj = null;',
  12632. 'this.oBird = null;',
  12633. 'this.IntfObj = null;',
  12634. 'this.IntfBird = null;',
  12635. '']),
  12636. LinesToStr([ // $mod.$main
  12637. '$mod.oObj = $mod.oBird;',
  12638. '']));
  12639. end;
  12640. procedure TTestModule.TestClass_AbstractMethod;
  12641. begin
  12642. StartProgram(false);
  12643. Add('type');
  12644. Add(' TObject = class');
  12645. Add(' public');
  12646. Add(' procedure DoIt; virtual; abstract;');
  12647. Add(' end;');
  12648. Add('begin');
  12649. ConvertProgram;
  12650. CheckSource('TestClass_AbstractMethod',
  12651. LinesToStr([ // statements
  12652. 'rtl.createClass(this,"TObject",null,function(){',
  12653. ' this.$init = function () {',
  12654. ' };',
  12655. ' this.$final = function () {',
  12656. ' };',
  12657. '});'
  12658. ]),
  12659. LinesToStr([ // this.$main
  12660. ''
  12661. ]));
  12662. end;
  12663. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12664. begin
  12665. StartProgram(false);
  12666. Add([
  12667. 'type',
  12668. ' TObject = class',
  12669. ' procedure DoAbstract; virtual; abstract;',
  12670. ' procedure DoVirtual; virtual;',
  12671. ' procedure DoIt;',
  12672. ' end;',
  12673. ' TA = class',
  12674. ' procedure doabstract; override;',
  12675. ' procedure dovirtual; override;',
  12676. ' procedure DoSome;',
  12677. ' end;',
  12678. 'procedure tobject.dovirtual;',
  12679. 'begin',
  12680. ' inherited; // call non existing ancestor -> ignore silently',
  12681. 'end;',
  12682. 'procedure tobject.doit;',
  12683. 'begin',
  12684. 'end;',
  12685. 'procedure ta.doabstract;',
  12686. 'begin',
  12687. ' inherited dovirtual; // call TObject.DoVirtual',
  12688. 'end;',
  12689. 'procedure ta.dovirtual;',
  12690. 'begin',
  12691. ' inherited; // call TObject.DoVirtual',
  12692. ' inherited dovirtual; // call TObject.DoVirtual',
  12693. ' inherited dovirtual(); // call TObject.DoVirtual',
  12694. ' doit;',
  12695. ' doit();',
  12696. 'end;',
  12697. 'procedure ta.dosome;',
  12698. 'begin',
  12699. ' inherited; // call non existing ancestor method -> silently ignore',
  12700. 'end;',
  12701. 'begin']);
  12702. ConvertProgram;
  12703. CheckSource('TestClass_CallInherited_ProcNoParams',
  12704. LinesToStr([ // statements
  12705. 'rtl.createClass(this,"TObject",null,function(){',
  12706. ' this.$init = function () {',
  12707. ' };',
  12708. ' this.$final = function () {',
  12709. ' };',
  12710. ' this.DoVirtual = function () {',
  12711. ' };',
  12712. ' this.DoIt = function () {',
  12713. ' };',
  12714. '});',
  12715. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12716. ' this.DoAbstract = function () {',
  12717. ' $mod.TObject.DoVirtual.call(this);',
  12718. ' };',
  12719. ' this.DoVirtual = function () {',
  12720. ' $mod.TObject.DoVirtual.call(this);',
  12721. ' $mod.TObject.DoVirtual.call(this);',
  12722. ' $mod.TObject.DoVirtual.call(this);',
  12723. ' this.DoIt();',
  12724. ' this.DoIt();',
  12725. ' };',
  12726. ' this.DoSome = function () {',
  12727. ' };',
  12728. '});'
  12729. ]),
  12730. LinesToStr([ // this.$main
  12731. ''
  12732. ]));
  12733. end;
  12734. procedure TTestModule.TestClass_CallInherited_WithParams;
  12735. begin
  12736. StartProgram(false);
  12737. Add([
  12738. 'type',
  12739. ' TObject = class',
  12740. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12741. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12742. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12743. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12744. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12745. ' end;',
  12746. ' TClassA = class',
  12747. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12748. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12749. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12750. ' end;',
  12751. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12752. 'begin',
  12753. 'end;',
  12754. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12755. 'begin',
  12756. 'end;',
  12757. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12758. 'begin',
  12759. 'end;',
  12760. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12761. 'begin',
  12762. 'end;',
  12763. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12764. 'begin',
  12765. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12766. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12767. 'end;',
  12768. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12769. 'begin',
  12770. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12771. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12772. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12773. ' doit(pa,pb);',
  12774. ' doit(pa);',
  12775. ' doit2(pa);',
  12776. ' doit2;',
  12777. 'end;',
  12778. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12779. 'begin',
  12780. ' pa:=inherited;',
  12781. 'end;',
  12782. 'begin']);
  12783. ConvertProgram;
  12784. CheckSource('TestClass_CallInherited_WithParams',
  12785. LinesToStr([ // statements
  12786. 'rtl.createClass(this,"TObject",null,function(){',
  12787. ' this.$init = function () {',
  12788. ' };',
  12789. ' this.$final = function () {',
  12790. ' };',
  12791. ' this.DoVirtual = function (pA,pB) {',
  12792. ' };',
  12793. ' this.DoIt = function (pA,pB) {',
  12794. ' };',
  12795. ' this.DoIt2 = function (pA,pB) {',
  12796. ' };',
  12797. ' this.GetIt = function (pA, pB) {',
  12798. ' var Result = 0;',
  12799. ' return Result;',
  12800. ' };',
  12801. '});',
  12802. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12803. ' this.DoAbstract = function (pA,pB) {',
  12804. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12805. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12806. ' };',
  12807. ' this.DoVirtual = function (pA,pB) {',
  12808. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12809. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12810. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12811. ' this.DoIt(pA,pB);',
  12812. ' this.DoIt(pA,0);',
  12813. ' this.DoIt2(pA,2);',
  12814. ' this.DoIt2(1,2);',
  12815. ' };',
  12816. ' this.GetIt$1 = function (pA, pB) {',
  12817. ' var Result = 0;',
  12818. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12819. ' return Result;',
  12820. ' };',
  12821. '});'
  12822. ]),
  12823. LinesToStr([ // this.$main
  12824. ''
  12825. ]));
  12826. end;
  12827. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12828. begin
  12829. StartProgram(false);
  12830. Add('type');
  12831. Add(' TObject = class');
  12832. Add(' constructor Create; virtual;');
  12833. Add(' constructor CreateWithB(b: boolean);');
  12834. Add(' end;');
  12835. Add(' TA = class');
  12836. Add(' constructor Create; override;');
  12837. Add(' constructor CreateWithC(c: char);');
  12838. Add(' procedure DoIt;');
  12839. Add(' class function DoSome: TObject;');
  12840. Add(' end;');
  12841. Add('constructor tobject.create;');
  12842. Add('begin');
  12843. Add(' inherited; // call non existing ancestor -> ignore silently');
  12844. Add('end;');
  12845. Add('constructor tobject.createwithb(b: boolean);');
  12846. Add('begin');
  12847. Add(' inherited; // call non existing ancestor -> ignore silently');
  12848. Add(' create; // normal call');
  12849. Add('end;');
  12850. Add('constructor ta.create;');
  12851. Add('begin');
  12852. Add(' inherited; // normal call TObject.Create');
  12853. Add(' inherited create; // normal call TObject.Create');
  12854. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12855. Add('end;');
  12856. Add('constructor ta.createwithc(c: char);');
  12857. Add('begin');
  12858. Add(' inherited create; // call TObject.Create');
  12859. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12860. Add(' doit;');
  12861. Add(' doit();');
  12862. Add(' dosome;');
  12863. Add('end;');
  12864. Add('procedure ta.doit;');
  12865. Add('begin');
  12866. Add(' create; // normal call');
  12867. Add(' createwithb(false); // normal call');
  12868. Add(' createwithc(''c''); // normal call');
  12869. Add('end;');
  12870. Add('class function ta.dosome: TObject;');
  12871. Add('begin');
  12872. Add(' Result:=create; // constructor');
  12873. Add(' Result:=createwithb(true); // constructor');
  12874. Add(' Result:=createwithc(''c''); // constructor');
  12875. Add('end;');
  12876. Add('begin');
  12877. ConvertProgram;
  12878. CheckSource('TestClass_CallInheritedConstructor',
  12879. LinesToStr([ // statements
  12880. 'rtl.createClass(this,"TObject",null,function(){',
  12881. ' this.$init = function () {',
  12882. ' };',
  12883. ' this.$final = function () {',
  12884. ' };',
  12885. ' this.Create = function () {',
  12886. ' return this;',
  12887. ' };',
  12888. ' this.CreateWithB = function (b) {',
  12889. ' this.Create();',
  12890. ' return this;',
  12891. ' };',
  12892. '});',
  12893. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12894. ' this.Create = function () {',
  12895. ' $mod.TObject.Create.call(this);',
  12896. ' $mod.TObject.Create.call(this);',
  12897. ' $mod.TObject.CreateWithB.call(this, false);',
  12898. ' return this;',
  12899. ' };',
  12900. ' this.CreateWithC = function (c) {',
  12901. ' $mod.TObject.Create.call(this);',
  12902. ' $mod.TObject.CreateWithB.call(this, true);',
  12903. ' this.DoIt();',
  12904. ' this.DoIt();',
  12905. ' this.$class.DoSome();',
  12906. ' return this;',
  12907. ' };',
  12908. ' this.DoIt = function () {',
  12909. ' this.Create();',
  12910. ' this.CreateWithB(false);',
  12911. ' this.CreateWithC("c");',
  12912. ' };',
  12913. ' this.DoSome = function () {',
  12914. ' var Result = null;',
  12915. ' Result = this.$create("Create");',
  12916. ' Result = this.$create("CreateWithB", [true]);',
  12917. ' Result = this.$create("CreateWithC", ["c"]);',
  12918. ' return Result;',
  12919. ' };',
  12920. '});'
  12921. ]),
  12922. LinesToStr([ // this.$main
  12923. ''
  12924. ]));
  12925. end;
  12926. procedure TTestModule.TestClass_ClassVar_Assign;
  12927. begin
  12928. StartProgram(false);
  12929. Add([
  12930. 'type',
  12931. ' TObject = class',
  12932. ' public',
  12933. ' class var vI: longint;',
  12934. ' class var Sub: TObject;',
  12935. ' constructor Create;',
  12936. ' class function GetIt(var Par: longint): tobject;',
  12937. ' end;',
  12938. 'constructor tobject.create;',
  12939. 'begin',
  12940. ' vi:=vi+1;',
  12941. ' Self.vi:=Self.vi+1;',
  12942. ' inc(vi);',
  12943. 'end;',
  12944. 'class function tobject.getit(var par: longint): tobject;',
  12945. 'begin',
  12946. ' vi:=vi+3;',
  12947. ' Self.vi:=Self.vi+4;',
  12948. ' inc(vi);',
  12949. ' Result:=self.sub;',
  12950. ' GetIt(vi);',
  12951. 'end;',
  12952. 'var Obj: tobject;',
  12953. 'begin',
  12954. ' obj:=tobject.create;',
  12955. ' tobject.vi:=3;',
  12956. ' if tobject.vi=4 then ;',
  12957. ' tobject.sub:=nil;',
  12958. ' obj.sub:=nil;',
  12959. ' obj.sub.sub:=nil;']);
  12960. ConvertProgram;
  12961. CheckSource('TestClass_ClassVar_Assign',
  12962. LinesToStr([ // statements
  12963. 'rtl.createClass(this,"TObject",null,function(){',
  12964. ' this.vI = 0;',
  12965. ' this.Sub = null;',
  12966. ' this.$init = function () {',
  12967. ' };',
  12968. ' this.$final = function () {',
  12969. ' };',
  12970. ' this.Create = function(){',
  12971. ' $mod.TObject.vI = this.vI+1;',
  12972. ' $mod.TObject.vI = this.vI+1;',
  12973. ' $mod.TObject.vI += 1;',
  12974. ' return this;',
  12975. ' };',
  12976. ' this.GetIt = function(Par){',
  12977. ' var Result = null;',
  12978. ' $mod.TObject.vI = this.vI + 3;',
  12979. ' $mod.TObject.vI = this.vI + 4;',
  12980. ' $mod.TObject.vI += 1;',
  12981. ' Result = this.Sub;',
  12982. ' this.GetIt({',
  12983. ' p: $mod.TObject,',
  12984. ' get: function () {',
  12985. ' return this.p.vI;',
  12986. ' },',
  12987. ' set: function (v) {',
  12988. ' this.p.vI = v;',
  12989. ' }',
  12990. ' });',
  12991. ' return Result;',
  12992. ' };',
  12993. '});',
  12994. 'this.Obj = null;'
  12995. ]),
  12996. LinesToStr([ // $mod.$main
  12997. '$mod.Obj = $mod.TObject.$create("Create");',
  12998. '$mod.TObject.vI = 3;',
  12999. 'if ($mod.TObject.vI === 4);',
  13000. '$mod.TObject.Sub=null;',
  13001. '$mod.TObject.Sub=null;',
  13002. '$mod.TObject.Sub=null;',
  13003. '']));
  13004. end;
  13005. procedure TTestModule.TestClass_CallClassMethod;
  13006. begin
  13007. StartProgram(false);
  13008. Add('type');
  13009. Add(' TObject = class');
  13010. Add(' public');
  13011. Add(' class var vI: longint;');
  13012. Add(' class var Sub: TObject;');
  13013. Add(' constructor Create;');
  13014. Add(' function GetMore(Par: longint): longint;');
  13015. Add(' class function GetIt(Par: longint): tobject;');
  13016. Add(' end;');
  13017. Add('constructor tobject.create;');
  13018. Add('begin');
  13019. Add(' sub:=getit(3);');
  13020. Add(' vi:=getmore(4);');
  13021. Add(' sub:=Self.getit(5);');
  13022. Add(' vi:=Self.getmore(6);');
  13023. Add('end;');
  13024. Add('function tobject.getmore(par: longint): longint;');
  13025. Add('begin');
  13026. Add(' sub:=getit(11);');
  13027. Add(' vi:=getmore(12);');
  13028. Add(' sub:=self.getit(13);');
  13029. Add(' vi:=self.getmore(14);');
  13030. Add('end;');
  13031. Add('class function tobject.getit(par: longint): tobject;');
  13032. Add('begin');
  13033. Add(' sub:=getit(21);');
  13034. Add(' vi:=sub.getmore(22);');
  13035. Add(' sub:=self.getit(23);');
  13036. Add(' vi:=self.sub.getmore(24);');
  13037. Add('end;');
  13038. Add('var Obj: tobject;');
  13039. Add('begin');
  13040. Add(' obj:=tobject.create;');
  13041. Add(' tobject.getit(5);');
  13042. Add(' obj.getit(6);');
  13043. Add(' obj.sub.getit(7);');
  13044. Add(' obj.sub.getit(8).SUB:=nil;');
  13045. Add(' obj.sub.getit(9).GETIT(10);');
  13046. Add(' obj.sub.getit(11).SuB.getit(12);');
  13047. ConvertProgram;
  13048. CheckSource('TestClass_CallClassMethod',
  13049. LinesToStr([ // statements
  13050. 'rtl.createClass(this,"TObject",null,function(){',
  13051. ' this.vI = 0;',
  13052. ' this.Sub = null;',
  13053. ' this.$init = function () {',
  13054. ' };',
  13055. ' this.$final = function () {',
  13056. ' };',
  13057. ' this.Create = function(){',
  13058. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13059. ' $mod.TObject.vI = this.GetMore(4);',
  13060. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13061. ' $mod.TObject.vI = this.GetMore(6);',
  13062. ' return this;',
  13063. ' };',
  13064. ' this.GetMore = function(Par){',
  13065. ' var Result = 0;',
  13066. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13067. ' $mod.TObject.vI = this.GetMore(12);',
  13068. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13069. ' $mod.TObject.vI = this.GetMore(14);',
  13070. ' return Result;',
  13071. ' };',
  13072. ' this.GetIt = function(Par){',
  13073. ' var Result = null;',
  13074. ' $mod.TObject.Sub = this.GetIt(21);',
  13075. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13076. ' $mod.TObject.Sub = this.GetIt(23);',
  13077. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13078. ' return Result;',
  13079. ' };',
  13080. '});',
  13081. 'this.Obj = null;'
  13082. ]),
  13083. LinesToStr([ // $mod.$main
  13084. '$mod.Obj = $mod.TObject.$create("Create");',
  13085. '$mod.TObject.GetIt(5);',
  13086. '$mod.Obj.$class.GetIt(6);',
  13087. '$mod.Obj.Sub.$class.GetIt(7);',
  13088. '$mod.TObject.Sub=null;',
  13089. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13090. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13091. '']));
  13092. end;
  13093. procedure TTestModule.TestClass_CallClassMethodStatic;
  13094. begin
  13095. StartProgram(false);
  13096. Add([
  13097. 'type',
  13098. ' TObject = class',
  13099. ' public',
  13100. ' class function Fly: tobject; static;',
  13101. ' end;',
  13102. 'class function tobject.Fly: tobject;',
  13103. 'begin',
  13104. ' Result.Fly;',
  13105. ' Result.Fly();',
  13106. ' Fly;',
  13107. ' Fly();',
  13108. ' Fly.Fly;',
  13109. ' Fly.Fly();',
  13110. 'end;',
  13111. 'var Obj: tobject;',
  13112. 'begin',
  13113. ' obj.Fly;',
  13114. ' obj.Fly();',
  13115. ' with obj do begin',
  13116. ' Fly;',
  13117. ' Fly();',
  13118. ' end;',
  13119. '']);
  13120. ConvertProgram;
  13121. CheckSource('TestClass_CallClassMethodStatic',
  13122. LinesToStr([ // statements
  13123. 'rtl.createClass(this, "TObject", null, function () {',
  13124. ' this.$init = function () {',
  13125. ' };',
  13126. ' this.$final = function () {',
  13127. ' };',
  13128. ' this.Fly = function () {',
  13129. ' var Result = null;',
  13130. ' $mod.TObject.Fly();',
  13131. ' $mod.TObject.Fly();',
  13132. ' $mod.TObject.Fly();',
  13133. ' $mod.TObject.Fly();',
  13134. ' $mod.TObject.Fly();',
  13135. ' $mod.TObject.Fly();',
  13136. ' return Result;',
  13137. ' };',
  13138. '});',
  13139. 'this.Obj = null;'
  13140. ]),
  13141. LinesToStr([ // $mod.$main
  13142. '$mod.TObject.Fly();',
  13143. '$mod.TObject.Fly();',
  13144. 'var $with = $mod.Obj;',
  13145. '$with.Fly();',
  13146. '$with.Fly();',
  13147. '']));
  13148. end;
  13149. procedure TTestModule.TestClass_Property;
  13150. begin
  13151. StartProgram(false);
  13152. Add('type');
  13153. Add(' TObject = class');
  13154. Add(' Fx: longint;');
  13155. Add(' Fy: longint;');
  13156. Add(' function GetInt: longint;');
  13157. Add(' procedure SetInt(Value: longint);');
  13158. Add(' procedure DoIt;');
  13159. Add(' property IntA: longint read Fx write Fy;');
  13160. Add(' property IntB: longint read GetInt write SetInt;');
  13161. Add(' end;');
  13162. Add('function tobject.getint: longint;');
  13163. Add('begin');
  13164. Add(' result:=fx;');
  13165. Add('end;');
  13166. Add('procedure tobject.setint(value: longint);');
  13167. Add('begin');
  13168. Add(' if value=fy then exit;');
  13169. Add(' fy:=value;');
  13170. Add('end;');
  13171. Add('procedure tobject.doit;');
  13172. Add('begin');
  13173. Add(' IntA:=IntA+1;');
  13174. Add(' Self.IntA:=Self.IntA+1;');
  13175. Add(' IntB:=IntB+1;');
  13176. Add(' Self.IntB:=Self.IntB+1;');
  13177. Add('end;');
  13178. Add('var Obj: tobject;');
  13179. Add('begin');
  13180. Add(' obj.inta:=obj.inta+1;');
  13181. Add(' if obj.intb=2 then;');
  13182. Add(' obj.intb:=obj.intb+2;');
  13183. Add(' obj.setint(obj.inta);');
  13184. ConvertProgram;
  13185. CheckSource('TestClass_Property',
  13186. LinesToStr([ // statements
  13187. 'rtl.createClass(this, "TObject", null, function () {',
  13188. ' this.$init = function () {',
  13189. ' this.Fx = 0;',
  13190. ' this.Fy = 0;',
  13191. ' };',
  13192. ' this.$final = function () {',
  13193. ' };',
  13194. ' this.GetInt = function () {',
  13195. ' var Result = 0;',
  13196. ' Result = this.Fx;',
  13197. ' return Result;',
  13198. ' };',
  13199. ' this.SetInt = function (Value) {',
  13200. ' if (Value === this.Fy) return;',
  13201. ' this.Fy = Value;',
  13202. ' };',
  13203. ' this.DoIt = function () {',
  13204. ' this.Fy = this.Fx + 1;',
  13205. ' this.Fy = this.Fx + 1;',
  13206. ' this.SetInt(this.GetInt() + 1);',
  13207. ' this.SetInt(this.GetInt() + 1);',
  13208. ' };',
  13209. '});',
  13210. 'this.Obj = null;'
  13211. ]),
  13212. LinesToStr([ // $mod.$main
  13213. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13214. 'if ($mod.Obj.GetInt() === 2);',
  13215. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13216. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13217. ]));
  13218. end;
  13219. procedure TTestModule.TestClass_Property_ClassMethod;
  13220. begin
  13221. StartProgram(false);
  13222. Add([
  13223. 'type',
  13224. ' TObject = class',
  13225. ' class var Fx: longint;',
  13226. ' class var Fy: longint;',
  13227. ' class function GetInt: longint;',
  13228. ' class procedure SetInt(Value: longint);',
  13229. ' end;',
  13230. ' TBird = class',
  13231. ' class procedure DoIt;',
  13232. ' class property IntA: longint read Fx write Fy;',
  13233. ' class property IntB: longint read GetInt write SetInt;',
  13234. ' end;',
  13235. 'class function tobject.getint: longint;',
  13236. 'begin',
  13237. ' result:=fx;',
  13238. 'end;',
  13239. 'class procedure tobject.setint(value: longint);',
  13240. 'begin',
  13241. 'end;',
  13242. 'class procedure tbird.doit;',
  13243. 'begin',
  13244. ' FX:=3;',
  13245. ' IntA:=IntA+1;',
  13246. ' Self.IntA:=Self.IntA+1;',
  13247. ' IntB:=IntB+1;',
  13248. ' Self.IntB:=Self.IntB+1;',
  13249. ' with Self do begin',
  13250. ' FX:=11;',
  13251. ' IntA:=IntA+12;',
  13252. ' IntB:=IntB+13;',
  13253. ' end;',
  13254. 'end;',
  13255. 'var Obj: tbird;',
  13256. 'begin',
  13257. ' tbird.fx:=tbird.fx+1;',
  13258. ' tbird.inta:=tbird.inta+1;',
  13259. ' if tbird.intb=2 then;',
  13260. ' tbird.intb:=tbird.intb+2;',
  13261. ' tbird.setint(tbird.inta);',
  13262. ' obj.inta:=obj.inta+1;',
  13263. ' if obj.intb=2 then;',
  13264. ' obj.intb:=obj.intb+2;',
  13265. ' obj.setint(obj.inta);',
  13266. ' with Tbird do begin',
  13267. ' FX:=FY+1;',
  13268. ' inta:=inta+2;',
  13269. ' intb:=intb+3;',
  13270. ' end;',
  13271. ' with Obj do begin',
  13272. ' FX:=FY+1;',
  13273. ' inta:=inta+2;',
  13274. ' intb:=intb+3;',
  13275. ' end;',
  13276. '']);
  13277. ConvertProgram;
  13278. CheckSource('TestClass_Property_ClassMethod',
  13279. LinesToStr([ // statements
  13280. 'rtl.createClass(this, "TObject", null, function () {',
  13281. ' this.Fx = 0;',
  13282. ' this.Fy = 0;',
  13283. ' this.$init = function () {',
  13284. ' };',
  13285. ' this.$final = function () {',
  13286. ' };',
  13287. ' this.GetInt = function () {',
  13288. ' var Result = 0;',
  13289. ' Result = this.Fx;',
  13290. ' return Result;',
  13291. ' };',
  13292. ' this.SetInt = function (Value) {',
  13293. ' };',
  13294. '});',
  13295. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13296. ' this.DoIt = function () {',
  13297. ' $mod.TObject.Fx = 3;',
  13298. ' $mod.TObject.Fy = this.Fx + 1;',
  13299. ' $mod.TObject.Fy = this.Fx + 1;',
  13300. ' this.SetInt(this.GetInt() + 1);',
  13301. ' this.SetInt(this.GetInt() + 1);',
  13302. ' $mod.TObject.Fx = 11;',
  13303. ' $mod.TObject.Fy = this.Fx + 12;',
  13304. ' this.SetInt(this.GetInt() + 13);',
  13305. ' };',
  13306. '});',
  13307. 'this.Obj = null;'
  13308. ]),
  13309. LinesToStr([ // $mod.$main
  13310. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13311. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13312. 'if ($mod.TBird.GetInt() === 2);',
  13313. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13314. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13315. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13316. 'if ($mod.Obj.$class.GetInt() === 2);',
  13317. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13318. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13319. 'var $with = $mod.TBird;',
  13320. '$mod.TObject.Fx = $with.Fy + 1;',
  13321. '$mod.TObject.Fy = $with.Fx + 2;',
  13322. '$with.SetInt($with.GetInt() + 3);',
  13323. 'var $with1 = $mod.Obj;',
  13324. '$mod.TObject.Fx = $with1.Fy + 1;',
  13325. '$mod.TObject.Fy = $with1.Fx + 2;',
  13326. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13327. '']));
  13328. end;
  13329. procedure TTestModule.TestClass_Property_Indexed;
  13330. begin
  13331. StartProgram(false);
  13332. Add([
  13333. 'type',
  13334. ' TObject = class',
  13335. ' FItems: array of longint;',
  13336. ' function GetItems(Index: longint): longint;',
  13337. ' procedure SetItems(Index: longint; Value: longint);',
  13338. ' procedure DoIt;',
  13339. ' property Items[Index: longint]: longint read getitems write setitems;',
  13340. ' end;',
  13341. 'function tobject.getitems(index: longint): longint;',
  13342. 'begin',
  13343. ' Result:=fitems[index];',
  13344. 'end;',
  13345. 'procedure tobject.setitems(index: longint; value: longint);',
  13346. 'begin',
  13347. ' fitems[index]:=value;',
  13348. 'end;',
  13349. 'procedure tobject.doit;',
  13350. 'begin',
  13351. ' items[1]:=2;',
  13352. ' items[3]:=items[4];',
  13353. ' self.items[5]:=self.items[6];',
  13354. ' items[items[7]]:=items[items[8]];',
  13355. 'end;',
  13356. 'var Obj: tobject;',
  13357. 'begin',
  13358. ' obj.Items[11]:=obj.Items[12];',
  13359. '']);
  13360. ConvertProgram;
  13361. CheckSource('TestClass_Property_Indexed',
  13362. LinesToStr([ // statements
  13363. 'rtl.createClass(this, "TObject", null, function () {',
  13364. ' this.$init = function () {',
  13365. ' this.FItems = [];',
  13366. ' };',
  13367. ' this.$final = function () {',
  13368. ' this.FItems = undefined;',
  13369. ' };',
  13370. ' this.GetItems = function (Index) {',
  13371. ' var Result = 0;',
  13372. ' Result = this.FItems[Index];',
  13373. ' return Result;',
  13374. ' };',
  13375. ' this.SetItems = function (Index, Value) {',
  13376. ' this.FItems[Index] = Value;',
  13377. ' };',
  13378. ' this.DoIt = function () {',
  13379. ' this.SetItems(1, 2);',
  13380. ' this.SetItems(3,this.GetItems(4));',
  13381. ' this.SetItems(5,this.GetItems(6));',
  13382. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13383. ' };',
  13384. '});',
  13385. 'this.Obj = null;'
  13386. ]),
  13387. LinesToStr([ // $mod.$main
  13388. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13389. ]));
  13390. end;
  13391. procedure TTestModule.TestClass_Property_IndexSpec;
  13392. begin
  13393. StartProgram(false);
  13394. Add([
  13395. 'type',
  13396. ' TEnum = (red, blue);',
  13397. ' TObject = class',
  13398. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13399. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13400. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13401. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13402. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13403. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13404. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13405. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13406. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13407. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13408. ' end;',
  13409. 'procedure DoIt(b: boolean); begin end;',
  13410. 'var',
  13411. ' o: TObject;',
  13412. 'begin',
  13413. ' o.B1:=o.B1;',
  13414. ' o.B2:=o.B2;',
  13415. ' o.B3:=o.B3;',
  13416. ' o.I1[''a'']:=o.I1[''b''];',
  13417. ' doit(o.b1);',
  13418. ' doit(o.b2);',
  13419. ' doit(o.i1[''c'']);',
  13420. '']);
  13421. ConvertProgram;
  13422. CheckSource('TestClass_Property_IndexSpec',
  13423. LinesToStr([ // statements
  13424. 'this.TEnum = {',
  13425. ' "0": "red",',
  13426. ' red: 0,',
  13427. ' "1": "blue",',
  13428. ' blue: 1',
  13429. '};',
  13430. 'rtl.createClass(this, "TObject", null, function () {',
  13431. ' this.$init = function () {',
  13432. ' };',
  13433. ' this.$final = function () {',
  13434. ' };',
  13435. '});',
  13436. 'this.DoIt = function (b) {',
  13437. '};',
  13438. 'this.o = null;',
  13439. '']),
  13440. LinesToStr([ // $mod.$main
  13441. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13442. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13443. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13444. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13445. '$mod.DoIt($mod.o.GetIntBool(1));',
  13446. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13447. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13448. '']));
  13449. end;
  13450. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13451. begin
  13452. StartProgram(false);
  13453. Add('type');
  13454. Add(' TArray = array of longint;');
  13455. Add(' TObject = class');
  13456. Add(' FItems: TArray;');
  13457. Add(' function GetItems: tarray;');
  13458. Add(' procedure SetItems(Value: tarray);');
  13459. Add(' property Items: tarray read getitems write setitems;');
  13460. Add(' procedure SetNumbers(const Value: tarray);');
  13461. Add(' property Numbers: tarray write setnumbers;');
  13462. Add(' end;');
  13463. Add('function tobject.getitems: tarray;');
  13464. Add('begin');
  13465. Add(' Result:=fitems;');
  13466. Add('end;');
  13467. Add('procedure tobject.setitems(value: tarray);');
  13468. Add('begin');
  13469. Add(' fitems:=value;');
  13470. Add(' fitems:=nil;');
  13471. Add(' Items:=nil;');
  13472. Add(' Items:=Items;');
  13473. Add(' Items[1]:=2;');
  13474. Add(' fitems[3]:=Items[4];');
  13475. Add(' Items[5]:=Items[6];');
  13476. Add(' Self.Items[7]:=8;');
  13477. Add(' Self.Items[9]:=Self.Items[10];');
  13478. Add(' Items[Items[11]]:=Items[Items[12]];');
  13479. Add('end;');
  13480. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13481. Add('begin;');
  13482. Add(' Numbers:=nil;');
  13483. Add(' Numbers:=Value;');
  13484. Add(' Self.Numbers:=Value;');
  13485. Add('end;');
  13486. Add('var Obj: tobject;');
  13487. Add('begin');
  13488. Add(' obj.items:=nil;');
  13489. Add(' obj.items:=obj.items;');
  13490. Add(' obj.items[11]:=obj.items[12];');
  13491. ConvertProgram;
  13492. CheckSource('TestClass_PropertyOfTypeArray',
  13493. LinesToStr([ // statements
  13494. 'rtl.createClass(this, "TObject", null, function () {',
  13495. ' this.$init = function () {',
  13496. ' this.FItems = [];',
  13497. ' };',
  13498. ' this.$final = function () {',
  13499. ' this.FItems = undefined;',
  13500. ' };',
  13501. ' this.GetItems = function () {',
  13502. ' var Result = [];',
  13503. ' Result = rtl.arrayRef(this.FItems);',
  13504. ' return Result;',
  13505. ' };',
  13506. ' this.SetItems = function (Value) {',
  13507. ' this.FItems = rtl.arrayRef(Value);',
  13508. ' this.FItems = [];',
  13509. ' this.SetItems([]);',
  13510. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13511. ' this.GetItems()[1] = 2;',
  13512. ' this.FItems[3] = this.GetItems()[4];',
  13513. ' this.GetItems()[5] = this.GetItems()[6];',
  13514. ' this.GetItems()[7] = 8;',
  13515. ' this.GetItems()[9] = this.GetItems()[10];',
  13516. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13517. ' };',
  13518. ' this.SetNumbers = function (Value) {',
  13519. ' this.SetNumbers([]);',
  13520. ' this.SetNumbers(Value);',
  13521. ' this.SetNumbers(Value);',
  13522. ' };',
  13523. '});',
  13524. 'this.Obj = null;'
  13525. ]),
  13526. LinesToStr([ // $mod.$main
  13527. '$mod.Obj.SetItems([]);',
  13528. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13529. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13530. ]));
  13531. end;
  13532. procedure TTestModule.TestClass_PropertyDefault;
  13533. begin
  13534. StartProgram(false);
  13535. Add([
  13536. 'type',
  13537. ' TArray = array of longint;',
  13538. ' TObject = class',
  13539. ' end;',
  13540. ' TBird = class',
  13541. ' FItems: TArray;',
  13542. ' function GetItems(Index: longint): longint;',
  13543. ' procedure SetItems(Index, Value: longint);',
  13544. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13545. ' end;',
  13546. 'function TBird.getitems(index: longint): longint;',
  13547. 'begin',
  13548. 'end;',
  13549. 'procedure TBird.setitems(index, value: longint);',
  13550. 'begin',
  13551. ' Self[1]:=2;',
  13552. ' Self[3]:=Self[index];',
  13553. ' Self[index]:=Self[Self[value]];',
  13554. ' Self[Self[4]]:=value;',
  13555. 'end;',
  13556. 'var',
  13557. ' Bird: TBird;',
  13558. ' Obj: TObject;',
  13559. 'begin',
  13560. ' bird[11]:=12;',
  13561. ' bird[13]:=bird[14];',
  13562. ' bird[Bird[15]]:=bird[Bird[15]];',
  13563. ' TBird(obj)[16]:=TBird(obj)[17];',
  13564. ' (obj as tbird)[18]:=19;',
  13565. '']);
  13566. ConvertProgram;
  13567. CheckSource('TestClass_PropertyDefault',
  13568. LinesToStr([ // statements
  13569. 'rtl.createClass(this, "TObject", null, function () {',
  13570. ' this.$init = function () {',
  13571. ' };',
  13572. ' this.$final = function () {',
  13573. ' };',
  13574. '});',
  13575. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13576. ' this.$init = function () {',
  13577. ' $mod.TObject.$init.call(this);',
  13578. ' this.FItems = [];',
  13579. ' };',
  13580. ' this.$final = function () {',
  13581. ' this.FItems = undefined;',
  13582. ' $mod.TObject.$final.call(this);',
  13583. ' };',
  13584. ' this.GetItems = function (Index) {',
  13585. ' var Result = 0;',
  13586. ' return Result;',
  13587. ' };',
  13588. ' this.SetItems = function (Index, Value) {',
  13589. ' this.SetItems(1, 2);',
  13590. ' this.SetItems(3, this.GetItems(Index));',
  13591. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13592. ' this.SetItems(this.GetItems(4), Value);',
  13593. ' };',
  13594. '});',
  13595. 'this.Bird = null;',
  13596. 'this.Obj = null;',
  13597. '']),
  13598. LinesToStr([ // $mod.$main
  13599. '$mod.Bird.SetItems(11, 12);',
  13600. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13601. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13602. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13603. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13604. '']));
  13605. end;
  13606. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13607. begin
  13608. StartProgram(false);
  13609. Add([
  13610. 'type',
  13611. ' TObject = class end;',
  13612. ' TAlphaList = class',
  13613. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13614. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13615. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13616. ' end;',
  13617. ' TBetaList = class',
  13618. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13619. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13620. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13621. ' end;',
  13622. ' TBird = class',
  13623. ' procedure DoIt;',
  13624. ' end;',
  13625. 'procedure TBird.DoIt;',
  13626. 'var',
  13627. ' List: TAlphaList;',
  13628. 'begin',
  13629. ' if TBetaList(List[true])[3]=nil then ;',
  13630. ' TBetaList(List[false])[5]:=nil;',
  13631. 'end;',
  13632. 'var',
  13633. ' List: TAlphaList;',
  13634. 'begin',
  13635. ' if TBetaList(List[true])[3]=nil then ;',
  13636. ' TBetaList(List[false])[5]:=nil;',
  13637. '']);
  13638. ConvertProgram;
  13639. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13640. LinesToStr([ // statements
  13641. 'rtl.createClass(this, "TObject", null, function () {',
  13642. ' this.$init = function () {',
  13643. ' };',
  13644. ' this.$final = function () {',
  13645. ' };',
  13646. '});',
  13647. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13648. '});',
  13649. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13650. '});',
  13651. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13652. ' this.DoIt = function () {',
  13653. ' var List = null;',
  13654. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13655. ' List.GetAlphas(false).SetBetas(5, null);',
  13656. ' };',
  13657. '});',
  13658. 'this.List = null;',
  13659. '']),
  13660. LinesToStr([ // $mod.$main
  13661. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13662. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13663. '']));
  13664. end;
  13665. procedure TTestModule.TestClass_PropertyOverride;
  13666. begin
  13667. StartProgram(false);
  13668. Add('type');
  13669. Add(' integer = longint;');
  13670. Add(' TObject = class');
  13671. Add(' FItem: integer;');
  13672. Add(' function GetItem: integer; external name ''GetItem'';');
  13673. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13674. Add(' property Item: integer read getitem write setitem;');
  13675. Add(' end;');
  13676. Add(' TCar = class');
  13677. Add(' FBag: integer;');
  13678. Add(' function GetBag: integer; external name ''GetBag'';');
  13679. Add(' property Item read getbag;');
  13680. Add(' end;');
  13681. Add('var');
  13682. Add(' Obj: tobject;');
  13683. Add(' Car: tcar;');
  13684. Add('begin');
  13685. Add(' Obj.Item:=Obj.Item;');
  13686. Add(' Car.Item:=Car.Item;');
  13687. ConvertProgram;
  13688. CheckSource('TestClass_PropertyOverride',
  13689. LinesToStr([ // statements
  13690. 'rtl.createClass(this, "TObject", null, function () {',
  13691. ' this.$init = function () {',
  13692. ' this.FItem = 0;',
  13693. ' };',
  13694. ' this.$final = function () {',
  13695. ' };',
  13696. '});',
  13697. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13698. ' this.$init = function () {',
  13699. ' $mod.TObject.$init.call(this);',
  13700. ' this.FBag = 0;',
  13701. ' };',
  13702. '});',
  13703. 'this.Obj = null;',
  13704. 'this.Car = null;',
  13705. '']),
  13706. LinesToStr([ // $mod.$main
  13707. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13708. '$mod.Car.SetItem($mod.Car.GetBag());',
  13709. '']));
  13710. end;
  13711. procedure TTestModule.TestClass_PropertyIncVisibility;
  13712. begin
  13713. AddModuleWithIntfImplSrc('unit1.pp',
  13714. LinesToStr([
  13715. 'type',
  13716. ' TNumber = longint;',
  13717. ' TInteger = longint;',
  13718. ' TObject = class',
  13719. ' private',
  13720. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13721. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13722. ' protected',
  13723. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13724. ' end;']),
  13725. LinesToStr([
  13726. '']));
  13727. StartProgram(true);
  13728. Add([
  13729. 'uses unit1;',
  13730. 'type',
  13731. ' TBird = class',
  13732. ' public',
  13733. ' property Items;',
  13734. ' end;',
  13735. 'procedure DoIt(i: TInteger);',
  13736. 'begin',
  13737. 'end;',
  13738. 'var b: TBird;',
  13739. 'begin',
  13740. ' b.Items[1]:=2;',
  13741. ' b.Items[3]:=b.Items[4];',
  13742. ' DoIt(b.Items[5]);',
  13743. '']);
  13744. ConvertProgram;
  13745. CheckSource('TestClass_PropertyIncVisibility',
  13746. LinesToStr([ // statements
  13747. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13748. '});',
  13749. 'this.DoIt = function (i) {',
  13750. '};',
  13751. 'this.b = null;'
  13752. ]),
  13753. LinesToStr([ // $mod.$main
  13754. '$mod.b.SetItems(1, 2);',
  13755. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13756. '$mod.DoIt($mod.b.GetItems(5));'
  13757. ]));
  13758. end;
  13759. procedure TTestModule.TestClass_Assigned;
  13760. begin
  13761. StartProgram(false);
  13762. Add('type');
  13763. Add(' TObject = class');
  13764. Add(' end;');
  13765. Add('var');
  13766. Add(' Obj: tobject;');
  13767. Add(' b: boolean;');
  13768. Add('begin');
  13769. Add(' if Assigned(obj) then ;');
  13770. Add(' b:=Assigned(obj) or false;');
  13771. ConvertProgram;
  13772. CheckSource('TestClass_Assigned',
  13773. LinesToStr([ // statements
  13774. 'rtl.createClass(this, "TObject", null, function () {',
  13775. ' this.$init = function () {',
  13776. ' };',
  13777. ' this.$final = function () {',
  13778. ' };',
  13779. '});',
  13780. 'this.Obj = null;',
  13781. 'this.b = false;'
  13782. ]),
  13783. LinesToStr([ // $mod.$main
  13784. 'if ($mod.Obj != null);',
  13785. '$mod.b = ($mod.Obj != null) || false;'
  13786. ]));
  13787. end;
  13788. procedure TTestModule.TestClass_WithClassDoCreate;
  13789. begin
  13790. StartProgram(false);
  13791. Add('type');
  13792. Add(' TObject = class');
  13793. Add(' aBool: boolean;');
  13794. Add(' Arr: array of boolean;');
  13795. Add(' constructor Create;');
  13796. Add(' end;');
  13797. Add('constructor TObject.Create; begin end;');
  13798. Add('var');
  13799. Add(' Obj: tobject;');
  13800. Add(' b: boolean;');
  13801. Add('begin');
  13802. Add(' with tobject.create do begin');
  13803. Add(' b:=abool;');
  13804. Add(' abool:=b;');
  13805. Add(' b:=arr[1];');
  13806. Add(' arr[2]:=b;');
  13807. Add(' end;');
  13808. Add(' with tobject do');
  13809. Add(' obj:=create;');
  13810. Add(' with obj do begin');
  13811. Add(' create;');
  13812. Add(' b:=abool;');
  13813. Add(' abool:=b;');
  13814. Add(' b:=arr[3];');
  13815. Add(' arr[4]:=b;');
  13816. Add(' end;');
  13817. ConvertProgram;
  13818. CheckSource('TestClass_WithClassDoCreate',
  13819. LinesToStr([ // statements
  13820. 'rtl.createClass(this, "TObject", null, function () {',
  13821. ' this.$init = function () {',
  13822. ' this.aBool = false;',
  13823. ' this.Arr = [];',
  13824. ' };',
  13825. ' this.$final = function () {',
  13826. ' this.Arr = undefined;',
  13827. ' };',
  13828. ' this.Create = function () {',
  13829. ' return this;',
  13830. ' };',
  13831. '});',
  13832. 'this.Obj = null;',
  13833. 'this.b = false;'
  13834. ]),
  13835. LinesToStr([ // $mod.$main
  13836. 'var $with = $mod.TObject.$create("Create");',
  13837. '$mod.b = $with.aBool;',
  13838. '$with.aBool = $mod.b;',
  13839. '$mod.b = $with.Arr[1];',
  13840. '$with.Arr[2] = $mod.b;',
  13841. 'var $with1 = $mod.TObject;',
  13842. '$mod.Obj = $with1.$create("Create");',
  13843. 'var $with2 = $mod.Obj;',
  13844. '$with2.Create();',
  13845. '$mod.b = $with2.aBool;',
  13846. '$with2.aBool = $mod.b;',
  13847. '$mod.b = $with2.Arr[3];',
  13848. '$with2.Arr[4] = $mod.b;',
  13849. '']));
  13850. end;
  13851. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13852. begin
  13853. StartProgram(false);
  13854. Add('type');
  13855. Add(' TObject = class');
  13856. Add(' FInt: longint;');
  13857. Add(' constructor Create;');
  13858. Add(' function GetSize: longint;');
  13859. Add(' procedure SetSize(Value: longint);');
  13860. Add(' property Int: longint read FInt write FInt;');
  13861. Add(' property Size: longint read GetSize write SetSize;');
  13862. Add(' end;');
  13863. Add('constructor TObject.Create; begin end;');
  13864. Add('function TObject.GetSize: longint; begin; end;');
  13865. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13866. Add('var');
  13867. Add(' Obj: tobject;');
  13868. Add(' i: longint;');
  13869. Add('begin');
  13870. Add(' with TObject.Create do begin');
  13871. Add(' i:=int;');
  13872. Add(' int:=i;');
  13873. Add(' i:=size;');
  13874. Add(' size:=i;');
  13875. Add(' end;');
  13876. Add(' with obj do begin');
  13877. Add(' i:=int;');
  13878. Add(' int:=i;');
  13879. Add(' i:=size;');
  13880. Add(' size:=i;');
  13881. Add(' end;');
  13882. ConvertProgram;
  13883. CheckSource('TestClass_WithClassInstDoProperty',
  13884. LinesToStr([ // statements
  13885. 'rtl.createClass(this, "TObject", null, function () {',
  13886. ' this.$init = function () {',
  13887. ' this.FInt = 0;',
  13888. ' };',
  13889. ' this.$final = function () {',
  13890. ' };',
  13891. ' this.Create = function () {',
  13892. ' return this;',
  13893. ' };',
  13894. ' this.GetSize = function () {',
  13895. ' var Result = 0;',
  13896. ' return Result;',
  13897. ' };',
  13898. ' this.SetSize = function (Value) {',
  13899. ' };',
  13900. '});',
  13901. 'this.Obj = null;',
  13902. 'this.i = 0;'
  13903. ]),
  13904. LinesToStr([ // $mod.$main
  13905. 'var $with = $mod.TObject.$create("Create");',
  13906. '$mod.i = $with.FInt;',
  13907. '$with.FInt = $mod.i;',
  13908. '$mod.i = $with.GetSize();',
  13909. '$with.SetSize($mod.i);',
  13910. 'var $with1 = $mod.Obj;',
  13911. '$mod.i = $with1.FInt;',
  13912. '$with1.FInt = $mod.i;',
  13913. '$mod.i = $with1.GetSize();',
  13914. '$with1.SetSize($mod.i);',
  13915. '']));
  13916. end;
  13917. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13918. begin
  13919. StartProgram(false);
  13920. Add('type');
  13921. Add(' TObject = class');
  13922. Add(' constructor Create;');
  13923. Add(' function GetItems(Index: longint): longint;');
  13924. Add(' procedure SetItems(Index, Value: longint);');
  13925. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13926. Add(' end;');
  13927. Add('constructor TObject.Create; begin end;');
  13928. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13929. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13930. Add('var');
  13931. Add(' Obj: tobject;');
  13932. Add(' i: longint;');
  13933. Add('begin');
  13934. Add(' with TObject.Create do begin');
  13935. Add(' i:=Items[1];');
  13936. Add(' Items[2]:=i;');
  13937. Add(' end;');
  13938. Add(' with obj do begin');
  13939. Add(' i:=Items[3];');
  13940. Add(' Items[4]:=i;');
  13941. Add(' end;');
  13942. ConvertProgram;
  13943. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13944. LinesToStr([ // statements
  13945. 'rtl.createClass(this, "TObject", null, function () {',
  13946. ' this.$init = function () {',
  13947. ' };',
  13948. ' this.$final = function () {',
  13949. ' };',
  13950. ' this.Create = function () {',
  13951. ' return this;',
  13952. ' };',
  13953. ' this.GetItems = function (Index) {',
  13954. ' var Result = 0;',
  13955. ' return Result;',
  13956. ' };',
  13957. ' this.SetItems = function (Index, Value) {',
  13958. ' };',
  13959. '});',
  13960. 'this.Obj = null;',
  13961. 'this.i = 0;'
  13962. ]),
  13963. LinesToStr([ // $mod.$main
  13964. 'var $with = $mod.TObject.$create("Create");',
  13965. '$mod.i = $with.GetItems(1);',
  13966. '$with.SetItems(2, $mod.i);',
  13967. 'var $with1 = $mod.Obj;',
  13968. '$mod.i = $with1.GetItems(3);',
  13969. '$with1.SetItems(4, $mod.i);',
  13970. '']));
  13971. end;
  13972. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13973. begin
  13974. StartProgram(false);
  13975. Add('type');
  13976. Add(' TObject = class');
  13977. Add(' constructor Create;');
  13978. Add(' function GetSize: longint;');
  13979. Add(' procedure SetSize(Value: longint);');
  13980. Add(' end;');
  13981. Add('constructor TObject.Create; begin end;');
  13982. Add('function TObject.GetSize: longint; begin; end;');
  13983. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13984. Add('var');
  13985. Add(' Obj: tobject;');
  13986. Add(' i: longint;');
  13987. Add('begin');
  13988. Add(' with TObject.Create do begin');
  13989. Add(' i:=GetSize;');
  13990. Add(' i:=GetSize();');
  13991. Add(' SetSize(i);');
  13992. Add(' end;');
  13993. Add(' with obj do begin');
  13994. Add(' i:=GetSize;');
  13995. Add(' i:=GetSize();');
  13996. Add(' SetSize(i);');
  13997. Add(' end;');
  13998. ConvertProgram;
  13999. CheckSource('TestClass_WithClassInstDoFunc',
  14000. LinesToStr([ // statements
  14001. 'rtl.createClass(this, "TObject", null, function () {',
  14002. ' this.$init = function () {',
  14003. ' };',
  14004. ' this.$final = function () {',
  14005. ' };',
  14006. ' this.Create = function () {',
  14007. ' return this;',
  14008. ' };',
  14009. ' this.GetSize = function () {',
  14010. ' var Result = 0;',
  14011. ' return Result;',
  14012. ' };',
  14013. ' this.SetSize = function (Value) {',
  14014. ' };',
  14015. '});',
  14016. 'this.Obj = null;',
  14017. 'this.i = 0;'
  14018. ]),
  14019. LinesToStr([ // $mod.$main
  14020. 'var $with = $mod.TObject.$create("Create");',
  14021. '$mod.i = $with.GetSize();',
  14022. '$mod.i = $with.GetSize();',
  14023. '$with.SetSize($mod.i);',
  14024. 'var $with1 = $mod.Obj;',
  14025. '$mod.i = $with1.GetSize();',
  14026. '$mod.i = $with1.GetSize();',
  14027. '$with1.SetSize($mod.i);',
  14028. '']));
  14029. end;
  14030. procedure TTestModule.TestClass_TypeCast;
  14031. begin
  14032. StartProgram(false);
  14033. Add('type');
  14034. Add(' TObject = class');
  14035. Add(' Next: TObject;');
  14036. Add(' constructor Create;');
  14037. Add(' end;');
  14038. Add(' TControl = class(TObject)');
  14039. Add(' Arr: array of TObject;');
  14040. Add(' function GetIt(vI: longint = 0): TObject;');
  14041. Add(' end;');
  14042. Add('constructor tobject.create; begin end;');
  14043. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14044. Add('var');
  14045. Add(' Obj: tobject;');
  14046. Add('begin');
  14047. Add(' obj:=tcontrol(obj).next;');
  14048. Add(' tcontrol(obj):=nil;');
  14049. Add(' obj:=tcontrol(obj);');
  14050. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14051. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14052. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14053. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14054. Add(' obj:=tcontrol(nil);');
  14055. ConvertProgram;
  14056. CheckSource('TestClass_TypeCast',
  14057. LinesToStr([ // statements
  14058. 'rtl.createClass(this, "TObject", null, function () {',
  14059. ' this.$init = function () {',
  14060. ' this.Next = null;',
  14061. ' };',
  14062. ' this.$final = function () {',
  14063. ' this.Next = undefined;',
  14064. ' };',
  14065. ' this.Create = function () {',
  14066. ' return this;',
  14067. ' };',
  14068. '});',
  14069. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14070. ' this.$init = function () {',
  14071. ' $mod.TObject.$init.call(this);',
  14072. ' this.Arr = [];',
  14073. ' };',
  14074. ' this.$final = function () {',
  14075. ' this.Arr = undefined;',
  14076. ' $mod.TObject.$final.call(this);',
  14077. ' };',
  14078. ' this.GetIt = function (vI) {',
  14079. ' var Result = null;',
  14080. ' return Result;',
  14081. ' };',
  14082. '});',
  14083. 'this.Obj = null;'
  14084. ]),
  14085. LinesToStr([ // $mod.$main
  14086. '$mod.Obj = $mod.Obj.Next;',
  14087. '$mod.Obj = null;',
  14088. '$mod.Obj = $mod.Obj;',
  14089. '$mod.Obj = $mod.Obj.GetIt(0);',
  14090. '$mod.Obj = $mod.Obj.GetIt(0);',
  14091. '$mod.Obj = $mod.Obj.GetIt(1);',
  14092. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14093. '$mod.Obj = null;',
  14094. '']));
  14095. end;
  14096. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14097. begin
  14098. StartProgram(false);
  14099. Add('type');
  14100. Add(' TObject = class end;');
  14101. Add('procedure ProcA(var A);');
  14102. Add('begin');
  14103. Add(' TObject(A):=nil;');
  14104. Add(' TObject(A):=TObject(A);');
  14105. Add(' if TObject(A)=nil then ;');
  14106. Add(' if nil=TObject(A) then ;');
  14107. Add('end;');
  14108. Add('procedure ProcB(out A);');
  14109. Add('begin');
  14110. Add(' TObject(A):=nil;');
  14111. Add(' TObject(A):=TObject(A);');
  14112. Add(' if TObject(A)=nil then ;');
  14113. Add(' if nil=TObject(A) then ;');
  14114. Add('end;');
  14115. Add('procedure ProcC(const A);');
  14116. Add('begin');
  14117. Add(' if TObject(A)=nil then ;');
  14118. Add(' if nil=TObject(A) then ;');
  14119. Add('end;');
  14120. Add('var o: TObject;');
  14121. Add('begin');
  14122. Add(' ProcA(o);');
  14123. Add(' ProcB(o);');
  14124. Add(' ProcC(o);');
  14125. ConvertProgram;
  14126. CheckSource('TestClass_TypeCastUntypedParam',
  14127. LinesToStr([ // statements
  14128. 'rtl.createClass(this, "TObject", null, function () {',
  14129. ' this.$init = function () {',
  14130. ' };',
  14131. ' this.$final = function () {',
  14132. ' };',
  14133. '});',
  14134. 'this.ProcA = function (A) {',
  14135. ' A.set(null);',
  14136. ' A.set(A.get());',
  14137. ' if (A.get() === null);',
  14138. ' if (null === A.get());',
  14139. '};',
  14140. 'this.ProcB = function (A) {',
  14141. ' A.set(null);',
  14142. ' A.set(A.get());',
  14143. ' if (A.get() === null);',
  14144. ' if (null === A.get());',
  14145. '};',
  14146. 'this.ProcC = function (A) {',
  14147. ' if (A === null);',
  14148. ' if (null === A);',
  14149. '};',
  14150. 'this.o = null;',
  14151. '']),
  14152. LinesToStr([ // $mod.$main
  14153. '$mod.ProcA({',
  14154. ' p: $mod,',
  14155. ' get: function () {',
  14156. ' return this.p.o;',
  14157. ' },',
  14158. ' set: function (v) {',
  14159. ' this.p.o = v;',
  14160. ' }',
  14161. '});',
  14162. '$mod.ProcB({',
  14163. ' p: $mod,',
  14164. ' get: function () {',
  14165. ' return this.p.o;',
  14166. ' },',
  14167. ' set: function (v) {',
  14168. ' this.p.o = v;',
  14169. ' }',
  14170. '});',
  14171. '$mod.ProcC($mod.o);',
  14172. '']));
  14173. end;
  14174. procedure TTestModule.TestClass_Overloads;
  14175. begin
  14176. StartProgram(false);
  14177. Add('type');
  14178. Add(' TObject = class');
  14179. Add(' procedure DoIt;');
  14180. Add(' procedure DoIt(vI: longint);');
  14181. Add(' end;');
  14182. Add('procedure TObject.DoIt;');
  14183. Add('begin');
  14184. Add(' DoIt;');
  14185. Add(' DoIt(1);');
  14186. Add('end;');
  14187. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14188. Add('begin');
  14189. ConvertProgram;
  14190. CheckSource('TestClass_Overloads',
  14191. LinesToStr([ // statements
  14192. 'rtl.createClass(this, "TObject", null, function () {',
  14193. ' this.$init = function () {',
  14194. ' };',
  14195. ' this.$final = function () {',
  14196. ' };',
  14197. ' this.DoIt = function () {',
  14198. ' this.DoIt();',
  14199. ' this.DoIt$1(1);',
  14200. ' };',
  14201. ' this.DoIt$1 = function (vI) {',
  14202. ' };',
  14203. '});',
  14204. '']),
  14205. LinesToStr([ // $mod.$main
  14206. '']));
  14207. end;
  14208. procedure TTestModule.TestClass_OverloadsAncestor;
  14209. begin
  14210. StartProgram(false);
  14211. Add('type');
  14212. Add(' TObject = class;');
  14213. Add(' TObject = class');
  14214. Add(' procedure DoIt(vA: longint);');
  14215. Add(' procedure DoIt(vA, vB: longint);');
  14216. Add(' end;');
  14217. Add(' TCar = class;');
  14218. Add(' TCar = class');
  14219. Add(' procedure DoIt(vA: longint);');
  14220. Add(' procedure DoIt(vA, vB: longint);');
  14221. Add(' end;');
  14222. Add('procedure tobject.doit(va: longint);');
  14223. Add('begin');
  14224. Add(' doit(1);');
  14225. Add(' doit(1,2);');
  14226. Add('end;');
  14227. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14228. Add('procedure tcar.doit(va: longint);');
  14229. Add('begin');
  14230. Add(' doit(1);');
  14231. Add(' doit(1,2);');
  14232. Add(' inherited doit(1);');
  14233. Add(' inherited doit(1,2);');
  14234. Add('end;');
  14235. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14236. Add('begin');
  14237. ConvertProgram;
  14238. CheckSource('TestClass_OverloadsAncestor',
  14239. LinesToStr([ // statements
  14240. 'rtl.createClass(this, "TObject", null, function () {',
  14241. ' this.$init = function () {',
  14242. ' };',
  14243. ' this.$final = function () {',
  14244. ' };',
  14245. ' this.DoIt = function (vA) {',
  14246. ' this.DoIt(1);',
  14247. ' this.DoIt$1(1,2);',
  14248. ' };',
  14249. ' this.DoIt$1 = function (vA, vB) {',
  14250. ' };',
  14251. '});',
  14252. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14253. ' this.DoIt$2 = function (vA) {',
  14254. ' this.DoIt$2(1);',
  14255. ' this.DoIt$3(1, 2);',
  14256. ' $mod.TObject.DoIt.call(this, 1);',
  14257. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14258. ' };',
  14259. ' this.DoIt$3 = function (vA, vB) {',
  14260. ' };',
  14261. '});',
  14262. '']),
  14263. LinesToStr([ // $mod.$main
  14264. '']));
  14265. end;
  14266. procedure TTestModule.TestClass_OverloadConstructor;
  14267. begin
  14268. StartProgram(false);
  14269. Add('type');
  14270. Add(' TObject = class');
  14271. Add(' constructor Create(vA: longint);');
  14272. Add(' constructor Create(vA, vB: longint);');
  14273. Add(' end;');
  14274. Add(' TCar = class');
  14275. Add(' constructor Create(vA: longint);');
  14276. Add(' constructor Create(vA, vB: longint);');
  14277. Add(' end;');
  14278. Add('constructor tobject.create(va: longint);');
  14279. Add('begin');
  14280. Add(' create(1);');
  14281. Add(' create(1,2);');
  14282. Add('end;');
  14283. Add('constructor tobject.create(va, vb: longint); begin end;');
  14284. Add('constructor tcar.create(va: longint);');
  14285. Add('begin');
  14286. Add(' create(1);');
  14287. Add(' create(1,2);');
  14288. Add(' inherited create(1);');
  14289. Add(' inherited create(1,2);');
  14290. Add('end;');
  14291. Add('constructor tcar.create(va, vb: longint); begin end;');
  14292. Add('begin');
  14293. Add(' tobject.create(1);');
  14294. Add(' tobject.create(1,2);');
  14295. Add(' tcar.create(1);');
  14296. Add(' tcar.create(1,2);');
  14297. ConvertProgram;
  14298. CheckSource('TestClass_OverloadConstructor',
  14299. LinesToStr([ // statements
  14300. 'rtl.createClass(this, "TObject", null, function () {',
  14301. ' this.$init = function () {',
  14302. ' };',
  14303. ' this.$final = function () {',
  14304. ' };',
  14305. ' this.Create = function (vA) {',
  14306. ' this.Create(1);',
  14307. ' this.Create$1(1,2);',
  14308. ' return this;',
  14309. ' };',
  14310. ' this.Create$1 = function (vA, vB) {',
  14311. ' return this;',
  14312. ' };',
  14313. '});',
  14314. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14315. ' this.Create$2 = function (vA) {',
  14316. ' this.Create$2(1);',
  14317. ' this.Create$3(1, 2);',
  14318. ' $mod.TObject.Create.call(this, 1);',
  14319. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14320. ' return this;',
  14321. ' };',
  14322. ' this.Create$3 = function (vA, vB) {',
  14323. ' return this;',
  14324. ' };',
  14325. '});',
  14326. '']),
  14327. LinesToStr([ // $mod.$main
  14328. '$mod.TObject.$create("Create", [1]);',
  14329. '$mod.TObject.$create("Create$1", [1, 2]);',
  14330. '$mod.TCar.$create("Create$2", [1]);',
  14331. '$mod.TCar.$create("Create$3", [1, 2]);',
  14332. '']));
  14333. end;
  14334. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14335. begin
  14336. StartProgram(false);
  14337. Add([
  14338. '{$mode delphi}',
  14339. 'type',
  14340. ' TObject = class end;',
  14341. ' TBird = class',
  14342. ' function {#a}GetValue: longint; overload; virtual;',
  14343. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14344. ' end;',
  14345. ' TEagle = class(TBird)',
  14346. ' function {#c}GetValue: longint; overload; override;',
  14347. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14348. ' end;',
  14349. 'function TBird.GetValue: longint;',
  14350. 'begin',
  14351. ' if 3={@a}GetValue then ;',
  14352. ' if 4={@b}GetValue(5) then ;',
  14353. 'end;',
  14354. 'function TBird.GetValue(AValue: longint): longint;',
  14355. 'begin',
  14356. 'end;',
  14357. 'function TEagle.GetValue: longint;',
  14358. 'begin',
  14359. ' if 13={@c}GetValue then ;',
  14360. ' if 14={@d}GetValue(15) then ;',
  14361. ' if 15=inherited {@a}GetValue then ;',
  14362. ' if 16=inherited {@b}GetValue(17) then ;',
  14363. 'end;',
  14364. 'function TEagle.GetValue(AValue: longint): longint;',
  14365. 'begin',
  14366. 'end;',
  14367. 'var',
  14368. ' e: TEagle;',
  14369. 'begin',
  14370. ' if 23=e.{@c}GetValue then ;',
  14371. ' if 24=e.{@d}GetValue(25) then ;']);
  14372. ConvertProgram;
  14373. CheckSource('TestClass_OverloadDelphiOverride',
  14374. LinesToStr([ // statements
  14375. 'rtl.createClass(this, "TObject", null, function () {',
  14376. ' this.$init = function () {',
  14377. ' };',
  14378. ' this.$final = function () {',
  14379. ' };',
  14380. '});',
  14381. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14382. ' this.GetValue = function () {',
  14383. ' var Result = 0;',
  14384. ' if (3 === this.GetValue()) ;',
  14385. ' if (4 === this.GetValue$1(5)) ;',
  14386. ' return Result;',
  14387. ' };',
  14388. ' this.GetValue$1 = function (AValue) {',
  14389. ' var Result = 0;',
  14390. ' return Result;',
  14391. ' };',
  14392. '});',
  14393. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14394. ' this.GetValue = function () {',
  14395. ' var Result = 0;',
  14396. ' if (13 === this.GetValue()) ;',
  14397. ' if (14 === this.GetValue$1(15)) ;',
  14398. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14399. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14400. ' return Result;',
  14401. ' };',
  14402. ' this.GetValue$1 = function (AValue) {',
  14403. ' var Result = 0;',
  14404. ' return Result;',
  14405. ' };',
  14406. '});',
  14407. 'this.e = null;',
  14408. '']),
  14409. LinesToStr([ // $mod.$main
  14410. 'if (23 === $mod.e.GetValue()) ;',
  14411. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14412. '']));
  14413. end;
  14414. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14415. begin
  14416. StartProgram(false);
  14417. Add([
  14418. '{$mode delphi}',
  14419. 'type',
  14420. ' TObject = class end;',
  14421. ' TAnimal = class',
  14422. ' public',
  14423. ' {#animal_a}A: longint;',
  14424. ' function {#animal_b}B: longint;',
  14425. ' end;',
  14426. ' TBird = class(TAnimal)',
  14427. ' public',
  14428. ' {#bird_a}A: double;',
  14429. ' {#bird_b}B: boolean;',
  14430. ' end;',
  14431. ' TEagle = class(TBird)',
  14432. ' public',
  14433. ' function {#eagle_a}A: boolean;',
  14434. ' {#eagle_b}B: double;',
  14435. ' end;',
  14436. 'function TAnimal.B: longint;',
  14437. 'begin',
  14438. 'end;',
  14439. 'function TEagle.A: boolean;',
  14440. 'begin',
  14441. ' {@eagle_b}B:=3.3;',
  14442. ' {@eagle_a}A();',
  14443. ' TBird(Self).{@bird_b}B:=true;',
  14444. ' TAnimal(Self).{@animal_a}A:=17;',
  14445. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14446. 'end;',
  14447. 'var',
  14448. ' e: TEagle;',
  14449. 'begin',
  14450. ' e.{@eagle_b}B:=5.3;',
  14451. ' if e.{@eagle_a}A then ;',
  14452. '']);
  14453. ConvertProgram;
  14454. CheckSource('TestClass_ReintroduceVarDelphi',
  14455. LinesToStr([ // statements
  14456. 'rtl.createClass(this, "TObject", null, function () {',
  14457. ' this.$init = function () {',
  14458. ' };',
  14459. ' this.$final = function () {',
  14460. ' };',
  14461. '});',
  14462. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14463. ' this.$init = function () {',
  14464. ' $mod.TObject.$init.call(this);',
  14465. ' this.A = 0;',
  14466. ' };',
  14467. ' this.B = function () {',
  14468. ' var Result = 0;',
  14469. ' return Result;',
  14470. ' };',
  14471. '});',
  14472. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14473. ' this.$init = function () {',
  14474. ' $mod.TAnimal.$init.call(this);',
  14475. ' this.A$1 = 0.0;',
  14476. ' this.B$1 = false;',
  14477. ' };',
  14478. '});',
  14479. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14480. ' this.$init = function () {',
  14481. ' $mod.TBird.$init.call(this);',
  14482. ' this.B$2 = 0.0;',
  14483. ' };',
  14484. ' this.A$2 = function () {',
  14485. ' var Result = false;',
  14486. ' this.B$2 = 3.3;',
  14487. ' this.A$2();',
  14488. ' this.B$1 = true;',
  14489. ' this.A = 17;',
  14490. ' this.B$1 = this.A$1 > 1;',
  14491. ' return Result;',
  14492. ' };',
  14493. '});',
  14494. 'this.e = null;',
  14495. '']),
  14496. LinesToStr([ // $mod.$main
  14497. '$mod.e.B$2 = 5.3;',
  14498. 'if ($mod.e.A$2()) ;',
  14499. '']));
  14500. end;
  14501. procedure TTestModule.TestClass_ReintroducedVar;
  14502. begin
  14503. StartProgram(false);
  14504. Add('type');
  14505. Add(' TObject = class');
  14506. Add(' strict private');
  14507. Add(' Some: longint;');
  14508. Add(' end;');
  14509. Add(' TMobile = class');
  14510. Add(' strict private');
  14511. Add(' Some: string;');
  14512. Add(' end;');
  14513. Add(' TCar = class(tmobile)');
  14514. Add(' procedure Some;');
  14515. Add(' procedure Some(vA: longint);');
  14516. Add(' end;');
  14517. Add('procedure tcar.some;');
  14518. Add('begin');
  14519. Add(' Some;');
  14520. Add(' Some(1);');
  14521. Add('end;');
  14522. Add('procedure tcar.some(va: longint); begin end;');
  14523. Add('begin');
  14524. ConvertProgram;
  14525. CheckSource('TestClass_ReintroducedVar',
  14526. LinesToStr([ // statements
  14527. 'rtl.createClass(this, "TObject", null, function () {',
  14528. ' this.$init = function () {',
  14529. ' this.Some = 0;',
  14530. ' };',
  14531. ' this.$final = function () {',
  14532. ' };',
  14533. '});',
  14534. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14535. ' this.$init = function () {',
  14536. ' $mod.TObject.$init.call(this);',
  14537. ' this.Some$1 = "";',
  14538. ' };',
  14539. '});',
  14540. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14541. ' this.Some$2 = function () {',
  14542. ' this.Some$2();',
  14543. ' this.Some$3(1);',
  14544. ' };',
  14545. ' this.Some$3 = function (vA) {',
  14546. ' };',
  14547. '});',
  14548. '']),
  14549. LinesToStr([ // $mod.$main
  14550. '']));
  14551. end;
  14552. procedure TTestModule.TestClass_RaiseDescendant;
  14553. begin
  14554. StartProgram(false);
  14555. Add([
  14556. 'type',
  14557. ' TObject = class',
  14558. ' constructor Create(Msg: string);',
  14559. ' end;',
  14560. ' Exception = class',
  14561. ' end;',
  14562. ' EConvertError = class(Exception)',
  14563. ' end;',
  14564. 'constructor TObject.Create(Msg: string); begin end;',
  14565. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14566. 'begin',
  14567. ' raise Exception.Create(''Bar1'');',
  14568. ' raise EConvertError.Create(''Bar2'');',
  14569. ' raise AssertConv(''Bar2'');',
  14570. ' raise AssertConv;',
  14571. '']);
  14572. ConvertProgram;
  14573. CheckSource('TestClass_RaiseDescendant',
  14574. LinesToStr([ // statements
  14575. 'rtl.createClass(this, "TObject", null, function () {',
  14576. ' this.$init = function () {',
  14577. ' };',
  14578. ' this.$final = function () {',
  14579. ' };',
  14580. ' this.Create = function (Msg) {',
  14581. ' return this;',
  14582. ' };',
  14583. '});',
  14584. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14585. '});',
  14586. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14587. '});',
  14588. 'this.AssertConv = function (Msg) {',
  14589. ' var Result = null;',
  14590. ' return Result;',
  14591. '};',
  14592. '']),
  14593. LinesToStr([ // $mod.$main
  14594. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14595. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14596. 'throw $mod.AssertConv("Bar2");',
  14597. 'throw $mod.AssertConv("def");',
  14598. '']));
  14599. end;
  14600. procedure TTestModule.TestClass_ExternalMethod;
  14601. begin
  14602. AddModuleWithIntfImplSrc('unit2.pas',
  14603. LinesToStr([
  14604. 'type',
  14605. ' TObject = class',
  14606. ' public',
  14607. ' procedure Intern; external name ''$DoIntern'';',
  14608. ' end;',
  14609. '']),
  14610. LinesToStr([
  14611. '']));
  14612. StartUnit(true);
  14613. Add('interface');
  14614. Add('uses unit2;');
  14615. Add('type');
  14616. Add(' TCar = class(TObject)');
  14617. Add(' public');
  14618. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14619. Add(' procedure DoIt;');
  14620. Add(' end;');
  14621. Add('implementation');
  14622. Add('procedure tcar.doit;');
  14623. Add('begin');
  14624. Add(' Intern;');
  14625. Add(' Intern();');
  14626. Add(' Intern2;');
  14627. Add(' Intern2();');
  14628. Add('end;');
  14629. Add('var Obj: TCar;');
  14630. Add('begin');
  14631. Add(' obj.intern;');
  14632. Add(' obj.intern();');
  14633. Add(' obj.intern2;');
  14634. Add(' obj.intern2();');
  14635. Add(' obj.doit;');
  14636. Add(' obj.doit();');
  14637. Add(' with obj do begin');
  14638. Add(' Intern;');
  14639. Add(' Intern();');
  14640. Add(' Intern2;');
  14641. Add(' Intern2();');
  14642. Add(' end;');
  14643. ConvertUnit;
  14644. CheckSource('TestClass_ExternalMethod',
  14645. LinesToStr([
  14646. 'var $impl = $mod.$impl;',
  14647. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14648. ' this.DoIt = function () {',
  14649. ' this.$DoIntern();',
  14650. ' this.$DoIntern();',
  14651. ' this.$DoIntern2();',
  14652. ' this.$DoIntern2();',
  14653. ' };',
  14654. ' });',
  14655. '']),
  14656. LinesToStr([ // this.$init
  14657. '$impl.Obj.$DoIntern();',
  14658. '$impl.Obj.$DoIntern();',
  14659. '$impl.Obj.$DoIntern2();',
  14660. '$impl.Obj.$DoIntern2();',
  14661. '$impl.Obj.DoIt();',
  14662. '$impl.Obj.DoIt();',
  14663. 'var $with = $impl.Obj;',
  14664. '$with.$DoIntern();',
  14665. '$with.$DoIntern();',
  14666. '$with.$DoIntern2();',
  14667. '$with.$DoIntern2();',
  14668. '']),
  14669. LinesToStr([ // implementation
  14670. '$impl.Obj = null;',
  14671. '']) );
  14672. end;
  14673. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14674. begin
  14675. StartProgram(false);
  14676. Add('type');
  14677. Add(' TObject = class');
  14678. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14679. Add(' end;');
  14680. Add('begin');
  14681. SetExpectedPasResolverError('Virtual method name must match external',
  14682. nVirtualMethodNameMustMatchExternal);
  14683. ConvertProgram;
  14684. end;
  14685. procedure TTestModule.TestClass_ExternalOverrideFail;
  14686. begin
  14687. StartProgram(false);
  14688. Add('type');
  14689. Add(' TObject = class');
  14690. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14691. Add(' end;');
  14692. Add(' TCar = class');
  14693. Add(' procedure DoIt; override; external name ''DoIt'';');
  14694. Add(' end;');
  14695. Add('begin');
  14696. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14697. nInvalidXModifierY);
  14698. ConvertProgram;
  14699. end;
  14700. procedure TTestModule.TestClass_ExternalVar;
  14701. begin
  14702. AddModuleWithIntfImplSrc('unit2.pas',
  14703. LinesToStr([
  14704. '{$modeswitch externalclass}',
  14705. 'type',
  14706. ' TObject = class',
  14707. ' public',
  14708. ' Intern: longint external name ''$Intern'';',
  14709. ' Bracket: longint external name ''["A B"]'';',
  14710. ' end;',
  14711. '']),
  14712. LinesToStr([
  14713. '']));
  14714. StartUnit(true);
  14715. Add([
  14716. 'interface',
  14717. 'uses unit2;',
  14718. '{$modeswitch externalclass}',
  14719. 'type',
  14720. ' TCar = class(tobject)',
  14721. ' public',
  14722. ' Intern2: longint external name ''$Intern2'';',
  14723. ' procedure DoIt;',
  14724. ' end;',
  14725. 'implementation',
  14726. 'procedure tcar.doit;',
  14727. 'begin',
  14728. ' Intern:=Intern+1;',
  14729. ' Intern2:=Intern2+2;',
  14730. ' Bracket:=Bracket+3;',
  14731. 'end;',
  14732. 'var Obj: TCar;',
  14733. 'begin',
  14734. ' obj.intern:=obj.intern+1;',
  14735. ' obj.intern2:=obj.intern2+2;',
  14736. ' obj.Bracket:=obj.Bracket+3;',
  14737. ' with obj do begin',
  14738. ' intern:=intern+1;',
  14739. ' intern2:=intern2+2;',
  14740. ' Bracket:=Bracket+3;',
  14741. ' end;']);
  14742. ConvertUnit;
  14743. CheckSource('TestClass_ExternalVar',
  14744. LinesToStr([
  14745. 'var $impl = $mod.$impl;',
  14746. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14747. ' this.DoIt = function () {',
  14748. ' this.$Intern = this.$Intern + 1;',
  14749. ' this.$Intern2 = this.$Intern2 + 2;',
  14750. ' this["A B"] = this["A B"] + 3;',
  14751. ' };',
  14752. ' });',
  14753. '']),
  14754. LinesToStr([
  14755. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14756. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14757. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14758. 'var $with = $impl.Obj;',
  14759. '$with.$Intern = $with.$Intern + 1;',
  14760. '$with.$Intern2 = $with.$Intern2 + 2;',
  14761. '$with["A B"] = $with["A B"] + 3;',
  14762. '']),
  14763. LinesToStr([ // implementation
  14764. '$impl.Obj = null;',
  14765. '']));
  14766. end;
  14767. procedure TTestModule.TestClass_Const;
  14768. begin
  14769. StartProgram(false);
  14770. Add([
  14771. 'type',
  14772. ' integer = longint;',
  14773. ' TClass = class of TObject;',
  14774. ' TObject = class',
  14775. ' public',
  14776. ' const cI: integer = 3;',
  14777. ' procedure DoIt;',
  14778. ' class procedure DoMore;',
  14779. ' end;',
  14780. 'procedure tobject.doit;',
  14781. 'begin',
  14782. ' if cI=4 then;',
  14783. ' if 5=cI then;',
  14784. ' if Self.cI=6 then;',
  14785. ' if 7=Self.cI then;',
  14786. ' with Self do begin',
  14787. ' if cI=11 then;',
  14788. ' if 12=cI then;',
  14789. ' end;',
  14790. 'end;',
  14791. 'class procedure tobject.domore;',
  14792. 'begin',
  14793. ' if cI=8 then;',
  14794. ' if Self.cI=9 then;',
  14795. ' if 10=cI then;',
  14796. ' if 11=Self.cI then;',
  14797. ' with Self do begin',
  14798. ' if cI=13 then;',
  14799. ' if 14=cI then;',
  14800. ' end;',
  14801. 'end;',
  14802. 'var',
  14803. ' Obj: TObject;',
  14804. ' Cla: TClass;',
  14805. 'begin',
  14806. ' if TObject.cI=21 then ;',
  14807. ' if Obj.cI=22 then ;',
  14808. ' if Cla.cI=23 then ;',
  14809. ' with obj do if ci=24 then;',
  14810. ' with TObject do if ci=25 then;',
  14811. ' with Cla do if ci=26 then;']);
  14812. ConvertProgram;
  14813. CheckSource('TestClass_Const',
  14814. LinesToStr([
  14815. 'rtl.createClass(this, "TObject", null, function () {',
  14816. ' this.cI = 3;',
  14817. ' this.$init = function () {',
  14818. ' };',
  14819. ' this.$final = function () {',
  14820. ' };',
  14821. ' this.DoIt = function () {',
  14822. ' if (this.cI === 4) ;',
  14823. ' if (5 === this.cI) ;',
  14824. ' if (this.cI === 6) ;',
  14825. ' if (7 === this.cI) ;',
  14826. ' if (this.cI === 11) ;',
  14827. ' if (12 === this.cI) ;',
  14828. ' };',
  14829. ' this.DoMore = function () {',
  14830. ' if (this.cI === 8) ;',
  14831. ' if (this.cI === 9) ;',
  14832. ' if (10 === this.cI) ;',
  14833. ' if (11 === this.cI) ;',
  14834. ' if (this.cI === 13) ;',
  14835. ' if (14 === this.cI) ;',
  14836. ' };',
  14837. '});',
  14838. 'this.Obj = null;',
  14839. 'this.Cla = null;',
  14840. '']),
  14841. LinesToStr([
  14842. 'if ($mod.TObject.cI === 21) ;',
  14843. 'if ($mod.Obj.cI === 22) ;',
  14844. 'if ($mod.Cla.cI === 23) ;',
  14845. 'var $with = $mod.Obj;',
  14846. 'if ($with.cI === 24) ;',
  14847. 'var $with1 = $mod.TObject;',
  14848. 'if ($with1.cI === 25) ;',
  14849. 'var $with2 = $mod.Cla;',
  14850. 'if ($with2.cI === 26) ;',
  14851. '']));
  14852. end;
  14853. procedure TTestModule.TestClass_ConstEnum;
  14854. begin
  14855. StartProgram(false);
  14856. Add([
  14857. 'type',
  14858. ' TEnum = (red,blue);',
  14859. ' TObject = class',
  14860. ' end;',
  14861. ' TAnimal = class',
  14862. ' public',
  14863. ' type TSubEnum = (light,dark);',
  14864. ' const a = high(TEnum);',
  14865. ' const b = high(TSubEnum);',
  14866. ' end;',
  14867. ' TBird = class(TAnimal)',
  14868. ' public',
  14869. ' const c = high(TEnum);',
  14870. ' const d = high(TSubEnum);',
  14871. ' end;',
  14872. ' TAnt = class',
  14873. ' public',
  14874. ' const e = high(TEnum);',
  14875. ' const f = high(TBird.TSubEnum);',
  14876. ' end;',
  14877. 'begin',
  14878. '']);
  14879. ConvertProgram;
  14880. CheckSource('TestClass_ConstEnum',
  14881. LinesToStr([
  14882. 'this.TEnum = {',
  14883. ' "0": "red",',
  14884. ' red: 0,',
  14885. ' "1": "blue",',
  14886. ' blue: 1',
  14887. '};',
  14888. 'rtl.createClass(this, "TObject", null, function () {',
  14889. ' this.$init = function () {',
  14890. ' };',
  14891. ' this.$final = function () {',
  14892. ' };',
  14893. '});',
  14894. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14895. ' this.TSubEnum = {',
  14896. ' "0": "light",',
  14897. ' light: 0,',
  14898. ' "1": "dark",',
  14899. ' dark: 1',
  14900. ' };',
  14901. ' this.a = $mod.TEnum.blue;',
  14902. ' this.b = this.TSubEnum.dark;',
  14903. '});',
  14904. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14905. ' this.c = $mod.TEnum.blue;',
  14906. ' this.d = this.TSubEnum.dark;',
  14907. '});',
  14908. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14909. ' this.e = $mod.TEnum.blue;',
  14910. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14911. '});',
  14912. '']),
  14913. LinesToStr([
  14914. '']));
  14915. end;
  14916. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14917. begin
  14918. StartProgram(false);
  14919. Add([
  14920. 'type',
  14921. ' TObject = class',
  14922. ' const cI: longint = 3;',
  14923. ' procedure Fly;',
  14924. ' procedure Run;',
  14925. ' end;',
  14926. ' TBird = class',
  14927. ' procedure Go;',
  14928. ' end;',
  14929. 'procedure tobject.fly;',
  14930. 'const cI: word = 4;',
  14931. 'begin',
  14932. ' if cI=Self.cI then ;',
  14933. 'end;',
  14934. 'procedure tobject.run;',
  14935. 'const cI: word = 5;',
  14936. 'begin',
  14937. ' if cI=Self.cI then ;',
  14938. 'end;',
  14939. 'procedure tbird.go;',
  14940. 'const cI: word = 6;',
  14941. 'begin',
  14942. ' if cI=Self.cI then ;',
  14943. 'end;',
  14944. 'begin',
  14945. '']);
  14946. ConvertProgram;
  14947. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14948. LinesToStr([
  14949. 'rtl.createClass(this, "TObject", null, function () {',
  14950. ' this.cI = 3;',
  14951. ' this.$init = function () {',
  14952. ' };',
  14953. ' this.$final = function () {',
  14954. ' };',
  14955. ' var cI$1 = 4;',
  14956. ' this.Fly = function () {',
  14957. ' if (cI$1 === this.cI) ;',
  14958. ' };',
  14959. ' var cI$2 = 5;',
  14960. ' this.Run = function () {',
  14961. ' if (cI$2 === this.cI) ;',
  14962. ' };',
  14963. '});',
  14964. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14965. ' var cI$3 = 6;',
  14966. ' this.Go = function () {',
  14967. ' if (cI$3 === this.cI) ;',
  14968. ' };',
  14969. '});',
  14970. '']),
  14971. LinesToStr([
  14972. '']));
  14973. end;
  14974. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14975. begin
  14976. StartUnit(false);
  14977. Add([
  14978. 'interface',
  14979. 'type',
  14980. ' TObject = class',
  14981. ' const cI: longint = 3;',
  14982. ' procedure Fly;',
  14983. ' procedure Run;',
  14984. ' end;',
  14985. ' TBird = class',
  14986. ' procedure Go;',
  14987. ' end;',
  14988. 'implementation',
  14989. 'procedure tobject.fly;',
  14990. 'const cI: word = 4;',
  14991. 'begin',
  14992. ' if cI=Self.cI then ;',
  14993. 'end;',
  14994. 'procedure tobject.run;',
  14995. 'const cI: word = 5;',
  14996. 'begin',
  14997. ' if cI=Self.cI then ;',
  14998. 'end;',
  14999. 'procedure tbird.go;',
  15000. 'const cI: word = 6;',
  15001. 'begin',
  15002. ' if cI=Self.cI then ;',
  15003. 'end;',
  15004. '']);
  15005. ConvertUnit;
  15006. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15007. LinesToStr([
  15008. 'rtl.createClass(this, "TObject", null, function () {',
  15009. ' this.cI = 3;',
  15010. ' this.$init = function () {',
  15011. ' };',
  15012. ' this.$final = function () {',
  15013. ' };',
  15014. ' var cI$1 = 4;',
  15015. ' this.Fly = function () {',
  15016. ' if (cI$1 === this.cI) ;',
  15017. ' };',
  15018. ' var cI$2 = 5;',
  15019. ' this.Run = function () {',
  15020. ' if (cI$2 === this.cI) ;',
  15021. ' };',
  15022. '});',
  15023. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15024. ' var cI$3 = 6;',
  15025. ' this.Go = function () {',
  15026. ' if (cI$3 === this.cI) ;',
  15027. ' };',
  15028. '});',
  15029. '']),
  15030. '',
  15031. '');
  15032. end;
  15033. procedure TTestModule.TestClass_LocalVarSelfFail;
  15034. begin
  15035. StartProgram(false);
  15036. Add([
  15037. 'type',
  15038. ' TObject = class',
  15039. ' constructor Create;',
  15040. ' end;',
  15041. 'constructor tobject.create;',
  15042. 'var self: longint;',
  15043. 'begin',
  15044. 'end',
  15045. 'begin',
  15046. '']);
  15047. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15048. ConvertProgram;
  15049. end;
  15050. procedure TTestModule.TestClass_ArgSelfFail;
  15051. begin
  15052. StartProgram(false);
  15053. Add([
  15054. 'type',
  15055. ' TObject = class',
  15056. ' procedure DoIt(Self: longint);',
  15057. ' end;',
  15058. 'procedure tobject.doit(self: longint);',
  15059. 'begin',
  15060. 'end',
  15061. 'begin',
  15062. '']);
  15063. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15064. ConvertProgram;
  15065. end;
  15066. procedure TTestModule.TestClass_NestedProcSelf;
  15067. begin
  15068. StartProgram(false);
  15069. Add([
  15070. 'type',
  15071. ' TObject = class',
  15072. ' Key: longint;',
  15073. ' class var State: longint;',
  15074. ' procedure DoIt;',
  15075. ' function GetSize: longint; virtual; abstract;',
  15076. ' procedure SetSize(Value: longint); virtual; abstract;',
  15077. ' property Size: longint read GetSize write SetSize;',
  15078. ' end;',
  15079. 'procedure tobject.doit;',
  15080. ' procedure Sub;',
  15081. ' begin',
  15082. ' key:=key+2;',
  15083. ' self.key:=self.key+3;',
  15084. ' state:=state+4;',
  15085. ' self.state:=self.state+5;',
  15086. ' tobject.state:=tobject.state+6;',
  15087. ' size:=size+7;',
  15088. ' self.size:=self.size+8;',
  15089. ' end;',
  15090. 'begin',
  15091. ' sub;',
  15092. ' key:=key+12;',
  15093. ' self.key:=self.key+13;',
  15094. ' state:=state+14;',
  15095. ' self.state:=self.state+15;',
  15096. ' tobject.state:=tobject.state+16;',
  15097. ' size:=size+17;',
  15098. ' self.size:=self.size+18;',
  15099. 'end;',
  15100. 'begin',
  15101. '']);
  15102. ConvertProgram;
  15103. CheckSource('TestClass_NestedProcSelf',
  15104. LinesToStr([ // statements
  15105. 'rtl.createClass(this, "TObject", null, function () {',
  15106. ' this.State = 0;',
  15107. ' this.$init = function () {',
  15108. ' this.Key = 0;',
  15109. ' };',
  15110. ' this.$final = function () {',
  15111. ' };',
  15112. ' this.DoIt = function () {',
  15113. ' var $Self = this;',
  15114. ' function Sub() {',
  15115. ' $Self.Key = $Self.Key + 2;',
  15116. ' $Self.Key = $Self.Key + 3;',
  15117. ' $mod.TObject.State = $Self.State + 4;',
  15118. ' $mod.TObject.State = $Self.State + 5;',
  15119. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15120. ' $Self.SetSize($Self.GetSize() + 7);',
  15121. ' $Self.SetSize($Self.GetSize() + 8);',
  15122. ' };',
  15123. ' Sub();',
  15124. ' this.Key = this.Key + 12;',
  15125. ' $Self.Key = $Self.Key + 13;',
  15126. ' $mod.TObject.State = this.State + 14;',
  15127. ' $mod.TObject.State = $Self.State + 15;',
  15128. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15129. ' this.SetSize(this.GetSize() + 17);',
  15130. ' $Self.SetSize($Self.GetSize() + 18);',
  15131. ' };',
  15132. '});',
  15133. '']),
  15134. LinesToStr([ // $mod.$main
  15135. '']));
  15136. end;
  15137. procedure TTestModule.TestClass_NestedProcSelf2;
  15138. begin
  15139. StartProgram(false);
  15140. Add([
  15141. 'type',
  15142. ' TObject = class',
  15143. ' Key: longint;',
  15144. ' class var State: longint;',
  15145. ' function GetSize: longint; virtual; abstract;',
  15146. ' procedure SetSize(Value: longint); virtual; abstract;',
  15147. ' property Size: longint read GetSize write SetSize;',
  15148. ' end;',
  15149. ' TBird = class',
  15150. ' procedure DoIt;',
  15151. ' end;',
  15152. 'procedure tbird.doit;',
  15153. ' procedure Sub;',
  15154. ' begin',
  15155. ' key:=key+2;',
  15156. ' self.key:=self.key+3;',
  15157. ' state:=state+4;',
  15158. ' self.state:=self.state+5;',
  15159. ' tobject.state:=tobject.state+6;',
  15160. ' size:=size+7;',
  15161. ' self.size:=self.size+8;',
  15162. ' end;',
  15163. 'begin',
  15164. ' sub;',
  15165. ' key:=key+12;',
  15166. ' self.key:=self.key+13;',
  15167. ' state:=state+14;',
  15168. ' self.state:=self.state+15;',
  15169. ' tobject.state:=tobject.state+16;',
  15170. ' size:=size+17;',
  15171. ' self.size:=self.size+18;',
  15172. 'end;',
  15173. 'begin',
  15174. '']);
  15175. ConvertProgram;
  15176. CheckSource('TestClass_NestedProcSelf2',
  15177. LinesToStr([ // statements
  15178. 'rtl.createClass(this, "TObject", null, function () {',
  15179. ' this.State = 0;',
  15180. ' this.$init = function () {',
  15181. ' this.Key = 0;',
  15182. ' };',
  15183. ' this.$final = function () {',
  15184. ' };',
  15185. '});',
  15186. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15187. ' this.DoIt = function () {',
  15188. ' var $Self = this;',
  15189. ' function Sub() {',
  15190. ' $Self.Key = $Self.Key + 2;',
  15191. ' $Self.Key = $Self.Key + 3;',
  15192. ' $mod.TObject.State = $Self.State + 4;',
  15193. ' $mod.TObject.State = $Self.State + 5;',
  15194. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15195. ' $Self.SetSize($Self.GetSize() + 7);',
  15196. ' $Self.SetSize($Self.GetSize() + 8);',
  15197. ' };',
  15198. ' Sub();',
  15199. ' this.Key = this.Key + 12;',
  15200. ' $Self.Key = $Self.Key + 13;',
  15201. ' $mod.TObject.State = this.State + 14;',
  15202. ' $mod.TObject.State = $Self.State + 15;',
  15203. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15204. ' this.SetSize(this.GetSize() + 17);',
  15205. ' $Self.SetSize($Self.GetSize() + 18);',
  15206. ' };',
  15207. '});',
  15208. '']),
  15209. LinesToStr([ // $mod.$main
  15210. '']));
  15211. end;
  15212. procedure TTestModule.TestClass_NestedProcClassSelf;
  15213. begin
  15214. StartProgram(false);
  15215. Add([
  15216. 'type',
  15217. ' TObject = class',
  15218. ' class var State: longint;',
  15219. ' class procedure DoIt;',
  15220. ' class function GetSize: longint; virtual; abstract;',
  15221. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15222. ' class property Size: longint read GetSize write SetSize;',
  15223. ' end;',
  15224. 'class procedure tobject.doit;',
  15225. ' procedure Sub;',
  15226. ' begin',
  15227. ' state:=state+2;',
  15228. ' self.state:=self.state+3;',
  15229. ' tobject.state:=tobject.state+4;',
  15230. ' size:=size+5;',
  15231. ' self.size:=self.size+6;',
  15232. ' tobject.size:=tobject.size+7;',
  15233. ' end;',
  15234. 'begin',
  15235. ' sub;',
  15236. ' state:=state+12;',
  15237. ' self.state:=self.state+13;',
  15238. ' tobject.state:=tobject.state+14;',
  15239. ' size:=size+15;',
  15240. ' self.size:=self.size+16;',
  15241. ' tobject.size:=tobject.size+17;',
  15242. 'end;',
  15243. 'begin',
  15244. '']);
  15245. ConvertProgram;
  15246. CheckSource('TestClass_NestedProcClassSelf',
  15247. LinesToStr([ // statements
  15248. 'rtl.createClass(this, "TObject", null, function () {',
  15249. ' this.State = 0;',
  15250. ' this.$init = function () {',
  15251. ' };',
  15252. ' this.$final = function () {',
  15253. ' };',
  15254. ' this.DoIt = function () {',
  15255. ' var $Self = this;',
  15256. ' function Sub() {',
  15257. ' $mod.TObject.State = $Self.State + 2;',
  15258. ' $mod.TObject.State = $Self.State + 3;',
  15259. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15260. ' $Self.SetSize($Self.GetSize() + 5);',
  15261. ' $Self.SetSize($Self.GetSize() + 6);',
  15262. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15263. ' };',
  15264. ' Sub();',
  15265. ' $mod.TObject.State = this.State + 12;',
  15266. ' $mod.TObject.State = $Self.State + 13;',
  15267. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15268. ' this.SetSize(this.GetSize() + 15);',
  15269. ' $Self.SetSize($Self.GetSize() + 16);',
  15270. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15271. ' };',
  15272. '});',
  15273. '']),
  15274. LinesToStr([ // $mod.$main
  15275. '']));
  15276. end;
  15277. procedure TTestModule.TestClass_NestedProcCallInherited;
  15278. begin
  15279. StartProgram(false);
  15280. Add([
  15281. 'type',
  15282. ' TObject = class',
  15283. ' function DoIt(k: boolean): longint; virtual;',
  15284. ' end;',
  15285. ' TBird = class',
  15286. ' function DoIt(k: boolean): longint; override;',
  15287. ' end;',
  15288. 'function tobject.doit(k: boolean): longint;',
  15289. 'begin',
  15290. 'end;',
  15291. 'function tbird.doit(k: boolean): longint;',
  15292. ' procedure Sub;',
  15293. ' begin',
  15294. ' inherited DoIt(true);',
  15295. //' if inherited DoIt(false)=4 then ;',
  15296. ' end;',
  15297. 'begin',
  15298. ' Sub;',
  15299. ' inherited;',
  15300. ' inherited DoIt(true);',
  15301. //' if inherited DoIt(false)=14 then ;',
  15302. 'end;',
  15303. 'begin',
  15304. '']);
  15305. ConvertProgram;
  15306. CheckSource('TestClass_NestedProcCallInherited',
  15307. LinesToStr([ // statements
  15308. 'rtl.createClass(this, "TObject", null, function () {',
  15309. ' this.$init = function () {',
  15310. ' };',
  15311. ' this.$final = function () {',
  15312. ' };',
  15313. ' this.DoIt = function (k) {',
  15314. ' var Result = 0;',
  15315. ' return Result;',
  15316. ' };',
  15317. '});',
  15318. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15319. ' this.DoIt = function (k) {',
  15320. ' var $Self = this;',
  15321. ' var Result = 0;',
  15322. ' function Sub() {',
  15323. ' $mod.TObject.DoIt.call($Self, true);',
  15324. ' };',
  15325. ' Sub();',
  15326. ' $mod.TObject.DoIt.apply(this, arguments);',
  15327. ' $mod.TObject.DoIt.call(this, true);',
  15328. ' return Result;',
  15329. ' };',
  15330. '});',
  15331. '']),
  15332. LinesToStr([ // $mod.$main
  15333. '']));
  15334. end;
  15335. procedure TTestModule.TestClass_TObjectFree;
  15336. begin
  15337. StartProgram(false);
  15338. Add([
  15339. 'type',
  15340. ' TObject = class',
  15341. ' Obj: tobject;',
  15342. ' procedure Free;',
  15343. ' procedure Release;',
  15344. ' end;',
  15345. 'procedure tobject.free;',
  15346. 'begin',
  15347. 'end;',
  15348. 'procedure tobject.release;',
  15349. 'begin',
  15350. ' free;',
  15351. ' if true then free;',
  15352. 'end;',
  15353. 'function DoIt(o: tobject): tobject;',
  15354. 'var l: tobject;',
  15355. 'begin',
  15356. ' o.free;',
  15357. ' o.free();',
  15358. ' l.free;',
  15359. ' l.free();',
  15360. ' o.obj.free;',
  15361. ' o.obj.free();',
  15362. ' with o do obj.free;',
  15363. ' with o do obj.free();',
  15364. ' result.Free;',
  15365. ' result.Free();',
  15366. 'end;',
  15367. 'var o: tobject;',
  15368. ' a: array of tobject;',
  15369. 'begin',
  15370. ' o.free;',
  15371. ' o.obj.free;',
  15372. ' a[1+2].free;',
  15373. '']);
  15374. ConvertProgram;
  15375. CheckSource('TestClass_TObjectFree',
  15376. LinesToStr([ // statements
  15377. 'rtl.createClass(this, "TObject", null, function () {',
  15378. ' this.$init = function () {',
  15379. ' this.Obj = null;',
  15380. ' };',
  15381. ' this.$final = function () {',
  15382. ' this.Obj = undefined;',
  15383. ' };',
  15384. ' this.Free = function () {',
  15385. ' };',
  15386. ' this.Release = function () {',
  15387. ' this.Free();',
  15388. ' if (true) this.Free();',
  15389. ' };',
  15390. '});',
  15391. 'this.DoIt = function (o) {',
  15392. ' var Result = null;',
  15393. ' var l = null;',
  15394. ' o = rtl.freeLoc(o);',
  15395. ' o = rtl.freeLoc(o);',
  15396. ' l = rtl.freeLoc(l);',
  15397. ' l = rtl.freeLoc(l);',
  15398. ' rtl.free(o, "Obj");',
  15399. ' rtl.free(o, "Obj");',
  15400. ' rtl.free(o, "Obj");',
  15401. ' rtl.free(o, "Obj");',
  15402. ' Result = rtl.freeLoc(Result);',
  15403. ' Result = rtl.freeLoc(Result);',
  15404. ' return Result;',
  15405. '};',
  15406. 'this.o = null;',
  15407. 'this.a = [];',
  15408. '']),
  15409. LinesToStr([ // $mod.$main
  15410. 'rtl.free($mod, "o");',
  15411. 'rtl.free($mod.o, "Obj");',
  15412. 'rtl.free($mod.a, 1 + 2);',
  15413. '']));
  15414. end;
  15415. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15416. begin
  15417. StartProgram(false);
  15418. Add([
  15419. 'type',
  15420. ' TObject = class',
  15421. ' Obj: tobject;',
  15422. ' procedure Free;',
  15423. ' end;',
  15424. 'procedure tobject.free;',
  15425. 'begin',
  15426. 'end;',
  15427. 'procedure DoIt(var o: tobject);',
  15428. 'begin',
  15429. ' o.free;',
  15430. ' o.free();',
  15431. 'end;',
  15432. 'begin',
  15433. '']);
  15434. ConvertProgram;
  15435. CheckSource('TestClass_TObjectFree_VarArg',
  15436. LinesToStr([ // statements
  15437. 'rtl.createClass(this, "TObject", null, function () {',
  15438. ' this.$init = function () {',
  15439. ' this.Obj = null;',
  15440. ' };',
  15441. ' this.$final = function () {',
  15442. ' this.Obj = undefined;',
  15443. ' };',
  15444. ' this.Free = function () {',
  15445. ' };',
  15446. '});',
  15447. 'this.DoIt = function (o) {',
  15448. ' o.set(rtl.freeLoc(o.get()));',
  15449. ' o.set(rtl.freeLoc(o.get()));',
  15450. '};',
  15451. '']),
  15452. LinesToStr([ // $mod.$main
  15453. '']));
  15454. end;
  15455. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15456. begin
  15457. StartProgram(false);
  15458. Add([
  15459. 'type',
  15460. ' TObject = class',
  15461. ' constructor Create;',
  15462. ' procedure Free;',
  15463. ' end;',
  15464. 'constructor TObject.Create; begin end;',
  15465. 'procedure tobject.free; begin end;',
  15466. 'begin',
  15467. ' with tobject.create do free;',
  15468. '']);
  15469. ConvertProgram;
  15470. CheckSource('TestClass_TObjectFreeNewInstance',
  15471. LinesToStr([ // statements
  15472. 'rtl.createClass(this, "TObject", null, function () {',
  15473. ' this.$init = function () {',
  15474. ' };',
  15475. ' this.$final = function () {',
  15476. ' };',
  15477. ' this.Create = function () {',
  15478. ' return this;',
  15479. ' };',
  15480. ' this.Free = function () {',
  15481. ' };',
  15482. '});',
  15483. '']),
  15484. LinesToStr([ // $mod.$main
  15485. 'var $with = $mod.TObject.$create("Create");',
  15486. '$with=rtl.freeLoc($with);',
  15487. '']));
  15488. end;
  15489. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15490. begin
  15491. StartProgram(false);
  15492. Add([
  15493. 'type',
  15494. ' TObject = class',
  15495. ' destructor Destroy;',
  15496. ' procedure Free;',
  15497. ' end;',
  15498. 'destructor TObject.Destroy; begin end;',
  15499. 'procedure tobject.free; begin end;',
  15500. 'var o: tobject;',
  15501. 'begin',
  15502. ' o.free;',
  15503. '']);
  15504. Converter.UseLowerCase:=true;
  15505. ConvertProgram;
  15506. CheckSource('TestClass_TObjectFreeLowerCase',
  15507. LinesToStr([ // statements
  15508. 'rtl.createClass(this, "tobject", null, function () {',
  15509. ' this.$init = function () {',
  15510. ' };',
  15511. ' this.$final = function () {',
  15512. ' };',
  15513. ' rtl.tObjectDestroy = "destroy";',
  15514. ' this.destroy = function () {',
  15515. ' };',
  15516. ' this.free = function () {',
  15517. ' };',
  15518. '});',
  15519. 'this.o = null;',
  15520. '']),
  15521. LinesToStr([ // $mod.$main
  15522. 'rtl.free($mod, "o");',
  15523. '']));
  15524. end;
  15525. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15526. begin
  15527. StartProgram(false);
  15528. Add([
  15529. 'type',
  15530. ' TObject = class',
  15531. ' procedure Free;',
  15532. ' function GetObj: tobject; virtual; abstract;',
  15533. ' end;',
  15534. 'procedure tobject.free;',
  15535. 'begin',
  15536. 'end;',
  15537. 'var o: tobject;',
  15538. 'begin',
  15539. ' o.getobj.free;',
  15540. '']);
  15541. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15542. ConvertProgram;
  15543. end;
  15544. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15545. begin
  15546. StartProgram(false);
  15547. Add([
  15548. 'type',
  15549. ' TObject = class',
  15550. ' procedure Free;',
  15551. ' FObj: TObject;',
  15552. ' property Obj: tobject read FObj write FObj;',
  15553. ' end;',
  15554. 'procedure tobject.free;',
  15555. 'begin',
  15556. 'end;',
  15557. 'var o: tobject;',
  15558. 'begin',
  15559. ' o.obj.free;',
  15560. '']);
  15561. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15562. ConvertProgram;
  15563. end;
  15564. procedure TTestModule.TestClass_ForIn;
  15565. begin
  15566. StartProgram(false);
  15567. Add([
  15568. 'type',
  15569. ' TObject = class end;',
  15570. ' TItem = TObject;',
  15571. ' TEnumerator = class',
  15572. ' FCurrent: TItem;',
  15573. ' property Current: TItem read FCurrent;',
  15574. ' function MoveNext: boolean;',
  15575. ' end;',
  15576. ' TBird = class',
  15577. ' function GetEnumerator: TEnumerator;',
  15578. ' end;',
  15579. 'function TEnumerator.MoveNext: boolean;',
  15580. 'begin',
  15581. 'end;',
  15582. 'function TBird.GetEnumerator: TEnumerator;',
  15583. 'begin',
  15584. 'end;',
  15585. 'var',
  15586. ' b: TBird;',
  15587. ' i, i2: TItem;',
  15588. 'begin',
  15589. ' for i in b do i2:=i;']);
  15590. ConvertProgram;
  15591. CheckSource('TestClass_ForIn',
  15592. LinesToStr([ // statements
  15593. 'rtl.createClass(this, "TObject", null, function () {',
  15594. ' this.$init = function () {',
  15595. ' };',
  15596. ' this.$final = function () {',
  15597. ' };',
  15598. '});',
  15599. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15600. ' this.$init = function () {',
  15601. ' $mod.TObject.$init.call(this);',
  15602. ' this.FCurrent = null;',
  15603. ' };',
  15604. ' this.$final = function () {',
  15605. ' this.FCurrent = undefined;',
  15606. ' $mod.TObject.$final.call(this);',
  15607. ' };',
  15608. ' this.MoveNext = function () {',
  15609. ' var Result = false;',
  15610. ' return Result;',
  15611. ' };',
  15612. '});',
  15613. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15614. ' this.GetEnumerator = function () {',
  15615. ' var Result = null;',
  15616. ' return Result;',
  15617. ' };',
  15618. '});',
  15619. 'this.b = null;',
  15620. 'this.i = null;',
  15621. 'this.i2 = null;'
  15622. ]),
  15623. LinesToStr([ // $mod.$main
  15624. 'var $in = $mod.b.GetEnumerator();',
  15625. 'try {',
  15626. ' while ($in.MoveNext()){',
  15627. ' $mod.i = $in.FCurrent;',
  15628. ' $mod.i2 = $mod.i;',
  15629. ' }',
  15630. '} finally {',
  15631. ' $in = rtl.freeLoc($in)',
  15632. '};',
  15633. '']));
  15634. end;
  15635. procedure TTestModule.TestClass_DispatchMessage;
  15636. begin
  15637. StartProgram(false);
  15638. Add([
  15639. 'type',
  15640. ' TObject = class',
  15641. ' {$DispatchField DispInt}',
  15642. ' procedure Dispatch(var Msg); virtual; abstract;',
  15643. ' {$DispatchStrField DispStr}',
  15644. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15645. ' end;',
  15646. ' THopMsg = record',
  15647. ' DispInt: longint;',
  15648. ' end;',
  15649. ' TPutMsg = record',
  15650. ' DispStr: string;',
  15651. ' end;',
  15652. ' TBird = class',
  15653. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15654. ' procedure Run; overload; virtual; abstract;',
  15655. ' procedure Run(var Msg); overload; message ''Fast'';',
  15656. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15657. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15658. ' end;',
  15659. 'procedure TBird.Run(var Msg);',
  15660. 'begin',
  15661. 'end;',
  15662. 'begin',
  15663. '']);
  15664. ConvertProgram;
  15665. CheckSource('TestClass_Message',
  15666. LinesToStr([ // statements
  15667. 'rtl.createClass(this, "TObject", null, function () {',
  15668. ' this.$init = function () {',
  15669. ' };',
  15670. ' this.$final = function () {',
  15671. ' };',
  15672. '});',
  15673. 'rtl.recNewT(this, "THopMsg", function () {',
  15674. ' this.DispInt = 0;',
  15675. ' this.$eq = function (b) {',
  15676. ' return this.DispInt === b.DispInt;',
  15677. ' };',
  15678. ' this.$assign = function (s) {',
  15679. ' this.DispInt = s.DispInt;',
  15680. ' return this;',
  15681. ' };',
  15682. '});',
  15683. 'rtl.recNewT(this, "TPutMsg", function () {',
  15684. ' this.DispStr = "";',
  15685. ' this.$eq = function (b) {',
  15686. ' return this.DispStr === b.DispStr;',
  15687. ' };',
  15688. ' this.$assign = function (s) {',
  15689. ' this.DispStr = s.DispStr;',
  15690. ' return this;',
  15691. ' };',
  15692. '});',
  15693. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15694. ' this.Run$1 = function (Msg) {',
  15695. ' };',
  15696. ' this.$msgint = {',
  15697. ' "2": "Fly",',
  15698. ' "3": "Hop"',
  15699. ' };',
  15700. ' this.$msgstr = {',
  15701. ' Fast: "Run$1",',
  15702. ' foo: "Put"',
  15703. ' };',
  15704. '});',
  15705. '']),
  15706. LinesToStr([ // $mod.$main
  15707. '']));
  15708. end;
  15709. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15710. begin
  15711. StartProgram(false);
  15712. Add([
  15713. 'type',
  15714. ' TObject = class',
  15715. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15716. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15717. ' end;',
  15718. 'begin',
  15719. '']);
  15720. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15721. ConvertProgram;
  15722. end;
  15723. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15724. begin
  15725. StartProgram(false);
  15726. Add([
  15727. 'type',
  15728. ' TObject = class',
  15729. ' {$dispatchfield Msg}',
  15730. ' procedure Dispatch(var Msg); virtual; abstract;',
  15731. ' end;',
  15732. ' TFlyMsg = record',
  15733. ' FlyId: longint;',
  15734. ' end;',
  15735. ' TBird = class',
  15736. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15737. ' end;',
  15738. 'begin',
  15739. '']);
  15740. ConvertProgram;
  15741. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15742. end;
  15743. procedure TTestModule.TestClassOf_Create;
  15744. begin
  15745. StartProgram(false);
  15746. Add('type');
  15747. Add(' TObject = class');
  15748. Add(' constructor Create;');
  15749. Add(' end;');
  15750. Add(' TClass = class of TObject;');
  15751. Add('constructor tobject.create; begin end;');
  15752. Add('var');
  15753. Add(' Obj: tobject;');
  15754. Add(' C: tclass;');
  15755. Add('begin');
  15756. Add(' obj:=C.create;');
  15757. Add(' with c do obj:=create;');
  15758. ConvertProgram;
  15759. CheckSource('TestClassOf_Create',
  15760. LinesToStr([ // statements
  15761. 'rtl.createClass(this, "TObject", null, function () {',
  15762. ' this.$init = function () {',
  15763. ' };',
  15764. ' this.$final = function () {',
  15765. ' };',
  15766. ' this.Create = function () {',
  15767. ' return this;',
  15768. ' };',
  15769. '});',
  15770. 'this.Obj = null;',
  15771. 'this.C = null;'
  15772. ]),
  15773. LinesToStr([ // $mod.$main
  15774. '$mod.Obj = $mod.C.$create("Create");',
  15775. 'var $with = $mod.C;',
  15776. '$mod.Obj = $with.$create("Create");',
  15777. '']));
  15778. end;
  15779. procedure TTestModule.TestClassOf_Call;
  15780. begin
  15781. StartProgram(false);
  15782. Add('type');
  15783. Add(' TObject = class');
  15784. Add(' class procedure DoIt;');
  15785. Add(' end;');
  15786. Add(' TClass = class of TObject;');
  15787. Add('class procedure tobject.doit; begin end;');
  15788. Add('var');
  15789. Add(' C: tclass;');
  15790. Add('begin');
  15791. Add(' c.doit;');
  15792. Add(' with c do doit;');
  15793. ConvertProgram;
  15794. CheckSource('TestClassOf_Call',
  15795. LinesToStr([ // statements
  15796. 'rtl.createClass(this, "TObject", null, function () {',
  15797. ' this.$init = function () {',
  15798. ' };',
  15799. ' this.$final = function () {',
  15800. ' };',
  15801. ' this.DoIt = function () {',
  15802. ' };',
  15803. '});',
  15804. 'this.C = null;'
  15805. ]),
  15806. LinesToStr([ // $mod.$main
  15807. '$mod.C.DoIt();',
  15808. 'var $with = $mod.C;',
  15809. '$with.DoIt();',
  15810. '']));
  15811. end;
  15812. procedure TTestModule.TestClassOf_Assign;
  15813. begin
  15814. StartProgram(false);
  15815. Add('type');
  15816. Add(' TClass = class of TObject;');
  15817. Add(' TObject = class');
  15818. Add(' ClassType: TClass; ');
  15819. Add(' end;');
  15820. Add('var');
  15821. Add(' Obj: tobject;');
  15822. Add(' C: tclass;');
  15823. Add('begin');
  15824. Add(' c:=nil;');
  15825. Add(' c:=obj.classtype;');
  15826. ConvertProgram;
  15827. CheckSource('TestClassOf_Assign',
  15828. LinesToStr([ // statements
  15829. 'rtl.createClass(this, "TObject", null, function () {',
  15830. ' this.$init = function () {',
  15831. ' this.ClassType = null;',
  15832. ' };',
  15833. ' this.$final = function () {',
  15834. ' this.ClassType = undefined;',
  15835. ' };',
  15836. '});',
  15837. 'this.Obj = null;',
  15838. 'this.C = null;'
  15839. ]),
  15840. LinesToStr([ // $mod.$main
  15841. '$mod.C = null;',
  15842. '$mod.C = $mod.Obj.ClassType;',
  15843. '']));
  15844. end;
  15845. procedure TTestModule.TestClassOf_Is;
  15846. begin
  15847. StartProgram(false);
  15848. Add('type');
  15849. Add(' TClass = class of TObject;');
  15850. Add(' TObject = class');
  15851. Add(' end;');
  15852. Add(' TCar = class');
  15853. Add(' end;');
  15854. Add(' TCars = class of TCar;');
  15855. Add('var');
  15856. Add(' Obj: tobject;');
  15857. Add(' C: tclass;');
  15858. Add(' Cars: tcars;');
  15859. Add('begin');
  15860. Add(' if c is tcar then ;');
  15861. Add(' if c is tcars then ;');
  15862. ConvertProgram;
  15863. CheckSource('TestClassOf_Is',
  15864. LinesToStr([ // statements
  15865. 'rtl.createClass(this, "TObject", null, function () {',
  15866. ' this.$init = function () {',
  15867. ' };',
  15868. ' this.$final = function () {',
  15869. ' };',
  15870. '});',
  15871. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15872. '});',
  15873. 'this.Obj = null;',
  15874. 'this.C = null;',
  15875. 'this.Cars = null;'
  15876. ]),
  15877. LinesToStr([ // $mod.$main
  15878. 'if(rtl.is($mod.C,$mod.TCar));',
  15879. 'if(rtl.is($mod.C,$mod.TCar));',
  15880. '']));
  15881. end;
  15882. procedure TTestModule.TestClassOf_Compare;
  15883. begin
  15884. StartProgram(false);
  15885. Add('type');
  15886. Add(' TClass = class of TObject;');
  15887. Add(' TObject = class');
  15888. Add(' ClassType: TClass; ');
  15889. Add(' end;');
  15890. Add('var');
  15891. Add(' b: boolean;');
  15892. Add(' Obj: tobject;');
  15893. Add(' C: tclass;');
  15894. Add('begin');
  15895. Add(' b:=c=nil;');
  15896. Add(' b:=nil=c;');
  15897. Add(' b:=c=obj.classtype;');
  15898. Add(' b:=obj.classtype=c;');
  15899. Add(' b:=c=TObject;');
  15900. Add(' b:=TObject=c;');
  15901. Add(' b:=c<>nil;');
  15902. Add(' b:=nil<>c;');
  15903. Add(' b:=c<>obj.classtype;');
  15904. Add(' b:=obj.classtype<>c;');
  15905. Add(' b:=c<>TObject;');
  15906. Add(' b:=TObject<>c;');
  15907. ConvertProgram;
  15908. CheckSource('TestClassOf_Compare',
  15909. LinesToStr([ // statements
  15910. 'rtl.createClass(this, "TObject", null, function () {',
  15911. ' this.$init = function () {',
  15912. ' this.ClassType = null;',
  15913. ' };',
  15914. ' this.$final = function () {',
  15915. ' this.ClassType = undefined;',
  15916. ' };',
  15917. '});',
  15918. 'this.b = false;',
  15919. 'this.Obj = null;',
  15920. 'this.C = null;'
  15921. ]),
  15922. LinesToStr([ // $mod.$main
  15923. '$mod.b = $mod.C === null;',
  15924. '$mod.b = null === $mod.C;',
  15925. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15926. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15927. '$mod.b = $mod.C === $mod.TObject;',
  15928. '$mod.b = $mod.TObject === $mod.C;',
  15929. '$mod.b = $mod.C !== null;',
  15930. '$mod.b = null !== $mod.C;',
  15931. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15932. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15933. '$mod.b = $mod.C !== $mod.TObject;',
  15934. '$mod.b = $mod.TObject !== $mod.C;',
  15935. '']));
  15936. end;
  15937. procedure TTestModule.TestClassOf_ClassVar;
  15938. begin
  15939. StartProgram(false);
  15940. Add('type');
  15941. Add(' TObject = class');
  15942. Add(' class var id: longint;');
  15943. Add(' end;');
  15944. Add(' TClass = class of TObject;');
  15945. Add('var');
  15946. Add(' C: tclass;');
  15947. Add('begin');
  15948. Add(' C.id:=C.id;');
  15949. ConvertProgram;
  15950. CheckSource('TestClassOf_ClassVar',
  15951. LinesToStr([ // statements
  15952. 'rtl.createClass(this, "TObject", null, function () {',
  15953. ' this.id = 0;',
  15954. ' this.$init = function () {',
  15955. ' };',
  15956. ' this.$final = function () {',
  15957. ' };',
  15958. '});',
  15959. 'this.C = null;'
  15960. ]),
  15961. LinesToStr([ // $mod.$main
  15962. '$mod.TObject.id = $mod.C.id;',
  15963. '']));
  15964. end;
  15965. procedure TTestModule.TestClassOf_ClassMethod;
  15966. begin
  15967. StartProgram(false);
  15968. Add('type');
  15969. Add(' TObject = class');
  15970. Add(' class function DoIt(i: longint = 0): longint;');
  15971. Add(' end;');
  15972. Add(' TClass = class of TObject;');
  15973. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15974. Add('var');
  15975. Add(' i: longint;');
  15976. Add(' C: tclass;');
  15977. Add('begin');
  15978. Add(' C.DoIt;');
  15979. Add(' C.DoIt();');
  15980. Add(' i:=C.DoIt;');
  15981. Add(' i:=C.DoIt();');
  15982. ConvertProgram;
  15983. CheckSource('TestClassOf_ClassMethod',
  15984. LinesToStr([ // statements
  15985. 'rtl.createClass(this, "TObject", null, function () {',
  15986. ' this.$init = function () {',
  15987. ' };',
  15988. ' this.$final = function () {',
  15989. ' };',
  15990. ' this.DoIt = function (i) {',
  15991. ' var Result = 0;',
  15992. ' return Result;',
  15993. ' };',
  15994. '});',
  15995. 'this.i = 0;',
  15996. 'this.C = null;'
  15997. ]),
  15998. LinesToStr([ // $mod.$main
  15999. '$mod.C.DoIt(0);',
  16000. '$mod.C.DoIt(0);',
  16001. '$mod.i = $mod.C.DoIt(0);',
  16002. '$mod.i = $mod.C.DoIt(0);',
  16003. '']));
  16004. end;
  16005. procedure TTestModule.TestClassOf_ClassProperty;
  16006. begin
  16007. StartProgram(false);
  16008. Add([
  16009. 'type',
  16010. ' TObject = class',
  16011. ' class var FA: longint;',
  16012. ' class function GetA: longint;',
  16013. ' class procedure SetA(Value: longint);',
  16014. ' class property pA: longint read fa write fa;',
  16015. ' class property pB: longint read geta write seta;',
  16016. ' end;',
  16017. ' TObjectClass = class of tobject;',
  16018. 'class function tobject.geta: longint; begin end;',
  16019. 'class procedure tobject.seta(value: longint); begin end;',
  16020. 'var',
  16021. ' b: boolean;',
  16022. ' Obj: tobject;',
  16023. ' Cla: tobjectclass;',
  16024. 'begin',
  16025. ' obj.pa:=obj.pa;',
  16026. ' obj.pb:=obj.pb;',
  16027. ' b:=obj.pa=4;',
  16028. ' b:=obj.pb=obj.pb;',
  16029. ' b:=5=obj.pa;',
  16030. ' cla.pa:=6;',
  16031. ' cla.pa:=cla.pa;',
  16032. ' cla.pb:=cla.pb;',
  16033. ' b:=cla.pa=7;',
  16034. ' b:=cla.pb=cla.pb;',
  16035. ' b:=8=cla.pa;',
  16036. ' tobject.pa:=9;',
  16037. ' tobject.pb:=tobject.pb;',
  16038. ' b:=tobject.pa=10;',
  16039. ' b:=11=tobject.pa;',
  16040. '']);
  16041. ConvertProgram;
  16042. CheckSource('TestClassOf_ClassProperty',
  16043. LinesToStr([ // statements
  16044. 'rtl.createClass(this, "TObject", null, function () {',
  16045. ' this.FA = 0;',
  16046. ' this.$init = function () {',
  16047. ' };',
  16048. ' this.$final = function () {',
  16049. ' };',
  16050. ' this.GetA = function () {',
  16051. ' var Result = 0;',
  16052. ' return Result;',
  16053. ' };',
  16054. ' this.SetA = function (Value) {',
  16055. ' };',
  16056. '});',
  16057. 'this.b = false;',
  16058. 'this.Obj = null;',
  16059. 'this.Cla = null;'
  16060. ]),
  16061. LinesToStr([ // $mod.$main
  16062. '$mod.TObject.FA = $mod.Obj.FA;',
  16063. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16064. '$mod.b = $mod.Obj.FA === 4;',
  16065. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16066. '$mod.b = 5 === $mod.Obj.FA;',
  16067. '$mod.TObject.FA = 6;',
  16068. '$mod.TObject.FA = $mod.Cla.FA;',
  16069. '$mod.Cla.SetA($mod.Cla.GetA());',
  16070. '$mod.b = $mod.Cla.FA === 7;',
  16071. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16072. '$mod.b = 8 === $mod.Cla.FA;',
  16073. '$mod.TObject.FA = 9;',
  16074. '$mod.TObject.SetA($mod.TObject.GetA());',
  16075. '$mod.b = $mod.TObject.FA === 10;',
  16076. '$mod.b = 11 === $mod.TObject.FA;',
  16077. '']));
  16078. end;
  16079. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16080. begin
  16081. StartProgram(false);
  16082. Add('type');
  16083. Add(' TObject = class');
  16084. Add(' class var GlobalId: longint;');
  16085. Add(' class procedure ProcA;');
  16086. Add(' end;');
  16087. Add('class procedure tobject.proca;');
  16088. Add('var b: boolean;');
  16089. Add('begin');
  16090. Add(' b:=self=nil;');
  16091. Add(' b:=self.globalid=3;');
  16092. Add(' b:=4=self.globalid;');
  16093. Add(' self.globalid:=5;');
  16094. Add(' self.proca;');
  16095. Add('end;');
  16096. Add('begin');
  16097. ConvertProgram;
  16098. CheckSource('TestClassOf_ClassMethodSelf',
  16099. LinesToStr([ // statements
  16100. 'rtl.createClass(this, "TObject", null, function () {',
  16101. ' this.GlobalId = 0;',
  16102. ' this.$init = function () {',
  16103. ' };',
  16104. ' this.$final = function () {',
  16105. ' };',
  16106. ' this.ProcA = function () {',
  16107. ' var b = false;',
  16108. ' b = this === null;',
  16109. ' b = this.GlobalId === 3;',
  16110. ' b = 4 === this.GlobalId;',
  16111. ' $mod.TObject.GlobalId = 5;',
  16112. ' this.ProcA();',
  16113. ' };',
  16114. '});'
  16115. ]),
  16116. LinesToStr([ // $mod.$main
  16117. '']));
  16118. end;
  16119. procedure TTestModule.TestClassOf_TypeCast;
  16120. begin
  16121. StartProgram(false);
  16122. Add('type');
  16123. Add(' TObject = class');
  16124. Add(' class procedure {#TObject_DoIt}DoIt;');
  16125. Add(' end;');
  16126. Add(' TClass = class of TObject;');
  16127. Add(' TMobile = class');
  16128. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16129. Add(' end;');
  16130. Add(' TMobileClass = class of TMobile;');
  16131. Add(' TCar = class(TMobile)');
  16132. Add(' class procedure {#TCar_DoIt}DoIt;');
  16133. Add(' end;');
  16134. Add(' TCarClass = class of TCar;');
  16135. Add('class procedure TObject.DoIt;');
  16136. Add('begin');
  16137. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16138. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16139. Add('end;');
  16140. Add('class procedure TMobile.DoIt;');
  16141. Add('begin');
  16142. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16143. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16144. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16145. Add('end;');
  16146. Add('class procedure TCar.DoIt; begin end;');
  16147. Add('var');
  16148. Add(' ObjC: TClass;');
  16149. Add(' MobileC: TMobileClass;');
  16150. Add(' CarC: TCarClass;');
  16151. Add('begin');
  16152. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16153. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16154. Add(' CarC.{@TCar_DoIt}DoIt;');
  16155. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16156. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16157. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16158. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16159. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16160. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16161. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16162. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16163. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16164. ConvertProgram;
  16165. CheckSource('TestClassOf_TypeCast',
  16166. LinesToStr([ // statements
  16167. 'rtl.createClass(this, "TObject", null, function () {',
  16168. ' this.$init = function () {',
  16169. ' };',
  16170. ' this.$final = function () {',
  16171. ' };',
  16172. ' this.DoIt = function () {',
  16173. ' this.DoIt();',
  16174. ' this.DoIt$1();',
  16175. ' };',
  16176. '});',
  16177. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16178. ' this.DoIt$1 = function () {',
  16179. ' this.DoIt();',
  16180. ' this.DoIt$1();',
  16181. ' this.DoIt$2();',
  16182. ' };',
  16183. '});',
  16184. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16185. ' this.DoIt$2 = function () {',
  16186. ' };',
  16187. '});',
  16188. 'this.ObjC = null;',
  16189. 'this.MobileC = null;',
  16190. 'this.CarC = null;',
  16191. '']),
  16192. LinesToStr([ // $mod.$main
  16193. '$mod.ObjC.DoIt();',
  16194. '$mod.MobileC.DoIt$1();',
  16195. '$mod.CarC.DoIt$2();',
  16196. '$mod.ObjC.DoIt();',
  16197. '$mod.ObjC.DoIt$1();',
  16198. '$mod.ObjC.DoIt$2();',
  16199. '$mod.MobileC.DoIt();',
  16200. '$mod.MobileC.DoIt$1();',
  16201. '$mod.MobileC.DoIt$2();',
  16202. '$mod.CarC.DoIt();',
  16203. '$mod.CarC.DoIt$1();',
  16204. '$mod.CarC.DoIt$2();',
  16205. '']));
  16206. end;
  16207. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16208. begin
  16209. StartProgram(false);
  16210. Add('type');
  16211. Add(' TObject = class');
  16212. Add(' function CurNow: longint; ');
  16213. Add(' class function Now: longint; ');
  16214. Add(' end;');
  16215. Add('function TObject.CurNow: longint; begin end;');
  16216. Add('class function TObject.Now: longint; begin end;');
  16217. Add('var');
  16218. Add(' Obj: tobject;');
  16219. Add(' vI: longint;');
  16220. Add('begin');
  16221. Add(' obj.curnow;');
  16222. Add(' vi:=obj.curnow;');
  16223. Add(' tobject.now;');
  16224. Add(' vi:=tobject.now;');
  16225. ConvertProgram;
  16226. CheckSource('TestClassOf_ImplicitFunctionCall',
  16227. LinesToStr([ // statements
  16228. 'rtl.createClass(this, "TObject", null, function () {',
  16229. ' this.$init = function () {',
  16230. ' };',
  16231. ' this.$final = function () {',
  16232. ' };',
  16233. ' this.CurNow = function () {',
  16234. ' var Result = 0;',
  16235. ' return Result;',
  16236. ' };',
  16237. ' this.Now = function () {',
  16238. ' var Result = 0;',
  16239. ' return Result;',
  16240. ' };',
  16241. '});',
  16242. 'this.Obj = null;',
  16243. 'this.vI = 0;',
  16244. '']),
  16245. LinesToStr([ // $mod.$main
  16246. '$mod.Obj.CurNow();',
  16247. '$mod.vI = $mod.Obj.CurNow();',
  16248. '$mod.TObject.Now();',
  16249. '$mod.vI = $mod.TObject.Now();',
  16250. '']));
  16251. end;
  16252. procedure TTestModule.TestClassOf_Const;
  16253. begin
  16254. StartProgram(false);
  16255. Add([
  16256. 'type',
  16257. ' TObject = class',
  16258. ' end;',
  16259. ' TBird = TObject;',
  16260. ' TBirds = class of TBird;',
  16261. ' TEagles = TBirds;',
  16262. ' THawk = class(TBird);',
  16263. 'const',
  16264. ' Hawk: TEagles = THawk;',
  16265. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16266. ' TBird,',
  16267. ' THawk',
  16268. ' );',
  16269. 'begin']);
  16270. ConvertProgram;
  16271. CheckSource('TestClassOf_Const',
  16272. LinesToStr([ // statements
  16273. 'rtl.createClass(this, "TObject", null, function () {',
  16274. ' this.$init = function () {',
  16275. ' };',
  16276. ' this.$final = function () {',
  16277. ' };',
  16278. '});',
  16279. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16280. '});',
  16281. 'this.Hawk = this.THawk;',
  16282. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16283. '']),
  16284. LinesToStr([ // $mod.$main
  16285. '']));
  16286. end;
  16287. procedure TTestModule.TestNestedClass_Alias;
  16288. begin
  16289. WithTypeInfo:=true;
  16290. StartProgram(false);
  16291. Add([
  16292. 'type',
  16293. ' TObject = class',
  16294. ' type TNested = type longint;',
  16295. ' end;',
  16296. 'type TAlias = type tobject.tnested;',
  16297. 'var i: tobject.tnested = 3;',
  16298. 'var j: TAlias = 4;',
  16299. 'begin',
  16300. ' if typeinfo(TAlias)=nil then ;',
  16301. ' if typeinfo(tobject.tnested)=nil then ;',
  16302. '']);
  16303. ConvertProgram;
  16304. CheckSource('TestNestedClass_Alias',
  16305. LinesToStr([ // statements
  16306. 'rtl.createClass(this, "TObject", null, function () {',
  16307. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16308. ' this.$init = function () {',
  16309. ' };',
  16310. ' this.$final = function () {',
  16311. ' };',
  16312. '});',
  16313. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16314. 'this.i = 3;',
  16315. 'this.j = 4;',
  16316. '']),
  16317. LinesToStr([ // $mod.$main
  16318. 'if ($mod.$rtti["TAlias"] === null) ;',
  16319. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16320. '']));
  16321. end;
  16322. procedure TTestModule.TestNestedClass_Record;
  16323. begin
  16324. WithTypeInfo:=true;
  16325. StartProgram(false);
  16326. Add([
  16327. 'type',
  16328. ' TObject = class',
  16329. ' type TPoint = record',
  16330. ' x,y: byte;',
  16331. ' end;',
  16332. ' procedure DoIt(t: TPoint);',
  16333. ' end;',
  16334. 'procedure tobject.DoIt(t: TPoint);',
  16335. 'var p: TPoint;',
  16336. 'begin',
  16337. ' t.x:=t.y;',
  16338. ' p:=t;',
  16339. 'end;',
  16340. 'var',
  16341. ' p: tobject.tpoint = (x:2; y:4);',
  16342. ' o: TObject;',
  16343. 'begin',
  16344. ' p:=p;',
  16345. ' o.doit(p);',
  16346. '']);
  16347. ConvertProgram;
  16348. CheckSource('TestNestedClass_Record',
  16349. LinesToStr([ // statements
  16350. 'rtl.createClass(this, "TObject", null, function () {',
  16351. ' rtl.recNewT(this, "TPoint", function () {',
  16352. ' this.x = 0;',
  16353. ' this.y = 0;',
  16354. ' this.$eq = function (b) {',
  16355. ' return (this.x === b.x) && (this.y === b.y);',
  16356. ' };',
  16357. ' this.$assign = function (s) {',
  16358. ' this.x = s.x;',
  16359. ' this.y = s.y;',
  16360. ' return this;',
  16361. ' };',
  16362. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16363. ' $r.addField("x", rtl.byte);',
  16364. ' $r.addField("y", rtl.byte);',
  16365. ' });',
  16366. ' this.$init = function () {',
  16367. ' };',
  16368. ' this.$final = function () {',
  16369. ' };',
  16370. ' this.DoIt = function (t) {',
  16371. ' var p = this.TPoint.$new();',
  16372. ' t.x = t.y;',
  16373. ' p.$assign(t);',
  16374. ' };',
  16375. '});',
  16376. 'this.p = this.TObject.TPoint.$clone({',
  16377. ' x: 2,',
  16378. ' y: 4',
  16379. '});',
  16380. 'this.o = null;',
  16381. '']),
  16382. LinesToStr([ // $mod.$main
  16383. '$mod.p.$assign($mod.p);',
  16384. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16385. '']));
  16386. end;
  16387. procedure TTestModule.TestNestedClass_Class;
  16388. begin
  16389. WithTypeInfo:=true;
  16390. StartProgram(false);
  16391. Add([
  16392. 'type',
  16393. ' TObject = class end;',
  16394. ' TBird = class',
  16395. ' type TLeg = class',
  16396. ' FId: longint;',
  16397. ' constructor Create;',
  16398. ' function Create(i: longint): TLeg;',
  16399. ' end;',
  16400. ' function DoIt(b: TBird): Tleg;',
  16401. ' end;',
  16402. 'constructor tbird.tleg.create;',
  16403. 'begin',
  16404. ' FId:=3;',
  16405. 'end;',
  16406. 'function tbird.tleg.Create(i: longint): TLeg;',
  16407. 'begin',
  16408. ' Create;',
  16409. ' Result:=TLeg.Create;',
  16410. ' Result:=TBird.TLeg.Create;',
  16411. ' Result:=Create(3);',
  16412. ' FId:=i;',
  16413. 'end;',
  16414. 'function tbird.DoIt(b: tbird): tleg;',
  16415. 'begin',
  16416. ' Result.Create;',
  16417. ' Result:=TLeg.Create;',
  16418. ' Result:=TBird.TLeg.Create;',
  16419. ' Result:=Result.Create(3);',
  16420. 'end;',
  16421. 'var',
  16422. ' b: Tbird.tleg;',
  16423. 'begin',
  16424. ' b.Create;',
  16425. ' b:=TBird.TLeg.Create;',
  16426. ' b:=b.Create(3);',
  16427. '']);
  16428. ConvertProgram;
  16429. CheckSource('TestNestedClass_Class',
  16430. LinesToStr([ // statements
  16431. 'rtl.createClass(this, "TObject", null, function () {',
  16432. ' this.$init = function () {',
  16433. ' };',
  16434. ' this.$final = function () {',
  16435. ' };',
  16436. '});',
  16437. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16438. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16439. ' this.$init = function () {',
  16440. ' $mod.TObject.$init.call(this);',
  16441. ' this.FId = 0;',
  16442. ' };',
  16443. ' this.Create = function () {',
  16444. ' this.FId = 3;',
  16445. ' return this;',
  16446. ' };',
  16447. ' this.Create$1 = function (i) {',
  16448. ' var Result = null;',
  16449. ' this.Create();',
  16450. ' Result = $mod.TBird.TLeg.$create("Create");',
  16451. ' Result = $mod.TBird.TLeg.$create("Create");',
  16452. ' Result = this.Create$1(3);',
  16453. ' this.FId = i;',
  16454. ' return Result;',
  16455. ' };',
  16456. ' }, "TBird.TLeg");',
  16457. ' this.DoIt = function (b) {',
  16458. ' var Result = null;',
  16459. ' Result.Create();',
  16460. ' Result = this.TLeg.$create("Create");',
  16461. ' Result = $mod.TBird.TLeg.$create("Create");',
  16462. ' Result = Result.Create$1(3);',
  16463. ' return Result;',
  16464. ' };',
  16465. '});',
  16466. 'this.b = null;',
  16467. '']),
  16468. LinesToStr([ // $mod.$main
  16469. '$mod.b.Create();',
  16470. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16471. '$mod.b = $mod.b.Create$1(3);',
  16472. '']));
  16473. end;
  16474. procedure TTestModule.TestExternalClass_Var;
  16475. begin
  16476. StartProgram(false);
  16477. Add([
  16478. '{$modeswitch externalclass}',
  16479. 'type',
  16480. ' TExtA = class external name ''ExtObj''',
  16481. ' Id: longint external name ''$Id'';',
  16482. ' B: longint;',
  16483. ' end;',
  16484. 'var Obj: TExtA;',
  16485. 'begin',
  16486. ' obj.id:=obj.id+1;',
  16487. ' obj.B:=obj.B+1;']);
  16488. ConvertProgram;
  16489. CheckSource('TestExternalClass_Var',
  16490. LinesToStr([ // statements
  16491. 'this.Obj = null;',
  16492. '']),
  16493. LinesToStr([ // $mod.$main
  16494. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16495. '$mod.Obj.B = $mod.Obj.B + 1;',
  16496. '']));
  16497. end;
  16498. procedure TTestModule.TestExternalClass_Const;
  16499. begin
  16500. StartProgram(false);
  16501. Add([
  16502. '{$modeswitch externalclass}',
  16503. 'type',
  16504. ' TExtA = class external name ''ExtObj''',
  16505. ' const Two: longint = 2;',
  16506. ' const Three = 3;',
  16507. ' const Id: longint;',
  16508. ' end;',
  16509. ' TExtB = class external name ''ExtB''',
  16510. ' A: TExtA;',
  16511. ' end;',
  16512. 'var',
  16513. ' A: texta;',
  16514. ' B: textb;',
  16515. ' i: longint;',
  16516. 'begin',
  16517. ' i:=a.two;',
  16518. ' i:=texta.two;',
  16519. ' i:=a.three;',
  16520. ' i:=texta.three;',
  16521. ' i:=a.id;',
  16522. ' i:=texta.id;',
  16523. '']);
  16524. ConvertProgram;
  16525. CheckSource('TestExternalClass_Const',
  16526. LinesToStr([ // statements
  16527. 'this.A = null;',
  16528. 'this.B = null;',
  16529. 'this.i = 0;',
  16530. '']),
  16531. LinesToStr([ // $mod.$main
  16532. '$mod.i = 2;',
  16533. '$mod.i = 2;',
  16534. '$mod.i = 3;',
  16535. '$mod.i = 3;',
  16536. '$mod.i = $mod.A.Id;',
  16537. '$mod.i = ExtObj.Id;',
  16538. '']));
  16539. end;
  16540. procedure TTestModule.TestExternalClass_Dollar;
  16541. begin
  16542. StartProgram(false);
  16543. Add([
  16544. '{$modeswitch externalclass}',
  16545. 'type',
  16546. ' TExtA = class external name ''$''',
  16547. ' Id: longint external name ''$'';',
  16548. ' function Bla(i: longint): longint; external name ''$'';',
  16549. ' end;',
  16550. 'function dollar(k: longint): longint; external name ''$'';',
  16551. 'var Obj: TExtA;',
  16552. 'begin',
  16553. ' dollar(1);',
  16554. ' obj.id:=obj.id+2;',
  16555. ' obj.Bla(3);',
  16556. '']);
  16557. ConvertProgram;
  16558. CheckSource('TestExternalClass_Dollar',
  16559. LinesToStr([ // statements
  16560. 'this.Obj = null;',
  16561. '']),
  16562. LinesToStr([ // $mod.$main
  16563. '$(1);',
  16564. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16565. '$mod.Obj.$(3);',
  16566. '']));
  16567. end;
  16568. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16569. begin
  16570. StartProgram(false);
  16571. Add('{$modeswitch externalclass}');
  16572. Add('type');
  16573. Add(' TExtA = class external name ''ExtA''');
  16574. Add(' Id: longint external name ''$Id'';');
  16575. Add(' end;');
  16576. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16577. Add(' Id: longint;');
  16578. Add(' end;');
  16579. Add('begin');
  16580. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16581. ConvertProgram;
  16582. end;
  16583. procedure TTestModule.TestExternalClass_Method;
  16584. begin
  16585. StartProgram(false);
  16586. Add(['{$modeswitch externalclass}',
  16587. 'type',
  16588. ' TExtA = class external name ''ExtObj''',
  16589. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16590. ' procedure DoSome(Id: longint = 1);',
  16591. ' end;',
  16592. 'var Obj: texta;',
  16593. 'begin',
  16594. ' obj.doit;',
  16595. ' obj.doit();',
  16596. ' obj.doit(2);',
  16597. ' with obj do begin',
  16598. ' doit;',
  16599. ' doit();',
  16600. ' doit(3);',
  16601. ' end;']);
  16602. ConvertProgram;
  16603. CheckSource('TestExternalClass_Method',
  16604. LinesToStr([ // statements
  16605. 'this.Obj = null;',
  16606. '']),
  16607. LinesToStr([ // $mod.$main
  16608. '$mod.Obj.$Execute(1);',
  16609. '$mod.Obj.$Execute(1);',
  16610. '$mod.Obj.$Execute(2);',
  16611. 'var $with = $mod.Obj;',
  16612. '$with.$Execute(1);',
  16613. '$with.$Execute(1);',
  16614. '$with.$Execute(3);',
  16615. '']));
  16616. end;
  16617. procedure TTestModule.TestExternalClass_ClassMethod;
  16618. begin
  16619. StartProgram(false);
  16620. Add([
  16621. '{$modeswitch externalclass}',
  16622. 'type',
  16623. ' TExtA = class external name ''ExtObj''',
  16624. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16625. ' end;',
  16626. ' TExtB = TExtA;',
  16627. 'var p: Pointer;',
  16628. 'begin',
  16629. ' texta.doit;',
  16630. ' texta.doit();',
  16631. ' texta.doit(2);',
  16632. ' p:[email protected];',
  16633. ' with texta do begin',
  16634. ' doit;',
  16635. ' doit();',
  16636. ' doit(3);',
  16637. ' p:=@DoIt;',
  16638. ' end;',
  16639. ' textb.doit;',
  16640. ' textb.doit();',
  16641. ' textb.doit(4);',
  16642. ' with textb do begin',
  16643. ' doit;',
  16644. ' doit();',
  16645. ' doit(5);',
  16646. ' end;',
  16647. '']);
  16648. ConvertProgram;
  16649. CheckSource('TestExternalClass_ClassMethod',
  16650. LinesToStr([ // statements
  16651. 'this.p = null;',
  16652. '']),
  16653. LinesToStr([ // $mod.$main
  16654. 'ExtObj.$Execute(1);',
  16655. 'ExtObj.$Execute(1);',
  16656. 'ExtObj.$Execute(2);',
  16657. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16658. 'ExtObj.$Execute(1);',
  16659. 'ExtObj.$Execute(1);',
  16660. 'ExtObj.$Execute(3);',
  16661. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16662. 'ExtObj.$Execute(1);',
  16663. 'ExtObj.$Execute(1);',
  16664. 'ExtObj.$Execute(4);',
  16665. 'ExtObj.$Execute(1);',
  16666. 'ExtObj.$Execute(1);',
  16667. 'ExtObj.$Execute(5);',
  16668. '']));
  16669. end;
  16670. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16671. begin
  16672. StartProgram(false);
  16673. Add([
  16674. '{$modeswitch externalclass}',
  16675. 'type',
  16676. ' TExtA = class external name ''ExtObj''',
  16677. ' class procedure DoIt(Id: longint = 1); static;',
  16678. ' end;',
  16679. 'var p: Pointer;',
  16680. 'begin',
  16681. ' texta.doit;',
  16682. ' texta.doit();',
  16683. ' texta.doit(2);',
  16684. ' p:[email protected];',
  16685. ' with texta do begin',
  16686. ' doit;',
  16687. ' doit();',
  16688. ' doit(3);',
  16689. ' p:=@DoIt;',
  16690. ' end;',
  16691. '']);
  16692. ConvertProgram;
  16693. CheckSource('TestExternalClass_ClassMethodStatic',
  16694. LinesToStr([ // statements
  16695. 'this.p = null;',
  16696. '']),
  16697. LinesToStr([ // $mod.$main
  16698. 'ExtObj.DoIt(1);',
  16699. 'ExtObj.DoIt(1);',
  16700. 'ExtObj.DoIt(2);',
  16701. '$mod.p = ExtObj.DoIt;',
  16702. 'ExtObj.DoIt(1);',
  16703. 'ExtObj.DoIt(1);',
  16704. 'ExtObj.DoIt(3);',
  16705. '$mod.p = ExtObj.DoIt;',
  16706. '']));
  16707. end;
  16708. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16709. begin
  16710. StartProgram(false);
  16711. Add([
  16712. '{$modeswitch externalclass}',
  16713. 'type',
  16714. ' TBird = class external name ''Array''',
  16715. ' end;',
  16716. 'function GetPtr: Pointer;',
  16717. 'begin',
  16718. 'end;',
  16719. 'procedure Write(const p);',
  16720. 'begin',
  16721. 'end;',
  16722. 'procedure WriteLn; varargs;',
  16723. 'begin',
  16724. 'end;',
  16725. 'begin',
  16726. ' if TBird(GetPtr)=nil then ;',
  16727. ' Write(GetPtr);',
  16728. ' WriteLn(GetPtr);',
  16729. ' Write(TBird(GetPtr));',
  16730. ' WriteLn(TBird(GetPtr));',
  16731. '']);
  16732. ConvertProgram;
  16733. CheckSource('TestFunctionResultInTypeCast',
  16734. LinesToStr([ // statements
  16735. 'this.GetPtr = function () {',
  16736. ' var Result = null;',
  16737. ' return Result;',
  16738. '};',
  16739. 'this.Write = function (p) {',
  16740. '};',
  16741. 'this.WriteLn = function () {',
  16742. '};',
  16743. '']),
  16744. LinesToStr([
  16745. 'if ($mod.GetPtr() === null) ;',
  16746. '$mod.Write($mod.GetPtr());',
  16747. '$mod.WriteLn($mod.GetPtr());',
  16748. '$mod.Write($mod.GetPtr());',
  16749. '$mod.WriteLn($mod.GetPtr());',
  16750. '']));
  16751. end;
  16752. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16753. begin
  16754. StartProgram(false);
  16755. Add([
  16756. '{$modeswitch externalclass}',
  16757. 'type',
  16758. ' TExtA = class external name ''ExtObjA''',
  16759. ' procedure ProcA; virtual;',
  16760. ' procedure ProcB; virtual;',
  16761. ' end;',
  16762. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16763. ' end;',
  16764. ' TExtC = class (TExtB)',
  16765. ' procedure ProcA; override;',
  16766. ' end;',
  16767. 'procedure TExtC.ProcA;',
  16768. 'begin',
  16769. ' ProcA;',
  16770. ' Self.ProcA;',
  16771. ' ProcB;',
  16772. ' Self.ProcB;',
  16773. 'end;',
  16774. 'var',
  16775. ' A: texta;',
  16776. ' B: textb;',
  16777. ' C: textc;',
  16778. 'begin',
  16779. ' a.proca;',
  16780. ' b.proca;',
  16781. ' c.proca;']);
  16782. ConvertProgram;
  16783. CheckSource('TestExternalClass_NonExternalOverride',
  16784. LinesToStr([ // statements
  16785. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16786. ' this.$init = function () {',
  16787. ' };',
  16788. ' this.$final = function () {',
  16789. ' };',
  16790. ' this.ProcA = function () {',
  16791. ' this.ProcA();',
  16792. ' this.ProcA();',
  16793. ' this.ProcB();',
  16794. ' this.ProcB();',
  16795. ' };',
  16796. '});',
  16797. 'this.A = null;',
  16798. 'this.B = null;',
  16799. 'this.C = null;',
  16800. '']),
  16801. LinesToStr([ // $mod.$main
  16802. '$mod.A.ProcA();',
  16803. '$mod.B.ProcA();',
  16804. '$mod.C.ProcA();',
  16805. '']));
  16806. end;
  16807. procedure TTestModule.TestExternalClass_OverloadHint;
  16808. begin
  16809. StartProgram(false);
  16810. Add([
  16811. '{$modeswitch externalclass}',
  16812. 'type',
  16813. ' TExtA = class external name ''ExtObjA''',
  16814. ' procedure DoIt;',
  16815. ' procedure DoIt(i: longint);',
  16816. ' end;',
  16817. 'begin',
  16818. '']);
  16819. ConvertProgram;
  16820. CheckResolverUnexpectedHints(true);
  16821. CheckSource('TestExternalClass_OverloadHint',
  16822. LinesToStr([ // statements
  16823. '']),
  16824. LinesToStr([ // $mod.$main
  16825. '']));
  16826. end;
  16827. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16828. begin
  16829. WithTypeInfo:=true;
  16830. StartProgram(false);
  16831. Add([
  16832. '{$modeswitch externalclass}',
  16833. 'type',
  16834. ' JSwiper = class external name ''Swiper''',
  16835. ' constructor New;',
  16836. ' end;',
  16837. ' TObject = class',
  16838. ' private',
  16839. ' FSwiper: JSwiper;',
  16840. ' published',
  16841. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16842. ' end;',
  16843. 'begin',
  16844. ' JSwiper.new;',
  16845. '']);
  16846. ConvertProgram;
  16847. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16848. LinesToStr([ // statements
  16849. 'this.$rtti.$ExtClass("JSwiper", {',
  16850. ' jsclass: "Swiper"',
  16851. '});',
  16852. 'rtl.createClass(this, "TObject", null, function () {',
  16853. ' this.$init = function () {',
  16854. ' this.FSwiper = null;',
  16855. ' };',
  16856. ' this.$final = function () {',
  16857. ' this.FSwiper = undefined;',
  16858. ' };',
  16859. ' var $r = this.$rtti;',
  16860. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16861. '});',
  16862. '']),
  16863. LinesToStr([ // $mod.$main
  16864. 'new Swiper();',
  16865. '']));
  16866. end;
  16867. procedure TTestModule.TestExternalClass_Property;
  16868. begin
  16869. StartProgram(false);
  16870. Add([
  16871. '{$modeswitch externalclass}',
  16872. 'type',
  16873. ' TExtA = class external name ''ExtA''',
  16874. ' function getYear: longint;',
  16875. ' procedure setYear(Value: longint);',
  16876. ' property Year: longint read getyear write setyear;',
  16877. ' end;',
  16878. ' TExtB = class (TExtA)',
  16879. ' procedure OtherSetYear(Value: longint);',
  16880. ' property year write othersetyear;',
  16881. ' end;',
  16882. 'procedure textb.othersetyear(value: longint);',
  16883. 'begin',
  16884. ' setYear(Value+4);',
  16885. 'end;',
  16886. 'var',
  16887. ' A: texta;',
  16888. ' B: textb;',
  16889. 'begin',
  16890. ' a.year:=a.year+1;',
  16891. ' b.year:=b.year+2;']);
  16892. ConvertProgram;
  16893. CheckSource('TestExternalClass_NonExternalOverride',
  16894. LinesToStr([ // statements
  16895. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16896. ' this.$init = function () {',
  16897. ' };',
  16898. ' this.$final = function () {',
  16899. ' };',
  16900. ' this.OtherSetYear = function (Value) {',
  16901. ' this.setYear(Value+4);',
  16902. ' };',
  16903. '});',
  16904. 'this.A = null;',
  16905. 'this.B = null;',
  16906. '']),
  16907. LinesToStr([ // $mod.$main
  16908. '$mod.A.setYear($mod.A.getYear()+1);',
  16909. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16910. '']));
  16911. end;
  16912. procedure TTestModule.TestExternalClass_PropertyDate;
  16913. begin
  16914. StartProgram(false);
  16915. Add([
  16916. '{$modeswitch externalclass}',
  16917. 'type',
  16918. ' TExtA = class external name ''ExtA''',
  16919. ' end;',
  16920. ' TExtB = class (TExtA)',
  16921. ' FDate: string;',
  16922. ' property Date: string read FDate write FDate;',
  16923. ' property ExtA: string read FDate write FDate;',
  16924. ' end;',
  16925. ' {$M+}',
  16926. ' TObject = class',
  16927. ' FDate: string;',
  16928. ' published',
  16929. ' property Date: string read FDate write FDate;',
  16930. ' property ExtA: string read FDate write FDate;',
  16931. ' end;',
  16932. 'var',
  16933. ' B: textb;',
  16934. ' o: TObject;',
  16935. 'begin',
  16936. ' b.date:=b.exta;',
  16937. ' o.date:=o.exta;']);
  16938. ConvertProgram;
  16939. CheckSource('TestExternalClass_PropertyDate',
  16940. LinesToStr([ // statements
  16941. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16942. ' this.$init = function () {',
  16943. ' this.FDate = "";',
  16944. ' };',
  16945. ' this.$final = function () {',
  16946. ' };',
  16947. '});',
  16948. 'rtl.createClass(this, "TObject", null, function () {',
  16949. ' this.$init = function () {',
  16950. ' this.FDate = "";',
  16951. ' };',
  16952. ' this.$final = function () {',
  16953. ' };',
  16954. ' var $r = this.$rtti;',
  16955. ' $r.addField("FDate", rtl.string);',
  16956. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16957. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16958. '});',
  16959. 'this.B = null;',
  16960. 'this.o = null;',
  16961. '']),
  16962. LinesToStr([ // $mod.$main
  16963. '$mod.B.FDate = $mod.B.FDate;',
  16964. '$mod.o.FDate = $mod.o.FDate;',
  16965. '']));
  16966. end;
  16967. procedure TTestModule.TestExternalClass_ClassProperty;
  16968. begin
  16969. StartProgram(false);
  16970. Add('{$modeswitch externalclass}');
  16971. Add('type');
  16972. Add(' TExtA = class external name ''ExtA''');
  16973. Add(' class function getYear: longint;');
  16974. Add(' class procedure setYear(Value: longint);');
  16975. Add(' class property Year: longint read getyear write setyear;');
  16976. Add(' end;');
  16977. Add(' TExtB = class (TExtA)');
  16978. Add(' class function GetCentury: longint;');
  16979. Add(' class procedure SetCentury(Value: longint);');
  16980. Add(' class property Century: longint read getcentury write setcentury;');
  16981. Add(' end;');
  16982. Add('class function textb.getcentury: longint;');
  16983. Add('begin');
  16984. Add('end;');
  16985. Add('class procedure textb.setcentury(value: longint);');
  16986. Add('begin');
  16987. Add(' setyear(value+11);');
  16988. Add(' texta.year:=texta.year+12;');
  16989. Add(' year:=year+13;');
  16990. Add(' textb.century:=textb.century+14;');
  16991. Add(' century:=century+15;');
  16992. Add('end;');
  16993. Add('var');
  16994. Add(' A: texta;');
  16995. Add(' B: textb;');
  16996. Add('begin');
  16997. Add(' texta.year:=texta.year+1;');
  16998. Add(' textb.year:=textb.year+2;');
  16999. Add(' TextA.year:=TextA.year+3;');
  17000. Add(' b.year:=b.year+4;');
  17001. Add(' textb.century:=textb.century+5;');
  17002. Add(' b.century:=b.century+6;');
  17003. ConvertProgram;
  17004. CheckSource('TestExternalClass_ClassProperty',
  17005. LinesToStr([ // statements
  17006. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17007. ' this.$init = function () {',
  17008. ' };',
  17009. ' this.$final = function () {',
  17010. ' };',
  17011. ' this.GetCentury = function () {',
  17012. ' var Result = 0;',
  17013. ' return Result;',
  17014. ' };',
  17015. ' this.SetCentury = function (Value) {',
  17016. ' this.setYear(Value + 11);',
  17017. ' ExtA.setYear(ExtA.getYear() + 12);',
  17018. ' this.setYear(this.getYear() + 13);',
  17019. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17020. ' this.SetCentury(this.GetCentury() + 15);',
  17021. ' };',
  17022. '});',
  17023. 'this.A = null;',
  17024. 'this.B = null;',
  17025. '']),
  17026. LinesToStr([ // $mod.$main
  17027. 'ExtA.setYear(ExtA.getYear() + 1);',
  17028. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17029. 'ExtA.setYear(ExtA.getYear() + 3);',
  17030. '$mod.B.setYear($mod.B.getYear() + 4);',
  17031. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17032. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17033. '']));
  17034. end;
  17035. procedure TTestModule.TestExternalClass_ClassOf;
  17036. begin
  17037. StartProgram(false);
  17038. Add('{$modeswitch externalclass}');
  17039. Add('type');
  17040. Add(' TExtA = class external name ''ExtA''');
  17041. Add(' procedure ProcA; virtual;');
  17042. Add(' procedure ProcB; virtual;');
  17043. Add(' end;');
  17044. Add(' TExtAClass = class of TExtA;');
  17045. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17046. Add(' end;');
  17047. Add(' TExtBClass = class of TExtB;');
  17048. Add(' TExtC = class (TExtB)');
  17049. Add(' procedure ProcA; override;');
  17050. Add(' end;');
  17051. Add(' TExtCClass = class of TExtC;');
  17052. Add('procedure TExtC.ProcA; begin end;');
  17053. Add('var');
  17054. Add(' A: texta; ClA: TExtAClass;');
  17055. Add(' B: textb; ClB: TExtBClass;');
  17056. Add(' C: textc; ClC: TExtCClass;');
  17057. Add('begin');
  17058. Add(' ClA:=texta;');
  17059. Add(' ClA:=textb;');
  17060. Add(' ClA:=textc;');
  17061. Add(' ClB:=textb;');
  17062. Add(' ClB:=textc;');
  17063. Add(' ClC:=textc;');
  17064. ConvertProgram;
  17065. CheckSource('TestExternalClass_ClassOf',
  17066. LinesToStr([ // statements
  17067. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17068. ' this.$init = function () {',
  17069. ' };',
  17070. ' this.$final = function () {',
  17071. ' };',
  17072. ' this.ProcA = function () {',
  17073. ' };',
  17074. '});',
  17075. 'this.A = null;',
  17076. 'this.ClA = null;',
  17077. 'this.B = null;',
  17078. 'this.ClB = null;',
  17079. 'this.C = null;',
  17080. 'this.ClC = null;',
  17081. '']),
  17082. LinesToStr([ // $mod.$main
  17083. '$mod.ClA = ExtA;',
  17084. '$mod.ClA = ExtB;',
  17085. '$mod.ClA = $mod.TExtC;',
  17086. '$mod.ClB = ExtB;',
  17087. '$mod.ClB = $mod.TExtC;',
  17088. '$mod.ClC = $mod.TExtC;',
  17089. '']));
  17090. end;
  17091. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17092. begin
  17093. AddModuleWithIntfImplSrc('unit2.pas',
  17094. LinesToStr([
  17095. '{$modeswitch externalclass}',
  17096. 'type',
  17097. ' TExtA = class external name ''ExtA''',
  17098. ' class var Id: longint;',
  17099. ' end;',
  17100. '']),
  17101. '');
  17102. StartUnit(true);
  17103. Add('interface');
  17104. Add('uses unit2;');
  17105. Add('implementation');
  17106. Add('begin');
  17107. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17108. ConvertUnit;
  17109. CheckSource('TestExternalClass_ClassOtherUnit',
  17110. LinesToStr([
  17111. '']),
  17112. LinesToStr([
  17113. 'ExtA.Id = ExtA.Id + 1;',
  17114. '']));
  17115. end;
  17116. procedure TTestModule.TestExternalClass_Is;
  17117. begin
  17118. StartProgram(false);
  17119. Add([
  17120. '{$modeswitch externalclass}',
  17121. 'type',
  17122. ' TExtA = class external name ''ExtA''',
  17123. ' end;',
  17124. ' TExtAClass = class of TExtA;',
  17125. ' TExtB = class external name ''ExtB'' (TExtA)',
  17126. ' end;',
  17127. ' TExtBClass = class of TExtB;',
  17128. ' TExtC = class (TExtB)',
  17129. ' end;',
  17130. ' TExtCClass = class of TExtC;',
  17131. 'var',
  17132. ' A: texta; ClA: TExtAClass;',
  17133. ' B: textb; ClB: TExtBClass;',
  17134. ' C: textc; ClC: TExtCClass;',
  17135. 'begin',
  17136. ' if a is textb then ;',
  17137. ' if a is textc then ;',
  17138. ' if b is textc then ;',
  17139. ' if cla is textb then ;',
  17140. ' if cla is textc then ;',
  17141. ' if clb is textc then ;',
  17142. ' try',
  17143. ' except',
  17144. ' on TExtA do ;',
  17145. ' on e: TExtB do ;',
  17146. ' end;',
  17147. '']);
  17148. ConvertProgram;
  17149. CheckSource('TestExternalClass_Is',
  17150. LinesToStr([ // statements
  17151. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17152. ' this.$init = function () {',
  17153. ' };',
  17154. ' this.$final = function () {',
  17155. ' };',
  17156. '});',
  17157. 'this.A = null;',
  17158. 'this.ClA = null;',
  17159. 'this.B = null;',
  17160. 'this.ClB = null;',
  17161. 'this.C = null;',
  17162. 'this.ClC = null;',
  17163. '']),
  17164. LinesToStr([ // $mod.$main
  17165. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17166. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17167. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17168. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17169. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17170. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17171. 'try {} catch ($e) {',
  17172. ' if (rtl.isExt($e,ExtA)) {}',
  17173. ' else if (rtl.isExt($e,ExtB)) {',
  17174. ' var e = $e;',
  17175. ' } else throw $e',
  17176. '};',
  17177. '']));
  17178. end;
  17179. procedure TTestModule.TestExternalClass_As;
  17180. begin
  17181. StartProgram(false);
  17182. Add('{$modeswitch externalclass}');
  17183. Add('type');
  17184. Add(' TExtA = class external name ''ExtA''');
  17185. Add(' end;');
  17186. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17187. Add(' end;');
  17188. Add(' TExtC = class (TExtB)');
  17189. Add(' end;');
  17190. Add('var');
  17191. Add(' A: texta;');
  17192. Add(' B: textb;');
  17193. Add(' C: textc;');
  17194. Add('begin');
  17195. Add(' b:=a as textb;');
  17196. Add(' c:=a as textc;');
  17197. Add(' c:=b as textc;');
  17198. ConvertProgram;
  17199. CheckSource('TestExternalClass_Is',
  17200. LinesToStr([ // statements
  17201. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17202. ' this.$init = function () {',
  17203. ' };',
  17204. ' this.$final = function () {',
  17205. ' };',
  17206. '});',
  17207. 'this.A = null;',
  17208. 'this.B = null;',
  17209. 'this.C = null;',
  17210. '']),
  17211. LinesToStr([ // $mod.$main
  17212. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17213. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17214. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17215. '']));
  17216. end;
  17217. procedure TTestModule.TestExternalClass_DestructorFail;
  17218. begin
  17219. StartProgram(false);
  17220. Add('{$modeswitch externalclass}');
  17221. Add('type');
  17222. Add(' TExtA = class external name ''ExtA''');
  17223. Add(' destructor Free;');
  17224. Add(' end;');
  17225. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17226. nPasElementNotSupported);
  17227. ConvertProgram;
  17228. end;
  17229. procedure TTestModule.TestExternalClass_New;
  17230. begin
  17231. StartProgram(false);
  17232. Add([
  17233. '{$modeswitch externalclass}',
  17234. 'type',
  17235. ' TExtA = class external name ''ExtA''',
  17236. ' constructor New;',
  17237. ' constructor New(i: longint; j: longint = 2);',
  17238. ' end;',
  17239. 'var',
  17240. ' A: texta;',
  17241. 'begin',
  17242. ' a:=texta.new;',
  17243. ' a:=texta(texta.new);',
  17244. ' a:=texta.new();',
  17245. ' a:=texta.new(1);',
  17246. ' with texta do begin',
  17247. ' a:=new;',
  17248. ' a:=new();',
  17249. ' a:=new(2);',
  17250. ' end;',
  17251. ' a:=test1.texta.new;',
  17252. ' a:=test1.texta.new();',
  17253. ' a:=test1.texta.new(3);',
  17254. '']);
  17255. ConvertProgram;
  17256. CheckSource('TestExternalClass_New',
  17257. LinesToStr([ // statements
  17258. 'this.A = null;',
  17259. '']),
  17260. LinesToStr([ // $mod.$main
  17261. '$mod.A = new ExtA();',
  17262. '$mod.A = new ExtA();',
  17263. '$mod.A = new ExtA();',
  17264. '$mod.A = new ExtA(1,2);',
  17265. '$mod.A = new ExtA();',
  17266. '$mod.A = new ExtA();',
  17267. '$mod.A = new ExtA(2,2);',
  17268. '$mod.A = new ExtA();',
  17269. '$mod.A = new ExtA();',
  17270. '$mod.A = new ExtA(3,2);',
  17271. '']));
  17272. end;
  17273. procedure TTestModule.TestExternalClass_ClassOf_New;
  17274. begin
  17275. StartProgram(false);
  17276. Add('{$modeswitch externalclass}');
  17277. Add('type');
  17278. Add(' TExtAClass = class of TExtA;');
  17279. Add(' TExtA = class external name ''ExtA''');
  17280. Add(' C: TExtAClass;');
  17281. Add(' constructor New;');
  17282. Add(' end;');
  17283. Add('var');
  17284. Add(' A: texta;');
  17285. Add(' C: textaclass;');
  17286. Add('begin');
  17287. Add(' a:=c.new;');
  17288. Add(' a:=c.new();');
  17289. Add(' with C do begin');
  17290. Add(' a:=new;');
  17291. Add(' a:=new();');
  17292. Add(' end;');
  17293. Add(' a:=test1.c.new;');
  17294. Add(' a:=test1.c.new();');
  17295. Add(' a:=A.c.new();');
  17296. ConvertProgram;
  17297. CheckSource('TestExternalClass_ClassOf_New',
  17298. LinesToStr([ // statements
  17299. 'this.A = null;',
  17300. 'this.C = null;',
  17301. '']),
  17302. LinesToStr([ // $mod.$main
  17303. '$mod.A = new $mod.C();',
  17304. '$mod.A = new $mod.C();',
  17305. 'var $with = $mod.C;',
  17306. '$mod.A = new $with();',
  17307. '$mod.A = new $with();',
  17308. '$mod.A = new $mod.C();',
  17309. '$mod.A = new $mod.C();',
  17310. '$mod.A = new $mod.A.C();',
  17311. '']));
  17312. end;
  17313. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17314. begin
  17315. StartProgram(false);
  17316. Add([
  17317. '{$modeswitch externalclass}',
  17318. 'type',
  17319. ' TExtAClass = class of TExtA;',
  17320. ' TExtA = class external name ''ExtA''',
  17321. ' constructor New;',
  17322. ' end;',
  17323. 'function GetCreator: TExtAClass;',
  17324. 'begin',
  17325. ' Result:=TExtA;',
  17326. 'end;',
  17327. 'var',
  17328. ' A: texta;',
  17329. 'begin',
  17330. ' a:=getcreator.new;',
  17331. ' a:=getcreator().new;',
  17332. ' a:=getcreator().new();',
  17333. ' a:=getcreator.new();',
  17334. ' with getcreator do begin',
  17335. ' a:=new;',
  17336. ' a:=new();',
  17337. ' end;']);
  17338. ConvertProgram;
  17339. CheckSource('TestExternalClass_FuncClassOf_New',
  17340. LinesToStr([ // statements
  17341. 'this.GetCreator = function () {',
  17342. ' var Result = null;',
  17343. ' Result = ExtA;',
  17344. ' return Result;',
  17345. '};',
  17346. 'this.A = null;',
  17347. '']),
  17348. LinesToStr([ // $mod.$main
  17349. '$mod.A = new ($mod.GetCreator())();',
  17350. '$mod.A = new ($mod.GetCreator())();',
  17351. '$mod.A = new ($mod.GetCreator())();',
  17352. '$mod.A = new ($mod.GetCreator())();',
  17353. 'var $with = $mod.GetCreator();',
  17354. '$mod.A = new $with();',
  17355. '$mod.A = new $with();',
  17356. '']));
  17357. end;
  17358. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17359. begin
  17360. StartProgram(false);
  17361. Add([
  17362. '{$modeswitch externalclass}',
  17363. 'type',
  17364. ' TExtA = class external name ''ExtA''',
  17365. ' constructor New;',
  17366. ' end;',
  17367. ' TBird = class(TExtA)',
  17368. ' end;',
  17369. 'begin',
  17370. ' TBird.new;',
  17371. '']);
  17372. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17373. ConvertProgram;
  17374. end;
  17375. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17376. begin
  17377. StartProgram(false);
  17378. Add([
  17379. '{$modeswitch externalclass}',
  17380. 'type',
  17381. ' TExtA = class external name ''ExtA''',
  17382. ' constructor New;',
  17383. ' end;',
  17384. ' TBird = class(TExtA)',
  17385. ' end;',
  17386. 'begin',
  17387. ' TBird.new();',
  17388. '']);
  17389. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17390. ConvertProgram;
  17391. end;
  17392. procedure TTestModule.TestExternalClass_NewExtName;
  17393. begin
  17394. StartProgram(false);
  17395. Add([
  17396. '{$modeswitch externalclass}',
  17397. 'type',
  17398. ' TExtA = class external name ''ExtA''',
  17399. ' constructor New; external name ''Other'';',
  17400. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17401. ' end;',
  17402. 'var',
  17403. ' A: texta;',
  17404. 'begin',
  17405. ' a:=texta.new;',
  17406. ' a:=texta(texta.new);',
  17407. ' a:=texta.new();',
  17408. ' a:=texta.new(1);',
  17409. ' with texta do begin',
  17410. ' a:=new;',
  17411. ' a:=new();',
  17412. ' a:=new(2);',
  17413. ' end;',
  17414. ' a:=test1.texta.new;',
  17415. ' a:=test1.texta.new();',
  17416. ' a:=test1.texta.new(3);',
  17417. '']);
  17418. ConvertProgram;
  17419. CheckSource('TestExternalClass_NewExtName',
  17420. LinesToStr([ // statements
  17421. 'this.A = null;',
  17422. '']),
  17423. LinesToStr([ // $mod.$main
  17424. '$mod.A = new Other();',
  17425. '$mod.A = new Other();',
  17426. '$mod.A = new Other();',
  17427. '$mod.A = new A.B(1,2);',
  17428. '$mod.A = new Other();',
  17429. '$mod.A = new Other();',
  17430. '$mod.A = new A.B(2,2);',
  17431. '$mod.A = new Other();',
  17432. '$mod.A = new Other();',
  17433. '$mod.A = new A.B(3,2);',
  17434. '']));
  17435. end;
  17436. procedure TTestModule.TestExternalClass_Constructor;
  17437. begin
  17438. StartProgram(false);
  17439. Add([
  17440. '{$modeswitch externalclass}',
  17441. 'type',
  17442. ' TExtA = class external name ''ExtA''',
  17443. ' constructor Create;',
  17444. ' constructor Create(i: longint; j: longint = 2);',
  17445. ' end;',
  17446. 'var',
  17447. ' A: texta;',
  17448. 'begin',
  17449. ' a:=texta.create;',
  17450. ' a:=texta(texta.create);',
  17451. ' a:=texta.create();',
  17452. ' a:=texta.create(1);',
  17453. ' with texta do begin',
  17454. ' a:=create;',
  17455. ' a:=create();',
  17456. ' a:=create(2);',
  17457. ' end;',
  17458. ' a:=test1.texta.create;',
  17459. ' a:=test1.texta.create();',
  17460. ' a:=test1.texta.create(3);',
  17461. '']);
  17462. ConvertProgram;
  17463. CheckSource('TestExternalClass_Constructor',
  17464. LinesToStr([ // statements
  17465. 'this.A = null;',
  17466. '']),
  17467. LinesToStr([ // $mod.$main
  17468. '$mod.A = new ExtA.Create();',
  17469. '$mod.A = new ExtA.Create();',
  17470. '$mod.A = new ExtA.Create();',
  17471. '$mod.A = new ExtA.Create(1,2);',
  17472. '$mod.A = new ExtA.Create();',
  17473. '$mod.A = new ExtA.Create();',
  17474. '$mod.A = new ExtA.Create(2,2);',
  17475. '$mod.A = new ExtA.Create();',
  17476. '$mod.A = new ExtA.Create();',
  17477. '$mod.A = new ExtA.Create(3,2);',
  17478. '']));
  17479. end;
  17480. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17481. begin
  17482. StartProgram(false);
  17483. Add([
  17484. '{$modeswitch externalclass}',
  17485. 'type',
  17486. ' TExtA = class external name ''ExtA''',
  17487. ' constructor Create; external name ''{}'';',
  17488. ' end;',
  17489. 'var',
  17490. ' A: texta;',
  17491. 'begin',
  17492. ' a:=texta.create;',
  17493. ' a:=texta(texta.create);',
  17494. ' a:=texta.create();',
  17495. ' with texta do begin',
  17496. ' a:=create;',
  17497. ' a:=create();',
  17498. ' end;',
  17499. ' a:=test1.texta.create;',
  17500. ' a:=test1.texta.create();',
  17501. '']);
  17502. ConvertProgram;
  17503. CheckSource('TestExternalClass_ConstructorBrackets',
  17504. LinesToStr([ // statements
  17505. 'this.A = null;',
  17506. '']),
  17507. LinesToStr([ // $mod.$main
  17508. '$mod.A = {};',
  17509. '$mod.A = {};',
  17510. '$mod.A = {};',
  17511. '$mod.A = {};',
  17512. '$mod.A = {};',
  17513. '$mod.A = {};',
  17514. '$mod.A = {};',
  17515. '']));
  17516. end;
  17517. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17518. begin
  17519. StartProgram(false);
  17520. Add('{$modeswitch externalclass}');
  17521. Add('type');
  17522. Add(' TExtA = class external name ''ExtA''');
  17523. Add(' constructor New;');
  17524. Add(' end;');
  17525. Add('function DoIt: longint;');
  17526. Add('const ExtA: longint = 3;');
  17527. Add('begin');
  17528. Add(' Result:=ExtA;');
  17529. Add('end;');
  17530. Add('var');
  17531. Add(' A: texta;');
  17532. Add('begin');
  17533. Add(' a:=texta.new;');
  17534. ConvertProgram;
  17535. CheckSource('TestExternalClass_LocalConstSameName',
  17536. LinesToStr([ // statements
  17537. 'var ExtA$1 = 3;',
  17538. 'this.DoIt = function () {',
  17539. ' var Result = 0;',
  17540. ' Result = ExtA$1;',
  17541. ' return Result;',
  17542. '};',
  17543. 'this.A = null;',
  17544. '']),
  17545. LinesToStr([ // $mod.$main
  17546. '$mod.A = new ExtA();',
  17547. '']));
  17548. end;
  17549. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17550. begin
  17551. StartProgram(false);
  17552. Add('{$modeswitch externalclass}');
  17553. Add('type');
  17554. Add(' TExtA = class external name ''ExtA''');
  17555. Add(' procedure DoIt;');
  17556. Add(' end;');
  17557. Add(' TMyA = class(TExtA)');
  17558. Add(' procedure DoIt;');
  17559. Add(' end;');
  17560. Add('procedure TMyA.DoIt; begin end;');
  17561. Add('begin');
  17562. ConvertProgram;
  17563. CheckSource('TestExternalClass_ReintroduceOverload',
  17564. LinesToStr([ // statements
  17565. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17566. ' this.$init = function () {',
  17567. ' };',
  17568. ' this.$final = function () {',
  17569. ' };',
  17570. ' this.DoIt$1 = function () {',
  17571. ' };',
  17572. '});',
  17573. '']),
  17574. LinesToStr([ // $mod.$main
  17575. '']));
  17576. end;
  17577. procedure TTestModule.TestExternalClass_Inherited;
  17578. begin
  17579. StartProgram(false);
  17580. Add('{$modeswitch externalclass}');
  17581. Add('type');
  17582. Add(' TExtA = class external name ''ExtA''');
  17583. Add(' procedure DoIt(i: longint = 1); virtual;');
  17584. Add(' procedure DoSome(j: longint = 2);');
  17585. Add(' end;');
  17586. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17587. Add(' end;');
  17588. Add(' TMyC = class(TExtB)');
  17589. Add(' procedure DoIt(i: longint = 1); override;');
  17590. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17591. Add(' end;');
  17592. Add('procedure TMyC.DoIt(i: longint);');
  17593. Add('begin');
  17594. Add(' inherited;');
  17595. Add(' inherited DoIt;');
  17596. Add(' inherited DoIt();');
  17597. Add(' inherited DoIt(3);');
  17598. Add(' inherited DoSome;');
  17599. Add(' inherited DoSome();');
  17600. Add(' inherited DoSome(4);');
  17601. Add('end;');
  17602. Add('procedure TMyC.DoSome(j: longint);');
  17603. Add('begin');
  17604. Add(' inherited;');
  17605. Add('end;');
  17606. Add('begin');
  17607. ConvertProgram;
  17608. CheckSource('TestExternalClass_ReintroduceOverload',
  17609. LinesToStr([ // statements
  17610. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17611. ' this.$init = function () {',
  17612. ' };',
  17613. ' this.$final = function () {',
  17614. ' };',
  17615. ' this.DoIt = function (i) {',
  17616. ' ExtB.DoIt.apply(this, arguments);',
  17617. ' ExtB.DoIt.call(this, 1);',
  17618. ' ExtB.DoIt.call(this, 1);',
  17619. ' ExtB.DoIt.call(this, 3);',
  17620. ' ExtB.DoSome.call(this, 2);',
  17621. ' ExtB.DoSome.call(this, 2);',
  17622. ' ExtB.DoSome.call(this, 4);',
  17623. ' };',
  17624. ' this.DoSome$1 = function (j) {',
  17625. ' ExtB.DoSome.apply(this, arguments);',
  17626. ' };',
  17627. '});',
  17628. '']),
  17629. LinesToStr([ // $mod.$main
  17630. '']));
  17631. end;
  17632. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17633. begin
  17634. StartProgram(false);
  17635. Add('{$modeswitch externalclass}');
  17636. Add('type');
  17637. Add(' TObject = class');
  17638. Add(' end;');
  17639. Add(' TExtA = class external name ''ExtA''(TObject)');
  17640. Add(' end;');
  17641. Add('begin');
  17642. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17643. ConvertProgram;
  17644. end;
  17645. procedure TTestModule.TestExternalClass_NewInstance;
  17646. begin
  17647. StartProgram(false);
  17648. Add('{$modeswitch externalclass}');
  17649. Add('type');
  17650. Add(' TExtA = class external name ''ExtA''');
  17651. Add(' end;');
  17652. Add(' TMyB = class(TExtA)');
  17653. Add(' protected');
  17654. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17655. Add(' end;');
  17656. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17657. Add('begin end;');
  17658. Add('begin');
  17659. ConvertProgram;
  17660. CheckSource('TestExternalClass_NewInstance',
  17661. LinesToStr([ // statements
  17662. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17663. ' this.$init = function () {',
  17664. ' };',
  17665. ' this.$final = function () {',
  17666. ' };',
  17667. ' this.NewInstance = function (fnname, paramarray) {',
  17668. ' var Result = null;',
  17669. ' return Result;',
  17670. ' };',
  17671. '});',
  17672. '']),
  17673. LinesToStr([ // $mod.$main
  17674. '']));
  17675. end;
  17676. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17677. begin
  17678. StartProgram(false);
  17679. Add('{$modeswitch externalclass}');
  17680. Add('type');
  17681. Add(' TExtA = class external name ''ExtA''');
  17682. Add(' end;');
  17683. Add(' TMyB = class(TExtA)');
  17684. Add(' protected');
  17685. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17686. Add(' end;');
  17687. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17688. Add('begin end;');
  17689. Add('begin');
  17690. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17691. ConvertProgram;
  17692. end;
  17693. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17694. begin
  17695. StartProgram(false);
  17696. Add('{$modeswitch externalclass}');
  17697. Add('type');
  17698. Add(' TExtA = class external name ''ExtA''');
  17699. Add(' end;');
  17700. Add(' TMyB = class(TExtA)');
  17701. Add(' protected');
  17702. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17703. Add(' end;');
  17704. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17705. Add('begin end;');
  17706. Add('begin');
  17707. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17708. nIncompatibleTypeArgNo);
  17709. ConvertProgram;
  17710. end;
  17711. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17712. begin
  17713. StartProgram(false);
  17714. Add('{$modeswitch externalclass}');
  17715. Add('type');
  17716. Add(' TExtA = class external name ''ExtA''');
  17717. Add(' end;');
  17718. Add(' TMyB = class(TExtA)');
  17719. Add(' protected');
  17720. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17721. Add(' end;');
  17722. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17723. Add('begin end;');
  17724. Add('begin');
  17725. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17726. nIncompatibleTypeArgNo);
  17727. ConvertProgram;
  17728. end;
  17729. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17730. begin
  17731. StartProgram(false);
  17732. Add([
  17733. '{$modeswitch externalclass}',
  17734. 'type',
  17735. ' TJSFunction = class external name ''Function''',
  17736. ' end;',
  17737. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17738. ' constructor New(w: word);',
  17739. ' end;',
  17740. ' TBird = class (TExtA)',
  17741. ' public',
  17742. ' Size: word;',
  17743. ' class var Legs: word;',
  17744. ' constructor Create(a: word);',
  17745. ' end;',
  17746. ' TEagle = class (TBird)',
  17747. ' public',
  17748. ' constructor Create(b: word); reintroduce;',
  17749. ' end;',
  17750. 'constructor TBird.Create(a: word);',
  17751. 'begin',
  17752. ' inherited;', // silently ignored
  17753. ' inherited New(a);', // this.$func(a)
  17754. 'end;',
  17755. 'constructor TEagle.Create(b: word);',
  17756. 'begin',
  17757. ' inherited Create(b);',
  17758. 'end;',
  17759. 'var',
  17760. ' Bird: TBird;',
  17761. ' Eagle: TEagle;',
  17762. 'begin',
  17763. ' Bird:=TBird.Create(3);',
  17764. ' Eagle:=TEagle.Create(4);',
  17765. ' Bird.Size:=Bird.Size+5;',
  17766. ' Bird.Legs:=Bird.Legs+6;',
  17767. ' Eagle.Size:=Eagle.Size+5;',
  17768. ' Eagle.Legs:=Eagle.Legs+6;',
  17769. '']);
  17770. ConvertProgram;
  17771. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17772. LinesToStr([ // statements
  17773. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17774. ' this.Legs = 0;',
  17775. ' this.$init = function () {',
  17776. ' this.Size = 0;',
  17777. ' };',
  17778. ' this.$final = function () {',
  17779. ' };',
  17780. ' this.Create = function (a) {',
  17781. ' this.$ancestorfunc(a);',
  17782. ' return this;',
  17783. ' };',
  17784. '});',
  17785. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17786. ' this.Create$1 = function (b) {',
  17787. ' $mod.TBird.Create.call(this, b);',
  17788. ' return this;',
  17789. ' };',
  17790. '});',
  17791. 'this.Bird = null;',
  17792. 'this.Eagle = null;',
  17793. '']),
  17794. LinesToStr([ // $mod.$main
  17795. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17796. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17797. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17798. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17799. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17800. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17801. '']));
  17802. end;
  17803. procedure TTestModule.TestExternalClass_PascalProperty;
  17804. begin
  17805. StartProgram(false);
  17806. Add('{$modeswitch externalclass}');
  17807. Add('type');
  17808. Add(' TJSElement = class;');
  17809. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17810. Add(' TJSElement = class external name ''ExtA''');
  17811. Add(' end;');
  17812. Add(' TControl = class(TJSElement)');
  17813. Add(' private');
  17814. Add(' FOnClick: TJSNotifyEvent;');
  17815. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17816. Add(' procedure Click(Sender: TJSElement);');
  17817. Add(' end;');
  17818. Add('procedure TControl.Click(Sender: TJSElement);');
  17819. Add('begin');
  17820. Add(' OnClick(Self);');
  17821. Add('end;');
  17822. Add('var');
  17823. Add(' Ctrl: TControl;');
  17824. Add('begin');
  17825. Add(' Ctrl.OnClick:[email protected];');
  17826. Add(' Ctrl.OnClick(Ctrl);');
  17827. ConvertProgram;
  17828. CheckSource('TestExternalClass_PascalProperty',
  17829. LinesToStr([ // statements
  17830. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17831. ' this.$init = function () {',
  17832. ' this.FOnClick = null;',
  17833. ' };',
  17834. ' this.$final = function () {',
  17835. ' this.FOnClick = undefined;',
  17836. ' };',
  17837. ' this.Click = function (Sender) {',
  17838. ' this.FOnClick(this);',
  17839. ' };',
  17840. '});',
  17841. 'this.Ctrl = null;',
  17842. '']),
  17843. LinesToStr([ // $mod.$main
  17844. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17845. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17846. '']));
  17847. end;
  17848. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17849. begin
  17850. StartProgram(false);
  17851. Add([
  17852. '{$modeswitch externalclass}',
  17853. 'type',
  17854. ' IUnknown = interface end;',
  17855. ' TObject = class',
  17856. ' end;',
  17857. ' TChild = class',
  17858. ' end;',
  17859. ' TExtRootA = class external name ''ExtRootA''',
  17860. ' end;',
  17861. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17862. ' end;',
  17863. ' TExtRootB = class external name ''ExtRootB''',
  17864. ' end;',
  17865. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17866. ' end;',
  17867. 'var',
  17868. ' Obj: TObject;',
  17869. ' Child: TChild;',
  17870. ' RootA: TExtRootA;',
  17871. ' ChildA: TExtChildA;',
  17872. ' RootB: TExtRootB;',
  17873. ' ChildB: TExtChildB;',
  17874. ' i: IUnknown;',
  17875. 'begin',
  17876. ' obj:=tobject(roota);',
  17877. ' obj:=tobject(childa);',
  17878. ' child:=tchild(tobject(roota));',
  17879. ' roota:=textroota(obj);',
  17880. ' roota:=textroota(child);',
  17881. ' roota:=textroota(rootb);',
  17882. ' roota:=textroota(childb);',
  17883. ' childa:=textchilda(textroota(obj));',
  17884. ' roota:=TExtRootA(i)',
  17885. '']);
  17886. ConvertProgram;
  17887. CheckSource('TestExternalClass_TypeCastToRootClass',
  17888. LinesToStr([ // statements
  17889. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17890. 'rtl.createClass(this, "TObject", null, function () {',
  17891. ' this.$init = function () {',
  17892. ' };',
  17893. ' this.$final = function () {',
  17894. ' };',
  17895. '});',
  17896. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17897. '});',
  17898. 'this.Obj = null;',
  17899. 'this.Child = null;',
  17900. 'this.RootA = null;',
  17901. 'this.ChildA = null;',
  17902. 'this.RootB = null;',
  17903. 'this.ChildB = null;',
  17904. 'this.i = null;',
  17905. '']),
  17906. LinesToStr([ // $mod.$main
  17907. '$mod.Obj = $mod.RootA;',
  17908. '$mod.Obj = $mod.ChildA;',
  17909. '$mod.Child = $mod.RootA;',
  17910. '$mod.RootA = $mod.Obj;',
  17911. '$mod.RootA = $mod.Child;',
  17912. '$mod.RootA = $mod.RootB;',
  17913. '$mod.RootA = $mod.ChildB;',
  17914. '$mod.ChildA = $mod.Obj;',
  17915. '$mod.RootA = $mod.i;',
  17916. '']));
  17917. end;
  17918. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17919. begin
  17920. StartProgram(false);
  17921. Add([
  17922. '{$modeswitch externalclass}',
  17923. 'type',
  17924. ' IUnknown = interface end;',
  17925. ' IBird = interface(IUnknown) end;',
  17926. ' TClass = class of TObject;',
  17927. ' TObject = class',
  17928. ' end;',
  17929. ' TChild = class',
  17930. ' end;',
  17931. ' TJSObject = class external name ''Object''',
  17932. ' end;',
  17933. ' TRec = record end;',
  17934. 'var',
  17935. ' Obj: TObject;',
  17936. ' Child: TChild;',
  17937. ' i: IUnknown;',
  17938. ' Bird: IBird;',
  17939. ' j: TJSObject;',
  17940. ' r: TRec;',
  17941. ' c: TClass;',
  17942. 'begin',
  17943. ' j:=tjsobject(IUnknown);',
  17944. ' j:=tjsobject(IBird);',
  17945. ' j:=tjsobject(TObject);',
  17946. ' j:=tjsobject(TChild);',
  17947. ' j:=tjsobject(TRec);',
  17948. ' j:=tjsobject(Obj);',
  17949. ' j:=tjsobject(Child);',
  17950. ' j:=tjsobject(i);',
  17951. ' j:=tjsobject(Bird);',
  17952. ' j:=tjsobject(r);',
  17953. ' j:=tjsobject(c);',
  17954. '']);
  17955. ConvertProgram;
  17956. CheckSource('TestExternalClass_TypeCastToJSObject',
  17957. LinesToStr([ // statements
  17958. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17959. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17960. 'rtl.createClass(this, "TObject", null, function () {',
  17961. ' this.$init = function () {',
  17962. ' };',
  17963. ' this.$final = function () {',
  17964. ' };',
  17965. '});',
  17966. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17967. '});',
  17968. 'rtl.recNewT(this, "TRec", function () {',
  17969. ' this.$eq = function (b) {',
  17970. ' return true;',
  17971. ' };',
  17972. ' this.$assign = function (s) {',
  17973. ' return this;',
  17974. ' };',
  17975. '});',
  17976. 'this.Obj = null;',
  17977. 'this.Child = null;',
  17978. 'this.i = null;',
  17979. 'this.Bird = null;',
  17980. 'this.j = null;',
  17981. 'this.r = this.TRec.$new();',
  17982. 'this.c = null;',
  17983. '']),
  17984. LinesToStr([ // $mod.$main
  17985. '$mod.j = $mod.IUnknown;',
  17986. '$mod.j = $mod.IBird;',
  17987. '$mod.j = $mod.TObject;',
  17988. '$mod.j = $mod.TChild;',
  17989. '$mod.j = $mod.TRec;',
  17990. '$mod.j = $mod.Obj;',
  17991. '$mod.j = $mod.Child;',
  17992. '$mod.j = $mod.i;',
  17993. '$mod.j = $mod.Bird;',
  17994. '$mod.j = $mod.r;',
  17995. '$mod.j = $mod.c;',
  17996. '']));
  17997. end;
  17998. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17999. begin
  18000. StartProgram(false);
  18001. Add('{$modeswitch externalclass}');
  18002. Add('type');
  18003. Add(' TJSString = class external name ''String''');
  18004. Add(' class function fromCharCode() : string; varargs;');
  18005. Add(' function anchor(const aName : string) : string;');
  18006. Add(' end;');
  18007. Add('var');
  18008. Add(' s: string;');
  18009. Add('begin');
  18010. Add(' s:=TJSString.fromCharCode(65,66);');
  18011. Add(' s:=TJSString(s).anchor(s);');
  18012. Add(' s:=TJSString(''foo'').anchor(s);');
  18013. ConvertProgram;
  18014. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18015. LinesToStr([ // statements
  18016. 'this.s = "";',
  18017. '']),
  18018. LinesToStr([ // $mod.$main
  18019. '$mod.s = String.fromCharCode(65, 66);',
  18020. '$mod.s = $mod.s.anchor($mod.s);',
  18021. '$mod.s = "foo".anchor($mod.s);',
  18022. '']));
  18023. end;
  18024. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18025. begin
  18026. StartProgram(false);
  18027. Add([
  18028. '{$modeswitch externalclass}',
  18029. 'type',
  18030. ' TJSObject = class external name ''Object'' end;',
  18031. ' TJSFunction = class external name ''Function''',
  18032. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18033. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18034. ' end;',
  18035. ' TObject = class',
  18036. ' procedure DoIt(i: longint);',
  18037. ' end;',
  18038. ' TFuncInt = function(o: TObject): longint;',
  18039. 'function GetIt(o: TObject): longint;',
  18040. ' procedure Sub; begin end;',
  18041. 'var',
  18042. ' f: TJSFunction;',
  18043. ' fi: TFuncInt;',
  18044. 'begin',
  18045. ' fi:=TFuncInt(f);',
  18046. ' f:=TJSFunction(fi);',
  18047. ' f:=TJSFunction(@GetIt);',
  18048. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18049. ' f:=TJSFunction(@Sub);',
  18050. ' f:=TJSFunction(@o.doit);',
  18051. ' f:=TJSFunction(fi).bind(nil,4)',
  18052. 'end;',
  18053. 'procedure TObject.DoIt(i: longint);',
  18054. ' procedure Sub; begin end;',
  18055. 'var f: TJSFunction;',
  18056. 'begin',
  18057. ' f:=TJSFunction(@DoIt);',
  18058. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18059. ' f:=TJSFunction(@Sub);',
  18060. ' f:=TJSFunction(@GetIt);',
  18061. 'end;',
  18062. 'begin']);
  18063. ConvertProgram;
  18064. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18065. LinesToStr([ // statements
  18066. 'rtl.createClass(this, "TObject", null, function () {',
  18067. ' this.$init = function () {',
  18068. ' };',
  18069. ' this.$final = function () {',
  18070. ' };',
  18071. ' this.DoIt = function (i) {',
  18072. ' var $Self = this;',
  18073. ' function Sub() {',
  18074. ' };',
  18075. ' var f = null;',
  18076. ' f = this.DoIt;',
  18077. ' f = this.DoIt.bind(null, 13);',
  18078. ' f = Sub;',
  18079. ' f = $mod.GetIt;',
  18080. ' };',
  18081. '});',
  18082. 'this.GetIt = function (o) {',
  18083. ' var Result = 0;',
  18084. ' function Sub() {',
  18085. ' };',
  18086. ' var f = null;',
  18087. ' var fi = null;',
  18088. ' fi = f;',
  18089. ' f = fi;',
  18090. ' f = $mod.GetIt;',
  18091. ' f = $mod.GetIt.bind(null, 3);',
  18092. ' f = Sub;',
  18093. ' f = $mod.TObject.DoIt;',
  18094. ' f = fi.bind(null, 4);',
  18095. ' return Result;',
  18096. '};',
  18097. '']),
  18098. LinesToStr([ // $mod.$main
  18099. '']));
  18100. end;
  18101. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18102. begin
  18103. StartProgram(false);
  18104. Add([
  18105. '{$mode delphi}',
  18106. '{$modeswitch externalclass}',
  18107. 'type',
  18108. ' TJSObject = class external name ''Object'' end;',
  18109. ' TJSWindow = class external name ''Window''(TJSObject)',
  18110. ' procedure Open;',
  18111. ' end;',
  18112. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18113. ' procedure Execute;',
  18114. ' end;',
  18115. 'procedure Fly;',
  18116. 'var',
  18117. ' w: TJSWindow;',
  18118. ' e: TJSEventTarget;',
  18119. 'begin',
  18120. ' w:=TJSWindow(e);',
  18121. ' e:=TJSEventTarget(w);',
  18122. 'end;',
  18123. 'begin']);
  18124. ConvertProgram;
  18125. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18126. LinesToStr([ // statements
  18127. 'this.Fly = function () {',
  18128. ' var w = null;',
  18129. ' var e = null;',
  18130. ' w = e;',
  18131. ' e = w;',
  18132. '};',
  18133. '']),
  18134. LinesToStr([ // $mod.$main
  18135. '']));
  18136. end;
  18137. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18138. begin
  18139. StartProgram(false);
  18140. Add('{$modeswitch externalclass}');
  18141. Add('type');
  18142. Add(' TJSString = class external name ''String''');
  18143. Add(' class function fromCharCode() : string; varargs;');
  18144. Add(' end;');
  18145. Add('var');
  18146. Add(' s: string;');
  18147. Add(' sObj: TJSString;');
  18148. Add('begin');
  18149. Add(' s:=sObj.fromCharCode(65,66);');
  18150. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18151. nExternalClassInstanceCannotAccessStaticX);
  18152. ConvertProgram;
  18153. end;
  18154. procedure TTestModule.TestExternalClass_BracketAccessor;
  18155. begin
  18156. StartProgram(false);
  18157. Add([
  18158. '{$modeswitch externalclass}',
  18159. 'type',
  18160. ' TJSArray = class external name ''Array2''',
  18161. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18162. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18163. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18164. ' end;',
  18165. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18166. 'begin end;',
  18167. 'var',
  18168. ' Arr: tjsarray;',
  18169. ' s: string;',
  18170. ' i: longint;',
  18171. ' v: jsvalue;',
  18172. 'begin',
  18173. ' v:=arr[0];',
  18174. ' v:=arr.items[1];',
  18175. ' arr[2]:=s;',
  18176. ' arr.items[3]:=s;',
  18177. ' arr[4]:=i;',
  18178. ' arr[5]:=arr[6];',
  18179. ' arr.items[7]:=arr.items[8];',
  18180. ' with arr do items[9]:=items[10];',
  18181. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18182. ' with arr do begin',
  18183. ' v:=GetItems(14);',
  18184. ' setitems(15,16);',
  18185. ' end;',
  18186. ' v:=test1.arr.items[17];',
  18187. ' test1.arr.items[18]:=v;',
  18188. '']);
  18189. ConvertProgram;
  18190. CheckSource('TestExternalClass_BracketAccessor',
  18191. LinesToStr([ // statements
  18192. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18193. '};',
  18194. 'this.Arr = null;',
  18195. 'this.s = "";',
  18196. 'this.i = 0;',
  18197. 'this.v = undefined;',
  18198. '']),
  18199. LinesToStr([ // $mod.$main
  18200. '$mod.v = $mod.Arr[0];',
  18201. '$mod.v = $mod.Arr[1];',
  18202. '$mod.Arr[2] = $mod.s;',
  18203. '$mod.Arr[3] = $mod.s;',
  18204. '$mod.Arr[4] = $mod.i;',
  18205. '$mod.Arr[5] = $mod.Arr[6];',
  18206. '$mod.Arr[7] = $mod.Arr[8];',
  18207. 'var $with = $mod.Arr;',
  18208. '$with[9] = $with[10];',
  18209. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18210. ' a: 9,',
  18211. ' p: $mod.Arr,',
  18212. ' get: function () {',
  18213. ' return this.p[this.a];',
  18214. ' },',
  18215. ' set: function (v) {',
  18216. ' this.p[this.a] = v;',
  18217. ' }',
  18218. '}, {',
  18219. ' a: 10,',
  18220. ' p: $mod.Arr,',
  18221. ' get: function () {',
  18222. ' return this.p[this.a];',
  18223. ' },',
  18224. ' set: function (v) {',
  18225. ' this.p[this.a] = v;',
  18226. ' }',
  18227. '});',
  18228. 'var $with1 = $mod.Arr;',
  18229. '$mod.v = $with1[14];',
  18230. '$with1[15] = 16;',
  18231. '$mod.v = $mod.Arr[17];',
  18232. '$mod.Arr[18] = $mod.v;',
  18233. '']));
  18234. end;
  18235. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18236. begin
  18237. StartProgram(false);
  18238. Add([
  18239. '{$modeswitch externalclass}',
  18240. 'type',
  18241. ' TJSArray = class external name ''Array2''',
  18242. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18243. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18244. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18245. ' end;',
  18246. ' TMyArr = class(TJSArray)',
  18247. ' procedure DoIt;',
  18248. ' end;',
  18249. 'procedure tmyarr.DoIt;',
  18250. 'begin',
  18251. ' Items[1]:=Items[2];',
  18252. ' SetItems(3,getItems(4));',
  18253. 'end;',
  18254. 'var',
  18255. ' Arr: tmyarr;',
  18256. ' s: string;',
  18257. ' i: longint;',
  18258. ' v: jsvalue;',
  18259. 'begin',
  18260. ' v:=arr[0];',
  18261. ' v:=arr.items[1];',
  18262. ' arr[2]:=s;',
  18263. ' arr.items[3]:=s;',
  18264. ' arr[4]:=i;',
  18265. ' arr[5]:=arr[6];',
  18266. ' arr.items[7]:=arr.items[8];',
  18267. ' with arr do items[9]:=items[10];',
  18268. ' with arr do begin',
  18269. ' v:=GetItems(14);',
  18270. ' setitems(15,16);',
  18271. ' end;',
  18272. '']);
  18273. ConvertProgram;
  18274. CheckSource('TestExternalClass_BracketAccessor_Call',
  18275. LinesToStr([ // statements
  18276. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18277. ' this.$init = function () {',
  18278. ' };',
  18279. ' this.$final = function () {',
  18280. ' };',
  18281. ' this.DoIt = function () {',
  18282. ' this[1] = this[2];',
  18283. ' this[3] = this[4];',
  18284. ' };',
  18285. '});',
  18286. 'this.Arr = null;',
  18287. 'this.s = "";',
  18288. 'this.i = 0;',
  18289. 'this.v = undefined;',
  18290. '']),
  18291. LinesToStr([ // $mod.$main
  18292. '$mod.v = $mod.Arr[0];',
  18293. '$mod.v = $mod.Arr[1];',
  18294. '$mod.Arr[2] = $mod.s;',
  18295. '$mod.Arr[3] = $mod.s;',
  18296. '$mod.Arr[4] = $mod.i;',
  18297. '$mod.Arr[5] = $mod.Arr[6];',
  18298. '$mod.Arr[7] = $mod.Arr[8];',
  18299. 'var $with = $mod.Arr;',
  18300. '$with[9] = $with[10];',
  18301. 'var $with1 = $mod.Arr;',
  18302. '$mod.v = $with1[14];',
  18303. '$with1[15] = 16;',
  18304. '']));
  18305. end;
  18306. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18307. begin
  18308. StartProgram(false);
  18309. Add('{$modeswitch externalclass}');
  18310. Add('type');
  18311. Add(' TJSArray = class external name ''Array2''');
  18312. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18313. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18314. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18315. Add(' end;');
  18316. Add('begin');
  18317. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18318. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18319. ConvertProgram;
  18320. end;
  18321. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18322. begin
  18323. StartProgram(false);
  18324. Add('{$modeswitch externalclass}');
  18325. Add('type');
  18326. Add(' TJSArray = class external name ''Array2''');
  18327. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18328. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18329. Add(' end;');
  18330. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18331. Add('begin end;');
  18332. Add('var');
  18333. Add(' Arr: tjsarray;');
  18334. Add(' v: jsvalue;');
  18335. Add('begin');
  18336. Add(' v:=arr[0];');
  18337. Add(' v:=arr.items[1];');
  18338. Add(' with arr do v:=items[2];');
  18339. Add(' doit(arr[3],arr[4]);');
  18340. ConvertProgram;
  18341. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18342. LinesToStr([ // statements
  18343. 'this.DoIt = function (vI, vJ) {',
  18344. '};',
  18345. 'this.Arr = null;',
  18346. 'this.v = undefined;',
  18347. '']),
  18348. LinesToStr([ // $mod.$main
  18349. '$mod.v = $mod.Arr[0];',
  18350. '$mod.v = $mod.Arr[1];',
  18351. 'var $with = $mod.Arr;',
  18352. '$mod.v = $with[2];',
  18353. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18354. '']));
  18355. end;
  18356. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18357. begin
  18358. StartProgram(false);
  18359. Add('{$modeswitch externalclass}');
  18360. Add('type');
  18361. Add(' TJSArray = class external name ''Array2''');
  18362. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18363. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18364. Add(' end;');
  18365. Add('var');
  18366. Add(' Arr: tjsarray;');
  18367. Add(' s: string;');
  18368. Add(' i: longint;');
  18369. Add(' v: jsvalue;');
  18370. Add('begin');
  18371. Add(' arr[2]:=s;');
  18372. Add(' arr.items[3]:=s;');
  18373. Add(' arr[4]:=i;');
  18374. Add(' with arr do items[5]:=i;');
  18375. ConvertProgram;
  18376. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18377. LinesToStr([ // statements
  18378. 'this.Arr = null;',
  18379. 'this.s = "";',
  18380. 'this.i = 0;',
  18381. 'this.v = undefined;',
  18382. '']),
  18383. LinesToStr([ // $mod.$main
  18384. '$mod.Arr[2] = $mod.s;',
  18385. '$mod.Arr[3] = $mod.s;',
  18386. '$mod.Arr[4] = $mod.i;',
  18387. 'var $with = $mod.Arr;',
  18388. '$with[5] = $mod.i;',
  18389. '']));
  18390. end;
  18391. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18392. begin
  18393. StartProgram(false);
  18394. Add('{$modeswitch externalclass}');
  18395. Add('type');
  18396. Add(' TJSArray = class external name ''Array2''');
  18397. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18398. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18399. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18400. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18401. Add(' end;');
  18402. Add('var');
  18403. Add(' Arr: tjsarray;');
  18404. Add(' s: string;');
  18405. Add(' i: longint;');
  18406. Add(' v: jsvalue;');
  18407. Add('begin');
  18408. Add(' arr[2]:=s;');
  18409. Add(' arr.items[3]:=s;');
  18410. Add(' arr.numbers[4]:=i;');
  18411. Add(' with arr do items[5]:=i;');
  18412. Add(' with arr do numbers[6]:=i;');
  18413. ConvertProgram;
  18414. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18415. LinesToStr([ // statements
  18416. 'this.Arr = null;',
  18417. 'this.s = "";',
  18418. 'this.i = 0;',
  18419. 'this.v = undefined;',
  18420. '']),
  18421. LinesToStr([ // $mod.$main
  18422. '$mod.Arr[2] = $mod.s;',
  18423. '$mod.Arr[3] = $mod.s;',
  18424. '$mod.Arr[4] = $mod.i;',
  18425. 'var $with = $mod.Arr;',
  18426. '$with[5] = $mod.i;',
  18427. 'var $with1 = $mod.Arr;',
  18428. '$with1[6] = $mod.i;',
  18429. '']));
  18430. end;
  18431. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18432. begin
  18433. StartProgram(false);
  18434. Add('{$modeswitch externalclass}');
  18435. Add('type');
  18436. Add(' TJSArray = class external name ''Array2''');
  18437. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18438. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18439. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18440. Add(' end;');
  18441. Add('var');
  18442. Add(' Arr: tjsarray;');
  18443. Add(' i: longint;');
  18444. Add(' IntArr: array of longint;');
  18445. Add(' v: jsvalue;');
  18446. Add('begin');
  18447. Add(' v:=arr.items[i];');
  18448. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18449. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18450. ConvertProgram;
  18451. CheckSource('TestExternalClass_BracketAccessor_Index',
  18452. LinesToStr([ // statements
  18453. 'this.Arr = null;',
  18454. 'this.i = 0;',
  18455. 'this.IntArr = [];',
  18456. 'this.v = undefined;',
  18457. '']),
  18458. LinesToStr([ // $mod.$main
  18459. '$mod.v = $mod.Arr[$mod.i];',
  18460. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18461. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18462. '']));
  18463. end;
  18464. procedure TTestModule.TestExternalClass_ForInJSObject;
  18465. begin
  18466. StartProgram(false);
  18467. Add([
  18468. '{$modeswitch externalclass}',
  18469. 'type',
  18470. ' TJSObject = class external name ''Object''',
  18471. ' end;',
  18472. 'var',
  18473. ' o: TJSObject;',
  18474. ' key: string;',
  18475. 'begin',
  18476. ' for key in o do',
  18477. ' if key=''abc'' then ;',
  18478. '']);
  18479. ConvertProgram;
  18480. CheckSource('TestExternalClass_ForInJSObject',
  18481. LinesToStr([ // statements
  18482. 'this.o = null;',
  18483. 'this.key = "";',
  18484. '']),
  18485. LinesToStr([ // $mod.$main
  18486. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18487. '']));
  18488. end;
  18489. procedure TTestModule.TestExternalClass_ForInJSArray;
  18490. begin
  18491. StartProgram(false);
  18492. Add([
  18493. '{$modeswitch externalclass}',
  18494. 'type',
  18495. ' TJSInt8Array = class external name ''Int8Array''',
  18496. ' private',
  18497. ' flength: NativeInt external name ''length'';',
  18498. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18499. ' public',
  18500. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18501. ' property Length: NativeInt read flength;',
  18502. ' end;',
  18503. 'var',
  18504. ' a: TJSInt8Array;',
  18505. ' value: shortint;',
  18506. 'begin',
  18507. ' for value in a do',
  18508. ' if value=3 then ;',
  18509. '']);
  18510. ConvertProgram;
  18511. CheckSource('TestExternalClass_ForInJSArray',
  18512. LinesToStr([ // statements
  18513. 'this.a = null;',
  18514. 'this.value = 0;',
  18515. '']),
  18516. LinesToStr([ // $mod.$main
  18517. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18518. ' $mod.value = $in[$l];',
  18519. ' if ($mod.value === 3) ;',
  18520. '};',
  18521. '']));
  18522. end;
  18523. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18524. begin
  18525. AddModuleWithIntfImplSrc('unit2.pas',
  18526. LinesToStr([
  18527. '{$modeswitch externalclass}',
  18528. 'type',
  18529. ' TJSBufferSource = class external name ''BufferSource''',
  18530. ' end;',
  18531. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18532. '']),
  18533. '');
  18534. AddModuleWithIntfImplSrc('unit3.pas',
  18535. LinesToStr([
  18536. '{$modeswitch externalclass}',
  18537. 'type',
  18538. ' TJSBufferSource = class external name ''BufferSource''',
  18539. ' end;',
  18540. '']),
  18541. '');
  18542. StartUnit(true);
  18543. Add([
  18544. 'interface',
  18545. 'uses unit2, unit3;',
  18546. 'procedure DoSome(s: TJSBufferSource);',
  18547. 'implementation',
  18548. 'procedure DoSome(s: TJSBufferSource);',
  18549. 'begin',
  18550. ' DoIt(s);',
  18551. 'end;',
  18552. '']);
  18553. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18554. nIncompatibleTypeArgNo);
  18555. ConvertUnit;
  18556. end;
  18557. procedure TTestModule.TestClassInterface_Corba;
  18558. begin
  18559. StartProgram(false);
  18560. Add([
  18561. '{$interfaces corba}',
  18562. 'type',
  18563. ' IUnknown = interface;',
  18564. ' IUnknown = interface',
  18565. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18566. ' end;',
  18567. ' IInterface = IUnknown;',
  18568. ' IBird = interface(IInterface)',
  18569. ' function GetSize: longint;',
  18570. ' procedure SetSize(i: longint);',
  18571. ' property Size: longint read GetSize write SetSize;',
  18572. ' procedure DoIt(i: longint);',
  18573. ' end;',
  18574. ' TObject = class',
  18575. ' end;',
  18576. ' TBird = class(TObject,IBird)',
  18577. ' function GetSize: longint; virtual; abstract;',
  18578. ' procedure SetSize(i: longint); virtual; abstract;',
  18579. ' procedure DoIt(i: longint); virtual; abstract;',
  18580. ' end;',
  18581. 'var',
  18582. ' BirdIntf: IBird;',
  18583. 'begin',
  18584. ' BirdIntf.Size:=BirdIntf.Size;',
  18585. '']);
  18586. ConvertProgram;
  18587. CheckSource('TestClassInterface_Corba',
  18588. LinesToStr([ // statements
  18589. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18590. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18591. 'rtl.createClass(this, "TObject", null, function () {',
  18592. ' this.$init = function () {',
  18593. ' };',
  18594. ' this.$final = function () {',
  18595. ' };',
  18596. '});',
  18597. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18598. ' rtl.addIntf(this, $mod.IBird);',
  18599. '});',
  18600. 'this.BirdIntf = null;',
  18601. '']),
  18602. LinesToStr([ // $mod.$main
  18603. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18604. '']));
  18605. end;
  18606. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18607. begin
  18608. StartProgram(false);
  18609. Add([
  18610. '{$interfaces corba}',
  18611. 'type',
  18612. ' IUnknown = interface',
  18613. ' procedure DoIt; external name ''foo'';',
  18614. ' end;',
  18615. 'begin']);
  18616. SetExpectedParserError(
  18617. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18618. nParserNoFieldsAllowed);
  18619. ConvertProgram;
  18620. end;
  18621. procedure TTestModule.TestClassInterface_Overloads;
  18622. begin
  18623. StartProgram(false);
  18624. Add([
  18625. '{$interfaces corba}',
  18626. 'type',
  18627. ' integer = longint;',
  18628. ' IUnknown = interface',
  18629. ' procedure DoIt(i: integer);',
  18630. ' procedure DoIt(s: string);',
  18631. ' end;',
  18632. ' IBird = interface(IUnknown)',
  18633. ' procedure DoIt(b: boolean); overload;',
  18634. ' end;',
  18635. ' TObject = class',
  18636. ' end;',
  18637. ' TBird = class(TObject,IBird)',
  18638. ' procedure DoIt(o: TObject);',
  18639. ' procedure DoIt(s: string);',
  18640. ' procedure DoIt(i: integer);',
  18641. ' procedure DoIt(b: boolean);',
  18642. ' end;',
  18643. 'procedure TBird.DoIt(o: TObject); begin end;',
  18644. 'procedure TBird.DoIt(s: string); begin end;',
  18645. 'procedure TBird.DoIt(i: integer); begin end;',
  18646. 'procedure TBird.DoIt(b: boolean); begin end;',
  18647. 'var',
  18648. ' BirdIntf: IBird;',
  18649. 'begin',
  18650. ' BirdIntf.DoIt(3);',
  18651. ' BirdIntf.DoIt(''abc'');',
  18652. ' BirdIntf.DoIt(true);',
  18653. '']);
  18654. ConvertProgram;
  18655. CheckSource('TestClassInterface_Overloads',
  18656. LinesToStr([ // statements
  18657. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18658. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18659. 'rtl.createClass(this, "TObject", null, function () {',
  18660. ' this.$init = function () {',
  18661. ' };',
  18662. ' this.$final = function () {',
  18663. ' };',
  18664. '});',
  18665. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18666. ' this.DoIt = function (o) {',
  18667. ' };',
  18668. ' this.DoIt$1 = function (s) {',
  18669. ' };',
  18670. ' this.DoIt$2 = function (i) {',
  18671. ' };',
  18672. ' this.DoIt$3 = function (b) {',
  18673. ' };',
  18674. ' rtl.addIntf(this, $mod.IBird, {',
  18675. ' DoIt$2: "DoIt$3",',
  18676. ' DoIt: "DoIt$2"',
  18677. ' });',
  18678. '});',
  18679. 'this.BirdIntf = null;',
  18680. '']),
  18681. LinesToStr([ // $mod.$main
  18682. '$mod.BirdIntf.DoIt(3);',
  18683. '$mod.BirdIntf.DoIt$1("abc");',
  18684. '$mod.BirdIntf.DoIt$2(true);',
  18685. '']));
  18686. end;
  18687. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18688. begin
  18689. StartProgram(false);
  18690. Add([
  18691. '{$interfaces corba}',
  18692. 'type',
  18693. ' IBird = interface',
  18694. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18695. ' end;',
  18696. ' IDog = interface',
  18697. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18698. ' end;',
  18699. ' TObject = class(IBird,IDog)',
  18700. ' end;',
  18701. 'begin']);
  18702. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18703. nDuplicateGUIDXInYZ);
  18704. ConvertProgram;
  18705. end;
  18706. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18707. begin
  18708. StartProgram(false);
  18709. Add([
  18710. '{$interfaces corba}',
  18711. 'type',
  18712. ' IAnimal = interface',
  18713. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18714. ' end;',
  18715. ' IBird = interface(IAnimal)',
  18716. ' end;',
  18717. ' IHawk = interface(IBird)',
  18718. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18719. ' end;',
  18720. 'begin']);
  18721. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18722. nDuplicateGUIDXInYZ);
  18723. ConvertProgram;
  18724. end;
  18725. procedure TTestModule.TestClassInterface_AncestorImpl;
  18726. begin
  18727. StartProgram(false);
  18728. Add([
  18729. '{$interfaces corba}',
  18730. 'type',
  18731. ' integer = longint;',
  18732. ' IUnknown = interface',
  18733. ' procedure DoIt(i: integer);',
  18734. ' end;',
  18735. ' IBird = interface',
  18736. ' procedure Fly(i: integer);',
  18737. ' end;',
  18738. ' TObject = class(IUnknown)',
  18739. ' procedure DoIt(i: integer);',
  18740. ' end;',
  18741. ' TBird = class(IBird)',
  18742. ' procedure Fly(i: integer);',
  18743. ' end;',
  18744. 'procedure TObject.DoIt(i: integer); begin end;',
  18745. 'procedure TBird.Fly(i: integer); begin end;',
  18746. 'begin',
  18747. '']);
  18748. ConvertProgram;
  18749. CheckSource('TestClassInterface_AncestorIntf',
  18750. LinesToStr([ // statements
  18751. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18752. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18753. 'rtl.createClass(this, "TObject", null, function () {',
  18754. ' this.$init = function () {',
  18755. ' };',
  18756. ' this.$final = function () {',
  18757. ' };',
  18758. ' this.DoIt = function (i) {',
  18759. ' };',
  18760. ' rtl.addIntf(this, $mod.IUnknown);',
  18761. '});',
  18762. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18763. ' this.Fly = function (i) {',
  18764. ' };',
  18765. ' rtl.addIntf(this, $mod.IBird);',
  18766. ' rtl.addIntf(this, $mod.IUnknown);',
  18767. '});',
  18768. '']),
  18769. LinesToStr([ // $mod.$main
  18770. '']));
  18771. end;
  18772. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18773. begin
  18774. StartProgram(false);
  18775. Add([
  18776. '{$interfaces corba}',
  18777. 'type',
  18778. ' integer = longint;',
  18779. ' IBird = interface',
  18780. ' procedure DoIt(i: integer);',
  18781. ' end;',
  18782. ' TObject = class',
  18783. ' procedure DoIt(i: integer);',
  18784. ' end;',
  18785. ' TBird = class(IBird)',
  18786. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18787. ' end;',
  18788. 'procedure TObject.DoIt(i: integer); begin end;',
  18789. 'procedure TBird.DoIt(i: integer); begin end;',
  18790. 'begin',
  18791. '']);
  18792. ConvertProgram;
  18793. CheckSource('TestClassInterface_ImplReintroduce',
  18794. LinesToStr([ // statements
  18795. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18796. 'rtl.createClass(this, "TObject", null, function () {',
  18797. ' this.$init = function () {',
  18798. ' };',
  18799. ' this.$final = function () {',
  18800. ' };',
  18801. ' this.DoIt = function (i) {',
  18802. ' };',
  18803. '});',
  18804. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18805. ' this.DoIt$1 = function (i) {',
  18806. ' };',
  18807. ' rtl.addIntf(this, $mod.IBird, {',
  18808. ' DoIt: "DoIt$1"',
  18809. ' });',
  18810. '});',
  18811. '']),
  18812. LinesToStr([ // $mod.$main
  18813. '']));
  18814. end;
  18815. procedure TTestModule.TestClassInterface_MethodResolution;
  18816. begin
  18817. StartProgram(false);
  18818. Add([
  18819. '{$interfaces corba}',
  18820. 'type',
  18821. ' IUnknown = interface',
  18822. ' procedure Walk(i: longint);',
  18823. ' end;',
  18824. ' IBird = interface(IUnknown)',
  18825. ' procedure Walk(b: boolean); overload;',
  18826. ' procedure Fly(s: string);',
  18827. ' end;',
  18828. ' TObject = class',
  18829. ' end;',
  18830. ' TBird = class(TObject,IBird)',
  18831. ' procedure IBird.Fly = Move;',
  18832. ' procedure IBird.Walk = Hop;',
  18833. ' procedure Hop(i: longint);',
  18834. ' procedure Move(s: string);',
  18835. ' procedure Hop(b: boolean);',
  18836. ' end;',
  18837. 'procedure TBird.Move(s: string); begin end;',
  18838. 'procedure TBird.Hop(i: longint); begin end;',
  18839. 'procedure TBird.Hop(b: boolean); begin end;',
  18840. 'var',
  18841. ' BirdIntf: IBird;',
  18842. 'begin',
  18843. ' BirdIntf.Walk(3);',
  18844. ' BirdIntf.Walk(true);',
  18845. ' BirdIntf.Fly(''abc'');',
  18846. '']);
  18847. ConvertProgram;
  18848. CheckSource('TestClassInterface_MethodResolution',
  18849. LinesToStr([ // statements
  18850. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18851. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18852. 'rtl.createClass(this, "TObject", null, function () {',
  18853. ' this.$init = function () {',
  18854. ' };',
  18855. ' this.$final = function () {',
  18856. ' };',
  18857. '});',
  18858. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18859. ' this.Hop = function (i) {',
  18860. ' };',
  18861. ' this.Move = function (s) {',
  18862. ' };',
  18863. ' this.Hop$1 = function (b) {',
  18864. ' };',
  18865. ' rtl.addIntf(this, $mod.IBird, {',
  18866. ' Walk$1: "Hop$1",',
  18867. ' Fly: "Move",',
  18868. ' Walk: "Hop"',
  18869. ' });',
  18870. '});',
  18871. 'this.BirdIntf = null;',
  18872. '']),
  18873. LinesToStr([ // $mod.$main
  18874. '$mod.BirdIntf.Walk(3);',
  18875. '$mod.BirdIntf.Walk$1(true);',
  18876. '$mod.BirdIntf.Fly("abc");',
  18877. '']));
  18878. end;
  18879. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18880. begin
  18881. StartProgram(false);
  18882. Add([
  18883. '{$interfaces com}',
  18884. 'type',
  18885. ' IUnknown = interface',
  18886. ' function _AddRef: longint;',
  18887. ' procedure Walk;',
  18888. ' end;',
  18889. ' IBird = interface end;',
  18890. ' IDog = interface end;',
  18891. ' TObject = class(IBird,IDog)',
  18892. ' function _AddRef: longint; virtual; abstract;',
  18893. ' procedure Walk; virtual; abstract;',
  18894. ' end;',
  18895. ' TBird = class(IUnknown)',
  18896. ' end;',
  18897. 'begin',
  18898. '']);
  18899. ConvertProgram;
  18900. CheckSource('TestClassInterface_COM_AncestorLess',
  18901. LinesToStr([ // statements
  18902. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18903. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18904. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18905. 'rtl.createClass(this, "TObject", null, function () {',
  18906. ' this.$init = function () {',
  18907. ' };',
  18908. ' this.$final = function () {',
  18909. ' };',
  18910. ' rtl.addIntf(this, $mod.IBird);',
  18911. ' rtl.addIntf(this, $mod.IDog);',
  18912. '});',
  18913. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18914. ' rtl.addIntf(this, $mod.IUnknown);',
  18915. ' rtl.addIntf(this, $mod.IBird);',
  18916. ' rtl.addIntf(this, $mod.IDog);',
  18917. '});',
  18918. '']),
  18919. LinesToStr([ // $mod.$main
  18920. '']));
  18921. end;
  18922. procedure TTestModule.TestClassInterface_MethodOverride;
  18923. begin
  18924. StartProgram(false);
  18925. Add([
  18926. '{$interfaces corba}',
  18927. 'type',
  18928. ' IUnknown = interface',
  18929. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18930. ' procedure Go;',
  18931. ' end;',
  18932. ' TObject = class(IUnknown)',
  18933. ' procedure Go; virtual; abstract;',
  18934. ' end;',
  18935. ' TBird = class',
  18936. ' procedure Go; override;',
  18937. ' end;',
  18938. ' TCat = class(TObject)',
  18939. ' procedure Go; override;',
  18940. ' end;',
  18941. ' TDog = class(TObject, IUnknown)',
  18942. ' procedure Go; override;',
  18943. ' end;',
  18944. 'procedure TBird.Go; begin end;',
  18945. 'procedure TCat.Go; begin end;',
  18946. 'procedure TDog.Go; begin end;',
  18947. 'begin',
  18948. '']);
  18949. ConvertProgram;
  18950. CheckSource('TestClassInterface_MethodOverride',
  18951. LinesToStr([ // statements
  18952. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18953. 'rtl.createClass(this, "TObject", null, function () {',
  18954. ' this.$init = function () {',
  18955. ' };',
  18956. ' this.$final = function () {',
  18957. ' };',
  18958. ' rtl.addIntf(this, $mod.IUnknown);',
  18959. '});',
  18960. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18961. ' this.Go = function () {',
  18962. ' };',
  18963. ' rtl.addIntf(this, $mod.IUnknown);',
  18964. '});',
  18965. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18966. ' this.Go = function () {',
  18967. ' };',
  18968. ' rtl.addIntf(this, $mod.IUnknown);',
  18969. '});',
  18970. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18971. ' this.Go = function () {',
  18972. ' };',
  18973. ' rtl.addIntf(this, $mod.IUnknown);',
  18974. '});',
  18975. '']),
  18976. LinesToStr([ // $mod.$main
  18977. '']));
  18978. end;
  18979. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18980. begin
  18981. StartProgram(false);
  18982. Add([
  18983. '{$interfaces corba}',
  18984. 'type',
  18985. ' IUnknown = interface',
  18986. ' end;',
  18987. ' IBird = interface(IUnknown)',
  18988. ' procedure Fly(s: string);',
  18989. ' end;',
  18990. ' IEagle = interface(IBird)',
  18991. ' end;',
  18992. ' IDove = interface(IBird)',
  18993. ' end;',
  18994. ' ISwallow = interface(IBird)',
  18995. ' end;',
  18996. ' TObject = class',
  18997. ' end;',
  18998. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18999. ' procedure Fly(s: string); virtual; abstract;',
  19000. ' end;',
  19001. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19002. ' FBirdIntf: IBird;',
  19003. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19004. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19005. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19006. ' FDoveObj: TBird;',
  19007. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19008. ' function GetSwallowObj: TBird; virtual; abstract;',
  19009. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19010. ' end;',
  19011. 'begin',
  19012. '']);
  19013. ConvertProgram;
  19014. CheckSource('TestClassInterface_Corba_Delegation',
  19015. LinesToStr([ // statements
  19016. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19017. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19018. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19019. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19020. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19021. 'rtl.createClass(this, "TObject", null, function () {',
  19022. ' this.$init = function () {',
  19023. ' };',
  19024. ' this.$final = function () {',
  19025. ' };',
  19026. '});',
  19027. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19028. ' rtl.addIntf(this, $mod.IBird);',
  19029. ' rtl.addIntf(this, $mod.IEagle);',
  19030. ' rtl.addIntf(this, $mod.IDove);',
  19031. ' rtl.addIntf(this, $mod.ISwallow);',
  19032. '});',
  19033. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19034. ' this.$init = function () {',
  19035. ' $mod.TObject.$init.call(this);',
  19036. ' this.FBirdIntf = null;',
  19037. ' this.FDoveObj = null;',
  19038. ' };',
  19039. ' this.$final = function () {',
  19040. ' this.FBirdIntf = undefined;',
  19041. ' this.FDoveObj = undefined;',
  19042. ' $mod.TObject.$final.call(this);',
  19043. ' };',
  19044. ' this.$intfmaps = {',
  19045. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19046. ' return this.FBirdIntf;',
  19047. ' },',
  19048. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19049. ' return this.GetEagleIntf();',
  19050. ' },',
  19051. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19052. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19053. ' },',
  19054. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19055. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19056. ' }',
  19057. ' };',
  19058. '});',
  19059. '']),
  19060. LinesToStr([ // $mod.$main
  19061. '']));
  19062. end;
  19063. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19064. begin
  19065. StartProgram(false);
  19066. Add([
  19067. '{$interfaces corba}',
  19068. 'type',
  19069. ' IUnknown = interface',
  19070. ' end;',
  19071. ' IBird = interface(IUnknown)',
  19072. ' procedure Fly(s: string);',
  19073. ' end;',
  19074. ' IEagle = interface(IBird)',
  19075. ' end;',
  19076. ' IDove = interface(IBird)',
  19077. ' end;',
  19078. ' ISwallow = interface(IBird)',
  19079. ' end;',
  19080. ' TObject = class',
  19081. ' end;',
  19082. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19083. ' procedure Fly(s: string); virtual; abstract;',
  19084. ' end;',
  19085. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19086. ' private',
  19087. ' class var FBirdIntf: IBird;',
  19088. ' class var FDoveObj: TBird;',
  19089. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19090. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19091. ' protected',
  19092. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19093. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19094. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19095. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19096. ' end;',
  19097. 'begin',
  19098. '']);
  19099. ConvertProgram;
  19100. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19101. LinesToStr([ // statements
  19102. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19103. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19104. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19105. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19106. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19107. 'rtl.createClass(this, "TObject", null, function () {',
  19108. ' this.$init = function () {',
  19109. ' };',
  19110. ' this.$final = function () {',
  19111. ' };',
  19112. '});',
  19113. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19114. ' rtl.addIntf(this, $mod.IBird);',
  19115. ' rtl.addIntf(this, $mod.IEagle);',
  19116. ' rtl.addIntf(this, $mod.IDove);',
  19117. ' rtl.addIntf(this, $mod.ISwallow);',
  19118. '});',
  19119. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19120. ' this.FBirdIntf = null;',
  19121. ' this.FDoveObj = null;',
  19122. ' this.$intfmaps = {',
  19123. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19124. ' return this.FBirdIntf;',
  19125. ' },',
  19126. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19127. ' return this.GetEagleIntf();',
  19128. ' },',
  19129. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19130. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19131. ' },',
  19132. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19133. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19134. ' }',
  19135. ' };',
  19136. '});',
  19137. '']),
  19138. LinesToStr([ // $mod.$main
  19139. '']));
  19140. end;
  19141. procedure TTestModule.TestClassInterface_Corba_Operators;
  19142. begin
  19143. StartProgram(false);
  19144. Add([
  19145. '{$interfaces corba}',
  19146. 'type',
  19147. ' IUnknown = interface',
  19148. ' end;',
  19149. ' IBird = interface(IUnknown)',
  19150. ' function GetItems(Index: longint): longint;',
  19151. ' procedure SetItems(Index: longint; Value: longint);',
  19152. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19153. ' end;',
  19154. ' TObject = class',
  19155. ' end;',
  19156. ' TBird = class(TObject,IBird)',
  19157. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19158. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19159. ' end;',
  19160. 'var',
  19161. ' IntfVar: IBird = nil;',
  19162. ' IntfVar2: IBird;',
  19163. ' ObjVar: TBird;',
  19164. ' v: JSValue;',
  19165. 'begin',
  19166. ' IntfVar:=nil;',
  19167. ' IntfVar[3]:=IntfVar[4];',
  19168. ' if Assigned(IntfVar) then ;',
  19169. ' IntfVar:=IntfVar2;',
  19170. ' IntfVar:=ObjVar;',
  19171. ' if IntfVar=IntfVar2 then ;',
  19172. ' if IntfVar<>IntfVar2 then ;',
  19173. ' if IntfVar is IBird then ;',
  19174. ' if IntfVar is TBird then ;',
  19175. ' if ObjVar is IBird then ;',
  19176. ' IntfVar:=IntfVar2 as IBird;',
  19177. ' ObjVar:=IntfVar2 as TBird;',
  19178. ' IntfVar:=ObjVar as IBird;',
  19179. ' IntfVar:=IBird(IntfVar2);',
  19180. ' ObjVar:=TBird(IntfVar);',
  19181. ' IntfVar:=IBird(ObjVar);',
  19182. ' v:=IntfVar;',
  19183. ' IntfVar:=IBird(v);',
  19184. ' if v is IBird then ;',
  19185. ' v:=JSValue(IntfVar);',
  19186. ' v:=IBird;',
  19187. '']);
  19188. ConvertProgram;
  19189. CheckSource('TestClassInterface_Corba_Operators',
  19190. LinesToStr([ // statements
  19191. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19192. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19193. 'rtl.createClass(this, "TObject", null, function () {',
  19194. ' this.$init = function () {',
  19195. ' };',
  19196. ' this.$final = function () {',
  19197. ' };',
  19198. '});',
  19199. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19200. ' rtl.addIntf(this, $mod.IBird);',
  19201. '});',
  19202. 'this.IntfVar = null;',
  19203. 'this.IntfVar2 = null;',
  19204. 'this.ObjVar = null;',
  19205. 'this.v = undefined;',
  19206. '']),
  19207. LinesToStr([ // $mod.$main
  19208. '$mod.IntfVar = null;',
  19209. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19210. 'if ($mod.IntfVar != null) ;',
  19211. '$mod.IntfVar = $mod.IntfVar2;',
  19212. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19213. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19214. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19215. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19216. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19217. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19218. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19219. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19220. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19221. '$mod.IntfVar = $mod.IntfVar2;',
  19222. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19223. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19224. '$mod.v = $mod.IntfVar;',
  19225. '$mod.IntfVar = rtl.getObject($mod.v);',
  19226. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19227. '$mod.v = $mod.IntfVar;',
  19228. '$mod.v = $mod.IBird;',
  19229. '']));
  19230. end;
  19231. procedure TTestModule.TestClassInterface_Corba_Args;
  19232. begin
  19233. StartProgram(false);
  19234. Add([
  19235. '{$interfaces corba}',
  19236. 'type',
  19237. ' IUnknown = interface',
  19238. ' end;',
  19239. ' IBird = interface(IUnknown)',
  19240. ' end;',
  19241. ' TObject = class',
  19242. ' end;',
  19243. ' TBird = class(TObject,IBird)',
  19244. ' end;',
  19245. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19246. 'begin',
  19247. ' DoIt(i,i,i);',
  19248. 'end;',
  19249. 'procedure Change(var i: IBird; out j: IBird);',
  19250. 'begin',
  19251. ' DoIt(i,i,i);',
  19252. ' Change(i,i);',
  19253. 'end;',
  19254. 'var',
  19255. ' i: IBird;',
  19256. ' o: TBird;',
  19257. 'begin',
  19258. ' DoIt(i,i,i);',
  19259. ' Change(i,i);',
  19260. ' DoIt(o,o,o);',
  19261. '']);
  19262. ConvertProgram;
  19263. CheckSource('TestClassInterface_Corba_Args',
  19264. LinesToStr([ // statements
  19265. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19266. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19267. 'rtl.createClass(this, "TObject", null, function () {',
  19268. ' this.$init = function () {',
  19269. ' };',
  19270. ' this.$final = function () {',
  19271. ' };',
  19272. '});',
  19273. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19274. ' rtl.addIntf(this, $mod.IBird);',
  19275. '});',
  19276. 'this.DoIt = function (u, i, j) {',
  19277. ' $mod.DoIt({',
  19278. ' get: function () {',
  19279. ' return i;',
  19280. ' },',
  19281. ' set: function (v) {',
  19282. ' i = v;',
  19283. ' }',
  19284. ' }, i, i);',
  19285. '};',
  19286. 'this.Change = function (i, j) {',
  19287. ' $mod.DoIt(i, i.get(), i.get());',
  19288. ' $mod.Change(i, i);',
  19289. '};',
  19290. 'this.i = null;',
  19291. 'this.o = null;',
  19292. '']),
  19293. LinesToStr([ // $mod.$main
  19294. '$mod.DoIt({',
  19295. ' p: $mod,',
  19296. ' get: function () {',
  19297. ' return this.p.i;',
  19298. ' },',
  19299. ' set: function (v) {',
  19300. ' this.p.i = v;',
  19301. ' }',
  19302. '}, $mod.i, $mod.i);',
  19303. '$mod.Change({',
  19304. ' p: $mod,',
  19305. ' get: function () {',
  19306. ' return this.p.i;',
  19307. ' },',
  19308. ' set: function (v) {',
  19309. ' this.p.i = v;',
  19310. ' }',
  19311. '}, {',
  19312. ' p: $mod,',
  19313. ' get: function () {',
  19314. ' return this.p.i;',
  19315. ' },',
  19316. ' set: function (v) {',
  19317. ' this.p.i = v;',
  19318. ' }',
  19319. '});',
  19320. '$mod.DoIt({',
  19321. ' p: $mod,',
  19322. ' get: function () {',
  19323. ' return this.p.o;',
  19324. ' },',
  19325. ' set: function (v) {',
  19326. ' this.p.o = v;',
  19327. ' }',
  19328. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19329. '']));
  19330. end;
  19331. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19332. begin
  19333. StartProgram(false);
  19334. Add([
  19335. '{$interfaces corba}',
  19336. 'type',
  19337. ' IUnknown = interface end;',
  19338. ' TObject = class',
  19339. ' Id: longint;',
  19340. ' end;',
  19341. ' IEnumerator = interface(IUnknown)',
  19342. ' function GetCurrent: TObject;',
  19343. ' function MoveNext: Boolean;',
  19344. ' property Current: TObject read GetCurrent;',
  19345. ' end;',
  19346. ' IEnumerable = interface(IUnknown)',
  19347. ' function GetEnumerator: IEnumerator;',
  19348. ' end;',
  19349. 'var',
  19350. ' o: TObject;',
  19351. ' i: IEnumerable;',
  19352. 'begin',
  19353. ' for o in i do o.Id:=3;',
  19354. '']);
  19355. ConvertProgram;
  19356. CheckSource('TestClassInterface_Corba_ForIn',
  19357. LinesToStr([ // statements
  19358. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19359. 'rtl.createClass(this, "TObject", null, function () {',
  19360. ' this.$init = function () {',
  19361. ' this.Id = 0;',
  19362. ' };',
  19363. ' this.$final = function () {',
  19364. ' };',
  19365. '});',
  19366. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19367. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19368. 'this.o = null;',
  19369. 'this.i = null;',
  19370. '']),
  19371. LinesToStr([ // $mod.$main
  19372. 'var $in = $mod.i.GetEnumerator();',
  19373. 'while ($in.MoveNext()) {',
  19374. ' $mod.o = $in.GetCurrent();',
  19375. ' $mod.o.Id = 3;',
  19376. '};',
  19377. '']));
  19378. end;
  19379. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19380. begin
  19381. StartProgram(false);
  19382. Add([
  19383. '{$interfaces com}',
  19384. 'type',
  19385. ' IUnknown = interface',
  19386. ' function _AddRef: longint;',
  19387. ' function _Release: longint;',
  19388. ' end;',
  19389. ' TObject = class(IUnknown)',
  19390. ' function _AddRef: longint; virtual; abstract;',
  19391. ' function _Release: longint; virtual; abstract;',
  19392. ' end;',
  19393. 'var',
  19394. ' i: IUnknown;',
  19395. 'procedure DoGlobal(o: TObject);',
  19396. 'begin',
  19397. ' i:=nil;',
  19398. ' i:=o;',
  19399. ' i:=i;',
  19400. 'end;',
  19401. 'procedure DoLocal(o: TObject);',
  19402. 'const k: IUnknown = nil;',
  19403. 'var j: IUnknown;',
  19404. 'begin',
  19405. ' k:=o;',
  19406. ' k:=i;',
  19407. ' j:=o;',
  19408. ' j:=i;',
  19409. 'end;',
  19410. 'var o: TObject;',
  19411. 'begin',
  19412. ' i:=nil;',
  19413. ' i:=o;',
  19414. '']);
  19415. ConvertProgram;
  19416. CheckSource('TestClassInterface_COM_AssignVar',
  19417. LinesToStr([ // statements
  19418. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19419. 'rtl.createClass(this, "TObject", null, function () {',
  19420. ' this.$init = function () {',
  19421. ' };',
  19422. ' this.$final = function () {',
  19423. ' };',
  19424. ' rtl.addIntf(this, $mod.IUnknown);',
  19425. '});',
  19426. 'this.i = null;',
  19427. 'this.DoGlobal = function (o) {',
  19428. ' rtl.setIntfP($mod, "i", null);',
  19429. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19430. ' rtl.setIntfP($mod, "i", $mod.i);',
  19431. '};',
  19432. 'var k = null;',
  19433. 'this.DoLocal = function (o) {',
  19434. ' var j = null;',
  19435. ' try{',
  19436. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19437. ' k = rtl.setIntfL(k, $mod.i);',
  19438. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19439. ' j = rtl.setIntfL(j, $mod.i);',
  19440. ' }finally{',
  19441. ' rtl._Release(j);',
  19442. ' };',
  19443. '};',
  19444. 'this.o = null;',
  19445. '']),
  19446. LinesToStr([ // $mod.$main
  19447. 'rtl.setIntfP($mod, "i", null);',
  19448. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19449. '']));
  19450. end;
  19451. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19452. begin
  19453. StartProgram(false);
  19454. Add([
  19455. '{$interfaces com}',
  19456. 'type',
  19457. ' IUnknown = interface',
  19458. ' function _AddRef: longint;',
  19459. ' function _Release: longint;',
  19460. ' end;',
  19461. ' TObject = class(IUnknown)',
  19462. ' function _AddRef: longint; virtual; abstract;',
  19463. ' function _Release: longint; virtual; abstract;',
  19464. ' end;',
  19465. 'procedure DoDefault(i, j: IUnknown);',
  19466. 'begin',
  19467. ' i:=nil;',
  19468. ' i:=j;',
  19469. 'end;',
  19470. 'begin',
  19471. '']);
  19472. ConvertProgram;
  19473. CheckSource('TestClassInterface_COM_AssignArg',
  19474. LinesToStr([ // statements
  19475. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19476. 'rtl.createClass(this, "TObject", null, function () {',
  19477. ' this.$init = function () {',
  19478. ' };',
  19479. ' this.$final = function () {',
  19480. ' };',
  19481. ' rtl.addIntf(this, $mod.IUnknown);',
  19482. '});',
  19483. 'this.DoDefault = function (i, j) {',
  19484. ' rtl._AddRef(i);',
  19485. ' try {',
  19486. ' i = rtl.setIntfL(i, null);',
  19487. ' i = rtl.setIntfL(i, j);',
  19488. ' } finally {',
  19489. ' rtl._Release(i);',
  19490. ' };',
  19491. '};',
  19492. '']),
  19493. LinesToStr([ // $mod.$main
  19494. '']));
  19495. end;
  19496. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19497. begin
  19498. StartProgram(false);
  19499. Add([
  19500. '{$interfaces com}',
  19501. 'type',
  19502. ' IUnknown = interface',
  19503. ' function _AddRef: longint;',
  19504. ' function _Release: longint;',
  19505. ' end;',
  19506. ' TObject = class(IUnknown)',
  19507. ' function _AddRef: longint; virtual; abstract;',
  19508. ' function _Release: longint; virtual; abstract;',
  19509. ' end;',
  19510. 'function DoDefault(i: IUnknown): IUnknown;',
  19511. 'begin',
  19512. ' Result:=i;',
  19513. ' if Result<>nil then exit;',
  19514. 'end;',
  19515. 'begin',
  19516. '']);
  19517. ConvertProgram;
  19518. CheckSource('TestClassInterface_COM_FunctionResult',
  19519. LinesToStr([ // statements
  19520. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19521. 'rtl.createClass(this, "TObject", null, function () {',
  19522. ' this.$init = function () {',
  19523. ' };',
  19524. ' this.$final = function () {',
  19525. ' };',
  19526. ' rtl.addIntf(this, $mod.IUnknown);',
  19527. '});',
  19528. 'this.DoDefault = function (i) {',
  19529. ' var Result = null;',
  19530. ' var $ok = false;',
  19531. ' try {',
  19532. ' Result = rtl.setIntfL(Result, i);',
  19533. ' if(Result !== null){',
  19534. ' $ok = true;',
  19535. ' return Result;',
  19536. ' };',
  19537. ' $ok = true;',
  19538. ' } finally {',
  19539. ' if(!$ok) rtl._Release(Result);',
  19540. ' };',
  19541. ' return Result;',
  19542. '};',
  19543. '']),
  19544. LinesToStr([ // $mod.$main
  19545. '']));
  19546. end;
  19547. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19548. begin
  19549. StartProgram(false);
  19550. Add([
  19551. '{$interfaces com}',
  19552. 'type',
  19553. ' IUnknown = interface',
  19554. ' function _AddRef: longint;',
  19555. ' function _Release: longint;',
  19556. ' end;',
  19557. ' TObject = class(IUnknown)',
  19558. ' function _AddRef: longint; virtual; abstract;',
  19559. ' function _Release: longint; virtual; abstract;',
  19560. ' function GetIntf: IUnknown; virtual;',
  19561. ' end;',
  19562. ' TMouse = class',
  19563. ' function GetIntf: IUnknown; override;',
  19564. ' end;',
  19565. 'function TObject.GetIntf: IUnknown; begin end;',
  19566. 'function TMouse.GetIntf: IUnknown;',
  19567. 'var i: IUnknown;',
  19568. 'begin',
  19569. ' inherited;',
  19570. ' inherited GetIntf;',
  19571. ' inherited GetIntf();',
  19572. ' Result:=inherited GetIntf;',
  19573. ' Result:=inherited GetIntf();',
  19574. ' i:=inherited GetIntf;',
  19575. ' i:=inherited GetIntf();',
  19576. 'end;',
  19577. 'begin',
  19578. '']);
  19579. ConvertProgram;
  19580. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19581. LinesToStr([ // statements
  19582. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19583. 'rtl.createClass(this, "TObject", null, function () {',
  19584. ' this.$init = function () {',
  19585. ' };',
  19586. ' this.$final = function () {',
  19587. ' };',
  19588. ' this.GetIntf = function () {',
  19589. ' var Result = null;',
  19590. ' return Result;',
  19591. ' };',
  19592. ' rtl.addIntf(this, $mod.IUnknown);',
  19593. '});',
  19594. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19595. ' this.GetIntf = function () {',
  19596. ' var Result = null;',
  19597. ' var i = null;',
  19598. ' var $ir = rtl.createIntfRefs();',
  19599. ' var $ok = false;',
  19600. ' try {',
  19601. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19602. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19603. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19604. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19605. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19606. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19607. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19608. ' $ok = true;',
  19609. ' } finally {',
  19610. ' $ir.free();',
  19611. ' rtl._Release(i);',
  19612. ' if (!$ok) rtl._Release(Result);',
  19613. ' };',
  19614. ' return Result;',
  19615. ' };',
  19616. ' rtl.addIntf(this, $mod.IUnknown);',
  19617. '});',
  19618. '']),
  19619. LinesToStr([ // $mod.$main
  19620. '']));
  19621. end;
  19622. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19623. begin
  19624. StartProgram(false);
  19625. Add([
  19626. '{$interfaces com}',
  19627. 'type',
  19628. ' IUnknown = interface',
  19629. ' function _AddRef: longint;',
  19630. ' function _Release: longint;',
  19631. ' end;',
  19632. ' TObject = class(IUnknown)',
  19633. ' function _AddRef: longint; virtual; abstract;',
  19634. ' function _Release: longint; virtual; abstract;',
  19635. ' end;',
  19636. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19637. 'begin',
  19638. ' if i is IUnknown then ;',
  19639. ' if o is IUnknown then ;',
  19640. ' if i is TObject then ;',
  19641. ' i:=j as IUnknown;',
  19642. ' i:=o as IUnknown;',
  19643. ' o:=j as TObject;',
  19644. ' i:=IUnknown(j);',
  19645. ' i:=IUnknown(o);',
  19646. ' o:=TObject(i);',
  19647. 'end;',
  19648. 'begin',
  19649. '']);
  19650. ConvertProgram;
  19651. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19652. LinesToStr([ // statements
  19653. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19654. 'rtl.createClass(this, "TObject", null, function () {',
  19655. ' this.$init = function () {',
  19656. ' };',
  19657. ' this.$final = function () {',
  19658. ' };',
  19659. ' rtl.addIntf(this, $mod.IUnknown);',
  19660. '});',
  19661. 'this.DoDefault = function (i, j, o) {',
  19662. ' rtl._AddRef(i);',
  19663. ' try {',
  19664. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19665. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19666. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19667. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19668. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19669. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19670. ' i = rtl.setIntfL(i, j);',
  19671. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19672. ' o = rtl.intfToClass(i, $mod.TObject);',
  19673. ' } finally {',
  19674. ' rtl._Release(i);',
  19675. ' };',
  19676. '};',
  19677. '']),
  19678. LinesToStr([ // $mod.$main
  19679. '']));
  19680. end;
  19681. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19682. begin
  19683. StartProgram(false);
  19684. Add([
  19685. '{$interfaces com}',
  19686. 'type',
  19687. ' IUnknown = interface',
  19688. ' function _AddRef: longint;',
  19689. ' function _Release: longint;',
  19690. ' end;',
  19691. ' TObject = class(IUnknown)',
  19692. ' function _AddRef: longint; virtual; abstract;',
  19693. ' function _Release: longint; virtual; abstract;',
  19694. ' end;',
  19695. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19696. 'var o: TObject;',
  19697. 'begin',
  19698. ' DoIt(v,v,v,v);',
  19699. ' DoIt(o,o,k,k);',
  19700. 'end;',
  19701. 'procedure DoSome;',
  19702. 'var v: IUnknown;',
  19703. 'begin',
  19704. ' DoIt(v,v,v,v);',
  19705. 'end;',
  19706. 'var i: IUnknown;',
  19707. 'begin',
  19708. ' DoIt(i,i,i,i);',
  19709. '']);
  19710. ConvertProgram;
  19711. CheckSource('TestClassInterface_COM_PassAsArg',
  19712. LinesToStr([ // statements
  19713. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19714. 'rtl.createClass(this, "TObject", null, function () {',
  19715. ' this.$init = function () {',
  19716. ' };',
  19717. ' this.$final = function () {',
  19718. ' };',
  19719. ' rtl.addIntf(this, $mod.IUnknown);',
  19720. '});',
  19721. 'this.DoIt = function (v, j, k, l) {',
  19722. ' var o = null;',
  19723. ' var $ir = rtl.createIntfRefs();',
  19724. ' rtl._AddRef(v);',
  19725. ' try {',
  19726. ' $mod.DoIt(v, v, {',
  19727. ' get: function () {',
  19728. ' return v;',
  19729. ' },',
  19730. ' set: function (w) {',
  19731. ' v = rtl.setIntfL(v, w);',
  19732. ' }',
  19733. ' }, {',
  19734. ' get: function () {',
  19735. ' return v;',
  19736. ' },',
  19737. ' set: function (w) {',
  19738. ' v = rtl.setIntfL(v, w);',
  19739. ' }',
  19740. ' });',
  19741. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19742. ' } finally {',
  19743. ' $ir.free();',
  19744. ' rtl._Release(v);',
  19745. ' };',
  19746. '};',
  19747. 'this.DoSome = function () {',
  19748. ' var v = null;',
  19749. ' try {',
  19750. ' $mod.DoIt(v, v, {',
  19751. ' get: function () {',
  19752. ' return v;',
  19753. ' },',
  19754. ' set: function (w) {',
  19755. ' v = rtl.setIntfL(v, w);',
  19756. ' }',
  19757. ' }, {',
  19758. ' get: function () {',
  19759. ' return v;',
  19760. ' },',
  19761. ' set: function (w) {',
  19762. ' v = rtl.setIntfL(v, w);',
  19763. ' }',
  19764. ' });',
  19765. ' } finally {',
  19766. ' rtl._Release(v);',
  19767. ' };',
  19768. '};',
  19769. 'this.i = null;',
  19770. '']),
  19771. LinesToStr([ // $mod.$main
  19772. '$mod.DoIt($mod.i, $mod.i, {',
  19773. ' p: $mod,',
  19774. ' get: function () {',
  19775. ' return this.p.i;',
  19776. ' },',
  19777. ' set: function (v) {',
  19778. ' rtl.setIntfP(this.p, "i", v);',
  19779. ' }',
  19780. '}, {',
  19781. ' p: $mod,',
  19782. ' get: function () {',
  19783. ' return this.p.i;',
  19784. ' },',
  19785. ' set: function (v) {',
  19786. ' rtl.setIntfP(this.p, "i", v);',
  19787. ' }',
  19788. '});',
  19789. '']));
  19790. end;
  19791. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19792. begin
  19793. StartProgram(false);
  19794. Add([
  19795. '{$interfaces com}',
  19796. 'type',
  19797. ' IUnknown = interface',
  19798. ' function _AddRef: longint;',
  19799. ' function _Release: longint;',
  19800. ' end;',
  19801. ' TObject = class(IUnknown)',
  19802. ' function _AddRef: longint; virtual; abstract;',
  19803. ' function _Release: longint; virtual; abstract;',
  19804. ' end;',
  19805. 'procedure DoIt(out i);',
  19806. 'begin end;',
  19807. 'procedure DoSome;',
  19808. 'var v: IUnknown;',
  19809. 'begin',
  19810. ' DoIt(v);',
  19811. 'end;',
  19812. 'function GetIt: IUnknown;',
  19813. 'begin',
  19814. ' DoIt(Result);',
  19815. 'end;',
  19816. 'var i: IUnknown;',
  19817. 'begin',
  19818. ' DoIt(i);',
  19819. '']);
  19820. ConvertProgram;
  19821. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19822. LinesToStr([ // statements
  19823. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19824. 'rtl.createClass(this, "TObject", null, function () {',
  19825. ' this.$init = function () {',
  19826. ' };',
  19827. ' this.$final = function () {',
  19828. ' };',
  19829. ' rtl.addIntf(this, $mod.IUnknown);',
  19830. '});',
  19831. 'this.DoIt = function (i) {',
  19832. '};',
  19833. 'this.DoSome = function () {',
  19834. ' var v = null;',
  19835. ' try {',
  19836. ' $mod.DoIt({',
  19837. ' get: function () {',
  19838. ' return v;',
  19839. ' },',
  19840. ' set: function (w) {',
  19841. ' v = w;',
  19842. ' }',
  19843. ' });',
  19844. ' } finally {',
  19845. ' rtl._Release(v);',
  19846. ' };',
  19847. '};',
  19848. 'this.GetIt = function () {',
  19849. ' var Result = null;',
  19850. ' var $ok = false;',
  19851. ' try {',
  19852. ' $mod.DoIt({',
  19853. ' get: function () {',
  19854. ' return Result;',
  19855. ' },',
  19856. ' set: function (v) {',
  19857. ' Result = v;',
  19858. ' }',
  19859. ' });',
  19860. ' $ok = true;',
  19861. ' } finally {',
  19862. ' if (!$ok) rtl._Release(Result);',
  19863. ' };',
  19864. ' return Result;',
  19865. '};',
  19866. 'this.i = null;',
  19867. '']),
  19868. LinesToStr([ // $mod.$main
  19869. 'try {',
  19870. ' $mod.DoIt({',
  19871. ' p: $mod,',
  19872. ' get: function () {',
  19873. ' return this.p.i;',
  19874. ' },',
  19875. ' set: function (v) {',
  19876. ' this.p.i = v;',
  19877. ' }',
  19878. ' });',
  19879. '} finally {',
  19880. ' rtl._Release($mod.i);',
  19881. '};',
  19882. '']));
  19883. end;
  19884. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19885. begin
  19886. StartProgram(false);
  19887. Add([
  19888. '{$interfaces com}',
  19889. 'type',
  19890. ' IUnknown = interface',
  19891. ' function _AddRef: longint;',
  19892. ' function _Release: longint;',
  19893. ' end;',
  19894. ' TObject = class(IUnknown)',
  19895. ' function _AddRef: longint; virtual; abstract;',
  19896. ' function _Release: longint; virtual; abstract;',
  19897. ' end;',
  19898. 'function GetIt: IUnknown;',
  19899. 'begin',
  19900. 'end;',
  19901. 'procedure DoSome;',
  19902. 'var v: IUnknown;',
  19903. ' i: longint;',
  19904. 'begin',
  19905. ' v:=GetIt;',
  19906. ' v:=GetIt();',
  19907. ' GetIt()._AddRef;',
  19908. ' i:=GetIt()._AddRef;',
  19909. 'end;',
  19910. 'var v: IUnknown;',
  19911. ' i: longint;',
  19912. 'begin',
  19913. ' v:=GetIt;',
  19914. ' v:=GetIt();',
  19915. ' GetIt()._AddRef;',
  19916. ' i:=GetIt()._AddRef;',
  19917. '']);
  19918. ConvertProgram;
  19919. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19920. LinesToStr([ // statements
  19921. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19922. 'rtl.createClass(this, "TObject", null, function () {',
  19923. ' this.$init = function () {',
  19924. ' };',
  19925. ' this.$final = function () {',
  19926. ' };',
  19927. ' rtl.addIntf(this, $mod.IUnknown);',
  19928. '});',
  19929. 'this.GetIt = function () {',
  19930. ' var Result = null;',
  19931. ' return Result;',
  19932. '};',
  19933. 'this.DoSome = function () {',
  19934. ' var v = null;',
  19935. ' var i = 0;',
  19936. ' var $ir = rtl.createIntfRefs();',
  19937. ' try {',
  19938. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19939. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19940. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19941. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19942. ' } finally {',
  19943. ' $ir.free();',
  19944. ' rtl._Release(v);',
  19945. ' };',
  19946. '};',
  19947. 'this.v = null;',
  19948. 'this.i = 0;',
  19949. '']),
  19950. LinesToStr([ // $mod.$main
  19951. 'var $ir = rtl.createIntfRefs();',
  19952. 'try {',
  19953. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19954. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19955. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19956. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19957. '} finally {',
  19958. ' $ir.free();',
  19959. '};',
  19960. '']));
  19961. end;
  19962. procedure TTestModule.TestClassInterface_COM_Property;
  19963. begin
  19964. StartProgram(false);
  19965. Add([
  19966. '{$interfaces com}',
  19967. 'type',
  19968. ' IUnknown = interface',
  19969. ' function _AddRef: longint;',
  19970. ' function _Release: longint;',
  19971. ' end;',
  19972. ' TObject = class(IUnknown)',
  19973. ' FAnt: IUnknown;',
  19974. ' function _AddRef: longint; virtual; abstract;',
  19975. ' function _Release: longint; virtual; abstract;',
  19976. ' function GetBird: IUnknown; virtual; abstract;',
  19977. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19978. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19979. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19980. ' property Ant: IUnknown read FAnt write FAnt;',
  19981. ' property Bird: IUnknown read GetBird write SetBird;',
  19982. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19983. ' end;',
  19984. 'procedure DoIt;',
  19985. 'var',
  19986. ' o: TObject;',
  19987. ' v: IUnknown;',
  19988. 'begin',
  19989. ' v:=o.Ant;',
  19990. ' o.Ant:=v;',
  19991. ' o.Ant:=o.Ant;',
  19992. ' v:=o.Bird;',
  19993. ' o.Bird:=v;',
  19994. ' o.Bird:=o.Bird;',
  19995. ' v:=o.Items[1];',
  19996. ' o.Items[2]:=v;',
  19997. ' o.Items[3]:=o.Items[4];',
  19998. ' v:=o[5];',
  19999. ' o[6]:=v;',
  20000. ' o[7]:=o[8];',
  20001. 'end;',
  20002. 'begin',
  20003. '']);
  20004. ConvertProgram;
  20005. CheckSource('TestClassInterface_COM_Property',
  20006. LinesToStr([ // statements
  20007. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20008. 'rtl.createClass(this, "TObject", null, function () {',
  20009. ' this.$init = function () {',
  20010. ' this.FAnt = null;',
  20011. ' };',
  20012. ' this.$final = function () {',
  20013. ' this.FAnt = undefined;',
  20014. ' };',
  20015. ' rtl.addIntf(this, $mod.IUnknown);',
  20016. '});',
  20017. 'this.DoIt = function () {',
  20018. ' var o = null;',
  20019. ' var v = null;',
  20020. ' var $ir = rtl.createIntfRefs();',
  20021. ' try {',
  20022. ' v = rtl.setIntfL(v, o.FAnt);',
  20023. ' rtl.setIntfP(o, "FAnt", v);',
  20024. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20025. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20026. ' o.SetBird(v);',
  20027. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20028. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20029. ' o.SetItems(2, v);',
  20030. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20031. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20032. ' o.SetItems(6, v);',
  20033. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20034. ' } finally {',
  20035. ' $ir.free();',
  20036. ' rtl._Release(v);',
  20037. ' };',
  20038. '};',
  20039. '']),
  20040. LinesToStr([ // $mod.$main
  20041. '']));
  20042. end;
  20043. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20044. begin
  20045. StartProgram(false);
  20046. Add([
  20047. '{$interfaces com}',
  20048. 'type',
  20049. ' IUnknown = interface',
  20050. ' function _AddRef: longint;',
  20051. ' function _Release: longint;',
  20052. ' function GetBird: IUnknown;',
  20053. ' procedure SetBird(Value: IUnknown);',
  20054. ' function GetItems(Index: longint): IUnknown;',
  20055. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20056. ' property Bird: IUnknown read GetBird write SetBird;',
  20057. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20058. ' end;',
  20059. ' TObject = class(IUnknown)',
  20060. ' function _AddRef: longint; virtual; abstract;',
  20061. ' function _Release: longint; virtual; abstract;',
  20062. ' function GetBird: IUnknown; virtual; abstract;',
  20063. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20064. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20065. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20066. ' end;',
  20067. 'procedure DoIt;',
  20068. 'var',
  20069. ' o: TObject;',
  20070. ' v: IUnknown;',
  20071. 'begin',
  20072. ' v:=v.Items[1];',
  20073. ' v.Items[2]:=v;',
  20074. ' v.Items[3]:=v.Items[4];',
  20075. ' v:=v[5];',
  20076. ' v[6]:=v;',
  20077. ' v[7]:=v[8];',
  20078. ' v[9].Bird.Bird:=v;',
  20079. ' v:=v.Bird[10].Bird',
  20080. 'end;',
  20081. 'begin',
  20082. '']);
  20083. ConvertProgram;
  20084. CheckSource('TestClassInterface_COM_IntfProperty',
  20085. LinesToStr([ // statements
  20086. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20087. ' "_AddRef",',
  20088. ' "_Release",',
  20089. ' "GetBird",',
  20090. ' "SetBird",',
  20091. ' "GetItems",',
  20092. ' "SetItems"',
  20093. '], null);',
  20094. 'rtl.createClass(this, "TObject", null, function () {',
  20095. ' this.$init = function () {',
  20096. ' };',
  20097. ' this.$final = function () {',
  20098. ' };',
  20099. ' rtl.addIntf(this, $mod.IUnknown);',
  20100. '});',
  20101. 'this.DoIt = function () {',
  20102. ' var o = null;',
  20103. ' var v = null;',
  20104. ' var $ir = rtl.createIntfRefs();',
  20105. ' try {',
  20106. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20107. ' v.SetItems(2, v);',
  20108. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20109. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20110. ' v.SetItems(6, v);',
  20111. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20112. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20113. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20114. ' } finally {',
  20115. ' $ir.free();',
  20116. ' rtl._Release(v);',
  20117. ' };',
  20118. '};',
  20119. '']),
  20120. LinesToStr([ // $mod.$main
  20121. '']));
  20122. end;
  20123. procedure TTestModule.TestClassInterface_COM_Delegation;
  20124. begin
  20125. StartProgram(false);
  20126. Add([
  20127. '{$interfaces com}',
  20128. 'type',
  20129. ' IUnknown = interface',
  20130. ' function _AddRef: longint;',
  20131. ' function _Release: longint;',
  20132. ' end;',
  20133. ' IBird = interface(IUnknown)',
  20134. ' procedure Fly(s: string);',
  20135. ' end;',
  20136. ' IEagle = interface(IBird) end;',
  20137. ' IDove = interface(IBird) end;',
  20138. ' ISwallow = interface(IBird) end;',
  20139. ' TObject = class',
  20140. ' end;',
  20141. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20142. ' function _AddRef: longint; virtual; abstract;',
  20143. ' function _Release: longint; virtual; abstract;',
  20144. ' procedure Fly(s: string); virtual; abstract;',
  20145. ' end;',
  20146. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20147. ' function _AddRef: longint; virtual; abstract;',
  20148. ' function _Release: longint; virtual; abstract;',
  20149. ' FBirdIntf: IBird;',
  20150. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20151. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20152. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20153. ' FDoveObj: TBird;',
  20154. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20155. ' function GetSwallowObj: TBird; virtual; abstract;',
  20156. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20157. ' end;',
  20158. 'begin',
  20159. '']);
  20160. ConvertProgram;
  20161. CheckSource('TestClassInterface_COM_Delegation',
  20162. LinesToStr([ // statements
  20163. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20164. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20165. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20166. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20167. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20168. 'rtl.createClass(this, "TObject", null, function () {',
  20169. ' this.$init = function () {',
  20170. ' };',
  20171. ' this.$final = function () {',
  20172. ' };',
  20173. '});',
  20174. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20175. ' rtl.addIntf(this, $mod.IBird);',
  20176. ' rtl.addIntf(this, $mod.IEagle);',
  20177. ' rtl.addIntf(this, $mod.IDove);',
  20178. ' rtl.addIntf(this, $mod.ISwallow);',
  20179. '});',
  20180. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20181. ' this.$init = function () {',
  20182. ' $mod.TObject.$init.call(this);',
  20183. ' this.FBirdIntf = null;',
  20184. ' this.FDoveObj = null;',
  20185. ' };',
  20186. ' this.$final = function () {',
  20187. ' this.FBirdIntf = undefined;',
  20188. ' this.FDoveObj = undefined;',
  20189. ' $mod.TObject.$final.call(this);',
  20190. ' };',
  20191. ' this.$intfmaps = {',
  20192. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20193. ' return rtl._AddRef(this.FBirdIntf);',
  20194. ' },',
  20195. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20196. ' return this.GetEagleIntf();',
  20197. ' },',
  20198. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20199. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20200. ' },',
  20201. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20202. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20203. ' }',
  20204. ' };',
  20205. '});',
  20206. '']),
  20207. LinesToStr([ // $mod.$main
  20208. '']));
  20209. end;
  20210. procedure TTestModule.TestClassInterface_COM_With;
  20211. begin
  20212. StartProgram(false);
  20213. Add([
  20214. '{$interfaces com}',
  20215. 'type',
  20216. ' IUnknown = interface',
  20217. ' function _AddRef: longint;',
  20218. ' function _Release: longint;',
  20219. ' function GetAnt: IUnknown;',
  20220. ' property Ant: IUnknown read GetAnt;',
  20221. ' end;',
  20222. ' TObject = class(IUnknown)',
  20223. ' function _AddRef: longint; virtual; abstract;',
  20224. ' function _Release: longint; virtual; abstract;',
  20225. ' function GetAnt: IUnknown; virtual; abstract;',
  20226. ' property Ant: IUnknown read GetAnt;',
  20227. ' end;',
  20228. 'procedure DoIt;',
  20229. 'var',
  20230. ' i: IUnknown;',
  20231. 'begin',
  20232. ' with i do ',
  20233. ' GetAnt;',
  20234. ' with i.Ant, Ant do ',
  20235. ' GetAnt;',
  20236. 'end;',
  20237. 'begin',
  20238. '']);
  20239. ConvertProgram;
  20240. CheckSource('TestClassInterface_COM_With',
  20241. LinesToStr([ // statements
  20242. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20243. 'rtl.createClass(this, "TObject", null, function () {',
  20244. ' this.$init = function () {',
  20245. ' };',
  20246. ' this.$final = function () {',
  20247. ' };',
  20248. ' rtl.addIntf(this, $mod.IUnknown);',
  20249. '});',
  20250. 'this.DoIt = function () {',
  20251. ' var i = null;',
  20252. ' var $ir = rtl.createIntfRefs();',
  20253. ' try {',
  20254. ' $ir.ref(1, i.GetAnt());',
  20255. ' var $with = $ir.ref(2, i.GetAnt());',
  20256. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20257. ' $ir.ref(4, $with1.GetAnt());',
  20258. ' } finally {',
  20259. ' $ir.free();',
  20260. ' };',
  20261. '};',
  20262. '']),
  20263. LinesToStr([ // $mod.$main
  20264. '']));
  20265. end;
  20266. procedure TTestModule.TestClassInterface_COM_ForIn;
  20267. begin
  20268. StartProgram(false);
  20269. Add([
  20270. '{$interfaces com}',
  20271. 'type',
  20272. ' IUnknown = interface end;',
  20273. ' TObject = class',
  20274. ' Id: longint;',
  20275. ' end;',
  20276. ' IEnumerator = interface(IUnknown)',
  20277. ' function GetCurrent: TObject;',
  20278. ' function MoveNext: Boolean;',
  20279. ' property Current: TObject read GetCurrent;',
  20280. ' end;',
  20281. ' IEnumerable = interface(IUnknown)',
  20282. ' function GetEnumerator: IEnumerator;',
  20283. ' end;',
  20284. 'var',
  20285. ' o: TObject;',
  20286. ' i: IEnumerable;',
  20287. 'begin',
  20288. ' for o in i do o.Id:=3;',
  20289. '']);
  20290. ConvertProgram;
  20291. CheckSource('TestClassInterface_COM_ForIn',
  20292. LinesToStr([ // statements
  20293. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20294. 'rtl.createClass(this, "TObject", null, function () {',
  20295. ' this.$init = function () {',
  20296. ' this.Id = 0;',
  20297. ' };',
  20298. ' this.$final = function () {',
  20299. ' };',
  20300. '});',
  20301. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20302. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20303. 'this.o = null;',
  20304. 'this.i = null;',
  20305. '']),
  20306. LinesToStr([ // $mod.$main
  20307. 'var $in = $mod.i.GetEnumerator();',
  20308. 'try {',
  20309. ' while ($in.MoveNext()) {',
  20310. ' $mod.o = $in.GetCurrent();',
  20311. ' $mod.o.Id = 3;',
  20312. ' }',
  20313. '} finally {',
  20314. ' rtl._Release($in)',
  20315. '};',
  20316. '']));
  20317. end;
  20318. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20319. begin
  20320. StartProgram(false);
  20321. Add([
  20322. '{$interfaces com}',
  20323. 'type',
  20324. ' IUnknown = interface',
  20325. ' function _AddRef: longint;',
  20326. ' function _Release: longint;',
  20327. ' end;',
  20328. ' TObject = class',
  20329. ' end;',
  20330. ' TArrOfIntf = array of IUnknown;',
  20331. 'begin',
  20332. '']);
  20333. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20334. ConvertProgram;
  20335. end;
  20336. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20337. begin
  20338. StartProgram(false);
  20339. Add([
  20340. '{$interfaces com}',
  20341. 'type',
  20342. ' IUnknown = interface',
  20343. ' function _AddRef: longint;',
  20344. ' function _Release: longint;',
  20345. ' end;',
  20346. ' TRec = record',
  20347. ' i: IUnknown;',
  20348. ' end;',
  20349. 'begin',
  20350. '']);
  20351. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20352. ConvertProgram;
  20353. end;
  20354. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20355. begin
  20356. StartUnit(false);
  20357. Add([
  20358. '{$interfaces com}',
  20359. 'interface',
  20360. 'implementation',
  20361. 'type',
  20362. ' IUnknown = interface',
  20363. ' function _AddRef: longint;',
  20364. ' end;',
  20365. ' TObject = class(IUnknown)',
  20366. ' function _AddRef: longint;',
  20367. ' end;',
  20368. 'function TObject._AddRef: longint; begin end;',
  20369. 'var i: IUnknown;',
  20370. ' o: TObject;',
  20371. 'initialization',
  20372. ' i:=nil;',
  20373. ' i:=i;',
  20374. ' i:=o;',
  20375. ' if (o as IUnknown)=nil then ;',
  20376. '']);
  20377. ConvertUnit;
  20378. CheckSource('TestClassInterface_COM_UnitInitialization',
  20379. LinesToStr([ // statements
  20380. 'var $impl = $mod.$impl;',
  20381. '']),
  20382. LinesToStr([ // this.$init
  20383. 'var $ir = rtl.createIntfRefs();',
  20384. 'try {',
  20385. ' rtl.setIntfP($impl, "i", null);',
  20386. ' rtl.setIntfP($impl, "i", $impl.i);',
  20387. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20388. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20389. '} finally {',
  20390. ' $ir.free();',
  20391. '};',
  20392. '']),
  20393. LinesToStr([ // implementation
  20394. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20395. 'rtl.createClass($impl, "TObject", null, function () {',
  20396. ' this.$init = function () {',
  20397. ' };',
  20398. ' this.$final = function () {',
  20399. ' };',
  20400. ' this._AddRef = function () {',
  20401. ' var Result = 0;',
  20402. ' return Result;',
  20403. ' };',
  20404. ' rtl.addIntf(this, $impl.IUnknown);',
  20405. '});',
  20406. '$impl.i = null;',
  20407. '$impl.o = null;',
  20408. ''])
  20409. );
  20410. end;
  20411. procedure TTestModule.TestClassInterface_GUID;
  20412. begin
  20413. StartProgram(false);
  20414. Add([
  20415. '{$interfaces corba}',
  20416. 'type',
  20417. ' IUnknown = interface',
  20418. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20419. ' end;',
  20420. ' TObject = class end;',
  20421. ' TGUID = record D1, D2, D3, D4: word; end;',
  20422. ' TAliasGUID = TGUID;',
  20423. ' TGUIDString = type string;',
  20424. ' TAliasGUIDString = TGUIDString;',
  20425. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20426. 'begin end;',
  20427. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20428. 'begin end;',
  20429. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20430. 'begin end;',
  20431. 'var',
  20432. ' i: IUnknown;',
  20433. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20434. ' s: TAliasGUIDString;',
  20435. 'begin',
  20436. ' DoConstGUIDIt(IUnknown);',
  20437. ' DoDefGUID(IUnknown);',
  20438. ' DoStr(IUnknown);',
  20439. ' DoConstGUIDIt(i);',
  20440. ' DoDefGUID(i);',
  20441. ' DoStr(i);',
  20442. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20443. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20444. ' DoStr(g);',
  20445. ' g:=i;',
  20446. ' g:=IUnknown;',
  20447. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20448. ' s:=i;',
  20449. ' s:=IUnknown;',
  20450. ' s:=g;',
  20451. ' if g=i then ;',
  20452. ' if i=g then ;',
  20453. ' if g=IUnknown then ;',
  20454. ' if IUnknown=g then ;',
  20455. ' if s=i then ;',
  20456. ' if i=s then ;',
  20457. ' if s=IUnknown then ;',
  20458. ' if IUnknown=s then ;',
  20459. ' if s=g then ;',
  20460. ' if g=s then ;',
  20461. '']);
  20462. ConvertProgram;
  20463. CheckSource('TestClassInterface_GUID',
  20464. LinesToStr([ // statements
  20465. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20466. 'rtl.createClass(this, "TObject", null, function () {',
  20467. ' this.$init = function () {',
  20468. ' };',
  20469. ' this.$final = function () {',
  20470. ' };',
  20471. '});',
  20472. 'rtl.recNewT(this, "TGUID", function () {',
  20473. ' this.D1 = 0;',
  20474. ' this.D2 = 0;',
  20475. ' this.D3 = 0;',
  20476. ' this.D4 = 0;',
  20477. ' this.$eq = function (b) {',
  20478. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20479. ' };',
  20480. ' this.$assign = function (s) {',
  20481. ' this.D1 = s.D1;',
  20482. ' this.D2 = s.D2;',
  20483. ' this.D3 = s.D3;',
  20484. ' this.D4 = s.D4;',
  20485. ' return this;',
  20486. ' };',
  20487. '});',
  20488. 'this.DoConstGUIDIt = function (g) {',
  20489. '};',
  20490. 'this.DoDefGUID = function (g) {',
  20491. '};',
  20492. 'this.DoStr = function (s) {',
  20493. '};',
  20494. 'this.i = null;',
  20495. 'this.g = this.TGUID.$clone({',
  20496. ' D1: 0xD91C9AF4,',
  20497. ' D2: 0x3C93,',
  20498. ' D3: 0x420F,',
  20499. ' D4: [',
  20500. ' 0xA3,',
  20501. ' 0x03,',
  20502. ' 0xBF,',
  20503. ' 0x5B,',
  20504. ' 0xA8,',
  20505. ' 0x2B,',
  20506. ' 0xFD,',
  20507. ' 0x23',
  20508. ' ]',
  20509. '});',
  20510. 'this.s = "";',
  20511. '']),
  20512. LinesToStr([ // $mod.$main
  20513. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20514. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20515. '$mod.DoStr($mod.IUnknown.$guid);',
  20516. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20517. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20518. '$mod.DoStr($mod.i.$guid);',
  20519. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20520. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20521. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20522. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20523. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20524. '$mod.g.$assign({',
  20525. ' D1: 0xD91C9AF4,',
  20526. ' D2: 0x3C93,',
  20527. ' D3: 0x420F,',
  20528. ' D4: [',
  20529. ' 0xA3,',
  20530. ' 0x03,',
  20531. ' 0xBF,',
  20532. ' 0x5B,',
  20533. ' 0xA8,',
  20534. ' 0x2B,',
  20535. ' 0xFD,',
  20536. ' 0x23',
  20537. ' ]',
  20538. '});',
  20539. '$mod.s = $mod.i.$guid;',
  20540. '$mod.s = $mod.IUnknown.$guid;',
  20541. '$mod.s = rtl.guidrToStr($mod.g);',
  20542. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20543. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20544. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20545. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20546. 'if ($mod.s === $mod.i.$guid) ;',
  20547. 'if ($mod.i.$guid === $mod.s) ;',
  20548. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20549. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20550. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20551. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20552. '']));
  20553. end;
  20554. procedure TTestModule.TestClassInterface_GUIDProperty;
  20555. begin
  20556. StartProgram(false);
  20557. Add([
  20558. '{$interfaces corba}',
  20559. 'type',
  20560. ' IUnknown = interface',
  20561. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20562. ' end;',
  20563. ' TGUID = record D1, D2, D3, D4: word; end;',
  20564. ' TAliasGUID = TGUID;',
  20565. ' TGUIDString = type string;',
  20566. ' TAliasGUIDString = TGUIDString;',
  20567. ' TObject = class',
  20568. ' function GetG: TAliasGUID; virtual; abstract;',
  20569. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20570. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20571. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20572. ' property g: TAliasGUID read GetG write SetG;',
  20573. ' property s: TAliasGUIDString read GetS write SetS;',
  20574. ' end;',
  20575. 'var o: TObject;',
  20576. 'begin',
  20577. ' o.g:=IUnknown;',
  20578. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20579. ' o.s:=IUnknown;',
  20580. ' o.s:=o.g;',
  20581. '']);
  20582. ConvertProgram;
  20583. CheckSource('TestClassInterface_GUIDProperty',
  20584. LinesToStr([ // statements
  20585. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20586. 'rtl.recNewT(this, "TGUID", function () {',
  20587. ' this.D1 = 0;',
  20588. ' this.D2 = 0;',
  20589. ' this.D3 = 0;',
  20590. ' this.D4 = 0;',
  20591. ' this.$eq = function (b) {',
  20592. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20593. ' };',
  20594. ' this.$assign = function (s) {',
  20595. ' this.D1 = s.D1;',
  20596. ' this.D2 = s.D2;',
  20597. ' this.D3 = s.D3;',
  20598. ' this.D4 = s.D4;',
  20599. ' return this;',
  20600. ' };',
  20601. '});',
  20602. 'rtl.createClass(this, "TObject", null, function () {',
  20603. ' this.$init = function () {',
  20604. ' };',
  20605. ' this.$final = function () {',
  20606. ' };',
  20607. '});',
  20608. 'this.o = null;',
  20609. '']),
  20610. LinesToStr([ // $mod.$main
  20611. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20612. '$mod.o.SetG({',
  20613. ' D1: 0xD91C9AF4,',
  20614. ' D2: 0x3C93,',
  20615. ' D3: 0x420F,',
  20616. ' D4: [',
  20617. ' 0xA3,',
  20618. ' 0x03,',
  20619. ' 0xBF,',
  20620. ' 0x5B,',
  20621. ' 0xA8,',
  20622. ' 0x2B,',
  20623. ' 0xFD,',
  20624. ' 0x23',
  20625. ' ]',
  20626. '});',
  20627. '$mod.o.SetS($mod.IUnknown.$guid);',
  20628. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20629. '']));
  20630. end;
  20631. procedure TTestModule.TestClassHelper_ClassVar;
  20632. begin
  20633. StartProgram(false);
  20634. Add([
  20635. 'type',
  20636. ' TObject = class',
  20637. ' end;',
  20638. ' THelper = class helper for TObject',
  20639. ' const',
  20640. ' One = 1;',
  20641. ' Two: word = 2;',
  20642. ' class var',
  20643. ' Glob: word;',
  20644. ' function Foo(w: word): word;',
  20645. ' class function Bar(w: word): word;',
  20646. ' end;',
  20647. 'function THelper.foo(w: word): word;',
  20648. 'begin',
  20649. ' Result:=w;',
  20650. ' Two:=One+w;',
  20651. ' Glob:=Glob;',
  20652. ' Result:=Self.Glob;',
  20653. ' Self.Glob:=Self.Glob;',
  20654. ' with Self do Glob:=Glob;',
  20655. 'end;',
  20656. 'class function THelper.bar(w: word): word;',
  20657. 'begin',
  20658. ' Result:=w;',
  20659. ' Two:=One;',
  20660. ' Glob:=Glob;',
  20661. ' Self.Glob:=Self.Glob;',
  20662. ' with Self do Glob:=Glob;',
  20663. 'end;',
  20664. 'var o: TObject;',
  20665. 'begin',
  20666. ' tobject.two:=tobject.one;',
  20667. ' tobject.Glob:=tobject.Glob;',
  20668. ' with tobject do begin',
  20669. ' two:=one;',
  20670. ' Glob:=Glob;',
  20671. ' end;',
  20672. ' o.two:=o.one;',
  20673. ' o.Glob:=o.Glob;',
  20674. ' with o do begin',
  20675. ' two:=one;',
  20676. ' Glob:=Glob;',
  20677. ' end;',
  20678. '']);
  20679. ConvertProgram;
  20680. CheckSource('TestClassHelper_ClassVar',
  20681. LinesToStr([ // statements
  20682. 'rtl.createClass(this, "TObject", null, function () {',
  20683. ' this.$init = function () {',
  20684. ' };',
  20685. ' this.$final = function () {',
  20686. ' };',
  20687. '});',
  20688. 'rtl.createHelper(this, "THelper", null, function () {',
  20689. ' this.One = 1;',
  20690. ' this.Two = 2;',
  20691. ' this.Glob = 0;',
  20692. ' this.Foo = function (w) {',
  20693. ' var Result = 0;',
  20694. ' Result = w;',
  20695. ' $mod.THelper.Two = 1 + w;',
  20696. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20697. ' Result = $mod.THelper.Glob;',
  20698. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20699. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20700. ' return Result;',
  20701. ' };',
  20702. ' this.Bar = function (w) {',
  20703. ' var Result = 0;',
  20704. ' Result = w;',
  20705. ' $mod.THelper.Two = 1;',
  20706. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20707. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20708. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20709. ' return Result;',
  20710. ' };',
  20711. '});',
  20712. 'this.o = null;',
  20713. '']),
  20714. LinesToStr([ // $mod.$main
  20715. '$mod.THelper.Two = 1;',
  20716. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20717. 'var $with = $mod.TObject;',
  20718. '$mod.THelper.Two = 1;',
  20719. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20720. '$mod.THelper.Two = 1;',
  20721. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20722. 'var $with1 = $mod.o;',
  20723. '$mod.THelper.Two = 1;',
  20724. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20725. '']));
  20726. end;
  20727. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20728. begin
  20729. StartProgram(false);
  20730. Add([
  20731. 'type',
  20732. ' TObject = class',
  20733. ' FSize: word;',
  20734. ' property Size: word read FSize write FSize;',
  20735. ' end;',
  20736. ' THelper = class helper for TObject',
  20737. ' function Foo(w: word = 1): word;',
  20738. ' end;',
  20739. 'function THelper.foo(w: word): word;',
  20740. 'begin',
  20741. ' Result:=Size;',
  20742. ' Size:=Size+2;',
  20743. ' Self.Size:=Self.Size+3;',
  20744. ' FSize:=FSize+4;',
  20745. ' Self.FSize:=Self.FSize+5;',
  20746. ' with Self do begin',
  20747. ' Size:=Size+6;',
  20748. ' FSize:=FSize+7;',
  20749. ' FSize:=FSize+8;',
  20750. ' end;',
  20751. 'end;',
  20752. 'begin',
  20753. '']);
  20754. ConvertProgram;
  20755. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20756. LinesToStr([ // statements
  20757. 'rtl.createClass(this, "TObject", null, function () {',
  20758. ' this.$init = function () {',
  20759. ' this.FSize = 0;',
  20760. ' };',
  20761. ' this.$final = function () {',
  20762. ' };',
  20763. '});',
  20764. 'rtl.createHelper(this, "THelper", null, function () {',
  20765. ' this.Foo = function (w) {',
  20766. ' var Result = 0;',
  20767. ' Result = this.FSize;',
  20768. ' this.FSize = this.FSize + 2;',
  20769. ' this.FSize = this.FSize + 3;',
  20770. ' this.FSize = this.FSize + 4;',
  20771. ' this.FSize = this.FSize + 5;',
  20772. ' this.FSize = this.FSize + 6;',
  20773. ' this.FSize = this.FSize + 7;',
  20774. ' this.FSize = this.FSize + 8;',
  20775. ' return Result;',
  20776. ' };',
  20777. '});',
  20778. '']),
  20779. LinesToStr([ // $mod.$main
  20780. '']));
  20781. end;
  20782. procedure TTestModule.TestClassHelper_Method_Call;
  20783. begin
  20784. StartProgram(false);
  20785. Add([
  20786. 'type',
  20787. ' TObject = class',
  20788. ' procedure Run(w: word = 10);',
  20789. ' end;',
  20790. ' THelper = class helper for TObject',
  20791. ' function Foo(w: word = 1): word;',
  20792. ' end;',
  20793. 'procedure TObject.Run(w: word);',
  20794. 'var o: TObject;',
  20795. 'begin',
  20796. ' Foo;',
  20797. ' Foo();',
  20798. ' Foo(2);',
  20799. ' Self.Foo;',
  20800. ' Self.Foo();',
  20801. ' Self.Foo(3);',
  20802. ' with Self do begin',
  20803. ' Foo;',
  20804. ' Foo();',
  20805. ' Foo(4);',
  20806. ' end;',
  20807. ' with o do Foo(5);',
  20808. 'end;',
  20809. 'function THelper.foo(w: word): word;',
  20810. 'begin',
  20811. ' Run;',
  20812. ' Run();',
  20813. ' Run(11);',
  20814. ' Foo;',
  20815. ' Foo();',
  20816. ' Foo(12);',
  20817. ' Self.Foo;',
  20818. ' Self.Foo();',
  20819. ' Self.Foo(13);',
  20820. ' with Self do begin',
  20821. ' Foo;',
  20822. ' Foo();',
  20823. ' Foo(14);',
  20824. ' end;',
  20825. 'end;',
  20826. 'var Obj: TObject;',
  20827. 'begin',
  20828. ' obj.Foo;',
  20829. ' obj.Foo();',
  20830. ' obj.Foo(21);',
  20831. ' with obj do begin',
  20832. ' Foo;',
  20833. ' Foo();',
  20834. ' Foo(22);',
  20835. ' end;',
  20836. '']);
  20837. ConvertProgram;
  20838. CheckSource('TestClassHelper_Method_Call',
  20839. LinesToStr([ // statements
  20840. 'rtl.createClass(this, "TObject", null, function () {',
  20841. ' this.$init = function () {',
  20842. ' };',
  20843. ' this.$final = function () {',
  20844. ' };',
  20845. ' this.Run = function (w) {',
  20846. ' var o = null;',
  20847. ' $mod.THelper.Foo.call(this, 1);',
  20848. ' $mod.THelper.Foo.call(this, 1);',
  20849. ' $mod.THelper.Foo.call(this, 2);',
  20850. ' $mod.THelper.Foo.call(this, 1);',
  20851. ' $mod.THelper.Foo.call(this, 1);',
  20852. ' $mod.THelper.Foo.call(this, 3);',
  20853. ' $mod.THelper.Foo.call(this, 1);',
  20854. ' $mod.THelper.Foo.call(this, 1);',
  20855. ' $mod.THelper.Foo.call(this, 4);',
  20856. ' $mod.THelper.Foo.call(o, 5);',
  20857. ' };',
  20858. '});',
  20859. 'rtl.createHelper(this, "THelper", null, function () {',
  20860. ' this.Foo = function (w) {',
  20861. ' var Result = 0;',
  20862. ' this.Run(10);',
  20863. ' this.Run(10);',
  20864. ' this.Run(11);',
  20865. ' $mod.THelper.Foo.call(this, 1);',
  20866. ' $mod.THelper.Foo.call(this, 1);',
  20867. ' $mod.THelper.Foo.call(this, 12);',
  20868. ' $mod.THelper.Foo.call(this, 1);',
  20869. ' $mod.THelper.Foo.call(this, 1);',
  20870. ' $mod.THelper.Foo.call(this, 13);',
  20871. ' $mod.THelper.Foo.call(this, 1);',
  20872. ' $mod.THelper.Foo.call(this, 1);',
  20873. ' $mod.THelper.Foo.call(this, 14);',
  20874. ' return Result;',
  20875. ' };',
  20876. '});',
  20877. 'this.Obj = null;',
  20878. '']),
  20879. LinesToStr([ // $mod.$main
  20880. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20881. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20882. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20883. 'var $with = $mod.Obj;',
  20884. '$mod.THelper.Foo.call($with, 1);',
  20885. '$mod.THelper.Foo.call($with, 1);',
  20886. '$mod.THelper.Foo.call($with, 22);',
  20887. '']));
  20888. end;
  20889. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20890. begin
  20891. StartProgram(false);
  20892. Add([
  20893. 'type',
  20894. ' TObject = class',
  20895. ' procedure Run(w: word = 10);',
  20896. ' end;',
  20897. ' THelper = class helper for TObject',
  20898. ' function Foo(w: word = 1): word;',
  20899. ' end;',
  20900. 'procedure TObject.Run(w: word);',
  20901. ' procedure Sub(Self: TObject);',
  20902. ' begin',
  20903. ' Foo;',
  20904. ' Foo();',
  20905. ' Self.Foo;',
  20906. ' Self.Foo();',
  20907. ' with Self do begin',
  20908. ' Foo;',
  20909. ' Foo();',
  20910. ' end;',
  20911. ' end;',
  20912. 'begin',
  20913. 'end;',
  20914. 'function THelper.foo(w: word): word;',
  20915. ' procedure Sub(Self: TObject);',
  20916. ' begin',
  20917. ' Run;',
  20918. ' Run();',
  20919. ' Foo;',
  20920. ' Foo();',
  20921. ' Self.Foo;',
  20922. ' Self.Foo();',
  20923. ' with Self do begin',
  20924. ' Foo;',
  20925. ' Foo();',
  20926. ' end;',
  20927. ' end;',
  20928. 'begin',
  20929. 'end;',
  20930. 'begin',
  20931. '']);
  20932. ConvertProgram;
  20933. CheckSource('TestClassHelper_Method_Nested_Call',
  20934. LinesToStr([ // statements
  20935. 'rtl.createClass(this, "TObject", null, function () {',
  20936. ' this.$init = function () {',
  20937. ' };',
  20938. ' this.$final = function () {',
  20939. ' };',
  20940. ' this.Run = function (w) {',
  20941. ' var $Self = this;',
  20942. ' function Sub(Self) {',
  20943. ' $mod.THelper.Foo.call($Self, 1);',
  20944. ' $mod.THelper.Foo.call($Self, 1);',
  20945. ' $mod.THelper.Foo.call(Self, 1);',
  20946. ' $mod.THelper.Foo.call(Self, 1);',
  20947. ' $mod.THelper.Foo.call(Self, 1);',
  20948. ' $mod.THelper.Foo.call(Self, 1);',
  20949. ' };',
  20950. ' };',
  20951. '});',
  20952. 'rtl.createHelper(this, "THelper", null, function () {',
  20953. ' this.Foo = function (w) {',
  20954. ' var $Self = this;',
  20955. ' var Result = 0;',
  20956. ' function Sub(Self) {',
  20957. ' $Self.Run(10);',
  20958. ' $Self.Run(10);',
  20959. ' $mod.THelper.Foo.call($Self, 1);',
  20960. ' $mod.THelper.Foo.call($Self, 1);',
  20961. ' $mod.THelper.Foo.call(Self, 1);',
  20962. ' $mod.THelper.Foo.call(Self, 1);',
  20963. ' $mod.THelper.Foo.call(Self, 1);',
  20964. ' $mod.THelper.Foo.call(Self, 1);',
  20965. ' };',
  20966. ' return Result;',
  20967. ' };',
  20968. '});',
  20969. '']),
  20970. LinesToStr([ // $mod.$main
  20971. '']));
  20972. end;
  20973. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20974. begin
  20975. StartProgram(false);
  20976. Add([
  20977. 'type',
  20978. ' TObject = class',
  20979. ' class procedure Run(w: word = 10);',
  20980. ' end;',
  20981. ' THelper = class helper for TObject',
  20982. ' class function Foo(w: word = 1): word;',
  20983. ' end;',
  20984. 'class procedure TObject.Run(w: word);',
  20985. 'begin',
  20986. ' Foo;',
  20987. ' Foo();',
  20988. ' Self.Foo;',
  20989. ' Self.Foo();',
  20990. ' with Self do begin',
  20991. ' Foo;',
  20992. ' Foo();',
  20993. ' end;',
  20994. 'end;',
  20995. 'class function THelper.foo(w: word): word;',
  20996. 'begin',
  20997. ' Run;',
  20998. ' Run();',
  20999. ' Foo;',
  21000. ' Foo();',
  21001. ' Self.Foo;',
  21002. ' Self.Foo();',
  21003. ' with Self do begin',
  21004. ' Foo;',
  21005. ' Foo();',
  21006. ' end;',
  21007. 'end;',
  21008. 'var',
  21009. ' Obj: TObject;',
  21010. 'begin',
  21011. ' obj.Foo;',
  21012. ' obj.Foo();',
  21013. ' with obj do begin',
  21014. ' Foo;',
  21015. ' Foo();',
  21016. ' end;',
  21017. ' tobject.Foo;',
  21018. ' tobject.Foo();',
  21019. ' with tobject do begin',
  21020. ' Foo;',
  21021. ' Foo();',
  21022. ' end;',
  21023. '']);
  21024. ConvertProgram;
  21025. CheckSource('TestClassHelper_ClassMethod_Call',
  21026. LinesToStr([ // statements
  21027. 'rtl.createClass(this, "TObject", null, function () {',
  21028. ' this.$init = function () {',
  21029. ' };',
  21030. ' this.$final = function () {',
  21031. ' };',
  21032. ' this.Run = function (w) {',
  21033. ' $mod.THelper.Foo.call(this, 1);',
  21034. ' $mod.THelper.Foo.call(this, 1);',
  21035. ' $mod.THelper.Foo.call(this, 1);',
  21036. ' $mod.THelper.Foo.call(this, 1);',
  21037. ' $mod.THelper.Foo.call(this, 1);',
  21038. ' $mod.THelper.Foo.call(this, 1);',
  21039. ' };',
  21040. '});',
  21041. 'rtl.createHelper(this, "THelper", null, function () {',
  21042. ' this.Foo = function (w) {',
  21043. ' var Result = 0;',
  21044. ' this.Run(10);',
  21045. ' this.Run(10);',
  21046. ' $mod.THelper.Foo.call(this, 1);',
  21047. ' $mod.THelper.Foo.call(this, 1);',
  21048. ' $mod.THelper.Foo.call(this, 1);',
  21049. ' $mod.THelper.Foo.call(this, 1);',
  21050. ' $mod.THelper.Foo.call(this, 1);',
  21051. ' $mod.THelper.Foo.call(this, 1);',
  21052. ' return Result;',
  21053. ' };',
  21054. '});',
  21055. 'this.Obj = null;',
  21056. '']),
  21057. LinesToStr([ // $mod.$main
  21058. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21059. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21060. 'var $with = $mod.Obj;',
  21061. '$mod.THelper.Foo.call($with.$class, 1);',
  21062. '$mod.THelper.Foo.call($with.$class, 1);',
  21063. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21064. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21065. 'var $with1 = $mod.TObject;',
  21066. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21067. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21068. '']));
  21069. end;
  21070. procedure TTestModule.TestClassHelper_ClassOf;
  21071. begin
  21072. StartProgram(false);
  21073. Add([
  21074. 'type',
  21075. ' TObject = class',
  21076. ' end;',
  21077. ' TClass = class of TObject;',
  21078. ' THelper = class helper for TObject',
  21079. ' class function Foo(w: word = 1): word;',
  21080. ' end;',
  21081. 'class function THelper.foo(w: word): word;',
  21082. 'begin',
  21083. 'end;',
  21084. 'var',
  21085. ' c: TClass;',
  21086. 'begin',
  21087. ' c.Foo;',
  21088. ' c.Foo();',
  21089. ' with c do begin',
  21090. ' Foo;',
  21091. ' Foo();',
  21092. ' end;',
  21093. '']);
  21094. ConvertProgram;
  21095. CheckSource('TestClassHelper_ClassOf',
  21096. LinesToStr([ // statements
  21097. 'rtl.createClass(this, "TObject", null, function () {',
  21098. ' this.$init = function () {',
  21099. ' };',
  21100. ' this.$final = function () {',
  21101. ' };',
  21102. '});',
  21103. 'rtl.createHelper(this, "THelper", null, function () {',
  21104. ' this.Foo = function (w) {',
  21105. ' var Result = 0;',
  21106. ' return Result;',
  21107. ' };',
  21108. '});',
  21109. 'this.c = null;',
  21110. '']),
  21111. LinesToStr([ // $mod.$main
  21112. '$mod.THelper.Foo.call($mod.c, 1);',
  21113. '$mod.THelper.Foo.call($mod.c, 1);',
  21114. 'var $with = $mod.c;',
  21115. '$mod.THelper.Foo.call($with, 1);',
  21116. '$mod.THelper.Foo.call($with, 1);',
  21117. '']));
  21118. end;
  21119. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21120. begin
  21121. StartProgram(false);
  21122. Add([
  21123. '{$mode objfpc}',
  21124. 'type',
  21125. ' TObject = class',
  21126. ' procedure DoIt;',
  21127. ' end;',
  21128. ' THelper = class helper for TObject',
  21129. ' procedure Fly(w: word = 1);',
  21130. ' class procedure Glide(w: word = 1);',
  21131. ' class procedure Run(w: word = 1); static;',
  21132. ' end;',
  21133. ' TFly = procedure(w: word) of object;',
  21134. ' TGlide = TFly;',
  21135. ' TRun = procedure(w: word);',
  21136. 'var',
  21137. ' f: TFly;',
  21138. ' g: TGlide;',
  21139. ' r: TRun;',
  21140. 'procedure TObject.DoIt;',
  21141. 'begin',
  21142. ' f:=@fly;',
  21143. ' g:=@glide;',
  21144. ' r:=@run;',
  21145. ' f:[email protected];',
  21146. ' g:[email protected];',
  21147. ' r:[email protected];',
  21148. ' with self do begin',
  21149. ' f:=@fly;',
  21150. ' g:=@glide;',
  21151. ' r:=@run;',
  21152. ' end;',
  21153. 'end;',
  21154. 'procedure THelper.fly(w: word);',
  21155. 'begin',
  21156. ' f:=@fly;',
  21157. ' g:=@glide;',
  21158. ' r:=@run;',
  21159. 'end;',
  21160. 'class procedure THelper.glide(w: word);',
  21161. 'begin',
  21162. ' g:=@glide;',
  21163. ' r:=@run;',
  21164. 'end;',
  21165. 'class procedure THelper.run(w: word);',
  21166. 'begin',
  21167. ' g:=@glide;',
  21168. ' r:=@run;',
  21169. 'end;',
  21170. 'var',
  21171. ' Obj: TObject;',
  21172. 'begin',
  21173. ' f:[email protected];',
  21174. ' g:[email protected];',
  21175. ' r:[email protected];',
  21176. ' with obj do begin',
  21177. ' f:=@fly;',
  21178. ' g:=@glide;',
  21179. ' r:=@run;',
  21180. ' end;',
  21181. ' g:[email protected];',
  21182. ' r:[email protected];',
  21183. ' with tobject do begin',
  21184. ' g:=@glide;',
  21185. ' r:=@run;',
  21186. ' end;',
  21187. '']);
  21188. ConvertProgram;
  21189. CheckSource('TestClassHelper_MethodRefObjFPC',
  21190. LinesToStr([ // statements
  21191. 'rtl.createClass(this, "TObject", null, function () {',
  21192. ' this.$init = function () {',
  21193. ' };',
  21194. ' this.$final = function () {',
  21195. ' };',
  21196. ' this.DoIt = function () {',
  21197. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21198. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21199. ' $mod.r = $mod.THelper.Run;',
  21200. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21201. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21202. ' $mod.r = $mod.THelper.Run;',
  21203. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21204. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21205. ' $mod.r = $mod.THelper.Run;',
  21206. ' };',
  21207. '});',
  21208. 'rtl.createHelper(this, "THelper", null, function () {',
  21209. ' this.Fly = function (w) {',
  21210. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21211. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21212. ' $mod.r = $mod.THelper.Run;',
  21213. ' };',
  21214. ' this.Glide = function (w) {',
  21215. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21216. ' $mod.r = $mod.THelper.Run;',
  21217. ' };',
  21218. ' this.Run = function (w) {',
  21219. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21220. ' $mod.r = $mod.THelper.Run;',
  21221. ' };',
  21222. '});',
  21223. 'this.f = null;',
  21224. 'this.g = null;',
  21225. 'this.r = null;',
  21226. 'this.Obj = null;',
  21227. '']),
  21228. LinesToStr([ // $mod.$main
  21229. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21230. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21231. '$mod.r = $mod.THelper.Run;',
  21232. 'var $with = $mod.Obj;',
  21233. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21234. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21235. '$mod.r = $mod.THelper.Run;',
  21236. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21237. '$mod.r = $mod.THelper.Run;',
  21238. 'var $with1 = $mod.TObject;',
  21239. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21240. '$mod.r = $mod.THelper.Run;',
  21241. '']));
  21242. end;
  21243. procedure TTestModule.TestClassHelper_Constructor;
  21244. begin
  21245. StartProgram(false);
  21246. Add([
  21247. 'type',
  21248. ' TObject = class',
  21249. ' constructor Create;',
  21250. ' end;',
  21251. ' TClass = class of TObject;',
  21252. ' THelper = class helper for TObject',
  21253. ' constructor NewHlp(w: word);',
  21254. ' end;',
  21255. 'var',
  21256. ' obj: TObject;',
  21257. ' c: TClass;',
  21258. 'constructor TObject.Create;',
  21259. 'begin',
  21260. ' NewHlp(2);', // normal call
  21261. ' tobject.NewHlp(3);', // new instance
  21262. ' c.newhlp(4);', // new instance
  21263. 'end;',
  21264. 'constructor THelper.NewHlp(w: word);',
  21265. 'begin',
  21266. ' create;', // normal call
  21267. ' tobject.create;', // new instance
  21268. ' NewHlp(2);', // normal call
  21269. ' tobject.NewHlp(3);', // new instance
  21270. ' c.newhlp(4);', // new instance
  21271. 'end;',
  21272. 'begin',
  21273. ' obj.newhlp(2);', // normal call
  21274. ' with Obj do newhlp(12);', // normal call
  21275. ' tobject.newhlp(3);', // new instance
  21276. ' with tobject do newhlp(13);', // new instance
  21277. ' c.newhlp(4);', // new instance
  21278. ' with c do newhlp(14);', // new instance
  21279. '']);
  21280. ConvertProgram;
  21281. CheckSource('TestClassHelper_Constructor',
  21282. LinesToStr([ // statements
  21283. 'rtl.createClass(this, "TObject", null, function () {',
  21284. ' this.$init = function () {',
  21285. ' };',
  21286. ' this.$final = function () {',
  21287. ' };',
  21288. ' this.Create = function () {',
  21289. ' $mod.THelper.NewHlp.call(this, 2);',
  21290. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21291. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21292. ' return this;',
  21293. ' };',
  21294. '});',
  21295. 'rtl.createHelper(this, "THelper", null, function () {',
  21296. ' this.NewHlp = function (w) {',
  21297. ' this.Create();',
  21298. ' $mod.TObject.$create("Create");',
  21299. ' $mod.THelper.NewHlp.call(this, 2);',
  21300. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21301. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21302. ' return this;',
  21303. ' };',
  21304. '});',
  21305. 'this.obj = null;',
  21306. 'this.c = null;',
  21307. '']),
  21308. LinesToStr([ // $mod.$main
  21309. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21310. 'var $with = $mod.obj;',
  21311. '$mod.THelper.NewHlp.call($with, 12);',
  21312. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21313. 'var $with1 = $mod.TObject;',
  21314. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21315. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21316. 'var $with2 = $mod.c;',
  21317. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21318. '']));
  21319. end;
  21320. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21321. begin
  21322. StartProgram(false);
  21323. Add([
  21324. 'type',
  21325. ' TObject = class',
  21326. ' procedure Fly;',
  21327. ' end;',
  21328. ' TObjHelper = class helper for TObject',
  21329. ' procedure Fly;',
  21330. ' end;',
  21331. ' TBird = class',
  21332. ' procedure Fly;',
  21333. ' end;',
  21334. ' TBirdHelper = class helper for TBird',
  21335. ' procedure Fly;',
  21336. ' procedure Walk(w: word);',
  21337. ' end;',
  21338. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21339. ' procedure Fly;',
  21340. ' procedure Walk(w: word);',
  21341. ' end;',
  21342. 'procedure Tobject.fly;',
  21343. 'begin',
  21344. ' inherited;', // ignore
  21345. 'end;',
  21346. 'procedure Tobjhelper.fly;',
  21347. 'begin',
  21348. ' {@TObject_Fly}inherited;',
  21349. ' inherited {@TObject_Fly}Fly;',
  21350. 'end;',
  21351. 'procedure Tbird.fly;',
  21352. 'begin',
  21353. ' {@TObjHelper_Fly}inherited;',
  21354. ' inherited {@TObjHelper_Fly}Fly;',
  21355. 'end;',
  21356. 'procedure Tbirdhelper.fly;',
  21357. 'begin',
  21358. ' {@TBird_Fly}inherited;',
  21359. ' inherited {@TBird_Fly}Fly;',
  21360. 'end;',
  21361. 'procedure Tbirdhelper.walk(w: word);',
  21362. 'begin',
  21363. 'end;',
  21364. 'procedure teagleHelper.fly;',
  21365. 'begin',
  21366. ' {@TBird_Fly}inherited;',
  21367. ' inherited {@TBird_Fly}Fly;',
  21368. 'end;',
  21369. 'procedure teagleHelper.walk(w: word);',
  21370. 'begin',
  21371. ' {@TBirdHelper_Walk}inherited;',
  21372. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21373. 'end;',
  21374. 'begin',
  21375. '']);
  21376. ConvertProgram;
  21377. CheckSource('TestClassHelper_InheritedObjFPC',
  21378. LinesToStr([ // statements
  21379. 'rtl.createClass(this, "TObject", null, function () {',
  21380. ' this.$init = function () {',
  21381. ' };',
  21382. ' this.$final = function () {',
  21383. ' };',
  21384. ' this.Fly = function () {',
  21385. ' };',
  21386. '});',
  21387. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21388. ' this.Fly = function () {',
  21389. ' $mod.TObject.Fly.call(this);',
  21390. ' $mod.TObject.Fly.call(this);',
  21391. ' };',
  21392. '});',
  21393. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21394. ' this.Fly$1 = function () {',
  21395. ' $mod.TObjHelper.Fly.call(this);',
  21396. ' $mod.TObjHelper.Fly.call(this);',
  21397. ' };',
  21398. '});',
  21399. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21400. ' this.Fly = function () {',
  21401. ' $mod.TBird.Fly$1.call(this);',
  21402. ' $mod.TBird.Fly$1.call(this);',
  21403. ' };',
  21404. ' this.Walk = function (w) {',
  21405. ' };',
  21406. '});',
  21407. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21408. ' this.Fly$1 = function () {',
  21409. ' $mod.TBird.Fly$1.call(this);',
  21410. ' $mod.TBird.Fly$1.call(this);',
  21411. ' };',
  21412. ' this.Walk$1 = function (w) {',
  21413. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21414. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21415. ' };',
  21416. '});',
  21417. '']),
  21418. LinesToStr([ // $mod.$main
  21419. '']));
  21420. end;
  21421. procedure TTestModule.TestClassHelper_Property;
  21422. begin
  21423. StartProgram(false);
  21424. Add([
  21425. 'type',
  21426. ' TObject = class',
  21427. ' FSize: word;',
  21428. ' function GetSpeed: word;',
  21429. ' procedure SetSpeed(Value: word);',
  21430. ' end;',
  21431. ' TObjHelper = class helper for TObject',
  21432. ' function GetLeft: word;',
  21433. ' procedure SetLeft(Value: word);',
  21434. ' property Size: word read FSize write FSize;',
  21435. ' property Speed: word read GetSpeed write SetSpeed;',
  21436. ' property Left: word read GetLeft write SetLeft;',
  21437. ' end;',
  21438. ' TBird = class',
  21439. ' property NotRight: word read GetLeft write SetLeft;',
  21440. ' procedure DoIt;',
  21441. ' end;',
  21442. 'var',
  21443. ' b: TBird;',
  21444. 'function Tobject.GetSpeed: word;',
  21445. 'begin',
  21446. ' Size:=Size+11;',
  21447. ' Speed:=Speed+12;',
  21448. ' Result:=Left+13;',
  21449. ' Left:=13;',
  21450. ' Left:=Left+13;',
  21451. ' Self.Size:=Self.Size+21;',
  21452. ' Self.Speed:=Self.Speed+22;',
  21453. ' Self.Left:=Self.Left+23;',
  21454. ' with Self do begin',
  21455. ' Size:=Size+31;',
  21456. ' Speed:=Speed+32;',
  21457. ' Left:=Left+33;',
  21458. ' end;',
  21459. 'end;',
  21460. 'procedure Tobject.SetSpeed(Value: word);',
  21461. 'begin',
  21462. 'end;',
  21463. 'function TObjHelper.GetLeft: word;',
  21464. 'begin',
  21465. ' Size:=Size+11;',
  21466. ' Speed:=Speed+12;',
  21467. ' Left:=Left+13;',
  21468. ' Self.Size:=Self.Size+21;',
  21469. ' Self.Speed:=Self.Speed+22;',
  21470. ' Self.Left:=Self.Left+23;',
  21471. ' with Self do begin',
  21472. ' Size:=Size+31;',
  21473. ' Speed:=Speed+32;',
  21474. ' Left:=Left+33;',
  21475. ' end;',
  21476. 'end;',
  21477. 'procedure TObjHelper.SetLeft(Value: word);',
  21478. 'begin',
  21479. 'end;',
  21480. 'procedure TBird.DoIt;',
  21481. 'begin',
  21482. ' NotRight:=NotRight+11;',
  21483. ' Self.NotRight:=Self.NotRight+21;',
  21484. ' with Self do begin',
  21485. ' NotRight:=NotRight+31;',
  21486. ' end;',
  21487. 'end;',
  21488. 'begin',
  21489. ' b.Size:=b.Size+11;',
  21490. ' b.Speed:=b.Speed+12;',
  21491. ' b.Left:=b.Left+13;',
  21492. ' b.NotRight:=b.NotRight+14;',
  21493. ' with b do begin',
  21494. ' Size:=Size+31;',
  21495. ' Speed:=Speed+32;',
  21496. ' Left:=Left+33;',
  21497. ' NotRight:=NotRight+34;',
  21498. ' end;',
  21499. '']);
  21500. ConvertProgram;
  21501. CheckSource('TestClassHelper_Property',
  21502. LinesToStr([ // statements
  21503. 'rtl.createClass(this, "TObject", null, function () {',
  21504. ' this.$init = function () {',
  21505. ' this.FSize = 0;',
  21506. ' };',
  21507. ' this.$final = function () {',
  21508. ' };',
  21509. ' this.GetSpeed = function () {',
  21510. ' var Result = 0;',
  21511. ' this.FSize = this.FSize + 11;',
  21512. ' this.SetSpeed(this.GetSpeed() + 12);',
  21513. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21514. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21515. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21516. ' this.FSize = this.FSize + 21;',
  21517. ' this.SetSpeed(this.GetSpeed() + 22);',
  21518. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21519. ' this.FSize = this.FSize + 31;',
  21520. ' this.SetSpeed(this.GetSpeed() + 32);',
  21521. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21522. ' return Result;',
  21523. ' };',
  21524. ' this.SetSpeed = function (Value) {',
  21525. ' };',
  21526. '});',
  21527. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21528. ' this.GetLeft = function () {',
  21529. ' var Result = 0;',
  21530. ' this.FSize = this.FSize + 11;',
  21531. ' this.SetSpeed(this.GetSpeed() + 12);',
  21532. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21533. ' this.FSize = this.FSize + 21;',
  21534. ' this.SetSpeed(this.GetSpeed() + 22);',
  21535. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21536. ' this.FSize = this.FSize + 31;',
  21537. ' this.SetSpeed(this.GetSpeed() + 32);',
  21538. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21539. ' return Result;',
  21540. ' };',
  21541. ' this.SetLeft = function (Value) {',
  21542. ' };',
  21543. '});',
  21544. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21545. ' this.DoIt = function () {',
  21546. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21547. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21548. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21549. ' };',
  21550. '});',
  21551. 'this.b = null;',
  21552. '']),
  21553. LinesToStr([ // $mod.$main
  21554. '$mod.b.FSize = $mod.b.FSize + 11;',
  21555. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21556. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21557. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21558. 'var $with = $mod.b;',
  21559. '$with.FSize = $with.FSize + 31;',
  21560. '$with.SetSpeed($with.GetSpeed() + 32);',
  21561. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21562. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21563. '']));
  21564. end;
  21565. procedure TTestModule.TestClassHelper_Property_Array;
  21566. begin
  21567. StartProgram(false);
  21568. Add([
  21569. 'type',
  21570. ' TObject = class',
  21571. ' function GetSpeed(Index: boolean): word;',
  21572. ' procedure SetSpeed(Index: boolean; Value: word);',
  21573. ' end;',
  21574. ' TObjHelper = class helper for TObject',
  21575. ' function GetSize(Index: boolean): word;',
  21576. ' procedure SetSize(Index: boolean; Value: word);',
  21577. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21578. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21579. ' end;',
  21580. ' TBird = class',
  21581. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21582. ' procedure DoIt;',
  21583. ' end;',
  21584. 'var',
  21585. ' b: TBird;',
  21586. 'function Tobject.GetSpeed(Index: boolean): word;',
  21587. 'begin',
  21588. ' Result:=Size[false];',
  21589. ' Size[true]:=Size[false]+11;',
  21590. ' Speed[true]:=Speed[false]+12;',
  21591. ' Self.Size[true]:=Self.Size[false]+21;',
  21592. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21593. ' with Self do begin',
  21594. ' Size[true]:=Size[false]+31;',
  21595. ' Speed[true]:=Speed[false]+32;',
  21596. ' end;',
  21597. 'end;',
  21598. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21599. 'begin',
  21600. 'end;',
  21601. 'function TObjHelper.GetSize(Index: boolean): word;',
  21602. 'begin',
  21603. ' Size[true]:=Size[false]+11;',
  21604. ' Speed[true]:=Speed[false]+12;',
  21605. ' Self.Size[true]:=Self.Size[false]+21;',
  21606. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21607. ' with Self do begin',
  21608. ' Size[true]:=Size[false]+31;',
  21609. ' Speed[true]:=Speed[false]+32;',
  21610. ' end;',
  21611. 'end;',
  21612. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21613. 'begin',
  21614. 'end;',
  21615. 'procedure TBird.DoIt;',
  21616. 'begin',
  21617. ' Items[true]:=Items[false]+11;',
  21618. ' Self.Items[true]:=Self.Items[false]+21;',
  21619. ' with Self do Items[true]:=Items[false]+31;',
  21620. 'end;',
  21621. 'begin',
  21622. ' b.Size[true]:=b.Size[false]+11;',
  21623. ' b.Speed[true]:=b.Speed[false]+12;',
  21624. ' b.Items[true]:=b.Items[false]+13;',
  21625. ' with b do begin',
  21626. ' Size[true]:=Size[false]+21;',
  21627. ' Speed[true]:=Speed[false]+22;',
  21628. ' Items[true]:=Items[false]+23;',
  21629. ' end;',
  21630. '']);
  21631. ConvertProgram;
  21632. CheckSource('TestClassHelper_Property_Array',
  21633. LinesToStr([ // statements
  21634. 'rtl.createClass(this, "TObject", null, function () {',
  21635. ' this.$init = function () {',
  21636. ' };',
  21637. ' this.$final = function () {',
  21638. ' };',
  21639. ' this.GetSpeed = function (Index) {',
  21640. ' var Result = 0;',
  21641. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21642. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21643. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21644. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21645. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21646. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21647. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21648. ' return Result;',
  21649. ' };',
  21650. ' this.SetSpeed = function (Index, Value) {',
  21651. ' };',
  21652. '});',
  21653. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21654. ' this.GetSize = function (Index) {',
  21655. ' var Result = 0;',
  21656. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21657. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21658. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21659. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21661. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21662. ' return Result;',
  21663. ' };',
  21664. ' this.SetSize = function (Index, Value) {',
  21665. ' };',
  21666. '});',
  21667. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21668. ' this.DoIt = function () {',
  21669. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21670. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21671. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21672. ' };',
  21673. '});',
  21674. 'this.b = null;',
  21675. '']),
  21676. LinesToStr([ // $mod.$main
  21677. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21678. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21679. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21680. 'var $with = $mod.b;',
  21681. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21682. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21683. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21684. '']));
  21685. end;
  21686. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21687. begin
  21688. StartProgram(false);
  21689. Add([
  21690. 'type',
  21691. ' TObject = class',
  21692. ' function GetSpeed(Index: boolean): word;',
  21693. ' procedure SetSpeed(Index: boolean; Value: word);',
  21694. ' end;',
  21695. ' TObjHelper = class helper for TObject',
  21696. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21697. ' end;',
  21698. ' TBird = class',
  21699. ' end;',
  21700. ' TBirdHelper = class helper for TBird',
  21701. ' function GetSize(Index: word): boolean;',
  21702. ' procedure SetSize(Index: word; Value: boolean);',
  21703. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21704. ' end;',
  21705. 'function Tobject.GetSpeed(Index: boolean): word;',
  21706. 'begin',
  21707. ' Self[true]:=Self[false]+1;',
  21708. 'end;',
  21709. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21710. 'begin',
  21711. 'end;',
  21712. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21713. 'begin',
  21714. ' Self[1]:=not Self[2];',
  21715. 'end;',
  21716. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21717. 'begin',
  21718. 'end;',
  21719. 'var',
  21720. ' o: TObject;',
  21721. ' b: TBird;',
  21722. 'begin',
  21723. ' o[true]:=o[false]+1;',
  21724. ' b[3]:=not b[4];',
  21725. '']);
  21726. ConvertProgram;
  21727. CheckSource('TestClassHelper_Property_Array_Default',
  21728. LinesToStr([ // statements
  21729. 'rtl.createClass(this, "TObject", null, function () {',
  21730. ' this.$init = function () {',
  21731. ' };',
  21732. ' this.$final = function () {',
  21733. ' };',
  21734. ' this.GetSpeed = function (Index) {',
  21735. ' var Result = 0;',
  21736. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21737. ' return Result;',
  21738. ' };',
  21739. ' this.SetSpeed = function (Index, Value) {',
  21740. ' };',
  21741. '});',
  21742. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21743. '});',
  21744. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21745. '});',
  21746. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21747. ' this.GetSize = function (Index) {',
  21748. ' var Result = false;',
  21749. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21750. ' return Result;',
  21751. ' };',
  21752. ' this.SetSize = function (Index, Value) {',
  21753. ' };',
  21754. '});',
  21755. 'this.o = null;',
  21756. 'this.b = null;',
  21757. '']),
  21758. LinesToStr([ // $mod.$main
  21759. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21760. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21761. '']));
  21762. end;
  21763. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21764. begin
  21765. StartProgram(false);
  21766. Add([
  21767. 'type',
  21768. ' TObject = class',
  21769. ' end;',
  21770. ' TObjHelper = class helper for TObject',
  21771. ' function GetItems(Index: word): TObject;',
  21772. ' procedure SetItems(Index: word; Value: TObject);',
  21773. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21774. ' end;',
  21775. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21776. 'begin',
  21777. ' Self[1][2]:=Self[3][4];',
  21778. 'end;',
  21779. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21780. 'begin',
  21781. 'end;',
  21782. 'var',
  21783. ' o: TObject;',
  21784. 'begin',
  21785. ' o[1][2]:=o[3][4];',
  21786. '']);
  21787. ConvertProgram;
  21788. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21789. LinesToStr([ // statements
  21790. 'rtl.createClass(this, "TObject", null, function () {',
  21791. ' this.$init = function () {',
  21792. ' };',
  21793. ' this.$final = function () {',
  21794. ' };',
  21795. '});',
  21796. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21797. ' this.GetItems = function (Index) {',
  21798. ' var Result = null;',
  21799. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21800. ' return Result;',
  21801. ' };',
  21802. ' this.SetItems = function (Index, Value) {',
  21803. ' };',
  21804. '});',
  21805. 'this.o = null;',
  21806. '']),
  21807. LinesToStr([ // $mod.$main
  21808. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21809. '']));
  21810. end;
  21811. procedure TTestModule.TestClassHelper_ClassProperty;
  21812. begin
  21813. StartProgram(false);
  21814. Add([
  21815. 'type',
  21816. ' TObject = class',
  21817. ' class var FSize: word;',
  21818. ' class function GetSpeed: word;',
  21819. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21820. ' end;',
  21821. ' TObjHelper = class helper for TObject',
  21822. ' class function GetLeft: word;',
  21823. ' class procedure SetLeft(Value: word);',
  21824. ' class property Size: word read FSize write FSize;',
  21825. ' class property Speed: word read GetSpeed write SetSpeed;',
  21826. ' class property Left: word read GetLeft write SetLeft;',
  21827. ' end;',
  21828. ' TBird = class',
  21829. ' class property NotRight: word read GetLeft write SetLeft;',
  21830. ' class procedure DoIt;',
  21831. ' end;',
  21832. ' TBirdClass = class of TBird;',
  21833. 'class function Tobject.GetSpeed: word;',
  21834. 'begin',
  21835. ' Size:=Size+11;',
  21836. ' Speed:=Speed+12;',
  21837. ' Left:=Left+13;',
  21838. ' Self.Size:=Self.Size+21;',
  21839. ' Self.Speed:=Self.Speed+22;',
  21840. ' Self.Left:=Self.Left+23;',
  21841. ' with Self do begin',
  21842. ' Size:=Size+31;',
  21843. ' Speed:=Speed+32;',
  21844. ' Left:=Left+33;',
  21845. ' end;',
  21846. 'end;',
  21847. 'class function TObjHelper.GetLeft: word;',
  21848. 'begin',
  21849. ' Size:=Size+11;',
  21850. ' Speed:=Speed+12;',
  21851. ' Left:=Left+13;',
  21852. ' Self.Size:=Self.Size+21;',
  21853. ' Self.Speed:=Self.Speed+22;',
  21854. ' Self.Left:=Self.Left+23;',
  21855. ' with Self do begin',
  21856. ' Size:=Size+31;',
  21857. ' Speed:=Speed+32;',
  21858. ' Left:=Left+33;',
  21859. ' end;',
  21860. 'end;',
  21861. 'class procedure TObjHelper.SetLeft(Value: word);',
  21862. 'begin',
  21863. 'end;',
  21864. 'class procedure TBird.DoIt;',
  21865. 'begin',
  21866. ' NotRight:=NotRight+11;',
  21867. ' Self.NotRight:=Self.NotRight+21;',
  21868. ' with Self do NotRight:=NotRight+31;',
  21869. 'end;',
  21870. 'var',
  21871. ' b: TBird;',
  21872. ' c: TBirdClass;',
  21873. 'begin',
  21874. ' b.Size:=b.Size+11;',
  21875. ' b.Speed:=b.Speed+12;',
  21876. ' b.Left:=b.Left+13;',
  21877. ' b.NotRight:=b.NotRight+14;',
  21878. ' with b do begin',
  21879. ' Size:=Size+31;',
  21880. ' Speed:=Speed+32;',
  21881. ' Left:=Left+33;',
  21882. ' NotRight:=NotRight+34;',
  21883. ' end;',
  21884. ' c.Size:=c.Size+11;',
  21885. ' c.Speed:=c.Speed+12;',
  21886. ' c.Left:=c.Left+13;',
  21887. ' c.NotRight:=c.NotRight+14;',
  21888. ' with c do begin',
  21889. ' Size:=Size+31;',
  21890. ' Speed:=Speed+32;',
  21891. ' Left:=Left+33;',
  21892. ' NotRight:=NotRight+34;',
  21893. ' end;',
  21894. ' tbird.Size:=tbird.Size+11;',
  21895. ' tbird.Speed:=tbird.Speed+12;',
  21896. ' tbird.Left:=tbird.Left+13;',
  21897. ' tbird.NotRight:=tbird.NotRight+14;',
  21898. ' with tbird do begin',
  21899. ' Size:=Size+31;',
  21900. ' Speed:=Speed+32;',
  21901. ' Left:=Left+33;',
  21902. ' NotRight:=NotRight+34;',
  21903. ' end;',
  21904. '']);
  21905. ConvertProgram;
  21906. CheckSource('TestClassHelper_ClassProperty',
  21907. LinesToStr([ // statements
  21908. 'rtl.createClass(this, "TObject", null, function () {',
  21909. ' this.FSize = 0;',
  21910. ' this.$init = function () {',
  21911. ' };',
  21912. ' this.$final = function () {',
  21913. ' };',
  21914. ' this.GetSpeed = function () {',
  21915. ' var Result = 0;',
  21916. ' $mod.TObject.FSize = this.FSize + 11;',
  21917. ' this.SetSpeed(this.GetSpeed() + 12);',
  21918. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21919. ' $mod.TObject.FSize = this.FSize + 21;',
  21920. ' this.SetSpeed(this.GetSpeed() + 22);',
  21921. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21922. ' $mod.TObject.FSize = this.FSize + 31;',
  21923. ' this.SetSpeed(this.GetSpeed() + 32);',
  21924. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21925. ' return Result;',
  21926. ' };',
  21927. '});',
  21928. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21929. ' this.GetLeft = function () {',
  21930. ' var Result = 0;',
  21931. ' $mod.TObject.FSize = this.FSize + 11;',
  21932. ' this.SetSpeed(this.GetSpeed() + 12);',
  21933. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21934. ' $mod.TObject.FSize = this.FSize + 21;',
  21935. ' this.SetSpeed(this.GetSpeed() + 22);',
  21936. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21937. ' $mod.TObject.FSize = this.FSize + 31;',
  21938. ' this.SetSpeed(this.GetSpeed() + 32);',
  21939. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21940. ' return Result;',
  21941. ' };',
  21942. ' this.SetLeft = function (Value) {',
  21943. ' };',
  21944. '});',
  21945. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21946. ' this.DoIt = function () {',
  21947. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21948. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21949. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21950. ' };',
  21951. '});',
  21952. 'this.b = null;',
  21953. 'this.c = null;',
  21954. '']),
  21955. LinesToStr([ // $mod.$main
  21956. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21957. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21958. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21959. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21960. 'var $with = $mod.b;',
  21961. '$mod.TObject.FSize = $with.FSize + 31;',
  21962. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21963. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21964. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21965. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21966. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21967. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21968. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21969. 'var $with1 = $mod.c;',
  21970. '$mod.TObject.FSize = $with1.FSize + 31;',
  21971. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21972. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21973. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21974. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21975. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21976. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21977. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21978. 'var $with2 = $mod.TBird;',
  21979. '$mod.TObject.FSize = $with2.FSize + 31;',
  21980. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21981. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21982. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21983. '']));
  21984. end;
  21985. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21986. begin
  21987. StartProgram(false);
  21988. Add([
  21989. 'type',
  21990. ' TObject = class',
  21991. ' class function GetSpeed: word; static;',
  21992. ' class procedure SetSpeed(Value: word); static;',
  21993. ' end;',
  21994. ' TObjHelper = class helper for TObject',
  21995. ' class function GetLeft: word; static;',
  21996. ' class procedure SetLeft(Value: word); static;',
  21997. ' class property Speed: word read GetSpeed write SetSpeed;',
  21998. ' class property Left: word read GetLeft write SetLeft;',
  21999. ' end;',
  22000. ' TBird = class',
  22001. ' class property NotRight: word read GetLeft write SetLeft;',
  22002. ' class procedure DoIt; static;',
  22003. ' class procedure DoSome;',
  22004. ' end;',
  22005. ' TBirdClass = class of TBird;',
  22006. 'class function Tobject.GetSpeed: word;',
  22007. 'begin',
  22008. ' Speed:=Speed+12;',
  22009. ' Left:=Left+13;',
  22010. 'end;',
  22011. 'class procedure TObject.SetSpeed(Value: word);',
  22012. 'begin',
  22013. 'end;',
  22014. 'class function TObjHelper.GetLeft: word;',
  22015. 'begin',
  22016. ' Speed:=Speed+12;',
  22017. ' Left:=Left+13;',
  22018. 'end;',
  22019. 'class procedure TObjHelper.SetLeft(Value: word);',
  22020. 'begin',
  22021. 'end;',
  22022. 'class procedure TBird.DoIt;',
  22023. 'begin',
  22024. ' NotRight:=NotRight+11;',
  22025. 'end;',
  22026. 'class procedure TBird.DoSome;',
  22027. 'begin',
  22028. ' Speed:=Speed+12;',
  22029. ' Left:=Left+13;',
  22030. ' Self.Speed:=Self.Speed+22;',
  22031. ' Self.Left:=Self.Left+23;',
  22032. ' with Self do begin',
  22033. ' Speed:=Speed+32;',
  22034. ' Left:=Left+33;',
  22035. ' end;',
  22036. ' NotRight:=NotRight+11;',
  22037. ' Self.NotRight:=Self.NotRight+21;',
  22038. ' with Self do NotRight:=NotRight+31;',
  22039. 'end;',
  22040. 'var',
  22041. ' b: TBird;',
  22042. ' c: TBirdClass;',
  22043. 'begin',
  22044. ' b.Speed:=b.Speed+12;',
  22045. ' b.Left:=b.Left+13;',
  22046. ' b.NotRight:=b.NotRight+14;',
  22047. ' with b do begin',
  22048. ' Speed:=Speed+32;',
  22049. ' Left:=Left+33;',
  22050. ' NotRight:=NotRight+34;',
  22051. ' end;',
  22052. ' c.Speed:=c.Speed+12;',
  22053. ' c.Left:=c.Left+13;',
  22054. ' c.NotRight:=c.NotRight+14;',
  22055. ' with c do begin',
  22056. ' Speed:=Speed+32;',
  22057. ' Left:=Left+33;',
  22058. ' NotRight:=NotRight+34;',
  22059. ' end;',
  22060. ' tbird.Speed:=tbird.Speed+12;',
  22061. ' tbird.Left:=tbird.Left+13;',
  22062. ' tbird.NotRight:=tbird.NotRight+14;',
  22063. ' with tbird do begin',
  22064. ' Speed:=Speed+32;',
  22065. ' Left:=Left+33;',
  22066. ' NotRight:=NotRight+34;',
  22067. ' end;',
  22068. '']);
  22069. ConvertProgram;
  22070. CheckSource('TestClassHelper_ClassPropertyStatic',
  22071. LinesToStr([ // statements
  22072. 'rtl.createClass(this, "TObject", null, function () {',
  22073. ' this.$init = function () {',
  22074. ' };',
  22075. ' this.$final = function () {',
  22076. ' };',
  22077. ' this.GetSpeed = function () {',
  22078. ' var Result = 0;',
  22079. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22080. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22081. ' return Result;',
  22082. ' };',
  22083. ' this.SetSpeed = function (Value) {',
  22084. ' };',
  22085. '});',
  22086. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22087. ' this.GetLeft = function () {',
  22088. ' var Result = 0;',
  22089. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22090. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22091. ' return Result;',
  22092. ' };',
  22093. ' this.SetLeft = function (Value) {',
  22094. ' };',
  22095. '});',
  22096. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22097. ' this.DoIt = function () {',
  22098. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22099. ' };',
  22100. ' this.DoSome = function () {',
  22101. ' this.SetSpeed(this.GetSpeed() + 12);',
  22102. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22103. ' this.SetSpeed(this.GetSpeed() + 22);',
  22104. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22105. ' this.SetSpeed(this.GetSpeed() + 32);',
  22106. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22107. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22108. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22109. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22110. ' };',
  22111. '});',
  22112. 'this.b = null;',
  22113. 'this.c = null;',
  22114. '']),
  22115. LinesToStr([ // $mod.$main
  22116. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22117. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22118. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22119. 'var $with = $mod.b;',
  22120. '$with.SetSpeed($with.GetSpeed() + 32);',
  22121. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22122. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22123. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22124. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22125. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22126. 'var $with1 = $mod.c;',
  22127. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22128. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22129. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22130. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22131. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22132. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22133. 'var $with2 = $mod.TBird;',
  22134. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22135. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22136. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22137. '']));
  22138. end;
  22139. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22140. begin
  22141. StartProgram(false);
  22142. Add([
  22143. 'type',
  22144. ' TObject = class',
  22145. ' class function GetSpeed(Index: boolean): word;',
  22146. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22147. ' end;',
  22148. ' TObjHelper = class helper for TObject',
  22149. ' class function GetSize(Index: boolean): word;',
  22150. ' class procedure SetSize(Index: boolean; Value: word);',
  22151. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22152. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22153. ' end;',
  22154. ' TBird = class',
  22155. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22156. ' class procedure DoIt;',
  22157. ' end;',
  22158. ' TBirdClass = class of TBird;',
  22159. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22160. 'begin',
  22161. ' Size[true]:=Size[false]+11;',
  22162. ' Speed[true]:=Speed[false]+12;',
  22163. ' Self.Size[true]:=Self.Size[false]+21;',
  22164. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22165. ' with Self do begin',
  22166. ' Size[true]:=Size[false]+31;',
  22167. ' Speed[true]:=Speed[false]+32;',
  22168. ' end;',
  22169. 'end;',
  22170. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22171. 'begin',
  22172. ' Size[true]:=Size[false]+11;',
  22173. ' Speed[true]:=Speed[false]+12;',
  22174. ' Self.Size[true]:=Self.Size[false]+21;',
  22175. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22176. ' with Self do begin',
  22177. ' Size[true]:=Size[false]+31;',
  22178. ' Speed[true]:=Speed[false]+32;',
  22179. ' end;',
  22180. 'end;',
  22181. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22182. 'begin',
  22183. 'end;',
  22184. 'class procedure TBird.DoIt;',
  22185. 'begin',
  22186. ' Items[true]:=Items[false]+11;',
  22187. ' Self.Items[true]:=Self.Items[false]+21;',
  22188. ' with Self do Items[true]:=Items[false]+31;',
  22189. 'end;',
  22190. 'var',
  22191. ' b: TBird;',
  22192. ' c: TBirdClass;',
  22193. 'begin',
  22194. ' b.Size[true]:=b.Size[false]+11;',
  22195. ' b.Speed[true]:=b.Speed[false]+12;',
  22196. ' b.Items[true]:=b.Items[false]+13;',
  22197. ' with b do begin',
  22198. ' Size[true]:=Size[false]+21;',
  22199. ' Speed[true]:=Speed[false]+22;',
  22200. ' Items[true]:=Items[false]+23;',
  22201. ' end;',
  22202. ' c.Size[true]:=c.Size[false]+11;',
  22203. ' c.Speed[true]:=c.Speed[false]+12;',
  22204. ' c.Items[true]:=c.Items[false]+13;',
  22205. ' with c do begin',
  22206. ' Size[true]:=Size[false]+21;',
  22207. ' Speed[true]:=Speed[false]+22;',
  22208. ' Items[true]:=Items[false]+23;',
  22209. ' end;',
  22210. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22211. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22212. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22213. ' with TBird do begin',
  22214. ' Size[true]:=Size[false]+21;',
  22215. ' Speed[true]:=Speed[false]+22;',
  22216. ' Items[true]:=Items[false]+23;',
  22217. ' end;',
  22218. '']);
  22219. ConvertProgram;
  22220. CheckSource('TestClassHelper_ClassProperty_Array',
  22221. LinesToStr([ // statements
  22222. 'rtl.createClass(this, "TObject", null, function () {',
  22223. ' this.$init = function () {',
  22224. ' };',
  22225. ' this.$final = function () {',
  22226. ' };',
  22227. ' this.GetSpeed = function (Index) {',
  22228. ' var Result = 0;',
  22229. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22230. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22231. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22232. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22233. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22234. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22235. ' return Result;',
  22236. ' };',
  22237. '});',
  22238. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22239. ' this.GetSize = function (Index) {',
  22240. ' var Result = 0;',
  22241. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22242. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22243. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22244. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22245. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22246. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22247. ' return Result;',
  22248. ' };',
  22249. ' this.SetSize = function (Index, Value) {',
  22250. ' };',
  22251. '});',
  22252. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22253. ' this.DoIt = function () {',
  22254. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22255. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22256. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22257. ' };',
  22258. '});',
  22259. 'this.b = null;',
  22260. 'this.c = null;',
  22261. '']),
  22262. LinesToStr([ // $mod.$main
  22263. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22264. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22265. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22266. 'var $with = $mod.b;',
  22267. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22268. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22269. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22270. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22271. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22272. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22273. 'var $with1 = $mod.c;',
  22274. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22275. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22276. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22277. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22278. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22279. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22280. 'var $with2 = $mod.TBird;',
  22281. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22282. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22283. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22284. '']));
  22285. end;
  22286. procedure TTestModule.TestClassHelper_ForIn;
  22287. begin
  22288. StartProgram(false);
  22289. Add([
  22290. 'type',
  22291. ' TObject = class end;',
  22292. ' TItem = TObject;',
  22293. ' TEnumerator = class',
  22294. ' FCurrent: TItem;',
  22295. ' property Current: TItem read FCurrent;',
  22296. ' function MoveNext: boolean;',
  22297. ' end;',
  22298. ' TBird = class',
  22299. ' end;',
  22300. ' TBirdHelper = class helper for TBird',
  22301. ' function GetEnumerator: TEnumerator;',
  22302. ' end;',
  22303. 'function TEnumerator.MoveNext: boolean;',
  22304. 'begin',
  22305. 'end;',
  22306. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22307. 'begin',
  22308. 'end;',
  22309. 'var',
  22310. ' b: TBird;',
  22311. ' i, i2: TItem;',
  22312. 'begin',
  22313. ' for i in b do i2:=i;']);
  22314. ConvertProgram;
  22315. CheckSource('TestClassHelper_ForIn',
  22316. LinesToStr([ // statements
  22317. 'rtl.createClass(this, "TObject", null, function () {',
  22318. ' this.$init = function () {',
  22319. ' };',
  22320. ' this.$final = function () {',
  22321. ' };',
  22322. '});',
  22323. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22324. ' this.$init = function () {',
  22325. ' $mod.TObject.$init.call(this);',
  22326. ' this.FCurrent = null;',
  22327. ' };',
  22328. ' this.$final = function () {',
  22329. ' this.FCurrent = undefined;',
  22330. ' $mod.TObject.$final.call(this);',
  22331. ' };',
  22332. ' this.MoveNext = function () {',
  22333. ' var Result = false;',
  22334. ' return Result;',
  22335. ' };',
  22336. '});',
  22337. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22338. '});',
  22339. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22340. ' this.GetEnumerator = function () {',
  22341. ' var Result = null;',
  22342. ' return Result;',
  22343. ' };',
  22344. '});',
  22345. 'this.b = null;',
  22346. 'this.i = null;',
  22347. 'this.i2 = null;'
  22348. ]),
  22349. LinesToStr([ // $mod.$main
  22350. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22351. 'try {',
  22352. ' while ($in.MoveNext()){',
  22353. ' $mod.i = $in.FCurrent;',
  22354. ' $mod.i2 = $mod.i;',
  22355. ' }',
  22356. '} finally {',
  22357. ' $in = rtl.freeLoc($in)',
  22358. '};',
  22359. '']));
  22360. end;
  22361. procedure TTestModule.TestClassHelper_PassProperty;
  22362. begin
  22363. StartProgram(false);
  22364. Add([
  22365. 'type',
  22366. ' TObject = class',
  22367. ' FField: TObject;',
  22368. ' property Field: TObject read FField write FField;',
  22369. ' end;',
  22370. ' THelper = class helper for TObject',
  22371. ' procedure Fly;',
  22372. ' class procedure Run;',
  22373. ' class procedure Jump; static;',
  22374. ' end;',
  22375. 'procedure THelper.Fly;',
  22376. 'begin',
  22377. ' Field.Fly;',
  22378. ' Field.Run;',
  22379. ' Field.Jump;',
  22380. ' with Field do begin',
  22381. ' Fly;',
  22382. ' Run;',
  22383. ' Jump;',
  22384. ' end;',
  22385. 'end;',
  22386. 'class procedure THelper.Run;',
  22387. 'begin',
  22388. 'end;',
  22389. 'class procedure THelper.Jump;',
  22390. 'begin',
  22391. 'end;',
  22392. 'var',
  22393. ' b: TObject;',
  22394. 'begin',
  22395. ' b.Field.Fly;',
  22396. ' b.Field.Run;',
  22397. ' b.Field.Jump;',
  22398. ' with b do begin',
  22399. ' Field.Run;',
  22400. ' Field.Fly;',
  22401. ' Field.Jump;',
  22402. ' end;',
  22403. ' with b.Field do begin',
  22404. ' Run;',
  22405. ' Fly;',
  22406. ' Jump;',
  22407. ' end;',
  22408. '']);
  22409. ConvertProgram;
  22410. CheckSource('TestClassHelper_PassProperty',
  22411. LinesToStr([ // statements
  22412. 'rtl.createClass(this, "TObject", null, function () {',
  22413. ' this.$init = function () {',
  22414. ' this.FField = null;',
  22415. ' };',
  22416. ' this.$final = function () {',
  22417. ' this.FField = undefined;',
  22418. ' };',
  22419. '});',
  22420. 'rtl.createHelper(this, "THelper", null, function () {',
  22421. ' this.Fly = function () {',
  22422. ' $mod.THelper.Fly.call(this.FField);',
  22423. ' $mod.THelper.Run.call(this.FField.$class);',
  22424. ' $mod.THelper.Jump();',
  22425. ' var $with = this.FField;',
  22426. ' $mod.THelper.Fly.call($with);',
  22427. ' $mod.THelper.Run.call($with.$class);',
  22428. ' $mod.THelper.Jump();',
  22429. ' };',
  22430. ' this.Run = function () {',
  22431. ' };',
  22432. ' this.Jump = function () {',
  22433. ' };',
  22434. '});',
  22435. 'this.b = null;',
  22436. '']),
  22437. LinesToStr([ // $mod.$main
  22438. '$mod.THelper.Fly.call($mod.b.FField);',
  22439. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22440. '$mod.THelper.Jump();',
  22441. 'var $with = $mod.b;',
  22442. '$mod.THelper.Run.call($with.FField.$class);',
  22443. '$mod.THelper.Fly.call($with.FField);',
  22444. '$mod.THelper.Jump();',
  22445. 'var $with1 = $mod.b.FField;',
  22446. '$mod.THelper.Run.call($with1.$class);',
  22447. '$mod.THelper.Fly.call($with1);',
  22448. '$mod.THelper.Jump();',
  22449. '']));
  22450. end;
  22451. procedure TTestModule.TestExtClassHelper_ClassVar;
  22452. begin
  22453. StartProgram(false);
  22454. Add([
  22455. '{$modeswitch externalclass}',
  22456. 'type',
  22457. ' TExtA = class external name ''ExtObj''',
  22458. ' end;',
  22459. ' THelper = class helper for TExtA',
  22460. ' const',
  22461. ' One = 1;',
  22462. ' Two: word = 2;',
  22463. ' class var',
  22464. ' Glob: word;',
  22465. ' function Foo(w: word): word;',
  22466. ' class function Bar(w: word): word; static;',
  22467. ' end;',
  22468. 'function THelper.foo(w: word): word;',
  22469. 'begin',
  22470. ' Result:=w;',
  22471. ' Two:=One+w;',
  22472. ' Glob:=Glob;',
  22473. ' Result:=Self.Glob;',
  22474. ' Self.Glob:=Self.Glob;',
  22475. ' with Self do Glob:=Glob;',
  22476. 'end;',
  22477. 'class function THelper.bar(w: word): word;',
  22478. 'begin',
  22479. ' Result:=w;',
  22480. ' Two:=One;',
  22481. ' Glob:=Glob;',
  22482. 'end;',
  22483. 'var o: TExtA;',
  22484. 'begin',
  22485. ' texta.two:=texta.one;',
  22486. ' texta.Glob:=texta.Glob;',
  22487. ' with texta do begin',
  22488. ' two:=one;',
  22489. ' Glob:=Glob;',
  22490. ' end;',
  22491. ' o.two:=o.one;',
  22492. ' o.Glob:=o.Glob;',
  22493. ' with o do begin',
  22494. ' two:=one;',
  22495. ' Glob:=Glob;',
  22496. ' end;',
  22497. '']);
  22498. ConvertProgram;
  22499. CheckSource('TestExtClassHelper_ClassVar',
  22500. LinesToStr([ // statements
  22501. 'rtl.createHelper(this, "THelper", null, function () {',
  22502. ' this.One = 1;',
  22503. ' this.Two = 2;',
  22504. ' this.Glob = 0;',
  22505. ' this.Foo = function (w) {',
  22506. ' var Result = 0;',
  22507. ' Result = w;',
  22508. ' $mod.THelper.Two = 1 + w;',
  22509. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22510. ' Result = $mod.THelper.Glob;',
  22511. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22512. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22513. ' return Result;',
  22514. ' };',
  22515. ' this.Bar = function (w) {',
  22516. ' var Result = 0;',
  22517. ' Result = w;',
  22518. ' $mod.THelper.Two = 1;',
  22519. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22520. ' return Result;',
  22521. ' };',
  22522. '});',
  22523. 'this.o = null;',
  22524. '']),
  22525. LinesToStr([ // $mod.$main
  22526. '$mod.THelper.Two = 1;',
  22527. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22528. '$mod.THelper.Two = 1;',
  22529. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22530. '$mod.THelper.Two = 1;',
  22531. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22532. 'var $with = $mod.o;',
  22533. '$mod.THelper.Two = 1;',
  22534. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22535. '']));
  22536. end;
  22537. procedure TTestModule.TestExtClassHelper_Method_Call;
  22538. begin
  22539. StartProgram(false);
  22540. Add([
  22541. '{$modeswitch externalclass}',
  22542. 'type',
  22543. ' TFly = function(w: word): word of object;',
  22544. ' TExtA = class external name ''ExtObj''',
  22545. ' procedure Run(w: word = 10);',
  22546. ' end;',
  22547. ' THelper = class helper for TExtA',
  22548. ' function Foo(w: word = 1): word;',
  22549. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22550. ' end;',
  22551. 'var p: TFly;',
  22552. 'function THelper.foo(w: word): word;',
  22553. 'begin',
  22554. ' Run;',
  22555. ' Run();',
  22556. ' Run(11);',
  22557. ' Foo;',
  22558. ' Foo();',
  22559. ' Foo(12);',
  22560. ' Self.Foo;',
  22561. ' Self.Foo();',
  22562. ' Self.Foo(13);',
  22563. ' Fly;',
  22564. ' Fly();',
  22565. ' with Self do begin',
  22566. ' Foo;',
  22567. ' Foo();',
  22568. ' Foo(14);',
  22569. ' Fly;',
  22570. ' Fly();',
  22571. ' end;',
  22572. ' p:=@Fly;',
  22573. 'end;',
  22574. 'var Obj: TExtA;',
  22575. 'begin',
  22576. ' obj.Foo;',
  22577. ' obj.Foo();',
  22578. ' obj.Foo(21);',
  22579. ' obj.Fly;',
  22580. ' obj.Fly();',
  22581. ' with obj do begin',
  22582. ' Foo;',
  22583. ' Foo();',
  22584. ' Foo(22);',
  22585. ' Fly;',
  22586. ' Fly();',
  22587. ' end;',
  22588. ' p:[email protected];',
  22589. '']);
  22590. ConvertProgram;
  22591. CheckSource('TestExtClassHelper_Method_Call',
  22592. LinesToStr([ // statements
  22593. 'rtl.createHelper(this, "THelper", null, function () {',
  22594. ' this.Foo = function (w) {',
  22595. ' var Result = 0;',
  22596. ' this.Run(10);',
  22597. ' this.Run(10);',
  22598. ' this.Run(11);',
  22599. ' $mod.THelper.Foo.call(this, 1);',
  22600. ' $mod.THelper.Foo.call(this, 1);',
  22601. ' $mod.THelper.Foo.call(this, 12);',
  22602. ' $mod.THelper.Foo.call(this, 1);',
  22603. ' $mod.THelper.Foo.call(this, 1);',
  22604. ' $mod.THelper.Foo.call(this, 13);',
  22605. ' this.Fly(2);',
  22606. ' this.Fly(2);',
  22607. ' $mod.THelper.Foo.call(this, 1);',
  22608. ' $mod.THelper.Foo.call(this, 1);',
  22609. ' $mod.THelper.Foo.call(this, 14);',
  22610. ' this.Fly(2);',
  22611. ' this.Fly(2);',
  22612. ' $mod.p = rtl.createCallback(this, "Fly");',
  22613. ' return Result;',
  22614. ' };',
  22615. '});',
  22616. 'this.p = null;',
  22617. 'this.Obj = null;',
  22618. '']),
  22619. LinesToStr([ // $mod.$main
  22620. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22621. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22622. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22623. '$mod.Obj.Fly(2);',
  22624. '$mod.Obj.Fly(2);',
  22625. 'var $with = $mod.Obj;',
  22626. '$mod.THelper.Foo.call($with, 1);',
  22627. '$mod.THelper.Foo.call($with, 1);',
  22628. '$mod.THelper.Foo.call($with, 22);',
  22629. '$with.Fly(2);',
  22630. '$with.Fly(2);',
  22631. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22632. '']));
  22633. end;
  22634. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22635. begin
  22636. StartProgram(false);
  22637. Add([
  22638. '{$modeswitch externalclass}',
  22639. 'type',
  22640. ' TExtA = class external name ''ExtObj''',
  22641. ' procedure Run(w: word = 10);',
  22642. ' end;',
  22643. ' THelper = class helper for TExtA',
  22644. ' class procedure Fly;',
  22645. ' end;',
  22646. 'class procedure THelper.Fly;',
  22647. 'begin end;',
  22648. 'begin',
  22649. '']);
  22650. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22651. nHelperClassMethodForExtClassMustBeStatic);
  22652. ConvertProgram;
  22653. end;
  22654. procedure TTestModule.TestRecordHelper_ClassVar;
  22655. begin
  22656. StartProgram(false);
  22657. Add([
  22658. 'type',
  22659. ' TRec = record',
  22660. ' end;',
  22661. ' THelper = record helper for TRec',
  22662. ' const',
  22663. ' One = 1;',
  22664. ' Two: word = 2;',
  22665. ' class var',
  22666. ' Glob: word;',
  22667. ' function Foo(w: word): word;',
  22668. ' class function Bar(w: word): word; static;',
  22669. ' end;',
  22670. 'function THelper.foo(w: word): word;',
  22671. 'begin',
  22672. ' Result:=w;',
  22673. ' Two:=One+w;',
  22674. ' Glob:=Glob;',
  22675. ' Result:=Self.Glob;',
  22676. ' Self.Glob:=Self.Glob;',
  22677. ' with Self do Glob:=Glob;',
  22678. ' Self:=Self;',
  22679. 'end;',
  22680. 'class function THelper.bar(w: word): word;',
  22681. 'begin',
  22682. ' Result:=w;',
  22683. ' Two:=One;',
  22684. ' Glob:=Glob;',
  22685. 'end;',
  22686. 'var r: TRec;',
  22687. 'begin',
  22688. ' trec.two:=trec.one;',
  22689. ' trec.Glob:=trec.Glob;',
  22690. ' with trec do begin',
  22691. ' two:=one;',
  22692. ' Glob:=Glob;',
  22693. ' end;',
  22694. ' r.two:=r.one;',
  22695. ' r.Glob:=r.Glob;',
  22696. ' with r do begin',
  22697. ' two:=one;',
  22698. ' Glob:=Glob;',
  22699. ' end;',
  22700. '']);
  22701. ConvertProgram;
  22702. CheckSource('TestRecordHelper_ClassVar',
  22703. LinesToStr([ // statements
  22704. 'rtl.recNewT(this, "TRec", function () {',
  22705. ' this.$eq = function (b) {',
  22706. ' return true;',
  22707. ' };',
  22708. ' this.$assign = function (s) {',
  22709. ' return this;',
  22710. ' };',
  22711. '});',
  22712. 'rtl.createHelper(this, "THelper", null, function () {',
  22713. ' this.One = 1;',
  22714. ' this.Two = 2;',
  22715. ' this.Glob = 0;',
  22716. ' this.Foo = function (w) {',
  22717. ' var Result = 0;',
  22718. ' Result = w;',
  22719. ' $mod.THelper.Two = 1 + w;',
  22720. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22721. ' Result = $mod.THelper.Glob;',
  22722. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22723. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22724. ' this.$assign(this);',
  22725. ' return Result;',
  22726. ' };',
  22727. ' this.Bar = function (w) {',
  22728. ' var Result = 0;',
  22729. ' Result = w;',
  22730. ' $mod.THelper.Two = 1;',
  22731. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22732. ' return Result;',
  22733. ' };',
  22734. '});',
  22735. 'this.r = this.TRec.$new();',
  22736. '']),
  22737. LinesToStr([ // $mod.$main
  22738. '$mod.THelper.Two = 1;',
  22739. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22740. 'var $with = $mod.TRec;',
  22741. '$mod.THelper.Two = 1;',
  22742. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22743. '$mod.THelper.Two = 1;',
  22744. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22745. 'var $with1 = $mod.r;',
  22746. '$mod.THelper.Two = 1;',
  22747. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22748. '']));
  22749. end;
  22750. procedure TTestModule.TestRecordHelper_Method_Call;
  22751. begin
  22752. StartProgram(false);
  22753. Add([
  22754. '{$modeswitch AdvancedRecords}',
  22755. 'type',
  22756. ' TRec = record',
  22757. ' procedure Run(w: word = 10);',
  22758. ' end;',
  22759. ' THelper = record helper for TRec',
  22760. ' function Foo(w: word = 1): word;',
  22761. ' end;',
  22762. 'procedure TRec.Run(w: word);',
  22763. 'begin',
  22764. ' Foo;',
  22765. ' Foo();',
  22766. ' Foo(2);',
  22767. ' Self.Foo;',
  22768. ' Self.Foo();',
  22769. ' Self.Foo(3);',
  22770. ' with Self do begin',
  22771. ' Foo;',
  22772. ' Foo();',
  22773. ' Foo(4);',
  22774. ' end;',
  22775. 'end;',
  22776. 'function THelper.foo(w: word): word;',
  22777. 'begin',
  22778. ' Run;',
  22779. ' Run();',
  22780. ' Run(11);',
  22781. ' Foo;',
  22782. ' Foo();',
  22783. ' Foo(12);',
  22784. ' Self.Foo;',
  22785. ' Self.Foo();',
  22786. ' Self.Foo(13);',
  22787. ' with Self do begin',
  22788. ' Foo;',
  22789. ' Foo();',
  22790. ' Foo(14);',
  22791. ' end;',
  22792. 'end;',
  22793. 'var Rec: TRec;',
  22794. 'begin',
  22795. ' Rec.Foo;',
  22796. ' Rec.Foo();',
  22797. ' Rec.Foo(21);',
  22798. ' with Rec do begin',
  22799. ' Foo;',
  22800. ' Foo();',
  22801. ' Foo(22);',
  22802. ' end;',
  22803. '']);
  22804. ConvertProgram;
  22805. CheckSource('TestRecordHelper_Method_Call',
  22806. LinesToStr([ // statements
  22807. 'rtl.recNewT(this, "TRec", function () {',
  22808. ' this.$eq = function (b) {',
  22809. ' return true;',
  22810. ' };',
  22811. ' this.$assign = function (s) {',
  22812. ' return this;',
  22813. ' };',
  22814. ' this.Run = function (w) {',
  22815. ' $mod.THelper.Foo.call(this, 1);',
  22816. ' $mod.THelper.Foo.call(this, 1);',
  22817. ' $mod.THelper.Foo.call(this, 2);',
  22818. ' $mod.THelper.Foo.call(this, 1);',
  22819. ' $mod.THelper.Foo.call(this, 1);',
  22820. ' $mod.THelper.Foo.call(this, 3);',
  22821. ' $mod.THelper.Foo.call(this, 1);',
  22822. ' $mod.THelper.Foo.call(this, 1);',
  22823. ' $mod.THelper.Foo.call(this, 4);',
  22824. ' };',
  22825. '});',
  22826. 'rtl.createHelper(this, "THelper", null, function () {',
  22827. ' this.Foo = function (w) {',
  22828. ' var Result = 0;',
  22829. ' this.Run(10);',
  22830. ' this.Run(10);',
  22831. ' this.Run(11);',
  22832. ' $mod.THelper.Foo.call(this, 1);',
  22833. ' $mod.THelper.Foo.call(this, 1);',
  22834. ' $mod.THelper.Foo.call(this, 12);',
  22835. ' $mod.THelper.Foo.call(this, 1);',
  22836. ' $mod.THelper.Foo.call(this, 1);',
  22837. ' $mod.THelper.Foo.call(this, 13);',
  22838. ' $mod.THelper.Foo.call(this, 1);',
  22839. ' $mod.THelper.Foo.call(this, 1);',
  22840. ' $mod.THelper.Foo.call(this, 14);',
  22841. ' return Result;',
  22842. ' };',
  22843. '});',
  22844. 'this.Rec = this.TRec.$new();',
  22845. '']),
  22846. LinesToStr([ // $mod.$main
  22847. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22848. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22849. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22850. 'var $with = $mod.Rec;',
  22851. '$mod.THelper.Foo.call($with, 1);',
  22852. '$mod.THelper.Foo.call($with, 1);',
  22853. '$mod.THelper.Foo.call($with, 22);',
  22854. '']));
  22855. end;
  22856. procedure TTestModule.TestRecordHelper_Constructor;
  22857. begin
  22858. StartProgram(false);
  22859. Add([
  22860. '{$modeswitch AdvancedRecords}',
  22861. 'type',
  22862. ' TRec = record',
  22863. ' constructor Create(w: word);',
  22864. ' end;',
  22865. ' THelper = record helper for TRec',
  22866. ' constructor NewHlp(w: word);',
  22867. ' end;',
  22868. 'var',
  22869. ' Rec: TRec;',
  22870. 'constructor TRec.Create(w: word);',
  22871. 'begin',
  22872. ' NewHlp(2);', // normal call
  22873. ' trec.NewHlp(3);', // new instance
  22874. 'end;',
  22875. 'constructor THelper.NewHlp(w: word);',
  22876. 'begin',
  22877. ' create(2);', // normal call
  22878. ' trec.create(3);', // new instance
  22879. ' NewHlp(4);', // normal call
  22880. ' trec.NewHlp(5);', // new instance
  22881. 'end;',
  22882. 'begin',
  22883. ' rec.newhlp(2);', // normal call
  22884. ' with rec do newhlp(12);', // normal call
  22885. ' trec.newhlp(3);', // new instance
  22886. ' with trec do newhlp(13);', // new instance
  22887. '']);
  22888. ConvertProgram;
  22889. CheckSource('TestRecordHelper_Constructor',
  22890. LinesToStr([ // statements
  22891. 'rtl.recNewT(this, "TRec", function () {',
  22892. ' this.$eq = function (b) {',
  22893. ' return true;',
  22894. ' };',
  22895. ' this.$assign = function (s) {',
  22896. ' return this;',
  22897. ' };',
  22898. ' this.Create = function (w) {',
  22899. ' $mod.THelper.NewHlp.call(this, 2);',
  22900. ' $mod.THelper.$new("NewHlp", [3]);',
  22901. ' return this;',
  22902. ' };',
  22903. '});',
  22904. 'rtl.createHelper(this, "THelper", null, function () {',
  22905. ' this.NewHlp = function (w) {',
  22906. ' this.Create(2);',
  22907. ' $mod.TRec.$new().Create(3);',
  22908. ' $mod.THelper.NewHlp.call(this, 4);',
  22909. ' $mod.THelper.$new("NewHlp", [5]);',
  22910. ' return this;',
  22911. ' };',
  22912. ' this.$new = function (fn, args) {',
  22913. ' return this[fn].apply($mod.TRec.$new(), args);',
  22914. ' };',
  22915. '});',
  22916. 'this.Rec = this.TRec.$new();',
  22917. '']),
  22918. LinesToStr([ // $mod.$main
  22919. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22920. 'var $with = $mod.Rec;',
  22921. '$mod.THelper.NewHlp.call($with, 12);',
  22922. '$mod.THelper.$new("NewHlp", [3]);',
  22923. 'var $with1 = $mod.TRec;',
  22924. '$mod.THelper.$new("NewHlp", [13]);',
  22925. '']));
  22926. end;
  22927. procedure TTestModule.TestTypeHelper_ClassVar;
  22928. begin
  22929. StartProgram(false);
  22930. Add([
  22931. '{$modeswitch typehelpers}',
  22932. 'type',
  22933. ' THelper = type helper for byte',
  22934. ' const',
  22935. ' One = 1;',
  22936. ' Two: word = 2;',
  22937. ' class var',
  22938. ' Glob: word;',
  22939. ' function Foo(w: word): word;',
  22940. ' class function Bar(w: word): word; static;',
  22941. ' end;',
  22942. 'function THelper.foo(w: word): word;',
  22943. 'begin',
  22944. ' Result:=w;',
  22945. ' Two:=One+w;',
  22946. ' Glob:=Glob;',
  22947. ' Result:=Self.Glob;',
  22948. ' Self.Glob:=Self.Glob;',
  22949. ' with Self do Glob:=Glob;',
  22950. 'end;',
  22951. 'class function THelper.bar(w: word): word;',
  22952. 'begin',
  22953. ' Result:=w;',
  22954. ' Two:=One;',
  22955. ' Glob:=Glob;',
  22956. 'end;',
  22957. 'var b: byte;',
  22958. 'begin',
  22959. ' byte.two:=byte.one;',
  22960. ' byte.Glob:=byte.Glob;',
  22961. ' with byte do begin',
  22962. ' two:=one;',
  22963. ' Glob:=Glob;',
  22964. ' end;',
  22965. ' b.two:=b.one;',
  22966. ' b.Glob:=b.Glob;',
  22967. ' with b do begin',
  22968. ' two:=one;',
  22969. ' Glob:=Glob;',
  22970. ' end;',
  22971. '']);
  22972. ConvertProgram;
  22973. CheckSource('TestTypeHelper_ClassVar',
  22974. LinesToStr([ // statements
  22975. 'rtl.createHelper(this, "THelper", null, function () {',
  22976. ' this.One = 1;',
  22977. ' this.Two = 2;',
  22978. ' this.Glob = 0;',
  22979. ' this.Foo = function (w) {',
  22980. ' var Result = 0;',
  22981. ' Result = w;',
  22982. ' $mod.THelper.Two = 1 + w;',
  22983. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22984. ' Result = $mod.THelper.Glob;',
  22985. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22986. ' var $with = this.get();',
  22987. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22988. ' return Result;',
  22989. ' };',
  22990. ' this.Bar = function (w) {',
  22991. ' var Result = 0;',
  22992. ' Result = w;',
  22993. ' $mod.THelper.Two = 1;',
  22994. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22995. ' return Result;',
  22996. ' };',
  22997. '});',
  22998. 'this.b = 0;',
  22999. '']),
  23000. LinesToStr([ // $mod.$main
  23001. '$mod.THelper.Two = 1;',
  23002. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23003. '$mod.THelper.Two = 1;',
  23004. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23005. '$mod.THelper.Two = 1;',
  23006. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23007. 'var $with = $mod.b;',
  23008. '$mod.THelper.Two = 1;',
  23009. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23010. '']));
  23011. end;
  23012. procedure TTestModule.TestTypeHelper_PassResultElement;
  23013. begin
  23014. StartProgram(false);
  23015. Add([
  23016. '{$modeswitch typehelpers}',
  23017. 'type',
  23018. ' THelper = type helper for word',
  23019. ' procedure DoIt(e: byte = 123);',
  23020. ' class procedure DoSome(e: byte = 456); static;',
  23021. ' end;',
  23022. 'procedure THelper.DoIt(e: byte);',
  23023. 'begin',
  23024. 'end;',
  23025. 'class procedure THelper.DoSome(e: byte);',
  23026. 'begin',
  23027. 'end;',
  23028. 'function Foo(w: word): word;',
  23029. 'begin',
  23030. ' Result.DoIt;',
  23031. ' Result.DoIt();',
  23032. ' Result.DoSome;',
  23033. ' Result.DoSome();',
  23034. ' with Result do begin',
  23035. ' DoIt;',
  23036. ' DoIt();',
  23037. ' DoSome;',
  23038. ' DoSome();',
  23039. ' end;',
  23040. 'end;',
  23041. 'begin',
  23042. '']);
  23043. ConvertProgram;
  23044. CheckSource('TestTypeHelper_PassResultElement',
  23045. LinesToStr([ // statements
  23046. 'rtl.createHelper(this, "THelper", null, function () {',
  23047. ' this.DoIt = function (e) {',
  23048. ' };',
  23049. ' this.DoSome = function (e) {',
  23050. ' };',
  23051. '});',
  23052. 'this.Foo = function (w) {',
  23053. ' var Result = 0;',
  23054. ' $mod.THelper.DoIt.call({',
  23055. ' get: function () {',
  23056. ' return Result;',
  23057. ' },',
  23058. ' set: function (v) {',
  23059. ' Result = v;',
  23060. ' }',
  23061. ' }, 123);',
  23062. ' $mod.THelper.DoIt.call({',
  23063. ' get: function () {',
  23064. ' return Result;',
  23065. ' },',
  23066. ' set: function (v) {',
  23067. ' Result = v;',
  23068. ' }',
  23069. ' }, 123);',
  23070. ' $mod.THelper.DoSome(456);',
  23071. ' $mod.THelper.DoSome(456);',
  23072. ' $mod.THelper.DoIt.call({',
  23073. ' get: function () {',
  23074. ' return Result;',
  23075. ' },',
  23076. ' set: function (v) {',
  23077. ' Result = v;',
  23078. ' }',
  23079. ' }, 123);',
  23080. ' $mod.THelper.DoIt.call({',
  23081. ' get: function () {',
  23082. ' return Result;',
  23083. ' },',
  23084. ' set: function (v) {',
  23085. ' Result = v;',
  23086. ' }',
  23087. ' }, 123);',
  23088. ' $mod.THelper.DoSome(456);',
  23089. ' $mod.THelper.DoSome(456);',
  23090. ' return Result;',
  23091. '};',
  23092. '']),
  23093. LinesToStr([ // $mod.$main
  23094. '']));
  23095. end;
  23096. procedure TTestModule.TestTypeHelper_PassArgs;
  23097. begin
  23098. StartProgram(false);
  23099. Add([
  23100. '{$modeswitch typehelpers}',
  23101. 'type',
  23102. ' THelper = type helper for word',
  23103. ' procedure DoIt(e: byte = 123);',
  23104. ' end;',
  23105. 'procedure THelper.DoIt(e: byte);',
  23106. 'begin',
  23107. 'end;',
  23108. 'procedure FooDefault(a: word);',
  23109. 'begin',
  23110. ' a.DoIt;',
  23111. ' with a do DoIt;',
  23112. 'end;',
  23113. 'procedure FooConst(const a: word);',
  23114. 'begin',
  23115. ' a.DoIt;',
  23116. ' with a do DoIt;',
  23117. 'end;',
  23118. 'procedure FooVar(var a: word);',
  23119. 'begin',
  23120. ' a.DoIt;',
  23121. ' with a do DoIt;',
  23122. 'end;',
  23123. 'begin',
  23124. '']);
  23125. ConvertProgram;
  23126. CheckSource('TestTypeHelper_PassArgs',
  23127. LinesToStr([ // statements
  23128. 'rtl.createHelper(this, "THelper", null, function () {',
  23129. ' this.DoIt = function (e) {',
  23130. ' };',
  23131. '});',
  23132. 'this.FooDefault = function (a) {',
  23133. ' $mod.THelper.DoIt.call({',
  23134. ' get: function () {',
  23135. ' return a;',
  23136. ' },',
  23137. ' set: function (v) {',
  23138. ' a = v;',
  23139. ' }',
  23140. ' }, 123);',
  23141. ' $mod.THelper.DoIt.call({',
  23142. ' get: function () {',
  23143. ' return a;',
  23144. ' },',
  23145. ' set: function (v) {',
  23146. ' a = v;',
  23147. ' }',
  23148. ' }, 123);',
  23149. '};',
  23150. 'this.FooConst = function (a) {',
  23151. ' $mod.THelper.DoIt.call({',
  23152. ' get: function () {',
  23153. ' return a;',
  23154. ' },',
  23155. ' set: function (v) {',
  23156. ' rtl.raiseE("EPropReadOnly");',
  23157. ' }',
  23158. ' }, 123);',
  23159. ' $mod.THelper.DoIt.call({',
  23160. ' get: function () {',
  23161. ' return a;',
  23162. ' },',
  23163. ' set: function () {',
  23164. ' rtl.raiseE("EPropReadOnly");',
  23165. ' }',
  23166. ' }, 123);',
  23167. '};',
  23168. 'this.FooVar = function (a) {',
  23169. ' $mod.THelper.DoIt.call(a, 123);',
  23170. ' var $with = a.get();',
  23171. ' $mod.THelper.DoIt.call(a, 123);',
  23172. '};',
  23173. '']),
  23174. LinesToStr([ // $mod.$main
  23175. '']));
  23176. end;
  23177. procedure TTestModule.TestTypeHelper_PassVarConst;
  23178. begin
  23179. StartProgram(false);
  23180. Add([
  23181. '{$modeswitch typehelpers}',
  23182. 'type',
  23183. ' THelper = type helper for word',
  23184. ' procedure DoIt(e: byte = 123);',
  23185. ' end;',
  23186. 'procedure THelper.DoIt(e: byte);',
  23187. 'begin',
  23188. 'end;',
  23189. 'var a: word;',
  23190. 'const c: word = 2;',
  23191. '{$writeableconst off}',
  23192. 'const r: word = 3;',
  23193. 'begin',
  23194. ' a.DoIt;',
  23195. ' with a do DoIt;',
  23196. ' c.DoIt;',
  23197. ' with c do DoIt;',
  23198. ' r.DoIt;',
  23199. ' with r do DoIt;',
  23200. '']);
  23201. ConvertProgram;
  23202. CheckSource('TestTypeHelper_PassVarConst',
  23203. LinesToStr([ // statements
  23204. 'rtl.createHelper(this, "THelper", null, function () {',
  23205. ' this.DoIt = function (e) {',
  23206. ' };',
  23207. '});',
  23208. 'this.a = 0;',
  23209. 'this.c = 2;',
  23210. 'this.r = 3;',
  23211. '']),
  23212. LinesToStr([ // $mod.$main
  23213. '$mod.THelper.DoIt.call({',
  23214. ' p: $mod,',
  23215. ' get: function () {',
  23216. ' return this.p.a;',
  23217. ' },',
  23218. ' set: function (v) {',
  23219. ' this.p.a = v;',
  23220. ' }',
  23221. '}, 123);',
  23222. 'var $with = $mod.a;',
  23223. '$mod.THelper.DoIt.call({',
  23224. ' get: function () {',
  23225. ' return $with;',
  23226. ' },',
  23227. ' set: function (v) {',
  23228. ' $with = v;',
  23229. ' }',
  23230. '}, 123);',
  23231. '$mod.THelper.DoIt.call({',
  23232. ' p: $mod,',
  23233. ' get: function () {',
  23234. ' return this.p.c;',
  23235. ' },',
  23236. ' set: function (v) {',
  23237. ' this.p.c = v;',
  23238. ' }',
  23239. '}, 123);',
  23240. 'var $with1 = $mod.c;',
  23241. '$mod.THelper.DoIt.call({',
  23242. ' get: function () {',
  23243. ' return $with1;',
  23244. ' },',
  23245. ' set: function (v) {',
  23246. ' $with1 = v;',
  23247. ' }',
  23248. '}, 123);',
  23249. '$mod.THelper.DoIt.call({',
  23250. ' get: function () {',
  23251. ' return 3;',
  23252. ' },',
  23253. ' set: function (v) {',
  23254. ' rtl.raiseE("EPropReadOnly");',
  23255. ' }',
  23256. '}, 123);',
  23257. 'var $with2 = 3;',
  23258. ' $mod.THelper.DoIt.call({',
  23259. ' get: function () {',
  23260. ' return $with2;',
  23261. ' },',
  23262. ' set: function () {',
  23263. ' rtl.raiseE("EPropReadOnly");',
  23264. ' }',
  23265. ' }, 123);',
  23266. '']));
  23267. end;
  23268. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23269. begin
  23270. StartProgram(false);
  23271. Add([
  23272. '{$modeswitch typehelpers}',
  23273. 'type',
  23274. ' THelper = type helper for word',
  23275. ' procedure DoIt(e: byte = 123);',
  23276. ' end;',
  23277. 'procedure THelper.DoIt(e: byte);',
  23278. 'begin',
  23279. 'end;',
  23280. 'function Foo(b: byte = 1): word;',
  23281. 'begin',
  23282. 'end;',
  23283. 'begin',
  23284. ' Foo.DoIt;',
  23285. ' Foo().DoIt;',
  23286. ' with Foo do DoIt;',
  23287. ' with Foo() do DoIt;',
  23288. '']);
  23289. ConvertProgram;
  23290. CheckSource('TestTypeHelper_PassFuncResult',
  23291. LinesToStr([ // statements
  23292. 'rtl.createHelper(this, "THelper", null, function () {',
  23293. ' this.DoIt = function (e) {',
  23294. ' };',
  23295. '});',
  23296. 'this.Foo = function (b) {',
  23297. ' var Result = 0;',
  23298. ' return Result;',
  23299. '};',
  23300. '']),
  23301. LinesToStr([ // $mod.$main
  23302. '$mod.THelper.DoIt.call({',
  23303. ' a: $mod.Foo(1),',
  23304. ' get: function () {',
  23305. ' return this.a;',
  23306. ' },',
  23307. ' set: function (v) {',
  23308. ' this.a = v;',
  23309. ' }',
  23310. '}, 123);',
  23311. '$mod.THelper.DoIt.call({',
  23312. ' a: $mod.Foo(1),',
  23313. ' get: function () {',
  23314. ' return this.a;',
  23315. ' },',
  23316. ' set: function (v) {',
  23317. ' this.a = v;',
  23318. ' }',
  23319. '}, 123);',
  23320. 'var $with = $mod.Foo(1);',
  23321. '$mod.THelper.DoIt.call({',
  23322. ' get: function () {',
  23323. ' return $with;',
  23324. ' },',
  23325. ' set: function (v) {',
  23326. ' $with = v;',
  23327. ' }',
  23328. '}, 123);',
  23329. 'var $with1 = $mod.Foo(1);',
  23330. '$mod.THelper.DoIt.call({',
  23331. ' get: function () {',
  23332. ' return $with1;',
  23333. ' },',
  23334. ' set: function (v) {',
  23335. ' $with1 = v;',
  23336. ' }',
  23337. '}, 123);',
  23338. '']));
  23339. end;
  23340. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23341. begin
  23342. StartProgram(false);
  23343. Add([
  23344. '{$modeswitch typehelpers}',
  23345. 'type',
  23346. ' TObject = class',
  23347. ' FField: word;',
  23348. ' procedure SetField(Value: word);',
  23349. ' property Field: word read FField write SetField;',
  23350. ' end;',
  23351. ' THelper = type helper for word',
  23352. ' procedure Fly;',
  23353. ' class procedure Run; static;',
  23354. ' end;',
  23355. 'procedure TObject.SetField(Value: word);',
  23356. 'begin',
  23357. ' Field.Fly;',
  23358. ' Field.Run;',
  23359. ' Self.Field.Fly;',
  23360. ' Self.Field.Run;',
  23361. ' with Self do begin',
  23362. ' Field.Fly;',
  23363. ' Field.Run;',
  23364. ' end;',
  23365. ' with Self.Field do begin',
  23366. ' Fly;',
  23367. ' Run;',
  23368. ' end;',
  23369. 'end;',
  23370. 'procedure THelper.Fly;',
  23371. 'begin',
  23372. 'end;',
  23373. 'class procedure THelper.Run;',
  23374. 'begin',
  23375. 'end;',
  23376. 'var',
  23377. ' o: TObject;',
  23378. 'begin',
  23379. ' o.Field.Fly;',
  23380. ' o.Field.Run;',
  23381. ' with o do begin',
  23382. ' Field.Fly;',
  23383. ' Field.Run;',
  23384. ' end;',
  23385. ' with o.Field do begin',
  23386. ' Fly;',
  23387. ' Run;',
  23388. ' end;',
  23389. '']);
  23390. ConvertProgram;
  23391. CheckSource('TestTypeHelper_PassPropertyField',
  23392. LinesToStr([ // statements
  23393. 'rtl.createClass(this, "TObject", null, function () {',
  23394. ' this.$init = function () {',
  23395. ' this.FField = 0;',
  23396. ' };',
  23397. ' this.$final = function () {',
  23398. ' };',
  23399. ' this.SetField = function (Value) {',
  23400. ' $mod.THelper.Fly.call({',
  23401. ' p: this,',
  23402. ' get: function () {',
  23403. ' return this.p.FField;',
  23404. ' },',
  23405. ' set: function (v) {',
  23406. ' this.p.FField = v;',
  23407. ' }',
  23408. ' });',
  23409. ' $mod.THelper.Run();',
  23410. ' $mod.THelper.Fly.call({',
  23411. ' p: this,',
  23412. ' get: function () {',
  23413. ' return this.p.FField;',
  23414. ' },',
  23415. ' set: function (v) {',
  23416. ' this.p.FField = v;',
  23417. ' }',
  23418. ' });',
  23419. ' $mod.THelper.Run();',
  23420. ' $mod.THelper.Fly.call({',
  23421. ' p: this,',
  23422. ' get: function () {',
  23423. ' return this.p.FField;',
  23424. ' },',
  23425. ' set: function (v) {',
  23426. ' this.p.FField = v;',
  23427. ' }',
  23428. ' });',
  23429. ' $mod.THelper.Run();',
  23430. ' var $with = this.FField;',
  23431. ' $mod.THelper.Fly.call({',
  23432. ' get: function () {',
  23433. ' return $with;',
  23434. ' },',
  23435. ' set: function (v) {',
  23436. ' $with = v;',
  23437. ' }',
  23438. ' });',
  23439. ' $mod.THelper.Run();',
  23440. ' };',
  23441. '});',
  23442. 'rtl.createHelper(this, "THelper", null, function () {',
  23443. ' this.Fly = function () {',
  23444. ' };',
  23445. ' this.Run = function () {',
  23446. ' };',
  23447. '});',
  23448. 'this.o = null;',
  23449. '']),
  23450. LinesToStr([ // $mod.$main
  23451. '$mod.THelper.Fly.call({',
  23452. ' p: $mod.o,',
  23453. ' get: function () {',
  23454. ' return this.p.FField;',
  23455. ' },',
  23456. ' set: function (v) {',
  23457. ' this.p.FField = v;',
  23458. ' }',
  23459. '});',
  23460. '$mod.THelper.Run();',
  23461. 'var $with = $mod.o;',
  23462. '$mod.THelper.Fly.call({',
  23463. ' p: $with,',
  23464. ' get: function () {',
  23465. ' return this.p.FField;',
  23466. ' },',
  23467. ' set: function (v) {',
  23468. ' this.p.FField = v;',
  23469. ' }',
  23470. '});',
  23471. '$mod.THelper.Run();',
  23472. 'var $with1 = $mod.o.FField;',
  23473. '$mod.THelper.Fly.call({',
  23474. ' get: function () {',
  23475. ' return $with1;',
  23476. ' },',
  23477. ' set: function (v) {',
  23478. ' $with1 = v;',
  23479. ' }',
  23480. '});',
  23481. '$mod.THelper.Run();',
  23482. '']));
  23483. end;
  23484. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23485. begin
  23486. StartProgram(false);
  23487. Add([
  23488. '{$modeswitch typehelpers}',
  23489. 'type',
  23490. ' TObject = class',
  23491. ' FField: word;',
  23492. ' function GetField: word;',
  23493. ' property Field: word read GetField write FField;',
  23494. ' end;',
  23495. ' THelper = type helper for word',
  23496. ' procedure Fly;',
  23497. ' class procedure Run; static;',
  23498. ' end;',
  23499. 'function TObject.GetField: word;',
  23500. 'begin',
  23501. ' Field.Fly;',
  23502. ' Field.Run;',
  23503. ' Self.Field.Fly;',
  23504. ' Self.Field.Run;',
  23505. ' with Self do begin',
  23506. ' Field.Fly;',
  23507. ' Field.Run;',
  23508. ' end;',
  23509. ' with Self.Field do begin',
  23510. ' Fly;',
  23511. ' Run;',
  23512. ' end;',
  23513. 'end;',
  23514. 'procedure THelper.Fly;',
  23515. 'begin',
  23516. 'end;',
  23517. 'class procedure THelper.Run;',
  23518. 'begin',
  23519. 'end;',
  23520. 'var',
  23521. ' o: TObject;',
  23522. 'begin',
  23523. ' o.Field.Fly;',
  23524. ' o.Field.Run;',
  23525. ' with o do begin',
  23526. ' Field.Fly;',
  23527. ' Field.Run;',
  23528. ' end;',
  23529. ' with o.Field do begin',
  23530. ' Fly;',
  23531. ' Run;',
  23532. ' end;',
  23533. '']);
  23534. ConvertProgram;
  23535. CheckSource('TestTypeHelper_PassPropertyGetter',
  23536. LinesToStr([ // statements
  23537. 'rtl.createClass(this, "TObject", null, function () {',
  23538. ' this.$init = function () {',
  23539. ' this.FField = 0;',
  23540. ' };',
  23541. ' this.$final = function () {',
  23542. ' };',
  23543. ' this.GetField = function () {',
  23544. ' var Result = 0;',
  23545. ' $mod.THelper.Fly.call({',
  23546. ' p: this.GetField(),',
  23547. ' get: function () {',
  23548. ' return this.p;',
  23549. ' },',
  23550. ' set: function (v) {',
  23551. ' this.p = v;',
  23552. ' }',
  23553. ' });',
  23554. ' $mod.THelper.Run();',
  23555. ' $mod.THelper.Fly.call({',
  23556. ' p: this.GetField(),',
  23557. ' get: function () {',
  23558. ' return this.p;',
  23559. ' },',
  23560. ' set: function (v) {',
  23561. ' this.p = v;',
  23562. ' }',
  23563. ' });',
  23564. ' $mod.THelper.Run();',
  23565. ' $mod.THelper.Fly.call({',
  23566. ' p: this.GetField(),',
  23567. ' get: function () {',
  23568. ' return this.p;',
  23569. ' },',
  23570. ' set: function (v) {',
  23571. ' this.p = v;',
  23572. ' }',
  23573. ' });',
  23574. ' $mod.THelper.Run();',
  23575. ' var $with = this.GetField();',
  23576. ' $mod.THelper.Fly.call({',
  23577. ' get: function () {',
  23578. ' return $with;',
  23579. ' },',
  23580. ' set: function (v) {',
  23581. ' $with = v;',
  23582. ' }',
  23583. ' });',
  23584. ' $mod.THelper.Run();',
  23585. ' return Result;',
  23586. ' };',
  23587. '});',
  23588. 'rtl.createHelper(this, "THelper", null, function () {',
  23589. ' this.Fly = function () {',
  23590. ' };',
  23591. ' this.Run = function () {',
  23592. ' };',
  23593. '});',
  23594. 'this.o = null;',
  23595. '']),
  23596. LinesToStr([ // $mod.$main
  23597. '$mod.THelper.Fly.call({',
  23598. ' p: $mod.o.GetField(),',
  23599. ' get: function () {',
  23600. ' return this.p;',
  23601. ' },',
  23602. ' set: function (v) {',
  23603. ' this.p = v;',
  23604. ' }',
  23605. '});',
  23606. '$mod.THelper.Run();',
  23607. 'var $with = $mod.o;',
  23608. '$mod.THelper.Fly.call({',
  23609. ' p: $with.GetField(),',
  23610. ' get: function () {',
  23611. ' return this.p;',
  23612. ' },',
  23613. ' set: function (v) {',
  23614. ' this.p = v;',
  23615. ' }',
  23616. '});',
  23617. '$mod.THelper.Run();',
  23618. 'var $with1 = $mod.o.GetField();',
  23619. '$mod.THelper.Fly.call({',
  23620. ' get: function () {',
  23621. ' return $with1;',
  23622. ' },',
  23623. ' set: function (v) {',
  23624. ' $with1 = v;',
  23625. ' }',
  23626. '});',
  23627. '$mod.THelper.Run();',
  23628. '']));
  23629. end;
  23630. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23631. begin
  23632. StartProgram(false);
  23633. Add([
  23634. '{$modeswitch typehelpers}',
  23635. 'type',
  23636. ' TObject = class',
  23637. ' class var FField: word;',
  23638. ' class procedure SetField(Value: word);',
  23639. ' class property Field: word read FField write SetField;',
  23640. ' end;',
  23641. ' THelper = type helper for word',
  23642. ' procedure Fly(n: byte);',
  23643. ' end;',
  23644. 'class procedure TObject.SetField(Value: word);',
  23645. 'begin',
  23646. ' Field.Fly(1);',
  23647. ' Self.Field.Fly(2);',
  23648. ' with Self do Field.Fly(3);',
  23649. ' with Self.Field do Fly(4);',
  23650. ' TObject.Field.Fly(5);',
  23651. ' with TObject do Field.Fly(6);',
  23652. ' with TObject.Field do Fly(7);',
  23653. 'end;',
  23654. 'procedure THelper.Fly(n: byte);',
  23655. 'begin',
  23656. 'end;',
  23657. 'var',
  23658. ' o: TObject;',
  23659. 'begin',
  23660. ' o.Field.Fly(11);',
  23661. ' with o do Field.Fly(12);',
  23662. ' with o.Field do Fly(13);',
  23663. ' TObject.Field.Fly(14);',
  23664. ' with TObject do Field.Fly(15);',
  23665. ' with TObject.Field do Fly(16);',
  23666. '']);
  23667. ConvertProgram;
  23668. CheckSource('TestTypeHelper_PassClassPropertyField',
  23669. LinesToStr([ // statements
  23670. 'rtl.createClass(this, "TObject", null, function () {',
  23671. ' this.FField = 0;',
  23672. ' this.$init = function () {',
  23673. ' };',
  23674. ' this.$final = function () {',
  23675. ' };',
  23676. ' this.SetField = function (Value) {',
  23677. ' $mod.THelper.Fly.call({',
  23678. ' p: this,',
  23679. ' get: function () {',
  23680. ' return this.p.FField;',
  23681. ' },',
  23682. ' set: function (v) {',
  23683. ' $mod.TObject.FField = v;',
  23684. ' }',
  23685. ' }, 1);',
  23686. ' $mod.THelper.Fly.call({',
  23687. ' p: this,',
  23688. ' get: function () {',
  23689. ' return this.p.FField;',
  23690. ' },',
  23691. ' set: function (v) {',
  23692. ' $mod.TObject.FField = v;',
  23693. ' }',
  23694. ' }, 2);',
  23695. ' $mod.THelper.Fly.call({',
  23696. ' p: this,',
  23697. ' get: function () {',
  23698. ' return this.p.FField;',
  23699. ' },',
  23700. ' set: function (v) {',
  23701. ' $mod.TObject.FField = v;',
  23702. ' }',
  23703. ' }, 3);',
  23704. ' var $with = this.FField;',
  23705. ' $mod.THelper.Fly.call({',
  23706. ' get: function () {',
  23707. ' return $with;',
  23708. ' },',
  23709. ' set: function (v) {',
  23710. ' $with = v;',
  23711. ' }',
  23712. ' }, 4);',
  23713. ' $mod.THelper.Fly.call({',
  23714. ' p: $mod.TObject,',
  23715. ' get: function () {',
  23716. ' return this.p.FField;',
  23717. ' },',
  23718. ' set: function (v) {',
  23719. ' $mod.TObject.FField = v;',
  23720. ' }',
  23721. ' }, 5);',
  23722. ' var $with1 = $mod.TObject;',
  23723. ' $mod.THelper.Fly.call({',
  23724. ' p: $with1,',
  23725. ' get: function () {',
  23726. ' return this.p.FField;',
  23727. ' },',
  23728. ' set: function (v) {',
  23729. ' $mod.TObject.FField = v;',
  23730. ' }',
  23731. ' }, 6);',
  23732. ' var $with2 = $mod.TObject.FField;',
  23733. ' $mod.THelper.Fly.call({',
  23734. ' get: function () {',
  23735. ' return $with2;',
  23736. ' },',
  23737. ' set: function (v) {',
  23738. ' $with2 = v;',
  23739. ' }',
  23740. ' }, 7);',
  23741. ' };',
  23742. '});',
  23743. 'rtl.createHelper(this, "THelper", null, function () {',
  23744. ' this.Fly = function (n) {',
  23745. ' };',
  23746. '});',
  23747. 'this.o = null;',
  23748. '']),
  23749. LinesToStr([ // $mod.$main
  23750. '$mod.THelper.Fly.call({',
  23751. ' p: $mod.o,',
  23752. ' get: function () {',
  23753. ' return this.p.FField;',
  23754. ' },',
  23755. ' set: function (v) {',
  23756. ' $mod.TObject.FField = v;',
  23757. ' }',
  23758. '}, 11);',
  23759. 'var $with = $mod.o;',
  23760. '$mod.THelper.Fly.call({',
  23761. ' p: $with,',
  23762. ' get: function () {',
  23763. ' return this.p.FField;',
  23764. ' },',
  23765. ' set: function (v) {',
  23766. ' $mod.TObject.FField = v;',
  23767. ' }',
  23768. '}, 12);',
  23769. 'var $with1 = $mod.o.FField;',
  23770. '$mod.THelper.Fly.call({',
  23771. ' get: function () {',
  23772. ' return $with1;',
  23773. ' },',
  23774. ' set: function (v) {',
  23775. ' $with1 = v;',
  23776. ' }',
  23777. '}, 13);',
  23778. '$mod.THelper.Fly.call({',
  23779. ' p: $mod.TObject,',
  23780. ' get: function () {',
  23781. ' return this.p.FField;',
  23782. ' },',
  23783. ' set: function (v) {',
  23784. ' $mod.TObject.FField = v;',
  23785. ' }',
  23786. '}, 14);',
  23787. 'var $with2 = $mod.TObject;',
  23788. '$mod.THelper.Fly.call({',
  23789. ' p: $with2,',
  23790. ' get: function () {',
  23791. ' return this.p.FField;',
  23792. ' },',
  23793. ' set: function (v) {',
  23794. ' $mod.TObject.FField = v;',
  23795. ' }',
  23796. '}, 15);',
  23797. 'var $with3 = $mod.TObject.FField;',
  23798. '$mod.THelper.Fly.call({',
  23799. ' get: function () {',
  23800. ' return $with3;',
  23801. ' },',
  23802. ' set: function (v) {',
  23803. ' $with3 = v;',
  23804. ' }',
  23805. '}, 16);',
  23806. '']));
  23807. end;
  23808. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23809. begin
  23810. StartProgram(false);
  23811. Add([
  23812. '{$modeswitch typehelpers}',
  23813. 'type',
  23814. ' TObject = class',
  23815. ' class var FField: word;',
  23816. ' class function GetField: word; static;',
  23817. ' class property Field: word read GetField write FField;',
  23818. ' end;',
  23819. ' THelper = type helper for word',
  23820. ' procedure Fly(n: byte);',
  23821. ' end;',
  23822. 'class function TObject.GetField: word;',
  23823. 'begin',
  23824. ' Field.Fly(1);',
  23825. ' TObject.Field.Fly(5);',
  23826. ' with TObject do Field.Fly(6);',
  23827. ' with TObject.Field do Fly(7);',
  23828. 'end;',
  23829. 'procedure THelper.Fly(n: byte);',
  23830. 'begin',
  23831. 'end;',
  23832. 'var',
  23833. ' o: TObject;',
  23834. 'begin',
  23835. ' o.Field.Fly(11);',
  23836. ' with o do Field.Fly(12);',
  23837. ' with o.Field do Fly(13);',
  23838. '']);
  23839. ConvertProgram;
  23840. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23841. LinesToStr([ // statements
  23842. 'rtl.createClass(this, "TObject", null, function () {',
  23843. ' this.FField = 0;',
  23844. ' this.$init = function () {',
  23845. ' };',
  23846. ' this.$final = function () {',
  23847. ' };',
  23848. ' this.GetField = function () {',
  23849. ' var Result = 0;',
  23850. ' $mod.THelper.Fly.call({',
  23851. ' p: $mod.TObject.GetField(),',
  23852. ' get: function () {',
  23853. ' return this.p;',
  23854. ' },',
  23855. ' set: function (v) {',
  23856. ' this.p = v;',
  23857. ' }',
  23858. ' }, 1);',
  23859. ' $mod.THelper.Fly.call({',
  23860. ' p: $mod.TObject.GetField(),',
  23861. ' get: function () {',
  23862. ' return this.p;',
  23863. ' },',
  23864. ' set: function (v) {',
  23865. ' this.p = v;',
  23866. ' }',
  23867. ' }, 5);',
  23868. ' var $with = $mod.TObject;',
  23869. ' $mod.THelper.Fly.call({',
  23870. ' p: $with.GetField(),',
  23871. ' get: function () {',
  23872. ' return this.p;',
  23873. ' },',
  23874. ' set: function (v) {',
  23875. ' this.p = v;',
  23876. ' }',
  23877. ' }, 6);',
  23878. ' var $with1 = $mod.TObject.GetField();',
  23879. ' $mod.THelper.Fly.call({',
  23880. ' get: function () {',
  23881. ' return $with1;',
  23882. ' },',
  23883. ' set: function (v) {',
  23884. ' $with1 = v;',
  23885. ' }',
  23886. ' }, 7);',
  23887. ' return Result;',
  23888. ' };',
  23889. '});',
  23890. 'rtl.createHelper(this, "THelper", null, function () {',
  23891. ' this.Fly = function (n) {',
  23892. ' };',
  23893. '});',
  23894. 'this.o = null;',
  23895. '']),
  23896. LinesToStr([ // $mod.$main
  23897. '$mod.THelper.Fly.call({',
  23898. ' p: $mod.TObject.GetField(),',
  23899. ' get: function () {',
  23900. ' return this.p;',
  23901. ' },',
  23902. ' set: function (v) {',
  23903. ' this.p = v;',
  23904. ' }',
  23905. '}, 11);',
  23906. 'var $with = $mod.o;',
  23907. '$mod.THelper.Fly.call({',
  23908. ' p: $with.GetField(),',
  23909. ' get: function () {',
  23910. ' return this.p;',
  23911. ' },',
  23912. ' set: function (v) {',
  23913. ' this.p = v;',
  23914. ' }',
  23915. '}, 12);',
  23916. 'var $with1 = $mod.TObject.GetField();',
  23917. '$mod.THelper.Fly.call({',
  23918. ' get: function () {',
  23919. ' return $with1;',
  23920. ' },',
  23921. ' set: function (v) {',
  23922. ' $with1 = v;',
  23923. ' }',
  23924. '}, 13);',
  23925. '']));
  23926. end;
  23927. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23928. begin
  23929. StartProgram(false);
  23930. Add([
  23931. '{$modeswitch typehelpers}',
  23932. 'type',
  23933. ' TObject = class',
  23934. ' class var FField: word;',
  23935. ' class function GetField: word;',
  23936. ' class property Field: word read GetField write FField;',
  23937. ' end;',
  23938. ' TClass = class of TObject;',
  23939. ' THelper = type helper for word',
  23940. ' procedure Fly(n: byte);',
  23941. ' end;',
  23942. 'class function TObject.GetField: word;',
  23943. 'begin',
  23944. ' Field.Fly(1);',
  23945. ' Self.Field.Fly(5);',
  23946. ' with Self do Field.Fly(6);',
  23947. ' with Self.Field do Fly(7);',
  23948. 'end;',
  23949. 'procedure THelper.Fly(n: byte);',
  23950. 'begin',
  23951. 'end;',
  23952. 'var',
  23953. ' o: TObject;',
  23954. ' c: TClass;',
  23955. 'begin',
  23956. ' o.Field.Fly(11);',
  23957. ' with o do Field.Fly(12);',
  23958. ' with o.Field do Fly(13);',
  23959. ' c.Field.Fly(14);',
  23960. ' with c do Field.Fly(15);',
  23961. ' with c.Field do Fly(16);',
  23962. '']);
  23963. ConvertProgram;
  23964. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23965. LinesToStr([ // statements
  23966. 'rtl.createClass(this, "TObject", null, function () {',
  23967. ' this.FField = 0;',
  23968. ' this.$init = function () {',
  23969. ' };',
  23970. ' this.$final = function () {',
  23971. ' };',
  23972. ' this.GetField = function () {',
  23973. ' var Result = 0;',
  23974. ' $mod.THelper.Fly.call({',
  23975. ' p: this.GetField(),',
  23976. ' get: function () {',
  23977. ' return this.p;',
  23978. ' },',
  23979. ' set: function (v) {',
  23980. ' this.p = v;',
  23981. ' }',
  23982. ' }, 1);',
  23983. ' $mod.THelper.Fly.call({',
  23984. ' p: this.GetField(),',
  23985. ' get: function () {',
  23986. ' return this.p;',
  23987. ' },',
  23988. ' set: function (v) {',
  23989. ' this.p = v;',
  23990. ' }',
  23991. ' }, 5);',
  23992. ' $mod.THelper.Fly.call({',
  23993. ' p: this.GetField(),',
  23994. ' get: function () {',
  23995. ' return this.p;',
  23996. ' },',
  23997. ' set: function (v) {',
  23998. ' this.p = v;',
  23999. ' }',
  24000. ' }, 6);',
  24001. ' var $with = this.GetField();',
  24002. ' $mod.THelper.Fly.call({',
  24003. ' get: function () {',
  24004. ' return $with;',
  24005. ' },',
  24006. ' set: function (v) {',
  24007. ' $with = v;',
  24008. ' }',
  24009. ' }, 7);',
  24010. ' return Result;',
  24011. ' };',
  24012. '});',
  24013. 'rtl.createHelper(this, "THelper", null, function () {',
  24014. ' this.Fly = function (n) {',
  24015. ' };',
  24016. '});',
  24017. 'this.o = null;',
  24018. 'this.c = null;',
  24019. '']),
  24020. LinesToStr([ // $mod.$main
  24021. '$mod.THelper.Fly.call({',
  24022. ' p: $mod.o.$class.GetField(),',
  24023. ' get: function () {',
  24024. ' return this.p;',
  24025. ' },',
  24026. ' set: function (v) {',
  24027. ' this.p = v;',
  24028. ' }',
  24029. '}, 11);',
  24030. 'var $with = $mod.o;',
  24031. '$mod.THelper.Fly.call({',
  24032. ' p: $with.$class.GetField(),',
  24033. ' get: function () {',
  24034. ' return this.p;',
  24035. ' },',
  24036. ' set: function (v) {',
  24037. ' this.p = v;',
  24038. ' }',
  24039. '}, 12);',
  24040. 'var $with1 = $mod.o.$class.GetField();',
  24041. '$mod.THelper.Fly.call({',
  24042. ' get: function () {',
  24043. ' return $with1;',
  24044. ' },',
  24045. ' set: function (v) {',
  24046. ' $with1 = v;',
  24047. ' }',
  24048. '}, 13);',
  24049. '$mod.THelper.Fly.call({',
  24050. ' p: $mod.c.GetField(),',
  24051. ' get: function () {',
  24052. ' return this.p;',
  24053. ' },',
  24054. ' set: function (v) {',
  24055. ' this.p = v;',
  24056. ' }',
  24057. '}, 14);',
  24058. 'var $with2 = $mod.c;',
  24059. '$mod.THelper.Fly.call({',
  24060. ' p: $with2.GetField(),',
  24061. ' get: function () {',
  24062. ' return this.p;',
  24063. ' },',
  24064. ' set: function (v) {',
  24065. ' this.p = v;',
  24066. ' }',
  24067. '}, 15);',
  24068. 'var $with3 = $mod.c.GetField();',
  24069. '$mod.THelper.Fly.call({',
  24070. ' get: function () {',
  24071. ' return $with3;',
  24072. ' },',
  24073. ' set: function (v) {',
  24074. ' $with3 = v;',
  24075. ' }',
  24076. '}, 16);',
  24077. '']));
  24078. end;
  24079. procedure TTestModule.TestTypeHelper_Property;
  24080. begin
  24081. StartProgram(false);
  24082. Add([
  24083. '{$modeswitch typehelpers}',
  24084. 'type',
  24085. ' THelper = type helper for word',
  24086. ' function GetSize: longint;',
  24087. ' procedure SetSize(Value: longint);',
  24088. ' property Size: longint read GetSize write SetSize;',
  24089. ' end;',
  24090. 'function THelper.GetSize: longint;',
  24091. 'begin',
  24092. ' Result:=Size+1;',
  24093. ' Size:=2;',
  24094. ' Result:=Self.Size+3;',
  24095. ' Self.Size:=4;',
  24096. ' with Self do begin',
  24097. ' Result:=Size+5;',
  24098. ' Size:=6;',
  24099. ' end;',
  24100. 'end;',
  24101. 'procedure THelper.SetSize(Value: longint);',
  24102. 'begin',
  24103. 'end;',
  24104. 'var w: word;',
  24105. 'begin',
  24106. ' w:=w.Size+7;',
  24107. ' w.Size:=w+8;',
  24108. ' with w do begin',
  24109. ' w:=Size+9;',
  24110. ' Size:=w+10;',
  24111. ' end;',
  24112. '']);
  24113. ConvertProgram;
  24114. CheckSource('TestTypeHelper_Property',
  24115. LinesToStr([ // statements
  24116. 'rtl.createHelper(this, "THelper", null, function () {',
  24117. ' this.GetSize = function () {',
  24118. ' var Result = 0;',
  24119. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24120. ' $mod.THelper.SetSize.call(this, 2);',
  24121. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24122. ' $mod.THelper.SetSize.call(this, 4);',
  24123. ' var $with = this.get();',
  24124. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24125. ' $mod.THelper.SetSize.call(this, 6);',
  24126. ' return Result;',
  24127. ' };',
  24128. ' this.SetSize = function (Value) {',
  24129. ' };',
  24130. '});',
  24131. 'this.w = 0;',
  24132. '']),
  24133. LinesToStr([ // $mod.$main
  24134. '$mod.w = $mod.THelper.GetSize.call({',
  24135. ' p: $mod,',
  24136. ' get: function () {',
  24137. ' return this.p.w;',
  24138. ' },',
  24139. ' set: function (v) {',
  24140. ' this.p.w = v;',
  24141. ' }',
  24142. '}) + 7;',
  24143. '$mod.THelper.SetSize.call({',
  24144. ' p: $mod,',
  24145. ' get: function () {',
  24146. ' return this.p.w;',
  24147. ' },',
  24148. ' set: function (v) {',
  24149. ' this.p.w = v;',
  24150. ' }',
  24151. '}, $mod.w + 8);',
  24152. 'var $with = $mod.w;',
  24153. '$mod.w = $mod.THelper.GetSize.call({',
  24154. ' get: function () {',
  24155. ' return $with;',
  24156. ' },',
  24157. ' set: function (v) {',
  24158. ' $with = v;',
  24159. ' }',
  24160. '}) + 9;',
  24161. '$mod.THelper.SetSize.call({',
  24162. ' get: function () {',
  24163. ' return $with;',
  24164. ' },',
  24165. ' set: function (v) {',
  24166. ' $with = v;',
  24167. ' }',
  24168. '}, $mod.w + 10);',
  24169. '']));
  24170. end;
  24171. procedure TTestModule.TestTypeHelper_Property_Array;
  24172. begin
  24173. StartProgram(false);
  24174. Add([
  24175. '{$modeswitch typehelpers}',
  24176. 'type',
  24177. ' THelper = type helper for word',
  24178. ' function GetItems(Index: byte): boolean;',
  24179. ' procedure SetItems(Index: byte; Value: boolean);',
  24180. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24181. ' end;',
  24182. 'function THelper.GetItems(Index: byte): boolean;',
  24183. 'begin',
  24184. ' Result:=Items[1];',
  24185. ' Items[2]:=false;',
  24186. ' Result:=Self.Items[3];',
  24187. ' Self.Items[4]:=true;',
  24188. ' with Self do begin',
  24189. ' Result:=Items[5];',
  24190. ' Items[6]:=false;',
  24191. ' end;',
  24192. 'end;',
  24193. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24194. 'begin',
  24195. 'end;',
  24196. 'var',
  24197. ' w: word;',
  24198. ' b: boolean;',
  24199. 'begin',
  24200. ' b:=w.Items[1];',
  24201. ' w.Items[2]:=b;',
  24202. ' with w do begin',
  24203. ' b:=Items[3];',
  24204. ' Items[4]:=b;',
  24205. ' end;',
  24206. '']);
  24207. ConvertProgram;
  24208. CheckSource('TestTypeHelper_Property_Array',
  24209. LinesToStr([ // statements
  24210. 'rtl.createHelper(this, "THelper", null, function () {',
  24211. ' this.GetItems = function (Index) {',
  24212. ' var Result = false;',
  24213. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24214. ' $mod.THelper.SetItems.call(this, 2, false);',
  24215. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24216. ' $mod.THelper.SetItems.call(this, 4, true);',
  24217. ' var $with = this.get();',
  24218. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24219. ' $mod.THelper.SetItems.call(this, 6, false);',
  24220. ' return Result;',
  24221. ' };',
  24222. ' this.SetItems = function (Index, Value) {',
  24223. ' };',
  24224. '});',
  24225. 'this.w = 0;',
  24226. 'this.b = false;',
  24227. '']),
  24228. LinesToStr([ // $mod.$main
  24229. '$mod.b = $mod.THelper.GetItems.call({',
  24230. ' p: $mod,',
  24231. ' get: function () {',
  24232. ' return this.p.w;',
  24233. ' },',
  24234. ' set: function (v) {',
  24235. ' this.p.w = v;',
  24236. ' }',
  24237. '}, 1);',
  24238. '$mod.THelper.SetItems.call({',
  24239. ' p: $mod,',
  24240. ' get: function () {',
  24241. ' return this.p.w;',
  24242. ' },',
  24243. ' set: function (v) {',
  24244. ' this.p.w = v;',
  24245. ' }',
  24246. '}, 2, $mod.b);',
  24247. 'var $with = $mod.w;',
  24248. '$mod.b = $mod.THelper.GetItems.call({',
  24249. ' get: function () {',
  24250. ' return $with;',
  24251. ' },',
  24252. ' set: function (v) {',
  24253. ' $with = v;',
  24254. ' }',
  24255. '}, 3);',
  24256. '$mod.THelper.SetItems.call({',
  24257. ' get: function () {',
  24258. ' return $with;',
  24259. ' },',
  24260. ' set: function (v) {',
  24261. ' $with = v;',
  24262. ' }',
  24263. '}, 4, $mod.b);',
  24264. '']));
  24265. end;
  24266. procedure TTestModule.TestTypeHelper_ClassProperty;
  24267. begin
  24268. StartProgram(false);
  24269. Add([
  24270. '{$modeswitch typehelpers}',
  24271. 'type',
  24272. ' THelper = type helper for word',
  24273. ' class function GetSize: longint; static;',
  24274. ' class procedure SetSize(Value: longint); static;',
  24275. ' class property Size: longint read GetSize write SetSize;',
  24276. ' end;',
  24277. 'class function THelper.GetSize: longint;',
  24278. 'begin',
  24279. ' Result:=Size+1;',
  24280. ' Size:=2;',
  24281. 'end;',
  24282. 'class procedure THelper.SetSize(Value: longint);',
  24283. 'begin',
  24284. 'end;',
  24285. 'begin',
  24286. '']);
  24287. ConvertProgram;
  24288. CheckSource('TestTypeHelper_ClassProperty',
  24289. LinesToStr([ // statements
  24290. 'rtl.createHelper(this, "THelper", null, function () {',
  24291. ' this.GetSize = function () {',
  24292. ' var Result = 0;',
  24293. ' Result = $mod.THelper.GetSize() + 1;',
  24294. ' $mod.THelper.SetSize(2);',
  24295. ' return Result;',
  24296. ' };',
  24297. ' this.SetSize = function (Value) {',
  24298. ' };',
  24299. '});',
  24300. '']),
  24301. LinesToStr([ // $mod.$main
  24302. '']));
  24303. end;
  24304. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24305. begin
  24306. StartProgram(false);
  24307. Add([
  24308. '{$modeswitch typehelpers}',
  24309. 'type',
  24310. ' THelper = type helper for word',
  24311. ' class function GetItems(Index: byte): boolean; static;',
  24312. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24313. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24314. ' end;',
  24315. 'class function THelper.GetItems(Index: byte): boolean;',
  24316. 'begin',
  24317. ' Result:=Items[1];',
  24318. ' Items[2]:=false;',
  24319. 'end;',
  24320. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24321. 'begin',
  24322. 'end;',
  24323. 'var',
  24324. ' w: word;',
  24325. ' b: boolean;',
  24326. 'begin',
  24327. ' b:=w.Items[1];',
  24328. ' w.Items[2]:=b;',
  24329. ' with w do begin',
  24330. ' b:=Items[3];',
  24331. ' Items[4]:=b;',
  24332. ' end;',
  24333. '']);
  24334. ConvertProgram;
  24335. CheckSource('TestTypeHelper_ClassProperty_Array',
  24336. LinesToStr([ // statements
  24337. 'rtl.createHelper(this, "THelper", null, function () {',
  24338. ' this.GetItems = function (Index) {',
  24339. ' var Result = false;',
  24340. ' Result = $mod.THelper.GetItems(1);',
  24341. ' $mod.THelper.SetItems(2, false);',
  24342. ' return Result;',
  24343. ' };',
  24344. ' this.SetItems = function (Index, Value) {',
  24345. ' };',
  24346. '});',
  24347. 'this.w = 0;',
  24348. 'this.b = false;',
  24349. '']),
  24350. LinesToStr([ // $mod.$main
  24351. '$mod.b = $mod.THelper.GetItems(1);',
  24352. '$mod.THelper.SetItems(2, $mod.b);',
  24353. 'var $with = $mod.w;',
  24354. '$mod.b = $mod.THelper.GetItems(3);',
  24355. '$mod.THelper.SetItems(4, $mod.b);',
  24356. '']));
  24357. end;
  24358. procedure TTestModule.TestTypeHelper_ClassMethod;
  24359. begin
  24360. StartProgram(false);
  24361. Add([
  24362. '{$modeswitch typehelpers}',
  24363. 'type',
  24364. ' THelper = type helper for word',
  24365. ' class procedure DoStatic; static;',
  24366. ' end;',
  24367. 'class procedure THelper.DoStatic;',
  24368. 'begin',
  24369. ' DoStatic;',
  24370. ' DoStatic();',
  24371. 'end;',
  24372. 'var w: word;',
  24373. 'begin',
  24374. ' w.DoStatic;',
  24375. ' w.DoStatic();',
  24376. '']);
  24377. ConvertProgram;
  24378. CheckSource('TestTypeHelper_ClassMethod',
  24379. LinesToStr([ // statements
  24380. 'rtl.createHelper(this, "THelper", null, function () {',
  24381. ' this.DoStatic = function () {',
  24382. ' $mod.THelper.DoStatic();',
  24383. ' $mod.THelper.DoStatic();',
  24384. ' };',
  24385. '});',
  24386. 'this.w = 0;',
  24387. '']),
  24388. LinesToStr([ // $mod.$main
  24389. '$mod.THelper.DoStatic();',
  24390. '$mod.THelper.DoStatic();',
  24391. '']));
  24392. end;
  24393. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24394. begin
  24395. StartProgram(false);
  24396. Add([
  24397. '{$modeswitch typehelpers}',
  24398. 'type',
  24399. ' THelper = type helper for word',
  24400. ' procedure Run; external name ''Run'';',
  24401. ' end;',
  24402. 'var w: word;',
  24403. 'begin',
  24404. ' w.Run;',
  24405. '']);
  24406. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24407. ConvertProgram;
  24408. end;
  24409. procedure TTestModule.TestTypeHelper_Constructor;
  24410. begin
  24411. StartProgram(false);
  24412. Add([
  24413. '{$modeswitch typehelpers}',
  24414. 'type',
  24415. ' THelper = type helper for word',
  24416. ' constructor Init(e: longint);',
  24417. ' end;',
  24418. 'constructor THelper.Init(e: longint);',
  24419. 'begin',
  24420. ' Self:=e;',
  24421. ' Init(e+1);',
  24422. 'end;',
  24423. 'var w: word;',
  24424. 'begin',
  24425. ' w:=word.Init(2);',
  24426. ' w:=w.Init(3);',
  24427. ' with word do w:=Init(4);',
  24428. ' with w do w:=Init(5);',
  24429. '']);
  24430. ConvertProgram;
  24431. CheckSource('TestTypeHelper_Constructor',
  24432. LinesToStr([ // statements
  24433. 'rtl.createHelper(this, "THelper", null, function () {',
  24434. ' this.Init = function (e) {',
  24435. ' this.set(e);',
  24436. ' $mod.THelper.Init.call(this, e + 1);',
  24437. ' return this.get();',
  24438. ' };',
  24439. ' this.$new = function (fn, args) {',
  24440. ' return this[fn].apply({',
  24441. ' p: 0,',
  24442. ' get: function () {',
  24443. ' return this.p;',
  24444. ' },',
  24445. ' set: function (v) {',
  24446. ' this.p = v;',
  24447. ' }',
  24448. ' }, args);',
  24449. ' };',
  24450. '});',
  24451. 'this.w = 0;',
  24452. '']),
  24453. LinesToStr([ // $mod.$main
  24454. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24455. '$mod.w = $mod.THelper.Init.call({',
  24456. ' p: $mod,',
  24457. ' get: function () {',
  24458. ' return this.p.w;',
  24459. ' },',
  24460. ' set: function (v) {',
  24461. ' this.p.w = v;',
  24462. ' }',
  24463. '}, 3);',
  24464. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24465. 'var $with = $mod.w;',
  24466. '$mod.w = $mod.THelper.Init.call({',
  24467. ' get: function () {',
  24468. ' return $with;',
  24469. ' },',
  24470. ' set: function (v) {',
  24471. ' $with = v;',
  24472. ' }',
  24473. '}, 5);',
  24474. '']));
  24475. end;
  24476. procedure TTestModule.TestTypeHelper_Word;
  24477. begin
  24478. StartProgram(false);
  24479. Add([
  24480. '{$modeswitch typehelpers}',
  24481. 'type',
  24482. ' THelper = type helper for word',
  24483. ' procedure DoIt(e: byte = 123);',
  24484. ' end;',
  24485. 'procedure THelper.DoIt(e: byte);',
  24486. 'begin',
  24487. ' Self:=e;',
  24488. ' Self:=Self+1;',
  24489. ' with Self do Doit;',
  24490. 'end;',
  24491. 'begin',
  24492. ' word(3).DoIt;',
  24493. '']);
  24494. ConvertProgram;
  24495. CheckSource('TestTypeHelper_Word',
  24496. LinesToStr([ // statements
  24497. 'rtl.createHelper(this, "THelper", null, function () {',
  24498. ' this.DoIt = function (e) {',
  24499. ' this.set(e);',
  24500. ' this.set(this.get() + 1);',
  24501. ' var $with = this.get();',
  24502. ' $mod.THelper.DoIt.call(this, 123);',
  24503. ' };',
  24504. '});',
  24505. '']),
  24506. LinesToStr([ // $mod.$main
  24507. '$mod.THelper.DoIt.call({',
  24508. ' get: function () {',
  24509. ' return 3;',
  24510. ' },',
  24511. ' set: function (v) {',
  24512. ' rtl.raiseE("EPropReadOnly");',
  24513. ' }',
  24514. '}, 123);',
  24515. '']));
  24516. end;
  24517. procedure TTestModule.TestTypeHelper_Boolean;
  24518. begin
  24519. StartProgram(false);
  24520. Add([
  24521. '{$modeswitch typehelpers}',
  24522. 'type',
  24523. ' Integer = longint;',
  24524. ' THelper = type helper for boolean',
  24525. ' procedure Run(e: wordbool = true);',
  24526. ' end;',
  24527. 'procedure THelper.Run(e: wordbool);',
  24528. 'begin',
  24529. ' Self:=e;',
  24530. ' Self:=not Self;',
  24531. ' with Self do Run;',
  24532. ' if Integer(Self)=0 then ;',
  24533. 'end;',
  24534. 'begin',
  24535. ' boolean(3).Run;',
  24536. '']);
  24537. ConvertProgram;
  24538. CheckSource('TestTypeHelper_Boolean',
  24539. LinesToStr([ // statements
  24540. 'rtl.createHelper(this, "THelper", null, function () {',
  24541. ' this.Run = function (e) {',
  24542. ' this.set(e);',
  24543. ' this.set(!this.get());',
  24544. ' var $with = this.get();',
  24545. ' $mod.THelper.Run.call(this, true);',
  24546. ' if ((this.get() ? 1 : 0) === 0) ;',
  24547. ' };',
  24548. '});',
  24549. '']),
  24550. LinesToStr([ // $mod.$main
  24551. '$mod.THelper.Run.call({',
  24552. ' a: 3 != 0,',
  24553. ' get: function () {',
  24554. ' return this.a;',
  24555. ' },',
  24556. ' set: function (v) {',
  24557. ' rtl.raiseE("EPropReadOnly");',
  24558. ' }',
  24559. '}, true);',
  24560. '']));
  24561. end;
  24562. procedure TTestModule.TestTypeHelper_WordBool;
  24563. begin
  24564. StartProgram(false);
  24565. Add([
  24566. '{$modeswitch typehelpers}',
  24567. 'type',
  24568. ' Integer = longint;',
  24569. ' THelper = type helper for WordBool',
  24570. ' procedure Run(e: wordbool = true);',
  24571. ' end;',
  24572. 'procedure THelper.Run(e: wordbool);',
  24573. 'var i: integer;',
  24574. 'begin',
  24575. ' i:=Integer(Self);',
  24576. 'end;',
  24577. 'var w: wordbool;',
  24578. 'begin',
  24579. ' w.Run;',
  24580. ' wordbool(3).Run;',
  24581. '']);
  24582. ConvertProgram;
  24583. CheckSource('TestTypeHelper_WordBool',
  24584. LinesToStr([ // statements
  24585. 'rtl.createHelper(this, "THelper", null, function () {',
  24586. ' this.Run = function (e) {',
  24587. ' var i = 0;',
  24588. ' i = (this.get() ? 1 : 0);',
  24589. ' };',
  24590. '});',
  24591. 'this.w = false;',
  24592. '']),
  24593. LinesToStr([ // $mod.$main
  24594. '$mod.THelper.Run.call({',
  24595. ' p: $mod,',
  24596. ' get: function () {',
  24597. ' return this.p.w;',
  24598. ' },',
  24599. ' set: function (v) {',
  24600. ' this.p.w = v;',
  24601. ' }',
  24602. '}, true);',
  24603. '$mod.THelper.Run.call({',
  24604. ' a: 3 != 0,',
  24605. ' get: function () {',
  24606. ' return this.a;',
  24607. ' },',
  24608. ' set: function (v) {',
  24609. ' rtl.raiseE("EPropReadOnly");',
  24610. ' }',
  24611. '}, true);',
  24612. '']));
  24613. end;
  24614. procedure TTestModule.TestTypeHelper_Double;
  24615. begin
  24616. StartProgram(false);
  24617. Add([
  24618. '{$modeswitch typehelpers}',
  24619. 'type',
  24620. ' Float = type double;',
  24621. ' THelper = type helper for Float',
  24622. ' const NPI = 3.141592;',
  24623. ' function ToStr: String;',
  24624. ' end;',
  24625. 'function THelper.ToStr: String;',
  24626. 'begin',
  24627. 'end;',
  24628. 'procedure DoIt(s: string);',
  24629. 'begin',
  24630. 'end;',
  24631. 'var f: Float;',
  24632. 'begin',
  24633. ' DoIt(f.toStr);',
  24634. ' DoIt(f.toStr());',
  24635. ' (f*f).toStr;',
  24636. ' DoIt((f*f).toStr);',
  24637. '']);
  24638. ConvertProgram;
  24639. CheckSource('TestTypeHelper_Double',
  24640. LinesToStr([ // statements
  24641. 'rtl.createHelper(this, "THelper", null, function () {',
  24642. ' this.NPI = 3.141592;',
  24643. ' this.ToStr = function () {',
  24644. ' var Result = "";',
  24645. ' return Result;',
  24646. ' };',
  24647. '});',
  24648. 'this.DoIt = function (s) {',
  24649. '};',
  24650. 'this.f = 0.0;',
  24651. '']),
  24652. LinesToStr([ // $mod.$main
  24653. '$mod.DoIt($mod.THelper.ToStr.call({',
  24654. ' p: $mod,',
  24655. ' get: function () {',
  24656. ' return this.p.f;',
  24657. ' },',
  24658. ' set: function (v) {',
  24659. ' this.p.f = v;',
  24660. ' }',
  24661. '}));',
  24662. '$mod.DoIt($mod.THelper.ToStr.call({',
  24663. ' p: $mod,',
  24664. ' get: function () {',
  24665. ' return this.p.f;',
  24666. ' },',
  24667. ' set: function (v) {',
  24668. ' this.p.f = v;',
  24669. ' }',
  24670. '}));',
  24671. '$mod.THelper.ToStr.call({',
  24672. ' a: $mod.f * $mod.f,',
  24673. ' get: function () {',
  24674. ' return this.a;',
  24675. ' },',
  24676. ' set: function (v) {',
  24677. ' rtl.raiseE("EPropReadOnly");',
  24678. ' }',
  24679. '});',
  24680. '$mod.DoIt($mod.THelper.ToStr.call({',
  24681. ' a: $mod.f * $mod.f,',
  24682. ' get: function () {',
  24683. ' return this.a;',
  24684. ' },',
  24685. ' set: function (v) {',
  24686. ' rtl.raiseE("EPropReadOnly");',
  24687. ' }',
  24688. '}));',
  24689. '']));
  24690. end;
  24691. procedure TTestModule.TestTypeHelper_NativeInt;
  24692. begin
  24693. StartProgram(false);
  24694. Add([
  24695. '{$modeswitch typehelpers}',
  24696. 'type',
  24697. ' MaxInt = type nativeint;',
  24698. ' THelperI = type helper for MaxInt',
  24699. ' function ToStr: String;',
  24700. ' end;',
  24701. ' MaxUInt = type nativeuint;',
  24702. ' THelperU = type helper for MaxUInt',
  24703. ' function ToStr: String;',
  24704. ' end;',
  24705. 'function THelperI.ToStr: String;',
  24706. 'begin',
  24707. ' Result:=str(Self);',
  24708. 'end;',
  24709. 'function THelperU.ToStr: String;',
  24710. 'begin',
  24711. ' Result:=str(Self);',
  24712. 'end;',
  24713. 'procedure DoIt(s: string);',
  24714. 'begin',
  24715. 'end;',
  24716. 'var i: MaxInt;',
  24717. 'begin',
  24718. ' DoIt(i.toStr);',
  24719. ' DoIt(i.toStr());',
  24720. ' (i*i).toStr;',
  24721. ' DoIt((i*i).toStr);',
  24722. '']);
  24723. ConvertProgram;
  24724. CheckSource('TestTypeHelper_NativeInt',
  24725. LinesToStr([ // statements
  24726. 'rtl.createHelper(this, "THelperI", null, function () {',
  24727. ' this.ToStr = function () {',
  24728. ' var Result = "";',
  24729. ' Result = "" + this.get();',
  24730. ' return Result;',
  24731. ' };',
  24732. '});',
  24733. 'rtl.createHelper(this, "THelperU", null, function () {',
  24734. ' this.ToStr = function () {',
  24735. ' var Result = "";',
  24736. ' Result = "" + this.get();',
  24737. ' return Result;',
  24738. ' };',
  24739. '});',
  24740. 'this.DoIt = function (s) {',
  24741. '};',
  24742. 'this.i = 0;',
  24743. '']),
  24744. LinesToStr([ // $mod.$main
  24745. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24746. ' p: $mod,',
  24747. ' get: function () {',
  24748. ' return this.p.i;',
  24749. ' },',
  24750. ' set: function (v) {',
  24751. ' this.p.i = v;',
  24752. ' }',
  24753. '}));',
  24754. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24755. ' p: $mod,',
  24756. ' get: function () {',
  24757. ' return this.p.i;',
  24758. ' },',
  24759. ' set: function (v) {',
  24760. ' this.p.i = v;',
  24761. ' }',
  24762. '}));',
  24763. '$mod.THelperI.ToStr.call({',
  24764. ' a: $mod.i * $mod.i,',
  24765. ' get: function () {',
  24766. ' return this.a;',
  24767. ' },',
  24768. ' set: function (v) {',
  24769. ' rtl.raiseE("EPropReadOnly");',
  24770. ' }',
  24771. '});',
  24772. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24773. ' a: $mod.i * $mod.i,',
  24774. ' get: function () {',
  24775. ' return this.a;',
  24776. ' },',
  24777. ' set: function (v) {',
  24778. ' rtl.raiseE("EPropReadOnly");',
  24779. ' }',
  24780. '}));',
  24781. '']));
  24782. end;
  24783. procedure TTestModule.TestTypeHelper_StringChar;
  24784. begin
  24785. StartProgram(false);
  24786. Add([
  24787. '{$modeswitch typehelpers}',
  24788. 'type',
  24789. ' TStringHelper = type helper for string',
  24790. ' procedure DoIt(e: byte = 123);',
  24791. ' end;',
  24792. ' TCharHelper = type helper for char',
  24793. ' procedure Fly;',
  24794. ' end;',
  24795. 'procedure TStringHelper.DoIt(e: byte);',
  24796. 'begin',
  24797. ' Self[1]:=''c'';',
  24798. ' Self[2]:=Self[3];',
  24799. 'end;',
  24800. 'procedure TCharHelper.Fly;',
  24801. 'begin',
  24802. ' Self:=''c'';',
  24803. 'end;',
  24804. 'begin',
  24805. ' ''abc''.DoIt;',
  24806. ' ''xyz''.DoIt();',
  24807. ' ''c''.Fly();',
  24808. '']);
  24809. ConvertProgram;
  24810. CheckSource('TestTypeHelper_StringChar',
  24811. LinesToStr([ // statements
  24812. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24813. ' this.DoIt = function (e) {',
  24814. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24815. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24816. ' };',
  24817. '});',
  24818. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24819. ' this.Fly = function () {',
  24820. ' this.set("c");',
  24821. ' };',
  24822. '});',
  24823. '']),
  24824. LinesToStr([ // $mod.$main
  24825. '$mod.TStringHelper.DoIt.call({',
  24826. ' get: function () {',
  24827. ' return "abc";',
  24828. ' },',
  24829. ' set: function (v) {',
  24830. ' rtl.raiseE("EPropReadOnly");',
  24831. ' }',
  24832. '}, 123);',
  24833. '$mod.TStringHelper.DoIt.call({',
  24834. ' get: function () {',
  24835. ' return "xyz";',
  24836. ' },',
  24837. ' set: function (v) {',
  24838. ' rtl.raiseE("EPropReadOnly");',
  24839. ' }',
  24840. '}, 123);',
  24841. '$mod.TCharHelper.Fly.call({',
  24842. ' get: function () {',
  24843. ' return "c";',
  24844. ' },',
  24845. ' set: function (v) {',
  24846. ' rtl.raiseE("EPropReadOnly");',
  24847. ' }',
  24848. '});',
  24849. '']));
  24850. end;
  24851. procedure TTestModule.TestTypeHelper_JSValue;
  24852. begin
  24853. StartProgram(false);
  24854. Add([
  24855. '{$modeswitch typehelpers}',
  24856. 'type',
  24857. ' TExtValue = type jsvalue;',
  24858. ' THelper = type helper for TExtValue',
  24859. ' function ToStr: String;',
  24860. ' end;',
  24861. 'function THelper.ToStr: String;',
  24862. 'begin',
  24863. 'end;',
  24864. 'var',
  24865. ' s: string;',
  24866. ' v: TExtValue;',
  24867. 'begin',
  24868. ' s:=v.toStr;',
  24869. ' s:=v.toStr();',
  24870. ' TExtValue(s).toStr;',
  24871. '']);
  24872. ConvertProgram;
  24873. CheckSource('TestTypeHelper_JSValue',
  24874. LinesToStr([ // statements
  24875. 'rtl.createHelper(this, "THelper", null, function () {',
  24876. ' this.ToStr = function () {',
  24877. ' var Result = "";',
  24878. ' return Result;',
  24879. ' };',
  24880. '});',
  24881. 'this.s = "";',
  24882. 'this.v = undefined;',
  24883. '']),
  24884. LinesToStr([ // $mod.$main
  24885. '$mod.s = $mod.THelper.ToStr.call({',
  24886. ' p: $mod,',
  24887. ' get: function () {',
  24888. ' return this.p.v;',
  24889. ' },',
  24890. ' set: function (v) {',
  24891. ' this.p.v = v;',
  24892. ' }',
  24893. '});',
  24894. '$mod.s = $mod.THelper.ToStr.call({',
  24895. ' p: $mod,',
  24896. ' get: function () {',
  24897. ' return this.p.v;',
  24898. ' },',
  24899. ' set: function (v) {',
  24900. ' this.p.v = v;',
  24901. ' }',
  24902. '});',
  24903. '$mod.THelper.ToStr.call({',
  24904. ' p: $mod,',
  24905. ' get: function () {',
  24906. ' return this.p.s;',
  24907. ' },',
  24908. ' set: function (v) {',
  24909. ' rtl.raiseE("EPropReadOnly");',
  24910. ' }',
  24911. '});',
  24912. '']));
  24913. end;
  24914. procedure TTestModule.TestTypeHelper_Array;
  24915. begin
  24916. StartProgram(false);
  24917. Add([
  24918. '{$modeswitch typehelpers}',
  24919. 'type',
  24920. ' TArrOfBool = array of boolean;',
  24921. ' TArrOfJS = array of jsvalue;',
  24922. ' THelper = type helper for TArrOfBool',
  24923. ' procedure DoIt(e: byte = 123);',
  24924. ' end;',
  24925. 'procedure THelper.DoIt(e: byte);',
  24926. 'begin',
  24927. ' Self[1]:=true;',
  24928. ' Self[2]:=not Self[3];',
  24929. ' SetLength(Self,4);',
  24930. 'end;',
  24931. 'var',
  24932. ' b: TArrOfBool;',
  24933. ' j: TArrOfJS;',
  24934. 'begin',
  24935. ' b.DoIt;',
  24936. ' TArrOfBool(j).DoIt();',
  24937. '']);
  24938. ConvertProgram;
  24939. CheckSource('TestTypeHelper_Array',
  24940. LinesToStr([ // statements
  24941. 'rtl.createHelper(this, "THelper", null, function () {',
  24942. ' this.DoIt = function (e) {',
  24943. ' this.get()[1] = true;',
  24944. ' this.get()[2] = !this.get()[3];',
  24945. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24946. ' };',
  24947. '});',
  24948. 'this.b = [];',
  24949. 'this.j = [];',
  24950. '']),
  24951. LinesToStr([ // $mod.$main
  24952. '$mod.THelper.DoIt.call({',
  24953. ' p: $mod,',
  24954. ' get: function () {',
  24955. ' return this.p.b;',
  24956. ' },',
  24957. ' set: function (v) {',
  24958. ' this.p.b = v;',
  24959. ' }',
  24960. '}, 123);',
  24961. '$mod.THelper.DoIt.call({',
  24962. ' p: $mod,',
  24963. ' get: function () {',
  24964. ' return this.p.j;',
  24965. ' },',
  24966. ' set: function (v) {',
  24967. ' this.p.j = v;',
  24968. ' }',
  24969. '}, 123);',
  24970. '']));
  24971. end;
  24972. procedure TTestModule.TestTypeHelper_EnumType;
  24973. begin
  24974. StartProgram(false);
  24975. Add([
  24976. '{$modeswitch typehelpers}',
  24977. 'type',
  24978. ' TEnum = (red,blue);',
  24979. ' THelper = type helper for TEnum',
  24980. ' procedure DoIt(e: byte = 123);',
  24981. ' class procedure Swing(w: word); static;',
  24982. ' end;',
  24983. 'procedure THelper.DoIt(e: byte);',
  24984. 'begin',
  24985. ' Self:=red;',
  24986. ' Self:=succ(Self);',
  24987. ' with Self do Doit;',
  24988. 'end;',
  24989. 'class procedure THelper.Swing(w: word);',
  24990. 'begin',
  24991. 'end;',
  24992. 'var e: TEnum;',
  24993. 'begin',
  24994. ' e.DoIt;',
  24995. ' red.DoIt;',
  24996. ' TEnum.blue.DoIt;',
  24997. ' TEnum(1).DoIt;',
  24998. ' TEnum.Swing(3);',
  24999. '']);
  25000. ConvertProgram;
  25001. CheckSource('TestTypeHelper_EnumType',
  25002. LinesToStr([ // statements
  25003. 'this.TEnum = {',
  25004. ' "0": "red",',
  25005. ' red: 0,',
  25006. ' "1": "blue",',
  25007. ' blue: 1',
  25008. '};',
  25009. 'rtl.createHelper(this, "THelper", null, function () {',
  25010. ' this.DoIt = function (e) {',
  25011. ' this.set($mod.TEnum.red);',
  25012. ' this.set(this.get() + 1);',
  25013. ' var $with = this.get();',
  25014. ' $mod.THelper.DoIt.call(this, 123);',
  25015. ' };',
  25016. ' this.Swing = function (w) {',
  25017. ' };',
  25018. '});',
  25019. 'this.e = 0;',
  25020. '']),
  25021. LinesToStr([ // $mod.$main
  25022. '$mod.THelper.DoIt.call({',
  25023. ' p: $mod,',
  25024. ' get: function () {',
  25025. ' return this.p.e;',
  25026. ' },',
  25027. ' set: function (v) {',
  25028. ' this.p.e = v;',
  25029. ' }',
  25030. '}, 123);',
  25031. '$mod.THelper.DoIt.call({',
  25032. ' p: $mod.TEnum,',
  25033. ' get: function () {',
  25034. ' return this.p.red;',
  25035. ' },',
  25036. ' set: function (v) {',
  25037. ' rtl.raiseE("EPropReadOnly");',
  25038. ' }',
  25039. '}, 123);',
  25040. '$mod.THelper.DoIt.call({',
  25041. ' p: $mod.TEnum,',
  25042. ' get: function () {',
  25043. ' return this.p.blue;',
  25044. ' },',
  25045. ' set: function (v) {',
  25046. ' rtl.raiseE("EPropReadOnly");',
  25047. ' }',
  25048. '}, 123);',
  25049. '$mod.THelper.DoIt.call({',
  25050. ' get: function () {',
  25051. ' return 1;',
  25052. ' },',
  25053. ' set: function (v) {',
  25054. ' rtl.raiseE("EPropReadOnly");',
  25055. ' }',
  25056. '}, 123);',
  25057. '$mod.THelper.Swing(3);',
  25058. '']));
  25059. end;
  25060. procedure TTestModule.TestTypeHelper_SetType;
  25061. begin
  25062. StartProgram(false);
  25063. Add([
  25064. '{$modeswitch typehelpers}',
  25065. 'type',
  25066. ' TEnum = (red,blue);',
  25067. ' TSetOfEnum = set of TEnum;',
  25068. ' THelper = type helper for TSetOfEnum',
  25069. ' procedure DoIt(e: byte = 123);',
  25070. ' constructor Init(e: TEnum);',
  25071. ' constructor InitEmpty;',
  25072. ' end;',
  25073. 'procedure THelper.DoIt(e: byte);',
  25074. 'begin',
  25075. ' Self:=[];',
  25076. ' Self:=[red];',
  25077. ' Include(Self,blue);',
  25078. 'end;',
  25079. 'constructor THelper.Init(e: TEnum);',
  25080. 'begin',
  25081. ' Self:=[];',
  25082. ' Self:=[e];',
  25083. ' Include(Self,blue);',
  25084. 'end;',
  25085. 'constructor THelper.InitEmpty;',
  25086. 'begin',
  25087. 'end;',
  25088. 'var s: TSetOfEnum;',
  25089. 'begin',
  25090. ' s.DoIt;',
  25091. //' [red].DoIt;',
  25092. //' with s do DoIt;',
  25093. //' with [red,blue] do DoIt;',
  25094. ' s:=TSetOfEnum.Init(blue);',
  25095. ' s:=s.Init(blue);',
  25096. '']);
  25097. ConvertProgram;
  25098. CheckSource('TestTypeHelper_SetType',
  25099. LinesToStr([ // statements
  25100. 'this.TEnum = {',
  25101. ' "0": "red",',
  25102. ' red: 0,',
  25103. ' "1": "blue",',
  25104. ' blue: 1',
  25105. '};',
  25106. 'rtl.createHelper(this, "THelper", null, function () {',
  25107. ' this.DoIt = function (e) {',
  25108. ' this.set({});',
  25109. ' this.set(rtl.createSet($mod.TEnum.red));',
  25110. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25111. ' };',
  25112. ' this.Init = function (e) {',
  25113. ' this.set({});',
  25114. ' this.set(rtl.createSet(e));',
  25115. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25116. ' return this.get();',
  25117. ' };',
  25118. ' this.InitEmpty = function () {',
  25119. ' return this.get();',
  25120. ' };',
  25121. ' this.$new = function (fn, args) {',
  25122. ' return this[fn].apply({',
  25123. ' p: {},',
  25124. ' get: function () {',
  25125. ' return this.p;',
  25126. ' },',
  25127. ' set: function (v) {',
  25128. ' this.p = v;',
  25129. ' }',
  25130. ' }, args);',
  25131. ' };',
  25132. '});',
  25133. 'this.s = {};',
  25134. '']),
  25135. LinesToStr([ // $mod.$main
  25136. '$mod.THelper.DoIt.call({',
  25137. ' p: $mod,',
  25138. ' get: function () {',
  25139. ' return this.p.s;',
  25140. ' },',
  25141. ' set: function (v) {',
  25142. ' this.p.s = v;',
  25143. ' }',
  25144. '}, 123);',
  25145. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25146. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25147. ' p: $mod,',
  25148. ' get: function () {',
  25149. ' return this.p.s;',
  25150. ' },',
  25151. ' set: function (v) {',
  25152. ' this.p.s = v;',
  25153. ' }',
  25154. '}, $mod.TEnum.blue));',
  25155. '']));
  25156. end;
  25157. procedure TTestModule.TestTypeHelper_InterfaceType;
  25158. begin
  25159. StartProgram(false);
  25160. Add([
  25161. '{$interfaces com}',
  25162. '{$modeswitch typehelpers}',
  25163. 'type',
  25164. ' IUnknown = interface',
  25165. ' function _AddRef: longint;',
  25166. ' function _Release: longint;',
  25167. ' end;',
  25168. ' TObject = class(IUnknown)',
  25169. ' function _AddRef: longint; virtual; abstract;',
  25170. ' function _Release: longint; virtual; abstract;',
  25171. ' end;',
  25172. ' THelper = type helper for IUnknown',
  25173. ' procedure Fly(e: byte = 123);',
  25174. ' class procedure Run; static;',
  25175. ' end;',
  25176. 'var',
  25177. ' i: IUnknown;',
  25178. ' o: TObject;',
  25179. 'procedure THelper.Fly(e: byte);',
  25180. 'begin',
  25181. ' i:=Self;',
  25182. ' o:=Self as TObject;',
  25183. ' Self:=nil;',
  25184. ' Self:=i;',
  25185. ' Self:=o;',
  25186. ' with Self do begin',
  25187. ' Fly;',
  25188. ' Fly();',
  25189. ' end;',
  25190. 'end;',
  25191. 'class procedure THelper.Run;',
  25192. 'var l: IUnknown;',
  25193. 'begin',
  25194. ' l.Fly;',
  25195. ' l.Fly();',
  25196. 'end;',
  25197. 'begin',
  25198. ' i.Fly;',
  25199. ' i.Fly();',
  25200. ' i.Run;',
  25201. ' i.Run();',
  25202. ' IUnknown.Run;',
  25203. ' IUnknown.Run();',
  25204. '']);
  25205. ConvertProgram;
  25206. CheckSource('TestTypeHelper_InterfaceType',
  25207. LinesToStr([ // statements
  25208. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25209. 'rtl.createClass(this, "TObject", null, function () {',
  25210. ' this.$init = function () {',
  25211. ' };',
  25212. ' this.$final = function () {',
  25213. ' };',
  25214. ' rtl.addIntf(this, $mod.IUnknown);',
  25215. '});',
  25216. 'rtl.createHelper(this, "THelper", null, function () {',
  25217. ' this.Fly = function (e) {',
  25218. ' var $ir = rtl.createIntfRefs();',
  25219. ' try {',
  25220. ' rtl.setIntfP($mod, "i", this.get());',
  25221. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25222. ' this.set(null);',
  25223. ' this.set($mod.i);',
  25224. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25225. ' var $with = this.get();',
  25226. ' $mod.THelper.Fly.call(this, 123);',
  25227. ' $mod.THelper.Fly.call(this, 123);',
  25228. ' } finally {',
  25229. ' $ir.free();',
  25230. ' };',
  25231. ' };',
  25232. ' this.Run = function () {',
  25233. ' var l = null;',
  25234. ' try {',
  25235. ' $mod.THelper.Fly.call({',
  25236. ' get: function () {',
  25237. ' return l;',
  25238. ' },',
  25239. ' set: function (v) {',
  25240. ' l = rtl.setIntfL(l, v);',
  25241. ' }',
  25242. ' }, 123);',
  25243. ' $mod.THelper.Fly.call({',
  25244. ' get: function () {',
  25245. ' return l;',
  25246. ' },',
  25247. ' set: function (v) {',
  25248. ' l = rtl.setIntfL(l, v);',
  25249. ' }',
  25250. ' }, 123);',
  25251. ' } finally {',
  25252. ' rtl._Release(l);',
  25253. ' };',
  25254. ' };',
  25255. '});',
  25256. 'this.i = null;',
  25257. 'this.o = null;',
  25258. '']),
  25259. LinesToStr([ // $mod.$main
  25260. '$mod.THelper.Fly.call({',
  25261. ' p: $mod,',
  25262. ' get: function () {',
  25263. ' return this.p.i;',
  25264. ' },',
  25265. ' set: function (v) {',
  25266. ' rtl.setIntfP(this.p, "i", v);',
  25267. ' }',
  25268. '}, 123);',
  25269. '$mod.THelper.Fly.call({',
  25270. ' p: $mod,',
  25271. ' get: function () {',
  25272. ' return this.p.i;',
  25273. ' },',
  25274. ' set: function (v) {',
  25275. ' rtl.setIntfP(this.p, "i", v);',
  25276. ' }',
  25277. '}, 123);',
  25278. '$mod.THelper.Run();',
  25279. '$mod.THelper.Run();',
  25280. '$mod.THelper.Run();',
  25281. '$mod.THelper.Run();',
  25282. '']));
  25283. end;
  25284. procedure TTestModule.TestTypeHelper_NestedSelf;
  25285. begin
  25286. StartProgram(false);
  25287. Add([
  25288. '{$modeswitch typehelpers}',
  25289. 'type',
  25290. ' THelper = type helper for string',
  25291. ' procedure Run(Value: string);',
  25292. ' end;',
  25293. 'procedure THelper.Run(Value: string);',
  25294. ' function Sub(i: nativeint): boolean;',
  25295. ' begin',
  25296. ' Result:=Self[i+1]=Value[i];',
  25297. ' end;',
  25298. 'begin',
  25299. ' if Self[3]=Value[4] then ;',
  25300. 'end;',
  25301. 'begin',
  25302. '']);
  25303. ConvertProgram;
  25304. CheckSource('TestTypeHelper_NestedSelf',
  25305. LinesToStr([ // statements
  25306. 'rtl.createHelper(this, "THelper", null, function () {',
  25307. ' this.Run = function (Value) {',
  25308. ' var $Self = this;',
  25309. ' function Sub(i) {',
  25310. ' var Result = false;',
  25311. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25312. ' return Result;',
  25313. ' };',
  25314. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25315. ' };',
  25316. '});',
  25317. '']),
  25318. LinesToStr([ // $mod.$main
  25319. '']));
  25320. end;
  25321. procedure TTestModule.TestProcType;
  25322. begin
  25323. StartProgram(false);
  25324. Add([
  25325. 'type',
  25326. ' TProcInt = procedure(vI: longint = 1);',
  25327. 'procedure DoIt(vJ: longint);',
  25328. 'begin end;',
  25329. 'var',
  25330. ' b: boolean;',
  25331. ' vP, vQ: tprocint;',
  25332. 'begin',
  25333. ' vp:=nil;',
  25334. ' vp:=vp;',
  25335. ' vp:=@doit;',
  25336. ' vp;',
  25337. ' vp();',
  25338. ' vp(2);',
  25339. ' b:=vp=nil;',
  25340. ' b:=nil=vp;',
  25341. ' b:=vp=vq;',
  25342. ' b:=vp=@doit;',
  25343. ' b:=@doit=vp;',
  25344. ' b:=vp<>nil;',
  25345. ' b:=nil<>vp;',
  25346. ' b:=vp<>vq;',
  25347. ' b:=vp<>@doit;',
  25348. ' b:=@doit<>vp;',
  25349. ' b:=Assigned(vp);',
  25350. ' if Assigned(vp) then ;']);
  25351. ConvertProgram;
  25352. CheckSource('TestProcType',
  25353. LinesToStr([ // statements
  25354. 'this.DoIt = function(vJ) {',
  25355. '};',
  25356. 'this.b = false;',
  25357. 'this.vP = null;',
  25358. 'this.vQ = null;'
  25359. ]),
  25360. LinesToStr([ // $mod.$main
  25361. '$mod.vP = null;',
  25362. '$mod.vP = $mod.vP;',
  25363. '$mod.vP = $mod.DoIt;',
  25364. '$mod.vP(1);',
  25365. '$mod.vP(1);',
  25366. '$mod.vP(2);',
  25367. '$mod.b = $mod.vP === null;',
  25368. '$mod.b = null === $mod.vP;',
  25369. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25370. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25371. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25372. '$mod.b = $mod.vP !== null;',
  25373. '$mod.b = null !== $mod.vP;',
  25374. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25375. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25376. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25377. '$mod.b = $mod.vP != null;',
  25378. 'if ($mod.vP != null) ;',
  25379. '']));
  25380. end;
  25381. procedure TTestModule.TestProcType_Arg;
  25382. begin
  25383. StartProgram(false);
  25384. Add([
  25385. 'type',
  25386. ' TProcInt = procedure(vI: longint = 1);',
  25387. 'procedure DoIt(vJ: longint); begin end;',
  25388. 'procedure DoSome(vP, vQ: TProcInt);',
  25389. 'var',
  25390. ' b: boolean;',
  25391. 'begin',
  25392. ' vp:=nil;',
  25393. ' vp:=vp;',
  25394. ' vp:=@doit;',
  25395. ' vp;',
  25396. ' vp();',
  25397. ' vp(2);',
  25398. ' b:=vp=nil;',
  25399. ' b:=nil=vp;',
  25400. ' b:=vp=vq;',
  25401. ' b:=vp=@doit;',
  25402. ' b:=@doit=vp;',
  25403. ' b:=vp<>nil;',
  25404. ' b:=nil<>vp;',
  25405. ' b:=vp<>vq;',
  25406. ' b:=vp<>@doit;',
  25407. ' b:=@doit<>vp;',
  25408. ' b:=Assigned(vp);',
  25409. ' if Assigned(vp) then ;',
  25410. 'end;',
  25411. 'begin',
  25412. ' DoSome(@DoIt,nil);']);
  25413. ConvertProgram;
  25414. CheckSource('TestProcType_Arg',
  25415. LinesToStr([ // statements
  25416. 'this.DoIt = function(vJ) {',
  25417. '};',
  25418. 'this.DoSome = function(vP, vQ) {',
  25419. ' var b = false;',
  25420. ' vP = null;',
  25421. ' vP = vP;',
  25422. ' vP = $mod.DoIt;',
  25423. ' vP(1);',
  25424. ' vP(1);',
  25425. ' vP(2);',
  25426. ' b = vP === null;',
  25427. ' b = null === vP;',
  25428. ' b = rtl.eqCallback(vP,vQ);',
  25429. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25430. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25431. ' b = vP !== null;',
  25432. ' b = null !== vP;',
  25433. ' b = !rtl.eqCallback(vP, vQ);',
  25434. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25435. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25436. ' b = vP != null;',
  25437. ' if (vP != null) ;',
  25438. '};',
  25439. '']),
  25440. LinesToStr([ // $mod.$main
  25441. '$mod.DoSome($mod.DoIt,null);',
  25442. '']));
  25443. end;
  25444. procedure TTestModule.TestProcType_FunctionFPC;
  25445. begin
  25446. StartProgram(false);
  25447. Add('type');
  25448. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25449. Add('function DoIt(vI: longint): longint;');
  25450. Add('begin end;');
  25451. Add('var');
  25452. Add(' b: boolean;');
  25453. Add(' vP, vQ: tfuncint;');
  25454. Add('begin');
  25455. Add(' vp:=nil;');
  25456. Add(' vp:=vp;');
  25457. Add(' vp:=@doit;'); // ok in fpc and delphi
  25458. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25459. Add(' vp;'); // ok in fpc and delphi
  25460. Add(' vp();');
  25461. Add(' vp(2);');
  25462. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25463. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25464. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25465. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25466. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25467. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25468. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25469. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25470. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25471. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25472. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25473. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25474. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25475. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25476. Add(' b:=Assigned(vp);');
  25477. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25478. Add(' doit(vp());'); // ok in fpc and delphi
  25479. Add(' doit(vp(2));'); // ok in fpc and delphi
  25480. ConvertProgram;
  25481. CheckSource('TestProcType_FunctionFPC',
  25482. LinesToStr([ // statements
  25483. 'this.DoIt = function(vI) {',
  25484. ' var Result = 0;',
  25485. ' return Result;',
  25486. '};',
  25487. 'this.b = false;',
  25488. 'this.vP = null;',
  25489. 'this.vQ = null;'
  25490. ]),
  25491. LinesToStr([ // $mod.$main
  25492. '$mod.vP = null;',
  25493. '$mod.vP = $mod.vP;',
  25494. '$mod.vP = $mod.DoIt;',
  25495. '$mod.vP(1);',
  25496. '$mod.vP(1);',
  25497. '$mod.vP(2);',
  25498. '$mod.b = $mod.vP === null;',
  25499. '$mod.b = null === $mod.vP;',
  25500. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25501. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25502. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25503. '$mod.b = 4 === $mod.vP(1);',
  25504. '$mod.b = $mod.vP !== null;',
  25505. '$mod.b = null !== $mod.vP;',
  25506. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25507. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25508. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25509. '$mod.b = 6 !== $mod.vP(1);',
  25510. '$mod.b = $mod.vP != null;',
  25511. '$mod.DoIt($mod.vP(1));',
  25512. '$mod.DoIt($mod.vP(2));',
  25513. '']));
  25514. end;
  25515. procedure TTestModule.TestProcType_FunctionDelphi;
  25516. begin
  25517. StartProgram(false);
  25518. Add('{$mode Delphi}');
  25519. Add('type');
  25520. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25521. Add('function DoIt(vI: longint): longint;');
  25522. Add('begin end;');
  25523. Add('var');
  25524. Add(' b: boolean;');
  25525. Add(' vP, vQ: tfuncint;');
  25526. Add('begin');
  25527. Add(' vp:=nil;');
  25528. Add(' vp:=vp;');
  25529. Add(' vp:=@doit;'); // ok in fpc and delphi
  25530. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25531. Add(' vp;'); // ok in fpc and delphi
  25532. Add(' vp();');
  25533. Add(' vp(2);');
  25534. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25535. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25536. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25537. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25538. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25539. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25540. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25541. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25542. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25543. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25544. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25545. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25546. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25547. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25548. Add(' b:=Assigned(vp);');
  25549. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25550. Add(' doit(vp());'); // ok in fpc and delphi
  25551. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25552. ConvertProgram;
  25553. CheckSource('TestProcType_FunctionDelphi',
  25554. LinesToStr([ // statements
  25555. 'this.DoIt = function(vI) {',
  25556. ' var Result = 0;',
  25557. ' return Result;',
  25558. '};',
  25559. 'this.b = false;',
  25560. 'this.vP = null;',
  25561. 'this.vQ = null;'
  25562. ]),
  25563. LinesToStr([ // $mod.$main
  25564. '$mod.vP = null;',
  25565. '$mod.vP = $mod.vP;',
  25566. '$mod.vP = $mod.DoIt;',
  25567. '$mod.vP = $mod.DoIt;',
  25568. '$mod.vP(1);',
  25569. '$mod.vP(1);',
  25570. '$mod.vP(2);',
  25571. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25572. '$mod.b = $mod.vP(1) === 3;',
  25573. '$mod.b = 4 === $mod.vP(1);',
  25574. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25575. '$mod.b = $mod.vP(1) !== 5;',
  25576. '$mod.b = 6 !== $mod.vP(1);',
  25577. '$mod.b = $mod.vP != null;',
  25578. '$mod.DoIt($mod.vP(1));',
  25579. '$mod.DoIt($mod.vP(1));',
  25580. '$mod.DoIt($mod.vP(2));',
  25581. '']));
  25582. end;
  25583. procedure TTestModule.TestProcType_ProcedureDelphi;
  25584. begin
  25585. StartProgram(false);
  25586. Add('{$mode Delphi}');
  25587. Add('type');
  25588. Add(' TProc = procedure;');
  25589. Add('procedure DoIt;');
  25590. Add('begin end;');
  25591. Add('var');
  25592. Add(' b: boolean;');
  25593. Add(' vP, vQ: tproc;');
  25594. Add('begin');
  25595. Add(' vp:=nil;');
  25596. Add(' vp:=vp;');
  25597. Add(' vp:=vq;');
  25598. 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
  25599. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25600. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25601. Add(' vp;'); // ok in fpc and delphi
  25602. Add(' vp();');
  25603. // equal
  25604. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25605. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25606. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25607. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25608. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25609. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25610. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25611. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25612. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25613. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25614. // unequal
  25615. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25616. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25617. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25618. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25619. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25620. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25621. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25622. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25623. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25624. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25625. Add(' b:=Assigned(vp);');
  25626. ConvertProgram;
  25627. CheckSource('TestProcType_ProcedureDelphi',
  25628. LinesToStr([ // statements
  25629. 'this.DoIt = function() {',
  25630. '};',
  25631. 'this.b = false;',
  25632. 'this.vP = null;',
  25633. 'this.vQ = null;'
  25634. ]),
  25635. LinesToStr([ // $mod.$main
  25636. '$mod.vP = null;',
  25637. '$mod.vP = $mod.vP;',
  25638. '$mod.vP = $mod.vQ;',
  25639. '$mod.vP = $mod.DoIt;',
  25640. '$mod.vP = $mod.DoIt;',
  25641. '$mod.vP();',
  25642. '$mod.vP();',
  25643. '$mod.b = $mod.vP === null;',
  25644. '$mod.b = null === $mod.vP;',
  25645. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25646. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25647. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25648. '$mod.b = $mod.vP !== null;',
  25649. '$mod.b = null !== $mod.vP;',
  25650. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25651. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25652. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25653. '$mod.b = $mod.vP != null;',
  25654. '']));
  25655. end;
  25656. procedure TTestModule.TestProcType_AsParam;
  25657. begin
  25658. StartProgram(false);
  25659. Add('type');
  25660. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25661. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25662. Add('var vJ: tfuncint;');
  25663. Add('begin');
  25664. Add(' vg:=vg;');
  25665. Add(' vj:=vh;');
  25666. Add(' vi:=vi;');
  25667. Add(' doit(vg,vg,vg);');
  25668. Add(' doit(vh,vh,vj);');
  25669. Add(' doit(vi,vi,vi);');
  25670. Add(' doit(vj,vj,vj);');
  25671. Add('end;');
  25672. Add('var i: tfuncint;');
  25673. Add('begin');
  25674. Add(' doit(i,i,i);');
  25675. ConvertProgram;
  25676. CheckSource('TestProcType_AsParam',
  25677. LinesToStr([ // statements
  25678. 'this.DoIt = function (vG,vH,vI) {',
  25679. ' var vJ = null;',
  25680. ' vG = vG;',
  25681. ' vJ = vH;',
  25682. ' vI.set(vI.get());',
  25683. ' $mod.DoIt(vG, vG, {',
  25684. ' get: function () {',
  25685. ' return vG;',
  25686. ' },',
  25687. ' set: function (v) {',
  25688. ' vG = v;',
  25689. ' }',
  25690. ' });',
  25691. ' $mod.DoIt(vH, vH, {',
  25692. ' get: function () {',
  25693. ' return vJ;',
  25694. ' },',
  25695. ' set: function (v) {',
  25696. ' vJ = v;',
  25697. ' }',
  25698. ' });',
  25699. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25700. ' $mod.DoIt(vJ, vJ, {',
  25701. ' get: function () {',
  25702. ' return vJ;',
  25703. ' },',
  25704. ' set: function (v) {',
  25705. ' vJ = v;',
  25706. ' }',
  25707. ' });',
  25708. '};',
  25709. 'this.i = null;'
  25710. ]),
  25711. LinesToStr([
  25712. '$mod.DoIt($mod.i,$mod.i,{',
  25713. ' p: $mod,',
  25714. ' get: function () {',
  25715. ' return this.p.i;',
  25716. ' },',
  25717. ' set: function (v) {',
  25718. ' this.p.i = v;',
  25719. ' }',
  25720. '});'
  25721. ]));
  25722. end;
  25723. procedure TTestModule.TestProcType_MethodFPC;
  25724. begin
  25725. StartProgram(false);
  25726. Add('type');
  25727. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25728. Add(' TObject = class');
  25729. Add(' function DoIt(vA: longint = 1): longint;');
  25730. Add(' end;');
  25731. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25732. Add('begin');
  25733. Add('end;');
  25734. Add('var');
  25735. Add(' Obj: TObject;');
  25736. Add(' vP: tfuncint;');
  25737. Add(' b: boolean;');
  25738. Add('begin');
  25739. Add(' vp:[email protected];'); // ok in fpc and delphi
  25740. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25741. Add(' vp;'); // ok in fpc and delphi
  25742. Add(' vp();');
  25743. Add(' vp(2);');
  25744. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25745. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25746. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25747. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25748. ConvertProgram;
  25749. CheckSource('TestProcType_MethodFPC',
  25750. LinesToStr([ // statements
  25751. 'rtl.createClass(this, "TObject", null, function () {',
  25752. ' this.$init = function () {',
  25753. ' };',
  25754. ' this.$final = function () {',
  25755. ' };',
  25756. ' this.DoIt = function (vA) {',
  25757. ' var Result = 0;',
  25758. ' return Result;',
  25759. ' };',
  25760. '});',
  25761. 'this.Obj = null;',
  25762. 'this.vP = null;',
  25763. 'this.b = false;'
  25764. ]),
  25765. LinesToStr([
  25766. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25767. '$mod.vP(1);',
  25768. '$mod.vP(1);',
  25769. '$mod.vP(2);',
  25770. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25771. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25772. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25773. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25774. '']));
  25775. end;
  25776. procedure TTestModule.TestProcType_MethodDelphi;
  25777. begin
  25778. StartProgram(false);
  25779. Add([
  25780. '{$mode delphi}',
  25781. 'type',
  25782. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25783. ' TObject = class',
  25784. ' function DoIt(vA: longint = 1): longint;',
  25785. ' end;',
  25786. 'function TObject.DoIt(vA: longint = 1): longint;',
  25787. 'begin',
  25788. 'end;',
  25789. 'var',
  25790. ' Obj: TObject;',
  25791. ' vP: tfuncint;',
  25792. ' b: boolean;',
  25793. 'begin',
  25794. ' vp:[email protected];', // ok in fpc and delphi
  25795. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25796. ' vp;', // ok in fpc and delphi
  25797. ' vp();',
  25798. ' vp(2);',
  25799. //' b:[email protected];', // ok in fpc, illegal in delphi
  25800. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25801. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25802. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25803. '']);
  25804. ConvertProgram;
  25805. CheckSource('TestProcType_MethodDelphi',
  25806. LinesToStr([ // statements
  25807. 'rtl.createClass(this, "TObject", null, function () {',
  25808. ' this.$init = function () {',
  25809. ' };',
  25810. ' this.$final = function () {',
  25811. ' };',
  25812. ' this.DoIt = function (vA) {',
  25813. ' var Result = 0;',
  25814. ' return Result;',
  25815. ' };',
  25816. '});',
  25817. 'this.Obj = null;',
  25818. 'this.vP = null;',
  25819. 'this.b = false;'
  25820. ]),
  25821. LinesToStr([
  25822. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25823. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25824. '$mod.vP(1);',
  25825. '$mod.vP(1);',
  25826. '$mod.vP(2);',
  25827. '']));
  25828. end;
  25829. procedure TTestModule.TestProcType_PropertyFPC;
  25830. begin
  25831. StartProgram(false);
  25832. Add('type');
  25833. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25834. Add(' TObject = class');
  25835. Add(' FOnFoo: TFuncInt;');
  25836. Add(' function DoIt(vA: longint = 1): longint;');
  25837. Add(' function GetFoo: TFuncInt;');
  25838. Add(' procedure SetFoo(const Value: TFuncInt);');
  25839. Add(' function GetEvents(Index: longint): TFuncInt;');
  25840. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25841. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25842. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25843. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25844. Add(' end;');
  25845. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25846. Add('function tobject.getfoo: tfuncint; begin end;');
  25847. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25848. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25849. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25850. Add('var');
  25851. Add(' Obj: TObject;');
  25852. Add(' vP: tfuncint;');
  25853. Add(' b: boolean;');
  25854. Add('begin');
  25855. Add(' obj.onfoo:=nil;');
  25856. Add(' obj.onbar:=nil;');
  25857. Add(' obj.events[1]:=nil;');
  25858. Add(' obj.onfoo:=obj.onfoo;');
  25859. Add(' obj.onbar:=obj.onbar;');
  25860. Add(' obj.events[2]:=obj.events[3];');
  25861. Add(' obj.onfoo:[email protected];');
  25862. Add(' obj.onbar:[email protected];');
  25863. Add(' obj.events[4]:[email protected];');
  25864. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25865. //Add(' obj.onbar:=obj.doit;'); // delphi
  25866. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25867. Add(' obj.onfoo;');
  25868. Add(' obj.onbar;');
  25869. //Add(' obj.events[5];'); ToDo in pasresolver
  25870. Add(' obj.onfoo();');
  25871. Add(' obj.onbar();');
  25872. Add(' obj.events[6]();');
  25873. Add(' b:=obj.onfoo=nil;');
  25874. Add(' b:=obj.onbar=nil;');
  25875. Add(' b:=obj.events[7]=nil;');
  25876. Add(' b:=obj.onfoo<>nil;');
  25877. Add(' b:=obj.onbar<>nil;');
  25878. Add(' b:=obj.events[8]<>nil;');
  25879. Add(' b:=obj.onfoo=vp;');
  25880. Add(' b:=obj.onbar=vp;');
  25881. Add(' b:=obj.events[9]=vp;');
  25882. Add(' b:=obj.onfoo=obj.onfoo;');
  25883. Add(' b:=obj.onbar=obj.onfoo;');
  25884. Add(' b:=obj.events[10]=obj.onfoo;');
  25885. Add(' b:=obj.onfoo<>obj.onfoo;');
  25886. Add(' b:=obj.onbar<>obj.onfoo;');
  25887. Add(' b:=obj.events[11]<>obj.onfoo;');
  25888. Add(' b:[email protected];');
  25889. Add(' b:[email protected];');
  25890. Add(' b:=obj.events[12][email protected];');
  25891. Add(' b:=obj.onfoo<>@obj.doit;');
  25892. Add(' b:=obj.onbar<>@obj.doit;');
  25893. Add(' b:=obj.events[12]<>@obj.doit;');
  25894. Add(' b:=Assigned(obj.onfoo);');
  25895. Add(' b:=Assigned(obj.onbar);');
  25896. Add(' b:=Assigned(obj.events[13]);');
  25897. ConvertProgram;
  25898. CheckSource('TestProcType_PropertyFPC',
  25899. LinesToStr([ // statements
  25900. 'rtl.createClass(this, "TObject", null, function () {',
  25901. ' this.$init = function () {',
  25902. ' this.FOnFoo = null;',
  25903. ' };',
  25904. ' this.$final = function () {',
  25905. ' this.FOnFoo = undefined;',
  25906. ' };',
  25907. ' this.DoIt = function (vA) {',
  25908. ' var Result = 0;',
  25909. ' return Result;',
  25910. ' };',
  25911. 'this.GetFoo = function () {',
  25912. ' var Result = null;',
  25913. ' return Result;',
  25914. '};',
  25915. 'this.SetFoo = function (Value) {',
  25916. '};',
  25917. 'this.GetEvents = function (Index) {',
  25918. ' var Result = null;',
  25919. ' return Result;',
  25920. '};',
  25921. 'this.SetEvents = function (Index, Value) {',
  25922. '};',
  25923. '});',
  25924. 'this.Obj = null;',
  25925. 'this.vP = null;',
  25926. 'this.b = false;'
  25927. ]),
  25928. LinesToStr([
  25929. '$mod.Obj.FOnFoo = null;',
  25930. '$mod.Obj.SetFoo(null);',
  25931. '$mod.Obj.SetEvents(1, null);',
  25932. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25933. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25934. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25935. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25936. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25937. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25938. '$mod.Obj.FOnFoo(1);',
  25939. '$mod.Obj.GetFoo();',
  25940. '$mod.Obj.FOnFoo(1);',
  25941. '$mod.Obj.GetFoo()(1);',
  25942. '$mod.Obj.GetEvents(6)(1);',
  25943. '$mod.b = $mod.Obj.FOnFoo === null;',
  25944. '$mod.b = $mod.Obj.GetFoo() === null;',
  25945. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25946. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25947. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25948. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25949. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25950. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25951. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25952. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25953. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25954. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25955. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25956. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25957. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25958. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25959. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25960. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25961. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25962. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25963. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25964. '$mod.b = $mod.Obj.FOnFoo != null;',
  25965. '$mod.b = $mod.Obj.GetFoo() != null;',
  25966. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25967. '']));
  25968. end;
  25969. procedure TTestModule.TestProcType_PropertyDelphi;
  25970. begin
  25971. StartProgram(false);
  25972. Add('{$mode delphi}');
  25973. Add('type');
  25974. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25975. Add(' TObject = class');
  25976. Add(' FOnFoo: TFuncInt;');
  25977. Add(' function DoIt(vA: longint = 1): longint;');
  25978. Add(' function GetFoo: TFuncInt;');
  25979. Add(' procedure SetFoo(const Value: TFuncInt);');
  25980. Add(' function GetEvents(Index: longint): TFuncInt;');
  25981. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25982. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25983. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25984. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25985. Add(' end;');
  25986. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25987. Add('function tobject.getfoo: tfuncint; begin end;');
  25988. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25989. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25990. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25991. Add('var');
  25992. Add(' Obj: TObject;');
  25993. Add(' vP: tfuncint;');
  25994. Add(' b: boolean;');
  25995. Add('begin');
  25996. Add(' obj.onfoo:=nil;');
  25997. Add(' obj.onbar:=nil;');
  25998. Add(' obj.events[1]:=nil;');
  25999. Add(' obj.onfoo:=obj.onfoo;');
  26000. Add(' obj.onbar:=obj.onbar;');
  26001. Add(' obj.events[2]:=obj.events[3];');
  26002. Add(' obj.onfoo:[email protected];');
  26003. Add(' obj.onbar:[email protected];');
  26004. Add(' obj.events[4]:[email protected];');
  26005. Add(' obj.onfoo:=obj.doit;'); // delphi
  26006. Add(' obj.onbar:=obj.doit;'); // delphi
  26007. Add(' obj.events[4]:=obj.doit;'); // delphi
  26008. Add(' obj.onfoo;');
  26009. Add(' obj.onbar;');
  26010. //Add(' obj.events[5];'); ToDo in pasresolver
  26011. Add(' obj.onfoo();');
  26012. Add(' obj.onbar();');
  26013. Add(' obj.events[6]();');
  26014. //Add(' b:=obj.onfoo=nil;'); // fpc
  26015. //Add(' b:=obj.onbar=nil;'); // fpc
  26016. //Add(' b:=obj.events[7]=nil;'); // fpc
  26017. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26018. //Add(' b:=obj.onbar<>nil;'); // fpc
  26019. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26020. Add(' b:=obj.onfoo=vp;');
  26021. Add(' b:=obj.onbar=vp;');
  26022. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26023. Add(' b:=obj.onfoo=obj.onfoo;');
  26024. Add(' b:=obj.onbar=obj.onfoo;');
  26025. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26026. Add(' b:=obj.onfoo<>obj.onfoo;');
  26027. Add(' b:=obj.onbar<>obj.onfoo;');
  26028. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26029. //Add(' b:[email protected];'); // fpc
  26030. //Add(' b:[email protected];'); // fpc
  26031. //Add(' b:=obj.events[12][email protected];'); // fpc
  26032. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26033. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26034. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26035. Add(' b:=Assigned(obj.onfoo);');
  26036. Add(' b:=Assigned(obj.onbar);');
  26037. Add(' b:=Assigned(obj.events[13]);');
  26038. ConvertProgram;
  26039. CheckSource('TestProcType_PropertyDelphi',
  26040. LinesToStr([ // statements
  26041. 'rtl.createClass(this, "TObject", null, function () {',
  26042. ' this.$init = function () {',
  26043. ' this.FOnFoo = null;',
  26044. ' };',
  26045. ' this.$final = function () {',
  26046. ' this.FOnFoo = undefined;',
  26047. ' };',
  26048. ' this.DoIt = function (vA) {',
  26049. ' var Result = 0;',
  26050. ' return Result;',
  26051. ' };',
  26052. 'this.GetFoo = function () {',
  26053. ' var Result = null;',
  26054. ' return Result;',
  26055. '};',
  26056. 'this.SetFoo = function (Value) {',
  26057. '};',
  26058. 'this.GetEvents = function (Index) {',
  26059. ' var Result = null;',
  26060. ' return Result;',
  26061. '};',
  26062. 'this.SetEvents = function (Index, Value) {',
  26063. '};',
  26064. '});',
  26065. 'this.Obj = null;',
  26066. 'this.vP = null;',
  26067. 'this.b = false;'
  26068. ]),
  26069. LinesToStr([
  26070. '$mod.Obj.FOnFoo = null;',
  26071. '$mod.Obj.SetFoo(null);',
  26072. '$mod.Obj.SetEvents(1, null);',
  26073. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26074. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26075. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26076. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26077. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26078. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26079. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26080. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26081. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26082. '$mod.Obj.FOnFoo(1);',
  26083. '$mod.Obj.GetFoo();',
  26084. '$mod.Obj.FOnFoo(1);',
  26085. '$mod.Obj.GetFoo()(1);',
  26086. '$mod.Obj.GetEvents(6)(1);',
  26087. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26088. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26089. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26090. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26091. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26092. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26093. '$mod.b = $mod.Obj.FOnFoo != null;',
  26094. '$mod.b = $mod.Obj.GetFoo() != null;',
  26095. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26096. '']));
  26097. end;
  26098. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26099. begin
  26100. StartProgram(false);
  26101. Add('type');
  26102. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26103. Add(' TObject = class');
  26104. Add(' FOnFoo: TFuncInt;');
  26105. Add(' function DoIt(vA: longint = 1): longint;');
  26106. Add(' function GetFoo: TFuncInt;');
  26107. Add(' procedure SetFoo(const Value: TFuncInt);');
  26108. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26109. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26110. Add(' end;');
  26111. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26112. Add('function tobject.getfoo: tfuncint; begin end;');
  26113. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26114. Add('var');
  26115. Add(' Obj: TObject;');
  26116. Add(' vP: tfuncint;');
  26117. Add(' b: boolean;');
  26118. Add('begin');
  26119. Add('with obj do begin');
  26120. Add(' fonfoo:=nil;');
  26121. Add(' onfoo:=nil;');
  26122. Add(' onbar:=nil;');
  26123. Add(' fonfoo:=fonfoo;');
  26124. Add(' onfoo:=onfoo;');
  26125. Add(' onbar:=onbar;');
  26126. Add(' fonfoo:=@doit;');
  26127. Add(' onfoo:=@doit;');
  26128. Add(' onbar:=@doit;');
  26129. //Add(' fonfoo:=doit;'); // delphi
  26130. //Add(' onfoo:=doit;'); // delphi
  26131. //Add(' onbar:=doit;'); // delphi
  26132. Add(' fonfoo;');
  26133. Add(' onfoo;');
  26134. Add(' onbar;');
  26135. Add(' fonfoo();');
  26136. Add(' onfoo();');
  26137. Add(' onbar();');
  26138. Add(' b:=fonfoo=nil;');
  26139. Add(' b:=onfoo=nil;');
  26140. Add(' b:=onbar=nil;');
  26141. Add(' b:=fonfoo<>nil;');
  26142. Add(' b:=onfoo<>nil;');
  26143. Add(' b:=onbar<>nil;');
  26144. Add(' b:=fonfoo=vp;');
  26145. Add(' b:=onfoo=vp;');
  26146. Add(' b:=onbar=vp;');
  26147. Add(' b:=fonfoo=fonfoo;');
  26148. Add(' b:=onfoo=onfoo;');
  26149. Add(' b:=onbar=onfoo;');
  26150. Add(' b:=fonfoo<>fonfoo;');
  26151. Add(' b:=onfoo<>onfoo;');
  26152. Add(' b:=onbar<>onfoo;');
  26153. Add(' b:=fonfoo=@doit;');
  26154. Add(' b:=onfoo=@doit;');
  26155. Add(' b:=onbar=@doit;');
  26156. Add(' b:=fonfoo<>@doit;');
  26157. Add(' b:=onfoo<>@doit;');
  26158. Add(' b:=onbar<>@doit;');
  26159. Add(' b:=Assigned(fonfoo);');
  26160. Add(' b:=Assigned(onfoo);');
  26161. Add(' b:=Assigned(onbar);');
  26162. Add('end;');
  26163. ConvertProgram;
  26164. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26165. LinesToStr([ // statements
  26166. 'rtl.createClass(this, "TObject", null, function () {',
  26167. ' this.$init = function () {',
  26168. ' this.FOnFoo = null;',
  26169. ' };',
  26170. ' this.$final = function () {',
  26171. ' this.FOnFoo = undefined;',
  26172. ' };',
  26173. ' this.DoIt = function (vA) {',
  26174. ' var Result = 0;',
  26175. ' return Result;',
  26176. ' };',
  26177. ' this.GetFoo = function () {',
  26178. ' var Result = null;',
  26179. ' return Result;',
  26180. ' };',
  26181. ' this.SetFoo = function (Value) {',
  26182. ' };',
  26183. '});',
  26184. 'this.Obj = null;',
  26185. 'this.vP = null;',
  26186. 'this.b = false;'
  26187. ]),
  26188. LinesToStr([
  26189. 'var $with = $mod.Obj;',
  26190. '$with.FOnFoo = null;',
  26191. '$with.FOnFoo = null;',
  26192. '$with.SetFoo(null);',
  26193. '$with.FOnFoo = $with.FOnFoo;',
  26194. '$with.FOnFoo = $with.FOnFoo;',
  26195. '$with.SetFoo($with.GetFoo());',
  26196. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26197. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26198. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26199. '$with.FOnFoo(1);',
  26200. '$with.FOnFoo(1);',
  26201. '$with.GetFoo();',
  26202. '$with.FOnFoo(1);',
  26203. '$with.FOnFoo(1);',
  26204. '$with.GetFoo()(1);',
  26205. '$mod.b = $with.FOnFoo === null;',
  26206. '$mod.b = $with.FOnFoo === null;',
  26207. '$mod.b = $with.GetFoo() === null;',
  26208. '$mod.b = $with.FOnFoo !== null;',
  26209. '$mod.b = $with.FOnFoo !== null;',
  26210. '$mod.b = $with.GetFoo() !== null;',
  26211. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26212. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26213. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26214. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26215. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26216. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26217. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26218. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26219. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26220. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26221. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26222. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26223. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26224. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26225. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26226. '$mod.b = $with.FOnFoo != null;',
  26227. '$mod.b = $with.FOnFoo != null;',
  26228. '$mod.b = $with.GetFoo() != null;',
  26229. '']));
  26230. end;
  26231. procedure TTestModule.TestProcType_Nested;
  26232. begin
  26233. StartProgram(false);
  26234. Add([
  26235. 'type',
  26236. ' TProcInt = procedure(vI: longint = 1);',
  26237. 'procedure DoIt(vJ: longint);',
  26238. 'var aProc: TProcInt;',
  26239. ' b: boolean;',
  26240. ' procedure Sub(vK: longint);',
  26241. ' var aSub: TProcInt;',
  26242. ' procedure SubSub(vK: longint);',
  26243. ' var aSubSub: TProcInt;',
  26244. ' begin;',
  26245. ' aProc:=@DoIt;',
  26246. ' aSub:=@DoIt;',
  26247. ' aSubSub:=@DoIt;',
  26248. ' aProc:=@Sub;',
  26249. ' aSub:=@Sub;',
  26250. ' aSubSub:=@Sub;',
  26251. ' aProc:=@SubSub;',
  26252. ' aSub:=@SubSub;',
  26253. ' aSubSub:=@SubSub;',
  26254. ' end;',
  26255. ' begin;',
  26256. ' end;',
  26257. 'begin;',
  26258. ' aProc:=@Sub;',
  26259. ' b:=aProc=@Sub;',
  26260. ' b:=@Sub=aProc;',
  26261. 'end;',
  26262. 'begin',
  26263. '']);
  26264. ConvertProgram;
  26265. CheckSource('TestProcType_Nested',
  26266. LinesToStr([ // statements
  26267. 'this.DoIt = function (vJ) {',
  26268. ' var aProc = null;',
  26269. ' var b = false;',
  26270. ' function Sub(vK) {',
  26271. ' var aSub = null;',
  26272. ' function SubSub(vK) {',
  26273. ' var aSubSub = null;',
  26274. ' aProc = $mod.DoIt;',
  26275. ' aSub = $mod.DoIt;',
  26276. ' aSubSub = $mod.DoIt;',
  26277. ' aProc = Sub;',
  26278. ' aSub = Sub;',
  26279. ' aSubSub = Sub;',
  26280. ' aProc = SubSub;',
  26281. ' aSub = SubSub;',
  26282. ' aSubSub = SubSub;',
  26283. ' };',
  26284. ' };',
  26285. ' aProc = Sub;',
  26286. ' b = rtl.eqCallback(aProc, Sub);',
  26287. ' b = rtl.eqCallback(Sub, aProc);',
  26288. '};',
  26289. '']),
  26290. LinesToStr([ // $mod.$main
  26291. '']));
  26292. end;
  26293. procedure TTestModule.TestProcType_NestedOfObject;
  26294. begin
  26295. StartProgram(false);
  26296. Add([
  26297. 'type',
  26298. ' TProcInt = procedure(vI: longint = 1) of object;',
  26299. ' TObject = class',
  26300. ' procedure DoIt(vJ: longint);',
  26301. ' end;',
  26302. 'procedure TObject.DoIt(vJ: longint);',
  26303. 'var aProc: TProcInt;',
  26304. ' b: boolean;',
  26305. ' procedure Sub(vK: longint);',
  26306. ' var aSub: TProcInt;',
  26307. ' procedure SubSub(vK: longint);',
  26308. ' var aSubSub: TProcInt;',
  26309. ' begin;',
  26310. ' aProc:=@DoIt;',
  26311. ' aSub:=@DoIt;',
  26312. ' aSubSub:=@DoIt;',
  26313. ' aProc:=@Sub;',
  26314. ' aSub:=@Sub;',
  26315. ' aSubSub:=@Sub;',
  26316. ' aProc:=@SubSub;',
  26317. ' aSub:=@SubSub;',
  26318. ' aSubSub:=@SubSub;',
  26319. ' end;',
  26320. ' begin;',
  26321. ' end;',
  26322. 'begin;',
  26323. ' aProc:=@Sub;',
  26324. ' b:=aProc=@Sub;',
  26325. ' b:=@Sub=aProc;',
  26326. 'end;',
  26327. 'begin',
  26328. '']);
  26329. ConvertProgram;
  26330. CheckSource('TestProcType_Nested',
  26331. LinesToStr([ // statements
  26332. 'rtl.createClass(this, "TObject", null, function () {',
  26333. ' this.$init = function () {',
  26334. ' };',
  26335. ' this.$final = function () {',
  26336. ' };',
  26337. ' this.DoIt = function (vJ) {',
  26338. ' var $Self = this;',
  26339. ' var aProc = null;',
  26340. ' var b = false;',
  26341. ' function Sub(vK) {',
  26342. ' var aSub = null;',
  26343. ' function SubSub(vK) {',
  26344. ' var aSubSub = null;',
  26345. ' aProc = rtl.createCallback($Self, "DoIt");',
  26346. ' aSub = rtl.createCallback($Self, "DoIt");',
  26347. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26348. ' aProc = Sub;',
  26349. ' aSub = Sub;',
  26350. ' aSubSub = Sub;',
  26351. ' aProc = SubSub;',
  26352. ' aSub = SubSub;',
  26353. ' aSubSub = SubSub;',
  26354. ' };',
  26355. ' };',
  26356. ' aProc = Sub;',
  26357. ' b = rtl.eqCallback(aProc, Sub);',
  26358. ' b = rtl.eqCallback(Sub, aProc);',
  26359. ' };',
  26360. '});',
  26361. '']),
  26362. LinesToStr([ // $mod.$main
  26363. '']));
  26364. end;
  26365. procedure TTestModule.TestProcType_ReferenceToProc;
  26366. begin
  26367. StartProgram(false);
  26368. Add([
  26369. 'type',
  26370. ' TProcRef = reference to procedure(i: longint = 0);',
  26371. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26372. 'var',
  26373. ' p: TProcRef;',
  26374. ' f: TFuncRef;',
  26375. 'procedure DoIt(i: longint);',
  26376. 'begin',
  26377. 'end;',
  26378. 'function GetIt(i: longint): longint;',
  26379. 'begin',
  26380. ' p:=@DoIt;',
  26381. ' f:=@GetIt;',
  26382. ' f;',
  26383. ' f();',
  26384. ' f(1);',
  26385. 'end;',
  26386. 'begin',
  26387. ' p:=@DoIt;',
  26388. ' f:=@GetIt;',
  26389. ' f;',
  26390. ' f();',
  26391. ' f(1);',
  26392. ' p:=TProcRef(f);',
  26393. '']);
  26394. ConvertProgram;
  26395. CheckSource('TestProcType_ReferenceToProc',
  26396. LinesToStr([ // statements
  26397. 'this.p = null;',
  26398. 'this.f = null;',
  26399. 'this.DoIt = function (i) {',
  26400. '};',
  26401. 'this.GetIt = function (i) {',
  26402. ' var Result = 0;',
  26403. ' $mod.p = $mod.DoIt;',
  26404. ' $mod.f = $mod.GetIt;',
  26405. ' $mod.f(0);',
  26406. ' $mod.f(0);',
  26407. ' $mod.f(1);',
  26408. ' return Result;',
  26409. '};',
  26410. '']),
  26411. LinesToStr([ // $mod.$main
  26412. '$mod.p = $mod.DoIt;',
  26413. '$mod.f = $mod.GetIt;',
  26414. '$mod.f(0);',
  26415. '$mod.f(0);',
  26416. '$mod.f(1);',
  26417. '$mod.p = $mod.f;',
  26418. '']));
  26419. end;
  26420. procedure TTestModule.TestProcType_ReferenceToMethod;
  26421. begin
  26422. StartProgram(false);
  26423. Add([
  26424. 'type',
  26425. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26426. ' TObject = class',
  26427. ' function Grow(s: longint): longint;',
  26428. ' end;',
  26429. 'var',
  26430. ' f: tfuncref;',
  26431. 'function tobject.grow(s: longint): longint;',
  26432. ' function GrowSub(i: longint): longint;',
  26433. ' begin',
  26434. ' f:=@grow;',
  26435. ' f:=@growsub;',
  26436. ' end;',
  26437. 'begin',
  26438. ' f:=@grow;',
  26439. ' f:=@growsub;',
  26440. 'end;',
  26441. 'begin',
  26442. '']);
  26443. ConvertProgram;
  26444. CheckSource('TestProcType_ReferenceToMethod',
  26445. LinesToStr([ // statements
  26446. 'rtl.createClass(this, "TObject", null, function () {',
  26447. ' this.$init = function () {',
  26448. ' };',
  26449. ' this.$final = function () {',
  26450. ' };',
  26451. ' this.Grow = function (s) {',
  26452. ' var $Self = this;',
  26453. ' var Result = 0;',
  26454. ' function GrowSub(i) {',
  26455. ' var Result = 0;',
  26456. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26457. ' $mod.f = GrowSub;',
  26458. ' return Result;',
  26459. ' };',
  26460. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26461. ' $mod.f = GrowSub;',
  26462. ' return Result;',
  26463. ' };',
  26464. '});',
  26465. 'this.f = null;',
  26466. '']),
  26467. LinesToStr([ // $mod.$main
  26468. '']));
  26469. end;
  26470. procedure TTestModule.TestProcType_Typecast;
  26471. begin
  26472. StartProgram(false);
  26473. Add([
  26474. 'type',
  26475. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26476. ' TEvent = procedure of object;',
  26477. ' TGetter = function:longint of object;',
  26478. ' TProcA = procedure(i: longint);',
  26479. ' TFuncB = function(i, j: longint): longint;',
  26480. 'procedure DoIt(); varargs; begin end;',
  26481. 'var',
  26482. ' Notify: tnotifyevent;',
  26483. ' Event: tevent;',
  26484. ' Getter: tgetter;',
  26485. ' ProcA: tproca;',
  26486. ' FuncB: tfuncb;',
  26487. ' p: pointer;',
  26488. 'begin',
  26489. ' notify:=tnotifyevent(event);',
  26490. ' event:=tevent(event);',
  26491. ' event:=tevent(notify);',
  26492. ' event:=tevent(getter);',
  26493. ' event:=tevent(proca);',
  26494. ' proca:=tproca(funcb);',
  26495. ' funcb:=tfuncb(funcb);',
  26496. ' funcb:=tfuncb(proca);',
  26497. ' funcb:=tfuncb(getter);',
  26498. ' proca:=tproca(p);',
  26499. ' funcb:=tfuncb(p);',
  26500. ' getter:=tgetter(p);',
  26501. ' p:=pointer(notify);',
  26502. ' p:=notify;',
  26503. ' p:=pointer(proca);',
  26504. ' p:=proca;',
  26505. ' p:=pointer(funcb);',
  26506. ' p:=funcb;',
  26507. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26508. '']);
  26509. ConvertProgram;
  26510. CheckSource('TestProcType_Typecast',
  26511. LinesToStr([ // statements
  26512. 'this.DoIt = function () {',
  26513. '};',
  26514. 'this.Notify = null;',
  26515. 'this.Event = null;',
  26516. 'this.Getter = null;',
  26517. 'this.ProcA = null;',
  26518. 'this.FuncB = null;',
  26519. 'this.p = null;',
  26520. '']),
  26521. LinesToStr([ // $mod.$main
  26522. '$mod.Notify = $mod.Event;',
  26523. '$mod.Event = $mod.Event;',
  26524. '$mod.Event = $mod.Notify;',
  26525. '$mod.Event = $mod.Getter;',
  26526. '$mod.Event = $mod.ProcA;',
  26527. '$mod.ProcA = $mod.FuncB;',
  26528. '$mod.FuncB = $mod.FuncB;',
  26529. '$mod.FuncB = $mod.ProcA;',
  26530. '$mod.FuncB = $mod.Getter;',
  26531. '$mod.ProcA = $mod.p;',
  26532. '$mod.FuncB = $mod.p;',
  26533. '$mod.Getter = $mod.p;',
  26534. '$mod.p = $mod.Notify;',
  26535. '$mod.p = $mod.Notify;',
  26536. '$mod.p = $mod.ProcA;',
  26537. '$mod.p = $mod.ProcA;',
  26538. '$mod.p = $mod.FuncB;',
  26539. '$mod.p = $mod.FuncB;',
  26540. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26541. '']));
  26542. end;
  26543. procedure TTestModule.TestProcType_PassProcToUntyped;
  26544. begin
  26545. StartProgram(false);
  26546. Add([
  26547. 'type',
  26548. ' TEvent = procedure of object;',
  26549. ' TFunc = function: longint;',
  26550. 'procedure DoIt(); varargs; begin end;',
  26551. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26552. 'var',
  26553. ' Event: tevent;',
  26554. ' Func: TFunc;',
  26555. 'begin',
  26556. ' doit(event,func);',
  26557. ' dosome(event,event,event);',
  26558. ' dosome(func,func,func);',
  26559. '']);
  26560. ConvertProgram;
  26561. CheckSource('TestProcType_PassProcToUntyped',
  26562. LinesToStr([ // statements
  26563. 'this.DoIt = function () {',
  26564. '};',
  26565. 'this.DoSome = function (a, b, p) {',
  26566. '};',
  26567. 'this.Event = null;',
  26568. 'this.Func = null;',
  26569. '']),
  26570. LinesToStr([ // $mod.$main
  26571. '$mod.DoIt($mod.Event, $mod.Func);',
  26572. '$mod.DoSome($mod.Event, {',
  26573. ' p: $mod,',
  26574. ' get: function () {',
  26575. ' return this.p.Event;',
  26576. ' },',
  26577. ' set: function (v) {',
  26578. ' this.p.Event = v;',
  26579. ' }',
  26580. '}, $mod.Event);',
  26581. '$mod.DoSome($mod.Func, {',
  26582. ' p: $mod,',
  26583. ' get: function () {',
  26584. ' return this.p.Func;',
  26585. ' },',
  26586. ' set: function (v) {',
  26587. ' this.p.Func = v;',
  26588. ' }',
  26589. '}, $mod.Func);',
  26590. '']));
  26591. end;
  26592. procedure TTestModule.TestProcType_PassProcToArray;
  26593. begin
  26594. StartProgram(false);
  26595. Add([
  26596. 'type',
  26597. ' TFunc = function: longint;',
  26598. ' TArrFunc = array of TFunc;',
  26599. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26600. 'function GetIt: longint; begin end;',
  26601. 'var',
  26602. ' Func: tfunc;',
  26603. 'begin',
  26604. ' doit([]);',
  26605. ' doit([@GetIt]);',
  26606. ' doit([Func]);',
  26607. '']);
  26608. ConvertProgram;
  26609. CheckSource('TestProcType_PassProcToArray',
  26610. LinesToStr([ // statements
  26611. 'this.DoIt = function (Arr) {',
  26612. '};',
  26613. 'this.GetIt = function () {',
  26614. ' var Result = 0;',
  26615. ' return Result;',
  26616. '};',
  26617. 'this.Func = null;',
  26618. '']),
  26619. LinesToStr([ // $mod.$main
  26620. '$mod.DoIt([]);',
  26621. '$mod.DoIt([$mod.GetIt]);',
  26622. '$mod.DoIt([$mod.Func]);',
  26623. '']));
  26624. end;
  26625. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26626. begin
  26627. StartProgram(false);
  26628. Add([
  26629. '{$modeswitch externalclass}',
  26630. 'type',
  26631. ' TProc = reference to procedure(i: longint); safecall;',
  26632. ' TEvent = procedure(i: longint) of object; safecall;',
  26633. ' TExtA = class external name ''ExtObj''',
  26634. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26635. ' procedure DoSome(Id: longint = 1);',
  26636. ' procedure SetOnClick(const e: TEvent);',
  26637. ' property OnClick: TEvent write SetOnClick;',
  26638. ' class procedure Fly(Id: longint = 1); static;',
  26639. ' procedure SetOnShow(const p: TProc);',
  26640. ' property OnShow: TProc write SetOnShow;',
  26641. ' end;',
  26642. 'procedure Run(i: longint = 1);',
  26643. 'begin',
  26644. 'end;',
  26645. 'var',
  26646. ' Obj: texta;',
  26647. ' e: TEvent;',
  26648. ' p: TProc;',
  26649. 'begin',
  26650. ' e:=e;',
  26651. ' e:[email protected];',
  26652. ' e:[email protected];',
  26653. ' e:=TEvent(@obj.dosome);', // no safecall
  26654. ' obj.OnClick:[email protected];',
  26655. ' obj.OnClick:[email protected];',
  26656. ' obj.setonclick(@obj.doit);',
  26657. ' obj.setonclick(@obj.dosome);',
  26658. ' p:=@Run;',
  26659. ' p:[email protected];',
  26660. ' obj.OnShow:=@Run;',
  26661. ' obj.OnShow:[email protected];',
  26662. ' obj.setOnShow(@Run);',
  26663. ' obj.setOnShow(@TExtA.Fly);',
  26664. ' with obj do begin',
  26665. ' e:=@doit;',
  26666. ' e:=@dosome;',
  26667. ' OnClick:=@doit;',
  26668. ' OnClick:=@dosome;',
  26669. ' setonclick(@doit);',
  26670. ' setonclick(@dosome);',
  26671. ' OnShow:=@Run;',
  26672. ' setOnShow(@Run);',
  26673. ' end;']);
  26674. ConvertProgram;
  26675. CheckSource('TestProcType_SafeCallObjFPC',
  26676. LinesToStr([ // statements
  26677. 'this.Run = function (i) {',
  26678. '};',
  26679. 'this.Obj = null;',
  26680. 'this.e = null;',
  26681. 'this.p = null;',
  26682. '']),
  26683. LinesToStr([ // $mod.$main
  26684. '$mod.e = $mod.e;',
  26685. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26686. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26687. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26688. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26689. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26690. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26691. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26692. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26693. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26694. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26695. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26696. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26697. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26698. 'var $with = $mod.Obj;',
  26699. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26700. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26701. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26702. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26703. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26704. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26705. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26706. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26707. '']));
  26708. end;
  26709. procedure TTestModule.TestProcType_SafeCallDelphi;
  26710. begin
  26711. StartProgram(false);
  26712. Add([
  26713. '{$mode delphi}',
  26714. '{$modeswitch externalclass}',
  26715. 'type',
  26716. ' TProc = reference to procedure(i: longint); safecall;',
  26717. ' TEvent = procedure(i: longint) of object; safecall;',
  26718. ' TExtA = class external name ''ExtObj''',
  26719. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26720. ' procedure DoSome(Id: longint = 1);',
  26721. ' procedure SetOnClick(const e: TEvent);',
  26722. ' property OnClick: TEvent write SetOnClick;',
  26723. ' class procedure Fly(Id: longint = 1); static;',
  26724. ' procedure SetOnShow(const p: TProc);',
  26725. ' property OnShow: TProc write SetOnShow;',
  26726. ' end;',
  26727. 'procedure Run(i: longint = 1);',
  26728. 'begin',
  26729. 'end;',
  26730. 'var',
  26731. ' Obj: texta;',
  26732. ' e: TEvent;',
  26733. ' p: TProc;',
  26734. 'begin',
  26735. ' e:=e;',
  26736. ' e:=obj.doit;',
  26737. ' e:=obj.dosome;',
  26738. ' e:=TEvent(@obj.dosome);', // no safecall
  26739. ' obj.OnClick:=obj.doit;',
  26740. ' obj.OnClick:=obj.dosome;',
  26741. ' obj.setonclick(obj.doit);',
  26742. ' obj.setonclick(obj.dosome);',
  26743. ' p:=Run;',
  26744. ' p:=TExtA.Fly;',
  26745. ' obj.OnShow:=Run;',
  26746. ' obj.OnShow:=TExtA.Fly;',
  26747. ' obj.setOnShow(Run);',
  26748. ' obj.setOnShow(TExtA.Fly);',
  26749. ' with obj do begin',
  26750. ' e:=doit;',
  26751. ' e:=dosome;',
  26752. ' OnClick:=doit;',
  26753. ' OnClick:=dosome;',
  26754. ' setonclick(doit);',
  26755. ' setonclick(dosome);',
  26756. ' OnShow:=@Run;',
  26757. ' setOnShow(@Run);',
  26758. ' end;']);
  26759. ConvertProgram;
  26760. CheckSource('TestProcType_SafeCallDelphi',
  26761. LinesToStr([ // statements
  26762. 'this.Run = function (i) {',
  26763. '};',
  26764. 'this.Obj = null;',
  26765. 'this.e = null;',
  26766. 'this.p = null;',
  26767. '']),
  26768. LinesToStr([ // $mod.$main
  26769. '$mod.e = $mod.e;',
  26770. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26771. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26772. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26773. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26774. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26775. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26776. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26777. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26778. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26779. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26780. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26781. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26782. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26783. 'var $with = $mod.Obj;',
  26784. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26785. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26786. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26787. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26788. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26789. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26790. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26791. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26792. '']));
  26793. end;
  26794. procedure TTestModule.TestPointer;
  26795. begin
  26796. StartProgram(false);
  26797. Add(['type',
  26798. ' TObject = class end;',
  26799. ' TClass = class of TObject;',
  26800. ' TArrInt = array of longint;',
  26801. 'const',
  26802. ' n = nil;',
  26803. 'var',
  26804. ' v: jsvalue;',
  26805. ' Obj: tobject;',
  26806. ' C: tclass;',
  26807. ' a: tarrint;',
  26808. ' p: Pointer = nil;',
  26809. ' s: string;',
  26810. 'begin',
  26811. ' p:=p;',
  26812. ' p:=nil;',
  26813. ' if p=nil then;',
  26814. ' if nil=p then;',
  26815. ' if Assigned(p) then;',
  26816. ' p:=Pointer(v);',
  26817. ' p:=obj;',
  26818. ' p:=c;',
  26819. ' p:=a;',
  26820. ' p:=tobject;',
  26821. ' obj:=TObject(p);',
  26822. ' c:=TClass(p);',
  26823. ' a:=TArrInt(p);',
  26824. ' p:=n;',
  26825. ' p:=Pointer(a);',
  26826. ' p:=pointer(s);',
  26827. ' s:=string(p);',
  26828. '']);
  26829. ConvertProgram;
  26830. CheckSource('TestPointer',
  26831. LinesToStr([ // statements
  26832. 'rtl.createClass(this, "TObject", null, function () {',
  26833. ' this.$init = function () {',
  26834. ' };',
  26835. ' this.$final = function () {',
  26836. ' };',
  26837. '});',
  26838. 'this.n = null;',
  26839. 'this.v = undefined;',
  26840. 'this.Obj = null;',
  26841. 'this.C = null;',
  26842. 'this.a = [];',
  26843. 'this.p = null;',
  26844. 'this.s = "";',
  26845. '']),
  26846. LinesToStr([ // $mod.$main
  26847. '$mod.p = $mod.p;',
  26848. '$mod.p = null;',
  26849. 'if ($mod.p === null) ;',
  26850. 'if (null === $mod.p) ;',
  26851. 'if ($mod.p != null) ;',
  26852. '$mod.p = $mod.v;',
  26853. '$mod.p = $mod.Obj;',
  26854. '$mod.p = $mod.C;',
  26855. '$mod.p = $mod.a;',
  26856. '$mod.p = $mod.TObject;',
  26857. '$mod.Obj = $mod.p;',
  26858. '$mod.C = $mod.p;',
  26859. '$mod.a = $mod.p;',
  26860. '$mod.p = null;',
  26861. '$mod.p = $mod.a;',
  26862. '$mod.p = $mod.s;',
  26863. '$mod.s = $mod.p;',
  26864. '']));
  26865. end;
  26866. procedure TTestModule.TestPointer_Proc;
  26867. begin
  26868. StartProgram(false);
  26869. Add('type');
  26870. Add(' TObject = class');
  26871. Add(' procedure DoIt; virtual; abstract;');
  26872. Add(' end;');
  26873. Add('procedure DoSome; begin end;');
  26874. Add('var');
  26875. Add(' o: TObject;');
  26876. Add(' p: Pointer;');
  26877. Add('begin');
  26878. Add(' p:=@DoSome;');
  26879. Add(' p:[email protected];');
  26880. ConvertProgram;
  26881. CheckSource('TestPointer_Proc',
  26882. LinesToStr([ // statements
  26883. 'rtl.createClass(this, "TObject", null, function () {',
  26884. ' this.$init = function () {',
  26885. ' };',
  26886. ' this.$final = function () {',
  26887. ' };',
  26888. '});',
  26889. 'this.DoSome = function () {',
  26890. '};',
  26891. 'this.o = null;',
  26892. 'this.p = null;',
  26893. '']),
  26894. LinesToStr([ // $mod.$main
  26895. '$mod.p = $mod.DoSome;',
  26896. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26897. '']));
  26898. end;
  26899. procedure TTestModule.TestPointer_AssignRecordFail;
  26900. begin
  26901. StartProgram(false);
  26902. Add('type');
  26903. Add(' TRec = record end;');
  26904. Add('var');
  26905. Add(' p: Pointer;');
  26906. Add(' r: TRec;');
  26907. Add('begin');
  26908. Add(' p:=r;');
  26909. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26910. nIncompatibleTypesGotExpected);
  26911. ConvertProgram;
  26912. end;
  26913. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26914. begin
  26915. StartProgram(false);
  26916. Add('type');
  26917. Add(' TArr = array[boolean] of longint;');
  26918. Add('var');
  26919. Add(' p: Pointer;');
  26920. Add(' a: TArr;');
  26921. Add('begin');
  26922. Add(' p:=a;');
  26923. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26924. nIncompatibleTypesGotExpected);
  26925. ConvertProgram;
  26926. end;
  26927. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26928. begin
  26929. StartProgram(false);
  26930. Add([
  26931. 'procedure DoIt(args: array of jsvalue); begin end;',
  26932. 'procedure DoAll; varargs; begin end;',
  26933. 'var',
  26934. ' v: jsvalue;',
  26935. 'begin',
  26936. ' DoIt([pointer(v)]);',
  26937. ' DoAll(pointer(v));',
  26938. '']);
  26939. ConvertProgram;
  26940. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26941. LinesToStr([ // statements
  26942. 'this.DoIt = function (args) {',
  26943. '};',
  26944. 'this.DoAll = function () {',
  26945. '};',
  26946. 'this.v = undefined;',
  26947. '']),
  26948. LinesToStr([ // $mod.$main
  26949. '$mod.DoIt([$mod.v]);',
  26950. '$mod.DoAll($mod.v);',
  26951. '']));
  26952. end;
  26953. procedure TTestModule.TestPointer_NonRecordFail;
  26954. begin
  26955. StartProgram(false);
  26956. Add([
  26957. 'type',
  26958. ' p = ^longint;',
  26959. 'begin',
  26960. '']);
  26961. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26962. ConvertProgram;
  26963. end;
  26964. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26965. begin
  26966. StartProgram(false);
  26967. Add([
  26968. 'procedure DoIt(p: ^longint); begin end;',
  26969. 'begin',
  26970. '']);
  26971. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26972. ConvertProgram;
  26973. end;
  26974. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26975. begin
  26976. StartProgram(false);
  26977. Add([
  26978. 'var p: ^longint;',
  26979. 'begin',
  26980. '']);
  26981. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26982. ConvertProgram;
  26983. end;
  26984. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26985. begin
  26986. StartProgram(false);
  26987. Add([
  26988. 'function DoIt: ^longint; begin end;',
  26989. 'begin',
  26990. '']);
  26991. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26992. ConvertProgram;
  26993. end;
  26994. procedure TTestModule.TestPointer_AddrOperatorFail;
  26995. begin
  26996. StartProgram(false);
  26997. Add([
  26998. 'var i: longint;',
  26999. 'begin',
  27000. ' if @i=nil then ;',
  27001. '']);
  27002. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27003. ConvertProgram;
  27004. end;
  27005. procedure TTestModule.TestPointer_ArrayParamsFail;
  27006. begin
  27007. StartProgram(false);
  27008. Add([
  27009. 'var',
  27010. ' p: Pointer;',
  27011. 'begin',
  27012. ' p:=p[1];',
  27013. '']);
  27014. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27015. ConvertProgram;
  27016. end;
  27017. procedure TTestModule.TestPointer_PointerAddFail;
  27018. begin
  27019. StartProgram(false);
  27020. Add([
  27021. 'var',
  27022. ' p: Pointer;',
  27023. 'begin',
  27024. ' p:=p+1;',
  27025. '']);
  27026. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27027. ConvertProgram;
  27028. end;
  27029. procedure TTestModule.TestPointer_IncPointerFail;
  27030. begin
  27031. StartProgram(false);
  27032. Add([
  27033. 'var',
  27034. ' p: Pointer;',
  27035. 'begin',
  27036. ' inc(p,1);',
  27037. '']);
  27038. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27039. nIncompatibleTypeArgNo);
  27040. ConvertProgram;
  27041. end;
  27042. procedure TTestModule.TestPointer_Record;
  27043. begin
  27044. StartProgram(false);
  27045. Add([
  27046. 'type',
  27047. ' TRec = record x: longint; end;',
  27048. ' PRec = ^TRec;',
  27049. 'var',
  27050. ' r: TRec;',
  27051. ' p: PRec;',
  27052. ' q: ^TRec;',
  27053. ' Ptr: pointer;',
  27054. 'begin',
  27055. ' new(p);',
  27056. ' p:=@r;',
  27057. ' r:=p^;',
  27058. ' r.x:=p^.x;',
  27059. ' p^.x:=r.x;',
  27060. ' if p^.x=3 then ;',
  27061. ' if 4=p^.x then ;',
  27062. ' dispose(p);',
  27063. ' new(q);',
  27064. ' dispose(q);',
  27065. ' Ptr:=p;',
  27066. ' p:=PRec(ptr);',
  27067. '']);
  27068. ConvertProgram;
  27069. CheckSource('TestPointer_Record',
  27070. LinesToStr([ // statements
  27071. 'rtl.recNewT(this, "TRec", function () {',
  27072. ' this.x = 0;',
  27073. ' this.$eq = function (b) {',
  27074. ' return this.x === b.x;',
  27075. ' };',
  27076. ' this.$assign = function (s) {',
  27077. ' this.x = s.x;',
  27078. ' return this;',
  27079. ' };',
  27080. '});',
  27081. 'this.r = this.TRec.$new();',
  27082. 'this.p = null;',
  27083. 'this.q = null;',
  27084. 'this.Ptr = null;',
  27085. '']),
  27086. LinesToStr([ // $mod.$main
  27087. '$mod.p = $mod.TRec.$new();',
  27088. '$mod.p = $mod.r;',
  27089. '$mod.r.$assign($mod.p);',
  27090. '$mod.r.x = $mod.p.x;',
  27091. '$mod.p.x = $mod.r.x;',
  27092. 'if ($mod.p.x === 3) ;',
  27093. 'if (4 === $mod.p.x) ;',
  27094. '$mod.p = null;',
  27095. '$mod.q = $mod.TRec.$new();',
  27096. '$mod.q = null;',
  27097. '$mod.Ptr = $mod.p;',
  27098. '$mod.p = $mod.Ptr;',
  27099. '']));
  27100. end;
  27101. procedure TTestModule.TestPointer_RecordArg;
  27102. begin
  27103. StartProgram(false);
  27104. Add([
  27105. '{$modeswitch autoderef}',
  27106. 'type',
  27107. ' TRec = record x: longint; end;',
  27108. ' PRec = ^TRec;',
  27109. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27110. 'begin',
  27111. ' a.x:=a.x;',
  27112. ' a^.x:=a^.x;',
  27113. ' with a^ do',
  27114. ' x:=x;',
  27115. 'end;',
  27116. 'function GetIt(p: PRec): PRec;',
  27117. 'begin',
  27118. ' p.x:=p.x;',
  27119. ' p^.x:=p^.x;',
  27120. ' with p^ do',
  27121. ' x:=x;',
  27122. 'end;',
  27123. 'var',
  27124. ' r: TRec;',
  27125. ' p: PRec;',
  27126. 'begin',
  27127. ' p:=GetIt(p);',
  27128. ' p^:=GetIt(@r)^;',
  27129. ' DoIt(p,p,p);',
  27130. ' DoIt(@r,p,p);',
  27131. '']);
  27132. ConvertProgram;
  27133. CheckSource('TestPointer_RecordArg',
  27134. LinesToStr([ // statements
  27135. 'rtl.recNewT(this, "TRec", function () {',
  27136. ' this.x = 0;',
  27137. ' this.$eq = function (b) {',
  27138. ' return this.x === b.x;',
  27139. ' };',
  27140. ' this.$assign = function (s) {',
  27141. ' this.x = s.x;',
  27142. ' return this;',
  27143. ' };',
  27144. '});',
  27145. 'this.DoIt = function (a, b, c) {',
  27146. ' var Result = $mod.TRec.$new();',
  27147. ' a.x = a.x;',
  27148. ' a.x = a.x;',
  27149. ' a.x = a.x;',
  27150. ' return Result;',
  27151. '};',
  27152. 'this.GetIt = function (p) {',
  27153. ' var Result = null;',
  27154. ' p.x = p.x;',
  27155. ' p.x = p.x;',
  27156. ' p.x = p.x;',
  27157. ' return Result;',
  27158. '};',
  27159. 'this.r = this.TRec.$new();',
  27160. 'this.p = null;',
  27161. '']),
  27162. LinesToStr([ // $mod.$main
  27163. '$mod.p = $mod.GetIt($mod.p);',
  27164. '$mod.p.$assign($mod.GetIt($mod.r));',
  27165. '$mod.DoIt($mod.p, {',
  27166. ' p: $mod,',
  27167. ' get: function () {',
  27168. ' return this.p.p;',
  27169. ' },',
  27170. ' set: function (v) {',
  27171. ' this.p.p = v;',
  27172. ' }',
  27173. '}, {',
  27174. ' p: $mod,',
  27175. ' get: function () {',
  27176. ' return this.p.p;',
  27177. ' },',
  27178. ' set: function (v) {',
  27179. ' this.p.p = v;',
  27180. ' }',
  27181. '});',
  27182. '$mod.DoIt($mod.r, {',
  27183. ' p: $mod,',
  27184. ' get: function () {',
  27185. ' return this.p.p;',
  27186. ' },',
  27187. ' set: function (v) {',
  27188. ' this.p.p = v;',
  27189. ' }',
  27190. '}, {',
  27191. ' p: $mod,',
  27192. ' get: function () {',
  27193. ' return this.p.p;',
  27194. ' },',
  27195. ' set: function (v) {',
  27196. ' this.p.p = v;',
  27197. ' }',
  27198. '});',
  27199. '']));
  27200. end;
  27201. procedure TTestModule.TestJSValue_AssignToJSValue;
  27202. begin
  27203. StartProgram(false);
  27204. Add('var');
  27205. Add(' v: jsvalue;');
  27206. Add(' i: longint;');
  27207. Add(' s: string;');
  27208. Add(' b: boolean;');
  27209. Add(' d: double;');
  27210. Add(' p: pointer;');
  27211. Add('begin');
  27212. Add(' v:=v;');
  27213. Add(' v:=1;');
  27214. Add(' v:=i;');
  27215. Add(' v:='''';');
  27216. Add(' v:=''c'';');
  27217. Add(' v:=''foo'';');
  27218. Add(' v:=s;');
  27219. Add(' v:=false;');
  27220. Add(' v:=true;');
  27221. Add(' v:=b;');
  27222. Add(' v:=0.1;');
  27223. Add(' v:=d;');
  27224. Add(' v:=nil;');
  27225. Add(' v:=p;');
  27226. ConvertProgram;
  27227. CheckSource('TestJSValue_AssignToJSValue',
  27228. LinesToStr([ // statements
  27229. 'this.v = undefined;',
  27230. 'this.i = 0;',
  27231. 'this.s = "";',
  27232. 'this.b = false;',
  27233. 'this.d = 0.0;',
  27234. 'this.p = null;',
  27235. '']),
  27236. LinesToStr([ // $mod.$main
  27237. '$mod.v = $mod.v;',
  27238. '$mod.v = 1;',
  27239. '$mod.v = $mod.i;',
  27240. '$mod.v = "";',
  27241. '$mod.v = "c";',
  27242. '$mod.v = "foo";',
  27243. '$mod.v = $mod.s;',
  27244. '$mod.v = false;',
  27245. '$mod.v = true;',
  27246. '$mod.v = $mod.b;',
  27247. '$mod.v = 0.1;',
  27248. '$mod.v = $mod.d;',
  27249. '$mod.v = null;',
  27250. '$mod.v = $mod.p;',
  27251. '']));
  27252. end;
  27253. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27254. begin
  27255. StartProgram(false);
  27256. Add('type');
  27257. Add(' integer = longint;');
  27258. Add(' TYesNo = boolean;');
  27259. Add(' TFloat = double;');
  27260. Add(' TCaption = string;');
  27261. Add(' TChar = char;');
  27262. Add('var');
  27263. Add(' v: jsvalue;');
  27264. Add(' i: integer;');
  27265. Add(' s: TCaption;');
  27266. Add(' b: TYesNo;');
  27267. Add(' d: TFloat;');
  27268. Add(' c: char;');
  27269. Add('begin');
  27270. Add(' i:=longint(v);');
  27271. Add(' i:=integer(v);');
  27272. Add(' s:=string(v);');
  27273. Add(' s:=TCaption(v);');
  27274. Add(' b:=boolean(v);');
  27275. Add(' b:=TYesNo(v);');
  27276. Add(' d:=double(v);');
  27277. Add(' d:=TFloat(v);');
  27278. Add(' c:=char(v);');
  27279. Add(' c:=TChar(v);');
  27280. ConvertProgram;
  27281. CheckSource('TestJSValue_TypeCastToBaseType',
  27282. LinesToStr([ // statements
  27283. 'this.v = undefined;',
  27284. 'this.i = 0;',
  27285. 'this.s = "";',
  27286. 'this.b = false;',
  27287. 'this.d = 0.0;',
  27288. 'this.c = "";',
  27289. '']),
  27290. LinesToStr([ // $mod.$main
  27291. '$mod.i = rtl.trunc($mod.v);',
  27292. '$mod.i = rtl.trunc($mod.v);',
  27293. '$mod.s = "" + $mod.v;',
  27294. '$mod.s = "" + $mod.v;',
  27295. '$mod.b = !($mod.v == false);',
  27296. '$mod.b = !($mod.v == false);',
  27297. '$mod.d = rtl.getNumber($mod.v);',
  27298. '$mod.d = rtl.getNumber($mod.v);',
  27299. '$mod.c = rtl.getChar($mod.v);',
  27300. '$mod.c = rtl.getChar($mod.v);',
  27301. '']));
  27302. end;
  27303. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27304. begin
  27305. StartProgram(false);
  27306. Add([
  27307. 'type',
  27308. ' TArr = array of word;',
  27309. ' TRec = record end;',
  27310. ' TSet = set of boolean;',
  27311. 'procedure Fly(v: jsvalue);',
  27312. 'begin',
  27313. 'end;',
  27314. 'var',
  27315. ' a: TArr;',
  27316. ' r: TRec;',
  27317. ' s: TSet;',
  27318. 'begin',
  27319. ' Fly(jsvalue(a));',
  27320. ' Fly(jsvalue(r));',
  27321. ' Fly(jsvalue(s));',
  27322. '']);
  27323. ConvertProgram;
  27324. CheckSource('TestJSValue_TypecastToJSValue',
  27325. LinesToStr([ // statements
  27326. 'rtl.recNewT(this, "TRec", function () {',
  27327. ' this.$eq = function (b) {',
  27328. ' return true;',
  27329. ' };',
  27330. ' this.$assign = function (s) {',
  27331. ' return this;',
  27332. ' };',
  27333. '});',
  27334. 'this.Fly = function (v) {',
  27335. '};',
  27336. 'this.a = [];',
  27337. 'this.r = this.TRec.$new();',
  27338. 'this.s = {};',
  27339. '']),
  27340. LinesToStr([ // $mod.$main
  27341. '$mod.Fly($mod.a);',
  27342. '$mod.Fly($mod.r);',
  27343. '$mod.Fly($mod.s);',
  27344. '']));
  27345. end;
  27346. procedure TTestModule.TestJSValue_Equal;
  27347. begin
  27348. StartProgram(false);
  27349. Add('type');
  27350. Add(' integer = longint;');
  27351. Add(' TYesNo = boolean;');
  27352. Add(' TFloat = double;');
  27353. Add(' TCaption = string;');
  27354. Add(' TChar = char;');
  27355. Add(' TMulti = JSValue;');
  27356. Add('var');
  27357. Add(' v: jsvalue;');
  27358. Add(' i: integer;');
  27359. Add(' s: TCaption;');
  27360. Add(' b: TYesNo;');
  27361. Add(' d: TFloat;');
  27362. Add(' c: char;');
  27363. Add(' m: TMulti;');
  27364. Add('begin');
  27365. Add(' b:=v=v;');
  27366. Add(' b:=v<>v;');
  27367. Add(' b:=v=1;');
  27368. Add(' b:=v<>1;');
  27369. Add(' b:=2=v;');
  27370. Add(' b:=2<>v;');
  27371. Add(' b:=v=i;');
  27372. Add(' b:=i=v;');
  27373. Add(' b:=v=nil;');
  27374. Add(' b:=nil=v;');
  27375. Add(' b:=v=false;');
  27376. Add(' b:=true=v;');
  27377. Add(' b:=v=b;');
  27378. Add(' b:=b=v;');
  27379. Add(' b:=v=s;');
  27380. Add(' b:=s=v;');
  27381. Add(' b:=v=''foo'';');
  27382. Add(' b:=''''=v;');
  27383. Add(' b:=v=d;');
  27384. Add(' b:=d=v;');
  27385. Add(' b:=v=3.4;');
  27386. Add(' b:=5.6=v;');
  27387. Add(' b:=v=c;');
  27388. Add(' b:=c=v;');
  27389. Add(' b:=m=m;');
  27390. Add(' b:=v=m;');
  27391. Add(' b:=m=v;');
  27392. ConvertProgram;
  27393. CheckSource('TestJSValue_Equal',
  27394. LinesToStr([ // statements
  27395. 'this.v = undefined;',
  27396. 'this.i = 0;',
  27397. 'this.s = "";',
  27398. 'this.b = false;',
  27399. 'this.d = 0.0;',
  27400. 'this.c = "";',
  27401. 'this.m = undefined;',
  27402. '']),
  27403. LinesToStr([ // $mod.$main
  27404. '$mod.b = $mod.v == $mod.v;',
  27405. '$mod.b = $mod.v != $mod.v;',
  27406. '$mod.b = $mod.v == 1;',
  27407. '$mod.b = $mod.v != 1;',
  27408. '$mod.b = 2 == $mod.v;',
  27409. '$mod.b = 2 != $mod.v;',
  27410. '$mod.b = $mod.v == $mod.i;',
  27411. '$mod.b = $mod.i == $mod.v;',
  27412. '$mod.b = $mod.v == null;',
  27413. '$mod.b = null == $mod.v;',
  27414. '$mod.b = $mod.v == false;',
  27415. '$mod.b = true == $mod.v;',
  27416. '$mod.b = $mod.v == $mod.b;',
  27417. '$mod.b = $mod.b == $mod.v;',
  27418. '$mod.b = $mod.v == $mod.s;',
  27419. '$mod.b = $mod.s == $mod.v;',
  27420. '$mod.b = $mod.v == "foo";',
  27421. '$mod.b = "" == $mod.v;',
  27422. '$mod.b = $mod.v == $mod.d;',
  27423. '$mod.b = $mod.d == $mod.v;',
  27424. '$mod.b = $mod.v == 3.4;',
  27425. '$mod.b = 5.6 == $mod.v;',
  27426. '$mod.b = $mod.v == $mod.c;',
  27427. '$mod.b = $mod.c == $mod.v;',
  27428. '$mod.b = $mod.m == $mod.m;',
  27429. '$mod.b = $mod.v == $mod.m;',
  27430. '$mod.b = $mod.m == $mod.v;',
  27431. '']));
  27432. end;
  27433. procedure TTestModule.TestJSValue_If;
  27434. begin
  27435. StartProgram(false);
  27436. Add([
  27437. 'procedure Fly(var u);',
  27438. 'begin',
  27439. ' if jsvalue(u) then ;',
  27440. 'end;',
  27441. 'var',
  27442. ' v: jsvalue;',
  27443. 'begin',
  27444. ' if v then ;',
  27445. ' while v do ;',
  27446. ' repeat until v;',
  27447. '']);
  27448. ConvertProgram;
  27449. CheckSource('TestJSValue_If',
  27450. LinesToStr([ // statements
  27451. 'this.Fly = function (u) {',
  27452. ' if (u.get()) ;',
  27453. '};',
  27454. 'this.v = undefined;',
  27455. '']),
  27456. LinesToStr([ // $mod.$main
  27457. 'if ($mod.v) ;',
  27458. 'while($mod.v){',
  27459. '};',
  27460. 'do{',
  27461. '} while(!$mod.v);',
  27462. '']));
  27463. end;
  27464. procedure TTestModule.TestJSValue_Not;
  27465. begin
  27466. StartProgram(false);
  27467. Add([
  27468. 'var',
  27469. ' v: jsvalue;',
  27470. ' b: boolean;',
  27471. 'begin',
  27472. ' b:=not v;',
  27473. ' if not v then ;',
  27474. ' while not v do ;',
  27475. ' repeat until not v;',
  27476. '']);
  27477. ConvertProgram;
  27478. CheckSource('TestJSValue_If',
  27479. LinesToStr([ // statements
  27480. 'this.v = undefined;',
  27481. 'this.b = false;',
  27482. '']),
  27483. LinesToStr([ // $mod.$main
  27484. '$mod.b=!$mod.v;',
  27485. 'if (!$mod.v) ;',
  27486. 'while(!$mod.v){',
  27487. '};',
  27488. 'do{',
  27489. '} while($mod.v);',
  27490. '']));
  27491. end;
  27492. procedure TTestModule.TestJSValue_Enum;
  27493. begin
  27494. StartProgram(false);
  27495. Add('type');
  27496. Add(' TColor = (red, blue);');
  27497. Add(' TRedBlue = TColor;');
  27498. Add('var');
  27499. Add(' v: jsvalue;');
  27500. Add(' e: TColor;');
  27501. Add('begin');
  27502. Add(' v:=e;');
  27503. Add(' v:=TColor(e);');
  27504. Add(' v:=TRedBlue(e);');
  27505. Add(' e:=TColor(v);');
  27506. Add(' e:=TRedBlue(v);');
  27507. ConvertProgram;
  27508. CheckSource('TestJSValue_Enum',
  27509. LinesToStr([ // statements
  27510. 'this.TColor = {',
  27511. ' "0": "red",',
  27512. ' red: 0,',
  27513. ' "1": "blue",',
  27514. ' blue: 1',
  27515. '};',
  27516. 'this.v = undefined;',
  27517. 'this.e = 0;',
  27518. '']),
  27519. LinesToStr([ // $mod.$main
  27520. '$mod.v = $mod.e;',
  27521. '$mod.v = $mod.e;',
  27522. '$mod.v = $mod.e;',
  27523. '$mod.e = $mod.v;',
  27524. '$mod.e = $mod.v;',
  27525. '']));
  27526. end;
  27527. procedure TTestModule.TestJSValue_ClassInstance;
  27528. begin
  27529. StartProgram(false);
  27530. Add([
  27531. 'type',
  27532. ' TObject = class',
  27533. ' end;',
  27534. ' TBirdObject = TObject;',
  27535. 'var',
  27536. ' v: jsvalue;',
  27537. ' o: TObject;',
  27538. 'begin',
  27539. ' v:=o;',
  27540. ' v:=TObject(o);',
  27541. ' v:=TBirdObject(o);',
  27542. ' o:=TObject(v);',
  27543. ' o:=TBirdObject(v);',
  27544. ' if v is TObject then ;',
  27545. '']);
  27546. ConvertProgram;
  27547. CheckSource('TestJSValue_ClassInstance',
  27548. LinesToStr([ // statements
  27549. 'rtl.createClass(this, "TObject", null, function () {',
  27550. ' this.$init = function () {',
  27551. ' };',
  27552. ' this.$final = function () {',
  27553. ' };',
  27554. '});',
  27555. 'this.v = undefined;',
  27556. 'this.o = null;',
  27557. '']),
  27558. LinesToStr([ // $mod.$main
  27559. '$mod.v = $mod.o;',
  27560. '$mod.v = $mod.o;',
  27561. '$mod.v = $mod.o;',
  27562. '$mod.o = rtl.getObject($mod.v);',
  27563. '$mod.o = rtl.getObject($mod.v);',
  27564. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27565. '']));
  27566. end;
  27567. procedure TTestModule.TestJSValue_ClassOf;
  27568. begin
  27569. StartProgram(false);
  27570. Add([
  27571. 'type',
  27572. ' TClass = class of TObject;',
  27573. ' TObject = class',
  27574. ' end;',
  27575. ' TBirds = class of TBird;',
  27576. ' TBird = class(TObject) end;',
  27577. 'var',
  27578. ' v: jsvalue;',
  27579. ' c: TClass;',
  27580. 'begin',
  27581. ' v:=c;',
  27582. ' v:=TObject;',
  27583. ' v:=TClass(c);',
  27584. ' v:=TBirds(c);',
  27585. ' c:=TClass(v);',
  27586. ' c:=TBirds(v);',
  27587. ' if v is TClass then ;',
  27588. '']);
  27589. ConvertProgram;
  27590. CheckSource('TestJSValue_ClassOf',
  27591. LinesToStr([ // statements
  27592. 'rtl.createClass(this, "TObject", null, function () {',
  27593. ' this.$init = function () {',
  27594. ' };',
  27595. ' this.$final = function () {',
  27596. ' };',
  27597. '});',
  27598. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27599. '});',
  27600. 'this.v = undefined;',
  27601. 'this.c = null;',
  27602. '']),
  27603. LinesToStr([ // $mod.$main
  27604. '$mod.v = $mod.c;',
  27605. '$mod.v = $mod.TObject;',
  27606. '$mod.v = $mod.c;',
  27607. '$mod.v = $mod.c;',
  27608. '$mod.c = rtl.getObject($mod.v);',
  27609. '$mod.c = rtl.getObject($mod.v);',
  27610. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27611. '']));
  27612. end;
  27613. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27614. begin
  27615. StartProgram(false);
  27616. Add([
  27617. 'type',
  27618. ' integer = longint;',
  27619. ' TArray = array of JSValue;',
  27620. ' TArrgh = tarray;',
  27621. ' TArrInt = array of integer;',
  27622. 'var',
  27623. ' v: jsvalue;',
  27624. ' TheArray: tarray = (1,''2'');',
  27625. ' Arr: tarrgh;',
  27626. ' i: integer;',
  27627. ' ArrInt: tarrint;',
  27628. 'begin',
  27629. ' arr:=thearray;',
  27630. ' thearray:=arr;',
  27631. ' setlength(arr,2);',
  27632. ' setlength(thearray,3);',
  27633. ' arr[4]:=v;',
  27634. ' arr[5]:=length(thearray);',
  27635. ' arr[6]:=nil;',
  27636. ' arr[7]:=thearray[8];',
  27637. ' arr[low(arr)]:=high(thearray);',
  27638. ' arr:=arrint;',
  27639. ' arrInt:=tarrint(arr);',
  27640. ' if TheArray = nil then ;',
  27641. ' if nil = TheArray then ;',
  27642. ' if TheArray <> nil then ;',
  27643. ' if nil <> TheArray then ;',
  27644. '']);
  27645. ConvertProgram;
  27646. CheckSource('TestJSValue_ArrayOfJSValue',
  27647. LinesToStr([ // statements
  27648. 'this.v = undefined;',
  27649. 'this.TheArray = [1, "2"];',
  27650. 'this.Arr = [];',
  27651. 'this.i = 0;',
  27652. 'this.ArrInt = [];',
  27653. '']),
  27654. LinesToStr([ // $mod.$main
  27655. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27656. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27657. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27658. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27659. '$mod.Arr[4] = $mod.v;',
  27660. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27661. '$mod.Arr[6] = null;',
  27662. '$mod.Arr[7] = $mod.TheArray[8];',
  27663. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27664. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27665. '$mod.ArrInt = $mod.Arr;',
  27666. 'if (rtl.length($mod.TheArray) === 0) ;',
  27667. 'if (rtl.length($mod.TheArray) === 0) ;',
  27668. 'if (rtl.length($mod.TheArray) > 0) ;',
  27669. 'if (rtl.length($mod.TheArray) > 0) ;',
  27670. '']));
  27671. end;
  27672. procedure TTestModule.TestJSValue_ArrayLit;
  27673. begin
  27674. StartProgram(false);
  27675. Add([
  27676. 'type',
  27677. ' TFlag = (big,small);',
  27678. ' TArray = array of JSValue;',
  27679. ' TObject = class end;',
  27680. ' TClass = class of TObject;',
  27681. 'var',
  27682. ' v: jsvalue;',
  27683. ' a: TArray;',
  27684. ' o: TObject;',
  27685. 'begin',
  27686. ' a:=[];',
  27687. ' a:=[1];',
  27688. ' a:=[1,2];',
  27689. ' a:=[big];',
  27690. ' a:=[1,big];',
  27691. ' a:=[o,nil];',
  27692. '']);
  27693. ConvertProgram;
  27694. CheckSource('TestJSValue_ArrayLit',
  27695. LinesToStr([ // statements
  27696. 'this.TFlag = {',
  27697. ' "0": "big",',
  27698. ' big: 0,',
  27699. ' "1": "small",',
  27700. ' small: 1',
  27701. '};',
  27702. 'rtl.createClass(this, "TObject", null, function () {',
  27703. ' this.$init = function () {',
  27704. ' };',
  27705. ' this.$final = function () {',
  27706. ' };',
  27707. '});',
  27708. 'this.v = undefined;',
  27709. 'this.a = [];',
  27710. 'this.o = null;',
  27711. '']),
  27712. LinesToStr([ // $mod.$main
  27713. '$mod.a = [];',
  27714. '$mod.a = [1];',
  27715. '$mod.a = [1, 2];',
  27716. '$mod.a = [$mod.TFlag.big];',
  27717. '$mod.a = [1, $mod.TFlag.big];',
  27718. '$mod.a = [$mod.o, null];',
  27719. '']));
  27720. end;
  27721. procedure TTestModule.TestJSValue_Params;
  27722. begin
  27723. StartProgram(false);
  27724. Add('type');
  27725. Add(' integer = longint;');
  27726. Add(' TYesNo = boolean;');
  27727. Add(' TFloat = double;');
  27728. Add(' TCaption = string;');
  27729. Add(' TChar = char;');
  27730. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27731. Add('var');
  27732. Add(' l: jsvalue;');
  27733. Add('begin');
  27734. Add(' a:=a;');
  27735. Add(' l:=b;');
  27736. Add(' c:=c;');
  27737. Add(' d:=d;');
  27738. Add(' Result:=l;');
  27739. Add('end;');
  27740. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27741. Add('var');
  27742. Add(' v: jsvalue;');
  27743. Add(' i: integer;');
  27744. Add(' b: TYesNo;');
  27745. Add(' d: TFloat;');
  27746. Add(' s: TCaption;');
  27747. Add(' c: TChar;');
  27748. Add('begin');
  27749. Add(' v:=doit(v,v,v,v);');
  27750. Add(' i:=integer(dosome(i,i));');
  27751. Add(' b:=TYesNo(dosome(b,b));');
  27752. Add(' d:=TFloat(dosome(d,d));');
  27753. Add(' s:=TCaption(dosome(s,s));');
  27754. Add(' c:=TChar(dosome(c,c));');
  27755. ConvertProgram;
  27756. CheckSource('TestJSValue_Params',
  27757. LinesToStr([ // statements
  27758. 'this.DoIt = function (a, b, c, d) {',
  27759. ' var Result = undefined;',
  27760. ' var l = undefined;',
  27761. ' a = a;',
  27762. ' l = b;',
  27763. ' c.set(c.get());',
  27764. ' d.set(d.get());',
  27765. ' Result = l;',
  27766. ' return Result;',
  27767. '};',
  27768. 'this.DoSome = function (a, b) {',
  27769. ' var Result = undefined;',
  27770. ' return Result;',
  27771. '};',
  27772. 'this.v = undefined;',
  27773. 'this.i = 0;',
  27774. 'this.b = false;',
  27775. 'this.d = 0.0;',
  27776. 'this.s = "";',
  27777. 'this.c = "";',
  27778. '']),
  27779. LinesToStr([ // $mod.$main
  27780. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27781. ' p: $mod,',
  27782. ' get: function () {',
  27783. ' return this.p.v;',
  27784. ' },',
  27785. ' set: function (v) {',
  27786. ' this.p.v = v;',
  27787. ' }',
  27788. '}, {',
  27789. ' p: $mod,',
  27790. ' get: function () {',
  27791. ' return this.p.v;',
  27792. ' },',
  27793. ' set: function (v) {',
  27794. ' this.p.v = v;',
  27795. ' }',
  27796. '});',
  27797. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27798. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27799. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27800. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27801. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27802. '']));
  27803. end;
  27804. procedure TTestModule.TestJSValue_UntypedParam;
  27805. begin
  27806. StartProgram(false);
  27807. Add('function DoIt(const a; var b; out c): jsvalue;');
  27808. Add('begin');
  27809. Add(' Result:=a;');
  27810. Add(' Result:=b;');
  27811. Add(' Result:=c;');
  27812. Add(' b:=Result;');
  27813. Add(' c:=Result;');
  27814. Add('end;');
  27815. Add('var i: longint;');
  27816. Add('begin');
  27817. Add(' doit(i,i,i);');
  27818. ConvertProgram;
  27819. CheckSource('TestJSValue_UntypedParam',
  27820. LinesToStr([ // statements
  27821. 'this.DoIt = function (a, b, c) {',
  27822. ' var Result = undefined;',
  27823. ' Result = a;',
  27824. ' Result = b.get();',
  27825. ' Result = c.get();',
  27826. ' b.set(Result);',
  27827. ' c.set(Result);',
  27828. ' return Result;',
  27829. '};',
  27830. 'this.i = 0;',
  27831. '']),
  27832. LinesToStr([ // $mod.$main
  27833. '$mod.DoIt($mod.i, {',
  27834. ' p: $mod,',
  27835. ' get: function () {',
  27836. ' return this.p.i;',
  27837. ' },',
  27838. ' set: function (v) {',
  27839. ' this.p.i = v;',
  27840. ' }',
  27841. '}, {',
  27842. ' p: $mod,',
  27843. ' get: function () {',
  27844. ' return this.p.i;',
  27845. ' },',
  27846. ' set: function (v) {',
  27847. ' this.p.i = v;',
  27848. ' }',
  27849. '});',
  27850. '']));
  27851. end;
  27852. procedure TTestModule.TestJSValue_FuncResultType;
  27853. begin
  27854. StartProgram(false);
  27855. Add('type');
  27856. Add(' integer = longint;');
  27857. Add(' TJSValueArray = array of JSValue;');
  27858. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27859. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27860. Add('begin');
  27861. Add(' while Compare(P,aList[0])>0 do ;');
  27862. Add('end;');
  27863. Add('var');
  27864. Add(' Compare: TListSortCompare;');
  27865. Add(' V: JSValue;');
  27866. Add(' i: integer;');
  27867. Add('begin');
  27868. Add(' if Compare(V,V)>0 then ;');
  27869. Add(' if Compare(i,i)>1 then ;');
  27870. Add(' if Compare(nil,false)>2 then ;');
  27871. Add(' if Compare(1,true)>3 then ;');
  27872. ConvertProgram;
  27873. CheckSource('TestJSValue_UntypedParam',
  27874. LinesToStr([ // statements
  27875. 'this.Sort = function (P, aList, Compare) {',
  27876. ' while (Compare(P, aList[0]) > 0) {',
  27877. ' };',
  27878. '};',
  27879. 'this.Compare = null;',
  27880. 'this.V = undefined;',
  27881. 'this.i = 0;',
  27882. '']),
  27883. LinesToStr([ // $mod.$main
  27884. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27885. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27886. 'if ($mod.Compare(null, false) > 2) ;',
  27887. 'if ($mod.Compare(1, true) > 3) ;',
  27888. '']));
  27889. end;
  27890. procedure TTestModule.TestJSValue_ProcType_Assign;
  27891. begin
  27892. StartProgram(false);
  27893. Add('type');
  27894. Add(' integer = longint;');
  27895. Add(' TObject = class');
  27896. Add(' class function GetGlob: integer;');
  27897. Add(' function Getter: integer;');
  27898. Add(' end;');
  27899. Add('class function TObject.GetGlob: integer;');
  27900. Add('var v1: jsvalue;');
  27901. Add('begin');
  27902. Add(' v1:=@GetGlob;');
  27903. Add(' v1:[email protected];');
  27904. Add('end;');
  27905. Add('function TObject.Getter: integer;');
  27906. Add('var v2: jsvalue;');
  27907. Add('begin');
  27908. Add(' v2:=@Getter;');
  27909. Add(' v2:[email protected];');
  27910. Add(' v2:=@GetGlob;');
  27911. Add(' v2:[email protected];');
  27912. Add('end;');
  27913. Add('function GetIt(i: integer): integer;');
  27914. Add('var v3: jsvalue;');
  27915. Add('begin');
  27916. Add(' v3:=@GetIt;');
  27917. Add('end;');
  27918. Add('var');
  27919. Add(' V: JSValue;');
  27920. Add(' o: TObject;');
  27921. Add('begin');
  27922. Add(' v:=@GetIt;');
  27923. Add(' v:[email protected];');
  27924. Add(' v:[email protected];');
  27925. ConvertProgram;
  27926. CheckSource('TestJSValue_ProcType_Assign',
  27927. LinesToStr([ // statements
  27928. 'rtl.createClass(this, "TObject", null, function () {',
  27929. ' this.$init = function () {',
  27930. ' };',
  27931. ' this.$final = function () {',
  27932. ' };',
  27933. ' this.GetGlob = function () {',
  27934. ' var Result = 0;',
  27935. ' var v1 = undefined;',
  27936. ' v1 = rtl.createCallback(this, "GetGlob");',
  27937. ' v1 = rtl.createCallback(this, "GetGlob");',
  27938. ' return Result;',
  27939. ' };',
  27940. ' this.Getter = function () {',
  27941. ' var Result = 0;',
  27942. ' var v2 = undefined;',
  27943. ' v2 = rtl.createCallback(this, "Getter");',
  27944. ' v2 = rtl.createCallback(this, "Getter");',
  27945. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27946. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27947. ' return Result;',
  27948. ' };',
  27949. '});',
  27950. 'this.GetIt = function (i) {',
  27951. ' var Result = 0;',
  27952. ' var v3 = undefined;',
  27953. ' v3 = $mod.GetIt;',
  27954. ' return Result;',
  27955. '};',
  27956. 'this.V = undefined;',
  27957. 'this.o = null;',
  27958. '']),
  27959. LinesToStr([ // $mod.$main
  27960. '$mod.V = $mod.GetIt;',
  27961. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27962. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27963. '']));
  27964. end;
  27965. procedure TTestModule.TestJSValue_ProcType_Equal;
  27966. begin
  27967. StartProgram(false);
  27968. Add('type');
  27969. Add(' integer = longint;');
  27970. Add(' TObject = class');
  27971. Add(' class function GetGlob: integer;');
  27972. Add(' function Getter: integer;');
  27973. Add(' end;');
  27974. Add('class function TObject.GetGlob: integer;');
  27975. Add('var v1: jsvalue;');
  27976. Add('begin');
  27977. Add(' if v1=@GetGlob then;');
  27978. Add(' if [email protected] then ;');
  27979. Add('end;');
  27980. Add('function TObject.Getter: integer;');
  27981. Add('var v2: jsvalue;');
  27982. Add('begin');
  27983. Add(' if v2=@Getter then;');
  27984. Add(' if [email protected] then ;');
  27985. Add(' if v2=@GetGlob then;');
  27986. Add(' if [email protected] then;');
  27987. Add('end;');
  27988. Add('function GetIt(i: integer): integer;');
  27989. Add('var v3: jsvalue;');
  27990. Add('begin');
  27991. Add(' if v3=@GetIt then;');
  27992. Add('end;');
  27993. Add('var');
  27994. Add(' V: JSValue;');
  27995. Add(' o: TObject;');
  27996. Add('begin');
  27997. Add(' if v=@GetIt then;');
  27998. Add(' if [email protected] then;');
  27999. Add(' if [email protected] then;');
  28000. Add(' if @GetIt=v then;');
  28001. Add(' if @o.Getter=v then;');
  28002. Add(' if @o.GetGlob=v then;');
  28003. ConvertProgram;
  28004. CheckSource('TestJSValue_ProcType_Equal',
  28005. LinesToStr([ // statements
  28006. 'rtl.createClass(this, "TObject", null, function () {',
  28007. ' this.$init = function () {',
  28008. ' };',
  28009. ' this.$final = function () {',
  28010. ' };',
  28011. ' this.GetGlob = function () {',
  28012. ' var Result = 0;',
  28013. ' var v1 = undefined;',
  28014. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28015. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28016. ' return Result;',
  28017. ' };',
  28018. ' this.Getter = function () {',
  28019. ' var Result = 0;',
  28020. ' var v2 = undefined;',
  28021. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28022. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28023. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28024. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28025. ' return Result;',
  28026. ' };',
  28027. '});',
  28028. 'this.GetIt = function (i) {',
  28029. ' var Result = 0;',
  28030. ' var v3 = undefined;',
  28031. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28032. ' return Result;',
  28033. '};',
  28034. 'this.V = undefined;',
  28035. 'this.o = null;',
  28036. '']),
  28037. LinesToStr([ // $mod.$main
  28038. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28039. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28040. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28041. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28042. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28043. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28044. '']));
  28045. end;
  28046. procedure TTestModule.TestJSValue_ProcType_Param;
  28047. begin
  28048. StartProgram(false);
  28049. Add([
  28050. 'type',
  28051. ' variant = jsvalue;',
  28052. ' TArrVariant = array of variant;',
  28053. ' TArrVar2 = TArrVariant;',
  28054. ' TFuncInt = function: longint;',
  28055. 'function GetIt: longint;',
  28056. 'begin',
  28057. 'end;',
  28058. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28059. 'var v: variant;',
  28060. 'begin',
  28061. ' v:=arr[1];',
  28062. 'end;',
  28063. 'var s: string;',
  28064. 'begin',
  28065. ' DoIt(GetIt,[]);',
  28066. ' DoIt(@GetIt,[]);',
  28067. ' DoIt(1,[s,GetIt]);',
  28068. ' DoIt(1,[s,@GetIt]);',
  28069. '']);
  28070. ConvertProgram;
  28071. CheckSource('TestJSValue_ProcType_Param',
  28072. LinesToStr([ // statements
  28073. 'this.GetIt = function () {',
  28074. ' var Result = 0;',
  28075. ' return Result;',
  28076. '};',
  28077. 'this.DoIt = function (p, Arr) {',
  28078. ' var v = undefined;',
  28079. ' v = Arr[1];',
  28080. '};',
  28081. 'this.s = "";',
  28082. '']),
  28083. LinesToStr([ // $mod.$main
  28084. '$mod.DoIt($mod.GetIt(), []);',
  28085. '$mod.DoIt($mod.GetIt, []);',
  28086. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28087. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28088. '']));
  28089. end;
  28090. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28091. begin
  28092. StartProgram(false);
  28093. Add([
  28094. 'var',
  28095. ' v: JSValue;',
  28096. ' p: Pointer;',
  28097. 'begin',
  28098. ' p:=v;',
  28099. '']);
  28100. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28101. nIncompatibleTypesGotExpected);
  28102. ConvertProgram;
  28103. end;
  28104. procedure TTestModule.TestJSValue_OverloadDouble;
  28105. begin
  28106. StartProgram(false);
  28107. Add([
  28108. 'type',
  28109. ' integer = longint;',
  28110. ' tdatetime = double;',
  28111. 'procedure DoIt(d: double); begin end;',
  28112. 'procedure DoIt(v: jsvalue); begin end;',
  28113. 'var',
  28114. ' d: double;',
  28115. ' dt: tdatetime;',
  28116. ' i: integer;',
  28117. ' b: byte;',
  28118. ' shi: shortint;',
  28119. ' w: word;',
  28120. ' smi: smallint;',
  28121. ' lw: longword;',
  28122. ' li: longint;',
  28123. ' ni: nativeint;',
  28124. ' nu: nativeuint;',
  28125. 'begin',
  28126. ' DoIt(d);',
  28127. ' DoIt(dt);',
  28128. ' DoIt(i);',
  28129. ' DoIt(b);',
  28130. ' DoIt(shi);',
  28131. ' DoIt(w);',
  28132. ' DoIt(smi);',
  28133. ' DoIt(lw);',
  28134. ' DoIt(li);',
  28135. ' DoIt(ni);',
  28136. ' DoIt(nu);',
  28137. '']);
  28138. ConvertProgram;
  28139. CheckSource('TestJSValue_OverloadDouble',
  28140. LinesToStr([ // statements
  28141. 'this.DoIt = function (d) {',
  28142. '};',
  28143. 'this.DoIt$1 = function (v) {',
  28144. '};',
  28145. 'this.d = 0.0;',
  28146. 'this.dt = 0.0;',
  28147. 'this.i = 0;',
  28148. 'this.b = 0;',
  28149. 'this.shi = 0;',
  28150. 'this.w = 0;',
  28151. 'this.smi = 0;',
  28152. 'this.lw = 0;',
  28153. 'this.li = 0;',
  28154. 'this.ni = 0;',
  28155. 'this.nu = 0;',
  28156. '']),
  28157. LinesToStr([ // $mod.$main
  28158. '$mod.DoIt($mod.d);',
  28159. '$mod.DoIt($mod.dt);',
  28160. '$mod.DoIt$1($mod.i);',
  28161. '$mod.DoIt$1($mod.b);',
  28162. '$mod.DoIt$1($mod.shi);',
  28163. '$mod.DoIt$1($mod.w);',
  28164. '$mod.DoIt$1($mod.smi);',
  28165. '$mod.DoIt$1($mod.lw);',
  28166. '$mod.DoIt$1($mod.li);',
  28167. '$mod.DoIt$1($mod.ni);',
  28168. '$mod.DoIt$1($mod.nu);',
  28169. '']));
  28170. end;
  28171. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28172. begin
  28173. StartProgram(false);
  28174. Add([
  28175. 'type',
  28176. ' integer = longint;',
  28177. ' int53 = nativeint;',
  28178. ' tdatetime = double;',
  28179. 'procedure DoIt(n: nativeint); begin end;',
  28180. 'procedure DoIt(v: jsvalue); begin end;',
  28181. 'var',
  28182. ' d: double;',
  28183. ' dt: tdatetime;',
  28184. ' i: integer;',
  28185. ' b: byte;',
  28186. ' shi: shortint;',
  28187. ' w: word;',
  28188. ' smi: smallint;',
  28189. ' lw: longword;',
  28190. ' li: longint;',
  28191. ' ni: nativeint;',
  28192. ' nu: nativeuint;',
  28193. 'begin',
  28194. ' DoIt(d);',
  28195. ' DoIt(dt);',
  28196. ' DoIt(i);',
  28197. ' DoIt(b);',
  28198. ' DoIt(shi);',
  28199. ' DoIt(w);',
  28200. ' DoIt(smi);',
  28201. ' DoIt(lw);',
  28202. ' DoIt(li);',
  28203. ' DoIt(ni);',
  28204. ' DoIt(nu);',
  28205. '']);
  28206. ConvertProgram;
  28207. CheckSource('TestJSValue_OverloadNativeInt',
  28208. LinesToStr([ // statements
  28209. 'this.DoIt = function (n) {',
  28210. '};',
  28211. 'this.DoIt$1 = function (v) {',
  28212. '};',
  28213. 'this.d = 0.0;',
  28214. 'this.dt = 0.0;',
  28215. 'this.i = 0;',
  28216. 'this.b = 0;',
  28217. 'this.shi = 0;',
  28218. 'this.w = 0;',
  28219. 'this.smi = 0;',
  28220. 'this.lw = 0;',
  28221. 'this.li = 0;',
  28222. 'this.ni = 0;',
  28223. 'this.nu = 0;',
  28224. '']),
  28225. LinesToStr([ // $mod.$main
  28226. '$mod.DoIt$1($mod.d);',
  28227. '$mod.DoIt$1($mod.dt);',
  28228. '$mod.DoIt($mod.i);',
  28229. '$mod.DoIt($mod.b);',
  28230. '$mod.DoIt($mod.shi);',
  28231. '$mod.DoIt($mod.w);',
  28232. '$mod.DoIt($mod.smi);',
  28233. '$mod.DoIt($mod.lw);',
  28234. '$mod.DoIt($mod.li);',
  28235. '$mod.DoIt($mod.ni);',
  28236. '$mod.DoIt($mod.nu);',
  28237. '']));
  28238. end;
  28239. procedure TTestModule.TestJSValue_OverloadWord;
  28240. begin
  28241. StartProgram(false);
  28242. Add([
  28243. 'type',
  28244. ' integer = longint;',
  28245. ' int53 = nativeint;',
  28246. ' tdatetime = double;',
  28247. 'procedure DoIt(w: word); begin end;',
  28248. 'procedure DoIt(v: jsvalue); begin end;',
  28249. 'var',
  28250. ' d: double;',
  28251. ' dt: tdatetime;',
  28252. ' i: integer;',
  28253. ' b: byte;',
  28254. ' shi: shortint;',
  28255. ' w: word;',
  28256. ' smi: smallint;',
  28257. ' lw: longword;',
  28258. ' li: longint;',
  28259. ' ni: nativeint;',
  28260. ' nu: nativeuint;',
  28261. 'begin',
  28262. ' DoIt(d);',
  28263. ' DoIt(dt);',
  28264. ' DoIt(i);',
  28265. ' DoIt(b);',
  28266. ' DoIt(shi);',
  28267. ' DoIt(w);',
  28268. ' DoIt(smi);',
  28269. ' DoIt(lw);',
  28270. ' DoIt(li);',
  28271. ' DoIt(ni);',
  28272. ' DoIt(nu);',
  28273. '']);
  28274. ConvertProgram;
  28275. CheckSource('TestJSValue_OverloadWord',
  28276. LinesToStr([ // statements
  28277. 'this.DoIt = function (w) {',
  28278. '};',
  28279. 'this.DoIt$1 = function (v) {',
  28280. '};',
  28281. 'this.d = 0.0;',
  28282. 'this.dt = 0.0;',
  28283. 'this.i = 0;',
  28284. 'this.b = 0;',
  28285. 'this.shi = 0;',
  28286. 'this.w = 0;',
  28287. 'this.smi = 0;',
  28288. 'this.lw = 0;',
  28289. 'this.li = 0;',
  28290. 'this.ni = 0;',
  28291. 'this.nu = 0;',
  28292. '']),
  28293. LinesToStr([ // $mod.$main
  28294. '$mod.DoIt$1($mod.d);',
  28295. '$mod.DoIt$1($mod.dt);',
  28296. '$mod.DoIt$1($mod.i);',
  28297. '$mod.DoIt($mod.b);',
  28298. '$mod.DoIt($mod.shi);',
  28299. '$mod.DoIt($mod.w);',
  28300. '$mod.DoIt$1($mod.smi);',
  28301. '$mod.DoIt$1($mod.lw);',
  28302. '$mod.DoIt$1($mod.li);',
  28303. '$mod.DoIt$1($mod.ni);',
  28304. '$mod.DoIt$1($mod.nu);',
  28305. '']));
  28306. end;
  28307. procedure TTestModule.TestJSValue_OverloadString;
  28308. begin
  28309. StartProgram(false);
  28310. Add([
  28311. 'type',
  28312. ' uni = string;',
  28313. ' WChar = char;',
  28314. 'procedure DoIt(s: string); begin end;',
  28315. 'procedure DoIt(v: jsvalue); begin end;',
  28316. 'var',
  28317. ' s: string;',
  28318. ' c: char;',
  28319. ' u: uni;',
  28320. 'begin',
  28321. ' DoIt(s);',
  28322. ' DoIt(c);',
  28323. ' DoIt(u);',
  28324. '']);
  28325. ConvertProgram;
  28326. CheckSource('TestJSValue_OverloadString',
  28327. LinesToStr([ // statements
  28328. 'this.DoIt = function (s) {',
  28329. '};',
  28330. 'this.DoIt$1 = function (v) {',
  28331. '};',
  28332. 'this.s = "";',
  28333. 'this.c = "";',
  28334. 'this.u = "";',
  28335. '']),
  28336. LinesToStr([ // $mod.$main
  28337. '$mod.DoIt($mod.s);',
  28338. '$mod.DoIt($mod.c);',
  28339. '$mod.DoIt($mod.u);',
  28340. '']));
  28341. end;
  28342. procedure TTestModule.TestJSValue_OverloadChar;
  28343. begin
  28344. StartProgram(false);
  28345. Add([
  28346. 'type',
  28347. ' uni = string;',
  28348. ' WChar = char;',
  28349. 'procedure DoIt(c: char); begin end;',
  28350. 'procedure DoIt(v: jsvalue); begin end;',
  28351. 'var',
  28352. ' s: string;',
  28353. ' c: char;',
  28354. ' u: uni;',
  28355. 'begin',
  28356. ' DoIt(s);',
  28357. ' DoIt(c);',
  28358. ' DoIt(u);',
  28359. '']);
  28360. ConvertProgram;
  28361. CheckSource('TestJSValue_OverloadChar',
  28362. LinesToStr([ // statements
  28363. 'this.DoIt = function (c) {',
  28364. '};',
  28365. 'this.DoIt$1 = function (v) {',
  28366. '};',
  28367. 'this.s = "";',
  28368. 'this.c = "";',
  28369. 'this.u = "";',
  28370. '']),
  28371. LinesToStr([ // $mod.$main
  28372. '$mod.DoIt$1($mod.s);',
  28373. '$mod.DoIt($mod.c);',
  28374. '$mod.DoIt$1($mod.u);',
  28375. '']));
  28376. end;
  28377. procedure TTestModule.TestJSValue_OverloadPointer;
  28378. begin
  28379. StartProgram(false);
  28380. Add([
  28381. 'type',
  28382. ' TObject = class end;',
  28383. 'procedure DoIt(p: pointer); begin end;',
  28384. 'procedure DoIt(v: jsvalue); begin end;',
  28385. 'var',
  28386. ' o: TObject;',
  28387. 'begin',
  28388. ' DoIt(o);',
  28389. '']);
  28390. ConvertProgram;
  28391. CheckSource('TestJSValue_OverloadPointer',
  28392. LinesToStr([ // statements
  28393. 'rtl.createClass(this, "TObject", null, function () {',
  28394. ' this.$init = function () {',
  28395. ' };',
  28396. ' this.$final = function () {',
  28397. ' };',
  28398. '});',
  28399. 'this.DoIt = function (p) {',
  28400. '};',
  28401. 'this.DoIt$1 = function (v) {',
  28402. '};',
  28403. 'this.o = null;',
  28404. '']),
  28405. LinesToStr([ // $mod.$main
  28406. '$mod.DoIt($mod.o);',
  28407. '']));
  28408. end;
  28409. procedure TTestModule.TestJSValue_ForIn;
  28410. begin
  28411. StartProgram(false);
  28412. Add([
  28413. 'var',
  28414. ' v: JSValue;',
  28415. ' key: string;',
  28416. 'begin',
  28417. ' for key in v do begin',
  28418. ' if key=''abc'' then ;',
  28419. ' end;',
  28420. '']);
  28421. ConvertProgram;
  28422. CheckSource('TestJSValue_ForIn',
  28423. LinesToStr([ // statements
  28424. 'this.v = undefined;',
  28425. 'this.key = "";',
  28426. '']),
  28427. LinesToStr([ // $mod.$main
  28428. 'for ($mod.key in $mod.v) {',
  28429. ' if ($mod.key === "abc") ;',
  28430. '};',
  28431. '']));
  28432. end;
  28433. procedure TTestModule.TestRTTI_IntRange;
  28434. begin
  28435. WithTypeInfo:=true;
  28436. StartProgram(true,[supTypeInfo]);
  28437. Add([
  28438. '{$modeswitch externalclass}',
  28439. 'type',
  28440. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28441. ' TColor = type TGraphicsColor;',
  28442. 'var',
  28443. ' p: TTypeInfo;',
  28444. ' k: TTypeKind;',
  28445. 'begin',
  28446. ' p:=typeinfo(TGraphicsColor);',
  28447. ' p:=typeinfo(TColor);',
  28448. ' k:=GetTypeKind(TGraphicsColor);',
  28449. ' k:=GetTypeKind(TColor);',
  28450. '']);
  28451. ConvertProgram;
  28452. CheckSource('TestRTTI_IntRange',
  28453. LinesToStr([ // statements
  28454. 'this.$rtti.$Int("TGraphicsColor", {',
  28455. ' minvalue: -2147483648,',
  28456. ' maxvalue: 2147483647,',
  28457. ' ordtype: 4',
  28458. '});',
  28459. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28460. 'this.p = null;',
  28461. 'this.k = 0;',
  28462. '']),
  28463. LinesToStr([ // $mod.$main
  28464. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28465. '$mod.p = $mod.$rtti["TColor"];',
  28466. '$mod.k = 1;',
  28467. '$mod.k = 1;',
  28468. '']));
  28469. end;
  28470. procedure TTestModule.TestRTTI_Double;
  28471. begin
  28472. WithTypeInfo:=true;
  28473. StartProgram(true,[supTypeInfo]);
  28474. Add([
  28475. '{$modeswitch externalclass}',
  28476. 'type',
  28477. ' TFloat = type double;',
  28478. 'var',
  28479. ' p: TTypeInfo;',
  28480. 'begin',
  28481. ' p:=typeinfo(double);',
  28482. ' p:=typeinfo(TFloat);',
  28483. '']);
  28484. ConvertProgram;
  28485. CheckSource('TestRTTI_Double',
  28486. LinesToStr([ // statements
  28487. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28488. 'this.p = null;',
  28489. '']),
  28490. LinesToStr([ // $mod.$main
  28491. '$mod.p = rtl.double;',
  28492. '$mod.p = $mod.$rtti["TFloat"];',
  28493. '']));
  28494. end;
  28495. procedure TTestModule.TestRTTI_ProcType;
  28496. begin
  28497. WithTypeInfo:=true;
  28498. StartProgram(false);
  28499. Add('type');
  28500. Add(' TProcA = procedure;');
  28501. Add(' TMethodB = procedure of object;');
  28502. Add(' TProcC = procedure; varargs;');
  28503. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28504. Add(' TProcE = function: nativeint;');
  28505. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28506. Add('var p: pointer;');
  28507. Add('begin');
  28508. Add(' p:=typeinfo(tproca);');
  28509. ConvertProgram;
  28510. CheckSource('TestRTTI_ProcType',
  28511. LinesToStr([ // statements
  28512. 'this.$rtti.$ProcVar("TProcA", {',
  28513. ' procsig: rtl.newTIProcSig(null)',
  28514. '});',
  28515. 'this.$rtti.$MethodVar("TMethodB", {',
  28516. ' procsig: rtl.newTIProcSig(null),',
  28517. ' methodkind: 0',
  28518. '});',
  28519. 'this.$rtti.$ProcVar("TProcC", {',
  28520. ' procsig: rtl.newTIProcSig(null, 2)',
  28521. '});',
  28522. 'this.$rtti.$ProcVar("TProcD", {',
  28523. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28524. '});',
  28525. 'this.$rtti.$ProcVar("TProcE", {',
  28526. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28527. '});',
  28528. 'this.$rtti.$ProcVar("TProcF", {',
  28529. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28530. '});',
  28531. 'this.p = null;',
  28532. '']),
  28533. LinesToStr([ // $mod.$main
  28534. '$mod.p = $mod.$rtti["TProcA"];',
  28535. '']));
  28536. end;
  28537. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28538. begin
  28539. WithTypeInfo:=true;
  28540. AddModuleWithIntfImplSrc('unit2.pas',
  28541. LinesToStr([
  28542. 'type',
  28543. ' TObject = class end;'
  28544. ]),
  28545. '');
  28546. StartUnit(true);
  28547. Add('interface');
  28548. Add('uses unit2;');
  28549. Add('type');
  28550. Add(' TProcA = function(o: tobject): tobject;');
  28551. Add('implementation');
  28552. Add('type');
  28553. Add(' TProcB = function(o: tobject): tobject;');
  28554. Add('var p: Pointer;');
  28555. Add('initialization');
  28556. Add(' p:=typeinfo(tproca);');
  28557. Add(' p:=typeinfo(tprocb);');
  28558. ConvertUnit;
  28559. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28560. LinesToStr([ // statements
  28561. 'var $impl = $mod.$impl;',
  28562. 'this.$rtti.$ProcVar("TProcA", {',
  28563. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28564. '});',
  28565. '']),
  28566. LinesToStr([ // this.$init
  28567. '$impl.p = $mod.$rtti["TProcA"];',
  28568. '$impl.p = $mod.$rtti["TProcB"];',
  28569. '']),
  28570. LinesToStr([ // implementation
  28571. '$mod.$rtti.$ProcVar("TProcB", {',
  28572. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28573. '});',
  28574. '$impl.p = null;',
  28575. '']) );
  28576. end;
  28577. procedure TTestModule.TestRTTI_EnumAndSetType;
  28578. begin
  28579. WithTypeInfo:=true;
  28580. StartProgram(false);
  28581. Add('type');
  28582. Add(' TFlag = (light,dark);');
  28583. Add(' TFlags = set of TFlag;');
  28584. Add(' TProc = function(f: TFlags): TFlag;');
  28585. Add('var p: pointer;');
  28586. Add('begin');
  28587. Add(' p:=typeinfo(tflag);');
  28588. Add(' p:=typeinfo(tflags);');
  28589. ConvertProgram;
  28590. CheckSource('TestRTTI_EnumAndType',
  28591. LinesToStr([ // statements
  28592. 'this.TFlag = {',
  28593. ' "0": "light",',
  28594. ' light: 0,',
  28595. ' "1": "dark",',
  28596. ' dark: 1',
  28597. '};',
  28598. 'this.$rtti.$Enum("TFlag", {',
  28599. ' minvalue: 0,',
  28600. ' maxvalue: 1,',
  28601. ' ordtype: 1,',
  28602. ' enumtype: this.TFlag',
  28603. '});',
  28604. 'this.$rtti.$Set("TFlags", {',
  28605. ' comptype: this.$rtti["TFlag"]',
  28606. '});',
  28607. 'this.$rtti.$ProcVar("TProc", {',
  28608. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28609. '});',
  28610. 'this.p = null;',
  28611. '']),
  28612. LinesToStr([ // $mod.$main
  28613. '$mod.p = $mod.$rtti["TFlag"];',
  28614. '$mod.p = $mod.$rtti["TFlags"];',
  28615. '']));
  28616. end;
  28617. procedure TTestModule.TestRTTI_EnumRange;
  28618. begin
  28619. WithTypeInfo:=true;
  28620. StartProgram(false);
  28621. Add([
  28622. 'type',
  28623. ' TCol = (red,green,blue);',
  28624. ' TColRg = green..blue;',
  28625. ' TSetOfColRg = set of TColRg;',
  28626. 'var p: pointer;',
  28627. 'begin',
  28628. ' p:=typeinfo(tcolrg);',
  28629. ' p:=typeinfo(tsetofcolrg);',
  28630. '']);
  28631. ConvertProgram;
  28632. end;
  28633. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28634. begin
  28635. WithTypeInfo:=true;
  28636. StartProgram(false);
  28637. Add('type');
  28638. Add(' TFlags = set of (red, green);');
  28639. Add('var');
  28640. Add(' f: TFlags;');
  28641. Add('begin');
  28642. Add(' Include(f,red);');
  28643. ConvertProgram;
  28644. CheckSource('TestRTTI_AnonymousEnumType',
  28645. LinesToStr([ // statements
  28646. 'this.TFlags$a = {',
  28647. ' "0": "red",',
  28648. ' red: 0,',
  28649. ' "1": "green",',
  28650. ' green: 1',
  28651. '};',
  28652. 'this.$rtti.$Enum("TFlags$a", {',
  28653. ' minvalue: 0,',
  28654. ' maxvalue: 1,',
  28655. ' ordtype: 1,',
  28656. ' enumtype: this.TFlags$a',
  28657. '});',
  28658. 'this.$rtti.$Set("TFlags", {',
  28659. ' comptype: this.$rtti["TFlags$a"]',
  28660. '});',
  28661. 'this.f = {};',
  28662. '']),
  28663. LinesToStr([
  28664. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28665. '']));
  28666. end;
  28667. procedure TTestModule.TestRTTI_StaticArray;
  28668. begin
  28669. WithTypeInfo:=true;
  28670. StartProgram(false);
  28671. Add('type');
  28672. Add(' TFlag = (light,dark);');
  28673. Add(' TFlagNames = array[TFlag] of string;');
  28674. Add(' TBoolNames = array[boolean] of string;');
  28675. Add(' TByteArray = array[1..32768] of byte;');
  28676. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28677. Add('var p: pointer;');
  28678. Add('begin');
  28679. Add(' p:=typeinfo(TFlagNames);');
  28680. Add(' p:=typeinfo(TBoolNames);');
  28681. ConvertProgram;
  28682. CheckSource('TestRTTI_StaticArray',
  28683. LinesToStr([ // statements
  28684. 'this.TFlag = {',
  28685. ' "0": "light",',
  28686. ' light: 0,',
  28687. ' "1": "dark",',
  28688. ' dark: 1',
  28689. '};',
  28690. 'this.$rtti.$Enum("TFlag", {',
  28691. ' minvalue: 0,',
  28692. ' maxvalue: 1,',
  28693. ' ordtype: 1,',
  28694. ' enumtype: this.TFlag',
  28695. '});',
  28696. 'this.$rtti.$StaticArray("TFlagNames", {',
  28697. ' dims: [2],',
  28698. ' eltype: rtl.string',
  28699. '});',
  28700. 'this.$rtti.$StaticArray("TBoolNames", {',
  28701. ' dims: [2],',
  28702. ' eltype: rtl.string',
  28703. '});',
  28704. 'this.$rtti.$StaticArray("TByteArray", {',
  28705. ' dims: [32768],',
  28706. ' eltype: rtl.byte',
  28707. '});',
  28708. 'this.$rtti.$ProcVar("TProc", {',
  28709. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28710. '});',
  28711. 'this.p = null;',
  28712. '']),
  28713. LinesToStr([ // $mod.$main
  28714. '$mod.p = $mod.$rtti["TFlagNames"];',
  28715. '$mod.p = $mod.$rtti["TBoolNames"];',
  28716. '']));
  28717. end;
  28718. procedure TTestModule.TestRTTI_DynArray;
  28719. begin
  28720. WithTypeInfo:=true;
  28721. StartProgram(false);
  28722. Add('type');
  28723. Add(' TArrStr = array of string;');
  28724. Add(' TArr2Dim = array of tarrstr;');
  28725. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28726. Add('var p: pointer;');
  28727. Add('begin');
  28728. Add(' p:=typeinfo(tarrstr);');
  28729. Add(' p:=typeinfo(tarr2dim);');
  28730. ConvertProgram;
  28731. CheckSource('TestRTTI_DynArray',
  28732. LinesToStr([ // statements
  28733. 'this.$rtti.$DynArray("TArrStr", {',
  28734. ' eltype: rtl.string',
  28735. '});',
  28736. 'this.$rtti.$DynArray("TArr2Dim", {',
  28737. ' eltype: this.$rtti["TArrStr"]',
  28738. '});',
  28739. 'this.$rtti.$ProcVar("TProc", {',
  28740. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28741. '});',
  28742. 'this.p = null;',
  28743. '']),
  28744. LinesToStr([ // $mod.$main
  28745. '$mod.p = $mod.$rtti["TArrStr"];',
  28746. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28747. '']));
  28748. end;
  28749. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28750. begin
  28751. WithTypeInfo:=true;
  28752. StartProgram(false);
  28753. Add('type');
  28754. Add(' TArr = array of array of longint;');
  28755. Add('var a: TArr;');
  28756. Add('begin');
  28757. ConvertProgram;
  28758. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28759. LinesToStr([ // statements
  28760. 'this.$rtti.$DynArray("TArr$a", {',
  28761. ' eltype: rtl.longint',
  28762. '});',
  28763. 'this.$rtti.$DynArray("TArr", {',
  28764. ' eltype: this.$rtti["TArr$a"]',
  28765. '});',
  28766. 'this.a = [];',
  28767. '']),
  28768. LinesToStr([ // $mod.$main
  28769. ]));
  28770. end;
  28771. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28772. begin
  28773. WithTypeInfo:=true;
  28774. StartProgram(false);
  28775. Add('type');
  28776. Add(' TObject = class');
  28777. Add(' published');
  28778. Add(' procedure Proc; virtual; abstract;');
  28779. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28780. Add(' end;');
  28781. Add('begin');
  28782. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28783. nDuplicatePublishedMethodXAtY);
  28784. ConvertProgram;
  28785. end;
  28786. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28787. begin
  28788. WithTypeInfo:=true;
  28789. StartProgram(false);
  28790. Add('type');
  28791. Add(' TObject = class');
  28792. Add(' published');
  28793. Add(' procedure Proc; external name ''foo'';');
  28794. Add(' end;');
  28795. Add('begin');
  28796. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28797. nPublishedNameMustMatchExternal);
  28798. ConvertProgram;
  28799. end;
  28800. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28801. begin
  28802. WithTypeInfo:=true;
  28803. StartProgram(false);
  28804. Add('type');
  28805. Add(' TObject = class');
  28806. Add(' class var FA: longint;');
  28807. Add(' published');
  28808. Add(' class property A: longint read FA;');
  28809. Add(' end;');
  28810. Add('begin');
  28811. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28812. nInvalidXModifierY);
  28813. ConvertProgram;
  28814. end;
  28815. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28816. begin
  28817. WithTypeInfo:=true;
  28818. StartProgram(false);
  28819. Add('type');
  28820. Add(' TObject = class');
  28821. Add(' published');
  28822. Add(' class var FA: longint;');
  28823. Add(' end;');
  28824. Add('begin');
  28825. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28826. nSymbolCannotBePublished);
  28827. ConvertProgram;
  28828. end;
  28829. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28830. begin
  28831. WithTypeInfo:=true;
  28832. StartProgram(false);
  28833. Add('{$modeswitch externalclass}');
  28834. Add('type');
  28835. Add(' TObject = class');
  28836. Add(' published');
  28837. Add(' V: longint; external name ''foo'';');
  28838. Add(' end;');
  28839. Add('begin');
  28840. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28841. nPublishedNameMustMatchExternal);
  28842. ConvertProgram;
  28843. end;
  28844. procedure TTestModule.TestRTTI_Class_Field;
  28845. begin
  28846. WithTypeInfo:=true;
  28847. StartProgram(false);
  28848. Add('{$modeswitch externalclass}');
  28849. Add('type');
  28850. Add(' TObject = class');
  28851. Add(' private');
  28852. Add(' FPropA: string;');
  28853. Add(' published');
  28854. Add(' VarLI: longint;');
  28855. Add(' VarC: char;');
  28856. Add(' VarS: string;');
  28857. Add(' VarD: double;');
  28858. Add(' VarB: boolean;');
  28859. Add(' VarLW: longword;');
  28860. Add(' VarSmI: smallint;');
  28861. Add(' VarW: word;');
  28862. Add(' VarShI: shortint;');
  28863. Add(' VarBy: byte;');
  28864. Add(' VarExt: longint external name ''VarExt'';');
  28865. Add(' ArrA, ArrB: array of byte;');
  28866. Add(' end;');
  28867. Add('var p: pointer;');
  28868. Add(' Obj: tobject;');
  28869. Add('begin');
  28870. Add(' p:=typeinfo(tobject);');
  28871. Add(' p:=typeinfo(p);');
  28872. Add(' p:=typeinfo(obj);');
  28873. ConvertProgram;
  28874. CheckSource('TestRTTI_Class_Field',
  28875. LinesToStr([ // statements
  28876. 'rtl.createClass(this, "TObject", null, function () {',
  28877. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28878. ' eltype: rtl.byte',
  28879. ' });',
  28880. ' this.$init = function () {',
  28881. ' this.FPropA = "";',
  28882. ' this.VarLI = 0;',
  28883. ' this.VarC = "";',
  28884. ' this.VarS = "";',
  28885. ' this.VarD = 0.0;',
  28886. ' this.VarB = false;',
  28887. ' this.VarLW = 0;',
  28888. ' this.VarSmI = 0;',
  28889. ' this.VarW = 0;',
  28890. ' this.VarShI = 0;',
  28891. ' this.VarBy = 0;',
  28892. ' this.ArrA = [];',
  28893. ' this.ArrB = [];',
  28894. ' };',
  28895. ' this.$final = function () {',
  28896. ' this.ArrA = undefined;',
  28897. ' this.ArrB = undefined;',
  28898. ' };',
  28899. ' var $r = this.$rtti;',
  28900. ' $r.addField("VarLI", rtl.longint);',
  28901. ' $r.addField("VarC", rtl.char);',
  28902. ' $r.addField("VarS", rtl.string);',
  28903. ' $r.addField("VarD", rtl.double);',
  28904. ' $r.addField("VarB", rtl.boolean);',
  28905. ' $r.addField("VarLW", rtl.longword);',
  28906. ' $r.addField("VarSmI", rtl.smallint);',
  28907. ' $r.addField("VarW", rtl.word);',
  28908. ' $r.addField("VarShI", rtl.shortint);',
  28909. ' $r.addField("VarBy", rtl.byte);',
  28910. ' $r.addField("VarExt", rtl.longint);',
  28911. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28912. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28913. '});',
  28914. 'this.p = null;',
  28915. 'this.Obj = null;',
  28916. '']),
  28917. LinesToStr([ // $mod.$main
  28918. '$mod.p = $mod.$rtti["TObject"];',
  28919. '$mod.p = rtl.pointer;',
  28920. '$mod.p = $mod.Obj.$rtti;',
  28921. '']));
  28922. end;
  28923. procedure TTestModule.TestRTTI_Class_Method;
  28924. begin
  28925. WithTypeInfo:=true;
  28926. StartProgram(false);
  28927. Add('type');
  28928. Add(' TObject = class');
  28929. Add(' private');
  28930. Add(' procedure Internal; external name ''$intern'';');
  28931. Add(' published');
  28932. Add(' procedure Click; virtual; abstract;');
  28933. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28934. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28935. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28936. Add(' end;');
  28937. Add('begin');
  28938. ConvertProgram;
  28939. CheckSource('TestRTTI_Class_Method',
  28940. LinesToStr([ // statements
  28941. 'rtl.createClass(this, "TObject", null, function () {',
  28942. ' this.$init = function () {',
  28943. ' };',
  28944. ' this.$final = function () {',
  28945. ' };',
  28946. ' var $r = this.$rtti;',
  28947. ' $r.addMethod("Click", 0, null);',
  28948. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28949. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28950. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28951. ' flags: 2',
  28952. ' });',
  28953. '});',
  28954. '']),
  28955. LinesToStr([ // $mod.$main
  28956. '']));
  28957. end;
  28958. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28959. begin
  28960. WithTypeInfo:=true;
  28961. StartProgram(false);
  28962. Add('type');
  28963. Add(' TObject = class');
  28964. Add(' published');
  28965. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28966. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28967. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28968. Add(' end;');
  28969. Add('begin');
  28970. ConvertProgram;
  28971. CheckSource('TestRTTI_Class_MethodOpenArray',
  28972. LinesToStr([ // statements
  28973. 'rtl.createClass(this, "TObject", null, function () {',
  28974. ' this.$init = function () {',
  28975. ' };',
  28976. ' this.$final = function () {',
  28977. ' };',
  28978. ' var $r = this.$rtti;',
  28979. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28980. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28981. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28982. '});',
  28983. '']),
  28984. LinesToStr([ // $mod.$main
  28985. '']));
  28986. end;
  28987. procedure TTestModule.TestRTTI_Class_Property;
  28988. begin
  28989. WithTypeInfo:=true;
  28990. StartProgram(false);
  28991. Add('{$modeswitch externalclass}');
  28992. Add('type');
  28993. Add(' TObject = class');
  28994. Add(' private');
  28995. Add(' FColor: longint;');
  28996. Add(' FColorStored: boolean;');
  28997. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28998. Add(' function GetColor: longint; virtual; abstract;');
  28999. Add(' function GetColorStored: boolean; virtual; abstract;');
  29000. Add(' FExtSize: longint external name ''$extSize'';');
  29001. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29002. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29003. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29004. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29005. Add(' published');
  29006. Add(' property ColorA: longint read FColor;');
  29007. Add(' property ColorB: longint write FColor;');
  29008. Add(' property ColorC: longint read GetColor write SetColor;');
  29009. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29010. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29011. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29012. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29013. Add(' end;');
  29014. Add('begin');
  29015. ConvertProgram;
  29016. CheckSource('TestRTTI_Class_Property',
  29017. LinesToStr([ // statements
  29018. 'rtl.createClass(this, "TObject", null, function () {',
  29019. ' this.$init = function () {',
  29020. ' this.FColor = 0;',
  29021. ' this.FColorStored = false;',
  29022. ' };',
  29023. ' this.$final = function () {',
  29024. ' };',
  29025. ' var $r = this.$rtti;',
  29026. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29027. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29028. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29029. ' $r.addProperty(',
  29030. ' "ColorD",',
  29031. ' 8,',
  29032. ' rtl.longint,',
  29033. ' "FColor",',
  29034. ' "FColor",',
  29035. ' {',
  29036. ' stored: "FColorStored"',
  29037. ' }',
  29038. ' );',
  29039. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29040. ' $r.addProperty(',
  29041. ' "ExtSizeB",',
  29042. ' 11,',
  29043. ' rtl.longint,',
  29044. ' "$getSize",',
  29045. ' "$setSize",',
  29046. ' {',
  29047. ' stored: "$extSizeStored"',
  29048. ' }',
  29049. ' );',
  29050. ' $r.addProperty(',
  29051. ' "ExtSizeC",',
  29052. ' 12,',
  29053. ' rtl.longint,',
  29054. ' "$extSize",',
  29055. ' "$extSize",',
  29056. ' {',
  29057. ' stored: "$getExtSizeStored"',
  29058. ' }',
  29059. ' );',
  29060. '});',
  29061. '']),
  29062. LinesToStr([ // $mod.$main
  29063. '']));
  29064. end;
  29065. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29066. begin
  29067. WithTypeInfo:=true;
  29068. StartProgram(false);
  29069. Add('{$modeswitch externalclass}');
  29070. Add('type');
  29071. Add(' integer = longint;');
  29072. Add(' TObject = class');
  29073. Add(' private');
  29074. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29075. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29076. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29077. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29078. Add(' published');
  29079. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29080. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29081. Add(' end;');
  29082. Add('begin');
  29083. ConvertProgram;
  29084. CheckSource('TestRTTI_Class_PropertyParams',
  29085. LinesToStr([ // statements
  29086. 'rtl.createClass(this, "TObject", null, function () {',
  29087. ' this.$init = function () {',
  29088. ' };',
  29089. ' this.$final = function () {',
  29090. ' };',
  29091. ' var $r = this.$rtti;',
  29092. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29093. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29094. '});',
  29095. '']),
  29096. LinesToStr([ // $mod.$main
  29097. '']));
  29098. end;
  29099. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29100. begin
  29101. WithTypeInfo:=true;
  29102. AddModuleWithIntfImplSrc('unit1.pas',
  29103. 'type TColor = -5..5;',
  29104. '');
  29105. StartProgram(true);
  29106. Add([
  29107. 'uses unit1;',
  29108. 'type',
  29109. ' TColorAlias = TColor;',
  29110. ' TColorTypeAlias = type TColor;',
  29111. ' TObject = class',
  29112. ' private',
  29113. ' fColor: TColor;',
  29114. ' fAlias: TColorAlias;',
  29115. ' fTypeAlias: TColorTypeAlias;',
  29116. ' published',
  29117. ' property Color: TColor read fcolor;',
  29118. ' property Alias: TColorAlias read falias;',
  29119. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29120. ' end;',
  29121. 'begin',
  29122. '']);
  29123. ConvertProgram;
  29124. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29125. LinesToStr([ // statements
  29126. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29127. 'rtl.createClass(this, "TObject", null, function () {',
  29128. ' this.$init = function () {',
  29129. ' this.fColor = 0;',
  29130. ' this.fAlias = 0;',
  29131. ' this.fTypeAlias = 0;',
  29132. ' };',
  29133. ' this.$final = function () {',
  29134. ' };',
  29135. ' var $r = this.$rtti;',
  29136. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29137. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29138. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29139. '});',
  29140. '']),
  29141. LinesToStr([ // $mod.$main
  29142. '']));
  29143. end;
  29144. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29145. begin
  29146. WithTypeInfo:=true;
  29147. StartProgram(false);
  29148. Add([
  29149. '{$modeswitch omitrtti}',
  29150. 'type',
  29151. ' TObject = class',
  29152. ' private',
  29153. ' FA: byte;',
  29154. ' published',
  29155. ' property A: byte read FA write FA;',
  29156. ' end;',
  29157. 'begin']);
  29158. ConvertProgram;
  29159. CheckSource('TestRTTI_Class_OmitRTTI',
  29160. LinesToStr([ // statements
  29161. 'rtl.createClass(this, "TObject", null, function () {',
  29162. ' this.$init = function () {',
  29163. ' this.FA = 0;',
  29164. ' };',
  29165. ' this.$final = function () {',
  29166. ' };',
  29167. '});',
  29168. '']),
  29169. LinesToStr([ // $mod.$main
  29170. '']));
  29171. end;
  29172. procedure TTestModule.TestRTTI_IndexModifier;
  29173. begin
  29174. WithTypeInfo:=true;
  29175. StartProgram(false);
  29176. Add([
  29177. 'type',
  29178. ' TEnum = (red, blue);',
  29179. ' TObject = class',
  29180. ' FB: boolean;',
  29181. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29182. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29183. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29184. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29185. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29186. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29187. ' published',
  29188. ' property B1: boolean index 1 read FB write SetIntBool;',
  29189. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29190. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29191. ' end;',
  29192. 'begin']);
  29193. ConvertProgram;
  29194. CheckSource('TestRTTI_IndexModifier',
  29195. LinesToStr([ // statements
  29196. 'this.TEnum = {',
  29197. ' "0": "red",',
  29198. ' red: 0,',
  29199. ' "1": "blue",',
  29200. ' blue: 1',
  29201. '};',
  29202. 'this.$rtti.$Enum("TEnum", {',
  29203. ' minvalue: 0,',
  29204. ' maxvalue: 1,',
  29205. ' ordtype: 1,',
  29206. ' enumtype: this.TEnum',
  29207. '});',
  29208. 'rtl.createClass(this, "TObject", null, function () {',
  29209. ' this.$init = function () {',
  29210. ' this.FB = false;',
  29211. ' };',
  29212. ' this.$final = function () {',
  29213. ' };',
  29214. ' var $r = this.$rtti;',
  29215. ' $r.addProperty(',
  29216. ' "B1",',
  29217. ' 18,',
  29218. ' rtl.boolean,',
  29219. ' "FB",',
  29220. ' "SetIntBool",',
  29221. ' {',
  29222. ' index: 1',
  29223. ' }',
  29224. ' );',
  29225. ' $r.addProperty(',
  29226. ' "B2",',
  29227. ' 17,',
  29228. ' rtl.boolean,',
  29229. ' "GetEnumBool",',
  29230. ' "FB",',
  29231. ' {',
  29232. ' index: $mod.TEnum.blue',
  29233. ' }',
  29234. ' );',
  29235. ' $r.addProperty(',
  29236. ' "I1",',
  29237. ' 19,',
  29238. ' rtl.boolean,',
  29239. ' "GetStrIntBool",',
  29240. ' "SetStrIntBool",',
  29241. ' {',
  29242. ' index: 2',
  29243. ' }',
  29244. ' );',
  29245. '});',
  29246. '']),
  29247. LinesToStr([ // $mod.$main
  29248. '']));
  29249. end;
  29250. procedure TTestModule.TestRTTI_StoredModifier;
  29251. begin
  29252. WithTypeInfo:=true;
  29253. StartProgram(false);
  29254. Add([
  29255. 'const',
  29256. ' ConstB = true;',
  29257. 'type',
  29258. ' TObject = class',
  29259. ' private',
  29260. ' FB: boolean;',
  29261. ' function IsBStored: boolean; virtual; abstract;',
  29262. ' published',
  29263. ' property BoolA: boolean read FB stored true;',
  29264. ' property BoolB: boolean read FB stored false;',
  29265. ' property BoolC: boolean read FB stored FB;',
  29266. ' property BoolD: boolean read FB stored ConstB;',
  29267. ' property BoolE: boolean read FB stored IsBStored;',
  29268. ' end;',
  29269. 'begin']);
  29270. ConvertProgram;
  29271. CheckSource('TestRTTI_StoredModifier',
  29272. LinesToStr([ // statements
  29273. 'this.ConstB = true;',
  29274. 'rtl.createClass(this, "TObject", null, function () {',
  29275. ' this.$init = function () {',
  29276. ' this.FB = false;',
  29277. ' };',
  29278. ' this.$final = function () {',
  29279. ' };',
  29280. ' var $r = this.$rtti;',
  29281. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29282. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29283. ' $r.addProperty(',
  29284. ' "BoolC",',
  29285. ' 8,',
  29286. ' rtl.boolean,',
  29287. ' "FB",',
  29288. ' "",',
  29289. ' {',
  29290. ' stored: "FB"',
  29291. ' }',
  29292. ' );',
  29293. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29294. ' $r.addProperty(',
  29295. ' "BoolE",',
  29296. ' 12,',
  29297. ' rtl.boolean,',
  29298. ' "FB",',
  29299. ' "",',
  29300. ' {',
  29301. ' stored: "IsBStored"',
  29302. ' }',
  29303. ' );',
  29304. '});',
  29305. '']),
  29306. LinesToStr([ // $mod.$main
  29307. '']));
  29308. end;
  29309. procedure TTestModule.TestRTTI_DefaultValue;
  29310. begin
  29311. WithTypeInfo:=true;
  29312. StartProgram(false);
  29313. Add([
  29314. 'type',
  29315. ' TEnum = (red, blue);',
  29316. 'const',
  29317. ' CB = true or false;',
  29318. ' CI = 1+2;',
  29319. 'type',
  29320. ' TObject = class',
  29321. ' FB: boolean;',
  29322. ' FI: longint;',
  29323. ' FE: TEnum;',
  29324. ' published',
  29325. ' property B1: boolean read FB default true;',
  29326. ' property B2: boolean read FB default CB;',
  29327. ' property B3: boolean read FB default test1.cb;',
  29328. ' property I1: longint read FI default 2;',
  29329. ' property I2: longint read FI default CI;',
  29330. ' property E1: TEnum read FE default red;',
  29331. ' property E2: TEnum read FE default TEnum.blue;',
  29332. ' end;',
  29333. 'begin']);
  29334. ConvertProgram;
  29335. CheckSource('TestRTTI_DefaultValue',
  29336. LinesToStr([ // statements
  29337. 'this.TEnum = {',
  29338. ' "0": "red",',
  29339. ' red: 0,',
  29340. ' "1": "blue",',
  29341. ' blue: 1',
  29342. '};',
  29343. 'this.$rtti.$Enum("TEnum", {',
  29344. ' minvalue: 0,',
  29345. ' maxvalue: 1,',
  29346. ' ordtype: 1,',
  29347. ' enumtype: this.TEnum',
  29348. '});',
  29349. 'this.CB = true || false;',
  29350. 'this.CI = 1 + 2;',
  29351. 'rtl.createClass(this, "TObject", null, function () {',
  29352. ' this.$init = function () {',
  29353. ' this.FB = false;',
  29354. ' this.FI = 0;',
  29355. ' this.FE = 0;',
  29356. ' };',
  29357. ' this.$final = function () {',
  29358. ' };',
  29359. ' var $r = this.$rtti;',
  29360. ' $r.addProperty(',
  29361. ' "B1",',
  29362. ' 0,',
  29363. ' rtl.boolean,',
  29364. ' "FB",',
  29365. ' "",',
  29366. ' {',
  29367. ' Default: true',
  29368. ' }',
  29369. ' );',
  29370. ' $r.addProperty(',
  29371. ' "B2",',
  29372. ' 0,',
  29373. ' rtl.boolean,',
  29374. ' "FB",',
  29375. ' "",',
  29376. ' {',
  29377. ' Default: true',
  29378. ' }',
  29379. ' );',
  29380. ' $r.addProperty(',
  29381. ' "B3",',
  29382. ' 0,',
  29383. ' rtl.boolean,',
  29384. ' "FB",',
  29385. ' "",',
  29386. ' {',
  29387. ' Default: true',
  29388. ' }',
  29389. ' );',
  29390. ' $r.addProperty(',
  29391. ' "I1",',
  29392. ' 0,',
  29393. ' rtl.longint,',
  29394. ' "FI",',
  29395. ' "",',
  29396. ' {',
  29397. ' Default: 2',
  29398. ' }',
  29399. ' );',
  29400. ' $r.addProperty(',
  29401. ' "I2",',
  29402. ' 0,',
  29403. ' rtl.longint,',
  29404. ' "FI",',
  29405. ' "",',
  29406. ' {',
  29407. ' Default: 3',
  29408. ' }',
  29409. ' );',
  29410. ' $r.addProperty(',
  29411. ' "E1",',
  29412. ' 0,',
  29413. ' $mod.$rtti["TEnum"],',
  29414. ' "FE",',
  29415. ' "",',
  29416. ' {',
  29417. ' Default: $mod.TEnum.red',
  29418. ' }',
  29419. ' );',
  29420. ' $r.addProperty(',
  29421. ' "E2",',
  29422. ' 0,',
  29423. ' $mod.$rtti["TEnum"],',
  29424. ' "FE",',
  29425. ' "",',
  29426. ' {',
  29427. ' Default: $mod.TEnum.blue',
  29428. ' }',
  29429. ' );',
  29430. '});',
  29431. '']),
  29432. LinesToStr([ // $mod.$main
  29433. '']));
  29434. end;
  29435. procedure TTestModule.TestRTTI_DefaultValueSet;
  29436. begin
  29437. WithTypeInfo:=true;
  29438. StartProgram(false);
  29439. Add([
  29440. 'type',
  29441. ' TEnum = (red, blue);',
  29442. ' TSet = set of TEnum;',
  29443. 'const',
  29444. ' CSet = [red,blue];',
  29445. 'type',
  29446. ' TObject = class',
  29447. ' FSet: TSet;',
  29448. ' published',
  29449. ' property Set1: TSet read FSet default [];',
  29450. ' property Set2: TSet read FSet default [red];',
  29451. ' property Set3: TSet read FSet default [red,blue];',
  29452. ' property Set4: TSet read FSet default CSet;',
  29453. ' end;',
  29454. 'begin']);
  29455. ConvertProgram;
  29456. CheckSource('TestRTTI_DefaultValueSet',
  29457. LinesToStr([ // statements
  29458. 'this.TEnum = {',
  29459. ' "0": "red",',
  29460. ' red: 0,',
  29461. ' "1": "blue",',
  29462. ' blue: 1',
  29463. '};',
  29464. 'this.$rtti.$Enum("TEnum", {',
  29465. ' minvalue: 0,',
  29466. ' maxvalue: 1,',
  29467. ' ordtype: 1,',
  29468. ' enumtype: this.TEnum',
  29469. '});',
  29470. 'this.$rtti.$Set("TSet", {',
  29471. ' comptype: this.$rtti["TEnum"]',
  29472. '});',
  29473. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29474. 'rtl.createClass(this, "TObject", null, function () {',
  29475. ' this.$init = function () {',
  29476. ' this.FSet = {};',
  29477. ' };',
  29478. ' this.$final = function () {',
  29479. ' this.FSet = undefined;',
  29480. ' };',
  29481. ' var $r = this.$rtti;',
  29482. ' $r.addProperty(',
  29483. ' "Set1",',
  29484. ' 0,',
  29485. ' $mod.$rtti["TSet"],',
  29486. ' "FSet",',
  29487. ' "",',
  29488. ' {',
  29489. ' Default: {}',
  29490. ' }',
  29491. ' );',
  29492. ' $r.addProperty(',
  29493. ' "Set2",',
  29494. ' 0,',
  29495. ' $mod.$rtti["TSet"],',
  29496. ' "FSet",',
  29497. ' "",',
  29498. ' {',
  29499. ' Default: rtl.createSet($mod.TEnum.red)',
  29500. ' }',
  29501. ' );',
  29502. ' $r.addProperty(',
  29503. ' "Set3",',
  29504. ' 0,',
  29505. ' $mod.$rtti["TSet"],',
  29506. ' "FSet",',
  29507. ' "",',
  29508. ' {',
  29509. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29510. ' }',
  29511. ' );',
  29512. ' $r.addProperty(',
  29513. ' "Set4",',
  29514. ' 0,',
  29515. ' $mod.$rtti["TSet"],',
  29516. ' "FSet",',
  29517. ' "",',
  29518. ' {',
  29519. ' Default: $mod.CSet',
  29520. ' }',
  29521. ' );',
  29522. '});',
  29523. '']),
  29524. LinesToStr([ // $mod.$main
  29525. '']));
  29526. end;
  29527. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29528. begin
  29529. WithTypeInfo:=true;
  29530. StartProgram(false);
  29531. Add([
  29532. 'type',
  29533. ' TRg = -1..1;',
  29534. 'const',
  29535. ' l = low(TRg);',
  29536. ' h = high(TRg);',
  29537. 'type',
  29538. ' TObject = class',
  29539. ' FV: TRg;',
  29540. ' published',
  29541. ' property V1: TRg read FV default -1;',
  29542. ' end;',
  29543. 'begin']);
  29544. ConvertProgram;
  29545. CheckSource('TestRTTI_DefaultValueRangeType',
  29546. LinesToStr([ // statements
  29547. 'this.$rtti.$Int("TRg", {',
  29548. ' minvalue: -1,',
  29549. ' maxvalue: 1,',
  29550. ' ordtype: 0',
  29551. '});',
  29552. 'this.l = -1;',
  29553. 'this.h = 1;',
  29554. 'rtl.createClass(this, "TObject", null, function () {',
  29555. ' this.$init = function () {',
  29556. ' this.FV = 0;',
  29557. ' };',
  29558. ' this.$final = function () {',
  29559. ' };',
  29560. ' var $r = this.$rtti;',
  29561. ' $r.addProperty(',
  29562. ' "V1",',
  29563. ' 0,',
  29564. ' $mod.$rtti["TRg"],',
  29565. ' "FV",',
  29566. ' "",',
  29567. ' {',
  29568. ' Default: -1',
  29569. ' }',
  29570. ' );',
  29571. '});',
  29572. '']),
  29573. LinesToStr([ // $mod.$main
  29574. '']));
  29575. end;
  29576. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29577. begin
  29578. WithTypeInfo:=true;
  29579. StartProgram(false);
  29580. Add([
  29581. 'type',
  29582. ' TObject = class',
  29583. ' FA, FB: byte;',
  29584. ' property A: byte read FA default 1;',
  29585. ' property B: byte read FB default 2;',
  29586. ' end;',
  29587. ' TBird = class',
  29588. ' published',
  29589. ' property A;',
  29590. ' property B nodefault;',
  29591. ' end;',
  29592. 'begin']);
  29593. ConvertProgram;
  29594. CheckSource('TestRTTI_DefaultValueInherit',
  29595. LinesToStr([ // statements
  29596. 'rtl.createClass(this, "TObject", null, function () {',
  29597. ' this.$init = function () {',
  29598. ' this.FA = 0;',
  29599. ' this.FB = 0;',
  29600. ' };',
  29601. ' this.$final = function () {',
  29602. ' };',
  29603. '});',
  29604. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29605. ' var $r = this.$rtti;',
  29606. ' $r.addProperty(',
  29607. ' "A",',
  29608. ' 0,',
  29609. ' rtl.byte,',
  29610. ' "FA",',
  29611. ' "",',
  29612. ' {',
  29613. ' Default: 1',
  29614. ' }',
  29615. ' );',
  29616. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29617. '});',
  29618. '']),
  29619. LinesToStr([ // $mod.$main
  29620. '']));
  29621. end;
  29622. procedure TTestModule.TestRTTI_OverrideMethod;
  29623. begin
  29624. WithTypeInfo:=true;
  29625. StartProgram(false);
  29626. Add('type');
  29627. Add(' TObject = class');
  29628. Add(' published');
  29629. Add(' procedure DoIt; virtual; abstract;');
  29630. Add(' end;');
  29631. Add(' TSky = class');
  29632. Add(' published');
  29633. Add(' procedure DoIt; override;');
  29634. Add(' end;');
  29635. Add('procedure TSky.DoIt; begin end;');
  29636. Add('begin');
  29637. ConvertProgram;
  29638. CheckSource('TestRTTI_OverrideMethod',
  29639. LinesToStr([ // statements
  29640. 'rtl.createClass(this, "TObject", null, function () {',
  29641. ' this.$init = function () {',
  29642. ' };',
  29643. ' this.$final = function () {',
  29644. ' };',
  29645. ' var $r = this.$rtti;',
  29646. ' $r.addMethod("DoIt", 0, null);',
  29647. '});',
  29648. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29649. ' this.DoIt = function () {',
  29650. ' };',
  29651. '});',
  29652. '']),
  29653. LinesToStr([ // $mod.$main
  29654. '']));
  29655. end;
  29656. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29657. begin
  29658. WithTypeInfo:=true;
  29659. StartProgram(false);
  29660. Add([
  29661. 'type',
  29662. ' TObject = class',
  29663. ' published',
  29664. ' procedure DoIt;',
  29665. ' end;',
  29666. ' TSky = class',
  29667. ' published',
  29668. ' procedure DoIt; reintroduce;',
  29669. ' end;',
  29670. 'procedure TObject.DoIt; begin end;',
  29671. 'procedure TSky.DoIt;',
  29672. 'begin',
  29673. ' inherited DoIt;',
  29674. 'end;',
  29675. 'begin']);
  29676. ConvertProgram;
  29677. CheckSource('TestRTTI_ReintroduceMethod',
  29678. LinesToStr([ // statements
  29679. 'rtl.createClass(this, "TObject", null, function () {',
  29680. ' this.$init = function () {',
  29681. ' };',
  29682. ' this.$final = function () {',
  29683. ' };',
  29684. ' this.DoIt = function () {',
  29685. ' };',
  29686. ' var $r = this.$rtti;',
  29687. ' $r.addMethod("DoIt", 0, null);',
  29688. '});',
  29689. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29690. ' this.DoIt = function () {',
  29691. ' $mod.TObject.DoIt.call(this);',
  29692. ' };',
  29693. ' var $r = this.$rtti;',
  29694. ' $r.addMethod("DoIt", 0, null);',
  29695. '});',
  29696. '']),
  29697. LinesToStr([ // $mod.$main
  29698. '']));
  29699. end;
  29700. procedure TTestModule.TestRTTI_OverloadProperty;
  29701. begin
  29702. WithTypeInfo:=true;
  29703. StartProgram(false);
  29704. Add('type');
  29705. Add(' TObject = class');
  29706. Add(' protected');
  29707. Add(' FFlag: longint;');
  29708. Add(' published');
  29709. Add(' property Flag: longint read fflag;');
  29710. Add(' end;');
  29711. Add(' TSky = class');
  29712. Add(' published');
  29713. Add(' property FLAG: longint write fflag;');
  29714. Add(' end;');
  29715. Add('begin');
  29716. ConvertProgram;
  29717. CheckSource('TestRTTI_OverrideMethod',
  29718. LinesToStr([ // statements
  29719. 'rtl.createClass(this, "TObject", null, function () {',
  29720. ' this.$init = function () {',
  29721. ' this.FFlag = 0;',
  29722. ' };',
  29723. ' this.$final = function () {',
  29724. ' };',
  29725. ' var $r = this.$rtti;',
  29726. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29727. '});',
  29728. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29729. ' var $r = this.$rtti;',
  29730. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29731. '});',
  29732. '']),
  29733. LinesToStr([ // $mod.$main
  29734. '']));
  29735. end;
  29736. procedure TTestModule.TestRTTI_ClassForward;
  29737. begin
  29738. WithTypeInfo:=true;
  29739. StartProgram(false);
  29740. Add('type');
  29741. Add(' TObject = class end;');
  29742. Add(' tbridge = class;');
  29743. Add(' TProc = function: tbridge;');
  29744. Add(' TOger = class');
  29745. Add(' published');
  29746. Add(' FBridge: tbridge;');
  29747. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29748. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29749. Add(' end;');
  29750. Add(' TBridge = class');
  29751. Add(' FOger: toger;');
  29752. Add(' end;');
  29753. Add('var p: Pointer;');
  29754. Add(' b: tbridge;');
  29755. Add('begin');
  29756. Add(' p:=typeinfo(tbridge);');
  29757. Add(' p:=typeinfo(b);');
  29758. ConvertProgram;
  29759. CheckSource('TestRTTI_ClassForward',
  29760. LinesToStr([ // statements
  29761. 'rtl.createClass(this, "TObject", null, function () {',
  29762. ' this.$init = function () {',
  29763. ' };',
  29764. ' this.$final = function () {',
  29765. ' };',
  29766. '});',
  29767. 'this.$rtti.$Class("TBridge");',
  29768. 'this.$rtti.$ProcVar("TProc", {',
  29769. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29770. '});',
  29771. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29772. ' this.$init = function () {',
  29773. ' $mod.TObject.$init.call(this);',
  29774. ' this.FBridge = null;',
  29775. ' };',
  29776. ' this.$final = function () {',
  29777. ' this.FBridge = undefined;',
  29778. ' $mod.TObject.$final.call(this);',
  29779. ' };',
  29780. ' var $r = this.$rtti;',
  29781. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29782. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29783. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29784. '});',
  29785. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29786. ' this.$init = function () {',
  29787. ' $mod.TObject.$init.call(this);',
  29788. ' this.FOger = null;',
  29789. ' };',
  29790. ' this.$final = function () {',
  29791. ' this.FOger = undefined;',
  29792. ' $mod.TObject.$final.call(this);',
  29793. ' };',
  29794. '});',
  29795. 'this.p = null;',
  29796. 'this.b = null;',
  29797. '']),
  29798. LinesToStr([ // $mod.$main
  29799. '$mod.p = $mod.$rtti["TBridge"];',
  29800. '$mod.p = $mod.b.$rtti;',
  29801. '']));
  29802. end;
  29803. procedure TTestModule.TestRTTI_ClassOf;
  29804. begin
  29805. WithTypeInfo:=true;
  29806. StartProgram(false);
  29807. Add('type');
  29808. Add(' TClass = class of tobject;');
  29809. Add(' TProcA = function: TClass;');
  29810. Add(' TObject = class');
  29811. Add(' published');
  29812. Add(' C: tclass;');
  29813. Add(' end;');
  29814. Add(' tfox = class;');
  29815. Add(' TBird = class end;');
  29816. Add(' TBirds = class of tbird;');
  29817. Add(' TFox = class end;');
  29818. Add(' TFoxes = class of tfox;');
  29819. Add(' TCows = class of TCow;');
  29820. Add(' TCow = class;');
  29821. Add(' TCow = class end;');
  29822. Add('begin');
  29823. ConvertProgram;
  29824. CheckSource('TestRTTI_ClassOf',
  29825. LinesToStr([ // statements
  29826. 'this.$rtti.$Class("TObject");',
  29827. 'this.$rtti.$ClassRef("TClass", {',
  29828. ' instancetype: this.$rtti["TObject"]',
  29829. '});',
  29830. 'this.$rtti.$ProcVar("TProcA", {',
  29831. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29832. '});',
  29833. 'rtl.createClass(this, "TObject", null, function () {',
  29834. ' this.$init = function () {',
  29835. ' this.C = null;',
  29836. ' };',
  29837. ' this.$final = function () {',
  29838. ' this.C = undefined;',
  29839. ' };',
  29840. ' var $r = this.$rtti;',
  29841. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29842. '});',
  29843. 'this.$rtti.$Class("TFox");',
  29844. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29845. '});',
  29846. 'this.$rtti.$ClassRef("TBirds", {',
  29847. ' instancetype: this.$rtti["TBird"]',
  29848. '});',
  29849. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29850. '});',
  29851. 'this.$rtti.$ClassRef("TFoxes", {',
  29852. ' instancetype: this.$rtti["TFox"]',
  29853. '});',
  29854. 'this.$rtti.$Class("TCow");',
  29855. 'this.$rtti.$ClassRef("TCows", {',
  29856. ' instancetype: this.$rtti["TCow"]',
  29857. '});',
  29858. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29859. '});',
  29860. '']),
  29861. LinesToStr([ // $mod.$main
  29862. '']));
  29863. end;
  29864. procedure TTestModule.TestRTTI_Record;
  29865. begin
  29866. WithTypeInfo:=true;
  29867. StartProgram(false);
  29868. Add('type');
  29869. Add(' integer = longint;');
  29870. Add(' TPoint = record');
  29871. Add(' x,y: integer;');
  29872. Add(' end;');
  29873. Add('var p: pointer;');
  29874. Add(' r: tpoint;');
  29875. Add('begin');
  29876. Add(' p:=typeinfo(tpoint);');
  29877. Add(' p:=typeinfo(r);');
  29878. Add(' p:=typeinfo(r.x);');
  29879. ConvertProgram;
  29880. CheckSource('TestRTTI_Record',
  29881. LinesToStr([ // statements
  29882. 'rtl.recNewT(this, "TPoint", function () {',
  29883. ' this.x = 0;',
  29884. ' this.y = 0;',
  29885. ' this.$eq = function (b) {',
  29886. ' return (this.x === b.x) && (this.y === b.y);',
  29887. ' };',
  29888. ' this.$assign = function (s) {',
  29889. ' this.x = s.x;',
  29890. ' this.y = s.y;',
  29891. ' return this;',
  29892. ' };',
  29893. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29894. ' $r.addField("x", rtl.longint);',
  29895. ' $r.addField("y", rtl.longint);',
  29896. '});',
  29897. 'this.p = null;',
  29898. 'this.r = this.TPoint.$new();',
  29899. '']),
  29900. LinesToStr([ // $mod.$main
  29901. '$mod.p = $mod.$rtti["TPoint"];',
  29902. '$mod.p = $mod.$rtti["TPoint"];',
  29903. '$mod.p = rtl.longint;',
  29904. '']));
  29905. end;
  29906. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29907. begin
  29908. WithTypeInfo:=true;
  29909. StartProgram(false);
  29910. Add('type');
  29911. Add(' TFloatRec = record');
  29912. Add(' c,d: array of char;');
  29913. // Add(' i: array of array of longint;');
  29914. Add(' end;');
  29915. Add('var p: pointer;');
  29916. Add(' r: tfloatrec;');
  29917. Add('begin');
  29918. Add(' p:=typeinfo(tfloatrec);');
  29919. Add(' p:=typeinfo(r);');
  29920. Add(' p:=typeinfo(r.d);');
  29921. ConvertProgram;
  29922. CheckSource('TestRTTI_Record',
  29923. LinesToStr([ // statements
  29924. 'rtl.recNewT(this, "TFloatRec", function () {',
  29925. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29926. ' eltype: rtl.char',
  29927. ' });',
  29928. ' this.$new = function () {',
  29929. ' var r = Object.create(this);',
  29930. ' r.c = [];',
  29931. ' r.d = [];',
  29932. ' return r;',
  29933. ' };',
  29934. ' this.$eq = function (b) {',
  29935. ' return (this.c === b.c) && (this.d === b.d);',
  29936. ' };',
  29937. ' this.$assign = function (s) {',
  29938. ' this.c = rtl.arrayRef(s.c);',
  29939. ' this.d = rtl.arrayRef(s.d);',
  29940. ' return this;',
  29941. ' };',
  29942. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29943. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29944. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29945. '});',
  29946. 'this.p = null;',
  29947. 'this.r = this.TFloatRec.$new();',
  29948. '']),
  29949. LinesToStr([ // $mod.$main
  29950. '$mod.p = $mod.$rtti["TFloatRec"];',
  29951. '$mod.p = $mod.$rtti["TFloatRec"];',
  29952. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29953. '']));
  29954. end;
  29955. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29956. begin
  29957. WithTypeInfo:=true;
  29958. StartProgram(false);
  29959. Add([
  29960. '{$modeswitch AdvancedRecords}',
  29961. 'type',
  29962. ' TPoint = record',
  29963. ' type TProc = procedure(w: word);',
  29964. ' class var p: TProc;',
  29965. ' end;',
  29966. 'begin',
  29967. '']);
  29968. ConvertProgram;
  29969. CheckSource('TestRTTI_Record_ClassVarType',
  29970. LinesToStr([ // statements
  29971. 'rtl.recNewT(this, "TPoint", function () {',
  29972. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  29973. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  29974. ' });',
  29975. ' this.p = null;',
  29976. ' this.$eq = function (b) {',
  29977. ' return true;',
  29978. ' };',
  29979. ' this.$assign = function (s) {',
  29980. ' return this;',
  29981. ' };',
  29982. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29983. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  29984. '}, true);',
  29985. '']),
  29986. LinesToStr([ // $mod.$main
  29987. '']));
  29988. end;
  29989. procedure TTestModule.TestRTTI_LocalTypes;
  29990. begin
  29991. WithTypeInfo:=true;
  29992. StartProgram(false);
  29993. Add([
  29994. 'procedure DoIt;',
  29995. 'type',
  29996. ' integer = longint;',
  29997. ' TPoint = record',
  29998. ' x,y: integer;',
  29999. ' end;',
  30000. 'var p: TPoint;',
  30001. 'begin',
  30002. 'end;',
  30003. 'begin']);
  30004. ConvertProgram;
  30005. CheckSource('TestRTTI_LocalTypes',
  30006. LinesToStr([ // statements
  30007. 'var TPoint = rtl.recNewT(null, "", function () {',
  30008. ' this.x = 0;',
  30009. ' this.y = 0;',
  30010. ' this.$eq = function (b) {',
  30011. ' return (this.x === b.x) && (this.y === b.y);',
  30012. ' };',
  30013. ' this.$assign = function (s) {',
  30014. ' this.x = s.x;',
  30015. ' this.y = s.y;',
  30016. ' return this;',
  30017. ' };',
  30018. '});',
  30019. 'this.DoIt = function () {',
  30020. ' var p = TPoint.$new();',
  30021. '};',
  30022. '']),
  30023. LinesToStr([ // $mod.$main
  30024. '']));
  30025. end;
  30026. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30027. begin
  30028. WithTypeInfo:=true;
  30029. StartProgram(false);
  30030. Add([
  30031. 'type',
  30032. ' TCaption = string;',
  30033. ' TYesNo = boolean;',
  30034. ' TLetter = char;',
  30035. ' TFloat = double;',
  30036. ' TPtr = pointer;',
  30037. ' TShortInt = shortint;',
  30038. ' TByte = byte;',
  30039. ' TSmallInt = smallint;',
  30040. ' TWord = word;',
  30041. ' TInt32 = longint;',
  30042. ' TDWord = longword;',
  30043. ' TValue = jsvalue;',
  30044. 'var p: TPtr;',
  30045. 'begin',
  30046. ' p:=typeinfo(string);',
  30047. ' p:=typeinfo(tcaption);',
  30048. ' p:=typeinfo(boolean);',
  30049. ' p:=typeinfo(tyesno);',
  30050. ' p:=typeinfo(char);',
  30051. ' p:=typeinfo(tletter);',
  30052. ' p:=typeinfo(double);',
  30053. ' p:=typeinfo(tfloat);',
  30054. ' p:=typeinfo(pointer);',
  30055. ' p:=typeinfo(tptr);',
  30056. ' p:=typeinfo(shortint);',
  30057. ' p:=typeinfo(tshortint);',
  30058. ' p:=typeinfo(byte);',
  30059. ' p:=typeinfo(tbyte);',
  30060. ' p:=typeinfo(smallint);',
  30061. ' p:=typeinfo(tsmallint);',
  30062. ' p:=typeinfo(word);',
  30063. ' p:=typeinfo(tword);',
  30064. ' p:=typeinfo(longword);',
  30065. ' p:=typeinfo(tdword);',
  30066. ' p:=typeinfo(jsvalue);',
  30067. ' p:=typeinfo(tvalue);',
  30068. '']);
  30069. ConvertProgram;
  30070. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30071. LinesToStr([ // statements
  30072. 'this.p = null;',
  30073. '']),
  30074. LinesToStr([ // $mod.$main
  30075. '$mod.p = rtl.string;',
  30076. '$mod.p = rtl.string;',
  30077. '$mod.p = rtl.boolean;',
  30078. '$mod.p = rtl.boolean;',
  30079. '$mod.p = rtl.char;',
  30080. '$mod.p = rtl.char;',
  30081. '$mod.p = rtl.double;',
  30082. '$mod.p = rtl.double;',
  30083. '$mod.p = rtl.pointer;',
  30084. '$mod.p = rtl.pointer;',
  30085. '$mod.p = rtl.shortint;',
  30086. '$mod.p = rtl.shortint;',
  30087. '$mod.p = rtl.byte;',
  30088. '$mod.p = rtl.byte;',
  30089. '$mod.p = rtl.smallint;',
  30090. '$mod.p = rtl.smallint;',
  30091. '$mod.p = rtl.word;',
  30092. '$mod.p = rtl.word;',
  30093. '$mod.p = rtl.longword;',
  30094. '$mod.p = rtl.longword;',
  30095. '$mod.p = rtl.jsvalue;',
  30096. '$mod.p = rtl.jsvalue;',
  30097. '']));
  30098. end;
  30099. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30100. begin
  30101. WithTypeInfo:=true;
  30102. StartProgram(false);
  30103. Add([
  30104. 'type',
  30105. ' TCaption = type string;',
  30106. ' TYesNo = type boolean;',
  30107. ' TLetter = type char;',
  30108. ' TFloat = type double;',
  30109. ' TPtr = type pointer;',
  30110. ' TShortInt = type shortint;',
  30111. ' TByte = type byte;',
  30112. ' TSmallInt = type smallint;',
  30113. ' TWord = type word;',
  30114. ' TInt32 = type longint;',
  30115. ' TDWord = type longword;',
  30116. ' TValue = type jsvalue;',
  30117. ' TAliasValue = type TValue;',
  30118. 'var',
  30119. ' p: TPtr;',
  30120. ' a: TAliasValue;',
  30121. 'begin',
  30122. ' p:=typeinfo(tcaption);',
  30123. ' p:=typeinfo(tyesno);',
  30124. ' p:=typeinfo(tletter);',
  30125. ' p:=typeinfo(tfloat);',
  30126. ' p:=typeinfo(tptr);',
  30127. ' p:=typeinfo(tshortint);',
  30128. ' p:=typeinfo(tbyte);',
  30129. ' p:=typeinfo(tsmallint);',
  30130. ' p:=typeinfo(tword);',
  30131. ' p:=typeinfo(tdword);',
  30132. ' p:=typeinfo(tvalue);',
  30133. ' p:=typeinfo(taliasvalue);',
  30134. ' p:=typeinfo(a);',
  30135. '']);
  30136. ConvertProgram;
  30137. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30138. LinesToStr([ // statements
  30139. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30140. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30141. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30142. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30143. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30144. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30145. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30146. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30147. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30148. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30149. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30150. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30151. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30152. 'this.p = null;',
  30153. 'this.a = undefined;',
  30154. '']),
  30155. LinesToStr([ // $mod.$main
  30156. '$mod.p = $mod.$rtti["TCaption"];',
  30157. '$mod.p = $mod.$rtti["TYesNo"];',
  30158. '$mod.p = $mod.$rtti["TLetter"];',
  30159. '$mod.p = $mod.$rtti["TFloat"];',
  30160. '$mod.p = $mod.$rtti["TPtr"];',
  30161. '$mod.p = $mod.$rtti["TShortInt"];',
  30162. '$mod.p = $mod.$rtti["TByte"];',
  30163. '$mod.p = $mod.$rtti["TSmallInt"];',
  30164. '$mod.p = $mod.$rtti["TWord"];',
  30165. '$mod.p = $mod.$rtti["TDWord"];',
  30166. '$mod.p = $mod.$rtti["TValue"];',
  30167. '$mod.p = $mod.$rtti["TAliasValue"];',
  30168. '$mod.p = $mod.$rtti["TAliasValue"];',
  30169. '']));
  30170. end;
  30171. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30172. begin
  30173. WithTypeInfo:=true;
  30174. StartProgram(false);
  30175. Add('procedure DoIt;');
  30176. Add('type');
  30177. Add(' integer = longint;');
  30178. Add(' TPoint = record');
  30179. Add(' x,y: integer;');
  30180. Add(' end;');
  30181. Add('var p: pointer;');
  30182. Add('begin');
  30183. Add(' p:=typeinfo(tpoint);');
  30184. Add('end;');
  30185. Add('begin');
  30186. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30187. ConvertProgram;
  30188. end;
  30189. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30190. begin
  30191. WithTypeInfo:=true;
  30192. StartProgram(true,[supTypeInfo]);
  30193. Add([
  30194. '{$modeswitch externalclass}',
  30195. 'type',
  30196. ' TFlag = (up,down);',
  30197. ' TFlags = set of TFlag;',
  30198. 'var',
  30199. ' ti: TTypeInfo;',
  30200. ' tiInt: TTypeInfoInteger;',
  30201. ' tiEnum: TTypeInfoEnum;',
  30202. ' tiSet: TTypeInfoSet;',
  30203. 'begin',
  30204. ' ti:=typeinfo(string);',
  30205. ' ti:=typeinfo(boolean);',
  30206. ' ti:=typeinfo(char);',
  30207. ' ti:=typeinfo(double);',
  30208. ' tiInt:=typeinfo(shortint);',
  30209. ' tiInt:=typeinfo(byte);',
  30210. ' tiInt:=typeinfo(smallint);',
  30211. ' tiInt:=typeinfo(word);',
  30212. ' tiInt:=typeinfo(longint);',
  30213. ' tiInt:=typeinfo(longword);',
  30214. ' ti:=typeinfo(jsvalue);',
  30215. ' tiEnum:=typeinfo(tflag);',
  30216. ' tiSet:=typeinfo(tflags);']);
  30217. ConvertProgram;
  30218. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30219. LinesToStr([ // statements
  30220. 'this.TFlag = {',
  30221. ' "0": "up",',
  30222. ' up: 0,',
  30223. ' "1": "down",',
  30224. ' down: 1',
  30225. '};',
  30226. 'this.$rtti.$Enum("TFlag", {',
  30227. ' minvalue: 0,',
  30228. ' maxvalue: 1,',
  30229. ' ordtype: 1,',
  30230. ' enumtype: this.TFlag',
  30231. '});',
  30232. 'this.$rtti.$Set("TFlags", {',
  30233. ' comptype: this.$rtti["TFlag"]',
  30234. '});',
  30235. 'this.ti = null;',
  30236. 'this.tiInt = null;',
  30237. 'this.tiEnum = null;',
  30238. 'this.tiSet = null;',
  30239. '']),
  30240. LinesToStr([ // $mod.$main
  30241. '$mod.ti = rtl.string;',
  30242. '$mod.ti = rtl.boolean;',
  30243. '$mod.ti = rtl.char;',
  30244. '$mod.ti = rtl.double;',
  30245. '$mod.tiInt = rtl.shortint;',
  30246. '$mod.tiInt = rtl.byte;',
  30247. '$mod.tiInt = rtl.smallint;',
  30248. '$mod.tiInt = rtl.word;',
  30249. '$mod.tiInt = rtl.longint;',
  30250. '$mod.tiInt = rtl.longword;',
  30251. '$mod.ti = rtl.jsvalue;',
  30252. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30253. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30254. '']));
  30255. end;
  30256. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30257. begin
  30258. WithTypeInfo:=true;
  30259. StartProgram(true,[supTypeInfo]);
  30260. Add('{$modeswitch externalclass}');
  30261. Add('type');
  30262. Add(' TStaticArr = array[boolean] of string;');
  30263. Add(' TDynArr = array of string;');
  30264. Add(' TProc = procedure;');
  30265. Add(' TMethod = procedure of object;');
  30266. Add('var');
  30267. Add(' StaticArray: TStaticArr;');
  30268. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30269. Add(' DynArray: TDynArr;');
  30270. Add(' tiDynArray: TTypeInfoDynArray;');
  30271. Add(' ProcVar: TProc;');
  30272. Add(' tiProcVar: TTypeInfoProcVar;');
  30273. Add(' MethodVar: TMethod;');
  30274. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30275. Add('begin');
  30276. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30277. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30278. Add(' tiDynArray:=typeinfo(DynArray);');
  30279. Add(' tiDynArray:=typeinfo(TDynArr);');
  30280. Add(' tiProcVar:=typeinfo(ProcVar);');
  30281. Add(' tiProcVar:=typeinfo(TProc);');
  30282. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30283. Add(' tiMethodVar:=typeinfo(TMethod);');
  30284. ConvertProgram;
  30285. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30286. LinesToStr([ // statements
  30287. 'this.$rtti.$StaticArray("TStaticArr", {',
  30288. ' dims: [2],',
  30289. ' eltype: rtl.string',
  30290. '});',
  30291. 'this.$rtti.$DynArray("TDynArr", {',
  30292. ' eltype: rtl.string',
  30293. '});',
  30294. 'this.$rtti.$ProcVar("TProc", {',
  30295. ' procsig: rtl.newTIProcSig(null)',
  30296. '});',
  30297. 'this.$rtti.$MethodVar("TMethod", {',
  30298. ' procsig: rtl.newTIProcSig(null),',
  30299. ' methodkind: 0',
  30300. '});',
  30301. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30302. 'this.tiStaticArray = null;',
  30303. 'this.DynArray = [];',
  30304. 'this.tiDynArray = null;',
  30305. 'this.ProcVar = null;',
  30306. 'this.tiProcVar = null;',
  30307. 'this.MethodVar = null;',
  30308. 'this.tiMethodVar = null;',
  30309. '']),
  30310. LinesToStr([ // $mod.$main
  30311. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30312. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30313. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30314. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30315. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30316. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30317. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30318. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30319. '']));
  30320. end;
  30321. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30322. begin
  30323. WithTypeInfo:=true;
  30324. StartProgram(true,[supTypeInfo]);
  30325. Add('{$modeswitch externalclass}');
  30326. Add('type');
  30327. Add(' TRec = record end;');
  30328. // ToDo: ^TRec
  30329. Add(' TObject = class end;');
  30330. Add(' TClass = class of tobject;');
  30331. Add('var');
  30332. Add(' Rec: trec;');
  30333. Add(' tiRecord: ttypeinforecord;');
  30334. Add(' Obj: tobject;');
  30335. Add(' tiClass: ttypeinfoclass;');
  30336. Add(' aClass: tclass;');
  30337. Add(' tiClassRef: ttypeinfoclassref;');
  30338. // ToDo: ^TRec
  30339. Add(' tiPointer: ttypeinfopointer;');
  30340. Add('begin');
  30341. Add(' tirecord:=typeinfo(trec);');
  30342. Add(' tirecord:=typeinfo(trec);');
  30343. Add(' ticlass:=typeinfo(obj);');
  30344. Add(' ticlass:=typeinfo(tobject);');
  30345. Add(' ticlass:=typeinfo(aclass);');
  30346. Add(' ticlassref:=typeinfo(tclass);');
  30347. ConvertProgram;
  30348. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30349. LinesToStr([ // statements
  30350. 'rtl.recNewT(this, "TRec", function () {',
  30351. ' this.$eq = function (b) {',
  30352. ' return true;',
  30353. ' };',
  30354. ' this.$assign = function (s) {',
  30355. ' return this;',
  30356. ' };',
  30357. ' $mod.$rtti.$Record("TRec", {});',
  30358. '});',
  30359. 'rtl.createClass(this, "TObject", null, function () {',
  30360. ' this.$init = function () {',
  30361. ' };',
  30362. ' this.$final = function () {',
  30363. ' };',
  30364. '});',
  30365. 'this.$rtti.$ClassRef("TClass", {',
  30366. ' instancetype: this.$rtti["TObject"]',
  30367. '});',
  30368. 'this.Rec = this.TRec.$new();',
  30369. 'this.tiRecord = null;',
  30370. 'this.Obj = null;',
  30371. 'this.tiClass = null;',
  30372. 'this.aClass = null;',
  30373. 'this.tiClassRef = null;',
  30374. 'this.tiPointer = null;',
  30375. '']),
  30376. LinesToStr([ // $mod.$main
  30377. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30378. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30379. '$mod.tiClass = $mod.Obj.$rtti;',
  30380. '$mod.tiClass = $mod.$rtti["TObject"];',
  30381. '$mod.tiClass = $mod.aClass.$rtti;',
  30382. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30383. '']));
  30384. end;
  30385. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30386. begin
  30387. WithTypeInfo:=true;
  30388. StartProgram(true,[supTypeInfo]);
  30389. Add([
  30390. '{$modeswitch externalclass}',
  30391. 'type',
  30392. ' TClass = class of tobject;',
  30393. ' TObject = class',
  30394. ' function MyClass: TClass;',
  30395. ' class function ClassType: TClass;',
  30396. ' end;',
  30397. 'function TObject.MyClass: TClass;',
  30398. 'var t: TTypeInfoClass;',
  30399. 'begin',
  30400. ' t:=TypeInfo(Self);',
  30401. ' t:=TypeInfo(Result);',
  30402. ' t:=TypeInfo(TObject);',
  30403. 'end;',
  30404. 'class function TObject.ClassType: TClass;',
  30405. 'var t: TTypeInfoClass;',
  30406. 'begin',
  30407. ' t:=TypeInfo(Self);',
  30408. ' t:=TypeInfo(Result);',
  30409. 'end;',
  30410. 'var',
  30411. ' Obj: TObject;',
  30412. ' t: TTypeInfoClass;',
  30413. 'begin',
  30414. ' t:=TypeInfo(TObject.ClassType);',
  30415. ' t:=TypeInfo(Obj.ClassType);',
  30416. ' t:=TypeInfo(Obj.MyClass);',
  30417. '']);
  30418. ConvertProgram;
  30419. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30420. LinesToStr([ // statements
  30421. 'this.$rtti.$Class("TObject");',
  30422. 'this.$rtti.$ClassRef("TClass", {',
  30423. ' instancetype: this.$rtti["TObject"]',
  30424. '});',
  30425. 'rtl.createClass(this, "TObject", null, function () {',
  30426. ' this.$init = function () {',
  30427. ' };',
  30428. ' this.$final = function () {',
  30429. ' };',
  30430. ' this.MyClass = function () {',
  30431. ' var Result = null;',
  30432. ' var t = null;',
  30433. ' t = this.$rtti;',
  30434. ' t = Result.$rtti;',
  30435. ' t = $mod.$rtti["TObject"];',
  30436. ' return Result;',
  30437. ' };',
  30438. ' this.ClassType = function () {',
  30439. ' var Result = null;',
  30440. ' var t = null;',
  30441. ' t = this.$rtti;',
  30442. ' t = Result.$rtti;',
  30443. ' return Result;',
  30444. ' };',
  30445. '});',
  30446. 'this.Obj = null;',
  30447. 'this.t = null;',
  30448. '']),
  30449. LinesToStr([ // $mod.$main
  30450. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30451. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30452. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30453. '']));
  30454. end;
  30455. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30456. begin
  30457. WithTypeInfo:=true;
  30458. AddModuleWithIntfImplSrc('typinfo.pas',
  30459. LinesToStr([
  30460. '{$modeswitch externalclass}',
  30461. 'type',
  30462. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30463. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30464. '']),
  30465. '');
  30466. AddModuleWithIntfImplSrc('unit2.pas',
  30467. LinesToStr([
  30468. 'uses typinfo;',
  30469. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30470. 'procedure DoPtr(p: PTypeInfo);',
  30471. 'procedure DoInfo(t: TTypeInfo);',
  30472. 'procedure DoInt(t: TTypeInfoInteger);',
  30473. '']),
  30474. LinesToStr([
  30475. 'procedure DoPtr(p: PTypeInfo);',
  30476. 'begin end;',
  30477. 'procedure DoInfo(t: TTypeInfo);',
  30478. 'begin end;',
  30479. 'procedure DoInt(t: TTypeInfoInteger);',
  30480. 'begin end;',
  30481. '']));
  30482. StartUnit(true);
  30483. Add([
  30484. 'interface',
  30485. 'uses unit2;', // does not use unit typinfo
  30486. 'implementation',
  30487. 'var',
  30488. ' i: byte;',
  30489. ' p: pointer;',
  30490. ' t: PTypeInfo;',
  30491. 'initialization',
  30492. ' p:=typeinfo(i);',
  30493. ' t:=typeinfo(i);',
  30494. ' if p=t then ;',
  30495. ' if p=typeinfo(i) then ;',
  30496. ' if typeinfo(i)=p then ;',
  30497. ' if t=typeinfo(i) then ;',
  30498. ' if typeinfo(i)=t then ;',
  30499. ' DoPtr(p);',
  30500. ' DoPtr(t);',
  30501. ' DoPtr(typeinfo(i));',
  30502. ' DoInfo(p);',
  30503. ' DoInfo(t);',
  30504. ' DoInfo(typeinfo(i));',
  30505. ' DoInt(typeinfo(i));',
  30506. '']);
  30507. ConvertUnit;
  30508. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30509. LinesToStr([ // statements
  30510. 'var $impl = $mod.$impl;',
  30511. '']),
  30512. LinesToStr([ // this.$init
  30513. '$impl.p = rtl.byte;',
  30514. '$impl.t = rtl.byte;',
  30515. 'if ($impl.p === $impl.t) ;',
  30516. 'if ($impl.p === rtl.byte) ;',
  30517. 'if (rtl.byte === $impl.p) ;',
  30518. 'if ($impl.t === rtl.byte) ;',
  30519. 'if (rtl.byte === $impl.t) ;',
  30520. 'pas.unit2.DoPtr($impl.p);',
  30521. 'pas.unit2.DoPtr($impl.t);',
  30522. 'pas.unit2.DoPtr(rtl.byte);',
  30523. 'pas.unit2.DoInfo($impl.p);',
  30524. 'pas.unit2.DoInfo($impl.t);',
  30525. 'pas.unit2.DoInfo(rtl.byte);',
  30526. 'pas.unit2.DoInt(rtl.byte);',
  30527. '']),
  30528. LinesToStr([ // implementation
  30529. '$impl.i = 0;',
  30530. '$impl.p = null;',
  30531. '$impl.t = null;',
  30532. '']) );
  30533. end;
  30534. procedure TTestModule.TestRTTI_Interface_Corba;
  30535. begin
  30536. WithTypeInfo:=true;
  30537. StartProgram(true,[supTypeInfo]);
  30538. Add([
  30539. '{$interfaces corba}',
  30540. '{$modeswitch externalclass}',
  30541. 'type',
  30542. ' IUnknown = interface',
  30543. ' end;',
  30544. ' IBird = interface',
  30545. ' function GetItem: longint;',
  30546. ' procedure SetItem(Value: longint);',
  30547. ' property Item: longint read GetItem write SetItem;',
  30548. ' end;',
  30549. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30550. 'var',
  30551. ' i: IBird;',
  30552. ' t: TTypeInfoInterface;',
  30553. 'begin',
  30554. ' t:=TypeInfo(IBird);',
  30555. ' t:=TypeInfo(i);',
  30556. ' DoIt(t);',
  30557. ' DoIt(TypeInfo(IBird));',
  30558. '']);
  30559. ConvertProgram;
  30560. CheckSource('TestRTTI_Interface_Corba',
  30561. LinesToStr([ // statements
  30562. 'rtl.createInterface(',
  30563. ' this,',
  30564. ' "IUnknown",',
  30565. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30566. ' [],',
  30567. ' null,',
  30568. ' function () {',
  30569. ' }',
  30570. ');',
  30571. 'rtl.createInterface(',
  30572. ' this,',
  30573. ' "IBird",',
  30574. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30575. ' ["GetItem", "SetItem"],',
  30576. ' null,',
  30577. ' function () {',
  30578. ' var $r = this.$rtti;',
  30579. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30580. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30581. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30582. ' }',
  30583. ');',
  30584. 'this.DoIt = function (t) {',
  30585. '}; ',
  30586. 'this.i = null;',
  30587. 'this.t = null;',
  30588. '']),
  30589. LinesToStr([ // $mod.$main
  30590. '$mod.t = $mod.$rtti["IBird"];',
  30591. '$mod.t = $mod.i.$rtti;',
  30592. '$mod.DoIt($mod.t);',
  30593. '$mod.DoIt($mod.$rtti["IBird"]);',
  30594. '']));
  30595. end;
  30596. procedure TTestModule.TestRTTI_Interface_COM;
  30597. begin
  30598. WithTypeInfo:=true;
  30599. StartProgram(true,[supTypeInfo]);
  30600. Add([
  30601. '{$interfaces com}',
  30602. '{$modeswitch externalclass}',
  30603. 'type',
  30604. ' TGuid = record end;',
  30605. ' integer = longint;',
  30606. ' IUnknown = interface',
  30607. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30608. ' function _AddRef: Integer;',
  30609. ' function _Release: Integer;',
  30610. ' end;',
  30611. ' IBird = interface',
  30612. ' function GetItem: longint;',
  30613. ' procedure SetItem(Value: longint);',
  30614. ' property Item: longint read GetItem write SetItem;',
  30615. ' end;',
  30616. 'var',
  30617. ' i: IBird;',
  30618. ' t: TTypeInfoInterface;',
  30619. 'begin',
  30620. ' t:=TypeInfo(IBird);',
  30621. ' t:=TypeInfo(i);',
  30622. '']);
  30623. ConvertProgram;
  30624. CheckSource('TestRTTI_Interface_COM',
  30625. LinesToStr([ // statements
  30626. 'rtl.recNewT(this, "TGuid", function () {',
  30627. ' this.$eq = function (b) {',
  30628. ' return true;',
  30629. ' };',
  30630. ' this.$assign = function (s) {',
  30631. ' return this;',
  30632. ' };',
  30633. ' $mod.$rtti.$Record("TGuid", {});',
  30634. '});',
  30635. 'rtl.createInterface(',
  30636. ' this,',
  30637. ' "IUnknown",',
  30638. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30639. ' ["QueryInterface", "_AddRef", "_Release"],',
  30640. ' null,',
  30641. ' function () {',
  30642. ' this.$kind = "com";',
  30643. ' var $r = this.$rtti;',
  30644. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30645. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30646. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30647. ' }',
  30648. ');',
  30649. 'rtl.createInterface(',
  30650. ' this,',
  30651. ' "IBird",',
  30652. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30653. ' ["GetItem", "SetItem"],',
  30654. ' this.IUnknown,',
  30655. ' function () {',
  30656. ' var $r = this.$rtti;',
  30657. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30658. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30659. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30660. ' }',
  30661. ');',
  30662. 'this.i = null;',
  30663. 'this.t = null;',
  30664. '']),
  30665. LinesToStr([ // $mod.$main
  30666. '$mod.t = $mod.$rtti["IBird"];',
  30667. '$mod.t = $mod.i.$rtti;',
  30668. '']));
  30669. end;
  30670. procedure TTestModule.TestRTTI_ClassHelper;
  30671. begin
  30672. WithTypeInfo:=true;
  30673. StartProgram(true,[supTypeInfo]);
  30674. Add([
  30675. '{$interfaces com}',
  30676. '{$modeswitch externalclass}',
  30677. 'type',
  30678. ' TObject = class',
  30679. ' end;',
  30680. ' THelper = class helper for TObject',
  30681. ' published',
  30682. ' function GetItem: longint;',
  30683. ' property Item: longint read GetItem;',
  30684. ' end;',
  30685. 'function THelper.GetItem: longint;',
  30686. 'begin',
  30687. 'end;',
  30688. 'var',
  30689. ' t: TTypeInfoHelper;',
  30690. 'begin',
  30691. ' t:=TypeInfo(THelper);',
  30692. '']);
  30693. ConvertProgram;
  30694. CheckSource('TestRTTI_ClassHelper',
  30695. LinesToStr([ // statements
  30696. 'rtl.createClass(this, "TObject", null, function () {',
  30697. ' this.$init = function () {',
  30698. ' };',
  30699. ' this.$final = function () {',
  30700. ' };',
  30701. '});',
  30702. 'rtl.createHelper(this, "THelper", null, function () {',
  30703. ' this.GetItem = function () {',
  30704. ' var Result = 0;',
  30705. ' return Result;',
  30706. ' };',
  30707. ' var $r = this.$rtti;',
  30708. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30709. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30710. '});',
  30711. 'this.t = null;',
  30712. '']),
  30713. LinesToStr([ // $mod.$main
  30714. '$mod.t = $mod.$rtti["THelper"];',
  30715. '']));
  30716. end;
  30717. procedure TTestModule.TestRTTI_ExternalClass;
  30718. begin
  30719. WithTypeInfo:=true;
  30720. StartProgram(true,[supTypeInfo]);
  30721. Add([
  30722. '{$modeswitch externalclass}',
  30723. 'type',
  30724. ' TJSObject = class external name ''Object''',
  30725. ' end;',
  30726. ' TJSArray = class external name ''Array'' (TJSObject)',
  30727. ' end;',
  30728. 'var',
  30729. ' p: Pointer;',
  30730. ' tc: TTypeInfoExtClass;',
  30731. 'begin',
  30732. ' p:=typeinfo(TJSArray);']);
  30733. ConvertProgram;
  30734. CheckSource('TestRTTI_ExternalClass',
  30735. LinesToStr([ // statements
  30736. 'this.$rtti.$ExtClass("TJSObject", {',
  30737. ' jsclass: "Object"',
  30738. '});',
  30739. 'this.$rtti.$ExtClass("TJSArray", {',
  30740. ' ancestor: this.$rtti["TJSObject"],',
  30741. ' jsclass: "Array"',
  30742. '});',
  30743. 'this.p = null;',
  30744. 'this.tc = null;',
  30745. '']),
  30746. LinesToStr([ // $mod.$main
  30747. '$mod.p = $mod.$rtti["TJSArray"];',
  30748. '']));
  30749. end;
  30750. procedure TTestModule.TestRTTI_Unit;
  30751. begin
  30752. WithTypeInfo:=true;
  30753. AddModuleWithIntfImplSrc('unit2.pas',
  30754. LinesToStr([
  30755. '{$mode delphi}',
  30756. 'type',
  30757. ' TWordArray = array of word;',
  30758. ' TArray<T> = array of T;',
  30759. '']),
  30760. '');
  30761. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30762. Add([
  30763. '{$mode delphi}',
  30764. 'interface',
  30765. 'uses unit2;',
  30766. 'type',
  30767. ' IBird = interface',
  30768. ' function Swoop: TWordArray;',
  30769. ' function Glide: TArray<word>;',
  30770. ' end;',
  30771. 'procedure Fly;',
  30772. 'implementation',
  30773. 'procedure Fly;',
  30774. 'var',
  30775. ' ta: tTypeInfoDynArray;',
  30776. ' ti: tTypeInfoInterface;',
  30777. 'begin',
  30778. ' ta:=typeinfo(TWordArray);',
  30779. ' ta:=typeinfo(TArray<word>);',
  30780. ' ti:=typeinfo(IBird);',
  30781. 'end;',
  30782. '']);
  30783. ConvertUnit;
  30784. CheckSource('TestRTTI_ExternalClass',
  30785. LinesToStr([ // statements
  30786. 'rtl.createInterface(',
  30787. ' this,',
  30788. ' "IBird",',
  30789. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30790. ' ["Swoop", "Glide"],',
  30791. ' pas.system.IUnknown,',
  30792. ' function () {',
  30793. ' var $r = this.$rtti;',
  30794. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30795. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30796. ' }',
  30797. ');',
  30798. 'this.Fly = function () {',
  30799. ' var ta = null;',
  30800. ' var ti = null;',
  30801. ' ta = pas.unit2.$rtti["TWordArray"];',
  30802. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30803. ' ti = $mod.$rtti["IBird"];',
  30804. '};',
  30805. '']),
  30806. LinesToStr([ // $mod.$main
  30807. '']));
  30808. end;
  30809. procedure TTestModule.TestResourcestringProgram;
  30810. begin
  30811. AddModuleWithIntfImplSrc('unit2.pas',
  30812. LinesToStr([
  30813. 'resourcestring Title = ''Nice'';',
  30814. '']),
  30815. '');
  30816. StartProgram(true);
  30817. Add([
  30818. 'uses unit2;',
  30819. 'const Bar = ''bar'';',
  30820. 'resourcestring',
  30821. ' Red = ''red'';',
  30822. ' Foobar = ''fOo''+bar;',
  30823. 'var s: string;',
  30824. ' c: char;',
  30825. 'begin',
  30826. ' s:=red;',
  30827. ' s:=test1.red;',
  30828. ' s:=Title;',
  30829. ' c:=red[1];',
  30830. ' c:=test1.red[2];',
  30831. ' if red=foobar then ;',
  30832. ' if red[3]=red[4] then ;']);
  30833. ConvertProgram;
  30834. CheckSource('TestResourcestringProgram',
  30835. LinesToStr([ // statements
  30836. 'this.Bar = "bar";',
  30837. 'this.s = "";',
  30838. 'this.c = "";',
  30839. '$mod.$resourcestrings = {',
  30840. ' Red: {',
  30841. ' org: "red"',
  30842. ' },',
  30843. ' Foobar: {',
  30844. ' org: "fOobar"',
  30845. ' }',
  30846. '};',
  30847. '']),
  30848. LinesToStr([ // $mod.$main
  30849. '$mod.s = rtl.getResStr($mod, "Red");',
  30850. '$mod.s = rtl.getResStr($mod, "Red");',
  30851. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30852. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30853. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30854. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30855. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30856. '']));
  30857. end;
  30858. procedure TTestModule.TestResourcestringUnit;
  30859. begin
  30860. AddModuleWithIntfImplSrc('unit2.pas',
  30861. LinesToStr([
  30862. 'resourcestring Title = ''Nice'';',
  30863. '']),
  30864. '');
  30865. StartUnit(true);
  30866. Add([
  30867. 'interface',
  30868. 'uses unit2;',
  30869. 'const Red = ''rEd'';',
  30870. 'resourcestring',
  30871. ' Blue = ''blue'';',
  30872. ' NotRed = ''not''+Red;',
  30873. 'var s: string;',
  30874. 'implementation',
  30875. 'resourcestring',
  30876. ' ImplGreen = ''green'';',
  30877. 'initialization',
  30878. ' s:=blue+ImplGreen;',
  30879. ' s:=test1.blue+test1.implgreen;',
  30880. ' s:=blue[1]+implgreen[2];',
  30881. ' s:=Title;',
  30882. '']);
  30883. ConvertUnit;
  30884. CheckSource('TestResourcestringUnit',
  30885. LinesToStr([ // statements
  30886. 'this.Red = "rEd";',
  30887. 'this.s = "";',
  30888. '$mod.$resourcestrings = {',
  30889. ' Blue: {',
  30890. ' org: "blue"',
  30891. ' },',
  30892. ' NotRed: {',
  30893. ' org: "notrEd"',
  30894. ' },',
  30895. ' ImplGreen: {',
  30896. ' org: "green"',
  30897. ' }',
  30898. '};',
  30899. '']),
  30900. LinesToStr([ // $mod.$main
  30901. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30902. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30903. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30904. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30905. '']));
  30906. end;
  30907. procedure TTestModule.TestResourcestringImplementation;
  30908. begin
  30909. StartUnit(false);
  30910. Add([
  30911. 'interface',
  30912. 'implementation',
  30913. 'resourcestring',
  30914. ' ImplRed = ''red'';']);
  30915. ConvertUnit;
  30916. CheckSource('TestResourcestringImplementation',
  30917. LinesToStr([ // intf statements
  30918. 'var $impl = $mod.$impl;']),
  30919. LinesToStr([ // $mod.$init
  30920. '']),
  30921. LinesToStr([ // impl statements
  30922. '$mod.$resourcestrings = {',
  30923. ' ImplRed: {',
  30924. ' org: "red"',
  30925. ' }',
  30926. '};',
  30927. '']));
  30928. end;
  30929. procedure TTestModule.TestAttributes_Members;
  30930. begin
  30931. WithTypeInfo:=true;
  30932. StartProgram(false);
  30933. Add([
  30934. '{$modeswitch PrefixedAttributes}',
  30935. 'type',
  30936. ' TObject = class',
  30937. ' constructor Create;',
  30938. ' end;',
  30939. ' TCustomAttribute = class',
  30940. ' constructor Create(Id: word);',
  30941. ' end;',
  30942. ' [Missing]',
  30943. ' TBird = class',
  30944. ' published',
  30945. ' [Tcustom]',
  30946. ' FField: word;',
  30947. ' [tcustom(14)]',
  30948. ' property Size: word read FField;',
  30949. ' [Tcustom(15)]',
  30950. ' procedure Fly; virtual; abstract;',
  30951. ' end;',
  30952. ' TRec = record',
  30953. ' [Tcustom,tcustom(14)]',
  30954. ' Size: word;',
  30955. ' end;',
  30956. 'constructor TObject.Create; begin end;',
  30957. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30958. 'begin',
  30959. '']);
  30960. ConvertProgram;
  30961. CheckSource('TestAttributes_Members',
  30962. LinesToStr([ // statements
  30963. 'rtl.createClass(this, "TObject", null, function () {',
  30964. ' this.$init = function () {',
  30965. ' };',
  30966. ' this.$final = function () {',
  30967. ' };',
  30968. ' this.Create = function () {',
  30969. ' return this;',
  30970. ' };',
  30971. '});',
  30972. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30973. ' this.Create$1 = function (Id) {',
  30974. ' return this;',
  30975. ' };',
  30976. '});',
  30977. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30978. ' this.$init = function () {',
  30979. ' $mod.TObject.$init.call(this);',
  30980. ' this.FField = 0;',
  30981. ' };',
  30982. ' var $r = this.$rtti;',
  30983. ' $r.addField("FField", rtl.word, {',
  30984. ' attr: [$mod.TCustomAttribute, "Create"]',
  30985. ' });',
  30986. ' $r.addProperty(',
  30987. ' "Size",',
  30988. ' 0,',
  30989. ' rtl.word,',
  30990. ' "FField",',
  30991. ' "",',
  30992. ' {',
  30993. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30994. ' }',
  30995. ' );',
  30996. ' $r.addMethod("Fly", 0, null, null, {',
  30997. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30998. ' });',
  30999. '});',
  31000. 'rtl.recNewT(this, "TRec", function () {',
  31001. ' this.Size = 0;',
  31002. ' this.$eq = function (b) {',
  31003. ' return this.Size === b.Size;',
  31004. ' };',
  31005. ' this.$assign = function (s) {',
  31006. ' this.Size = s.Size;',
  31007. ' return this;',
  31008. ' };',
  31009. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31010. ' $r.addField("Size", rtl.word, {',
  31011. ' attr: [',
  31012. ' $mod.TCustomAttribute,',
  31013. ' "Create",',
  31014. ' $mod.TCustomAttribute,',
  31015. ' "Create$1",',
  31016. ' [14]',
  31017. ' ]',
  31018. ' });',
  31019. '});',
  31020. '']),
  31021. LinesToStr([ // $mod.$main
  31022. '']));
  31023. end;
  31024. procedure TTestModule.TestAttributes_Types;
  31025. begin
  31026. WithTypeInfo:=true;
  31027. StartProgram(false);
  31028. Add([
  31029. '{$modeswitch PrefixedAttributes}',
  31030. 'type',
  31031. ' TObject = class',
  31032. ' constructor Create(Id: word);',
  31033. ' end;',
  31034. ' TCustomAttribute = class',
  31035. ' end;',
  31036. ' [TCustom(1)]',
  31037. ' TMyClass = class',
  31038. ' end;',
  31039. ' [TCustom(11)]',
  31040. ' TMyDescendant = class(TMyClass)',
  31041. ' end;',
  31042. ' [TCustom(2)]',
  31043. ' TRec = record',
  31044. ' end;',
  31045. ' [TCustom(3)]',
  31046. ' TInt = type word;',
  31047. 'constructor TObject.Create(Id: word);',
  31048. 'begin',
  31049. 'end;',
  31050. 'var p: pointer;',
  31051. 'begin',
  31052. ' p:=typeinfo(TMyClass);',
  31053. ' p:=typeinfo(TRec);',
  31054. ' p:=typeinfo(TInt);',
  31055. '']);
  31056. ConvertProgram;
  31057. CheckSource('TestAttributes_Types',
  31058. LinesToStr([ // statements
  31059. 'rtl.createClass(this, "TObject", null, function () {',
  31060. ' this.$init = function () {',
  31061. ' };',
  31062. ' this.$final = function () {',
  31063. ' };',
  31064. ' this.Create = function (Id) {',
  31065. ' return this;',
  31066. ' };',
  31067. '});',
  31068. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31069. '});',
  31070. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31071. ' var $r = this.$rtti;',
  31072. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31073. '});',
  31074. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31075. ' var $r = this.$rtti;',
  31076. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31077. '});',
  31078. 'rtl.recNewT(this, "TRec", function () {',
  31079. ' this.$eq = function (b) {',
  31080. ' return true;',
  31081. ' };',
  31082. ' this.$assign = function (s) {',
  31083. ' return this;',
  31084. ' };',
  31085. ' $mod.$rtti.$Record("TRec", {',
  31086. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31087. ' });',
  31088. '});',
  31089. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31090. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31091. '});',
  31092. 'this.p = null;',
  31093. '']),
  31094. LinesToStr([ // $mod.$main
  31095. '$mod.p = $mod.$rtti["TMyClass"];',
  31096. '$mod.p = $mod.$rtti["TRec"];',
  31097. '$mod.p = $mod.$rtti["TInt"];',
  31098. '']));
  31099. end;
  31100. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31101. begin
  31102. WithTypeInfo:=true;
  31103. StartProgram(false);
  31104. Add([
  31105. '{$modeswitch PrefixedAttributes}',
  31106. 'type',
  31107. ' TObject = class',
  31108. ' constructor Create;',
  31109. ' end;',
  31110. ' TCustomAttribute = class',
  31111. ' end;',
  31112. ' THelper = class helper for TCustomAttribute',
  31113. ' constructor Create(Id: word);',
  31114. ' end;',
  31115. ' [TCustom(3)]',
  31116. ' TMyInt = word;',
  31117. 'constructor TObject.Create; begin end;',
  31118. 'constructor THelper.Create(Id: word); begin end;',
  31119. 'begin',
  31120. ' if typeinfo(TMyInt)=nil then ;']);
  31121. ConvertProgram;
  31122. end;
  31123. procedure TTestModule.TestAssert;
  31124. begin
  31125. StartProgram(false);
  31126. Add([
  31127. 'procedure DoIt;',
  31128. 'var',
  31129. ' b: boolean;',
  31130. ' s: string;',
  31131. 'begin',
  31132. ' {$Assertions on}',
  31133. ' Assert(b);',
  31134. 'end;',
  31135. 'begin',
  31136. ' DoIt;',
  31137. '']);
  31138. ConvertProgram;
  31139. CheckSource('TestAssert',
  31140. LinesToStr([ // statements
  31141. 'this.DoIt = function () {',
  31142. ' var b = false;',
  31143. ' var s = "";',
  31144. ' if (!b) throw "assert failed";',
  31145. '};',
  31146. '']),
  31147. LinesToStr([ // $mod.$main
  31148. '$mod.DoIt();',
  31149. '']));
  31150. end;
  31151. procedure TTestModule.TestAssert_SysUtils;
  31152. begin
  31153. AddModuleWithIntfImplSrc('SysUtils.pas',
  31154. LinesToStr([
  31155. 'type',
  31156. ' TObject = class',
  31157. ' constructor Create;',
  31158. ' end;',
  31159. ' EAssertionFailed = class',
  31160. ' constructor Create(s: string);',
  31161. ' end;',
  31162. '']),
  31163. LinesToStr([
  31164. 'constructor TObject.Create;',
  31165. 'begin end;',
  31166. 'constructor EAssertionFailed.Create(s: string);',
  31167. 'begin end;',
  31168. '']) );
  31169. StartProgram(true);
  31170. Add([
  31171. 'uses sysutils;',
  31172. 'procedure DoIt;',
  31173. 'var',
  31174. ' b: boolean;',
  31175. ' s: string;',
  31176. 'begin',
  31177. ' {$Assertions on}',
  31178. ' Assert(b);',
  31179. ' Assert(b,''msg'');',
  31180. 'end;',
  31181. 'begin',
  31182. ' DoIt;',
  31183. '']);
  31184. ConvertProgram;
  31185. CheckSource('TestAssert_SysUtils',
  31186. LinesToStr([ // statements
  31187. 'this.DoIt = function () {',
  31188. ' var b = false;',
  31189. ' var s = "";',
  31190. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31191. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31192. '};',
  31193. '']),
  31194. LinesToStr([ // $mod.$main
  31195. '$mod.DoIt();',
  31196. '']));
  31197. end;
  31198. procedure TTestModule.TestObjectChecks;
  31199. begin
  31200. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31201. StartProgram(false);
  31202. Add([
  31203. 'type',
  31204. ' TObject = class',
  31205. ' procedure DoIt;',
  31206. ' end;',
  31207. ' TClass = class of tobject;',
  31208. ' TBird = class',
  31209. ' end;',
  31210. ' TBirdClass = class of TBird;',
  31211. 'var',
  31212. ' o : TObject;',
  31213. ' c: TClass;',
  31214. ' b: TBird;',
  31215. ' bc: TBirdClass;',
  31216. 'procedure TObject.DoIt;',
  31217. 'begin',
  31218. ' b:=TBird(o);',
  31219. 'end;',
  31220. 'begin',
  31221. ' o.DoIt;',
  31222. ' b:=TBird(o);',
  31223. ' bc:=TBirdClass(c);',
  31224. '']);
  31225. ConvertProgram;
  31226. CheckSource('TestCheckMethodCall',
  31227. LinesToStr([ // statements
  31228. 'rtl.createClass(this, "TObject", null, function () {',
  31229. ' this.$init = function () {',
  31230. ' };',
  31231. ' this.$final = function () {',
  31232. ' };',
  31233. ' this.DoIt = function () {',
  31234. ' rtl.checkMethodCall(this,$mod.TObject);',
  31235. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31236. ' };',
  31237. '});',
  31238. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31239. '});',
  31240. 'this.o = null;',
  31241. 'this.c = null;',
  31242. 'this.b = null;',
  31243. 'this.bc = null;',
  31244. '']),
  31245. LinesToStr([ // $mod.$main
  31246. '$mod.o.DoIt();',
  31247. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31248. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31249. '']));
  31250. end;
  31251. procedure TTestModule.TestOverflowChecks_Int;
  31252. begin
  31253. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31254. StartProgram(false);
  31255. Add([
  31256. 'procedure DoIt;',
  31257. 'var',
  31258. ' b: byte;',
  31259. ' n: nativeint;',
  31260. ' u: nativeuint;',
  31261. ' c: currency;',
  31262. 'begin',
  31263. ' n:=n+n;',
  31264. ' n:=n-n;',
  31265. ' n:=n+b;',
  31266. ' n:=b-n;',
  31267. ' n:=n*n;',
  31268. ' n:=n*u;',
  31269. ' c:=c+b;',
  31270. ' c:=b+c;',
  31271. ' c:=c*b;',
  31272. ' c:=b*c;',
  31273. 'end;',
  31274. 'begin',
  31275. '']);
  31276. ConvertProgram;
  31277. CheckSource('TestOverflowChecks_Int',
  31278. LinesToStr([ // statements
  31279. 'this.DoIt = function () {',
  31280. ' var b = 0;',
  31281. ' var n = 0;',
  31282. ' var u = 0;',
  31283. ' var c = 0;',
  31284. ' n = rtl.oc(n + n);',
  31285. ' n = rtl.oc(n - n);',
  31286. ' n = rtl.oc(n + b);',
  31287. ' n = rtl.oc(b - n);',
  31288. ' n = rtl.oc(n * n);',
  31289. ' n = rtl.oc(n * u);',
  31290. ' c = rtl.oc(c + (b * 10000));',
  31291. ' c = rtl.oc((b * 10000) + c);',
  31292. ' c = rtl.oc(c * b);',
  31293. ' c = rtl.oc(b * c);',
  31294. '};',
  31295. '']),
  31296. LinesToStr([ // $mod.$main
  31297. '']));
  31298. end;
  31299. procedure TTestModule.TestRangeChecks_AssignInt;
  31300. begin
  31301. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31302. StartProgram(false);
  31303. Add([
  31304. '{$R+}',
  31305. 'var',
  31306. ' b: byte = 2;',
  31307. ' w: word = 3;',
  31308. 'procedure DoIt(p: byte);',
  31309. 'begin',
  31310. ' b:=w;',
  31311. ' b+=w;',
  31312. ' b:=1;',
  31313. 'end;',
  31314. '{$R-}',
  31315. 'procedure DoSome;',
  31316. 'begin',
  31317. ' DoIt(w);',
  31318. ' b:=w;',
  31319. ' b:=2;',
  31320. 'end;',
  31321. 'begin',
  31322. '{$R+}',
  31323. '']);
  31324. ConvertProgram;
  31325. CheckSource('TestRangeChecks_AssignInt',
  31326. LinesToStr([ // statements
  31327. 'this.b = 2;',
  31328. 'this.w = 3;',
  31329. 'this.DoIt = function (p) {',
  31330. ' rtl.rc(p, 0, 255);',
  31331. ' $mod.b = rtl.rc($mod.w,0,255);',
  31332. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31333. ' $mod.b = 1;',
  31334. '};',
  31335. 'this.DoSome = function () {',
  31336. ' $mod.DoIt($mod.w);',
  31337. ' $mod.b = $mod.w;',
  31338. ' $mod.b = 2;',
  31339. '};',
  31340. '']),
  31341. LinesToStr([ // $mod.$main
  31342. '']));
  31343. end;
  31344. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31345. begin
  31346. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31347. StartProgram(false);
  31348. Add([
  31349. '{$R+}',
  31350. 'type Ten = 1..10;',
  31351. 'var',
  31352. ' b: Ten = 2;',
  31353. ' w: Ten = 3;',
  31354. 'procedure DoIt(p: Ten);',
  31355. 'begin',
  31356. ' b:=w;',
  31357. ' b+=w;',
  31358. ' b:=1;',
  31359. 'end;',
  31360. '{$R-}',
  31361. 'procedure DoSome;',
  31362. 'begin',
  31363. ' DoIt(w);',
  31364. ' b:=w;',
  31365. ' b:=2;',
  31366. 'end;',
  31367. 'begin',
  31368. '{$R+}',
  31369. '']);
  31370. ConvertProgram;
  31371. CheckSource('TestRangeChecks_AssignIntRange',
  31372. LinesToStr([ // statements
  31373. 'this.b = 2;',
  31374. 'this.w = 3;',
  31375. 'this.DoIt = function (p) {',
  31376. ' rtl.rc(p, 1, 10);',
  31377. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31378. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31379. ' $mod.b = 1;',
  31380. '};',
  31381. 'this.DoSome = function () {',
  31382. ' $mod.DoIt($mod.w);',
  31383. ' $mod.b = $mod.w;',
  31384. ' $mod.b = 2;',
  31385. '};',
  31386. '']),
  31387. LinesToStr([ // $mod.$main
  31388. '']));
  31389. end;
  31390. procedure TTestModule.TestRangeChecks_AssignEnum;
  31391. begin
  31392. StartProgram(false);
  31393. Add([
  31394. '{$R+}',
  31395. 'type TEnum = (red,green);',
  31396. 'var',
  31397. ' e: TEnum = red;',
  31398. 'procedure DoIt(p: TEnum);',
  31399. 'begin',
  31400. ' e:=p;',
  31401. ' p:=TEnum(0);',
  31402. ' p:=succ(e);',
  31403. 'end;',
  31404. '{$R-}',
  31405. 'procedure DoSome;',
  31406. 'begin',
  31407. ' DoIt(e);',
  31408. ' e:=TEnum(1);',
  31409. ' e:=pred(e);',
  31410. 'end;',
  31411. 'begin',
  31412. '{$R+}',
  31413. '']);
  31414. ConvertProgram;
  31415. CheckSource('TestRangeChecks_AssignEnum',
  31416. LinesToStr([ // statements
  31417. 'this.TEnum = {',
  31418. ' "0": "red",',
  31419. ' red: 0,',
  31420. ' "1": "green",',
  31421. ' green: 1',
  31422. '};',
  31423. 'this.e = this.TEnum.red;',
  31424. 'this.DoIt = function (p) {',
  31425. ' rtl.rc(p, 0, 1);',
  31426. ' $mod.e = rtl.rc(p, 0, 1);',
  31427. ' p = 0;',
  31428. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31429. '};',
  31430. 'this.DoSome = function () {',
  31431. ' $mod.DoIt($mod.e);',
  31432. ' $mod.e = 1;',
  31433. ' $mod.e = $mod.e - 1;',
  31434. '};',
  31435. '']),
  31436. LinesToStr([ // $mod.$main
  31437. '']));
  31438. end;
  31439. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31440. begin
  31441. StartProgram(false);
  31442. Add([
  31443. '{$R+}',
  31444. 'type',
  31445. ' TEnum = (red,green);',
  31446. ' TEnumRg = red..green;',
  31447. 'var',
  31448. ' e: TEnumRg = red;',
  31449. 'procedure DoIt(p: TEnumRg);',
  31450. 'begin',
  31451. ' e:=p;',
  31452. ' p:=TEnumRg(0);',
  31453. ' p:=succ(e);',
  31454. 'end;',
  31455. '{$R-}',
  31456. 'procedure DoSome;',
  31457. 'begin',
  31458. ' DoIt(e);',
  31459. ' e:=TEnum(1);',
  31460. ' e:=pred(e);',
  31461. 'end;',
  31462. 'begin',
  31463. '{$R+}',
  31464. '']);
  31465. ConvertProgram;
  31466. CheckSource('TestRangeChecks_AssignEnumRange',
  31467. LinesToStr([ // statements
  31468. 'this.TEnum = {',
  31469. ' "0": "red",',
  31470. ' red: 0,',
  31471. ' "1": "green",',
  31472. ' green: 1',
  31473. '};',
  31474. 'this.e = this.TEnum.red;',
  31475. 'this.DoIt = function (p) {',
  31476. ' rtl.rc(p, 0, 1);',
  31477. ' $mod.e = rtl.rc(p, 0, 1);',
  31478. ' p = 0;',
  31479. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31480. '};',
  31481. 'this.DoSome = function () {',
  31482. ' $mod.DoIt($mod.e);',
  31483. ' $mod.e = 1;',
  31484. ' $mod.e = $mod.e - 1;',
  31485. '};',
  31486. '']),
  31487. LinesToStr([ // $mod.$main
  31488. '']));
  31489. end;
  31490. procedure TTestModule.TestRangeChecks_AssignChar;
  31491. begin
  31492. StartProgram(false);
  31493. Add([
  31494. '{$R+}',
  31495. 'type',
  31496. ' TLetter = char;',
  31497. 'var',
  31498. ' b: TLetter = ''2'';',
  31499. ' w: TLetter = ''3'';',
  31500. 'procedure DoIt(p: TLetter);',
  31501. 'begin',
  31502. ' b:=w;',
  31503. ' b:=''1'';',
  31504. 'end;',
  31505. '{$R-}',
  31506. 'procedure DoSome;',
  31507. 'begin',
  31508. ' DoIt(w);',
  31509. ' b:=w;',
  31510. ' b:=''2'';',
  31511. 'end;',
  31512. 'begin',
  31513. '{$R+}',
  31514. '']);
  31515. ConvertProgram;
  31516. CheckSource('TestRangeChecks_AssignChar',
  31517. LinesToStr([ // statements
  31518. 'this.b = "2";',
  31519. 'this.w = "3";',
  31520. 'this.DoIt = function (p) {',
  31521. ' rtl.rcc(p, 0, 65535);',
  31522. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31523. ' $mod.b = "1";',
  31524. '};',
  31525. 'this.DoSome = function () {',
  31526. ' $mod.DoIt($mod.w);',
  31527. ' $mod.b = $mod.w;',
  31528. ' $mod.b = "2";',
  31529. '};',
  31530. '']),
  31531. LinesToStr([ // $mod.$main
  31532. '']));
  31533. end;
  31534. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31535. begin
  31536. StartProgram(false);
  31537. Add([
  31538. '{$R+}',
  31539. 'type TDigit = ''0''..''9'';',
  31540. 'var',
  31541. ' b: TDigit = ''2'';',
  31542. ' w: TDigit = ''3'';',
  31543. 'procedure DoIt(p: TDigit);',
  31544. 'begin',
  31545. ' b:=w;',
  31546. ' b:=''1'';',
  31547. 'end;',
  31548. '{$R-}',
  31549. 'procedure DoSome;',
  31550. 'begin',
  31551. ' DoIt(w);',
  31552. ' b:=w;',
  31553. ' b:=''2'';',
  31554. 'end;',
  31555. 'begin',
  31556. '{$R+}',
  31557. '']);
  31558. ConvertProgram;
  31559. CheckSource('TestRangeChecks_AssignCharRange',
  31560. LinesToStr([ // statements
  31561. 'this.b = "2";',
  31562. 'this.w = "3";',
  31563. 'this.DoIt = function (p) {',
  31564. ' rtl.rcc(p, 48, 57);',
  31565. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31566. ' $mod.b = "1";',
  31567. '};',
  31568. 'this.DoSome = function () {',
  31569. ' $mod.DoIt($mod.w);',
  31570. ' $mod.b = $mod.w;',
  31571. ' $mod.b = "2";',
  31572. '};',
  31573. '']),
  31574. LinesToStr([ // $mod.$main
  31575. '']));
  31576. end;
  31577. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31578. begin
  31579. StartProgram(false);
  31580. Add([
  31581. '{$R+}',
  31582. 'type',
  31583. ' Ten = 1..10;',
  31584. ' TArr = array of Ten;',
  31585. ' TArrArr = array of TArr;',
  31586. ' TArrByte = array[byte] of Ten;',
  31587. ' TArrChar = array[''0''..''9''] of Ten;',
  31588. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31589. ' TObject = class',
  31590. ' A: TArr;',
  31591. ' end;',
  31592. 'procedure DoIt;',
  31593. 'var',
  31594. ' Arr: TArr;',
  31595. ' ArrArr: TArrArr;',
  31596. ' ArrByte: TArrByte;',
  31597. ' ArrChar: TArrChar;',
  31598. ' ArrByteChar: TArrByteChar;',
  31599. ' i: Ten;',
  31600. ' c: char;',
  31601. ' o: tobject;',
  31602. 'begin',
  31603. ' i:=Arr[1];',
  31604. ' i:=ArrByteChar[1,''2''];',
  31605. ' Arr[1]:=Arr[1];',
  31606. ' Arr[i]:=Arr[i];',
  31607. ' ArrByte[3]:=ArrByte[3];',
  31608. ' ArrByte[i]:=ArrByte[i];',
  31609. ' ArrChar[''5'']:=ArrChar[''5''];',
  31610. ' ArrChar[c]:=ArrChar[c];',
  31611. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31612. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31613. ' o.a[i]:=o.a[i];',
  31614. 'end;',
  31615. 'begin',
  31616. '']);
  31617. ConvertProgram;
  31618. CheckSource('TestRangeChecks_ArrayIndex',
  31619. LinesToStr([ // statements
  31620. 'rtl.createClass(this, "TObject", null, function () {',
  31621. ' this.$init = function () {',
  31622. ' this.A = [];',
  31623. ' };',
  31624. ' this.$final = function () {',
  31625. ' this.A = undefined;',
  31626. ' };',
  31627. '});',
  31628. 'this.DoIt = function () {',
  31629. ' var Arr = [];',
  31630. ' var ArrArr = [];',
  31631. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31632. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31633. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31634. ' var i = 0;',
  31635. ' var c = "";',
  31636. ' var o = null;',
  31637. ' i = rtl.rc(Arr[1], 1, 10);',
  31638. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31639. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31640. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31641. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31642. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31643. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31644. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31645. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31646. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31647. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31648. '};',
  31649. '']),
  31650. LinesToStr([ // $mod.$main
  31651. '']));
  31652. end;
  31653. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31654. begin
  31655. StartProgram(false);
  31656. Add([
  31657. '{$R+}',
  31658. 'type',
  31659. ' Ten = 1..10;',
  31660. ' TRec = record x: Ten end;',
  31661. ' TArr = array of TRec;',
  31662. ' TArrArr = array of TArr;',
  31663. ' TObject = class',
  31664. ' A: TArr;',
  31665. ' end;',
  31666. 'procedure DoIt;',
  31667. 'var',
  31668. ' Arr: TArr;',
  31669. ' ArrArr: TArrArr;',
  31670. ' i: Ten;',
  31671. ' o: tobject;',
  31672. 'begin',
  31673. ' Arr[1]:=Arr[1];',
  31674. ' Arr[i]:=Arr[i+1];',
  31675. ' o.a[i]:=o.a[i+2];',
  31676. 'end;',
  31677. 'begin',
  31678. '']);
  31679. ConvertProgram;
  31680. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31681. LinesToStr([ // statements
  31682. 'rtl.recNewT(this, "TRec", function () {',
  31683. ' this.x = 0;',
  31684. ' this.$eq = function (b) {',
  31685. ' return this.x === b.x;',
  31686. ' };',
  31687. ' this.$assign = function (s) {',
  31688. ' this.x = s.x;',
  31689. ' return this;',
  31690. ' };',
  31691. '});',
  31692. 'rtl.createClass(this, "TObject", null, function () {',
  31693. ' this.$init = function () {',
  31694. ' this.A = [];',
  31695. ' };',
  31696. ' this.$final = function () {',
  31697. ' this.A = undefined;',
  31698. ' };',
  31699. '});',
  31700. 'this.DoIt = function () {',
  31701. ' var Arr = [];',
  31702. ' var ArrArr = [];',
  31703. ' var i = 0;',
  31704. ' var o = null;',
  31705. ' Arr[1].$assign(Arr[1]);',
  31706. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31707. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31708. '};',
  31709. '']),
  31710. LinesToStr([ // $mod.$main
  31711. '']));
  31712. end;
  31713. procedure TTestModule.TestRangeChecks_StringIndex;
  31714. begin
  31715. StartProgram(false);
  31716. Add([
  31717. 'type',
  31718. ' TObject = class',
  31719. ' S: string;',
  31720. ' end;',
  31721. '{$R+}',
  31722. 'procedure DoIt(var h: string);',
  31723. 'var',
  31724. ' s: string;',
  31725. ' i: longint;',
  31726. ' c: char;',
  31727. ' o: tobject;',
  31728. 'begin',
  31729. ' c:=s[1];',
  31730. ' s[i]:=s[i];',
  31731. ' h[i]:=h[i];',
  31732. ' c:=o.s[i];',
  31733. ' o.s[i]:=c;',
  31734. 'end;',
  31735. 'begin',
  31736. '']);
  31737. ConvertProgram;
  31738. CheckSource('TestRangeChecks_StringIndex',
  31739. LinesToStr([ // statements
  31740. 'rtl.createClass(this, "TObject", null, function () {',
  31741. ' this.$init = function () {',
  31742. ' this.S = "";',
  31743. ' };',
  31744. ' this.$final = function () {',
  31745. ' };',
  31746. '});',
  31747. 'this.DoIt = function (h) {',
  31748. ' var s = "";',
  31749. ' var i = 0;',
  31750. ' var c = "";',
  31751. ' var o = null;',
  31752. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31753. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31754. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31755. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31756. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31757. '};',
  31758. '']),
  31759. LinesToStr([ // $mod.$main
  31760. '']));
  31761. end;
  31762. procedure TTestModule.TestRangeChecks_TypecastInt;
  31763. begin
  31764. StartProgram(false);
  31765. Add([
  31766. '{$R+}',
  31767. 'var',
  31768. ' i: nativeint;',
  31769. ' b: byte;',
  31770. ' sh: shortint;',
  31771. ' w: word;',
  31772. ' sm: smallint;',
  31773. ' lw: longword;',
  31774. ' li: longint;',
  31775. 'begin',
  31776. ' b:=12+byte(i);',
  31777. ' sh:=12+shortint(i);',
  31778. ' w:=12+word(i);',
  31779. ' sm:=12+smallint(i);',
  31780. ' lw:=12+longword(i);',
  31781. ' li:=12+longint(i);',
  31782. '']);
  31783. ConvertProgram;
  31784. CheckSource('TestRangeChecks_TypecastInt',
  31785. LinesToStr([
  31786. 'this.i = 0;',
  31787. 'this.b = 0;',
  31788. 'this.sh = 0;',
  31789. 'this.w = 0;',
  31790. 'this.sm = 0;',
  31791. 'this.lw = 0;',
  31792. 'this.li = 0;',
  31793. '']),
  31794. LinesToStr([
  31795. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31796. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31797. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31798. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31799. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31800. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31801. '']));
  31802. end;
  31803. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31804. begin
  31805. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31806. StartProgram(false);
  31807. Add([
  31808. '{$modeswitch typehelpers}',
  31809. '{$R+}',
  31810. 'type',
  31811. ' TObject = class',
  31812. ' FSize: byte;',
  31813. ' property Size: byte read FSize;',
  31814. ' end;',
  31815. ' THelper = type helper for byte',
  31816. ' procedure SetIt(w: word);',
  31817. ' end;',
  31818. 'procedure THelper.SetIt(w: word);',
  31819. 'begin',
  31820. ' Self:=w;',
  31821. 'end;',
  31822. 'function GetIt: byte;',
  31823. 'begin',
  31824. ' Result.SetIt(2);',
  31825. 'end;',
  31826. 'var',
  31827. ' b: byte = 3;',
  31828. ' o: TObject;',
  31829. 'begin',
  31830. ' b.SetIt(14);',
  31831. ' with b do SetIt(15);',
  31832. ' o.Size.SetIt(16);',
  31833. '']);
  31834. ConvertProgram;
  31835. CheckSource('TestRangeChecks_AssignInt',
  31836. LinesToStr([ // statements
  31837. 'rtl.createClass(this, "TObject", null, function () {',
  31838. ' this.$init = function () {',
  31839. ' this.FSize = 0;',
  31840. ' };',
  31841. ' this.$final = function () {',
  31842. ' };',
  31843. '});',
  31844. 'rtl.createHelper(this, "THelper", null, function () {',
  31845. ' this.SetIt = function (w) {',
  31846. ' rtl.rc(w, 0, 65535);',
  31847. ' this.set(w);',
  31848. ' };',
  31849. '});',
  31850. 'this.GetIt = function () {',
  31851. ' var Result = 0;',
  31852. ' $mod.THelper.SetIt.call({',
  31853. ' get: function () {',
  31854. ' return Result;',
  31855. ' },',
  31856. ' set: function (v) {',
  31857. ' rtl.rc(v, 0, 255);',
  31858. ' Result = v;',
  31859. ' }',
  31860. ' }, 2);',
  31861. ' return Result;',
  31862. '};',
  31863. 'this.b = 3;',
  31864. 'this.o = null;',
  31865. '']),
  31866. LinesToStr([ // $mod.$main
  31867. '$mod.THelper.SetIt.call({',
  31868. ' p: $mod,',
  31869. ' get: function () {',
  31870. ' return this.p.b;',
  31871. ' },',
  31872. ' set: function (v) {',
  31873. ' rtl.rc(v, 0, 255);',
  31874. ' this.p.b = v;',
  31875. ' }',
  31876. '}, 14);',
  31877. 'var $with = $mod.b;',
  31878. '$mod.THelper.SetIt.call({',
  31879. ' get: function () {',
  31880. ' return $with;',
  31881. ' },',
  31882. ' set: function (v) {',
  31883. ' rtl.rc(v, 0, 255);',
  31884. ' $with = v;',
  31885. ' }',
  31886. '}, 15);',
  31887. '$mod.THelper.SetIt.call({',
  31888. ' p: $mod.o,',
  31889. ' get: function () {',
  31890. ' return this.p.FSize;',
  31891. ' },',
  31892. ' set: function (v) {',
  31893. ' rtl.rc(v, 0, 255);',
  31894. ' this.p.FSize = v;',
  31895. ' }',
  31896. '}, 16);',
  31897. '']));
  31898. end;
  31899. procedure TTestModule.TestAsync_Proc;
  31900. begin
  31901. StartProgram(false);
  31902. Add([
  31903. 'procedure Fly(w: word = 1); async; forward;',
  31904. 'procedure Run(w: word = 2); async;',
  31905. 'begin',
  31906. ' Fly(w);',
  31907. ' Fly;',
  31908. ' await(Fly(w));',
  31909. ' await(Fly);',
  31910. 'end;',
  31911. 'procedure Fly(w: word); ',
  31912. 'begin',
  31913. 'end;',
  31914. 'begin',
  31915. ' Run;',
  31916. ' Run(3);',
  31917. '']);
  31918. CheckResolverUnexpectedHints();
  31919. ConvertProgram;
  31920. CheckSource('TestAsync_Proc',
  31921. LinesToStr([ // statements
  31922. 'this.Run = async function (w) {',
  31923. ' $mod.Fly(w);',
  31924. ' $mod.Fly(1);',
  31925. ' await $mod.Fly(w);',
  31926. ' await $mod.Fly(1);',
  31927. '};',
  31928. 'this.Fly = async function (w) {',
  31929. '};',
  31930. '']),
  31931. LinesToStr([
  31932. '$mod.Run(2);',
  31933. '$mod.Run(3);',
  31934. '']));
  31935. end;
  31936. procedure TTestModule.TestAsync_CallResultIsPromise;
  31937. begin
  31938. StartProgram(false);
  31939. Add([
  31940. '{$modeswitch externalclass}',
  31941. 'type',
  31942. ' TObject = class',
  31943. ' end;',
  31944. ' TJSPromise = class external name ''Promise''',
  31945. ' end;',
  31946. ' TBird = class',
  31947. ' function Fly: word; async; ',
  31948. ' end;',
  31949. 'function TBird.Fly: word; async; ',
  31950. 'begin',
  31951. ' Result:=3;',
  31952. ' Fly:=4+Result;',
  31953. ' if Result=5 then ;',
  31954. ' exit(6);',
  31955. 'end;',
  31956. 'function Run: word; async;',
  31957. 'begin',
  31958. ' Result:=11+Result;',
  31959. ' inc(Result);',
  31960. 'end;',
  31961. 'var',
  31962. ' p: TJSPromise;',
  31963. ' o: TBird;',
  31964. 'begin',
  31965. ' p:=Run;',
  31966. ' p:=Run();',
  31967. ' if Run=p then ;',
  31968. ' if p=Run then ;',
  31969. ' if Run()=p then ;',
  31970. ' if p=Run() then ;',
  31971. ' p:=o.Fly;',
  31972. ' p:=o.Fly();',
  31973. ' if o.Fly=p then ;',
  31974. ' if o.Fly()=p then ;',
  31975. ' with o do begin',
  31976. ' p:=Fly;',
  31977. ' p:=Fly();',
  31978. ' if Fly=p then ;',
  31979. ' if Fly()=p then ;',
  31980. ' end;',
  31981. '']);
  31982. CheckResolverUnexpectedHints();
  31983. ConvertProgram;
  31984. CheckSource('TestAsync_CallResultIsPromise',
  31985. LinesToStr([ // statements
  31986. 'rtl.createClass(this, "TObject", null, function () {',
  31987. ' this.$init = function () {',
  31988. ' };',
  31989. ' this.$final = function () {',
  31990. ' };',
  31991. '});',
  31992. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31993. ' this.Fly = async function () {',
  31994. ' var Result = 0;',
  31995. ' Result = 3;',
  31996. ' Result = 4 + Result;',
  31997. ' if (Result === 5) ;',
  31998. ' return 6;',
  31999. ' return Result;',
  32000. ' };',
  32001. '});',
  32002. 'this.Run = async function () {',
  32003. ' var Result = 0;',
  32004. ' Result = 11 + Result;',
  32005. ' Result += 1;',
  32006. ' return Result;',
  32007. '};',
  32008. 'this.p = null;',
  32009. 'this.o = null;',
  32010. '']),
  32011. LinesToStr([
  32012. '$mod.p = $mod.Run();',
  32013. '$mod.p = $mod.Run();',
  32014. 'if ($mod.Run() === $mod.p) ;',
  32015. 'if ($mod.p === $mod.Run()) ;',
  32016. 'if ($mod.Run() === $mod.p) ;',
  32017. 'if ($mod.p === $mod.Run()) ;',
  32018. '$mod.p = $mod.o.Fly();',
  32019. '$mod.p = $mod.o.Fly();',
  32020. 'if ($mod.o.Fly() === $mod.p) ;',
  32021. 'if ($mod.o.Fly() === $mod.p) ;',
  32022. 'var $with = $mod.o;',
  32023. '$mod.p = $with.Fly();',
  32024. '$mod.p = $with.Fly();',
  32025. 'if ($with.Fly() === $mod.p) ;',
  32026. 'if ($with.Fly() === $mod.p) ;',
  32027. '']));
  32028. end;
  32029. procedure TTestModule.TestAsync_ConstructorFail;
  32030. begin
  32031. StartProgram(false);
  32032. Add([
  32033. 'type',
  32034. ' TObject = class',
  32035. ' end;',
  32036. ' TBird = class',
  32037. ' constructor Create; async;',
  32038. ' end;',
  32039. 'constructor TBird.Create; async;',
  32040. 'begin',
  32041. 'end;',
  32042. 'begin',
  32043. '']);
  32044. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32045. ConvertProgram;
  32046. end;
  32047. procedure TTestModule.TestAsync_PropertyGetterFail;
  32048. begin
  32049. StartProgram(false);
  32050. Add([
  32051. 'type',
  32052. ' TObject = class',
  32053. ' end;',
  32054. ' TBird = class',
  32055. ' function GetSize: word; async;',
  32056. ' property Size: word read GetSize;',
  32057. ' end;',
  32058. 'function TBird.GetSize: word; async;',
  32059. 'begin',
  32060. 'end;',
  32061. 'begin',
  32062. '']);
  32063. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32064. ConvertProgram;
  32065. end;
  32066. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32067. begin
  32068. StartProgram(false);
  32069. Add([
  32070. 'procedure Run; async;',
  32071. 'begin',
  32072. ' await(word,1);',
  32073. 'end;',
  32074. 'begin',
  32075. '']);
  32076. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32077. ConvertProgram;
  32078. end;
  32079. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32080. begin
  32081. StartProgram(false);
  32082. Add([
  32083. 'type',
  32084. ' TObject = class',
  32085. ' end;',
  32086. ' TBird = class',
  32087. ' end;',
  32088. 'function Fly: TObject; async;',
  32089. 'begin',
  32090. 'end;',
  32091. 'procedure Run; async;',
  32092. 'begin',
  32093. ' await(TBird,Fly);',
  32094. 'end;',
  32095. 'begin',
  32096. '']);
  32097. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32098. ConvertProgram;
  32099. end;
  32100. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32101. begin
  32102. StartProgram(false);
  32103. Add([
  32104. 'procedure Crawl(w: double); ',
  32105. 'begin',
  32106. 'end;',
  32107. 'procedure Run(w: double);',
  32108. 'begin',
  32109. ' await(Crawl(w));',
  32110. 'end;',
  32111. 'begin',
  32112. ' Run(1);']);
  32113. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32114. ConvertProgram;
  32115. end;
  32116. procedure TTestModule.TestAWait_IntegerFail;
  32117. begin
  32118. StartProgram(false);
  32119. Add([
  32120. 'function Run: word;',
  32121. 'begin',
  32122. 'end;',
  32123. 'procedure Fly(w: word); async;',
  32124. 'begin',
  32125. ' await(Run());',
  32126. 'end;',
  32127. 'begin',
  32128. ' Fly(1);']);
  32129. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32130. ConvertProgram;
  32131. end;
  32132. procedure TTestModule.TestAWait_ExternalClassPromise;
  32133. begin
  32134. StartProgram(false);
  32135. Add([
  32136. '{$modeswitch externalclass}',
  32137. 'type',
  32138. ' TJSPromise = class external name ''Promise''',
  32139. ' end;',
  32140. ' TJSThenable = class external name ''Thenable''',
  32141. ' end;',
  32142. 'function Fly(w: word): TJSPromise;',
  32143. 'begin',
  32144. 'end;',
  32145. 'function Jump(w: word): word; async;',
  32146. 'begin',
  32147. 'end;',
  32148. 'function Eat(w: word): TJSPromise; async;',
  32149. 'begin',
  32150. 'end;',
  32151. 'function Run(d: double): word; async;',
  32152. 'var',
  32153. ' p: TJSPromise;',
  32154. 'begin',
  32155. ' Result:=await(word,p);', // promise needs type
  32156. ' Result:=await(word,Fly(3));', // promise needs type
  32157. ' Result:=await(Jump(4));', // async non promise must omit the type
  32158. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32159. ' Result:=await(word,Eat(6));', // promise needs type
  32160. 'end;',
  32161. 'begin',
  32162. '']);
  32163. ConvertProgram;
  32164. CheckSource('TestAWait_ExternalClassPromise',
  32165. LinesToStr([ // statements
  32166. 'this.Fly = function (w) {',
  32167. ' var Result = null;',
  32168. ' return Result;',
  32169. '};',
  32170. 'this.Jump = async function (w) {',
  32171. ' var Result = 0;',
  32172. ' return Result;',
  32173. '};',
  32174. 'this.Eat = async function (w) {',
  32175. ' var Result = null;',
  32176. ' return Result;',
  32177. '};',
  32178. 'this.Run = async function (d) {',
  32179. ' var Result = 0;',
  32180. ' var p = null;',
  32181. ' Result = await p;',
  32182. ' Result = await $mod.Fly(3);',
  32183. ' Result = await $mod.Jump(4);',
  32184. ' Result = await $mod.Jump(5);',
  32185. ' Result = await $mod.Eat(6);',
  32186. ' return Result;',
  32187. '};',
  32188. '']),
  32189. LinesToStr([
  32190. ]));
  32191. CheckResolverUnexpectedHints();
  32192. end;
  32193. procedure TTestModule.TestAWait_JSValue;
  32194. begin
  32195. StartProgram(false);
  32196. Add([
  32197. '{$modeswitch externalclass}',
  32198. 'type',
  32199. ' TJSPromise = class external name ''Promise''',
  32200. ' end;',
  32201. 'function Fly(w: word): jsvalue; async;',
  32202. 'begin',
  32203. 'end;',
  32204. 'function Run(d: jsvalue; var e): word; async;',
  32205. 'begin',
  32206. ' Result:=await(word,d);', // promise needs type
  32207. ' d:=await(Fly(4));', // async non promise must omit the type
  32208. ' Result:=await(word,e);', // promise needs type
  32209. 'end;',
  32210. 'begin',
  32211. '']);
  32212. ConvertProgram;
  32213. CheckSource('TestAWait_JSValue',
  32214. LinesToStr([ // statements
  32215. 'this.Fly = async function (w) {',
  32216. ' var Result = undefined;',
  32217. ' return Result;',
  32218. '};',
  32219. 'this.Run = async function (d, e) {',
  32220. ' var Result = 0;',
  32221. ' Result = await d;',
  32222. ' d = await $mod.Fly(4);',
  32223. ' Result = await e.get();',
  32224. ' return Result;',
  32225. '};',
  32226. '']),
  32227. LinesToStr([
  32228. ]));
  32229. CheckResolverUnexpectedHints();
  32230. end;
  32231. procedure TTestModule.TestAWait_Result;
  32232. begin
  32233. StartProgram(false);
  32234. Add([
  32235. '{$modeswitch externalclass}',
  32236. 'type',
  32237. ' TJSPromise = class external name ''Promise''',
  32238. ' end;',
  32239. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32240. 'begin',
  32241. 'end;',
  32242. 'function Run(d: double = 1.6): word; async;',
  32243. 'begin',
  32244. ' Result:=await(word,Crawl);',
  32245. ' Result:=await(word,Crawl(4.5));',
  32246. ' Result:=await(Run);',
  32247. ' Result:=await(Run(6.7));',
  32248. 'end;',
  32249. 'begin',
  32250. ' Run(1);']);
  32251. ConvertProgram;
  32252. CheckSource('TestAWait_Result',
  32253. LinesToStr([ // statements
  32254. 'this.Crawl = function (d) {',
  32255. ' var Result = null;',
  32256. ' return Result;',
  32257. '};',
  32258. 'this.Run = async function (d) {',
  32259. ' var Result = 0;',
  32260. ' Result = await $mod.Crawl(1.3);',
  32261. ' Result = await $mod.Crawl(4.5);',
  32262. ' Result = await $mod.Run(1.6);',
  32263. ' Result = await $mod.Run(6.7);',
  32264. ' return Result;',
  32265. '};',
  32266. '']),
  32267. LinesToStr([
  32268. '$mod.Run(1);'
  32269. ]));
  32270. CheckResolverUnexpectedHints();
  32271. end;
  32272. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32273. begin
  32274. StartProgram(false);
  32275. Add([
  32276. '{$mode objfpc}',
  32277. '{$modeswitch externalclass}',
  32278. 'type',
  32279. ' TJSPromise = class external name ''Promise''',
  32280. ' end;',
  32281. 'function Run: TJSPromise; async;',
  32282. 'begin',
  32283. 'end;',
  32284. 'procedure Fly(w: word); async;',
  32285. 'begin',
  32286. ' await(Run());',
  32287. 'end;',
  32288. 'begin',
  32289. ' Fly(1);']);
  32290. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32291. nWrongNumberOfParametersForCallTo);
  32292. ConvertProgram;
  32293. end;
  32294. procedure TTestModule.TestAsync_AnonymousProc;
  32295. begin
  32296. StartProgram(false);
  32297. Add([
  32298. '{$mode objfpc}',
  32299. '{$modeswitch externalclass}',
  32300. 'type',
  32301. ' TJSPromise = class external name ''Promise''',
  32302. ' end;',
  32303. 'type',
  32304. ' TFunc = reference to function(x: double): word; async;',
  32305. 'function Crawl(d: double = 1.3): word; async;',
  32306. 'begin',
  32307. 'end;',
  32308. 'var Func: TFunc;',
  32309. 'begin',
  32310. ' Func:=function(c:double):word async begin',
  32311. ' Result:=await(Crawl(c));',
  32312. ' end;',
  32313. ' Func:=function(c:double):word async assembler asm',
  32314. ' end;',
  32315. ' ']);
  32316. ConvertProgram;
  32317. CheckSource('TestAsync_AnonymousProc',
  32318. LinesToStr([ // statements
  32319. 'this.Crawl = async function (d) {',
  32320. ' var Result = 0;',
  32321. ' return Result;',
  32322. '};',
  32323. 'this.Func = null;',
  32324. '']),
  32325. LinesToStr([
  32326. '$mod.Func = async function (c) {',
  32327. ' var Result = 0;',
  32328. ' Result = await $mod.Crawl(c);',
  32329. ' return Result;',
  32330. '};',
  32331. '$mod.Func = async function (c) {',
  32332. '};',
  32333. '']));
  32334. CheckResolverUnexpectedHints();
  32335. end;
  32336. procedure TTestModule.TestAsync_ProcType;
  32337. begin
  32338. StartProgram(false);
  32339. Add([
  32340. '{$mode objfpc}',
  32341. 'type',
  32342. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32343. ' TFunc = function(x: double = 1.1): word; async;',
  32344. ' TProc = procedure(x: longint = 7); async;',
  32345. 'function Crawl(d: double): word; async;',
  32346. 'begin',
  32347. 'end;',
  32348. 'procedure Run(e:longint); async;',
  32349. 'begin',
  32350. 'end;',
  32351. 'procedure Fly(p: TProc); async;',
  32352. 'begin',
  32353. ' await(p);',
  32354. ' await(p());',
  32355. 'end;',
  32356. 'var',
  32357. ' RefFunc: TRefFunc;',
  32358. ' Func: TFunc;',
  32359. ' Proc, ProcB: TProc;',
  32360. 'begin',
  32361. ' Func:=@Crawl;',
  32362. ' RefFunc:=@Crawl;',
  32363. ' RefFunc:=function(c:double):word async begin',
  32364. ' Result:=await(RefFunc);',
  32365. ' Result:=await(RefFunc());',
  32366. ' Result:=await(Func);',
  32367. ' Result:=await(Func());',
  32368. ' await(Proc);',
  32369. ' await(Proc());',
  32370. ' await(Proc(13));',
  32371. ' end;',
  32372. ' Proc:=@Run;',
  32373. ' if Proc=ProcB then ;',
  32374. ' ']);
  32375. ConvertProgram;
  32376. CheckResolverUnexpectedHints();
  32377. CheckSource('TestAsync_ProcType',
  32378. LinesToStr([ // statements
  32379. 'this.Crawl = async function (d) {',
  32380. ' var Result = 0;',
  32381. ' return Result;',
  32382. '};',
  32383. 'this.Run = async function (e) {',
  32384. '};',
  32385. 'this.Fly = async function (p) {',
  32386. ' await p(7);',
  32387. ' await p(7);',
  32388. '};',
  32389. 'this.RefFunc = null;',
  32390. 'this.Func = null;',
  32391. 'this.Proc = null;',
  32392. 'this.ProcB = null;',
  32393. '']),
  32394. LinesToStr([
  32395. '$mod.Func = $mod.Crawl;',
  32396. '$mod.RefFunc = $mod.Crawl;',
  32397. '$mod.RefFunc = async function (c) {',
  32398. ' var Result = 0;',
  32399. ' Result = await $mod.RefFunc(1.3);',
  32400. ' Result = await $mod.RefFunc(1.3);',
  32401. ' Result = await $mod.Func(1.1);',
  32402. ' Result = await $mod.Func(1.1);',
  32403. ' await $mod.Proc(7);',
  32404. ' await $mod.Proc(7);',
  32405. ' await $mod.Proc(13);',
  32406. ' return Result;',
  32407. '};',
  32408. '$mod.Proc = $mod.Run;',
  32409. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32410. '']));
  32411. end;
  32412. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32413. begin
  32414. StartProgram(false);
  32415. Add([
  32416. '{$mode objfpc}',
  32417. 'type',
  32418. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32419. 'function Crawl(d: double): word; async;',
  32420. 'begin',
  32421. 'end;',
  32422. 'var',
  32423. ' RefFunc: TRefFunc;',
  32424. 'begin',
  32425. ' RefFunc:=@Crawl;',
  32426. ' ']);
  32427. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32428. ConvertProgram;
  32429. end;
  32430. procedure TTestModule.TestAsync_Inherited;
  32431. begin
  32432. StartProgram(false);
  32433. Add([
  32434. '{$mode objfpc}',
  32435. '{$modeswitch externalclass}',
  32436. 'type',
  32437. ' TJSPromise = class external name ''Promise''',
  32438. ' end;',
  32439. ' TObject = class',
  32440. ' function Run(w: word = 3): word; async; virtual;',
  32441. ' end;',
  32442. ' TBird = class',
  32443. ' function Run(w: word = 3): word; async; override;',
  32444. ' end;',
  32445. 'function TObject.Run(w: word = 3): word; async;',
  32446. 'begin',
  32447. 'end;',
  32448. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32449. 'var p: TJSPromise;',
  32450. 'begin',
  32451. ' p:=inherited;',
  32452. ' p:=inherited Run;',
  32453. ' p:=inherited Run();',
  32454. ' p:=inherited Run(4);',
  32455. ' exit(p);',
  32456. ' exit(inherited);',
  32457. ' exit(inherited Run);',
  32458. ' exit(inherited Run(5));',
  32459. ' exit(6);',
  32460. 'end;',
  32461. 'begin',
  32462. ' ']);
  32463. ConvertProgram;
  32464. CheckSource('TestAsync_Inherited',
  32465. LinesToStr([ // statements
  32466. 'rtl.createClass(this, "TObject", null, function () {',
  32467. ' this.$init = function () {',
  32468. ' };',
  32469. ' this.$final = function () {',
  32470. ' };',
  32471. ' this.Run = async function (w) {',
  32472. ' var Result = 0;',
  32473. ' return Result;',
  32474. ' };',
  32475. '});',
  32476. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32477. ' this.Run = async function (w) {',
  32478. ' var Result = 0;',
  32479. ' var p = null;',
  32480. ' p = $mod.TObject.Run.apply(this, arguments);',
  32481. ' p = $mod.TObject.Run.call(this, 3);',
  32482. ' p = $mod.TObject.Run.call(this, 3);',
  32483. ' p = $mod.TObject.Run.call(this, 4);',
  32484. ' return p;',
  32485. ' return $mod.TObject.Run.apply(this, arguments);',
  32486. ' return $mod.TObject.Run.call(this, 3);',
  32487. ' return $mod.TObject.Run.call(this, 5);',
  32488. ' return 6;',
  32489. ' return Result;',
  32490. ' };',
  32491. '});',
  32492. '']),
  32493. LinesToStr([
  32494. '']));
  32495. CheckResolverUnexpectedHints();
  32496. end;
  32497. procedure TTestModule.TestAsync_ClassInterface;
  32498. begin
  32499. StartProgram(false);
  32500. Add([
  32501. '{$mode objfpc}',
  32502. '{$modeswitch externalclass}',
  32503. 'type',
  32504. ' TJSPromise = class external name ''Promise''',
  32505. ' end;',
  32506. ' IUnknown = interface',
  32507. ' function _AddRef: longint;',
  32508. ' function _Release: longint;',
  32509. ' end;',
  32510. 'function Say(i: IUnknown): IUnknown; async;',
  32511. 'begin',
  32512. 'end;',
  32513. 'function Run: IUnknown; async;',
  32514. 'begin',
  32515. ' Result:=await(Run);',
  32516. ' Result:=await(Run());',
  32517. ' Result:=await(Run) as IUnknown;',
  32518. ' Result:=await(Say(nil));',
  32519. ' Result:=await(Say(await(Run())));',
  32520. ' Result:=await(Say(await(Run()) as IUnknown));',
  32521. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32522. 'end;',
  32523. 'procedure Fly;',
  32524. 'var p: TJSPromise;',
  32525. 'begin',
  32526. ' Run;',
  32527. ' Run();',
  32528. ' p:=Run;',
  32529. ' p:=Run();',
  32530. 'end;',
  32531. 'begin',
  32532. ' ']);
  32533. ConvertProgram;
  32534. CheckSource('TestAsync_ClassInterface',
  32535. LinesToStr([ // statements
  32536. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32537. 'this.Say = async function (i) {',
  32538. ' var Result = null;',
  32539. ' return Result;',
  32540. '};',
  32541. 'this.Run = async function () {',
  32542. ' var Result = null;',
  32543. ' var $ok = false;',
  32544. ' try {',
  32545. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32546. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32547. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32548. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32549. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32550. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32551. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32552. ' $ok = true;',
  32553. ' } finally {',
  32554. ' if (!$ok) rtl._Release(Result);',
  32555. ' };',
  32556. ' return Result;',
  32557. '};',
  32558. 'this.Fly = function () {',
  32559. ' var p = null;',
  32560. ' $mod.Run();',
  32561. ' $mod.Run();',
  32562. ' p = $mod.Run();',
  32563. ' p = $mod.Run();',
  32564. '};',
  32565. '']),
  32566. LinesToStr([
  32567. '']));
  32568. CheckResolverUnexpectedHints();
  32569. end;
  32570. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32571. begin
  32572. StartProgram(true,[supTInterfacedObject]);
  32573. Add([
  32574. '{$mode objfpc}',
  32575. '{$modeswitch externalclass}',
  32576. 'type',
  32577. ' TJSPromise = class external name ''Promise''',
  32578. ' end;',
  32579. ' IBird = interface',
  32580. ' procedure Run;',
  32581. ' end;',
  32582. ' TBird = class(TInterfacedObject,IBird)',
  32583. ' procedure Run; async;',
  32584. ' end;',
  32585. 'procedure TBird.Run;',
  32586. 'begin',
  32587. 'end;',
  32588. 'begin',
  32589. ' ']);
  32590. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32591. ConvertProgram;
  32592. end;
  32593. procedure TTestModule.TestLibrary_Empty;
  32594. begin
  32595. StartLibrary(false);
  32596. Add([
  32597. '']);
  32598. ConvertLibrary;
  32599. CheckSource('TestLibrary_Empty',
  32600. LinesToStr([ // statements
  32601. '']),
  32602. LinesToStr([
  32603. '']));
  32604. CheckResolverUnexpectedHints();
  32605. end;
  32606. procedure TTestModule.TestLibrary_ExportFunc;
  32607. begin
  32608. exit;
  32609. StartLibrary(false);
  32610. Add([
  32611. 'procedure Run(w: word);',
  32612. 'begin',
  32613. 'end;',
  32614. 'exports',
  32615. ' Run,',
  32616. ' run name ''Foo'';',
  32617. '']);
  32618. ConvertLibrary;
  32619. CheckSource('TestLibrary_ExportFunc',
  32620. LinesToStr([ // statements
  32621. '']),
  32622. LinesToStr([
  32623. '']));
  32624. CheckResolverUnexpectedHints();
  32625. end;
  32626. Initialization
  32627. RegisterTests([TTestModule]);
  32628. end.