tcmodules.pas 869 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130
  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. FHintMsgs: TObjectList; // list of TTestHintMessage
  110. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  111. FJSRegModuleCall: TJSCallExpression;
  112. FScanner: TPas2jsPasScanner;
  113. FSkipTests: boolean;
  114. FSource: TStringList;
  115. FFirstPasStatement: TPasImplBlock;
  116. FWithTypeInfo: boolean;
  117. {$IFDEF EnablePasTreeGlobalRefCount}
  118. FElementRefCountAtSetup: int64;
  119. {$ENDIF}
  120. function GetMsgCount: integer;
  121. function GetMsgs(Index: integer): TTestHintMessage;
  122. function GetResolverCount: integer;
  123. function GetResolvers(Index: integer): TTestEnginePasResolver;
  124. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  125. procedure OnParserLog(Sender: TObject; const Msg: String);
  126. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  127. procedure OnScannerLog(Sender: TObject; const Msg: String);
  128. procedure SetWithTypeInfo(const AValue: boolean);
  129. protected
  130. procedure SetUp; override;
  131. function CreateConverter: TPasToJSConverter; virtual;
  132. function LoadUnit(const aUnitName: String): TPasModule;
  133. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  134. procedure TearDown; override;
  135. Procedure Add(Line: string); virtual;
  136. Procedure Add(const Lines: array of string);
  137. Procedure StartParsing; virtual;
  138. procedure ParseModuleQueue; virtual;
  139. procedure ParseModule; virtual;
  140. procedure ParseProgram; virtual;
  141. procedure ParseUnit; virtual;
  142. protected
  143. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  146. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  147. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  148. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  149. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  151. procedure ConvertModule; virtual;
  152. procedure ConvertProgram; virtual;
  153. procedure ConvertUnit; virtual;
  154. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  155. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  156. function GetDottedIdentifier(El: TJSElement): string;
  157. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  158. ImplStatements: string = ''); virtual;
  159. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  160. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  161. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  162. Msg: string; Marker: PSrcMarker = nil); virtual;
  163. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  164. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  167. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  168. function IsErrorExpected(E: Exception): boolean;
  169. procedure HandleScannerError(E: EScannerError);
  170. procedure HandleParserError(E: EParserError);
  171. procedure HandlePasResolveError(E: EPasResolve);
  172. procedure HandlePas2JSError(E: EPas2JS);
  173. procedure HandleException(E: Exception);
  174. procedure FailException(E: Exception);
  175. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  176. function IndexOfResolver(const Filename: string): integer;
  177. function GetResolver(const Filename: string): TTestEnginePasResolver;
  178. function GetDefaultNamespace: string;
  179. property PasProgram: TPasProgram Read FPasProgram;
  180. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  181. property ResolverCount: integer read GetResolverCount;
  182. property Engine: TTestEnginePasResolver read FEngine;
  183. property Filename: string read FFilename;
  184. Property Module: TPasModule Read FModule;
  185. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  186. property Converter: TPasToJSConverter read FConverter;
  187. property JSSource: TStringList read FJSSource;
  188. property JSModule: TJSSourceElements read FJSModule;
  189. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  190. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  191. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  192. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  193. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  194. property JSInitBody: TJSFunctionBody read FJSInitBody;
  195. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  196. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  197. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  198. property SkipTests: boolean read FSkipTests write FSkipTests;
  199. public
  200. constructor Create; override;
  201. destructor Destroy; override;
  202. property Hub: TPas2JSResolverHub read FHub;
  203. property Source: TStringList read FSource;
  204. property FileResolver: TStreamResolver read FFileResolver;
  205. property Scanner: TPas2jsPasScanner read FScanner;
  206. property Parser: TTestPasParser read FParser;
  207. property MsgCount: integer read GetMsgCount;
  208. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  209. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  210. end;
  211. { TTestModule }
  212. TTestModule = class(TCustomTestModule)
  213. Published
  214. Procedure TestReservedWords;
  215. // program, units, includes
  216. Procedure TestEmptyProgram;
  217. Procedure TestEmptyProgramUseStrict;
  218. Procedure TestEmptyUnit;
  219. Procedure TestEmptyUnitUseStrict;
  220. Procedure TestDottedUnitNames;
  221. Procedure TestDottedUnitNameImpl;
  222. Procedure TestDottedUnitExpr;
  223. Procedure Test_ModeFPCFail;
  224. Procedure Test_ModeSwitchCBlocksFail;
  225. Procedure TestUnit_UseSystem;
  226. Procedure TestUnit_Intf1Impl2Intf1;
  227. Procedure TestIncludeVersion;
  228. // vars/const
  229. Procedure TestVarInt;
  230. Procedure TestVarBaseTypes;
  231. Procedure TestBaseTypeSingleFail;
  232. Procedure TestBaseTypeExtendedFail;
  233. Procedure TestConstBaseTypes;
  234. Procedure TestUnitImplVars;
  235. Procedure TestUnitImplConsts;
  236. Procedure TestUnitImplRecord;
  237. Procedure TestRenameJSNameConflict;
  238. Procedure TestLocalConst;
  239. Procedure TestVarExternal;
  240. Procedure TestVarExternalOtherUnit;
  241. Procedure TestVarAbsoluteFail;
  242. Procedure TestConstExternal;
  243. // numbers
  244. Procedure TestDouble;
  245. Procedure TestInteger;
  246. Procedure TestIntegerRange;
  247. Procedure TestIntegerTypecasts;
  248. Procedure TestInteger_BitwiseShrNativeInt;
  249. Procedure TestInteger_BitwiseShlNativeInt;
  250. Procedure TestInteger_SystemFunc;
  251. Procedure TestCurrency;
  252. Procedure TestForBoolDo;
  253. Procedure TestForIntDo;
  254. Procedure TestForIntInDo;
  255. // strings
  256. Procedure TestCharConst;
  257. Procedure TestChar_Compare;
  258. Procedure TestChar_BuiltInProcs;
  259. Procedure TestStringConst;
  260. Procedure TestStringConst_InvalidUTF16;
  261. Procedure TestStringConstSurrogate;
  262. Procedure TestString_Length;
  263. Procedure TestString_Compare;
  264. Procedure TestString_SetLength;
  265. Procedure TestString_CharAt;
  266. Procedure TestStringHMinusFail;
  267. Procedure TestStr;
  268. Procedure TestBaseType_AnsiStringFail;
  269. Procedure TestBaseType_WideStringFail;
  270. Procedure TestBaseType_ShortStringFail;
  271. Procedure TestBaseType_RawByteStringFail;
  272. Procedure TestTypeShortstring_Fail;
  273. Procedure TestCharSet_Custom;
  274. Procedure TestWideChar;
  275. Procedure TestForCharDo;
  276. Procedure TestForCharInDo;
  277. // alias types
  278. Procedure TestAliasTypeRef;
  279. Procedure TestTypeCast_BaseTypes;
  280. Procedure TestTypeCast_AliasBaseTypes;
  281. // functions
  282. Procedure TestEmptyProc;
  283. Procedure TestProcOneParam;
  284. Procedure TestFunctionWithoutParams;
  285. Procedure TestProcedureWithoutParams;
  286. Procedure TestPrgProcVar;
  287. Procedure TestProcTwoArgs;
  288. Procedure TestProc_DefaultValue;
  289. Procedure TestUnitProcVar;
  290. Procedure TestImplProc;
  291. Procedure TestFunctionResult;
  292. Procedure TestNestedProc;
  293. Procedure TestNestedProc_ResultString;
  294. Procedure TestForwardProc;
  295. Procedure TestNestedForwardProc;
  296. Procedure TestAssignFunctionResult;
  297. Procedure TestFunctionResultInCondition;
  298. Procedure TestFunctionResultInForLoop;
  299. Procedure TestFunctionResultInTypeCast;
  300. Procedure TestExit;
  301. Procedure TestExit_ResultInFinally;
  302. Procedure TestBreak;
  303. Procedure TestBreakAsVar;
  304. Procedure TestContinue;
  305. Procedure TestProc_External;
  306. Procedure TestProc_ExternalOtherUnit;
  307. Procedure TestProc_Asm;
  308. Procedure TestProc_Assembler;
  309. Procedure TestProc_VarParam;
  310. Procedure TestProc_VarParamString;
  311. Procedure TestProc_VarParamV;
  312. Procedure TestProc_Overload;
  313. Procedure TestProc_OverloadForward;
  314. Procedure TestProc_OverloadIntfImpl;
  315. Procedure TestProc_OverloadNested;
  316. Procedure TestProc_OverloadNestedForward;
  317. Procedure TestProc_OverloadUnitCycle;
  318. Procedure TestProc_Varargs;
  319. Procedure TestProc_ConstOrder;
  320. Procedure TestProc_DuplicateConst;
  321. Procedure TestProc_LocalVarAbsolute;
  322. Procedure TestProc_LocalVarInit;
  323. Procedure TestProc_ReservedWords;
  324. Procedure TestProc_ConstRefWord;
  325. // anonymous functions
  326. Procedure TestAnonymousProc_Assign_ObjFPC;
  327. Procedure TestAnonymousProc_Assign_Delphi;
  328. Procedure TestAnonymousProc_Arg;
  329. Procedure TestAnonymousProc_Typecast;
  330. Procedure TestAnonymousProc_With;
  331. Procedure TestAnonymousProc_ExceptOn;
  332. Procedure TestAnonymousProc_Nested;
  333. Procedure TestAnonymousProc_NestedAssignResult;
  334. Procedure TestAnonymousProc_Class;
  335. Procedure TestAnonymousProc_ForLoop;
  336. Procedure TestAnonymousProc_AsmDelphi;
  337. // enums, sets
  338. Procedure TestEnum_Name;
  339. Procedure TestEnum_Number;
  340. Procedure TestEnum_ConstFail;
  341. Procedure TestEnum_Functions;
  342. Procedure TestEnumRg_Functions;
  343. Procedure TestEnum_AsParams;
  344. Procedure TestEnumRange_Array;
  345. Procedure TestEnum_ForIn;
  346. Procedure TestEnum_ScopedNumber;
  347. Procedure TestEnum_InFunction;
  348. Procedure TestSet_Enum;
  349. Procedure TestSet_Operators;
  350. Procedure TestSet_Operator_In;
  351. Procedure TestSet_Functions;
  352. Procedure TestSet_PassAsArgClone;
  353. Procedure TestSet_AsParams;
  354. Procedure TestSet_Property;
  355. Procedure TestSet_EnumConst;
  356. Procedure TestSet_IntConst;
  357. Procedure TestSet_AnonymousEnumType;
  358. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  359. Procedure TestSet_ConstEnum;
  360. Procedure TestSet_ConstChar;
  361. Procedure TestSet_ConstInt;
  362. Procedure TestSet_InFunction;
  363. Procedure TestSet_ForIn;
  364. // statements
  365. Procedure TestNestBegin;
  366. Procedure TestIncDec;
  367. Procedure TestLoHiFpcMode;
  368. Procedure TestLoHiDelphiMode;
  369. Procedure TestAssignments;
  370. Procedure TestArithmeticOperators1;
  371. Procedure TestLogicalOperators;
  372. Procedure TestBitwiseOperators;
  373. Procedure TestBitwiseOperatorsLongword;
  374. Procedure TestFunctionInt;
  375. Procedure TestFunctionString;
  376. Procedure TestIfThen;
  377. Procedure TestForLoop;
  378. Procedure TestForLoopInsideFunction;
  379. Procedure TestForLoop_ReadVarAfter;
  380. Procedure TestForLoop_Nested;
  381. Procedure TestRepeatUntil;
  382. Procedure TestAsmBlock;
  383. Procedure TestAsmPas_Impl; // ToDo
  384. Procedure TestTryFinally;
  385. Procedure TestTryExcept;
  386. Procedure TestTryExcept_ReservedWords;
  387. Procedure TestIfThenRaiseElse;
  388. Procedure TestCaseOf;
  389. Procedure TestCaseOf_UseSwitch;
  390. Procedure TestCaseOfNoElse;
  391. Procedure TestCaseOfNoElse_UseSwitch;
  392. Procedure TestCaseOfRange;
  393. Procedure TestCaseOfString;
  394. Procedure TestCaseOfChar;
  395. Procedure TestCaseOfExternalClassConst;
  396. Procedure TestDebugger;
  397. // arrays
  398. Procedure TestArray_Dynamic;
  399. Procedure TestArray_Dynamic_Nil;
  400. Procedure TestArray_DynMultiDimensional;
  401. Procedure TestArray_DynamicAssign;
  402. Procedure TestArray_StaticInt;
  403. Procedure TestArray_StaticBool;
  404. Procedure TestArray_StaticChar;
  405. Procedure TestArray_StaticMultiDim;
  406. Procedure TestArray_StaticInFunction;
  407. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  408. Procedure TestArrayOfRecord;
  409. Procedure TestArray_StaticRecord;
  410. Procedure TestArrayOfSet;
  411. Procedure TestArray_DynAsParam;
  412. Procedure TestArray_StaticAsParam;
  413. Procedure TestArrayElement_AsParams;
  414. Procedure TestArrayElementFromFuncResult_AsParams;
  415. Procedure TestArrayEnumTypeRange;
  416. Procedure TestArray_SetLengthOutArg;
  417. Procedure TestArray_SetLengthProperty;
  418. Procedure TestArray_SetLengthMultiDim;
  419. Procedure TestArray_SetLengthDynOfStatic;
  420. Procedure TestArray_OpenArrayOfString;
  421. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  422. Procedure TestArray_ConstRef;
  423. Procedure TestArray_Concat;
  424. Procedure TestArray_Copy;
  425. Procedure TestArray_InsertDelete;
  426. Procedure TestArray_DynArrayConstObjFPC;
  427. Procedure TestArray_DynArrayConstDelphi;
  428. Procedure TestArray_ArrayLitAsParam;
  429. Procedure TestArray_ArrayLitMultiDimAsParam;
  430. Procedure TestArray_ArrayLitStaticAsParam;
  431. Procedure TestArray_ForInArrOfString;
  432. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  433. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  434. Procedure TestArrayOfConst_TVarRec;
  435. Procedure TestArrayOfConst_PassBaseTypes;
  436. Procedure TestArrayOfConst_PassObj;
  437. // record
  438. Procedure TestRecord_Empty;
  439. Procedure TestRecord_Var;
  440. Procedure TestRecord_VarExternal;
  441. Procedure TestRecord_WithDo;
  442. Procedure TestRecord_Assign;
  443. Procedure TestRecord_AsParams;
  444. Procedure TestRecord_ConstRef;
  445. Procedure TestRecordElement_AsParams;
  446. Procedure TestRecordElementFromFuncResult_AsParams;
  447. Procedure TestRecordElementFromWith_AsParams;
  448. Procedure TestRecord_Equal;
  449. Procedure TestRecord_JSValue;
  450. Procedure TestRecord_VariantFail;
  451. Procedure TestRecord_FieldArray;
  452. Procedure TestRecord_Const;
  453. Procedure TestRecord_TypecastFail;
  454. Procedure TestRecord_InFunction;
  455. Procedure TestRecord_AnonymousFail;
  456. // advanced record
  457. Procedure TestAdvRecord_Function;
  458. Procedure TestAdvRecord_Property;
  459. Procedure TestAdvRecord_PropertyDefault;
  460. Procedure TestAdvRecord_Property_ClassMethod;
  461. Procedure TestAdvRecord_Const;
  462. Procedure TestAdvRecord_ExternalField;
  463. Procedure TestAdvRecord_SubRecord;
  464. Procedure TestAdvRecord_SubClass;
  465. Procedure TestAdvRecord_SubInterfaceFail;
  466. Procedure TestAdvRecord_Constructor;
  467. Procedure TestAdvRecord_ClassConstructor_Program;
  468. Procedure TestAdvRecord_ClassConstructor_Unit;
  469. // classes
  470. Procedure TestClass_TObjectDefaultConstructor;
  471. Procedure TestClass_TObjectConstructorWithParams;
  472. Procedure TestClass_TObjectConstructorWithDefaultParam;
  473. Procedure TestClass_Var;
  474. Procedure TestClass_Method;
  475. Procedure TestClass_Implementation;
  476. Procedure TestClass_Inheritance;
  477. Procedure TestClass_TypeAlias;
  478. Procedure TestClass_AbstractMethod;
  479. Procedure TestClass_CallInherited_ProcNoParams;
  480. Procedure TestClass_CallInherited_WithParams;
  481. Procedure TestClasS_CallInheritedConstructor;
  482. Procedure TestClass_ClassVar_Assign;
  483. Procedure TestClass_CallClassMethod;
  484. Procedure TestClass_Property;
  485. Procedure TestClass_Property_ClassMethod;
  486. Procedure TestClass_Property_Indexed;
  487. Procedure TestClass_Property_IndexSpec;
  488. Procedure TestClass_PropertyOfTypeArray;
  489. Procedure TestClass_PropertyDefault;
  490. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  491. //Procedure TestClass_PropertyDefault;
  492. Procedure TestClass_PropertyOverride;
  493. Procedure TestClass_PropertyIncVisibility;
  494. Procedure TestClass_Assigned;
  495. Procedure TestClass_WithClassDoCreate;
  496. Procedure TestClass_WithClassInstDoProperty;
  497. Procedure TestClass_WithClassInstDoPropertyWithParams;
  498. Procedure TestClass_WithClassInstDoFunc;
  499. Procedure TestClass_TypeCast;
  500. Procedure TestClass_TypeCastUntypedParam;
  501. Procedure TestClass_Overloads;
  502. Procedure TestClass_OverloadsAncestor;
  503. Procedure TestClass_OverloadConstructor;
  504. Procedure TestClass_OverloadDelphiOverride;
  505. Procedure TestClass_ReintroduceVarDelphi;
  506. Procedure TestClass_ReintroducedVar;
  507. Procedure TestClass_RaiseDescendant;
  508. Procedure TestClass_ExternalMethod;
  509. Procedure TestClass_ExternalVirtualNameMismatchFail;
  510. Procedure TestClass_ExternalOverrideFail;
  511. Procedure TestClass_ExternalVar;
  512. Procedure TestClass_Const;
  513. Procedure TestClass_ConstEnum;
  514. Procedure TestClass_LocalConstDuplicate_Prg;
  515. Procedure TestClass_LocalConstDuplicate_Unit;
  516. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  517. Procedure TestClass_LocalVarSelfFail;
  518. Procedure TestClass_ArgSelfFail;
  519. Procedure TestClass_NestedProcSelf;
  520. Procedure TestClass_NestedProcSelf2;
  521. Procedure TestClass_NestedProcClassSelf;
  522. Procedure TestClass_NestedProcCallInherited;
  523. Procedure TestClass_TObjectFree;
  524. Procedure TestClass_TObjectFree_VarArg;
  525. Procedure TestClass_TObjectFreeNewInstance;
  526. Procedure TestClass_TObjectFreeLowerCase;
  527. Procedure TestClass_TObjectFreeFunctionFail;
  528. Procedure TestClass_TObjectFreePropertyFail;
  529. Procedure TestClass_ForIn;
  530. Procedure TestClass_DispatchMessage;
  531. Procedure TestClass_Message_DuplicateIntFail;
  532. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  533. // class of
  534. Procedure TestClassOf_Create;
  535. Procedure TestClassOf_Call;
  536. Procedure TestClassOf_Assign;
  537. Procedure TestClassOf_Is;
  538. Procedure TestClassOf_Compare;
  539. Procedure TestClassOf_ClassVar;
  540. Procedure TestClassOf_ClassMethod;
  541. Procedure TestClassOf_ClassProperty;
  542. Procedure TestClassOf_ClassMethodSelf;
  543. Procedure TestClassOf_TypeCast;
  544. Procedure TestClassOf_ImplicitFunctionCall;
  545. Procedure TestClassOf_Const;
  546. // nested class
  547. Procedure TestNestedClass_Alias;
  548. Procedure TestNestedClass_Record;
  549. Procedure TestNestedClass_Class;
  550. // external class
  551. Procedure TestExternalClass_Var;
  552. Procedure TestExternalClass_Const;
  553. Procedure TestExternalClass_Dollar;
  554. Procedure TestExternalClass_DuplicateVarFail;
  555. Procedure TestExternalClass_Method;
  556. Procedure TestExternalClass_ClassMethod;
  557. Procedure TestExternalClass_ClassMethodStatic;
  558. Procedure TestExternalClass_FunctionResultInTypeCast;
  559. Procedure TestExternalClass_NonExternalOverride;
  560. Procedure TestExternalClass_OverloadHint;
  561. Procedure TestExternalClass_SameNamePublishedProperty;
  562. Procedure TestExternalClass_Property;
  563. Procedure TestExternalClass_PropertyDate;
  564. Procedure TestExternalClass_ClassProperty;
  565. Procedure TestExternalClass_ClassOf;
  566. Procedure TestExternalClass_ClassOtherUnit;
  567. Procedure TestExternalClass_Is;
  568. Procedure TestExternalClass_As;
  569. Procedure TestExternalClass_DestructorFail;
  570. Procedure TestExternalClass_New;
  571. Procedure TestExternalClass_ClassOf_New;
  572. Procedure TestExternalClass_FuncClassOf_New;
  573. Procedure TestExternalClass_New_PasClassFail;
  574. Procedure TestExternalClass_New_PasClassBracketsFail;
  575. Procedure TestExternalClass_NewExtName;
  576. Procedure TestExternalClass_Constructor;
  577. Procedure TestExternalClass_ConstructorBrackets;
  578. Procedure TestExternalClass_LocalConstSameName;
  579. Procedure TestExternalClass_ReintroduceOverload;
  580. Procedure TestExternalClass_Inherited;
  581. Procedure TestExternalClass_PascalAncestorFail;
  582. Procedure TestExternalClass_NewInstance;
  583. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  584. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  585. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  586. Procedure TestExternalClass_JSFunctionPasDescendant;
  587. Procedure TestExternalClass_PascalProperty;
  588. Procedure TestExternalClass_TypeCastToRootClass;
  589. Procedure TestExternalClass_TypeCastToJSObject;
  590. Procedure TestExternalClass_TypeCastStringToExternalString;
  591. Procedure TestExternalClass_TypeCastToJSFunction;
  592. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  593. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  594. Procedure TestExternalClass_BracketAccessor;
  595. Procedure TestExternalClass_BracketAccessor_Call;
  596. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  597. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  598. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  599. Procedure TestExternalClass_BracketAccessor_MultiType;
  600. Procedure TestExternalClass_BracketAccessor_Index;
  601. Procedure TestExternalClass_ForInJSObject;
  602. Procedure TestExternalClass_ForInJSArray;
  603. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  604. // class interfaces
  605. Procedure TestClassInterface_Corba;
  606. Procedure TestClassInterface_ProcExternalFail;
  607. Procedure TestClassInterface_Overloads;
  608. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  609. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  610. Procedure TestClassInterface_AncestorImpl;
  611. Procedure TestClassInterface_ImplReintroduce;
  612. Procedure TestClassInterface_MethodResolution;
  613. Procedure TestClassInterface_AncestorMoreInterfaces;
  614. Procedure TestClassInterface_MethodOverride;
  615. Procedure TestClassInterface_Corba_Delegation;
  616. Procedure TestClassInterface_Corba_DelegationStatic;
  617. Procedure TestClassInterface_Corba_Operators;
  618. Procedure TestClassInterface_Corba_Args;
  619. Procedure TestClassInterface_Corba_ForIn;
  620. Procedure TestClassInterface_COM_AssignVar;
  621. Procedure TestClassInterface_COM_AssignArg;
  622. Procedure TestClassInterface_COM_FunctionResult;
  623. Procedure TestClassInterface_COM_InheritedFuncResult;
  624. Procedure TestClassInterface_COM_IsAsTypeCasts;
  625. Procedure TestClassInterface_COM_PassAsArg;
  626. Procedure TestClassInterface_COM_PassToUntypedParam;
  627. Procedure TestClassInterface_COM_FunctionInExpr;
  628. Procedure TestClassInterface_COM_Property;
  629. Procedure TestClassInterface_COM_IntfProperty;
  630. Procedure TestClassInterface_COM_Delegation;
  631. Procedure TestClassInterface_COM_With;
  632. Procedure TestClassInterface_COM_ForIn;
  633. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  634. Procedure TestClassInterface_COM_RecordIntfFail;
  635. Procedure TestClassInterface_COM_UnitInitialization;
  636. Procedure TestClassInterface_GUID;
  637. Procedure TestClassInterface_GUIDProperty;
  638. // helpers
  639. Procedure TestClassHelper_ClassVar;
  640. Procedure TestClassHelper_Method_AccessInstanceFields;
  641. Procedure TestClassHelper_Method_Call;
  642. Procedure TestClassHelper_Method_Nested_Call;
  643. Procedure TestClassHelper_ClassMethod_Call;
  644. Procedure TestClassHelper_ClassOf;
  645. Procedure TestClassHelper_MethodRefObjFPC;
  646. Procedure TestClassHelper_Constructor;
  647. Procedure TestClassHelper_InheritedObjFPC;
  648. Procedure TestClassHelper_Property;
  649. Procedure TestClassHelper_Property_Array;
  650. Procedure TestClassHelper_Property_Array_Default;
  651. Procedure TestClassHelper_Property_Array_DefaultDefault;
  652. Procedure TestClassHelper_ClassProperty;
  653. Procedure TestClassHelper_ClassPropertyStatic;
  654. Procedure TestClassHelper_ClassProperty_Array;
  655. Procedure TestClassHelper_ForIn;
  656. Procedure TestClassHelper_PassProperty;
  657. Procedure TestExtClassHelper_ClassVar;
  658. Procedure TestExtClassHelper_Method_Call;
  659. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  660. Procedure TestRecordHelper_ClassVar;
  661. Procedure TestRecordHelper_Method_Call;
  662. Procedure TestRecordHelper_Constructor;
  663. Procedure TestTypeHelper_ClassVar;
  664. Procedure TestTypeHelper_PassResultElement;
  665. Procedure TestTypeHelper_PassArgs;
  666. Procedure TestTypeHelper_PassVarConst;
  667. Procedure TestTypeHelper_PassFuncResult;
  668. Procedure TestTypeHelper_PassPropertyField;
  669. Procedure TestTypeHelper_PassPropertyGetter;
  670. Procedure TestTypeHelper_PassClassPropertyField;
  671. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  672. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  673. Procedure TestTypeHelper_Property;
  674. Procedure TestTypeHelper_Property_Array;
  675. Procedure TestTypeHelper_ClassProperty;
  676. Procedure TestTypeHelper_ClassProperty_Array;
  677. Procedure TestTypeHelper_ClassMethod;
  678. Procedure TestTypeHelper_ExtClassMethodFail;
  679. Procedure TestTypeHelper_Constructor;
  680. Procedure TestTypeHelper_Word;
  681. Procedure TestTypeHelper_Boolean;
  682. Procedure TestTypeHelper_WordBool;
  683. Procedure TestTypeHelper_Double;
  684. Procedure TestTypeHelper_NativeInt;
  685. Procedure TestTypeHelper_StringChar;
  686. Procedure TestTypeHelper_JSValue;
  687. Procedure TestTypeHelper_Array;
  688. Procedure TestTypeHelper_EnumType;
  689. Procedure TestTypeHelper_SetType;
  690. Procedure TestTypeHelper_InterfaceType;
  691. Procedure TestTypeHelper_NestedSelf;
  692. // proc types
  693. Procedure TestProcType;
  694. Procedure TestProcType_Arg;
  695. Procedure TestProcType_FunctionFPC;
  696. Procedure TestProcType_FunctionDelphi;
  697. Procedure TestProcType_ProcedureDelphi;
  698. Procedure TestProcType_AsParam;
  699. Procedure TestProcType_MethodFPC;
  700. Procedure TestProcType_MethodDelphi;
  701. Procedure TestProcType_PropertyFPC;
  702. Procedure TestProcType_PropertyDelphi;
  703. Procedure TestProcType_WithClassInstDoPropertyFPC;
  704. Procedure TestProcType_Nested;
  705. Procedure TestProcType_NestedOfObject;
  706. Procedure TestProcType_ReferenceToProc;
  707. Procedure TestProcType_ReferenceToMethod;
  708. Procedure TestProcType_Typecast;
  709. Procedure TestProcType_PassProcToUntyped;
  710. Procedure TestProcType_PassProcToArray;
  711. Procedure TestProcType_SafeCallObjFPC;
  712. Procedure TestProcType_SafeCallDelphi;
  713. // pointer
  714. Procedure TestPointer;
  715. Procedure TestPointer_Proc;
  716. Procedure TestPointer_AssignRecordFail;
  717. Procedure TestPointer_AssignStaticArrayFail;
  718. Procedure TestPointer_TypeCastJSValueToPointer;
  719. Procedure TestPointer_NonRecordFail;
  720. Procedure TestPointer_AnonymousArgTypeFail;
  721. Procedure TestPointer_AnonymousVarTypeFail;
  722. Procedure TestPointer_AnonymousResultTypeFail;
  723. Procedure TestPointer_AddrOperatorFail;
  724. Procedure TestPointer_ArrayParamsFail;
  725. Procedure TestPointer_PointerAddFail;
  726. Procedure TestPointer_IncPointerFail;
  727. Procedure TestPointer_Record;
  728. Procedure TestPointer_RecordArg;
  729. // jsvalue
  730. Procedure TestJSValue_AssignToJSValue;
  731. Procedure TestJSValue_TypeCastToBaseType;
  732. Procedure TestJSValue_TypecastToJSValue;
  733. Procedure TestJSValue_Equal;
  734. Procedure TestJSValue_If;
  735. Procedure TestJSValue_Not;
  736. Procedure TestJSValue_Enum;
  737. Procedure TestJSValue_ClassInstance;
  738. Procedure TestJSValue_ClassOf;
  739. Procedure TestJSValue_ArrayOfJSValue;
  740. Procedure TestJSValue_ArrayLit;
  741. Procedure TestJSValue_Params;
  742. Procedure TestJSValue_UntypedParam;
  743. Procedure TestJSValue_FuncResultType;
  744. Procedure TestJSValue_ProcType_Assign;
  745. Procedure TestJSValue_ProcType_Equal;
  746. Procedure TestJSValue_ProcType_Param;
  747. Procedure TestJSValue_AssignToPointerFail;
  748. Procedure TestJSValue_OverloadDouble;
  749. Procedure TestJSValue_OverloadNativeInt;
  750. Procedure TestJSValue_OverloadWord;
  751. Procedure TestJSValue_OverloadString;
  752. Procedure TestJSValue_OverloadChar;
  753. Procedure TestJSValue_OverloadPointer;
  754. Procedure TestJSValue_ForIn;
  755. // RTTI
  756. Procedure TestRTTI_IntRange;
  757. Procedure TestRTTI_Double;
  758. Procedure TestRTTI_ProcType;
  759. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  760. Procedure TestRTTI_EnumAndSetType;
  761. Procedure TestRTTI_EnumRange;
  762. Procedure TestRTTI_AnonymousEnumType;
  763. Procedure TestRTTI_StaticArray;
  764. Procedure TestRTTI_DynArray;
  765. Procedure TestRTTI_ArrayNestedAnonymous;
  766. Procedure TestRTTI_PublishedMethodOverloadFail;
  767. Procedure TestRTTI_PublishedMethodExternalFail;
  768. Procedure TestRTTI_PublishedClassPropertyFail;
  769. Procedure TestRTTI_PublishedClassFieldFail;
  770. Procedure TestRTTI_PublishedFieldExternalFail;
  771. Procedure TestRTTI_Class_Field;
  772. Procedure TestRTTI_Class_Method;
  773. Procedure TestRTTI_Class_MethodArgFlags;
  774. Procedure TestRTTI_Class_Property;
  775. Procedure TestRTTI_Class_PropertyParams;
  776. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  777. Procedure TestRTTI_Class_OmitRTTI;
  778. Procedure TestRTTI_IndexModifier;
  779. Procedure TestRTTI_StoredModifier;
  780. Procedure TestRTTI_DefaultValue;
  781. Procedure TestRTTI_DefaultValueSet;
  782. Procedure TestRTTI_DefaultValueRangeType;
  783. Procedure TestRTTI_DefaultValueInherit;
  784. Procedure TestRTTI_OverrideMethod;
  785. Procedure TestRTTI_ReintroduceMethod;
  786. Procedure TestRTTI_OverloadProperty;
  787. // ToDo: array argument
  788. Procedure TestRTTI_ClassForward;
  789. Procedure TestRTTI_ClassOf;
  790. Procedure TestRTTI_Record;
  791. Procedure TestRTTI_RecordAnonymousArray;
  792. Procedure TestRTTI_Record_ClassVarType;
  793. Procedure TestRTTI_LocalTypes;
  794. Procedure TestRTTI_TypeInfo_BaseTypes;
  795. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  796. Procedure TestRTTI_TypeInfo_LocalFail;
  797. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  798. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  799. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  800. Procedure TestRTTI_TypeInfo_FunctionClassType;
  801. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  802. Procedure TestRTTI_Interface_Corba;
  803. Procedure TestRTTI_Interface_COM;
  804. Procedure TestRTTI_ClassHelper;
  805. Procedure TestRTTI_ExternalClass;
  806. Procedure TestRTTI_Unit;
  807. // Resourcestring
  808. Procedure TestResourcestringProgram;
  809. Procedure TestResourcestringUnit;
  810. Procedure TestResourcestringImplementation;
  811. // Attributes
  812. Procedure TestAttributes_Members;
  813. Procedure TestAttributes_Types;
  814. Procedure TestAttributes_HelperConstructor_Fail;
  815. // Assertions, checks
  816. procedure TestAssert;
  817. procedure TestAssert_SysUtils;
  818. procedure TestObjectChecks;
  819. procedure TestOverflowChecks_Int;
  820. procedure TestRangeChecks_AssignInt;
  821. procedure TestRangeChecks_AssignIntRange;
  822. procedure TestRangeChecks_AssignEnum;
  823. procedure TestRangeChecks_AssignEnumRange;
  824. procedure TestRangeChecks_AssignChar;
  825. procedure TestRangeChecks_AssignCharRange;
  826. procedure TestRangeChecks_ArrayIndex;
  827. procedure TestRangeChecks_ArrayOfRecIndex;
  828. procedure TestRangeChecks_StringIndex;
  829. procedure TestRangeChecks_TypecastInt;
  830. procedure TestRangeChecks_TypeHelperInt;
  831. // Async/AWait
  832. Procedure TestAsync_Proc;
  833. Procedure TestAsync_CallResultIsPromise;
  834. Procedure TestAsync_ConstructorFail;
  835. Procedure TestAsync_PropertyGetterFail;
  836. Procedure TestAwait_NonPromiseWithTypeFail;
  837. Procedure TestAwait_AsyncCallTypeMismatch;
  838. Procedure TestAWait_OutsideAsyncFail;
  839. Procedure TestAWait_IntegerFail;
  840. Procedure TestAWait_ExternalClassPromise;
  841. Procedure TestAWait_JSValue;
  842. Procedure TestAWait_Result;
  843. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  844. Procedure TestAsync_AnonymousProc;
  845. Procedure TestAsync_ProcType;
  846. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  847. Procedure TestAsync_Inherited;
  848. Procedure TestAsync_ClassInterface;
  849. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  850. end;
  851. function LinesToStr(Args: array of const): string;
  852. function ExtractFileUnitName(aFilename: string): string;
  853. function JSToStr(El: TJSElement): string;
  854. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  855. implementation
  856. function LinesToStr(Args: array of const): string;
  857. var
  858. s: String;
  859. i: Integer;
  860. begin
  861. s:='';
  862. for i:=Low(Args) to High(Args) do
  863. case Args[i].VType of
  864. vtChar: s += Args[i].VChar+LineEnding;
  865. vtString: s += Args[i].VString^+LineEnding;
  866. vtPChar: s += Args[i].VPChar+LineEnding;
  867. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  868. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  869. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  870. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  871. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  872. end;
  873. Result:=s;
  874. end;
  875. function ExtractFileUnitName(aFilename: string): string;
  876. var
  877. p: Integer;
  878. begin
  879. Result:=ExtractFileName(aFilename);
  880. if Result='' then exit;
  881. for p:=length(Result) downto 1 do
  882. case Result[p] of
  883. '/','\': exit;
  884. '.':
  885. begin
  886. Delete(Result,p,length(Result));
  887. exit;
  888. end;
  889. end;
  890. end;
  891. function JSToStr(El: TJSElement): string;
  892. var
  893. aWriter: TBufferWriter;
  894. aJSWriter: TJSWriter;
  895. begin
  896. aJSWriter:=nil;
  897. aWriter:=TBufferWriter.Create(1000);
  898. try
  899. aJSWriter:=TJSWriter.Create(aWriter);
  900. aJSWriter.IndentSize:=2;
  901. aJSWriter.WriteJS(El);
  902. Result:=aWriter.AsString;
  903. finally
  904. aJSWriter.Free;
  905. aWriter.Free;
  906. end;
  907. end;
  908. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  909. // search diff, ignore changes in spaces
  910. const
  911. SpaceChars = [#9,#10,#13,' '];
  912. var
  913. ExpectedP, ActualP: PChar;
  914. function FindLineEnd(p: PChar): PChar;
  915. begin
  916. Result:=p;
  917. while not (Result^ in [#0,#10,#13]) do inc(Result);
  918. end;
  919. function FindLineStart(p, MinP: PChar): PChar;
  920. begin
  921. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  922. Result:=p;
  923. end;
  924. procedure SkipLineEnd(var p: PChar);
  925. begin
  926. if p^ in [#10,#13] then
  927. begin
  928. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  929. inc(p,2)
  930. else
  931. inc(p);
  932. end;
  933. end;
  934. function HasSpecialChar(s: string): boolean;
  935. var
  936. i: Integer;
  937. begin
  938. for i:=1 to length(s) do
  939. if s[i] in [#0..#31,#127..#255] then
  940. exit(true);
  941. Result:=false;
  942. end;
  943. function HashSpecialChars(s: string): string;
  944. var
  945. i: Integer;
  946. begin
  947. Result:='';
  948. for i:=1 to length(s) do
  949. if s[i] in [#0..#31,#127..#255] then
  950. Result:=Result+'#'+hexstr(ord(s[i]),2)
  951. else
  952. Result:=Result+s[i];
  953. end;
  954. procedure DiffFound;
  955. var
  956. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  957. ExpLine, ActLine: String;
  958. i, LineNo, DiffLineNo: Integer;
  959. begin
  960. writeln('Diff found "',Msg,'". Lines:');
  961. // write correct lines
  962. p:=PChar(Expected);
  963. LineNo:=0;
  964. DiffLineNo:=0;
  965. repeat
  966. StartPos:=p;
  967. while not (p^ in [#0,#10,#13]) do inc(p);
  968. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  969. SkipLineEnd(p);
  970. inc(LineNo);
  971. if (p<=ExpectedP) and (p^<>#0) then
  972. begin
  973. writeln('= ',ExpLine);
  974. end else begin
  975. // diff line
  976. if DiffLineNo=0 then DiffLineNo:=LineNo;
  977. // write actual line
  978. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  979. ActLineEndP:=FindLineEnd(ActualP);
  980. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  981. writeln('- ',ActLine);
  982. if HasSpecialChar(ActLine) then
  983. writeln('- ',HashSpecialChars(ActLine));
  984. // write expected line
  985. writeln('+ ',ExpLine);
  986. if HasSpecialChar(ExpLine) then
  987. writeln('- ',HashSpecialChars(ExpLine));
  988. // write empty line with pointer ^
  989. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  990. writeln('^');
  991. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  992. CheckSrcDiff:=false;
  993. // write up to three following actual lines to get some context
  994. for i:=1 to 3 do begin
  995. ActLineStartP:=ActLineEndP;
  996. SkipLineEnd(ActLineStartP);
  997. if ActLineStartP^=#0 then break;
  998. ActLineEndP:=FindLineEnd(ActLineStartP);
  999. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1000. writeln('~ ',ActLine);
  1001. end;
  1002. exit;
  1003. end;
  1004. until p^=#0;
  1005. writeln('DiffFound Actual:-----------------------');
  1006. writeln(Actual);
  1007. writeln('DiffFound Expected:---------------------');
  1008. writeln(Expected);
  1009. writeln('DiffFound ------------------------------');
  1010. Msg:='diff found, but lines are the same, internal error';
  1011. CheckSrcDiff:=false;
  1012. end;
  1013. var
  1014. IsSpaceNeeded: Boolean;
  1015. LastChar, Quote: Char;
  1016. begin
  1017. Result:=true;
  1018. Msg:='';
  1019. if Expected='' then Expected:=' ';
  1020. if Actual='' then Actual:=' ';
  1021. ExpectedP:=PChar(Expected);
  1022. ActualP:=PChar(Actual);
  1023. repeat
  1024. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1025. case ExpectedP^ of
  1026. #0:
  1027. begin
  1028. // check that rest of Actual has only spaces
  1029. while ActualP^ in SpaceChars do inc(ActualP);
  1030. if ActualP^<>#0 then
  1031. begin
  1032. DiffFound;
  1033. exit;
  1034. end;
  1035. exit(true);
  1036. end;
  1037. ' ',#9,#10,#13:
  1038. begin
  1039. // skip space in Expected
  1040. IsSpaceNeeded:=false;
  1041. if ExpectedP>PChar(Expected) then
  1042. LastChar:=ExpectedP[-1]
  1043. else
  1044. LastChar:=#0;
  1045. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1046. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1047. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1048. IsSpaceNeeded:=true;
  1049. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1050. begin
  1051. DiffFound;
  1052. exit;
  1053. end;
  1054. while ActualP^ in SpaceChars do inc(ActualP);
  1055. end;
  1056. '''','"':
  1057. begin
  1058. while ActualP^ in SpaceChars do inc(ActualP);
  1059. if ExpectedP^<>ActualP^ then
  1060. begin
  1061. DiffFound;
  1062. exit;
  1063. end;
  1064. Quote:=ExpectedP^;
  1065. repeat
  1066. inc(ExpectedP);
  1067. inc(ActualP);
  1068. if ExpectedP^<>ActualP^ then
  1069. begin
  1070. DiffFound;
  1071. exit;
  1072. end;
  1073. if (ExpectedP^ in [#0,#10,#13]) then
  1074. break
  1075. else if (ExpectedP^=Quote) then
  1076. begin
  1077. inc(ExpectedP);
  1078. inc(ActualP);
  1079. break;
  1080. end;
  1081. until false;
  1082. end;
  1083. else
  1084. while ActualP^ in SpaceChars do inc(ActualP);
  1085. if ExpectedP^<>ActualP^ then
  1086. begin
  1087. DiffFound;
  1088. exit;
  1089. end;
  1090. inc(ExpectedP);
  1091. inc(ActualP);
  1092. end;
  1093. until false;
  1094. end;
  1095. { TTestEnginePasResolver }
  1096. destructor TTestEnginePasResolver.Destroy;
  1097. begin
  1098. FreeAndNil(FStreamResolver);
  1099. FreeAndNil(FParser);
  1100. FreeAndNil(FScanner);
  1101. FreeAndNil(FStreamResolver);
  1102. if Module<>nil then
  1103. begin
  1104. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1105. FModule:=nil;
  1106. end;
  1107. inherited Destroy;
  1108. end;
  1109. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1110. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1111. begin
  1112. Result:=nil;
  1113. if InFilename<>'' then
  1114. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1115. if Assigned(OnFindUnit) then
  1116. Result:=OnFindUnit(AName);
  1117. if NameExpr=nil then ;
  1118. end;
  1119. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1120. begin
  1121. // do not parse recursively
  1122. // parse via the queue
  1123. if Section=nil then ;
  1124. end;
  1125. { TCustomTestModule }
  1126. function TCustomTestModule.GetMsgCount: integer;
  1127. begin
  1128. Result:=FHintMsgs.Count;
  1129. end;
  1130. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1131. begin
  1132. Result:=TTestHintMessage(FHintMsgs[Index]);
  1133. end;
  1134. function TCustomTestModule.GetResolverCount: integer;
  1135. begin
  1136. Result:=FModules.Count;
  1137. end;
  1138. function TCustomTestModule.GetResolvers(Index: integer
  1139. ): TTestEnginePasResolver;
  1140. begin
  1141. Result:=TTestEnginePasResolver(FModules[Index]);
  1142. end;
  1143. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1144. ): TPasModule;
  1145. var
  1146. DefNamespace: String;
  1147. begin
  1148. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1149. if (Pos('.',aUnitName)<1) then
  1150. begin
  1151. DefNamespace:=GetDefaultNamespace;
  1152. if DefNamespace<>'' then
  1153. begin
  1154. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1155. if Result<>nil then exit;
  1156. end;
  1157. end;
  1158. Result:=LoadUnit(aUnitName);
  1159. if Result<>nil then exit;
  1160. {$IFDEF VerbosePas2JS}
  1161. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1162. {$ENDIF}
  1163. Fail('can''t find unit "'+aUnitName+'"');
  1164. end;
  1165. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1166. var
  1167. aParser: TPasParser;
  1168. Item: TTestHintMessage;
  1169. begin
  1170. aParser:=Sender as TPasParser;
  1171. Item:=TTestHintMessage.Create;
  1172. Item.Id:=aParser.LastMsgNumber;
  1173. Item.MsgType:=aParser.LastMsgType;
  1174. Item.MsgNumber:=aParser.LastMsgNumber;
  1175. Item.Msg:=Msg;
  1176. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1177. {$IFDEF VerbosePas2JS}
  1178. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1179. {$ENDIF}
  1180. FHintMsgs.Add(Item);
  1181. end;
  1182. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1183. );
  1184. var
  1185. aResolver: TTestEnginePasResolver;
  1186. Item: TTestHintMessage;
  1187. begin
  1188. aResolver:=Sender as TTestEnginePasResolver;
  1189. Item:=TTestHintMessage.Create;
  1190. Item.Id:=aResolver.LastMsgId;
  1191. Item.MsgType:=aResolver.LastMsgType;
  1192. Item.MsgNumber:=aResolver.LastMsgNumber;
  1193. Item.Msg:=Msg;
  1194. Item.SourcePos:=aResolver.LastSourcePos;
  1195. {$IFDEF VerbosePas2JS}
  1196. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1197. {$ENDIF}
  1198. FHintMsgs.Add(Item);
  1199. end;
  1200. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1201. var
  1202. Item: TTestHintMessage;
  1203. aScanner: TPas2jsPasScanner;
  1204. begin
  1205. aScanner:=Sender as TPas2jsPasScanner;
  1206. Item:=TTestHintMessage.Create;
  1207. Item.Id:=aScanner.LastMsgNumber;
  1208. Item.MsgType:=aScanner.LastMsgType;
  1209. Item.MsgNumber:=aScanner.LastMsgNumber;
  1210. Item.Msg:=Msg;
  1211. Item.SourcePos:=aScanner.CurSourcePos;
  1212. {$IFDEF VerbosePas2JS}
  1213. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1214. {$ENDIF}
  1215. FHintMsgs.Add(Item);
  1216. end;
  1217. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1218. begin
  1219. if FWithTypeInfo=AValue then Exit;
  1220. FWithTypeInfo:=AValue;
  1221. if AValue then
  1222. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1223. else
  1224. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1225. end;
  1226. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1227. var
  1228. i: Integer;
  1229. CurEngine: TTestEnginePasResolver;
  1230. CurUnitName: String;
  1231. begin
  1232. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1233. Result:=nil;
  1234. if (Module.ClassType=TPasModule)
  1235. and (CompareText(Module.Name,aUnitName)=0) then
  1236. exit(Module);
  1237. for i:=0 to ResolverCount-1 do
  1238. begin
  1239. CurEngine:=Resolvers[i];
  1240. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1241. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1242. if CompareText(aUnitName,CurUnitName)=0 then
  1243. begin
  1244. Result:=CurEngine.Module;
  1245. if Result<>nil then exit;
  1246. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1247. FileResolver.FindSourceFile(aUnitName);
  1248. CurEngine.StreamResolver:=TStreamResolver.Create;
  1249. CurEngine.StreamResolver.OwnsStreams:=True;
  1250. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1251. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1252. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1253. InitScanner(CurEngine.Scanner);
  1254. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1255. CurEngine.Parser.Options:=po_tcmodules;
  1256. if CompareText(CurUnitName,'System')=0 then
  1257. CurEngine.Parser.ImplicitUses.Clear;
  1258. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1259. try
  1260. CurEngine.Parser.NextToken;
  1261. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1262. except
  1263. on E: Exception do
  1264. HandleException(E);
  1265. end;
  1266. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1267. Result:=CurEngine.Module;
  1268. exit;
  1269. end;
  1270. end;
  1271. end;
  1272. procedure TCustomTestModule.SetUp;
  1273. begin
  1274. {$IFDEF EnablePasTreeGlobalRefCount}
  1275. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1276. {$ENDIF}
  1277. if FModules<>nil then
  1278. begin
  1279. writeln('TCustomTestModule.SetUp FModules<>nil');
  1280. Halt;
  1281. end;
  1282. inherited SetUp;
  1283. FSkipTests:=false;
  1284. FWithTypeInfo:=false;
  1285. FSource:=TStringList.Create;
  1286. FHub:=TPas2JSResolverHub.Create(Self);
  1287. FModules:=TObjectList.Create(true);
  1288. FFilename:='test1.pp';
  1289. FFileResolver:=TStreamResolver.Create;
  1290. FFileResolver.OwnsStreams:=True;
  1291. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1292. InitScanner(FScanner);
  1293. FEngine:=AddModule(Filename);
  1294. FEngine.Scanner:=FScanner;
  1295. FScanner.Resolver:=FEngine;
  1296. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1297. FParser.OnLog:=@OnParserLog;
  1298. FEngine.Parser:=FParser;
  1299. Parser.Options:=po_tcmodules;
  1300. FModule:=Nil;
  1301. FConverter:=CreateConverter;
  1302. FExpectedErrorClass:=nil;
  1303. end;
  1304. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1305. var
  1306. Options: TPasToJsConverterOptions;
  1307. begin
  1308. Result:=TPasToJSConverter.Create;
  1309. Options:=co_tcmodules;
  1310. if WithTypeInfo then
  1311. Exclude(Options,coNoTypeInfo)
  1312. else
  1313. Include(Options,coNoTypeInfo);
  1314. Result.Options:=Options;
  1315. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1316. end;
  1317. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1318. begin
  1319. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1320. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1321. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1322. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1323. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1324. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1325. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1326. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1327. aScanner.OnLog:=@OnScannerLog;
  1328. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1329. end;
  1330. procedure TCustomTestModule.TearDown;
  1331. {$IFDEF CheckPasTreeRefCount}
  1332. var
  1333. El: TPasElement;
  1334. {$ENDIF}
  1335. var
  1336. i: Integer;
  1337. CurModule: TPasModule;
  1338. begin
  1339. FHintMsgs.Clear;
  1340. FHintMsgsGood.Clear;
  1341. FSkipTests:=false;
  1342. FWithTypeInfo:=false;
  1343. FJSRegModuleCall:=nil;
  1344. FJSModuleCallArgs:=nil;
  1345. FJSImplentationUses:=nil;
  1346. FJSInterfaceUses:=nil;
  1347. FJSModuleSrc:=nil;
  1348. FJSInitBody:=nil;
  1349. FreeAndNil(FJSSource);
  1350. FreeAndNil(FJSModule);
  1351. FreeAndNil(FConverter);
  1352. Engine.Clear;
  1353. FreeAndNil(FSource);
  1354. FreeAndNil(FFileResolver);
  1355. if FModules<>nil then
  1356. begin
  1357. for i:=0 to FModules.Count-1 do
  1358. begin
  1359. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1360. if CurModule=nil then continue;
  1361. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1362. CurModule.ReleaseUsedUnits;
  1363. end;
  1364. if FModule<>nil then
  1365. FModule.ReleaseUsedUnits;
  1366. for i:=0 to FModules.Count-1 do
  1367. begin
  1368. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1369. if CurModule=nil then continue;
  1370. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1371. end;
  1372. FreeAndNil(FModules);
  1373. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1374. FEngine:=nil;
  1375. end;
  1376. FreeAndNil(FHub);
  1377. inherited TearDown;
  1378. {$IFDEF EnablePasTreeGlobalRefCount}
  1379. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1380. begin
  1381. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1382. {$IFDEF CheckPasTreeRefCount}
  1383. El:=TPasElement.FirstRefEl;
  1384. while El<>nil do
  1385. begin
  1386. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1387. for i:=0 to El.RefIds.Count-1 do
  1388. writeln(' ',El.RefIds[i]);
  1389. El:=El.NextRefEl;
  1390. end;
  1391. {$ENDIF}
  1392. Halt;
  1393. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1394. end;
  1395. {$ENDIF}
  1396. end;
  1397. procedure TCustomTestModule.Add(Line: string);
  1398. begin
  1399. Source.Add(Line);
  1400. end;
  1401. procedure TCustomTestModule.Add(const Lines: array of string);
  1402. var
  1403. i: Integer;
  1404. begin
  1405. for i:=low(Lines) to high(Lines) do
  1406. Add(Lines[i]);
  1407. end;
  1408. procedure TCustomTestModule.StartParsing;
  1409. var
  1410. Src: String;
  1411. begin
  1412. Src:=Source.Text;
  1413. FEngine.Source:=Src;
  1414. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1415. Scanner.OpenFile(FileName);
  1416. Writeln('// Test : ',Self.TestName);
  1417. Writeln(Src);
  1418. end;
  1419. procedure TCustomTestModule.ParseModuleQueue;
  1420. var
  1421. i: Integer;
  1422. CurResolver: TTestEnginePasResolver;
  1423. Found: Boolean;
  1424. Section: TPasSection;
  1425. begin
  1426. // parse til exception or all modules finished
  1427. while not SkipTests do
  1428. begin
  1429. Found:=false;
  1430. for i:=0 to ResolverCount-1 do
  1431. begin
  1432. CurResolver:=Resolvers[i];
  1433. if CurResolver.CurrentParser=nil then continue;
  1434. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1435. continue;
  1436. CurResolver.Parser.ParseContinue;
  1437. Found:=true;
  1438. break;
  1439. end;
  1440. if not Found then break;
  1441. end;
  1442. for i:=0 to ResolverCount-1 do
  1443. begin
  1444. CurResolver:=Resolvers[i];
  1445. if CurResolver.Parser=nil then
  1446. begin
  1447. if CurResolver.CurrentParser<>nil then
  1448. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1449. continue;
  1450. end;
  1451. if CurResolver.Parser.CurModule<>nil then
  1452. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1453. end;
  1454. end;
  1455. procedure TCustomTestModule.ParseModule;
  1456. begin
  1457. if SkipTests then exit;
  1458. FFirstPasStatement:=nil;
  1459. try
  1460. StartParsing;
  1461. Parser.ParseMain(FModule);
  1462. ParseModuleQueue;
  1463. except
  1464. on E: Exception do
  1465. HandleException(E);
  1466. end;
  1467. if SkipTests then exit;
  1468. AssertNotNull('Module resulted in Module',Module);
  1469. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1470. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1471. end;
  1472. procedure TCustomTestModule.ParseProgram;
  1473. begin
  1474. if SkipTests then exit;
  1475. ParseModule;
  1476. if SkipTests then exit;
  1477. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1478. FPasProgram:=TPasProgram(Module);
  1479. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1480. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1481. if (PasProgram.InitializationSection.Elements.Count>0) then
  1482. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1483. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1484. end;
  1485. procedure TCustomTestModule.ParseUnit;
  1486. begin
  1487. if SkipTests then exit;
  1488. ParseModule;
  1489. if SkipTests then exit;
  1490. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1491. AssertNotNull('Has interface section',Module.InterfaceSection);
  1492. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1493. if (Module.InitializationSection<>nil)
  1494. and (Module.InitializationSection.Elements.Count>0)
  1495. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1496. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1497. end;
  1498. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1499. ): TTestEnginePasResolver;
  1500. var
  1501. i: Integer;
  1502. begin
  1503. for i:=0 to ResolverCount-1 do
  1504. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1505. exit(Resolvers[i]);
  1506. Result:=nil;
  1507. end;
  1508. function TCustomTestModule.AddModule(aFilename: string
  1509. ): TTestEnginePasResolver;
  1510. begin
  1511. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1512. if FindModuleWithFilename(aFilename)<>nil then
  1513. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1514. Result:=TTestEnginePasResolver.Create;
  1515. Result.Filename:=aFilename;
  1516. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1517. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1518. Result.OnLog:=@OnPasResolverLog;
  1519. Result.Hub:=Hub;
  1520. FModules.Add(Result);
  1521. end;
  1522. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1523. ): TTestEnginePasResolver;
  1524. begin
  1525. Result:=AddModule(aFilename);
  1526. Result.Source:=Src;
  1527. end;
  1528. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1529. ImplementationSrc: string): TTestEnginePasResolver;
  1530. var
  1531. Src: String;
  1532. begin
  1533. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1534. Src+=LineEnding;
  1535. Src+='interface'+LineEnding;
  1536. Src+=LineEnding;
  1537. Src+=InterfaceSrc;
  1538. Src+='implementation'+LineEnding;
  1539. Src+=LineEnding;
  1540. Src+=ImplementationSrc;
  1541. Src+='end.'+LineEnding;
  1542. Result:=AddModuleWithSrc(aFilename,Src);
  1543. end;
  1544. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1545. var
  1546. Intf, Impl: TStringList;
  1547. begin
  1548. Intf:=TStringList.Create;
  1549. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1550. // unit interface
  1551. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1552. Intf.Add('{$modeswitch externalclass}');
  1553. Intf.Add('type');
  1554. Intf.Add(' integer=longint;');
  1555. Intf.Add(' sizeint=nativeint;');
  1556. //'const',
  1557. //' LineEnding = #10;',
  1558. //' DirectorySeparator = ''/'';',
  1559. //' DriveSeparator = '''';',
  1560. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1561. //' AllowDriveSeparators : set of char = [];',
  1562. if supTObject in Parts then
  1563. Intf.AddStrings([
  1564. 'type',
  1565. ' TClass = class of TObject;',
  1566. ' TObject = class',
  1567. ' constructor Create;',
  1568. ' destructor Destroy; virtual;',
  1569. ' class function ClassType: TClass; assembler;',
  1570. ' class function ClassName: String; assembler;',
  1571. ' class function ClassNameIs(const Name: string): boolean;',
  1572. ' class function ClassParent: TClass; assembler;',
  1573. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1574. ' class function UnitName: String; assembler;',
  1575. ' procedure AfterConstruction; virtual;',
  1576. ' procedure BeforeDestruction;virtual;',
  1577. ' function Equals(Obj: TObject): boolean; virtual;',
  1578. ' function ToString: String; virtual;',
  1579. ' end;']);
  1580. if supTInterfacedObject in Parts then
  1581. Intf.AddStrings([
  1582. ' {$Interfaces COM}',
  1583. ' IUnknown = interface',
  1584. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1585. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1586. ' function _AddRef: Integer;',
  1587. ' function _Release: Integer;',
  1588. ' end;',
  1589. ' IInterface = IUnknown;',
  1590. ' TInterfacedObject = class(TObject,IUnknown)',
  1591. ' protected',
  1592. ' fRefCount: Integer;',
  1593. ' { implement methods of IUnknown }',
  1594. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1595. ' function _AddRef: Integer; virtual;',
  1596. ' function _Release: Integer; virtual;',
  1597. ' end;',
  1598. ' TInterfacedClass = class of TInterfacedObject;',
  1599. '',
  1600. '']);
  1601. if supTVarRec in Parts then
  1602. Intf.AddStrings([
  1603. 'const',
  1604. ' vtInteger = 0;',
  1605. ' vtBoolean = 1;',
  1606. ' vtJSValue = 19;',
  1607. 'type',
  1608. ' PVarRec = ^TVarRec;',
  1609. ' TVarRec = record',
  1610. ' VType : byte;',
  1611. ' VJSValue: JSValue;',
  1612. ' vInteger: longint external name ''VJSValue'';',
  1613. ' vBoolean: boolean external name ''VJSValue'';',
  1614. ' end;',
  1615. ' TVarRecArray = array of TVarRec;',
  1616. 'function VarRecs: TVarRecArray; varargs;',
  1617. '']);
  1618. if supTypeInfo in Parts then
  1619. begin
  1620. Intf.AddStrings([
  1621. 'type',
  1622. ' TTypeKind = (',
  1623. ' tkUnknown, // 0',
  1624. ' tkInteger, // 1',
  1625. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1626. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1627. ' tkEnumeration, // 4',
  1628. ' tkSet, // 5',
  1629. ' tkDouble, // 6',
  1630. ' tkBool, // 7',
  1631. ' tkProcVar, // 8 function or procedure',
  1632. ' tkMethod, // 9 proc var of object',
  1633. ' tkArray, // 10 static array',
  1634. ' tkDynArray, // 11',
  1635. ' tkRecord, // 12',
  1636. ' tkClass, // 13',
  1637. ' tkClassRef, // 14',
  1638. ' tkPointer, // 15',
  1639. ' tkJSValue, // 16',
  1640. ' tkRefToProcVar, // 17 variable of procedure type',
  1641. ' tkInterface, // 18',
  1642. ' //tkObject,',
  1643. ' //tkSString,tkLString,tkAString,tkWString,',
  1644. ' //tkVariant,',
  1645. ' //tkWChar,',
  1646. ' //tkInt64,',
  1647. ' //tkQWord,',
  1648. ' //tkInterfaceRaw,',
  1649. ' //tkUString,tkUChar,',
  1650. ' tkHelper, // 19',
  1651. ' //tkFile,',
  1652. ' tkExtClass // 20',
  1653. ' );',
  1654. ' TTypeKinds = set of TTypeKind;',
  1655. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1656. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1657. ' end;',
  1658. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1659. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1660. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1661. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1662. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1663. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1664. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1665. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1666. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1667. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1668. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1669. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1670. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1671. '']);
  1672. end;
  1673. if supWriteln in Parts then
  1674. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1675. Intf.Add('var');
  1676. Intf.Add(' ExitCode: Longint = 0;');
  1677. // unit implementation
  1678. Impl:=TStringList.Create;
  1679. if supTObject in Parts then
  1680. Impl.AddStrings([
  1681. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1682. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1683. 'asm',
  1684. 'end;',
  1685. 'constructor TObject.Create; begin end;',
  1686. 'destructor TObject.Destroy; begin end;',
  1687. 'class function TObject.ClassType: TClass; assembler;',
  1688. 'asm',
  1689. 'end;',
  1690. 'class function TObject.ClassName: String; assembler;',
  1691. 'asm',
  1692. 'end;',
  1693. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1694. 'begin',
  1695. ' Result:=SameText(Name,ClassName);',
  1696. 'end;',
  1697. 'class function TObject.ClassParent: TClass; assembler;',
  1698. 'asm',
  1699. 'end;',
  1700. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1701. 'asm',
  1702. 'end;',
  1703. 'class function TObject.UnitName: String; assembler;',
  1704. 'asm',
  1705. 'end;',
  1706. 'procedure TObject.AfterConstruction; begin end;',
  1707. 'procedure TObject.BeforeDestruction; begin end;',
  1708. 'function TObject.Equals(Obj: TObject): boolean;',
  1709. 'begin',
  1710. ' Result:=Obj=Self;',
  1711. 'end;',
  1712. 'function TObject.ToString: String;',
  1713. 'begin',
  1714. ' Result:=ClassName;',
  1715. 'end;'
  1716. ]);
  1717. if supTInterfacedObject in Parts then
  1718. Impl.AddStrings([
  1719. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1720. //'begin',
  1721. //'end;',
  1722. 'function TInterfacedObject._AddRef: Integer;',
  1723. 'begin',
  1724. 'end;',
  1725. 'function TInterfacedObject._Release: Integer;',
  1726. 'begin',
  1727. 'end;',
  1728. '']);
  1729. if supTVarRec in Parts then
  1730. Impl.AddStrings([
  1731. 'function VarRecs: TVarRecArray; varargs;',
  1732. 'var',
  1733. ' v: PVarRec;',
  1734. 'begin',
  1735. ' v^.VType:=1;',
  1736. ' v^.VJSValue:=2;',
  1737. 'end;',
  1738. '']);
  1739. try
  1740. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1741. finally
  1742. Intf.Free;
  1743. Impl.Free;
  1744. end;
  1745. end;
  1746. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1747. SystemUnitParts: TSystemUnitParts);
  1748. begin
  1749. if NeedSystemUnit then
  1750. AddSystemUnit(SystemUnitParts)
  1751. else
  1752. Parser.ImplicitUses.Clear;
  1753. Add('program '+ExtractFileUnitName(Filename)+';');
  1754. Add('');
  1755. end;
  1756. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1757. SystemUnitParts: TSystemUnitParts);
  1758. begin
  1759. if NeedSystemUnit then
  1760. AddSystemUnit(SystemUnitParts)
  1761. else
  1762. Parser.ImplicitUses.Clear;
  1763. Add('unit Test1;');
  1764. Add('');
  1765. end;
  1766. procedure TCustomTestModule.ConvertModule;
  1767. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1768. out UsesLit: TJSArrayLiteral);
  1769. var
  1770. i: Integer;
  1771. Item: TJSElement;
  1772. Lit: TJSLiteral;
  1773. begin
  1774. UsesLit:=nil;
  1775. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1776. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1777. exit; // null is ok
  1778. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1779. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1780. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1781. begin
  1782. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1783. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1784. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1785. Lit:=TJSLiteral(Item);
  1786. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1787. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1788. end;
  1789. end;
  1790. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1791. out Src: TJSSourceElements);
  1792. var
  1793. FunDecl: TJSFunctionDeclarationStatement;
  1794. FunDef: TJSFuncDef;
  1795. FunBody: TJSFunctionBody;
  1796. begin
  1797. Src:=nil;
  1798. AssertNotNull(ParamName,Arg.Expr);
  1799. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1800. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1801. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1802. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1803. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1804. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1805. AssertNotNull(ParamName+' body',FunDef.Body);
  1806. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1807. FunBody:=FunDef.Body as TJSFunctionBody;
  1808. AssertNotNull(ParamName+' body.A',FunBody.A);
  1809. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1810. Src:=FunBody.A as TJSSourceElements;
  1811. end;
  1812. var
  1813. ModuleNameExpr: TJSLiteral;
  1814. InitFunction: TJSFunctionDeclarationStatement;
  1815. InitAssign: TJSSimpleAssignStatement;
  1816. InitName: String;
  1817. LastNode: TJSElement;
  1818. Arg: TJSArrayLiteralElement;
  1819. begin
  1820. if SkipTests then exit;
  1821. try
  1822. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1823. except
  1824. on E: Exception do
  1825. HandleException(E);
  1826. end;
  1827. if SkipTests then exit;
  1828. if ExpectedErrorClass<>nil then
  1829. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1830. FJSSource:=TStringList.Create;
  1831. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1832. {$IFDEF VerbosePas2JS}
  1833. writeln('TTestModule.ConvertModule JS:');
  1834. write(FJSSource.Text);
  1835. {$ENDIF}
  1836. // rtl.module(...
  1837. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1838. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1839. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1840. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1841. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1842. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1843. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1844. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1845. // parameter 'unitname'
  1846. if JSModuleCallArgs.Elements.Count<1 then
  1847. Fail('rtl.module first param unit missing');
  1848. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1849. AssertNotNull('module name param',Arg.Expr);
  1850. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1851. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1852. if Module is TPasProgram then
  1853. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1854. else
  1855. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1856. // main uses section
  1857. if JSModuleCallArgs.Elements.Count<2 then
  1858. Fail('rtl.module second param main uses missing');
  1859. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1860. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1861. // program/library/interface function()
  1862. if JSModuleCallArgs.Elements.Count<3 then
  1863. Fail('rtl.module third param intf-function missing');
  1864. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1865. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1866. // search for $mod.$init or $mod.$main - the last statement
  1867. if Module is TPasProgram then
  1868. begin
  1869. InitName:='$main';
  1870. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1871. end
  1872. else
  1873. InitName:='$init';
  1874. FJSInitBody:=nil;
  1875. if JSModuleSrc.Statements.Count>0 then
  1876. begin
  1877. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1878. if LastNode is TJSSimpleAssignStatement then
  1879. begin
  1880. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1881. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1882. begin
  1883. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1884. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1885. end
  1886. else if Module is TPasProgram then
  1887. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1888. end;
  1889. end;
  1890. // optional: implementation uses section
  1891. if JSModuleCallArgs.Elements.Count<4 then
  1892. exit;
  1893. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1894. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1895. end;
  1896. procedure TCustomTestModule.ConvertProgram;
  1897. begin
  1898. Add('end.');
  1899. ParseProgram;
  1900. ConvertModule;
  1901. end;
  1902. procedure TCustomTestModule.ConvertUnit;
  1903. begin
  1904. Add('end.');
  1905. ParseUnit;
  1906. ConvertModule;
  1907. end;
  1908. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1909. begin
  1910. Result:=tcmodules.JSToStr(El);
  1911. end;
  1912. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1913. DottedName: string);
  1914. begin
  1915. if DottedName='' then
  1916. begin
  1917. AssertNull(Msg,El);
  1918. end
  1919. else
  1920. begin
  1921. AssertNotNull(Msg,El);
  1922. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1923. end;
  1924. end;
  1925. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1926. begin
  1927. if El=nil then
  1928. Result:=''
  1929. else if El is TJSPrimaryExpressionIdent then
  1930. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1931. else if El is TJSDotMemberExpression then
  1932. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1933. else
  1934. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1935. end;
  1936. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1937. InitStatements: string; ImplStatements: string);
  1938. var
  1939. ActualSrc, ExpectedSrc, InitName: String;
  1940. begin
  1941. ActualSrc:=JSToStr(JSModuleSrc);
  1942. if coUseStrict in Converter.Options then
  1943. ExpectedSrc:='"use strict";'+LineEnding
  1944. else
  1945. ExpectedSrc:='';
  1946. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1947. ExpectedSrc:=ExpectedSrc+Statements;
  1948. // unit implementation
  1949. if (Trim(ImplStatements)<>'') then
  1950. ExpectedSrc:=ExpectedSrc+LineEnding
  1951. +'$mod.$implcode = function () {'+LineEnding
  1952. +ImplStatements
  1953. +'};'+LineEnding;
  1954. // program main or unit initialization
  1955. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1956. begin
  1957. if Module is TPasProgram then
  1958. InitName:='$main'
  1959. else
  1960. InitName:='$init';
  1961. ExpectedSrc:=ExpectedSrc+LineEnding
  1962. +'$mod.'+InitName+' = function () {'+LineEnding
  1963. +InitStatements
  1964. +'};'+LineEnding;
  1965. end;
  1966. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1967. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1968. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1969. end;
  1970. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1971. // search diff, ignore changes in spaces
  1972. var
  1973. s: string;
  1974. begin
  1975. if CheckSrcDiff(Expected,Actual,s) then exit;
  1976. Fail(Msg+': '+s);
  1977. end;
  1978. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1979. var
  1980. aResolver: TTestEnginePasResolver;
  1981. aConverter: TPasToJSConverter;
  1982. aJSModule: TJSSourceElements;
  1983. ActualSrc: String;
  1984. begin
  1985. aResolver:=GetResolver(Filename);
  1986. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1987. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1988. {$IFDEF VerbosePas2JS}
  1989. writeln('CheckUnit '+Filename+' converting ...');
  1990. {$ENDIF}
  1991. aConverter:=CreateConverter;
  1992. aJSModule:=nil;
  1993. try
  1994. try
  1995. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1996. except
  1997. on E: Exception do
  1998. HandleException(E);
  1999. end;
  2000. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2001. {$IFDEF VerbosePas2JS}
  2002. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2003. write(aResolver.Source);
  2004. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2005. write(ActualSrc);
  2006. {$ENDIF}
  2007. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2008. finally
  2009. aJSModule.Free;
  2010. aConverter.Free;
  2011. end;
  2012. end;
  2013. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2014. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2015. var
  2016. i: Integer;
  2017. Item: TTestHintMessage;
  2018. Expected,Actual: string;
  2019. begin
  2020. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2021. for i:=0 to MsgCount-1 do
  2022. begin
  2023. Item:=Msgs[i];
  2024. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2025. if (Marker<>nil) then
  2026. begin
  2027. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2028. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2029. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2030. end;
  2031. // found
  2032. FHintMsgsGood.Add(Item);
  2033. str(Item.MsgType,Actual);
  2034. str(MsgType,Expected);
  2035. AssertEquals('MsgType',Expected,Actual);
  2036. exit;
  2037. end;
  2038. // needed message missing -> show emitted messages
  2039. WriteSources('',0,0);
  2040. for i:=0 to MsgCount-1 do
  2041. begin
  2042. Item:=Msgs[i];
  2043. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2044. ' ('+IntToStr(Item.MsgNumber),')');
  2045. if Marker<>nil then
  2046. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2047. writeln(' {',Item.Msg,'}');
  2048. end;
  2049. str(MsgType,Expected);
  2050. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2051. if Marker<>nil then
  2052. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2053. Actual:=Actual+' '+Msg;
  2054. Fail(Actual);
  2055. end;
  2056. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2057. );
  2058. var
  2059. i: Integer;
  2060. s, Txt: String;
  2061. Msg: TTestHintMessage;
  2062. begin
  2063. for i:=0 to MsgCount-1 do
  2064. begin
  2065. Msg:=Msgs[i];
  2066. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2067. s:='';
  2068. str(Msg.MsgType,s);
  2069. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2070. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2071. if WithSourcePos then
  2072. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2073. Txt:=Txt+' {'+Msg.Msg+'}';
  2074. Fail(Txt);
  2075. end;
  2076. end;
  2077. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2078. MsgNumber: integer);
  2079. begin
  2080. ExpectedErrorClass:=EScannerError;
  2081. ExpectedErrorMsg:=Msg;
  2082. ExpectedErrorNumber:=MsgNumber;
  2083. end;
  2084. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2085. MsgNumber: integer);
  2086. begin
  2087. ExpectedErrorClass:=EParserError;
  2088. ExpectedErrorMsg:=Msg;
  2089. ExpectedErrorNumber:=MsgNumber;
  2090. end;
  2091. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2092. MsgNumber: integer);
  2093. begin
  2094. ExpectedErrorClass:=EPasResolve;
  2095. ExpectedErrorMsg:=Msg;
  2096. ExpectedErrorNumber:=MsgNumber;
  2097. end;
  2098. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2099. MsgNumber: integer);
  2100. begin
  2101. ExpectedErrorClass:=EPas2JS;
  2102. ExpectedErrorMsg:=Msg;
  2103. ExpectedErrorNumber:=MsgNumber;
  2104. end;
  2105. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2106. var
  2107. MsgNumber: Integer;
  2108. Msg: String;
  2109. begin
  2110. Result:=false;
  2111. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2112. Msg:=E.Message;
  2113. if E is EPas2JS then
  2114. MsgNumber:=EPas2JS(E).MsgNumber
  2115. else if E is EPasResolve then
  2116. MsgNumber:=EPasResolve(E).MsgNumber
  2117. else if E is EParserError then
  2118. MsgNumber:=Parser.LastMsgNumber
  2119. else if E is EScannerError then
  2120. begin
  2121. MsgNumber:=Scanner.LastMsgNumber;
  2122. Msg:=Scanner.LastMsg;
  2123. end
  2124. else
  2125. MsgNumber:=0;
  2126. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2127. if Result then
  2128. SkipTests:=true;
  2129. end;
  2130. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2131. begin
  2132. if IsErrorExpected(E) then exit;
  2133. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2134. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2135. +' '+Scanner.CurFilename
  2136. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2137. FailException(E);
  2138. end;
  2139. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2140. begin
  2141. if IsErrorExpected(E) then exit;
  2142. WriteSources(E.Filename,E.Row,E.Column);
  2143. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2144. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2145. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2146. );
  2147. FailException(E);
  2148. end;
  2149. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2150. var
  2151. P: TPasSourcePos;
  2152. begin
  2153. if IsErrorExpected(E) then exit;
  2154. P:=E.SourcePos;
  2155. WriteSources(P.FileName,P.Row,P.Column);
  2156. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2157. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2158. FailException(E);
  2159. end;
  2160. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2161. var
  2162. Row, Col: integer;
  2163. begin
  2164. if IsErrorExpected(E) then exit;
  2165. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2166. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2167. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2168. +' '+E.PasElement.SourceFilename
  2169. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2170. FailException(E);
  2171. end;
  2172. procedure TCustomTestModule.HandleException(E: Exception);
  2173. begin
  2174. if E is EScannerError then
  2175. HandleScannerError(EScannerError(E))
  2176. else if E is EParserError then
  2177. HandleParserError(EParserError(E))
  2178. else if E is EPasResolve then
  2179. HandlePasResolveError(EPasResolve(E))
  2180. else if E is EPas2JS then
  2181. HandlePas2JSError(EPas2JS(E))
  2182. else
  2183. begin
  2184. if IsErrorExpected(E) then exit;
  2185. if not (E is EAssertionFailedError) then
  2186. begin
  2187. WriteSources('',0,0);
  2188. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2189. end;
  2190. FailException(E);
  2191. end;
  2192. end;
  2193. procedure TCustomTestModule.FailException(E: Exception);
  2194. var
  2195. MsgNumber: Integer;
  2196. begin
  2197. if ExpectedErrorClass<>nil then
  2198. begin
  2199. if FExpectedErrorClass=E.ClassType then
  2200. begin
  2201. if E is EPas2JS then
  2202. MsgNumber:=EPas2JS(E).MsgNumber
  2203. else if E is EPasResolve then
  2204. MsgNumber:=EPasResolve(E).MsgNumber
  2205. else if E is EParserError then
  2206. MsgNumber:=Parser.LastMsgNumber
  2207. else if E is EScannerError then
  2208. MsgNumber:=Scanner.LastMsgNumber
  2209. else
  2210. MsgNumber:=0;
  2211. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2212. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2213. ExpectedErrorNumber,MsgNumber);
  2214. end else begin
  2215. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2216. end;
  2217. end;
  2218. Fail(E.Message);
  2219. end;
  2220. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2221. aCol: integer);
  2222. var
  2223. IsSrc: Boolean;
  2224. i, j: Integer;
  2225. SrcLines: TStringList;
  2226. Line: string;
  2227. aModule: TTestEnginePasResolver;
  2228. begin
  2229. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2230. for i:=0 to ResolverCount-1 do
  2231. begin
  2232. aModule:=Resolvers[i];
  2233. SrcLines:=TStringList.Create;
  2234. try
  2235. SrcLines.Text:=aModule.Source;
  2236. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2237. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2238. for j:=1 to SrcLines.Count do
  2239. begin
  2240. Line:=SrcLines[j-1];
  2241. if IsSrc and (j=aRow) then
  2242. begin
  2243. write('*');
  2244. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2245. end;
  2246. writeln(Format('%:4d: ',[j]),Line);
  2247. end;
  2248. finally
  2249. SrcLines.Free;
  2250. end;
  2251. end;
  2252. end;
  2253. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2254. var
  2255. i: Integer;
  2256. begin
  2257. for i:=0 to ResolverCount-1 do
  2258. if Filename=Resolvers[i].Filename then exit(i);
  2259. Result:=-1;
  2260. end;
  2261. function TCustomTestModule.GetResolver(const Filename: string
  2262. ): TTestEnginePasResolver;
  2263. var
  2264. i: Integer;
  2265. begin
  2266. i:=IndexOfResolver(Filename);
  2267. if i<0 then exit(nil);
  2268. Result:=Resolvers[i];
  2269. end;
  2270. function TCustomTestModule.GetDefaultNamespace: string;
  2271. var
  2272. C: TClass;
  2273. begin
  2274. Result:='';
  2275. if FModule=nil then exit;
  2276. C:=FModule.ClassType;
  2277. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2278. Result:=Engine.DefaultNameSpace;
  2279. end;
  2280. constructor TCustomTestModule.Create;
  2281. begin
  2282. inherited Create;
  2283. FHintMsgs:=TObjectList.Create(true);
  2284. FHintMsgsGood:=TFPList.Create;
  2285. end;
  2286. destructor TCustomTestModule.Destroy;
  2287. begin
  2288. FreeAndNil(FHintMsgs);
  2289. FreeAndNil(FHintMsgsGood);
  2290. inherited Destroy;
  2291. end;
  2292. { TTestModule }
  2293. procedure TTestModule.TestReservedWords;
  2294. var
  2295. i: integer;
  2296. begin
  2297. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2298. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2299. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2300. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2301. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2302. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2303. end;
  2304. procedure TTestModule.TestEmptyProgram;
  2305. begin
  2306. StartProgram(false);
  2307. Add('begin');
  2308. ConvertProgram;
  2309. CheckSource('TestEmptyProgram','','');
  2310. end;
  2311. procedure TTestModule.TestEmptyProgramUseStrict;
  2312. begin
  2313. Converter.Options:=Converter.Options+[coUseStrict];
  2314. StartProgram(false);
  2315. Add('begin');
  2316. ConvertProgram;
  2317. CheckSource('TestEmptyProgramUseStrict','','');
  2318. end;
  2319. procedure TTestModule.TestEmptyUnit;
  2320. begin
  2321. StartUnit(false);
  2322. Add('interface');
  2323. Add('implementation');
  2324. ConvertUnit;
  2325. CheckSource('TestEmptyUnit',
  2326. LinesToStr([
  2327. ]),
  2328. '');
  2329. end;
  2330. procedure TTestModule.TestEmptyUnitUseStrict;
  2331. begin
  2332. Converter.Options:=Converter.Options+[coUseStrict];
  2333. StartUnit(false);
  2334. Add('interface');
  2335. Add('implementation');
  2336. ConvertUnit;
  2337. CheckSource('TestEmptyUnitUseStrict',
  2338. LinesToStr([
  2339. ''
  2340. ]),
  2341. '');
  2342. end;
  2343. procedure TTestModule.TestDottedUnitNames;
  2344. begin
  2345. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2346. LinesToStr([
  2347. 'var iV: longint;'
  2348. ]),
  2349. '');
  2350. FFilename:='ns1.test1.pp';
  2351. StartProgram(true);
  2352. Add('uses unIt2;');
  2353. Add('var');
  2354. Add(' i: longint;');
  2355. Add('begin');
  2356. Add(' i:=iv;');
  2357. Add(' i:=uNit2.iv;');
  2358. Add(' i:=Ns1.TEst1.i;');
  2359. ConvertProgram;
  2360. CheckSource('TestDottedUnitNames',
  2361. LinesToStr([
  2362. 'this.i = 0;',
  2363. '']),
  2364. LinesToStr([ // this.$init
  2365. '$mod.i = pas["NS1.Unit2"].iV;',
  2366. '$mod.i = pas["NS1.Unit2"].iV;',
  2367. '$mod.i = $mod.i;',
  2368. '']) );
  2369. end;
  2370. procedure TTestModule.TestDottedUnitNameImpl;
  2371. begin
  2372. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2373. LinesToStr([
  2374. 'type',
  2375. ' TObject = class end;',
  2376. ' TTestA = class',
  2377. ' end;'
  2378. ]),
  2379. LinesToStr(['uses TEST.UnitB;'])
  2380. );
  2381. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2382. LinesToStr([
  2383. 'uses TEST.UnitA;',
  2384. 'type TTestB = class(TTestA);'
  2385. ]),
  2386. ''
  2387. );
  2388. StartProgram(true);
  2389. Add('uses TEST.UnitA;');
  2390. Add('begin');
  2391. ConvertProgram;
  2392. CheckSource('TestDottedUnitNameImpl',
  2393. LinesToStr([
  2394. '']),
  2395. LinesToStr([ // this.$init
  2396. '']) );
  2397. CheckUnit('TEST.UnitA.pas',
  2398. LinesToStr([
  2399. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2400. ' var $mod = this;',
  2401. ' rtl.createClass(this, "TObject", null, function () {',
  2402. ' this.$init = function () {',
  2403. ' };',
  2404. ' this.$final = function () {',
  2405. ' };',
  2406. ' });',
  2407. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2408. ' });',
  2409. '}, ["TEST.UnitB"]);'
  2410. ]));
  2411. CheckUnit('TEST.UnitB.pas',
  2412. LinesToStr([
  2413. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2414. ' var $mod = this;',
  2415. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2416. ' });',
  2417. '});'
  2418. ]));
  2419. end;
  2420. procedure TTestModule.TestDottedUnitExpr;
  2421. begin
  2422. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2423. LinesToStr([
  2424. 'procedure DoIt;'
  2425. ]),
  2426. 'procedure DoIt; begin end;');
  2427. FFilename:='Ns1.SubNs1.Test1.pp';
  2428. StartProgram(true);
  2429. Add('uses Ns2.sUbnS2.unIt2;');
  2430. Add('var');
  2431. Add(' i: longint;');
  2432. Add('begin');
  2433. Add(' ns2.subns2.unit2.doit;');
  2434. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2435. ConvertProgram;
  2436. CheckSource('TestDottedUnitExpr',
  2437. LinesToStr([
  2438. 'this.i = 0;',
  2439. '']),
  2440. LinesToStr([ // this.$init
  2441. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2442. '$mod.i = $mod.i;',
  2443. '']) );
  2444. end;
  2445. procedure TTestModule.Test_ModeFPCFail;
  2446. begin
  2447. StartProgram(false);
  2448. Add('{$mode FPC}');
  2449. Add('begin');
  2450. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2451. ConvertProgram;
  2452. end;
  2453. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2454. begin
  2455. StartProgram(false);
  2456. Add('{$modeswitch cblocks-}');
  2457. Add('begin');
  2458. ConvertProgram;
  2459. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2460. CheckResolverUnexpectedHints();
  2461. end;
  2462. procedure TTestModule.TestUnit_UseSystem;
  2463. begin
  2464. StartUnit(true);
  2465. Add([
  2466. 'interface',
  2467. 'var i: integer;',
  2468. 'implementation']);
  2469. ConvertUnit;
  2470. CheckSource('TestUnit_UseSystem',
  2471. LinesToStr([
  2472. 'this.i = 0;',
  2473. '']),
  2474. LinesToStr([
  2475. '']) );
  2476. end;
  2477. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2478. begin
  2479. AddModuleWithIntfImplSrc('unit1.pp',
  2480. LinesToStr([
  2481. 'type number = longint;']),
  2482. LinesToStr([
  2483. 'uses test1;',
  2484. 'procedure DoIt;',
  2485. 'begin',
  2486. ' i:=3;',
  2487. 'end;']));
  2488. StartUnit(true);
  2489. Add([
  2490. 'interface',
  2491. 'uses unit1;',
  2492. 'var i: number;',
  2493. 'implementation']);
  2494. ConvertUnit;
  2495. CheckSource('TestUnit_Intf1Impl2Intf1',
  2496. LinesToStr([
  2497. 'this.i = 0;',
  2498. '']),
  2499. LinesToStr([
  2500. '']) );
  2501. end;
  2502. procedure TTestModule.TestIncludeVersion;
  2503. begin
  2504. StartProgram(false);
  2505. Add([
  2506. 'var',
  2507. ' s: string;',
  2508. ' i: word;',
  2509. 'begin',
  2510. ' s:={$I %line%};',
  2511. ' i:={$I %linenum%};',
  2512. ' s:={$I %currentroutine%};',
  2513. ' s:={$I %pas2jsversion%};',
  2514. ' s:={$I %pas2jstarget%};',
  2515. ' s:={$I %pas2jstargetos%};',
  2516. ' s:={$I %pas2jstargetcpu%};',
  2517. ' s:={$I %file%};',
  2518. '']);
  2519. ConvertProgram;
  2520. CheckSource('TestIncludeVersion',
  2521. LinesToStr([
  2522. 'this.s="";',
  2523. 'this.i = 0;']),
  2524. LinesToStr([
  2525. '$mod.s = "7";',
  2526. '$mod.i = 8;',
  2527. '$mod.s = "<anonymous>";',
  2528. '$mod.s = "Comp.Ver.tcmodules";',
  2529. '$mod.s = "Browser";',
  2530. '$mod.s = "Browser";',
  2531. '$mod.s = "ECMAScript5";',
  2532. '$mod.s = "test1.pp";',
  2533. '']));
  2534. end;
  2535. procedure TTestModule.TestVarInt;
  2536. begin
  2537. StartProgram(false);
  2538. Add('var MyI: longint;');
  2539. Add('begin');
  2540. ConvertProgram;
  2541. CheckSource('TestVarInt','this.MyI=0;','');
  2542. end;
  2543. procedure TTestModule.TestVarBaseTypes;
  2544. begin
  2545. StartProgram(false);
  2546. Add('var');
  2547. Add(' i: longint;');
  2548. Add(' s: string;');
  2549. Add(' c: char;');
  2550. Add(' b: boolean;');
  2551. Add(' d: double;');
  2552. Add(' i2: longint = 3;');
  2553. Add(' s2: string = ''foo'';');
  2554. Add(' c2: char = ''4'';');
  2555. Add(' b2: boolean = true;');
  2556. Add(' d2: double = 5.6;');
  2557. Add(' i3: longint = $707;');
  2558. Add(' i4: nativeint = 9007199254740991;');
  2559. Add(' i5: nativeint = -9007199254740991-1;');
  2560. Add(' i6: nativeint = $fffffffffffff;');
  2561. Add(' i7: nativeint = -$fffffffffffff-1;');
  2562. Add(' i8: byte = 00;');
  2563. Add(' u8: nativeuint = $fffffffffffff;');
  2564. Add(' u9: nativeuint = $0000000000000;');
  2565. Add(' u10: nativeuint = $00ff00;');
  2566. Add('begin');
  2567. ConvertProgram;
  2568. CheckSource('TestVarBaseTypes',
  2569. LinesToStr([
  2570. 'this.i = 0;',
  2571. 'this.s = "";',
  2572. 'this.c = "";',
  2573. 'this.b = false;',
  2574. 'this.d = 0.0;',
  2575. 'this.i2 = 3;',
  2576. 'this.s2 = "foo";',
  2577. 'this.c2 = "4";',
  2578. 'this.b2 = true;',
  2579. 'this.d2 = 5.6;',
  2580. 'this.i3 = 0x707;',
  2581. 'this.i4 = 9007199254740991;',
  2582. 'this.i5 = -9007199254740991-1;',
  2583. 'this.i6 = 0xfffffffffffff;',
  2584. 'this.i7 =-0xfffffffffffff-1;',
  2585. 'this.i8 = 0;',
  2586. 'this.u8 = 0xfffffffffffff;',
  2587. 'this.u9 = 0x0;',
  2588. 'this.u10 = 0xff00;'
  2589. ]),
  2590. '');
  2591. end;
  2592. procedure TTestModule.TestBaseTypeSingleFail;
  2593. begin
  2594. StartProgram(false);
  2595. Add('var s: single;');
  2596. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2597. ConvertProgram;
  2598. end;
  2599. procedure TTestModule.TestBaseTypeExtendedFail;
  2600. begin
  2601. StartProgram(false);
  2602. Add('var e: extended;');
  2603. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2604. ConvertProgram;
  2605. end;
  2606. procedure TTestModule.TestConstBaseTypes;
  2607. begin
  2608. StartProgram(false);
  2609. Add('const');
  2610. Add(' i: longint = 3;');
  2611. Add(' s: string = ''foo'';');
  2612. Add(' c: char = ''4'';');
  2613. Add(' b: boolean = true;');
  2614. Add(' d: double = 5.6;');
  2615. Add(' e = low(word);');
  2616. Add(' f = high(word);');
  2617. Add('begin');
  2618. ConvertProgram;
  2619. CheckSource('TestVarBaseTypes',
  2620. LinesToStr([
  2621. 'this.i=3;',
  2622. 'this.s="foo";',
  2623. 'this.c="4";',
  2624. 'this.b=true;',
  2625. 'this.d=5.6;',
  2626. 'this.e = 0;',
  2627. 'this.f = 65535;'
  2628. ]),
  2629. '');
  2630. end;
  2631. procedure TTestModule.TestAliasTypeRef;
  2632. begin
  2633. StartProgram(false);
  2634. Add('type');
  2635. Add(' a=longint;');
  2636. Add(' b=a;');
  2637. Add('var');
  2638. Add(' c: A;');
  2639. Add(' d: B;');
  2640. Add('begin');
  2641. ConvertProgram;
  2642. CheckSource('TestAliasTypeRef',
  2643. LinesToStr([ // statements
  2644. 'this.c = 0;',
  2645. 'this.d = 0;'
  2646. ]),
  2647. LinesToStr([ // this.$main
  2648. ''
  2649. ]));
  2650. end;
  2651. procedure TTestModule.TestTypeCast_BaseTypes;
  2652. begin
  2653. StartProgram(false);
  2654. Add([
  2655. 'var',
  2656. ' i: longint;',
  2657. ' b: boolean;',
  2658. ' d: double;',
  2659. ' s: string;',
  2660. ' c: char;',
  2661. 'begin',
  2662. ' i:=longint(i);',
  2663. ' i:=longint(b);',
  2664. ' b:=boolean(b);',
  2665. ' b:=boolean(i);',
  2666. ' d:=double(d);',
  2667. ' d:=double(i);',
  2668. ' s:=string(s);',
  2669. ' s:=string(c);',
  2670. ' c:=char(c);',
  2671. ' c:=char(i);',
  2672. ' c:=char(65);',
  2673. ' c:=char(#10);',
  2674. ' c:=char(#$E000);',
  2675. '']);
  2676. ConvertProgram;
  2677. CheckSource('TestAliasTypeRef',
  2678. LinesToStr([ // statements
  2679. 'this.i = 0;',
  2680. 'this.b = false;',
  2681. 'this.d = 0.0;',
  2682. 'this.s = "";',
  2683. 'this.c = "";',
  2684. '']),
  2685. LinesToStr([ // this.$main
  2686. '$mod.i = $mod.i;',
  2687. '$mod.i = ($mod.b ? 1 : 0);',
  2688. '$mod.b = $mod.b;',
  2689. '$mod.b = $mod.i != 0;',
  2690. '$mod.d = $mod.d;',
  2691. '$mod.d = $mod.i;',
  2692. '$mod.s = $mod.s;',
  2693. '$mod.s = $mod.c;',
  2694. '$mod.c = $mod.c;',
  2695. '$mod.c = String.fromCharCode($mod.i);',
  2696. '$mod.c = "A";',
  2697. '$mod.c = "\n";',
  2698. '$mod.c = "";',
  2699. '']));
  2700. end;
  2701. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2702. begin
  2703. StartProgram(false);
  2704. Add('type');
  2705. Add(' integer = longint;');
  2706. Add(' TYesNo = boolean;');
  2707. Add(' TFloat = double;');
  2708. Add(' TCaption = string;');
  2709. Add(' TChar = char;');
  2710. Add('var');
  2711. Add(' i: integer;');
  2712. Add(' b: TYesNo;');
  2713. Add(' d: TFloat;');
  2714. Add(' s: TCaption;');
  2715. Add(' c: TChar;');
  2716. Add('begin');
  2717. Add(' i:=integer(i);');
  2718. Add(' i:=integer(b);');
  2719. Add(' b:=TYesNo(b);');
  2720. Add(' b:=TYesNo(i);');
  2721. Add(' d:=TFloat(d);');
  2722. Add(' d:=TFloat(i);');
  2723. Add(' s:=TCaption(s);');
  2724. Add(' s:=TCaption(c);');
  2725. Add(' c:=TChar(c);');
  2726. ConvertProgram;
  2727. CheckSource('TestAliasTypeRef',
  2728. LinesToStr([ // statements
  2729. 'this.i = 0;',
  2730. 'this.b = false;',
  2731. 'this.d = 0.0;',
  2732. 'this.s = "";',
  2733. 'this.c = "";',
  2734. '']),
  2735. LinesToStr([ // this.$main
  2736. '$mod.i = $mod.i;',
  2737. '$mod.i = ($mod.b ? 1 : 0);',
  2738. '$mod.b = $mod.b;',
  2739. '$mod.b = $mod.i != 0;',
  2740. '$mod.d = $mod.d;',
  2741. '$mod.d = $mod.i;',
  2742. '$mod.s = $mod.s;',
  2743. '$mod.s = $mod.c;',
  2744. '$mod.c = $mod.c;',
  2745. '']));
  2746. end;
  2747. procedure TTestModule.TestEmptyProc;
  2748. begin
  2749. StartProgram(false);
  2750. Add('procedure Test;');
  2751. Add('begin');
  2752. Add('end;');
  2753. Add('begin');
  2754. ConvertProgram;
  2755. CheckSource('TestEmptyProc',
  2756. LinesToStr([ // statements
  2757. 'this.Test = function () {',
  2758. '};'
  2759. ]),
  2760. LinesToStr([ // this.$main
  2761. ''
  2762. ]));
  2763. end;
  2764. procedure TTestModule.TestProcOneParam;
  2765. begin
  2766. StartProgram(false);
  2767. Add('procedure ProcA(i: longint);');
  2768. Add('begin');
  2769. Add('end;');
  2770. Add('begin');
  2771. Add(' PROCA(3);');
  2772. ConvertProgram;
  2773. CheckSource('TestProcOneParam',
  2774. LinesToStr([ // statements
  2775. 'this.ProcA = function (i) {',
  2776. '};'
  2777. ]),
  2778. LinesToStr([ // this.$main
  2779. '$mod.ProcA(3);'
  2780. ]));
  2781. end;
  2782. procedure TTestModule.TestFunctionWithoutParams;
  2783. begin
  2784. StartProgram(false);
  2785. Add('function FuncA: longint;');
  2786. Add('begin');
  2787. Add('end;');
  2788. Add('var i: longint;');
  2789. Add('begin');
  2790. Add(' I:=FUNCA();');
  2791. Add(' I:=FUNCA;');
  2792. Add(' FUNCA();');
  2793. Add(' FUNCA;');
  2794. ConvertProgram;
  2795. CheckSource('TestProcWithoutParams',
  2796. LinesToStr([ // statements
  2797. 'this.FuncA = function () {',
  2798. ' var Result = 0;',
  2799. ' return Result;',
  2800. '};',
  2801. 'this.i=0;'
  2802. ]),
  2803. LinesToStr([ // this.$main
  2804. '$mod.i=$mod.FuncA();',
  2805. '$mod.i=$mod.FuncA();',
  2806. '$mod.FuncA();',
  2807. '$mod.FuncA();'
  2808. ]));
  2809. end;
  2810. procedure TTestModule.TestProcedureWithoutParams;
  2811. begin
  2812. StartProgram(false);
  2813. Add('procedure ProcA;');
  2814. Add('begin');
  2815. Add('end;');
  2816. Add('begin');
  2817. Add(' PROCA();');
  2818. Add(' PROCA;');
  2819. ConvertProgram;
  2820. CheckSource('TestProcWithoutParams',
  2821. LinesToStr([ // statements
  2822. 'this.ProcA = function () {',
  2823. '};'
  2824. ]),
  2825. LinesToStr([ // this.$main
  2826. '$mod.ProcA();',
  2827. '$mod.ProcA();'
  2828. ]));
  2829. end;
  2830. procedure TTestModule.TestIncDec;
  2831. begin
  2832. StartProgram(false);
  2833. Add([
  2834. 'procedure DoIt(var i: longint);',
  2835. 'begin',
  2836. ' inc(i);',
  2837. ' inc(i,2);',
  2838. 'end;',
  2839. 'var',
  2840. ' Bar: longint;',
  2841. 'begin',
  2842. ' inc(bar);',
  2843. ' inc(bar,2);',
  2844. ' dec(bar);',
  2845. ' dec(bar,3);',
  2846. '']);
  2847. ConvertProgram;
  2848. CheckSource('TestIncDec',
  2849. LinesToStr([ // statements
  2850. 'this.DoIt = function (i) {',
  2851. ' i.set(i.get()+1);',
  2852. ' i.set(i.get()+2);',
  2853. '};',
  2854. 'this.Bar = 0;'
  2855. ]),
  2856. LinesToStr([ // this.$main
  2857. '$mod.Bar+=1;',
  2858. '$mod.Bar+=2;',
  2859. '$mod.Bar-=1;',
  2860. '$mod.Bar-=3;'
  2861. ]));
  2862. end;
  2863. procedure TTestModule.TestLoHiFpcMode;
  2864. begin
  2865. StartProgram(false);
  2866. Add([
  2867. '{$mode objfpc}',
  2868. 'const',
  2869. ' LoByte1 = Lo(Word($1234));',
  2870. ' HiByte1 = Hi(Word($1234));',
  2871. ' LoByte2 = Lo(SmallInt($1234));',
  2872. ' HiByte2 = Hi(SmallInt($1234));',
  2873. ' LoWord1 = Lo($1234CDEF);',
  2874. ' HiWord1 = Hi($1234CDEF);',
  2875. ' LoWord2 = Lo(-$1234CDEF);',
  2876. ' HiWord2 = Hi(-$1234CDEF);',
  2877. ' lo4:byte=lo(byte($34));',
  2878. ' hi4:byte=hi(byte($34));',
  2879. ' lo5:byte=lo(shortint(-$34));',
  2880. ' hi5:byte=hi(shortint(-$34));',
  2881. ' lo6:longword=lo($123456789ABCD);',
  2882. ' hi6:longword=hi($123456789ABCD);',
  2883. ' lo7:longword=lo(-$123456789ABCD);',
  2884. ' hi7:longword=hi(-$123456789ABCD);',
  2885. 'var',
  2886. ' b: Byte;',
  2887. ' ss: shortint;',
  2888. ' w: Word;',
  2889. ' si: SmallInt;',
  2890. ' lw: LongWord;',
  2891. ' li: LongInt;',
  2892. ' b2: Byte;',
  2893. ' ni: nativeint;',
  2894. 'begin',
  2895. ' w := $1234;',
  2896. ' ss := -$12;',
  2897. ' b := lo(ss);',
  2898. ' b := HI(ss);',
  2899. ' b := lo(w);',
  2900. ' b := HI(w);',
  2901. ' b2 := lo(b);',
  2902. ' b2 := hi(b);',
  2903. ' lw := $1234CDEF;',
  2904. ' w := lo(lw);',
  2905. ' w := hi(lw);',
  2906. ' ni := $123456789ABCD;',
  2907. ' lw := lo(ni);',
  2908. ' lw := hi(ni);',
  2909. '']);
  2910. ConvertProgram;
  2911. CheckSource('TestLoHiFpcMode',
  2912. LinesToStr([ // statements
  2913. 'this.LoByte1 = 0x1234 & 0xFF;',
  2914. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2915. 'this.LoByte2 = 0x1234 & 0xFF;',
  2916. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2917. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2918. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2919. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2920. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2921. 'this.lo4 = 0x34 & 0xF;',
  2922. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2923. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2924. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2925. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2926. 'this.hi6 = 74565 >>> 0;',
  2927. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2928. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2929. 'this.b = 0;',
  2930. 'this.ss = 0;',
  2931. 'this.w = 0;',
  2932. 'this.si = 0;',
  2933. 'this.lw = 0;',
  2934. 'this.li = 0;',
  2935. 'this.b2 = 0;',
  2936. 'this.ni = 0;',
  2937. '']),
  2938. LinesToStr([ // this.$main
  2939. '$mod.w = 0x1234;',
  2940. '$mod.ss = -0x12;',
  2941. '$mod.b = $mod.ss & 0xFF;',
  2942. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2943. '$mod.b = $mod.w & 0xFF;',
  2944. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2945. '$mod.b2 = $mod.b & 0xF;',
  2946. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2947. '$mod.lw = 0x1234CDEF;',
  2948. '$mod.w = $mod.lw & 0xFFFF;',
  2949. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2950. '$mod.ni = 0x123456789ABCD;',
  2951. '$mod.lw = $mod.ni >>> 0;',
  2952. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2953. '']));
  2954. end;
  2955. procedure TTestModule.TestLoHiDelphiMode;
  2956. begin
  2957. StartProgram(false);
  2958. Add([
  2959. '{$mode delphi}',
  2960. 'const',
  2961. ' LoByte1 = Lo(Word($1234));',
  2962. ' HiByte1 = Hi(Word($1234));',
  2963. ' LoByte2 = Lo(SmallInt($1234));',
  2964. ' HiByte2 = Hi(SmallInt($1234));',
  2965. ' LoByte3 = Lo($1234CDEF);',
  2966. ' HiByte3 = Hi($1234CDEF);',
  2967. ' LoByte4 = Lo(-$1234CDEF);',
  2968. ' HiByte4 = Hi(-$1234CDEF);',
  2969. 'var',
  2970. ' b: Byte;',
  2971. ' w: Word;',
  2972. ' si: SmallInt;',
  2973. ' lw: LongWord;',
  2974. ' li: LongInt;',
  2975. 'begin',
  2976. ' w := $1234;',
  2977. ' b := lo(w);',
  2978. ' b := HI(w);',
  2979. ' lw := $1234CDEF;',
  2980. ' b := lo(lw);',
  2981. ' b := hi(lw);',
  2982. '']);
  2983. ConvertProgram;
  2984. CheckSource('TestLoHiDelphiMode',
  2985. LinesToStr([ // statements
  2986. 'this.LoByte1 = 0x1234 & 0xFF;',
  2987. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2988. 'this.LoByte2 = 0x1234 & 0xFF;',
  2989. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2990. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2991. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2992. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2993. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2994. 'this.b = 0;',
  2995. 'this.w = 0;',
  2996. 'this.si = 0;',
  2997. 'this.lw = 0;',
  2998. 'this.li = 0;'
  2999. ]),
  3000. LinesToStr([ // this.$main
  3001. '$mod.w = 0x1234;',
  3002. '$mod.b = $mod.w & 0xFF;',
  3003. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3004. '$mod.lw = 0x1234CDEF;',
  3005. '$mod.b = $mod.lw & 0xFF;',
  3006. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3007. ]));
  3008. end;
  3009. procedure TTestModule.TestAssignments;
  3010. begin
  3011. StartProgram(false);
  3012. Parser.Options:=Parser.Options+[po_cassignments];
  3013. Add('var');
  3014. Add(' Bar:longint;');
  3015. Add('begin');
  3016. Add(' bar:=3;');
  3017. Add(' bar+=4;');
  3018. Add(' bar-=5;');
  3019. Add(' bar*=6;');
  3020. ConvertProgram;
  3021. CheckSource('TestAssignments',
  3022. LinesToStr([ // statements
  3023. 'this.Bar = 0;'
  3024. ]),
  3025. LinesToStr([ // this.$main
  3026. '$mod.Bar=3;',
  3027. '$mod.Bar+=4;',
  3028. '$mod.Bar-=5;',
  3029. '$mod.Bar*=6;'
  3030. ]));
  3031. end;
  3032. procedure TTestModule.TestArithmeticOperators1;
  3033. begin
  3034. StartProgram(false);
  3035. Add('var');
  3036. Add(' vA,vB,vC:longint;');
  3037. Add('begin');
  3038. Add(' va:=1;');
  3039. Add(' vb:=va+va;');
  3040. Add(' vb:=va div vb;');
  3041. Add(' vb:=va mod vb;');
  3042. Add(' vb:=va+va*vb+va div vb;');
  3043. Add(' vc:=-va;');
  3044. Add(' va:=va-vb;');
  3045. Add(' vb:=va;');
  3046. Add(' if va<vb then vc:=va else vc:=vb;');
  3047. ConvertProgram;
  3048. CheckSource('TestArithmeticOperators1',
  3049. LinesToStr([ // statements
  3050. 'this.vA = 0;',
  3051. 'this.vB = 0;',
  3052. 'this.vC = 0;'
  3053. ]),
  3054. LinesToStr([ // this.$main
  3055. '$mod.vA = 1;',
  3056. '$mod.vB = $mod.vA + $mod.vA;',
  3057. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3058. '$mod.vB = $mod.vA % $mod.vB;',
  3059. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3060. '$mod.vC = -$mod.vA;',
  3061. '$mod.vA = $mod.vA - $mod.vB;',
  3062. '$mod.vB = $mod.vA;',
  3063. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3064. ]));
  3065. end;
  3066. procedure TTestModule.TestLogicalOperators;
  3067. begin
  3068. StartProgram(false);
  3069. Add('var');
  3070. Add(' vA,vB,vC:boolean;');
  3071. Add('begin');
  3072. Add(' va:=vb and vc;');
  3073. Add(' va:=vb or vc;');
  3074. Add(' va:=vb xor vc;');
  3075. Add(' va:=true and vc;');
  3076. Add(' va:=(vb and vc) or (va and vb);');
  3077. Add(' va:=not vb;');
  3078. ConvertProgram;
  3079. CheckSource('TestLogicalOperators',
  3080. LinesToStr([ // statements
  3081. 'this.vA = false;',
  3082. 'this.vB = false;',
  3083. 'this.vC = false;'
  3084. ]),
  3085. LinesToStr([ // this.$main
  3086. '$mod.vA = $mod.vB && $mod.vC;',
  3087. '$mod.vA = $mod.vB || $mod.vC;',
  3088. '$mod.vA = $mod.vB ^ $mod.vC;',
  3089. '$mod.vA = true && $mod.vC;',
  3090. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3091. '$mod.vA = !$mod.vB;'
  3092. ]));
  3093. end;
  3094. procedure TTestModule.TestBitwiseOperators;
  3095. begin
  3096. StartProgram(false);
  3097. Add([
  3098. 'var',
  3099. ' vA,vB,vC:longint;',
  3100. ' X,Y,Z: nativeint;',
  3101. 'begin',
  3102. ' va:=vb and vc;',
  3103. ' va:=vb or vc;',
  3104. ' va:=vb xor vc;',
  3105. ' va:=vb shl vc;',
  3106. ' va:=vb shr vc;',
  3107. ' va:=3 and vc;',
  3108. ' va:=(vb and vc) or (va and vb);',
  3109. ' va:=not vb;',
  3110. ' X:=Y and Z;',
  3111. ' X:=Y and va;',
  3112. ' X:=Y or Z;',
  3113. ' X:=Y or va;',
  3114. ' X:=Y xor Z;',
  3115. ' X:=Y xor va;',
  3116. '']);
  3117. ConvertProgram;
  3118. CheckSource('TestBitwiseOperators',
  3119. LinesToStr([ // statements
  3120. 'this.vA = 0;',
  3121. 'this.vB = 0;',
  3122. 'this.vC = 0;',
  3123. 'this.X = 0;',
  3124. 'this.Y = 0;',
  3125. 'this.Z = 0;',
  3126. '']),
  3127. LinesToStr([ // this.$main
  3128. '$mod.vA = $mod.vB & $mod.vC;',
  3129. '$mod.vA = $mod.vB | $mod.vC;',
  3130. '$mod.vA = $mod.vB ^ $mod.vC;',
  3131. '$mod.vA = $mod.vB << $mod.vC;',
  3132. '$mod.vA = $mod.vB >>> $mod.vC;',
  3133. '$mod.vA = 3 & $mod.vC;',
  3134. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3135. '$mod.vA = ~$mod.vB;',
  3136. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3137. '$mod.X = $mod.Y & $mod.vA;',
  3138. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3139. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3140. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3141. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3142. '']));
  3143. end;
  3144. procedure TTestModule.TestBitwiseOperatorsLongword;
  3145. begin
  3146. StartProgram(false);
  3147. Add([
  3148. 'var',
  3149. ' a,b,c:longword;',
  3150. ' i: longint;',
  3151. 'begin',
  3152. ' a:=$12345678;',
  3153. ' b:=$EDCBA987;',
  3154. ' c:=not a;',
  3155. ' c:=a and b;',
  3156. ' c:=a and $ffff0000;',
  3157. ' c:=a or b;',
  3158. ' c:=a or $ff00ff00;',
  3159. ' c:=a xor b;',
  3160. ' c:=a xor $f0f0f0f0;',
  3161. ' c:=a shl 1;',
  3162. ' c:=a shl 16;',
  3163. ' c:=a shl 24;',
  3164. ' c:=a shl b;',
  3165. ' c:=a shr 1;',
  3166. ' c:=a shr 16;',
  3167. ' c:=a shr 24;',
  3168. ' c:=a shr b;',
  3169. ' c:=(b and c) or (a and b);',
  3170. ' c:=i and a;',
  3171. ' c:=i or a;',
  3172. ' c:=i xor a;',
  3173. '']);
  3174. ConvertProgram;
  3175. CheckSource('TestBitwiseOperatorsLongword',
  3176. LinesToStr([ // statements
  3177. 'this.a = 0;',
  3178. 'this.b = 0;',
  3179. 'this.c = 0;',
  3180. 'this.i = 0;',
  3181. '']),
  3182. LinesToStr([ // this.$main
  3183. '$mod.a = 0x12345678;',
  3184. '$mod.b = 0xEDCBA987;',
  3185. '$mod.c = rtl.lw(~$mod.a);',
  3186. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3187. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3188. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3189. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3190. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3191. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3192. '$mod.c = rtl.lw($mod.a << 1);',
  3193. '$mod.c = rtl.lw($mod.a << 16);',
  3194. '$mod.c = rtl.lw($mod.a << 24);',
  3195. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3196. '$mod.c = rtl.lw($mod.a >>> 1);',
  3197. '$mod.c = rtl.lw($mod.a >>> 16);',
  3198. '$mod.c = rtl.lw($mod.a >>> 24);',
  3199. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3200. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3201. '$mod.c = $mod.i & $mod.a;',
  3202. '$mod.c = $mod.i | $mod.a;',
  3203. '$mod.c = $mod.i ^ $mod.a;',
  3204. '']));
  3205. end;
  3206. procedure TTestModule.TestPrgProcVar;
  3207. begin
  3208. StartProgram(false);
  3209. Add('procedure Proc1;');
  3210. Add('type');
  3211. Add(' t1=longint;');
  3212. Add('var');
  3213. Add(' vA:t1;');
  3214. Add('begin');
  3215. Add('end;');
  3216. Add('begin');
  3217. ConvertProgram;
  3218. CheckSource('TestPrgProcVar',
  3219. LinesToStr([ // statements
  3220. 'this.Proc1 = function () {',
  3221. ' var vA=0;',
  3222. '};'
  3223. ]),
  3224. LinesToStr([ // this.$main
  3225. ''
  3226. ]));
  3227. end;
  3228. procedure TTestModule.TestUnitProcVar;
  3229. begin
  3230. StartUnit(false);
  3231. Add('interface');
  3232. Add('');
  3233. Add('type tA=string; // unit scope');
  3234. Add('procedure Proc1;');
  3235. Add('');
  3236. Add('implementation');
  3237. Add('');
  3238. Add('procedure Proc1;');
  3239. Add('type tA=longint; // local proc scope');
  3240. Add('var v1:tA; // using local tA');
  3241. Add('begin');
  3242. Add('end;');
  3243. Add('var v2:tA; // using interface tA');
  3244. ConvertUnit;
  3245. CheckSource('TestUnitProcVar',
  3246. LinesToStr([ // statements
  3247. 'var $impl = $mod.$impl;',
  3248. 'this.Proc1 = function () {',
  3249. ' var v1 = 0;',
  3250. '};',
  3251. '']),
  3252. // this.$init
  3253. '',
  3254. // implementation
  3255. LinesToStr([
  3256. '$impl.v2 = "";',
  3257. '']));
  3258. end;
  3259. procedure TTestModule.TestImplProc;
  3260. begin
  3261. StartUnit(false);
  3262. Add('interface');
  3263. Add('');
  3264. Add('procedure Proc1;');
  3265. Add('');
  3266. Add('implementation');
  3267. Add('');
  3268. Add('procedure Proc1; begin end;');
  3269. Add('procedure Proc2; begin end;');
  3270. Add('initialization');
  3271. Add(' Proc1;');
  3272. Add(' Proc2;');
  3273. ConvertUnit;
  3274. CheckSource('TestImplProc',
  3275. LinesToStr([ // statements
  3276. 'var $impl = $mod.$impl;',
  3277. 'this.Proc1 = function () {',
  3278. '};',
  3279. '']),
  3280. LinesToStr([ // this.$init
  3281. '$mod.Proc1();',
  3282. '$impl.Proc2();',
  3283. '']),
  3284. LinesToStr([ // implementation
  3285. '$impl.Proc2 = function () {',
  3286. '};',
  3287. ''])
  3288. );
  3289. end;
  3290. procedure TTestModule.TestFunctionResult;
  3291. begin
  3292. StartProgram(false);
  3293. Add('function Func1: longint;');
  3294. Add('begin');
  3295. Add(' Result:=3;');
  3296. Add(' Func1:=4;');
  3297. Add('end;');
  3298. Add('begin');
  3299. ConvertProgram;
  3300. CheckSource('TestFunctionResult',
  3301. LinesToStr([ // statements
  3302. 'this.Func1 = function () {',
  3303. ' var Result = 0;',
  3304. ' Result = 3;',
  3305. ' Result = 4;',
  3306. ' return Result;',
  3307. '};'
  3308. ]),
  3309. '');
  3310. end;
  3311. procedure TTestModule.TestNestedProc;
  3312. begin
  3313. StartProgram(false);
  3314. Add([
  3315. 'var vInUnit: longint;',
  3316. 'function DoIt(pA,pD: longint): longint;',
  3317. 'var',
  3318. ' vB: longint;',
  3319. ' vC: longint;',
  3320. ' function Nesty(pA: longint): longint; ',
  3321. ' var vB: longint;',
  3322. ' begin',
  3323. ' Result:=pa+vb+vc+pd+vInUnit;',
  3324. ' nesty:=3;',
  3325. ' doit:=4;',
  3326. ' exit;',
  3327. ' end;',
  3328. 'begin',
  3329. ' Result:=pa+vb+vc;',
  3330. ' doit:=6;',
  3331. ' exit;',
  3332. 'end;',
  3333. 'begin']);
  3334. ConvertProgram;
  3335. CheckSource('TestNestedProc',
  3336. LinesToStr([ // statements
  3337. 'this.vInUnit = 0;',
  3338. 'this.DoIt = function (pA, pD) {',
  3339. ' var Result = 0;',
  3340. ' var vB = 0;',
  3341. ' var vC = 0;',
  3342. ' function Nesty(pA) {',
  3343. ' var Result$1 = 0;',
  3344. ' var vB = 0;',
  3345. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3346. ' Result$1 = 3;',
  3347. ' Result = 4;',
  3348. ' return Result$1;',
  3349. ' return Result$1;',
  3350. ' };',
  3351. ' Result = pA + vB + vC;',
  3352. ' Result = 6;',
  3353. ' return Result;',
  3354. ' return Result;',
  3355. '};'
  3356. ]),
  3357. '');
  3358. end;
  3359. procedure TTestModule.TestNestedProc_ResultString;
  3360. begin
  3361. StartProgram(false);
  3362. Add([
  3363. 'function DoIt: string;',
  3364. ' function Nesty: string; ',
  3365. ' begin',
  3366. ' nesty:=#65#66;',
  3367. ' nesty[1]:=#67;',
  3368. ' doit:=#68;',
  3369. ' doit[2]:=#69;',
  3370. ' end;',
  3371. 'begin',
  3372. ' doit:=#70;',
  3373. ' doit[3]:=#71;',
  3374. 'end;',
  3375. 'begin']);
  3376. ConvertProgram;
  3377. CheckSource('TestNestedProc_ResultString',
  3378. LinesToStr([ // statements
  3379. 'this.DoIt = function () {',
  3380. ' var Result = "";',
  3381. ' function Nesty() {',
  3382. ' var Result$1 = "";',
  3383. ' Result$1 = "AB";',
  3384. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3385. ' Result = "D";',
  3386. ' Result = rtl.setCharAt(Result, 1, "E");',
  3387. ' return Result$1;',
  3388. ' };',
  3389. ' Result = "F";',
  3390. ' Result = rtl.setCharAt(Result, 2, "G");',
  3391. ' return Result;',
  3392. '};'
  3393. ]),
  3394. '');
  3395. end;
  3396. procedure TTestModule.TestForwardProc;
  3397. begin
  3398. StartProgram(false);
  3399. Add('procedure FuncA(Bar: longint); forward;');
  3400. Add('procedure FuncB(Bar: longint);');
  3401. Add('begin');
  3402. Add(' funca(bar);');
  3403. Add('end;');
  3404. Add('procedure funca(bar: longint);');
  3405. Add('begin');
  3406. Add(' if bar=3 then ;');
  3407. Add('end;');
  3408. Add('begin');
  3409. Add(' funca(4);');
  3410. Add(' funcb(5);');
  3411. ConvertProgram;
  3412. CheckSource('TestForwardProc',
  3413. LinesToStr([ // statements'
  3414. 'this.FuncB = function (Bar) {',
  3415. ' $mod.FuncA(Bar);',
  3416. '};',
  3417. 'this.FuncA = function (Bar) {',
  3418. ' if (Bar === 3);',
  3419. '};'
  3420. ]),
  3421. LinesToStr([
  3422. '$mod.FuncA(4);',
  3423. '$mod.FuncB(5);'
  3424. ])
  3425. );
  3426. end;
  3427. procedure TTestModule.TestNestedForwardProc;
  3428. begin
  3429. StartProgram(false);
  3430. Add('procedure FuncA;');
  3431. Add(' procedure FuncB(i: longint); forward;');
  3432. Add(' procedure FuncC(i: longint);');
  3433. Add(' begin');
  3434. Add(' funcb(i);');
  3435. Add(' end;');
  3436. Add(' procedure FuncB(i: longint);');
  3437. Add(' begin');
  3438. Add(' if i=3 then ;');
  3439. Add(' end;');
  3440. Add('begin');
  3441. Add(' funcc(4)');
  3442. Add('end;');
  3443. Add('begin');
  3444. Add(' funca;');
  3445. ConvertProgram;
  3446. CheckSource('TestNestedForwardProc',
  3447. LinesToStr([ // statements'
  3448. 'this.FuncA = function () {',
  3449. ' function FuncC(i) {',
  3450. ' FuncB(i);',
  3451. ' };',
  3452. ' function FuncB(i) {',
  3453. ' if (i === 3);',
  3454. ' };',
  3455. ' FuncC(4);',
  3456. '};'
  3457. ]),
  3458. LinesToStr([
  3459. '$mod.FuncA();'
  3460. ])
  3461. );
  3462. end;
  3463. procedure TTestModule.TestAssignFunctionResult;
  3464. begin
  3465. StartProgram(false);
  3466. Add('function Func1: longint;');
  3467. Add('begin');
  3468. Add('end;');
  3469. Add('var i: longint;');
  3470. Add('begin');
  3471. Add(' i:=func1();');
  3472. Add(' i:=func1()+func1();');
  3473. ConvertProgram;
  3474. CheckSource('TestAssignFunctionResult',
  3475. LinesToStr([ // statements
  3476. 'this.Func1 = function () {',
  3477. ' var Result = 0;',
  3478. ' return Result;',
  3479. '};',
  3480. 'this.i = 0;'
  3481. ]),
  3482. LinesToStr([
  3483. '$mod.i = $mod.Func1();',
  3484. '$mod.i = $mod.Func1() + $mod.Func1();'
  3485. ]));
  3486. end;
  3487. procedure TTestModule.TestFunctionResultInCondition;
  3488. begin
  3489. StartProgram(false);
  3490. Add('function Func1: longint;');
  3491. Add('begin');
  3492. Add('end;');
  3493. Add('function Func2: boolean;');
  3494. Add('begin');
  3495. Add('end;');
  3496. Add('var i: longint;');
  3497. Add('begin');
  3498. Add(' if func2 then ;');
  3499. Add(' if i=func1() then ;');
  3500. Add(' if i=func1 then ;');
  3501. ConvertProgram;
  3502. CheckSource('TestFunctionResultInCondition',
  3503. LinesToStr([ // statements
  3504. 'this.Func1 = function () {',
  3505. ' var Result = 0;',
  3506. ' return Result;',
  3507. '};',
  3508. 'this.Func2 = function () {',
  3509. ' var Result = false;',
  3510. ' return Result;',
  3511. '};',
  3512. 'this.i = 0;'
  3513. ]),
  3514. LinesToStr([
  3515. 'if ($mod.Func2());',
  3516. 'if ($mod.i === $mod.Func1());',
  3517. 'if ($mod.i === $mod.Func1());'
  3518. ]));
  3519. end;
  3520. procedure TTestModule.TestFunctionResultInForLoop;
  3521. begin
  3522. StartProgram(false);
  3523. Add([
  3524. 'function Func1(a: array of longint): longint;',
  3525. 'begin',
  3526. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3527. ' for Result in a do if a[Result]=0 then exit;',
  3528. 'end;',
  3529. 'begin',
  3530. ' Func1([1,2,3])']);
  3531. ConvertProgram;
  3532. CheckSource('TestFunctionResultInForLoop',
  3533. LinesToStr([ // statements
  3534. 'this.Func1 = function (a) {',
  3535. ' var Result = 0;',
  3536. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3537. ' Result = $l;',
  3538. ' if (a[Result] === 0) return Result;',
  3539. ' };',
  3540. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3541. ' Result = $in[$l1];',
  3542. ' if (a[Result] === 0) return Result;',
  3543. ' };',
  3544. ' return Result;',
  3545. '};',
  3546. '']),
  3547. LinesToStr([
  3548. '$mod.Func1([1, 2, 3]);'
  3549. ]));
  3550. end;
  3551. procedure TTestModule.TestFunctionResultInTypeCast;
  3552. begin
  3553. StartProgram(false);
  3554. Add([
  3555. 'function GetInt: longint;',
  3556. 'begin',
  3557. 'end;',
  3558. 'begin',
  3559. ' if Byte(GetInt)=0 then ;',
  3560. '']);
  3561. ConvertProgram;
  3562. CheckSource('TestFunctionResultInTypeCast',
  3563. LinesToStr([ // statements
  3564. 'this.GetInt = function () {',
  3565. ' var Result = 0;',
  3566. ' return Result;',
  3567. '};',
  3568. '']),
  3569. LinesToStr([
  3570. 'if (($mod.GetInt() & 255) === 0) ;'
  3571. ]));
  3572. end;
  3573. procedure TTestModule.TestExit;
  3574. begin
  3575. StartProgram(false);
  3576. Add('procedure ProcA;');
  3577. Add('begin');
  3578. Add(' exit;');
  3579. Add('end;');
  3580. Add('function FuncB: longint;');
  3581. Add('begin');
  3582. Add(' exit;');
  3583. Add(' exit(3);');
  3584. Add('end;');
  3585. Add('function FuncC: string;');
  3586. Add('begin');
  3587. Add(' exit;');
  3588. Add(' exit(''a'');');
  3589. Add(' exit(''abc'');');
  3590. Add('end;');
  3591. Add('begin');
  3592. Add(' exit;');
  3593. Add(' exit(1);');
  3594. ConvertProgram;
  3595. CheckSource('TestExit',
  3596. LinesToStr([ // statements
  3597. 'this.ProcA = function () {',
  3598. ' return;',
  3599. '};',
  3600. 'this.FuncB = function () {',
  3601. ' var Result = 0;',
  3602. ' return Result;',
  3603. ' return 3;',
  3604. ' return Result;',
  3605. '};',
  3606. 'this.FuncC = function () {',
  3607. ' var Result = "";',
  3608. ' return Result;',
  3609. ' return "a";',
  3610. ' return "abc";',
  3611. ' return Result;',
  3612. '};'
  3613. ]),
  3614. LinesToStr([
  3615. 'return;',
  3616. 'return 1;',
  3617. '']));
  3618. end;
  3619. procedure TTestModule.TestExit_ResultInFinally;
  3620. begin
  3621. StartProgram(false);
  3622. Add([
  3623. 'function Run: word;',
  3624. 'begin',
  3625. ' try',
  3626. ' exit(3);', // no Result in finally -> use return 3
  3627. ' finally',
  3628. ' end;',
  3629. 'end;',
  3630. 'function Fly: word;',
  3631. 'begin',
  3632. ' try',
  3633. ' exit(3);',
  3634. ' finally',
  3635. ' if Result>0 then ;',
  3636. ' end;',
  3637. 'end;',
  3638. 'function Jump: word;',
  3639. 'begin',
  3640. ' try',
  3641. ' try',
  3642. ' exit(4);',
  3643. ' finally',
  3644. ' end;',
  3645. ' finally',
  3646. ' if Result>0 then ;',
  3647. ' end;',
  3648. 'end;',
  3649. 'begin',
  3650. '']);
  3651. ConvertProgram;
  3652. CheckSource('TestExit_ResultInFinally',
  3653. LinesToStr([ // statements
  3654. 'this.Run = function () {',
  3655. ' var Result = 0;',
  3656. ' try {',
  3657. ' return 3;',
  3658. ' } finally {',
  3659. ' };',
  3660. ' return Result;',
  3661. '};',
  3662. 'this.Fly = function () {',
  3663. ' var Result = 0;',
  3664. ' try {',
  3665. ' Result = 3;',
  3666. ' return Result;',
  3667. ' } finally {',
  3668. ' if (Result > 0) ;',
  3669. ' };',
  3670. ' return Result;',
  3671. '};',
  3672. 'this.Jump = function () {',
  3673. ' var Result = 0;',
  3674. ' try {',
  3675. ' try {',
  3676. ' Result = 4;',
  3677. ' return Result;',
  3678. ' } finally {',
  3679. ' };',
  3680. ' } finally {',
  3681. ' if (Result > 0) ;',
  3682. ' };',
  3683. ' return Result;',
  3684. '};',
  3685. '']),
  3686. LinesToStr([
  3687. '']));
  3688. end;
  3689. procedure TTestModule.TestBreak;
  3690. begin
  3691. StartProgram(false);
  3692. Add([
  3693. 'var',
  3694. ' i: longint;',
  3695. 'begin',
  3696. ' repeat',
  3697. ' break;',
  3698. ' until true;',
  3699. ' while true do',
  3700. ' break;',
  3701. ' for i:=1 to 2 do',
  3702. ' break;']);
  3703. ConvertProgram;
  3704. CheckSource('TestBreak',
  3705. LinesToStr([ // statements
  3706. 'this.i = 0;'
  3707. ]),
  3708. LinesToStr([
  3709. 'do {',
  3710. ' break;',
  3711. '} while (!true);',
  3712. 'while (true) break;',
  3713. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3714. '']));
  3715. end;
  3716. procedure TTestModule.TestBreakAsVar;
  3717. begin
  3718. StartProgram(false);
  3719. Add([
  3720. 'procedure DoIt(break: boolean);',
  3721. 'begin',
  3722. ' if break then ;',
  3723. 'end;',
  3724. 'var',
  3725. ' break: boolean;',
  3726. 'begin',
  3727. ' if break then ;']);
  3728. ConvertProgram;
  3729. CheckSource('TestBreakAsVar',
  3730. LinesToStr([ // statements
  3731. 'this.DoIt = function (Break) {',
  3732. ' if (Break) ;',
  3733. '};',
  3734. 'this.Break = false;',
  3735. '']),
  3736. LinesToStr([
  3737. 'if($mod.Break) ;',
  3738. '']));
  3739. end;
  3740. procedure TTestModule.TestContinue;
  3741. begin
  3742. StartProgram(false);
  3743. Add('var i: longint;');
  3744. Add('begin');
  3745. Add(' repeat');
  3746. Add(' continue;');
  3747. Add(' until true;');
  3748. Add(' while true do');
  3749. Add(' continue;');
  3750. Add(' for i:=1 to 2 do');
  3751. Add(' continue;');
  3752. ConvertProgram;
  3753. CheckSource('TestContinue',
  3754. LinesToStr([ // statements
  3755. 'this.i = 0;'
  3756. ]),
  3757. LinesToStr([
  3758. 'do {',
  3759. ' continue;',
  3760. '} while (!true);',
  3761. 'while (true) continue;',
  3762. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3763. '']));
  3764. end;
  3765. procedure TTestModule.TestProc_External;
  3766. begin
  3767. StartProgram(false);
  3768. Add('procedure Foo; external name ''console.log'';');
  3769. Add('function Bar: longint; external name ''get.item'';');
  3770. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3771. Add('var');
  3772. Add(' i: longint;');
  3773. Add('begin');
  3774. Add(' Foo;');
  3775. Add(' i:=Bar;');
  3776. Add(' i:=Bla(''abc'');');
  3777. ConvertProgram;
  3778. CheckSource('TestProc_External',
  3779. LinesToStr([ // statements
  3780. 'this.i = 0;'
  3781. ]),
  3782. LinesToStr([
  3783. 'console.log();',
  3784. '$mod.i = get.item();',
  3785. '$mod.i = apply.something("abc");'
  3786. ]));
  3787. end;
  3788. procedure TTestModule.TestProc_ExternalOtherUnit;
  3789. begin
  3790. AddModuleWithIntfImplSrc('unit2.pas',
  3791. LinesToStr([
  3792. 'procedure Now; external name ''Date.now'';',
  3793. 'procedure DoIt;'
  3794. ]),
  3795. 'procedure doit; begin end;');
  3796. StartUnit(true);
  3797. Add('interface');
  3798. Add('uses unit2;');
  3799. Add('implementation');
  3800. Add('begin');
  3801. Add(' now;');
  3802. Add(' now();');
  3803. Add(' uNit2.now;');
  3804. Add(' uNit2.now();');
  3805. Add(' doit;');
  3806. Add(' uNit2.doit;');
  3807. ConvertUnit;
  3808. CheckSource('TestProc_ExternalOtherUnit',
  3809. LinesToStr([
  3810. '']),
  3811. LinesToStr([
  3812. 'Date.now();',
  3813. 'Date.now();',
  3814. 'Date.now();',
  3815. 'Date.now();',
  3816. 'pas.unit2.DoIt();',
  3817. 'pas.unit2.DoIt();',
  3818. '']));
  3819. end;
  3820. procedure TTestModule.TestProc_Asm;
  3821. begin
  3822. StartProgram(false);
  3823. Add([
  3824. '{$mode delphi}',
  3825. 'function DoIt: longint;',
  3826. 'begin;',
  3827. ' asm',
  3828. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3829. ' end;',
  3830. ' asm console.log(); end;',
  3831. ' asm',
  3832. ' s = "'' ";',
  3833. ' s = ''" '';',
  3834. ' s = s + "world" + "''";',
  3835. ' // end',
  3836. ' s = ''end'';',
  3837. ' s = "end";',
  3838. ' s = "foo\"bar";',
  3839. ' s = ''a\''b'';',
  3840. ' s = `${expr}\`-"-''-`;',
  3841. ' s = `multi',
  3842. 'line`;',
  3843. ' end;',
  3844. 'end;',
  3845. 'procedure Fly;',
  3846. 'asm',
  3847. ' return;',
  3848. 'end;',
  3849. 'begin']);
  3850. ConvertProgram;
  3851. CheckSource('TestProc_Asm',
  3852. LinesToStr([ // statements
  3853. 'this.DoIt = function () {',
  3854. ' var Result = 0;',
  3855. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3856. ' console.log();',
  3857. ' s = "'' ";',
  3858. ' s = ''" '';',
  3859. ' s = s + "world" + "''";',
  3860. ' // end',
  3861. ' s = ''end'';',
  3862. ' s = "end";',
  3863. ' s = "foo\"bar";',
  3864. ' s = ''a\''b'';',
  3865. ' s = `${expr}\`-"-''-`;',
  3866. ' s = `multi',
  3867. 'line`;',
  3868. ' return Result;',
  3869. '};',
  3870. 'this.Fly = function () {',
  3871. ' return;',
  3872. '};',
  3873. '']),
  3874. LinesToStr([
  3875. ''
  3876. ]));
  3877. end;
  3878. procedure TTestModule.TestProc_Assembler;
  3879. begin
  3880. StartProgram(false);
  3881. Add('function DoIt: longint; assembler;');
  3882. Add('asm');
  3883. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3884. Add('end;');
  3885. Add('begin');
  3886. ConvertProgram;
  3887. CheckSource('TestProc_Assembler',
  3888. LinesToStr([ // statements
  3889. 'this.DoIt = function () {',
  3890. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3891. '};'
  3892. ]),
  3893. LinesToStr([
  3894. ''
  3895. ]));
  3896. end;
  3897. procedure TTestModule.TestProc_VarParam;
  3898. begin
  3899. StartProgram(false);
  3900. Add('type integer = longint;');
  3901. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3902. Add('var vJ: integer;');
  3903. Add('begin');
  3904. Add(' vg:=vg+1;');
  3905. Add(' vj:=vh+2;');
  3906. Add(' vi:=vi+3;');
  3907. Add(' doit(vg,vg,vg);');
  3908. Add(' doit(vh,vh,vj);');
  3909. Add(' doit(vi,vi,vi);');
  3910. Add(' doit(vj,vj,vj);');
  3911. Add('end;');
  3912. Add('var i: integer;');
  3913. Add('begin');
  3914. Add(' doit(i,i,i);');
  3915. ConvertProgram;
  3916. CheckSource('TestProc_VarParam',
  3917. LinesToStr([ // statements
  3918. 'this.DoIt = function (vG,vH,vI) {',
  3919. ' var vJ = 0;',
  3920. ' vG = vG + 1;',
  3921. ' vJ = vH + 2;',
  3922. ' vI.set(vI.get()+3);',
  3923. ' $mod.DoIt(vG, vG, {',
  3924. ' get: function () {',
  3925. ' return vG;',
  3926. ' },',
  3927. ' set: function (v) {',
  3928. ' vG = v;',
  3929. ' }',
  3930. ' });',
  3931. ' $mod.DoIt(vH, vH, {',
  3932. ' get: function () {',
  3933. ' return vJ;',
  3934. ' },',
  3935. ' set: function (v) {',
  3936. ' vJ = v;',
  3937. ' }',
  3938. ' });',
  3939. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3940. ' $mod.DoIt(vJ, vJ, {',
  3941. ' get: function () {',
  3942. ' return vJ;',
  3943. ' },',
  3944. ' set: function (v) {',
  3945. ' vJ = v;',
  3946. ' }',
  3947. ' });',
  3948. '};',
  3949. 'this.i = 0;'
  3950. ]),
  3951. LinesToStr([
  3952. '$mod.DoIt($mod.i,$mod.i,{',
  3953. ' p: $mod,',
  3954. ' get: function () {',
  3955. ' return this.p.i;',
  3956. ' },',
  3957. ' set: function (v) {',
  3958. ' this.p.i = v;',
  3959. ' }',
  3960. '});'
  3961. ]));
  3962. end;
  3963. procedure TTestModule.TestProc_VarParamString;
  3964. begin
  3965. StartProgram(false);
  3966. Add(['type TCaption = string;',
  3967. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3968. 'var c: char;',
  3969. 'begin',
  3970. ' va[1]:=c;',
  3971. ' vb[2]:=c;',
  3972. ' vc[3]:=c;',
  3973. 'end;',
  3974. 'begin']);
  3975. ConvertProgram;
  3976. CheckSource('TestProc_VarParamString',
  3977. LinesToStr([ // statements
  3978. 'this.DoIt = function (vA,vB,vC) {',
  3979. ' var c = "";',
  3980. ' vA = rtl.setCharAt(vA, 0, c);',
  3981. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3982. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3983. '};',
  3984. '']),
  3985. LinesToStr([
  3986. ]));
  3987. end;
  3988. procedure TTestModule.TestProc_VarParamV;
  3989. begin
  3990. StartProgram(false);
  3991. Add([
  3992. 'procedure Inc2(var i: longint);',
  3993. 'begin',
  3994. ' i:=i+2;',
  3995. 'end;',
  3996. 'procedure DoIt(v: longint);',
  3997. 'var p: array of longint;',
  3998. 'begin',
  3999. ' Inc2(v);',
  4000. ' Inc2(p[v]);',
  4001. 'end;',
  4002. 'begin']);
  4003. ConvertProgram;
  4004. CheckSource('TestProc_VarParamV',
  4005. LinesToStr([ // statements
  4006. 'this.Inc2 = function (i) {',
  4007. ' i.set(i.get()+2);',
  4008. '};',
  4009. 'this.DoIt = function (v) {',
  4010. ' var p = [];',
  4011. ' $mod.Inc2({get: function () {',
  4012. ' return v;',
  4013. ' }, set: function (w) {',
  4014. ' v = w;',
  4015. ' }});',
  4016. ' $mod.Inc2({',
  4017. ' a: v,',
  4018. ' p: p,',
  4019. ' get: function () {',
  4020. ' return this.p[this.a];',
  4021. ' },',
  4022. ' set: function (v) {',
  4023. ' this.p[this.a] = v;',
  4024. ' }',
  4025. ' });',
  4026. '};',
  4027. '']),
  4028. LinesToStr([
  4029. '']));
  4030. end;
  4031. procedure TTestModule.TestProc_Overload;
  4032. begin
  4033. StartProgram(false);
  4034. Add('procedure DoIt(vI: longint); begin end;');
  4035. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4036. Add('procedure DoIt(vD: double); begin end;');
  4037. Add('begin');
  4038. Add(' DoIt(1);');
  4039. Add(' DoIt(2,3);');
  4040. Add(' DoIt(4.5);');
  4041. ConvertProgram;
  4042. CheckSource('TestProcedureOverload',
  4043. LinesToStr([ // statements
  4044. 'this.DoIt = function (vI) {',
  4045. '};',
  4046. 'this.DoIt$1 = function (vI, vJ) {',
  4047. '};',
  4048. 'this.DoIt$2 = function (vD) {',
  4049. '};',
  4050. '']),
  4051. LinesToStr([
  4052. '$mod.DoIt(1);',
  4053. '$mod.DoIt$1(2, 3);',
  4054. '$mod.DoIt$2(4.5);',
  4055. '']));
  4056. end;
  4057. procedure TTestModule.TestProc_OverloadForward;
  4058. begin
  4059. StartProgram(false);
  4060. Add('procedure DoIt(vI: longint); forward;');
  4061. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4062. Add('procedure doit(vi: longint); begin end;');
  4063. Add('begin');
  4064. Add(' doit(1);');
  4065. Add(' doit(2,3);');
  4066. ConvertProgram;
  4067. CheckSource('TestProcedureOverloadForward',
  4068. LinesToStr([ // statements
  4069. 'this.DoIt$1 = function (vI, vJ) {',
  4070. '};',
  4071. 'this.DoIt = function (vI) {',
  4072. '};',
  4073. '']),
  4074. LinesToStr([
  4075. '$mod.DoIt(1);',
  4076. '$mod.DoIt$1(2, 3);',
  4077. '']));
  4078. end;
  4079. procedure TTestModule.TestProc_OverloadIntfImpl;
  4080. begin
  4081. StartUnit(false);
  4082. Add('interface');
  4083. Add('procedure DoIt(vI: longint);');
  4084. Add('procedure DoIt(vI, vJ: longint);');
  4085. Add('implementation');
  4086. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4087. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4088. Add('procedure DoIt(vi: longint); begin end;');
  4089. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4090. Add('procedure DoIt(vi, vj: longint); begin end;');
  4091. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4092. Add('begin');
  4093. Add(' doit(1);');
  4094. Add(' doit(2,3);');
  4095. Add(' doit(4,5,6);');
  4096. Add(' doit(7,8,9,10);');
  4097. Add(' doit(11,12,13,14,15);');
  4098. ConvertUnit;
  4099. CheckSource('TestProcedureOverloadUnit',
  4100. LinesToStr([ // statements
  4101. 'var $impl = $mod.$impl;',
  4102. 'this.DoIt = function (vI) {',
  4103. '};',
  4104. 'this.DoIt$1 = function (vI, vJ) {',
  4105. '};',
  4106. '']),
  4107. LinesToStr([ // this.$init
  4108. '$mod.DoIt(1);',
  4109. '$mod.DoIt$1(2, 3);',
  4110. '$impl.DoIt$3(4,5,6);',
  4111. '$impl.DoIt$4(7,8,9,10);',
  4112. '$impl.DoIt$2(11,12,13,14,15);',
  4113. '']),
  4114. LinesToStr([ // implementation
  4115. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4116. '};',
  4117. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4118. '};',
  4119. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4120. '};',
  4121. '']));
  4122. end;
  4123. procedure TTestModule.TestProc_OverloadNested;
  4124. begin
  4125. StartProgram(false);
  4126. Add([
  4127. 'procedure doit(vA: longint);',
  4128. ' procedure DoIt(vA, vB: longint); overload;',
  4129. ' begin',
  4130. ' doit(1);',
  4131. ' doit(1,2);',
  4132. ' end;',
  4133. ' procedure doit(vA, vB, vC: longint);',
  4134. ' begin',
  4135. ' doit(1);',
  4136. ' doit(1,2);',
  4137. ' doit(1,2,3);',
  4138. ' end;',
  4139. 'begin',
  4140. ' doit(1);',
  4141. ' doit(1,2);',
  4142. ' doit(1,2,3);',
  4143. 'end;',
  4144. 'begin // main',
  4145. ' doit(1);']);
  4146. ConvertProgram;
  4147. CheckSource('TestProcedureOverloadNested',
  4148. LinesToStr([ // statements
  4149. 'this.doit = function (vA) {',
  4150. ' function DoIt$1(vA, vB) {',
  4151. ' $mod.doit(1);',
  4152. ' DoIt$1(1, 2);',
  4153. ' };',
  4154. ' function doit$2(vA, vB, vC) {',
  4155. ' $mod.doit(1);',
  4156. ' DoIt$1(1, 2);',
  4157. ' doit$2(1, 2, 3);',
  4158. ' };',
  4159. ' $mod.doit(1);',
  4160. ' DoIt$1(1, 2);',
  4161. ' doit$2(1, 2, 3);',
  4162. '};',
  4163. '']),
  4164. LinesToStr([
  4165. '$mod.doit(1);',
  4166. '']));
  4167. end;
  4168. procedure TTestModule.TestProc_OverloadNestedForward;
  4169. begin
  4170. StartProgram(false);
  4171. Add([
  4172. 'procedure DoIt(vA: longint); overload; forward;',
  4173. 'procedure DoIt(vB, vC: longint); overload;',
  4174. 'begin // 2 param overload',
  4175. ' doit(1);',
  4176. ' doit(1,2);',
  4177. 'end;',
  4178. 'procedure doit(vA: longint);',
  4179. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4180. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4181. ' begin // 4 param overload',
  4182. ' doit(1);',
  4183. ' doit(1,2);',
  4184. ' doit(1,2,3);',
  4185. ' doit(1,2,3,4);',
  4186. ' end;',
  4187. ' procedure doit(vA, vB, vC: longint);',
  4188. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4189. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4190. ' begin // 6 param overload',
  4191. ' doit(1);',
  4192. ' doit(1,2);',
  4193. ' doit(1,2,3);',
  4194. ' doit(1,2,3,4);',
  4195. ' doit(1,2,3,4,5);',
  4196. ' doit(1,2,3,4,5,6);',
  4197. ' end;',
  4198. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4199. ' begin // 5 param overload',
  4200. ' doit(1);',
  4201. ' doit(1,2);',
  4202. ' doit(1,2,3);',
  4203. ' doit(1,2,3,4);',
  4204. ' doit(1,2,3,4,5);',
  4205. ' doit(1,2,3,4,5,6);',
  4206. ' end;',
  4207. ' begin // 3 param overload',
  4208. ' doit(1);',
  4209. ' doit(1,2);',
  4210. ' doit(1,2,3);',
  4211. ' doit(1,2,3,4);',
  4212. ' doit(1,2,3,4,5);',
  4213. ' doit(1,2,3,4,5,6);',
  4214. ' end;',
  4215. 'begin // 1 param overload',
  4216. ' doit(1);',
  4217. ' doit(1,2);',
  4218. ' doit(1,2,3);',
  4219. ' doit(1,2,3,4);',
  4220. 'end;',
  4221. 'begin // main',
  4222. ' doit(1);',
  4223. ' doit(1,2);']);
  4224. ConvertProgram;
  4225. CheckSource('TestProc_OverloadNestedForward',
  4226. LinesToStr([ // statements
  4227. 'this.DoIt$1 = function (vB, vC) {',
  4228. ' $mod.DoIt(1);',
  4229. ' $mod.DoIt$1(1, 2);',
  4230. '};',
  4231. 'this.DoIt = function (vA) {',
  4232. ' function DoIt$3(vA, vB, vC, vD) {',
  4233. ' $mod.DoIt(1);',
  4234. ' $mod.DoIt$1(1, 2);',
  4235. ' DoIt$2(1, 2, 3);',
  4236. ' DoIt$3(1, 2, 3, 4);',
  4237. ' };',
  4238. ' function DoIt$2(vA, vB, vC) {',
  4239. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4240. ' $mod.DoIt(1);',
  4241. ' $mod.DoIt$1(1, 2);',
  4242. ' DoIt$2(1, 2, 3);',
  4243. ' DoIt$3(1, 2, 3, 4);',
  4244. ' DoIt$4(1, 2, 3, 4, 5);',
  4245. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4246. ' };',
  4247. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4248. ' $mod.DoIt(1);',
  4249. ' $mod.DoIt$1(1, 2);',
  4250. ' DoIt$2(1, 2, 3);',
  4251. ' DoIt$3(1, 2, 3, 4);',
  4252. ' DoIt$4(1, 2, 3, 4, 5);',
  4253. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4254. ' };',
  4255. ' $mod.DoIt(1);',
  4256. ' $mod.DoIt$1(1, 2);',
  4257. ' DoIt$2(1, 2, 3);',
  4258. ' DoIt$3(1, 2, 3, 4);',
  4259. ' DoIt$4(1, 2, 3, 4, 5);',
  4260. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4261. ' };',
  4262. ' $mod.DoIt(1);',
  4263. ' $mod.DoIt$1(1, 2);',
  4264. ' DoIt$2(1, 2, 3);',
  4265. ' DoIt$3(1, 2, 3, 4);',
  4266. '};',
  4267. '']),
  4268. LinesToStr([
  4269. '$mod.DoIt(1);',
  4270. '$mod.DoIt$1(1, 2);',
  4271. '']));
  4272. end;
  4273. procedure TTestModule.TestProc_OverloadUnitCycle;
  4274. begin
  4275. AddModuleWithIntfImplSrc('Unit2.pas',
  4276. LinesToStr([
  4277. 'type',
  4278. ' TObject = class',
  4279. ' procedure DoIt(b: boolean); virtual; abstract;',
  4280. ' procedure DoIt(i: longint); virtual; abstract;',
  4281. ' end;',
  4282. '']),
  4283. 'uses test1;');
  4284. StartUnit(true);
  4285. Add([
  4286. 'interface',
  4287. 'uses unit2;',
  4288. 'type',
  4289. ' TEagle = class(TObject)',
  4290. ' procedure DoIt(b: boolean); override;',
  4291. ' procedure DoIt(i: longint); override;',
  4292. ' end;',
  4293. 'implementation',
  4294. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4295. 'procedure TEagle.DoIt(i: longint); begin end;',
  4296. '']);
  4297. ConvertUnit;
  4298. CheckSource('TestProc_OverloadUnitCycle',
  4299. LinesToStr([ // statements
  4300. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4301. ' this.DoIt = function (b) {',
  4302. ' };',
  4303. ' this.DoIt$1 = function (i) {',
  4304. ' };',
  4305. '});',
  4306. '']),
  4307. '',
  4308. LinesToStr([
  4309. '']));
  4310. end;
  4311. procedure TTestModule.TestProc_Varargs;
  4312. begin
  4313. StartProgram(false);
  4314. Add([
  4315. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4316. 'procedure ProcB; varargs; external name ''ProcB'';',
  4317. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4318. 'function GetIt: longint; begin end;',
  4319. 'begin',
  4320. ' ProcA(1);',
  4321. ' ProcA(1,2);',
  4322. ' ProcA(1,2.0);',
  4323. ' ProcA(1,2,3);',
  4324. ' ProcA(1,''2'');',
  4325. ' ProcA(2,'''');',
  4326. ' ProcA(3,false);',
  4327. ' ProcB;',
  4328. ' ProcB();',
  4329. ' ProcB(4);',
  4330. ' ProcB(''foo'');',
  4331. ' ProcC;',
  4332. ' ProcC();',
  4333. ' ProcC(4);',
  4334. ' ProcC(5,''foo'');',
  4335. ' ProcB(GetIt);',
  4336. ' ProcB(GetIt());',
  4337. ' ProcB(GetIt,GetIt());']);
  4338. ConvertProgram;
  4339. CheckSource('TestProc_Varargs',
  4340. LinesToStr([ // statements
  4341. 'this.GetIt = function () {',
  4342. ' var Result = 0;',
  4343. ' return Result;',
  4344. '};',
  4345. '']),
  4346. LinesToStr([
  4347. 'ProcA(1);',
  4348. 'ProcA(1, 2);',
  4349. 'ProcA(1, 2.0);',
  4350. 'ProcA(1, 2, 3);',
  4351. 'ProcA(1, "2");',
  4352. 'ProcA(2, "");',
  4353. 'ProcA(3, false);',
  4354. 'ProcB();',
  4355. 'ProcB();',
  4356. 'ProcB(4);',
  4357. 'ProcB("foo");',
  4358. 'ProcC(17);',
  4359. 'ProcC(17);',
  4360. 'ProcC(4);',
  4361. 'ProcC(5, "foo");',
  4362. 'ProcB($mod.GetIt());',
  4363. 'ProcB($mod.GetIt());',
  4364. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4365. '']));
  4366. end;
  4367. procedure TTestModule.TestProc_ConstOrder;
  4368. begin
  4369. StartProgram(false);
  4370. Add([
  4371. 'const A = 3;',
  4372. 'const B = A+1;',
  4373. 'procedure DoIt;',
  4374. 'const C = A+1;',
  4375. 'const D = B+1;',
  4376. 'const E = D+C+B+A;',
  4377. 'begin',
  4378. 'end;',
  4379. 'begin'
  4380. ]);
  4381. ConvertProgram;
  4382. CheckSource('TestProc_ConstOrder',
  4383. LinesToStr([ // statements
  4384. 'this.A = 3;',
  4385. 'this.B = 3 + 1;',
  4386. 'var C = 3 + 1;',
  4387. 'var D = 4 + 1;',
  4388. 'var E = 5 + 4 + 4 + 3;',
  4389. 'this.DoIt = function () {',
  4390. '};',
  4391. '']),
  4392. LinesToStr([
  4393. ''
  4394. ]));
  4395. end;
  4396. procedure TTestModule.TestProc_DuplicateConst;
  4397. begin
  4398. StartProgram(false);
  4399. Add([
  4400. 'const A = 1;',
  4401. 'procedure DoIt;',
  4402. 'const A = 2;',
  4403. ' procedure SubIt;',
  4404. ' const A = 21;',
  4405. ' begin',
  4406. ' end;',
  4407. 'begin',
  4408. 'end;',
  4409. 'procedure DoSome;',
  4410. 'const A = 3;',
  4411. 'begin',
  4412. 'end;',
  4413. 'begin'
  4414. ]);
  4415. ConvertProgram;
  4416. CheckSource('TestProc_DuplicateConst',
  4417. LinesToStr([ // statements
  4418. 'this.A = 1;',
  4419. 'var A$1 = 2;',
  4420. 'var A$2 = 21;',
  4421. 'this.DoIt = function () {',
  4422. ' function SubIt() {',
  4423. ' };',
  4424. '};',
  4425. 'var A$3 = 3;',
  4426. 'this.DoSome = function () {',
  4427. '};',
  4428. '']),
  4429. LinesToStr([
  4430. ''
  4431. ]));
  4432. end;
  4433. procedure TTestModule.TestProc_LocalVarAbsolute;
  4434. begin
  4435. StartProgram(false);
  4436. Add([
  4437. 'type',
  4438. ' TObject = class',
  4439. ' Index: longint;',
  4440. ' procedure DoAbs(Item: pointer);',
  4441. ' end;',
  4442. 'procedure TObject.DoAbs(Item: pointer);',
  4443. 'var',
  4444. ' o: TObject absolute Item;',
  4445. 'begin',
  4446. ' if o.Index<o.Index then o.Index:=o.Index;',
  4447. 'end;',
  4448. 'procedure DoIt(i: longint; p: pointer);',
  4449. 'var',
  4450. ' d: double absolute i;',
  4451. ' s: string absolute d;',
  4452. ' oi: TObject absolute i;',
  4453. ' op: TObject absolute p;',
  4454. 'begin',
  4455. ' if d=d then d:=d;',
  4456. ' if s=s then s:=s;',
  4457. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4458. ' if op.Index=op.Index then op.Index:=op.Index;',
  4459. 'end;',
  4460. 'begin']);
  4461. ConvertProgram;
  4462. CheckSource('TestProc_LocalVarAbsolute',
  4463. LinesToStr([ // statements
  4464. 'rtl.createClass(this, "TObject", null, function () {',
  4465. ' this.$init = function () {',
  4466. ' this.Index = 0;',
  4467. ' };',
  4468. ' this.$final = function () {',
  4469. ' };',
  4470. ' this.DoAbs = function (Item) {',
  4471. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4472. ' };',
  4473. '});',
  4474. 'this.DoIt = function (i, p) {',
  4475. ' if (i === i) i = i;',
  4476. ' if (i === i) i = i;',
  4477. ' if (i.Index < i.Index) i.Index = i.Index;',
  4478. ' if (p.Index === p.Index) p.Index = p.Index;',
  4479. '};'
  4480. ]),
  4481. LinesToStr([
  4482. ]));
  4483. end;
  4484. procedure TTestModule.TestProc_LocalVarInit;
  4485. begin
  4486. StartProgram(false);
  4487. Add([
  4488. 'type TBytes = array of byte;',
  4489. 'procedure DoIt;',
  4490. 'const c = 4;',
  4491. 'var',
  4492. ' b: byte = 1;',
  4493. ' w: word = 2+c;',
  4494. ' p: pointer = nil;',
  4495. ' Buffer: TBytes = nil;',
  4496. 'begin',
  4497. 'end;',
  4498. 'begin']);
  4499. ConvertProgram;
  4500. CheckSource('TestProc_LocalVarInit',
  4501. LinesToStr([ // statements
  4502. 'var c = 4;',
  4503. 'this.DoIt = function () {',
  4504. ' var b = 1;',
  4505. ' var w = 2 + 4;',
  4506. ' var p = null;',
  4507. ' var Buffer = [];',
  4508. '};',
  4509. '']),
  4510. LinesToStr([
  4511. ]));
  4512. end;
  4513. procedure TTestModule.TestProc_ReservedWords;
  4514. begin
  4515. StartProgram(false);
  4516. Add([
  4517. 'procedure Date(ArrayBuffer: longint);',
  4518. 'const',
  4519. ' NaN: longint = 3;',
  4520. 'var',
  4521. ' &Boolean: longint;',
  4522. ' procedure Error(ArrayBuffer: longint);',
  4523. ' begin',
  4524. ' end;',
  4525. 'begin',
  4526. ' Nan:=&bOolean;',
  4527. 'end;',
  4528. 'begin',
  4529. ' Date(1);']);
  4530. ConvertProgram;
  4531. CheckSource('TestProc_ReservedWords',
  4532. LinesToStr([ // statements
  4533. 'var naN = 3;',
  4534. 'this.Date = function (arrayBuffer) {',
  4535. ' var boolean = 0;',
  4536. ' function error(arrayBuffer) {',
  4537. ' };',
  4538. ' naN = boolean;',
  4539. '};',
  4540. '']),
  4541. LinesToStr([
  4542. ' $mod.Date(1);'
  4543. ]));
  4544. end;
  4545. procedure TTestModule.TestProc_ConstRefWord;
  4546. begin
  4547. StartProgram(false);
  4548. Add([
  4549. 'procedure Run(constref w: word);',
  4550. 'var l: word;',
  4551. 'begin',
  4552. ' l:=w;',
  4553. ' Run(w);',
  4554. ' Run(l);',
  4555. 'end;',
  4556. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4557. 'begin',
  4558. ' Run(a);',
  4559. ' Run(b);',
  4560. ' Run(c);',
  4561. ' Run(d);',
  4562. ' Run(e);',
  4563. 'end;',
  4564. 'begin',
  4565. ' Run(1);']);
  4566. ConvertProgram;
  4567. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4568. CheckSource('TestProc_ConstRefWord',
  4569. LinesToStr([ // statements
  4570. 'this.Run = function (w) {',
  4571. ' var l = 0;',
  4572. ' l = w;',
  4573. ' $mod.Run(w);',
  4574. ' $mod.Run(l);',
  4575. '};',
  4576. 'this.Fly = function (a, b, c, d, e) {',
  4577. ' $mod.Run(a);',
  4578. ' $mod.Run(b.get());',
  4579. ' $mod.Run(c.get());',
  4580. ' $mod.Run(d);',
  4581. ' $mod.Run(e);',
  4582. '};',
  4583. '']),
  4584. LinesToStr([
  4585. '$mod.Run(1);'
  4586. ]));
  4587. end;
  4588. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4589. begin
  4590. StartProgram(false);
  4591. Add([
  4592. '{$mode objfpc}',
  4593. 'type',
  4594. ' TFunc = reference to function(x: word): word;',
  4595. 'var Func: TFunc;',
  4596. 'procedure DoIt(a: word);',
  4597. 'begin',
  4598. ' Func:=function(b:word): word',
  4599. ' begin',
  4600. ' Result:=a+b;',
  4601. ' exit(b);',
  4602. ' exit(Result);',
  4603. ' end;',// test semicolon
  4604. ' a:=3;',
  4605. 'end;',
  4606. 'begin',
  4607. ' Func:=function(c:word):word begin',
  4608. ' Result:=3+c;',
  4609. ' exit(c);',
  4610. ' exit(Result);',
  4611. ' end;']);
  4612. ConvertProgram;
  4613. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4614. LinesToStr([ // statements
  4615. 'this.Func = null;',
  4616. 'this.DoIt = function (a) {',
  4617. ' $mod.Func = function (b) {',
  4618. ' var Result = 0;',
  4619. ' Result = a + b;',
  4620. ' return b;',
  4621. ' return Result;',
  4622. ' return Result;',
  4623. ' };',
  4624. ' a = 3;',
  4625. '};',
  4626. '']),
  4627. LinesToStr([
  4628. '$mod.Func = function (c) {',
  4629. ' var Result = 0;',
  4630. ' Result = 3 + c;',
  4631. ' return c;',
  4632. ' return Result;',
  4633. ' return Result;',
  4634. '};',
  4635. '']));
  4636. end;
  4637. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4638. begin
  4639. StartProgram(false);
  4640. Add([
  4641. '{$mode delphi}',
  4642. 'type',
  4643. ' TProc = reference to procedure(x: word);',
  4644. 'procedure DoIt(a: word);',
  4645. 'var Proc: TProc;',
  4646. 'begin',
  4647. ' Proc:=procedure(b:word) begin end;',
  4648. 'end;',
  4649. 'var Proc: TProc;',
  4650. 'begin',
  4651. ' Proc:=procedure(c:word) begin end;',
  4652. '']);
  4653. ConvertProgram;
  4654. CheckSource('TestAnonymousProc_Assign_Delphi',
  4655. LinesToStr([ // statements
  4656. 'this.DoIt = function (a) {',
  4657. ' var Proc = null;',
  4658. ' Proc = function (b) {',
  4659. ' };',
  4660. '};',
  4661. 'this.Proc = null;',
  4662. '']),
  4663. LinesToStr([
  4664. '$mod.Proc = function (c) {',
  4665. '};',
  4666. '']));
  4667. end;
  4668. procedure TTestModule.TestAnonymousProc_Arg;
  4669. begin
  4670. StartProgram(false);
  4671. Add([
  4672. 'type',
  4673. ' TProc = reference to procedure;',
  4674. ' TFunc = reference to function(x: word): word;',
  4675. 'procedure DoMore(f,g: TProc);',
  4676. 'begin',
  4677. 'end;',
  4678. 'procedure DoOdd(v: jsvalue);',
  4679. 'begin',
  4680. 'end;',
  4681. 'procedure DoIt(f: TFunc);',
  4682. 'begin',
  4683. ' DoIt(function(b:word): word',
  4684. ' begin',
  4685. ' Result:=1+b;',
  4686. ' end);',
  4687. ' DoMore(procedure begin end, procedure begin end);',
  4688. ' DoOdd(procedure begin end);',
  4689. 'end;',
  4690. 'begin',
  4691. ' DoMore(procedure begin end,',
  4692. ' procedure assembler asm',
  4693. ' console.log("c");',
  4694. ' end);',
  4695. '']);
  4696. ConvertProgram;
  4697. CheckSource('TestAnonymousProc_Arg',
  4698. LinesToStr([ // statements
  4699. 'this.DoMore = function (f, g) {',
  4700. '};',
  4701. 'this.DoOdd = function (v) {',
  4702. '};',
  4703. 'this.DoIt = function (f) {',
  4704. ' $mod.DoIt(function (b) {',
  4705. ' var Result = 0;',
  4706. ' Result = 1 + b;',
  4707. ' return Result;',
  4708. ' });',
  4709. ' $mod.DoMore(function () {',
  4710. ' }, function () {',
  4711. ' });',
  4712. ' $mod.DoOdd(function () {',
  4713. ' });',
  4714. '};',
  4715. '']),
  4716. LinesToStr([
  4717. '$mod.DoMore(function () {',
  4718. '}, function () {',
  4719. ' console.log("c");',
  4720. '});',
  4721. '']));
  4722. end;
  4723. procedure TTestModule.TestAnonymousProc_Typecast;
  4724. begin
  4725. StartProgram(false);
  4726. Add([
  4727. 'type',
  4728. ' TProc = reference to procedure(w: word);',
  4729. ' TArr = array of word;',
  4730. ' TFuncArr = reference to function: TArr;',
  4731. 'procedure DoIt(p: TProc);',
  4732. 'var',
  4733. ' w: word;',
  4734. ' a: TArr;',
  4735. 'begin',
  4736. ' p:=TProc(procedure(b: smallint) begin end);',
  4737. ' a:=TFuncArr(function: TArr begin end)();',
  4738. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4739. 'end;',
  4740. 'begin']);
  4741. ConvertProgram;
  4742. CheckSource('TestAnonymousProc_Typecast',
  4743. LinesToStr([ // statements
  4744. 'this.DoIt = function (p) {',
  4745. ' var w = 0;',
  4746. ' var a = [];',
  4747. ' p = function (b) {',
  4748. ' };',
  4749. ' a = function () {',
  4750. ' var Result = [];',
  4751. ' return Result;',
  4752. ' }();',
  4753. ' w = function () {',
  4754. ' var Result = [];',
  4755. ' return Result;',
  4756. ' }()[3];',
  4757. '};',
  4758. '']),
  4759. LinesToStr([
  4760. '']));
  4761. end;
  4762. procedure TTestModule.TestAnonymousProc_With;
  4763. begin
  4764. StartProgram(false);
  4765. Add([
  4766. 'type',
  4767. ' TProc = reference to procedure(w: word);',
  4768. ' TObject = class',
  4769. ' b: boolean;',
  4770. ' end;',
  4771. 'var',
  4772. ' p: TProc;',
  4773. ' bird: TObject;',
  4774. 'begin',
  4775. ' with bird do',
  4776. ' p:=procedure(w: word)',
  4777. ' begin',
  4778. ' b:=w>2;',
  4779. ' end;',
  4780. '']);
  4781. ConvertProgram;
  4782. CheckSource('TestAnonymousProc_With',
  4783. LinesToStr([ // statements
  4784. 'rtl.createClass(this, "TObject", null, function () {',
  4785. ' this.$init = function () {',
  4786. ' this.b = false;',
  4787. ' };',
  4788. ' this.$final = function () {',
  4789. ' };',
  4790. '});',
  4791. 'this.p = null;',
  4792. 'this.bird = null;',
  4793. '']),
  4794. LinesToStr([
  4795. 'var $with = $mod.bird;',
  4796. '$mod.p = function (w) {',
  4797. ' $with.b = w > 2;',
  4798. '};',
  4799. '']));
  4800. end;
  4801. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4802. begin
  4803. StartProgram(false);
  4804. Add([
  4805. 'type',
  4806. ' TProc = reference to procedure;',
  4807. ' TObject = class',
  4808. ' b: boolean;',
  4809. ' end;',
  4810. 'procedure DoIt;',
  4811. 'var',
  4812. ' p: TProc;',
  4813. 'begin',
  4814. ' try',
  4815. ' except',
  4816. ' on E: TObject do',
  4817. ' p:=procedure',
  4818. ' begin',
  4819. ' E.b:=true;',
  4820. ' end;',
  4821. ' end;',
  4822. 'end;',
  4823. 'begin']);
  4824. ConvertProgram;
  4825. CheckSource('TestAnonymousProc_ExceptOn',
  4826. LinesToStr([ // statements
  4827. 'rtl.createClass(this, "TObject", null, function () {',
  4828. ' this.$init = function () {',
  4829. ' this.b = false;',
  4830. ' };',
  4831. ' this.$final = function () {',
  4832. ' };',
  4833. '});',
  4834. 'this.DoIt = function () {',
  4835. ' var p = null;',
  4836. ' try {} catch ($e) {',
  4837. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4838. ' var E = $e;',
  4839. ' p = function () {',
  4840. ' E.b = true;',
  4841. ' };',
  4842. ' } else throw $e',
  4843. ' };',
  4844. '};',
  4845. '']),
  4846. LinesToStr([
  4847. '']));
  4848. end;
  4849. procedure TTestModule.TestAnonymousProc_Nested;
  4850. begin
  4851. StartProgram(false);
  4852. Add([
  4853. 'type',
  4854. ' TProc = reference to procedure;',
  4855. ' TObject = class',
  4856. ' i: byte;',
  4857. ' procedure DoIt;',
  4858. ' end;',
  4859. 'procedure TObject.DoIt;',
  4860. 'var',
  4861. ' p: TProc;',
  4862. ' procedure Sub;',
  4863. ' begin',
  4864. ' p:=procedure',
  4865. ' begin',
  4866. ' i:=3;',
  4867. ' Self.i:=4;',
  4868. ' p:=procedure',
  4869. ' procedure SubSub;',
  4870. ' begin',
  4871. ' i:=13;',
  4872. ' Self.i:=14;',
  4873. ' end;',
  4874. ' begin',
  4875. ' i:=13;',
  4876. ' Self.i:=14;',
  4877. ' end;',
  4878. ' end;',
  4879. ' end;',
  4880. 'begin',
  4881. 'end;',
  4882. 'begin']);
  4883. ConvertProgram;
  4884. CheckSource('TestAnonymousProc_Nested',
  4885. LinesToStr([ // statements
  4886. 'rtl.createClass(this, "TObject", null, function () {',
  4887. ' this.$init = function () {',
  4888. ' this.i = 0;',
  4889. ' };',
  4890. ' this.$final = function () {',
  4891. ' };',
  4892. ' this.DoIt = function () {',
  4893. ' var $Self = this;',
  4894. ' var p = null;',
  4895. ' function Sub() {',
  4896. ' p = function () {',
  4897. ' $Self.i = 3;',
  4898. ' $Self.i = 4;',
  4899. ' p = function () {',
  4900. ' function SubSub() {',
  4901. ' $Self.i = 13;',
  4902. ' $Self.i = 14;',
  4903. ' };',
  4904. ' $Self.i = 13;',
  4905. ' $Self.i = 14;',
  4906. ' };',
  4907. ' };',
  4908. ' };',
  4909. ' };',
  4910. '});',
  4911. '']),
  4912. LinesToStr([
  4913. '']));
  4914. end;
  4915. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4916. begin
  4917. StartProgram(false);
  4918. Add([
  4919. 'type',
  4920. ' TProc = reference to procedure;',
  4921. 'function DoIt: TProc;',
  4922. ' function Sub: TProc;',
  4923. ' begin',
  4924. ' Result:=procedure',
  4925. ' begin',
  4926. ' Sub:=procedure',
  4927. ' procedure SubSub;',
  4928. ' begin',
  4929. ' Result:=nil;',
  4930. ' Sub:=nil;',
  4931. ' DoIt:=nil;',
  4932. ' end;',
  4933. ' begin',
  4934. ' Result:=nil;',
  4935. ' Sub:=nil;',
  4936. ' DoIt:=nil;',
  4937. ' end;',
  4938. ' end;',
  4939. ' end;',
  4940. 'begin',
  4941. 'end;',
  4942. 'begin']);
  4943. ConvertProgram;
  4944. CheckSource('TestAnonymousProc_NestedAssignResult',
  4945. LinesToStr([ // statements
  4946. 'this.DoIt = function () {',
  4947. ' var Result = null;',
  4948. ' function Sub() {',
  4949. ' var Result$1 = null;',
  4950. ' Result$1 = function () {',
  4951. ' Result$1 = function () {',
  4952. ' function SubSub() {',
  4953. ' Result$1 = null;',
  4954. ' Result$1 = null;',
  4955. ' Result = null;',
  4956. ' };',
  4957. ' Result$1 = null;',
  4958. ' Result$1 = null;',
  4959. ' Result = null;',
  4960. ' };',
  4961. ' };',
  4962. ' return Result$1;',
  4963. ' };',
  4964. ' return Result;',
  4965. '};',
  4966. '']),
  4967. LinesToStr([
  4968. '']));
  4969. end;
  4970. procedure TTestModule.TestAnonymousProc_Class;
  4971. begin
  4972. StartProgram(false);
  4973. Add([
  4974. 'type',
  4975. ' TProc = reference to procedure;',
  4976. ' TEvent = procedure of object;',
  4977. ' TObject = class',
  4978. ' Size: word;',
  4979. ' function GetIt: TProc;',
  4980. ' procedure DoIt; virtual; abstract;',
  4981. ' end;',
  4982. 'function TObject.GetIt: TProc;',
  4983. 'begin',
  4984. ' Result:=procedure',
  4985. ' var p: TEvent;',
  4986. ' begin',
  4987. ' Size:=Size;',
  4988. ' Size:=Self.Size;',
  4989. ' p:=@DoIt;',
  4990. ' p:[email protected];',
  4991. ' end;',
  4992. 'end;',
  4993. 'begin']);
  4994. ConvertProgram;
  4995. CheckSource('TestAnonymousProc_Class',
  4996. LinesToStr([ // statements
  4997. 'rtl.createClass(this, "TObject", null, function () {',
  4998. ' this.$init = function () {',
  4999. ' this.Size = 0;',
  5000. ' };',
  5001. ' this.$final = function () {',
  5002. ' };',
  5003. ' this.GetIt = function () {',
  5004. ' var $Self = this;',
  5005. ' var Result = null;',
  5006. ' Result = function () {',
  5007. ' var p = null;',
  5008. ' $Self.Size = $Self.Size;',
  5009. ' $Self.Size = $Self.Size;',
  5010. ' p = rtl.createCallback($Self, "DoIt");',
  5011. ' p = rtl.createCallback($Self, "DoIt");',
  5012. ' };',
  5013. ' return Result;',
  5014. ' };',
  5015. '});',
  5016. '']),
  5017. LinesToStr([
  5018. '']));
  5019. end;
  5020. procedure TTestModule.TestAnonymousProc_ForLoop;
  5021. begin
  5022. StartProgram(false);
  5023. Add([
  5024. 'type TProc = reference to procedure;',
  5025. 'procedure Foo(p: TProc);',
  5026. 'begin',
  5027. 'end;',
  5028. 'procedure DoIt;',
  5029. 'var i: word;',
  5030. ' a: word;',
  5031. 'begin',
  5032. ' for i:=1 to 10 do begin',
  5033. ' Foo(procedure begin a:=3; end);',
  5034. ' end;',
  5035. 'end;',
  5036. 'begin',
  5037. ' DoIt;']);
  5038. ConvertProgram;
  5039. CheckSource('TestAnonymousProc_ForLoop',
  5040. LinesToStr([ // statements
  5041. 'this.Foo = function (p) {',
  5042. '};',
  5043. 'this.DoIt = function () {',
  5044. ' var i = 0;',
  5045. ' var a = 0;',
  5046. ' for (i = 1; i <= 10; i++) {',
  5047. ' $mod.Foo(function () {',
  5048. ' a = 3;',
  5049. ' });',
  5050. ' };',
  5051. '};',
  5052. '']),
  5053. LinesToStr([
  5054. '$mod.DoIt();'
  5055. ]));
  5056. end;
  5057. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5058. begin
  5059. StartProgram(false);
  5060. Add([
  5061. '{$mode delphi}',
  5062. 'type',
  5063. ' TProc = reference to procedure;',
  5064. ' TFunc = reference to function(x: word): word;',
  5065. 'procedure Run;',
  5066. 'asm',
  5067. 'end;',
  5068. 'procedure Walk(p: TProc; f: TFunc);',
  5069. 'begin',
  5070. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5071. 'end;',
  5072. 'begin',
  5073. ' Walk(procedure',
  5074. ' asm',
  5075. ' console.log("a");',
  5076. ' end,',
  5077. ' function(x: word): word asm',
  5078. ' console.log("c");',
  5079. ' end);',
  5080. '']);
  5081. ConvertProgram;
  5082. CheckSource('TestAnonymousProc_AsmDelphi',
  5083. LinesToStr([ // statements
  5084. 'this.Run = function () {',
  5085. '};',
  5086. 'this.Walk = function (p, f) {',
  5087. ' $mod.Walk(function () {',
  5088. ' }, function (b) {',
  5089. ' return 1+b;',
  5090. ' });',
  5091. '};',
  5092. '']),
  5093. LinesToStr([
  5094. '$mod.Walk(function () {',
  5095. ' console.log("a");',
  5096. '}, function (x) {',
  5097. ' console.log("c");',
  5098. '});',
  5099. '']));
  5100. end;
  5101. procedure TTestModule.TestEnum_Name;
  5102. begin
  5103. StartProgram(false);
  5104. Add('type TMyEnum = (Red, Green, Blue);');
  5105. Add('var e: TMyEnum;');
  5106. Add('var f: TMyEnum = Blue;');
  5107. Add('begin');
  5108. Add(' e:=green;');
  5109. Add(' e:=default(TMyEnum);');
  5110. ConvertProgram;
  5111. CheckSource('TestEnum_Name',
  5112. LinesToStr([ // statements
  5113. 'this.TMyEnum = {',
  5114. ' "0":"Red",',
  5115. ' Red:0,',
  5116. ' "1":"Green",',
  5117. ' Green:1,',
  5118. ' "2":"Blue",',
  5119. ' Blue:2',
  5120. ' };',
  5121. 'this.e = 0;',
  5122. 'this.f = this.TMyEnum.Blue;'
  5123. ]),
  5124. LinesToStr([
  5125. '$mod.e=$mod.TMyEnum.Green;',
  5126. '$mod.e=$mod.TMyEnum.Red;'
  5127. ]));
  5128. end;
  5129. procedure TTestModule.TestEnum_Number;
  5130. begin
  5131. Converter.Options:=Converter.Options+[coEnumNumbers];
  5132. StartProgram(false);
  5133. Add('type TMyEnum = (Red, Green);');
  5134. Add('var');
  5135. Add(' e: TMyEnum;');
  5136. Add(' f: TMyEnum = Green;');
  5137. Add(' i: longint;');
  5138. Add('begin');
  5139. Add(' e:=green;');
  5140. Add(' i:=longint(e);');
  5141. ConvertProgram;
  5142. CheckSource('TestEnumNumber',
  5143. LinesToStr([ // statements
  5144. 'this.TMyEnum = {',
  5145. ' "0":"Red",',
  5146. ' Red:0,',
  5147. ' "1":"Green",',
  5148. ' Green:1',
  5149. ' };',
  5150. 'this.e = 0;',
  5151. 'this.f = 1;',
  5152. 'this.i = 0;'
  5153. ]),
  5154. LinesToStr([
  5155. '$mod.e=1;',
  5156. '$mod.i=$mod.e;'
  5157. ]));
  5158. end;
  5159. procedure TTestModule.TestEnum_ConstFail;
  5160. begin
  5161. StartProgram(false);
  5162. Add([
  5163. 'type TMyEnum = (Red = 100, Green = 101);',
  5164. 'var',
  5165. ' e: TMyEnum;',
  5166. ' f: TMyEnum = Green;',
  5167. 'begin',
  5168. ' e:=green;']);
  5169. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5170. ConvertProgram;
  5171. end;
  5172. procedure TTestModule.TestEnum_Functions;
  5173. begin
  5174. StartProgram(false);
  5175. Add([
  5176. 'type TMyEnum = (Red, Green);',
  5177. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5178. 'var',
  5179. ' v: longint;',
  5180. ' s: string;',
  5181. 'begin',
  5182. ' val(s,e,v);',
  5183. ' val(s,e,i);',
  5184. 'end;',
  5185. 'var',
  5186. ' e: TMyEnum;',
  5187. ' i: longint;',
  5188. ' s: string;',
  5189. ' b: boolean;',
  5190. 'begin',
  5191. ' i:=ord(red);',
  5192. ' i:=ord(green);',
  5193. ' i:=ord(e);',
  5194. ' i:=ord(b);',
  5195. ' e:=low(tmyenum);',
  5196. ' e:=low(e);',
  5197. ' b:=low(boolean);',
  5198. ' e:=high(tmyenum);',
  5199. ' e:=high(e);',
  5200. ' b:=high(boolean);',
  5201. ' e:=pred(green);',
  5202. ' e:=pred(e);',
  5203. ' b:=pred(b);',
  5204. ' e:=succ(red);',
  5205. ' e:=succ(e);',
  5206. ' b:=succ(b);',
  5207. ' e:=tmyenum(1);',
  5208. ' e:=tmyenum(i);',
  5209. ' s:=str(e);',
  5210. ' str(e,s);',
  5211. ' str(red,s);',
  5212. ' s:=str(e:3);',
  5213. ' writestr(s,e:3,red);',
  5214. ' val(s,e,i);',
  5215. ' i:=longint(e);']);
  5216. ConvertProgram;
  5217. CheckSource('TestEnum_Functions',
  5218. LinesToStr([ // statements
  5219. 'this.TMyEnum = {',
  5220. ' "0":"Red",',
  5221. ' Red:0,',
  5222. ' "1":"Green",',
  5223. ' Green:1',
  5224. ' };',
  5225. 'this.DoIt = function (e, i) {',
  5226. ' var v = 0;',
  5227. ' var s = "";',
  5228. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5229. ' v = w;',
  5230. ' }));',
  5231. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5232. '};',
  5233. 'this.e = 0;',
  5234. 'this.i = 0;',
  5235. 'this.s = "";',
  5236. 'this.b = false;',
  5237. '']),
  5238. LinesToStr([
  5239. '$mod.i=$mod.TMyEnum.Red;',
  5240. '$mod.i=$mod.TMyEnum.Green;',
  5241. '$mod.i=$mod.e;',
  5242. '$mod.i=$mod.b+0;',
  5243. '$mod.e=$mod.TMyEnum.Red;',
  5244. '$mod.e=$mod.TMyEnum.Red;',
  5245. '$mod.b=false;',
  5246. '$mod.e=$mod.TMyEnum.Green;',
  5247. '$mod.e=$mod.TMyEnum.Green;',
  5248. '$mod.b=true;',
  5249. '$mod.e=$mod.TMyEnum.Green-1;',
  5250. '$mod.e=$mod.e-1;',
  5251. '$mod.b=false;',
  5252. '$mod.e=$mod.TMyEnum.Red+1;',
  5253. '$mod.e=$mod.e+1;',
  5254. '$mod.b=true;',
  5255. '$mod.e=1;',
  5256. '$mod.e=$mod.i;',
  5257. '$mod.s = $mod.TMyEnum[$mod.e];',
  5258. '$mod.s = $mod.TMyEnum[$mod.e];',
  5259. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5260. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5261. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5262. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5263. ' $mod.i = v;',
  5264. '});',
  5265. '$mod.i=$mod.e;',
  5266. '']));
  5267. end;
  5268. procedure TTestModule.TestEnumRg_Functions;
  5269. begin
  5270. StartProgram(false);
  5271. Add([
  5272. 'type',
  5273. ' TEnum = (Red, Green, Blue);',
  5274. ' TEnumRg = Green..Blue;',
  5275. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5276. 'var',
  5277. ' v: longint;',
  5278. ' s: string;',
  5279. 'begin',
  5280. ' val(s,e,v);',
  5281. ' val(s,e,i);',
  5282. 'end;',
  5283. 'var',
  5284. ' e: TEnumRg;',
  5285. ' i: longint;',
  5286. ' s: string;',
  5287. 'begin',
  5288. ' i:=ord(green);',
  5289. ' i:=ord(e);',
  5290. ' e:=low(tenumrg);',
  5291. ' e:=low(e);',
  5292. ' e:=high(tenumrg);',
  5293. ' e:=high(e);',
  5294. ' e:=pred(blue);',
  5295. ' e:=pred(e);',
  5296. ' e:=succ(green);',
  5297. ' e:=succ(e);',
  5298. ' e:=tenumrg(1);',
  5299. ' e:=tenumrg(i);',
  5300. ' s:=str(e);',
  5301. ' str(e,s);',
  5302. ' str(red,s);',
  5303. ' s:=str(e:3);',
  5304. ' writestr(s,e:3,blue);',
  5305. ' val(s,e,i);',
  5306. ' i:=longint(e);']);
  5307. ConvertProgram;
  5308. CheckSource('TestEnumRg_Functions',
  5309. LinesToStr([ // statements
  5310. 'this.TEnum = {',
  5311. ' "0":"Red",',
  5312. ' Red:0,',
  5313. ' "1":"Green",',
  5314. ' Green:1,',
  5315. ' "2":"Blue",',
  5316. ' Blue:2',
  5317. ' };',
  5318. 'this.DoIt = function (e, i) {',
  5319. ' var v = 0;',
  5320. ' var s = "";',
  5321. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5322. ' v = w;',
  5323. ' }));',
  5324. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5325. '};',
  5326. 'this.e = this.TEnum.Green;',
  5327. 'this.i = 0;',
  5328. 'this.s = "";',
  5329. '']),
  5330. LinesToStr([
  5331. '$mod.i=$mod.TEnum.Green;',
  5332. '$mod.i=$mod.e;',
  5333. '$mod.e=$mod.TEnum.Green;',
  5334. '$mod.e=$mod.TEnum.Green;',
  5335. '$mod.e=$mod.TEnum.Blue;',
  5336. '$mod.e=$mod.TEnum.Blue;',
  5337. '$mod.e=$mod.TEnum.Blue-1;',
  5338. '$mod.e=$mod.e-1;',
  5339. '$mod.e=$mod.TEnum.Green+1;',
  5340. '$mod.e=$mod.e+1;',
  5341. '$mod.e=1;',
  5342. '$mod.e=$mod.i;',
  5343. '$mod.s = $mod.TEnum[$mod.e];',
  5344. '$mod.s = $mod.TEnum[$mod.e];',
  5345. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5346. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5347. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5348. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5349. ' $mod.i = v;',
  5350. '});',
  5351. '$mod.i=$mod.e;',
  5352. '']));
  5353. end;
  5354. procedure TTestModule.TestEnum_AsParams;
  5355. begin
  5356. StartProgram(false);
  5357. Add('type TEnum = (Red,Blue);');
  5358. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5359. Add('var vJ: TEnum;');
  5360. Add('begin');
  5361. Add(' vg:=vg;');
  5362. Add(' vj:=vh;');
  5363. Add(' vi:=vi;');
  5364. Add(' doit(vg,vg,vg);');
  5365. Add(' doit(vh,vh,vj);');
  5366. Add(' doit(vi,vi,vi);');
  5367. Add(' doit(vj,vj,vj);');
  5368. Add('end;');
  5369. Add('var i: TEnum;');
  5370. Add('begin');
  5371. Add(' doit(i,i,i);');
  5372. ConvertProgram;
  5373. CheckSource('TestEnum_AsParams',
  5374. LinesToStr([ // statements
  5375. 'this.TEnum = {',
  5376. ' "0": "Red",',
  5377. ' Red: 0,',
  5378. ' "1": "Blue",',
  5379. ' Blue: 1',
  5380. '};',
  5381. 'this.DoIt = function (vG,vH,vI) {',
  5382. ' var vJ = 0;',
  5383. ' vG = vG;',
  5384. ' vJ = vH;',
  5385. ' vI.set(vI.get());',
  5386. ' $mod.DoIt(vG, vG, {',
  5387. ' get: function () {',
  5388. ' return vG;',
  5389. ' },',
  5390. ' set: function (v) {',
  5391. ' vG = v;',
  5392. ' }',
  5393. ' });',
  5394. ' $mod.DoIt(vH, vH, {',
  5395. ' get: function () {',
  5396. ' return vJ;',
  5397. ' },',
  5398. ' set: function (v) {',
  5399. ' vJ = v;',
  5400. ' }',
  5401. ' });',
  5402. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5403. ' $mod.DoIt(vJ, vJ, {',
  5404. ' get: function () {',
  5405. ' return vJ;',
  5406. ' },',
  5407. ' set: function (v) {',
  5408. ' vJ = v;',
  5409. ' }',
  5410. ' });',
  5411. '};',
  5412. 'this.i = 0;'
  5413. ]),
  5414. LinesToStr([
  5415. '$mod.DoIt($mod.i,$mod.i,{',
  5416. ' p: $mod,',
  5417. ' get: function () {',
  5418. ' return this.p.i;',
  5419. ' },',
  5420. ' set: function (v) {',
  5421. ' this.p.i = v;',
  5422. ' }',
  5423. '});'
  5424. ]));
  5425. end;
  5426. procedure TTestModule.TestEnumRange_Array;
  5427. begin
  5428. StartProgram(false);
  5429. Add([
  5430. 'type',
  5431. ' TEnum = (Red, Green, Blue);',
  5432. ' TEnumRg = green..blue;',
  5433. ' TArr = array[TEnumRg] of byte;',
  5434. ' TArr2 = array[green..blue] of byte;',
  5435. 'var',
  5436. ' a: TArr;',
  5437. ' b: TArr = (3,4);',
  5438. ' c: TArr2 = (5,6);',
  5439. 'begin',
  5440. ' a[green] := b[blue];',
  5441. ' c[green] := c[blue];',
  5442. '']);
  5443. ConvertProgram;
  5444. CheckSource('TestEnumRange_Array',
  5445. LinesToStr([ // statements
  5446. 'this.TEnum = {',
  5447. ' "0": "Red",',
  5448. ' Red: 0,',
  5449. ' "1": "Green",',
  5450. ' Green: 1,',
  5451. ' "2": "Blue",',
  5452. ' Blue: 2',
  5453. '};',
  5454. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5455. 'this.b = [3, 4];',
  5456. 'this.c = [5, 6];',
  5457. '']),
  5458. LinesToStr([
  5459. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5460. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5461. '']));
  5462. end;
  5463. procedure TTestModule.TestEnum_ForIn;
  5464. begin
  5465. StartProgram(false);
  5466. Add([
  5467. 'type',
  5468. ' TEnum = (Red, Green, Blue);',
  5469. ' TEnumRg = green..blue;',
  5470. ' TArr = array[TEnum] of byte;',
  5471. ' TArrRg = array[TEnumRg] of byte;',
  5472. 'var',
  5473. ' e: TEnum;',
  5474. ' a1: TArr = (3,4,5);',
  5475. ' a2: TArrRg = (11,12);',
  5476. ' b: byte;',
  5477. 'begin',
  5478. ' for e in TEnum do ;',
  5479. ' for e in TEnumRg do ;',
  5480. ' for e in TArr do ;',
  5481. ' for e in TArrRg do ;',
  5482. ' for b in a1 do ;',
  5483. ' for b in a2 do ;',
  5484. '']);
  5485. ConvertProgram;
  5486. CheckSource('TestEnum_ForIn',
  5487. LinesToStr([ // statements
  5488. 'this.TEnum = {',
  5489. ' "0": "Red",',
  5490. ' Red: 0,',
  5491. ' "1": "Green",',
  5492. ' Green: 1,',
  5493. ' "2": "Blue",',
  5494. ' Blue: 2',
  5495. '};',
  5496. 'this.e = 0;',
  5497. 'this.a1 = [3, 4, 5];',
  5498. 'this.a2 = [11, 12];',
  5499. 'this.b = 0;',
  5500. '']),
  5501. LinesToStr([
  5502. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5503. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5504. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5505. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5506. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5507. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5508. '']));
  5509. end;
  5510. procedure TTestModule.TestEnum_ScopedNumber;
  5511. begin
  5512. Converter.Options:=Converter.Options+[coEnumNumbers];
  5513. StartProgram(false);
  5514. Add([
  5515. 'type',
  5516. ' TEnum = (Red, Green);',
  5517. 'var',
  5518. ' e: TEnum;',
  5519. 'begin',
  5520. ' e:=TEnum.Green;',
  5521. '']);
  5522. ConvertProgram;
  5523. CheckSource('TestEnum_ScopedNumber',
  5524. LinesToStr([ // statements
  5525. 'this.TEnum = {',
  5526. ' "0": "Red",',
  5527. ' Red: 0,',
  5528. ' "1": "Green",',
  5529. ' Green: 1',
  5530. '};',
  5531. 'this.e = 0;',
  5532. '']),
  5533. LinesToStr([
  5534. '$mod.e = 1;']));
  5535. end;
  5536. procedure TTestModule.TestEnum_InFunction;
  5537. begin
  5538. StartProgram(false);
  5539. Add([
  5540. 'const TEnum = 3;',
  5541. 'procedure DoIt;',
  5542. 'type',
  5543. ' TEnum = (Red, Green, Blue);',
  5544. ' procedure Sub;',
  5545. ' type',
  5546. ' TEnumSub = (Left, Right);',
  5547. ' var',
  5548. ' es: TEnumSub;',
  5549. ' begin',
  5550. ' es:=Left;',
  5551. ' end;',
  5552. 'var',
  5553. ' e, e2: TEnum;',
  5554. 'begin',
  5555. ' if e in [red,blue] then e2:=e;',
  5556. 'end;',
  5557. 'begin']);
  5558. ConvertProgram;
  5559. CheckSource('TestEnum_InFunction',
  5560. LinesToStr([ // statements
  5561. 'this.TEnum = 3;',
  5562. 'var TEnum$1 = {',
  5563. ' "0":"Red",',
  5564. ' Red:0,',
  5565. ' "1":"Green",',
  5566. ' Green:1,',
  5567. ' "2":"Blue",',
  5568. ' Blue:2',
  5569. ' };',
  5570. 'var TEnumSub = {',
  5571. ' "0": "Left",',
  5572. ' Left: 0,',
  5573. ' "1": "Right",',
  5574. ' Right: 1',
  5575. '};',
  5576. 'this.DoIt = function () {',
  5577. ' function Sub() {',
  5578. ' var es = 0;',
  5579. ' es = TEnumSub.Left;',
  5580. ' };',
  5581. ' var e = 0;',
  5582. ' var e2 = 0;',
  5583. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5584. '};',
  5585. '']),
  5586. LinesToStr([
  5587. '']));
  5588. end;
  5589. procedure TTestModule.TestSet_Enum;
  5590. begin
  5591. StartProgram(false);
  5592. Add([
  5593. 'type',
  5594. ' TColor = (Red, Green, Blue);',
  5595. ' TColors = set of TColor;',
  5596. 'var',
  5597. ' c: TColor;',
  5598. ' s: TColors;',
  5599. ' t: TColors = [];',
  5600. ' u: TColors = [Red];',
  5601. 'begin',
  5602. ' s:=[];',
  5603. ' s:=[Green];',
  5604. ' s:=[Green,Blue];',
  5605. ' s:=[Red..Blue];',
  5606. ' s:=[Red,Green..Blue];',
  5607. ' s:=[Red,c];',
  5608. ' s:=t;',
  5609. ' s:=default(TColors);',
  5610. '']);
  5611. ConvertProgram;
  5612. CheckSource('TestSet',
  5613. LinesToStr([ // statements
  5614. 'this.TColor = {',
  5615. ' "0":"Red",',
  5616. ' Red:0,',
  5617. ' "1":"Green",',
  5618. ' Green:1,',
  5619. ' "2":"Blue",',
  5620. ' Blue:2',
  5621. ' };',
  5622. 'this.c = 0;',
  5623. 'this.s = {};',
  5624. 'this.t = {};',
  5625. 'this.u = rtl.createSet(this.TColor.Red);'
  5626. ]),
  5627. LinesToStr([
  5628. '$mod.s={};',
  5629. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5630. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5631. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5632. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5633. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5634. '$mod.s=rtl.refSet($mod.t);',
  5635. '$mod.s={};',
  5636. '']));
  5637. end;
  5638. procedure TTestModule.TestSet_Operators;
  5639. begin
  5640. StartProgram(false);
  5641. Add('type');
  5642. Add(' TColor = (Red, Green, Blue);');
  5643. Add(' TColors = set of tcolor;');
  5644. Add('var');
  5645. Add(' vC: TColor;');
  5646. Add(' vS: TColors;');
  5647. Add(' vT: TColors;');
  5648. Add(' vU: TColors;');
  5649. Add(' B: boolean;');
  5650. Add('begin');
  5651. Add(' include(vs,green);');
  5652. Add(' exclude(vs,vc);');
  5653. Add(' vs:=vt+vu;');
  5654. Add(' vs:=vt+[red];');
  5655. Add(' vs:=[red]+vt;');
  5656. Add(' vs:=[red]+[green];');
  5657. Add(' vs:=vt-vu;');
  5658. Add(' vs:=vt-[red];');
  5659. Add(' vs:=[red]-vt;');
  5660. Add(' vs:=[red]-[green];');
  5661. Add(' vs:=vt*vu;');
  5662. Add(' vs:=vt*[red];');
  5663. Add(' vs:=[red]*vt;');
  5664. Add(' vs:=[red]*[green];');
  5665. Add(' vs:=vt><vu;');
  5666. Add(' vs:=vt><[red];');
  5667. Add(' vs:=[red]><vt;');
  5668. Add(' vs:=[red]><[green];');
  5669. Add(' b:=vt=vu;');
  5670. Add(' b:=vt=[red];');
  5671. Add(' b:=[red]=vt;');
  5672. Add(' b:=[red]=[green];');
  5673. Add(' b:=vt<>vu;');
  5674. Add(' b:=vt<>[red];');
  5675. Add(' b:=[red]<>vt;');
  5676. Add(' b:=[red]<>[green];');
  5677. Add(' b:=vt<=vu;');
  5678. Add(' b:=vt<=[red];');
  5679. Add(' b:=[red]<=vt;');
  5680. Add(' b:=[red]<=[green];');
  5681. Add(' b:=vt>=vu;');
  5682. Add(' b:=vt>=[red];');
  5683. Add(' b:=[red]>=vt;');
  5684. Add(' b:=[red]>=[green];');
  5685. ConvertProgram;
  5686. CheckSource('TestSet_Operators',
  5687. LinesToStr([ // statements
  5688. 'this.TColor = {',
  5689. ' "0":"Red",',
  5690. ' Red:0,',
  5691. ' "1":"Green",',
  5692. ' Green:1,',
  5693. ' "2":"Blue",',
  5694. ' Blue:2',
  5695. ' };',
  5696. 'this.vC = 0;',
  5697. 'this.vS = {};',
  5698. 'this.vT = {};',
  5699. 'this.vU = {};',
  5700. 'this.B = false;'
  5701. ]),
  5702. LinesToStr([
  5703. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5704. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5705. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5706. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5707. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5708. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5709. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5710. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5711. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5712. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5713. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5714. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5715. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5716. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5717. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5718. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5719. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5720. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5721. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5722. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5723. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5724. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5725. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5726. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5727. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5728. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5729. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5730. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5731. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5732. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5733. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5734. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5735. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5736. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5737. '']));
  5738. end;
  5739. procedure TTestModule.TestSet_Operator_In;
  5740. begin
  5741. StartProgram(false);
  5742. Add([
  5743. 'type',
  5744. ' TColor = (Red, Green, Blue);',
  5745. ' TColors = set of tcolor;',
  5746. ' TColorRg = green..blue;',
  5747. 'var',
  5748. ' vC: tcolor;',
  5749. ' vT: tcolors;',
  5750. ' B: boolean;',
  5751. ' rg: TColorRg;',
  5752. 'begin',
  5753. ' b:=red in vt;',
  5754. ' b:=vc in vt;',
  5755. ' b:=green in [red..blue];',
  5756. ' b:=vc in [red..blue];',
  5757. ' ',
  5758. ' if red in vt then ;',
  5759. ' while vC in vt do ;',
  5760. ' repeat',
  5761. ' until vC in vt;',
  5762. ' if rg in [green..blue] then ;',
  5763. '']);
  5764. ConvertProgram;
  5765. CheckSource('TestSet_Operator_In',
  5766. LinesToStr([ // statements
  5767. 'this.TColor = {',
  5768. ' "0":"Red",',
  5769. ' Red:0,',
  5770. ' "1":"Green",',
  5771. ' Green:1,',
  5772. ' "2":"Blue",',
  5773. ' Blue:2',
  5774. ' };',
  5775. 'this.vC = 0;',
  5776. 'this.vT = {};',
  5777. 'this.B = false;',
  5778. 'this.rg = this.TColor.Green;',
  5779. '']),
  5780. LinesToStr([
  5781. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5782. '$mod.B = $mod.vC in $mod.vT;',
  5783. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5784. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5785. 'if ($mod.TColor.Red in $mod.vT) ;',
  5786. 'while ($mod.vC in $mod.vT) {',
  5787. '};',
  5788. 'do {',
  5789. '} while (!($mod.vC in $mod.vT));',
  5790. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5791. '']));
  5792. end;
  5793. procedure TTestModule.TestSet_Functions;
  5794. begin
  5795. StartProgram(false);
  5796. Add('type');
  5797. Add(' TMyEnum = (Red, Green);');
  5798. Add(' TMyEnums = set of TMyEnum;');
  5799. Add('var');
  5800. Add(' e: TMyEnum;');
  5801. Add(' s: TMyEnums;');
  5802. Add('begin');
  5803. Add(' e:=Low(TMyEnums);');
  5804. Add(' e:=Low(s);');
  5805. Add(' e:=High(TMyEnums);');
  5806. Add(' e:=High(s);');
  5807. ConvertProgram;
  5808. CheckSource('TestSetFunctions',
  5809. LinesToStr([ // statements
  5810. 'this.TMyEnum = {',
  5811. ' "0":"Red",',
  5812. ' Red:0,',
  5813. ' "1":"Green",',
  5814. ' Green:1',
  5815. ' };',
  5816. 'this.e = 0;',
  5817. 'this.s = {};'
  5818. ]),
  5819. LinesToStr([
  5820. '$mod.e=$mod.TMyEnum.Red;',
  5821. '$mod.e=$mod.TMyEnum.Red;',
  5822. '$mod.e=$mod.TMyEnum.Green;',
  5823. '$mod.e=$mod.TMyEnum.Green;',
  5824. '']));
  5825. end;
  5826. procedure TTestModule.TestSet_PassAsArgClone;
  5827. begin
  5828. StartProgram(false);
  5829. Add('type');
  5830. Add(' TMyEnum = (Red, Green);');
  5831. Add(' TMyEnums = set of TMyEnum;');
  5832. Add('procedure DoDefault(s: tmyenums); begin end;');
  5833. Add('procedure DoConst(const s: tmyenums); begin end;');
  5834. Add('var');
  5835. Add(' aSet: tmyenums;');
  5836. Add('begin');
  5837. Add(' dodefault(aset);');
  5838. Add(' doconst(aset);');
  5839. ConvertProgram;
  5840. CheckSource('TestSetFunctions',
  5841. LinesToStr([ // statements
  5842. 'this.TMyEnum = {',
  5843. ' "0":"Red",',
  5844. ' Red:0,',
  5845. ' "1":"Green",',
  5846. ' Green:1',
  5847. ' };',
  5848. 'this.DoDefault = function (s) {',
  5849. '};',
  5850. 'this.DoConst = function (s) {',
  5851. '};',
  5852. 'this.aSet = {};'
  5853. ]),
  5854. LinesToStr([
  5855. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5856. '$mod.DoConst($mod.aSet);',
  5857. '']));
  5858. end;
  5859. procedure TTestModule.TestSet_AsParams;
  5860. begin
  5861. StartProgram(false);
  5862. Add([
  5863. 'type TEnum = (Red,Blue);',
  5864. 'type TEnums = set of TEnum;',
  5865. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5866. 'var vJ: TEnums;',
  5867. 'begin',
  5868. ' Include(vg,red);',
  5869. ' Include(result,blue);',
  5870. ' vg:=vg;',
  5871. ' vj:=vh;',
  5872. ' vi:=vi;',
  5873. ' doit(vg,vg,vg);',
  5874. ' doit(vh,vh,vj);',
  5875. ' doit(vi,vi,vi);',
  5876. ' doit(vj,vj,vj);',
  5877. 'end;',
  5878. 'var i: TEnums;',
  5879. 'begin',
  5880. ' doit(i,i,i);']);
  5881. ConvertProgram;
  5882. CheckSource('TestSet_AsParams',
  5883. LinesToStr([ // statements
  5884. 'this.TEnum = {',
  5885. ' "0": "Red",',
  5886. ' Red: 0,',
  5887. ' "1": "Blue",',
  5888. ' Blue: 1',
  5889. '};',
  5890. 'this.DoIt = function (vG,vH,vI) {',
  5891. ' var Result = {};',
  5892. ' var vJ = {};',
  5893. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5894. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5895. ' vG = rtl.refSet(vG);',
  5896. ' vJ = rtl.refSet(vH);',
  5897. ' vI.set(rtl.refSet(vI.get()));',
  5898. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5899. ' get: function () {',
  5900. ' return vG;',
  5901. ' },',
  5902. ' set: function (v) {',
  5903. ' vG = v;',
  5904. ' }',
  5905. ' });',
  5906. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5907. ' get: function () {',
  5908. ' return vJ;',
  5909. ' },',
  5910. ' set: function (v) {',
  5911. ' vJ = v;',
  5912. ' }',
  5913. ' });',
  5914. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5915. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5916. ' get: function () {',
  5917. ' return vJ;',
  5918. ' },',
  5919. ' set: function (v) {',
  5920. ' vJ = v;',
  5921. ' }',
  5922. ' });',
  5923. ' return Result;',
  5924. '};',
  5925. 'this.i = {};'
  5926. ]),
  5927. LinesToStr([
  5928. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5929. ' p: $mod,',
  5930. ' get: function () {',
  5931. ' return this.p.i;',
  5932. ' },',
  5933. ' set: function (v) {',
  5934. ' this.p.i = v;',
  5935. ' }',
  5936. '});'
  5937. ]));
  5938. end;
  5939. procedure TTestModule.TestSet_Property;
  5940. begin
  5941. StartProgram(false);
  5942. Add('type');
  5943. Add(' TEnum = (Red,Blue);');
  5944. Add(' TEnums = set of TEnum;');
  5945. Add(' TObject = class');
  5946. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5947. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5948. Add(' property Colors: TEnums read GetColors write SetColors;');
  5949. Add(' end;');
  5950. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5951. Add('begin end;');
  5952. Add('var Obj: TObject;');
  5953. Add('begin');
  5954. Add(' Include(Obj.Colors,Red);');
  5955. Add(' Exclude(Obj.Colors,Red);');
  5956. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5957. ConvertProgram;
  5958. CheckSource('TestSet_Property',
  5959. LinesToStr([ // statements
  5960. 'this.TEnum = {',
  5961. ' "0": "Red",',
  5962. ' Red: 0,',
  5963. ' "1": "Blue",',
  5964. ' Blue: 1',
  5965. '};',
  5966. 'rtl.createClass(this, "TObject", null, function () {',
  5967. ' this.$init = function () {',
  5968. ' };',
  5969. ' this.$final = function () {',
  5970. ' };',
  5971. '});',
  5972. 'this.DoIt = function (i, j, k, l) {',
  5973. '};',
  5974. 'this.Obj = null;',
  5975. '']),
  5976. LinesToStr([
  5977. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5978. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5979. '']));
  5980. end;
  5981. procedure TTestModule.TestSet_EnumConst;
  5982. begin
  5983. StartProgram(false);
  5984. Add([
  5985. 'type',
  5986. ' TEnum = (Red,Blue);',
  5987. ' TEnums = set of TEnum;',
  5988. 'const',
  5989. ' Orange = red;',
  5990. 'var',
  5991. ' Enum: tenum;',
  5992. ' Enums: tenums;',
  5993. 'begin',
  5994. ' Include(enums,orange);',
  5995. ' Exclude(enums,orange);',
  5996. ' if orange in enums then;',
  5997. ' if orange in [orange,red] then;']);
  5998. ConvertProgram;
  5999. CheckSource('TestSet_EnumConst',
  6000. LinesToStr([ // statements
  6001. 'this.TEnum = {',
  6002. ' "0": "Red",',
  6003. ' Red: 0,',
  6004. ' "1": "Blue",',
  6005. ' Blue: 1',
  6006. '};',
  6007. 'this.Orange = this.TEnum.Red;',
  6008. 'this.Enum = 0;',
  6009. 'this.Enums = {};',
  6010. '']),
  6011. LinesToStr([
  6012. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6013. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6014. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6015. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6016. '']));
  6017. end;
  6018. procedure TTestModule.TestSet_IntConst;
  6019. begin
  6020. StartProgram(false);
  6021. Add([
  6022. 'type',
  6023. ' TEnums = set of Byte;',
  6024. 'const',
  6025. ' Orange = 0;',
  6026. 'var',
  6027. ' Enum: byte;',
  6028. ' Enums: tenums;',
  6029. 'begin',
  6030. ' Enums:=[];',
  6031. ' Enums:=[0];',
  6032. ' Enums:=[1..2];',
  6033. //' Include(enums,orange);',
  6034. //' Exclude(enums,orange);',
  6035. ' if orange in enums then;',
  6036. ' if orange in [orange,1] then;']);
  6037. ConvertProgram;
  6038. CheckSource('TestSet_IntConst',
  6039. LinesToStr([ // statements
  6040. 'this.Orange = 0;',
  6041. 'this.Enum = 0;',
  6042. 'this.Enums = {};',
  6043. '']),
  6044. LinesToStr([
  6045. '$mod.Enums = {};',
  6046. '$mod.Enums = rtl.createSet(0);',
  6047. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6048. 'if (0 in $mod.Enums) ;',
  6049. 'if (0 in rtl.createSet(0, 1)) ;',
  6050. '']));
  6051. end;
  6052. procedure TTestModule.TestSet_AnonymousEnumType;
  6053. begin
  6054. StartProgram(false);
  6055. Add('type');
  6056. Add(' TFlags = set of (red, green);');
  6057. Add('const');
  6058. Add(' favorite = red;');
  6059. Add('var');
  6060. Add(' f: TFlags;');
  6061. Add(' i: longint;');
  6062. Add('begin');
  6063. Add(' Include(f,red);');
  6064. Add(' Include(f,favorite);');
  6065. Add(' i:=ord(red);');
  6066. Add(' i:=ord(favorite);');
  6067. Add(' i:=ord(low(TFlags));');
  6068. Add(' i:=ord(low(f));');
  6069. Add(' i:=ord(low(favorite));');
  6070. Add(' i:=ord(high(TFlags));');
  6071. Add(' i:=ord(high(f));');
  6072. Add(' i:=ord(high(favorite));');
  6073. Add(' f:=[green,favorite];');
  6074. ConvertProgram;
  6075. CheckSource('TestSet_AnonymousEnumType',
  6076. LinesToStr([ // statements
  6077. 'this.TFlags$a = {',
  6078. ' "0": "red",',
  6079. ' red: 0,',
  6080. ' "1": "green",',
  6081. ' green: 1',
  6082. '};',
  6083. 'this.favorite = this.TFlags$a.red;',
  6084. 'this.f = {};',
  6085. 'this.i = 0;',
  6086. '']),
  6087. LinesToStr([
  6088. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6089. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6090. '$mod.i = $mod.TFlags$a.red;',
  6091. '$mod.i = $mod.TFlags$a.red;',
  6092. '$mod.i = $mod.TFlags$a.red;',
  6093. '$mod.i = $mod.TFlags$a.red;',
  6094. '$mod.i = $mod.TFlags$a.red;',
  6095. '$mod.i = $mod.TFlags$a.green;',
  6096. '$mod.i = $mod.TFlags$a.green;',
  6097. '$mod.i = $mod.TFlags$a.green;',
  6098. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6099. '']));
  6100. end;
  6101. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6102. begin
  6103. exit;
  6104. StartProgram(false);
  6105. Add([
  6106. 'type',
  6107. ' TAtoZ = ''A''..''Z'';',
  6108. ' TSetOfAZ = set of TAtoZ;',
  6109. 'var',
  6110. ' c: char;',
  6111. ' a: TAtoZ;',
  6112. ' s: TSetOfAZ = [''P'',''A''];',
  6113. ' i: longint;',
  6114. 'begin',
  6115. ' Include(s,''S'');',
  6116. ' Include(s,c);',
  6117. ' Include(s,a);',
  6118. ' c:=low(TAtoZ);',
  6119. ' i:=ord(low(TAtoZ));',
  6120. ' a:=high(TAtoZ);',
  6121. ' a:=high(TSetOfAtoZ);',
  6122. ' s:=[a,c,''M''];',
  6123. '']);
  6124. ConvertProgram;
  6125. CheckSource('TestSet_AnonymousEnumTypeChar',
  6126. LinesToStr([ // statements
  6127. '']),
  6128. LinesToStr([
  6129. '']));
  6130. end;
  6131. procedure TTestModule.TestSet_ConstEnum;
  6132. begin
  6133. StartProgram(false);
  6134. Add([
  6135. 'type',
  6136. ' TEnum = (red,blue,green);',
  6137. ' TEnums = set of TEnum;',
  6138. 'const',
  6139. ' teAny = [low(TEnum)..high(TEnum)];',
  6140. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6141. 'var',
  6142. ' e: TEnum;',
  6143. ' s: TEnums;',
  6144. 'begin',
  6145. ' if blue in teAny then;',
  6146. ' if blue in teAny+[e] then;',
  6147. ' if blue in teAny+teRedBlue then;',
  6148. ' if e in [red,blue] then;',
  6149. ' s:=teAny;',
  6150. ' s:=teAny+[e];',
  6151. ' s:=[e]+teAny;',
  6152. ' s:=teAny+teRedBlue;',
  6153. ' s:=teAny+teRedBlue+[e];',
  6154. '']);
  6155. ConvertProgram;
  6156. CheckSource('TestSet_ConstEnum',
  6157. LinesToStr([ // statements
  6158. 'this.TEnum = {',
  6159. ' "0": "red",',
  6160. ' red: 0,',
  6161. ' "1": "blue",',
  6162. ' blue: 1,',
  6163. ' "2": "green",',
  6164. ' green: 2',
  6165. '};',
  6166. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6167. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6168. 'this.e = 0;',
  6169. 'this.s = {};',
  6170. '']),
  6171. LinesToStr([
  6172. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6173. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6174. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6175. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6176. '$mod.s = rtl.refSet($mod.teAny);',
  6177. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6178. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6179. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6180. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6181. '']));
  6182. end;
  6183. procedure TTestModule.TestSet_ConstChar;
  6184. begin
  6185. StartProgram(false);
  6186. Add([
  6187. 'const',
  6188. ' LowChars = [''a''..''z''];',
  6189. ' Chars = LowChars+[''A''..''Z''];',
  6190. ' sc = [''А'', ''Я''];',
  6191. 'var',
  6192. ' c: char;',
  6193. ' s: string;',
  6194. 'begin',
  6195. ' if c in lowchars then ;',
  6196. ' if ''a'' in lowchars then ;',
  6197. ' if s[1] in lowchars then ;',
  6198. ' if c in chars then ;',
  6199. ' if c in [''a''..''z'',''_''] then ;',
  6200. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6201. ' if ''Я'' in sc then ;',
  6202. ' if 3=ord('' '') then ;',
  6203. '']);
  6204. ConvertProgram;
  6205. CheckSource('TestSet_ConstChar',
  6206. LinesToStr([ // statements
  6207. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6208. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6209. 'this.sc = rtl.createSet(1040, 1071);',
  6210. 'this.c = "";',
  6211. 'this.s = "";',
  6212. '']),
  6213. LinesToStr([
  6214. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6215. 'if (97 in $mod.LowChars) ;',
  6216. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6217. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6218. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6219. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6220. 'if (1071 in $mod.sc) ;',
  6221. 'if (3 === 32) ;',
  6222. '']));
  6223. end;
  6224. procedure TTestModule.TestSet_ConstInt;
  6225. begin
  6226. StartProgram(false);
  6227. Add([
  6228. 'const',
  6229. ' Months = [1..12];',
  6230. ' Mirror = [-12..-1]+Months;',
  6231. 'var',
  6232. ' i: smallint;',
  6233. 'begin',
  6234. ' if 3 in Months then;',
  6235. ' if i in Months+[i] then;',
  6236. ' if i in Months+Mirror then;',
  6237. ' if i in [4..6,8] then;',
  6238. '']);
  6239. ConvertProgram;
  6240. CheckSource('TestSet_ConstInt',
  6241. LinesToStr([ // statements
  6242. 'this.Months = rtl.createSet(null, 1, 12);',
  6243. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6244. 'this.i = 0;',
  6245. '']),
  6246. LinesToStr([
  6247. 'if (3 in $mod.Months) ;',
  6248. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6249. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6250. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6251. '']));
  6252. end;
  6253. procedure TTestModule.TestSet_InFunction;
  6254. begin
  6255. StartProgram(false);
  6256. Add([
  6257. 'const',
  6258. ' TEnum = 3;',
  6259. ' TSetOfEnum = 4;',
  6260. ' TSetOfAno = 5;',
  6261. 'procedure DoIt;',
  6262. 'type',
  6263. ' TEnum = (red, blue);',
  6264. ' TSetOfEnum = set of TEnum;',
  6265. ' TSetOfAno = set of (up,down);',
  6266. 'var',
  6267. ' e: TEnum;',
  6268. ' se: TSetOfEnum;',
  6269. ' sa: TSetOfAno;',
  6270. 'begin',
  6271. ' se:=[e];',
  6272. ' sa:=[up];',
  6273. 'end;',
  6274. 'begin',
  6275. '']);
  6276. ConvertProgram;
  6277. CheckSource('TestSet_InFunction',
  6278. LinesToStr([ // statements
  6279. 'this.TEnum = 3;',
  6280. 'this.TSetOfEnum = 4;',
  6281. 'this.TSetOfAno = 5;',
  6282. 'var TEnum$1 = {',
  6283. ' "0": "red",',
  6284. ' red: 0,',
  6285. ' "1": "blue",',
  6286. ' blue: 1',
  6287. '};',
  6288. 'var TSetOfAno$a = {',
  6289. ' "0": "up",',
  6290. ' up: 0,',
  6291. ' "1": "down",',
  6292. ' down: 1',
  6293. '};',
  6294. 'this.DoIt = function () {',
  6295. ' var e = 0;',
  6296. ' var se = {};',
  6297. ' var sa = {};',
  6298. ' se = rtl.createSet(e);',
  6299. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6300. '};',
  6301. '']),
  6302. LinesToStr([
  6303. '']));
  6304. end;
  6305. procedure TTestModule.TestSet_ForIn;
  6306. begin
  6307. StartProgram(false);
  6308. Add([
  6309. 'type',
  6310. ' TEnum = (Red, Green, Blue);',
  6311. ' TEnumRg = green..blue;',
  6312. ' TSetOfEnum = set of TEnum;',
  6313. ' TSetOfEnumRg = set of TEnumRg;',
  6314. 'var',
  6315. ' e, e2: TEnum;',
  6316. ' er: TEnum;',
  6317. ' s: TSetOfEnum;',
  6318. 'begin',
  6319. ' for e in TSetOfEnum do ;',
  6320. ' for e in TSetOfEnumRg do ;',
  6321. ' for e in [] do e2:=e;',
  6322. ' for e in [red..green] do e2:=e;',
  6323. ' for e in [green,blue] do e2:=e;',
  6324. ' for e in [red,blue] do e2:=e;',
  6325. ' for e in s do e2:=e;',
  6326. ' for er in TSetOfEnumRg do ;',
  6327. '']);
  6328. ConvertProgram;
  6329. CheckSource('TestSet_ForIn',
  6330. LinesToStr([ // statements
  6331. 'this.TEnum = {',
  6332. ' "0":"Red",',
  6333. ' Red:0,',
  6334. ' "1":"Green",',
  6335. ' Green:1,',
  6336. ' "2":"Blue",',
  6337. ' Blue:2',
  6338. ' };',
  6339. 'this.e = 0;',
  6340. 'this.e2 = 0;',
  6341. 'this.er = 0;',
  6342. 'this.s = {};',
  6343. '']),
  6344. LinesToStr([
  6345. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6346. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6347. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6348. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6349. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6350. 'for (var $l in $mod.s){',
  6351. ' $mod.e = +$l;',
  6352. ' $mod.e2 = $mod.e;',
  6353. '};',
  6354. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6355. '']));
  6356. end;
  6357. procedure TTestModule.TestNestBegin;
  6358. begin
  6359. StartProgram(false);
  6360. Add('begin');
  6361. Add(' begin');
  6362. Add(' begin');
  6363. Add(' end;');
  6364. Add(' begin');
  6365. Add(' if true then ;');
  6366. Add(' end;');
  6367. Add(' end;');
  6368. ConvertProgram;
  6369. CheckSource('TestNestBegin',
  6370. '',
  6371. 'if (true) ;');
  6372. end;
  6373. procedure TTestModule.TestUnitImplVars;
  6374. begin
  6375. StartUnit(false);
  6376. Add('interface');
  6377. Add('implementation');
  6378. Add('var');
  6379. Add(' V1:longint;');
  6380. Add(' V2:longint = 3;');
  6381. Add(' V3:string = ''abc'';');
  6382. ConvertUnit;
  6383. CheckSource('TestUnitImplVars',
  6384. LinesToStr([ // statements
  6385. 'var $impl = $mod.$impl;',
  6386. '']),
  6387. '', // this.$init
  6388. LinesToStr([ // implementation
  6389. '$impl.V1 = 0;',
  6390. '$impl.V2 = 3;',
  6391. '$impl.V3 = "abc";',
  6392. '']) );
  6393. end;
  6394. procedure TTestModule.TestUnitImplConsts;
  6395. begin
  6396. StartUnit(false);
  6397. Add('interface');
  6398. Add('implementation');
  6399. Add('const');
  6400. Add(' v1 = 3;');
  6401. Add(' v2:longint = 4;');
  6402. Add(' v3:string = ''abc'';');
  6403. ConvertUnit;
  6404. CheckSource('TestUnitImplConsts',
  6405. LinesToStr([ // statements
  6406. 'var $impl = $mod.$impl;',
  6407. '']),
  6408. '', // this.$init
  6409. LinesToStr([ // implementation
  6410. '$impl.v1 = 3;',
  6411. '$impl.v2 = 4;',
  6412. '$impl.v3 = "abc";',
  6413. '']) );
  6414. end;
  6415. procedure TTestModule.TestUnitImplRecord;
  6416. begin
  6417. StartUnit(false);
  6418. Add('interface');
  6419. Add('implementation');
  6420. Add('type');
  6421. Add(' TMyRecord = record');
  6422. Add(' i: longint;');
  6423. Add(' end;');
  6424. Add('var aRec: TMyRecord;');
  6425. Add('initialization');
  6426. Add(' arec.i:=3;');
  6427. ConvertUnit;
  6428. CheckSource('TestUnitImplRecord',
  6429. LinesToStr([ // statements
  6430. 'var $impl = $mod.$impl;',
  6431. '']),
  6432. // this.$init
  6433. '$impl.aRec.i = 3;',
  6434. LinesToStr([ // implementation
  6435. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6436. ' this.i = 0;',
  6437. ' this.$eq = function (b) {',
  6438. ' return this.i === b.i;',
  6439. ' };',
  6440. ' this.$assign = function (s) {',
  6441. ' this.i = s.i;',
  6442. ' return this;',
  6443. ' };',
  6444. '});',
  6445. '$impl.aRec = $impl.TMyRecord.$new();',
  6446. '']) );
  6447. end;
  6448. procedure TTestModule.TestRenameJSNameConflict;
  6449. begin
  6450. StartProgram(false);
  6451. Add('var apply: longint;');
  6452. Add('var bind: longint;');
  6453. Add('var call: longint;');
  6454. Add('begin');
  6455. ConvertProgram;
  6456. CheckSource('TestRenameJSNameConflict',
  6457. LinesToStr([ // statements
  6458. 'this.Apply = 0;',
  6459. 'this.Bind = 0;',
  6460. 'this.Call = 0;'
  6461. ]),
  6462. LinesToStr([ // this.$main
  6463. ''
  6464. ]));
  6465. end;
  6466. procedure TTestModule.TestLocalConst;
  6467. begin
  6468. StartProgram(false);
  6469. Add('procedure DoIt;');
  6470. Add('const');
  6471. Add(' cA: longint = 1;');
  6472. Add(' cB = 2;');
  6473. Add(' procedure Sub;');
  6474. Add(' const');
  6475. Add(' csA = 3;');
  6476. Add(' cB: double = 4;');
  6477. Add(' begin');
  6478. Add(' cb:=cb+csa;');
  6479. Add(' ca:=ca+csa+5;');
  6480. Add(' end;');
  6481. Add('begin');
  6482. Add(' ca:=ca+cb+6;');
  6483. Add('end;');
  6484. Add('begin');
  6485. ConvertProgram;
  6486. CheckSource('TestLocalConst',
  6487. LinesToStr([
  6488. 'var cA = 1;',
  6489. 'var cB = 2;',
  6490. 'var csA = 3;',
  6491. 'var cB$1 = 4;',
  6492. 'this.DoIt = function () {',
  6493. ' function Sub() {',
  6494. ' cB$1 = cB$1 + 3;',
  6495. ' cA = cA + 3 + 5;',
  6496. ' };',
  6497. ' cA = cA + 2 + 6;',
  6498. '};'
  6499. ]),
  6500. LinesToStr([
  6501. ]));
  6502. end;
  6503. procedure TTestModule.TestVarExternal;
  6504. begin
  6505. StartProgram(false);
  6506. Add('var');
  6507. Add(' NaN: double; external name ''Global.NaN'';');
  6508. Add(' d: double;');
  6509. Add('begin');
  6510. Add(' d:=NaN;');
  6511. ConvertProgram;
  6512. CheckSource('TestVarExternal',
  6513. LinesToStr([
  6514. 'this.d = 0.0;'
  6515. ]),
  6516. LinesToStr([
  6517. '$mod.d = Global.NaN;'
  6518. ]));
  6519. end;
  6520. procedure TTestModule.TestVarExternalOtherUnit;
  6521. begin
  6522. AddModuleWithIntfImplSrc('unit2.pas',
  6523. LinesToStr([
  6524. 'var NaN: double; external name ''Global.NaN'';',
  6525. 'var iV: longint;'
  6526. ]),
  6527. '');
  6528. StartUnit(true);
  6529. Add('interface');
  6530. Add('uses unit2;');
  6531. Add('implementation');
  6532. Add('var');
  6533. Add(' d: double;');
  6534. Add(' i: longint; external name ''$i'';');
  6535. Add('begin');
  6536. Add(' d:=nan;');
  6537. Add(' d:=uNit2.nan;');
  6538. Add(' d:=test1.d;');
  6539. Add(' i:=iv;');
  6540. Add(' i:=uNit2.iv;');
  6541. Add(' i:=test1.i;');
  6542. ConvertUnit;
  6543. CheckSource('TestVarExternalOtherUnit',
  6544. LinesToStr([
  6545. 'var $impl = $mod.$impl;',
  6546. '']),
  6547. LinesToStr([ // this.$init
  6548. '$impl.d = Global.NaN;',
  6549. '$impl.d = Global.NaN;',
  6550. '$impl.d = $impl.d;',
  6551. '$i = pas.unit2.iV;',
  6552. '$i = pas.unit2.iV;',
  6553. '$i = $i;',
  6554. '']),
  6555. LinesToStr([ // implementation
  6556. '$impl.d = 0.0;',
  6557. '']) );
  6558. end;
  6559. procedure TTestModule.TestVarAbsoluteFail;
  6560. begin
  6561. StartProgram(false);
  6562. Add([
  6563. 'var',
  6564. ' a: longint;',
  6565. ' b: longword absolute a;',
  6566. 'begin']);
  6567. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6568. ConvertProgram;
  6569. end;
  6570. procedure TTestModule.TestConstExternal;
  6571. begin
  6572. StartProgram(false);
  6573. Add([
  6574. 'const',
  6575. ' PI: double; external name ''Global.PI'';',
  6576. ' Tau = 2*pi;',
  6577. 'var d: double;',
  6578. 'begin',
  6579. ' d:=pi;',
  6580. ' d:=tau+pi;']);
  6581. ConvertProgram;
  6582. CheckSource('TestConstExternal',
  6583. LinesToStr([
  6584. 'this.Tau = 2*Global.PI;',
  6585. 'this.d = 0.0;'
  6586. ]),
  6587. LinesToStr([
  6588. '$mod.d = Global.PI;',
  6589. '$mod.d = $mod.Tau + Global.PI;'
  6590. ]));
  6591. end;
  6592. procedure TTestModule.TestDouble;
  6593. begin
  6594. StartProgram(false);
  6595. Add([
  6596. 'type',
  6597. ' TDateTime = double;',
  6598. 'const',
  6599. ' a = TDateTime(2.7);',
  6600. ' b = a + TDateTime(1.7);',
  6601. ' c = 0.9 + 0.1;',
  6602. ' f0_1 = 0.1;',
  6603. ' f0_3 = 0.3;',
  6604. ' fn0_1 = -0.1;',
  6605. ' fn0_3 = -0.3;',
  6606. ' fn0_003 = -0.003;',
  6607. ' fn0_123456789 = -0.123456789;',
  6608. ' fn300_0 = -300.0;',
  6609. ' fn123456_0 = -123456.0;',
  6610. ' fn1234567_8 = -1234567.8;',
  6611. ' fn12345678_9 = -12345678.9;',
  6612. ' f1_0En12 = 1E-12;',
  6613. ' fn1_0En12 = -1E-12;',
  6614. ' maxdouble = 1.7e+308;',
  6615. ' mindouble = -1.7e+308;',
  6616. ' MinSafeIntDouble = -$1fffffffffffff;',
  6617. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6618. ' MaxSafeIntDouble = $1fffffffffffff;',
  6619. ' DZeroResolution = 1E-12;',
  6620. ' Minus1 = -1E-12;',
  6621. ' EPS = 1E-9;',
  6622. ' DELTA = 0.001;',
  6623. ' Big = 129.789E+100;',
  6624. ' Test0_15 = 0.15;',
  6625. ' Test999 = 2.9999999999999;',
  6626. ' Test111999 = 211199999999999000.0;',
  6627. ' TestMinus111999 = -211199999999999000.0;',
  6628. 'var',
  6629. ' d: double = b;',
  6630. 'begin',
  6631. ' d:=1.0;',
  6632. ' d:=1.0/3.0;',
  6633. ' d:=1/3;',
  6634. ' d:=5.0E-324;',
  6635. ' d:=1.7E308;',
  6636. ' d:=001.00E00;',
  6637. ' d:=002.00E001;',
  6638. ' d:=003.000E000;',
  6639. ' d:=-004.00E-00;',
  6640. ' d:=-005.00E-001;',
  6641. ' d:=10**3;',
  6642. ' d:=10 mod 3;',
  6643. ' d:=10 div 3;',
  6644. ' d:=c;',
  6645. ' d:=f0_1;',
  6646. ' d:=f0_3;',
  6647. ' d:=fn0_1;',
  6648. ' d:=fn0_3;',
  6649. ' d:=fn0_003;',
  6650. ' d:=fn0_123456789;',
  6651. ' d:=fn300_0;',
  6652. ' d:=fn123456_0;',
  6653. ' d:=fn1234567_8;',
  6654. ' d:=fn12345678_9;',
  6655. ' d:=f1_0En12;',
  6656. ' d:=fn1_0En12;',
  6657. ' d:=maxdouble;',
  6658. ' d:=mindouble;',
  6659. ' d:=MinSafeIntDouble;',
  6660. ' d:=double(MinSafeIntDouble);',
  6661. ' d:=MinSafeIntDouble2;',
  6662. ' d:=double(MinSafeIntDouble2);',
  6663. ' d:=MaxSafeIntDouble;',
  6664. ' d:=default(double);',
  6665. '']);
  6666. ConvertProgram;
  6667. CheckSource('TestDouble',
  6668. LinesToStr([
  6669. 'this.a = 2.7;',
  6670. 'this.b = 2.7 + 1.7;',
  6671. 'this.c = 0.9 + 0.1;',
  6672. 'this.f0_1 = 0.1;',
  6673. 'this.f0_3 = 0.3;',
  6674. 'this.fn0_1 = -0.1;',
  6675. 'this.fn0_3 = -0.3;',
  6676. 'this.fn0_003 = -0.003;',
  6677. 'this.fn0_123456789 = -0.123456789;',
  6678. 'this.fn300_0 = -300.0;',
  6679. 'this.fn123456_0 = -123456.0;',
  6680. 'this.fn1234567_8 = -1234567.8;',
  6681. 'this.fn12345678_9 = -12345678.9;',
  6682. 'this.f1_0En12 = 1E-12;',
  6683. 'this.fn1_0En12 = -1E-12;',
  6684. 'this.maxdouble = 1.7e+308;',
  6685. 'this.mindouble = -1.7e+308;',
  6686. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6687. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6688. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6689. 'this.DZeroResolution = 1E-12;',
  6690. 'this.Minus1 = -1E-12;',
  6691. 'this.EPS = 1E-9;',
  6692. 'this.DELTA = 0.001;',
  6693. 'this.Big = 129.789E+100;',
  6694. 'this.Test0_15 = 0.15;',
  6695. 'this.Test999 = 2.9999999999999;',
  6696. 'this.Test111999 = 211199999999999000.0;',
  6697. 'this.TestMinus111999 = -211199999999999000.0;',
  6698. 'this.d = 4.4;'
  6699. ]),
  6700. LinesToStr([
  6701. '$mod.d = 1.0;',
  6702. '$mod.d = 1.0 / 3.0;',
  6703. '$mod.d = 1 / 3;',
  6704. '$mod.d = 5.0E-324;',
  6705. '$mod.d = 1.7E308;',
  6706. '$mod.d = 1.00E0;',
  6707. '$mod.d = 2.00E1;',
  6708. '$mod.d = 3.000E0;',
  6709. '$mod.d = -4.00E-0;',
  6710. '$mod.d = -5.00E-1;',
  6711. '$mod.d = Math.pow(10, 3);',
  6712. '$mod.d = 10 % 3;',
  6713. '$mod.d = rtl.trunc(10 / 3);',
  6714. '$mod.d = 1;',
  6715. '$mod.d = 0.1;',
  6716. '$mod.d = 0.3;',
  6717. '$mod.d = -0.1;',
  6718. '$mod.d = -0.3;',
  6719. '$mod.d = -0.003;',
  6720. '$mod.d = -0.123456789;',
  6721. '$mod.d = -300;',
  6722. '$mod.d = -123456;',
  6723. '$mod.d = -1234567.8;',
  6724. '$mod.d = -1.23456789E7;',
  6725. '$mod.d = 1E-12;',
  6726. '$mod.d = -1E-12;',
  6727. '$mod.d = 1.7E308;',
  6728. '$mod.d = -1.7E308;',
  6729. '$mod.d = -9007199254740991;',
  6730. '$mod.d = -9007199254740991;',
  6731. '$mod.d = -9.007199254740992E15;',
  6732. '$mod.d = -9.007199254740992E15;',
  6733. '$mod.d = 9007199254740991;',
  6734. '$mod.d = 0.0;',
  6735. '']));
  6736. end;
  6737. procedure TTestModule.TestInteger;
  6738. begin
  6739. StartProgram(false);
  6740. Add([
  6741. 'const',
  6742. ' MinInt = low(NativeInt);',
  6743. ' MaxInt = high(NativeInt);',
  6744. 'type',
  6745. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6746. 'const',
  6747. ' a = low(TMyInt)+High(TMyInt);',
  6748. 'var',
  6749. ' i: TMyInt;',
  6750. 'begin',
  6751. ' i:=-MinInt;',
  6752. ' i:=default(TMyInt);',
  6753. ' i:=low(i)+high(i);',
  6754. '']);
  6755. ConvertProgram;
  6756. CheckSource('TestIntegerRange',
  6757. LinesToStr([
  6758. 'this.MinInt = -9007199254740991;',
  6759. 'this.MaxInt = 9007199254740991;',
  6760. 'this.a = -9007199254740991 + 9007199254740991;',
  6761. 'this.i = 0;',
  6762. '']),
  6763. LinesToStr([
  6764. '$mod.i = - -9007199254740991;',
  6765. '$mod.i = -9007199254740991;',
  6766. '$mod.i = -9007199254740991 + 9007199254740991;',
  6767. '']));
  6768. end;
  6769. procedure TTestModule.TestIntegerRange;
  6770. begin
  6771. StartProgram(false);
  6772. Add([
  6773. 'const',
  6774. ' MinInt = -1;',
  6775. ' MaxInt = +1;',
  6776. 'type',
  6777. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6778. ' TInt2 = 1..3;',
  6779. 'const',
  6780. ' a = low(TMyInt)+High(TMyInt);',
  6781. ' b = low(TInt2)+High(TInt2);',
  6782. ' s1 = [1];',
  6783. ' s2 = [1,2];',
  6784. ' s3 = [1..3];',
  6785. ' s4 = [low(shortint)..high(shortint)];',
  6786. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6787. ' s6 = 1 in s2;',
  6788. 'var',
  6789. ' i: TMyInt;',
  6790. ' i2: TInt2;',
  6791. 'begin',
  6792. ' i:=i2;',
  6793. ' i:=default(TMyInt);',
  6794. ' if i=i2 then ;']);
  6795. ConvertProgram;
  6796. CheckSource('TestIntegerRange',
  6797. LinesToStr([
  6798. 'this.MinInt = -1;',
  6799. 'this.MaxInt = +1;',
  6800. 'this.a = -1 + 1;',
  6801. 'this.b = 1 + 3;',
  6802. 'this.s1 = rtl.createSet(1);',
  6803. 'this.s2 = rtl.createSet(1, 2);',
  6804. 'this.s3 = rtl.createSet(null, 1, 3);',
  6805. 'this.s4 = rtl.createSet(null, -128, 127);',
  6806. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6807. 'this.s6 = 1 in this.s2;',
  6808. 'this.i = 0;',
  6809. 'this.i2 = 0;',
  6810. '']),
  6811. LinesToStr([
  6812. '$mod.i = $mod.i2;',
  6813. '$mod.i = -1;',
  6814. 'if ($mod.i === $mod.i2) ;',
  6815. '']));
  6816. end;
  6817. procedure TTestModule.TestIntegerTypecasts;
  6818. begin
  6819. StartProgram(false);
  6820. Add([
  6821. 'var',
  6822. ' i: nativeint;',
  6823. ' b: byte;',
  6824. ' sh: shortint;',
  6825. ' w: word;',
  6826. ' sm: smallint;',
  6827. ' lw: longword;',
  6828. ' li: longint;',
  6829. 'begin',
  6830. ' b:=byte(i);',
  6831. ' sh:=shortint(i);',
  6832. ' w:=word(i);',
  6833. ' sm:=smallint(i);',
  6834. ' lw:=longword(i);',
  6835. ' li:=longint(i);',
  6836. '']);
  6837. ConvertProgram;
  6838. CheckSource('TestIntegerTypecasts',
  6839. LinesToStr([
  6840. 'this.i = 0;',
  6841. 'this.b = 0;',
  6842. 'this.sh = 0;',
  6843. 'this.w = 0;',
  6844. 'this.sm = 0;',
  6845. 'this.lw = 0;',
  6846. 'this.li = 0;',
  6847. '']),
  6848. LinesToStr([
  6849. '$mod.b = $mod.i & 255;',
  6850. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6851. '$mod.w = $mod.i & 65535;',
  6852. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6853. '$mod.lw = $mod.i >>> 0;',
  6854. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6855. '']));
  6856. end;
  6857. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6858. begin
  6859. StartProgram(false);
  6860. Add([
  6861. 'var',
  6862. ' i,j: nativeint;',
  6863. 'begin',
  6864. ' i:=i shr 0;',
  6865. ' i:=i shr 1;',
  6866. ' i:=i shr 3;',
  6867. ' i:=i shr 54;',
  6868. ' i:=j shr i;',
  6869. '']);
  6870. ConvertProgram;
  6871. CheckResolverUnexpectedHints;
  6872. CheckSource('TestInteger_BitwiseShrNativeInt',
  6873. LinesToStr([
  6874. 'this.i = 0;',
  6875. 'this.j = 0;',
  6876. '']),
  6877. LinesToStr([
  6878. '$mod.i = $mod.i;',
  6879. '$mod.i = Math.floor($mod.i / 2);',
  6880. '$mod.i = Math.floor($mod.i / 8);',
  6881. '$mod.i = 0;',
  6882. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6883. '']));
  6884. end;
  6885. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6886. begin
  6887. StartProgram(false);
  6888. Add([
  6889. 'var',
  6890. ' i: nativeint;',
  6891. 'begin',
  6892. ' i:=i shl 0;',
  6893. ' i:=i shl 54;',
  6894. ' i:=123456789012 shl 1;',
  6895. ' i:=i shl 1;',
  6896. '']);
  6897. ConvertProgram;
  6898. CheckResolverUnexpectedHints;
  6899. CheckSource('TestInteger_BitwiseShrNativeInt',
  6900. LinesToStr([
  6901. 'this.i = 0;',
  6902. '']),
  6903. LinesToStr([
  6904. '$mod.i = $mod.i;',
  6905. '$mod.i = 0;',
  6906. '$mod.i = 246913578024;',
  6907. '$mod.i = rtl.shl($mod.i, 1);',
  6908. '']));
  6909. end;
  6910. procedure TTestModule.TestInteger_SystemFunc;
  6911. begin
  6912. StartProgram(true);
  6913. Add([
  6914. 'var',
  6915. ' i: byte;',
  6916. ' s: string;',
  6917. 'begin',
  6918. ' system.inc(i);',
  6919. ' system.str(i,s);',
  6920. ' s:=system.str(i);',
  6921. ' i:=system.low(i);',
  6922. ' i:=system.high(i);',
  6923. ' i:=system.pred(i);',
  6924. ' i:=system.succ(i);',
  6925. '']);
  6926. ConvertProgram;
  6927. CheckResolverUnexpectedHints;
  6928. CheckSource('TestInteger_SystemFunc',
  6929. LinesToStr([
  6930. 'this.i = 0;',
  6931. 'this.s = "";',
  6932. '']),
  6933. LinesToStr([
  6934. '$mod.i += 1;',
  6935. '$mod.s = "" + $mod.i;',
  6936. '$mod.s = "" + $mod.i;',
  6937. '$mod.i = 0;',
  6938. '$mod.i = 255;',
  6939. '$mod.i = $mod.i - 1;',
  6940. '$mod.i = $mod.i + 1;',
  6941. '']));
  6942. end;
  6943. procedure TTestModule.TestCurrency;
  6944. begin
  6945. StartProgram(false);
  6946. Add([
  6947. 'type',
  6948. ' TCoin = currency;',
  6949. 'const',
  6950. ' a = TCoin(2.7);',
  6951. ' b = a + TCoin(1.7);',
  6952. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6953. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6954. 'var',
  6955. ' c: TCoin = b;',
  6956. ' i: nativeint;',
  6957. ' d: double;',
  6958. ' j: jsvalue;',
  6959. 'function DoIt(c: currency): currency; begin end;',
  6960. 'function GetIt(d: double): double; begin end;',
  6961. 'procedure Write(v: jsvalue); begin end;',
  6962. 'begin',
  6963. ' c:=1.0;',
  6964. ' c:=0.1;',
  6965. ' c:=1.0/3.0;',
  6966. ' c:=1/3;',
  6967. ' c:=a;',
  6968. ' d:=c;',
  6969. ' c:=d;',
  6970. ' c:=currency(c);',
  6971. ' c:=currency(d);',
  6972. ' d:=double(c);',
  6973. ' c:=i;',
  6974. ' c:=currency(i);',
  6975. //' i:=c;', not allowed
  6976. ' i:=nativeint(c);',
  6977. ' c:=c+a;',
  6978. ' c:=-c-a;',
  6979. ' c:=d+c;',
  6980. ' c:=c+d;',
  6981. ' c:=d-c;',
  6982. ' c:=c-d;',
  6983. ' c:=c*a;',
  6984. ' c:=a*c;',
  6985. ' c:=d*c;',
  6986. ' c:=c*d;',
  6987. ' c:=c/a;',
  6988. ' c:=a/c;',
  6989. ' c:=d/c;',
  6990. ' c:=c/d;',
  6991. ' c:=c**a;',
  6992. ' c:=a**c;',
  6993. ' c:=d**c;',
  6994. ' c:=c**d;',
  6995. ' if c=c then ;',
  6996. ' if c=a then ;',
  6997. ' if a=c then ;',
  6998. ' if d=c then ;',
  6999. ' if c=d then ;',
  7000. ' c:=DoIt(c);',
  7001. ' c:=DoIt(i);',
  7002. ' c:=DoIt(d);',
  7003. ' c:=GetIt(c);',
  7004. ' j:=c;',
  7005. ' Write(c);',
  7006. ' c:=default(currency);',
  7007. ' j:=str(c);',
  7008. ' j:=str(c:0:3);',
  7009. '']);
  7010. ConvertProgram;
  7011. CheckSource('TestCurrency',
  7012. LinesToStr([
  7013. 'this.a = 27000;',
  7014. 'this.b = this.a + 17000;',
  7015. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7016. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7017. 'this.c = this.b;',
  7018. 'this.i = 0;',
  7019. 'this.d = 0.0;',
  7020. 'this.j = undefined;',
  7021. 'this.DoIt = function (c) {',
  7022. ' var Result = 0;',
  7023. ' return Result;',
  7024. '};',
  7025. 'this.GetIt = function (d) {',
  7026. ' var Result = 0.0;',
  7027. ' return Result;',
  7028. '};',
  7029. 'this.Write = function (v) {',
  7030. '};',
  7031. '']),
  7032. LinesToStr([
  7033. '$mod.c = 10000;',
  7034. '$mod.c = 1000;',
  7035. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7036. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7037. '$mod.c = $mod.a;',
  7038. '$mod.d = $mod.c / 10000;',
  7039. '$mod.c = rtl.trunc($mod.d * 10000);',
  7040. '$mod.c = $mod.c;',
  7041. '$mod.c = $mod.d * 10000;',
  7042. '$mod.d = $mod.c / 10000;',
  7043. '$mod.c = $mod.i * 10000;',
  7044. '$mod.c = $mod.i * 10000;',
  7045. '$mod.i = rtl.trunc($mod.c / 10000);',
  7046. '$mod.c = $mod.c + $mod.a;',
  7047. '$mod.c = -$mod.c - $mod.a;',
  7048. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7049. '$mod.c = $mod.c + ($mod.d * 10000);',
  7050. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7051. '$mod.c = $mod.c - ($mod.d * 10000);',
  7052. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7053. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7054. '$mod.c = $mod.d * $mod.c;',
  7055. '$mod.c = $mod.c * $mod.d;',
  7056. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7057. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7058. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7059. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7060. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7061. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7062. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7063. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7064. 'if ($mod.c === $mod.c) ;',
  7065. 'if ($mod.c === $mod.a) ;',
  7066. 'if ($mod.a === $mod.c) ;',
  7067. 'if (($mod.d * 10000) === $mod.c) ;',
  7068. 'if ($mod.c === ($mod.d * 10000)) ;',
  7069. '$mod.c = $mod.DoIt($mod.c);',
  7070. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7071. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7072. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7073. '$mod.j = $mod.c / 10000;',
  7074. '$mod.Write($mod.c / 10000);',
  7075. '$mod.c = 0;',
  7076. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7077. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7078. '']));
  7079. end;
  7080. procedure TTestModule.TestForBoolDo;
  7081. begin
  7082. StartProgram(false);
  7083. Add([
  7084. 'var b: boolean;',
  7085. 'begin',
  7086. ' for b:=false to true do ;',
  7087. ' for b:=b downto false do ;',
  7088. ' for b in boolean do ;',
  7089. '']);
  7090. ConvertProgram;
  7091. CheckSource('TestForBoolDo',
  7092. LinesToStr([ // statements
  7093. 'this.b = false;']),
  7094. LinesToStr([ // this.$main
  7095. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7096. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7097. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7098. '']));
  7099. end;
  7100. procedure TTestModule.TestForIntDo;
  7101. begin
  7102. StartProgram(false);
  7103. Add([
  7104. 'var i: longint;',
  7105. 'begin',
  7106. ' for i:=3 to 5 do ;',
  7107. ' for i:=i downto 2 do ;',
  7108. ' for i in byte do ;',
  7109. '']);
  7110. ConvertProgram;
  7111. CheckSource('TestForIntDo',
  7112. LinesToStr([ // statements
  7113. 'this.i = 0;']),
  7114. LinesToStr([ // this.$main
  7115. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7116. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7117. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7118. '']));
  7119. end;
  7120. procedure TTestModule.TestForIntInDo;
  7121. begin
  7122. StartProgram(false);
  7123. Add([
  7124. 'type',
  7125. ' TSetOfInt = set of byte;',
  7126. ' TIntRg = 3..7;',
  7127. ' TSetOfIntRg = set of TIntRg;',
  7128. 'var',
  7129. ' i,i2: longint;',
  7130. ' a1: array of byte;',
  7131. ' a2: array[1..3] of byte;',
  7132. ' soi: TSetOfInt;',
  7133. ' soir: TSetOfIntRg;',
  7134. ' ir: TIntRg;',
  7135. 'begin',
  7136. ' for i in byte do ;',
  7137. ' for i in a1 do ;',
  7138. ' for i in a2 do ;',
  7139. ' for i in [11..13] do ;',
  7140. ' for i in TSetOfInt do ;',
  7141. ' for i in TIntRg do ;',
  7142. ' for i in soi do i2:=i;',
  7143. ' for i in TSetOfIntRg do ;',
  7144. ' for i in soir do ;',
  7145. ' for ir in TIntRg do ;',
  7146. ' for ir in TSetOfIntRg do ;',
  7147. ' for ir in soir do ;',
  7148. '']);
  7149. ConvertProgram;
  7150. CheckSource('TestForIntInDo',
  7151. LinesToStr([ // statements
  7152. 'this.i = 0;',
  7153. 'this.i2 = 0;',
  7154. 'this.a1 = [];',
  7155. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7156. 'this.soi = {};',
  7157. 'this.soir = {};',
  7158. 'this.ir = 0;',
  7159. '']),
  7160. LinesToStr([ // this.$main
  7161. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7162. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7163. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7164. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7165. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7166. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7167. 'for (var $l6 in $mod.soi) {',
  7168. ' $mod.i = +$l6;',
  7169. ' $mod.i2 = $mod.i;',
  7170. '};',
  7171. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7172. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7173. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7174. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7175. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7176. '']));
  7177. end;
  7178. procedure TTestModule.TestCharConst;
  7179. begin
  7180. StartProgram(false);
  7181. Add([
  7182. 'const',
  7183. ' a = #$00F3;',
  7184. ' c: char = ''1'';',
  7185. ' wc: widechar = ''ä'';',
  7186. 'begin',
  7187. ' c:=#0;',
  7188. ' c:=#1;',
  7189. ' c:=#9;',
  7190. ' c:=#10;',
  7191. ' c:=#13;',
  7192. ' c:=#31;',
  7193. ' c:=#32;',
  7194. ' c:=#$A;',
  7195. ' c:=#$0A;',
  7196. ' c:=#$b;',
  7197. ' c:=#$0b;',
  7198. ' c:=^A;',
  7199. ' c:=''"'';',
  7200. ' c:=default(char);',
  7201. ' c:=#$00E4;', // ä
  7202. ' c:=''ä'';',
  7203. ' c:=#$E4;', // ä
  7204. ' c:=#$D800;', // invalid UTF-16
  7205. ' c:=#$DFFF;', // invalid UTF-16
  7206. ' c:=#$FFFF;', // last UCS-2
  7207. ' c:=high(c);', // last UCS-2
  7208. ' c:=#269;',
  7209. '']);
  7210. ConvertProgram;
  7211. CheckSource('TestCharConst',
  7212. LinesToStr([
  7213. 'this.a="ó";',
  7214. 'this.c="1";',
  7215. 'this.wc="ä";'
  7216. ]),
  7217. LinesToStr([
  7218. '$mod.c="\x00";',
  7219. '$mod.c="\x01";',
  7220. '$mod.c="\t";',
  7221. '$mod.c="\n";',
  7222. '$mod.c="\r";',
  7223. '$mod.c="\x1F";',
  7224. '$mod.c=" ";',
  7225. '$mod.c="\n";',
  7226. '$mod.c="\n";',
  7227. '$mod.c="\x0B";',
  7228. '$mod.c="\x0B";',
  7229. '$mod.c="\x01";',
  7230. '$mod.c=''"'';',
  7231. '$mod.c="\x00";',
  7232. '$mod.c = "ä";',
  7233. '$mod.c = "ä";',
  7234. '$mod.c = "ä";',
  7235. '$mod.c="\uD800";',
  7236. '$mod.c="\uDFFF";',
  7237. '$mod.c="\uFFFF";',
  7238. '$mod.c="\uFFFF";',
  7239. '$mod.c = "č";',
  7240. '']));
  7241. end;
  7242. procedure TTestModule.TestChar_Compare;
  7243. begin
  7244. StartProgram(false);
  7245. Add('var');
  7246. Add(' c: char;');
  7247. Add(' b: boolean;');
  7248. Add('begin');
  7249. Add(' b:=c=''1'';');
  7250. Add(' b:=''2''=c;');
  7251. Add(' b:=''3''=''4'';');
  7252. Add(' b:=c<>''5'';');
  7253. Add(' b:=''6''<>c;');
  7254. Add(' b:=c>''7'';');
  7255. Add(' b:=''8''>c;');
  7256. Add(' b:=c>=''9'';');
  7257. Add(' b:=''A''>=c;');
  7258. Add(' b:=c<''B'';');
  7259. Add(' b:=''C''<c;');
  7260. Add(' b:=c<=''D'';');
  7261. Add(' b:=''E''<=c;');
  7262. ConvertProgram;
  7263. CheckSource('TestChar_Compare',
  7264. LinesToStr([
  7265. 'this.c="";',
  7266. 'this.b = false;'
  7267. ]),
  7268. LinesToStr([
  7269. '$mod.b = $mod.c === "1";',
  7270. '$mod.b = "2" === $mod.c;',
  7271. '$mod.b = "3" === "4";',
  7272. '$mod.b = $mod.c !== "5";',
  7273. '$mod.b = "6" !== $mod.c;',
  7274. '$mod.b = $mod.c > "7";',
  7275. '$mod.b = "8" > $mod.c;',
  7276. '$mod.b = $mod.c >= "9";',
  7277. '$mod.b = "A" >= $mod.c;',
  7278. '$mod.b = $mod.c < "B";',
  7279. '$mod.b = "C" < $mod.c;',
  7280. '$mod.b = $mod.c <= "D";',
  7281. '$mod.b = "E" <= $mod.c;',
  7282. '']));
  7283. end;
  7284. procedure TTestModule.TestChar_BuiltInProcs;
  7285. begin
  7286. StartProgram(false);
  7287. Add([
  7288. 'var',
  7289. ' c: char;',
  7290. ' i: longint;',
  7291. ' s: string;',
  7292. 'begin',
  7293. ' i:=ord(c);',
  7294. ' i:=ord(s[i]);',
  7295. ' c:=chr(i);',
  7296. ' c:=pred(c);',
  7297. ' c:=succ(c);',
  7298. ' c:=low(c);',
  7299. ' c:=high(c);',
  7300. ' i:=byte(c);',
  7301. ' i:=word(c);',
  7302. ' i:=longint(c);',
  7303. '']);
  7304. ConvertProgram;
  7305. CheckSource('TestChar_BuiltInProcs',
  7306. LinesToStr([
  7307. 'this.c = "";',
  7308. 'this.i = 0;',
  7309. 'this.s = "";'
  7310. ]),
  7311. LinesToStr([
  7312. '$mod.i = $mod.c.charCodeAt();',
  7313. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7314. '$mod.c = String.fromCharCode($mod.i);',
  7315. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7316. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7317. '$mod.c = "\x00";',
  7318. '$mod.c = "\uFFFF";',
  7319. '$mod.i = $mod.c.charCodeAt() & 255;',
  7320. '$mod.i = $mod.c.charCodeAt();',
  7321. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7322. '']));
  7323. end;
  7324. procedure TTestModule.TestStringConst;
  7325. begin
  7326. StartProgram(false);
  7327. Add([
  7328. '{$H+}',
  7329. 'const',
  7330. ' a = #$00F3#$017C;', // first <256, then >=256
  7331. ' b = string(''a'');',
  7332. ' c = string(''ä'');',
  7333. ' d = UnicodeString(''b'');',
  7334. ' e = UnicodeString(''ö'');',
  7335. 'var',
  7336. ' s: string = ''abc'';',
  7337. 'begin',
  7338. ' s:='''';',
  7339. ' s:=#13#10;',
  7340. ' s:=#9''foo'';',
  7341. ' s:=#$A9;',
  7342. ' s:=''foo''#13''bar'';',
  7343. ' s:=''"'';',
  7344. ' s:=''"''''"'';',
  7345. ' s:=#$20AC;', // euro
  7346. ' s:=#$10437;', // outside BMP
  7347. ' s:=''abc''#$20AC;', // ascii,#
  7348. ' s:=''ä''#$20AC;', // non ascii,#
  7349. ' s:=#$20AC''abc'';', // #, ascii
  7350. ' s:=#$20AC''ä'';', // #, non ascii
  7351. ' s:=default(string);',
  7352. ' s:=concat(s);',
  7353. ' s:=concat(s,''a'',s);',
  7354. ' s:=#250#269;',
  7355. //' s:=#$2F804;',
  7356. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7357. '']);
  7358. ConvertProgram;
  7359. CheckSource('TestStringConst',
  7360. LinesToStr([
  7361. 'this.a = "óż";',
  7362. 'this.b = "a";',
  7363. 'this.c = "ä";',
  7364. 'this.d = "b";',
  7365. 'this.e = "ö";',
  7366. 'this.s="abc";',
  7367. '']),
  7368. LinesToStr([
  7369. '$mod.s="";',
  7370. '$mod.s="\r\n";',
  7371. '$mod.s="\tfoo";',
  7372. '$mod.s="©";',
  7373. '$mod.s="foo\rbar";',
  7374. '$mod.s=''"'';',
  7375. '$mod.s=''"\''"'';',
  7376. '$mod.s="€";',
  7377. '$mod.s="'#$F0#$90#$90#$B7'";',
  7378. '$mod.s = "abc€";',
  7379. '$mod.s = "ä€";',
  7380. '$mod.s = "€abc";',
  7381. '$mod.s = "ۊ";',
  7382. '$mod.s="";',
  7383. '$mod.s = $mod.s;',
  7384. '$mod.s = $mod.s.concat("a", $mod.s);',
  7385. '$mod.s = "úč";',
  7386. '']));
  7387. end;
  7388. procedure TTestModule.TestStringConst_InvalidUTF16;
  7389. begin
  7390. StartProgram(false);
  7391. Add([
  7392. 'const',
  7393. ' a: char = #$D87E;',
  7394. ' b: string = #$D87E;',
  7395. ' c: string = #$D87E#43;',
  7396. 'begin',
  7397. ' c:=''abc''#$D87E;',
  7398. ' c:=#0#1#2;',
  7399. ' c:=#127;',
  7400. ' c:=#128;',
  7401. ' c:=#255;',
  7402. ' c:=#256;',
  7403. '']);
  7404. ConvertProgram;
  7405. CheckSource('TestStringConst',
  7406. LinesToStr([
  7407. 'this.a = "\uD87E";',
  7408. 'this.b = "\uD87E";',
  7409. 'this.c = "\uD87E+";',
  7410. '']),
  7411. LinesToStr([
  7412. '$mod.c = "abc\uD87E";',
  7413. '$mod.c = "\x00\x01\x02";',
  7414. '$mod.c = "'#127'";',
  7415. '$mod.c = "'#$c2#$80'";',
  7416. '$mod.c = "'#$c3#$BF'";',
  7417. '$mod.c = "'#$c4#$80'";',
  7418. '']));
  7419. end;
  7420. procedure TTestModule.TestStringConstSurrogate;
  7421. begin
  7422. StartProgram(false);
  7423. Add([
  7424. 'var',
  7425. ' s: string;',
  7426. 'begin',
  7427. ' s:=''😊'';', // 1F60A
  7428. '']);
  7429. ConvertProgram;
  7430. CheckSource('TestStringConstSurrogate',
  7431. LinesToStr([
  7432. 'this.s="";'
  7433. ]),
  7434. LinesToStr([
  7435. '$mod.s="😊";'
  7436. ]));
  7437. end;
  7438. procedure TTestModule.TestString_Length;
  7439. begin
  7440. StartProgram(false);
  7441. Add('const c = ''foo'';');
  7442. Add('var');
  7443. Add(' s: string;');
  7444. Add(' i: longint;');
  7445. Add('begin');
  7446. Add(' i:=length(s);');
  7447. Add(' i:=length(s+s);');
  7448. Add(' i:=length(''abc'');');
  7449. Add(' i:=length(c);');
  7450. ConvertProgram;
  7451. CheckSource('TestString_Length',
  7452. LinesToStr([
  7453. 'this.c = "foo";',
  7454. 'this.s = "";',
  7455. 'this.i = 0;',
  7456. '']),
  7457. LinesToStr([
  7458. '$mod.i = $mod.s.length;',
  7459. '$mod.i = ($mod.s+$mod.s).length;',
  7460. '$mod.i = "abc".length;',
  7461. '$mod.i = $mod.c.length;',
  7462. '']));
  7463. end;
  7464. procedure TTestModule.TestString_Compare;
  7465. begin
  7466. StartProgram(false);
  7467. Add('var');
  7468. Add(' s, t: string;');
  7469. Add(' b: boolean;');
  7470. Add('begin');
  7471. Add(' b:=s=t;');
  7472. Add(' b:=s<>t;');
  7473. Add(' b:=s>t;');
  7474. Add(' b:=s>=t;');
  7475. Add(' b:=s<t;');
  7476. Add(' b:=s<=t;');
  7477. ConvertProgram;
  7478. CheckSource('TestString_Compare',
  7479. LinesToStr([ // statements
  7480. 'this.s = "";',
  7481. 'this.t = "";',
  7482. 'this.b =false;'
  7483. ]),
  7484. LinesToStr([ // this.$main
  7485. '$mod.b = $mod.s === $mod.t;',
  7486. '$mod.b = $mod.s !== $mod.t;',
  7487. '$mod.b = $mod.s > $mod.t;',
  7488. '$mod.b = $mod.s >= $mod.t;',
  7489. '$mod.b = $mod.s < $mod.t;',
  7490. '$mod.b = $mod.s <= $mod.t;',
  7491. '']));
  7492. end;
  7493. procedure TTestModule.TestString_SetLength;
  7494. begin
  7495. StartProgram(false);
  7496. Add([
  7497. 'procedure DoIt(var s: string);',
  7498. 'begin',
  7499. ' SetLength(s,2);',
  7500. 'end;',
  7501. 'var s: string;',
  7502. 'begin',
  7503. ' SetLength(s,3);',
  7504. '']);
  7505. ConvertProgram;
  7506. CheckSource('TestString_SetLength',
  7507. LinesToStr([ // statements
  7508. 'this.DoIt = function (s) {',
  7509. ' s.set(rtl.strSetLength(s.get(), 2));',
  7510. '};',
  7511. 'this.s = "";',
  7512. '']),
  7513. LinesToStr([ // this.$main
  7514. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7515. ]));
  7516. end;
  7517. procedure TTestModule.TestString_CharAt;
  7518. begin
  7519. StartProgram(false);
  7520. Add([
  7521. 'var',
  7522. ' s: string;',
  7523. ' c: char;',
  7524. ' b: boolean;',
  7525. 'begin',
  7526. ' b:= s[1] = c;',
  7527. ' b:= c = s[1];',
  7528. ' b:= c <> s[1];',
  7529. ' b:= c > s[1];',
  7530. ' b:= c >= s[1];',
  7531. ' b:= c < s[2];',
  7532. ' b:= c <= s[1];',
  7533. ' s[1] := c;',
  7534. ' s[2+3] := c;']);
  7535. ConvertProgram;
  7536. CheckSource('TestString_CharAt',
  7537. LinesToStr([ // statements
  7538. 'this.s = "";',
  7539. 'this.c = "";',
  7540. 'this.b = false;'
  7541. ]),
  7542. LinesToStr([ // this.$main
  7543. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7544. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7545. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7546. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7547. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7548. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7549. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7550. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7551. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7552. '']));
  7553. end;
  7554. procedure TTestModule.TestStringHMinusFail;
  7555. begin
  7556. StartProgram(false);
  7557. Add([
  7558. '{$H-}',
  7559. 'var s: string;',
  7560. 'begin']);
  7561. ConvertProgram;
  7562. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7563. end;
  7564. procedure TTestModule.TestStr;
  7565. begin
  7566. StartProgram(false);
  7567. Add('var');
  7568. Add(' b: boolean;');
  7569. Add(' i: longint;');
  7570. Add(' d: double;');
  7571. Add(' s: string;');
  7572. Add('begin');
  7573. Add(' str(b,s);');
  7574. Add(' str(i,s);');
  7575. Add(' str(d,s);');
  7576. Add(' str(i:3,s);');
  7577. Add(' str(d:3:2,s);');
  7578. Add(' Str(12.456:12:1,s);');
  7579. Add(' Str(12.456:12,s);');
  7580. Add(' s:=str(b);');
  7581. Add(' s:=str(i);');
  7582. Add(' s:=str(d);');
  7583. Add(' s:=str(i,i);');
  7584. Add(' s:=str(i:3);');
  7585. Add(' s:=str(d:3:2);');
  7586. Add(' s:=str(i:4,i);');
  7587. Add(' s:=str(i,i:5);');
  7588. Add(' s:=str(i:4,i:5);');
  7589. Add(' s:=str(s,s);');
  7590. Add(' s:=str(s,''foo'');');
  7591. ConvertProgram;
  7592. CheckSource('TestStr',
  7593. LinesToStr([ // statements
  7594. 'this.b = false;',
  7595. 'this.i = 0;',
  7596. 'this.d = 0.0;',
  7597. 'this.s = "";',
  7598. '']),
  7599. LinesToStr([ // this.$main
  7600. '$mod.s = ""+$mod.b;',
  7601. '$mod.s = ""+$mod.i;',
  7602. '$mod.s = rtl.floatToStr($mod.d);',
  7603. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7604. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7605. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7606. '$mod.s = rtl.floatToStr(12.456,12);',
  7607. '$mod.s = ""+$mod.b;',
  7608. '$mod.s = ""+$mod.i;',
  7609. '$mod.s = rtl.floatToStr($mod.d);',
  7610. '$mod.s = ""+$mod.i+$mod.i;',
  7611. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7612. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7613. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7614. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7615. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7616. '$mod.s = $mod.s + $mod.s;',
  7617. '$mod.s = $mod.s + "foo";',
  7618. '']));
  7619. end;
  7620. procedure TTestModule.TestBaseType_AnsiStringFail;
  7621. begin
  7622. StartProgram(false);
  7623. Add('var s: AnsiString');
  7624. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7625. ConvertProgram;
  7626. end;
  7627. procedure TTestModule.TestBaseType_WideStringFail;
  7628. begin
  7629. StartProgram(false);
  7630. Add('var s: WideString');
  7631. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7632. ConvertProgram;
  7633. end;
  7634. procedure TTestModule.TestBaseType_ShortStringFail;
  7635. begin
  7636. StartProgram(false);
  7637. Add('var s: ShortString');
  7638. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7639. ConvertProgram;
  7640. end;
  7641. procedure TTestModule.TestBaseType_RawByteStringFail;
  7642. begin
  7643. StartProgram(false);
  7644. Add('var s: RawByteString');
  7645. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7646. ConvertProgram;
  7647. end;
  7648. procedure TTestModule.TestTypeShortstring_Fail;
  7649. begin
  7650. StartProgram(false);
  7651. Add('type t = string[12];');
  7652. Add('var s: t;');
  7653. Add('begin');
  7654. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7655. ConvertProgram;
  7656. end;
  7657. procedure TTestModule.TestCharSet_Custom;
  7658. begin
  7659. StartProgram(false);
  7660. Add([
  7661. 'type',
  7662. ' TCharRg = ''a''..''z'';',
  7663. ' TSetOfCharRg = set of TCharRg;',
  7664. ' TCharRg2 = ''m''..''p'';',
  7665. 'const',
  7666. ' crg: TCharRg = ''b'';',
  7667. 'var',
  7668. ' c: char;',
  7669. ' crg2: TCharRg2;',
  7670. ' s: TSetOfCharRg;',
  7671. 'begin',
  7672. ' c:=crg;',
  7673. ' crg:=c;',
  7674. ' crg2:=crg;',
  7675. ' if c=crg then ;',
  7676. ' if crg=c then ;',
  7677. ' if crg=crg2 then ;',
  7678. ' if c in s then ;',
  7679. ' if crg2 in s then ;',
  7680. ' c:=default(TCharRg);',
  7681. '']);
  7682. ConvertProgram;
  7683. CheckSource('TestCharSet_Custom',
  7684. LinesToStr([ // statements
  7685. 'this.crg = "b";',
  7686. 'this.c = "";',
  7687. 'this.crg2 = "m";',
  7688. 'this.s = {};',
  7689. '']),
  7690. LinesToStr([ // this.$main
  7691. '$mod.c = $mod.crg;',
  7692. '$mod.crg = $mod.c;',
  7693. '$mod.crg2 = $mod.crg;',
  7694. 'if ($mod.c === $mod.crg) ;',
  7695. 'if ($mod.crg === $mod.c) ;',
  7696. 'if ($mod.crg === $mod.crg2) ;',
  7697. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7698. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7699. '$mod.c = "a";',
  7700. '']));
  7701. end;
  7702. procedure TTestModule.TestWideChar;
  7703. begin
  7704. StartProgram(false);
  7705. Add([
  7706. 'procedure Fly(var c: char);',
  7707. 'begin',
  7708. 'end;',
  7709. 'procedure Run(var c: widechar);',
  7710. 'begin',
  7711. 'end;',
  7712. 'var',
  7713. ' c: char;',
  7714. ' wc: widechar;',
  7715. ' w: word;',
  7716. 'begin',
  7717. ' Fly(wc);',
  7718. ' Run(c);',
  7719. ' wc:=WideChar(w);',
  7720. ' w:=ord(wc);',
  7721. '']);
  7722. ConvertProgram;
  7723. CheckSource('TestWideChar_VarArg',
  7724. LinesToStr([ // statements
  7725. 'this.Fly = function (c) {',
  7726. '};',
  7727. 'this.Run = function (c) {',
  7728. '};',
  7729. 'this.c = "";',
  7730. 'this.wc = "";',
  7731. 'this.w = 0;',
  7732. '']),
  7733. LinesToStr([ // this.$main
  7734. '$mod.Fly({',
  7735. ' p: $mod,',
  7736. ' get: function () {',
  7737. ' return this.p.wc;',
  7738. ' },',
  7739. ' set: function (v) {',
  7740. ' this.p.wc = v;',
  7741. ' }',
  7742. '});',
  7743. '$mod.Run({',
  7744. ' p: $mod,',
  7745. ' get: function () {',
  7746. ' return this.p.c;',
  7747. ' },',
  7748. ' set: function (v) {',
  7749. ' this.p.c = v;',
  7750. ' }',
  7751. '});',
  7752. '$mod.wc = String.fromCharCode($mod.w);',
  7753. '$mod.w = $mod.wc.charCodeAt();',
  7754. '',
  7755. '']));
  7756. end;
  7757. procedure TTestModule.TestForCharDo;
  7758. begin
  7759. StartProgram(false);
  7760. Add([
  7761. 'var c: char;',
  7762. 'begin',
  7763. ' for c:=''a'' to ''c'' do ;',
  7764. ' for c:=c downto ''a'' do ;',
  7765. ' for c:=''Б'' to ''Я'' do ;',
  7766. '']);
  7767. ConvertProgram;
  7768. CheckSource('TestForCharDo',
  7769. LinesToStr([ // statements
  7770. 'this.c = "";']),
  7771. LinesToStr([ // this.$main
  7772. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7773. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7774. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7775. '']));
  7776. end;
  7777. procedure TTestModule.TestForCharInDo;
  7778. begin
  7779. StartProgram(false);
  7780. Add([
  7781. 'type',
  7782. ' TSetOfChar = set of char;',
  7783. ' TCharRg = ''a''..''z'';',
  7784. ' TSetOfCharRg = set of TCharRg;',
  7785. 'const Foo = ''foo'';',
  7786. 'var',
  7787. ' c,c2: char;',
  7788. ' s: string;',
  7789. ' a1: array of char;',
  7790. ' a2: array[1..3] of char;',
  7791. ' soc: TSetOfChar;',
  7792. ' socr: TSetOfCharRg;',
  7793. ' cr: TCharRg;',
  7794. 'begin',
  7795. ' for c in foo do ;',
  7796. ' for c in s do ;',
  7797. ' for c in char do ;',
  7798. ' for c in a1 do ;',
  7799. ' for c in a2 do ;',
  7800. ' for c in [''1''..''3''] do ;',
  7801. ' for c in TSetOfChar do ;',
  7802. ' for c in TCharRg do ;',
  7803. ' for c in soc do c2:=c;',
  7804. ' for c in TSetOfCharRg do ;',
  7805. ' for c in socr do ;',
  7806. ' for cr in TCharRg do ;',
  7807. ' for cr in TSetOfCharRg do ;',
  7808. ' for cr in socr do ;',
  7809. '']);
  7810. ConvertProgram;
  7811. CheckSource('TestForCharInDo',
  7812. LinesToStr([ // statements
  7813. 'this.Foo = "foo";',
  7814. 'this.c = "";',
  7815. 'this.c2 = "";',
  7816. 'this.s = "";',
  7817. 'this.a1 = [];',
  7818. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7819. 'this.soc = {};',
  7820. 'this.socr = {};',
  7821. 'this.cr = "a";',
  7822. '']),
  7823. LinesToStr([ // this.$main
  7824. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7825. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7826. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7827. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7828. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7829. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7830. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7831. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7832. 'for (var $l8 in $mod.soc) {',
  7833. ' $mod.c = String.fromCharCode($l8);',
  7834. ' $mod.c2 = $mod.c;',
  7835. '};',
  7836. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7837. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7838. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7839. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7840. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7841. '']));
  7842. end;
  7843. procedure TTestModule.TestProcTwoArgs;
  7844. begin
  7845. StartProgram(false);
  7846. Add('procedure Test(a,b: longint);');
  7847. Add('begin');
  7848. Add('end;');
  7849. Add('begin');
  7850. ConvertProgram;
  7851. CheckSource('TestProcTwoArgs',
  7852. LinesToStr([ // statements
  7853. 'this.Test = function (a,b) {',
  7854. '};'
  7855. ]),
  7856. LinesToStr([ // this.$main
  7857. ''
  7858. ]));
  7859. end;
  7860. procedure TTestModule.TestProc_DefaultValue;
  7861. begin
  7862. StartProgram(false);
  7863. Add('procedure p1(i: longint = 1);');
  7864. Add('begin');
  7865. Add('end;');
  7866. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7867. Add('begin');
  7868. Add('end;');
  7869. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7870. Add('begin');
  7871. Add('end;');
  7872. Add('begin');
  7873. Add(' p1;');
  7874. Add(' p1();');
  7875. Add(' p1(11);');
  7876. Add(' p2;');
  7877. Add(' p2();');
  7878. Add(' p2(12);');
  7879. Add(' p2(13,''b'');');
  7880. Add(' p3();');
  7881. ConvertProgram;
  7882. CheckSource('TestProc_DefaultValue',
  7883. LinesToStr([ // statements
  7884. 'this.p1 = function (i) {',
  7885. '};',
  7886. 'this.p2 = function (i,c) {',
  7887. '};',
  7888. 'this.p3 = function (d,b,s) {',
  7889. '};'
  7890. ]),
  7891. LinesToStr([ // this.$main
  7892. ' $mod.p1(1);',
  7893. ' $mod.p1(1);',
  7894. ' $mod.p1(11);',
  7895. ' $mod.p2(1,"a");',
  7896. ' $mod.p2(1,"a");',
  7897. ' $mod.p2(12,"a");',
  7898. ' $mod.p2(13,"b");',
  7899. ' $mod.p3(1.0,false,"abc");'
  7900. ]));
  7901. end;
  7902. procedure TTestModule.TestFunctionInt;
  7903. begin
  7904. StartProgram(false);
  7905. Add('function MyTest(Bar: longint): longint;');
  7906. Add('begin');
  7907. Add(' Result:=2*bar');
  7908. Add('end;');
  7909. Add('begin');
  7910. ConvertProgram;
  7911. CheckSource('TestFunctionInt',
  7912. LinesToStr([ // statements
  7913. 'this.MyTest = function (Bar) {',
  7914. ' var Result = 0;',
  7915. ' Result = 2*Bar;',
  7916. ' return Result;',
  7917. '};'
  7918. ]),
  7919. LinesToStr([ // this.$main
  7920. ''
  7921. ]));
  7922. end;
  7923. procedure TTestModule.TestFunctionString;
  7924. begin
  7925. StartProgram(false);
  7926. Add('function Test(Bar: string): string;');
  7927. Add('begin');
  7928. Add(' Result:=bar+BAR');
  7929. Add('end;');
  7930. Add('begin');
  7931. ConvertProgram;
  7932. CheckSource('TestFunctionString',
  7933. LinesToStr([ // statements
  7934. 'this.Test = function (Bar) {',
  7935. ' var Result = "";',
  7936. ' Result = Bar+Bar;',
  7937. ' return Result;',
  7938. '};'
  7939. ]),
  7940. LinesToStr([ // this.$main
  7941. ''
  7942. ]));
  7943. end;
  7944. procedure TTestModule.TestIfThen;
  7945. begin
  7946. StartProgram(false);
  7947. Add([
  7948. 'var b: boolean;',
  7949. 'begin',
  7950. ' if b then ;',
  7951. ' if b then else ;']);
  7952. ConvertProgram;
  7953. CheckSource('TestIfThen',
  7954. LinesToStr([ // statements
  7955. 'this.b = false;',
  7956. '']),
  7957. LinesToStr([ // this.$main
  7958. 'if ($mod.b) ;',
  7959. 'if ($mod.b) ;',
  7960. '']));
  7961. end;
  7962. procedure TTestModule.TestForLoop;
  7963. begin
  7964. StartProgram(false);
  7965. Add('var');
  7966. Add(' vI, vJ, vN: longint;');
  7967. Add('begin');
  7968. Add(' VJ:=0;');
  7969. Add(' VN:=3;');
  7970. Add(' for VI:=1 to VN do');
  7971. Add(' begin');
  7972. Add(' VJ:=VJ+VI;');
  7973. Add(' end;');
  7974. ConvertProgram;
  7975. CheckSource('TestForLoop',
  7976. LinesToStr([ // statements
  7977. 'this.vI = 0;',
  7978. 'this.vJ = 0;',
  7979. 'this.vN = 0;'
  7980. ]),
  7981. LinesToStr([ // this.$main
  7982. ' $mod.vJ = 0;',
  7983. ' $mod.vN = 3;',
  7984. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7985. ' $mod.vI = $l;',
  7986. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7987. ' };',
  7988. '']));
  7989. end;
  7990. procedure TTestModule.TestForLoopInsideFunction;
  7991. begin
  7992. StartProgram(false);
  7993. Add('function SumNumbers(Count: longint): longint;');
  7994. Add('var');
  7995. Add(' vI, vJ: longint;');
  7996. Add('begin');
  7997. Add(' vj:=0;');
  7998. Add(' for vi:=1 to count do');
  7999. Add(' begin');
  8000. Add(' vj:=vj+vi;');
  8001. Add(' end;');
  8002. Add('end;');
  8003. Add('begin');
  8004. Add(' sumnumbers(3);');
  8005. ConvertProgram;
  8006. CheckSource('TestForLoopInsideFunction',
  8007. LinesToStr([ // statements
  8008. 'this.SumNumbers = function (Count) {',
  8009. ' var Result = 0;',
  8010. ' var vI = 0;',
  8011. ' var vJ = 0;',
  8012. ' vJ = 0;',
  8013. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8014. ' vI = $l;',
  8015. ' vJ = vJ + vI;',
  8016. ' };',
  8017. ' return Result;',
  8018. '};'
  8019. ]),
  8020. LinesToStr([ // $mod.$main
  8021. ' $mod.SumNumbers(3);'
  8022. ]));
  8023. end;
  8024. procedure TTestModule.TestForLoop_ReadVarAfter;
  8025. begin
  8026. StartProgram(false);
  8027. Add('var');
  8028. Add(' vI: longint;');
  8029. Add('begin');
  8030. Add(' for vi:=1 to 2 do ;');
  8031. Add(' if vi=3 then ;');
  8032. ConvertProgram;
  8033. CheckSource('TestForLoop',
  8034. LinesToStr([ // statements
  8035. 'this.vI = 0;'
  8036. ]),
  8037. LinesToStr([ // this.$main
  8038. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8039. ' if ($mod.vI===3) ;'
  8040. ]));
  8041. end;
  8042. procedure TTestModule.TestForLoop_Nested;
  8043. begin
  8044. StartProgram(false);
  8045. Add('function SumNumbers(Count: longint): longint;');
  8046. Add('var');
  8047. Add(' vI, vJ, vK: longint;');
  8048. Add('begin');
  8049. Add(' VK:=0;');
  8050. Add(' for VI:=1 to count do');
  8051. Add(' begin');
  8052. Add(' for vj:=1 to vi do');
  8053. Add(' begin');
  8054. Add(' vk:=VK+VI;');
  8055. Add(' end;');
  8056. Add(' end;');
  8057. Add('end;');
  8058. Add('begin');
  8059. Add(' sumnumbers(3);');
  8060. ConvertProgram;
  8061. CheckSource('TestForLoopInFunction',
  8062. LinesToStr([ // statements
  8063. 'this.SumNumbers = function (Count) {',
  8064. ' var Result = 0;',
  8065. ' var vI = 0;',
  8066. ' var vJ = 0;',
  8067. ' var vK = 0;',
  8068. ' vK = 0;',
  8069. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8070. ' vI = $l;',
  8071. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8072. ' vJ = $l1;',
  8073. ' vK = vK + vI;',
  8074. ' };',
  8075. ' };',
  8076. ' return Result;',
  8077. '};'
  8078. ]),
  8079. LinesToStr([ // $mod.$main
  8080. ' $mod.SumNumbers(3);'
  8081. ]));
  8082. end;
  8083. procedure TTestModule.TestRepeatUntil;
  8084. begin
  8085. StartProgram(false);
  8086. Add('var');
  8087. Add(' vI, vJ, vN: longint;');
  8088. Add('begin');
  8089. Add(' vn:=3;');
  8090. Add(' vj:=0;');
  8091. Add(' VI:=0;');
  8092. Add(' repeat');
  8093. Add(' VI:=vi+1;');
  8094. Add(' vj:=VJ+vI;');
  8095. Add(' until vi>=vn');
  8096. ConvertProgram;
  8097. CheckSource('TestRepeatUntil',
  8098. LinesToStr([ // statements
  8099. 'this.vI = 0;',
  8100. 'this.vJ = 0;',
  8101. 'this.vN = 0;'
  8102. ]),
  8103. LinesToStr([ // $mod.$main
  8104. ' $mod.vN = 3;',
  8105. ' $mod.vJ = 0;',
  8106. ' $mod.vI = 0;',
  8107. ' do{',
  8108. ' $mod.vI = $mod.vI + 1;',
  8109. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8110. ' }while(!($mod.vI>=$mod.vN));'
  8111. ]));
  8112. end;
  8113. procedure TTestModule.TestAsmBlock;
  8114. begin
  8115. StartProgram(false);
  8116. Add([
  8117. 'var',
  8118. ' vI: longint;',
  8119. 'begin',
  8120. ' vi:=1;',
  8121. ' asm',
  8122. ' if (vI===1) {',
  8123. ' vI=2;',
  8124. //' console.log(''end;'');', ToDo
  8125. ' }',
  8126. ' if (vI===2){ vI=3; }',
  8127. ' end;',
  8128. ' VI:=4;']);
  8129. ConvertProgram;
  8130. CheckSource('TestAsmBlock',
  8131. LinesToStr([ // statements
  8132. 'this.vI = 0;'
  8133. ]),
  8134. LinesToStr([ // $mod.$main
  8135. '$mod.vI = 1;',
  8136. 'if (vI===1) {',
  8137. ' vI=2;',
  8138. '}',
  8139. 'if (vI===2){ vI=3; }',
  8140. ';',
  8141. '$mod.vI = 4;'
  8142. ]));
  8143. end;
  8144. procedure TTestModule.TestAsmPas_Impl;
  8145. begin
  8146. StartUnit(false);
  8147. Add('interface');
  8148. Add('const cIntf: longint = 1;');
  8149. Add('var vIntf: longint;');
  8150. Add('implementation');
  8151. Add('const cImpl: longint = 2;');
  8152. Add('var vImpl: longint;');
  8153. Add('procedure DoIt;');
  8154. Add('const cLoc: longint = 3;');
  8155. Add('var vLoc: longint;');
  8156. Add('begin;');
  8157. Add(' asm');
  8158. //Add(' pas(vIntf)=pas(cIntf);');
  8159. //Add(' pas(vImpl)=pas(cImpl);');
  8160. //Add(' pas(vLoc)=pas(cLoc);');
  8161. Add(' end;');
  8162. Add('end;');
  8163. ConvertUnit;
  8164. CheckSource('TestAsmPas_Impl',
  8165. LinesToStr([
  8166. 'var $impl = $mod.$impl;',
  8167. 'this.cIntf = 1;',
  8168. 'this.vIntf = 0;',
  8169. '']),
  8170. '', // this.$init
  8171. LinesToStr([ // implementation
  8172. '$impl.cImpl = 2;',
  8173. '$impl.vImpl = 0;',
  8174. 'var cLoc = 3;',
  8175. '$impl.DoIt = function () {',
  8176. ' var vLoc = 0;',
  8177. '};',
  8178. '']) );
  8179. end;
  8180. procedure TTestModule.TestTryFinally;
  8181. begin
  8182. StartProgram(false);
  8183. Add('var i: longint;');
  8184. Add('begin');
  8185. Add(' try');
  8186. Add(' i:=0; i:=2 div i;');
  8187. Add(' finally');
  8188. Add(' i:=3');
  8189. Add(' end;');
  8190. ConvertProgram;
  8191. CheckSource('TestTryFinally',
  8192. LinesToStr([ // statements
  8193. 'this.i = 0;'
  8194. ]),
  8195. LinesToStr([ // $mod.$main
  8196. 'try {',
  8197. ' $mod.i = 0;',
  8198. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8199. '} finally {',
  8200. ' $mod.i = 3;',
  8201. '};'
  8202. ]));
  8203. end;
  8204. procedure TTestModule.TestTryExcept;
  8205. begin
  8206. StartProgram(false);
  8207. Add([
  8208. 'type',
  8209. ' TObject = class end;',
  8210. ' Exception = class Msg: string; end;',
  8211. ' EInvalidCast = class(Exception) end;',
  8212. 'var vI: longint;',
  8213. 'begin',
  8214. ' try',
  8215. ' vi:=1;',
  8216. ' except',
  8217. ' vi:=2',
  8218. ' end;',
  8219. ' try',
  8220. ' vi:=3;',
  8221. ' except',
  8222. ' raise;',
  8223. ' end;',
  8224. ' try',
  8225. ' VI:=4;',
  8226. ' except',
  8227. ' on einvalidcast do',
  8228. ' raise;',
  8229. ' on E: exception do',
  8230. ' if e.msg='''' then',
  8231. ' raise e;',
  8232. ' else',
  8233. ' vi:=5',
  8234. ' end;',
  8235. ' try',
  8236. ' VI:=6;',
  8237. ' except',
  8238. ' on einvalidcast do ;',
  8239. ' end;',
  8240. '']);
  8241. ConvertProgram;
  8242. CheckSource('TestTryExcept',
  8243. LinesToStr([ // statements
  8244. 'rtl.createClass(this, "TObject", null, function () {',
  8245. ' this.$init = function () {',
  8246. ' };',
  8247. ' this.$final = function () {',
  8248. ' };',
  8249. '});',
  8250. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8251. ' this.$init = function () {',
  8252. ' $mod.TObject.$init.call(this);',
  8253. ' this.Msg = "";',
  8254. ' };',
  8255. '});',
  8256. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8257. '});',
  8258. 'this.vI = 0;'
  8259. ]),
  8260. LinesToStr([ // $mod.$main
  8261. 'try {',
  8262. ' $mod.vI = 1;',
  8263. '} catch ($e) {',
  8264. ' $mod.vI = 2;',
  8265. '};',
  8266. 'try {',
  8267. ' $mod.vI = 3;',
  8268. '} catch ($e) {',
  8269. ' throw $e;',
  8270. '};',
  8271. 'try {',
  8272. ' $mod.vI = 4;',
  8273. '} catch ($e) {',
  8274. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8275. ' throw $e',
  8276. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8277. ' var E = $e;',
  8278. ' if (E.Msg === "") throw E;',
  8279. ' } else {',
  8280. ' $mod.vI = 5;',
  8281. ' }',
  8282. '};',
  8283. 'try {',
  8284. ' $mod.vI = 6;',
  8285. '} catch ($e) {',
  8286. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8287. ' } else throw $e',
  8288. '};',
  8289. '']));
  8290. end;
  8291. procedure TTestModule.TestTryExcept_ReservedWords;
  8292. begin
  8293. StartProgram(false);
  8294. Add([
  8295. 'type',
  8296. ' TObject = class end;',
  8297. ' Exception = class',
  8298. ' Symbol: string;',
  8299. ' end;',
  8300. 'var &try: longint;',
  8301. 'begin',
  8302. ' try',
  8303. ' &try:=4;',
  8304. ' except',
  8305. ' on Error: exception do',
  8306. ' if errOR.symBol='''' then',
  8307. ' raise ERRor;',
  8308. ' end;',
  8309. '']);
  8310. ConvertProgram;
  8311. CheckSource('TestTryExcept_ReservedWords',
  8312. LinesToStr([ // statements
  8313. 'rtl.createClass(this, "TObject", null, function () {',
  8314. ' this.$init = function () {',
  8315. ' };',
  8316. ' this.$final = function () {',
  8317. ' };',
  8318. '});',
  8319. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8320. ' this.$init = function () {',
  8321. ' $mod.TObject.$init.call(this);',
  8322. ' this.Symbol = "";',
  8323. ' };',
  8324. '});',
  8325. 'this.Try = 0;',
  8326. '']),
  8327. LinesToStr([ // $mod.$main
  8328. 'try {',
  8329. ' $mod.Try = 4;',
  8330. '} catch ($e) {',
  8331. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8332. ' var error = $e;',
  8333. ' if (error.Symbol === "") throw error;',
  8334. ' } else throw $e',
  8335. '};',
  8336. '']));
  8337. end;
  8338. procedure TTestModule.TestIfThenRaiseElse;
  8339. begin
  8340. StartProgram(false);
  8341. Add([
  8342. 'type',
  8343. ' TObject = class',
  8344. ' constructor Create;',
  8345. ' end;',
  8346. 'constructor TObject.Create;',
  8347. 'begin',
  8348. 'end;',
  8349. 'var b: boolean;',
  8350. 'begin',
  8351. ' if b then',
  8352. ' raise TObject.Create',
  8353. ' else',
  8354. ' b:=false;',
  8355. '']);
  8356. ConvertProgram;
  8357. CheckSource('TestIfThenRaiseElse',
  8358. LinesToStr([ // statements
  8359. 'rtl.createClass(this, "TObject", null, function () {',
  8360. ' this.$init = function () {',
  8361. ' };',
  8362. ' this.$final = function () {',
  8363. ' };',
  8364. ' this.Create = function () {',
  8365. ' return this;',
  8366. ' };',
  8367. '});',
  8368. 'this.b = false;',
  8369. '']),
  8370. LinesToStr([ // $mod.$main
  8371. 'if ($mod.b) {',
  8372. ' throw $mod.TObject.$create("Create")}',
  8373. ' else $mod.b = false;',
  8374. '']));
  8375. end;
  8376. procedure TTestModule.TestCaseOf;
  8377. begin
  8378. StartProgram(false);
  8379. Add([
  8380. 'const e: longint; external name ''$e'';',
  8381. 'var vI: longint;',
  8382. 'begin',
  8383. ' case vi of',
  8384. ' 1: ;',
  8385. ' 2: vi:=3;',
  8386. ' e: ;',
  8387. ' else',
  8388. ' VI:=4',
  8389. ' end;']);
  8390. ConvertProgram;
  8391. CheckSource('TestCaseOf',
  8392. LinesToStr([ // statements
  8393. 'this.vI = 0;'
  8394. ]),
  8395. LinesToStr([ // $mod.$main
  8396. 'var $tmp = $mod.vI;',
  8397. 'if ($tmp === 1) {}',
  8398. 'else if ($tmp === 2) {',
  8399. ' $mod.vI = 3}',
  8400. ' else if ($tmp === $e) {}',
  8401. 'else {',
  8402. ' $mod.vI = 4;',
  8403. '};'
  8404. ]));
  8405. end;
  8406. procedure TTestModule.TestCaseOf_UseSwitch;
  8407. begin
  8408. StartProgram(false);
  8409. Converter.UseSwitchStatement:=true;
  8410. Add('var Vi: longint;');
  8411. Add('begin');
  8412. Add(' case vi of');
  8413. Add(' 1: ;');
  8414. Add(' 2: VI:=3;');
  8415. Add(' else');
  8416. Add(' vi:=4');
  8417. Add(' end;');
  8418. ConvertProgram;
  8419. CheckSource('TestCaseOf_UseSwitch',
  8420. LinesToStr([ // statements
  8421. 'this.Vi = 0;'
  8422. ]),
  8423. LinesToStr([ // $mod.$main
  8424. 'switch ($mod.Vi) {',
  8425. 'case 1:',
  8426. ' break;',
  8427. 'case 2:',
  8428. ' $mod.Vi = 3;',
  8429. ' break;',
  8430. 'default:',
  8431. ' $mod.Vi = 4;',
  8432. '};'
  8433. ]));
  8434. end;
  8435. procedure TTestModule.TestCaseOfNoElse;
  8436. begin
  8437. StartProgram(false);
  8438. Add('var Vi: longint;');
  8439. Add('begin');
  8440. Add(' case vi of');
  8441. Add(' 1: begin vi:=2; VI:=3; end;');
  8442. Add(' end;');
  8443. ConvertProgram;
  8444. CheckSource('TestCaseOfNoElse',
  8445. LinesToStr([ // statements
  8446. 'this.Vi = 0;'
  8447. ]),
  8448. LinesToStr([ // $mod.$main
  8449. 'var $tmp = $mod.Vi;',
  8450. 'if ($tmp === 1) {',
  8451. ' $mod.Vi = 2;',
  8452. ' $mod.Vi = 3;',
  8453. '};'
  8454. ]));
  8455. end;
  8456. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8457. begin
  8458. StartProgram(false);
  8459. Converter.UseSwitchStatement:=true;
  8460. Add('var vI: longint;');
  8461. Add('begin');
  8462. Add(' case vi of');
  8463. Add(' 1: begin VI:=2; vi:=3; end;');
  8464. Add(' end;');
  8465. ConvertProgram;
  8466. CheckSource('TestCaseOfNoElse_UseSwitch',
  8467. LinesToStr([ // statements
  8468. 'this.vI = 0;'
  8469. ]),
  8470. LinesToStr([ // $mod.$main
  8471. 'switch ($mod.vI) {',
  8472. 'case 1:',
  8473. ' $mod.vI = 2;',
  8474. ' $mod.vI = 3;',
  8475. ' break;',
  8476. '};'
  8477. ]));
  8478. end;
  8479. procedure TTestModule.TestCaseOfRange;
  8480. begin
  8481. StartProgram(false);
  8482. Add('var vI: longint;');
  8483. Add('begin');
  8484. Add(' case vi of');
  8485. Add(' 1..3: vi:=14;');
  8486. Add(' 4,5: vi:=16;');
  8487. Add(' 6..7,9..10: ;');
  8488. Add(' else ;');
  8489. Add(' end;');
  8490. ConvertProgram;
  8491. CheckSource('TestCaseOfRange',
  8492. LinesToStr([ // statements
  8493. 'this.vI = 0;'
  8494. ]),
  8495. LinesToStr([ // $mod.$main
  8496. 'var $tmp = $mod.vI;',
  8497. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8498. ' $mod.vI = 14',
  8499. '} else if (($tmp === 4) || ($tmp === 5)){',
  8500. ' $mod.vI = 16',
  8501. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8502. ]));
  8503. end;
  8504. procedure TTestModule.TestCaseOfString;
  8505. begin
  8506. StartProgram(false);
  8507. Add([
  8508. 'var s,h: string;',
  8509. 'begin',
  8510. ' case s of',
  8511. ' ''foo'': s:=h;',
  8512. ' ''a''..''z'': h:=s;',
  8513. ' ''ў'', ''ё'': ;',
  8514. ' ''Б''..''Я'': ;',
  8515. ' end;',
  8516. '']);
  8517. ConvertProgram;
  8518. CheckSource('TestCaseOfString',
  8519. LinesToStr([ // statements
  8520. 'this.s = "";',
  8521. 'this.h = "";',
  8522. '']),
  8523. LinesToStr([ // $mod.$main
  8524. 'var $tmp = $mod.s;',
  8525. 'if ($tmp === "foo") {',
  8526. ' $mod.s = $mod.h}',
  8527. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8528. ' $mod.h = $mod.s}',
  8529. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8530. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8531. '']));
  8532. end;
  8533. procedure TTestModule.TestCaseOfChar;
  8534. begin
  8535. StartProgram(false);
  8536. Add([
  8537. 'var s,h: char;',
  8538. 'begin',
  8539. ' case s of',
  8540. ' ''a''..''z'': h:=s;',
  8541. ' ''ä'': ;',
  8542. ' ''ў'', ''ё'': ;',
  8543. ' ''Б''..''Я'': ;',
  8544. ' end;',
  8545. '']);
  8546. ConvertProgram;
  8547. CheckSource('TestCaseOfString',
  8548. LinesToStr([ // statements
  8549. 'this.s = "";',
  8550. 'this.h = "";',
  8551. '']),
  8552. LinesToStr([ // $mod.$main
  8553. 'var $tmp = $mod.s;',
  8554. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8555. ' $mod.h = $mod.s}',
  8556. ' else if ($tmp === "ä") {}',
  8557. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8558. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8559. '']));
  8560. end;
  8561. procedure TTestModule.TestCaseOfExternalClassConst;
  8562. begin
  8563. StartProgram(false);
  8564. Add([
  8565. '{$modeswitch externalclass}',
  8566. 'type',
  8567. ' TBird = class external name ''Bird''',
  8568. ' const e: longint;',
  8569. ' end;',
  8570. 'var vI: longint;',
  8571. 'begin',
  8572. ' case vi of',
  8573. ' 1: vi:=3;',
  8574. ' TBird.e: ;',
  8575. ' end;']);
  8576. ConvertProgram;
  8577. CheckSource('TestCaseOfExternalClassConst',
  8578. LinesToStr([ // statements
  8579. 'this.vI = 0;'
  8580. ]),
  8581. LinesToStr([ // $mod.$main
  8582. 'var $tmp = $mod.vI;',
  8583. 'if ($tmp === 1) {',
  8584. ' $mod.vI = 3}',
  8585. ' else if ($tmp === Bird.e) ;'
  8586. ]));
  8587. end;
  8588. procedure TTestModule.TestDebugger;
  8589. begin
  8590. StartProgram(false);
  8591. Add([
  8592. 'procedure DoIt;',
  8593. 'begin',
  8594. ' deBugger;',
  8595. ' DeBugger();',
  8596. 'end;',
  8597. 'begin',
  8598. ' Debugger;']);
  8599. ConvertProgram;
  8600. CheckSource('TestDebugger',
  8601. LinesToStr([ // statements
  8602. 'this.DoIt = function () {',
  8603. ' debugger;',
  8604. ' debugger;',
  8605. '};',
  8606. '']),
  8607. LinesToStr([ // $mod.$main
  8608. 'debugger;',
  8609. '']));
  8610. end;
  8611. procedure TTestModule.TestArray_Dynamic;
  8612. begin
  8613. StartProgram(false);
  8614. Add([
  8615. 'type',
  8616. ' TArrayInt = array of longint;',
  8617. 'var',
  8618. ' Arr: TArrayInt;',
  8619. ' i: longint;',
  8620. ' b: boolean;',
  8621. 'begin',
  8622. ' SetLength(arr,3);',
  8623. ' arr[0]:=4;',
  8624. ' arr[1]:=length(arr)+arr[0];',
  8625. ' arr[i]:=5;',
  8626. ' arr[arr[i]]:=arr[6];',
  8627. ' i:=low(arr);',
  8628. ' i:=high(arr);',
  8629. ' b:=Assigned(arr);',
  8630. ' Arr:=default(TArrayInt);']);
  8631. ConvertProgram;
  8632. CheckSource('TestArray_Dynamic',
  8633. LinesToStr([ // statements
  8634. 'this.Arr = [];',
  8635. 'this.i = 0;',
  8636. 'this.b = false;'
  8637. ]),
  8638. LinesToStr([ // $mod.$main
  8639. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8640. '$mod.Arr[0] = 4;',
  8641. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8642. '$mod.Arr[$mod.i] = 5;',
  8643. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8644. '$mod.i = 0;',
  8645. '$mod.i = rtl.length($mod.Arr) - 1;',
  8646. '$mod.b = rtl.length($mod.Arr) > 0;',
  8647. '$mod.Arr = [];',
  8648. '']));
  8649. end;
  8650. procedure TTestModule.TestArray_Dynamic_Nil;
  8651. begin
  8652. StartProgram(false);
  8653. Add('type');
  8654. Add(' TArrayInt = array of longint;');
  8655. Add('var');
  8656. Add(' Arr: TArrayInt;');
  8657. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8658. Add('begin');
  8659. Add(' arr:=nil;');
  8660. Add(' if arr=nil then;');
  8661. Add(' if nil=arr then;');
  8662. Add(' if arr<>nil then;');
  8663. Add(' if nil<>arr then;');
  8664. Add(' DoIt(nil,nil);');
  8665. ConvertProgram;
  8666. CheckSource('TestArray_Dynamic',
  8667. LinesToStr([ // statements
  8668. 'this.Arr = [];',
  8669. 'this.DoIt = function(i,j){',
  8670. '};'
  8671. ]),
  8672. LinesToStr([ // $mod.$main
  8673. '$mod.Arr = [];',
  8674. 'if (rtl.length($mod.Arr) === 0) ;',
  8675. 'if (rtl.length($mod.Arr) === 0) ;',
  8676. 'if (rtl.length($mod.Arr) > 0) ;',
  8677. 'if (rtl.length($mod.Arr) > 0) ;',
  8678. '$mod.DoIt([],[]);',
  8679. '']));
  8680. end;
  8681. procedure TTestModule.TestArray_DynMultiDimensional;
  8682. begin
  8683. StartProgram(false);
  8684. Add([
  8685. 'type',
  8686. ' TArrayInt = array of longint;',
  8687. ' TArrayArrayInt = array of TArrayInt;',
  8688. 'var',
  8689. ' Arr: TArrayInt;',
  8690. ' Arr2: TArrayArrayInt;',
  8691. ' i: longint;',
  8692. 'begin',
  8693. ' arr2:=nil;',
  8694. ' if arr2=nil then;',
  8695. ' if nil=arr2 then;',
  8696. ' i:=low(arr2);',
  8697. ' i:=low(arr2[1]);',
  8698. ' i:=high(arr2);',
  8699. ' i:=high(arr2[2]);',
  8700. ' arr2[3]:=arr;',
  8701. ' arr2[4][5]:=i;',
  8702. ' i:=arr2[6][7];',
  8703. ' arr2[8,9]:=i;',
  8704. ' i:=arr2[10,11];',
  8705. ' SetLength(arr2,14);',
  8706. ' SetLength(arr2[15],16);']);
  8707. ConvertProgram;
  8708. CheckSource('TestArray_Dynamic',
  8709. LinesToStr([ // statements
  8710. 'this.Arr = [];',
  8711. 'this.Arr2 = [];',
  8712. 'this.i = 0;'
  8713. ]),
  8714. LinesToStr([ // $mod.$main
  8715. '$mod.Arr2 = [];',
  8716. 'if (rtl.length($mod.Arr2) === 0) ;',
  8717. 'if (rtl.length($mod.Arr2) === 0) ;',
  8718. '$mod.i = 0;',
  8719. '$mod.i = 0;',
  8720. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8721. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8722. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8723. '$mod.Arr2[4][5] = $mod.i;',
  8724. '$mod.i = $mod.Arr2[6][7];',
  8725. '$mod.Arr2[8][9] = $mod.i;',
  8726. '$mod.i = $mod.Arr2[10][11];',
  8727. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8728. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8729. '']));
  8730. end;
  8731. procedure TTestModule.TestArray_DynamicAssign;
  8732. begin
  8733. StartProgram(false);
  8734. Add([
  8735. 'type',
  8736. ' TArrayInt = array of longint;',
  8737. ' TArrayArrayInt = array of TArrayInt;',
  8738. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8739. 'begin',
  8740. 'end;',
  8741. 'procedure Fly(var a: TArrayInt);',
  8742. 'begin',
  8743. 'end;',
  8744. 'var',
  8745. ' Arr: TArrayInt;',
  8746. ' Arr2: TArrayArrayInt;',
  8747. 'begin',
  8748. ' arr:=nil;',
  8749. ' arr2:=nil;',
  8750. ' arr2[1]:=nil;',
  8751. ' arr2[2]:=arr;',
  8752. ' Run(arr,arr,arr);',
  8753. ' Fly(arr);',
  8754. ' Run(arr2[4],arr2[5],arr2[6]);',
  8755. ' Fly(arr2[7]);',
  8756. '']);
  8757. ConvertProgram;
  8758. CheckSource('TestArray_DynamicAssign',
  8759. LinesToStr([ // statements
  8760. 'this.Run = function (a, b, c) {',
  8761. '};',
  8762. 'this.Fly = function (a) {',
  8763. '};',
  8764. 'this.Arr = [];',
  8765. 'this.Arr2 = [];',
  8766. '']),
  8767. LinesToStr([ // $mod.$main
  8768. '$mod.Arr = [];',
  8769. '$mod.Arr2 = [];',
  8770. '$mod.Arr2[1] = [];',
  8771. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8772. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8773. '$mod.Fly({',
  8774. ' p: $mod,',
  8775. ' get: function () {',
  8776. ' return this.p.Arr;',
  8777. ' },',
  8778. ' set: function (v) {',
  8779. ' this.p.Arr = v;',
  8780. ' }',
  8781. '});',
  8782. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8783. '$mod.Fly({',
  8784. ' a: 7,',
  8785. ' p: $mod.Arr2,',
  8786. ' get: function () {',
  8787. ' return this.p[this.a];',
  8788. ' },',
  8789. ' set: function (v) {',
  8790. ' this.p[this.a] = v;',
  8791. ' }',
  8792. '});',
  8793. '']));
  8794. end;
  8795. procedure TTestModule.TestArray_StaticInt;
  8796. begin
  8797. StartProgram(false);
  8798. Add('type');
  8799. Add(' TArrayInt = array[2..4] of longint;');
  8800. Add('var');
  8801. Add(' Arr: TArrayInt;');
  8802. Add(' Arr2: TArrayInt = (5,6,7);');
  8803. Add(' i: longint;');
  8804. Add(' b: boolean;');
  8805. Add('begin');
  8806. Add(' arr[2]:=4;');
  8807. Add(' arr[3]:=arr[2]+arr[3];');
  8808. Add(' arr[i]:=5;');
  8809. Add(' arr[arr[i]]:=arr[high(arr)];');
  8810. Add(' i:=low(arr);');
  8811. Add(' i:=high(arr);');
  8812. Add(' b:=arr[2]=arr[3];');
  8813. Add(' arr:=default(TArrayInt);');
  8814. ConvertProgram;
  8815. CheckSource('TestArray_StaticInt',
  8816. LinesToStr([ // statements
  8817. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8818. 'this.Arr2 = [5, 6, 7];',
  8819. 'this.i = 0;',
  8820. 'this.b = false;'
  8821. ]),
  8822. LinesToStr([ // $mod.$main
  8823. '$mod.Arr[0] = 4;',
  8824. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8825. '$mod.Arr[$mod.i-2] = 5;',
  8826. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8827. '$mod.i = 2;',
  8828. '$mod.i = 4;',
  8829. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8830. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8831. '']));
  8832. end;
  8833. procedure TTestModule.TestArray_StaticBool;
  8834. begin
  8835. StartProgram(false);
  8836. Add('type');
  8837. Add(' TBools = array[boolean] of boolean;');
  8838. Add(' TBool2 = array[true..true] of boolean;');
  8839. Add('var');
  8840. Add(' Arr: TBools;');
  8841. Add(' Arr2: TBool2;');
  8842. Add(' Arr3: TBools = (true,false);');
  8843. Add(' b: boolean;');
  8844. Add('begin');
  8845. Add(' b:=low(arr);');
  8846. Add(' b:=high(arr);');
  8847. Add(' arr[true]:=false;');
  8848. Add(' arr[false]:=arr[b] or arr[true];');
  8849. Add(' arr[b]:=true;');
  8850. Add(' arr[arr[b]]:=arr[high(arr)];');
  8851. Add(' b:=arr[false]=arr[true];');
  8852. Add(' b:=low(arr2);');
  8853. Add(' b:=high(arr2);');
  8854. Add(' arr2[true]:=true;');
  8855. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8856. Add(' arr2[b]:=false;');
  8857. ConvertProgram;
  8858. CheckSource('TestArray_StaticBool',
  8859. LinesToStr([ // statements
  8860. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8861. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8862. 'this.Arr3 = [true, false];',
  8863. 'this.b = false;'
  8864. ]),
  8865. LinesToStr([ // $mod.$main
  8866. '$mod.b = false;',
  8867. '$mod.b = true;',
  8868. '$mod.Arr[1] = false;',
  8869. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8870. '$mod.Arr[+$mod.b] = true;',
  8871. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8872. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8873. '$mod.b = true;',
  8874. '$mod.b = true;',
  8875. '$mod.Arr2[0] = true;',
  8876. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8877. '$mod.Arr2[1-$mod.b] = false;',
  8878. '']));
  8879. end;
  8880. procedure TTestModule.TestArray_StaticChar;
  8881. begin
  8882. StartProgram(false);
  8883. Add([
  8884. 'type',
  8885. ' TChars = array[char] of char;',
  8886. ' TChars2 = array[''a''..''z''] of char;',
  8887. 'var',
  8888. ' Arr: TChars;',
  8889. ' Arr2: TChars2;',
  8890. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8891. ' Arr4: array[11..13] of char = ''pas'';',
  8892. ' Arr5: array[21..22] of char = ''äö'';',
  8893. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8894. ' c: char;',
  8895. ' b: boolean;',
  8896. 'begin',
  8897. ' c:=low(arr);',
  8898. ' c:=high(arr);',
  8899. ' arr[''B'']:=''a'';',
  8900. ' arr[''D'']:=arr[c];',
  8901. ' arr[c]:=arr[''d''];',
  8902. ' arr[arr[c]]:=arr[high(arr)];',
  8903. ' b:=arr[low(arr)]=arr[''e''];',
  8904. ' c:=low(arr2);',
  8905. ' c:=high(arr2);',
  8906. ' arr2[''b'']:=''f'';',
  8907. ' arr2[''a'']:=arr2[c];',
  8908. ' arr2[c]:=arr2[''g''];']);
  8909. ConvertProgram;
  8910. CheckSource('TestArray_StaticChar',
  8911. LinesToStr([ // statements
  8912. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8913. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8914. 'this.Arr3 = ["p", "a", "s"];',
  8915. 'this.Arr4 = ["p", "a", "s"];',
  8916. 'this.Arr5 = ["ä", "ö"];',
  8917. 'this.Arr6 = ["ä", "ö"];',
  8918. 'this.c = "";',
  8919. 'this.b = false;',
  8920. '']),
  8921. LinesToStr([ // $mod.$main
  8922. '$mod.c = "\x00";',
  8923. '$mod.c = "\uFFFF";',
  8924. '$mod.Arr[66] = "a";',
  8925. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8926. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8927. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8928. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8929. '$mod.c = "a";',
  8930. '$mod.c = "z";',
  8931. '$mod.Arr2[1] = "f";',
  8932. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8933. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8934. '']));
  8935. end;
  8936. procedure TTestModule.TestArray_StaticMultiDim;
  8937. begin
  8938. StartProgram(false);
  8939. Add([
  8940. 'type',
  8941. ' TArrayInt = array[1..3] of longint;',
  8942. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8943. 'var',
  8944. ' Arr: TArrayInt;',
  8945. ' Arr2: TArrayArrayInt;',
  8946. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8947. ' i: longint;',
  8948. 'begin',
  8949. ' i:=low(arr);',
  8950. ' i:=low(arr2);',
  8951. ' i:=low(arr2[5]);',
  8952. ' i:=high(arr);',
  8953. ' i:=high(arr2);',
  8954. ' i:=high(arr2[6]);',
  8955. ' arr2[5]:=arr;',
  8956. ' arr2[6][2]:=i;',
  8957. ' i:=arr2[6][3];',
  8958. ' arr2[6,3]:=i;',
  8959. ' i:=arr2[5,2];',
  8960. ' arr2:=arr2;',// clone multi dim static array
  8961. //' arr3:=arr3;',// clone anonymous multi dim static array
  8962. '']);
  8963. ConvertProgram;
  8964. CheckSource('TestArray_StaticMultiDim',
  8965. LinesToStr([ // statements
  8966. 'this.TArrayArrayInt$clone = function (a) {',
  8967. ' var r = [];',
  8968. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8969. ' return r;',
  8970. '};',
  8971. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8972. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8973. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8974. 'this.i = 0;'
  8975. ]),
  8976. LinesToStr([ // $mod.$main
  8977. '$mod.i = 1;',
  8978. '$mod.i = 5;',
  8979. '$mod.i = 1;',
  8980. '$mod.i = 3;',
  8981. '$mod.i = 6;',
  8982. '$mod.i = 3;',
  8983. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8984. '$mod.Arr2[1][1] = $mod.i;',
  8985. '$mod.i = $mod.Arr2[1][2];',
  8986. '$mod.Arr2[1][2] = $mod.i;',
  8987. '$mod.i = $mod.Arr2[0][1];',
  8988. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8989. '']));
  8990. end;
  8991. procedure TTestModule.TestArray_StaticInFunction;
  8992. begin
  8993. StartProgram(false);
  8994. Add([
  8995. 'const TArrayInt = 3;',
  8996. 'const TArrayArrayInt = 4;',
  8997. 'procedure DoIt;',
  8998. 'type',
  8999. ' TArrayInt = array[1..3] of longint;',
  9000. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9001. 'var',
  9002. ' Arr: TArrayInt;',
  9003. ' Arr2: TArrayArrayInt;',
  9004. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9005. ' i: longint;',
  9006. 'begin',
  9007. ' arr2[5]:=arr;',
  9008. ' arr2:=arr2;',// clone multi dim static array
  9009. 'end;',
  9010. 'begin',
  9011. '']);
  9012. ConvertProgram;
  9013. CheckSource('TestArray_StaticInFunction',
  9014. LinesToStr([ // statements
  9015. 'this.TArrayInt = 3;',
  9016. 'this.TArrayArrayInt = 4;',
  9017. 'var TArrayArrayInt$1$clone = function (a) {',
  9018. ' var r = [];',
  9019. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9020. ' return r;',
  9021. '};',
  9022. 'this.DoIt = function () {',
  9023. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9024. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9025. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9026. ' var i = 0;',
  9027. ' Arr2[0] = Arr.slice(0);',
  9028. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9029. '};',
  9030. '']),
  9031. LinesToStr([ // $mod.$main
  9032. '']));
  9033. end;
  9034. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9035. begin
  9036. StartProgram(false);
  9037. Add([
  9038. 'type',
  9039. ' TArrayInt = array[1..3,1..2] of longint;',
  9040. 'var',
  9041. ' a,b: TArrayInt;',
  9042. 'begin',
  9043. ' if a=b then ;',
  9044. '']);
  9045. SetExpectedPasResolverError('compare static array is not supported',
  9046. nXIsNotSupported);
  9047. ConvertProgram;
  9048. end;
  9049. procedure TTestModule.TestArrayOfRecord;
  9050. begin
  9051. StartProgram(false);
  9052. Add([
  9053. 'type',
  9054. ' TRec = record',
  9055. ' Int: longint;',
  9056. ' end;',
  9057. ' TArrayRec = array of TRec;',
  9058. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9059. 'begin',
  9060. 'end;',
  9061. 'var',
  9062. ' Arr: TArrayRec;',
  9063. ' r: TRec;',
  9064. ' i: longint;',
  9065. 'begin',
  9066. ' SetLength(arr,3);',
  9067. ' arr[0].int:=4;',
  9068. ' arr[1].int:=length(arr)+arr[2].int;',
  9069. ' arr[arr[i].int].int:=arr[5].int;',
  9070. ' arr[7]:=r;',
  9071. ' r:=arr[8];',
  9072. ' i:=low(arr);',
  9073. ' i:=high(arr);',
  9074. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9075. ConvertProgram;
  9076. CheckSource('TestArrayOfRecord',
  9077. LinesToStr([ // statements
  9078. 'rtl.recNewT(this, "TRec", function () {',
  9079. ' this.Int = 0;',
  9080. ' this.$eq = function (b) {',
  9081. ' return this.Int === b.Int;',
  9082. ' };',
  9083. ' this.$assign = function (s) {',
  9084. ' this.Int = s.Int;',
  9085. ' return this;',
  9086. ' };',
  9087. '});',
  9088. 'this.DoIt = function (vd, vc, vv) {',
  9089. '};',
  9090. 'this.Arr = [];',
  9091. 'this.r = this.TRec.$new();',
  9092. 'this.i = 0;'
  9093. ]),
  9094. LinesToStr([ // $mod.$main
  9095. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9096. '$mod.Arr[0].Int = 4;',
  9097. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9098. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9099. '$mod.Arr[7].$assign($mod.r);',
  9100. '$mod.r.$assign($mod.Arr[8]);',
  9101. '$mod.i = 0;',
  9102. '$mod.i = rtl.length($mod.Arr)-1;',
  9103. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9104. '']));
  9105. end;
  9106. procedure TTestModule.TestArray_StaticRecord;
  9107. begin
  9108. StartProgram(false);
  9109. Add([
  9110. 'type',
  9111. ' TRec = record',
  9112. ' Int: longint;',
  9113. ' end;',
  9114. ' TArrayRec = array[1..2] of TRec;',
  9115. 'var',
  9116. ' Arr: TArrayRec;',
  9117. 'begin',
  9118. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9119. '']);
  9120. ConvertProgram;
  9121. CheckSource('TestArray_StaticRecord',
  9122. LinesToStr([ // statements
  9123. 'rtl.recNewT(this, "TRec", function () {',
  9124. ' this.Int = 0;',
  9125. ' this.$eq = function (b) {',
  9126. ' return this.Int === b.Int;',
  9127. ' };',
  9128. ' this.$assign = function (s) {',
  9129. ' this.Int = s.Int;',
  9130. ' return this;',
  9131. ' };',
  9132. '});',
  9133. 'this.TArrayRec$clone = function (a) {',
  9134. ' var r = [];',
  9135. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9136. ' return r;',
  9137. '};',
  9138. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9139. '']),
  9140. LinesToStr([ // $mod.$main
  9141. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9142. end;
  9143. procedure TTestModule.TestArrayOfSet;
  9144. begin
  9145. StartProgram(false);
  9146. Add([
  9147. 'type',
  9148. ' TFlag = (big,small);',
  9149. ' TSetOfFlag = set of tflag;',
  9150. ' TArrayFlag = array of TSetOfFlag;',
  9151. 'procedure DoIt(const a: Tarrayflag);',
  9152. 'begin',
  9153. 'end;',
  9154. 'var',
  9155. ' f: TFlag;',
  9156. ' s: TSetOfFlag;',
  9157. ' Arr: TArrayFlag;',
  9158. ' i: longint;',
  9159. 'begin',
  9160. ' SetLength(arr,3);',
  9161. ' arr[0]:=s;',
  9162. ' arr[1]:=[big];',
  9163. ' arr[2]:=[big]+s;',
  9164. ' arr[3]:=s+[big];',
  9165. ' arr[4]:=arr[5];',
  9166. ' s:=arr[6];',
  9167. ' i:=low(arr);',
  9168. ' i:=high(arr);',
  9169. ' DoIt(arr);',
  9170. ' DoIt([s]);',
  9171. ' DoIt([[],s]);',
  9172. ' DoIt([s,[]]);',
  9173. '']);
  9174. ConvertProgram;
  9175. CheckSource('TestArrayOfSet',
  9176. LinesToStr([ // statements
  9177. 'this.TFlag = {',
  9178. ' "0": "big",',
  9179. ' big: 0,',
  9180. ' "1": "small",',
  9181. ' small: 1',
  9182. '};',
  9183. 'this.DoIt = function (a) {',
  9184. '};',
  9185. 'this.f = 0;',
  9186. 'this.s = {};',
  9187. 'this.Arr = [];',
  9188. 'this.i = 0;',
  9189. '']),
  9190. LinesToStr([ // $mod.$main
  9191. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9192. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9193. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9194. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9195. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9196. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9197. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9198. '$mod.i = 0;',
  9199. '$mod.i = rtl.length($mod.Arr) - 1;',
  9200. '$mod.DoIt($mod.Arr);',
  9201. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9202. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9203. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9204. '']));
  9205. end;
  9206. procedure TTestModule.TestArray_DynAsParam;
  9207. begin
  9208. StartProgram(false);
  9209. Add([
  9210. 'type integer = longint;',
  9211. 'type TArrInt = array of integer;',
  9212. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9213. 'var vJ: TArrInt;',
  9214. 'begin',
  9215. ' vg:=vg;',
  9216. ' vj:=vh;',
  9217. ' vi:=vi;',
  9218. ' doit(vg,vg,vg);',
  9219. ' doit(vh,vh,vj);',
  9220. ' doit(vi,vi,vi);',
  9221. ' doit(vj,vj,vj);',
  9222. 'end;',
  9223. 'var i: TArrInt;',
  9224. 'begin',
  9225. ' doit(i,i,i);']);
  9226. ConvertProgram;
  9227. CheckSource('TestArray_DynAsParams',
  9228. LinesToStr([ // statements
  9229. 'this.DoIt = function (vG,vH,vI) {',
  9230. ' var vJ = [];',
  9231. ' vG = rtl.arrayRef(vG);',
  9232. ' vJ = rtl.arrayRef(vH);',
  9233. ' vI.set(rtl.arrayRef(vI.get()));',
  9234. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9235. ' get: function () {',
  9236. ' return vG;',
  9237. ' },',
  9238. ' set: function (v) {',
  9239. ' vG = v;',
  9240. ' }',
  9241. ' });',
  9242. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9243. ' get: function () {',
  9244. ' return vJ;',
  9245. ' },',
  9246. ' set: function (v) {',
  9247. ' vJ = v;',
  9248. ' }',
  9249. ' });',
  9250. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9251. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9252. ' get: function () {',
  9253. ' return vJ;',
  9254. ' },',
  9255. ' set: function (v) {',
  9256. ' vJ = v;',
  9257. ' }',
  9258. ' });',
  9259. '};',
  9260. 'this.i = [];'
  9261. ]),
  9262. LinesToStr([
  9263. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9264. ' p: $mod,',
  9265. ' get: function () {',
  9266. ' return this.p.i;',
  9267. ' },',
  9268. ' set: function (v) {',
  9269. ' this.p.i = v;',
  9270. ' }',
  9271. '});'
  9272. ]));
  9273. end;
  9274. procedure TTestModule.TestArray_StaticAsParam;
  9275. begin
  9276. StartProgram(false);
  9277. Add([
  9278. 'type integer = longint;',
  9279. 'type TArrInt = array[1..2] of integer;',
  9280. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9281. 'var vJ: TArrInt;',
  9282. 'begin',
  9283. ' vg:=vg;',
  9284. ' vj:=vh;',
  9285. ' vi:=vi;',
  9286. ' doit(vg,vg,vg);',
  9287. ' doit(vh,vh,vj);',
  9288. ' doit(vi,vi,vi);',
  9289. ' doit(vj,vj,vj);',
  9290. 'end;',
  9291. 'var i: TArrInt;',
  9292. 'begin',
  9293. ' doit(i,i,i);']);
  9294. ConvertProgram;
  9295. CheckSource('TestArray_StaticAsParams',
  9296. LinesToStr([ // statements
  9297. 'this.DoIt = function (vG,vH,vI) {',
  9298. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9299. ' vG = vG.slice(0);',
  9300. ' vJ = vH.slice(0);',
  9301. ' vI.set(vI.get().slice(0));',
  9302. ' $mod.DoIt(vG.slice(0), vG, {',
  9303. ' get: function () {',
  9304. ' return vG;',
  9305. ' },',
  9306. ' set: function (v) {',
  9307. ' vG = v;',
  9308. ' }',
  9309. ' });',
  9310. ' $mod.DoIt(vH.slice(0), vH, {',
  9311. ' get: function () {',
  9312. ' return vJ;',
  9313. ' },',
  9314. ' set: function (v) {',
  9315. ' vJ = v;',
  9316. ' }',
  9317. ' });',
  9318. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9319. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9320. ' get: function () {',
  9321. ' return vJ;',
  9322. ' },',
  9323. ' set: function (v) {',
  9324. ' vJ = v;',
  9325. ' }',
  9326. ' });',
  9327. '};',
  9328. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9329. ]),
  9330. LinesToStr([
  9331. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9332. ' p: $mod,',
  9333. ' get: function () {',
  9334. ' return this.p.i;',
  9335. ' },',
  9336. ' set: function (v) {',
  9337. ' this.p.i = v;',
  9338. ' }',
  9339. '});'
  9340. ]));
  9341. end;
  9342. procedure TTestModule.TestArrayElement_AsParams;
  9343. begin
  9344. StartProgram(false);
  9345. Add('type integer = longint;');
  9346. Add('type TArrayInt = array of integer;');
  9347. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9348. Add('var vJ: tarrayint;');
  9349. Add('begin');
  9350. Add(' vi:=vi;');
  9351. Add(' doit(vi,vi,vi);');
  9352. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9353. Add('end;');
  9354. Add('var a: TArrayInt;');
  9355. Add('begin');
  9356. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9357. ConvertProgram;
  9358. CheckSource('TestArrayElement_AsParams',
  9359. LinesToStr([ // statements
  9360. 'this.DoIt = function (vG,vH,vI) {',
  9361. ' var vJ = [];',
  9362. ' vI.set(vI.get());',
  9363. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9364. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9365. ' a:1+3,',
  9366. ' p:vJ,',
  9367. ' get: function () {',
  9368. ' return this.p[this.a];',
  9369. ' },',
  9370. ' set: function (v) {',
  9371. ' this.p[this.a] = v;',
  9372. ' }',
  9373. ' });',
  9374. '};',
  9375. 'this.a = [];'
  9376. ]),
  9377. LinesToStr([
  9378. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9379. ' a: 1+6,',
  9380. ' p: $mod.a,',
  9381. ' get: function () {',
  9382. ' return this.p[this.a];',
  9383. ' },',
  9384. ' set: function (v) {',
  9385. ' this.p[this.a] = v;',
  9386. ' }',
  9387. '});'
  9388. ]));
  9389. end;
  9390. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9391. begin
  9392. StartProgram(false);
  9393. Add('type Integer = longint;');
  9394. Add('type TArrayInt = array of integer;');
  9395. Add('function GetArr(vB: integer = 0): tarrayint;');
  9396. Add('begin');
  9397. Add('end;');
  9398. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9399. Add('begin');
  9400. Add('end;');
  9401. Add('begin');
  9402. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9403. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9404. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9405. ConvertProgram;
  9406. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9407. LinesToStr([ // statements
  9408. 'this.GetArr = function (vB) {',
  9409. ' var Result = [];',
  9410. ' return Result;',
  9411. '};',
  9412. 'this.DoIt = function (vG,vH,vI) {',
  9413. '};'
  9414. ]),
  9415. LinesToStr([
  9416. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9417. ' a: 1+3,',
  9418. ' p: $mod.GetArr(0),',
  9419. ' get: function () {',
  9420. ' return this.p[this.a];',
  9421. ' },',
  9422. ' set: function (v) {',
  9423. ' this.p[this.a] = v;',
  9424. ' }',
  9425. '});',
  9426. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9427. ' a: 2+3,',
  9428. ' p: $mod.GetArr(0),',
  9429. ' get: function () {',
  9430. ' return this.p[this.a];',
  9431. ' },',
  9432. ' set: function (v) {',
  9433. ' this.p[this.a] = v;',
  9434. ' }',
  9435. '});',
  9436. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9437. ' a: 3+3,',
  9438. ' p: $mod.GetArr(9),',
  9439. ' get: function () {',
  9440. ' return this.p[this.a];',
  9441. ' },',
  9442. ' set: function (v) {',
  9443. ' this.p[this.a] = v;',
  9444. ' }',
  9445. '});',
  9446. '']));
  9447. end;
  9448. procedure TTestModule.TestArrayEnumTypeRange;
  9449. begin
  9450. StartProgram(false);
  9451. Add([
  9452. 'type',
  9453. ' TEnum = (red,blue);',
  9454. ' TEnumArray = array[TEnum] of longint;',
  9455. 'var',
  9456. ' e: TEnum;',
  9457. ' i: longint;',
  9458. ' a: TEnumArray;',
  9459. ' numbers: TEnumArray = (1,2);',
  9460. ' names: array[TEnum] of string = (''red'',''blue'');',
  9461. 'begin',
  9462. ' e:=low(a);',
  9463. ' e:=high(a);',
  9464. ' i:=a[red];',
  9465. ' a[e]:=a[e];']);
  9466. ConvertProgram;
  9467. CheckSource('TestArrayEnumTypeRange',
  9468. LinesToStr([ // statements
  9469. ' this.TEnum = {',
  9470. ' "0": "red",',
  9471. ' red: 0,',
  9472. ' "1": "blue",',
  9473. ' blue: 1',
  9474. '};',
  9475. 'this.e = 0;',
  9476. 'this.i = 0;',
  9477. 'this.a = rtl.arraySetLength(null,0,2);',
  9478. 'this.numbers = [1, 2];',
  9479. 'this.names = ["red", "blue"];',
  9480. '']),
  9481. LinesToStr([ // $mod.$main
  9482. '$mod.e = $mod.TEnum.red;',
  9483. '$mod.e = $mod.TEnum.blue;',
  9484. '$mod.i = $mod.a[$mod.TEnum.red];',
  9485. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9486. '']));
  9487. end;
  9488. procedure TTestModule.TestArray_SetLengthOutArg;
  9489. begin
  9490. StartProgram(false);
  9491. Add([
  9492. 'type TArrInt = array of longint;',
  9493. 'procedure DoIt(out a: TArrInt);',
  9494. 'begin',
  9495. ' SetLength(a,2);',
  9496. 'end;',
  9497. 'begin',
  9498. '']);
  9499. ConvertProgram;
  9500. CheckSource('TestArray_SetLengthOutArg',
  9501. LinesToStr([ // statements
  9502. 'this.DoIt = function (a) {',
  9503. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9504. '};',
  9505. '']),
  9506. LinesToStr([
  9507. '']));
  9508. end;
  9509. procedure TTestModule.TestArray_SetLengthProperty;
  9510. begin
  9511. StartProgram(false);
  9512. Add('type');
  9513. Add(' TArrInt = array of longint;');
  9514. Add(' TObject = class');
  9515. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9516. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9517. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9518. Add(' end;');
  9519. Add('var Obj: TObject;');
  9520. Add('begin');
  9521. Add(' SetLength(Obj.Colors,2);');
  9522. ConvertProgram;
  9523. CheckSource('TestArray_SetLengthProperty',
  9524. LinesToStr([ // statements
  9525. 'rtl.createClass(this, "TObject", null, function () {',
  9526. ' this.$init = function () {',
  9527. ' };',
  9528. ' this.$final = function () {',
  9529. ' };',
  9530. '});',
  9531. 'this.Obj = null;',
  9532. '']),
  9533. LinesToStr([
  9534. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9535. '']));
  9536. end;
  9537. procedure TTestModule.TestArray_SetLengthMultiDim;
  9538. begin
  9539. StartProgram(false);
  9540. Add([
  9541. 'type',
  9542. ' TArrArrInt = array of array of longint;',
  9543. ' TArrStaInt = array of array[1..2] of longint;',
  9544. 'var',
  9545. ' a: TArrArrInt;',
  9546. ' b: TArrStaInt;',
  9547. 'begin',
  9548. ' SetLength(a,2);',
  9549. ' SetLength(a,3,4);',
  9550. ' SetLength(b,5);',
  9551. '']);
  9552. ConvertProgram;
  9553. CheckSource('TestArray_SetLengthMultiDim',
  9554. LinesToStr([ // statements
  9555. 'this.a = [];',
  9556. 'this.b = [];',
  9557. '']),
  9558. LinesToStr([
  9559. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9560. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9561. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9562. '']));
  9563. end;
  9564. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9565. begin
  9566. StartProgram(false);
  9567. Add([
  9568. 'type',
  9569. ' TStaArr1 = array[1..3] of boolean;',
  9570. //' TStaArr2 = array[5..6] of TStaArr1;',
  9571. ' TDynArr1StaArr1 = array of TStaArr1;',
  9572. //' TDynArr1StaArr2 = array of TStaArr2;',
  9573. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9574. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9575. 'var',
  9576. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9577. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9578. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9579. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9580. 'begin',
  9581. ' SetLength(DynArr1StaArr1,11);',
  9582. ' SetLength(DynArr2StaArr1,12);',
  9583. ' SetLength(DynArr2StaArr1[13],14);',
  9584. ' SetLength(DynArr2StaArr1,15,16);',
  9585. //' SetLength(DynArr1StaArr2,21);',
  9586. //' SetLength(DynArr2StaArr2,22);',
  9587. //' SetLength(DynArr2StaArr2[23],24);',
  9588. //' SetLength(DynArr2StaArr2,25,26);',
  9589. '']);
  9590. ConvertProgram;
  9591. CheckSource('TestArray_DynOfStatic',
  9592. LinesToStr([ // statements
  9593. 'this.DynArr1StaArr1 = [];',
  9594. 'this.DynArr2StaArr1 = [];',
  9595. '']),
  9596. LinesToStr([ // $mod.$main
  9597. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9598. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9599. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9600. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9601. ' $mod.DynArr2StaArr1,',
  9602. ' false,',
  9603. ' 15,',
  9604. ' 16,',
  9605. ' "s",',
  9606. ' 3',
  9607. ');',
  9608. '']));
  9609. end;
  9610. procedure TTestModule.TestArray_OpenArrayOfString;
  9611. begin
  9612. StartProgram(false);
  9613. Add('procedure DoIt(const a: array of String);');
  9614. Add('var');
  9615. Add(' i: longint;');
  9616. Add(' s: string;');
  9617. Add('begin');
  9618. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9619. Add('end;');
  9620. Add('var s: string;');
  9621. Add('begin');
  9622. Add(' DoIt([]);');
  9623. Add(' DoIt([s,''foo'','''',s+s]);');
  9624. ConvertProgram;
  9625. CheckSource('TestArray_OpenArrayOfString',
  9626. LinesToStr([ // statements
  9627. 'this.DoIt = function (a) {',
  9628. ' var i = 0;',
  9629. ' var s = "";',
  9630. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9631. ' i = $l;',
  9632. ' s = a[rtl.length(a) - i - 1];',
  9633. ' };',
  9634. '};',
  9635. 'this.s = "";',
  9636. '']),
  9637. LinesToStr([
  9638. '$mod.DoIt([]);',
  9639. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9640. '']));
  9641. end;
  9642. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9643. begin
  9644. StartProgram(false);
  9645. Add([
  9646. 'type TArr = array of char;',
  9647. 'var',
  9648. ' c: char;',
  9649. ' s: string;',
  9650. ' a: TArr;',
  9651. 'procedure Run(const a: array of char);',
  9652. 'begin',
  9653. ' Run(c);',
  9654. ' Run(s);',
  9655. 'end;',
  9656. 'begin',
  9657. ' a:=c;',
  9658. ' a:=s;',
  9659. ' a:=#13;',
  9660. ' a:=''Foo'';',
  9661. ' Run(c);',
  9662. ' Run(s);',
  9663. '']);
  9664. ConvertProgram;
  9665. CheckSource('TestArray_ArrayOfCharAssignString',
  9666. LinesToStr([ // statements
  9667. 'this.c = "";',
  9668. 'this.s = "";',
  9669. 'this.a = [];',
  9670. 'this.Run = function (a) {',
  9671. ' $mod.Run($mod.c.split(""));',
  9672. ' $mod.Run($mod.s.split(""));',
  9673. '};',
  9674. '']),
  9675. LinesToStr([
  9676. '$mod.a = $mod.c.split("");',
  9677. '$mod.a = $mod.s.split("");',
  9678. '$mod.a = "\r".split("");',
  9679. '$mod.a = "Foo".split("");',
  9680. '$mod.Run($mod.c.split(""));',
  9681. '$mod.Run($mod.s.split(""));',
  9682. '']));
  9683. end;
  9684. procedure TTestModule.TestArray_ConstRef;
  9685. begin
  9686. StartProgram(false);
  9687. Add([
  9688. 'type TArr = array of word;',
  9689. 'procedure Run(constref a: TArr);',
  9690. 'begin',
  9691. 'end;',
  9692. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9693. 'var l: TArr;',
  9694. 'begin',
  9695. ' Run(l);',
  9696. ' Run(a);',
  9697. ' Run(b);',
  9698. ' Run(c);',
  9699. ' Run(d);',
  9700. ' Run(e);',
  9701. 'end;',
  9702. 'begin',
  9703. '']);
  9704. ConvertProgram;
  9705. CheckResolverUnexpectedHints();
  9706. CheckSource('TestArray_ConstRef',
  9707. LinesToStr([ // statements
  9708. 'this.Run = function (a) {',
  9709. '};',
  9710. 'this.Fly = function (a, b, c, d, e) {',
  9711. ' var l = [];',
  9712. ' $mod.Run(l);',
  9713. ' $mod.Run(a);',
  9714. ' $mod.Run(b.get());',
  9715. ' $mod.Run(c.get());',
  9716. ' $mod.Run(d);',
  9717. ' $mod.Run(e);',
  9718. '};',
  9719. '']),
  9720. LinesToStr([
  9721. '']));
  9722. end;
  9723. procedure TTestModule.TestArray_Concat;
  9724. begin
  9725. StartProgram(false);
  9726. Add([
  9727. 'type',
  9728. ' integer = longint;',
  9729. ' TFlag = (big,small);',
  9730. ' TFlags = set of TFlag;',
  9731. ' TRec = record',
  9732. ' i: integer;',
  9733. ' end;',
  9734. ' TArrInt = array of integer;',
  9735. ' TArrRec = array of TRec;',
  9736. ' TArrFlag = array of TFlag;',
  9737. ' TArrSet = array of TFlags;',
  9738. ' TArrJSValue = array of jsvalue;',
  9739. 'var',
  9740. ' ArrInt: tarrint;',
  9741. ' ArrRec: tarrrec;',
  9742. ' ArrFlag: tarrflag;',
  9743. ' ArrSet: tarrset;',
  9744. ' ArrJSValue: tarrjsvalue;',
  9745. 'begin',
  9746. ' arrint:=concat(arrint);',
  9747. ' arrint:=concat(arrint,arrint);',
  9748. ' arrint:=concat(arrint,arrint,arrint);',
  9749. ' arrrec:=concat(arrrec);',
  9750. ' arrrec:=concat(arrrec,arrrec);',
  9751. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9752. ' arrset:=concat(arrset);',
  9753. ' arrset:=concat(arrset,arrset);',
  9754. ' arrset:=concat(arrset,arrset,arrset);',
  9755. ' arrjsvalue:=concat(arrjsvalue);',
  9756. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9757. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9758. ' arrint:=concat([1],arrint);',
  9759. ' arrflag:=concat([big]);',
  9760. ' arrflag:=concat([big],arrflag);',
  9761. ' arrflag:=concat(arrflag,[small]);',
  9762. '']);
  9763. ConvertProgram;
  9764. CheckSource('TestArray_Concat',
  9765. LinesToStr([ // statements
  9766. 'this.TFlag = {',
  9767. ' "0": "big",',
  9768. ' big: 0,',
  9769. ' "1": "small",',
  9770. ' small: 1',
  9771. '};',
  9772. 'rtl.recNewT(this, "TRec", function () {',
  9773. ' this.i = 0;',
  9774. ' this.$eq = function (b) {',
  9775. ' return this.i === b.i;',
  9776. ' };',
  9777. ' this.$assign = function (s) {',
  9778. ' this.i = s.i;',
  9779. ' return this;',
  9780. ' };',
  9781. '});',
  9782. 'this.ArrInt = [];',
  9783. 'this.ArrRec = [];',
  9784. 'this.ArrFlag = [];',
  9785. 'this.ArrSet = [];',
  9786. 'this.ArrJSValue = [];',
  9787. '']),
  9788. LinesToStr([ // $mod.$main
  9789. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9790. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9791. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9792. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9793. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9794. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9795. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9796. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9797. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9798. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9799. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9800. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9801. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9802. '$mod.ArrFlag = [$mod.TFlag.big];',
  9803. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9804. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9805. '']));
  9806. end;
  9807. procedure TTestModule.TestArray_Copy;
  9808. begin
  9809. StartProgram(false);
  9810. Add([
  9811. 'type',
  9812. ' integer = longint;',
  9813. ' TFlag = (big,small);',
  9814. ' TFlags = set of TFlag;',
  9815. ' TRec = record',
  9816. ' i: integer;',
  9817. ' end;',
  9818. ' TArrInt = array of integer;',
  9819. ' TArrRec = array of TRec;',
  9820. ' TArrSet = array of TFlags;',
  9821. ' TArrJSValue = array of jsvalue;',
  9822. 'var',
  9823. ' ArrInt: tarrint;',
  9824. ' ArrRec: tarrrec;',
  9825. ' ArrSet: tarrset;',
  9826. ' ArrJSValue: tarrjsvalue;',
  9827. 'begin',
  9828. ' arrint:=copy(arrint);',
  9829. ' arrint:=copy(arrint,2);',
  9830. ' arrint:=copy(arrint,3,4);',
  9831. ' arrint:=copy([1,1],1,2);',
  9832. ' arrrec:=copy(arrrec);',
  9833. ' arrrec:=copy(arrrec,5);',
  9834. ' arrrec:=copy(arrrec,6,7);',
  9835. ' arrset:=copy(arrset);',
  9836. ' arrset:=copy(arrset,8);',
  9837. ' arrset:=copy(arrset,9,10);',
  9838. ' arrjsvalue:=copy(arrjsvalue);',
  9839. ' arrjsvalue:=copy(arrjsvalue,11);',
  9840. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9841. ' ']);
  9842. ConvertProgram;
  9843. CheckSource('TestArray_Copy',
  9844. LinesToStr([ // statements
  9845. 'this.TFlag = {',
  9846. ' "0": "big",',
  9847. ' big: 0,',
  9848. ' "1": "small",',
  9849. ' small: 1',
  9850. '};',
  9851. 'rtl.recNewT(this, "TRec", function () {',
  9852. ' this.i = 0;',
  9853. ' this.$eq = function (b) {',
  9854. ' return this.i === b.i;',
  9855. ' };',
  9856. ' this.$assign = function (s) {',
  9857. ' this.i = s.i;',
  9858. ' return this;',
  9859. ' };',
  9860. '});',
  9861. 'this.ArrInt = [];',
  9862. 'this.ArrRec = [];',
  9863. 'this.ArrSet = [];',
  9864. 'this.ArrJSValue = [];',
  9865. '']),
  9866. LinesToStr([ // $mod.$main
  9867. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9868. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9869. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9870. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9871. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9872. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9873. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9874. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9875. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9876. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9877. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9878. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9879. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9880. '']));
  9881. end;
  9882. procedure TTestModule.TestArray_InsertDelete;
  9883. begin
  9884. StartProgram(false);
  9885. Add([
  9886. 'type',
  9887. ' integer = longint;',
  9888. ' TFlag = (big,small);',
  9889. ' TFlags = set of TFlag;',
  9890. ' TRec = record',
  9891. ' i: integer;',
  9892. ' end;',
  9893. ' TArrInt = array of integer;',
  9894. ' TArrRec = array of TRec;',
  9895. ' TArrSet = array of TFlags;',
  9896. ' TArrJSValue = array of jsvalue;',
  9897. ' TArrArrInt = array of TArrInt;',
  9898. 'var',
  9899. ' ArrInt: tarrint;',
  9900. ' ArrRec: tarrrec;',
  9901. ' ArrSet: tarrset;',
  9902. ' ArrJSValue: tarrjsvalue;',
  9903. ' ArrArrInt: TArrArrInt;',
  9904. 'begin',
  9905. ' Insert(1,arrint,2);',
  9906. ' Insert(arrint[3],arrint,4);',
  9907. ' Insert(arrrec[5],arrrec,6);',
  9908. ' Insert(arrset[7],arrset,7);',
  9909. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9910. ' Insert(10,arrjsvalue,11);',
  9911. ' Insert([23],arrarrint,22);',
  9912. ' Delete(arrint,12,13);',
  9913. ' Delete(arrrec,14,15);',
  9914. ' Delete(arrset,17,18);',
  9915. ' Delete(arrjsvalue,19,10);']);
  9916. ConvertProgram;
  9917. CheckSource('TestArray_InsertDelete',
  9918. LinesToStr([ // statements
  9919. 'this.TFlag = {',
  9920. ' "0": "big",',
  9921. ' big: 0,',
  9922. ' "1": "small",',
  9923. ' small: 1',
  9924. '};',
  9925. 'rtl.recNewT(this, "TRec", function () {',
  9926. ' this.i = 0;',
  9927. ' this.$eq = function (b) {',
  9928. ' return this.i === b.i;',
  9929. ' };',
  9930. ' this.$assign = function (s) {',
  9931. ' this.i = s.i;',
  9932. ' return this;',
  9933. ' };',
  9934. '});',
  9935. 'this.ArrInt = [];',
  9936. 'this.ArrRec = [];',
  9937. 'this.ArrSet = [];',
  9938. 'this.ArrJSValue = [];',
  9939. 'this.ArrArrInt = [];',
  9940. '']),
  9941. LinesToStr([ // $mod.$main
  9942. '$mod.ArrInt.splice(2, 0, 1);',
  9943. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9944. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9945. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9946. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9947. '$mod.ArrJSValue.splice(11, 0, 10);',
  9948. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9949. '$mod.ArrInt.splice(12, 13);',
  9950. '$mod.ArrRec.splice(14, 15);',
  9951. '$mod.ArrSet.splice(17, 18);',
  9952. '$mod.ArrJSValue.splice(19, 10);',
  9953. '']));
  9954. end;
  9955. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9956. begin
  9957. Parser.Options:=Parser.Options+[po_cassignments];
  9958. StartProgram(false);
  9959. Add([
  9960. '{$modeswitch arrayoperators}',
  9961. 'type',
  9962. ' integer = longint;',
  9963. ' TArrInt = array of integer;',
  9964. ' TArrStr = array of string;',
  9965. 'const',
  9966. ' Ints: TArrInt = (1,2,3);',
  9967. ' Aliases: TarrStr = (''foo'',''b'');',
  9968. ' OneInt: TArrInt = (7);',
  9969. ' OneStr: array of integer = (7);',
  9970. ' Chars: array of char = ''aoc'';',
  9971. ' Names: array of string = (''a'',''foo'');',
  9972. ' NameCount = low(Names)+high(Names)+length(Names);',
  9973. 'var i: integer;',
  9974. 'begin',
  9975. ' Ints:=[];',
  9976. ' Ints:=[1,1];',
  9977. ' Ints:=[1]+[2];',
  9978. ' Ints:=[2];',
  9979. ' Ints:=[]+ints;',
  9980. ' Ints:=Ints+[];',
  9981. ' Ints:=Ints+OneInt;',
  9982. ' Ints:=Ints+[1,1];',
  9983. ' Ints:=[i,i]+Ints;',
  9984. ' Ints:=[1]+[i]+[3];',
  9985. '']);
  9986. ConvertProgram;
  9987. CheckSource('TestArray_DynArrayConstObjFPC',
  9988. LinesToStr([ // statements
  9989. 'this.Ints = [1, 2, 3];',
  9990. 'this.Aliases = ["foo", "b"];',
  9991. 'this.OneInt = [7];',
  9992. 'this.OneStr = [7];',
  9993. 'this.Chars = ["a", "o", "c"];',
  9994. 'this.Names = ["a", "foo"];',
  9995. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9996. 'this.i = 0;',
  9997. '']),
  9998. LinesToStr([ // $mod.$main
  9999. '$mod.Ints = [];',
  10000. '$mod.Ints = [1, 1];',
  10001. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10002. '$mod.Ints = [2];',
  10003. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10004. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10005. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10006. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10007. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10008. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10009. '']));
  10010. end;
  10011. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10012. begin
  10013. StartProgram(false);
  10014. // Note: const c = [1,1]; defines a set!
  10015. Add([
  10016. '{$mode delphi}',
  10017. 'type',
  10018. ' integer = longint;',
  10019. ' TArrInt = array of integer;',
  10020. ' TArrStr = array of string;',
  10021. 'const',
  10022. ' Ints: TArrInt = [1,1,2];',
  10023. ' Aliases: TarrStr = [''foo'',''b''];',
  10024. ' OneInt: TArrInt = [7];',
  10025. ' OneStr: array of integer = [7]+[8];',
  10026. ' Chars: array of char = ''aoc'';',
  10027. ' Names: array of string = [''a'',''a''];',
  10028. ' NameCount = low(Names)+high(Names)+length(Names);',
  10029. 'begin',
  10030. '']);
  10031. ConvertProgram;
  10032. CheckSource('TestArray_DynArrayConstDelphi',
  10033. LinesToStr([ // statements
  10034. 'this.Ints = [1, 1, 2];',
  10035. 'this.Aliases = ["foo", "b"];',
  10036. 'this.OneInt = [7];',
  10037. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10038. 'this.Chars = ["a", "o", "c"];',
  10039. 'this.Names = ["a", "a"];',
  10040. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10041. '']),
  10042. LinesToStr([ // $mod.$main
  10043. '']));
  10044. end;
  10045. procedure TTestModule.TestArray_ArrayLitAsParam;
  10046. begin
  10047. StartProgram(false);
  10048. Add([
  10049. '{$modeswitch arrayoperators}',
  10050. 'type',
  10051. ' integer = longint;',
  10052. ' TArrInt = array of integer;',
  10053. ' TArrSet = array of (red,green,blue);',
  10054. 'procedure DoOpenInt(const a: array of integer); forward;',
  10055. 'procedure DoInt(const a: TArrInt);',
  10056. 'begin',
  10057. ' DoInt(a+[1]);',
  10058. ' DoInt([1]+a);',
  10059. ' DoOpenInt(a);',
  10060. ' DoOpenInt(a+[1]);',
  10061. ' DoOpenInt([1]+a);',
  10062. 'end;',
  10063. 'procedure DoOpenInt(const a: array of integer);',
  10064. 'begin',
  10065. ' DoOpenInt(a+[1]);',
  10066. ' DoOpenInt([1]+a);',
  10067. ' DoInt(a);',
  10068. ' DoInt(a+[1]);',
  10069. ' DoInt([1]+a);',
  10070. 'end;',
  10071. 'procedure DoSet(const a: TArrSet);',
  10072. 'begin',
  10073. ' DoSet(a+[red]);',
  10074. ' DoSet([blue]+a);',
  10075. 'end;',
  10076. 'var',
  10077. ' i: TArrInt;',
  10078. ' s: TArrSet;',
  10079. 'begin',
  10080. ' DoInt([1]);',
  10081. ' DoInt([1]+[2]);',
  10082. ' DoInt(i+[1]);',
  10083. ' DoInt([1]+i);',
  10084. ' DoOpenInt([1]);',
  10085. ' DoOpenInt([1]+[2]);',
  10086. ' DoOpenInt(i+[1]);',
  10087. ' DoOpenInt([1]+i);',
  10088. ' DoSet([red]);',
  10089. ' DoSet([blue]+[green]);',
  10090. ' DoSet(s+[blue]);',
  10091. ' DoSet([red]+s);',
  10092. '']);
  10093. ConvertProgram;
  10094. CheckSource('TestArray_ArrayLitAsParam',
  10095. LinesToStr([ // statements
  10096. 'this.TArrSet$a = {',
  10097. ' "0": "red",',
  10098. ' red: 0,',
  10099. ' "1": "green",',
  10100. ' green: 1,',
  10101. ' "2": "blue",',
  10102. ' blue: 2',
  10103. '};',
  10104. 'this.DoInt = function (a) {',
  10105. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10106. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10107. ' $mod.DoOpenInt(a);',
  10108. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10109. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10110. '};',
  10111. 'this.DoOpenInt = function (a) {',
  10112. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10113. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10114. ' $mod.DoInt(a);',
  10115. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10116. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10117. '};',
  10118. 'this.DoSet = function (a) {',
  10119. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10120. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10121. '};',
  10122. 'this.i = [];',
  10123. 'this.s = [];',
  10124. '']),
  10125. LinesToStr([ // $mod.$main
  10126. '$mod.DoInt([1]);',
  10127. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10128. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10129. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10130. '$mod.DoOpenInt([1]);',
  10131. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10132. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10133. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10134. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10135. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10136. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10137. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10138. '']));
  10139. end;
  10140. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10141. begin
  10142. StartProgram(false);
  10143. Add([
  10144. '{$modeswitch arrayoperators}',
  10145. 'type',
  10146. ' integer = longint;',
  10147. ' TArrInt = array of integer;',
  10148. ' TArrArrInt = array of TArrInt;',
  10149. 'procedure DoInt(const a: TArrArrInt);',
  10150. 'begin',
  10151. ' DoInt(a+[[1]]);',
  10152. ' DoInt([[1]]+a);',
  10153. ' DoInt(a);',
  10154. 'end;',
  10155. 'var',
  10156. ' i: TArrInt;',
  10157. ' a: TArrArrInt;',
  10158. 'begin',
  10159. ' a:=[[1]];',
  10160. ' a:=[i];',
  10161. ' a:=a+[i];',
  10162. ' a:=[i]+a;',
  10163. ' a:=[[1]+i];',
  10164. ' a:=[[1]+[2]];',
  10165. ' a:=[i+[2]];',
  10166. ' DoInt([[1]]);',
  10167. ' DoInt([[1]+[2],[3,4],[5]]);',
  10168. ' DoInt([i+[1]]+a);',
  10169. ' DoInt([i]+a);',
  10170. '']);
  10171. ConvertProgram;
  10172. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10173. LinesToStr([ // statements
  10174. 'this.DoInt = function (a) {',
  10175. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10176. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10177. ' $mod.DoInt(a);',
  10178. '};',
  10179. 'this.i = [];',
  10180. 'this.a = [];',
  10181. '']),
  10182. LinesToStr([ // $mod.$main
  10183. '$mod.a = [[1]];',
  10184. '$mod.a = [$mod.i];',
  10185. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10186. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10187. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10188. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10189. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10190. '$mod.DoInt([[1]]);',
  10191. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10192. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10193. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10194. '']));
  10195. end;
  10196. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10197. begin
  10198. StartProgram(false);
  10199. Add([
  10200. '{$modeswitch arrayoperators}',
  10201. 'type',
  10202. ' integer = longint;',
  10203. ' TArrInt = array[1..2] of integer;',
  10204. ' TArrArrInt = array of TArrInt;',
  10205. 'procedure DoInt(const a: TArrArrInt);',
  10206. 'begin',
  10207. ' DoInt(a+[[1,2]]);',
  10208. ' DoInt([[1,2]]+a);',
  10209. ' DoInt(a);',
  10210. 'end;',
  10211. 'var',
  10212. ' i: TArrInt;',
  10213. ' a: TArrArrInt;',
  10214. 'begin',
  10215. ' a:=[[1,1]];',
  10216. ' a:=[i];',
  10217. ' a:=a+[i];',
  10218. ' a:=[i]+a;',
  10219. ' DoInt([[1,1]]);',
  10220. ' DoInt([[1,2],[3,4]]);',
  10221. '']);
  10222. ConvertProgram;
  10223. CheckSource('TestArray_ArrayLitStaticAsParam',
  10224. LinesToStr([ // statements
  10225. 'this.DoInt = function (a) {',
  10226. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10227. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10228. ' $mod.DoInt(a);',
  10229. '};',
  10230. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10231. 'this.a = [];',
  10232. '']),
  10233. LinesToStr([ // $mod.$main
  10234. '$mod.a = [[1, 1]];',
  10235. '$mod.a = [$mod.i.slice(0)];',
  10236. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10237. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10238. '$mod.DoInt([[1, 1]]);',
  10239. '$mod.DoInt([[1, 2], [3, 4]]);',
  10240. '']));
  10241. end;
  10242. procedure TTestModule.TestArray_ForInArrOfString;
  10243. begin
  10244. StartProgram(false);
  10245. Add([
  10246. 'type',
  10247. 'type',
  10248. ' TMonthNameArray = array [1..12] of string;',
  10249. ' TMonthNames = TMonthNameArray;',
  10250. ' TObject = class',
  10251. ' private',
  10252. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10253. ' public',
  10254. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10255. ' end;',
  10256. 'var',
  10257. ' f: TObject;',
  10258. ' Month: string;',
  10259. ' Names: array of string = (''a'',''foo'',''bar'');',
  10260. ' i: longint;',
  10261. 'begin',
  10262. ' for Month in f.LongMonthNames do ;',
  10263. ' for Month in Names do ;',
  10264. ' for i:=low(Names) to high(Names) do ;',
  10265. '']);
  10266. ConvertProgram;
  10267. CheckSource('TestArray_ForInArrOfString',
  10268. LinesToStr([ // statements
  10269. 'rtl.createClass(this, "TObject", null, function () {',
  10270. ' this.$init = function () {',
  10271. ' };',
  10272. ' this.$final = function () {',
  10273. ' };',
  10274. '});',
  10275. 'this.f = null;',
  10276. 'this.Month = "";',
  10277. 'this.Names = ["a", "foo", "bar"];',
  10278. 'this.i = 0;',
  10279. '']),
  10280. LinesToStr([ // $mod.$main
  10281. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10282. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10283. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10284. '']));
  10285. end;
  10286. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10287. begin
  10288. StartProgram(false);
  10289. Add([
  10290. '{$modeswitch externalclass}',
  10291. 'type',
  10292. ' TJSObject = class external name ''Object''',
  10293. ' end;',
  10294. ' TJSArray = class external name ''Array''',
  10295. ' class function isArray(Value: JSValue) : boolean;',
  10296. ' function concat() : TJSArray; varargs;',
  10297. ' end;',
  10298. 'var',
  10299. ' aObj: TJSArray;',
  10300. ' a: array of longint;',
  10301. ' o: TJSObject;',
  10302. 'begin',
  10303. ' if TJSArray.isArray(65) then ;',
  10304. ' aObj:=TJSArray(a).concat(a);',
  10305. ' o:=TJSObject(a);']);
  10306. ConvertProgram;
  10307. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10308. LinesToStr([ // statements
  10309. 'this.aObj = null;',
  10310. 'this.a = [];',
  10311. 'this.o = null;',
  10312. '']),
  10313. LinesToStr([ // $mod.$main
  10314. 'if (Array.isArray(65)) ;',
  10315. '$mod.aObj = $mod.a.concat($mod.a);',
  10316. '$mod.o = $mod.a;',
  10317. '']));
  10318. end;
  10319. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10320. begin
  10321. StartProgram(false);
  10322. Add([
  10323. '{$modeswitch externalclass}',
  10324. 'type',
  10325. ' TArrStr = array of string;',
  10326. ' TJSArray = class external name ''Array''',
  10327. ' end;',
  10328. ' TJSObject = class external name ''Object''',
  10329. ' end;',
  10330. 'var',
  10331. ' aObj: TJSArray;',
  10332. ' a: TArrStr;',
  10333. ' jo: TJSObject;',
  10334. 'begin',
  10335. ' a:=TArrStr(aObj);',
  10336. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10337. ' a:=TarrStr(jo);',
  10338. '']);
  10339. ConvertProgram;
  10340. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10341. LinesToStr([ // statements
  10342. 'this.aObj = null;',
  10343. 'this.a = [];',
  10344. 'this.jo = null;',
  10345. '']),
  10346. LinesToStr([ // $mod.$main
  10347. '$mod.a = $mod.aObj;',
  10348. '$mod.aObj[1] = $mod.aObj[2];',
  10349. '$mod.a = $mod.jo;',
  10350. '']));
  10351. end;
  10352. procedure TTestModule.TestArrayOfConst_TVarRec;
  10353. begin
  10354. StartProgram(true,[supTVarRec]);
  10355. Add([
  10356. 'procedure Say(args: array of const);',
  10357. 'var',
  10358. ' i: longint;',
  10359. ' v: TVarRec;',
  10360. 'begin',
  10361. ' for i:=low(args) to high(args) do begin',
  10362. ' v:=args[i];',
  10363. ' case v.vtype of',
  10364. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10365. ' end;',
  10366. ' end;',
  10367. ' for v in args do ;',
  10368. ' args:=nil;',
  10369. ' SetLength(args,2);',
  10370. 'end;',
  10371. 'begin']);
  10372. ConvertProgram;
  10373. CheckSource('TestArrayOfConst_TVarRec',
  10374. LinesToStr([ // statements
  10375. 'this.Say = function (args) {',
  10376. ' var i = 0;',
  10377. ' var v = pas.system.TVarRec.$new();',
  10378. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10379. ' i = $l;',
  10380. ' v.$assign(args[i]);',
  10381. ' var $tmp = v.VType;',
  10382. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10383. ' };',
  10384. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10385. ' args = [];',
  10386. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10387. '};',
  10388. '']),
  10389. LinesToStr([ // $mod.$main
  10390. ]));
  10391. end;
  10392. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10393. begin
  10394. StartProgram(true,[supTVarRec]);
  10395. Add([
  10396. 'procedure Say(args: array of const);',
  10397. 'begin',
  10398. ' Say(args);',
  10399. 'end;',
  10400. 'var',
  10401. ' p: Pointer;',
  10402. ' j: jsvalue;',
  10403. ' c: currency;',
  10404. 'begin',
  10405. ' Say([]);',
  10406. ' Say([1]);',
  10407. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10408. '']);
  10409. ConvertProgram;
  10410. CheckSource('TestArrayOfConst_PassBaseTypes',
  10411. LinesToStr([ // statements
  10412. 'this.Say = function (args) {',
  10413. ' $mod.Say(args);',
  10414. '};',
  10415. 'this.p = null;',
  10416. 'this.j = undefined;',
  10417. 'this.c = 0;',
  10418. '']),
  10419. LinesToStr([ // $mod.$main
  10420. '$mod.Say([]);',
  10421. '$mod.Say(pas.system.VarRecs(0, 1));',
  10422. '$mod.Say(pas.system.VarRecs(',
  10423. ' 9,',
  10424. ' "c",',
  10425. ' 18,',
  10426. ' "foo",',
  10427. ' 5,',
  10428. ' null,',
  10429. ' 1,',
  10430. ' true,',
  10431. ' 3,',
  10432. ' 1.3,',
  10433. ' 5,',
  10434. ' $mod.p,',
  10435. ' 20,',
  10436. ' $mod.j,',
  10437. ' 12,',
  10438. ' $mod.c',
  10439. ' ));',
  10440. '']));
  10441. end;
  10442. procedure TTestModule.TestArrayOfConst_PassObj;
  10443. begin
  10444. StartProgram(true,[supTVarRec]);
  10445. Add([
  10446. '{$interfaces corba}',
  10447. 'type',
  10448. ' TObject = class',
  10449. ' end;',
  10450. ' TClass = class of TObject;',
  10451. ' IUnknown = interface',
  10452. ' end;',
  10453. 'procedure Say(args: array of const);',
  10454. 'begin',
  10455. 'end;',
  10456. 'var',
  10457. ' o: TObject;',
  10458. ' c: TClass;',
  10459. ' i: IUnknown;',
  10460. 'begin',
  10461. ' Say([o,c,TObject]);',
  10462. ' Say([nil,i]);',
  10463. '']);
  10464. ConvertProgram;
  10465. CheckSource('TestArrayOfConst_PassObj',
  10466. LinesToStr([ // statements
  10467. 'rtl.createClass(this, "TObject", null, function () {',
  10468. ' this.$init = function () {',
  10469. ' };',
  10470. ' this.$final = function () {',
  10471. ' };',
  10472. '});',
  10473. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10474. 'this.Say = function (args) {',
  10475. '};',
  10476. 'this.o = null;',
  10477. 'this.c = null;',
  10478. 'this.i = null;',
  10479. '']),
  10480. LinesToStr([ // $mod.$main
  10481. '$mod.Say(pas.system.VarRecs(',
  10482. ' 7,',
  10483. ' $mod.o,',
  10484. ' 8,',
  10485. ' $mod.c,',
  10486. ' 8,',
  10487. ' $mod.TObject',
  10488. '));',
  10489. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10490. '']));
  10491. end;
  10492. procedure TTestModule.TestRecord_Empty;
  10493. begin
  10494. StartProgram(false);
  10495. Add([
  10496. 'type',
  10497. ' TRecA = record',
  10498. ' end;',
  10499. 'var a,b: TRecA;',
  10500. 'begin',
  10501. ' if a=b then ;']);
  10502. ConvertProgram;
  10503. CheckSource('TestRecord_Empty',
  10504. LinesToStr([ // statements
  10505. 'rtl.recNewT(this, "TRecA", function () {',
  10506. ' this.$eq = function (b) {',
  10507. ' return true;',
  10508. ' };',
  10509. ' this.$assign = function (s) {',
  10510. ' return this;',
  10511. ' };',
  10512. '});',
  10513. 'this.a = this.TRecA.$new();',
  10514. 'this.b = this.TRecA.$new();',
  10515. '']),
  10516. LinesToStr([ // $mod.$main
  10517. 'if ($mod.a.$eq($mod.b)) ;'
  10518. ]));
  10519. end;
  10520. procedure TTestModule.TestRecord_Var;
  10521. begin
  10522. StartProgram(false);
  10523. Add('type');
  10524. Add(' TRecA = record');
  10525. Add(' Bold: longint;');
  10526. Add(' end;');
  10527. Add('var Rec: TRecA;');
  10528. Add('begin');
  10529. Add(' rec.bold:=123');
  10530. ConvertProgram;
  10531. CheckSource('TestRecord_Var',
  10532. LinesToStr([ // statements
  10533. 'rtl.recNewT(this, "TRecA", function () {',
  10534. ' this.Bold = 0;',
  10535. ' this.$eq = function (b) {',
  10536. ' return this.Bold === b.Bold;',
  10537. ' };',
  10538. ' this.$assign = function (s) {',
  10539. ' this.Bold = s.Bold;',
  10540. ' return this;',
  10541. ' };',
  10542. '});',
  10543. 'this.Rec = this.TRecA.$new();',
  10544. '']),
  10545. LinesToStr([ // $mod.$main
  10546. '$mod.Rec.Bold = 123;'
  10547. ]));
  10548. end;
  10549. procedure TTestModule.TestRecord_VarExternal;
  10550. begin
  10551. StartProgram(false);
  10552. Add([
  10553. '{$modeswitch externalclass}',
  10554. 'type',
  10555. ' TRecA = record',
  10556. ' i: byte;',
  10557. ' length_: longint external name ''length'';',
  10558. ' end;',
  10559. 'var Rec: TRecA;',
  10560. 'begin',
  10561. ' rec.length_ := rec.length_',
  10562. '']);
  10563. ConvertProgram;
  10564. CheckSource('TestRecord_VarExternal',
  10565. LinesToStr([ // statements
  10566. 'rtl.recNewT(this, "TRecA", function () {',
  10567. ' this.i = 0;',
  10568. ' this.$eq = function (b) {',
  10569. ' return (this.i === b.i) && (this.length === b.length);',
  10570. ' };',
  10571. ' this.$assign = function (s) {',
  10572. ' this.i = s.i;',
  10573. ' this.length = s.length;',
  10574. ' return this;',
  10575. ' };',
  10576. '});',
  10577. 'this.Rec = this.TRecA.$new();',
  10578. '']),
  10579. LinesToStr([ // $mod.$main
  10580. '$mod.Rec.length = $mod.Rec.length;'
  10581. ]));
  10582. end;
  10583. procedure TTestModule.TestRecord_WithDo;
  10584. begin
  10585. StartProgram(false);
  10586. Add('type');
  10587. Add(' TRec = record');
  10588. Add(' vI: longint;');
  10589. Add(' end;');
  10590. Add('var');
  10591. Add(' Int: longint;');
  10592. Add(' r: TRec;');
  10593. Add('begin');
  10594. Add(' with r do');
  10595. Add(' int:=vi;');
  10596. Add(' with r do begin');
  10597. Add(' int:=vi;');
  10598. Add(' vi:=int;');
  10599. Add(' end;');
  10600. ConvertProgram;
  10601. CheckSource('TestWithRecordDo',
  10602. LinesToStr([ // statements
  10603. 'rtl.recNewT(this, "TRec", function () {',
  10604. ' this.vI = 0;',
  10605. ' this.$eq = function (b) {',
  10606. ' return this.vI === b.vI;',
  10607. ' };',
  10608. ' this.$assign = function (s) {',
  10609. ' this.vI = s.vI;',
  10610. ' return this;',
  10611. ' };',
  10612. '});',
  10613. 'this.Int = 0;',
  10614. 'this.r = this.TRec.$new();',
  10615. '']),
  10616. LinesToStr([ // $mod.$main
  10617. 'var $with = $mod.r;',
  10618. '$mod.Int = $with.vI;',
  10619. 'var $with1 = $mod.r;',
  10620. '$mod.Int = $with1.vI;',
  10621. '$with1.vI = $mod.Int;'
  10622. ]));
  10623. end;
  10624. procedure TTestModule.TestRecord_Assign;
  10625. begin
  10626. StartProgram(false);
  10627. Add('type');
  10628. Add(' TEnum = (red,green);');
  10629. Add(' TEnums = set of TEnum;');
  10630. Add(' TSmallRec = record');
  10631. Add(' N: longint;');
  10632. Add(' end;');
  10633. Add(' TBigRec = record');
  10634. Add(' Int: longint;');
  10635. Add(' D: double;');
  10636. Add(' Arr: array of longint;');
  10637. Add(' Arr2: array[1..2] of longint;');
  10638. Add(' Small: TSmallRec;');
  10639. Add(' Enums: TEnums;');
  10640. Add(' end;');
  10641. Add('var');
  10642. Add(' r, s: TBigRec;');
  10643. Add('begin');
  10644. Add(' r:=s;');
  10645. Add(' r:=default(TBigRec);');
  10646. Add(' r:=default(s);');
  10647. ConvertProgram;
  10648. CheckSource('TestRecord_Assign',
  10649. LinesToStr([ // statements
  10650. 'this.TEnum = {',
  10651. ' "0": "red",',
  10652. ' red: 0,',
  10653. ' "1": "green",',
  10654. ' green: 1',
  10655. '};',
  10656. 'rtl.recNewT(this, "TSmallRec", function () {',
  10657. ' this.N = 0;',
  10658. ' this.$eq = function (b) {',
  10659. ' return this.N === b.N;',
  10660. ' };',
  10661. ' this.$assign = function (s) {',
  10662. ' this.N = s.N;',
  10663. ' return this;',
  10664. ' };',
  10665. '});',
  10666. 'rtl.recNewT(this, "TBigRec", function () {',
  10667. ' this.Int = 0;',
  10668. ' this.D = 0.0;',
  10669. ' this.$new = function () {',
  10670. ' var r = Object.create(this);',
  10671. ' r.Arr = [];',
  10672. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10673. ' r.Small = $mod.TSmallRec.$new();',
  10674. ' r.Enums = {};',
  10675. ' return r;',
  10676. ' };',
  10677. ' this.$eq = function (b) {',
  10678. ' 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);',
  10679. ' };',
  10680. ' this.$assign = function (s) {',
  10681. ' this.Int = s.Int;',
  10682. ' this.D = s.D;',
  10683. ' this.Arr = rtl.arrayRef(s.Arr);',
  10684. ' this.Arr2 = s.Arr2.slice(0);',
  10685. ' this.Small.$assign(s.Small);',
  10686. ' this.Enums = rtl.refSet(s.Enums);',
  10687. ' return this;',
  10688. ' };',
  10689. '});',
  10690. 'this.r = this.TBigRec.$new();',
  10691. 'this.s = this.TBigRec.$new();',
  10692. '']),
  10693. LinesToStr([ // $mod.$main
  10694. '$mod.r.$assign($mod.s);',
  10695. '$mod.r.$assign($mod.TBigRec.$new());',
  10696. '$mod.r.$assign($mod.TBigRec.$new());',
  10697. '']));
  10698. end;
  10699. procedure TTestModule.TestRecord_AsParams;
  10700. begin
  10701. StartProgram(false);
  10702. Add([
  10703. 'type',
  10704. ' integer = longint;',
  10705. ' TRecord = record',
  10706. ' i: integer;',
  10707. ' end;',
  10708. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10709. 'var vL: TRecord;',
  10710. 'begin',
  10711. ' vd:=vd;',
  10712. ' vd.i:=vd.i;',
  10713. ' vl:=vc;',
  10714. ' vv:=vv;',
  10715. ' vv.i:=vv.i;',
  10716. ' U:=vl;',
  10717. ' U:=vd;',
  10718. ' U:=vc;',
  10719. ' U:=vv;',
  10720. ' vl:=TRecord(U);',
  10721. ' vd:=TRecord(U);',
  10722. ' vv:=TRecord(U);',
  10723. ' doit(vd,vd,vd,vd);',
  10724. ' doit(vc,vc,vl,vl);',
  10725. ' doit(vv,vv,vv,vv);',
  10726. ' doit(vl,vl,vl,vl);',
  10727. ' TRecord(U).i:=3;',
  10728. 'end;',
  10729. 'var i: TRecord;',
  10730. 'begin',
  10731. ' doit(i,i,i,i);',
  10732. '']);
  10733. ConvertProgram;
  10734. CheckSource('TestRecord_AsParams',
  10735. LinesToStr([ // statements
  10736. 'rtl.recNewT(this, "TRecord", function () {',
  10737. ' this.i = 0;',
  10738. ' this.$eq = function (b) {',
  10739. ' return this.i === b.i;',
  10740. ' };',
  10741. ' this.$assign = function (s) {',
  10742. ' this.i = s.i;',
  10743. ' return this;',
  10744. ' };',
  10745. '});',
  10746. 'this.DoIt = function (vD, vC, vV, U) {',
  10747. ' var vL = $mod.TRecord.$new();',
  10748. ' vD.$assign(vD);',
  10749. ' vD.i = vD.i;',
  10750. ' vL.$assign(vC);',
  10751. ' vV.$assign(vV);',
  10752. ' vV.i = vV.i;',
  10753. ' U.$assign(vL);',
  10754. ' U.$assign(vD);',
  10755. ' U.$assign(vC);',
  10756. ' U.$assign(vV);',
  10757. ' vL.$assign(U);',
  10758. ' vD.$assign(U);',
  10759. ' vV.$assign(U);',
  10760. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10761. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10762. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10763. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10764. ' U.i = 3;',
  10765. '};',
  10766. 'this.i = this.TRecord.$new();'
  10767. ]),
  10768. LinesToStr([
  10769. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10770. '']));
  10771. end;
  10772. procedure TTestModule.TestRecord_ConstRef;
  10773. begin
  10774. StartProgram(false);
  10775. Add([
  10776. 'type TRec = record i: word; end;',
  10777. 'procedure Run(constref a: TRec);',
  10778. 'begin',
  10779. 'end;',
  10780. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10781. 'var l: TRec;',
  10782. 'begin',
  10783. ' Run(l);',
  10784. ' Run(a);',
  10785. ' Run(b);',
  10786. ' Run(c);',
  10787. ' Run(d);',
  10788. ' Run(e);',
  10789. 'end;',
  10790. 'begin',
  10791. '']);
  10792. ConvertProgram;
  10793. CheckResolverUnexpectedHints();
  10794. CheckSource('TestRecord_ConstRef',
  10795. LinesToStr([ // statements
  10796. 'rtl.recNewT(this, "TRec", function () {',
  10797. ' this.i = 0;',
  10798. ' this.$eq = function (b) {',
  10799. ' return this.i === b.i;',
  10800. ' };',
  10801. ' this.$assign = function (s) {',
  10802. ' this.i = s.i;',
  10803. ' return this;',
  10804. ' };',
  10805. '});',
  10806. 'this.Run = function (a) {',
  10807. '};',
  10808. 'this.Fly = function (a, b, c, d, e) {',
  10809. ' var l = $mod.TRec.$new();',
  10810. ' $mod.Run(l);',
  10811. ' $mod.Run(a);',
  10812. ' $mod.Run(b);',
  10813. ' $mod.Run(c);',
  10814. ' $mod.Run(d);',
  10815. ' $mod.Run(e);',
  10816. '};',
  10817. '']),
  10818. LinesToStr([
  10819. '']));
  10820. end;
  10821. procedure TTestModule.TestRecordElement_AsParams;
  10822. begin
  10823. StartProgram(false);
  10824. Add('type');
  10825. Add(' integer = longint;');
  10826. Add(' TRecord = record');
  10827. Add(' i: integer;');
  10828. Add(' end;');
  10829. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10830. Add('var vJ: TRecord;');
  10831. Add('begin');
  10832. Add(' doit(vj.i,vj.i,vj.i);');
  10833. Add('end;');
  10834. Add('var r: TRecord;');
  10835. Add('begin');
  10836. Add(' doit(r.i,r.i,r.i);');
  10837. ConvertProgram;
  10838. CheckSource('TestRecordElement_AsParams',
  10839. LinesToStr([ // statements
  10840. 'rtl.recNewT(this, "TRecord", function () {',
  10841. ' this.i = 0;',
  10842. ' this.$eq = function (b) {',
  10843. ' return this.i === b.i;',
  10844. ' };',
  10845. ' this.$assign = function (s) {',
  10846. ' this.i = s.i;',
  10847. ' return this;',
  10848. ' };',
  10849. '});',
  10850. 'this.DoIt = function (vG,vH,vI) {',
  10851. ' var vJ = $mod.TRecord.$new();',
  10852. ' $mod.DoIt(vJ.i, vJ.i, {',
  10853. ' p: vJ,',
  10854. ' get: function () {',
  10855. ' return this.p.i;',
  10856. ' },',
  10857. ' set: function (v) {',
  10858. ' this.p.i = v;',
  10859. ' }',
  10860. ' });',
  10861. '};',
  10862. 'this.r = this.TRecord.$new();'
  10863. ]),
  10864. LinesToStr([
  10865. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10866. ' p: $mod.r,',
  10867. ' get: function () {',
  10868. ' return this.p.i;',
  10869. ' },',
  10870. ' set: function (v) {',
  10871. ' this.p.i = v;',
  10872. ' }',
  10873. '});'
  10874. ]));
  10875. end;
  10876. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10877. begin
  10878. StartProgram(false);
  10879. Add('type');
  10880. Add(' integer = longint;');
  10881. Add(' TRecord = record');
  10882. Add(' i: integer;');
  10883. Add(' end;');
  10884. Add('function GetRec(vB: integer = 0): TRecord;');
  10885. Add('begin');
  10886. Add('end;');
  10887. Add('procedure DoIt(vG: integer; const vH: integer);');
  10888. Add('begin');
  10889. Add('end;');
  10890. Add('begin');
  10891. Add(' doit(getrec.i,getrec.i);');
  10892. Add(' doit(getrec().i,getrec().i);');
  10893. Add(' doit(getrec(1).i,getrec(2).i);');
  10894. ConvertProgram;
  10895. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10896. LinesToStr([ // statements
  10897. 'rtl.recNewT(this, "TRecord", function () {',
  10898. ' this.i = 0;',
  10899. ' this.$eq = function (b) {',
  10900. ' return this.i === b.i;',
  10901. ' };',
  10902. ' this.$assign = function (s) {',
  10903. ' this.i = s.i;',
  10904. ' return this;',
  10905. ' };',
  10906. '});',
  10907. 'this.GetRec = function (vB) {',
  10908. ' var Result = $mod.TRecord.$new();',
  10909. ' return Result;',
  10910. '};',
  10911. 'this.DoIt = function (vG, vH) {',
  10912. '};',
  10913. '']),
  10914. LinesToStr([
  10915. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10916. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10917. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10918. '']));
  10919. end;
  10920. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10921. begin
  10922. StartProgram(false);
  10923. Add('type');
  10924. Add(' integer = longint;');
  10925. Add(' TRecord = record');
  10926. Add(' i: integer;');
  10927. Add(' end;');
  10928. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10929. Add('begin');
  10930. Add('end;');
  10931. Add('var r: trecord;');
  10932. Add('begin');
  10933. Add(' with r do ');
  10934. Add(' doit(i,i,i);');
  10935. ConvertProgram;
  10936. CheckSource('TestRecordElementFromWith_AsParams',
  10937. LinesToStr([ // statements
  10938. 'rtl.recNewT(this, "TRecord", function () {',
  10939. ' this.i = 0;',
  10940. ' this.$eq = function (b) {',
  10941. ' return this.i === b.i;',
  10942. ' };',
  10943. ' this.$assign = function (s) {',
  10944. ' this.i = s.i;',
  10945. ' return this;',
  10946. ' };',
  10947. '});',
  10948. 'this.DoIt = function (vG,vH,vI) {',
  10949. '};',
  10950. 'this.r = this.TRecord.$new();'
  10951. ]),
  10952. LinesToStr([
  10953. 'var $with = $mod.r;',
  10954. '$mod.DoIt($with.i,$with.i,{',
  10955. ' p: $with,',
  10956. ' get: function () {',
  10957. ' return this.p.i;',
  10958. ' },',
  10959. ' set: function (v) {',
  10960. ' this.p.i = v;',
  10961. ' }',
  10962. '});',
  10963. '']));
  10964. end;
  10965. procedure TTestModule.TestRecord_Equal;
  10966. begin
  10967. StartProgram(false);
  10968. Add('type');
  10969. Add(' integer = longint;');
  10970. Add(' TFlag = (red,blue);');
  10971. Add(' TFlags = set of TFlag;');
  10972. Add(' TProc = procedure;');
  10973. Add(' TRecord = record');
  10974. Add(' i: integer;');
  10975. Add(' Event: TProc;');
  10976. Add(' f: TFlags;');
  10977. Add(' end;');
  10978. Add(' TNested = record');
  10979. Add(' r: TRecord;');
  10980. Add(' end;');
  10981. Add('var');
  10982. Add(' b: boolean;');
  10983. Add(' r,s: trecord;');
  10984. Add('begin');
  10985. Add(' b:=r=s;');
  10986. Add(' b:=r<>s;');
  10987. ConvertProgram;
  10988. CheckSource('TestRecord_Equal',
  10989. LinesToStr([ // statements
  10990. 'this.TFlag = {',
  10991. ' "0": "red",',
  10992. ' red: 0,',
  10993. ' "1": "blue",',
  10994. ' blue: 1',
  10995. '};',
  10996. 'rtl.recNewT(this, "TRecord", function () {',
  10997. ' this.i = 0;',
  10998. ' this.Event = null;',
  10999. ' this.$new = function () {',
  11000. ' var r = Object.create(this);',
  11001. ' r.f = {};',
  11002. ' return r;',
  11003. ' };',
  11004. ' this.$eq = function (b) {',
  11005. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11006. ' };',
  11007. ' this.$assign = function (s) {',
  11008. ' this.i = s.i;',
  11009. ' this.Event = s.Event;',
  11010. ' this.f = rtl.refSet(s.f);',
  11011. ' return this;',
  11012. ' };',
  11013. '});',
  11014. 'rtl.recNewT(this, "TNested", function () {',
  11015. ' this.$new = function () {',
  11016. ' var r = Object.create(this);',
  11017. ' r.r = $mod.TRecord.$new();',
  11018. ' return r;',
  11019. ' };',
  11020. ' this.$eq = function (b) {',
  11021. ' return this.r.$eq(b.r);',
  11022. ' };',
  11023. ' this.$assign = function (s) {',
  11024. ' this.r.$assign(s.r);',
  11025. ' return this;',
  11026. ' };',
  11027. '});',
  11028. 'this.b = false;',
  11029. 'this.r = this.TRecord.$new();',
  11030. 'this.s = this.TRecord.$new();',
  11031. '']),
  11032. LinesToStr([
  11033. '$mod.b = $mod.r.$eq($mod.s);',
  11034. '$mod.b = !$mod.r.$eq($mod.s);',
  11035. '']));
  11036. end;
  11037. procedure TTestModule.TestRecord_JSValue;
  11038. begin
  11039. StartProgram(false);
  11040. Add([
  11041. 'type',
  11042. ' TRecord = record',
  11043. ' i: longint;',
  11044. ' end;',
  11045. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11046. 'begin',
  11047. 'end;',
  11048. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11049. 'begin',
  11050. ' if jsvalue(d) then ;',
  11051. ' if jsvalue(c) then ;',
  11052. ' if jsvalue(v) then ;',
  11053. 'end;',
  11054. 'var',
  11055. ' Jv: jsvalue;',
  11056. ' Rec: trecord;',
  11057. 'begin',
  11058. ' rec:=trecord(jv);',
  11059. ' jv:=rec;',
  11060. ' Fly(rec,rec);',
  11061. ' Fly(@rec,@rec);',
  11062. ' if jsvalue(Rec) then ;',
  11063. ' Run(trecord(jv),trecord(jv),rec);',
  11064. '']);
  11065. ConvertProgram;
  11066. CheckSource('TestRecord_JSValue',
  11067. LinesToStr([ // statements
  11068. 'rtl.recNewT(this, "TRecord", function () {',
  11069. ' this.i = 0;',
  11070. ' this.$eq = function (b) {',
  11071. ' return this.i === b.i;',
  11072. ' };',
  11073. ' this.$assign = function (s) {',
  11074. ' this.i = s.i;',
  11075. ' return this;',
  11076. ' };',
  11077. '});',
  11078. 'this.Fly = function (d, c) {',
  11079. '};',
  11080. 'this.Run = function (d, c, v) {',
  11081. ' if (d) ;',
  11082. ' if (c) ;',
  11083. ' if (v) ;',
  11084. '};',
  11085. 'this.Jv = undefined;',
  11086. 'this.Rec = this.TRecord.$new();',
  11087. '']),
  11088. LinesToStr([
  11089. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11090. '$mod.Jv = $mod.Rec;',
  11091. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11092. '$mod.Fly($mod.Rec, $mod.Rec);',
  11093. 'if ($mod.Rec) ;',
  11094. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11095. '']));
  11096. end;
  11097. procedure TTestModule.TestRecord_VariantFail;
  11098. begin
  11099. StartProgram(false);
  11100. Add([
  11101. 'type',
  11102. ' TRec = record',
  11103. ' case word of',
  11104. ' 0: (b0, b1: Byte);',
  11105. ' 1: (i: word);',
  11106. ' end;',
  11107. 'begin']);
  11108. SetExpectedPasResolverError('variant record is not supported',
  11109. nXIsNotSupported);
  11110. ConvertProgram;
  11111. end;
  11112. procedure TTestModule.TestRecord_FieldArray;
  11113. begin
  11114. StartProgram(false);
  11115. Add([
  11116. 'type',
  11117. ' TArrInt = array[3..4] of longint;',
  11118. ' TArrArrInt = array[3..4] of longint;',
  11119. ' TRec = record',
  11120. ' a: array of longint;',
  11121. ' s: array[1..2] of longint;',
  11122. ' m: array[1..2,3..4] of longint;',
  11123. ' o: TArrArrInt;',
  11124. ' end;',
  11125. 'begin']);
  11126. ConvertProgram;
  11127. CheckSource('TestRecord_FieldArray',
  11128. LinesToStr([ // statements
  11129. 'rtl.recNewT(this, "TRec", function () {',
  11130. ' this.$new = function () {',
  11131. ' var r = Object.create(this);',
  11132. ' r.a = [];',
  11133. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11134. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11135. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11136. ' return r;',
  11137. ' };',
  11138. ' this.$eq = function (b) {',
  11139. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11140. ' };',
  11141. ' this.$assign = function (s) {',
  11142. ' this.a = rtl.arrayRef(s.a);',
  11143. ' this.s = s.s.slice(0);',
  11144. ' this.m = s.m.slice(0);',
  11145. ' this.o = s.o.slice(0);',
  11146. ' return this;',
  11147. ' };',
  11148. '});',
  11149. '']),
  11150. LinesToStr([ // $mod.$main
  11151. '']));
  11152. end;
  11153. procedure TTestModule.TestRecord_Const;
  11154. begin
  11155. StartProgram(false);
  11156. Add([
  11157. 'type',
  11158. ' TArrInt = array[3..4] of longint;',
  11159. ' TPoint = record x,y: longint; end;',
  11160. ' TRec = record',
  11161. ' i: longint;',
  11162. ' a: array of longint;',
  11163. ' s: array[1..2] of longint;',
  11164. ' m: array[1..2,3..4] of longint;',
  11165. ' p: TPoint;',
  11166. ' end;',
  11167. ' TPoints = array of TPoint;',
  11168. 'const',
  11169. ' r: TRec = (',
  11170. ' i:1;',
  11171. ' a:(2,3);',
  11172. ' s:(4,5);',
  11173. ' m:( (11,12), (13,14) );',
  11174. ' p: (x:21; y:22)',
  11175. ' );',
  11176. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11177. 'begin']);
  11178. ConvertProgram;
  11179. CheckSource('TestRecord_Const',
  11180. LinesToStr([ // statements
  11181. 'rtl.recNewT(this, "TPoint", function () {',
  11182. ' this.x = 0;',
  11183. ' this.y = 0;',
  11184. ' this.$eq = function (b) {',
  11185. ' return (this.x === b.x) && (this.y === b.y);',
  11186. ' };',
  11187. ' this.$assign = function (s) {',
  11188. ' this.x = s.x;',
  11189. ' this.y = s.y;',
  11190. ' return this;',
  11191. ' };',
  11192. '});',
  11193. 'rtl.recNewT(this, "TRec", function () {',
  11194. ' this.i = 0;',
  11195. ' this.$new = function () {',
  11196. ' var r = Object.create(this);',
  11197. ' r.a = [];',
  11198. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11199. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11200. ' r.p = $mod.TPoint.$new();',
  11201. ' return r;',
  11202. ' };',
  11203. ' this.$eq = function (b) {',
  11204. ' 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);',
  11205. ' };',
  11206. ' this.$assign = function (s) {',
  11207. ' this.i = s.i;',
  11208. ' this.a = rtl.arrayRef(s.a);',
  11209. ' this.s = s.s.slice(0);',
  11210. ' this.m = s.m.slice(0);',
  11211. ' this.p.$assign(s.p);',
  11212. ' return this;',
  11213. ' };',
  11214. '});',
  11215. 'this.r = this.TRec.$clone({',
  11216. ' i: 1,',
  11217. ' a: [2, 3],',
  11218. ' s: [4, 5],',
  11219. ' m: [[11, 12], [13, 14]],',
  11220. ' p: this.TPoint.$clone({',
  11221. ' x: 21,',
  11222. ' y: 22',
  11223. ' })',
  11224. '});',
  11225. 'this.p = [this.TPoint.$clone({',
  11226. ' x: 1,',
  11227. ' y: 2',
  11228. '}), this.TPoint.$clone({',
  11229. ' x: 3,',
  11230. ' y: 4',
  11231. '})];',
  11232. '']),
  11233. LinesToStr([ // $mod.$main
  11234. '']));
  11235. end;
  11236. procedure TTestModule.TestRecord_TypecastFail;
  11237. begin
  11238. StartProgram(false);
  11239. Add([
  11240. 'type',
  11241. ' TPoint = record x,y: longint; end;',
  11242. ' TRec = record l: longint end;',
  11243. 'var p: TPoint;',
  11244. 'begin',
  11245. ' if TRec(p).l=2 then ;']);
  11246. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11247. nIllegalTypeConversionTo);
  11248. ConvertProgram;
  11249. end;
  11250. procedure TTestModule.TestRecord_InFunction;
  11251. begin
  11252. StartProgram(false);
  11253. Add([
  11254. 'var TPoint: longint = 3;',
  11255. 'procedure DoIt;',
  11256. 'type',
  11257. ' TPoint = record x,y: longint; end;',
  11258. ' TPoints = array of TPoint;',
  11259. 'var',
  11260. ' r: TPoint;',
  11261. ' p: TPoints;',
  11262. 'begin',
  11263. ' SetLength(p,2);',
  11264. 'end;',
  11265. 'begin']);
  11266. ConvertProgram;
  11267. CheckSource('TestRecord_InFunction',
  11268. LinesToStr([ // statements
  11269. 'this.TPoint = 3;',
  11270. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11271. ' this.x = 0;',
  11272. ' this.y = 0;',
  11273. ' this.$eq = function (b) {',
  11274. ' return (this.x === b.x) && (this.y === b.y);',
  11275. ' };',
  11276. ' this.$assign = function (s) {',
  11277. ' this.x = s.x;',
  11278. ' this.y = s.y;',
  11279. ' return this;',
  11280. ' };',
  11281. '});',
  11282. 'this.DoIt = function () {',
  11283. ' var r = TPoint$1.$new();',
  11284. ' var p = [];',
  11285. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11286. '};',
  11287. '']),
  11288. LinesToStr([ // $mod.$main
  11289. '']));
  11290. end;
  11291. procedure TTestModule.TestRecord_AnonymousFail;
  11292. begin
  11293. StartProgram(false);
  11294. Add([
  11295. 'var',
  11296. ' r: record x: word end;',
  11297. 'begin']);
  11298. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11299. nNotYetImplemented);
  11300. ConvertProgram;
  11301. end;
  11302. procedure TTestModule.TestAdvRecord_Function;
  11303. begin
  11304. StartProgram(false);
  11305. Parser.Options:=Parser.Options+[po_cassignments];
  11306. Add([
  11307. '{$modeswitch AdvancedRecords}',
  11308. 'type',
  11309. ' TPoint = record',
  11310. ' x,y: word;',
  11311. ' function Add(const apt: TPoint): TPoint;',
  11312. ' end;',
  11313. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11314. 'begin',
  11315. ' Result:=Self;',
  11316. ' Result.x+=apt.x;',
  11317. ' Result.y:=Result.y+apt.y;',
  11318. ' Self:=apt;',
  11319. 'end;',
  11320. 'var p,q: TPoint;',
  11321. 'begin',
  11322. ' p.add(q);',
  11323. ' p:=default(TPoint);',
  11324. ' p:=q;',
  11325. '']);
  11326. ConvertProgram;
  11327. CheckSource('TestAdvRecord_Function',
  11328. LinesToStr([ // statements
  11329. 'rtl.recNewT(this, "TPoint", function () {',
  11330. ' this.x = 0;',
  11331. ' this.y = 0;',
  11332. ' this.$eq = function (b) {',
  11333. ' return (this.x === b.x) && (this.y === b.y);',
  11334. ' };',
  11335. ' this.$assign = function (s) {',
  11336. ' this.x = s.x;',
  11337. ' this.y = s.y;',
  11338. ' return this;',
  11339. ' };',
  11340. ' this.Add = function (apt) {',
  11341. ' var Result = $mod.TPoint.$new();',
  11342. ' Result.$assign(this);',
  11343. ' Result.x += apt.x;',
  11344. ' Result.y = Result.y + apt.y;',
  11345. ' this.$assign(apt);',
  11346. ' return Result;',
  11347. ' };',
  11348. '});',
  11349. 'this.p = this.TPoint.$new();',
  11350. 'this.q = this.TPoint.$new();',
  11351. '']),
  11352. LinesToStr([ // $mod.$main
  11353. '$mod.p.Add($mod.q);',
  11354. '$mod.p.$assign($mod.TPoint.$new());',
  11355. '$mod.p.$assign($mod.q);',
  11356. '']));
  11357. end;
  11358. procedure TTestModule.TestAdvRecord_Property;
  11359. begin
  11360. StartProgram(false);
  11361. Add([
  11362. '{$modeswitch AdvancedRecords}',
  11363. 'type',
  11364. ' TPoint = record',
  11365. ' x,y: word;',
  11366. ' strict private',
  11367. ' function GetSize: longword;',
  11368. ' procedure SetSize(Value: longword);',
  11369. ' public',
  11370. ' property Size: longword read GetSize write SetSize;',
  11371. ' property Left: word read x write y;',
  11372. ' end;',
  11373. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11374. 'function TPoint.GetSize: longword;',
  11375. 'begin',
  11376. ' x:=y;',
  11377. ' Size:=Size;',
  11378. ' Left:=Left;',
  11379. 'end;',
  11380. 'procedure TPoint.SetSize(Value: longword);',
  11381. 'begin',
  11382. 'end;',
  11383. 'var p,q: TPoint;',
  11384. 'begin',
  11385. ' p.Size:=q.Size;',
  11386. ' p.Left:=q.Left;',
  11387. '']);
  11388. ConvertProgram;
  11389. CheckSource('TestAdvRecord_Property',
  11390. LinesToStr([ // statements
  11391. 'rtl.recNewT(this, "TPoint", function () {',
  11392. ' this.x = 0;',
  11393. ' this.y = 0;',
  11394. ' this.$eq = function (b) {',
  11395. ' return (this.x === b.x) && (this.y === b.y);',
  11396. ' };',
  11397. ' this.$assign = function (s) {',
  11398. ' this.x = s.x;',
  11399. ' this.y = s.y;',
  11400. ' return this;',
  11401. ' };',
  11402. ' this.GetSize = function () {',
  11403. ' var Result = 0;',
  11404. ' this.x = this.y;',
  11405. ' this.SetSize(this.GetSize());',
  11406. ' this.y = this.x;',
  11407. ' return Result;',
  11408. ' };',
  11409. ' this.SetSize = function (Value) {',
  11410. ' };',
  11411. '});',
  11412. 'this.SetSize = function (Value) {',
  11413. '};',
  11414. 'this.p = this.TPoint.$new();',
  11415. 'this.q = this.TPoint.$new();',
  11416. '']),
  11417. LinesToStr([ // $mod.$main
  11418. '$mod.p.SetSize($mod.q.GetSize());',
  11419. '$mod.p.y = $mod.q.x;',
  11420. '']));
  11421. end;
  11422. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11423. begin
  11424. StartProgram(false);
  11425. Add([
  11426. '{$modeswitch AdvancedRecords}',
  11427. 'type',
  11428. ' TPoint = record',
  11429. ' strict private',
  11430. ' function GetItems(Index: word): word;',
  11431. ' procedure SetItems(Index: word; Value: word);',
  11432. ' public',
  11433. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11434. ' end;',
  11435. 'function TPoint.GetItems(Index: word): word;',
  11436. 'begin',
  11437. ' Items[index]:=Items[index];',
  11438. ' self.Items[index]:=self.Items[index];',
  11439. 'end;',
  11440. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11441. 'begin',
  11442. 'end;',
  11443. 'var p: TPoint;',
  11444. 'begin',
  11445. ' p[1]:=p[2];',
  11446. ' p.Items[3]:=p.Items[4];',
  11447. '']);
  11448. ConvertProgram;
  11449. CheckSource('TestAdvRecord_PropertyDefault',
  11450. LinesToStr([ // statements
  11451. 'rtl.recNewT(this, "TPoint", function () {',
  11452. ' this.$eq = function (b) {',
  11453. ' return true;',
  11454. ' };',
  11455. ' this.$assign = function (s) {',
  11456. ' return this;',
  11457. ' };',
  11458. ' this.GetItems = function (Index) {',
  11459. ' var Result = 0;',
  11460. ' this.SetItems(Index, this.GetItems(Index));',
  11461. ' this.SetItems(Index, this.GetItems(Index));',
  11462. ' return Result;',
  11463. ' };',
  11464. ' this.SetItems = function (Index, Value) {',
  11465. ' };',
  11466. '});',
  11467. 'this.p = this.TPoint.$new();',
  11468. '']),
  11469. LinesToStr([ // $mod.$main
  11470. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11471. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11472. '']));
  11473. end;
  11474. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11475. begin
  11476. StartProgram(false);
  11477. Add([
  11478. '{$modeswitch AdvancedRecords}',
  11479. 'type',
  11480. ' TRec = record',
  11481. ' class var',
  11482. ' Fx: longint;',
  11483. ' Fy: longint;',
  11484. ' class function GetInt: longint; static;',
  11485. ' class procedure SetInt(Value: longint); static;',
  11486. ' class procedure DoIt; static;',
  11487. ' class property IntA: longint read Fx write Fy;',
  11488. ' class property IntB: longint read GetInt write SetInt;',
  11489. ' end;',
  11490. 'class function trec.getint: longint;',
  11491. 'begin',
  11492. ' result:=fx;',
  11493. 'end;',
  11494. 'class procedure trec.setint(value: longint);',
  11495. 'begin',
  11496. 'end;',
  11497. 'class procedure trec.doit;',
  11498. 'begin',
  11499. ' IntA:=IntA+1;',
  11500. ' IntB:=IntB+1;',
  11501. 'end;',
  11502. 'var r: trec;',
  11503. 'begin',
  11504. ' trec.inta:=trec.inta+1;',
  11505. ' if trec.intb=2 then;',
  11506. ' trec.intb:=trec.intb+2;',
  11507. ' trec.setint(trec.inta);',
  11508. ' r.inta:=r.inta+1;',
  11509. ' if r.intb=2 then;',
  11510. ' r.intb:=r.intb+2;',
  11511. ' r.setint(r.inta);']);
  11512. ConvertProgram;
  11513. CheckSource('TestAdvRecord_Property_ClassMethod',
  11514. LinesToStr([ // statements
  11515. 'rtl.recNewT(this, "TRec", function () {',
  11516. ' this.Fx = 0;',
  11517. ' this.Fy = 0;',
  11518. ' this.$eq = function (b) {',
  11519. ' return true;',
  11520. ' };',
  11521. ' this.$assign = function (s) {',
  11522. ' return this;',
  11523. ' };',
  11524. ' this.GetInt = function () {',
  11525. ' var Result = 0;',
  11526. ' Result = $mod.TRec.Fx;',
  11527. ' return Result;',
  11528. ' };',
  11529. ' this.SetInt = function (Value) {',
  11530. ' };',
  11531. ' this.DoIt = function () {',
  11532. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11533. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11534. ' };',
  11535. '}, true);',
  11536. 'this.r = this.TRec.$new();',
  11537. '']),
  11538. LinesToStr([ // $mod.$main
  11539. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11540. 'if ($mod.TRec.GetInt() === 2) ;',
  11541. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11542. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11543. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11544. 'if ($mod.r.GetInt() === 2) ;',
  11545. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11546. '$mod.r.SetInt($mod.r.Fx);',
  11547. '']));
  11548. end;
  11549. procedure TTestModule.TestAdvRecord_Const;
  11550. begin
  11551. StartProgram(false);
  11552. Add([
  11553. '{$modeswitch AdvancedRecords}',
  11554. 'type',
  11555. ' TArrInt = array[3..4] of longint;',
  11556. ' TPoint = record',
  11557. ' x,y: longint;',
  11558. ' class var Count: nativeint;',
  11559. ' end;',
  11560. ' TRec = record',
  11561. ' i: longint;',
  11562. ' a: array of longint;',
  11563. ' s: array[1..2] of longint;',
  11564. ' m: array[1..2,3..4] of longint;',
  11565. ' p: TPoint;',
  11566. ' end;',
  11567. ' TPoints = array of TPoint;',
  11568. 'const',
  11569. ' r: TRec = (',
  11570. ' i:1;',
  11571. ' a:(2,3);',
  11572. ' s:(4,5);',
  11573. ' m:( (11,12), (13,14) );',
  11574. ' p: (x:21)',
  11575. ' );',
  11576. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11577. 'begin']);
  11578. ConvertProgram;
  11579. CheckSource('TestAdvRecord_Const',
  11580. LinesToStr([ // statements
  11581. 'rtl.recNewT(this, "TPoint", function () {',
  11582. ' this.x = 0;',
  11583. ' this.y = 0;',
  11584. ' this.Count = 0;',
  11585. ' this.$eq = function (b) {',
  11586. ' return (this.x === b.x) && (this.y === b.y);',
  11587. ' };',
  11588. ' this.$assign = function (s) {',
  11589. ' this.x = s.x;',
  11590. ' this.y = s.y;',
  11591. ' return this;',
  11592. ' };',
  11593. '}, true);',
  11594. 'rtl.recNewT(this, "TRec", function () {',
  11595. ' this.i = 0;',
  11596. ' this.$new = function () {',
  11597. ' var r = Object.create(this);',
  11598. ' r.a = [];',
  11599. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11600. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11601. ' r.p = $mod.TPoint.$new();',
  11602. ' return r;',
  11603. ' };',
  11604. ' this.$eq = function (b) {',
  11605. ' 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);',
  11606. ' };',
  11607. ' this.$assign = function (s) {',
  11608. ' this.i = s.i;',
  11609. ' this.a = rtl.arrayRef(s.a);',
  11610. ' this.s = s.s.slice(0);',
  11611. ' this.m = s.m.slice(0);',
  11612. ' this.p.$assign(s.p);',
  11613. ' return this;',
  11614. ' };',
  11615. '});',
  11616. 'this.r = this.TRec.$clone({',
  11617. ' i: 1,',
  11618. ' a: [2, 3],',
  11619. ' s: [4, 5],',
  11620. ' m: [[11, 12], [13, 14]],',
  11621. ' p: this.TPoint.$clone({',
  11622. ' x: 21,',
  11623. ' y: 0',
  11624. ' })',
  11625. '});',
  11626. 'this.p = [this.TPoint.$clone({',
  11627. ' x: 1,',
  11628. ' y: 2',
  11629. '}), this.TPoint.$clone({',
  11630. ' x: 3,',
  11631. ' y: 4',
  11632. '})];',
  11633. '']),
  11634. LinesToStr([ // $mod.$main
  11635. '']));
  11636. end;
  11637. procedure TTestModule.TestAdvRecord_ExternalField;
  11638. begin
  11639. StartProgram(false);
  11640. Add([
  11641. '{$modeswitch AdvancedRecords}',
  11642. '{$modeswitch externalclass}',
  11643. 'type',
  11644. ' TCar = record',
  11645. ' public',
  11646. ' Intern: longint external name ''$Intern'';',
  11647. ' Intern2: longint external name ''$Intern2'';',
  11648. ' Bracket: longint external name ''["A B"]'';',
  11649. ' procedure DoIt;',
  11650. ' end;',
  11651. 'procedure tcar.doit;',
  11652. 'begin',
  11653. ' Intern:=Intern+1;',
  11654. ' Intern2:=Intern2+2;',
  11655. ' Bracket:=Bracket+3;',
  11656. 'end;',
  11657. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11658. 'begin',
  11659. ' Rec.intern:=Rec.intern+1;',
  11660. ' Rec.intern2:=Rec.intern2+2;',
  11661. ' Rec.Bracket:=Rec.Bracket+3;',
  11662. ' with Rec do begin',
  11663. ' intern:=intern+1;',
  11664. ' intern2:=intern2+2;',
  11665. ' Bracket:=Bracket+3;',
  11666. ' end;']);
  11667. ConvertProgram;
  11668. CheckSource('TestAdvRecord_ExternalField',
  11669. LinesToStr([ // statements
  11670. 'rtl.recNewT(this, "TCar", function () {',
  11671. ' this.$eq = function (b) {',
  11672. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11673. ' };',
  11674. ' this.$assign = function (s) {',
  11675. ' this.$Intern = s.$Intern;',
  11676. ' this.$Intern2 = s.$Intern2;',
  11677. ' this["A B"] = s["A B"];',
  11678. ' return this;',
  11679. ' };',
  11680. ' this.DoIt = function () {',
  11681. ' this.$Intern = this.$Intern + 1;',
  11682. ' this.$Intern2 = this.$Intern2 + 2;',
  11683. ' this["A B"] = this["A B"] + 3;',
  11684. ' };',
  11685. '});',
  11686. 'this.Rec = this.TCar.$clone({',
  11687. ' $Intern: 11,',
  11688. ' $Intern2: 12,',
  11689. ' "A B": 13',
  11690. '});',
  11691. '']),
  11692. LinesToStr([ // $mod.$main
  11693. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11694. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11695. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11696. 'var $with = $mod.Rec;',
  11697. '$with.$Intern = $with.$Intern + 1;',
  11698. '$with.$Intern2 = $with.$Intern2 + 2;',
  11699. '$with["A B"] = $with["A B"] + 3;',
  11700. '']));
  11701. end;
  11702. procedure TTestModule.TestAdvRecord_SubRecord;
  11703. begin
  11704. StartProgram(false);
  11705. Add([
  11706. '{$modeswitch AdvancedRecords}',
  11707. 'type',
  11708. ' TRec = record',
  11709. ' type',
  11710. ' TPoint = record',
  11711. ' x,y: longint;',
  11712. ' class var Count: nativeint;',
  11713. ' procedure DoIt;',
  11714. ' class procedure DoThat; static;',
  11715. ' end;',
  11716. ' var',
  11717. ' i: longint;',
  11718. ' p: TPoint;',
  11719. ' procedure DoSome;',
  11720. ' end;',
  11721. 'const',
  11722. ' r: TRec = (',
  11723. ' i:1;',
  11724. ' p: (x:21;y:22)',
  11725. ' );',
  11726. 'procedure TRec.DoSome;',
  11727. 'begin',
  11728. ' p.x:=p.y+1;',
  11729. ' p.Count:=p.Count+2;',
  11730. 'end;',
  11731. 'procedure TRec.TPoint.DoIt;',
  11732. 'begin',
  11733. ' Count:=Count+3;',
  11734. 'end;',
  11735. 'class procedure TRec.TPoint.DoThat;',
  11736. 'begin',
  11737. ' Count:=Count+4;',
  11738. 'end;',
  11739. 'begin']);
  11740. ConvertProgram;
  11741. CheckSource('TestAdvRecord_SubRecord',
  11742. LinesToStr([ // statements
  11743. 'rtl.recNewT(this, "TRec", function () {',
  11744. ' rtl.recNewT(this, "TPoint", function () {',
  11745. ' this.x = 0;',
  11746. ' this.y = 0;',
  11747. ' this.Count = 0;',
  11748. ' this.$eq = function (b) {',
  11749. ' return (this.x === b.x) && (this.y === b.y);',
  11750. ' };',
  11751. ' this.$assign = function (s) {',
  11752. ' this.x = s.x;',
  11753. ' this.y = s.y;',
  11754. ' return this;',
  11755. ' };',
  11756. ' this.DoIt = function () {',
  11757. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11758. ' };',
  11759. ' this.DoThat = function () {',
  11760. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11761. ' };',
  11762. ' }, true);',
  11763. ' this.i = 0;',
  11764. ' this.$new = function () {',
  11765. ' var r = Object.create(this);',
  11766. ' r.p = this.TPoint.$new();',
  11767. ' return r;',
  11768. ' };',
  11769. ' this.$eq = function (b) {',
  11770. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11771. ' };',
  11772. ' this.$assign = function (s) {',
  11773. ' this.i = s.i;',
  11774. ' this.p.$assign(s.p);',
  11775. ' return this;',
  11776. ' };',
  11777. ' this.DoSome = function () {',
  11778. ' this.p.x = this.p.y + 1;',
  11779. ' this.TPoint.Count = this.p.Count + 2;',
  11780. ' };',
  11781. '}, true);',
  11782. 'this.r = this.TRec.$clone({',
  11783. ' i: 1,',
  11784. ' p: this.TRec.TPoint.$clone({',
  11785. ' x: 21,',
  11786. ' y: 22',
  11787. ' })',
  11788. '});',
  11789. '']),
  11790. LinesToStr([ // $mod.$main
  11791. '']));
  11792. end;
  11793. procedure TTestModule.TestAdvRecord_SubClass;
  11794. begin
  11795. StartProgram(false);
  11796. Add([
  11797. '{$modeswitch AdvancedRecords}',
  11798. 'type',
  11799. ' TObject = class end;',
  11800. ' TPoint = record',
  11801. ' type',
  11802. ' TBird = class',
  11803. ' procedure DoIt;',
  11804. ' class procedure Glob;',
  11805. ' end;',
  11806. ' procedure DoIt(b: TBird);',
  11807. ' end;',
  11808. 'procedure TPoint.TBird.DoIt;',
  11809. 'begin',
  11810. ' doit;',
  11811. ' self.doit;',
  11812. ' glob;',
  11813. ' self.glob;',
  11814. 'end;',
  11815. 'class procedure TPoint.TBird.Glob;',
  11816. 'begin',
  11817. ' glob;',
  11818. ' self.glob;',
  11819. 'end;',
  11820. 'procedure TPoint.DoIt(b: TBird);',
  11821. 'begin',
  11822. ' b.doit;',
  11823. ' b.glob;',
  11824. ' TBird.glob;',
  11825. 'end;',
  11826. 'begin',
  11827. '']);
  11828. ConvertProgram;
  11829. CheckSource('TestAdvRecord_SubClass',
  11830. LinesToStr([ // statements
  11831. 'rtl.createClass(this, "TObject", null, function () {',
  11832. ' this.$init = function () {',
  11833. ' };',
  11834. ' this.$final = function () {',
  11835. ' };',
  11836. '});',
  11837. 'rtl.recNewT(this, "TPoint", function () {',
  11838. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11839. ' this.DoIt = function () {',
  11840. ' this.DoIt();',
  11841. ' this.DoIt();',
  11842. ' this.$class.Glob();',
  11843. ' this.$class.Glob();',
  11844. ' };',
  11845. ' this.Glob = function () {',
  11846. ' this.Glob();',
  11847. ' this.Glob();',
  11848. ' };',
  11849. ' }, "TPoint.TBird");',
  11850. ' this.$eq = function (b) {',
  11851. ' return true;',
  11852. ' };',
  11853. ' this.$assign = function (s) {',
  11854. ' return this;',
  11855. ' };',
  11856. ' this.DoIt = function (b) {',
  11857. ' b.DoIt();',
  11858. ' b.$class.Glob();',
  11859. ' this.TBird.Glob();',
  11860. ' };',
  11861. '}, true);',
  11862. '']),
  11863. LinesToStr([ // $mod.$main
  11864. '']));
  11865. end;
  11866. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11867. begin
  11868. StartProgram(false);
  11869. Add([
  11870. '{$modeswitch AdvancedRecords}',
  11871. 'type',
  11872. ' IUnknown = interface end;',
  11873. ' TPoint = record',
  11874. ' type IBird = interface end;',
  11875. ' end;',
  11876. 'begin',
  11877. '']);
  11878. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11879. nNotYetImplemented);
  11880. ParseProgram;
  11881. end;
  11882. procedure TTestModule.TestAdvRecord_Constructor;
  11883. begin
  11884. StartProgram(false);
  11885. Add([
  11886. '{$modeswitch AdvancedRecords}',
  11887. 'type',
  11888. ' TPoint = record',
  11889. ' x,y: longint;',
  11890. ' class procedure Run(w: longint = 13); static;',
  11891. ' constructor Create(ax: longint; ay: longint = -1);',
  11892. ' end;',
  11893. 'class procedure tpoint.run(w: longint);',
  11894. 'begin',
  11895. ' run;',
  11896. ' run();',
  11897. 'end;',
  11898. 'constructor tpoint.create(ax,ay: longint);',
  11899. 'begin',
  11900. ' x:=ax;',
  11901. ' self.y:=ay;',
  11902. ' run;',
  11903. ' run(ax);',
  11904. 'end;',
  11905. 'var r: TPoint;',
  11906. 'begin',
  11907. ' r:=TPoint.Create(1,2);',
  11908. ' with TPoint do r:=Create(1,2);',
  11909. ' r.Create(3);',
  11910. ' r:=r.Create(4);',
  11911. '']);
  11912. ConvertProgram;
  11913. CheckSource('TestAdvRecord_Constructor',
  11914. LinesToStr([ // statements
  11915. 'rtl.recNewT(this, "TPoint", function () {',
  11916. ' this.x = 0;',
  11917. ' this.y = 0;',
  11918. ' this.$eq = function (b) {',
  11919. ' return (this.x === b.x) && (this.y === b.y);',
  11920. ' };',
  11921. ' this.$assign = function (s) {',
  11922. ' this.x = s.x;',
  11923. ' this.y = s.y;',
  11924. ' return this;',
  11925. ' };',
  11926. ' this.Run = function (w) {',
  11927. ' $mod.TPoint.Run(13);',
  11928. ' $mod.TPoint.Run(13);',
  11929. ' };',
  11930. ' this.Create = function (ax, ay) {',
  11931. ' this.x = ax;',
  11932. ' this.y = ay;',
  11933. ' this.Run(13);',
  11934. ' this.Run(ax);',
  11935. ' return this;',
  11936. ' };',
  11937. '});',
  11938. 'this.r = this.TPoint.$new();',
  11939. '']),
  11940. LinesToStr([ // $mod.$main
  11941. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11942. 'var $with = $mod.TPoint;',
  11943. '$mod.r.$assign($with.$new().Create(1, 2));',
  11944. '$mod.r.Create(3, -1);',
  11945. '$mod.r.$assign($mod.r.Create(4, -1));',
  11946. '']));
  11947. end;
  11948. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11949. begin
  11950. StartProgram(false);
  11951. Add([
  11952. '{$modeswitch AdvancedRecords}',
  11953. 'type',
  11954. ' TPoint = record',
  11955. ' class var x: longint;',
  11956. ' class procedure Fly; static;',
  11957. ' class constructor Init;',
  11958. ' end;',
  11959. 'var count: word;',
  11960. 'class procedure Tpoint.Fly;',
  11961. 'begin',
  11962. 'end;',
  11963. 'class constructor tpoint.init;',
  11964. 'begin',
  11965. ' count:=count+1;',
  11966. ' x:=x+3;',
  11967. ' tpoint.x:=tpoint.x+4;',
  11968. ' fly;',
  11969. ' tpoint.fly;',
  11970. 'end;',
  11971. 'var r: TPoint;',
  11972. 'begin',
  11973. ' r.x:=r.x+10;',
  11974. ' r.Fly;',
  11975. ' r.Fly();',
  11976. '']);
  11977. ConvertProgram;
  11978. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11979. LinesToStr([ // statements
  11980. 'rtl.recNewT(this, "TPoint", function () {',
  11981. ' this.x = 0;',
  11982. ' this.$eq = function (b) {',
  11983. ' return true;',
  11984. ' };',
  11985. ' this.$assign = function (s) {',
  11986. ' return this;',
  11987. ' };',
  11988. ' this.Fly = function () {',
  11989. ' };',
  11990. '}, true);',
  11991. 'this.count = 0;',
  11992. 'this.r = this.TPoint.$new();',
  11993. '']),
  11994. LinesToStr([ // $mod.$main
  11995. '(function () {',
  11996. ' $mod.count = $mod.count + 1;',
  11997. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11998. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11999. ' $mod.TPoint.Fly();',
  12000. ' $mod.TPoint.Fly();',
  12001. '})();',
  12002. '$mod.TPoint.x = $mod.r.x + 10;',
  12003. '$mod.r.Fly();',
  12004. '$mod.r.Fly();',
  12005. '']));
  12006. end;
  12007. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12008. begin
  12009. StartUnit(false);
  12010. Add([
  12011. 'interface',
  12012. '{$modeswitch AdvancedRecords}',
  12013. 'type',
  12014. ' TPoint = record',
  12015. ' class var x: longint;',
  12016. ' class procedure Fly; static;',
  12017. ' class constructor Init;',
  12018. ' end;',
  12019. 'implementation',
  12020. 'var count: word;',
  12021. 'class procedure Tpoint.Fly;',
  12022. 'begin',
  12023. 'end;',
  12024. 'class constructor tpoint.init;',
  12025. 'begin',
  12026. ' count:=count+1;',
  12027. ' x:=3;',
  12028. ' tpoint.x:=4;',
  12029. ' fly;',
  12030. ' tpoint.fly;',
  12031. 'end;',
  12032. '']);
  12033. ConvertUnit;
  12034. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12035. LinesToStr([ // statements
  12036. 'var $impl = $mod.$impl;',
  12037. 'rtl.recNewT(this, "TPoint", function () {',
  12038. ' this.x = 0;',
  12039. ' this.$eq = function (b) {',
  12040. ' return true;',
  12041. ' };',
  12042. ' this.$assign = function (s) {',
  12043. ' return this;',
  12044. ' };',
  12045. ' this.Fly = function () {',
  12046. ' };',
  12047. '}, true);',
  12048. '']),
  12049. LinesToStr([ // $mod.$init
  12050. '(function () {',
  12051. ' $impl.count = $impl.count + 1;',
  12052. ' $mod.TPoint.x = 3;',
  12053. ' $mod.TPoint.x = 4;',
  12054. ' $mod.TPoint.Fly();',
  12055. ' $mod.TPoint.Fly();',
  12056. '})();',
  12057. '']),
  12058. LinesToStr([ // $mod.$main
  12059. '$impl.count = 0;',
  12060. '']));
  12061. end;
  12062. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12063. begin
  12064. StartProgram(false);
  12065. Add(['type',
  12066. ' TObject = class',
  12067. ' public',
  12068. ' constructor Create;',
  12069. ' destructor Destroy;',
  12070. ' end;',
  12071. ' TBird = TObject;',
  12072. 'constructor tobject.create;',
  12073. 'begin end;',
  12074. 'destructor tobject.destroy;',
  12075. 'begin end;',
  12076. 'var Obj: tobject;',
  12077. 'begin',
  12078. ' obj:=tobject.create;',
  12079. ' obj:=tobject.create();',
  12080. ' obj:=tbird.create;',
  12081. ' obj:=tbird.create();',
  12082. ' obj:=obj.create();',
  12083. ' obj.destroy;',
  12084. '']);
  12085. ConvertProgram;
  12086. CheckSource('TestClass_TObjectDefaultConstructor',
  12087. LinesToStr([ // statements
  12088. 'rtl.createClass(this,"TObject",null,function(){',
  12089. ' this.$init = function () {',
  12090. ' };',
  12091. ' this.$final = function () {',
  12092. ' };',
  12093. ' this.Create = function(){',
  12094. ' return this;',
  12095. ' };',
  12096. ' this.Destroy = function(){',
  12097. ' };',
  12098. '});',
  12099. 'this.Obj = null;'
  12100. ]),
  12101. LinesToStr([ // $mod.$main
  12102. '$mod.Obj = $mod.TObject.$create("Create");',
  12103. '$mod.Obj = $mod.TObject.$create("Create");',
  12104. '$mod.Obj = $mod.TObject.$create("Create");',
  12105. '$mod.Obj = $mod.TObject.$create("Create");',
  12106. '$mod.Obj = $mod.Obj.Create();',
  12107. '$mod.Obj.$destroy("Destroy");',
  12108. '']));
  12109. end;
  12110. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12111. begin
  12112. StartProgram(false);
  12113. Add('type');
  12114. Add(' TObject = class');
  12115. Add(' public');
  12116. Add(' constructor Create(Par: longint);');
  12117. Add(' end;');
  12118. Add('constructor tobject.create(par: longint);');
  12119. Add('begin end;');
  12120. Add('var Obj: tobject;');
  12121. Add('begin');
  12122. Add(' obj:=tobject.create(3);');
  12123. ConvertProgram;
  12124. CheckSource('TestClass_TObjectConstructorWithParams',
  12125. LinesToStr([ // statements
  12126. 'rtl.createClass(this,"TObject",null,function(){',
  12127. ' this.$init = function () {',
  12128. ' };',
  12129. ' this.$final = function () {',
  12130. ' };',
  12131. ' this.Create = function(Par){',
  12132. ' return this;',
  12133. ' };',
  12134. '});',
  12135. 'this.Obj = null;'
  12136. ]),
  12137. LinesToStr([ // $mod.$main
  12138. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12139. ]));
  12140. end;
  12141. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12142. begin
  12143. StartProgram(false);
  12144. Add('type');
  12145. Add(' TObject = class');
  12146. Add(' public');
  12147. Add(' constructor Create;');
  12148. Add(' end;');
  12149. Add(' TTest = class(TObject)');
  12150. Add(' public');
  12151. Add(' constructor Create(const Par: longint = 1);');
  12152. Add(' end;');
  12153. Add('constructor tobject.create;');
  12154. Add('begin end;');
  12155. Add('constructor ttest.create(const par: longint);');
  12156. Add('begin end;');
  12157. Add('var t: ttest;');
  12158. Add('begin');
  12159. Add(' t:=ttest.create;');
  12160. Add(' t:=ttest.create(2);');
  12161. ConvertProgram;
  12162. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12163. LinesToStr([ // statements
  12164. 'rtl.createClass(this,"TObject",null,function(){',
  12165. ' this.$init = function () {',
  12166. ' };',
  12167. ' this.$final = function () {',
  12168. ' };',
  12169. ' this.Create = function(){',
  12170. ' return this;',
  12171. ' };',
  12172. '});',
  12173. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12174. ' this.Create$1 = function (Par) {',
  12175. ' return this;',
  12176. ' };',
  12177. '});',
  12178. 'this.t = null;'
  12179. ]),
  12180. LinesToStr([ // $mod.$main
  12181. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12182. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12183. ]));
  12184. end;
  12185. procedure TTestModule.TestClass_Var;
  12186. begin
  12187. StartProgram(false);
  12188. Add([
  12189. 'type',
  12190. ' TObject = class',
  12191. ' public',
  12192. ' vI: longint;',
  12193. ' constructor Create(Par: longint);',
  12194. ' end;',
  12195. 'constructor tobject.create(par: longint);',
  12196. 'begin',
  12197. ' vi:=par+3',
  12198. 'end;',
  12199. 'var Obj: tobject;',
  12200. 'begin',
  12201. ' obj:=tobject.create(4);',
  12202. ' obj.vi:=obj.VI+5;']);
  12203. ConvertProgram;
  12204. CheckSource('TestClass_Var',
  12205. LinesToStr([ // statements
  12206. 'rtl.createClass(this,"TObject",null,function(){',
  12207. ' this.$init = function () {',
  12208. ' this.vI = 0;',
  12209. ' };',
  12210. ' this.$final = function () {',
  12211. ' };',
  12212. ' this.Create = function(Par){',
  12213. ' this.vI = Par+3;',
  12214. ' return this;',
  12215. ' };',
  12216. '});',
  12217. 'this.Obj = null;'
  12218. ]),
  12219. LinesToStr([ // $mod.$main
  12220. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12221. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12222. ]));
  12223. end;
  12224. procedure TTestModule.TestClass_Method;
  12225. begin
  12226. StartProgram(false);
  12227. Add('type');
  12228. Add(' TObject = class');
  12229. Add(' public');
  12230. Add(' vI: longint;');
  12231. Add(' Sub: TObject;');
  12232. Add(' constructor Create;');
  12233. Add(' function GetIt(Par: longint): tobject;');
  12234. Add(' end;');
  12235. Add('constructor tobject.create; begin end;');
  12236. Add('function tobject.getit(par: longint): tobject;');
  12237. Add('begin');
  12238. Add(' Self.vi:=par+3;');
  12239. Add(' Result:=self.sub;');
  12240. Add('end;');
  12241. Add('var Obj: tobject;');
  12242. Add('begin');
  12243. Add(' obj:=tobject.create;');
  12244. Add(' obj.getit(4);');
  12245. Add(' obj.sub.sub:=nil;');
  12246. Add(' obj.sub.getit(5);');
  12247. Add(' obj.sub.getit(6).SUB:=nil;');
  12248. Add(' obj.sub.getit(7).GETIT(8);');
  12249. Add(' obj.sub.getit(9).SuB.getit(10);');
  12250. ConvertProgram;
  12251. CheckSource('TestClass_Method',
  12252. LinesToStr([ // statements
  12253. 'rtl.createClass(this,"TObject",null,function(){',
  12254. ' this.$init = function () {',
  12255. ' this.vI = 0;',
  12256. ' this.Sub = null;',
  12257. ' };',
  12258. ' this.$final = function () {',
  12259. ' this.Sub = undefined;',
  12260. ' };',
  12261. ' this.Create = function(){',
  12262. ' return this;',
  12263. ' };',
  12264. ' this.GetIt = function(Par){',
  12265. ' var Result = null;',
  12266. ' this.vI = Par + 3;',
  12267. ' Result = this.Sub;',
  12268. ' return Result;',
  12269. ' };',
  12270. '});',
  12271. 'this.Obj = null;'
  12272. ]),
  12273. LinesToStr([ // $mod.$main
  12274. '$mod.Obj = $mod.TObject.$create("Create");',
  12275. '$mod.Obj.GetIt(4);',
  12276. '$mod.Obj.Sub.Sub=null;',
  12277. '$mod.Obj.Sub.GetIt(5);',
  12278. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12279. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12280. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12281. ]));
  12282. end;
  12283. procedure TTestModule.TestClass_Implementation;
  12284. begin
  12285. StartUnit(false);
  12286. Add([
  12287. 'interface',
  12288. 'type',
  12289. ' TObject = class',
  12290. ' constructor Create;',
  12291. ' end;',
  12292. 'implementation',
  12293. 'type',
  12294. ' TIntClass = class',
  12295. ' constructor Create; reintroduce;',
  12296. ' class procedure DoGlob;',
  12297. ' end;',
  12298. 'constructor tintclass.create;',
  12299. 'begin',
  12300. ' inherited;',
  12301. ' inherited create;',
  12302. ' doglob;',
  12303. 'end;',
  12304. 'class procedure tintclass.doglob;',
  12305. 'begin',
  12306. 'end;',
  12307. 'constructor tobject.create;',
  12308. 'var',
  12309. ' iC: tintclass;',
  12310. 'begin',
  12311. ' ic:=tintclass.create;',
  12312. ' tintclass.doglob;',
  12313. ' ic.doglob;',
  12314. 'end;',
  12315. 'initialization',
  12316. ' tintclass.doglob;',
  12317. '']);
  12318. ConvertUnit;
  12319. CheckSource('TestClass_Implementation',
  12320. LinesToStr([ // statements
  12321. 'var $impl = $mod.$impl;',
  12322. 'rtl.createClass(this, "TObject", null, function () {',
  12323. ' this.$init = function () {',
  12324. ' };',
  12325. ' this.$final = function () {',
  12326. ' };',
  12327. ' this.Create = function () {',
  12328. ' var iC = null;',
  12329. ' iC = $impl.TIntClass.$create("Create$1");',
  12330. ' $impl.TIntClass.DoGlob();',
  12331. ' iC.$class.DoGlob();',
  12332. ' return this;',
  12333. ' };',
  12334. '});',
  12335. '']),
  12336. LinesToStr([ // $mod.$main
  12337. '$impl.TIntClass.DoGlob();',
  12338. '']),
  12339. LinesToStr([
  12340. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12341. ' this.Create$1 = function () {',
  12342. ' $mod.TObject.Create.call(this);',
  12343. ' $mod.TObject.Create.call(this);',
  12344. ' this.$class.DoGlob();',
  12345. ' return this;',
  12346. ' };',
  12347. ' this.DoGlob = function () {',
  12348. ' };',
  12349. '});',
  12350. '']));
  12351. end;
  12352. procedure TTestModule.TestClass_Inheritance;
  12353. begin
  12354. StartProgram(false);
  12355. Add('type');
  12356. Add(' TObject = class');
  12357. Add(' public');
  12358. Add(' constructor Create;');
  12359. Add(' end;');
  12360. Add(' TClassA = class');
  12361. Add(' end;');
  12362. Add(' TClassB = class(TObject)');
  12363. Add(' procedure ProcB;');
  12364. Add(' end;');
  12365. Add('constructor tobject.create; begin end;');
  12366. Add('procedure tclassb.procb; begin end;');
  12367. Add('var');
  12368. Add(' oO: TObject;');
  12369. Add(' oA: TClassA;');
  12370. Add(' oB: TClassB;');
  12371. Add('begin');
  12372. Add(' oO:=tobject.Create;');
  12373. Add(' oA:=tclassa.Create;');
  12374. Add(' ob:=tclassb.Create;');
  12375. Add(' if oo is tclassa then ;');
  12376. Add(' ob:=oo as tclassb;');
  12377. Add(' (oo as tclassb).procb;');
  12378. ConvertProgram;
  12379. CheckSource('TestClass_Inheritance',
  12380. LinesToStr([ // statements
  12381. 'rtl.createClass(this,"TObject",null,function(){',
  12382. ' this.$init = function () {',
  12383. ' };',
  12384. ' this.$final = function () {',
  12385. ' };',
  12386. ' this.Create = function () {',
  12387. ' return this;',
  12388. ' };',
  12389. '});',
  12390. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12391. '});',
  12392. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12393. ' this.ProcB = function () {',
  12394. ' };',
  12395. '});',
  12396. 'this.oO = null;',
  12397. 'this.oA = null;',
  12398. 'this.oB = null;'
  12399. ]),
  12400. LinesToStr([ // $mod.$main
  12401. '$mod.oO = $mod.TObject.$create("Create");',
  12402. '$mod.oA = $mod.TClassA.$create("Create");',
  12403. '$mod.oB = $mod.TClassB.$create("Create");',
  12404. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12405. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12406. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12407. ]));
  12408. end;
  12409. procedure TTestModule.TestClass_TypeAlias;
  12410. begin
  12411. StartProgram(false);
  12412. Add([
  12413. '{$interfaces corba}',
  12414. 'type',
  12415. ' IObject = interface',
  12416. ' end;',
  12417. ' IBird = type IObject;',
  12418. ' TObject = class',
  12419. ' end;',
  12420. ' TBird = type TObject;',
  12421. 'var',
  12422. ' oObj: TObject;',
  12423. ' oBird: TBird;',
  12424. ' IntfObj: IObject;',
  12425. ' IntfBird: IBird;',
  12426. 'begin',
  12427. ' oObj:=oBird;',
  12428. '']);
  12429. ConvertProgram;
  12430. CheckSource('TestClass_TypeAlias',
  12431. LinesToStr([ // statements
  12432. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12433. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12434. 'rtl.createClass(this, "TObject", null, function () {',
  12435. ' this.$init = function () {',
  12436. ' };',
  12437. ' this.$final = function () {',
  12438. ' };',
  12439. '});',
  12440. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12441. '});',
  12442. 'this.oObj = null;',
  12443. 'this.oBird = null;',
  12444. 'this.IntfObj = null;',
  12445. 'this.IntfBird = null;',
  12446. '']),
  12447. LinesToStr([ // $mod.$main
  12448. '$mod.oObj = $mod.oBird;',
  12449. '']));
  12450. end;
  12451. procedure TTestModule.TestClass_AbstractMethod;
  12452. begin
  12453. StartProgram(false);
  12454. Add('type');
  12455. Add(' TObject = class');
  12456. Add(' public');
  12457. Add(' procedure DoIt; virtual; abstract;');
  12458. Add(' end;');
  12459. Add('begin');
  12460. ConvertProgram;
  12461. CheckSource('TestClass_AbstractMethod',
  12462. LinesToStr([ // statements
  12463. 'rtl.createClass(this,"TObject",null,function(){',
  12464. ' this.$init = function () {',
  12465. ' };',
  12466. ' this.$final = function () {',
  12467. ' };',
  12468. '});'
  12469. ]),
  12470. LinesToStr([ // this.$main
  12471. ''
  12472. ]));
  12473. end;
  12474. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12475. begin
  12476. StartProgram(false);
  12477. Add([
  12478. 'type',
  12479. ' TObject = class',
  12480. ' procedure DoAbstract; virtual; abstract;',
  12481. ' procedure DoVirtual; virtual;',
  12482. ' procedure DoIt;',
  12483. ' end;',
  12484. ' TA = class',
  12485. ' procedure doabstract; override;',
  12486. ' procedure dovirtual; override;',
  12487. ' procedure DoSome;',
  12488. ' end;',
  12489. 'procedure tobject.dovirtual;',
  12490. 'begin',
  12491. ' inherited; // call non existing ancestor -> ignore silently',
  12492. 'end;',
  12493. 'procedure tobject.doit;',
  12494. 'begin',
  12495. 'end;',
  12496. 'procedure ta.doabstract;',
  12497. 'begin',
  12498. ' inherited dovirtual; // call TObject.DoVirtual',
  12499. 'end;',
  12500. 'procedure ta.dovirtual;',
  12501. 'begin',
  12502. ' inherited; // call TObject.DoVirtual',
  12503. ' inherited dovirtual; // call TObject.DoVirtual',
  12504. ' inherited dovirtual(); // call TObject.DoVirtual',
  12505. ' doit;',
  12506. ' doit();',
  12507. 'end;',
  12508. 'procedure ta.dosome;',
  12509. 'begin',
  12510. ' inherited; // call non existing ancestor method -> silently ignore',
  12511. 'end;',
  12512. 'begin']);
  12513. ConvertProgram;
  12514. CheckSource('TestClass_CallInherited_ProcNoParams',
  12515. LinesToStr([ // statements
  12516. 'rtl.createClass(this,"TObject",null,function(){',
  12517. ' this.$init = function () {',
  12518. ' };',
  12519. ' this.$final = function () {',
  12520. ' };',
  12521. ' this.DoVirtual = function () {',
  12522. ' };',
  12523. ' this.DoIt = function () {',
  12524. ' };',
  12525. '});',
  12526. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12527. ' this.DoAbstract = function () {',
  12528. ' $mod.TObject.DoVirtual.call(this);',
  12529. ' };',
  12530. ' this.DoVirtual = function () {',
  12531. ' $mod.TObject.DoVirtual.call(this);',
  12532. ' $mod.TObject.DoVirtual.call(this);',
  12533. ' $mod.TObject.DoVirtual.call(this);',
  12534. ' this.DoIt();',
  12535. ' this.DoIt();',
  12536. ' };',
  12537. ' this.DoSome = function () {',
  12538. ' };',
  12539. '});'
  12540. ]),
  12541. LinesToStr([ // this.$main
  12542. ''
  12543. ]));
  12544. end;
  12545. procedure TTestModule.TestClass_CallInherited_WithParams;
  12546. begin
  12547. StartProgram(false);
  12548. Add([
  12549. 'type',
  12550. ' TObject = class',
  12551. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12552. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12553. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12554. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12555. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12556. ' end;',
  12557. ' TClassA = class',
  12558. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12559. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12560. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12561. ' end;',
  12562. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12563. 'begin',
  12564. 'end;',
  12565. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12566. 'begin',
  12567. 'end;',
  12568. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12569. 'begin',
  12570. 'end;',
  12571. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12572. 'begin',
  12573. 'end;',
  12574. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12575. 'begin',
  12576. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12577. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12578. 'end;',
  12579. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12580. 'begin',
  12581. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12582. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12583. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12584. ' doit(pa,pb);',
  12585. ' doit(pa);',
  12586. ' doit2(pa);',
  12587. ' doit2;',
  12588. 'end;',
  12589. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12590. 'begin',
  12591. ' pa:=inherited;',
  12592. 'end;',
  12593. 'begin']);
  12594. ConvertProgram;
  12595. CheckSource('TestClass_CallInherited_WithParams',
  12596. LinesToStr([ // statements
  12597. 'rtl.createClass(this,"TObject",null,function(){',
  12598. ' this.$init = function () {',
  12599. ' };',
  12600. ' this.$final = function () {',
  12601. ' };',
  12602. ' this.DoVirtual = function (pA,pB) {',
  12603. ' };',
  12604. ' this.DoIt = function (pA,pB) {',
  12605. ' };',
  12606. ' this.DoIt2 = function (pA,pB) {',
  12607. ' };',
  12608. ' this.GetIt = function (pA, pB) {',
  12609. ' var Result = 0;',
  12610. ' return Result;',
  12611. ' };',
  12612. '});',
  12613. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12614. ' this.DoAbstract = function (pA,pB) {',
  12615. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12616. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12617. ' };',
  12618. ' this.DoVirtual = function (pA,pB) {',
  12619. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12620. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12621. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12622. ' this.DoIt(pA,pB);',
  12623. ' this.DoIt(pA,0);',
  12624. ' this.DoIt2(pA,2);',
  12625. ' this.DoIt2(1,2);',
  12626. ' };',
  12627. ' this.GetIt$1 = function (pA, pB) {',
  12628. ' var Result = 0;',
  12629. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12630. ' return Result;',
  12631. ' };',
  12632. '});'
  12633. ]),
  12634. LinesToStr([ // this.$main
  12635. ''
  12636. ]));
  12637. end;
  12638. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12639. begin
  12640. StartProgram(false);
  12641. Add('type');
  12642. Add(' TObject = class');
  12643. Add(' constructor Create; virtual;');
  12644. Add(' constructor CreateWithB(b: boolean);');
  12645. Add(' end;');
  12646. Add(' TA = class');
  12647. Add(' constructor Create; override;');
  12648. Add(' constructor CreateWithC(c: char);');
  12649. Add(' procedure DoIt;');
  12650. Add(' class function DoSome: TObject;');
  12651. Add(' end;');
  12652. Add('constructor tobject.create;');
  12653. Add('begin');
  12654. Add(' inherited; // call non existing ancestor -> ignore silently');
  12655. Add('end;');
  12656. Add('constructor tobject.createwithb(b: boolean);');
  12657. Add('begin');
  12658. Add(' inherited; // call non existing ancestor -> ignore silently');
  12659. Add(' create; // normal call');
  12660. Add('end;');
  12661. Add('constructor ta.create;');
  12662. Add('begin');
  12663. Add(' inherited; // normal call TObject.Create');
  12664. Add(' inherited create; // normal call TObject.Create');
  12665. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12666. Add('end;');
  12667. Add('constructor ta.createwithc(c: char);');
  12668. Add('begin');
  12669. Add(' inherited create; // call TObject.Create');
  12670. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12671. Add(' doit;');
  12672. Add(' doit();');
  12673. Add(' dosome;');
  12674. Add('end;');
  12675. Add('procedure ta.doit;');
  12676. Add('begin');
  12677. Add(' create; // normal call');
  12678. Add(' createwithb(false); // normal call');
  12679. Add(' createwithc(''c''); // normal call');
  12680. Add('end;');
  12681. Add('class function ta.dosome: TObject;');
  12682. Add('begin');
  12683. Add(' Result:=create; // constructor');
  12684. Add(' Result:=createwithb(true); // constructor');
  12685. Add(' Result:=createwithc(''c''); // constructor');
  12686. Add('end;');
  12687. Add('begin');
  12688. ConvertProgram;
  12689. CheckSource('TestClass_CallInheritedConstructor',
  12690. LinesToStr([ // statements
  12691. 'rtl.createClass(this,"TObject",null,function(){',
  12692. ' this.$init = function () {',
  12693. ' };',
  12694. ' this.$final = function () {',
  12695. ' };',
  12696. ' this.Create = function () {',
  12697. ' return this;',
  12698. ' };',
  12699. ' this.CreateWithB = function (b) {',
  12700. ' this.Create();',
  12701. ' return this;',
  12702. ' };',
  12703. '});',
  12704. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12705. ' this.Create = function () {',
  12706. ' $mod.TObject.Create.call(this);',
  12707. ' $mod.TObject.Create.call(this);',
  12708. ' $mod.TObject.CreateWithB.call(this, false);',
  12709. ' return this;',
  12710. ' };',
  12711. ' this.CreateWithC = function (c) {',
  12712. ' $mod.TObject.Create.call(this);',
  12713. ' $mod.TObject.CreateWithB.call(this, true);',
  12714. ' this.DoIt();',
  12715. ' this.DoIt();',
  12716. ' this.$class.DoSome();',
  12717. ' return this;',
  12718. ' };',
  12719. ' this.DoIt = function () {',
  12720. ' this.Create();',
  12721. ' this.CreateWithB(false);',
  12722. ' this.CreateWithC("c");',
  12723. ' };',
  12724. ' this.DoSome = function () {',
  12725. ' var Result = null;',
  12726. ' Result = this.$create("Create");',
  12727. ' Result = this.$create("CreateWithB", [true]);',
  12728. ' Result = this.$create("CreateWithC", ["c"]);',
  12729. ' return Result;',
  12730. ' };',
  12731. '});'
  12732. ]),
  12733. LinesToStr([ // this.$main
  12734. ''
  12735. ]));
  12736. end;
  12737. procedure TTestModule.TestClass_ClassVar_Assign;
  12738. begin
  12739. StartProgram(false);
  12740. Add([
  12741. 'type',
  12742. ' TObject = class',
  12743. ' public',
  12744. ' class var vI: longint;',
  12745. ' class var Sub: TObject;',
  12746. ' constructor Create;',
  12747. ' class function GetIt(var Par: longint): tobject;',
  12748. ' end;',
  12749. 'constructor tobject.create;',
  12750. 'begin',
  12751. ' vi:=vi+1;',
  12752. ' Self.vi:=Self.vi+1;',
  12753. ' inc(vi);',
  12754. 'end;',
  12755. 'class function tobject.getit(var par: longint): tobject;',
  12756. 'begin',
  12757. ' vi:=vi+3;',
  12758. ' Self.vi:=Self.vi+4;',
  12759. ' inc(vi);',
  12760. ' Result:=self.sub;',
  12761. ' GetIt(vi);',
  12762. 'end;',
  12763. 'var Obj: tobject;',
  12764. 'begin',
  12765. ' obj:=tobject.create;',
  12766. ' tobject.vi:=3;',
  12767. ' if tobject.vi=4 then ;',
  12768. ' tobject.sub:=nil;',
  12769. ' obj.sub:=nil;',
  12770. ' obj.sub.sub:=nil;']);
  12771. ConvertProgram;
  12772. CheckSource('TestClass_ClassVar_Assign',
  12773. LinesToStr([ // statements
  12774. 'rtl.createClass(this,"TObject",null,function(){',
  12775. ' this.vI = 0;',
  12776. ' this.Sub = null;',
  12777. ' this.$init = function () {',
  12778. ' };',
  12779. ' this.$final = function () {',
  12780. ' };',
  12781. ' this.Create = function(){',
  12782. ' $mod.TObject.vI = this.vI+1;',
  12783. ' $mod.TObject.vI = this.vI+1;',
  12784. ' $mod.TObject.vI += 1;',
  12785. ' return this;',
  12786. ' };',
  12787. ' this.GetIt = function(Par){',
  12788. ' var Result = null;',
  12789. ' $mod.TObject.vI = this.vI + 3;',
  12790. ' $mod.TObject.vI = this.vI + 4;',
  12791. ' $mod.TObject.vI += 1;',
  12792. ' Result = this.Sub;',
  12793. ' this.GetIt({',
  12794. ' p: $mod.TObject,',
  12795. ' get: function () {',
  12796. ' return this.p.vI;',
  12797. ' },',
  12798. ' set: function (v) {',
  12799. ' this.p.vI = v;',
  12800. ' }',
  12801. ' });',
  12802. ' return Result;',
  12803. ' };',
  12804. '});',
  12805. 'this.Obj = null;'
  12806. ]),
  12807. LinesToStr([ // $mod.$main
  12808. '$mod.Obj = $mod.TObject.$create("Create");',
  12809. '$mod.TObject.vI = 3;',
  12810. 'if ($mod.TObject.vI === 4);',
  12811. '$mod.TObject.Sub=null;',
  12812. '$mod.TObject.Sub=null;',
  12813. '$mod.TObject.Sub=null;',
  12814. '']));
  12815. end;
  12816. procedure TTestModule.TestClass_CallClassMethod;
  12817. begin
  12818. StartProgram(false);
  12819. Add('type');
  12820. Add(' TObject = class');
  12821. Add(' public');
  12822. Add(' class var vI: longint;');
  12823. Add(' class var Sub: TObject;');
  12824. Add(' constructor Create;');
  12825. Add(' function GetMore(Par: longint): longint;');
  12826. Add(' class function GetIt(Par: longint): tobject;');
  12827. Add(' end;');
  12828. Add('constructor tobject.create;');
  12829. Add('begin');
  12830. Add(' sub:=getit(3);');
  12831. Add(' vi:=getmore(4);');
  12832. Add(' sub:=Self.getit(5);');
  12833. Add(' vi:=Self.getmore(6);');
  12834. Add('end;');
  12835. Add('function tobject.getmore(par: longint): longint;');
  12836. Add('begin');
  12837. Add(' sub:=getit(11);');
  12838. Add(' vi:=getmore(12);');
  12839. Add(' sub:=self.getit(13);');
  12840. Add(' vi:=self.getmore(14);');
  12841. Add('end;');
  12842. Add('class function tobject.getit(par: longint): tobject;');
  12843. Add('begin');
  12844. Add(' sub:=getit(21);');
  12845. Add(' vi:=sub.getmore(22);');
  12846. Add(' sub:=self.getit(23);');
  12847. Add(' vi:=self.sub.getmore(24);');
  12848. Add('end;');
  12849. Add('var Obj: tobject;');
  12850. Add('begin');
  12851. Add(' obj:=tobject.create;');
  12852. Add(' tobject.getit(5);');
  12853. Add(' obj.getit(6);');
  12854. Add(' obj.sub.getit(7);');
  12855. Add(' obj.sub.getit(8).SUB:=nil;');
  12856. Add(' obj.sub.getit(9).GETIT(10);');
  12857. Add(' obj.sub.getit(11).SuB.getit(12);');
  12858. ConvertProgram;
  12859. CheckSource('TestClass_CallClassMethod',
  12860. LinesToStr([ // statements
  12861. 'rtl.createClass(this,"TObject",null,function(){',
  12862. ' this.vI = 0;',
  12863. ' this.Sub = null;',
  12864. ' this.$init = function () {',
  12865. ' };',
  12866. ' this.$final = function () {',
  12867. ' };',
  12868. ' this.Create = function(){',
  12869. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12870. ' $mod.TObject.vI = this.GetMore(4);',
  12871. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12872. ' $mod.TObject.vI = this.GetMore(6);',
  12873. ' return this;',
  12874. ' };',
  12875. ' this.GetMore = function(Par){',
  12876. ' var Result = 0;',
  12877. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12878. ' $mod.TObject.vI = this.GetMore(12);',
  12879. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12880. ' $mod.TObject.vI = this.GetMore(14);',
  12881. ' return Result;',
  12882. ' };',
  12883. ' this.GetIt = function(Par){',
  12884. ' var Result = null;',
  12885. ' $mod.TObject.Sub = this.GetIt(21);',
  12886. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12887. ' $mod.TObject.Sub = this.GetIt(23);',
  12888. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12889. ' return Result;',
  12890. ' };',
  12891. '});',
  12892. 'this.Obj = null;'
  12893. ]),
  12894. LinesToStr([ // $mod.$main
  12895. '$mod.Obj = $mod.TObject.$create("Create");',
  12896. '$mod.TObject.GetIt(5);',
  12897. '$mod.Obj.$class.GetIt(6);',
  12898. '$mod.Obj.Sub.$class.GetIt(7);',
  12899. '$mod.TObject.Sub=null;',
  12900. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12901. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12902. '']));
  12903. end;
  12904. procedure TTestModule.TestClass_Property;
  12905. begin
  12906. StartProgram(false);
  12907. Add('type');
  12908. Add(' TObject = class');
  12909. Add(' Fx: longint;');
  12910. Add(' Fy: longint;');
  12911. Add(' function GetInt: longint;');
  12912. Add(' procedure SetInt(Value: longint);');
  12913. Add(' procedure DoIt;');
  12914. Add(' property IntA: longint read Fx write Fy;');
  12915. Add(' property IntB: longint read GetInt write SetInt;');
  12916. Add(' end;');
  12917. Add('function tobject.getint: longint;');
  12918. Add('begin');
  12919. Add(' result:=fx;');
  12920. Add('end;');
  12921. Add('procedure tobject.setint(value: longint);');
  12922. Add('begin');
  12923. Add(' if value=fy then exit;');
  12924. Add(' fy:=value;');
  12925. Add('end;');
  12926. Add('procedure tobject.doit;');
  12927. Add('begin');
  12928. Add(' IntA:=IntA+1;');
  12929. Add(' Self.IntA:=Self.IntA+1;');
  12930. Add(' IntB:=IntB+1;');
  12931. Add(' Self.IntB:=Self.IntB+1;');
  12932. Add('end;');
  12933. Add('var Obj: tobject;');
  12934. Add('begin');
  12935. Add(' obj.inta:=obj.inta+1;');
  12936. Add(' if obj.intb=2 then;');
  12937. Add(' obj.intb:=obj.intb+2;');
  12938. Add(' obj.setint(obj.inta);');
  12939. ConvertProgram;
  12940. CheckSource('TestClass_Property',
  12941. LinesToStr([ // statements
  12942. 'rtl.createClass(this, "TObject", null, function () {',
  12943. ' this.$init = function () {',
  12944. ' this.Fx = 0;',
  12945. ' this.Fy = 0;',
  12946. ' };',
  12947. ' this.$final = function () {',
  12948. ' };',
  12949. ' this.GetInt = function () {',
  12950. ' var Result = 0;',
  12951. ' Result = this.Fx;',
  12952. ' return Result;',
  12953. ' };',
  12954. ' this.SetInt = function (Value) {',
  12955. ' if (Value === this.Fy) return;',
  12956. ' this.Fy = Value;',
  12957. ' };',
  12958. ' this.DoIt = function () {',
  12959. ' this.Fy = this.Fx + 1;',
  12960. ' this.Fy = this.Fx + 1;',
  12961. ' this.SetInt(this.GetInt() + 1);',
  12962. ' this.SetInt(this.GetInt() + 1);',
  12963. ' };',
  12964. '});',
  12965. 'this.Obj = null;'
  12966. ]),
  12967. LinesToStr([ // $mod.$main
  12968. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12969. 'if ($mod.Obj.GetInt() === 2);',
  12970. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12971. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12972. ]));
  12973. end;
  12974. procedure TTestModule.TestClass_Property_ClassMethod;
  12975. begin
  12976. StartProgram(false);
  12977. Add([
  12978. 'type',
  12979. ' TObject = class',
  12980. ' class var Fx: longint;',
  12981. ' class var Fy: longint;',
  12982. ' class function GetInt: longint;',
  12983. ' class procedure SetInt(Value: longint);',
  12984. ' end;',
  12985. ' TBird = class',
  12986. ' class procedure DoIt;',
  12987. ' class property IntA: longint read Fx write Fy;',
  12988. ' class property IntB: longint read GetInt write SetInt;',
  12989. ' end;',
  12990. 'class function tobject.getint: longint;',
  12991. 'begin',
  12992. ' result:=fx;',
  12993. 'end;',
  12994. 'class procedure tobject.setint(value: longint);',
  12995. 'begin',
  12996. 'end;',
  12997. 'class procedure tbird.doit;',
  12998. 'begin',
  12999. ' FX:=3;',
  13000. ' IntA:=IntA+1;',
  13001. ' Self.IntA:=Self.IntA+1;',
  13002. ' IntB:=IntB+1;',
  13003. ' Self.IntB:=Self.IntB+1;',
  13004. ' with Self do begin',
  13005. ' FX:=11;',
  13006. ' IntA:=IntA+12;',
  13007. ' IntB:=IntB+13;',
  13008. ' end;',
  13009. 'end;',
  13010. 'var Obj: tbird;',
  13011. 'begin',
  13012. ' tbird.fx:=tbird.fx+1;',
  13013. ' tbird.inta:=tbird.inta+1;',
  13014. ' if tbird.intb=2 then;',
  13015. ' tbird.intb:=tbird.intb+2;',
  13016. ' tbird.setint(tbird.inta);',
  13017. ' obj.inta:=obj.inta+1;',
  13018. ' if obj.intb=2 then;',
  13019. ' obj.intb:=obj.intb+2;',
  13020. ' obj.setint(obj.inta);',
  13021. ' with Tbird do begin',
  13022. ' FX:=FY+1;',
  13023. ' inta:=inta+2;',
  13024. ' intb:=intb+3;',
  13025. ' end;',
  13026. ' with Obj do begin',
  13027. ' FX:=FY+1;',
  13028. ' inta:=inta+2;',
  13029. ' intb:=intb+3;',
  13030. ' end;',
  13031. '']);
  13032. ConvertProgram;
  13033. CheckSource('TestClass_Property_ClassMethod',
  13034. LinesToStr([ // statements
  13035. 'rtl.createClass(this, "TObject", null, function () {',
  13036. ' this.Fx = 0;',
  13037. ' this.Fy = 0;',
  13038. ' this.$init = function () {',
  13039. ' };',
  13040. ' this.$final = function () {',
  13041. ' };',
  13042. ' this.GetInt = function () {',
  13043. ' var Result = 0;',
  13044. ' Result = this.Fx;',
  13045. ' return Result;',
  13046. ' };',
  13047. ' this.SetInt = function (Value) {',
  13048. ' };',
  13049. '});',
  13050. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13051. ' this.DoIt = function () {',
  13052. ' $mod.TObject.Fx = 3;',
  13053. ' $mod.TObject.Fy = this.Fx + 1;',
  13054. ' $mod.TObject.Fy = this.Fx + 1;',
  13055. ' this.SetInt(this.GetInt() + 1);',
  13056. ' this.SetInt(this.GetInt() + 1);',
  13057. ' $mod.TObject.Fx = 11;',
  13058. ' $mod.TObject.Fy = this.Fx + 12;',
  13059. ' this.SetInt(this.GetInt() + 13);',
  13060. ' };',
  13061. '});',
  13062. 'this.Obj = null;'
  13063. ]),
  13064. LinesToStr([ // $mod.$main
  13065. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13066. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13067. 'if ($mod.TBird.GetInt() === 2);',
  13068. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13069. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13070. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13071. 'if ($mod.Obj.$class.GetInt() === 2);',
  13072. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13073. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13074. 'var $with = $mod.TBird;',
  13075. '$mod.TObject.Fx = $with.Fy + 1;',
  13076. '$mod.TObject.Fy = $with.Fx + 2;',
  13077. '$with.SetInt($with.GetInt() + 3);',
  13078. 'var $with1 = $mod.Obj;',
  13079. '$mod.TObject.Fx = $with1.Fy + 1;',
  13080. '$mod.TObject.Fy = $with1.Fx + 2;',
  13081. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13082. '']));
  13083. end;
  13084. procedure TTestModule.TestClass_Property_Indexed;
  13085. begin
  13086. StartProgram(false);
  13087. Add([
  13088. 'type',
  13089. ' TObject = class',
  13090. ' FItems: array of longint;',
  13091. ' function GetItems(Index: longint): longint;',
  13092. ' procedure SetItems(Index: longint; Value: longint);',
  13093. ' procedure DoIt;',
  13094. ' property Items[Index: longint]: longint read getitems write setitems;',
  13095. ' end;',
  13096. 'function tobject.getitems(index: longint): longint;',
  13097. 'begin',
  13098. ' Result:=fitems[index];',
  13099. 'end;',
  13100. 'procedure tobject.setitems(index: longint; value: longint);',
  13101. 'begin',
  13102. ' fitems[index]:=value;',
  13103. 'end;',
  13104. 'procedure tobject.doit;',
  13105. 'begin',
  13106. ' items[1]:=2;',
  13107. ' items[3]:=items[4];',
  13108. ' self.items[5]:=self.items[6];',
  13109. ' items[items[7]]:=items[items[8]];',
  13110. 'end;',
  13111. 'var Obj: tobject;',
  13112. 'begin',
  13113. ' obj.Items[11]:=obj.Items[12];',
  13114. '']);
  13115. ConvertProgram;
  13116. CheckSource('TestClass_Property_Indexed',
  13117. LinesToStr([ // statements
  13118. 'rtl.createClass(this, "TObject", null, function () {',
  13119. ' this.$init = function () {',
  13120. ' this.FItems = [];',
  13121. ' };',
  13122. ' this.$final = function () {',
  13123. ' this.FItems = undefined;',
  13124. ' };',
  13125. ' this.GetItems = function (Index) {',
  13126. ' var Result = 0;',
  13127. ' Result = this.FItems[Index];',
  13128. ' return Result;',
  13129. ' };',
  13130. ' this.SetItems = function (Index, Value) {',
  13131. ' this.FItems[Index] = Value;',
  13132. ' };',
  13133. ' this.DoIt = function () {',
  13134. ' this.SetItems(1, 2);',
  13135. ' this.SetItems(3,this.GetItems(4));',
  13136. ' this.SetItems(5,this.GetItems(6));',
  13137. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13138. ' };',
  13139. '});',
  13140. 'this.Obj = null;'
  13141. ]),
  13142. LinesToStr([ // $mod.$main
  13143. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13144. ]));
  13145. end;
  13146. procedure TTestModule.TestClass_Property_IndexSpec;
  13147. begin
  13148. StartProgram(false);
  13149. Add([
  13150. 'type',
  13151. ' TEnum = (red, blue);',
  13152. ' TObject = class',
  13153. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13154. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13155. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13156. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13157. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13158. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13159. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13160. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13161. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13162. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13163. ' end;',
  13164. 'procedure DoIt(b: boolean); begin end;',
  13165. 'var',
  13166. ' o: TObject;',
  13167. 'begin',
  13168. ' o.B1:=o.B1;',
  13169. ' o.B2:=o.B2;',
  13170. ' o.B3:=o.B3;',
  13171. ' o.I1[''a'']:=o.I1[''b''];',
  13172. ' doit(o.b1);',
  13173. ' doit(o.b2);',
  13174. ' doit(o.i1[''c'']);',
  13175. '']);
  13176. ConvertProgram;
  13177. CheckSource('TestClass_Property_IndexSpec',
  13178. LinesToStr([ // statements
  13179. 'this.TEnum = {',
  13180. ' "0": "red",',
  13181. ' red: 0,',
  13182. ' "1": "blue",',
  13183. ' blue: 1',
  13184. '};',
  13185. 'rtl.createClass(this, "TObject", null, function () {',
  13186. ' this.$init = function () {',
  13187. ' };',
  13188. ' this.$final = function () {',
  13189. ' };',
  13190. '});',
  13191. 'this.DoIt = function (b) {',
  13192. '};',
  13193. 'this.o = null;',
  13194. '']),
  13195. LinesToStr([ // $mod.$main
  13196. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13197. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13198. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13199. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13200. '$mod.DoIt($mod.o.GetIntBool(1));',
  13201. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13202. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13203. '']));
  13204. end;
  13205. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13206. begin
  13207. StartProgram(false);
  13208. Add('type');
  13209. Add(' TArray = array of longint;');
  13210. Add(' TObject = class');
  13211. Add(' FItems: TArray;');
  13212. Add(' function GetItems: tarray;');
  13213. Add(' procedure SetItems(Value: tarray);');
  13214. Add(' property Items: tarray read getitems write setitems;');
  13215. Add(' procedure SetNumbers(const Value: tarray);');
  13216. Add(' property Numbers: tarray write setnumbers;');
  13217. Add(' end;');
  13218. Add('function tobject.getitems: tarray;');
  13219. Add('begin');
  13220. Add(' Result:=fitems;');
  13221. Add('end;');
  13222. Add('procedure tobject.setitems(value: tarray);');
  13223. Add('begin');
  13224. Add(' fitems:=value;');
  13225. Add(' fitems:=nil;');
  13226. Add(' Items:=nil;');
  13227. Add(' Items:=Items;');
  13228. Add(' Items[1]:=2;');
  13229. Add(' fitems[3]:=Items[4];');
  13230. Add(' Items[5]:=Items[6];');
  13231. Add(' Self.Items[7]:=8;');
  13232. Add(' Self.Items[9]:=Self.Items[10];');
  13233. Add(' Items[Items[11]]:=Items[Items[12]];');
  13234. Add('end;');
  13235. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13236. Add('begin;');
  13237. Add(' Numbers:=nil;');
  13238. Add(' Numbers:=Value;');
  13239. Add(' Self.Numbers:=Value;');
  13240. Add('end;');
  13241. Add('var Obj: tobject;');
  13242. Add('begin');
  13243. Add(' obj.items:=nil;');
  13244. Add(' obj.items:=obj.items;');
  13245. Add(' obj.items[11]:=obj.items[12];');
  13246. ConvertProgram;
  13247. CheckSource('TestClass_PropertyOfTypeArray',
  13248. LinesToStr([ // statements
  13249. 'rtl.createClass(this, "TObject", null, function () {',
  13250. ' this.$init = function () {',
  13251. ' this.FItems = [];',
  13252. ' };',
  13253. ' this.$final = function () {',
  13254. ' this.FItems = undefined;',
  13255. ' };',
  13256. ' this.GetItems = function () {',
  13257. ' var Result = [];',
  13258. ' Result = rtl.arrayRef(this.FItems);',
  13259. ' return Result;',
  13260. ' };',
  13261. ' this.SetItems = function (Value) {',
  13262. ' this.FItems = rtl.arrayRef(Value);',
  13263. ' this.FItems = [];',
  13264. ' this.SetItems([]);',
  13265. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13266. ' this.GetItems()[1] = 2;',
  13267. ' this.FItems[3] = this.GetItems()[4];',
  13268. ' this.GetItems()[5] = this.GetItems()[6];',
  13269. ' this.GetItems()[7] = 8;',
  13270. ' this.GetItems()[9] = this.GetItems()[10];',
  13271. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13272. ' };',
  13273. ' this.SetNumbers = function (Value) {',
  13274. ' this.SetNumbers([]);',
  13275. ' this.SetNumbers(Value);',
  13276. ' this.SetNumbers(Value);',
  13277. ' };',
  13278. '});',
  13279. 'this.Obj = null;'
  13280. ]),
  13281. LinesToStr([ // $mod.$main
  13282. '$mod.Obj.SetItems([]);',
  13283. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13284. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13285. ]));
  13286. end;
  13287. procedure TTestModule.TestClass_PropertyDefault;
  13288. begin
  13289. StartProgram(false);
  13290. Add([
  13291. 'type',
  13292. ' TArray = array of longint;',
  13293. ' TObject = class',
  13294. ' end;',
  13295. ' TBird = class',
  13296. ' FItems: TArray;',
  13297. ' function GetItems(Index: longint): longint;',
  13298. ' procedure SetItems(Index, Value: longint);',
  13299. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13300. ' end;',
  13301. 'function TBird.getitems(index: longint): longint;',
  13302. 'begin',
  13303. 'end;',
  13304. 'procedure TBird.setitems(index, value: longint);',
  13305. 'begin',
  13306. ' Self[1]:=2;',
  13307. ' Self[3]:=Self[index];',
  13308. ' Self[index]:=Self[Self[value]];',
  13309. ' Self[Self[4]]:=value;',
  13310. 'end;',
  13311. 'var',
  13312. ' Bird: TBird;',
  13313. ' Obj: TObject;',
  13314. 'begin',
  13315. ' bird[11]:=12;',
  13316. ' bird[13]:=bird[14];',
  13317. ' bird[Bird[15]]:=bird[Bird[15]];',
  13318. ' TBird(obj)[16]:=TBird(obj)[17];',
  13319. ' (obj as tbird)[18]:=19;',
  13320. '']);
  13321. ConvertProgram;
  13322. CheckSource('TestClass_PropertyDefault',
  13323. LinesToStr([ // statements
  13324. 'rtl.createClass(this, "TObject", null, function () {',
  13325. ' this.$init = function () {',
  13326. ' };',
  13327. ' this.$final = function () {',
  13328. ' };',
  13329. '});',
  13330. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13331. ' this.$init = function () {',
  13332. ' $mod.TObject.$init.call(this);',
  13333. ' this.FItems = [];',
  13334. ' };',
  13335. ' this.$final = function () {',
  13336. ' this.FItems = undefined;',
  13337. ' $mod.TObject.$final.call(this);',
  13338. ' };',
  13339. ' this.GetItems = function (Index) {',
  13340. ' var Result = 0;',
  13341. ' return Result;',
  13342. ' };',
  13343. ' this.SetItems = function (Index, Value) {',
  13344. ' this.SetItems(1, 2);',
  13345. ' this.SetItems(3, this.GetItems(Index));',
  13346. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13347. ' this.SetItems(this.GetItems(4), Value);',
  13348. ' };',
  13349. '});',
  13350. 'this.Bird = null;',
  13351. 'this.Obj = null;',
  13352. '']),
  13353. LinesToStr([ // $mod.$main
  13354. '$mod.Bird.SetItems(11, 12);',
  13355. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13356. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13357. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13358. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13359. '']));
  13360. end;
  13361. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13362. begin
  13363. StartProgram(false);
  13364. Add([
  13365. 'type',
  13366. ' TObject = class end;',
  13367. ' TAlphaList = class',
  13368. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13369. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13370. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13371. ' end;',
  13372. ' TBetaList = class',
  13373. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13374. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13375. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13376. ' end;',
  13377. ' TBird = class',
  13378. ' procedure DoIt;',
  13379. ' end;',
  13380. 'procedure TBird.DoIt;',
  13381. 'var',
  13382. ' List: TAlphaList;',
  13383. 'begin',
  13384. ' if TBetaList(List[true])[3]=nil then ;',
  13385. ' TBetaList(List[false])[5]:=nil;',
  13386. 'end;',
  13387. 'var',
  13388. ' List: TAlphaList;',
  13389. 'begin',
  13390. ' if TBetaList(List[true])[3]=nil then ;',
  13391. ' TBetaList(List[false])[5]:=nil;',
  13392. '']);
  13393. ConvertProgram;
  13394. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13395. LinesToStr([ // statements
  13396. 'rtl.createClass(this, "TObject", null, function () {',
  13397. ' this.$init = function () {',
  13398. ' };',
  13399. ' this.$final = function () {',
  13400. ' };',
  13401. '});',
  13402. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13403. '});',
  13404. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13405. '});',
  13406. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13407. ' this.DoIt = function () {',
  13408. ' var List = null;',
  13409. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13410. ' List.GetAlphas(false).SetBetas(5, null);',
  13411. ' };',
  13412. '});',
  13413. 'this.List = null;',
  13414. '']),
  13415. LinesToStr([ // $mod.$main
  13416. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13417. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13418. '']));
  13419. end;
  13420. procedure TTestModule.TestClass_PropertyOverride;
  13421. begin
  13422. StartProgram(false);
  13423. Add('type');
  13424. Add(' integer = longint;');
  13425. Add(' TObject = class');
  13426. Add(' FItem: integer;');
  13427. Add(' function GetItem: integer; external name ''GetItem'';');
  13428. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13429. Add(' property Item: integer read getitem write setitem;');
  13430. Add(' end;');
  13431. Add(' TCar = class');
  13432. Add(' FBag: integer;');
  13433. Add(' function GetBag: integer; external name ''GetBag'';');
  13434. Add(' property Item read getbag;');
  13435. Add(' end;');
  13436. Add('var');
  13437. Add(' Obj: tobject;');
  13438. Add(' Car: tcar;');
  13439. Add('begin');
  13440. Add(' Obj.Item:=Obj.Item;');
  13441. Add(' Car.Item:=Car.Item;');
  13442. ConvertProgram;
  13443. CheckSource('TestClass_PropertyOverride',
  13444. LinesToStr([ // statements
  13445. 'rtl.createClass(this, "TObject", null, function () {',
  13446. ' this.$init = function () {',
  13447. ' this.FItem = 0;',
  13448. ' };',
  13449. ' this.$final = function () {',
  13450. ' };',
  13451. '});',
  13452. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13453. ' this.$init = function () {',
  13454. ' $mod.TObject.$init.call(this);',
  13455. ' this.FBag = 0;',
  13456. ' };',
  13457. '});',
  13458. 'this.Obj = null;',
  13459. 'this.Car = null;',
  13460. '']),
  13461. LinesToStr([ // $mod.$main
  13462. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13463. '$mod.Car.SetItem($mod.Car.GetBag());',
  13464. '']));
  13465. end;
  13466. procedure TTestModule.TestClass_PropertyIncVisibility;
  13467. begin
  13468. AddModuleWithIntfImplSrc('unit1.pp',
  13469. LinesToStr([
  13470. 'type',
  13471. ' TNumber = longint;',
  13472. ' TInteger = longint;',
  13473. ' TObject = class',
  13474. ' private',
  13475. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13476. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13477. ' protected',
  13478. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13479. ' end;']),
  13480. LinesToStr([
  13481. '']));
  13482. StartProgram(true);
  13483. Add([
  13484. 'uses unit1;',
  13485. 'type',
  13486. ' TBird = class',
  13487. ' public',
  13488. ' property Items;',
  13489. ' end;',
  13490. 'procedure DoIt(i: TInteger);',
  13491. 'begin',
  13492. 'end;',
  13493. 'var b: TBird;',
  13494. 'begin',
  13495. ' b.Items[1]:=2;',
  13496. ' b.Items[3]:=b.Items[4];',
  13497. ' DoIt(b.Items[5]);',
  13498. '']);
  13499. ConvertProgram;
  13500. CheckSource('TestClass_PropertyIncVisibility',
  13501. LinesToStr([ // statements
  13502. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13503. '});',
  13504. 'this.DoIt = function (i) {',
  13505. '};',
  13506. 'this.b = null;'
  13507. ]),
  13508. LinesToStr([ // $mod.$main
  13509. '$mod.b.SetItems(1, 2);',
  13510. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13511. '$mod.DoIt($mod.b.GetItems(5));'
  13512. ]));
  13513. end;
  13514. procedure TTestModule.TestClass_Assigned;
  13515. begin
  13516. StartProgram(false);
  13517. Add('type');
  13518. Add(' TObject = class');
  13519. Add(' end;');
  13520. Add('var');
  13521. Add(' Obj: tobject;');
  13522. Add(' b: boolean;');
  13523. Add('begin');
  13524. Add(' if Assigned(obj) then ;');
  13525. Add(' b:=Assigned(obj) or false;');
  13526. ConvertProgram;
  13527. CheckSource('TestClass_Assigned',
  13528. LinesToStr([ // statements
  13529. 'rtl.createClass(this, "TObject", null, function () {',
  13530. ' this.$init = function () {',
  13531. ' };',
  13532. ' this.$final = function () {',
  13533. ' };',
  13534. '});',
  13535. 'this.Obj = null;',
  13536. 'this.b = false;'
  13537. ]),
  13538. LinesToStr([ // $mod.$main
  13539. 'if ($mod.Obj != null);',
  13540. '$mod.b = ($mod.Obj != null) || false;'
  13541. ]));
  13542. end;
  13543. procedure TTestModule.TestClass_WithClassDoCreate;
  13544. begin
  13545. StartProgram(false);
  13546. Add('type');
  13547. Add(' TObject = class');
  13548. Add(' aBool: boolean;');
  13549. Add(' Arr: array of boolean;');
  13550. Add(' constructor Create;');
  13551. Add(' end;');
  13552. Add('constructor TObject.Create; begin end;');
  13553. Add('var');
  13554. Add(' Obj: tobject;');
  13555. Add(' b: boolean;');
  13556. Add('begin');
  13557. Add(' with tobject.create do begin');
  13558. Add(' b:=abool;');
  13559. Add(' abool:=b;');
  13560. Add(' b:=arr[1];');
  13561. Add(' arr[2]:=b;');
  13562. Add(' end;');
  13563. Add(' with tobject do');
  13564. Add(' obj:=create;');
  13565. Add(' with obj do begin');
  13566. Add(' create;');
  13567. Add(' b:=abool;');
  13568. Add(' abool:=b;');
  13569. Add(' b:=arr[3];');
  13570. Add(' arr[4]:=b;');
  13571. Add(' end;');
  13572. ConvertProgram;
  13573. CheckSource('TestClass_WithClassDoCreate',
  13574. LinesToStr([ // statements
  13575. 'rtl.createClass(this, "TObject", null, function () {',
  13576. ' this.$init = function () {',
  13577. ' this.aBool = false;',
  13578. ' this.Arr = [];',
  13579. ' };',
  13580. ' this.$final = function () {',
  13581. ' this.Arr = undefined;',
  13582. ' };',
  13583. ' this.Create = function () {',
  13584. ' return this;',
  13585. ' };',
  13586. '});',
  13587. 'this.Obj = null;',
  13588. 'this.b = false;'
  13589. ]),
  13590. LinesToStr([ // $mod.$main
  13591. 'var $with = $mod.TObject.$create("Create");',
  13592. '$mod.b = $with.aBool;',
  13593. '$with.aBool = $mod.b;',
  13594. '$mod.b = $with.Arr[1];',
  13595. '$with.Arr[2] = $mod.b;',
  13596. 'var $with1 = $mod.TObject;',
  13597. '$mod.Obj = $with1.$create("Create");',
  13598. 'var $with2 = $mod.Obj;',
  13599. '$with2.Create();',
  13600. '$mod.b = $with2.aBool;',
  13601. '$with2.aBool = $mod.b;',
  13602. '$mod.b = $with2.Arr[3];',
  13603. '$with2.Arr[4] = $mod.b;',
  13604. '']));
  13605. end;
  13606. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13607. begin
  13608. StartProgram(false);
  13609. Add('type');
  13610. Add(' TObject = class');
  13611. Add(' FInt: longint;');
  13612. Add(' constructor Create;');
  13613. Add(' function GetSize: longint;');
  13614. Add(' procedure SetSize(Value: longint);');
  13615. Add(' property Int: longint read FInt write FInt;');
  13616. Add(' property Size: longint read GetSize write SetSize;');
  13617. Add(' end;');
  13618. Add('constructor TObject.Create; begin end;');
  13619. Add('function TObject.GetSize: longint; begin; end;');
  13620. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13621. Add('var');
  13622. Add(' Obj: tobject;');
  13623. Add(' i: longint;');
  13624. Add('begin');
  13625. Add(' with TObject.Create do begin');
  13626. Add(' i:=int;');
  13627. Add(' int:=i;');
  13628. Add(' i:=size;');
  13629. Add(' size:=i;');
  13630. Add(' end;');
  13631. Add(' with obj do begin');
  13632. Add(' i:=int;');
  13633. Add(' int:=i;');
  13634. Add(' i:=size;');
  13635. Add(' size:=i;');
  13636. Add(' end;');
  13637. ConvertProgram;
  13638. CheckSource('TestClass_WithClassInstDoProperty',
  13639. LinesToStr([ // statements
  13640. 'rtl.createClass(this, "TObject", null, function () {',
  13641. ' this.$init = function () {',
  13642. ' this.FInt = 0;',
  13643. ' };',
  13644. ' this.$final = function () {',
  13645. ' };',
  13646. ' this.Create = function () {',
  13647. ' return this;',
  13648. ' };',
  13649. ' this.GetSize = function () {',
  13650. ' var Result = 0;',
  13651. ' return Result;',
  13652. ' };',
  13653. ' this.SetSize = function (Value) {',
  13654. ' };',
  13655. '});',
  13656. 'this.Obj = null;',
  13657. 'this.i = 0;'
  13658. ]),
  13659. LinesToStr([ // $mod.$main
  13660. 'var $with = $mod.TObject.$create("Create");',
  13661. '$mod.i = $with.FInt;',
  13662. '$with.FInt = $mod.i;',
  13663. '$mod.i = $with.GetSize();',
  13664. '$with.SetSize($mod.i);',
  13665. 'var $with1 = $mod.Obj;',
  13666. '$mod.i = $with1.FInt;',
  13667. '$with1.FInt = $mod.i;',
  13668. '$mod.i = $with1.GetSize();',
  13669. '$with1.SetSize($mod.i);',
  13670. '']));
  13671. end;
  13672. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13673. begin
  13674. StartProgram(false);
  13675. Add('type');
  13676. Add(' TObject = class');
  13677. Add(' constructor Create;');
  13678. Add(' function GetItems(Index: longint): longint;');
  13679. Add(' procedure SetItems(Index, Value: longint);');
  13680. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13681. Add(' end;');
  13682. Add('constructor TObject.Create; begin end;');
  13683. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13684. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13685. Add('var');
  13686. Add(' Obj: tobject;');
  13687. Add(' i: longint;');
  13688. Add('begin');
  13689. Add(' with TObject.Create do begin');
  13690. Add(' i:=Items[1];');
  13691. Add(' Items[2]:=i;');
  13692. Add(' end;');
  13693. Add(' with obj do begin');
  13694. Add(' i:=Items[3];');
  13695. Add(' Items[4]:=i;');
  13696. Add(' end;');
  13697. ConvertProgram;
  13698. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13699. LinesToStr([ // statements
  13700. 'rtl.createClass(this, "TObject", null, function () {',
  13701. ' this.$init = function () {',
  13702. ' };',
  13703. ' this.$final = function () {',
  13704. ' };',
  13705. ' this.Create = function () {',
  13706. ' return this;',
  13707. ' };',
  13708. ' this.GetItems = function (Index) {',
  13709. ' var Result = 0;',
  13710. ' return Result;',
  13711. ' };',
  13712. ' this.SetItems = function (Index, Value) {',
  13713. ' };',
  13714. '});',
  13715. 'this.Obj = null;',
  13716. 'this.i = 0;'
  13717. ]),
  13718. LinesToStr([ // $mod.$main
  13719. 'var $with = $mod.TObject.$create("Create");',
  13720. '$mod.i = $with.GetItems(1);',
  13721. '$with.SetItems(2, $mod.i);',
  13722. 'var $with1 = $mod.Obj;',
  13723. '$mod.i = $with1.GetItems(3);',
  13724. '$with1.SetItems(4, $mod.i);',
  13725. '']));
  13726. end;
  13727. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13728. begin
  13729. StartProgram(false);
  13730. Add('type');
  13731. Add(' TObject = class');
  13732. Add(' constructor Create;');
  13733. Add(' function GetSize: longint;');
  13734. Add(' procedure SetSize(Value: longint);');
  13735. Add(' end;');
  13736. Add('constructor TObject.Create; begin end;');
  13737. Add('function TObject.GetSize: longint; begin; end;');
  13738. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13739. Add('var');
  13740. Add(' Obj: tobject;');
  13741. Add(' i: longint;');
  13742. Add('begin');
  13743. Add(' with TObject.Create do begin');
  13744. Add(' i:=GetSize;');
  13745. Add(' i:=GetSize();');
  13746. Add(' SetSize(i);');
  13747. Add(' end;');
  13748. Add(' with obj do begin');
  13749. Add(' i:=GetSize;');
  13750. Add(' i:=GetSize();');
  13751. Add(' SetSize(i);');
  13752. Add(' end;');
  13753. ConvertProgram;
  13754. CheckSource('TestClass_WithClassInstDoFunc',
  13755. LinesToStr([ // statements
  13756. 'rtl.createClass(this, "TObject", null, function () {',
  13757. ' this.$init = function () {',
  13758. ' };',
  13759. ' this.$final = function () {',
  13760. ' };',
  13761. ' this.Create = function () {',
  13762. ' return this;',
  13763. ' };',
  13764. ' this.GetSize = function () {',
  13765. ' var Result = 0;',
  13766. ' return Result;',
  13767. ' };',
  13768. ' this.SetSize = function (Value) {',
  13769. ' };',
  13770. '});',
  13771. 'this.Obj = null;',
  13772. 'this.i = 0;'
  13773. ]),
  13774. LinesToStr([ // $mod.$main
  13775. 'var $with = $mod.TObject.$create("Create");',
  13776. '$mod.i = $with.GetSize();',
  13777. '$mod.i = $with.GetSize();',
  13778. '$with.SetSize($mod.i);',
  13779. 'var $with1 = $mod.Obj;',
  13780. '$mod.i = $with1.GetSize();',
  13781. '$mod.i = $with1.GetSize();',
  13782. '$with1.SetSize($mod.i);',
  13783. '']));
  13784. end;
  13785. procedure TTestModule.TestClass_TypeCast;
  13786. begin
  13787. StartProgram(false);
  13788. Add('type');
  13789. Add(' TObject = class');
  13790. Add(' Next: TObject;');
  13791. Add(' constructor Create;');
  13792. Add(' end;');
  13793. Add(' TControl = class(TObject)');
  13794. Add(' Arr: array of TObject;');
  13795. Add(' function GetIt(vI: longint = 0): TObject;');
  13796. Add(' end;');
  13797. Add('constructor tobject.create; begin end;');
  13798. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13799. Add('var');
  13800. Add(' Obj: tobject;');
  13801. Add('begin');
  13802. Add(' obj:=tcontrol(obj).next;');
  13803. Add(' tcontrol(obj):=nil;');
  13804. Add(' obj:=tcontrol(obj);');
  13805. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13806. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13807. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13808. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13809. Add(' obj:=tcontrol(nil);');
  13810. ConvertProgram;
  13811. CheckSource('TestClass_TypeCast',
  13812. LinesToStr([ // statements
  13813. 'rtl.createClass(this, "TObject", null, function () {',
  13814. ' this.$init = function () {',
  13815. ' this.Next = null;',
  13816. ' };',
  13817. ' this.$final = function () {',
  13818. ' this.Next = undefined;',
  13819. ' };',
  13820. ' this.Create = function () {',
  13821. ' return this;',
  13822. ' };',
  13823. '});',
  13824. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  13825. ' this.$init = function () {',
  13826. ' $mod.TObject.$init.call(this);',
  13827. ' this.Arr = [];',
  13828. ' };',
  13829. ' this.$final = function () {',
  13830. ' this.Arr = undefined;',
  13831. ' $mod.TObject.$final.call(this);',
  13832. ' };',
  13833. ' this.GetIt = function (vI) {',
  13834. ' var Result = null;',
  13835. ' return Result;',
  13836. ' };',
  13837. '});',
  13838. 'this.Obj = null;'
  13839. ]),
  13840. LinesToStr([ // $mod.$main
  13841. '$mod.Obj = $mod.Obj.Next;',
  13842. '$mod.Obj = null;',
  13843. '$mod.Obj = $mod.Obj;',
  13844. '$mod.Obj = $mod.Obj.GetIt(0);',
  13845. '$mod.Obj = $mod.Obj.GetIt(0);',
  13846. '$mod.Obj = $mod.Obj.GetIt(1);',
  13847. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13848. '$mod.Obj = null;',
  13849. '']));
  13850. end;
  13851. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13852. begin
  13853. StartProgram(false);
  13854. Add('type');
  13855. Add(' TObject = class end;');
  13856. Add('procedure ProcA(var A);');
  13857. Add('begin');
  13858. Add(' TObject(A):=nil;');
  13859. Add(' TObject(A):=TObject(A);');
  13860. Add(' if TObject(A)=nil then ;');
  13861. Add(' if nil=TObject(A) then ;');
  13862. Add('end;');
  13863. Add('procedure ProcB(out A);');
  13864. Add('begin');
  13865. Add(' TObject(A):=nil;');
  13866. Add(' TObject(A):=TObject(A);');
  13867. Add(' if TObject(A)=nil then ;');
  13868. Add(' if nil=TObject(A) then ;');
  13869. Add('end;');
  13870. Add('procedure ProcC(const A);');
  13871. Add('begin');
  13872. Add(' if TObject(A)=nil then ;');
  13873. Add(' if nil=TObject(A) then ;');
  13874. Add('end;');
  13875. Add('var o: TObject;');
  13876. Add('begin');
  13877. Add(' ProcA(o);');
  13878. Add(' ProcB(o);');
  13879. Add(' ProcC(o);');
  13880. ConvertProgram;
  13881. CheckSource('TestClass_TypeCastUntypedParam',
  13882. LinesToStr([ // statements
  13883. 'rtl.createClass(this, "TObject", null, function () {',
  13884. ' this.$init = function () {',
  13885. ' };',
  13886. ' this.$final = function () {',
  13887. ' };',
  13888. '});',
  13889. 'this.ProcA = function (A) {',
  13890. ' A.set(null);',
  13891. ' A.set(A.get());',
  13892. ' if (A.get() === null);',
  13893. ' if (null === A.get());',
  13894. '};',
  13895. 'this.ProcB = function (A) {',
  13896. ' A.set(null);',
  13897. ' A.set(A.get());',
  13898. ' if (A.get() === null);',
  13899. ' if (null === A.get());',
  13900. '};',
  13901. 'this.ProcC = function (A) {',
  13902. ' if (A === null);',
  13903. ' if (null === A);',
  13904. '};',
  13905. 'this.o = null;',
  13906. '']),
  13907. LinesToStr([ // $mod.$main
  13908. '$mod.ProcA({',
  13909. ' p: $mod,',
  13910. ' get: function () {',
  13911. ' return this.p.o;',
  13912. ' },',
  13913. ' set: function (v) {',
  13914. ' this.p.o = v;',
  13915. ' }',
  13916. '});',
  13917. '$mod.ProcB({',
  13918. ' p: $mod,',
  13919. ' get: function () {',
  13920. ' return this.p.o;',
  13921. ' },',
  13922. ' set: function (v) {',
  13923. ' this.p.o = v;',
  13924. ' }',
  13925. '});',
  13926. '$mod.ProcC($mod.o);',
  13927. '']));
  13928. end;
  13929. procedure TTestModule.TestClass_Overloads;
  13930. begin
  13931. StartProgram(false);
  13932. Add('type');
  13933. Add(' TObject = class');
  13934. Add(' procedure DoIt;');
  13935. Add(' procedure DoIt(vI: longint);');
  13936. Add(' end;');
  13937. Add('procedure TObject.DoIt;');
  13938. Add('begin');
  13939. Add(' DoIt;');
  13940. Add(' DoIt(1);');
  13941. Add('end;');
  13942. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13943. Add('begin');
  13944. ConvertProgram;
  13945. CheckSource('TestClass_Overloads',
  13946. LinesToStr([ // statements
  13947. 'rtl.createClass(this, "TObject", null, function () {',
  13948. ' this.$init = function () {',
  13949. ' };',
  13950. ' this.$final = function () {',
  13951. ' };',
  13952. ' this.DoIt = function () {',
  13953. ' this.DoIt();',
  13954. ' this.DoIt$1(1);',
  13955. ' };',
  13956. ' this.DoIt$1 = function (vI) {',
  13957. ' };',
  13958. '});',
  13959. '']),
  13960. LinesToStr([ // $mod.$main
  13961. '']));
  13962. end;
  13963. procedure TTestModule.TestClass_OverloadsAncestor;
  13964. begin
  13965. StartProgram(false);
  13966. Add('type');
  13967. Add(' TObject = class;');
  13968. Add(' TObject = class');
  13969. Add(' procedure DoIt(vA: longint);');
  13970. Add(' procedure DoIt(vA, vB: longint);');
  13971. Add(' end;');
  13972. Add(' TCar = class;');
  13973. Add(' TCar = class');
  13974. Add(' procedure DoIt(vA: longint);');
  13975. Add(' procedure DoIt(vA, vB: longint);');
  13976. Add(' end;');
  13977. Add('procedure tobject.doit(va: longint);');
  13978. Add('begin');
  13979. Add(' doit(1);');
  13980. Add(' doit(1,2);');
  13981. Add('end;');
  13982. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13983. Add('procedure tcar.doit(va: longint);');
  13984. Add('begin');
  13985. Add(' doit(1);');
  13986. Add(' doit(1,2);');
  13987. Add(' inherited doit(1);');
  13988. Add(' inherited doit(1,2);');
  13989. Add('end;');
  13990. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13991. Add('begin');
  13992. ConvertProgram;
  13993. CheckSource('TestClass_OverloadsAncestor',
  13994. LinesToStr([ // statements
  13995. 'rtl.createClass(this, "TObject", null, function () {',
  13996. ' this.$init = function () {',
  13997. ' };',
  13998. ' this.$final = function () {',
  13999. ' };',
  14000. ' this.DoIt = function (vA) {',
  14001. ' this.DoIt(1);',
  14002. ' this.DoIt$1(1,2);',
  14003. ' };',
  14004. ' this.DoIt$1 = function (vA, vB) {',
  14005. ' };',
  14006. '});',
  14007. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14008. ' this.DoIt$2 = function (vA) {',
  14009. ' this.DoIt$2(1);',
  14010. ' this.DoIt$3(1, 2);',
  14011. ' $mod.TObject.DoIt.call(this, 1);',
  14012. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14013. ' };',
  14014. ' this.DoIt$3 = function (vA, vB) {',
  14015. ' };',
  14016. '});',
  14017. '']),
  14018. LinesToStr([ // $mod.$main
  14019. '']));
  14020. end;
  14021. procedure TTestModule.TestClass_OverloadConstructor;
  14022. begin
  14023. StartProgram(false);
  14024. Add('type');
  14025. Add(' TObject = class');
  14026. Add(' constructor Create(vA: longint);');
  14027. Add(' constructor Create(vA, vB: longint);');
  14028. Add(' end;');
  14029. Add(' TCar = class');
  14030. Add(' constructor Create(vA: longint);');
  14031. Add(' constructor Create(vA, vB: longint);');
  14032. Add(' end;');
  14033. Add('constructor tobject.create(va: longint);');
  14034. Add('begin');
  14035. Add(' create(1);');
  14036. Add(' create(1,2);');
  14037. Add('end;');
  14038. Add('constructor tobject.create(va, vb: longint); begin end;');
  14039. Add('constructor tcar.create(va: longint);');
  14040. Add('begin');
  14041. Add(' create(1);');
  14042. Add(' create(1,2);');
  14043. Add(' inherited create(1);');
  14044. Add(' inherited create(1,2);');
  14045. Add('end;');
  14046. Add('constructor tcar.create(va, vb: longint); begin end;');
  14047. Add('begin');
  14048. Add(' tobject.create(1);');
  14049. Add(' tobject.create(1,2);');
  14050. Add(' tcar.create(1);');
  14051. Add(' tcar.create(1,2);');
  14052. ConvertProgram;
  14053. CheckSource('TestClass_OverloadConstructor',
  14054. LinesToStr([ // statements
  14055. 'rtl.createClass(this, "TObject", null, function () {',
  14056. ' this.$init = function () {',
  14057. ' };',
  14058. ' this.$final = function () {',
  14059. ' };',
  14060. ' this.Create = function (vA) {',
  14061. ' this.Create(1);',
  14062. ' this.Create$1(1,2);',
  14063. ' return this;',
  14064. ' };',
  14065. ' this.Create$1 = function (vA, vB) {',
  14066. ' return this;',
  14067. ' };',
  14068. '});',
  14069. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14070. ' this.Create$2 = function (vA) {',
  14071. ' this.Create$2(1);',
  14072. ' this.Create$3(1, 2);',
  14073. ' $mod.TObject.Create.call(this, 1);',
  14074. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14075. ' return this;',
  14076. ' };',
  14077. ' this.Create$3 = function (vA, vB) {',
  14078. ' return this;',
  14079. ' };',
  14080. '});',
  14081. '']),
  14082. LinesToStr([ // $mod.$main
  14083. '$mod.TObject.$create("Create", [1]);',
  14084. '$mod.TObject.$create("Create$1", [1, 2]);',
  14085. '$mod.TCar.$create("Create$2", [1]);',
  14086. '$mod.TCar.$create("Create$3", [1, 2]);',
  14087. '']));
  14088. end;
  14089. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14090. begin
  14091. StartProgram(false);
  14092. Add([
  14093. '{$mode delphi}',
  14094. 'type',
  14095. ' TObject = class end;',
  14096. ' TBird = class',
  14097. ' function {#a}GetValue: longint; overload; virtual;',
  14098. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14099. ' end;',
  14100. ' TEagle = class(TBird)',
  14101. ' function {#c}GetValue: longint; overload; override;',
  14102. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14103. ' end;',
  14104. 'function TBird.GetValue: longint;',
  14105. 'begin',
  14106. ' if 3={@a}GetValue then ;',
  14107. ' if 4={@b}GetValue(5) then ;',
  14108. 'end;',
  14109. 'function TBird.GetValue(AValue: longint): longint;',
  14110. 'begin',
  14111. 'end;',
  14112. 'function TEagle.GetValue: longint;',
  14113. 'begin',
  14114. ' if 13={@c}GetValue then ;',
  14115. ' if 14={@d}GetValue(15) then ;',
  14116. ' if 15=inherited {@a}GetValue then ;',
  14117. ' if 16=inherited {@b}GetValue(17) then ;',
  14118. 'end;',
  14119. 'function TEagle.GetValue(AValue: longint): longint;',
  14120. 'begin',
  14121. 'end;',
  14122. 'var',
  14123. ' e: TEagle;',
  14124. 'begin',
  14125. ' if 23=e.{@c}GetValue then ;',
  14126. ' if 24=e.{@d}GetValue(25) then ;']);
  14127. ConvertProgram;
  14128. CheckSource('TestClass_OverloadDelphiOverride',
  14129. LinesToStr([ // statements
  14130. 'rtl.createClass(this, "TObject", null, function () {',
  14131. ' this.$init = function () {',
  14132. ' };',
  14133. ' this.$final = function () {',
  14134. ' };',
  14135. '});',
  14136. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14137. ' this.GetValue = function () {',
  14138. ' var Result = 0;',
  14139. ' if (3 === this.GetValue()) ;',
  14140. ' if (4 === this.GetValue$1(5)) ;',
  14141. ' return Result;',
  14142. ' };',
  14143. ' this.GetValue$1 = function (AValue) {',
  14144. ' var Result = 0;',
  14145. ' return Result;',
  14146. ' };',
  14147. '});',
  14148. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14149. ' this.GetValue = function () {',
  14150. ' var Result = 0;',
  14151. ' if (13 === this.GetValue()) ;',
  14152. ' if (14 === this.GetValue$1(15)) ;',
  14153. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14154. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14155. ' return Result;',
  14156. ' };',
  14157. ' this.GetValue$1 = function (AValue) {',
  14158. ' var Result = 0;',
  14159. ' return Result;',
  14160. ' };',
  14161. '});',
  14162. 'this.e = null;',
  14163. '']),
  14164. LinesToStr([ // $mod.$main
  14165. 'if (23 === $mod.e.GetValue()) ;',
  14166. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14167. '']));
  14168. end;
  14169. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14170. begin
  14171. StartProgram(false);
  14172. Add([
  14173. '{$mode delphi}',
  14174. 'type',
  14175. ' TObject = class end;',
  14176. ' TAnimal = class',
  14177. ' public',
  14178. ' {#animal_a}A: longint;',
  14179. ' function {#animal_b}B: longint;',
  14180. ' end;',
  14181. ' TBird = class(TAnimal)',
  14182. ' public',
  14183. ' {#bird_a}A: double;',
  14184. ' {#bird_b}B: boolean;',
  14185. ' end;',
  14186. ' TEagle = class(TBird)',
  14187. ' public',
  14188. ' function {#eagle_a}A: boolean;',
  14189. ' {#eagle_b}B: double;',
  14190. ' end;',
  14191. 'function TAnimal.B: longint;',
  14192. 'begin',
  14193. 'end;',
  14194. 'function TEagle.A: boolean;',
  14195. 'begin',
  14196. ' {@eagle_b}B:=3.3;',
  14197. ' {@eagle_a}A();',
  14198. ' TBird(Self).{@bird_b}B:=true;',
  14199. ' TAnimal(Self).{@animal_a}A:=17;',
  14200. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14201. 'end;',
  14202. 'var',
  14203. ' e: TEagle;',
  14204. 'begin',
  14205. ' e.{@eagle_b}B:=5.3;',
  14206. ' if e.{@eagle_a}A then ;',
  14207. '']);
  14208. ConvertProgram;
  14209. CheckSource('TestClass_ReintroduceVarDelphi',
  14210. LinesToStr([ // statements
  14211. 'rtl.createClass(this, "TObject", null, function () {',
  14212. ' this.$init = function () {',
  14213. ' };',
  14214. ' this.$final = function () {',
  14215. ' };',
  14216. '});',
  14217. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14218. ' this.$init = function () {',
  14219. ' $mod.TObject.$init.call(this);',
  14220. ' this.A = 0;',
  14221. ' };',
  14222. ' this.B = function () {',
  14223. ' var Result = 0;',
  14224. ' return Result;',
  14225. ' };',
  14226. '});',
  14227. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14228. ' this.$init = function () {',
  14229. ' $mod.TAnimal.$init.call(this);',
  14230. ' this.A$1 = 0.0;',
  14231. ' this.B$1 = false;',
  14232. ' };',
  14233. '});',
  14234. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14235. ' this.$init = function () {',
  14236. ' $mod.TBird.$init.call(this);',
  14237. ' this.B$2 = 0.0;',
  14238. ' };',
  14239. ' this.A$2 = function () {',
  14240. ' var Result = false;',
  14241. ' this.B$2 = 3.3;',
  14242. ' this.A$2();',
  14243. ' this.B$1 = true;',
  14244. ' this.A = 17;',
  14245. ' this.B$1 = this.A$1 > 1;',
  14246. ' return Result;',
  14247. ' };',
  14248. '});',
  14249. 'this.e = null;',
  14250. '']),
  14251. LinesToStr([ // $mod.$main
  14252. '$mod.e.B$2 = 5.3;',
  14253. 'if ($mod.e.A$2()) ;',
  14254. '']));
  14255. end;
  14256. procedure TTestModule.TestClass_ReintroducedVar;
  14257. begin
  14258. StartProgram(false);
  14259. Add('type');
  14260. Add(' TObject = class');
  14261. Add(' strict private');
  14262. Add(' Some: longint;');
  14263. Add(' end;');
  14264. Add(' TMobile = class');
  14265. Add(' strict private');
  14266. Add(' Some: string;');
  14267. Add(' end;');
  14268. Add(' TCar = class(tmobile)');
  14269. Add(' procedure Some;');
  14270. Add(' procedure Some(vA: longint);');
  14271. Add(' end;');
  14272. Add('procedure tcar.some;');
  14273. Add('begin');
  14274. Add(' Some;');
  14275. Add(' Some(1);');
  14276. Add('end;');
  14277. Add('procedure tcar.some(va: longint); begin end;');
  14278. Add('begin');
  14279. ConvertProgram;
  14280. CheckSource('TestClass_ReintroducedVar',
  14281. LinesToStr([ // statements
  14282. 'rtl.createClass(this, "TObject", null, function () {',
  14283. ' this.$init = function () {',
  14284. ' this.Some = 0;',
  14285. ' };',
  14286. ' this.$final = function () {',
  14287. ' };',
  14288. '});',
  14289. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14290. ' this.$init = function () {',
  14291. ' $mod.TObject.$init.call(this);',
  14292. ' this.Some$1 = "";',
  14293. ' };',
  14294. '});',
  14295. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14296. ' this.Some$2 = function () {',
  14297. ' this.Some$2();',
  14298. ' this.Some$3(1);',
  14299. ' };',
  14300. ' this.Some$3 = function (vA) {',
  14301. ' };',
  14302. '});',
  14303. '']),
  14304. LinesToStr([ // $mod.$main
  14305. '']));
  14306. end;
  14307. procedure TTestModule.TestClass_RaiseDescendant;
  14308. begin
  14309. StartProgram(false);
  14310. Add([
  14311. 'type',
  14312. ' TObject = class',
  14313. ' constructor Create(Msg: string);',
  14314. ' end;',
  14315. ' Exception = class',
  14316. ' end;',
  14317. ' EConvertError = class(Exception)',
  14318. ' end;',
  14319. 'constructor TObject.Create(Msg: string); begin end;',
  14320. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14321. 'begin',
  14322. ' raise Exception.Create(''Bar1'');',
  14323. ' raise EConvertError.Create(''Bar2'');',
  14324. ' raise AssertConv(''Bar2'');',
  14325. ' raise AssertConv;',
  14326. '']);
  14327. ConvertProgram;
  14328. CheckSource('TestClass_RaiseDescendant',
  14329. LinesToStr([ // statements
  14330. 'rtl.createClass(this, "TObject", null, function () {',
  14331. ' this.$init = function () {',
  14332. ' };',
  14333. ' this.$final = function () {',
  14334. ' };',
  14335. ' this.Create = function (Msg) {',
  14336. ' return this;',
  14337. ' };',
  14338. '});',
  14339. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14340. '});',
  14341. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14342. '});',
  14343. 'this.AssertConv = function (Msg) {',
  14344. ' var Result = null;',
  14345. ' return Result;',
  14346. '};',
  14347. '']),
  14348. LinesToStr([ // $mod.$main
  14349. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14350. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14351. 'throw $mod.AssertConv("Bar2");',
  14352. 'throw $mod.AssertConv("def");',
  14353. '']));
  14354. end;
  14355. procedure TTestModule.TestClass_ExternalMethod;
  14356. begin
  14357. AddModuleWithIntfImplSrc('unit2.pas',
  14358. LinesToStr([
  14359. 'type',
  14360. ' TObject = class',
  14361. ' public',
  14362. ' procedure Intern; external name ''$DoIntern'';',
  14363. ' end;',
  14364. '']),
  14365. LinesToStr([
  14366. '']));
  14367. StartUnit(true);
  14368. Add('interface');
  14369. Add('uses unit2;');
  14370. Add('type');
  14371. Add(' TCar = class(TObject)');
  14372. Add(' public');
  14373. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14374. Add(' procedure DoIt;');
  14375. Add(' end;');
  14376. Add('implementation');
  14377. Add('procedure tcar.doit;');
  14378. Add('begin');
  14379. Add(' Intern;');
  14380. Add(' Intern();');
  14381. Add(' Intern2;');
  14382. Add(' Intern2();');
  14383. Add('end;');
  14384. Add('var Obj: TCar;');
  14385. Add('begin');
  14386. Add(' obj.intern;');
  14387. Add(' obj.intern();');
  14388. Add(' obj.intern2;');
  14389. Add(' obj.intern2();');
  14390. Add(' obj.doit;');
  14391. Add(' obj.doit();');
  14392. Add(' with obj do begin');
  14393. Add(' Intern;');
  14394. Add(' Intern();');
  14395. Add(' Intern2;');
  14396. Add(' Intern2();');
  14397. Add(' end;');
  14398. ConvertUnit;
  14399. CheckSource('TestClass_ExternalMethod',
  14400. LinesToStr([
  14401. 'var $impl = $mod.$impl;',
  14402. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14403. ' this.DoIt = function () {',
  14404. ' this.$DoIntern();',
  14405. ' this.$DoIntern();',
  14406. ' this.$DoIntern2();',
  14407. ' this.$DoIntern2();',
  14408. ' };',
  14409. ' });',
  14410. '']),
  14411. LinesToStr([ // this.$init
  14412. '$impl.Obj.$DoIntern();',
  14413. '$impl.Obj.$DoIntern();',
  14414. '$impl.Obj.$DoIntern2();',
  14415. '$impl.Obj.$DoIntern2();',
  14416. '$impl.Obj.DoIt();',
  14417. '$impl.Obj.DoIt();',
  14418. 'var $with = $impl.Obj;',
  14419. '$with.$DoIntern();',
  14420. '$with.$DoIntern();',
  14421. '$with.$DoIntern2();',
  14422. '$with.$DoIntern2();',
  14423. '']),
  14424. LinesToStr([ // implementation
  14425. '$impl.Obj = null;',
  14426. '']) );
  14427. end;
  14428. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14429. begin
  14430. StartProgram(false);
  14431. Add('type');
  14432. Add(' TObject = class');
  14433. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14434. Add(' end;');
  14435. Add('begin');
  14436. SetExpectedPasResolverError('Virtual method name must match external',
  14437. nVirtualMethodNameMustMatchExternal);
  14438. ConvertProgram;
  14439. end;
  14440. procedure TTestModule.TestClass_ExternalOverrideFail;
  14441. begin
  14442. StartProgram(false);
  14443. Add('type');
  14444. Add(' TObject = class');
  14445. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14446. Add(' end;');
  14447. Add(' TCar = class');
  14448. Add(' procedure DoIt; override; external name ''DoIt'';');
  14449. Add(' end;');
  14450. Add('begin');
  14451. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14452. nInvalidXModifierY);
  14453. ConvertProgram;
  14454. end;
  14455. procedure TTestModule.TestClass_ExternalVar;
  14456. begin
  14457. AddModuleWithIntfImplSrc('unit2.pas',
  14458. LinesToStr([
  14459. '{$modeswitch externalclass}',
  14460. 'type',
  14461. ' TObject = class',
  14462. ' public',
  14463. ' Intern: longint external name ''$Intern'';',
  14464. ' Bracket: longint external name ''["A B"]'';',
  14465. ' end;',
  14466. '']),
  14467. LinesToStr([
  14468. '']));
  14469. StartUnit(true);
  14470. Add([
  14471. 'interface',
  14472. 'uses unit2;',
  14473. '{$modeswitch externalclass}',
  14474. 'type',
  14475. ' TCar = class(tobject)',
  14476. ' public',
  14477. ' Intern2: longint external name ''$Intern2'';',
  14478. ' procedure DoIt;',
  14479. ' end;',
  14480. 'implementation',
  14481. 'procedure tcar.doit;',
  14482. 'begin',
  14483. ' Intern:=Intern+1;',
  14484. ' Intern2:=Intern2+2;',
  14485. ' Bracket:=Bracket+3;',
  14486. 'end;',
  14487. 'var Obj: TCar;',
  14488. 'begin',
  14489. ' obj.intern:=obj.intern+1;',
  14490. ' obj.intern2:=obj.intern2+2;',
  14491. ' obj.Bracket:=obj.Bracket+3;',
  14492. ' with obj do begin',
  14493. ' intern:=intern+1;',
  14494. ' intern2:=intern2+2;',
  14495. ' Bracket:=Bracket+3;',
  14496. ' end;']);
  14497. ConvertUnit;
  14498. CheckSource('TestClass_ExternalVar',
  14499. LinesToStr([
  14500. 'var $impl = $mod.$impl;',
  14501. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14502. ' this.DoIt = function () {',
  14503. ' this.$Intern = this.$Intern + 1;',
  14504. ' this.$Intern2 = this.$Intern2 + 2;',
  14505. ' this["A B"] = this["A B"] + 3;',
  14506. ' };',
  14507. ' });',
  14508. '']),
  14509. LinesToStr([
  14510. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14511. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14512. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14513. 'var $with = $impl.Obj;',
  14514. '$with.$Intern = $with.$Intern + 1;',
  14515. '$with.$Intern2 = $with.$Intern2 + 2;',
  14516. '$with["A B"] = $with["A B"] + 3;',
  14517. '']),
  14518. LinesToStr([ // implementation
  14519. '$impl.Obj = null;',
  14520. '']));
  14521. end;
  14522. procedure TTestModule.TestClass_Const;
  14523. begin
  14524. StartProgram(false);
  14525. Add([
  14526. 'type',
  14527. ' integer = longint;',
  14528. ' TClass = class of TObject;',
  14529. ' TObject = class',
  14530. ' public',
  14531. ' const cI: integer = 3;',
  14532. ' procedure DoIt;',
  14533. ' class procedure DoMore;',
  14534. ' end;',
  14535. 'procedure tobject.doit;',
  14536. 'begin',
  14537. ' if cI=4 then;',
  14538. ' if 5=cI then;',
  14539. ' if Self.cI=6 then;',
  14540. ' if 7=Self.cI then;',
  14541. ' with Self do begin',
  14542. ' if cI=11 then;',
  14543. ' if 12=cI then;',
  14544. ' end;',
  14545. 'end;',
  14546. 'class procedure tobject.domore;',
  14547. 'begin',
  14548. ' if cI=8 then;',
  14549. ' if Self.cI=9 then;',
  14550. ' if 10=cI then;',
  14551. ' if 11=Self.cI then;',
  14552. ' with Self do begin',
  14553. ' if cI=13 then;',
  14554. ' if 14=cI then;',
  14555. ' end;',
  14556. 'end;',
  14557. 'var',
  14558. ' Obj: TObject;',
  14559. ' Cla: TClass;',
  14560. 'begin',
  14561. ' if TObject.cI=21 then ;',
  14562. ' if Obj.cI=22 then ;',
  14563. ' if Cla.cI=23 then ;',
  14564. ' with obj do if ci=24 then;',
  14565. ' with TObject do if ci=25 then;',
  14566. ' with Cla do if ci=26 then;']);
  14567. ConvertProgram;
  14568. CheckSource('TestClass_Const',
  14569. LinesToStr([
  14570. 'rtl.createClass(this, "TObject", null, function () {',
  14571. ' this.cI = 3;',
  14572. ' this.$init = function () {',
  14573. ' };',
  14574. ' this.$final = function () {',
  14575. ' };',
  14576. ' this.DoIt = function () {',
  14577. ' if (this.cI === 4) ;',
  14578. ' if (5 === this.cI) ;',
  14579. ' if (this.cI === 6) ;',
  14580. ' if (7 === this.cI) ;',
  14581. ' if (this.cI === 11) ;',
  14582. ' if (12 === this.cI) ;',
  14583. ' };',
  14584. ' this.DoMore = function () {',
  14585. ' if (this.cI === 8) ;',
  14586. ' if (this.cI === 9) ;',
  14587. ' if (10 === this.cI) ;',
  14588. ' if (11 === this.cI) ;',
  14589. ' if (this.cI === 13) ;',
  14590. ' if (14 === this.cI) ;',
  14591. ' };',
  14592. '});',
  14593. 'this.Obj = null;',
  14594. 'this.Cla = null;',
  14595. '']),
  14596. LinesToStr([
  14597. 'if ($mod.TObject.cI === 21) ;',
  14598. 'if ($mod.Obj.cI === 22) ;',
  14599. 'if ($mod.Cla.cI === 23) ;',
  14600. 'var $with = $mod.Obj;',
  14601. 'if ($with.cI === 24) ;',
  14602. 'var $with1 = $mod.TObject;',
  14603. 'if ($with1.cI === 25) ;',
  14604. 'var $with2 = $mod.Cla;',
  14605. 'if ($with2.cI === 26) ;',
  14606. '']));
  14607. end;
  14608. procedure TTestModule.TestClass_ConstEnum;
  14609. begin
  14610. StartProgram(false);
  14611. Add([
  14612. 'type',
  14613. ' TEnum = (red,blue);',
  14614. ' TObject = class',
  14615. ' end;',
  14616. ' TAnimal = class',
  14617. ' public',
  14618. ' type TSubEnum = (light,dark);',
  14619. ' const a = high(TEnum);',
  14620. ' const b = high(TSubEnum);',
  14621. ' end;',
  14622. ' TBird = class(TAnimal)',
  14623. ' public',
  14624. ' const c = high(TEnum);',
  14625. ' const d = high(TSubEnum);',
  14626. ' end;',
  14627. ' TAnt = class',
  14628. ' public',
  14629. ' const e = high(TEnum);',
  14630. ' const f = high(TBird.TSubEnum);',
  14631. ' end;',
  14632. 'begin',
  14633. '']);
  14634. ConvertProgram;
  14635. CheckSource('TestClass_ConstEnum',
  14636. LinesToStr([
  14637. 'this.TEnum = {',
  14638. ' "0": "red",',
  14639. ' red: 0,',
  14640. ' "1": "blue",',
  14641. ' blue: 1',
  14642. '};',
  14643. 'rtl.createClass(this, "TObject", null, function () {',
  14644. ' this.$init = function () {',
  14645. ' };',
  14646. ' this.$final = function () {',
  14647. ' };',
  14648. '});',
  14649. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14650. ' this.TSubEnum = {',
  14651. ' "0": "light",',
  14652. ' light: 0,',
  14653. ' "1": "dark",',
  14654. ' dark: 1',
  14655. ' };',
  14656. ' this.a = $mod.TEnum.blue;',
  14657. ' this.b = this.TSubEnum.dark;',
  14658. '});',
  14659. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14660. ' this.c = $mod.TEnum.blue;',
  14661. ' this.d = this.TSubEnum.dark;',
  14662. '});',
  14663. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14664. ' this.e = $mod.TEnum.blue;',
  14665. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14666. '});',
  14667. '']),
  14668. LinesToStr([
  14669. '']));
  14670. end;
  14671. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14672. begin
  14673. StartProgram(false);
  14674. Add([
  14675. 'type',
  14676. ' TObject = class',
  14677. ' const cI: longint = 3;',
  14678. ' procedure Fly;',
  14679. ' procedure Run;',
  14680. ' end;',
  14681. ' TBird = class',
  14682. ' procedure Go;',
  14683. ' end;',
  14684. 'procedure tobject.fly;',
  14685. 'const cI: word = 4;',
  14686. 'begin',
  14687. ' if cI=Self.cI then ;',
  14688. 'end;',
  14689. 'procedure tobject.run;',
  14690. 'const cI: word = 5;',
  14691. 'begin',
  14692. ' if cI=Self.cI then ;',
  14693. 'end;',
  14694. 'procedure tbird.go;',
  14695. 'const cI: word = 6;',
  14696. 'begin',
  14697. ' if cI=Self.cI then ;',
  14698. 'end;',
  14699. 'begin',
  14700. '']);
  14701. ConvertProgram;
  14702. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14703. LinesToStr([
  14704. 'rtl.createClass(this, "TObject", null, function () {',
  14705. ' this.cI = 3;',
  14706. ' this.$init = function () {',
  14707. ' };',
  14708. ' this.$final = function () {',
  14709. ' };',
  14710. ' var cI$1 = 4;',
  14711. ' this.Fly = function () {',
  14712. ' if (cI$1 === this.cI) ;',
  14713. ' };',
  14714. ' var cI$2 = 5;',
  14715. ' this.Run = function () {',
  14716. ' if (cI$2 === this.cI) ;',
  14717. ' };',
  14718. '});',
  14719. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14720. ' var cI$3 = 6;',
  14721. ' this.Go = function () {',
  14722. ' if (cI$3 === this.cI) ;',
  14723. ' };',
  14724. '});',
  14725. '']),
  14726. LinesToStr([
  14727. '']));
  14728. end;
  14729. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14730. begin
  14731. StartUnit(false);
  14732. Add([
  14733. 'interface',
  14734. 'type',
  14735. ' TObject = class',
  14736. ' const cI: longint = 3;',
  14737. ' procedure Fly;',
  14738. ' procedure Run;',
  14739. ' end;',
  14740. ' TBird = class',
  14741. ' procedure Go;',
  14742. ' end;',
  14743. 'implementation',
  14744. 'procedure tobject.fly;',
  14745. 'const cI: word = 4;',
  14746. 'begin',
  14747. ' if cI=Self.cI then ;',
  14748. 'end;',
  14749. 'procedure tobject.run;',
  14750. 'const cI: word = 5;',
  14751. 'begin',
  14752. ' if cI=Self.cI then ;',
  14753. 'end;',
  14754. 'procedure tbird.go;',
  14755. 'const cI: word = 6;',
  14756. 'begin',
  14757. ' if cI=Self.cI then ;',
  14758. 'end;',
  14759. '']);
  14760. ConvertUnit;
  14761. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14762. LinesToStr([
  14763. 'rtl.createClass(this, "TObject", null, function () {',
  14764. ' this.cI = 3;',
  14765. ' this.$init = function () {',
  14766. ' };',
  14767. ' this.$final = function () {',
  14768. ' };',
  14769. ' var cI$1 = 4;',
  14770. ' this.Fly = function () {',
  14771. ' if (cI$1 === this.cI) ;',
  14772. ' };',
  14773. ' var cI$2 = 5;',
  14774. ' this.Run = function () {',
  14775. ' if (cI$2 === this.cI) ;',
  14776. ' };',
  14777. '});',
  14778. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14779. ' var cI$3 = 6;',
  14780. ' this.Go = function () {',
  14781. ' if (cI$3 === this.cI) ;',
  14782. ' };',
  14783. '});',
  14784. '']),
  14785. '',
  14786. '');
  14787. end;
  14788. procedure TTestModule.TestClass_LocalVarSelfFail;
  14789. begin
  14790. StartProgram(false);
  14791. Add([
  14792. 'type',
  14793. ' TObject = class',
  14794. ' constructor Create;',
  14795. ' end;',
  14796. 'constructor tobject.create;',
  14797. 'var self: longint;',
  14798. 'begin',
  14799. 'end',
  14800. 'begin',
  14801. '']);
  14802. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14803. ConvertProgram;
  14804. end;
  14805. procedure TTestModule.TestClass_ArgSelfFail;
  14806. begin
  14807. StartProgram(false);
  14808. Add([
  14809. 'type',
  14810. ' TObject = class',
  14811. ' procedure DoIt(Self: longint);',
  14812. ' end;',
  14813. 'procedure tobject.doit(self: longint);',
  14814. 'begin',
  14815. 'end',
  14816. 'begin',
  14817. '']);
  14818. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14819. ConvertProgram;
  14820. end;
  14821. procedure TTestModule.TestClass_NestedProcSelf;
  14822. begin
  14823. StartProgram(false);
  14824. Add([
  14825. 'type',
  14826. ' TObject = class',
  14827. ' Key: longint;',
  14828. ' class var State: longint;',
  14829. ' procedure DoIt;',
  14830. ' function GetSize: longint; virtual; abstract;',
  14831. ' procedure SetSize(Value: longint); virtual; abstract;',
  14832. ' property Size: longint read GetSize write SetSize;',
  14833. ' end;',
  14834. 'procedure tobject.doit;',
  14835. ' procedure Sub;',
  14836. ' begin',
  14837. ' key:=key+2;',
  14838. ' self.key:=self.key+3;',
  14839. ' state:=state+4;',
  14840. ' self.state:=self.state+5;',
  14841. ' tobject.state:=tobject.state+6;',
  14842. ' size:=size+7;',
  14843. ' self.size:=self.size+8;',
  14844. ' end;',
  14845. 'begin',
  14846. ' sub;',
  14847. ' key:=key+12;',
  14848. ' self.key:=self.key+13;',
  14849. ' state:=state+14;',
  14850. ' self.state:=self.state+15;',
  14851. ' tobject.state:=tobject.state+16;',
  14852. ' size:=size+17;',
  14853. ' self.size:=self.size+18;',
  14854. 'end;',
  14855. 'begin',
  14856. '']);
  14857. ConvertProgram;
  14858. CheckSource('TestClass_NestedProcSelf',
  14859. LinesToStr([ // statements
  14860. 'rtl.createClass(this, "TObject", null, function () {',
  14861. ' this.State = 0;',
  14862. ' this.$init = function () {',
  14863. ' this.Key = 0;',
  14864. ' };',
  14865. ' this.$final = function () {',
  14866. ' };',
  14867. ' this.DoIt = function () {',
  14868. ' var $Self = this;',
  14869. ' function Sub() {',
  14870. ' $Self.Key = $Self.Key + 2;',
  14871. ' $Self.Key = $Self.Key + 3;',
  14872. ' $mod.TObject.State = $Self.State + 4;',
  14873. ' $mod.TObject.State = $Self.State + 5;',
  14874. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14875. ' $Self.SetSize($Self.GetSize() + 7);',
  14876. ' $Self.SetSize($Self.GetSize() + 8);',
  14877. ' };',
  14878. ' Sub();',
  14879. ' this.Key = this.Key + 12;',
  14880. ' $Self.Key = $Self.Key + 13;',
  14881. ' $mod.TObject.State = this.State + 14;',
  14882. ' $mod.TObject.State = $Self.State + 15;',
  14883. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14884. ' this.SetSize(this.GetSize() + 17);',
  14885. ' $Self.SetSize($Self.GetSize() + 18);',
  14886. ' };',
  14887. '});',
  14888. '']),
  14889. LinesToStr([ // $mod.$main
  14890. '']));
  14891. end;
  14892. procedure TTestModule.TestClass_NestedProcSelf2;
  14893. begin
  14894. StartProgram(false);
  14895. Add([
  14896. 'type',
  14897. ' TObject = class',
  14898. ' Key: longint;',
  14899. ' class var State: longint;',
  14900. ' function GetSize: longint; virtual; abstract;',
  14901. ' procedure SetSize(Value: longint); virtual; abstract;',
  14902. ' property Size: longint read GetSize write SetSize;',
  14903. ' end;',
  14904. ' TBird = class',
  14905. ' procedure DoIt;',
  14906. ' end;',
  14907. 'procedure tbird.doit;',
  14908. ' procedure Sub;',
  14909. ' begin',
  14910. ' key:=key+2;',
  14911. ' self.key:=self.key+3;',
  14912. ' state:=state+4;',
  14913. ' self.state:=self.state+5;',
  14914. ' tobject.state:=tobject.state+6;',
  14915. ' size:=size+7;',
  14916. ' self.size:=self.size+8;',
  14917. ' end;',
  14918. 'begin',
  14919. ' sub;',
  14920. ' key:=key+12;',
  14921. ' self.key:=self.key+13;',
  14922. ' state:=state+14;',
  14923. ' self.state:=self.state+15;',
  14924. ' tobject.state:=tobject.state+16;',
  14925. ' size:=size+17;',
  14926. ' self.size:=self.size+18;',
  14927. 'end;',
  14928. 'begin',
  14929. '']);
  14930. ConvertProgram;
  14931. CheckSource('TestClass_NestedProcSelf2',
  14932. LinesToStr([ // statements
  14933. 'rtl.createClass(this, "TObject", null, function () {',
  14934. ' this.State = 0;',
  14935. ' this.$init = function () {',
  14936. ' this.Key = 0;',
  14937. ' };',
  14938. ' this.$final = function () {',
  14939. ' };',
  14940. '});',
  14941. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14942. ' this.DoIt = function () {',
  14943. ' var $Self = this;',
  14944. ' function Sub() {',
  14945. ' $Self.Key = $Self.Key + 2;',
  14946. ' $Self.Key = $Self.Key + 3;',
  14947. ' $mod.TObject.State = $Self.State + 4;',
  14948. ' $mod.TObject.State = $Self.State + 5;',
  14949. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14950. ' $Self.SetSize($Self.GetSize() + 7);',
  14951. ' $Self.SetSize($Self.GetSize() + 8);',
  14952. ' };',
  14953. ' Sub();',
  14954. ' this.Key = this.Key + 12;',
  14955. ' $Self.Key = $Self.Key + 13;',
  14956. ' $mod.TObject.State = this.State + 14;',
  14957. ' $mod.TObject.State = $Self.State + 15;',
  14958. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14959. ' this.SetSize(this.GetSize() + 17);',
  14960. ' $Self.SetSize($Self.GetSize() + 18);',
  14961. ' };',
  14962. '});',
  14963. '']),
  14964. LinesToStr([ // $mod.$main
  14965. '']));
  14966. end;
  14967. procedure TTestModule.TestClass_NestedProcClassSelf;
  14968. begin
  14969. StartProgram(false);
  14970. Add([
  14971. 'type',
  14972. ' TObject = class',
  14973. ' class var State: longint;',
  14974. ' class procedure DoIt;',
  14975. ' class function GetSize: longint; virtual; abstract;',
  14976. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14977. ' class property Size: longint read GetSize write SetSize;',
  14978. ' end;',
  14979. 'class procedure tobject.doit;',
  14980. ' procedure Sub;',
  14981. ' begin',
  14982. ' state:=state+2;',
  14983. ' self.state:=self.state+3;',
  14984. ' tobject.state:=tobject.state+4;',
  14985. ' size:=size+5;',
  14986. ' self.size:=self.size+6;',
  14987. ' tobject.size:=tobject.size+7;',
  14988. ' end;',
  14989. 'begin',
  14990. ' sub;',
  14991. ' state:=state+12;',
  14992. ' self.state:=self.state+13;',
  14993. ' tobject.state:=tobject.state+14;',
  14994. ' size:=size+15;',
  14995. ' self.size:=self.size+16;',
  14996. ' tobject.size:=tobject.size+17;',
  14997. 'end;',
  14998. 'begin',
  14999. '']);
  15000. ConvertProgram;
  15001. CheckSource('TestClass_NestedProcClassSelf',
  15002. LinesToStr([ // statements
  15003. 'rtl.createClass(this, "TObject", null, function () {',
  15004. ' this.State = 0;',
  15005. ' this.$init = function () {',
  15006. ' };',
  15007. ' this.$final = function () {',
  15008. ' };',
  15009. ' this.DoIt = function () {',
  15010. ' var $Self = this;',
  15011. ' function Sub() {',
  15012. ' $mod.TObject.State = $Self.State + 2;',
  15013. ' $mod.TObject.State = $Self.State + 3;',
  15014. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15015. ' $Self.SetSize($Self.GetSize() + 5);',
  15016. ' $Self.SetSize($Self.GetSize() + 6);',
  15017. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15018. ' };',
  15019. ' Sub();',
  15020. ' $mod.TObject.State = this.State + 12;',
  15021. ' $mod.TObject.State = $Self.State + 13;',
  15022. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15023. ' this.SetSize(this.GetSize() + 15);',
  15024. ' $Self.SetSize($Self.GetSize() + 16);',
  15025. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15026. ' };',
  15027. '});',
  15028. '']),
  15029. LinesToStr([ // $mod.$main
  15030. '']));
  15031. end;
  15032. procedure TTestModule.TestClass_NestedProcCallInherited;
  15033. begin
  15034. StartProgram(false);
  15035. Add([
  15036. 'type',
  15037. ' TObject = class',
  15038. ' function DoIt(k: boolean): longint; virtual;',
  15039. ' end;',
  15040. ' TBird = class',
  15041. ' function DoIt(k: boolean): longint; override;',
  15042. ' end;',
  15043. 'function tobject.doit(k: boolean): longint;',
  15044. 'begin',
  15045. 'end;',
  15046. 'function tbird.doit(k: boolean): longint;',
  15047. ' procedure Sub;',
  15048. ' begin',
  15049. ' inherited DoIt(true);',
  15050. //' if inherited DoIt(false)=4 then ;',
  15051. ' end;',
  15052. 'begin',
  15053. ' Sub;',
  15054. ' inherited;',
  15055. ' inherited DoIt(true);',
  15056. //' if inherited DoIt(false)=14 then ;',
  15057. 'end;',
  15058. 'begin',
  15059. '']);
  15060. ConvertProgram;
  15061. CheckSource('TestClass_NestedProcCallInherited',
  15062. LinesToStr([ // statements
  15063. 'rtl.createClass(this, "TObject", null, function () {',
  15064. ' this.$init = function () {',
  15065. ' };',
  15066. ' this.$final = function () {',
  15067. ' };',
  15068. ' this.DoIt = function (k) {',
  15069. ' var Result = 0;',
  15070. ' return Result;',
  15071. ' };',
  15072. '});',
  15073. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15074. ' this.DoIt = function (k) {',
  15075. ' var $Self = this;',
  15076. ' var Result = 0;',
  15077. ' function Sub() {',
  15078. ' $mod.TObject.DoIt.call($Self, true);',
  15079. ' };',
  15080. ' Sub();',
  15081. ' $mod.TObject.DoIt.apply(this, arguments);',
  15082. ' $mod.TObject.DoIt.call(this, true);',
  15083. ' return Result;',
  15084. ' };',
  15085. '});',
  15086. '']),
  15087. LinesToStr([ // $mod.$main
  15088. '']));
  15089. end;
  15090. procedure TTestModule.TestClass_TObjectFree;
  15091. begin
  15092. StartProgram(false);
  15093. Add([
  15094. 'type',
  15095. ' TObject = class',
  15096. ' Obj: tobject;',
  15097. ' procedure Free;',
  15098. ' procedure Release;',
  15099. ' end;',
  15100. 'procedure tobject.free;',
  15101. 'begin',
  15102. 'end;',
  15103. 'procedure tobject.release;',
  15104. 'begin',
  15105. ' free;',
  15106. ' if true then free;',
  15107. 'end;',
  15108. 'function DoIt(o: tobject): tobject;',
  15109. 'var l: tobject;',
  15110. 'begin',
  15111. ' o.free;',
  15112. ' o.free();',
  15113. ' l.free;',
  15114. ' l.free();',
  15115. ' o.obj.free;',
  15116. ' o.obj.free();',
  15117. ' with o do obj.free;',
  15118. ' with o do obj.free();',
  15119. ' result.Free;',
  15120. ' result.Free();',
  15121. 'end;',
  15122. 'var o: tobject;',
  15123. ' a: array of tobject;',
  15124. 'begin',
  15125. ' o.free;',
  15126. ' o.obj.free;',
  15127. ' a[1+2].free;',
  15128. '']);
  15129. ConvertProgram;
  15130. CheckSource('TestClass_TObjectFree',
  15131. LinesToStr([ // statements
  15132. 'rtl.createClass(this, "TObject", null, function () {',
  15133. ' this.$init = function () {',
  15134. ' this.Obj = null;',
  15135. ' };',
  15136. ' this.$final = function () {',
  15137. ' this.Obj = undefined;',
  15138. ' };',
  15139. ' this.Free = function () {',
  15140. ' };',
  15141. ' this.Release = function () {',
  15142. ' this.Free();',
  15143. ' if (true) this.Free();',
  15144. ' };',
  15145. '});',
  15146. 'this.DoIt = function (o) {',
  15147. ' var Result = null;',
  15148. ' var l = null;',
  15149. ' o = rtl.freeLoc(o);',
  15150. ' o = rtl.freeLoc(o);',
  15151. ' l = rtl.freeLoc(l);',
  15152. ' l = rtl.freeLoc(l);',
  15153. ' rtl.free(o, "Obj");',
  15154. ' rtl.free(o, "Obj");',
  15155. ' rtl.free(o, "Obj");',
  15156. ' rtl.free(o, "Obj");',
  15157. ' Result = rtl.freeLoc(Result);',
  15158. ' Result = rtl.freeLoc(Result);',
  15159. ' return Result;',
  15160. '};',
  15161. 'this.o = null;',
  15162. 'this.a = [];',
  15163. '']),
  15164. LinesToStr([ // $mod.$main
  15165. 'rtl.free($mod, "o");',
  15166. 'rtl.free($mod.o, "Obj");',
  15167. 'rtl.free($mod.a, 1 + 2);',
  15168. '']));
  15169. end;
  15170. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15171. begin
  15172. StartProgram(false);
  15173. Add([
  15174. 'type',
  15175. ' TObject = class',
  15176. ' Obj: tobject;',
  15177. ' procedure Free;',
  15178. ' end;',
  15179. 'procedure tobject.free;',
  15180. 'begin',
  15181. 'end;',
  15182. 'procedure DoIt(var o: tobject);',
  15183. 'begin',
  15184. ' o.free;',
  15185. ' o.free();',
  15186. 'end;',
  15187. 'begin',
  15188. '']);
  15189. ConvertProgram;
  15190. CheckSource('TestClass_TObjectFree_VarArg',
  15191. LinesToStr([ // statements
  15192. 'rtl.createClass(this, "TObject", null, function () {',
  15193. ' this.$init = function () {',
  15194. ' this.Obj = null;',
  15195. ' };',
  15196. ' this.$final = function () {',
  15197. ' this.Obj = undefined;',
  15198. ' };',
  15199. ' this.Free = function () {',
  15200. ' };',
  15201. '});',
  15202. 'this.DoIt = function (o) {',
  15203. ' o.set(rtl.freeLoc(o.get()));',
  15204. ' o.set(rtl.freeLoc(o.get()));',
  15205. '};',
  15206. '']),
  15207. LinesToStr([ // $mod.$main
  15208. '']));
  15209. end;
  15210. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15211. begin
  15212. StartProgram(false);
  15213. Add([
  15214. 'type',
  15215. ' TObject = class',
  15216. ' constructor Create;',
  15217. ' procedure Free;',
  15218. ' end;',
  15219. 'constructor TObject.Create; begin end;',
  15220. 'procedure tobject.free; begin end;',
  15221. 'begin',
  15222. ' with tobject.create do free;',
  15223. '']);
  15224. ConvertProgram;
  15225. CheckSource('TestClass_TObjectFreeNewInstance',
  15226. LinesToStr([ // statements
  15227. 'rtl.createClass(this, "TObject", null, function () {',
  15228. ' this.$init = function () {',
  15229. ' };',
  15230. ' this.$final = function () {',
  15231. ' };',
  15232. ' this.Create = function () {',
  15233. ' return this;',
  15234. ' };',
  15235. ' this.Free = function () {',
  15236. ' };',
  15237. '});',
  15238. '']),
  15239. LinesToStr([ // $mod.$main
  15240. 'var $with = $mod.TObject.$create("Create");',
  15241. '$with=rtl.freeLoc($with);',
  15242. '']));
  15243. end;
  15244. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15245. begin
  15246. StartProgram(false);
  15247. Add([
  15248. 'type',
  15249. ' TObject = class',
  15250. ' destructor Destroy;',
  15251. ' procedure Free;',
  15252. ' end;',
  15253. 'destructor TObject.Destroy; begin end;',
  15254. 'procedure tobject.free; begin end;',
  15255. 'var o: tobject;',
  15256. 'begin',
  15257. ' o.free;',
  15258. '']);
  15259. Converter.UseLowerCase:=true;
  15260. ConvertProgram;
  15261. CheckSource('TestClass_TObjectFreeLowerCase',
  15262. LinesToStr([ // statements
  15263. 'rtl.createClass(this, "tobject", null, function () {',
  15264. ' this.$init = function () {',
  15265. ' };',
  15266. ' this.$final = function () {',
  15267. ' };',
  15268. ' rtl.tObjectDestroy = "destroy";',
  15269. ' this.destroy = function () {',
  15270. ' };',
  15271. ' this.free = function () {',
  15272. ' };',
  15273. '});',
  15274. 'this.o = null;',
  15275. '']),
  15276. LinesToStr([ // $mod.$main
  15277. 'rtl.free($mod, "o");',
  15278. '']));
  15279. end;
  15280. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15281. begin
  15282. StartProgram(false);
  15283. Add([
  15284. 'type',
  15285. ' TObject = class',
  15286. ' procedure Free;',
  15287. ' function GetObj: tobject; virtual; abstract;',
  15288. ' end;',
  15289. 'procedure tobject.free;',
  15290. 'begin',
  15291. 'end;',
  15292. 'var o: tobject;',
  15293. 'begin',
  15294. ' o.getobj.free;',
  15295. '']);
  15296. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15297. ConvertProgram;
  15298. end;
  15299. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15300. begin
  15301. StartProgram(false);
  15302. Add([
  15303. 'type',
  15304. ' TObject = class',
  15305. ' procedure Free;',
  15306. ' FObj: TObject;',
  15307. ' property Obj: tobject read FObj write FObj;',
  15308. ' end;',
  15309. 'procedure tobject.free;',
  15310. 'begin',
  15311. 'end;',
  15312. 'var o: tobject;',
  15313. 'begin',
  15314. ' o.obj.free;',
  15315. '']);
  15316. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15317. ConvertProgram;
  15318. end;
  15319. procedure TTestModule.TestClass_ForIn;
  15320. begin
  15321. StartProgram(false);
  15322. Add([
  15323. 'type',
  15324. ' TObject = class end;',
  15325. ' TItem = TObject;',
  15326. ' TEnumerator = class',
  15327. ' FCurrent: TItem;',
  15328. ' property Current: TItem read FCurrent;',
  15329. ' function MoveNext: boolean;',
  15330. ' end;',
  15331. ' TBird = class',
  15332. ' function GetEnumerator: TEnumerator;',
  15333. ' end;',
  15334. 'function TEnumerator.MoveNext: boolean;',
  15335. 'begin',
  15336. 'end;',
  15337. 'function TBird.GetEnumerator: TEnumerator;',
  15338. 'begin',
  15339. 'end;',
  15340. 'var',
  15341. ' b: TBird;',
  15342. ' i, i2: TItem;',
  15343. 'begin',
  15344. ' for i in b do i2:=i;']);
  15345. ConvertProgram;
  15346. CheckSource('TestClass_ForIn',
  15347. LinesToStr([ // statements
  15348. 'rtl.createClass(this, "TObject", null, function () {',
  15349. ' this.$init = function () {',
  15350. ' };',
  15351. ' this.$final = function () {',
  15352. ' };',
  15353. '});',
  15354. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15355. ' this.$init = function () {',
  15356. ' $mod.TObject.$init.call(this);',
  15357. ' this.FCurrent = null;',
  15358. ' };',
  15359. ' this.$final = function () {',
  15360. ' this.FCurrent = undefined;',
  15361. ' $mod.TObject.$final.call(this);',
  15362. ' };',
  15363. ' this.MoveNext = function () {',
  15364. ' var Result = false;',
  15365. ' return Result;',
  15366. ' };',
  15367. '});',
  15368. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15369. ' this.GetEnumerator = function () {',
  15370. ' var Result = null;',
  15371. ' return Result;',
  15372. ' };',
  15373. '});',
  15374. 'this.b = null;',
  15375. 'this.i = null;',
  15376. 'this.i2 = null;'
  15377. ]),
  15378. LinesToStr([ // $mod.$main
  15379. 'var $in = $mod.b.GetEnumerator();',
  15380. 'try {',
  15381. ' while ($in.MoveNext()){',
  15382. ' $mod.i = $in.FCurrent;',
  15383. ' $mod.i2 = $mod.i;',
  15384. ' }',
  15385. '} finally {',
  15386. ' $in = rtl.freeLoc($in)',
  15387. '};',
  15388. '']));
  15389. end;
  15390. procedure TTestModule.TestClass_DispatchMessage;
  15391. begin
  15392. StartProgram(false);
  15393. Add([
  15394. 'type',
  15395. ' TObject = class',
  15396. ' {$DispatchField DispInt}',
  15397. ' procedure Dispatch(var Msg); virtual; abstract;',
  15398. ' {$DispatchStrField DispStr}',
  15399. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15400. ' end;',
  15401. ' THopMsg = record',
  15402. ' DispInt: longint;',
  15403. ' end;',
  15404. ' TPutMsg = record',
  15405. ' DispStr: string;',
  15406. ' end;',
  15407. ' TBird = class',
  15408. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15409. ' procedure Run; overload; virtual; abstract;',
  15410. ' procedure Run(var Msg); overload; message ''Fast'';',
  15411. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15412. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15413. ' end;',
  15414. 'procedure TBird.Run(var Msg);',
  15415. 'begin',
  15416. 'end;',
  15417. 'begin',
  15418. '']);
  15419. ConvertProgram;
  15420. CheckSource('TestClass_Message',
  15421. LinesToStr([ // statements
  15422. 'rtl.createClass(this, "TObject", null, function () {',
  15423. ' this.$init = function () {',
  15424. ' };',
  15425. ' this.$final = function () {',
  15426. ' };',
  15427. '});',
  15428. 'rtl.recNewT(this, "THopMsg", function () {',
  15429. ' this.DispInt = 0;',
  15430. ' this.$eq = function (b) {',
  15431. ' return this.DispInt === b.DispInt;',
  15432. ' };',
  15433. ' this.$assign = function (s) {',
  15434. ' this.DispInt = s.DispInt;',
  15435. ' return this;',
  15436. ' };',
  15437. '});',
  15438. 'rtl.recNewT(this, "TPutMsg", function () {',
  15439. ' this.DispStr = "";',
  15440. ' this.$eq = function (b) {',
  15441. ' return this.DispStr === b.DispStr;',
  15442. ' };',
  15443. ' this.$assign = function (s) {',
  15444. ' this.DispStr = s.DispStr;',
  15445. ' return this;',
  15446. ' };',
  15447. '});',
  15448. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15449. ' this.Run$1 = function (Msg) {',
  15450. ' };',
  15451. ' this.$msgint = {',
  15452. ' "2": "Fly",',
  15453. ' "3": "Hop"',
  15454. ' };',
  15455. ' this.$msgstr = {',
  15456. ' Fast: "Run$1",',
  15457. ' foo: "Put"',
  15458. ' };',
  15459. '});',
  15460. '']),
  15461. LinesToStr([ // $mod.$main
  15462. '']));
  15463. end;
  15464. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15465. begin
  15466. StartProgram(false);
  15467. Add([
  15468. 'type',
  15469. ' TObject = class',
  15470. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15471. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15472. ' end;',
  15473. 'begin',
  15474. '']);
  15475. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15476. ConvertProgram;
  15477. end;
  15478. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15479. begin
  15480. StartProgram(false);
  15481. Add([
  15482. 'type',
  15483. ' TObject = class',
  15484. ' {$dispatchfield Msg}',
  15485. ' procedure Dispatch(var Msg); virtual; abstract;',
  15486. ' end;',
  15487. ' TFlyMsg = record',
  15488. ' FlyId: longint;',
  15489. ' end;',
  15490. ' TBird = class',
  15491. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15492. ' end;',
  15493. 'begin',
  15494. '']);
  15495. ConvertProgram;
  15496. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15497. end;
  15498. procedure TTestModule.TestClassOf_Create;
  15499. begin
  15500. StartProgram(false);
  15501. Add('type');
  15502. Add(' TObject = class');
  15503. Add(' constructor Create;');
  15504. Add(' end;');
  15505. Add(' TClass = class of TObject;');
  15506. Add('constructor tobject.create; begin end;');
  15507. Add('var');
  15508. Add(' Obj: tobject;');
  15509. Add(' C: tclass;');
  15510. Add('begin');
  15511. Add(' obj:=C.create;');
  15512. Add(' with c do obj:=create;');
  15513. ConvertProgram;
  15514. CheckSource('TestClassOf_Create',
  15515. LinesToStr([ // statements
  15516. 'rtl.createClass(this, "TObject", null, function () {',
  15517. ' this.$init = function () {',
  15518. ' };',
  15519. ' this.$final = function () {',
  15520. ' };',
  15521. ' this.Create = function () {',
  15522. ' return this;',
  15523. ' };',
  15524. '});',
  15525. 'this.Obj = null;',
  15526. 'this.C = null;'
  15527. ]),
  15528. LinesToStr([ // $mod.$main
  15529. '$mod.Obj = $mod.C.$create("Create");',
  15530. 'var $with = $mod.C;',
  15531. '$mod.Obj = $with.$create("Create");',
  15532. '']));
  15533. end;
  15534. procedure TTestModule.TestClassOf_Call;
  15535. begin
  15536. StartProgram(false);
  15537. Add('type');
  15538. Add(' TObject = class');
  15539. Add(' class procedure DoIt;');
  15540. Add(' end;');
  15541. Add(' TClass = class of TObject;');
  15542. Add('class procedure tobject.doit; begin end;');
  15543. Add('var');
  15544. Add(' C: tclass;');
  15545. Add('begin');
  15546. Add(' c.doit;');
  15547. Add(' with c do doit;');
  15548. ConvertProgram;
  15549. CheckSource('TestClassOf_Call',
  15550. LinesToStr([ // statements
  15551. 'rtl.createClass(this, "TObject", null, function () {',
  15552. ' this.$init = function () {',
  15553. ' };',
  15554. ' this.$final = function () {',
  15555. ' };',
  15556. ' this.DoIt = function () {',
  15557. ' };',
  15558. '});',
  15559. 'this.C = null;'
  15560. ]),
  15561. LinesToStr([ // $mod.$main
  15562. '$mod.C.DoIt();',
  15563. 'var $with = $mod.C;',
  15564. '$with.DoIt();',
  15565. '']));
  15566. end;
  15567. procedure TTestModule.TestClassOf_Assign;
  15568. begin
  15569. StartProgram(false);
  15570. Add('type');
  15571. Add(' TClass = class of TObject;');
  15572. Add(' TObject = class');
  15573. Add(' ClassType: TClass; ');
  15574. Add(' end;');
  15575. Add('var');
  15576. Add(' Obj: tobject;');
  15577. Add(' C: tclass;');
  15578. Add('begin');
  15579. Add(' c:=nil;');
  15580. Add(' c:=obj.classtype;');
  15581. ConvertProgram;
  15582. CheckSource('TestClassOf_Assign',
  15583. LinesToStr([ // statements
  15584. 'rtl.createClass(this, "TObject", null, function () {',
  15585. ' this.$init = function () {',
  15586. ' this.ClassType = null;',
  15587. ' };',
  15588. ' this.$final = function () {',
  15589. ' this.ClassType = undefined;',
  15590. ' };',
  15591. '});',
  15592. 'this.Obj = null;',
  15593. 'this.C = null;'
  15594. ]),
  15595. LinesToStr([ // $mod.$main
  15596. '$mod.C = null;',
  15597. '$mod.C = $mod.Obj.ClassType;',
  15598. '']));
  15599. end;
  15600. procedure TTestModule.TestClassOf_Is;
  15601. begin
  15602. StartProgram(false);
  15603. Add('type');
  15604. Add(' TClass = class of TObject;');
  15605. Add(' TObject = class');
  15606. Add(' end;');
  15607. Add(' TCar = class');
  15608. Add(' end;');
  15609. Add(' TCars = class of TCar;');
  15610. Add('var');
  15611. Add(' Obj: tobject;');
  15612. Add(' C: tclass;');
  15613. Add(' Cars: tcars;');
  15614. Add('begin');
  15615. Add(' if c is tcar then ;');
  15616. Add(' if c is tcars then ;');
  15617. ConvertProgram;
  15618. CheckSource('TestClassOf_Is',
  15619. LinesToStr([ // statements
  15620. 'rtl.createClass(this, "TObject", null, function () {',
  15621. ' this.$init = function () {',
  15622. ' };',
  15623. ' this.$final = function () {',
  15624. ' };',
  15625. '});',
  15626. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15627. '});',
  15628. 'this.Obj = null;',
  15629. 'this.C = null;',
  15630. 'this.Cars = null;'
  15631. ]),
  15632. LinesToStr([ // $mod.$main
  15633. 'if(rtl.is($mod.C,$mod.TCar));',
  15634. 'if(rtl.is($mod.C,$mod.TCar));',
  15635. '']));
  15636. end;
  15637. procedure TTestModule.TestClassOf_Compare;
  15638. begin
  15639. StartProgram(false);
  15640. Add('type');
  15641. Add(' TClass = class of TObject;');
  15642. Add(' TObject = class');
  15643. Add(' ClassType: TClass; ');
  15644. Add(' end;');
  15645. Add('var');
  15646. Add(' b: boolean;');
  15647. Add(' Obj: tobject;');
  15648. Add(' C: tclass;');
  15649. Add('begin');
  15650. Add(' b:=c=nil;');
  15651. Add(' b:=nil=c;');
  15652. Add(' b:=c=obj.classtype;');
  15653. Add(' b:=obj.classtype=c;');
  15654. Add(' b:=c=TObject;');
  15655. Add(' b:=TObject=c;');
  15656. Add(' b:=c<>nil;');
  15657. Add(' b:=nil<>c;');
  15658. Add(' b:=c<>obj.classtype;');
  15659. Add(' b:=obj.classtype<>c;');
  15660. Add(' b:=c<>TObject;');
  15661. Add(' b:=TObject<>c;');
  15662. ConvertProgram;
  15663. CheckSource('TestClassOf_Compare',
  15664. LinesToStr([ // statements
  15665. 'rtl.createClass(this, "TObject", null, function () {',
  15666. ' this.$init = function () {',
  15667. ' this.ClassType = null;',
  15668. ' };',
  15669. ' this.$final = function () {',
  15670. ' this.ClassType = undefined;',
  15671. ' };',
  15672. '});',
  15673. 'this.b = false;',
  15674. 'this.Obj = null;',
  15675. 'this.C = null;'
  15676. ]),
  15677. LinesToStr([ // $mod.$main
  15678. '$mod.b = $mod.C === null;',
  15679. '$mod.b = null === $mod.C;',
  15680. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15681. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15682. '$mod.b = $mod.C === $mod.TObject;',
  15683. '$mod.b = $mod.TObject === $mod.C;',
  15684. '$mod.b = $mod.C !== null;',
  15685. '$mod.b = null !== $mod.C;',
  15686. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15687. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15688. '$mod.b = $mod.C !== $mod.TObject;',
  15689. '$mod.b = $mod.TObject !== $mod.C;',
  15690. '']));
  15691. end;
  15692. procedure TTestModule.TestClassOf_ClassVar;
  15693. begin
  15694. StartProgram(false);
  15695. Add('type');
  15696. Add(' TObject = class');
  15697. Add(' class var id: longint;');
  15698. Add(' end;');
  15699. Add(' TClass = class of TObject;');
  15700. Add('var');
  15701. Add(' C: tclass;');
  15702. Add('begin');
  15703. Add(' C.id:=C.id;');
  15704. ConvertProgram;
  15705. CheckSource('TestClassOf_ClassVar',
  15706. LinesToStr([ // statements
  15707. 'rtl.createClass(this, "TObject", null, function () {',
  15708. ' this.id = 0;',
  15709. ' this.$init = function () {',
  15710. ' };',
  15711. ' this.$final = function () {',
  15712. ' };',
  15713. '});',
  15714. 'this.C = null;'
  15715. ]),
  15716. LinesToStr([ // $mod.$main
  15717. '$mod.TObject.id = $mod.C.id;',
  15718. '']));
  15719. end;
  15720. procedure TTestModule.TestClassOf_ClassMethod;
  15721. begin
  15722. StartProgram(false);
  15723. Add('type');
  15724. Add(' TObject = class');
  15725. Add(' class function DoIt(i: longint = 0): longint;');
  15726. Add(' end;');
  15727. Add(' TClass = class of TObject;');
  15728. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15729. Add('var');
  15730. Add(' i: longint;');
  15731. Add(' C: tclass;');
  15732. Add('begin');
  15733. Add(' C.DoIt;');
  15734. Add(' C.DoIt();');
  15735. Add(' i:=C.DoIt;');
  15736. Add(' i:=C.DoIt();');
  15737. ConvertProgram;
  15738. CheckSource('TestClassOf_ClassMethod',
  15739. LinesToStr([ // statements
  15740. 'rtl.createClass(this, "TObject", null, function () {',
  15741. ' this.$init = function () {',
  15742. ' };',
  15743. ' this.$final = function () {',
  15744. ' };',
  15745. ' this.DoIt = function (i) {',
  15746. ' var Result = 0;',
  15747. ' return Result;',
  15748. ' };',
  15749. '});',
  15750. 'this.i = 0;',
  15751. 'this.C = null;'
  15752. ]),
  15753. LinesToStr([ // $mod.$main
  15754. '$mod.C.DoIt(0);',
  15755. '$mod.C.DoIt(0);',
  15756. '$mod.i = $mod.C.DoIt(0);',
  15757. '$mod.i = $mod.C.DoIt(0);',
  15758. '']));
  15759. end;
  15760. procedure TTestModule.TestClassOf_ClassProperty;
  15761. begin
  15762. StartProgram(false);
  15763. Add([
  15764. 'type',
  15765. ' TObject = class',
  15766. ' class var FA: longint;',
  15767. ' class function GetA: longint;',
  15768. ' class procedure SetA(Value: longint);',
  15769. ' class property pA: longint read fa write fa;',
  15770. ' class property pB: longint read geta write seta;',
  15771. ' end;',
  15772. ' TObjectClass = class of tobject;',
  15773. 'class function tobject.geta: longint; begin end;',
  15774. 'class procedure tobject.seta(value: longint); begin end;',
  15775. 'var',
  15776. ' b: boolean;',
  15777. ' Obj: tobject;',
  15778. ' Cla: tobjectclass;',
  15779. 'begin',
  15780. ' obj.pa:=obj.pa;',
  15781. ' obj.pb:=obj.pb;',
  15782. ' b:=obj.pa=4;',
  15783. ' b:=obj.pb=obj.pb;',
  15784. ' b:=5=obj.pa;',
  15785. ' cla.pa:=6;',
  15786. ' cla.pa:=cla.pa;',
  15787. ' cla.pb:=cla.pb;',
  15788. ' b:=cla.pa=7;',
  15789. ' b:=cla.pb=cla.pb;',
  15790. ' b:=8=cla.pa;',
  15791. ' tobject.pa:=9;',
  15792. ' tobject.pb:=tobject.pb;',
  15793. ' b:=tobject.pa=10;',
  15794. ' b:=11=tobject.pa;',
  15795. '']);
  15796. ConvertProgram;
  15797. CheckSource('TestClassOf_ClassProperty',
  15798. LinesToStr([ // statements
  15799. 'rtl.createClass(this, "TObject", null, function () {',
  15800. ' this.FA = 0;',
  15801. ' this.$init = function () {',
  15802. ' };',
  15803. ' this.$final = function () {',
  15804. ' };',
  15805. ' this.GetA = function () {',
  15806. ' var Result = 0;',
  15807. ' return Result;',
  15808. ' };',
  15809. ' this.SetA = function (Value) {',
  15810. ' };',
  15811. '});',
  15812. 'this.b = false;',
  15813. 'this.Obj = null;',
  15814. 'this.Cla = null;'
  15815. ]),
  15816. LinesToStr([ // $mod.$main
  15817. '$mod.TObject.FA = $mod.Obj.FA;',
  15818. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15819. '$mod.b = $mod.Obj.FA === 4;',
  15820. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15821. '$mod.b = 5 === $mod.Obj.FA;',
  15822. '$mod.TObject.FA = 6;',
  15823. '$mod.TObject.FA = $mod.Cla.FA;',
  15824. '$mod.Cla.SetA($mod.Cla.GetA());',
  15825. '$mod.b = $mod.Cla.FA === 7;',
  15826. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15827. '$mod.b = 8 === $mod.Cla.FA;',
  15828. '$mod.TObject.FA = 9;',
  15829. '$mod.TObject.SetA($mod.TObject.GetA());',
  15830. '$mod.b = $mod.TObject.FA === 10;',
  15831. '$mod.b = 11 === $mod.TObject.FA;',
  15832. '']));
  15833. end;
  15834. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15835. begin
  15836. StartProgram(false);
  15837. Add('type');
  15838. Add(' TObject = class');
  15839. Add(' class var GlobalId: longint;');
  15840. Add(' class procedure ProcA;');
  15841. Add(' end;');
  15842. Add('class procedure tobject.proca;');
  15843. Add('var b: boolean;');
  15844. Add('begin');
  15845. Add(' b:=self=nil;');
  15846. Add(' b:=self.globalid=3;');
  15847. Add(' b:=4=self.globalid;');
  15848. Add(' self.globalid:=5;');
  15849. Add(' self.proca;');
  15850. Add('end;');
  15851. Add('begin');
  15852. ConvertProgram;
  15853. CheckSource('TestClassOf_ClassMethodSelf',
  15854. LinesToStr([ // statements
  15855. 'rtl.createClass(this, "TObject", null, function () {',
  15856. ' this.GlobalId = 0;',
  15857. ' this.$init = function () {',
  15858. ' };',
  15859. ' this.$final = function () {',
  15860. ' };',
  15861. ' this.ProcA = function () {',
  15862. ' var b = false;',
  15863. ' b = this === null;',
  15864. ' b = this.GlobalId === 3;',
  15865. ' b = 4 === this.GlobalId;',
  15866. ' $mod.TObject.GlobalId = 5;',
  15867. ' this.ProcA();',
  15868. ' };',
  15869. '});'
  15870. ]),
  15871. LinesToStr([ // $mod.$main
  15872. '']));
  15873. end;
  15874. procedure TTestModule.TestClassOf_TypeCast;
  15875. begin
  15876. StartProgram(false);
  15877. Add('type');
  15878. Add(' TObject = class');
  15879. Add(' class procedure {#TObject_DoIt}DoIt;');
  15880. Add(' end;');
  15881. Add(' TClass = class of TObject;');
  15882. Add(' TMobile = class');
  15883. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15884. Add(' end;');
  15885. Add(' TMobileClass = class of TMobile;');
  15886. Add(' TCar = class(TMobile)');
  15887. Add(' class procedure {#TCar_DoIt}DoIt;');
  15888. Add(' end;');
  15889. Add(' TCarClass = class of TCar;');
  15890. Add('class procedure TObject.DoIt;');
  15891. Add('begin');
  15892. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15893. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15894. Add('end;');
  15895. Add('class procedure TMobile.DoIt;');
  15896. Add('begin');
  15897. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15898. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15899. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15900. Add('end;');
  15901. Add('class procedure TCar.DoIt; begin end;');
  15902. Add('var');
  15903. Add(' ObjC: TClass;');
  15904. Add(' MobileC: TMobileClass;');
  15905. Add(' CarC: TCarClass;');
  15906. Add('begin');
  15907. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15908. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15909. Add(' CarC.{@TCar_DoIt}DoIt;');
  15910. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15911. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15912. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15913. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15914. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15915. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15916. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15917. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15918. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15919. ConvertProgram;
  15920. CheckSource('TestClassOf_TypeCast',
  15921. LinesToStr([ // statements
  15922. 'rtl.createClass(this, "TObject", null, function () {',
  15923. ' this.$init = function () {',
  15924. ' };',
  15925. ' this.$final = function () {',
  15926. ' };',
  15927. ' this.DoIt = function () {',
  15928. ' this.DoIt();',
  15929. ' this.DoIt$1();',
  15930. ' };',
  15931. '});',
  15932. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15933. ' this.DoIt$1 = function () {',
  15934. ' this.DoIt();',
  15935. ' this.DoIt$1();',
  15936. ' this.DoIt$2();',
  15937. ' };',
  15938. '});',
  15939. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15940. ' this.DoIt$2 = function () {',
  15941. ' };',
  15942. '});',
  15943. 'this.ObjC = null;',
  15944. 'this.MobileC = null;',
  15945. 'this.CarC = null;',
  15946. '']),
  15947. LinesToStr([ // $mod.$main
  15948. '$mod.ObjC.DoIt();',
  15949. '$mod.MobileC.DoIt$1();',
  15950. '$mod.CarC.DoIt$2();',
  15951. '$mod.ObjC.DoIt();',
  15952. '$mod.ObjC.DoIt$1();',
  15953. '$mod.ObjC.DoIt$2();',
  15954. '$mod.MobileC.DoIt();',
  15955. '$mod.MobileC.DoIt$1();',
  15956. '$mod.MobileC.DoIt$2();',
  15957. '$mod.CarC.DoIt();',
  15958. '$mod.CarC.DoIt$1();',
  15959. '$mod.CarC.DoIt$2();',
  15960. '']));
  15961. end;
  15962. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15963. begin
  15964. StartProgram(false);
  15965. Add('type');
  15966. Add(' TObject = class');
  15967. Add(' function CurNow: longint; ');
  15968. Add(' class function Now: longint; ');
  15969. Add(' end;');
  15970. Add('function TObject.CurNow: longint; begin end;');
  15971. Add('class function TObject.Now: longint; begin end;');
  15972. Add('var');
  15973. Add(' Obj: tobject;');
  15974. Add(' vI: longint;');
  15975. Add('begin');
  15976. Add(' obj.curnow;');
  15977. Add(' vi:=obj.curnow;');
  15978. Add(' tobject.now;');
  15979. Add(' vi:=tobject.now;');
  15980. ConvertProgram;
  15981. CheckSource('TestClassOf_ImplicitFunctionCall',
  15982. LinesToStr([ // statements
  15983. 'rtl.createClass(this, "TObject", null, function () {',
  15984. ' this.$init = function () {',
  15985. ' };',
  15986. ' this.$final = function () {',
  15987. ' };',
  15988. ' this.CurNow = function () {',
  15989. ' var Result = 0;',
  15990. ' return Result;',
  15991. ' };',
  15992. ' this.Now = function () {',
  15993. ' var Result = 0;',
  15994. ' return Result;',
  15995. ' };',
  15996. '});',
  15997. 'this.Obj = null;',
  15998. 'this.vI = 0;',
  15999. '']),
  16000. LinesToStr([ // $mod.$main
  16001. '$mod.Obj.CurNow();',
  16002. '$mod.vI = $mod.Obj.CurNow();',
  16003. '$mod.TObject.Now();',
  16004. '$mod.vI = $mod.TObject.Now();',
  16005. '']));
  16006. end;
  16007. procedure TTestModule.TestClassOf_Const;
  16008. begin
  16009. StartProgram(false);
  16010. Add([
  16011. 'type',
  16012. ' TObject = class',
  16013. ' end;',
  16014. ' TBird = TObject;',
  16015. ' TBirds = class of TBird;',
  16016. ' TEagles = TBirds;',
  16017. ' THawk = class(TBird);',
  16018. 'const',
  16019. ' Hawk: TEagles = THawk;',
  16020. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16021. ' TBird,',
  16022. ' THawk',
  16023. ' );',
  16024. 'begin']);
  16025. ConvertProgram;
  16026. CheckSource('TestClassOf_Const',
  16027. LinesToStr([ // statements
  16028. 'rtl.createClass(this, "TObject", null, function () {',
  16029. ' this.$init = function () {',
  16030. ' };',
  16031. ' this.$final = function () {',
  16032. ' };',
  16033. '});',
  16034. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16035. '});',
  16036. 'this.Hawk = this.THawk;',
  16037. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16038. '']),
  16039. LinesToStr([ // $mod.$main
  16040. '']));
  16041. end;
  16042. procedure TTestModule.TestNestedClass_Alias;
  16043. begin
  16044. WithTypeInfo:=true;
  16045. StartProgram(false);
  16046. Add([
  16047. 'type',
  16048. ' TObject = class',
  16049. ' type TNested = type longint;',
  16050. ' end;',
  16051. 'type TAlias = type tobject.tnested;',
  16052. 'var i: tobject.tnested = 3;',
  16053. 'var j: TAlias = 4;',
  16054. 'begin',
  16055. ' if typeinfo(TAlias)=nil then ;',
  16056. ' if typeinfo(tobject.tnested)=nil then ;',
  16057. '']);
  16058. ConvertProgram;
  16059. CheckSource('TestNestedClass_Alias',
  16060. LinesToStr([ // statements
  16061. 'rtl.createClass(this, "TObject", null, function () {',
  16062. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16063. ' this.$init = function () {',
  16064. ' };',
  16065. ' this.$final = function () {',
  16066. ' };',
  16067. '});',
  16068. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16069. 'this.i = 3;',
  16070. 'this.j = 4;',
  16071. '']),
  16072. LinesToStr([ // $mod.$main
  16073. 'if ($mod.$rtti["TAlias"] === null) ;',
  16074. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16075. '']));
  16076. end;
  16077. procedure TTestModule.TestNestedClass_Record;
  16078. begin
  16079. WithTypeInfo:=true;
  16080. StartProgram(false);
  16081. Add([
  16082. 'type',
  16083. ' TObject = class',
  16084. ' type TPoint = record',
  16085. ' x,y: byte;',
  16086. ' end;',
  16087. ' procedure DoIt(t: TPoint);',
  16088. ' end;',
  16089. 'procedure tobject.DoIt(t: TPoint);',
  16090. 'var p: TPoint;',
  16091. 'begin',
  16092. ' t.x:=t.y;',
  16093. ' p:=t;',
  16094. 'end;',
  16095. 'var',
  16096. ' p: tobject.tpoint = (x:2; y:4);',
  16097. ' o: TObject;',
  16098. 'begin',
  16099. ' p:=p;',
  16100. ' o.doit(p);',
  16101. '']);
  16102. ConvertProgram;
  16103. CheckSource('TestNestedClass_Record',
  16104. LinesToStr([ // statements
  16105. 'rtl.createClass(this, "TObject", null, function () {',
  16106. ' rtl.recNewT(this, "TPoint", function () {',
  16107. ' this.x = 0;',
  16108. ' this.y = 0;',
  16109. ' this.$eq = function (b) {',
  16110. ' return (this.x === b.x) && (this.y === b.y);',
  16111. ' };',
  16112. ' this.$assign = function (s) {',
  16113. ' this.x = s.x;',
  16114. ' this.y = s.y;',
  16115. ' return this;',
  16116. ' };',
  16117. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16118. ' $r.addField("x", rtl.byte);',
  16119. ' $r.addField("y", rtl.byte);',
  16120. ' });',
  16121. ' this.$init = function () {',
  16122. ' };',
  16123. ' this.$final = function () {',
  16124. ' };',
  16125. ' this.DoIt = function (t) {',
  16126. ' var p = this.TPoint.$new();',
  16127. ' t.x = t.y;',
  16128. ' p.$assign(t);',
  16129. ' };',
  16130. '});',
  16131. 'this.p = this.TObject.TPoint.$clone({',
  16132. ' x: 2,',
  16133. ' y: 4',
  16134. '});',
  16135. 'this.o = null;',
  16136. '']),
  16137. LinesToStr([ // $mod.$main
  16138. '$mod.p.$assign($mod.p);',
  16139. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16140. '']));
  16141. end;
  16142. procedure TTestModule.TestNestedClass_Class;
  16143. begin
  16144. WithTypeInfo:=true;
  16145. StartProgram(false);
  16146. Add([
  16147. 'type',
  16148. ' TObject = class end;',
  16149. ' TBird = class',
  16150. ' type TLeg = class',
  16151. ' FId: longint;',
  16152. ' constructor Create;',
  16153. ' function Create(i: longint): TLeg;',
  16154. ' end;',
  16155. ' function DoIt(b: TBird): Tleg;',
  16156. ' end;',
  16157. 'constructor tbird.tleg.create;',
  16158. 'begin',
  16159. ' FId:=3;',
  16160. 'end;',
  16161. 'function tbird.tleg.Create(i: longint): TLeg;',
  16162. 'begin',
  16163. ' Create;',
  16164. ' Result:=TLeg.Create;',
  16165. ' Result:=TBird.TLeg.Create;',
  16166. ' Result:=Create(3);',
  16167. ' FId:=i;',
  16168. 'end;',
  16169. 'function tbird.DoIt(b: tbird): tleg;',
  16170. 'begin',
  16171. ' Result.Create;',
  16172. ' Result:=TLeg.Create;',
  16173. ' Result:=TBird.TLeg.Create;',
  16174. ' Result:=Result.Create(3);',
  16175. 'end;',
  16176. 'var',
  16177. ' b: Tbird.tleg;',
  16178. 'begin',
  16179. ' b.Create;',
  16180. ' b:=TBird.TLeg.Create;',
  16181. ' b:=b.Create(3);',
  16182. '']);
  16183. ConvertProgram;
  16184. CheckSource('TestNestedClass_Class',
  16185. LinesToStr([ // statements
  16186. 'rtl.createClass(this, "TObject", null, function () {',
  16187. ' this.$init = function () {',
  16188. ' };',
  16189. ' this.$final = function () {',
  16190. ' };',
  16191. '});',
  16192. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16193. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16194. ' this.$init = function () {',
  16195. ' $mod.TObject.$init.call(this);',
  16196. ' this.FId = 0;',
  16197. ' };',
  16198. ' this.Create = function () {',
  16199. ' this.FId = 3;',
  16200. ' return this;',
  16201. ' };',
  16202. ' this.Create$1 = function (i) {',
  16203. ' var Result = null;',
  16204. ' this.Create();',
  16205. ' Result = $mod.TBird.TLeg.$create("Create");',
  16206. ' Result = $mod.TBird.TLeg.$create("Create");',
  16207. ' Result = this.Create$1(3);',
  16208. ' this.FId = i;',
  16209. ' return Result;',
  16210. ' };',
  16211. ' }, "TBird.TLeg");',
  16212. ' this.DoIt = function (b) {',
  16213. ' var Result = null;',
  16214. ' Result.Create();',
  16215. ' Result = this.TLeg.$create("Create");',
  16216. ' Result = $mod.TBird.TLeg.$create("Create");',
  16217. ' Result = Result.Create$1(3);',
  16218. ' return Result;',
  16219. ' };',
  16220. '});',
  16221. 'this.b = null;',
  16222. '']),
  16223. LinesToStr([ // $mod.$main
  16224. '$mod.b.Create();',
  16225. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16226. '$mod.b = $mod.b.Create$1(3);',
  16227. '']));
  16228. end;
  16229. procedure TTestModule.TestExternalClass_Var;
  16230. begin
  16231. StartProgram(false);
  16232. Add([
  16233. '{$modeswitch externalclass}',
  16234. 'type',
  16235. ' TExtA = class external name ''ExtObj''',
  16236. ' Id: longint external name ''$Id'';',
  16237. ' B: longint;',
  16238. ' end;',
  16239. 'var Obj: TExtA;',
  16240. 'begin',
  16241. ' obj.id:=obj.id+1;',
  16242. ' obj.B:=obj.B+1;']);
  16243. ConvertProgram;
  16244. CheckSource('TestExternalClass_Var',
  16245. LinesToStr([ // statements
  16246. 'this.Obj = null;',
  16247. '']),
  16248. LinesToStr([ // $mod.$main
  16249. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16250. '$mod.Obj.B = $mod.Obj.B + 1;',
  16251. '']));
  16252. end;
  16253. procedure TTestModule.TestExternalClass_Const;
  16254. begin
  16255. StartProgram(false);
  16256. Add([
  16257. '{$modeswitch externalclass}',
  16258. 'type',
  16259. ' TExtA = class external name ''ExtObj''',
  16260. ' const Two: longint = 2;',
  16261. ' const Three = 3;',
  16262. ' const Id: longint;',
  16263. ' end;',
  16264. ' TExtB = class external name ''ExtB''',
  16265. ' A: TExtA;',
  16266. ' end;',
  16267. 'var',
  16268. ' A: texta;',
  16269. ' B: textb;',
  16270. ' i: longint;',
  16271. 'begin',
  16272. ' i:=a.two;',
  16273. ' i:=texta.two;',
  16274. ' i:=a.three;',
  16275. ' i:=texta.three;',
  16276. ' i:=a.id;',
  16277. ' i:=texta.id;',
  16278. '']);
  16279. ConvertProgram;
  16280. CheckSource('TestExternalClass_Const',
  16281. LinesToStr([ // statements
  16282. 'this.A = null;',
  16283. 'this.B = null;',
  16284. 'this.i = 0;',
  16285. '']),
  16286. LinesToStr([ // $mod.$main
  16287. '$mod.i = 2;',
  16288. '$mod.i = 2;',
  16289. '$mod.i = 3;',
  16290. '$mod.i = 3;',
  16291. '$mod.i = $mod.A.Id;',
  16292. '$mod.i = ExtObj.Id;',
  16293. '']));
  16294. end;
  16295. procedure TTestModule.TestExternalClass_Dollar;
  16296. begin
  16297. StartProgram(false);
  16298. Add([
  16299. '{$modeswitch externalclass}',
  16300. 'type',
  16301. ' TExtA = class external name ''$''',
  16302. ' Id: longint external name ''$'';',
  16303. ' function Bla(i: longint): longint; external name ''$'';',
  16304. ' end;',
  16305. 'function dollar(k: longint): longint; external name ''$'';',
  16306. 'var Obj: TExtA;',
  16307. 'begin',
  16308. ' dollar(1);',
  16309. ' obj.id:=obj.id+2;',
  16310. ' obj.Bla(3);',
  16311. '']);
  16312. ConvertProgram;
  16313. CheckSource('TestExternalClass_Dollar',
  16314. LinesToStr([ // statements
  16315. 'this.Obj = null;',
  16316. '']),
  16317. LinesToStr([ // $mod.$main
  16318. '$(1);',
  16319. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16320. '$mod.Obj.$(3);',
  16321. '']));
  16322. end;
  16323. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16324. begin
  16325. StartProgram(false);
  16326. Add('{$modeswitch externalclass}');
  16327. Add('type');
  16328. Add(' TExtA = class external name ''ExtA''');
  16329. Add(' Id: longint external name ''$Id'';');
  16330. Add(' end;');
  16331. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16332. Add(' Id: longint;');
  16333. Add(' end;');
  16334. Add('begin');
  16335. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16336. ConvertProgram;
  16337. end;
  16338. procedure TTestModule.TestExternalClass_Method;
  16339. begin
  16340. StartProgram(false);
  16341. Add(['{$modeswitch externalclass}',
  16342. 'type',
  16343. ' TExtA = class external name ''ExtObj''',
  16344. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16345. ' procedure DoSome(Id: longint = 1);',
  16346. ' end;',
  16347. 'var Obj: texta;',
  16348. 'begin',
  16349. ' obj.doit;',
  16350. ' obj.doit();',
  16351. ' obj.doit(2);',
  16352. ' with obj do begin',
  16353. ' doit;',
  16354. ' doit();',
  16355. ' doit(3);',
  16356. ' end;']);
  16357. ConvertProgram;
  16358. CheckSource('TestExternalClass_Method',
  16359. LinesToStr([ // statements
  16360. 'this.Obj = null;',
  16361. '']),
  16362. LinesToStr([ // $mod.$main
  16363. '$mod.Obj.$Execute(1);',
  16364. '$mod.Obj.$Execute(1);',
  16365. '$mod.Obj.$Execute(2);',
  16366. 'var $with = $mod.Obj;',
  16367. '$with.$Execute(1);',
  16368. '$with.$Execute(1);',
  16369. '$with.$Execute(3);',
  16370. '']));
  16371. end;
  16372. procedure TTestModule.TestExternalClass_ClassMethod;
  16373. begin
  16374. StartProgram(false);
  16375. Add([
  16376. '{$modeswitch externalclass}',
  16377. 'type',
  16378. ' TExtA = class external name ''ExtObj''',
  16379. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16380. ' end;',
  16381. ' TExtB = TExtA;',
  16382. 'var p: Pointer;',
  16383. 'begin',
  16384. ' texta.doit;',
  16385. ' texta.doit();',
  16386. ' texta.doit(2);',
  16387. ' p:[email protected];',
  16388. ' with texta do begin',
  16389. ' doit;',
  16390. ' doit();',
  16391. ' doit(3);',
  16392. ' p:=@DoIt;',
  16393. ' end;',
  16394. ' textb.doit;',
  16395. ' textb.doit();',
  16396. ' textb.doit(4);',
  16397. ' with textb do begin',
  16398. ' doit;',
  16399. ' doit();',
  16400. ' doit(5);',
  16401. ' end;',
  16402. '']);
  16403. ConvertProgram;
  16404. CheckSource('TestExternalClass_ClassMethod',
  16405. LinesToStr([ // statements
  16406. 'this.p = null;',
  16407. '']),
  16408. LinesToStr([ // $mod.$main
  16409. 'ExtObj.$Execute(1);',
  16410. 'ExtObj.$Execute(1);',
  16411. 'ExtObj.$Execute(2);',
  16412. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16413. 'ExtObj.$Execute(1);',
  16414. 'ExtObj.$Execute(1);',
  16415. 'ExtObj.$Execute(3);',
  16416. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16417. 'ExtObj.$Execute(1);',
  16418. 'ExtObj.$Execute(1);',
  16419. 'ExtObj.$Execute(4);',
  16420. 'ExtObj.$Execute(1);',
  16421. 'ExtObj.$Execute(1);',
  16422. 'ExtObj.$Execute(5);',
  16423. '']));
  16424. end;
  16425. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16426. begin
  16427. StartProgram(false);
  16428. Add([
  16429. '{$modeswitch externalclass}',
  16430. 'type',
  16431. ' TExtA = class external name ''ExtObj''',
  16432. ' class procedure DoIt(Id: longint = 1); static;',
  16433. ' end;',
  16434. 'var p: Pointer;',
  16435. 'begin',
  16436. ' texta.doit;',
  16437. ' texta.doit();',
  16438. ' texta.doit(2);',
  16439. ' p:[email protected];',
  16440. ' with texta do begin',
  16441. ' doit;',
  16442. ' doit();',
  16443. ' doit(3);',
  16444. ' p:=@DoIt;',
  16445. ' end;',
  16446. '']);
  16447. ConvertProgram;
  16448. CheckSource('TestExternalClass_ClassMethodStatic',
  16449. LinesToStr([ // statements
  16450. 'this.p = null;',
  16451. '']),
  16452. LinesToStr([ // $mod.$main
  16453. 'ExtObj.DoIt(1);',
  16454. 'ExtObj.DoIt(1);',
  16455. 'ExtObj.DoIt(2);',
  16456. '$mod.p = ExtObj.DoIt;',
  16457. 'ExtObj.DoIt(1);',
  16458. 'ExtObj.DoIt(1);',
  16459. 'ExtObj.DoIt(3);',
  16460. '$mod.p = ExtObj.DoIt;',
  16461. '']));
  16462. end;
  16463. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16464. begin
  16465. StartProgram(false);
  16466. Add([
  16467. '{$modeswitch externalclass}',
  16468. 'type',
  16469. ' TBird = class external name ''Array''',
  16470. ' end;',
  16471. 'function GetPtr: Pointer;',
  16472. 'begin',
  16473. 'end;',
  16474. 'procedure Write(const p);',
  16475. 'begin',
  16476. 'end;',
  16477. 'procedure WriteLn; varargs;',
  16478. 'begin',
  16479. 'end;',
  16480. 'begin',
  16481. ' if TBird(GetPtr)=nil then ;',
  16482. ' Write(GetPtr);',
  16483. ' WriteLn(GetPtr);',
  16484. ' Write(TBird(GetPtr));',
  16485. ' WriteLn(TBird(GetPtr));',
  16486. '']);
  16487. ConvertProgram;
  16488. CheckSource('TestFunctionResultInTypeCast',
  16489. LinesToStr([ // statements
  16490. 'this.GetPtr = function () {',
  16491. ' var Result = null;',
  16492. ' return Result;',
  16493. '};',
  16494. 'this.Write = function (p) {',
  16495. '};',
  16496. 'this.WriteLn = function () {',
  16497. '};',
  16498. '']),
  16499. LinesToStr([
  16500. 'if ($mod.GetPtr() === null) ;',
  16501. '$mod.Write($mod.GetPtr());',
  16502. '$mod.WriteLn($mod.GetPtr());',
  16503. '$mod.Write($mod.GetPtr());',
  16504. '$mod.WriteLn($mod.GetPtr());',
  16505. '']));
  16506. end;
  16507. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16508. begin
  16509. StartProgram(false);
  16510. Add([
  16511. '{$modeswitch externalclass}',
  16512. 'type',
  16513. ' TExtA = class external name ''ExtObjA''',
  16514. ' procedure ProcA; virtual;',
  16515. ' procedure ProcB; virtual;',
  16516. ' end;',
  16517. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16518. ' end;',
  16519. ' TExtC = class (TExtB)',
  16520. ' procedure ProcA; override;',
  16521. ' end;',
  16522. 'procedure TExtC.ProcA;',
  16523. 'begin',
  16524. ' ProcA;',
  16525. ' Self.ProcA;',
  16526. ' ProcB;',
  16527. ' Self.ProcB;',
  16528. 'end;',
  16529. 'var',
  16530. ' A: texta;',
  16531. ' B: textb;',
  16532. ' C: textc;',
  16533. 'begin',
  16534. ' a.proca;',
  16535. ' b.proca;',
  16536. ' c.proca;']);
  16537. ConvertProgram;
  16538. CheckSource('TestExternalClass_NonExternalOverride',
  16539. LinesToStr([ // statements
  16540. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16541. ' this.$init = function () {',
  16542. ' };',
  16543. ' this.$final = function () {',
  16544. ' };',
  16545. ' this.ProcA = function () {',
  16546. ' this.ProcA();',
  16547. ' this.ProcA();',
  16548. ' this.ProcB();',
  16549. ' this.ProcB();',
  16550. ' };',
  16551. '});',
  16552. 'this.A = null;',
  16553. 'this.B = null;',
  16554. 'this.C = null;',
  16555. '']),
  16556. LinesToStr([ // $mod.$main
  16557. '$mod.A.ProcA();',
  16558. '$mod.B.ProcA();',
  16559. '$mod.C.ProcA();',
  16560. '']));
  16561. end;
  16562. procedure TTestModule.TestExternalClass_OverloadHint;
  16563. begin
  16564. StartProgram(false);
  16565. Add([
  16566. '{$modeswitch externalclass}',
  16567. 'type',
  16568. ' TExtA = class external name ''ExtObjA''',
  16569. ' procedure DoIt;',
  16570. ' procedure DoIt(i: longint);',
  16571. ' end;',
  16572. 'begin',
  16573. '']);
  16574. ConvertProgram;
  16575. CheckResolverUnexpectedHints(true);
  16576. CheckSource('TestExternalClass_OverloadHint',
  16577. LinesToStr([ // statements
  16578. '']),
  16579. LinesToStr([ // $mod.$main
  16580. '']));
  16581. end;
  16582. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16583. begin
  16584. WithTypeInfo:=true;
  16585. StartProgram(false);
  16586. Add([
  16587. '{$modeswitch externalclass}',
  16588. 'type',
  16589. ' JSwiper = class external name ''Swiper''',
  16590. ' constructor New;',
  16591. ' end;',
  16592. ' TObject = class',
  16593. ' private',
  16594. ' FSwiper: JSwiper;',
  16595. ' published',
  16596. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16597. ' end;',
  16598. 'begin',
  16599. ' JSwiper.new;',
  16600. '']);
  16601. ConvertProgram;
  16602. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16603. LinesToStr([ // statements
  16604. 'this.$rtti.$ExtClass("JSwiper", {',
  16605. ' jsclass: "Swiper"',
  16606. '});',
  16607. 'rtl.createClass(this, "TObject", null, function () {',
  16608. ' this.$init = function () {',
  16609. ' this.FSwiper = null;',
  16610. ' };',
  16611. ' this.$final = function () {',
  16612. ' this.FSwiper = undefined;',
  16613. ' };',
  16614. ' var $r = this.$rtti;',
  16615. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16616. '});',
  16617. '']),
  16618. LinesToStr([ // $mod.$main
  16619. 'new Swiper();',
  16620. '']));
  16621. end;
  16622. procedure TTestModule.TestExternalClass_Property;
  16623. begin
  16624. StartProgram(false);
  16625. Add([
  16626. '{$modeswitch externalclass}',
  16627. 'type',
  16628. ' TExtA = class external name ''ExtA''',
  16629. ' function getYear: longint;',
  16630. ' procedure setYear(Value: longint);',
  16631. ' property Year: longint read getyear write setyear;',
  16632. ' end;',
  16633. ' TExtB = class (TExtA)',
  16634. ' procedure OtherSetYear(Value: longint);',
  16635. ' property year write othersetyear;',
  16636. ' end;',
  16637. 'procedure textb.othersetyear(value: longint);',
  16638. 'begin',
  16639. ' setYear(Value+4);',
  16640. 'end;',
  16641. 'var',
  16642. ' A: texta;',
  16643. ' B: textb;',
  16644. 'begin',
  16645. ' a.year:=a.year+1;',
  16646. ' b.year:=b.year+2;']);
  16647. ConvertProgram;
  16648. CheckSource('TestExternalClass_NonExternalOverride',
  16649. LinesToStr([ // statements
  16650. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16651. ' this.$init = function () {',
  16652. ' };',
  16653. ' this.$final = function () {',
  16654. ' };',
  16655. ' this.OtherSetYear = function (Value) {',
  16656. ' this.setYear(Value+4);',
  16657. ' };',
  16658. '});',
  16659. 'this.A = null;',
  16660. 'this.B = null;',
  16661. '']),
  16662. LinesToStr([ // $mod.$main
  16663. '$mod.A.setYear($mod.A.getYear()+1);',
  16664. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16665. '']));
  16666. end;
  16667. procedure TTestModule.TestExternalClass_PropertyDate;
  16668. begin
  16669. StartProgram(false);
  16670. Add([
  16671. '{$modeswitch externalclass}',
  16672. 'type',
  16673. ' TExtA = class external name ''ExtA''',
  16674. ' end;',
  16675. ' TExtB = class (TExtA)',
  16676. ' FDate: string;',
  16677. ' property Date: string read FDate write FDate;',
  16678. ' property ExtA: string read FDate write FDate;',
  16679. ' end;',
  16680. ' {$M+}',
  16681. ' TObject = class',
  16682. ' FDate: string;',
  16683. ' published',
  16684. ' property Date: string read FDate write FDate;',
  16685. ' property ExtA: string read FDate write FDate;',
  16686. ' end;',
  16687. 'var',
  16688. ' B: textb;',
  16689. ' o: TObject;',
  16690. 'begin',
  16691. ' b.date:=b.exta;',
  16692. ' o.date:=o.exta;']);
  16693. ConvertProgram;
  16694. CheckSource('TestExternalClass_PropertyDate',
  16695. LinesToStr([ // statements
  16696. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16697. ' this.$init = function () {',
  16698. ' this.FDate = "";',
  16699. ' };',
  16700. ' this.$final = function () {',
  16701. ' };',
  16702. '});',
  16703. 'rtl.createClass(this, "TObject", null, function () {',
  16704. ' this.$init = function () {',
  16705. ' this.FDate = "";',
  16706. ' };',
  16707. ' this.$final = function () {',
  16708. ' };',
  16709. ' var $r = this.$rtti;',
  16710. ' $r.addField("FDate", rtl.string);',
  16711. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16712. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16713. '});',
  16714. 'this.B = null;',
  16715. 'this.o = null;',
  16716. '']),
  16717. LinesToStr([ // $mod.$main
  16718. '$mod.B.FDate = $mod.B.FDate;',
  16719. '$mod.o.FDate = $mod.o.FDate;',
  16720. '']));
  16721. end;
  16722. procedure TTestModule.TestExternalClass_ClassProperty;
  16723. begin
  16724. StartProgram(false);
  16725. Add('{$modeswitch externalclass}');
  16726. Add('type');
  16727. Add(' TExtA = class external name ''ExtA''');
  16728. Add(' class function getYear: longint;');
  16729. Add(' class procedure setYear(Value: longint);');
  16730. Add(' class property Year: longint read getyear write setyear;');
  16731. Add(' end;');
  16732. Add(' TExtB = class (TExtA)');
  16733. Add(' class function GetCentury: longint;');
  16734. Add(' class procedure SetCentury(Value: longint);');
  16735. Add(' class property Century: longint read getcentury write setcentury;');
  16736. Add(' end;');
  16737. Add('class function textb.getcentury: longint;');
  16738. Add('begin');
  16739. Add('end;');
  16740. Add('class procedure textb.setcentury(value: longint);');
  16741. Add('begin');
  16742. Add(' setyear(value+11);');
  16743. Add(' texta.year:=texta.year+12;');
  16744. Add(' year:=year+13;');
  16745. Add(' textb.century:=textb.century+14;');
  16746. Add(' century:=century+15;');
  16747. Add('end;');
  16748. Add('var');
  16749. Add(' A: texta;');
  16750. Add(' B: textb;');
  16751. Add('begin');
  16752. Add(' texta.year:=texta.year+1;');
  16753. Add(' textb.year:=textb.year+2;');
  16754. Add(' TextA.year:=TextA.year+3;');
  16755. Add(' b.year:=b.year+4;');
  16756. Add(' textb.century:=textb.century+5;');
  16757. Add(' b.century:=b.century+6;');
  16758. ConvertProgram;
  16759. CheckSource('TestExternalClass_ClassProperty',
  16760. LinesToStr([ // statements
  16761. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16762. ' this.$init = function () {',
  16763. ' };',
  16764. ' this.$final = function () {',
  16765. ' };',
  16766. ' this.GetCentury = function () {',
  16767. ' var Result = 0;',
  16768. ' return Result;',
  16769. ' };',
  16770. ' this.SetCentury = function (Value) {',
  16771. ' this.setYear(Value + 11);',
  16772. ' ExtA.setYear(ExtA.getYear() + 12);',
  16773. ' this.setYear(this.getYear() + 13);',
  16774. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16775. ' this.SetCentury(this.GetCentury() + 15);',
  16776. ' };',
  16777. '});',
  16778. 'this.A = null;',
  16779. 'this.B = null;',
  16780. '']),
  16781. LinesToStr([ // $mod.$main
  16782. 'ExtA.setYear(ExtA.getYear() + 1);',
  16783. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16784. 'ExtA.setYear(ExtA.getYear() + 3);',
  16785. '$mod.B.setYear($mod.B.getYear() + 4);',
  16786. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16787. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16788. '']));
  16789. end;
  16790. procedure TTestModule.TestExternalClass_ClassOf;
  16791. begin
  16792. StartProgram(false);
  16793. Add('{$modeswitch externalclass}');
  16794. Add('type');
  16795. Add(' TExtA = class external name ''ExtA''');
  16796. Add(' procedure ProcA; virtual;');
  16797. Add(' procedure ProcB; virtual;');
  16798. Add(' end;');
  16799. Add(' TExtAClass = class of TExtA;');
  16800. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16801. Add(' end;');
  16802. Add(' TExtBClass = class of TExtB;');
  16803. Add(' TExtC = class (TExtB)');
  16804. Add(' procedure ProcA; override;');
  16805. Add(' end;');
  16806. Add(' TExtCClass = class of TExtC;');
  16807. Add('procedure TExtC.ProcA; begin end;');
  16808. Add('var');
  16809. Add(' A: texta; ClA: TExtAClass;');
  16810. Add(' B: textb; ClB: TExtBClass;');
  16811. Add(' C: textc; ClC: TExtCClass;');
  16812. Add('begin');
  16813. Add(' ClA:=texta;');
  16814. Add(' ClA:=textb;');
  16815. Add(' ClA:=textc;');
  16816. Add(' ClB:=textb;');
  16817. Add(' ClB:=textc;');
  16818. Add(' ClC:=textc;');
  16819. ConvertProgram;
  16820. CheckSource('TestExternalClass_ClassOf',
  16821. LinesToStr([ // statements
  16822. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16823. ' this.$init = function () {',
  16824. ' };',
  16825. ' this.$final = function () {',
  16826. ' };',
  16827. ' this.ProcA = function () {',
  16828. ' };',
  16829. '});',
  16830. 'this.A = null;',
  16831. 'this.ClA = null;',
  16832. 'this.B = null;',
  16833. 'this.ClB = null;',
  16834. 'this.C = null;',
  16835. 'this.ClC = null;',
  16836. '']),
  16837. LinesToStr([ // $mod.$main
  16838. '$mod.ClA = ExtA;',
  16839. '$mod.ClA = ExtB;',
  16840. '$mod.ClA = $mod.TExtC;',
  16841. '$mod.ClB = ExtB;',
  16842. '$mod.ClB = $mod.TExtC;',
  16843. '$mod.ClC = $mod.TExtC;',
  16844. '']));
  16845. end;
  16846. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16847. begin
  16848. AddModuleWithIntfImplSrc('unit2.pas',
  16849. LinesToStr([
  16850. '{$modeswitch externalclass}',
  16851. 'type',
  16852. ' TExtA = class external name ''ExtA''',
  16853. ' class var Id: longint;',
  16854. ' end;',
  16855. '']),
  16856. '');
  16857. StartUnit(true);
  16858. Add('interface');
  16859. Add('uses unit2;');
  16860. Add('implementation');
  16861. Add('begin');
  16862. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16863. ConvertUnit;
  16864. CheckSource('TestExternalClass_ClassOtherUnit',
  16865. LinesToStr([
  16866. '']),
  16867. LinesToStr([
  16868. 'ExtA.Id = ExtA.Id + 1;',
  16869. '']));
  16870. end;
  16871. procedure TTestModule.TestExternalClass_Is;
  16872. begin
  16873. StartProgram(false);
  16874. Add([
  16875. '{$modeswitch externalclass}',
  16876. 'type',
  16877. ' TExtA = class external name ''ExtA''',
  16878. ' end;',
  16879. ' TExtAClass = class of TExtA;',
  16880. ' TExtB = class external name ''ExtB'' (TExtA)',
  16881. ' end;',
  16882. ' TExtBClass = class of TExtB;',
  16883. ' TExtC = class (TExtB)',
  16884. ' end;',
  16885. ' TExtCClass = class of TExtC;',
  16886. 'var',
  16887. ' A: texta; ClA: TExtAClass;',
  16888. ' B: textb; ClB: TExtBClass;',
  16889. ' C: textc; ClC: TExtCClass;',
  16890. 'begin',
  16891. ' if a is textb then ;',
  16892. ' if a is textc then ;',
  16893. ' if b is textc then ;',
  16894. ' if cla is textb then ;',
  16895. ' if cla is textc then ;',
  16896. ' if clb is textc then ;',
  16897. ' try',
  16898. ' except',
  16899. ' on TExtA do ;',
  16900. ' on e: TExtB do ;',
  16901. ' end;',
  16902. '']);
  16903. ConvertProgram;
  16904. CheckSource('TestExternalClass_Is',
  16905. LinesToStr([ // statements
  16906. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16907. ' this.$init = function () {',
  16908. ' };',
  16909. ' this.$final = function () {',
  16910. ' };',
  16911. '});',
  16912. 'this.A = null;',
  16913. 'this.ClA = null;',
  16914. 'this.B = null;',
  16915. 'this.ClB = null;',
  16916. 'this.C = null;',
  16917. 'this.ClC = null;',
  16918. '']),
  16919. LinesToStr([ // $mod.$main
  16920. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16921. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16922. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16923. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16924. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16925. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16926. 'try {} catch ($e) {',
  16927. ' if (rtl.isExt($e,ExtA)) {}',
  16928. ' else if (rtl.isExt($e,ExtB)) {',
  16929. ' var e = $e;',
  16930. ' } else throw $e',
  16931. '};',
  16932. '']));
  16933. end;
  16934. procedure TTestModule.TestExternalClass_As;
  16935. begin
  16936. StartProgram(false);
  16937. Add('{$modeswitch externalclass}');
  16938. Add('type');
  16939. Add(' TExtA = class external name ''ExtA''');
  16940. Add(' end;');
  16941. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16942. Add(' end;');
  16943. Add(' TExtC = class (TExtB)');
  16944. Add(' end;');
  16945. Add('var');
  16946. Add(' A: texta;');
  16947. Add(' B: textb;');
  16948. Add(' C: textc;');
  16949. Add('begin');
  16950. Add(' b:=a as textb;');
  16951. Add(' c:=a as textc;');
  16952. Add(' c:=b as textc;');
  16953. ConvertProgram;
  16954. CheckSource('TestExternalClass_Is',
  16955. LinesToStr([ // statements
  16956. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16957. ' this.$init = function () {',
  16958. ' };',
  16959. ' this.$final = function () {',
  16960. ' };',
  16961. '});',
  16962. 'this.A = null;',
  16963. 'this.B = null;',
  16964. 'this.C = null;',
  16965. '']),
  16966. LinesToStr([ // $mod.$main
  16967. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16968. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16969. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16970. '']));
  16971. end;
  16972. procedure TTestModule.TestExternalClass_DestructorFail;
  16973. begin
  16974. StartProgram(false);
  16975. Add('{$modeswitch externalclass}');
  16976. Add('type');
  16977. Add(' TExtA = class external name ''ExtA''');
  16978. Add(' destructor Free;');
  16979. Add(' end;');
  16980. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16981. nPasElementNotSupported);
  16982. ConvertProgram;
  16983. end;
  16984. procedure TTestModule.TestExternalClass_New;
  16985. begin
  16986. StartProgram(false);
  16987. Add([
  16988. '{$modeswitch externalclass}',
  16989. 'type',
  16990. ' TExtA = class external name ''ExtA''',
  16991. ' constructor New;',
  16992. ' constructor New(i: longint; j: longint = 2);',
  16993. ' end;',
  16994. 'var',
  16995. ' A: texta;',
  16996. 'begin',
  16997. ' a:=texta.new;',
  16998. ' a:=texta(texta.new);',
  16999. ' a:=texta.new();',
  17000. ' a:=texta.new(1);',
  17001. ' with texta do begin',
  17002. ' a:=new;',
  17003. ' a:=new();',
  17004. ' a:=new(2);',
  17005. ' end;',
  17006. ' a:=test1.texta.new;',
  17007. ' a:=test1.texta.new();',
  17008. ' a:=test1.texta.new(3);',
  17009. '']);
  17010. ConvertProgram;
  17011. CheckSource('TestExternalClass_New',
  17012. LinesToStr([ // statements
  17013. 'this.A = null;',
  17014. '']),
  17015. LinesToStr([ // $mod.$main
  17016. '$mod.A = new ExtA();',
  17017. '$mod.A = new ExtA();',
  17018. '$mod.A = new ExtA();',
  17019. '$mod.A = new ExtA(1,2);',
  17020. '$mod.A = new ExtA();',
  17021. '$mod.A = new ExtA();',
  17022. '$mod.A = new ExtA(2,2);',
  17023. '$mod.A = new ExtA();',
  17024. '$mod.A = new ExtA();',
  17025. '$mod.A = new ExtA(3,2);',
  17026. '']));
  17027. end;
  17028. procedure TTestModule.TestExternalClass_ClassOf_New;
  17029. begin
  17030. StartProgram(false);
  17031. Add('{$modeswitch externalclass}');
  17032. Add('type');
  17033. Add(' TExtAClass = class of TExtA;');
  17034. Add(' TExtA = class external name ''ExtA''');
  17035. Add(' C: TExtAClass;');
  17036. Add(' constructor New;');
  17037. Add(' end;');
  17038. Add('var');
  17039. Add(' A: texta;');
  17040. Add(' C: textaclass;');
  17041. Add('begin');
  17042. Add(' a:=c.new;');
  17043. Add(' a:=c.new();');
  17044. Add(' with C do begin');
  17045. Add(' a:=new;');
  17046. Add(' a:=new();');
  17047. Add(' end;');
  17048. Add(' a:=test1.c.new;');
  17049. Add(' a:=test1.c.new();');
  17050. Add(' a:=A.c.new();');
  17051. ConvertProgram;
  17052. CheckSource('TestExternalClass_ClassOf_New',
  17053. LinesToStr([ // statements
  17054. 'this.A = null;',
  17055. 'this.C = null;',
  17056. '']),
  17057. LinesToStr([ // $mod.$main
  17058. '$mod.A = new $mod.C();',
  17059. '$mod.A = new $mod.C();',
  17060. 'var $with = $mod.C;',
  17061. '$mod.A = new $with();',
  17062. '$mod.A = new $with();',
  17063. '$mod.A = new $mod.C();',
  17064. '$mod.A = new $mod.C();',
  17065. '$mod.A = new $mod.A.C();',
  17066. '']));
  17067. end;
  17068. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17069. begin
  17070. StartProgram(false);
  17071. Add([
  17072. '{$modeswitch externalclass}',
  17073. 'type',
  17074. ' TExtAClass = class of TExtA;',
  17075. ' TExtA = class external name ''ExtA''',
  17076. ' constructor New;',
  17077. ' end;',
  17078. 'function GetCreator: TExtAClass;',
  17079. 'begin',
  17080. ' Result:=TExtA;',
  17081. 'end;',
  17082. 'var',
  17083. ' A: texta;',
  17084. 'begin',
  17085. ' a:=getcreator.new;',
  17086. ' a:=getcreator().new;',
  17087. ' a:=getcreator().new();',
  17088. ' a:=getcreator.new();',
  17089. ' with getcreator do begin',
  17090. ' a:=new;',
  17091. ' a:=new();',
  17092. ' end;']);
  17093. ConvertProgram;
  17094. CheckSource('TestExternalClass_FuncClassOf_New',
  17095. LinesToStr([ // statements
  17096. 'this.GetCreator = function () {',
  17097. ' var Result = null;',
  17098. ' Result = ExtA;',
  17099. ' return Result;',
  17100. '};',
  17101. 'this.A = null;',
  17102. '']),
  17103. LinesToStr([ // $mod.$main
  17104. '$mod.A = new ($mod.GetCreator())();',
  17105. '$mod.A = new ($mod.GetCreator())();',
  17106. '$mod.A = new ($mod.GetCreator())();',
  17107. '$mod.A = new ($mod.GetCreator())();',
  17108. 'var $with = $mod.GetCreator();',
  17109. '$mod.A = new $with();',
  17110. '$mod.A = new $with();',
  17111. '']));
  17112. end;
  17113. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17114. begin
  17115. StartProgram(false);
  17116. Add([
  17117. '{$modeswitch externalclass}',
  17118. 'type',
  17119. ' TExtA = class external name ''ExtA''',
  17120. ' constructor New;',
  17121. ' end;',
  17122. ' TBird = class(TExtA)',
  17123. ' end;',
  17124. 'begin',
  17125. ' TBird.new;',
  17126. '']);
  17127. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17128. ConvertProgram;
  17129. end;
  17130. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17131. begin
  17132. StartProgram(false);
  17133. Add([
  17134. '{$modeswitch externalclass}',
  17135. 'type',
  17136. ' TExtA = class external name ''ExtA''',
  17137. ' constructor New;',
  17138. ' end;',
  17139. ' TBird = class(TExtA)',
  17140. ' end;',
  17141. 'begin',
  17142. ' TBird.new();',
  17143. '']);
  17144. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17145. ConvertProgram;
  17146. end;
  17147. procedure TTestModule.TestExternalClass_NewExtName;
  17148. begin
  17149. StartProgram(false);
  17150. Add([
  17151. '{$modeswitch externalclass}',
  17152. 'type',
  17153. ' TExtA = class external name ''ExtA''',
  17154. ' constructor New; external name ''Other'';',
  17155. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17156. ' end;',
  17157. 'var',
  17158. ' A: texta;',
  17159. 'begin',
  17160. ' a:=texta.new;',
  17161. ' a:=texta(texta.new);',
  17162. ' a:=texta.new();',
  17163. ' a:=texta.new(1);',
  17164. ' with texta do begin',
  17165. ' a:=new;',
  17166. ' a:=new();',
  17167. ' a:=new(2);',
  17168. ' end;',
  17169. ' a:=test1.texta.new;',
  17170. ' a:=test1.texta.new();',
  17171. ' a:=test1.texta.new(3);',
  17172. '']);
  17173. ConvertProgram;
  17174. CheckSource('TestExternalClass_NewExtName',
  17175. LinesToStr([ // statements
  17176. 'this.A = null;',
  17177. '']),
  17178. LinesToStr([ // $mod.$main
  17179. '$mod.A = new Other();',
  17180. '$mod.A = new Other();',
  17181. '$mod.A = new Other();',
  17182. '$mod.A = new A.B(1,2);',
  17183. '$mod.A = new Other();',
  17184. '$mod.A = new Other();',
  17185. '$mod.A = new A.B(2,2);',
  17186. '$mod.A = new Other();',
  17187. '$mod.A = new Other();',
  17188. '$mod.A = new A.B(3,2);',
  17189. '']));
  17190. end;
  17191. procedure TTestModule.TestExternalClass_Constructor;
  17192. begin
  17193. StartProgram(false);
  17194. Add([
  17195. '{$modeswitch externalclass}',
  17196. 'type',
  17197. ' TExtA = class external name ''ExtA''',
  17198. ' constructor Create;',
  17199. ' constructor Create(i: longint; j: longint = 2);',
  17200. ' end;',
  17201. 'var',
  17202. ' A: texta;',
  17203. 'begin',
  17204. ' a:=texta.create;',
  17205. ' a:=texta(texta.create);',
  17206. ' a:=texta.create();',
  17207. ' a:=texta.create(1);',
  17208. ' with texta do begin',
  17209. ' a:=create;',
  17210. ' a:=create();',
  17211. ' a:=create(2);',
  17212. ' end;',
  17213. ' a:=test1.texta.create;',
  17214. ' a:=test1.texta.create();',
  17215. ' a:=test1.texta.create(3);',
  17216. '']);
  17217. ConvertProgram;
  17218. CheckSource('TestExternalClass_Constructor',
  17219. LinesToStr([ // statements
  17220. 'this.A = null;',
  17221. '']),
  17222. LinesToStr([ // $mod.$main
  17223. '$mod.A = new ExtA.Create();',
  17224. '$mod.A = new ExtA.Create();',
  17225. '$mod.A = new ExtA.Create();',
  17226. '$mod.A = new ExtA.Create(1,2);',
  17227. '$mod.A = new ExtA.Create();',
  17228. '$mod.A = new ExtA.Create();',
  17229. '$mod.A = new ExtA.Create(2,2);',
  17230. '$mod.A = new ExtA.Create();',
  17231. '$mod.A = new ExtA.Create();',
  17232. '$mod.A = new ExtA.Create(3,2);',
  17233. '']));
  17234. end;
  17235. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17236. begin
  17237. StartProgram(false);
  17238. Add([
  17239. '{$modeswitch externalclass}',
  17240. 'type',
  17241. ' TExtA = class external name ''ExtA''',
  17242. ' constructor Create; external name ''{}'';',
  17243. ' end;',
  17244. 'var',
  17245. ' A: texta;',
  17246. 'begin',
  17247. ' a:=texta.create;',
  17248. ' a:=texta(texta.create);',
  17249. ' a:=texta.create();',
  17250. ' with texta do begin',
  17251. ' a:=create;',
  17252. ' a:=create();',
  17253. ' end;',
  17254. ' a:=test1.texta.create;',
  17255. ' a:=test1.texta.create();',
  17256. '']);
  17257. ConvertProgram;
  17258. CheckSource('TestExternalClass_ConstructorBrackets',
  17259. LinesToStr([ // statements
  17260. 'this.A = null;',
  17261. '']),
  17262. LinesToStr([ // $mod.$main
  17263. '$mod.A = {};',
  17264. '$mod.A = {};',
  17265. '$mod.A = {};',
  17266. '$mod.A = {};',
  17267. '$mod.A = {};',
  17268. '$mod.A = {};',
  17269. '$mod.A = {};',
  17270. '']));
  17271. end;
  17272. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17273. begin
  17274. StartProgram(false);
  17275. Add('{$modeswitch externalclass}');
  17276. Add('type');
  17277. Add(' TExtA = class external name ''ExtA''');
  17278. Add(' constructor New;');
  17279. Add(' end;');
  17280. Add('function DoIt: longint;');
  17281. Add('const ExtA: longint = 3;');
  17282. Add('begin');
  17283. Add(' Result:=ExtA;');
  17284. Add('end;');
  17285. Add('var');
  17286. Add(' A: texta;');
  17287. Add('begin');
  17288. Add(' a:=texta.new;');
  17289. ConvertProgram;
  17290. CheckSource('TestExternalClass_LocalConstSameName',
  17291. LinesToStr([ // statements
  17292. 'var ExtA$1 = 3;',
  17293. 'this.DoIt = function () {',
  17294. ' var Result = 0;',
  17295. ' Result = ExtA$1;',
  17296. ' return Result;',
  17297. '};',
  17298. 'this.A = null;',
  17299. '']),
  17300. LinesToStr([ // $mod.$main
  17301. '$mod.A = new ExtA();',
  17302. '']));
  17303. end;
  17304. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17305. begin
  17306. StartProgram(false);
  17307. Add('{$modeswitch externalclass}');
  17308. Add('type');
  17309. Add(' TExtA = class external name ''ExtA''');
  17310. Add(' procedure DoIt;');
  17311. Add(' end;');
  17312. Add(' TMyA = class(TExtA)');
  17313. Add(' procedure DoIt;');
  17314. Add(' end;');
  17315. Add('procedure TMyA.DoIt; begin end;');
  17316. Add('begin');
  17317. ConvertProgram;
  17318. CheckSource('TestExternalClass_ReintroduceOverload',
  17319. LinesToStr([ // statements
  17320. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17321. ' this.$init = function () {',
  17322. ' };',
  17323. ' this.$final = function () {',
  17324. ' };',
  17325. ' this.DoIt$1 = function () {',
  17326. ' };',
  17327. '});',
  17328. '']),
  17329. LinesToStr([ // $mod.$main
  17330. '']));
  17331. end;
  17332. procedure TTestModule.TestExternalClass_Inherited;
  17333. begin
  17334. StartProgram(false);
  17335. Add('{$modeswitch externalclass}');
  17336. Add('type');
  17337. Add(' TExtA = class external name ''ExtA''');
  17338. Add(' procedure DoIt(i: longint = 1); virtual;');
  17339. Add(' procedure DoSome(j: longint = 2);');
  17340. Add(' end;');
  17341. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17342. Add(' end;');
  17343. Add(' TMyC = class(TExtB)');
  17344. Add(' procedure DoIt(i: longint = 1); override;');
  17345. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17346. Add(' end;');
  17347. Add('procedure TMyC.DoIt(i: longint);');
  17348. Add('begin');
  17349. Add(' inherited;');
  17350. Add(' inherited DoIt;');
  17351. Add(' inherited DoIt();');
  17352. Add(' inherited DoIt(3);');
  17353. Add(' inherited DoSome;');
  17354. Add(' inherited DoSome();');
  17355. Add(' inherited DoSome(4);');
  17356. Add('end;');
  17357. Add('procedure TMyC.DoSome(j: longint);');
  17358. Add('begin');
  17359. Add(' inherited;');
  17360. Add('end;');
  17361. Add('begin');
  17362. ConvertProgram;
  17363. CheckSource('TestExternalClass_ReintroduceOverload',
  17364. LinesToStr([ // statements
  17365. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17366. ' this.$init = function () {',
  17367. ' };',
  17368. ' this.$final = function () {',
  17369. ' };',
  17370. ' this.DoIt = function (i) {',
  17371. ' ExtB.DoIt.apply(this, arguments);',
  17372. ' ExtB.DoIt.call(this, 1);',
  17373. ' ExtB.DoIt.call(this, 1);',
  17374. ' ExtB.DoIt.call(this, 3);',
  17375. ' ExtB.DoSome.call(this, 2);',
  17376. ' ExtB.DoSome.call(this, 2);',
  17377. ' ExtB.DoSome.call(this, 4);',
  17378. ' };',
  17379. ' this.DoSome$1 = function (j) {',
  17380. ' ExtB.DoSome.apply(this, arguments);',
  17381. ' };',
  17382. '});',
  17383. '']),
  17384. LinesToStr([ // $mod.$main
  17385. '']));
  17386. end;
  17387. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17388. begin
  17389. StartProgram(false);
  17390. Add('{$modeswitch externalclass}');
  17391. Add('type');
  17392. Add(' TObject = class');
  17393. Add(' end;');
  17394. Add(' TExtA = class external name ''ExtA''(TObject)');
  17395. Add(' end;');
  17396. Add('begin');
  17397. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17398. ConvertProgram;
  17399. end;
  17400. procedure TTestModule.TestExternalClass_NewInstance;
  17401. begin
  17402. StartProgram(false);
  17403. Add('{$modeswitch externalclass}');
  17404. Add('type');
  17405. Add(' TExtA = class external name ''ExtA''');
  17406. Add(' end;');
  17407. Add(' TMyB = class(TExtA)');
  17408. Add(' protected');
  17409. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17410. Add(' end;');
  17411. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17412. Add('begin end;');
  17413. Add('begin');
  17414. ConvertProgram;
  17415. CheckSource('TestExternalClass_NewInstance',
  17416. LinesToStr([ // statements
  17417. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17418. ' this.$init = function () {',
  17419. ' };',
  17420. ' this.$final = function () {',
  17421. ' };',
  17422. ' this.NewInstance = function (fnname, paramarray) {',
  17423. ' var Result = null;',
  17424. ' return Result;',
  17425. ' };',
  17426. '});',
  17427. '']),
  17428. LinesToStr([ // $mod.$main
  17429. '']));
  17430. end;
  17431. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17432. begin
  17433. StartProgram(false);
  17434. Add('{$modeswitch externalclass}');
  17435. Add('type');
  17436. Add(' TExtA = class external name ''ExtA''');
  17437. Add(' end;');
  17438. Add(' TMyB = class(TExtA)');
  17439. Add(' protected');
  17440. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17441. Add(' end;');
  17442. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17443. Add('begin end;');
  17444. Add('begin');
  17445. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17446. ConvertProgram;
  17447. end;
  17448. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17449. begin
  17450. StartProgram(false);
  17451. Add('{$modeswitch externalclass}');
  17452. Add('type');
  17453. Add(' TExtA = class external name ''ExtA''');
  17454. Add(' end;');
  17455. Add(' TMyB = class(TExtA)');
  17456. Add(' protected');
  17457. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17458. Add(' end;');
  17459. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17460. Add('begin end;');
  17461. Add('begin');
  17462. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17463. nIncompatibleTypeArgNo);
  17464. ConvertProgram;
  17465. end;
  17466. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17467. begin
  17468. StartProgram(false);
  17469. Add('{$modeswitch externalclass}');
  17470. Add('type');
  17471. Add(' TExtA = class external name ''ExtA''');
  17472. Add(' end;');
  17473. Add(' TMyB = class(TExtA)');
  17474. Add(' protected');
  17475. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17476. Add(' end;');
  17477. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17478. Add('begin end;');
  17479. Add('begin');
  17480. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17481. nIncompatibleTypeArgNo);
  17482. ConvertProgram;
  17483. end;
  17484. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17485. begin
  17486. StartProgram(false);
  17487. Add([
  17488. '{$modeswitch externalclass}',
  17489. 'type',
  17490. ' TJSFunction = class external name ''Function''',
  17491. ' end;',
  17492. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17493. ' constructor New(w: word);',
  17494. ' end;',
  17495. ' TBird = class (TExtA)',
  17496. ' public',
  17497. ' Size: word;',
  17498. ' class var Legs: word;',
  17499. ' constructor Create(a: word);',
  17500. ' end;',
  17501. ' TEagle = class (TBird)',
  17502. ' public',
  17503. ' constructor Create(b: word); reintroduce;',
  17504. ' end;',
  17505. 'constructor TBird.Create(a: word);',
  17506. 'begin',
  17507. ' inherited;', // silently ignored
  17508. ' inherited New(a);', // this.$func(a)
  17509. 'end;',
  17510. 'constructor TEagle.Create(b: word);',
  17511. 'begin',
  17512. ' inherited Create(b);',
  17513. 'end;',
  17514. 'var',
  17515. ' Bird: TBird;',
  17516. ' Eagle: TEagle;',
  17517. 'begin',
  17518. ' Bird:=TBird.Create(3);',
  17519. ' Eagle:=TEagle.Create(4);',
  17520. ' Bird.Size:=Bird.Size+5;',
  17521. ' Bird.Legs:=Bird.Legs+6;',
  17522. ' Eagle.Size:=Eagle.Size+5;',
  17523. ' Eagle.Legs:=Eagle.Legs+6;',
  17524. '']);
  17525. ConvertProgram;
  17526. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17527. LinesToStr([ // statements
  17528. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17529. ' this.Legs = 0;',
  17530. ' this.$init = function () {',
  17531. ' this.Size = 0;',
  17532. ' };',
  17533. ' this.$final = function () {',
  17534. ' };',
  17535. ' this.Create = function (a) {',
  17536. ' this.$ancestorfunc(a);',
  17537. ' return this;',
  17538. ' };',
  17539. '});',
  17540. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17541. ' this.Create$1 = function (b) {',
  17542. ' $mod.TBird.Create.call(this, b);',
  17543. ' return this;',
  17544. ' };',
  17545. '});',
  17546. 'this.Bird = null;',
  17547. 'this.Eagle = null;',
  17548. '']),
  17549. LinesToStr([ // $mod.$main
  17550. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17551. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17552. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17553. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17554. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17555. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17556. '']));
  17557. end;
  17558. procedure TTestModule.TestExternalClass_PascalProperty;
  17559. begin
  17560. StartProgram(false);
  17561. Add('{$modeswitch externalclass}');
  17562. Add('type');
  17563. Add(' TJSElement = class;');
  17564. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17565. Add(' TJSElement = class external name ''ExtA''');
  17566. Add(' end;');
  17567. Add(' TControl = class(TJSElement)');
  17568. Add(' private');
  17569. Add(' FOnClick: TJSNotifyEvent;');
  17570. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17571. Add(' procedure Click(Sender: TJSElement);');
  17572. Add(' end;');
  17573. Add('procedure TControl.Click(Sender: TJSElement);');
  17574. Add('begin');
  17575. Add(' OnClick(Self);');
  17576. Add('end;');
  17577. Add('var');
  17578. Add(' Ctrl: TControl;');
  17579. Add('begin');
  17580. Add(' Ctrl.OnClick:[email protected];');
  17581. Add(' Ctrl.OnClick(Ctrl);');
  17582. ConvertProgram;
  17583. CheckSource('TestExternalClass_PascalProperty',
  17584. LinesToStr([ // statements
  17585. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17586. ' this.$init = function () {',
  17587. ' this.FOnClick = null;',
  17588. ' };',
  17589. ' this.$final = function () {',
  17590. ' this.FOnClick = undefined;',
  17591. ' };',
  17592. ' this.Click = function (Sender) {',
  17593. ' this.FOnClick(this);',
  17594. ' };',
  17595. '});',
  17596. 'this.Ctrl = null;',
  17597. '']),
  17598. LinesToStr([ // $mod.$main
  17599. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17600. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17601. '']));
  17602. end;
  17603. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17604. begin
  17605. StartProgram(false);
  17606. Add([
  17607. '{$modeswitch externalclass}',
  17608. 'type',
  17609. ' IUnknown = interface end;',
  17610. ' TObject = class',
  17611. ' end;',
  17612. ' TChild = class',
  17613. ' end;',
  17614. ' TExtRootA = class external name ''ExtRootA''',
  17615. ' end;',
  17616. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17617. ' end;',
  17618. ' TExtRootB = class external name ''ExtRootB''',
  17619. ' end;',
  17620. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17621. ' end;',
  17622. 'var',
  17623. ' Obj: TObject;',
  17624. ' Child: TChild;',
  17625. ' RootA: TExtRootA;',
  17626. ' ChildA: TExtChildA;',
  17627. ' RootB: TExtRootB;',
  17628. ' ChildB: TExtChildB;',
  17629. ' i: IUnknown;',
  17630. 'begin',
  17631. ' obj:=tobject(roota);',
  17632. ' obj:=tobject(childa);',
  17633. ' child:=tchild(tobject(roota));',
  17634. ' roota:=textroota(obj);',
  17635. ' roota:=textroota(child);',
  17636. ' roota:=textroota(rootb);',
  17637. ' roota:=textroota(childb);',
  17638. ' childa:=textchilda(textroota(obj));',
  17639. ' roota:=TExtRootA(i)',
  17640. '']);
  17641. ConvertProgram;
  17642. CheckSource('TestExternalClass_TypeCastToRootClass',
  17643. LinesToStr([ // statements
  17644. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17645. 'rtl.createClass(this, "TObject", null, function () {',
  17646. ' this.$init = function () {',
  17647. ' };',
  17648. ' this.$final = function () {',
  17649. ' };',
  17650. '});',
  17651. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17652. '});',
  17653. 'this.Obj = null;',
  17654. 'this.Child = null;',
  17655. 'this.RootA = null;',
  17656. 'this.ChildA = null;',
  17657. 'this.RootB = null;',
  17658. 'this.ChildB = null;',
  17659. 'this.i = null;',
  17660. '']),
  17661. LinesToStr([ // $mod.$main
  17662. '$mod.Obj = $mod.RootA;',
  17663. '$mod.Obj = $mod.ChildA;',
  17664. '$mod.Child = $mod.RootA;',
  17665. '$mod.RootA = $mod.Obj;',
  17666. '$mod.RootA = $mod.Child;',
  17667. '$mod.RootA = $mod.RootB;',
  17668. '$mod.RootA = $mod.ChildB;',
  17669. '$mod.ChildA = $mod.Obj;',
  17670. '$mod.RootA = $mod.i;',
  17671. '']));
  17672. end;
  17673. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17674. begin
  17675. StartProgram(false);
  17676. Add([
  17677. '{$modeswitch externalclass}',
  17678. 'type',
  17679. ' IUnknown = interface end;',
  17680. ' IBird = interface(IUnknown) end;',
  17681. ' TClass = class of TObject;',
  17682. ' TObject = class',
  17683. ' end;',
  17684. ' TChild = class',
  17685. ' end;',
  17686. ' TJSObject = class external name ''Object''',
  17687. ' end;',
  17688. ' TRec = record end;',
  17689. 'var',
  17690. ' Obj: TObject;',
  17691. ' Child: TChild;',
  17692. ' i: IUnknown;',
  17693. ' Bird: IBird;',
  17694. ' j: TJSObject;',
  17695. ' r: TRec;',
  17696. ' c: TClass;',
  17697. 'begin',
  17698. ' j:=tjsobject(IUnknown);',
  17699. ' j:=tjsobject(IBird);',
  17700. ' j:=tjsobject(TObject);',
  17701. ' j:=tjsobject(TChild);',
  17702. ' j:=tjsobject(TRec);',
  17703. ' j:=tjsobject(Obj);',
  17704. ' j:=tjsobject(Child);',
  17705. ' j:=tjsobject(i);',
  17706. ' j:=tjsobject(Bird);',
  17707. ' j:=tjsobject(r);',
  17708. ' j:=tjsobject(c);',
  17709. '']);
  17710. ConvertProgram;
  17711. CheckSource('TestExternalClass_TypeCastToJSObject',
  17712. LinesToStr([ // statements
  17713. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17714. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17715. 'rtl.createClass(this, "TObject", null, function () {',
  17716. ' this.$init = function () {',
  17717. ' };',
  17718. ' this.$final = function () {',
  17719. ' };',
  17720. '});',
  17721. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17722. '});',
  17723. 'rtl.recNewT(this, "TRec", function () {',
  17724. ' this.$eq = function (b) {',
  17725. ' return true;',
  17726. ' };',
  17727. ' this.$assign = function (s) {',
  17728. ' return this;',
  17729. ' };',
  17730. '});',
  17731. 'this.Obj = null;',
  17732. 'this.Child = null;',
  17733. 'this.i = null;',
  17734. 'this.Bird = null;',
  17735. 'this.j = null;',
  17736. 'this.r = this.TRec.$new();',
  17737. 'this.c = null;',
  17738. '']),
  17739. LinesToStr([ // $mod.$main
  17740. '$mod.j = $mod.IUnknown;',
  17741. '$mod.j = $mod.IBird;',
  17742. '$mod.j = $mod.TObject;',
  17743. '$mod.j = $mod.TChild;',
  17744. '$mod.j = $mod.TRec;',
  17745. '$mod.j = $mod.Obj;',
  17746. '$mod.j = $mod.Child;',
  17747. '$mod.j = $mod.i;',
  17748. '$mod.j = $mod.Bird;',
  17749. '$mod.j = $mod.r;',
  17750. '$mod.j = $mod.c;',
  17751. '']));
  17752. end;
  17753. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17754. begin
  17755. StartProgram(false);
  17756. Add('{$modeswitch externalclass}');
  17757. Add('type');
  17758. Add(' TJSString = class external name ''String''');
  17759. Add(' class function fromCharCode() : string; varargs;');
  17760. Add(' function anchor(const aName : string) : string;');
  17761. Add(' end;');
  17762. Add('var');
  17763. Add(' s: string;');
  17764. Add('begin');
  17765. Add(' s:=TJSString.fromCharCode(65,66);');
  17766. Add(' s:=TJSString(s).anchor(s);');
  17767. Add(' s:=TJSString(''foo'').anchor(s);');
  17768. ConvertProgram;
  17769. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17770. LinesToStr([ // statements
  17771. 'this.s = "";',
  17772. '']),
  17773. LinesToStr([ // $mod.$main
  17774. '$mod.s = String.fromCharCode(65, 66);',
  17775. '$mod.s = $mod.s.anchor($mod.s);',
  17776. '$mod.s = "foo".anchor($mod.s);',
  17777. '']));
  17778. end;
  17779. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17780. begin
  17781. StartProgram(false);
  17782. Add([
  17783. '{$modeswitch externalclass}',
  17784. 'type',
  17785. ' TJSObject = class external name ''Object'' end;',
  17786. ' TJSFunction = class external name ''Function''',
  17787. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17788. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17789. ' end;',
  17790. ' TObject = class',
  17791. ' procedure DoIt(i: longint);',
  17792. ' end;',
  17793. ' TFuncInt = function(o: TObject): longint;',
  17794. 'function GetIt(o: TObject): longint;',
  17795. ' procedure Sub; begin end;',
  17796. 'var',
  17797. ' f: TJSFunction;',
  17798. ' fi: TFuncInt;',
  17799. 'begin',
  17800. ' fi:=TFuncInt(f);',
  17801. ' f:=TJSFunction(fi);',
  17802. ' f:=TJSFunction(@GetIt);',
  17803. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17804. ' f:=TJSFunction(@Sub);',
  17805. ' f:=TJSFunction(@o.doit);',
  17806. ' f:=TJSFunction(fi).bind(nil,4)',
  17807. 'end;',
  17808. 'procedure TObject.DoIt(i: longint);',
  17809. ' procedure Sub; begin end;',
  17810. 'var f: TJSFunction;',
  17811. 'begin',
  17812. ' f:=TJSFunction(@DoIt);',
  17813. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17814. ' f:=TJSFunction(@Sub);',
  17815. ' f:=TJSFunction(@GetIt);',
  17816. 'end;',
  17817. 'begin']);
  17818. ConvertProgram;
  17819. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17820. LinesToStr([ // statements
  17821. 'rtl.createClass(this, "TObject", null, function () {',
  17822. ' this.$init = function () {',
  17823. ' };',
  17824. ' this.$final = function () {',
  17825. ' };',
  17826. ' this.DoIt = function (i) {',
  17827. ' var $Self = this;',
  17828. ' function Sub() {',
  17829. ' };',
  17830. ' var f = null;',
  17831. ' f = this.DoIt;',
  17832. ' f = this.DoIt.bind(null, 13);',
  17833. ' f = Sub;',
  17834. ' f = $mod.GetIt;',
  17835. ' };',
  17836. '});',
  17837. 'this.GetIt = function (o) {',
  17838. ' var Result = 0;',
  17839. ' function Sub() {',
  17840. ' };',
  17841. ' var f = null;',
  17842. ' var fi = null;',
  17843. ' fi = f;',
  17844. ' f = fi;',
  17845. ' f = $mod.GetIt;',
  17846. ' f = $mod.GetIt.bind(null, 3);',
  17847. ' f = Sub;',
  17848. ' f = $mod.TObject.DoIt;',
  17849. ' f = fi.bind(null, 4);',
  17850. ' return Result;',
  17851. '};',
  17852. '']),
  17853. LinesToStr([ // $mod.$main
  17854. '']));
  17855. end;
  17856. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17857. begin
  17858. StartProgram(false);
  17859. Add([
  17860. '{$mode delphi}',
  17861. '{$modeswitch externalclass}',
  17862. 'type',
  17863. ' TJSObject = class external name ''Object'' end;',
  17864. ' TJSWindow = class external name ''Window''(TJSObject)',
  17865. ' procedure Open;',
  17866. ' end;',
  17867. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17868. ' procedure Execute;',
  17869. ' end;',
  17870. 'procedure Fly;',
  17871. 'var',
  17872. ' w: TJSWindow;',
  17873. ' e: TJSEventTarget;',
  17874. 'begin',
  17875. ' w:=TJSWindow(e);',
  17876. ' e:=TJSEventTarget(w);',
  17877. 'end;',
  17878. 'begin']);
  17879. ConvertProgram;
  17880. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17881. LinesToStr([ // statements
  17882. 'this.Fly = function () {',
  17883. ' var w = null;',
  17884. ' var e = null;',
  17885. ' w = e;',
  17886. ' e = w;',
  17887. '};',
  17888. '']),
  17889. LinesToStr([ // $mod.$main
  17890. '']));
  17891. end;
  17892. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17893. begin
  17894. StartProgram(false);
  17895. Add('{$modeswitch externalclass}');
  17896. Add('type');
  17897. Add(' TJSString = class external name ''String''');
  17898. Add(' class function fromCharCode() : string; varargs;');
  17899. Add(' end;');
  17900. Add('var');
  17901. Add(' s: string;');
  17902. Add(' sObj: TJSString;');
  17903. Add('begin');
  17904. Add(' s:=sObj.fromCharCode(65,66);');
  17905. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17906. nExternalClassInstanceCannotAccessStaticX);
  17907. ConvertProgram;
  17908. end;
  17909. procedure TTestModule.TestExternalClass_BracketAccessor;
  17910. begin
  17911. StartProgram(false);
  17912. Add([
  17913. '{$modeswitch externalclass}',
  17914. 'type',
  17915. ' TJSArray = class external name ''Array2''',
  17916. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17917. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17918. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17919. ' end;',
  17920. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17921. 'begin end;',
  17922. 'var',
  17923. ' Arr: tjsarray;',
  17924. ' s: string;',
  17925. ' i: longint;',
  17926. ' v: jsvalue;',
  17927. 'begin',
  17928. ' v:=arr[0];',
  17929. ' v:=arr.items[1];',
  17930. ' arr[2]:=s;',
  17931. ' arr.items[3]:=s;',
  17932. ' arr[4]:=i;',
  17933. ' arr[5]:=arr[6];',
  17934. ' arr.items[7]:=arr.items[8];',
  17935. ' with arr do items[9]:=items[10];',
  17936. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17937. ' with arr do begin',
  17938. ' v:=GetItems(14);',
  17939. ' setitems(15,16);',
  17940. ' end;',
  17941. ' v:=test1.arr.items[17];',
  17942. ' test1.arr.items[18]:=v;',
  17943. '']);
  17944. ConvertProgram;
  17945. CheckSource('TestExternalClass_BracketAccessor',
  17946. LinesToStr([ // statements
  17947. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17948. '};',
  17949. 'this.Arr = null;',
  17950. 'this.s = "";',
  17951. 'this.i = 0;',
  17952. 'this.v = undefined;',
  17953. '']),
  17954. LinesToStr([ // $mod.$main
  17955. '$mod.v = $mod.Arr[0];',
  17956. '$mod.v = $mod.Arr[1];',
  17957. '$mod.Arr[2] = $mod.s;',
  17958. '$mod.Arr[3] = $mod.s;',
  17959. '$mod.Arr[4] = $mod.i;',
  17960. '$mod.Arr[5] = $mod.Arr[6];',
  17961. '$mod.Arr[7] = $mod.Arr[8];',
  17962. 'var $with = $mod.Arr;',
  17963. '$with[9] = $with[10];',
  17964. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17965. ' a: 9,',
  17966. ' p: $mod.Arr,',
  17967. ' get: function () {',
  17968. ' return this.p[this.a];',
  17969. ' },',
  17970. ' set: function (v) {',
  17971. ' this.p[this.a] = v;',
  17972. ' }',
  17973. '}, {',
  17974. ' a: 10,',
  17975. ' p: $mod.Arr,',
  17976. ' get: function () {',
  17977. ' return this.p[this.a];',
  17978. ' },',
  17979. ' set: function (v) {',
  17980. ' this.p[this.a] = v;',
  17981. ' }',
  17982. '});',
  17983. 'var $with1 = $mod.Arr;',
  17984. '$mod.v = $with1[14];',
  17985. '$with1[15] = 16;',
  17986. '$mod.v = $mod.Arr[17];',
  17987. '$mod.Arr[18] = $mod.v;',
  17988. '']));
  17989. end;
  17990. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17991. begin
  17992. StartProgram(false);
  17993. Add([
  17994. '{$modeswitch externalclass}',
  17995. 'type',
  17996. ' TJSArray = class external name ''Array2''',
  17997. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17998. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17999. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18000. ' end;',
  18001. ' TMyArr = class(TJSArray)',
  18002. ' procedure DoIt;',
  18003. ' end;',
  18004. 'procedure tmyarr.DoIt;',
  18005. 'begin',
  18006. ' Items[1]:=Items[2];',
  18007. ' SetItems(3,getItems(4));',
  18008. 'end;',
  18009. 'var',
  18010. ' Arr: tmyarr;',
  18011. ' s: string;',
  18012. ' i: longint;',
  18013. ' v: jsvalue;',
  18014. 'begin',
  18015. ' v:=arr[0];',
  18016. ' v:=arr.items[1];',
  18017. ' arr[2]:=s;',
  18018. ' arr.items[3]:=s;',
  18019. ' arr[4]:=i;',
  18020. ' arr[5]:=arr[6];',
  18021. ' arr.items[7]:=arr.items[8];',
  18022. ' with arr do items[9]:=items[10];',
  18023. ' with arr do begin',
  18024. ' v:=GetItems(14);',
  18025. ' setitems(15,16);',
  18026. ' end;',
  18027. '']);
  18028. ConvertProgram;
  18029. CheckSource('TestExternalClass_BracketAccessor_Call',
  18030. LinesToStr([ // statements
  18031. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18032. ' this.$init = function () {',
  18033. ' };',
  18034. ' this.$final = function () {',
  18035. ' };',
  18036. ' this.DoIt = function () {',
  18037. ' this[1] = this[2];',
  18038. ' this[3] = this[4];',
  18039. ' };',
  18040. '});',
  18041. 'this.Arr = null;',
  18042. 'this.s = "";',
  18043. 'this.i = 0;',
  18044. 'this.v = undefined;',
  18045. '']),
  18046. LinesToStr([ // $mod.$main
  18047. '$mod.v = $mod.Arr[0];',
  18048. '$mod.v = $mod.Arr[1];',
  18049. '$mod.Arr[2] = $mod.s;',
  18050. '$mod.Arr[3] = $mod.s;',
  18051. '$mod.Arr[4] = $mod.i;',
  18052. '$mod.Arr[5] = $mod.Arr[6];',
  18053. '$mod.Arr[7] = $mod.Arr[8];',
  18054. 'var $with = $mod.Arr;',
  18055. '$with[9] = $with[10];',
  18056. 'var $with1 = $mod.Arr;',
  18057. '$mod.v = $with1[14];',
  18058. '$with1[15] = 16;',
  18059. '']));
  18060. end;
  18061. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18062. begin
  18063. StartProgram(false);
  18064. Add('{$modeswitch externalclass}');
  18065. Add('type');
  18066. Add(' TJSArray = class external name ''Array2''');
  18067. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18068. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18069. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18070. Add(' end;');
  18071. Add('begin');
  18072. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18073. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18074. ConvertProgram;
  18075. end;
  18076. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18077. begin
  18078. StartProgram(false);
  18079. Add('{$modeswitch externalclass}');
  18080. Add('type');
  18081. Add(' TJSArray = class external name ''Array2''');
  18082. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18083. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18084. Add(' end;');
  18085. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18086. Add('begin end;');
  18087. Add('var');
  18088. Add(' Arr: tjsarray;');
  18089. Add(' v: jsvalue;');
  18090. Add('begin');
  18091. Add(' v:=arr[0];');
  18092. Add(' v:=arr.items[1];');
  18093. Add(' with arr do v:=items[2];');
  18094. Add(' doit(arr[3],arr[4]);');
  18095. ConvertProgram;
  18096. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18097. LinesToStr([ // statements
  18098. 'this.DoIt = function (vI, vJ) {',
  18099. '};',
  18100. 'this.Arr = null;',
  18101. 'this.v = undefined;',
  18102. '']),
  18103. LinesToStr([ // $mod.$main
  18104. '$mod.v = $mod.Arr[0];',
  18105. '$mod.v = $mod.Arr[1];',
  18106. 'var $with = $mod.Arr;',
  18107. '$mod.v = $with[2];',
  18108. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18109. '']));
  18110. end;
  18111. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18112. begin
  18113. StartProgram(false);
  18114. Add('{$modeswitch externalclass}');
  18115. Add('type');
  18116. Add(' TJSArray = class external name ''Array2''');
  18117. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18118. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18119. Add(' end;');
  18120. Add('var');
  18121. Add(' Arr: tjsarray;');
  18122. Add(' s: string;');
  18123. Add(' i: longint;');
  18124. Add(' v: jsvalue;');
  18125. Add('begin');
  18126. Add(' arr[2]:=s;');
  18127. Add(' arr.items[3]:=s;');
  18128. Add(' arr[4]:=i;');
  18129. Add(' with arr do items[5]:=i;');
  18130. ConvertProgram;
  18131. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18132. LinesToStr([ // statements
  18133. 'this.Arr = null;',
  18134. 'this.s = "";',
  18135. 'this.i = 0;',
  18136. 'this.v = undefined;',
  18137. '']),
  18138. LinesToStr([ // $mod.$main
  18139. '$mod.Arr[2] = $mod.s;',
  18140. '$mod.Arr[3] = $mod.s;',
  18141. '$mod.Arr[4] = $mod.i;',
  18142. 'var $with = $mod.Arr;',
  18143. '$with[5] = $mod.i;',
  18144. '']));
  18145. end;
  18146. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18147. begin
  18148. StartProgram(false);
  18149. Add('{$modeswitch externalclass}');
  18150. Add('type');
  18151. Add(' TJSArray = class external name ''Array2''');
  18152. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18153. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18154. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18155. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18156. Add(' end;');
  18157. Add('var');
  18158. Add(' Arr: tjsarray;');
  18159. Add(' s: string;');
  18160. Add(' i: longint;');
  18161. Add(' v: jsvalue;');
  18162. Add('begin');
  18163. Add(' arr[2]:=s;');
  18164. Add(' arr.items[3]:=s;');
  18165. Add(' arr.numbers[4]:=i;');
  18166. Add(' with arr do items[5]:=i;');
  18167. Add(' with arr do numbers[6]:=i;');
  18168. ConvertProgram;
  18169. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18170. LinesToStr([ // statements
  18171. 'this.Arr = null;',
  18172. 'this.s = "";',
  18173. 'this.i = 0;',
  18174. 'this.v = undefined;',
  18175. '']),
  18176. LinesToStr([ // $mod.$main
  18177. '$mod.Arr[2] = $mod.s;',
  18178. '$mod.Arr[3] = $mod.s;',
  18179. '$mod.Arr[4] = $mod.i;',
  18180. 'var $with = $mod.Arr;',
  18181. '$with[5] = $mod.i;',
  18182. 'var $with1 = $mod.Arr;',
  18183. '$with1[6] = $mod.i;',
  18184. '']));
  18185. end;
  18186. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18187. begin
  18188. StartProgram(false);
  18189. Add('{$modeswitch externalclass}');
  18190. Add('type');
  18191. Add(' TJSArray = class external name ''Array2''');
  18192. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18193. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18194. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18195. Add(' end;');
  18196. Add('var');
  18197. Add(' Arr: tjsarray;');
  18198. Add(' i: longint;');
  18199. Add(' IntArr: array of longint;');
  18200. Add(' v: jsvalue;');
  18201. Add('begin');
  18202. Add(' v:=arr.items[i];');
  18203. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18204. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18205. ConvertProgram;
  18206. CheckSource('TestExternalClass_BracketAccessor_Index',
  18207. LinesToStr([ // statements
  18208. 'this.Arr = null;',
  18209. 'this.i = 0;',
  18210. 'this.IntArr = [];',
  18211. 'this.v = undefined;',
  18212. '']),
  18213. LinesToStr([ // $mod.$main
  18214. '$mod.v = $mod.Arr[$mod.i];',
  18215. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18216. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18217. '']));
  18218. end;
  18219. procedure TTestModule.TestExternalClass_ForInJSObject;
  18220. begin
  18221. StartProgram(false);
  18222. Add([
  18223. '{$modeswitch externalclass}',
  18224. 'type',
  18225. ' TJSObject = class external name ''Object''',
  18226. ' end;',
  18227. 'var',
  18228. ' o: TJSObject;',
  18229. ' key: string;',
  18230. 'begin',
  18231. ' for key in o do',
  18232. ' if key=''abc'' then ;',
  18233. '']);
  18234. ConvertProgram;
  18235. CheckSource('TestExternalClass_ForInJSObject',
  18236. LinesToStr([ // statements
  18237. 'this.o = null;',
  18238. 'this.key = "";',
  18239. '']),
  18240. LinesToStr([ // $mod.$main
  18241. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18242. '']));
  18243. end;
  18244. procedure TTestModule.TestExternalClass_ForInJSArray;
  18245. begin
  18246. StartProgram(false);
  18247. Add([
  18248. '{$modeswitch externalclass}',
  18249. 'type',
  18250. ' TJSInt8Array = class external name ''Int8Array''',
  18251. ' private',
  18252. ' flength: NativeInt external name ''length'';',
  18253. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18254. ' public',
  18255. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18256. ' property Length: NativeInt read flength;',
  18257. ' end;',
  18258. 'var',
  18259. ' a: TJSInt8Array;',
  18260. ' value: shortint;',
  18261. 'begin',
  18262. ' for value in a do',
  18263. ' if value=3 then ;',
  18264. '']);
  18265. ConvertProgram;
  18266. CheckSource('TestExternalClass_ForInJSArray',
  18267. LinesToStr([ // statements
  18268. 'this.a = null;',
  18269. 'this.value = 0;',
  18270. '']),
  18271. LinesToStr([ // $mod.$main
  18272. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18273. ' $mod.value = $in[$l];',
  18274. ' if ($mod.value === 3) ;',
  18275. '};',
  18276. '']));
  18277. end;
  18278. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18279. begin
  18280. AddModuleWithIntfImplSrc('unit2.pas',
  18281. LinesToStr([
  18282. '{$modeswitch externalclass}',
  18283. 'type',
  18284. ' TJSBufferSource = class external name ''BufferSource''',
  18285. ' end;',
  18286. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18287. '']),
  18288. '');
  18289. AddModuleWithIntfImplSrc('unit3.pas',
  18290. LinesToStr([
  18291. '{$modeswitch externalclass}',
  18292. 'type',
  18293. ' TJSBufferSource = class external name ''BufferSource''',
  18294. ' end;',
  18295. '']),
  18296. '');
  18297. StartUnit(true);
  18298. Add([
  18299. 'interface',
  18300. 'uses unit2, unit3;',
  18301. 'procedure DoSome(s: TJSBufferSource);',
  18302. 'implementation',
  18303. 'procedure DoSome(s: TJSBufferSource);',
  18304. 'begin',
  18305. ' DoIt(s);',
  18306. 'end;',
  18307. '']);
  18308. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18309. nIncompatibleTypeArgNo);
  18310. ConvertUnit;
  18311. end;
  18312. procedure TTestModule.TestClassInterface_Corba;
  18313. begin
  18314. StartProgram(false);
  18315. Add([
  18316. '{$interfaces corba}',
  18317. 'type',
  18318. ' IUnknown = interface;',
  18319. ' IUnknown = interface',
  18320. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18321. ' end;',
  18322. ' IInterface = IUnknown;',
  18323. ' IBird = interface(IInterface)',
  18324. ' function GetSize: longint;',
  18325. ' procedure SetSize(i: longint);',
  18326. ' property Size: longint read GetSize write SetSize;',
  18327. ' procedure DoIt(i: longint);',
  18328. ' end;',
  18329. ' TObject = class',
  18330. ' end;',
  18331. ' TBird = class(TObject,IBird)',
  18332. ' function GetSize: longint; virtual; abstract;',
  18333. ' procedure SetSize(i: longint); virtual; abstract;',
  18334. ' procedure DoIt(i: longint); virtual; abstract;',
  18335. ' end;',
  18336. 'var',
  18337. ' BirdIntf: IBird;',
  18338. 'begin',
  18339. ' BirdIntf.Size:=BirdIntf.Size;',
  18340. '']);
  18341. ConvertProgram;
  18342. CheckSource('TestClassInterface_Corba',
  18343. LinesToStr([ // statements
  18344. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18345. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18346. 'rtl.createClass(this, "TObject", null, function () {',
  18347. ' this.$init = function () {',
  18348. ' };',
  18349. ' this.$final = function () {',
  18350. ' };',
  18351. '});',
  18352. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18353. ' rtl.addIntf(this, $mod.IBird);',
  18354. '});',
  18355. 'this.BirdIntf = null;',
  18356. '']),
  18357. LinesToStr([ // $mod.$main
  18358. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18359. '']));
  18360. end;
  18361. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18362. begin
  18363. StartProgram(false);
  18364. Add([
  18365. '{$interfaces corba}',
  18366. 'type',
  18367. ' IUnknown = interface',
  18368. ' procedure DoIt; external name ''foo'';',
  18369. ' end;',
  18370. 'begin']);
  18371. SetExpectedParserError(
  18372. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18373. nParserNoFieldsAllowed);
  18374. ConvertProgram;
  18375. end;
  18376. procedure TTestModule.TestClassInterface_Overloads;
  18377. begin
  18378. StartProgram(false);
  18379. Add([
  18380. '{$interfaces corba}',
  18381. 'type',
  18382. ' integer = longint;',
  18383. ' IUnknown = interface',
  18384. ' procedure DoIt(i: integer);',
  18385. ' procedure DoIt(s: string);',
  18386. ' end;',
  18387. ' IBird = interface(IUnknown)',
  18388. ' procedure DoIt(b: boolean); overload;',
  18389. ' end;',
  18390. ' TObject = class',
  18391. ' end;',
  18392. ' TBird = class(TObject,IBird)',
  18393. ' procedure DoIt(o: TObject);',
  18394. ' procedure DoIt(s: string);',
  18395. ' procedure DoIt(i: integer);',
  18396. ' procedure DoIt(b: boolean);',
  18397. ' end;',
  18398. 'procedure TBird.DoIt(o: TObject); begin end;',
  18399. 'procedure TBird.DoIt(s: string); begin end;',
  18400. 'procedure TBird.DoIt(i: integer); begin end;',
  18401. 'procedure TBird.DoIt(b: boolean); begin end;',
  18402. 'var',
  18403. ' BirdIntf: IBird;',
  18404. 'begin',
  18405. ' BirdIntf.DoIt(3);',
  18406. ' BirdIntf.DoIt(''abc'');',
  18407. ' BirdIntf.DoIt(true);',
  18408. '']);
  18409. ConvertProgram;
  18410. CheckSource('TestClassInterface_Overloads',
  18411. LinesToStr([ // statements
  18412. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18413. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18414. 'rtl.createClass(this, "TObject", null, function () {',
  18415. ' this.$init = function () {',
  18416. ' };',
  18417. ' this.$final = function () {',
  18418. ' };',
  18419. '});',
  18420. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18421. ' this.DoIt = function (o) {',
  18422. ' };',
  18423. ' this.DoIt$1 = function (s) {',
  18424. ' };',
  18425. ' this.DoIt$2 = function (i) {',
  18426. ' };',
  18427. ' this.DoIt$3 = function (b) {',
  18428. ' };',
  18429. ' rtl.addIntf(this, $mod.IBird, {',
  18430. ' DoIt$2: "DoIt$3",',
  18431. ' DoIt: "DoIt$2"',
  18432. ' });',
  18433. '});',
  18434. 'this.BirdIntf = null;',
  18435. '']),
  18436. LinesToStr([ // $mod.$main
  18437. '$mod.BirdIntf.DoIt(3);',
  18438. '$mod.BirdIntf.DoIt$1("abc");',
  18439. '$mod.BirdIntf.DoIt$2(true);',
  18440. '']));
  18441. end;
  18442. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18443. begin
  18444. StartProgram(false);
  18445. Add([
  18446. '{$interfaces corba}',
  18447. 'type',
  18448. ' IBird = interface',
  18449. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18450. ' end;',
  18451. ' IDog = interface',
  18452. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18453. ' end;',
  18454. ' TObject = class(IBird,IDog)',
  18455. ' end;',
  18456. 'begin']);
  18457. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18458. nDuplicateGUIDXInYZ);
  18459. ConvertProgram;
  18460. end;
  18461. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18462. begin
  18463. StartProgram(false);
  18464. Add([
  18465. '{$interfaces corba}',
  18466. 'type',
  18467. ' IAnimal = interface',
  18468. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18469. ' end;',
  18470. ' IBird = interface(IAnimal)',
  18471. ' end;',
  18472. ' IHawk = interface(IBird)',
  18473. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18474. ' end;',
  18475. 'begin']);
  18476. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18477. nDuplicateGUIDXInYZ);
  18478. ConvertProgram;
  18479. end;
  18480. procedure TTestModule.TestClassInterface_AncestorImpl;
  18481. begin
  18482. StartProgram(false);
  18483. Add([
  18484. '{$interfaces corba}',
  18485. 'type',
  18486. ' integer = longint;',
  18487. ' IUnknown = interface',
  18488. ' procedure DoIt(i: integer);',
  18489. ' end;',
  18490. ' IBird = interface',
  18491. ' procedure Fly(i: integer);',
  18492. ' end;',
  18493. ' TObject = class(IUnknown)',
  18494. ' procedure DoIt(i: integer);',
  18495. ' end;',
  18496. ' TBird = class(IBird)',
  18497. ' procedure Fly(i: integer);',
  18498. ' end;',
  18499. 'procedure TObject.DoIt(i: integer); begin end;',
  18500. 'procedure TBird.Fly(i: integer); begin end;',
  18501. 'begin',
  18502. '']);
  18503. ConvertProgram;
  18504. CheckSource('TestClassInterface_AncestorIntf',
  18505. LinesToStr([ // statements
  18506. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18507. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18508. 'rtl.createClass(this, "TObject", null, function () {',
  18509. ' this.$init = function () {',
  18510. ' };',
  18511. ' this.$final = function () {',
  18512. ' };',
  18513. ' this.DoIt = function (i) {',
  18514. ' };',
  18515. ' rtl.addIntf(this, $mod.IUnknown);',
  18516. '});',
  18517. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18518. ' this.Fly = function (i) {',
  18519. ' };',
  18520. ' rtl.addIntf(this, $mod.IBird);',
  18521. ' rtl.addIntf(this, $mod.IUnknown);',
  18522. '});',
  18523. '']),
  18524. LinesToStr([ // $mod.$main
  18525. '']));
  18526. end;
  18527. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18528. begin
  18529. StartProgram(false);
  18530. Add([
  18531. '{$interfaces corba}',
  18532. 'type',
  18533. ' integer = longint;',
  18534. ' IBird = interface',
  18535. ' procedure DoIt(i: integer);',
  18536. ' end;',
  18537. ' TObject = class',
  18538. ' procedure DoIt(i: integer);',
  18539. ' end;',
  18540. ' TBird = class(IBird)',
  18541. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18542. ' end;',
  18543. 'procedure TObject.DoIt(i: integer); begin end;',
  18544. 'procedure TBird.DoIt(i: integer); begin end;',
  18545. 'begin',
  18546. '']);
  18547. ConvertProgram;
  18548. CheckSource('TestClassInterface_ImplReintroduce',
  18549. LinesToStr([ // statements
  18550. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18551. 'rtl.createClass(this, "TObject", null, function () {',
  18552. ' this.$init = function () {',
  18553. ' };',
  18554. ' this.$final = function () {',
  18555. ' };',
  18556. ' this.DoIt = function (i) {',
  18557. ' };',
  18558. '});',
  18559. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18560. ' this.DoIt$1 = function (i) {',
  18561. ' };',
  18562. ' rtl.addIntf(this, $mod.IBird, {',
  18563. ' DoIt: "DoIt$1"',
  18564. ' });',
  18565. '});',
  18566. '']),
  18567. LinesToStr([ // $mod.$main
  18568. '']));
  18569. end;
  18570. procedure TTestModule.TestClassInterface_MethodResolution;
  18571. begin
  18572. StartProgram(false);
  18573. Add([
  18574. '{$interfaces corba}',
  18575. 'type',
  18576. ' IUnknown = interface',
  18577. ' procedure Walk(i: longint);',
  18578. ' end;',
  18579. ' IBird = interface(IUnknown)',
  18580. ' procedure Walk(b: boolean); overload;',
  18581. ' procedure Fly(s: string);',
  18582. ' end;',
  18583. ' TObject = class',
  18584. ' end;',
  18585. ' TBird = class(TObject,IBird)',
  18586. ' procedure IBird.Fly = Move;',
  18587. ' procedure IBird.Walk = Hop;',
  18588. ' procedure Hop(i: longint);',
  18589. ' procedure Move(s: string);',
  18590. ' procedure Hop(b: boolean);',
  18591. ' end;',
  18592. 'procedure TBird.Move(s: string); begin end;',
  18593. 'procedure TBird.Hop(i: longint); begin end;',
  18594. 'procedure TBird.Hop(b: boolean); begin end;',
  18595. 'var',
  18596. ' BirdIntf: IBird;',
  18597. 'begin',
  18598. ' BirdIntf.Walk(3);',
  18599. ' BirdIntf.Walk(true);',
  18600. ' BirdIntf.Fly(''abc'');',
  18601. '']);
  18602. ConvertProgram;
  18603. CheckSource('TestClassInterface_MethodResolution',
  18604. LinesToStr([ // statements
  18605. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18606. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18607. 'rtl.createClass(this, "TObject", null, function () {',
  18608. ' this.$init = function () {',
  18609. ' };',
  18610. ' this.$final = function () {',
  18611. ' };',
  18612. '});',
  18613. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18614. ' this.Hop = function (i) {',
  18615. ' };',
  18616. ' this.Move = function (s) {',
  18617. ' };',
  18618. ' this.Hop$1 = function (b) {',
  18619. ' };',
  18620. ' rtl.addIntf(this, $mod.IBird, {',
  18621. ' Walk$1: "Hop$1",',
  18622. ' Fly: "Move",',
  18623. ' Walk: "Hop"',
  18624. ' });',
  18625. '});',
  18626. 'this.BirdIntf = null;',
  18627. '']),
  18628. LinesToStr([ // $mod.$main
  18629. '$mod.BirdIntf.Walk(3);',
  18630. '$mod.BirdIntf.Walk$1(true);',
  18631. '$mod.BirdIntf.Fly("abc");',
  18632. '']));
  18633. end;
  18634. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18635. begin
  18636. StartProgram(false);
  18637. Add([
  18638. '{$interfaces com}',
  18639. 'type',
  18640. ' IUnknown = interface',
  18641. ' function _AddRef: longint;',
  18642. ' procedure Walk;',
  18643. ' end;',
  18644. ' IBird = interface end;',
  18645. ' IDog = interface end;',
  18646. ' TObject = class(IBird,IDog)',
  18647. ' function _AddRef: longint; virtual; abstract;',
  18648. ' procedure Walk; virtual; abstract;',
  18649. ' end;',
  18650. ' TBird = class(IUnknown)',
  18651. ' end;',
  18652. 'begin',
  18653. '']);
  18654. ConvertProgram;
  18655. CheckSource('TestClassInterface_COM_AncestorLess',
  18656. LinesToStr([ // statements
  18657. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18658. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18659. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18660. 'rtl.createClass(this, "TObject", null, function () {',
  18661. ' this.$init = function () {',
  18662. ' };',
  18663. ' this.$final = function () {',
  18664. ' };',
  18665. ' rtl.addIntf(this, $mod.IBird);',
  18666. ' rtl.addIntf(this, $mod.IDog);',
  18667. '});',
  18668. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18669. ' rtl.addIntf(this, $mod.IUnknown);',
  18670. ' rtl.addIntf(this, $mod.IBird);',
  18671. ' rtl.addIntf(this, $mod.IDog);',
  18672. '});',
  18673. '']),
  18674. LinesToStr([ // $mod.$main
  18675. '']));
  18676. end;
  18677. procedure TTestModule.TestClassInterface_MethodOverride;
  18678. begin
  18679. StartProgram(false);
  18680. Add([
  18681. '{$interfaces corba}',
  18682. 'type',
  18683. ' IUnknown = interface',
  18684. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18685. ' procedure Go;',
  18686. ' end;',
  18687. ' TObject = class(IUnknown)',
  18688. ' procedure Go; virtual; abstract;',
  18689. ' end;',
  18690. ' TBird = class',
  18691. ' procedure Go; override;',
  18692. ' end;',
  18693. ' TCat = class(TObject)',
  18694. ' procedure Go; override;',
  18695. ' end;',
  18696. ' TDog = class(TObject, IUnknown)',
  18697. ' procedure Go; override;',
  18698. ' end;',
  18699. 'procedure TBird.Go; begin end;',
  18700. 'procedure TCat.Go; begin end;',
  18701. 'procedure TDog.Go; begin end;',
  18702. 'begin',
  18703. '']);
  18704. ConvertProgram;
  18705. CheckSource('TestClassInterface_MethodOverride',
  18706. LinesToStr([ // statements
  18707. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18708. 'rtl.createClass(this, "TObject", null, function () {',
  18709. ' this.$init = function () {',
  18710. ' };',
  18711. ' this.$final = function () {',
  18712. ' };',
  18713. ' rtl.addIntf(this, $mod.IUnknown);',
  18714. '});',
  18715. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18716. ' this.Go = function () {',
  18717. ' };',
  18718. ' rtl.addIntf(this, $mod.IUnknown);',
  18719. '});',
  18720. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18721. ' this.Go = function () {',
  18722. ' };',
  18723. ' rtl.addIntf(this, $mod.IUnknown);',
  18724. '});',
  18725. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18726. ' this.Go = function () {',
  18727. ' };',
  18728. ' rtl.addIntf(this, $mod.IUnknown);',
  18729. '});',
  18730. '']),
  18731. LinesToStr([ // $mod.$main
  18732. '']));
  18733. end;
  18734. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18735. begin
  18736. StartProgram(false);
  18737. Add([
  18738. '{$interfaces corba}',
  18739. 'type',
  18740. ' IUnknown = interface',
  18741. ' end;',
  18742. ' IBird = interface(IUnknown)',
  18743. ' procedure Fly(s: string);',
  18744. ' end;',
  18745. ' IEagle = interface(IBird)',
  18746. ' end;',
  18747. ' IDove = interface(IBird)',
  18748. ' end;',
  18749. ' ISwallow = interface(IBird)',
  18750. ' end;',
  18751. ' TObject = class',
  18752. ' end;',
  18753. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18754. ' procedure Fly(s: string); virtual; abstract;',
  18755. ' end;',
  18756. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18757. ' FBirdIntf: IBird;',
  18758. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18759. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18760. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18761. ' FDoveObj: TBird;',
  18762. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18763. ' function GetSwallowObj: TBird; virtual; abstract;',
  18764. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18765. ' end;',
  18766. 'begin',
  18767. '']);
  18768. ConvertProgram;
  18769. CheckSource('TestClassInterface_Corba_Delegation',
  18770. LinesToStr([ // statements
  18771. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18772. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18773. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18774. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18775. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18776. 'rtl.createClass(this, "TObject", null, function () {',
  18777. ' this.$init = function () {',
  18778. ' };',
  18779. ' this.$final = function () {',
  18780. ' };',
  18781. '});',
  18782. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18783. ' rtl.addIntf(this, $mod.IBird);',
  18784. ' rtl.addIntf(this, $mod.IEagle);',
  18785. ' rtl.addIntf(this, $mod.IDove);',
  18786. ' rtl.addIntf(this, $mod.ISwallow);',
  18787. '});',
  18788. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18789. ' this.$init = function () {',
  18790. ' $mod.TObject.$init.call(this);',
  18791. ' this.FBirdIntf = null;',
  18792. ' this.FDoveObj = null;',
  18793. ' };',
  18794. ' this.$final = function () {',
  18795. ' this.FBirdIntf = undefined;',
  18796. ' this.FDoveObj = undefined;',
  18797. ' $mod.TObject.$final.call(this);',
  18798. ' };',
  18799. ' this.$intfmaps = {',
  18800. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18801. ' return this.FBirdIntf;',
  18802. ' },',
  18803. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18804. ' return this.GetEagleIntf();',
  18805. ' },',
  18806. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18807. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18808. ' },',
  18809. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18810. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18811. ' }',
  18812. ' };',
  18813. '});',
  18814. '']),
  18815. LinesToStr([ // $mod.$main
  18816. '']));
  18817. end;
  18818. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18819. begin
  18820. StartProgram(false);
  18821. Add([
  18822. '{$interfaces corba}',
  18823. 'type',
  18824. ' IUnknown = interface',
  18825. ' end;',
  18826. ' IBird = interface(IUnknown)',
  18827. ' procedure Fly(s: string);',
  18828. ' end;',
  18829. ' IEagle = interface(IBird)',
  18830. ' end;',
  18831. ' IDove = interface(IBird)',
  18832. ' end;',
  18833. ' ISwallow = interface(IBird)',
  18834. ' end;',
  18835. ' TObject = class',
  18836. ' end;',
  18837. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18838. ' procedure Fly(s: string); virtual; abstract;',
  18839. ' end;',
  18840. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18841. ' private',
  18842. ' class var FBirdIntf: IBird;',
  18843. ' class var FDoveObj: TBird;',
  18844. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18845. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18846. ' protected',
  18847. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18848. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18849. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18850. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18851. ' end;',
  18852. 'begin',
  18853. '']);
  18854. ConvertProgram;
  18855. CheckSource('TestClassInterface_Corba_DelegationStatic',
  18856. LinesToStr([ // statements
  18857. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18858. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18859. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18860. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18861. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18862. 'rtl.createClass(this, "TObject", null, function () {',
  18863. ' this.$init = function () {',
  18864. ' };',
  18865. ' this.$final = function () {',
  18866. ' };',
  18867. '});',
  18868. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18869. ' rtl.addIntf(this, $mod.IBird);',
  18870. ' rtl.addIntf(this, $mod.IEagle);',
  18871. ' rtl.addIntf(this, $mod.IDove);',
  18872. ' rtl.addIntf(this, $mod.ISwallow);',
  18873. '});',
  18874. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18875. ' this.FBirdIntf = null;',
  18876. ' this.FDoveObj = null;',
  18877. ' this.$intfmaps = {',
  18878. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18879. ' return this.FBirdIntf;',
  18880. ' },',
  18881. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18882. ' return this.GetEagleIntf();',
  18883. ' },',
  18884. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18885. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18886. ' },',
  18887. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18888. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18889. ' }',
  18890. ' };',
  18891. '});',
  18892. '']),
  18893. LinesToStr([ // $mod.$main
  18894. '']));
  18895. end;
  18896. procedure TTestModule.TestClassInterface_Corba_Operators;
  18897. begin
  18898. StartProgram(false);
  18899. Add([
  18900. '{$interfaces corba}',
  18901. 'type',
  18902. ' IUnknown = interface',
  18903. ' end;',
  18904. ' IBird = interface(IUnknown)',
  18905. ' function GetItems(Index: longint): longint;',
  18906. ' procedure SetItems(Index: longint; Value: longint);',
  18907. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18908. ' end;',
  18909. ' TObject = class',
  18910. ' end;',
  18911. ' TBird = class(TObject,IBird)',
  18912. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18913. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18914. ' end;',
  18915. 'var',
  18916. ' IntfVar: IBird = nil;',
  18917. ' IntfVar2: IBird;',
  18918. ' ObjVar: TBird;',
  18919. ' v: JSValue;',
  18920. 'begin',
  18921. ' IntfVar:=nil;',
  18922. ' IntfVar[3]:=IntfVar[4];',
  18923. ' if Assigned(IntfVar) then ;',
  18924. ' IntfVar:=IntfVar2;',
  18925. ' IntfVar:=ObjVar;',
  18926. ' if IntfVar=IntfVar2 then ;',
  18927. ' if IntfVar<>IntfVar2 then ;',
  18928. ' if IntfVar is IBird then ;',
  18929. ' if IntfVar is TBird then ;',
  18930. ' if ObjVar is IBird then ;',
  18931. ' IntfVar:=IntfVar2 as IBird;',
  18932. ' ObjVar:=IntfVar2 as TBird;',
  18933. ' IntfVar:=ObjVar as IBird;',
  18934. ' IntfVar:=IBird(IntfVar2);',
  18935. ' ObjVar:=TBird(IntfVar);',
  18936. ' IntfVar:=IBird(ObjVar);',
  18937. ' v:=IntfVar;',
  18938. ' IntfVar:=IBird(v);',
  18939. ' if v is IBird then ;',
  18940. ' v:=JSValue(IntfVar);',
  18941. ' v:=IBird;',
  18942. '']);
  18943. ConvertProgram;
  18944. CheckSource('TestClassInterface_Corba_Operators',
  18945. LinesToStr([ // statements
  18946. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18947. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  18948. 'rtl.createClass(this, "TObject", null, function () {',
  18949. ' this.$init = function () {',
  18950. ' };',
  18951. ' this.$final = function () {',
  18952. ' };',
  18953. '});',
  18954. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18955. ' rtl.addIntf(this, $mod.IBird);',
  18956. '});',
  18957. 'this.IntfVar = null;',
  18958. 'this.IntfVar2 = null;',
  18959. 'this.ObjVar = null;',
  18960. 'this.v = undefined;',
  18961. '']),
  18962. LinesToStr([ // $mod.$main
  18963. '$mod.IntfVar = null;',
  18964. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18965. 'if ($mod.IntfVar != null) ;',
  18966. '$mod.IntfVar = $mod.IntfVar2;',
  18967. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18968. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18969. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18970. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18971. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18972. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18973. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18974. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18975. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18976. '$mod.IntfVar = $mod.IntfVar2;',
  18977. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18978. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18979. '$mod.v = $mod.IntfVar;',
  18980. '$mod.IntfVar = rtl.getObject($mod.v);',
  18981. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18982. '$mod.v = $mod.IntfVar;',
  18983. '$mod.v = $mod.IBird;',
  18984. '']));
  18985. end;
  18986. procedure TTestModule.TestClassInterface_Corba_Args;
  18987. begin
  18988. StartProgram(false);
  18989. Add([
  18990. '{$interfaces corba}',
  18991. 'type',
  18992. ' IUnknown = interface',
  18993. ' end;',
  18994. ' IBird = interface(IUnknown)',
  18995. ' end;',
  18996. ' TObject = class',
  18997. ' end;',
  18998. ' TBird = class(TObject,IBird)',
  18999. ' end;',
  19000. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19001. 'begin',
  19002. ' DoIt(i,i,i);',
  19003. 'end;',
  19004. 'procedure Change(var i: IBird; out j: IBird);',
  19005. 'begin',
  19006. ' DoIt(i,i,i);',
  19007. ' Change(i,i);',
  19008. 'end;',
  19009. 'var',
  19010. ' i: IBird;',
  19011. ' o: TBird;',
  19012. 'begin',
  19013. ' DoIt(i,i,i);',
  19014. ' Change(i,i);',
  19015. ' DoIt(o,o,o);',
  19016. '']);
  19017. ConvertProgram;
  19018. CheckSource('TestClassInterface_Corba_Args',
  19019. LinesToStr([ // statements
  19020. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19021. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19022. 'rtl.createClass(this, "TObject", null, function () {',
  19023. ' this.$init = function () {',
  19024. ' };',
  19025. ' this.$final = function () {',
  19026. ' };',
  19027. '});',
  19028. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19029. ' rtl.addIntf(this, $mod.IBird);',
  19030. '});',
  19031. 'this.DoIt = function (u, i, j) {',
  19032. ' $mod.DoIt({',
  19033. ' get: function () {',
  19034. ' return i;',
  19035. ' },',
  19036. ' set: function (v) {',
  19037. ' i = v;',
  19038. ' }',
  19039. ' }, i, i);',
  19040. '};',
  19041. 'this.Change = function (i, j) {',
  19042. ' $mod.DoIt(i, i.get(), i.get());',
  19043. ' $mod.Change(i, i);',
  19044. '};',
  19045. 'this.i = null;',
  19046. 'this.o = null;',
  19047. '']),
  19048. LinesToStr([ // $mod.$main
  19049. '$mod.DoIt({',
  19050. ' p: $mod,',
  19051. ' get: function () {',
  19052. ' return this.p.i;',
  19053. ' },',
  19054. ' set: function (v) {',
  19055. ' this.p.i = v;',
  19056. ' }',
  19057. '}, $mod.i, $mod.i);',
  19058. '$mod.Change({',
  19059. ' p: $mod,',
  19060. ' get: function () {',
  19061. ' return this.p.i;',
  19062. ' },',
  19063. ' set: function (v) {',
  19064. ' this.p.i = v;',
  19065. ' }',
  19066. '}, {',
  19067. ' p: $mod,',
  19068. ' get: function () {',
  19069. ' return this.p.i;',
  19070. ' },',
  19071. ' set: function (v) {',
  19072. ' this.p.i = v;',
  19073. ' }',
  19074. '});',
  19075. '$mod.DoIt({',
  19076. ' p: $mod,',
  19077. ' get: function () {',
  19078. ' return this.p.o;',
  19079. ' },',
  19080. ' set: function (v) {',
  19081. ' this.p.o = v;',
  19082. ' }',
  19083. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19084. '']));
  19085. end;
  19086. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19087. begin
  19088. StartProgram(false);
  19089. Add([
  19090. '{$interfaces corba}',
  19091. 'type',
  19092. ' IUnknown = interface end;',
  19093. ' TObject = class',
  19094. ' Id: longint;',
  19095. ' end;',
  19096. ' IEnumerator = interface(IUnknown)',
  19097. ' function GetCurrent: TObject;',
  19098. ' function MoveNext: Boolean;',
  19099. ' property Current: TObject read GetCurrent;',
  19100. ' end;',
  19101. ' IEnumerable = interface(IUnknown)',
  19102. ' function GetEnumerator: IEnumerator;',
  19103. ' end;',
  19104. 'var',
  19105. ' o: TObject;',
  19106. ' i: IEnumerable;',
  19107. 'begin',
  19108. ' for o in i do o.Id:=3;',
  19109. '']);
  19110. ConvertProgram;
  19111. CheckSource('TestClassInterface_Corba_ForIn',
  19112. LinesToStr([ // statements
  19113. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19114. 'rtl.createClass(this, "TObject", null, function () {',
  19115. ' this.$init = function () {',
  19116. ' this.Id = 0;',
  19117. ' };',
  19118. ' this.$final = function () {',
  19119. ' };',
  19120. '});',
  19121. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19122. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19123. 'this.o = null;',
  19124. 'this.i = null;',
  19125. '']),
  19126. LinesToStr([ // $mod.$main
  19127. 'var $in = $mod.i.GetEnumerator();',
  19128. 'while ($in.MoveNext()) {',
  19129. ' $mod.o = $in.GetCurrent();',
  19130. ' $mod.o.Id = 3;',
  19131. '};',
  19132. '']));
  19133. end;
  19134. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19135. begin
  19136. StartProgram(false);
  19137. Add([
  19138. '{$interfaces com}',
  19139. 'type',
  19140. ' IUnknown = interface',
  19141. ' function _AddRef: longint;',
  19142. ' function _Release: longint;',
  19143. ' end;',
  19144. ' TObject = class(IUnknown)',
  19145. ' function _AddRef: longint; virtual; abstract;',
  19146. ' function _Release: longint; virtual; abstract;',
  19147. ' end;',
  19148. 'var',
  19149. ' i: IUnknown;',
  19150. 'procedure DoGlobal(o: TObject);',
  19151. 'begin',
  19152. ' i:=nil;',
  19153. ' i:=o;',
  19154. ' i:=i;',
  19155. 'end;',
  19156. 'procedure DoLocal(o: TObject);',
  19157. 'const k: IUnknown = nil;',
  19158. 'var j: IUnknown;',
  19159. 'begin',
  19160. ' k:=o;',
  19161. ' k:=i;',
  19162. ' j:=o;',
  19163. ' j:=i;',
  19164. 'end;',
  19165. 'var o: TObject;',
  19166. 'begin',
  19167. ' i:=nil;',
  19168. ' i:=o;',
  19169. '']);
  19170. ConvertProgram;
  19171. CheckSource('TestClassInterface_COM_AssignVar',
  19172. LinesToStr([ // statements
  19173. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19174. 'rtl.createClass(this, "TObject", null, function () {',
  19175. ' this.$init = function () {',
  19176. ' };',
  19177. ' this.$final = function () {',
  19178. ' };',
  19179. ' rtl.addIntf(this, $mod.IUnknown);',
  19180. '});',
  19181. 'this.i = null;',
  19182. 'this.DoGlobal = function (o) {',
  19183. ' rtl.setIntfP($mod, "i", null);',
  19184. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19185. ' rtl.setIntfP($mod, "i", $mod.i);',
  19186. '};',
  19187. 'var k = null;',
  19188. 'this.DoLocal = function (o) {',
  19189. ' var j = null;',
  19190. ' try{',
  19191. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19192. ' k = rtl.setIntfL(k, $mod.i);',
  19193. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19194. ' j = rtl.setIntfL(j, $mod.i);',
  19195. ' }finally{',
  19196. ' rtl._Release(j);',
  19197. ' };',
  19198. '};',
  19199. 'this.o = null;',
  19200. '']),
  19201. LinesToStr([ // $mod.$main
  19202. 'rtl.setIntfP($mod, "i", null);',
  19203. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19204. '']));
  19205. end;
  19206. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19207. begin
  19208. StartProgram(false);
  19209. Add([
  19210. '{$interfaces com}',
  19211. 'type',
  19212. ' IUnknown = interface',
  19213. ' function _AddRef: longint;',
  19214. ' function _Release: longint;',
  19215. ' end;',
  19216. ' TObject = class(IUnknown)',
  19217. ' function _AddRef: longint; virtual; abstract;',
  19218. ' function _Release: longint; virtual; abstract;',
  19219. ' end;',
  19220. 'procedure DoDefault(i, j: IUnknown);',
  19221. 'begin',
  19222. ' i:=nil;',
  19223. ' i:=j;',
  19224. 'end;',
  19225. 'begin',
  19226. '']);
  19227. ConvertProgram;
  19228. CheckSource('TestClassInterface_COM_AssignArg',
  19229. LinesToStr([ // statements
  19230. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19231. 'rtl.createClass(this, "TObject", null, function () {',
  19232. ' this.$init = function () {',
  19233. ' };',
  19234. ' this.$final = function () {',
  19235. ' };',
  19236. ' rtl.addIntf(this, $mod.IUnknown);',
  19237. '});',
  19238. 'this.DoDefault = function (i, j) {',
  19239. ' rtl._AddRef(i);',
  19240. ' try {',
  19241. ' i = rtl.setIntfL(i, null);',
  19242. ' i = rtl.setIntfL(i, j);',
  19243. ' } finally {',
  19244. ' rtl._Release(i);',
  19245. ' };',
  19246. '};',
  19247. '']),
  19248. LinesToStr([ // $mod.$main
  19249. '']));
  19250. end;
  19251. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19252. begin
  19253. StartProgram(false);
  19254. Add([
  19255. '{$interfaces com}',
  19256. 'type',
  19257. ' IUnknown = interface',
  19258. ' function _AddRef: longint;',
  19259. ' function _Release: longint;',
  19260. ' end;',
  19261. ' TObject = class(IUnknown)',
  19262. ' function _AddRef: longint; virtual; abstract;',
  19263. ' function _Release: longint; virtual; abstract;',
  19264. ' end;',
  19265. 'function DoDefault(i: IUnknown): IUnknown;',
  19266. 'begin',
  19267. ' Result:=i;',
  19268. ' if Result<>nil then exit;',
  19269. 'end;',
  19270. 'begin',
  19271. '']);
  19272. ConvertProgram;
  19273. CheckSource('TestClassInterface_COM_FunctionResult',
  19274. LinesToStr([ // statements
  19275. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19276. 'rtl.createClass(this, "TObject", null, function () {',
  19277. ' this.$init = function () {',
  19278. ' };',
  19279. ' this.$final = function () {',
  19280. ' };',
  19281. ' rtl.addIntf(this, $mod.IUnknown);',
  19282. '});',
  19283. 'this.DoDefault = function (i) {',
  19284. ' var Result = null;',
  19285. ' var $ok = false;',
  19286. ' try {',
  19287. ' Result = rtl.setIntfL(Result, i);',
  19288. ' if(Result !== null){',
  19289. ' $ok = true;',
  19290. ' return Result;',
  19291. ' };',
  19292. ' $ok = true;',
  19293. ' } finally {',
  19294. ' if(!$ok) rtl._Release(Result);',
  19295. ' };',
  19296. ' return Result;',
  19297. '};',
  19298. '']),
  19299. LinesToStr([ // $mod.$main
  19300. '']));
  19301. end;
  19302. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19303. begin
  19304. StartProgram(false);
  19305. Add([
  19306. '{$interfaces com}',
  19307. 'type',
  19308. ' IUnknown = interface',
  19309. ' function _AddRef: longint;',
  19310. ' function _Release: longint;',
  19311. ' end;',
  19312. ' TObject = class(IUnknown)',
  19313. ' function _AddRef: longint; virtual; abstract;',
  19314. ' function _Release: longint; virtual; abstract;',
  19315. ' function GetIntf: IUnknown; virtual;',
  19316. ' end;',
  19317. ' TMouse = class',
  19318. ' function GetIntf: IUnknown; override;',
  19319. ' end;',
  19320. 'function TObject.GetIntf: IUnknown; begin end;',
  19321. 'function TMouse.GetIntf: IUnknown;',
  19322. 'var i: IUnknown;',
  19323. 'begin',
  19324. ' inherited;',
  19325. ' inherited GetIntf;',
  19326. ' inherited GetIntf();',
  19327. ' Result:=inherited GetIntf;',
  19328. ' Result:=inherited GetIntf();',
  19329. ' i:=inherited GetIntf;',
  19330. ' i:=inherited GetIntf();',
  19331. 'end;',
  19332. 'begin',
  19333. '']);
  19334. ConvertProgram;
  19335. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19336. LinesToStr([ // statements
  19337. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19338. 'rtl.createClass(this, "TObject", null, function () {',
  19339. ' this.$init = function () {',
  19340. ' };',
  19341. ' this.$final = function () {',
  19342. ' };',
  19343. ' this.GetIntf = function () {',
  19344. ' var Result = null;',
  19345. ' return Result;',
  19346. ' };',
  19347. ' rtl.addIntf(this, $mod.IUnknown);',
  19348. '});',
  19349. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19350. ' this.GetIntf = function () {',
  19351. ' var Result = null;',
  19352. ' var i = null;',
  19353. ' var $ir = rtl.createIntfRefs();',
  19354. ' var $ok = false;',
  19355. ' try {',
  19356. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19357. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19358. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19359. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19360. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19361. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19362. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19363. ' $ok = true;',
  19364. ' } finally {',
  19365. ' $ir.free();',
  19366. ' rtl._Release(i);',
  19367. ' if (!$ok) rtl._Release(Result);',
  19368. ' };',
  19369. ' return Result;',
  19370. ' };',
  19371. ' rtl.addIntf(this, $mod.IUnknown);',
  19372. '});',
  19373. '']),
  19374. LinesToStr([ // $mod.$main
  19375. '']));
  19376. end;
  19377. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19378. begin
  19379. StartProgram(false);
  19380. Add([
  19381. '{$interfaces com}',
  19382. 'type',
  19383. ' IUnknown = interface',
  19384. ' function _AddRef: longint;',
  19385. ' function _Release: longint;',
  19386. ' end;',
  19387. ' TObject = class(IUnknown)',
  19388. ' function _AddRef: longint; virtual; abstract;',
  19389. ' function _Release: longint; virtual; abstract;',
  19390. ' end;',
  19391. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19392. 'begin',
  19393. ' if i is IUnknown then ;',
  19394. ' if o is IUnknown then ;',
  19395. ' if i is TObject then ;',
  19396. ' i:=j as IUnknown;',
  19397. ' i:=o as IUnknown;',
  19398. ' o:=j as TObject;',
  19399. ' i:=IUnknown(j);',
  19400. ' i:=IUnknown(o);',
  19401. ' o:=TObject(i);',
  19402. 'end;',
  19403. 'begin',
  19404. '']);
  19405. ConvertProgram;
  19406. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19407. LinesToStr([ // statements
  19408. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19409. 'rtl.createClass(this, "TObject", null, function () {',
  19410. ' this.$init = function () {',
  19411. ' };',
  19412. ' this.$final = function () {',
  19413. ' };',
  19414. ' rtl.addIntf(this, $mod.IUnknown);',
  19415. '});',
  19416. 'this.DoDefault = function (i, j, o) {',
  19417. ' rtl._AddRef(i);',
  19418. ' try {',
  19419. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19420. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19421. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19422. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19423. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19424. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19425. ' i = rtl.setIntfL(i, j);',
  19426. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19427. ' o = rtl.intfToClass(i, $mod.TObject);',
  19428. ' } finally {',
  19429. ' rtl._Release(i);',
  19430. ' };',
  19431. '};',
  19432. '']),
  19433. LinesToStr([ // $mod.$main
  19434. '']));
  19435. end;
  19436. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19437. begin
  19438. StartProgram(false);
  19439. Add([
  19440. '{$interfaces com}',
  19441. 'type',
  19442. ' IUnknown = interface',
  19443. ' function _AddRef: longint;',
  19444. ' function _Release: longint;',
  19445. ' end;',
  19446. ' TObject = class(IUnknown)',
  19447. ' function _AddRef: longint; virtual; abstract;',
  19448. ' function _Release: longint; virtual; abstract;',
  19449. ' end;',
  19450. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19451. 'var o: TObject;',
  19452. 'begin',
  19453. ' DoIt(v,v,v,v);',
  19454. ' DoIt(o,o,k,k);',
  19455. 'end;',
  19456. 'procedure DoSome;',
  19457. 'var v: IUnknown;',
  19458. 'begin',
  19459. ' DoIt(v,v,v,v);',
  19460. 'end;',
  19461. 'var i: IUnknown;',
  19462. 'begin',
  19463. ' DoIt(i,i,i,i);',
  19464. '']);
  19465. ConvertProgram;
  19466. CheckSource('TestClassInterface_COM_PassAsArg',
  19467. LinesToStr([ // statements
  19468. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19469. 'rtl.createClass(this, "TObject", null, function () {',
  19470. ' this.$init = function () {',
  19471. ' };',
  19472. ' this.$final = function () {',
  19473. ' };',
  19474. ' rtl.addIntf(this, $mod.IUnknown);',
  19475. '});',
  19476. 'this.DoIt = function (v, j, k, l) {',
  19477. ' var o = null;',
  19478. ' var $ir = rtl.createIntfRefs();',
  19479. ' rtl._AddRef(v);',
  19480. ' try {',
  19481. ' $mod.DoIt(v, v, {',
  19482. ' get: function () {',
  19483. ' return v;',
  19484. ' },',
  19485. ' set: function (w) {',
  19486. ' v = rtl.setIntfL(v, w);',
  19487. ' }',
  19488. ' }, {',
  19489. ' get: function () {',
  19490. ' return v;',
  19491. ' },',
  19492. ' set: function (w) {',
  19493. ' v = rtl.setIntfL(v, w);',
  19494. ' }',
  19495. ' });',
  19496. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19497. ' } finally {',
  19498. ' $ir.free();',
  19499. ' rtl._Release(v);',
  19500. ' };',
  19501. '};',
  19502. 'this.DoSome = function () {',
  19503. ' var v = null;',
  19504. ' try {',
  19505. ' $mod.DoIt(v, v, {',
  19506. ' get: function () {',
  19507. ' return v;',
  19508. ' },',
  19509. ' set: function (w) {',
  19510. ' v = rtl.setIntfL(v, w);',
  19511. ' }',
  19512. ' }, {',
  19513. ' get: function () {',
  19514. ' return v;',
  19515. ' },',
  19516. ' set: function (w) {',
  19517. ' v = rtl.setIntfL(v, w);',
  19518. ' }',
  19519. ' });',
  19520. ' } finally {',
  19521. ' rtl._Release(v);',
  19522. ' };',
  19523. '};',
  19524. 'this.i = null;',
  19525. '']),
  19526. LinesToStr([ // $mod.$main
  19527. '$mod.DoIt($mod.i, $mod.i, {',
  19528. ' p: $mod,',
  19529. ' get: function () {',
  19530. ' return this.p.i;',
  19531. ' },',
  19532. ' set: function (v) {',
  19533. ' rtl.setIntfP(this.p, "i", v);',
  19534. ' }',
  19535. '}, {',
  19536. ' p: $mod,',
  19537. ' get: function () {',
  19538. ' return this.p.i;',
  19539. ' },',
  19540. ' set: function (v) {',
  19541. ' rtl.setIntfP(this.p, "i", v);',
  19542. ' }',
  19543. '});',
  19544. '']));
  19545. end;
  19546. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19547. begin
  19548. StartProgram(false);
  19549. Add([
  19550. '{$interfaces com}',
  19551. 'type',
  19552. ' IUnknown = interface',
  19553. ' function _AddRef: longint;',
  19554. ' function _Release: longint;',
  19555. ' end;',
  19556. ' TObject = class(IUnknown)',
  19557. ' function _AddRef: longint; virtual; abstract;',
  19558. ' function _Release: longint; virtual; abstract;',
  19559. ' end;',
  19560. 'procedure DoIt(out i);',
  19561. 'begin end;',
  19562. 'procedure DoSome;',
  19563. 'var v: IUnknown;',
  19564. 'begin',
  19565. ' DoIt(v);',
  19566. 'end;',
  19567. 'function GetIt: IUnknown;',
  19568. 'begin',
  19569. ' DoIt(Result);',
  19570. 'end;',
  19571. 'var i: IUnknown;',
  19572. 'begin',
  19573. ' DoIt(i);',
  19574. '']);
  19575. ConvertProgram;
  19576. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19577. LinesToStr([ // statements
  19578. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19579. 'rtl.createClass(this, "TObject", null, function () {',
  19580. ' this.$init = function () {',
  19581. ' };',
  19582. ' this.$final = function () {',
  19583. ' };',
  19584. ' rtl.addIntf(this, $mod.IUnknown);',
  19585. '});',
  19586. 'this.DoIt = function (i) {',
  19587. '};',
  19588. 'this.DoSome = function () {',
  19589. ' var v = null;',
  19590. ' try {',
  19591. ' $mod.DoIt({',
  19592. ' get: function () {',
  19593. ' return v;',
  19594. ' },',
  19595. ' set: function (w) {',
  19596. ' v = w;',
  19597. ' }',
  19598. ' });',
  19599. ' } finally {',
  19600. ' rtl._Release(v);',
  19601. ' };',
  19602. '};',
  19603. 'this.GetIt = function () {',
  19604. ' var Result = null;',
  19605. ' var $ok = false;',
  19606. ' try {',
  19607. ' $mod.DoIt({',
  19608. ' get: function () {',
  19609. ' return Result;',
  19610. ' },',
  19611. ' set: function (v) {',
  19612. ' Result = v;',
  19613. ' }',
  19614. ' });',
  19615. ' $ok = true;',
  19616. ' } finally {',
  19617. ' if (!$ok) rtl._Release(Result);',
  19618. ' };',
  19619. ' return Result;',
  19620. '};',
  19621. 'this.i = null;',
  19622. '']),
  19623. LinesToStr([ // $mod.$main
  19624. 'try {',
  19625. ' $mod.DoIt({',
  19626. ' p: $mod,',
  19627. ' get: function () {',
  19628. ' return this.p.i;',
  19629. ' },',
  19630. ' set: function (v) {',
  19631. ' this.p.i = v;',
  19632. ' }',
  19633. ' });',
  19634. '} finally {',
  19635. ' rtl._Release($mod.i);',
  19636. '};',
  19637. '']));
  19638. end;
  19639. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19640. begin
  19641. StartProgram(false);
  19642. Add([
  19643. '{$interfaces com}',
  19644. 'type',
  19645. ' IUnknown = interface',
  19646. ' function _AddRef: longint;',
  19647. ' function _Release: longint;',
  19648. ' end;',
  19649. ' TObject = class(IUnknown)',
  19650. ' function _AddRef: longint; virtual; abstract;',
  19651. ' function _Release: longint; virtual; abstract;',
  19652. ' end;',
  19653. 'function GetIt: IUnknown;',
  19654. 'begin',
  19655. 'end;',
  19656. 'procedure DoSome;',
  19657. 'var v: IUnknown;',
  19658. ' i: longint;',
  19659. 'begin',
  19660. ' v:=GetIt;',
  19661. ' v:=GetIt();',
  19662. ' GetIt()._AddRef;',
  19663. ' i:=GetIt()._AddRef;',
  19664. 'end;',
  19665. 'var v: IUnknown;',
  19666. ' i: longint;',
  19667. 'begin',
  19668. ' v:=GetIt;',
  19669. ' v:=GetIt();',
  19670. ' GetIt()._AddRef;',
  19671. ' i:=GetIt()._AddRef;',
  19672. '']);
  19673. ConvertProgram;
  19674. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19675. LinesToStr([ // statements
  19676. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19677. 'rtl.createClass(this, "TObject", null, function () {',
  19678. ' this.$init = function () {',
  19679. ' };',
  19680. ' this.$final = function () {',
  19681. ' };',
  19682. ' rtl.addIntf(this, $mod.IUnknown);',
  19683. '});',
  19684. 'this.GetIt = function () {',
  19685. ' var Result = null;',
  19686. ' return Result;',
  19687. '};',
  19688. 'this.DoSome = function () {',
  19689. ' var v = null;',
  19690. ' var i = 0;',
  19691. ' var $ir = rtl.createIntfRefs();',
  19692. ' try {',
  19693. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19694. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19695. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19696. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19697. ' } finally {',
  19698. ' $ir.free();',
  19699. ' rtl._Release(v);',
  19700. ' };',
  19701. '};',
  19702. 'this.v = null;',
  19703. 'this.i = 0;',
  19704. '']),
  19705. LinesToStr([ // $mod.$main
  19706. 'var $ir = rtl.createIntfRefs();',
  19707. 'try {',
  19708. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19709. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19710. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19711. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19712. '} finally {',
  19713. ' $ir.free();',
  19714. '};',
  19715. '']));
  19716. end;
  19717. procedure TTestModule.TestClassInterface_COM_Property;
  19718. begin
  19719. StartProgram(false);
  19720. Add([
  19721. '{$interfaces com}',
  19722. 'type',
  19723. ' IUnknown = interface',
  19724. ' function _AddRef: longint;',
  19725. ' function _Release: longint;',
  19726. ' end;',
  19727. ' TObject = class(IUnknown)',
  19728. ' FAnt: IUnknown;',
  19729. ' function _AddRef: longint; virtual; abstract;',
  19730. ' function _Release: longint; virtual; abstract;',
  19731. ' function GetBird: IUnknown; virtual; abstract;',
  19732. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19733. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19734. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19735. ' property Ant: IUnknown read FAnt write FAnt;',
  19736. ' property Bird: IUnknown read GetBird write SetBird;',
  19737. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19738. ' end;',
  19739. 'procedure DoIt;',
  19740. 'var',
  19741. ' o: TObject;',
  19742. ' v: IUnknown;',
  19743. 'begin',
  19744. ' v:=o.Ant;',
  19745. ' o.Ant:=v;',
  19746. ' o.Ant:=o.Ant;',
  19747. ' v:=o.Bird;',
  19748. ' o.Bird:=v;',
  19749. ' o.Bird:=o.Bird;',
  19750. ' v:=o.Items[1];',
  19751. ' o.Items[2]:=v;',
  19752. ' o.Items[3]:=o.Items[4];',
  19753. ' v:=o[5];',
  19754. ' o[6]:=v;',
  19755. ' o[7]:=o[8];',
  19756. 'end;',
  19757. 'begin',
  19758. '']);
  19759. ConvertProgram;
  19760. CheckSource('TestClassInterface_COM_Property',
  19761. LinesToStr([ // statements
  19762. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19763. 'rtl.createClass(this, "TObject", null, function () {',
  19764. ' this.$init = function () {',
  19765. ' this.FAnt = null;',
  19766. ' };',
  19767. ' this.$final = function () {',
  19768. ' this.FAnt = undefined;',
  19769. ' };',
  19770. ' rtl.addIntf(this, $mod.IUnknown);',
  19771. '});',
  19772. 'this.DoIt = function () {',
  19773. ' var o = null;',
  19774. ' var v = null;',
  19775. ' var $ir = rtl.createIntfRefs();',
  19776. ' try {',
  19777. ' v = rtl.setIntfL(v, o.FAnt);',
  19778. ' rtl.setIntfP(o, "FAnt", v);',
  19779. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19780. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19781. ' o.SetBird(v);',
  19782. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19783. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19784. ' o.SetItems(2, v);',
  19785. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19786. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19787. ' o.SetItems(6, v);',
  19788. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19789. ' } finally {',
  19790. ' $ir.free();',
  19791. ' rtl._Release(v);',
  19792. ' };',
  19793. '};',
  19794. '']),
  19795. LinesToStr([ // $mod.$main
  19796. '']));
  19797. end;
  19798. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19799. begin
  19800. StartProgram(false);
  19801. Add([
  19802. '{$interfaces com}',
  19803. 'type',
  19804. ' IUnknown = interface',
  19805. ' function _AddRef: longint;',
  19806. ' function _Release: longint;',
  19807. ' function GetBird: IUnknown;',
  19808. ' procedure SetBird(Value: IUnknown);',
  19809. ' function GetItems(Index: longint): IUnknown;',
  19810. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19811. ' property Bird: IUnknown read GetBird write SetBird;',
  19812. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19813. ' end;',
  19814. ' TObject = class(IUnknown)',
  19815. ' function _AddRef: longint; virtual; abstract;',
  19816. ' function _Release: longint; virtual; abstract;',
  19817. ' function GetBird: IUnknown; virtual; abstract;',
  19818. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19819. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19820. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19821. ' end;',
  19822. 'procedure DoIt;',
  19823. 'var',
  19824. ' o: TObject;',
  19825. ' v: IUnknown;',
  19826. 'begin',
  19827. ' v:=v.Items[1];',
  19828. ' v.Items[2]:=v;',
  19829. ' v.Items[3]:=v.Items[4];',
  19830. ' v:=v[5];',
  19831. ' v[6]:=v;',
  19832. ' v[7]:=v[8];',
  19833. ' v[9].Bird.Bird:=v;',
  19834. ' v:=v.Bird[10].Bird',
  19835. 'end;',
  19836. 'begin',
  19837. '']);
  19838. ConvertProgram;
  19839. CheckSource('TestClassInterface_COM_IntfProperty',
  19840. LinesToStr([ // statements
  19841. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19842. ' "_AddRef",',
  19843. ' "_Release",',
  19844. ' "GetBird",',
  19845. ' "SetBird",',
  19846. ' "GetItems",',
  19847. ' "SetItems"',
  19848. '], null);',
  19849. 'rtl.createClass(this, "TObject", null, function () {',
  19850. ' this.$init = function () {',
  19851. ' };',
  19852. ' this.$final = function () {',
  19853. ' };',
  19854. ' rtl.addIntf(this, $mod.IUnknown);',
  19855. '});',
  19856. 'this.DoIt = function () {',
  19857. ' var o = null;',
  19858. ' var v = null;',
  19859. ' var $ir = rtl.createIntfRefs();',
  19860. ' try {',
  19861. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19862. ' v.SetItems(2, v);',
  19863. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19864. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19865. ' v.SetItems(6, v);',
  19866. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19867. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19868. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19869. ' } finally {',
  19870. ' $ir.free();',
  19871. ' rtl._Release(v);',
  19872. ' };',
  19873. '};',
  19874. '']),
  19875. LinesToStr([ // $mod.$main
  19876. '']));
  19877. end;
  19878. procedure TTestModule.TestClassInterface_COM_Delegation;
  19879. begin
  19880. StartProgram(false);
  19881. Add([
  19882. '{$interfaces com}',
  19883. 'type',
  19884. ' IUnknown = interface',
  19885. ' function _AddRef: longint;',
  19886. ' function _Release: longint;',
  19887. ' end;',
  19888. ' IBird = interface(IUnknown)',
  19889. ' procedure Fly(s: string);',
  19890. ' end;',
  19891. ' IEagle = interface(IBird) end;',
  19892. ' IDove = interface(IBird) end;',
  19893. ' ISwallow = interface(IBird) end;',
  19894. ' TObject = class',
  19895. ' end;',
  19896. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19897. ' function _AddRef: longint; virtual; abstract;',
  19898. ' function _Release: longint; virtual; abstract;',
  19899. ' procedure Fly(s: string); virtual; abstract;',
  19900. ' end;',
  19901. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19902. ' function _AddRef: longint; virtual; abstract;',
  19903. ' function _Release: longint; virtual; abstract;',
  19904. ' FBirdIntf: IBird;',
  19905. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19906. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19907. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19908. ' FDoveObj: TBird;',
  19909. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19910. ' function GetSwallowObj: TBird; virtual; abstract;',
  19911. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19912. ' end;',
  19913. 'begin',
  19914. '']);
  19915. ConvertProgram;
  19916. CheckSource('TestClassInterface_COM_Delegation',
  19917. LinesToStr([ // statements
  19918. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19919. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  19920. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  19921. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  19922. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  19923. 'rtl.createClass(this, "TObject", null, function () {',
  19924. ' this.$init = function () {',
  19925. ' };',
  19926. ' this.$final = function () {',
  19927. ' };',
  19928. '});',
  19929. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19930. ' rtl.addIntf(this, $mod.IBird);',
  19931. ' rtl.addIntf(this, $mod.IEagle);',
  19932. ' rtl.addIntf(this, $mod.IDove);',
  19933. ' rtl.addIntf(this, $mod.ISwallow);',
  19934. '});',
  19935. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19936. ' this.$init = function () {',
  19937. ' $mod.TObject.$init.call(this);',
  19938. ' this.FBirdIntf = null;',
  19939. ' this.FDoveObj = null;',
  19940. ' };',
  19941. ' this.$final = function () {',
  19942. ' this.FBirdIntf = undefined;',
  19943. ' this.FDoveObj = undefined;',
  19944. ' $mod.TObject.$final.call(this);',
  19945. ' };',
  19946. ' this.$intfmaps = {',
  19947. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19948. ' return rtl._AddRef(this.FBirdIntf);',
  19949. ' },',
  19950. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19951. ' return this.GetEagleIntf();',
  19952. ' },',
  19953. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19954. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19955. ' },',
  19956. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19957. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19958. ' }',
  19959. ' };',
  19960. '});',
  19961. '']),
  19962. LinesToStr([ // $mod.$main
  19963. '']));
  19964. end;
  19965. procedure TTestModule.TestClassInterface_COM_With;
  19966. begin
  19967. StartProgram(false);
  19968. Add([
  19969. '{$interfaces com}',
  19970. 'type',
  19971. ' IUnknown = interface',
  19972. ' function _AddRef: longint;',
  19973. ' function _Release: longint;',
  19974. ' function GetAnt: IUnknown;',
  19975. ' property Ant: IUnknown read GetAnt;',
  19976. ' end;',
  19977. ' TObject = class(IUnknown)',
  19978. ' function _AddRef: longint; virtual; abstract;',
  19979. ' function _Release: longint; virtual; abstract;',
  19980. ' function GetAnt: IUnknown; virtual; abstract;',
  19981. ' property Ant: IUnknown read GetAnt;',
  19982. ' end;',
  19983. 'procedure DoIt;',
  19984. 'var',
  19985. ' i: IUnknown;',
  19986. 'begin',
  19987. ' with i do ',
  19988. ' GetAnt;',
  19989. ' with i.Ant, Ant do ',
  19990. ' GetAnt;',
  19991. 'end;',
  19992. 'begin',
  19993. '']);
  19994. ConvertProgram;
  19995. CheckSource('TestClassInterface_COM_With',
  19996. LinesToStr([ // statements
  19997. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19998. 'rtl.createClass(this, "TObject", null, function () {',
  19999. ' this.$init = function () {',
  20000. ' };',
  20001. ' this.$final = function () {',
  20002. ' };',
  20003. ' rtl.addIntf(this, $mod.IUnknown);',
  20004. '});',
  20005. 'this.DoIt = function () {',
  20006. ' var i = null;',
  20007. ' var $ir = rtl.createIntfRefs();',
  20008. ' try {',
  20009. ' $ir.ref(1, i.GetAnt());',
  20010. ' var $with = $ir.ref(2, i.GetAnt());',
  20011. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20012. ' $ir.ref(4, $with1.GetAnt());',
  20013. ' } finally {',
  20014. ' $ir.free();',
  20015. ' };',
  20016. '};',
  20017. '']),
  20018. LinesToStr([ // $mod.$main
  20019. '']));
  20020. end;
  20021. procedure TTestModule.TestClassInterface_COM_ForIn;
  20022. begin
  20023. StartProgram(false);
  20024. Add([
  20025. '{$interfaces com}',
  20026. 'type',
  20027. ' IUnknown = interface end;',
  20028. ' TObject = class',
  20029. ' Id: longint;',
  20030. ' end;',
  20031. ' IEnumerator = interface(IUnknown)',
  20032. ' function GetCurrent: TObject;',
  20033. ' function MoveNext: Boolean;',
  20034. ' property Current: TObject read GetCurrent;',
  20035. ' end;',
  20036. ' IEnumerable = interface(IUnknown)',
  20037. ' function GetEnumerator: IEnumerator;',
  20038. ' end;',
  20039. 'var',
  20040. ' o: TObject;',
  20041. ' i: IEnumerable;',
  20042. 'begin',
  20043. ' for o in i do o.Id:=3;',
  20044. '']);
  20045. ConvertProgram;
  20046. CheckSource('TestClassInterface_COM_ForIn',
  20047. LinesToStr([ // statements
  20048. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20049. 'rtl.createClass(this, "TObject", null, function () {',
  20050. ' this.$init = function () {',
  20051. ' this.Id = 0;',
  20052. ' };',
  20053. ' this.$final = function () {',
  20054. ' };',
  20055. '});',
  20056. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20057. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20058. 'this.o = null;',
  20059. 'this.i = null;',
  20060. '']),
  20061. LinesToStr([ // $mod.$main
  20062. 'var $in = $mod.i.GetEnumerator();',
  20063. 'try {',
  20064. ' while ($in.MoveNext()) {',
  20065. ' $mod.o = $in.GetCurrent();',
  20066. ' $mod.o.Id = 3;',
  20067. ' }',
  20068. '} finally {',
  20069. ' rtl._Release($in)',
  20070. '};',
  20071. '']));
  20072. end;
  20073. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20074. begin
  20075. StartProgram(false);
  20076. Add([
  20077. '{$interfaces com}',
  20078. 'type',
  20079. ' IUnknown = interface',
  20080. ' function _AddRef: longint;',
  20081. ' function _Release: longint;',
  20082. ' end;',
  20083. ' TObject = class',
  20084. ' end;',
  20085. ' TArrOfIntf = array of IUnknown;',
  20086. 'begin',
  20087. '']);
  20088. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20089. ConvertProgram;
  20090. end;
  20091. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20092. begin
  20093. StartProgram(false);
  20094. Add([
  20095. '{$interfaces com}',
  20096. 'type',
  20097. ' IUnknown = interface',
  20098. ' function _AddRef: longint;',
  20099. ' function _Release: longint;',
  20100. ' end;',
  20101. ' TRec = record',
  20102. ' i: IUnknown;',
  20103. ' end;',
  20104. 'begin',
  20105. '']);
  20106. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20107. ConvertProgram;
  20108. end;
  20109. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20110. begin
  20111. StartUnit(false);
  20112. Add([
  20113. '{$interfaces com}',
  20114. 'interface',
  20115. 'implementation',
  20116. 'type',
  20117. ' IUnknown = interface',
  20118. ' function _AddRef: longint;',
  20119. ' end;',
  20120. ' TObject = class(IUnknown)',
  20121. ' function _AddRef: longint;',
  20122. ' end;',
  20123. 'function TObject._AddRef: longint; begin end;',
  20124. 'var i: IUnknown;',
  20125. ' o: TObject;',
  20126. 'initialization',
  20127. ' i:=nil;',
  20128. ' i:=i;',
  20129. ' i:=o;',
  20130. ' if (o as IUnknown)=nil then ;',
  20131. '']);
  20132. ConvertUnit;
  20133. CheckSource('TestClassInterface_COM_UnitInitialization',
  20134. LinesToStr([ // statements
  20135. 'var $impl = $mod.$impl;',
  20136. '']),
  20137. LinesToStr([ // this.$init
  20138. 'var $ir = rtl.createIntfRefs();',
  20139. 'try {',
  20140. ' rtl.setIntfP($impl, "i", null);',
  20141. ' rtl.setIntfP($impl, "i", $impl.i);',
  20142. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20143. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20144. '} finally {',
  20145. ' $ir.free();',
  20146. '};',
  20147. '']),
  20148. LinesToStr([ // implementation
  20149. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20150. 'rtl.createClass($impl, "TObject", null, function () {',
  20151. ' this.$init = function () {',
  20152. ' };',
  20153. ' this.$final = function () {',
  20154. ' };',
  20155. ' this._AddRef = function () {',
  20156. ' var Result = 0;',
  20157. ' return Result;',
  20158. ' };',
  20159. ' rtl.addIntf(this, $impl.IUnknown);',
  20160. '});',
  20161. '$impl.i = null;',
  20162. '$impl.o = null;',
  20163. ''])
  20164. );
  20165. end;
  20166. procedure TTestModule.TestClassInterface_GUID;
  20167. begin
  20168. StartProgram(false);
  20169. Add([
  20170. '{$interfaces corba}',
  20171. 'type',
  20172. ' IUnknown = interface',
  20173. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20174. ' end;',
  20175. ' TObject = class end;',
  20176. ' TGUID = record D1, D2, D3, D4: word; end;',
  20177. ' TAliasGUID = TGUID;',
  20178. ' TGUIDString = type string;',
  20179. ' TAliasGUIDString = TGUIDString;',
  20180. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20181. 'begin end;',
  20182. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20183. 'begin end;',
  20184. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20185. 'begin end;',
  20186. 'var',
  20187. ' i: IUnknown;',
  20188. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20189. ' s: TAliasGUIDString;',
  20190. 'begin',
  20191. ' DoConstGUIDIt(IUnknown);',
  20192. ' DoDefGUID(IUnknown);',
  20193. ' DoStr(IUnknown);',
  20194. ' DoConstGUIDIt(i);',
  20195. ' DoDefGUID(i);',
  20196. ' DoStr(i);',
  20197. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20198. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20199. ' DoStr(g);',
  20200. ' g:=i;',
  20201. ' g:=IUnknown;',
  20202. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20203. ' s:=i;',
  20204. ' s:=IUnknown;',
  20205. ' s:=g;',
  20206. ' if g=i then ;',
  20207. ' if i=g then ;',
  20208. ' if g=IUnknown then ;',
  20209. ' if IUnknown=g then ;',
  20210. ' if s=i then ;',
  20211. ' if i=s then ;',
  20212. ' if s=IUnknown then ;',
  20213. ' if IUnknown=s then ;',
  20214. ' if s=g then ;',
  20215. ' if g=s then ;',
  20216. '']);
  20217. ConvertProgram;
  20218. CheckSource('TestClassInterface_GUID',
  20219. LinesToStr([ // statements
  20220. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20221. 'rtl.createClass(this, "TObject", null, function () {',
  20222. ' this.$init = function () {',
  20223. ' };',
  20224. ' this.$final = function () {',
  20225. ' };',
  20226. '});',
  20227. 'rtl.recNewT(this, "TGUID", function () {',
  20228. ' this.D1 = 0;',
  20229. ' this.D2 = 0;',
  20230. ' this.D3 = 0;',
  20231. ' this.D4 = 0;',
  20232. ' this.$eq = function (b) {',
  20233. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20234. ' };',
  20235. ' this.$assign = function (s) {',
  20236. ' this.D1 = s.D1;',
  20237. ' this.D2 = s.D2;',
  20238. ' this.D3 = s.D3;',
  20239. ' this.D4 = s.D4;',
  20240. ' return this;',
  20241. ' };',
  20242. '});',
  20243. 'this.DoConstGUIDIt = function (g) {',
  20244. '};',
  20245. 'this.DoDefGUID = function (g) {',
  20246. '};',
  20247. 'this.DoStr = function (s) {',
  20248. '};',
  20249. 'this.i = null;',
  20250. 'this.g = this.TGUID.$clone({',
  20251. ' D1: 0xD91C9AF4,',
  20252. ' D2: 0x3C93,',
  20253. ' D3: 0x420F,',
  20254. ' D4: [',
  20255. ' 0xA3,',
  20256. ' 0x03,',
  20257. ' 0xBF,',
  20258. ' 0x5B,',
  20259. ' 0xA8,',
  20260. ' 0x2B,',
  20261. ' 0xFD,',
  20262. ' 0x23',
  20263. ' ]',
  20264. '});',
  20265. 'this.s = "";',
  20266. '']),
  20267. LinesToStr([ // $mod.$main
  20268. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20269. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20270. '$mod.DoStr($mod.IUnknown.$guid);',
  20271. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20272. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20273. '$mod.DoStr($mod.i.$guid);',
  20274. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20275. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20276. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20277. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20278. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20279. '$mod.g.$assign({',
  20280. ' D1: 0xD91C9AF4,',
  20281. ' D2: 0x3C93,',
  20282. ' D3: 0x420F,',
  20283. ' D4: [',
  20284. ' 0xA3,',
  20285. ' 0x03,',
  20286. ' 0xBF,',
  20287. ' 0x5B,',
  20288. ' 0xA8,',
  20289. ' 0x2B,',
  20290. ' 0xFD,',
  20291. ' 0x23',
  20292. ' ]',
  20293. '});',
  20294. '$mod.s = $mod.i.$guid;',
  20295. '$mod.s = $mod.IUnknown.$guid;',
  20296. '$mod.s = rtl.guidrToStr($mod.g);',
  20297. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20298. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20299. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20300. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20301. 'if ($mod.s === $mod.i.$guid) ;',
  20302. 'if ($mod.i.$guid === $mod.s) ;',
  20303. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20304. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20305. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20306. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20307. '']));
  20308. end;
  20309. procedure TTestModule.TestClassInterface_GUIDProperty;
  20310. begin
  20311. StartProgram(false);
  20312. Add([
  20313. '{$interfaces corba}',
  20314. 'type',
  20315. ' IUnknown = interface',
  20316. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20317. ' end;',
  20318. ' TGUID = record D1, D2, D3, D4: word; end;',
  20319. ' TAliasGUID = TGUID;',
  20320. ' TGUIDString = type string;',
  20321. ' TAliasGUIDString = TGUIDString;',
  20322. ' TObject = class',
  20323. ' function GetG: TAliasGUID; virtual; abstract;',
  20324. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20325. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20326. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20327. ' property g: TAliasGUID read GetG write SetG;',
  20328. ' property s: TAliasGUIDString read GetS write SetS;',
  20329. ' end;',
  20330. 'var o: TObject;',
  20331. 'begin',
  20332. ' o.g:=IUnknown;',
  20333. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20334. ' o.s:=IUnknown;',
  20335. ' o.s:=o.g;',
  20336. '']);
  20337. ConvertProgram;
  20338. CheckSource('TestClassInterface_GUIDProperty',
  20339. LinesToStr([ // statements
  20340. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20341. 'rtl.recNewT(this, "TGUID", function () {',
  20342. ' this.D1 = 0;',
  20343. ' this.D2 = 0;',
  20344. ' this.D3 = 0;',
  20345. ' this.D4 = 0;',
  20346. ' this.$eq = function (b) {',
  20347. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20348. ' };',
  20349. ' this.$assign = function (s) {',
  20350. ' this.D1 = s.D1;',
  20351. ' this.D2 = s.D2;',
  20352. ' this.D3 = s.D3;',
  20353. ' this.D4 = s.D4;',
  20354. ' return this;',
  20355. ' };',
  20356. '});',
  20357. 'rtl.createClass(this, "TObject", null, function () {',
  20358. ' this.$init = function () {',
  20359. ' };',
  20360. ' this.$final = function () {',
  20361. ' };',
  20362. '});',
  20363. 'this.o = null;',
  20364. '']),
  20365. LinesToStr([ // $mod.$main
  20366. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20367. '$mod.o.SetG({',
  20368. ' D1: 0xD91C9AF4,',
  20369. ' D2: 0x3C93,',
  20370. ' D3: 0x420F,',
  20371. ' D4: [',
  20372. ' 0xA3,',
  20373. ' 0x03,',
  20374. ' 0xBF,',
  20375. ' 0x5B,',
  20376. ' 0xA8,',
  20377. ' 0x2B,',
  20378. ' 0xFD,',
  20379. ' 0x23',
  20380. ' ]',
  20381. '});',
  20382. '$mod.o.SetS($mod.IUnknown.$guid);',
  20383. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20384. '']));
  20385. end;
  20386. procedure TTestModule.TestClassHelper_ClassVar;
  20387. begin
  20388. StartProgram(false);
  20389. Add([
  20390. 'type',
  20391. ' TObject = class',
  20392. ' end;',
  20393. ' THelper = class helper for TObject',
  20394. ' const',
  20395. ' One = 1;',
  20396. ' Two: word = 2;',
  20397. ' class var',
  20398. ' Glob: word;',
  20399. ' function Foo(w: word): word;',
  20400. ' class function Bar(w: word): word;',
  20401. ' end;',
  20402. 'function THelper.foo(w: word): word;',
  20403. 'begin',
  20404. ' Result:=w;',
  20405. ' Two:=One+w;',
  20406. ' Glob:=Glob;',
  20407. ' Result:=Self.Glob;',
  20408. ' Self.Glob:=Self.Glob;',
  20409. ' with Self do Glob:=Glob;',
  20410. 'end;',
  20411. 'class function THelper.bar(w: word): word;',
  20412. 'begin',
  20413. ' Result:=w;',
  20414. ' Two:=One;',
  20415. ' Glob:=Glob;',
  20416. ' Self.Glob:=Self.Glob;',
  20417. ' with Self do Glob:=Glob;',
  20418. 'end;',
  20419. 'var o: TObject;',
  20420. 'begin',
  20421. ' tobject.two:=tobject.one;',
  20422. ' tobject.Glob:=tobject.Glob;',
  20423. ' with tobject do begin',
  20424. ' two:=one;',
  20425. ' Glob:=Glob;',
  20426. ' end;',
  20427. ' o.two:=o.one;',
  20428. ' o.Glob:=o.Glob;',
  20429. ' with o do begin',
  20430. ' two:=one;',
  20431. ' Glob:=Glob;',
  20432. ' end;',
  20433. '']);
  20434. ConvertProgram;
  20435. CheckSource('TestClassHelper_ClassVar',
  20436. LinesToStr([ // statements
  20437. 'rtl.createClass(this, "TObject", null, function () {',
  20438. ' this.$init = function () {',
  20439. ' };',
  20440. ' this.$final = function () {',
  20441. ' };',
  20442. '});',
  20443. 'rtl.createHelper(this, "THelper", null, function () {',
  20444. ' this.One = 1;',
  20445. ' this.Two = 2;',
  20446. ' this.Glob = 0;',
  20447. ' this.Foo = function (w) {',
  20448. ' var Result = 0;',
  20449. ' Result = w;',
  20450. ' $mod.THelper.Two = 1 + w;',
  20451. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20452. ' Result = $mod.THelper.Glob;',
  20453. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20454. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20455. ' return Result;',
  20456. ' };',
  20457. ' this.Bar = function (w) {',
  20458. ' var Result = 0;',
  20459. ' Result = w;',
  20460. ' $mod.THelper.Two = 1;',
  20461. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20462. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20463. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20464. ' return Result;',
  20465. ' };',
  20466. '});',
  20467. 'this.o = null;',
  20468. '']),
  20469. LinesToStr([ // $mod.$main
  20470. '$mod.THelper.Two = 1;',
  20471. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20472. 'var $with = $mod.TObject;',
  20473. '$mod.THelper.Two = 1;',
  20474. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20475. '$mod.THelper.Two = 1;',
  20476. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20477. 'var $with1 = $mod.o;',
  20478. '$mod.THelper.Two = 1;',
  20479. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20480. '']));
  20481. end;
  20482. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20483. begin
  20484. StartProgram(false);
  20485. Add([
  20486. 'type',
  20487. ' TObject = class',
  20488. ' FSize: word;',
  20489. ' property Size: word read FSize write FSize;',
  20490. ' end;',
  20491. ' THelper = class helper for TObject',
  20492. ' function Foo(w: word = 1): word;',
  20493. ' end;',
  20494. 'function THelper.foo(w: word): word;',
  20495. 'begin',
  20496. ' Result:=Size;',
  20497. ' Size:=Size+2;',
  20498. ' Self.Size:=Self.Size+3;',
  20499. ' FSize:=FSize+4;',
  20500. ' Self.FSize:=Self.FSize+5;',
  20501. ' with Self do begin',
  20502. ' Size:=Size+6;',
  20503. ' FSize:=FSize+7;',
  20504. ' FSize:=FSize+8;',
  20505. ' end;',
  20506. 'end;',
  20507. 'begin',
  20508. '']);
  20509. ConvertProgram;
  20510. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20511. LinesToStr([ // statements
  20512. 'rtl.createClass(this, "TObject", null, function () {',
  20513. ' this.$init = function () {',
  20514. ' this.FSize = 0;',
  20515. ' };',
  20516. ' this.$final = function () {',
  20517. ' };',
  20518. '});',
  20519. 'rtl.createHelper(this, "THelper", null, function () {',
  20520. ' this.Foo = function (w) {',
  20521. ' var Result = 0;',
  20522. ' Result = this.FSize;',
  20523. ' this.FSize = this.FSize + 2;',
  20524. ' this.FSize = this.FSize + 3;',
  20525. ' this.FSize = this.FSize + 4;',
  20526. ' this.FSize = this.FSize + 5;',
  20527. ' this.FSize = this.FSize + 6;',
  20528. ' this.FSize = this.FSize + 7;',
  20529. ' this.FSize = this.FSize + 8;',
  20530. ' return Result;',
  20531. ' };',
  20532. '});',
  20533. '']),
  20534. LinesToStr([ // $mod.$main
  20535. '']));
  20536. end;
  20537. procedure TTestModule.TestClassHelper_Method_Call;
  20538. begin
  20539. StartProgram(false);
  20540. Add([
  20541. 'type',
  20542. ' TObject = class',
  20543. ' procedure Run(w: word = 10);',
  20544. ' end;',
  20545. ' THelper = class helper for TObject',
  20546. ' function Foo(w: word = 1): word;',
  20547. ' end;',
  20548. 'procedure TObject.Run(w: word);',
  20549. 'var o: TObject;',
  20550. 'begin',
  20551. ' Foo;',
  20552. ' Foo();',
  20553. ' Foo(2);',
  20554. ' Self.Foo;',
  20555. ' Self.Foo();',
  20556. ' Self.Foo(3);',
  20557. ' with Self do begin',
  20558. ' Foo;',
  20559. ' Foo();',
  20560. ' Foo(4);',
  20561. ' end;',
  20562. ' with o do Foo(5);',
  20563. 'end;',
  20564. 'function THelper.foo(w: word): word;',
  20565. 'begin',
  20566. ' Run;',
  20567. ' Run();',
  20568. ' Run(11);',
  20569. ' Foo;',
  20570. ' Foo();',
  20571. ' Foo(12);',
  20572. ' Self.Foo;',
  20573. ' Self.Foo();',
  20574. ' Self.Foo(13);',
  20575. ' with Self do begin',
  20576. ' Foo;',
  20577. ' Foo();',
  20578. ' Foo(14);',
  20579. ' end;',
  20580. 'end;',
  20581. 'var Obj: TObject;',
  20582. 'begin',
  20583. ' obj.Foo;',
  20584. ' obj.Foo();',
  20585. ' obj.Foo(21);',
  20586. ' with obj do begin',
  20587. ' Foo;',
  20588. ' Foo();',
  20589. ' Foo(22);',
  20590. ' end;',
  20591. '']);
  20592. ConvertProgram;
  20593. CheckSource('TestClassHelper_Method_Call',
  20594. LinesToStr([ // statements
  20595. 'rtl.createClass(this, "TObject", null, function () {',
  20596. ' this.$init = function () {',
  20597. ' };',
  20598. ' this.$final = function () {',
  20599. ' };',
  20600. ' this.Run = function (w) {',
  20601. ' var o = null;',
  20602. ' $mod.THelper.Foo.call(this, 1);',
  20603. ' $mod.THelper.Foo.call(this, 1);',
  20604. ' $mod.THelper.Foo.call(this, 2);',
  20605. ' $mod.THelper.Foo.call(this, 1);',
  20606. ' $mod.THelper.Foo.call(this, 1);',
  20607. ' $mod.THelper.Foo.call(this, 3);',
  20608. ' $mod.THelper.Foo.call(this, 1);',
  20609. ' $mod.THelper.Foo.call(this, 1);',
  20610. ' $mod.THelper.Foo.call(this, 4);',
  20611. ' $mod.THelper.Foo.call(o, 5);',
  20612. ' };',
  20613. '});',
  20614. 'rtl.createHelper(this, "THelper", null, function () {',
  20615. ' this.Foo = function (w) {',
  20616. ' var Result = 0;',
  20617. ' this.Run(10);',
  20618. ' this.Run(10);',
  20619. ' this.Run(11);',
  20620. ' $mod.THelper.Foo.call(this, 1);',
  20621. ' $mod.THelper.Foo.call(this, 1);',
  20622. ' $mod.THelper.Foo.call(this, 12);',
  20623. ' $mod.THelper.Foo.call(this, 1);',
  20624. ' $mod.THelper.Foo.call(this, 1);',
  20625. ' $mod.THelper.Foo.call(this, 13);',
  20626. ' $mod.THelper.Foo.call(this, 1);',
  20627. ' $mod.THelper.Foo.call(this, 1);',
  20628. ' $mod.THelper.Foo.call(this, 14);',
  20629. ' return Result;',
  20630. ' };',
  20631. '});',
  20632. 'this.Obj = null;',
  20633. '']),
  20634. LinesToStr([ // $mod.$main
  20635. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20636. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20637. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20638. 'var $with = $mod.Obj;',
  20639. '$mod.THelper.Foo.call($with, 1);',
  20640. '$mod.THelper.Foo.call($with, 1);',
  20641. '$mod.THelper.Foo.call($with, 22);',
  20642. '']));
  20643. end;
  20644. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20645. begin
  20646. StartProgram(false);
  20647. Add([
  20648. 'type',
  20649. ' TObject = class',
  20650. ' procedure Run(w: word = 10);',
  20651. ' end;',
  20652. ' THelper = class helper for TObject',
  20653. ' function Foo(w: word = 1): word;',
  20654. ' end;',
  20655. 'procedure TObject.Run(w: word);',
  20656. ' procedure Sub(Self: TObject);',
  20657. ' begin',
  20658. ' Foo;',
  20659. ' Foo();',
  20660. ' Self.Foo;',
  20661. ' Self.Foo();',
  20662. ' with Self do begin',
  20663. ' Foo;',
  20664. ' Foo();',
  20665. ' end;',
  20666. ' end;',
  20667. 'begin',
  20668. 'end;',
  20669. 'function THelper.foo(w: word): word;',
  20670. ' procedure Sub(Self: TObject);',
  20671. ' begin',
  20672. ' Run;',
  20673. ' Run();',
  20674. ' Foo;',
  20675. ' Foo();',
  20676. ' Self.Foo;',
  20677. ' Self.Foo();',
  20678. ' with Self do begin',
  20679. ' Foo;',
  20680. ' Foo();',
  20681. ' end;',
  20682. ' end;',
  20683. 'begin',
  20684. 'end;',
  20685. 'begin',
  20686. '']);
  20687. ConvertProgram;
  20688. CheckSource('TestClassHelper_Method_Nested_Call',
  20689. LinesToStr([ // statements
  20690. 'rtl.createClass(this, "TObject", null, function () {',
  20691. ' this.$init = function () {',
  20692. ' };',
  20693. ' this.$final = function () {',
  20694. ' };',
  20695. ' this.Run = function (w) {',
  20696. ' var $Self = this;',
  20697. ' function Sub(Self) {',
  20698. ' $mod.THelper.Foo.call($Self, 1);',
  20699. ' $mod.THelper.Foo.call($Self, 1);',
  20700. ' $mod.THelper.Foo.call(Self, 1);',
  20701. ' $mod.THelper.Foo.call(Self, 1);',
  20702. ' $mod.THelper.Foo.call(Self, 1);',
  20703. ' $mod.THelper.Foo.call(Self, 1);',
  20704. ' };',
  20705. ' };',
  20706. '});',
  20707. 'rtl.createHelper(this, "THelper", null, function () {',
  20708. ' this.Foo = function (w) {',
  20709. ' var $Self = this;',
  20710. ' var Result = 0;',
  20711. ' function Sub(Self) {',
  20712. ' $Self.Run(10);',
  20713. ' $Self.Run(10);',
  20714. ' $mod.THelper.Foo.call($Self, 1);',
  20715. ' $mod.THelper.Foo.call($Self, 1);',
  20716. ' $mod.THelper.Foo.call(Self, 1);',
  20717. ' $mod.THelper.Foo.call(Self, 1);',
  20718. ' $mod.THelper.Foo.call(Self, 1);',
  20719. ' $mod.THelper.Foo.call(Self, 1);',
  20720. ' };',
  20721. ' return Result;',
  20722. ' };',
  20723. '});',
  20724. '']),
  20725. LinesToStr([ // $mod.$main
  20726. '']));
  20727. end;
  20728. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20729. begin
  20730. StartProgram(false);
  20731. Add([
  20732. 'type',
  20733. ' TObject = class',
  20734. ' class procedure Run(w: word = 10);',
  20735. ' end;',
  20736. ' THelper = class helper for TObject',
  20737. ' class function Foo(w: word = 1): word;',
  20738. ' end;',
  20739. 'class procedure TObject.Run(w: word);',
  20740. 'begin',
  20741. ' Foo;',
  20742. ' Foo();',
  20743. ' Self.Foo;',
  20744. ' Self.Foo();',
  20745. ' with Self do begin',
  20746. ' Foo;',
  20747. ' Foo();',
  20748. ' end;',
  20749. 'end;',
  20750. 'class function THelper.foo(w: word): word;',
  20751. 'begin',
  20752. ' Run;',
  20753. ' Run();',
  20754. ' Foo;',
  20755. ' Foo();',
  20756. ' Self.Foo;',
  20757. ' Self.Foo();',
  20758. ' with Self do begin',
  20759. ' Foo;',
  20760. ' Foo();',
  20761. ' end;',
  20762. 'end;',
  20763. 'var',
  20764. ' Obj: TObject;',
  20765. 'begin',
  20766. ' obj.Foo;',
  20767. ' obj.Foo();',
  20768. ' with obj do begin',
  20769. ' Foo;',
  20770. ' Foo();',
  20771. ' end;',
  20772. ' tobject.Foo;',
  20773. ' tobject.Foo();',
  20774. ' with tobject do begin',
  20775. ' Foo;',
  20776. ' Foo();',
  20777. ' end;',
  20778. '']);
  20779. ConvertProgram;
  20780. CheckSource('TestClassHelper_ClassMethod_Call',
  20781. LinesToStr([ // statements
  20782. 'rtl.createClass(this, "TObject", null, function () {',
  20783. ' this.$init = function () {',
  20784. ' };',
  20785. ' this.$final = function () {',
  20786. ' };',
  20787. ' this.Run = function (w) {',
  20788. ' $mod.THelper.Foo.call(this, 1);',
  20789. ' $mod.THelper.Foo.call(this, 1);',
  20790. ' $mod.THelper.Foo.call(this, 1);',
  20791. ' $mod.THelper.Foo.call(this, 1);',
  20792. ' $mod.THelper.Foo.call(this, 1);',
  20793. ' $mod.THelper.Foo.call(this, 1);',
  20794. ' };',
  20795. '});',
  20796. 'rtl.createHelper(this, "THelper", null, function () {',
  20797. ' this.Foo = function (w) {',
  20798. ' var Result = 0;',
  20799. ' this.Run(10);',
  20800. ' this.Run(10);',
  20801. ' $mod.THelper.Foo.call(this, 1);',
  20802. ' $mod.THelper.Foo.call(this, 1);',
  20803. ' $mod.THelper.Foo.call(this, 1);',
  20804. ' $mod.THelper.Foo.call(this, 1);',
  20805. ' $mod.THelper.Foo.call(this, 1);',
  20806. ' $mod.THelper.Foo.call(this, 1);',
  20807. ' return Result;',
  20808. ' };',
  20809. '});',
  20810. 'this.Obj = null;',
  20811. '']),
  20812. LinesToStr([ // $mod.$main
  20813. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20814. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20815. 'var $with = $mod.Obj;',
  20816. '$mod.THelper.Foo.call($with.$class, 1);',
  20817. '$mod.THelper.Foo.call($with.$class, 1);',
  20818. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20819. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20820. 'var $with1 = $mod.TObject;',
  20821. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20822. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20823. '']));
  20824. end;
  20825. procedure TTestModule.TestClassHelper_ClassOf;
  20826. begin
  20827. StartProgram(false);
  20828. Add([
  20829. 'type',
  20830. ' TObject = class',
  20831. ' end;',
  20832. ' TClass = class of TObject;',
  20833. ' THelper = class helper for TObject',
  20834. ' class function Foo(w: word = 1): word;',
  20835. ' end;',
  20836. 'class function THelper.foo(w: word): word;',
  20837. 'begin',
  20838. 'end;',
  20839. 'var',
  20840. ' c: TClass;',
  20841. 'begin',
  20842. ' c.Foo;',
  20843. ' c.Foo();',
  20844. ' with c do begin',
  20845. ' Foo;',
  20846. ' Foo();',
  20847. ' end;',
  20848. '']);
  20849. ConvertProgram;
  20850. CheckSource('TestClassHelper_ClassOf',
  20851. LinesToStr([ // statements
  20852. 'rtl.createClass(this, "TObject", null, function () {',
  20853. ' this.$init = function () {',
  20854. ' };',
  20855. ' this.$final = function () {',
  20856. ' };',
  20857. '});',
  20858. 'rtl.createHelper(this, "THelper", null, function () {',
  20859. ' this.Foo = function (w) {',
  20860. ' var Result = 0;',
  20861. ' return Result;',
  20862. ' };',
  20863. '});',
  20864. 'this.c = null;',
  20865. '']),
  20866. LinesToStr([ // $mod.$main
  20867. '$mod.THelper.Foo.call($mod.c, 1);',
  20868. '$mod.THelper.Foo.call($mod.c, 1);',
  20869. 'var $with = $mod.c;',
  20870. '$mod.THelper.Foo.call($with, 1);',
  20871. '$mod.THelper.Foo.call($with, 1);',
  20872. '']));
  20873. end;
  20874. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20875. begin
  20876. StartProgram(false);
  20877. Add([
  20878. '{$mode objfpc}',
  20879. 'type',
  20880. ' TObject = class',
  20881. ' procedure DoIt;',
  20882. ' end;',
  20883. ' THelper = class helper for TObject',
  20884. ' procedure Fly(w: word = 1);',
  20885. ' class procedure Glide(w: word = 1);',
  20886. ' class procedure Run(w: word = 1); static;',
  20887. ' end;',
  20888. ' TFly = procedure(w: word) of object;',
  20889. ' TGlide = TFly;',
  20890. ' TRun = procedure(w: word);',
  20891. 'var',
  20892. ' f: TFly;',
  20893. ' g: TGlide;',
  20894. ' r: TRun;',
  20895. 'procedure TObject.DoIt;',
  20896. 'begin',
  20897. ' f:=@fly;',
  20898. ' g:=@glide;',
  20899. ' r:=@run;',
  20900. ' f:[email protected];',
  20901. ' g:[email protected];',
  20902. ' r:[email protected];',
  20903. ' with self do begin',
  20904. ' f:=@fly;',
  20905. ' g:=@glide;',
  20906. ' r:=@run;',
  20907. ' end;',
  20908. 'end;',
  20909. 'procedure THelper.fly(w: word);',
  20910. 'begin',
  20911. ' f:=@fly;',
  20912. ' g:=@glide;',
  20913. ' r:=@run;',
  20914. 'end;',
  20915. 'class procedure THelper.glide(w: word);',
  20916. 'begin',
  20917. ' g:=@glide;',
  20918. ' r:=@run;',
  20919. 'end;',
  20920. 'class procedure THelper.run(w: word);',
  20921. 'begin',
  20922. ' g:=@glide;',
  20923. ' r:=@run;',
  20924. 'end;',
  20925. 'var',
  20926. ' Obj: TObject;',
  20927. 'begin',
  20928. ' f:[email protected];',
  20929. ' g:[email protected];',
  20930. ' r:[email protected];',
  20931. ' with obj do begin',
  20932. ' f:=@fly;',
  20933. ' g:=@glide;',
  20934. ' r:=@run;',
  20935. ' end;',
  20936. ' g:[email protected];',
  20937. ' r:[email protected];',
  20938. ' with tobject do begin',
  20939. ' g:=@glide;',
  20940. ' r:=@run;',
  20941. ' end;',
  20942. '']);
  20943. ConvertProgram;
  20944. CheckSource('TestClassHelper_MethodRefObjFPC',
  20945. LinesToStr([ // statements
  20946. 'rtl.createClass(this, "TObject", null, function () {',
  20947. ' this.$init = function () {',
  20948. ' };',
  20949. ' this.$final = function () {',
  20950. ' };',
  20951. ' this.DoIt = function () {',
  20952. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20953. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20954. ' $mod.r = $mod.THelper.Run;',
  20955. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20956. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20957. ' $mod.r = $mod.THelper.Run;',
  20958. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20959. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20960. ' $mod.r = $mod.THelper.Run;',
  20961. ' };',
  20962. '});',
  20963. 'rtl.createHelper(this, "THelper", null, function () {',
  20964. ' this.Fly = function (w) {',
  20965. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20966. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20967. ' $mod.r = $mod.THelper.Run;',
  20968. ' };',
  20969. ' this.Glide = function (w) {',
  20970. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20971. ' $mod.r = $mod.THelper.Run;',
  20972. ' };',
  20973. ' this.Run = function (w) {',
  20974. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20975. ' $mod.r = $mod.THelper.Run;',
  20976. ' };',
  20977. '});',
  20978. 'this.f = null;',
  20979. 'this.g = null;',
  20980. 'this.r = null;',
  20981. 'this.Obj = null;',
  20982. '']),
  20983. LinesToStr([ // $mod.$main
  20984. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20985. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20986. '$mod.r = $mod.THelper.Run;',
  20987. 'var $with = $mod.Obj;',
  20988. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20989. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20990. '$mod.r = $mod.THelper.Run;',
  20991. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20992. '$mod.r = $mod.THelper.Run;',
  20993. 'var $with1 = $mod.TObject;',
  20994. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20995. '$mod.r = $mod.THelper.Run;',
  20996. '']));
  20997. end;
  20998. procedure TTestModule.TestClassHelper_Constructor;
  20999. begin
  21000. StartProgram(false);
  21001. Add([
  21002. 'type',
  21003. ' TObject = class',
  21004. ' constructor Create;',
  21005. ' end;',
  21006. ' TClass = class of TObject;',
  21007. ' THelper = class helper for TObject',
  21008. ' constructor NewHlp(w: word);',
  21009. ' end;',
  21010. 'var',
  21011. ' obj: TObject;',
  21012. ' c: TClass;',
  21013. 'constructor TObject.Create;',
  21014. 'begin',
  21015. ' NewHlp(2);', // normal call
  21016. ' tobject.NewHlp(3);', // new instance
  21017. ' c.newhlp(4);', // new instance
  21018. 'end;',
  21019. 'constructor THelper.NewHlp(w: word);',
  21020. 'begin',
  21021. ' create;', // normal call
  21022. ' tobject.create;', // new instance
  21023. ' NewHlp(2);', // normal call
  21024. ' tobject.NewHlp(3);', // new instance
  21025. ' c.newhlp(4);', // new instance
  21026. 'end;',
  21027. 'begin',
  21028. ' obj.newhlp(2);', // normal call
  21029. ' with Obj do newhlp(12);', // normal call
  21030. ' tobject.newhlp(3);', // new instance
  21031. ' with tobject do newhlp(13);', // new instance
  21032. ' c.newhlp(4);', // new instance
  21033. ' with c do newhlp(14);', // new instance
  21034. '']);
  21035. ConvertProgram;
  21036. CheckSource('TestClassHelper_Constructor',
  21037. LinesToStr([ // statements
  21038. 'rtl.createClass(this, "TObject", null, function () {',
  21039. ' this.$init = function () {',
  21040. ' };',
  21041. ' this.$final = function () {',
  21042. ' };',
  21043. ' this.Create = function () {',
  21044. ' $mod.THelper.NewHlp.call(this, 2);',
  21045. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21046. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21047. ' return this;',
  21048. ' };',
  21049. '});',
  21050. 'rtl.createHelper(this, "THelper", null, function () {',
  21051. ' this.NewHlp = function (w) {',
  21052. ' this.Create();',
  21053. ' $mod.TObject.$create("Create");',
  21054. ' $mod.THelper.NewHlp.call(this, 2);',
  21055. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21056. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21057. ' return this;',
  21058. ' };',
  21059. '});',
  21060. 'this.obj = null;',
  21061. 'this.c = null;',
  21062. '']),
  21063. LinesToStr([ // $mod.$main
  21064. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21065. 'var $with = $mod.obj;',
  21066. '$mod.THelper.NewHlp.call($with, 12);',
  21067. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21068. 'var $with1 = $mod.TObject;',
  21069. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21070. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21071. 'var $with2 = $mod.c;',
  21072. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21073. '']));
  21074. end;
  21075. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21076. begin
  21077. StartProgram(false);
  21078. Add([
  21079. 'type',
  21080. ' TObject = class',
  21081. ' procedure Fly;',
  21082. ' end;',
  21083. ' TObjHelper = class helper for TObject',
  21084. ' procedure Fly;',
  21085. ' end;',
  21086. ' TBird = class',
  21087. ' procedure Fly;',
  21088. ' end;',
  21089. ' TBirdHelper = class helper for TBird',
  21090. ' procedure Fly;',
  21091. ' procedure Walk(w: word);',
  21092. ' end;',
  21093. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21094. ' procedure Fly;',
  21095. ' procedure Walk(w: word);',
  21096. ' end;',
  21097. 'procedure Tobject.fly;',
  21098. 'begin',
  21099. ' inherited;', // ignore
  21100. 'end;',
  21101. 'procedure Tobjhelper.fly;',
  21102. 'begin',
  21103. ' {@TObject_Fly}inherited;',
  21104. ' inherited {@TObject_Fly}Fly;',
  21105. 'end;',
  21106. 'procedure Tbird.fly;',
  21107. 'begin',
  21108. ' {@TObjHelper_Fly}inherited;',
  21109. ' inherited {@TObjHelper_Fly}Fly;',
  21110. 'end;',
  21111. 'procedure Tbirdhelper.fly;',
  21112. 'begin',
  21113. ' {@TBird_Fly}inherited;',
  21114. ' inherited {@TBird_Fly}Fly;',
  21115. 'end;',
  21116. 'procedure Tbirdhelper.walk(w: word);',
  21117. 'begin',
  21118. 'end;',
  21119. 'procedure teagleHelper.fly;',
  21120. 'begin',
  21121. ' {@TBird_Fly}inherited;',
  21122. ' inherited {@TBird_Fly}Fly;',
  21123. 'end;',
  21124. 'procedure teagleHelper.walk(w: word);',
  21125. 'begin',
  21126. ' {@TBirdHelper_Walk}inherited;',
  21127. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21128. 'end;',
  21129. 'begin',
  21130. '']);
  21131. ConvertProgram;
  21132. CheckSource('TestClassHelper_InheritedObjFPC',
  21133. LinesToStr([ // statements
  21134. 'rtl.createClass(this, "TObject", null, function () {',
  21135. ' this.$init = function () {',
  21136. ' };',
  21137. ' this.$final = function () {',
  21138. ' };',
  21139. ' this.Fly = function () {',
  21140. ' };',
  21141. '});',
  21142. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21143. ' this.Fly = function () {',
  21144. ' $mod.TObject.Fly.call(this);',
  21145. ' $mod.TObject.Fly.call(this);',
  21146. ' };',
  21147. '});',
  21148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21149. ' this.Fly$1 = function () {',
  21150. ' $mod.TObjHelper.Fly.call(this);',
  21151. ' $mod.TObjHelper.Fly.call(this);',
  21152. ' };',
  21153. '});',
  21154. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21155. ' this.Fly = function () {',
  21156. ' $mod.TBird.Fly$1.call(this);',
  21157. ' $mod.TBird.Fly$1.call(this);',
  21158. ' };',
  21159. ' this.Walk = function (w) {',
  21160. ' };',
  21161. '});',
  21162. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21163. ' this.Fly$1 = function () {',
  21164. ' $mod.TBird.Fly$1.call(this);',
  21165. ' $mod.TBird.Fly$1.call(this);',
  21166. ' };',
  21167. ' this.Walk$1 = function (w) {',
  21168. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21169. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21170. ' };',
  21171. '});',
  21172. '']),
  21173. LinesToStr([ // $mod.$main
  21174. '']));
  21175. end;
  21176. procedure TTestModule.TestClassHelper_Property;
  21177. begin
  21178. StartProgram(false);
  21179. Add([
  21180. 'type',
  21181. ' TObject = class',
  21182. ' FSize: word;',
  21183. ' function GetSpeed: word;',
  21184. ' procedure SetSpeed(Value: word);',
  21185. ' end;',
  21186. ' TObjHelper = class helper for TObject',
  21187. ' function GetLeft: word;',
  21188. ' procedure SetLeft(Value: word);',
  21189. ' property Size: word read FSize write FSize;',
  21190. ' property Speed: word read GetSpeed write SetSpeed;',
  21191. ' property Left: word read GetLeft write SetLeft;',
  21192. ' end;',
  21193. ' TBird = class',
  21194. ' property NotRight: word read GetLeft write SetLeft;',
  21195. ' procedure DoIt;',
  21196. ' end;',
  21197. 'var',
  21198. ' b: TBird;',
  21199. 'function Tobject.GetSpeed: word;',
  21200. 'begin',
  21201. ' Size:=Size+11;',
  21202. ' Speed:=Speed+12;',
  21203. ' Result:=Left+13;',
  21204. ' Left:=13;',
  21205. ' Left:=Left+13;',
  21206. ' Self.Size:=Self.Size+21;',
  21207. ' Self.Speed:=Self.Speed+22;',
  21208. ' Self.Left:=Self.Left+23;',
  21209. ' with Self do begin',
  21210. ' Size:=Size+31;',
  21211. ' Speed:=Speed+32;',
  21212. ' Left:=Left+33;',
  21213. ' end;',
  21214. 'end;',
  21215. 'procedure Tobject.SetSpeed(Value: word);',
  21216. 'begin',
  21217. 'end;',
  21218. 'function TObjHelper.GetLeft: word;',
  21219. 'begin',
  21220. ' Size:=Size+11;',
  21221. ' Speed:=Speed+12;',
  21222. ' Left:=Left+13;',
  21223. ' Self.Size:=Self.Size+21;',
  21224. ' Self.Speed:=Self.Speed+22;',
  21225. ' Self.Left:=Self.Left+23;',
  21226. ' with Self do begin',
  21227. ' Size:=Size+31;',
  21228. ' Speed:=Speed+32;',
  21229. ' Left:=Left+33;',
  21230. ' end;',
  21231. 'end;',
  21232. 'procedure TObjHelper.SetLeft(Value: word);',
  21233. 'begin',
  21234. 'end;',
  21235. 'procedure TBird.DoIt;',
  21236. 'begin',
  21237. ' NotRight:=NotRight+11;',
  21238. ' Self.NotRight:=Self.NotRight+21;',
  21239. ' with Self do begin',
  21240. ' NotRight:=NotRight+31;',
  21241. ' end;',
  21242. 'end;',
  21243. 'begin',
  21244. ' b.Size:=b.Size+11;',
  21245. ' b.Speed:=b.Speed+12;',
  21246. ' b.Left:=b.Left+13;',
  21247. ' b.NotRight:=b.NotRight+14;',
  21248. ' with b do begin',
  21249. ' Size:=Size+31;',
  21250. ' Speed:=Speed+32;',
  21251. ' Left:=Left+33;',
  21252. ' NotRight:=NotRight+34;',
  21253. ' end;',
  21254. '']);
  21255. ConvertProgram;
  21256. CheckSource('TestClassHelper_Property',
  21257. LinesToStr([ // statements
  21258. 'rtl.createClass(this, "TObject", null, function () {',
  21259. ' this.$init = function () {',
  21260. ' this.FSize = 0;',
  21261. ' };',
  21262. ' this.$final = function () {',
  21263. ' };',
  21264. ' this.GetSpeed = function () {',
  21265. ' var Result = 0;',
  21266. ' this.FSize = this.FSize + 11;',
  21267. ' this.SetSpeed(this.GetSpeed() + 12);',
  21268. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21269. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21270. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21271. ' this.FSize = this.FSize + 21;',
  21272. ' this.SetSpeed(this.GetSpeed() + 22);',
  21273. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21274. ' this.FSize = this.FSize + 31;',
  21275. ' this.SetSpeed(this.GetSpeed() + 32);',
  21276. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21277. ' return Result;',
  21278. ' };',
  21279. ' this.SetSpeed = function (Value) {',
  21280. ' };',
  21281. '});',
  21282. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21283. ' this.GetLeft = function () {',
  21284. ' var Result = 0;',
  21285. ' this.FSize = this.FSize + 11;',
  21286. ' this.SetSpeed(this.GetSpeed() + 12);',
  21287. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21288. ' this.FSize = this.FSize + 21;',
  21289. ' this.SetSpeed(this.GetSpeed() + 22);',
  21290. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21291. ' this.FSize = this.FSize + 31;',
  21292. ' this.SetSpeed(this.GetSpeed() + 32);',
  21293. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21294. ' return Result;',
  21295. ' };',
  21296. ' this.SetLeft = function (Value) {',
  21297. ' };',
  21298. '});',
  21299. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21300. ' this.DoIt = function () {',
  21301. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21302. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21303. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21304. ' };',
  21305. '});',
  21306. 'this.b = null;',
  21307. '']),
  21308. LinesToStr([ // $mod.$main
  21309. '$mod.b.FSize = $mod.b.FSize + 11;',
  21310. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21311. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21312. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21313. 'var $with = $mod.b;',
  21314. '$with.FSize = $with.FSize + 31;',
  21315. '$with.SetSpeed($with.GetSpeed() + 32);',
  21316. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21317. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21318. '']));
  21319. end;
  21320. procedure TTestModule.TestClassHelper_Property_Array;
  21321. begin
  21322. StartProgram(false);
  21323. Add([
  21324. 'type',
  21325. ' TObject = class',
  21326. ' function GetSpeed(Index: boolean): word;',
  21327. ' procedure SetSpeed(Index: boolean; Value: word);',
  21328. ' end;',
  21329. ' TObjHelper = class helper for TObject',
  21330. ' function GetSize(Index: boolean): word;',
  21331. ' procedure SetSize(Index: boolean; Value: word);',
  21332. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21333. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21334. ' end;',
  21335. ' TBird = class',
  21336. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21337. ' procedure DoIt;',
  21338. ' end;',
  21339. 'var',
  21340. ' b: TBird;',
  21341. 'function Tobject.GetSpeed(Index: boolean): word;',
  21342. 'begin',
  21343. ' Result:=Size[false];',
  21344. ' Size[true]:=Size[false]+11;',
  21345. ' Speed[true]:=Speed[false]+12;',
  21346. ' Self.Size[true]:=Self.Size[false]+21;',
  21347. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21348. ' with Self do begin',
  21349. ' Size[true]:=Size[false]+31;',
  21350. ' Speed[true]:=Speed[false]+32;',
  21351. ' end;',
  21352. 'end;',
  21353. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21354. 'begin',
  21355. 'end;',
  21356. 'function TObjHelper.GetSize(Index: boolean): word;',
  21357. 'begin',
  21358. ' Size[true]:=Size[false]+11;',
  21359. ' Speed[true]:=Speed[false]+12;',
  21360. ' Self.Size[true]:=Self.Size[false]+21;',
  21361. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21362. ' with Self do begin',
  21363. ' Size[true]:=Size[false]+31;',
  21364. ' Speed[true]:=Speed[false]+32;',
  21365. ' end;',
  21366. 'end;',
  21367. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21368. 'begin',
  21369. 'end;',
  21370. 'procedure TBird.DoIt;',
  21371. 'begin',
  21372. ' Items[true]:=Items[false]+11;',
  21373. ' Self.Items[true]:=Self.Items[false]+21;',
  21374. ' with Self do Items[true]:=Items[false]+31;',
  21375. 'end;',
  21376. 'begin',
  21377. ' b.Size[true]:=b.Size[false]+11;',
  21378. ' b.Speed[true]:=b.Speed[false]+12;',
  21379. ' b.Items[true]:=b.Items[false]+13;',
  21380. ' with b do begin',
  21381. ' Size[true]:=Size[false]+21;',
  21382. ' Speed[true]:=Speed[false]+22;',
  21383. ' Items[true]:=Items[false]+23;',
  21384. ' end;',
  21385. '']);
  21386. ConvertProgram;
  21387. CheckSource('TestClassHelper_Property_Array',
  21388. LinesToStr([ // statements
  21389. 'rtl.createClass(this, "TObject", null, function () {',
  21390. ' this.$init = function () {',
  21391. ' };',
  21392. ' this.$final = function () {',
  21393. ' };',
  21394. ' this.GetSpeed = function (Index) {',
  21395. ' var Result = 0;',
  21396. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21397. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21398. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21399. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21400. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21401. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21402. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21403. ' return Result;',
  21404. ' };',
  21405. ' this.SetSpeed = function (Index, Value) {',
  21406. ' };',
  21407. '});',
  21408. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21409. ' this.GetSize = function (Index) {',
  21410. ' var Result = 0;',
  21411. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21412. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21413. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21414. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21415. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21416. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21417. ' return Result;',
  21418. ' };',
  21419. ' this.SetSize = function (Index, Value) {',
  21420. ' };',
  21421. '});',
  21422. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21423. ' this.DoIt = function () {',
  21424. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21425. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21426. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21427. ' };',
  21428. '});',
  21429. 'this.b = null;',
  21430. '']),
  21431. LinesToStr([ // $mod.$main
  21432. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21433. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21434. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21435. 'var $with = $mod.b;',
  21436. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21437. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21438. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21439. '']));
  21440. end;
  21441. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21442. begin
  21443. StartProgram(false);
  21444. Add([
  21445. 'type',
  21446. ' TObject = class',
  21447. ' function GetSpeed(Index: boolean): word;',
  21448. ' procedure SetSpeed(Index: boolean; Value: word);',
  21449. ' end;',
  21450. ' TObjHelper = class helper for TObject',
  21451. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21452. ' end;',
  21453. ' TBird = class',
  21454. ' end;',
  21455. ' TBirdHelper = class helper for TBird',
  21456. ' function GetSize(Index: word): boolean;',
  21457. ' procedure SetSize(Index: word; Value: boolean);',
  21458. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21459. ' end;',
  21460. 'function Tobject.GetSpeed(Index: boolean): word;',
  21461. 'begin',
  21462. ' Self[true]:=Self[false]+1;',
  21463. 'end;',
  21464. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21465. 'begin',
  21466. 'end;',
  21467. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21468. 'begin',
  21469. ' Self[1]:=not Self[2];',
  21470. 'end;',
  21471. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21472. 'begin',
  21473. 'end;',
  21474. 'var',
  21475. ' o: TObject;',
  21476. ' b: TBird;',
  21477. 'begin',
  21478. ' o[true]:=o[false]+1;',
  21479. ' b[3]:=not b[4];',
  21480. '']);
  21481. ConvertProgram;
  21482. CheckSource('TestClassHelper_Property_Array_Default',
  21483. LinesToStr([ // statements
  21484. 'rtl.createClass(this, "TObject", null, function () {',
  21485. ' this.$init = function () {',
  21486. ' };',
  21487. ' this.$final = function () {',
  21488. ' };',
  21489. ' this.GetSpeed = function (Index) {',
  21490. ' var Result = 0;',
  21491. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21492. ' return Result;',
  21493. ' };',
  21494. ' this.SetSpeed = function (Index, Value) {',
  21495. ' };',
  21496. '});',
  21497. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21498. '});',
  21499. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21500. '});',
  21501. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21502. ' this.GetSize = function (Index) {',
  21503. ' var Result = false;',
  21504. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21505. ' return Result;',
  21506. ' };',
  21507. ' this.SetSize = function (Index, Value) {',
  21508. ' };',
  21509. '});',
  21510. 'this.o = null;',
  21511. 'this.b = null;',
  21512. '']),
  21513. LinesToStr([ // $mod.$main
  21514. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21515. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21516. '']));
  21517. end;
  21518. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21519. begin
  21520. StartProgram(false);
  21521. Add([
  21522. 'type',
  21523. ' TObject = class',
  21524. ' end;',
  21525. ' TObjHelper = class helper for TObject',
  21526. ' function GetItems(Index: word): TObject;',
  21527. ' procedure SetItems(Index: word; Value: TObject);',
  21528. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21529. ' end;',
  21530. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21531. 'begin',
  21532. ' Self[1][2]:=Self[3][4];',
  21533. 'end;',
  21534. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21535. 'begin',
  21536. 'end;',
  21537. 'var',
  21538. ' o: TObject;',
  21539. 'begin',
  21540. ' o[1][2]:=o[3][4];',
  21541. '']);
  21542. ConvertProgram;
  21543. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21544. LinesToStr([ // statements
  21545. 'rtl.createClass(this, "TObject", null, function () {',
  21546. ' this.$init = function () {',
  21547. ' };',
  21548. ' this.$final = function () {',
  21549. ' };',
  21550. '});',
  21551. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21552. ' this.GetItems = function (Index) {',
  21553. ' var Result = null;',
  21554. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21555. ' return Result;',
  21556. ' };',
  21557. ' this.SetItems = function (Index, Value) {',
  21558. ' };',
  21559. '});',
  21560. 'this.o = null;',
  21561. '']),
  21562. LinesToStr([ // $mod.$main
  21563. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21564. '']));
  21565. end;
  21566. procedure TTestModule.TestClassHelper_ClassProperty;
  21567. begin
  21568. StartProgram(false);
  21569. Add([
  21570. 'type',
  21571. ' TObject = class',
  21572. ' class var FSize: word;',
  21573. ' class function GetSpeed: word;',
  21574. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21575. ' end;',
  21576. ' TObjHelper = class helper for TObject',
  21577. ' class function GetLeft: word;',
  21578. ' class procedure SetLeft(Value: word);',
  21579. ' class property Size: word read FSize write FSize;',
  21580. ' class property Speed: word read GetSpeed write SetSpeed;',
  21581. ' class property Left: word read GetLeft write SetLeft;',
  21582. ' end;',
  21583. ' TBird = class',
  21584. ' class property NotRight: word read GetLeft write SetLeft;',
  21585. ' class procedure DoIt;',
  21586. ' end;',
  21587. ' TBirdClass = class of TBird;',
  21588. 'class function Tobject.GetSpeed: word;',
  21589. 'begin',
  21590. ' Size:=Size+11;',
  21591. ' Speed:=Speed+12;',
  21592. ' Left:=Left+13;',
  21593. ' Self.Size:=Self.Size+21;',
  21594. ' Self.Speed:=Self.Speed+22;',
  21595. ' Self.Left:=Self.Left+23;',
  21596. ' with Self do begin',
  21597. ' Size:=Size+31;',
  21598. ' Speed:=Speed+32;',
  21599. ' Left:=Left+33;',
  21600. ' end;',
  21601. 'end;',
  21602. 'class function TObjHelper.GetLeft: word;',
  21603. 'begin',
  21604. ' Size:=Size+11;',
  21605. ' Speed:=Speed+12;',
  21606. ' Left:=Left+13;',
  21607. ' Self.Size:=Self.Size+21;',
  21608. ' Self.Speed:=Self.Speed+22;',
  21609. ' Self.Left:=Self.Left+23;',
  21610. ' with Self do begin',
  21611. ' Size:=Size+31;',
  21612. ' Speed:=Speed+32;',
  21613. ' Left:=Left+33;',
  21614. ' end;',
  21615. 'end;',
  21616. 'class procedure TObjHelper.SetLeft(Value: word);',
  21617. 'begin',
  21618. 'end;',
  21619. 'class procedure TBird.DoIt;',
  21620. 'begin',
  21621. ' NotRight:=NotRight+11;',
  21622. ' Self.NotRight:=Self.NotRight+21;',
  21623. ' with Self do NotRight:=NotRight+31;',
  21624. 'end;',
  21625. 'var',
  21626. ' b: TBird;',
  21627. ' c: TBirdClass;',
  21628. 'begin',
  21629. ' b.Size:=b.Size+11;',
  21630. ' b.Speed:=b.Speed+12;',
  21631. ' b.Left:=b.Left+13;',
  21632. ' b.NotRight:=b.NotRight+14;',
  21633. ' with b do begin',
  21634. ' Size:=Size+31;',
  21635. ' Speed:=Speed+32;',
  21636. ' Left:=Left+33;',
  21637. ' NotRight:=NotRight+34;',
  21638. ' end;',
  21639. ' c.Size:=c.Size+11;',
  21640. ' c.Speed:=c.Speed+12;',
  21641. ' c.Left:=c.Left+13;',
  21642. ' c.NotRight:=c.NotRight+14;',
  21643. ' with c do begin',
  21644. ' Size:=Size+31;',
  21645. ' Speed:=Speed+32;',
  21646. ' Left:=Left+33;',
  21647. ' NotRight:=NotRight+34;',
  21648. ' end;',
  21649. ' tbird.Size:=tbird.Size+11;',
  21650. ' tbird.Speed:=tbird.Speed+12;',
  21651. ' tbird.Left:=tbird.Left+13;',
  21652. ' tbird.NotRight:=tbird.NotRight+14;',
  21653. ' with tbird do begin',
  21654. ' Size:=Size+31;',
  21655. ' Speed:=Speed+32;',
  21656. ' Left:=Left+33;',
  21657. ' NotRight:=NotRight+34;',
  21658. ' end;',
  21659. '']);
  21660. ConvertProgram;
  21661. CheckSource('TestClassHelper_ClassProperty',
  21662. LinesToStr([ // statements
  21663. 'rtl.createClass(this, "TObject", null, function () {',
  21664. ' this.FSize = 0;',
  21665. ' this.$init = function () {',
  21666. ' };',
  21667. ' this.$final = function () {',
  21668. ' };',
  21669. ' this.GetSpeed = function () {',
  21670. ' var Result = 0;',
  21671. ' $mod.TObject.FSize = this.FSize + 11;',
  21672. ' this.SetSpeed(this.GetSpeed() + 12);',
  21673. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21674. ' $mod.TObject.FSize = this.FSize + 21;',
  21675. ' this.SetSpeed(this.GetSpeed() + 22);',
  21676. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21677. ' $mod.TObject.FSize = this.FSize + 31;',
  21678. ' this.SetSpeed(this.GetSpeed() + 32);',
  21679. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21680. ' return Result;',
  21681. ' };',
  21682. '});',
  21683. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21684. ' this.GetLeft = function () {',
  21685. ' var Result = 0;',
  21686. ' $mod.TObject.FSize = this.FSize + 11;',
  21687. ' this.SetSpeed(this.GetSpeed() + 12);',
  21688. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21689. ' $mod.TObject.FSize = this.FSize + 21;',
  21690. ' this.SetSpeed(this.GetSpeed() + 22);',
  21691. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21692. ' $mod.TObject.FSize = this.FSize + 31;',
  21693. ' this.SetSpeed(this.GetSpeed() + 32);',
  21694. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21695. ' return Result;',
  21696. ' };',
  21697. ' this.SetLeft = function (Value) {',
  21698. ' };',
  21699. '});',
  21700. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21701. ' this.DoIt = function () {',
  21702. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21703. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21704. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21705. ' };',
  21706. '});',
  21707. 'this.b = null;',
  21708. 'this.c = null;',
  21709. '']),
  21710. LinesToStr([ // $mod.$main
  21711. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21712. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21713. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21714. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21715. 'var $with = $mod.b;',
  21716. '$mod.TObject.FSize = $with.FSize + 31;',
  21717. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21718. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21719. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21720. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21721. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21722. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21723. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21724. 'var $with1 = $mod.c;',
  21725. '$mod.TObject.FSize = $with1.FSize + 31;',
  21726. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21727. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21728. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21729. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21730. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21731. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21732. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21733. 'var $with2 = $mod.TBird;',
  21734. '$mod.TObject.FSize = $with2.FSize + 31;',
  21735. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21736. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21737. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21738. '']));
  21739. end;
  21740. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21741. begin
  21742. StartProgram(false);
  21743. Add([
  21744. 'type',
  21745. ' TObject = class',
  21746. ' class function GetSpeed: word; static;',
  21747. ' class procedure SetSpeed(Value: word); static;',
  21748. ' end;',
  21749. ' TObjHelper = class helper for TObject',
  21750. ' class function GetLeft: word; static;',
  21751. ' class procedure SetLeft(Value: word); static;',
  21752. ' class property Speed: word read GetSpeed write SetSpeed;',
  21753. ' class property Left: word read GetLeft write SetLeft;',
  21754. ' end;',
  21755. ' TBird = class',
  21756. ' class property NotRight: word read GetLeft write SetLeft;',
  21757. ' class procedure DoIt; static;',
  21758. ' class procedure DoSome;',
  21759. ' end;',
  21760. ' TBirdClass = class of TBird;',
  21761. 'class function Tobject.GetSpeed: word;',
  21762. 'begin',
  21763. ' Speed:=Speed+12;',
  21764. ' Left:=Left+13;',
  21765. 'end;',
  21766. 'class procedure TObject.SetSpeed(Value: word);',
  21767. 'begin',
  21768. 'end;',
  21769. 'class function TObjHelper.GetLeft: word;',
  21770. 'begin',
  21771. ' Speed:=Speed+12;',
  21772. ' Left:=Left+13;',
  21773. 'end;',
  21774. 'class procedure TObjHelper.SetLeft(Value: word);',
  21775. 'begin',
  21776. 'end;',
  21777. 'class procedure TBird.DoIt;',
  21778. 'begin',
  21779. ' NotRight:=NotRight+11;',
  21780. 'end;',
  21781. 'class procedure TBird.DoSome;',
  21782. 'begin',
  21783. ' Speed:=Speed+12;',
  21784. ' Left:=Left+13;',
  21785. ' Self.Speed:=Self.Speed+22;',
  21786. ' Self.Left:=Self.Left+23;',
  21787. ' with Self do begin',
  21788. ' Speed:=Speed+32;',
  21789. ' Left:=Left+33;',
  21790. ' end;',
  21791. ' NotRight:=NotRight+11;',
  21792. ' Self.NotRight:=Self.NotRight+21;',
  21793. ' with Self do NotRight:=NotRight+31;',
  21794. 'end;',
  21795. 'var',
  21796. ' b: TBird;',
  21797. ' c: TBirdClass;',
  21798. 'begin',
  21799. ' b.Speed:=b.Speed+12;',
  21800. ' b.Left:=b.Left+13;',
  21801. ' b.NotRight:=b.NotRight+14;',
  21802. ' with b do begin',
  21803. ' Speed:=Speed+32;',
  21804. ' Left:=Left+33;',
  21805. ' NotRight:=NotRight+34;',
  21806. ' end;',
  21807. ' c.Speed:=c.Speed+12;',
  21808. ' c.Left:=c.Left+13;',
  21809. ' c.NotRight:=c.NotRight+14;',
  21810. ' with c do begin',
  21811. ' Speed:=Speed+32;',
  21812. ' Left:=Left+33;',
  21813. ' NotRight:=NotRight+34;',
  21814. ' end;',
  21815. ' tbird.Speed:=tbird.Speed+12;',
  21816. ' tbird.Left:=tbird.Left+13;',
  21817. ' tbird.NotRight:=tbird.NotRight+14;',
  21818. ' with tbird do begin',
  21819. ' Speed:=Speed+32;',
  21820. ' Left:=Left+33;',
  21821. ' NotRight:=NotRight+34;',
  21822. ' end;',
  21823. '']);
  21824. ConvertProgram;
  21825. CheckSource('TestClassHelper_ClassPropertyStatic',
  21826. LinesToStr([ // statements
  21827. 'rtl.createClass(this, "TObject", null, function () {',
  21828. ' this.$init = function () {',
  21829. ' };',
  21830. ' this.$final = function () {',
  21831. ' };',
  21832. ' this.GetSpeed = function () {',
  21833. ' var Result = 0;',
  21834. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21835. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21836. ' return Result;',
  21837. ' };',
  21838. ' this.SetSpeed = function (Value) {',
  21839. ' };',
  21840. '});',
  21841. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21842. ' this.GetLeft = function () {',
  21843. ' var Result = 0;',
  21844. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21845. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21846. ' return Result;',
  21847. ' };',
  21848. ' this.SetLeft = function (Value) {',
  21849. ' };',
  21850. '});',
  21851. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21852. ' this.DoIt = function () {',
  21853. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21854. ' };',
  21855. ' this.DoSome = function () {',
  21856. ' this.SetSpeed(this.GetSpeed() + 12);',
  21857. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21858. ' this.SetSpeed(this.GetSpeed() + 22);',
  21859. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21860. ' this.SetSpeed(this.GetSpeed() + 32);',
  21861. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21862. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21863. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21864. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21865. ' };',
  21866. '});',
  21867. 'this.b = null;',
  21868. 'this.c = null;',
  21869. '']),
  21870. LinesToStr([ // $mod.$main
  21871. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21872. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21873. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21874. 'var $with = $mod.b;',
  21875. '$with.SetSpeed($with.GetSpeed() + 32);',
  21876. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21877. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21878. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21879. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21880. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21881. 'var $with1 = $mod.c;',
  21882. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21883. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21884. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21885. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21886. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21887. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21888. 'var $with2 = $mod.TBird;',
  21889. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21890. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21891. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21892. '']));
  21893. end;
  21894. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21895. begin
  21896. StartProgram(false);
  21897. Add([
  21898. 'type',
  21899. ' TObject = class',
  21900. ' class function GetSpeed(Index: boolean): word;',
  21901. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21902. ' end;',
  21903. ' TObjHelper = class helper for TObject',
  21904. ' class function GetSize(Index: boolean): word;',
  21905. ' class procedure SetSize(Index: boolean; Value: word);',
  21906. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21907. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21908. ' end;',
  21909. ' TBird = class',
  21910. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21911. ' class procedure DoIt;',
  21912. ' end;',
  21913. ' TBirdClass = class of TBird;',
  21914. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21915. 'begin',
  21916. ' Size[true]:=Size[false]+11;',
  21917. ' Speed[true]:=Speed[false]+12;',
  21918. ' Self.Size[true]:=Self.Size[false]+21;',
  21919. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21920. ' with Self do begin',
  21921. ' Size[true]:=Size[false]+31;',
  21922. ' Speed[true]:=Speed[false]+32;',
  21923. ' end;',
  21924. 'end;',
  21925. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21926. 'begin',
  21927. ' Size[true]:=Size[false]+11;',
  21928. ' Speed[true]:=Speed[false]+12;',
  21929. ' Self.Size[true]:=Self.Size[false]+21;',
  21930. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21931. ' with Self do begin',
  21932. ' Size[true]:=Size[false]+31;',
  21933. ' Speed[true]:=Speed[false]+32;',
  21934. ' end;',
  21935. 'end;',
  21936. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21937. 'begin',
  21938. 'end;',
  21939. 'class procedure TBird.DoIt;',
  21940. 'begin',
  21941. ' Items[true]:=Items[false]+11;',
  21942. ' Self.Items[true]:=Self.Items[false]+21;',
  21943. ' with Self do Items[true]:=Items[false]+31;',
  21944. 'end;',
  21945. 'var',
  21946. ' b: TBird;',
  21947. ' c: TBirdClass;',
  21948. 'begin',
  21949. ' b.Size[true]:=b.Size[false]+11;',
  21950. ' b.Speed[true]:=b.Speed[false]+12;',
  21951. ' b.Items[true]:=b.Items[false]+13;',
  21952. ' with b do begin',
  21953. ' Size[true]:=Size[false]+21;',
  21954. ' Speed[true]:=Speed[false]+22;',
  21955. ' Items[true]:=Items[false]+23;',
  21956. ' end;',
  21957. ' c.Size[true]:=c.Size[false]+11;',
  21958. ' c.Speed[true]:=c.Speed[false]+12;',
  21959. ' c.Items[true]:=c.Items[false]+13;',
  21960. ' with c do begin',
  21961. ' Size[true]:=Size[false]+21;',
  21962. ' Speed[true]:=Speed[false]+22;',
  21963. ' Items[true]:=Items[false]+23;',
  21964. ' end;',
  21965. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21966. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21967. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21968. ' with TBird do begin',
  21969. ' Size[true]:=Size[false]+21;',
  21970. ' Speed[true]:=Speed[false]+22;',
  21971. ' Items[true]:=Items[false]+23;',
  21972. ' end;',
  21973. '']);
  21974. ConvertProgram;
  21975. CheckSource('TestClassHelper_ClassProperty_Array',
  21976. LinesToStr([ // statements
  21977. 'rtl.createClass(this, "TObject", null, function () {',
  21978. ' this.$init = function () {',
  21979. ' };',
  21980. ' this.$final = function () {',
  21981. ' };',
  21982. ' this.GetSpeed = function (Index) {',
  21983. ' var Result = 0;',
  21984. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21985. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21986. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21987. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21988. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21989. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21990. ' return Result;',
  21991. ' };',
  21992. '});',
  21993. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21994. ' this.GetSize = function (Index) {',
  21995. ' var Result = 0;',
  21996. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21997. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21998. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21999. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22000. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22001. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22002. ' return Result;',
  22003. ' };',
  22004. ' this.SetSize = function (Index, Value) {',
  22005. ' };',
  22006. '});',
  22007. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22008. ' this.DoIt = function () {',
  22009. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22010. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22011. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22012. ' };',
  22013. '});',
  22014. 'this.b = null;',
  22015. 'this.c = null;',
  22016. '']),
  22017. LinesToStr([ // $mod.$main
  22018. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22019. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22020. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22021. 'var $with = $mod.b;',
  22022. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22023. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22024. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22025. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22026. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22027. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22028. 'var $with1 = $mod.c;',
  22029. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22030. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22031. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22032. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22033. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22034. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22035. 'var $with2 = $mod.TBird;',
  22036. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22037. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22038. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22039. '']));
  22040. end;
  22041. procedure TTestModule.TestClassHelper_ForIn;
  22042. begin
  22043. StartProgram(false);
  22044. Add([
  22045. 'type',
  22046. ' TObject = class end;',
  22047. ' TItem = TObject;',
  22048. ' TEnumerator = class',
  22049. ' FCurrent: TItem;',
  22050. ' property Current: TItem read FCurrent;',
  22051. ' function MoveNext: boolean;',
  22052. ' end;',
  22053. ' TBird = class',
  22054. ' end;',
  22055. ' TBirdHelper = class helper for TBird',
  22056. ' function GetEnumerator: TEnumerator;',
  22057. ' end;',
  22058. 'function TEnumerator.MoveNext: boolean;',
  22059. 'begin',
  22060. 'end;',
  22061. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22062. 'begin',
  22063. 'end;',
  22064. 'var',
  22065. ' b: TBird;',
  22066. ' i, i2: TItem;',
  22067. 'begin',
  22068. ' for i in b do i2:=i;']);
  22069. ConvertProgram;
  22070. CheckSource('TestClassHelper_ForIn',
  22071. LinesToStr([ // statements
  22072. 'rtl.createClass(this, "TObject", null, function () {',
  22073. ' this.$init = function () {',
  22074. ' };',
  22075. ' this.$final = function () {',
  22076. ' };',
  22077. '});',
  22078. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22079. ' this.$init = function () {',
  22080. ' $mod.TObject.$init.call(this);',
  22081. ' this.FCurrent = null;',
  22082. ' };',
  22083. ' this.$final = function () {',
  22084. ' this.FCurrent = undefined;',
  22085. ' $mod.TObject.$final.call(this);',
  22086. ' };',
  22087. ' this.MoveNext = function () {',
  22088. ' var Result = false;',
  22089. ' return Result;',
  22090. ' };',
  22091. '});',
  22092. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22093. '});',
  22094. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22095. ' this.GetEnumerator = function () {',
  22096. ' var Result = null;',
  22097. ' return Result;',
  22098. ' };',
  22099. '});',
  22100. 'this.b = null;',
  22101. 'this.i = null;',
  22102. 'this.i2 = null;'
  22103. ]),
  22104. LinesToStr([ // $mod.$main
  22105. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22106. 'try {',
  22107. ' while ($in.MoveNext()){',
  22108. ' $mod.i = $in.FCurrent;',
  22109. ' $mod.i2 = $mod.i;',
  22110. ' }',
  22111. '} finally {',
  22112. ' $in = rtl.freeLoc($in)',
  22113. '};',
  22114. '']));
  22115. end;
  22116. procedure TTestModule.TestClassHelper_PassProperty;
  22117. begin
  22118. StartProgram(false);
  22119. Add([
  22120. 'type',
  22121. ' TObject = class',
  22122. ' FField: TObject;',
  22123. ' property Field: TObject read FField write FField;',
  22124. ' end;',
  22125. ' THelper = class helper for TObject',
  22126. ' procedure Fly;',
  22127. ' class procedure Run;',
  22128. ' class procedure Jump; static;',
  22129. ' end;',
  22130. 'procedure THelper.Fly;',
  22131. 'begin',
  22132. ' Field.Fly;',
  22133. ' Field.Run;',
  22134. ' Field.Jump;',
  22135. ' with Field do begin',
  22136. ' Fly;',
  22137. ' Run;',
  22138. ' Jump;',
  22139. ' end;',
  22140. 'end;',
  22141. 'class procedure THelper.Run;',
  22142. 'begin',
  22143. 'end;',
  22144. 'class procedure THelper.Jump;',
  22145. 'begin',
  22146. 'end;',
  22147. 'var',
  22148. ' b: TObject;',
  22149. 'begin',
  22150. ' b.Field.Fly;',
  22151. ' b.Field.Run;',
  22152. ' b.Field.Jump;',
  22153. ' with b do begin',
  22154. ' Field.Run;',
  22155. ' Field.Fly;',
  22156. ' Field.Jump;',
  22157. ' end;',
  22158. ' with b.Field do begin',
  22159. ' Run;',
  22160. ' Fly;',
  22161. ' Jump;',
  22162. ' end;',
  22163. '']);
  22164. ConvertProgram;
  22165. CheckSource('TestClassHelper_PassProperty',
  22166. LinesToStr([ // statements
  22167. 'rtl.createClass(this, "TObject", null, function () {',
  22168. ' this.$init = function () {',
  22169. ' this.FField = null;',
  22170. ' };',
  22171. ' this.$final = function () {',
  22172. ' this.FField = undefined;',
  22173. ' };',
  22174. '});',
  22175. 'rtl.createHelper(this, "THelper", null, function () {',
  22176. ' this.Fly = function () {',
  22177. ' $mod.THelper.Fly.call(this.FField);',
  22178. ' $mod.THelper.Run.call(this.FField.$class);',
  22179. ' $mod.THelper.Jump();',
  22180. ' var $with = this.FField;',
  22181. ' $mod.THelper.Fly.call($with);',
  22182. ' $mod.THelper.Run.call($with.$class);',
  22183. ' $mod.THelper.Jump();',
  22184. ' };',
  22185. ' this.Run = function () {',
  22186. ' };',
  22187. ' this.Jump = function () {',
  22188. ' };',
  22189. '});',
  22190. 'this.b = null;',
  22191. '']),
  22192. LinesToStr([ // $mod.$main
  22193. '$mod.THelper.Fly.call($mod.b.FField);',
  22194. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22195. '$mod.THelper.Jump();',
  22196. 'var $with = $mod.b;',
  22197. '$mod.THelper.Run.call($with.FField.$class);',
  22198. '$mod.THelper.Fly.call($with.FField);',
  22199. '$mod.THelper.Jump();',
  22200. 'var $with1 = $mod.b.FField;',
  22201. '$mod.THelper.Run.call($with1.$class);',
  22202. '$mod.THelper.Fly.call($with1);',
  22203. '$mod.THelper.Jump();',
  22204. '']));
  22205. end;
  22206. procedure TTestModule.TestExtClassHelper_ClassVar;
  22207. begin
  22208. StartProgram(false);
  22209. Add([
  22210. '{$modeswitch externalclass}',
  22211. 'type',
  22212. ' TExtA = class external name ''ExtObj''',
  22213. ' end;',
  22214. ' THelper = class helper for TExtA',
  22215. ' const',
  22216. ' One = 1;',
  22217. ' Two: word = 2;',
  22218. ' class var',
  22219. ' Glob: word;',
  22220. ' function Foo(w: word): word;',
  22221. ' class function Bar(w: word): word; static;',
  22222. ' end;',
  22223. 'function THelper.foo(w: word): word;',
  22224. 'begin',
  22225. ' Result:=w;',
  22226. ' Two:=One+w;',
  22227. ' Glob:=Glob;',
  22228. ' Result:=Self.Glob;',
  22229. ' Self.Glob:=Self.Glob;',
  22230. ' with Self do Glob:=Glob;',
  22231. 'end;',
  22232. 'class function THelper.bar(w: word): word;',
  22233. 'begin',
  22234. ' Result:=w;',
  22235. ' Two:=One;',
  22236. ' Glob:=Glob;',
  22237. 'end;',
  22238. 'var o: TExtA;',
  22239. 'begin',
  22240. ' texta.two:=texta.one;',
  22241. ' texta.Glob:=texta.Glob;',
  22242. ' with texta do begin',
  22243. ' two:=one;',
  22244. ' Glob:=Glob;',
  22245. ' end;',
  22246. ' o.two:=o.one;',
  22247. ' o.Glob:=o.Glob;',
  22248. ' with o do begin',
  22249. ' two:=one;',
  22250. ' Glob:=Glob;',
  22251. ' end;',
  22252. '']);
  22253. ConvertProgram;
  22254. CheckSource('TestExtClassHelper_ClassVar',
  22255. LinesToStr([ // statements
  22256. 'rtl.createHelper(this, "THelper", null, function () {',
  22257. ' this.One = 1;',
  22258. ' this.Two = 2;',
  22259. ' this.Glob = 0;',
  22260. ' this.Foo = function (w) {',
  22261. ' var Result = 0;',
  22262. ' Result = w;',
  22263. ' $mod.THelper.Two = 1 + w;',
  22264. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22265. ' Result = $mod.THelper.Glob;',
  22266. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22267. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22268. ' return Result;',
  22269. ' };',
  22270. ' this.Bar = function (w) {',
  22271. ' var Result = 0;',
  22272. ' Result = w;',
  22273. ' $mod.THelper.Two = 1;',
  22274. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22275. ' return Result;',
  22276. ' };',
  22277. '});',
  22278. 'this.o = null;',
  22279. '']),
  22280. LinesToStr([ // $mod.$main
  22281. '$mod.THelper.Two = 1;',
  22282. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22283. '$mod.THelper.Two = 1;',
  22284. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22285. '$mod.THelper.Two = 1;',
  22286. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22287. 'var $with = $mod.o;',
  22288. '$mod.THelper.Two = 1;',
  22289. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22290. '']));
  22291. end;
  22292. procedure TTestModule.TestExtClassHelper_Method_Call;
  22293. begin
  22294. StartProgram(false);
  22295. Add([
  22296. '{$modeswitch externalclass}',
  22297. 'type',
  22298. ' TFly = function(w: word): word of object;',
  22299. ' TExtA = class external name ''ExtObj''',
  22300. ' procedure Run(w: word = 10);',
  22301. ' end;',
  22302. ' THelper = class helper for TExtA',
  22303. ' function Foo(w: word = 1): word;',
  22304. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22305. ' end;',
  22306. 'var p: TFly;',
  22307. 'function THelper.foo(w: word): word;',
  22308. 'begin',
  22309. ' Run;',
  22310. ' Run();',
  22311. ' Run(11);',
  22312. ' Foo;',
  22313. ' Foo();',
  22314. ' Foo(12);',
  22315. ' Self.Foo;',
  22316. ' Self.Foo();',
  22317. ' Self.Foo(13);',
  22318. ' Fly;',
  22319. ' Fly();',
  22320. ' with Self do begin',
  22321. ' Foo;',
  22322. ' Foo();',
  22323. ' Foo(14);',
  22324. ' Fly;',
  22325. ' Fly();',
  22326. ' end;',
  22327. ' p:=@Fly;',
  22328. 'end;',
  22329. 'var Obj: TExtA;',
  22330. 'begin',
  22331. ' obj.Foo;',
  22332. ' obj.Foo();',
  22333. ' obj.Foo(21);',
  22334. ' obj.Fly;',
  22335. ' obj.Fly();',
  22336. ' with obj do begin',
  22337. ' Foo;',
  22338. ' Foo();',
  22339. ' Foo(22);',
  22340. ' Fly;',
  22341. ' Fly();',
  22342. ' end;',
  22343. ' p:[email protected];',
  22344. '']);
  22345. ConvertProgram;
  22346. CheckSource('TestExtClassHelper_Method_Call',
  22347. LinesToStr([ // statements
  22348. 'rtl.createHelper(this, "THelper", null, function () {',
  22349. ' this.Foo = function (w) {',
  22350. ' var Result = 0;',
  22351. ' this.Run(10);',
  22352. ' this.Run(10);',
  22353. ' this.Run(11);',
  22354. ' $mod.THelper.Foo.call(this, 1);',
  22355. ' $mod.THelper.Foo.call(this, 1);',
  22356. ' $mod.THelper.Foo.call(this, 12);',
  22357. ' $mod.THelper.Foo.call(this, 1);',
  22358. ' $mod.THelper.Foo.call(this, 1);',
  22359. ' $mod.THelper.Foo.call(this, 13);',
  22360. ' this.Fly(2);',
  22361. ' this.Fly(2);',
  22362. ' $mod.THelper.Foo.call(this, 1);',
  22363. ' $mod.THelper.Foo.call(this, 1);',
  22364. ' $mod.THelper.Foo.call(this, 14);',
  22365. ' this.Fly(2);',
  22366. ' this.Fly(2);',
  22367. ' $mod.p = rtl.createCallback(this, "Fly");',
  22368. ' return Result;',
  22369. ' };',
  22370. '});',
  22371. 'this.p = null;',
  22372. 'this.Obj = null;',
  22373. '']),
  22374. LinesToStr([ // $mod.$main
  22375. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22376. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22377. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22378. '$mod.Obj.Fly(2);',
  22379. '$mod.Obj.Fly(2);',
  22380. 'var $with = $mod.Obj;',
  22381. '$mod.THelper.Foo.call($with, 1);',
  22382. '$mod.THelper.Foo.call($with, 1);',
  22383. '$mod.THelper.Foo.call($with, 22);',
  22384. '$with.Fly(2);',
  22385. '$with.Fly(2);',
  22386. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22387. '']));
  22388. end;
  22389. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22390. begin
  22391. StartProgram(false);
  22392. Add([
  22393. '{$modeswitch externalclass}',
  22394. 'type',
  22395. ' TExtA = class external name ''ExtObj''',
  22396. ' procedure Run(w: word = 10);',
  22397. ' end;',
  22398. ' THelper = class helper for TExtA',
  22399. ' class procedure Fly;',
  22400. ' end;',
  22401. 'class procedure THelper.Fly;',
  22402. 'begin end;',
  22403. 'begin',
  22404. '']);
  22405. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22406. nHelperClassMethodForExtClassMustBeStatic);
  22407. ConvertProgram;
  22408. end;
  22409. procedure TTestModule.TestRecordHelper_ClassVar;
  22410. begin
  22411. StartProgram(false);
  22412. Add([
  22413. 'type',
  22414. ' TRec = record',
  22415. ' end;',
  22416. ' THelper = record helper for TRec',
  22417. ' const',
  22418. ' One = 1;',
  22419. ' Two: word = 2;',
  22420. ' class var',
  22421. ' Glob: word;',
  22422. ' function Foo(w: word): word;',
  22423. ' class function Bar(w: word): word; static;',
  22424. ' end;',
  22425. 'function THelper.foo(w: word): word;',
  22426. 'begin',
  22427. ' Result:=w;',
  22428. ' Two:=One+w;',
  22429. ' Glob:=Glob;',
  22430. ' Result:=Self.Glob;',
  22431. ' Self.Glob:=Self.Glob;',
  22432. ' with Self do Glob:=Glob;',
  22433. ' Self:=Self;',
  22434. 'end;',
  22435. 'class function THelper.bar(w: word): word;',
  22436. 'begin',
  22437. ' Result:=w;',
  22438. ' Two:=One;',
  22439. ' Glob:=Glob;',
  22440. 'end;',
  22441. 'var r: TRec;',
  22442. 'begin',
  22443. ' trec.two:=trec.one;',
  22444. ' trec.Glob:=trec.Glob;',
  22445. ' with trec do begin',
  22446. ' two:=one;',
  22447. ' Glob:=Glob;',
  22448. ' end;',
  22449. ' r.two:=r.one;',
  22450. ' r.Glob:=r.Glob;',
  22451. ' with r do begin',
  22452. ' two:=one;',
  22453. ' Glob:=Glob;',
  22454. ' end;',
  22455. '']);
  22456. ConvertProgram;
  22457. CheckSource('TestRecordHelper_ClassVar',
  22458. LinesToStr([ // statements
  22459. 'rtl.recNewT(this, "TRec", function () {',
  22460. ' this.$eq = function (b) {',
  22461. ' return true;',
  22462. ' };',
  22463. ' this.$assign = function (s) {',
  22464. ' return this;',
  22465. ' };',
  22466. '});',
  22467. 'rtl.createHelper(this, "THelper", null, function () {',
  22468. ' this.One = 1;',
  22469. ' this.Two = 2;',
  22470. ' this.Glob = 0;',
  22471. ' this.Foo = function (w) {',
  22472. ' var Result = 0;',
  22473. ' Result = w;',
  22474. ' $mod.THelper.Two = 1 + w;',
  22475. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22476. ' Result = $mod.THelper.Glob;',
  22477. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22478. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22479. ' this.$assign(this);',
  22480. ' return Result;',
  22481. ' };',
  22482. ' this.Bar = function (w) {',
  22483. ' var Result = 0;',
  22484. ' Result = w;',
  22485. ' $mod.THelper.Two = 1;',
  22486. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22487. ' return Result;',
  22488. ' };',
  22489. '});',
  22490. 'this.r = this.TRec.$new();',
  22491. '']),
  22492. LinesToStr([ // $mod.$main
  22493. '$mod.THelper.Two = 1;',
  22494. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22495. 'var $with = $mod.TRec;',
  22496. '$mod.THelper.Two = 1;',
  22497. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22498. '$mod.THelper.Two = 1;',
  22499. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22500. 'var $with1 = $mod.r;',
  22501. '$mod.THelper.Two = 1;',
  22502. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22503. '']));
  22504. end;
  22505. procedure TTestModule.TestRecordHelper_Method_Call;
  22506. begin
  22507. StartProgram(false);
  22508. Add([
  22509. '{$modeswitch AdvancedRecords}',
  22510. 'type',
  22511. ' TRec = record',
  22512. ' procedure Run(w: word = 10);',
  22513. ' end;',
  22514. ' THelper = record helper for TRec',
  22515. ' function Foo(w: word = 1): word;',
  22516. ' end;',
  22517. 'procedure TRec.Run(w: word);',
  22518. 'begin',
  22519. ' Foo;',
  22520. ' Foo();',
  22521. ' Foo(2);',
  22522. ' Self.Foo;',
  22523. ' Self.Foo();',
  22524. ' Self.Foo(3);',
  22525. ' with Self do begin',
  22526. ' Foo;',
  22527. ' Foo();',
  22528. ' Foo(4);',
  22529. ' end;',
  22530. 'end;',
  22531. 'function THelper.foo(w: word): word;',
  22532. 'begin',
  22533. ' Run;',
  22534. ' Run();',
  22535. ' Run(11);',
  22536. ' Foo;',
  22537. ' Foo();',
  22538. ' Foo(12);',
  22539. ' Self.Foo;',
  22540. ' Self.Foo();',
  22541. ' Self.Foo(13);',
  22542. ' with Self do begin',
  22543. ' Foo;',
  22544. ' Foo();',
  22545. ' Foo(14);',
  22546. ' end;',
  22547. 'end;',
  22548. 'var Rec: TRec;',
  22549. 'begin',
  22550. ' Rec.Foo;',
  22551. ' Rec.Foo();',
  22552. ' Rec.Foo(21);',
  22553. ' with Rec do begin',
  22554. ' Foo;',
  22555. ' Foo();',
  22556. ' Foo(22);',
  22557. ' end;',
  22558. '']);
  22559. ConvertProgram;
  22560. CheckSource('TestRecordHelper_Method_Call',
  22561. LinesToStr([ // statements
  22562. 'rtl.recNewT(this, "TRec", function () {',
  22563. ' this.$eq = function (b) {',
  22564. ' return true;',
  22565. ' };',
  22566. ' this.$assign = function (s) {',
  22567. ' return this;',
  22568. ' };',
  22569. ' this.Run = function (w) {',
  22570. ' $mod.THelper.Foo.call(this, 1);',
  22571. ' $mod.THelper.Foo.call(this, 1);',
  22572. ' $mod.THelper.Foo.call(this, 2);',
  22573. ' $mod.THelper.Foo.call(this, 1);',
  22574. ' $mod.THelper.Foo.call(this, 1);',
  22575. ' $mod.THelper.Foo.call(this, 3);',
  22576. ' $mod.THelper.Foo.call(this, 1);',
  22577. ' $mod.THelper.Foo.call(this, 1);',
  22578. ' $mod.THelper.Foo.call(this, 4);',
  22579. ' };',
  22580. '});',
  22581. 'rtl.createHelper(this, "THelper", null, function () {',
  22582. ' this.Foo = function (w) {',
  22583. ' var Result = 0;',
  22584. ' this.Run(10);',
  22585. ' this.Run(10);',
  22586. ' this.Run(11);',
  22587. ' $mod.THelper.Foo.call(this, 1);',
  22588. ' $mod.THelper.Foo.call(this, 1);',
  22589. ' $mod.THelper.Foo.call(this, 12);',
  22590. ' $mod.THelper.Foo.call(this, 1);',
  22591. ' $mod.THelper.Foo.call(this, 1);',
  22592. ' $mod.THelper.Foo.call(this, 13);',
  22593. ' $mod.THelper.Foo.call(this, 1);',
  22594. ' $mod.THelper.Foo.call(this, 1);',
  22595. ' $mod.THelper.Foo.call(this, 14);',
  22596. ' return Result;',
  22597. ' };',
  22598. '});',
  22599. 'this.Rec = this.TRec.$new();',
  22600. '']),
  22601. LinesToStr([ // $mod.$main
  22602. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22603. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22604. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22605. 'var $with = $mod.Rec;',
  22606. '$mod.THelper.Foo.call($with, 1);',
  22607. '$mod.THelper.Foo.call($with, 1);',
  22608. '$mod.THelper.Foo.call($with, 22);',
  22609. '']));
  22610. end;
  22611. procedure TTestModule.TestRecordHelper_Constructor;
  22612. begin
  22613. StartProgram(false);
  22614. Add([
  22615. '{$modeswitch AdvancedRecords}',
  22616. 'type',
  22617. ' TRec = record',
  22618. ' constructor Create(w: word);',
  22619. ' end;',
  22620. ' THelper = record helper for TRec',
  22621. ' constructor NewHlp(w: word);',
  22622. ' end;',
  22623. 'var',
  22624. ' Rec: TRec;',
  22625. 'constructor TRec.Create(w: word);',
  22626. 'begin',
  22627. ' NewHlp(2);', // normal call
  22628. ' trec.NewHlp(3);', // new instance
  22629. 'end;',
  22630. 'constructor THelper.NewHlp(w: word);',
  22631. 'begin',
  22632. ' create(2);', // normal call
  22633. ' trec.create(3);', // new instance
  22634. ' NewHlp(4);', // normal call
  22635. ' trec.NewHlp(5);', // new instance
  22636. 'end;',
  22637. 'begin',
  22638. ' rec.newhlp(2);', // normal call
  22639. ' with rec do newhlp(12);', // normal call
  22640. ' trec.newhlp(3);', // new instance
  22641. ' with trec do newhlp(13);', // new instance
  22642. '']);
  22643. ConvertProgram;
  22644. CheckSource('TestRecordHelper_Constructor',
  22645. LinesToStr([ // statements
  22646. 'rtl.recNewT(this, "TRec", function () {',
  22647. ' this.$eq = function (b) {',
  22648. ' return true;',
  22649. ' };',
  22650. ' this.$assign = function (s) {',
  22651. ' return this;',
  22652. ' };',
  22653. ' this.Create = function (w) {',
  22654. ' $mod.THelper.NewHlp.call(this, 2);',
  22655. ' $mod.THelper.$new("NewHlp", [3]);',
  22656. ' return this;',
  22657. ' };',
  22658. '});',
  22659. 'rtl.createHelper(this, "THelper", null, function () {',
  22660. ' this.NewHlp = function (w) {',
  22661. ' this.Create(2);',
  22662. ' $mod.TRec.$new().Create(3);',
  22663. ' $mod.THelper.NewHlp.call(this, 4);',
  22664. ' $mod.THelper.$new("NewHlp", [5]);',
  22665. ' return this;',
  22666. ' };',
  22667. ' this.$new = function (fn, args) {',
  22668. ' return this[fn].apply($mod.TRec.$new(), args);',
  22669. ' };',
  22670. '});',
  22671. 'this.Rec = this.TRec.$new();',
  22672. '']),
  22673. LinesToStr([ // $mod.$main
  22674. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22675. 'var $with = $mod.Rec;',
  22676. '$mod.THelper.NewHlp.call($with, 12);',
  22677. '$mod.THelper.$new("NewHlp", [3]);',
  22678. 'var $with1 = $mod.TRec;',
  22679. '$mod.THelper.$new("NewHlp", [13]);',
  22680. '']));
  22681. end;
  22682. procedure TTestModule.TestTypeHelper_ClassVar;
  22683. begin
  22684. StartProgram(false);
  22685. Add([
  22686. '{$modeswitch typehelpers}',
  22687. 'type',
  22688. ' THelper = type helper for byte',
  22689. ' const',
  22690. ' One = 1;',
  22691. ' Two: word = 2;',
  22692. ' class var',
  22693. ' Glob: word;',
  22694. ' function Foo(w: word): word;',
  22695. ' class function Bar(w: word): word; static;',
  22696. ' end;',
  22697. 'function THelper.foo(w: word): word;',
  22698. 'begin',
  22699. ' Result:=w;',
  22700. ' Two:=One+w;',
  22701. ' Glob:=Glob;',
  22702. ' Result:=Self.Glob;',
  22703. ' Self.Glob:=Self.Glob;',
  22704. ' with Self do Glob:=Glob;',
  22705. 'end;',
  22706. 'class function THelper.bar(w: word): word;',
  22707. 'begin',
  22708. ' Result:=w;',
  22709. ' Two:=One;',
  22710. ' Glob:=Glob;',
  22711. 'end;',
  22712. 'var b: byte;',
  22713. 'begin',
  22714. ' byte.two:=byte.one;',
  22715. ' byte.Glob:=byte.Glob;',
  22716. ' with byte do begin',
  22717. ' two:=one;',
  22718. ' Glob:=Glob;',
  22719. ' end;',
  22720. ' b.two:=b.one;',
  22721. ' b.Glob:=b.Glob;',
  22722. ' with b do begin',
  22723. ' two:=one;',
  22724. ' Glob:=Glob;',
  22725. ' end;',
  22726. '']);
  22727. ConvertProgram;
  22728. CheckSource('TestTypeHelper_ClassVar',
  22729. LinesToStr([ // statements
  22730. 'rtl.createHelper(this, "THelper", null, function () {',
  22731. ' this.One = 1;',
  22732. ' this.Two = 2;',
  22733. ' this.Glob = 0;',
  22734. ' this.Foo = function (w) {',
  22735. ' var Result = 0;',
  22736. ' Result = w;',
  22737. ' $mod.THelper.Two = 1 + w;',
  22738. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22739. ' Result = $mod.THelper.Glob;',
  22740. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22741. ' var $with = this.get();',
  22742. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22743. ' return Result;',
  22744. ' };',
  22745. ' this.Bar = function (w) {',
  22746. ' var Result = 0;',
  22747. ' Result = w;',
  22748. ' $mod.THelper.Two = 1;',
  22749. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22750. ' return Result;',
  22751. ' };',
  22752. '});',
  22753. 'this.b = 0;',
  22754. '']),
  22755. LinesToStr([ // $mod.$main
  22756. '$mod.THelper.Two = 1;',
  22757. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22758. '$mod.THelper.Two = 1;',
  22759. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22760. '$mod.THelper.Two = 1;',
  22761. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22762. 'var $with = $mod.b;',
  22763. '$mod.THelper.Two = 1;',
  22764. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22765. '']));
  22766. end;
  22767. procedure TTestModule.TestTypeHelper_PassResultElement;
  22768. begin
  22769. StartProgram(false);
  22770. Add([
  22771. '{$modeswitch typehelpers}',
  22772. 'type',
  22773. ' THelper = type helper for word',
  22774. ' procedure DoIt(e: byte = 123);',
  22775. ' class procedure DoSome(e: byte = 456); static;',
  22776. ' end;',
  22777. 'procedure THelper.DoIt(e: byte);',
  22778. 'begin',
  22779. 'end;',
  22780. 'class procedure THelper.DoSome(e: byte);',
  22781. 'begin',
  22782. 'end;',
  22783. 'function Foo(w: word): word;',
  22784. 'begin',
  22785. ' Result.DoIt;',
  22786. ' Result.DoIt();',
  22787. ' Result.DoSome;',
  22788. ' Result.DoSome();',
  22789. ' with Result do begin',
  22790. ' DoIt;',
  22791. ' DoIt();',
  22792. ' DoSome;',
  22793. ' DoSome();',
  22794. ' end;',
  22795. 'end;',
  22796. 'begin',
  22797. '']);
  22798. ConvertProgram;
  22799. CheckSource('TestTypeHelper_PassResultElement',
  22800. LinesToStr([ // statements
  22801. 'rtl.createHelper(this, "THelper", null, function () {',
  22802. ' this.DoIt = function (e) {',
  22803. ' };',
  22804. ' this.DoSome = function (e) {',
  22805. ' };',
  22806. '});',
  22807. 'this.Foo = function (w) {',
  22808. ' var Result = 0;',
  22809. ' $mod.THelper.DoIt.call({',
  22810. ' get: function () {',
  22811. ' return Result;',
  22812. ' },',
  22813. ' set: function (v) {',
  22814. ' Result = v;',
  22815. ' }',
  22816. ' }, 123);',
  22817. ' $mod.THelper.DoIt.call({',
  22818. ' get: function () {',
  22819. ' return Result;',
  22820. ' },',
  22821. ' set: function (v) {',
  22822. ' Result = v;',
  22823. ' }',
  22824. ' }, 123);',
  22825. ' $mod.THelper.DoSome(456);',
  22826. ' $mod.THelper.DoSome(456);',
  22827. ' $mod.THelper.DoIt.call({',
  22828. ' get: function () {',
  22829. ' return Result;',
  22830. ' },',
  22831. ' set: function (v) {',
  22832. ' Result = v;',
  22833. ' }',
  22834. ' }, 123);',
  22835. ' $mod.THelper.DoIt.call({',
  22836. ' get: function () {',
  22837. ' return Result;',
  22838. ' },',
  22839. ' set: function (v) {',
  22840. ' Result = v;',
  22841. ' }',
  22842. ' }, 123);',
  22843. ' $mod.THelper.DoSome(456);',
  22844. ' $mod.THelper.DoSome(456);',
  22845. ' return Result;',
  22846. '};',
  22847. '']),
  22848. LinesToStr([ // $mod.$main
  22849. '']));
  22850. end;
  22851. procedure TTestModule.TestTypeHelper_PassArgs;
  22852. begin
  22853. StartProgram(false);
  22854. Add([
  22855. '{$modeswitch typehelpers}',
  22856. 'type',
  22857. ' THelper = type helper for word',
  22858. ' procedure DoIt(e: byte = 123);',
  22859. ' end;',
  22860. 'procedure THelper.DoIt(e: byte);',
  22861. 'begin',
  22862. 'end;',
  22863. 'procedure FooDefault(a: word);',
  22864. 'begin',
  22865. ' a.DoIt;',
  22866. ' with a do DoIt;',
  22867. 'end;',
  22868. 'procedure FooConst(const a: word);',
  22869. 'begin',
  22870. ' a.DoIt;',
  22871. ' with a do DoIt;',
  22872. 'end;',
  22873. 'procedure FooVar(var a: word);',
  22874. 'begin',
  22875. ' a.DoIt;',
  22876. ' with a do DoIt;',
  22877. 'end;',
  22878. 'begin',
  22879. '']);
  22880. ConvertProgram;
  22881. CheckSource('TestTypeHelper_PassArgs',
  22882. LinesToStr([ // statements
  22883. 'rtl.createHelper(this, "THelper", null, function () {',
  22884. ' this.DoIt = function (e) {',
  22885. ' };',
  22886. '});',
  22887. 'this.FooDefault = function (a) {',
  22888. ' $mod.THelper.DoIt.call({',
  22889. ' get: function () {',
  22890. ' return a;',
  22891. ' },',
  22892. ' set: function (v) {',
  22893. ' a = v;',
  22894. ' }',
  22895. ' }, 123);',
  22896. ' $mod.THelper.DoIt.call({',
  22897. ' get: function () {',
  22898. ' return a;',
  22899. ' },',
  22900. ' set: function (v) {',
  22901. ' a = v;',
  22902. ' }',
  22903. ' }, 123);',
  22904. '};',
  22905. 'this.FooConst = function (a) {',
  22906. ' $mod.THelper.DoIt.call({',
  22907. ' get: function () {',
  22908. ' return a;',
  22909. ' },',
  22910. ' set: function (v) {',
  22911. ' rtl.raiseE("EPropReadOnly");',
  22912. ' }',
  22913. ' }, 123);',
  22914. ' $mod.THelper.DoIt.call({',
  22915. ' get: function () {',
  22916. ' return a;',
  22917. ' },',
  22918. ' set: function () {',
  22919. ' rtl.raiseE("EPropReadOnly");',
  22920. ' }',
  22921. ' }, 123);',
  22922. '};',
  22923. 'this.FooVar = function (a) {',
  22924. ' $mod.THelper.DoIt.call(a, 123);',
  22925. ' var $with = a.get();',
  22926. ' $mod.THelper.DoIt.call(a, 123);',
  22927. '};',
  22928. '']),
  22929. LinesToStr([ // $mod.$main
  22930. '']));
  22931. end;
  22932. procedure TTestModule.TestTypeHelper_PassVarConst;
  22933. begin
  22934. StartProgram(false);
  22935. Add([
  22936. '{$modeswitch typehelpers}',
  22937. 'type',
  22938. ' THelper = type helper for word',
  22939. ' procedure DoIt(e: byte = 123);',
  22940. ' end;',
  22941. 'procedure THelper.DoIt(e: byte);',
  22942. 'begin',
  22943. 'end;',
  22944. 'var a: word;',
  22945. 'const c: word = 2;',
  22946. '{$writeableconst off}',
  22947. 'const r: word = 3;',
  22948. 'begin',
  22949. ' a.DoIt;',
  22950. ' with a do DoIt;',
  22951. ' c.DoIt;',
  22952. ' with c do DoIt;',
  22953. ' r.DoIt;',
  22954. ' with r do DoIt;',
  22955. '']);
  22956. ConvertProgram;
  22957. CheckSource('TestTypeHelper_PassVarConst',
  22958. LinesToStr([ // statements
  22959. 'rtl.createHelper(this, "THelper", null, function () {',
  22960. ' this.DoIt = function (e) {',
  22961. ' };',
  22962. '});',
  22963. 'this.a = 0;',
  22964. 'this.c = 2;',
  22965. 'this.r = 3;',
  22966. '']),
  22967. LinesToStr([ // $mod.$main
  22968. '$mod.THelper.DoIt.call({',
  22969. ' p: $mod,',
  22970. ' get: function () {',
  22971. ' return this.p.a;',
  22972. ' },',
  22973. ' set: function (v) {',
  22974. ' this.p.a = v;',
  22975. ' }',
  22976. '}, 123);',
  22977. 'var $with = $mod.a;',
  22978. '$mod.THelper.DoIt.call({',
  22979. ' get: function () {',
  22980. ' return $with;',
  22981. ' },',
  22982. ' set: function (v) {',
  22983. ' $with = v;',
  22984. ' }',
  22985. '}, 123);',
  22986. '$mod.THelper.DoIt.call({',
  22987. ' p: $mod,',
  22988. ' get: function () {',
  22989. ' return this.p.c;',
  22990. ' },',
  22991. ' set: function (v) {',
  22992. ' this.p.c = v;',
  22993. ' }',
  22994. '}, 123);',
  22995. 'var $with1 = $mod.c;',
  22996. '$mod.THelper.DoIt.call({',
  22997. ' get: function () {',
  22998. ' return $with1;',
  22999. ' },',
  23000. ' set: function (v) {',
  23001. ' $with1 = v;',
  23002. ' }',
  23003. '}, 123);',
  23004. '$mod.THelper.DoIt.call({',
  23005. ' get: function () {',
  23006. ' return 3;',
  23007. ' },',
  23008. ' set: function (v) {',
  23009. ' rtl.raiseE("EPropReadOnly");',
  23010. ' }',
  23011. '}, 123);',
  23012. 'var $with2 = 3;',
  23013. ' $mod.THelper.DoIt.call({',
  23014. ' get: function () {',
  23015. ' return $with2;',
  23016. ' },',
  23017. ' set: function () {',
  23018. ' rtl.raiseE("EPropReadOnly");',
  23019. ' }',
  23020. ' }, 123);',
  23021. '']));
  23022. end;
  23023. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23024. begin
  23025. StartProgram(false);
  23026. Add([
  23027. '{$modeswitch typehelpers}',
  23028. 'type',
  23029. ' THelper = type helper for word',
  23030. ' procedure DoIt(e: byte = 123);',
  23031. ' end;',
  23032. 'procedure THelper.DoIt(e: byte);',
  23033. 'begin',
  23034. 'end;',
  23035. 'function Foo(b: byte = 1): word;',
  23036. 'begin',
  23037. 'end;',
  23038. 'begin',
  23039. ' Foo.DoIt;',
  23040. ' Foo().DoIt;',
  23041. ' with Foo do DoIt;',
  23042. ' with Foo() do DoIt;',
  23043. '']);
  23044. ConvertProgram;
  23045. CheckSource('TestTypeHelper_PassFuncResult',
  23046. LinesToStr([ // statements
  23047. 'rtl.createHelper(this, "THelper", null, function () {',
  23048. ' this.DoIt = function (e) {',
  23049. ' };',
  23050. '});',
  23051. 'this.Foo = function (b) {',
  23052. ' var Result = 0;',
  23053. ' return Result;',
  23054. '};',
  23055. '']),
  23056. LinesToStr([ // $mod.$main
  23057. '$mod.THelper.DoIt.call({',
  23058. ' a: $mod.Foo(1),',
  23059. ' get: function () {',
  23060. ' return this.a;',
  23061. ' },',
  23062. ' set: function (v) {',
  23063. ' this.a = v;',
  23064. ' }',
  23065. '}, 123);',
  23066. '$mod.THelper.DoIt.call({',
  23067. ' a: $mod.Foo(1),',
  23068. ' get: function () {',
  23069. ' return this.a;',
  23070. ' },',
  23071. ' set: function (v) {',
  23072. ' this.a = v;',
  23073. ' }',
  23074. '}, 123);',
  23075. 'var $with = $mod.Foo(1);',
  23076. '$mod.THelper.DoIt.call({',
  23077. ' get: function () {',
  23078. ' return $with;',
  23079. ' },',
  23080. ' set: function (v) {',
  23081. ' $with = v;',
  23082. ' }',
  23083. '}, 123);',
  23084. 'var $with1 = $mod.Foo(1);',
  23085. '$mod.THelper.DoIt.call({',
  23086. ' get: function () {',
  23087. ' return $with1;',
  23088. ' },',
  23089. ' set: function (v) {',
  23090. ' $with1 = v;',
  23091. ' }',
  23092. '}, 123);',
  23093. '']));
  23094. end;
  23095. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23096. begin
  23097. StartProgram(false);
  23098. Add([
  23099. '{$modeswitch typehelpers}',
  23100. 'type',
  23101. ' TObject = class',
  23102. ' FField: word;',
  23103. ' procedure SetField(Value: word);',
  23104. ' property Field: word read FField write SetField;',
  23105. ' end;',
  23106. ' THelper = type helper for word',
  23107. ' procedure Fly;',
  23108. ' class procedure Run; static;',
  23109. ' end;',
  23110. 'procedure TObject.SetField(Value: word);',
  23111. 'begin',
  23112. ' Field.Fly;',
  23113. ' Field.Run;',
  23114. ' Self.Field.Fly;',
  23115. ' Self.Field.Run;',
  23116. ' with Self do begin',
  23117. ' Field.Fly;',
  23118. ' Field.Run;',
  23119. ' end;',
  23120. ' with Self.Field do begin',
  23121. ' Fly;',
  23122. ' Run;',
  23123. ' end;',
  23124. 'end;',
  23125. 'procedure THelper.Fly;',
  23126. 'begin',
  23127. 'end;',
  23128. 'class procedure THelper.Run;',
  23129. 'begin',
  23130. 'end;',
  23131. 'var',
  23132. ' o: TObject;',
  23133. 'begin',
  23134. ' o.Field.Fly;',
  23135. ' o.Field.Run;',
  23136. ' with o do begin',
  23137. ' Field.Fly;',
  23138. ' Field.Run;',
  23139. ' end;',
  23140. ' with o.Field do begin',
  23141. ' Fly;',
  23142. ' Run;',
  23143. ' end;',
  23144. '']);
  23145. ConvertProgram;
  23146. CheckSource('TestTypeHelper_PassPropertyField',
  23147. LinesToStr([ // statements
  23148. 'rtl.createClass(this, "TObject", null, function () {',
  23149. ' this.$init = function () {',
  23150. ' this.FField = 0;',
  23151. ' };',
  23152. ' this.$final = function () {',
  23153. ' };',
  23154. ' this.SetField = function (Value) {',
  23155. ' $mod.THelper.Fly.call({',
  23156. ' p: this,',
  23157. ' get: function () {',
  23158. ' return this.p.FField;',
  23159. ' },',
  23160. ' set: function (v) {',
  23161. ' this.p.FField = v;',
  23162. ' }',
  23163. ' });',
  23164. ' $mod.THelper.Run();',
  23165. ' $mod.THelper.Fly.call({',
  23166. ' p: this,',
  23167. ' get: function () {',
  23168. ' return this.p.FField;',
  23169. ' },',
  23170. ' set: function (v) {',
  23171. ' this.p.FField = v;',
  23172. ' }',
  23173. ' });',
  23174. ' $mod.THelper.Run();',
  23175. ' $mod.THelper.Fly.call({',
  23176. ' p: this,',
  23177. ' get: function () {',
  23178. ' return this.p.FField;',
  23179. ' },',
  23180. ' set: function (v) {',
  23181. ' this.p.FField = v;',
  23182. ' }',
  23183. ' });',
  23184. ' $mod.THelper.Run();',
  23185. ' var $with = this.FField;',
  23186. ' $mod.THelper.Fly.call({',
  23187. ' get: function () {',
  23188. ' return $with;',
  23189. ' },',
  23190. ' set: function (v) {',
  23191. ' $with = v;',
  23192. ' }',
  23193. ' });',
  23194. ' $mod.THelper.Run();',
  23195. ' };',
  23196. '});',
  23197. 'rtl.createHelper(this, "THelper", null, function () {',
  23198. ' this.Fly = function () {',
  23199. ' };',
  23200. ' this.Run = function () {',
  23201. ' };',
  23202. '});',
  23203. 'this.o = null;',
  23204. '']),
  23205. LinesToStr([ // $mod.$main
  23206. '$mod.THelper.Fly.call({',
  23207. ' p: $mod.o,',
  23208. ' get: function () {',
  23209. ' return this.p.FField;',
  23210. ' },',
  23211. ' set: function (v) {',
  23212. ' this.p.FField = v;',
  23213. ' }',
  23214. '});',
  23215. '$mod.THelper.Run();',
  23216. 'var $with = $mod.o;',
  23217. '$mod.THelper.Fly.call({',
  23218. ' p: $with,',
  23219. ' get: function () {',
  23220. ' return this.p.FField;',
  23221. ' },',
  23222. ' set: function (v) {',
  23223. ' this.p.FField = v;',
  23224. ' }',
  23225. '});',
  23226. '$mod.THelper.Run();',
  23227. 'var $with1 = $mod.o.FField;',
  23228. '$mod.THelper.Fly.call({',
  23229. ' get: function () {',
  23230. ' return $with1;',
  23231. ' },',
  23232. ' set: function (v) {',
  23233. ' $with1 = v;',
  23234. ' }',
  23235. '});',
  23236. '$mod.THelper.Run();',
  23237. '']));
  23238. end;
  23239. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23240. begin
  23241. StartProgram(false);
  23242. Add([
  23243. '{$modeswitch typehelpers}',
  23244. 'type',
  23245. ' TObject = class',
  23246. ' FField: word;',
  23247. ' function GetField: word;',
  23248. ' property Field: word read GetField write FField;',
  23249. ' end;',
  23250. ' THelper = type helper for word',
  23251. ' procedure Fly;',
  23252. ' class procedure Run; static;',
  23253. ' end;',
  23254. 'function TObject.GetField: word;',
  23255. 'begin',
  23256. ' Field.Fly;',
  23257. ' Field.Run;',
  23258. ' Self.Field.Fly;',
  23259. ' Self.Field.Run;',
  23260. ' with Self do begin',
  23261. ' Field.Fly;',
  23262. ' Field.Run;',
  23263. ' end;',
  23264. ' with Self.Field do begin',
  23265. ' Fly;',
  23266. ' Run;',
  23267. ' end;',
  23268. 'end;',
  23269. 'procedure THelper.Fly;',
  23270. 'begin',
  23271. 'end;',
  23272. 'class procedure THelper.Run;',
  23273. 'begin',
  23274. 'end;',
  23275. 'var',
  23276. ' o: TObject;',
  23277. 'begin',
  23278. ' o.Field.Fly;',
  23279. ' o.Field.Run;',
  23280. ' with o do begin',
  23281. ' Field.Fly;',
  23282. ' Field.Run;',
  23283. ' end;',
  23284. ' with o.Field do begin',
  23285. ' Fly;',
  23286. ' Run;',
  23287. ' end;',
  23288. '']);
  23289. ConvertProgram;
  23290. CheckSource('TestTypeHelper_PassPropertyGetter',
  23291. LinesToStr([ // statements
  23292. 'rtl.createClass(this, "TObject", null, function () {',
  23293. ' this.$init = function () {',
  23294. ' this.FField = 0;',
  23295. ' };',
  23296. ' this.$final = function () {',
  23297. ' };',
  23298. ' this.GetField = function () {',
  23299. ' var Result = 0;',
  23300. ' $mod.THelper.Fly.call({',
  23301. ' p: this.GetField(),',
  23302. ' get: function () {',
  23303. ' return this.p;',
  23304. ' },',
  23305. ' set: function (v) {',
  23306. ' this.p = v;',
  23307. ' }',
  23308. ' });',
  23309. ' $mod.THelper.Run();',
  23310. ' $mod.THelper.Fly.call({',
  23311. ' p: this.GetField(),',
  23312. ' get: function () {',
  23313. ' return this.p;',
  23314. ' },',
  23315. ' set: function (v) {',
  23316. ' this.p = v;',
  23317. ' }',
  23318. ' });',
  23319. ' $mod.THelper.Run();',
  23320. ' $mod.THelper.Fly.call({',
  23321. ' p: this.GetField(),',
  23322. ' get: function () {',
  23323. ' return this.p;',
  23324. ' },',
  23325. ' set: function (v) {',
  23326. ' this.p = v;',
  23327. ' }',
  23328. ' });',
  23329. ' $mod.THelper.Run();',
  23330. ' var $with = this.GetField();',
  23331. ' $mod.THelper.Fly.call({',
  23332. ' get: function () {',
  23333. ' return $with;',
  23334. ' },',
  23335. ' set: function (v) {',
  23336. ' $with = v;',
  23337. ' }',
  23338. ' });',
  23339. ' $mod.THelper.Run();',
  23340. ' return Result;',
  23341. ' };',
  23342. '});',
  23343. 'rtl.createHelper(this, "THelper", null, function () {',
  23344. ' this.Fly = function () {',
  23345. ' };',
  23346. ' this.Run = function () {',
  23347. ' };',
  23348. '});',
  23349. 'this.o = null;',
  23350. '']),
  23351. LinesToStr([ // $mod.$main
  23352. '$mod.THelper.Fly.call({',
  23353. ' p: $mod.o.GetField(),',
  23354. ' get: function () {',
  23355. ' return this.p;',
  23356. ' },',
  23357. ' set: function (v) {',
  23358. ' this.p = v;',
  23359. ' }',
  23360. '});',
  23361. '$mod.THelper.Run();',
  23362. 'var $with = $mod.o;',
  23363. '$mod.THelper.Fly.call({',
  23364. ' p: $with.GetField(),',
  23365. ' get: function () {',
  23366. ' return this.p;',
  23367. ' },',
  23368. ' set: function (v) {',
  23369. ' this.p = v;',
  23370. ' }',
  23371. '});',
  23372. '$mod.THelper.Run();',
  23373. 'var $with1 = $mod.o.GetField();',
  23374. '$mod.THelper.Fly.call({',
  23375. ' get: function () {',
  23376. ' return $with1;',
  23377. ' },',
  23378. ' set: function (v) {',
  23379. ' $with1 = v;',
  23380. ' }',
  23381. '});',
  23382. '$mod.THelper.Run();',
  23383. '']));
  23384. end;
  23385. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23386. begin
  23387. StartProgram(false);
  23388. Add([
  23389. '{$modeswitch typehelpers}',
  23390. 'type',
  23391. ' TObject = class',
  23392. ' class var FField: word;',
  23393. ' class procedure SetField(Value: word);',
  23394. ' class property Field: word read FField write SetField;',
  23395. ' end;',
  23396. ' THelper = type helper for word',
  23397. ' procedure Fly(n: byte);',
  23398. ' end;',
  23399. 'class procedure TObject.SetField(Value: word);',
  23400. 'begin',
  23401. ' Field.Fly(1);',
  23402. ' Self.Field.Fly(2);',
  23403. ' with Self do Field.Fly(3);',
  23404. ' with Self.Field do Fly(4);',
  23405. ' TObject.Field.Fly(5);',
  23406. ' with TObject do Field.Fly(6);',
  23407. ' with TObject.Field do Fly(7);',
  23408. 'end;',
  23409. 'procedure THelper.Fly(n: byte);',
  23410. 'begin',
  23411. 'end;',
  23412. 'var',
  23413. ' o: TObject;',
  23414. 'begin',
  23415. ' o.Field.Fly(11);',
  23416. ' with o do Field.Fly(12);',
  23417. ' with o.Field do Fly(13);',
  23418. ' TObject.Field.Fly(14);',
  23419. ' with TObject do Field.Fly(15);',
  23420. ' with TObject.Field do Fly(16);',
  23421. '']);
  23422. ConvertProgram;
  23423. CheckSource('TestTypeHelper_PassClassPropertyField',
  23424. LinesToStr([ // statements
  23425. 'rtl.createClass(this, "TObject", null, function () {',
  23426. ' this.FField = 0;',
  23427. ' this.$init = function () {',
  23428. ' };',
  23429. ' this.$final = function () {',
  23430. ' };',
  23431. ' this.SetField = function (Value) {',
  23432. ' $mod.THelper.Fly.call({',
  23433. ' p: this,',
  23434. ' get: function () {',
  23435. ' return this.p.FField;',
  23436. ' },',
  23437. ' set: function (v) {',
  23438. ' $mod.TObject.FField = v;',
  23439. ' }',
  23440. ' }, 1);',
  23441. ' $mod.THelper.Fly.call({',
  23442. ' p: this,',
  23443. ' get: function () {',
  23444. ' return this.p.FField;',
  23445. ' },',
  23446. ' set: function (v) {',
  23447. ' $mod.TObject.FField = v;',
  23448. ' }',
  23449. ' }, 2);',
  23450. ' $mod.THelper.Fly.call({',
  23451. ' p: this,',
  23452. ' get: function () {',
  23453. ' return this.p.FField;',
  23454. ' },',
  23455. ' set: function (v) {',
  23456. ' $mod.TObject.FField = v;',
  23457. ' }',
  23458. ' }, 3);',
  23459. ' var $with = this.FField;',
  23460. ' $mod.THelper.Fly.call({',
  23461. ' get: function () {',
  23462. ' return $with;',
  23463. ' },',
  23464. ' set: function (v) {',
  23465. ' $with = v;',
  23466. ' }',
  23467. ' }, 4);',
  23468. ' $mod.THelper.Fly.call({',
  23469. ' p: $mod.TObject,',
  23470. ' get: function () {',
  23471. ' return this.p.FField;',
  23472. ' },',
  23473. ' set: function (v) {',
  23474. ' $mod.TObject.FField = v;',
  23475. ' }',
  23476. ' }, 5);',
  23477. ' var $with1 = $mod.TObject;',
  23478. ' $mod.THelper.Fly.call({',
  23479. ' p: $with1,',
  23480. ' get: function () {',
  23481. ' return this.p.FField;',
  23482. ' },',
  23483. ' set: function (v) {',
  23484. ' $mod.TObject.FField = v;',
  23485. ' }',
  23486. ' }, 6);',
  23487. ' var $with2 = $mod.TObject.FField;',
  23488. ' $mod.THelper.Fly.call({',
  23489. ' get: function () {',
  23490. ' return $with2;',
  23491. ' },',
  23492. ' set: function (v) {',
  23493. ' $with2 = v;',
  23494. ' }',
  23495. ' }, 7);',
  23496. ' };',
  23497. '});',
  23498. 'rtl.createHelper(this, "THelper", null, function () {',
  23499. ' this.Fly = function (n) {',
  23500. ' };',
  23501. '});',
  23502. 'this.o = null;',
  23503. '']),
  23504. LinesToStr([ // $mod.$main
  23505. '$mod.THelper.Fly.call({',
  23506. ' p: $mod.o,',
  23507. ' get: function () {',
  23508. ' return this.p.FField;',
  23509. ' },',
  23510. ' set: function (v) {',
  23511. ' $mod.TObject.FField = v;',
  23512. ' }',
  23513. '}, 11);',
  23514. 'var $with = $mod.o;',
  23515. '$mod.THelper.Fly.call({',
  23516. ' p: $with,',
  23517. ' get: function () {',
  23518. ' return this.p.FField;',
  23519. ' },',
  23520. ' set: function (v) {',
  23521. ' $mod.TObject.FField = v;',
  23522. ' }',
  23523. '}, 12);',
  23524. 'var $with1 = $mod.o.FField;',
  23525. '$mod.THelper.Fly.call({',
  23526. ' get: function () {',
  23527. ' return $with1;',
  23528. ' },',
  23529. ' set: function (v) {',
  23530. ' $with1 = v;',
  23531. ' }',
  23532. '}, 13);',
  23533. '$mod.THelper.Fly.call({',
  23534. ' p: $mod.TObject,',
  23535. ' get: function () {',
  23536. ' return this.p.FField;',
  23537. ' },',
  23538. ' set: function (v) {',
  23539. ' $mod.TObject.FField = v;',
  23540. ' }',
  23541. '}, 14);',
  23542. 'var $with2 = $mod.TObject;',
  23543. '$mod.THelper.Fly.call({',
  23544. ' p: $with2,',
  23545. ' get: function () {',
  23546. ' return this.p.FField;',
  23547. ' },',
  23548. ' set: function (v) {',
  23549. ' $mod.TObject.FField = v;',
  23550. ' }',
  23551. '}, 15);',
  23552. 'var $with3 = $mod.TObject.FField;',
  23553. '$mod.THelper.Fly.call({',
  23554. ' get: function () {',
  23555. ' return $with3;',
  23556. ' },',
  23557. ' set: function (v) {',
  23558. ' $with3 = v;',
  23559. ' }',
  23560. '}, 16);',
  23561. '']));
  23562. end;
  23563. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23564. begin
  23565. StartProgram(false);
  23566. Add([
  23567. '{$modeswitch typehelpers}',
  23568. 'type',
  23569. ' TObject = class',
  23570. ' class var FField: word;',
  23571. ' class function GetField: word; static;',
  23572. ' class property Field: word read GetField write FField;',
  23573. ' end;',
  23574. ' THelper = type helper for word',
  23575. ' procedure Fly(n: byte);',
  23576. ' end;',
  23577. 'class function TObject.GetField: word;',
  23578. 'begin',
  23579. ' Field.Fly(1);',
  23580. ' TObject.Field.Fly(5);',
  23581. ' with TObject do Field.Fly(6);',
  23582. ' with TObject.Field do Fly(7);',
  23583. 'end;',
  23584. 'procedure THelper.Fly(n: byte);',
  23585. 'begin',
  23586. 'end;',
  23587. 'var',
  23588. ' o: TObject;',
  23589. 'begin',
  23590. ' o.Field.Fly(11);',
  23591. ' with o do Field.Fly(12);',
  23592. ' with o.Field do Fly(13);',
  23593. '']);
  23594. ConvertProgram;
  23595. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23596. LinesToStr([ // statements
  23597. 'rtl.createClass(this, "TObject", null, function () {',
  23598. ' this.FField = 0;',
  23599. ' this.$init = function () {',
  23600. ' };',
  23601. ' this.$final = function () {',
  23602. ' };',
  23603. ' this.GetField = function () {',
  23604. ' var Result = 0;',
  23605. ' $mod.THelper.Fly.call({',
  23606. ' p: $mod.TObject.GetField(),',
  23607. ' get: function () {',
  23608. ' return this.p;',
  23609. ' },',
  23610. ' set: function (v) {',
  23611. ' this.p = v;',
  23612. ' }',
  23613. ' }, 1);',
  23614. ' $mod.THelper.Fly.call({',
  23615. ' p: $mod.TObject.GetField(),',
  23616. ' get: function () {',
  23617. ' return this.p;',
  23618. ' },',
  23619. ' set: function (v) {',
  23620. ' this.p = v;',
  23621. ' }',
  23622. ' }, 5);',
  23623. ' var $with = $mod.TObject;',
  23624. ' $mod.THelper.Fly.call({',
  23625. ' p: $with.GetField(),',
  23626. ' get: function () {',
  23627. ' return this.p;',
  23628. ' },',
  23629. ' set: function (v) {',
  23630. ' this.p = v;',
  23631. ' }',
  23632. ' }, 6);',
  23633. ' var $with1 = $mod.TObject.GetField();',
  23634. ' $mod.THelper.Fly.call({',
  23635. ' get: function () {',
  23636. ' return $with1;',
  23637. ' },',
  23638. ' set: function (v) {',
  23639. ' $with1 = v;',
  23640. ' }',
  23641. ' }, 7);',
  23642. ' return Result;',
  23643. ' };',
  23644. '});',
  23645. 'rtl.createHelper(this, "THelper", null, function () {',
  23646. ' this.Fly = function (n) {',
  23647. ' };',
  23648. '});',
  23649. 'this.o = null;',
  23650. '']),
  23651. LinesToStr([ // $mod.$main
  23652. '$mod.THelper.Fly.call({',
  23653. ' p: $mod.o.GetField(),',
  23654. ' get: function () {',
  23655. ' return this.p;',
  23656. ' },',
  23657. ' set: function (v) {',
  23658. ' this.p = v;',
  23659. ' }',
  23660. '}, 11);',
  23661. 'var $with = $mod.o;',
  23662. '$mod.THelper.Fly.call({',
  23663. ' p: $with.GetField(),',
  23664. ' get: function () {',
  23665. ' return this.p;',
  23666. ' },',
  23667. ' set: function (v) {',
  23668. ' this.p = v;',
  23669. ' }',
  23670. '}, 12);',
  23671. 'var $with1 = $mod.o.GetField();',
  23672. '$mod.THelper.Fly.call({',
  23673. ' get: function () {',
  23674. ' return $with1;',
  23675. ' },',
  23676. ' set: function (v) {',
  23677. ' $with1 = v;',
  23678. ' }',
  23679. '}, 13);',
  23680. '']));
  23681. end;
  23682. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23683. begin
  23684. StartProgram(false);
  23685. Add([
  23686. '{$modeswitch typehelpers}',
  23687. 'type',
  23688. ' TObject = class',
  23689. ' class var FField: word;',
  23690. ' class function GetField: word;',
  23691. ' class property Field: word read GetField write FField;',
  23692. ' end;',
  23693. ' TClass = class of TObject;',
  23694. ' THelper = type helper for word',
  23695. ' procedure Fly(n: byte);',
  23696. ' end;',
  23697. 'class function TObject.GetField: word;',
  23698. 'begin',
  23699. ' Field.Fly(1);',
  23700. ' Self.Field.Fly(5);',
  23701. ' with Self do Field.Fly(6);',
  23702. ' with Self.Field do Fly(7);',
  23703. 'end;',
  23704. 'procedure THelper.Fly(n: byte);',
  23705. 'begin',
  23706. 'end;',
  23707. 'var',
  23708. ' o: TObject;',
  23709. ' c: TClass;',
  23710. 'begin',
  23711. ' o.Field.Fly(11);',
  23712. ' with o do Field.Fly(12);',
  23713. ' with o.Field do Fly(13);',
  23714. ' c.Field.Fly(14);',
  23715. ' with c do Field.Fly(15);',
  23716. ' with c.Field do Fly(16);',
  23717. '']);
  23718. ConvertProgram;
  23719. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23720. LinesToStr([ // statements
  23721. 'rtl.createClass(this, "TObject", null, function () {',
  23722. ' this.FField = 0;',
  23723. ' this.$init = function () {',
  23724. ' };',
  23725. ' this.$final = function () {',
  23726. ' };',
  23727. ' this.GetField = function () {',
  23728. ' var Result = 0;',
  23729. ' $mod.THelper.Fly.call({',
  23730. ' p: this.GetField(),',
  23731. ' get: function () {',
  23732. ' return this.p;',
  23733. ' },',
  23734. ' set: function (v) {',
  23735. ' this.p = v;',
  23736. ' }',
  23737. ' }, 1);',
  23738. ' $mod.THelper.Fly.call({',
  23739. ' p: this.GetField(),',
  23740. ' get: function () {',
  23741. ' return this.p;',
  23742. ' },',
  23743. ' set: function (v) {',
  23744. ' this.p = v;',
  23745. ' }',
  23746. ' }, 5);',
  23747. ' $mod.THelper.Fly.call({',
  23748. ' p: this.GetField(),',
  23749. ' get: function () {',
  23750. ' return this.p;',
  23751. ' },',
  23752. ' set: function (v) {',
  23753. ' this.p = v;',
  23754. ' }',
  23755. ' }, 6);',
  23756. ' var $with = this.GetField();',
  23757. ' $mod.THelper.Fly.call({',
  23758. ' get: function () {',
  23759. ' return $with;',
  23760. ' },',
  23761. ' set: function (v) {',
  23762. ' $with = v;',
  23763. ' }',
  23764. ' }, 7);',
  23765. ' return Result;',
  23766. ' };',
  23767. '});',
  23768. 'rtl.createHelper(this, "THelper", null, function () {',
  23769. ' this.Fly = function (n) {',
  23770. ' };',
  23771. '});',
  23772. 'this.o = null;',
  23773. 'this.c = null;',
  23774. '']),
  23775. LinesToStr([ // $mod.$main
  23776. '$mod.THelper.Fly.call({',
  23777. ' p: $mod.o.$class.GetField(),',
  23778. ' get: function () {',
  23779. ' return this.p;',
  23780. ' },',
  23781. ' set: function (v) {',
  23782. ' this.p = v;',
  23783. ' }',
  23784. '}, 11);',
  23785. 'var $with = $mod.o;',
  23786. '$mod.THelper.Fly.call({',
  23787. ' p: $with.$class.GetField(),',
  23788. ' get: function () {',
  23789. ' return this.p;',
  23790. ' },',
  23791. ' set: function (v) {',
  23792. ' this.p = v;',
  23793. ' }',
  23794. '}, 12);',
  23795. 'var $with1 = $mod.o.$class.GetField();',
  23796. '$mod.THelper.Fly.call({',
  23797. ' get: function () {',
  23798. ' return $with1;',
  23799. ' },',
  23800. ' set: function (v) {',
  23801. ' $with1 = v;',
  23802. ' }',
  23803. '}, 13);',
  23804. '$mod.THelper.Fly.call({',
  23805. ' p: $mod.c.GetField(),',
  23806. ' get: function () {',
  23807. ' return this.p;',
  23808. ' },',
  23809. ' set: function (v) {',
  23810. ' this.p = v;',
  23811. ' }',
  23812. '}, 14);',
  23813. 'var $with2 = $mod.c;',
  23814. '$mod.THelper.Fly.call({',
  23815. ' p: $with2.GetField(),',
  23816. ' get: function () {',
  23817. ' return this.p;',
  23818. ' },',
  23819. ' set: function (v) {',
  23820. ' this.p = v;',
  23821. ' }',
  23822. '}, 15);',
  23823. 'var $with3 = $mod.c.GetField();',
  23824. '$mod.THelper.Fly.call({',
  23825. ' get: function () {',
  23826. ' return $with3;',
  23827. ' },',
  23828. ' set: function (v) {',
  23829. ' $with3 = v;',
  23830. ' }',
  23831. '}, 16);',
  23832. '']));
  23833. end;
  23834. procedure TTestModule.TestTypeHelper_Property;
  23835. begin
  23836. StartProgram(false);
  23837. Add([
  23838. '{$modeswitch typehelpers}',
  23839. 'type',
  23840. ' THelper = type helper for word',
  23841. ' function GetSize: longint;',
  23842. ' procedure SetSize(Value: longint);',
  23843. ' property Size: longint read GetSize write SetSize;',
  23844. ' end;',
  23845. 'function THelper.GetSize: longint;',
  23846. 'begin',
  23847. ' Result:=Size+1;',
  23848. ' Size:=2;',
  23849. ' Result:=Self.Size+3;',
  23850. ' Self.Size:=4;',
  23851. ' with Self do begin',
  23852. ' Result:=Size+5;',
  23853. ' Size:=6;',
  23854. ' end;',
  23855. 'end;',
  23856. 'procedure THelper.SetSize(Value: longint);',
  23857. 'begin',
  23858. 'end;',
  23859. 'var w: word;',
  23860. 'begin',
  23861. ' w:=w.Size+7;',
  23862. ' w.Size:=w+8;',
  23863. ' with w do begin',
  23864. ' w:=Size+9;',
  23865. ' Size:=w+10;',
  23866. ' end;',
  23867. '']);
  23868. ConvertProgram;
  23869. CheckSource('TestTypeHelper_Property',
  23870. LinesToStr([ // statements
  23871. 'rtl.createHelper(this, "THelper", null, function () {',
  23872. ' this.GetSize = function () {',
  23873. ' var Result = 0;',
  23874. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23875. ' $mod.THelper.SetSize.call(this, 2);',
  23876. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23877. ' $mod.THelper.SetSize.call(this, 4);',
  23878. ' var $with = this.get();',
  23879. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23880. ' $mod.THelper.SetSize.call(this, 6);',
  23881. ' return Result;',
  23882. ' };',
  23883. ' this.SetSize = function (Value) {',
  23884. ' };',
  23885. '});',
  23886. 'this.w = 0;',
  23887. '']),
  23888. LinesToStr([ // $mod.$main
  23889. '$mod.w = $mod.THelper.GetSize.call({',
  23890. ' p: $mod,',
  23891. ' get: function () {',
  23892. ' return this.p.w;',
  23893. ' },',
  23894. ' set: function (v) {',
  23895. ' this.p.w = v;',
  23896. ' }',
  23897. '}) + 7;',
  23898. '$mod.THelper.SetSize.call({',
  23899. ' p: $mod,',
  23900. ' get: function () {',
  23901. ' return this.p.w;',
  23902. ' },',
  23903. ' set: function (v) {',
  23904. ' this.p.w = v;',
  23905. ' }',
  23906. '}, $mod.w + 8);',
  23907. 'var $with = $mod.w;',
  23908. '$mod.w = $mod.THelper.GetSize.call({',
  23909. ' get: function () {',
  23910. ' return $with;',
  23911. ' },',
  23912. ' set: function (v) {',
  23913. ' $with = v;',
  23914. ' }',
  23915. '}) + 9;',
  23916. '$mod.THelper.SetSize.call({',
  23917. ' get: function () {',
  23918. ' return $with;',
  23919. ' },',
  23920. ' set: function (v) {',
  23921. ' $with = v;',
  23922. ' }',
  23923. '}, $mod.w + 10);',
  23924. '']));
  23925. end;
  23926. procedure TTestModule.TestTypeHelper_Property_Array;
  23927. begin
  23928. StartProgram(false);
  23929. Add([
  23930. '{$modeswitch typehelpers}',
  23931. 'type',
  23932. ' THelper = type helper for word',
  23933. ' function GetItems(Index: byte): boolean;',
  23934. ' procedure SetItems(Index: byte; Value: boolean);',
  23935. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23936. ' end;',
  23937. 'function THelper.GetItems(Index: byte): boolean;',
  23938. 'begin',
  23939. ' Result:=Items[1];',
  23940. ' Items[2]:=false;',
  23941. ' Result:=Self.Items[3];',
  23942. ' Self.Items[4]:=true;',
  23943. ' with Self do begin',
  23944. ' Result:=Items[5];',
  23945. ' Items[6]:=false;',
  23946. ' end;',
  23947. 'end;',
  23948. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23949. 'begin',
  23950. 'end;',
  23951. 'var',
  23952. ' w: word;',
  23953. ' b: boolean;',
  23954. 'begin',
  23955. ' b:=w.Items[1];',
  23956. ' w.Items[2]:=b;',
  23957. ' with w do begin',
  23958. ' b:=Items[3];',
  23959. ' Items[4]:=b;',
  23960. ' end;',
  23961. '']);
  23962. ConvertProgram;
  23963. CheckSource('TestTypeHelper_Property_Array',
  23964. LinesToStr([ // statements
  23965. 'rtl.createHelper(this, "THelper", null, function () {',
  23966. ' this.GetItems = function (Index) {',
  23967. ' var Result = false;',
  23968. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23969. ' $mod.THelper.SetItems.call(this, 2, false);',
  23970. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23971. ' $mod.THelper.SetItems.call(this, 4, true);',
  23972. ' var $with = this.get();',
  23973. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23974. ' $mod.THelper.SetItems.call(this, 6, false);',
  23975. ' return Result;',
  23976. ' };',
  23977. ' this.SetItems = function (Index, Value) {',
  23978. ' };',
  23979. '});',
  23980. 'this.w = 0;',
  23981. 'this.b = false;',
  23982. '']),
  23983. LinesToStr([ // $mod.$main
  23984. '$mod.b = $mod.THelper.GetItems.call({',
  23985. ' p: $mod,',
  23986. ' get: function () {',
  23987. ' return this.p.w;',
  23988. ' },',
  23989. ' set: function (v) {',
  23990. ' this.p.w = v;',
  23991. ' }',
  23992. '}, 1);',
  23993. '$mod.THelper.SetItems.call({',
  23994. ' p: $mod,',
  23995. ' get: function () {',
  23996. ' return this.p.w;',
  23997. ' },',
  23998. ' set: function (v) {',
  23999. ' this.p.w = v;',
  24000. ' }',
  24001. '}, 2, $mod.b);',
  24002. 'var $with = $mod.w;',
  24003. '$mod.b = $mod.THelper.GetItems.call({',
  24004. ' get: function () {',
  24005. ' return $with;',
  24006. ' },',
  24007. ' set: function (v) {',
  24008. ' $with = v;',
  24009. ' }',
  24010. '}, 3);',
  24011. '$mod.THelper.SetItems.call({',
  24012. ' get: function () {',
  24013. ' return $with;',
  24014. ' },',
  24015. ' set: function (v) {',
  24016. ' $with = v;',
  24017. ' }',
  24018. '}, 4, $mod.b);',
  24019. '']));
  24020. end;
  24021. procedure TTestModule.TestTypeHelper_ClassProperty;
  24022. begin
  24023. StartProgram(false);
  24024. Add([
  24025. '{$modeswitch typehelpers}',
  24026. 'type',
  24027. ' THelper = type helper for word',
  24028. ' class function GetSize: longint; static;',
  24029. ' class procedure SetSize(Value: longint); static;',
  24030. ' class property Size: longint read GetSize write SetSize;',
  24031. ' end;',
  24032. 'class function THelper.GetSize: longint;',
  24033. 'begin',
  24034. ' Result:=Size+1;',
  24035. ' Size:=2;',
  24036. 'end;',
  24037. 'class procedure THelper.SetSize(Value: longint);',
  24038. 'begin',
  24039. 'end;',
  24040. 'begin',
  24041. '']);
  24042. ConvertProgram;
  24043. CheckSource('TestTypeHelper_ClassProperty',
  24044. LinesToStr([ // statements
  24045. 'rtl.createHelper(this, "THelper", null, function () {',
  24046. ' this.GetSize = function () {',
  24047. ' var Result = 0;',
  24048. ' Result = $mod.THelper.GetSize() + 1;',
  24049. ' $mod.THelper.SetSize(2);',
  24050. ' return Result;',
  24051. ' };',
  24052. ' this.SetSize = function (Value) {',
  24053. ' };',
  24054. '});',
  24055. '']),
  24056. LinesToStr([ // $mod.$main
  24057. '']));
  24058. end;
  24059. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24060. begin
  24061. StartProgram(false);
  24062. Add([
  24063. '{$modeswitch typehelpers}',
  24064. 'type',
  24065. ' THelper = type helper for word',
  24066. ' class function GetItems(Index: byte): boolean; static;',
  24067. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24068. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24069. ' end;',
  24070. 'class function THelper.GetItems(Index: byte): boolean;',
  24071. 'begin',
  24072. ' Result:=Items[1];',
  24073. ' Items[2]:=false;',
  24074. 'end;',
  24075. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24076. 'begin',
  24077. 'end;',
  24078. 'var',
  24079. ' w: word;',
  24080. ' b: boolean;',
  24081. 'begin',
  24082. ' b:=w.Items[1];',
  24083. ' w.Items[2]:=b;',
  24084. ' with w do begin',
  24085. ' b:=Items[3];',
  24086. ' Items[4]:=b;',
  24087. ' end;',
  24088. '']);
  24089. ConvertProgram;
  24090. CheckSource('TestTypeHelper_ClassProperty_Array',
  24091. LinesToStr([ // statements
  24092. 'rtl.createHelper(this, "THelper", null, function () {',
  24093. ' this.GetItems = function (Index) {',
  24094. ' var Result = false;',
  24095. ' Result = $mod.THelper.GetItems(1);',
  24096. ' $mod.THelper.SetItems(2, false);',
  24097. ' return Result;',
  24098. ' };',
  24099. ' this.SetItems = function (Index, Value) {',
  24100. ' };',
  24101. '});',
  24102. 'this.w = 0;',
  24103. 'this.b = false;',
  24104. '']),
  24105. LinesToStr([ // $mod.$main
  24106. '$mod.b = $mod.THelper.GetItems(1);',
  24107. '$mod.THelper.SetItems(2, $mod.b);',
  24108. 'var $with = $mod.w;',
  24109. '$mod.b = $mod.THelper.GetItems(3);',
  24110. '$mod.THelper.SetItems(4, $mod.b);',
  24111. '']));
  24112. end;
  24113. procedure TTestModule.TestTypeHelper_ClassMethod;
  24114. begin
  24115. StartProgram(false);
  24116. Add([
  24117. '{$modeswitch typehelpers}',
  24118. 'type',
  24119. ' THelper = type helper for word',
  24120. ' class procedure DoStatic; static;',
  24121. ' end;',
  24122. 'class procedure THelper.DoStatic;',
  24123. 'begin',
  24124. ' DoStatic;',
  24125. ' DoStatic();',
  24126. 'end;',
  24127. 'var w: word;',
  24128. 'begin',
  24129. ' w.DoStatic;',
  24130. ' w.DoStatic();',
  24131. '']);
  24132. ConvertProgram;
  24133. CheckSource('TestTypeHelper_ClassMethod',
  24134. LinesToStr([ // statements
  24135. 'rtl.createHelper(this, "THelper", null, function () {',
  24136. ' this.DoStatic = function () {',
  24137. ' $mod.THelper.DoStatic();',
  24138. ' $mod.THelper.DoStatic();',
  24139. ' };',
  24140. '});',
  24141. 'this.w = 0;',
  24142. '']),
  24143. LinesToStr([ // $mod.$main
  24144. '$mod.THelper.DoStatic();',
  24145. '$mod.THelper.DoStatic();',
  24146. '']));
  24147. end;
  24148. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24149. begin
  24150. StartProgram(false);
  24151. Add([
  24152. '{$modeswitch typehelpers}',
  24153. 'type',
  24154. ' THelper = type helper for word',
  24155. ' procedure Run; external name ''Run'';',
  24156. ' end;',
  24157. 'var w: word;',
  24158. 'begin',
  24159. ' w.Run;',
  24160. '']);
  24161. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24162. ConvertProgram;
  24163. end;
  24164. procedure TTestModule.TestTypeHelper_Constructor;
  24165. begin
  24166. StartProgram(false);
  24167. Add([
  24168. '{$modeswitch typehelpers}',
  24169. 'type',
  24170. ' THelper = type helper for word',
  24171. ' constructor Init(e: longint);',
  24172. ' end;',
  24173. 'constructor THelper.Init(e: longint);',
  24174. 'begin',
  24175. ' Self:=e;',
  24176. ' Init(e+1);',
  24177. 'end;',
  24178. 'var w: word;',
  24179. 'begin',
  24180. ' w:=word.Init(2);',
  24181. ' w:=w.Init(3);',
  24182. ' with word do w:=Init(4);',
  24183. ' with w do w:=Init(5);',
  24184. '']);
  24185. ConvertProgram;
  24186. CheckSource('TestTypeHelper_Constructor',
  24187. LinesToStr([ // statements
  24188. 'rtl.createHelper(this, "THelper", null, function () {',
  24189. ' this.Init = function (e) {',
  24190. ' this.set(e);',
  24191. ' $mod.THelper.Init.call(this, e + 1);',
  24192. ' return this.get();',
  24193. ' };',
  24194. ' this.$new = function (fn, args) {',
  24195. ' return this[fn].apply({',
  24196. ' p: 0,',
  24197. ' get: function () {',
  24198. ' return this.p;',
  24199. ' },',
  24200. ' set: function (v) {',
  24201. ' this.p = v;',
  24202. ' }',
  24203. ' }, args);',
  24204. ' };',
  24205. '});',
  24206. 'this.w = 0;',
  24207. '']),
  24208. LinesToStr([ // $mod.$main
  24209. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24210. '$mod.w = $mod.THelper.Init.call({',
  24211. ' p: $mod,',
  24212. ' get: function () {',
  24213. ' return this.p.w;',
  24214. ' },',
  24215. ' set: function (v) {',
  24216. ' this.p.w = v;',
  24217. ' }',
  24218. '}, 3);',
  24219. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24220. 'var $with = $mod.w;',
  24221. '$mod.w = $mod.THelper.Init.call({',
  24222. ' get: function () {',
  24223. ' return $with;',
  24224. ' },',
  24225. ' set: function (v) {',
  24226. ' $with = v;',
  24227. ' }',
  24228. '}, 5);',
  24229. '']));
  24230. end;
  24231. procedure TTestModule.TestTypeHelper_Word;
  24232. begin
  24233. StartProgram(false);
  24234. Add([
  24235. '{$modeswitch typehelpers}',
  24236. 'type',
  24237. ' THelper = type helper for word',
  24238. ' procedure DoIt(e: byte = 123);',
  24239. ' end;',
  24240. 'procedure THelper.DoIt(e: byte);',
  24241. 'begin',
  24242. ' Self:=e;',
  24243. ' Self:=Self+1;',
  24244. ' with Self do Doit;',
  24245. 'end;',
  24246. 'begin',
  24247. ' word(3).DoIt;',
  24248. '']);
  24249. ConvertProgram;
  24250. CheckSource('TestTypeHelper_Word',
  24251. LinesToStr([ // statements
  24252. 'rtl.createHelper(this, "THelper", null, function () {',
  24253. ' this.DoIt = function (e) {',
  24254. ' this.set(e);',
  24255. ' this.set(this.get() + 1);',
  24256. ' var $with = this.get();',
  24257. ' $mod.THelper.DoIt.call(this, 123);',
  24258. ' };',
  24259. '});',
  24260. '']),
  24261. LinesToStr([ // $mod.$main
  24262. '$mod.THelper.DoIt.call({',
  24263. ' get: function () {',
  24264. ' return 3;',
  24265. ' },',
  24266. ' set: function (v) {',
  24267. ' rtl.raiseE("EPropReadOnly");',
  24268. ' }',
  24269. '}, 123);',
  24270. '']));
  24271. end;
  24272. procedure TTestModule.TestTypeHelper_Boolean;
  24273. begin
  24274. StartProgram(false);
  24275. Add([
  24276. '{$modeswitch typehelpers}',
  24277. 'type',
  24278. ' Integer = longint;',
  24279. ' THelper = type helper for boolean',
  24280. ' procedure Run(e: wordbool = true);',
  24281. ' end;',
  24282. 'procedure THelper.Run(e: wordbool);',
  24283. 'begin',
  24284. ' Self:=e;',
  24285. ' Self:=not Self;',
  24286. ' with Self do Run;',
  24287. ' if Integer(Self)=0 then ;',
  24288. 'end;',
  24289. 'begin',
  24290. ' boolean(3).Run;',
  24291. '']);
  24292. ConvertProgram;
  24293. CheckSource('TestTypeHelper_Boolean',
  24294. LinesToStr([ // statements
  24295. 'rtl.createHelper(this, "THelper", null, function () {',
  24296. ' this.Run = function (e) {',
  24297. ' this.set(e);',
  24298. ' this.set(!this.get());',
  24299. ' var $with = this.get();',
  24300. ' $mod.THelper.Run.call(this, true);',
  24301. ' if ((this.get() ? 1 : 0) === 0) ;',
  24302. ' };',
  24303. '});',
  24304. '']),
  24305. LinesToStr([ // $mod.$main
  24306. '$mod.THelper.Run.call({',
  24307. ' a: 3 != 0,',
  24308. ' get: function () {',
  24309. ' return this.a;',
  24310. ' },',
  24311. ' set: function (v) {',
  24312. ' rtl.raiseE("EPropReadOnly");',
  24313. ' }',
  24314. '}, true);',
  24315. '']));
  24316. end;
  24317. procedure TTestModule.TestTypeHelper_WordBool;
  24318. begin
  24319. StartProgram(false);
  24320. Add([
  24321. '{$modeswitch typehelpers}',
  24322. 'type',
  24323. ' Integer = longint;',
  24324. ' THelper = type helper for WordBool',
  24325. ' procedure Run(e: wordbool = true);',
  24326. ' end;',
  24327. 'procedure THelper.Run(e: wordbool);',
  24328. 'var i: integer;',
  24329. 'begin',
  24330. ' i:=Integer(Self);',
  24331. 'end;',
  24332. 'var w: wordbool;',
  24333. 'begin',
  24334. ' w.Run;',
  24335. ' wordbool(3).Run;',
  24336. '']);
  24337. ConvertProgram;
  24338. CheckSource('TestTypeHelper_WordBool',
  24339. LinesToStr([ // statements
  24340. 'rtl.createHelper(this, "THelper", null, function () {',
  24341. ' this.Run = function (e) {',
  24342. ' var i = 0;',
  24343. ' i = (this.get() ? 1 : 0);',
  24344. ' };',
  24345. '});',
  24346. 'this.w = false;',
  24347. '']),
  24348. LinesToStr([ // $mod.$main
  24349. '$mod.THelper.Run.call({',
  24350. ' p: $mod,',
  24351. ' get: function () {',
  24352. ' return this.p.w;',
  24353. ' },',
  24354. ' set: function (v) {',
  24355. ' this.p.w = v;',
  24356. ' }',
  24357. '}, true);',
  24358. '$mod.THelper.Run.call({',
  24359. ' a: 3 != 0,',
  24360. ' get: function () {',
  24361. ' return this.a;',
  24362. ' },',
  24363. ' set: function (v) {',
  24364. ' rtl.raiseE("EPropReadOnly");',
  24365. ' }',
  24366. '}, true);',
  24367. '']));
  24368. end;
  24369. procedure TTestModule.TestTypeHelper_Double;
  24370. begin
  24371. StartProgram(false);
  24372. Add([
  24373. '{$modeswitch typehelpers}',
  24374. 'type',
  24375. ' Float = type double;',
  24376. ' THelper = type helper for Float',
  24377. ' const NPI = 3.141592;',
  24378. ' function ToStr: String;',
  24379. ' end;',
  24380. 'function THelper.ToStr: String;',
  24381. 'begin',
  24382. 'end;',
  24383. 'procedure DoIt(s: string);',
  24384. 'begin',
  24385. 'end;',
  24386. 'var f: Float;',
  24387. 'begin',
  24388. ' DoIt(f.toStr);',
  24389. ' DoIt(f.toStr());',
  24390. ' (f*f).toStr;',
  24391. ' DoIt((f*f).toStr);',
  24392. '']);
  24393. ConvertProgram;
  24394. CheckSource('TestTypeHelper_Double',
  24395. LinesToStr([ // statements
  24396. 'rtl.createHelper(this, "THelper", null, function () {',
  24397. ' this.NPI = 3.141592;',
  24398. ' this.ToStr = function () {',
  24399. ' var Result = "";',
  24400. ' return Result;',
  24401. ' };',
  24402. '});',
  24403. 'this.DoIt = function (s) {',
  24404. '};',
  24405. 'this.f = 0.0;',
  24406. '']),
  24407. LinesToStr([ // $mod.$main
  24408. '$mod.DoIt($mod.THelper.ToStr.call({',
  24409. ' p: $mod,',
  24410. ' get: function () {',
  24411. ' return this.p.f;',
  24412. ' },',
  24413. ' set: function (v) {',
  24414. ' this.p.f = v;',
  24415. ' }',
  24416. '}));',
  24417. '$mod.DoIt($mod.THelper.ToStr.call({',
  24418. ' p: $mod,',
  24419. ' get: function () {',
  24420. ' return this.p.f;',
  24421. ' },',
  24422. ' set: function (v) {',
  24423. ' this.p.f = v;',
  24424. ' }',
  24425. '}));',
  24426. '$mod.THelper.ToStr.call({',
  24427. ' a: $mod.f * $mod.f,',
  24428. ' get: function () {',
  24429. ' return this.a;',
  24430. ' },',
  24431. ' set: function (v) {',
  24432. ' rtl.raiseE("EPropReadOnly");',
  24433. ' }',
  24434. '});',
  24435. '$mod.DoIt($mod.THelper.ToStr.call({',
  24436. ' a: $mod.f * $mod.f,',
  24437. ' get: function () {',
  24438. ' return this.a;',
  24439. ' },',
  24440. ' set: function (v) {',
  24441. ' rtl.raiseE("EPropReadOnly");',
  24442. ' }',
  24443. '}));',
  24444. '']));
  24445. end;
  24446. procedure TTestModule.TestTypeHelper_NativeInt;
  24447. begin
  24448. StartProgram(false);
  24449. Add([
  24450. '{$modeswitch typehelpers}',
  24451. 'type',
  24452. ' MaxInt = type nativeint;',
  24453. ' THelperI = type helper for MaxInt',
  24454. ' function ToStr: String;',
  24455. ' end;',
  24456. ' MaxUInt = type nativeuint;',
  24457. ' THelperU = type helper for MaxUInt',
  24458. ' function ToStr: String;',
  24459. ' end;',
  24460. 'function THelperI.ToStr: String;',
  24461. 'begin',
  24462. ' Result:=str(Self);',
  24463. 'end;',
  24464. 'function THelperU.ToStr: String;',
  24465. 'begin',
  24466. ' Result:=str(Self);',
  24467. 'end;',
  24468. 'procedure DoIt(s: string);',
  24469. 'begin',
  24470. 'end;',
  24471. 'var i: MaxInt;',
  24472. 'begin',
  24473. ' DoIt(i.toStr);',
  24474. ' DoIt(i.toStr());',
  24475. ' (i*i).toStr;',
  24476. ' DoIt((i*i).toStr);',
  24477. '']);
  24478. ConvertProgram;
  24479. CheckSource('TestTypeHelper_NativeInt',
  24480. LinesToStr([ // statements
  24481. 'rtl.createHelper(this, "THelperI", null, function () {',
  24482. ' this.ToStr = function () {',
  24483. ' var Result = "";',
  24484. ' Result = "" + this.get();',
  24485. ' return Result;',
  24486. ' };',
  24487. '});',
  24488. 'rtl.createHelper(this, "THelperU", null, function () {',
  24489. ' this.ToStr = function () {',
  24490. ' var Result = "";',
  24491. ' Result = "" + this.get();',
  24492. ' return Result;',
  24493. ' };',
  24494. '});',
  24495. 'this.DoIt = function (s) {',
  24496. '};',
  24497. 'this.i = 0;',
  24498. '']),
  24499. LinesToStr([ // $mod.$main
  24500. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24501. ' p: $mod,',
  24502. ' get: function () {',
  24503. ' return this.p.i;',
  24504. ' },',
  24505. ' set: function (v) {',
  24506. ' this.p.i = v;',
  24507. ' }',
  24508. '}));',
  24509. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24510. ' p: $mod,',
  24511. ' get: function () {',
  24512. ' return this.p.i;',
  24513. ' },',
  24514. ' set: function (v) {',
  24515. ' this.p.i = v;',
  24516. ' }',
  24517. '}));',
  24518. '$mod.THelperI.ToStr.call({',
  24519. ' a: $mod.i * $mod.i,',
  24520. ' get: function () {',
  24521. ' return this.a;',
  24522. ' },',
  24523. ' set: function (v) {',
  24524. ' rtl.raiseE("EPropReadOnly");',
  24525. ' }',
  24526. '});',
  24527. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24528. ' a: $mod.i * $mod.i,',
  24529. ' get: function () {',
  24530. ' return this.a;',
  24531. ' },',
  24532. ' set: function (v) {',
  24533. ' rtl.raiseE("EPropReadOnly");',
  24534. ' }',
  24535. '}));',
  24536. '']));
  24537. end;
  24538. procedure TTestModule.TestTypeHelper_StringChar;
  24539. begin
  24540. StartProgram(false);
  24541. Add([
  24542. '{$modeswitch typehelpers}',
  24543. 'type',
  24544. ' TStringHelper = type helper for string',
  24545. ' procedure DoIt(e: byte = 123);',
  24546. ' end;',
  24547. ' TCharHelper = type helper for char',
  24548. ' procedure Fly;',
  24549. ' end;',
  24550. 'procedure TStringHelper.DoIt(e: byte);',
  24551. 'begin',
  24552. ' Self[1]:=''c'';',
  24553. ' Self[2]:=Self[3];',
  24554. 'end;',
  24555. 'procedure TCharHelper.Fly;',
  24556. 'begin',
  24557. ' Self:=''c'';',
  24558. 'end;',
  24559. 'begin',
  24560. ' ''abc''.DoIt;',
  24561. ' ''xyz''.DoIt();',
  24562. ' ''c''.Fly();',
  24563. '']);
  24564. ConvertProgram;
  24565. CheckSource('TestTypeHelper_StringChar',
  24566. LinesToStr([ // statements
  24567. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24568. ' this.DoIt = function (e) {',
  24569. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24570. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24571. ' };',
  24572. '});',
  24573. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24574. ' this.Fly = function () {',
  24575. ' this.set("c");',
  24576. ' };',
  24577. '});',
  24578. '']),
  24579. LinesToStr([ // $mod.$main
  24580. '$mod.TStringHelper.DoIt.call({',
  24581. ' get: function () {',
  24582. ' return "abc";',
  24583. ' },',
  24584. ' set: function (v) {',
  24585. ' rtl.raiseE("EPropReadOnly");',
  24586. ' }',
  24587. '}, 123);',
  24588. '$mod.TStringHelper.DoIt.call({',
  24589. ' get: function () {',
  24590. ' return "xyz";',
  24591. ' },',
  24592. ' set: function (v) {',
  24593. ' rtl.raiseE("EPropReadOnly");',
  24594. ' }',
  24595. '}, 123);',
  24596. '$mod.TCharHelper.Fly.call({',
  24597. ' get: function () {',
  24598. ' return "c";',
  24599. ' },',
  24600. ' set: function (v) {',
  24601. ' rtl.raiseE("EPropReadOnly");',
  24602. ' }',
  24603. '});',
  24604. '']));
  24605. end;
  24606. procedure TTestModule.TestTypeHelper_JSValue;
  24607. begin
  24608. StartProgram(false);
  24609. Add([
  24610. '{$modeswitch typehelpers}',
  24611. 'type',
  24612. ' TExtValue = type jsvalue;',
  24613. ' THelper = type helper for TExtValue',
  24614. ' function ToStr: String;',
  24615. ' end;',
  24616. 'function THelper.ToStr: String;',
  24617. 'begin',
  24618. 'end;',
  24619. 'var',
  24620. ' s: string;',
  24621. ' v: TExtValue;',
  24622. 'begin',
  24623. ' s:=v.toStr;',
  24624. ' s:=v.toStr();',
  24625. ' TExtValue(s).toStr;',
  24626. '']);
  24627. ConvertProgram;
  24628. CheckSource('TestTypeHelper_JSValue',
  24629. LinesToStr([ // statements
  24630. 'rtl.createHelper(this, "THelper", null, function () {',
  24631. ' this.ToStr = function () {',
  24632. ' var Result = "";',
  24633. ' return Result;',
  24634. ' };',
  24635. '});',
  24636. 'this.s = "";',
  24637. 'this.v = undefined;',
  24638. '']),
  24639. LinesToStr([ // $mod.$main
  24640. '$mod.s = $mod.THelper.ToStr.call({',
  24641. ' p: $mod,',
  24642. ' get: function () {',
  24643. ' return this.p.v;',
  24644. ' },',
  24645. ' set: function (v) {',
  24646. ' this.p.v = v;',
  24647. ' }',
  24648. '});',
  24649. '$mod.s = $mod.THelper.ToStr.call({',
  24650. ' p: $mod,',
  24651. ' get: function () {',
  24652. ' return this.p.v;',
  24653. ' },',
  24654. ' set: function (v) {',
  24655. ' this.p.v = v;',
  24656. ' }',
  24657. '});',
  24658. '$mod.THelper.ToStr.call({',
  24659. ' p: $mod,',
  24660. ' get: function () {',
  24661. ' return this.p.s;',
  24662. ' },',
  24663. ' set: function (v) {',
  24664. ' rtl.raiseE("EPropReadOnly");',
  24665. ' }',
  24666. '});',
  24667. '']));
  24668. end;
  24669. procedure TTestModule.TestTypeHelper_Array;
  24670. begin
  24671. StartProgram(false);
  24672. Add([
  24673. '{$modeswitch typehelpers}',
  24674. 'type',
  24675. ' TArrOfBool = array of boolean;',
  24676. ' TArrOfJS = array of jsvalue;',
  24677. ' THelper = type helper for TArrOfBool',
  24678. ' procedure DoIt(e: byte = 123);',
  24679. ' end;',
  24680. 'procedure THelper.DoIt(e: byte);',
  24681. 'begin',
  24682. ' Self[1]:=true;',
  24683. ' Self[2]:=not Self[3];',
  24684. ' SetLength(Self,4);',
  24685. 'end;',
  24686. 'var',
  24687. ' b: TArrOfBool;',
  24688. ' j: TArrOfJS;',
  24689. 'begin',
  24690. ' b.DoIt;',
  24691. ' TArrOfBool(j).DoIt();',
  24692. '']);
  24693. ConvertProgram;
  24694. CheckSource('TestTypeHelper_Array',
  24695. LinesToStr([ // statements
  24696. 'rtl.createHelper(this, "THelper", null, function () {',
  24697. ' this.DoIt = function (e) {',
  24698. ' this.get()[1] = true;',
  24699. ' this.get()[2] = !this.get()[3];',
  24700. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24701. ' };',
  24702. '});',
  24703. 'this.b = [];',
  24704. 'this.j = [];',
  24705. '']),
  24706. LinesToStr([ // $mod.$main
  24707. '$mod.THelper.DoIt.call({',
  24708. ' p: $mod,',
  24709. ' get: function () {',
  24710. ' return this.p.b;',
  24711. ' },',
  24712. ' set: function (v) {',
  24713. ' this.p.b = v;',
  24714. ' }',
  24715. '}, 123);',
  24716. '$mod.THelper.DoIt.call({',
  24717. ' p: $mod,',
  24718. ' get: function () {',
  24719. ' return this.p.j;',
  24720. ' },',
  24721. ' set: function (v) {',
  24722. ' this.p.j = v;',
  24723. ' }',
  24724. '}, 123);',
  24725. '']));
  24726. end;
  24727. procedure TTestModule.TestTypeHelper_EnumType;
  24728. begin
  24729. StartProgram(false);
  24730. Add([
  24731. '{$modeswitch typehelpers}',
  24732. 'type',
  24733. ' TEnum = (red,blue);',
  24734. ' THelper = type helper for TEnum',
  24735. ' procedure DoIt(e: byte = 123);',
  24736. ' class procedure Swing(w: word); static;',
  24737. ' end;',
  24738. 'procedure THelper.DoIt(e: byte);',
  24739. 'begin',
  24740. ' Self:=red;',
  24741. ' Self:=succ(Self);',
  24742. ' with Self do Doit;',
  24743. 'end;',
  24744. 'class procedure THelper.Swing(w: word);',
  24745. 'begin',
  24746. 'end;',
  24747. 'var e: TEnum;',
  24748. 'begin',
  24749. ' e.DoIt;',
  24750. ' red.DoIt;',
  24751. ' TEnum.blue.DoIt;',
  24752. ' TEnum(1).DoIt;',
  24753. ' TEnum.Swing(3);',
  24754. '']);
  24755. ConvertProgram;
  24756. CheckSource('TestTypeHelper_EnumType',
  24757. LinesToStr([ // statements
  24758. 'this.TEnum = {',
  24759. ' "0": "red",',
  24760. ' red: 0,',
  24761. ' "1": "blue",',
  24762. ' blue: 1',
  24763. '};',
  24764. 'rtl.createHelper(this, "THelper", null, function () {',
  24765. ' this.DoIt = function (e) {',
  24766. ' this.set($mod.TEnum.red);',
  24767. ' this.set(this.get() + 1);',
  24768. ' var $with = this.get();',
  24769. ' $mod.THelper.DoIt.call(this, 123);',
  24770. ' };',
  24771. ' this.Swing = function (w) {',
  24772. ' };',
  24773. '});',
  24774. 'this.e = 0;',
  24775. '']),
  24776. LinesToStr([ // $mod.$main
  24777. '$mod.THelper.DoIt.call({',
  24778. ' p: $mod,',
  24779. ' get: function () {',
  24780. ' return this.p.e;',
  24781. ' },',
  24782. ' set: function (v) {',
  24783. ' this.p.e = v;',
  24784. ' }',
  24785. '}, 123);',
  24786. '$mod.THelper.DoIt.call({',
  24787. ' p: $mod.TEnum,',
  24788. ' get: function () {',
  24789. ' return this.p.red;',
  24790. ' },',
  24791. ' set: function (v) {',
  24792. ' rtl.raiseE("EPropReadOnly");',
  24793. ' }',
  24794. '}, 123);',
  24795. '$mod.THelper.DoIt.call({',
  24796. ' p: $mod.TEnum,',
  24797. ' get: function () {',
  24798. ' return this.p.blue;',
  24799. ' },',
  24800. ' set: function (v) {',
  24801. ' rtl.raiseE("EPropReadOnly");',
  24802. ' }',
  24803. '}, 123);',
  24804. '$mod.THelper.DoIt.call({',
  24805. ' get: function () {',
  24806. ' return 1;',
  24807. ' },',
  24808. ' set: function (v) {',
  24809. ' rtl.raiseE("EPropReadOnly");',
  24810. ' }',
  24811. '}, 123);',
  24812. '$mod.THelper.Swing(3);',
  24813. '']));
  24814. end;
  24815. procedure TTestModule.TestTypeHelper_SetType;
  24816. begin
  24817. StartProgram(false);
  24818. Add([
  24819. '{$modeswitch typehelpers}',
  24820. 'type',
  24821. ' TEnum = (red,blue);',
  24822. ' TSetOfEnum = set of TEnum;',
  24823. ' THelper = type helper for TSetOfEnum',
  24824. ' procedure DoIt(e: byte = 123);',
  24825. ' constructor Init(e: TEnum);',
  24826. ' constructor InitEmpty;',
  24827. ' end;',
  24828. 'procedure THelper.DoIt(e: byte);',
  24829. 'begin',
  24830. ' Self:=[];',
  24831. ' Self:=[red];',
  24832. ' Include(Self,blue);',
  24833. 'end;',
  24834. 'constructor THelper.Init(e: TEnum);',
  24835. 'begin',
  24836. ' Self:=[];',
  24837. ' Self:=[e];',
  24838. ' Include(Self,blue);',
  24839. 'end;',
  24840. 'constructor THelper.InitEmpty;',
  24841. 'begin',
  24842. 'end;',
  24843. 'var s: TSetOfEnum;',
  24844. 'begin',
  24845. ' s.DoIt;',
  24846. //' [red].DoIt;',
  24847. //' with s do DoIt;',
  24848. //' with [red,blue] do DoIt;',
  24849. ' s:=TSetOfEnum.Init(blue);',
  24850. ' s:=s.Init(blue);',
  24851. '']);
  24852. ConvertProgram;
  24853. CheckSource('TestTypeHelper_SetType',
  24854. LinesToStr([ // statements
  24855. 'this.TEnum = {',
  24856. ' "0": "red",',
  24857. ' red: 0,',
  24858. ' "1": "blue",',
  24859. ' blue: 1',
  24860. '};',
  24861. 'rtl.createHelper(this, "THelper", null, function () {',
  24862. ' this.DoIt = function (e) {',
  24863. ' this.set({});',
  24864. ' this.set(rtl.createSet($mod.TEnum.red));',
  24865. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24866. ' };',
  24867. ' this.Init = function (e) {',
  24868. ' this.set({});',
  24869. ' this.set(rtl.createSet(e));',
  24870. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24871. ' return this.get();',
  24872. ' };',
  24873. ' this.InitEmpty = function () {',
  24874. ' return this.get();',
  24875. ' };',
  24876. ' this.$new = function (fn, args) {',
  24877. ' return this[fn].apply({',
  24878. ' p: {},',
  24879. ' get: function () {',
  24880. ' return this.p;',
  24881. ' },',
  24882. ' set: function (v) {',
  24883. ' this.p = v;',
  24884. ' }',
  24885. ' }, args);',
  24886. ' };',
  24887. '});',
  24888. 'this.s = {};',
  24889. '']),
  24890. LinesToStr([ // $mod.$main
  24891. '$mod.THelper.DoIt.call({',
  24892. ' p: $mod,',
  24893. ' get: function () {',
  24894. ' return this.p.s;',
  24895. ' },',
  24896. ' set: function (v) {',
  24897. ' this.p.s = v;',
  24898. ' }',
  24899. '}, 123);',
  24900. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24901. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24902. ' p: $mod,',
  24903. ' get: function () {',
  24904. ' return this.p.s;',
  24905. ' },',
  24906. ' set: function (v) {',
  24907. ' this.p.s = v;',
  24908. ' }',
  24909. '}, $mod.TEnum.blue));',
  24910. '']));
  24911. end;
  24912. procedure TTestModule.TestTypeHelper_InterfaceType;
  24913. begin
  24914. StartProgram(false);
  24915. Add([
  24916. '{$interfaces com}',
  24917. '{$modeswitch typehelpers}',
  24918. 'type',
  24919. ' IUnknown = interface',
  24920. ' function _AddRef: longint;',
  24921. ' function _Release: longint;',
  24922. ' end;',
  24923. ' TObject = class(IUnknown)',
  24924. ' function _AddRef: longint; virtual; abstract;',
  24925. ' function _Release: longint; virtual; abstract;',
  24926. ' end;',
  24927. ' THelper = type helper for IUnknown',
  24928. ' procedure Fly(e: byte = 123);',
  24929. ' class procedure Run; static;',
  24930. ' end;',
  24931. 'var',
  24932. ' i: IUnknown;',
  24933. ' o: TObject;',
  24934. 'procedure THelper.Fly(e: byte);',
  24935. 'begin',
  24936. ' i:=Self;',
  24937. ' o:=Self as TObject;',
  24938. ' Self:=nil;',
  24939. ' Self:=i;',
  24940. ' Self:=o;',
  24941. ' with Self do begin',
  24942. ' Fly;',
  24943. ' Fly();',
  24944. ' end;',
  24945. 'end;',
  24946. 'class procedure THelper.Run;',
  24947. 'var l: IUnknown;',
  24948. 'begin',
  24949. ' l.Fly;',
  24950. ' l.Fly();',
  24951. 'end;',
  24952. 'begin',
  24953. ' i.Fly;',
  24954. ' i.Fly();',
  24955. ' i.Run;',
  24956. ' i.Run();',
  24957. ' IUnknown.Run;',
  24958. ' IUnknown.Run();',
  24959. '']);
  24960. ConvertProgram;
  24961. CheckSource('TestTypeHelper_InterfaceType',
  24962. LinesToStr([ // statements
  24963. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24964. 'rtl.createClass(this, "TObject", null, function () {',
  24965. ' this.$init = function () {',
  24966. ' };',
  24967. ' this.$final = function () {',
  24968. ' };',
  24969. ' rtl.addIntf(this, $mod.IUnknown);',
  24970. '});',
  24971. 'rtl.createHelper(this, "THelper", null, function () {',
  24972. ' this.Fly = function (e) {',
  24973. ' var $ir = rtl.createIntfRefs();',
  24974. ' try {',
  24975. ' rtl.setIntfP($mod, "i", this.get());',
  24976. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24977. ' this.set(null);',
  24978. ' this.set($mod.i);',
  24979. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24980. ' var $with = this.get();',
  24981. ' $mod.THelper.Fly.call(this, 123);',
  24982. ' $mod.THelper.Fly.call(this, 123);',
  24983. ' } finally {',
  24984. ' $ir.free();',
  24985. ' };',
  24986. ' };',
  24987. ' this.Run = function () {',
  24988. ' var l = null;',
  24989. ' try {',
  24990. ' $mod.THelper.Fly.call({',
  24991. ' get: function () {',
  24992. ' return l;',
  24993. ' },',
  24994. ' set: function (v) {',
  24995. ' l = rtl.setIntfL(l, v);',
  24996. ' }',
  24997. ' }, 123);',
  24998. ' $mod.THelper.Fly.call({',
  24999. ' get: function () {',
  25000. ' return l;',
  25001. ' },',
  25002. ' set: function (v) {',
  25003. ' l = rtl.setIntfL(l, v);',
  25004. ' }',
  25005. ' }, 123);',
  25006. ' } finally {',
  25007. ' rtl._Release(l);',
  25008. ' };',
  25009. ' };',
  25010. '});',
  25011. 'this.i = null;',
  25012. 'this.o = null;',
  25013. '']),
  25014. LinesToStr([ // $mod.$main
  25015. '$mod.THelper.Fly.call({',
  25016. ' p: $mod,',
  25017. ' get: function () {',
  25018. ' return this.p.i;',
  25019. ' },',
  25020. ' set: function (v) {',
  25021. ' rtl.setIntfP(this.p, "i", v);',
  25022. ' }',
  25023. '}, 123);',
  25024. '$mod.THelper.Fly.call({',
  25025. ' p: $mod,',
  25026. ' get: function () {',
  25027. ' return this.p.i;',
  25028. ' },',
  25029. ' set: function (v) {',
  25030. ' rtl.setIntfP(this.p, "i", v);',
  25031. ' }',
  25032. '}, 123);',
  25033. '$mod.THelper.Run();',
  25034. '$mod.THelper.Run();',
  25035. '$mod.THelper.Run();',
  25036. '$mod.THelper.Run();',
  25037. '']));
  25038. end;
  25039. procedure TTestModule.TestTypeHelper_NestedSelf;
  25040. begin
  25041. StartProgram(false);
  25042. Add([
  25043. '{$modeswitch typehelpers}',
  25044. 'type',
  25045. ' THelper = type helper for string',
  25046. ' procedure Run(Value: string);',
  25047. ' end;',
  25048. 'procedure THelper.Run(Value: string);',
  25049. ' function Sub(i: nativeint): boolean;',
  25050. ' begin',
  25051. ' Result:=Self[i+1]=Value[i];',
  25052. ' end;',
  25053. 'begin',
  25054. ' if Self[3]=Value[4] then ;',
  25055. 'end;',
  25056. 'begin',
  25057. '']);
  25058. ConvertProgram;
  25059. CheckSource('TestTypeHelper_NestedSelf',
  25060. LinesToStr([ // statements
  25061. 'rtl.createHelper(this, "THelper", null, function () {',
  25062. ' this.Run = function (Value) {',
  25063. ' var $Self = this;',
  25064. ' function Sub(i) {',
  25065. ' var Result = false;',
  25066. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25067. ' return Result;',
  25068. ' };',
  25069. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25070. ' };',
  25071. '});',
  25072. '']),
  25073. LinesToStr([ // $mod.$main
  25074. '']));
  25075. end;
  25076. procedure TTestModule.TestProcType;
  25077. begin
  25078. StartProgram(false);
  25079. Add([
  25080. 'type',
  25081. ' TProcInt = procedure(vI: longint = 1);',
  25082. 'procedure DoIt(vJ: longint);',
  25083. 'begin end;',
  25084. 'var',
  25085. ' b: boolean;',
  25086. ' vP, vQ: tprocint;',
  25087. 'begin',
  25088. ' vp:=nil;',
  25089. ' vp:=vp;',
  25090. ' vp:=@doit;',
  25091. ' vp;',
  25092. ' vp();',
  25093. ' vp(2);',
  25094. ' b:=vp=nil;',
  25095. ' b:=nil=vp;',
  25096. ' b:=vp=vq;',
  25097. ' b:=vp=@doit;',
  25098. ' b:=@doit=vp;',
  25099. ' b:=vp<>nil;',
  25100. ' b:=nil<>vp;',
  25101. ' b:=vp<>vq;',
  25102. ' b:=vp<>@doit;',
  25103. ' b:=@doit<>vp;',
  25104. ' b:=Assigned(vp);',
  25105. ' if Assigned(vp) then ;']);
  25106. ConvertProgram;
  25107. CheckSource('TestProcType',
  25108. LinesToStr([ // statements
  25109. 'this.DoIt = function(vJ) {',
  25110. '};',
  25111. 'this.b = false;',
  25112. 'this.vP = null;',
  25113. 'this.vQ = null;'
  25114. ]),
  25115. LinesToStr([ // $mod.$main
  25116. '$mod.vP = null;',
  25117. '$mod.vP = $mod.vP;',
  25118. '$mod.vP = $mod.DoIt;',
  25119. '$mod.vP(1);',
  25120. '$mod.vP(1);',
  25121. '$mod.vP(2);',
  25122. '$mod.b = $mod.vP === null;',
  25123. '$mod.b = null === $mod.vP;',
  25124. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25125. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25126. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25127. '$mod.b = $mod.vP !== null;',
  25128. '$mod.b = null !== $mod.vP;',
  25129. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25130. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25131. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25132. '$mod.b = $mod.vP != null;',
  25133. 'if ($mod.vP != null) ;',
  25134. '']));
  25135. end;
  25136. procedure TTestModule.TestProcType_Arg;
  25137. begin
  25138. StartProgram(false);
  25139. Add([
  25140. 'type',
  25141. ' TProcInt = procedure(vI: longint = 1);',
  25142. 'procedure DoIt(vJ: longint); begin end;',
  25143. 'procedure DoSome(vP, vQ: TProcInt);',
  25144. 'var',
  25145. ' b: boolean;',
  25146. 'begin',
  25147. ' vp:=nil;',
  25148. ' vp:=vp;',
  25149. ' vp:=@doit;',
  25150. ' vp;',
  25151. ' vp();',
  25152. ' vp(2);',
  25153. ' b:=vp=nil;',
  25154. ' b:=nil=vp;',
  25155. ' b:=vp=vq;',
  25156. ' b:=vp=@doit;',
  25157. ' b:=@doit=vp;',
  25158. ' b:=vp<>nil;',
  25159. ' b:=nil<>vp;',
  25160. ' b:=vp<>vq;',
  25161. ' b:=vp<>@doit;',
  25162. ' b:=@doit<>vp;',
  25163. ' b:=Assigned(vp);',
  25164. ' if Assigned(vp) then ;',
  25165. 'end;',
  25166. 'begin',
  25167. ' DoSome(@DoIt,nil);']);
  25168. ConvertProgram;
  25169. CheckSource('TestProcType_Arg',
  25170. LinesToStr([ // statements
  25171. 'this.DoIt = function(vJ) {',
  25172. '};',
  25173. 'this.DoSome = function(vP, vQ) {',
  25174. ' var b = false;',
  25175. ' vP = null;',
  25176. ' vP = vP;',
  25177. ' vP = $mod.DoIt;',
  25178. ' vP(1);',
  25179. ' vP(1);',
  25180. ' vP(2);',
  25181. ' b = vP === null;',
  25182. ' b = null === vP;',
  25183. ' b = rtl.eqCallback(vP,vQ);',
  25184. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25185. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25186. ' b = vP !== null;',
  25187. ' b = null !== vP;',
  25188. ' b = !rtl.eqCallback(vP, vQ);',
  25189. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25190. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25191. ' b = vP != null;',
  25192. ' if (vP != null) ;',
  25193. '};',
  25194. '']),
  25195. LinesToStr([ // $mod.$main
  25196. '$mod.DoSome($mod.DoIt,null);',
  25197. '']));
  25198. end;
  25199. procedure TTestModule.TestProcType_FunctionFPC;
  25200. begin
  25201. StartProgram(false);
  25202. Add('type');
  25203. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25204. Add('function DoIt(vI: longint): longint;');
  25205. Add('begin end;');
  25206. Add('var');
  25207. Add(' b: boolean;');
  25208. Add(' vP, vQ: tfuncint;');
  25209. Add('begin');
  25210. Add(' vp:=nil;');
  25211. Add(' vp:=vp;');
  25212. Add(' vp:=@doit;'); // ok in fpc and delphi
  25213. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25214. Add(' vp;'); // ok in fpc and delphi
  25215. Add(' vp();');
  25216. Add(' vp(2);');
  25217. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25218. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25219. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25220. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25221. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25222. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25223. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25224. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25225. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25226. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25227. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25228. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25229. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25230. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25231. Add(' b:=Assigned(vp);');
  25232. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25233. Add(' doit(vp());'); // ok in fpc and delphi
  25234. Add(' doit(vp(2));'); // ok in fpc and delphi
  25235. ConvertProgram;
  25236. CheckSource('TestProcType_FunctionFPC',
  25237. LinesToStr([ // statements
  25238. 'this.DoIt = function(vI) {',
  25239. ' var Result = 0;',
  25240. ' return Result;',
  25241. '};',
  25242. 'this.b = false;',
  25243. 'this.vP = null;',
  25244. 'this.vQ = null;'
  25245. ]),
  25246. LinesToStr([ // $mod.$main
  25247. '$mod.vP = null;',
  25248. '$mod.vP = $mod.vP;',
  25249. '$mod.vP = $mod.DoIt;',
  25250. '$mod.vP(1);',
  25251. '$mod.vP(1);',
  25252. '$mod.vP(2);',
  25253. '$mod.b = $mod.vP === null;',
  25254. '$mod.b = null === $mod.vP;',
  25255. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25256. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25257. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25258. '$mod.b = 4 === $mod.vP(1);',
  25259. '$mod.b = $mod.vP !== null;',
  25260. '$mod.b = null !== $mod.vP;',
  25261. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25262. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25263. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25264. '$mod.b = 6 !== $mod.vP(1);',
  25265. '$mod.b = $mod.vP != null;',
  25266. '$mod.DoIt($mod.vP(1));',
  25267. '$mod.DoIt($mod.vP(2));',
  25268. '']));
  25269. end;
  25270. procedure TTestModule.TestProcType_FunctionDelphi;
  25271. begin
  25272. StartProgram(false);
  25273. Add('{$mode Delphi}');
  25274. Add('type');
  25275. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25276. Add('function DoIt(vI: longint): longint;');
  25277. Add('begin end;');
  25278. Add('var');
  25279. Add(' b: boolean;');
  25280. Add(' vP, vQ: tfuncint;');
  25281. Add('begin');
  25282. Add(' vp:=nil;');
  25283. Add(' vp:=vp;');
  25284. Add(' vp:=@doit;'); // ok in fpc and delphi
  25285. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25286. Add(' vp;'); // ok in fpc and delphi
  25287. Add(' vp();');
  25288. Add(' vp(2);');
  25289. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25290. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25291. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25292. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25293. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25294. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25295. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25296. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25297. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25298. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25299. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25300. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25301. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25302. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25303. Add(' b:=Assigned(vp);');
  25304. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25305. Add(' doit(vp());'); // ok in fpc and delphi
  25306. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25307. ConvertProgram;
  25308. CheckSource('TestProcType_FunctionDelphi',
  25309. LinesToStr([ // statements
  25310. 'this.DoIt = function(vI) {',
  25311. ' var Result = 0;',
  25312. ' return Result;',
  25313. '};',
  25314. 'this.b = false;',
  25315. 'this.vP = null;',
  25316. 'this.vQ = null;'
  25317. ]),
  25318. LinesToStr([ // $mod.$main
  25319. '$mod.vP = null;',
  25320. '$mod.vP = $mod.vP;',
  25321. '$mod.vP = $mod.DoIt;',
  25322. '$mod.vP = $mod.DoIt;',
  25323. '$mod.vP(1);',
  25324. '$mod.vP(1);',
  25325. '$mod.vP(2);',
  25326. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25327. '$mod.b = $mod.vP(1) === 3;',
  25328. '$mod.b = 4 === $mod.vP(1);',
  25329. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25330. '$mod.b = $mod.vP(1) !== 5;',
  25331. '$mod.b = 6 !== $mod.vP(1);',
  25332. '$mod.b = $mod.vP != null;',
  25333. '$mod.DoIt($mod.vP(1));',
  25334. '$mod.DoIt($mod.vP(1));',
  25335. '$mod.DoIt($mod.vP(2));',
  25336. '']));
  25337. end;
  25338. procedure TTestModule.TestProcType_ProcedureDelphi;
  25339. begin
  25340. StartProgram(false);
  25341. Add('{$mode Delphi}');
  25342. Add('type');
  25343. Add(' TProc = procedure;');
  25344. Add('procedure DoIt;');
  25345. Add('begin end;');
  25346. Add('var');
  25347. Add(' b: boolean;');
  25348. Add(' vP, vQ: tproc;');
  25349. Add('begin');
  25350. Add(' vp:=nil;');
  25351. Add(' vp:=vp;');
  25352. Add(' vp:=vq;');
  25353. 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
  25354. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25355. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25356. Add(' vp;'); // ok in fpc and delphi
  25357. Add(' vp();');
  25358. // equal
  25359. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25360. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25361. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25362. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25363. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25364. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25365. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25366. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25367. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25368. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25369. // unequal
  25370. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25371. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25372. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25373. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25374. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25375. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25376. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25377. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25378. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25379. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25380. Add(' b:=Assigned(vp);');
  25381. ConvertProgram;
  25382. CheckSource('TestProcType_ProcedureDelphi',
  25383. LinesToStr([ // statements
  25384. 'this.DoIt = function() {',
  25385. '};',
  25386. 'this.b = false;',
  25387. 'this.vP = null;',
  25388. 'this.vQ = null;'
  25389. ]),
  25390. LinesToStr([ // $mod.$main
  25391. '$mod.vP = null;',
  25392. '$mod.vP = $mod.vP;',
  25393. '$mod.vP = $mod.vQ;',
  25394. '$mod.vP = $mod.DoIt;',
  25395. '$mod.vP = $mod.DoIt;',
  25396. '$mod.vP();',
  25397. '$mod.vP();',
  25398. '$mod.b = $mod.vP === null;',
  25399. '$mod.b = null === $mod.vP;',
  25400. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25401. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25402. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25403. '$mod.b = $mod.vP !== null;',
  25404. '$mod.b = null !== $mod.vP;',
  25405. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25406. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25407. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25408. '$mod.b = $mod.vP != null;',
  25409. '']));
  25410. end;
  25411. procedure TTestModule.TestProcType_AsParam;
  25412. begin
  25413. StartProgram(false);
  25414. Add('type');
  25415. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25416. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25417. Add('var vJ: tfuncint;');
  25418. Add('begin');
  25419. Add(' vg:=vg;');
  25420. Add(' vj:=vh;');
  25421. Add(' vi:=vi;');
  25422. Add(' doit(vg,vg,vg);');
  25423. Add(' doit(vh,vh,vj);');
  25424. Add(' doit(vi,vi,vi);');
  25425. Add(' doit(vj,vj,vj);');
  25426. Add('end;');
  25427. Add('var i: tfuncint;');
  25428. Add('begin');
  25429. Add(' doit(i,i,i);');
  25430. ConvertProgram;
  25431. CheckSource('TestProcType_AsParam',
  25432. LinesToStr([ // statements
  25433. 'this.DoIt = function (vG,vH,vI) {',
  25434. ' var vJ = null;',
  25435. ' vG = vG;',
  25436. ' vJ = vH;',
  25437. ' vI.set(vI.get());',
  25438. ' $mod.DoIt(vG, vG, {',
  25439. ' get: function () {',
  25440. ' return vG;',
  25441. ' },',
  25442. ' set: function (v) {',
  25443. ' vG = v;',
  25444. ' }',
  25445. ' });',
  25446. ' $mod.DoIt(vH, vH, {',
  25447. ' get: function () {',
  25448. ' return vJ;',
  25449. ' },',
  25450. ' set: function (v) {',
  25451. ' vJ = v;',
  25452. ' }',
  25453. ' });',
  25454. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25455. ' $mod.DoIt(vJ, vJ, {',
  25456. ' get: function () {',
  25457. ' return vJ;',
  25458. ' },',
  25459. ' set: function (v) {',
  25460. ' vJ = v;',
  25461. ' }',
  25462. ' });',
  25463. '};',
  25464. 'this.i = null;'
  25465. ]),
  25466. LinesToStr([
  25467. '$mod.DoIt($mod.i,$mod.i,{',
  25468. ' p: $mod,',
  25469. ' get: function () {',
  25470. ' return this.p.i;',
  25471. ' },',
  25472. ' set: function (v) {',
  25473. ' this.p.i = v;',
  25474. ' }',
  25475. '});'
  25476. ]));
  25477. end;
  25478. procedure TTestModule.TestProcType_MethodFPC;
  25479. begin
  25480. StartProgram(false);
  25481. Add('type');
  25482. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25483. Add(' TObject = class');
  25484. Add(' function DoIt(vA: longint = 1): longint;');
  25485. Add(' end;');
  25486. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25487. Add('begin');
  25488. Add('end;');
  25489. Add('var');
  25490. Add(' Obj: TObject;');
  25491. Add(' vP: tfuncint;');
  25492. Add(' b: boolean;');
  25493. Add('begin');
  25494. Add(' vp:[email protected];'); // ok in fpc and delphi
  25495. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25496. Add(' vp;'); // ok in fpc and delphi
  25497. Add(' vp();');
  25498. Add(' vp(2);');
  25499. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25500. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25501. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25502. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25503. ConvertProgram;
  25504. CheckSource('TestProcType_MethodFPC',
  25505. LinesToStr([ // statements
  25506. 'rtl.createClass(this, "TObject", null, function () {',
  25507. ' this.$init = function () {',
  25508. ' };',
  25509. ' this.$final = function () {',
  25510. ' };',
  25511. ' this.DoIt = function (vA) {',
  25512. ' var Result = 0;',
  25513. ' return Result;',
  25514. ' };',
  25515. '});',
  25516. 'this.Obj = null;',
  25517. 'this.vP = null;',
  25518. 'this.b = false;'
  25519. ]),
  25520. LinesToStr([
  25521. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25522. '$mod.vP(1);',
  25523. '$mod.vP(1);',
  25524. '$mod.vP(2);',
  25525. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25526. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25527. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25528. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25529. '']));
  25530. end;
  25531. procedure TTestModule.TestProcType_MethodDelphi;
  25532. begin
  25533. StartProgram(false);
  25534. Add([
  25535. '{$mode delphi}',
  25536. 'type',
  25537. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25538. ' TObject = class',
  25539. ' function DoIt(vA: longint = 1): longint;',
  25540. ' end;',
  25541. 'function TObject.DoIt(vA: longint = 1): longint;',
  25542. 'begin',
  25543. 'end;',
  25544. 'var',
  25545. ' Obj: TObject;',
  25546. ' vP: tfuncint;',
  25547. ' b: boolean;',
  25548. 'begin',
  25549. ' vp:[email protected];', // ok in fpc and delphi
  25550. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25551. ' vp;', // ok in fpc and delphi
  25552. ' vp();',
  25553. ' vp(2);',
  25554. //' b:[email protected];', // ok in fpc, illegal in delphi
  25555. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25556. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25557. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25558. '']);
  25559. ConvertProgram;
  25560. CheckSource('TestProcType_MethodDelphi',
  25561. LinesToStr([ // statements
  25562. 'rtl.createClass(this, "TObject", null, function () {',
  25563. ' this.$init = function () {',
  25564. ' };',
  25565. ' this.$final = function () {',
  25566. ' };',
  25567. ' this.DoIt = function (vA) {',
  25568. ' var Result = 0;',
  25569. ' return Result;',
  25570. ' };',
  25571. '});',
  25572. 'this.Obj = null;',
  25573. 'this.vP = null;',
  25574. 'this.b = false;'
  25575. ]),
  25576. LinesToStr([
  25577. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25578. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25579. '$mod.vP(1);',
  25580. '$mod.vP(1);',
  25581. '$mod.vP(2);',
  25582. '']));
  25583. end;
  25584. procedure TTestModule.TestProcType_PropertyFPC;
  25585. begin
  25586. StartProgram(false);
  25587. Add('type');
  25588. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25589. Add(' TObject = class');
  25590. Add(' FOnFoo: TFuncInt;');
  25591. Add(' function DoIt(vA: longint = 1): longint;');
  25592. Add(' function GetFoo: TFuncInt;');
  25593. Add(' procedure SetFoo(const Value: TFuncInt);');
  25594. Add(' function GetEvents(Index: longint): TFuncInt;');
  25595. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25596. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25597. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25598. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25599. Add(' end;');
  25600. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25601. Add('function tobject.getfoo: tfuncint; begin end;');
  25602. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25603. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25604. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25605. Add('var');
  25606. Add(' Obj: TObject;');
  25607. Add(' vP: tfuncint;');
  25608. Add(' b: boolean;');
  25609. Add('begin');
  25610. Add(' obj.onfoo:=nil;');
  25611. Add(' obj.onbar:=nil;');
  25612. Add(' obj.events[1]:=nil;');
  25613. Add(' obj.onfoo:=obj.onfoo;');
  25614. Add(' obj.onbar:=obj.onbar;');
  25615. Add(' obj.events[2]:=obj.events[3];');
  25616. Add(' obj.onfoo:[email protected];');
  25617. Add(' obj.onbar:[email protected];');
  25618. Add(' obj.events[4]:[email protected];');
  25619. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25620. //Add(' obj.onbar:=obj.doit;'); // delphi
  25621. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25622. Add(' obj.onfoo;');
  25623. Add(' obj.onbar;');
  25624. //Add(' obj.events[5];'); ToDo in pasresolver
  25625. Add(' obj.onfoo();');
  25626. Add(' obj.onbar();');
  25627. Add(' obj.events[6]();');
  25628. Add(' b:=obj.onfoo=nil;');
  25629. Add(' b:=obj.onbar=nil;');
  25630. Add(' b:=obj.events[7]=nil;');
  25631. Add(' b:=obj.onfoo<>nil;');
  25632. Add(' b:=obj.onbar<>nil;');
  25633. Add(' b:=obj.events[8]<>nil;');
  25634. Add(' b:=obj.onfoo=vp;');
  25635. Add(' b:=obj.onbar=vp;');
  25636. Add(' b:=obj.events[9]=vp;');
  25637. Add(' b:=obj.onfoo=obj.onfoo;');
  25638. Add(' b:=obj.onbar=obj.onfoo;');
  25639. Add(' b:=obj.events[10]=obj.onfoo;');
  25640. Add(' b:=obj.onfoo<>obj.onfoo;');
  25641. Add(' b:=obj.onbar<>obj.onfoo;');
  25642. Add(' b:=obj.events[11]<>obj.onfoo;');
  25643. Add(' b:[email protected];');
  25644. Add(' b:[email protected];');
  25645. Add(' b:=obj.events[12][email protected];');
  25646. Add(' b:=obj.onfoo<>@obj.doit;');
  25647. Add(' b:=obj.onbar<>@obj.doit;');
  25648. Add(' b:=obj.events[12]<>@obj.doit;');
  25649. Add(' b:=Assigned(obj.onfoo);');
  25650. Add(' b:=Assigned(obj.onbar);');
  25651. Add(' b:=Assigned(obj.events[13]);');
  25652. ConvertProgram;
  25653. CheckSource('TestProcType_PropertyFPC',
  25654. LinesToStr([ // statements
  25655. 'rtl.createClass(this, "TObject", null, function () {',
  25656. ' this.$init = function () {',
  25657. ' this.FOnFoo = null;',
  25658. ' };',
  25659. ' this.$final = function () {',
  25660. ' this.FOnFoo = undefined;',
  25661. ' };',
  25662. ' this.DoIt = function (vA) {',
  25663. ' var Result = 0;',
  25664. ' return Result;',
  25665. ' };',
  25666. 'this.GetFoo = function () {',
  25667. ' var Result = null;',
  25668. ' return Result;',
  25669. '};',
  25670. 'this.SetFoo = function (Value) {',
  25671. '};',
  25672. 'this.GetEvents = function (Index) {',
  25673. ' var Result = null;',
  25674. ' return Result;',
  25675. '};',
  25676. 'this.SetEvents = function (Index, Value) {',
  25677. '};',
  25678. '});',
  25679. 'this.Obj = null;',
  25680. 'this.vP = null;',
  25681. 'this.b = false;'
  25682. ]),
  25683. LinesToStr([
  25684. '$mod.Obj.FOnFoo = null;',
  25685. '$mod.Obj.SetFoo(null);',
  25686. '$mod.Obj.SetEvents(1, null);',
  25687. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25688. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25689. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25690. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25691. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25692. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25693. '$mod.Obj.FOnFoo(1);',
  25694. '$mod.Obj.GetFoo();',
  25695. '$mod.Obj.FOnFoo(1);',
  25696. '$mod.Obj.GetFoo()(1);',
  25697. '$mod.Obj.GetEvents(6)(1);',
  25698. '$mod.b = $mod.Obj.FOnFoo === null;',
  25699. '$mod.b = $mod.Obj.GetFoo() === null;',
  25700. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25701. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25702. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25703. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25704. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25705. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25706. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25707. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25708. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25709. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25710. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25711. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25712. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25713. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25714. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25715. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25716. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25717. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25718. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25719. '$mod.b = $mod.Obj.FOnFoo != null;',
  25720. '$mod.b = $mod.Obj.GetFoo() != null;',
  25721. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25722. '']));
  25723. end;
  25724. procedure TTestModule.TestProcType_PropertyDelphi;
  25725. begin
  25726. StartProgram(false);
  25727. Add('{$mode delphi}');
  25728. Add('type');
  25729. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25730. Add(' TObject = class');
  25731. Add(' FOnFoo: TFuncInt;');
  25732. Add(' function DoIt(vA: longint = 1): longint;');
  25733. Add(' function GetFoo: TFuncInt;');
  25734. Add(' procedure SetFoo(const Value: TFuncInt);');
  25735. Add(' function GetEvents(Index: longint): TFuncInt;');
  25736. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25737. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25738. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25739. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25740. Add(' end;');
  25741. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25742. Add('function tobject.getfoo: tfuncint; begin end;');
  25743. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25744. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25745. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25746. Add('var');
  25747. Add(' Obj: TObject;');
  25748. Add(' vP: tfuncint;');
  25749. Add(' b: boolean;');
  25750. Add('begin');
  25751. Add(' obj.onfoo:=nil;');
  25752. Add(' obj.onbar:=nil;');
  25753. Add(' obj.events[1]:=nil;');
  25754. Add(' obj.onfoo:=obj.onfoo;');
  25755. Add(' obj.onbar:=obj.onbar;');
  25756. Add(' obj.events[2]:=obj.events[3];');
  25757. Add(' obj.onfoo:[email protected];');
  25758. Add(' obj.onbar:[email protected];');
  25759. Add(' obj.events[4]:[email protected];');
  25760. Add(' obj.onfoo:=obj.doit;'); // delphi
  25761. Add(' obj.onbar:=obj.doit;'); // delphi
  25762. Add(' obj.events[4]:=obj.doit;'); // delphi
  25763. Add(' obj.onfoo;');
  25764. Add(' obj.onbar;');
  25765. //Add(' obj.events[5];'); ToDo in pasresolver
  25766. Add(' obj.onfoo();');
  25767. Add(' obj.onbar();');
  25768. Add(' obj.events[6]();');
  25769. //Add(' b:=obj.onfoo=nil;'); // fpc
  25770. //Add(' b:=obj.onbar=nil;'); // fpc
  25771. //Add(' b:=obj.events[7]=nil;'); // fpc
  25772. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25773. //Add(' b:=obj.onbar<>nil;'); // fpc
  25774. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25775. Add(' b:=obj.onfoo=vp;');
  25776. Add(' b:=obj.onbar=vp;');
  25777. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25778. Add(' b:=obj.onfoo=obj.onfoo;');
  25779. Add(' b:=obj.onbar=obj.onfoo;');
  25780. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25781. Add(' b:=obj.onfoo<>obj.onfoo;');
  25782. Add(' b:=obj.onbar<>obj.onfoo;');
  25783. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25784. //Add(' b:[email protected];'); // fpc
  25785. //Add(' b:[email protected];'); // fpc
  25786. //Add(' b:=obj.events[12][email protected];'); // fpc
  25787. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25788. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25789. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25790. Add(' b:=Assigned(obj.onfoo);');
  25791. Add(' b:=Assigned(obj.onbar);');
  25792. Add(' b:=Assigned(obj.events[13]);');
  25793. ConvertProgram;
  25794. CheckSource('TestProcType_PropertyDelphi',
  25795. LinesToStr([ // statements
  25796. 'rtl.createClass(this, "TObject", null, function () {',
  25797. ' this.$init = function () {',
  25798. ' this.FOnFoo = null;',
  25799. ' };',
  25800. ' this.$final = function () {',
  25801. ' this.FOnFoo = undefined;',
  25802. ' };',
  25803. ' this.DoIt = function (vA) {',
  25804. ' var Result = 0;',
  25805. ' return Result;',
  25806. ' };',
  25807. 'this.GetFoo = function () {',
  25808. ' var Result = null;',
  25809. ' return Result;',
  25810. '};',
  25811. 'this.SetFoo = function (Value) {',
  25812. '};',
  25813. 'this.GetEvents = function (Index) {',
  25814. ' var Result = null;',
  25815. ' return Result;',
  25816. '};',
  25817. 'this.SetEvents = function (Index, Value) {',
  25818. '};',
  25819. '});',
  25820. 'this.Obj = null;',
  25821. 'this.vP = null;',
  25822. 'this.b = false;'
  25823. ]),
  25824. LinesToStr([
  25825. '$mod.Obj.FOnFoo = null;',
  25826. '$mod.Obj.SetFoo(null);',
  25827. '$mod.Obj.SetEvents(1, null);',
  25828. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25829. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25830. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25831. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25832. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25833. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25834. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25835. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25836. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25837. '$mod.Obj.FOnFoo(1);',
  25838. '$mod.Obj.GetFoo();',
  25839. '$mod.Obj.FOnFoo(1);',
  25840. '$mod.Obj.GetFoo()(1);',
  25841. '$mod.Obj.GetEvents(6)(1);',
  25842. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25843. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25844. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25845. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25846. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25847. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25848. '$mod.b = $mod.Obj.FOnFoo != null;',
  25849. '$mod.b = $mod.Obj.GetFoo() != null;',
  25850. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25851. '']));
  25852. end;
  25853. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25854. begin
  25855. StartProgram(false);
  25856. Add('type');
  25857. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25858. Add(' TObject = class');
  25859. Add(' FOnFoo: TFuncInt;');
  25860. Add(' function DoIt(vA: longint = 1): longint;');
  25861. Add(' function GetFoo: TFuncInt;');
  25862. Add(' procedure SetFoo(const Value: TFuncInt);');
  25863. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25864. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25865. Add(' end;');
  25866. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25867. Add('function tobject.getfoo: tfuncint; begin end;');
  25868. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25869. Add('var');
  25870. Add(' Obj: TObject;');
  25871. Add(' vP: tfuncint;');
  25872. Add(' b: boolean;');
  25873. Add('begin');
  25874. Add('with obj do begin');
  25875. Add(' fonfoo:=nil;');
  25876. Add(' onfoo:=nil;');
  25877. Add(' onbar:=nil;');
  25878. Add(' fonfoo:=fonfoo;');
  25879. Add(' onfoo:=onfoo;');
  25880. Add(' onbar:=onbar;');
  25881. Add(' fonfoo:=@doit;');
  25882. Add(' onfoo:=@doit;');
  25883. Add(' onbar:=@doit;');
  25884. //Add(' fonfoo:=doit;'); // delphi
  25885. //Add(' onfoo:=doit;'); // delphi
  25886. //Add(' onbar:=doit;'); // delphi
  25887. Add(' fonfoo;');
  25888. Add(' onfoo;');
  25889. Add(' onbar;');
  25890. Add(' fonfoo();');
  25891. Add(' onfoo();');
  25892. Add(' onbar();');
  25893. Add(' b:=fonfoo=nil;');
  25894. Add(' b:=onfoo=nil;');
  25895. Add(' b:=onbar=nil;');
  25896. Add(' b:=fonfoo<>nil;');
  25897. Add(' b:=onfoo<>nil;');
  25898. Add(' b:=onbar<>nil;');
  25899. Add(' b:=fonfoo=vp;');
  25900. Add(' b:=onfoo=vp;');
  25901. Add(' b:=onbar=vp;');
  25902. Add(' b:=fonfoo=fonfoo;');
  25903. Add(' b:=onfoo=onfoo;');
  25904. Add(' b:=onbar=onfoo;');
  25905. Add(' b:=fonfoo<>fonfoo;');
  25906. Add(' b:=onfoo<>onfoo;');
  25907. Add(' b:=onbar<>onfoo;');
  25908. Add(' b:=fonfoo=@doit;');
  25909. Add(' b:=onfoo=@doit;');
  25910. Add(' b:=onbar=@doit;');
  25911. Add(' b:=fonfoo<>@doit;');
  25912. Add(' b:=onfoo<>@doit;');
  25913. Add(' b:=onbar<>@doit;');
  25914. Add(' b:=Assigned(fonfoo);');
  25915. Add(' b:=Assigned(onfoo);');
  25916. Add(' b:=Assigned(onbar);');
  25917. Add('end;');
  25918. ConvertProgram;
  25919. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25920. LinesToStr([ // statements
  25921. 'rtl.createClass(this, "TObject", null, function () {',
  25922. ' this.$init = function () {',
  25923. ' this.FOnFoo = null;',
  25924. ' };',
  25925. ' this.$final = function () {',
  25926. ' this.FOnFoo = undefined;',
  25927. ' };',
  25928. ' this.DoIt = function (vA) {',
  25929. ' var Result = 0;',
  25930. ' return Result;',
  25931. ' };',
  25932. ' this.GetFoo = function () {',
  25933. ' var Result = null;',
  25934. ' return Result;',
  25935. ' };',
  25936. ' this.SetFoo = function (Value) {',
  25937. ' };',
  25938. '});',
  25939. 'this.Obj = null;',
  25940. 'this.vP = null;',
  25941. 'this.b = false;'
  25942. ]),
  25943. LinesToStr([
  25944. 'var $with = $mod.Obj;',
  25945. '$with.FOnFoo = null;',
  25946. '$with.FOnFoo = null;',
  25947. '$with.SetFoo(null);',
  25948. '$with.FOnFoo = $with.FOnFoo;',
  25949. '$with.FOnFoo = $with.FOnFoo;',
  25950. '$with.SetFoo($with.GetFoo());',
  25951. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25952. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25953. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25954. '$with.FOnFoo(1);',
  25955. '$with.FOnFoo(1);',
  25956. '$with.GetFoo();',
  25957. '$with.FOnFoo(1);',
  25958. '$with.FOnFoo(1);',
  25959. '$with.GetFoo()(1);',
  25960. '$mod.b = $with.FOnFoo === null;',
  25961. '$mod.b = $with.FOnFoo === null;',
  25962. '$mod.b = $with.GetFoo() === null;',
  25963. '$mod.b = $with.FOnFoo !== null;',
  25964. '$mod.b = $with.FOnFoo !== null;',
  25965. '$mod.b = $with.GetFoo() !== null;',
  25966. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25967. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25968. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25969. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25970. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25971. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25972. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25973. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25974. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25975. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25976. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25977. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25978. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25979. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25980. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25981. '$mod.b = $with.FOnFoo != null;',
  25982. '$mod.b = $with.FOnFoo != null;',
  25983. '$mod.b = $with.GetFoo() != null;',
  25984. '']));
  25985. end;
  25986. procedure TTestModule.TestProcType_Nested;
  25987. begin
  25988. StartProgram(false);
  25989. Add([
  25990. 'type',
  25991. ' TProcInt = procedure(vI: longint = 1);',
  25992. 'procedure DoIt(vJ: longint);',
  25993. 'var aProc: TProcInt;',
  25994. ' b: boolean;',
  25995. ' procedure Sub(vK: longint);',
  25996. ' var aSub: TProcInt;',
  25997. ' procedure SubSub(vK: longint);',
  25998. ' var aSubSub: TProcInt;',
  25999. ' begin;',
  26000. ' aProc:=@DoIt;',
  26001. ' aSub:=@DoIt;',
  26002. ' aSubSub:=@DoIt;',
  26003. ' aProc:=@Sub;',
  26004. ' aSub:=@Sub;',
  26005. ' aSubSub:=@Sub;',
  26006. ' aProc:=@SubSub;',
  26007. ' aSub:=@SubSub;',
  26008. ' aSubSub:=@SubSub;',
  26009. ' end;',
  26010. ' begin;',
  26011. ' end;',
  26012. 'begin;',
  26013. ' aProc:=@Sub;',
  26014. ' b:=aProc=@Sub;',
  26015. ' b:=@Sub=aProc;',
  26016. 'end;',
  26017. 'begin',
  26018. '']);
  26019. ConvertProgram;
  26020. CheckSource('TestProcType_Nested',
  26021. LinesToStr([ // statements
  26022. 'this.DoIt = function (vJ) {',
  26023. ' var aProc = null;',
  26024. ' var b = false;',
  26025. ' function Sub(vK) {',
  26026. ' var aSub = null;',
  26027. ' function SubSub(vK) {',
  26028. ' var aSubSub = null;',
  26029. ' aProc = $mod.DoIt;',
  26030. ' aSub = $mod.DoIt;',
  26031. ' aSubSub = $mod.DoIt;',
  26032. ' aProc = Sub;',
  26033. ' aSub = Sub;',
  26034. ' aSubSub = Sub;',
  26035. ' aProc = SubSub;',
  26036. ' aSub = SubSub;',
  26037. ' aSubSub = SubSub;',
  26038. ' };',
  26039. ' };',
  26040. ' aProc = Sub;',
  26041. ' b = rtl.eqCallback(aProc, Sub);',
  26042. ' b = rtl.eqCallback(Sub, aProc);',
  26043. '};',
  26044. '']),
  26045. LinesToStr([ // $mod.$main
  26046. '']));
  26047. end;
  26048. procedure TTestModule.TestProcType_NestedOfObject;
  26049. begin
  26050. StartProgram(false);
  26051. Add([
  26052. 'type',
  26053. ' TProcInt = procedure(vI: longint = 1) of object;',
  26054. ' TObject = class',
  26055. ' procedure DoIt(vJ: longint);',
  26056. ' end;',
  26057. 'procedure TObject.DoIt(vJ: longint);',
  26058. 'var aProc: TProcInt;',
  26059. ' b: boolean;',
  26060. ' procedure Sub(vK: longint);',
  26061. ' var aSub: TProcInt;',
  26062. ' procedure SubSub(vK: longint);',
  26063. ' var aSubSub: TProcInt;',
  26064. ' begin;',
  26065. ' aProc:=@DoIt;',
  26066. ' aSub:=@DoIt;',
  26067. ' aSubSub:=@DoIt;',
  26068. ' aProc:=@Sub;',
  26069. ' aSub:=@Sub;',
  26070. ' aSubSub:=@Sub;',
  26071. ' aProc:=@SubSub;',
  26072. ' aSub:=@SubSub;',
  26073. ' aSubSub:=@SubSub;',
  26074. ' end;',
  26075. ' begin;',
  26076. ' end;',
  26077. 'begin;',
  26078. ' aProc:=@Sub;',
  26079. ' b:=aProc=@Sub;',
  26080. ' b:=@Sub=aProc;',
  26081. 'end;',
  26082. 'begin',
  26083. '']);
  26084. ConvertProgram;
  26085. CheckSource('TestProcType_Nested',
  26086. LinesToStr([ // statements
  26087. 'rtl.createClass(this, "TObject", null, function () {',
  26088. ' this.$init = function () {',
  26089. ' };',
  26090. ' this.$final = function () {',
  26091. ' };',
  26092. ' this.DoIt = function (vJ) {',
  26093. ' var $Self = this;',
  26094. ' var aProc = null;',
  26095. ' var b = false;',
  26096. ' function Sub(vK) {',
  26097. ' var aSub = null;',
  26098. ' function SubSub(vK) {',
  26099. ' var aSubSub = null;',
  26100. ' aProc = rtl.createCallback($Self, "DoIt");',
  26101. ' aSub = rtl.createCallback($Self, "DoIt");',
  26102. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26103. ' aProc = Sub;',
  26104. ' aSub = Sub;',
  26105. ' aSubSub = Sub;',
  26106. ' aProc = SubSub;',
  26107. ' aSub = SubSub;',
  26108. ' aSubSub = SubSub;',
  26109. ' };',
  26110. ' };',
  26111. ' aProc = Sub;',
  26112. ' b = rtl.eqCallback(aProc, Sub);',
  26113. ' b = rtl.eqCallback(Sub, aProc);',
  26114. ' };',
  26115. '});',
  26116. '']),
  26117. LinesToStr([ // $mod.$main
  26118. '']));
  26119. end;
  26120. procedure TTestModule.TestProcType_ReferenceToProc;
  26121. begin
  26122. StartProgram(false);
  26123. Add([
  26124. 'type',
  26125. ' TProcRef = reference to procedure(i: longint = 0);',
  26126. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26127. 'var',
  26128. ' p: TProcRef;',
  26129. ' f: TFuncRef;',
  26130. 'procedure DoIt(i: longint);',
  26131. 'begin',
  26132. 'end;',
  26133. 'function GetIt(i: longint): longint;',
  26134. 'begin',
  26135. ' p:=@DoIt;',
  26136. ' f:=@GetIt;',
  26137. ' f;',
  26138. ' f();',
  26139. ' f(1);',
  26140. 'end;',
  26141. 'begin',
  26142. ' p:=@DoIt;',
  26143. ' f:=@GetIt;',
  26144. ' f;',
  26145. ' f();',
  26146. ' f(1);',
  26147. ' p:=TProcRef(f);',
  26148. '']);
  26149. ConvertProgram;
  26150. CheckSource('TestProcType_ReferenceToProc',
  26151. LinesToStr([ // statements
  26152. 'this.p = null;',
  26153. 'this.f = null;',
  26154. 'this.DoIt = function (i) {',
  26155. '};',
  26156. 'this.GetIt = function (i) {',
  26157. ' var Result = 0;',
  26158. ' $mod.p = $mod.DoIt;',
  26159. ' $mod.f = $mod.GetIt;',
  26160. ' $mod.f(0);',
  26161. ' $mod.f(0);',
  26162. ' $mod.f(1);',
  26163. ' return Result;',
  26164. '};',
  26165. '']),
  26166. LinesToStr([ // $mod.$main
  26167. '$mod.p = $mod.DoIt;',
  26168. '$mod.f = $mod.GetIt;',
  26169. '$mod.f(0);',
  26170. '$mod.f(0);',
  26171. '$mod.f(1);',
  26172. '$mod.p = $mod.f;',
  26173. '']));
  26174. end;
  26175. procedure TTestModule.TestProcType_ReferenceToMethod;
  26176. begin
  26177. StartProgram(false);
  26178. Add([
  26179. 'type',
  26180. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26181. ' TObject = class',
  26182. ' function Grow(s: longint): longint;',
  26183. ' end;',
  26184. 'var',
  26185. ' f: tfuncref;',
  26186. 'function tobject.grow(s: longint): longint;',
  26187. ' function GrowSub(i: longint): longint;',
  26188. ' begin',
  26189. ' f:=@grow;',
  26190. ' f:=@growsub;',
  26191. ' end;',
  26192. 'begin',
  26193. ' f:=@grow;',
  26194. ' f:=@growsub;',
  26195. 'end;',
  26196. 'begin',
  26197. '']);
  26198. ConvertProgram;
  26199. CheckSource('TestProcType_ReferenceToMethod',
  26200. LinesToStr([ // statements
  26201. 'rtl.createClass(this, "TObject", null, function () {',
  26202. ' this.$init = function () {',
  26203. ' };',
  26204. ' this.$final = function () {',
  26205. ' };',
  26206. ' this.Grow = function (s) {',
  26207. ' var $Self = this;',
  26208. ' var Result = 0;',
  26209. ' function GrowSub(i) {',
  26210. ' var Result = 0;',
  26211. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26212. ' $mod.f = GrowSub;',
  26213. ' return Result;',
  26214. ' };',
  26215. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26216. ' $mod.f = GrowSub;',
  26217. ' return Result;',
  26218. ' };',
  26219. '});',
  26220. 'this.f = null;',
  26221. '']),
  26222. LinesToStr([ // $mod.$main
  26223. '']));
  26224. end;
  26225. procedure TTestModule.TestProcType_Typecast;
  26226. begin
  26227. StartProgram(false);
  26228. Add([
  26229. 'type',
  26230. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26231. ' TEvent = procedure of object;',
  26232. ' TGetter = function:longint of object;',
  26233. ' TProcA = procedure(i: longint);',
  26234. ' TFuncB = function(i, j: longint): longint;',
  26235. 'procedure DoIt(); varargs; begin end;',
  26236. 'var',
  26237. ' Notify: tnotifyevent;',
  26238. ' Event: tevent;',
  26239. ' Getter: tgetter;',
  26240. ' ProcA: tproca;',
  26241. ' FuncB: tfuncb;',
  26242. ' p: pointer;',
  26243. 'begin',
  26244. ' notify:=tnotifyevent(event);',
  26245. ' event:=tevent(event);',
  26246. ' event:=tevent(notify);',
  26247. ' event:=tevent(getter);',
  26248. ' event:=tevent(proca);',
  26249. ' proca:=tproca(funcb);',
  26250. ' funcb:=tfuncb(funcb);',
  26251. ' funcb:=tfuncb(proca);',
  26252. ' funcb:=tfuncb(getter);',
  26253. ' proca:=tproca(p);',
  26254. ' funcb:=tfuncb(p);',
  26255. ' getter:=tgetter(p);',
  26256. ' p:=pointer(notify);',
  26257. ' p:=notify;',
  26258. ' p:=pointer(proca);',
  26259. ' p:=proca;',
  26260. ' p:=pointer(funcb);',
  26261. ' p:=funcb;',
  26262. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26263. '']);
  26264. ConvertProgram;
  26265. CheckSource('TestProcType_Typecast',
  26266. LinesToStr([ // statements
  26267. 'this.DoIt = function () {',
  26268. '};',
  26269. 'this.Notify = null;',
  26270. 'this.Event = null;',
  26271. 'this.Getter = null;',
  26272. 'this.ProcA = null;',
  26273. 'this.FuncB = null;',
  26274. 'this.p = null;',
  26275. '']),
  26276. LinesToStr([ // $mod.$main
  26277. '$mod.Notify = $mod.Event;',
  26278. '$mod.Event = $mod.Event;',
  26279. '$mod.Event = $mod.Notify;',
  26280. '$mod.Event = $mod.Getter;',
  26281. '$mod.Event = $mod.ProcA;',
  26282. '$mod.ProcA = $mod.FuncB;',
  26283. '$mod.FuncB = $mod.FuncB;',
  26284. '$mod.FuncB = $mod.ProcA;',
  26285. '$mod.FuncB = $mod.Getter;',
  26286. '$mod.ProcA = $mod.p;',
  26287. '$mod.FuncB = $mod.p;',
  26288. '$mod.Getter = $mod.p;',
  26289. '$mod.p = $mod.Notify;',
  26290. '$mod.p = $mod.Notify;',
  26291. '$mod.p = $mod.ProcA;',
  26292. '$mod.p = $mod.ProcA;',
  26293. '$mod.p = $mod.FuncB;',
  26294. '$mod.p = $mod.FuncB;',
  26295. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26296. '']));
  26297. end;
  26298. procedure TTestModule.TestProcType_PassProcToUntyped;
  26299. begin
  26300. StartProgram(false);
  26301. Add([
  26302. 'type',
  26303. ' TEvent = procedure of object;',
  26304. ' TFunc = function: longint;',
  26305. 'procedure DoIt(); varargs; begin end;',
  26306. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26307. 'var',
  26308. ' Event: tevent;',
  26309. ' Func: TFunc;',
  26310. 'begin',
  26311. ' doit(event,func);',
  26312. ' dosome(event,event,event);',
  26313. ' dosome(func,func,func);',
  26314. '']);
  26315. ConvertProgram;
  26316. CheckSource('TestProcType_PassProcToUntyped',
  26317. LinesToStr([ // statements
  26318. 'this.DoIt = function () {',
  26319. '};',
  26320. 'this.DoSome = function (a, b, p) {',
  26321. '};',
  26322. 'this.Event = null;',
  26323. 'this.Func = null;',
  26324. '']),
  26325. LinesToStr([ // $mod.$main
  26326. '$mod.DoIt($mod.Event, $mod.Func);',
  26327. '$mod.DoSome($mod.Event, {',
  26328. ' p: $mod,',
  26329. ' get: function () {',
  26330. ' return this.p.Event;',
  26331. ' },',
  26332. ' set: function (v) {',
  26333. ' this.p.Event = v;',
  26334. ' }',
  26335. '}, $mod.Event);',
  26336. '$mod.DoSome($mod.Func, {',
  26337. ' p: $mod,',
  26338. ' get: function () {',
  26339. ' return this.p.Func;',
  26340. ' },',
  26341. ' set: function (v) {',
  26342. ' this.p.Func = v;',
  26343. ' }',
  26344. '}, $mod.Func);',
  26345. '']));
  26346. end;
  26347. procedure TTestModule.TestProcType_PassProcToArray;
  26348. begin
  26349. StartProgram(false);
  26350. Add([
  26351. 'type',
  26352. ' TFunc = function: longint;',
  26353. ' TArrFunc = array of TFunc;',
  26354. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26355. 'function GetIt: longint; begin end;',
  26356. 'var',
  26357. ' Func: tfunc;',
  26358. 'begin',
  26359. ' doit([]);',
  26360. ' doit([@GetIt]);',
  26361. ' doit([Func]);',
  26362. '']);
  26363. ConvertProgram;
  26364. CheckSource('TestProcType_PassProcToArray',
  26365. LinesToStr([ // statements
  26366. 'this.DoIt = function (Arr) {',
  26367. '};',
  26368. 'this.GetIt = function () {',
  26369. ' var Result = 0;',
  26370. ' return Result;',
  26371. '};',
  26372. 'this.Func = null;',
  26373. '']),
  26374. LinesToStr([ // $mod.$main
  26375. '$mod.DoIt([]);',
  26376. '$mod.DoIt([$mod.GetIt]);',
  26377. '$mod.DoIt([$mod.Func]);',
  26378. '']));
  26379. end;
  26380. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26381. begin
  26382. StartProgram(false);
  26383. Add([
  26384. '{$modeswitch externalclass}',
  26385. 'type',
  26386. ' TProc = reference to procedure(i: longint); safecall;',
  26387. ' TEvent = procedure(i: longint) of object; safecall;',
  26388. ' TExtA = class external name ''ExtObj''',
  26389. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26390. ' procedure DoSome(Id: longint = 1);',
  26391. ' procedure SetOnClick(const e: TEvent);',
  26392. ' property OnClick: TEvent write SetOnClick;',
  26393. ' class procedure Fly(Id: longint = 1); static;',
  26394. ' procedure SetOnShow(const p: TProc);',
  26395. ' property OnShow: TProc write SetOnShow;',
  26396. ' end;',
  26397. 'procedure Run(i: longint = 1);',
  26398. 'begin',
  26399. 'end;',
  26400. 'var',
  26401. ' Obj: texta;',
  26402. ' e: TEvent;',
  26403. ' p: TProc;',
  26404. 'begin',
  26405. ' e:=e;',
  26406. ' e:[email protected];',
  26407. ' e:[email protected];',
  26408. ' e:=TEvent(@obj.dosome);', // no safecall
  26409. ' obj.OnClick:[email protected];',
  26410. ' obj.OnClick:[email protected];',
  26411. ' obj.setonclick(@obj.doit);',
  26412. ' obj.setonclick(@obj.dosome);',
  26413. ' p:=@Run;',
  26414. ' p:[email protected];',
  26415. ' obj.OnShow:=@Run;',
  26416. ' obj.OnShow:[email protected];',
  26417. ' obj.setOnShow(@Run);',
  26418. ' obj.setOnShow(@TExtA.Fly);',
  26419. ' with obj do begin',
  26420. ' e:=@doit;',
  26421. ' e:=@dosome;',
  26422. ' OnClick:=@doit;',
  26423. ' OnClick:=@dosome;',
  26424. ' setonclick(@doit);',
  26425. ' setonclick(@dosome);',
  26426. ' OnShow:=@Run;',
  26427. ' setOnShow(@Run);',
  26428. ' end;']);
  26429. ConvertProgram;
  26430. CheckSource('TestProcType_SafeCallObjFPC',
  26431. LinesToStr([ // statements
  26432. 'this.Run = function (i) {',
  26433. '};',
  26434. 'this.Obj = null;',
  26435. 'this.e = null;',
  26436. 'this.p = null;',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '$mod.e = $mod.e;',
  26440. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26441. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26442. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26443. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26444. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26445. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26446. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26447. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26448. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26449. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26450. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26451. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26452. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26453. 'var $with = $mod.Obj;',
  26454. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26455. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26456. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26457. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26458. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26459. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26460. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26461. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26462. '']));
  26463. end;
  26464. procedure TTestModule.TestProcType_SafeCallDelphi;
  26465. begin
  26466. StartProgram(false);
  26467. Add([
  26468. '{$mode delphi}',
  26469. '{$modeswitch externalclass}',
  26470. 'type',
  26471. ' TProc = reference to procedure(i: longint); safecall;',
  26472. ' TEvent = procedure(i: longint) of object; safecall;',
  26473. ' TExtA = class external name ''ExtObj''',
  26474. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26475. ' procedure DoSome(Id: longint = 1);',
  26476. ' procedure SetOnClick(const e: TEvent);',
  26477. ' property OnClick: TEvent write SetOnClick;',
  26478. ' class procedure Fly(Id: longint = 1); static;',
  26479. ' procedure SetOnShow(const p: TProc);',
  26480. ' property OnShow: TProc write SetOnShow;',
  26481. ' end;',
  26482. 'procedure Run(i: longint = 1);',
  26483. 'begin',
  26484. 'end;',
  26485. 'var',
  26486. ' Obj: texta;',
  26487. ' e: TEvent;',
  26488. ' p: TProc;',
  26489. 'begin',
  26490. ' e:=e;',
  26491. ' e:=obj.doit;',
  26492. ' e:=obj.dosome;',
  26493. ' e:=TEvent(@obj.dosome);', // no safecall
  26494. ' obj.OnClick:=obj.doit;',
  26495. ' obj.OnClick:=obj.dosome;',
  26496. ' obj.setonclick(obj.doit);',
  26497. ' obj.setonclick(obj.dosome);',
  26498. ' p:=Run;',
  26499. ' p:=TExtA.Fly;',
  26500. ' obj.OnShow:=Run;',
  26501. ' obj.OnShow:=TExtA.Fly;',
  26502. ' obj.setOnShow(Run);',
  26503. ' obj.setOnShow(TExtA.Fly);',
  26504. ' with obj do begin',
  26505. ' e:=doit;',
  26506. ' e:=dosome;',
  26507. ' OnClick:=doit;',
  26508. ' OnClick:=dosome;',
  26509. ' setonclick(doit);',
  26510. ' setonclick(dosome);',
  26511. ' OnShow:=@Run;',
  26512. ' setOnShow(@Run);',
  26513. ' end;']);
  26514. ConvertProgram;
  26515. CheckSource('TestProcType_SafeCallDelphi',
  26516. LinesToStr([ // statements
  26517. 'this.Run = function (i) {',
  26518. '};',
  26519. 'this.Obj = null;',
  26520. 'this.e = null;',
  26521. 'this.p = null;',
  26522. '']),
  26523. LinesToStr([ // $mod.$main
  26524. '$mod.e = $mod.e;',
  26525. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26526. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26527. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26528. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26529. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26530. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26531. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26532. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26533. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26534. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26535. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26536. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26537. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26538. 'var $with = $mod.Obj;',
  26539. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26540. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26541. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26542. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26543. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26544. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26545. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26546. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26547. '']));
  26548. end;
  26549. procedure TTestModule.TestPointer;
  26550. begin
  26551. StartProgram(false);
  26552. Add(['type',
  26553. ' TObject = class end;',
  26554. ' TClass = class of TObject;',
  26555. ' TArrInt = array of longint;',
  26556. 'const',
  26557. ' n = nil;',
  26558. 'var',
  26559. ' v: jsvalue;',
  26560. ' Obj: tobject;',
  26561. ' C: tclass;',
  26562. ' a: tarrint;',
  26563. ' p: Pointer = nil;',
  26564. ' s: string;',
  26565. 'begin',
  26566. ' p:=p;',
  26567. ' p:=nil;',
  26568. ' if p=nil then;',
  26569. ' if nil=p then;',
  26570. ' if Assigned(p) then;',
  26571. ' p:=Pointer(v);',
  26572. ' p:=obj;',
  26573. ' p:=c;',
  26574. ' p:=a;',
  26575. ' p:=tobject;',
  26576. ' obj:=TObject(p);',
  26577. ' c:=TClass(p);',
  26578. ' a:=TArrInt(p);',
  26579. ' p:=n;',
  26580. ' p:=Pointer(a);',
  26581. ' p:=pointer(s);',
  26582. ' s:=string(p);',
  26583. '']);
  26584. ConvertProgram;
  26585. CheckSource('TestPointer',
  26586. LinesToStr([ // statements
  26587. 'rtl.createClass(this, "TObject", null, function () {',
  26588. ' this.$init = function () {',
  26589. ' };',
  26590. ' this.$final = function () {',
  26591. ' };',
  26592. '});',
  26593. 'this.n = null;',
  26594. 'this.v = undefined;',
  26595. 'this.Obj = null;',
  26596. 'this.C = null;',
  26597. 'this.a = [];',
  26598. 'this.p = null;',
  26599. 'this.s = "";',
  26600. '']),
  26601. LinesToStr([ // $mod.$main
  26602. '$mod.p = $mod.p;',
  26603. '$mod.p = null;',
  26604. 'if ($mod.p === null) ;',
  26605. 'if (null === $mod.p) ;',
  26606. 'if ($mod.p != null) ;',
  26607. '$mod.p = $mod.v;',
  26608. '$mod.p = $mod.Obj;',
  26609. '$mod.p = $mod.C;',
  26610. '$mod.p = $mod.a;',
  26611. '$mod.p = $mod.TObject;',
  26612. '$mod.Obj = $mod.p;',
  26613. '$mod.C = $mod.p;',
  26614. '$mod.a = $mod.p;',
  26615. '$mod.p = null;',
  26616. '$mod.p = $mod.a;',
  26617. '$mod.p = $mod.s;',
  26618. '$mod.s = $mod.p;',
  26619. '']));
  26620. end;
  26621. procedure TTestModule.TestPointer_Proc;
  26622. begin
  26623. StartProgram(false);
  26624. Add('type');
  26625. Add(' TObject = class');
  26626. Add(' procedure DoIt; virtual; abstract;');
  26627. Add(' end;');
  26628. Add('procedure DoSome; begin end;');
  26629. Add('var');
  26630. Add(' o: TObject;');
  26631. Add(' p: Pointer;');
  26632. Add('begin');
  26633. Add(' p:=@DoSome;');
  26634. Add(' p:[email protected];');
  26635. ConvertProgram;
  26636. CheckSource('TestPointer_Proc',
  26637. LinesToStr([ // statements
  26638. 'rtl.createClass(this, "TObject", null, function () {',
  26639. ' this.$init = function () {',
  26640. ' };',
  26641. ' this.$final = function () {',
  26642. ' };',
  26643. '});',
  26644. 'this.DoSome = function () {',
  26645. '};',
  26646. 'this.o = null;',
  26647. 'this.p = null;',
  26648. '']),
  26649. LinesToStr([ // $mod.$main
  26650. '$mod.p = $mod.DoSome;',
  26651. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26652. '']));
  26653. end;
  26654. procedure TTestModule.TestPointer_AssignRecordFail;
  26655. begin
  26656. StartProgram(false);
  26657. Add('type');
  26658. Add(' TRec = record end;');
  26659. Add('var');
  26660. Add(' p: Pointer;');
  26661. Add(' r: TRec;');
  26662. Add('begin');
  26663. Add(' p:=r;');
  26664. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26665. nIncompatibleTypesGotExpected);
  26666. ConvertProgram;
  26667. end;
  26668. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26669. begin
  26670. StartProgram(false);
  26671. Add('type');
  26672. Add(' TArr = array[boolean] of longint;');
  26673. Add('var');
  26674. Add(' p: Pointer;');
  26675. Add(' a: TArr;');
  26676. Add('begin');
  26677. Add(' p:=a;');
  26678. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26679. nIncompatibleTypesGotExpected);
  26680. ConvertProgram;
  26681. end;
  26682. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26683. begin
  26684. StartProgram(false);
  26685. Add([
  26686. 'procedure DoIt(args: array of jsvalue); begin end;',
  26687. 'procedure DoAll; varargs; begin end;',
  26688. 'var',
  26689. ' v: jsvalue;',
  26690. 'begin',
  26691. ' DoIt([pointer(v)]);',
  26692. ' DoAll(pointer(v));',
  26693. '']);
  26694. ConvertProgram;
  26695. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26696. LinesToStr([ // statements
  26697. 'this.DoIt = function (args) {',
  26698. '};',
  26699. 'this.DoAll = function () {',
  26700. '};',
  26701. 'this.v = undefined;',
  26702. '']),
  26703. LinesToStr([ // $mod.$main
  26704. '$mod.DoIt([$mod.v]);',
  26705. '$mod.DoAll($mod.v);',
  26706. '']));
  26707. end;
  26708. procedure TTestModule.TestPointer_NonRecordFail;
  26709. begin
  26710. StartProgram(false);
  26711. Add([
  26712. 'type',
  26713. ' p = ^longint;',
  26714. 'begin',
  26715. '']);
  26716. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26717. ConvertProgram;
  26718. end;
  26719. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26720. begin
  26721. StartProgram(false);
  26722. Add([
  26723. 'procedure DoIt(p: ^longint); begin end;',
  26724. 'begin',
  26725. '']);
  26726. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26727. ConvertProgram;
  26728. end;
  26729. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26730. begin
  26731. StartProgram(false);
  26732. Add([
  26733. 'var p: ^longint;',
  26734. 'begin',
  26735. '']);
  26736. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26737. ConvertProgram;
  26738. end;
  26739. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26740. begin
  26741. StartProgram(false);
  26742. Add([
  26743. 'function DoIt: ^longint; begin end;',
  26744. 'begin',
  26745. '']);
  26746. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26747. ConvertProgram;
  26748. end;
  26749. procedure TTestModule.TestPointer_AddrOperatorFail;
  26750. begin
  26751. StartProgram(false);
  26752. Add([
  26753. 'var i: longint;',
  26754. 'begin',
  26755. ' if @i=nil then ;',
  26756. '']);
  26757. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26758. ConvertProgram;
  26759. end;
  26760. procedure TTestModule.TestPointer_ArrayParamsFail;
  26761. begin
  26762. StartProgram(false);
  26763. Add([
  26764. 'var',
  26765. ' p: Pointer;',
  26766. 'begin',
  26767. ' p:=p[1];',
  26768. '']);
  26769. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26770. ConvertProgram;
  26771. end;
  26772. procedure TTestModule.TestPointer_PointerAddFail;
  26773. begin
  26774. StartProgram(false);
  26775. Add([
  26776. 'var',
  26777. ' p: Pointer;',
  26778. 'begin',
  26779. ' p:=p+1;',
  26780. '']);
  26781. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26782. ConvertProgram;
  26783. end;
  26784. procedure TTestModule.TestPointer_IncPointerFail;
  26785. begin
  26786. StartProgram(false);
  26787. Add([
  26788. 'var',
  26789. ' p: Pointer;',
  26790. 'begin',
  26791. ' inc(p,1);',
  26792. '']);
  26793. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26794. nIncompatibleTypeArgNo);
  26795. ConvertProgram;
  26796. end;
  26797. procedure TTestModule.TestPointer_Record;
  26798. begin
  26799. StartProgram(false);
  26800. Add([
  26801. 'type',
  26802. ' TRec = record x: longint; end;',
  26803. ' PRec = ^TRec;',
  26804. 'var',
  26805. ' r: TRec;',
  26806. ' p: PRec;',
  26807. ' q: ^TRec;',
  26808. ' Ptr: pointer;',
  26809. 'begin',
  26810. ' new(p);',
  26811. ' p:=@r;',
  26812. ' r:=p^;',
  26813. ' r.x:=p^.x;',
  26814. ' p^.x:=r.x;',
  26815. ' if p^.x=3 then ;',
  26816. ' if 4=p^.x then ;',
  26817. ' dispose(p);',
  26818. ' new(q);',
  26819. ' dispose(q);',
  26820. ' Ptr:=p;',
  26821. ' p:=PRec(ptr);',
  26822. '']);
  26823. ConvertProgram;
  26824. CheckSource('TestPointer_Record',
  26825. LinesToStr([ // statements
  26826. 'rtl.recNewT(this, "TRec", function () {',
  26827. ' this.x = 0;',
  26828. ' this.$eq = function (b) {',
  26829. ' return this.x === b.x;',
  26830. ' };',
  26831. ' this.$assign = function (s) {',
  26832. ' this.x = s.x;',
  26833. ' return this;',
  26834. ' };',
  26835. '});',
  26836. 'this.r = this.TRec.$new();',
  26837. 'this.p = null;',
  26838. 'this.q = null;',
  26839. 'this.Ptr = null;',
  26840. '']),
  26841. LinesToStr([ // $mod.$main
  26842. '$mod.p = $mod.TRec.$new();',
  26843. '$mod.p = $mod.r;',
  26844. '$mod.r.$assign($mod.p);',
  26845. '$mod.r.x = $mod.p.x;',
  26846. '$mod.p.x = $mod.r.x;',
  26847. 'if ($mod.p.x === 3) ;',
  26848. 'if (4 === $mod.p.x) ;',
  26849. '$mod.p = null;',
  26850. '$mod.q = $mod.TRec.$new();',
  26851. '$mod.q = null;',
  26852. '$mod.Ptr = $mod.p;',
  26853. '$mod.p = $mod.Ptr;',
  26854. '']));
  26855. end;
  26856. procedure TTestModule.TestPointer_RecordArg;
  26857. begin
  26858. StartProgram(false);
  26859. Add([
  26860. '{$modeswitch autoderef}',
  26861. 'type',
  26862. ' TRec = record x: longint; end;',
  26863. ' PRec = ^TRec;',
  26864. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26865. 'begin',
  26866. ' a.x:=a.x;',
  26867. ' a^.x:=a^.x;',
  26868. ' with a^ do',
  26869. ' x:=x;',
  26870. 'end;',
  26871. 'function GetIt(p: PRec): PRec;',
  26872. 'begin',
  26873. ' p.x:=p.x;',
  26874. ' p^.x:=p^.x;',
  26875. ' with p^ do',
  26876. ' x:=x;',
  26877. 'end;',
  26878. 'var',
  26879. ' r: TRec;',
  26880. ' p: PRec;',
  26881. 'begin',
  26882. ' p:=GetIt(p);',
  26883. ' p^:=GetIt(@r)^;',
  26884. ' DoIt(p,p,p);',
  26885. ' DoIt(@r,p,p);',
  26886. '']);
  26887. ConvertProgram;
  26888. CheckSource('TestPointer_RecordArg',
  26889. LinesToStr([ // statements
  26890. 'rtl.recNewT(this, "TRec", function () {',
  26891. ' this.x = 0;',
  26892. ' this.$eq = function (b) {',
  26893. ' return this.x === b.x;',
  26894. ' };',
  26895. ' this.$assign = function (s) {',
  26896. ' this.x = s.x;',
  26897. ' return this;',
  26898. ' };',
  26899. '});',
  26900. 'this.DoIt = function (a, b, c) {',
  26901. ' var Result = $mod.TRec.$new();',
  26902. ' a.x = a.x;',
  26903. ' a.x = a.x;',
  26904. ' a.x = a.x;',
  26905. ' return Result;',
  26906. '};',
  26907. 'this.GetIt = function (p) {',
  26908. ' var Result = null;',
  26909. ' p.x = p.x;',
  26910. ' p.x = p.x;',
  26911. ' p.x = p.x;',
  26912. ' return Result;',
  26913. '};',
  26914. 'this.r = this.TRec.$new();',
  26915. 'this.p = null;',
  26916. '']),
  26917. LinesToStr([ // $mod.$main
  26918. '$mod.p = $mod.GetIt($mod.p);',
  26919. '$mod.p.$assign($mod.GetIt($mod.r));',
  26920. '$mod.DoIt($mod.p, {',
  26921. ' p: $mod,',
  26922. ' get: function () {',
  26923. ' return this.p.p;',
  26924. ' },',
  26925. ' set: function (v) {',
  26926. ' this.p.p = v;',
  26927. ' }',
  26928. '}, {',
  26929. ' p: $mod,',
  26930. ' get: function () {',
  26931. ' return this.p.p;',
  26932. ' },',
  26933. ' set: function (v) {',
  26934. ' this.p.p = v;',
  26935. ' }',
  26936. '});',
  26937. '$mod.DoIt($mod.r, {',
  26938. ' p: $mod,',
  26939. ' get: function () {',
  26940. ' return this.p.p;',
  26941. ' },',
  26942. ' set: function (v) {',
  26943. ' this.p.p = v;',
  26944. ' }',
  26945. '}, {',
  26946. ' p: $mod,',
  26947. ' get: function () {',
  26948. ' return this.p.p;',
  26949. ' },',
  26950. ' set: function (v) {',
  26951. ' this.p.p = v;',
  26952. ' }',
  26953. '});',
  26954. '']));
  26955. end;
  26956. procedure TTestModule.TestJSValue_AssignToJSValue;
  26957. begin
  26958. StartProgram(false);
  26959. Add('var');
  26960. Add(' v: jsvalue;');
  26961. Add(' i: longint;');
  26962. Add(' s: string;');
  26963. Add(' b: boolean;');
  26964. Add(' d: double;');
  26965. Add(' p: pointer;');
  26966. Add('begin');
  26967. Add(' v:=v;');
  26968. Add(' v:=1;');
  26969. Add(' v:=i;');
  26970. Add(' v:='''';');
  26971. Add(' v:=''c'';');
  26972. Add(' v:=''foo'';');
  26973. Add(' v:=s;');
  26974. Add(' v:=false;');
  26975. Add(' v:=true;');
  26976. Add(' v:=b;');
  26977. Add(' v:=0.1;');
  26978. Add(' v:=d;');
  26979. Add(' v:=nil;');
  26980. Add(' v:=p;');
  26981. ConvertProgram;
  26982. CheckSource('TestJSValue_AssignToJSValue',
  26983. LinesToStr([ // statements
  26984. 'this.v = undefined;',
  26985. 'this.i = 0;',
  26986. 'this.s = "";',
  26987. 'this.b = false;',
  26988. 'this.d = 0.0;',
  26989. 'this.p = null;',
  26990. '']),
  26991. LinesToStr([ // $mod.$main
  26992. '$mod.v = $mod.v;',
  26993. '$mod.v = 1;',
  26994. '$mod.v = $mod.i;',
  26995. '$mod.v = "";',
  26996. '$mod.v = "c";',
  26997. '$mod.v = "foo";',
  26998. '$mod.v = $mod.s;',
  26999. '$mod.v = false;',
  27000. '$mod.v = true;',
  27001. '$mod.v = $mod.b;',
  27002. '$mod.v = 0.1;',
  27003. '$mod.v = $mod.d;',
  27004. '$mod.v = null;',
  27005. '$mod.v = $mod.p;',
  27006. '']));
  27007. end;
  27008. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27009. begin
  27010. StartProgram(false);
  27011. Add('type');
  27012. Add(' integer = longint;');
  27013. Add(' TYesNo = boolean;');
  27014. Add(' TFloat = double;');
  27015. Add(' TCaption = string;');
  27016. Add(' TChar = char;');
  27017. Add('var');
  27018. Add(' v: jsvalue;');
  27019. Add(' i: integer;');
  27020. Add(' s: TCaption;');
  27021. Add(' b: TYesNo;');
  27022. Add(' d: TFloat;');
  27023. Add(' c: char;');
  27024. Add('begin');
  27025. Add(' i:=longint(v);');
  27026. Add(' i:=integer(v);');
  27027. Add(' s:=string(v);');
  27028. Add(' s:=TCaption(v);');
  27029. Add(' b:=boolean(v);');
  27030. Add(' b:=TYesNo(v);');
  27031. Add(' d:=double(v);');
  27032. Add(' d:=TFloat(v);');
  27033. Add(' c:=char(v);');
  27034. Add(' c:=TChar(v);');
  27035. ConvertProgram;
  27036. CheckSource('TestJSValue_TypeCastToBaseType',
  27037. LinesToStr([ // statements
  27038. 'this.v = undefined;',
  27039. 'this.i = 0;',
  27040. 'this.s = "";',
  27041. 'this.b = false;',
  27042. 'this.d = 0.0;',
  27043. 'this.c = "";',
  27044. '']),
  27045. LinesToStr([ // $mod.$main
  27046. '$mod.i = rtl.trunc($mod.v);',
  27047. '$mod.i = rtl.trunc($mod.v);',
  27048. '$mod.s = "" + $mod.v;',
  27049. '$mod.s = "" + $mod.v;',
  27050. '$mod.b = !($mod.v == false);',
  27051. '$mod.b = !($mod.v == false);',
  27052. '$mod.d = rtl.getNumber($mod.v);',
  27053. '$mod.d = rtl.getNumber($mod.v);',
  27054. '$mod.c = rtl.getChar($mod.v);',
  27055. '$mod.c = rtl.getChar($mod.v);',
  27056. '']));
  27057. end;
  27058. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27059. begin
  27060. StartProgram(false);
  27061. Add([
  27062. 'type',
  27063. ' TArr = array of word;',
  27064. ' TRec = record end;',
  27065. ' TSet = set of boolean;',
  27066. 'procedure Fly(v: jsvalue);',
  27067. 'begin',
  27068. 'end;',
  27069. 'var',
  27070. ' a: TArr;',
  27071. ' r: TRec;',
  27072. ' s: TSet;',
  27073. 'begin',
  27074. ' Fly(jsvalue(a));',
  27075. ' Fly(jsvalue(r));',
  27076. ' Fly(jsvalue(s));',
  27077. '']);
  27078. ConvertProgram;
  27079. CheckSource('TestJSValue_TypecastToJSValue',
  27080. LinesToStr([ // statements
  27081. 'rtl.recNewT(this, "TRec", function () {',
  27082. ' this.$eq = function (b) {',
  27083. ' return true;',
  27084. ' };',
  27085. ' this.$assign = function (s) {',
  27086. ' return this;',
  27087. ' };',
  27088. '});',
  27089. 'this.Fly = function (v) {',
  27090. '};',
  27091. 'this.a = [];',
  27092. 'this.r = this.TRec.$new();',
  27093. 'this.s = {};',
  27094. '']),
  27095. LinesToStr([ // $mod.$main
  27096. '$mod.Fly($mod.a);',
  27097. '$mod.Fly($mod.r);',
  27098. '$mod.Fly($mod.s);',
  27099. '']));
  27100. end;
  27101. procedure TTestModule.TestJSValue_Equal;
  27102. begin
  27103. StartProgram(false);
  27104. Add('type');
  27105. Add(' integer = longint;');
  27106. Add(' TYesNo = boolean;');
  27107. Add(' TFloat = double;');
  27108. Add(' TCaption = string;');
  27109. Add(' TChar = char;');
  27110. Add(' TMulti = JSValue;');
  27111. Add('var');
  27112. Add(' v: jsvalue;');
  27113. Add(' i: integer;');
  27114. Add(' s: TCaption;');
  27115. Add(' b: TYesNo;');
  27116. Add(' d: TFloat;');
  27117. Add(' c: char;');
  27118. Add(' m: TMulti;');
  27119. Add('begin');
  27120. Add(' b:=v=v;');
  27121. Add(' b:=v<>v;');
  27122. Add(' b:=v=1;');
  27123. Add(' b:=v<>1;');
  27124. Add(' b:=2=v;');
  27125. Add(' b:=2<>v;');
  27126. Add(' b:=v=i;');
  27127. Add(' b:=i=v;');
  27128. Add(' b:=v=nil;');
  27129. Add(' b:=nil=v;');
  27130. Add(' b:=v=false;');
  27131. Add(' b:=true=v;');
  27132. Add(' b:=v=b;');
  27133. Add(' b:=b=v;');
  27134. Add(' b:=v=s;');
  27135. Add(' b:=s=v;');
  27136. Add(' b:=v=''foo'';');
  27137. Add(' b:=''''=v;');
  27138. Add(' b:=v=d;');
  27139. Add(' b:=d=v;');
  27140. Add(' b:=v=3.4;');
  27141. Add(' b:=5.6=v;');
  27142. Add(' b:=v=c;');
  27143. Add(' b:=c=v;');
  27144. Add(' b:=m=m;');
  27145. Add(' b:=v=m;');
  27146. Add(' b:=m=v;');
  27147. ConvertProgram;
  27148. CheckSource('TestJSValue_Equal',
  27149. LinesToStr([ // statements
  27150. 'this.v = undefined;',
  27151. 'this.i = 0;',
  27152. 'this.s = "";',
  27153. 'this.b = false;',
  27154. 'this.d = 0.0;',
  27155. 'this.c = "";',
  27156. 'this.m = undefined;',
  27157. '']),
  27158. LinesToStr([ // $mod.$main
  27159. '$mod.b = $mod.v == $mod.v;',
  27160. '$mod.b = $mod.v != $mod.v;',
  27161. '$mod.b = $mod.v == 1;',
  27162. '$mod.b = $mod.v != 1;',
  27163. '$mod.b = 2 == $mod.v;',
  27164. '$mod.b = 2 != $mod.v;',
  27165. '$mod.b = $mod.v == $mod.i;',
  27166. '$mod.b = $mod.i == $mod.v;',
  27167. '$mod.b = $mod.v == null;',
  27168. '$mod.b = null == $mod.v;',
  27169. '$mod.b = $mod.v == false;',
  27170. '$mod.b = true == $mod.v;',
  27171. '$mod.b = $mod.v == $mod.b;',
  27172. '$mod.b = $mod.b == $mod.v;',
  27173. '$mod.b = $mod.v == $mod.s;',
  27174. '$mod.b = $mod.s == $mod.v;',
  27175. '$mod.b = $mod.v == "foo";',
  27176. '$mod.b = "" == $mod.v;',
  27177. '$mod.b = $mod.v == $mod.d;',
  27178. '$mod.b = $mod.d == $mod.v;',
  27179. '$mod.b = $mod.v == 3.4;',
  27180. '$mod.b = 5.6 == $mod.v;',
  27181. '$mod.b = $mod.v == $mod.c;',
  27182. '$mod.b = $mod.c == $mod.v;',
  27183. '$mod.b = $mod.m == $mod.m;',
  27184. '$mod.b = $mod.v == $mod.m;',
  27185. '$mod.b = $mod.m == $mod.v;',
  27186. '']));
  27187. end;
  27188. procedure TTestModule.TestJSValue_If;
  27189. begin
  27190. StartProgram(false);
  27191. Add([
  27192. 'procedure Fly(var u);',
  27193. 'begin',
  27194. ' if jsvalue(u) then ;',
  27195. 'end;',
  27196. 'var',
  27197. ' v: jsvalue;',
  27198. 'begin',
  27199. ' if v then ;',
  27200. ' while v do ;',
  27201. ' repeat until v;',
  27202. '']);
  27203. ConvertProgram;
  27204. CheckSource('TestJSValue_If',
  27205. LinesToStr([ // statements
  27206. 'this.Fly = function (u) {',
  27207. ' if (u.get()) ;',
  27208. '};',
  27209. 'this.v = undefined;',
  27210. '']),
  27211. LinesToStr([ // $mod.$main
  27212. 'if ($mod.v) ;',
  27213. 'while($mod.v){',
  27214. '};',
  27215. 'do{',
  27216. '} while(!$mod.v);',
  27217. '']));
  27218. end;
  27219. procedure TTestModule.TestJSValue_Not;
  27220. begin
  27221. StartProgram(false);
  27222. Add([
  27223. 'var',
  27224. ' v: jsvalue;',
  27225. ' b: boolean;',
  27226. 'begin',
  27227. ' b:=not v;',
  27228. ' if not v then ;',
  27229. ' while not v do ;',
  27230. ' repeat until not v;',
  27231. '']);
  27232. ConvertProgram;
  27233. CheckSource('TestJSValue_If',
  27234. LinesToStr([ // statements
  27235. 'this.v = undefined;',
  27236. 'this.b = false;',
  27237. '']),
  27238. LinesToStr([ // $mod.$main
  27239. '$mod.b=!$mod.v;',
  27240. 'if (!$mod.v) ;',
  27241. 'while(!$mod.v){',
  27242. '};',
  27243. 'do{',
  27244. '} while($mod.v);',
  27245. '']));
  27246. end;
  27247. procedure TTestModule.TestJSValue_Enum;
  27248. begin
  27249. StartProgram(false);
  27250. Add('type');
  27251. Add(' TColor = (red, blue);');
  27252. Add(' TRedBlue = TColor;');
  27253. Add('var');
  27254. Add(' v: jsvalue;');
  27255. Add(' e: TColor;');
  27256. Add('begin');
  27257. Add(' v:=e;');
  27258. Add(' v:=TColor(e);');
  27259. Add(' v:=TRedBlue(e);');
  27260. Add(' e:=TColor(v);');
  27261. Add(' e:=TRedBlue(v);');
  27262. ConvertProgram;
  27263. CheckSource('TestJSValue_Enum',
  27264. LinesToStr([ // statements
  27265. 'this.TColor = {',
  27266. ' "0": "red",',
  27267. ' red: 0,',
  27268. ' "1": "blue",',
  27269. ' blue: 1',
  27270. '};',
  27271. 'this.v = undefined;',
  27272. 'this.e = 0;',
  27273. '']),
  27274. LinesToStr([ // $mod.$main
  27275. '$mod.v = $mod.e;',
  27276. '$mod.v = $mod.e;',
  27277. '$mod.v = $mod.e;',
  27278. '$mod.e = $mod.v;',
  27279. '$mod.e = $mod.v;',
  27280. '']));
  27281. end;
  27282. procedure TTestModule.TestJSValue_ClassInstance;
  27283. begin
  27284. StartProgram(false);
  27285. Add([
  27286. 'type',
  27287. ' TObject = class',
  27288. ' end;',
  27289. ' TBirdObject = TObject;',
  27290. 'var',
  27291. ' v: jsvalue;',
  27292. ' o: TObject;',
  27293. 'begin',
  27294. ' v:=o;',
  27295. ' v:=TObject(o);',
  27296. ' v:=TBirdObject(o);',
  27297. ' o:=TObject(v);',
  27298. ' o:=TBirdObject(v);',
  27299. ' if v is TObject then ;',
  27300. '']);
  27301. ConvertProgram;
  27302. CheckSource('TestJSValue_ClassInstance',
  27303. LinesToStr([ // statements
  27304. 'rtl.createClass(this, "TObject", null, function () {',
  27305. ' this.$init = function () {',
  27306. ' };',
  27307. ' this.$final = function () {',
  27308. ' };',
  27309. '});',
  27310. 'this.v = undefined;',
  27311. 'this.o = null;',
  27312. '']),
  27313. LinesToStr([ // $mod.$main
  27314. '$mod.v = $mod.o;',
  27315. '$mod.v = $mod.o;',
  27316. '$mod.v = $mod.o;',
  27317. '$mod.o = rtl.getObject($mod.v);',
  27318. '$mod.o = rtl.getObject($mod.v);',
  27319. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27320. '']));
  27321. end;
  27322. procedure TTestModule.TestJSValue_ClassOf;
  27323. begin
  27324. StartProgram(false);
  27325. Add([
  27326. 'type',
  27327. ' TClass = class of TObject;',
  27328. ' TObject = class',
  27329. ' end;',
  27330. ' TBirds = class of TBird;',
  27331. ' TBird = class(TObject) end;',
  27332. 'var',
  27333. ' v: jsvalue;',
  27334. ' c: TClass;',
  27335. 'begin',
  27336. ' v:=c;',
  27337. ' v:=TObject;',
  27338. ' v:=TClass(c);',
  27339. ' v:=TBirds(c);',
  27340. ' c:=TClass(v);',
  27341. ' c:=TBirds(v);',
  27342. ' if v is TClass then ;',
  27343. '']);
  27344. ConvertProgram;
  27345. CheckSource('TestJSValue_ClassOf',
  27346. LinesToStr([ // statements
  27347. 'rtl.createClass(this, "TObject", null, function () {',
  27348. ' this.$init = function () {',
  27349. ' };',
  27350. ' this.$final = function () {',
  27351. ' };',
  27352. '});',
  27353. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27354. '});',
  27355. 'this.v = undefined;',
  27356. 'this.c = null;',
  27357. '']),
  27358. LinesToStr([ // $mod.$main
  27359. '$mod.v = $mod.c;',
  27360. '$mod.v = $mod.TObject;',
  27361. '$mod.v = $mod.c;',
  27362. '$mod.v = $mod.c;',
  27363. '$mod.c = rtl.getObject($mod.v);',
  27364. '$mod.c = rtl.getObject($mod.v);',
  27365. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27366. '']));
  27367. end;
  27368. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27369. begin
  27370. StartProgram(false);
  27371. Add([
  27372. 'type',
  27373. ' integer = longint;',
  27374. ' TArray = array of JSValue;',
  27375. ' TArrgh = tarray;',
  27376. ' TArrInt = array of integer;',
  27377. 'var',
  27378. ' v: jsvalue;',
  27379. ' TheArray: tarray = (1,''2'');',
  27380. ' Arr: tarrgh;',
  27381. ' i: integer;',
  27382. ' ArrInt: tarrint;',
  27383. 'begin',
  27384. ' arr:=thearray;',
  27385. ' thearray:=arr;',
  27386. ' setlength(arr,2);',
  27387. ' setlength(thearray,3);',
  27388. ' arr[4]:=v;',
  27389. ' arr[5]:=length(thearray);',
  27390. ' arr[6]:=nil;',
  27391. ' arr[7]:=thearray[8];',
  27392. ' arr[low(arr)]:=high(thearray);',
  27393. ' arr:=arrint;',
  27394. ' arrInt:=tarrint(arr);',
  27395. ' if TheArray = nil then ;',
  27396. ' if nil = TheArray then ;',
  27397. ' if TheArray <> nil then ;',
  27398. ' if nil <> TheArray then ;',
  27399. '']);
  27400. ConvertProgram;
  27401. CheckSource('TestJSValue_ArrayOfJSValue',
  27402. LinesToStr([ // statements
  27403. 'this.v = undefined;',
  27404. 'this.TheArray = [1, "2"];',
  27405. 'this.Arr = [];',
  27406. 'this.i = 0;',
  27407. 'this.ArrInt = [];',
  27408. '']),
  27409. LinesToStr([ // $mod.$main
  27410. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27411. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27412. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27413. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27414. '$mod.Arr[4] = $mod.v;',
  27415. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27416. '$mod.Arr[6] = null;',
  27417. '$mod.Arr[7] = $mod.TheArray[8];',
  27418. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27419. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27420. '$mod.ArrInt = $mod.Arr;',
  27421. 'if (rtl.length($mod.TheArray) === 0) ;',
  27422. 'if (rtl.length($mod.TheArray) === 0) ;',
  27423. 'if (rtl.length($mod.TheArray) > 0) ;',
  27424. 'if (rtl.length($mod.TheArray) > 0) ;',
  27425. '']));
  27426. end;
  27427. procedure TTestModule.TestJSValue_ArrayLit;
  27428. begin
  27429. StartProgram(false);
  27430. Add([
  27431. 'type',
  27432. ' TFlag = (big,small);',
  27433. ' TArray = array of JSValue;',
  27434. ' TObject = class end;',
  27435. ' TClass = class of TObject;',
  27436. 'var',
  27437. ' v: jsvalue;',
  27438. ' a: TArray;',
  27439. ' o: TObject;',
  27440. 'begin',
  27441. ' a:=[];',
  27442. ' a:=[1];',
  27443. ' a:=[1,2];',
  27444. ' a:=[big];',
  27445. ' a:=[1,big];',
  27446. ' a:=[o,nil];',
  27447. '']);
  27448. ConvertProgram;
  27449. CheckSource('TestJSValue_ArrayLit',
  27450. LinesToStr([ // statements
  27451. 'this.TFlag = {',
  27452. ' "0": "big",',
  27453. ' big: 0,',
  27454. ' "1": "small",',
  27455. ' small: 1',
  27456. '};',
  27457. 'rtl.createClass(this, "TObject", null, function () {',
  27458. ' this.$init = function () {',
  27459. ' };',
  27460. ' this.$final = function () {',
  27461. ' };',
  27462. '});',
  27463. 'this.v = undefined;',
  27464. 'this.a = [];',
  27465. 'this.o = null;',
  27466. '']),
  27467. LinesToStr([ // $mod.$main
  27468. '$mod.a = [];',
  27469. '$mod.a = [1];',
  27470. '$mod.a = [1, 2];',
  27471. '$mod.a = [$mod.TFlag.big];',
  27472. '$mod.a = [1, $mod.TFlag.big];',
  27473. '$mod.a = [$mod.o, null];',
  27474. '']));
  27475. end;
  27476. procedure TTestModule.TestJSValue_Params;
  27477. begin
  27478. StartProgram(false);
  27479. Add('type');
  27480. Add(' integer = longint;');
  27481. Add(' TYesNo = boolean;');
  27482. Add(' TFloat = double;');
  27483. Add(' TCaption = string;');
  27484. Add(' TChar = char;');
  27485. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27486. Add('var');
  27487. Add(' l: jsvalue;');
  27488. Add('begin');
  27489. Add(' a:=a;');
  27490. Add(' l:=b;');
  27491. Add(' c:=c;');
  27492. Add(' d:=d;');
  27493. Add(' Result:=l;');
  27494. Add('end;');
  27495. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27496. Add('var');
  27497. Add(' v: jsvalue;');
  27498. Add(' i: integer;');
  27499. Add(' b: TYesNo;');
  27500. Add(' d: TFloat;');
  27501. Add(' s: TCaption;');
  27502. Add(' c: TChar;');
  27503. Add('begin');
  27504. Add(' v:=doit(v,v,v,v);');
  27505. Add(' i:=integer(dosome(i,i));');
  27506. Add(' b:=TYesNo(dosome(b,b));');
  27507. Add(' d:=TFloat(dosome(d,d));');
  27508. Add(' s:=TCaption(dosome(s,s));');
  27509. Add(' c:=TChar(dosome(c,c));');
  27510. ConvertProgram;
  27511. CheckSource('TestJSValue_Params',
  27512. LinesToStr([ // statements
  27513. 'this.DoIt = function (a, b, c, d) {',
  27514. ' var Result = undefined;',
  27515. ' var l = undefined;',
  27516. ' a = a;',
  27517. ' l = b;',
  27518. ' c.set(c.get());',
  27519. ' d.set(d.get());',
  27520. ' Result = l;',
  27521. ' return Result;',
  27522. '};',
  27523. 'this.DoSome = function (a, b) {',
  27524. ' var Result = undefined;',
  27525. ' return Result;',
  27526. '};',
  27527. 'this.v = undefined;',
  27528. 'this.i = 0;',
  27529. 'this.b = false;',
  27530. 'this.d = 0.0;',
  27531. 'this.s = "";',
  27532. 'this.c = "";',
  27533. '']),
  27534. LinesToStr([ // $mod.$main
  27535. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27536. ' p: $mod,',
  27537. ' get: function () {',
  27538. ' return this.p.v;',
  27539. ' },',
  27540. ' set: function (v) {',
  27541. ' this.p.v = v;',
  27542. ' }',
  27543. '}, {',
  27544. ' p: $mod,',
  27545. ' get: function () {',
  27546. ' return this.p.v;',
  27547. ' },',
  27548. ' set: function (v) {',
  27549. ' this.p.v = v;',
  27550. ' }',
  27551. '});',
  27552. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27553. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27554. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27555. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27556. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27557. '']));
  27558. end;
  27559. procedure TTestModule.TestJSValue_UntypedParam;
  27560. begin
  27561. StartProgram(false);
  27562. Add('function DoIt(const a; var b; out c): jsvalue;');
  27563. Add('begin');
  27564. Add(' Result:=a;');
  27565. Add(' Result:=b;');
  27566. Add(' Result:=c;');
  27567. Add(' b:=Result;');
  27568. Add(' c:=Result;');
  27569. Add('end;');
  27570. Add('var i: longint;');
  27571. Add('begin');
  27572. Add(' doit(i,i,i);');
  27573. ConvertProgram;
  27574. CheckSource('TestJSValue_UntypedParam',
  27575. LinesToStr([ // statements
  27576. 'this.DoIt = function (a, b, c) {',
  27577. ' var Result = undefined;',
  27578. ' Result = a;',
  27579. ' Result = b.get();',
  27580. ' Result = c.get();',
  27581. ' b.set(Result);',
  27582. ' c.set(Result);',
  27583. ' return Result;',
  27584. '};',
  27585. 'this.i = 0;',
  27586. '']),
  27587. LinesToStr([ // $mod.$main
  27588. '$mod.DoIt($mod.i, {',
  27589. ' p: $mod,',
  27590. ' get: function () {',
  27591. ' return this.p.i;',
  27592. ' },',
  27593. ' set: function (v) {',
  27594. ' this.p.i = v;',
  27595. ' }',
  27596. '}, {',
  27597. ' p: $mod,',
  27598. ' get: function () {',
  27599. ' return this.p.i;',
  27600. ' },',
  27601. ' set: function (v) {',
  27602. ' this.p.i = v;',
  27603. ' }',
  27604. '});',
  27605. '']));
  27606. end;
  27607. procedure TTestModule.TestJSValue_FuncResultType;
  27608. begin
  27609. StartProgram(false);
  27610. Add('type');
  27611. Add(' integer = longint;');
  27612. Add(' TJSValueArray = array of JSValue;');
  27613. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27614. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27615. Add('begin');
  27616. Add(' while Compare(P,aList[0])>0 do ;');
  27617. Add('end;');
  27618. Add('var');
  27619. Add(' Compare: TListSortCompare;');
  27620. Add(' V: JSValue;');
  27621. Add(' i: integer;');
  27622. Add('begin');
  27623. Add(' if Compare(V,V)>0 then ;');
  27624. Add(' if Compare(i,i)>1 then ;');
  27625. Add(' if Compare(nil,false)>2 then ;');
  27626. Add(' if Compare(1,true)>3 then ;');
  27627. ConvertProgram;
  27628. CheckSource('TestJSValue_UntypedParam',
  27629. LinesToStr([ // statements
  27630. 'this.Sort = function (P, aList, Compare) {',
  27631. ' while (Compare(P, aList[0]) > 0) {',
  27632. ' };',
  27633. '};',
  27634. 'this.Compare = null;',
  27635. 'this.V = undefined;',
  27636. 'this.i = 0;',
  27637. '']),
  27638. LinesToStr([ // $mod.$main
  27639. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27640. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27641. 'if ($mod.Compare(null, false) > 2) ;',
  27642. 'if ($mod.Compare(1, true) > 3) ;',
  27643. '']));
  27644. end;
  27645. procedure TTestModule.TestJSValue_ProcType_Assign;
  27646. begin
  27647. StartProgram(false);
  27648. Add('type');
  27649. Add(' integer = longint;');
  27650. Add(' TObject = class');
  27651. Add(' class function GetGlob: integer;');
  27652. Add(' function Getter: integer;');
  27653. Add(' end;');
  27654. Add('class function TObject.GetGlob: integer;');
  27655. Add('var v1: jsvalue;');
  27656. Add('begin');
  27657. Add(' v1:=@GetGlob;');
  27658. Add(' v1:[email protected];');
  27659. Add('end;');
  27660. Add('function TObject.Getter: integer;');
  27661. Add('var v2: jsvalue;');
  27662. Add('begin');
  27663. Add(' v2:=@Getter;');
  27664. Add(' v2:[email protected];');
  27665. Add(' v2:=@GetGlob;');
  27666. Add(' v2:[email protected];');
  27667. Add('end;');
  27668. Add('function GetIt(i: integer): integer;');
  27669. Add('var v3: jsvalue;');
  27670. Add('begin');
  27671. Add(' v3:=@GetIt;');
  27672. Add('end;');
  27673. Add('var');
  27674. Add(' V: JSValue;');
  27675. Add(' o: TObject;');
  27676. Add('begin');
  27677. Add(' v:=@GetIt;');
  27678. Add(' v:[email protected];');
  27679. Add(' v:[email protected];');
  27680. ConvertProgram;
  27681. CheckSource('TestJSValue_ProcType_Assign',
  27682. LinesToStr([ // statements
  27683. 'rtl.createClass(this, "TObject", null, function () {',
  27684. ' this.$init = function () {',
  27685. ' };',
  27686. ' this.$final = function () {',
  27687. ' };',
  27688. ' this.GetGlob = function () {',
  27689. ' var Result = 0;',
  27690. ' var v1 = undefined;',
  27691. ' v1 = rtl.createCallback(this, "GetGlob");',
  27692. ' v1 = rtl.createCallback(this, "GetGlob");',
  27693. ' return Result;',
  27694. ' };',
  27695. ' this.Getter = function () {',
  27696. ' var Result = 0;',
  27697. ' var v2 = undefined;',
  27698. ' v2 = rtl.createCallback(this, "Getter");',
  27699. ' v2 = rtl.createCallback(this, "Getter");',
  27700. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27701. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27702. ' return Result;',
  27703. ' };',
  27704. '});',
  27705. 'this.GetIt = function (i) {',
  27706. ' var Result = 0;',
  27707. ' var v3 = undefined;',
  27708. ' v3 = $mod.GetIt;',
  27709. ' return Result;',
  27710. '};',
  27711. 'this.V = undefined;',
  27712. 'this.o = null;',
  27713. '']),
  27714. LinesToStr([ // $mod.$main
  27715. '$mod.V = $mod.GetIt;',
  27716. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27717. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27718. '']));
  27719. end;
  27720. procedure TTestModule.TestJSValue_ProcType_Equal;
  27721. begin
  27722. StartProgram(false);
  27723. Add('type');
  27724. Add(' integer = longint;');
  27725. Add(' TObject = class');
  27726. Add(' class function GetGlob: integer;');
  27727. Add(' function Getter: integer;');
  27728. Add(' end;');
  27729. Add('class function TObject.GetGlob: integer;');
  27730. Add('var v1: jsvalue;');
  27731. Add('begin');
  27732. Add(' if v1=@GetGlob then;');
  27733. Add(' if [email protected] then ;');
  27734. Add('end;');
  27735. Add('function TObject.Getter: integer;');
  27736. Add('var v2: jsvalue;');
  27737. Add('begin');
  27738. Add(' if v2=@Getter then;');
  27739. Add(' if [email protected] then ;');
  27740. Add(' if v2=@GetGlob then;');
  27741. Add(' if [email protected] then;');
  27742. Add('end;');
  27743. Add('function GetIt(i: integer): integer;');
  27744. Add('var v3: jsvalue;');
  27745. Add('begin');
  27746. Add(' if v3=@GetIt then;');
  27747. Add('end;');
  27748. Add('var');
  27749. Add(' V: JSValue;');
  27750. Add(' o: TObject;');
  27751. Add('begin');
  27752. Add(' if v=@GetIt then;');
  27753. Add(' if [email protected] then;');
  27754. Add(' if [email protected] then;');
  27755. Add(' if @GetIt=v then;');
  27756. Add(' if @o.Getter=v then;');
  27757. Add(' if @o.GetGlob=v then;');
  27758. ConvertProgram;
  27759. CheckSource('TestJSValue_ProcType_Equal',
  27760. LinesToStr([ // statements
  27761. 'rtl.createClass(this, "TObject", null, function () {',
  27762. ' this.$init = function () {',
  27763. ' };',
  27764. ' this.$final = function () {',
  27765. ' };',
  27766. ' this.GetGlob = function () {',
  27767. ' var Result = 0;',
  27768. ' var v1 = undefined;',
  27769. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27770. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27771. ' return Result;',
  27772. ' };',
  27773. ' this.Getter = function () {',
  27774. ' var Result = 0;',
  27775. ' var v2 = undefined;',
  27776. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27777. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27778. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27779. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27780. ' return Result;',
  27781. ' };',
  27782. '});',
  27783. 'this.GetIt = function (i) {',
  27784. ' var Result = 0;',
  27785. ' var v3 = undefined;',
  27786. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27787. ' return Result;',
  27788. '};',
  27789. 'this.V = undefined;',
  27790. 'this.o = null;',
  27791. '']),
  27792. LinesToStr([ // $mod.$main
  27793. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27794. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27795. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27796. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27797. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27798. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27799. '']));
  27800. end;
  27801. procedure TTestModule.TestJSValue_ProcType_Param;
  27802. begin
  27803. StartProgram(false);
  27804. Add([
  27805. 'type',
  27806. ' variant = jsvalue;',
  27807. ' TArrVariant = array of variant;',
  27808. ' TArrVar2 = TArrVariant;',
  27809. ' TFuncInt = function: longint;',
  27810. 'function GetIt: longint;',
  27811. 'begin',
  27812. 'end;',
  27813. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27814. 'var v: variant;',
  27815. 'begin',
  27816. ' v:=arr[1];',
  27817. 'end;',
  27818. 'var s: string;',
  27819. 'begin',
  27820. ' DoIt(GetIt,[]);',
  27821. ' DoIt(@GetIt,[]);',
  27822. ' DoIt(1,[s,GetIt]);',
  27823. ' DoIt(1,[s,@GetIt]);',
  27824. '']);
  27825. ConvertProgram;
  27826. CheckSource('TestJSValue_ProcType_Param',
  27827. LinesToStr([ // statements
  27828. 'this.GetIt = function () {',
  27829. ' var Result = 0;',
  27830. ' return Result;',
  27831. '};',
  27832. 'this.DoIt = function (p, Arr) {',
  27833. ' var v = undefined;',
  27834. ' v = Arr[1];',
  27835. '};',
  27836. 'this.s = "";',
  27837. '']),
  27838. LinesToStr([ // $mod.$main
  27839. '$mod.DoIt($mod.GetIt(), []);',
  27840. '$mod.DoIt($mod.GetIt, []);',
  27841. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27842. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27843. '']));
  27844. end;
  27845. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27846. begin
  27847. StartProgram(false);
  27848. Add([
  27849. 'var',
  27850. ' v: JSValue;',
  27851. ' p: Pointer;',
  27852. 'begin',
  27853. ' p:=v;',
  27854. '']);
  27855. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27856. nIncompatibleTypesGotExpected);
  27857. ConvertProgram;
  27858. end;
  27859. procedure TTestModule.TestJSValue_OverloadDouble;
  27860. begin
  27861. StartProgram(false);
  27862. Add([
  27863. 'type',
  27864. ' integer = longint;',
  27865. ' tdatetime = double;',
  27866. 'procedure DoIt(d: double); begin end;',
  27867. 'procedure DoIt(v: jsvalue); begin end;',
  27868. 'var',
  27869. ' d: double;',
  27870. ' dt: tdatetime;',
  27871. ' i: integer;',
  27872. ' b: byte;',
  27873. ' shi: shortint;',
  27874. ' w: word;',
  27875. ' smi: smallint;',
  27876. ' lw: longword;',
  27877. ' li: longint;',
  27878. ' ni: nativeint;',
  27879. ' nu: nativeuint;',
  27880. 'begin',
  27881. ' DoIt(d);',
  27882. ' DoIt(dt);',
  27883. ' DoIt(i);',
  27884. ' DoIt(b);',
  27885. ' DoIt(shi);',
  27886. ' DoIt(w);',
  27887. ' DoIt(smi);',
  27888. ' DoIt(lw);',
  27889. ' DoIt(li);',
  27890. ' DoIt(ni);',
  27891. ' DoIt(nu);',
  27892. '']);
  27893. ConvertProgram;
  27894. CheckSource('TestJSValue_OverloadDouble',
  27895. LinesToStr([ // statements
  27896. 'this.DoIt = function (d) {',
  27897. '};',
  27898. 'this.DoIt$1 = function (v) {',
  27899. '};',
  27900. 'this.d = 0.0;',
  27901. 'this.dt = 0.0;',
  27902. 'this.i = 0;',
  27903. 'this.b = 0;',
  27904. 'this.shi = 0;',
  27905. 'this.w = 0;',
  27906. 'this.smi = 0;',
  27907. 'this.lw = 0;',
  27908. 'this.li = 0;',
  27909. 'this.ni = 0;',
  27910. 'this.nu = 0;',
  27911. '']),
  27912. LinesToStr([ // $mod.$main
  27913. '$mod.DoIt($mod.d);',
  27914. '$mod.DoIt($mod.dt);',
  27915. '$mod.DoIt$1($mod.i);',
  27916. '$mod.DoIt$1($mod.b);',
  27917. '$mod.DoIt$1($mod.shi);',
  27918. '$mod.DoIt$1($mod.w);',
  27919. '$mod.DoIt$1($mod.smi);',
  27920. '$mod.DoIt$1($mod.lw);',
  27921. '$mod.DoIt$1($mod.li);',
  27922. '$mod.DoIt$1($mod.ni);',
  27923. '$mod.DoIt$1($mod.nu);',
  27924. '']));
  27925. end;
  27926. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27927. begin
  27928. StartProgram(false);
  27929. Add([
  27930. 'type',
  27931. ' integer = longint;',
  27932. ' int53 = nativeint;',
  27933. ' tdatetime = double;',
  27934. 'procedure DoIt(n: nativeint); begin end;',
  27935. 'procedure DoIt(v: jsvalue); begin end;',
  27936. 'var',
  27937. ' d: double;',
  27938. ' dt: tdatetime;',
  27939. ' i: integer;',
  27940. ' b: byte;',
  27941. ' shi: shortint;',
  27942. ' w: word;',
  27943. ' smi: smallint;',
  27944. ' lw: longword;',
  27945. ' li: longint;',
  27946. ' ni: nativeint;',
  27947. ' nu: nativeuint;',
  27948. 'begin',
  27949. ' DoIt(d);',
  27950. ' DoIt(dt);',
  27951. ' DoIt(i);',
  27952. ' DoIt(b);',
  27953. ' DoIt(shi);',
  27954. ' DoIt(w);',
  27955. ' DoIt(smi);',
  27956. ' DoIt(lw);',
  27957. ' DoIt(li);',
  27958. ' DoIt(ni);',
  27959. ' DoIt(nu);',
  27960. '']);
  27961. ConvertProgram;
  27962. CheckSource('TestJSValue_OverloadNativeInt',
  27963. LinesToStr([ // statements
  27964. 'this.DoIt = function (n) {',
  27965. '};',
  27966. 'this.DoIt$1 = function (v) {',
  27967. '};',
  27968. 'this.d = 0.0;',
  27969. 'this.dt = 0.0;',
  27970. 'this.i = 0;',
  27971. 'this.b = 0;',
  27972. 'this.shi = 0;',
  27973. 'this.w = 0;',
  27974. 'this.smi = 0;',
  27975. 'this.lw = 0;',
  27976. 'this.li = 0;',
  27977. 'this.ni = 0;',
  27978. 'this.nu = 0;',
  27979. '']),
  27980. LinesToStr([ // $mod.$main
  27981. '$mod.DoIt$1($mod.d);',
  27982. '$mod.DoIt$1($mod.dt);',
  27983. '$mod.DoIt($mod.i);',
  27984. '$mod.DoIt($mod.b);',
  27985. '$mod.DoIt($mod.shi);',
  27986. '$mod.DoIt($mod.w);',
  27987. '$mod.DoIt($mod.smi);',
  27988. '$mod.DoIt($mod.lw);',
  27989. '$mod.DoIt($mod.li);',
  27990. '$mod.DoIt($mod.ni);',
  27991. '$mod.DoIt($mod.nu);',
  27992. '']));
  27993. end;
  27994. procedure TTestModule.TestJSValue_OverloadWord;
  27995. begin
  27996. StartProgram(false);
  27997. Add([
  27998. 'type',
  27999. ' integer = longint;',
  28000. ' int53 = nativeint;',
  28001. ' tdatetime = double;',
  28002. 'procedure DoIt(w: word); begin end;',
  28003. 'procedure DoIt(v: jsvalue); begin end;',
  28004. 'var',
  28005. ' d: double;',
  28006. ' dt: tdatetime;',
  28007. ' i: integer;',
  28008. ' b: byte;',
  28009. ' shi: shortint;',
  28010. ' w: word;',
  28011. ' smi: smallint;',
  28012. ' lw: longword;',
  28013. ' li: longint;',
  28014. ' ni: nativeint;',
  28015. ' nu: nativeuint;',
  28016. 'begin',
  28017. ' DoIt(d);',
  28018. ' DoIt(dt);',
  28019. ' DoIt(i);',
  28020. ' DoIt(b);',
  28021. ' DoIt(shi);',
  28022. ' DoIt(w);',
  28023. ' DoIt(smi);',
  28024. ' DoIt(lw);',
  28025. ' DoIt(li);',
  28026. ' DoIt(ni);',
  28027. ' DoIt(nu);',
  28028. '']);
  28029. ConvertProgram;
  28030. CheckSource('TestJSValue_OverloadWord',
  28031. LinesToStr([ // statements
  28032. 'this.DoIt = function (w) {',
  28033. '};',
  28034. 'this.DoIt$1 = function (v) {',
  28035. '};',
  28036. 'this.d = 0.0;',
  28037. 'this.dt = 0.0;',
  28038. 'this.i = 0;',
  28039. 'this.b = 0;',
  28040. 'this.shi = 0;',
  28041. 'this.w = 0;',
  28042. 'this.smi = 0;',
  28043. 'this.lw = 0;',
  28044. 'this.li = 0;',
  28045. 'this.ni = 0;',
  28046. 'this.nu = 0;',
  28047. '']),
  28048. LinesToStr([ // $mod.$main
  28049. '$mod.DoIt$1($mod.d);',
  28050. '$mod.DoIt$1($mod.dt);',
  28051. '$mod.DoIt$1($mod.i);',
  28052. '$mod.DoIt($mod.b);',
  28053. '$mod.DoIt($mod.shi);',
  28054. '$mod.DoIt($mod.w);',
  28055. '$mod.DoIt$1($mod.smi);',
  28056. '$mod.DoIt$1($mod.lw);',
  28057. '$mod.DoIt$1($mod.li);',
  28058. '$mod.DoIt$1($mod.ni);',
  28059. '$mod.DoIt$1($mod.nu);',
  28060. '']));
  28061. end;
  28062. procedure TTestModule.TestJSValue_OverloadString;
  28063. begin
  28064. StartProgram(false);
  28065. Add([
  28066. 'type',
  28067. ' uni = string;',
  28068. ' WChar = char;',
  28069. 'procedure DoIt(s: string); begin end;',
  28070. 'procedure DoIt(v: jsvalue); begin end;',
  28071. 'var',
  28072. ' s: string;',
  28073. ' c: char;',
  28074. ' u: uni;',
  28075. 'begin',
  28076. ' DoIt(s);',
  28077. ' DoIt(c);',
  28078. ' DoIt(u);',
  28079. '']);
  28080. ConvertProgram;
  28081. CheckSource('TestJSValue_OverloadString',
  28082. LinesToStr([ // statements
  28083. 'this.DoIt = function (s) {',
  28084. '};',
  28085. 'this.DoIt$1 = function (v) {',
  28086. '};',
  28087. 'this.s = "";',
  28088. 'this.c = "";',
  28089. 'this.u = "";',
  28090. '']),
  28091. LinesToStr([ // $mod.$main
  28092. '$mod.DoIt($mod.s);',
  28093. '$mod.DoIt($mod.c);',
  28094. '$mod.DoIt($mod.u);',
  28095. '']));
  28096. end;
  28097. procedure TTestModule.TestJSValue_OverloadChar;
  28098. begin
  28099. StartProgram(false);
  28100. Add([
  28101. 'type',
  28102. ' uni = string;',
  28103. ' WChar = char;',
  28104. 'procedure DoIt(c: char); begin end;',
  28105. 'procedure DoIt(v: jsvalue); begin end;',
  28106. 'var',
  28107. ' s: string;',
  28108. ' c: char;',
  28109. ' u: uni;',
  28110. 'begin',
  28111. ' DoIt(s);',
  28112. ' DoIt(c);',
  28113. ' DoIt(u);',
  28114. '']);
  28115. ConvertProgram;
  28116. CheckSource('TestJSValue_OverloadChar',
  28117. LinesToStr([ // statements
  28118. 'this.DoIt = function (c) {',
  28119. '};',
  28120. 'this.DoIt$1 = function (v) {',
  28121. '};',
  28122. 'this.s = "";',
  28123. 'this.c = "";',
  28124. 'this.u = "";',
  28125. '']),
  28126. LinesToStr([ // $mod.$main
  28127. '$mod.DoIt$1($mod.s);',
  28128. '$mod.DoIt($mod.c);',
  28129. '$mod.DoIt$1($mod.u);',
  28130. '']));
  28131. end;
  28132. procedure TTestModule.TestJSValue_OverloadPointer;
  28133. begin
  28134. StartProgram(false);
  28135. Add([
  28136. 'type',
  28137. ' TObject = class end;',
  28138. 'procedure DoIt(p: pointer); begin end;',
  28139. 'procedure DoIt(v: jsvalue); begin end;',
  28140. 'var',
  28141. ' o: TObject;',
  28142. 'begin',
  28143. ' DoIt(o);',
  28144. '']);
  28145. ConvertProgram;
  28146. CheckSource('TestJSValue_OverloadPointer',
  28147. LinesToStr([ // statements
  28148. 'rtl.createClass(this, "TObject", null, function () {',
  28149. ' this.$init = function () {',
  28150. ' };',
  28151. ' this.$final = function () {',
  28152. ' };',
  28153. '});',
  28154. 'this.DoIt = function (p) {',
  28155. '};',
  28156. 'this.DoIt$1 = function (v) {',
  28157. '};',
  28158. 'this.o = null;',
  28159. '']),
  28160. LinesToStr([ // $mod.$main
  28161. '$mod.DoIt($mod.o);',
  28162. '']));
  28163. end;
  28164. procedure TTestModule.TestJSValue_ForIn;
  28165. begin
  28166. StartProgram(false);
  28167. Add([
  28168. 'var',
  28169. ' v: JSValue;',
  28170. ' key: string;',
  28171. 'begin',
  28172. ' for key in v do begin',
  28173. ' if key=''abc'' then ;',
  28174. ' end;',
  28175. '']);
  28176. ConvertProgram;
  28177. CheckSource('TestJSValue_ForIn',
  28178. LinesToStr([ // statements
  28179. 'this.v = undefined;',
  28180. 'this.key = "";',
  28181. '']),
  28182. LinesToStr([ // $mod.$main
  28183. 'for ($mod.key in $mod.v) {',
  28184. ' if ($mod.key === "abc") ;',
  28185. '};',
  28186. '']));
  28187. end;
  28188. procedure TTestModule.TestRTTI_IntRange;
  28189. begin
  28190. WithTypeInfo:=true;
  28191. StartProgram(true,[supTypeInfo]);
  28192. Add([
  28193. '{$modeswitch externalclass}',
  28194. 'type',
  28195. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28196. ' TColor = type TGraphicsColor;',
  28197. 'var',
  28198. ' p: TTypeInfo;',
  28199. ' k: TTypeKind;',
  28200. 'begin',
  28201. ' p:=typeinfo(TGraphicsColor);',
  28202. ' p:=typeinfo(TColor);',
  28203. ' k:=GetTypeKind(TGraphicsColor);',
  28204. ' k:=GetTypeKind(TColor);',
  28205. '']);
  28206. ConvertProgram;
  28207. CheckSource('TestRTTI_IntRange',
  28208. LinesToStr([ // statements
  28209. 'this.$rtti.$Int("TGraphicsColor", {',
  28210. ' minvalue: -2147483648,',
  28211. ' maxvalue: 2147483647,',
  28212. ' ordtype: 4',
  28213. '});',
  28214. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28215. 'this.p = null;',
  28216. 'this.k = 0;',
  28217. '']),
  28218. LinesToStr([ // $mod.$main
  28219. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28220. '$mod.p = $mod.$rtti["TColor"];',
  28221. '$mod.k = 1;',
  28222. '$mod.k = 1;',
  28223. '']));
  28224. end;
  28225. procedure TTestModule.TestRTTI_Double;
  28226. begin
  28227. WithTypeInfo:=true;
  28228. StartProgram(true,[supTypeInfo]);
  28229. Add([
  28230. '{$modeswitch externalclass}',
  28231. 'type',
  28232. ' TFloat = type double;',
  28233. 'var',
  28234. ' p: TTypeInfo;',
  28235. 'begin',
  28236. ' p:=typeinfo(double);',
  28237. ' p:=typeinfo(TFloat);',
  28238. '']);
  28239. ConvertProgram;
  28240. CheckSource('TestRTTI_Double',
  28241. LinesToStr([ // statements
  28242. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28243. 'this.p = null;',
  28244. '']),
  28245. LinesToStr([ // $mod.$main
  28246. '$mod.p = rtl.double;',
  28247. '$mod.p = $mod.$rtti["TFloat"];',
  28248. '']));
  28249. end;
  28250. procedure TTestModule.TestRTTI_ProcType;
  28251. begin
  28252. WithTypeInfo:=true;
  28253. StartProgram(false);
  28254. Add('type');
  28255. Add(' TProcA = procedure;');
  28256. Add(' TMethodB = procedure of object;');
  28257. Add(' TProcC = procedure; varargs;');
  28258. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28259. Add(' TProcE = function: nativeint;');
  28260. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28261. Add('var p: pointer;');
  28262. Add('begin');
  28263. Add(' p:=typeinfo(tproca);');
  28264. ConvertProgram;
  28265. CheckSource('TestRTTI_ProcType',
  28266. LinesToStr([ // statements
  28267. 'this.$rtti.$ProcVar("TProcA", {',
  28268. ' procsig: rtl.newTIProcSig(null)',
  28269. '});',
  28270. 'this.$rtti.$MethodVar("TMethodB", {',
  28271. ' procsig: rtl.newTIProcSig(null),',
  28272. ' methodkind: 0',
  28273. '});',
  28274. 'this.$rtti.$ProcVar("TProcC", {',
  28275. ' procsig: rtl.newTIProcSig(null, 2)',
  28276. '});',
  28277. 'this.$rtti.$ProcVar("TProcD", {',
  28278. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28279. '});',
  28280. 'this.$rtti.$ProcVar("TProcE", {',
  28281. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28282. '});',
  28283. 'this.$rtti.$ProcVar("TProcF", {',
  28284. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28285. '});',
  28286. 'this.p = null;',
  28287. '']),
  28288. LinesToStr([ // $mod.$main
  28289. '$mod.p = $mod.$rtti["TProcA"];',
  28290. '']));
  28291. end;
  28292. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28293. begin
  28294. WithTypeInfo:=true;
  28295. AddModuleWithIntfImplSrc('unit2.pas',
  28296. LinesToStr([
  28297. 'type',
  28298. ' TObject = class end;'
  28299. ]),
  28300. '');
  28301. StartUnit(true);
  28302. Add('interface');
  28303. Add('uses unit2;');
  28304. Add('type');
  28305. Add(' TProcA = function(o: tobject): tobject;');
  28306. Add('implementation');
  28307. Add('type');
  28308. Add(' TProcB = function(o: tobject): tobject;');
  28309. Add('var p: Pointer;');
  28310. Add('initialization');
  28311. Add(' p:=typeinfo(tproca);');
  28312. Add(' p:=typeinfo(tprocb);');
  28313. ConvertUnit;
  28314. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28315. LinesToStr([ // statements
  28316. 'var $impl = $mod.$impl;',
  28317. 'this.$rtti.$ProcVar("TProcA", {',
  28318. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28319. '});',
  28320. '']),
  28321. LinesToStr([ // this.$init
  28322. '$impl.p = $mod.$rtti["TProcA"];',
  28323. '$impl.p = $mod.$rtti["TProcB"];',
  28324. '']),
  28325. LinesToStr([ // implementation
  28326. '$mod.$rtti.$ProcVar("TProcB", {',
  28327. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28328. '});',
  28329. '$impl.p = null;',
  28330. '']) );
  28331. end;
  28332. procedure TTestModule.TestRTTI_EnumAndSetType;
  28333. begin
  28334. WithTypeInfo:=true;
  28335. StartProgram(false);
  28336. Add('type');
  28337. Add(' TFlag = (light,dark);');
  28338. Add(' TFlags = set of TFlag;');
  28339. Add(' TProc = function(f: TFlags): TFlag;');
  28340. Add('var p: pointer;');
  28341. Add('begin');
  28342. Add(' p:=typeinfo(tflag);');
  28343. Add(' p:=typeinfo(tflags);');
  28344. ConvertProgram;
  28345. CheckSource('TestRTTI_EnumAndType',
  28346. LinesToStr([ // statements
  28347. 'this.TFlag = {',
  28348. ' "0": "light",',
  28349. ' light: 0,',
  28350. ' "1": "dark",',
  28351. ' dark: 1',
  28352. '};',
  28353. 'this.$rtti.$Enum("TFlag", {',
  28354. ' minvalue: 0,',
  28355. ' maxvalue: 1,',
  28356. ' ordtype: 1,',
  28357. ' enumtype: this.TFlag',
  28358. '});',
  28359. 'this.$rtti.$Set("TFlags", {',
  28360. ' comptype: this.$rtti["TFlag"]',
  28361. '});',
  28362. 'this.$rtti.$ProcVar("TProc", {',
  28363. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28364. '});',
  28365. 'this.p = null;',
  28366. '']),
  28367. LinesToStr([ // $mod.$main
  28368. '$mod.p = $mod.$rtti["TFlag"];',
  28369. '$mod.p = $mod.$rtti["TFlags"];',
  28370. '']));
  28371. end;
  28372. procedure TTestModule.TestRTTI_EnumRange;
  28373. begin
  28374. WithTypeInfo:=true;
  28375. StartProgram(false);
  28376. Add([
  28377. 'type',
  28378. ' TCol = (red,green,blue);',
  28379. ' TColRg = green..blue;',
  28380. ' TSetOfColRg = set of TColRg;',
  28381. 'var p: pointer;',
  28382. 'begin',
  28383. ' p:=typeinfo(tcolrg);',
  28384. ' p:=typeinfo(tsetofcolrg);',
  28385. '']);
  28386. ConvertProgram;
  28387. end;
  28388. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28389. begin
  28390. WithTypeInfo:=true;
  28391. StartProgram(false);
  28392. Add('type');
  28393. Add(' TFlags = set of (red, green);');
  28394. Add('var');
  28395. Add(' f: TFlags;');
  28396. Add('begin');
  28397. Add(' Include(f,red);');
  28398. ConvertProgram;
  28399. CheckSource('TestRTTI_AnonymousEnumType',
  28400. LinesToStr([ // statements
  28401. 'this.TFlags$a = {',
  28402. ' "0": "red",',
  28403. ' red: 0,',
  28404. ' "1": "green",',
  28405. ' green: 1',
  28406. '};',
  28407. 'this.$rtti.$Enum("TFlags$a", {',
  28408. ' minvalue: 0,',
  28409. ' maxvalue: 1,',
  28410. ' ordtype: 1,',
  28411. ' enumtype: this.TFlags$a',
  28412. '});',
  28413. 'this.$rtti.$Set("TFlags", {',
  28414. ' comptype: this.$rtti["TFlags$a"]',
  28415. '});',
  28416. 'this.f = {};',
  28417. '']),
  28418. LinesToStr([
  28419. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28420. '']));
  28421. end;
  28422. procedure TTestModule.TestRTTI_StaticArray;
  28423. begin
  28424. WithTypeInfo:=true;
  28425. StartProgram(false);
  28426. Add('type');
  28427. Add(' TFlag = (light,dark);');
  28428. Add(' TFlagNames = array[TFlag] of string;');
  28429. Add(' TBoolNames = array[boolean] of string;');
  28430. Add(' TByteArray = array[1..32768] of byte;');
  28431. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28432. Add('var p: pointer;');
  28433. Add('begin');
  28434. Add(' p:=typeinfo(TFlagNames);');
  28435. Add(' p:=typeinfo(TBoolNames);');
  28436. ConvertProgram;
  28437. CheckSource('TestRTTI_StaticArray',
  28438. LinesToStr([ // statements
  28439. 'this.TFlag = {',
  28440. ' "0": "light",',
  28441. ' light: 0,',
  28442. ' "1": "dark",',
  28443. ' dark: 1',
  28444. '};',
  28445. 'this.$rtti.$Enum("TFlag", {',
  28446. ' minvalue: 0,',
  28447. ' maxvalue: 1,',
  28448. ' ordtype: 1,',
  28449. ' enumtype: this.TFlag',
  28450. '});',
  28451. 'this.$rtti.$StaticArray("TFlagNames", {',
  28452. ' dims: [2],',
  28453. ' eltype: rtl.string',
  28454. '});',
  28455. 'this.$rtti.$StaticArray("TBoolNames", {',
  28456. ' dims: [2],',
  28457. ' eltype: rtl.string',
  28458. '});',
  28459. 'this.$rtti.$StaticArray("TByteArray", {',
  28460. ' dims: [32768],',
  28461. ' eltype: rtl.byte',
  28462. '});',
  28463. 'this.$rtti.$ProcVar("TProc", {',
  28464. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28465. '});',
  28466. 'this.p = null;',
  28467. '']),
  28468. LinesToStr([ // $mod.$main
  28469. '$mod.p = $mod.$rtti["TFlagNames"];',
  28470. '$mod.p = $mod.$rtti["TBoolNames"];',
  28471. '']));
  28472. end;
  28473. procedure TTestModule.TestRTTI_DynArray;
  28474. begin
  28475. WithTypeInfo:=true;
  28476. StartProgram(false);
  28477. Add('type');
  28478. Add(' TArrStr = array of string;');
  28479. Add(' TArr2Dim = array of tarrstr;');
  28480. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28481. Add('var p: pointer;');
  28482. Add('begin');
  28483. Add(' p:=typeinfo(tarrstr);');
  28484. Add(' p:=typeinfo(tarr2dim);');
  28485. ConvertProgram;
  28486. CheckSource('TestRTTI_DynArray',
  28487. LinesToStr([ // statements
  28488. 'this.$rtti.$DynArray("TArrStr", {',
  28489. ' eltype: rtl.string',
  28490. '});',
  28491. 'this.$rtti.$DynArray("TArr2Dim", {',
  28492. ' eltype: this.$rtti["TArrStr"]',
  28493. '});',
  28494. 'this.$rtti.$ProcVar("TProc", {',
  28495. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28496. '});',
  28497. 'this.p = null;',
  28498. '']),
  28499. LinesToStr([ // $mod.$main
  28500. '$mod.p = $mod.$rtti["TArrStr"];',
  28501. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28502. '']));
  28503. end;
  28504. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28505. begin
  28506. WithTypeInfo:=true;
  28507. StartProgram(false);
  28508. Add('type');
  28509. Add(' TArr = array of array of longint;');
  28510. Add('var a: TArr;');
  28511. Add('begin');
  28512. ConvertProgram;
  28513. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28514. LinesToStr([ // statements
  28515. 'this.$rtti.$DynArray("TArr$a", {',
  28516. ' eltype: rtl.longint',
  28517. '});',
  28518. 'this.$rtti.$DynArray("TArr", {',
  28519. ' eltype: this.$rtti["TArr$a"]',
  28520. '});',
  28521. 'this.a = [];',
  28522. '']),
  28523. LinesToStr([ // $mod.$main
  28524. ]));
  28525. end;
  28526. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28527. begin
  28528. WithTypeInfo:=true;
  28529. StartProgram(false);
  28530. Add('type');
  28531. Add(' TObject = class');
  28532. Add(' published');
  28533. Add(' procedure Proc; virtual; abstract;');
  28534. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28535. Add(' end;');
  28536. Add('begin');
  28537. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28538. nDuplicatePublishedMethodXAtY);
  28539. ConvertProgram;
  28540. end;
  28541. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28542. begin
  28543. WithTypeInfo:=true;
  28544. StartProgram(false);
  28545. Add('type');
  28546. Add(' TObject = class');
  28547. Add(' published');
  28548. Add(' procedure Proc; external name ''foo'';');
  28549. Add(' end;');
  28550. Add('begin');
  28551. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28552. nPublishedNameMustMatchExternal);
  28553. ConvertProgram;
  28554. end;
  28555. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28556. begin
  28557. WithTypeInfo:=true;
  28558. StartProgram(false);
  28559. Add('type');
  28560. Add(' TObject = class');
  28561. Add(' class var FA: longint;');
  28562. Add(' published');
  28563. Add(' class property A: longint read FA;');
  28564. Add(' end;');
  28565. Add('begin');
  28566. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28567. nInvalidXModifierY);
  28568. ConvertProgram;
  28569. end;
  28570. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28571. begin
  28572. WithTypeInfo:=true;
  28573. StartProgram(false);
  28574. Add('type');
  28575. Add(' TObject = class');
  28576. Add(' published');
  28577. Add(' class var FA: longint;');
  28578. Add(' end;');
  28579. Add('begin');
  28580. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28581. nSymbolCannotBePublished);
  28582. ConvertProgram;
  28583. end;
  28584. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28585. begin
  28586. WithTypeInfo:=true;
  28587. StartProgram(false);
  28588. Add('{$modeswitch externalclass}');
  28589. Add('type');
  28590. Add(' TObject = class');
  28591. Add(' published');
  28592. Add(' V: longint; external name ''foo'';');
  28593. Add(' end;');
  28594. Add('begin');
  28595. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28596. nPublishedNameMustMatchExternal);
  28597. ConvertProgram;
  28598. end;
  28599. procedure TTestModule.TestRTTI_Class_Field;
  28600. begin
  28601. WithTypeInfo:=true;
  28602. StartProgram(false);
  28603. Add('{$modeswitch externalclass}');
  28604. Add('type');
  28605. Add(' TObject = class');
  28606. Add(' private');
  28607. Add(' FPropA: string;');
  28608. Add(' published');
  28609. Add(' VarLI: longint;');
  28610. Add(' VarC: char;');
  28611. Add(' VarS: string;');
  28612. Add(' VarD: double;');
  28613. Add(' VarB: boolean;');
  28614. Add(' VarLW: longword;');
  28615. Add(' VarSmI: smallint;');
  28616. Add(' VarW: word;');
  28617. Add(' VarShI: shortint;');
  28618. Add(' VarBy: byte;');
  28619. Add(' VarExt: longint external name ''VarExt'';');
  28620. Add(' ArrA, ArrB: array of byte;');
  28621. Add(' end;');
  28622. Add('var p: pointer;');
  28623. Add(' Obj: tobject;');
  28624. Add('begin');
  28625. Add(' p:=typeinfo(tobject);');
  28626. Add(' p:=typeinfo(p);');
  28627. Add(' p:=typeinfo(obj);');
  28628. ConvertProgram;
  28629. CheckSource('TestRTTI_Class_Field',
  28630. LinesToStr([ // statements
  28631. 'rtl.createClass(this, "TObject", null, function () {',
  28632. ' this.$init = function () {',
  28633. ' this.FPropA = "";',
  28634. ' this.VarLI = 0;',
  28635. ' this.VarC = "";',
  28636. ' this.VarS = "";',
  28637. ' this.VarD = 0.0;',
  28638. ' this.VarB = false;',
  28639. ' this.VarLW = 0;',
  28640. ' this.VarSmI = 0;',
  28641. ' this.VarW = 0;',
  28642. ' this.VarShI = 0;',
  28643. ' this.VarBy = 0;',
  28644. ' this.ArrA = [];',
  28645. ' this.ArrB = [];',
  28646. ' };',
  28647. ' this.$final = function () {',
  28648. ' this.ArrA = undefined;',
  28649. ' this.ArrB = undefined;',
  28650. ' };',
  28651. ' var $r = this.$rtti;',
  28652. ' $r.addField("VarLI", rtl.longint);',
  28653. ' $r.addField("VarC", rtl.char);',
  28654. ' $r.addField("VarS", rtl.string);',
  28655. ' $r.addField("VarD", rtl.double);',
  28656. ' $r.addField("VarB", rtl.boolean);',
  28657. ' $r.addField("VarLW", rtl.longword);',
  28658. ' $r.addField("VarSmI", rtl.smallint);',
  28659. ' $r.addField("VarW", rtl.word);',
  28660. ' $r.addField("VarShI", rtl.shortint);',
  28661. ' $r.addField("VarBy", rtl.byte);',
  28662. ' $r.addField("VarExt", rtl.longint);',
  28663. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28664. ' eltype: rtl.byte',
  28665. ' });',
  28666. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28667. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28668. '});',
  28669. 'this.p = null;',
  28670. 'this.Obj = null;',
  28671. '']),
  28672. LinesToStr([ // $mod.$main
  28673. '$mod.p = $mod.$rtti["TObject"];',
  28674. '$mod.p = rtl.pointer;',
  28675. '$mod.p = $mod.Obj.$rtti;',
  28676. '']));
  28677. end;
  28678. procedure TTestModule.TestRTTI_Class_Method;
  28679. begin
  28680. WithTypeInfo:=true;
  28681. StartProgram(false);
  28682. Add('type');
  28683. Add(' TObject = class');
  28684. Add(' private');
  28685. Add(' procedure Internal; external name ''$intern'';');
  28686. Add(' published');
  28687. Add(' procedure Click; virtual; abstract;');
  28688. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28689. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28690. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28691. Add(' end;');
  28692. Add('begin');
  28693. ConvertProgram;
  28694. CheckSource('TestRTTI_Class_Method',
  28695. LinesToStr([ // statements
  28696. 'rtl.createClass(this, "TObject", null, function () {',
  28697. ' this.$init = function () {',
  28698. ' };',
  28699. ' this.$final = function () {',
  28700. ' };',
  28701. ' var $r = this.$rtti;',
  28702. ' $r.addMethod("Click", 0, null);',
  28703. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28704. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28705. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28706. ' flags: 2',
  28707. ' });',
  28708. '});',
  28709. '']),
  28710. LinesToStr([ // $mod.$main
  28711. '']));
  28712. end;
  28713. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28714. begin
  28715. WithTypeInfo:=true;
  28716. StartProgram(false);
  28717. Add('type');
  28718. Add(' TObject = class');
  28719. Add(' published');
  28720. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28721. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28722. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28723. Add(' end;');
  28724. Add('begin');
  28725. ConvertProgram;
  28726. CheckSource('TestRTTI_Class_MethodOpenArray',
  28727. LinesToStr([ // statements
  28728. 'rtl.createClass(this, "TObject", null, function () {',
  28729. ' this.$init = function () {',
  28730. ' };',
  28731. ' this.$final = function () {',
  28732. ' };',
  28733. ' var $r = this.$rtti;',
  28734. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28735. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28736. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28737. '});',
  28738. '']),
  28739. LinesToStr([ // $mod.$main
  28740. '']));
  28741. end;
  28742. procedure TTestModule.TestRTTI_Class_Property;
  28743. begin
  28744. WithTypeInfo:=true;
  28745. StartProgram(false);
  28746. Add('{$modeswitch externalclass}');
  28747. Add('type');
  28748. Add(' TObject = class');
  28749. Add(' private');
  28750. Add(' FColor: longint;');
  28751. Add(' FColorStored: boolean;');
  28752. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28753. Add(' function GetColor: longint; virtual; abstract;');
  28754. Add(' function GetColorStored: boolean; virtual; abstract;');
  28755. Add(' FExtSize: longint external name ''$extSize'';');
  28756. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28757. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28758. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28759. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28760. Add(' published');
  28761. Add(' property ColorA: longint read FColor;');
  28762. Add(' property ColorB: longint write FColor;');
  28763. Add(' property ColorC: longint read GetColor write SetColor;');
  28764. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28765. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28766. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28767. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28768. Add(' end;');
  28769. Add('begin');
  28770. ConvertProgram;
  28771. CheckSource('TestRTTI_Class_Property',
  28772. LinesToStr([ // statements
  28773. 'rtl.createClass(this, "TObject", null, function () {',
  28774. ' this.$init = function () {',
  28775. ' this.FColor = 0;',
  28776. ' this.FColorStored = false;',
  28777. ' };',
  28778. ' this.$final = function () {',
  28779. ' };',
  28780. ' var $r = this.$rtti;',
  28781. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28782. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28783. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28784. ' $r.addProperty(',
  28785. ' "ColorD",',
  28786. ' 8,',
  28787. ' rtl.longint,',
  28788. ' "FColor",',
  28789. ' "FColor",',
  28790. ' {',
  28791. ' stored: "FColorStored"',
  28792. ' }',
  28793. ' );',
  28794. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28795. ' $r.addProperty(',
  28796. ' "ExtSizeB",',
  28797. ' 11,',
  28798. ' rtl.longint,',
  28799. ' "$getSize",',
  28800. ' "$setSize",',
  28801. ' {',
  28802. ' stored: "$extSizeStored"',
  28803. ' }',
  28804. ' );',
  28805. ' $r.addProperty(',
  28806. ' "ExtSizeC",',
  28807. ' 12,',
  28808. ' rtl.longint,',
  28809. ' "$extSize",',
  28810. ' "$extSize",',
  28811. ' {',
  28812. ' stored: "$getExtSizeStored"',
  28813. ' }',
  28814. ' );',
  28815. '});',
  28816. '']),
  28817. LinesToStr([ // $mod.$main
  28818. '']));
  28819. end;
  28820. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28821. begin
  28822. WithTypeInfo:=true;
  28823. StartProgram(false);
  28824. Add('{$modeswitch externalclass}');
  28825. Add('type');
  28826. Add(' integer = longint;');
  28827. Add(' TObject = class');
  28828. Add(' private');
  28829. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28830. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28831. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28832. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28833. Add(' published');
  28834. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28835. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28836. Add(' end;');
  28837. Add('begin');
  28838. ConvertProgram;
  28839. CheckSource('TestRTTI_Class_PropertyParams',
  28840. LinesToStr([ // statements
  28841. 'rtl.createClass(this, "TObject", null, function () {',
  28842. ' this.$init = function () {',
  28843. ' };',
  28844. ' this.$final = function () {',
  28845. ' };',
  28846. ' var $r = this.$rtti;',
  28847. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28848. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28849. '});',
  28850. '']),
  28851. LinesToStr([ // $mod.$main
  28852. '']));
  28853. end;
  28854. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28855. begin
  28856. WithTypeInfo:=true;
  28857. AddModuleWithIntfImplSrc('unit1.pas',
  28858. 'type TColor = -5..5;',
  28859. '');
  28860. StartProgram(true);
  28861. Add([
  28862. 'uses unit1;',
  28863. 'type',
  28864. ' TColorAlias = TColor;',
  28865. ' TColorTypeAlias = type TColor;',
  28866. ' TObject = class',
  28867. ' private',
  28868. ' fColor: TColor;',
  28869. ' fAlias: TColorAlias;',
  28870. ' fTypeAlias: TColorTypeAlias;',
  28871. ' published',
  28872. ' property Color: TColor read fcolor;',
  28873. ' property Alias: TColorAlias read falias;',
  28874. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28875. ' end;',
  28876. 'begin',
  28877. '']);
  28878. ConvertProgram;
  28879. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28880. LinesToStr([ // statements
  28881. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28882. 'rtl.createClass(this, "TObject", null, function () {',
  28883. ' this.$init = function () {',
  28884. ' this.fColor = 0;',
  28885. ' this.fAlias = 0;',
  28886. ' this.fTypeAlias = 0;',
  28887. ' };',
  28888. ' this.$final = function () {',
  28889. ' };',
  28890. ' var $r = this.$rtti;',
  28891. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28892. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28893. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28894. '});',
  28895. '']),
  28896. LinesToStr([ // $mod.$main
  28897. '']));
  28898. end;
  28899. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28900. begin
  28901. WithTypeInfo:=true;
  28902. StartProgram(false);
  28903. Add([
  28904. '{$modeswitch omitrtti}',
  28905. 'type',
  28906. ' TObject = class',
  28907. ' private',
  28908. ' FA: byte;',
  28909. ' published',
  28910. ' property A: byte read FA write FA;',
  28911. ' end;',
  28912. 'begin']);
  28913. ConvertProgram;
  28914. CheckSource('TestRTTI_Class_OmitRTTI',
  28915. LinesToStr([ // statements
  28916. 'rtl.createClass(this, "TObject", null, function () {',
  28917. ' this.$init = function () {',
  28918. ' this.FA = 0;',
  28919. ' };',
  28920. ' this.$final = function () {',
  28921. ' };',
  28922. '});',
  28923. '']),
  28924. LinesToStr([ // $mod.$main
  28925. '']));
  28926. end;
  28927. procedure TTestModule.TestRTTI_IndexModifier;
  28928. begin
  28929. WithTypeInfo:=true;
  28930. StartProgram(false);
  28931. Add([
  28932. 'type',
  28933. ' TEnum = (red, blue);',
  28934. ' TObject = class',
  28935. ' FB: boolean;',
  28936. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28937. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28938. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28939. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28940. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28941. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28942. ' published',
  28943. ' property B1: boolean index 1 read FB write SetIntBool;',
  28944. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28945. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28946. ' end;',
  28947. 'begin']);
  28948. ConvertProgram;
  28949. CheckSource('TestRTTI_IndexModifier',
  28950. LinesToStr([ // statements
  28951. 'this.TEnum = {',
  28952. ' "0": "red",',
  28953. ' red: 0,',
  28954. ' "1": "blue",',
  28955. ' blue: 1',
  28956. '};',
  28957. 'this.$rtti.$Enum("TEnum", {',
  28958. ' minvalue: 0,',
  28959. ' maxvalue: 1,',
  28960. ' ordtype: 1,',
  28961. ' enumtype: this.TEnum',
  28962. '});',
  28963. 'rtl.createClass(this, "TObject", null, function () {',
  28964. ' this.$init = function () {',
  28965. ' this.FB = false;',
  28966. ' };',
  28967. ' this.$final = function () {',
  28968. ' };',
  28969. ' var $r = this.$rtti;',
  28970. ' $r.addProperty(',
  28971. ' "B1",',
  28972. ' 18,',
  28973. ' rtl.boolean,',
  28974. ' "FB",',
  28975. ' "SetIntBool",',
  28976. ' {',
  28977. ' index: 1',
  28978. ' }',
  28979. ' );',
  28980. ' $r.addProperty(',
  28981. ' "B2",',
  28982. ' 17,',
  28983. ' rtl.boolean,',
  28984. ' "GetEnumBool",',
  28985. ' "FB",',
  28986. ' {',
  28987. ' index: $mod.TEnum.blue',
  28988. ' }',
  28989. ' );',
  28990. ' $r.addProperty(',
  28991. ' "I1",',
  28992. ' 19,',
  28993. ' rtl.boolean,',
  28994. ' "GetStrIntBool",',
  28995. ' "SetStrIntBool",',
  28996. ' {',
  28997. ' index: 2',
  28998. ' }',
  28999. ' );',
  29000. '});',
  29001. '']),
  29002. LinesToStr([ // $mod.$main
  29003. '']));
  29004. end;
  29005. procedure TTestModule.TestRTTI_StoredModifier;
  29006. begin
  29007. WithTypeInfo:=true;
  29008. StartProgram(false);
  29009. Add([
  29010. 'const',
  29011. ' ConstB = true;',
  29012. 'type',
  29013. ' TObject = class',
  29014. ' private',
  29015. ' FB: boolean;',
  29016. ' function IsBStored: boolean; virtual; abstract;',
  29017. ' published',
  29018. ' property BoolA: boolean read FB stored true;',
  29019. ' property BoolB: boolean read FB stored false;',
  29020. ' property BoolC: boolean read FB stored FB;',
  29021. ' property BoolD: boolean read FB stored ConstB;',
  29022. ' property BoolE: boolean read FB stored IsBStored;',
  29023. ' end;',
  29024. 'begin']);
  29025. ConvertProgram;
  29026. CheckSource('TestRTTI_StoredModifier',
  29027. LinesToStr([ // statements
  29028. 'this.ConstB = true;',
  29029. 'rtl.createClass(this, "TObject", null, function () {',
  29030. ' this.$init = function () {',
  29031. ' this.FB = false;',
  29032. ' };',
  29033. ' this.$final = function () {',
  29034. ' };',
  29035. ' var $r = this.$rtti;',
  29036. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29037. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29038. ' $r.addProperty(',
  29039. ' "BoolC",',
  29040. ' 8,',
  29041. ' rtl.boolean,',
  29042. ' "FB",',
  29043. ' "",',
  29044. ' {',
  29045. ' stored: "FB"',
  29046. ' }',
  29047. ' );',
  29048. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29049. ' $r.addProperty(',
  29050. ' "BoolE",',
  29051. ' 12,',
  29052. ' rtl.boolean,',
  29053. ' "FB",',
  29054. ' "",',
  29055. ' {',
  29056. ' stored: "IsBStored"',
  29057. ' }',
  29058. ' );',
  29059. '});',
  29060. '']),
  29061. LinesToStr([ // $mod.$main
  29062. '']));
  29063. end;
  29064. procedure TTestModule.TestRTTI_DefaultValue;
  29065. begin
  29066. WithTypeInfo:=true;
  29067. StartProgram(false);
  29068. Add([
  29069. 'type',
  29070. ' TEnum = (red, blue);',
  29071. 'const',
  29072. ' CB = true or false;',
  29073. ' CI = 1+2;',
  29074. 'type',
  29075. ' TObject = class',
  29076. ' FB: boolean;',
  29077. ' FI: longint;',
  29078. ' FE: TEnum;',
  29079. ' published',
  29080. ' property B1: boolean read FB default true;',
  29081. ' property B2: boolean read FB default CB;',
  29082. ' property B3: boolean read FB default test1.cb;',
  29083. ' property I1: longint read FI default 2;',
  29084. ' property I2: longint read FI default CI;',
  29085. ' property E1: TEnum read FE default red;',
  29086. ' property E2: TEnum read FE default TEnum.blue;',
  29087. ' end;',
  29088. 'begin']);
  29089. ConvertProgram;
  29090. CheckSource('TestRTTI_DefaultValue',
  29091. LinesToStr([ // statements
  29092. 'this.TEnum = {',
  29093. ' "0": "red",',
  29094. ' red: 0,',
  29095. ' "1": "blue",',
  29096. ' blue: 1',
  29097. '};',
  29098. 'this.$rtti.$Enum("TEnum", {',
  29099. ' minvalue: 0,',
  29100. ' maxvalue: 1,',
  29101. ' ordtype: 1,',
  29102. ' enumtype: this.TEnum',
  29103. '});',
  29104. 'this.CB = true || false;',
  29105. 'this.CI = 1 + 2;',
  29106. 'rtl.createClass(this, "TObject", null, function () {',
  29107. ' this.$init = function () {',
  29108. ' this.FB = false;',
  29109. ' this.FI = 0;',
  29110. ' this.FE = 0;',
  29111. ' };',
  29112. ' this.$final = function () {',
  29113. ' };',
  29114. ' var $r = this.$rtti;',
  29115. ' $r.addProperty(',
  29116. ' "B1",',
  29117. ' 0,',
  29118. ' rtl.boolean,',
  29119. ' "FB",',
  29120. ' "",',
  29121. ' {',
  29122. ' Default: true',
  29123. ' }',
  29124. ' );',
  29125. ' $r.addProperty(',
  29126. ' "B2",',
  29127. ' 0,',
  29128. ' rtl.boolean,',
  29129. ' "FB",',
  29130. ' "",',
  29131. ' {',
  29132. ' Default: true',
  29133. ' }',
  29134. ' );',
  29135. ' $r.addProperty(',
  29136. ' "B3",',
  29137. ' 0,',
  29138. ' rtl.boolean,',
  29139. ' "FB",',
  29140. ' "",',
  29141. ' {',
  29142. ' Default: true',
  29143. ' }',
  29144. ' );',
  29145. ' $r.addProperty(',
  29146. ' "I1",',
  29147. ' 0,',
  29148. ' rtl.longint,',
  29149. ' "FI",',
  29150. ' "",',
  29151. ' {',
  29152. ' Default: 2',
  29153. ' }',
  29154. ' );',
  29155. ' $r.addProperty(',
  29156. ' "I2",',
  29157. ' 0,',
  29158. ' rtl.longint,',
  29159. ' "FI",',
  29160. ' "",',
  29161. ' {',
  29162. ' Default: 3',
  29163. ' }',
  29164. ' );',
  29165. ' $r.addProperty(',
  29166. ' "E1",',
  29167. ' 0,',
  29168. ' $mod.$rtti["TEnum"],',
  29169. ' "FE",',
  29170. ' "",',
  29171. ' {',
  29172. ' Default: $mod.TEnum.red',
  29173. ' }',
  29174. ' );',
  29175. ' $r.addProperty(',
  29176. ' "E2",',
  29177. ' 0,',
  29178. ' $mod.$rtti["TEnum"],',
  29179. ' "FE",',
  29180. ' "",',
  29181. ' {',
  29182. ' Default: $mod.TEnum.blue',
  29183. ' }',
  29184. ' );',
  29185. '});',
  29186. '']),
  29187. LinesToStr([ // $mod.$main
  29188. '']));
  29189. end;
  29190. procedure TTestModule.TestRTTI_DefaultValueSet;
  29191. begin
  29192. WithTypeInfo:=true;
  29193. StartProgram(false);
  29194. Add([
  29195. 'type',
  29196. ' TEnum = (red, blue);',
  29197. ' TSet = set of TEnum;',
  29198. 'const',
  29199. ' CSet = [red,blue];',
  29200. 'type',
  29201. ' TObject = class',
  29202. ' FSet: TSet;',
  29203. ' published',
  29204. ' property Set1: TSet read FSet default [];',
  29205. ' property Set2: TSet read FSet default [red];',
  29206. ' property Set3: TSet read FSet default [red,blue];',
  29207. ' property Set4: TSet read FSet default CSet;',
  29208. ' end;',
  29209. 'begin']);
  29210. ConvertProgram;
  29211. CheckSource('TestRTTI_DefaultValueSet',
  29212. LinesToStr([ // statements
  29213. 'this.TEnum = {',
  29214. ' "0": "red",',
  29215. ' red: 0,',
  29216. ' "1": "blue",',
  29217. ' blue: 1',
  29218. '};',
  29219. 'this.$rtti.$Enum("TEnum", {',
  29220. ' minvalue: 0,',
  29221. ' maxvalue: 1,',
  29222. ' ordtype: 1,',
  29223. ' enumtype: this.TEnum',
  29224. '});',
  29225. 'this.$rtti.$Set("TSet", {',
  29226. ' comptype: this.$rtti["TEnum"]',
  29227. '});',
  29228. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29229. 'rtl.createClass(this, "TObject", null, function () {',
  29230. ' this.$init = function () {',
  29231. ' this.FSet = {};',
  29232. ' };',
  29233. ' this.$final = function () {',
  29234. ' this.FSet = undefined;',
  29235. ' };',
  29236. ' var $r = this.$rtti;',
  29237. ' $r.addProperty(',
  29238. ' "Set1",',
  29239. ' 0,',
  29240. ' $mod.$rtti["TSet"],',
  29241. ' "FSet",',
  29242. ' "",',
  29243. ' {',
  29244. ' Default: {}',
  29245. ' }',
  29246. ' );',
  29247. ' $r.addProperty(',
  29248. ' "Set2",',
  29249. ' 0,',
  29250. ' $mod.$rtti["TSet"],',
  29251. ' "FSet",',
  29252. ' "",',
  29253. ' {',
  29254. ' Default: rtl.createSet($mod.TEnum.red)',
  29255. ' }',
  29256. ' );',
  29257. ' $r.addProperty(',
  29258. ' "Set3",',
  29259. ' 0,',
  29260. ' $mod.$rtti["TSet"],',
  29261. ' "FSet",',
  29262. ' "",',
  29263. ' {',
  29264. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29265. ' }',
  29266. ' );',
  29267. ' $r.addProperty(',
  29268. ' "Set4",',
  29269. ' 0,',
  29270. ' $mod.$rtti["TSet"],',
  29271. ' "FSet",',
  29272. ' "",',
  29273. ' {',
  29274. ' Default: $mod.CSet',
  29275. ' }',
  29276. ' );',
  29277. '});',
  29278. '']),
  29279. LinesToStr([ // $mod.$main
  29280. '']));
  29281. end;
  29282. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29283. begin
  29284. WithTypeInfo:=true;
  29285. StartProgram(false);
  29286. Add([
  29287. 'type',
  29288. ' TRg = -1..1;',
  29289. 'const',
  29290. ' l = low(TRg);',
  29291. ' h = high(TRg);',
  29292. 'type',
  29293. ' TObject = class',
  29294. ' FV: TRg;',
  29295. ' published',
  29296. ' property V1: TRg read FV default -1;',
  29297. ' end;',
  29298. 'begin']);
  29299. ConvertProgram;
  29300. CheckSource('TestRTTI_DefaultValueRangeType',
  29301. LinesToStr([ // statements
  29302. 'this.$rtti.$Int("TRg", {',
  29303. ' minvalue: -1,',
  29304. ' maxvalue: 1,',
  29305. ' ordtype: 0',
  29306. '});',
  29307. 'this.l = -1;',
  29308. 'this.h = 1;',
  29309. 'rtl.createClass(this, "TObject", null, function () {',
  29310. ' this.$init = function () {',
  29311. ' this.FV = 0;',
  29312. ' };',
  29313. ' this.$final = function () {',
  29314. ' };',
  29315. ' var $r = this.$rtti;',
  29316. ' $r.addProperty(',
  29317. ' "V1",',
  29318. ' 0,',
  29319. ' $mod.$rtti["TRg"],',
  29320. ' "FV",',
  29321. ' "",',
  29322. ' {',
  29323. ' Default: -1',
  29324. ' }',
  29325. ' );',
  29326. '});',
  29327. '']),
  29328. LinesToStr([ // $mod.$main
  29329. '']));
  29330. end;
  29331. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29332. begin
  29333. WithTypeInfo:=true;
  29334. StartProgram(false);
  29335. Add([
  29336. 'type',
  29337. ' TObject = class',
  29338. ' FA, FB: byte;',
  29339. ' property A: byte read FA default 1;',
  29340. ' property B: byte read FB default 2;',
  29341. ' end;',
  29342. ' TBird = class',
  29343. ' published',
  29344. ' property A;',
  29345. ' property B nodefault;',
  29346. ' end;',
  29347. 'begin']);
  29348. ConvertProgram;
  29349. CheckSource('TestRTTI_DefaultValueInherit',
  29350. LinesToStr([ // statements
  29351. 'rtl.createClass(this, "TObject", null, function () {',
  29352. ' this.$init = function () {',
  29353. ' this.FA = 0;',
  29354. ' this.FB = 0;',
  29355. ' };',
  29356. ' this.$final = function () {',
  29357. ' };',
  29358. '});',
  29359. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29360. ' var $r = this.$rtti;',
  29361. ' $r.addProperty(',
  29362. ' "A",',
  29363. ' 0,',
  29364. ' rtl.byte,',
  29365. ' "FA",',
  29366. ' "",',
  29367. ' {',
  29368. ' Default: 1',
  29369. ' }',
  29370. ' );',
  29371. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29372. '});',
  29373. '']),
  29374. LinesToStr([ // $mod.$main
  29375. '']));
  29376. end;
  29377. procedure TTestModule.TestRTTI_OverrideMethod;
  29378. begin
  29379. WithTypeInfo:=true;
  29380. StartProgram(false);
  29381. Add('type');
  29382. Add(' TObject = class');
  29383. Add(' published');
  29384. Add(' procedure DoIt; virtual; abstract;');
  29385. Add(' end;');
  29386. Add(' TSky = class');
  29387. Add(' published');
  29388. Add(' procedure DoIt; override;');
  29389. Add(' end;');
  29390. Add('procedure TSky.DoIt; begin end;');
  29391. Add('begin');
  29392. ConvertProgram;
  29393. CheckSource('TestRTTI_OverrideMethod',
  29394. LinesToStr([ // statements
  29395. 'rtl.createClass(this, "TObject", null, function () {',
  29396. ' this.$init = function () {',
  29397. ' };',
  29398. ' this.$final = function () {',
  29399. ' };',
  29400. ' var $r = this.$rtti;',
  29401. ' $r.addMethod("DoIt", 0, null);',
  29402. '});',
  29403. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29404. ' this.DoIt = function () {',
  29405. ' };',
  29406. '});',
  29407. '']),
  29408. LinesToStr([ // $mod.$main
  29409. '']));
  29410. end;
  29411. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29412. begin
  29413. WithTypeInfo:=true;
  29414. StartProgram(false);
  29415. Add([
  29416. 'type',
  29417. ' TObject = class',
  29418. ' published',
  29419. ' procedure DoIt;',
  29420. ' end;',
  29421. ' TSky = class',
  29422. ' published',
  29423. ' procedure DoIt; reintroduce;',
  29424. ' end;',
  29425. 'procedure TObject.DoIt; begin end;',
  29426. 'procedure TSky.DoIt;',
  29427. 'begin',
  29428. ' inherited DoIt;',
  29429. 'end;',
  29430. 'begin']);
  29431. ConvertProgram;
  29432. CheckSource('TestRTTI_ReintroduceMethod',
  29433. LinesToStr([ // statements
  29434. 'rtl.createClass(this, "TObject", null, function () {',
  29435. ' this.$init = function () {',
  29436. ' };',
  29437. ' this.$final = function () {',
  29438. ' };',
  29439. ' this.DoIt = function () {',
  29440. ' };',
  29441. ' var $r = this.$rtti;',
  29442. ' $r.addMethod("DoIt", 0, null);',
  29443. '});',
  29444. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29445. ' this.DoIt = function () {',
  29446. ' $mod.TObject.DoIt.call(this);',
  29447. ' };',
  29448. ' var $r = this.$rtti;',
  29449. ' $r.addMethod("DoIt", 0, null);',
  29450. '});',
  29451. '']),
  29452. LinesToStr([ // $mod.$main
  29453. '']));
  29454. end;
  29455. procedure TTestModule.TestRTTI_OverloadProperty;
  29456. begin
  29457. WithTypeInfo:=true;
  29458. StartProgram(false);
  29459. Add('type');
  29460. Add(' TObject = class');
  29461. Add(' protected');
  29462. Add(' FFlag: longint;');
  29463. Add(' published');
  29464. Add(' property Flag: longint read fflag;');
  29465. Add(' end;');
  29466. Add(' TSky = class');
  29467. Add(' published');
  29468. Add(' property FLAG: longint write fflag;');
  29469. Add(' end;');
  29470. Add('begin');
  29471. ConvertProgram;
  29472. CheckSource('TestRTTI_OverrideMethod',
  29473. LinesToStr([ // statements
  29474. 'rtl.createClass(this, "TObject", null, function () {',
  29475. ' this.$init = function () {',
  29476. ' this.FFlag = 0;',
  29477. ' };',
  29478. ' this.$final = function () {',
  29479. ' };',
  29480. ' var $r = this.$rtti;',
  29481. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29482. '});',
  29483. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29484. ' var $r = this.$rtti;',
  29485. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29486. '});',
  29487. '']),
  29488. LinesToStr([ // $mod.$main
  29489. '']));
  29490. end;
  29491. procedure TTestModule.TestRTTI_ClassForward;
  29492. begin
  29493. WithTypeInfo:=true;
  29494. StartProgram(false);
  29495. Add('type');
  29496. Add(' TObject = class end;');
  29497. Add(' tbridge = class;');
  29498. Add(' TProc = function: tbridge;');
  29499. Add(' TOger = class');
  29500. Add(' published');
  29501. Add(' FBridge: tbridge;');
  29502. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29503. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29504. Add(' end;');
  29505. Add(' TBridge = class');
  29506. Add(' FOger: toger;');
  29507. Add(' end;');
  29508. Add('var p: Pointer;');
  29509. Add(' b: tbridge;');
  29510. Add('begin');
  29511. Add(' p:=typeinfo(tbridge);');
  29512. Add(' p:=typeinfo(b);');
  29513. ConvertProgram;
  29514. CheckSource('TestRTTI_ClassForward',
  29515. LinesToStr([ // statements
  29516. 'rtl.createClass(this, "TObject", null, function () {',
  29517. ' this.$init = function () {',
  29518. ' };',
  29519. ' this.$final = function () {',
  29520. ' };',
  29521. '});',
  29522. 'this.$rtti.$Class("TBridge");',
  29523. 'this.$rtti.$ProcVar("TProc", {',
  29524. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29525. '});',
  29526. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29527. ' this.$init = function () {',
  29528. ' $mod.TObject.$init.call(this);',
  29529. ' this.FBridge = null;',
  29530. ' };',
  29531. ' this.$final = function () {',
  29532. ' this.FBridge = undefined;',
  29533. ' $mod.TObject.$final.call(this);',
  29534. ' };',
  29535. ' var $r = this.$rtti;',
  29536. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29537. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29538. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29539. '});',
  29540. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29541. ' this.$init = function () {',
  29542. ' $mod.TObject.$init.call(this);',
  29543. ' this.FOger = null;',
  29544. ' };',
  29545. ' this.$final = function () {',
  29546. ' this.FOger = undefined;',
  29547. ' $mod.TObject.$final.call(this);',
  29548. ' };',
  29549. '});',
  29550. 'this.p = null;',
  29551. 'this.b = null;',
  29552. '']),
  29553. LinesToStr([ // $mod.$main
  29554. '$mod.p = $mod.$rtti["TBridge"];',
  29555. '$mod.p = $mod.b.$rtti;',
  29556. '']));
  29557. end;
  29558. procedure TTestModule.TestRTTI_ClassOf;
  29559. begin
  29560. WithTypeInfo:=true;
  29561. StartProgram(false);
  29562. Add('type');
  29563. Add(' TClass = class of tobject;');
  29564. Add(' TProcA = function: TClass;');
  29565. Add(' TObject = class');
  29566. Add(' published');
  29567. Add(' C: tclass;');
  29568. Add(' end;');
  29569. Add(' tfox = class;');
  29570. Add(' TBird = class end;');
  29571. Add(' TBirds = class of tbird;');
  29572. Add(' TFox = class end;');
  29573. Add(' TFoxes = class of tfox;');
  29574. Add(' TCows = class of TCow;');
  29575. Add(' TCow = class;');
  29576. Add(' TCow = class end;');
  29577. Add('begin');
  29578. ConvertProgram;
  29579. CheckSource('TestRTTI_ClassOf',
  29580. LinesToStr([ // statements
  29581. 'this.$rtti.$Class("TObject");',
  29582. 'this.$rtti.$ClassRef("TClass", {',
  29583. ' instancetype: this.$rtti["TObject"]',
  29584. '});',
  29585. 'this.$rtti.$ProcVar("TProcA", {',
  29586. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29587. '});',
  29588. 'rtl.createClass(this, "TObject", null, function () {',
  29589. ' this.$init = function () {',
  29590. ' this.C = null;',
  29591. ' };',
  29592. ' this.$final = function () {',
  29593. ' this.C = undefined;',
  29594. ' };',
  29595. ' var $r = this.$rtti;',
  29596. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29597. '});',
  29598. 'this.$rtti.$Class("TFox");',
  29599. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29600. '});',
  29601. 'this.$rtti.$ClassRef("TBirds", {',
  29602. ' instancetype: this.$rtti["TBird"]',
  29603. '});',
  29604. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29605. '});',
  29606. 'this.$rtti.$ClassRef("TFoxes", {',
  29607. ' instancetype: this.$rtti["TFox"]',
  29608. '});',
  29609. 'this.$rtti.$Class("TCow");',
  29610. 'this.$rtti.$ClassRef("TCows", {',
  29611. ' instancetype: this.$rtti["TCow"]',
  29612. '});',
  29613. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29614. '});',
  29615. '']),
  29616. LinesToStr([ // $mod.$main
  29617. '']));
  29618. end;
  29619. procedure TTestModule.TestRTTI_Record;
  29620. begin
  29621. WithTypeInfo:=true;
  29622. StartProgram(false);
  29623. Add('type');
  29624. Add(' integer = longint;');
  29625. Add(' TPoint = record');
  29626. Add(' x,y: integer;');
  29627. Add(' end;');
  29628. Add('var p: pointer;');
  29629. Add(' r: tpoint;');
  29630. Add('begin');
  29631. Add(' p:=typeinfo(tpoint);');
  29632. Add(' p:=typeinfo(r);');
  29633. Add(' p:=typeinfo(r.x);');
  29634. ConvertProgram;
  29635. CheckSource('TestRTTI_Record',
  29636. LinesToStr([ // statements
  29637. 'rtl.recNewT(this, "TPoint", function () {',
  29638. ' this.x = 0;',
  29639. ' this.y = 0;',
  29640. ' this.$eq = function (b) {',
  29641. ' return (this.x === b.x) && (this.y === b.y);',
  29642. ' };',
  29643. ' this.$assign = function (s) {',
  29644. ' this.x = s.x;',
  29645. ' this.y = s.y;',
  29646. ' return this;',
  29647. ' };',
  29648. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29649. ' $r.addField("x", rtl.longint);',
  29650. ' $r.addField("y", rtl.longint);',
  29651. '});',
  29652. 'this.p = null;',
  29653. 'this.r = this.TPoint.$new();',
  29654. '']),
  29655. LinesToStr([ // $mod.$main
  29656. '$mod.p = $mod.$rtti["TPoint"];',
  29657. '$mod.p = $mod.$rtti["TPoint"];',
  29658. '$mod.p = rtl.longint;',
  29659. '']));
  29660. end;
  29661. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29662. begin
  29663. WithTypeInfo:=true;
  29664. StartProgram(false);
  29665. Add('type');
  29666. Add(' TFloatRec = record');
  29667. Add(' c,d: array of char;');
  29668. // Add(' i: array of array of longint;');
  29669. Add(' end;');
  29670. Add('var p: pointer;');
  29671. Add(' r: tfloatrec;');
  29672. Add('begin');
  29673. Add(' p:=typeinfo(tfloatrec);');
  29674. Add(' p:=typeinfo(r);');
  29675. Add(' p:=typeinfo(r.d);');
  29676. ConvertProgram;
  29677. CheckSource('TestRTTI_Record',
  29678. LinesToStr([ // statements
  29679. 'rtl.recNewT(this, "TFloatRec", function () {',
  29680. ' this.$new = function () {',
  29681. ' var r = Object.create(this);',
  29682. ' r.c = [];',
  29683. ' r.d = [];',
  29684. ' return r;',
  29685. ' };',
  29686. ' this.$eq = function (b) {',
  29687. ' return (this.c === b.c) && (this.d === b.d);',
  29688. ' };',
  29689. ' this.$assign = function (s) {',
  29690. ' this.c = rtl.arrayRef(s.c);',
  29691. ' this.d = rtl.arrayRef(s.d);',
  29692. ' return this;',
  29693. ' };',
  29694. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29695. ' eltype: rtl.char',
  29696. ' });',
  29697. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29698. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29699. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29700. '});',
  29701. 'this.p = null;',
  29702. 'this.r = this.TFloatRec.$new();',
  29703. '']),
  29704. LinesToStr([ // $mod.$main
  29705. '$mod.p = $mod.$rtti["TFloatRec"];',
  29706. '$mod.p = $mod.$rtti["TFloatRec"];',
  29707. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29708. '']));
  29709. end;
  29710. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29711. begin
  29712. WithTypeInfo:=true;
  29713. StartProgram(false);
  29714. Add([
  29715. '{$modeswitch AdvancedRecords}',
  29716. 'type',
  29717. ' TPoint = record',
  29718. ' type TProc = procedure(w: word);',
  29719. ' class var p: TProc;',
  29720. ' end;',
  29721. 'begin',
  29722. '']);
  29723. ConvertProgram;
  29724. CheckSource('TestRTTI_Record_ClassVarType',
  29725. LinesToStr([ // statements
  29726. 'rtl.recNewT(this, "TPoint", function () {',
  29727. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  29728. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  29729. ' });',
  29730. ' this.p = null;',
  29731. ' this.$eq = function (b) {',
  29732. ' return true;',
  29733. ' };',
  29734. ' this.$assign = function (s) {',
  29735. ' return this;',
  29736. ' };',
  29737. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29738. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  29739. '}, true);',
  29740. '']),
  29741. LinesToStr([ // $mod.$main
  29742. '']));
  29743. end;
  29744. procedure TTestModule.TestRTTI_LocalTypes;
  29745. begin
  29746. WithTypeInfo:=true;
  29747. StartProgram(false);
  29748. Add([
  29749. 'procedure DoIt;',
  29750. 'type',
  29751. ' integer = longint;',
  29752. ' TPoint = record',
  29753. ' x,y: integer;',
  29754. ' end;',
  29755. 'var p: TPoint;',
  29756. 'begin',
  29757. 'end;',
  29758. 'begin']);
  29759. ConvertProgram;
  29760. CheckSource('TestRTTI_LocalTypes',
  29761. LinesToStr([ // statements
  29762. 'var TPoint = rtl.recNewT(null, "", function () {',
  29763. ' this.x = 0;',
  29764. ' this.y = 0;',
  29765. ' this.$eq = function (b) {',
  29766. ' return (this.x === b.x) && (this.y === b.y);',
  29767. ' };',
  29768. ' this.$assign = function (s) {',
  29769. ' this.x = s.x;',
  29770. ' this.y = s.y;',
  29771. ' return this;',
  29772. ' };',
  29773. '});',
  29774. 'this.DoIt = function () {',
  29775. ' var p = TPoint.$new();',
  29776. '};',
  29777. '']),
  29778. LinesToStr([ // $mod.$main
  29779. '']));
  29780. end;
  29781. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29782. begin
  29783. WithTypeInfo:=true;
  29784. StartProgram(false);
  29785. Add([
  29786. 'type',
  29787. ' TCaption = string;',
  29788. ' TYesNo = boolean;',
  29789. ' TLetter = char;',
  29790. ' TFloat = double;',
  29791. ' TPtr = pointer;',
  29792. ' TShortInt = shortint;',
  29793. ' TByte = byte;',
  29794. ' TSmallInt = smallint;',
  29795. ' TWord = word;',
  29796. ' TInt32 = longint;',
  29797. ' TDWord = longword;',
  29798. ' TValue = jsvalue;',
  29799. 'var p: TPtr;',
  29800. 'begin',
  29801. ' p:=typeinfo(string);',
  29802. ' p:=typeinfo(tcaption);',
  29803. ' p:=typeinfo(boolean);',
  29804. ' p:=typeinfo(tyesno);',
  29805. ' p:=typeinfo(char);',
  29806. ' p:=typeinfo(tletter);',
  29807. ' p:=typeinfo(double);',
  29808. ' p:=typeinfo(tfloat);',
  29809. ' p:=typeinfo(pointer);',
  29810. ' p:=typeinfo(tptr);',
  29811. ' p:=typeinfo(shortint);',
  29812. ' p:=typeinfo(tshortint);',
  29813. ' p:=typeinfo(byte);',
  29814. ' p:=typeinfo(tbyte);',
  29815. ' p:=typeinfo(smallint);',
  29816. ' p:=typeinfo(tsmallint);',
  29817. ' p:=typeinfo(word);',
  29818. ' p:=typeinfo(tword);',
  29819. ' p:=typeinfo(longword);',
  29820. ' p:=typeinfo(tdword);',
  29821. ' p:=typeinfo(jsvalue);',
  29822. ' p:=typeinfo(tvalue);',
  29823. '']);
  29824. ConvertProgram;
  29825. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29826. LinesToStr([ // statements
  29827. 'this.p = null;',
  29828. '']),
  29829. LinesToStr([ // $mod.$main
  29830. '$mod.p = rtl.string;',
  29831. '$mod.p = rtl.string;',
  29832. '$mod.p = rtl.boolean;',
  29833. '$mod.p = rtl.boolean;',
  29834. '$mod.p = rtl.char;',
  29835. '$mod.p = rtl.char;',
  29836. '$mod.p = rtl.double;',
  29837. '$mod.p = rtl.double;',
  29838. '$mod.p = rtl.pointer;',
  29839. '$mod.p = rtl.pointer;',
  29840. '$mod.p = rtl.shortint;',
  29841. '$mod.p = rtl.shortint;',
  29842. '$mod.p = rtl.byte;',
  29843. '$mod.p = rtl.byte;',
  29844. '$mod.p = rtl.smallint;',
  29845. '$mod.p = rtl.smallint;',
  29846. '$mod.p = rtl.word;',
  29847. '$mod.p = rtl.word;',
  29848. '$mod.p = rtl.longword;',
  29849. '$mod.p = rtl.longword;',
  29850. '$mod.p = rtl.jsvalue;',
  29851. '$mod.p = rtl.jsvalue;',
  29852. '']));
  29853. end;
  29854. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29855. begin
  29856. WithTypeInfo:=true;
  29857. StartProgram(false);
  29858. Add([
  29859. 'type',
  29860. ' TCaption = type string;',
  29861. ' TYesNo = type boolean;',
  29862. ' TLetter = type char;',
  29863. ' TFloat = type double;',
  29864. ' TPtr = type pointer;',
  29865. ' TShortInt = type shortint;',
  29866. ' TByte = type byte;',
  29867. ' TSmallInt = type smallint;',
  29868. ' TWord = type word;',
  29869. ' TInt32 = type longint;',
  29870. ' TDWord = type longword;',
  29871. ' TValue = type jsvalue;',
  29872. ' TAliasValue = type TValue;',
  29873. 'var',
  29874. ' p: TPtr;',
  29875. ' a: TAliasValue;',
  29876. 'begin',
  29877. ' p:=typeinfo(tcaption);',
  29878. ' p:=typeinfo(tyesno);',
  29879. ' p:=typeinfo(tletter);',
  29880. ' p:=typeinfo(tfloat);',
  29881. ' p:=typeinfo(tptr);',
  29882. ' p:=typeinfo(tshortint);',
  29883. ' p:=typeinfo(tbyte);',
  29884. ' p:=typeinfo(tsmallint);',
  29885. ' p:=typeinfo(tword);',
  29886. ' p:=typeinfo(tdword);',
  29887. ' p:=typeinfo(tvalue);',
  29888. ' p:=typeinfo(taliasvalue);',
  29889. ' p:=typeinfo(a);',
  29890. '']);
  29891. ConvertProgram;
  29892. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29893. LinesToStr([ // statements
  29894. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  29895. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29896. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  29897. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29898. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29899. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29900. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  29901. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29902. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  29903. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  29904. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  29905. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29906. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  29907. 'this.p = null;',
  29908. 'this.a = undefined;',
  29909. '']),
  29910. LinesToStr([ // $mod.$main
  29911. '$mod.p = $mod.$rtti["TCaption"];',
  29912. '$mod.p = $mod.$rtti["TYesNo"];',
  29913. '$mod.p = $mod.$rtti["TLetter"];',
  29914. '$mod.p = $mod.$rtti["TFloat"];',
  29915. '$mod.p = $mod.$rtti["TPtr"];',
  29916. '$mod.p = $mod.$rtti["TShortInt"];',
  29917. '$mod.p = $mod.$rtti["TByte"];',
  29918. '$mod.p = $mod.$rtti["TSmallInt"];',
  29919. '$mod.p = $mod.$rtti["TWord"];',
  29920. '$mod.p = $mod.$rtti["TDWord"];',
  29921. '$mod.p = $mod.$rtti["TValue"];',
  29922. '$mod.p = $mod.$rtti["TAliasValue"];',
  29923. '$mod.p = $mod.$rtti["TAliasValue"];',
  29924. '']));
  29925. end;
  29926. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29927. begin
  29928. WithTypeInfo:=true;
  29929. StartProgram(false);
  29930. Add('procedure DoIt;');
  29931. Add('type');
  29932. Add(' integer = longint;');
  29933. Add(' TPoint = record');
  29934. Add(' x,y: integer;');
  29935. Add(' end;');
  29936. Add('var p: pointer;');
  29937. Add('begin');
  29938. Add(' p:=typeinfo(tpoint);');
  29939. Add('end;');
  29940. Add('begin');
  29941. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29942. ConvertProgram;
  29943. end;
  29944. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29945. begin
  29946. WithTypeInfo:=true;
  29947. StartProgram(true,[supTypeInfo]);
  29948. Add([
  29949. '{$modeswitch externalclass}',
  29950. 'type',
  29951. ' TFlag = (up,down);',
  29952. ' TFlags = set of TFlag;',
  29953. 'var',
  29954. ' ti: TTypeInfo;',
  29955. ' tiInt: TTypeInfoInteger;',
  29956. ' tiEnum: TTypeInfoEnum;',
  29957. ' tiSet: TTypeInfoSet;',
  29958. 'begin',
  29959. ' ti:=typeinfo(string);',
  29960. ' ti:=typeinfo(boolean);',
  29961. ' ti:=typeinfo(char);',
  29962. ' ti:=typeinfo(double);',
  29963. ' tiInt:=typeinfo(shortint);',
  29964. ' tiInt:=typeinfo(byte);',
  29965. ' tiInt:=typeinfo(smallint);',
  29966. ' tiInt:=typeinfo(word);',
  29967. ' tiInt:=typeinfo(longint);',
  29968. ' tiInt:=typeinfo(longword);',
  29969. ' ti:=typeinfo(jsvalue);',
  29970. ' tiEnum:=typeinfo(tflag);',
  29971. ' tiSet:=typeinfo(tflags);']);
  29972. ConvertProgram;
  29973. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29974. LinesToStr([ // statements
  29975. 'this.TFlag = {',
  29976. ' "0": "up",',
  29977. ' up: 0,',
  29978. ' "1": "down",',
  29979. ' down: 1',
  29980. '};',
  29981. 'this.$rtti.$Enum("TFlag", {',
  29982. ' minvalue: 0,',
  29983. ' maxvalue: 1,',
  29984. ' ordtype: 1,',
  29985. ' enumtype: this.TFlag',
  29986. '});',
  29987. 'this.$rtti.$Set("TFlags", {',
  29988. ' comptype: this.$rtti["TFlag"]',
  29989. '});',
  29990. 'this.ti = null;',
  29991. 'this.tiInt = null;',
  29992. 'this.tiEnum = null;',
  29993. 'this.tiSet = null;',
  29994. '']),
  29995. LinesToStr([ // $mod.$main
  29996. '$mod.ti = rtl.string;',
  29997. '$mod.ti = rtl.boolean;',
  29998. '$mod.ti = rtl.char;',
  29999. '$mod.ti = rtl.double;',
  30000. '$mod.tiInt = rtl.shortint;',
  30001. '$mod.tiInt = rtl.byte;',
  30002. '$mod.tiInt = rtl.smallint;',
  30003. '$mod.tiInt = rtl.word;',
  30004. '$mod.tiInt = rtl.longint;',
  30005. '$mod.tiInt = rtl.longword;',
  30006. '$mod.ti = rtl.jsvalue;',
  30007. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30008. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30009. '']));
  30010. end;
  30011. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30012. begin
  30013. WithTypeInfo:=true;
  30014. StartProgram(true,[supTypeInfo]);
  30015. Add('{$modeswitch externalclass}');
  30016. Add('type');
  30017. Add(' TStaticArr = array[boolean] of string;');
  30018. Add(' TDynArr = array of string;');
  30019. Add(' TProc = procedure;');
  30020. Add(' TMethod = procedure of object;');
  30021. Add('var');
  30022. Add(' StaticArray: TStaticArr;');
  30023. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30024. Add(' DynArray: TDynArr;');
  30025. Add(' tiDynArray: TTypeInfoDynArray;');
  30026. Add(' ProcVar: TProc;');
  30027. Add(' tiProcVar: TTypeInfoProcVar;');
  30028. Add(' MethodVar: TMethod;');
  30029. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30030. Add('begin');
  30031. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30032. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30033. Add(' tiDynArray:=typeinfo(DynArray);');
  30034. Add(' tiDynArray:=typeinfo(TDynArr);');
  30035. Add(' tiProcVar:=typeinfo(ProcVar);');
  30036. Add(' tiProcVar:=typeinfo(TProc);');
  30037. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30038. Add(' tiMethodVar:=typeinfo(TMethod);');
  30039. ConvertProgram;
  30040. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30041. LinesToStr([ // statements
  30042. 'this.$rtti.$StaticArray("TStaticArr", {',
  30043. ' dims: [2],',
  30044. ' eltype: rtl.string',
  30045. '});',
  30046. 'this.$rtti.$DynArray("TDynArr", {',
  30047. ' eltype: rtl.string',
  30048. '});',
  30049. 'this.$rtti.$ProcVar("TProc", {',
  30050. ' procsig: rtl.newTIProcSig(null)',
  30051. '});',
  30052. 'this.$rtti.$MethodVar("TMethod", {',
  30053. ' procsig: rtl.newTIProcSig(null),',
  30054. ' methodkind: 0',
  30055. '});',
  30056. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30057. 'this.tiStaticArray = null;',
  30058. 'this.DynArray = [];',
  30059. 'this.tiDynArray = null;',
  30060. 'this.ProcVar = null;',
  30061. 'this.tiProcVar = null;',
  30062. 'this.MethodVar = null;',
  30063. 'this.tiMethodVar = null;',
  30064. '']),
  30065. LinesToStr([ // $mod.$main
  30066. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30067. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30068. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30069. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30070. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30071. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30072. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30073. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30074. '']));
  30075. end;
  30076. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30077. begin
  30078. WithTypeInfo:=true;
  30079. StartProgram(true,[supTypeInfo]);
  30080. Add('{$modeswitch externalclass}');
  30081. Add('type');
  30082. Add(' TRec = record end;');
  30083. // ToDo: ^TRec
  30084. Add(' TObject = class end;');
  30085. Add(' TClass = class of tobject;');
  30086. Add('var');
  30087. Add(' Rec: trec;');
  30088. Add(' tiRecord: ttypeinforecord;');
  30089. Add(' Obj: tobject;');
  30090. Add(' tiClass: ttypeinfoclass;');
  30091. Add(' aClass: tclass;');
  30092. Add(' tiClassRef: ttypeinfoclassref;');
  30093. // ToDo: ^TRec
  30094. Add(' tiPointer: ttypeinfopointer;');
  30095. Add('begin');
  30096. Add(' tirecord:=typeinfo(trec);');
  30097. Add(' tirecord:=typeinfo(trec);');
  30098. Add(' ticlass:=typeinfo(obj);');
  30099. Add(' ticlass:=typeinfo(tobject);');
  30100. Add(' ticlass:=typeinfo(aclass);');
  30101. Add(' ticlassref:=typeinfo(tclass);');
  30102. ConvertProgram;
  30103. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30104. LinesToStr([ // statements
  30105. 'rtl.recNewT(this, "TRec", function () {',
  30106. ' this.$eq = function (b) {',
  30107. ' return true;',
  30108. ' };',
  30109. ' this.$assign = function (s) {',
  30110. ' return this;',
  30111. ' };',
  30112. ' $mod.$rtti.$Record("TRec", {});',
  30113. '});',
  30114. 'rtl.createClass(this, "TObject", null, function () {',
  30115. ' this.$init = function () {',
  30116. ' };',
  30117. ' this.$final = function () {',
  30118. ' };',
  30119. '});',
  30120. 'this.$rtti.$ClassRef("TClass", {',
  30121. ' instancetype: this.$rtti["TObject"]',
  30122. '});',
  30123. 'this.Rec = this.TRec.$new();',
  30124. 'this.tiRecord = null;',
  30125. 'this.Obj = null;',
  30126. 'this.tiClass = null;',
  30127. 'this.aClass = null;',
  30128. 'this.tiClassRef = null;',
  30129. 'this.tiPointer = null;',
  30130. '']),
  30131. LinesToStr([ // $mod.$main
  30132. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30133. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30134. '$mod.tiClass = $mod.Obj.$rtti;',
  30135. '$mod.tiClass = $mod.$rtti["TObject"];',
  30136. '$mod.tiClass = $mod.aClass.$rtti;',
  30137. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30138. '']));
  30139. end;
  30140. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30141. begin
  30142. WithTypeInfo:=true;
  30143. StartProgram(true,[supTypeInfo]);
  30144. Add([
  30145. '{$modeswitch externalclass}',
  30146. 'type',
  30147. ' TClass = class of tobject;',
  30148. ' TObject = class',
  30149. ' function MyClass: TClass;',
  30150. ' class function ClassType: TClass;',
  30151. ' end;',
  30152. 'function TObject.MyClass: TClass;',
  30153. 'var t: TTypeInfoClass;',
  30154. 'begin',
  30155. ' t:=TypeInfo(Self);',
  30156. ' t:=TypeInfo(Result);',
  30157. ' t:=TypeInfo(TObject);',
  30158. 'end;',
  30159. 'class function TObject.ClassType: TClass;',
  30160. 'var t: TTypeInfoClass;',
  30161. 'begin',
  30162. ' t:=TypeInfo(Self);',
  30163. ' t:=TypeInfo(Result);',
  30164. 'end;',
  30165. 'var',
  30166. ' Obj: TObject;',
  30167. ' t: TTypeInfoClass;',
  30168. 'begin',
  30169. ' t:=TypeInfo(TObject.ClassType);',
  30170. ' t:=TypeInfo(Obj.ClassType);',
  30171. ' t:=TypeInfo(Obj.MyClass);',
  30172. '']);
  30173. ConvertProgram;
  30174. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30175. LinesToStr([ // statements
  30176. 'this.$rtti.$Class("TObject");',
  30177. 'this.$rtti.$ClassRef("TClass", {',
  30178. ' instancetype: this.$rtti["TObject"]',
  30179. '});',
  30180. 'rtl.createClass(this, "TObject", null, function () {',
  30181. ' this.$init = function () {',
  30182. ' };',
  30183. ' this.$final = function () {',
  30184. ' };',
  30185. ' this.MyClass = function () {',
  30186. ' var Result = null;',
  30187. ' var t = null;',
  30188. ' t = this.$rtti;',
  30189. ' t = Result.$rtti;',
  30190. ' t = $mod.$rtti["TObject"];',
  30191. ' return Result;',
  30192. ' };',
  30193. ' this.ClassType = function () {',
  30194. ' var Result = null;',
  30195. ' var t = null;',
  30196. ' t = this.$rtti;',
  30197. ' t = Result.$rtti;',
  30198. ' return Result;',
  30199. ' };',
  30200. '});',
  30201. 'this.Obj = null;',
  30202. 'this.t = null;',
  30203. '']),
  30204. LinesToStr([ // $mod.$main
  30205. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30206. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30207. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30208. '']));
  30209. end;
  30210. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30211. begin
  30212. WithTypeInfo:=true;
  30213. AddModuleWithIntfImplSrc('typinfo.pas',
  30214. LinesToStr([
  30215. '{$modeswitch externalclass}',
  30216. 'type',
  30217. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30218. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30219. '']),
  30220. '');
  30221. AddModuleWithIntfImplSrc('unit2.pas',
  30222. LinesToStr([
  30223. 'uses typinfo;',
  30224. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30225. 'procedure DoPtr(p: PTypeInfo);',
  30226. 'procedure DoInfo(t: TTypeInfo);',
  30227. 'procedure DoInt(t: TTypeInfoInteger);',
  30228. '']),
  30229. LinesToStr([
  30230. 'procedure DoPtr(p: PTypeInfo);',
  30231. 'begin end;',
  30232. 'procedure DoInfo(t: TTypeInfo);',
  30233. 'begin end;',
  30234. 'procedure DoInt(t: TTypeInfoInteger);',
  30235. 'begin end;',
  30236. '']));
  30237. StartUnit(true);
  30238. Add([
  30239. 'interface',
  30240. 'uses unit2;', // does not use unit typinfo
  30241. 'implementation',
  30242. 'var',
  30243. ' i: byte;',
  30244. ' p: pointer;',
  30245. ' t: PTypeInfo;',
  30246. 'initialization',
  30247. ' p:=typeinfo(i);',
  30248. ' t:=typeinfo(i);',
  30249. ' if p=t then ;',
  30250. ' if p=typeinfo(i) then ;',
  30251. ' if typeinfo(i)=p then ;',
  30252. ' if t=typeinfo(i) then ;',
  30253. ' if typeinfo(i)=t then ;',
  30254. ' DoPtr(p);',
  30255. ' DoPtr(t);',
  30256. ' DoPtr(typeinfo(i));',
  30257. ' DoInfo(p);',
  30258. ' DoInfo(t);',
  30259. ' DoInfo(typeinfo(i));',
  30260. ' DoInt(typeinfo(i));',
  30261. '']);
  30262. ConvertUnit;
  30263. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30264. LinesToStr([ // statements
  30265. 'var $impl = $mod.$impl;',
  30266. '']),
  30267. LinesToStr([ // this.$init
  30268. '$impl.p = rtl.byte;',
  30269. '$impl.t = rtl.byte;',
  30270. 'if ($impl.p === $impl.t) ;',
  30271. 'if ($impl.p === rtl.byte) ;',
  30272. 'if (rtl.byte === $impl.p) ;',
  30273. 'if ($impl.t === rtl.byte) ;',
  30274. 'if (rtl.byte === $impl.t) ;',
  30275. 'pas.unit2.DoPtr($impl.p);',
  30276. 'pas.unit2.DoPtr($impl.t);',
  30277. 'pas.unit2.DoPtr(rtl.byte);',
  30278. 'pas.unit2.DoInfo($impl.p);',
  30279. 'pas.unit2.DoInfo($impl.t);',
  30280. 'pas.unit2.DoInfo(rtl.byte);',
  30281. 'pas.unit2.DoInt(rtl.byte);',
  30282. '']),
  30283. LinesToStr([ // implementation
  30284. '$impl.i = 0;',
  30285. '$impl.p = null;',
  30286. '$impl.t = null;',
  30287. '']) );
  30288. end;
  30289. procedure TTestModule.TestRTTI_Interface_Corba;
  30290. begin
  30291. WithTypeInfo:=true;
  30292. StartProgram(true,[supTypeInfo]);
  30293. Add([
  30294. '{$interfaces corba}',
  30295. '{$modeswitch externalclass}',
  30296. 'type',
  30297. ' IUnknown = interface',
  30298. ' end;',
  30299. ' IBird = interface',
  30300. ' function GetItem: longint;',
  30301. ' procedure SetItem(Value: longint);',
  30302. ' property Item: longint read GetItem write SetItem;',
  30303. ' end;',
  30304. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30305. 'var',
  30306. ' i: IBird;',
  30307. ' t: TTypeInfoInterface;',
  30308. 'begin',
  30309. ' t:=TypeInfo(IBird);',
  30310. ' t:=TypeInfo(i);',
  30311. ' DoIt(t);',
  30312. ' DoIt(TypeInfo(IBird));',
  30313. '']);
  30314. ConvertProgram;
  30315. CheckSource('TestRTTI_Interface_Corba',
  30316. LinesToStr([ // statements
  30317. 'rtl.createInterface(',
  30318. ' this,',
  30319. ' "IUnknown",',
  30320. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30321. ' [],',
  30322. ' null,',
  30323. ' function () {',
  30324. ' }',
  30325. ');',
  30326. 'rtl.createInterface(',
  30327. ' this,',
  30328. ' "IBird",',
  30329. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30330. ' ["GetItem", "SetItem"],',
  30331. ' null,',
  30332. ' function () {',
  30333. ' var $r = this.$rtti;',
  30334. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30335. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30336. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30337. ' }',
  30338. ');',
  30339. 'this.DoIt = function (t) {',
  30340. '}; ',
  30341. 'this.i = null;',
  30342. 'this.t = null;',
  30343. '']),
  30344. LinesToStr([ // $mod.$main
  30345. '$mod.t = $mod.$rtti["IBird"];',
  30346. '$mod.t = $mod.i.$rtti;',
  30347. '$mod.DoIt($mod.t);',
  30348. '$mod.DoIt($mod.$rtti["IBird"]);',
  30349. '']));
  30350. end;
  30351. procedure TTestModule.TestRTTI_Interface_COM;
  30352. begin
  30353. WithTypeInfo:=true;
  30354. StartProgram(true,[supTypeInfo]);
  30355. Add([
  30356. '{$interfaces com}',
  30357. '{$modeswitch externalclass}',
  30358. 'type',
  30359. ' TGuid = record end;',
  30360. ' integer = longint;',
  30361. ' IUnknown = interface',
  30362. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30363. ' function _AddRef: Integer;',
  30364. ' function _Release: Integer;',
  30365. ' end;',
  30366. ' IBird = interface',
  30367. ' function GetItem: longint;',
  30368. ' procedure SetItem(Value: longint);',
  30369. ' property Item: longint read GetItem write SetItem;',
  30370. ' end;',
  30371. 'var',
  30372. ' i: IBird;',
  30373. ' t: TTypeInfoInterface;',
  30374. 'begin',
  30375. ' t:=TypeInfo(IBird);',
  30376. ' t:=TypeInfo(i);',
  30377. '']);
  30378. ConvertProgram;
  30379. CheckSource('TestRTTI_Interface_COM',
  30380. LinesToStr([ // statements
  30381. 'rtl.recNewT(this, "TGuid", function () {',
  30382. ' this.$eq = function (b) {',
  30383. ' return true;',
  30384. ' };',
  30385. ' this.$assign = function (s) {',
  30386. ' return this;',
  30387. ' };',
  30388. ' $mod.$rtti.$Record("TGuid", {});',
  30389. '});',
  30390. 'rtl.createInterface(',
  30391. ' this,',
  30392. ' "IUnknown",',
  30393. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30394. ' ["QueryInterface", "_AddRef", "_Release"],',
  30395. ' null,',
  30396. ' function () {',
  30397. ' this.$kind = "com";',
  30398. ' var $r = this.$rtti;',
  30399. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30400. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30401. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30402. ' }',
  30403. ');',
  30404. 'rtl.createInterface(',
  30405. ' this,',
  30406. ' "IBird",',
  30407. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30408. ' ["GetItem", "SetItem"],',
  30409. ' this.IUnknown,',
  30410. ' function () {',
  30411. ' var $r = this.$rtti;',
  30412. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30413. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30414. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30415. ' }',
  30416. ');',
  30417. 'this.i = null;',
  30418. 'this.t = null;',
  30419. '']),
  30420. LinesToStr([ // $mod.$main
  30421. '$mod.t = $mod.$rtti["IBird"];',
  30422. '$mod.t = $mod.i.$rtti;',
  30423. '']));
  30424. end;
  30425. procedure TTestModule.TestRTTI_ClassHelper;
  30426. begin
  30427. WithTypeInfo:=true;
  30428. StartProgram(true,[supTypeInfo]);
  30429. Add([
  30430. '{$interfaces com}',
  30431. '{$modeswitch externalclass}',
  30432. 'type',
  30433. ' TObject = class',
  30434. ' end;',
  30435. ' THelper = class helper for TObject',
  30436. ' published',
  30437. ' function GetItem: longint;',
  30438. ' property Item: longint read GetItem;',
  30439. ' end;',
  30440. 'function THelper.GetItem: longint;',
  30441. 'begin',
  30442. 'end;',
  30443. 'var',
  30444. ' t: TTypeInfoHelper;',
  30445. 'begin',
  30446. ' t:=TypeInfo(THelper);',
  30447. '']);
  30448. ConvertProgram;
  30449. CheckSource('TestRTTI_ClassHelper',
  30450. LinesToStr([ // statements
  30451. 'rtl.createClass(this, "TObject", null, function () {',
  30452. ' this.$init = function () {',
  30453. ' };',
  30454. ' this.$final = function () {',
  30455. ' };',
  30456. '});',
  30457. 'rtl.createHelper(this, "THelper", null, function () {',
  30458. ' this.GetItem = function () {',
  30459. ' var Result = 0;',
  30460. ' return Result;',
  30461. ' };',
  30462. ' var $r = this.$rtti;',
  30463. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30464. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30465. '});',
  30466. 'this.t = null;',
  30467. '']),
  30468. LinesToStr([ // $mod.$main
  30469. '$mod.t = $mod.$rtti["THelper"];',
  30470. '']));
  30471. end;
  30472. procedure TTestModule.TestRTTI_ExternalClass;
  30473. begin
  30474. WithTypeInfo:=true;
  30475. StartProgram(true,[supTypeInfo]);
  30476. Add([
  30477. '{$modeswitch externalclass}',
  30478. 'type',
  30479. ' TJSObject = class external name ''Object''',
  30480. ' end;',
  30481. ' TJSArray = class external name ''Array'' (TJSObject)',
  30482. ' end;',
  30483. 'var',
  30484. ' p: Pointer;',
  30485. ' tc: TTypeInfoExtClass;',
  30486. 'begin',
  30487. ' p:=typeinfo(TJSArray);']);
  30488. ConvertProgram;
  30489. CheckSource('TestRTTI_ExternalClass',
  30490. LinesToStr([ // statements
  30491. 'this.$rtti.$ExtClass("TJSObject", {',
  30492. ' jsclass: "Object"',
  30493. '});',
  30494. 'this.$rtti.$ExtClass("TJSArray", {',
  30495. ' ancestor: this.$rtti["TJSObject"],',
  30496. ' jsclass: "Array"',
  30497. '});',
  30498. 'this.p = null;',
  30499. 'this.tc = null;',
  30500. '']),
  30501. LinesToStr([ // $mod.$main
  30502. '$mod.p = $mod.$rtti["TJSArray"];',
  30503. '']));
  30504. end;
  30505. procedure TTestModule.TestRTTI_Unit;
  30506. begin
  30507. WithTypeInfo:=true;
  30508. AddModuleWithIntfImplSrc('unit2.pas',
  30509. LinesToStr([
  30510. '{$mode delphi}',
  30511. 'type',
  30512. ' TWordArray = array of word;',
  30513. ' TArray<T> = array of T;',
  30514. '']),
  30515. '');
  30516. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30517. Add([
  30518. '{$mode delphi}',
  30519. 'interface',
  30520. 'uses unit2;',
  30521. 'type',
  30522. ' IBird = interface',
  30523. ' function Swoop: TWordArray;',
  30524. ' function Glide: TArray<word>;',
  30525. ' end;',
  30526. 'procedure Fly;',
  30527. 'implementation',
  30528. 'procedure Fly;',
  30529. 'var',
  30530. ' ta: tTypeInfoDynArray;',
  30531. ' ti: tTypeInfoInterface;',
  30532. 'begin',
  30533. ' ta:=typeinfo(TWordArray);',
  30534. ' ta:=typeinfo(TArray<word>);',
  30535. ' ti:=typeinfo(IBird);',
  30536. 'end;',
  30537. '']);
  30538. ConvertUnit;
  30539. CheckSource('TestRTTI_ExternalClass',
  30540. LinesToStr([ // statements
  30541. 'rtl.createInterface(',
  30542. ' this,',
  30543. ' "IBird",',
  30544. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30545. ' ["Swoop", "Glide"],',
  30546. ' pas.system.IUnknown,',
  30547. ' function () {',
  30548. ' var $r = this.$rtti;',
  30549. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30550. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30551. ' }',
  30552. ');',
  30553. 'this.Fly = function () {',
  30554. ' var ta = null;',
  30555. ' var ti = null;',
  30556. ' ta = pas.unit2.$rtti["TWordArray"];',
  30557. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30558. ' ti = $mod.$rtti["IBird"];',
  30559. '};',
  30560. '']),
  30561. LinesToStr([ // $mod.$main
  30562. '']));
  30563. end;
  30564. procedure TTestModule.TestResourcestringProgram;
  30565. begin
  30566. AddModuleWithIntfImplSrc('unit2.pas',
  30567. LinesToStr([
  30568. 'resourcestring Title = ''Nice'';',
  30569. '']),
  30570. '');
  30571. StartProgram(true);
  30572. Add([
  30573. 'uses unit2;',
  30574. 'const Bar = ''bar'';',
  30575. 'resourcestring',
  30576. ' Red = ''red'';',
  30577. ' Foobar = ''fOo''+bar;',
  30578. 'var s: string;',
  30579. ' c: char;',
  30580. 'begin',
  30581. ' s:=red;',
  30582. ' s:=test1.red;',
  30583. ' s:=Title;',
  30584. ' c:=red[1];',
  30585. ' c:=test1.red[2];',
  30586. ' if red=foobar then ;',
  30587. ' if red[3]=red[4] then ;']);
  30588. ConvertProgram;
  30589. CheckSource('TestResourcestringProgram',
  30590. LinesToStr([ // statements
  30591. 'this.Bar = "bar";',
  30592. 'this.s = "";',
  30593. 'this.c = "";',
  30594. '$mod.$resourcestrings = {',
  30595. ' Red: {',
  30596. ' org: "red"',
  30597. ' },',
  30598. ' Foobar: {',
  30599. ' org: "fOobar"',
  30600. ' }',
  30601. '};',
  30602. '']),
  30603. LinesToStr([ // $mod.$main
  30604. '$mod.s = rtl.getResStr($mod, "Red");',
  30605. '$mod.s = rtl.getResStr($mod, "Red");',
  30606. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30607. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30608. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30609. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30610. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30611. '']));
  30612. end;
  30613. procedure TTestModule.TestResourcestringUnit;
  30614. begin
  30615. AddModuleWithIntfImplSrc('unit2.pas',
  30616. LinesToStr([
  30617. 'resourcestring Title = ''Nice'';',
  30618. '']),
  30619. '');
  30620. StartUnit(true);
  30621. Add([
  30622. 'interface',
  30623. 'uses unit2;',
  30624. 'const Red = ''rEd'';',
  30625. 'resourcestring',
  30626. ' Blue = ''blue'';',
  30627. ' NotRed = ''not''+Red;',
  30628. 'var s: string;',
  30629. 'implementation',
  30630. 'resourcestring',
  30631. ' ImplGreen = ''green'';',
  30632. 'initialization',
  30633. ' s:=blue+ImplGreen;',
  30634. ' s:=test1.blue+test1.implgreen;',
  30635. ' s:=blue[1]+implgreen[2];',
  30636. ' s:=Title;',
  30637. '']);
  30638. ConvertUnit;
  30639. CheckSource('TestResourcestringUnit',
  30640. LinesToStr([ // statements
  30641. 'this.Red = "rEd";',
  30642. 'this.s = "";',
  30643. '$mod.$resourcestrings = {',
  30644. ' Blue: {',
  30645. ' org: "blue"',
  30646. ' },',
  30647. ' NotRed: {',
  30648. ' org: "notrEd"',
  30649. ' },',
  30650. ' ImplGreen: {',
  30651. ' org: "green"',
  30652. ' }',
  30653. '};',
  30654. '']),
  30655. LinesToStr([ // $mod.$main
  30656. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30657. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30658. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30659. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30660. '']));
  30661. end;
  30662. procedure TTestModule.TestResourcestringImplementation;
  30663. begin
  30664. StartUnit(false);
  30665. Add([
  30666. 'interface',
  30667. 'implementation',
  30668. 'resourcestring',
  30669. ' ImplRed = ''red'';']);
  30670. ConvertUnit;
  30671. CheckSource('TestResourcestringImplementation',
  30672. LinesToStr([ // intf statements
  30673. 'var $impl = $mod.$impl;']),
  30674. LinesToStr([ // $mod.$init
  30675. '']),
  30676. LinesToStr([ // impl statements
  30677. '$mod.$resourcestrings = {',
  30678. ' ImplRed: {',
  30679. ' org: "red"',
  30680. ' }',
  30681. '};',
  30682. '']));
  30683. end;
  30684. procedure TTestModule.TestAttributes_Members;
  30685. begin
  30686. WithTypeInfo:=true;
  30687. StartProgram(false);
  30688. Add([
  30689. '{$modeswitch PrefixedAttributes}',
  30690. 'type',
  30691. ' TObject = class',
  30692. ' constructor Create;',
  30693. ' end;',
  30694. ' TCustomAttribute = class',
  30695. ' constructor Create(Id: word);',
  30696. ' end;',
  30697. ' [Missing]',
  30698. ' TBird = class',
  30699. ' published',
  30700. ' [Tcustom]',
  30701. ' FField: word;',
  30702. ' [tcustom(14)]',
  30703. ' property Size: word read FField;',
  30704. ' [Tcustom(15)]',
  30705. ' procedure Fly; virtual; abstract;',
  30706. ' end;',
  30707. ' TRec = record',
  30708. ' [Tcustom,tcustom(14)]',
  30709. ' Size: word;',
  30710. ' end;',
  30711. 'constructor TObject.Create; begin end;',
  30712. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30713. 'begin',
  30714. '']);
  30715. ConvertProgram;
  30716. CheckSource('TestAttributes_Members',
  30717. LinesToStr([ // statements
  30718. 'rtl.createClass(this, "TObject", null, function () {',
  30719. ' this.$init = function () {',
  30720. ' };',
  30721. ' this.$final = function () {',
  30722. ' };',
  30723. ' this.Create = function () {',
  30724. ' return this;',
  30725. ' };',
  30726. '});',
  30727. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30728. ' this.Create$1 = function (Id) {',
  30729. ' return this;',
  30730. ' };',
  30731. '});',
  30732. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30733. ' this.$init = function () {',
  30734. ' $mod.TObject.$init.call(this);',
  30735. ' this.FField = 0;',
  30736. ' };',
  30737. ' var $r = this.$rtti;',
  30738. ' $r.addField("FField", rtl.word, {',
  30739. ' attr: [$mod.TCustomAttribute, "Create"]',
  30740. ' });',
  30741. ' $r.addProperty(',
  30742. ' "Size",',
  30743. ' 0,',
  30744. ' rtl.word,',
  30745. ' "FField",',
  30746. ' "",',
  30747. ' {',
  30748. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30749. ' }',
  30750. ' );',
  30751. ' $r.addMethod("Fly", 0, null, null, {',
  30752. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30753. ' });',
  30754. '});',
  30755. 'rtl.recNewT(this, "TRec", function () {',
  30756. ' this.Size = 0;',
  30757. ' this.$eq = function (b) {',
  30758. ' return this.Size === b.Size;',
  30759. ' };',
  30760. ' this.$assign = function (s) {',
  30761. ' this.Size = s.Size;',
  30762. ' return this;',
  30763. ' };',
  30764. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30765. ' $r.addField("Size", rtl.word, {',
  30766. ' attr: [',
  30767. ' $mod.TCustomAttribute,',
  30768. ' "Create",',
  30769. ' $mod.TCustomAttribute,',
  30770. ' "Create$1",',
  30771. ' [14]',
  30772. ' ]',
  30773. ' });',
  30774. '});',
  30775. '']),
  30776. LinesToStr([ // $mod.$main
  30777. '']));
  30778. end;
  30779. procedure TTestModule.TestAttributes_Types;
  30780. begin
  30781. WithTypeInfo:=true;
  30782. StartProgram(false);
  30783. Add([
  30784. '{$modeswitch PrefixedAttributes}',
  30785. 'type',
  30786. ' TObject = class',
  30787. ' constructor Create(Id: word);',
  30788. ' end;',
  30789. ' TCustomAttribute = class',
  30790. ' end;',
  30791. ' [TCustom(1)]',
  30792. ' TMyClass = class',
  30793. ' end;',
  30794. ' [TCustom(2)]',
  30795. ' TRec = record',
  30796. ' end;',
  30797. ' [TCustom(3)]',
  30798. ' TInt = type word;',
  30799. 'constructor TObject.Create(Id: word);',
  30800. 'begin',
  30801. 'end;',
  30802. 'var p: pointer;',
  30803. 'begin',
  30804. ' p:=typeinfo(TMyClass);',
  30805. ' p:=typeinfo(TRec);',
  30806. ' p:=typeinfo(TInt);',
  30807. '']);
  30808. ConvertProgram;
  30809. CheckSource('TestAttributes_Types',
  30810. LinesToStr([ // statements
  30811. 'rtl.createClass(this, "TObject", null, function () {',
  30812. ' this.$init = function () {',
  30813. ' };',
  30814. ' this.$final = function () {',
  30815. ' };',
  30816. ' this.Create = function (Id) {',
  30817. ' return this;',
  30818. ' };',
  30819. '});',
  30820. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30821. '});',
  30822. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  30823. ' var $r = this.$rtti;',
  30824. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30825. '});',
  30826. 'rtl.recNewT(this, "TRec", function () {',
  30827. ' this.$eq = function (b) {',
  30828. ' return true;',
  30829. ' };',
  30830. ' this.$assign = function (s) {',
  30831. ' return this;',
  30832. ' };',
  30833. ' $mod.$rtti.$Record("TRec", {',
  30834. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30835. ' });',
  30836. '});',
  30837. 'this.$rtti.$inherited("TInt", rtl.word, {',
  30838. ' attr: [this.TCustomAttribute, "Create", [3]]',
  30839. '});',
  30840. 'this.p = null;',
  30841. '']),
  30842. LinesToStr([ // $mod.$main
  30843. '$mod.p = $mod.$rtti["TMyClass"];',
  30844. '$mod.p = $mod.$rtti["TRec"];',
  30845. '$mod.p = $mod.$rtti["TInt"];',
  30846. '']));
  30847. end;
  30848. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30849. begin
  30850. WithTypeInfo:=true;
  30851. StartProgram(false);
  30852. Add([
  30853. '{$modeswitch PrefixedAttributes}',
  30854. 'type',
  30855. ' TObject = class',
  30856. ' constructor Create;',
  30857. ' end;',
  30858. ' TCustomAttribute = class',
  30859. ' end;',
  30860. ' THelper = class helper for TCustomAttribute',
  30861. ' constructor Create(Id: word);',
  30862. ' end;',
  30863. ' [TCustom(3)]',
  30864. ' TMyInt = word;',
  30865. 'constructor TObject.Create; begin end;',
  30866. 'constructor THelper.Create(Id: word); begin end;',
  30867. 'begin',
  30868. ' if typeinfo(TMyInt)=nil then ;']);
  30869. ConvertProgram;
  30870. end;
  30871. procedure TTestModule.TestAssert;
  30872. begin
  30873. StartProgram(false);
  30874. Add([
  30875. 'procedure DoIt;',
  30876. 'var',
  30877. ' b: boolean;',
  30878. ' s: string;',
  30879. 'begin',
  30880. ' {$Assertions on}',
  30881. ' Assert(b);',
  30882. 'end;',
  30883. 'begin',
  30884. ' DoIt;',
  30885. '']);
  30886. ConvertProgram;
  30887. CheckSource('TestAssert',
  30888. LinesToStr([ // statements
  30889. 'this.DoIt = function () {',
  30890. ' var b = false;',
  30891. ' var s = "";',
  30892. ' if (!b) throw "assert failed";',
  30893. '};',
  30894. '']),
  30895. LinesToStr([ // $mod.$main
  30896. '$mod.DoIt();',
  30897. '']));
  30898. end;
  30899. procedure TTestModule.TestAssert_SysUtils;
  30900. begin
  30901. AddModuleWithIntfImplSrc('SysUtils.pas',
  30902. LinesToStr([
  30903. 'type',
  30904. ' TObject = class',
  30905. ' constructor Create;',
  30906. ' end;',
  30907. ' EAssertionFailed = class',
  30908. ' constructor Create(s: string);',
  30909. ' end;',
  30910. '']),
  30911. LinesToStr([
  30912. 'constructor TObject.Create;',
  30913. 'begin end;',
  30914. 'constructor EAssertionFailed.Create(s: string);',
  30915. 'begin end;',
  30916. '']) );
  30917. StartProgram(true);
  30918. Add([
  30919. 'uses sysutils;',
  30920. 'procedure DoIt;',
  30921. 'var',
  30922. ' b: boolean;',
  30923. ' s: string;',
  30924. 'begin',
  30925. ' {$Assertions on}',
  30926. ' Assert(b);',
  30927. ' Assert(b,''msg'');',
  30928. 'end;',
  30929. 'begin',
  30930. ' DoIt;',
  30931. '']);
  30932. ConvertProgram;
  30933. CheckSource('TestAssert_SysUtils',
  30934. LinesToStr([ // statements
  30935. 'this.DoIt = function () {',
  30936. ' var b = false;',
  30937. ' var s = "";',
  30938. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30939. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30940. '};',
  30941. '']),
  30942. LinesToStr([ // $mod.$main
  30943. '$mod.DoIt();',
  30944. '']));
  30945. end;
  30946. procedure TTestModule.TestObjectChecks;
  30947. begin
  30948. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30949. StartProgram(false);
  30950. Add([
  30951. 'type',
  30952. ' TObject = class',
  30953. ' procedure DoIt;',
  30954. ' end;',
  30955. ' TClass = class of tobject;',
  30956. ' TBird = class',
  30957. ' end;',
  30958. ' TBirdClass = class of TBird;',
  30959. 'var',
  30960. ' o : TObject;',
  30961. ' c: TClass;',
  30962. ' b: TBird;',
  30963. ' bc: TBirdClass;',
  30964. 'procedure TObject.DoIt;',
  30965. 'begin',
  30966. ' b:=TBird(o);',
  30967. 'end;',
  30968. 'begin',
  30969. ' o.DoIt;',
  30970. ' b:=TBird(o);',
  30971. ' bc:=TBirdClass(c);',
  30972. '']);
  30973. ConvertProgram;
  30974. CheckSource('TestCheckMethodCall',
  30975. LinesToStr([ // statements
  30976. 'rtl.createClass(this, "TObject", null, function () {',
  30977. ' this.$init = function () {',
  30978. ' };',
  30979. ' this.$final = function () {',
  30980. ' };',
  30981. ' this.DoIt = function () {',
  30982. ' rtl.checkMethodCall(this,$mod.TObject);',
  30983. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30984. ' };',
  30985. '});',
  30986. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30987. '});',
  30988. 'this.o = null;',
  30989. 'this.c = null;',
  30990. 'this.b = null;',
  30991. 'this.bc = null;',
  30992. '']),
  30993. LinesToStr([ // $mod.$main
  30994. '$mod.o.DoIt();',
  30995. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30996. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30997. '']));
  30998. end;
  30999. procedure TTestModule.TestOverflowChecks_Int;
  31000. begin
  31001. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31002. StartProgram(false);
  31003. Add([
  31004. 'procedure DoIt;',
  31005. 'var',
  31006. ' b: byte;',
  31007. ' n: nativeint;',
  31008. ' u: nativeuint;',
  31009. ' c: currency;',
  31010. 'begin',
  31011. ' n:=n+n;',
  31012. ' n:=n-n;',
  31013. ' n:=n+b;',
  31014. ' n:=b-n;',
  31015. ' n:=n*n;',
  31016. ' n:=n*u;',
  31017. ' c:=c+b;',
  31018. ' c:=b+c;',
  31019. ' c:=c*b;',
  31020. ' c:=b*c;',
  31021. 'end;',
  31022. 'begin',
  31023. '']);
  31024. ConvertProgram;
  31025. CheckSource('TestOverflowChecks_Int',
  31026. LinesToStr([ // statements
  31027. 'this.DoIt = function () {',
  31028. ' var b = 0;',
  31029. ' var n = 0;',
  31030. ' var u = 0;',
  31031. ' var c = 0;',
  31032. ' n = rtl.oc(n + n);',
  31033. ' n = rtl.oc(n - n);',
  31034. ' n = rtl.oc(n + b);',
  31035. ' n = rtl.oc(b - n);',
  31036. ' n = rtl.oc(n * n);',
  31037. ' n = rtl.oc(n * u);',
  31038. ' c = rtl.oc(c + (b * 10000));',
  31039. ' c = rtl.oc((b * 10000) + c);',
  31040. ' c = rtl.oc(c * b);',
  31041. ' c = rtl.oc(b * c);',
  31042. '};',
  31043. '']),
  31044. LinesToStr([ // $mod.$main
  31045. '']));
  31046. end;
  31047. procedure TTestModule.TestRangeChecks_AssignInt;
  31048. begin
  31049. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31050. StartProgram(false);
  31051. Add([
  31052. '{$R+}',
  31053. 'var',
  31054. ' b: byte = 2;',
  31055. ' w: word = 3;',
  31056. 'procedure DoIt(p: byte);',
  31057. 'begin',
  31058. ' b:=w;',
  31059. ' b+=w;',
  31060. ' b:=1;',
  31061. 'end;',
  31062. '{$R-}',
  31063. 'procedure DoSome;',
  31064. 'begin',
  31065. ' DoIt(w);',
  31066. ' b:=w;',
  31067. ' b:=2;',
  31068. 'end;',
  31069. 'begin',
  31070. '{$R+}',
  31071. '']);
  31072. ConvertProgram;
  31073. CheckSource('TestRangeChecks_AssignInt',
  31074. LinesToStr([ // statements
  31075. 'this.b = 2;',
  31076. 'this.w = 3;',
  31077. 'this.DoIt = function (p) {',
  31078. ' rtl.rc(p, 0, 255);',
  31079. ' $mod.b = rtl.rc($mod.w,0,255);',
  31080. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31081. ' $mod.b = 1;',
  31082. '};',
  31083. 'this.DoSome = function () {',
  31084. ' $mod.DoIt($mod.w);',
  31085. ' $mod.b = $mod.w;',
  31086. ' $mod.b = 2;',
  31087. '};',
  31088. '']),
  31089. LinesToStr([ // $mod.$main
  31090. '']));
  31091. end;
  31092. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31093. begin
  31094. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31095. StartProgram(false);
  31096. Add([
  31097. '{$R+}',
  31098. 'type Ten = 1..10;',
  31099. 'var',
  31100. ' b: Ten = 2;',
  31101. ' w: Ten = 3;',
  31102. 'procedure DoIt(p: Ten);',
  31103. 'begin',
  31104. ' b:=w;',
  31105. ' b+=w;',
  31106. ' b:=1;',
  31107. 'end;',
  31108. '{$R-}',
  31109. 'procedure DoSome;',
  31110. 'begin',
  31111. ' DoIt(w);',
  31112. ' b:=w;',
  31113. ' b:=2;',
  31114. 'end;',
  31115. 'begin',
  31116. '{$R+}',
  31117. '']);
  31118. ConvertProgram;
  31119. CheckSource('TestRangeChecks_AssignIntRange',
  31120. LinesToStr([ // statements
  31121. 'this.b = 2;',
  31122. 'this.w = 3;',
  31123. 'this.DoIt = function (p) {',
  31124. ' rtl.rc(p, 1, 10);',
  31125. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31126. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31127. ' $mod.b = 1;',
  31128. '};',
  31129. 'this.DoSome = function () {',
  31130. ' $mod.DoIt($mod.w);',
  31131. ' $mod.b = $mod.w;',
  31132. ' $mod.b = 2;',
  31133. '};',
  31134. '']),
  31135. LinesToStr([ // $mod.$main
  31136. '']));
  31137. end;
  31138. procedure TTestModule.TestRangeChecks_AssignEnum;
  31139. begin
  31140. StartProgram(false);
  31141. Add([
  31142. '{$R+}',
  31143. 'type TEnum = (red,green);',
  31144. 'var',
  31145. ' e: TEnum = red;',
  31146. 'procedure DoIt(p: TEnum);',
  31147. 'begin',
  31148. ' e:=p;',
  31149. ' p:=TEnum(0);',
  31150. ' p:=succ(e);',
  31151. 'end;',
  31152. '{$R-}',
  31153. 'procedure DoSome;',
  31154. 'begin',
  31155. ' DoIt(e);',
  31156. ' e:=TEnum(1);',
  31157. ' e:=pred(e);',
  31158. 'end;',
  31159. 'begin',
  31160. '{$R+}',
  31161. '']);
  31162. ConvertProgram;
  31163. CheckSource('TestRangeChecks_AssignEnum',
  31164. LinesToStr([ // statements
  31165. 'this.TEnum = {',
  31166. ' "0": "red",',
  31167. ' red: 0,',
  31168. ' "1": "green",',
  31169. ' green: 1',
  31170. '};',
  31171. 'this.e = this.TEnum.red;',
  31172. 'this.DoIt = function (p) {',
  31173. ' rtl.rc(p, 0, 1);',
  31174. ' $mod.e = rtl.rc(p, 0, 1);',
  31175. ' p = 0;',
  31176. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31177. '};',
  31178. 'this.DoSome = function () {',
  31179. ' $mod.DoIt($mod.e);',
  31180. ' $mod.e = 1;',
  31181. ' $mod.e = $mod.e - 1;',
  31182. '};',
  31183. '']),
  31184. LinesToStr([ // $mod.$main
  31185. '']));
  31186. end;
  31187. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31188. begin
  31189. StartProgram(false);
  31190. Add([
  31191. '{$R+}',
  31192. 'type',
  31193. ' TEnum = (red,green);',
  31194. ' TEnumRg = red..green;',
  31195. 'var',
  31196. ' e: TEnumRg = red;',
  31197. 'procedure DoIt(p: TEnumRg);',
  31198. 'begin',
  31199. ' e:=p;',
  31200. ' p:=TEnumRg(0);',
  31201. ' p:=succ(e);',
  31202. 'end;',
  31203. '{$R-}',
  31204. 'procedure DoSome;',
  31205. 'begin',
  31206. ' DoIt(e);',
  31207. ' e:=TEnum(1);',
  31208. ' e:=pred(e);',
  31209. 'end;',
  31210. 'begin',
  31211. '{$R+}',
  31212. '']);
  31213. ConvertProgram;
  31214. CheckSource('TestRangeChecks_AssignEnumRange',
  31215. LinesToStr([ // statements
  31216. 'this.TEnum = {',
  31217. ' "0": "red",',
  31218. ' red: 0,',
  31219. ' "1": "green",',
  31220. ' green: 1',
  31221. '};',
  31222. 'this.e = this.TEnum.red;',
  31223. 'this.DoIt = function (p) {',
  31224. ' rtl.rc(p, 0, 1);',
  31225. ' $mod.e = rtl.rc(p, 0, 1);',
  31226. ' p = 0;',
  31227. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31228. '};',
  31229. 'this.DoSome = function () {',
  31230. ' $mod.DoIt($mod.e);',
  31231. ' $mod.e = 1;',
  31232. ' $mod.e = $mod.e - 1;',
  31233. '};',
  31234. '']),
  31235. LinesToStr([ // $mod.$main
  31236. '']));
  31237. end;
  31238. procedure TTestModule.TestRangeChecks_AssignChar;
  31239. begin
  31240. StartProgram(false);
  31241. Add([
  31242. '{$R+}',
  31243. 'type',
  31244. ' TLetter = char;',
  31245. 'var',
  31246. ' b: TLetter = ''2'';',
  31247. ' w: TLetter = ''3'';',
  31248. 'procedure DoIt(p: TLetter);',
  31249. 'begin',
  31250. ' b:=w;',
  31251. ' b:=''1'';',
  31252. 'end;',
  31253. '{$R-}',
  31254. 'procedure DoSome;',
  31255. 'begin',
  31256. ' DoIt(w);',
  31257. ' b:=w;',
  31258. ' b:=''2'';',
  31259. 'end;',
  31260. 'begin',
  31261. '{$R+}',
  31262. '']);
  31263. ConvertProgram;
  31264. CheckSource('TestRangeChecks_AssignChar',
  31265. LinesToStr([ // statements
  31266. 'this.b = "2";',
  31267. 'this.w = "3";',
  31268. 'this.DoIt = function (p) {',
  31269. ' rtl.rcc(p, 0, 65535);',
  31270. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31271. ' $mod.b = "1";',
  31272. '};',
  31273. 'this.DoSome = function () {',
  31274. ' $mod.DoIt($mod.w);',
  31275. ' $mod.b = $mod.w;',
  31276. ' $mod.b = "2";',
  31277. '};',
  31278. '']),
  31279. LinesToStr([ // $mod.$main
  31280. '']));
  31281. end;
  31282. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31283. begin
  31284. StartProgram(false);
  31285. Add([
  31286. '{$R+}',
  31287. 'type TDigit = ''0''..''9'';',
  31288. 'var',
  31289. ' b: TDigit = ''2'';',
  31290. ' w: TDigit = ''3'';',
  31291. 'procedure DoIt(p: TDigit);',
  31292. 'begin',
  31293. ' b:=w;',
  31294. ' b:=''1'';',
  31295. 'end;',
  31296. '{$R-}',
  31297. 'procedure DoSome;',
  31298. 'begin',
  31299. ' DoIt(w);',
  31300. ' b:=w;',
  31301. ' b:=''2'';',
  31302. 'end;',
  31303. 'begin',
  31304. '{$R+}',
  31305. '']);
  31306. ConvertProgram;
  31307. CheckSource('TestRangeChecks_AssignCharRange',
  31308. LinesToStr([ // statements
  31309. 'this.b = "2";',
  31310. 'this.w = "3";',
  31311. 'this.DoIt = function (p) {',
  31312. ' rtl.rcc(p, 48, 57);',
  31313. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31314. ' $mod.b = "1";',
  31315. '};',
  31316. 'this.DoSome = function () {',
  31317. ' $mod.DoIt($mod.w);',
  31318. ' $mod.b = $mod.w;',
  31319. ' $mod.b = "2";',
  31320. '};',
  31321. '']),
  31322. LinesToStr([ // $mod.$main
  31323. '']));
  31324. end;
  31325. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31326. begin
  31327. StartProgram(false);
  31328. Add([
  31329. '{$R+}',
  31330. 'type',
  31331. ' Ten = 1..10;',
  31332. ' TArr = array of Ten;',
  31333. ' TArrArr = array of TArr;',
  31334. ' TArrByte = array[byte] of Ten;',
  31335. ' TArrChar = array[''0''..''9''] of Ten;',
  31336. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31337. ' TObject = class',
  31338. ' A: TArr;',
  31339. ' end;',
  31340. 'procedure DoIt;',
  31341. 'var',
  31342. ' Arr: TArr;',
  31343. ' ArrArr: TArrArr;',
  31344. ' ArrByte: TArrByte;',
  31345. ' ArrChar: TArrChar;',
  31346. ' ArrByteChar: TArrByteChar;',
  31347. ' i: Ten;',
  31348. ' c: char;',
  31349. ' o: tobject;',
  31350. 'begin',
  31351. ' i:=Arr[1];',
  31352. ' i:=ArrByteChar[1,''2''];',
  31353. ' Arr[1]:=Arr[1];',
  31354. ' Arr[i]:=Arr[i];',
  31355. ' ArrByte[3]:=ArrByte[3];',
  31356. ' ArrByte[i]:=ArrByte[i];',
  31357. ' ArrChar[''5'']:=ArrChar[''5''];',
  31358. ' ArrChar[c]:=ArrChar[c];',
  31359. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31360. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31361. ' o.a[i]:=o.a[i];',
  31362. 'end;',
  31363. 'begin',
  31364. '']);
  31365. ConvertProgram;
  31366. CheckSource('TestRangeChecks_ArrayIndex',
  31367. LinesToStr([ // statements
  31368. 'rtl.createClass(this, "TObject", null, function () {',
  31369. ' this.$init = function () {',
  31370. ' this.A = [];',
  31371. ' };',
  31372. ' this.$final = function () {',
  31373. ' this.A = undefined;',
  31374. ' };',
  31375. '});',
  31376. 'this.DoIt = function () {',
  31377. ' var Arr = [];',
  31378. ' var ArrArr = [];',
  31379. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31380. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31381. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31382. ' var i = 0;',
  31383. ' var c = "";',
  31384. ' var o = null;',
  31385. ' i = rtl.rc(Arr[1], 1, 10);',
  31386. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31387. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31388. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31389. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31390. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31391. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31392. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31393. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31394. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31395. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31396. '};',
  31397. '']),
  31398. LinesToStr([ // $mod.$main
  31399. '']));
  31400. end;
  31401. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31402. begin
  31403. StartProgram(false);
  31404. Add([
  31405. '{$R+}',
  31406. 'type',
  31407. ' Ten = 1..10;',
  31408. ' TRec = record x: Ten end;',
  31409. ' TArr = array of TRec;',
  31410. ' TArrArr = array of TArr;',
  31411. ' TObject = class',
  31412. ' A: TArr;',
  31413. ' end;',
  31414. 'procedure DoIt;',
  31415. 'var',
  31416. ' Arr: TArr;',
  31417. ' ArrArr: TArrArr;',
  31418. ' i: Ten;',
  31419. ' o: tobject;',
  31420. 'begin',
  31421. ' Arr[1]:=Arr[1];',
  31422. ' Arr[i]:=Arr[i+1];',
  31423. ' o.a[i]:=o.a[i+2];',
  31424. 'end;',
  31425. 'begin',
  31426. '']);
  31427. ConvertProgram;
  31428. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31429. LinesToStr([ // statements
  31430. 'rtl.recNewT(this, "TRec", function () {',
  31431. ' this.x = 0;',
  31432. ' this.$eq = function (b) {',
  31433. ' return this.x === b.x;',
  31434. ' };',
  31435. ' this.$assign = function (s) {',
  31436. ' this.x = s.x;',
  31437. ' return this;',
  31438. ' };',
  31439. '});',
  31440. 'rtl.createClass(this, "TObject", null, function () {',
  31441. ' this.$init = function () {',
  31442. ' this.A = [];',
  31443. ' };',
  31444. ' this.$final = function () {',
  31445. ' this.A = undefined;',
  31446. ' };',
  31447. '});',
  31448. 'this.DoIt = function () {',
  31449. ' var Arr = [];',
  31450. ' var ArrArr = [];',
  31451. ' var i = 0;',
  31452. ' var o = null;',
  31453. ' Arr[1].$assign(Arr[1]);',
  31454. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31455. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31456. '};',
  31457. '']),
  31458. LinesToStr([ // $mod.$main
  31459. '']));
  31460. end;
  31461. procedure TTestModule.TestRangeChecks_StringIndex;
  31462. begin
  31463. StartProgram(false);
  31464. Add([
  31465. 'type',
  31466. ' TObject = class',
  31467. ' S: string;',
  31468. ' end;',
  31469. '{$R+}',
  31470. 'procedure DoIt(var h: string);',
  31471. 'var',
  31472. ' s: string;',
  31473. ' i: longint;',
  31474. ' c: char;',
  31475. ' o: tobject;',
  31476. 'begin',
  31477. ' c:=s[1];',
  31478. ' s[i]:=s[i];',
  31479. ' h[i]:=h[i];',
  31480. ' c:=o.s[i];',
  31481. ' o.s[i]:=c;',
  31482. 'end;',
  31483. 'begin',
  31484. '']);
  31485. ConvertProgram;
  31486. CheckSource('TestRangeChecks_StringIndex',
  31487. LinesToStr([ // statements
  31488. 'rtl.createClass(this, "TObject", null, function () {',
  31489. ' this.$init = function () {',
  31490. ' this.S = "";',
  31491. ' };',
  31492. ' this.$final = function () {',
  31493. ' };',
  31494. '});',
  31495. 'this.DoIt = function (h) {',
  31496. ' var s = "";',
  31497. ' var i = 0;',
  31498. ' var c = "";',
  31499. ' var o = null;',
  31500. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31501. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31502. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31503. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31504. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31505. '};',
  31506. '']),
  31507. LinesToStr([ // $mod.$main
  31508. '']));
  31509. end;
  31510. procedure TTestModule.TestRangeChecks_TypecastInt;
  31511. begin
  31512. StartProgram(false);
  31513. Add([
  31514. '{$R+}',
  31515. 'var',
  31516. ' i: nativeint;',
  31517. ' b: byte;',
  31518. ' sh: shortint;',
  31519. ' w: word;',
  31520. ' sm: smallint;',
  31521. ' lw: longword;',
  31522. ' li: longint;',
  31523. 'begin',
  31524. ' b:=12+byte(i);',
  31525. ' sh:=12+shortint(i);',
  31526. ' w:=12+word(i);',
  31527. ' sm:=12+smallint(i);',
  31528. ' lw:=12+longword(i);',
  31529. ' li:=12+longint(i);',
  31530. '']);
  31531. ConvertProgram;
  31532. CheckSource('TestRangeChecks_TypecastInt',
  31533. LinesToStr([
  31534. 'this.i = 0;',
  31535. 'this.b = 0;',
  31536. 'this.sh = 0;',
  31537. 'this.w = 0;',
  31538. 'this.sm = 0;',
  31539. 'this.lw = 0;',
  31540. 'this.li = 0;',
  31541. '']),
  31542. LinesToStr([
  31543. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31544. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31545. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31546. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31547. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31548. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31549. '']));
  31550. end;
  31551. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31552. begin
  31553. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31554. StartProgram(false);
  31555. Add([
  31556. '{$modeswitch typehelpers}',
  31557. '{$R+}',
  31558. 'type',
  31559. ' TObject = class',
  31560. ' FSize: byte;',
  31561. ' property Size: byte read FSize;',
  31562. ' end;',
  31563. ' THelper = type helper for byte',
  31564. ' procedure SetIt(w: word);',
  31565. ' end;',
  31566. 'procedure THelper.SetIt(w: word);',
  31567. 'begin',
  31568. ' Self:=w;',
  31569. 'end;',
  31570. 'function GetIt: byte;',
  31571. 'begin',
  31572. ' Result.SetIt(2);',
  31573. 'end;',
  31574. 'var',
  31575. ' b: byte = 3;',
  31576. ' o: TObject;',
  31577. 'begin',
  31578. ' b.SetIt(14);',
  31579. ' with b do SetIt(15);',
  31580. ' o.Size.SetIt(16);',
  31581. '']);
  31582. ConvertProgram;
  31583. CheckSource('TestRangeChecks_AssignInt',
  31584. LinesToStr([ // statements
  31585. 'rtl.createClass(this, "TObject", null, function () {',
  31586. ' this.$init = function () {',
  31587. ' this.FSize = 0;',
  31588. ' };',
  31589. ' this.$final = function () {',
  31590. ' };',
  31591. '});',
  31592. 'rtl.createHelper(this, "THelper", null, function () {',
  31593. ' this.SetIt = function (w) {',
  31594. ' rtl.rc(w, 0, 65535);',
  31595. ' this.set(w);',
  31596. ' };',
  31597. '});',
  31598. 'this.GetIt = function () {',
  31599. ' var Result = 0;',
  31600. ' $mod.THelper.SetIt.call({',
  31601. ' get: function () {',
  31602. ' return Result;',
  31603. ' },',
  31604. ' set: function (v) {',
  31605. ' rtl.rc(v, 0, 255);',
  31606. ' Result = v;',
  31607. ' }',
  31608. ' }, 2);',
  31609. ' return Result;',
  31610. '};',
  31611. 'this.b = 3;',
  31612. 'this.o = null;',
  31613. '']),
  31614. LinesToStr([ // $mod.$main
  31615. '$mod.THelper.SetIt.call({',
  31616. ' p: $mod,',
  31617. ' get: function () {',
  31618. ' return this.p.b;',
  31619. ' },',
  31620. ' set: function (v) {',
  31621. ' rtl.rc(v, 0, 255);',
  31622. ' this.p.b = v;',
  31623. ' }',
  31624. '}, 14);',
  31625. 'var $with = $mod.b;',
  31626. '$mod.THelper.SetIt.call({',
  31627. ' get: function () {',
  31628. ' return $with;',
  31629. ' },',
  31630. ' set: function (v) {',
  31631. ' rtl.rc(v, 0, 255);',
  31632. ' $with = v;',
  31633. ' }',
  31634. '}, 15);',
  31635. '$mod.THelper.SetIt.call({',
  31636. ' p: $mod.o,',
  31637. ' get: function () {',
  31638. ' return this.p.FSize;',
  31639. ' },',
  31640. ' set: function (v) {',
  31641. ' rtl.rc(v, 0, 255);',
  31642. ' this.p.FSize = v;',
  31643. ' }',
  31644. '}, 16);',
  31645. '']));
  31646. end;
  31647. procedure TTestModule.TestAsync_Proc;
  31648. begin
  31649. StartProgram(false);
  31650. Add([
  31651. 'procedure Fly(w: word = 1); async; forward;',
  31652. 'procedure Run(w: word = 2); async;',
  31653. 'begin',
  31654. ' Fly(w);',
  31655. ' Fly;',
  31656. ' await(Fly(w));',
  31657. ' await(Fly);',
  31658. 'end;',
  31659. 'procedure Fly(w: word); ',
  31660. 'begin',
  31661. 'end;',
  31662. 'begin',
  31663. ' Run;',
  31664. ' Run(3);',
  31665. '']);
  31666. CheckResolverUnexpectedHints();
  31667. ConvertProgram;
  31668. CheckSource('TestAsync_Proc',
  31669. LinesToStr([ // statements
  31670. 'this.Run = async function (w) {',
  31671. ' $mod.Fly(w);',
  31672. ' $mod.Fly(1);',
  31673. ' await $mod.Fly(w);',
  31674. ' await $mod.Fly(1);',
  31675. '};',
  31676. 'this.Fly = async function (w) {',
  31677. '};',
  31678. '']),
  31679. LinesToStr([
  31680. '$mod.Run(2);',
  31681. '$mod.Run(3);',
  31682. '']));
  31683. end;
  31684. procedure TTestModule.TestAsync_CallResultIsPromise;
  31685. begin
  31686. StartProgram(false);
  31687. Add([
  31688. '{$modeswitch externalclass}',
  31689. 'type',
  31690. ' TObject = class',
  31691. ' end;',
  31692. ' TJSPromise = class external name ''Promise''',
  31693. ' end;',
  31694. ' TBird = class',
  31695. ' function Fly: word; async; ',
  31696. ' end;',
  31697. 'function TBird.Fly: word; async; ',
  31698. 'begin',
  31699. ' Result:=3;',
  31700. ' Fly:=4+Result;',
  31701. ' if Result=5 then ;',
  31702. ' exit(6);',
  31703. 'end;',
  31704. 'function Run: word; async;',
  31705. 'begin',
  31706. ' Result:=11+Result;',
  31707. ' inc(Result);',
  31708. 'end;',
  31709. 'var',
  31710. ' p: TJSPromise;',
  31711. ' o: TBird;',
  31712. 'begin',
  31713. ' p:=Run;',
  31714. ' p:=Run();',
  31715. ' if Run=p then ;',
  31716. ' if p=Run then ;',
  31717. ' if Run()=p then ;',
  31718. ' if p=Run() then ;',
  31719. ' p:=o.Fly;',
  31720. ' p:=o.Fly();',
  31721. ' if o.Fly=p then ;',
  31722. ' if o.Fly()=p then ;',
  31723. ' with o do begin',
  31724. ' p:=Fly;',
  31725. ' p:=Fly();',
  31726. ' if Fly=p then ;',
  31727. ' if Fly()=p then ;',
  31728. ' end;',
  31729. '']);
  31730. CheckResolverUnexpectedHints();
  31731. ConvertProgram;
  31732. CheckSource('TestAsync_CallResultIsPromise',
  31733. LinesToStr([ // statements
  31734. 'rtl.createClass(this, "TObject", null, function () {',
  31735. ' this.$init = function () {',
  31736. ' };',
  31737. ' this.$final = function () {',
  31738. ' };',
  31739. '});',
  31740. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31741. ' this.Fly = async function () {',
  31742. ' var Result = 0;',
  31743. ' Result = 3;',
  31744. ' Result = 4 + Result;',
  31745. ' if (Result === 5) ;',
  31746. ' return 6;',
  31747. ' return Result;',
  31748. ' };',
  31749. '});',
  31750. 'this.Run = async function () {',
  31751. ' var Result = 0;',
  31752. ' Result = 11 + Result;',
  31753. ' Result += 1;',
  31754. ' return Result;',
  31755. '};',
  31756. 'this.p = null;',
  31757. 'this.o = null;',
  31758. '']),
  31759. LinesToStr([
  31760. '$mod.p = $mod.Run();',
  31761. '$mod.p = $mod.Run();',
  31762. 'if ($mod.Run() === $mod.p) ;',
  31763. 'if ($mod.p === $mod.Run()) ;',
  31764. 'if ($mod.Run() === $mod.p) ;',
  31765. 'if ($mod.p === $mod.Run()) ;',
  31766. '$mod.p = $mod.o.Fly();',
  31767. '$mod.p = $mod.o.Fly();',
  31768. 'if ($mod.o.Fly() === $mod.p) ;',
  31769. 'if ($mod.o.Fly() === $mod.p) ;',
  31770. 'var $with = $mod.o;',
  31771. '$mod.p = $with.Fly();',
  31772. '$mod.p = $with.Fly();',
  31773. 'if ($with.Fly() === $mod.p) ;',
  31774. 'if ($with.Fly() === $mod.p) ;',
  31775. '']));
  31776. end;
  31777. procedure TTestModule.TestAsync_ConstructorFail;
  31778. begin
  31779. StartProgram(false);
  31780. Add([
  31781. 'type',
  31782. ' TObject = class',
  31783. ' end;',
  31784. ' TBird = class',
  31785. ' constructor Create; async;',
  31786. ' end;',
  31787. 'constructor TBird.Create; async;',
  31788. 'begin',
  31789. 'end;',
  31790. 'begin',
  31791. '']);
  31792. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31793. ConvertProgram;
  31794. end;
  31795. procedure TTestModule.TestAsync_PropertyGetterFail;
  31796. begin
  31797. StartProgram(false);
  31798. Add([
  31799. 'type',
  31800. ' TObject = class',
  31801. ' end;',
  31802. ' TBird = class',
  31803. ' function GetSize: word; async;',
  31804. ' property Size: word read GetSize;',
  31805. ' end;',
  31806. 'function TBird.GetSize: word; async;',
  31807. 'begin',
  31808. 'end;',
  31809. 'begin',
  31810. '']);
  31811. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31812. ConvertProgram;
  31813. end;
  31814. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31815. begin
  31816. StartProgram(false);
  31817. Add([
  31818. 'procedure Run; async;',
  31819. 'begin',
  31820. ' await(word,1);',
  31821. 'end;',
  31822. 'begin',
  31823. '']);
  31824. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31825. ConvertProgram;
  31826. end;
  31827. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  31828. begin
  31829. StartProgram(false);
  31830. Add([
  31831. 'type',
  31832. ' TObject = class',
  31833. ' end;',
  31834. ' TBird = class',
  31835. ' end;',
  31836. 'function Fly: TObject; async;',
  31837. 'begin',
  31838. 'end;',
  31839. 'procedure Run; async;',
  31840. 'begin',
  31841. ' await(TBird,Fly);',
  31842. 'end;',
  31843. 'begin',
  31844. '']);
  31845. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  31846. ConvertProgram;
  31847. end;
  31848. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31849. begin
  31850. StartProgram(false);
  31851. Add([
  31852. 'procedure Crawl(w: double); ',
  31853. 'begin',
  31854. 'end;',
  31855. 'procedure Run(w: double);',
  31856. 'begin',
  31857. ' await(Crawl(w));',
  31858. 'end;',
  31859. 'begin',
  31860. ' Run(1);']);
  31861. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31862. ConvertProgram;
  31863. end;
  31864. procedure TTestModule.TestAWait_IntegerFail;
  31865. begin
  31866. StartProgram(false);
  31867. Add([
  31868. 'function Run: word;',
  31869. 'begin',
  31870. 'end;',
  31871. 'procedure Fly(w: word); async;',
  31872. 'begin',
  31873. ' await(Run());',
  31874. 'end;',
  31875. 'begin',
  31876. ' Fly(1);']);
  31877. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  31878. ConvertProgram;
  31879. end;
  31880. procedure TTestModule.TestAWait_ExternalClassPromise;
  31881. begin
  31882. StartProgram(false);
  31883. Add([
  31884. '{$modeswitch externalclass}',
  31885. 'type',
  31886. ' TJSPromise = class external name ''Promise''',
  31887. ' end;',
  31888. ' TJSThenable = class external name ''Thenable''',
  31889. ' end;',
  31890. 'function Fly(w: word): TJSPromise;',
  31891. 'begin',
  31892. 'end;',
  31893. 'function Jump(w: word): word; async;',
  31894. 'begin',
  31895. 'end;',
  31896. 'function Eat(w: word): TJSPromise; async;',
  31897. 'begin',
  31898. 'end;',
  31899. 'function Run(d: double): word; async;',
  31900. 'var',
  31901. ' p: TJSPromise;',
  31902. 'begin',
  31903. ' Result:=await(word,p);', // promise needs type
  31904. ' Result:=await(word,Fly(3));', // promise needs type
  31905. ' Result:=await(Jump(4));', // async non promise must omit the type
  31906. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  31907. ' Result:=await(word,Eat(6));', // promise needs type
  31908. 'end;',
  31909. 'begin',
  31910. '']);
  31911. ConvertProgram;
  31912. CheckSource('TestAWait_ExternalClassPromise',
  31913. LinesToStr([ // statements
  31914. 'this.Fly = function (w) {',
  31915. ' var Result = null;',
  31916. ' return Result;',
  31917. '};',
  31918. 'this.Jump = async function (w) {',
  31919. ' var Result = 0;',
  31920. ' return Result;',
  31921. '};',
  31922. 'this.Eat = async function (w) {',
  31923. ' var Result = null;',
  31924. ' return Result;',
  31925. '};',
  31926. 'this.Run = async function (d) {',
  31927. ' var Result = 0;',
  31928. ' var p = null;',
  31929. ' Result = await p;',
  31930. ' Result = await $mod.Fly(3);',
  31931. ' Result = await $mod.Jump(4);',
  31932. ' Result = await $mod.Jump(5);',
  31933. ' Result = await $mod.Eat(6);',
  31934. ' return Result;',
  31935. '};',
  31936. '']),
  31937. LinesToStr([
  31938. ]));
  31939. CheckResolverUnexpectedHints();
  31940. end;
  31941. procedure TTestModule.TestAWait_JSValue;
  31942. begin
  31943. StartProgram(false);
  31944. Add([
  31945. '{$modeswitch externalclass}',
  31946. 'type',
  31947. ' TJSPromise = class external name ''Promise''',
  31948. ' end;',
  31949. 'function Fly(w: word): jsvalue; async;',
  31950. 'begin',
  31951. 'end;',
  31952. 'function Run(d: jsvalue; var e): word; async;',
  31953. 'begin',
  31954. ' Result:=await(word,d);', // promise needs type
  31955. ' d:=await(Fly(4));', // async non promise must omit the type
  31956. ' Result:=await(word,e);', // promise needs type
  31957. 'end;',
  31958. 'begin',
  31959. '']);
  31960. ConvertProgram;
  31961. CheckSource('TestAWait_JSValue',
  31962. LinesToStr([ // statements
  31963. 'this.Fly = async function (w) {',
  31964. ' var Result = undefined;',
  31965. ' return Result;',
  31966. '};',
  31967. 'this.Run = async function (d, e) {',
  31968. ' var Result = 0;',
  31969. ' Result = await d;',
  31970. ' d = await $mod.Fly(4);',
  31971. ' Result = await e.get();',
  31972. ' return Result;',
  31973. '};',
  31974. '']),
  31975. LinesToStr([
  31976. ]));
  31977. CheckResolverUnexpectedHints();
  31978. end;
  31979. procedure TTestModule.TestAWait_Result;
  31980. begin
  31981. StartProgram(false);
  31982. Add([
  31983. '{$modeswitch externalclass}',
  31984. 'type',
  31985. ' TJSPromise = class external name ''Promise''',
  31986. ' end;',
  31987. 'function Crawl(d: double = 1.3): TJSPromise; ',
  31988. 'begin',
  31989. 'end;',
  31990. 'function Run(d: double = 1.6): word; async;',
  31991. 'begin',
  31992. ' Result:=await(word,Crawl);',
  31993. ' Result:=await(word,Crawl(4.5));',
  31994. ' Result:=await(Run);',
  31995. ' Result:=await(Run(6.7));',
  31996. 'end;',
  31997. 'begin',
  31998. ' Run(1);']);
  31999. ConvertProgram;
  32000. CheckSource('TestAWait_Result',
  32001. LinesToStr([ // statements
  32002. 'this.Crawl = function (d) {',
  32003. ' var Result = null;',
  32004. ' return Result;',
  32005. '};',
  32006. 'this.Run = async function (d) {',
  32007. ' var Result = 0;',
  32008. ' Result = await $mod.Crawl(1.3);',
  32009. ' Result = await $mod.Crawl(4.5);',
  32010. ' Result = await $mod.Run(1.6);',
  32011. ' Result = await $mod.Run(6.7);',
  32012. ' return Result;',
  32013. '};',
  32014. '']),
  32015. LinesToStr([
  32016. '$mod.Run(1);'
  32017. ]));
  32018. CheckResolverUnexpectedHints();
  32019. end;
  32020. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32021. begin
  32022. StartProgram(false);
  32023. Add([
  32024. '{$mode objfpc}',
  32025. '{$modeswitch externalclass}',
  32026. 'type',
  32027. ' TJSPromise = class external name ''Promise''',
  32028. ' end;',
  32029. 'function Run: TJSPromise; async;',
  32030. 'begin',
  32031. 'end;',
  32032. 'procedure Fly(w: word); async;',
  32033. 'begin',
  32034. ' await(Run());',
  32035. 'end;',
  32036. 'begin',
  32037. ' Fly(1);']);
  32038. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32039. nWrongNumberOfParametersForCallTo);
  32040. ConvertProgram;
  32041. end;
  32042. procedure TTestModule.TestAsync_AnonymousProc;
  32043. begin
  32044. StartProgram(false);
  32045. Add([
  32046. '{$mode objfpc}',
  32047. '{$modeswitch externalclass}',
  32048. 'type',
  32049. ' TJSPromise = class external name ''Promise''',
  32050. ' end;',
  32051. 'type',
  32052. ' TFunc = reference to function(x: double): word; async;',
  32053. 'function Crawl(d: double = 1.3): word; async;',
  32054. 'begin',
  32055. 'end;',
  32056. 'var Func: TFunc;',
  32057. 'begin',
  32058. ' Func:=function(c:double):word async begin',
  32059. ' Result:=await(Crawl(c));',
  32060. ' end;',
  32061. ' Func:=function(c:double):word async assembler asm',
  32062. ' end;',
  32063. ' ']);
  32064. ConvertProgram;
  32065. CheckSource('TestAsync_AnonymousProc',
  32066. LinesToStr([ // statements
  32067. 'this.Crawl = async function (d) {',
  32068. ' var Result = 0;',
  32069. ' return Result;',
  32070. '};',
  32071. 'this.Func = null;',
  32072. '']),
  32073. LinesToStr([
  32074. '$mod.Func = async function (c) {',
  32075. ' var Result = 0;',
  32076. ' Result = await $mod.Crawl(c);',
  32077. ' return Result;',
  32078. '};',
  32079. '$mod.Func = async function (c) {',
  32080. '};',
  32081. '']));
  32082. CheckResolverUnexpectedHints();
  32083. end;
  32084. procedure TTestModule.TestAsync_ProcType;
  32085. begin
  32086. StartProgram(false);
  32087. Add([
  32088. '{$mode objfpc}',
  32089. 'type',
  32090. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32091. ' TFunc = function(x: double = 1.1): word; async;',
  32092. ' TProc = procedure(x: longint = 7); async;',
  32093. 'function Crawl(d: double): word; async;',
  32094. 'begin',
  32095. 'end;',
  32096. 'procedure Run(e:longint); async;',
  32097. 'begin',
  32098. 'end;',
  32099. 'procedure Fly(p: TProc); async;',
  32100. 'begin',
  32101. ' await(p);',
  32102. ' await(p());',
  32103. 'end;',
  32104. 'var',
  32105. ' RefFunc: TRefFunc;',
  32106. ' Func: TFunc;',
  32107. ' Proc, ProcB: TProc;',
  32108. 'begin',
  32109. ' Func:=@Crawl;',
  32110. ' RefFunc:=@Crawl;',
  32111. ' RefFunc:=function(c:double):word async begin',
  32112. ' Result:=await(RefFunc);',
  32113. ' Result:=await(RefFunc());',
  32114. ' Result:=await(Func);',
  32115. ' Result:=await(Func());',
  32116. ' await(Proc);',
  32117. ' await(Proc());',
  32118. ' await(Proc(13));',
  32119. ' end;',
  32120. ' Proc:=@Run;',
  32121. ' if Proc=ProcB then ;',
  32122. ' ']);
  32123. ConvertProgram;
  32124. CheckResolverUnexpectedHints();
  32125. CheckSource('TestAsync_ProcType',
  32126. LinesToStr([ // statements
  32127. 'this.Crawl = async function (d) {',
  32128. ' var Result = 0;',
  32129. ' return Result;',
  32130. '};',
  32131. 'this.Run = async function (e) {',
  32132. '};',
  32133. 'this.Fly = async function (p) {',
  32134. ' await p(7);',
  32135. ' await p(7);',
  32136. '};',
  32137. 'this.RefFunc = null;',
  32138. 'this.Func = null;',
  32139. 'this.Proc = null;',
  32140. 'this.ProcB = null;',
  32141. '']),
  32142. LinesToStr([
  32143. '$mod.Func = $mod.Crawl;',
  32144. '$mod.RefFunc = $mod.Crawl;',
  32145. '$mod.RefFunc = async function (c) {',
  32146. ' var Result = 0;',
  32147. ' Result = await $mod.RefFunc(1.3);',
  32148. ' Result = await $mod.RefFunc(1.3);',
  32149. ' Result = await $mod.Func(1.1);',
  32150. ' Result = await $mod.Func(1.1);',
  32151. ' await $mod.Proc(7);',
  32152. ' await $mod.Proc(7);',
  32153. ' await $mod.Proc(13);',
  32154. ' return Result;',
  32155. '};',
  32156. '$mod.Proc = $mod.Run;',
  32157. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32158. '']));
  32159. end;
  32160. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32161. begin
  32162. StartProgram(false);
  32163. Add([
  32164. '{$mode objfpc}',
  32165. 'type',
  32166. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32167. 'function Crawl(d: double): word; async;',
  32168. 'begin',
  32169. 'end;',
  32170. 'var',
  32171. ' RefFunc: TRefFunc;',
  32172. 'begin',
  32173. ' RefFunc:=@Crawl;',
  32174. ' ']);
  32175. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32176. ConvertProgram;
  32177. end;
  32178. procedure TTestModule.TestAsync_Inherited;
  32179. begin
  32180. StartProgram(false);
  32181. Add([
  32182. '{$mode objfpc}',
  32183. '{$modeswitch externalclass}',
  32184. 'type',
  32185. ' TJSPromise = class external name ''Promise''',
  32186. ' end;',
  32187. ' TObject = class',
  32188. ' function Run(w: word = 3): word; async; virtual;',
  32189. ' end;',
  32190. ' TBird = class',
  32191. ' function Run(w: word = 3): word; async; override;',
  32192. ' end;',
  32193. 'function TObject.Run(w: word = 3): word; async;',
  32194. 'begin',
  32195. 'end;',
  32196. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32197. 'var p: TJSPromise;',
  32198. 'begin',
  32199. ' p:=inherited;',
  32200. ' p:=inherited Run;',
  32201. ' p:=inherited Run();',
  32202. ' p:=inherited Run(4);',
  32203. ' exit(p);',
  32204. ' exit(inherited);',
  32205. ' exit(inherited Run);',
  32206. ' exit(inherited Run(5));',
  32207. ' exit(6);',
  32208. 'end;',
  32209. 'begin',
  32210. ' ']);
  32211. ConvertProgram;
  32212. CheckSource('TestAsync_Inherited',
  32213. LinesToStr([ // statements
  32214. 'rtl.createClass(this, "TObject", null, function () {',
  32215. ' this.$init = function () {',
  32216. ' };',
  32217. ' this.$final = function () {',
  32218. ' };',
  32219. ' this.Run = async function (w) {',
  32220. ' var Result = 0;',
  32221. ' return Result;',
  32222. ' };',
  32223. '});',
  32224. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32225. ' this.Run = async function (w) {',
  32226. ' var Result = 0;',
  32227. ' var p = null;',
  32228. ' p = $mod.TObject.Run.apply(this, arguments);',
  32229. ' p = $mod.TObject.Run.call(this, 3);',
  32230. ' p = $mod.TObject.Run.call(this, 3);',
  32231. ' p = $mod.TObject.Run.call(this, 4);',
  32232. ' return p;',
  32233. ' return $mod.TObject.Run.apply(this, arguments);',
  32234. ' return $mod.TObject.Run.call(this, 3);',
  32235. ' return $mod.TObject.Run.call(this, 5);',
  32236. ' return 6;',
  32237. ' return Result;',
  32238. ' };',
  32239. '});',
  32240. '']),
  32241. LinesToStr([
  32242. '']));
  32243. CheckResolverUnexpectedHints();
  32244. end;
  32245. procedure TTestModule.TestAsync_ClassInterface;
  32246. begin
  32247. StartProgram(false);
  32248. Add([
  32249. '{$mode objfpc}',
  32250. '{$modeswitch externalclass}',
  32251. 'type',
  32252. ' TJSPromise = class external name ''Promise''',
  32253. ' end;',
  32254. ' IUnknown = interface',
  32255. ' function _AddRef: longint;',
  32256. ' function _Release: longint;',
  32257. ' end;',
  32258. 'function Say(i: IUnknown): IUnknown; async;',
  32259. 'begin',
  32260. 'end;',
  32261. 'function Run: IUnknown; async;',
  32262. 'begin',
  32263. ' Result:=await(Run);',
  32264. ' Result:=await(Run());',
  32265. ' Result:=await(Run) as IUnknown;',
  32266. ' Result:=await(Say(nil));',
  32267. ' Result:=await(Say(await(Run())));',
  32268. ' Result:=await(Say(await(Run()) as IUnknown));',
  32269. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32270. 'end;',
  32271. 'procedure Fly;',
  32272. 'var p: TJSPromise;',
  32273. 'begin',
  32274. ' Run;',
  32275. ' Run();',
  32276. ' p:=Run;',
  32277. ' p:=Run();',
  32278. 'end;',
  32279. 'begin',
  32280. ' ']);
  32281. ConvertProgram;
  32282. CheckSource('TestAsync_ClassInterface',
  32283. LinesToStr([ // statements
  32284. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32285. 'this.Say = async function (i) {',
  32286. ' var Result = null;',
  32287. ' return Result;',
  32288. '};',
  32289. 'this.Run = async function () {',
  32290. ' var Result = null;',
  32291. ' var $ok = false;',
  32292. ' try {',
  32293. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32294. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32295. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32296. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32297. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32298. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32299. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32300. ' $ok = true;',
  32301. ' } finally {',
  32302. ' if (!$ok) rtl._Release(Result);',
  32303. ' };',
  32304. ' return Result;',
  32305. '};',
  32306. 'this.Fly = function () {',
  32307. ' var p = null;',
  32308. ' $mod.Run();',
  32309. ' $mod.Run();',
  32310. ' p = $mod.Run();',
  32311. ' p = $mod.Run();',
  32312. '};',
  32313. '']),
  32314. LinesToStr([
  32315. '']));
  32316. CheckResolverUnexpectedHints();
  32317. end;
  32318. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32319. begin
  32320. StartProgram(true,[supTInterfacedObject]);
  32321. Add([
  32322. '{$mode objfpc}',
  32323. '{$modeswitch externalclass}',
  32324. 'type',
  32325. ' TJSPromise = class external name ''Promise''',
  32326. ' end;',
  32327. ' IBird = interface',
  32328. ' procedure Run;',
  32329. ' end;',
  32330. ' TBird = class(TInterfacedObject,IBird)',
  32331. ' procedure Run; async;',
  32332. ' end;',
  32333. 'procedure TBird.Run;',
  32334. 'begin',
  32335. 'end;',
  32336. 'begin',
  32337. ' ']);
  32338. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32339. ConvertProgram;
  32340. end;
  32341. Initialization
  32342. RegisterTests([TTestModule]);
  32343. end.