tcmodules.pas 977 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(aFilename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const aFilename: string): integer;
  209. function GetResolver(const aFilename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConstWhitespaces;
  303. Procedure TestStringConst_Multiline;
  304. Procedure TestString_Length;
  305. Procedure TestString_Compare;
  306. Procedure TestString_SetLength;
  307. Procedure TestString_CharAt;
  308. Procedure TestStringHMinusFail;
  309. Procedure TestStr;
  310. Procedure TestBaseType_AnsiStringFail;
  311. Procedure TestBaseType_WideStringFail;
  312. Procedure TestBaseType_ShortStringFail;
  313. Procedure TestBaseType_RawByteStringFail;
  314. Procedure TestTypeShortstring_Fail;
  315. Procedure TestCharSet_Custom;
  316. Procedure TestWideChar;
  317. Procedure TestForCharDo;
  318. Procedure TestForCharInDo;
  319. // alias types
  320. Procedure TestAliasTypeRef;
  321. Procedure TestTypeCast_BaseTypes;
  322. Procedure TestTypeCast_AliasBaseTypes;
  323. // functions
  324. Procedure TestEmptyProc;
  325. Procedure TestProcOneParam;
  326. Procedure TestFunctionWithoutParams;
  327. Procedure TestProcedureWithoutParams;
  328. Procedure TestPrgProcVar;
  329. Procedure TestProcTwoArgs;
  330. Procedure TestProc_DefaultValue;
  331. Procedure TestUnitProcVar;
  332. Procedure TestImplProc;
  333. Procedure TestFunctionResult;
  334. Procedure TestNestedProc;
  335. Procedure TestNestedProc_ResultString;
  336. Procedure TestForwardProc;
  337. Procedure TestNestedForwardProc;
  338. Procedure TestAssignFunctionResult;
  339. Procedure TestFunctionResultInCondition;
  340. Procedure TestFunctionResultInForLoop;
  341. Procedure TestFunctionResultInTypeCast;
  342. Procedure TestExit;
  343. Procedure TestExit_ResultInFinally;
  344. Procedure TestBreak;
  345. Procedure TestBreakAsVar;
  346. Procedure TestContinue;
  347. Procedure TestProc_External;
  348. Procedure TestProc_ExternalOtherUnit;
  349. Procedure TestProc_Asm;
  350. Procedure TestProc_AsmSubBlock;
  351. Procedure TestProc_Assembler;
  352. Procedure TestProc_VarParam;
  353. Procedure TestProc_VarParamString;
  354. Procedure TestProc_VarParamV;
  355. Procedure TestProc_Overload;
  356. Procedure TestProc_OverloadForward;
  357. Procedure TestProc_OverloadIntfImpl;
  358. Procedure TestProc_OverloadNested;
  359. Procedure TestProc_OverloadNestedForward;
  360. Procedure TestProc_OverloadUnitCycle;
  361. Procedure TestProc_Varargs;
  362. Procedure TestProc_ConstOrder;
  363. Procedure TestProc_DuplicateConst;
  364. Procedure TestProc_LocalVarAbsolute;
  365. Procedure TestProc_ResultAbsolute;
  366. Procedure TestProc_LocalVarInit;
  367. Procedure TestProc_ReservedWords;
  368. Procedure TestProc_ConstRefWord;
  369. // anonymous functions
  370. Procedure TestAnonymousProc_Assign_ObjFPC;
  371. Procedure TestAnonymousProc_Assign_Delphi;
  372. Procedure TestAnonymousProc_Arg;
  373. Procedure TestAnonymousProc_Typecast;
  374. Procedure TestAnonymousProc_With;
  375. Procedure TestAnonymousProc_ExceptOn;
  376. Procedure TestAnonymousProc_Nested;
  377. Procedure TestAnonymousProc_NestedAssignResult;
  378. Procedure TestAnonymousProc_Class;
  379. Procedure TestAnonymousProc_ForLoop;
  380. Procedure TestAnonymousProc_AsmDelphi;
  381. // enums, sets
  382. Procedure TestEnum_Name;
  383. Procedure TestEnum_Number;
  384. Procedure TestEnum_ConstFail;
  385. Procedure TestEnum_Functions;
  386. Procedure TestEnumRg_Functions;
  387. Procedure TestEnum_AsParams;
  388. Procedure TestEnumRange_Array;
  389. Procedure TestEnum_ForIn;
  390. Procedure TestEnum_ScopedNumber;
  391. Procedure TestEnum_InFunction;
  392. Procedure TestEnum_Name_Anonymous_Unit;
  393. Procedure TestSet_Enum;
  394. Procedure TestSet_Operators;
  395. Procedure TestSet_Operator_In;
  396. Procedure TestSet_Functions;
  397. Procedure TestSet_PassAsArgClone;
  398. Procedure TestSet_AsParams;
  399. Procedure TestSet_Property;
  400. Procedure TestSet_EnumConst;
  401. Procedure TestSet_IntConst;
  402. Procedure TestSet_IntRange;
  403. Procedure TestSet_AnonymousEnumType;
  404. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  405. Procedure TestSet_ConstEnum;
  406. Procedure TestSet_ConstChar;
  407. Procedure TestSet_ConstInt;
  408. Procedure TestSet_InFunction;
  409. Procedure TestSet_ForIn;
  410. // statements
  411. Procedure TestNestBegin;
  412. Procedure TestIncDec;
  413. Procedure TestLoHiFpcMode;
  414. Procedure TestLoHiDelphiMode;
  415. Procedure TestAssignments;
  416. Procedure TestArithmeticOperators1;
  417. Procedure TestMultiAdd;
  418. Procedure TestLogicalOperators;
  419. Procedure TestBitwiseOperators;
  420. Procedure TestBitwiseOperatorsLongword;
  421. Procedure TestFunctionInt;
  422. Procedure TestFunctionString;
  423. Procedure TestIfThen;
  424. Procedure TestForLoop;
  425. Procedure TestForLoopInsideFunction;
  426. Procedure TestForLoop_ReadVarAfter;
  427. Procedure TestForLoop_Nested;
  428. Procedure TestRepeatUntil;
  429. Procedure TestAsmBlock;
  430. Procedure TestAsmPas_Impl; // ToDo
  431. Procedure TestTryFinally;
  432. Procedure TestTryExcept;
  433. Procedure TestTryExcept_ReservedWords;
  434. Procedure TestIfThenRaiseElse;
  435. Procedure TestCaseOf;
  436. Procedure TestCaseOf_UseSwitch;
  437. Procedure TestCaseOfNoElse;
  438. Procedure TestCaseOfNoElse_UseSwitch;
  439. Procedure TestCaseOfRange;
  440. Procedure TestCaseOfString;
  441. Procedure TestCaseOfChar;
  442. Procedure TestCaseOfExternalClassConst;
  443. Procedure TestDebugger;
  444. // arrays
  445. Procedure TestArray_Dynamic;
  446. Procedure TestArray_Dynamic_Nil;
  447. Procedure TestArray_DynMultiDimensional;
  448. Procedure TestArray_DynamicAssign;
  449. Procedure TestArray_StaticInt;
  450. Procedure TestArray_StaticBool;
  451. Procedure TestArray_StaticChar;
  452. Procedure TestArray_StaticMultiDim;
  453. Procedure TestArray_StaticInFunction;
  454. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  455. Procedure TestArrayOfRecord;
  456. Procedure TestArray_StaticRecord;
  457. Procedure TestArrayOfSet;
  458. Procedure TestArray_DynAsParam;
  459. Procedure TestArray_StaticAsParam;
  460. Procedure TestArrayElement_AsParams;
  461. Procedure TestArrayElementFromFuncResult_AsParams;
  462. Procedure TestArrayEnumTypeRange;
  463. Procedure TestArray_SetLengthOutArg;
  464. Procedure TestArray_SetLengthProperty;
  465. Procedure TestArray_SetLengthMultiDim;
  466. Procedure TestArray_SetLengthDynOfStatic;
  467. Procedure TestArray_OpenArrayOfString;
  468. Procedure TestArray_ArrayOfCharAssignString;
  469. Procedure TestArray_ConstRef;
  470. Procedure TestArray_Concat;
  471. Procedure TestArray_Concat_Append;
  472. Procedure TestArray_Concat_Append_Var;
  473. Procedure TestArray_Copy;
  474. Procedure TestArray_InsertDelete;
  475. Procedure TestArray_Add_Append;
  476. Procedure TestArray_DynArrayConstObjFPC;
  477. Procedure TestArray_DynArrayConstDelphi;
  478. Procedure TestArray_ArrayLitAsParam;
  479. Procedure TestArray_ArrayLitMultiDimAsParam;
  480. Procedure TestArray_ArrayLitStaticAsParam;
  481. Procedure TestArray_ForInArrOfString;
  482. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  483. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  484. Procedure TestArrayOfConst_TVarRec;
  485. Procedure TestArrayOfConst_PassBaseTypes;
  486. Procedure TestArrayOfConst_PassObj;
  487. // record
  488. Procedure TestRecord_Empty;
  489. Procedure TestRecord_Var;
  490. Procedure TestRecord_VarExternal;
  491. Procedure TestRecord_WithDo;
  492. Procedure TestRecord_Assign;
  493. Procedure TestRecord_AsParams;
  494. Procedure TestRecord_ConstRef;
  495. Procedure TestRecordElement_AsParams;
  496. Procedure TestRecordElementFromFuncResult_AsParams;
  497. Procedure TestRecordElementFromWith_AsParams;
  498. Procedure TestRecord_Equal;
  499. Procedure TestRecord_JSValue;
  500. Procedure TestRecord_VariantFail;
  501. Procedure TestRecord_FieldArray;
  502. Procedure TestRecord_Const;
  503. Procedure TestRecord_TypecastFail;
  504. Procedure TestRecord_InFunction;
  505. Procedure TestRecord_ArrayConstMultiline;
  506. // ToDo: insert(record,ArrayOfRecord,0)
  507. // anonymous record
  508. Procedure TestRecordAnonym_Field;
  509. Procedure TestRecordAnonym_Assign;
  510. Procedure TestRecordAnonym_Nested;
  511. Procedure TestRecordAnonym_Const;
  512. Procedure TestRecordAnonym_InFunction;
  513. // advanced record
  514. Procedure TestAdvRecord_Function;
  515. Procedure TestAdvRecord_Property;
  516. Procedure TestAdvRecord_PropertyDefault;
  517. Procedure TestAdvRecord_Property_ClassMethod;
  518. Procedure TestAdvRecord_Const;
  519. Procedure TestAdvRecord_ExternalField;
  520. Procedure TestAdvRecord_SubRecord;
  521. Procedure TestAdvRecord_SubClass;
  522. Procedure TestAdvRecord_SubInterfaceFail;
  523. Procedure TestAdvRecord_Constructor;
  524. Procedure TestAdvRecord_ClassConstructor_Program;
  525. Procedure TestAdvRecord_ClassConstructor_Unit;
  526. // classes
  527. Procedure TestClass_TObjectDefaultConstructor;
  528. Procedure TestClass_TObjectConstructorWithParams;
  529. Procedure TestClass_TObjectConstructorWithDefaultParam;
  530. Procedure TestClass_Var;
  531. Procedure TestClass_Method;
  532. Procedure TestClass_Implementation;
  533. Procedure TestClass_Inheritance;
  534. Procedure TestClass_TypeAlias;
  535. Procedure TestClass_AbstractMethod;
  536. Procedure TestClass_CallInherited_ProcNoParams;
  537. Procedure TestClass_CallInherited_WithParams;
  538. Procedure TestClasS_CallInheritedConstructor;
  539. Procedure TestClass_ClassVar_Assign;
  540. Procedure TestClass_CallClassMethod;
  541. Procedure TestClass_CallClassMethodStatic;
  542. Procedure TestClass_Property;
  543. Procedure TestClass_Property_ClassMethod;
  544. Procedure TestClass_Property_ClassMethodStatic;
  545. Procedure TestClass_Property_Indexed;
  546. Procedure TestClass_Property_IndexSpec;
  547. Procedure TestClass_PropertyOfTypeArray;
  548. Procedure TestClass_PropertyDefault;
  549. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  550. //Procedure TestClass_PropertyDefault;
  551. Procedure TestClass_PropertyOverride;
  552. Procedure TestClass_PropertyIncVisibility;
  553. Procedure TestClass_Assigned;
  554. Procedure TestClass_WithClassDoCreate;
  555. Procedure TestClass_WithClassInstDoProperty;
  556. Procedure TestClass_WithClassInstDoPropertyWithParams;
  557. Procedure TestClass_WithClassInstDoFunc;
  558. Procedure TestClass_TypeCast;
  559. Procedure TestClass_TypeCastUntypedParam;
  560. Procedure TestClass_Overloads;
  561. Procedure TestClass_OverloadsAncestor;
  562. Procedure TestClass_OverloadConstructor;
  563. Procedure TestClass_OverloadDelphiOverride;
  564. Procedure TestClass_ReintroduceVarDelphi;
  565. Procedure TestClass_ReintroducedVar;
  566. Procedure TestClass_RaiseDescendant;
  567. Procedure TestClass_ExternalMethod;
  568. Procedure TestClass_ExternalVirtualNameMismatchFail;
  569. Procedure TestClass_ExternalOverrideFail;
  570. Procedure TestClass_ExternalVar;
  571. Procedure TestClass_Const;
  572. Procedure TestClass_ConstEnum;
  573. Procedure TestClass_LocalConstDuplicate_Prg;
  574. Procedure TestClass_LocalConstDuplicate_Unit;
  575. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  576. Procedure TestClass_LocalVarSelfFail;
  577. Procedure TestClass_ArgSelfFail;
  578. Procedure TestClass_NestedProcSelf;
  579. Procedure TestClass_NestedProcSelf2;
  580. Procedure TestClass_NestedProcClassSelf;
  581. Procedure TestClass_NestedProcCallInherited;
  582. Procedure TestClass_TObjectFree;
  583. Procedure TestClass_TObjectFree_VarArg;
  584. Procedure TestClass_TObjectFreeNewInstance;
  585. Procedure TestClass_TObjectFreeLowerCase;
  586. Procedure TestClass_TObjectFreeFunctionFail;
  587. Procedure TestClass_TObjectFreePropertyFail;
  588. Procedure TestClass_ForIn;
  589. Procedure TestClass_DispatchMessage;
  590. Procedure TestClass_Message_DuplicateIntFail;
  591. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  592. // class of
  593. Procedure TestClassOf_Create;
  594. Procedure TestClassOf_Call;
  595. Procedure TestClassOf_Assign;
  596. Procedure TestClassOf_Is;
  597. Procedure TestClassOf_Compare;
  598. Procedure TestClassOf_ClassVar;
  599. Procedure TestClassOf_ClassMethod;
  600. Procedure TestClassOf_ClassProperty;
  601. Procedure TestClassOf_ClassMethodSelf;
  602. Procedure TestClassOf_TypeCast;
  603. Procedure TestClassOf_ImplicitFunctionCall;
  604. Procedure TestClassOf_Const;
  605. // nested class
  606. Procedure TestNestedClass_Alias;
  607. Procedure TestNestedClass_Record;
  608. Procedure TestNestedClass_Class;
  609. Procedure TestNestedClass_CallInherited;
  610. // external class
  611. Procedure TestExternalClass_Var;
  612. Procedure TestExternalClass_Const;
  613. Procedure TestExternalClass_Dollar;
  614. Procedure TestExternalClass_DuplicateVarFail;
  615. Procedure TestExternalClass_Method;
  616. Procedure TestExternalClass_ClassMethod;
  617. Procedure TestExternalClass_ClassMethodStatic;
  618. Procedure TestExternalClass_FunctionResultInTypeCast;
  619. Procedure TestExternalClass_NonExternalOverride;
  620. Procedure TestExternalClass_OverloadHint;
  621. Procedure TestExternalClass_SameNamePublishedProperty;
  622. Procedure TestExternalClass_Property;
  623. Procedure TestExternalClass_PropertyDate;
  624. Procedure TestExternalClass_ClassProperty;
  625. Procedure TestExternalClass_ClassOf;
  626. Procedure TestExternalClass_ClassOtherUnit;
  627. Procedure TestExternalClass_Is;
  628. Procedure TestExternalClass_As;
  629. Procedure TestExternalClass_DestructorFail;
  630. Procedure TestExternalClass_New;
  631. Procedure TestExternalClass_ClassOf_New;
  632. Procedure TestExternalClass_FuncClassOf_New;
  633. Procedure TestExternalClass_New_PasClassFail;
  634. Procedure TestExternalClass_New_PasClassBracketsFail;
  635. Procedure TestExternalClass_NewExtName;
  636. Procedure TestExternalClass_Constructor;
  637. Procedure TestExternalClass_ConstructorBrackets;
  638. Procedure TestExternalClass_LocalConstSameName;
  639. Procedure TestExternalClass_ReintroduceOverload;
  640. Procedure TestExternalClass_Inherited;
  641. Procedure TestExternalClass_PascalAncestorFail;
  642. Procedure TestExternalClass_NewInstance;
  643. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  644. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  645. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  646. Procedure TestExternalClass_JSFunctionPasDescendant;
  647. Procedure TestExternalClass_PascalProperty;
  648. Procedure TestExternalClass_TypeCastToRootClass;
  649. Procedure TestExternalClass_TypeCastToJSObject;
  650. Procedure TestExternalClass_TypeCastStringToExternalString;
  651. Procedure TestExternalClass_TypeCastToJSFunction;
  652. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  653. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  654. Procedure TestExternalClass_BracketAccessor;
  655. Procedure TestExternalClass_BracketAccessor_Call;
  656. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  657. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  658. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  659. Procedure TestExternalClass_BracketAccessor_MultiType;
  660. Procedure TestExternalClass_BracketAccessor_Index;
  661. Procedure TestExternalClass_ForInJSObject;
  662. Procedure TestExternalClass_ForInJSArray;
  663. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  664. Procedure TestExternalClass_NestedConstructor;
  665. // class interfaces
  666. Procedure TestClassInterface_Corba;
  667. Procedure TestClassInterface_Corba_ProcExternalFail;
  668. Procedure TestClassInterface_Corba_Overloads;
  669. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  670. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  671. Procedure TestClassInterface_Corba_AncestorImpl;
  672. Procedure TestClassInterface_Corba_ImplReintroduce;
  673. Procedure TestClassInterface_Corba_MethodResolution;
  674. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  675. Procedure TestClassInterface_Corba_MethodOverride;
  676. Procedure TestClassInterface_Corba_Delegation;
  677. Procedure TestClassInterface_Corba_DelegationStatic;
  678. Procedure TestClassInterface_Corba_Operators;
  679. Procedure TestClassInterface_Corba_Args;
  680. Procedure TestClassInterface_Corba_ForIn;
  681. Procedure TestClassInterface_Corba_ArrayOfIntf;
  682. Procedure TestClassInterface_COM_AssignVar;
  683. Procedure TestClassInterface_COM_AssignArg;
  684. Procedure TestClassInterface_COM_FunctionResult;
  685. Procedure TestClassInterface_COM_InheritedFuncResult;
  686. Procedure TestClassInterface_COM_FunctionExit;
  687. Procedure TestClassInterface_COM_IsAsTypeCasts;
  688. Procedure TestClassInterface_COM_PassAsArg;
  689. Procedure TestClassInterface_COM_PassToUntypedParam;
  690. Procedure TestClassInterface_COM_FunctionInExpr;
  691. Procedure TestClassInterface_COM_Property;
  692. Procedure TestClassInterface_COM_IntfProperty;
  693. Procedure TestClassInterface_COM_Delegation;
  694. Procedure TestClassInterface_COM_With;
  695. Procedure TestClassInterface_COM_ForObjectInInterface;
  696. Procedure TestClassInterface_COM_ForInterfaceInObject;
  697. Procedure TestClassInterface_COM_ArrayOfIntf_AssignVar;
  698. Procedure TestClassInterface_COM_ArrayOfIntf_AssignPlus;
  699. Procedure TestClassInterface_COM_ArrayOfIntf_AssignArg;
  700. Procedure TestClassInterface_COM_ArrayOfIntf_InitFail;
  701. Procedure TestClassInterface_COM_ArrayOfIntf_FunctionResult;
  702. Procedure TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult;
  703. Procedure TestClassInterface_COM_ArrayOfIntf_FunctionExit;
  704. Procedure TestClassInterface_COM_ArrayOfIntf_Property;
  705. Procedure TestClassInterface_COM_ArrayOfIntf_BIFuncs;
  706. Procedure TestClassInterface_COM_ArrayOfIntf_ForIn;
  707. Procedure TestClassInterface_COM_StaticArrayOfIntfFail;
  708. Procedure TestClassInterface_COM_RecordIntfFail;
  709. Procedure TestClassInterface_COM_UnitInitialization;
  710. Procedure TestClassInterface_Corba_GUID;
  711. Procedure TestClassInterface_Corba_GUIDProperty;
  712. // helpers
  713. Procedure TestClassHelper_ClassVar;
  714. Procedure TestClassHelper_Method_AccessInstanceFields;
  715. Procedure TestClassHelper_Method_Call;
  716. Procedure TestClassHelper_Method_Nested_Call;
  717. Procedure TestClassHelper_ClassMethod_Call;
  718. Procedure TestClassHelper_ClassOf;
  719. Procedure TestClassHelper_MethodRefObjFPC;
  720. Procedure TestClassHelper_Constructor;
  721. Procedure TestClassHelper_InheritedObjFPC;
  722. Procedure TestClassHelper_Property;
  723. Procedure TestClassHelper_Property_Array;
  724. Procedure TestClassHelper_Property_Array_Default;
  725. Procedure TestClassHelper_Property_Array_DefaultDefault;
  726. Procedure TestClassHelper_ClassProperty;
  727. Procedure TestClassHelper_ClassPropertyStatic;
  728. Procedure TestClassHelper_ClassProperty_Array;
  729. Procedure TestClassHelper_ForIn;
  730. Procedure TestClassHelper_PassProperty;
  731. Procedure TestExtClassHelper_ClassVar;
  732. Procedure TestExtClassHelper_Method_Call;
  733. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  734. Procedure TestRecordHelper_ClassVar;
  735. Procedure TestRecordHelper_Method_Call;
  736. Procedure TestRecordHelper_Constructor;
  737. Procedure TestTypeHelper_ClassVar;
  738. Procedure TestTypeHelper_PassResultElement;
  739. Procedure TestTypeHelper_PassArgs;
  740. Procedure TestTypeHelper_PassVarConst;
  741. Procedure TestTypeHelper_PassFuncResult;
  742. Procedure TestTypeHelper_PassPropertyField;
  743. Procedure TestTypeHelper_PassPropertyGetter;
  744. Procedure TestTypeHelper_PassClassPropertyField;
  745. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  746. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  747. Procedure TestTypeHelper_Property;
  748. Procedure TestTypeHelper_Property_Array;
  749. Procedure TestTypeHelper_ClassProperty;
  750. Procedure TestTypeHelper_ClassProperty_Array;
  751. Procedure TestTypeHelper_ClassMethod;
  752. Procedure TestTypeHelper_ExtClassMethodFail;
  753. Procedure TestTypeHelper_Constructor;
  754. Procedure TestTypeHelper_Word;
  755. Procedure TestTypeHelper_Boolean;
  756. Procedure TestTypeHelper_WordBool;
  757. Procedure TestTypeHelper_Double;
  758. Procedure TestTypeHelper_NativeInt;
  759. Procedure TestTypeHelper_StringChar;
  760. Procedure TestTypeHelper_JSValue;
  761. Procedure TestTypeHelper_Array;
  762. Procedure TestTypeHelper_EnumType;
  763. Procedure TestTypeHelper_SetType;
  764. Procedure TestTypeHelper_InterfaceType;
  765. Procedure TestTypeHelper_NestedSelf;
  766. // proc types
  767. Procedure TestProcType;
  768. Procedure TestProcType_Arg;
  769. Procedure TestProcType_FunctionFPC;
  770. Procedure TestProcType_FunctionDelphi;
  771. Procedure TestProcType_ProcedureDelphi;
  772. Procedure TestProcType_AsParam;
  773. Procedure TestProcType_MethodFPC;
  774. Procedure TestProcType_MethodDelphi;
  775. Procedure TestProcType_PropertyFPC;
  776. Procedure TestProcType_PropertyDelphi;
  777. Procedure TestProcType_WithClassInstDoPropertyFPC;
  778. Procedure TestProcType_Nested;
  779. Procedure TestProcType_NestedOfObject;
  780. Procedure TestProcType_ReferenceToProc;
  781. Procedure TestProcType_ReferenceToMethod;
  782. Procedure TestProcType_Typecast;
  783. Procedure TestProcType_PassProcToUntyped;
  784. Procedure TestProcType_PassProcToArray;
  785. Procedure TestProcType_SafeCallObjFPC;
  786. Procedure TestProcType_SafeCallDelphi;
  787. Procedure TestProcType_SafeCall_Arg;
  788. // pointer
  789. Procedure TestPointer;
  790. Procedure TestPointer_Proc;
  791. Procedure TestPointer_AssignRecordFail;
  792. Procedure TestPointer_AssignStaticArrayFail;
  793. Procedure TestPointer_TypeCastJSValueToPointer;
  794. Procedure TestPointer_NonRecordFail;
  795. Procedure TestPointer_AnonymousArgTypeFail;
  796. Procedure TestPointer_AnonymousVarTypeFail;
  797. Procedure TestPointer_AnonymousResultTypeFail;
  798. Procedure TestPointer_AddrOperatorFail;
  799. Procedure TestPointer_ArrayParamsFail;
  800. Procedure TestPointer_PointerAddFail;
  801. Procedure TestPointer_IncPointerFail;
  802. Procedure TestPointer_Record;
  803. Procedure TestPointer_RecordArg;
  804. // jsvalue
  805. Procedure TestJSValue_AssignToJSValue;
  806. Procedure TestJSValue_TypeCastToBaseType;
  807. Procedure TestJSValue_TypecastToJSValue;
  808. Procedure TestJSValue_Equal;
  809. Procedure TestJSValue_If;
  810. Procedure TestJSValue_Not;
  811. Procedure TestJSValue_Enum;
  812. Procedure TestJSValue_ClassInstance;
  813. Procedure TestJSValue_ClassOf;
  814. Procedure TestJSValue_ArrayOfJSValue;
  815. Procedure TestJSValue_ArrayLit;
  816. Procedure TestJSValue_Params;
  817. Procedure TestJSValue_UntypedParam;
  818. Procedure TestJSValue_FuncResultType;
  819. Procedure TestJSValue_ProcType_Assign;
  820. Procedure TestJSValue_ProcType_Equal;
  821. Procedure TestJSValue_ProcType_Param;
  822. Procedure TestJSValue_AssignToPointerFail;
  823. Procedure TestJSValue_OverloadDouble;
  824. Procedure TestJSValue_OverloadNativeInt;
  825. Procedure TestJSValue_OverloadWord;
  826. Procedure TestJSValue_OverloadString;
  827. Procedure TestJSValue_OverloadChar;
  828. Procedure TestJSValue_OverloadPointer;
  829. Procedure TestJSValue_ForIn;
  830. // RTTI
  831. Procedure TestRTTI_IntRange;
  832. Procedure TestRTTI_Double;
  833. Procedure TestRTTI_ProcType;
  834. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  835. Procedure TestRTTI_ProcTypeAnonymous;
  836. Procedure TestRTTI_EnumAndSetType;
  837. Procedure TestRTTI_EnumRange;
  838. Procedure TestRTTI_AnonymousEnumType;
  839. Procedure TestRTTI_StaticArray;
  840. Procedure TestRTTI_DynArray;
  841. Procedure TestRTTI_ArrayNestedAnonymous;
  842. Procedure TestRTTI_PublishedMethodOverloadFail;
  843. Procedure TestRTTI_PublishedMethodHideNoHint;
  844. Procedure TestRTTI_PublishedMethodExternalFail;
  845. Procedure TestRTTI_PublishedClassPropertyFail;
  846. Procedure TestRTTI_PublishedClassFieldFail;
  847. Procedure TestRTTI_PublishedFieldExternalFail;
  848. Procedure TestRTTI_Class_Field;
  849. Procedure TestRTTI_Class_FieldPrivate;
  850. Procedure TestRTTI_Class_Method;
  851. Procedure TestRTTI_Class_MethodArgFlags;
  852. Procedure TestRTTI_Class_MethodPrivate;
  853. Procedure TestRTTI_Class_Property;
  854. Procedure TestRTTI_Class_PropertyParams;
  855. Procedure TestRTTI_Class_PropertyPrivate;
  856. Procedure TestRTTI_Class_ClassProperty;
  857. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  858. Procedure TestRTTI_Class_OmitRTTI;
  859. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  860. Procedure TestRTTI_IndexModifier;
  861. Procedure TestRTTI_StoredModifier;
  862. Procedure TestRTTI_DefaultValue;
  863. Procedure TestRTTI_DefaultValueSet;
  864. Procedure TestRTTI_DefaultValueRangeType;
  865. Procedure TestRTTI_DefaultValueInherit;
  866. Procedure TestRTTI_OverrideMethod;
  867. Procedure TestRTTI_ReintroduceMethod;
  868. Procedure TestRTTI_OverloadProperty;
  869. // ToDo: array argument
  870. Procedure TestRTTI_ClassForward;
  871. Procedure TestRTTI_ClassOf;
  872. Procedure TestRTTI_Record;
  873. Procedure TestRTTI_RecordAnonymousArray;
  874. Procedure TestRTTI_Record_ClassVarType;
  875. Procedure TestRTTI_LocalTypes;
  876. Procedure TestRTTI_TypeInfo_BaseTypes;
  877. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  878. Procedure TestRTTI_TypeInfo_LocalFail;
  879. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  880. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  881. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  882. Procedure TestRTTI_TypeInfo_FunctionClassType;
  883. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  884. Procedure TestRTTI_Interface_Corba;
  885. Procedure TestRTTI_Interface_COM;
  886. Procedure TestRTTI_ClassHelper;
  887. Procedure TestRTTI_ExternalClass;
  888. Procedure TestRTTI_Unit;
  889. // Resourcestring
  890. Procedure TestResourcestringProgram;
  891. Procedure TestResourcestringUnit;
  892. Procedure TestResourcestringImplementation;
  893. // Attributes
  894. Procedure TestAttributes_Members;
  895. Procedure TestAttributes_Types;
  896. Procedure TestAttributes_HelperConstructor_Fail;
  897. Procedure TestAttributes_InterfacesList;
  898. // Assertions, checks
  899. Procedure TestAssert;
  900. Procedure TestAssert_SysUtils;
  901. Procedure TestObjectChecks;
  902. Procedure TestOverflowChecks_Int;
  903. Procedure TestRangeChecks_AssignInt;
  904. Procedure TestRangeChecks_AssignIntRange;
  905. Procedure TestRangeChecks_AssignEnum;
  906. Procedure TestRangeChecks_AssignEnumRange;
  907. Procedure TestRangeChecks_AssignChar;
  908. Procedure TestRangeChecks_AssignCharRange;
  909. Procedure TestRangeChecks_ArrayIndex;
  910. Procedure TestRangeChecks_ArrayOfRecIndex;
  911. Procedure TestRangeChecks_StringIndex;
  912. Procedure TestRangeChecks_TypecastInt;
  913. Procedure TestRangeChecks_TypeHelperInt;
  914. Procedure TestRangeChecks_AssignCurrency;
  915. // Async/AWait
  916. Procedure TestAsync_Proc;
  917. Procedure TestAsync_CallFuncResultIsPromise;
  918. Procedure TestAsync_CallProcResultIsPromise;
  919. Procedure TestAsync_ConstructorFail;
  920. Procedure TestAsync_PropertyGetterFail;
  921. Procedure TestAwait_NonPromiseWithTypeFail;
  922. Procedure TestAwait_AsyncCallTypeMismatch;
  923. Procedure TestAWait_OutsideAsyncFail;
  924. Procedure TestAWait_IntegerFail;
  925. Procedure TestAWait_ExternalClassPromise;
  926. Procedure TestAWait_JSValue;
  927. Procedure TestAWait_Result;
  928. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  929. Procedure TestAsync_AnonymousProc;
  930. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  931. Procedure TestAsync_ProcType;
  932. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  933. Procedure TestAsync_ProcTypeDelphi_NoTJSPromise;
  934. Procedure TestAsync_ProcTypeDelphi_TJSPromise;
  935. Procedure TestAsync_Inherited;
  936. Procedure TestAsync_ClassInterface;
  937. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  938. Procedure TestAWait_ClassAs;
  939. // Library
  940. Procedure TestLibrary_Empty;
  941. Procedure TestLibrary_ExportFunc;
  942. Procedure TestLibrary_ExportFuncOverloadedFail;
  943. Procedure TestLibrary_Export_Index_Fail;
  944. Procedure TestLibrary_ExportVar;
  945. Procedure TestLibrary_ExportUnitFunc;
  946. end;
  947. function LinesToStr(Args: array of const): string;
  948. function ExtractFileUnitName(aFilename: string): string;
  949. function JSToStr(El: TJSElement): string;
  950. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  951. implementation
  952. function LinesToStr(Args: array of const): string;
  953. var
  954. s: String;
  955. i: Integer;
  956. begin
  957. s:='';
  958. for i:=Low(Args) to High(Args) do
  959. case Args[i].VType of
  960. vtChar: s += Args[i].VChar+LineEnding;
  961. vtString: s += Args[i].VString^+LineEnding;
  962. vtPChar: s += Args[i].VPChar+LineEnding;
  963. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  964. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  965. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  966. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  967. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  968. end;
  969. Result:=s;
  970. end;
  971. function ExtractFileUnitName(aFilename: string): string;
  972. var
  973. p: Integer;
  974. begin
  975. Result:=ExtractFileName(aFilename);
  976. if Result='' then exit;
  977. for p:=length(Result) downto 1 do
  978. case Result[p] of
  979. '/','\': exit;
  980. '.':
  981. begin
  982. Delete(Result,p,length(Result));
  983. exit;
  984. end;
  985. end;
  986. end;
  987. function JSToStr(El: TJSElement): string;
  988. var
  989. aWriter: TBufferWriter;
  990. aJSWriter: TJSWriter;
  991. begin
  992. aJSWriter:=nil;
  993. aWriter:=TBufferWriter.Create(1000);
  994. try
  995. aJSWriter:=TJSWriter.Create(aWriter);
  996. aJSWriter.IndentSize:=2;
  997. aJSWriter.WriteJS(El);
  998. Result:=aWriter.AsString;
  999. finally
  1000. aJSWriter.Free;
  1001. aWriter.Free;
  1002. end;
  1003. end;
  1004. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  1005. // search diff, ignore changes in spaces
  1006. const
  1007. SpaceChars = [#9,#10,#13,' '];
  1008. var
  1009. ExpectedP, ActualP: PChar;
  1010. function FindLineEnd(p: PChar): PChar;
  1011. begin
  1012. Result:=p;
  1013. while not (Result^ in [#0,#10,#13]) do inc(Result);
  1014. end;
  1015. function FindLineStart(p, MinP: PChar): PChar;
  1016. begin
  1017. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  1018. Result:=p;
  1019. end;
  1020. procedure SkipLineEnd(var p: PChar);
  1021. begin
  1022. if p^ in [#10,#13] then
  1023. begin
  1024. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1025. inc(p,2)
  1026. else
  1027. inc(p);
  1028. end;
  1029. end;
  1030. function HasSpecialChar(s: string): boolean;
  1031. var
  1032. i: Integer;
  1033. begin
  1034. for i:=1 to length(s) do
  1035. if s[i] in [#0..#31,#127..#255] then
  1036. exit(true);
  1037. Result:=false;
  1038. end;
  1039. function HashSpecialChars(s: string): string;
  1040. var
  1041. i: Integer;
  1042. begin
  1043. Result:='';
  1044. for i:=1 to length(s) do
  1045. if s[i] in [#0..#31,#127..#255] then
  1046. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1047. else
  1048. Result:=Result+s[i];
  1049. end;
  1050. procedure DiffFound;
  1051. var
  1052. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1053. ExpLine, ActLine: String;
  1054. i, LineNo, DiffLineNo: Integer;
  1055. begin
  1056. writeln('Diff found "',Msg,'". Lines:');
  1057. // write correct lines
  1058. p:=PChar(Expected);
  1059. LineNo:=0;
  1060. DiffLineNo:=0;
  1061. repeat
  1062. StartPos:=p;
  1063. while not (p^ in [#0,#10,#13]) do inc(p);
  1064. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1065. SkipLineEnd(p);
  1066. inc(LineNo);
  1067. if (p<=ExpectedP) and (p^<>#0) then
  1068. begin
  1069. writeln('= ',ExpLine);
  1070. end else begin
  1071. // diff line
  1072. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1073. // write actual line
  1074. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1075. ActLineEndP:=FindLineEnd(ActualP);
  1076. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1077. writeln('- ',ActLine);
  1078. if HasSpecialChar(ActLine) then
  1079. writeln('- ',HashSpecialChars(ActLine));
  1080. // write expected line
  1081. writeln('+ ',ExpLine);
  1082. if HasSpecialChar(ExpLine) then
  1083. writeln('- ',HashSpecialChars(ExpLine));
  1084. // write empty line with pointer ^
  1085. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1086. writeln('^');
  1087. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1088. CheckSrcDiff:=false;
  1089. // write up to three following actual lines to get some context
  1090. for i:=1 to 3 do begin
  1091. ActLineStartP:=ActLineEndP;
  1092. SkipLineEnd(ActLineStartP);
  1093. if ActLineStartP^=#0 then break;
  1094. ActLineEndP:=FindLineEnd(ActLineStartP);
  1095. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1096. writeln('~ ',ActLine);
  1097. end;
  1098. exit;
  1099. end;
  1100. until p^=#0;
  1101. writeln('DiffFound Actual:-----------------------');
  1102. writeln(Actual);
  1103. writeln('DiffFound Expected:---------------------');
  1104. writeln(Expected);
  1105. writeln('DiffFound ------------------------------');
  1106. Msg:='diff found, but lines are the same, internal error';
  1107. CheckSrcDiff:=false;
  1108. end;
  1109. var
  1110. IsSpaceNeeded: Boolean;
  1111. LastChar, Quote: Char;
  1112. begin
  1113. Result:=true;
  1114. Msg:='';
  1115. if Expected='' then Expected:=' ';
  1116. if Actual='' then Actual:=' ';
  1117. ExpectedP:=PChar(Expected);
  1118. ActualP:=PChar(Actual);
  1119. repeat
  1120. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1121. case ExpectedP^ of
  1122. #0:
  1123. begin
  1124. // check that rest of Actual has only spaces
  1125. while ActualP^ in SpaceChars do inc(ActualP);
  1126. if ActualP^<>#0 then
  1127. begin
  1128. DiffFound;
  1129. exit;
  1130. end;
  1131. exit(true);
  1132. end;
  1133. ' ',#9,#10,#13:
  1134. begin
  1135. // skip space in Expected
  1136. IsSpaceNeeded:=false;
  1137. if ExpectedP>PChar(Expected) then
  1138. LastChar:=ExpectedP[-1]
  1139. else
  1140. LastChar:=#0;
  1141. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1142. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1143. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1144. IsSpaceNeeded:=true;
  1145. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1146. begin
  1147. DiffFound;
  1148. exit;
  1149. end;
  1150. while ActualP^ in SpaceChars do inc(ActualP);
  1151. end;
  1152. '''','"':
  1153. begin
  1154. while ActualP^ in SpaceChars do inc(ActualP);
  1155. if ExpectedP^<>ActualP^ then
  1156. begin
  1157. DiffFound;
  1158. exit;
  1159. end;
  1160. Quote:=ExpectedP^;
  1161. repeat
  1162. inc(ExpectedP);
  1163. inc(ActualP);
  1164. if ExpectedP^<>ActualP^ then
  1165. begin
  1166. DiffFound;
  1167. exit;
  1168. end;
  1169. if (ExpectedP^ in [#0,#10,#13]) then
  1170. break
  1171. else if (ExpectedP^=Quote) then
  1172. begin
  1173. inc(ExpectedP);
  1174. inc(ActualP);
  1175. break;
  1176. end;
  1177. until false;
  1178. end;
  1179. else
  1180. while ActualP^ in SpaceChars do inc(ActualP);
  1181. if ExpectedP^<>ActualP^ then
  1182. begin
  1183. DiffFound;
  1184. exit;
  1185. end;
  1186. inc(ExpectedP);
  1187. inc(ActualP);
  1188. end;
  1189. until false;
  1190. end;
  1191. { TTestEnginePasResolver }
  1192. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1193. begin
  1194. if FModule=AValue then Exit;
  1195. FModule:=AValue;
  1196. end;
  1197. destructor TTestEnginePasResolver.Destroy;
  1198. begin
  1199. FreeAndNil(FStreamResolver);
  1200. FreeAndNil(FParser);
  1201. FreeAndNil(FScanner);
  1202. FreeAndNil(FStreamResolver);
  1203. Module:=nil;
  1204. inherited Destroy;
  1205. end;
  1206. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1207. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1208. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1209. begin
  1210. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1211. TypeParams);
  1212. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1213. Module:=TPasModule(Result);
  1214. end;
  1215. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1216. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1217. begin
  1218. Result:=nil;
  1219. if InFilename<>'' then
  1220. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1221. if Assigned(OnFindUnit) then
  1222. Result:=OnFindUnit(AName);
  1223. if NameExpr=nil then ;
  1224. end;
  1225. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1226. begin
  1227. // do not parse recursively
  1228. // parse via the queue
  1229. if Section=nil then ;
  1230. end;
  1231. { TCustomTestModule }
  1232. procedure TCustomTestModule.FreeSrcMarkers;
  1233. var
  1234. aMarker, Last: PSrcMarker;
  1235. begin
  1236. aMarker:=FirstSrcMarker;
  1237. while aMarker<>nil do
  1238. begin
  1239. Last:=aMarker;
  1240. aMarker:=aMarker^.Next;
  1241. Dispose(Last);
  1242. end;
  1243. FirstSrcMarker:=nil;
  1244. LastSrcMarker:=nil;
  1245. end;
  1246. function TCustomTestModule.GetResolverCount: integer;
  1247. begin
  1248. Result:=FResolvers.Count;
  1249. end;
  1250. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1251. begin
  1252. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1253. end;
  1254. function TCustomTestModule.GetMsgCount: integer;
  1255. begin
  1256. Result:=FHintMsgs.Count;
  1257. end;
  1258. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1259. begin
  1260. Result:=TTestHintMessage(FHintMsgs[Index]);
  1261. end;
  1262. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1263. ): TPasModule;
  1264. var
  1265. DefNamespace: String;
  1266. begin
  1267. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1268. if (Pos('.',aUnitName)<1) then
  1269. begin
  1270. DefNamespace:=GetDefaultNamespace;
  1271. if DefNamespace<>'' then
  1272. begin
  1273. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1274. if Result<>nil then exit;
  1275. end;
  1276. end;
  1277. Result:=LoadUnit(aUnitName);
  1278. if Result<>nil then exit;
  1279. {$IFDEF VerbosePas2JS}
  1280. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1281. {$ENDIF}
  1282. Fail('can''t find unit "'+aUnitName+'"');
  1283. end;
  1284. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1285. var
  1286. aParser: TPasParser;
  1287. Item: TTestHintMessage;
  1288. begin
  1289. aParser:=Sender as TPasParser;
  1290. Item:=TTestHintMessage.Create;
  1291. Item.Id:=aParser.LastMsgNumber;
  1292. Item.MsgType:=aParser.LastMsgType;
  1293. Item.MsgNumber:=aParser.LastMsgNumber;
  1294. Item.Msg:=Msg;
  1295. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1296. {$IFDEF VerbosePas2JS}
  1297. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1298. {$ENDIF}
  1299. FHintMsgs.Add(Item);
  1300. end;
  1301. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1302. );
  1303. var
  1304. aResolver: TTestEnginePasResolver;
  1305. Item: TTestHintMessage;
  1306. begin
  1307. aResolver:=Sender as TTestEnginePasResolver;
  1308. Item:=TTestHintMessage.Create;
  1309. Item.Id:=aResolver.LastMsgId;
  1310. Item.MsgType:=aResolver.LastMsgType;
  1311. Item.MsgNumber:=aResolver.LastMsgNumber;
  1312. Item.Msg:=Msg;
  1313. Item.SourcePos:=aResolver.LastSourcePos;
  1314. {$IFDEF VerbosePas2JS}
  1315. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1316. {$ENDIF}
  1317. FHintMsgs.Add(Item);
  1318. end;
  1319. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1320. var
  1321. Item: TTestHintMessage;
  1322. aScanner: TPas2jsPasScanner;
  1323. begin
  1324. aScanner:=Sender as TPas2jsPasScanner;
  1325. Item:=TTestHintMessage.Create;
  1326. Item.Id:=aScanner.LastMsgNumber;
  1327. Item.MsgType:=aScanner.LastMsgType;
  1328. Item.MsgNumber:=aScanner.LastMsgNumber;
  1329. Item.Msg:=Msg;
  1330. Item.SourcePos:=aScanner.CurSourcePos;
  1331. {$IFDEF VerbosePas2JS}
  1332. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1333. {$ENDIF}
  1334. FHintMsgs.Add(Item);
  1335. end;
  1336. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1337. var
  1338. SubEl: TPasElement;
  1339. i: Integer;
  1340. procedure E(Msg: string);
  1341. var
  1342. s: String;
  1343. begin
  1344. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1345. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1346. writeln('ERROR: ',s);
  1347. Fail(s);
  1348. end;
  1349. begin
  1350. if arg=nil then ;
  1351. if El=nil then exit;
  1352. if El.Parent=El then
  1353. E('El.Parent=El='+GetObjName(El));
  1354. if El is TBinaryExpr then
  1355. begin
  1356. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1357. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1358. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1359. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1360. end
  1361. else if El is TParamsExpr then
  1362. begin
  1363. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1364. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1365. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1366. if TParamsExpr(El).Params[i].Parent<>El then
  1367. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1368. end
  1369. else if El is TProcedureExpr then
  1370. begin
  1371. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1372. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1373. end
  1374. else if El is TPasDeclarations then
  1375. begin
  1376. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1377. begin
  1378. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1379. if SubEl.Parent<>El then
  1380. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1381. end;
  1382. end
  1383. else if El is TPasImplBlock then
  1384. begin
  1385. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1386. begin
  1387. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1388. if SubEl.Parent<>El then
  1389. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1390. end;
  1391. end
  1392. else if El is TPasImplWithDo then
  1393. begin
  1394. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1395. begin
  1396. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1397. if SubEl.Parent<>El then
  1398. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1399. end;
  1400. end
  1401. else if El is TPasProcedure then
  1402. begin
  1403. if TPasProcedure(El).ProcType.Parent<>El then
  1404. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1405. end
  1406. else if El is TPasProcedureType then
  1407. begin
  1408. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1409. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1410. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1411. end;
  1412. end;
  1413. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1414. var
  1415. Data: PTestResolverReferenceData absolute FindData;
  1416. Line, Col: integer;
  1417. begin
  1418. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1419. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1420. if (Data^.Filename=El.SourceFilename)
  1421. and (Data^.Row=Line)
  1422. and (Data^.StartCol<=Col)
  1423. and (Data^.EndCol>=Col)
  1424. then
  1425. Data^.Found.Add(El);
  1426. end;
  1427. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1428. begin
  1429. if FWithTypeInfo=AValue then Exit;
  1430. FWithTypeInfo:=AValue;
  1431. if AValue then
  1432. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1433. else
  1434. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1435. end;
  1436. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1437. var
  1438. i: Integer;
  1439. CurEngine: TTestEnginePasResolver;
  1440. CurUnitName: String;
  1441. begin
  1442. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1443. Result:=nil;
  1444. if (Module.ClassType=TPasModule)
  1445. and (CompareText(Module.Name,aUnitName)=0) then
  1446. exit(Module);
  1447. for i:=0 to ResolverCount-1 do
  1448. begin
  1449. CurEngine:=Resolvers[i];
  1450. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1451. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1452. if CompareText(aUnitName,CurUnitName)=0 then
  1453. begin
  1454. Result:=CurEngine.Module;
  1455. if Result<>nil then exit;
  1456. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1457. FileResolver.FindSourceFile(aUnitName);
  1458. CurEngine.StreamResolver:=TStreamResolver.Create;
  1459. CurEngine.StreamResolver.OwnsStreams:=True;
  1460. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1461. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1462. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1463. InitScanner(CurEngine.Scanner);
  1464. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1465. CurEngine.Parser.Options:=po_tcmodules;
  1466. if CompareText(CurUnitName,'System')=0 then
  1467. CurEngine.Parser.ImplicitUses.Clear;
  1468. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1469. try
  1470. CurEngine.Parser.NextToken;
  1471. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1472. except
  1473. on E: Exception do
  1474. HandleException(E);
  1475. end;
  1476. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1477. Result:=CurEngine.Module;
  1478. exit;
  1479. end;
  1480. end;
  1481. end;
  1482. procedure TCustomTestModule.SetUp;
  1483. begin
  1484. {$IFDEF EnablePasTreeGlobalRefCount}
  1485. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1486. {$ENDIF}
  1487. if FResolvers<>nil then
  1488. begin
  1489. writeln('TCustomTestModule.SetUp FModules<>nil');
  1490. Halt;
  1491. end;
  1492. inherited SetUp;
  1493. FSkipTests:=false;
  1494. FWithTypeInfo:=false;
  1495. FSource:=TStringList.Create;
  1496. FHub:=TPas2JSResolverHub.Create(Self);
  1497. FResolvers:=TObjectList.Create(true);
  1498. FFilename:='test1.pp';
  1499. FFileResolver:=TStreamResolver.Create;
  1500. FFileResolver.OwnsStreams:=True;
  1501. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1502. InitScanner(FScanner);
  1503. FEngine:=AddModule(Filename);
  1504. FEngine.Scanner:=FScanner;
  1505. FScanner.Resolver:=FEngine;
  1506. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1507. FParser.OnLog:=@OnParserLog;
  1508. FEngine.Parser:=FParser;
  1509. Parser.Options:=po_tcmodules;
  1510. FModule:=Nil;
  1511. FConverter:=CreateConverter;
  1512. FExpectedErrorClass:=nil;
  1513. end;
  1514. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1515. var
  1516. Options: TPasToJsConverterOptions;
  1517. begin
  1518. Result:=TPasToJSConverter.Create;
  1519. Options:=co_tcmodules;
  1520. if WithTypeInfo then
  1521. Exclude(Options,coNoTypeInfo)
  1522. else
  1523. Include(Options,coNoTypeInfo);
  1524. Result.Options:=Options;
  1525. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1526. end;
  1527. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1528. begin
  1529. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1530. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1531. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1532. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1533. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1534. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1535. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1536. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1537. aScanner.OnLog:=@OnScannerLog;
  1538. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1539. end;
  1540. procedure TCustomTestModule.TearDown;
  1541. {$IFDEF CheckPasTreeRefCount}
  1542. var
  1543. El: TPasElement;
  1544. {$ENDIF}
  1545. var
  1546. i: Integer;
  1547. CurModule: TPasModule;
  1548. begin
  1549. FreeSrcMarkers;
  1550. FHintMsgs.Clear;
  1551. FHintMsgsGood.Clear;
  1552. FSkipTests:=false;
  1553. FWithTypeInfo:=false;
  1554. FJSRegModuleCall:=nil;
  1555. FJSModuleCallArgs:=nil;
  1556. FJSImplentationUses:=nil;
  1557. FJSInterfaceUses:=nil;
  1558. FJSModuleSrc:=nil;
  1559. FJSInitBody:=nil;
  1560. FreeAndNil(FJSSource);
  1561. FreeAndNil(FJSModule);
  1562. FreeAndNil(FConverter);
  1563. ResolverEngine.Clear;
  1564. FreeAndNil(FSource);
  1565. FreeAndNil(FFileResolver);
  1566. if FResolvers<>nil then
  1567. begin
  1568. for i:=0 to FResolvers.Count-1 do
  1569. begin
  1570. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1571. if CurModule=nil then continue;
  1572. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1573. end;
  1574. for i:=0 to FResolvers.Count-1 do
  1575. begin
  1576. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1577. if CurModule=nil then continue;
  1578. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1579. end;
  1580. FreeAndNil(FResolvers);
  1581. FModule:=nil;
  1582. FEngine:=nil;
  1583. end;
  1584. FreeAndNil(FHub);
  1585. inherited TearDown;
  1586. {$IFDEF EnablePasTreeGlobalRefCount}
  1587. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1588. begin
  1589. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1590. {$IFDEF CheckPasTreeRefCount}
  1591. El:=TPasElement.FirstRefEl;
  1592. while El<>nil do
  1593. begin
  1594. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1595. for i:=0 to El.RefIds.Count-1 do
  1596. writeln(' ',El.RefIds[i]);
  1597. El:=El.NextRefEl;
  1598. end;
  1599. {$ENDIF}
  1600. Halt;
  1601. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1602. end;
  1603. {$ENDIF}
  1604. end;
  1605. procedure TCustomTestModule.Add(Line: string);
  1606. begin
  1607. Source.Add(Line);
  1608. end;
  1609. procedure TCustomTestModule.Add(const Lines: array of string);
  1610. var
  1611. i: Integer;
  1612. begin
  1613. for i:=low(Lines) to high(Lines) do
  1614. Add(Lines[i]);
  1615. end;
  1616. procedure TCustomTestModule.StartParsing;
  1617. var
  1618. Src: String;
  1619. begin
  1620. Src:=Source.Text;
  1621. FEngine.Source:=Src;
  1622. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1623. Scanner.OpenFile(FileName);
  1624. Writeln('// Test : ',Self.TestName);
  1625. Writeln(Src);
  1626. end;
  1627. procedure TCustomTestModule.ParseModuleQueue;
  1628. var
  1629. i: Integer;
  1630. CurResolver: TTestEnginePasResolver;
  1631. Found: Boolean;
  1632. Section: TPasSection;
  1633. begin
  1634. // parse til exception or all Resolvers finished
  1635. while not SkipTests do
  1636. begin
  1637. Found:=false;
  1638. for i:=0 to ResolverCount-1 do
  1639. begin
  1640. CurResolver:=Resolvers[i];
  1641. if CurResolver.CurrentParser=nil then continue;
  1642. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1643. continue;
  1644. CurResolver.Parser.ParseContinue;
  1645. Found:=true;
  1646. break;
  1647. end;
  1648. if not Found then break;
  1649. end;
  1650. for i:=0 to ResolverCount-1 do
  1651. begin
  1652. CurResolver:=Resolvers[i];
  1653. if CurResolver.Parser=nil then
  1654. begin
  1655. if CurResolver.CurrentParser<>nil then
  1656. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1657. continue;
  1658. end;
  1659. if CurResolver.Parser.CurModule<>nil then
  1660. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1661. end;
  1662. end;
  1663. procedure TCustomTestModule.ParseModule;
  1664. begin
  1665. if SkipTests then exit;
  1666. FFirstPasStatement:=nil;
  1667. try
  1668. StartParsing;
  1669. Parser.ParseMain(FModule);
  1670. ParseModuleQueue;
  1671. except
  1672. on E: Exception do
  1673. HandleException(E);
  1674. end;
  1675. if SkipTests then exit;
  1676. AssertNotNull('Module resulted in Module',Module);
  1677. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1678. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1679. end;
  1680. procedure TCustomTestModule.ParseProgram;
  1681. begin
  1682. if SkipTests then exit;
  1683. ParseModule;
  1684. if SkipTests then exit;
  1685. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1686. FPasProgram:=TPasProgram(Module);
  1687. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1688. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1689. if (PasProgram.InitializationSection.Elements.Count>0) then
  1690. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1691. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1692. end;
  1693. procedure TCustomTestModule.ParseLibrary;
  1694. var
  1695. Init: TInitializationSection;
  1696. begin
  1697. if SkipTests then exit;
  1698. ParseModule;
  1699. if SkipTests then exit;
  1700. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1701. FPasLibrary:=TPasLibrary(Module);
  1702. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1703. Init:=PasLibrary.InitializationSection;
  1704. if (Init<>nil) and (Init.Elements.Count>0) then
  1705. if TObject(Init.Elements[0]) is TPasImplBlock then
  1706. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1707. end;
  1708. procedure TCustomTestModule.ParseUnit;
  1709. begin
  1710. if SkipTests then exit;
  1711. ParseModule;
  1712. if SkipTests then exit;
  1713. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1714. AssertNotNull('Has interface section',Module.InterfaceSection);
  1715. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1716. if (Module.InitializationSection<>nil)
  1717. and (Module.InitializationSection.Elements.Count>0)
  1718. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1719. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1720. end;
  1721. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1722. ): TTestEnginePasResolver;
  1723. var
  1724. i: Integer;
  1725. begin
  1726. for i:=0 to ResolverCount-1 do
  1727. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1728. exit(Resolvers[i]);
  1729. Result:=nil;
  1730. end;
  1731. function TCustomTestModule.AddModule(aFilename: string
  1732. ): TTestEnginePasResolver;
  1733. begin
  1734. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1735. if FindModuleWithFilename(aFilename)<>nil then
  1736. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1737. Result:=TTestEnginePasResolver.Create;
  1738. Result.Filename:=aFilename;
  1739. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1740. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1741. Result.OnLog:=@OnPasResolverLog;
  1742. Result.Hub:=Hub;
  1743. FResolvers.Add(Result);
  1744. end;
  1745. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1746. ): TTestEnginePasResolver;
  1747. begin
  1748. Result:=AddModule(aFilename);
  1749. Result.Source:=Src;
  1750. end;
  1751. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1752. ImplementationSrc: string): TTestEnginePasResolver;
  1753. var
  1754. Src: String;
  1755. begin
  1756. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1757. Src+=LineEnding;
  1758. Src+='interface'+LineEnding;
  1759. Src+=LineEnding;
  1760. Src+=InterfaceSrc;
  1761. Src+='implementation'+LineEnding;
  1762. Src+=LineEnding;
  1763. Src+=ImplementationSrc;
  1764. Src+='end.'+LineEnding;
  1765. Result:=AddModuleWithSrc(aFilename,Src);
  1766. end;
  1767. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1768. var
  1769. Intf, Impl: TStringList;
  1770. begin
  1771. Intf:=TStringList.Create;
  1772. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1773. // unit interface
  1774. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1775. Intf.Add('{$modeswitch externalclass}');
  1776. Intf.Add('type');
  1777. Intf.Add(' integer=longint;');
  1778. Intf.Add(' sizeint=nativeint;');
  1779. //'const',
  1780. //' LineEnding = #10;',
  1781. //' DirectorySeparator = ''/'';',
  1782. //' DriveSeparator = '''';',
  1783. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1784. //' AllowDriveSeparators : set of char = [];',
  1785. if supTObject in Parts then
  1786. Intf.AddStrings([
  1787. 'type',
  1788. ' TClass = class of TObject;',
  1789. ' TObject = class',
  1790. ' constructor Create;',
  1791. ' destructor Destroy; virtual;',
  1792. ' class function ClassType: TClass; assembler;',
  1793. ' class function ClassName: String; assembler;',
  1794. ' class function ClassNameIs(const Name: string): boolean;',
  1795. ' class function ClassParent: TClass; assembler;',
  1796. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1797. ' class function UnitName: String; assembler;',
  1798. ' procedure AfterConstruction; virtual;',
  1799. ' procedure BeforeDestruction;virtual;',
  1800. ' function Equals(Obj: TObject): boolean; virtual;',
  1801. ' function ToString: String; virtual;',
  1802. ' end;']);
  1803. if supTInterfacedObject in Parts then
  1804. Intf.AddStrings([
  1805. ' {$Interfaces COM}',
  1806. ' IUnknown = interface',
  1807. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1808. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1809. ' function _AddRef: Integer;',
  1810. ' function _Release: Integer;',
  1811. ' end;',
  1812. ' IInterface = IUnknown;',
  1813. ' TInterfacedObject = class(TObject,IUnknown)',
  1814. ' protected',
  1815. ' fRefCount: Integer;',
  1816. ' { implement methods of IUnknown }',
  1817. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1818. ' function _AddRef: Integer; virtual;',
  1819. ' function _Release: Integer; virtual;',
  1820. ' end;',
  1821. ' TInterfacedClass = class of TInterfacedObject;',
  1822. '',
  1823. '']);
  1824. if supTVarRec in Parts then
  1825. Intf.AddStrings([
  1826. 'const',
  1827. ' vtInteger = 0;',
  1828. ' vtBoolean = 1;',
  1829. ' vtJSValue = 19;',
  1830. 'type',
  1831. ' PVarRec = ^TVarRec;',
  1832. ' TVarRec = record',
  1833. ' VType : byte;',
  1834. ' VJSValue: JSValue;',
  1835. ' vInteger: longint external name ''VJSValue'';',
  1836. ' vBoolean: boolean external name ''VJSValue'';',
  1837. ' end;',
  1838. ' TVarRecArray = array of TVarRec;',
  1839. 'function VarRecs: TVarRecArray; varargs;',
  1840. '']);
  1841. if supTypeInfo in Parts then
  1842. begin
  1843. Intf.AddStrings([
  1844. 'type',
  1845. ' TTypeKind = (',
  1846. ' tkUnknown, // 0',
  1847. ' tkInteger, // 1',
  1848. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1849. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1850. ' tkEnumeration, // 4',
  1851. ' tkSet, // 5',
  1852. ' tkDouble, // 6',
  1853. ' tkBool, // 7',
  1854. ' tkProcVar, // 8 function or procedure',
  1855. ' tkMethod, // 9 proc var of object',
  1856. ' tkArray, // 10 static array',
  1857. ' tkDynArray, // 11',
  1858. ' tkRecord, // 12',
  1859. ' tkClass, // 13',
  1860. ' tkClassRef, // 14',
  1861. ' tkPointer, // 15',
  1862. ' tkJSValue, // 16',
  1863. ' tkRefToProcVar, // 17 variable of procedure type',
  1864. ' tkInterface, // 18',
  1865. ' //tkObject,',
  1866. ' //tkSString,tkLString,tkAString,tkWString,',
  1867. ' //tkVariant,',
  1868. ' //tkWChar,',
  1869. ' //tkInt64,',
  1870. ' //tkQWord,',
  1871. ' //tkInterfaceRaw,',
  1872. ' //tkUString,tkUChar,',
  1873. ' tkHelper, // 19',
  1874. ' //tkFile,',
  1875. ' tkExtClass // 20',
  1876. ' );',
  1877. ' TTypeKinds = set of TTypeKind;',
  1878. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1879. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1880. ' end;',
  1881. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1882. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1883. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1884. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1885. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1886. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1887. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1888. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1889. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1890. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1891. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1892. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1893. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1894. '']);
  1895. end;
  1896. if supWriteln in Parts then
  1897. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1898. Intf.Add('var');
  1899. Intf.Add(' ExitCode: Longint = 0;');
  1900. // unit implementation
  1901. Impl:=TStringList.Create;
  1902. if supTObject in Parts then
  1903. Impl.AddStrings([
  1904. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1905. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1906. 'asm',
  1907. 'end;',
  1908. 'constructor TObject.Create; begin end;',
  1909. 'destructor TObject.Destroy; begin end;',
  1910. 'class function TObject.ClassType: TClass; assembler;',
  1911. 'asm',
  1912. 'end;',
  1913. 'class function TObject.ClassName: String; assembler;',
  1914. 'asm',
  1915. 'end;',
  1916. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1917. 'begin',
  1918. ' Result:=SameText(Name,ClassName);',
  1919. 'end;',
  1920. 'class function TObject.ClassParent: TClass; assembler;',
  1921. 'asm',
  1922. 'end;',
  1923. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1924. 'asm',
  1925. 'end;',
  1926. 'class function TObject.UnitName: String; assembler;',
  1927. 'asm',
  1928. 'end;',
  1929. 'procedure TObject.AfterConstruction; begin end;',
  1930. 'procedure TObject.BeforeDestruction; begin end;',
  1931. 'function TObject.Equals(Obj: TObject): boolean;',
  1932. 'begin',
  1933. ' Result:=Obj=Self;',
  1934. 'end;',
  1935. 'function TObject.ToString: String;',
  1936. 'begin',
  1937. ' Result:=ClassName;',
  1938. 'end;'
  1939. ]);
  1940. if supTInterfacedObject in Parts then
  1941. Impl.AddStrings([
  1942. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1943. //'begin',
  1944. //'end;',
  1945. 'function TInterfacedObject._AddRef: Integer;',
  1946. 'begin',
  1947. 'end;',
  1948. 'function TInterfacedObject._Release: Integer;',
  1949. 'begin',
  1950. 'end;',
  1951. '']);
  1952. if supTVarRec in Parts then
  1953. Impl.AddStrings([
  1954. 'function VarRecs: TVarRecArray; varargs;',
  1955. 'var',
  1956. ' v: PVarRec;',
  1957. 'begin',
  1958. ' v^.VType:=1;',
  1959. ' v^.VJSValue:=2;',
  1960. 'end;',
  1961. '']);
  1962. try
  1963. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1964. finally
  1965. Intf.Free;
  1966. Impl.Free;
  1967. end;
  1968. end;
  1969. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1970. SystemUnitParts: TSystemUnitParts);
  1971. begin
  1972. if NeedSystemUnit then
  1973. AddSystemUnit(SystemUnitParts)
  1974. else
  1975. Parser.ImplicitUses.Clear;
  1976. Add('program '+ExtractFileUnitName(Filename)+';');
  1977. Add('');
  1978. end;
  1979. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1980. SystemUnitParts: TSystemUnitParts);
  1981. begin
  1982. if NeedSystemUnit then
  1983. AddSystemUnit(SystemUnitParts)
  1984. else
  1985. Parser.ImplicitUses.Clear;
  1986. Add('library '+ExtractFileUnitName(Filename)+';');
  1987. Add('');
  1988. end;
  1989. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1990. SystemUnitParts: TSystemUnitParts);
  1991. begin
  1992. if NeedSystemUnit then
  1993. AddSystemUnit(SystemUnitParts)
  1994. else
  1995. Parser.ImplicitUses.Clear;
  1996. Add('unit Test1;');
  1997. Add('');
  1998. end;
  1999. procedure TCustomTestModule.ConvertModule;
  2000. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  2001. out UsesLit: TJSArrayLiteral);
  2002. var
  2003. i: Integer;
  2004. Item: TJSElement;
  2005. Lit: TJSLiteral;
  2006. begin
  2007. UsesLit:=nil;
  2008. AssertNotNull(UsesName+' uses section',Arg.Expr);
  2009. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  2010. exit; // null is ok
  2011. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  2012. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  2013. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  2014. begin
  2015. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  2016. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  2017. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  2018. Lit:=TJSLiteral(Item);
  2019. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2020. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2021. end;
  2022. end;
  2023. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2024. out Src: TJSSourceElements);
  2025. var
  2026. FunDecl: TJSFunctionDeclarationStatement;
  2027. FunDef: TJSFuncDef;
  2028. FunBody: TJSFunctionBody;
  2029. begin
  2030. Src:=nil;
  2031. AssertNotNull(ParamName,Arg.Expr);
  2032. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2033. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2034. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2035. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2036. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2037. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2038. AssertNotNull(ParamName+' body',FunDef.Body);
  2039. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2040. FunBody:=FunDef.Body as TJSFunctionBody;
  2041. AssertNotNull(ParamName+' body.A',FunBody.A);
  2042. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2043. Src:=FunBody.A as TJSSourceElements;
  2044. end;
  2045. var
  2046. ModuleNameExpr: TJSLiteral;
  2047. InitFunction: TJSFunctionDeclarationStatement;
  2048. InitAssign: TJSSimpleAssignStatement;
  2049. InitName: String;
  2050. LastNode, FirstNode: TJSElement;
  2051. Arg: TJSArrayLiteralElement;
  2052. IsProg, IsLib: Boolean;
  2053. begin
  2054. if SkipTests then exit;
  2055. IsProg:=false;
  2056. IsLib:=false;
  2057. if Module is TPasProgram then
  2058. IsProg:=true
  2059. else if Module is TPasLibrary then
  2060. IsLib:=true;
  2061. try
  2062. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2063. except
  2064. on E: Exception do
  2065. HandleException(E);
  2066. end;
  2067. if SkipTests then exit;
  2068. if ExpectedErrorClass<>nil then
  2069. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2070. FJSSource:=TStringList.Create;
  2071. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2072. {$IFDEF VerbosePas2JS}
  2073. writeln('TTestModule.ConvertModule JS:');
  2074. write(FJSSource.Text);
  2075. {$ENDIF}
  2076. // rtl.module(...
  2077. if JSModule.Statements.Count<1 then
  2078. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2079. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2080. AssertNotNull('register module call',FirstNode);
  2081. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2082. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2083. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2084. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2085. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2086. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2087. // parameter 'unitname'
  2088. if JSModuleCallArgs.Elements.Count<1 then
  2089. Fail('rtl.module first param unit missing');
  2090. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2091. AssertNotNull('module name param',Arg.Expr);
  2092. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2093. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2094. if IsProg then
  2095. begin
  2096. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2097. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2098. end
  2099. else if IsLib then
  2100. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2101. else
  2102. begin
  2103. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2104. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2105. end;
  2106. // main uses section
  2107. if JSModuleCallArgs.Elements.Count<2 then
  2108. Fail('rtl.module second param main uses missing');
  2109. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2110. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2111. // program/library/interface function()
  2112. if JSModuleCallArgs.Elements.Count<3 then
  2113. Fail('rtl.module third param intf-function missing');
  2114. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2115. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2116. // search for $mod.$init or $mod.$main - the last statement
  2117. if IsProg or IsLib then
  2118. begin
  2119. InitName:='$main';
  2120. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2121. end
  2122. else
  2123. InitName:='$init';
  2124. InitAssign:=nil;
  2125. InitFunction:=nil;
  2126. FJSInitBody:=nil;
  2127. if JSModuleSrc.Statements.Count>0 then
  2128. begin
  2129. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2130. if LastNode is TJSSimpleAssignStatement then
  2131. begin
  2132. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2133. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2134. begin
  2135. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2136. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2137. end
  2138. else if IsProg or IsLib then
  2139. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2140. end;
  2141. end;
  2142. // optional: implementation uses section
  2143. if JSModuleCallArgs.Elements.Count<4 then
  2144. exit;
  2145. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2146. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2147. end;
  2148. procedure TCustomTestModule.ConvertProgram;
  2149. begin
  2150. Add('end.');
  2151. ParseProgram;
  2152. ConvertModule;
  2153. end;
  2154. procedure TCustomTestModule.ConvertLibrary;
  2155. begin
  2156. Add('end.');
  2157. ParseLibrary;
  2158. ConvertModule;
  2159. end;
  2160. procedure TCustomTestModule.ConvertUnit;
  2161. begin
  2162. Add('end.');
  2163. ParseUnit;
  2164. ConvertModule;
  2165. end;
  2166. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2167. begin
  2168. Result:=tcmodules.JSToStr(El);
  2169. end;
  2170. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2171. DottedName: string);
  2172. begin
  2173. if DottedName='' then
  2174. begin
  2175. AssertNull(Msg,El);
  2176. end
  2177. else
  2178. begin
  2179. AssertNotNull(Msg,El);
  2180. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2181. end;
  2182. end;
  2183. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2184. begin
  2185. if El=nil then
  2186. Result:=''
  2187. else if El is TJSPrimaryExpressionIdent then
  2188. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2189. else if El is TJSDotMemberExpression then
  2190. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2191. else
  2192. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2193. end;
  2194. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2195. InitStatements: string; ImplStatements: string);
  2196. var
  2197. ActualSrc, ExpectedSrc, InitName: String;
  2198. IsProg, IsLib: Boolean;
  2199. begin
  2200. ActualSrc:=JSToStr(JSModuleSrc);
  2201. if coUseStrict in Converter.Options then
  2202. ExpectedSrc:='"use strict";'+LineEnding
  2203. else
  2204. ExpectedSrc:='';
  2205. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2206. ExpectedSrc:=ExpectedSrc+Statements;
  2207. // unit implementation
  2208. if (Trim(ImplStatements)<>'') then
  2209. ExpectedSrc:=ExpectedSrc+LineEnding
  2210. +'$mod.$implcode = function () {'+LineEnding
  2211. +ImplStatements
  2212. +'};'+LineEnding;
  2213. // program main or unit initialization
  2214. IsProg:=false;
  2215. IsLib:=false;
  2216. if Module is TPasProgram then
  2217. IsProg:=true
  2218. else if Module is TPasLibrary then
  2219. IsLib:=true;
  2220. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2221. begin
  2222. if IsProg or IsLib then
  2223. InitName:='$main'
  2224. else
  2225. InitName:='$init';
  2226. ExpectedSrc:=ExpectedSrc+LineEnding
  2227. +'$mod.'+InitName+' = function () {'+LineEnding
  2228. +InitStatements
  2229. +'};'+LineEnding;
  2230. end;
  2231. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2232. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2233. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2234. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2235. end;
  2236. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2237. var
  2238. ActualSrc: String;
  2239. begin
  2240. ActualSrc:=JSToStr(JSModule);
  2241. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2242. end;
  2243. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2244. // search diff, ignore changes in spaces
  2245. var
  2246. s: string;
  2247. begin
  2248. if CheckSrcDiff(Expected,Actual,s) then exit;
  2249. Fail(Msg+': '+s);
  2250. end;
  2251. procedure TCustomTestModule.CheckUnit(aFilename, ExpectedSrc: string);
  2252. var
  2253. aResolver: TTestEnginePasResolver;
  2254. aConverter: TPasToJSConverter;
  2255. aJSModule: TJSSourceElements;
  2256. ActualSrc: String;
  2257. begin
  2258. aResolver:=GetResolver(aFilename);
  2259. AssertNotNull('missing resolver of unit '+aFilename,aResolver);
  2260. AssertNotNull('missing resolver.module of unit '+aFilename,aResolver.Module);
  2261. {$IFDEF VerbosePas2JS}
  2262. writeln('CheckUnit '+aFilename+' converting ...');
  2263. {$ENDIF}
  2264. aConverter:=CreateConverter;
  2265. aJSModule:=nil;
  2266. try
  2267. try
  2268. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2269. except
  2270. on E: Exception do
  2271. HandleException(E);
  2272. end;
  2273. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2274. {$IFDEF VerbosePas2JS}
  2275. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2276. write(aResolver.Source);
  2277. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2278. write(ActualSrc);
  2279. {$ENDIF}
  2280. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2281. finally
  2282. aJSModule.Free;
  2283. aConverter.Free;
  2284. end;
  2285. end;
  2286. procedure TCustomTestModule.CheckReferenceDirectives;
  2287. var
  2288. CurFilename: string;
  2289. LineNumber: Integer;
  2290. SrcLine: String;
  2291. CommentStartP, CommentEndP: PChar;
  2292. procedure RaiseError(Msg: string; p: PChar);
  2293. begin
  2294. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2295. end;
  2296. procedure AddMarker(Marker: PSrcMarker);
  2297. begin
  2298. if LastSrcMarker<>nil then
  2299. LastSrcMarker^.Next:=Marker
  2300. else
  2301. FirstSrcMarker:=Marker;
  2302. LastSrcMarker:=Marker;
  2303. end;
  2304. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2305. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2306. begin
  2307. New(Result);
  2308. Result^.Kind:=Kind;
  2309. Result^.Filename:=aFilename;
  2310. Result^.Row:=aLine;
  2311. Result^.StartCol:=aStartCol;
  2312. Result^.EndCol:=aEndCol;
  2313. Result^.Identifier:=Identifier;
  2314. Result^.Next:=nil;
  2315. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2316. AddMarker(Result);
  2317. end;
  2318. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2319. const Identifier: string): PSrcMarker;
  2320. var
  2321. TokenStart, p: PChar;
  2322. begin
  2323. p:=CommentEndP;
  2324. ReadNextPascalToken(p,TokenStart,false,false);
  2325. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2326. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2327. end;
  2328. function ReadIdentifier(var p: PChar): string;
  2329. var
  2330. StartP: PChar;
  2331. begin
  2332. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2333. RaiseError('identifier expected',p);
  2334. StartP:=p;
  2335. inc(p);
  2336. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2337. Result:='';
  2338. SetLength(Result,p-StartP);
  2339. Move(StartP^,Result[1],length(Result));
  2340. end;
  2341. procedure AddLabel;
  2342. var
  2343. Identifier: String;
  2344. p: PChar;
  2345. begin
  2346. p:=CommentStartP+2;
  2347. Identifier:=ReadIdentifier(p);
  2348. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2349. if FindSrcLabel(Identifier)<>nil then
  2350. RaiseError('duplicate label "'+Identifier+'"',p);
  2351. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2352. end;
  2353. procedure AddResolverReference;
  2354. var
  2355. Identifier: String;
  2356. p: PChar;
  2357. begin
  2358. p:=CommentStartP+2;
  2359. Identifier:=ReadIdentifier(p);
  2360. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2361. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2362. end;
  2363. procedure AddDirectReference;
  2364. var
  2365. Identifier: String;
  2366. p: PChar;
  2367. begin
  2368. p:=CommentStartP+2;
  2369. Identifier:=ReadIdentifier(p);
  2370. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2371. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2372. end;
  2373. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2374. var
  2375. p: PChar;
  2376. IsDirective: Boolean;
  2377. begin
  2378. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2379. CurFilename:=aFilename;
  2380. // parse code, find all labels
  2381. LineNumber:=0;
  2382. while LineNumber<SrcLines.Count do
  2383. begin
  2384. inc(LineNumber);
  2385. SrcLine:=SrcLines[LineNumber-1];
  2386. if SrcLine='' then continue;
  2387. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2388. p:=PChar(SrcLine);
  2389. repeat
  2390. case p^ of
  2391. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2392. '{':
  2393. begin
  2394. CommentStartP:=p;
  2395. inc(p);
  2396. IsDirective:=p^ in ['#','@','='];
  2397. // skip to end of comment
  2398. repeat
  2399. case p^ of
  2400. #0:
  2401. if (p-PChar(SrcLine)=length(SrcLine)) then
  2402. begin
  2403. // multi line comment
  2404. if IsDirective then
  2405. RaiseError('directive missing closing bracket',CommentStartP);
  2406. repeat
  2407. inc(LineNumber);
  2408. if LineNumber>SrcLines.Count then exit;
  2409. SrcLine:=SrcLines[LineNumber-1];
  2410. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2411. until SrcLine<>'';
  2412. p:=PChar(SrcLine);
  2413. continue;
  2414. end;
  2415. '}':
  2416. begin
  2417. inc(p);
  2418. break;
  2419. end;
  2420. end;
  2421. inc(p);
  2422. until false;
  2423. CommentEndP:=p;
  2424. case CommentStartP[1] of
  2425. '#': AddLabel;
  2426. '@': AddResolverReference;
  2427. '=': AddDirectReference;
  2428. end;
  2429. p:=CommentEndP;
  2430. continue;
  2431. end;
  2432. '/':
  2433. if p[1]='/' then
  2434. break; // rest of line is comment -> skip
  2435. end;
  2436. inc(p);
  2437. until false;
  2438. end;
  2439. end;
  2440. procedure CheckResolverReference(aMarker: PSrcMarker);
  2441. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2442. var
  2443. aLabel: PSrcMarker;
  2444. ReferenceElements, LabelElements: TFPList;
  2445. i, j, aLine, aCol: Integer;
  2446. El, Ref, LabelEl: TPasElement;
  2447. begin
  2448. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2449. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2450. if aLabel=nil then
  2451. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2452. LabelElements:=nil;
  2453. ReferenceElements:=nil;
  2454. try
  2455. LabelElements:=FindElementsAt(aLabel);
  2456. ReferenceElements:=FindElementsAt(aMarker);
  2457. for i:=0 to ReferenceElements.Count-1 do
  2458. begin
  2459. El:=TPasElement(ReferenceElements[i]);
  2460. Ref:=nil;
  2461. if El.CustomData is TResolvedReference then
  2462. Ref:=TResolvedReference(El.CustomData).Declaration
  2463. else if El.CustomData is TPasPropertyScope then
  2464. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2465. else if El.CustomData is TPasSpecializeTypeData then
  2466. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2467. if Ref<>nil then
  2468. for j:=0 to LabelElements.Count-1 do
  2469. begin
  2470. LabelEl:=TPasElement(LabelElements[j]);
  2471. if Ref=LabelEl then
  2472. exit; // success
  2473. end;
  2474. end;
  2475. // failure write candidates
  2476. for i:=0 to ReferenceElements.Count-1 do
  2477. begin
  2478. El:=TPasElement(ReferenceElements[i]);
  2479. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2480. write(' El=',GetObjName(El));
  2481. if EL is TPrimitiveExpr then
  2482. begin
  2483. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2484. end;
  2485. Ref:=nil;
  2486. if El.CustomData is TResolvedReference then
  2487. Ref:=TResolvedReference(El.CustomData).Declaration
  2488. else if El.CustomData is TPasPropertyScope then
  2489. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2490. else if El.CustomData is TPasSpecializeTypeData then
  2491. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2492. if Ref<>nil then
  2493. begin
  2494. write(' Decl=',GetObjName(Ref));
  2495. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2496. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2497. end
  2498. else
  2499. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2500. writeln;
  2501. end;
  2502. for i:=0 to LabelElements.Count-1 do
  2503. begin
  2504. El:=TPasElement(LabelElements[i]);
  2505. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2506. write(' El=',GetObjName(El));
  2507. writeln;
  2508. end;
  2509. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2510. finally
  2511. LabelElements.Free;
  2512. ReferenceElements.Free;
  2513. end;
  2514. end;
  2515. procedure CheckDirectReference(aMarker: PSrcMarker);
  2516. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2517. var
  2518. aLabel: PSrcMarker;
  2519. ReferenceElements, LabelElements: TFPList;
  2520. i, LabelLine, LabelCol, j: Integer;
  2521. El, LabelEl: TPasElement;
  2522. DeclEl, TypeEl: TPasType;
  2523. begin
  2524. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2525. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2526. if aLabel=nil then
  2527. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2528. LabelElements:=nil;
  2529. ReferenceElements:=nil;
  2530. try
  2531. //writeln('CheckDirectReference finding elements at label ...');
  2532. LabelElements:=FindElementsAt(aLabel);
  2533. //writeln('CheckDirectReference finding elements at reference ...');
  2534. ReferenceElements:=FindElementsAt(aMarker);
  2535. for i:=0 to ReferenceElements.Count-1 do
  2536. begin
  2537. El:=TPasElement(ReferenceElements[i]);
  2538. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2539. if El.ClassType=TPasVariable then
  2540. begin
  2541. if TPasVariable(El).VarType=nil then
  2542. begin
  2543. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2544. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2545. end;
  2546. TypeEl:=TPasVariable(El).VarType;
  2547. for j:=0 to LabelElements.Count-1 do
  2548. begin
  2549. LabelEl:=TPasElement(LabelElements[j]);
  2550. if TypeEl=LabelEl then
  2551. exit; // success
  2552. end;
  2553. end
  2554. else if El is TPasAliasType then
  2555. begin
  2556. DeclEl:=TPasAliasType(El).DestType;
  2557. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2558. if (aLabel^.Filename=DeclEl.SourceFilename)
  2559. and (integer(aLabel^.Row)=LabelLine)
  2560. and (aLabel^.StartCol<=LabelCol)
  2561. and (aLabel^.EndCol>=LabelCol) then
  2562. exit; // success
  2563. end
  2564. else if El.ClassType=TPasArgument then
  2565. begin
  2566. TypeEl:=TPasArgument(El).ArgType;
  2567. for j:=0 to LabelElements.Count-1 do
  2568. begin
  2569. LabelEl:=TPasElement(LabelElements[j]);
  2570. if TypeEl=LabelEl then
  2571. exit; // success
  2572. end;
  2573. end;
  2574. end;
  2575. // failed -> show candidates
  2576. writeln('CheckDirectReference failed: Labels:');
  2577. for j:=0 to LabelElements.Count-1 do
  2578. begin
  2579. LabelEl:=TPasElement(LabelElements[j]);
  2580. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2581. end;
  2582. writeln('CheckDirectReference failed: References:');
  2583. for i:=0 to ReferenceElements.Count-1 do
  2584. begin
  2585. El:=TPasElement(ReferenceElements[i]);
  2586. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2587. //if EL is TPasVariable then
  2588. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2589. end;
  2590. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2591. finally
  2592. LabelElements.Free;
  2593. ReferenceElements.Free;
  2594. end;
  2595. end;
  2596. var
  2597. aMarker: PSrcMarker;
  2598. i: Integer;
  2599. SrcLines: TStringList;
  2600. begin
  2601. Module.ForEachCall(@OnCheckElementParent,nil);
  2602. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2603. // find all markers
  2604. for i:=0 to FileResolver.Streams.Count-1 do
  2605. begin
  2606. GetSrc(i,SrcLines,CurFilename);
  2607. ParseCode(SrcLines,CurFilename);
  2608. SrcLines.Free;
  2609. end;
  2610. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2611. // check references
  2612. aMarker:=FirstSrcMarker;
  2613. while aMarker<>nil do
  2614. begin
  2615. case aMarker^.Kind of
  2616. mkResolverReference: CheckResolverReference(aMarker);
  2617. mkDirectReference: CheckDirectReference(aMarker);
  2618. end;
  2619. aMarker:=aMarker^.Next;
  2620. end;
  2621. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2622. end;
  2623. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2624. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2625. var
  2626. i: Integer;
  2627. Item: TTestHintMessage;
  2628. Expected,Actual: string;
  2629. begin
  2630. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2631. for i:=0 to MsgCount-1 do
  2632. begin
  2633. Item:=Msgs[i];
  2634. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2635. if (Marker<>nil) then
  2636. begin
  2637. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2638. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2639. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2640. end;
  2641. // found
  2642. FHintMsgsGood.Add(Item);
  2643. str(Item.MsgType,Actual);
  2644. str(MsgType,Expected);
  2645. AssertEquals('MsgType',Expected,Actual);
  2646. exit;
  2647. end;
  2648. // needed message missing -> show emitted messages
  2649. WriteSources('',0,0);
  2650. for i:=0 to MsgCount-1 do
  2651. begin
  2652. Item:=Msgs[i];
  2653. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2654. ' ('+IntToStr(Item.MsgNumber),')');
  2655. if Marker<>nil then
  2656. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2657. writeln(' {',Item.Msg,'}');
  2658. end;
  2659. str(MsgType,Expected);
  2660. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2661. if Marker<>nil then
  2662. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2663. Actual:=Actual+' '+Msg;
  2664. Fail(Actual);
  2665. end;
  2666. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2667. );
  2668. var
  2669. i: Integer;
  2670. s, Txt: String;
  2671. Msg: TTestHintMessage;
  2672. begin
  2673. for i:=0 to MsgCount-1 do
  2674. begin
  2675. Msg:=Msgs[i];
  2676. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2677. s:='';
  2678. str(Msg.MsgType,s);
  2679. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2680. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2681. if WithSourcePos then
  2682. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2683. Txt:=Txt+' {'+Msg.Msg+'}';
  2684. Fail(Txt);
  2685. end;
  2686. end;
  2687. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2688. MsgNumber: integer);
  2689. begin
  2690. ExpectedErrorClass:=EScannerError;
  2691. ExpectedErrorMsg:=Msg;
  2692. ExpectedErrorNumber:=MsgNumber;
  2693. end;
  2694. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2695. MsgNumber: integer);
  2696. begin
  2697. ExpectedErrorClass:=EParserError;
  2698. ExpectedErrorMsg:=Msg;
  2699. ExpectedErrorNumber:=MsgNumber;
  2700. end;
  2701. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2702. MsgNumber: integer);
  2703. begin
  2704. ExpectedErrorClass:=EPasResolve;
  2705. ExpectedErrorMsg:=Msg;
  2706. ExpectedErrorNumber:=MsgNumber;
  2707. end;
  2708. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2709. MsgNumber: integer);
  2710. begin
  2711. ExpectedErrorClass:=EPas2JS;
  2712. ExpectedErrorMsg:=Msg;
  2713. ExpectedErrorNumber:=MsgNumber;
  2714. end;
  2715. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2716. var
  2717. MsgNumber: Integer;
  2718. Msg: String;
  2719. begin
  2720. Result:=false;
  2721. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2722. Msg:=E.Message;
  2723. if E is EPas2JS then
  2724. MsgNumber:=EPas2JS(E).MsgNumber
  2725. else if E is EPasResolve then
  2726. MsgNumber:=EPasResolve(E).MsgNumber
  2727. else if E is EParserError then
  2728. MsgNumber:=Parser.LastMsgNumber
  2729. else if E is EScannerError then
  2730. begin
  2731. MsgNumber:=Scanner.LastMsgNumber;
  2732. Msg:=Scanner.LastMsg;
  2733. end
  2734. else
  2735. MsgNumber:=0;
  2736. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2737. if Result then
  2738. SkipTests:=true;
  2739. end;
  2740. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2741. const aFilename: string; aRow, aCol: integer);
  2742. var
  2743. s: String;
  2744. begin
  2745. WriteSources(aFilename,aRow,aCol);
  2746. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2747. writeln('ERROR: ',s);
  2748. Fail(s);
  2749. end;
  2750. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2751. aMarker: PSrcMarker);
  2752. begin
  2753. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2754. end;
  2755. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2756. begin
  2757. if IsErrorExpected(E) then exit;
  2758. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2759. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2760. +' '+Scanner.CurFilename
  2761. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2762. FailException(E);
  2763. end;
  2764. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2765. begin
  2766. if IsErrorExpected(E) then exit;
  2767. WriteSources(E.Filename,E.Row,E.Column);
  2768. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2769. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2770. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2771. );
  2772. FailException(E);
  2773. end;
  2774. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2775. var
  2776. P: TPasSourcePos;
  2777. begin
  2778. if IsErrorExpected(E) then exit;
  2779. P:=E.SourcePos;
  2780. WriteSources(P.FileName,P.Row,P.Column);
  2781. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+'['+IntToStr(E.Id)+']:'
  2782. +E.Message
  2783. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2784. FailException(E);
  2785. end;
  2786. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2787. var
  2788. Row, Col: integer;
  2789. begin
  2790. if IsErrorExpected(E) then exit;
  2791. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2792. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2793. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2794. +' '+E.PasElement.SourceFilename
  2795. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2796. FailException(E);
  2797. end;
  2798. procedure TCustomTestModule.HandleException(E: Exception);
  2799. begin
  2800. if E is EScannerError then
  2801. HandleScannerError(EScannerError(E))
  2802. else if E is EParserError then
  2803. HandleParserError(EParserError(E))
  2804. else if E is EPasResolve then
  2805. HandlePasResolveError(EPasResolve(E))
  2806. else if E is EPas2JS then
  2807. HandlePas2JSError(EPas2JS(E))
  2808. else
  2809. begin
  2810. if IsErrorExpected(E) then exit;
  2811. if not (E is EAssertionFailedError) then
  2812. begin
  2813. WriteSources('',0,0);
  2814. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2815. end;
  2816. FailException(E);
  2817. end;
  2818. end;
  2819. procedure TCustomTestModule.FailException(E: Exception);
  2820. var
  2821. MsgNumber: Integer;
  2822. begin
  2823. if ExpectedErrorClass<>nil then
  2824. begin
  2825. if FExpectedErrorClass=E.ClassType then
  2826. begin
  2827. if E is EPas2JS then
  2828. MsgNumber:=EPas2JS(E).MsgNumber
  2829. else if E is EPasResolve then
  2830. MsgNumber:=EPasResolve(E).MsgNumber
  2831. else if E is EParserError then
  2832. MsgNumber:=Parser.LastMsgNumber
  2833. else if E is EScannerError then
  2834. MsgNumber:=Scanner.LastMsgNumber
  2835. else
  2836. MsgNumber:=0;
  2837. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2838. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2839. ExpectedErrorNumber,MsgNumber);
  2840. end else begin
  2841. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2842. end;
  2843. end;
  2844. Fail(E.Message);
  2845. end;
  2846. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2847. aCol: integer);
  2848. var
  2849. IsSrc: Boolean;
  2850. i, j: Integer;
  2851. SrcLines: TStringList;
  2852. Line: string;
  2853. aModule: TTestEnginePasResolver;
  2854. begin
  2855. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2856. for i:=0 to ResolverCount-1 do
  2857. begin
  2858. aModule:=Resolvers[i];
  2859. SrcLines:=TStringList.Create;
  2860. try
  2861. SrcLines.Text:=aModule.Source;
  2862. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2863. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2864. for j:=1 to SrcLines.Count do
  2865. begin
  2866. Line:=SrcLines[j-1];
  2867. if IsSrc and (j=aRow) then
  2868. begin
  2869. write('*');
  2870. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2871. end;
  2872. writeln(Format('%:4d: ',[j]),Line);
  2873. end;
  2874. finally
  2875. SrcLines.Free;
  2876. end;
  2877. end;
  2878. end;
  2879. function TCustomTestModule.IndexOfResolver(const aFilename: string): integer;
  2880. var
  2881. i: Integer;
  2882. begin
  2883. for i:=0 to ResolverCount-1 do
  2884. if aFilename=Resolvers[i].Filename then exit(i);
  2885. Result:=-1;
  2886. end;
  2887. function TCustomTestModule.GetResolver(const aFilename: string
  2888. ): TTestEnginePasResolver;
  2889. var
  2890. i: Integer;
  2891. begin
  2892. i:=IndexOfResolver(aFilename);
  2893. if i<0 then exit(nil);
  2894. Result:=Resolvers[i];
  2895. end;
  2896. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2897. out aFilename: string);
  2898. var
  2899. aStream: TStream;
  2900. begin
  2901. SrcLines:=TStringList.Create;
  2902. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2903. aStream.Position:=0;
  2904. SrcLines.LoadFromStream(aStream);
  2905. aFilename:=FileResolver.Streams[Index];
  2906. end;
  2907. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2908. aEndCol: integer): TFPList;
  2909. var
  2910. ok: Boolean;
  2911. FoundRefs: TTestResolverReferenceData;
  2912. i: Integer;
  2913. CurResolver: TTestEnginePasResolver;
  2914. begin
  2915. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2916. FoundRefs:=Default(TTestResolverReferenceData);
  2917. FoundRefs.Filename:=aFilename;
  2918. FoundRefs.Row:=aLine;
  2919. FoundRefs.StartCol:=aStartCol;
  2920. FoundRefs.EndCol:=aEndCol;
  2921. FoundRefs.Found:=TFPList.Create;
  2922. ok:=false;
  2923. try
  2924. // find all markers
  2925. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2926. for i:=0 to ResolverCount-1 do
  2927. begin
  2928. CurResolver:=Resolvers[i];
  2929. if CurResolver.Module=Module then continue;
  2930. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2931. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2932. end;
  2933. ok:=true;
  2934. finally
  2935. if not ok then
  2936. FreeAndNil(FoundRefs.Found);
  2937. end;
  2938. Result:=FoundRefs.Found;
  2939. FoundRefs.Found:=nil;
  2940. end;
  2941. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2942. ErrorOnNoElements: boolean): TFPList;
  2943. begin
  2944. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2945. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2946. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2947. end;
  2948. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2949. begin
  2950. Result:=FirstSrcMarker;
  2951. while Result<>nil do
  2952. begin
  2953. if (Result^.Kind=mkLabel)
  2954. and (CompareText(Result^.Identifier,Identifier)=0) then
  2955. exit;
  2956. Result:=Result^.Next;
  2957. end;
  2958. end;
  2959. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2960. ErrorOnNoElements: boolean): TFPList;
  2961. var
  2962. SrcLabel: PSrcMarker;
  2963. begin
  2964. SrcLabel:=FindSrcLabel(Identifier);
  2965. if SrcLabel=nil then
  2966. Fail('missing label "'+Identifier+'"');
  2967. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2968. end;
  2969. function TCustomTestModule.GetDefaultNamespace: string;
  2970. var
  2971. C: TClass;
  2972. begin
  2973. Result:='';
  2974. if FModule=nil then exit;
  2975. C:=FModule.ClassType;
  2976. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2977. Result:=ResolverEngine.DefaultNameSpace;
  2978. end;
  2979. constructor TCustomTestModule.Create;
  2980. begin
  2981. inherited Create;
  2982. FHintMsgs:=TObjectList.Create(true);
  2983. FHintMsgsGood:=TFPList.Create;
  2984. end;
  2985. destructor TCustomTestModule.Destroy;
  2986. begin
  2987. FreeAndNil(FHintMsgs);
  2988. FreeAndNil(FHintMsgsGood);
  2989. inherited Destroy;
  2990. end;
  2991. { TTestModule }
  2992. procedure TTestModule.TestReservedWords;
  2993. var
  2994. i: integer;
  2995. begin
  2996. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2997. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2998. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2999. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  3000. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  3001. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  3002. end;
  3003. procedure TTestModule.TestEmptyProgram;
  3004. begin
  3005. StartProgram(false);
  3006. Add('begin');
  3007. ConvertProgram;
  3008. CheckSource('TestEmptyProgram','','');
  3009. end;
  3010. procedure TTestModule.TestEmptyProgramUseStrict;
  3011. begin
  3012. Converter.Options:=Converter.Options+[coUseStrict];
  3013. StartProgram(false);
  3014. Add('begin');
  3015. ConvertProgram;
  3016. CheckSource('TestEmptyProgramUseStrict','','');
  3017. end;
  3018. procedure TTestModule.TestEmptyUnit;
  3019. begin
  3020. StartUnit(false);
  3021. Add('interface');
  3022. Add('implementation');
  3023. ConvertUnit;
  3024. CheckSource('TestEmptyUnit',
  3025. LinesToStr([
  3026. ]),
  3027. '');
  3028. end;
  3029. procedure TTestModule.TestEmptyUnitUseStrict;
  3030. begin
  3031. Converter.Options:=Converter.Options+[coUseStrict];
  3032. StartUnit(false);
  3033. Add('interface');
  3034. Add('implementation');
  3035. ConvertUnit;
  3036. CheckSource('TestEmptyUnitUseStrict',
  3037. LinesToStr([
  3038. ''
  3039. ]),
  3040. '');
  3041. end;
  3042. procedure TTestModule.TestDottedUnitNames;
  3043. begin
  3044. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3045. LinesToStr([
  3046. 'var iV: longint;'
  3047. ]),
  3048. '');
  3049. FFilename:='ns1.test1.pp';
  3050. StartProgram(true);
  3051. Add('uses unIt2;');
  3052. Add('var');
  3053. Add(' i: longint;');
  3054. Add('begin');
  3055. Add(' i:=iv;');
  3056. Add(' i:=uNit2.iv;');
  3057. Add(' i:=Ns1.TEst1.i;');
  3058. ConvertProgram;
  3059. CheckSource('TestDottedUnitNames',
  3060. LinesToStr([
  3061. 'this.i = 0;',
  3062. '']),
  3063. LinesToStr([ // this.$init
  3064. '$mod.i = pas["NS1.Unit2"].iV;',
  3065. '$mod.i = pas["NS1.Unit2"].iV;',
  3066. '$mod.i = $mod.i;',
  3067. '']) );
  3068. end;
  3069. procedure TTestModule.TestDottedUnitNameImpl;
  3070. begin
  3071. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3072. LinesToStr([
  3073. 'type',
  3074. ' TObject = class end;',
  3075. ' TTestA = class',
  3076. ' end;'
  3077. ]),
  3078. LinesToStr(['uses TEST.UnitB;'])
  3079. );
  3080. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3081. LinesToStr([
  3082. 'uses TEST.UnitA;',
  3083. 'type TTestB = class(TTestA);'
  3084. ]),
  3085. ''
  3086. );
  3087. StartProgram(true);
  3088. Add('uses TEST.UnitA;');
  3089. Add('begin');
  3090. ConvertProgram;
  3091. CheckSource('TestDottedUnitNameImpl',
  3092. LinesToStr([
  3093. '']),
  3094. LinesToStr([ // this.$init
  3095. '']) );
  3096. CheckUnit('TEST.UnitA.pas',
  3097. LinesToStr([
  3098. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3099. ' var $mod = this;',
  3100. ' rtl.createClass(this, "TObject", null, function () {',
  3101. ' this.$init = function () {',
  3102. ' };',
  3103. ' this.$final = function () {',
  3104. ' };',
  3105. ' });',
  3106. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3107. ' });',
  3108. '}, ["TEST.UnitB"]);'
  3109. ]));
  3110. CheckUnit('TEST.UnitB.pas',
  3111. LinesToStr([
  3112. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3113. ' var $mod = this;',
  3114. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3115. ' });',
  3116. '});'
  3117. ]));
  3118. end;
  3119. procedure TTestModule.TestDottedUnitExpr;
  3120. begin
  3121. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3122. LinesToStr([
  3123. 'procedure DoIt;'
  3124. ]),
  3125. 'procedure DoIt; begin end;');
  3126. FFilename:='Ns1.SubNs1.Test1.pp';
  3127. StartProgram(true);
  3128. Add('uses Ns2.sUbnS2.unIt2;');
  3129. Add('var');
  3130. Add(' i: longint;');
  3131. Add('begin');
  3132. Add(' ns2.subns2.unit2.doit;');
  3133. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3134. ConvertProgram;
  3135. CheckSource('TestDottedUnitExpr',
  3136. LinesToStr([
  3137. 'this.i = 0;',
  3138. '']),
  3139. LinesToStr([ // this.$init
  3140. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3141. '$mod.i = $mod.i;',
  3142. '']) );
  3143. end;
  3144. procedure TTestModule.Test_ModeFPCFail;
  3145. begin
  3146. StartProgram(false);
  3147. Add('{$mode FPC}');
  3148. Add('begin');
  3149. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3150. ConvertProgram;
  3151. end;
  3152. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3153. begin
  3154. StartProgram(false);
  3155. Add('{$modeswitch cblocks-}');
  3156. Add('begin');
  3157. ConvertProgram;
  3158. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3159. CheckResolverUnexpectedHints();
  3160. end;
  3161. procedure TTestModule.TestUnit_UseSystem;
  3162. begin
  3163. StartUnit(true);
  3164. Add([
  3165. 'interface',
  3166. 'var i: integer;',
  3167. 'implementation']);
  3168. ConvertUnit;
  3169. CheckSource('TestUnit_UseSystem',
  3170. LinesToStr([
  3171. 'this.i = 0;',
  3172. '']),
  3173. LinesToStr([
  3174. '']) );
  3175. end;
  3176. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3177. begin
  3178. AddModuleWithIntfImplSrc('unit1.pp',
  3179. LinesToStr([
  3180. 'type number = longint;']),
  3181. LinesToStr([
  3182. 'uses test1;',
  3183. 'procedure DoIt;',
  3184. 'begin',
  3185. ' i:=3;',
  3186. 'end;']));
  3187. StartUnit(true);
  3188. Add([
  3189. 'interface',
  3190. 'uses unit1;',
  3191. 'var i: number;',
  3192. 'implementation']);
  3193. ConvertUnit;
  3194. CheckSource('TestUnit_Intf1Impl2Intf1',
  3195. LinesToStr([
  3196. 'this.i = 0;',
  3197. '']),
  3198. LinesToStr([
  3199. '']) );
  3200. end;
  3201. procedure TTestModule.TestIncludeVersion;
  3202. begin
  3203. StartProgram(false);
  3204. Add([
  3205. 'var',
  3206. ' s: string;',
  3207. ' i: word;',
  3208. 'begin',
  3209. ' s:={$I %line%};',
  3210. ' i:={$I %linenum%};',
  3211. ' s:={$I %currentroutine%};',
  3212. ' s:={$I %pas2jsversion%};',
  3213. ' s:={$I %pas2jstarget%};',
  3214. ' s:={$I %pas2jstargetos%};',
  3215. ' s:={$I %pas2jstargetcpu%};',
  3216. ' s:={$I %file%};',
  3217. '']);
  3218. ConvertProgram;
  3219. CheckSource('TestIncludeVersion',
  3220. LinesToStr([
  3221. 'this.s="";',
  3222. 'this.i = 0;']),
  3223. LinesToStr([
  3224. '$mod.s = "7";',
  3225. '$mod.i = 8;',
  3226. '$mod.s = "<anonymous>";',
  3227. '$mod.s = "Comp.Ver.tcmodules";',
  3228. '$mod.s = "Browser";',
  3229. '$mod.s = "Browser";',
  3230. '$mod.s = "ECMAScript5";',
  3231. '$mod.s = "test1.pp";',
  3232. '']));
  3233. end;
  3234. procedure TTestModule.TestVarInt;
  3235. begin
  3236. StartProgram(false);
  3237. Add('var MyI: longint;');
  3238. Add('begin');
  3239. ConvertProgram;
  3240. CheckSource('TestVarInt','this.MyI=0;','');
  3241. end;
  3242. procedure TTestModule.TestVarBaseTypes;
  3243. begin
  3244. StartProgram(false);
  3245. Add('var');
  3246. Add(' i: longint;');
  3247. Add(' s: string;');
  3248. Add(' c: char;');
  3249. Add(' b: boolean;');
  3250. Add(' d: double;');
  3251. Add(' i2: longint = 3;');
  3252. Add(' s2: string = ''foo'';');
  3253. Add(' c2: char = ''4'';');
  3254. Add(' b2: boolean = true;');
  3255. Add(' d2: double = 5.6;');
  3256. Add(' i3: longint = $707;');
  3257. Add(' i4: nativeint = 9007199254740991;');
  3258. Add(' i5: nativeint = -9007199254740991-1;');
  3259. Add(' i6: nativeint = $fffffffffffff;');
  3260. Add(' i7: nativeint = -$fffffffffffff-1;');
  3261. Add(' i8: byte = 00;');
  3262. Add(' u8: nativeuint = $fffffffffffff;');
  3263. Add(' u9: nativeuint = $0000000000000;');
  3264. Add(' u10: nativeuint = $00ff00;');
  3265. Add('begin');
  3266. ConvertProgram;
  3267. CheckSource('TestVarBaseTypes',
  3268. LinesToStr([
  3269. 'this.i = 0;',
  3270. 'this.s = "";',
  3271. 'this.c = "\x00";',
  3272. 'this.b = false;',
  3273. 'this.d = 0.0;',
  3274. 'this.i2 = 3;',
  3275. 'this.s2 = "foo";',
  3276. 'this.c2 = "4";',
  3277. 'this.b2 = true;',
  3278. 'this.d2 = 5.6;',
  3279. 'this.i3 = 0x707;',
  3280. 'this.i4 = 9007199254740991;',
  3281. 'this.i5 = -9007199254740991-1;',
  3282. 'this.i6 = 0xfffffffffffff;',
  3283. 'this.i7 =-0xfffffffffffff-1;',
  3284. 'this.i8 = 0;',
  3285. 'this.u8 = 0xfffffffffffff;',
  3286. 'this.u9 = 0x0;',
  3287. 'this.u10 = 0xff00;'
  3288. ]),
  3289. '');
  3290. end;
  3291. procedure TTestModule.TestBaseTypeSingleFail;
  3292. begin
  3293. StartProgram(false);
  3294. Add('var s: single;');
  3295. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3296. ConvertProgram;
  3297. end;
  3298. procedure TTestModule.TestBaseTypeExtendedFail;
  3299. begin
  3300. StartProgram(false);
  3301. Add('var e: extended;');
  3302. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3303. ConvertProgram;
  3304. end;
  3305. procedure TTestModule.TestConstBaseTypes;
  3306. begin
  3307. StartProgram(false);
  3308. Add('const');
  3309. Add(' i: longint = 3;');
  3310. Add(' s: string = ''foo'';');
  3311. Add(' c: char = ''4'';');
  3312. Add(' b: boolean = true;');
  3313. Add(' d: double = 5.6;');
  3314. Add(' e = low(word);');
  3315. Add(' f = high(word);');
  3316. Add('begin');
  3317. ConvertProgram;
  3318. CheckSource('TestVarBaseTypes',
  3319. LinesToStr([
  3320. 'this.i=3;',
  3321. 'this.s="foo";',
  3322. 'this.c="4";',
  3323. 'this.b=true;',
  3324. 'this.d=5.6;',
  3325. 'this.e = 0;',
  3326. 'this.f = 65535;'
  3327. ]),
  3328. '');
  3329. end;
  3330. procedure TTestModule.TestAliasTypeRef;
  3331. begin
  3332. StartProgram(false);
  3333. Add('type');
  3334. Add(' a=longint;');
  3335. Add(' b=a;');
  3336. Add('var');
  3337. Add(' c: A;');
  3338. Add(' d: B;');
  3339. Add('begin');
  3340. ConvertProgram;
  3341. CheckSource('TestAliasTypeRef',
  3342. LinesToStr([ // statements
  3343. 'this.c = 0;',
  3344. 'this.d = 0;'
  3345. ]),
  3346. LinesToStr([ // this.$main
  3347. ''
  3348. ]));
  3349. end;
  3350. procedure TTestModule.TestTypeCast_BaseTypes;
  3351. begin
  3352. StartProgram(false);
  3353. Add([
  3354. 'var',
  3355. ' i: longint;',
  3356. ' b: boolean;',
  3357. ' d: double;',
  3358. ' s: string;',
  3359. ' c: char;',
  3360. 'begin',
  3361. ' i:=longint(i);',
  3362. ' i:=longint(b);',
  3363. ' b:=boolean(b);',
  3364. ' b:=boolean(i);',
  3365. ' d:=double(d);',
  3366. ' d:=double(i);',
  3367. ' s:=string(s);',
  3368. ' s:=string(c);',
  3369. ' c:=char(c);',
  3370. ' c:=char(i);',
  3371. ' c:=char(65);',
  3372. ' c:=char(#10);',
  3373. ' c:=char(#$E000);',
  3374. '']);
  3375. ConvertProgram;
  3376. CheckSource('TestAliasTypeRef',
  3377. LinesToStr([ // statements
  3378. 'this.i = 0;',
  3379. 'this.b = false;',
  3380. 'this.d = 0.0;',
  3381. 'this.s = "";',
  3382. 'this.c = "\x00";',
  3383. '']),
  3384. LinesToStr([ // this.$main
  3385. '$mod.i = $mod.i;',
  3386. '$mod.i = ($mod.b ? 1 : 0);',
  3387. '$mod.b = $mod.b;',
  3388. '$mod.b = $mod.i != 0;',
  3389. '$mod.d = $mod.d;',
  3390. '$mod.d = $mod.i;',
  3391. '$mod.s = $mod.s;',
  3392. '$mod.s = $mod.c;',
  3393. '$mod.c = $mod.c;',
  3394. '$mod.c = String.fromCharCode($mod.i);',
  3395. '$mod.c = "A";',
  3396. '$mod.c = "\n";',
  3397. '$mod.c = "";',
  3398. '']));
  3399. end;
  3400. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3401. begin
  3402. StartProgram(false);
  3403. Add('type');
  3404. Add(' integer = longint;');
  3405. Add(' TYesNo = boolean;');
  3406. Add(' TFloat = double;');
  3407. Add(' TCaption = string;');
  3408. Add(' TChar = char;');
  3409. Add('var');
  3410. Add(' i: integer;');
  3411. Add(' b: TYesNo;');
  3412. Add(' d: TFloat;');
  3413. Add(' s: TCaption;');
  3414. Add(' c: TChar;');
  3415. Add('begin');
  3416. Add(' i:=integer(i);');
  3417. Add(' i:=integer(b);');
  3418. Add(' b:=TYesNo(b);');
  3419. Add(' b:=TYesNo(i);');
  3420. Add(' d:=TFloat(d);');
  3421. Add(' d:=TFloat(i);');
  3422. Add(' s:=TCaption(s);');
  3423. Add(' s:=TCaption(c);');
  3424. Add(' c:=TChar(c);');
  3425. ConvertProgram;
  3426. CheckSource('TestAliasTypeRef',
  3427. LinesToStr([ // statements
  3428. 'this.i = 0;',
  3429. 'this.b = false;',
  3430. 'this.d = 0.0;',
  3431. 'this.s = "";',
  3432. 'this.c = "\x00";',
  3433. '']),
  3434. LinesToStr([ // this.$main
  3435. '$mod.i = $mod.i;',
  3436. '$mod.i = ($mod.b ? 1 : 0);',
  3437. '$mod.b = $mod.b;',
  3438. '$mod.b = $mod.i != 0;',
  3439. '$mod.d = $mod.d;',
  3440. '$mod.d = $mod.i;',
  3441. '$mod.s = $mod.s;',
  3442. '$mod.s = $mod.c;',
  3443. '$mod.c = $mod.c;',
  3444. '']));
  3445. end;
  3446. procedure TTestModule.TestEmptyProc;
  3447. begin
  3448. StartProgram(false);
  3449. Add('procedure Test;');
  3450. Add('begin');
  3451. Add('end;');
  3452. Add('begin');
  3453. ConvertProgram;
  3454. CheckSource('TestEmptyProc',
  3455. LinesToStr([ // statements
  3456. 'this.Test = function () {',
  3457. '};'
  3458. ]),
  3459. LinesToStr([ // this.$main
  3460. ''
  3461. ]));
  3462. end;
  3463. procedure TTestModule.TestProcOneParam;
  3464. begin
  3465. StartProgram(false);
  3466. Add('procedure ProcA(i: longint);');
  3467. Add('begin');
  3468. Add('end;');
  3469. Add('begin');
  3470. Add(' PROCA(3);');
  3471. ConvertProgram;
  3472. CheckSource('TestProcOneParam',
  3473. LinesToStr([ // statements
  3474. 'this.ProcA = function (i) {',
  3475. '};'
  3476. ]),
  3477. LinesToStr([ // this.$main
  3478. '$mod.ProcA(3);'
  3479. ]));
  3480. end;
  3481. procedure TTestModule.TestFunctionWithoutParams;
  3482. begin
  3483. StartProgram(false);
  3484. Add('function FuncA: longint;');
  3485. Add('begin');
  3486. Add('end;');
  3487. Add('var i: longint;');
  3488. Add('begin');
  3489. Add(' I:=FUNCA();');
  3490. Add(' I:=FUNCA;');
  3491. Add(' FUNCA();');
  3492. Add(' FUNCA;');
  3493. ConvertProgram;
  3494. CheckSource('TestProcWithoutParams',
  3495. LinesToStr([ // statements
  3496. 'this.FuncA = function () {',
  3497. ' var Result = 0;',
  3498. ' return Result;',
  3499. '};',
  3500. 'this.i=0;'
  3501. ]),
  3502. LinesToStr([ // this.$main
  3503. '$mod.i=$mod.FuncA();',
  3504. '$mod.i=$mod.FuncA();',
  3505. '$mod.FuncA();',
  3506. '$mod.FuncA();'
  3507. ]));
  3508. end;
  3509. procedure TTestModule.TestProcedureWithoutParams;
  3510. begin
  3511. StartProgram(false);
  3512. Add('procedure ProcA;');
  3513. Add('begin');
  3514. Add('end;');
  3515. Add('begin');
  3516. Add(' PROCA();');
  3517. Add(' PROCA;');
  3518. ConvertProgram;
  3519. CheckSource('TestProcWithoutParams',
  3520. LinesToStr([ // statements
  3521. 'this.ProcA = function () {',
  3522. '};'
  3523. ]),
  3524. LinesToStr([ // this.$main
  3525. '$mod.ProcA();',
  3526. '$mod.ProcA();'
  3527. ]));
  3528. end;
  3529. procedure TTestModule.TestIncDec;
  3530. begin
  3531. StartProgram(false);
  3532. Add([
  3533. 'procedure DoIt(var i: longint);',
  3534. 'begin',
  3535. ' inc(i);',
  3536. ' inc(i,2);',
  3537. 'end;',
  3538. 'var',
  3539. ' Bar: longint;',
  3540. 'begin',
  3541. ' inc(bar);',
  3542. ' inc(bar,2);',
  3543. ' dec(bar);',
  3544. ' dec(bar,3);',
  3545. '']);
  3546. ConvertProgram;
  3547. CheckSource('TestIncDec',
  3548. LinesToStr([ // statements
  3549. 'this.DoIt = function (i) {',
  3550. ' i.set(i.get()+1);',
  3551. ' i.set(i.get()+2);',
  3552. '};',
  3553. 'this.Bar = 0;'
  3554. ]),
  3555. LinesToStr([ // this.$main
  3556. '$mod.Bar+=1;',
  3557. '$mod.Bar+=2;',
  3558. '$mod.Bar-=1;',
  3559. '$mod.Bar-=3;'
  3560. ]));
  3561. end;
  3562. procedure TTestModule.TestLoHiFpcMode;
  3563. begin
  3564. StartProgram(false);
  3565. Add([
  3566. '{$mode objfpc}',
  3567. 'const',
  3568. ' LoByte1 = Lo(Word($1234));',
  3569. ' HiByte1 = Hi(Word($1234));',
  3570. ' LoByte2 = Lo(SmallInt($1234));',
  3571. ' HiByte2 = Hi(SmallInt($1234));',
  3572. ' LoWord1 = Lo($1234CDEF);',
  3573. ' HiWord1 = Hi($1234CDEF);',
  3574. ' LoWord2 = Lo(-$1234CDEF);',
  3575. ' HiWord2 = Hi(-$1234CDEF);',
  3576. ' lo4:byte=lo(byte($34));',
  3577. ' hi4:byte=hi(byte($34));',
  3578. ' lo5:byte=lo(shortint(-$34));',
  3579. ' hi5:byte=hi(shortint(-$34));',
  3580. ' lo6:longword=lo($123456789ABCD);',
  3581. ' hi6:longword=hi($123456789ABCD);',
  3582. ' lo7:longword=lo(-$123456789ABCD);',
  3583. ' hi7:longword=hi(-$123456789ABCD);',
  3584. 'var',
  3585. ' b: Byte;',
  3586. ' ss: shortint;',
  3587. ' w: Word;',
  3588. ' si: SmallInt;',
  3589. ' lw: LongWord;',
  3590. ' li: LongInt;',
  3591. ' b2: Byte;',
  3592. ' ni: nativeint;',
  3593. 'begin',
  3594. ' w := $1234;',
  3595. ' ss := -$12;',
  3596. ' b := lo(ss);',
  3597. ' b := HI(ss);',
  3598. ' b := lo(w);',
  3599. ' b := HI(w);',
  3600. ' b2 := lo(b);',
  3601. ' b2 := hi(b);',
  3602. ' lw := $1234CDEF;',
  3603. ' w := lo(lw);',
  3604. ' w := hi(lw);',
  3605. ' ni := $123456789ABCD;',
  3606. ' lw := lo(ni);',
  3607. ' lw := hi(ni);',
  3608. '']);
  3609. ConvertProgram;
  3610. CheckSource('TestLoHiFpcMode',
  3611. LinesToStr([ // statements
  3612. 'this.LoByte1 = 0x1234 & 0xFF;',
  3613. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3614. 'this.LoByte2 = 0x1234 & 0xFF;',
  3615. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3616. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3617. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3618. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3619. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3620. 'this.lo4 = 0x34 & 0xF;',
  3621. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3622. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3623. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3624. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3625. 'this.hi6 = 74565 >>> 0;',
  3626. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3627. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3628. 'this.b = 0;',
  3629. 'this.ss = 0;',
  3630. 'this.w = 0;',
  3631. 'this.si = 0;',
  3632. 'this.lw = 0;',
  3633. 'this.li = 0;',
  3634. 'this.b2 = 0;',
  3635. 'this.ni = 0;',
  3636. '']),
  3637. LinesToStr([ // this.$main
  3638. '$mod.w = 0x1234;',
  3639. '$mod.ss = -0x12;',
  3640. '$mod.b = $mod.ss & 0xFF;',
  3641. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3642. '$mod.b = $mod.w & 0xFF;',
  3643. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3644. '$mod.b2 = $mod.b & 0xF;',
  3645. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3646. '$mod.lw = 0x1234CDEF;',
  3647. '$mod.w = $mod.lw & 0xFFFF;',
  3648. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3649. '$mod.ni = 0x123456789ABCD;',
  3650. '$mod.lw = $mod.ni >>> 0;',
  3651. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3652. '']));
  3653. end;
  3654. procedure TTestModule.TestLoHiDelphiMode;
  3655. begin
  3656. StartProgram(false);
  3657. Add([
  3658. '{$mode delphi}',
  3659. 'const',
  3660. ' LoByte1 = Lo(Word($1234));',
  3661. ' HiByte1 = Hi(Word($1234));',
  3662. ' LoByte2 = Lo(SmallInt($1234));',
  3663. ' HiByte2 = Hi(SmallInt($1234));',
  3664. ' LoByte3 = Lo($1234CDEF);',
  3665. ' HiByte3 = Hi($1234CDEF);',
  3666. ' LoByte4 = Lo(-$1234CDEF);',
  3667. ' HiByte4 = Hi(-$1234CDEF);',
  3668. 'var',
  3669. ' b: Byte;',
  3670. ' w: Word;',
  3671. ' si: SmallInt;',
  3672. ' lw: LongWord;',
  3673. ' li: LongInt;',
  3674. 'begin',
  3675. ' w := $1234;',
  3676. ' b := lo(w);',
  3677. ' b := HI(w);',
  3678. ' lw := $1234CDEF;',
  3679. ' b := lo(lw);',
  3680. ' b := hi(lw);',
  3681. '']);
  3682. ConvertProgram;
  3683. CheckSource('TestLoHiDelphiMode',
  3684. LinesToStr([ // statements
  3685. 'this.LoByte1 = 0x1234 & 0xFF;',
  3686. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3687. 'this.LoByte2 = 0x1234 & 0xFF;',
  3688. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3689. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3690. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3691. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3692. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3693. 'this.b = 0;',
  3694. 'this.w = 0;',
  3695. 'this.si = 0;',
  3696. 'this.lw = 0;',
  3697. 'this.li = 0;'
  3698. ]),
  3699. LinesToStr([ // this.$main
  3700. '$mod.w = 0x1234;',
  3701. '$mod.b = $mod.w & 0xFF;',
  3702. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3703. '$mod.lw = 0x1234CDEF;',
  3704. '$mod.b = $mod.lw & 0xFF;',
  3705. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3706. ]));
  3707. end;
  3708. procedure TTestModule.TestAssignments;
  3709. begin
  3710. StartProgram(false);
  3711. Parser.Options:=Parser.Options+[po_cassignments];
  3712. Add('var');
  3713. Add(' Bar:longint;');
  3714. Add('begin');
  3715. Add(' bar:=3;');
  3716. Add(' bar+=4;');
  3717. Add(' bar-=5;');
  3718. Add(' bar*=6;');
  3719. ConvertProgram;
  3720. CheckSource('TestAssignments',
  3721. LinesToStr([ // statements
  3722. 'this.Bar = 0;'
  3723. ]),
  3724. LinesToStr([ // this.$main
  3725. '$mod.Bar=3;',
  3726. '$mod.Bar+=4;',
  3727. '$mod.Bar-=5;',
  3728. '$mod.Bar*=6;'
  3729. ]));
  3730. end;
  3731. procedure TTestModule.TestArithmeticOperators1;
  3732. begin
  3733. StartProgram(false);
  3734. Add('var');
  3735. Add(' vA,vB,vC:longint;');
  3736. Add('begin');
  3737. Add(' va:=1;');
  3738. Add(' vb:=va+va;');
  3739. Add(' vb:=va div vb;');
  3740. Add(' vb:=va mod vb;');
  3741. Add(' vb:=va+va*vb+va div vb;');
  3742. Add(' vc:=-va;');
  3743. Add(' va:=va-vb;');
  3744. Add(' vb:=va;');
  3745. Add(' if va<vb then vc:=va else vc:=vb;');
  3746. ConvertProgram;
  3747. CheckSource('TestArithmeticOperators1',
  3748. LinesToStr([ // statements
  3749. 'this.vA = 0;',
  3750. 'this.vB = 0;',
  3751. 'this.vC = 0;'
  3752. ]),
  3753. LinesToStr([ // this.$main
  3754. '$mod.vA = 1;',
  3755. '$mod.vB = $mod.vA + $mod.vA;',
  3756. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3757. '$mod.vB = $mod.vA % $mod.vB;',
  3758. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3759. '$mod.vC = -$mod.vA;',
  3760. '$mod.vA = $mod.vA - $mod.vB;',
  3761. '$mod.vB = $mod.vA;',
  3762. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3763. ]));
  3764. end;
  3765. procedure TTestModule.TestMultiAdd;
  3766. begin
  3767. StartProgram(false);
  3768. Add([
  3769. 'function Fly: string; external name ''fly'';',
  3770. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3771. 'var',
  3772. ' Date: double;',
  3773. 'begin',
  3774. ' Result:=(Year>0) and (Year<10000) and',
  3775. ' (Month >= 1) and (Month<=12) and',
  3776. ' (Day>0) and (Day<=31);',
  3777. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3778. 'end;',
  3779. 'var s: string;',
  3780. 'begin',
  3781. ' s:=''a''+''b''+''c''+''d'';',
  3782. ' s:=s+Fly+''e'';',
  3783. ' s:=Fly+Fly+Fly;',
  3784. '']);
  3785. ConvertProgram;
  3786. CheckSource('TestMultiAdd',
  3787. LinesToStr([ // statements
  3788. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3789. ' var Result = false;',
  3790. ' var date = 0.0;',
  3791. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3792. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3793. ' return Result;',
  3794. '};',
  3795. 'this.s = "";',
  3796. '']),
  3797. LinesToStr([ // this.$main
  3798. '$mod.s = "a" + "b" + "c" + "d";',
  3799. '$mod.s = $mod.s + fly() + "e";',
  3800. '$mod.s = fly() + fly() + fly();',
  3801. '']));
  3802. end;
  3803. procedure TTestModule.TestLogicalOperators;
  3804. begin
  3805. StartProgram(false);
  3806. Add('var');
  3807. Add(' vA,vB,vC:boolean;');
  3808. Add('begin');
  3809. Add(' va:=vb and vc;');
  3810. Add(' va:=vb or vc;');
  3811. Add(' va:=vb xor vc;');
  3812. Add(' va:=true and vc;');
  3813. Add(' va:=(vb and vc) or (va and vb);');
  3814. Add(' va:=not vb;');
  3815. ConvertProgram;
  3816. CheckSource('TestLogicalOperators',
  3817. LinesToStr([ // statements
  3818. 'this.vA = false;',
  3819. 'this.vB = false;',
  3820. 'this.vC = false;'
  3821. ]),
  3822. LinesToStr([ // this.$main
  3823. '$mod.vA = $mod.vB && $mod.vC;',
  3824. '$mod.vA = $mod.vB || $mod.vC;',
  3825. '$mod.vA = $mod.vB ^ $mod.vC;',
  3826. '$mod.vA = true && $mod.vC;',
  3827. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3828. '$mod.vA = !$mod.vB;'
  3829. ]));
  3830. end;
  3831. procedure TTestModule.TestBitwiseOperators;
  3832. begin
  3833. StartProgram(false);
  3834. Add([
  3835. 'var',
  3836. ' vA,vB,vC:longint;',
  3837. ' X,Y,Z: nativeint;',
  3838. 'begin',
  3839. ' va:=vb and vc;',
  3840. ' va:=vb or vc;',
  3841. ' va:=vb xor vc;',
  3842. ' va:=vb shl vc;',
  3843. ' va:=vb shr vc;',
  3844. ' va:=3 and vc;',
  3845. ' va:=(vb and vc) or (va and vb);',
  3846. ' va:=not vb;',
  3847. ' X:=Y and Z;',
  3848. ' X:=Y and va;',
  3849. ' X:=Y or Z;',
  3850. ' X:=Y or va;',
  3851. ' X:=Y xor Z;',
  3852. ' X:=Y xor va;',
  3853. '']);
  3854. ConvertProgram;
  3855. CheckSource('TestBitwiseOperators',
  3856. LinesToStr([ // statements
  3857. 'this.vA = 0;',
  3858. 'this.vB = 0;',
  3859. 'this.vC = 0;',
  3860. 'this.X = 0;',
  3861. 'this.Y = 0;',
  3862. 'this.Z = 0;',
  3863. '']),
  3864. LinesToStr([ // this.$main
  3865. '$mod.vA = $mod.vB & $mod.vC;',
  3866. '$mod.vA = $mod.vB | $mod.vC;',
  3867. '$mod.vA = $mod.vB ^ $mod.vC;',
  3868. '$mod.vA = $mod.vB << $mod.vC;',
  3869. '$mod.vA = $mod.vB >>> $mod.vC;',
  3870. '$mod.vA = 3 & $mod.vC;',
  3871. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3872. '$mod.vA = ~$mod.vB;',
  3873. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3874. '$mod.X = $mod.Y & $mod.vA;',
  3875. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3876. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3877. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3878. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3879. '']));
  3880. end;
  3881. procedure TTestModule.TestBitwiseOperatorsLongword;
  3882. begin
  3883. StartProgram(false);
  3884. Add([
  3885. 'var',
  3886. ' a,b,c:longword;',
  3887. ' i: longint;',
  3888. 'begin',
  3889. ' a:=$12345678;',
  3890. ' b:=$EDCBA987;',
  3891. ' c:=not a;',
  3892. ' c:=a and b;',
  3893. ' c:=a and $ffff0000;',
  3894. ' c:=a or b;',
  3895. ' c:=a or $ff00ff00;',
  3896. ' c:=a xor b;',
  3897. ' c:=a xor $f0f0f0f0;',
  3898. ' c:=a shl 1;',
  3899. ' c:=a shl 16;',
  3900. ' c:=a shl 24;',
  3901. ' c:=a shl b;',
  3902. ' c:=a shr 1;',
  3903. ' c:=a shr 16;',
  3904. ' c:=a shr 24;',
  3905. ' c:=a shr b;',
  3906. ' c:=(b and c) or (a and b);',
  3907. ' c:=i and a;',
  3908. ' c:=i or a;',
  3909. ' c:=i xor a;',
  3910. '']);
  3911. ConvertProgram;
  3912. CheckSource('TestBitwiseOperatorsLongword',
  3913. LinesToStr([ // statements
  3914. 'this.a = 0;',
  3915. 'this.b = 0;',
  3916. 'this.c = 0;',
  3917. 'this.i = 0;',
  3918. '']),
  3919. LinesToStr([ // this.$main
  3920. '$mod.a = 0x12345678;',
  3921. '$mod.b = 0xEDCBA987;',
  3922. '$mod.c = rtl.lw(~$mod.a);',
  3923. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3924. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3925. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3926. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3927. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3928. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3929. '$mod.c = rtl.lw($mod.a << 1);',
  3930. '$mod.c = rtl.lw($mod.a << 16);',
  3931. '$mod.c = rtl.lw($mod.a << 24);',
  3932. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3933. '$mod.c = rtl.lw($mod.a >>> 1);',
  3934. '$mod.c = rtl.lw($mod.a >>> 16);',
  3935. '$mod.c = rtl.lw($mod.a >>> 24);',
  3936. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3937. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3938. '$mod.c = $mod.i & $mod.a;',
  3939. '$mod.c = $mod.i | $mod.a;',
  3940. '$mod.c = $mod.i ^ $mod.a;',
  3941. '']));
  3942. end;
  3943. procedure TTestModule.TestPrgProcVar;
  3944. begin
  3945. StartProgram(false);
  3946. Add('procedure Proc1;');
  3947. Add('type');
  3948. Add(' t1=longint;');
  3949. Add('var');
  3950. Add(' vA:t1;');
  3951. Add('begin');
  3952. Add('end;');
  3953. Add('begin');
  3954. ConvertProgram;
  3955. CheckSource('TestPrgProcVar',
  3956. LinesToStr([ // statements
  3957. 'this.Proc1 = function () {',
  3958. ' var vA=0;',
  3959. '};'
  3960. ]),
  3961. LinesToStr([ // this.$main
  3962. ''
  3963. ]));
  3964. end;
  3965. procedure TTestModule.TestUnitProcVar;
  3966. begin
  3967. StartUnit(false);
  3968. Add('interface');
  3969. Add('');
  3970. Add('type tA=string; // unit scope');
  3971. Add('procedure Proc1;');
  3972. Add('');
  3973. Add('implementation');
  3974. Add('');
  3975. Add('procedure Proc1;');
  3976. Add('type tA=longint; // local proc scope');
  3977. Add('var v1:tA; // using local tA');
  3978. Add('begin');
  3979. Add('end;');
  3980. Add('var v2:tA; // using interface tA');
  3981. ConvertUnit;
  3982. CheckSource('TestUnitProcVar',
  3983. LinesToStr([ // statements
  3984. 'var $impl = $mod.$impl;',
  3985. 'this.Proc1 = function () {',
  3986. ' var v1 = 0;',
  3987. '};',
  3988. '']),
  3989. // this.$init
  3990. '',
  3991. // implementation
  3992. LinesToStr([
  3993. '$impl.v2 = "";',
  3994. '']));
  3995. end;
  3996. procedure TTestModule.TestImplProc;
  3997. begin
  3998. StartUnit(false);
  3999. Add('interface');
  4000. Add('');
  4001. Add('procedure Proc1;');
  4002. Add('');
  4003. Add('implementation');
  4004. Add('');
  4005. Add('procedure Proc1; begin end;');
  4006. Add('procedure Proc2; begin end;');
  4007. Add('initialization');
  4008. Add(' Proc1;');
  4009. Add(' Proc2;');
  4010. ConvertUnit;
  4011. CheckSource('TestImplProc',
  4012. LinesToStr([ // statements
  4013. 'var $impl = $mod.$impl;',
  4014. 'this.Proc1 = function () {',
  4015. '};',
  4016. '']),
  4017. LinesToStr([ // this.$init
  4018. '$mod.Proc1();',
  4019. '$impl.Proc2();',
  4020. '']),
  4021. LinesToStr([ // implementation
  4022. '$impl.Proc2 = function () {',
  4023. '};',
  4024. ''])
  4025. );
  4026. end;
  4027. procedure TTestModule.TestFunctionResult;
  4028. begin
  4029. StartProgram(false);
  4030. Add('function Func1: longint;');
  4031. Add('begin');
  4032. Add(' Result:=3;');
  4033. Add(' Func1:=4;');
  4034. Add('end;');
  4035. Add('begin');
  4036. ConvertProgram;
  4037. CheckSource('TestFunctionResult',
  4038. LinesToStr([ // statements
  4039. 'this.Func1 = function () {',
  4040. ' var Result = 0;',
  4041. ' Result = 3;',
  4042. ' Result = 4;',
  4043. ' return Result;',
  4044. '};'
  4045. ]),
  4046. '');
  4047. end;
  4048. procedure TTestModule.TestNestedProc;
  4049. begin
  4050. StartProgram(false);
  4051. Add([
  4052. 'var vInUnit: longint;',
  4053. 'function DoIt(pA,pD: longint): longint;',
  4054. 'var',
  4055. ' vB: longint;',
  4056. ' vC: longint;',
  4057. ' function Nesty(pA: longint): longint; ',
  4058. ' var vB: longint;',
  4059. ' begin',
  4060. ' Result:=pa+vb+vc+pd+vInUnit;',
  4061. ' nesty:=3;',
  4062. ' doit:=4;',
  4063. ' exit;',
  4064. ' end;',
  4065. 'begin',
  4066. ' Result:=pa+vb+vc;',
  4067. ' doit:=6;',
  4068. ' exit;',
  4069. 'end;',
  4070. 'begin']);
  4071. ConvertProgram;
  4072. CheckSource('TestNestedProc',
  4073. LinesToStr([ // statements
  4074. 'this.vInUnit = 0;',
  4075. 'this.DoIt = function (pA, pD) {',
  4076. ' var Result = 0;',
  4077. ' var vB = 0;',
  4078. ' var vC = 0;',
  4079. ' function Nesty(pA) {',
  4080. ' var Result$1 = 0;',
  4081. ' var vB = 0;',
  4082. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4083. ' Result$1 = 3;',
  4084. ' Result = 4;',
  4085. ' return Result$1;',
  4086. ' return Result$1;',
  4087. ' };',
  4088. ' Result = pA + vB + vC;',
  4089. ' Result = 6;',
  4090. ' return Result;',
  4091. ' return Result;',
  4092. '};'
  4093. ]),
  4094. '');
  4095. end;
  4096. procedure TTestModule.TestNestedProc_ResultString;
  4097. begin
  4098. StartProgram(false);
  4099. Add([
  4100. 'function DoIt: string;',
  4101. ' function Nesty: string; ',
  4102. ' begin',
  4103. ' nesty:=#65#66;',
  4104. ' nesty[1]:=#67;',
  4105. ' doit:=#68;',
  4106. ' doit[2]:=#69;',
  4107. ' end;',
  4108. 'begin',
  4109. ' doit:=#70;',
  4110. ' doit[3]:=#71;',
  4111. 'end;',
  4112. 'begin']);
  4113. ConvertProgram;
  4114. CheckSource('TestNestedProc_ResultString',
  4115. LinesToStr([ // statements
  4116. 'this.DoIt = function () {',
  4117. ' var Result = "";',
  4118. ' function Nesty() {',
  4119. ' var Result$1 = "";',
  4120. ' Result$1 = "AB";',
  4121. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4122. ' Result = "D";',
  4123. ' Result = rtl.setCharAt(Result, 1, "E");',
  4124. ' return Result$1;',
  4125. ' };',
  4126. ' Result = "F";',
  4127. ' Result = rtl.setCharAt(Result, 2, "G");',
  4128. ' return Result;',
  4129. '};'
  4130. ]),
  4131. '');
  4132. end;
  4133. procedure TTestModule.TestForwardProc;
  4134. begin
  4135. StartProgram(false);
  4136. Add('procedure FuncA(Bar: longint); forward;');
  4137. Add('procedure FuncB(Bar: longint);');
  4138. Add('begin');
  4139. Add(' funca(bar);');
  4140. Add('end;');
  4141. Add('procedure funca(bar: longint);');
  4142. Add('begin');
  4143. Add(' if bar=3 then ;');
  4144. Add('end;');
  4145. Add('begin');
  4146. Add(' funca(4);');
  4147. Add(' funcb(5);');
  4148. ConvertProgram;
  4149. CheckSource('TestForwardProc',
  4150. LinesToStr([ // statements'
  4151. 'this.FuncB = function (Bar) {',
  4152. ' $mod.FuncA(Bar);',
  4153. '};',
  4154. 'this.FuncA = function (Bar) {',
  4155. ' if (Bar === 3);',
  4156. '};'
  4157. ]),
  4158. LinesToStr([
  4159. '$mod.FuncA(4);',
  4160. '$mod.FuncB(5);'
  4161. ])
  4162. );
  4163. end;
  4164. procedure TTestModule.TestNestedForwardProc;
  4165. begin
  4166. StartProgram(false);
  4167. Add('procedure FuncA;');
  4168. Add(' procedure FuncB(i: longint); forward;');
  4169. Add(' procedure FuncC(i: longint);');
  4170. Add(' begin');
  4171. Add(' funcb(i);');
  4172. Add(' end;');
  4173. Add(' procedure FuncB(i: longint);');
  4174. Add(' begin');
  4175. Add(' if i=3 then ;');
  4176. Add(' end;');
  4177. Add('begin');
  4178. Add(' funcc(4)');
  4179. Add('end;');
  4180. Add('begin');
  4181. Add(' funca;');
  4182. ConvertProgram;
  4183. CheckSource('TestNestedForwardProc',
  4184. LinesToStr([ // statements'
  4185. 'this.FuncA = function () {',
  4186. ' function FuncC(i) {',
  4187. ' FuncB(i);',
  4188. ' };',
  4189. ' function FuncB(i) {',
  4190. ' if (i === 3);',
  4191. ' };',
  4192. ' FuncC(4);',
  4193. '};'
  4194. ]),
  4195. LinesToStr([
  4196. '$mod.FuncA();'
  4197. ])
  4198. );
  4199. end;
  4200. procedure TTestModule.TestAssignFunctionResult;
  4201. begin
  4202. StartProgram(false);
  4203. Add('function Func1: longint;');
  4204. Add('begin');
  4205. Add('end;');
  4206. Add('var i: longint;');
  4207. Add('begin');
  4208. Add(' i:=func1();');
  4209. Add(' i:=func1()+func1();');
  4210. ConvertProgram;
  4211. CheckSource('TestAssignFunctionResult',
  4212. LinesToStr([ // statements
  4213. 'this.Func1 = function () {',
  4214. ' var Result = 0;',
  4215. ' return Result;',
  4216. '};',
  4217. 'this.i = 0;'
  4218. ]),
  4219. LinesToStr([
  4220. '$mod.i = $mod.Func1();',
  4221. '$mod.i = $mod.Func1() + $mod.Func1();'
  4222. ]));
  4223. end;
  4224. procedure TTestModule.TestFunctionResultInCondition;
  4225. begin
  4226. StartProgram(false);
  4227. Add('function Func1: longint;');
  4228. Add('begin');
  4229. Add('end;');
  4230. Add('function Func2: boolean;');
  4231. Add('begin');
  4232. Add('end;');
  4233. Add('var i: longint;');
  4234. Add('begin');
  4235. Add(' if func2 then ;');
  4236. Add(' if i=func1() then ;');
  4237. Add(' if i=func1 then ;');
  4238. ConvertProgram;
  4239. CheckSource('TestFunctionResultInCondition',
  4240. LinesToStr([ // statements
  4241. 'this.Func1 = function () {',
  4242. ' var Result = 0;',
  4243. ' return Result;',
  4244. '};',
  4245. 'this.Func2 = function () {',
  4246. ' var Result = false;',
  4247. ' return Result;',
  4248. '};',
  4249. 'this.i = 0;'
  4250. ]),
  4251. LinesToStr([
  4252. 'if ($mod.Func2());',
  4253. 'if ($mod.i === $mod.Func1());',
  4254. 'if ($mod.i === $mod.Func1());'
  4255. ]));
  4256. end;
  4257. procedure TTestModule.TestFunctionResultInForLoop;
  4258. begin
  4259. StartProgram(false);
  4260. Add([
  4261. 'function Func1(a: array of longint): longint;',
  4262. 'begin',
  4263. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4264. ' for Result in a do if a[Result]=0 then exit;',
  4265. 'end;',
  4266. 'begin',
  4267. ' Func1([1,2,3])']);
  4268. ConvertProgram;
  4269. CheckSource('TestFunctionResultInForLoop',
  4270. LinesToStr([ // statements
  4271. 'this.Func1 = function (a) {',
  4272. ' var Result = 0;',
  4273. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4274. ' Result = $l;',
  4275. ' if (a[Result] === 0) return Result;',
  4276. ' };',
  4277. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4278. ' Result = $in[$l1];',
  4279. ' if (a[Result] === 0) return Result;',
  4280. ' };',
  4281. ' return Result;',
  4282. '};',
  4283. '']),
  4284. LinesToStr([
  4285. '$mod.Func1([1, 2, 3]);'
  4286. ]));
  4287. end;
  4288. procedure TTestModule.TestFunctionResultInTypeCast;
  4289. begin
  4290. StartProgram(false);
  4291. Add([
  4292. 'function GetInt: longint;',
  4293. 'begin',
  4294. 'end;',
  4295. 'begin',
  4296. ' if Byte(GetInt)=0 then ;',
  4297. '']);
  4298. ConvertProgram;
  4299. CheckSource('TestFunctionResultInTypeCast',
  4300. LinesToStr([ // statements
  4301. 'this.GetInt = function () {',
  4302. ' var Result = 0;',
  4303. ' return Result;',
  4304. '};',
  4305. '']),
  4306. LinesToStr([
  4307. 'if (($mod.GetInt() & 255) === 0) ;'
  4308. ]));
  4309. end;
  4310. procedure TTestModule.TestExit;
  4311. begin
  4312. StartProgram(false);
  4313. Add('procedure ProcA;');
  4314. Add('begin');
  4315. Add(' exit;');
  4316. Add('end;');
  4317. Add('function FuncB: longint;');
  4318. Add('begin');
  4319. Add(' exit;');
  4320. Add(' exit(3);');
  4321. Add('end;');
  4322. Add('function FuncC: string;');
  4323. Add('begin');
  4324. Add(' exit;');
  4325. Add(' exit(''a'');');
  4326. Add(' exit(''abc'');');
  4327. Add('end;');
  4328. Add('begin');
  4329. Add(' exit;');
  4330. Add(' exit(1);');
  4331. ConvertProgram;
  4332. CheckSource('TestExit',
  4333. LinesToStr([ // statements
  4334. 'this.ProcA = function () {',
  4335. ' return;',
  4336. '};',
  4337. 'this.FuncB = function () {',
  4338. ' var Result = 0;',
  4339. ' return Result;',
  4340. ' return 3;',
  4341. ' return Result;',
  4342. '};',
  4343. 'this.FuncC = function () {',
  4344. ' var Result = "";',
  4345. ' return Result;',
  4346. ' return "a";',
  4347. ' return "abc";',
  4348. ' return Result;',
  4349. '};'
  4350. ]),
  4351. LinesToStr([
  4352. 'return;',
  4353. 'return 1;',
  4354. '']));
  4355. end;
  4356. procedure TTestModule.TestExit_ResultInFinally;
  4357. begin
  4358. StartProgram(false);
  4359. Add([
  4360. 'function Run: word;',
  4361. 'begin',
  4362. ' try',
  4363. ' exit(3);', // no Result in finally -> use return 3
  4364. ' finally',
  4365. ' end;',
  4366. 'end;',
  4367. 'function Fly: word;',
  4368. 'begin',
  4369. ' try',
  4370. ' exit(3);',
  4371. ' finally',
  4372. ' if Result>0 then ;',
  4373. ' end;',
  4374. 'end;',
  4375. 'function Jump: word;',
  4376. 'begin',
  4377. ' try',
  4378. ' try',
  4379. ' exit(4);',
  4380. ' finally',
  4381. ' end;',
  4382. ' finally',
  4383. ' if Result>0 then ;',
  4384. ' end;',
  4385. 'end;',
  4386. 'begin',
  4387. '']);
  4388. ConvertProgram;
  4389. CheckSource('TestExit_ResultInFinally',
  4390. LinesToStr([ // statements
  4391. 'this.Run = function () {',
  4392. ' var Result = 0;',
  4393. ' try {',
  4394. ' return 3;',
  4395. ' } finally {',
  4396. ' };',
  4397. ' return Result;',
  4398. '};',
  4399. 'this.Fly = function () {',
  4400. ' var Result = 0;',
  4401. ' try {',
  4402. ' Result = 3;',
  4403. ' return Result;',
  4404. ' } finally {',
  4405. ' if (Result > 0) ;',
  4406. ' };',
  4407. ' return Result;',
  4408. '};',
  4409. 'this.Jump = function () {',
  4410. ' var Result = 0;',
  4411. ' try {',
  4412. ' try {',
  4413. ' Result = 4;',
  4414. ' return Result;',
  4415. ' } finally {',
  4416. ' };',
  4417. ' } finally {',
  4418. ' if (Result > 0) ;',
  4419. ' };',
  4420. ' return Result;',
  4421. '};',
  4422. '']),
  4423. LinesToStr([
  4424. '']));
  4425. end;
  4426. procedure TTestModule.TestBreak;
  4427. begin
  4428. StartProgram(false);
  4429. Add([
  4430. 'var',
  4431. ' i: longint;',
  4432. 'begin',
  4433. ' repeat',
  4434. ' break;',
  4435. ' until true;',
  4436. ' while true do',
  4437. ' break;',
  4438. ' for i:=1 to 2 do',
  4439. ' break;']);
  4440. ConvertProgram;
  4441. CheckSource('TestBreak',
  4442. LinesToStr([ // statements
  4443. 'this.i = 0;'
  4444. ]),
  4445. LinesToStr([
  4446. 'do {',
  4447. ' break;',
  4448. '} while (!true);',
  4449. 'while (true) break;',
  4450. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4451. '']));
  4452. end;
  4453. procedure TTestModule.TestBreakAsVar;
  4454. begin
  4455. StartProgram(false);
  4456. Add([
  4457. 'procedure DoIt(break: boolean);',
  4458. 'begin',
  4459. ' if break then ;',
  4460. 'end;',
  4461. 'var',
  4462. ' break: boolean;',
  4463. 'begin',
  4464. ' if break then ;']);
  4465. ConvertProgram;
  4466. CheckSource('TestBreakAsVar',
  4467. LinesToStr([ // statements
  4468. 'this.DoIt = function (Break) {',
  4469. ' if (Break) ;',
  4470. '};',
  4471. 'this.Break = false;',
  4472. '']),
  4473. LinesToStr([
  4474. 'if($mod.Break) ;',
  4475. '']));
  4476. end;
  4477. procedure TTestModule.TestContinue;
  4478. begin
  4479. StartProgram(false);
  4480. Add('var i: longint;');
  4481. Add('begin');
  4482. Add(' repeat');
  4483. Add(' continue;');
  4484. Add(' until true;');
  4485. Add(' while true do');
  4486. Add(' continue;');
  4487. Add(' for i:=1 to 2 do');
  4488. Add(' continue;');
  4489. ConvertProgram;
  4490. CheckSource('TestContinue',
  4491. LinesToStr([ // statements
  4492. 'this.i = 0;'
  4493. ]),
  4494. LinesToStr([
  4495. 'do {',
  4496. ' continue;',
  4497. '} while (!true);',
  4498. 'while (true) continue;',
  4499. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4500. '']));
  4501. end;
  4502. procedure TTestModule.TestProc_External;
  4503. begin
  4504. StartProgram(false);
  4505. Add('procedure Foo; external name ''console.log'';');
  4506. Add('function Bar: longint; external name ''get.item'';');
  4507. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4508. Add('var');
  4509. Add(' i: longint;');
  4510. Add('begin');
  4511. Add(' Foo;');
  4512. Add(' i:=Bar;');
  4513. Add(' i:=Bla(''abc'');');
  4514. ConvertProgram;
  4515. CheckSource('TestProc_External',
  4516. LinesToStr([ // statements
  4517. 'this.i = 0;'
  4518. ]),
  4519. LinesToStr([
  4520. 'console.log();',
  4521. '$mod.i = get.item();',
  4522. '$mod.i = apply.something("abc");'
  4523. ]));
  4524. end;
  4525. procedure TTestModule.TestProc_ExternalOtherUnit;
  4526. begin
  4527. AddModuleWithIntfImplSrc('unit2.pas',
  4528. LinesToStr([
  4529. 'procedure Now; external name ''Date.now'';',
  4530. 'procedure DoIt;'
  4531. ]),
  4532. 'procedure doit; begin end;');
  4533. StartUnit(true);
  4534. Add('interface');
  4535. Add('uses unit2;');
  4536. Add('implementation');
  4537. Add('begin');
  4538. Add(' now;');
  4539. Add(' now();');
  4540. Add(' uNit2.now;');
  4541. Add(' uNit2.now();');
  4542. Add(' doit;');
  4543. Add(' uNit2.doit;');
  4544. ConvertUnit;
  4545. CheckSource('TestProc_ExternalOtherUnit',
  4546. LinesToStr([
  4547. '']),
  4548. LinesToStr([
  4549. 'Date.now();',
  4550. 'Date.now();',
  4551. 'Date.now();',
  4552. 'Date.now();',
  4553. 'pas.unit2.DoIt();',
  4554. 'pas.unit2.DoIt();',
  4555. '']));
  4556. end;
  4557. procedure TTestModule.TestProc_Asm;
  4558. begin
  4559. StartProgram(false);
  4560. Add([
  4561. '{$mode delphi}',
  4562. 'function DoIt: longint;',
  4563. 'begin;',
  4564. ' asm',
  4565. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4566. ' end;',
  4567. ' asm console.log(); end;',
  4568. ' asm',
  4569. ' s = "'' ";',
  4570. ' s = ''" '';',
  4571. ' s = s + "world" + "''";',
  4572. ' // end',
  4573. ' s = ''end'';',
  4574. ' s = "end";',
  4575. ' s = "foo\"bar";',
  4576. ' s = ''a\''b'';',
  4577. ' s = `${expr}\`-"-''-`;',
  4578. ' s = `multi',
  4579. 'line`;',
  4580. ' end;',
  4581. 'end;',
  4582. 'procedure Fly;',
  4583. 'asm',
  4584. ' return;',
  4585. 'end;',
  4586. 'begin']);
  4587. ConvertProgram;
  4588. CheckSource('TestProc_Asm',
  4589. LinesToStr([ // statements
  4590. 'this.DoIt = function () {',
  4591. ' var Result = 0;',
  4592. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4593. ' console.log();',
  4594. ' s = "'' ";',
  4595. ' s = ''" '';',
  4596. ' s = s + "world" + "''";',
  4597. ' // end',
  4598. ' s = ''end'';',
  4599. ' s = "end";',
  4600. ' s = "foo\"bar";',
  4601. ' s = ''a\''b'';',
  4602. ' s = `${expr}\`-"-''-`;',
  4603. ' s = `multi',
  4604. 'line`;',
  4605. ' return Result;',
  4606. '};',
  4607. 'this.Fly = function () {',
  4608. ' return;',
  4609. '};',
  4610. '']),
  4611. LinesToStr([
  4612. ''
  4613. ]));
  4614. end;
  4615. procedure TTestModule.TestProc_AsmSubBlock;
  4616. begin
  4617. StartProgram(true,[supTObject]);
  4618. Add([
  4619. '{$mode delphi}',
  4620. 'type',
  4621. ' TBird = class end;',
  4622. 'procedure Run(w: word);',
  4623. 'begin;',
  4624. ' if true then asm console.log(); end;',
  4625. ' if w>3 then asm',
  4626. ' var a = w+1;',
  4627. ' w = a+3;',
  4628. ' end;',
  4629. ' while (w>7) do asm',
  4630. ' w+=3; w*=2;',
  4631. ' end;',
  4632. ' try',
  4633. ' except',
  4634. ' on E: TBird do',
  4635. ' asm console.log(E); end;',
  4636. ' on E: TObject do',
  4637. ' asm var i=3; i--; end;',
  4638. ' else asm Fly; High; end;',
  4639. ' end;',
  4640. 'end;',
  4641. 'begin']);
  4642. ConvertProgram;
  4643. CheckSource('TestProc_AsmSubBlock',
  4644. LinesToStr([ // statements
  4645. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4646. '});',
  4647. 'this.Run = function (w) {',
  4648. ' if (true) console.log();',
  4649. ' if (w > 3) {',
  4650. ' var a = w+1;',
  4651. ' w = a+3;',
  4652. ' };',
  4653. ' while (w > 7) {',
  4654. ' w+=3; w*=2;',
  4655. ' };',
  4656. ' try {} catch ($e) {',
  4657. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4658. ' var E = $e;',
  4659. ' console.log(E);',
  4660. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4661. ' var E = $e;',
  4662. ' var i=3; i--;',
  4663. ' } else {',
  4664. ' Fly; High;',
  4665. ' }',
  4666. ' };',
  4667. '};',
  4668. '']),
  4669. LinesToStr([
  4670. ''
  4671. ]));
  4672. end;
  4673. procedure TTestModule.TestProc_Assembler;
  4674. begin
  4675. StartProgram(false);
  4676. Add('function DoIt: longint; assembler;');
  4677. Add('asm');
  4678. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4679. Add('end;');
  4680. Add('begin');
  4681. ConvertProgram;
  4682. CheckSource('TestProc_Assembler',
  4683. LinesToStr([ // statements
  4684. 'this.DoIt = function () {',
  4685. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4686. '};'
  4687. ]),
  4688. LinesToStr([
  4689. ''
  4690. ]));
  4691. end;
  4692. procedure TTestModule.TestProc_VarParam;
  4693. begin
  4694. StartProgram(false);
  4695. Add('type integer = longint;');
  4696. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4697. Add('var vJ: integer;');
  4698. Add('begin');
  4699. Add(' vg:=vg+1;');
  4700. Add(' vj:=vh+2;');
  4701. Add(' vi:=vi+3;');
  4702. Add(' doit(vg,vg,vg);');
  4703. Add(' doit(vh,vh,vj);');
  4704. Add(' doit(vi,vi,vi);');
  4705. Add(' doit(vj,vj,vj);');
  4706. Add('end;');
  4707. Add('var i: integer;');
  4708. Add('begin');
  4709. Add(' doit(i,i,i);');
  4710. ConvertProgram;
  4711. CheckSource('TestProc_VarParam',
  4712. LinesToStr([ // statements
  4713. 'this.DoIt = function (vG,vH,vI) {',
  4714. ' var vJ = 0;',
  4715. ' vG = vG + 1;',
  4716. ' vJ = vH + 2;',
  4717. ' vI.set(vI.get()+3);',
  4718. ' $mod.DoIt(vG, vG, {',
  4719. ' get: function () {',
  4720. ' return vG;',
  4721. ' },',
  4722. ' set: function (v) {',
  4723. ' vG = v;',
  4724. ' }',
  4725. ' });',
  4726. ' $mod.DoIt(vH, vH, {',
  4727. ' get: function () {',
  4728. ' return vJ;',
  4729. ' },',
  4730. ' set: function (v) {',
  4731. ' vJ = v;',
  4732. ' }',
  4733. ' });',
  4734. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4735. ' $mod.DoIt(vJ, vJ, {',
  4736. ' get: function () {',
  4737. ' return vJ;',
  4738. ' },',
  4739. ' set: function (v) {',
  4740. ' vJ = v;',
  4741. ' }',
  4742. ' });',
  4743. '};',
  4744. 'this.i = 0;'
  4745. ]),
  4746. LinesToStr([
  4747. '$mod.DoIt($mod.i,$mod.i,{',
  4748. ' p: $mod,',
  4749. ' get: function () {',
  4750. ' return this.p.i;',
  4751. ' },',
  4752. ' set: function (v) {',
  4753. ' this.p.i = v;',
  4754. ' }',
  4755. '});'
  4756. ]));
  4757. end;
  4758. procedure TTestModule.TestProc_VarParamString;
  4759. begin
  4760. StartProgram(false);
  4761. Add(['type TCaption = string;',
  4762. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4763. 'var c: char;',
  4764. 'begin',
  4765. ' va[1]:=c;',
  4766. ' vb[2]:=c;',
  4767. ' vc[3]:=c;',
  4768. 'end;',
  4769. 'begin']);
  4770. ConvertProgram;
  4771. CheckSource('TestProc_VarParamString',
  4772. LinesToStr([ // statements
  4773. 'this.DoIt = function (vA,vB,vC) {',
  4774. ' var c = "\x00";',
  4775. ' vA = rtl.setCharAt(vA, 0, c);',
  4776. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4777. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4778. '};',
  4779. '']),
  4780. LinesToStr([
  4781. ]));
  4782. end;
  4783. procedure TTestModule.TestProc_VarParamV;
  4784. begin
  4785. StartProgram(false);
  4786. Add([
  4787. 'procedure Inc2(var i: longint);',
  4788. 'begin',
  4789. ' i:=i+2;',
  4790. 'end;',
  4791. 'procedure DoIt(v: longint);',
  4792. 'var p: array of longint;',
  4793. 'begin',
  4794. ' Inc2(v);',
  4795. ' Inc2(p[v]);',
  4796. 'end;',
  4797. 'begin']);
  4798. ConvertProgram;
  4799. CheckSource('TestProc_VarParamV',
  4800. LinesToStr([ // statements
  4801. 'this.Inc2 = function (i) {',
  4802. ' i.set(i.get()+2);',
  4803. '};',
  4804. 'this.DoIt = function (v) {',
  4805. ' var p = [];',
  4806. ' $mod.Inc2({get: function () {',
  4807. ' return v;',
  4808. ' }, set: function (w) {',
  4809. ' v = w;',
  4810. ' }});',
  4811. ' $mod.Inc2({',
  4812. ' a: v,',
  4813. ' p: p,',
  4814. ' get: function () {',
  4815. ' return this.p[this.a];',
  4816. ' },',
  4817. ' set: function (v) {',
  4818. ' this.p[this.a] = v;',
  4819. ' }',
  4820. ' });',
  4821. '};',
  4822. '']),
  4823. LinesToStr([
  4824. '']));
  4825. end;
  4826. procedure TTestModule.TestProc_Overload;
  4827. begin
  4828. StartProgram(false);
  4829. Add('procedure DoIt(vI: longint); begin end;');
  4830. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4831. Add('procedure DoIt(vD: double); begin end;');
  4832. Add('begin');
  4833. Add(' DoIt(1);');
  4834. Add(' DoIt(2,3);');
  4835. Add(' DoIt(4.5);');
  4836. ConvertProgram;
  4837. CheckSource('TestProcedureOverload',
  4838. LinesToStr([ // statements
  4839. 'this.DoIt = function (vI) {',
  4840. '};',
  4841. 'this.DoIt$1 = function (vI, vJ) {',
  4842. '};',
  4843. 'this.DoIt$2 = function (vD) {',
  4844. '};',
  4845. '']),
  4846. LinesToStr([
  4847. '$mod.DoIt(1);',
  4848. '$mod.DoIt$1(2, 3);',
  4849. '$mod.DoIt$2(4.5);',
  4850. '']));
  4851. end;
  4852. procedure TTestModule.TestProc_OverloadForward;
  4853. begin
  4854. StartProgram(false);
  4855. Add('procedure DoIt(vI: longint); forward;');
  4856. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4857. Add('procedure doit(vi: longint); begin end;');
  4858. Add('begin');
  4859. Add(' doit(1);');
  4860. Add(' doit(2,3);');
  4861. ConvertProgram;
  4862. CheckSource('TestProcedureOverloadForward',
  4863. LinesToStr([ // statements
  4864. 'this.DoIt$1 = function (vI, vJ) {',
  4865. '};',
  4866. 'this.DoIt = function (vI) {',
  4867. '};',
  4868. '']),
  4869. LinesToStr([
  4870. '$mod.DoIt(1);',
  4871. '$mod.DoIt$1(2, 3);',
  4872. '']));
  4873. end;
  4874. procedure TTestModule.TestProc_OverloadIntfImpl;
  4875. begin
  4876. StartUnit(false);
  4877. Add('interface');
  4878. Add('procedure DoIt(vI: longint);');
  4879. Add('procedure DoIt(vI, vJ: longint);');
  4880. Add('implementation');
  4881. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4882. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4883. Add('procedure DoIt(vi: longint); begin end;');
  4884. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4885. Add('procedure DoIt(vi, vj: longint); begin end;');
  4886. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4887. Add('begin');
  4888. Add(' doit(1);');
  4889. Add(' doit(2,3);');
  4890. Add(' doit(4,5,6);');
  4891. Add(' doit(7,8,9,10);');
  4892. Add(' doit(11,12,13,14,15);');
  4893. ConvertUnit;
  4894. CheckSource('TestProcedureOverloadUnit',
  4895. LinesToStr([ // statements
  4896. 'var $impl = $mod.$impl;',
  4897. 'this.DoIt = function (vI) {',
  4898. '};',
  4899. 'this.DoIt$1 = function (vI, vJ) {',
  4900. '};',
  4901. '']),
  4902. LinesToStr([ // this.$init
  4903. '$mod.DoIt(1);',
  4904. '$mod.DoIt$1(2, 3);',
  4905. '$impl.DoIt$3(4,5,6);',
  4906. '$impl.DoIt$4(7,8,9,10);',
  4907. '$impl.DoIt$2(11,12,13,14,15);',
  4908. '']),
  4909. LinesToStr([ // implementation
  4910. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4911. '};',
  4912. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4913. '};',
  4914. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4915. '};',
  4916. '']));
  4917. end;
  4918. procedure TTestModule.TestProc_OverloadNested;
  4919. begin
  4920. StartProgram(false);
  4921. Add([
  4922. 'procedure doit(vA: longint);',
  4923. ' procedure DoIt(vA, vB: longint); overload;',
  4924. ' begin',
  4925. ' doit(1);',
  4926. ' doit(1,2);',
  4927. ' end;',
  4928. ' procedure doit(vA, vB, vC: longint);',
  4929. ' begin',
  4930. ' doit(1);',
  4931. ' doit(1,2);',
  4932. ' doit(1,2,3);',
  4933. ' end;',
  4934. 'begin',
  4935. ' doit(1);',
  4936. ' doit(1,2);',
  4937. ' doit(1,2,3);',
  4938. 'end;',
  4939. 'begin // main',
  4940. ' doit(1);']);
  4941. ConvertProgram;
  4942. CheckSource('TestProcedureOverloadNested',
  4943. LinesToStr([ // statements
  4944. 'this.doit = function (vA) {',
  4945. ' function DoIt$1(vA, vB) {',
  4946. ' $mod.doit(1);',
  4947. ' DoIt$1(1, 2);',
  4948. ' };',
  4949. ' function doit$2(vA, vB, vC) {',
  4950. ' $mod.doit(1);',
  4951. ' DoIt$1(1, 2);',
  4952. ' doit$2(1, 2, 3);',
  4953. ' };',
  4954. ' $mod.doit(1);',
  4955. ' DoIt$1(1, 2);',
  4956. ' doit$2(1, 2, 3);',
  4957. '};',
  4958. '']),
  4959. LinesToStr([
  4960. '$mod.doit(1);',
  4961. '']));
  4962. end;
  4963. procedure TTestModule.TestProc_OverloadNestedForward;
  4964. begin
  4965. StartProgram(false);
  4966. Add([
  4967. 'procedure DoIt(vA: longint); overload; forward;',
  4968. 'procedure DoIt(vB, vC: longint); overload;',
  4969. 'begin // 2 param overload',
  4970. ' doit(1);',
  4971. ' doit(1,2);',
  4972. 'end;',
  4973. 'procedure doit(vA: longint);',
  4974. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4975. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4976. ' begin // 4 param overload',
  4977. ' doit(1);',
  4978. ' doit(1,2);',
  4979. ' doit(1,2,3);',
  4980. ' doit(1,2,3,4);',
  4981. ' end;',
  4982. ' procedure doit(vA, vB, vC: longint);',
  4983. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4984. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4985. ' begin // 6 param overload',
  4986. ' doit(1);',
  4987. ' doit(1,2);',
  4988. ' doit(1,2,3);',
  4989. ' doit(1,2,3,4);',
  4990. ' doit(1,2,3,4,5);',
  4991. ' doit(1,2,3,4,5,6);',
  4992. ' end;',
  4993. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4994. ' begin // 5 param overload',
  4995. ' doit(1);',
  4996. ' doit(1,2);',
  4997. ' doit(1,2,3);',
  4998. ' doit(1,2,3,4);',
  4999. ' doit(1,2,3,4,5);',
  5000. ' doit(1,2,3,4,5,6);',
  5001. ' end;',
  5002. ' begin // 3 param overload',
  5003. ' doit(1);',
  5004. ' doit(1,2);',
  5005. ' doit(1,2,3);',
  5006. ' doit(1,2,3,4);',
  5007. ' doit(1,2,3,4,5);',
  5008. ' doit(1,2,3,4,5,6);',
  5009. ' end;',
  5010. 'begin // 1 param overload',
  5011. ' doit(1);',
  5012. ' doit(1,2);',
  5013. ' doit(1,2,3);',
  5014. ' doit(1,2,3,4);',
  5015. 'end;',
  5016. 'begin // main',
  5017. ' doit(1);',
  5018. ' doit(1,2);']);
  5019. ConvertProgram;
  5020. CheckSource('TestProc_OverloadNestedForward',
  5021. LinesToStr([ // statements
  5022. 'this.DoIt$1 = function (vB, vC) {',
  5023. ' $mod.DoIt(1);',
  5024. ' $mod.DoIt$1(1, 2);',
  5025. '};',
  5026. 'this.DoIt = function (vA) {',
  5027. ' function DoIt$3(vA, vB, vC, vD) {',
  5028. ' $mod.DoIt(1);',
  5029. ' $mod.DoIt$1(1, 2);',
  5030. ' DoIt$2(1, 2, 3);',
  5031. ' DoIt$3(1, 2, 3, 4);',
  5032. ' };',
  5033. ' function DoIt$2(vA, vB, vC) {',
  5034. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5035. ' $mod.DoIt(1);',
  5036. ' $mod.DoIt$1(1, 2);',
  5037. ' DoIt$2(1, 2, 3);',
  5038. ' DoIt$3(1, 2, 3, 4);',
  5039. ' DoIt$4(1, 2, 3, 4, 5);',
  5040. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5041. ' };',
  5042. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5043. ' $mod.DoIt(1);',
  5044. ' $mod.DoIt$1(1, 2);',
  5045. ' DoIt$2(1, 2, 3);',
  5046. ' DoIt$3(1, 2, 3, 4);',
  5047. ' DoIt$4(1, 2, 3, 4, 5);',
  5048. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5049. ' };',
  5050. ' $mod.DoIt(1);',
  5051. ' $mod.DoIt$1(1, 2);',
  5052. ' DoIt$2(1, 2, 3);',
  5053. ' DoIt$3(1, 2, 3, 4);',
  5054. ' DoIt$4(1, 2, 3, 4, 5);',
  5055. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5056. ' };',
  5057. ' $mod.DoIt(1);',
  5058. ' $mod.DoIt$1(1, 2);',
  5059. ' DoIt$2(1, 2, 3);',
  5060. ' DoIt$3(1, 2, 3, 4);',
  5061. '};',
  5062. '']),
  5063. LinesToStr([
  5064. '$mod.DoIt(1);',
  5065. '$mod.DoIt$1(1, 2);',
  5066. '']));
  5067. end;
  5068. procedure TTestModule.TestProc_OverloadUnitCycle;
  5069. begin
  5070. AddModuleWithIntfImplSrc('Unit2.pas',
  5071. LinesToStr([
  5072. 'type',
  5073. ' TObject = class',
  5074. ' procedure DoIt(b: boolean); virtual; abstract;',
  5075. ' procedure DoIt(i: longint); virtual; abstract;',
  5076. ' end;',
  5077. '']),
  5078. 'uses test1;');
  5079. StartUnit(true);
  5080. Add([
  5081. 'interface',
  5082. 'uses unit2;',
  5083. 'type',
  5084. ' TEagle = class(TObject)',
  5085. ' procedure DoIt(b: boolean); override;',
  5086. ' procedure DoIt(i: longint); override;',
  5087. ' end;',
  5088. 'implementation',
  5089. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5090. 'procedure TEagle.DoIt(i: longint); begin end;',
  5091. '']);
  5092. ConvertUnit;
  5093. CheckSource('TestProc_OverloadUnitCycle',
  5094. LinesToStr([ // statements
  5095. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5096. ' this.DoIt = function (b) {',
  5097. ' };',
  5098. ' this.DoIt$1 = function (i) {',
  5099. ' };',
  5100. '});',
  5101. '']),
  5102. '',
  5103. LinesToStr([
  5104. '']));
  5105. end;
  5106. procedure TTestModule.TestProc_Varargs;
  5107. begin
  5108. StartProgram(false);
  5109. Add([
  5110. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5111. 'procedure ProcB; varargs; external name ''ProcB'';',
  5112. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5113. 'function GetIt: longint; begin end;',
  5114. 'begin',
  5115. ' ProcA(1);',
  5116. ' ProcA(1,2);',
  5117. ' ProcA(1,2.0);',
  5118. ' ProcA(1,2,3);',
  5119. ' ProcA(1,''2'');',
  5120. ' ProcA(2,'''');',
  5121. ' ProcA(3,false);',
  5122. ' ProcB;',
  5123. ' ProcB();',
  5124. ' ProcB(4);',
  5125. ' ProcB(''foo'');',
  5126. ' ProcC;',
  5127. ' ProcC();',
  5128. ' ProcC(4);',
  5129. ' ProcC(5,''foo'');',
  5130. ' ProcB(GetIt);',
  5131. ' ProcB(GetIt());',
  5132. ' ProcB(GetIt,GetIt());']);
  5133. ConvertProgram;
  5134. CheckSource('TestProc_Varargs',
  5135. LinesToStr([ // statements
  5136. 'this.GetIt = function () {',
  5137. ' var Result = 0;',
  5138. ' return Result;',
  5139. '};',
  5140. '']),
  5141. LinesToStr([
  5142. 'ProcA(1);',
  5143. 'ProcA(1, 2);',
  5144. 'ProcA(1, 2.0);',
  5145. 'ProcA(1, 2, 3);',
  5146. 'ProcA(1, "2");',
  5147. 'ProcA(2, "");',
  5148. 'ProcA(3, false);',
  5149. 'ProcB();',
  5150. 'ProcB();',
  5151. 'ProcB(4);',
  5152. 'ProcB("foo");',
  5153. 'ProcC(17);',
  5154. 'ProcC(17);',
  5155. 'ProcC(4);',
  5156. 'ProcC(5, "foo");',
  5157. 'ProcB($mod.GetIt());',
  5158. 'ProcB($mod.GetIt());',
  5159. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5160. '']));
  5161. end;
  5162. procedure TTestModule.TestProc_ConstOrder;
  5163. begin
  5164. StartProgram(false);
  5165. Add([
  5166. 'const A = 3;',
  5167. 'const B = A+1;',
  5168. 'procedure DoIt;',
  5169. 'const C = A+1;',
  5170. 'const D = B+1;',
  5171. 'const E = D+C+B+A;',
  5172. 'begin',
  5173. 'end;',
  5174. 'begin'
  5175. ]);
  5176. ConvertProgram;
  5177. CheckSource('TestProc_ConstOrder',
  5178. LinesToStr([ // statements
  5179. 'this.A = 3;',
  5180. 'this.B = 3 + 1;',
  5181. 'var C = 3 + 1;',
  5182. 'var D = 4 + 1;',
  5183. 'var E = 5 + 4 + 4 + 3;',
  5184. 'this.DoIt = function () {',
  5185. '};',
  5186. '']),
  5187. LinesToStr([
  5188. ''
  5189. ]));
  5190. end;
  5191. procedure TTestModule.TestProc_DuplicateConst;
  5192. begin
  5193. StartProgram(false);
  5194. Add([
  5195. 'const A = 1;',
  5196. 'procedure DoIt;',
  5197. 'const A = 2;',
  5198. ' procedure SubIt;',
  5199. ' const A = 21;',
  5200. ' begin',
  5201. ' end;',
  5202. 'begin',
  5203. 'end;',
  5204. 'procedure DoSome;',
  5205. 'const A = 3;',
  5206. 'begin',
  5207. 'end;',
  5208. 'begin'
  5209. ]);
  5210. ConvertProgram;
  5211. CheckSource('TestProc_DuplicateConst',
  5212. LinesToStr([ // statements
  5213. 'this.A = 1;',
  5214. 'var A$1 = 2;',
  5215. 'var A$2 = 21;',
  5216. 'this.DoIt = function () {',
  5217. ' function SubIt() {',
  5218. ' };',
  5219. '};',
  5220. 'var A$3 = 3;',
  5221. 'this.DoSome = function () {',
  5222. '};',
  5223. '']),
  5224. LinesToStr([
  5225. ''
  5226. ]));
  5227. end;
  5228. procedure TTestModule.TestProc_LocalVarAbsolute;
  5229. begin
  5230. StartProgram(false);
  5231. Add([
  5232. 'type',
  5233. ' TObject = class',
  5234. ' Index: longint;',
  5235. ' procedure DoAbs(Item: pointer);',
  5236. ' end;',
  5237. 'procedure TObject.DoAbs(Item: pointer);',
  5238. 'var',
  5239. ' o: TObject absolute Item;',
  5240. 'begin',
  5241. ' if o.Index<o.Index then o.Index:=o.Index;',
  5242. 'end;',
  5243. 'procedure DoIt(i: longint; p: pointer);',
  5244. 'var',
  5245. ' d: double absolute i;',
  5246. ' s: string absolute d;',
  5247. ' oi: TObject absolute i;',
  5248. ' op: TObject absolute p;',
  5249. 'begin',
  5250. ' if d=d then d:=d;',
  5251. ' if s=s then s:=s;',
  5252. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5253. ' if op.Index=op.Index then op.Index:=op.Index;',
  5254. 'end;',
  5255. 'begin']);
  5256. ConvertProgram;
  5257. CheckSource('TestProc_LocalVarAbsolute',
  5258. LinesToStr([ // statements
  5259. 'rtl.createClass(this, "TObject", null, function () {',
  5260. ' this.$init = function () {',
  5261. ' this.Index = 0;',
  5262. ' };',
  5263. ' this.$final = function () {',
  5264. ' };',
  5265. ' this.DoAbs = function (Item) {',
  5266. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5267. ' };',
  5268. '});',
  5269. 'this.DoIt = function (i, p) {',
  5270. ' if (i === i) i = i;',
  5271. ' if (i === i) i = i;',
  5272. ' if (i.Index < i.Index) i.Index = i.Index;',
  5273. ' if (p.Index === p.Index) p.Index = p.Index;',
  5274. '};'
  5275. ]),
  5276. LinesToStr([
  5277. ]));
  5278. end;
  5279. procedure TTestModule.TestProc_ResultAbsolute;
  5280. begin
  5281. StartProgram(false);
  5282. Add([
  5283. 'type',
  5284. ' TObject = class',
  5285. ' Index: longint;',
  5286. ' function DoAbs: pointer;',
  5287. ' end;',
  5288. 'function TObject.DoAbs: pointer;',
  5289. 'var',
  5290. ' o: TObject absolute Result;',
  5291. 'begin',
  5292. ' if o.Index<o.Index then o.Index:=o.Index;',
  5293. 'end;',
  5294. 'function DoIt: jsvalue;',
  5295. 'var',
  5296. ' d: double absolute Result;',
  5297. ' s: string absolute Result;',
  5298. ' o: TObject absolute Result;',
  5299. 'begin',
  5300. ' if d=d then d:=d;',
  5301. ' if s=s then s:=s;',
  5302. ' if o.Index<o.Index then o.Index:=o.Index;',
  5303. 'end;',
  5304. 'begin']);
  5305. ConvertProgram;
  5306. CheckSource('TestProc_ResultAbsolute',
  5307. LinesToStr([ // statements
  5308. 'rtl.createClass(this, "TObject", null, function () {',
  5309. ' this.$init = function () {',
  5310. ' this.Index = 0;',
  5311. ' };',
  5312. ' this.$final = function () {',
  5313. ' };',
  5314. ' this.DoAbs = function () {',
  5315. ' var Result = null;',
  5316. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5317. ' return Result;',
  5318. ' };',
  5319. '});',
  5320. 'this.DoIt = function () {',
  5321. ' var Result = undefined;',
  5322. ' if (Result === Result) Result = Result;',
  5323. ' if (Result === Result) Result = Result;',
  5324. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5325. ' return Result;',
  5326. '};',
  5327. '']),
  5328. LinesToStr([
  5329. ]));
  5330. end;
  5331. procedure TTestModule.TestProc_LocalVarInit;
  5332. begin
  5333. StartProgram(false);
  5334. Add([
  5335. 'type TBytes = array of byte;',
  5336. 'procedure DoIt;',
  5337. 'const c = 4;',
  5338. 'var',
  5339. ' b: byte = 1;',
  5340. ' w: word = 2+c;',
  5341. ' p: pointer = nil;',
  5342. ' Buffer: TBytes = nil;',
  5343. 'begin',
  5344. 'end;',
  5345. 'begin']);
  5346. ConvertProgram;
  5347. CheckSource('TestProc_LocalVarInit',
  5348. LinesToStr([ // statements
  5349. 'var c = 4;',
  5350. 'this.DoIt = function () {',
  5351. ' var b = 1;',
  5352. ' var w = 2 + 4;',
  5353. ' var p = null;',
  5354. ' var Buffer = [];',
  5355. '};',
  5356. '']),
  5357. LinesToStr([
  5358. ]));
  5359. end;
  5360. procedure TTestModule.TestProc_ReservedWords;
  5361. begin
  5362. StartProgram(false);
  5363. Add([
  5364. 'procedure Date(ArrayBuffer: longint);',
  5365. 'const',
  5366. ' NaN: longint = 3;',
  5367. 'var',
  5368. ' &Boolean: longint;',
  5369. ' procedure Error(ArrayBuffer: longint);',
  5370. ' begin',
  5371. ' end;',
  5372. 'begin',
  5373. ' Nan:=&bOolean;',
  5374. 'end;',
  5375. 'begin',
  5376. ' Date(1);']);
  5377. ConvertProgram;
  5378. CheckSource('TestProc_ReservedWords',
  5379. LinesToStr([ // statements
  5380. 'var naN = 3;',
  5381. 'this.Date = function (arrayBuffer) {',
  5382. ' var boolean = 0;',
  5383. ' function error(arrayBuffer) {',
  5384. ' };',
  5385. ' naN = boolean;',
  5386. '};',
  5387. '']),
  5388. LinesToStr([
  5389. ' $mod.Date(1);'
  5390. ]));
  5391. end;
  5392. procedure TTestModule.TestProc_ConstRefWord;
  5393. begin
  5394. StartProgram(false);
  5395. Add([
  5396. 'procedure Run(constref w: word);',
  5397. 'var l: word;',
  5398. 'begin',
  5399. ' l:=w;',
  5400. ' Run(w);',
  5401. ' Run(l);',
  5402. 'end;',
  5403. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5404. 'begin',
  5405. ' Run(a);',
  5406. ' Run(b);',
  5407. ' Run(c);',
  5408. ' Run(d);',
  5409. ' Run(e);',
  5410. 'end;',
  5411. 'begin',
  5412. ' Run(1);']);
  5413. ConvertProgram;
  5414. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5415. CheckSource('TestProc_ConstRefWord',
  5416. LinesToStr([ // statements
  5417. 'this.Run = function (w) {',
  5418. ' var l = 0;',
  5419. ' l = w;',
  5420. ' $mod.Run(w);',
  5421. ' $mod.Run(l);',
  5422. '};',
  5423. 'this.Fly = function (a, b, c, d, e) {',
  5424. ' $mod.Run(a);',
  5425. ' $mod.Run(b.get());',
  5426. ' $mod.Run(c.get());',
  5427. ' $mod.Run(d);',
  5428. ' $mod.Run(e);',
  5429. '};',
  5430. '']),
  5431. LinesToStr([
  5432. '$mod.Run(1);'
  5433. ]));
  5434. end;
  5435. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5436. begin
  5437. StartProgram(false);
  5438. Add([
  5439. '{$mode objfpc}',
  5440. 'type',
  5441. ' TFunc = reference to function(x: word): word;',
  5442. 'var Func: TFunc;',
  5443. 'procedure DoIt(a: word);',
  5444. 'begin',
  5445. ' Func:=function(b:word): word',
  5446. ' begin',
  5447. ' Result:=a+b;',
  5448. ' exit(b);',
  5449. ' exit(Result);',
  5450. ' end;',// test semicolon
  5451. ' a:=3;',
  5452. 'end;',
  5453. 'begin',
  5454. ' Func:=function(c:word):word begin',
  5455. ' Result:=3+c;',
  5456. ' exit(c);',
  5457. ' exit(Result);',
  5458. ' end;']);
  5459. ConvertProgram;
  5460. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5461. LinesToStr([ // statements
  5462. 'this.Func = null;',
  5463. 'this.DoIt = function (a) {',
  5464. ' $mod.Func = function (b) {',
  5465. ' var Result = 0;',
  5466. ' Result = a + b;',
  5467. ' return b;',
  5468. ' return Result;',
  5469. ' return Result;',
  5470. ' };',
  5471. ' a = 3;',
  5472. '};',
  5473. '']),
  5474. LinesToStr([
  5475. '$mod.Func = function (c) {',
  5476. ' var Result = 0;',
  5477. ' Result = 3 + c;',
  5478. ' return c;',
  5479. ' return Result;',
  5480. ' return Result;',
  5481. '};',
  5482. '']));
  5483. end;
  5484. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5485. begin
  5486. StartProgram(false);
  5487. Add([
  5488. '{$mode delphi}',
  5489. 'type',
  5490. ' TProc = reference to procedure(x: word);',
  5491. 'procedure DoIt(a: word);',
  5492. 'var Proc: TProc;',
  5493. 'begin',
  5494. ' Proc:=procedure(b:word) begin end;',
  5495. 'end;',
  5496. 'var Proc: TProc;',
  5497. 'begin',
  5498. ' Proc:=procedure(c:word) begin end;',
  5499. '']);
  5500. ConvertProgram;
  5501. CheckSource('TestAnonymousProc_Assign_Delphi',
  5502. LinesToStr([ // statements
  5503. 'this.DoIt = function (a) {',
  5504. ' var Proc = null;',
  5505. ' Proc = function (b) {',
  5506. ' };',
  5507. '};',
  5508. 'this.Proc = null;',
  5509. '']),
  5510. LinesToStr([
  5511. '$mod.Proc = function (c) {',
  5512. '};',
  5513. '']));
  5514. end;
  5515. procedure TTestModule.TestAnonymousProc_Arg;
  5516. begin
  5517. StartProgram(false);
  5518. Add([
  5519. 'type',
  5520. ' TProc = reference to procedure;',
  5521. ' TFunc = reference to function(x: word): word;',
  5522. 'procedure DoMore(f,g: TProc);',
  5523. 'begin',
  5524. 'end;',
  5525. 'procedure DoOdd(v: jsvalue);',
  5526. 'begin',
  5527. 'end;',
  5528. 'procedure DoIt(f: TFunc);',
  5529. 'begin',
  5530. ' DoIt(function(b:word): word',
  5531. ' begin',
  5532. ' Result:=1+b;',
  5533. ' end);',
  5534. ' DoMore(procedure begin end, procedure begin end);',
  5535. ' DoOdd(procedure begin end);',
  5536. 'end;',
  5537. 'begin',
  5538. ' DoMore(procedure begin end,',
  5539. ' procedure assembler asm',
  5540. ' console.log("c");',
  5541. ' end);',
  5542. '']);
  5543. ConvertProgram;
  5544. CheckSource('TestAnonymousProc_Arg',
  5545. LinesToStr([ // statements
  5546. 'this.DoMore = function (f, g) {',
  5547. '};',
  5548. 'this.DoOdd = function (v) {',
  5549. '};',
  5550. 'this.DoIt = function (f) {',
  5551. ' $mod.DoIt(function (b) {',
  5552. ' var Result = 0;',
  5553. ' Result = 1 + b;',
  5554. ' return Result;',
  5555. ' });',
  5556. ' $mod.DoMore(function () {',
  5557. ' }, function () {',
  5558. ' });',
  5559. ' $mod.DoOdd(function () {',
  5560. ' });',
  5561. '};',
  5562. '']),
  5563. LinesToStr([
  5564. '$mod.DoMore(function () {',
  5565. '}, function () {',
  5566. ' console.log("c");',
  5567. '});',
  5568. '']));
  5569. end;
  5570. procedure TTestModule.TestAnonymousProc_Typecast;
  5571. begin
  5572. StartProgram(false);
  5573. Add([
  5574. 'type',
  5575. ' TProc = reference to procedure(w: word);',
  5576. ' TArr = array of word;',
  5577. ' TFuncArr = reference to function: TArr;',
  5578. 'procedure DoIt(p: TProc);',
  5579. 'var',
  5580. ' w: word;',
  5581. ' a: TArr;',
  5582. 'begin',
  5583. ' p:=TProc(procedure(b: smallint) begin end);',
  5584. ' a:=TFuncArr(function: TArr begin end)();',
  5585. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5586. 'end;',
  5587. 'begin']);
  5588. ConvertProgram;
  5589. CheckSource('TestAnonymousProc_Typecast',
  5590. LinesToStr([ // statements
  5591. 'this.DoIt = function (p) {',
  5592. ' var w = 0;',
  5593. ' var a = [];',
  5594. ' p = function (b) {',
  5595. ' };',
  5596. ' a = function () {',
  5597. ' var Result = [];',
  5598. ' return Result;',
  5599. ' }();',
  5600. ' w = function () {',
  5601. ' var Result = [];',
  5602. ' return Result;',
  5603. ' }()[3];',
  5604. '};',
  5605. '']),
  5606. LinesToStr([
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestAnonymousProc_With;
  5610. begin
  5611. StartProgram(false);
  5612. Add([
  5613. 'type',
  5614. ' TProc = reference to procedure(w: word);',
  5615. ' TObject = class',
  5616. ' b: boolean;',
  5617. ' end;',
  5618. 'var',
  5619. ' p: TProc;',
  5620. ' bird: TObject;',
  5621. 'begin',
  5622. ' with bird do',
  5623. ' p:=procedure(w: word)',
  5624. ' begin',
  5625. ' b:=w>2;',
  5626. ' end;',
  5627. '']);
  5628. ConvertProgram;
  5629. CheckSource('TestAnonymousProc_With',
  5630. LinesToStr([ // statements
  5631. 'rtl.createClass(this, "TObject", null, function () {',
  5632. ' this.$init = function () {',
  5633. ' this.b = false;',
  5634. ' };',
  5635. ' this.$final = function () {',
  5636. ' };',
  5637. '});',
  5638. 'this.p = null;',
  5639. 'this.bird = null;',
  5640. '']),
  5641. LinesToStr([
  5642. 'var $with = $mod.bird;',
  5643. '$mod.p = function (w) {',
  5644. ' $with.b = w > 2;',
  5645. '};',
  5646. '']));
  5647. end;
  5648. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5649. begin
  5650. StartProgram(false);
  5651. Add([
  5652. 'type',
  5653. ' TProc = reference to procedure;',
  5654. ' TObject = class',
  5655. ' b: boolean;',
  5656. ' end;',
  5657. 'procedure DoIt;',
  5658. 'var',
  5659. ' p: TProc;',
  5660. 'begin',
  5661. ' try',
  5662. ' except',
  5663. ' on E: TObject do',
  5664. ' p:=procedure',
  5665. ' begin',
  5666. ' E.b:=true;',
  5667. ' end;',
  5668. ' end;',
  5669. 'end;',
  5670. 'begin']);
  5671. ConvertProgram;
  5672. CheckSource('TestAnonymousProc_ExceptOn',
  5673. LinesToStr([ // statements
  5674. 'rtl.createClass(this, "TObject", null, function () {',
  5675. ' this.$init = function () {',
  5676. ' this.b = false;',
  5677. ' };',
  5678. ' this.$final = function () {',
  5679. ' };',
  5680. '});',
  5681. 'this.DoIt = function () {',
  5682. ' var p = null;',
  5683. ' try {} catch ($e) {',
  5684. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5685. ' var E = $e;',
  5686. ' p = function () {',
  5687. ' E.b = true;',
  5688. ' };',
  5689. ' } else throw $e',
  5690. ' };',
  5691. '};',
  5692. '']),
  5693. LinesToStr([
  5694. '']));
  5695. end;
  5696. procedure TTestModule.TestAnonymousProc_Nested;
  5697. begin
  5698. StartProgram(false);
  5699. Add([
  5700. 'type',
  5701. ' TProc = reference to procedure;',
  5702. ' TObject = class',
  5703. ' i: byte;',
  5704. ' procedure DoIt;',
  5705. ' end;',
  5706. 'procedure TObject.DoIt;',
  5707. 'var',
  5708. ' p: TProc;',
  5709. ' procedure Sub;',
  5710. ' begin',
  5711. ' p:=procedure',
  5712. ' begin',
  5713. ' i:=3;',
  5714. ' Self.i:=4;',
  5715. ' p:=procedure',
  5716. ' procedure SubSub;',
  5717. ' begin',
  5718. ' i:=13;',
  5719. ' Self.i:=14;',
  5720. ' end;',
  5721. ' begin',
  5722. ' i:=13;',
  5723. ' Self.i:=14;',
  5724. ' end;',
  5725. ' end;',
  5726. ' end;',
  5727. 'begin',
  5728. 'end;',
  5729. 'begin']);
  5730. ConvertProgram;
  5731. CheckSource('TestAnonymousProc_Nested',
  5732. LinesToStr([ // statements
  5733. 'rtl.createClass(this, "TObject", null, function () {',
  5734. ' this.$init = function () {',
  5735. ' this.i = 0;',
  5736. ' };',
  5737. ' this.$final = function () {',
  5738. ' };',
  5739. ' this.DoIt = function () {',
  5740. ' var $Self = this;',
  5741. ' var p = null;',
  5742. ' function Sub() {',
  5743. ' p = function () {',
  5744. ' $Self.i = 3;',
  5745. ' $Self.i = 4;',
  5746. ' p = function () {',
  5747. ' function SubSub() {',
  5748. ' $Self.i = 13;',
  5749. ' $Self.i = 14;',
  5750. ' };',
  5751. ' $Self.i = 13;',
  5752. ' $Self.i = 14;',
  5753. ' };',
  5754. ' };',
  5755. ' };',
  5756. ' };',
  5757. '});',
  5758. '']),
  5759. LinesToStr([
  5760. '']));
  5761. end;
  5762. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5763. begin
  5764. StartProgram(false);
  5765. Add([
  5766. 'type',
  5767. ' TProc = reference to procedure;',
  5768. 'function DoIt: TProc;',
  5769. ' function Sub: TProc;',
  5770. ' begin',
  5771. ' Result:=procedure',
  5772. ' begin',
  5773. ' Sub:=procedure',
  5774. ' procedure SubSub;',
  5775. ' begin',
  5776. ' Result:=nil;',
  5777. ' Sub:=nil;',
  5778. ' DoIt:=nil;',
  5779. ' end;',
  5780. ' begin',
  5781. ' Result:=nil;',
  5782. ' Sub:=nil;',
  5783. ' DoIt:=nil;',
  5784. ' end;',
  5785. ' end;',
  5786. ' end;',
  5787. 'begin',
  5788. 'end;',
  5789. 'begin']);
  5790. ConvertProgram;
  5791. CheckSource('TestAnonymousProc_NestedAssignResult',
  5792. LinesToStr([ // statements
  5793. 'this.DoIt = function () {',
  5794. ' var Result = null;',
  5795. ' function Sub() {',
  5796. ' var Result$1 = null;',
  5797. ' Result$1 = function () {',
  5798. ' Result$1 = function () {',
  5799. ' function SubSub() {',
  5800. ' Result$1 = null;',
  5801. ' Result$1 = null;',
  5802. ' Result = null;',
  5803. ' };',
  5804. ' Result$1 = null;',
  5805. ' Result$1 = null;',
  5806. ' Result = null;',
  5807. ' };',
  5808. ' };',
  5809. ' return Result$1;',
  5810. ' };',
  5811. ' return Result;',
  5812. '};',
  5813. '']),
  5814. LinesToStr([
  5815. '']));
  5816. end;
  5817. procedure TTestModule.TestAnonymousProc_Class;
  5818. begin
  5819. StartProgram(false);
  5820. Add([
  5821. 'type',
  5822. ' TProc = reference to procedure;',
  5823. ' TEvent = procedure of object;',
  5824. ' TObject = class',
  5825. ' Size: word;',
  5826. ' function GetIt: TProc;',
  5827. ' procedure DoIt; virtual; abstract;',
  5828. ' end;',
  5829. 'function TObject.GetIt: TProc;',
  5830. 'begin',
  5831. ' Result:=procedure',
  5832. ' var p: TEvent;',
  5833. ' begin',
  5834. ' Size:=Size;',
  5835. ' Size:=Self.Size;',
  5836. ' p:=@DoIt;',
  5837. ' p:[email protected];',
  5838. ' end;',
  5839. 'end;',
  5840. 'begin']);
  5841. ConvertProgram;
  5842. CheckSource('TestAnonymousProc_Class',
  5843. LinesToStr([ // statements
  5844. 'rtl.createClass(this, "TObject", null, function () {',
  5845. ' this.$init = function () {',
  5846. ' this.Size = 0;',
  5847. ' };',
  5848. ' this.$final = function () {',
  5849. ' };',
  5850. ' this.GetIt = function () {',
  5851. ' var $Self = this;',
  5852. ' var Result = null;',
  5853. ' Result = function () {',
  5854. ' var p = null;',
  5855. ' $Self.Size = $Self.Size;',
  5856. ' $Self.Size = $Self.Size;',
  5857. ' p = rtl.createCallback($Self, "DoIt");',
  5858. ' p = rtl.createCallback($Self, "DoIt");',
  5859. ' };',
  5860. ' return Result;',
  5861. ' };',
  5862. '});',
  5863. '']),
  5864. LinesToStr([
  5865. '']));
  5866. end;
  5867. procedure TTestModule.TestAnonymousProc_ForLoop;
  5868. begin
  5869. StartProgram(false);
  5870. Add([
  5871. 'type TProc = reference to procedure;',
  5872. 'procedure Foo(p: TProc);',
  5873. 'begin',
  5874. 'end;',
  5875. 'procedure DoIt;',
  5876. 'var i: word;',
  5877. ' a: word;',
  5878. 'begin',
  5879. ' for i:=1 to 10 do begin',
  5880. ' Foo(procedure begin a:=3; end);',
  5881. ' end;',
  5882. 'end;',
  5883. 'begin',
  5884. ' DoIt;']);
  5885. ConvertProgram;
  5886. CheckSource('TestAnonymousProc_ForLoop',
  5887. LinesToStr([ // statements
  5888. 'this.Foo = function (p) {',
  5889. '};',
  5890. 'this.DoIt = function () {',
  5891. ' var i = 0;',
  5892. ' var a = 0;',
  5893. ' for (i = 1; i <= 10; i++) {',
  5894. ' $mod.Foo(function () {',
  5895. ' a = 3;',
  5896. ' });',
  5897. ' };',
  5898. '};',
  5899. '']),
  5900. LinesToStr([
  5901. '$mod.DoIt();'
  5902. ]));
  5903. end;
  5904. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5905. begin
  5906. StartProgram(false);
  5907. Add([
  5908. '{$mode delphi}',
  5909. 'type',
  5910. ' TProc = reference to procedure;',
  5911. ' TFunc = reference to function(x: word): word;',
  5912. 'procedure Run;',
  5913. 'asm',
  5914. 'end;',
  5915. 'procedure Walk(p: TProc; f: TFunc);',
  5916. 'begin',
  5917. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5918. 'end;',
  5919. 'begin',
  5920. ' Walk(procedure',
  5921. ' asm',
  5922. ' console.log("a");',
  5923. ' end,',
  5924. ' function(x: word): word asm',
  5925. ' console.log("c");',
  5926. ' end);',
  5927. '']);
  5928. ConvertProgram;
  5929. CheckSource('TestAnonymousProc_AsmDelphi',
  5930. LinesToStr([ // statements
  5931. 'this.Run = function () {',
  5932. '};',
  5933. 'this.Walk = function (p, f) {',
  5934. ' $mod.Walk(function () {',
  5935. ' }, function (b) {',
  5936. ' return 1+b;',
  5937. ' });',
  5938. '};',
  5939. '']),
  5940. LinesToStr([
  5941. '$mod.Walk(function () {',
  5942. ' console.log("a");',
  5943. '}, function (x) {',
  5944. ' console.log("c");',
  5945. '});',
  5946. '']));
  5947. end;
  5948. procedure TTestModule.TestEnum_Name;
  5949. begin
  5950. StartProgram(false);
  5951. Add('type TMyEnum = (Red, Green, Blue);');
  5952. Add('var e: TMyEnum;');
  5953. Add('var f: TMyEnum = Blue;');
  5954. Add('begin');
  5955. Add(' e:=green;');
  5956. Add(' e:=default(TMyEnum);');
  5957. ConvertProgram;
  5958. CheckSource('TestEnum_Name',
  5959. LinesToStr([ // statements
  5960. 'this.TMyEnum = {',
  5961. ' "0":"Red",',
  5962. ' Red:0,',
  5963. ' "1":"Green",',
  5964. ' Green:1,',
  5965. ' "2":"Blue",',
  5966. ' Blue:2',
  5967. ' };',
  5968. 'this.e = 0;',
  5969. 'this.f = this.TMyEnum.Blue;'
  5970. ]),
  5971. LinesToStr([
  5972. '$mod.e=$mod.TMyEnum.Green;',
  5973. '$mod.e=$mod.TMyEnum.Red;'
  5974. ]));
  5975. end;
  5976. procedure TTestModule.TestEnum_Number;
  5977. begin
  5978. Converter.Options:=Converter.Options+[coEnumNumbers];
  5979. StartProgram(false);
  5980. Add('type TMyEnum = (Red, Green);');
  5981. Add('var');
  5982. Add(' e: TMyEnum;');
  5983. Add(' f: TMyEnum = Green;');
  5984. Add(' i: longint;');
  5985. Add('begin');
  5986. Add(' e:=green;');
  5987. Add(' i:=longint(e);');
  5988. ConvertProgram;
  5989. CheckSource('TestEnumNumber',
  5990. LinesToStr([ // statements
  5991. 'this.TMyEnum = {',
  5992. ' "0":"Red",',
  5993. ' Red:0,',
  5994. ' "1":"Green",',
  5995. ' Green:1',
  5996. ' };',
  5997. 'this.e = 0;',
  5998. 'this.f = 1;',
  5999. 'this.i = 0;'
  6000. ]),
  6001. LinesToStr([
  6002. '$mod.e=1;',
  6003. '$mod.i=$mod.e;'
  6004. ]));
  6005. end;
  6006. procedure TTestModule.TestEnum_ConstFail;
  6007. begin
  6008. StartProgram(false);
  6009. Add([
  6010. 'type TMyEnum = (Red = 100, Green = 101);',
  6011. 'var',
  6012. ' e: TMyEnum;',
  6013. ' f: TMyEnum = Green;',
  6014. 'begin',
  6015. ' e:=green;']);
  6016. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  6017. ConvertProgram;
  6018. end;
  6019. procedure TTestModule.TestEnum_Functions;
  6020. begin
  6021. StartProgram(false);
  6022. Add([
  6023. 'type TMyEnum = (Red, Green);',
  6024. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6025. 'var',
  6026. ' v: longint;',
  6027. ' s: string;',
  6028. 'begin',
  6029. ' val(s,e,v);',
  6030. ' val(s,e,i);',
  6031. 'end;',
  6032. 'var',
  6033. ' e: TMyEnum;',
  6034. ' i: longint;',
  6035. ' s: string;',
  6036. ' b: boolean;',
  6037. 'begin',
  6038. ' i:=ord(red);',
  6039. ' i:=ord(green);',
  6040. ' i:=ord(e);',
  6041. ' i:=ord(b);',
  6042. ' e:=low(tmyenum);',
  6043. ' e:=low(e);',
  6044. ' b:=low(boolean);',
  6045. ' e:=high(tmyenum);',
  6046. ' e:=high(e);',
  6047. ' b:=high(boolean);',
  6048. ' e:=pred(green);',
  6049. ' e:=pred(e);',
  6050. ' b:=pred(b);',
  6051. ' e:=succ(red);',
  6052. ' e:=succ(e);',
  6053. ' b:=succ(b);',
  6054. ' e:=tmyenum(1);',
  6055. ' e:=tmyenum(i);',
  6056. ' s:=str(e);',
  6057. ' str(e,s);',
  6058. ' str(red,s);',
  6059. ' s:=str(e:3);',
  6060. ' writestr(s,e:3,red);',
  6061. ' val(s,e,i);',
  6062. ' i:=longint(e);']);
  6063. ConvertProgram;
  6064. CheckSource('TestEnum_Functions',
  6065. LinesToStr([ // statements
  6066. 'this.TMyEnum = {',
  6067. ' "0":"Red",',
  6068. ' Red:0,',
  6069. ' "1":"Green",',
  6070. ' Green:1',
  6071. ' };',
  6072. 'this.DoIt = function (e, i) {',
  6073. ' var v = 0;',
  6074. ' var s = "";',
  6075. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6076. ' v = w;',
  6077. ' }));',
  6078. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6079. '};',
  6080. 'this.e = 0;',
  6081. 'this.i = 0;',
  6082. 'this.s = "";',
  6083. 'this.b = false;',
  6084. '']),
  6085. LinesToStr([
  6086. '$mod.i=$mod.TMyEnum.Red;',
  6087. '$mod.i=$mod.TMyEnum.Green;',
  6088. '$mod.i=$mod.e;',
  6089. '$mod.i=$mod.b+0;',
  6090. '$mod.e=$mod.TMyEnum.Red;',
  6091. '$mod.e=$mod.TMyEnum.Red;',
  6092. '$mod.b=false;',
  6093. '$mod.e=$mod.TMyEnum.Green;',
  6094. '$mod.e=$mod.TMyEnum.Green;',
  6095. '$mod.b=true;',
  6096. '$mod.e=$mod.TMyEnum.Green-1;',
  6097. '$mod.e=$mod.e-1;',
  6098. '$mod.b=false;',
  6099. '$mod.e=$mod.TMyEnum.Red+1;',
  6100. '$mod.e=$mod.e+1;',
  6101. '$mod.b=true;',
  6102. '$mod.e=1;',
  6103. '$mod.e=$mod.i;',
  6104. '$mod.s = $mod.TMyEnum[$mod.e];',
  6105. '$mod.s = $mod.TMyEnum[$mod.e];',
  6106. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6107. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6108. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6109. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6110. ' $mod.i = v;',
  6111. '});',
  6112. '$mod.i=$mod.e;',
  6113. '']));
  6114. end;
  6115. procedure TTestModule.TestEnumRg_Functions;
  6116. begin
  6117. StartProgram(false);
  6118. Add([
  6119. 'type',
  6120. ' TEnum = (Red, Green, Blue);',
  6121. ' TEnumRg = Green..Blue;',
  6122. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6123. 'var',
  6124. ' v: longint;',
  6125. ' s: string;',
  6126. 'begin',
  6127. ' val(s,e,v);',
  6128. ' val(s,e,i);',
  6129. 'end;',
  6130. 'var',
  6131. ' e: TEnumRg;',
  6132. ' i: longint;',
  6133. ' s: string;',
  6134. 'begin',
  6135. ' i:=ord(green);',
  6136. ' i:=ord(e);',
  6137. ' e:=low(tenumrg);',
  6138. ' e:=low(e);',
  6139. ' e:=high(tenumrg);',
  6140. ' e:=high(e);',
  6141. ' e:=pred(blue);',
  6142. ' e:=pred(e);',
  6143. ' e:=succ(green);',
  6144. ' e:=succ(e);',
  6145. ' e:=tenumrg(1);',
  6146. ' e:=tenumrg(i);',
  6147. ' s:=str(e);',
  6148. ' str(e,s);',
  6149. ' str(red,s);',
  6150. ' s:=str(e:3);',
  6151. ' writestr(s,e:3,blue);',
  6152. ' val(s,e,i);',
  6153. ' i:=longint(e);']);
  6154. ConvertProgram;
  6155. CheckSource('TestEnumRg_Functions',
  6156. LinesToStr([ // statements
  6157. 'this.TEnum = {',
  6158. ' "0":"Red",',
  6159. ' Red:0,',
  6160. ' "1":"Green",',
  6161. ' Green:1,',
  6162. ' "2":"Blue",',
  6163. ' Blue:2',
  6164. ' };',
  6165. 'this.DoIt = function (e, i) {',
  6166. ' var v = 0;',
  6167. ' var s = "";',
  6168. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6169. ' v = w;',
  6170. ' }));',
  6171. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6172. '};',
  6173. 'this.e = this.TEnum.Green;',
  6174. 'this.i = 0;',
  6175. 'this.s = "";',
  6176. '']),
  6177. LinesToStr([
  6178. '$mod.i=$mod.TEnum.Green;',
  6179. '$mod.i=$mod.e;',
  6180. '$mod.e=$mod.TEnum.Green;',
  6181. '$mod.e=$mod.TEnum.Green;',
  6182. '$mod.e=$mod.TEnum.Blue;',
  6183. '$mod.e=$mod.TEnum.Blue;',
  6184. '$mod.e=$mod.TEnum.Blue-1;',
  6185. '$mod.e=$mod.e-1;',
  6186. '$mod.e=$mod.TEnum.Green+1;',
  6187. '$mod.e=$mod.e+1;',
  6188. '$mod.e=1;',
  6189. '$mod.e=$mod.i;',
  6190. '$mod.s = $mod.TEnum[$mod.e];',
  6191. '$mod.s = $mod.TEnum[$mod.e];',
  6192. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6193. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6194. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6195. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6196. ' $mod.i = v;',
  6197. '});',
  6198. '$mod.i=$mod.e;',
  6199. '']));
  6200. end;
  6201. procedure TTestModule.TestEnum_AsParams;
  6202. begin
  6203. StartProgram(false);
  6204. Add('type TEnum = (Red,Blue);');
  6205. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6206. Add('var vJ: TEnum;');
  6207. Add('begin');
  6208. Add(' vg:=vg;');
  6209. Add(' vj:=vh;');
  6210. Add(' vi:=vi;');
  6211. Add(' doit(vg,vg,vg);');
  6212. Add(' doit(vh,vh,vj);');
  6213. Add(' doit(vi,vi,vi);');
  6214. Add(' doit(vj,vj,vj);');
  6215. Add('end;');
  6216. Add('var i: TEnum;');
  6217. Add('begin');
  6218. Add(' doit(i,i,i);');
  6219. ConvertProgram;
  6220. CheckSource('TestEnum_AsParams',
  6221. LinesToStr([ // statements
  6222. 'this.TEnum = {',
  6223. ' "0": "Red",',
  6224. ' Red: 0,',
  6225. ' "1": "Blue",',
  6226. ' Blue: 1',
  6227. '};',
  6228. 'this.DoIt = function (vG,vH,vI) {',
  6229. ' var vJ = 0;',
  6230. ' vG = vG;',
  6231. ' vJ = vH;',
  6232. ' vI.set(vI.get());',
  6233. ' $mod.DoIt(vG, vG, {',
  6234. ' get: function () {',
  6235. ' return vG;',
  6236. ' },',
  6237. ' set: function (v) {',
  6238. ' vG = v;',
  6239. ' }',
  6240. ' });',
  6241. ' $mod.DoIt(vH, vH, {',
  6242. ' get: function () {',
  6243. ' return vJ;',
  6244. ' },',
  6245. ' set: function (v) {',
  6246. ' vJ = v;',
  6247. ' }',
  6248. ' });',
  6249. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6250. ' $mod.DoIt(vJ, vJ, {',
  6251. ' get: function () {',
  6252. ' return vJ;',
  6253. ' },',
  6254. ' set: function (v) {',
  6255. ' vJ = v;',
  6256. ' }',
  6257. ' });',
  6258. '};',
  6259. 'this.i = 0;'
  6260. ]),
  6261. LinesToStr([
  6262. '$mod.DoIt($mod.i,$mod.i,{',
  6263. ' p: $mod,',
  6264. ' get: function () {',
  6265. ' return this.p.i;',
  6266. ' },',
  6267. ' set: function (v) {',
  6268. ' this.p.i = v;',
  6269. ' }',
  6270. '});'
  6271. ]));
  6272. end;
  6273. procedure TTestModule.TestEnumRange_Array;
  6274. begin
  6275. StartProgram(false);
  6276. Add([
  6277. 'type',
  6278. ' TEnum = (Red, Green, Blue);',
  6279. ' TEnumRg = green..blue;',
  6280. ' TArr = array[TEnumRg] of byte;',
  6281. ' TArr2 = array[green..blue] of byte;',
  6282. 'var',
  6283. ' a: TArr;',
  6284. ' b: TArr = (3,4);',
  6285. ' c: TArr2 = (5,6);',
  6286. 'begin',
  6287. ' a[green] := b[blue];',
  6288. ' c[green] := c[blue];',
  6289. '']);
  6290. ConvertProgram;
  6291. CheckSource('TestEnumRange_Array',
  6292. LinesToStr([ // statements
  6293. 'this.TEnum = {',
  6294. ' "0": "Red",',
  6295. ' Red: 0,',
  6296. ' "1": "Green",',
  6297. ' Green: 1,',
  6298. ' "2": "Blue",',
  6299. ' Blue: 2',
  6300. '};',
  6301. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6302. 'this.b = [3, 4];',
  6303. 'this.c = [5, 6];',
  6304. '']),
  6305. LinesToStr([
  6306. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6307. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6308. '']));
  6309. end;
  6310. procedure TTestModule.TestEnum_ForIn;
  6311. begin
  6312. StartProgram(false);
  6313. Add([
  6314. 'type',
  6315. ' TEnum = (Red, Green, Blue);',
  6316. ' TEnumRg = green..blue;',
  6317. ' TArr = array[TEnum] of byte;',
  6318. ' TArrRg = array[TEnumRg] of byte;',
  6319. 'var',
  6320. ' e: TEnum;',
  6321. ' a1: TArr = (3,4,5);',
  6322. ' a2: TArrRg = (11,12);',
  6323. ' b: byte;',
  6324. 'begin',
  6325. ' for e in TEnum do ;',
  6326. ' for e in TEnumRg do ;',
  6327. ' for e in TArr do ;',
  6328. ' for e in TArrRg do ;',
  6329. ' for b in a1 do ;',
  6330. ' for b in a2 do ;',
  6331. '']);
  6332. ConvertProgram;
  6333. CheckSource('TestEnum_ForIn',
  6334. LinesToStr([ // statements
  6335. 'this.TEnum = {',
  6336. ' "0": "Red",',
  6337. ' Red: 0,',
  6338. ' "1": "Green",',
  6339. ' Green: 1,',
  6340. ' "2": "Blue",',
  6341. ' Blue: 2',
  6342. '};',
  6343. 'this.e = 0;',
  6344. 'this.a1 = [3, 4, 5];',
  6345. 'this.a2 = [11, 12];',
  6346. 'this.b = 0;',
  6347. '']),
  6348. LinesToStr([
  6349. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6350. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6351. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6352. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6353. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6354. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6355. '']));
  6356. end;
  6357. procedure TTestModule.TestEnum_ScopedNumber;
  6358. begin
  6359. Converter.Options:=Converter.Options+[coEnumNumbers];
  6360. StartProgram(false);
  6361. Add([
  6362. 'type',
  6363. ' TEnum = (Red, Green);',
  6364. 'var',
  6365. ' e: TEnum;',
  6366. 'begin',
  6367. ' e:=TEnum.Green;',
  6368. '']);
  6369. ConvertProgram;
  6370. CheckSource('TestEnum_ScopedNumber',
  6371. LinesToStr([ // statements
  6372. 'this.TEnum = {',
  6373. ' "0": "Red",',
  6374. ' Red: 0,',
  6375. ' "1": "Green",',
  6376. ' Green: 1',
  6377. '};',
  6378. 'this.e = 0;',
  6379. '']),
  6380. LinesToStr([
  6381. '$mod.e = 1;']));
  6382. end;
  6383. procedure TTestModule.TestEnum_InFunction;
  6384. begin
  6385. StartProgram(false);
  6386. Add([
  6387. 'const TEnum = 3;',
  6388. 'procedure DoIt;',
  6389. 'type',
  6390. ' TEnum = (Red, Green, Blue);',
  6391. ' procedure Sub;',
  6392. ' type',
  6393. ' TEnumSub = (Left, Right);',
  6394. ' var',
  6395. ' es: TEnumSub;',
  6396. ' begin',
  6397. ' es:=Left;',
  6398. ' end;',
  6399. 'var',
  6400. ' e, e2: TEnum;',
  6401. 'begin',
  6402. ' if e in [red,blue] then e2:=e;',
  6403. 'end;',
  6404. 'begin']);
  6405. ConvertProgram;
  6406. CheckSource('TestEnum_InFunction',
  6407. LinesToStr([ // statements
  6408. 'this.TEnum = 3;',
  6409. 'var TEnum$1 = {',
  6410. ' "0":"Red",',
  6411. ' Red:0,',
  6412. ' "1":"Green",',
  6413. ' Green:1,',
  6414. ' "2":"Blue",',
  6415. ' Blue:2',
  6416. ' };',
  6417. 'var TEnumSub = {',
  6418. ' "0": "Left",',
  6419. ' Left: 0,',
  6420. ' "1": "Right",',
  6421. ' Right: 1',
  6422. '};',
  6423. 'this.DoIt = function () {',
  6424. ' function Sub() {',
  6425. ' var es = 0;',
  6426. ' es = TEnumSub.Left;',
  6427. ' };',
  6428. ' var e = 0;',
  6429. ' var e2 = 0;',
  6430. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6431. '};',
  6432. '']),
  6433. LinesToStr([
  6434. '']));
  6435. end;
  6436. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6437. begin
  6438. StartUnit(true);
  6439. Add([
  6440. 'interface',
  6441. 'var color: (red, green);',
  6442. 'implementation',
  6443. 'initialization',
  6444. ' color:=green;',
  6445. '']);
  6446. ConvertUnit;
  6447. CheckSource('TestEnum_Name_Anonymous_Unit',
  6448. LinesToStr([
  6449. 'this.color$a = {',
  6450. ' "0": "red",',
  6451. ' red: 0,',
  6452. ' "1": "green",',
  6453. ' green: 1',
  6454. '};',
  6455. 'this.color = 0;',
  6456. '']),
  6457. LinesToStr([ // this.$init
  6458. '$mod.color = $mod.color$a.green;',
  6459. '']),
  6460. LinesToStr([ // implementation
  6461. '']) );
  6462. end;
  6463. procedure TTestModule.TestSet_Enum;
  6464. begin
  6465. StartProgram(false);
  6466. Add([
  6467. 'type',
  6468. ' TColor = (Red, Green, Blue);',
  6469. ' TColors = set of TColor;',
  6470. 'var',
  6471. ' c: TColor;',
  6472. ' s: TColors;',
  6473. ' t: TColors = [];',
  6474. ' u: TColors = [Red];',
  6475. 'begin',
  6476. ' s:=[];',
  6477. ' s:=[Green];',
  6478. ' s:=[Green,Blue];',
  6479. ' s:=[Red..Blue];',
  6480. ' s:=[Red,Green..Blue];',
  6481. ' s:=[Red,c];',
  6482. ' s:=t;',
  6483. ' s:=default(TColors);',
  6484. '']);
  6485. ConvertProgram;
  6486. CheckSource('TestSet',
  6487. LinesToStr([ // statements
  6488. 'this.TColor = {',
  6489. ' "0":"Red",',
  6490. ' Red:0,',
  6491. ' "1":"Green",',
  6492. ' Green:1,',
  6493. ' "2":"Blue",',
  6494. ' Blue:2',
  6495. ' };',
  6496. 'this.c = 0;',
  6497. 'this.s = {};',
  6498. 'this.t = {};',
  6499. 'this.u = rtl.createSet(this.TColor.Red);'
  6500. ]),
  6501. LinesToStr([
  6502. '$mod.s={};',
  6503. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6504. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6505. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6506. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6507. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6508. '$mod.s=rtl.refSet($mod.t);',
  6509. '$mod.s={};',
  6510. '']));
  6511. end;
  6512. procedure TTestModule.TestSet_Operators;
  6513. begin
  6514. StartProgram(false);
  6515. Add('type');
  6516. Add(' TColor = (Red, Green, Blue);');
  6517. Add(' TColors = set of tcolor;');
  6518. Add('var');
  6519. Add(' vC: TColor;');
  6520. Add(' vS: TColors;');
  6521. Add(' vT: TColors;');
  6522. Add(' vU: TColors;');
  6523. Add(' B: boolean;');
  6524. Add('begin');
  6525. Add(' include(vs,green);');
  6526. Add(' exclude(vs,vc);');
  6527. Add(' vs:=vt+vu;');
  6528. Add(' vs:=vt+[red];');
  6529. Add(' vs:=[red]+vt;');
  6530. Add(' vs:=[red]+[green];');
  6531. Add(' vs:=vt-vu;');
  6532. Add(' vs:=vt-[red];');
  6533. Add(' vs:=[red]-vt;');
  6534. Add(' vs:=[red]-[green];');
  6535. Add(' vs:=vt*vu;');
  6536. Add(' vs:=vt*[red];');
  6537. Add(' vs:=[red]*vt;');
  6538. Add(' vs:=[red]*[green];');
  6539. Add(' vs:=vt><vu;');
  6540. Add(' vs:=vt><[red];');
  6541. Add(' vs:=[red]><vt;');
  6542. Add(' vs:=[red]><[green];');
  6543. Add(' b:=vt=vu;');
  6544. Add(' b:=vt=[red];');
  6545. Add(' b:=[red]=vt;');
  6546. Add(' b:=[red]=[green];');
  6547. Add(' b:=vt<>vu;');
  6548. Add(' b:=vt<>[red];');
  6549. Add(' b:=[red]<>vt;');
  6550. Add(' b:=[red]<>[green];');
  6551. Add(' b:=vt<=vu;');
  6552. Add(' b:=vt<=[red];');
  6553. Add(' b:=[red]<=vt;');
  6554. Add(' b:=[red]<=[green];');
  6555. Add(' b:=vt>=vu;');
  6556. Add(' b:=vt>=[red];');
  6557. Add(' b:=[red]>=vt;');
  6558. Add(' b:=[red]>=[green];');
  6559. ConvertProgram;
  6560. CheckSource('TestSet_Operators',
  6561. LinesToStr([ // statements
  6562. 'this.TColor = {',
  6563. ' "0":"Red",',
  6564. ' Red:0,',
  6565. ' "1":"Green",',
  6566. ' Green:1,',
  6567. ' "2":"Blue",',
  6568. ' Blue:2',
  6569. ' };',
  6570. 'this.vC = 0;',
  6571. 'this.vS = {};',
  6572. 'this.vT = {};',
  6573. 'this.vU = {};',
  6574. 'this.B = false;'
  6575. ]),
  6576. LinesToStr([
  6577. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6578. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6579. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6580. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6581. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6582. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6583. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6584. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6585. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6586. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6587. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6588. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6589. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6590. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6591. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6592. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6593. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6594. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6595. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6596. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6597. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6598. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6599. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6600. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6601. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6602. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6603. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6604. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6605. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6606. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6607. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6608. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6609. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6610. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6611. '']));
  6612. end;
  6613. procedure TTestModule.TestSet_Operator_In;
  6614. begin
  6615. StartProgram(false);
  6616. Add([
  6617. 'type',
  6618. ' TColor = (Red, Green, Blue);',
  6619. ' TColors = set of tcolor;',
  6620. ' TColorRg = green..blue;',
  6621. 'var',
  6622. ' vC: tcolor;',
  6623. ' vT: tcolors;',
  6624. ' B: boolean;',
  6625. ' rg: TColorRg;',
  6626. 'begin',
  6627. ' b:=red in vt;',
  6628. ' b:=vc in vt;',
  6629. ' b:=green in [red..blue];',
  6630. ' b:=vc in [red..blue];',
  6631. ' ',
  6632. ' if red in vt then ;',
  6633. ' while vC in vt do ;',
  6634. ' repeat',
  6635. ' until vC in vt;',
  6636. ' if rg in [green..blue] then ;',
  6637. '']);
  6638. ConvertProgram;
  6639. CheckSource('TestSet_Operator_In',
  6640. LinesToStr([ // statements
  6641. 'this.TColor = {',
  6642. ' "0":"Red",',
  6643. ' Red:0,',
  6644. ' "1":"Green",',
  6645. ' Green:1,',
  6646. ' "2":"Blue",',
  6647. ' Blue:2',
  6648. ' };',
  6649. 'this.vC = 0;',
  6650. 'this.vT = {};',
  6651. 'this.B = false;',
  6652. 'this.rg = this.TColor.Green;',
  6653. '']),
  6654. LinesToStr([
  6655. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6656. '$mod.B = $mod.vC in $mod.vT;',
  6657. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6658. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6659. 'if ($mod.TColor.Red in $mod.vT) ;',
  6660. 'while ($mod.vC in $mod.vT) {',
  6661. '};',
  6662. 'do {',
  6663. '} while (!($mod.vC in $mod.vT));',
  6664. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6665. '']));
  6666. end;
  6667. procedure TTestModule.TestSet_Functions;
  6668. begin
  6669. StartProgram(false);
  6670. Add('type');
  6671. Add(' TMyEnum = (Red, Green);');
  6672. Add(' TMyEnums = set of TMyEnum;');
  6673. Add('var');
  6674. Add(' e: TMyEnum;');
  6675. Add(' s: TMyEnums;');
  6676. Add('begin');
  6677. Add(' e:=Low(TMyEnums);');
  6678. Add(' e:=Low(s);');
  6679. Add(' e:=High(TMyEnums);');
  6680. Add(' e:=High(s);');
  6681. ConvertProgram;
  6682. CheckSource('TestSetFunctions',
  6683. LinesToStr([ // statements
  6684. 'this.TMyEnum = {',
  6685. ' "0":"Red",',
  6686. ' Red:0,',
  6687. ' "1":"Green",',
  6688. ' Green:1',
  6689. ' };',
  6690. 'this.e = 0;',
  6691. 'this.s = {};'
  6692. ]),
  6693. LinesToStr([
  6694. '$mod.e=$mod.TMyEnum.Red;',
  6695. '$mod.e=$mod.TMyEnum.Red;',
  6696. '$mod.e=$mod.TMyEnum.Green;',
  6697. '$mod.e=$mod.TMyEnum.Green;',
  6698. '']));
  6699. end;
  6700. procedure TTestModule.TestSet_PassAsArgClone;
  6701. begin
  6702. StartProgram(false);
  6703. Add('type');
  6704. Add(' TMyEnum = (Red, Green);');
  6705. Add(' TMyEnums = set of TMyEnum;');
  6706. Add('procedure DoDefault(s: tmyenums); begin end;');
  6707. Add('procedure DoConst(const s: tmyenums); begin end;');
  6708. Add('var');
  6709. Add(' aSet: tmyenums;');
  6710. Add('begin');
  6711. Add(' dodefault(aset);');
  6712. Add(' doconst(aset);');
  6713. ConvertProgram;
  6714. CheckSource('TestSetFunctions',
  6715. LinesToStr([ // statements
  6716. 'this.TMyEnum = {',
  6717. ' "0":"Red",',
  6718. ' Red:0,',
  6719. ' "1":"Green",',
  6720. ' Green:1',
  6721. ' };',
  6722. 'this.DoDefault = function (s) {',
  6723. '};',
  6724. 'this.DoConst = function (s) {',
  6725. '};',
  6726. 'this.aSet = {};'
  6727. ]),
  6728. LinesToStr([
  6729. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6730. '$mod.DoConst($mod.aSet);',
  6731. '']));
  6732. end;
  6733. procedure TTestModule.TestSet_AsParams;
  6734. begin
  6735. StartProgram(false);
  6736. Add([
  6737. 'type TEnum = (Red,Blue);',
  6738. 'type TEnums = set of TEnum;',
  6739. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6740. 'var vJ: TEnums;',
  6741. 'begin',
  6742. ' Include(vg,red);',
  6743. ' Include(result,blue);',
  6744. ' vg:=vg;',
  6745. ' vj:=vh;',
  6746. ' vi:=vi;',
  6747. ' doit(vg,vg,vg);',
  6748. ' doit(vh,vh,vj);',
  6749. ' doit(vi,vi,vi);',
  6750. ' doit(vj,vj,vj);',
  6751. 'end;',
  6752. 'var i: TEnums;',
  6753. 'begin',
  6754. ' doit(i,i,i);']);
  6755. ConvertProgram;
  6756. CheckSource('TestSet_AsParams',
  6757. LinesToStr([ // statements
  6758. 'this.TEnum = {',
  6759. ' "0": "Red",',
  6760. ' Red: 0,',
  6761. ' "1": "Blue",',
  6762. ' Blue: 1',
  6763. '};',
  6764. 'this.DoIt = function (vG,vH,vI) {',
  6765. ' var Result = {};',
  6766. ' var vJ = {};',
  6767. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6768. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6769. ' vG = rtl.refSet(vG);',
  6770. ' vJ = rtl.refSet(vH);',
  6771. ' vI.set(rtl.refSet(vI.get()));',
  6772. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6773. ' get: function () {',
  6774. ' return vG;',
  6775. ' },',
  6776. ' set: function (v) {',
  6777. ' vG = v;',
  6778. ' }',
  6779. ' });',
  6780. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6781. ' get: function () {',
  6782. ' return vJ;',
  6783. ' },',
  6784. ' set: function (v) {',
  6785. ' vJ = v;',
  6786. ' }',
  6787. ' });',
  6788. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6789. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6790. ' get: function () {',
  6791. ' return vJ;',
  6792. ' },',
  6793. ' set: function (v) {',
  6794. ' vJ = v;',
  6795. ' }',
  6796. ' });',
  6797. ' return Result;',
  6798. '};',
  6799. 'this.i = {};'
  6800. ]),
  6801. LinesToStr([
  6802. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6803. ' p: $mod,',
  6804. ' get: function () {',
  6805. ' return this.p.i;',
  6806. ' },',
  6807. ' set: function (v) {',
  6808. ' this.p.i = v;',
  6809. ' }',
  6810. '});'
  6811. ]));
  6812. end;
  6813. procedure TTestModule.TestSet_Property;
  6814. begin
  6815. StartProgram(false);
  6816. Add('type');
  6817. Add(' TEnum = (Red,Blue);');
  6818. Add(' TEnums = set of TEnum;');
  6819. Add(' TObject = class');
  6820. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6821. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6822. Add(' property Colors: TEnums read GetColors write SetColors;');
  6823. Add(' end;');
  6824. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6825. Add('begin end;');
  6826. Add('var Obj: TObject;');
  6827. Add('begin');
  6828. Add(' Include(Obj.Colors,Red);');
  6829. Add(' Exclude(Obj.Colors,Red);');
  6830. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6831. ConvertProgram;
  6832. CheckSource('TestSet_Property',
  6833. LinesToStr([ // statements
  6834. 'this.TEnum = {',
  6835. ' "0": "Red",',
  6836. ' Red: 0,',
  6837. ' "1": "Blue",',
  6838. ' Blue: 1',
  6839. '};',
  6840. 'rtl.createClass(this, "TObject", null, function () {',
  6841. ' this.$init = function () {',
  6842. ' };',
  6843. ' this.$final = function () {',
  6844. ' };',
  6845. '});',
  6846. 'this.DoIt = function (i, j, k, l) {',
  6847. '};',
  6848. 'this.Obj = null;',
  6849. '']),
  6850. LinesToStr([
  6851. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6852. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6853. '']));
  6854. end;
  6855. procedure TTestModule.TestSet_EnumConst;
  6856. begin
  6857. StartProgram(false);
  6858. Add([
  6859. 'type',
  6860. ' TEnum = (Red,Blue);',
  6861. ' TEnums = set of TEnum;',
  6862. 'const',
  6863. ' Orange = red;',
  6864. 'var',
  6865. ' Enum: tenum;',
  6866. ' Enums: tenums;',
  6867. 'begin',
  6868. ' Include(enums,orange);',
  6869. ' Exclude(enums,orange);',
  6870. ' if orange in enums then;',
  6871. ' if orange in [orange,red] then;']);
  6872. ConvertProgram;
  6873. CheckSource('TestSet_EnumConst',
  6874. LinesToStr([ // statements
  6875. 'this.TEnum = {',
  6876. ' "0": "Red",',
  6877. ' Red: 0,',
  6878. ' "1": "Blue",',
  6879. ' Blue: 1',
  6880. '};',
  6881. 'this.Orange = this.TEnum.Red;',
  6882. 'this.Enum = 0;',
  6883. 'this.Enums = {};',
  6884. '']),
  6885. LinesToStr([
  6886. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6887. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6888. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6889. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6890. '']));
  6891. end;
  6892. procedure TTestModule.TestSet_IntConst;
  6893. begin
  6894. StartProgram(false);
  6895. Add([
  6896. 'type',
  6897. ' TEnums = set of Byte;',
  6898. 'const',
  6899. ' Orange = 0;',
  6900. 'var',
  6901. ' Enum: byte;',
  6902. ' Enums: tenums;',
  6903. 'begin',
  6904. ' Enum:=ord(1);',
  6905. ' Enums:=[];',
  6906. ' Enums:=[0];',
  6907. ' Enums:=[1..2];',
  6908. //' Include(enums,orange);',
  6909. //' Exclude(enums,orange);',
  6910. ' if orange in enums then;',
  6911. ' if orange in [orange,1] then;']);
  6912. ConvertProgram;
  6913. CheckSource('TestSet_IntConst',
  6914. LinesToStr([ // statements
  6915. 'this.Orange = 0;',
  6916. 'this.Enum = 0;',
  6917. 'this.Enums = {};',
  6918. '']),
  6919. LinesToStr([
  6920. '$mod.Enum = 1;',
  6921. '$mod.Enums = {};',
  6922. '$mod.Enums = rtl.createSet(0);',
  6923. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6924. 'if (0 in $mod.Enums) ;',
  6925. 'if (0 in rtl.createSet(0, 1)) ;',
  6926. '']));
  6927. end;
  6928. procedure TTestModule.TestSet_IntRange;
  6929. begin
  6930. StartProgram(false);
  6931. Add([
  6932. 'type',
  6933. ' TRange = 1..3;',
  6934. ' TEnums = set of TRange;',
  6935. 'const',
  6936. ' Orange = 2;',
  6937. 'var',
  6938. ' Enum: byte;',
  6939. ' Enums: TEnums;',
  6940. 'begin',
  6941. ' Enums:=[];',
  6942. ' Enums:=[1];',
  6943. ' Enums:=[2..3];',
  6944. ' Include(enums,orange);',
  6945. ' Exclude(enums,orange);',
  6946. ' if orange in enums then;',
  6947. ' if orange in [orange,1] then;']);
  6948. ConvertProgram;
  6949. CheckSource('TestSet_IntRange',
  6950. LinesToStr([ // statements
  6951. 'this.Orange = 2;',
  6952. 'this.Enum = 0;',
  6953. 'this.Enums = {};',
  6954. '']),
  6955. LinesToStr([
  6956. '$mod.Enums = {};',
  6957. '$mod.Enums = rtl.createSet(1);',
  6958. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6959. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6960. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6961. 'if (2 in $mod.Enums) ;',
  6962. 'if (2 in rtl.createSet(2, 1)) ;',
  6963. '']));
  6964. end;
  6965. procedure TTestModule.TestSet_AnonymousEnumType;
  6966. begin
  6967. StartProgram(false);
  6968. Add('type');
  6969. Add(' TFlags = set of (red, green);');
  6970. Add('const');
  6971. Add(' favorite = red;');
  6972. Add('var');
  6973. Add(' f: TFlags;');
  6974. Add(' i: longint;');
  6975. Add('begin');
  6976. Add(' Include(f,red);');
  6977. Add(' Include(f,favorite);');
  6978. Add(' i:=ord(red);');
  6979. Add(' i:=ord(favorite);');
  6980. Add(' i:=ord(low(TFlags));');
  6981. Add(' i:=ord(low(f));');
  6982. Add(' i:=ord(low(favorite));');
  6983. Add(' i:=ord(high(TFlags));');
  6984. Add(' i:=ord(high(f));');
  6985. Add(' i:=ord(high(favorite));');
  6986. Add(' f:=[green,favorite];');
  6987. ConvertProgram;
  6988. CheckSource('TestSet_AnonymousEnumType',
  6989. LinesToStr([ // statements
  6990. 'this.TFlags$a = {',
  6991. ' "0": "red",',
  6992. ' red: 0,',
  6993. ' "1": "green",',
  6994. ' green: 1',
  6995. '};',
  6996. 'this.favorite = this.TFlags$a.red;',
  6997. 'this.f = {};',
  6998. 'this.i = 0;',
  6999. '']),
  7000. LinesToStr([
  7001. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  7002. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  7003. '$mod.i = $mod.TFlags$a.red;',
  7004. '$mod.i = $mod.TFlags$a.red;',
  7005. '$mod.i = $mod.TFlags$a.red;',
  7006. '$mod.i = $mod.TFlags$a.red;',
  7007. '$mod.i = $mod.TFlags$a.red;',
  7008. '$mod.i = $mod.TFlags$a.green;',
  7009. '$mod.i = $mod.TFlags$a.green;',
  7010. '$mod.i = $mod.TFlags$a.green;',
  7011. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  7012. '']));
  7013. end;
  7014. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  7015. begin
  7016. exit;
  7017. StartProgram(false);
  7018. Add([
  7019. 'type',
  7020. ' TAtoZ = ''A''..''Z'';',
  7021. ' TSetOfAZ = set of TAtoZ;',
  7022. 'var',
  7023. ' c: char;',
  7024. ' a: TAtoZ;',
  7025. ' s: TSetOfAZ = [''P'',''A''];',
  7026. ' i: longint;',
  7027. 'begin',
  7028. ' Include(s,''S'');',
  7029. ' Include(s,c);',
  7030. ' Include(s,a);',
  7031. ' c:=low(TAtoZ);',
  7032. ' i:=ord(low(TAtoZ));',
  7033. ' a:=high(TAtoZ);',
  7034. ' a:=high(TSetOfAtoZ);',
  7035. ' s:=[a,c,''M''];',
  7036. '']);
  7037. ConvertProgram;
  7038. CheckSource('TestSet_AnonymousEnumTypeChar',
  7039. LinesToStr([ // statements
  7040. '']),
  7041. LinesToStr([
  7042. '']));
  7043. end;
  7044. procedure TTestModule.TestSet_ConstEnum;
  7045. begin
  7046. StartProgram(false);
  7047. Add([
  7048. 'type',
  7049. ' TEnum = (red,blue,green);',
  7050. ' TEnums = set of TEnum;',
  7051. 'const',
  7052. ' teAny = [low(TEnum)..high(TEnum)];',
  7053. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7054. 'var',
  7055. ' e: TEnum;',
  7056. ' s: TEnums;',
  7057. 'begin',
  7058. ' if blue in teAny then;',
  7059. ' if blue in teAny+[e] then;',
  7060. ' if blue in teAny+teRedBlue then;',
  7061. ' if e in [red,blue] then;',
  7062. ' s:=teAny;',
  7063. ' s:=teAny+[e];',
  7064. ' s:=[e]+teAny;',
  7065. ' s:=teAny+teRedBlue;',
  7066. ' s:=teAny+teRedBlue+[e];',
  7067. '']);
  7068. ConvertProgram;
  7069. CheckSource('TestSet_ConstEnum',
  7070. LinesToStr([ // statements
  7071. 'this.TEnum = {',
  7072. ' "0": "red",',
  7073. ' red: 0,',
  7074. ' "1": "blue",',
  7075. ' blue: 1,',
  7076. ' "2": "green",',
  7077. ' green: 2',
  7078. '};',
  7079. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7080. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7081. 'this.e = 0;',
  7082. 'this.s = {};',
  7083. '']),
  7084. LinesToStr([
  7085. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7086. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7087. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7088. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7089. '$mod.s = rtl.refSet($mod.teAny);',
  7090. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7091. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7092. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7093. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7094. '']));
  7095. end;
  7096. procedure TTestModule.TestSet_ConstChar;
  7097. begin
  7098. StartProgram(false);
  7099. Add([
  7100. 'const',
  7101. ' LowChars = [''a''..''z''];',
  7102. ' Chars = LowChars+[''A''..''Z''];',
  7103. ' sc = [''А'', ''Я''];',
  7104. 'var',
  7105. ' c: char;',
  7106. ' s: string;',
  7107. 'begin',
  7108. ' if c in lowchars then ;',
  7109. ' if ''a'' in lowchars then ;',
  7110. ' if s[1] in lowchars then ;',
  7111. ' if c in chars then ;',
  7112. ' if c in [''a''..''z'',''_''] then ;',
  7113. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7114. ' if ''Я'' in sc then ;',
  7115. ' if 3=ord('' '') then ;',
  7116. '']);
  7117. ConvertProgram;
  7118. CheckSource('TestSet_ConstChar',
  7119. LinesToStr([ // statements
  7120. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7121. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7122. 'this.sc = rtl.createSet(1040, 1071);',
  7123. 'this.c = "\x00";',
  7124. 'this.s = "";',
  7125. '']),
  7126. LinesToStr([
  7127. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7128. 'if (97 in $mod.LowChars) ;',
  7129. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7130. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7131. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7132. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7133. 'if (1071 in $mod.sc) ;',
  7134. 'if (3 === 32) ;',
  7135. '']));
  7136. end;
  7137. procedure TTestModule.TestSet_ConstInt;
  7138. begin
  7139. StartProgram(false);
  7140. Add([
  7141. 'const',
  7142. ' Months = [1..12];',
  7143. ' Mirror = [-12..-1]+Months;',
  7144. 'var',
  7145. ' i: smallint;',
  7146. 'begin',
  7147. ' if 3 in Months then;',
  7148. ' if i in Months+[i] then;',
  7149. ' if i in Months+Mirror then;',
  7150. ' if i in [4..6,8] then;',
  7151. '']);
  7152. ConvertProgram;
  7153. CheckSource('TestSet_ConstInt',
  7154. LinesToStr([ // statements
  7155. 'this.Months = rtl.createSet(null, 1, 12);',
  7156. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7157. 'this.i = 0;',
  7158. '']),
  7159. LinesToStr([
  7160. 'if (3 in $mod.Months) ;',
  7161. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7162. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7163. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7164. '']));
  7165. end;
  7166. procedure TTestModule.TestSet_InFunction;
  7167. begin
  7168. StartProgram(false);
  7169. Add([
  7170. 'const',
  7171. ' TEnum = 3;',
  7172. ' TSetOfEnum = 4;',
  7173. ' TSetOfAno = 5;',
  7174. 'procedure DoIt;',
  7175. 'type',
  7176. ' TEnum = (red, blue);',
  7177. ' TSetOfEnum = set of TEnum;',
  7178. ' TSetOfAno = set of (up,down);',
  7179. 'var',
  7180. ' e: TEnum;',
  7181. ' se: TSetOfEnum;',
  7182. ' sa: TSetOfAno;',
  7183. 'begin',
  7184. ' se:=[e];',
  7185. ' sa:=[up];',
  7186. 'end;',
  7187. 'begin',
  7188. '']);
  7189. ConvertProgram;
  7190. CheckSource('TestSet_InFunction',
  7191. LinesToStr([ // statements
  7192. 'this.TEnum = 3;',
  7193. 'this.TSetOfEnum = 4;',
  7194. 'this.TSetOfAno = 5;',
  7195. 'var TEnum$1 = {',
  7196. ' "0": "red",',
  7197. ' red: 0,',
  7198. ' "1": "blue",',
  7199. ' blue: 1',
  7200. '};',
  7201. 'var TSetOfAno$a = {',
  7202. ' "0": "up",',
  7203. ' up: 0,',
  7204. ' "1": "down",',
  7205. ' down: 1',
  7206. '};',
  7207. 'this.DoIt = function () {',
  7208. ' var e = 0;',
  7209. ' var se = {};',
  7210. ' var sa = {};',
  7211. ' se = rtl.createSet(e);',
  7212. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7213. '};',
  7214. '']),
  7215. LinesToStr([
  7216. '']));
  7217. end;
  7218. procedure TTestModule.TestSet_ForIn;
  7219. begin
  7220. StartProgram(false);
  7221. Add([
  7222. 'type',
  7223. ' TEnum = (Red, Green, Blue);',
  7224. ' TEnumRg = green..blue;',
  7225. ' TSetOfEnum = set of TEnum;',
  7226. ' TSetOfEnumRg = set of TEnumRg;',
  7227. 'var',
  7228. ' e, e2: TEnum;',
  7229. ' er: TEnum;',
  7230. ' s: TSetOfEnum;',
  7231. 'begin',
  7232. ' for e in TSetOfEnum do ;',
  7233. ' for e in TSetOfEnumRg do ;',
  7234. ' for e in [] do e2:=e;',
  7235. ' for e in [red..green] do e2:=e;',
  7236. ' for e in [green,blue] do e2:=e;',
  7237. ' for e in [red,blue] do e2:=e;',
  7238. ' for e in s do e2:=e;',
  7239. ' for er in TSetOfEnumRg do ;',
  7240. '']);
  7241. ConvertProgram;
  7242. CheckSource('TestSet_ForIn',
  7243. LinesToStr([ // statements
  7244. 'this.TEnum = {',
  7245. ' "0":"Red",',
  7246. ' Red:0,',
  7247. ' "1":"Green",',
  7248. ' Green:1,',
  7249. ' "2":"Blue",',
  7250. ' Blue:2',
  7251. ' };',
  7252. 'this.e = 0;',
  7253. 'this.e2 = 0;',
  7254. 'this.er = 0;',
  7255. 'this.s = {};',
  7256. '']),
  7257. LinesToStr([
  7258. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7259. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7260. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7261. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7262. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7263. 'for (var $l in $mod.s){',
  7264. ' $mod.e = +$l;',
  7265. ' $mod.e2 = $mod.e;',
  7266. '};',
  7267. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7268. '']));
  7269. end;
  7270. procedure TTestModule.TestNestBegin;
  7271. begin
  7272. StartProgram(false);
  7273. Add('begin');
  7274. Add(' begin');
  7275. Add(' begin');
  7276. Add(' end;');
  7277. Add(' begin');
  7278. Add(' if true then ;');
  7279. Add(' end;');
  7280. Add(' end;');
  7281. ConvertProgram;
  7282. CheckSource('TestNestBegin',
  7283. '',
  7284. 'if (true) ;');
  7285. end;
  7286. procedure TTestModule.TestUnitImplVars;
  7287. begin
  7288. StartUnit(false);
  7289. Add('interface');
  7290. Add('implementation');
  7291. Add('var');
  7292. Add(' V1:longint;');
  7293. Add(' V2:longint = 3;');
  7294. Add(' V3:string = ''abc'';');
  7295. ConvertUnit;
  7296. CheckSource('TestUnitImplVars',
  7297. LinesToStr([ // statements
  7298. 'var $impl = $mod.$impl;',
  7299. '']),
  7300. '', // this.$init
  7301. LinesToStr([ // implementation
  7302. '$impl.V1 = 0;',
  7303. '$impl.V2 = 3;',
  7304. '$impl.V3 = "abc";',
  7305. '']) );
  7306. end;
  7307. procedure TTestModule.TestUnitImplConsts;
  7308. begin
  7309. StartUnit(false);
  7310. Add('interface');
  7311. Add('implementation');
  7312. Add('const');
  7313. Add(' v1 = 3;');
  7314. Add(' v2:longint = 4;');
  7315. Add(' v3:string = ''abc'';');
  7316. ConvertUnit;
  7317. CheckSource('TestUnitImplConsts',
  7318. LinesToStr([ // statements
  7319. 'var $impl = $mod.$impl;',
  7320. '']),
  7321. '', // this.$init
  7322. LinesToStr([ // implementation
  7323. '$impl.v1 = 3;',
  7324. '$impl.v2 = 4;',
  7325. '$impl.v3 = "abc";',
  7326. '']) );
  7327. end;
  7328. procedure TTestModule.TestUnitImplRecord;
  7329. begin
  7330. StartUnit(false);
  7331. Add('interface');
  7332. Add('implementation');
  7333. Add('type');
  7334. Add(' TMyRecord = record');
  7335. Add(' i: longint;');
  7336. Add(' end;');
  7337. Add('var aRec: TMyRecord;');
  7338. Add('initialization');
  7339. Add(' arec.i:=3;');
  7340. ConvertUnit;
  7341. CheckSource('TestUnitImplRecord',
  7342. LinesToStr([ // statements
  7343. 'var $impl = $mod.$impl;',
  7344. '']),
  7345. // this.$init
  7346. '$impl.aRec.i = 3;',
  7347. LinesToStr([ // implementation
  7348. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7349. ' this.i = 0;',
  7350. ' this.$eq = function (b) {',
  7351. ' return this.i === b.i;',
  7352. ' };',
  7353. ' this.$assign = function (s) {',
  7354. ' this.i = s.i;',
  7355. ' return this;',
  7356. ' };',
  7357. '});',
  7358. '$impl.aRec = $impl.TMyRecord.$new();',
  7359. '']) );
  7360. end;
  7361. procedure TTestModule.TestRenameJSNameConflict;
  7362. begin
  7363. StartProgram(false);
  7364. Add('var apply: longint;');
  7365. Add('var bind: longint;');
  7366. Add('var call: longint;');
  7367. Add('begin');
  7368. ConvertProgram;
  7369. CheckSource('TestRenameJSNameConflict',
  7370. LinesToStr([ // statements
  7371. 'this.Apply = 0;',
  7372. 'this.Bind = 0;',
  7373. 'this.Call = 0;'
  7374. ]),
  7375. LinesToStr([ // this.$main
  7376. ''
  7377. ]));
  7378. end;
  7379. procedure TTestModule.TestLocalConst;
  7380. begin
  7381. StartProgram(false);
  7382. Add('procedure DoIt;');
  7383. Add('const');
  7384. Add(' cA: longint = 1;');
  7385. Add(' cB = 2;');
  7386. Add(' procedure Sub;');
  7387. Add(' const');
  7388. Add(' csA = 3;');
  7389. Add(' cB: double = 4;');
  7390. Add(' begin');
  7391. Add(' cb:=cb+csa;');
  7392. Add(' ca:=ca+csa+5;');
  7393. Add(' end;');
  7394. Add('begin');
  7395. Add(' ca:=ca+cb+6;');
  7396. Add('end;');
  7397. Add('begin');
  7398. ConvertProgram;
  7399. CheckSource('TestLocalConst',
  7400. LinesToStr([
  7401. 'var cA = 1;',
  7402. 'var cB = 2;',
  7403. 'var csA = 3;',
  7404. 'var cB$1 = 4;',
  7405. 'this.DoIt = function () {',
  7406. ' function Sub() {',
  7407. ' cB$1 = cB$1 + 3;',
  7408. ' cA = cA + 3 + 5;',
  7409. ' };',
  7410. ' cA = cA + 2 + 6;',
  7411. '};'
  7412. ]),
  7413. LinesToStr([
  7414. ]));
  7415. end;
  7416. procedure TTestModule.TestVarExternal;
  7417. begin
  7418. StartProgram(false);
  7419. Add('var');
  7420. Add(' NaN: double; external name ''Global.NaN'';');
  7421. Add(' d: double;');
  7422. Add('begin');
  7423. Add(' d:=NaN;');
  7424. ConvertProgram;
  7425. CheckSource('TestVarExternal',
  7426. LinesToStr([
  7427. 'this.d = 0.0;'
  7428. ]),
  7429. LinesToStr([
  7430. '$mod.d = Global.NaN;'
  7431. ]));
  7432. end;
  7433. procedure TTestModule.TestVarExternalOtherUnit;
  7434. begin
  7435. AddModuleWithIntfImplSrc('unit2.pas',
  7436. LinesToStr([
  7437. 'var NaN: double; external name ''Global.NaN'';',
  7438. 'var iV: longint;'
  7439. ]),
  7440. '');
  7441. StartUnit(true);
  7442. Add('interface');
  7443. Add('uses unit2;');
  7444. Add('implementation');
  7445. Add('var');
  7446. Add(' d: double;');
  7447. Add(' i: longint; external name ''$i'';');
  7448. Add('begin');
  7449. Add(' d:=nan;');
  7450. Add(' d:=uNit2.nan;');
  7451. Add(' d:=test1.d;');
  7452. Add(' i:=iv;');
  7453. Add(' i:=uNit2.iv;');
  7454. Add(' i:=test1.i;');
  7455. ConvertUnit;
  7456. CheckSource('TestVarExternalOtherUnit',
  7457. LinesToStr([
  7458. 'var $impl = $mod.$impl;',
  7459. '']),
  7460. LinesToStr([ // this.$init
  7461. '$impl.d = Global.NaN;',
  7462. '$impl.d = Global.NaN;',
  7463. '$impl.d = $impl.d;',
  7464. '$i = pas.unit2.iV;',
  7465. '$i = pas.unit2.iV;',
  7466. '$i = $i;',
  7467. '']),
  7468. LinesToStr([ // implementation
  7469. '$impl.d = 0.0;',
  7470. '']) );
  7471. end;
  7472. procedure TTestModule.TestVarAbsoluteFail;
  7473. begin
  7474. StartProgram(false);
  7475. Add([
  7476. 'var',
  7477. ' a: longint;',
  7478. ' b: longword absolute a;',
  7479. 'begin']);
  7480. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7481. ConvertProgram;
  7482. end;
  7483. procedure TTestModule.TestConstExternal;
  7484. begin
  7485. StartProgram(false);
  7486. Add([
  7487. 'const',
  7488. ' PI: double; external name ''Global.PI'';',
  7489. ' Tau = 2*pi;',
  7490. 'var d: double;',
  7491. 'begin',
  7492. ' d:=pi;',
  7493. ' d:=tau+pi;']);
  7494. ConvertProgram;
  7495. CheckSource('TestConstExternal',
  7496. LinesToStr([
  7497. 'this.Tau = 2*Global.PI;',
  7498. 'this.d = 0.0;'
  7499. ]),
  7500. LinesToStr([
  7501. '$mod.d = Global.PI;',
  7502. '$mod.d = $mod.Tau + Global.PI;'
  7503. ]));
  7504. end;
  7505. procedure TTestModule.TestDouble;
  7506. begin
  7507. StartProgram(false);
  7508. Add([
  7509. 'type',
  7510. ' TDateTime = double;',
  7511. 'const',
  7512. ' a = TDateTime(2.7);',
  7513. ' b = a + TDateTime(1.7);',
  7514. ' c = 0.9 + 0.1;',
  7515. ' f0_1 = 0.1;',
  7516. ' f0_3 = 0.3;',
  7517. ' fn0_1 = -0.1;',
  7518. ' fn0_3 = -0.3;',
  7519. ' fn0_003 = -0.003;',
  7520. ' fn0_123456789 = -0.123456789;',
  7521. ' fn300_0 = -300.0;',
  7522. ' fn123456_0 = -123456.0;',
  7523. ' fn1234567_8 = -1234567.8;',
  7524. ' fn12345678_9 = -12345678.9;',
  7525. ' f1_0En12 = 1E-12;',
  7526. ' fn1_0En12 = -1E-12;',
  7527. ' maxdouble = 1.7e+308;',
  7528. ' mindouble = -1.7e+308;',
  7529. ' MinSafeIntDouble = -$1fffffffffffff;',
  7530. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7531. ' MaxSafeIntDouble = $1fffffffffffff;',
  7532. ' DZeroResolution = 1E-12;',
  7533. ' Minus1 = -1E-12;',
  7534. ' EPS = 1E-9;',
  7535. ' DELTA = 0.001;',
  7536. ' Big = 129.789E+100;',
  7537. ' Test0_15 = 0.15;',
  7538. ' Test999 = 2.9999999999999;',
  7539. ' Test111999 = 211199999999999000.0;',
  7540. ' TestMinus111999 = -211199999999999000.0;',
  7541. ' Inf = 1.0 / 0.0;',
  7542. ' NegInf = -1.0 / 0.0;',
  7543. 'procedure Run(d: double); external name ''Run'';',
  7544. 'var',
  7545. ' d: double = b;',
  7546. 'begin',
  7547. ' d:=1.0;',
  7548. ' d:=1.0/3.0;',
  7549. ' d:=1.0/(3-2-1);',
  7550. ' d:=1/3;',
  7551. ' d:=5.0E-324;',
  7552. ' d:=1.7E308;',
  7553. ' d:=001.00E00;',
  7554. ' d:=002.00E001;',
  7555. ' d:=003.000E000;',
  7556. ' d:=-004.00E-00;',
  7557. ' d:=-005.00E-001;',
  7558. ' d:=10**3;',
  7559. ' d:=100*9**0.5;',
  7560. ' d:=10 mod 3;',
  7561. ' d:=10 div 3;',
  7562. ' d:=c;',
  7563. ' d:=f0_1;',
  7564. ' d:=f0_3;',
  7565. ' d:=fn0_1;',
  7566. ' d:=fn0_3;',
  7567. ' d:=fn0_003;',
  7568. ' d:=fn0_123456789;',
  7569. ' d:=fn300_0;',
  7570. ' d:=fn123456_0;',
  7571. ' d:=fn1234567_8;',
  7572. ' d:=fn12345678_9;',
  7573. ' d:=f1_0En12;',
  7574. ' d:=fn1_0En12;',
  7575. ' d:=maxdouble;',
  7576. ' d:=mindouble;',
  7577. ' d:=MinSafeIntDouble;',
  7578. ' d:=double(MinSafeIntDouble);',
  7579. ' d:=MinSafeIntDouble2;',
  7580. ' d:=double(MinSafeIntDouble2);',
  7581. ' d:=MaxSafeIntDouble;',
  7582. ' d:=default(double);',
  7583. ' Run(Inf);',
  7584. ' Run(NegInf);',
  7585. '']);
  7586. ConvertProgram;
  7587. CheckSource('TestDouble',
  7588. LinesToStr([
  7589. 'this.a = 2.7;',
  7590. 'this.b = 2.7 + 1.7;',
  7591. 'this.c = 0.9 + 0.1;',
  7592. 'this.f0_1 = 0.1;',
  7593. 'this.f0_3 = 0.3;',
  7594. 'this.fn0_1 = -0.1;',
  7595. 'this.fn0_3 = -0.3;',
  7596. 'this.fn0_003 = -0.003;',
  7597. 'this.fn0_123456789 = -0.123456789;',
  7598. 'this.fn300_0 = -300.0;',
  7599. 'this.fn123456_0 = -123456.0;',
  7600. 'this.fn1234567_8 = -1234567.8;',
  7601. 'this.fn12345678_9 = -12345678.9;',
  7602. 'this.f1_0En12 = 1E-12;',
  7603. 'this.fn1_0En12 = -1E-12;',
  7604. 'this.maxdouble = 1.7e+308;',
  7605. 'this.mindouble = -1.7e+308;',
  7606. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7607. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7608. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7609. 'this.DZeroResolution = 1E-12;',
  7610. 'this.Minus1 = -1E-12;',
  7611. 'this.EPS = 1E-9;',
  7612. 'this.DELTA = 0.001;',
  7613. 'this.Big = 129.789E+100;',
  7614. 'this.Test0_15 = 0.15;',
  7615. 'this.Test999 = 2.9999999999999;',
  7616. 'this.Test111999 = 211199999999999000.0;',
  7617. 'this.TestMinus111999 = -211199999999999000.0;',
  7618. 'this.Inf = 1.0 / 0.0;',
  7619. 'this.NegInf = -1.0 / 0.0;',
  7620. 'this.d = 4.4;',
  7621. '']),
  7622. LinesToStr([
  7623. '$mod.d = 1.0;',
  7624. '$mod.d = 1.0 / 3.0;',
  7625. '$mod.d = 1.0 / (3 - 2 - 1);',
  7626. '$mod.d = 1 / 3;',
  7627. '$mod.d = 5.0E-324;',
  7628. '$mod.d = 1.7E308;',
  7629. '$mod.d = 1.00E0;',
  7630. '$mod.d = 2.00E1;',
  7631. '$mod.d = 3.000E0;',
  7632. '$mod.d = -4.00E-0;',
  7633. '$mod.d = -5.00E-1;',
  7634. '$mod.d = 10 ** 3;',
  7635. '$mod.d = 100 * (9 ** 0.5);',
  7636. '$mod.d = 10 % 3;',
  7637. '$mod.d = rtl.trunc(10 / 3);',
  7638. '$mod.d = 1;',
  7639. '$mod.d = 0.1;',
  7640. '$mod.d = 0.3;',
  7641. '$mod.d = -0.1;',
  7642. '$mod.d = -0.3;',
  7643. '$mod.d = -3E-3;',
  7644. '$mod.d = -0.123456789;',
  7645. '$mod.d = -300;',
  7646. '$mod.d = -123456;',
  7647. '$mod.d = -1234567.8;',
  7648. '$mod.d = -1.23456789E7;',
  7649. '$mod.d = 1E-12;',
  7650. '$mod.d = -1E-12;',
  7651. '$mod.d = 1.7E308;',
  7652. '$mod.d = -1.7E308;',
  7653. '$mod.d = -9007199254740991;',
  7654. '$mod.d = -9007199254740991;',
  7655. '$mod.d = -9.007199254740992E15;',
  7656. '$mod.d = -9.007199254740992E15;',
  7657. '$mod.d = 9007199254740991;',
  7658. '$mod.d = 0.0;',
  7659. 'Run(1 / 0);',
  7660. 'Run(-1 / 0);',
  7661. '']));
  7662. end;
  7663. procedure TTestModule.TestDoubleSmall;
  7664. begin
  7665. StartProgram(false);
  7666. Add([
  7667. 'const',
  7668. ' a = 1e-1;',
  7669. ' b = 1e-2;',
  7670. ' c = 1e-3;',
  7671. ' d = 1e-4;',
  7672. ' e = 1e-5;',
  7673. ' f = 1e-6;',
  7674. ' g = 1e-7;',
  7675. ' h = -1e-1;',
  7676. ' i = -1e-2;',
  7677. 'procedure Fly(d: double);',
  7678. 'begin',
  7679. 'end;',
  7680. 'begin',
  7681. ' Fly(a);',
  7682. ' Fly(b);',
  7683. ' Fly(c);',
  7684. ' Fly(d);',
  7685. ' Fly(e);',
  7686. ' Fly(f);',
  7687. ' Fly(g);',
  7688. ' Fly(h);',
  7689. ' Fly(i);',
  7690. '']);
  7691. ConvertProgram;
  7692. CheckSource('TestDoubleSmall',
  7693. LinesToStr([
  7694. 'this.a = 1e-1;',
  7695. 'this.b = 1e-2;',
  7696. 'this.c = 1e-3;',
  7697. 'this.d = 1e-4;',
  7698. 'this.e = 1e-5;',
  7699. 'this.f = 1e-6;',
  7700. 'this.g = 1e-7;',
  7701. 'this.h = -1e-1;',
  7702. 'this.i = -1e-2;',
  7703. 'this.Fly = function (d) {',
  7704. '};',
  7705. '']),
  7706. LinesToStr([
  7707. '$mod.Fly(0.1);',
  7708. '$mod.Fly(0.01);',
  7709. '$mod.Fly(1E-3);',
  7710. '$mod.Fly(1E-4);',
  7711. '$mod.Fly(1E-5);',
  7712. '$mod.Fly(1E-6);',
  7713. '$mod.Fly(1E-7);',
  7714. '$mod.Fly(-0.1);',
  7715. '$mod.Fly(-0.01);',
  7716. '']));
  7717. end;
  7718. procedure TTestModule.TestInteger;
  7719. begin
  7720. StartProgram(false);
  7721. Add([
  7722. 'const',
  7723. ' MinInt = low(NativeInt);',
  7724. ' MaxInt = high(NativeInt);',
  7725. 'type',
  7726. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7727. 'const',
  7728. ' a = low(TMyInt)+High(TMyInt);',
  7729. 'var',
  7730. ' i: TMyInt;',
  7731. 'begin',
  7732. ' i:=-MinInt;',
  7733. ' i:=default(TMyInt);',
  7734. ' i:=low(i)+high(i);',
  7735. '']);
  7736. ConvertProgram;
  7737. CheckSource('TestIntegerRange',
  7738. LinesToStr([
  7739. 'this.MinInt = -9007199254740991;',
  7740. 'this.MaxInt = 9007199254740991;',
  7741. 'this.a = -9007199254740991 + 9007199254740991;',
  7742. 'this.i = 0;',
  7743. '']),
  7744. LinesToStr([
  7745. '$mod.i = - -9007199254740991;',
  7746. '$mod.i = -9007199254740991;',
  7747. '$mod.i = -9007199254740991 + 9007199254740991;',
  7748. '']));
  7749. end;
  7750. procedure TTestModule.TestIntegerRange;
  7751. begin
  7752. StartProgram(false);
  7753. Add([
  7754. 'const',
  7755. ' MinInt = -1;',
  7756. ' MaxInt = +1;',
  7757. 'type',
  7758. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7759. ' TInt2 = 1..3;',
  7760. 'const',
  7761. ' a = low(TMyInt)+High(TMyInt);',
  7762. ' b = low(TInt2)+High(TInt2);',
  7763. ' s1 = [1];',
  7764. ' s2 = [1,2];',
  7765. ' s3 = [1..3];',
  7766. ' s4 = [low(shortint)..high(shortint)];',
  7767. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7768. ' s6 = 1 in s2;',
  7769. 'var',
  7770. ' i: TMyInt;',
  7771. ' i2: TInt2;',
  7772. 'begin',
  7773. ' i:=i2;',
  7774. ' i:=default(TMyInt);',
  7775. ' if i=i2 then ;',
  7776. ' i:=ord(i2);',
  7777. '']);
  7778. ConvertProgram;
  7779. CheckSource('TestIntegerRange',
  7780. LinesToStr([
  7781. 'this.MinInt = -1;',
  7782. 'this.MaxInt = +1;',
  7783. 'this.a = -1 + 1;',
  7784. 'this.b = 1 + 3;',
  7785. 'this.s1 = rtl.createSet(1);',
  7786. 'this.s2 = rtl.createSet(1, 2);',
  7787. 'this.s3 = rtl.createSet(null, 1, 3);',
  7788. 'this.s4 = rtl.createSet(null, -128, 127);',
  7789. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7790. 'this.s6 = 1 in this.s2;',
  7791. 'this.i = 0;',
  7792. 'this.i2 = 0;',
  7793. '']),
  7794. LinesToStr([
  7795. '$mod.i = $mod.i2;',
  7796. '$mod.i = -1;',
  7797. 'if ($mod.i === $mod.i2) ;',
  7798. '$mod.i = $mod.i2;',
  7799. '']));
  7800. end;
  7801. procedure TTestModule.TestIntegerTypecasts;
  7802. begin
  7803. StartProgram(false);
  7804. Add([
  7805. 'var',
  7806. ' i: nativeint;',
  7807. ' b: byte;',
  7808. ' sh: shortint;',
  7809. ' w: word;',
  7810. ' sm: smallint;',
  7811. ' lw: longword;',
  7812. ' li: longint;',
  7813. 'begin',
  7814. ' b:=byte(i);',
  7815. ' sh:=shortint(i);',
  7816. ' w:=word(i);',
  7817. ' sm:=smallint(i);',
  7818. ' lw:=longword(i);',
  7819. ' li:=longint(i);',
  7820. '']);
  7821. ConvertProgram;
  7822. CheckSource('TestIntegerTypecasts',
  7823. LinesToStr([
  7824. 'this.i = 0;',
  7825. 'this.b = 0;',
  7826. 'this.sh = 0;',
  7827. 'this.w = 0;',
  7828. 'this.sm = 0;',
  7829. 'this.lw = 0;',
  7830. 'this.li = 0;',
  7831. '']),
  7832. LinesToStr([
  7833. '$mod.b = $mod.i & 255;',
  7834. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7835. '$mod.w = $mod.i & 65535;',
  7836. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7837. '$mod.lw = $mod.i >>> 0;',
  7838. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7839. '']));
  7840. end;
  7841. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7842. begin
  7843. StartProgram(false);
  7844. Add([
  7845. 'var',
  7846. ' i,j: nativeint;',
  7847. 'begin',
  7848. ' i:=i shr 0;',
  7849. ' i:=i shr 1;',
  7850. ' i:=i shr 3;',
  7851. ' i:=i shr 54;',
  7852. ' i:=j shr i;',
  7853. '']);
  7854. ConvertProgram;
  7855. CheckResolverUnexpectedHints;
  7856. CheckSource('TestInteger_BitwiseShrNativeInt',
  7857. LinesToStr([
  7858. 'this.i = 0;',
  7859. 'this.j = 0;',
  7860. '']),
  7861. LinesToStr([
  7862. '$mod.i = $mod.i;',
  7863. '$mod.i = Math.floor($mod.i / 2);',
  7864. '$mod.i = Math.floor($mod.i / 8);',
  7865. '$mod.i = 0;',
  7866. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7867. '']));
  7868. end;
  7869. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7870. begin
  7871. StartProgram(false);
  7872. Add([
  7873. 'var',
  7874. ' i: nativeint;',
  7875. 'begin',
  7876. ' i:=i shl 0;',
  7877. ' i:=i shl 54;',
  7878. ' i:=123456789012 shl 1;',
  7879. ' i:=i shl 1;',
  7880. '']);
  7881. ConvertProgram;
  7882. CheckResolverUnexpectedHints;
  7883. CheckSource('TestInteger_BitwiseShrNativeInt',
  7884. LinesToStr([
  7885. 'this.i = 0;',
  7886. '']),
  7887. LinesToStr([
  7888. '$mod.i = $mod.i;',
  7889. '$mod.i = 0;',
  7890. '$mod.i = 246913578024;',
  7891. '$mod.i = rtl.shl($mod.i, 1);',
  7892. '']));
  7893. end;
  7894. procedure TTestModule.TestInteger_SystemFunc;
  7895. begin
  7896. StartProgram(true);
  7897. Add([
  7898. 'var',
  7899. ' i: byte;',
  7900. ' s: string;',
  7901. 'begin',
  7902. ' system.inc(i);',
  7903. ' system.str(i,s);',
  7904. ' s:=system.str(i);',
  7905. ' i:=system.low(i);',
  7906. ' i:=system.high(i);',
  7907. ' i:=system.pred(i);',
  7908. ' i:=system.succ(i);',
  7909. ' i:=system.ord(i);',
  7910. '']);
  7911. ConvertProgram;
  7912. CheckResolverUnexpectedHints;
  7913. CheckSource('TestInteger_SystemFunc',
  7914. LinesToStr([
  7915. 'this.i = 0;',
  7916. 'this.s = "";',
  7917. '']),
  7918. LinesToStr([
  7919. '$mod.i += 1;',
  7920. '$mod.s = "" + $mod.i;',
  7921. '$mod.s = "" + $mod.i;',
  7922. '$mod.i = 0;',
  7923. '$mod.i = 255;',
  7924. '$mod.i = $mod.i - 1;',
  7925. '$mod.i = $mod.i + 1;',
  7926. '$mod.i = $mod.i;',
  7927. '']));
  7928. end;
  7929. procedure TTestModule.TestInteger_AssignOutsideConst;
  7930. begin
  7931. StartProgram(false);
  7932. Add([
  7933. 'const',
  7934. ' MinInt = low(longint);',
  7935. ' MaxInt = high(longint);',
  7936. 'type',
  7937. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7938. 'var',
  7939. ' i: TMyInt;',
  7940. ' aByte: byte;',
  7941. ' aShortInt: shortint;',
  7942. ' aWord: word;',
  7943. ' aSmallInt: smallint;',
  7944. ' aLongWord: longword;',
  7945. ' aLongInt: longint;',
  7946. ' aNativeInt: nativeint;',
  7947. ' aNativeUInt: nativeuint;',
  7948. 'begin',
  7949. ' aByte:=$FF;',
  7950. ' aByte:=$100;',
  7951. ' aByte:=-1;',
  7952. ' aByte:=-127;',
  7953. ' aByte:=-128;',
  7954. ' aByte:=-254;',
  7955. ' aByte:=-255;',
  7956. ' aByte:=-256;',
  7957. ' aShortInt:=127;',
  7958. ' aShortInt:=128;',
  7959. ' aShortInt:=-128;',
  7960. ' aShortInt:=-129;',
  7961. ' aWord:=$ffff;',
  7962. ' aWord:=$10000;',
  7963. ' aWord:=-1;',
  7964. ' aWord:=-$ffff;',
  7965. ' aWord:=-$10000;',
  7966. ' aWord:=-$10001;',
  7967. ' aSmallInt:=$7fff;',
  7968. ' aSmallInt:=$8000;',
  7969. ' aSmallInt:=-$8000;',
  7970. ' aSmallInt:=-$8001;',
  7971. ' aLongWord:=$ffffffff;',
  7972. ' aLongWord:=$100000000;',
  7973. ' aLongWord:=-1;',
  7974. ' aLongWord:=-$ffffffff;',
  7975. ' aNativeInt:=$1fffffffffffff;',
  7976. ' aNativeInt:=-$1fffffffffffff;',
  7977. ' aNativeUInt:=$1fffffffffffff;',
  7978. ' aNativeUInt:=-$1fffffffffffff;',
  7979. '']);
  7980. ConvertProgram;
  7981. CheckSource('TestInteger_AssignOutsideConst',
  7982. LinesToStr([
  7983. 'this.MinInt = -2147483648;',
  7984. 'this.MaxInt = 2147483647;',
  7985. 'this.i = 0;',
  7986. 'this.aByte = 0;',
  7987. 'this.aShortInt = 0;',
  7988. 'this.aWord = 0;',
  7989. 'this.aSmallInt = 0;',
  7990. 'this.aLongWord = 0;',
  7991. 'this.aLongInt = 0;',
  7992. 'this.aNativeInt = 0;',
  7993. 'this.aNativeUInt = 0;',
  7994. '']),
  7995. LinesToStr([
  7996. '$mod.aByte = 0xFF;',
  7997. '$mod.aByte = 0;',
  7998. '$mod.aByte = 255;',
  7999. '$mod.aByte = 129;',
  8000. '$mod.aByte = 128;',
  8001. '$mod.aByte = 2;',
  8002. '$mod.aByte = 1;',
  8003. '$mod.aByte = 0;',
  8004. '$mod.aShortInt = 127;',
  8005. '$mod.aShortInt = -128;',
  8006. '$mod.aShortInt = -128;',
  8007. '$mod.aShortInt = 127;',
  8008. '$mod.aWord = 0xffff;',
  8009. '$mod.aWord = 0;',
  8010. '$mod.aWord = 65535;',
  8011. '$mod.aWord = 1;',
  8012. '$mod.aWord = 0;',
  8013. '$mod.aWord = 65535;',
  8014. '$mod.aSmallInt = 0x7fff;',
  8015. '$mod.aSmallInt = -32768;',
  8016. '$mod.aSmallInt = -0x8000;',
  8017. '$mod.aSmallInt = 32767;',
  8018. '$mod.aLongWord = 0xffffffff;',
  8019. '$mod.aLongWord = 0;',
  8020. '$mod.aLongWord = 4294967295;',
  8021. '$mod.aLongWord = 1;',
  8022. '$mod.aNativeInt = 0x1fffffffffffff;',
  8023. '$mod.aNativeInt = -0x1fffffffffffff;',
  8024. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8025. '$mod.aNativeUInt = 1;',
  8026. '']));
  8027. end;
  8028. procedure TTestModule.TestCurrency;
  8029. begin
  8030. StartProgram(false);
  8031. Add([
  8032. 'type',
  8033. ' TCoin = currency;',
  8034. 'const',
  8035. ' a = TCoin(2.7);',
  8036. ' b = a + TCoin(1.7);',
  8037. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8038. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8039. 'var',
  8040. ' c: TCoin = b;',
  8041. ' i: nativeint;',
  8042. ' d: double;',
  8043. ' j: jsvalue;',
  8044. 'function DoIt(c: currency): currency; begin end;',
  8045. 'function GetIt(d: double): double; begin end;',
  8046. 'procedure Write(v: jsvalue); begin end;',
  8047. 'begin',
  8048. ' c:=1.0;',
  8049. ' c:=0.1;',
  8050. ' c:=1.0/3.0;',
  8051. ' c:=1/3;',
  8052. ' c:=a;',
  8053. ' d:=c;',
  8054. ' c:=d;',
  8055. ' c:=currency(c);',
  8056. ' c:=currency(d);',
  8057. ' d:=double(c);',
  8058. ' c:=i;',
  8059. ' c:=currency(i);',
  8060. //' i:=c;', not allowed
  8061. ' i:=nativeint(c);',
  8062. ' c:=c+a;',
  8063. ' c:=-c-a;',
  8064. ' c:=d+c;',
  8065. ' c:=c+d;',
  8066. ' c:=d-c;',
  8067. ' c:=c-d;',
  8068. ' c:=c*a;',
  8069. ' c:=a*c;',
  8070. ' c:=d*c;',
  8071. ' c:=c*d;',
  8072. ' c:=c/a;',
  8073. ' c:=a/c;',
  8074. ' c:=d/c;',
  8075. ' c:=c/d;',
  8076. ' c:=c**a;',
  8077. ' c:=a**c;',
  8078. ' c:=d**c;',
  8079. ' c:=c**d;',
  8080. ' if c=c then ;',
  8081. ' if c=a then ;',
  8082. ' if a=c then ;',
  8083. ' if d=c then ;',
  8084. ' if c=d then ;',
  8085. ' c:=DoIt(c);',
  8086. ' c:=DoIt(i);',
  8087. ' c:=DoIt(d);',
  8088. ' c:=GetIt(c);',
  8089. ' j:=c;',
  8090. ' Write(c);',
  8091. ' c:=default(currency);',
  8092. ' j:=str(c);',
  8093. ' j:=str(c:0:3);',
  8094. '']);
  8095. ConvertProgram;
  8096. CheckSource('TestCurrency',
  8097. LinesToStr([
  8098. 'this.a = 27000;',
  8099. 'this.b = this.a + 17000;',
  8100. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8101. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8102. 'this.c = this.b;',
  8103. 'this.i = 0;',
  8104. 'this.d = 0.0;',
  8105. 'this.j = undefined;',
  8106. 'this.DoIt = function (c) {',
  8107. ' var Result = 0;',
  8108. ' return Result;',
  8109. '};',
  8110. 'this.GetIt = function (d) {',
  8111. ' var Result = 0.0;',
  8112. ' return Result;',
  8113. '};',
  8114. 'this.Write = function (v) {',
  8115. '};',
  8116. '']),
  8117. LinesToStr([
  8118. '$mod.c = 10000;',
  8119. '$mod.c = 1000;',
  8120. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8121. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8122. '$mod.c = $mod.a;',
  8123. '$mod.d = $mod.c / 10000;',
  8124. '$mod.c = rtl.trunc($mod.d * 10000);',
  8125. '$mod.c = $mod.c;',
  8126. '$mod.c = $mod.d * 10000;',
  8127. '$mod.d = $mod.c / 10000;',
  8128. '$mod.c = $mod.i * 10000;',
  8129. '$mod.c = $mod.i * 10000;',
  8130. '$mod.i = rtl.trunc($mod.c / 10000);',
  8131. '$mod.c = $mod.c + $mod.a;',
  8132. '$mod.c = -$mod.c - $mod.a;',
  8133. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8134. '$mod.c = $mod.c + ($mod.d * 10000);',
  8135. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8136. '$mod.c = $mod.c - ($mod.d * 10000);',
  8137. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8138. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8139. '$mod.c = $mod.d * $mod.c;',
  8140. '$mod.c = $mod.c * $mod.d;',
  8141. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8142. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8143. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8144. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8145. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8146. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8147. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8148. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8149. 'if ($mod.c === $mod.c) ;',
  8150. 'if ($mod.c === $mod.a) ;',
  8151. 'if ($mod.a === $mod.c) ;',
  8152. 'if (($mod.d * 10000) === $mod.c) ;',
  8153. 'if ($mod.c === ($mod.d * 10000)) ;',
  8154. '$mod.c = $mod.DoIt($mod.c);',
  8155. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8156. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8157. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8158. '$mod.j = $mod.c / 10000;',
  8159. '$mod.Write($mod.c / 10000);',
  8160. '$mod.c = 0;',
  8161. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8162. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8163. '']));
  8164. end;
  8165. procedure TTestModule.TestForBoolDo;
  8166. begin
  8167. StartProgram(false);
  8168. Add([
  8169. 'var b: boolean;',
  8170. 'begin',
  8171. ' for b:=false to true do ;',
  8172. ' for b:=b downto false do ;',
  8173. ' for b in boolean do ;',
  8174. '']);
  8175. ConvertProgram;
  8176. CheckSource('TestForBoolDo',
  8177. LinesToStr([ // statements
  8178. 'this.b = false;']),
  8179. LinesToStr([ // this.$main
  8180. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8181. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8182. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8183. '']));
  8184. end;
  8185. procedure TTestModule.TestForIntDo;
  8186. begin
  8187. StartProgram(false);
  8188. Add([
  8189. 'var i: longint;',
  8190. 'begin',
  8191. ' for i:=3 to 5 do ;',
  8192. ' for i:=i downto 2 do ;',
  8193. ' for i in byte do ;',
  8194. '']);
  8195. ConvertProgram;
  8196. CheckSource('TestForIntDo',
  8197. LinesToStr([ // statements
  8198. 'this.i = 0;']),
  8199. LinesToStr([ // this.$main
  8200. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8201. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8202. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8203. '']));
  8204. end;
  8205. procedure TTestModule.TestForIntInDo;
  8206. begin
  8207. StartProgram(false);
  8208. Add([
  8209. 'type',
  8210. ' TSetOfInt = set of byte;',
  8211. ' TIntRg = 3..7;',
  8212. ' TSetOfIntRg = set of TIntRg;',
  8213. 'var',
  8214. ' i,i2: longint;',
  8215. ' a1: array of byte;',
  8216. ' a2: array[1..3] of byte;',
  8217. ' soi: TSetOfInt;',
  8218. ' soir: TSetOfIntRg;',
  8219. ' ir: TIntRg;',
  8220. 'begin',
  8221. ' for i in byte do ;',
  8222. ' for i in a1 do ;',
  8223. ' for i in a2 do ;',
  8224. ' for i in [11..13] do ;',
  8225. ' for i in TSetOfInt do ;',
  8226. ' for i in TIntRg do ;',
  8227. ' for i in soi do i2:=i;',
  8228. ' for i in TSetOfIntRg do ;',
  8229. ' for i in soir do ;',
  8230. ' for ir in TIntRg do ;',
  8231. ' for ir in TSetOfIntRg do ;',
  8232. ' for ir in soir do ;',
  8233. '']);
  8234. ConvertProgram;
  8235. CheckSource('TestForIntInDo',
  8236. LinesToStr([ // statements
  8237. 'this.i = 0;',
  8238. 'this.i2 = 0;',
  8239. 'this.a1 = [];',
  8240. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8241. 'this.soi = {};',
  8242. 'this.soir = {};',
  8243. 'this.ir = 0;',
  8244. '']),
  8245. LinesToStr([ // this.$main
  8246. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8247. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8248. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8249. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8250. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8251. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8252. 'for (var $l6 in $mod.soi) {',
  8253. ' $mod.i = +$l6;',
  8254. ' $mod.i2 = $mod.i;',
  8255. '};',
  8256. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8257. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8258. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8259. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8260. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8261. '']));
  8262. end;
  8263. procedure TTestModule.TestCharConst;
  8264. begin
  8265. StartProgram(false);
  8266. Add([
  8267. 'const',
  8268. ' a = #$00F3;',
  8269. ' c: char = ''1'';',
  8270. ' wc: widechar = ''ä'';',
  8271. 'begin',
  8272. ' c:=#0;',
  8273. ' c:=#1;',
  8274. ' c:=#9;',
  8275. ' c:=#10;',
  8276. ' c:=#13;',
  8277. ' c:=#31;',
  8278. ' c:=#32;',
  8279. ' c:=#$A;',
  8280. ' c:=#$0A;',
  8281. ' c:=#$b;',
  8282. ' c:=#$0b;',
  8283. ' c:=^A;',
  8284. ' c:=''"'';',
  8285. ' c:=default(char);',
  8286. ' c:=#$00E4;', // ä
  8287. ' c:=''ä'';',
  8288. ' c:=#$E4;', // ä
  8289. ' c:=#$D800;', // invalid UTF-16
  8290. ' c:=#$DFFF;', // invalid UTF-16
  8291. ' c:=#$FFFF;', // last UCS-2
  8292. ' c:=high(c);', // last UCS-2
  8293. ' c:=#269;',
  8294. '']);
  8295. ConvertProgram;
  8296. CheckSource('TestCharConst',
  8297. LinesToStr([
  8298. 'this.a="ó";',
  8299. 'this.c="1";',
  8300. 'this.wc="ä";'
  8301. ]),
  8302. LinesToStr([
  8303. '$mod.c="\x00";',
  8304. '$mod.c="\x01";',
  8305. '$mod.c="\t";',
  8306. '$mod.c="\n";',
  8307. '$mod.c="\r";',
  8308. '$mod.c="\x1F";',
  8309. '$mod.c=" ";',
  8310. '$mod.c="\n";',
  8311. '$mod.c="\n";',
  8312. '$mod.c="\x0B";',
  8313. '$mod.c="\x0B";',
  8314. '$mod.c="\x01";',
  8315. '$mod.c=''"'';',
  8316. '$mod.c="\x00";',
  8317. '$mod.c = "ä";',
  8318. '$mod.c = "ä";',
  8319. '$mod.c = "ä";',
  8320. '$mod.c="\uD800";',
  8321. '$mod.c="\uDFFF";',
  8322. '$mod.c="\uFFFF";',
  8323. '$mod.c="\uFFFF";',
  8324. '$mod.c = "č";',
  8325. '']));
  8326. end;
  8327. procedure TTestModule.TestChar_Compare;
  8328. begin
  8329. StartProgram(false);
  8330. Add('var');
  8331. Add(' c: char;');
  8332. Add(' b: boolean;');
  8333. Add('begin');
  8334. Add(' b:=c=''1'';');
  8335. Add(' b:=''2''=c;');
  8336. Add(' b:=''3''=''4'';');
  8337. Add(' b:=c<>''5'';');
  8338. Add(' b:=''6''<>c;');
  8339. Add(' b:=c>''7'';');
  8340. Add(' b:=''8''>c;');
  8341. Add(' b:=c>=''9'';');
  8342. Add(' b:=''A''>=c;');
  8343. Add(' b:=c<''B'';');
  8344. Add(' b:=''C''<c;');
  8345. Add(' b:=c<=''D'';');
  8346. Add(' b:=''E''<=c;');
  8347. ConvertProgram;
  8348. CheckSource('TestChar_Compare',
  8349. LinesToStr([
  8350. 'this.c = "\x00";',
  8351. 'this.b = false;'
  8352. ]),
  8353. LinesToStr([
  8354. '$mod.b = $mod.c === "1";',
  8355. '$mod.b = "2" === $mod.c;',
  8356. '$mod.b = "3" === "4";',
  8357. '$mod.b = $mod.c !== "5";',
  8358. '$mod.b = "6" !== $mod.c;',
  8359. '$mod.b = $mod.c > "7";',
  8360. '$mod.b = "8" > $mod.c;',
  8361. '$mod.b = $mod.c >= "9";',
  8362. '$mod.b = "A" >= $mod.c;',
  8363. '$mod.b = $mod.c < "B";',
  8364. '$mod.b = "C" < $mod.c;',
  8365. '$mod.b = $mod.c <= "D";',
  8366. '$mod.b = "E" <= $mod.c;',
  8367. '']));
  8368. end;
  8369. procedure TTestModule.TestChar_BuiltInProcs;
  8370. begin
  8371. StartProgram(false);
  8372. Add([
  8373. 'var',
  8374. ' c: char;',
  8375. ' i: longint;',
  8376. ' s: string;',
  8377. 'begin',
  8378. ' i:=ord(c);',
  8379. ' i:=ord(s[i]);',
  8380. ' c:=chr(i);',
  8381. ' c:=pred(c);',
  8382. ' c:=succ(c);',
  8383. ' c:=low(c);',
  8384. ' c:=high(c);',
  8385. ' i:=byte(c);',
  8386. ' i:=word(c);',
  8387. ' i:=longint(c);',
  8388. '']);
  8389. ConvertProgram;
  8390. CheckSource('TestChar_BuiltInProcs',
  8391. LinesToStr([
  8392. 'this.c = "\x00";',
  8393. 'this.i = 0;',
  8394. 'this.s = "";'
  8395. ]),
  8396. LinesToStr([
  8397. '$mod.i = $mod.c.charCodeAt();',
  8398. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8399. '$mod.c = String.fromCharCode($mod.i);',
  8400. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8401. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8402. '$mod.c = "\x00";',
  8403. '$mod.c = "\uFFFF";',
  8404. '$mod.i = $mod.c.charCodeAt() & 255;',
  8405. '$mod.i = $mod.c.charCodeAt();',
  8406. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8407. '']));
  8408. end;
  8409. procedure TTestModule.TestStringConst;
  8410. begin
  8411. StartProgram(false);
  8412. Add([
  8413. '{$H+}',
  8414. 'const',
  8415. ' a = #$00F3#$017C;', // first <256, then >=256
  8416. ' b = string(''a'');',
  8417. ' c = string(''ä'');',
  8418. ' d = UnicodeString(''b'');',
  8419. ' e = UnicodeString(''ö'');',
  8420. ' f = low(a)+high(b);',
  8421. ' g: word = low(a);',
  8422. 'var',
  8423. ' s: string = ''abc'';',
  8424. ' i: longint;',
  8425. 'begin',
  8426. ' s:='''';',
  8427. ' s:=#13#10;',
  8428. ' s:=#9''foo'';',
  8429. ' s:=#$A9;',
  8430. ' s:=''foo''#13''bar'';',
  8431. ' s:=''"'';',
  8432. ' s:=''"''''"'';',
  8433. ' s:=#$20AC;', // euro
  8434. ' s:=#$10437;', // outside BMP
  8435. ' s:=''abc''#$20AC;', // ascii,#
  8436. ' s:=''ä''#$20AC;', // non ascii,#
  8437. ' s:=#$20AC''abc'';', // #, ascii
  8438. ' s:=#$20AC''ä'';', // #, non ascii
  8439. ' s:=default(string);',
  8440. ' s:=concat(s);',
  8441. ' s:=concat(s,''a'',s);',
  8442. ' s:=#250#269;',
  8443. ' i:=low(s)+high(a);',
  8444. ' s:=''a/b'';',
  8445. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8446. '']);
  8447. ConvertProgram;
  8448. CheckSource('TestStringConst',
  8449. LinesToStr([
  8450. 'this.a = "óż";',
  8451. 'this.b = "a";',
  8452. 'this.c = "ä";',
  8453. 'this.d = "b";',
  8454. 'this.e = "ö";',
  8455. 'this.f = 1 + this.b.length;',
  8456. 'this.g = 1;',
  8457. 'this.s="abc";',
  8458. 'this.i = 0;',
  8459. '']),
  8460. LinesToStr([
  8461. '$mod.s="";',
  8462. '$mod.s="\r\n";',
  8463. '$mod.s="\tfoo";',
  8464. '$mod.s="©";',
  8465. '$mod.s="foo\rbar";',
  8466. '$mod.s=''"'';',
  8467. '$mod.s=''"\''"'';',
  8468. '$mod.s="€";',
  8469. '$mod.s="'#$F0#$90#$90#$B7'";',
  8470. '$mod.s = "abc€";',
  8471. '$mod.s = "ä€";',
  8472. '$mod.s = "€abc";',
  8473. '$mod.s = "ۊ";',
  8474. '$mod.s="";',
  8475. '$mod.s = $mod.s;',
  8476. '$mod.s = $mod.s.concat("a", $mod.s);',
  8477. '$mod.s = "úč";',
  8478. '$mod.i = 1 + $mod.a.length;',
  8479. '$mod.s = "a/b";',
  8480. '']));
  8481. end;
  8482. procedure TTestModule.TestStringConst_InvalidUTF16;
  8483. begin
  8484. StartProgram(false);
  8485. Add([
  8486. 'const',
  8487. ' a: char = #$D87E;',
  8488. ' b: string = #$D87E;',
  8489. ' c: string = #$D87E#43;',
  8490. 'begin',
  8491. ' c:=''abc''#$D87E;',
  8492. ' c:=#0#1#2;',
  8493. ' c:=#127;',
  8494. ' c:=#128;',
  8495. ' c:=#255;',
  8496. ' c:=#256;',
  8497. '']);
  8498. ConvertProgram;
  8499. CheckSource('TestStringConst',
  8500. LinesToStr([
  8501. 'this.a = "\uD87E";',
  8502. 'this.b = "\uD87E";',
  8503. 'this.c = "\uD87E+";',
  8504. '']),
  8505. LinesToStr([
  8506. '$mod.c = "abc\uD87E";',
  8507. '$mod.c = "\x00\x01\x02";',
  8508. '$mod.c = "'#127'";',
  8509. '$mod.c = "'#$c2#$80'";',
  8510. '$mod.c = "'#$c3#$BF'";',
  8511. '$mod.c = "'#$c4#$80'";',
  8512. '']));
  8513. end;
  8514. procedure TTestModule.TestStringConstSurrogate;
  8515. begin
  8516. StartProgram(false);
  8517. Add([
  8518. 'var',
  8519. ' s: string;',
  8520. 'begin',
  8521. ' s:=''😊'';', // 1F60A
  8522. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8523. '']);
  8524. ConvertProgram;
  8525. CheckSource('TestStringConstSurrogate',
  8526. LinesToStr([
  8527. 'this.s="";'
  8528. ]),
  8529. LinesToStr([
  8530. '$mod.s="😊";',
  8531. '$mod.s="Hello 😉";'
  8532. ]));
  8533. end;
  8534. procedure TTestModule.TestStringConstWhitespaces;
  8535. begin
  8536. StartProgram(false);
  8537. Add([
  8538. 'var',
  8539. ' s: string;',
  8540. 'begin',
  8541. ' s:=#$2028;', // line separator not supported by some editors, e.g. vsc
  8542. ' s:=''Medium Mathematical Space ''#$205f',
  8543. '']);
  8544. ConvertProgram;
  8545. CheckSource('TestStringConstSurrogate',
  8546. LinesToStr([
  8547. 'this.s="";'
  8548. ]),
  8549. LinesToStr([
  8550. '$mod.s="\u2028";',
  8551. '$mod.s="Medium Mathematical Space \u205F";'
  8552. ]));
  8553. end;
  8554. procedure TTestModule.TestStringConst_Multiline;
  8555. begin
  8556. StartProgram(false);
  8557. Add([
  8558. '{$modeswitch multilinestrings}',
  8559. 'const',
  8560. ' a = ``;',
  8561. ' b = `',
  8562. 'line`;',
  8563. ' c = `Single`;',
  8564. ' d = ````;',
  8565. ' e = `abc``xyz`;',
  8566. ' f = `first''line',
  8567. ' second''line`#10;',
  8568. 'begin',
  8569. '']);
  8570. ConvertProgram;
  8571. CheckSource('TestStringConst_Multiline',
  8572. LinesToStr([
  8573. 'this.a = "";',
  8574. 'this.b = "'+JSONNewLine+'line";',
  8575. 'this.c = "Single";',
  8576. 'this.d = "`";',
  8577. 'this.e = "abc`xyz";',
  8578. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8579. '']),
  8580. LinesToStr([
  8581. ]));
  8582. end;
  8583. procedure TTestModule.TestString_Length;
  8584. begin
  8585. StartProgram(false);
  8586. Add('const c = ''foo'';');
  8587. Add('var');
  8588. Add(' s: string;');
  8589. Add(' i: longint;');
  8590. Add('begin');
  8591. Add(' i:=length(s);');
  8592. Add(' i:=length(s+s);');
  8593. Add(' i:=length(''abc'');');
  8594. Add(' i:=length(c);');
  8595. ConvertProgram;
  8596. CheckSource('TestString_Length',
  8597. LinesToStr([
  8598. 'this.c = "foo";',
  8599. 'this.s = "";',
  8600. 'this.i = 0;',
  8601. '']),
  8602. LinesToStr([
  8603. '$mod.i = $mod.s.length;',
  8604. '$mod.i = ($mod.s+$mod.s).length;',
  8605. '$mod.i = "abc".length;',
  8606. '$mod.i = $mod.c.length;',
  8607. '']));
  8608. end;
  8609. procedure TTestModule.TestString_Compare;
  8610. begin
  8611. StartProgram(false);
  8612. Add('var');
  8613. Add(' s, t: string;');
  8614. Add(' b: boolean;');
  8615. Add('begin');
  8616. Add(' b:=s=t;');
  8617. Add(' b:=s<>t;');
  8618. Add(' b:=s>t;');
  8619. Add(' b:=s>=t;');
  8620. Add(' b:=s<t;');
  8621. Add(' b:=s<=t;');
  8622. ConvertProgram;
  8623. CheckSource('TestString_Compare',
  8624. LinesToStr([ // statements
  8625. 'this.s = "";',
  8626. 'this.t = "";',
  8627. 'this.b =false;'
  8628. ]),
  8629. LinesToStr([ // this.$main
  8630. '$mod.b = $mod.s === $mod.t;',
  8631. '$mod.b = $mod.s !== $mod.t;',
  8632. '$mod.b = $mod.s > $mod.t;',
  8633. '$mod.b = $mod.s >= $mod.t;',
  8634. '$mod.b = $mod.s < $mod.t;',
  8635. '$mod.b = $mod.s <= $mod.t;',
  8636. '']));
  8637. end;
  8638. procedure TTestModule.TestString_SetLength;
  8639. begin
  8640. StartProgram(false);
  8641. Add([
  8642. 'procedure Fly(var s: string);',
  8643. 'begin',
  8644. ' SetLength(s,1);',
  8645. 'end;',
  8646. 'procedure Run(var s: unicodestring);',
  8647. 'begin',
  8648. ' SetLength(s,2);',
  8649. 'end;',
  8650. 'var s: string;',
  8651. ' u: unicodestring;',
  8652. 'begin',
  8653. ' SetLength(s,3);',
  8654. ' SetLength(u,4);',
  8655. '']);
  8656. ConvertProgram;
  8657. CheckSource('TestString_SetLength',
  8658. LinesToStr([ // statements
  8659. 'this.Fly = function (s) {',
  8660. ' s.set(rtl.strSetLength(s.get(), 1));',
  8661. '};',
  8662. 'this.Run = function (s) {',
  8663. ' s.set(rtl.strSetLength(s.get(), 2));',
  8664. '};',
  8665. 'this.s = "";',
  8666. 'this.u = "";',
  8667. '']),
  8668. LinesToStr([ // this.$main
  8669. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8670. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8671. ]));
  8672. end;
  8673. procedure TTestModule.TestString_CharAt;
  8674. begin
  8675. StartProgram(false);
  8676. Add([
  8677. 'var',
  8678. ' s: string;',
  8679. ' c: char;',
  8680. ' b: boolean;',
  8681. 'begin',
  8682. ' b:= s[1] = c;',
  8683. ' b:= c = s[1];',
  8684. ' b:= c <> s[1];',
  8685. ' b:= c > s[1];',
  8686. ' b:= c >= s[1];',
  8687. ' b:= c < s[2];',
  8688. ' b:= c <= s[1];',
  8689. ' s[1] := c;',
  8690. ' s[2+3] := c;']);
  8691. ConvertProgram;
  8692. CheckSource('TestString_CharAt',
  8693. LinesToStr([ // statements
  8694. 'this.s = "";',
  8695. 'this.c = "\x00";',
  8696. 'this.b = false;'
  8697. ]),
  8698. LinesToStr([ // this.$main
  8699. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8700. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8701. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8702. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8703. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8704. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8705. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8706. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8707. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8708. '']));
  8709. end;
  8710. procedure TTestModule.TestStringHMinusFail;
  8711. begin
  8712. StartProgram(false);
  8713. Add([
  8714. '{$H-}',
  8715. 'var s: string;',
  8716. 'begin']);
  8717. ConvertProgram;
  8718. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8719. end;
  8720. procedure TTestModule.TestStr;
  8721. begin
  8722. StartProgram(false);
  8723. Add('var');
  8724. Add(' b: boolean;');
  8725. Add(' i: longint;');
  8726. Add(' d: double;');
  8727. Add(' s: string;');
  8728. Add('begin');
  8729. Add(' str(b,s);');
  8730. Add(' str(i,s);');
  8731. Add(' str(d,s);');
  8732. Add(' str(i:3,s);');
  8733. Add(' str(d:3:2,s);');
  8734. Add(' Str(12.456:12:1,s);');
  8735. Add(' Str(12.456:12,s);');
  8736. Add(' s:=str(b);');
  8737. Add(' s:=str(i);');
  8738. Add(' s:=str(d);');
  8739. Add(' s:=str(i,i);');
  8740. Add(' s:=str(i:3);');
  8741. Add(' s:=str(d:3:2);');
  8742. Add(' s:=str(i:4,i);');
  8743. Add(' s:=str(i,i:5);');
  8744. Add(' s:=str(i:4,i:5);');
  8745. Add(' s:=str(s,s);');
  8746. Add(' s:=str(s,''foo'');');
  8747. ConvertProgram;
  8748. CheckSource('TestStr',
  8749. LinesToStr([ // statements
  8750. 'this.b = false;',
  8751. 'this.i = 0;',
  8752. 'this.d = 0.0;',
  8753. 'this.s = "";',
  8754. '']),
  8755. LinesToStr([ // this.$main
  8756. '$mod.s = ""+$mod.b;',
  8757. '$mod.s = ""+$mod.i;',
  8758. '$mod.s = rtl.floatToStr($mod.d);',
  8759. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8760. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8761. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8762. '$mod.s = rtl.floatToStr(12.456,12);',
  8763. '$mod.s = ""+$mod.b;',
  8764. '$mod.s = ""+$mod.i;',
  8765. '$mod.s = rtl.floatToStr($mod.d);',
  8766. '$mod.s = ""+$mod.i+$mod.i;',
  8767. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8768. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8769. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8770. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8771. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8772. '$mod.s = $mod.s + $mod.s;',
  8773. '$mod.s = $mod.s + "foo";',
  8774. '']));
  8775. end;
  8776. procedure TTestModule.TestBaseType_AnsiStringFail;
  8777. begin
  8778. StartProgram(false);
  8779. Add('var s: AnsiString');
  8780. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8781. ConvertProgram;
  8782. end;
  8783. procedure TTestModule.TestBaseType_WideStringFail;
  8784. begin
  8785. StartProgram(false);
  8786. Add('var s: WideString');
  8787. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8788. ConvertProgram;
  8789. end;
  8790. procedure TTestModule.TestBaseType_ShortStringFail;
  8791. begin
  8792. StartProgram(false);
  8793. Add('var s: ShortString');
  8794. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8795. ConvertProgram;
  8796. end;
  8797. procedure TTestModule.TestBaseType_RawByteStringFail;
  8798. begin
  8799. StartProgram(false);
  8800. Add('var s: RawByteString');
  8801. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8802. ConvertProgram;
  8803. end;
  8804. procedure TTestModule.TestTypeShortstring_Fail;
  8805. begin
  8806. StartProgram(false);
  8807. Add('type t = string[12];');
  8808. Add('var s: t;');
  8809. Add('begin');
  8810. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8811. ConvertProgram;
  8812. end;
  8813. procedure TTestModule.TestCharSet_Custom;
  8814. begin
  8815. StartProgram(false);
  8816. Add([
  8817. 'type',
  8818. ' TCharRg = ''a''..''z'';',
  8819. ' TSetOfCharRg = set of TCharRg;',
  8820. ' TCharRg2 = ''m''..''p'';',
  8821. 'const',
  8822. ' crg: TCharRg = ''b'';',
  8823. 'var',
  8824. ' c: char;',
  8825. ' crg2: TCharRg2;',
  8826. ' s: TSetOfCharRg;',
  8827. 'begin',
  8828. ' c:=crg;',
  8829. ' crg:=c;',
  8830. ' crg2:=crg;',
  8831. ' if c=crg then ;',
  8832. ' if crg=c then ;',
  8833. ' if crg=crg2 then ;',
  8834. ' if c in s then ;',
  8835. ' if crg2 in s then ;',
  8836. ' c:=default(TCharRg);',
  8837. '']);
  8838. ConvertProgram;
  8839. CheckSource('TestCharSet_Custom',
  8840. LinesToStr([ // statements
  8841. 'this.crg = "b";',
  8842. 'this.c = "\x00";',
  8843. 'this.crg2 = "m";',
  8844. 'this.s = {};',
  8845. '']),
  8846. LinesToStr([ // this.$main
  8847. '$mod.c = $mod.crg;',
  8848. '$mod.crg = $mod.c;',
  8849. '$mod.crg2 = $mod.crg;',
  8850. 'if ($mod.c === $mod.crg) ;',
  8851. 'if ($mod.crg === $mod.c) ;',
  8852. 'if ($mod.crg === $mod.crg2) ;',
  8853. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8854. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8855. '$mod.c = "a";',
  8856. '']));
  8857. end;
  8858. procedure TTestModule.TestWideChar;
  8859. begin
  8860. StartProgram(false);
  8861. Add([
  8862. 'procedure Fly(var c: char);',
  8863. 'begin',
  8864. 'end;',
  8865. 'procedure Run(var c: widechar);',
  8866. 'begin',
  8867. 'end;',
  8868. 'var',
  8869. ' c: char;',
  8870. ' wc: widechar;',
  8871. ' w: word;',
  8872. 'begin',
  8873. ' Fly(wc);',
  8874. ' Run(c);',
  8875. ' wc:=WideChar(w);',
  8876. ' w:=ord(wc);',
  8877. '']);
  8878. ConvertProgram;
  8879. CheckSource('TestWideChar_VarArg',
  8880. LinesToStr([ // statements
  8881. 'this.Fly = function (c) {',
  8882. '};',
  8883. 'this.Run = function (c) {',
  8884. '};',
  8885. 'this.c = "\x00";',
  8886. 'this.wc = "\x00";',
  8887. 'this.w = 0;',
  8888. '']),
  8889. LinesToStr([ // this.$main
  8890. '$mod.Fly({',
  8891. ' p: $mod,',
  8892. ' get: function () {',
  8893. ' return this.p.wc;',
  8894. ' },',
  8895. ' set: function (v) {',
  8896. ' this.p.wc = v;',
  8897. ' }',
  8898. '});',
  8899. '$mod.Run({',
  8900. ' p: $mod,',
  8901. ' get: function () {',
  8902. ' return this.p.c;',
  8903. ' },',
  8904. ' set: function (v) {',
  8905. ' this.p.c = v;',
  8906. ' }',
  8907. '});',
  8908. '$mod.wc = String.fromCharCode($mod.w);',
  8909. '$mod.w = $mod.wc.charCodeAt();',
  8910. '',
  8911. '']));
  8912. end;
  8913. procedure TTestModule.TestForCharDo;
  8914. begin
  8915. StartProgram(false);
  8916. Add([
  8917. 'var c: char;',
  8918. 'begin',
  8919. ' for c:=''a'' to ''c'' do ;',
  8920. ' for c:=c downto ''a'' do ;',
  8921. ' for c:=''Б'' to ''Я'' do ;',
  8922. '']);
  8923. ConvertProgram;
  8924. CheckSource('TestForCharDo',
  8925. LinesToStr([ // statements
  8926. 'this.c = "\x00";']),
  8927. LinesToStr([ // this.$main
  8928. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8929. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8930. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8931. '']));
  8932. end;
  8933. procedure TTestModule.TestForCharInDo;
  8934. begin
  8935. StartProgram(false);
  8936. Add([
  8937. 'type',
  8938. ' TSetOfChar = set of char;',
  8939. ' TCharRg = ''a''..''z'';',
  8940. ' TSetOfCharRg = set of TCharRg;',
  8941. 'const Foo = ''foo'';',
  8942. 'var',
  8943. ' c,c2: char;',
  8944. ' s: string;',
  8945. ' a1: array of char;',
  8946. ' a2: array[1..3] of char;',
  8947. ' soc: TSetOfChar;',
  8948. ' socr: TSetOfCharRg;',
  8949. ' cr: TCharRg;',
  8950. 'begin',
  8951. ' for c in foo do ;',
  8952. ' for c in s do ;',
  8953. ' for c in char do ;',
  8954. ' for c in a1 do ;',
  8955. ' for c in a2 do ;',
  8956. ' for c in [''1''..''3''] do ;',
  8957. ' for c in TSetOfChar do ;',
  8958. ' for c in TCharRg do ;',
  8959. ' for c in soc do c2:=c;',
  8960. ' for c in TSetOfCharRg do ;',
  8961. ' for c in socr do ;',
  8962. ' for cr in TCharRg do ;',
  8963. ' for cr in TSetOfCharRg do ;',
  8964. ' for cr in socr do ;',
  8965. '']);
  8966. ConvertProgram;
  8967. CheckSource('TestForCharInDo',
  8968. LinesToStr([ // statements
  8969. 'this.Foo = "foo";',
  8970. 'this.c = "\x00";',
  8971. 'this.c2 = "\x00";',
  8972. 'this.s = "";',
  8973. 'this.a1 = [];',
  8974. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8975. 'this.soc = {};',
  8976. 'this.socr = {};',
  8977. 'this.cr = "a";',
  8978. '']),
  8979. LinesToStr([ // this.$main
  8980. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8981. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8982. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8983. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8984. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8985. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8986. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8987. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8988. 'for (var $l8 in $mod.soc) {',
  8989. ' $mod.c = String.fromCharCode($l8);',
  8990. ' $mod.c2 = $mod.c;',
  8991. '};',
  8992. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8993. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8994. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8995. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8996. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8997. '']));
  8998. end;
  8999. procedure TTestModule.TestProcTwoArgs;
  9000. begin
  9001. StartProgram(false);
  9002. Add('procedure Test(a,b: longint);');
  9003. Add('begin');
  9004. Add('end;');
  9005. Add('begin');
  9006. ConvertProgram;
  9007. CheckSource('TestProcTwoArgs',
  9008. LinesToStr([ // statements
  9009. 'this.Test = function (a,b) {',
  9010. '};'
  9011. ]),
  9012. LinesToStr([ // this.$main
  9013. ''
  9014. ]));
  9015. end;
  9016. procedure TTestModule.TestProc_DefaultValue;
  9017. begin
  9018. StartProgram(false);
  9019. Add('procedure p1(i: longint = 1);');
  9020. Add('begin');
  9021. Add('end;');
  9022. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  9023. Add('begin');
  9024. Add('end;');
  9025. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  9026. Add('begin');
  9027. Add('end;');
  9028. Add('begin');
  9029. Add(' p1;');
  9030. Add(' p1();');
  9031. Add(' p1(11);');
  9032. Add(' p2;');
  9033. Add(' p2();');
  9034. Add(' p2(12);');
  9035. Add(' p2(13,''b'');');
  9036. Add(' p3();');
  9037. ConvertProgram;
  9038. CheckSource('TestProc_DefaultValue',
  9039. LinesToStr([ // statements
  9040. 'this.p1 = function (i) {',
  9041. '};',
  9042. 'this.p2 = function (i,c) {',
  9043. '};',
  9044. 'this.p3 = function (d,b,s) {',
  9045. '};'
  9046. ]),
  9047. LinesToStr([ // this.$main
  9048. ' $mod.p1(1);',
  9049. ' $mod.p1(1);',
  9050. ' $mod.p1(11);',
  9051. ' $mod.p2(1,"a");',
  9052. ' $mod.p2(1,"a");',
  9053. ' $mod.p2(12,"a");',
  9054. ' $mod.p2(13,"b");',
  9055. ' $mod.p3(1.0,false,"abc");'
  9056. ]));
  9057. end;
  9058. procedure TTestModule.TestFunctionInt;
  9059. begin
  9060. StartProgram(false);
  9061. Add('function MyTest(Bar: longint): longint;');
  9062. Add('begin');
  9063. Add(' Result:=2*bar');
  9064. Add('end;');
  9065. Add('begin');
  9066. ConvertProgram;
  9067. CheckSource('TestFunctionInt',
  9068. LinesToStr([ // statements
  9069. 'this.MyTest = function (Bar) {',
  9070. ' var Result = 0;',
  9071. ' Result = 2*Bar;',
  9072. ' return Result;',
  9073. '};'
  9074. ]),
  9075. LinesToStr([ // this.$main
  9076. ''
  9077. ]));
  9078. end;
  9079. procedure TTestModule.TestFunctionString;
  9080. begin
  9081. StartProgram(false);
  9082. Add('function Test(Bar: string): string;');
  9083. Add('begin');
  9084. Add(' Result:=bar+BAR');
  9085. Add('end;');
  9086. Add('begin');
  9087. ConvertProgram;
  9088. CheckSource('TestFunctionString',
  9089. LinesToStr([ // statements
  9090. 'this.Test = function (Bar) {',
  9091. ' var Result = "";',
  9092. ' Result = Bar+Bar;',
  9093. ' return Result;',
  9094. '};'
  9095. ]),
  9096. LinesToStr([ // this.$main
  9097. ''
  9098. ]));
  9099. end;
  9100. procedure TTestModule.TestIfThen;
  9101. begin
  9102. StartProgram(false);
  9103. Add([
  9104. 'var b: boolean;',
  9105. 'begin',
  9106. ' if b then ;',
  9107. ' if b then else ;']);
  9108. ConvertProgram;
  9109. CheckSource('TestIfThen',
  9110. LinesToStr([ // statements
  9111. 'this.b = false;',
  9112. '']),
  9113. LinesToStr([ // this.$main
  9114. 'if ($mod.b) ;',
  9115. 'if ($mod.b) ;',
  9116. '']));
  9117. end;
  9118. procedure TTestModule.TestForLoop;
  9119. begin
  9120. StartProgram(false);
  9121. Add('var');
  9122. Add(' vI, vJ, vN: longint;');
  9123. Add('begin');
  9124. Add(' VJ:=0;');
  9125. Add(' VN:=3;');
  9126. Add(' for VI:=1 to VN do');
  9127. Add(' begin');
  9128. Add(' VJ:=VJ+VI;');
  9129. Add(' end;');
  9130. ConvertProgram;
  9131. CheckSource('TestForLoop',
  9132. LinesToStr([ // statements
  9133. 'this.vI = 0;',
  9134. 'this.vJ = 0;',
  9135. 'this.vN = 0;'
  9136. ]),
  9137. LinesToStr([ // this.$main
  9138. ' $mod.vJ = 0;',
  9139. ' $mod.vN = 3;',
  9140. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9141. ' $mod.vI = $l;',
  9142. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9143. ' };',
  9144. '']));
  9145. end;
  9146. procedure TTestModule.TestForLoopInsideFunction;
  9147. begin
  9148. StartProgram(false);
  9149. Add('function SumNumbers(Count: longint): longint;');
  9150. Add('var');
  9151. Add(' vI, vJ: longint;');
  9152. Add('begin');
  9153. Add(' vj:=0;');
  9154. Add(' for vi:=1 to count do');
  9155. Add(' begin');
  9156. Add(' vj:=vj+vi;');
  9157. Add(' end;');
  9158. Add('end;');
  9159. Add('begin');
  9160. Add(' sumnumbers(3);');
  9161. ConvertProgram;
  9162. CheckSource('TestForLoopInsideFunction',
  9163. LinesToStr([ // statements
  9164. 'this.SumNumbers = function (Count) {',
  9165. ' var Result = 0;',
  9166. ' var vI = 0;',
  9167. ' var vJ = 0;',
  9168. ' vJ = 0;',
  9169. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9170. ' vI = $l;',
  9171. ' vJ = vJ + vI;',
  9172. ' };',
  9173. ' return Result;',
  9174. '};'
  9175. ]),
  9176. LinesToStr([ // $mod.$main
  9177. ' $mod.SumNumbers(3);'
  9178. ]));
  9179. end;
  9180. procedure TTestModule.TestForLoop_ReadVarAfter;
  9181. begin
  9182. StartProgram(false);
  9183. Add('var');
  9184. Add(' vI: longint;');
  9185. Add('begin');
  9186. Add(' for vi:=1 to 2 do ;');
  9187. Add(' if vi=3 then ;');
  9188. ConvertProgram;
  9189. CheckSource('TestForLoop',
  9190. LinesToStr([ // statements
  9191. 'this.vI = 0;'
  9192. ]),
  9193. LinesToStr([ // this.$main
  9194. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9195. ' if ($mod.vI===3) ;'
  9196. ]));
  9197. end;
  9198. procedure TTestModule.TestForLoop_Nested;
  9199. begin
  9200. StartProgram(false);
  9201. Add('function SumNumbers(Count: longint): longint;');
  9202. Add('var');
  9203. Add(' vI, vJ, vK: longint;');
  9204. Add('begin');
  9205. Add(' VK:=0;');
  9206. Add(' for VI:=1 to count do');
  9207. Add(' begin');
  9208. Add(' for vj:=1 to vi do');
  9209. Add(' begin');
  9210. Add(' vk:=VK+VI;');
  9211. Add(' end;');
  9212. Add(' end;');
  9213. Add('end;');
  9214. Add('begin');
  9215. Add(' sumnumbers(3);');
  9216. ConvertProgram;
  9217. CheckSource('TestForLoopInFunction',
  9218. LinesToStr([ // statements
  9219. 'this.SumNumbers = function (Count) {',
  9220. ' var Result = 0;',
  9221. ' var vI = 0;',
  9222. ' var vJ = 0;',
  9223. ' var vK = 0;',
  9224. ' vK = 0;',
  9225. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9226. ' vI = $l;',
  9227. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9228. ' vJ = $l1;',
  9229. ' vK = vK + vI;',
  9230. ' };',
  9231. ' };',
  9232. ' return Result;',
  9233. '};'
  9234. ]),
  9235. LinesToStr([ // $mod.$main
  9236. ' $mod.SumNumbers(3);'
  9237. ]));
  9238. end;
  9239. procedure TTestModule.TestRepeatUntil;
  9240. begin
  9241. StartProgram(false);
  9242. Add('var');
  9243. Add(' vI, vJ, vN: longint;');
  9244. Add('begin');
  9245. Add(' vn:=3;');
  9246. Add(' vj:=0;');
  9247. Add(' VI:=0;');
  9248. Add(' repeat');
  9249. Add(' VI:=vi+1;');
  9250. Add(' vj:=VJ+vI;');
  9251. Add(' until vi>=vn');
  9252. ConvertProgram;
  9253. CheckSource('TestRepeatUntil',
  9254. LinesToStr([ // statements
  9255. 'this.vI = 0;',
  9256. 'this.vJ = 0;',
  9257. 'this.vN = 0;'
  9258. ]),
  9259. LinesToStr([ // $mod.$main
  9260. ' $mod.vN = 3;',
  9261. ' $mod.vJ = 0;',
  9262. ' $mod.vI = 0;',
  9263. ' do{',
  9264. ' $mod.vI = $mod.vI + 1;',
  9265. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9266. ' }while(!($mod.vI>=$mod.vN));'
  9267. ]));
  9268. end;
  9269. procedure TTestModule.TestAsmBlock;
  9270. begin
  9271. StartProgram(false);
  9272. Add([
  9273. 'var',
  9274. ' vI: longint;',
  9275. 'begin',
  9276. ' vi:=1;',
  9277. ' asm',
  9278. ' if (vI===1) {',
  9279. ' vI=2;',
  9280. //' console.log(''end;'');', ToDo
  9281. ' }',
  9282. ' if (vI===2){ vI=3; }',
  9283. ' end;',
  9284. ' VI:=4;']);
  9285. ConvertProgram;
  9286. CheckSource('TestAsmBlock',
  9287. LinesToStr([ // statements
  9288. 'this.vI = 0;'
  9289. ]),
  9290. LinesToStr([ // $mod.$main
  9291. '$mod.vI = 1;',
  9292. 'if (vI===1) {',
  9293. ' vI=2;',
  9294. '}',
  9295. 'if (vI===2){ vI=3; }',
  9296. ';',
  9297. '$mod.vI = 4;'
  9298. ]));
  9299. end;
  9300. procedure TTestModule.TestAsmPas_Impl;
  9301. begin
  9302. StartUnit(false);
  9303. Add('interface');
  9304. Add('const cIntf: longint = 1;');
  9305. Add('var vIntf: longint;');
  9306. Add('implementation');
  9307. Add('const cImpl: longint = 2;');
  9308. Add('var vImpl: longint;');
  9309. Add('procedure DoIt;');
  9310. Add('const cLoc: longint = 3;');
  9311. Add('var vLoc: longint;');
  9312. Add('begin;');
  9313. Add(' asm');
  9314. //Add(' pas(vIntf)=pas(cIntf);');
  9315. //Add(' pas(vImpl)=pas(cImpl);');
  9316. //Add(' pas(vLoc)=pas(cLoc);');
  9317. Add(' end;');
  9318. Add('end;');
  9319. ConvertUnit;
  9320. CheckSource('TestAsmPas_Impl',
  9321. LinesToStr([
  9322. 'var $impl = $mod.$impl;',
  9323. 'this.cIntf = 1;',
  9324. 'this.vIntf = 0;',
  9325. '']),
  9326. '', // this.$init
  9327. LinesToStr([ // implementation
  9328. '$impl.cImpl = 2;',
  9329. '$impl.vImpl = 0;',
  9330. 'var cLoc = 3;',
  9331. '$impl.DoIt = function () {',
  9332. ' var vLoc = 0;',
  9333. '};',
  9334. '']) );
  9335. end;
  9336. procedure TTestModule.TestTryFinally;
  9337. begin
  9338. StartProgram(false);
  9339. Add('var i: longint;');
  9340. Add('begin');
  9341. Add(' try');
  9342. Add(' i:=0; i:=2 div i;');
  9343. Add(' finally');
  9344. Add(' i:=3');
  9345. Add(' end;');
  9346. ConvertProgram;
  9347. CheckSource('TestTryFinally',
  9348. LinesToStr([ // statements
  9349. 'this.i = 0;'
  9350. ]),
  9351. LinesToStr([ // $mod.$main
  9352. 'try {',
  9353. ' $mod.i = 0;',
  9354. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9355. '} finally {',
  9356. ' $mod.i = 3;',
  9357. '};'
  9358. ]));
  9359. end;
  9360. procedure TTestModule.TestTryExcept;
  9361. begin
  9362. StartProgram(false);
  9363. Add([
  9364. 'type',
  9365. ' TObject = class end;',
  9366. ' Exception = class Msg: string; end;',
  9367. ' EInvalidCast = class(Exception) end;',
  9368. 'var vI: longint;',
  9369. 'begin',
  9370. ' try',
  9371. ' vi:=1;',
  9372. ' except',
  9373. ' vi:=2',
  9374. ' end;',
  9375. ' try',
  9376. ' vi:=3;',
  9377. ' except',
  9378. ' raise;',
  9379. ' end;',
  9380. ' try',
  9381. ' VI:=4;',
  9382. ' except',
  9383. ' on einvalidcast do',
  9384. ' raise;',
  9385. ' on E: exception do',
  9386. ' if e.msg='''' then',
  9387. ' raise e;',
  9388. ' else',
  9389. ' vi:=5',
  9390. ' end;',
  9391. ' try',
  9392. ' VI:=6;',
  9393. ' except',
  9394. ' on einvalidcast do ;',
  9395. ' end;',
  9396. '']);
  9397. ConvertProgram;
  9398. CheckSource('TestTryExcept',
  9399. LinesToStr([ // statements
  9400. 'rtl.createClass(this, "TObject", null, function () {',
  9401. ' this.$init = function () {',
  9402. ' };',
  9403. ' this.$final = function () {',
  9404. ' };',
  9405. '});',
  9406. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9407. ' this.$init = function () {',
  9408. ' $mod.TObject.$init.call(this);',
  9409. ' this.Msg = "";',
  9410. ' };',
  9411. '});',
  9412. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9413. '});',
  9414. 'this.vI = 0;'
  9415. ]),
  9416. LinesToStr([ // $mod.$main
  9417. 'try {',
  9418. ' $mod.vI = 1;',
  9419. '} catch ($e) {',
  9420. ' $mod.vI = 2;',
  9421. '};',
  9422. 'try {',
  9423. ' $mod.vI = 3;',
  9424. '} catch ($e) {',
  9425. ' throw $e;',
  9426. '};',
  9427. 'try {',
  9428. ' $mod.vI = 4;',
  9429. '} catch ($e) {',
  9430. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9431. ' throw $e',
  9432. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9433. ' var E = $e;',
  9434. ' if (E.Msg === "") throw E;',
  9435. ' } else {',
  9436. ' $mod.vI = 5;',
  9437. ' }',
  9438. '};',
  9439. 'try {',
  9440. ' $mod.vI = 6;',
  9441. '} catch ($e) {',
  9442. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9443. ' } else throw $e',
  9444. '};',
  9445. '']));
  9446. end;
  9447. procedure TTestModule.TestTryExcept_ReservedWords;
  9448. begin
  9449. StartProgram(false);
  9450. Add([
  9451. 'type',
  9452. ' TObject = class end;',
  9453. ' Exception = class',
  9454. ' Symbol: string;',
  9455. ' end;',
  9456. 'var &try: longint;',
  9457. 'begin',
  9458. ' try',
  9459. ' &try:=4;',
  9460. ' except',
  9461. ' on Error: exception do',
  9462. ' if errOR.symBol='''' then',
  9463. ' raise ERRor;',
  9464. ' end;',
  9465. '']);
  9466. ConvertProgram;
  9467. CheckSource('TestTryExcept_ReservedWords',
  9468. LinesToStr([ // statements
  9469. 'rtl.createClass(this, "TObject", null, function () {',
  9470. ' this.$init = function () {',
  9471. ' };',
  9472. ' this.$final = function () {',
  9473. ' };',
  9474. '});',
  9475. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9476. ' this.$init = function () {',
  9477. ' $mod.TObject.$init.call(this);',
  9478. ' this.Symbol = "";',
  9479. ' };',
  9480. '});',
  9481. 'this.Try = 0;',
  9482. '']),
  9483. LinesToStr([ // $mod.$main
  9484. 'try {',
  9485. ' $mod.Try = 4;',
  9486. '} catch ($e) {',
  9487. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9488. ' var error = $e;',
  9489. ' if (error.Symbol === "") throw error;',
  9490. ' } else throw $e',
  9491. '};',
  9492. '']));
  9493. end;
  9494. procedure TTestModule.TestIfThenRaiseElse;
  9495. begin
  9496. StartProgram(false);
  9497. Add([
  9498. 'type',
  9499. ' TObject = class',
  9500. ' constructor Create;',
  9501. ' end;',
  9502. 'constructor TObject.Create;',
  9503. 'begin',
  9504. 'end;',
  9505. 'var b: boolean;',
  9506. 'begin',
  9507. ' if b then',
  9508. ' raise TObject.Create',
  9509. ' else',
  9510. ' b:=false;',
  9511. '']);
  9512. ConvertProgram;
  9513. CheckSource('TestIfThenRaiseElse',
  9514. LinesToStr([ // statements
  9515. 'rtl.createClass(this, "TObject", null, function () {',
  9516. ' this.$init = function () {',
  9517. ' };',
  9518. ' this.$final = function () {',
  9519. ' };',
  9520. ' this.Create = function () {',
  9521. ' return this;',
  9522. ' };',
  9523. '});',
  9524. 'this.b = false;',
  9525. '']),
  9526. LinesToStr([ // $mod.$main
  9527. 'if ($mod.b) {',
  9528. ' throw $mod.TObject.$create("Create")}',
  9529. ' else $mod.b = false;',
  9530. '']));
  9531. end;
  9532. procedure TTestModule.TestCaseOf;
  9533. begin
  9534. StartProgram(false);
  9535. Add([
  9536. 'const e: longint; external name ''$e'';',
  9537. 'var vI: longint;',
  9538. 'begin',
  9539. ' case vi of',
  9540. ' 1: ;',
  9541. ' 2: vi:=3;',
  9542. ' e: ;',
  9543. ' else',
  9544. ' VI:=4',
  9545. ' end;']);
  9546. ConvertProgram;
  9547. CheckSource('TestCaseOf',
  9548. LinesToStr([ // statements
  9549. 'this.vI = 0;'
  9550. ]),
  9551. LinesToStr([ // $mod.$main
  9552. 'var $tmp = $mod.vI;',
  9553. 'if ($tmp === 1) {}',
  9554. 'else if ($tmp === 2) {',
  9555. ' $mod.vI = 3}',
  9556. ' else if ($tmp === $e) {}',
  9557. 'else {',
  9558. ' $mod.vI = 4;',
  9559. '};'
  9560. ]));
  9561. end;
  9562. procedure TTestModule.TestCaseOf_UseSwitch;
  9563. begin
  9564. StartProgram(false);
  9565. Converter.UseSwitchStatement:=true;
  9566. Add('var Vi: longint;');
  9567. Add('begin');
  9568. Add(' case vi of');
  9569. Add(' 1: ;');
  9570. Add(' 2: VI:=3;');
  9571. Add(' else');
  9572. Add(' vi:=4');
  9573. Add(' end;');
  9574. ConvertProgram;
  9575. CheckSource('TestCaseOf_UseSwitch',
  9576. LinesToStr([ // statements
  9577. 'this.Vi = 0;'
  9578. ]),
  9579. LinesToStr([ // $mod.$main
  9580. 'switch ($mod.Vi) {',
  9581. 'case 1:',
  9582. ' break;',
  9583. 'case 2:',
  9584. ' $mod.Vi = 3;',
  9585. ' break;',
  9586. 'default:',
  9587. ' $mod.Vi = 4;',
  9588. '};'
  9589. ]));
  9590. end;
  9591. procedure TTestModule.TestCaseOfNoElse;
  9592. begin
  9593. StartProgram(false);
  9594. Add('var Vi: longint;');
  9595. Add('begin');
  9596. Add(' case vi of');
  9597. Add(' 1: begin vi:=2; VI:=3; end;');
  9598. Add(' end;');
  9599. ConvertProgram;
  9600. CheckSource('TestCaseOfNoElse',
  9601. LinesToStr([ // statements
  9602. 'this.Vi = 0;'
  9603. ]),
  9604. LinesToStr([ // $mod.$main
  9605. 'var $tmp = $mod.Vi;',
  9606. 'if ($tmp === 1) {',
  9607. ' $mod.Vi = 2;',
  9608. ' $mod.Vi = 3;',
  9609. '};'
  9610. ]));
  9611. end;
  9612. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9613. begin
  9614. StartProgram(false);
  9615. Converter.UseSwitchStatement:=true;
  9616. Add('var vI: longint;');
  9617. Add('begin');
  9618. Add(' case vi of');
  9619. Add(' 1: begin VI:=2; vi:=3; end;');
  9620. Add(' end;');
  9621. ConvertProgram;
  9622. CheckSource('TestCaseOfNoElse_UseSwitch',
  9623. LinesToStr([ // statements
  9624. 'this.vI = 0;'
  9625. ]),
  9626. LinesToStr([ // $mod.$main
  9627. 'switch ($mod.vI) {',
  9628. 'case 1:',
  9629. ' $mod.vI = 2;',
  9630. ' $mod.vI = 3;',
  9631. ' break;',
  9632. '};'
  9633. ]));
  9634. end;
  9635. procedure TTestModule.TestCaseOfRange;
  9636. begin
  9637. StartProgram(false);
  9638. Add('var vI: longint;');
  9639. Add('begin');
  9640. Add(' case vi of');
  9641. Add(' 1..3: vi:=14;');
  9642. Add(' 4,5: vi:=16;');
  9643. Add(' 6..7,9..10: ;');
  9644. Add(' else ;');
  9645. Add(' end;');
  9646. ConvertProgram;
  9647. CheckSource('TestCaseOfRange',
  9648. LinesToStr([ // statements
  9649. 'this.vI = 0;'
  9650. ]),
  9651. LinesToStr([ // $mod.$main
  9652. 'var $tmp = $mod.vI;',
  9653. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9654. ' $mod.vI = 14',
  9655. '} else if (($tmp === 4) || ($tmp === 5)){',
  9656. ' $mod.vI = 16',
  9657. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9658. ]));
  9659. end;
  9660. procedure TTestModule.TestCaseOfString;
  9661. begin
  9662. StartProgram(false);
  9663. Add([
  9664. 'var s,h: string;',
  9665. 'begin',
  9666. ' case s of',
  9667. ' ''foo'': s:=h;',
  9668. ' ''a''..''z'': h:=s;',
  9669. ' ''ў'', ''ё'': ;',
  9670. ' ''Б''..''Я'': ;',
  9671. ' end;',
  9672. '']);
  9673. ConvertProgram;
  9674. CheckSource('TestCaseOfString',
  9675. LinesToStr([ // statements
  9676. 'this.s = "";',
  9677. 'this.h = "";',
  9678. '']),
  9679. LinesToStr([ // $mod.$main
  9680. 'var $tmp = $mod.s;',
  9681. 'if ($tmp === "foo") {',
  9682. ' $mod.s = $mod.h}',
  9683. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9684. ' $mod.h = $mod.s}',
  9685. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9686. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9687. '']));
  9688. end;
  9689. procedure TTestModule.TestCaseOfChar;
  9690. begin
  9691. StartProgram(false);
  9692. Add([
  9693. 'var s,h: char;',
  9694. 'begin',
  9695. ' case s of',
  9696. ' ''a''..''z'': h:=s;',
  9697. ' ''ä'': ;',
  9698. ' ''ў'', ''ё'': ;',
  9699. ' ''Б''..''Я'': ;',
  9700. ' end;',
  9701. '']);
  9702. ConvertProgram;
  9703. CheckSource('TestCaseOfString',
  9704. LinesToStr([ // statements
  9705. 'this.s = "\x00";',
  9706. 'this.h = "\x00";',
  9707. '']),
  9708. LinesToStr([ // $mod.$main
  9709. 'var $tmp = $mod.s;',
  9710. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9711. ' $mod.h = $mod.s}',
  9712. ' else if ($tmp === "ä") {}',
  9713. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9714. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9715. '']));
  9716. end;
  9717. procedure TTestModule.TestCaseOfExternalClassConst;
  9718. begin
  9719. StartProgram(false);
  9720. Add([
  9721. '{$modeswitch externalclass}',
  9722. 'type',
  9723. ' TBird = class external name ''Bird''',
  9724. ' const e: longint;',
  9725. ' end;',
  9726. 'var vI: longint;',
  9727. 'begin',
  9728. ' case vi of',
  9729. ' 1: vi:=3;',
  9730. ' TBird.e: ;',
  9731. ' end;']);
  9732. ConvertProgram;
  9733. CheckSource('TestCaseOfExternalClassConst',
  9734. LinesToStr([ // statements
  9735. 'this.vI = 0;'
  9736. ]),
  9737. LinesToStr([ // $mod.$main
  9738. 'var $tmp = $mod.vI;',
  9739. 'if ($tmp === 1) {',
  9740. ' $mod.vI = 3}',
  9741. ' else if ($tmp === Bird.e) ;'
  9742. ]));
  9743. end;
  9744. procedure TTestModule.TestDebugger;
  9745. begin
  9746. StartProgram(false);
  9747. Add([
  9748. 'procedure DoIt;',
  9749. 'begin',
  9750. ' deBugger;',
  9751. ' DeBugger();',
  9752. 'end;',
  9753. 'begin',
  9754. ' Debugger;']);
  9755. ConvertProgram;
  9756. CheckSource('TestDebugger',
  9757. LinesToStr([ // statements
  9758. 'this.DoIt = function () {',
  9759. ' debugger;',
  9760. ' debugger;',
  9761. '};',
  9762. '']),
  9763. LinesToStr([ // $mod.$main
  9764. 'debugger;',
  9765. '']));
  9766. end;
  9767. procedure TTestModule.TestArray_Dynamic;
  9768. begin
  9769. StartProgram(false);
  9770. Add([
  9771. 'type',
  9772. ' TArrayInt = array of longint;',
  9773. 'var',
  9774. ' Arr: TArrayInt;',
  9775. ' i: longint;',
  9776. ' b: boolean;',
  9777. 'begin',
  9778. ' SetLength(arr,3);',
  9779. ' arr[0]:=4;',
  9780. ' arr[1]:=length(arr)+arr[0];',
  9781. ' arr[i]:=5;',
  9782. ' arr[arr[i]]:=arr[6];',
  9783. ' i:=low(arr);',
  9784. ' i:=high(arr);',
  9785. ' b:=Assigned(arr);',
  9786. ' Arr:=default(TArrayInt);']);
  9787. ConvertProgram;
  9788. CheckSource('TestArray_Dynamic',
  9789. LinesToStr([ // statements
  9790. 'this.Arr = [];',
  9791. 'this.i = 0;',
  9792. 'this.b = false;'
  9793. ]),
  9794. LinesToStr([ // $mod.$main
  9795. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9796. '$mod.Arr[0] = 4;',
  9797. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9798. '$mod.Arr[$mod.i] = 5;',
  9799. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9800. '$mod.i = 0;',
  9801. '$mod.i = rtl.length($mod.Arr) - 1;',
  9802. '$mod.b = rtl.length($mod.Arr) > 0;',
  9803. '$mod.Arr = [];',
  9804. '']));
  9805. end;
  9806. procedure TTestModule.TestArray_Dynamic_Nil;
  9807. begin
  9808. StartProgram(false);
  9809. Add('type');
  9810. Add(' TArrayInt = array of longint;');
  9811. Add('var');
  9812. Add(' Arr: TArrayInt;');
  9813. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9814. Add('begin');
  9815. Add(' arr:=nil;');
  9816. Add(' if arr=nil then;');
  9817. Add(' if nil=arr then;');
  9818. Add(' if arr<>nil then;');
  9819. Add(' if nil<>arr then;');
  9820. Add(' DoIt(nil,nil);');
  9821. ConvertProgram;
  9822. CheckSource('TestArray_Dynamic',
  9823. LinesToStr([ // statements
  9824. 'this.Arr = [];',
  9825. 'this.DoIt = function(i,j){',
  9826. '};'
  9827. ]),
  9828. LinesToStr([ // $mod.$main
  9829. '$mod.Arr = [];',
  9830. 'if (rtl.length($mod.Arr) === 0) ;',
  9831. 'if (rtl.length($mod.Arr) === 0) ;',
  9832. 'if (rtl.length($mod.Arr) > 0) ;',
  9833. 'if (rtl.length($mod.Arr) > 0) ;',
  9834. '$mod.DoIt([],[]);',
  9835. '']));
  9836. end;
  9837. procedure TTestModule.TestArray_DynMultiDimensional;
  9838. begin
  9839. StartProgram(false);
  9840. Add([
  9841. 'type',
  9842. ' TArrayInt = array of longint;',
  9843. ' TArrayArrayInt = array of TArrayInt;',
  9844. 'var',
  9845. ' Arr: TArrayInt;',
  9846. ' Arr2: TArrayArrayInt;',
  9847. ' i: longint;',
  9848. 'begin',
  9849. ' arr2:=nil;',
  9850. ' if arr2=nil then;',
  9851. ' if nil=arr2 then;',
  9852. ' i:=low(arr2);',
  9853. ' i:=low(arr2[1]);',
  9854. ' i:=high(arr2);',
  9855. ' i:=high(arr2[2]);',
  9856. ' arr2[3]:=arr;',
  9857. ' arr2[4][5]:=i;',
  9858. ' i:=arr2[6][7];',
  9859. ' arr2[8,9]:=i;',
  9860. ' i:=arr2[10,11];',
  9861. ' SetLength(arr2,14);',
  9862. ' SetLength(arr2[15],16);']);
  9863. ConvertProgram;
  9864. CheckSource('TestArray_Dynamic',
  9865. LinesToStr([ // statements
  9866. 'this.Arr = [];',
  9867. 'this.Arr2 = [];',
  9868. 'this.i = 0;'
  9869. ]),
  9870. LinesToStr([ // $mod.$main
  9871. '$mod.Arr2 = [];',
  9872. 'if (rtl.length($mod.Arr2) === 0) ;',
  9873. 'if (rtl.length($mod.Arr2) === 0) ;',
  9874. '$mod.i = 0;',
  9875. '$mod.i = 0;',
  9876. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9877. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9878. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9879. '$mod.Arr2[4][5] = $mod.i;',
  9880. '$mod.i = $mod.Arr2[6][7];',
  9881. '$mod.Arr2[8][9] = $mod.i;',
  9882. '$mod.i = $mod.Arr2[10][11];',
  9883. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9884. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9885. '']));
  9886. end;
  9887. procedure TTestModule.TestArray_DynamicAssign;
  9888. begin
  9889. StartProgram(false);
  9890. Add([
  9891. 'type',
  9892. ' TArrayInt = array of longint;',
  9893. ' TArrayArrayInt = array of TArrayInt;',
  9894. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9895. 'begin',
  9896. 'end;',
  9897. 'procedure Fly(var a: TArrayInt);',
  9898. 'begin',
  9899. 'end;',
  9900. 'var',
  9901. ' Arr: TArrayInt;',
  9902. ' Arr2: TArrayArrayInt;',
  9903. 'begin',
  9904. ' arr:=nil;',
  9905. ' arr2:=nil;',
  9906. ' arr2[1]:=nil;',
  9907. ' arr2[2]:=arr;',
  9908. ' Run(arr,arr,arr);',
  9909. ' Fly(arr);',
  9910. ' Run(arr2[4],arr2[5],arr2[6]);',
  9911. ' Fly(arr2[7]);',
  9912. '']);
  9913. ConvertProgram;
  9914. CheckSource('TestArray_DynamicAssign',
  9915. LinesToStr([ // statements
  9916. 'this.Run = function (a, b, c) {',
  9917. '};',
  9918. 'this.Fly = function (a) {',
  9919. '};',
  9920. 'this.Arr = [];',
  9921. 'this.Arr2 = [];',
  9922. '']),
  9923. LinesToStr([ // $mod.$main
  9924. '$mod.Arr = [];',
  9925. '$mod.Arr2 = [];',
  9926. '$mod.Arr2[1] = [];',
  9927. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9928. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9929. '$mod.Fly({',
  9930. ' p: $mod,',
  9931. ' get: function () {',
  9932. ' return this.p.Arr;',
  9933. ' },',
  9934. ' set: function (v) {',
  9935. ' this.p.Arr = v;',
  9936. ' }',
  9937. '});',
  9938. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9939. '$mod.Fly({',
  9940. ' a: 7,',
  9941. ' p: $mod.Arr2,',
  9942. ' get: function () {',
  9943. ' return this.p[this.a];',
  9944. ' },',
  9945. ' set: function (v) {',
  9946. ' this.p[this.a] = v;',
  9947. ' }',
  9948. '});',
  9949. '']));
  9950. end;
  9951. procedure TTestModule.TestArray_StaticInt;
  9952. begin
  9953. StartProgram(false);
  9954. Add('type');
  9955. Add(' TArrayInt = array[2..4] of longint;');
  9956. Add('var');
  9957. Add(' Arr: TArrayInt;');
  9958. Add(' Arr2: TArrayInt = (5,6,7);');
  9959. Add(' i: longint;');
  9960. Add(' b: boolean;');
  9961. Add('begin');
  9962. Add(' arr[2]:=4;');
  9963. Add(' arr[3]:=arr[2]+arr[3];');
  9964. Add(' arr[i]:=5;');
  9965. Add(' arr[arr[i]]:=arr[high(arr)];');
  9966. Add(' i:=low(arr);');
  9967. Add(' i:=high(arr);');
  9968. Add(' b:=arr[2]=arr[3];');
  9969. Add(' arr:=default(TArrayInt);');
  9970. ConvertProgram;
  9971. CheckSource('TestArray_StaticInt',
  9972. LinesToStr([ // statements
  9973. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9974. 'this.Arr2 = [5, 6, 7];',
  9975. 'this.i = 0;',
  9976. 'this.b = false;'
  9977. ]),
  9978. LinesToStr([ // $mod.$main
  9979. '$mod.Arr[0] = 4;',
  9980. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9981. '$mod.Arr[$mod.i-2] = 5;',
  9982. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9983. '$mod.i = 2;',
  9984. '$mod.i = 4;',
  9985. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9986. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9987. '']));
  9988. end;
  9989. procedure TTestModule.TestArray_StaticBool;
  9990. begin
  9991. StartProgram(false);
  9992. Add('type');
  9993. Add(' TBools = array[boolean] of boolean;');
  9994. Add(' TBool2 = array[true..true] of boolean;');
  9995. Add('var');
  9996. Add(' Arr: TBools;');
  9997. Add(' Arr2: TBool2;');
  9998. Add(' Arr3: TBools = (true,false);');
  9999. Add(' b: boolean;');
  10000. Add('begin');
  10001. Add(' b:=low(arr);');
  10002. Add(' b:=high(arr);');
  10003. Add(' arr[true]:=false;');
  10004. Add(' arr[false]:=arr[b] or arr[true];');
  10005. Add(' arr[b]:=true;');
  10006. Add(' arr[arr[b]]:=arr[high(arr)];');
  10007. Add(' b:=arr[false]=arr[true];');
  10008. Add(' b:=low(arr2);');
  10009. Add(' b:=high(arr2);');
  10010. Add(' arr2[true]:=true;');
  10011. Add(' arr2[true]:=arr2[true] and arr2[b];');
  10012. Add(' arr2[b]:=false;');
  10013. ConvertProgram;
  10014. CheckSource('TestArray_StaticBool',
  10015. LinesToStr([ // statements
  10016. 'this.Arr = rtl.arraySetLength(null,false,2);',
  10017. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  10018. 'this.Arr3 = [true, false];',
  10019. 'this.b = false;'
  10020. ]),
  10021. LinesToStr([ // $mod.$main
  10022. '$mod.b = false;',
  10023. '$mod.b = true;',
  10024. '$mod.Arr[1] = false;',
  10025. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  10026. '$mod.Arr[+$mod.b] = true;',
  10027. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  10028. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  10029. '$mod.b = true;',
  10030. '$mod.b = true;',
  10031. '$mod.Arr2[0] = true;',
  10032. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  10033. '$mod.Arr2[1-$mod.b] = false;',
  10034. '']));
  10035. end;
  10036. procedure TTestModule.TestArray_StaticChar;
  10037. begin
  10038. StartProgram(false);
  10039. Add([
  10040. 'type',
  10041. ' TChars = array[char] of char;',
  10042. ' TChars2 = array[''a''..''z''] of char;',
  10043. 'var',
  10044. ' Arr: TChars;',
  10045. ' Arr2: TChars2;',
  10046. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10047. ' Arr4: array[11..13] of char = ''pas'';',
  10048. ' Arr5: array[21..22] of char = ''äö'';',
  10049. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10050. ' c: char;',
  10051. ' b: boolean;',
  10052. 'begin',
  10053. ' c:=low(arr);',
  10054. ' c:=high(arr);',
  10055. ' arr[''B'']:=''a'';',
  10056. ' arr[''D'']:=arr[c];',
  10057. ' arr[c]:=arr[''d''];',
  10058. ' arr[arr[c]]:=arr[high(arr)];',
  10059. ' b:=arr[low(arr)]=arr[''e''];',
  10060. ' c:=low(arr2);',
  10061. ' c:=high(arr2);',
  10062. ' arr2[''b'']:=''f'';',
  10063. ' arr2[''a'']:=arr2[c];',
  10064. ' arr2[c]:=arr2[''g''];']);
  10065. ConvertProgram;
  10066. CheckSource('TestArray_StaticChar',
  10067. LinesToStr([ // statements
  10068. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10069. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10070. 'this.Arr3 = ["p", "a", "s"];',
  10071. 'this.Arr4 = ["p", "a", "s"];',
  10072. 'this.Arr5 = ["ä", "ö"];',
  10073. 'this.Arr6 = ["ä", "ö"];',
  10074. 'this.c = "\x00";',
  10075. 'this.b = false;',
  10076. '']),
  10077. LinesToStr([ // $mod.$main
  10078. '$mod.c = "\x00";',
  10079. '$mod.c = "\uFFFF";',
  10080. '$mod.Arr[66] = "a";',
  10081. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10082. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10083. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10084. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10085. '$mod.c = "a";',
  10086. '$mod.c = "z";',
  10087. '$mod.Arr2[1] = "f";',
  10088. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10089. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10090. '']));
  10091. end;
  10092. procedure TTestModule.TestArray_StaticMultiDim;
  10093. begin
  10094. StartProgram(false);
  10095. Add([
  10096. 'type',
  10097. ' TArrayInt = array[1..3] of longint;',
  10098. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10099. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10100. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10101. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10102. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10103. 'var',
  10104. ' Arr: TArrayInt;',
  10105. ' Arr2: TArrayArrayInt;',
  10106. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10107. ' Arr4: TArrayArrayInt;',
  10108. ' ArrDim2: TArrayDim2Int;',
  10109. ' ArrDim3: TArrayDim3Int;',
  10110. ' ArrDim4: TArrayDim4Int;',
  10111. ' i: longint;',
  10112. 'begin',
  10113. ' i:=low(arr);',
  10114. ' i:=low(arr2);',
  10115. ' i:=low(arr2[5]);',
  10116. ' i:=high(arr);',
  10117. ' i:=high(arr2);',
  10118. ' i:=high(arr2[6]);',
  10119. ' arr2[5]:=arr;',
  10120. ' arr2[6][2]:=i;',
  10121. ' i:=arr2[6][3];',
  10122. ' arr2[6,3]:=i;',
  10123. ' i:=arr2[5,2];',
  10124. ' arr2:=arr2;',// clone multi dim static array
  10125. ' arr3:=arr3;',// clone anonymous multi dim static array
  10126. ' arr4:=arr4;',
  10127. ' Arr:=Arr;',
  10128. ' ArrDim2:=ArrDim2;',
  10129. ' ArrDim3:=ArrDim3;',
  10130. ' ArrDim4:=ArrDim4;',
  10131. '']);
  10132. ConvertProgram;
  10133. CheckSource('TestArray_StaticMultiDim',
  10134. LinesToStr([ // statements
  10135. 'this.TArrayArrayInt$clone = function (a) {',
  10136. ' var b = [];',
  10137. ' b.length = 2;',
  10138. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10139. ' return b;',
  10140. '};',
  10141. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10142. ' var b = [];',
  10143. ' b.length = 2;',
  10144. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10145. ' return b;',
  10146. '};',
  10147. 'this.TArrayDim2Int$clone = function (a) {',
  10148. ' var b = [];',
  10149. ' b.length = 2;',
  10150. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10151. ' return b;',
  10152. '};',
  10153. 'this.TArrayDim3Int$clone = function (a) {',
  10154. ' var b = [];',
  10155. ' b.length = 2;',
  10156. ' for (var c = 0; c < 2; c++) {',
  10157. ' var d = b[c] = [];',
  10158. ' d.length = 3;',
  10159. ' var e = a[c];',
  10160. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10161. ' };',
  10162. ' return b;',
  10163. '};',
  10164. 'this.TArrayDim4Int$clone = function (a) {',
  10165. ' var b = [];',
  10166. ' b.length = 2;',
  10167. ' for (var c = 0; c < 2; c++) {',
  10168. ' var d = b[c] = [];',
  10169. ' d.length = 3;',
  10170. ' var e = a[c];',
  10171. ' for (var f = 0; f < 3; f++) {',
  10172. ' var g = d[f] = [];',
  10173. ' g.length = 4;',
  10174. ' var h = e[f];',
  10175. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10176. ' };',
  10177. ' };',
  10178. ' return b;',
  10179. '};',
  10180. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10181. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10182. 'this.Arr3$a$clone = function (a) {',
  10183. ' var b = [];',
  10184. ' b.length = 2;',
  10185. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10186. ' return b;',
  10187. '};',
  10188. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10189. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10190. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10191. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10192. 'this.ArrDim4 = rtl.arraySetLength(',
  10193. ' null,',
  10194. ' 0,',
  10195. ' 2,',
  10196. ' 3,',
  10197. ' 4,',
  10198. ' 5',
  10199. ');',
  10200. 'this.i = 0;'
  10201. ]),
  10202. LinesToStr([ // $mod.$main
  10203. '$mod.i = 1;',
  10204. '$mod.i = 5;',
  10205. '$mod.i = 1;',
  10206. '$mod.i = 3;',
  10207. '$mod.i = 6;',
  10208. '$mod.i = 3;',
  10209. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10210. '$mod.Arr2[1][1] = $mod.i;',
  10211. '$mod.i = $mod.Arr2[1][2];',
  10212. '$mod.Arr2[1][2] = $mod.i;',
  10213. '$mod.i = $mod.Arr2[0][1];',
  10214. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10215. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10216. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10217. '$mod.Arr = $mod.Arr.slice(0);',
  10218. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10219. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10220. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10221. '']));
  10222. end;
  10223. procedure TTestModule.TestArray_StaticInFunction;
  10224. begin
  10225. StartProgram(false);
  10226. Add([
  10227. 'const TArrayInt = 3;',
  10228. 'const TArrayArrayInt = 4;',
  10229. 'procedure DoIt;',
  10230. 'type',
  10231. ' TArrayInt = array[1..3] of longint;',
  10232. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10233. 'var',
  10234. ' Arr: TArrayInt;',
  10235. ' Arr2: TArrayArrayInt;',
  10236. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10237. ' i: longint;',
  10238. 'begin',
  10239. ' arr2[5]:=arr;',
  10240. ' arr2:=arr2;',// clone multi dim static array
  10241. ' arr3:=arr3;',// clone multi dim anonymous static array
  10242. 'end;',
  10243. 'begin',
  10244. '']);
  10245. ConvertProgram;
  10246. CheckSource('TestArray_StaticInFunction',
  10247. LinesToStr([ // statements
  10248. 'this.TArrayInt = 3;',
  10249. 'this.TArrayArrayInt = 4;',
  10250. 'var TArrayArrayInt$1$clone = function (a) {',
  10251. ' var b = [];',
  10252. ' b.length = 2;',
  10253. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10254. ' return b;',
  10255. '};',
  10256. 'var Arr3$a$clone = function (a) {',
  10257. ' var b = [];',
  10258. ' b.length = 2;',
  10259. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10260. ' return b;',
  10261. '};',
  10262. 'this.DoIt = function () {',
  10263. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10264. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10265. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10266. ' var i = 0;',
  10267. ' Arr2[0] = Arr.slice(0);',
  10268. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10269. ' Arr3 = Arr3$a$clone(Arr3);',
  10270. '};',
  10271. '']),
  10272. LinesToStr([ // $mod.$main
  10273. '']));
  10274. end;
  10275. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10276. begin
  10277. StartProgram(false);
  10278. Add([
  10279. 'type',
  10280. ' TArrayInt = array[1..3,1..2] of longint;',
  10281. 'var',
  10282. ' a,b: TArrayInt;',
  10283. 'begin',
  10284. ' if a=b then ;',
  10285. '']);
  10286. SetExpectedPasResolverError('compare static array is not supported',
  10287. nXIsNotSupported);
  10288. ConvertProgram;
  10289. end;
  10290. procedure TTestModule.TestArrayOfRecord;
  10291. begin
  10292. StartProgram(false);
  10293. Add([
  10294. 'type',
  10295. ' TRec = record',
  10296. ' Int: longint;',
  10297. ' end;',
  10298. ' TArrayRec = array of TRec;',
  10299. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10300. 'begin',
  10301. 'end;',
  10302. 'var',
  10303. ' Arr: TArrayRec;',
  10304. ' r: TRec;',
  10305. ' i: longint;',
  10306. 'begin',
  10307. ' SetLength(arr,3);',
  10308. ' arr[0].int:=4;',
  10309. ' arr[1].int:=length(arr)+arr[2].int;',
  10310. ' arr[arr[i].int].int:=arr[5].int;',
  10311. ' arr[7]:=r;',
  10312. ' r:=arr[8];',
  10313. ' i:=low(arr);',
  10314. ' i:=high(arr);',
  10315. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10316. ConvertProgram;
  10317. CheckSource('TestArrayOfRecord',
  10318. LinesToStr([ // statements
  10319. 'rtl.recNewT(this, "TRec", function () {',
  10320. ' this.Int = 0;',
  10321. ' this.$eq = function (b) {',
  10322. ' return this.Int === b.Int;',
  10323. ' };',
  10324. ' this.$assign = function (s) {',
  10325. ' this.Int = s.Int;',
  10326. ' return this;',
  10327. ' };',
  10328. '});',
  10329. 'this.DoIt = function (vd, vc, vv) {',
  10330. '};',
  10331. 'this.Arr = [];',
  10332. 'this.r = this.TRec.$new();',
  10333. 'this.i = 0;'
  10334. ]),
  10335. LinesToStr([ // $mod.$main
  10336. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10337. '$mod.Arr[0].Int = 4;',
  10338. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10339. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10340. '$mod.Arr[7].$assign($mod.r);',
  10341. '$mod.r.$assign($mod.Arr[8]);',
  10342. '$mod.i = 0;',
  10343. '$mod.i = rtl.length($mod.Arr)-1;',
  10344. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10345. '']));
  10346. end;
  10347. procedure TTestModule.TestArray_StaticRecord;
  10348. begin
  10349. StartProgram(false);
  10350. Add([
  10351. 'type',
  10352. ' TRec = record',
  10353. ' Int: longint;',
  10354. ' end;',
  10355. ' TArrayRec = array[1..2] of TRec;',
  10356. 'var',
  10357. ' Arr: TArrayRec;',
  10358. 'begin',
  10359. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10360. '']);
  10361. ConvertProgram;
  10362. CheckSource('TestArray_StaticRecord',
  10363. LinesToStr([ // statements
  10364. 'rtl.recNewT(this, "TRec", function () {',
  10365. ' this.Int = 0;',
  10366. ' this.$eq = function (b) {',
  10367. ' return this.Int === b.Int;',
  10368. ' };',
  10369. ' this.$assign = function (s) {',
  10370. ' this.Int = s.Int;',
  10371. ' return this;',
  10372. ' };',
  10373. '});',
  10374. 'this.TArrayRec$clone = function (a) {',
  10375. ' var b = [];',
  10376. ' b.length = 2;',
  10377. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10378. ' return b;',
  10379. '};',
  10380. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10381. '']),
  10382. LinesToStr([ // $mod.$main
  10383. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10384. end;
  10385. procedure TTestModule.TestArrayOfSet;
  10386. begin
  10387. StartProgram(false);
  10388. Add([
  10389. 'type',
  10390. ' TFlag = (big,small);',
  10391. ' TSetOfFlag = set of tflag;',
  10392. ' TArrayFlag = array of TSetOfFlag;',
  10393. 'procedure DoIt(const a: Tarrayflag);',
  10394. 'begin',
  10395. 'end;',
  10396. 'var',
  10397. ' f: TFlag;',
  10398. ' s: TSetOfFlag;',
  10399. ' Arr: TArrayFlag;',
  10400. ' i: longint;',
  10401. 'begin',
  10402. ' SetLength(arr,3);',
  10403. ' arr[0]:=s;',
  10404. ' arr[1]:=[big];',
  10405. ' arr[2]:=[big]+s;',
  10406. ' arr[3]:=s+[big];',
  10407. ' arr[4]:=arr[5];',
  10408. ' s:=arr[6];',
  10409. ' i:=low(arr);',
  10410. ' i:=high(arr);',
  10411. ' DoIt(arr);',
  10412. ' DoIt([s]);',
  10413. ' DoIt([[],s]);',
  10414. ' DoIt([s,[]]);',
  10415. '']);
  10416. ConvertProgram;
  10417. CheckSource('TestArrayOfSet',
  10418. LinesToStr([ // statements
  10419. 'this.TFlag = {',
  10420. ' "0": "big",',
  10421. ' big: 0,',
  10422. ' "1": "small",',
  10423. ' small: 1',
  10424. '};',
  10425. 'this.DoIt = function (a) {',
  10426. '};',
  10427. 'this.f = 0;',
  10428. 'this.s = {};',
  10429. 'this.Arr = [];',
  10430. 'this.i = 0;',
  10431. '']),
  10432. LinesToStr([ // $mod.$main
  10433. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10434. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10435. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10436. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10437. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10438. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10439. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10440. '$mod.i = 0;',
  10441. '$mod.i = rtl.length($mod.Arr) - 1;',
  10442. '$mod.DoIt($mod.Arr);',
  10443. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10444. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10445. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10446. '']));
  10447. end;
  10448. procedure TTestModule.TestArray_DynAsParam;
  10449. begin
  10450. StartProgram(false);
  10451. Add([
  10452. 'type integer = longint;',
  10453. 'type TArrInt = array of integer;',
  10454. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10455. 'var vJ: TArrInt;',
  10456. 'begin',
  10457. ' vg:=vg;',
  10458. ' vj:=vh;',
  10459. ' vi:=vi;',
  10460. ' doit(vg,vg,vg);',
  10461. ' doit(vh,vh,vj);',
  10462. ' doit(vi,vi,vi);',
  10463. ' doit(vj,vj,vj);',
  10464. 'end;',
  10465. 'var i: TArrInt;',
  10466. 'begin',
  10467. ' doit(i,i,i);']);
  10468. ConvertProgram;
  10469. CheckSource('TestArray_DynAsParams',
  10470. LinesToStr([ // statements
  10471. 'this.DoIt = function (vG,vH,vI) {',
  10472. ' var vJ = [];',
  10473. ' vG = rtl.arrayRef(vG);',
  10474. ' vJ = rtl.arrayRef(vH);',
  10475. ' vI.set(rtl.arrayRef(vI.get()));',
  10476. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10477. ' get: function () {',
  10478. ' return vG;',
  10479. ' },',
  10480. ' set: function (v) {',
  10481. ' vG = v;',
  10482. ' }',
  10483. ' });',
  10484. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10485. ' get: function () {',
  10486. ' return vJ;',
  10487. ' },',
  10488. ' set: function (v) {',
  10489. ' vJ = v;',
  10490. ' }',
  10491. ' });',
  10492. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10493. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10494. ' get: function () {',
  10495. ' return vJ;',
  10496. ' },',
  10497. ' set: function (v) {',
  10498. ' vJ = v;',
  10499. ' }',
  10500. ' });',
  10501. '};',
  10502. 'this.i = [];'
  10503. ]),
  10504. LinesToStr([
  10505. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10506. ' p: $mod,',
  10507. ' get: function () {',
  10508. ' return this.p.i;',
  10509. ' },',
  10510. ' set: function (v) {',
  10511. ' this.p.i = v;',
  10512. ' }',
  10513. '});'
  10514. ]));
  10515. end;
  10516. procedure TTestModule.TestArray_StaticAsParam;
  10517. begin
  10518. StartProgram(false);
  10519. Add([
  10520. 'type integer = longint;',
  10521. 'type TArrInt = array[1..2] of integer;',
  10522. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10523. 'var vJ: TArrInt;',
  10524. 'begin',
  10525. ' vg:=vg;',
  10526. ' vj:=vh;',
  10527. ' vi:=vi;',
  10528. ' doit(vg,vg,vg);',
  10529. ' doit(vh,vh,vj);',
  10530. ' doit(vi,vi,vi);',
  10531. ' doit(vj,vj,vj);',
  10532. 'end;',
  10533. 'var i: TArrInt;',
  10534. 'begin',
  10535. ' doit(i,i,i);']);
  10536. ConvertProgram;
  10537. CheckSource('TestArray_StaticAsParams',
  10538. LinesToStr([ // statements
  10539. 'this.DoIt = function (vG,vH,vI) {',
  10540. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10541. ' vG = vG.slice(0);',
  10542. ' vJ = vH.slice(0);',
  10543. ' vI.set(vI.get().slice(0));',
  10544. ' $mod.DoIt(vG.slice(0), vG, {',
  10545. ' get: function () {',
  10546. ' return vG;',
  10547. ' },',
  10548. ' set: function (v) {',
  10549. ' vG = v;',
  10550. ' }',
  10551. ' });',
  10552. ' $mod.DoIt(vH.slice(0), vH, {',
  10553. ' get: function () {',
  10554. ' return vJ;',
  10555. ' },',
  10556. ' set: function (v) {',
  10557. ' vJ = v;',
  10558. ' }',
  10559. ' });',
  10560. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10561. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10562. ' get: function () {',
  10563. ' return vJ;',
  10564. ' },',
  10565. ' set: function (v) {',
  10566. ' vJ = v;',
  10567. ' }',
  10568. ' });',
  10569. '};',
  10570. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10571. ]),
  10572. LinesToStr([
  10573. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10574. ' p: $mod,',
  10575. ' get: function () {',
  10576. ' return this.p.i;',
  10577. ' },',
  10578. ' set: function (v) {',
  10579. ' this.p.i = v;',
  10580. ' }',
  10581. '});'
  10582. ]));
  10583. end;
  10584. procedure TTestModule.TestArrayElement_AsParams;
  10585. begin
  10586. StartProgram(false);
  10587. Add('type integer = longint;');
  10588. Add('type TArrayInt = array of integer;');
  10589. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10590. Add('var vJ: tarrayint;');
  10591. Add('begin');
  10592. Add(' vi:=vi;');
  10593. Add(' doit(vi,vi,vi);');
  10594. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10595. Add('end;');
  10596. Add('var a: TArrayInt;');
  10597. Add('begin');
  10598. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10599. ConvertProgram;
  10600. CheckSource('TestArrayElement_AsParams',
  10601. LinesToStr([ // statements
  10602. 'this.DoIt = function (vG,vH,vI) {',
  10603. ' var vJ = [];',
  10604. ' vI.set(vI.get());',
  10605. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10606. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10607. ' a:1+3,',
  10608. ' p:vJ,',
  10609. ' get: function () {',
  10610. ' return this.p[this.a];',
  10611. ' },',
  10612. ' set: function (v) {',
  10613. ' this.p[this.a] = v;',
  10614. ' }',
  10615. ' });',
  10616. '};',
  10617. 'this.a = [];'
  10618. ]),
  10619. LinesToStr([
  10620. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10621. ' a: 1+6,',
  10622. ' p: $mod.a,',
  10623. ' get: function () {',
  10624. ' return this.p[this.a];',
  10625. ' },',
  10626. ' set: function (v) {',
  10627. ' this.p[this.a] = v;',
  10628. ' }',
  10629. '});'
  10630. ]));
  10631. end;
  10632. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10633. begin
  10634. StartProgram(false);
  10635. Add('type Integer = longint;');
  10636. Add('type TArrayInt = array of integer;');
  10637. Add('function GetArr(vB: integer = 0): tarrayint;');
  10638. Add('begin');
  10639. Add('end;');
  10640. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10641. Add('begin');
  10642. Add('end;');
  10643. Add('begin');
  10644. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10645. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10646. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10647. ConvertProgram;
  10648. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10649. LinesToStr([ // statements
  10650. 'this.GetArr = function (vB) {',
  10651. ' var Result = [];',
  10652. ' return Result;',
  10653. '};',
  10654. 'this.DoIt = function (vG,vH,vI) {',
  10655. '};'
  10656. ]),
  10657. LinesToStr([
  10658. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10659. ' a: 1+3,',
  10660. ' p: $mod.GetArr(0),',
  10661. ' get: function () {',
  10662. ' return this.p[this.a];',
  10663. ' },',
  10664. ' set: function (v) {',
  10665. ' this.p[this.a] = v;',
  10666. ' }',
  10667. '});',
  10668. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10669. ' a: 2+3,',
  10670. ' p: $mod.GetArr(0),',
  10671. ' get: function () {',
  10672. ' return this.p[this.a];',
  10673. ' },',
  10674. ' set: function (v) {',
  10675. ' this.p[this.a] = v;',
  10676. ' }',
  10677. '});',
  10678. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10679. ' a: 3+3,',
  10680. ' p: $mod.GetArr(9),',
  10681. ' get: function () {',
  10682. ' return this.p[this.a];',
  10683. ' },',
  10684. ' set: function (v) {',
  10685. ' this.p[this.a] = v;',
  10686. ' }',
  10687. '});',
  10688. '']));
  10689. end;
  10690. procedure TTestModule.TestArrayEnumTypeRange;
  10691. begin
  10692. StartProgram(false);
  10693. Add([
  10694. 'type',
  10695. ' TEnum = (red,blue);',
  10696. ' TEnumArray = array[TEnum] of longint;',
  10697. 'var',
  10698. ' e: TEnum;',
  10699. ' i: longint;',
  10700. ' a: TEnumArray;',
  10701. ' numbers: TEnumArray = (1,2);',
  10702. ' names: array[TEnum] of string = (''red'',''blue'');',
  10703. 'begin',
  10704. ' e:=low(a);',
  10705. ' e:=high(a);',
  10706. ' i:=a[red];',
  10707. ' a[e]:=a[e];']);
  10708. ConvertProgram;
  10709. CheckSource('TestArrayEnumTypeRange',
  10710. LinesToStr([ // statements
  10711. ' this.TEnum = {',
  10712. ' "0": "red",',
  10713. ' red: 0,',
  10714. ' "1": "blue",',
  10715. ' blue: 1',
  10716. '};',
  10717. 'this.e = 0;',
  10718. 'this.i = 0;',
  10719. 'this.a = rtl.arraySetLength(null,0,2);',
  10720. 'this.numbers = [1, 2];',
  10721. 'this.names = ["red", "blue"];',
  10722. '']),
  10723. LinesToStr([ // $mod.$main
  10724. '$mod.e = $mod.TEnum.red;',
  10725. '$mod.e = $mod.TEnum.blue;',
  10726. '$mod.i = $mod.a[$mod.TEnum.red];',
  10727. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10728. '']));
  10729. end;
  10730. procedure TTestModule.TestArray_SetLengthOutArg;
  10731. begin
  10732. StartProgram(false);
  10733. Add([
  10734. 'type TArrInt = array of longint;',
  10735. 'procedure DoIt(out a: TArrInt);',
  10736. 'begin',
  10737. ' SetLength(a,2);',
  10738. 'end;',
  10739. 'begin',
  10740. '']);
  10741. ConvertProgram;
  10742. CheckSource('TestArray_SetLengthOutArg',
  10743. LinesToStr([ // statements
  10744. 'this.DoIt = function (a) {',
  10745. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10746. '};',
  10747. '']),
  10748. LinesToStr([
  10749. '']));
  10750. end;
  10751. procedure TTestModule.TestArray_SetLengthProperty;
  10752. begin
  10753. StartProgram(false);
  10754. Add('type');
  10755. Add(' TArrInt = array of longint;');
  10756. Add(' TObject = class');
  10757. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10758. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10759. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10760. Add(' end;');
  10761. Add('var Obj: TObject;');
  10762. Add('begin');
  10763. Add(' SetLength(Obj.Colors,2);');
  10764. ConvertProgram;
  10765. CheckSource('TestArray_SetLengthProperty',
  10766. LinesToStr([ // statements
  10767. 'rtl.createClass(this, "TObject", null, function () {',
  10768. ' this.$init = function () {',
  10769. ' };',
  10770. ' this.$final = function () {',
  10771. ' };',
  10772. '});',
  10773. 'this.Obj = null;',
  10774. '']),
  10775. LinesToStr([
  10776. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10777. '']));
  10778. end;
  10779. procedure TTestModule.TestArray_SetLengthMultiDim;
  10780. begin
  10781. StartProgram(false);
  10782. Add([
  10783. 'type',
  10784. ' TArrArrInt = array of array of longint;',
  10785. ' TArrStaInt = array of array[1..2] of longint;',
  10786. 'var',
  10787. ' a: TArrArrInt;',
  10788. ' b: TArrStaInt;',
  10789. 'begin',
  10790. ' SetLength(a,2);',
  10791. ' SetLength(a,3,4);',
  10792. ' SetLength(b,5);',
  10793. '']);
  10794. ConvertProgram;
  10795. CheckSource('TestArray_SetLengthMultiDim',
  10796. LinesToStr([ // statements
  10797. 'this.a = [];',
  10798. 'this.b = [];',
  10799. '']),
  10800. LinesToStr([
  10801. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10802. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10803. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10804. '']));
  10805. end;
  10806. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10807. begin
  10808. StartProgram(false);
  10809. Add([
  10810. 'type',
  10811. ' TStaArr1 = array[1..3] of boolean;',
  10812. //' TStaArr2 = array[5..6] of TStaArr1;',
  10813. ' TDynArr1StaArr1 = array of TStaArr1;',
  10814. //' TDynArr1StaArr2 = array of TStaArr2;',
  10815. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10816. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10817. 'var',
  10818. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10819. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10820. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10821. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10822. 'begin',
  10823. ' SetLength(DynArr1StaArr1,11);',
  10824. ' SetLength(DynArr2StaArr1,12);',
  10825. ' SetLength(DynArr2StaArr1[13],14);',
  10826. ' SetLength(DynArr2StaArr1,15,16);',
  10827. //' SetLength(DynArr1StaArr2,21);',
  10828. //' SetLength(DynArr2StaArr2,22);',
  10829. //' SetLength(DynArr2StaArr2[23],24);',
  10830. //' SetLength(DynArr2StaArr2,25,26);',
  10831. '']);
  10832. ConvertProgram;
  10833. CheckSource('TestArray_DynOfStatic',
  10834. LinesToStr([ // statements
  10835. 'this.DynArr1StaArr1 = [];',
  10836. 'this.DynArr2StaArr1 = [];',
  10837. '']),
  10838. LinesToStr([ // $mod.$main
  10839. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10840. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10841. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10842. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10843. ' $mod.DynArr2StaArr1,',
  10844. ' false,',
  10845. ' 15,',
  10846. ' 16,',
  10847. ' "s",',
  10848. ' 3',
  10849. ');',
  10850. '']));
  10851. end;
  10852. procedure TTestModule.TestArray_OpenArrayOfString;
  10853. begin
  10854. StartProgram(false);
  10855. Add('procedure DoIt(const a: array of String);');
  10856. Add('var');
  10857. Add(' i: longint;');
  10858. Add(' s: string;');
  10859. Add('begin');
  10860. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10861. Add('end;');
  10862. Add('var s: string;');
  10863. Add('begin');
  10864. Add(' DoIt([]);');
  10865. Add(' DoIt([s,''foo'','''',s+s]);');
  10866. ConvertProgram;
  10867. CheckSource('TestArray_OpenArrayOfString',
  10868. LinesToStr([ // statements
  10869. 'this.DoIt = function (a) {',
  10870. ' var i = 0;',
  10871. ' var s = "";',
  10872. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10873. ' i = $l;',
  10874. ' s = a[rtl.length(a) - i - 1];',
  10875. ' };',
  10876. '};',
  10877. 'this.s = "";',
  10878. '']),
  10879. LinesToStr([
  10880. '$mod.DoIt([]);',
  10881. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10882. '']));
  10883. end;
  10884. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10885. begin
  10886. StartProgram(false);
  10887. Add([
  10888. 'type TArr = array of char;',
  10889. 'var',
  10890. ' c: char;',
  10891. ' s: string;',
  10892. ' a: TArr;',
  10893. 'procedure Run(const a: array of char);',
  10894. 'begin',
  10895. ' Run(c);',
  10896. ' Run(s);',
  10897. 'end;',
  10898. 'begin',
  10899. ' a:=c;',
  10900. ' a:=s;',
  10901. ' a:=#13;',
  10902. ' a:=''Foo'';',
  10903. ' Run(c);',
  10904. ' Run(s);',
  10905. '']);
  10906. ConvertProgram;
  10907. CheckSource('TestArray_ArrayOfCharAssignString',
  10908. LinesToStr([ // statements
  10909. 'this.c = "\x00";',
  10910. 'this.s = "";',
  10911. 'this.a = [];',
  10912. 'this.Run = function (a) {',
  10913. ' $mod.Run($mod.c.split(""));',
  10914. ' $mod.Run($mod.s.split(""));',
  10915. '};',
  10916. '']),
  10917. LinesToStr([
  10918. '$mod.a = $mod.c.split("");',
  10919. '$mod.a = $mod.s.split("");',
  10920. '$mod.a = "\r".split("");',
  10921. '$mod.a = "Foo".split("");',
  10922. '$mod.Run($mod.c.split(""));',
  10923. '$mod.Run($mod.s.split(""));',
  10924. '']));
  10925. end;
  10926. procedure TTestModule.TestArray_ConstRef;
  10927. begin
  10928. StartProgram(false);
  10929. Add([
  10930. 'type TArr = array of word;',
  10931. 'procedure Run(constref a: TArr);',
  10932. 'begin',
  10933. 'end;',
  10934. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10935. 'var l: TArr;',
  10936. 'begin',
  10937. ' Run(l);',
  10938. ' Run(a);',
  10939. ' Run(b);',
  10940. ' Run(c);',
  10941. ' Run(d);',
  10942. ' Run(e);',
  10943. 'end;',
  10944. 'begin',
  10945. '']);
  10946. ConvertProgram;
  10947. CheckResolverUnexpectedHints();
  10948. CheckSource('TestArray_ConstRef',
  10949. LinesToStr([ // statements
  10950. 'this.Run = function (a) {',
  10951. '};',
  10952. 'this.Fly = function (a, b, c, d, e) {',
  10953. ' var l = [];',
  10954. ' $mod.Run(l);',
  10955. ' $mod.Run(a);',
  10956. ' $mod.Run(b.get());',
  10957. ' $mod.Run(c.get());',
  10958. ' $mod.Run(d);',
  10959. ' $mod.Run(e);',
  10960. '};',
  10961. '']),
  10962. LinesToStr([
  10963. '']));
  10964. end;
  10965. procedure TTestModule.TestArray_Concat;
  10966. begin
  10967. StartProgram(false);
  10968. Add([
  10969. 'type',
  10970. ' integer = longint;',
  10971. ' TFlag = (big,small);',
  10972. ' TFlags = set of TFlag;',
  10973. ' TRec = record',
  10974. ' i: integer;',
  10975. ' end;',
  10976. ' TArrInt = array of integer;',
  10977. ' TArrRec = array of TRec;',
  10978. ' TArrFlag = array of TFlag;',
  10979. ' TArrSet = array of TFlags;',
  10980. ' TArrJSValue = array of jsvalue;',
  10981. 'var',
  10982. ' ArrInt1, ArrInt2: tarrint;',
  10983. ' ArrRec1, ArrRec2: tarrrec;',
  10984. ' ArrFlag1, ArrFlag2: tarrflag;',
  10985. ' ArrSet1, ArrSet2: tarrset;',
  10986. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10987. 'begin',
  10988. ' arrint1:=concat(arrint2);',
  10989. ' arrint1:=concat(arrint2,arrint2);',
  10990. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10991. ' arrrec1:=concat(arrrec2);',
  10992. ' arrrec1:=concat(arrrec2,arrrec2);',
  10993. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10994. ' arrset1:=concat(arrset2);',
  10995. ' arrset1:=concat(arrset2,arrset2);',
  10996. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10997. ' arrjsvalue1:=concat(arrjsvalue2);',
  10998. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10999. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  11000. ' arrint1:=concat([1],arrint2);',
  11001. ' arrflag1:=concat([big]);',
  11002. ' arrflag1:=concat([big],arrflag2);',
  11003. ' arrflag1:=concat(arrflag2,[small]);',
  11004. '']);
  11005. ConvertProgram;
  11006. CheckSource('TestArray_Concat',
  11007. LinesToStr([ // statements
  11008. 'this.TFlag = {',
  11009. ' "0": "big",',
  11010. ' big: 0,',
  11011. ' "1": "small",',
  11012. ' small: 1',
  11013. '};',
  11014. 'rtl.recNewT(this, "TRec", function () {',
  11015. ' this.i = 0;',
  11016. ' this.$eq = function (b) {',
  11017. ' return this.i === b.i;',
  11018. ' };',
  11019. ' this.$assign = function (s) {',
  11020. ' this.i = s.i;',
  11021. ' return this;',
  11022. ' };',
  11023. '});',
  11024. 'this.ArrInt1 = [];',
  11025. 'this.ArrInt2 = [];',
  11026. 'this.ArrRec1 = [];',
  11027. 'this.ArrRec2 = [];',
  11028. 'this.ArrFlag1 = [];',
  11029. 'this.ArrFlag2 = [];',
  11030. 'this.ArrSet1 = [];',
  11031. 'this.ArrSet2 = [];',
  11032. 'this.ArrJSValue1 = [];',
  11033. 'this.ArrJSValue2 = [];',
  11034. '']),
  11035. LinesToStr([ // $mod.$main
  11036. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  11037. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  11038. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  11039. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  11040. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  11041. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  11042. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  11043. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11044. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11045. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11046. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11047. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11048. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11049. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11050. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11051. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11052. '']));
  11053. end;
  11054. procedure TTestModule.TestArray_Concat_Append;
  11055. begin
  11056. StartProgram(false);
  11057. Add([
  11058. 'type',
  11059. ' integer = longint;',
  11060. ' TFlag = (big,small);',
  11061. ' TFlags = set of TFlag;',
  11062. ' TRec = record',
  11063. ' i: integer;',
  11064. ' end;',
  11065. ' TArrInt = array of integer;',
  11066. ' TArrRec = array of TRec;',
  11067. ' TArrFlag = array of TFlag;',
  11068. ' TArrSet = array of TFlags;',
  11069. ' TArrJSValue = array of jsvalue;',
  11070. 'var',
  11071. ' ArrInt: tarrint;',
  11072. ' ArrRec: tarrrec;',
  11073. ' ArrFlag: tarrflag;',
  11074. ' ArrSet: tarrset;',
  11075. ' ArrJSValue: tarrjsvalue;',
  11076. ' r: TRec;',
  11077. ' f: TFlags;',
  11078. 'begin',
  11079. ' // append',
  11080. ' arrint:=concat(arrint);',
  11081. ' arrint:=concat(arrint,[2]);',
  11082. ' arrint:=concat(arrint,[3,4]);',
  11083. ' arrrec:=concat(arrrec);',
  11084. ' arrrec:=concat(arrrec,[r]);',
  11085. ' arrrec:=concat(arrrec,[r,r]);',
  11086. ' arrset:=concat(arrset);',
  11087. ' arrset:=concat(arrset,[f]);',
  11088. ' arrset:=concat(arrset,[f,f]);',
  11089. ' arrjsvalue:=concat(arrjsvalue);',
  11090. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11091. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11092. ' arrflag:=concat(arrflag);',
  11093. ' arrflag:=concat(arrflag,[small]);',
  11094. ' arrflag:=concat(arrflag,[small,big]);',
  11095. '']);
  11096. ConvertProgram;
  11097. CheckSource('TestArray_Concat_Append',
  11098. LinesToStr([ // statements
  11099. 'this.TFlag = {',
  11100. ' "0": "big",',
  11101. ' big: 0,',
  11102. ' "1": "small",',
  11103. ' small: 1',
  11104. '};',
  11105. 'rtl.recNewT(this, "TRec", function () {',
  11106. ' this.i = 0;',
  11107. ' this.$eq = function (b) {',
  11108. ' return this.i === b.i;',
  11109. ' };',
  11110. ' this.$assign = function (s) {',
  11111. ' this.i = s.i;',
  11112. ' return this;',
  11113. ' };',
  11114. '});',
  11115. 'this.ArrInt = [];',
  11116. 'this.ArrRec = [];',
  11117. 'this.ArrFlag = [];',
  11118. 'this.ArrSet = [];',
  11119. 'this.ArrJSValue = [];',
  11120. 'this.r = this.TRec.$new();',
  11121. 'this.f = {};',
  11122. '']),
  11123. LinesToStr([ // $mod.$main
  11124. '$mod.ArrInt = $mod.ArrInt;',
  11125. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11126. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11127. '$mod.ArrRec = $mod.ArrRec;',
  11128. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11129. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11130. '$mod.ArrSet = $mod.ArrSet;',
  11131. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11132. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11133. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11134. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11135. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11136. '$mod.ArrFlag = $mod.ArrFlag;',
  11137. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11138. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11139. '']));
  11140. end;
  11141. procedure TTestModule.TestArray_Concat_Append_Var;
  11142. begin
  11143. StartProgram(false);
  11144. Add([
  11145. 'type',
  11146. ' TArrInt = array of word;',
  11147. '',
  11148. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11149. 'begin',
  11150. ' a:=concat(a,[2]);',
  11151. ' b:=concat(b,[2]);',
  11152. 'end;',
  11153. 'var',
  11154. ' ArrInt: tarrint;',
  11155. 'begin',
  11156. ' Fly(ArrInt,ArrInt);',
  11157. '']);
  11158. ConvertProgram;
  11159. CheckSource('TestArray_Concat_Append_Var',
  11160. LinesToStr([ // statements
  11161. 'this.Fly = function (a, b) {',
  11162. ' a = rtl.arrayPushN(a, 2);',
  11163. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11164. '};',
  11165. 'this.ArrInt = [];',
  11166. '']),
  11167. LinesToStr([ // $mod.$main
  11168. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11169. ' p: $mod,',
  11170. ' get: function () {',
  11171. ' return this.p.ArrInt;',
  11172. ' },',
  11173. ' set: function (v) {',
  11174. ' this.p.ArrInt = v;',
  11175. ' }',
  11176. '});',
  11177. '']));
  11178. end;
  11179. procedure TTestModule.TestArray_Copy;
  11180. begin
  11181. StartProgram(false);
  11182. Add([
  11183. 'type',
  11184. ' integer = longint;',
  11185. ' TFlag = (big,small);',
  11186. ' TFlags = set of TFlag;',
  11187. ' TRec = record',
  11188. ' i: integer;',
  11189. ' end;',
  11190. ' TArrInt = array of integer;',
  11191. ' TArrRec = array of TRec;',
  11192. ' TArrSet = array of TFlags;',
  11193. ' TArrJSValue = array of jsvalue;',
  11194. 'var',
  11195. ' ArrInt: tarrint;',
  11196. ' ArrRec: tarrrec;',
  11197. ' ArrSet: tarrset;',
  11198. ' ArrJSValue: tarrjsvalue;',
  11199. 'begin',
  11200. ' arrint:=copy(arrint);',
  11201. ' arrint:=copy(arrint,2);',
  11202. ' arrint:=copy(arrint,3,4);',
  11203. ' arrint:=copy([1,1],1,2);',
  11204. ' arrrec:=copy(arrrec);',
  11205. ' arrrec:=copy(arrrec,5);',
  11206. ' arrrec:=copy(arrrec,6,7);',
  11207. ' arrset:=copy(arrset);',
  11208. ' arrset:=copy(arrset,8);',
  11209. ' arrset:=copy(arrset,9,10);',
  11210. ' arrjsvalue:=copy(arrjsvalue);',
  11211. ' arrjsvalue:=copy(arrjsvalue,11);',
  11212. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11213. ' ']);
  11214. ConvertProgram;
  11215. CheckSource('TestArray_Copy',
  11216. LinesToStr([ // statements
  11217. 'this.TFlag = {',
  11218. ' "0": "big",',
  11219. ' big: 0,',
  11220. ' "1": "small",',
  11221. ' small: 1',
  11222. '};',
  11223. 'rtl.recNewT(this, "TRec", function () {',
  11224. ' this.i = 0;',
  11225. ' this.$eq = function (b) {',
  11226. ' return this.i === b.i;',
  11227. ' };',
  11228. ' this.$assign = function (s) {',
  11229. ' this.i = s.i;',
  11230. ' return this;',
  11231. ' };',
  11232. '});',
  11233. 'this.ArrInt = [];',
  11234. 'this.ArrRec = [];',
  11235. 'this.ArrSet = [];',
  11236. 'this.ArrJSValue = [];',
  11237. '']),
  11238. LinesToStr([ // $mod.$main
  11239. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11240. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11241. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11242. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11243. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11244. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11245. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11246. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11247. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11248. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11249. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11250. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11251. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11252. '']));
  11253. end;
  11254. procedure TTestModule.TestArray_InsertDelete;
  11255. begin
  11256. StartProgram(false);
  11257. Add([
  11258. 'type',
  11259. ' integer = longint;',
  11260. ' TFlag = (big,small);',
  11261. ' TFlags = set of TFlag;',
  11262. ' TRec = record',
  11263. ' i: integer;',
  11264. ' end;',
  11265. ' TArrInt = array of integer;',
  11266. ' TArrRec = array of TRec;',
  11267. ' TArrSet = array of TFlags;',
  11268. ' TArrJSValue = array of jsvalue;',
  11269. ' TArrArrInt = array of TArrInt;',
  11270. 'var',
  11271. ' ArrInt: tarrint;',
  11272. ' ArrRec: tarrrec;',
  11273. ' ArrSet: tarrset;',
  11274. ' ArrJSValue: tarrjsvalue;',
  11275. ' ArrArrInt: TArrArrInt;',
  11276. 'begin',
  11277. ' Insert(1,arrint,2);',
  11278. ' Insert(arrint[3],arrint,4);',
  11279. ' Insert(arrrec[5],arrrec,6);',
  11280. ' Insert(arrset[7],arrset,7);',
  11281. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11282. ' Insert(10,arrjsvalue,11);',
  11283. ' Insert([23],arrarrint,22);',
  11284. ' Delete(arrint,12,13);',
  11285. ' Delete(arrrec,14,15);',
  11286. ' Delete(arrset,17,18);',
  11287. ' Delete(arrjsvalue,19,10);']);
  11288. ConvertProgram;
  11289. CheckSource('TestArray_InsertDelete',
  11290. LinesToStr([ // statements
  11291. 'this.TFlag = {',
  11292. ' "0": "big",',
  11293. ' big: 0,',
  11294. ' "1": "small",',
  11295. ' small: 1',
  11296. '};',
  11297. 'rtl.recNewT(this, "TRec", function () {',
  11298. ' this.i = 0;',
  11299. ' this.$eq = function (b) {',
  11300. ' return this.i === b.i;',
  11301. ' };',
  11302. ' this.$assign = function (s) {',
  11303. ' this.i = s.i;',
  11304. ' return this;',
  11305. ' };',
  11306. '});',
  11307. 'this.ArrInt = [];',
  11308. 'this.ArrRec = [];',
  11309. 'this.ArrSet = [];',
  11310. 'this.ArrJSValue = [];',
  11311. 'this.ArrArrInt = [];',
  11312. '']),
  11313. LinesToStr([ // $mod.$main
  11314. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11315. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11316. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11317. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11318. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11319. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11320. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11321. '$mod.ArrInt.splice(12, 13);',
  11322. '$mod.ArrRec.splice(14, 15);',
  11323. '$mod.ArrSet.splice(17, 18);',
  11324. '$mod.ArrJSValue.splice(19, 10);',
  11325. '']));
  11326. end;
  11327. procedure TTestModule.TestArray_Add_Append;
  11328. begin
  11329. StartProgram(false);
  11330. Add([
  11331. '{$modeswitch arrayoperators}',
  11332. 'type',
  11333. ' integer = longint;',
  11334. ' TFlag = (big,small);',
  11335. ' TFlags = set of TFlag;',
  11336. ' TRec = record',
  11337. ' i: integer;',
  11338. ' end;',
  11339. ' TArrInt = array of integer;',
  11340. ' TArrRec = array of TRec;',
  11341. ' TArrFlag = array of TFlag;',
  11342. ' TArrSet = array of TFlags;',
  11343. ' TArrJSValue = array of jsvalue;',
  11344. 'var',
  11345. ' ArrInt: tarrint;',
  11346. ' ArrRec: tarrrec;',
  11347. ' ArrFlag: tarrflag;',
  11348. ' ArrSet: tarrset;',
  11349. ' ArrJSValue: tarrjsvalue;',
  11350. ' r: TRec;',
  11351. ' f: TFlags;',
  11352. 'begin',
  11353. ' // append',
  11354. ' arrint:=arrint+[2];',
  11355. ' arrint:=arrint+[3,4];',
  11356. ' arrrec:=arrrec+[r];',
  11357. ' arrrec:=arrrec+[r,r];',
  11358. ' arrset:=arrset+[f];',
  11359. ' arrset:=arrset+[f,f];',
  11360. ' arrjsvalue:=arrjsvalue+[11];',
  11361. ' arrjsvalue:=arrjsvalue+[12,13];',
  11362. ' arrflag:=arrflag+[small];',
  11363. ' arrflag:=arrflag+[small,big];',
  11364. '']);
  11365. ConvertProgram;
  11366. CheckSource('TestArray_Add_Append',
  11367. LinesToStr([ // statements
  11368. 'this.TFlag = {',
  11369. ' "0": "big",',
  11370. ' big: 0,',
  11371. ' "1": "small",',
  11372. ' small: 1',
  11373. '};',
  11374. 'rtl.recNewT(this, "TRec", function () {',
  11375. ' this.i = 0;',
  11376. ' this.$eq = function (b) {',
  11377. ' return this.i === b.i;',
  11378. ' };',
  11379. ' this.$assign = function (s) {',
  11380. ' this.i = s.i;',
  11381. ' return this;',
  11382. ' };',
  11383. '});',
  11384. 'this.ArrInt = [];',
  11385. 'this.ArrRec = [];',
  11386. 'this.ArrFlag = [];',
  11387. 'this.ArrSet = [];',
  11388. 'this.ArrJSValue = [];',
  11389. 'this.r = this.TRec.$new();',
  11390. 'this.f = {};',
  11391. '']),
  11392. LinesToStr([ // $mod.$main
  11393. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11394. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11395. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11396. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11397. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11398. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11399. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11400. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11401. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11402. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11403. '']));
  11404. end;
  11405. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11406. begin
  11407. Parser.Options:=Parser.Options+[po_cassignments];
  11408. StartProgram(false);
  11409. Add([
  11410. '{$modeswitch arrayoperators}',
  11411. 'type',
  11412. ' integer = longint;',
  11413. ' TArrInt = array of integer;',
  11414. ' TArrStr = array of string;',
  11415. 'const',
  11416. ' Ints: TArrInt = (1,2,3);',
  11417. ' Aliases: TarrStr = (''foo'',''b'');',
  11418. ' OneInt: TArrInt = (7);',
  11419. ' OneStr: array of integer = (7);',
  11420. ' Chars: array of char = ''aoc'';',
  11421. ' Names: array of string = (''a'',''foo'');',
  11422. ' NameCount = low(Names)+high(Names)+length(Names);',
  11423. 'var i: integer;',
  11424. 'begin',
  11425. ' Ints:=[];',
  11426. ' Ints:=[1,1];',
  11427. ' Ints:=[1]+[2];',
  11428. ' Ints:=[2];',
  11429. ' Ints:=[]+ints;',
  11430. ' Ints:=Ints+[];',
  11431. ' Ints:=Ints+OneInt;',
  11432. ' Ints:=Ints+[1,1];',
  11433. ' Ints:=[i,i]+Ints;',
  11434. ' Ints:=[1]+[i]+[3];',
  11435. '']);
  11436. ConvertProgram;
  11437. CheckSource('TestArray_DynArrayConstObjFPC',
  11438. LinesToStr([ // statements
  11439. 'this.Ints = [1, 2, 3];',
  11440. 'this.Aliases = ["foo", "b"];',
  11441. 'this.OneInt = [7];',
  11442. 'this.OneStr = [7];',
  11443. 'this.Chars = ["a", "o", "c"];',
  11444. 'this.Names = ["a", "foo"];',
  11445. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11446. 'this.i = 0;',
  11447. '']),
  11448. LinesToStr([ // $mod.$main
  11449. '$mod.Ints = [];',
  11450. '$mod.Ints = [1, 1];',
  11451. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11452. '$mod.Ints = [2];',
  11453. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11454. '$mod.Ints = $mod.Ints;',
  11455. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11456. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11457. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11458. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11459. '']));
  11460. end;
  11461. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11462. begin
  11463. StartProgram(false);
  11464. // Note: const c = [1,1]; defines a set!
  11465. Add([
  11466. '{$mode delphi}',
  11467. 'type',
  11468. ' integer = longint;',
  11469. ' TArrInt = array of integer;',
  11470. ' TArrStr = array of string;',
  11471. 'const',
  11472. ' Ints: TArrInt = [1,1,2];',
  11473. ' Aliases: TarrStr = [''foo'',''b''];',
  11474. ' OneInt: TArrInt = [7];',
  11475. ' OneStr: array of integer = [7]+[8];',
  11476. ' Chars: array of char = ''aoc'';',
  11477. ' Names: array of string = [''a'',''a''];',
  11478. ' NameCount = low(Names)+high(Names)+length(Names);',
  11479. 'begin',
  11480. '']);
  11481. ConvertProgram;
  11482. CheckSource('TestArray_DynArrayConstDelphi',
  11483. LinesToStr([ // statements
  11484. 'this.Ints = [1, 1, 2];',
  11485. 'this.Aliases = ["foo", "b"];',
  11486. 'this.OneInt = [7];',
  11487. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11488. 'this.Chars = ["a", "o", "c"];',
  11489. 'this.Names = ["a", "a"];',
  11490. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11491. '']),
  11492. LinesToStr([ // $mod.$main
  11493. '']));
  11494. end;
  11495. procedure TTestModule.TestArray_ArrayLitAsParam;
  11496. begin
  11497. StartProgram(false);
  11498. Add([
  11499. '{$modeswitch arrayoperators}',
  11500. 'type',
  11501. ' integer = longint;',
  11502. ' TArrInt = array of integer;',
  11503. ' TArrSet = array of (red,green,blue);',
  11504. 'procedure DoOpenInt(const a: array of integer); forward;',
  11505. 'procedure DoInt(const a: TArrInt);',
  11506. 'begin',
  11507. ' DoInt(a+[1]);',
  11508. ' DoInt([1]+a);',
  11509. ' DoOpenInt(a);',
  11510. ' DoOpenInt(a+[1]);',
  11511. ' DoOpenInt([1]+a);',
  11512. 'end;',
  11513. 'procedure DoOpenInt(const a: array of integer);',
  11514. 'begin',
  11515. ' DoOpenInt(a+[1]);',
  11516. ' DoOpenInt([1]+a);',
  11517. ' DoInt(a);',
  11518. ' DoInt(a+[1]);',
  11519. ' DoInt([1]+a);',
  11520. 'end;',
  11521. 'procedure DoSet(const a: TArrSet);',
  11522. 'begin',
  11523. ' DoSet(a+[red]);',
  11524. ' DoSet([blue]+a);',
  11525. 'end;',
  11526. 'var',
  11527. ' i: TArrInt;',
  11528. ' s: TArrSet;',
  11529. 'begin',
  11530. ' DoInt([1]);',
  11531. ' DoInt([1]+[2]);',
  11532. ' DoInt(i+[1]);',
  11533. ' DoInt([1]+i);',
  11534. ' DoOpenInt([1]);',
  11535. ' DoOpenInt([1]+[2]);',
  11536. ' DoOpenInt(i+[1]);',
  11537. ' DoOpenInt([1]+i);',
  11538. ' DoSet([red]);',
  11539. ' DoSet([blue]+[green]);',
  11540. ' DoSet(s+[blue]);',
  11541. ' DoSet([red]+s);',
  11542. '']);
  11543. ConvertProgram;
  11544. CheckSource('TestArray_ArrayLitAsParam',
  11545. LinesToStr([ // statements
  11546. 'this.TArrSet$a = {',
  11547. ' "0": "red",',
  11548. ' red: 0,',
  11549. ' "1": "green",',
  11550. ' green: 1,',
  11551. ' "2": "blue",',
  11552. ' blue: 2',
  11553. '};',
  11554. 'this.DoInt = function (a) {',
  11555. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11556. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11557. ' $mod.DoOpenInt(a);',
  11558. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11559. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11560. '};',
  11561. 'this.DoOpenInt = function (a) {',
  11562. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11563. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11564. ' $mod.DoInt(a);',
  11565. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11566. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11567. '};',
  11568. 'this.DoSet = function (a) {',
  11569. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11570. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11571. '};',
  11572. 'this.i = [];',
  11573. 'this.s = [];',
  11574. '']),
  11575. LinesToStr([ // $mod.$main
  11576. '$mod.DoInt([1]);',
  11577. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11578. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11579. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11580. '$mod.DoOpenInt([1]);',
  11581. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11582. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11583. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11584. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11585. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11586. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11587. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11588. '']));
  11589. end;
  11590. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11591. begin
  11592. StartProgram(false);
  11593. Add([
  11594. '{$modeswitch arrayoperators}',
  11595. 'type',
  11596. ' integer = longint;',
  11597. ' TArrInt = array of integer;',
  11598. ' TArrArrInt = array of TArrInt;',
  11599. 'procedure DoInt(const a: TArrArrInt);',
  11600. 'begin',
  11601. ' DoInt(a+[[1]]);',
  11602. ' DoInt([[1]]+a);',
  11603. ' DoInt(a);',
  11604. 'end;',
  11605. 'var',
  11606. ' i: TArrInt;',
  11607. ' a: TArrArrInt;',
  11608. 'begin',
  11609. ' a:=[[1]];',
  11610. ' a:=[i];',
  11611. ' a:=a+[i];',
  11612. ' a:=[i]+a;',
  11613. ' a:=[[1]+i];',
  11614. ' a:=[[1]+[2]];',
  11615. ' a:=[i+[2]];',
  11616. ' DoInt([[1]]);',
  11617. ' DoInt([[1]+[2],[3,4],[5]]);',
  11618. ' DoInt([i+[1]]+a);',
  11619. ' DoInt([i]+a);',
  11620. '']);
  11621. ConvertProgram;
  11622. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11623. LinesToStr([ // statements
  11624. 'this.DoInt = function (a) {',
  11625. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11626. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11627. ' $mod.DoInt(a);',
  11628. '};',
  11629. 'this.i = [];',
  11630. 'this.a = [];',
  11631. '']),
  11632. LinesToStr([ // $mod.$main
  11633. '$mod.a = [[1]];',
  11634. '$mod.a = [$mod.i];',
  11635. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11636. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11637. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11638. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11639. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11640. '$mod.DoInt([[1]]);',
  11641. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11642. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11643. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11644. '']));
  11645. end;
  11646. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11647. begin
  11648. StartProgram(false);
  11649. Add([
  11650. '{$modeswitch arrayoperators}',
  11651. 'type',
  11652. ' integer = longint;',
  11653. ' TArrInt = array[1..2] of integer;',
  11654. ' TArrArrInt = array of TArrInt;',
  11655. 'procedure DoInt(const a: TArrArrInt);',
  11656. 'begin',
  11657. ' DoInt(a+[[1,2]]);',
  11658. ' DoInt([[1,2]]+a);',
  11659. ' DoInt(a);',
  11660. 'end;',
  11661. 'var',
  11662. ' i: TArrInt;',
  11663. ' a: TArrArrInt;',
  11664. 'begin',
  11665. ' a:=[[1,1]];',
  11666. ' a:=[i];',
  11667. ' a:=a+[i];',
  11668. ' a:=[i]+a;',
  11669. ' DoInt([[1,1]]);',
  11670. ' DoInt([[1,2],[3,4]]);',
  11671. '']);
  11672. ConvertProgram;
  11673. CheckSource('TestArray_ArrayLitStaticAsParam',
  11674. LinesToStr([ // statements
  11675. 'this.DoInt = function (a) {',
  11676. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11677. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11678. ' $mod.DoInt(a);',
  11679. '};',
  11680. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11681. 'this.a = [];',
  11682. '']),
  11683. LinesToStr([ // $mod.$main
  11684. '$mod.a = [[1, 1]];',
  11685. '$mod.a = [$mod.i.slice(0)];',
  11686. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11687. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11688. '$mod.DoInt([[1, 1]]);',
  11689. '$mod.DoInt([[1, 2], [3, 4]]);',
  11690. '']));
  11691. end;
  11692. procedure TTestModule.TestArray_ForInArrOfString;
  11693. begin
  11694. StartProgram(false);
  11695. Add([
  11696. 'type',
  11697. 'type',
  11698. ' TMonthNameArray = array [1..12] of string;',
  11699. ' TMonthNames = TMonthNameArray;',
  11700. ' TObject = class',
  11701. ' private',
  11702. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11703. ' public',
  11704. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11705. ' end;',
  11706. 'var',
  11707. ' f: TObject;',
  11708. ' Month: string;',
  11709. ' Names: array of string = (''a'',''foo'',''bar'');',
  11710. ' i: longint;',
  11711. 'begin',
  11712. ' for Month in f.LongMonthNames do ;',
  11713. ' for Month in Names do ;',
  11714. ' for i:=low(Names) to high(Names) do ;',
  11715. '']);
  11716. ConvertProgram;
  11717. CheckSource('TestArray_ForInArrOfString',
  11718. LinesToStr([ // statements
  11719. 'rtl.createClass(this, "TObject", null, function () {',
  11720. ' this.$init = function () {',
  11721. ' };',
  11722. ' this.$final = function () {',
  11723. ' };',
  11724. '});',
  11725. 'this.f = null;',
  11726. 'this.Month = "";',
  11727. 'this.Names = ["a", "foo", "bar"];',
  11728. 'this.i = 0;',
  11729. '']),
  11730. LinesToStr([ // $mod.$main
  11731. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11732. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11733. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11734. '']));
  11735. end;
  11736. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11737. begin
  11738. StartProgram(false);
  11739. Add([
  11740. '{$modeswitch externalclass}',
  11741. 'type',
  11742. ' TJSObject = class external name ''Object''',
  11743. ' end;',
  11744. ' TJSArray = class external name ''Array''',
  11745. ' class function isArray(Value: JSValue) : boolean;',
  11746. ' function concat() : TJSArray; varargs;',
  11747. ' end;',
  11748. 'var',
  11749. ' aObj: TJSArray;',
  11750. ' a: array of longint;',
  11751. ' o: TJSObject;',
  11752. 'begin',
  11753. ' if TJSArray.isArray(65) then ;',
  11754. ' aObj:=TJSArray(a).concat(a);',
  11755. ' o:=TJSObject(a);',
  11756. ' aObj:=TJSArray([''bird'',''ant'']);',
  11757. '']);
  11758. ConvertProgram;
  11759. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11760. LinesToStr([ // statements
  11761. 'this.aObj = null;',
  11762. 'this.a = [];',
  11763. 'this.o = null;',
  11764. '']),
  11765. LinesToStr([ // $mod.$main
  11766. 'if (Array.isArray(65)) ;',
  11767. '$mod.aObj = $mod.a.concat($mod.a);',
  11768. '$mod.o = $mod.a;',
  11769. '$mod.aObj = ["bird", "ant"];',
  11770. '']));
  11771. end;
  11772. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11773. begin
  11774. StartProgram(false);
  11775. Add([
  11776. '{$modeswitch externalclass}',
  11777. 'type',
  11778. ' TArrStr = array of string;',
  11779. ' TJSArray = class external name ''Array''',
  11780. ' end;',
  11781. ' TJSObject = class external name ''Object''',
  11782. ' end;',
  11783. 'var',
  11784. ' aObj: TJSArray;',
  11785. ' a: TArrStr;',
  11786. ' jo: TJSObject;',
  11787. 'begin',
  11788. ' a:=TArrStr(aObj);',
  11789. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11790. ' a:=TarrStr(jo);',
  11791. '']);
  11792. ConvertProgram;
  11793. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11794. LinesToStr([ // statements
  11795. 'this.aObj = null;',
  11796. 'this.a = [];',
  11797. 'this.jo = null;',
  11798. '']),
  11799. LinesToStr([ // $mod.$main
  11800. '$mod.a = $mod.aObj;',
  11801. '$mod.aObj[1] = $mod.aObj[2];',
  11802. '$mod.a = $mod.jo;',
  11803. '']));
  11804. end;
  11805. procedure TTestModule.TestArrayOfConst_TVarRec;
  11806. begin
  11807. StartProgram(true,[supTVarRec]);
  11808. Add([
  11809. 'procedure Say(args: array of const);',
  11810. 'var',
  11811. ' i: longint;',
  11812. ' v: TVarRec;',
  11813. 'begin',
  11814. ' for i:=low(args) to high(args) do begin',
  11815. ' v:=args[i];',
  11816. ' case v.vtype of',
  11817. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11818. ' end;',
  11819. ' end;',
  11820. ' for v in args do ;',
  11821. ' args:=nil;',
  11822. ' SetLength(args,2);',
  11823. 'end;',
  11824. 'begin']);
  11825. ConvertProgram;
  11826. CheckSource('TestArrayOfConst_TVarRec',
  11827. LinesToStr([ // statements
  11828. 'this.Say = function (args) {',
  11829. ' var i = 0;',
  11830. ' var v = pas.system.TVarRec.$new();',
  11831. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11832. ' i = $l;',
  11833. ' v.$assign(args[i]);',
  11834. ' var $tmp = v.VType;',
  11835. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11836. ' };',
  11837. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11838. ' args = [];',
  11839. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11840. '};',
  11841. '']),
  11842. LinesToStr([ // $mod.$main
  11843. ]));
  11844. end;
  11845. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11846. begin
  11847. StartProgram(true,[supTVarRec]);
  11848. Add([
  11849. 'procedure Say(args: array of const);',
  11850. 'begin',
  11851. ' Say(args);',
  11852. 'end;',
  11853. 'var',
  11854. ' p: Pointer;',
  11855. ' j: jsvalue;',
  11856. ' c: currency;',
  11857. 'begin',
  11858. ' Say([]);',
  11859. ' Say([1]);',
  11860. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11861. '']);
  11862. ConvertProgram;
  11863. CheckSource('TestArrayOfConst_PassBaseTypes',
  11864. LinesToStr([ // statements
  11865. 'this.Say = function (args) {',
  11866. ' $mod.Say(args);',
  11867. '};',
  11868. 'this.p = null;',
  11869. 'this.j = undefined;',
  11870. 'this.c = 0;',
  11871. '']),
  11872. LinesToStr([ // $mod.$main
  11873. '$mod.Say([]);',
  11874. '$mod.Say(pas.system.VarRecs(0, 1));',
  11875. '$mod.Say(pas.system.VarRecs(',
  11876. ' 9,',
  11877. ' "c",',
  11878. ' 18,',
  11879. ' "foo",',
  11880. ' 5,',
  11881. ' null,',
  11882. ' 1,',
  11883. ' true,',
  11884. ' 3,',
  11885. ' 1.3,',
  11886. ' 5,',
  11887. ' $mod.p,',
  11888. ' 20,',
  11889. ' $mod.j,',
  11890. ' 12,',
  11891. ' $mod.c',
  11892. ' ));',
  11893. '']));
  11894. end;
  11895. procedure TTestModule.TestArrayOfConst_PassObj;
  11896. begin
  11897. StartProgram(true,[supTVarRec]);
  11898. Add([
  11899. '{$interfaces corba}',
  11900. 'type',
  11901. ' TObject = class',
  11902. ' end;',
  11903. ' TClass = class of TObject;',
  11904. ' IUnknown = interface',
  11905. ' end;',
  11906. 'procedure Say(args: array of const);',
  11907. 'begin',
  11908. 'end;',
  11909. 'var',
  11910. ' o: TObject;',
  11911. ' c: TClass;',
  11912. ' i: IUnknown;',
  11913. 'begin',
  11914. ' Say([o,c,TObject]);',
  11915. ' Say([nil,i]);',
  11916. '']);
  11917. ConvertProgram;
  11918. CheckSource('TestArrayOfConst_PassObj',
  11919. LinesToStr([ // statements
  11920. 'rtl.createClass(this, "TObject", null, function () {',
  11921. ' this.$init = function () {',
  11922. ' };',
  11923. ' this.$final = function () {',
  11924. ' };',
  11925. '});',
  11926. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11927. 'this.Say = function (args) {',
  11928. '};',
  11929. 'this.o = null;',
  11930. 'this.c = null;',
  11931. 'this.i = null;',
  11932. '']),
  11933. LinesToStr([ // $mod.$main
  11934. '$mod.Say(pas.system.VarRecs(',
  11935. ' 7,',
  11936. ' $mod.o,',
  11937. ' 8,',
  11938. ' $mod.c,',
  11939. ' 8,',
  11940. ' $mod.TObject',
  11941. '));',
  11942. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11943. '']));
  11944. end;
  11945. procedure TTestModule.TestRecord_Empty;
  11946. begin
  11947. StartProgram(false);
  11948. Add([
  11949. 'type',
  11950. ' TRecA = record',
  11951. ' end;',
  11952. 'var a,b: TRecA;',
  11953. 'begin',
  11954. ' if a=b then ;']);
  11955. ConvertProgram;
  11956. CheckSource('TestRecord_Empty',
  11957. LinesToStr([ // statements
  11958. 'rtl.recNewT(this, "TRecA", function () {',
  11959. ' this.$eq = function (b) {',
  11960. ' return true;',
  11961. ' };',
  11962. ' this.$assign = function (s) {',
  11963. ' return this;',
  11964. ' };',
  11965. '});',
  11966. 'this.a = this.TRecA.$new();',
  11967. 'this.b = this.TRecA.$new();',
  11968. '']),
  11969. LinesToStr([ // $mod.$main
  11970. 'if ($mod.a.$eq($mod.b)) ;'
  11971. ]));
  11972. end;
  11973. procedure TTestModule.TestRecord_Var;
  11974. begin
  11975. StartProgram(false);
  11976. Add('type');
  11977. Add(' TRecA = record');
  11978. Add(' Bold: longint;');
  11979. Add(' end;');
  11980. Add('var Rec: TRecA;');
  11981. Add('begin');
  11982. Add(' rec.bold:=123');
  11983. ConvertProgram;
  11984. CheckSource('TestRecord_Var',
  11985. LinesToStr([ // statements
  11986. 'rtl.recNewT(this, "TRecA", function () {',
  11987. ' this.Bold = 0;',
  11988. ' this.$eq = function (b) {',
  11989. ' return this.Bold === b.Bold;',
  11990. ' };',
  11991. ' this.$assign = function (s) {',
  11992. ' this.Bold = s.Bold;',
  11993. ' return this;',
  11994. ' };',
  11995. '});',
  11996. 'this.Rec = this.TRecA.$new();',
  11997. '']),
  11998. LinesToStr([ // $mod.$main
  11999. '$mod.Rec.Bold = 123;'
  12000. ]));
  12001. end;
  12002. procedure TTestModule.TestRecord_VarExternal;
  12003. begin
  12004. StartProgram(false);
  12005. Add([
  12006. '{$modeswitch externalclass}',
  12007. 'type',
  12008. ' TRecA = record',
  12009. ' i: byte;',
  12010. ' length_: longint external name ''length'';',
  12011. ' end;',
  12012. 'var Rec: TRecA;',
  12013. 'begin',
  12014. ' rec.length_ := rec.length_',
  12015. '']);
  12016. ConvertProgram;
  12017. CheckSource('TestRecord_VarExternal',
  12018. LinesToStr([ // statements
  12019. 'rtl.recNewT(this, "TRecA", function () {',
  12020. ' this.i = 0;',
  12021. ' this.$eq = function (b) {',
  12022. ' return (this.i === b.i) && (this.length === b.length);',
  12023. ' };',
  12024. ' this.$assign = function (s) {',
  12025. ' this.i = s.i;',
  12026. ' this.length = s.length;',
  12027. ' return this;',
  12028. ' };',
  12029. '});',
  12030. 'this.Rec = this.TRecA.$new();',
  12031. '']),
  12032. LinesToStr([ // $mod.$main
  12033. '$mod.Rec.length = $mod.Rec.length;'
  12034. ]));
  12035. end;
  12036. procedure TTestModule.TestRecord_WithDo;
  12037. begin
  12038. StartProgram(false);
  12039. Add('type');
  12040. Add(' TRec = record');
  12041. Add(' vI: longint;');
  12042. Add(' end;');
  12043. Add('var');
  12044. Add(' Int: longint;');
  12045. Add(' r: TRec;');
  12046. Add('begin');
  12047. Add(' with r do');
  12048. Add(' int:=vi;');
  12049. Add(' with r do begin');
  12050. Add(' int:=vi;');
  12051. Add(' vi:=int;');
  12052. Add(' end;');
  12053. ConvertProgram;
  12054. CheckSource('TestWithRecordDo',
  12055. LinesToStr([ // statements
  12056. 'rtl.recNewT(this, "TRec", function () {',
  12057. ' this.vI = 0;',
  12058. ' this.$eq = function (b) {',
  12059. ' return this.vI === b.vI;',
  12060. ' };',
  12061. ' this.$assign = function (s) {',
  12062. ' this.vI = s.vI;',
  12063. ' return this;',
  12064. ' };',
  12065. '});',
  12066. 'this.Int = 0;',
  12067. 'this.r = this.TRec.$new();',
  12068. '']),
  12069. LinesToStr([ // $mod.$main
  12070. 'var $with = $mod.r;',
  12071. '$mod.Int = $with.vI;',
  12072. 'var $with1 = $mod.r;',
  12073. '$mod.Int = $with1.vI;',
  12074. '$with1.vI = $mod.Int;'
  12075. ]));
  12076. end;
  12077. procedure TTestModule.TestRecord_Assign;
  12078. begin
  12079. StartProgram(false);
  12080. Add([
  12081. 'type',
  12082. ' TEnum = (red,green);',
  12083. ' TEnums = set of TEnum;',
  12084. ' TSmallRec = record',
  12085. ' N: longint;',
  12086. ' end;',
  12087. ' TBigRec = record',
  12088. ' Int: longint;',
  12089. ' D: double;',
  12090. ' Arr: array of longint;',
  12091. ' Arr2: array[1..2] of longint;',
  12092. ' Small: TSmallRec;',
  12093. ' Enums: TEnums;',
  12094. ' end;',
  12095. 'var',
  12096. ' r, s: TBigRec;',
  12097. 'begin',
  12098. ' r:=s;',
  12099. ' r:=default(TBigRec);',
  12100. ' r:=default(s);',
  12101. '']);
  12102. ConvertProgram;
  12103. CheckSource('TestRecord_Assign',
  12104. LinesToStr([ // statements
  12105. 'this.TEnum = {',
  12106. ' "0": "red",',
  12107. ' red: 0,',
  12108. ' "1": "green",',
  12109. ' green: 1',
  12110. '};',
  12111. 'rtl.recNewT(this, "TSmallRec", function () {',
  12112. ' this.N = 0;',
  12113. ' this.$eq = function (b) {',
  12114. ' return this.N === b.N;',
  12115. ' };',
  12116. ' this.$assign = function (s) {',
  12117. ' this.N = s.N;',
  12118. ' return this;',
  12119. ' };',
  12120. '});',
  12121. 'rtl.recNewT(this, "TBigRec", function () {',
  12122. ' this.Int = 0;',
  12123. ' this.D = 0.0;',
  12124. ' this.$new = function () {',
  12125. ' var r = Object.create(this);',
  12126. ' r.Arr = [];',
  12127. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12128. ' r.Small = $mod.TSmallRec.$new();',
  12129. ' r.Enums = {};',
  12130. ' return r;',
  12131. ' };',
  12132. ' this.$eq = function (b) {',
  12133. ' 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);',
  12134. ' };',
  12135. ' this.$assign = function (s) {',
  12136. ' this.Int = s.Int;',
  12137. ' this.D = s.D;',
  12138. ' this.Arr = rtl.arrayRef(s.Arr);',
  12139. ' this.Arr2 = s.Arr2.slice(0);',
  12140. ' this.Small.$assign(s.Small);',
  12141. ' this.Enums = rtl.refSet(s.Enums);',
  12142. ' return this;',
  12143. ' };',
  12144. '});',
  12145. 'this.r = this.TBigRec.$new();',
  12146. 'this.s = this.TBigRec.$new();',
  12147. '']),
  12148. LinesToStr([ // $mod.$main
  12149. '$mod.r.$assign($mod.s);',
  12150. '$mod.r.$assign($mod.TBigRec.$new());',
  12151. '$mod.r.$assign($mod.TBigRec.$new());',
  12152. '']));
  12153. end;
  12154. procedure TTestModule.TestRecord_AsParams;
  12155. begin
  12156. StartProgram(false);
  12157. Add([
  12158. 'type',
  12159. ' integer = longint;',
  12160. ' TRecord = record',
  12161. ' i: integer;',
  12162. ' end;',
  12163. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12164. 'var vL: TRecord;',
  12165. 'begin',
  12166. ' vd:=vd;',
  12167. ' vd.i:=vd.i;',
  12168. ' vl:=vc;',
  12169. ' vv:=vv;',
  12170. ' vv.i:=vv.i;',
  12171. ' U:=vl;',
  12172. ' U:=vd;',
  12173. ' U:=vc;',
  12174. ' U:=vv;',
  12175. ' vl:=TRecord(U);',
  12176. ' vd:=TRecord(U);',
  12177. ' vv:=TRecord(U);',
  12178. ' doit(vd,vd,vd,vd);',
  12179. ' doit(vc,vc,vl,vl);',
  12180. ' doit(vv,vv,vv,vv);',
  12181. ' doit(vl,vl,vl,vl);',
  12182. ' TRecord(U).i:=3;',
  12183. 'end;',
  12184. 'var i: TRecord;',
  12185. 'begin',
  12186. ' doit(i,i,i,i);',
  12187. '']);
  12188. ConvertProgram;
  12189. CheckSource('TestRecord_AsParams',
  12190. LinesToStr([ // statements
  12191. 'rtl.recNewT(this, "TRecord", function () {',
  12192. ' this.i = 0;',
  12193. ' this.$eq = function (b) {',
  12194. ' return this.i === b.i;',
  12195. ' };',
  12196. ' this.$assign = function (s) {',
  12197. ' this.i = s.i;',
  12198. ' return this;',
  12199. ' };',
  12200. '});',
  12201. 'this.DoIt = function (vD, vC, vV, U) {',
  12202. ' var vL = $mod.TRecord.$new();',
  12203. ' vD.$assign(vD);',
  12204. ' vD.i = vD.i;',
  12205. ' vL.$assign(vC);',
  12206. ' vV.$assign(vV);',
  12207. ' vV.i = vV.i;',
  12208. ' U.$assign(vL);',
  12209. ' U.$assign(vD);',
  12210. ' U.$assign(vC);',
  12211. ' U.$assign(vV);',
  12212. ' vL.$assign(U);',
  12213. ' vD.$assign(U);',
  12214. ' vV.$assign(U);',
  12215. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12216. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12217. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12218. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12219. ' U.i = 3;',
  12220. '};',
  12221. 'this.i = this.TRecord.$new();'
  12222. ]),
  12223. LinesToStr([
  12224. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12225. '']));
  12226. end;
  12227. procedure TTestModule.TestRecord_ConstRef;
  12228. begin
  12229. StartProgram(false);
  12230. Add([
  12231. 'type TRec = record i: word; end;',
  12232. 'procedure Run(constref a: TRec);',
  12233. 'begin',
  12234. 'end;',
  12235. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12236. 'var l: TRec;',
  12237. 'begin',
  12238. ' Run(l);',
  12239. ' Run(a);',
  12240. ' Run(b);',
  12241. ' Run(c);',
  12242. ' Run(d);',
  12243. ' Run(e);',
  12244. 'end;',
  12245. 'begin',
  12246. '']);
  12247. ConvertProgram;
  12248. CheckResolverUnexpectedHints();
  12249. CheckSource('TestRecord_ConstRef',
  12250. LinesToStr([ // statements
  12251. 'rtl.recNewT(this, "TRec", function () {',
  12252. ' this.i = 0;',
  12253. ' this.$eq = function (b) {',
  12254. ' return this.i === b.i;',
  12255. ' };',
  12256. ' this.$assign = function (s) {',
  12257. ' this.i = s.i;',
  12258. ' return this;',
  12259. ' };',
  12260. '});',
  12261. 'this.Run = function (a) {',
  12262. '};',
  12263. 'this.Fly = function (a, b, c, d, e) {',
  12264. ' var l = $mod.TRec.$new();',
  12265. ' $mod.Run(l);',
  12266. ' $mod.Run(a);',
  12267. ' $mod.Run(b);',
  12268. ' $mod.Run(c);',
  12269. ' $mod.Run(d);',
  12270. ' $mod.Run(e);',
  12271. '};',
  12272. '']),
  12273. LinesToStr([
  12274. '']));
  12275. end;
  12276. procedure TTestModule.TestRecordElement_AsParams;
  12277. begin
  12278. StartProgram(false);
  12279. Add('type');
  12280. Add(' integer = longint;');
  12281. Add(' TRecord = record');
  12282. Add(' i: integer;');
  12283. Add(' end;');
  12284. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12285. Add('var vJ: TRecord;');
  12286. Add('begin');
  12287. Add(' doit(vj.i,vj.i,vj.i);');
  12288. Add('end;');
  12289. Add('var r: TRecord;');
  12290. Add('begin');
  12291. Add(' doit(r.i,r.i,r.i);');
  12292. ConvertProgram;
  12293. CheckSource('TestRecordElement_AsParams',
  12294. LinesToStr([ // statements
  12295. 'rtl.recNewT(this, "TRecord", function () {',
  12296. ' this.i = 0;',
  12297. ' this.$eq = function (b) {',
  12298. ' return this.i === b.i;',
  12299. ' };',
  12300. ' this.$assign = function (s) {',
  12301. ' this.i = s.i;',
  12302. ' return this;',
  12303. ' };',
  12304. '});',
  12305. 'this.DoIt = function (vG,vH,vI) {',
  12306. ' var vJ = $mod.TRecord.$new();',
  12307. ' $mod.DoIt(vJ.i, vJ.i, {',
  12308. ' p: vJ,',
  12309. ' get: function () {',
  12310. ' return this.p.i;',
  12311. ' },',
  12312. ' set: function (v) {',
  12313. ' this.p.i = v;',
  12314. ' }',
  12315. ' });',
  12316. '};',
  12317. 'this.r = this.TRecord.$new();'
  12318. ]),
  12319. LinesToStr([
  12320. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12321. ' p: $mod.r,',
  12322. ' get: function () {',
  12323. ' return this.p.i;',
  12324. ' },',
  12325. ' set: function (v) {',
  12326. ' this.p.i = v;',
  12327. ' }',
  12328. '});'
  12329. ]));
  12330. end;
  12331. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12332. begin
  12333. StartProgram(false);
  12334. Add('type');
  12335. Add(' integer = longint;');
  12336. Add(' TRecord = record');
  12337. Add(' i: integer;');
  12338. Add(' end;');
  12339. Add('function GetRec(vB: integer = 0): TRecord;');
  12340. Add('begin');
  12341. Add('end;');
  12342. Add('procedure DoIt(vG: integer; const vH: integer);');
  12343. Add('begin');
  12344. Add('end;');
  12345. Add('begin');
  12346. Add(' doit(getrec.i,getrec.i);');
  12347. Add(' doit(getrec().i,getrec().i);');
  12348. Add(' doit(getrec(1).i,getrec(2).i);');
  12349. ConvertProgram;
  12350. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12351. LinesToStr([ // statements
  12352. 'rtl.recNewT(this, "TRecord", function () {',
  12353. ' this.i = 0;',
  12354. ' this.$eq = function (b) {',
  12355. ' return this.i === b.i;',
  12356. ' };',
  12357. ' this.$assign = function (s) {',
  12358. ' this.i = s.i;',
  12359. ' return this;',
  12360. ' };',
  12361. '});',
  12362. 'this.GetRec = function (vB) {',
  12363. ' var Result = $mod.TRecord.$new();',
  12364. ' return Result;',
  12365. '};',
  12366. 'this.DoIt = function (vG, vH) {',
  12367. '};',
  12368. '']),
  12369. LinesToStr([
  12370. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12371. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12372. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12373. '']));
  12374. end;
  12375. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12376. begin
  12377. StartProgram(false);
  12378. Add('type');
  12379. Add(' integer = longint;');
  12380. Add(' TRecord = record');
  12381. Add(' i: integer;');
  12382. Add(' end;');
  12383. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12384. Add('begin');
  12385. Add('end;');
  12386. Add('var r: trecord;');
  12387. Add('begin');
  12388. Add(' with r do ');
  12389. Add(' doit(i,i,i);');
  12390. ConvertProgram;
  12391. CheckSource('TestRecordElementFromWith_AsParams',
  12392. LinesToStr([ // statements
  12393. 'rtl.recNewT(this, "TRecord", function () {',
  12394. ' this.i = 0;',
  12395. ' this.$eq = function (b) {',
  12396. ' return this.i === b.i;',
  12397. ' };',
  12398. ' this.$assign = function (s) {',
  12399. ' this.i = s.i;',
  12400. ' return this;',
  12401. ' };',
  12402. '});',
  12403. 'this.DoIt = function (vG,vH,vI) {',
  12404. '};',
  12405. 'this.r = this.TRecord.$new();'
  12406. ]),
  12407. LinesToStr([
  12408. 'var $with = $mod.r;',
  12409. '$mod.DoIt($with.i,$with.i,{',
  12410. ' p: $with,',
  12411. ' get: function () {',
  12412. ' return this.p.i;',
  12413. ' },',
  12414. ' set: function (v) {',
  12415. ' this.p.i = v;',
  12416. ' }',
  12417. '});',
  12418. '']));
  12419. end;
  12420. procedure TTestModule.TestRecord_Equal;
  12421. begin
  12422. StartProgram(false);
  12423. Add('type');
  12424. Add(' integer = longint;');
  12425. Add(' TFlag = (red,blue);');
  12426. Add(' TFlags = set of TFlag;');
  12427. Add(' TProc = procedure;');
  12428. Add(' TRecord = record');
  12429. Add(' i: integer;');
  12430. Add(' Event: TProc;');
  12431. Add(' f: TFlags;');
  12432. Add(' end;');
  12433. Add(' TNested = record');
  12434. Add(' r: TRecord;');
  12435. Add(' end;');
  12436. Add('var');
  12437. Add(' b: boolean;');
  12438. Add(' r,s: trecord;');
  12439. Add('begin');
  12440. Add(' b:=r=s;');
  12441. Add(' b:=r<>s;');
  12442. ConvertProgram;
  12443. CheckSource('TestRecord_Equal',
  12444. LinesToStr([ // statements
  12445. 'this.TFlag = {',
  12446. ' "0": "red",',
  12447. ' red: 0,',
  12448. ' "1": "blue",',
  12449. ' blue: 1',
  12450. '};',
  12451. 'rtl.recNewT(this, "TRecord", function () {',
  12452. ' this.i = 0;',
  12453. ' this.Event = null;',
  12454. ' this.$new = function () {',
  12455. ' var r = Object.create(this);',
  12456. ' r.f = {};',
  12457. ' return r;',
  12458. ' };',
  12459. ' this.$eq = function (b) {',
  12460. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12461. ' };',
  12462. ' this.$assign = function (s) {',
  12463. ' this.i = s.i;',
  12464. ' this.Event = s.Event;',
  12465. ' this.f = rtl.refSet(s.f);',
  12466. ' return this;',
  12467. ' };',
  12468. '});',
  12469. 'rtl.recNewT(this, "TNested", function () {',
  12470. ' this.$new = function () {',
  12471. ' var r = Object.create(this);',
  12472. ' r.r = $mod.TRecord.$new();',
  12473. ' return r;',
  12474. ' };',
  12475. ' this.$eq = function (b) {',
  12476. ' return this.r.$eq(b.r);',
  12477. ' };',
  12478. ' this.$assign = function (s) {',
  12479. ' this.r.$assign(s.r);',
  12480. ' return this;',
  12481. ' };',
  12482. '});',
  12483. 'this.b = false;',
  12484. 'this.r = this.TRecord.$new();',
  12485. 'this.s = this.TRecord.$new();',
  12486. '']),
  12487. LinesToStr([
  12488. '$mod.b = $mod.r.$eq($mod.s);',
  12489. '$mod.b = !$mod.r.$eq($mod.s);',
  12490. '']));
  12491. end;
  12492. procedure TTestModule.TestRecord_JSValue;
  12493. begin
  12494. StartProgram(false);
  12495. Add([
  12496. 'type',
  12497. ' TRecord = record',
  12498. ' i: longint;',
  12499. ' end;',
  12500. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12501. 'begin',
  12502. 'end;',
  12503. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12504. 'begin',
  12505. ' if jsvalue(d) then ;',
  12506. ' if jsvalue(c) then ;',
  12507. ' if jsvalue(v) then ;',
  12508. 'end;',
  12509. 'var',
  12510. ' Jv: jsvalue;',
  12511. ' Rec: trecord;',
  12512. 'begin',
  12513. ' rec:=trecord(jv);',
  12514. ' jv:=rec;',
  12515. ' Fly(rec,rec);',
  12516. ' Fly(@rec,@rec);',
  12517. ' if jsvalue(Rec) then ;',
  12518. ' Run(trecord(jv),trecord(jv),rec);',
  12519. '']);
  12520. ConvertProgram;
  12521. CheckSource('TestRecord_JSValue',
  12522. LinesToStr([ // statements
  12523. 'rtl.recNewT(this, "TRecord", function () {',
  12524. ' this.i = 0;',
  12525. ' this.$eq = function (b) {',
  12526. ' return this.i === b.i;',
  12527. ' };',
  12528. ' this.$assign = function (s) {',
  12529. ' this.i = s.i;',
  12530. ' return this;',
  12531. ' };',
  12532. '});',
  12533. 'this.Fly = function (d, c) {',
  12534. '};',
  12535. 'this.Run = function (d, c, v) {',
  12536. ' if (d) ;',
  12537. ' if (c) ;',
  12538. ' if (v) ;',
  12539. '};',
  12540. 'this.Jv = undefined;',
  12541. 'this.Rec = this.TRecord.$new();',
  12542. '']),
  12543. LinesToStr([
  12544. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12545. '$mod.Jv = $mod.Rec;',
  12546. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12547. '$mod.Fly($mod.Rec, $mod.Rec);',
  12548. 'if ($mod.Rec) ;',
  12549. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12550. '']));
  12551. end;
  12552. procedure TTestModule.TestRecord_VariantFail;
  12553. begin
  12554. StartProgram(false);
  12555. Add([
  12556. 'type',
  12557. ' TRec = record',
  12558. ' case word of',
  12559. ' 0: (b0, b1: Byte);',
  12560. ' 1: (i: word);',
  12561. ' end;',
  12562. 'begin']);
  12563. SetExpectedPasResolverError('Not supported: variant record',
  12564. nNotSupportedX);
  12565. ConvertProgram;
  12566. end;
  12567. procedure TTestModule.TestRecord_FieldArray;
  12568. begin
  12569. StartProgram(false);
  12570. Add([
  12571. 'type',
  12572. ' TArrInt = array[3..4] of longint;',
  12573. ' TArrArrInt = array[3..4] of longint;',
  12574. ' TRec = record',
  12575. ' a: array of longint;',
  12576. ' s: array[1..2] of longint;',
  12577. ' m: array[1..2,3..4] of longint;',
  12578. ' o: TArrArrInt;',
  12579. ' end;',
  12580. 'begin']);
  12581. ConvertProgram;
  12582. CheckSource('TestRecord_FieldArray',
  12583. LinesToStr([ // statements
  12584. 'rtl.recNewT(this, "TRec", function () {',
  12585. ' this.m$a$clone = function (a) {',
  12586. ' var b = [];',
  12587. ' b.length = 2;',
  12588. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12589. ' return b;',
  12590. ' };',
  12591. ' this.$new = function () {',
  12592. ' var r = Object.create(this);',
  12593. ' r.a = [];',
  12594. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12595. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12596. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12597. ' return r;',
  12598. ' };',
  12599. ' this.$eq = function (b) {',
  12600. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12601. ' };',
  12602. ' this.$assign = function (s) {',
  12603. ' this.a = rtl.arrayRef(s.a);',
  12604. ' this.s = s.s.slice(0);',
  12605. ' this.m = this.m$a$clone(s.m);',
  12606. ' this.o = s.o.slice(0);',
  12607. ' return this;',
  12608. ' };',
  12609. '});',
  12610. '']),
  12611. LinesToStr([ // $mod.$main
  12612. '']));
  12613. end;
  12614. procedure TTestModule.TestRecord_Const;
  12615. begin
  12616. StartProgram(false);
  12617. Add([
  12618. 'type',
  12619. ' TArrInt = array[3..4] of longint;',
  12620. ' TPoint = record x,y: longint; end;',
  12621. ' TRec = record',
  12622. ' i: longint;',
  12623. ' a: array of longint;',
  12624. ' s: array[1..2] of longint;',
  12625. ' m: array[1..2,3..4] of longint;',
  12626. ' p: TPoint;',
  12627. ' end;',
  12628. ' TPoints = array of TPoint;',
  12629. 'const',
  12630. ' r: TRec = (',
  12631. ' i:1;',
  12632. ' a:(2,3);',
  12633. ' s:(4,5);',
  12634. ' m:( (11,12), (13,14) );',
  12635. ' p: (x:21; y:22)',
  12636. ' );',
  12637. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12638. 'begin']);
  12639. ConvertProgram;
  12640. CheckSource('TestRecord_Const',
  12641. LinesToStr([ // statements
  12642. 'rtl.recNewT(this, "TPoint", function () {',
  12643. ' this.x = 0;',
  12644. ' this.y = 0;',
  12645. ' this.$eq = function (b) {',
  12646. ' return (this.x === b.x) && (this.y === b.y);',
  12647. ' };',
  12648. ' this.$assign = function (s) {',
  12649. ' this.x = s.x;',
  12650. ' this.y = s.y;',
  12651. ' return this;',
  12652. ' };',
  12653. '});',
  12654. 'rtl.recNewT(this, "TRec", function () {',
  12655. ' this.i = 0;',
  12656. ' this.m$a$clone = function (a) {',
  12657. ' var b = [];',
  12658. ' b.length = 2;',
  12659. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12660. ' return b;',
  12661. ' };',
  12662. ' this.$new = function () {',
  12663. ' var r = Object.create(this);',
  12664. ' r.a = [];',
  12665. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12666. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12667. ' r.p = $mod.TPoint.$new();',
  12668. ' return r;',
  12669. ' };',
  12670. ' this.$eq = function (b) {',
  12671. ' 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);',
  12672. ' };',
  12673. ' this.$assign = function (s) {',
  12674. ' this.i = s.i;',
  12675. ' this.a = rtl.arrayRef(s.a);',
  12676. ' this.s = s.s.slice(0);',
  12677. ' this.m = this.m$a$clone(s.m);',
  12678. ' this.p.$assign(s.p);',
  12679. ' return this;',
  12680. ' };',
  12681. '});',
  12682. 'this.r = this.TRec.$clone({',
  12683. ' i: 1,',
  12684. ' a: [2, 3],',
  12685. ' s: [4, 5],',
  12686. ' m: [[11, 12], [13, 14]],',
  12687. ' p: this.TPoint.$clone({',
  12688. ' x: 21,',
  12689. ' y: 22',
  12690. ' })',
  12691. '});',
  12692. 'this.p = [this.TPoint.$clone({',
  12693. ' x: 1,',
  12694. ' y: 2',
  12695. '}), this.TPoint.$clone({',
  12696. ' x: 3,',
  12697. ' y: 4',
  12698. '})];',
  12699. '']),
  12700. LinesToStr([ // $mod.$main
  12701. '']));
  12702. end;
  12703. procedure TTestModule.TestRecord_TypecastFail;
  12704. begin
  12705. StartProgram(false);
  12706. Add([
  12707. 'type',
  12708. ' TPoint = record x,y: longint; end;',
  12709. ' TRec = record l: longint end;',
  12710. 'var p: TPoint;',
  12711. 'begin',
  12712. ' if TRec(p).l=2 then ;']);
  12713. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12714. nIllegalTypeConversionTo);
  12715. ConvertProgram;
  12716. end;
  12717. procedure TTestModule.TestRecord_InFunction;
  12718. begin
  12719. StartProgram(false);
  12720. Add([
  12721. 'var TPoint: longint = 3;',
  12722. 'procedure DoIt;',
  12723. 'type',
  12724. ' TPoint = record x,y: longint; end;',
  12725. ' TPoints = array of TPoint;',
  12726. 'var',
  12727. ' r: TPoint;',
  12728. ' p: TPoints;',
  12729. 'begin',
  12730. ' SetLength(p,2);',
  12731. 'end;',
  12732. 'begin']);
  12733. ConvertProgram;
  12734. CheckSource('TestRecord_InFunction',
  12735. LinesToStr([ // statements
  12736. 'this.TPoint = 3;',
  12737. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12738. ' this.x = 0;',
  12739. ' this.y = 0;',
  12740. ' this.$eq = function (b) {',
  12741. ' return (this.x === b.x) && (this.y === b.y);',
  12742. ' };',
  12743. ' this.$assign = function (s) {',
  12744. ' this.x = s.x;',
  12745. ' this.y = s.y;',
  12746. ' return this;',
  12747. ' };',
  12748. '});',
  12749. 'this.DoIt = function () {',
  12750. ' var r = TPoint$1.$new();',
  12751. ' var p = [];',
  12752. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12753. '};',
  12754. '']),
  12755. LinesToStr([ // $mod.$main
  12756. '']));
  12757. end;
  12758. procedure TTestModule.TestRecord_ArrayConstMultiline;
  12759. begin
  12760. StartProgram(false);
  12761. Add([
  12762. '{$mode delphi}',
  12763. 'type',
  12764. ' TBird = record Wing: string; end;',
  12765. 'const',
  12766. ' Birds: array[1..2] of TBird = (',
  12767. ' (Wing: ''''''',
  12768. ' First',
  12769. ' Second',
  12770. ' Third',
  12771. ' ''''''),',
  12772. ' (Wing: ''''''',
  12773. ' Value:=''Im in quotes''; ',
  12774. ' '''''')',
  12775. ' );',
  12776. 'begin']);
  12777. ConvertProgram;
  12778. CheckSource('TestRecord_ArrayConstMultiline',
  12779. LinesToStr([ // statements
  12780. 'rtl.recNewT(this, "TBird", function () {',
  12781. ' this.Wing = "";',
  12782. ' this.$eq = function (b) {',
  12783. ' return this.Wing === b.Wing;',
  12784. ' };',
  12785. ' this.$assign = function (s) {',
  12786. ' this.Wing = s.Wing;',
  12787. ' return this;',
  12788. ' };',
  12789. '});',
  12790. 'this.Birds$a$clone = function (a) {',
  12791. ' var b = [];',
  12792. ' b.length = 2;',
  12793. ' for (var c = 0; c < 2; c++) b[c] = $mod.TBird.$clone(a[c]);',
  12794. ' return b;',
  12795. '};',
  12796. 'this.Birds = [this.TBird.$clone({',
  12797. ' Wing: " First\n Second\n Third"',
  12798. '}), this.TBird.$clone({',
  12799. ' Wing: " Value:=''Im in quotes''; "',
  12800. '})];',
  12801. '']),
  12802. LinesToStr([ // $mod.$main
  12803. '']));
  12804. end;
  12805. procedure TTestModule.TestRecordAnonym_Field;
  12806. begin
  12807. StartProgram(false);
  12808. Add(['',
  12809. 'var Rec: record',
  12810. ' Bold: longint;',
  12811. ' end;',
  12812. 'begin',
  12813. ' rec.bold:=123;',
  12814. ' rec.bold:=rec.bold+7;',
  12815. '']);
  12816. ConvertProgram;
  12817. CheckSource('TestRecordAnonym_Field',
  12818. LinesToStr([ // statements
  12819. 'rtl.recNewT(this, "Rec$a", function () {',
  12820. ' this.Bold = 0;',
  12821. ' this.$eq = function (b) {',
  12822. ' return this.Bold === b.Bold;',
  12823. ' };',
  12824. ' this.$assign = function (s) {',
  12825. ' this.Bold = s.Bold;',
  12826. ' return this;',
  12827. ' };',
  12828. '});',
  12829. 'this.Rec = this.Rec$a.$new();',
  12830. '']),
  12831. LinesToStr([ // $mod.$main
  12832. '$mod.Rec.Bold = 123;',
  12833. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12834. '']));
  12835. end;
  12836. procedure TTestModule.TestRecordAnonym_Assign;
  12837. begin
  12838. StartProgram(false);
  12839. Add(['',
  12840. 'var S,T: record',
  12841. ' Bold: longint;',
  12842. ' end;',
  12843. ' b: boolean;',
  12844. 'begin',
  12845. ' S:=T;',
  12846. ' b:=s=t;',
  12847. '']);
  12848. ConvertProgram;
  12849. CheckSource('TestRecordAnonym_Assign',
  12850. LinesToStr([ // statements
  12851. 'rtl.recNewT(this, "T$a", function () {',
  12852. ' this.Bold = 0;',
  12853. ' this.$eq = function (b) {',
  12854. ' return this.Bold === b.Bold;',
  12855. ' };',
  12856. ' this.$assign = function (s) {',
  12857. ' this.Bold = s.Bold;',
  12858. ' return this;',
  12859. ' };',
  12860. '});',
  12861. 'this.S = this.T$a.$new();',
  12862. 'this.T = this.T$a.$new();',
  12863. 'this.b = false;',
  12864. '']),
  12865. LinesToStr([ // $mod.$main
  12866. '$mod.S.$assign($mod.T);',
  12867. '$mod.b = $mod.S.$eq($mod.T);',
  12868. '']));
  12869. end;
  12870. procedure TTestModule.TestRecordAnonym_Nested;
  12871. begin
  12872. StartProgram(false);
  12873. Add(['',
  12874. 'var S,T: record',
  12875. ' Bold: longint;',
  12876. ' Sub: record',
  12877. ' Color: word;',
  12878. ' end;',
  12879. ' end;',
  12880. ' b: boolean;',
  12881. 'begin',
  12882. ' S:=T;',
  12883. ' S.Sub:=T.Sub;',
  12884. ' S.Sub.Color:=T.Sub.Color+3;',
  12885. ' b:=s=t;',
  12886. ' b:=s.Sub=t.Sub;',
  12887. '']);
  12888. ConvertProgram;
  12889. CheckSource('TestRecordAnonym_Nested',
  12890. LinesToStr([ // statements
  12891. 'rtl.recNewT(this, "T$a", function () {',
  12892. ' this.Bold = 0;',
  12893. ' rtl.recNewT(this, "Sub$a", function () {',
  12894. ' this.Color = 0;',
  12895. ' this.$eq = function (b) {',
  12896. ' return this.Color === b.Color;',
  12897. ' };',
  12898. ' this.$assign = function (s) {',
  12899. ' this.Color = s.Color;',
  12900. ' return this;',
  12901. ' };',
  12902. ' });',
  12903. ' this.$new = function () {',
  12904. ' var r = Object.create(this);',
  12905. ' r.Sub = this.Sub$a.$new();',
  12906. ' return r;',
  12907. ' };',
  12908. ' this.$eq = function (b) {',
  12909. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12910. ' };',
  12911. ' this.$assign = function (s) {',
  12912. ' this.Bold = s.Bold;',
  12913. ' this.Sub.$assign(s.Sub);',
  12914. ' return this;',
  12915. ' };',
  12916. '}, true);',
  12917. 'this.S = this.T$a.$new();',
  12918. 'this.T = this.T$a.$new();',
  12919. 'this.b = false;',
  12920. '']),
  12921. LinesToStr([ // $mod.$main
  12922. '$mod.S.$assign($mod.T);',
  12923. '$mod.S.Sub.$assign($mod.T.Sub);',
  12924. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12925. '$mod.b = $mod.S.$eq($mod.T);',
  12926. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12927. '']));
  12928. end;
  12929. procedure TTestModule.TestRecordAnonym_Const;
  12930. begin
  12931. StartProgram(false);
  12932. Add(['',
  12933. 'var T: record',
  12934. ' Bold: longint;',
  12935. ' Sub: record',
  12936. ' Color: word;',
  12937. ' end;',
  12938. ' end = (Bold: 2; Sub: (Color: 3));',
  12939. 'begin',
  12940. '']);
  12941. ConvertProgram;
  12942. CheckSource('TestRecordAnonym_Const',
  12943. LinesToStr([ // statements
  12944. 'rtl.recNewT(this, "T$a", function () {',
  12945. ' this.Bold = 0;',
  12946. ' rtl.recNewT(this, "Sub$a", function () {',
  12947. ' this.Color = 0;',
  12948. ' this.$eq = function (b) {',
  12949. ' return this.Color === b.Color;',
  12950. ' };',
  12951. ' this.$assign = function (s) {',
  12952. ' this.Color = s.Color;',
  12953. ' return this;',
  12954. ' };',
  12955. ' });',
  12956. ' this.$new = function () {',
  12957. ' var r = Object.create(this);',
  12958. ' r.Sub = this.Sub$a.$new();',
  12959. ' return r;',
  12960. ' };',
  12961. ' this.$eq = function (b) {',
  12962. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12963. ' };',
  12964. ' this.$assign = function (s) {',
  12965. ' this.Bold = s.Bold;',
  12966. ' this.Sub.$assign(s.Sub);',
  12967. ' return this;',
  12968. ' };',
  12969. '}, true);',
  12970. 'this.T = this.T$a.$clone({',
  12971. ' Bold: 2,',
  12972. ' Sub: this.T$a.Sub$a.$clone({',
  12973. ' Color: 3',
  12974. ' })',
  12975. '});',
  12976. '']),
  12977. LinesToStr([ // $mod.$main
  12978. '']));
  12979. end;
  12980. procedure TTestModule.TestRecordAnonym_InFunction;
  12981. begin
  12982. StartProgram(false);
  12983. Add(['',
  12984. 'procedure Fly;',
  12985. 'var T: record',
  12986. ' Bold: longint;',
  12987. ' Sub: record',
  12988. ' Color: word;',
  12989. ' end;',
  12990. ' end = (Bold: 2; Sub: (Color: 3));',
  12991. 'begin',
  12992. 'end;',
  12993. 'begin',
  12994. '']);
  12995. ConvertProgram;
  12996. CheckSource('TestRecordAnonym_InFunction',
  12997. LinesToStr([ // statements
  12998. 'var T$a = rtl.recNewT(null, "", function () {',
  12999. ' this.Bold = 0;',
  13000. ' rtl.recNewT(this, "Sub$a", function () {',
  13001. ' this.Color = 0;',
  13002. ' this.$eq = function (b) {',
  13003. ' return this.Color === b.Color;',
  13004. ' };',
  13005. ' this.$assign = function (s) {',
  13006. ' this.Color = s.Color;',
  13007. ' return this;',
  13008. ' };',
  13009. ' });',
  13010. ' this.$new = function () {',
  13011. ' var r = Object.create(this);',
  13012. ' r.Sub = this.Sub$a.$new();',
  13013. ' return r;',
  13014. ' };',
  13015. ' this.$eq = function (b) {',
  13016. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  13017. ' };',
  13018. ' this.$assign = function (s) {',
  13019. ' this.Bold = s.Bold;',
  13020. ' this.Sub.$assign(s.Sub);',
  13021. ' return this;',
  13022. ' };',
  13023. '}, true);',
  13024. 'this.Fly = function () {',
  13025. ' var T = T$a.$clone({',
  13026. ' Bold: 2,',
  13027. ' Sub: T$a.Sub$a.$clone({',
  13028. ' Color: 3',
  13029. ' })',
  13030. ' });',
  13031. '};',
  13032. '']),
  13033. LinesToStr([ // $mod.$main
  13034. '']));
  13035. end;
  13036. procedure TTestModule.TestAdvRecord_Function;
  13037. begin
  13038. StartProgram(false);
  13039. Parser.Options:=Parser.Options+[po_cassignments];
  13040. Add([
  13041. '{$modeswitch AdvancedRecords}',
  13042. 'type',
  13043. ' TPoint = record',
  13044. ' x,y: word;',
  13045. ' function Add(const apt: TPoint): TPoint;',
  13046. ' end;',
  13047. 'function TPoint.Add(const apt: TPoint): TPoint;',
  13048. 'begin',
  13049. ' Result:=Self;',
  13050. ' Result.x+=apt.x;',
  13051. ' Result.y:=Result.y+apt.y;',
  13052. ' Self:=apt;',
  13053. 'end;',
  13054. 'var p,q: TPoint;',
  13055. 'begin',
  13056. ' p.add(q);',
  13057. ' p:=default(TPoint);',
  13058. ' p:=q;',
  13059. '']);
  13060. ConvertProgram;
  13061. CheckSource('TestAdvRecord_Function',
  13062. LinesToStr([ // statements
  13063. 'rtl.recNewT(this, "TPoint", function () {',
  13064. ' this.x = 0;',
  13065. ' this.y = 0;',
  13066. ' this.$eq = function (b) {',
  13067. ' return (this.x === b.x) && (this.y === b.y);',
  13068. ' };',
  13069. ' this.$assign = function (s) {',
  13070. ' this.x = s.x;',
  13071. ' this.y = s.y;',
  13072. ' return this;',
  13073. ' };',
  13074. ' this.Add = function (apt) {',
  13075. ' var Result = $mod.TPoint.$new();',
  13076. ' Result.$assign(this);',
  13077. ' Result.x += apt.x;',
  13078. ' Result.y = Result.y + apt.y;',
  13079. ' this.$assign(apt);',
  13080. ' return Result;',
  13081. ' };',
  13082. '});',
  13083. 'this.p = this.TPoint.$new();',
  13084. 'this.q = this.TPoint.$new();',
  13085. '']),
  13086. LinesToStr([ // $mod.$main
  13087. '$mod.p.Add($mod.q);',
  13088. '$mod.p.$assign($mod.TPoint.$new());',
  13089. '$mod.p.$assign($mod.q);',
  13090. '']));
  13091. end;
  13092. procedure TTestModule.TestAdvRecord_Property;
  13093. begin
  13094. StartProgram(false);
  13095. Add([
  13096. '{$modeswitch AdvancedRecords}',
  13097. 'type',
  13098. ' TPoint = record',
  13099. ' x,y: word;',
  13100. ' strict private',
  13101. ' function GetSize: longword;',
  13102. ' procedure SetSize(Value: longword);',
  13103. ' public',
  13104. ' property Size: longword read GetSize write SetSize;',
  13105. ' property Left: word read x write y;',
  13106. ' end;',
  13107. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13108. 'function TPoint.GetSize: longword;',
  13109. 'begin',
  13110. ' x:=y;',
  13111. ' Size:=Size;',
  13112. ' Left:=Left;',
  13113. 'end;',
  13114. 'procedure TPoint.SetSize(Value: longword);',
  13115. 'begin',
  13116. 'end;',
  13117. 'var p,q: TPoint;',
  13118. 'begin',
  13119. ' p.Size:=q.Size;',
  13120. ' p.Left:=q.Left;',
  13121. '']);
  13122. ConvertProgram;
  13123. CheckSource('TestAdvRecord_Property',
  13124. LinesToStr([ // statements
  13125. 'rtl.recNewT(this, "TPoint", function () {',
  13126. ' this.x = 0;',
  13127. ' this.y = 0;',
  13128. ' this.$eq = function (b) {',
  13129. ' return (this.x === b.x) && (this.y === b.y);',
  13130. ' };',
  13131. ' this.$assign = function (s) {',
  13132. ' this.x = s.x;',
  13133. ' this.y = s.y;',
  13134. ' return this;',
  13135. ' };',
  13136. ' this.GetSize = function () {',
  13137. ' var Result = 0;',
  13138. ' this.x = this.y;',
  13139. ' this.SetSize(this.GetSize());',
  13140. ' this.y = this.x;',
  13141. ' return Result;',
  13142. ' };',
  13143. ' this.SetSize = function (Value) {',
  13144. ' };',
  13145. '});',
  13146. 'this.SetSize = function (Value) {',
  13147. '};',
  13148. 'this.p = this.TPoint.$new();',
  13149. 'this.q = this.TPoint.$new();',
  13150. '']),
  13151. LinesToStr([ // $mod.$main
  13152. '$mod.p.SetSize($mod.q.GetSize());',
  13153. '$mod.p.y = $mod.q.x;',
  13154. '']));
  13155. end;
  13156. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13157. begin
  13158. StartProgram(false);
  13159. Add([
  13160. '{$modeswitch AdvancedRecords}',
  13161. 'type',
  13162. ' TPoint = record',
  13163. ' strict private',
  13164. ' function GetItems(Index: word): word;',
  13165. ' procedure SetItems(Index: word; Value: word);',
  13166. ' public',
  13167. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13168. ' end;',
  13169. 'function TPoint.GetItems(Index: word): word;',
  13170. 'begin',
  13171. ' Items[index]:=Items[index];',
  13172. ' self.Items[index]:=self.Items[index];',
  13173. 'end;',
  13174. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13175. 'begin',
  13176. 'end;',
  13177. 'var p: TPoint;',
  13178. 'begin',
  13179. ' p[1]:=p[2];',
  13180. ' p.Items[3]:=p.Items[4];',
  13181. '']);
  13182. ConvertProgram;
  13183. CheckSource('TestAdvRecord_PropertyDefault',
  13184. LinesToStr([ // statements
  13185. 'rtl.recNewT(this, "TPoint", function () {',
  13186. ' this.$eq = function (b) {',
  13187. ' return true;',
  13188. ' };',
  13189. ' this.$assign = function (s) {',
  13190. ' return this;',
  13191. ' };',
  13192. ' this.GetItems = function (Index) {',
  13193. ' var Result = 0;',
  13194. ' this.SetItems(Index, this.GetItems(Index));',
  13195. ' this.SetItems(Index, this.GetItems(Index));',
  13196. ' return Result;',
  13197. ' };',
  13198. ' this.SetItems = function (Index, Value) {',
  13199. ' };',
  13200. '});',
  13201. 'this.p = this.TPoint.$new();',
  13202. '']),
  13203. LinesToStr([ // $mod.$main
  13204. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13205. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13206. '']));
  13207. end;
  13208. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13209. begin
  13210. StartProgram(false);
  13211. Add([
  13212. '{$modeswitch AdvancedRecords}',
  13213. 'type',
  13214. ' TRec = record',
  13215. ' class var',
  13216. ' Fx: longint;',
  13217. ' Fy: longint;',
  13218. ' class function GetInt: longint; static;',
  13219. ' class procedure SetInt(Value: longint); static;',
  13220. ' class procedure DoIt; static;',
  13221. ' class property IntA: longint read Fx write Fy;',
  13222. ' class property IntB: longint read GetInt write SetInt;',
  13223. ' end;',
  13224. 'class function trec.getint: longint;',
  13225. 'begin',
  13226. ' result:=fx;',
  13227. 'end;',
  13228. 'class procedure trec.setint(value: longint);',
  13229. 'begin',
  13230. 'end;',
  13231. 'class procedure trec.doit;',
  13232. 'begin',
  13233. ' IntA:=IntA+1;',
  13234. ' IntB:=IntB+1;',
  13235. 'end;',
  13236. 'var r: trec;',
  13237. 'begin',
  13238. ' trec.inta:=trec.inta+1;',
  13239. ' if trec.intb=2 then;',
  13240. ' trec.intb:=trec.intb+2;',
  13241. ' trec.setint(trec.inta);',
  13242. ' r.inta:=r.inta+1;',
  13243. ' if r.intb=2 then;',
  13244. ' r.intb:=r.intb+2;',
  13245. ' r.setint(r.inta);']);
  13246. ConvertProgram;
  13247. CheckSource('TestAdvRecord_Property_ClassMethod',
  13248. LinesToStr([ // statements
  13249. 'rtl.recNewT(this, "TRec", function () {',
  13250. ' this.Fx = 0;',
  13251. ' this.Fy = 0;',
  13252. ' this.$eq = function (b) {',
  13253. ' return true;',
  13254. ' };',
  13255. ' this.$assign = function (s) {',
  13256. ' return this;',
  13257. ' };',
  13258. ' this.GetInt = function () {',
  13259. ' var Result = 0;',
  13260. ' Result = $mod.TRec.Fx;',
  13261. ' return Result;',
  13262. ' };',
  13263. ' this.SetInt = function (Value) {',
  13264. ' };',
  13265. ' this.DoIt = function () {',
  13266. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13267. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13268. ' };',
  13269. '}, true);',
  13270. 'this.r = this.TRec.$new();',
  13271. '']),
  13272. LinesToStr([ // $mod.$main
  13273. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13274. 'if ($mod.TRec.GetInt() === 2) ;',
  13275. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13276. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13277. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13278. 'if ($mod.TRec.GetInt() === 2) ;',
  13279. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13280. '$mod.TRec.SetInt($mod.r.Fx);',
  13281. '']));
  13282. end;
  13283. procedure TTestModule.TestAdvRecord_Const;
  13284. begin
  13285. StartProgram(false);
  13286. Add([
  13287. '{$modeswitch AdvancedRecords}',
  13288. 'type',
  13289. ' TArrInt = array[3..4] of longint;',
  13290. ' TPoint = record',
  13291. ' x,y: longint;',
  13292. ' class var Count: nativeint;',
  13293. ' end;',
  13294. ' TRec = record',
  13295. ' i: longint;',
  13296. ' a: array of longint;',
  13297. ' s: array[1..2] of longint;',
  13298. ' m: array[1..2,3..4] of longint;',
  13299. ' p: TPoint;',
  13300. ' end;',
  13301. ' TPoints = array of TPoint;',
  13302. 'const',
  13303. ' r: TRec = (',
  13304. ' i:1;',
  13305. ' a:(2,3);',
  13306. ' s:(4,5);',
  13307. ' m:( (11,12), (13,14) );',
  13308. ' p: (x:21)',
  13309. ' );',
  13310. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13311. 'begin']);
  13312. ConvertProgram;
  13313. CheckSource('TestAdvRecord_Const',
  13314. LinesToStr([ // statements
  13315. 'rtl.recNewT(this, "TPoint", function () {',
  13316. ' this.x = 0;',
  13317. ' this.y = 0;',
  13318. ' this.Count = 0;',
  13319. ' this.$eq = function (b) {',
  13320. ' return (this.x === b.x) && (this.y === b.y);',
  13321. ' };',
  13322. ' this.$assign = function (s) {',
  13323. ' this.x = s.x;',
  13324. ' this.y = s.y;',
  13325. ' return this;',
  13326. ' };',
  13327. '}, true);',
  13328. 'rtl.recNewT(this, "TRec", function () {',
  13329. ' this.i = 0;',
  13330. ' this.m$a$clone = function (a) {',
  13331. ' var b = [];',
  13332. ' b.length = 2;',
  13333. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13334. ' return b;',
  13335. ' };',
  13336. ' this.$new = function () {',
  13337. ' var r = Object.create(this);',
  13338. ' r.a = [];',
  13339. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13340. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13341. ' r.p = $mod.TPoint.$new();',
  13342. ' return r;',
  13343. ' };',
  13344. ' this.$eq = function (b) {',
  13345. ' 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);',
  13346. ' };',
  13347. ' this.$assign = function (s) {',
  13348. ' this.i = s.i;',
  13349. ' this.a = rtl.arrayRef(s.a);',
  13350. ' this.s = s.s.slice(0);',
  13351. ' this.m = this.m$a$clone(s.m);',
  13352. ' this.p.$assign(s.p);',
  13353. ' return this;',
  13354. ' };',
  13355. '});',
  13356. 'this.r = this.TRec.$clone({',
  13357. ' i: 1,',
  13358. ' a: [2, 3],',
  13359. ' s: [4, 5],',
  13360. ' m: [[11, 12], [13, 14]],',
  13361. ' p: this.TPoint.$clone({',
  13362. ' x: 21,',
  13363. ' y: 0',
  13364. ' })',
  13365. '});',
  13366. 'this.p = [this.TPoint.$clone({',
  13367. ' x: 1,',
  13368. ' y: 2',
  13369. '}), this.TPoint.$clone({',
  13370. ' x: 3,',
  13371. ' y: 4',
  13372. '})];',
  13373. '']),
  13374. LinesToStr([ // $mod.$main
  13375. '']));
  13376. end;
  13377. procedure TTestModule.TestAdvRecord_ExternalField;
  13378. begin
  13379. StartProgram(false);
  13380. Add([
  13381. '{$modeswitch AdvancedRecords}',
  13382. '{$modeswitch externalclass}',
  13383. 'type',
  13384. ' TCar = record',
  13385. ' public',
  13386. ' Intern: longint external name ''$Intern'';',
  13387. ' Intern2: longint external name ''$Intern2'';',
  13388. ' Bracket: longint external name ''["A B"]'';',
  13389. ' procedure DoIt;',
  13390. ' end;',
  13391. 'procedure tcar.doit;',
  13392. 'begin',
  13393. ' Intern:=Intern+1;',
  13394. ' Intern2:=Intern2+2;',
  13395. ' Bracket:=Bracket+3;',
  13396. 'end;',
  13397. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13398. 'begin',
  13399. ' Rec.intern:=Rec.intern+1;',
  13400. ' Rec.intern2:=Rec.intern2+2;',
  13401. ' Rec.Bracket:=Rec.Bracket+3;',
  13402. ' with Rec do begin',
  13403. ' intern:=intern+1;',
  13404. ' intern2:=intern2+2;',
  13405. ' Bracket:=Bracket+3;',
  13406. ' end;']);
  13407. ConvertProgram;
  13408. CheckSource('TestAdvRecord_ExternalField',
  13409. LinesToStr([ // statements
  13410. 'rtl.recNewT(this, "TCar", function () {',
  13411. ' this.$eq = function (b) {',
  13412. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13413. ' };',
  13414. ' this.$assign = function (s) {',
  13415. ' this.$Intern = s.$Intern;',
  13416. ' this.$Intern2 = s.$Intern2;',
  13417. ' this["A B"] = s["A B"];',
  13418. ' return this;',
  13419. ' };',
  13420. ' this.DoIt = function () {',
  13421. ' this.$Intern = this.$Intern + 1;',
  13422. ' this.$Intern2 = this.$Intern2 + 2;',
  13423. ' this["A B"] = this["A B"] + 3;',
  13424. ' };',
  13425. '});',
  13426. 'this.Rec = this.TCar.$clone({',
  13427. ' $Intern: 11,',
  13428. ' $Intern2: 12,',
  13429. ' "A B": 13',
  13430. '});',
  13431. '']),
  13432. LinesToStr([ // $mod.$main
  13433. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13434. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13435. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13436. 'var $with = $mod.Rec;',
  13437. '$with.$Intern = $with.$Intern + 1;',
  13438. '$with.$Intern2 = $with.$Intern2 + 2;',
  13439. '$with["A B"] = $with["A B"] + 3;',
  13440. '']));
  13441. end;
  13442. procedure TTestModule.TestAdvRecord_SubRecord;
  13443. begin
  13444. StartProgram(false);
  13445. Add([
  13446. '{$modeswitch AdvancedRecords}',
  13447. 'type',
  13448. ' TRec = record',
  13449. ' type',
  13450. ' TPoint = record',
  13451. ' x,y: longint;',
  13452. ' class var Count: nativeint;',
  13453. ' procedure DoIt;',
  13454. ' class procedure DoThat; static;',
  13455. ' end;',
  13456. ' var',
  13457. ' i: longint;',
  13458. ' p: TPoint;',
  13459. ' procedure DoSome;',
  13460. ' end;',
  13461. 'const',
  13462. ' r: TRec = (',
  13463. ' i:1;',
  13464. ' p: (x:21;y:22)',
  13465. ' );',
  13466. 'procedure TRec.DoSome;',
  13467. 'begin',
  13468. ' p.x:=p.y+1;',
  13469. ' p.Count:=p.Count+2;',
  13470. 'end;',
  13471. 'procedure TRec.TPoint.DoIt;',
  13472. 'begin',
  13473. ' Count:=Count+3;',
  13474. 'end;',
  13475. 'class procedure TRec.TPoint.DoThat;',
  13476. 'begin',
  13477. ' Count:=Count+4;',
  13478. 'end;',
  13479. 'begin']);
  13480. ConvertProgram;
  13481. CheckSource('TestAdvRecord_SubRecord',
  13482. LinesToStr([ // statements
  13483. 'rtl.recNewT(this, "TRec", function () {',
  13484. ' rtl.recNewT(this, "TPoint", function () {',
  13485. ' this.x = 0;',
  13486. ' this.y = 0;',
  13487. ' this.Count = 0;',
  13488. ' this.$eq = function (b) {',
  13489. ' return (this.x === b.x) && (this.y === b.y);',
  13490. ' };',
  13491. ' this.$assign = function (s) {',
  13492. ' this.x = s.x;',
  13493. ' this.y = s.y;',
  13494. ' return this;',
  13495. ' };',
  13496. ' this.DoIt = function () {',
  13497. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13498. ' };',
  13499. ' this.DoThat = function () {',
  13500. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13501. ' };',
  13502. ' }, true);',
  13503. ' this.i = 0;',
  13504. ' this.$new = function () {',
  13505. ' var r = Object.create(this);',
  13506. ' r.p = this.TPoint.$new();',
  13507. ' return r;',
  13508. ' };',
  13509. ' this.$eq = function (b) {',
  13510. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13511. ' };',
  13512. ' this.$assign = function (s) {',
  13513. ' this.i = s.i;',
  13514. ' this.p.$assign(s.p);',
  13515. ' return this;',
  13516. ' };',
  13517. ' this.DoSome = function () {',
  13518. ' this.p.x = this.p.y + 1;',
  13519. ' this.TPoint.Count = this.p.Count + 2;',
  13520. ' };',
  13521. '}, true);',
  13522. 'this.r = this.TRec.$clone({',
  13523. ' i: 1,',
  13524. ' p: this.TRec.TPoint.$clone({',
  13525. ' x: 21,',
  13526. ' y: 22',
  13527. ' })',
  13528. '});',
  13529. '']),
  13530. LinesToStr([ // $mod.$main
  13531. '']));
  13532. end;
  13533. procedure TTestModule.TestAdvRecord_SubClass;
  13534. begin
  13535. StartProgram(false);
  13536. Add([
  13537. '{$modeswitch AdvancedRecords}',
  13538. 'type',
  13539. ' TObject = class end;',
  13540. ' TPoint = record',
  13541. ' type',
  13542. ' TBird = class',
  13543. ' procedure DoIt;',
  13544. ' class procedure Glob;',
  13545. ' end;',
  13546. ' procedure DoIt(b: TBird);',
  13547. ' end;',
  13548. 'procedure TPoint.TBird.DoIt;',
  13549. 'begin',
  13550. ' doit;',
  13551. ' self.doit;',
  13552. ' glob;',
  13553. ' self.glob;',
  13554. 'end;',
  13555. 'class procedure TPoint.TBird.Glob;',
  13556. 'begin',
  13557. ' glob;',
  13558. ' self.glob;',
  13559. 'end;',
  13560. 'procedure TPoint.DoIt(b: TBird);',
  13561. 'begin',
  13562. ' b.doit;',
  13563. ' b.glob;',
  13564. ' TBird.glob;',
  13565. 'end;',
  13566. 'begin',
  13567. '']);
  13568. ConvertProgram;
  13569. CheckSource('TestAdvRecord_SubClass',
  13570. LinesToStr([ // statements
  13571. 'rtl.createClass(this, "TObject", null, function () {',
  13572. ' this.$init = function () {',
  13573. ' };',
  13574. ' this.$final = function () {',
  13575. ' };',
  13576. '});',
  13577. 'rtl.recNewT(this, "TPoint", function () {',
  13578. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13579. ' this.DoIt = function () {',
  13580. ' this.DoIt();',
  13581. ' this.DoIt();',
  13582. ' this.$class.Glob();',
  13583. ' this.$class.Glob();',
  13584. ' };',
  13585. ' this.Glob = function () {',
  13586. ' this.Glob();',
  13587. ' this.Glob();',
  13588. ' };',
  13589. ' }, "TPoint.TBird");',
  13590. ' this.$eq = function (b) {',
  13591. ' return true;',
  13592. ' };',
  13593. ' this.$assign = function (s) {',
  13594. ' return this;',
  13595. ' };',
  13596. ' this.DoIt = function (b) {',
  13597. ' b.DoIt();',
  13598. ' b.$class.Glob();',
  13599. ' this.TBird.Glob();',
  13600. ' };',
  13601. '}, true);',
  13602. '']),
  13603. LinesToStr([ // $mod.$main
  13604. '']));
  13605. end;
  13606. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13607. begin
  13608. StartProgram(false);
  13609. Add([
  13610. '{$modeswitch AdvancedRecords}',
  13611. 'type',
  13612. ' IUnknown = interface end;',
  13613. ' TPoint = record',
  13614. ' type IBird = interface end;',
  13615. ' end;',
  13616. 'begin',
  13617. '']);
  13618. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13619. nNotYetImplemented);
  13620. ParseProgram;
  13621. end;
  13622. procedure TTestModule.TestAdvRecord_Constructor;
  13623. begin
  13624. StartProgram(false);
  13625. Add([
  13626. '{$modeswitch AdvancedRecords}',
  13627. 'type',
  13628. ' TPoint = record',
  13629. ' x,y: longint;',
  13630. ' class procedure Run(w: longint = 13); static;',
  13631. ' constructor Create(ax: longint; ay: longint = -1);',
  13632. ' end;',
  13633. 'class procedure tpoint.run(w: longint);',
  13634. 'begin',
  13635. ' run;',
  13636. ' run();',
  13637. 'end;',
  13638. 'constructor tpoint.create(ax,ay: longint);',
  13639. 'begin',
  13640. ' x:=ax;',
  13641. ' self.y:=ay;',
  13642. ' run;',
  13643. ' run(ax);',
  13644. 'end;',
  13645. 'var r: TPoint;',
  13646. 'begin',
  13647. ' r:=TPoint.Create(1,2);',
  13648. ' with TPoint do r:=Create(1,2);',
  13649. ' r.Create(3);',
  13650. ' r:=r.Create(4);',
  13651. '']);
  13652. ConvertProgram;
  13653. CheckSource('TestAdvRecord_Constructor',
  13654. LinesToStr([ // statements
  13655. 'rtl.recNewT(this, "TPoint", function () {',
  13656. ' this.x = 0;',
  13657. ' this.y = 0;',
  13658. ' this.$eq = function (b) {',
  13659. ' return (this.x === b.x) && (this.y === b.y);',
  13660. ' };',
  13661. ' this.$assign = function (s) {',
  13662. ' this.x = s.x;',
  13663. ' this.y = s.y;',
  13664. ' return this;',
  13665. ' };',
  13666. ' this.Run = function (w) {',
  13667. ' $mod.TPoint.Run(13);',
  13668. ' $mod.TPoint.Run(13);',
  13669. ' };',
  13670. ' this.Create = function (ax, ay) {',
  13671. ' this.x = ax;',
  13672. ' this.y = ay;',
  13673. ' this.Run(13);',
  13674. ' this.Run(ax);',
  13675. ' return this;',
  13676. ' };',
  13677. '});',
  13678. 'this.r = this.TPoint.$new();',
  13679. '']),
  13680. LinesToStr([ // $mod.$main
  13681. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13682. 'var $with = $mod.TPoint;',
  13683. '$mod.r.$assign($with.$new().Create(1, 2));',
  13684. '$mod.r.Create(3, -1);',
  13685. '$mod.r.$assign($mod.r.Create(4, -1));',
  13686. '']));
  13687. end;
  13688. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13689. begin
  13690. StartProgram(false);
  13691. Add([
  13692. '{$modeswitch AdvancedRecords}',
  13693. 'type',
  13694. ' TPoint = record',
  13695. ' class var x: longint;',
  13696. ' class procedure Fly; static;',
  13697. ' class constructor Init;',
  13698. ' end;',
  13699. 'var count: word;',
  13700. 'class procedure Tpoint.Fly;',
  13701. 'begin',
  13702. 'end;',
  13703. 'class constructor tpoint.init;',
  13704. 'begin',
  13705. ' count:=count+1;',
  13706. ' x:=x+3;',
  13707. ' tpoint.x:=tpoint.x+4;',
  13708. ' fly;',
  13709. ' tpoint.fly;',
  13710. 'end;',
  13711. 'var r: TPoint;',
  13712. 'begin',
  13713. ' r.x:=r.x+10;',
  13714. ' r.Fly;',
  13715. ' r.Fly();',
  13716. '']);
  13717. ConvertProgram;
  13718. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13719. LinesToStr([ // statements
  13720. 'rtl.recNewT(this, "TPoint", function () {',
  13721. ' this.x = 0;',
  13722. ' this.$eq = function (b) {',
  13723. ' return true;',
  13724. ' };',
  13725. ' this.$assign = function (s) {',
  13726. ' return this;',
  13727. ' };',
  13728. ' this.Fly = function () {',
  13729. ' };',
  13730. '}, true);',
  13731. 'this.count = 0;',
  13732. 'this.r = this.TPoint.$new();',
  13733. '']),
  13734. LinesToStr([ // $mod.$main
  13735. '(function () {',
  13736. ' $mod.count = $mod.count + 1;',
  13737. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13738. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13739. ' $mod.TPoint.Fly();',
  13740. ' $mod.TPoint.Fly();',
  13741. '})();',
  13742. '$mod.TPoint.x = $mod.r.x + 10;',
  13743. '$mod.TPoint.Fly();',
  13744. '$mod.TPoint.Fly();',
  13745. '']));
  13746. end;
  13747. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13748. begin
  13749. StartUnit(false);
  13750. Add([
  13751. 'interface',
  13752. '{$modeswitch AdvancedRecords}',
  13753. 'type',
  13754. ' TPoint = record',
  13755. ' class var x: longint;',
  13756. ' class procedure Fly; static;',
  13757. ' class constructor Init;',
  13758. ' end;',
  13759. 'implementation',
  13760. 'var count: word;',
  13761. 'class procedure Tpoint.Fly;',
  13762. 'begin',
  13763. 'end;',
  13764. 'class constructor tpoint.init;',
  13765. 'begin',
  13766. ' count:=count+1;',
  13767. ' x:=3;',
  13768. ' tpoint.x:=4;',
  13769. ' fly;',
  13770. ' tpoint.fly;',
  13771. 'end;',
  13772. '']);
  13773. ConvertUnit;
  13774. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13775. LinesToStr([ // statements
  13776. 'var $impl = $mod.$impl;',
  13777. 'rtl.recNewT(this, "TPoint", function () {',
  13778. ' this.x = 0;',
  13779. ' this.$eq = function (b) {',
  13780. ' return true;',
  13781. ' };',
  13782. ' this.$assign = function (s) {',
  13783. ' return this;',
  13784. ' };',
  13785. ' this.Fly = function () {',
  13786. ' };',
  13787. '}, true);',
  13788. '']),
  13789. LinesToStr([ // $mod.$init
  13790. '(function () {',
  13791. ' $impl.count = $impl.count + 1;',
  13792. ' $mod.TPoint.x = 3;',
  13793. ' $mod.TPoint.x = 4;',
  13794. ' $mod.TPoint.Fly();',
  13795. ' $mod.TPoint.Fly();',
  13796. '})();',
  13797. '']),
  13798. LinesToStr([ // $mod.$main
  13799. '$impl.count = 0;',
  13800. '']));
  13801. end;
  13802. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13803. begin
  13804. StartProgram(false);
  13805. Add(['type',
  13806. ' TObject = class',
  13807. ' public',
  13808. ' constructor Create;',
  13809. ' destructor Destroy;',
  13810. ' end;',
  13811. ' TBird = TObject;',
  13812. 'constructor tobject.create;',
  13813. 'begin end;',
  13814. 'destructor tobject.destroy;',
  13815. 'begin end;',
  13816. 'var Obj: tobject;',
  13817. 'begin',
  13818. ' obj:=tobject.create;',
  13819. ' obj:=tobject.create();',
  13820. ' obj:=tbird.create;',
  13821. ' obj:=tbird.create();',
  13822. ' obj:=obj.create();',
  13823. ' obj.destroy;',
  13824. '']);
  13825. ConvertProgram;
  13826. CheckSource('TestClass_TObjectDefaultConstructor',
  13827. LinesToStr([ // statements
  13828. 'rtl.createClass(this,"TObject",null,function(){',
  13829. ' this.$init = function () {',
  13830. ' };',
  13831. ' this.$final = function () {',
  13832. ' };',
  13833. ' this.Create = function(){',
  13834. ' return this;',
  13835. ' };',
  13836. ' this.Destroy = function(){',
  13837. ' };',
  13838. '});',
  13839. 'this.Obj = null;'
  13840. ]),
  13841. LinesToStr([ // $mod.$main
  13842. '$mod.Obj = $mod.TObject.$create("Create");',
  13843. '$mod.Obj = $mod.TObject.$create("Create");',
  13844. '$mod.Obj = $mod.TObject.$create("Create");',
  13845. '$mod.Obj = $mod.TObject.$create("Create");',
  13846. '$mod.Obj = $mod.Obj.Create();',
  13847. '$mod.Obj.$destroy("Destroy");',
  13848. '']));
  13849. end;
  13850. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13851. begin
  13852. StartProgram(false);
  13853. Add('type');
  13854. Add(' TObject = class');
  13855. Add(' public');
  13856. Add(' constructor Create(Par: longint);');
  13857. Add(' end;');
  13858. Add('constructor tobject.create(par: longint);');
  13859. Add('begin end;');
  13860. Add('var Obj: tobject;');
  13861. Add('begin');
  13862. Add(' obj:=tobject.create(3);');
  13863. ConvertProgram;
  13864. CheckSource('TestClass_TObjectConstructorWithParams',
  13865. LinesToStr([ // statements
  13866. 'rtl.createClass(this,"TObject",null,function(){',
  13867. ' this.$init = function () {',
  13868. ' };',
  13869. ' this.$final = function () {',
  13870. ' };',
  13871. ' this.Create = function(Par){',
  13872. ' return this;',
  13873. ' };',
  13874. '});',
  13875. 'this.Obj = null;'
  13876. ]),
  13877. LinesToStr([ // $mod.$main
  13878. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13879. ]));
  13880. end;
  13881. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13882. begin
  13883. StartProgram(false);
  13884. Add('type');
  13885. Add(' TObject = class');
  13886. Add(' public');
  13887. Add(' constructor Create;');
  13888. Add(' end;');
  13889. Add(' TTest = class(TObject)');
  13890. Add(' public');
  13891. Add(' constructor Create(const Par: longint = 1);');
  13892. Add(' end;');
  13893. Add('constructor tobject.create;');
  13894. Add('begin end;');
  13895. Add('constructor ttest.create(const par: longint);');
  13896. Add('begin end;');
  13897. Add('var t: ttest;');
  13898. Add('begin');
  13899. Add(' t:=ttest.create;');
  13900. Add(' t:=ttest.create(2);');
  13901. ConvertProgram;
  13902. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13903. LinesToStr([ // statements
  13904. 'rtl.createClass(this,"TObject",null,function(){',
  13905. ' this.$init = function () {',
  13906. ' };',
  13907. ' this.$final = function () {',
  13908. ' };',
  13909. ' this.Create = function(){',
  13910. ' return this;',
  13911. ' };',
  13912. '});',
  13913. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13914. ' this.Create$1 = function (Par) {',
  13915. ' return this;',
  13916. ' };',
  13917. '});',
  13918. 'this.t = null;'
  13919. ]),
  13920. LinesToStr([ // $mod.$main
  13921. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13922. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13923. ]));
  13924. end;
  13925. procedure TTestModule.TestClass_Var;
  13926. begin
  13927. StartProgram(false);
  13928. Add([
  13929. 'type',
  13930. ' TObject = class',
  13931. ' public',
  13932. ' vI: longint;',
  13933. ' constructor Create(Par: longint);',
  13934. ' end;',
  13935. 'constructor tobject.create(par: longint);',
  13936. 'begin',
  13937. ' vi:=par+3',
  13938. 'end;',
  13939. 'var Obj: tobject;',
  13940. 'begin',
  13941. ' obj:=tobject.create(4);',
  13942. ' obj.vi:=obj.VI+5;']);
  13943. ConvertProgram;
  13944. CheckSource('TestClass_Var',
  13945. LinesToStr([ // statements
  13946. 'rtl.createClass(this,"TObject",null,function(){',
  13947. ' this.$init = function () {',
  13948. ' this.vI = 0;',
  13949. ' };',
  13950. ' this.$final = function () {',
  13951. ' };',
  13952. ' this.Create = function(Par){',
  13953. ' this.vI = Par+3;',
  13954. ' return this;',
  13955. ' };',
  13956. '});',
  13957. 'this.Obj = null;'
  13958. ]),
  13959. LinesToStr([ // $mod.$main
  13960. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13961. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13962. ]));
  13963. end;
  13964. procedure TTestModule.TestClass_Method;
  13965. begin
  13966. StartProgram(false);
  13967. Add('type');
  13968. Add(' TObject = class');
  13969. Add(' public');
  13970. Add(' vI: longint;');
  13971. Add(' Sub: TObject;');
  13972. Add(' constructor Create;');
  13973. Add(' function GetIt(Par: longint): tobject;');
  13974. Add(' end;');
  13975. Add('constructor tobject.create; begin end;');
  13976. Add('function tobject.getit(par: longint): tobject;');
  13977. Add('begin');
  13978. Add(' Self.vi:=par+3;');
  13979. Add(' Result:=self.sub;');
  13980. Add('end;');
  13981. Add('var Obj: tobject;');
  13982. Add('begin');
  13983. Add(' obj:=tobject.create;');
  13984. Add(' obj.getit(4);');
  13985. Add(' obj.sub.sub:=nil;');
  13986. Add(' obj.sub.getit(5);');
  13987. Add(' obj.sub.getit(6).SUB:=nil;');
  13988. Add(' obj.sub.getit(7).GETIT(8);');
  13989. Add(' obj.sub.getit(9).SuB.getit(10);');
  13990. ConvertProgram;
  13991. CheckSource('TestClass_Method',
  13992. LinesToStr([ // statements
  13993. 'rtl.createClass(this,"TObject",null,function(){',
  13994. ' this.$init = function () {',
  13995. ' this.vI = 0;',
  13996. ' this.Sub = null;',
  13997. ' };',
  13998. ' this.$final = function () {',
  13999. ' this.Sub = undefined;',
  14000. ' };',
  14001. ' this.Create = function(){',
  14002. ' return this;',
  14003. ' };',
  14004. ' this.GetIt = function(Par){',
  14005. ' var Result = null;',
  14006. ' this.vI = Par + 3;',
  14007. ' Result = this.Sub;',
  14008. ' return Result;',
  14009. ' };',
  14010. '});',
  14011. 'this.Obj = null;'
  14012. ]),
  14013. LinesToStr([ // $mod.$main
  14014. '$mod.Obj = $mod.TObject.$create("Create");',
  14015. '$mod.Obj.GetIt(4);',
  14016. '$mod.Obj.Sub.Sub=null;',
  14017. '$mod.Obj.Sub.GetIt(5);',
  14018. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  14019. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  14020. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  14021. ]));
  14022. end;
  14023. procedure TTestModule.TestClass_Implementation;
  14024. begin
  14025. StartUnit(false);
  14026. Add([
  14027. 'interface',
  14028. 'type',
  14029. ' TObject = class',
  14030. ' constructor Create;',
  14031. ' end;',
  14032. 'implementation',
  14033. 'type',
  14034. ' TIntClass = class',
  14035. ' constructor Create; reintroduce;',
  14036. ' class procedure DoGlob;',
  14037. ' end;',
  14038. 'constructor tintclass.create;',
  14039. 'begin',
  14040. ' inherited;',
  14041. ' inherited create;',
  14042. ' doglob;',
  14043. 'end;',
  14044. 'class procedure tintclass.doglob;',
  14045. 'begin',
  14046. 'end;',
  14047. 'constructor tobject.create;',
  14048. 'var',
  14049. ' iC: tintclass;',
  14050. 'begin',
  14051. ' ic:=tintclass.create;',
  14052. ' tintclass.doglob;',
  14053. ' ic.doglob;',
  14054. 'end;',
  14055. 'initialization',
  14056. ' tintclass.doglob;',
  14057. '']);
  14058. ConvertUnit;
  14059. CheckSource('TestClass_Implementation',
  14060. LinesToStr([ // statements
  14061. 'var $impl = $mod.$impl;',
  14062. 'rtl.createClass(this, "TObject", null, function () {',
  14063. ' this.$init = function () {',
  14064. ' };',
  14065. ' this.$final = function () {',
  14066. ' };',
  14067. ' this.Create = function () {',
  14068. ' var iC = null;',
  14069. ' iC = $impl.TIntClass.$create("Create$1");',
  14070. ' $impl.TIntClass.DoGlob();',
  14071. ' iC.$class.DoGlob();',
  14072. ' return this;',
  14073. ' };',
  14074. '});',
  14075. '']),
  14076. LinesToStr([ // $mod.$main
  14077. '$impl.TIntClass.DoGlob();',
  14078. '']),
  14079. LinesToStr([
  14080. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  14081. ' this.Create$1 = function () {',
  14082. ' $mod.TObject.Create.call(this);',
  14083. ' $mod.TObject.Create.call(this);',
  14084. ' this.$class.DoGlob();',
  14085. ' return this;',
  14086. ' };',
  14087. ' this.DoGlob = function () {',
  14088. ' };',
  14089. '});',
  14090. '']));
  14091. end;
  14092. procedure TTestModule.TestClass_Inheritance;
  14093. begin
  14094. StartProgram(false);
  14095. Add('type');
  14096. Add(' TObject = class');
  14097. Add(' public');
  14098. Add(' constructor Create;');
  14099. Add(' end;');
  14100. Add(' TClassA = class');
  14101. Add(' end;');
  14102. Add(' TClassB = class(TObject)');
  14103. Add(' procedure ProcB;');
  14104. Add(' end;');
  14105. Add('constructor tobject.create; begin end;');
  14106. Add('procedure tclassb.procb; begin end;');
  14107. Add('var');
  14108. Add(' oO: TObject;');
  14109. Add(' oA: TClassA;');
  14110. Add(' oB: TClassB;');
  14111. Add('begin');
  14112. Add(' oO:=tobject.Create;');
  14113. Add(' oA:=tclassa.Create;');
  14114. Add(' ob:=tclassb.Create;');
  14115. Add(' if oo is tclassa then ;');
  14116. Add(' ob:=oo as tclassb;');
  14117. Add(' (oo as tclassb).procb;');
  14118. ConvertProgram;
  14119. CheckSource('TestClass_Inheritance',
  14120. LinesToStr([ // statements
  14121. 'rtl.createClass(this,"TObject",null,function(){',
  14122. ' this.$init = function () {',
  14123. ' };',
  14124. ' this.$final = function () {',
  14125. ' };',
  14126. ' this.Create = function () {',
  14127. ' return this;',
  14128. ' };',
  14129. '});',
  14130. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14131. '});',
  14132. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14133. ' this.ProcB = function () {',
  14134. ' };',
  14135. '});',
  14136. 'this.oO = null;',
  14137. 'this.oA = null;',
  14138. 'this.oB = null;'
  14139. ]),
  14140. LinesToStr([ // $mod.$main
  14141. '$mod.oO = $mod.TObject.$create("Create");',
  14142. '$mod.oA = $mod.TClassA.$create("Create");',
  14143. '$mod.oB = $mod.TClassB.$create("Create");',
  14144. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14145. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14146. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14147. ]));
  14148. end;
  14149. procedure TTestModule.TestClass_TypeAlias;
  14150. begin
  14151. StartProgram(false);
  14152. Add([
  14153. '{$interfaces corba}',
  14154. 'type',
  14155. ' IObject = interface',
  14156. ' end;',
  14157. ' IBird = type IObject;',
  14158. ' TObject = class',
  14159. ' end;',
  14160. ' TBird = type TObject;',
  14161. 'var',
  14162. ' oObj: TObject;',
  14163. ' oBird: TBird;',
  14164. ' IntfObj: IObject;',
  14165. ' IntfBird: IBird;',
  14166. 'begin',
  14167. ' oObj:=oBird;',
  14168. '']);
  14169. ConvertProgram;
  14170. CheckSource('TestClass_TypeAlias',
  14171. LinesToStr([ // statements
  14172. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14173. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14174. 'rtl.createClass(this, "TObject", null, function () {',
  14175. ' this.$init = function () {',
  14176. ' };',
  14177. ' this.$final = function () {',
  14178. ' };',
  14179. '});',
  14180. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14181. '});',
  14182. 'this.oObj = null;',
  14183. 'this.oBird = null;',
  14184. 'this.IntfObj = null;',
  14185. 'this.IntfBird = null;',
  14186. '']),
  14187. LinesToStr([ // $mod.$main
  14188. '$mod.oObj = $mod.oBird;',
  14189. '']));
  14190. end;
  14191. procedure TTestModule.TestClass_AbstractMethod;
  14192. begin
  14193. StartProgram(false);
  14194. Add('type');
  14195. Add(' TObject = class');
  14196. Add(' public');
  14197. Add(' procedure DoIt; virtual; abstract;');
  14198. Add(' end;');
  14199. Add('begin');
  14200. ConvertProgram;
  14201. CheckSource('TestClass_AbstractMethod',
  14202. LinesToStr([ // statements
  14203. 'rtl.createClass(this,"TObject",null,function(){',
  14204. ' this.$init = function () {',
  14205. ' };',
  14206. ' this.$final = function () {',
  14207. ' };',
  14208. '});'
  14209. ]),
  14210. LinesToStr([ // this.$main
  14211. ''
  14212. ]));
  14213. end;
  14214. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14215. begin
  14216. StartProgram(false);
  14217. Add([
  14218. 'type',
  14219. ' TObject = class',
  14220. ' procedure DoAbstract; virtual; abstract;',
  14221. ' procedure DoVirtual; virtual;',
  14222. ' procedure DoIt;',
  14223. ' end;',
  14224. ' TA = class',
  14225. ' procedure doabstract; override;',
  14226. ' procedure dovirtual; override;',
  14227. ' procedure DoSome;',
  14228. ' end;',
  14229. 'procedure tobject.dovirtual;',
  14230. 'begin',
  14231. ' inherited; // call non existing ancestor -> ignore silently',
  14232. 'end;',
  14233. 'procedure tobject.doit;',
  14234. 'begin',
  14235. 'end;',
  14236. 'procedure ta.doabstract;',
  14237. 'begin',
  14238. ' inherited dovirtual; // call TObject.DoVirtual',
  14239. 'end;',
  14240. 'procedure ta.dovirtual;',
  14241. 'begin',
  14242. ' inherited; // call TObject.DoVirtual',
  14243. ' inherited dovirtual; // call TObject.DoVirtual',
  14244. ' inherited dovirtual(); // call TObject.DoVirtual',
  14245. ' doit;',
  14246. ' doit();',
  14247. 'end;',
  14248. 'procedure ta.dosome;',
  14249. 'begin',
  14250. ' inherited; // call non existing ancestor method -> silently ignore',
  14251. 'end;',
  14252. 'begin']);
  14253. ConvertProgram;
  14254. CheckSource('TestClass_CallInherited_ProcNoParams',
  14255. LinesToStr([ // statements
  14256. 'rtl.createClass(this,"TObject",null,function(){',
  14257. ' this.$init = function () {',
  14258. ' };',
  14259. ' this.$final = function () {',
  14260. ' };',
  14261. ' this.DoVirtual = function () {',
  14262. ' };',
  14263. ' this.DoIt = function () {',
  14264. ' };',
  14265. '});',
  14266. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14267. ' this.DoAbstract = function () {',
  14268. ' $mod.TObject.DoVirtual.call(this);',
  14269. ' };',
  14270. ' this.DoVirtual = function () {',
  14271. ' $mod.TObject.DoVirtual.call(this);',
  14272. ' $mod.TObject.DoVirtual.call(this);',
  14273. ' $mod.TObject.DoVirtual.call(this);',
  14274. ' this.DoIt();',
  14275. ' this.DoIt();',
  14276. ' };',
  14277. ' this.DoSome = function () {',
  14278. ' };',
  14279. '});'
  14280. ]),
  14281. LinesToStr([ // this.$main
  14282. ''
  14283. ]));
  14284. end;
  14285. procedure TTestModule.TestClass_CallInherited_WithParams;
  14286. begin
  14287. StartProgram(false);
  14288. Add([
  14289. 'type',
  14290. ' TObject = class',
  14291. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14292. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14293. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14294. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14295. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14296. ' end;',
  14297. ' TClassA = class',
  14298. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14299. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14300. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14301. ' end;',
  14302. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14303. 'begin',
  14304. 'end;',
  14305. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14306. 'begin',
  14307. 'end;',
  14308. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14309. 'begin',
  14310. 'end;',
  14311. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14312. 'begin',
  14313. 'end;',
  14314. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14315. 'begin',
  14316. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14317. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14318. 'end;',
  14319. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14320. 'begin',
  14321. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14322. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14323. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14324. ' doit(pa,pb);',
  14325. ' doit(pa);',
  14326. ' doit2(pa);',
  14327. ' doit2;',
  14328. 'end;',
  14329. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14330. 'begin',
  14331. ' pa:=inherited;',
  14332. 'end;',
  14333. 'begin']);
  14334. ConvertProgram;
  14335. CheckSource('TestClass_CallInherited_WithParams',
  14336. LinesToStr([ // statements
  14337. 'rtl.createClass(this,"TObject",null,function(){',
  14338. ' this.$init = function () {',
  14339. ' };',
  14340. ' this.$final = function () {',
  14341. ' };',
  14342. ' this.DoVirtual = function (pA,pB) {',
  14343. ' };',
  14344. ' this.DoIt = function (pA,pB) {',
  14345. ' };',
  14346. ' this.DoIt2 = function (pA,pB) {',
  14347. ' };',
  14348. ' this.GetIt = function (pA, pB) {',
  14349. ' var Result = 0;',
  14350. ' return Result;',
  14351. ' };',
  14352. '});',
  14353. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14354. ' this.DoAbstract = function (pA,pB) {',
  14355. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14356. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14357. ' };',
  14358. ' this.DoVirtual = function (pA,pB) {',
  14359. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14360. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14361. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14362. ' this.DoIt(pA,pB);',
  14363. ' this.DoIt(pA,0);',
  14364. ' this.DoIt2(pA,2);',
  14365. ' this.DoIt2(1,2);',
  14366. ' };',
  14367. ' this.GetIt$1 = function (pA, pB) {',
  14368. ' var Result = 0;',
  14369. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14370. ' return Result;',
  14371. ' };',
  14372. '});'
  14373. ]),
  14374. LinesToStr([ // this.$main
  14375. ''
  14376. ]));
  14377. end;
  14378. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14379. begin
  14380. StartProgram(false);
  14381. Add('type');
  14382. Add(' TObject = class');
  14383. Add(' constructor Create; virtual;');
  14384. Add(' constructor CreateWithB(b: boolean);');
  14385. Add(' end;');
  14386. Add(' TA = class');
  14387. Add(' constructor Create; override;');
  14388. Add(' constructor CreateWithC(c: char);');
  14389. Add(' procedure DoIt;');
  14390. Add(' class function DoSome: TObject;');
  14391. Add(' end;');
  14392. Add('constructor tobject.create;');
  14393. Add('begin');
  14394. Add(' inherited; // call non existing ancestor -> ignore silently');
  14395. Add('end;');
  14396. Add('constructor tobject.createwithb(b: boolean);');
  14397. Add('begin');
  14398. Add(' inherited; // call non existing ancestor -> ignore silently');
  14399. Add(' create; // normal call');
  14400. Add('end;');
  14401. Add('constructor ta.create;');
  14402. Add('begin');
  14403. Add(' inherited; // normal call TObject.Create');
  14404. Add(' inherited create; // normal call TObject.Create');
  14405. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14406. Add('end;');
  14407. Add('constructor ta.createwithc(c: char);');
  14408. Add('begin');
  14409. Add(' inherited create; // call TObject.Create');
  14410. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14411. Add(' doit;');
  14412. Add(' doit();');
  14413. Add(' dosome;');
  14414. Add('end;');
  14415. Add('procedure ta.doit;');
  14416. Add('begin');
  14417. Add(' create; // normal call');
  14418. Add(' createwithb(false); // normal call');
  14419. Add(' createwithc(''c''); // normal call');
  14420. Add('end;');
  14421. Add('class function ta.dosome: TObject;');
  14422. Add('begin');
  14423. Add(' Result:=create; // constructor');
  14424. Add(' Result:=createwithb(true); // constructor');
  14425. Add(' Result:=createwithc(''c''); // constructor');
  14426. Add('end;');
  14427. Add('begin');
  14428. ConvertProgram;
  14429. CheckSource('TestClass_CallInheritedConstructor',
  14430. LinesToStr([ // statements
  14431. 'rtl.createClass(this,"TObject",null,function(){',
  14432. ' this.$init = function () {',
  14433. ' };',
  14434. ' this.$final = function () {',
  14435. ' };',
  14436. ' this.Create = function () {',
  14437. ' return this;',
  14438. ' };',
  14439. ' this.CreateWithB = function (b) {',
  14440. ' this.Create();',
  14441. ' return this;',
  14442. ' };',
  14443. '});',
  14444. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14445. ' this.Create = function () {',
  14446. ' $mod.TObject.Create.call(this);',
  14447. ' $mod.TObject.Create.call(this);',
  14448. ' $mod.TObject.CreateWithB.call(this, false);',
  14449. ' return this;',
  14450. ' };',
  14451. ' this.CreateWithC = function (c) {',
  14452. ' $mod.TObject.Create.call(this);',
  14453. ' $mod.TObject.CreateWithB.call(this, true);',
  14454. ' this.DoIt();',
  14455. ' this.DoIt();',
  14456. ' this.$class.DoSome();',
  14457. ' return this;',
  14458. ' };',
  14459. ' this.DoIt = function () {',
  14460. ' this.Create();',
  14461. ' this.CreateWithB(false);',
  14462. ' this.CreateWithC("c");',
  14463. ' };',
  14464. ' this.DoSome = function () {',
  14465. ' var Result = null;',
  14466. ' Result = this.$create("Create");',
  14467. ' Result = this.$create("CreateWithB", [true]);',
  14468. ' Result = this.$create("CreateWithC", ["c"]);',
  14469. ' return Result;',
  14470. ' };',
  14471. '});'
  14472. ]),
  14473. LinesToStr([ // this.$main
  14474. ''
  14475. ]));
  14476. end;
  14477. procedure TTestModule.TestClass_ClassVar_Assign;
  14478. begin
  14479. StartProgram(false);
  14480. Add([
  14481. 'type',
  14482. ' TObject = class',
  14483. ' public',
  14484. ' class var vI: longint;',
  14485. ' class var Sub: TObject;',
  14486. ' constructor Create;',
  14487. ' class function GetIt(var Par: longint): tobject;',
  14488. ' end;',
  14489. 'constructor tobject.create;',
  14490. 'begin',
  14491. ' vi:=vi+1;',
  14492. ' Self.vi:=Self.vi+1;',
  14493. ' inc(vi);',
  14494. 'end;',
  14495. 'class function tobject.getit(var par: longint): tobject;',
  14496. 'begin',
  14497. ' vi:=vi+3;',
  14498. ' Self.vi:=Self.vi+4;',
  14499. ' inc(vi);',
  14500. ' Result:=self.sub;',
  14501. ' GetIt(vi);',
  14502. 'end;',
  14503. 'var Obj: tobject;',
  14504. 'begin',
  14505. ' obj:=tobject.create;',
  14506. ' tobject.vi:=3;',
  14507. ' if tobject.vi=4 then ;',
  14508. ' tobject.sub:=nil;',
  14509. ' obj.sub:=nil;',
  14510. ' obj.sub.sub:=nil;']);
  14511. ConvertProgram;
  14512. CheckSource('TestClass_ClassVar_Assign',
  14513. LinesToStr([ // statements
  14514. 'rtl.createClass(this,"TObject",null,function(){',
  14515. ' this.vI = 0;',
  14516. ' this.Sub = null;',
  14517. ' this.$init = function () {',
  14518. ' };',
  14519. ' this.$final = function () {',
  14520. ' };',
  14521. ' this.Create = function(){',
  14522. ' $mod.TObject.vI = this.vI+1;',
  14523. ' $mod.TObject.vI = this.vI+1;',
  14524. ' $mod.TObject.vI += 1;',
  14525. ' return this;',
  14526. ' };',
  14527. ' this.GetIt = function(Par){',
  14528. ' var Result = null;',
  14529. ' $mod.TObject.vI = this.vI + 3;',
  14530. ' $mod.TObject.vI = this.vI + 4;',
  14531. ' $mod.TObject.vI += 1;',
  14532. ' Result = this.Sub;',
  14533. ' this.GetIt({',
  14534. ' p: $mod.TObject,',
  14535. ' get: function () {',
  14536. ' return this.p.vI;',
  14537. ' },',
  14538. ' set: function (v) {',
  14539. ' this.p.vI = v;',
  14540. ' }',
  14541. ' });',
  14542. ' return Result;',
  14543. ' };',
  14544. '});',
  14545. 'this.Obj = null;'
  14546. ]),
  14547. LinesToStr([ // $mod.$main
  14548. '$mod.Obj = $mod.TObject.$create("Create");',
  14549. '$mod.TObject.vI = 3;',
  14550. 'if ($mod.TObject.vI === 4);',
  14551. '$mod.TObject.Sub=null;',
  14552. '$mod.TObject.Sub=null;',
  14553. '$mod.TObject.Sub=null;',
  14554. '']));
  14555. end;
  14556. procedure TTestModule.TestClass_CallClassMethod;
  14557. begin
  14558. StartProgram(false);
  14559. Add('type');
  14560. Add(' TObject = class');
  14561. Add(' public');
  14562. Add(' class var vI: longint;');
  14563. Add(' class var Sub: TObject;');
  14564. Add(' constructor Create;');
  14565. Add(' function GetMore(Par: longint): longint;');
  14566. Add(' class function GetIt(Par: longint): tobject;');
  14567. Add(' end;');
  14568. Add('constructor tobject.create;');
  14569. Add('begin');
  14570. Add(' sub:=getit(3);');
  14571. Add(' vi:=getmore(4);');
  14572. Add(' sub:=Self.getit(5);');
  14573. Add(' vi:=Self.getmore(6);');
  14574. Add('end;');
  14575. Add('function tobject.getmore(par: longint): longint;');
  14576. Add('begin');
  14577. Add(' sub:=getit(11);');
  14578. Add(' vi:=getmore(12);');
  14579. Add(' sub:=self.getit(13);');
  14580. Add(' vi:=self.getmore(14);');
  14581. Add('end;');
  14582. Add('class function tobject.getit(par: longint): tobject;');
  14583. Add('begin');
  14584. Add(' sub:=getit(21);');
  14585. Add(' vi:=sub.getmore(22);');
  14586. Add(' sub:=self.getit(23);');
  14587. Add(' vi:=self.sub.getmore(24);');
  14588. Add('end;');
  14589. Add('var Obj: tobject;');
  14590. Add('begin');
  14591. Add(' obj:=tobject.create;');
  14592. Add(' tobject.getit(5);');
  14593. Add(' obj.getit(6);');
  14594. Add(' obj.sub.getit(7);');
  14595. Add(' obj.sub.getit(8).SUB:=nil;');
  14596. Add(' obj.sub.getit(9).GETIT(10);');
  14597. Add(' obj.sub.getit(11).SuB.getit(12);');
  14598. ConvertProgram;
  14599. CheckSource('TestClass_CallClassMethod',
  14600. LinesToStr([ // statements
  14601. 'rtl.createClass(this,"TObject",null,function(){',
  14602. ' this.vI = 0;',
  14603. ' this.Sub = null;',
  14604. ' this.$init = function () {',
  14605. ' };',
  14606. ' this.$final = function () {',
  14607. ' };',
  14608. ' this.Create = function(){',
  14609. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14610. ' $mod.TObject.vI = this.GetMore(4);',
  14611. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14612. ' $mod.TObject.vI = this.GetMore(6);',
  14613. ' return this;',
  14614. ' };',
  14615. ' this.GetMore = function(Par){',
  14616. ' var Result = 0;',
  14617. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14618. ' $mod.TObject.vI = this.GetMore(12);',
  14619. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14620. ' $mod.TObject.vI = this.GetMore(14);',
  14621. ' return Result;',
  14622. ' };',
  14623. ' this.GetIt = function(Par){',
  14624. ' var Result = null;',
  14625. ' $mod.TObject.Sub = this.GetIt(21);',
  14626. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14627. ' $mod.TObject.Sub = this.GetIt(23);',
  14628. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14629. ' return Result;',
  14630. ' };',
  14631. '});',
  14632. 'this.Obj = null;'
  14633. ]),
  14634. LinesToStr([ // $mod.$main
  14635. '$mod.Obj = $mod.TObject.$create("Create");',
  14636. '$mod.TObject.GetIt(5);',
  14637. '$mod.Obj.$class.GetIt(6);',
  14638. '$mod.Obj.Sub.$class.GetIt(7);',
  14639. '$mod.TObject.Sub=null;',
  14640. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14641. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14642. '']));
  14643. end;
  14644. procedure TTestModule.TestClass_CallClassMethodStatic;
  14645. begin
  14646. StartProgram(false);
  14647. Add([
  14648. 'type',
  14649. ' TObject = class',
  14650. ' public',
  14651. ' class function Fly: tobject; static;',
  14652. ' end;',
  14653. 'class function tobject.Fly: tobject;',
  14654. 'begin',
  14655. ' Result.Fly;',
  14656. ' Result.Fly();',
  14657. ' Fly;',
  14658. ' Fly();',
  14659. ' Fly.Fly;',
  14660. ' Fly.Fly();',
  14661. 'end;',
  14662. 'var Obj: tobject;',
  14663. 'begin',
  14664. ' obj.Fly;',
  14665. ' obj.Fly();',
  14666. ' with obj do begin',
  14667. ' Fly;',
  14668. ' Fly();',
  14669. ' end;',
  14670. '']);
  14671. ConvertProgram;
  14672. CheckSource('TestClass_CallClassMethodStatic',
  14673. LinesToStr([ // statements
  14674. 'rtl.createClass(this, "TObject", null, function () {',
  14675. ' this.$init = function () {',
  14676. ' };',
  14677. ' this.$final = function () {',
  14678. ' };',
  14679. ' this.Fly = function () {',
  14680. ' var Result = null;',
  14681. ' $mod.TObject.Fly();',
  14682. ' $mod.TObject.Fly();',
  14683. ' $mod.TObject.Fly();',
  14684. ' $mod.TObject.Fly();',
  14685. ' $mod.TObject.Fly();',
  14686. ' $mod.TObject.Fly();',
  14687. ' return Result;',
  14688. ' };',
  14689. '});',
  14690. 'this.Obj = null;'
  14691. ]),
  14692. LinesToStr([ // $mod.$main
  14693. '$mod.TObject.Fly();',
  14694. '$mod.TObject.Fly();',
  14695. 'var $with = $mod.Obj;',
  14696. '$with.Fly();',
  14697. '$with.Fly();',
  14698. '']));
  14699. end;
  14700. procedure TTestModule.TestClass_Property;
  14701. begin
  14702. StartProgram(false);
  14703. Add('type');
  14704. Add(' TObject = class');
  14705. Add(' Fx: longint;');
  14706. Add(' Fy: longint;');
  14707. Add(' function GetInt: longint;');
  14708. Add(' procedure SetInt(Value: longint);');
  14709. Add(' procedure DoIt;');
  14710. Add(' property IntA: longint read Fx write Fy;');
  14711. Add(' property IntB: longint read GetInt write SetInt;');
  14712. Add(' end;');
  14713. Add('function tobject.getint: longint;');
  14714. Add('begin');
  14715. Add(' result:=fx;');
  14716. Add('end;');
  14717. Add('procedure tobject.setint(value: longint);');
  14718. Add('begin');
  14719. Add(' if value=fy then exit;');
  14720. Add(' fy:=value;');
  14721. Add('end;');
  14722. Add('procedure tobject.doit;');
  14723. Add('begin');
  14724. Add(' IntA:=IntA+1;');
  14725. Add(' Self.IntA:=Self.IntA+1;');
  14726. Add(' IntB:=IntB+1;');
  14727. Add(' Self.IntB:=Self.IntB+1;');
  14728. Add('end;');
  14729. Add('var Obj: tobject;');
  14730. Add('begin');
  14731. Add(' obj.inta:=obj.inta+1;');
  14732. Add(' if obj.intb=2 then;');
  14733. Add(' obj.intb:=obj.intb+2;');
  14734. Add(' obj.setint(obj.inta);');
  14735. ConvertProgram;
  14736. CheckSource('TestClass_Property',
  14737. LinesToStr([ // statements
  14738. 'rtl.createClass(this, "TObject", null, function () {',
  14739. ' this.$init = function () {',
  14740. ' this.Fx = 0;',
  14741. ' this.Fy = 0;',
  14742. ' };',
  14743. ' this.$final = function () {',
  14744. ' };',
  14745. ' this.GetInt = function () {',
  14746. ' var Result = 0;',
  14747. ' Result = this.Fx;',
  14748. ' return Result;',
  14749. ' };',
  14750. ' this.SetInt = function (Value) {',
  14751. ' if (Value === this.Fy) return;',
  14752. ' this.Fy = Value;',
  14753. ' };',
  14754. ' this.DoIt = function () {',
  14755. ' this.Fy = this.Fx + 1;',
  14756. ' this.Fy = this.Fx + 1;',
  14757. ' this.SetInt(this.GetInt() + 1);',
  14758. ' this.SetInt(this.GetInt() + 1);',
  14759. ' };',
  14760. '});',
  14761. 'this.Obj = null;'
  14762. ]),
  14763. LinesToStr([ // $mod.$main
  14764. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14765. 'if ($mod.Obj.GetInt() === 2);',
  14766. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14767. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14768. ]));
  14769. end;
  14770. procedure TTestModule.TestClass_Property_ClassMethod;
  14771. begin
  14772. StartProgram(false);
  14773. Add([
  14774. 'type',
  14775. ' TObject = class',
  14776. ' class var Fx: longint;',
  14777. ' class var Fy: longint;',
  14778. ' class function GetInt: longint;',
  14779. ' class procedure SetInt(Value: longint);',
  14780. ' end;',
  14781. ' TBird = class',
  14782. ' class procedure DoIt;',
  14783. ' class property IntA: longint read Fx write Fy;',
  14784. ' class property IntB: longint read GetInt write SetInt;',
  14785. ' end;',
  14786. 'class function tobject.getint: longint;',
  14787. 'begin',
  14788. ' result:=fx;',
  14789. 'end;',
  14790. 'class procedure tobject.setint(value: longint);',
  14791. 'begin',
  14792. 'end;',
  14793. 'class procedure tbird.doit;',
  14794. 'begin',
  14795. ' FX:=3;',
  14796. ' IntA:=IntA+1;',
  14797. ' Self.IntA:=Self.IntA+1;',
  14798. ' IntB:=IntB+1;',
  14799. ' Self.IntB:=Self.IntB+1;',
  14800. ' with Self do begin',
  14801. ' FX:=11;',
  14802. ' IntA:=IntA+12;',
  14803. ' IntB:=IntB+13;',
  14804. ' end;',
  14805. 'end;',
  14806. 'var Obj: tbird;',
  14807. 'begin',
  14808. ' tbird.fx:=tbird.fx+1;',
  14809. ' tbird.inta:=tbird.inta+1;',
  14810. ' if tbird.intb=2 then;',
  14811. ' tbird.intb:=tbird.intb+2;',
  14812. ' tbird.setint(tbird.inta);',
  14813. ' obj.inta:=obj.inta+1;',
  14814. ' if obj.intb=2 then;',
  14815. ' obj.intb:=obj.intb+2;',
  14816. ' obj.setint(obj.inta);',
  14817. ' with Tbird do begin',
  14818. ' FX:=FY+1;',
  14819. ' inta:=inta+2;',
  14820. ' intb:=intb+3;',
  14821. ' end;',
  14822. ' with Obj do begin',
  14823. ' FX:=FY+1;',
  14824. ' inta:=inta+2;',
  14825. ' intb:=intb+3;',
  14826. ' end;',
  14827. '']);
  14828. ConvertProgram;
  14829. CheckSource('TestClass_Property_ClassMethod',
  14830. LinesToStr([ // statements
  14831. 'rtl.createClass(this, "TObject", null, function () {',
  14832. ' this.Fx = 0;',
  14833. ' this.Fy = 0;',
  14834. ' this.$init = function () {',
  14835. ' };',
  14836. ' this.$final = function () {',
  14837. ' };',
  14838. ' this.GetInt = function () {',
  14839. ' var Result = 0;',
  14840. ' Result = this.Fx;',
  14841. ' return Result;',
  14842. ' };',
  14843. ' this.SetInt = function (Value) {',
  14844. ' };',
  14845. '});',
  14846. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14847. ' this.DoIt = function () {',
  14848. ' $mod.TObject.Fx = 3;',
  14849. ' $mod.TObject.Fy = this.Fx + 1;',
  14850. ' $mod.TObject.Fy = this.Fx + 1;',
  14851. ' this.SetInt(this.GetInt() + 1);',
  14852. ' this.SetInt(this.GetInt() + 1);',
  14853. ' $mod.TObject.Fx = 11;',
  14854. ' $mod.TObject.Fy = this.Fx + 12;',
  14855. ' this.SetInt(this.GetInt() + 13);',
  14856. ' };',
  14857. '});',
  14858. 'this.Obj = null;'
  14859. ]),
  14860. LinesToStr([ // $mod.$main
  14861. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14862. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14863. 'if ($mod.TBird.GetInt() === 2);',
  14864. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14865. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14866. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14867. 'if ($mod.Obj.$class.GetInt() === 2);',
  14868. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14869. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14870. 'var $with = $mod.TBird;',
  14871. '$mod.TObject.Fx = $with.Fy + 1;',
  14872. '$mod.TObject.Fy = $with.Fx + 2;',
  14873. '$with.SetInt($with.GetInt() + 3);',
  14874. 'var $with1 = $mod.Obj;',
  14875. '$mod.TObject.Fx = $with1.Fy + 1;',
  14876. '$mod.TObject.Fy = $with1.Fx + 2;',
  14877. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14878. '']));
  14879. end;
  14880. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14881. begin
  14882. StartProgram(false);
  14883. Add([
  14884. 'type',
  14885. ' TObject = class',
  14886. ' class function GetInt: longint; static;',
  14887. ' class procedure SetInt(Value: longint); static;',
  14888. ' class function GetItems(Index: word): longint; static;',
  14889. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14890. ' end;',
  14891. ' TBird = class',
  14892. ' class procedure Fly;',
  14893. ' class property IntA: longint read GetInt write SetInt;',
  14894. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14895. ' end;',
  14896. 'class function tobject.getint: longint;',
  14897. 'begin',
  14898. 'end;',
  14899. 'class procedure tobject.setint(value: longint);',
  14900. 'begin',
  14901. 'end;',
  14902. 'class function tobject.GetItems(Index: word): longint;',
  14903. 'begin',
  14904. 'end;',
  14905. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14906. 'begin',
  14907. 'end;',
  14908. 'class procedure tbird.fly;',
  14909. 'var w: longint;',
  14910. 'begin',
  14911. ' inta:=inta+51;',
  14912. ' w:=items[52];',
  14913. ' items[53]:=54;',
  14914. 'end;',
  14915. 'var Obj: tbird;',
  14916. ' i: longint;',
  14917. 'begin',
  14918. ' tbird.inta:=tbird.inta+1;',
  14919. ' i:=tbird.items[2];',
  14920. ' tbird.items[3]:=4;',
  14921. ' obj.inta:=obj.inta+11;',
  14922. ' i:=obj.items[12];',
  14923. ' obj.items[13]:=14;',
  14924. ' with Tbird do begin',
  14925. ' inta:=inta+21;',
  14926. ' i:=items[22];',
  14927. ' items[23]:=24;',
  14928. ' end;',
  14929. ' with Obj do begin',
  14930. ' inta:=inta+31;',
  14931. ' i:=items[32];',
  14932. ' items[33]:=34;',
  14933. ' end;',
  14934. '']);
  14935. ConvertProgram;
  14936. CheckSource('TestClass_Property_ClassMethod',
  14937. LinesToStr([ // statements
  14938. 'rtl.createClass(this, "TObject", null, function () {',
  14939. ' this.$init = function () {',
  14940. ' };',
  14941. ' this.$final = function () {',
  14942. ' };',
  14943. ' this.GetInt = function () {',
  14944. ' var Result = 0;',
  14945. ' return Result;',
  14946. ' };',
  14947. ' this.SetInt = function (Value) {',
  14948. ' };',
  14949. ' this.GetItems = function (Index) {',
  14950. ' var Result = 0;',
  14951. ' return Result;',
  14952. ' };',
  14953. ' this.SetItems = function (Index, Value) {',
  14954. ' };',
  14955. '});',
  14956. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14957. ' this.Fly = function () {',
  14958. ' var w = 0;',
  14959. ' this.SetInt(this.GetInt() + 51);',
  14960. ' w = this.GetItems(52);',
  14961. ' this.SetItems(53, 54);',
  14962. ' };',
  14963. '});',
  14964. 'this.Obj = null;',
  14965. 'this.i = 0;',
  14966. '']),
  14967. LinesToStr([ // $mod.$main
  14968. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14969. '$mod.i = $mod.TObject.GetItems(2);',
  14970. '$mod.TObject.SetItems(3, 4);',
  14971. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14972. '$mod.i = $mod.TObject.GetItems(12);',
  14973. '$mod.TObject.SetItems(13, 14);',
  14974. 'var $with = $mod.TBird;',
  14975. '$with.SetInt($with.GetInt() + 21);',
  14976. '$mod.i = $with.GetItems(22);',
  14977. '$with.SetItems(23, 24);',
  14978. 'var $with1 = $mod.Obj;',
  14979. '$with1.SetInt($with1.GetInt() + 31);',
  14980. '$mod.i = $with1.GetItems(32);',
  14981. '$with1.SetItems(33, 34);',
  14982. '']));
  14983. end;
  14984. procedure TTestModule.TestClass_Property_Indexed;
  14985. begin
  14986. StartProgram(false);
  14987. Add([
  14988. 'type',
  14989. ' TObject = class',
  14990. ' FItems: array of longint;',
  14991. ' function GetItems(Index: longint): longint;',
  14992. ' procedure SetItems(Index: longint; Value: longint);',
  14993. ' procedure DoIt;',
  14994. ' property Items[Index: longint]: longint read getitems write setitems;',
  14995. ' end;',
  14996. 'function tobject.getitems(index: longint): longint;',
  14997. 'begin',
  14998. ' Result:=fitems[index];',
  14999. 'end;',
  15000. 'procedure tobject.setitems(index: longint; value: longint);',
  15001. 'begin',
  15002. ' fitems[index]:=value;',
  15003. 'end;',
  15004. 'procedure tobject.doit;',
  15005. 'begin',
  15006. ' items[1]:=2;',
  15007. ' items[3]:=items[4];',
  15008. ' self.items[5]:=self.items[6];',
  15009. ' items[items[7]]:=items[items[8]];',
  15010. 'end;',
  15011. 'var Obj: tobject;',
  15012. 'begin',
  15013. ' obj.Items[11]:=obj.Items[12];',
  15014. '']);
  15015. ConvertProgram;
  15016. CheckSource('TestClass_Property_Indexed',
  15017. LinesToStr([ // statements
  15018. 'rtl.createClass(this, "TObject", null, function () {',
  15019. ' this.$init = function () {',
  15020. ' this.FItems = [];',
  15021. ' };',
  15022. ' this.$final = function () {',
  15023. ' this.FItems = undefined;',
  15024. ' };',
  15025. ' this.GetItems = function (Index) {',
  15026. ' var Result = 0;',
  15027. ' Result = this.FItems[Index];',
  15028. ' return Result;',
  15029. ' };',
  15030. ' this.SetItems = function (Index, Value) {',
  15031. ' this.FItems[Index] = Value;',
  15032. ' };',
  15033. ' this.DoIt = function () {',
  15034. ' this.SetItems(1, 2);',
  15035. ' this.SetItems(3,this.GetItems(4));',
  15036. ' this.SetItems(5,this.GetItems(6));',
  15037. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  15038. ' };',
  15039. '});',
  15040. 'this.Obj = null;'
  15041. ]),
  15042. LinesToStr([ // $mod.$main
  15043. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  15044. ]));
  15045. end;
  15046. procedure TTestModule.TestClass_Property_IndexSpec;
  15047. begin
  15048. StartProgram(false);
  15049. Add([
  15050. 'type',
  15051. ' TEnum = (red, blue);',
  15052. ' TObject = class',
  15053. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  15054. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  15055. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  15056. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  15057. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  15058. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  15059. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  15060. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  15061. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  15062. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  15063. ' end;',
  15064. 'procedure DoIt(b: boolean); begin end;',
  15065. 'var',
  15066. ' o: TObject;',
  15067. 'begin',
  15068. ' o.B1:=o.B1;',
  15069. ' o.B2:=o.B2;',
  15070. ' o.B3:=o.B3;',
  15071. ' o.I1[''a'']:=o.I1[''b''];',
  15072. ' doit(o.b1);',
  15073. ' doit(o.b2);',
  15074. ' doit(o.i1[''c'']);',
  15075. '']);
  15076. ConvertProgram;
  15077. CheckSource('TestClass_Property_IndexSpec',
  15078. LinesToStr([ // statements
  15079. 'this.TEnum = {',
  15080. ' "0": "red",',
  15081. ' red: 0,',
  15082. ' "1": "blue",',
  15083. ' blue: 1',
  15084. '};',
  15085. 'rtl.createClass(this, "TObject", null, function () {',
  15086. ' this.$init = function () {',
  15087. ' };',
  15088. ' this.$final = function () {',
  15089. ' };',
  15090. '});',
  15091. 'this.DoIt = function (b) {',
  15092. '};',
  15093. 'this.o = null;',
  15094. '']),
  15095. LinesToStr([ // $mod.$main
  15096. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15097. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15098. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15099. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15100. '$mod.DoIt($mod.o.GetIntBool(1));',
  15101. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15102. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15103. '']));
  15104. end;
  15105. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15106. begin
  15107. StartProgram(false);
  15108. Add('type');
  15109. Add(' TArray = array of longint;');
  15110. Add(' TObject = class');
  15111. Add(' FItems: TArray;');
  15112. Add(' function GetItems: tarray;');
  15113. Add(' procedure SetItems(Value: tarray);');
  15114. Add(' property Items: tarray read getitems write setitems;');
  15115. Add(' procedure SetNumbers(const Value: tarray);');
  15116. Add(' property Numbers: tarray write setnumbers;');
  15117. Add(' end;');
  15118. Add('function tobject.getitems: tarray;');
  15119. Add('begin');
  15120. Add(' Result:=fitems;');
  15121. Add('end;');
  15122. Add('procedure tobject.setitems(value: tarray);');
  15123. Add('begin');
  15124. Add(' fitems:=value;');
  15125. Add(' fitems:=nil;');
  15126. Add(' Items:=nil;');
  15127. Add(' Items:=Items;');
  15128. Add(' Items[1]:=2;');
  15129. Add(' fitems[3]:=Items[4];');
  15130. Add(' Items[5]:=Items[6];');
  15131. Add(' Self.Items[7]:=8;');
  15132. Add(' Self.Items[9]:=Self.Items[10];');
  15133. Add(' Items[Items[11]]:=Items[Items[12]];');
  15134. Add('end;');
  15135. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15136. Add('begin;');
  15137. Add(' Numbers:=nil;');
  15138. Add(' Numbers:=Value;');
  15139. Add(' Self.Numbers:=Value;');
  15140. Add('end;');
  15141. Add('var Obj: tobject;');
  15142. Add('begin');
  15143. Add(' obj.items:=nil;');
  15144. Add(' obj.items:=obj.items;');
  15145. Add(' obj.items[11]:=obj.items[12];');
  15146. ConvertProgram;
  15147. CheckSource('TestClass_PropertyOfTypeArray',
  15148. LinesToStr([ // statements
  15149. 'rtl.createClass(this, "TObject", null, function () {',
  15150. ' this.$init = function () {',
  15151. ' this.FItems = [];',
  15152. ' };',
  15153. ' this.$final = function () {',
  15154. ' this.FItems = undefined;',
  15155. ' };',
  15156. ' this.GetItems = function () {',
  15157. ' var Result = [];',
  15158. ' Result = rtl.arrayRef(this.FItems);',
  15159. ' return Result;',
  15160. ' };',
  15161. ' this.SetItems = function (Value) {',
  15162. ' this.FItems = rtl.arrayRef(Value);',
  15163. ' this.FItems = [];',
  15164. ' this.SetItems([]);',
  15165. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15166. ' this.GetItems()[1] = 2;',
  15167. ' this.FItems[3] = this.GetItems()[4];',
  15168. ' this.GetItems()[5] = this.GetItems()[6];',
  15169. ' this.GetItems()[7] = 8;',
  15170. ' this.GetItems()[9] = this.GetItems()[10];',
  15171. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15172. ' };',
  15173. ' this.SetNumbers = function (Value) {',
  15174. ' this.SetNumbers([]);',
  15175. ' this.SetNumbers(Value);',
  15176. ' this.SetNumbers(Value);',
  15177. ' };',
  15178. '});',
  15179. 'this.Obj = null;'
  15180. ]),
  15181. LinesToStr([ // $mod.$main
  15182. '$mod.Obj.SetItems([]);',
  15183. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15184. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15185. ]));
  15186. end;
  15187. procedure TTestModule.TestClass_PropertyDefault;
  15188. begin
  15189. StartProgram(false);
  15190. Add([
  15191. 'type',
  15192. ' TArray = array of longint;',
  15193. ' TObject = class',
  15194. ' end;',
  15195. ' TBird = class',
  15196. ' FItems: TArray;',
  15197. ' function GetItems(Index: longint): longint;',
  15198. ' procedure SetItems(Index, Value: longint);',
  15199. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15200. ' end;',
  15201. 'function TBird.getitems(index: longint): longint;',
  15202. 'begin',
  15203. 'end;',
  15204. 'procedure TBird.setitems(index, value: longint);',
  15205. 'begin',
  15206. ' Self[1]:=2;',
  15207. ' Self[3]:=Self[index];',
  15208. ' Self[index]:=Self[Self[value]];',
  15209. ' Self[Self[4]]:=value;',
  15210. 'end;',
  15211. 'var',
  15212. ' Bird: TBird;',
  15213. ' Obj: TObject;',
  15214. 'begin',
  15215. ' bird[11]:=12;',
  15216. ' bird[13]:=bird[14];',
  15217. ' bird[Bird[15]]:=bird[Bird[15]];',
  15218. ' TBird(obj)[16]:=TBird(obj)[17];',
  15219. ' (obj as tbird)[18]:=19;',
  15220. '']);
  15221. ConvertProgram;
  15222. CheckSource('TestClass_PropertyDefault',
  15223. LinesToStr([ // statements
  15224. 'rtl.createClass(this, "TObject", null, function () {',
  15225. ' this.$init = function () {',
  15226. ' };',
  15227. ' this.$final = function () {',
  15228. ' };',
  15229. '});',
  15230. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15231. ' this.$init = function () {',
  15232. ' $mod.TObject.$init.call(this);',
  15233. ' this.FItems = [];',
  15234. ' };',
  15235. ' this.$final = function () {',
  15236. ' this.FItems = undefined;',
  15237. ' $mod.TObject.$final.call(this);',
  15238. ' };',
  15239. ' this.GetItems = function (Index) {',
  15240. ' var Result = 0;',
  15241. ' return Result;',
  15242. ' };',
  15243. ' this.SetItems = function (Index, Value) {',
  15244. ' this.SetItems(1, 2);',
  15245. ' this.SetItems(3, this.GetItems(Index));',
  15246. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15247. ' this.SetItems(this.GetItems(4), Value);',
  15248. ' };',
  15249. '});',
  15250. 'this.Bird = null;',
  15251. 'this.Obj = null;',
  15252. '']),
  15253. LinesToStr([ // $mod.$main
  15254. '$mod.Bird.SetItems(11, 12);',
  15255. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15256. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15257. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15258. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15259. '']));
  15260. end;
  15261. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15262. begin
  15263. StartProgram(false);
  15264. Add([
  15265. 'type',
  15266. ' TObject = class end;',
  15267. ' TAlphaList = class',
  15268. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15269. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15270. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15271. ' end;',
  15272. ' TBetaList = class',
  15273. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15274. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15275. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15276. ' end;',
  15277. ' TBird = class',
  15278. ' procedure DoIt;',
  15279. ' end;',
  15280. 'procedure TBird.DoIt;',
  15281. 'var',
  15282. ' List: TAlphaList;',
  15283. 'begin',
  15284. ' if TBetaList(List[true])[3]=nil then ;',
  15285. ' TBetaList(List[false])[5]:=nil;',
  15286. 'end;',
  15287. 'var',
  15288. ' List: TAlphaList;',
  15289. 'begin',
  15290. ' if TBetaList(List[true])[3]=nil then ;',
  15291. ' TBetaList(List[false])[5]:=nil;',
  15292. '']);
  15293. ConvertProgram;
  15294. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15295. LinesToStr([ // statements
  15296. 'rtl.createClass(this, "TObject", null, function () {',
  15297. ' this.$init = function () {',
  15298. ' };',
  15299. ' this.$final = function () {',
  15300. ' };',
  15301. '});',
  15302. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15303. '});',
  15304. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15305. '});',
  15306. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15307. ' this.DoIt = function () {',
  15308. ' var List = null;',
  15309. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15310. ' List.GetAlphas(false).SetBetas(5, null);',
  15311. ' };',
  15312. '});',
  15313. 'this.List = null;',
  15314. '']),
  15315. LinesToStr([ // $mod.$main
  15316. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15317. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15318. '']));
  15319. end;
  15320. procedure TTestModule.TestClass_PropertyOverride;
  15321. begin
  15322. StartProgram(false);
  15323. Add('type');
  15324. Add(' integer = longint;');
  15325. Add(' TObject = class');
  15326. Add(' FItem: integer;');
  15327. Add(' function GetItem: integer; external name ''GetItem'';');
  15328. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15329. Add(' property Item: integer read getitem write setitem;');
  15330. Add(' end;');
  15331. Add(' TCar = class');
  15332. Add(' FBag: integer;');
  15333. Add(' function GetBag: integer; external name ''GetBag'';');
  15334. Add(' property Item read getbag;');
  15335. Add(' end;');
  15336. Add('var');
  15337. Add(' Obj: tobject;');
  15338. Add(' Car: tcar;');
  15339. Add('begin');
  15340. Add(' Obj.Item:=Obj.Item;');
  15341. Add(' Car.Item:=Car.Item;');
  15342. ConvertProgram;
  15343. CheckSource('TestClass_PropertyOverride',
  15344. LinesToStr([ // statements
  15345. 'rtl.createClass(this, "TObject", null, function () {',
  15346. ' this.$init = function () {',
  15347. ' this.FItem = 0;',
  15348. ' };',
  15349. ' this.$final = function () {',
  15350. ' };',
  15351. '});',
  15352. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15353. ' this.$init = function () {',
  15354. ' $mod.TObject.$init.call(this);',
  15355. ' this.FBag = 0;',
  15356. ' };',
  15357. '});',
  15358. 'this.Obj = null;',
  15359. 'this.Car = null;',
  15360. '']),
  15361. LinesToStr([ // $mod.$main
  15362. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15363. '$mod.Car.SetItem($mod.Car.GetBag());',
  15364. '']));
  15365. end;
  15366. procedure TTestModule.TestClass_PropertyIncVisibility;
  15367. begin
  15368. AddModuleWithIntfImplSrc('unit1.pp',
  15369. LinesToStr([
  15370. 'type',
  15371. ' TNumber = longint;',
  15372. ' TInteger = longint;',
  15373. ' TObject = class',
  15374. ' private',
  15375. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15376. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15377. ' protected',
  15378. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15379. ' end;']),
  15380. LinesToStr([
  15381. '']));
  15382. StartProgram(true);
  15383. Add([
  15384. 'uses unit1;',
  15385. 'type',
  15386. ' TBird = class',
  15387. ' public',
  15388. ' property Items;',
  15389. ' end;',
  15390. 'procedure DoIt(i: TInteger);',
  15391. 'begin',
  15392. 'end;',
  15393. 'var b: TBird;',
  15394. 'begin',
  15395. ' b.Items[1]:=2;',
  15396. ' b.Items[3]:=b.Items[4];',
  15397. ' DoIt(b.Items[5]);',
  15398. '']);
  15399. ConvertProgram;
  15400. CheckSource('TestClass_PropertyIncVisibility',
  15401. LinesToStr([ // statements
  15402. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15403. '});',
  15404. 'this.DoIt = function (i) {',
  15405. '};',
  15406. 'this.b = null;'
  15407. ]),
  15408. LinesToStr([ // $mod.$main
  15409. '$mod.b.SetItems(1, 2);',
  15410. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15411. '$mod.DoIt($mod.b.GetItems(5));'
  15412. ]));
  15413. end;
  15414. procedure TTestModule.TestClass_Assigned;
  15415. begin
  15416. StartProgram(false);
  15417. Add('type');
  15418. Add(' TObject = class');
  15419. Add(' end;');
  15420. Add('var');
  15421. Add(' Obj: tobject;');
  15422. Add(' b: boolean;');
  15423. Add('begin');
  15424. Add(' if Assigned(obj) then ;');
  15425. Add(' b:=Assigned(obj) or false;');
  15426. ConvertProgram;
  15427. CheckSource('TestClass_Assigned',
  15428. LinesToStr([ // statements
  15429. 'rtl.createClass(this, "TObject", null, function () {',
  15430. ' this.$init = function () {',
  15431. ' };',
  15432. ' this.$final = function () {',
  15433. ' };',
  15434. '});',
  15435. 'this.Obj = null;',
  15436. 'this.b = false;'
  15437. ]),
  15438. LinesToStr([ // $mod.$main
  15439. 'if ($mod.Obj != null);',
  15440. '$mod.b = ($mod.Obj != null) || false;'
  15441. ]));
  15442. end;
  15443. procedure TTestModule.TestClass_WithClassDoCreate;
  15444. begin
  15445. StartProgram(false);
  15446. Add('type');
  15447. Add(' TObject = class');
  15448. Add(' aBool: boolean;');
  15449. Add(' Arr: array of boolean;');
  15450. Add(' constructor Create;');
  15451. Add(' end;');
  15452. Add('constructor TObject.Create; begin end;');
  15453. Add('var');
  15454. Add(' Obj: tobject;');
  15455. Add(' b: boolean;');
  15456. Add('begin');
  15457. Add(' with tobject.create do begin');
  15458. Add(' b:=abool;');
  15459. Add(' abool:=b;');
  15460. Add(' b:=arr[1];');
  15461. Add(' arr[2]:=b;');
  15462. Add(' end;');
  15463. Add(' with tobject do');
  15464. Add(' obj:=create;');
  15465. Add(' with obj do begin');
  15466. Add(' create;');
  15467. Add(' b:=abool;');
  15468. Add(' abool:=b;');
  15469. Add(' b:=arr[3];');
  15470. Add(' arr[4]:=b;');
  15471. Add(' end;');
  15472. ConvertProgram;
  15473. CheckSource('TestClass_WithClassDoCreate',
  15474. LinesToStr([ // statements
  15475. 'rtl.createClass(this, "TObject", null, function () {',
  15476. ' this.$init = function () {',
  15477. ' this.aBool = false;',
  15478. ' this.Arr = [];',
  15479. ' };',
  15480. ' this.$final = function () {',
  15481. ' this.Arr = undefined;',
  15482. ' };',
  15483. ' this.Create = function () {',
  15484. ' return this;',
  15485. ' };',
  15486. '});',
  15487. 'this.Obj = null;',
  15488. 'this.b = false;'
  15489. ]),
  15490. LinesToStr([ // $mod.$main
  15491. 'var $with = $mod.TObject.$create("Create");',
  15492. '$mod.b = $with.aBool;',
  15493. '$with.aBool = $mod.b;',
  15494. '$mod.b = $with.Arr[1];',
  15495. '$with.Arr[2] = $mod.b;',
  15496. 'var $with1 = $mod.TObject;',
  15497. '$mod.Obj = $with1.$create("Create");',
  15498. 'var $with2 = $mod.Obj;',
  15499. '$with2.Create();',
  15500. '$mod.b = $with2.aBool;',
  15501. '$with2.aBool = $mod.b;',
  15502. '$mod.b = $with2.Arr[3];',
  15503. '$with2.Arr[4] = $mod.b;',
  15504. '']));
  15505. end;
  15506. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15507. begin
  15508. StartProgram(false);
  15509. Add('type');
  15510. Add(' TObject = class');
  15511. Add(' FInt: longint;');
  15512. Add(' constructor Create;');
  15513. Add(' function GetSize: longint;');
  15514. Add(' procedure SetSize(Value: longint);');
  15515. Add(' property Int: longint read FInt write FInt;');
  15516. Add(' property Size: longint read GetSize write SetSize;');
  15517. Add(' end;');
  15518. Add('constructor TObject.Create; begin end;');
  15519. Add('function TObject.GetSize: longint; begin; end;');
  15520. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15521. Add('var');
  15522. Add(' Obj: tobject;');
  15523. Add(' i: longint;');
  15524. Add('begin');
  15525. Add(' with TObject.Create do begin');
  15526. Add(' i:=int;');
  15527. Add(' int:=i;');
  15528. Add(' i:=size;');
  15529. Add(' size:=i;');
  15530. Add(' end;');
  15531. Add(' with obj do begin');
  15532. Add(' i:=int;');
  15533. Add(' int:=i;');
  15534. Add(' i:=size;');
  15535. Add(' size:=i;');
  15536. Add(' end;');
  15537. ConvertProgram;
  15538. CheckSource('TestClass_WithClassInstDoProperty',
  15539. LinesToStr([ // statements
  15540. 'rtl.createClass(this, "TObject", null, function () {',
  15541. ' this.$init = function () {',
  15542. ' this.FInt = 0;',
  15543. ' };',
  15544. ' this.$final = function () {',
  15545. ' };',
  15546. ' this.Create = function () {',
  15547. ' return this;',
  15548. ' };',
  15549. ' this.GetSize = function () {',
  15550. ' var Result = 0;',
  15551. ' return Result;',
  15552. ' };',
  15553. ' this.SetSize = function (Value) {',
  15554. ' };',
  15555. '});',
  15556. 'this.Obj = null;',
  15557. 'this.i = 0;'
  15558. ]),
  15559. LinesToStr([ // $mod.$main
  15560. 'var $with = $mod.TObject.$create("Create");',
  15561. '$mod.i = $with.FInt;',
  15562. '$with.FInt = $mod.i;',
  15563. '$mod.i = $with.GetSize();',
  15564. '$with.SetSize($mod.i);',
  15565. 'var $with1 = $mod.Obj;',
  15566. '$mod.i = $with1.FInt;',
  15567. '$with1.FInt = $mod.i;',
  15568. '$mod.i = $with1.GetSize();',
  15569. '$with1.SetSize($mod.i);',
  15570. '']));
  15571. end;
  15572. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15573. begin
  15574. StartProgram(false);
  15575. Add('type');
  15576. Add(' TObject = class');
  15577. Add(' constructor Create;');
  15578. Add(' function GetItems(Index: longint): longint;');
  15579. Add(' procedure SetItems(Index, Value: longint);');
  15580. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15581. Add(' end;');
  15582. Add('constructor TObject.Create; begin end;');
  15583. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15584. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15585. Add('var');
  15586. Add(' Obj: tobject;');
  15587. Add(' i: longint;');
  15588. Add('begin');
  15589. Add(' with TObject.Create do begin');
  15590. Add(' i:=Items[1];');
  15591. Add(' Items[2]:=i;');
  15592. Add(' end;');
  15593. Add(' with obj do begin');
  15594. Add(' i:=Items[3];');
  15595. Add(' Items[4]:=i;');
  15596. Add(' end;');
  15597. ConvertProgram;
  15598. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15599. LinesToStr([ // statements
  15600. 'rtl.createClass(this, "TObject", null, function () {',
  15601. ' this.$init = function () {',
  15602. ' };',
  15603. ' this.$final = function () {',
  15604. ' };',
  15605. ' this.Create = function () {',
  15606. ' return this;',
  15607. ' };',
  15608. ' this.GetItems = function (Index) {',
  15609. ' var Result = 0;',
  15610. ' return Result;',
  15611. ' };',
  15612. ' this.SetItems = function (Index, Value) {',
  15613. ' };',
  15614. '});',
  15615. 'this.Obj = null;',
  15616. 'this.i = 0;'
  15617. ]),
  15618. LinesToStr([ // $mod.$main
  15619. 'var $with = $mod.TObject.$create("Create");',
  15620. '$mod.i = $with.GetItems(1);',
  15621. '$with.SetItems(2, $mod.i);',
  15622. 'var $with1 = $mod.Obj;',
  15623. '$mod.i = $with1.GetItems(3);',
  15624. '$with1.SetItems(4, $mod.i);',
  15625. '']));
  15626. end;
  15627. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15628. begin
  15629. StartProgram(false);
  15630. Add('type');
  15631. Add(' TObject = class');
  15632. Add(' constructor Create;');
  15633. Add(' function GetSize: longint;');
  15634. Add(' procedure SetSize(Value: longint);');
  15635. Add(' end;');
  15636. Add('constructor TObject.Create; begin end;');
  15637. Add('function TObject.GetSize: longint; begin; end;');
  15638. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15639. Add('var');
  15640. Add(' Obj: tobject;');
  15641. Add(' i: longint;');
  15642. Add('begin');
  15643. Add(' with TObject.Create do begin');
  15644. Add(' i:=GetSize;');
  15645. Add(' i:=GetSize();');
  15646. Add(' SetSize(i);');
  15647. Add(' end;');
  15648. Add(' with obj do begin');
  15649. Add(' i:=GetSize;');
  15650. Add(' i:=GetSize();');
  15651. Add(' SetSize(i);');
  15652. Add(' end;');
  15653. ConvertProgram;
  15654. CheckSource('TestClass_WithClassInstDoFunc',
  15655. LinesToStr([ // statements
  15656. 'rtl.createClass(this, "TObject", null, function () {',
  15657. ' this.$init = function () {',
  15658. ' };',
  15659. ' this.$final = function () {',
  15660. ' };',
  15661. ' this.Create = function () {',
  15662. ' return this;',
  15663. ' };',
  15664. ' this.GetSize = function () {',
  15665. ' var Result = 0;',
  15666. ' return Result;',
  15667. ' };',
  15668. ' this.SetSize = function (Value) {',
  15669. ' };',
  15670. '});',
  15671. 'this.Obj = null;',
  15672. 'this.i = 0;'
  15673. ]),
  15674. LinesToStr([ // $mod.$main
  15675. 'var $with = $mod.TObject.$create("Create");',
  15676. '$mod.i = $with.GetSize();',
  15677. '$mod.i = $with.GetSize();',
  15678. '$with.SetSize($mod.i);',
  15679. 'var $with1 = $mod.Obj;',
  15680. '$mod.i = $with1.GetSize();',
  15681. '$mod.i = $with1.GetSize();',
  15682. '$with1.SetSize($mod.i);',
  15683. '']));
  15684. end;
  15685. procedure TTestModule.TestClass_TypeCast;
  15686. begin
  15687. StartProgram(false);
  15688. Add('type');
  15689. Add(' TObject = class');
  15690. Add(' Next: TObject;');
  15691. Add(' constructor Create;');
  15692. Add(' end;');
  15693. Add(' TControl = class(TObject)');
  15694. Add(' Arr: array of TObject;');
  15695. Add(' function GetIt(vI: longint = 0): TObject;');
  15696. Add(' end;');
  15697. Add('constructor tobject.create; begin end;');
  15698. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15699. Add('var');
  15700. Add(' Obj: tobject;');
  15701. Add('begin');
  15702. Add(' obj:=tcontrol(obj).next;');
  15703. Add(' tcontrol(obj):=nil;');
  15704. Add(' obj:=tcontrol(obj);');
  15705. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15706. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15707. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15708. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15709. Add(' obj:=tcontrol(nil);');
  15710. ConvertProgram;
  15711. CheckSource('TestClass_TypeCast',
  15712. LinesToStr([ // statements
  15713. 'rtl.createClass(this, "TObject", null, function () {',
  15714. ' this.$init = function () {',
  15715. ' this.Next = null;',
  15716. ' };',
  15717. ' this.$final = function () {',
  15718. ' this.Next = undefined;',
  15719. ' };',
  15720. ' this.Create = function () {',
  15721. ' return this;',
  15722. ' };',
  15723. '});',
  15724. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15725. ' this.$init = function () {',
  15726. ' $mod.TObject.$init.call(this);',
  15727. ' this.Arr = [];',
  15728. ' };',
  15729. ' this.$final = function () {',
  15730. ' this.Arr = undefined;',
  15731. ' $mod.TObject.$final.call(this);',
  15732. ' };',
  15733. ' this.GetIt = function (vI) {',
  15734. ' var Result = null;',
  15735. ' return Result;',
  15736. ' };',
  15737. '});',
  15738. 'this.Obj = null;'
  15739. ]),
  15740. LinesToStr([ // $mod.$main
  15741. '$mod.Obj = $mod.Obj.Next;',
  15742. '$mod.Obj = null;',
  15743. '$mod.Obj = $mod.Obj;',
  15744. '$mod.Obj = $mod.Obj.GetIt(0);',
  15745. '$mod.Obj = $mod.Obj.GetIt(0);',
  15746. '$mod.Obj = $mod.Obj.GetIt(1);',
  15747. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15748. '$mod.Obj = null;',
  15749. '']));
  15750. end;
  15751. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15752. begin
  15753. StartProgram(false);
  15754. Add('type');
  15755. Add(' TObject = class end;');
  15756. Add('procedure ProcA(var A);');
  15757. Add('begin');
  15758. Add(' TObject(A):=nil;');
  15759. Add(' TObject(A):=TObject(A);');
  15760. Add(' if TObject(A)=nil then ;');
  15761. Add(' if nil=TObject(A) then ;');
  15762. Add('end;');
  15763. Add('procedure ProcB(out A);');
  15764. Add('begin');
  15765. Add(' TObject(A):=nil;');
  15766. Add(' TObject(A):=TObject(A);');
  15767. Add(' if TObject(A)=nil then ;');
  15768. Add(' if nil=TObject(A) then ;');
  15769. Add('end;');
  15770. Add('procedure ProcC(const A);');
  15771. Add('begin');
  15772. Add(' if TObject(A)=nil then ;');
  15773. Add(' if nil=TObject(A) then ;');
  15774. Add('end;');
  15775. Add('var o: TObject;');
  15776. Add('begin');
  15777. Add(' ProcA(o);');
  15778. Add(' ProcB(o);');
  15779. Add(' ProcC(o);');
  15780. ConvertProgram;
  15781. CheckSource('TestClass_TypeCastUntypedParam',
  15782. LinesToStr([ // statements
  15783. 'rtl.createClass(this, "TObject", null, function () {',
  15784. ' this.$init = function () {',
  15785. ' };',
  15786. ' this.$final = function () {',
  15787. ' };',
  15788. '});',
  15789. 'this.ProcA = function (A) {',
  15790. ' A.set(null);',
  15791. ' A.set(A.get());',
  15792. ' if (A.get() === null);',
  15793. ' if (null === A.get());',
  15794. '};',
  15795. 'this.ProcB = function (A) {',
  15796. ' A.set(null);',
  15797. ' A.set(A.get());',
  15798. ' if (A.get() === null);',
  15799. ' if (null === A.get());',
  15800. '};',
  15801. 'this.ProcC = function (A) {',
  15802. ' if (A === null);',
  15803. ' if (null === A);',
  15804. '};',
  15805. 'this.o = null;',
  15806. '']),
  15807. LinesToStr([ // $mod.$main
  15808. '$mod.ProcA({',
  15809. ' p: $mod,',
  15810. ' get: function () {',
  15811. ' return this.p.o;',
  15812. ' },',
  15813. ' set: function (v) {',
  15814. ' this.p.o = v;',
  15815. ' }',
  15816. '});',
  15817. '$mod.ProcB({',
  15818. ' p: $mod,',
  15819. ' get: function () {',
  15820. ' return this.p.o;',
  15821. ' },',
  15822. ' set: function (v) {',
  15823. ' this.p.o = v;',
  15824. ' }',
  15825. '});',
  15826. '$mod.ProcC($mod.o);',
  15827. '']));
  15828. end;
  15829. procedure TTestModule.TestClass_Overloads;
  15830. begin
  15831. StartProgram(false);
  15832. Add('type');
  15833. Add(' TObject = class');
  15834. Add(' procedure DoIt;');
  15835. Add(' procedure DoIt(vI: longint);');
  15836. Add(' end;');
  15837. Add('procedure TObject.DoIt;');
  15838. Add('begin');
  15839. Add(' DoIt;');
  15840. Add(' DoIt(1);');
  15841. Add('end;');
  15842. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15843. Add('begin');
  15844. ConvertProgram;
  15845. CheckSource('TestClass_Overloads',
  15846. LinesToStr([ // statements
  15847. 'rtl.createClass(this, "TObject", null, function () {',
  15848. ' this.$init = function () {',
  15849. ' };',
  15850. ' this.$final = function () {',
  15851. ' };',
  15852. ' this.DoIt = function () {',
  15853. ' this.DoIt();',
  15854. ' this.DoIt$1(1);',
  15855. ' };',
  15856. ' this.DoIt$1 = function (vI) {',
  15857. ' };',
  15858. '});',
  15859. '']),
  15860. LinesToStr([ // $mod.$main
  15861. '']));
  15862. end;
  15863. procedure TTestModule.TestClass_OverloadsAncestor;
  15864. begin
  15865. StartProgram(false);
  15866. Add('type');
  15867. Add(' TObject = class;');
  15868. Add(' TObject = class');
  15869. Add(' procedure DoIt(vA: longint);');
  15870. Add(' procedure DoIt(vA, vB: longint);');
  15871. Add(' end;');
  15872. Add(' TCar = class;');
  15873. Add(' TCar = class');
  15874. Add(' procedure DoIt(vA: longint);');
  15875. Add(' procedure DoIt(vA, vB: longint);');
  15876. Add(' end;');
  15877. Add('procedure tobject.doit(va: longint);');
  15878. Add('begin');
  15879. Add(' doit(1);');
  15880. Add(' doit(1,2);');
  15881. Add('end;');
  15882. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15883. Add('procedure tcar.doit(va: longint);');
  15884. Add('begin');
  15885. Add(' doit(1);');
  15886. Add(' doit(1,2);');
  15887. Add(' inherited doit(1);');
  15888. Add(' inherited doit(1,2);');
  15889. Add('end;');
  15890. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15891. Add('begin');
  15892. ConvertProgram;
  15893. CheckSource('TestClass_OverloadsAncestor',
  15894. LinesToStr([ // statements
  15895. 'rtl.createClass(this, "TObject", null, function () {',
  15896. ' this.$init = function () {',
  15897. ' };',
  15898. ' this.$final = function () {',
  15899. ' };',
  15900. ' this.DoIt = function (vA) {',
  15901. ' this.DoIt(1);',
  15902. ' this.DoIt$1(1,2);',
  15903. ' };',
  15904. ' this.DoIt$1 = function (vA, vB) {',
  15905. ' };',
  15906. '});',
  15907. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15908. ' this.DoIt$2 = function (vA) {',
  15909. ' this.DoIt$2(1);',
  15910. ' this.DoIt$3(1, 2);',
  15911. ' $mod.TObject.DoIt.call(this, 1);',
  15912. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15913. ' };',
  15914. ' this.DoIt$3 = function (vA, vB) {',
  15915. ' };',
  15916. '});',
  15917. '']),
  15918. LinesToStr([ // $mod.$main
  15919. '']));
  15920. end;
  15921. procedure TTestModule.TestClass_OverloadConstructor;
  15922. begin
  15923. StartProgram(false);
  15924. Add('type');
  15925. Add(' TObject = class');
  15926. Add(' constructor Create(vA: longint);');
  15927. Add(' constructor Create(vA, vB: longint);');
  15928. Add(' end;');
  15929. Add(' TCar = class');
  15930. Add(' constructor Create(vA: longint);');
  15931. Add(' constructor Create(vA, vB: longint);');
  15932. Add(' end;');
  15933. Add('constructor tobject.create(va: longint);');
  15934. Add('begin');
  15935. Add(' create(1);');
  15936. Add(' create(1,2);');
  15937. Add('end;');
  15938. Add('constructor tobject.create(va, vb: longint); begin end;');
  15939. Add('constructor tcar.create(va: longint);');
  15940. Add('begin');
  15941. Add(' create(1);');
  15942. Add(' create(1,2);');
  15943. Add(' inherited create(1);');
  15944. Add(' inherited create(1,2);');
  15945. Add('end;');
  15946. Add('constructor tcar.create(va, vb: longint); begin end;');
  15947. Add('begin');
  15948. Add(' tobject.create(1);');
  15949. Add(' tobject.create(1,2);');
  15950. Add(' tcar.create(1);');
  15951. Add(' tcar.create(1,2);');
  15952. ConvertProgram;
  15953. CheckSource('TestClass_OverloadConstructor',
  15954. LinesToStr([ // statements
  15955. 'rtl.createClass(this, "TObject", null, function () {',
  15956. ' this.$init = function () {',
  15957. ' };',
  15958. ' this.$final = function () {',
  15959. ' };',
  15960. ' this.Create = function (vA) {',
  15961. ' this.Create(1);',
  15962. ' this.Create$1(1,2);',
  15963. ' return this;',
  15964. ' };',
  15965. ' this.Create$1 = function (vA, vB) {',
  15966. ' return this;',
  15967. ' };',
  15968. '});',
  15969. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15970. ' this.Create$2 = function (vA) {',
  15971. ' this.Create$2(1);',
  15972. ' this.Create$3(1, 2);',
  15973. ' $mod.TObject.Create.call(this, 1);',
  15974. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15975. ' return this;',
  15976. ' };',
  15977. ' this.Create$3 = function (vA, vB) {',
  15978. ' return this;',
  15979. ' };',
  15980. '});',
  15981. '']),
  15982. LinesToStr([ // $mod.$main
  15983. '$mod.TObject.$create("Create", [1]);',
  15984. '$mod.TObject.$create("Create$1", [1, 2]);',
  15985. '$mod.TCar.$create("Create$2", [1]);',
  15986. '$mod.TCar.$create("Create$3", [1, 2]);',
  15987. '']));
  15988. end;
  15989. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15990. begin
  15991. StartProgram(false);
  15992. Add([
  15993. '{$mode delphi}',
  15994. 'type',
  15995. ' TObject = class end;',
  15996. ' TBird = class',
  15997. ' function {#a}GetValue: longint; overload; virtual;',
  15998. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15999. ' end;',
  16000. ' TEagle = class(TBird)',
  16001. ' function {#c}GetValue: longint; overload; override;',
  16002. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  16003. ' end;',
  16004. 'function TBird.GetValue: longint;',
  16005. 'begin',
  16006. ' if 3={@a}GetValue then ;',
  16007. ' if 4={@b}GetValue(5) then ;',
  16008. 'end;',
  16009. 'function TBird.GetValue(AValue: longint): longint;',
  16010. 'begin',
  16011. 'end;',
  16012. 'function TEagle.GetValue: longint;',
  16013. 'begin',
  16014. ' if 13={@c}GetValue then ;',
  16015. ' if 14={@d}GetValue(15) then ;',
  16016. ' if 15=inherited {@a}GetValue then ;',
  16017. ' if 16=inherited {@b}GetValue(17) then ;',
  16018. 'end;',
  16019. 'function TEagle.GetValue(AValue: longint): longint;',
  16020. 'begin',
  16021. 'end;',
  16022. 'var',
  16023. ' e: TEagle;',
  16024. 'begin',
  16025. ' if 23=e.{@c}GetValue then ;',
  16026. ' if 24=e.{@d}GetValue(25) then ;']);
  16027. ConvertProgram;
  16028. CheckSource('TestClass_OverloadDelphiOverride',
  16029. LinesToStr([ // statements
  16030. 'rtl.createClass(this, "TObject", null, function () {',
  16031. ' this.$init = function () {',
  16032. ' };',
  16033. ' this.$final = function () {',
  16034. ' };',
  16035. '});',
  16036. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16037. ' this.GetValue = function () {',
  16038. ' var Result = 0;',
  16039. ' if (3 === this.GetValue()) ;',
  16040. ' if (4 === this.GetValue$1(5)) ;',
  16041. ' return Result;',
  16042. ' };',
  16043. ' this.GetValue$1 = function (AValue) {',
  16044. ' var Result = 0;',
  16045. ' return Result;',
  16046. ' };',
  16047. '});',
  16048. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16049. ' this.GetValue = function () {',
  16050. ' var Result = 0;',
  16051. ' if (13 === this.GetValue()) ;',
  16052. ' if (14 === this.GetValue$1(15)) ;',
  16053. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  16054. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  16055. ' return Result;',
  16056. ' };',
  16057. ' this.GetValue$1 = function (AValue) {',
  16058. ' var Result = 0;',
  16059. ' return Result;',
  16060. ' };',
  16061. '});',
  16062. 'this.e = null;',
  16063. '']),
  16064. LinesToStr([ // $mod.$main
  16065. 'if (23 === $mod.e.GetValue()) ;',
  16066. 'if (24 === $mod.e.GetValue$1(25)) ;',
  16067. '']));
  16068. end;
  16069. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  16070. begin
  16071. StartProgram(false);
  16072. Add([
  16073. '{$mode delphi}',
  16074. 'type',
  16075. ' TObject = class end;',
  16076. ' TAnimal = class',
  16077. ' public',
  16078. ' {#animal_a}A: longint;',
  16079. ' function {#animal_b}B: longint;',
  16080. ' end;',
  16081. ' TBird = class(TAnimal)',
  16082. ' public',
  16083. ' {#bird_a}A: double;',
  16084. ' {#bird_b}B: boolean;',
  16085. ' end;',
  16086. ' TEagle = class(TBird)',
  16087. ' public',
  16088. ' function {#eagle_a}A: boolean;',
  16089. ' {#eagle_b}B: double;',
  16090. ' end;',
  16091. 'function TAnimal.B: longint;',
  16092. 'begin',
  16093. 'end;',
  16094. 'function TEagle.A: boolean;',
  16095. 'begin',
  16096. ' {@eagle_b}B:=3.3;',
  16097. ' {@eagle_a}A();',
  16098. ' TBird(Self).{@bird_b}B:=true;',
  16099. ' TAnimal(Self).{@animal_a}A:=17;',
  16100. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16101. 'end;',
  16102. 'var',
  16103. ' e: TEagle;',
  16104. 'begin',
  16105. ' e.{@eagle_b}B:=5.3;',
  16106. ' if e.{@eagle_a}A then ;',
  16107. '']);
  16108. ConvertProgram;
  16109. CheckSource('TestClass_ReintroduceVarDelphi',
  16110. LinesToStr([ // statements
  16111. 'rtl.createClass(this, "TObject", null, function () {',
  16112. ' this.$init = function () {',
  16113. ' };',
  16114. ' this.$final = function () {',
  16115. ' };',
  16116. '});',
  16117. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16118. ' this.$init = function () {',
  16119. ' $mod.TObject.$init.call(this);',
  16120. ' this.A = 0;',
  16121. ' };',
  16122. ' this.B = function () {',
  16123. ' var Result = 0;',
  16124. ' return Result;',
  16125. ' };',
  16126. '});',
  16127. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16128. ' this.$init = function () {',
  16129. ' $mod.TAnimal.$init.call(this);',
  16130. ' this.A$1 = 0.0;',
  16131. ' this.B$1 = false;',
  16132. ' };',
  16133. '});',
  16134. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16135. ' this.$init = function () {',
  16136. ' $mod.TBird.$init.call(this);',
  16137. ' this.B$2 = 0.0;',
  16138. ' };',
  16139. ' this.A$2 = function () {',
  16140. ' var Result = false;',
  16141. ' this.B$2 = 3.3;',
  16142. ' this.A$2();',
  16143. ' this.B$1 = true;',
  16144. ' this.A = 17;',
  16145. ' this.B$1 = this.A$1 > 1;',
  16146. ' return Result;',
  16147. ' };',
  16148. '});',
  16149. 'this.e = null;',
  16150. '']),
  16151. LinesToStr([ // $mod.$main
  16152. '$mod.e.B$2 = 5.3;',
  16153. 'if ($mod.e.A$2()) ;',
  16154. '']));
  16155. end;
  16156. procedure TTestModule.TestClass_ReintroducedVar;
  16157. begin
  16158. StartProgram(false);
  16159. Add('type');
  16160. Add(' TObject = class');
  16161. Add(' strict private');
  16162. Add(' Some: longint;');
  16163. Add(' end;');
  16164. Add(' TMobile = class');
  16165. Add(' strict private');
  16166. Add(' Some: string;');
  16167. Add(' end;');
  16168. Add(' TCar = class(tmobile)');
  16169. Add(' procedure Some;');
  16170. Add(' procedure Some(vA: longint);');
  16171. Add(' end;');
  16172. Add('procedure tcar.some;');
  16173. Add('begin');
  16174. Add(' Some;');
  16175. Add(' Some(1);');
  16176. Add('end;');
  16177. Add('procedure tcar.some(va: longint); begin end;');
  16178. Add('begin');
  16179. ConvertProgram;
  16180. CheckSource('TestClass_ReintroducedVar',
  16181. LinesToStr([ // statements
  16182. 'rtl.createClass(this, "TObject", null, function () {',
  16183. ' this.$init = function () {',
  16184. ' this.Some = 0;',
  16185. ' };',
  16186. ' this.$final = function () {',
  16187. ' };',
  16188. '});',
  16189. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16190. ' this.$init = function () {',
  16191. ' $mod.TObject.$init.call(this);',
  16192. ' this.Some$1 = "";',
  16193. ' };',
  16194. '});',
  16195. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16196. ' this.Some$2 = function () {',
  16197. ' this.Some$2();',
  16198. ' this.Some$3(1);',
  16199. ' };',
  16200. ' this.Some$3 = function (vA) {',
  16201. ' };',
  16202. '});',
  16203. '']),
  16204. LinesToStr([ // $mod.$main
  16205. '']));
  16206. end;
  16207. procedure TTestModule.TestClass_RaiseDescendant;
  16208. begin
  16209. StartProgram(false);
  16210. Add([
  16211. 'type',
  16212. ' TObject = class',
  16213. ' constructor Create(Msg: string);',
  16214. ' end;',
  16215. ' Exception = class',
  16216. ' end;',
  16217. ' EConvertError = class(Exception)',
  16218. ' end;',
  16219. 'constructor TObject.Create(Msg: string); begin end;',
  16220. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16221. 'begin',
  16222. ' raise Exception.Create(''Bar1'');',
  16223. ' raise EConvertError.Create(''Bar2'');',
  16224. ' raise AssertConv(''Bar2'');',
  16225. ' raise AssertConv;',
  16226. '']);
  16227. ConvertProgram;
  16228. CheckSource('TestClass_RaiseDescendant',
  16229. LinesToStr([ // statements
  16230. 'rtl.createClass(this, "TObject", null, function () {',
  16231. ' this.$init = function () {',
  16232. ' };',
  16233. ' this.$final = function () {',
  16234. ' };',
  16235. ' this.Create = function (Msg) {',
  16236. ' return this;',
  16237. ' };',
  16238. '});',
  16239. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16240. '});',
  16241. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16242. '});',
  16243. 'this.AssertConv = function (Msg) {',
  16244. ' var Result = null;',
  16245. ' return Result;',
  16246. '};',
  16247. '']),
  16248. LinesToStr([ // $mod.$main
  16249. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16250. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16251. 'throw $mod.AssertConv("Bar2");',
  16252. 'throw $mod.AssertConv("def");',
  16253. '']));
  16254. end;
  16255. procedure TTestModule.TestClass_ExternalMethod;
  16256. begin
  16257. AddModuleWithIntfImplSrc('unit2.pas',
  16258. LinesToStr([
  16259. 'type',
  16260. ' TObject = class',
  16261. ' public',
  16262. ' procedure Intern; external name ''$DoIntern'';',
  16263. ' end;',
  16264. '']),
  16265. LinesToStr([
  16266. '']));
  16267. StartUnit(true);
  16268. Add('interface');
  16269. Add('uses unit2;');
  16270. Add('type');
  16271. Add(' TCar = class(TObject)');
  16272. Add(' public');
  16273. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16274. Add(' procedure DoIt;');
  16275. Add(' end;');
  16276. Add('implementation');
  16277. Add('procedure tcar.doit;');
  16278. Add('begin');
  16279. Add(' Intern;');
  16280. Add(' Intern();');
  16281. Add(' Intern2;');
  16282. Add(' Intern2();');
  16283. Add('end;');
  16284. Add('var Obj: TCar;');
  16285. Add('begin');
  16286. Add(' obj.intern;');
  16287. Add(' obj.intern();');
  16288. Add(' obj.intern2;');
  16289. Add(' obj.intern2();');
  16290. Add(' obj.doit;');
  16291. Add(' obj.doit();');
  16292. Add(' with obj do begin');
  16293. Add(' Intern;');
  16294. Add(' Intern();');
  16295. Add(' Intern2;');
  16296. Add(' Intern2();');
  16297. Add(' end;');
  16298. ConvertUnit;
  16299. CheckSource('TestClass_ExternalMethod',
  16300. LinesToStr([
  16301. 'var $impl = $mod.$impl;',
  16302. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16303. ' this.DoIt = function () {',
  16304. ' this.$DoIntern();',
  16305. ' this.$DoIntern();',
  16306. ' this.$DoIntern2();',
  16307. ' this.$DoIntern2();',
  16308. ' };',
  16309. ' });',
  16310. '']),
  16311. LinesToStr([ // this.$init
  16312. '$impl.Obj.$DoIntern();',
  16313. '$impl.Obj.$DoIntern();',
  16314. '$impl.Obj.$DoIntern2();',
  16315. '$impl.Obj.$DoIntern2();',
  16316. '$impl.Obj.DoIt();',
  16317. '$impl.Obj.DoIt();',
  16318. 'var $with = $impl.Obj;',
  16319. '$with.$DoIntern();',
  16320. '$with.$DoIntern();',
  16321. '$with.$DoIntern2();',
  16322. '$with.$DoIntern2();',
  16323. '']),
  16324. LinesToStr([ // implementation
  16325. '$impl.Obj = null;',
  16326. '']) );
  16327. end;
  16328. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16329. begin
  16330. StartProgram(false);
  16331. Add('type');
  16332. Add(' TObject = class');
  16333. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16334. Add(' end;');
  16335. Add('begin');
  16336. SetExpectedPasResolverError('Virtual method name must match external',
  16337. nVirtualMethodNameMustMatchExternal);
  16338. ConvertProgram;
  16339. end;
  16340. procedure TTestModule.TestClass_ExternalOverrideFail;
  16341. begin
  16342. StartProgram(false);
  16343. Add('type');
  16344. Add(' TObject = class');
  16345. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16346. Add(' end;');
  16347. Add(' TCar = class');
  16348. Add(' procedure DoIt; override; external name ''DoIt'';');
  16349. Add(' end;');
  16350. Add('begin');
  16351. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16352. nInvalidXModifierY);
  16353. ConvertProgram;
  16354. end;
  16355. procedure TTestModule.TestClass_ExternalVar;
  16356. begin
  16357. AddModuleWithIntfImplSrc('unit2.pas',
  16358. LinesToStr([
  16359. '{$modeswitch externalclass}',
  16360. 'type',
  16361. ' TObject = class',
  16362. ' public',
  16363. ' Intern: longint external name ''$Intern'';',
  16364. ' Bracket: longint external name ''["A B"]'';',
  16365. ' end;',
  16366. '']),
  16367. LinesToStr([
  16368. '']));
  16369. StartUnit(true);
  16370. Add([
  16371. 'interface',
  16372. 'uses unit2;',
  16373. '{$modeswitch externalclass}',
  16374. 'type',
  16375. ' TCar = class(tobject)',
  16376. ' public',
  16377. ' Intern2: longint external name ''$Intern2'';',
  16378. ' procedure DoIt;',
  16379. ' end;',
  16380. 'implementation',
  16381. 'procedure tcar.doit;',
  16382. 'begin',
  16383. ' Intern:=Intern+1;',
  16384. ' Intern2:=Intern2+2;',
  16385. ' Bracket:=Bracket+3;',
  16386. 'end;',
  16387. 'var Obj: TCar;',
  16388. 'begin',
  16389. ' obj.intern:=obj.intern+1;',
  16390. ' obj.intern2:=obj.intern2+2;',
  16391. ' obj.Bracket:=obj.Bracket+3;',
  16392. ' with obj do begin',
  16393. ' intern:=intern+1;',
  16394. ' intern2:=intern2+2;',
  16395. ' Bracket:=Bracket+3;',
  16396. ' end;']);
  16397. ConvertUnit;
  16398. CheckSource('TestClass_ExternalVar',
  16399. LinesToStr([
  16400. 'var $impl = $mod.$impl;',
  16401. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16402. ' this.DoIt = function () {',
  16403. ' this.$Intern = this.$Intern + 1;',
  16404. ' this.$Intern2 = this.$Intern2 + 2;',
  16405. ' this["A B"] = this["A B"] + 3;',
  16406. ' };',
  16407. ' });',
  16408. '']),
  16409. LinesToStr([
  16410. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16411. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16412. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16413. 'var $with = $impl.Obj;',
  16414. '$with.$Intern = $with.$Intern + 1;',
  16415. '$with.$Intern2 = $with.$Intern2 + 2;',
  16416. '$with["A B"] = $with["A B"] + 3;',
  16417. '']),
  16418. LinesToStr([ // implementation
  16419. '$impl.Obj = null;',
  16420. '']));
  16421. end;
  16422. procedure TTestModule.TestClass_Const;
  16423. begin
  16424. StartProgram(false);
  16425. Add([
  16426. 'type',
  16427. ' integer = longint;',
  16428. ' TClass = class of TObject;',
  16429. ' TObject = class',
  16430. ' public',
  16431. ' const cI: integer = 3;',
  16432. ' procedure DoIt;',
  16433. ' class procedure DoMore;',
  16434. ' end;',
  16435. 'procedure tobject.doit;',
  16436. 'begin',
  16437. ' if cI=4 then;',
  16438. ' if 5=cI then;',
  16439. ' if Self.cI=6 then;',
  16440. ' if 7=Self.cI then;',
  16441. ' with Self do begin',
  16442. ' if cI=11 then;',
  16443. ' if 12=cI then;',
  16444. ' end;',
  16445. 'end;',
  16446. 'class procedure tobject.domore;',
  16447. 'begin',
  16448. ' if cI=8 then;',
  16449. ' if Self.cI=9 then;',
  16450. ' if 10=cI then;',
  16451. ' if 11=Self.cI then;',
  16452. ' with Self do begin',
  16453. ' if cI=13 then;',
  16454. ' if 14=cI then;',
  16455. ' end;',
  16456. 'end;',
  16457. 'var',
  16458. ' Obj: TObject;',
  16459. ' Cla: TClass;',
  16460. 'begin',
  16461. ' if TObject.cI=21 then ;',
  16462. ' if Obj.cI=22 then ;',
  16463. ' if Cla.cI=23 then ;',
  16464. ' with obj do if ci=24 then;',
  16465. ' with TObject do if ci=25 then;',
  16466. ' with Cla do if ci=26 then;']);
  16467. ConvertProgram;
  16468. CheckSource('TestClass_Const',
  16469. LinesToStr([
  16470. 'rtl.createClass(this, "TObject", null, function () {',
  16471. ' this.cI = 3;',
  16472. ' this.$init = function () {',
  16473. ' };',
  16474. ' this.$final = function () {',
  16475. ' };',
  16476. ' this.DoIt = function () {',
  16477. ' if (this.cI === 4) ;',
  16478. ' if (5 === this.cI) ;',
  16479. ' if (this.cI === 6) ;',
  16480. ' if (7 === this.cI) ;',
  16481. ' if (this.cI === 11) ;',
  16482. ' if (12 === this.cI) ;',
  16483. ' };',
  16484. ' this.DoMore = function () {',
  16485. ' if (this.cI === 8) ;',
  16486. ' if (this.cI === 9) ;',
  16487. ' if (10 === this.cI) ;',
  16488. ' if (11 === this.cI) ;',
  16489. ' if (this.cI === 13) ;',
  16490. ' if (14 === this.cI) ;',
  16491. ' };',
  16492. '});',
  16493. 'this.Obj = null;',
  16494. 'this.Cla = null;',
  16495. '']),
  16496. LinesToStr([
  16497. 'if ($mod.TObject.cI === 21) ;',
  16498. 'if ($mod.Obj.cI === 22) ;',
  16499. 'if ($mod.Cla.cI === 23) ;',
  16500. 'var $with = $mod.Obj;',
  16501. 'if ($with.cI === 24) ;',
  16502. 'var $with1 = $mod.TObject;',
  16503. 'if ($with1.cI === 25) ;',
  16504. 'var $with2 = $mod.Cla;',
  16505. 'if ($with2.cI === 26) ;',
  16506. '']));
  16507. end;
  16508. procedure TTestModule.TestClass_ConstEnum;
  16509. begin
  16510. StartProgram(false);
  16511. Add([
  16512. 'type',
  16513. ' TEnum = (red,blue);',
  16514. ' TObject = class',
  16515. ' end;',
  16516. ' TAnimal = class',
  16517. ' public',
  16518. ' type TSubEnum = (light,dark);',
  16519. ' const a = high(TEnum);',
  16520. ' const b = high(TSubEnum);',
  16521. ' end;',
  16522. ' TBird = class(TAnimal)',
  16523. ' public',
  16524. ' const c = high(TEnum);',
  16525. ' const d = high(TSubEnum);',
  16526. ' end;',
  16527. ' TAnt = class',
  16528. ' public',
  16529. ' const e = high(TEnum);',
  16530. ' const f = high(TBird.TSubEnum);',
  16531. ' end;',
  16532. 'begin',
  16533. '']);
  16534. ConvertProgram;
  16535. CheckSource('TestClass_ConstEnum',
  16536. LinesToStr([
  16537. 'this.TEnum = {',
  16538. ' "0": "red",',
  16539. ' red: 0,',
  16540. ' "1": "blue",',
  16541. ' blue: 1',
  16542. '};',
  16543. 'rtl.createClass(this, "TObject", null, function () {',
  16544. ' this.$init = function () {',
  16545. ' };',
  16546. ' this.$final = function () {',
  16547. ' };',
  16548. '});',
  16549. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16550. ' this.TSubEnum = {',
  16551. ' "0": "light",',
  16552. ' light: 0,',
  16553. ' "1": "dark",',
  16554. ' dark: 1',
  16555. ' };',
  16556. ' this.a = $mod.TEnum.blue;',
  16557. ' this.b = this.TSubEnum.dark;',
  16558. '});',
  16559. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16560. ' this.c = $mod.TEnum.blue;',
  16561. ' this.d = this.TSubEnum.dark;',
  16562. '});',
  16563. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16564. ' this.e = $mod.TEnum.blue;',
  16565. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16566. '});',
  16567. '']),
  16568. LinesToStr([
  16569. '']));
  16570. end;
  16571. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16572. begin
  16573. StartProgram(false);
  16574. Add([
  16575. 'type',
  16576. ' TObject = class',
  16577. ' const cI: longint = 3;',
  16578. ' procedure Fly;',
  16579. ' procedure Run;',
  16580. ' end;',
  16581. ' TBird = class',
  16582. ' procedure Go;',
  16583. ' end;',
  16584. 'procedure tobject.fly;',
  16585. 'const cI: word = 4;',
  16586. 'begin',
  16587. ' if cI=Self.cI then ;',
  16588. 'end;',
  16589. 'procedure tobject.run;',
  16590. 'const cI: word = 5;',
  16591. 'begin',
  16592. ' if cI=Self.cI then ;',
  16593. 'end;',
  16594. 'procedure tbird.go;',
  16595. 'const cI: word = 6;',
  16596. 'begin',
  16597. ' if cI=Self.cI then ;',
  16598. 'end;',
  16599. 'begin',
  16600. '']);
  16601. ConvertProgram;
  16602. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16603. LinesToStr([
  16604. 'rtl.createClass(this, "TObject", null, function () {',
  16605. ' this.cI = 3;',
  16606. ' this.$init = function () {',
  16607. ' };',
  16608. ' this.$final = function () {',
  16609. ' };',
  16610. ' var cI$1 = 4;',
  16611. ' this.Fly = function () {',
  16612. ' if (cI$1 === this.cI) ;',
  16613. ' };',
  16614. ' var cI$2 = 5;',
  16615. ' this.Run = function () {',
  16616. ' if (cI$2 === this.cI) ;',
  16617. ' };',
  16618. '});',
  16619. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16620. ' var cI$3 = 6;',
  16621. ' this.Go = function () {',
  16622. ' if (cI$3 === this.cI) ;',
  16623. ' };',
  16624. '});',
  16625. '']),
  16626. LinesToStr([
  16627. '']));
  16628. end;
  16629. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16630. begin
  16631. StartUnit(false);
  16632. Add([
  16633. 'interface',
  16634. 'type',
  16635. ' TObject = class',
  16636. ' const cI: longint = 3;',
  16637. ' procedure Fly;',
  16638. ' procedure Run;',
  16639. ' end;',
  16640. ' TBird = class',
  16641. ' procedure Go;',
  16642. ' end;',
  16643. 'implementation',
  16644. 'procedure tobject.fly;',
  16645. 'const cI: word = 4;',
  16646. 'begin',
  16647. ' if cI=Self.cI then ;',
  16648. 'end;',
  16649. 'procedure tobject.run;',
  16650. 'const cI: word = 5;',
  16651. 'begin',
  16652. ' if cI=Self.cI then ;',
  16653. 'end;',
  16654. 'procedure tbird.go;',
  16655. 'const cI: word = 6;',
  16656. 'begin',
  16657. ' if cI=Self.cI then ;',
  16658. 'end;',
  16659. '']);
  16660. ConvertUnit;
  16661. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16662. LinesToStr([
  16663. 'rtl.createClass(this, "TObject", null, function () {',
  16664. ' this.cI = 3;',
  16665. ' this.$init = function () {',
  16666. ' };',
  16667. ' this.$final = function () {',
  16668. ' };',
  16669. ' var cI$1 = 4;',
  16670. ' this.Fly = function () {',
  16671. ' if (cI$1 === this.cI) ;',
  16672. ' };',
  16673. ' var cI$2 = 5;',
  16674. ' this.Run = function () {',
  16675. ' if (cI$2 === this.cI) ;',
  16676. ' };',
  16677. '});',
  16678. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16679. ' var cI$3 = 6;',
  16680. ' this.Go = function () {',
  16681. ' if (cI$3 === this.cI) ;',
  16682. ' };',
  16683. '});',
  16684. '']),
  16685. '',
  16686. '');
  16687. end;
  16688. procedure TTestModule.TestClass_LocalVarSelfFail;
  16689. begin
  16690. StartProgram(false);
  16691. Add([
  16692. 'type',
  16693. ' TObject = class',
  16694. ' constructor Create;',
  16695. ' end;',
  16696. 'constructor tobject.create;',
  16697. 'var self: longint;',
  16698. 'begin',
  16699. 'end',
  16700. 'begin',
  16701. '']);
  16702. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16703. ConvertProgram;
  16704. end;
  16705. procedure TTestModule.TestClass_ArgSelfFail;
  16706. begin
  16707. StartProgram(false);
  16708. Add([
  16709. 'type',
  16710. ' TObject = class',
  16711. ' procedure DoIt(Self: longint);',
  16712. ' end;',
  16713. 'procedure tobject.doit(self: longint);',
  16714. 'begin',
  16715. 'end',
  16716. 'begin',
  16717. '']);
  16718. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16719. ConvertProgram;
  16720. end;
  16721. procedure TTestModule.TestClass_NestedProcSelf;
  16722. begin
  16723. StartProgram(false);
  16724. Add([
  16725. 'type',
  16726. ' TObject = class',
  16727. ' Key: longint;',
  16728. ' class var State: longint;',
  16729. ' procedure DoIt;',
  16730. ' function GetSize: longint; virtual; abstract;',
  16731. ' procedure SetSize(Value: longint); virtual; abstract;',
  16732. ' property Size: longint read GetSize write SetSize;',
  16733. ' end;',
  16734. 'procedure tobject.doit;',
  16735. ' procedure Sub;',
  16736. ' begin',
  16737. ' key:=key+2;',
  16738. ' self.key:=self.key+3;',
  16739. ' state:=state+4;',
  16740. ' self.state:=self.state+5;',
  16741. ' tobject.state:=tobject.state+6;',
  16742. ' size:=size+7;',
  16743. ' self.size:=self.size+8;',
  16744. ' end;',
  16745. 'begin',
  16746. ' sub;',
  16747. ' key:=key+12;',
  16748. ' self.key:=self.key+13;',
  16749. ' state:=state+14;',
  16750. ' self.state:=self.state+15;',
  16751. ' tobject.state:=tobject.state+16;',
  16752. ' size:=size+17;',
  16753. ' self.size:=self.size+18;',
  16754. 'end;',
  16755. 'begin',
  16756. '']);
  16757. ConvertProgram;
  16758. CheckSource('TestClass_NestedProcSelf',
  16759. LinesToStr([ // statements
  16760. 'rtl.createClass(this, "TObject", null, function () {',
  16761. ' this.State = 0;',
  16762. ' this.$init = function () {',
  16763. ' this.Key = 0;',
  16764. ' };',
  16765. ' this.$final = function () {',
  16766. ' };',
  16767. ' this.DoIt = function () {',
  16768. ' var $Self = this;',
  16769. ' function Sub() {',
  16770. ' $Self.Key = $Self.Key + 2;',
  16771. ' $Self.Key = $Self.Key + 3;',
  16772. ' $mod.TObject.State = $Self.State + 4;',
  16773. ' $mod.TObject.State = $Self.State + 5;',
  16774. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16775. ' $Self.SetSize($Self.GetSize() + 7);',
  16776. ' $Self.SetSize($Self.GetSize() + 8);',
  16777. ' };',
  16778. ' Sub();',
  16779. ' this.Key = this.Key + 12;',
  16780. ' $Self.Key = $Self.Key + 13;',
  16781. ' $mod.TObject.State = this.State + 14;',
  16782. ' $mod.TObject.State = $Self.State + 15;',
  16783. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16784. ' this.SetSize(this.GetSize() + 17);',
  16785. ' $Self.SetSize($Self.GetSize() + 18);',
  16786. ' };',
  16787. '});',
  16788. '']),
  16789. LinesToStr([ // $mod.$main
  16790. '']));
  16791. end;
  16792. procedure TTestModule.TestClass_NestedProcSelf2;
  16793. begin
  16794. StartProgram(false);
  16795. Add([
  16796. 'type',
  16797. ' TObject = class',
  16798. ' Key: longint;',
  16799. ' class var State: longint;',
  16800. ' function GetSize: longint; virtual; abstract;',
  16801. ' procedure SetSize(Value: longint); virtual; abstract;',
  16802. ' property Size: longint read GetSize write SetSize;',
  16803. ' end;',
  16804. ' TBird = class',
  16805. ' procedure DoIt;',
  16806. ' end;',
  16807. 'procedure tbird.doit;',
  16808. ' procedure Sub;',
  16809. ' begin',
  16810. ' key:=key+2;',
  16811. ' self.key:=self.key+3;',
  16812. ' state:=state+4;',
  16813. ' self.state:=self.state+5;',
  16814. ' tobject.state:=tobject.state+6;',
  16815. ' size:=size+7;',
  16816. ' self.size:=self.size+8;',
  16817. ' end;',
  16818. 'begin',
  16819. ' sub;',
  16820. ' key:=key+12;',
  16821. ' self.key:=self.key+13;',
  16822. ' state:=state+14;',
  16823. ' self.state:=self.state+15;',
  16824. ' tobject.state:=tobject.state+16;',
  16825. ' size:=size+17;',
  16826. ' self.size:=self.size+18;',
  16827. 'end;',
  16828. 'begin',
  16829. '']);
  16830. ConvertProgram;
  16831. CheckSource('TestClass_NestedProcSelf2',
  16832. LinesToStr([ // statements
  16833. 'rtl.createClass(this, "TObject", null, function () {',
  16834. ' this.State = 0;',
  16835. ' this.$init = function () {',
  16836. ' this.Key = 0;',
  16837. ' };',
  16838. ' this.$final = function () {',
  16839. ' };',
  16840. '});',
  16841. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16842. ' this.DoIt = function () {',
  16843. ' var $Self = this;',
  16844. ' function Sub() {',
  16845. ' $Self.Key = $Self.Key + 2;',
  16846. ' $Self.Key = $Self.Key + 3;',
  16847. ' $mod.TObject.State = $Self.State + 4;',
  16848. ' $mod.TObject.State = $Self.State + 5;',
  16849. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16850. ' $Self.SetSize($Self.GetSize() + 7);',
  16851. ' $Self.SetSize($Self.GetSize() + 8);',
  16852. ' };',
  16853. ' Sub();',
  16854. ' this.Key = this.Key + 12;',
  16855. ' $Self.Key = $Self.Key + 13;',
  16856. ' $mod.TObject.State = this.State + 14;',
  16857. ' $mod.TObject.State = $Self.State + 15;',
  16858. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16859. ' this.SetSize(this.GetSize() + 17);',
  16860. ' $Self.SetSize($Self.GetSize() + 18);',
  16861. ' };',
  16862. '});',
  16863. '']),
  16864. LinesToStr([ // $mod.$main
  16865. '']));
  16866. end;
  16867. procedure TTestModule.TestClass_NestedProcClassSelf;
  16868. begin
  16869. StartProgram(false);
  16870. Add([
  16871. 'type',
  16872. ' TObject = class',
  16873. ' class var State: longint;',
  16874. ' class procedure DoIt;',
  16875. ' class function GetSize: longint; virtual; abstract;',
  16876. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16877. ' class property Size: longint read GetSize write SetSize;',
  16878. ' end;',
  16879. 'class procedure tobject.doit;',
  16880. ' procedure Sub;',
  16881. ' begin',
  16882. ' state:=state+2;',
  16883. ' self.state:=self.state+3;',
  16884. ' tobject.state:=tobject.state+4;',
  16885. ' size:=size+5;',
  16886. ' self.size:=self.size+6;',
  16887. ' tobject.size:=tobject.size+7;',
  16888. ' end;',
  16889. 'begin',
  16890. ' sub;',
  16891. ' state:=state+12;',
  16892. ' self.state:=self.state+13;',
  16893. ' tobject.state:=tobject.state+14;',
  16894. ' size:=size+15;',
  16895. ' self.size:=self.size+16;',
  16896. ' tobject.size:=tobject.size+17;',
  16897. 'end;',
  16898. 'begin',
  16899. '']);
  16900. ConvertProgram;
  16901. CheckSource('TestClass_NestedProcClassSelf',
  16902. LinesToStr([ // statements
  16903. 'rtl.createClass(this, "TObject", null, function () {',
  16904. ' this.State = 0;',
  16905. ' this.$init = function () {',
  16906. ' };',
  16907. ' this.$final = function () {',
  16908. ' };',
  16909. ' this.DoIt = function () {',
  16910. ' var $Self = this;',
  16911. ' function Sub() {',
  16912. ' $mod.TObject.State = $Self.State + 2;',
  16913. ' $mod.TObject.State = $Self.State + 3;',
  16914. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16915. ' $Self.SetSize($Self.GetSize() + 5);',
  16916. ' $Self.SetSize($Self.GetSize() + 6);',
  16917. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16918. ' };',
  16919. ' Sub();',
  16920. ' $mod.TObject.State = this.State + 12;',
  16921. ' $mod.TObject.State = $Self.State + 13;',
  16922. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16923. ' this.SetSize(this.GetSize() + 15);',
  16924. ' $Self.SetSize($Self.GetSize() + 16);',
  16925. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16926. ' };',
  16927. '});',
  16928. '']),
  16929. LinesToStr([ // $mod.$main
  16930. '']));
  16931. end;
  16932. procedure TTestModule.TestClass_NestedProcCallInherited;
  16933. begin
  16934. StartProgram(false);
  16935. Add([
  16936. 'type',
  16937. ' TObject = class',
  16938. ' function DoIt(k: boolean): longint; virtual;',
  16939. ' end;',
  16940. ' TBird = class',
  16941. ' function DoIt(k: boolean): longint; override;',
  16942. ' end;',
  16943. 'function tobject.doit(k: boolean): longint;',
  16944. 'begin',
  16945. 'end;',
  16946. 'function tbird.doit(k: boolean): longint;',
  16947. ' procedure Sub;',
  16948. ' begin',
  16949. ' inherited DoIt(true);',
  16950. //' if inherited DoIt(false)=4 then ;',
  16951. ' end;',
  16952. 'begin',
  16953. ' Sub;',
  16954. ' inherited;',
  16955. ' inherited DoIt(true);',
  16956. //' if inherited DoIt(false)=14 then ;',
  16957. 'end;',
  16958. 'begin',
  16959. '']);
  16960. ConvertProgram;
  16961. CheckSource('TestClass_NestedProcCallInherited',
  16962. LinesToStr([ // statements
  16963. 'rtl.createClass(this, "TObject", null, function () {',
  16964. ' this.$init = function () {',
  16965. ' };',
  16966. ' this.$final = function () {',
  16967. ' };',
  16968. ' this.DoIt = function (k) {',
  16969. ' var Result = 0;',
  16970. ' return Result;',
  16971. ' };',
  16972. '});',
  16973. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16974. ' this.DoIt = function (k) {',
  16975. ' var $Self = this;',
  16976. ' var Result = 0;',
  16977. ' function Sub() {',
  16978. ' $mod.TObject.DoIt.call($Self, true);',
  16979. ' };',
  16980. ' Sub();',
  16981. ' $mod.TObject.DoIt.apply(this, arguments);',
  16982. ' $mod.TObject.DoIt.call(this, true);',
  16983. ' return Result;',
  16984. ' };',
  16985. '});',
  16986. '']),
  16987. LinesToStr([ // $mod.$main
  16988. '']));
  16989. end;
  16990. procedure TTestModule.TestClass_TObjectFree;
  16991. begin
  16992. StartProgram(false);
  16993. Add([
  16994. 'type',
  16995. ' TObject = class',
  16996. ' Obj: tobject;',
  16997. ' procedure Free;',
  16998. ' procedure Release;',
  16999. ' end;',
  17000. 'procedure tobject.free;',
  17001. 'begin',
  17002. 'end;',
  17003. 'procedure tobject.release;',
  17004. 'begin',
  17005. ' free;',
  17006. ' if true then free;',
  17007. 'end;',
  17008. 'function DoIt(o: tobject): tobject;',
  17009. 'var l: tobject;',
  17010. 'begin',
  17011. ' o.free;',
  17012. ' o.free();',
  17013. ' l.free;',
  17014. ' l.free();',
  17015. ' o.obj.free;',
  17016. ' o.obj.free();',
  17017. ' with o do obj.free;',
  17018. ' with o do obj.free();',
  17019. ' result.Free;',
  17020. ' result.Free();',
  17021. 'end;',
  17022. 'var o: tobject;',
  17023. ' a: array of tobject;',
  17024. 'begin',
  17025. ' o.free;',
  17026. ' o.obj.free;',
  17027. ' a[1+2].free;',
  17028. '']);
  17029. ConvertProgram;
  17030. CheckSource('TestClass_TObjectFree',
  17031. LinesToStr([ // statements
  17032. 'rtl.createClass(this, "TObject", null, function () {',
  17033. ' this.$init = function () {',
  17034. ' this.Obj = null;',
  17035. ' };',
  17036. ' this.$final = function () {',
  17037. ' this.Obj = undefined;',
  17038. ' };',
  17039. ' this.Free = function () {',
  17040. ' };',
  17041. ' this.Release = function () {',
  17042. ' this.Free();',
  17043. ' if (true) this.Free();',
  17044. ' };',
  17045. '});',
  17046. 'this.DoIt = function (o) {',
  17047. ' var Result = null;',
  17048. ' var l = null;',
  17049. ' o = rtl.freeLoc(o);',
  17050. ' o = rtl.freeLoc(o);',
  17051. ' l = rtl.freeLoc(l);',
  17052. ' l = rtl.freeLoc(l);',
  17053. ' rtl.free(o, "Obj");',
  17054. ' rtl.free(o, "Obj");',
  17055. ' rtl.free(o, "Obj");',
  17056. ' rtl.free(o, "Obj");',
  17057. ' Result = rtl.freeLoc(Result);',
  17058. ' Result = rtl.freeLoc(Result);',
  17059. ' return Result;',
  17060. '};',
  17061. 'this.o = null;',
  17062. 'this.a = [];',
  17063. '']),
  17064. LinesToStr([ // $mod.$main
  17065. 'rtl.free($mod, "o");',
  17066. 'rtl.free($mod.o, "Obj");',
  17067. 'rtl.free($mod.a, 1 + 2);',
  17068. '']));
  17069. end;
  17070. procedure TTestModule.TestClass_TObjectFree_VarArg;
  17071. begin
  17072. StartProgram(false);
  17073. Add([
  17074. 'type',
  17075. ' TObject = class',
  17076. ' Obj: tobject;',
  17077. ' procedure Free;',
  17078. ' end;',
  17079. 'procedure tobject.free;',
  17080. 'begin',
  17081. 'end;',
  17082. 'procedure DoIt(var o: tobject);',
  17083. 'begin',
  17084. ' o.free;',
  17085. ' o.free();',
  17086. 'end;',
  17087. 'begin',
  17088. '']);
  17089. ConvertProgram;
  17090. CheckSource('TestClass_TObjectFree_VarArg',
  17091. LinesToStr([ // statements
  17092. 'rtl.createClass(this, "TObject", null, function () {',
  17093. ' this.$init = function () {',
  17094. ' this.Obj = null;',
  17095. ' };',
  17096. ' this.$final = function () {',
  17097. ' this.Obj = undefined;',
  17098. ' };',
  17099. ' this.Free = function () {',
  17100. ' };',
  17101. '});',
  17102. 'this.DoIt = function (o) {',
  17103. ' o.set(rtl.freeLoc(o.get()));',
  17104. ' o.set(rtl.freeLoc(o.get()));',
  17105. '};',
  17106. '']),
  17107. LinesToStr([ // $mod.$main
  17108. '']));
  17109. end;
  17110. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17111. begin
  17112. StartProgram(false);
  17113. Add([
  17114. 'type',
  17115. ' TObject = class',
  17116. ' constructor Create;',
  17117. ' procedure Free;',
  17118. ' end;',
  17119. 'constructor TObject.Create; begin end;',
  17120. 'procedure tobject.free; begin end;',
  17121. 'begin',
  17122. ' with tobject.create do free;',
  17123. '']);
  17124. ConvertProgram;
  17125. CheckSource('TestClass_TObjectFreeNewInstance',
  17126. LinesToStr([ // statements
  17127. 'rtl.createClass(this, "TObject", null, function () {',
  17128. ' this.$init = function () {',
  17129. ' };',
  17130. ' this.$final = function () {',
  17131. ' };',
  17132. ' this.Create = function () {',
  17133. ' return this;',
  17134. ' };',
  17135. ' this.Free = function () {',
  17136. ' };',
  17137. '});',
  17138. '']),
  17139. LinesToStr([ // $mod.$main
  17140. 'var $with = $mod.TObject.$create("Create");',
  17141. '$with=rtl.freeLoc($with);',
  17142. '']));
  17143. end;
  17144. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17145. begin
  17146. StartProgram(false);
  17147. Add([
  17148. 'type',
  17149. ' TObject = class',
  17150. ' destructor Destroy;',
  17151. ' procedure Free;',
  17152. ' end;',
  17153. 'destructor TObject.Destroy; begin end;',
  17154. 'procedure tobject.free; begin end;',
  17155. 'var o: tobject;',
  17156. 'begin',
  17157. ' o.free;',
  17158. '']);
  17159. Converter.UseLowerCase:=true;
  17160. ConvertProgram;
  17161. CheckSource('TestClass_TObjectFreeLowerCase',
  17162. LinesToStr([ // statements
  17163. 'rtl.createClass(this, "tobject", null, function () {',
  17164. ' this.$init = function () {',
  17165. ' };',
  17166. ' this.$final = function () {',
  17167. ' };',
  17168. ' rtl.tObjectDestroy = "destroy";',
  17169. ' this.destroy = function () {',
  17170. ' };',
  17171. ' this.free = function () {',
  17172. ' };',
  17173. '});',
  17174. 'this.o = null;',
  17175. '']),
  17176. LinesToStr([ // $mod.$main
  17177. 'rtl.free($mod, "o");',
  17178. '']));
  17179. end;
  17180. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17181. begin
  17182. StartProgram(false);
  17183. Add([
  17184. 'type',
  17185. ' TObject = class',
  17186. ' procedure Free;',
  17187. ' function GetObj: tobject; virtual; abstract;',
  17188. ' end;',
  17189. 'procedure tobject.free;',
  17190. 'begin',
  17191. 'end;',
  17192. 'var o: tobject;',
  17193. 'begin',
  17194. ' o.getobj.free;',
  17195. '']);
  17196. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17197. ConvertProgram;
  17198. end;
  17199. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17200. begin
  17201. StartProgram(false);
  17202. Add([
  17203. 'type',
  17204. ' TObject = class',
  17205. ' procedure Free;',
  17206. ' FObj: TObject;',
  17207. ' property Obj: tobject read FObj write FObj;',
  17208. ' end;',
  17209. 'procedure tobject.free;',
  17210. 'begin',
  17211. 'end;',
  17212. 'var o: tobject;',
  17213. 'begin',
  17214. ' o.obj.free;',
  17215. '']);
  17216. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17217. ConvertProgram;
  17218. end;
  17219. procedure TTestModule.TestClass_ForIn;
  17220. begin
  17221. StartProgram(false);
  17222. Add([
  17223. 'type',
  17224. ' TObject = class end;',
  17225. ' TItem = TObject;',
  17226. ' TEnumerator = class',
  17227. ' FCurrent: TItem;',
  17228. ' property Current: TItem read FCurrent;',
  17229. ' function MoveNext: boolean;',
  17230. ' end;',
  17231. ' TBird = class',
  17232. ' function GetEnumerator: TEnumerator;',
  17233. ' end;',
  17234. 'function TEnumerator.MoveNext: boolean;',
  17235. 'begin',
  17236. 'end;',
  17237. 'function TBird.GetEnumerator: TEnumerator;',
  17238. 'begin',
  17239. 'end;',
  17240. 'var',
  17241. ' b: TBird;',
  17242. ' i, i2: TItem;',
  17243. 'begin',
  17244. ' for i in b do i2:=i;']);
  17245. ConvertProgram;
  17246. CheckSource('TestClass_ForIn',
  17247. LinesToStr([ // statements
  17248. 'rtl.createClass(this, "TObject", null, function () {',
  17249. ' this.$init = function () {',
  17250. ' };',
  17251. ' this.$final = function () {',
  17252. ' };',
  17253. '});',
  17254. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17255. ' this.$init = function () {',
  17256. ' $mod.TObject.$init.call(this);',
  17257. ' this.FCurrent = null;',
  17258. ' };',
  17259. ' this.$final = function () {',
  17260. ' this.FCurrent = undefined;',
  17261. ' $mod.TObject.$final.call(this);',
  17262. ' };',
  17263. ' this.MoveNext = function () {',
  17264. ' var Result = false;',
  17265. ' return Result;',
  17266. ' };',
  17267. '});',
  17268. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17269. ' this.GetEnumerator = function () {',
  17270. ' var Result = null;',
  17271. ' return Result;',
  17272. ' };',
  17273. '});',
  17274. 'this.b = null;',
  17275. 'this.i = null;',
  17276. 'this.i2 = null;'
  17277. ]),
  17278. LinesToStr([ // $mod.$main
  17279. 'var $in = $mod.b.GetEnumerator();',
  17280. 'try {',
  17281. ' while ($in.MoveNext()){',
  17282. ' $mod.i = $in.FCurrent;',
  17283. ' $mod.i2 = $mod.i;',
  17284. ' }',
  17285. '} finally {',
  17286. ' $in = rtl.freeLoc($in)',
  17287. '};',
  17288. '']));
  17289. end;
  17290. procedure TTestModule.TestClass_DispatchMessage;
  17291. begin
  17292. StartProgram(false);
  17293. Add([
  17294. 'type',
  17295. ' {$DispatchField DispInt}',
  17296. ' {$DispatchStrField DispStr}',
  17297. ' TObject = class',
  17298. ' procedure Dispatch(var Msg); virtual; abstract;',
  17299. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17300. ' end;',
  17301. ' THopMsg = record',
  17302. ' DispInt: longint;',
  17303. ' end;',
  17304. ' TPutMsg = record',
  17305. ' DispStr: string;',
  17306. ' end;',
  17307. ' TBird = class',
  17308. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17309. ' procedure Run; overload; virtual; abstract;',
  17310. ' procedure Run(var Msg); overload; message ''Fast'';',
  17311. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17312. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17313. ' end;',
  17314. 'procedure TBird.Run(var Msg);',
  17315. 'begin',
  17316. 'end;',
  17317. 'begin',
  17318. '']);
  17319. ConvertProgram;
  17320. CheckResolverUnexpectedHints(true);
  17321. CheckSource('TestClass_Message',
  17322. LinesToStr([ // statements
  17323. 'rtl.createClass(this, "TObject", null, function () {',
  17324. ' this.$init = function () {',
  17325. ' };',
  17326. ' this.$final = function () {',
  17327. ' };',
  17328. '});',
  17329. 'rtl.recNewT(this, "THopMsg", function () {',
  17330. ' this.DispInt = 0;',
  17331. ' this.$eq = function (b) {',
  17332. ' return this.DispInt === b.DispInt;',
  17333. ' };',
  17334. ' this.$assign = function (s) {',
  17335. ' this.DispInt = s.DispInt;',
  17336. ' return this;',
  17337. ' };',
  17338. '});',
  17339. 'rtl.recNewT(this, "TPutMsg", function () {',
  17340. ' this.DispStr = "";',
  17341. ' this.$eq = function (b) {',
  17342. ' return this.DispStr === b.DispStr;',
  17343. ' };',
  17344. ' this.$assign = function (s) {',
  17345. ' this.DispStr = s.DispStr;',
  17346. ' return this;',
  17347. ' };',
  17348. '});',
  17349. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17350. ' this.Run$1 = function (Msg) {',
  17351. ' };',
  17352. ' this.$msgint = {',
  17353. ' "2": "Fly",',
  17354. ' "3": "Hop"',
  17355. ' };',
  17356. ' this.$msgstr = {',
  17357. ' Fast: "Run$1",',
  17358. ' foo: "Put"',
  17359. ' };',
  17360. '});',
  17361. '']),
  17362. LinesToStr([ // $mod.$main
  17363. '']));
  17364. end;
  17365. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17366. begin
  17367. StartProgram(false);
  17368. Add([
  17369. 'type',
  17370. ' TObject = class',
  17371. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17372. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17373. ' end;',
  17374. 'begin',
  17375. '']);
  17376. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17377. ConvertProgram;
  17378. end;
  17379. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17380. begin
  17381. StartProgram(false);
  17382. Add([
  17383. 'type',
  17384. ' TObject = class',
  17385. ' procedure Dispatch(var Msg); virtual; abstract;',
  17386. ' end;',
  17387. ' TFlyMsg = record',
  17388. ' FlyId: longint;',
  17389. ' end;',
  17390. ' TBird = class',
  17391. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17392. ' end;',
  17393. 'begin',
  17394. '']);
  17395. ConvertProgram;
  17396. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17397. end;
  17398. procedure TTestModule.TestClassOf_Create;
  17399. begin
  17400. StartProgram(false);
  17401. Add('type');
  17402. Add(' TObject = class');
  17403. Add(' constructor Create;');
  17404. Add(' end;');
  17405. Add(' TClass = class of TObject;');
  17406. Add('constructor tobject.create; begin end;');
  17407. Add('var');
  17408. Add(' Obj: tobject;');
  17409. Add(' C: tclass;');
  17410. Add('begin');
  17411. Add(' obj:=C.create;');
  17412. Add(' with c do obj:=create;');
  17413. ConvertProgram;
  17414. CheckSource('TestClassOf_Create',
  17415. LinesToStr([ // statements
  17416. 'rtl.createClass(this, "TObject", null, function () {',
  17417. ' this.$init = function () {',
  17418. ' };',
  17419. ' this.$final = function () {',
  17420. ' };',
  17421. ' this.Create = function () {',
  17422. ' return this;',
  17423. ' };',
  17424. '});',
  17425. 'this.Obj = null;',
  17426. 'this.C = null;'
  17427. ]),
  17428. LinesToStr([ // $mod.$main
  17429. '$mod.Obj = $mod.C.$create("Create");',
  17430. 'var $with = $mod.C;',
  17431. '$mod.Obj = $with.$create("Create");',
  17432. '']));
  17433. end;
  17434. procedure TTestModule.TestClassOf_Call;
  17435. begin
  17436. StartProgram(false);
  17437. Add('type');
  17438. Add(' TObject = class');
  17439. Add(' class procedure DoIt;');
  17440. Add(' end;');
  17441. Add(' TClass = class of TObject;');
  17442. Add('class procedure tobject.doit; begin end;');
  17443. Add('var');
  17444. Add(' C: tclass;');
  17445. Add('begin');
  17446. Add(' c.doit;');
  17447. Add(' with c do doit;');
  17448. ConvertProgram;
  17449. CheckSource('TestClassOf_Call',
  17450. LinesToStr([ // statements
  17451. 'rtl.createClass(this, "TObject", null, function () {',
  17452. ' this.$init = function () {',
  17453. ' };',
  17454. ' this.$final = function () {',
  17455. ' };',
  17456. ' this.DoIt = function () {',
  17457. ' };',
  17458. '});',
  17459. 'this.C = null;'
  17460. ]),
  17461. LinesToStr([ // $mod.$main
  17462. '$mod.C.DoIt();',
  17463. 'var $with = $mod.C;',
  17464. '$with.DoIt();',
  17465. '']));
  17466. end;
  17467. procedure TTestModule.TestClassOf_Assign;
  17468. begin
  17469. StartProgram(false);
  17470. Add('type');
  17471. Add(' TClass = class of TObject;');
  17472. Add(' TObject = class');
  17473. Add(' ClassType: TClass; ');
  17474. Add(' end;');
  17475. Add('var');
  17476. Add(' Obj: tobject;');
  17477. Add(' C: tclass;');
  17478. Add('begin');
  17479. Add(' c:=nil;');
  17480. Add(' c:=obj.classtype;');
  17481. ConvertProgram;
  17482. CheckSource('TestClassOf_Assign',
  17483. LinesToStr([ // statements
  17484. 'rtl.createClass(this, "TObject", null, function () {',
  17485. ' this.$init = function () {',
  17486. ' this.ClassType = null;',
  17487. ' };',
  17488. ' this.$final = function () {',
  17489. ' this.ClassType = undefined;',
  17490. ' };',
  17491. '});',
  17492. 'this.Obj = null;',
  17493. 'this.C = null;'
  17494. ]),
  17495. LinesToStr([ // $mod.$main
  17496. '$mod.C = null;',
  17497. '$mod.C = $mod.Obj.ClassType;',
  17498. '']));
  17499. end;
  17500. procedure TTestModule.TestClassOf_Is;
  17501. begin
  17502. StartProgram(false);
  17503. Add('type');
  17504. Add(' TClass = class of TObject;');
  17505. Add(' TObject = class');
  17506. Add(' end;');
  17507. Add(' TCar = class');
  17508. Add(' end;');
  17509. Add(' TCars = class of TCar;');
  17510. Add('var');
  17511. Add(' Obj: tobject;');
  17512. Add(' C: tclass;');
  17513. Add(' Cars: tcars;');
  17514. Add('begin');
  17515. Add(' if c is tcar then ;');
  17516. Add(' if c is tcars then ;');
  17517. ConvertProgram;
  17518. CheckSource('TestClassOf_Is',
  17519. LinesToStr([ // statements
  17520. 'rtl.createClass(this, "TObject", null, function () {',
  17521. ' this.$init = function () {',
  17522. ' };',
  17523. ' this.$final = function () {',
  17524. ' };',
  17525. '});',
  17526. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17527. '});',
  17528. 'this.Obj = null;',
  17529. 'this.C = null;',
  17530. 'this.Cars = null;'
  17531. ]),
  17532. LinesToStr([ // $mod.$main
  17533. 'if(rtl.is($mod.C,$mod.TCar));',
  17534. 'if(rtl.is($mod.C,$mod.TCar));',
  17535. '']));
  17536. end;
  17537. procedure TTestModule.TestClassOf_Compare;
  17538. begin
  17539. StartProgram(false);
  17540. Add('type');
  17541. Add(' TClass = class of TObject;');
  17542. Add(' TObject = class');
  17543. Add(' ClassType: TClass; ');
  17544. Add(' end;');
  17545. Add('var');
  17546. Add(' b: boolean;');
  17547. Add(' Obj: tobject;');
  17548. Add(' C: tclass;');
  17549. Add('begin');
  17550. Add(' b:=c=nil;');
  17551. Add(' b:=nil=c;');
  17552. Add(' b:=c=obj.classtype;');
  17553. Add(' b:=obj.classtype=c;');
  17554. Add(' b:=c=TObject;');
  17555. Add(' b:=TObject=c;');
  17556. Add(' b:=c<>nil;');
  17557. Add(' b:=nil<>c;');
  17558. Add(' b:=c<>obj.classtype;');
  17559. Add(' b:=obj.classtype<>c;');
  17560. Add(' b:=c<>TObject;');
  17561. Add(' b:=TObject<>c;');
  17562. ConvertProgram;
  17563. CheckSource('TestClassOf_Compare',
  17564. LinesToStr([ // statements
  17565. 'rtl.createClass(this, "TObject", null, function () {',
  17566. ' this.$init = function () {',
  17567. ' this.ClassType = null;',
  17568. ' };',
  17569. ' this.$final = function () {',
  17570. ' this.ClassType = undefined;',
  17571. ' };',
  17572. '});',
  17573. 'this.b = false;',
  17574. 'this.Obj = null;',
  17575. 'this.C = null;'
  17576. ]),
  17577. LinesToStr([ // $mod.$main
  17578. '$mod.b = $mod.C === null;',
  17579. '$mod.b = null === $mod.C;',
  17580. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17581. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17582. '$mod.b = $mod.C === $mod.TObject;',
  17583. '$mod.b = $mod.TObject === $mod.C;',
  17584. '$mod.b = $mod.C !== null;',
  17585. '$mod.b = null !== $mod.C;',
  17586. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17587. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17588. '$mod.b = $mod.C !== $mod.TObject;',
  17589. '$mod.b = $mod.TObject !== $mod.C;',
  17590. '']));
  17591. end;
  17592. procedure TTestModule.TestClassOf_ClassVar;
  17593. begin
  17594. StartProgram(false);
  17595. Add('type');
  17596. Add(' TObject = class');
  17597. Add(' class var id: longint;');
  17598. Add(' end;');
  17599. Add(' TClass = class of TObject;');
  17600. Add('var');
  17601. Add(' C: tclass;');
  17602. Add('begin');
  17603. Add(' C.id:=C.id;');
  17604. ConvertProgram;
  17605. CheckSource('TestClassOf_ClassVar',
  17606. LinesToStr([ // statements
  17607. 'rtl.createClass(this, "TObject", null, function () {',
  17608. ' this.id = 0;',
  17609. ' this.$init = function () {',
  17610. ' };',
  17611. ' this.$final = function () {',
  17612. ' };',
  17613. '});',
  17614. 'this.C = null;'
  17615. ]),
  17616. LinesToStr([ // $mod.$main
  17617. '$mod.TObject.id = $mod.C.id;',
  17618. '']));
  17619. end;
  17620. procedure TTestModule.TestClassOf_ClassMethod;
  17621. begin
  17622. StartProgram(false);
  17623. Add('type');
  17624. Add(' TObject = class');
  17625. Add(' class function DoIt(i: longint = 0): longint;');
  17626. Add(' end;');
  17627. Add(' TClass = class of TObject;');
  17628. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17629. Add('var');
  17630. Add(' i: longint;');
  17631. Add(' C: tclass;');
  17632. Add('begin');
  17633. Add(' C.DoIt;');
  17634. Add(' C.DoIt();');
  17635. Add(' i:=C.DoIt;');
  17636. Add(' i:=C.DoIt();');
  17637. ConvertProgram;
  17638. CheckSource('TestClassOf_ClassMethod',
  17639. LinesToStr([ // statements
  17640. 'rtl.createClass(this, "TObject", null, function () {',
  17641. ' this.$init = function () {',
  17642. ' };',
  17643. ' this.$final = function () {',
  17644. ' };',
  17645. ' this.DoIt = function (i) {',
  17646. ' var Result = 0;',
  17647. ' return Result;',
  17648. ' };',
  17649. '});',
  17650. 'this.i = 0;',
  17651. 'this.C = null;'
  17652. ]),
  17653. LinesToStr([ // $mod.$main
  17654. '$mod.C.DoIt(0);',
  17655. '$mod.C.DoIt(0);',
  17656. '$mod.i = $mod.C.DoIt(0);',
  17657. '$mod.i = $mod.C.DoIt(0);',
  17658. '']));
  17659. end;
  17660. procedure TTestModule.TestClassOf_ClassProperty;
  17661. begin
  17662. StartProgram(false);
  17663. Add([
  17664. 'type',
  17665. ' TObject = class',
  17666. ' class var FA: longint;',
  17667. ' class function GetA: longint;',
  17668. ' class procedure SetA(Value: longint);',
  17669. ' class property pA: longint read fa write fa;',
  17670. ' class property pB: longint read geta write seta;',
  17671. ' end;',
  17672. ' TObjectClass = class of tobject;',
  17673. 'class function tobject.geta: longint; begin end;',
  17674. 'class procedure tobject.seta(value: longint); begin end;',
  17675. 'var',
  17676. ' b: boolean;',
  17677. ' Obj: tobject;',
  17678. ' Cla: tobjectclass;',
  17679. 'begin',
  17680. ' obj.pa:=obj.pa;',
  17681. ' obj.pb:=obj.pb;',
  17682. ' b:=obj.pa=4;',
  17683. ' b:=obj.pb=obj.pb;',
  17684. ' b:=5=obj.pa;',
  17685. ' cla.pa:=6;',
  17686. ' cla.pa:=cla.pa;',
  17687. ' cla.pb:=cla.pb;',
  17688. ' b:=cla.pa=7;',
  17689. ' b:=cla.pb=cla.pb;',
  17690. ' b:=8=cla.pa;',
  17691. ' tobject.pa:=9;',
  17692. ' tobject.pb:=tobject.pb;',
  17693. ' b:=tobject.pa=10;',
  17694. ' b:=11=tobject.pa;',
  17695. '']);
  17696. ConvertProgram;
  17697. CheckSource('TestClassOf_ClassProperty',
  17698. LinesToStr([ // statements
  17699. 'rtl.createClass(this, "TObject", null, function () {',
  17700. ' this.FA = 0;',
  17701. ' this.$init = function () {',
  17702. ' };',
  17703. ' this.$final = function () {',
  17704. ' };',
  17705. ' this.GetA = function () {',
  17706. ' var Result = 0;',
  17707. ' return Result;',
  17708. ' };',
  17709. ' this.SetA = function (Value) {',
  17710. ' };',
  17711. '});',
  17712. 'this.b = false;',
  17713. 'this.Obj = null;',
  17714. 'this.Cla = null;'
  17715. ]),
  17716. LinesToStr([ // $mod.$main
  17717. '$mod.TObject.FA = $mod.Obj.FA;',
  17718. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17719. '$mod.b = $mod.Obj.FA === 4;',
  17720. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17721. '$mod.b = 5 === $mod.Obj.FA;',
  17722. '$mod.TObject.FA = 6;',
  17723. '$mod.TObject.FA = $mod.Cla.FA;',
  17724. '$mod.Cla.SetA($mod.Cla.GetA());',
  17725. '$mod.b = $mod.Cla.FA === 7;',
  17726. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17727. '$mod.b = 8 === $mod.Cla.FA;',
  17728. '$mod.TObject.FA = 9;',
  17729. '$mod.TObject.SetA($mod.TObject.GetA());',
  17730. '$mod.b = $mod.TObject.FA === 10;',
  17731. '$mod.b = 11 === $mod.TObject.FA;',
  17732. '']));
  17733. end;
  17734. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17735. begin
  17736. StartProgram(false);
  17737. Add('type');
  17738. Add(' TObject = class');
  17739. Add(' class var GlobalId: longint;');
  17740. Add(' class procedure ProcA;');
  17741. Add(' end;');
  17742. Add('class procedure tobject.proca;');
  17743. Add('var b: boolean;');
  17744. Add('begin');
  17745. Add(' b:=self=nil;');
  17746. Add(' b:=self.globalid=3;');
  17747. Add(' b:=4=self.globalid;');
  17748. Add(' self.globalid:=5;');
  17749. Add(' self.proca;');
  17750. Add('end;');
  17751. Add('begin');
  17752. ConvertProgram;
  17753. CheckSource('TestClassOf_ClassMethodSelf',
  17754. LinesToStr([ // statements
  17755. 'rtl.createClass(this, "TObject", null, function () {',
  17756. ' this.GlobalId = 0;',
  17757. ' this.$init = function () {',
  17758. ' };',
  17759. ' this.$final = function () {',
  17760. ' };',
  17761. ' this.ProcA = function () {',
  17762. ' var b = false;',
  17763. ' b = this === null;',
  17764. ' b = this.GlobalId === 3;',
  17765. ' b = 4 === this.GlobalId;',
  17766. ' $mod.TObject.GlobalId = 5;',
  17767. ' this.ProcA();',
  17768. ' };',
  17769. '});'
  17770. ]),
  17771. LinesToStr([ // $mod.$main
  17772. '']));
  17773. end;
  17774. procedure TTestModule.TestClassOf_TypeCast;
  17775. begin
  17776. StartProgram(false);
  17777. Add('type');
  17778. Add(' TObject = class');
  17779. Add(' class procedure {#TObject_DoIt}DoIt;');
  17780. Add(' end;');
  17781. Add(' TClass = class of TObject;');
  17782. Add(' TMobile = class');
  17783. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17784. Add(' end;');
  17785. Add(' TMobileClass = class of TMobile;');
  17786. Add(' TCar = class(TMobile)');
  17787. Add(' class procedure {#TCar_DoIt}DoIt;');
  17788. Add(' end;');
  17789. Add(' TCarClass = class of TCar;');
  17790. Add('class procedure TObject.DoIt;');
  17791. Add('begin');
  17792. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17793. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17794. Add('end;');
  17795. Add('class procedure TMobile.DoIt;');
  17796. Add('begin');
  17797. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17798. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17799. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17800. Add('end;');
  17801. Add('class procedure TCar.DoIt; begin end;');
  17802. Add('var');
  17803. Add(' ObjC: TClass;');
  17804. Add(' MobileC: TMobileClass;');
  17805. Add(' CarC: TCarClass;');
  17806. Add('begin');
  17807. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17808. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17809. Add(' CarC.{@TCar_DoIt}DoIt;');
  17810. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17811. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17812. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17813. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17814. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17815. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17816. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17817. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17818. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17819. ConvertProgram;
  17820. CheckSource('TestClassOf_TypeCast',
  17821. LinesToStr([ // statements
  17822. 'rtl.createClass(this, "TObject", null, function () {',
  17823. ' this.$init = function () {',
  17824. ' };',
  17825. ' this.$final = function () {',
  17826. ' };',
  17827. ' this.DoIt = function () {',
  17828. ' this.DoIt();',
  17829. ' this.DoIt$1();',
  17830. ' };',
  17831. '});',
  17832. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17833. ' this.DoIt$1 = function () {',
  17834. ' this.DoIt();',
  17835. ' this.DoIt$1();',
  17836. ' this.DoIt$2();',
  17837. ' };',
  17838. '});',
  17839. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17840. ' this.DoIt$2 = function () {',
  17841. ' };',
  17842. '});',
  17843. 'this.ObjC = null;',
  17844. 'this.MobileC = null;',
  17845. 'this.CarC = null;',
  17846. '']),
  17847. LinesToStr([ // $mod.$main
  17848. '$mod.ObjC.DoIt();',
  17849. '$mod.MobileC.DoIt$1();',
  17850. '$mod.CarC.DoIt$2();',
  17851. '$mod.ObjC.DoIt();',
  17852. '$mod.ObjC.DoIt$1();',
  17853. '$mod.ObjC.DoIt$2();',
  17854. '$mod.MobileC.DoIt();',
  17855. '$mod.MobileC.DoIt$1();',
  17856. '$mod.MobileC.DoIt$2();',
  17857. '$mod.CarC.DoIt();',
  17858. '$mod.CarC.DoIt$1();',
  17859. '$mod.CarC.DoIt$2();',
  17860. '']));
  17861. end;
  17862. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17863. begin
  17864. StartProgram(false);
  17865. Add('type');
  17866. Add(' TObject = class');
  17867. Add(' function CurNow: longint; ');
  17868. Add(' class function Now: longint; ');
  17869. Add(' end;');
  17870. Add('function TObject.CurNow: longint; begin end;');
  17871. Add('class function TObject.Now: longint; begin end;');
  17872. Add('var');
  17873. Add(' Obj: tobject;');
  17874. Add(' vI: longint;');
  17875. Add('begin');
  17876. Add(' obj.curnow;');
  17877. Add(' vi:=obj.curnow;');
  17878. Add(' tobject.now;');
  17879. Add(' vi:=tobject.now;');
  17880. ConvertProgram;
  17881. CheckSource('TestClassOf_ImplicitFunctionCall',
  17882. LinesToStr([ // statements
  17883. 'rtl.createClass(this, "TObject", null, function () {',
  17884. ' this.$init = function () {',
  17885. ' };',
  17886. ' this.$final = function () {',
  17887. ' };',
  17888. ' this.CurNow = function () {',
  17889. ' var Result = 0;',
  17890. ' return Result;',
  17891. ' };',
  17892. ' this.Now = function () {',
  17893. ' var Result = 0;',
  17894. ' return Result;',
  17895. ' };',
  17896. '});',
  17897. 'this.Obj = null;',
  17898. 'this.vI = 0;',
  17899. '']),
  17900. LinesToStr([ // $mod.$main
  17901. '$mod.Obj.CurNow();',
  17902. '$mod.vI = $mod.Obj.CurNow();',
  17903. '$mod.TObject.Now();',
  17904. '$mod.vI = $mod.TObject.Now();',
  17905. '']));
  17906. end;
  17907. procedure TTestModule.TestClassOf_Const;
  17908. begin
  17909. StartProgram(false);
  17910. Add([
  17911. 'type',
  17912. ' TObject = class',
  17913. ' end;',
  17914. ' TBird = TObject;',
  17915. ' TBirds = class of TBird;',
  17916. ' TEagles = TBirds;',
  17917. ' THawk = class(TBird);',
  17918. 'const',
  17919. ' Hawk: TEagles = THawk;',
  17920. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17921. ' TBird,',
  17922. ' THawk',
  17923. ' );',
  17924. 'begin']);
  17925. ConvertProgram;
  17926. CheckSource('TestClassOf_Const',
  17927. LinesToStr([ // statements
  17928. 'rtl.createClass(this, "TObject", null, function () {',
  17929. ' this.$init = function () {',
  17930. ' };',
  17931. ' this.$final = function () {',
  17932. ' };',
  17933. '});',
  17934. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17935. '});',
  17936. 'this.Hawk = this.THawk;',
  17937. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17938. '']),
  17939. LinesToStr([ // $mod.$main
  17940. '']));
  17941. end;
  17942. procedure TTestModule.TestNestedClass_Alias;
  17943. begin
  17944. WithTypeInfo:=true;
  17945. StartProgram(false);
  17946. Add([
  17947. 'type',
  17948. ' TObject = class',
  17949. ' type TNested = type longint;',
  17950. ' end;',
  17951. 'type TAlias = type tobject.tnested;',
  17952. 'var i: tobject.tnested = 3;',
  17953. 'var j: TAlias = 4;',
  17954. 'begin',
  17955. ' if typeinfo(TAlias)=nil then ;',
  17956. ' if typeinfo(tobject.tnested)=nil then ;',
  17957. '']);
  17958. ConvertProgram;
  17959. CheckSource('TestNestedClass_Alias',
  17960. LinesToStr([ // statements
  17961. 'rtl.createClass(this, "TObject", null, function () {',
  17962. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17963. ' this.$init = function () {',
  17964. ' };',
  17965. ' this.$final = function () {',
  17966. ' };',
  17967. '});',
  17968. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17969. 'this.i = 3;',
  17970. 'this.j = 4;',
  17971. '']),
  17972. LinesToStr([ // $mod.$main
  17973. 'if ($mod.$rtti["TAlias"] === null) ;',
  17974. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17975. '']));
  17976. end;
  17977. procedure TTestModule.TestNestedClass_Record;
  17978. begin
  17979. WithTypeInfo:=true;
  17980. StartProgram(false);
  17981. Add([
  17982. 'type',
  17983. ' TObject = class',
  17984. ' type TPoint = record',
  17985. ' x,y: byte;',
  17986. ' end;',
  17987. ' procedure DoIt(t: TPoint);',
  17988. ' end;',
  17989. 'procedure tobject.DoIt(t: TPoint);',
  17990. 'var p: TPoint;',
  17991. 'begin',
  17992. ' t.x:=t.y;',
  17993. ' p:=t;',
  17994. 'end;',
  17995. 'var',
  17996. ' p: tobject.tpoint = (x:2; y:4);',
  17997. ' o: TObject;',
  17998. 'begin',
  17999. ' p:=p;',
  18000. ' o.doit(p);',
  18001. '']);
  18002. ConvertProgram;
  18003. CheckSource('TestNestedClass_Record',
  18004. LinesToStr([ // statements
  18005. 'rtl.createClass(this, "TObject", null, function () {',
  18006. ' rtl.recNewT(this, "TPoint", function () {',
  18007. ' this.x = 0;',
  18008. ' this.y = 0;',
  18009. ' this.$eq = function (b) {',
  18010. ' return (this.x === b.x) && (this.y === b.y);',
  18011. ' };',
  18012. ' this.$assign = function (s) {',
  18013. ' this.x = s.x;',
  18014. ' this.y = s.y;',
  18015. ' return this;',
  18016. ' };',
  18017. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {}, this);',
  18018. ' $r.addField("x", rtl.byte);',
  18019. ' $r.addField("y", rtl.byte);',
  18020. ' });',
  18021. ' this.$init = function () {',
  18022. ' };',
  18023. ' this.$final = function () {',
  18024. ' };',
  18025. ' this.DoIt = function (t) {',
  18026. ' var p = this.TPoint.$new();',
  18027. ' t.x = t.y;',
  18028. ' p.$assign(t);',
  18029. ' };',
  18030. '});',
  18031. 'this.p = this.TObject.TPoint.$clone({',
  18032. ' x: 2,',
  18033. ' y: 4',
  18034. '});',
  18035. 'this.o = null;',
  18036. '']),
  18037. LinesToStr([ // $mod.$main
  18038. '$mod.p.$assign($mod.p);',
  18039. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  18040. '']));
  18041. end;
  18042. procedure TTestModule.TestNestedClass_Class;
  18043. begin
  18044. StartProgram(false);
  18045. Add([
  18046. 'type',
  18047. ' TObject = class end;',
  18048. ' TBird = class',
  18049. ' type TLeg = class',
  18050. ' FId: longint;',
  18051. ' constructor Create;',
  18052. ' function Create(i: longint): TLeg;',
  18053. ' end;',
  18054. ' function DoIt(b: TBird): Tleg;',
  18055. ' end;',
  18056. 'constructor tbird.tleg.create;',
  18057. 'begin',
  18058. ' FId:=3;',
  18059. 'end;',
  18060. 'function tbird.tleg.Create(i: longint): TLeg;',
  18061. 'begin',
  18062. ' Create;',
  18063. ' Result:=TLeg.Create;',
  18064. ' Result:=TBird.TLeg.Create;',
  18065. ' Result:=Create(3);',
  18066. ' FId:=i;',
  18067. 'end;',
  18068. 'function tbird.DoIt(b: tbird): tleg;',
  18069. 'begin',
  18070. ' Result.Create;',
  18071. ' Result:=TLeg.Create;',
  18072. ' Result:=TBird.TLeg.Create;',
  18073. ' Result:=Result.Create(3);',
  18074. 'end;',
  18075. 'var',
  18076. ' b: Tbird.tleg;',
  18077. 'begin',
  18078. ' b.Create;',
  18079. ' b:=TBird.TLeg.Create;',
  18080. ' b:=b.Create(3);',
  18081. '']);
  18082. ConvertProgram;
  18083. CheckSource('TestNestedClass_Class',
  18084. LinesToStr([ // statements
  18085. 'rtl.createClass(this, "TObject", null, function () {',
  18086. ' this.$init = function () {',
  18087. ' };',
  18088. ' this.$final = function () {',
  18089. ' };',
  18090. '});',
  18091. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18092. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18093. ' this.$init = function () {',
  18094. ' $mod.TObject.$init.call(this);',
  18095. ' this.FId = 0;',
  18096. ' };',
  18097. ' this.Create = function () {',
  18098. ' this.FId = 3;',
  18099. ' return this;',
  18100. ' };',
  18101. ' this.Create$1 = function (i) {',
  18102. ' var Result = null;',
  18103. ' this.Create();',
  18104. ' Result = $mod.TBird.TLeg.$create("Create");',
  18105. ' Result = $mod.TBird.TLeg.$create("Create");',
  18106. ' Result = this.Create$1(3);',
  18107. ' this.FId = i;',
  18108. ' return Result;',
  18109. ' };',
  18110. ' }, "TBird.TLeg");',
  18111. ' this.DoIt = function (b) {',
  18112. ' var Result = null;',
  18113. ' Result.Create();',
  18114. ' Result = this.TLeg.$create("Create");',
  18115. ' Result = $mod.TBird.TLeg.$create("Create");',
  18116. ' Result = Result.Create$1(3);',
  18117. ' return Result;',
  18118. ' };',
  18119. '});',
  18120. 'this.b = null;',
  18121. '']),
  18122. LinesToStr([ // $mod.$main
  18123. '$mod.b.Create();',
  18124. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18125. '$mod.b = $mod.b.Create$1(3);',
  18126. '']));
  18127. end;
  18128. procedure TTestModule.TestNestedClass_CallInherited;
  18129. begin
  18130. StartProgram(false);
  18131. Add([
  18132. 'type',
  18133. ' TObject = class end;',
  18134. ' TBird = class',
  18135. ' type',
  18136. ' TWing = class',
  18137. ' function Fly(w: word = 17): word; virtual;',
  18138. ' end;',
  18139. ' end;',
  18140. ' TEagle = class(TBird)',
  18141. ' type',
  18142. ' TEagleWing = class(TWing)',
  18143. ' function Fly(w: word): word; override;',
  18144. ' end;',
  18145. ' end;',
  18146. 'function TBird.TWing.Fly(w: word): word;',
  18147. 'begin',
  18148. 'end;',
  18149. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18150. 'begin',
  18151. ' inherited;',
  18152. ' inherited Fly;',
  18153. ' inherited Fly(3);',
  18154. ' Result:=inherited Fly;',
  18155. ' Result:=inherited Fly(4);',
  18156. 'end;',
  18157. 'begin',
  18158. '']);
  18159. ConvertProgram;
  18160. CheckSource('TestNestedClass_CallInherited',
  18161. LinesToStr([ // statements
  18162. 'rtl.createClass(this, "TObject", null, function () {',
  18163. ' this.$init = function () {',
  18164. ' };',
  18165. ' this.$final = function () {',
  18166. ' };',
  18167. '});',
  18168. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18169. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18170. ' this.Fly = function (w) {',
  18171. ' var Result = 0;',
  18172. ' return Result;',
  18173. ' };',
  18174. ' }, "TBird.TWing");',
  18175. '});',
  18176. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18177. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18178. ' this.Fly = function (w) {',
  18179. ' var Result = 0;',
  18180. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18181. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18182. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18183. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18184. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18185. ' return Result;',
  18186. ' };',
  18187. ' }, "TEagle.TEagleWing");',
  18188. '});',
  18189. '']),
  18190. LinesToStr([ // $mod.$main
  18191. '']));
  18192. end;
  18193. procedure TTestModule.TestExternalClass_Var;
  18194. begin
  18195. StartProgram(false);
  18196. Add([
  18197. '{$modeswitch externalclass}',
  18198. 'type',
  18199. ' TExtA = class external name ''ExtObj''',
  18200. ' Id: longint external name ''$Id'';',
  18201. ' B: longint;',
  18202. ' end;',
  18203. 'var Obj: TExtA;',
  18204. 'begin',
  18205. ' obj.id:=obj.id+1;',
  18206. ' obj.B:=obj.B+1;']);
  18207. ConvertProgram;
  18208. CheckSource('TestExternalClass_Var',
  18209. LinesToStr([ // statements
  18210. 'this.Obj = null;',
  18211. '']),
  18212. LinesToStr([ // $mod.$main
  18213. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18214. '$mod.Obj.B = $mod.Obj.B + 1;',
  18215. '']));
  18216. end;
  18217. procedure TTestModule.TestExternalClass_Const;
  18218. begin
  18219. StartProgram(false);
  18220. Add([
  18221. '{$modeswitch externalclass}',
  18222. 'type',
  18223. ' TExtA = class external name ''ExtObj''',
  18224. ' const Two: longint = 2;',
  18225. ' const Three = 3;',
  18226. ' const Id: longint;',
  18227. ' end;',
  18228. ' TExtB = class external name ''ExtB''',
  18229. ' A: TExtA;',
  18230. ' end;',
  18231. 'var',
  18232. ' A: texta;',
  18233. ' B: textb;',
  18234. ' i: longint;',
  18235. 'begin',
  18236. ' i:=a.two;',
  18237. ' i:=texta.two;',
  18238. ' i:=a.three;',
  18239. ' i:=texta.three;',
  18240. ' i:=a.id;',
  18241. ' i:=texta.id;',
  18242. '']);
  18243. ConvertProgram;
  18244. CheckSource('TestExternalClass_Const',
  18245. LinesToStr([ // statements
  18246. 'this.A = null;',
  18247. 'this.B = null;',
  18248. 'this.i = 0;',
  18249. '']),
  18250. LinesToStr([ // $mod.$main
  18251. '$mod.i = 2;',
  18252. '$mod.i = 2;',
  18253. '$mod.i = 3;',
  18254. '$mod.i = 3;',
  18255. '$mod.i = $mod.A.Id;',
  18256. '$mod.i = ExtObj.Id;',
  18257. '']));
  18258. end;
  18259. procedure TTestModule.TestExternalClass_Dollar;
  18260. begin
  18261. StartProgram(false);
  18262. Add([
  18263. '{$modeswitch externalclass}',
  18264. 'type',
  18265. ' TExtA = class external name ''$''',
  18266. ' Id: longint external name ''$'';',
  18267. ' function Bla(i: longint): longint; external name ''$'';',
  18268. ' end;',
  18269. 'function dollar(k: longint): longint; external name ''$'';',
  18270. 'var Obj: TExtA;',
  18271. 'begin',
  18272. ' dollar(1);',
  18273. ' obj.id:=obj.id+2;',
  18274. ' obj.Bla(3);',
  18275. '']);
  18276. ConvertProgram;
  18277. CheckSource('TestExternalClass_Dollar',
  18278. LinesToStr([ // statements
  18279. 'this.Obj = null;',
  18280. '']),
  18281. LinesToStr([ // $mod.$main
  18282. '$(1);',
  18283. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18284. '$mod.Obj.$(3);',
  18285. '']));
  18286. end;
  18287. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18288. begin
  18289. StartProgram(false);
  18290. Add('{$modeswitch externalclass}');
  18291. Add('type');
  18292. Add(' TExtA = class external name ''ExtA''');
  18293. Add(' Id: longint external name ''$Id'';');
  18294. Add(' end;');
  18295. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18296. Add(' Id: longint;');
  18297. Add(' end;');
  18298. Add('begin');
  18299. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18300. ConvertProgram;
  18301. end;
  18302. procedure TTestModule.TestExternalClass_Method;
  18303. begin
  18304. StartProgram(false);
  18305. Add(['{$modeswitch externalclass}',
  18306. 'type',
  18307. ' TExtA = class external name ''ExtObj''',
  18308. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18309. ' procedure DoSome(Id: longint = 1);',
  18310. ' end;',
  18311. 'var Obj: texta;',
  18312. 'begin',
  18313. ' obj.doit;',
  18314. ' obj.doit();',
  18315. ' obj.doit(2);',
  18316. ' with obj do begin',
  18317. ' doit;',
  18318. ' doit();',
  18319. ' doit(3);',
  18320. ' end;']);
  18321. ConvertProgram;
  18322. CheckSource('TestExternalClass_Method',
  18323. LinesToStr([ // statements
  18324. 'this.Obj = null;',
  18325. '']),
  18326. LinesToStr([ // $mod.$main
  18327. '$mod.Obj.$Execute(1);',
  18328. '$mod.Obj.$Execute(1);',
  18329. '$mod.Obj.$Execute(2);',
  18330. 'var $with = $mod.Obj;',
  18331. '$with.$Execute(1);',
  18332. '$with.$Execute(1);',
  18333. '$with.$Execute(3);',
  18334. '']));
  18335. end;
  18336. procedure TTestModule.TestExternalClass_ClassMethod;
  18337. begin
  18338. StartProgram(false);
  18339. Add([
  18340. '{$modeswitch externalclass}',
  18341. 'type',
  18342. ' TExtA = class external name ''ExtObj''',
  18343. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18344. ' end;',
  18345. ' TExtB = TExtA;',
  18346. 'var p: Pointer;',
  18347. 'begin',
  18348. ' texta.doit;',
  18349. ' texta.doit();',
  18350. ' texta.doit(2);',
  18351. ' p:[email protected];',
  18352. ' with texta do begin',
  18353. ' doit;',
  18354. ' doit();',
  18355. ' doit(3);',
  18356. ' p:=@DoIt;',
  18357. ' end;',
  18358. ' textb.doit;',
  18359. ' textb.doit();',
  18360. ' textb.doit(4);',
  18361. ' with textb do begin',
  18362. ' doit;',
  18363. ' doit();',
  18364. ' doit(5);',
  18365. ' end;',
  18366. '']);
  18367. ConvertProgram;
  18368. CheckSource('TestExternalClass_ClassMethod',
  18369. LinesToStr([ // statements
  18370. 'this.p = null;',
  18371. '']),
  18372. LinesToStr([ // $mod.$main
  18373. 'ExtObj.$Execute(1);',
  18374. 'ExtObj.$Execute(1);',
  18375. 'ExtObj.$Execute(2);',
  18376. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18377. 'ExtObj.$Execute(1);',
  18378. 'ExtObj.$Execute(1);',
  18379. 'ExtObj.$Execute(3);',
  18380. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18381. 'ExtObj.$Execute(1);',
  18382. 'ExtObj.$Execute(1);',
  18383. 'ExtObj.$Execute(4);',
  18384. 'ExtObj.$Execute(1);',
  18385. 'ExtObj.$Execute(1);',
  18386. 'ExtObj.$Execute(5);',
  18387. '']));
  18388. end;
  18389. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18390. begin
  18391. StartProgram(false);
  18392. Add([
  18393. '{$modeswitch externalclass}',
  18394. 'type',
  18395. ' TExtA = class external name ''ExtObj''',
  18396. ' class procedure DoIt(Id: longint = 1); static;',
  18397. ' end;',
  18398. 'var p: Pointer;',
  18399. 'begin',
  18400. ' texta.doit;',
  18401. ' texta.doit();',
  18402. ' texta.doit(2);',
  18403. ' p:[email protected];',
  18404. ' with texta do begin',
  18405. ' doit;',
  18406. ' doit();',
  18407. ' doit(3);',
  18408. ' p:=@DoIt;',
  18409. ' end;',
  18410. '']);
  18411. ConvertProgram;
  18412. CheckSource('TestExternalClass_ClassMethodStatic',
  18413. LinesToStr([ // statements
  18414. 'this.p = null;',
  18415. '']),
  18416. LinesToStr([ // $mod.$main
  18417. 'ExtObj.DoIt(1);',
  18418. 'ExtObj.DoIt(1);',
  18419. 'ExtObj.DoIt(2);',
  18420. '$mod.p = ExtObj.DoIt;',
  18421. 'ExtObj.DoIt(1);',
  18422. 'ExtObj.DoIt(1);',
  18423. 'ExtObj.DoIt(3);',
  18424. '$mod.p = ExtObj.DoIt;',
  18425. '']));
  18426. end;
  18427. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18428. begin
  18429. StartProgram(false);
  18430. Add([
  18431. '{$modeswitch externalclass}',
  18432. 'type',
  18433. ' TBird = class external name ''Array''',
  18434. ' end;',
  18435. 'function GetPtr: Pointer;',
  18436. 'begin',
  18437. 'end;',
  18438. 'procedure Write(const p);',
  18439. 'begin',
  18440. 'end;',
  18441. 'procedure WriteLn; varargs;',
  18442. 'begin',
  18443. 'end;',
  18444. 'begin',
  18445. ' if TBird(GetPtr)=nil then ;',
  18446. ' Write(GetPtr);',
  18447. ' WriteLn(GetPtr);',
  18448. ' Write(TBird(GetPtr));',
  18449. ' WriteLn(TBird(GetPtr));',
  18450. '']);
  18451. ConvertProgram;
  18452. CheckSource('TestFunctionResultInTypeCast',
  18453. LinesToStr([ // statements
  18454. 'this.GetPtr = function () {',
  18455. ' var Result = null;',
  18456. ' return Result;',
  18457. '};',
  18458. 'this.Write = function (p) {',
  18459. '};',
  18460. 'this.WriteLn = function () {',
  18461. '};',
  18462. '']),
  18463. LinesToStr([
  18464. 'if ($mod.GetPtr() === null) ;',
  18465. '$mod.Write($mod.GetPtr());',
  18466. '$mod.WriteLn($mod.GetPtr());',
  18467. '$mod.Write($mod.GetPtr());',
  18468. '$mod.WriteLn($mod.GetPtr());',
  18469. '']));
  18470. end;
  18471. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18472. begin
  18473. StartProgram(false);
  18474. Add([
  18475. '{$modeswitch externalclass}',
  18476. 'type',
  18477. ' TExtA = class external name ''ExtObjA''',
  18478. ' procedure ProcA; virtual;',
  18479. ' procedure ProcB; virtual;',
  18480. ' end;',
  18481. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18482. ' end;',
  18483. ' TExtC = class (TExtB)',
  18484. ' procedure ProcA; override;',
  18485. ' end;',
  18486. 'procedure TExtC.ProcA;',
  18487. 'begin',
  18488. ' ProcA;',
  18489. ' Self.ProcA;',
  18490. ' ProcB;',
  18491. ' Self.ProcB;',
  18492. 'end;',
  18493. 'var',
  18494. ' A: texta;',
  18495. ' B: textb;',
  18496. ' C: textc;',
  18497. 'begin',
  18498. ' a.proca;',
  18499. ' b.proca;',
  18500. ' c.proca;']);
  18501. ConvertProgram;
  18502. CheckSource('TestExternalClass_NonExternalOverride',
  18503. LinesToStr([ // statements
  18504. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18505. ' this.$init = function () {',
  18506. ' };',
  18507. ' this.$final = function () {',
  18508. ' };',
  18509. ' this.ProcA = function () {',
  18510. ' this.ProcA();',
  18511. ' this.ProcA();',
  18512. ' this.ProcB();',
  18513. ' this.ProcB();',
  18514. ' };',
  18515. '});',
  18516. 'this.A = null;',
  18517. 'this.B = null;',
  18518. 'this.C = null;',
  18519. '']),
  18520. LinesToStr([ // $mod.$main
  18521. '$mod.A.ProcA();',
  18522. '$mod.B.ProcA();',
  18523. '$mod.C.ProcA();',
  18524. '']));
  18525. end;
  18526. procedure TTestModule.TestExternalClass_OverloadHint;
  18527. begin
  18528. StartProgram(false);
  18529. Add([
  18530. '{$modeswitch externalclass}',
  18531. 'type',
  18532. ' TExtA = class external name ''ExtObjA''',
  18533. ' procedure DoIt;',
  18534. ' procedure DoIt(i: longint);',
  18535. ' end;',
  18536. 'begin',
  18537. '']);
  18538. ConvertProgram;
  18539. CheckResolverUnexpectedHints(true);
  18540. CheckSource('TestExternalClass_OverloadHint',
  18541. LinesToStr([ // statements
  18542. '']),
  18543. LinesToStr([ // $mod.$main
  18544. '']));
  18545. end;
  18546. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18547. begin
  18548. WithTypeInfo:=true;
  18549. StartProgram(false);
  18550. Add([
  18551. '{$modeswitch externalclass}',
  18552. 'type',
  18553. ' JSwiper = class external name ''Swiper''',
  18554. ' constructor New;',
  18555. ' end;',
  18556. ' TObject = class',
  18557. ' private',
  18558. ' FSwiper: JSwiper;',
  18559. ' published',
  18560. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18561. ' end;',
  18562. 'begin',
  18563. ' JSwiper.new;',
  18564. '']);
  18565. ConvertProgram;
  18566. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18567. LinesToStr([ // statements
  18568. 'this.$rtti.$ExtClass("JSwiper", {',
  18569. ' jsclass: "Swiper"',
  18570. '});',
  18571. 'rtl.createClass(this, "TObject", null, function () {',
  18572. ' this.$init = function () {',
  18573. ' this.FSwiper = null;',
  18574. ' };',
  18575. ' this.$final = function () {',
  18576. ' this.FSwiper = undefined;',
  18577. ' };',
  18578. ' var $r = this.$rtti;',
  18579. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18580. '});',
  18581. '']),
  18582. LinesToStr([ // $mod.$main
  18583. 'new Swiper();',
  18584. '']));
  18585. end;
  18586. procedure TTestModule.TestExternalClass_Property;
  18587. begin
  18588. StartProgram(false);
  18589. Add([
  18590. '{$modeswitch externalclass}',
  18591. 'type',
  18592. ' TExtA = class external name ''ExtA''',
  18593. ' function getYear: longint;',
  18594. ' procedure setYear(Value: longint);',
  18595. ' property Year: longint read getyear write setyear;',
  18596. ' end;',
  18597. ' TExtB = class (TExtA)',
  18598. ' procedure OtherSetYear(Value: longint);',
  18599. ' property year write othersetyear;',
  18600. ' end;',
  18601. 'procedure textb.othersetyear(value: longint);',
  18602. 'begin',
  18603. ' setYear(Value+4);',
  18604. 'end;',
  18605. 'var',
  18606. ' A: texta;',
  18607. ' B: textb;',
  18608. 'begin',
  18609. ' a.year:=a.year+1;',
  18610. ' b.year:=b.year+2;']);
  18611. ConvertProgram;
  18612. CheckSource('TestExternalClass_NonExternalOverride',
  18613. LinesToStr([ // statements
  18614. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18615. ' this.$init = function () {',
  18616. ' };',
  18617. ' this.$final = function () {',
  18618. ' };',
  18619. ' this.OtherSetYear = function (Value) {',
  18620. ' this.setYear(Value+4);',
  18621. ' };',
  18622. '});',
  18623. 'this.A = null;',
  18624. 'this.B = null;',
  18625. '']),
  18626. LinesToStr([ // $mod.$main
  18627. '$mod.A.setYear($mod.A.getYear()+1);',
  18628. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18629. '']));
  18630. end;
  18631. procedure TTestModule.TestExternalClass_PropertyDate;
  18632. begin
  18633. StartProgram(false);
  18634. Add([
  18635. '{$modeswitch externalclass}',
  18636. 'type',
  18637. ' TExtA = class external name ''ExtA''',
  18638. ' end;',
  18639. ' TExtB = class (TExtA)',
  18640. ' FDate: string;',
  18641. ' property Date: string read FDate write FDate;',
  18642. ' property ExtA: string read FDate write FDate;',
  18643. ' end;',
  18644. ' {$M+}',
  18645. ' TObject = class',
  18646. ' FDate: string;',
  18647. ' published',
  18648. ' property Date: string read FDate write FDate;',
  18649. ' property ExtA: string read FDate write FDate;',
  18650. ' end;',
  18651. 'var',
  18652. ' B: textb;',
  18653. ' o: TObject;',
  18654. 'begin',
  18655. ' b.date:=b.exta;',
  18656. ' o.date:=o.exta;']);
  18657. ConvertProgram;
  18658. CheckSource('TestExternalClass_PropertyDate',
  18659. LinesToStr([ // statements
  18660. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18661. ' this.$init = function () {',
  18662. ' this.FDate = "";',
  18663. ' };',
  18664. ' this.$final = function () {',
  18665. ' };',
  18666. '});',
  18667. 'rtl.createClass(this, "TObject", null, function () {',
  18668. ' this.$init = function () {',
  18669. ' this.FDate = "";',
  18670. ' };',
  18671. ' this.$final = function () {',
  18672. ' };',
  18673. ' var $r = this.$rtti;',
  18674. ' $r.addField("FDate", rtl.string, 4);',
  18675. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18676. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18677. '});',
  18678. 'this.B = null;',
  18679. 'this.o = null;',
  18680. '']),
  18681. LinesToStr([ // $mod.$main
  18682. '$mod.B.FDate = $mod.B.FDate;',
  18683. '$mod.o.FDate = $mod.o.FDate;',
  18684. '']));
  18685. end;
  18686. procedure TTestModule.TestExternalClass_ClassProperty;
  18687. begin
  18688. StartProgram(false);
  18689. Add('{$modeswitch externalclass}');
  18690. Add('type');
  18691. Add(' TExtA = class external name ''ExtA''');
  18692. Add(' class function getYear: longint;');
  18693. Add(' class procedure setYear(Value: longint);');
  18694. Add(' class property Year: longint read getyear write setyear;');
  18695. Add(' end;');
  18696. Add(' TExtB = class (TExtA)');
  18697. Add(' class function GetCentury: longint;');
  18698. Add(' class procedure SetCentury(Value: longint);');
  18699. Add(' class property Century: longint read getcentury write setcentury;');
  18700. Add(' end;');
  18701. Add('class function textb.getcentury: longint;');
  18702. Add('begin');
  18703. Add('end;');
  18704. Add('class procedure textb.setcentury(value: longint);');
  18705. Add('begin');
  18706. Add(' setyear(value+11);');
  18707. Add(' texta.year:=texta.year+12;');
  18708. Add(' year:=year+13;');
  18709. Add(' textb.century:=textb.century+14;');
  18710. Add(' century:=century+15;');
  18711. Add('end;');
  18712. Add('var');
  18713. Add(' A: texta;');
  18714. Add(' B: textb;');
  18715. Add('begin');
  18716. Add(' texta.year:=texta.year+1;');
  18717. Add(' textb.year:=textb.year+2;');
  18718. Add(' TextA.year:=TextA.year+3;');
  18719. Add(' b.year:=b.year+4;');
  18720. Add(' textb.century:=textb.century+5;');
  18721. Add(' b.century:=b.century+6;');
  18722. ConvertProgram;
  18723. CheckSource('TestExternalClass_ClassProperty',
  18724. LinesToStr([ // statements
  18725. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18726. ' this.$init = function () {',
  18727. ' };',
  18728. ' this.$final = function () {',
  18729. ' };',
  18730. ' this.GetCentury = function () {',
  18731. ' var Result = 0;',
  18732. ' return Result;',
  18733. ' };',
  18734. ' this.SetCentury = function (Value) {',
  18735. ' this.setYear(Value + 11);',
  18736. ' ExtA.setYear(ExtA.getYear() + 12);',
  18737. ' this.setYear(this.getYear() + 13);',
  18738. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18739. ' this.SetCentury(this.GetCentury() + 15);',
  18740. ' };',
  18741. '});',
  18742. 'this.A = null;',
  18743. 'this.B = null;',
  18744. '']),
  18745. LinesToStr([ // $mod.$main
  18746. 'ExtA.setYear(ExtA.getYear() + 1);',
  18747. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18748. 'ExtA.setYear(ExtA.getYear() + 3);',
  18749. '$mod.B.setYear($mod.B.getYear() + 4);',
  18750. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18751. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18752. '']));
  18753. end;
  18754. procedure TTestModule.TestExternalClass_ClassOf;
  18755. begin
  18756. StartProgram(false);
  18757. Add('{$modeswitch externalclass}');
  18758. Add('type');
  18759. Add(' TExtA = class external name ''ExtA''');
  18760. Add(' procedure ProcA; virtual;');
  18761. Add(' procedure ProcB; virtual;');
  18762. Add(' end;');
  18763. Add(' TExtAClass = class of TExtA;');
  18764. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18765. Add(' end;');
  18766. Add(' TExtBClass = class of TExtB;');
  18767. Add(' TExtC = class (TExtB)');
  18768. Add(' procedure ProcA; override;');
  18769. Add(' end;');
  18770. Add(' TExtCClass = class of TExtC;');
  18771. Add('procedure TExtC.ProcA; begin end;');
  18772. Add('var');
  18773. Add(' A: texta; ClA: TExtAClass;');
  18774. Add(' B: textb; ClB: TExtBClass;');
  18775. Add(' C: textc; ClC: TExtCClass;');
  18776. Add('begin');
  18777. Add(' ClA:=texta;');
  18778. Add(' ClA:=textb;');
  18779. Add(' ClA:=textc;');
  18780. Add(' ClB:=textb;');
  18781. Add(' ClB:=textc;');
  18782. Add(' ClC:=textc;');
  18783. ConvertProgram;
  18784. CheckSource('TestExternalClass_ClassOf',
  18785. LinesToStr([ // statements
  18786. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18787. ' this.$init = function () {',
  18788. ' };',
  18789. ' this.$final = function () {',
  18790. ' };',
  18791. ' this.ProcA = function () {',
  18792. ' };',
  18793. '});',
  18794. 'this.A = null;',
  18795. 'this.ClA = null;',
  18796. 'this.B = null;',
  18797. 'this.ClB = null;',
  18798. 'this.C = null;',
  18799. 'this.ClC = null;',
  18800. '']),
  18801. LinesToStr([ // $mod.$main
  18802. '$mod.ClA = ExtA;',
  18803. '$mod.ClA = ExtB;',
  18804. '$mod.ClA = $mod.TExtC;',
  18805. '$mod.ClB = ExtB;',
  18806. '$mod.ClB = $mod.TExtC;',
  18807. '$mod.ClC = $mod.TExtC;',
  18808. '']));
  18809. end;
  18810. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18811. begin
  18812. AddModuleWithIntfImplSrc('unit2.pas',
  18813. LinesToStr([
  18814. '{$modeswitch externalclass}',
  18815. 'type',
  18816. ' TExtA = class external name ''ExtA''',
  18817. ' class var Id: longint;',
  18818. ' end;',
  18819. '']),
  18820. '');
  18821. StartUnit(true);
  18822. Add('interface');
  18823. Add('uses unit2;');
  18824. Add('implementation');
  18825. Add('begin');
  18826. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18827. ConvertUnit;
  18828. CheckSource('TestExternalClass_ClassOtherUnit',
  18829. LinesToStr([
  18830. '']),
  18831. LinesToStr([
  18832. 'ExtA.Id = ExtA.Id + 1;',
  18833. '']));
  18834. end;
  18835. procedure TTestModule.TestExternalClass_Is;
  18836. begin
  18837. StartProgram(false);
  18838. Add([
  18839. '{$modeswitch externalclass}',
  18840. 'type',
  18841. ' TExtA = class external name ''ExtA''',
  18842. ' end;',
  18843. ' TExtAClass = class of TExtA;',
  18844. ' TExtB = class external name ''ExtB'' (TExtA)',
  18845. ' end;',
  18846. ' TExtBClass = class of TExtB;',
  18847. ' TExtC = class (TExtB)',
  18848. ' end;',
  18849. ' TExtCClass = class of TExtC;',
  18850. 'var',
  18851. ' A: texta; ClA: TExtAClass;',
  18852. ' B: textb; ClB: TExtBClass;',
  18853. ' C: textc; ClC: TExtCClass;',
  18854. 'begin',
  18855. ' if a is textb then ;',
  18856. ' if a is textc then ;',
  18857. ' if b is textc then ;',
  18858. ' if cla is textb then ;',
  18859. ' if cla is textc then ;',
  18860. ' if clb is textc then ;',
  18861. ' try',
  18862. ' except',
  18863. ' on TExtA do ;',
  18864. ' on e: TExtB do ;',
  18865. ' end;',
  18866. '']);
  18867. ConvertProgram;
  18868. CheckSource('TestExternalClass_Is',
  18869. LinesToStr([ // statements
  18870. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18871. ' this.$init = function () {',
  18872. ' };',
  18873. ' this.$final = function () {',
  18874. ' };',
  18875. '});',
  18876. 'this.A = null;',
  18877. 'this.ClA = null;',
  18878. 'this.B = null;',
  18879. 'this.ClB = null;',
  18880. 'this.C = null;',
  18881. 'this.ClC = null;',
  18882. '']),
  18883. LinesToStr([ // $mod.$main
  18884. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18885. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18886. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18887. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18888. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18889. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18890. 'try {} catch ($e) {',
  18891. ' if (rtl.isExt($e,ExtA)) {}',
  18892. ' else if (rtl.isExt($e,ExtB)) {',
  18893. ' var e = $e;',
  18894. ' } else throw $e',
  18895. '};',
  18896. '']));
  18897. end;
  18898. procedure TTestModule.TestExternalClass_As;
  18899. begin
  18900. StartProgram(false);
  18901. Add('{$modeswitch externalclass}');
  18902. Add('type');
  18903. Add(' TExtA = class external name ''ExtA''');
  18904. Add(' end;');
  18905. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18906. Add(' end;');
  18907. Add(' TExtC = class (TExtB)');
  18908. Add(' end;');
  18909. Add('var');
  18910. Add(' A: texta;');
  18911. Add(' B: textb;');
  18912. Add(' C: textc;');
  18913. Add('begin');
  18914. Add(' b:=a as textb;');
  18915. Add(' c:=a as textc;');
  18916. Add(' c:=b as textc;');
  18917. ConvertProgram;
  18918. CheckSource('TestExternalClass_Is',
  18919. LinesToStr([ // statements
  18920. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18921. ' this.$init = function () {',
  18922. ' };',
  18923. ' this.$final = function () {',
  18924. ' };',
  18925. '});',
  18926. 'this.A = null;',
  18927. 'this.B = null;',
  18928. 'this.C = null;',
  18929. '']),
  18930. LinesToStr([ // $mod.$main
  18931. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18932. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18933. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18934. '']));
  18935. end;
  18936. procedure TTestModule.TestExternalClass_DestructorFail;
  18937. begin
  18938. StartProgram(false);
  18939. Add('{$modeswitch externalclass}');
  18940. Add('type');
  18941. Add(' TExtA = class external name ''ExtA''');
  18942. Add(' destructor Free;');
  18943. Add(' end;');
  18944. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18945. nPasElementNotSupported);
  18946. ConvertProgram;
  18947. end;
  18948. procedure TTestModule.TestExternalClass_New;
  18949. begin
  18950. StartProgram(false);
  18951. Add([
  18952. '{$modeswitch externalclass}',
  18953. 'type',
  18954. ' TExtA = class external name ''ExtA''',
  18955. ' constructor New;',
  18956. ' constructor New(i: longint; j: longint = 2);',
  18957. ' end;',
  18958. 'var',
  18959. ' A: texta;',
  18960. 'begin',
  18961. ' a:=texta.new;',
  18962. ' a:=texta(texta.new);',
  18963. ' a:=texta.new();',
  18964. ' a:=texta.new(1);',
  18965. ' with texta do begin',
  18966. ' a:=new;',
  18967. ' a:=new();',
  18968. ' a:=new(2);',
  18969. ' end;',
  18970. ' a:=test1.texta.new;',
  18971. ' a:=test1.texta.new();',
  18972. ' a:=test1.texta.new(3);',
  18973. '']);
  18974. ConvertProgram;
  18975. CheckSource('TestExternalClass_New',
  18976. LinesToStr([ // statements
  18977. 'this.A = null;',
  18978. '']),
  18979. LinesToStr([ // $mod.$main
  18980. '$mod.A = new ExtA();',
  18981. '$mod.A = new ExtA();',
  18982. '$mod.A = new ExtA();',
  18983. '$mod.A = new ExtA(1,2);',
  18984. '$mod.A = new ExtA();',
  18985. '$mod.A = new ExtA();',
  18986. '$mod.A = new ExtA(2,2);',
  18987. '$mod.A = new ExtA();',
  18988. '$mod.A = new ExtA();',
  18989. '$mod.A = new ExtA(3,2);',
  18990. '']));
  18991. end;
  18992. procedure TTestModule.TestExternalClass_ClassOf_New;
  18993. begin
  18994. StartProgram(false);
  18995. Add('{$modeswitch externalclass}');
  18996. Add('type');
  18997. Add(' TExtAClass = class of TExtA;');
  18998. Add(' TExtA = class external name ''ExtA''');
  18999. Add(' C: TExtAClass;');
  19000. Add(' constructor New;');
  19001. Add(' end;');
  19002. Add('var');
  19003. Add(' A: texta;');
  19004. Add(' C: textaclass;');
  19005. Add('begin');
  19006. Add(' a:=c.new;');
  19007. Add(' a:=c.new();');
  19008. Add(' with C do begin');
  19009. Add(' a:=new;');
  19010. Add(' a:=new();');
  19011. Add(' end;');
  19012. Add(' a:=test1.c.new;');
  19013. Add(' a:=test1.c.new();');
  19014. Add(' a:=A.c.new();');
  19015. ConvertProgram;
  19016. CheckSource('TestExternalClass_ClassOf_New',
  19017. LinesToStr([ // statements
  19018. 'this.A = null;',
  19019. 'this.C = null;',
  19020. '']),
  19021. LinesToStr([ // $mod.$main
  19022. '$mod.A = new $mod.C();',
  19023. '$mod.A = new $mod.C();',
  19024. 'var $with = $mod.C;',
  19025. '$mod.A = new $with();',
  19026. '$mod.A = new $with();',
  19027. '$mod.A = new $mod.C();',
  19028. '$mod.A = new $mod.C();',
  19029. '$mod.A = new $mod.A.C();',
  19030. '']));
  19031. end;
  19032. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  19033. begin
  19034. StartProgram(false);
  19035. Add([
  19036. '{$modeswitch externalclass}',
  19037. 'type',
  19038. ' TExtAClass = class of TExtA;',
  19039. ' TExtA = class external name ''ExtA''',
  19040. ' constructor New;',
  19041. ' end;',
  19042. 'function GetCreator: TExtAClass;',
  19043. 'begin',
  19044. ' Result:=TExtA;',
  19045. 'end;',
  19046. 'var',
  19047. ' A: texta;',
  19048. 'begin',
  19049. ' a:=getcreator.new;',
  19050. ' a:=getcreator().new;',
  19051. ' a:=getcreator().new();',
  19052. ' a:=getcreator.new();',
  19053. ' with getcreator do begin',
  19054. ' a:=new;',
  19055. ' a:=new();',
  19056. ' end;']);
  19057. ConvertProgram;
  19058. CheckSource('TestExternalClass_FuncClassOf_New',
  19059. LinesToStr([ // statements
  19060. 'this.GetCreator = function () {',
  19061. ' var Result = null;',
  19062. ' Result = ExtA;',
  19063. ' return Result;',
  19064. '};',
  19065. 'this.A = null;',
  19066. '']),
  19067. LinesToStr([ // $mod.$main
  19068. '$mod.A = new ($mod.GetCreator())();',
  19069. '$mod.A = new ($mod.GetCreator())();',
  19070. '$mod.A = new ($mod.GetCreator())();',
  19071. '$mod.A = new ($mod.GetCreator())();',
  19072. 'var $with = $mod.GetCreator();',
  19073. '$mod.A = new $with();',
  19074. '$mod.A = new $with();',
  19075. '']));
  19076. end;
  19077. procedure TTestModule.TestExternalClass_New_PasClassFail;
  19078. begin
  19079. StartProgram(false);
  19080. Add([
  19081. '{$modeswitch externalclass}',
  19082. 'type',
  19083. ' TExtA = class external name ''ExtA''',
  19084. ' constructor New;',
  19085. ' end;',
  19086. ' TBird = class(TExtA)',
  19087. ' end;',
  19088. 'begin',
  19089. ' TBird.new;',
  19090. '']);
  19091. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19092. ConvertProgram;
  19093. end;
  19094. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19095. begin
  19096. StartProgram(false);
  19097. Add([
  19098. '{$modeswitch externalclass}',
  19099. 'type',
  19100. ' TExtA = class external name ''ExtA''',
  19101. ' constructor New;',
  19102. ' end;',
  19103. ' TBird = class(TExtA)',
  19104. ' end;',
  19105. 'begin',
  19106. ' TBird.new();',
  19107. '']);
  19108. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19109. ConvertProgram;
  19110. end;
  19111. procedure TTestModule.TestExternalClass_NewExtName;
  19112. begin
  19113. StartProgram(false);
  19114. Add([
  19115. '{$modeswitch externalclass}',
  19116. 'type',
  19117. ' TExtA = class external name ''ExtA''',
  19118. ' constructor New; external name ''Other'';',
  19119. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19120. ' end;',
  19121. 'var',
  19122. ' A: texta;',
  19123. 'begin',
  19124. ' a:=texta.new;',
  19125. ' a:=texta(texta.new);',
  19126. ' a:=texta.new();',
  19127. ' a:=texta.new(1);',
  19128. ' with texta do begin',
  19129. ' a:=new;',
  19130. ' a:=new();',
  19131. ' a:=new(2);',
  19132. ' end;',
  19133. ' a:=test1.texta.new;',
  19134. ' a:=test1.texta.new();',
  19135. ' a:=test1.texta.new(3);',
  19136. '']);
  19137. ConvertProgram;
  19138. CheckSource('TestExternalClass_NewExtName',
  19139. LinesToStr([ // statements
  19140. 'this.A = null;',
  19141. '']),
  19142. LinesToStr([ // $mod.$main
  19143. '$mod.A = new Other();',
  19144. '$mod.A = new Other();',
  19145. '$mod.A = new Other();',
  19146. '$mod.A = new A.B(1,2);',
  19147. '$mod.A = new Other();',
  19148. '$mod.A = new Other();',
  19149. '$mod.A = new A.B(2,2);',
  19150. '$mod.A = new Other();',
  19151. '$mod.A = new Other();',
  19152. '$mod.A = new A.B(3,2);',
  19153. '']));
  19154. end;
  19155. procedure TTestModule.TestExternalClass_Constructor;
  19156. begin
  19157. StartProgram(false);
  19158. Add([
  19159. '{$modeswitch externalclass}',
  19160. 'type',
  19161. ' TExtA = class external name ''ExtA''',
  19162. ' public type',
  19163. ' TExtB = class external name ''ExtB''',
  19164. ' public type',
  19165. ' TExtC = class external name ''ExtC''',
  19166. ' constructor New;',
  19167. ' constructor New(i: word);',
  19168. ' end;',
  19169. ' end;',
  19170. ' constructor Create;',
  19171. ' constructor Create(i: longint; j: longint = 2);',
  19172. ' end;',
  19173. 'var',
  19174. ' A: texta;',
  19175. ' C: texta.textb.textc;',
  19176. 'begin',
  19177. ' a:=texta.create;',
  19178. ' a:=texta(texta.create);',
  19179. ' a:=texta.create();',
  19180. ' a:=texta.create(1);',
  19181. ' with texta do begin',
  19182. ' a:=create;',
  19183. ' a:=create();',
  19184. ' a:=create(2);',
  19185. ' end;',
  19186. ' a:=test1.texta.create;',
  19187. ' a:=test1.texta.create();',
  19188. ' a:=test1.texta.create(3);',
  19189. ' c:=texta.textb.textc.new;',
  19190. ' c:=texta.textb.textc.new();',
  19191. ' c:=texta.textb.textc.new(4);',
  19192. '']);
  19193. ConvertProgram;
  19194. CheckSource('TestExternalClass_Constructor',
  19195. LinesToStr([ // statements
  19196. 'this.A = null;',
  19197. 'this.C = null;',
  19198. '']),
  19199. LinesToStr([ // $mod.$main
  19200. '$mod.A = new ExtA.Create();',
  19201. '$mod.A = new ExtA.Create();',
  19202. '$mod.A = new ExtA.Create();',
  19203. '$mod.A = new ExtA.Create(1,2);',
  19204. '$mod.A = new ExtA.Create();',
  19205. '$mod.A = new ExtA.Create();',
  19206. '$mod.A = new ExtA.Create(2,2);',
  19207. '$mod.A = new ExtA.Create();',
  19208. '$mod.A = new ExtA.Create();',
  19209. '$mod.A = new ExtA.Create(3,2);',
  19210. '$mod.C = new ExtA.ExtB.ExtC();',
  19211. '$mod.C = new ExtA.ExtB.ExtC();',
  19212. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19213. '']));
  19214. end;
  19215. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19216. begin
  19217. StartProgram(false);
  19218. Add([
  19219. '{$modeswitch externalclass}',
  19220. 'type',
  19221. ' TExtA = class external name ''ExtA''',
  19222. ' constructor Create; external name ''{}'';',
  19223. ' end;',
  19224. 'var',
  19225. ' A: texta;',
  19226. 'begin',
  19227. ' a:=texta.create;',
  19228. ' a:=texta(texta.create);',
  19229. ' a:=texta.create();',
  19230. ' with texta do begin',
  19231. ' a:=create;',
  19232. ' a:=create();',
  19233. ' end;',
  19234. ' a:=test1.texta.create;',
  19235. ' a:=test1.texta.create();',
  19236. '']);
  19237. ConvertProgram;
  19238. CheckSource('TestExternalClass_ConstructorBrackets',
  19239. LinesToStr([ // statements
  19240. 'this.A = null;',
  19241. '']),
  19242. LinesToStr([ // $mod.$main
  19243. '$mod.A = {};',
  19244. '$mod.A = {};',
  19245. '$mod.A = {};',
  19246. '$mod.A = {};',
  19247. '$mod.A = {};',
  19248. '$mod.A = {};',
  19249. '$mod.A = {};',
  19250. '']));
  19251. end;
  19252. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19253. begin
  19254. StartProgram(false);
  19255. Add('{$modeswitch externalclass}');
  19256. Add('type');
  19257. Add(' TExtA = class external name ''ExtA''');
  19258. Add(' constructor New;');
  19259. Add(' end;');
  19260. Add('function DoIt: longint;');
  19261. Add('const ExtA: longint = 3;');
  19262. Add('begin');
  19263. Add(' Result:=ExtA;');
  19264. Add('end;');
  19265. Add('var');
  19266. Add(' A: texta;');
  19267. Add('begin');
  19268. Add(' a:=texta.new;');
  19269. ConvertProgram;
  19270. CheckSource('TestExternalClass_LocalConstSameName',
  19271. LinesToStr([ // statements
  19272. 'var ExtA$1 = 3;',
  19273. 'this.DoIt = function () {',
  19274. ' var Result = 0;',
  19275. ' Result = ExtA$1;',
  19276. ' return Result;',
  19277. '};',
  19278. 'this.A = null;',
  19279. '']),
  19280. LinesToStr([ // $mod.$main
  19281. '$mod.A = new ExtA();',
  19282. '']));
  19283. end;
  19284. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19285. begin
  19286. StartProgram(false);
  19287. Add('{$modeswitch externalclass}');
  19288. Add('type');
  19289. Add(' TExtA = class external name ''ExtA''');
  19290. Add(' procedure DoIt;');
  19291. Add(' end;');
  19292. Add(' TMyA = class(TExtA)');
  19293. Add(' procedure DoIt;');
  19294. Add(' end;');
  19295. Add('procedure TMyA.DoIt; begin end;');
  19296. Add('begin');
  19297. ConvertProgram;
  19298. CheckSource('TestExternalClass_ReintroduceOverload',
  19299. LinesToStr([ // statements
  19300. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19301. ' this.$init = function () {',
  19302. ' };',
  19303. ' this.$final = function () {',
  19304. ' };',
  19305. ' this.DoIt$1 = function () {',
  19306. ' };',
  19307. '});',
  19308. '']),
  19309. LinesToStr([ // $mod.$main
  19310. '']));
  19311. end;
  19312. procedure TTestModule.TestExternalClass_Inherited;
  19313. begin
  19314. StartProgram(false);
  19315. Add('{$modeswitch externalclass}');
  19316. Add('type');
  19317. Add(' TExtA = class external name ''ExtA''');
  19318. Add(' procedure DoIt(i: longint = 1); virtual;');
  19319. Add(' procedure DoSome(j: longint = 2);');
  19320. Add(' end;');
  19321. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19322. Add(' end;');
  19323. Add(' TMyC = class(TExtB)');
  19324. Add(' procedure DoIt(i: longint = 1); override;');
  19325. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19326. Add(' end;');
  19327. Add('procedure TMyC.DoIt(i: longint);');
  19328. Add('begin');
  19329. Add(' inherited;');
  19330. Add(' inherited DoIt;');
  19331. Add(' inherited DoIt();');
  19332. Add(' inherited DoIt(3);');
  19333. Add(' inherited DoSome;');
  19334. Add(' inherited DoSome();');
  19335. Add(' inherited DoSome(4);');
  19336. Add('end;');
  19337. Add('procedure TMyC.DoSome(j: longint);');
  19338. Add('begin');
  19339. Add(' inherited;');
  19340. Add('end;');
  19341. Add('begin');
  19342. ConvertProgram;
  19343. CheckSource('TestExternalClass_ReintroduceOverload',
  19344. LinesToStr([ // statements
  19345. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19346. ' this.$init = function () {',
  19347. ' };',
  19348. ' this.$final = function () {',
  19349. ' };',
  19350. ' this.DoIt = function (i) {',
  19351. ' ExtB.DoIt.apply(this, arguments);',
  19352. ' ExtB.DoIt.call(this, 1);',
  19353. ' ExtB.DoIt.call(this, 1);',
  19354. ' ExtB.DoIt.call(this, 3);',
  19355. ' ExtB.DoSome.call(this, 2);',
  19356. ' ExtB.DoSome.call(this, 2);',
  19357. ' ExtB.DoSome.call(this, 4);',
  19358. ' };',
  19359. ' this.DoSome$1 = function (j) {',
  19360. ' ExtB.DoSome.apply(this, arguments);',
  19361. ' };',
  19362. '});',
  19363. '']),
  19364. LinesToStr([ // $mod.$main
  19365. '']));
  19366. end;
  19367. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19368. begin
  19369. StartProgram(false);
  19370. Add('{$modeswitch externalclass}');
  19371. Add('type');
  19372. Add(' TObject = class');
  19373. Add(' end;');
  19374. Add(' TExtA = class external name ''ExtA''(TObject)');
  19375. Add(' end;');
  19376. Add('begin');
  19377. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19378. ConvertProgram;
  19379. end;
  19380. procedure TTestModule.TestExternalClass_NewInstance;
  19381. begin
  19382. StartProgram(false);
  19383. Add('{$modeswitch externalclass}');
  19384. Add('type');
  19385. Add(' TExtA = class external name ''ExtA''');
  19386. Add(' end;');
  19387. Add(' TMyB = class(TExtA)');
  19388. Add(' protected');
  19389. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19390. Add(' end;');
  19391. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19392. Add('begin end;');
  19393. Add('begin');
  19394. ConvertProgram;
  19395. CheckSource('TestExternalClass_NewInstance',
  19396. LinesToStr([ // statements
  19397. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19398. ' this.$init = function () {',
  19399. ' };',
  19400. ' this.$final = function () {',
  19401. ' };',
  19402. ' this.NewInstance = function (fnname, paramarray) {',
  19403. ' var Result = null;',
  19404. ' return Result;',
  19405. ' };',
  19406. '});',
  19407. '']),
  19408. LinesToStr([ // $mod.$main
  19409. '']));
  19410. end;
  19411. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19412. begin
  19413. StartProgram(false);
  19414. Add('{$modeswitch externalclass}');
  19415. Add('type');
  19416. Add(' TExtA = class external name ''ExtA''');
  19417. Add(' end;');
  19418. Add(' TMyB = class(TExtA)');
  19419. Add(' protected');
  19420. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19421. Add(' end;');
  19422. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19423. Add('begin end;');
  19424. Add('begin');
  19425. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19426. ConvertProgram;
  19427. end;
  19428. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19429. begin
  19430. StartProgram(false);
  19431. Add('{$modeswitch externalclass}');
  19432. Add('type');
  19433. Add(' TExtA = class external name ''ExtA''');
  19434. Add(' end;');
  19435. Add(' TMyB = class(TExtA)');
  19436. Add(' protected');
  19437. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19438. Add(' end;');
  19439. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19440. Add('begin end;');
  19441. Add('begin');
  19442. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19443. nIncompatibleTypeArgNo);
  19444. ConvertProgram;
  19445. end;
  19446. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19447. begin
  19448. StartProgram(false);
  19449. Add('{$modeswitch externalclass}');
  19450. Add('type');
  19451. Add(' TExtA = class external name ''ExtA''');
  19452. Add(' end;');
  19453. Add(' TMyB = class(TExtA)');
  19454. Add(' protected');
  19455. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19456. Add(' end;');
  19457. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19458. Add('begin end;');
  19459. Add('begin');
  19460. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19461. nIncompatibleTypeArgNo);
  19462. ConvertProgram;
  19463. end;
  19464. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19465. begin
  19466. StartProgram(false);
  19467. Add([
  19468. '{$modeswitch externalclass}',
  19469. 'type',
  19470. ' TJSFunction = class external name ''Function''',
  19471. ' end;',
  19472. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19473. ' constructor New(w: word);',
  19474. ' end;',
  19475. ' TBird = class (TExtA)',
  19476. ' public',
  19477. ' Size: word;',
  19478. ' class var Legs: word;',
  19479. ' constructor Create(a: word);',
  19480. ' end;',
  19481. ' TEagle = class (TBird)',
  19482. ' public',
  19483. ' constructor Create(b: word); reintroduce;',
  19484. ' end;',
  19485. 'constructor TBird.Create(a: word);',
  19486. 'begin',
  19487. ' inherited;', // silently ignored
  19488. ' inherited New(a);', // this.$func(a)
  19489. 'end;',
  19490. 'constructor TEagle.Create(b: word);',
  19491. 'begin',
  19492. ' inherited Create(b);',
  19493. 'end;',
  19494. 'var',
  19495. ' Bird: TBird;',
  19496. ' Eagle: TEagle;',
  19497. 'begin',
  19498. ' Bird:=TBird.Create(3);',
  19499. ' Eagle:=TEagle.Create(4);',
  19500. ' Bird.Size:=Bird.Size+5;',
  19501. ' Bird.Legs:=Bird.Legs+6;',
  19502. ' Eagle.Size:=Eagle.Size+5;',
  19503. ' Eagle.Legs:=Eagle.Legs+6;',
  19504. '']);
  19505. ConvertProgram;
  19506. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19507. LinesToStr([ // statements
  19508. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19509. ' this.Legs = 0;',
  19510. ' this.$init = function () {',
  19511. ' this.Size = 0;',
  19512. ' };',
  19513. ' this.$final = function () {',
  19514. ' };',
  19515. ' this.Create = function (a) {',
  19516. ' this.$ancestorfunc(a);',
  19517. ' return this;',
  19518. ' };',
  19519. '});',
  19520. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19521. ' this.Create$1 = function (b) {',
  19522. ' $mod.TBird.Create.call(this, b);',
  19523. ' return this;',
  19524. ' };',
  19525. '});',
  19526. 'this.Bird = null;',
  19527. 'this.Eagle = null;',
  19528. '']),
  19529. LinesToStr([ // $mod.$main
  19530. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19531. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19532. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19533. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19534. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19535. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19536. '']));
  19537. end;
  19538. procedure TTestModule.TestExternalClass_PascalProperty;
  19539. begin
  19540. StartProgram(false);
  19541. Add('{$modeswitch externalclass}');
  19542. Add('type');
  19543. Add(' TJSElement = class;');
  19544. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19545. Add(' TJSElement = class external name ''ExtA''');
  19546. Add(' end;');
  19547. Add(' TControl = class(TJSElement)');
  19548. Add(' private');
  19549. Add(' FOnClick: TJSNotifyEvent;');
  19550. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19551. Add(' procedure Click(Sender: TJSElement);');
  19552. Add(' end;');
  19553. Add('procedure TControl.Click(Sender: TJSElement);');
  19554. Add('begin');
  19555. Add(' OnClick(Self);');
  19556. Add('end;');
  19557. Add('var');
  19558. Add(' Ctrl: TControl;');
  19559. Add('begin');
  19560. Add(' Ctrl.OnClick:[email protected];');
  19561. Add(' Ctrl.OnClick(Ctrl);');
  19562. ConvertProgram;
  19563. CheckSource('TestExternalClass_PascalProperty',
  19564. LinesToStr([ // statements
  19565. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19566. ' this.$init = function () {',
  19567. ' this.FOnClick = null;',
  19568. ' };',
  19569. ' this.$final = function () {',
  19570. ' this.FOnClick = undefined;',
  19571. ' };',
  19572. ' this.Click = function (Sender) {',
  19573. ' this.FOnClick(this);',
  19574. ' };',
  19575. '});',
  19576. 'this.Ctrl = null;',
  19577. '']),
  19578. LinesToStr([ // $mod.$main
  19579. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19580. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19581. '']));
  19582. end;
  19583. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19584. begin
  19585. StartProgram(false);
  19586. Add([
  19587. '{$modeswitch externalclass}',
  19588. 'type',
  19589. ' IUnknown = interface end;',
  19590. ' TObject = class',
  19591. ' end;',
  19592. ' TChild = class',
  19593. ' end;',
  19594. ' TExtRootA = class external name ''ExtRootA''',
  19595. ' end;',
  19596. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19597. ' end;',
  19598. ' TExtRootB = class external name ''ExtRootB''',
  19599. ' end;',
  19600. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19601. ' end;',
  19602. ' TExtString = class external name ''String''',
  19603. ' function charAt(aIndex : NativeInt) : string;',
  19604. ' end;',
  19605. 'var',
  19606. ' Obj: TObject;',
  19607. ' Child: TChild;',
  19608. ' RootA: TExtRootA;',
  19609. ' ChildA: TExtChildA;',
  19610. ' RootB: TExtRootB;',
  19611. ' ChildB: TExtChildB;',
  19612. ' i: IUnknown;',
  19613. ' s: string;',
  19614. ' v: jsvalue;',
  19615. 'begin',
  19616. ' obj:=tobject(roota);',
  19617. ' obj:=tobject(childa);',
  19618. ' child:=tchild(tobject(roota));',
  19619. ' roota:=textroota(obj);',
  19620. ' roota:=textroota(child);',
  19621. ' roota:=textroota(rootb);',
  19622. ' roota:=textroota(childb);',
  19623. ' childa:=textchilda(textroota(obj));',
  19624. ' roota:=TExtRootA(i);',
  19625. ' s:=TExtString(s).charAt(7);',
  19626. ' s:=TExtString(v).charAt(8);',
  19627. '']);
  19628. ConvertProgram;
  19629. CheckSource('TestExternalClass_TypeCastToRootClass',
  19630. LinesToStr([ // statements
  19631. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19632. 'rtl.createClass(this, "TObject", null, function () {',
  19633. ' this.$init = function () {',
  19634. ' };',
  19635. ' this.$final = function () {',
  19636. ' };',
  19637. '});',
  19638. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19639. '});',
  19640. 'this.Obj = null;',
  19641. 'this.Child = null;',
  19642. 'this.RootA = null;',
  19643. 'this.ChildA = null;',
  19644. 'this.RootB = null;',
  19645. 'this.ChildB = null;',
  19646. 'this.i = null;',
  19647. 'this.s = "";',
  19648. 'this.v = undefined;',
  19649. '']),
  19650. LinesToStr([ // $mod.$main
  19651. '$mod.Obj = $mod.RootA;',
  19652. '$mod.Obj = $mod.ChildA;',
  19653. '$mod.Child = $mod.RootA;',
  19654. '$mod.RootA = $mod.Obj;',
  19655. '$mod.RootA = $mod.Child;',
  19656. '$mod.RootA = $mod.RootB;',
  19657. '$mod.RootA = $mod.ChildB;',
  19658. '$mod.ChildA = $mod.Obj;',
  19659. '$mod.RootA = $mod.i;',
  19660. '$mod.s = $mod.s.charAt(7);',
  19661. '$mod.s = $mod.v.charAt(8);',
  19662. '']));
  19663. end;
  19664. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19665. begin
  19666. StartProgram(false);
  19667. Add([
  19668. '{$modeswitch externalclass}',
  19669. 'type',
  19670. ' IUnknown = interface end;',
  19671. ' IBird = interface(IUnknown) end;',
  19672. ' TClass = class of TObject;',
  19673. ' TObject = class',
  19674. ' end;',
  19675. ' TChild = class',
  19676. ' end;',
  19677. ' TJSObject = class external name ''Object''',
  19678. ' end;',
  19679. ' TRec = record end;',
  19680. 'var',
  19681. ' Obj: TObject;',
  19682. ' Child: TChild;',
  19683. ' i: IUnknown;',
  19684. ' Bird: IBird;',
  19685. ' j: TJSObject;',
  19686. ' r: TRec;',
  19687. ' c: TClass;',
  19688. 'begin',
  19689. ' j:=tjsobject(IUnknown);',
  19690. ' j:=tjsobject(IBird);',
  19691. ' j:=tjsobject(TObject);',
  19692. ' j:=tjsobject(TChild);',
  19693. ' j:=tjsobject(TRec);',
  19694. ' j:=tjsobject(Obj);',
  19695. ' j:=tjsobject(Child);',
  19696. ' j:=tjsobject(i);',
  19697. ' j:=tjsobject(Bird);',
  19698. ' j:=tjsobject(r);',
  19699. ' j:=tjsobject(c);',
  19700. '']);
  19701. ConvertProgram;
  19702. CheckSource('TestExternalClass_TypeCastToJSObject',
  19703. LinesToStr([ // statements
  19704. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19705. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19706. 'rtl.createClass(this, "TObject", null, function () {',
  19707. ' this.$init = function () {',
  19708. ' };',
  19709. ' this.$final = function () {',
  19710. ' };',
  19711. '});',
  19712. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19713. '});',
  19714. 'rtl.recNewT(this, "TRec", function () {',
  19715. ' this.$eq = function (b) {',
  19716. ' return true;',
  19717. ' };',
  19718. ' this.$assign = function (s) {',
  19719. ' return this;',
  19720. ' };',
  19721. '});',
  19722. 'this.Obj = null;',
  19723. 'this.Child = null;',
  19724. 'this.i = null;',
  19725. 'this.Bird = null;',
  19726. 'this.j = null;',
  19727. 'this.r = this.TRec.$new();',
  19728. 'this.c = null;',
  19729. '']),
  19730. LinesToStr([ // $mod.$main
  19731. '$mod.j = $mod.IUnknown;',
  19732. '$mod.j = $mod.IBird;',
  19733. '$mod.j = $mod.TObject;',
  19734. '$mod.j = $mod.TChild;',
  19735. '$mod.j = $mod.TRec;',
  19736. '$mod.j = $mod.Obj;',
  19737. '$mod.j = $mod.Child;',
  19738. '$mod.j = $mod.i;',
  19739. '$mod.j = $mod.Bird;',
  19740. '$mod.j = $mod.r;',
  19741. '$mod.j = $mod.c;',
  19742. '']));
  19743. end;
  19744. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19745. begin
  19746. StartProgram(false);
  19747. Add('{$modeswitch externalclass}');
  19748. Add('type');
  19749. Add(' TJSString = class external name ''String''');
  19750. Add(' class function fromCharCode() : string; varargs;');
  19751. Add(' function anchor(const aName : string) : string;');
  19752. Add(' end;');
  19753. Add('var');
  19754. Add(' s: string;');
  19755. Add('begin');
  19756. Add(' s:=TJSString.fromCharCode(65,66);');
  19757. Add(' s:=TJSString(s).anchor(s);');
  19758. Add(' s:=TJSString(''foo'').anchor(s);');
  19759. ConvertProgram;
  19760. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19761. LinesToStr([ // statements
  19762. 'this.s = "";',
  19763. '']),
  19764. LinesToStr([ // $mod.$main
  19765. '$mod.s = String.fromCharCode(65, 66);',
  19766. '$mod.s = $mod.s.anchor($mod.s);',
  19767. '$mod.s = "foo".anchor($mod.s);',
  19768. '']));
  19769. end;
  19770. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19771. begin
  19772. StartProgram(false);
  19773. Add([
  19774. '{$modeswitch externalclass}',
  19775. 'type',
  19776. ' TJSObject = class external name ''Object'' end;',
  19777. ' TJSFunction = class external name ''Function''',
  19778. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19779. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19780. ' end;',
  19781. ' TObject = class',
  19782. ' procedure DoIt(i: longint);',
  19783. ' end;',
  19784. ' TFuncInt = function(o: TObject): longint;',
  19785. 'function GetIt(o: TObject): longint;',
  19786. ' procedure Sub; begin end;',
  19787. 'var',
  19788. ' f: TJSFunction;',
  19789. ' fi: TFuncInt;',
  19790. 'begin',
  19791. ' fi:=TFuncInt(f);',
  19792. ' f:=TJSFunction(fi);',
  19793. ' f:=TJSFunction(@GetIt);',
  19794. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19795. ' f:=TJSFunction(@Sub);',
  19796. ' f:=TJSFunction(@o.doit);',
  19797. ' f:=TJSFunction(fi).bind(nil,4)',
  19798. 'end;',
  19799. 'procedure TObject.DoIt(i: longint);',
  19800. ' procedure Sub; begin end;',
  19801. 'var f: TJSFunction;',
  19802. 'begin',
  19803. ' f:=TJSFunction(@DoIt);',
  19804. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19805. ' f:=TJSFunction(@Sub);',
  19806. ' f:=TJSFunction(@GetIt);',
  19807. 'end;',
  19808. 'begin']);
  19809. ConvertProgram;
  19810. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19811. LinesToStr([ // statements
  19812. 'rtl.createClass(this, "TObject", null, function () {',
  19813. ' this.$init = function () {',
  19814. ' };',
  19815. ' this.$final = function () {',
  19816. ' };',
  19817. ' this.DoIt = function (i) {',
  19818. ' var $Self = this;',
  19819. ' function Sub() {',
  19820. ' };',
  19821. ' var f = null;',
  19822. ' f = this.DoIt;',
  19823. ' f = this.DoIt.bind(null, 13);',
  19824. ' f = Sub;',
  19825. ' f = $mod.GetIt;',
  19826. ' };',
  19827. '});',
  19828. 'this.GetIt = function (o) {',
  19829. ' var Result = 0;',
  19830. ' function Sub() {',
  19831. ' };',
  19832. ' var f = null;',
  19833. ' var fi = null;',
  19834. ' fi = f;',
  19835. ' f = fi;',
  19836. ' f = $mod.GetIt;',
  19837. ' f = $mod.GetIt.bind(null, 3);',
  19838. ' f = Sub;',
  19839. ' f = $mod.TObject.DoIt;',
  19840. ' f = fi.bind(null, 4);',
  19841. ' return Result;',
  19842. '};',
  19843. '']),
  19844. LinesToStr([ // $mod.$main
  19845. '']));
  19846. end;
  19847. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19848. begin
  19849. StartProgram(false);
  19850. Add([
  19851. '{$mode delphi}',
  19852. '{$modeswitch externalclass}',
  19853. 'type',
  19854. ' TJSObject = class external name ''Object'' end;',
  19855. ' TJSWindow = class external name ''Window''(TJSObject)',
  19856. ' procedure Open;',
  19857. ' end;',
  19858. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19859. ' procedure Execute;',
  19860. ' end;',
  19861. 'procedure Fly;',
  19862. 'var',
  19863. ' w: TJSWindow;',
  19864. ' e: TJSEventTarget;',
  19865. 'begin',
  19866. ' w:=TJSWindow(e);',
  19867. ' e:=TJSEventTarget(w);',
  19868. 'end;',
  19869. 'begin']);
  19870. ConvertProgram;
  19871. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19872. LinesToStr([ // statements
  19873. 'this.Fly = function () {',
  19874. ' var w = null;',
  19875. ' var e = null;',
  19876. ' w = e;',
  19877. ' e = w;',
  19878. '};',
  19879. '']),
  19880. LinesToStr([ // $mod.$main
  19881. '']));
  19882. end;
  19883. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19884. begin
  19885. StartProgram(false);
  19886. Add('{$modeswitch externalclass}');
  19887. Add('type');
  19888. Add(' TJSString = class external name ''String''');
  19889. Add(' class function fromCharCode() : string; varargs;');
  19890. Add(' end;');
  19891. Add('var');
  19892. Add(' s: string;');
  19893. Add(' sObj: TJSString;');
  19894. Add('begin');
  19895. Add(' s:=sObj.fromCharCode(65,66);');
  19896. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19897. nExternalClassInstanceCannotAccessStaticX);
  19898. ConvertProgram;
  19899. end;
  19900. procedure TTestModule.TestExternalClass_BracketAccessor;
  19901. begin
  19902. StartProgram(false);
  19903. Add([
  19904. '{$modeswitch externalclass}',
  19905. 'type',
  19906. ' TJSArray = class external name ''Array2''',
  19907. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19908. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19909. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19910. ' end;',
  19911. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19912. 'begin end;',
  19913. 'var',
  19914. ' Arr: tjsarray;',
  19915. ' s: string;',
  19916. ' i: longint;',
  19917. ' v: jsvalue;',
  19918. 'begin',
  19919. ' v:=arr[0];',
  19920. ' v:=arr.items[1];',
  19921. ' arr[2]:=s;',
  19922. ' arr.items[3]:=s;',
  19923. ' arr[4]:=i;',
  19924. ' arr[5]:=arr[6];',
  19925. ' arr.items[7]:=arr.items[8];',
  19926. ' with arr do items[9]:=items[10];',
  19927. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19928. ' with arr do begin',
  19929. ' v:=GetItems(14);',
  19930. ' setitems(15,16);',
  19931. ' end;',
  19932. ' v:=test1.arr.items[17];',
  19933. ' test1.arr.items[18]:=v;',
  19934. '']);
  19935. ConvertProgram;
  19936. CheckSource('TestExternalClass_BracketAccessor',
  19937. LinesToStr([ // statements
  19938. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19939. '};',
  19940. 'this.Arr = null;',
  19941. 'this.s = "";',
  19942. 'this.i = 0;',
  19943. 'this.v = undefined;',
  19944. '']),
  19945. LinesToStr([ // $mod.$main
  19946. '$mod.v = $mod.Arr[0];',
  19947. '$mod.v = $mod.Arr[1];',
  19948. '$mod.Arr[2] = $mod.s;',
  19949. '$mod.Arr[3] = $mod.s;',
  19950. '$mod.Arr[4] = $mod.i;',
  19951. '$mod.Arr[5] = $mod.Arr[6];',
  19952. '$mod.Arr[7] = $mod.Arr[8];',
  19953. 'var $with = $mod.Arr;',
  19954. '$with[9] = $with[10];',
  19955. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19956. ' a: 9,',
  19957. ' p: $mod.Arr,',
  19958. ' get: function () {',
  19959. ' return this.p[this.a];',
  19960. ' },',
  19961. ' set: function (v) {',
  19962. ' this.p[this.a] = v;',
  19963. ' }',
  19964. '}, {',
  19965. ' a: 10,',
  19966. ' p: $mod.Arr,',
  19967. ' get: function () {',
  19968. ' return this.p[this.a];',
  19969. ' },',
  19970. ' set: function (v) {',
  19971. ' this.p[this.a] = v;',
  19972. ' }',
  19973. '});',
  19974. 'var $with1 = $mod.Arr;',
  19975. '$mod.v = $with1[14];',
  19976. '$with1[15] = 16;',
  19977. '$mod.v = $mod.Arr[17];',
  19978. '$mod.Arr[18] = $mod.v;',
  19979. '']));
  19980. end;
  19981. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19982. begin
  19983. StartProgram(false);
  19984. Add([
  19985. '{$modeswitch externalclass}',
  19986. 'type',
  19987. ' TJSArray = class external name ''Array2''',
  19988. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19989. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19990. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19991. ' end;',
  19992. ' TMyArr = class(TJSArray)',
  19993. ' procedure DoIt;',
  19994. ' end;',
  19995. 'procedure tmyarr.DoIt;',
  19996. 'begin',
  19997. ' Items[1]:=Items[2];',
  19998. ' SetItems(3,getItems(4));',
  19999. 'end;',
  20000. 'var',
  20001. ' Arr: tmyarr;',
  20002. ' s: string;',
  20003. ' i: longint;',
  20004. ' v: jsvalue;',
  20005. 'begin',
  20006. ' v:=arr[0];',
  20007. ' v:=arr.items[1];',
  20008. ' arr[2]:=s;',
  20009. ' arr.items[3]:=s;',
  20010. ' arr[4]:=i;',
  20011. ' arr[5]:=arr[6];',
  20012. ' arr.items[7]:=arr.items[8];',
  20013. ' with arr do items[9]:=items[10];',
  20014. ' with arr do begin',
  20015. ' v:=GetItems(14);',
  20016. ' setitems(15,16);',
  20017. ' end;',
  20018. '']);
  20019. ConvertProgram;
  20020. CheckSource('TestExternalClass_BracketAccessor_Call',
  20021. LinesToStr([ // statements
  20022. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  20023. ' this.$init = function () {',
  20024. ' };',
  20025. ' this.$final = function () {',
  20026. ' };',
  20027. ' this.DoIt = function () {',
  20028. ' this[1] = this[2];',
  20029. ' this[3] = this[4];',
  20030. ' };',
  20031. '});',
  20032. 'this.Arr = null;',
  20033. 'this.s = "";',
  20034. 'this.i = 0;',
  20035. 'this.v = undefined;',
  20036. '']),
  20037. LinesToStr([ // $mod.$main
  20038. '$mod.v = $mod.Arr[0];',
  20039. '$mod.v = $mod.Arr[1];',
  20040. '$mod.Arr[2] = $mod.s;',
  20041. '$mod.Arr[3] = $mod.s;',
  20042. '$mod.Arr[4] = $mod.i;',
  20043. '$mod.Arr[5] = $mod.Arr[6];',
  20044. '$mod.Arr[7] = $mod.Arr[8];',
  20045. 'var $with = $mod.Arr;',
  20046. '$with[9] = $with[10];',
  20047. 'var $with1 = $mod.Arr;',
  20048. '$mod.v = $with1[14];',
  20049. '$with1[15] = 16;',
  20050. '']));
  20051. end;
  20052. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  20053. begin
  20054. StartProgram(false);
  20055. Add('{$modeswitch externalclass}');
  20056. Add('type');
  20057. Add(' TJSArray = class external name ''Array2''');
  20058. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  20059. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  20060. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  20061. Add(' end;');
  20062. Add('begin');
  20063. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  20064. nBracketAccessorOfExternalClassMustHaveOneParameter);
  20065. ConvertProgram;
  20066. end;
  20067. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  20068. begin
  20069. StartProgram(false);
  20070. Add('{$modeswitch externalclass}');
  20071. Add('type');
  20072. Add(' TJSArray = class external name ''Array2''');
  20073. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20074. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  20075. Add(' end;');
  20076. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  20077. Add('begin end;');
  20078. Add('var');
  20079. Add(' Arr: tjsarray;');
  20080. Add(' v: jsvalue;');
  20081. Add('begin');
  20082. Add(' v:=arr[0];');
  20083. Add(' v:=arr.items[1];');
  20084. Add(' with arr do v:=items[2];');
  20085. Add(' doit(arr[3],arr[4]);');
  20086. ConvertProgram;
  20087. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  20088. LinesToStr([ // statements
  20089. 'this.DoIt = function (vI, vJ) {',
  20090. '};',
  20091. 'this.Arr = null;',
  20092. 'this.v = undefined;',
  20093. '']),
  20094. LinesToStr([ // $mod.$main
  20095. '$mod.v = $mod.Arr[0];',
  20096. '$mod.v = $mod.Arr[1];',
  20097. 'var $with = $mod.Arr;',
  20098. '$mod.v = $with[2];',
  20099. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20100. '']));
  20101. end;
  20102. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20103. begin
  20104. StartProgram(false);
  20105. Add('{$modeswitch externalclass}');
  20106. Add('type');
  20107. Add(' TJSArray = class external name ''Array2''');
  20108. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20109. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20110. Add(' end;');
  20111. Add('var');
  20112. Add(' Arr: tjsarray;');
  20113. Add(' s: string;');
  20114. Add(' i: longint;');
  20115. Add(' v: jsvalue;');
  20116. Add('begin');
  20117. Add(' arr[2]:=s;');
  20118. Add(' arr.items[3]:=s;');
  20119. Add(' arr[4]:=i;');
  20120. Add(' with arr do items[5]:=i;');
  20121. ConvertProgram;
  20122. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20123. LinesToStr([ // statements
  20124. 'this.Arr = null;',
  20125. 'this.s = "";',
  20126. 'this.i = 0;',
  20127. 'this.v = undefined;',
  20128. '']),
  20129. LinesToStr([ // $mod.$main
  20130. '$mod.Arr[2] = $mod.s;',
  20131. '$mod.Arr[3] = $mod.s;',
  20132. '$mod.Arr[4] = $mod.i;',
  20133. 'var $with = $mod.Arr;',
  20134. '$with[5] = $mod.i;',
  20135. '']));
  20136. end;
  20137. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20138. begin
  20139. StartProgram(false);
  20140. Add('{$modeswitch externalclass}');
  20141. Add('type');
  20142. Add(' TJSArray = class external name ''Array2''');
  20143. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20144. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20145. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20146. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20147. Add(' end;');
  20148. Add('var');
  20149. Add(' Arr: tjsarray;');
  20150. Add(' s: string;');
  20151. Add(' i: longint;');
  20152. Add(' v: jsvalue;');
  20153. Add('begin');
  20154. Add(' arr[2]:=s;');
  20155. Add(' arr.items[3]:=s;');
  20156. Add(' arr.numbers[4]:=i;');
  20157. Add(' with arr do items[5]:=i;');
  20158. Add(' with arr do numbers[6]:=i;');
  20159. ConvertProgram;
  20160. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20161. LinesToStr([ // statements
  20162. 'this.Arr = null;',
  20163. 'this.s = "";',
  20164. 'this.i = 0;',
  20165. 'this.v = undefined;',
  20166. '']),
  20167. LinesToStr([ // $mod.$main
  20168. '$mod.Arr[2] = $mod.s;',
  20169. '$mod.Arr[3] = $mod.s;',
  20170. '$mod.Arr[4] = $mod.i;',
  20171. 'var $with = $mod.Arr;',
  20172. '$with[5] = $mod.i;',
  20173. 'var $with1 = $mod.Arr;',
  20174. '$with1[6] = $mod.i;',
  20175. '']));
  20176. end;
  20177. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20178. begin
  20179. StartProgram(false);
  20180. Add('{$modeswitch externalclass}');
  20181. Add('type');
  20182. Add(' TJSArray = class external name ''Array2''');
  20183. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20184. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20185. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20186. Add(' end;');
  20187. Add('var');
  20188. Add(' Arr: tjsarray;');
  20189. Add(' i: longint;');
  20190. Add(' IntArr: array of longint;');
  20191. Add(' v: jsvalue;');
  20192. Add('begin');
  20193. Add(' v:=arr.items[i];');
  20194. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20195. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20196. ConvertProgram;
  20197. CheckSource('TestExternalClass_BracketAccessor_Index',
  20198. LinesToStr([ // statements
  20199. 'this.Arr = null;',
  20200. 'this.i = 0;',
  20201. 'this.IntArr = [];',
  20202. 'this.v = undefined;',
  20203. '']),
  20204. LinesToStr([ // $mod.$main
  20205. '$mod.v = $mod.Arr[$mod.i];',
  20206. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20207. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20208. '']));
  20209. end;
  20210. procedure TTestModule.TestExternalClass_ForInJSObject;
  20211. begin
  20212. StartProgram(false);
  20213. Add([
  20214. '{$modeswitch externalclass}',
  20215. 'type',
  20216. ' TJSObject = class external name ''Object''',
  20217. ' end;',
  20218. 'var',
  20219. ' o: TJSObject;',
  20220. ' key: string;',
  20221. 'begin',
  20222. ' for key in o do',
  20223. ' if key=''abc'' then ;',
  20224. '']);
  20225. ConvertProgram;
  20226. CheckSource('TestExternalClass_ForInJSObject',
  20227. LinesToStr([ // statements
  20228. 'this.o = null;',
  20229. 'this.key = "";',
  20230. '']),
  20231. LinesToStr([ // $mod.$main
  20232. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20233. '']));
  20234. end;
  20235. procedure TTestModule.TestExternalClass_ForInJSArray;
  20236. begin
  20237. StartProgram(false);
  20238. Add([
  20239. '{$modeswitch externalclass}',
  20240. 'type',
  20241. ' TJSInt8Array = class external name ''Int8Array''',
  20242. ' private',
  20243. ' flength: NativeInt external name ''length'';',
  20244. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20245. ' public',
  20246. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20247. ' property Length: NativeInt read flength;',
  20248. ' end;',
  20249. 'var',
  20250. ' a: TJSInt8Array;',
  20251. ' value: shortint;',
  20252. 'begin',
  20253. ' for value in a do',
  20254. ' if value=3 then ;',
  20255. '']);
  20256. ConvertProgram;
  20257. CheckSource('TestExternalClass_ForInJSArray',
  20258. LinesToStr([ // statements
  20259. 'this.a = null;',
  20260. 'this.value = 0;',
  20261. '']),
  20262. LinesToStr([ // $mod.$main
  20263. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20264. ' $mod.value = $in[$l];',
  20265. ' if ($mod.value === 3) ;',
  20266. '};',
  20267. '']));
  20268. end;
  20269. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20270. begin
  20271. AddModuleWithIntfImplSrc('unit2.pas',
  20272. LinesToStr([
  20273. '{$modeswitch externalclass}',
  20274. 'type',
  20275. ' TJSBufferSource = class external name ''BufferSource''',
  20276. ' end;',
  20277. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20278. '']),
  20279. '');
  20280. AddModuleWithIntfImplSrc('unit3.pas',
  20281. LinesToStr([
  20282. '{$modeswitch externalclass}',
  20283. 'type',
  20284. ' TJSBufferSource = class external name ''BufferSource''',
  20285. ' end;',
  20286. '']),
  20287. '');
  20288. StartUnit(true);
  20289. Add([
  20290. 'interface',
  20291. 'uses unit2, unit3;',
  20292. 'procedure DoSome(s: TJSBufferSource);',
  20293. 'implementation',
  20294. 'procedure DoSome(s: TJSBufferSource);',
  20295. 'begin',
  20296. ' DoIt(s);',
  20297. 'end;',
  20298. '']);
  20299. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20300. nIncompatibleTypeArgNo);
  20301. ConvertUnit;
  20302. end;
  20303. procedure TTestModule.TestExternalClass_NestedConstructor;
  20304. begin
  20305. StartProgram(false);
  20306. Add([
  20307. '{$modeswitch externalclass}',
  20308. 'type',
  20309. ' TJSObject = class external name ''Object''',
  20310. ' type TBird = class external name ''Bird''',
  20311. ' type TWing = class external name ''Wing''',
  20312. ' constructor New;',
  20313. ' constructor Create(w: word = 3);',
  20314. ' end;',
  20315. ' end;',
  20316. ' end;',
  20317. 'var',
  20318. ' w: TJSObject.TBird.TWing;',
  20319. 'begin',
  20320. ' w:=tjsobject.tbird.twing.new;',
  20321. ' w:=tjsobject.tbird.twing.new();',
  20322. ' w:=tjsobject.tbird.twing.create;',
  20323. ' w:=tjsobject.tbird.twing.create(4);',
  20324. ' with tjsobject do begin',
  20325. ' w:=tbird.twing.new;',
  20326. ' w:=tbird.twing.new();',
  20327. ' w:=tbird.twing.create;',
  20328. ' w:=tbird.twing.create(11);',
  20329. ' end;',
  20330. ' with tjsobject.tbird do begin',
  20331. ' w:=twing.new;',
  20332. ' w:=twing.new();',
  20333. ' w:=twing.create;',
  20334. ' w:=twing.create(21);',
  20335. ' end;',
  20336. ' with tjsobject.tbird.twing do begin',
  20337. ' w:=new;',
  20338. ' w:=new();',
  20339. ' w:=create;',
  20340. ' w:=create(31);',
  20341. ' end;',
  20342. '']);
  20343. ConvertProgram;
  20344. CheckSource('TestExternalClass_NestedConstructor',
  20345. LinesToStr([ // statements
  20346. 'this.w = null;',
  20347. '']),
  20348. LinesToStr([ // $mod.$main
  20349. '$mod.w = new Object.Bird.Wing();',
  20350. '$mod.w = new Object.Bird.Wing();',
  20351. '$mod.w = new Object.Bird.Wing.Create();',
  20352. '$mod.w = new Object.Bird.Wing.Create(4);',
  20353. '$mod.w = new Object.Bird.Wing();',
  20354. '$mod.w = new Object.Bird.Wing();',
  20355. '$mod.w = new Object.Bird.Wing.Create();',
  20356. '$mod.w = new Object.Bird.Wing.Create(11);',
  20357. 'var $with = Object.Bird;',
  20358. '$mod.w = new Object.Bird.Wing();',
  20359. '$mod.w = new Object.Bird.Wing();',
  20360. '$mod.w = new Object.Bird.Wing.Create();',
  20361. '$mod.w = new Object.Bird.Wing.Create(21);',
  20362. 'var $with1 = Object.Bird.Wing;',
  20363. '$mod.w = new $with1();',
  20364. '$mod.w = new $with1();',
  20365. '$mod.w = new Object.Bird.Wing.Create();',
  20366. '$mod.w = new Object.Bird.Wing.Create(31);',
  20367. '']));
  20368. end;
  20369. procedure TTestModule.TestClassInterface_Corba;
  20370. begin
  20371. StartProgram(false);
  20372. Add([
  20373. '{$interfaces corba}',
  20374. 'type',
  20375. ' IUnknown = interface;',
  20376. ' IUnknown = interface',
  20377. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20378. ' end;',
  20379. ' IInterface = IUnknown;',
  20380. ' IBird = interface(IInterface)',
  20381. ' function GetSize: longint;',
  20382. ' procedure SetSize(i: longint);',
  20383. ' property Size: longint read GetSize write SetSize;',
  20384. ' procedure DoIt(i: longint);',
  20385. ' end;',
  20386. ' TObject = class',
  20387. ' end;',
  20388. ' TBird = class(TObject,IBird)',
  20389. ' function GetSize: longint; virtual; abstract;',
  20390. ' procedure SetSize(i: longint); virtual; abstract;',
  20391. ' procedure DoIt(i: longint); virtual; abstract;',
  20392. ' end;',
  20393. 'var',
  20394. ' BirdIntf: IBird;',
  20395. 'begin',
  20396. ' BirdIntf.Size:=BirdIntf.Size;',
  20397. '']);
  20398. ConvertProgram;
  20399. CheckSource('TestClassInterface_Corba',
  20400. LinesToStr([ // statements
  20401. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20402. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20403. 'rtl.createClass(this, "TObject", null, function () {',
  20404. ' this.$init = function () {',
  20405. ' };',
  20406. ' this.$final = function () {',
  20407. ' };',
  20408. '});',
  20409. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20410. ' rtl.addIntf(this, $mod.IBird);',
  20411. '});',
  20412. 'this.BirdIntf = null;',
  20413. '']),
  20414. LinesToStr([ // $mod.$main
  20415. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20416. '']));
  20417. end;
  20418. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20419. begin
  20420. StartProgram(false);
  20421. Add([
  20422. '{$interfaces corba}',
  20423. 'type',
  20424. ' IUnknown = interface',
  20425. ' procedure DoIt; external name ''foo'';',
  20426. ' end;',
  20427. 'begin']);
  20428. SetExpectedParserError(
  20429. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20430. nParserNoFieldsAllowed);
  20431. ConvertProgram;
  20432. end;
  20433. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20434. begin
  20435. StartProgram(false);
  20436. Add([
  20437. '{$interfaces corba}',
  20438. 'type',
  20439. ' integer = longint;',
  20440. ' IUnknown = interface',
  20441. ' procedure DoIt(i: integer);',
  20442. ' procedure DoIt(s: string);',
  20443. ' end;',
  20444. ' IBird = interface(IUnknown)',
  20445. ' procedure DoIt(b: boolean); overload;',
  20446. ' end;',
  20447. ' TObject = class',
  20448. ' end;',
  20449. ' TBird = class(TObject,IBird)',
  20450. ' procedure DoIt(o: TObject);',
  20451. ' procedure DoIt(s: string);',
  20452. ' procedure DoIt(i: integer);',
  20453. ' procedure DoIt(b: boolean);',
  20454. ' end;',
  20455. 'procedure TBird.DoIt(o: TObject); begin end;',
  20456. 'procedure TBird.DoIt(s: string); begin end;',
  20457. 'procedure TBird.DoIt(i: integer); begin end;',
  20458. 'procedure TBird.DoIt(b: boolean); begin end;',
  20459. 'var',
  20460. ' BirdIntf: IBird;',
  20461. 'begin',
  20462. ' BirdIntf.DoIt(3);',
  20463. ' BirdIntf.DoIt(''abc'');',
  20464. ' BirdIntf.DoIt(true);',
  20465. '']);
  20466. ConvertProgram;
  20467. CheckSource('TestClassInterface_Overloads',
  20468. LinesToStr([ // statements
  20469. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20470. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20471. 'rtl.createClass(this, "TObject", null, function () {',
  20472. ' this.$init = function () {',
  20473. ' };',
  20474. ' this.$final = function () {',
  20475. ' };',
  20476. '});',
  20477. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20478. ' this.DoIt = function (o) {',
  20479. ' };',
  20480. ' this.DoIt$1 = function (s) {',
  20481. ' };',
  20482. ' this.DoIt$2 = function (i) {',
  20483. ' };',
  20484. ' this.DoIt$3 = function (b) {',
  20485. ' };',
  20486. ' rtl.addIntf(this, $mod.IBird, {',
  20487. ' DoIt$2: "DoIt$3",',
  20488. ' DoIt: "DoIt$2"',
  20489. ' });',
  20490. '});',
  20491. 'this.BirdIntf = null;',
  20492. '']),
  20493. LinesToStr([ // $mod.$main
  20494. '$mod.BirdIntf.DoIt(3);',
  20495. '$mod.BirdIntf.DoIt$1("abc");',
  20496. '$mod.BirdIntf.DoIt$2(true);',
  20497. '']));
  20498. end;
  20499. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20500. begin
  20501. StartProgram(false);
  20502. Add([
  20503. '{$interfaces corba}',
  20504. 'type',
  20505. ' IBird = interface',
  20506. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20507. ' end;',
  20508. ' IDog = interface',
  20509. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20510. ' end;',
  20511. ' TObject = class(IBird,IDog)',
  20512. ' end;',
  20513. 'begin']);
  20514. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20515. nDuplicateGUIDXInYZ);
  20516. ConvertProgram;
  20517. end;
  20518. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20519. begin
  20520. StartProgram(false);
  20521. Add([
  20522. '{$interfaces corba}',
  20523. 'type',
  20524. ' IAnimal = interface',
  20525. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20526. ' end;',
  20527. ' IBird = interface(IAnimal)',
  20528. ' end;',
  20529. ' IHawk = interface(IBird)',
  20530. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20531. ' end;',
  20532. 'begin']);
  20533. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20534. nDuplicateGUIDXInYZ);
  20535. ConvertProgram;
  20536. end;
  20537. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20538. begin
  20539. StartProgram(false);
  20540. Add([
  20541. '{$interfaces corba}',
  20542. 'type',
  20543. ' integer = longint;',
  20544. ' IUnknown = interface',
  20545. ' procedure DoIt(i: integer);',
  20546. ' end;',
  20547. ' IBird = interface',
  20548. ' procedure Fly(i: integer);',
  20549. ' end;',
  20550. ' TObject = class(IUnknown)',
  20551. ' procedure DoIt(i: integer);',
  20552. ' end;',
  20553. ' TBird = class(IBird)',
  20554. ' procedure Fly(i: integer);',
  20555. ' end;',
  20556. 'procedure TObject.DoIt(i: integer); begin end;',
  20557. 'procedure TBird.Fly(i: integer); begin end;',
  20558. 'begin',
  20559. '']);
  20560. ConvertProgram;
  20561. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20562. LinesToStr([ // statements
  20563. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20564. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20565. 'rtl.createClass(this, "TObject", null, function () {',
  20566. ' this.$init = function () {',
  20567. ' };',
  20568. ' this.$final = function () {',
  20569. ' };',
  20570. ' this.DoIt = function (i) {',
  20571. ' };',
  20572. ' rtl.addIntf(this, $mod.IUnknown);',
  20573. '});',
  20574. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20575. ' this.Fly = function (i) {',
  20576. ' };',
  20577. ' rtl.addIntf(this, $mod.IBird);',
  20578. ' rtl.addIntf(this, $mod.IUnknown);',
  20579. '});',
  20580. '']),
  20581. LinesToStr([ // $mod.$main
  20582. '']));
  20583. end;
  20584. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20585. begin
  20586. StartProgram(false);
  20587. Add([
  20588. '{$interfaces corba}',
  20589. 'type',
  20590. ' integer = longint;',
  20591. ' IBird = interface',
  20592. ' procedure DoIt(i: integer);',
  20593. ' end;',
  20594. ' TObject = class',
  20595. ' procedure DoIt(i: integer);',
  20596. ' end;',
  20597. ' TBird = class(IBird)',
  20598. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20599. ' end;',
  20600. 'procedure TObject.DoIt(i: integer); begin end;',
  20601. 'procedure TBird.DoIt(i: integer); begin end;',
  20602. 'begin',
  20603. '']);
  20604. ConvertProgram;
  20605. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20606. LinesToStr([ // statements
  20607. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20608. 'rtl.createClass(this, "TObject", null, function () {',
  20609. ' this.$init = function () {',
  20610. ' };',
  20611. ' this.$final = function () {',
  20612. ' };',
  20613. ' this.DoIt = function (i) {',
  20614. ' };',
  20615. '});',
  20616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20617. ' this.DoIt$1 = function (i) {',
  20618. ' };',
  20619. ' rtl.addIntf(this, $mod.IBird, {',
  20620. ' DoIt: "DoIt$1"',
  20621. ' });',
  20622. '});',
  20623. '']),
  20624. LinesToStr([ // $mod.$main
  20625. '']));
  20626. end;
  20627. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20628. begin
  20629. StartProgram(false);
  20630. Add([
  20631. '{$interfaces corba}',
  20632. 'type',
  20633. ' IUnknown = interface',
  20634. ' procedure Walk(i: longint);',
  20635. ' end;',
  20636. ' IBird = interface(IUnknown)',
  20637. ' procedure Walk(b: boolean); overload;',
  20638. ' procedure Fly(s: string);',
  20639. ' end;',
  20640. ' TObject = class',
  20641. ' end;',
  20642. ' TBird = class(TObject,IBird)',
  20643. ' procedure IBird.Fly = Move;',
  20644. ' procedure IBird.Walk = Hop;',
  20645. ' procedure Hop(i: longint);',
  20646. ' procedure Move(s: string);',
  20647. ' procedure Hop(b: boolean);',
  20648. ' end;',
  20649. 'procedure TBird.Move(s: string); begin end;',
  20650. 'procedure TBird.Hop(i: longint); begin end;',
  20651. 'procedure TBird.Hop(b: boolean); begin end;',
  20652. 'var',
  20653. ' BirdIntf: IBird;',
  20654. 'begin',
  20655. ' BirdIntf.Walk(3);',
  20656. ' BirdIntf.Walk(true);',
  20657. ' BirdIntf.Fly(''abc'');',
  20658. '']);
  20659. ConvertProgram;
  20660. CheckSource('TestClassInterface_Corba_MethodResolution',
  20661. LinesToStr([ // statements
  20662. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20663. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20664. 'rtl.createClass(this, "TObject", null, function () {',
  20665. ' this.$init = function () {',
  20666. ' };',
  20667. ' this.$final = function () {',
  20668. ' };',
  20669. '});',
  20670. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20671. ' this.Hop = function (i) {',
  20672. ' };',
  20673. ' this.Move = function (s) {',
  20674. ' };',
  20675. ' this.Hop$1 = function (b) {',
  20676. ' };',
  20677. ' rtl.addIntf(this, $mod.IBird, {',
  20678. ' Walk$1: "Hop$1",',
  20679. ' Fly: "Move",',
  20680. ' Walk: "Hop"',
  20681. ' });',
  20682. '});',
  20683. 'this.BirdIntf = null;',
  20684. '']),
  20685. LinesToStr([ // $mod.$main
  20686. '$mod.BirdIntf.Walk(3);',
  20687. '$mod.BirdIntf.Walk$1(true);',
  20688. '$mod.BirdIntf.Fly("abc");',
  20689. '']));
  20690. end;
  20691. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20692. begin
  20693. StartProgram(false);
  20694. Add([
  20695. '{$interfaces com}',
  20696. 'type',
  20697. ' IUnknown = interface',
  20698. ' function _AddRef: longint;',
  20699. ' procedure Walk;',
  20700. ' end;',
  20701. ' IBird = interface end;',
  20702. ' IDog = interface end;',
  20703. ' TObject = class(IBird,IDog)',
  20704. ' function _AddRef: longint; virtual; abstract;',
  20705. ' procedure Walk; virtual; abstract;',
  20706. ' end;',
  20707. ' TBird = class(IUnknown)',
  20708. ' end;',
  20709. 'begin',
  20710. '']);
  20711. ConvertProgram;
  20712. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20713. LinesToStr([ // statements
  20714. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20715. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20716. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20717. 'rtl.createClass(this, "TObject", null, function () {',
  20718. ' this.$init = function () {',
  20719. ' };',
  20720. ' this.$final = function () {',
  20721. ' };',
  20722. ' rtl.addIntf(this, $mod.IBird);',
  20723. ' rtl.addIntf(this, $mod.IDog);',
  20724. '});',
  20725. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20726. ' rtl.addIntf(this, $mod.IUnknown);',
  20727. ' rtl.addIntf(this, $mod.IBird);',
  20728. ' rtl.addIntf(this, $mod.IDog);',
  20729. '});',
  20730. '']),
  20731. LinesToStr([ // $mod.$main
  20732. '']));
  20733. end;
  20734. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20735. begin
  20736. StartProgram(false);
  20737. Add([
  20738. '{$interfaces corba}',
  20739. 'type',
  20740. ' IUnknown = interface',
  20741. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20742. ' procedure Go;',
  20743. ' end;',
  20744. ' TObject = class(IUnknown)',
  20745. ' procedure Go; virtual; abstract;',
  20746. ' end;',
  20747. ' TBird = class',
  20748. ' procedure Go; override;',
  20749. ' end;',
  20750. ' TCat = class(TObject)',
  20751. ' procedure Go; override;',
  20752. ' end;',
  20753. ' TDog = class(TObject, IUnknown)',
  20754. ' procedure Go; override;',
  20755. ' end;',
  20756. 'procedure TBird.Go; begin end;',
  20757. 'procedure TCat.Go; begin end;',
  20758. 'procedure TDog.Go; begin end;',
  20759. 'begin',
  20760. '']);
  20761. ConvertProgram;
  20762. CheckSource('TestClassInterface_Corba_MethodOverride',
  20763. LinesToStr([ // statements
  20764. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20765. 'rtl.createClass(this, "TObject", null, function () {',
  20766. ' this.$init = function () {',
  20767. ' };',
  20768. ' this.$final = function () {',
  20769. ' };',
  20770. ' rtl.addIntf(this, $mod.IUnknown);',
  20771. '});',
  20772. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20773. ' this.Go = function () {',
  20774. ' };',
  20775. ' rtl.addIntf(this, $mod.IUnknown);',
  20776. '});',
  20777. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20778. ' this.Go = function () {',
  20779. ' };',
  20780. ' rtl.addIntf(this, $mod.IUnknown);',
  20781. '});',
  20782. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20783. ' this.Go = function () {',
  20784. ' };',
  20785. ' rtl.addIntf(this, $mod.IUnknown);',
  20786. '});',
  20787. '']),
  20788. LinesToStr([ // $mod.$main
  20789. '']));
  20790. end;
  20791. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20792. begin
  20793. StartProgram(false);
  20794. Add([
  20795. '{$interfaces corba}',
  20796. 'type',
  20797. ' IUnknown = interface',
  20798. ' end;',
  20799. ' IBird = interface(IUnknown)',
  20800. ' procedure Fly(s: string);',
  20801. ' end;',
  20802. ' IEagle = interface(IBird)',
  20803. ' end;',
  20804. ' IDove = interface(IBird)',
  20805. ' end;',
  20806. ' ISwallow = interface(IBird)',
  20807. ' end;',
  20808. ' TObject = class',
  20809. ' end;',
  20810. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20811. ' procedure Fly(s: string); virtual; abstract;',
  20812. ' end;',
  20813. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20814. ' FBirdIntf: IBird;',
  20815. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20816. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20817. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20818. ' FDoveObj: TBird;',
  20819. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20820. ' function GetSwallowObj: TBird; virtual; abstract;',
  20821. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20822. ' end;',
  20823. 'begin',
  20824. '']);
  20825. ConvertProgram;
  20826. CheckSource('TestClassInterface_Corba_Delegation',
  20827. LinesToStr([ // statements
  20828. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20829. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20830. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20831. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20832. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20833. 'rtl.createClass(this, "TObject", null, function () {',
  20834. ' this.$init = function () {',
  20835. ' };',
  20836. ' this.$final = function () {',
  20837. ' };',
  20838. '});',
  20839. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20840. ' rtl.addIntf(this, $mod.IBird);',
  20841. ' rtl.addIntf(this, $mod.IEagle);',
  20842. ' rtl.addIntf(this, $mod.IDove);',
  20843. ' rtl.addIntf(this, $mod.ISwallow);',
  20844. '});',
  20845. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20846. ' this.$init = function () {',
  20847. ' $mod.TObject.$init.call(this);',
  20848. ' this.FBirdIntf = null;',
  20849. ' this.FDoveObj = null;',
  20850. ' };',
  20851. ' this.$final = function () {',
  20852. ' this.FBirdIntf = undefined;',
  20853. ' this.FDoveObj = undefined;',
  20854. ' $mod.TObject.$final.call(this);',
  20855. ' };',
  20856. ' this.$intfmaps = {',
  20857. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20858. ' return this.FBirdIntf;',
  20859. ' },',
  20860. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20861. ' return this.GetEagleIntf();',
  20862. ' },',
  20863. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20864. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20865. ' },',
  20866. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20867. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20868. ' }',
  20869. ' };',
  20870. '});',
  20871. '']),
  20872. LinesToStr([ // $mod.$main
  20873. '']));
  20874. end;
  20875. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20876. begin
  20877. StartProgram(false);
  20878. Add([
  20879. '{$interfaces corba}',
  20880. 'type',
  20881. ' IUnknown = interface',
  20882. ' end;',
  20883. ' IBird = interface(IUnknown)',
  20884. ' procedure Fly(s: string);',
  20885. ' end;',
  20886. ' IEagle = interface(IBird)',
  20887. ' end;',
  20888. ' IDove = interface(IBird)',
  20889. ' end;',
  20890. ' ISwallow = interface(IBird)',
  20891. ' end;',
  20892. ' TObject = class',
  20893. ' end;',
  20894. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20895. ' procedure Fly(s: string); virtual; abstract;',
  20896. ' end;',
  20897. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20898. ' private',
  20899. ' class var FBirdIntf: IBird;',
  20900. ' class var FDoveObj: TBird;',
  20901. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20902. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20903. ' protected',
  20904. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20905. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20906. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20907. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20908. ' end;',
  20909. 'begin',
  20910. '']);
  20911. ConvertProgram;
  20912. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20913. LinesToStr([ // statements
  20914. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20915. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20916. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20917. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20918. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20919. 'rtl.createClass(this, "TObject", null, function () {',
  20920. ' this.$init = function () {',
  20921. ' };',
  20922. ' this.$final = function () {',
  20923. ' };',
  20924. '});',
  20925. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20926. ' rtl.addIntf(this, $mod.IBird);',
  20927. ' rtl.addIntf(this, $mod.IEagle);',
  20928. ' rtl.addIntf(this, $mod.IDove);',
  20929. ' rtl.addIntf(this, $mod.ISwallow);',
  20930. '});',
  20931. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20932. ' this.FBirdIntf = null;',
  20933. ' this.FDoveObj = null;',
  20934. ' this.$intfmaps = {',
  20935. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20936. ' return this.FBirdIntf;',
  20937. ' },',
  20938. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20939. ' return this.GetEagleIntf();',
  20940. ' },',
  20941. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20942. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20943. ' },',
  20944. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20945. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20946. ' }',
  20947. ' };',
  20948. '});',
  20949. '']),
  20950. LinesToStr([ // $mod.$main
  20951. '']));
  20952. end;
  20953. procedure TTestModule.TestClassInterface_Corba_Operators;
  20954. begin
  20955. StartProgram(false);
  20956. Add([
  20957. '{$interfaces corba}',
  20958. 'type',
  20959. ' IUnknown = interface',
  20960. ' end;',
  20961. ' IBird = interface(IUnknown)',
  20962. ' function GetItems(Index: longint): longint;',
  20963. ' procedure SetItems(Index: longint; Value: longint);',
  20964. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20965. ' end;',
  20966. ' TObject = class',
  20967. ' end;',
  20968. ' TBird = class(TObject,IBird)',
  20969. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20970. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20971. ' end;',
  20972. 'var',
  20973. ' IntfVar: IBird = nil;',
  20974. ' IntfVar2: IBird;',
  20975. ' ObjVar: TBird;',
  20976. ' v: JSValue;',
  20977. 'begin',
  20978. ' IntfVar:=nil;',
  20979. ' IntfVar[3]:=IntfVar[4];',
  20980. ' if Assigned(IntfVar) then ;',
  20981. ' IntfVar:=IntfVar2;',
  20982. ' IntfVar:=ObjVar;',
  20983. ' if IntfVar=IntfVar2 then ;',
  20984. ' if IntfVar<>IntfVar2 then ;',
  20985. ' if IntfVar is IBird then ;',
  20986. ' if IntfVar is TBird then ;',
  20987. ' if ObjVar is IBird then ;',
  20988. ' IntfVar:=IntfVar2 as IBird;',
  20989. ' ObjVar:=IntfVar2 as TBird;',
  20990. ' IntfVar:=ObjVar as IBird;',
  20991. ' IntfVar:=IBird(IntfVar2);',
  20992. ' ObjVar:=TBird(IntfVar);',
  20993. ' IntfVar:=IBird(ObjVar);',
  20994. ' v:=IntfVar;',
  20995. ' IntfVar:=IBird(v);',
  20996. ' if v is IBird then ;',
  20997. ' v:=JSValue(IntfVar);',
  20998. ' v:=IBird;',
  20999. '']);
  21000. ConvertProgram;
  21001. CheckSource('TestClassInterface_Corba_Operators',
  21002. LinesToStr([ // statements
  21003. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21004. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  21005. 'rtl.createClass(this, "TObject", null, function () {',
  21006. ' this.$init = function () {',
  21007. ' };',
  21008. ' this.$final = function () {',
  21009. ' };',
  21010. '});',
  21011. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21012. ' rtl.addIntf(this, $mod.IBird);',
  21013. '});',
  21014. 'this.IntfVar = null;',
  21015. 'this.IntfVar2 = null;',
  21016. 'this.ObjVar = null;',
  21017. 'this.v = undefined;',
  21018. '']),
  21019. LinesToStr([ // $mod.$main
  21020. '$mod.IntfVar = null;',
  21021. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  21022. 'if ($mod.IntfVar != null) ;',
  21023. '$mod.IntfVar = $mod.IntfVar2;',
  21024. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  21025. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  21026. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  21027. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  21028. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  21029. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  21030. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  21031. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  21032. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21033. '$mod.IntfVar = $mod.IntfVar2;',
  21034. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  21035. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21036. '$mod.v = $mod.IntfVar;',
  21037. '$mod.IntfVar = rtl.getObject($mod.v);',
  21038. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  21039. '$mod.v = $mod.IntfVar;',
  21040. '$mod.v = $mod.IBird;',
  21041. '']));
  21042. end;
  21043. procedure TTestModule.TestClassInterface_Corba_Args;
  21044. begin
  21045. StartProgram(false);
  21046. Add([
  21047. '{$interfaces corba}',
  21048. 'type',
  21049. ' IUnknown = interface',
  21050. ' end;',
  21051. ' IBird = interface(IUnknown)',
  21052. ' end;',
  21053. ' TObject = class',
  21054. ' end;',
  21055. ' TBird = class(TObject,IBird)',
  21056. ' end;',
  21057. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  21058. 'begin',
  21059. ' DoIt(i,i,i);',
  21060. 'end;',
  21061. 'procedure Change(var i: IBird; out j: IBird);',
  21062. 'begin',
  21063. ' DoIt(i,i,i);',
  21064. ' Change(i,i);',
  21065. 'end;',
  21066. 'var',
  21067. ' i: IBird;',
  21068. ' o: TBird;',
  21069. 'begin',
  21070. ' DoIt(i,i,i);',
  21071. ' Change(i,i);',
  21072. ' DoIt(o,o,o);',
  21073. '']);
  21074. ConvertProgram;
  21075. CheckSource('TestClassInterface_Corba_Args',
  21076. LinesToStr([ // statements
  21077. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21078. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  21079. 'rtl.createClass(this, "TObject", null, function () {',
  21080. ' this.$init = function () {',
  21081. ' };',
  21082. ' this.$final = function () {',
  21083. ' };',
  21084. '});',
  21085. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21086. ' rtl.addIntf(this, $mod.IBird);',
  21087. '});',
  21088. 'this.DoIt = function (u, i, j) {',
  21089. ' $mod.DoIt({',
  21090. ' get: function () {',
  21091. ' return i;',
  21092. ' },',
  21093. ' set: function (v) {',
  21094. ' i = v;',
  21095. ' }',
  21096. ' }, i, i);',
  21097. '};',
  21098. 'this.Change = function (i, j) {',
  21099. ' $mod.DoIt(i, i.get(), i.get());',
  21100. ' $mod.Change(i, i);',
  21101. '};',
  21102. 'this.i = null;',
  21103. 'this.o = null;',
  21104. '']),
  21105. LinesToStr([ // $mod.$main
  21106. '$mod.DoIt({',
  21107. ' p: $mod,',
  21108. ' get: function () {',
  21109. ' return this.p.i;',
  21110. ' },',
  21111. ' set: function (v) {',
  21112. ' this.p.i = v;',
  21113. ' }',
  21114. '}, $mod.i, $mod.i);',
  21115. '$mod.Change({',
  21116. ' p: $mod,',
  21117. ' get: function () {',
  21118. ' return this.p.i;',
  21119. ' },',
  21120. ' set: function (v) {',
  21121. ' this.p.i = v;',
  21122. ' }',
  21123. '}, {',
  21124. ' p: $mod,',
  21125. ' get: function () {',
  21126. ' return this.p.i;',
  21127. ' },',
  21128. ' set: function (v) {',
  21129. ' this.p.i = v;',
  21130. ' }',
  21131. '});',
  21132. '$mod.DoIt({',
  21133. ' p: $mod,',
  21134. ' get: function () {',
  21135. ' return this.p.o;',
  21136. ' },',
  21137. ' set: function (v) {',
  21138. ' this.p.o = v;',
  21139. ' }',
  21140. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21141. '']));
  21142. end;
  21143. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21144. begin
  21145. StartProgram(false);
  21146. Add([
  21147. '{$interfaces corba}',
  21148. 'type',
  21149. ' IUnknown = interface end;',
  21150. ' TObject = class',
  21151. ' Id: longint;',
  21152. ' end;',
  21153. ' IEnumerator = interface(IUnknown)',
  21154. ' function GetCurrent: TObject;',
  21155. ' function MoveNext: Boolean;',
  21156. ' property Current: TObject read GetCurrent;',
  21157. ' end;',
  21158. ' IEnumerable = interface(IUnknown)',
  21159. ' function GetEnumerator: IEnumerator;',
  21160. ' end;',
  21161. 'var',
  21162. ' o: TObject;',
  21163. ' i: IEnumerable;',
  21164. 'begin',
  21165. ' for o in i do o.Id:=3;',
  21166. '']);
  21167. ConvertProgram;
  21168. CheckSource('TestClassInterface_Corba_ForIn',
  21169. LinesToStr([ // statements
  21170. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21171. 'rtl.createClass(this, "TObject", null, function () {',
  21172. ' this.$init = function () {',
  21173. ' this.Id = 0;',
  21174. ' };',
  21175. ' this.$final = function () {',
  21176. ' };',
  21177. '});',
  21178. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21179. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21180. 'this.o = null;',
  21181. 'this.i = null;',
  21182. '']),
  21183. LinesToStr([ // $mod.$main
  21184. 'var $in = $mod.i.GetEnumerator();',
  21185. 'while ($in.MoveNext()) {',
  21186. ' $mod.o = $in.GetCurrent();',
  21187. ' $mod.o.Id = 3;',
  21188. '};',
  21189. '']));
  21190. end;
  21191. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21192. begin
  21193. StartProgram(false);
  21194. Add([
  21195. '{$interfaces corba}',
  21196. 'type',
  21197. ' IUnknown = interface end;',
  21198. ' IBird = interface(IUnknown)',
  21199. ' function Fly(w: word): word;',
  21200. ' end;',
  21201. ' TBirdArray = array of IBird;',
  21202. 'var',
  21203. ' i: IBird;',
  21204. ' a: TBirdArray;',
  21205. 'begin',
  21206. ' SetLength(a,3);',
  21207. ' i:=a[1];',
  21208. ' a[2]:=i;',
  21209. ' for i in a do i.fly(3);',
  21210. '']);
  21211. ConvertProgram;
  21212. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21213. LinesToStr([ // statements
  21214. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21215. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21216. 'this.i = null;',
  21217. 'this.a = [];',
  21218. '']),
  21219. LinesToStr([ // $mod.$main
  21220. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21221. '$mod.i = $mod.a[1];',
  21222. '$mod.a[2] = $mod.i;',
  21223. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21224. ' $mod.i = $in[$l];',
  21225. ' $mod.i.Fly(3);',
  21226. '};',
  21227. '']));
  21228. end;
  21229. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21230. begin
  21231. StartProgram(false);
  21232. Add([
  21233. '{$interfaces com}',
  21234. 'type',
  21235. ' IUnknown = interface',
  21236. ' function _AddRef: longint;',
  21237. ' function _Release: longint;',
  21238. ' end;',
  21239. ' TObject = class(IUnknown)',
  21240. ' function _AddRef: longint; virtual; abstract;',
  21241. ' function _Release: longint; virtual; abstract;',
  21242. ' end;',
  21243. 'var',
  21244. ' i: IUnknown;',
  21245. 'procedure DoGlobal(o: TObject);',
  21246. 'begin',
  21247. ' i:=nil;',
  21248. ' i:=o;',
  21249. ' i:=i;',
  21250. 'end;',
  21251. 'procedure DoLocal(o: TObject);',
  21252. 'const k: IUnknown = nil;',
  21253. 'var j: IUnknown;',
  21254. 'begin',
  21255. ' k:=o;',
  21256. ' k:=i;',
  21257. ' j:=o;',
  21258. ' j:=i;',
  21259. 'end;',
  21260. 'var o: TObject;',
  21261. 'begin',
  21262. ' i:=nil;',
  21263. ' i:=o;',
  21264. '']);
  21265. ConvertProgram;
  21266. CheckSource('TestClassInterface_COM_AssignVar',
  21267. LinesToStr([ // statements
  21268. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21269. 'rtl.createClass(this, "TObject", null, function () {',
  21270. ' this.$init = function () {',
  21271. ' };',
  21272. ' this.$final = function () {',
  21273. ' };',
  21274. ' rtl.addIntf(this, $mod.IUnknown);',
  21275. '});',
  21276. 'this.i = null;',
  21277. 'this.DoGlobal = function (o) {',
  21278. ' rtl.setIntfP($mod, "i", null);',
  21279. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21280. ' rtl.setIntfP($mod, "i", $mod.i);',
  21281. '};',
  21282. 'var k = null;',
  21283. 'this.DoLocal = function (o) {',
  21284. ' var j = null;',
  21285. ' try{',
  21286. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21287. ' k = rtl.setIntfL(k, $mod.i);',
  21288. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21289. ' j = rtl.setIntfL(j, $mod.i);',
  21290. ' }finally{',
  21291. ' rtl._Release(j);',
  21292. ' };',
  21293. '};',
  21294. 'this.o = null;',
  21295. '']),
  21296. LinesToStr([ // $mod.$main
  21297. 'rtl.setIntfP($mod, "i", null);',
  21298. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21299. '']));
  21300. end;
  21301. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21302. begin
  21303. StartProgram(false);
  21304. Add([
  21305. '{$interfaces com}',
  21306. 'type',
  21307. ' IUnknown = interface',
  21308. ' function _AddRef: longint;',
  21309. ' function _Release: longint;',
  21310. ' end;',
  21311. ' TObject = class(IUnknown)',
  21312. ' function _AddRef: longint; virtual; abstract;',
  21313. ' function _Release: longint; virtual; abstract;',
  21314. ' end;',
  21315. 'procedure DoDefault(i, j: IUnknown);',
  21316. 'begin',
  21317. ' i:=nil;',
  21318. ' i:=j;',
  21319. 'end;',
  21320. 'begin',
  21321. '']);
  21322. ConvertProgram;
  21323. CheckSource('TestClassInterface_COM_AssignArg',
  21324. LinesToStr([ // statements
  21325. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21326. 'rtl.createClass(this, "TObject", null, function () {',
  21327. ' this.$init = function () {',
  21328. ' };',
  21329. ' this.$final = function () {',
  21330. ' };',
  21331. ' rtl.addIntf(this, $mod.IUnknown);',
  21332. '});',
  21333. 'this.DoDefault = function (i, j) {',
  21334. ' rtl._AddRef(i);',
  21335. ' rtl._AddRef(j);',
  21336. ' try {',
  21337. ' i = rtl.setIntfL(i, null);',
  21338. ' i = rtl.setIntfL(i, j);',
  21339. ' } finally {',
  21340. ' rtl._Release(i);',
  21341. ' rtl._Release(j);',
  21342. ' };',
  21343. '};',
  21344. '']),
  21345. LinesToStr([ // $mod.$main
  21346. '']));
  21347. end;
  21348. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21349. begin
  21350. StartProgram(false);
  21351. Add([
  21352. '{$interfaces com}',
  21353. 'type',
  21354. ' IUnknown = interface',
  21355. ' function _AddRef: longint;',
  21356. ' function _Release: longint;',
  21357. ' end;',
  21358. ' TObject = class(IUnknown)',
  21359. ' function _AddRef: longint; virtual; abstract;',
  21360. ' function _Release: longint; virtual; abstract;',
  21361. ' end;',
  21362. 'function DoDefault(i: IUnknown): IUnknown;',
  21363. 'begin',
  21364. ' Result:=i;',
  21365. ' if Result<>nil then exit;',
  21366. 'end;',
  21367. 'var i: IUnknown;',
  21368. 'begin',
  21369. ' DoDefault(i);',
  21370. ' i:=DoDefault(i);',
  21371. '']);
  21372. ConvertProgram;
  21373. CheckSource('TestClassInterface_COM_FunctionResult',
  21374. LinesToStr([ // statements
  21375. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21376. 'rtl.createClass(this, "TObject", null, function () {',
  21377. ' this.$init = function () {',
  21378. ' };',
  21379. ' this.$final = function () {',
  21380. ' };',
  21381. ' rtl.addIntf(this, $mod.IUnknown);',
  21382. '});',
  21383. 'this.DoDefault = function (i) {',
  21384. ' var Result = null;',
  21385. ' rtl._AddRef(i);',
  21386. ' var $ok = false;',
  21387. ' try {',
  21388. ' Result = rtl.setIntfL(Result, i);',
  21389. ' if(Result !== null){',
  21390. ' $ok = true;',
  21391. ' return Result;',
  21392. ' };',
  21393. ' $ok = true;',
  21394. ' } finally {',
  21395. ' rtl._Release(i);',
  21396. ' if(!$ok) rtl._Release(Result);',
  21397. ' };',
  21398. ' return Result;',
  21399. '};',
  21400. 'this.i = null;',
  21401. '']),
  21402. LinesToStr([ // $mod.$main
  21403. 'var $ir = rtl.createIntfRefs();',
  21404. 'try {',
  21405. ' $ir.ref(1, $mod.DoDefault($mod.i));',
  21406. ' rtl.setIntfP($mod, "i", $mod.DoDefault($mod.i), true);',
  21407. '} finally {',
  21408. ' $ir.free();',
  21409. '};',
  21410. '']));
  21411. end;
  21412. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21413. begin
  21414. StartProgram(false);
  21415. Add([
  21416. '{$interfaces com}',
  21417. 'type',
  21418. ' IUnknown = interface',
  21419. ' function _AddRef: longint;',
  21420. ' function _Release: longint;',
  21421. ' end;',
  21422. ' TObject = class(IUnknown)',
  21423. ' function _AddRef: longint; virtual; abstract;',
  21424. ' function _Release: longint; virtual; abstract;',
  21425. ' function GetIntf: IUnknown; virtual;',
  21426. ' end;',
  21427. ' TMouse = class',
  21428. ' function GetIntf: IUnknown; override;',
  21429. ' end;',
  21430. 'function TObject.GetIntf: IUnknown; begin end;',
  21431. 'function TMouse.GetIntf: IUnknown;',
  21432. 'var i: IUnknown;',
  21433. 'begin',
  21434. ' inherited;',
  21435. ' inherited GetIntf;',
  21436. ' inherited GetIntf();',
  21437. ' Result:=inherited GetIntf;',
  21438. ' Result:=inherited GetIntf();',
  21439. ' i:=inherited GetIntf;',
  21440. ' i:=inherited GetIntf();',
  21441. 'end;',
  21442. 'begin',
  21443. '']);
  21444. ConvertProgram;
  21445. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21446. LinesToStr([ // statements
  21447. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21448. 'rtl.createClass(this, "TObject", null, function () {',
  21449. ' this.$init = function () {',
  21450. ' };',
  21451. ' this.$final = function () {',
  21452. ' };',
  21453. ' this.GetIntf = function () {',
  21454. ' var Result = null;',
  21455. ' return Result;',
  21456. ' };',
  21457. ' rtl.addIntf(this, $mod.IUnknown);',
  21458. '});',
  21459. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21460. ' this.GetIntf = function () {',
  21461. ' var Result = null;',
  21462. ' var i = null;',
  21463. ' var $ir = rtl.createIntfRefs();',
  21464. ' var $ok = false;',
  21465. ' try {',
  21466. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21467. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21468. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21469. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21470. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21471. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21472. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21473. ' $ok = true;',
  21474. ' } finally {',
  21475. ' $ir.free();',
  21476. ' rtl._Release(i);',
  21477. ' if (!$ok) rtl._Release(Result);',
  21478. ' };',
  21479. ' return Result;',
  21480. ' };',
  21481. ' rtl.addIntf(this, $mod.IUnknown);',
  21482. '});',
  21483. '']),
  21484. LinesToStr([ // $mod.$main
  21485. '']));
  21486. end;
  21487. procedure TTestModule.TestClassInterface_COM_FunctionExit;
  21488. begin
  21489. StartProgram(false);
  21490. Add([
  21491. '{$interfaces com}',
  21492. 'type',
  21493. ' IUnknown = interface',
  21494. ' function _AddRef: longint;',
  21495. ' function _Release: longint;',
  21496. ' end;',
  21497. ' TObject = class(IUnknown)',
  21498. ' function _AddRef: longint; virtual; abstract;',
  21499. ' function _Release: longint; virtual; abstract;',
  21500. ' constructor Create;',
  21501. ' end;',
  21502. 'constructor TObject.Create;',
  21503. 'begin',
  21504. 'end;',
  21505. 'function GetIntf: IUnknown;',
  21506. 'var Intf: IUnknown;',
  21507. 'begin',
  21508. ' Intf := TObject.Create;',
  21509. ' Exit(Intf);',
  21510. 'end;',
  21511. 'begin',
  21512. '']);
  21513. ConvertProgram;
  21514. CheckSource('TestClassInterface_COM_FunctionExit',
  21515. LinesToStr([ // statements
  21516. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21517. 'rtl.createClass(this, "TObject", null, function () {',
  21518. ' this.$init = function () {',
  21519. ' };',
  21520. ' this.$final = function () {',
  21521. ' };',
  21522. ' this.Create = function () {',
  21523. ' return this;',
  21524. ' };',
  21525. ' rtl.addIntf(this, $mod.IUnknown);',
  21526. '});',
  21527. 'this.GetIntf = function () {',
  21528. ' var Result = null;',
  21529. ' var Intf = null;',
  21530. ' var $ok = false;',
  21531. ' try {',
  21532. ' Intf = rtl.setIntfL(Intf, rtl.queryIntfT($mod.TObject.$create("Create"), $mod.IUnknown), true);',
  21533. ' $ok = true;',
  21534. ' Result = rtl.setIntfL(Result, Intf);',
  21535. ' return Result;',
  21536. ' $ok = true;',
  21537. ' } finally {',
  21538. ' rtl._Release(Intf);',
  21539. ' if (!$ok) rtl._Release(Result);',
  21540. ' };',
  21541. ' return Result;',
  21542. '};',
  21543. '']),
  21544. LinesToStr([ // $mod.$main
  21545. '']));
  21546. end;
  21547. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21548. begin
  21549. StartProgram(false);
  21550. Add([
  21551. '{$interfaces com}',
  21552. 'type',
  21553. ' IUnknown = interface',
  21554. ' function _AddRef: longint;',
  21555. ' function _Release: longint;',
  21556. ' end;',
  21557. ' TObject = class(IUnknown)',
  21558. ' function _AddRef: longint; virtual; abstract;',
  21559. ' function _Release: longint; virtual; abstract;',
  21560. ' end;',
  21561. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21562. 'begin',
  21563. ' if i is IUnknown then ;',
  21564. ' if o is IUnknown then ;',
  21565. ' if i is TObject then ;',
  21566. ' i:=j as IUnknown;',
  21567. ' i:=o as IUnknown;',
  21568. ' o:=j as TObject;',
  21569. ' i:=IUnknown(j);',
  21570. ' i:=IUnknown(o);',
  21571. ' o:=TObject(i);',
  21572. 'end;',
  21573. 'begin',
  21574. '']);
  21575. ConvertProgram;
  21576. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21577. LinesToStr([ // statements
  21578. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21579. 'rtl.createClass(this, "TObject", null, function () {',
  21580. ' this.$init = function () {',
  21581. ' };',
  21582. ' this.$final = function () {',
  21583. ' };',
  21584. ' rtl.addIntf(this, $mod.IUnknown);',
  21585. '});',
  21586. 'this.DoDefault = function (i, j, o) {',
  21587. ' rtl._AddRef(i);',
  21588. ' rtl._AddRef(j);',
  21589. ' try {',
  21590. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21591. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21592. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21593. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21594. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21595. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21596. ' i = rtl.setIntfL(i, j);',
  21597. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21598. ' o = rtl.intfToClass(i, $mod.TObject);',
  21599. ' } finally {',
  21600. ' rtl._Release(i);',
  21601. ' rtl._Release(j);',
  21602. ' };',
  21603. '};',
  21604. '']),
  21605. LinesToStr([ // $mod.$main
  21606. '']));
  21607. end;
  21608. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21609. begin
  21610. StartProgram(false);
  21611. Add([
  21612. '{$interfaces com}',
  21613. 'type',
  21614. ' IUnknown = interface',
  21615. ' function _AddRef: longint;',
  21616. ' function _Release: longint;',
  21617. ' end;',
  21618. ' TObject = class(IUnknown)',
  21619. ' function _AddRef: longint; virtual; abstract;',
  21620. ' function _Release: longint; virtual; abstract;',
  21621. ' end;',
  21622. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21623. 'var o: TObject;',
  21624. 'begin',
  21625. ' DoIt(v,v,v,v);',
  21626. ' DoIt(o,o,k,k);',
  21627. 'end;',
  21628. 'procedure DoSome;',
  21629. 'var v: IUnknown;',
  21630. 'begin',
  21631. ' DoIt(v,v,v,v);',
  21632. 'end;',
  21633. 'var i: IUnknown;',
  21634. 'begin',
  21635. ' DoIt(i,i,i,i);',
  21636. '']);
  21637. ConvertProgram;
  21638. CheckSource('TestClassInterface_COM_PassAsArg',
  21639. LinesToStr([ // statements
  21640. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21641. 'rtl.createClass(this, "TObject", null, function () {',
  21642. ' this.$init = function () {',
  21643. ' };',
  21644. ' this.$final = function () {',
  21645. ' };',
  21646. ' rtl.addIntf(this, $mod.IUnknown);',
  21647. '});',
  21648. 'this.DoIt = function (v, j, k, l) {',
  21649. ' var o = null;',
  21650. ' var $ir = rtl.createIntfRefs();',
  21651. ' rtl._AddRef(v);',
  21652. ' try {',
  21653. ' $mod.DoIt(v, v, {',
  21654. ' get: function () {',
  21655. ' return v;',
  21656. ' },',
  21657. ' set: function (w) {',
  21658. ' v = rtl.setIntfL(v, w);',
  21659. ' }',
  21660. ' }, {',
  21661. ' get: function () {',
  21662. ' return v;',
  21663. ' },',
  21664. ' set: function (w) {',
  21665. ' v = rtl.setIntfL(v, w);',
  21666. ' }',
  21667. ' });',
  21668. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21669. ' } finally {',
  21670. ' $ir.free();',
  21671. ' rtl._Release(v);',
  21672. ' };',
  21673. '};',
  21674. 'this.DoSome = function () {',
  21675. ' var v = null;',
  21676. ' try {',
  21677. ' $mod.DoIt(v, v, {',
  21678. ' get: function () {',
  21679. ' return v;',
  21680. ' },',
  21681. ' set: function (w) {',
  21682. ' v = rtl.setIntfL(v, w);',
  21683. ' }',
  21684. ' }, {',
  21685. ' get: function () {',
  21686. ' return v;',
  21687. ' },',
  21688. ' set: function (w) {',
  21689. ' v = rtl.setIntfL(v, w);',
  21690. ' }',
  21691. ' });',
  21692. ' } finally {',
  21693. ' rtl._Release(v);',
  21694. ' };',
  21695. '};',
  21696. 'this.i = null;',
  21697. '']),
  21698. LinesToStr([ // $mod.$main
  21699. '$mod.DoIt($mod.i, $mod.i, {',
  21700. ' p: $mod,',
  21701. ' get: function () {',
  21702. ' return this.p.i;',
  21703. ' },',
  21704. ' set: function (v) {',
  21705. ' rtl.setIntfP(this.p, "i", v);',
  21706. ' }',
  21707. '}, {',
  21708. ' p: $mod,',
  21709. ' get: function () {',
  21710. ' return this.p.i;',
  21711. ' },',
  21712. ' set: function (v) {',
  21713. ' rtl.setIntfP(this.p, "i", v);',
  21714. ' }',
  21715. '});',
  21716. '']));
  21717. end;
  21718. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21719. begin
  21720. StartProgram(false);
  21721. Add([
  21722. '{$interfaces com}',
  21723. 'type',
  21724. ' IUnknown = interface',
  21725. ' function _AddRef: longint;',
  21726. ' function _Release: longint;',
  21727. ' end;',
  21728. ' TObject = class(IUnknown)',
  21729. ' function _AddRef: longint; virtual; abstract;',
  21730. ' function _Release: longint; virtual; abstract;',
  21731. ' end;',
  21732. 'procedure DoIt(out i);',
  21733. 'begin end;',
  21734. 'procedure DoSome;',
  21735. 'var v: IUnknown;',
  21736. 'begin',
  21737. ' DoIt(v);',
  21738. 'end;',
  21739. 'function GetIt: IUnknown;',
  21740. 'begin',
  21741. ' DoIt(Result);',
  21742. 'end;',
  21743. 'var i: IUnknown;',
  21744. 'begin',
  21745. ' DoIt(i);',
  21746. '']);
  21747. ConvertProgram;
  21748. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21749. LinesToStr([ // statements
  21750. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21751. 'rtl.createClass(this, "TObject", null, function () {',
  21752. ' this.$init = function () {',
  21753. ' };',
  21754. ' this.$final = function () {',
  21755. ' };',
  21756. ' rtl.addIntf(this, $mod.IUnknown);',
  21757. '});',
  21758. 'this.DoIt = function (i) {',
  21759. '};',
  21760. 'this.DoSome = function () {',
  21761. ' var v = null;',
  21762. ' try {',
  21763. ' $mod.DoIt({',
  21764. ' get: function () {',
  21765. ' return v;',
  21766. ' },',
  21767. ' set: function (w) {',
  21768. ' v = w;',
  21769. ' }',
  21770. ' });',
  21771. ' } finally {',
  21772. ' rtl._Release(v);',
  21773. ' };',
  21774. '};',
  21775. 'this.GetIt = function () {',
  21776. ' var Result = null;',
  21777. ' var $ok = false;',
  21778. ' try {',
  21779. ' $mod.DoIt({',
  21780. ' get: function () {',
  21781. ' return Result;',
  21782. ' },',
  21783. ' set: function (v) {',
  21784. ' Result = v;',
  21785. ' }',
  21786. ' });',
  21787. ' $ok = true;',
  21788. ' } finally {',
  21789. ' if (!$ok) rtl._Release(Result);',
  21790. ' };',
  21791. ' return Result;',
  21792. '};',
  21793. 'this.i = null;',
  21794. '']),
  21795. LinesToStr([ // $mod.$main
  21796. 'try {',
  21797. ' $mod.DoIt({',
  21798. ' p: $mod,',
  21799. ' get: function () {',
  21800. ' return this.p.i;',
  21801. ' },',
  21802. ' set: function (v) {',
  21803. ' this.p.i = v;',
  21804. ' }',
  21805. ' });',
  21806. '} finally {',
  21807. ' rtl._Release($mod.i);',
  21808. '};',
  21809. '']));
  21810. end;
  21811. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21812. begin
  21813. StartProgram(false);
  21814. Add([
  21815. '{$interfaces com}',
  21816. 'type',
  21817. ' IUnknown = interface',
  21818. ' function _AddRef: longint;',
  21819. ' function _Release: longint;',
  21820. ' end;',
  21821. ' TObject = class(IUnknown)',
  21822. ' function _AddRef: longint; virtual; abstract;',
  21823. ' function _Release: longint; virtual; abstract;',
  21824. ' end;',
  21825. 'function GetIt: IUnknown;',
  21826. 'begin',
  21827. 'end;',
  21828. 'procedure DoSome;',
  21829. 'var v: IUnknown;',
  21830. ' i: longint;',
  21831. 'begin',
  21832. ' v:=GetIt;',
  21833. ' v:=GetIt();',
  21834. ' GetIt()._AddRef;',
  21835. ' i:=GetIt()._AddRef;',
  21836. 'end;',
  21837. 'var v: IUnknown;',
  21838. ' i: longint;',
  21839. 'begin',
  21840. ' v:=GetIt;',
  21841. ' v:=GetIt();',
  21842. ' GetIt()._AddRef;',
  21843. ' i:=GetIt()._AddRef;',
  21844. '']);
  21845. ConvertProgram;
  21846. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21847. LinesToStr([ // statements
  21848. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21849. 'rtl.createClass(this, "TObject", null, function () {',
  21850. ' this.$init = function () {',
  21851. ' };',
  21852. ' this.$final = function () {',
  21853. ' };',
  21854. ' rtl.addIntf(this, $mod.IUnknown);',
  21855. '});',
  21856. 'this.GetIt = function () {',
  21857. ' var Result = null;',
  21858. ' return Result;',
  21859. '};',
  21860. 'this.DoSome = function () {',
  21861. ' var v = null;',
  21862. ' var i = 0;',
  21863. ' var $ir = rtl.createIntfRefs();',
  21864. ' try {',
  21865. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21866. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21867. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21868. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21869. ' } finally {',
  21870. ' $ir.free();',
  21871. ' rtl._Release(v);',
  21872. ' };',
  21873. '};',
  21874. 'this.v = null;',
  21875. 'this.i = 0;',
  21876. '']),
  21877. LinesToStr([ // $mod.$main
  21878. 'var $ir = rtl.createIntfRefs();',
  21879. 'try {',
  21880. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21881. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21882. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21883. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21884. '} finally {',
  21885. ' $ir.free();',
  21886. '};',
  21887. '']));
  21888. end;
  21889. procedure TTestModule.TestClassInterface_COM_Property;
  21890. begin
  21891. StartProgram(false);
  21892. Add([
  21893. '{$interfaces com}',
  21894. 'type',
  21895. ' IUnknown = interface',
  21896. ' function _AddRef: longint;',
  21897. ' function _Release: longint;',
  21898. ' end;',
  21899. ' TObject = class(IUnknown)',
  21900. ' FAnt: IUnknown;',
  21901. ' function _AddRef: longint; virtual; abstract;',
  21902. ' function _Release: longint; virtual; abstract;',
  21903. ' function GetBird: IUnknown; virtual; abstract;',
  21904. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21905. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21906. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21907. ' property Ant: IUnknown read FAnt write FAnt;',
  21908. ' property Bird: IUnknown read GetBird write SetBird;',
  21909. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21910. ' end;',
  21911. 'procedure DoIt;',
  21912. 'var',
  21913. ' o: TObject;',
  21914. ' v: IUnknown;',
  21915. 'begin',
  21916. ' v:=o.Ant;',
  21917. ' o.Ant:=v;',
  21918. ' o.Ant:=o.Ant;',
  21919. ' v:=o.Bird;',
  21920. ' o.Bird:=v;',
  21921. ' o.Bird:=o.Bird;',
  21922. ' v:=o.Items[1];',
  21923. ' o.Items[2]:=v;',
  21924. ' o.Items[3]:=o.Items[4];',
  21925. ' v:=o[5];',
  21926. ' o[6]:=v;',
  21927. ' o[7]:=o[8];',
  21928. 'end;',
  21929. 'begin',
  21930. '']);
  21931. ConvertProgram;
  21932. CheckSource('TestClassInterface_COM_Property',
  21933. LinesToStr([ // statements
  21934. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21935. 'rtl.createClass(this, "TObject", null, function () {',
  21936. ' this.$init = function () {',
  21937. ' this.FAnt = null;',
  21938. ' };',
  21939. ' this.$final = function () {',
  21940. ' rtl.setIntfP(this, "FAnt", null);',
  21941. ' };',
  21942. ' rtl.addIntf(this, $mod.IUnknown);',
  21943. '});',
  21944. 'this.DoIt = function () {',
  21945. ' var o = null;',
  21946. ' var v = null;',
  21947. ' var $ir = rtl.createIntfRefs();',
  21948. ' try {',
  21949. ' v = rtl.setIntfL(v, o.FAnt);',
  21950. ' rtl.setIntfP(o, "FAnt", v);',
  21951. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21952. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21953. ' o.SetBird(v);',
  21954. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21955. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21956. ' o.SetItems(2, v);',
  21957. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21958. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21959. ' o.SetItems(6, v);',
  21960. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21961. ' } finally {',
  21962. ' $ir.free();',
  21963. ' rtl._Release(v);',
  21964. ' };',
  21965. '};',
  21966. '']),
  21967. LinesToStr([ // $mod.$main
  21968. '']));
  21969. end;
  21970. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21971. begin
  21972. StartProgram(false);
  21973. Add([
  21974. '{$interfaces com}',
  21975. 'type',
  21976. ' IUnknown = interface',
  21977. ' function _AddRef: longint;',
  21978. ' function _Release: longint;',
  21979. ' function GetBird: IUnknown;',
  21980. ' procedure SetBird(Value: IUnknown);',
  21981. ' function GetItems(Index: longint): IUnknown;',
  21982. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21983. ' property Bird: IUnknown read GetBird write SetBird;',
  21984. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21985. ' end;',
  21986. ' TObject = class(IUnknown)',
  21987. ' function _AddRef: longint; virtual; abstract;',
  21988. ' function _Release: longint; virtual; abstract;',
  21989. ' function GetBird: IUnknown; virtual; abstract;',
  21990. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21991. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21992. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21993. ' end;',
  21994. 'procedure DoIt;',
  21995. 'var',
  21996. ' o: TObject;',
  21997. ' v: IUnknown;',
  21998. 'begin',
  21999. ' v:=v.Items[1];',
  22000. ' v.Items[2]:=v;',
  22001. ' v.Items[3]:=v.Items[4];',
  22002. ' v:=v[5];',
  22003. ' v[6]:=v;',
  22004. ' v[7]:=v[8];',
  22005. ' v[9].Bird.Bird:=v;',
  22006. ' v:=v.Bird[10].Bird',
  22007. 'end;',
  22008. 'begin',
  22009. '']);
  22010. ConvertProgram;
  22011. CheckSource('TestClassInterface_COM_IntfProperty',
  22012. LinesToStr([ // statements
  22013. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  22014. ' "_AddRef",',
  22015. ' "_Release",',
  22016. ' "GetBird",',
  22017. ' "SetBird",',
  22018. ' "GetItems",',
  22019. ' "SetItems"',
  22020. '], null);',
  22021. 'rtl.createClass(this, "TObject", null, function () {',
  22022. ' this.$init = function () {',
  22023. ' };',
  22024. ' this.$final = function () {',
  22025. ' };',
  22026. ' rtl.addIntf(this, $mod.IUnknown);',
  22027. '});',
  22028. 'this.DoIt = function () {',
  22029. ' var o = null;',
  22030. ' var v = null;',
  22031. ' var $ir = rtl.createIntfRefs();',
  22032. ' try {',
  22033. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  22034. ' v.SetItems(2, v);',
  22035. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  22036. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  22037. ' v.SetItems(6, v);',
  22038. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  22039. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  22040. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  22041. ' } finally {',
  22042. ' $ir.free();',
  22043. ' rtl._Release(v);',
  22044. ' };',
  22045. '};',
  22046. '']),
  22047. LinesToStr([ // $mod.$main
  22048. '']));
  22049. end;
  22050. procedure TTestModule.TestClassInterface_COM_Delegation;
  22051. begin
  22052. StartProgram(false);
  22053. Add([
  22054. '{$interfaces com}',
  22055. 'type',
  22056. ' IUnknown = interface',
  22057. ' function _AddRef: longint;',
  22058. ' function _Release: longint;',
  22059. ' end;',
  22060. ' IBird = interface(IUnknown)',
  22061. ' procedure Fly(s: string);',
  22062. ' end;',
  22063. ' IEagle = interface(IBird) end;',
  22064. ' IDove = interface(IBird) end;',
  22065. ' ISwallow = interface(IBird) end;',
  22066. ' TObject = class',
  22067. ' end;',
  22068. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  22069. ' function _AddRef: longint; virtual; abstract;',
  22070. ' function _Release: longint; virtual; abstract;',
  22071. ' procedure Fly(s: string); virtual; abstract;',
  22072. ' end;',
  22073. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  22074. ' function _AddRef: longint; virtual; abstract;',
  22075. ' function _Release: longint; virtual; abstract;',
  22076. ' FBirdIntf: IBird;',
  22077. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  22078. ' function GetEagleIntf: IEagle; virtual; abstract;',
  22079. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  22080. ' FDoveObj: TBird;',
  22081. ' property DoveObj: TBird read FDoveObj implements IDove;',
  22082. ' function GetSwallowObj: TBird; virtual; abstract;',
  22083. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  22084. ' end;',
  22085. 'begin',
  22086. '']);
  22087. ConvertProgram;
  22088. CheckSource('TestClassInterface_COM_Delegation',
  22089. LinesToStr([ // statements
  22090. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22091. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  22092. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  22093. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  22094. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  22095. 'rtl.createClass(this, "TObject", null, function () {',
  22096. ' this.$init = function () {',
  22097. ' };',
  22098. ' this.$final = function () {',
  22099. ' };',
  22100. '});',
  22101. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22102. ' rtl.addIntf(this, $mod.IBird);',
  22103. ' rtl.addIntf(this, $mod.IEagle);',
  22104. ' rtl.addIntf(this, $mod.IDove);',
  22105. ' rtl.addIntf(this, $mod.ISwallow);',
  22106. '});',
  22107. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  22108. ' this.$init = function () {',
  22109. ' $mod.TObject.$init.call(this);',
  22110. ' this.FBirdIntf = null;',
  22111. ' this.FDoveObj = null;',
  22112. ' };',
  22113. ' this.$final = function () {',
  22114. ' rtl.setIntfP(this, "FBirdIntf", null);',
  22115. ' this.FDoveObj = undefined;',
  22116. ' $mod.TObject.$final.call(this);',
  22117. ' };',
  22118. ' this.$intfmaps = {',
  22119. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  22120. ' return rtl._AddRef(this.FBirdIntf);',
  22121. ' },',
  22122. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  22123. ' return this.GetEagleIntf();',
  22124. ' },',
  22125. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  22126. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  22127. ' },',
  22128. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  22129. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  22130. ' }',
  22131. ' };',
  22132. '});',
  22133. '']),
  22134. LinesToStr([ // $mod.$main
  22135. '']));
  22136. end;
  22137. procedure TTestModule.TestClassInterface_COM_With;
  22138. begin
  22139. StartProgram(false);
  22140. Add([
  22141. '{$interfaces com}',
  22142. 'type',
  22143. ' IUnknown = interface',
  22144. ' function _AddRef: longint;',
  22145. ' function _Release: longint;',
  22146. ' function GetAnt: IUnknown;',
  22147. ' property Ant: IUnknown read GetAnt;',
  22148. ' end;',
  22149. ' TObject = class(IUnknown)',
  22150. ' function _AddRef: longint; virtual; abstract;',
  22151. ' function _Release: longint; virtual; abstract;',
  22152. ' function GetAnt: IUnknown; virtual; abstract;',
  22153. ' property Ant: IUnknown read GetAnt;',
  22154. ' end;',
  22155. 'procedure DoIt;',
  22156. 'var',
  22157. ' i: IUnknown;',
  22158. 'begin',
  22159. ' with i do ',
  22160. ' GetAnt;',
  22161. ' with i.Ant, Ant do ',
  22162. ' GetAnt;',
  22163. 'end;',
  22164. 'begin',
  22165. '']);
  22166. ConvertProgram;
  22167. CheckSource('TestClassInterface_COM_With',
  22168. LinesToStr([ // statements
  22169. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22170. 'rtl.createClass(this, "TObject", null, function () {',
  22171. ' this.$init = function () {',
  22172. ' };',
  22173. ' this.$final = function () {',
  22174. ' };',
  22175. ' rtl.addIntf(this, $mod.IUnknown);',
  22176. '});',
  22177. 'this.DoIt = function () {',
  22178. ' var i = null;',
  22179. ' var $ir = rtl.createIntfRefs();',
  22180. ' try {',
  22181. ' $ir.ref(1, i.GetAnt());',
  22182. ' var $with = $ir.ref(2, i.GetAnt());',
  22183. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22184. ' $ir.ref(4, $with1.GetAnt());',
  22185. ' } finally {',
  22186. ' $ir.free();',
  22187. ' };',
  22188. '};',
  22189. '']),
  22190. LinesToStr([ // $mod.$main
  22191. '']));
  22192. end;
  22193. procedure TTestModule.TestClassInterface_COM_ForObjectInInterface;
  22194. begin
  22195. StartProgram(false);
  22196. Add([
  22197. '{$interfaces com}',
  22198. 'type',
  22199. ' IUnknown = interface end;',
  22200. ' TObject = class',
  22201. ' Id: longint;',
  22202. ' end;',
  22203. ' IEnumerator = interface(IUnknown)',
  22204. ' function GetCurrent: TObject;',
  22205. ' function MoveNext: Boolean;',
  22206. ' property Current: TObject read GetCurrent;',
  22207. ' end;',
  22208. ' IEnumerable = interface(IUnknown)',
  22209. ' function GetEnumerator: IEnumerator;',
  22210. ' end;',
  22211. 'var',
  22212. ' o: TObject;',
  22213. ' i: IEnumerable;',
  22214. 'begin',
  22215. ' for o in i do o.Id:=3;',
  22216. '']);
  22217. ConvertProgram;
  22218. CheckSource('TestClassInterface_COM_ForObjectInInterface',
  22219. LinesToStr([ // statements
  22220. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22221. 'rtl.createClass(this, "TObject", null, function () {',
  22222. ' this.$init = function () {',
  22223. ' this.Id = 0;',
  22224. ' };',
  22225. ' this.$final = function () {',
  22226. ' };',
  22227. '});',
  22228. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22229. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22230. 'this.o = null;',
  22231. 'this.i = null;',
  22232. '']),
  22233. LinesToStr([ // $mod.$main
  22234. 'var $in = $mod.i.GetEnumerator();',
  22235. 'try {',
  22236. ' while ($in.MoveNext()) {',
  22237. ' $mod.o = $in.GetCurrent();',
  22238. ' $mod.o.Id = 3;',
  22239. ' }',
  22240. '} finally {',
  22241. ' rtl._Release($in)',
  22242. '};',
  22243. '']));
  22244. end;
  22245. procedure TTestModule.TestClassInterface_COM_ForInterfaceInObject;
  22246. begin
  22247. StartProgram(false);
  22248. Add([
  22249. '{$interfaces com}',
  22250. 'type',
  22251. ' IUnknown = interface end;',
  22252. ' TObject = class',
  22253. ' end;',
  22254. ' IWing = interface',
  22255. ' function Id: longint;',
  22256. ' end;',
  22257. ' TEnumerator = class',
  22258. ' function GetCurrent: IWing; virtual; abstract;',
  22259. ' function MoveNext: Boolean; virtual; abstract;',
  22260. ' property Current: IWing read GetCurrent;',
  22261. ' end;',
  22262. ' TBird = class',
  22263. ' function GetEnumerator: TEnumerator; virtual; abstract;',
  22264. ' procedure Test;',
  22265. ' end;',
  22266. 'procedure TBird.Test;',
  22267. 'var',
  22268. ' Wing: IWing;',
  22269. 'begin',
  22270. ' for Wing in Self do',
  22271. ' if Wing.Id=1 then ;',
  22272. 'end;',
  22273. 'var',
  22274. ' Bird: TBird;',
  22275. ' Wing: IWing;',
  22276. 'begin',
  22277. ' for Wing in Bird do',
  22278. ' if Wing.Id=2 then ;',
  22279. '']);
  22280. ConvertProgram;
  22281. CheckSource('TestClassInterface_COM_ForInterfaceInObject',
  22282. LinesToStr([ // statements
  22283. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22284. 'rtl.createClass(this, "TObject", null, function () {',
  22285. ' this.$init = function () {',
  22286. ' };',
  22287. ' this.$final = function () {',
  22288. ' };',
  22289. '});',
  22290. 'rtl.createInterface(this, "IWing", "{8B0D080B-C0F6-396E-AE88-000BDB74730C}", ["Id"], this.IUnknown);',
  22291. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22292. '});',
  22293. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22294. ' this.Test = function () {',
  22295. ' var Wing = null;',
  22296. ' try {',
  22297. ' var $in = this.GetEnumerator();',
  22298. ' try {',
  22299. ' while ($in.MoveNext()) {',
  22300. ' Wing = rtl.setIntfL(Wing, $in.GetCurrent(), true);',
  22301. ' if (Wing.Id() === 1) ;',
  22302. ' }',
  22303. ' } finally {',
  22304. ' $in = rtl.freeLoc($in)',
  22305. ' };',
  22306. ' } finally {',
  22307. ' rtl._Release(Wing);',
  22308. ' };',
  22309. ' };',
  22310. '});',
  22311. 'this.Bird = null;',
  22312. 'this.Wing = null;',
  22313. '']),
  22314. LinesToStr([ // $mod.$main
  22315. 'var $in = $mod.Bird.GetEnumerator();',
  22316. 'try {',
  22317. ' while ($in.MoveNext()) {',
  22318. ' rtl.setIntfP($mod, "Wing", $in.GetCurrent(), true);',
  22319. ' if ($mod.Wing.Id() === 2) ;',
  22320. ' }',
  22321. '} finally {',
  22322. ' $in = rtl.freeLoc($in)',
  22323. '};',
  22324. '']));
  22325. end;
  22326. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignVar;
  22327. begin
  22328. StartProgram(false);
  22329. Add([
  22330. '{$interfaces com}',
  22331. 'type',
  22332. ' IUnknown = interface end;',
  22333. ' IBird = interface(IUnknown)',
  22334. ' function Fly(w: word): word;',
  22335. ' end;',
  22336. ' TBirdArray = array of IBird;',
  22337. 'procedure Run;',
  22338. 'var',
  22339. ' i: IBird;',
  22340. ' a: TBirdArray;',
  22341. ' b: TBirdArray = nil;',
  22342. 'begin',
  22343. ' a:=nil;',
  22344. ' a:=[];',
  22345. ' SetLength(a,3);',
  22346. ' b:=a;',
  22347. ' i:=a[1];',
  22348. ' a[2]:=i;',
  22349. 'end;',
  22350. 'begin',
  22351. '']);
  22352. ConvertProgram;
  22353. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignVar',
  22354. LinesToStr([ // statements
  22355. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22356. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22357. 'this.Run = function () {',
  22358. ' var i = null;',
  22359. ' var a = null;',
  22360. ' var b = null;',
  22361. ' try {',
  22362. ' a = rtl.setIntfL(a, null);',
  22363. ' a = rtl.setIntfL(a, null);',
  22364. ' a = rtl.arraySetLength(a, "R", 3);',
  22365. ' b = rtl.setIntfL(b, a);',
  22366. ' i = rtl.setIntfL(i, a[1]);',
  22367. ' rtl.setIntfP(a, 2, i);',
  22368. ' } finally {',
  22369. ' rtl._Release(a);',
  22370. ' rtl._Release(b);',
  22371. ' rtl._Release(i);',
  22372. ' };',
  22373. '};',
  22374. '']),
  22375. LinesToStr([ // $mod.$main
  22376. '']));
  22377. end;
  22378. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignPlus;
  22379. begin
  22380. StartProgram(false);
  22381. Add([
  22382. '{$interfaces com}',
  22383. '{$modeswitch ArrayOperators}',
  22384. 'type',
  22385. ' IUnknown = interface end;',
  22386. ' IBird = interface(IUnknown)',
  22387. ' function Fly(w: word): word;',
  22388. ' end;',
  22389. ' TBirdArray = array of IBird;',
  22390. 'procedure Run;',
  22391. 'var',
  22392. ' i: IBird;',
  22393. ' a: TBirdArray;',
  22394. ' b: TBirdArray = nil;',
  22395. 'begin',
  22396. ' a:=a+b;',
  22397. ' a:=[i,i];',
  22398. ' a:=a+[i];',
  22399. ' a:=b+[i];',
  22400. ' a:=[i]+a;',
  22401. 'end;',
  22402. 'begin',
  22403. '']);
  22404. ConvertProgram;
  22405. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignPlus',
  22406. LinesToStr([ // statements
  22407. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22408. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22409. 'this.Run = function () {',
  22410. ' var i = null;',
  22411. ' var a = null;',
  22412. ' var b = null;',
  22413. ' var $ir = rtl.createIntfRefs();',
  22414. ' try {',
  22415. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", a, b), true);',
  22416. ' a = rtl.setIntfL(a, rtl.arrayManaged(1, 2, [i, i]), true);',
  22417. ' a = rtl.setIntfL(a, rtl.arrayPush("R", a, i), true);',
  22418. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", b, $ir.ref(1, rtl.arrayManaged(1, 2, [i]))), true);',
  22419. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", $ir.ref(2, rtl.arrayManaged(1, 2, [i])), a), true);',
  22420. ' } finally {',
  22421. ' $ir.free();',
  22422. ' rtl._Release(a);',
  22423. ' };',
  22424. '};',
  22425. '']),
  22426. LinesToStr([ // $mod.$main
  22427. '']));
  22428. end;
  22429. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignArg;
  22430. begin
  22431. StartProgram(false);
  22432. Add([
  22433. '{$interfaces com}',
  22434. 'type',
  22435. ' IUnknown = interface end;',
  22436. ' IBird = interface(IUnknown)',
  22437. ' function Fly(w: word): word;',
  22438. ' end;',
  22439. ' TBirdArray = array of IBird;',
  22440. 'procedure ArgDefault(a: TBirdArray);',
  22441. 'var b: TBirdArray;',
  22442. 'begin',
  22443. ' b:=a;',
  22444. 'end;',
  22445. 'procedure ArgConst(const a: TBirdArray);',
  22446. 'begin',
  22447. 'end;',
  22448. 'procedure ArgVar(var a: TBirdArray);',
  22449. 'begin',
  22450. ' a:=nil;',
  22451. 'end;',
  22452. 'procedure ArgOut(out a: TBirdArray);',
  22453. 'begin',
  22454. 'end;',
  22455. 'procedure Run;',
  22456. 'var',
  22457. ' i: IBird;',
  22458. ' a: TBirdArray;',
  22459. 'begin',
  22460. ' ArgDefault(a);',
  22461. ' ArgDefault(nil);',
  22462. ' ArgDefault([i]);',
  22463. ' ArgConst(a);',
  22464. ' ArgConst([i]);',
  22465. ' ArgVar(a);',
  22466. ' ArgOut(a);',
  22467. 'end;',
  22468. 'begin',
  22469. '']);
  22470. ConvertProgram;
  22471. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignArg',
  22472. LinesToStr([ // statements
  22473. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22474. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22475. 'this.ArgDefault = function (a) {',
  22476. ' var b = null;',
  22477. ' rtl._AddRef(a);',
  22478. ' try {',
  22479. ' b = rtl.setIntfL(b, a);',
  22480. ' } finally {',
  22481. ' rtl._Release(a);',
  22482. ' rtl._Release(b);',
  22483. ' };',
  22484. '};',
  22485. 'this.ArgConst = function (a) {',
  22486. '};',
  22487. 'this.ArgVar = function (a) {',
  22488. ' a.set(null);',
  22489. '};',
  22490. 'this.ArgOut = function (a) {',
  22491. '};',
  22492. 'this.Run = function () {',
  22493. ' var i = null;',
  22494. ' var a = null;',
  22495. ' var $ir = rtl.createIntfRefs();',
  22496. ' try {',
  22497. ' $mod.ArgDefault(a);',
  22498. ' $mod.ArgDefault(null);',
  22499. ' $mod.ArgDefault($ir.ref(1, rtl.arrayManaged(1, 2, [i])));',
  22500. ' $mod.ArgConst(a);',
  22501. ' $mod.ArgConst($ir.ref(2, rtl.arrayManaged(1, 2, [i])));',
  22502. ' $mod.ArgVar({',
  22503. ' get: function () {',
  22504. ' return a;',
  22505. ' },',
  22506. ' set: function (v) {',
  22507. ' a = rtl.setIntfL(a, v);',
  22508. ' }',
  22509. ' });',
  22510. ' $mod.ArgOut({',
  22511. ' get: function () {',
  22512. ' return a;',
  22513. ' },',
  22514. ' set: function (v) {',
  22515. ' a = rtl.setIntfL(a, v);',
  22516. ' }',
  22517. ' });',
  22518. ' } finally {',
  22519. ' $ir.free();',
  22520. ' rtl._Release(a);',
  22521. ' };',
  22522. '};',
  22523. '']),
  22524. LinesToStr([ // $mod.$main
  22525. '']));
  22526. end;
  22527. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_InitFail;
  22528. begin
  22529. StartProgram(false);
  22530. Add([
  22531. '{$interfaces com}',
  22532. 'type',
  22533. ' IUnknown = interface',
  22534. ' function _AddRef: longint;',
  22535. ' function _Release: longint;',
  22536. ' end;',
  22537. ' TObject = class',
  22538. ' end;',
  22539. ' TBirdArray = array of IUnknown;',
  22540. 'var',
  22541. ' i: IUnknown;',
  22542. ' a: TBirdArray = (i);',
  22543. 'begin',
  22544. '']);
  22545. SetExpectedPasResolverError('Not supported: initial value of managed type',nNotSupportedX);
  22546. ConvertProgram;
  22547. end;
  22548. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_FunctionResult;
  22549. begin
  22550. StartProgram(false);
  22551. Add([
  22552. '{$interfaces com}',
  22553. 'type',
  22554. ' IUnknown = interface',
  22555. ' function _AddRef: longint;',
  22556. ' function _Release: longint;',
  22557. ' end;',
  22558. ' TObject = class end;',
  22559. ' TBird = array of IUnknown;',
  22560. 'function DoDefault(i: TBird): TBird;',
  22561. 'begin',
  22562. ' Result:=i;',
  22563. ' if Result<>nil then exit;',
  22564. 'end;',
  22565. 'var b: TBird;',
  22566. 'begin',
  22567. ' DoDefault(b);',
  22568. ' b:=DoDefault(b);',
  22569. '']);
  22570. ConvertProgram;
  22571. CheckSource('TestClassInterface_COM_ArrayOfIntf_FunctionResult',
  22572. LinesToStr([ // statements
  22573. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22574. 'rtl.createClass(this, "TObject", null, function () {',
  22575. ' this.$init = function () {',
  22576. ' };',
  22577. ' this.$final = function () {',
  22578. ' };',
  22579. '});',
  22580. 'this.DoDefault = function (i) {',
  22581. ' var Result = null;',
  22582. ' rtl._AddRef(i);',
  22583. ' var $ok = false;',
  22584. ' try {',
  22585. ' Result = rtl.setIntfL(Result, i);',
  22586. ' if (rtl.length(Result) > 0) {',
  22587. ' $ok = true;',
  22588. ' return Result;',
  22589. ' };',
  22590. ' $ok = true;',
  22591. ' } finally {',
  22592. ' rtl._Release(i);',
  22593. ' if(!$ok) rtl._Release(Result);',
  22594. ' };',
  22595. ' return Result;',
  22596. '};',
  22597. 'this.b = null;',
  22598. '']),
  22599. LinesToStr([ // $mod.$main
  22600. 'var $ir = rtl.createIntfRefs();',
  22601. 'try {',
  22602. ' $ir.ref(1, $mod.DoDefault($mod.b));',
  22603. ' rtl.setIntfP($mod, "b", $mod.DoDefault($mod.b), true);',
  22604. '} finally {',
  22605. ' $ir.free();',
  22606. '};',
  22607. '']));
  22608. end;
  22609. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult;
  22610. begin
  22611. StartProgram(false);
  22612. Add([
  22613. '{$interfaces com}',
  22614. 'type',
  22615. ' IUnknown = interface',
  22616. ' function _AddRef: longint;',
  22617. ' function _Release: longint;',
  22618. ' end;',
  22619. ' TBird = array of IUnknown;',
  22620. ' TObject = class',
  22621. ' function GetIntf: TBird; virtual;',
  22622. ' end;',
  22623. ' TMouse = class',
  22624. ' function GetIntf: TBird; override;',
  22625. ' end;',
  22626. 'function TObject.GetIntf: TBird; begin end;',
  22627. 'function TMouse.GetIntf: TBird;',
  22628. 'var i: TBird;',
  22629. 'begin',
  22630. ' inherited;',
  22631. ' inherited GetIntf;',
  22632. ' inherited GetIntf();',
  22633. ' Result:=inherited GetIntf;',
  22634. ' Result:=inherited GetIntf();',
  22635. ' i:=inherited GetIntf;',
  22636. ' i:=inherited GetIntf();',
  22637. 'end;',
  22638. 'begin',
  22639. '']);
  22640. ConvertProgram;
  22641. CheckSource('TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult',
  22642. LinesToStr([ // statements
  22643. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22644. 'rtl.createClass(this, "TObject", null, function () {',
  22645. ' this.$init = function () {',
  22646. ' };',
  22647. ' this.$final = function () {',
  22648. ' };',
  22649. ' this.GetIntf = function () {',
  22650. ' var Result = null;',
  22651. ' return Result;',
  22652. ' };',
  22653. '});',
  22654. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  22655. ' this.GetIntf = function () {',
  22656. ' var Result = null;',
  22657. ' var i = null;',
  22658. ' var $ir = rtl.createIntfRefs();',
  22659. ' var $ok = false;',
  22660. ' try {',
  22661. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  22662. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  22663. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  22664. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  22665. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  22666. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  22667. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  22668. ' $ok = true;',
  22669. ' } finally {',
  22670. ' $ir.free();',
  22671. ' rtl._Release(i);',
  22672. ' if (!$ok) rtl._Release(Result);',
  22673. ' };',
  22674. ' return Result;',
  22675. ' };',
  22676. '});',
  22677. '']),
  22678. LinesToStr([ // $mod.$main
  22679. '']));
  22680. end;
  22681. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_FunctionExit;
  22682. begin
  22683. StartProgram(false);
  22684. Add([
  22685. '{$interfaces com}',
  22686. 'type',
  22687. ' IUnknown = interface',
  22688. ' function _AddRef: longint;',
  22689. ' function _Release: longint;',
  22690. ' end;',
  22691. ' TBird = array of IUnknown;',
  22692. ' TObject = class',
  22693. ' constructor Create;',
  22694. ' end;',
  22695. 'constructor TObject.Create;',
  22696. 'begin',
  22697. 'end;',
  22698. 'function GetIntf: TBird;',
  22699. 'var b: TBird;',
  22700. 'begin',
  22701. ' b:=[];',
  22702. ' Exit(b);',
  22703. 'end;',
  22704. 'begin',
  22705. '']);
  22706. ConvertProgram;
  22707. CheckSource('TestClassInterface_COM_ArrayOfIntf_FunctionExit',
  22708. LinesToStr([ // statements
  22709. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22710. 'rtl.createClass(this, "TObject", null, function () {',
  22711. ' this.$init = function () {',
  22712. ' };',
  22713. ' this.$final = function () {',
  22714. ' };',
  22715. ' this.Create = function () {',
  22716. ' return this;',
  22717. ' };',
  22718. '});',
  22719. 'this.GetIntf = function () {',
  22720. ' var Result = null;',
  22721. ' var b = null;',
  22722. ' var $ok = false;',
  22723. ' try {',
  22724. ' b = rtl.setIntfL(b, null);',
  22725. ' $ok = true;',
  22726. ' Result = rtl.setIntfL(Result, b);',
  22727. ' return Result;',
  22728. ' $ok = true;',
  22729. ' } finally {',
  22730. ' rtl._Release(b);',
  22731. ' if (!$ok) rtl._Release(Result);',
  22732. ' };',
  22733. ' return Result;',
  22734. '};',
  22735. '']),
  22736. LinesToStr([ // $mod.$main
  22737. '']));
  22738. end;
  22739. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_Property;
  22740. begin
  22741. StartProgram(false);
  22742. Add([
  22743. '{$interfaces com}',
  22744. 'type',
  22745. ' IUnknown = interface',
  22746. ' function _AddRef: longint;',
  22747. ' function _Release: longint;',
  22748. ' end;',
  22749. ' TAnimal = array of IUnknown;',
  22750. ' TObject = class',
  22751. ' FAnt: TAnimal;',
  22752. ' function GetBird: TAnimal; virtual; abstract;',
  22753. ' procedure SetBird(Value: TAnimal); virtual; abstract;',
  22754. ' function GetItems(Index: longint): TAnimal; virtual; abstract;',
  22755. ' procedure SetItems(Index: longint; Value: TAnimal); virtual; abstract;',
  22756. ' property Ant: TAnimal read FAnt write FAnt;',
  22757. ' property Bird: TAnimal read GetBird write SetBird;',
  22758. ' property Items[Index: longint]: TAnimal read GetItems write SetItems; default;',
  22759. ' end;',
  22760. 'procedure DoIt;',
  22761. 'var',
  22762. ' o: TObject;',
  22763. ' v: TAnimal;',
  22764. 'begin',
  22765. ' v:=o.Ant;',
  22766. ' o.Ant:=v;',
  22767. ' o.Ant:=o.Ant;',
  22768. ' v:=o.Bird;',
  22769. ' o.Bird:=v;',
  22770. ' o.Bird:=o.Bird;',
  22771. ' v:=o.Items[1];',
  22772. ' o.Items[2]:=v;',
  22773. ' o.Items[3]:=o.Items[4];',
  22774. ' v:=o[5];',
  22775. ' o[6]:=v;',
  22776. ' o[7]:=o[8];',
  22777. 'end;',
  22778. 'begin',
  22779. '']);
  22780. ConvertProgram;
  22781. CheckSource('TestClassInterface_COM_ArrayOfIntf_Property',
  22782. LinesToStr([ // statements
  22783. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22784. 'rtl.createClass(this, "TObject", null, function () {',
  22785. ' this.$init = function () {',
  22786. ' this.FAnt = null;',
  22787. ' };',
  22788. ' this.$final = function () {',
  22789. ' rtl.setIntfP(this, "FAnt", null);',
  22790. ' };',
  22791. '});',
  22792. 'this.DoIt = function () {',
  22793. ' var o = null;',
  22794. ' var v = null;',
  22795. ' var $ir = rtl.createIntfRefs();',
  22796. ' try {',
  22797. ' v = rtl.setIntfL(v, o.FAnt);',
  22798. ' rtl.setIntfP(o, "FAnt", v);',
  22799. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  22800. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  22801. ' o.SetBird(v);',
  22802. ' o.SetBird($ir.ref(1, o.GetBird()));',
  22803. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  22804. ' o.SetItems(2, v);',
  22805. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  22806. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  22807. ' o.SetItems(6, v);',
  22808. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  22809. ' } finally {',
  22810. ' $ir.free();',
  22811. ' rtl._Release(v);',
  22812. ' };',
  22813. '};',
  22814. '']),
  22815. LinesToStr([ // $mod.$main
  22816. '']));
  22817. end;
  22818. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_BIFuncs;
  22819. begin
  22820. StartProgram(false);
  22821. Add([
  22822. '{$interfaces com}',
  22823. 'type',
  22824. ' IUnknown = interface end;',
  22825. ' IBird = interface(IUnknown)',
  22826. ' end;',
  22827. ' TBirdArray = array of IBird;',
  22828. 'procedure Run;',
  22829. 'var',
  22830. ' i: IBird;',
  22831. ' a, b: TBirdArray;',
  22832. 'begin',
  22833. ' SetLength(a,3);',
  22834. ' a:=copy(b,1,2);',
  22835. ' a:=concat(b);',
  22836. ' a:=concat(b,a);',
  22837. ' insert(i,b,1);',
  22838. ' delete(a,1,2);', // array,index,count
  22839. 'end;',
  22840. 'begin',
  22841. '']);
  22842. ConvertProgram;
  22843. CheckSource('TestClassInterface_COM_ArrayOfIntf_BIFuncs',
  22844. LinesToStr([ // statements
  22845. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22846. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  22847. 'this.Run = function () {',
  22848. ' var i = null;',
  22849. ' var a = null;',
  22850. ' var b = null;',
  22851. ' try {',
  22852. ' a = rtl.arraySetLength(a, "R", 3);',
  22853. ' a = rtl.setIntfL(a, rtl.arrayCopy("R", b, 1, 2), true);',
  22854. ' a = rtl.setIntfL(a, b);',
  22855. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", b, a), true);',
  22856. ' b = rtl.arrayInsert(i, b, 1, "R");',
  22857. ' a = rtl.arrayDeleteR(a, 1, 2);',
  22858. ' } finally {',
  22859. ' rtl._Release(a);',
  22860. ' };',
  22861. '};',
  22862. '']),
  22863. LinesToStr([ // $mod.$main
  22864. '']));
  22865. end;
  22866. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_ForIn;
  22867. begin
  22868. StartProgram(false);
  22869. Add([
  22870. '{$interfaces com}',
  22871. 'type',
  22872. ' IUnknown = interface end;',
  22873. ' IBird = interface(IUnknown)',
  22874. ' end;',
  22875. ' TBirdArray = array of IBird;',
  22876. 'procedure Run;',
  22877. 'var',
  22878. ' i, j: IBird;',
  22879. ' a: TBirdArray;',
  22880. 'begin',
  22881. ' for i in a do begin',
  22882. ' j:=i;',
  22883. ' end;',
  22884. 'end;',
  22885. 'begin',
  22886. '']);
  22887. ConvertProgram;
  22888. CheckSource('TestClassInterface_COM_ArrayOfIntf_ForIn',
  22889. LinesToStr([ // statements
  22890. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22891. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  22892. 'this.Run = function () {',
  22893. ' var i = null;',
  22894. ' var j = null;',
  22895. ' var a = null;',
  22896. ' try {',
  22897. ' for (var $in = a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  22898. ' i = rtl.setIntfL(i, $in[$l]);',
  22899. ' j = rtl.setIntfL(j, i);',
  22900. ' };',
  22901. ' } finally {',
  22902. ' rtl._Release(i);',
  22903. ' rtl._Release(j);',
  22904. ' };',
  22905. '};',
  22906. '']),
  22907. LinesToStr([ // $mod.$main
  22908. '']));
  22909. end;
  22910. procedure TTestModule.TestClassInterface_COM_StaticArrayOfIntfFail;
  22911. begin
  22912. StartProgram(false);
  22913. Add([
  22914. '{$interfaces com}',
  22915. 'type',
  22916. ' IUnknown = interface',
  22917. ' function _AddRef: longint;',
  22918. ' function _Release: longint;',
  22919. ' end;',
  22920. ' TObject = class',
  22921. ' end;',
  22922. ' TArrOfIntf = array[0..1] of IUnknown;',
  22923. 'begin',
  22924. '']);
  22925. SetExpectedPasResolverError('Not supported: static array of COM-interface',nNotSupportedX);
  22926. ConvertProgram;
  22927. end;
  22928. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22929. begin
  22930. StartProgram(false);
  22931. Add([
  22932. '{$interfaces com}',
  22933. 'type',
  22934. ' IUnknown = interface',
  22935. ' function _AddRef: longint;',
  22936. ' function _Release: longint;',
  22937. ' end;',
  22938. ' TRec = record',
  22939. ' i: IUnknown;',
  22940. ' end;',
  22941. 'begin',
  22942. '']);
  22943. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22944. ConvertProgram;
  22945. end;
  22946. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22947. begin
  22948. StartUnit(false);
  22949. Add([
  22950. '{$interfaces com}',
  22951. 'interface',
  22952. 'implementation',
  22953. 'type',
  22954. ' IUnknown = interface',
  22955. ' function _AddRef: longint;',
  22956. ' end;',
  22957. ' TObject = class(IUnknown)',
  22958. ' function _AddRef: longint;',
  22959. ' end;',
  22960. 'function TObject._AddRef: longint; begin end;',
  22961. 'var i: IUnknown;',
  22962. ' o: TObject;',
  22963. 'initialization',
  22964. ' i:=nil;',
  22965. ' i:=i;',
  22966. ' i:=o;',
  22967. ' if (o as IUnknown)=nil then ;',
  22968. '']);
  22969. ConvertUnit;
  22970. CheckSource('TestClassInterface_COM_UnitInitialization',
  22971. LinesToStr([ // statements
  22972. 'var $impl = $mod.$impl;',
  22973. '']),
  22974. LinesToStr([ // this.$init
  22975. 'var $ir = rtl.createIntfRefs();',
  22976. 'try {',
  22977. ' rtl.setIntfP($impl, "i", null);',
  22978. ' rtl.setIntfP($impl, "i", $impl.i);',
  22979. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22980. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22981. '} finally {',
  22982. ' $ir.free();',
  22983. '};',
  22984. '']),
  22985. LinesToStr([ // implementation
  22986. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22987. 'rtl.createClass($impl, "TObject", null, function () {',
  22988. ' this.$init = function () {',
  22989. ' };',
  22990. ' this.$final = function () {',
  22991. ' };',
  22992. ' this._AddRef = function () {',
  22993. ' var Result = 0;',
  22994. ' return Result;',
  22995. ' };',
  22996. ' rtl.addIntf(this, $impl.IUnknown);',
  22997. '});',
  22998. '$impl.i = null;',
  22999. '$impl.o = null;',
  23000. ''])
  23001. );
  23002. end;
  23003. procedure TTestModule.TestClassInterface_Corba_GUID;
  23004. begin
  23005. StartProgram(false);
  23006. Add([
  23007. '{$interfaces corba}',
  23008. 'type',
  23009. ' IUnknown = interface',
  23010. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  23011. ' end;',
  23012. ' TObject = class end;',
  23013. ' TGUID = record D1, D2, D3, D4: word; end;',
  23014. ' TAliasGUID = TGUID;',
  23015. ' TGUIDString = type string;',
  23016. ' TAliasGUIDString = TGUIDString;',
  23017. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  23018. 'begin end;',
  23019. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  23020. 'begin end;',
  23021. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  23022. 'begin end;',
  23023. 'var',
  23024. ' i: IUnknown;',
  23025. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  23026. ' s: TAliasGUIDString;',
  23027. 'begin',
  23028. ' DoConstGUIDIt(IUnknown);',
  23029. ' DoDefGUID(IUnknown);',
  23030. ' DoStr(IUnknown);',
  23031. ' DoConstGUIDIt(i);',
  23032. ' DoDefGUID(i);',
  23033. ' DoStr(i);',
  23034. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  23035. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  23036. ' DoStr(g);',
  23037. ' g:=i;',
  23038. ' g:=IUnknown;',
  23039. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  23040. ' s:=i;',
  23041. ' s:=IUnknown;',
  23042. ' s:=g;',
  23043. ' if g=i then ;',
  23044. ' if i=g then ;',
  23045. ' if g=IUnknown then ;',
  23046. ' if IUnknown=g then ;',
  23047. ' if s=i then ;',
  23048. ' if i=s then ;',
  23049. ' if s=IUnknown then ;',
  23050. ' if IUnknown=s then ;',
  23051. ' if s=g then ;',
  23052. ' if g=s then ;',
  23053. '']);
  23054. ConvertProgram;
  23055. CheckSource('TestClassInterface_Corba_GUID',
  23056. LinesToStr([ // statements
  23057. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  23058. 'rtl.createClass(this, "TObject", null, function () {',
  23059. ' this.$init = function () {',
  23060. ' };',
  23061. ' this.$final = function () {',
  23062. ' };',
  23063. '});',
  23064. 'rtl.recNewT(this, "TGUID", function () {',
  23065. ' this.D1 = 0;',
  23066. ' this.D2 = 0;',
  23067. ' this.D3 = 0;',
  23068. ' this.D4 = 0;',
  23069. ' this.$eq = function (b) {',
  23070. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  23071. ' };',
  23072. ' this.$assign = function (s) {',
  23073. ' this.D1 = s.D1;',
  23074. ' this.D2 = s.D2;',
  23075. ' this.D3 = s.D3;',
  23076. ' this.D4 = s.D4;',
  23077. ' return this;',
  23078. ' };',
  23079. '});',
  23080. 'this.DoConstGUIDIt = function (g) {',
  23081. '};',
  23082. 'this.DoDefGUID = function (g) {',
  23083. '};',
  23084. 'this.DoStr = function (s) {',
  23085. '};',
  23086. 'this.i = null;',
  23087. 'this.g = this.TGUID.$clone({',
  23088. ' D1: 0xD91C9AF4,',
  23089. ' D2: 0x3C93,',
  23090. ' D3: 0x420F,',
  23091. ' D4: [',
  23092. ' 0xA3,',
  23093. ' 0x03,',
  23094. ' 0xBF,',
  23095. ' 0x5B,',
  23096. ' 0xA8,',
  23097. ' 0x2B,',
  23098. ' 0xFD,',
  23099. ' 0x23',
  23100. ' ]',
  23101. '});',
  23102. 'this.s = "";',
  23103. '']),
  23104. LinesToStr([ // $mod.$main
  23105. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  23106. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  23107. '$mod.DoStr($mod.IUnknown.$guid);',
  23108. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  23109. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  23110. '$mod.DoStr($mod.i.$guid);',
  23111. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  23112. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  23113. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  23114. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  23115. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  23116. '$mod.g.$assign({',
  23117. ' D1: 0xD91C9AF4,',
  23118. ' D2: 0x3C93,',
  23119. ' D3: 0x420F,',
  23120. ' D4: [',
  23121. ' 0xA3,',
  23122. ' 0x03,',
  23123. ' 0xBF,',
  23124. ' 0x5B,',
  23125. ' 0xA8,',
  23126. ' 0x2B,',
  23127. ' 0xFD,',
  23128. ' 0x23',
  23129. ' ]',
  23130. '});',
  23131. '$mod.s = $mod.i.$guid;',
  23132. '$mod.s = $mod.IUnknown.$guid;',
  23133. '$mod.s = rtl.guidrToStr($mod.g);',
  23134. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  23135. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  23136. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  23137. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  23138. 'if ($mod.s === $mod.i.$guid) ;',
  23139. 'if ($mod.i.$guid === $mod.s) ;',
  23140. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  23141. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  23142. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  23143. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  23144. '']));
  23145. end;
  23146. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  23147. begin
  23148. StartProgram(false);
  23149. Add([
  23150. '{$interfaces corba}',
  23151. 'type',
  23152. ' IUnknown = interface',
  23153. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  23154. ' end;',
  23155. ' TGUID = record D1, D2, D3, D4: word; end;',
  23156. ' TAliasGUID = TGUID;',
  23157. ' TGUIDString = type string;',
  23158. ' TAliasGUIDString = TGUIDString;',
  23159. ' TObject = class',
  23160. ' function GetG: TAliasGUID; virtual; abstract;',
  23161. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  23162. ' function GetS: TAliasGUIDString; virtual; abstract;',
  23163. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  23164. ' property g: TAliasGUID read GetG write SetG;',
  23165. ' property s: TAliasGUIDString read GetS write SetS;',
  23166. ' end;',
  23167. 'var o: TObject;',
  23168. 'begin',
  23169. ' o.g:=IUnknown;',
  23170. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  23171. ' o.s:=IUnknown;',
  23172. ' o.s:=o.g;',
  23173. '']);
  23174. ConvertProgram;
  23175. CheckSource('TestClassInterface_GUIDProperty',
  23176. LinesToStr([ // statements
  23177. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  23178. 'rtl.recNewT(this, "TGUID", function () {',
  23179. ' this.D1 = 0;',
  23180. ' this.D2 = 0;',
  23181. ' this.D3 = 0;',
  23182. ' this.D4 = 0;',
  23183. ' this.$eq = function (b) {',
  23184. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  23185. ' };',
  23186. ' this.$assign = function (s) {',
  23187. ' this.D1 = s.D1;',
  23188. ' this.D2 = s.D2;',
  23189. ' this.D3 = s.D3;',
  23190. ' this.D4 = s.D4;',
  23191. ' return this;',
  23192. ' };',
  23193. '});',
  23194. 'rtl.createClass(this, "TObject", null, function () {',
  23195. ' this.$init = function () {',
  23196. ' };',
  23197. ' this.$final = function () {',
  23198. ' };',
  23199. '});',
  23200. 'this.o = null;',
  23201. '']),
  23202. LinesToStr([ // $mod.$main
  23203. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  23204. '$mod.o.SetG({',
  23205. ' D1: 0xD91C9AF4,',
  23206. ' D2: 0x3C93,',
  23207. ' D3: 0x420F,',
  23208. ' D4: [',
  23209. ' 0xA3,',
  23210. ' 0x03,',
  23211. ' 0xBF,',
  23212. ' 0x5B,',
  23213. ' 0xA8,',
  23214. ' 0x2B,',
  23215. ' 0xFD,',
  23216. ' 0x23',
  23217. ' ]',
  23218. '});',
  23219. '$mod.o.SetS($mod.IUnknown.$guid);',
  23220. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  23221. '']));
  23222. end;
  23223. procedure TTestModule.TestClassHelper_ClassVar;
  23224. begin
  23225. StartProgram(false);
  23226. Add([
  23227. 'type',
  23228. ' TObject = class',
  23229. ' end;',
  23230. ' THelper = class helper for TObject',
  23231. ' const',
  23232. ' One = 1;',
  23233. ' Two: word = 2;',
  23234. ' class var',
  23235. ' Glob: word;',
  23236. ' function Foo(w: word): word;',
  23237. ' class function Bar(w: word): word;',
  23238. ' end;',
  23239. 'function THelper.foo(w: word): word;',
  23240. 'begin',
  23241. ' Result:=w;',
  23242. ' Two:=One+w;',
  23243. ' Glob:=Glob;',
  23244. ' Result:=Self.Glob;',
  23245. ' Self.Glob:=Self.Glob;',
  23246. ' with Self do Glob:=Glob;',
  23247. 'end;',
  23248. 'class function THelper.bar(w: word): word;',
  23249. 'begin',
  23250. ' Result:=w;',
  23251. ' Two:=One;',
  23252. ' Glob:=Glob;',
  23253. ' Self.Glob:=Self.Glob;',
  23254. ' with Self do Glob:=Glob;',
  23255. 'end;',
  23256. 'var o: TObject;',
  23257. 'begin',
  23258. ' tobject.two:=tobject.one;',
  23259. ' tobject.Glob:=tobject.Glob;',
  23260. ' with tobject do begin',
  23261. ' two:=one;',
  23262. ' Glob:=Glob;',
  23263. ' end;',
  23264. ' o.two:=o.one;',
  23265. ' o.Glob:=o.Glob;',
  23266. ' with o do begin',
  23267. ' two:=one;',
  23268. ' Glob:=Glob;',
  23269. ' end;',
  23270. '']);
  23271. ConvertProgram;
  23272. CheckSource('TestClassHelper_ClassVar',
  23273. LinesToStr([ // statements
  23274. 'rtl.createClass(this, "TObject", null, function () {',
  23275. ' this.$init = function () {',
  23276. ' };',
  23277. ' this.$final = function () {',
  23278. ' };',
  23279. '});',
  23280. 'rtl.createHelper(this, "THelper", null, function () {',
  23281. ' this.One = 1;',
  23282. ' this.Two = 2;',
  23283. ' this.Glob = 0;',
  23284. ' this.Foo = function (w) {',
  23285. ' var Result = 0;',
  23286. ' Result = w;',
  23287. ' $mod.THelper.Two = 1 + w;',
  23288. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23289. ' Result = $mod.THelper.Glob;',
  23290. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23291. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23292. ' return Result;',
  23293. ' };',
  23294. ' this.Bar = function (w) {',
  23295. ' var Result = 0;',
  23296. ' Result = w;',
  23297. ' $mod.THelper.Two = 1;',
  23298. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23299. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23300. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23301. ' return Result;',
  23302. ' };',
  23303. '});',
  23304. 'this.o = null;',
  23305. '']),
  23306. LinesToStr([ // $mod.$main
  23307. '$mod.THelper.Two = 1;',
  23308. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23309. 'var $with = $mod.TObject;',
  23310. '$mod.THelper.Two = 1;',
  23311. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23312. '$mod.THelper.Two = 1;',
  23313. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23314. 'var $with1 = $mod.o;',
  23315. '$mod.THelper.Two = 1;',
  23316. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23317. '']));
  23318. end;
  23319. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  23320. begin
  23321. StartProgram(false);
  23322. Add([
  23323. 'type',
  23324. ' TObject = class',
  23325. ' FSize: word;',
  23326. ' property Size: word read FSize write FSize;',
  23327. ' end;',
  23328. ' THelper = class helper for TObject',
  23329. ' function Foo(w: word = 1): word;',
  23330. ' end;',
  23331. 'function THelper.foo(w: word): word;',
  23332. 'begin',
  23333. ' Result:=Size;',
  23334. ' Size:=Size+2;',
  23335. ' Self.Size:=Self.Size+3;',
  23336. ' FSize:=FSize+4;',
  23337. ' Self.FSize:=Self.FSize+5;',
  23338. ' with Self do begin',
  23339. ' Size:=Size+6;',
  23340. ' FSize:=FSize+7;',
  23341. ' FSize:=FSize+8;',
  23342. ' end;',
  23343. 'end;',
  23344. 'begin',
  23345. '']);
  23346. ConvertProgram;
  23347. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  23348. LinesToStr([ // statements
  23349. 'rtl.createClass(this, "TObject", null, function () {',
  23350. ' this.$init = function () {',
  23351. ' this.FSize = 0;',
  23352. ' };',
  23353. ' this.$final = function () {',
  23354. ' };',
  23355. '});',
  23356. 'rtl.createHelper(this, "THelper", null, function () {',
  23357. ' this.Foo = function (w) {',
  23358. ' var Result = 0;',
  23359. ' Result = this.FSize;',
  23360. ' this.FSize = this.FSize + 2;',
  23361. ' this.FSize = this.FSize + 3;',
  23362. ' this.FSize = this.FSize + 4;',
  23363. ' this.FSize = this.FSize + 5;',
  23364. ' this.FSize = this.FSize + 6;',
  23365. ' this.FSize = this.FSize + 7;',
  23366. ' this.FSize = this.FSize + 8;',
  23367. ' return Result;',
  23368. ' };',
  23369. '});',
  23370. '']),
  23371. LinesToStr([ // $mod.$main
  23372. '']));
  23373. end;
  23374. procedure TTestModule.TestClassHelper_Method_Call;
  23375. begin
  23376. StartProgram(false);
  23377. Add([
  23378. 'type',
  23379. ' TObject = class',
  23380. ' procedure Run(w: word = 10);',
  23381. ' end;',
  23382. ' THelper = class helper for TObject',
  23383. ' function Foo(w: word = 1): word;',
  23384. ' end;',
  23385. 'procedure TObject.Run(w: word);',
  23386. 'var o: TObject;',
  23387. 'begin',
  23388. ' Foo;',
  23389. ' Foo();',
  23390. ' Foo(2);',
  23391. ' Self.Foo;',
  23392. ' Self.Foo();',
  23393. ' Self.Foo(3);',
  23394. ' with Self do begin',
  23395. ' Foo;',
  23396. ' Foo();',
  23397. ' Foo(4);',
  23398. ' end;',
  23399. ' with o do Foo(5);',
  23400. 'end;',
  23401. 'function THelper.foo(w: word): word;',
  23402. 'begin',
  23403. ' Run;',
  23404. ' Run();',
  23405. ' Run(11);',
  23406. ' Foo;',
  23407. ' Foo();',
  23408. ' Foo(12);',
  23409. ' Self.Foo;',
  23410. ' Self.Foo();',
  23411. ' Self.Foo(13);',
  23412. ' with Self do begin',
  23413. ' Foo;',
  23414. ' Foo();',
  23415. ' Foo(14);',
  23416. ' end;',
  23417. 'end;',
  23418. 'var Obj: TObject;',
  23419. 'begin',
  23420. ' obj.Foo;',
  23421. ' obj.Foo();',
  23422. ' obj.Foo(21);',
  23423. ' with obj do begin',
  23424. ' Foo;',
  23425. ' Foo();',
  23426. ' Foo(22);',
  23427. ' end;',
  23428. '']);
  23429. ConvertProgram;
  23430. CheckSource('TestClassHelper_Method_Call',
  23431. LinesToStr([ // statements
  23432. 'rtl.createClass(this, "TObject", null, function () {',
  23433. ' this.$init = function () {',
  23434. ' };',
  23435. ' this.$final = function () {',
  23436. ' };',
  23437. ' this.Run = function (w) {',
  23438. ' var o = null;',
  23439. ' $mod.THelper.Foo.call(this, 1);',
  23440. ' $mod.THelper.Foo.call(this, 1);',
  23441. ' $mod.THelper.Foo.call(this, 2);',
  23442. ' $mod.THelper.Foo.call(this, 1);',
  23443. ' $mod.THelper.Foo.call(this, 1);',
  23444. ' $mod.THelper.Foo.call(this, 3);',
  23445. ' $mod.THelper.Foo.call(this, 1);',
  23446. ' $mod.THelper.Foo.call(this, 1);',
  23447. ' $mod.THelper.Foo.call(this, 4);',
  23448. ' $mod.THelper.Foo.call(o, 5);',
  23449. ' };',
  23450. '});',
  23451. 'rtl.createHelper(this, "THelper", null, function () {',
  23452. ' this.Foo = function (w) {',
  23453. ' var Result = 0;',
  23454. ' this.Run(10);',
  23455. ' this.Run(10);',
  23456. ' this.Run(11);',
  23457. ' $mod.THelper.Foo.call(this, 1);',
  23458. ' $mod.THelper.Foo.call(this, 1);',
  23459. ' $mod.THelper.Foo.call(this, 12);',
  23460. ' $mod.THelper.Foo.call(this, 1);',
  23461. ' $mod.THelper.Foo.call(this, 1);',
  23462. ' $mod.THelper.Foo.call(this, 13);',
  23463. ' $mod.THelper.Foo.call(this, 1);',
  23464. ' $mod.THelper.Foo.call(this, 1);',
  23465. ' $mod.THelper.Foo.call(this, 14);',
  23466. ' return Result;',
  23467. ' };',
  23468. '});',
  23469. 'this.Obj = null;',
  23470. '']),
  23471. LinesToStr([ // $mod.$main
  23472. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23473. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23474. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23475. 'var $with = $mod.Obj;',
  23476. '$mod.THelper.Foo.call($with, 1);',
  23477. '$mod.THelper.Foo.call($with, 1);',
  23478. '$mod.THelper.Foo.call($with, 22);',
  23479. '']));
  23480. end;
  23481. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  23482. begin
  23483. StartProgram(false);
  23484. Add([
  23485. 'type',
  23486. ' TObject = class',
  23487. ' procedure Run(w: word = 10);',
  23488. ' end;',
  23489. ' THelper = class helper for TObject',
  23490. ' function Foo(w: word = 1): word;',
  23491. ' end;',
  23492. 'procedure TObject.Run(w: word);',
  23493. ' procedure Sub(Self: TObject);',
  23494. ' begin',
  23495. ' Foo;',
  23496. ' Foo();',
  23497. ' Self.Foo;',
  23498. ' Self.Foo();',
  23499. ' with Self do begin',
  23500. ' Foo;',
  23501. ' Foo();',
  23502. ' end;',
  23503. ' end;',
  23504. 'begin',
  23505. 'end;',
  23506. 'function THelper.foo(w: word): word;',
  23507. ' procedure Sub(Self: TObject);',
  23508. ' begin',
  23509. ' Run;',
  23510. ' Run();',
  23511. ' Foo;',
  23512. ' Foo();',
  23513. ' Self.Foo;',
  23514. ' Self.Foo();',
  23515. ' with Self do begin',
  23516. ' Foo;',
  23517. ' Foo();',
  23518. ' end;',
  23519. ' end;',
  23520. 'begin',
  23521. 'end;',
  23522. 'begin',
  23523. '']);
  23524. ConvertProgram;
  23525. CheckSource('TestClassHelper_Method_Nested_Call',
  23526. LinesToStr([ // statements
  23527. 'rtl.createClass(this, "TObject", null, function () {',
  23528. ' this.$init = function () {',
  23529. ' };',
  23530. ' this.$final = function () {',
  23531. ' };',
  23532. ' this.Run = function (w) {',
  23533. ' var $Self = this;',
  23534. ' function Sub(Self) {',
  23535. ' $mod.THelper.Foo.call($Self, 1);',
  23536. ' $mod.THelper.Foo.call($Self, 1);',
  23537. ' $mod.THelper.Foo.call(Self, 1);',
  23538. ' $mod.THelper.Foo.call(Self, 1);',
  23539. ' $mod.THelper.Foo.call(Self, 1);',
  23540. ' $mod.THelper.Foo.call(Self, 1);',
  23541. ' };',
  23542. ' };',
  23543. '});',
  23544. 'rtl.createHelper(this, "THelper", null, function () {',
  23545. ' this.Foo = function (w) {',
  23546. ' var $Self = this;',
  23547. ' var Result = 0;',
  23548. ' function Sub(Self) {',
  23549. ' $Self.Run(10);',
  23550. ' $Self.Run(10);',
  23551. ' $mod.THelper.Foo.call($Self, 1);',
  23552. ' $mod.THelper.Foo.call($Self, 1);',
  23553. ' $mod.THelper.Foo.call(Self, 1);',
  23554. ' $mod.THelper.Foo.call(Self, 1);',
  23555. ' $mod.THelper.Foo.call(Self, 1);',
  23556. ' $mod.THelper.Foo.call(Self, 1);',
  23557. ' };',
  23558. ' return Result;',
  23559. ' };',
  23560. '});',
  23561. '']),
  23562. LinesToStr([ // $mod.$main
  23563. '']));
  23564. end;
  23565. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  23566. begin
  23567. StartProgram(false);
  23568. Add([
  23569. 'type',
  23570. ' TObject = class',
  23571. ' class procedure Run(w: word = 10);',
  23572. ' end;',
  23573. ' THelper = class helper for TObject',
  23574. ' class function Foo(w: word = 1): word;',
  23575. ' end;',
  23576. 'class procedure TObject.Run(w: word);',
  23577. 'begin',
  23578. ' Foo;',
  23579. ' Foo();',
  23580. ' Self.Foo;',
  23581. ' Self.Foo();',
  23582. ' with Self do begin',
  23583. ' Foo;',
  23584. ' Foo();',
  23585. ' end;',
  23586. 'end;',
  23587. 'class function THelper.foo(w: word): word;',
  23588. 'begin',
  23589. ' Run;',
  23590. ' Run();',
  23591. ' Foo;',
  23592. ' Foo();',
  23593. ' Self.Foo;',
  23594. ' Self.Foo();',
  23595. ' with Self do begin',
  23596. ' Foo;',
  23597. ' Foo();',
  23598. ' end;',
  23599. 'end;',
  23600. 'var',
  23601. ' Obj: TObject;',
  23602. 'begin',
  23603. ' obj.Foo;',
  23604. ' obj.Foo();',
  23605. ' with obj do begin',
  23606. ' Foo;',
  23607. ' Foo();',
  23608. ' end;',
  23609. ' tobject.Foo;',
  23610. ' tobject.Foo();',
  23611. ' with tobject do begin',
  23612. ' Foo;',
  23613. ' Foo();',
  23614. ' end;',
  23615. '']);
  23616. ConvertProgram;
  23617. CheckSource('TestClassHelper_ClassMethod_Call',
  23618. LinesToStr([ // statements
  23619. 'rtl.createClass(this, "TObject", null, function () {',
  23620. ' this.$init = function () {',
  23621. ' };',
  23622. ' this.$final = function () {',
  23623. ' };',
  23624. ' this.Run = function (w) {',
  23625. ' $mod.THelper.Foo.call(this, 1);',
  23626. ' $mod.THelper.Foo.call(this, 1);',
  23627. ' $mod.THelper.Foo.call(this, 1);',
  23628. ' $mod.THelper.Foo.call(this, 1);',
  23629. ' $mod.THelper.Foo.call(this, 1);',
  23630. ' $mod.THelper.Foo.call(this, 1);',
  23631. ' };',
  23632. '});',
  23633. 'rtl.createHelper(this, "THelper", null, function () {',
  23634. ' this.Foo = function (w) {',
  23635. ' var Result = 0;',
  23636. ' this.Run(10);',
  23637. ' this.Run(10);',
  23638. ' $mod.THelper.Foo.call(this, 1);',
  23639. ' $mod.THelper.Foo.call(this, 1);',
  23640. ' $mod.THelper.Foo.call(this, 1);',
  23641. ' $mod.THelper.Foo.call(this, 1);',
  23642. ' $mod.THelper.Foo.call(this, 1);',
  23643. ' $mod.THelper.Foo.call(this, 1);',
  23644. ' return Result;',
  23645. ' };',
  23646. '});',
  23647. 'this.Obj = null;',
  23648. '']),
  23649. LinesToStr([ // $mod.$main
  23650. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23651. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23652. 'var $with = $mod.Obj;',
  23653. '$mod.THelper.Foo.call($with.$class, 1);',
  23654. '$mod.THelper.Foo.call($with.$class, 1);',
  23655. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23656. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23657. 'var $with1 = $mod.TObject;',
  23658. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23659. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23660. '']));
  23661. end;
  23662. procedure TTestModule.TestClassHelper_ClassOf;
  23663. begin
  23664. StartProgram(false);
  23665. Add([
  23666. 'type',
  23667. ' TObject = class',
  23668. ' end;',
  23669. ' TClass = class of TObject;',
  23670. ' THelper = class helper for TObject',
  23671. ' class function Foo(w: word = 1): word;',
  23672. ' end;',
  23673. 'class function THelper.foo(w: word): word;',
  23674. 'begin',
  23675. 'end;',
  23676. 'var',
  23677. ' c: TClass;',
  23678. 'begin',
  23679. ' c.Foo;',
  23680. ' c.Foo();',
  23681. ' with c do begin',
  23682. ' Foo;',
  23683. ' Foo();',
  23684. ' end;',
  23685. '']);
  23686. ConvertProgram;
  23687. CheckSource('TestClassHelper_ClassOf',
  23688. LinesToStr([ // statements
  23689. 'rtl.createClass(this, "TObject", null, function () {',
  23690. ' this.$init = function () {',
  23691. ' };',
  23692. ' this.$final = function () {',
  23693. ' };',
  23694. '});',
  23695. 'rtl.createHelper(this, "THelper", null, function () {',
  23696. ' this.Foo = function (w) {',
  23697. ' var Result = 0;',
  23698. ' return Result;',
  23699. ' };',
  23700. '});',
  23701. 'this.c = null;',
  23702. '']),
  23703. LinesToStr([ // $mod.$main
  23704. '$mod.THelper.Foo.call($mod.c, 1);',
  23705. '$mod.THelper.Foo.call($mod.c, 1);',
  23706. 'var $with = $mod.c;',
  23707. '$mod.THelper.Foo.call($with, 1);',
  23708. '$mod.THelper.Foo.call($with, 1);',
  23709. '']));
  23710. end;
  23711. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  23712. begin
  23713. StartProgram(false);
  23714. Add([
  23715. '{$mode objfpc}',
  23716. 'type',
  23717. ' TObject = class',
  23718. ' procedure DoIt;',
  23719. ' end;',
  23720. ' THelper = class helper for TObject',
  23721. ' procedure Fly(w: word = 1);',
  23722. ' class procedure Glide(w: word = 1);',
  23723. ' class procedure Run(w: word = 1); static;',
  23724. ' end;',
  23725. ' TFly = procedure(w: word) of object;',
  23726. ' TGlide = TFly;',
  23727. ' TRun = procedure(w: word);',
  23728. 'var',
  23729. ' f: TFly;',
  23730. ' g: TGlide;',
  23731. ' r: TRun;',
  23732. 'procedure TObject.DoIt;',
  23733. 'begin',
  23734. ' f:=@fly;',
  23735. ' g:=@glide;',
  23736. ' r:=@run;',
  23737. ' f:[email protected];',
  23738. ' g:[email protected];',
  23739. ' r:[email protected];',
  23740. ' with self do begin',
  23741. ' f:=@fly;',
  23742. ' g:=@glide;',
  23743. ' r:=@run;',
  23744. ' end;',
  23745. 'end;',
  23746. 'procedure THelper.fly(w: word);',
  23747. 'begin',
  23748. ' f:=@fly;',
  23749. ' g:=@glide;',
  23750. ' r:=@run;',
  23751. 'end;',
  23752. 'class procedure THelper.glide(w: word);',
  23753. 'begin',
  23754. ' g:=@glide;',
  23755. ' r:=@run;',
  23756. 'end;',
  23757. 'class procedure THelper.run(w: word);',
  23758. 'begin',
  23759. ' g:=@glide;',
  23760. ' r:=@run;',
  23761. 'end;',
  23762. 'var',
  23763. ' Obj: TObject;',
  23764. 'begin',
  23765. ' f:[email protected];',
  23766. ' g:[email protected];',
  23767. ' r:[email protected];',
  23768. ' with obj do begin',
  23769. ' f:=@fly;',
  23770. ' g:=@glide;',
  23771. ' r:=@run;',
  23772. ' end;',
  23773. ' g:[email protected];',
  23774. ' r:[email protected];',
  23775. ' with tobject do begin',
  23776. ' g:=@glide;',
  23777. ' r:=@run;',
  23778. ' end;',
  23779. '']);
  23780. ConvertProgram;
  23781. CheckSource('TestClassHelper_MethodRefObjFPC',
  23782. LinesToStr([ // statements
  23783. 'rtl.createClass(this, "TObject", null, function () {',
  23784. ' this.$init = function () {',
  23785. ' };',
  23786. ' this.$final = function () {',
  23787. ' };',
  23788. ' this.DoIt = function () {',
  23789. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23790. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23791. ' $mod.r = $mod.THelper.Run;',
  23792. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23793. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23794. ' $mod.r = $mod.THelper.Run;',
  23795. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23796. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23797. ' $mod.r = $mod.THelper.Run;',
  23798. ' };',
  23799. '});',
  23800. 'rtl.createHelper(this, "THelper", null, function () {',
  23801. ' this.Fly = function (w) {',
  23802. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23803. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23804. ' $mod.r = $mod.THelper.Run;',
  23805. ' };',
  23806. ' this.Glide = function (w) {',
  23807. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23808. ' $mod.r = $mod.THelper.Run;',
  23809. ' };',
  23810. ' this.Run = function (w) {',
  23811. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23812. ' $mod.r = $mod.THelper.Run;',
  23813. ' };',
  23814. '});',
  23815. 'this.f = null;',
  23816. 'this.g = null;',
  23817. 'this.r = null;',
  23818. 'this.Obj = null;',
  23819. '']),
  23820. LinesToStr([ // $mod.$main
  23821. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23822. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23823. '$mod.r = $mod.THelper.Run;',
  23824. 'var $with = $mod.Obj;',
  23825. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23826. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23827. '$mod.r = $mod.THelper.Run;',
  23828. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23829. '$mod.r = $mod.THelper.Run;',
  23830. 'var $with1 = $mod.TObject;',
  23831. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23832. '$mod.r = $mod.THelper.Run;',
  23833. '']));
  23834. end;
  23835. procedure TTestModule.TestClassHelper_Constructor;
  23836. begin
  23837. StartProgram(false);
  23838. Add([
  23839. 'type',
  23840. ' TObject = class',
  23841. ' constructor Create;',
  23842. ' end;',
  23843. ' TClass = class of TObject;',
  23844. ' THelper = class helper for TObject',
  23845. ' constructor NewHlp(w: word);',
  23846. ' end;',
  23847. 'var',
  23848. ' obj: TObject;',
  23849. ' c: TClass;',
  23850. 'constructor TObject.Create;',
  23851. 'begin',
  23852. ' NewHlp(2);', // normal call
  23853. ' tobject.NewHlp(3);', // new instance
  23854. ' c.newhlp(4);', // new instance
  23855. 'end;',
  23856. 'constructor THelper.NewHlp(w: word);',
  23857. 'begin',
  23858. ' create;', // normal call
  23859. ' tobject.create;', // new instance
  23860. ' NewHlp(2);', // normal call
  23861. ' tobject.NewHlp(3);', // new instance
  23862. ' c.newhlp(4);', // new instance
  23863. 'end;',
  23864. 'begin',
  23865. ' obj.newhlp(2);', // normal call
  23866. ' with Obj do newhlp(12);', // normal call
  23867. ' tobject.newhlp(3);', // new instance
  23868. ' with tobject do newhlp(13);', // new instance
  23869. ' c.newhlp(4);', // new instance
  23870. ' with c do newhlp(14);', // new instance
  23871. '']);
  23872. ConvertProgram;
  23873. CheckSource('TestClassHelper_Constructor',
  23874. LinesToStr([ // statements
  23875. 'rtl.createClass(this, "TObject", null, function () {',
  23876. ' this.$init = function () {',
  23877. ' };',
  23878. ' this.$final = function () {',
  23879. ' };',
  23880. ' this.Create = function () {',
  23881. ' $mod.THelper.NewHlp.call(this, 2);',
  23882. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23883. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23884. ' return this;',
  23885. ' };',
  23886. '});',
  23887. 'rtl.createHelper(this, "THelper", null, function () {',
  23888. ' this.NewHlp = function (w) {',
  23889. ' this.Create();',
  23890. ' $mod.TObject.$create("Create");',
  23891. ' $mod.THelper.NewHlp.call(this, 2);',
  23892. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23893. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23894. ' return this;',
  23895. ' };',
  23896. '});',
  23897. 'this.obj = null;',
  23898. 'this.c = null;',
  23899. '']),
  23900. LinesToStr([ // $mod.$main
  23901. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23902. 'var $with = $mod.obj;',
  23903. '$mod.THelper.NewHlp.call($with, 12);',
  23904. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23905. 'var $with1 = $mod.TObject;',
  23906. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23907. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23908. 'var $with2 = $mod.c;',
  23909. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23910. '']));
  23911. end;
  23912. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23913. begin
  23914. StartProgram(false);
  23915. Add([
  23916. 'type',
  23917. ' TObject = class',
  23918. ' procedure Fly;',
  23919. ' end;',
  23920. ' TObjHelper = class helper for TObject',
  23921. ' procedure Fly;',
  23922. ' end;',
  23923. ' TBird = class',
  23924. ' procedure Fly;',
  23925. ' end;',
  23926. ' TBirdHelper = class helper for TBird',
  23927. ' procedure Fly;',
  23928. ' procedure Walk(w: word);',
  23929. ' end;',
  23930. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23931. ' procedure Fly;',
  23932. ' procedure Walk(w: word);',
  23933. ' end;',
  23934. 'procedure Tobject.fly;',
  23935. 'begin',
  23936. ' inherited;', // ignore
  23937. 'end;',
  23938. 'procedure Tobjhelper.fly;',
  23939. 'begin',
  23940. ' {@TObject_Fly}inherited;',
  23941. ' inherited {@TObject_Fly}Fly;',
  23942. 'end;',
  23943. 'procedure Tbird.fly;',
  23944. 'begin',
  23945. ' {@TObjHelper_Fly}inherited;',
  23946. ' inherited {@TObjHelper_Fly}Fly;',
  23947. 'end;',
  23948. 'procedure Tbirdhelper.fly;',
  23949. 'begin',
  23950. ' {@TBird_Fly}inherited;',
  23951. ' inherited {@TBird_Fly}Fly;',
  23952. 'end;',
  23953. 'procedure Tbirdhelper.walk(w: word);',
  23954. 'begin',
  23955. 'end;',
  23956. 'procedure teagleHelper.fly;',
  23957. 'begin',
  23958. ' {@TBird_Fly}inherited;',
  23959. ' inherited {@TBird_Fly}Fly;',
  23960. 'end;',
  23961. 'procedure teagleHelper.walk(w: word);',
  23962. 'begin',
  23963. ' {@TBirdHelper_Walk}inherited;',
  23964. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23965. 'end;',
  23966. 'begin',
  23967. '']);
  23968. ConvertProgram;
  23969. CheckSource('TestClassHelper_InheritedObjFPC',
  23970. LinesToStr([ // statements
  23971. 'rtl.createClass(this, "TObject", null, function () {',
  23972. ' this.$init = function () {',
  23973. ' };',
  23974. ' this.$final = function () {',
  23975. ' };',
  23976. ' this.Fly = function () {',
  23977. ' };',
  23978. '});',
  23979. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23980. ' this.Fly = function () {',
  23981. ' $mod.TObject.Fly.call(this);',
  23982. ' $mod.TObject.Fly.call(this);',
  23983. ' };',
  23984. '});',
  23985. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23986. ' this.Fly$1 = function () {',
  23987. ' $mod.TObjHelper.Fly.call(this);',
  23988. ' $mod.TObjHelper.Fly.call(this);',
  23989. ' };',
  23990. '});',
  23991. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23992. ' this.Fly = function () {',
  23993. ' $mod.TBird.Fly$1.call(this);',
  23994. ' $mod.TBird.Fly$1.call(this);',
  23995. ' };',
  23996. ' this.Walk = function (w) {',
  23997. ' };',
  23998. '});',
  23999. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  24000. ' this.Fly$1 = function () {',
  24001. ' $mod.TBird.Fly$1.call(this);',
  24002. ' $mod.TBird.Fly$1.call(this);',
  24003. ' };',
  24004. ' this.Walk$1 = function (w) {',
  24005. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  24006. ' $mod.TBirdHelper.Walk.call(this, 3);',
  24007. ' };',
  24008. '});',
  24009. '']),
  24010. LinesToStr([ // $mod.$main
  24011. '']));
  24012. end;
  24013. procedure TTestModule.TestClassHelper_Property;
  24014. begin
  24015. StartProgram(false);
  24016. Add([
  24017. 'type',
  24018. ' TObject = class',
  24019. ' FSize: word;',
  24020. ' function GetSpeed: word;',
  24021. ' procedure SetSpeed(Value: word);',
  24022. ' end;',
  24023. ' TObjHelper = class helper for TObject',
  24024. ' function GetLeft: word;',
  24025. ' procedure SetLeft(Value: word);',
  24026. ' property Size: word read FSize write FSize;',
  24027. ' property Speed: word read GetSpeed write SetSpeed;',
  24028. ' property Left: word read GetLeft write SetLeft;',
  24029. ' end;',
  24030. ' TBird = class',
  24031. ' property NotRight: word read GetLeft write SetLeft;',
  24032. ' procedure DoIt;',
  24033. ' end;',
  24034. 'var',
  24035. ' b: TBird;',
  24036. 'function Tobject.GetSpeed: word;',
  24037. 'begin',
  24038. ' Size:=Size+11;',
  24039. ' Speed:=Speed+12;',
  24040. ' Result:=Left+13;',
  24041. ' Left:=13;',
  24042. ' Left:=Left+13;',
  24043. ' Self.Size:=Self.Size+21;',
  24044. ' Self.Speed:=Self.Speed+22;',
  24045. ' Self.Left:=Self.Left+23;',
  24046. ' with Self do begin',
  24047. ' Size:=Size+31;',
  24048. ' Speed:=Speed+32;',
  24049. ' Left:=Left+33;',
  24050. ' end;',
  24051. 'end;',
  24052. 'procedure Tobject.SetSpeed(Value: word);',
  24053. 'begin',
  24054. 'end;',
  24055. 'function TObjHelper.GetLeft: word;',
  24056. 'begin',
  24057. ' Size:=Size+11;',
  24058. ' Speed:=Speed+12;',
  24059. ' Left:=Left+13;',
  24060. ' Self.Size:=Self.Size+21;',
  24061. ' Self.Speed:=Self.Speed+22;',
  24062. ' Self.Left:=Self.Left+23;',
  24063. ' with Self do begin',
  24064. ' Size:=Size+31;',
  24065. ' Speed:=Speed+32;',
  24066. ' Left:=Left+33;',
  24067. ' end;',
  24068. 'end;',
  24069. 'procedure TObjHelper.SetLeft(Value: word);',
  24070. 'begin',
  24071. 'end;',
  24072. 'procedure TBird.DoIt;',
  24073. 'begin',
  24074. ' NotRight:=NotRight+11;',
  24075. ' Self.NotRight:=Self.NotRight+21;',
  24076. ' with Self do begin',
  24077. ' NotRight:=NotRight+31;',
  24078. ' end;',
  24079. 'end;',
  24080. 'begin',
  24081. ' b.Size:=b.Size+11;',
  24082. ' b.Speed:=b.Speed+12;',
  24083. ' b.Left:=b.Left+13;',
  24084. ' b.NotRight:=b.NotRight+14;',
  24085. ' with b do begin',
  24086. ' Size:=Size+31;',
  24087. ' Speed:=Speed+32;',
  24088. ' Left:=Left+33;',
  24089. ' NotRight:=NotRight+34;',
  24090. ' end;',
  24091. '']);
  24092. ConvertProgram;
  24093. CheckSource('TestClassHelper_Property',
  24094. LinesToStr([ // statements
  24095. 'rtl.createClass(this, "TObject", null, function () {',
  24096. ' this.$init = function () {',
  24097. ' this.FSize = 0;',
  24098. ' };',
  24099. ' this.$final = function () {',
  24100. ' };',
  24101. ' this.GetSpeed = function () {',
  24102. ' var Result = 0;',
  24103. ' this.FSize = this.FSize + 11;',
  24104. ' this.SetSpeed(this.GetSpeed() + 12);',
  24105. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  24106. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  24107. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24108. ' this.FSize = this.FSize + 21;',
  24109. ' this.SetSpeed(this.GetSpeed() + 22);',
  24110. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24111. ' this.FSize = this.FSize + 31;',
  24112. ' this.SetSpeed(this.GetSpeed() + 32);',
  24113. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24114. ' return Result;',
  24115. ' };',
  24116. ' this.SetSpeed = function (Value) {',
  24117. ' };',
  24118. '});',
  24119. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24120. ' this.GetLeft = function () {',
  24121. ' var Result = 0;',
  24122. ' this.FSize = this.FSize + 11;',
  24123. ' this.SetSpeed(this.GetSpeed() + 12);',
  24124. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24125. ' this.FSize = this.FSize + 21;',
  24126. ' this.SetSpeed(this.GetSpeed() + 22);',
  24127. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24128. ' this.FSize = this.FSize + 31;',
  24129. ' this.SetSpeed(this.GetSpeed() + 32);',
  24130. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24131. ' return Result;',
  24132. ' };',
  24133. ' this.SetLeft = function (Value) {',
  24134. ' };',
  24135. '});',
  24136. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24137. ' this.DoIt = function () {',
  24138. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  24139. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  24140. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  24141. ' };',
  24142. '});',
  24143. 'this.b = null;',
  24144. '']),
  24145. LinesToStr([ // $mod.$main
  24146. '$mod.b.FSize = $mod.b.FSize + 11;',
  24147. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  24148. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  24149. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  24150. 'var $with = $mod.b;',
  24151. '$with.FSize = $with.FSize + 31;',
  24152. '$with.SetSpeed($with.GetSpeed() + 32);',
  24153. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  24154. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  24155. '']));
  24156. end;
  24157. procedure TTestModule.TestClassHelper_Property_Array;
  24158. begin
  24159. StartProgram(false);
  24160. Add([
  24161. 'type',
  24162. ' TObject = class',
  24163. ' function GetSpeed(Index: boolean): word;',
  24164. ' procedure SetSpeed(Index: boolean; Value: word);',
  24165. ' end;',
  24166. ' TObjHelper = class helper for TObject',
  24167. ' function GetSize(Index: boolean): word;',
  24168. ' procedure SetSize(Index: boolean; Value: word);',
  24169. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  24170. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24171. ' end;',
  24172. ' TBird = class',
  24173. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  24174. ' procedure DoIt;',
  24175. ' end;',
  24176. 'var',
  24177. ' b: TBird;',
  24178. 'function Tobject.GetSpeed(Index: boolean): word;',
  24179. 'begin',
  24180. ' Result:=Size[false];',
  24181. ' Size[true]:=Size[false]+11;',
  24182. ' Speed[true]:=Speed[false]+12;',
  24183. ' Self.Size[true]:=Self.Size[false]+21;',
  24184. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24185. ' with Self do begin',
  24186. ' Size[true]:=Size[false]+31;',
  24187. ' Speed[true]:=Speed[false]+32;',
  24188. ' end;',
  24189. 'end;',
  24190. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  24191. 'begin',
  24192. 'end;',
  24193. 'function TObjHelper.GetSize(Index: boolean): word;',
  24194. 'begin',
  24195. ' Size[true]:=Size[false]+11;',
  24196. ' Speed[true]:=Speed[false]+12;',
  24197. ' Self.Size[true]:=Self.Size[false]+21;',
  24198. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24199. ' with Self do begin',
  24200. ' Size[true]:=Size[false]+31;',
  24201. ' Speed[true]:=Speed[false]+32;',
  24202. ' end;',
  24203. 'end;',
  24204. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24205. 'begin',
  24206. 'end;',
  24207. 'procedure TBird.DoIt;',
  24208. 'begin',
  24209. ' Items[true]:=Items[false]+11;',
  24210. ' Self.Items[true]:=Self.Items[false]+21;',
  24211. ' with Self do Items[true]:=Items[false]+31;',
  24212. 'end;',
  24213. 'begin',
  24214. ' b.Size[true]:=b.Size[false]+11;',
  24215. ' b.Speed[true]:=b.Speed[false]+12;',
  24216. ' b.Items[true]:=b.Items[false]+13;',
  24217. ' with b do begin',
  24218. ' Size[true]:=Size[false]+21;',
  24219. ' Speed[true]:=Speed[false]+22;',
  24220. ' Items[true]:=Items[false]+23;',
  24221. ' end;',
  24222. '']);
  24223. ConvertProgram;
  24224. CheckSource('TestClassHelper_Property_Array',
  24225. LinesToStr([ // statements
  24226. 'rtl.createClass(this, "TObject", null, function () {',
  24227. ' this.$init = function () {',
  24228. ' };',
  24229. ' this.$final = function () {',
  24230. ' };',
  24231. ' this.GetSpeed = function (Index) {',
  24232. ' var Result = 0;',
  24233. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  24234. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24235. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24236. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24237. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24238. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24239. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24240. ' return Result;',
  24241. ' };',
  24242. ' this.SetSpeed = function (Index, Value) {',
  24243. ' };',
  24244. '});',
  24245. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24246. ' this.GetSize = function (Index) {',
  24247. ' var Result = 0;',
  24248. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24249. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24250. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24251. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24252. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24253. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24254. ' return Result;',
  24255. ' };',
  24256. ' this.SetSize = function (Index, Value) {',
  24257. ' };',
  24258. '});',
  24259. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24260. ' this.DoIt = function () {',
  24261. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24262. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24263. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24264. ' };',
  24265. '});',
  24266. 'this.b = null;',
  24267. '']),
  24268. LinesToStr([ // $mod.$main
  24269. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  24270. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  24271. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  24272. 'var $with = $mod.b;',
  24273. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  24274. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  24275. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  24276. '']));
  24277. end;
  24278. procedure TTestModule.TestClassHelper_Property_Array_Default;
  24279. begin
  24280. StartProgram(false);
  24281. Add([
  24282. 'type',
  24283. ' TObject = class',
  24284. ' function GetSpeed(Index: boolean): word;',
  24285. ' procedure SetSpeed(Index: boolean; Value: word);',
  24286. ' end;',
  24287. ' TObjHelper = class helper for TObject',
  24288. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  24289. ' end;',
  24290. ' TBird = class',
  24291. ' end;',
  24292. ' TBirdHelper = class helper for TBird',
  24293. ' function GetSize(Index: word): boolean;',
  24294. ' procedure SetSize(Index: word; Value: boolean);',
  24295. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  24296. ' end;',
  24297. 'function Tobject.GetSpeed(Index: boolean): word;',
  24298. 'begin',
  24299. ' Self[true]:=Self[false]+1;',
  24300. 'end;',
  24301. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  24302. 'begin',
  24303. 'end;',
  24304. 'function TBirdHelper.GetSize(Index: word): boolean;',
  24305. 'begin',
  24306. ' Self[1]:=not Self[2];',
  24307. 'end;',
  24308. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  24309. 'begin',
  24310. 'end;',
  24311. 'var',
  24312. ' o: TObject;',
  24313. ' b: TBird;',
  24314. 'begin',
  24315. ' o[true]:=o[false]+1;',
  24316. ' b[3]:=not b[4];',
  24317. '']);
  24318. ConvertProgram;
  24319. CheckSource('TestClassHelper_Property_Array_Default',
  24320. LinesToStr([ // statements
  24321. 'rtl.createClass(this, "TObject", null, function () {',
  24322. ' this.$init = function () {',
  24323. ' };',
  24324. ' this.$final = function () {',
  24325. ' };',
  24326. ' this.GetSpeed = function (Index) {',
  24327. ' var Result = 0;',
  24328. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  24329. ' return Result;',
  24330. ' };',
  24331. ' this.SetSpeed = function (Index, Value) {',
  24332. ' };',
  24333. '});',
  24334. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24335. '});',
  24336. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24337. '});',
  24338. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24339. ' this.GetSize = function (Index) {',
  24340. ' var Result = false;',
  24341. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  24342. ' return Result;',
  24343. ' };',
  24344. ' this.SetSize = function (Index, Value) {',
  24345. ' };',
  24346. '});',
  24347. 'this.o = null;',
  24348. 'this.b = null;',
  24349. '']),
  24350. LinesToStr([ // $mod.$main
  24351. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  24352. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  24353. '']));
  24354. end;
  24355. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  24356. begin
  24357. StartProgram(false);
  24358. Add([
  24359. 'type',
  24360. ' TObject = class',
  24361. ' end;',
  24362. ' TObjHelper = class helper for TObject',
  24363. ' function GetItems(Index: word): TObject;',
  24364. ' procedure SetItems(Index: word; Value: TObject);',
  24365. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  24366. ' end;',
  24367. 'function Tobjhelper.GetItems(Index: word): TObject;',
  24368. 'begin',
  24369. ' Self[1][2]:=Self[3][4];',
  24370. 'end;',
  24371. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  24372. 'begin',
  24373. 'end;',
  24374. 'var',
  24375. ' o: TObject;',
  24376. 'begin',
  24377. ' o[1][2]:=o[3][4];',
  24378. '']);
  24379. ConvertProgram;
  24380. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  24381. LinesToStr([ // statements
  24382. 'rtl.createClass(this, "TObject", null, function () {',
  24383. ' this.$init = function () {',
  24384. ' };',
  24385. ' this.$final = function () {',
  24386. ' };',
  24387. '});',
  24388. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24389. ' this.GetItems = function (Index) {',
  24390. ' var Result = null;',
  24391. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  24392. ' return Result;',
  24393. ' };',
  24394. ' this.SetItems = function (Index, Value) {',
  24395. ' };',
  24396. '});',
  24397. 'this.o = null;',
  24398. '']),
  24399. LinesToStr([ // $mod.$main
  24400. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  24401. '']));
  24402. end;
  24403. procedure TTestModule.TestClassHelper_ClassProperty;
  24404. begin
  24405. StartProgram(false);
  24406. Add([
  24407. 'type',
  24408. ' TObject = class',
  24409. ' class var FSize: word;',
  24410. ' class function GetSpeed: word;',
  24411. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  24412. ' end;',
  24413. ' TObjHelper = class helper for TObject',
  24414. ' class function GetLeft: word;',
  24415. ' class procedure SetLeft(Value: word);',
  24416. ' class property Size: word read FSize write FSize;',
  24417. ' class property Speed: word read GetSpeed write SetSpeed;',
  24418. ' class property Left: word read GetLeft write SetLeft;',
  24419. ' end;',
  24420. ' TBird = class',
  24421. ' class property NotRight: word read GetLeft write SetLeft;',
  24422. ' class procedure DoIt;',
  24423. ' end;',
  24424. ' TBirdClass = class of TBird;',
  24425. 'class function Tobject.GetSpeed: word;',
  24426. 'begin',
  24427. ' Size:=Size+11;',
  24428. ' Speed:=Speed+12;',
  24429. ' Left:=Left+13;',
  24430. ' Self.Size:=Self.Size+21;',
  24431. ' Self.Speed:=Self.Speed+22;',
  24432. ' Self.Left:=Self.Left+23;',
  24433. ' with Self do begin',
  24434. ' Size:=Size+31;',
  24435. ' Speed:=Speed+32;',
  24436. ' Left:=Left+33;',
  24437. ' end;',
  24438. 'end;',
  24439. 'class function TObjHelper.GetLeft: word;',
  24440. 'begin',
  24441. ' Size:=Size+11;',
  24442. ' Speed:=Speed+12;',
  24443. ' Left:=Left+13;',
  24444. ' Self.Size:=Self.Size+21;',
  24445. ' Self.Speed:=Self.Speed+22;',
  24446. ' Self.Left:=Self.Left+23;',
  24447. ' with Self do begin',
  24448. ' Size:=Size+31;',
  24449. ' Speed:=Speed+32;',
  24450. ' Left:=Left+33;',
  24451. ' end;',
  24452. 'end;',
  24453. 'class procedure TObjHelper.SetLeft(Value: word);',
  24454. 'begin',
  24455. 'end;',
  24456. 'class procedure TBird.DoIt;',
  24457. 'begin',
  24458. ' NotRight:=NotRight+11;',
  24459. ' Self.NotRight:=Self.NotRight+21;',
  24460. ' with Self do NotRight:=NotRight+31;',
  24461. 'end;',
  24462. 'var',
  24463. ' b: TBird;',
  24464. ' c: TBirdClass;',
  24465. 'begin',
  24466. ' b.Size:=b.Size+11;',
  24467. ' b.Speed:=b.Speed+12;',
  24468. ' b.Left:=b.Left+13;',
  24469. ' b.NotRight:=b.NotRight+14;',
  24470. ' with b do begin',
  24471. ' Size:=Size+31;',
  24472. ' Speed:=Speed+32;',
  24473. ' Left:=Left+33;',
  24474. ' NotRight:=NotRight+34;',
  24475. ' end;',
  24476. ' c.Size:=c.Size+11;',
  24477. ' c.Speed:=c.Speed+12;',
  24478. ' c.Left:=c.Left+13;',
  24479. ' c.NotRight:=c.NotRight+14;',
  24480. ' with c do begin',
  24481. ' Size:=Size+31;',
  24482. ' Speed:=Speed+32;',
  24483. ' Left:=Left+33;',
  24484. ' NotRight:=NotRight+34;',
  24485. ' end;',
  24486. ' tbird.Size:=tbird.Size+11;',
  24487. ' tbird.Speed:=tbird.Speed+12;',
  24488. ' tbird.Left:=tbird.Left+13;',
  24489. ' tbird.NotRight:=tbird.NotRight+14;',
  24490. ' with tbird do begin',
  24491. ' Size:=Size+31;',
  24492. ' Speed:=Speed+32;',
  24493. ' Left:=Left+33;',
  24494. ' NotRight:=NotRight+34;',
  24495. ' end;',
  24496. '']);
  24497. ConvertProgram;
  24498. CheckSource('TestClassHelper_ClassProperty',
  24499. LinesToStr([ // statements
  24500. 'rtl.createClass(this, "TObject", null, function () {',
  24501. ' this.FSize = 0;',
  24502. ' this.$init = function () {',
  24503. ' };',
  24504. ' this.$final = function () {',
  24505. ' };',
  24506. ' this.GetSpeed = function () {',
  24507. ' var Result = 0;',
  24508. ' $mod.TObject.FSize = this.FSize + 11;',
  24509. ' this.SetSpeed(this.GetSpeed() + 12);',
  24510. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24511. ' $mod.TObject.FSize = this.FSize + 21;',
  24512. ' this.SetSpeed(this.GetSpeed() + 22);',
  24513. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24514. ' $mod.TObject.FSize = this.FSize + 31;',
  24515. ' this.SetSpeed(this.GetSpeed() + 32);',
  24516. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24517. ' return Result;',
  24518. ' };',
  24519. '});',
  24520. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24521. ' this.GetLeft = function () {',
  24522. ' var Result = 0;',
  24523. ' $mod.TObject.FSize = this.FSize + 11;',
  24524. ' this.SetSpeed(this.GetSpeed() + 12);',
  24525. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24526. ' $mod.TObject.FSize = this.FSize + 21;',
  24527. ' this.SetSpeed(this.GetSpeed() + 22);',
  24528. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24529. ' $mod.TObject.FSize = this.FSize + 31;',
  24530. ' this.SetSpeed(this.GetSpeed() + 32);',
  24531. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24532. ' return Result;',
  24533. ' };',
  24534. ' this.SetLeft = function (Value) {',
  24535. ' };',
  24536. '});',
  24537. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24538. ' this.DoIt = function () {',
  24539. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  24540. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  24541. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  24542. ' };',
  24543. '});',
  24544. 'this.b = null;',
  24545. 'this.c = null;',
  24546. '']),
  24547. LinesToStr([ // $mod.$main
  24548. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  24549. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  24550. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  24551. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  24552. 'var $with = $mod.b;',
  24553. '$mod.TObject.FSize = $with.FSize + 31;',
  24554. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  24555. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  24556. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  24557. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  24558. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  24559. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  24560. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  24561. 'var $with1 = $mod.c;',
  24562. '$mod.TObject.FSize = $with1.FSize + 31;',
  24563. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24564. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  24565. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  24566. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  24567. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  24568. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  24569. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  24570. 'var $with2 = $mod.TBird;',
  24571. '$mod.TObject.FSize = $with2.FSize + 31;',
  24572. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24573. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  24574. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  24575. '']));
  24576. end;
  24577. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  24578. begin
  24579. StartProgram(false);
  24580. Add([
  24581. 'type',
  24582. ' TObject = class',
  24583. ' class function GetSpeed: word; static;',
  24584. ' class procedure SetSpeed(Value: word); static;',
  24585. ' end;',
  24586. ' TObjHelper = class helper for TObject',
  24587. ' class function GetLeft: word; static;',
  24588. ' class procedure SetLeft(Value: word); static;',
  24589. ' class property Speed: word read GetSpeed write SetSpeed;',
  24590. ' class property Left: word read GetLeft write SetLeft;',
  24591. ' end;',
  24592. ' TBird = class',
  24593. ' class property NotRight: word read GetLeft write SetLeft;',
  24594. ' class procedure DoIt; static;',
  24595. ' class procedure DoSome;',
  24596. ' end;',
  24597. ' TBirdClass = class of TBird;',
  24598. 'class function Tobject.GetSpeed: word;',
  24599. 'begin',
  24600. ' Speed:=Speed+12;',
  24601. ' Left:=Left+13;',
  24602. 'end;',
  24603. 'class procedure TObject.SetSpeed(Value: word);',
  24604. 'begin',
  24605. 'end;',
  24606. 'class function TObjHelper.GetLeft: word;',
  24607. 'begin',
  24608. ' Speed:=Speed+12;',
  24609. ' Left:=Left+13;',
  24610. 'end;',
  24611. 'class procedure TObjHelper.SetLeft(Value: word);',
  24612. 'begin',
  24613. 'end;',
  24614. 'class procedure TBird.DoIt;',
  24615. 'begin',
  24616. ' NotRight:=NotRight+11;',
  24617. 'end;',
  24618. 'class procedure TBird.DoSome;',
  24619. 'begin',
  24620. ' Speed:=Speed+12;',
  24621. ' Left:=Left+13;',
  24622. ' Self.Speed:=Self.Speed+22;',
  24623. ' Self.Left:=Self.Left+23;',
  24624. ' with Self do begin',
  24625. ' Speed:=Speed+32;',
  24626. ' Left:=Left+33;',
  24627. ' end;',
  24628. ' NotRight:=NotRight+11;',
  24629. ' Self.NotRight:=Self.NotRight+21;',
  24630. ' with Self do NotRight:=NotRight+31;',
  24631. 'end;',
  24632. 'var',
  24633. ' b: TBird;',
  24634. ' c: TBirdClass;',
  24635. 'begin',
  24636. ' b.Speed:=b.Speed+12;',
  24637. ' b.Left:=b.Left+13;',
  24638. ' b.NotRight:=b.NotRight+14;',
  24639. ' with b do begin',
  24640. ' Speed:=Speed+32;',
  24641. ' Left:=Left+33;',
  24642. ' NotRight:=NotRight+34;',
  24643. ' end;',
  24644. ' c.Speed:=c.Speed+12;',
  24645. ' c.Left:=c.Left+13;',
  24646. ' c.NotRight:=c.NotRight+14;',
  24647. ' with c do begin',
  24648. ' Speed:=Speed+32;',
  24649. ' Left:=Left+33;',
  24650. ' NotRight:=NotRight+34;',
  24651. ' end;',
  24652. ' tbird.Speed:=tbird.Speed+12;',
  24653. ' tbird.Left:=tbird.Left+13;',
  24654. ' tbird.NotRight:=tbird.NotRight+14;',
  24655. ' with tbird do begin',
  24656. ' Speed:=Speed+32;',
  24657. ' Left:=Left+33;',
  24658. ' NotRight:=NotRight+34;',
  24659. ' end;',
  24660. '']);
  24661. ConvertProgram;
  24662. CheckSource('TestClassHelper_ClassPropertyStatic',
  24663. LinesToStr([ // statements
  24664. 'rtl.createClass(this, "TObject", null, function () {',
  24665. ' this.$init = function () {',
  24666. ' };',
  24667. ' this.$final = function () {',
  24668. ' };',
  24669. ' this.GetSpeed = function () {',
  24670. ' var Result = 0;',
  24671. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24672. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24673. ' return Result;',
  24674. ' };',
  24675. ' this.SetSpeed = function (Value) {',
  24676. ' };',
  24677. '});',
  24678. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24679. ' this.GetLeft = function () {',
  24680. ' var Result = 0;',
  24681. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24682. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24683. ' return Result;',
  24684. ' };',
  24685. ' this.SetLeft = function (Value) {',
  24686. ' };',
  24687. '});',
  24688. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24689. ' this.DoIt = function () {',
  24690. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24691. ' };',
  24692. ' this.DoSome = function () {',
  24693. ' this.SetSpeed(this.GetSpeed() + 12);',
  24694. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24695. ' this.SetSpeed(this.GetSpeed() + 22);',
  24696. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  24697. ' this.SetSpeed(this.GetSpeed() + 32);',
  24698. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24699. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24700. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  24701. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  24702. ' };',
  24703. '});',
  24704. 'this.b = null;',
  24705. 'this.c = null;',
  24706. '']),
  24707. LinesToStr([ // $mod.$main
  24708. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24709. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24710. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24711. 'var $with = $mod.b;',
  24712. '$with.SetSpeed($with.GetSpeed() + 32);',
  24713. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24714. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24715. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24716. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24717. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24718. 'var $with1 = $mod.c;',
  24719. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24720. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24721. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24722. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24723. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24724. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24725. 'var $with2 = $mod.TBird;',
  24726. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24727. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24728. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24729. '']));
  24730. end;
  24731. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  24732. begin
  24733. StartProgram(false);
  24734. Add([
  24735. 'type',
  24736. ' TObject = class',
  24737. ' class function GetSpeed(Index: boolean): word;',
  24738. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  24739. ' end;',
  24740. ' TObjHelper = class helper for TObject',
  24741. ' class function GetSize(Index: boolean): word;',
  24742. ' class procedure SetSize(Index: boolean; Value: word);',
  24743. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  24744. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24745. ' end;',
  24746. ' TBird = class',
  24747. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  24748. ' class procedure DoIt;',
  24749. ' end;',
  24750. ' TBirdClass = class of TBird;',
  24751. 'class function Tobject.GetSpeed(Index: boolean): word;',
  24752. 'begin',
  24753. ' Size[true]:=Size[false]+11;',
  24754. ' Speed[true]:=Speed[false]+12;',
  24755. ' Self.Size[true]:=Self.Size[false]+21;',
  24756. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24757. ' with Self do begin',
  24758. ' Size[true]:=Size[false]+31;',
  24759. ' Speed[true]:=Speed[false]+32;',
  24760. ' end;',
  24761. 'end;',
  24762. 'class function TObjHelper.GetSize(Index: boolean): word;',
  24763. 'begin',
  24764. ' Size[true]:=Size[false]+11;',
  24765. ' Speed[true]:=Speed[false]+12;',
  24766. ' Self.Size[true]:=Self.Size[false]+21;',
  24767. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24768. ' with Self do begin',
  24769. ' Size[true]:=Size[false]+31;',
  24770. ' Speed[true]:=Speed[false]+32;',
  24771. ' end;',
  24772. 'end;',
  24773. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24774. 'begin',
  24775. 'end;',
  24776. 'class procedure TBird.DoIt;',
  24777. 'begin',
  24778. ' Items[true]:=Items[false]+11;',
  24779. ' Self.Items[true]:=Self.Items[false]+21;',
  24780. ' with Self do Items[true]:=Items[false]+31;',
  24781. 'end;',
  24782. 'var',
  24783. ' b: TBird;',
  24784. ' c: TBirdClass;',
  24785. 'begin',
  24786. ' b.Size[true]:=b.Size[false]+11;',
  24787. ' b.Speed[true]:=b.Speed[false]+12;',
  24788. ' b.Items[true]:=b.Items[false]+13;',
  24789. ' with b do begin',
  24790. ' Size[true]:=Size[false]+21;',
  24791. ' Speed[true]:=Speed[false]+22;',
  24792. ' Items[true]:=Items[false]+23;',
  24793. ' end;',
  24794. ' c.Size[true]:=c.Size[false]+11;',
  24795. ' c.Speed[true]:=c.Speed[false]+12;',
  24796. ' c.Items[true]:=c.Items[false]+13;',
  24797. ' with c do begin',
  24798. ' Size[true]:=Size[false]+21;',
  24799. ' Speed[true]:=Speed[false]+22;',
  24800. ' Items[true]:=Items[false]+23;',
  24801. ' end;',
  24802. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24803. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24804. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24805. ' with TBird do begin',
  24806. ' Size[true]:=Size[false]+21;',
  24807. ' Speed[true]:=Speed[false]+22;',
  24808. ' Items[true]:=Items[false]+23;',
  24809. ' end;',
  24810. '']);
  24811. ConvertProgram;
  24812. CheckSource('TestClassHelper_ClassProperty_Array',
  24813. LinesToStr([ // statements
  24814. 'rtl.createClass(this, "TObject", null, function () {',
  24815. ' this.$init = function () {',
  24816. ' };',
  24817. ' this.$final = function () {',
  24818. ' };',
  24819. ' this.GetSpeed = function (Index) {',
  24820. ' var Result = 0;',
  24821. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24822. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24823. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24824. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24825. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24826. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24827. ' return Result;',
  24828. ' };',
  24829. '});',
  24830. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24831. ' this.GetSize = function (Index) {',
  24832. ' var Result = 0;',
  24833. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24834. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24835. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24836. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24837. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24838. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24839. ' return Result;',
  24840. ' };',
  24841. ' this.SetSize = function (Index, Value) {',
  24842. ' };',
  24843. '});',
  24844. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24845. ' this.DoIt = function () {',
  24846. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24847. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24848. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24849. ' };',
  24850. '});',
  24851. 'this.b = null;',
  24852. 'this.c = null;',
  24853. '']),
  24854. LinesToStr([ // $mod.$main
  24855. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24856. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24857. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24858. 'var $with = $mod.b;',
  24859. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24860. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24861. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24862. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24863. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24864. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24865. 'var $with1 = $mod.c;',
  24866. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24867. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24868. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24869. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24870. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24871. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24872. 'var $with2 = $mod.TBird;',
  24873. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24874. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24875. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24876. '']));
  24877. end;
  24878. procedure TTestModule.TestClassHelper_ForIn;
  24879. begin
  24880. StartProgram(false);
  24881. Add([
  24882. 'type',
  24883. ' TObject = class end;',
  24884. ' TItem = TObject;',
  24885. ' TEnumerator = class',
  24886. ' FCurrent: TItem;',
  24887. ' property Current: TItem read FCurrent;',
  24888. ' function MoveNext: boolean;',
  24889. ' end;',
  24890. ' TBird = class',
  24891. ' end;',
  24892. ' TBirdHelper = class helper for TBird',
  24893. ' function GetEnumerator: TEnumerator;',
  24894. ' end;',
  24895. 'function TEnumerator.MoveNext: boolean;',
  24896. 'begin',
  24897. 'end;',
  24898. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24899. 'begin',
  24900. 'end;',
  24901. 'var',
  24902. ' b: TBird;',
  24903. ' i, i2: TItem;',
  24904. 'begin',
  24905. ' for i in b do i2:=i;']);
  24906. ConvertProgram;
  24907. CheckSource('TestClassHelper_ForIn',
  24908. LinesToStr([ // statements
  24909. 'rtl.createClass(this, "TObject", null, function () {',
  24910. ' this.$init = function () {',
  24911. ' };',
  24912. ' this.$final = function () {',
  24913. ' };',
  24914. '});',
  24915. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24916. ' this.$init = function () {',
  24917. ' $mod.TObject.$init.call(this);',
  24918. ' this.FCurrent = null;',
  24919. ' };',
  24920. ' this.$final = function () {',
  24921. ' this.FCurrent = undefined;',
  24922. ' $mod.TObject.$final.call(this);',
  24923. ' };',
  24924. ' this.MoveNext = function () {',
  24925. ' var Result = false;',
  24926. ' return Result;',
  24927. ' };',
  24928. '});',
  24929. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24930. '});',
  24931. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24932. ' this.GetEnumerator = function () {',
  24933. ' var Result = null;',
  24934. ' return Result;',
  24935. ' };',
  24936. '});',
  24937. 'this.b = null;',
  24938. 'this.i = null;',
  24939. 'this.i2 = null;'
  24940. ]),
  24941. LinesToStr([ // $mod.$main
  24942. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24943. 'try {',
  24944. ' while ($in.MoveNext()){',
  24945. ' $mod.i = $in.FCurrent;',
  24946. ' $mod.i2 = $mod.i;',
  24947. ' }',
  24948. '} finally {',
  24949. ' $in = rtl.freeLoc($in)',
  24950. '};',
  24951. '']));
  24952. end;
  24953. procedure TTestModule.TestClassHelper_PassProperty;
  24954. begin
  24955. StartProgram(false);
  24956. Add([
  24957. 'type',
  24958. ' TObject = class',
  24959. ' FField: TObject;',
  24960. ' property Field: TObject read FField write FField;',
  24961. ' end;',
  24962. ' THelper = class helper for TObject',
  24963. ' procedure Fly;',
  24964. ' class procedure Run;',
  24965. ' class procedure Jump; static;',
  24966. ' end;',
  24967. 'procedure THelper.Fly;',
  24968. 'begin',
  24969. ' Field.Fly;',
  24970. ' Field.Run;',
  24971. ' Field.Jump;',
  24972. ' with Field do begin',
  24973. ' Fly;',
  24974. ' Run;',
  24975. ' Jump;',
  24976. ' end;',
  24977. 'end;',
  24978. 'class procedure THelper.Run;',
  24979. 'begin',
  24980. 'end;',
  24981. 'class procedure THelper.Jump;',
  24982. 'begin',
  24983. 'end;',
  24984. 'var',
  24985. ' b: TObject;',
  24986. 'begin',
  24987. ' b.Field.Fly;',
  24988. ' b.Field.Run;',
  24989. ' b.Field.Jump;',
  24990. ' with b do begin',
  24991. ' Field.Run;',
  24992. ' Field.Fly;',
  24993. ' Field.Jump;',
  24994. ' end;',
  24995. ' with b.Field do begin',
  24996. ' Run;',
  24997. ' Fly;',
  24998. ' Jump;',
  24999. ' end;',
  25000. '']);
  25001. ConvertProgram;
  25002. CheckSource('TestClassHelper_PassProperty',
  25003. LinesToStr([ // statements
  25004. 'rtl.createClass(this, "TObject", null, function () {',
  25005. ' this.$init = function () {',
  25006. ' this.FField = null;',
  25007. ' };',
  25008. ' this.$final = function () {',
  25009. ' this.FField = undefined;',
  25010. ' };',
  25011. '});',
  25012. 'rtl.createHelper(this, "THelper", null, function () {',
  25013. ' this.Fly = function () {',
  25014. ' $mod.THelper.Fly.call(this.FField);',
  25015. ' $mod.THelper.Run.call(this.FField.$class);',
  25016. ' $mod.THelper.Jump();',
  25017. ' var $with = this.FField;',
  25018. ' $mod.THelper.Fly.call($with);',
  25019. ' $mod.THelper.Run.call($with.$class);',
  25020. ' $mod.THelper.Jump();',
  25021. ' };',
  25022. ' this.Run = function () {',
  25023. ' };',
  25024. ' this.Jump = function () {',
  25025. ' };',
  25026. '});',
  25027. 'this.b = null;',
  25028. '']),
  25029. LinesToStr([ // $mod.$main
  25030. '$mod.THelper.Fly.call($mod.b.FField);',
  25031. '$mod.THelper.Run.call($mod.b.FField.$class);',
  25032. '$mod.THelper.Jump();',
  25033. 'var $with = $mod.b;',
  25034. '$mod.THelper.Run.call($with.FField.$class);',
  25035. '$mod.THelper.Fly.call($with.FField);',
  25036. '$mod.THelper.Jump();',
  25037. 'var $with1 = $mod.b.FField;',
  25038. '$mod.THelper.Run.call($with1.$class);',
  25039. '$mod.THelper.Fly.call($with1);',
  25040. '$mod.THelper.Jump();',
  25041. '']));
  25042. end;
  25043. procedure TTestModule.TestExtClassHelper_ClassVar;
  25044. begin
  25045. StartProgram(false);
  25046. Add([
  25047. '{$modeswitch externalclass}',
  25048. 'type',
  25049. ' TExtA = class external name ''ExtObj''',
  25050. ' end;',
  25051. ' THelper = class helper for TExtA',
  25052. ' const',
  25053. ' One = 1;',
  25054. ' Two: word = 2;',
  25055. ' class var',
  25056. ' Glob: word;',
  25057. ' function Foo(w: word): word;',
  25058. ' class function Bar(w: word): word; static;',
  25059. ' end;',
  25060. 'function THelper.foo(w: word): word;',
  25061. 'begin',
  25062. ' Result:=w;',
  25063. ' Two:=One+w;',
  25064. ' Glob:=Glob;',
  25065. ' Result:=Self.Glob;',
  25066. ' Self.Glob:=Self.Glob;',
  25067. ' with Self do Glob:=Glob;',
  25068. 'end;',
  25069. 'class function THelper.bar(w: word): word;',
  25070. 'begin',
  25071. ' Result:=w;',
  25072. ' Two:=One;',
  25073. ' Glob:=Glob;',
  25074. 'end;',
  25075. 'var o: TExtA;',
  25076. 'begin',
  25077. ' texta.two:=texta.one;',
  25078. ' texta.Glob:=texta.Glob;',
  25079. ' with texta do begin',
  25080. ' two:=one;',
  25081. ' Glob:=Glob;',
  25082. ' end;',
  25083. ' o.two:=o.one;',
  25084. ' o.Glob:=o.Glob;',
  25085. ' with o do begin',
  25086. ' two:=one;',
  25087. ' Glob:=Glob;',
  25088. ' end;',
  25089. '']);
  25090. ConvertProgram;
  25091. CheckSource('TestExtClassHelper_ClassVar',
  25092. LinesToStr([ // statements
  25093. 'rtl.createHelper(this, "THelper", null, function () {',
  25094. ' this.One = 1;',
  25095. ' this.Two = 2;',
  25096. ' this.Glob = 0;',
  25097. ' this.Foo = function (w) {',
  25098. ' var Result = 0;',
  25099. ' Result = w;',
  25100. ' $mod.THelper.Two = 1 + w;',
  25101. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25102. ' Result = $mod.THelper.Glob;',
  25103. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25104. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25105. ' return Result;',
  25106. ' };',
  25107. ' this.Bar = function (w) {',
  25108. ' var Result = 0;',
  25109. ' Result = w;',
  25110. ' $mod.THelper.Two = 1;',
  25111. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25112. ' return Result;',
  25113. ' };',
  25114. '});',
  25115. 'this.o = null;',
  25116. '']),
  25117. LinesToStr([ // $mod.$main
  25118. '$mod.THelper.Two = 1;',
  25119. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25120. '$mod.THelper.Two = 1;',
  25121. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25122. '$mod.THelper.Two = 1;',
  25123. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25124. 'var $with = $mod.o;',
  25125. '$mod.THelper.Two = 1;',
  25126. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25127. '']));
  25128. end;
  25129. procedure TTestModule.TestExtClassHelper_Method_Call;
  25130. begin
  25131. StartProgram(false);
  25132. Add([
  25133. '{$modeswitch externalclass}',
  25134. 'type',
  25135. ' TFly = function(w: word): word of object;',
  25136. ' TExtA = class external name ''ExtObj''',
  25137. ' procedure Run(w: word = 10);',
  25138. ' end;',
  25139. ' THelper = class helper for TExtA',
  25140. ' function Foo(w: word = 1): word;',
  25141. ' function Fly(w: word = 2): word; external name ''Fly'';',
  25142. ' end;',
  25143. 'var p: TFly;',
  25144. 'function THelper.foo(w: word): word;',
  25145. 'begin',
  25146. ' Run;',
  25147. ' Run();',
  25148. ' Run(11);',
  25149. ' Foo;',
  25150. ' Foo();',
  25151. ' Foo(12);',
  25152. ' Self.Foo;',
  25153. ' Self.Foo();',
  25154. ' Self.Foo(13);',
  25155. ' Fly;',
  25156. ' Fly();',
  25157. ' with Self do begin',
  25158. ' Foo;',
  25159. ' Foo();',
  25160. ' Foo(14);',
  25161. ' Fly;',
  25162. ' Fly();',
  25163. ' end;',
  25164. ' p:=@Fly;',
  25165. 'end;',
  25166. 'var Obj: TExtA;',
  25167. 'begin',
  25168. ' obj.Foo;',
  25169. ' obj.Foo();',
  25170. ' obj.Foo(21);',
  25171. ' obj.Fly;',
  25172. ' obj.Fly();',
  25173. ' with obj do begin',
  25174. ' Foo;',
  25175. ' Foo();',
  25176. ' Foo(22);',
  25177. ' Fly;',
  25178. ' Fly();',
  25179. ' end;',
  25180. ' p:[email protected];',
  25181. '']);
  25182. ConvertProgram;
  25183. CheckSource('TestExtClassHelper_Method_Call',
  25184. LinesToStr([ // statements
  25185. 'rtl.createHelper(this, "THelper", null, function () {',
  25186. ' this.Foo = function (w) {',
  25187. ' var Result = 0;',
  25188. ' this.Run(10);',
  25189. ' this.Run(10);',
  25190. ' this.Run(11);',
  25191. ' $mod.THelper.Foo.call(this, 1);',
  25192. ' $mod.THelper.Foo.call(this, 1);',
  25193. ' $mod.THelper.Foo.call(this, 12);',
  25194. ' $mod.THelper.Foo.call(this, 1);',
  25195. ' $mod.THelper.Foo.call(this, 1);',
  25196. ' $mod.THelper.Foo.call(this, 13);',
  25197. ' this.Fly(2);',
  25198. ' this.Fly(2);',
  25199. ' $mod.THelper.Foo.call(this, 1);',
  25200. ' $mod.THelper.Foo.call(this, 1);',
  25201. ' $mod.THelper.Foo.call(this, 14);',
  25202. ' this.Fly(2);',
  25203. ' this.Fly(2);',
  25204. ' $mod.p = rtl.createCallback(this, "Fly");',
  25205. ' return Result;',
  25206. ' };',
  25207. '});',
  25208. 'this.p = null;',
  25209. 'this.Obj = null;',
  25210. '']),
  25211. LinesToStr([ // $mod.$main
  25212. '$mod.THelper.Foo.call($mod.Obj, 1);',
  25213. '$mod.THelper.Foo.call($mod.Obj, 1);',
  25214. '$mod.THelper.Foo.call($mod.Obj, 21);',
  25215. '$mod.Obj.Fly(2);',
  25216. '$mod.Obj.Fly(2);',
  25217. 'var $with = $mod.Obj;',
  25218. '$mod.THelper.Foo.call($with, 1);',
  25219. '$mod.THelper.Foo.call($with, 1);',
  25220. '$mod.THelper.Foo.call($with, 22);',
  25221. '$with.Fly(2);',
  25222. '$with.Fly(2);',
  25223. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  25224. '']));
  25225. end;
  25226. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  25227. begin
  25228. StartProgram(false);
  25229. Add([
  25230. '{$modeswitch externalclass}',
  25231. 'type',
  25232. ' TExtA = class external name ''ExtObj''',
  25233. ' procedure Run(w: word = 10);',
  25234. ' end;',
  25235. ' THelper = class helper for TExtA',
  25236. ' class procedure Fly;',
  25237. ' end;',
  25238. 'class procedure THelper.Fly;',
  25239. 'begin end;',
  25240. 'begin',
  25241. '']);
  25242. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  25243. nHelperClassMethodForExtClassMustBeStatic);
  25244. ConvertProgram;
  25245. end;
  25246. procedure TTestModule.TestRecordHelper_ClassVar;
  25247. begin
  25248. StartProgram(false);
  25249. Add([
  25250. 'type',
  25251. ' TRec = record',
  25252. ' end;',
  25253. ' THelper = record helper for TRec',
  25254. ' const',
  25255. ' One = 1;',
  25256. ' Two: word = 2;',
  25257. ' class var',
  25258. ' Glob: word;',
  25259. ' function Foo(w: word): word;',
  25260. ' class function Bar(w: word): word; static;',
  25261. ' end;',
  25262. 'function THelper.foo(w: word): word;',
  25263. 'begin',
  25264. ' Result:=w;',
  25265. ' Two:=One+w;',
  25266. ' Glob:=Glob;',
  25267. ' Result:=Self.Glob;',
  25268. ' Self.Glob:=Self.Glob;',
  25269. ' with Self do Glob:=Glob;',
  25270. ' Self:=Self;',
  25271. 'end;',
  25272. 'class function THelper.bar(w: word): word;',
  25273. 'begin',
  25274. ' Result:=w;',
  25275. ' Two:=One;',
  25276. ' Glob:=Glob;',
  25277. 'end;',
  25278. 'var r: TRec;',
  25279. 'begin',
  25280. ' trec.two:=trec.one;',
  25281. ' trec.Glob:=trec.Glob;',
  25282. ' with trec do begin',
  25283. ' two:=one;',
  25284. ' Glob:=Glob;',
  25285. ' end;',
  25286. ' r.two:=r.one;',
  25287. ' r.Glob:=r.Glob;',
  25288. ' with r do begin',
  25289. ' two:=one;',
  25290. ' Glob:=Glob;',
  25291. ' end;',
  25292. '']);
  25293. ConvertProgram;
  25294. CheckSource('TestRecordHelper_ClassVar',
  25295. LinesToStr([ // statements
  25296. 'rtl.recNewT(this, "TRec", function () {',
  25297. ' this.$eq = function (b) {',
  25298. ' return true;',
  25299. ' };',
  25300. ' this.$assign = function (s) {',
  25301. ' return this;',
  25302. ' };',
  25303. '});',
  25304. 'rtl.createHelper(this, "THelper", null, function () {',
  25305. ' this.One = 1;',
  25306. ' this.Two = 2;',
  25307. ' this.Glob = 0;',
  25308. ' this.Foo = function (w) {',
  25309. ' var Result = 0;',
  25310. ' Result = w;',
  25311. ' $mod.THelper.Two = 1 + w;',
  25312. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25313. ' Result = $mod.THelper.Glob;',
  25314. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25315. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25316. ' this.$assign(this);',
  25317. ' return Result;',
  25318. ' };',
  25319. ' this.Bar = function (w) {',
  25320. ' var Result = 0;',
  25321. ' Result = w;',
  25322. ' $mod.THelper.Two = 1;',
  25323. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25324. ' return Result;',
  25325. ' };',
  25326. '});',
  25327. 'this.r = this.TRec.$new();',
  25328. '']),
  25329. LinesToStr([ // $mod.$main
  25330. '$mod.THelper.Two = 1;',
  25331. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25332. 'var $with = $mod.TRec;',
  25333. '$mod.THelper.Two = 1;',
  25334. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25335. '$mod.THelper.Two = 1;',
  25336. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25337. 'var $with1 = $mod.r;',
  25338. '$mod.THelper.Two = 1;',
  25339. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25340. '']));
  25341. end;
  25342. procedure TTestModule.TestRecordHelper_Method_Call;
  25343. begin
  25344. StartProgram(false);
  25345. Add([
  25346. '{$modeswitch AdvancedRecords}',
  25347. 'type',
  25348. ' TRec = record',
  25349. ' procedure Run(w: word = 10);',
  25350. ' end;',
  25351. ' THelper = record helper for TRec',
  25352. ' function Foo(w: word = 1): word;',
  25353. ' end;',
  25354. 'procedure TRec.Run(w: word);',
  25355. 'begin',
  25356. ' Foo;',
  25357. ' Foo();',
  25358. ' Foo(2);',
  25359. ' Self.Foo;',
  25360. ' Self.Foo();',
  25361. ' Self.Foo(3);',
  25362. ' with Self do begin',
  25363. ' Foo;',
  25364. ' Foo();',
  25365. ' Foo(4);',
  25366. ' end;',
  25367. 'end;',
  25368. 'function THelper.foo(w: word): word;',
  25369. 'begin',
  25370. ' Run;',
  25371. ' Run();',
  25372. ' Run(11);',
  25373. ' Foo;',
  25374. ' Foo();',
  25375. ' Foo(12);',
  25376. ' Self.Foo;',
  25377. ' Self.Foo();',
  25378. ' Self.Foo(13);',
  25379. ' with Self do begin',
  25380. ' Foo;',
  25381. ' Foo();',
  25382. ' Foo(14);',
  25383. ' end;',
  25384. 'end;',
  25385. 'var Rec: TRec;',
  25386. 'begin',
  25387. ' Rec.Foo;',
  25388. ' Rec.Foo();',
  25389. ' Rec.Foo(21);',
  25390. ' with Rec do begin',
  25391. ' Foo;',
  25392. ' Foo();',
  25393. ' Foo(22);',
  25394. ' end;',
  25395. '']);
  25396. ConvertProgram;
  25397. CheckSource('TestRecordHelper_Method_Call',
  25398. LinesToStr([ // statements
  25399. 'rtl.recNewT(this, "TRec", function () {',
  25400. ' this.$eq = function (b) {',
  25401. ' return true;',
  25402. ' };',
  25403. ' this.$assign = function (s) {',
  25404. ' return this;',
  25405. ' };',
  25406. ' this.Run = function (w) {',
  25407. ' $mod.THelper.Foo.call(this, 1);',
  25408. ' $mod.THelper.Foo.call(this, 1);',
  25409. ' $mod.THelper.Foo.call(this, 2);',
  25410. ' $mod.THelper.Foo.call(this, 1);',
  25411. ' $mod.THelper.Foo.call(this, 1);',
  25412. ' $mod.THelper.Foo.call(this, 3);',
  25413. ' $mod.THelper.Foo.call(this, 1);',
  25414. ' $mod.THelper.Foo.call(this, 1);',
  25415. ' $mod.THelper.Foo.call(this, 4);',
  25416. ' };',
  25417. '});',
  25418. 'rtl.createHelper(this, "THelper", null, function () {',
  25419. ' this.Foo = function (w) {',
  25420. ' var Result = 0;',
  25421. ' this.Run(10);',
  25422. ' this.Run(10);',
  25423. ' this.Run(11);',
  25424. ' $mod.THelper.Foo.call(this, 1);',
  25425. ' $mod.THelper.Foo.call(this, 1);',
  25426. ' $mod.THelper.Foo.call(this, 12);',
  25427. ' $mod.THelper.Foo.call(this, 1);',
  25428. ' $mod.THelper.Foo.call(this, 1);',
  25429. ' $mod.THelper.Foo.call(this, 13);',
  25430. ' $mod.THelper.Foo.call(this, 1);',
  25431. ' $mod.THelper.Foo.call(this, 1);',
  25432. ' $mod.THelper.Foo.call(this, 14);',
  25433. ' return Result;',
  25434. ' };',
  25435. '});',
  25436. 'this.Rec = this.TRec.$new();',
  25437. '']),
  25438. LinesToStr([ // $mod.$main
  25439. '$mod.THelper.Foo.call($mod.Rec, 1);',
  25440. '$mod.THelper.Foo.call($mod.Rec, 1);',
  25441. '$mod.THelper.Foo.call($mod.Rec, 21);',
  25442. 'var $with = $mod.Rec;',
  25443. '$mod.THelper.Foo.call($with, 1);',
  25444. '$mod.THelper.Foo.call($with, 1);',
  25445. '$mod.THelper.Foo.call($with, 22);',
  25446. '']));
  25447. end;
  25448. procedure TTestModule.TestRecordHelper_Constructor;
  25449. begin
  25450. StartProgram(false);
  25451. Add([
  25452. '{$modeswitch AdvancedRecords}',
  25453. 'type',
  25454. ' TRec = record',
  25455. ' constructor Create(w: word);',
  25456. ' end;',
  25457. ' THelper = record helper for TRec',
  25458. ' constructor NewHlp(w: word);',
  25459. ' end;',
  25460. 'var',
  25461. ' Rec: TRec;',
  25462. 'constructor TRec.Create(w: word);',
  25463. 'begin',
  25464. ' NewHlp(2);', // normal call
  25465. ' trec.NewHlp(3);', // new instance
  25466. 'end;',
  25467. 'constructor THelper.NewHlp(w: word);',
  25468. 'begin',
  25469. ' create(2);', // normal call
  25470. ' trec.create(3);', // new instance
  25471. ' NewHlp(4);', // normal call
  25472. ' trec.NewHlp(5);', // new instance
  25473. 'end;',
  25474. 'begin',
  25475. ' rec.newhlp(2);', // normal call
  25476. ' with rec do newhlp(12);', // normal call
  25477. ' trec.newhlp(3);', // new instance
  25478. ' with trec do newhlp(13);', // new instance
  25479. '']);
  25480. ConvertProgram;
  25481. CheckSource('TestRecordHelper_Constructor',
  25482. LinesToStr([ // statements
  25483. 'rtl.recNewT(this, "TRec", function () {',
  25484. ' this.$eq = function (b) {',
  25485. ' return true;',
  25486. ' };',
  25487. ' this.$assign = function (s) {',
  25488. ' return this;',
  25489. ' };',
  25490. ' this.Create = function (w) {',
  25491. ' $mod.THelper.NewHlp.call(this, 2);',
  25492. ' $mod.THelper.$new("NewHlp", [3]);',
  25493. ' return this;',
  25494. ' };',
  25495. '});',
  25496. 'rtl.createHelper(this, "THelper", null, function () {',
  25497. ' this.NewHlp = function (w) {',
  25498. ' this.Create(2);',
  25499. ' $mod.TRec.$new().Create(3);',
  25500. ' $mod.THelper.NewHlp.call(this, 4);',
  25501. ' $mod.THelper.$new("NewHlp", [5]);',
  25502. ' return this;',
  25503. ' };',
  25504. ' this.$new = function (fn, args) {',
  25505. ' return this[fn].apply($mod.TRec.$new(), args);',
  25506. ' };',
  25507. '});',
  25508. 'this.Rec = this.TRec.$new();',
  25509. '']),
  25510. LinesToStr([ // $mod.$main
  25511. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  25512. 'var $with = $mod.Rec;',
  25513. '$mod.THelper.NewHlp.call($with, 12);',
  25514. '$mod.THelper.$new("NewHlp", [3]);',
  25515. 'var $with1 = $mod.TRec;',
  25516. '$mod.THelper.$new("NewHlp", [13]);',
  25517. '']));
  25518. end;
  25519. procedure TTestModule.TestTypeHelper_ClassVar;
  25520. begin
  25521. StartProgram(false);
  25522. Add([
  25523. '{$modeswitch typehelpers}',
  25524. 'type',
  25525. ' THelper = type helper for byte',
  25526. ' const',
  25527. ' One = 1;',
  25528. ' Two: word = 2;',
  25529. ' class var',
  25530. ' Glob: word;',
  25531. ' function Foo(w: word): word;',
  25532. ' class function Bar(w: word): word; static;',
  25533. ' end;',
  25534. 'function THelper.foo(w: word): word;',
  25535. 'begin',
  25536. ' Result:=w;',
  25537. ' Two:=One+w;',
  25538. ' Glob:=Glob;',
  25539. ' Result:=Self.Glob;',
  25540. ' Self.Glob:=Self.Glob;',
  25541. ' with Self do Glob:=Glob;',
  25542. 'end;',
  25543. 'class function THelper.bar(w: word): word;',
  25544. 'begin',
  25545. ' Result:=w;',
  25546. ' Two:=One;',
  25547. ' Glob:=Glob;',
  25548. 'end;',
  25549. 'var b: byte;',
  25550. 'begin',
  25551. ' byte.two:=byte.one;',
  25552. ' byte.Glob:=byte.Glob;',
  25553. ' with byte do begin',
  25554. ' two:=one;',
  25555. ' Glob:=Glob;',
  25556. ' end;',
  25557. ' b.two:=b.one;',
  25558. ' b.Glob:=b.Glob;',
  25559. ' with b do begin',
  25560. ' two:=one;',
  25561. ' Glob:=Glob;',
  25562. ' end;',
  25563. '']);
  25564. ConvertProgram;
  25565. CheckSource('TestTypeHelper_ClassVar',
  25566. LinesToStr([ // statements
  25567. 'rtl.createHelper(this, "THelper", null, function () {',
  25568. ' this.One = 1;',
  25569. ' this.Two = 2;',
  25570. ' this.Glob = 0;',
  25571. ' this.Foo = function (w) {',
  25572. ' var Result = 0;',
  25573. ' Result = w;',
  25574. ' $mod.THelper.Two = 1 + w;',
  25575. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25576. ' Result = $mod.THelper.Glob;',
  25577. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25578. ' var $with = this.get();',
  25579. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25580. ' return Result;',
  25581. ' };',
  25582. ' this.Bar = function (w) {',
  25583. ' var Result = 0;',
  25584. ' Result = w;',
  25585. ' $mod.THelper.Two = 1;',
  25586. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25587. ' return Result;',
  25588. ' };',
  25589. '});',
  25590. 'this.b = 0;',
  25591. '']),
  25592. LinesToStr([ // $mod.$main
  25593. '$mod.THelper.Two = 1;',
  25594. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25595. '$mod.THelper.Two = 1;',
  25596. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25597. '$mod.THelper.Two = 1;',
  25598. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25599. 'var $with = $mod.b;',
  25600. '$mod.THelper.Two = 1;',
  25601. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25602. '']));
  25603. end;
  25604. procedure TTestModule.TestTypeHelper_PassResultElement;
  25605. begin
  25606. StartProgram(false);
  25607. Add([
  25608. '{$modeswitch typehelpers}',
  25609. 'type',
  25610. ' THelper = type helper for word',
  25611. ' procedure DoIt(e: byte = 123);',
  25612. ' class procedure DoSome(e: byte = 456); static;',
  25613. ' end;',
  25614. 'procedure THelper.DoIt(e: byte);',
  25615. 'begin',
  25616. 'end;',
  25617. 'class procedure THelper.DoSome(e: byte);',
  25618. 'begin',
  25619. 'end;',
  25620. 'function Foo(w: word): word;',
  25621. 'begin',
  25622. ' Result.DoIt;',
  25623. ' Result.DoIt();',
  25624. ' Result.DoSome;',
  25625. ' Result.DoSome();',
  25626. ' with Result do begin',
  25627. ' DoIt;',
  25628. ' DoIt();',
  25629. ' DoSome;',
  25630. ' DoSome();',
  25631. ' end;',
  25632. 'end;',
  25633. 'begin',
  25634. '']);
  25635. ConvertProgram;
  25636. CheckSource('TestTypeHelper_PassResultElement',
  25637. LinesToStr([ // statements
  25638. 'rtl.createHelper(this, "THelper", null, function () {',
  25639. ' this.DoIt = function (e) {',
  25640. ' };',
  25641. ' this.DoSome = function (e) {',
  25642. ' };',
  25643. '});',
  25644. 'this.Foo = function (w) {',
  25645. ' var Result = 0;',
  25646. ' $mod.THelper.DoIt.call({',
  25647. ' get: function () {',
  25648. ' return Result;',
  25649. ' },',
  25650. ' set: function (v) {',
  25651. ' Result = v;',
  25652. ' }',
  25653. ' }, 123);',
  25654. ' $mod.THelper.DoIt.call({',
  25655. ' get: function () {',
  25656. ' return Result;',
  25657. ' },',
  25658. ' set: function (v) {',
  25659. ' Result = v;',
  25660. ' }',
  25661. ' }, 123);',
  25662. ' $mod.THelper.DoSome(456);',
  25663. ' $mod.THelper.DoSome(456);',
  25664. ' $mod.THelper.DoIt.call({',
  25665. ' get: function () {',
  25666. ' return Result;',
  25667. ' },',
  25668. ' set: function (v) {',
  25669. ' Result = v;',
  25670. ' }',
  25671. ' }, 123);',
  25672. ' $mod.THelper.DoIt.call({',
  25673. ' get: function () {',
  25674. ' return Result;',
  25675. ' },',
  25676. ' set: function (v) {',
  25677. ' Result = v;',
  25678. ' }',
  25679. ' }, 123);',
  25680. ' $mod.THelper.DoSome(456);',
  25681. ' $mod.THelper.DoSome(456);',
  25682. ' return Result;',
  25683. '};',
  25684. '']),
  25685. LinesToStr([ // $mod.$main
  25686. '']));
  25687. end;
  25688. procedure TTestModule.TestTypeHelper_PassArgs;
  25689. begin
  25690. StartProgram(false);
  25691. Add([
  25692. '{$modeswitch typehelpers}',
  25693. 'type',
  25694. ' THelper = type helper for word',
  25695. ' procedure DoIt(e: byte = 123);',
  25696. ' end;',
  25697. 'procedure THelper.DoIt(e: byte);',
  25698. 'begin',
  25699. 'end;',
  25700. 'procedure FooDefault(a: word);',
  25701. 'begin',
  25702. ' a.DoIt;',
  25703. ' with a do DoIt;',
  25704. 'end;',
  25705. 'procedure FooConst(const a: word);',
  25706. 'begin',
  25707. ' a.DoIt;',
  25708. ' with a do DoIt;',
  25709. 'end;',
  25710. 'procedure FooVar(var a: word);',
  25711. 'begin',
  25712. ' a.DoIt;',
  25713. ' with a do DoIt;',
  25714. 'end;',
  25715. 'begin',
  25716. '']);
  25717. ConvertProgram;
  25718. CheckSource('TestTypeHelper_PassArgs',
  25719. LinesToStr([ // statements
  25720. 'rtl.createHelper(this, "THelper", null, function () {',
  25721. ' this.DoIt = function (e) {',
  25722. ' };',
  25723. '});',
  25724. 'this.FooDefault = function (a) {',
  25725. ' $mod.THelper.DoIt.call({',
  25726. ' get: function () {',
  25727. ' return a;',
  25728. ' },',
  25729. ' set: function (v) {',
  25730. ' a = v;',
  25731. ' }',
  25732. ' }, 123);',
  25733. ' $mod.THelper.DoIt.call({',
  25734. ' get: function () {',
  25735. ' return a;',
  25736. ' },',
  25737. ' set: function (v) {',
  25738. ' a = v;',
  25739. ' }',
  25740. ' }, 123);',
  25741. '};',
  25742. 'this.FooConst = function (a) {',
  25743. ' $mod.THelper.DoIt.call({',
  25744. ' get: function () {',
  25745. ' return a;',
  25746. ' },',
  25747. ' set: function (v) {',
  25748. ' rtl.raiseE("EPropReadOnly");',
  25749. ' }',
  25750. ' }, 123);',
  25751. ' $mod.THelper.DoIt.call({',
  25752. ' get: function () {',
  25753. ' return a;',
  25754. ' },',
  25755. ' set: function () {',
  25756. ' rtl.raiseE("EPropReadOnly");',
  25757. ' }',
  25758. ' }, 123);',
  25759. '};',
  25760. 'this.FooVar = function (a) {',
  25761. ' $mod.THelper.DoIt.call(a, 123);',
  25762. ' var $with = a.get();',
  25763. ' $mod.THelper.DoIt.call(a, 123);',
  25764. '};',
  25765. '']),
  25766. LinesToStr([ // $mod.$main
  25767. '']));
  25768. end;
  25769. procedure TTestModule.TestTypeHelper_PassVarConst;
  25770. begin
  25771. StartProgram(false);
  25772. Add([
  25773. '{$modeswitch typehelpers}',
  25774. 'type',
  25775. ' THelper = type helper for word',
  25776. ' procedure DoIt(e: byte = 123);',
  25777. ' end;',
  25778. 'procedure THelper.DoIt(e: byte);',
  25779. 'begin',
  25780. 'end;',
  25781. 'var a: word;',
  25782. 'const c: word = 2;',
  25783. '{$writeableconst off}',
  25784. 'const r: word = 3;',
  25785. 'begin',
  25786. ' a.DoIt;',
  25787. ' with a do DoIt;',
  25788. ' c.DoIt;',
  25789. ' with c do DoIt;',
  25790. ' r.DoIt;',
  25791. ' with r do DoIt;',
  25792. '']);
  25793. ConvertProgram;
  25794. CheckSource('TestTypeHelper_PassVarConst',
  25795. LinesToStr([ // statements
  25796. 'rtl.createHelper(this, "THelper", null, function () {',
  25797. ' this.DoIt = function (e) {',
  25798. ' };',
  25799. '});',
  25800. 'this.a = 0;',
  25801. 'this.c = 2;',
  25802. 'this.r = 3;',
  25803. '']),
  25804. LinesToStr([ // $mod.$main
  25805. '$mod.THelper.DoIt.call({',
  25806. ' p: $mod,',
  25807. ' get: function () {',
  25808. ' return this.p.a;',
  25809. ' },',
  25810. ' set: function (v) {',
  25811. ' this.p.a = v;',
  25812. ' }',
  25813. '}, 123);',
  25814. 'var $with = $mod.a;',
  25815. '$mod.THelper.DoIt.call({',
  25816. ' get: function () {',
  25817. ' return $with;',
  25818. ' },',
  25819. ' set: function (v) {',
  25820. ' $with = v;',
  25821. ' }',
  25822. '}, 123);',
  25823. '$mod.THelper.DoIt.call({',
  25824. ' p: $mod,',
  25825. ' get: function () {',
  25826. ' return this.p.c;',
  25827. ' },',
  25828. ' set: function (v) {',
  25829. ' this.p.c = v;',
  25830. ' }',
  25831. '}, 123);',
  25832. 'var $with1 = $mod.c;',
  25833. '$mod.THelper.DoIt.call({',
  25834. ' get: function () {',
  25835. ' return $with1;',
  25836. ' },',
  25837. ' set: function (v) {',
  25838. ' $with1 = v;',
  25839. ' }',
  25840. '}, 123);',
  25841. '$mod.THelper.DoIt.call({',
  25842. ' get: function () {',
  25843. ' return 3;',
  25844. ' },',
  25845. ' set: function (v) {',
  25846. ' rtl.raiseE("EPropReadOnly");',
  25847. ' }',
  25848. '}, 123);',
  25849. 'var $with2 = 3;',
  25850. ' $mod.THelper.DoIt.call({',
  25851. ' get: function () {',
  25852. ' return $with2;',
  25853. ' },',
  25854. ' set: function () {',
  25855. ' rtl.raiseE("EPropReadOnly");',
  25856. ' }',
  25857. ' }, 123);',
  25858. '']));
  25859. end;
  25860. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25861. begin
  25862. StartProgram(false);
  25863. Add([
  25864. '{$modeswitch typehelpers}',
  25865. 'type',
  25866. ' THelper = type helper for word',
  25867. ' procedure DoIt(e: byte = 123);',
  25868. ' end;',
  25869. 'procedure THelper.DoIt(e: byte);',
  25870. 'begin',
  25871. 'end;',
  25872. 'function Foo(b: byte = 1): word;',
  25873. 'begin',
  25874. 'end;',
  25875. 'begin',
  25876. ' Foo.DoIt;',
  25877. ' Foo().DoIt;',
  25878. ' with Foo do DoIt;',
  25879. ' with Foo() do DoIt;',
  25880. '']);
  25881. ConvertProgram;
  25882. CheckSource('TestTypeHelper_PassFuncResult',
  25883. LinesToStr([ // statements
  25884. 'rtl.createHelper(this, "THelper", null, function () {',
  25885. ' this.DoIt = function (e) {',
  25886. ' };',
  25887. '});',
  25888. 'this.Foo = function (b) {',
  25889. ' var Result = 0;',
  25890. ' return Result;',
  25891. '};',
  25892. '']),
  25893. LinesToStr([ // $mod.$main
  25894. '$mod.THelper.DoIt.call({',
  25895. ' a: $mod.Foo(1),',
  25896. ' get: function () {',
  25897. ' return this.a;',
  25898. ' },',
  25899. ' set: function (v) {',
  25900. ' this.a = v;',
  25901. ' }',
  25902. '}, 123);',
  25903. '$mod.THelper.DoIt.call({',
  25904. ' a: $mod.Foo(1),',
  25905. ' get: function () {',
  25906. ' return this.a;',
  25907. ' },',
  25908. ' set: function (v) {',
  25909. ' this.a = v;',
  25910. ' }',
  25911. '}, 123);',
  25912. 'var $with = $mod.Foo(1);',
  25913. '$mod.THelper.DoIt.call({',
  25914. ' get: function () {',
  25915. ' return $with;',
  25916. ' },',
  25917. ' set: function (v) {',
  25918. ' $with = v;',
  25919. ' }',
  25920. '}, 123);',
  25921. 'var $with1 = $mod.Foo(1);',
  25922. '$mod.THelper.DoIt.call({',
  25923. ' get: function () {',
  25924. ' return $with1;',
  25925. ' },',
  25926. ' set: function (v) {',
  25927. ' $with1 = v;',
  25928. ' }',
  25929. '}, 123);',
  25930. '']));
  25931. end;
  25932. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25933. begin
  25934. StartProgram(false);
  25935. Add([
  25936. '{$modeswitch typehelpers}',
  25937. 'type',
  25938. ' TObject = class',
  25939. ' FField: word;',
  25940. ' procedure SetField(Value: word);',
  25941. ' property Field: word read FField write SetField;',
  25942. ' end;',
  25943. ' THelper = type helper for word',
  25944. ' procedure Fly;',
  25945. ' class procedure Run; static;',
  25946. ' end;',
  25947. 'procedure TObject.SetField(Value: word);',
  25948. 'begin',
  25949. ' Field.Fly;',
  25950. ' Field.Run;',
  25951. ' Self.Field.Fly;',
  25952. ' Self.Field.Run;',
  25953. ' with Self do begin',
  25954. ' Field.Fly;',
  25955. ' Field.Run;',
  25956. ' end;',
  25957. ' with Self.Field do begin',
  25958. ' Fly;',
  25959. ' Run;',
  25960. ' end;',
  25961. 'end;',
  25962. 'procedure THelper.Fly;',
  25963. 'begin',
  25964. 'end;',
  25965. 'class procedure THelper.Run;',
  25966. 'begin',
  25967. 'end;',
  25968. 'var',
  25969. ' o: TObject;',
  25970. 'begin',
  25971. ' o.Field.Fly;',
  25972. ' o.Field.Run;',
  25973. ' with o do begin',
  25974. ' Field.Fly;',
  25975. ' Field.Run;',
  25976. ' end;',
  25977. ' with o.Field do begin',
  25978. ' Fly;',
  25979. ' Run;',
  25980. ' end;',
  25981. '']);
  25982. ConvertProgram;
  25983. CheckSource('TestTypeHelper_PassPropertyField',
  25984. LinesToStr([ // statements
  25985. 'rtl.createClass(this, "TObject", null, function () {',
  25986. ' this.$init = function () {',
  25987. ' this.FField = 0;',
  25988. ' };',
  25989. ' this.$final = function () {',
  25990. ' };',
  25991. ' this.SetField = function (Value) {',
  25992. ' $mod.THelper.Fly.call({',
  25993. ' p: this,',
  25994. ' get: function () {',
  25995. ' return this.p.FField;',
  25996. ' },',
  25997. ' set: function (v) {',
  25998. ' this.p.FField = v;',
  25999. ' }',
  26000. ' });',
  26001. ' $mod.THelper.Run();',
  26002. ' $mod.THelper.Fly.call({',
  26003. ' p: this,',
  26004. ' get: function () {',
  26005. ' return this.p.FField;',
  26006. ' },',
  26007. ' set: function (v) {',
  26008. ' this.p.FField = v;',
  26009. ' }',
  26010. ' });',
  26011. ' $mod.THelper.Run();',
  26012. ' $mod.THelper.Fly.call({',
  26013. ' p: this,',
  26014. ' get: function () {',
  26015. ' return this.p.FField;',
  26016. ' },',
  26017. ' set: function (v) {',
  26018. ' this.p.FField = v;',
  26019. ' }',
  26020. ' });',
  26021. ' $mod.THelper.Run();',
  26022. ' var $with = this.FField;',
  26023. ' $mod.THelper.Fly.call({',
  26024. ' get: function () {',
  26025. ' return $with;',
  26026. ' },',
  26027. ' set: function (v) {',
  26028. ' $with = v;',
  26029. ' }',
  26030. ' });',
  26031. ' $mod.THelper.Run();',
  26032. ' };',
  26033. '});',
  26034. 'rtl.createHelper(this, "THelper", null, function () {',
  26035. ' this.Fly = function () {',
  26036. ' };',
  26037. ' this.Run = function () {',
  26038. ' };',
  26039. '});',
  26040. 'this.o = null;',
  26041. '']),
  26042. LinesToStr([ // $mod.$main
  26043. '$mod.THelper.Fly.call({',
  26044. ' p: $mod.o,',
  26045. ' get: function () {',
  26046. ' return this.p.FField;',
  26047. ' },',
  26048. ' set: function (v) {',
  26049. ' this.p.FField = v;',
  26050. ' }',
  26051. '});',
  26052. '$mod.THelper.Run();',
  26053. 'var $with = $mod.o;',
  26054. '$mod.THelper.Fly.call({',
  26055. ' p: $with,',
  26056. ' get: function () {',
  26057. ' return this.p.FField;',
  26058. ' },',
  26059. ' set: function (v) {',
  26060. ' this.p.FField = v;',
  26061. ' }',
  26062. '});',
  26063. '$mod.THelper.Run();',
  26064. 'var $with1 = $mod.o.FField;',
  26065. '$mod.THelper.Fly.call({',
  26066. ' get: function () {',
  26067. ' return $with1;',
  26068. ' },',
  26069. ' set: function (v) {',
  26070. ' $with1 = v;',
  26071. ' }',
  26072. '});',
  26073. '$mod.THelper.Run();',
  26074. '']));
  26075. end;
  26076. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  26077. begin
  26078. StartProgram(false);
  26079. Add([
  26080. '{$modeswitch typehelpers}',
  26081. 'type',
  26082. ' TObject = class',
  26083. ' FField: word;',
  26084. ' function GetField: word;',
  26085. ' property Field: word read GetField write FField;',
  26086. ' end;',
  26087. ' THelper = type helper for word',
  26088. ' procedure Fly;',
  26089. ' class procedure Run; static;',
  26090. ' end;',
  26091. 'function TObject.GetField: word;',
  26092. 'begin',
  26093. ' Field.Fly;',
  26094. ' Field.Run;',
  26095. ' Self.Field.Fly;',
  26096. ' Self.Field.Run;',
  26097. ' with Self do begin',
  26098. ' Field.Fly;',
  26099. ' Field.Run;',
  26100. ' end;',
  26101. ' with Self.Field do begin',
  26102. ' Fly;',
  26103. ' Run;',
  26104. ' end;',
  26105. 'end;',
  26106. 'procedure THelper.Fly;',
  26107. 'begin',
  26108. 'end;',
  26109. 'class procedure THelper.Run;',
  26110. 'begin',
  26111. 'end;',
  26112. 'var',
  26113. ' o: TObject;',
  26114. 'begin',
  26115. ' o.Field.Fly;',
  26116. ' o.Field.Run;',
  26117. ' with o do begin',
  26118. ' Field.Fly;',
  26119. ' Field.Run;',
  26120. ' end;',
  26121. ' with o.Field do begin',
  26122. ' Fly;',
  26123. ' Run;',
  26124. ' end;',
  26125. '']);
  26126. ConvertProgram;
  26127. CheckSource('TestTypeHelper_PassPropertyGetter',
  26128. LinesToStr([ // statements
  26129. 'rtl.createClass(this, "TObject", null, function () {',
  26130. ' this.$init = function () {',
  26131. ' this.FField = 0;',
  26132. ' };',
  26133. ' this.$final = function () {',
  26134. ' };',
  26135. ' this.GetField = function () {',
  26136. ' var Result = 0;',
  26137. ' $mod.THelper.Fly.call({',
  26138. ' p: this.GetField(),',
  26139. ' get: function () {',
  26140. ' return this.p;',
  26141. ' },',
  26142. ' set: function (v) {',
  26143. ' this.p = v;',
  26144. ' }',
  26145. ' });',
  26146. ' $mod.THelper.Run();',
  26147. ' $mod.THelper.Fly.call({',
  26148. ' p: this.GetField(),',
  26149. ' get: function () {',
  26150. ' return this.p;',
  26151. ' },',
  26152. ' set: function (v) {',
  26153. ' this.p = v;',
  26154. ' }',
  26155. ' });',
  26156. ' $mod.THelper.Run();',
  26157. ' $mod.THelper.Fly.call({',
  26158. ' p: this.GetField(),',
  26159. ' get: function () {',
  26160. ' return this.p;',
  26161. ' },',
  26162. ' set: function (v) {',
  26163. ' this.p = v;',
  26164. ' }',
  26165. ' });',
  26166. ' $mod.THelper.Run();',
  26167. ' var $with = this.GetField();',
  26168. ' $mod.THelper.Fly.call({',
  26169. ' get: function () {',
  26170. ' return $with;',
  26171. ' },',
  26172. ' set: function (v) {',
  26173. ' $with = v;',
  26174. ' }',
  26175. ' });',
  26176. ' $mod.THelper.Run();',
  26177. ' return Result;',
  26178. ' };',
  26179. '});',
  26180. 'rtl.createHelper(this, "THelper", null, function () {',
  26181. ' this.Fly = function () {',
  26182. ' };',
  26183. ' this.Run = function () {',
  26184. ' };',
  26185. '});',
  26186. 'this.o = null;',
  26187. '']),
  26188. LinesToStr([ // $mod.$main
  26189. '$mod.THelper.Fly.call({',
  26190. ' p: $mod.o.GetField(),',
  26191. ' get: function () {',
  26192. ' return this.p;',
  26193. ' },',
  26194. ' set: function (v) {',
  26195. ' this.p = v;',
  26196. ' }',
  26197. '});',
  26198. '$mod.THelper.Run();',
  26199. 'var $with = $mod.o;',
  26200. '$mod.THelper.Fly.call({',
  26201. ' p: $with.GetField(),',
  26202. ' get: function () {',
  26203. ' return this.p;',
  26204. ' },',
  26205. ' set: function (v) {',
  26206. ' this.p = v;',
  26207. ' }',
  26208. '});',
  26209. '$mod.THelper.Run();',
  26210. 'var $with1 = $mod.o.GetField();',
  26211. '$mod.THelper.Fly.call({',
  26212. ' get: function () {',
  26213. ' return $with1;',
  26214. ' },',
  26215. ' set: function (v) {',
  26216. ' $with1 = v;',
  26217. ' }',
  26218. '});',
  26219. '$mod.THelper.Run();',
  26220. '']));
  26221. end;
  26222. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  26223. begin
  26224. StartProgram(false);
  26225. Add([
  26226. '{$modeswitch typehelpers}',
  26227. 'type',
  26228. ' TObject = class',
  26229. ' class var FField: word;',
  26230. ' class procedure SetField(Value: word);',
  26231. ' class property Field: word read FField write SetField;',
  26232. ' end;',
  26233. ' THelper = type helper for word',
  26234. ' procedure Fly(n: byte);',
  26235. ' end;',
  26236. 'class procedure TObject.SetField(Value: word);',
  26237. 'begin',
  26238. ' Field.Fly(1);',
  26239. ' Self.Field.Fly(2);',
  26240. ' with Self do Field.Fly(3);',
  26241. ' with Self.Field do Fly(4);',
  26242. ' TObject.Field.Fly(5);',
  26243. ' with TObject do Field.Fly(6);',
  26244. ' with TObject.Field do Fly(7);',
  26245. 'end;',
  26246. 'procedure THelper.Fly(n: byte);',
  26247. 'begin',
  26248. 'end;',
  26249. 'var',
  26250. ' o: TObject;',
  26251. 'begin',
  26252. ' o.Field.Fly(11);',
  26253. ' with o do Field.Fly(12);',
  26254. ' with o.Field do Fly(13);',
  26255. ' TObject.Field.Fly(14);',
  26256. ' with TObject do Field.Fly(15);',
  26257. ' with TObject.Field do Fly(16);',
  26258. '']);
  26259. ConvertProgram;
  26260. CheckSource('TestTypeHelper_PassClassPropertyField',
  26261. LinesToStr([ // statements
  26262. 'rtl.createClass(this, "TObject", null, function () {',
  26263. ' this.FField = 0;',
  26264. ' this.$init = function () {',
  26265. ' };',
  26266. ' this.$final = function () {',
  26267. ' };',
  26268. ' this.SetField = function (Value) {',
  26269. ' $mod.THelper.Fly.call({',
  26270. ' p: this,',
  26271. ' get: function () {',
  26272. ' return this.p.FField;',
  26273. ' },',
  26274. ' set: function (v) {',
  26275. ' $mod.TObject.FField = v;',
  26276. ' }',
  26277. ' }, 1);',
  26278. ' $mod.THelper.Fly.call({',
  26279. ' p: this,',
  26280. ' get: function () {',
  26281. ' return this.p.FField;',
  26282. ' },',
  26283. ' set: function (v) {',
  26284. ' $mod.TObject.FField = v;',
  26285. ' }',
  26286. ' }, 2);',
  26287. ' $mod.THelper.Fly.call({',
  26288. ' p: this,',
  26289. ' get: function () {',
  26290. ' return this.p.FField;',
  26291. ' },',
  26292. ' set: function (v) {',
  26293. ' $mod.TObject.FField = v;',
  26294. ' }',
  26295. ' }, 3);',
  26296. ' var $with = this.FField;',
  26297. ' $mod.THelper.Fly.call({',
  26298. ' get: function () {',
  26299. ' return $with;',
  26300. ' },',
  26301. ' set: function (v) {',
  26302. ' $with = v;',
  26303. ' }',
  26304. ' }, 4);',
  26305. ' $mod.THelper.Fly.call({',
  26306. ' p: $mod.TObject,',
  26307. ' get: function () {',
  26308. ' return this.p.FField;',
  26309. ' },',
  26310. ' set: function (v) {',
  26311. ' $mod.TObject.FField = v;',
  26312. ' }',
  26313. ' }, 5);',
  26314. ' var $with1 = $mod.TObject;',
  26315. ' $mod.THelper.Fly.call({',
  26316. ' p: $with1,',
  26317. ' get: function () {',
  26318. ' return this.p.FField;',
  26319. ' },',
  26320. ' set: function (v) {',
  26321. ' $mod.TObject.FField = v;',
  26322. ' }',
  26323. ' }, 6);',
  26324. ' var $with2 = $mod.TObject.FField;',
  26325. ' $mod.THelper.Fly.call({',
  26326. ' get: function () {',
  26327. ' return $with2;',
  26328. ' },',
  26329. ' set: function (v) {',
  26330. ' $with2 = v;',
  26331. ' }',
  26332. ' }, 7);',
  26333. ' };',
  26334. '});',
  26335. 'rtl.createHelper(this, "THelper", null, function () {',
  26336. ' this.Fly = function (n) {',
  26337. ' };',
  26338. '});',
  26339. 'this.o = null;',
  26340. '']),
  26341. LinesToStr([ // $mod.$main
  26342. '$mod.THelper.Fly.call({',
  26343. ' p: $mod.o,',
  26344. ' get: function () {',
  26345. ' return this.p.FField;',
  26346. ' },',
  26347. ' set: function (v) {',
  26348. ' $mod.TObject.FField = v;',
  26349. ' }',
  26350. '}, 11);',
  26351. 'var $with = $mod.o;',
  26352. '$mod.THelper.Fly.call({',
  26353. ' p: $with,',
  26354. ' get: function () {',
  26355. ' return this.p.FField;',
  26356. ' },',
  26357. ' set: function (v) {',
  26358. ' $mod.TObject.FField = v;',
  26359. ' }',
  26360. '}, 12);',
  26361. 'var $with1 = $mod.o.FField;',
  26362. '$mod.THelper.Fly.call({',
  26363. ' get: function () {',
  26364. ' return $with1;',
  26365. ' },',
  26366. ' set: function (v) {',
  26367. ' $with1 = v;',
  26368. ' }',
  26369. '}, 13);',
  26370. '$mod.THelper.Fly.call({',
  26371. ' p: $mod.TObject,',
  26372. ' get: function () {',
  26373. ' return this.p.FField;',
  26374. ' },',
  26375. ' set: function (v) {',
  26376. ' $mod.TObject.FField = v;',
  26377. ' }',
  26378. '}, 14);',
  26379. 'var $with2 = $mod.TObject;',
  26380. '$mod.THelper.Fly.call({',
  26381. ' p: $with2,',
  26382. ' get: function () {',
  26383. ' return this.p.FField;',
  26384. ' },',
  26385. ' set: function (v) {',
  26386. ' $mod.TObject.FField = v;',
  26387. ' }',
  26388. '}, 15);',
  26389. 'var $with3 = $mod.TObject.FField;',
  26390. '$mod.THelper.Fly.call({',
  26391. ' get: function () {',
  26392. ' return $with3;',
  26393. ' },',
  26394. ' set: function (v) {',
  26395. ' $with3 = v;',
  26396. ' }',
  26397. '}, 16);',
  26398. '']));
  26399. end;
  26400. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  26401. begin
  26402. StartProgram(false);
  26403. Add([
  26404. '{$modeswitch typehelpers}',
  26405. 'type',
  26406. ' TObject = class',
  26407. ' class var FField: word;',
  26408. ' class function GetField: word; static;',
  26409. ' class property Field: word read GetField write FField;',
  26410. ' end;',
  26411. ' THelper = type helper for word',
  26412. ' procedure Fly(n: byte);',
  26413. ' end;',
  26414. 'class function TObject.GetField: word;',
  26415. 'begin',
  26416. ' Field.Fly(1);',
  26417. ' TObject.Field.Fly(5);',
  26418. ' with TObject do Field.Fly(6);',
  26419. ' with TObject.Field do Fly(7);',
  26420. 'end;',
  26421. 'procedure THelper.Fly(n: byte);',
  26422. 'begin',
  26423. 'end;',
  26424. 'var',
  26425. ' o: TObject;',
  26426. 'begin',
  26427. ' o.Field.Fly(11);',
  26428. ' with o do Field.Fly(12);',
  26429. ' with o.Field do Fly(13);',
  26430. '']);
  26431. ConvertProgram;
  26432. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  26433. LinesToStr([ // statements
  26434. 'rtl.createClass(this, "TObject", null, function () {',
  26435. ' this.FField = 0;',
  26436. ' this.$init = function () {',
  26437. ' };',
  26438. ' this.$final = function () {',
  26439. ' };',
  26440. ' this.GetField = function () {',
  26441. ' var Result = 0;',
  26442. ' $mod.THelper.Fly.call({',
  26443. ' p: $mod.TObject.GetField(),',
  26444. ' get: function () {',
  26445. ' return this.p;',
  26446. ' },',
  26447. ' set: function (v) {',
  26448. ' this.p = v;',
  26449. ' }',
  26450. ' }, 1);',
  26451. ' $mod.THelper.Fly.call({',
  26452. ' p: $mod.TObject.GetField(),',
  26453. ' get: function () {',
  26454. ' return this.p;',
  26455. ' },',
  26456. ' set: function (v) {',
  26457. ' this.p = v;',
  26458. ' }',
  26459. ' }, 5);',
  26460. ' var $with = $mod.TObject;',
  26461. ' $mod.THelper.Fly.call({',
  26462. ' p: $with.GetField(),',
  26463. ' get: function () {',
  26464. ' return this.p;',
  26465. ' },',
  26466. ' set: function (v) {',
  26467. ' this.p = v;',
  26468. ' }',
  26469. ' }, 6);',
  26470. ' var $with1 = $mod.TObject.GetField();',
  26471. ' $mod.THelper.Fly.call({',
  26472. ' get: function () {',
  26473. ' return $with1;',
  26474. ' },',
  26475. ' set: function (v) {',
  26476. ' $with1 = v;',
  26477. ' }',
  26478. ' }, 7);',
  26479. ' return Result;',
  26480. ' };',
  26481. '});',
  26482. 'rtl.createHelper(this, "THelper", null, function () {',
  26483. ' this.Fly = function (n) {',
  26484. ' };',
  26485. '});',
  26486. 'this.o = null;',
  26487. '']),
  26488. LinesToStr([ // $mod.$main
  26489. '$mod.THelper.Fly.call({',
  26490. ' p: $mod.TObject.GetField(),',
  26491. ' get: function () {',
  26492. ' return this.p;',
  26493. ' },',
  26494. ' set: function (v) {',
  26495. ' this.p = v;',
  26496. ' }',
  26497. '}, 11);',
  26498. 'var $with = $mod.o;',
  26499. '$mod.THelper.Fly.call({',
  26500. ' p: $with.GetField(),',
  26501. ' get: function () {',
  26502. ' return this.p;',
  26503. ' },',
  26504. ' set: function (v) {',
  26505. ' this.p = v;',
  26506. ' }',
  26507. '}, 12);',
  26508. 'var $with1 = $mod.TObject.GetField();',
  26509. '$mod.THelper.Fly.call({',
  26510. ' get: function () {',
  26511. ' return $with1;',
  26512. ' },',
  26513. ' set: function (v) {',
  26514. ' $with1 = v;',
  26515. ' }',
  26516. '}, 13);',
  26517. '']));
  26518. end;
  26519. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  26520. begin
  26521. StartProgram(false);
  26522. Add([
  26523. '{$modeswitch typehelpers}',
  26524. 'type',
  26525. ' TObject = class',
  26526. ' class var FField: word;',
  26527. ' class function GetField: word;',
  26528. ' class property Field: word read GetField write FField;',
  26529. ' end;',
  26530. ' TClass = class of TObject;',
  26531. ' THelper = type helper for word',
  26532. ' procedure Fly(n: byte);',
  26533. ' end;',
  26534. 'class function TObject.GetField: word;',
  26535. 'begin',
  26536. ' Field.Fly(1);',
  26537. ' Self.Field.Fly(5);',
  26538. ' with Self do Field.Fly(6);',
  26539. ' with Self.Field do Fly(7);',
  26540. 'end;',
  26541. 'procedure THelper.Fly(n: byte);',
  26542. 'begin',
  26543. 'end;',
  26544. 'var',
  26545. ' o: TObject;',
  26546. ' c: TClass;',
  26547. 'begin',
  26548. ' o.Field.Fly(11);',
  26549. ' with o do Field.Fly(12);',
  26550. ' with o.Field do Fly(13);',
  26551. ' c.Field.Fly(14);',
  26552. ' with c do Field.Fly(15);',
  26553. ' with c.Field do Fly(16);',
  26554. '']);
  26555. ConvertProgram;
  26556. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  26557. LinesToStr([ // statements
  26558. 'rtl.createClass(this, "TObject", null, function () {',
  26559. ' this.FField = 0;',
  26560. ' this.$init = function () {',
  26561. ' };',
  26562. ' this.$final = function () {',
  26563. ' };',
  26564. ' this.GetField = function () {',
  26565. ' var Result = 0;',
  26566. ' $mod.THelper.Fly.call({',
  26567. ' p: this.GetField(),',
  26568. ' get: function () {',
  26569. ' return this.p;',
  26570. ' },',
  26571. ' set: function (v) {',
  26572. ' this.p = v;',
  26573. ' }',
  26574. ' }, 1);',
  26575. ' $mod.THelper.Fly.call({',
  26576. ' p: this.GetField(),',
  26577. ' get: function () {',
  26578. ' return this.p;',
  26579. ' },',
  26580. ' set: function (v) {',
  26581. ' this.p = v;',
  26582. ' }',
  26583. ' }, 5);',
  26584. ' $mod.THelper.Fly.call({',
  26585. ' p: this.GetField(),',
  26586. ' get: function () {',
  26587. ' return this.p;',
  26588. ' },',
  26589. ' set: function (v) {',
  26590. ' this.p = v;',
  26591. ' }',
  26592. ' }, 6);',
  26593. ' var $with = this.GetField();',
  26594. ' $mod.THelper.Fly.call({',
  26595. ' get: function () {',
  26596. ' return $with;',
  26597. ' },',
  26598. ' set: function (v) {',
  26599. ' $with = v;',
  26600. ' }',
  26601. ' }, 7);',
  26602. ' return Result;',
  26603. ' };',
  26604. '});',
  26605. 'rtl.createHelper(this, "THelper", null, function () {',
  26606. ' this.Fly = function (n) {',
  26607. ' };',
  26608. '});',
  26609. 'this.o = null;',
  26610. 'this.c = null;',
  26611. '']),
  26612. LinesToStr([ // $mod.$main
  26613. '$mod.THelper.Fly.call({',
  26614. ' p: $mod.o.$class.GetField(),',
  26615. ' get: function () {',
  26616. ' return this.p;',
  26617. ' },',
  26618. ' set: function (v) {',
  26619. ' this.p = v;',
  26620. ' }',
  26621. '}, 11);',
  26622. 'var $with = $mod.o;',
  26623. '$mod.THelper.Fly.call({',
  26624. ' p: $with.$class.GetField(),',
  26625. ' get: function () {',
  26626. ' return this.p;',
  26627. ' },',
  26628. ' set: function (v) {',
  26629. ' this.p = v;',
  26630. ' }',
  26631. '}, 12);',
  26632. 'var $with1 = $mod.o.$class.GetField();',
  26633. '$mod.THelper.Fly.call({',
  26634. ' get: function () {',
  26635. ' return $with1;',
  26636. ' },',
  26637. ' set: function (v) {',
  26638. ' $with1 = v;',
  26639. ' }',
  26640. '}, 13);',
  26641. '$mod.THelper.Fly.call({',
  26642. ' p: $mod.c.GetField(),',
  26643. ' get: function () {',
  26644. ' return this.p;',
  26645. ' },',
  26646. ' set: function (v) {',
  26647. ' this.p = v;',
  26648. ' }',
  26649. '}, 14);',
  26650. 'var $with2 = $mod.c;',
  26651. '$mod.THelper.Fly.call({',
  26652. ' p: $with2.GetField(),',
  26653. ' get: function () {',
  26654. ' return this.p;',
  26655. ' },',
  26656. ' set: function (v) {',
  26657. ' this.p = v;',
  26658. ' }',
  26659. '}, 15);',
  26660. 'var $with3 = $mod.c.GetField();',
  26661. '$mod.THelper.Fly.call({',
  26662. ' get: function () {',
  26663. ' return $with3;',
  26664. ' },',
  26665. ' set: function (v) {',
  26666. ' $with3 = v;',
  26667. ' }',
  26668. '}, 16);',
  26669. '']));
  26670. end;
  26671. procedure TTestModule.TestTypeHelper_Property;
  26672. begin
  26673. StartProgram(false);
  26674. Add([
  26675. '{$modeswitch typehelpers}',
  26676. 'type',
  26677. ' THelper = type helper for word',
  26678. ' function GetSize: longint;',
  26679. ' procedure SetSize(Value: longint);',
  26680. ' property Size: longint read GetSize write SetSize;',
  26681. ' end;',
  26682. 'function THelper.GetSize: longint;',
  26683. 'begin',
  26684. ' Result:=Size+1;',
  26685. ' Size:=2;',
  26686. ' Result:=Self.Size+3;',
  26687. ' Self.Size:=4;',
  26688. ' with Self do begin',
  26689. ' Result:=Size+5;',
  26690. ' Size:=6;',
  26691. ' end;',
  26692. 'end;',
  26693. 'procedure THelper.SetSize(Value: longint);',
  26694. 'begin',
  26695. 'end;',
  26696. 'var w: word;',
  26697. 'begin',
  26698. ' w:=w.Size+7;',
  26699. ' w.Size:=w+8;',
  26700. ' with w do begin',
  26701. ' w:=Size+9;',
  26702. ' Size:=w+10;',
  26703. ' end;',
  26704. '']);
  26705. ConvertProgram;
  26706. CheckSource('TestTypeHelper_Property',
  26707. LinesToStr([ // statements
  26708. 'rtl.createHelper(this, "THelper", null, function () {',
  26709. ' this.GetSize = function () {',
  26710. ' var Result = 0;',
  26711. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  26712. ' $mod.THelper.SetSize.call(this, 2);',
  26713. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  26714. ' $mod.THelper.SetSize.call(this, 4);',
  26715. ' var $with = this.get();',
  26716. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  26717. ' $mod.THelper.SetSize.call(this, 6);',
  26718. ' return Result;',
  26719. ' };',
  26720. ' this.SetSize = function (Value) {',
  26721. ' };',
  26722. '});',
  26723. 'this.w = 0;',
  26724. '']),
  26725. LinesToStr([ // $mod.$main
  26726. '$mod.w = $mod.THelper.GetSize.call({',
  26727. ' p: $mod,',
  26728. ' get: function () {',
  26729. ' return this.p.w;',
  26730. ' },',
  26731. ' set: function (v) {',
  26732. ' this.p.w = v;',
  26733. ' }',
  26734. '}) + 7;',
  26735. '$mod.THelper.SetSize.call({',
  26736. ' p: $mod,',
  26737. ' get: function () {',
  26738. ' return this.p.w;',
  26739. ' },',
  26740. ' set: function (v) {',
  26741. ' this.p.w = v;',
  26742. ' }',
  26743. '}, $mod.w + 8);',
  26744. 'var $with = $mod.w;',
  26745. '$mod.w = $mod.THelper.GetSize.call({',
  26746. ' get: function () {',
  26747. ' return $with;',
  26748. ' },',
  26749. ' set: function (v) {',
  26750. ' $with = v;',
  26751. ' }',
  26752. '}) + 9;',
  26753. '$mod.THelper.SetSize.call({',
  26754. ' get: function () {',
  26755. ' return $with;',
  26756. ' },',
  26757. ' set: function (v) {',
  26758. ' $with = v;',
  26759. ' }',
  26760. '}, $mod.w + 10);',
  26761. '']));
  26762. end;
  26763. procedure TTestModule.TestTypeHelper_Property_Array;
  26764. begin
  26765. StartProgram(false);
  26766. Add([
  26767. '{$modeswitch typehelpers}',
  26768. 'type',
  26769. ' THelper = type helper for word',
  26770. ' function GetItems(Index: byte): boolean;',
  26771. ' procedure SetItems(Index: byte; Value: boolean);',
  26772. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  26773. ' end;',
  26774. 'function THelper.GetItems(Index: byte): boolean;',
  26775. 'begin',
  26776. ' Result:=Items[1];',
  26777. ' Items[2]:=false;',
  26778. ' Result:=Self.Items[3];',
  26779. ' Self.Items[4]:=true;',
  26780. ' with Self do begin',
  26781. ' Result:=Items[5];',
  26782. ' Items[6]:=false;',
  26783. ' end;',
  26784. 'end;',
  26785. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26786. 'begin',
  26787. 'end;',
  26788. 'var',
  26789. ' w: word;',
  26790. ' b: boolean;',
  26791. 'begin',
  26792. ' b:=w.Items[1];',
  26793. ' w.Items[2]:=b;',
  26794. ' with w do begin',
  26795. ' b:=Items[3];',
  26796. ' Items[4]:=b;',
  26797. ' end;',
  26798. '']);
  26799. ConvertProgram;
  26800. CheckSource('TestTypeHelper_Property_Array',
  26801. LinesToStr([ // statements
  26802. 'rtl.createHelper(this, "THelper", null, function () {',
  26803. ' this.GetItems = function (Index) {',
  26804. ' var Result = false;',
  26805. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26806. ' $mod.THelper.SetItems.call(this, 2, false);',
  26807. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26808. ' $mod.THelper.SetItems.call(this, 4, true);',
  26809. ' var $with = this.get();',
  26810. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26811. ' $mod.THelper.SetItems.call(this, 6, false);',
  26812. ' return Result;',
  26813. ' };',
  26814. ' this.SetItems = function (Index, Value) {',
  26815. ' };',
  26816. '});',
  26817. 'this.w = 0;',
  26818. 'this.b = false;',
  26819. '']),
  26820. LinesToStr([ // $mod.$main
  26821. '$mod.b = $mod.THelper.GetItems.call({',
  26822. ' p: $mod,',
  26823. ' get: function () {',
  26824. ' return this.p.w;',
  26825. ' },',
  26826. ' set: function (v) {',
  26827. ' this.p.w = v;',
  26828. ' }',
  26829. '}, 1);',
  26830. '$mod.THelper.SetItems.call({',
  26831. ' p: $mod,',
  26832. ' get: function () {',
  26833. ' return this.p.w;',
  26834. ' },',
  26835. ' set: function (v) {',
  26836. ' this.p.w = v;',
  26837. ' }',
  26838. '}, 2, $mod.b);',
  26839. 'var $with = $mod.w;',
  26840. '$mod.b = $mod.THelper.GetItems.call({',
  26841. ' get: function () {',
  26842. ' return $with;',
  26843. ' },',
  26844. ' set: function (v) {',
  26845. ' $with = v;',
  26846. ' }',
  26847. '}, 3);',
  26848. '$mod.THelper.SetItems.call({',
  26849. ' get: function () {',
  26850. ' return $with;',
  26851. ' },',
  26852. ' set: function (v) {',
  26853. ' $with = v;',
  26854. ' }',
  26855. '}, 4, $mod.b);',
  26856. '']));
  26857. end;
  26858. procedure TTestModule.TestTypeHelper_ClassProperty;
  26859. begin
  26860. StartProgram(false);
  26861. Add([
  26862. '{$modeswitch typehelpers}',
  26863. 'type',
  26864. ' THelper = type helper for word',
  26865. ' class function GetSize: longint; static;',
  26866. ' class procedure SetSize(Value: longint); static;',
  26867. ' class property Size: longint read GetSize write SetSize;',
  26868. ' end;',
  26869. 'class function THelper.GetSize: longint;',
  26870. 'begin',
  26871. ' Result:=Size+1;',
  26872. ' Size:=2;',
  26873. 'end;',
  26874. 'class procedure THelper.SetSize(Value: longint);',
  26875. 'begin',
  26876. 'end;',
  26877. 'begin',
  26878. '']);
  26879. ConvertProgram;
  26880. CheckSource('TestTypeHelper_ClassProperty',
  26881. LinesToStr([ // statements
  26882. 'rtl.createHelper(this, "THelper", null, function () {',
  26883. ' this.GetSize = function () {',
  26884. ' var Result = 0;',
  26885. ' Result = $mod.THelper.GetSize() + 1;',
  26886. ' $mod.THelper.SetSize(2);',
  26887. ' return Result;',
  26888. ' };',
  26889. ' this.SetSize = function (Value) {',
  26890. ' };',
  26891. '});',
  26892. '']),
  26893. LinesToStr([ // $mod.$main
  26894. '']));
  26895. end;
  26896. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26897. begin
  26898. StartProgram(false);
  26899. Add([
  26900. '{$modeswitch typehelpers}',
  26901. 'type',
  26902. ' THelper = type helper for word',
  26903. ' class function GetItems(Index: byte): boolean; static;',
  26904. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26905. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26906. ' end;',
  26907. 'class function THelper.GetItems(Index: byte): boolean;',
  26908. 'begin',
  26909. ' Result:=Items[1];',
  26910. ' Items[2]:=false;',
  26911. 'end;',
  26912. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26913. 'begin',
  26914. 'end;',
  26915. 'var',
  26916. ' w: word;',
  26917. ' b: boolean;',
  26918. 'begin',
  26919. ' b:=w.Items[1];',
  26920. ' w.Items[2]:=b;',
  26921. ' with w do begin',
  26922. ' b:=Items[3];',
  26923. ' Items[4]:=b;',
  26924. ' end;',
  26925. '']);
  26926. ConvertProgram;
  26927. CheckSource('TestTypeHelper_ClassProperty_Array',
  26928. LinesToStr([ // statements
  26929. 'rtl.createHelper(this, "THelper", null, function () {',
  26930. ' this.GetItems = function (Index) {',
  26931. ' var Result = false;',
  26932. ' Result = $mod.THelper.GetItems(1);',
  26933. ' $mod.THelper.SetItems(2, false);',
  26934. ' return Result;',
  26935. ' };',
  26936. ' this.SetItems = function (Index, Value) {',
  26937. ' };',
  26938. '});',
  26939. 'this.w = 0;',
  26940. 'this.b = false;',
  26941. '']),
  26942. LinesToStr([ // $mod.$main
  26943. '$mod.b = $mod.THelper.GetItems(1);',
  26944. '$mod.THelper.SetItems(2, $mod.b);',
  26945. 'var $with = $mod.w;',
  26946. '$mod.b = $mod.THelper.GetItems(3);',
  26947. '$mod.THelper.SetItems(4, $mod.b);',
  26948. '']));
  26949. end;
  26950. procedure TTestModule.TestTypeHelper_ClassMethod;
  26951. begin
  26952. StartProgram(false);
  26953. Add([
  26954. '{$modeswitch typehelpers}',
  26955. 'type',
  26956. ' THelper = type helper for word',
  26957. ' class procedure DoStatic; static;',
  26958. ' end;',
  26959. 'class procedure THelper.DoStatic;',
  26960. 'begin',
  26961. ' DoStatic;',
  26962. ' DoStatic();',
  26963. 'end;',
  26964. 'var w: word;',
  26965. 'begin',
  26966. ' w.DoStatic;',
  26967. ' w.DoStatic();',
  26968. '']);
  26969. ConvertProgram;
  26970. CheckSource('TestTypeHelper_ClassMethod',
  26971. LinesToStr([ // statements
  26972. 'rtl.createHelper(this, "THelper", null, function () {',
  26973. ' this.DoStatic = function () {',
  26974. ' $mod.THelper.DoStatic();',
  26975. ' $mod.THelper.DoStatic();',
  26976. ' };',
  26977. '});',
  26978. 'this.w = 0;',
  26979. '']),
  26980. LinesToStr([ // $mod.$main
  26981. '$mod.THelper.DoStatic();',
  26982. '$mod.THelper.DoStatic();',
  26983. '']));
  26984. end;
  26985. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26986. begin
  26987. StartProgram(false);
  26988. Add([
  26989. '{$modeswitch typehelpers}',
  26990. 'type',
  26991. ' THelper = type helper for word',
  26992. ' procedure Run; external name ''Run'';',
  26993. ' end;',
  26994. 'var w: word;',
  26995. 'begin',
  26996. ' w.Run;',
  26997. '']);
  26998. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26999. ConvertProgram;
  27000. end;
  27001. procedure TTestModule.TestTypeHelper_Constructor;
  27002. begin
  27003. StartProgram(false);
  27004. Add([
  27005. '{$modeswitch typehelpers}',
  27006. 'type',
  27007. ' THelper = type helper for word',
  27008. ' constructor Init(e: longint);',
  27009. ' end;',
  27010. 'constructor THelper.Init(e: longint);',
  27011. 'begin',
  27012. ' Self:=e;',
  27013. ' Init(e+1);',
  27014. 'end;',
  27015. 'var w: word;',
  27016. 'begin',
  27017. ' w:=word.Init(2);',
  27018. ' w:=w.Init(3);',
  27019. ' with word do w:=Init(4);',
  27020. ' with w do w:=Init(5);',
  27021. '']);
  27022. ConvertProgram;
  27023. CheckSource('TestTypeHelper_Constructor',
  27024. LinesToStr([ // statements
  27025. 'rtl.createHelper(this, "THelper", null, function () {',
  27026. ' this.Init = function (e) {',
  27027. ' this.set(e);',
  27028. ' $mod.THelper.Init.call(this, e + 1);',
  27029. ' return this.get();',
  27030. ' };',
  27031. ' this.$new = function (fn, args) {',
  27032. ' return this[fn].apply({',
  27033. ' p: 0,',
  27034. ' get: function () {',
  27035. ' return this.p;',
  27036. ' },',
  27037. ' set: function (v) {',
  27038. ' this.p = v;',
  27039. ' }',
  27040. ' }, args);',
  27041. ' };',
  27042. '});',
  27043. 'this.w = 0;',
  27044. '']),
  27045. LinesToStr([ // $mod.$main
  27046. '$mod.w = $mod.THelper.$new("Init", [2]);',
  27047. '$mod.w = $mod.THelper.Init.call({',
  27048. ' p: $mod,',
  27049. ' get: function () {',
  27050. ' return this.p.w;',
  27051. ' },',
  27052. ' set: function (v) {',
  27053. ' this.p.w = v;',
  27054. ' }',
  27055. '}, 3);',
  27056. '$mod.w = $mod.THelper.$new("Init", [4]);',
  27057. 'var $with = $mod.w;',
  27058. '$mod.w = $mod.THelper.Init.call({',
  27059. ' get: function () {',
  27060. ' return $with;',
  27061. ' },',
  27062. ' set: function (v) {',
  27063. ' $with = v;',
  27064. ' }',
  27065. '}, 5);',
  27066. '']));
  27067. end;
  27068. procedure TTestModule.TestTypeHelper_Word;
  27069. begin
  27070. StartProgram(false);
  27071. Add([
  27072. '{$modeswitch typehelpers}',
  27073. 'type',
  27074. ' THelper = type helper for word',
  27075. ' procedure DoIt(e: byte = 123);',
  27076. ' end;',
  27077. 'procedure THelper.DoIt(e: byte);',
  27078. 'begin',
  27079. ' Self:=e;',
  27080. ' Self:=Self+1;',
  27081. ' with Self do Doit;',
  27082. 'end;',
  27083. 'begin',
  27084. ' word(3).DoIt;',
  27085. '']);
  27086. ConvertProgram;
  27087. CheckSource('TestTypeHelper_Word',
  27088. LinesToStr([ // statements
  27089. 'rtl.createHelper(this, "THelper", null, function () {',
  27090. ' this.DoIt = function (e) {',
  27091. ' this.set(e);',
  27092. ' this.set(this.get() + 1);',
  27093. ' var $with = this.get();',
  27094. ' $mod.THelper.DoIt.call(this, 123);',
  27095. ' };',
  27096. '});',
  27097. '']),
  27098. LinesToStr([ // $mod.$main
  27099. '$mod.THelper.DoIt.call({',
  27100. ' get: function () {',
  27101. ' return 3;',
  27102. ' },',
  27103. ' set: function (v) {',
  27104. ' rtl.raiseE("EPropReadOnly");',
  27105. ' }',
  27106. '}, 123);',
  27107. '']));
  27108. end;
  27109. procedure TTestModule.TestTypeHelper_Boolean;
  27110. begin
  27111. StartProgram(false);
  27112. Add([
  27113. '{$modeswitch typehelpers}',
  27114. 'type',
  27115. ' Integer = longint;',
  27116. ' THelper = type helper for boolean',
  27117. ' procedure Run(e: wordbool = true);',
  27118. ' end;',
  27119. 'procedure THelper.Run(e: wordbool);',
  27120. 'begin',
  27121. ' Self:=e;',
  27122. ' Self:=not Self;',
  27123. ' with Self do Run;',
  27124. ' if Integer(Self)=0 then ;',
  27125. 'end;',
  27126. 'begin',
  27127. ' boolean(3).Run;',
  27128. '']);
  27129. ConvertProgram;
  27130. CheckSource('TestTypeHelper_Boolean',
  27131. LinesToStr([ // statements
  27132. 'rtl.createHelper(this, "THelper", null, function () {',
  27133. ' this.Run = function (e) {',
  27134. ' this.set(e);',
  27135. ' this.set(!this.get());',
  27136. ' var $with = this.get();',
  27137. ' $mod.THelper.Run.call(this, true);',
  27138. ' if ((this.get() ? 1 : 0) === 0) ;',
  27139. ' };',
  27140. '});',
  27141. '']),
  27142. LinesToStr([ // $mod.$main
  27143. '$mod.THelper.Run.call({',
  27144. ' a: 3 != 0,',
  27145. ' get: function () {',
  27146. ' return this.a;',
  27147. ' },',
  27148. ' set: function (v) {',
  27149. ' rtl.raiseE("EPropReadOnly");',
  27150. ' }',
  27151. '}, true);',
  27152. '']));
  27153. end;
  27154. procedure TTestModule.TestTypeHelper_WordBool;
  27155. begin
  27156. StartProgram(false);
  27157. Add([
  27158. '{$modeswitch typehelpers}',
  27159. 'type',
  27160. ' Integer = longint;',
  27161. ' THelper = type helper for WordBool',
  27162. ' procedure Run(e: wordbool = true);',
  27163. ' end;',
  27164. 'procedure THelper.Run(e: wordbool);',
  27165. 'var i: integer;',
  27166. 'begin',
  27167. ' i:=Integer(Self);',
  27168. 'end;',
  27169. 'var w: wordbool;',
  27170. 'begin',
  27171. ' w.Run;',
  27172. ' wordbool(3).Run;',
  27173. '']);
  27174. ConvertProgram;
  27175. CheckSource('TestTypeHelper_WordBool',
  27176. LinesToStr([ // statements
  27177. 'rtl.createHelper(this, "THelper", null, function () {',
  27178. ' this.Run = function (e) {',
  27179. ' var i = 0;',
  27180. ' i = (this.get() ? 1 : 0);',
  27181. ' };',
  27182. '});',
  27183. 'this.w = false;',
  27184. '']),
  27185. LinesToStr([ // $mod.$main
  27186. '$mod.THelper.Run.call({',
  27187. ' p: $mod,',
  27188. ' get: function () {',
  27189. ' return this.p.w;',
  27190. ' },',
  27191. ' set: function (v) {',
  27192. ' this.p.w = v;',
  27193. ' }',
  27194. '}, true);',
  27195. '$mod.THelper.Run.call({',
  27196. ' a: 3 != 0,',
  27197. ' get: function () {',
  27198. ' return this.a;',
  27199. ' },',
  27200. ' set: function (v) {',
  27201. ' rtl.raiseE("EPropReadOnly");',
  27202. ' }',
  27203. '}, true);',
  27204. '']));
  27205. end;
  27206. procedure TTestModule.TestTypeHelper_Double;
  27207. begin
  27208. StartProgram(false);
  27209. Add([
  27210. '{$modeswitch typehelpers}',
  27211. 'type',
  27212. ' Float = type double;',
  27213. ' THelper = type helper for Float',
  27214. ' const NPI = 3.141592;',
  27215. ' function ToStr: String;',
  27216. ' end;',
  27217. 'function THelper.ToStr: String;',
  27218. 'begin',
  27219. 'end;',
  27220. 'procedure DoIt(s: string);',
  27221. 'begin',
  27222. 'end;',
  27223. 'var f: Float;',
  27224. 'begin',
  27225. ' DoIt(f.toStr);',
  27226. ' DoIt(f.toStr());',
  27227. ' (f*f).toStr;',
  27228. ' DoIt((f*f).toStr);',
  27229. '']);
  27230. ConvertProgram;
  27231. CheckSource('TestTypeHelper_Double',
  27232. LinesToStr([ // statements
  27233. 'rtl.createHelper(this, "THelper", null, function () {',
  27234. ' this.NPI = 3.141592;',
  27235. ' this.ToStr = function () {',
  27236. ' var Result = "";',
  27237. ' return Result;',
  27238. ' };',
  27239. '});',
  27240. 'this.DoIt = function (s) {',
  27241. '};',
  27242. 'this.f = 0.0;',
  27243. '']),
  27244. LinesToStr([ // $mod.$main
  27245. '$mod.DoIt($mod.THelper.ToStr.call({',
  27246. ' p: $mod,',
  27247. ' get: function () {',
  27248. ' return this.p.f;',
  27249. ' },',
  27250. ' set: function (v) {',
  27251. ' this.p.f = v;',
  27252. ' }',
  27253. '}));',
  27254. '$mod.DoIt($mod.THelper.ToStr.call({',
  27255. ' p: $mod,',
  27256. ' get: function () {',
  27257. ' return this.p.f;',
  27258. ' },',
  27259. ' set: function (v) {',
  27260. ' this.p.f = v;',
  27261. ' }',
  27262. '}));',
  27263. '$mod.THelper.ToStr.call({',
  27264. ' a: $mod.f * $mod.f,',
  27265. ' get: function () {',
  27266. ' return this.a;',
  27267. ' },',
  27268. ' set: function (v) {',
  27269. ' rtl.raiseE("EPropReadOnly");',
  27270. ' }',
  27271. '});',
  27272. '$mod.DoIt($mod.THelper.ToStr.call({',
  27273. ' a: $mod.f * $mod.f,',
  27274. ' get: function () {',
  27275. ' return this.a;',
  27276. ' },',
  27277. ' set: function (v) {',
  27278. ' rtl.raiseE("EPropReadOnly");',
  27279. ' }',
  27280. '}));',
  27281. '']));
  27282. end;
  27283. procedure TTestModule.TestTypeHelper_NativeInt;
  27284. begin
  27285. StartProgram(false);
  27286. Add([
  27287. '{$modeswitch typehelpers}',
  27288. 'type',
  27289. ' MaxInt = type nativeint;',
  27290. ' THelperI = type helper for MaxInt',
  27291. ' function ToStr: String;',
  27292. ' end;',
  27293. ' MaxUInt = type nativeuint;',
  27294. ' THelperU = type helper for MaxUInt',
  27295. ' function ToStr: String;',
  27296. ' end;',
  27297. 'function THelperI.ToStr: String;',
  27298. 'begin',
  27299. ' Result:=str(Self);',
  27300. 'end;',
  27301. 'function THelperU.ToStr: String;',
  27302. 'begin',
  27303. ' Result:=str(Self);',
  27304. 'end;',
  27305. 'procedure DoIt(s: string);',
  27306. 'begin',
  27307. 'end;',
  27308. 'var i: MaxInt;',
  27309. 'begin',
  27310. ' DoIt(i.toStr);',
  27311. ' DoIt(i.toStr());',
  27312. ' (i*i).toStr;',
  27313. ' DoIt((i*i).toStr);',
  27314. '']);
  27315. ConvertProgram;
  27316. CheckSource('TestTypeHelper_NativeInt',
  27317. LinesToStr([ // statements
  27318. 'rtl.createHelper(this, "THelperI", null, function () {',
  27319. ' this.ToStr = function () {',
  27320. ' var Result = "";',
  27321. ' Result = "" + this.get();',
  27322. ' return Result;',
  27323. ' };',
  27324. '});',
  27325. 'rtl.createHelper(this, "THelperU", null, function () {',
  27326. ' this.ToStr = function () {',
  27327. ' var Result = "";',
  27328. ' Result = "" + this.get();',
  27329. ' return Result;',
  27330. ' };',
  27331. '});',
  27332. 'this.DoIt = function (s) {',
  27333. '};',
  27334. 'this.i = 0;',
  27335. '']),
  27336. LinesToStr([ // $mod.$main
  27337. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27338. ' p: $mod,',
  27339. ' get: function () {',
  27340. ' return this.p.i;',
  27341. ' },',
  27342. ' set: function (v) {',
  27343. ' this.p.i = v;',
  27344. ' }',
  27345. '}));',
  27346. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27347. ' p: $mod,',
  27348. ' get: function () {',
  27349. ' return this.p.i;',
  27350. ' },',
  27351. ' set: function (v) {',
  27352. ' this.p.i = v;',
  27353. ' }',
  27354. '}));',
  27355. '$mod.THelperI.ToStr.call({',
  27356. ' a: $mod.i * $mod.i,',
  27357. ' get: function () {',
  27358. ' return this.a;',
  27359. ' },',
  27360. ' set: function (v) {',
  27361. ' rtl.raiseE("EPropReadOnly");',
  27362. ' }',
  27363. '});',
  27364. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27365. ' a: $mod.i * $mod.i,',
  27366. ' get: function () {',
  27367. ' return this.a;',
  27368. ' },',
  27369. ' set: function (v) {',
  27370. ' rtl.raiseE("EPropReadOnly");',
  27371. ' }',
  27372. '}));',
  27373. '']));
  27374. end;
  27375. procedure TTestModule.TestTypeHelper_StringChar;
  27376. begin
  27377. StartProgram(false);
  27378. Add([
  27379. '{$modeswitch typehelpers}',
  27380. 'type',
  27381. ' TStringHelper = type helper for string',
  27382. ' procedure DoIt(e: byte = 123);',
  27383. ' end;',
  27384. ' TCharHelper = type helper for char',
  27385. ' procedure Fly;',
  27386. ' end;',
  27387. 'procedure TStringHelper.DoIt(e: byte);',
  27388. 'begin',
  27389. ' Self[1]:=''c'';',
  27390. ' Self[2]:=Self[3];',
  27391. 'end;',
  27392. 'procedure TCharHelper.Fly;',
  27393. 'begin',
  27394. ' Self:=''c'';',
  27395. 'end;',
  27396. 'begin',
  27397. ' ''abc''.DoIt;',
  27398. ' ''xyz''.DoIt();',
  27399. ' ''c''.Fly();',
  27400. '']);
  27401. ConvertProgram;
  27402. CheckSource('TestTypeHelper_StringChar',
  27403. LinesToStr([ // statements
  27404. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  27405. ' this.DoIt = function (e) {',
  27406. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  27407. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  27408. ' };',
  27409. '});',
  27410. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  27411. ' this.Fly = function () {',
  27412. ' this.set("c");',
  27413. ' };',
  27414. '});',
  27415. '']),
  27416. LinesToStr([ // $mod.$main
  27417. '$mod.TStringHelper.DoIt.call({',
  27418. ' get: function () {',
  27419. ' return "abc";',
  27420. ' },',
  27421. ' set: function (v) {',
  27422. ' rtl.raiseE("EPropReadOnly");',
  27423. ' }',
  27424. '}, 123);',
  27425. '$mod.TStringHelper.DoIt.call({',
  27426. ' get: function () {',
  27427. ' return "xyz";',
  27428. ' },',
  27429. ' set: function (v) {',
  27430. ' rtl.raiseE("EPropReadOnly");',
  27431. ' }',
  27432. '}, 123);',
  27433. '$mod.TCharHelper.Fly.call({',
  27434. ' get: function () {',
  27435. ' return "c";',
  27436. ' },',
  27437. ' set: function (v) {',
  27438. ' rtl.raiseE("EPropReadOnly");',
  27439. ' }',
  27440. '});',
  27441. '']));
  27442. end;
  27443. procedure TTestModule.TestTypeHelper_JSValue;
  27444. begin
  27445. StartProgram(false);
  27446. Add([
  27447. '{$modeswitch typehelpers}',
  27448. 'type',
  27449. ' TExtValue = type jsvalue;',
  27450. ' THelper = type helper for TExtValue',
  27451. ' function ToStr: String;',
  27452. ' end;',
  27453. 'function THelper.ToStr: String;',
  27454. 'begin',
  27455. 'end;',
  27456. 'var',
  27457. ' s: string;',
  27458. ' v: TExtValue;',
  27459. 'begin',
  27460. ' s:=v.toStr;',
  27461. ' s:=v.toStr();',
  27462. ' TExtValue(s).toStr;',
  27463. '']);
  27464. ConvertProgram;
  27465. CheckSource('TestTypeHelper_JSValue',
  27466. LinesToStr([ // statements
  27467. 'rtl.createHelper(this, "THelper", null, function () {',
  27468. ' this.ToStr = function () {',
  27469. ' var Result = "";',
  27470. ' return Result;',
  27471. ' };',
  27472. '});',
  27473. 'this.s = "";',
  27474. 'this.v = undefined;',
  27475. '']),
  27476. LinesToStr([ // $mod.$main
  27477. '$mod.s = $mod.THelper.ToStr.call({',
  27478. ' p: $mod,',
  27479. ' get: function () {',
  27480. ' return this.p.v;',
  27481. ' },',
  27482. ' set: function (v) {',
  27483. ' this.p.v = v;',
  27484. ' }',
  27485. '});',
  27486. '$mod.s = $mod.THelper.ToStr.call({',
  27487. ' p: $mod,',
  27488. ' get: function () {',
  27489. ' return this.p.v;',
  27490. ' },',
  27491. ' set: function (v) {',
  27492. ' this.p.v = v;',
  27493. ' }',
  27494. '});',
  27495. '$mod.THelper.ToStr.call({',
  27496. ' p: $mod,',
  27497. ' get: function () {',
  27498. ' return this.p.s;',
  27499. ' },',
  27500. ' set: function (v) {',
  27501. ' rtl.raiseE("EPropReadOnly");',
  27502. ' }',
  27503. '});',
  27504. '']));
  27505. end;
  27506. procedure TTestModule.TestTypeHelper_Array;
  27507. begin
  27508. StartProgram(false);
  27509. Add([
  27510. '{$modeswitch typehelpers}',
  27511. 'type',
  27512. ' TArrOfBool = array of boolean;',
  27513. ' TArrOfJS = array of jsvalue;',
  27514. ' THelper = type helper for TArrOfBool',
  27515. ' procedure DoIt(e: byte = 123);',
  27516. ' end;',
  27517. 'procedure THelper.DoIt(e: byte);',
  27518. 'begin',
  27519. ' Self[1]:=true;',
  27520. ' Self[2]:=not Self[3];',
  27521. ' SetLength(Self,4);',
  27522. ' Self:=Concat(Self,[true]);',
  27523. 'end;',
  27524. 'var',
  27525. ' b: TArrOfBool;',
  27526. ' j: TArrOfJS;',
  27527. 'begin',
  27528. ' b.DoIt;',
  27529. ' TArrOfBool(j).DoIt();',
  27530. '']);
  27531. ConvertProgram;
  27532. CheckSource('TestTypeHelper_Array',
  27533. LinesToStr([ // statements
  27534. 'rtl.createHelper(this, "THelper", null, function () {',
  27535. ' this.DoIt = function (e) {',
  27536. ' this.get()[1] = true;',
  27537. ' this.get()[2] = !this.get()[3];',
  27538. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  27539. ' this.set(rtl.arrayPushN(this.get(), true));',
  27540. ' };',
  27541. '});',
  27542. 'this.b = [];',
  27543. 'this.j = [];',
  27544. '']),
  27545. LinesToStr([ // $mod.$main
  27546. '$mod.THelper.DoIt.call({',
  27547. ' p: $mod,',
  27548. ' get: function () {',
  27549. ' return this.p.b;',
  27550. ' },',
  27551. ' set: function (v) {',
  27552. ' this.p.b = v;',
  27553. ' }',
  27554. '}, 123);',
  27555. '$mod.THelper.DoIt.call({',
  27556. ' p: $mod,',
  27557. ' get: function () {',
  27558. ' return this.p.j;',
  27559. ' },',
  27560. ' set: function (v) {',
  27561. ' this.p.j = v;',
  27562. ' }',
  27563. '}, 123);',
  27564. '']));
  27565. end;
  27566. procedure TTestModule.TestTypeHelper_EnumType;
  27567. begin
  27568. StartProgram(false);
  27569. Add([
  27570. '{$modeswitch typehelpers}',
  27571. 'type',
  27572. ' TEnum = (red,blue);',
  27573. ' THelper = type helper for TEnum',
  27574. ' procedure DoIt(e: byte = 123);',
  27575. ' class procedure Swing(w: word); static;',
  27576. ' end;',
  27577. 'procedure THelper.DoIt(e: byte);',
  27578. 'begin',
  27579. ' Self:=red;',
  27580. ' Self:=succ(Self);',
  27581. ' with Self do Doit;',
  27582. 'end;',
  27583. 'class procedure THelper.Swing(w: word);',
  27584. 'begin',
  27585. 'end;',
  27586. 'var e: TEnum;',
  27587. 'begin',
  27588. ' e.DoIt;',
  27589. ' red.DoIt;',
  27590. ' TEnum.blue.DoIt;',
  27591. ' TEnum(1).DoIt;',
  27592. ' TEnum.Swing(3);',
  27593. '']);
  27594. ConvertProgram;
  27595. CheckSource('TestTypeHelper_EnumType',
  27596. LinesToStr([ // statements
  27597. 'this.TEnum = {',
  27598. ' "0": "red",',
  27599. ' red: 0,',
  27600. ' "1": "blue",',
  27601. ' blue: 1',
  27602. '};',
  27603. 'rtl.createHelper(this, "THelper", null, function () {',
  27604. ' this.DoIt = function (e) {',
  27605. ' this.set($mod.TEnum.red);',
  27606. ' this.set(this.get() + 1);',
  27607. ' var $with = this.get();',
  27608. ' $mod.THelper.DoIt.call(this, 123);',
  27609. ' };',
  27610. ' this.Swing = function (w) {',
  27611. ' };',
  27612. '});',
  27613. 'this.e = 0;',
  27614. '']),
  27615. LinesToStr([ // $mod.$main
  27616. '$mod.THelper.DoIt.call({',
  27617. ' p: $mod,',
  27618. ' get: function () {',
  27619. ' return this.p.e;',
  27620. ' },',
  27621. ' set: function (v) {',
  27622. ' this.p.e = v;',
  27623. ' }',
  27624. '}, 123);',
  27625. '$mod.THelper.DoIt.call({',
  27626. ' p: $mod.TEnum,',
  27627. ' get: function () {',
  27628. ' return this.p.red;',
  27629. ' },',
  27630. ' set: function (v) {',
  27631. ' rtl.raiseE("EPropReadOnly");',
  27632. ' }',
  27633. '}, 123);',
  27634. '$mod.THelper.DoIt.call({',
  27635. ' p: $mod.TEnum,',
  27636. ' get: function () {',
  27637. ' return this.p.blue;',
  27638. ' },',
  27639. ' set: function (v) {',
  27640. ' rtl.raiseE("EPropReadOnly");',
  27641. ' }',
  27642. '}, 123);',
  27643. '$mod.THelper.DoIt.call({',
  27644. ' get: function () {',
  27645. ' return 1;',
  27646. ' },',
  27647. ' set: function (v) {',
  27648. ' rtl.raiseE("EPropReadOnly");',
  27649. ' }',
  27650. '}, 123);',
  27651. '$mod.THelper.Swing(3);',
  27652. '']));
  27653. end;
  27654. procedure TTestModule.TestTypeHelper_SetType;
  27655. begin
  27656. StartProgram(false);
  27657. Add([
  27658. '{$modeswitch typehelpers}',
  27659. 'type',
  27660. ' TEnum = (red,blue);',
  27661. ' TSetOfEnum = set of TEnum;',
  27662. ' THelper = type helper for TSetOfEnum',
  27663. ' procedure DoIt(e: byte = 123);',
  27664. ' constructor Init(e: TEnum);',
  27665. ' constructor InitEmpty;',
  27666. ' end;',
  27667. 'procedure THelper.DoIt(e: byte);',
  27668. 'begin',
  27669. ' Self:=[];',
  27670. ' Self:=[red];',
  27671. ' Include(Self,blue);',
  27672. 'end;',
  27673. 'constructor THelper.Init(e: TEnum);',
  27674. 'begin',
  27675. ' Self:=[];',
  27676. ' Self:=[e];',
  27677. ' Include(Self,blue);',
  27678. 'end;',
  27679. 'constructor THelper.InitEmpty;',
  27680. 'begin',
  27681. 'end;',
  27682. 'var s: TSetOfEnum;',
  27683. 'begin',
  27684. ' s.DoIt;',
  27685. //' [red].DoIt;',
  27686. //' with s do DoIt;',
  27687. //' with [red,blue] do DoIt;',
  27688. ' s:=TSetOfEnum.Init(blue);',
  27689. ' s:=s.Init(blue);',
  27690. '']);
  27691. ConvertProgram;
  27692. CheckSource('TestTypeHelper_SetType',
  27693. LinesToStr([ // statements
  27694. 'this.TEnum = {',
  27695. ' "0": "red",',
  27696. ' red: 0,',
  27697. ' "1": "blue",',
  27698. ' blue: 1',
  27699. '};',
  27700. 'rtl.createHelper(this, "THelper", null, function () {',
  27701. ' this.DoIt = function (e) {',
  27702. ' this.set({});',
  27703. ' this.set(rtl.createSet($mod.TEnum.red));',
  27704. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27705. ' };',
  27706. ' this.Init = function (e) {',
  27707. ' this.set({});',
  27708. ' this.set(rtl.createSet(e));',
  27709. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27710. ' return this.get();',
  27711. ' };',
  27712. ' this.InitEmpty = function () {',
  27713. ' return this.get();',
  27714. ' };',
  27715. ' this.$new = function (fn, args) {',
  27716. ' return this[fn].apply({',
  27717. ' p: {},',
  27718. ' get: function () {',
  27719. ' return this.p;',
  27720. ' },',
  27721. ' set: function (v) {',
  27722. ' this.p = v;',
  27723. ' }',
  27724. ' }, args);',
  27725. ' };',
  27726. '});',
  27727. 'this.s = {};',
  27728. '']),
  27729. LinesToStr([ // $mod.$main
  27730. '$mod.THelper.DoIt.call({',
  27731. ' p: $mod,',
  27732. ' get: function () {',
  27733. ' return this.p.s;',
  27734. ' },',
  27735. ' set: function (v) {',
  27736. ' this.p.s = v;',
  27737. ' }',
  27738. '}, 123);',
  27739. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  27740. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  27741. ' p: $mod,',
  27742. ' get: function () {',
  27743. ' return this.p.s;',
  27744. ' },',
  27745. ' set: function (v) {',
  27746. ' this.p.s = v;',
  27747. ' }',
  27748. '}, $mod.TEnum.blue));',
  27749. '']));
  27750. end;
  27751. procedure TTestModule.TestTypeHelper_InterfaceType;
  27752. begin
  27753. StartProgram(false);
  27754. Add([
  27755. '{$interfaces com}',
  27756. '{$modeswitch typehelpers}',
  27757. 'type',
  27758. ' IUnknown = interface',
  27759. ' function _AddRef: longint;',
  27760. ' function _Release: longint;',
  27761. ' end;',
  27762. ' TObject = class(IUnknown)',
  27763. ' function _AddRef: longint; virtual; abstract;',
  27764. ' function _Release: longint; virtual; abstract;',
  27765. ' end;',
  27766. ' THelper = type helper for IUnknown',
  27767. ' procedure Fly(e: byte = 123);',
  27768. ' class procedure Run; static;',
  27769. ' end;',
  27770. 'var',
  27771. ' i: IUnknown;',
  27772. ' o: TObject;',
  27773. 'procedure THelper.Fly(e: byte);',
  27774. 'begin',
  27775. ' i:=Self;',
  27776. ' o:=Self as TObject;',
  27777. ' Self:=nil;',
  27778. ' Self:=i;',
  27779. ' Self:=o;',
  27780. ' with Self do begin',
  27781. ' Fly;',
  27782. ' Fly();',
  27783. ' end;',
  27784. 'end;',
  27785. 'class procedure THelper.Run;',
  27786. 'var l: IUnknown;',
  27787. 'begin',
  27788. ' l.Fly;',
  27789. ' l.Fly();',
  27790. 'end;',
  27791. 'begin',
  27792. ' i.Fly;',
  27793. ' i.Fly();',
  27794. ' i.Run;',
  27795. ' i.Run();',
  27796. ' IUnknown.Run;',
  27797. ' IUnknown.Run();',
  27798. '']);
  27799. ConvertProgram;
  27800. CheckSource('TestTypeHelper_InterfaceType',
  27801. LinesToStr([ // statements
  27802. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27803. 'rtl.createClass(this, "TObject", null, function () {',
  27804. ' this.$init = function () {',
  27805. ' };',
  27806. ' this.$final = function () {',
  27807. ' };',
  27808. ' rtl.addIntf(this, $mod.IUnknown);',
  27809. '});',
  27810. 'rtl.createHelper(this, "THelper", null, function () {',
  27811. ' this.Fly = function (e) {',
  27812. ' var $ir = rtl.createIntfRefs();',
  27813. ' try {',
  27814. ' rtl.setIntfP($mod, "i", this.get());',
  27815. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27816. ' this.set(null);',
  27817. ' this.set($mod.i);',
  27818. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27819. ' var $with = this.get();',
  27820. ' $mod.THelper.Fly.call(this, 123);',
  27821. ' $mod.THelper.Fly.call(this, 123);',
  27822. ' } finally {',
  27823. ' $ir.free();',
  27824. ' };',
  27825. ' };',
  27826. ' this.Run = function () {',
  27827. ' var l = null;',
  27828. ' try {',
  27829. ' $mod.THelper.Fly.call({',
  27830. ' get: function () {',
  27831. ' return l;',
  27832. ' },',
  27833. ' set: function (v) {',
  27834. ' l = rtl.setIntfL(l, v);',
  27835. ' }',
  27836. ' }, 123);',
  27837. ' $mod.THelper.Fly.call({',
  27838. ' get: function () {',
  27839. ' return l;',
  27840. ' },',
  27841. ' set: function (v) {',
  27842. ' l = rtl.setIntfL(l, v);',
  27843. ' }',
  27844. ' }, 123);',
  27845. ' } finally {',
  27846. ' rtl._Release(l);',
  27847. ' };',
  27848. ' };',
  27849. '});',
  27850. 'this.i = null;',
  27851. 'this.o = null;',
  27852. '']),
  27853. LinesToStr([ // $mod.$main
  27854. '$mod.THelper.Fly.call({',
  27855. ' p: $mod,',
  27856. ' get: function () {',
  27857. ' return this.p.i;',
  27858. ' },',
  27859. ' set: function (v) {',
  27860. ' rtl.setIntfP(this.p, "i", v);',
  27861. ' }',
  27862. '}, 123);',
  27863. '$mod.THelper.Fly.call({',
  27864. ' p: $mod,',
  27865. ' get: function () {',
  27866. ' return this.p.i;',
  27867. ' },',
  27868. ' set: function (v) {',
  27869. ' rtl.setIntfP(this.p, "i", v);',
  27870. ' }',
  27871. '}, 123);',
  27872. '$mod.THelper.Run();',
  27873. '$mod.THelper.Run();',
  27874. '$mod.THelper.Run();',
  27875. '$mod.THelper.Run();',
  27876. '']));
  27877. end;
  27878. procedure TTestModule.TestTypeHelper_NestedSelf;
  27879. begin
  27880. StartProgram(false);
  27881. Add([
  27882. '{$modeswitch typehelpers}',
  27883. 'type',
  27884. ' THelper = type helper for string',
  27885. ' procedure Run(Value: string);',
  27886. ' end;',
  27887. 'procedure THelper.Run(Value: string);',
  27888. ' function Sub(i: nativeint): boolean;',
  27889. ' begin',
  27890. ' Result:=Self[i+1]=Value[i];',
  27891. ' end;',
  27892. 'begin',
  27893. ' if Self[3]=Value[4] then ;',
  27894. 'end;',
  27895. 'begin',
  27896. '']);
  27897. ConvertProgram;
  27898. CheckSource('TestTypeHelper_NestedSelf',
  27899. LinesToStr([ // statements
  27900. 'rtl.createHelper(this, "THelper", null, function () {',
  27901. ' this.Run = function (Value) {',
  27902. ' var $Self = this;',
  27903. ' function Sub(i) {',
  27904. ' var Result = false;',
  27905. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27906. ' return Result;',
  27907. ' };',
  27908. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27909. ' };',
  27910. '});',
  27911. '']),
  27912. LinesToStr([ // $mod.$main
  27913. '']));
  27914. end;
  27915. procedure TTestModule.TestProcType;
  27916. begin
  27917. StartProgram(false);
  27918. Add([
  27919. 'type',
  27920. ' TProcInt = procedure(vI: longint = 1);',
  27921. 'procedure DoIt(vJ: longint);',
  27922. 'begin end;',
  27923. 'var',
  27924. ' b: boolean;',
  27925. ' vP, vQ: tprocint;',
  27926. 'begin',
  27927. ' vp:=nil;',
  27928. ' vp:=vp;',
  27929. ' vp:=@doit;',
  27930. ' vp;',
  27931. ' vp();',
  27932. ' vp(2);',
  27933. ' b:=vp=nil;',
  27934. ' b:=nil=vp;',
  27935. ' b:=vp=vq;',
  27936. ' b:=vp=@doit;',
  27937. ' b:=@doit=vp;',
  27938. ' b:=vp<>nil;',
  27939. ' b:=nil<>vp;',
  27940. ' b:=vp<>vq;',
  27941. ' b:=vp<>@doit;',
  27942. ' b:=@doit<>vp;',
  27943. ' b:=Assigned(vp);',
  27944. ' if Assigned(vp) then ;']);
  27945. ConvertProgram;
  27946. CheckSource('TestProcType',
  27947. LinesToStr([ // statements
  27948. 'this.DoIt = function(vJ) {',
  27949. '};',
  27950. 'this.b = false;',
  27951. 'this.vP = null;',
  27952. 'this.vQ = null;'
  27953. ]),
  27954. LinesToStr([ // $mod.$main
  27955. '$mod.vP = null;',
  27956. '$mod.vP = $mod.vP;',
  27957. '$mod.vP = $mod.DoIt;',
  27958. '$mod.vP(1);',
  27959. '$mod.vP(1);',
  27960. '$mod.vP(2);',
  27961. '$mod.b = $mod.vP === null;',
  27962. '$mod.b = null === $mod.vP;',
  27963. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27964. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27965. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27966. '$mod.b = $mod.vP !== null;',
  27967. '$mod.b = null !== $mod.vP;',
  27968. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27969. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27970. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27971. '$mod.b = $mod.vP != null;',
  27972. 'if ($mod.vP != null) ;',
  27973. '']));
  27974. end;
  27975. procedure TTestModule.TestProcType_Arg;
  27976. begin
  27977. StartProgram(false);
  27978. Add([
  27979. 'type',
  27980. ' TProcInt = procedure(vI: longint = 1);',
  27981. 'procedure DoIt(vJ: longint); begin end;',
  27982. 'procedure DoSome(vP, vQ: TProcInt);',
  27983. 'var',
  27984. ' b: boolean;',
  27985. 'begin',
  27986. ' vp:=nil;',
  27987. ' vp:=vp;',
  27988. ' vp:=@doit;',
  27989. ' vp;',
  27990. ' vp();',
  27991. ' vp(2);',
  27992. ' b:=vp=nil;',
  27993. ' b:=nil=vp;',
  27994. ' b:=vp=vq;',
  27995. ' b:=vp=@doit;',
  27996. ' b:=@doit=vp;',
  27997. ' b:=vp<>nil;',
  27998. ' b:=nil<>vp;',
  27999. ' b:=vp<>vq;',
  28000. ' b:=vp<>@doit;',
  28001. ' b:=@doit<>vp;',
  28002. ' b:=Assigned(vp);',
  28003. ' if Assigned(vp) then ;',
  28004. 'end;',
  28005. 'begin',
  28006. ' DoSome(@DoIt,nil);']);
  28007. ConvertProgram;
  28008. CheckSource('TestProcType_Arg',
  28009. LinesToStr([ // statements
  28010. 'this.DoIt = function(vJ) {',
  28011. '};',
  28012. 'this.DoSome = function(vP, vQ) {',
  28013. ' var b = false;',
  28014. ' vP = null;',
  28015. ' vP = vP;',
  28016. ' vP = $mod.DoIt;',
  28017. ' vP(1);',
  28018. ' vP(1);',
  28019. ' vP(2);',
  28020. ' b = vP === null;',
  28021. ' b = null === vP;',
  28022. ' b = rtl.eqCallback(vP,vQ);',
  28023. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  28024. ' b = rtl.eqCallback($mod.DoIt, vP);',
  28025. ' b = vP !== null;',
  28026. ' b = null !== vP;',
  28027. ' b = !rtl.eqCallback(vP, vQ);',
  28028. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  28029. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  28030. ' b = vP != null;',
  28031. ' if (vP != null) ;',
  28032. '};',
  28033. '']),
  28034. LinesToStr([ // $mod.$main
  28035. '$mod.DoSome($mod.DoIt,null);',
  28036. '']));
  28037. end;
  28038. procedure TTestModule.TestProcType_FunctionFPC;
  28039. begin
  28040. StartProgram(false);
  28041. Add('type');
  28042. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28043. Add('function DoIt(vI: longint): longint;');
  28044. Add('begin end;');
  28045. Add('var');
  28046. Add(' b: boolean;');
  28047. Add(' vP, vQ: tfuncint;');
  28048. Add('begin');
  28049. Add(' vp:=nil;');
  28050. Add(' vp:=vp;');
  28051. Add(' vp:=@doit;'); // ok in fpc and delphi
  28052. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28053. Add(' vp;'); // ok in fpc and delphi
  28054. Add(' vp();');
  28055. Add(' vp(2);');
  28056. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28057. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28058. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28059. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28060. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28061. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  28062. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  28063. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28064. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28065. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28066. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28067. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28068. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  28069. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  28070. Add(' b:=Assigned(vp);');
  28071. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  28072. Add(' doit(vp());'); // ok in fpc and delphi
  28073. Add(' doit(vp(2));'); // ok in fpc and delphi
  28074. ConvertProgram;
  28075. CheckSource('TestProcType_FunctionFPC',
  28076. LinesToStr([ // statements
  28077. 'this.DoIt = function(vI) {',
  28078. ' var Result = 0;',
  28079. ' return Result;',
  28080. '};',
  28081. 'this.b = false;',
  28082. 'this.vP = null;',
  28083. 'this.vQ = null;'
  28084. ]),
  28085. LinesToStr([ // $mod.$main
  28086. '$mod.vP = null;',
  28087. '$mod.vP = $mod.vP;',
  28088. '$mod.vP = $mod.DoIt;',
  28089. '$mod.vP(1);',
  28090. '$mod.vP(1);',
  28091. '$mod.vP(2);',
  28092. '$mod.b = $mod.vP === null;',
  28093. '$mod.b = null === $mod.vP;',
  28094. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  28095. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  28096. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  28097. '$mod.b = 4 === $mod.vP(1);',
  28098. '$mod.b = $mod.vP !== null;',
  28099. '$mod.b = null !== $mod.vP;',
  28100. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  28101. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  28102. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  28103. '$mod.b = 6 !== $mod.vP(1);',
  28104. '$mod.b = $mod.vP != null;',
  28105. '$mod.DoIt($mod.vP(1));',
  28106. '$mod.DoIt($mod.vP(2));',
  28107. '']));
  28108. end;
  28109. procedure TTestModule.TestProcType_FunctionDelphi;
  28110. begin
  28111. StartProgram(false);
  28112. Add('{$mode Delphi}');
  28113. Add('type');
  28114. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28115. Add('function DoIt(vI: longint): longint;');
  28116. Add('begin end;');
  28117. Add('var');
  28118. Add(' b: boolean;');
  28119. Add(' vP, vQ: tfuncint;');
  28120. Add('begin');
  28121. Add(' vp:=nil;');
  28122. Add(' vp:=vp;');
  28123. Add(' vp:=@doit;'); // ok in fpc and delphi
  28124. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28125. Add(' vp;'); // ok in fpc and delphi
  28126. Add(' vp();');
  28127. Add(' vp(2);');
  28128. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28129. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28130. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28131. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28132. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28133. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  28134. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  28135. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28136. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28137. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28138. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28139. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28140. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  28141. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  28142. Add(' b:=Assigned(vp);');
  28143. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  28144. Add(' doit(vp());'); // ok in fpc and delphi
  28145. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  28146. ConvertProgram;
  28147. CheckSource('TestProcType_FunctionDelphi',
  28148. LinesToStr([ // statements
  28149. 'this.DoIt = function(vI) {',
  28150. ' var Result = 0;',
  28151. ' return Result;',
  28152. '};',
  28153. 'this.b = false;',
  28154. 'this.vP = null;',
  28155. 'this.vQ = null;'
  28156. ]),
  28157. LinesToStr([ // $mod.$main
  28158. '$mod.vP = null;',
  28159. '$mod.vP = $mod.vP;',
  28160. '$mod.vP = $mod.DoIt;',
  28161. '$mod.vP = $mod.DoIt;',
  28162. '$mod.vP(1);',
  28163. '$mod.vP(1);',
  28164. '$mod.vP(2);',
  28165. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  28166. '$mod.b = $mod.vP(1) === 3;',
  28167. '$mod.b = 4 === $mod.vP(1);',
  28168. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  28169. '$mod.b = $mod.vP(1) !== 5;',
  28170. '$mod.b = 6 !== $mod.vP(1);',
  28171. '$mod.b = $mod.vP != null;',
  28172. '$mod.DoIt($mod.vP(1));',
  28173. '$mod.DoIt($mod.vP(1));',
  28174. '$mod.DoIt($mod.vP(2));',
  28175. '']));
  28176. end;
  28177. procedure TTestModule.TestProcType_ProcedureDelphi;
  28178. begin
  28179. StartProgram(false);
  28180. Add('{$mode Delphi}');
  28181. Add('type');
  28182. Add(' TProc = procedure;');
  28183. Add('procedure DoIt;');
  28184. Add('begin end;');
  28185. Add('var');
  28186. Add(' b: boolean;');
  28187. Add(' vP, vQ: tproc;');
  28188. Add('begin');
  28189. Add(' vp:=nil;');
  28190. Add(' vp:=vp;');
  28191. Add(' vp:=vq;');
  28192. 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
  28193. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28194. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  28195. Add(' vp;'); // ok in fpc and delphi
  28196. Add(' vp();');
  28197. // equal
  28198. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28199. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  28200. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28201. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  28202. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  28203. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28204. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28205. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  28206. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28207. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  28208. // unequal
  28209. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28210. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  28211. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28212. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  28213. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28214. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  28215. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28216. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  28217. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28218. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  28219. Add(' b:=Assigned(vp);');
  28220. ConvertProgram;
  28221. CheckSource('TestProcType_ProcedureDelphi',
  28222. LinesToStr([ // statements
  28223. 'this.DoIt = function() {',
  28224. '};',
  28225. 'this.b = false;',
  28226. 'this.vP = null;',
  28227. 'this.vQ = null;'
  28228. ]),
  28229. LinesToStr([ // $mod.$main
  28230. '$mod.vP = null;',
  28231. '$mod.vP = $mod.vP;',
  28232. '$mod.vP = $mod.vQ;',
  28233. '$mod.vP = $mod.DoIt;',
  28234. '$mod.vP = $mod.DoIt;',
  28235. '$mod.vP();',
  28236. '$mod.vP();',
  28237. '$mod.b = $mod.vP === null;',
  28238. '$mod.b = null === $mod.vP;',
  28239. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  28240. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  28241. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  28242. '$mod.b = $mod.vP !== null;',
  28243. '$mod.b = null !== $mod.vP;',
  28244. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  28245. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  28246. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  28247. '$mod.b = $mod.vP != null;',
  28248. '']));
  28249. end;
  28250. procedure TTestModule.TestProcType_AsParam;
  28251. begin
  28252. StartProgram(false);
  28253. Add('type');
  28254. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28255. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  28256. Add('var vJ: tfuncint;');
  28257. Add('begin');
  28258. Add(' vg:=vg;');
  28259. Add(' vj:=vh;');
  28260. Add(' vi:=vi;');
  28261. Add(' doit(vg,vg,vg);');
  28262. Add(' doit(vh,vh,vj);');
  28263. Add(' doit(vi,vi,vi);');
  28264. Add(' doit(vj,vj,vj);');
  28265. Add('end;');
  28266. Add('var i: tfuncint;');
  28267. Add('begin');
  28268. Add(' doit(i,i,i);');
  28269. ConvertProgram;
  28270. CheckSource('TestProcType_AsParam',
  28271. LinesToStr([ // statements
  28272. 'this.DoIt = function (vG,vH,vI) {',
  28273. ' var vJ = null;',
  28274. ' vG = vG;',
  28275. ' vJ = vH;',
  28276. ' vI.set(vI.get());',
  28277. ' $mod.DoIt(vG, vG, {',
  28278. ' get: function () {',
  28279. ' return vG;',
  28280. ' },',
  28281. ' set: function (v) {',
  28282. ' vG = v;',
  28283. ' }',
  28284. ' });',
  28285. ' $mod.DoIt(vH, vH, {',
  28286. ' get: function () {',
  28287. ' return vJ;',
  28288. ' },',
  28289. ' set: function (v) {',
  28290. ' vJ = v;',
  28291. ' }',
  28292. ' });',
  28293. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  28294. ' $mod.DoIt(vJ, vJ, {',
  28295. ' get: function () {',
  28296. ' return vJ;',
  28297. ' },',
  28298. ' set: function (v) {',
  28299. ' vJ = v;',
  28300. ' }',
  28301. ' });',
  28302. '};',
  28303. 'this.i = null;'
  28304. ]),
  28305. LinesToStr([
  28306. '$mod.DoIt($mod.i,$mod.i,{',
  28307. ' p: $mod,',
  28308. ' get: function () {',
  28309. ' return this.p.i;',
  28310. ' },',
  28311. ' set: function (v) {',
  28312. ' this.p.i = v;',
  28313. ' }',
  28314. '});'
  28315. ]));
  28316. end;
  28317. procedure TTestModule.TestProcType_MethodFPC;
  28318. begin
  28319. StartProgram(false);
  28320. Add('type');
  28321. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28322. Add(' TObject = class');
  28323. Add(' function DoIt(vA: longint = 1): longint;');
  28324. Add(' end;');
  28325. Add('function TObject.DoIt(vA: longint = 1): longint;');
  28326. Add('begin');
  28327. Add('end;');
  28328. Add('var');
  28329. Add(' Obj: TObject;');
  28330. Add(' vP: tfuncint;');
  28331. Add(' b: boolean;');
  28332. Add('begin');
  28333. Add(' vp:[email protected];'); // ok in fpc and delphi
  28334. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  28335. Add(' vp;'); // ok in fpc and delphi
  28336. Add(' vp();');
  28337. Add(' vp(2);');
  28338. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  28339. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  28340. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  28341. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  28342. ConvertProgram;
  28343. CheckSource('TestProcType_MethodFPC',
  28344. LinesToStr([ // statements
  28345. 'rtl.createClass(this, "TObject", null, function () {',
  28346. ' this.$init = function () {',
  28347. ' };',
  28348. ' this.$final = function () {',
  28349. ' };',
  28350. ' this.DoIt = function (vA) {',
  28351. ' var Result = 0;',
  28352. ' return Result;',
  28353. ' };',
  28354. '});',
  28355. 'this.Obj = null;',
  28356. 'this.vP = null;',
  28357. 'this.b = false;'
  28358. ]),
  28359. LinesToStr([
  28360. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28361. '$mod.vP(1);',
  28362. '$mod.vP(1);',
  28363. '$mod.vP(2);',
  28364. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  28365. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  28366. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  28367. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  28368. '']));
  28369. end;
  28370. procedure TTestModule.TestProcType_MethodDelphi;
  28371. begin
  28372. StartProgram(false);
  28373. Add([
  28374. '{$mode delphi}',
  28375. 'type',
  28376. ' TFuncInt = function(vA: longint = 1): longint of object;',
  28377. ' TObject = class',
  28378. ' function DoIt(vA: longint = 1): longint;',
  28379. ' end;',
  28380. 'function TObject.DoIt(vA: longint = 1): longint;',
  28381. 'begin',
  28382. 'end;',
  28383. 'var',
  28384. ' Obj: TObject;',
  28385. ' vP: tfuncint;',
  28386. ' b: boolean;',
  28387. 'begin',
  28388. ' vp:[email protected];', // ok in fpc and delphi
  28389. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  28390. ' vp;', // ok in fpc and delphi
  28391. ' vp();',
  28392. ' vp(2);',
  28393. //' b:[email protected];', // ok in fpc, illegal in delphi
  28394. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  28395. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  28396. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  28397. '']);
  28398. ConvertProgram;
  28399. CheckSource('TestProcType_MethodDelphi',
  28400. LinesToStr([ // statements
  28401. 'rtl.createClass(this, "TObject", null, function () {',
  28402. ' this.$init = function () {',
  28403. ' };',
  28404. ' this.$final = function () {',
  28405. ' };',
  28406. ' this.DoIt = function (vA) {',
  28407. ' var Result = 0;',
  28408. ' return Result;',
  28409. ' };',
  28410. '});',
  28411. 'this.Obj = null;',
  28412. 'this.vP = null;',
  28413. 'this.b = false;'
  28414. ]),
  28415. LinesToStr([
  28416. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28417. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28418. '$mod.vP(1);',
  28419. '$mod.vP(1);',
  28420. '$mod.vP(2);',
  28421. '']));
  28422. end;
  28423. procedure TTestModule.TestProcType_PropertyFPC;
  28424. begin
  28425. StartProgram(false);
  28426. Add('type');
  28427. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28428. Add(' TObject = class');
  28429. Add(' FOnFoo: TFuncInt;');
  28430. Add(' function DoIt(vA: longint = 1): longint;');
  28431. Add(' function GetFoo: TFuncInt;');
  28432. Add(' procedure SetFoo(const Value: TFuncInt);');
  28433. Add(' function GetEvents(Index: longint): TFuncInt;');
  28434. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  28435. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28436. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28437. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  28438. Add(' end;');
  28439. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28440. Add('function tobject.getfoo: tfuncint; begin end;');
  28441. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28442. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  28443. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  28444. Add('var');
  28445. Add(' Obj: TObject;');
  28446. Add(' vP: tfuncint;');
  28447. Add(' b: boolean;');
  28448. Add('begin');
  28449. Add(' obj.onfoo:=nil;');
  28450. Add(' obj.onbar:=nil;');
  28451. Add(' obj.events[1]:=nil;');
  28452. Add(' obj.onfoo:=obj.onfoo;');
  28453. Add(' obj.onbar:=obj.onbar;');
  28454. Add(' obj.events[2]:=obj.events[3];');
  28455. Add(' obj.onfoo:[email protected];');
  28456. Add(' obj.onbar:[email protected];');
  28457. Add(' obj.events[4]:[email protected];');
  28458. //Add(' obj.onfoo:=obj.doit;'); // delphi
  28459. //Add(' obj.onbar:=obj.doit;'); // delphi
  28460. //Add(' obj.events[4]:=obj.doit;'); // delphi
  28461. Add(' obj.onfoo;');
  28462. Add(' obj.onbar;');
  28463. //Add(' obj.events[5];'); ToDo in pasresolver
  28464. Add(' obj.onfoo();');
  28465. Add(' obj.onbar();');
  28466. Add(' obj.events[6]();');
  28467. Add(' b:=obj.onfoo=nil;');
  28468. Add(' b:=obj.onbar=nil;');
  28469. Add(' b:=obj.events[7]=nil;');
  28470. Add(' b:=obj.onfoo<>nil;');
  28471. Add(' b:=obj.onbar<>nil;');
  28472. Add(' b:=obj.events[8]<>nil;');
  28473. Add(' b:=obj.onfoo=vp;');
  28474. Add(' b:=obj.onbar=vp;');
  28475. Add(' b:=obj.events[9]=vp;');
  28476. Add(' b:=obj.onfoo=obj.onfoo;');
  28477. Add(' b:=obj.onbar=obj.onfoo;');
  28478. Add(' b:=obj.events[10]=obj.onfoo;');
  28479. Add(' b:=obj.onfoo<>obj.onfoo;');
  28480. Add(' b:=obj.onbar<>obj.onfoo;');
  28481. Add(' b:=obj.events[11]<>obj.onfoo;');
  28482. Add(' b:[email protected];');
  28483. Add(' b:[email protected];');
  28484. Add(' b:=obj.events[12][email protected];');
  28485. Add(' b:=obj.onfoo<>@obj.doit;');
  28486. Add(' b:=obj.onbar<>@obj.doit;');
  28487. Add(' b:=obj.events[12]<>@obj.doit;');
  28488. Add(' b:=Assigned(obj.onfoo);');
  28489. Add(' b:=Assigned(obj.onbar);');
  28490. Add(' b:=Assigned(obj.events[13]);');
  28491. ConvertProgram;
  28492. CheckSource('TestProcType_PropertyFPC',
  28493. LinesToStr([ // statements
  28494. 'rtl.createClass(this, "TObject", null, function () {',
  28495. ' this.$init = function () {',
  28496. ' this.FOnFoo = null;',
  28497. ' };',
  28498. ' this.$final = function () {',
  28499. ' this.FOnFoo = undefined;',
  28500. ' };',
  28501. ' this.DoIt = function (vA) {',
  28502. ' var Result = 0;',
  28503. ' return Result;',
  28504. ' };',
  28505. 'this.GetFoo = function () {',
  28506. ' var Result = null;',
  28507. ' return Result;',
  28508. '};',
  28509. 'this.SetFoo = function (Value) {',
  28510. '};',
  28511. 'this.GetEvents = function (Index) {',
  28512. ' var Result = null;',
  28513. ' return Result;',
  28514. '};',
  28515. 'this.SetEvents = function (Index, Value) {',
  28516. '};',
  28517. '});',
  28518. 'this.Obj = null;',
  28519. 'this.vP = null;',
  28520. 'this.b = false;'
  28521. ]),
  28522. LinesToStr([
  28523. '$mod.Obj.FOnFoo = null;',
  28524. '$mod.Obj.SetFoo(null);',
  28525. '$mod.Obj.SetEvents(1, null);',
  28526. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  28527. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  28528. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  28529. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28530. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28531. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28532. '$mod.Obj.FOnFoo(1);',
  28533. '$mod.Obj.GetFoo();',
  28534. '$mod.Obj.FOnFoo(1);',
  28535. '$mod.Obj.GetFoo()(1);',
  28536. '$mod.Obj.GetEvents(6)(1);',
  28537. '$mod.b = $mod.Obj.FOnFoo === null;',
  28538. '$mod.b = $mod.Obj.GetFoo() === null;',
  28539. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  28540. '$mod.b = $mod.Obj.FOnFoo !== null;',
  28541. '$mod.b = $mod.Obj.GetFoo() !== null;',
  28542. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  28543. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  28544. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  28545. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  28546. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  28547. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  28548. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  28549. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  28550. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  28551. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  28552. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  28553. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  28554. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  28555. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  28556. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  28557. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  28558. '$mod.b = $mod.Obj.FOnFoo != null;',
  28559. '$mod.b = $mod.Obj.GetFoo() != null;',
  28560. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28561. '']));
  28562. end;
  28563. procedure TTestModule.TestProcType_PropertyDelphi;
  28564. begin
  28565. StartProgram(false);
  28566. Add('{$mode delphi}');
  28567. Add('type');
  28568. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28569. Add(' TObject = class');
  28570. Add(' FOnFoo: TFuncInt;');
  28571. Add(' function DoIt(vA: longint = 1): longint;');
  28572. Add(' function GetFoo: TFuncInt;');
  28573. Add(' procedure SetFoo(const Value: TFuncInt);');
  28574. Add(' function GetEvents(Index: longint): TFuncInt;');
  28575. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  28576. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28577. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28578. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  28579. Add(' end;');
  28580. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28581. Add('function tobject.getfoo: tfuncint; begin end;');
  28582. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28583. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  28584. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  28585. Add('var');
  28586. Add(' Obj: TObject;');
  28587. Add(' vP: tfuncint;');
  28588. Add(' b: boolean;');
  28589. Add('begin');
  28590. Add(' obj.onfoo:=nil;');
  28591. Add(' obj.onbar:=nil;');
  28592. Add(' obj.events[1]:=nil;');
  28593. Add(' obj.onfoo:=obj.onfoo;');
  28594. Add(' obj.onbar:=obj.onbar;');
  28595. Add(' obj.events[2]:=obj.events[3];');
  28596. Add(' obj.onfoo:[email protected];');
  28597. Add(' obj.onbar:[email protected];');
  28598. Add(' obj.events[4]:[email protected];');
  28599. Add(' obj.onfoo:=obj.doit;'); // delphi
  28600. Add(' obj.onbar:=obj.doit;'); // delphi
  28601. Add(' obj.events[4]:=obj.doit;'); // delphi
  28602. Add(' obj.onfoo;');
  28603. Add(' obj.onbar;');
  28604. //Add(' obj.events[5];'); ToDo in pasresolver
  28605. Add(' obj.onfoo();');
  28606. Add(' obj.onbar();');
  28607. Add(' obj.events[6]();');
  28608. //Add(' b:=obj.onfoo=nil;'); // fpc
  28609. //Add(' b:=obj.onbar=nil;'); // fpc
  28610. //Add(' b:=obj.events[7]=nil;'); // fpc
  28611. //Add(' b:=obj.onfoo<>nil;'); // fpc
  28612. //Add(' b:=obj.onbar<>nil;'); // fpc
  28613. //Add(' b:=obj.events[8]<>nil;'); // fpc
  28614. Add(' b:=obj.onfoo=vp;');
  28615. Add(' b:=obj.onbar=vp;');
  28616. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  28617. Add(' b:=obj.onfoo=obj.onfoo;');
  28618. Add(' b:=obj.onbar=obj.onfoo;');
  28619. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  28620. Add(' b:=obj.onfoo<>obj.onfoo;');
  28621. Add(' b:=obj.onbar<>obj.onfoo;');
  28622. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  28623. //Add(' b:[email protected];'); // fpc
  28624. //Add(' b:[email protected];'); // fpc
  28625. //Add(' b:=obj.events[12][email protected];'); // fpc
  28626. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  28627. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  28628. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  28629. Add(' b:=Assigned(obj.onfoo);');
  28630. Add(' b:=Assigned(obj.onbar);');
  28631. Add(' b:=Assigned(obj.events[13]);');
  28632. ConvertProgram;
  28633. CheckSource('TestProcType_PropertyDelphi',
  28634. LinesToStr([ // statements
  28635. 'rtl.createClass(this, "TObject", null, function () {',
  28636. ' this.$init = function () {',
  28637. ' this.FOnFoo = null;',
  28638. ' };',
  28639. ' this.$final = function () {',
  28640. ' this.FOnFoo = undefined;',
  28641. ' };',
  28642. ' this.DoIt = function (vA) {',
  28643. ' var Result = 0;',
  28644. ' return Result;',
  28645. ' };',
  28646. 'this.GetFoo = function () {',
  28647. ' var Result = null;',
  28648. ' return Result;',
  28649. '};',
  28650. 'this.SetFoo = function (Value) {',
  28651. '};',
  28652. 'this.GetEvents = function (Index) {',
  28653. ' var Result = null;',
  28654. ' return Result;',
  28655. '};',
  28656. 'this.SetEvents = function (Index, Value) {',
  28657. '};',
  28658. '});',
  28659. 'this.Obj = null;',
  28660. 'this.vP = null;',
  28661. 'this.b = false;'
  28662. ]),
  28663. LinesToStr([
  28664. '$mod.Obj.FOnFoo = null;',
  28665. '$mod.Obj.SetFoo(null);',
  28666. '$mod.Obj.SetEvents(1, null);',
  28667. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  28668. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  28669. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  28670. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28671. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28672. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28673. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28674. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28675. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28676. '$mod.Obj.FOnFoo(1);',
  28677. '$mod.Obj.GetFoo();',
  28678. '$mod.Obj.FOnFoo(1);',
  28679. '$mod.Obj.GetFoo()(1);',
  28680. '$mod.Obj.GetEvents(6)(1);',
  28681. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  28682. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  28683. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  28684. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  28685. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  28686. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  28687. '$mod.b = $mod.Obj.FOnFoo != null;',
  28688. '$mod.b = $mod.Obj.GetFoo() != null;',
  28689. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28690. '']));
  28691. end;
  28692. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  28693. begin
  28694. StartProgram(false);
  28695. Add('type');
  28696. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28697. Add(' TObject = class');
  28698. Add(' FOnFoo: TFuncInt;');
  28699. Add(' function DoIt(vA: longint = 1): longint;');
  28700. Add(' function GetFoo: TFuncInt;');
  28701. Add(' procedure SetFoo(const Value: TFuncInt);');
  28702. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28703. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28704. Add(' end;');
  28705. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28706. Add('function tobject.getfoo: tfuncint; begin end;');
  28707. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28708. Add('var');
  28709. Add(' Obj: TObject;');
  28710. Add(' vP: tfuncint;');
  28711. Add(' b: boolean;');
  28712. Add('begin');
  28713. Add('with obj do begin');
  28714. Add(' fonfoo:=nil;');
  28715. Add(' onfoo:=nil;');
  28716. Add(' onbar:=nil;');
  28717. Add(' fonfoo:=fonfoo;');
  28718. Add(' onfoo:=onfoo;');
  28719. Add(' onbar:=onbar;');
  28720. Add(' fonfoo:=@doit;');
  28721. Add(' onfoo:=@doit;');
  28722. Add(' onbar:=@doit;');
  28723. //Add(' fonfoo:=doit;'); // delphi
  28724. //Add(' onfoo:=doit;'); // delphi
  28725. //Add(' onbar:=doit;'); // delphi
  28726. Add(' fonfoo;');
  28727. Add(' onfoo;');
  28728. Add(' onbar;');
  28729. Add(' fonfoo();');
  28730. Add(' onfoo();');
  28731. Add(' onbar();');
  28732. Add(' b:=fonfoo=nil;');
  28733. Add(' b:=onfoo=nil;');
  28734. Add(' b:=onbar=nil;');
  28735. Add(' b:=fonfoo<>nil;');
  28736. Add(' b:=onfoo<>nil;');
  28737. Add(' b:=onbar<>nil;');
  28738. Add(' b:=fonfoo=vp;');
  28739. Add(' b:=onfoo=vp;');
  28740. Add(' b:=onbar=vp;');
  28741. Add(' b:=fonfoo=fonfoo;');
  28742. Add(' b:=onfoo=onfoo;');
  28743. Add(' b:=onbar=onfoo;');
  28744. Add(' b:=fonfoo<>fonfoo;');
  28745. Add(' b:=onfoo<>onfoo;');
  28746. Add(' b:=onbar<>onfoo;');
  28747. Add(' b:=fonfoo=@doit;');
  28748. Add(' b:=onfoo=@doit;');
  28749. Add(' b:=onbar=@doit;');
  28750. Add(' b:=fonfoo<>@doit;');
  28751. Add(' b:=onfoo<>@doit;');
  28752. Add(' b:=onbar<>@doit;');
  28753. Add(' b:=Assigned(fonfoo);');
  28754. Add(' b:=Assigned(onfoo);');
  28755. Add(' b:=Assigned(onbar);');
  28756. Add('end;');
  28757. ConvertProgram;
  28758. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  28759. LinesToStr([ // statements
  28760. 'rtl.createClass(this, "TObject", null, function () {',
  28761. ' this.$init = function () {',
  28762. ' this.FOnFoo = null;',
  28763. ' };',
  28764. ' this.$final = function () {',
  28765. ' this.FOnFoo = undefined;',
  28766. ' };',
  28767. ' this.DoIt = function (vA) {',
  28768. ' var Result = 0;',
  28769. ' return Result;',
  28770. ' };',
  28771. ' this.GetFoo = function () {',
  28772. ' var Result = null;',
  28773. ' return Result;',
  28774. ' };',
  28775. ' this.SetFoo = function (Value) {',
  28776. ' };',
  28777. '});',
  28778. 'this.Obj = null;',
  28779. 'this.vP = null;',
  28780. 'this.b = false;'
  28781. ]),
  28782. LinesToStr([
  28783. 'var $with = $mod.Obj;',
  28784. '$with.FOnFoo = null;',
  28785. '$with.FOnFoo = null;',
  28786. '$with.SetFoo(null);',
  28787. '$with.FOnFoo = $with.FOnFoo;',
  28788. '$with.FOnFoo = $with.FOnFoo;',
  28789. '$with.SetFoo($with.GetFoo());',
  28790. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28791. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28792. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28793. '$with.FOnFoo(1);',
  28794. '$with.FOnFoo(1);',
  28795. '$with.GetFoo();',
  28796. '$with.FOnFoo(1);',
  28797. '$with.FOnFoo(1);',
  28798. '$with.GetFoo()(1);',
  28799. '$mod.b = $with.FOnFoo === null;',
  28800. '$mod.b = $with.FOnFoo === null;',
  28801. '$mod.b = $with.GetFoo() === null;',
  28802. '$mod.b = $with.FOnFoo !== null;',
  28803. '$mod.b = $with.FOnFoo !== null;',
  28804. '$mod.b = $with.GetFoo() !== null;',
  28805. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28806. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28807. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28808. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28809. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28810. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28811. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28812. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28813. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28814. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28815. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28816. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28817. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28818. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28819. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28820. '$mod.b = $with.FOnFoo != null;',
  28821. '$mod.b = $with.FOnFoo != null;',
  28822. '$mod.b = $with.GetFoo() != null;',
  28823. '']));
  28824. end;
  28825. procedure TTestModule.TestProcType_Nested;
  28826. begin
  28827. StartProgram(false);
  28828. Add([
  28829. 'type',
  28830. ' TProcInt = procedure(vI: longint = 1);',
  28831. 'procedure DoIt(vJ: longint);',
  28832. 'var aProc: TProcInt;',
  28833. ' b: boolean;',
  28834. ' procedure Sub(vK: longint);',
  28835. ' var aSub: TProcInt;',
  28836. ' procedure SubSub(vK: longint);',
  28837. ' var aSubSub: TProcInt;',
  28838. ' begin;',
  28839. ' aProc:=@DoIt;',
  28840. ' aSub:=@DoIt;',
  28841. ' aSubSub:=@DoIt;',
  28842. ' aProc:=@Sub;',
  28843. ' aSub:=@Sub;',
  28844. ' aSubSub:=@Sub;',
  28845. ' aProc:=@SubSub;',
  28846. ' aSub:=@SubSub;',
  28847. ' aSubSub:=@SubSub;',
  28848. ' end;',
  28849. ' begin;',
  28850. ' end;',
  28851. 'begin;',
  28852. ' aProc:=@Sub;',
  28853. ' b:=aProc=@Sub;',
  28854. ' b:=@Sub=aProc;',
  28855. 'end;',
  28856. 'begin',
  28857. '']);
  28858. ConvertProgram;
  28859. CheckSource('TestProcType_Nested',
  28860. LinesToStr([ // statements
  28861. 'this.DoIt = function (vJ) {',
  28862. ' var aProc = null;',
  28863. ' var b = false;',
  28864. ' function Sub(vK) {',
  28865. ' var aSub = null;',
  28866. ' function SubSub(vK) {',
  28867. ' var aSubSub = null;',
  28868. ' aProc = $mod.DoIt;',
  28869. ' aSub = $mod.DoIt;',
  28870. ' aSubSub = $mod.DoIt;',
  28871. ' aProc = Sub;',
  28872. ' aSub = Sub;',
  28873. ' aSubSub = Sub;',
  28874. ' aProc = SubSub;',
  28875. ' aSub = SubSub;',
  28876. ' aSubSub = SubSub;',
  28877. ' };',
  28878. ' };',
  28879. ' aProc = Sub;',
  28880. ' b = rtl.eqCallback(aProc, Sub);',
  28881. ' b = rtl.eqCallback(Sub, aProc);',
  28882. '};',
  28883. '']),
  28884. LinesToStr([ // $mod.$main
  28885. '']));
  28886. end;
  28887. procedure TTestModule.TestProcType_NestedOfObject;
  28888. begin
  28889. StartProgram(false);
  28890. Add([
  28891. 'type',
  28892. ' TProcInt = procedure(vI: longint = 1) of object;',
  28893. ' TObject = class',
  28894. ' procedure DoIt(vJ: longint);',
  28895. ' end;',
  28896. 'procedure TObject.DoIt(vJ: longint);',
  28897. 'var aProc: TProcInt;',
  28898. ' b: boolean;',
  28899. ' procedure Sub(vK: longint);',
  28900. ' var aSub: TProcInt;',
  28901. ' procedure SubSub(vK: longint);',
  28902. ' var aSubSub: TProcInt;',
  28903. ' begin;',
  28904. ' aProc:=@DoIt;',
  28905. ' aSub:=@DoIt;',
  28906. ' aSubSub:=@DoIt;',
  28907. ' aProc:=@Sub;',
  28908. ' aSub:=@Sub;',
  28909. ' aSubSub:=@Sub;',
  28910. ' aProc:=@SubSub;',
  28911. ' aSub:=@SubSub;',
  28912. ' aSubSub:=@SubSub;',
  28913. ' end;',
  28914. ' begin;',
  28915. ' end;',
  28916. 'begin;',
  28917. ' aProc:=@Sub;',
  28918. ' b:=aProc=@Sub;',
  28919. ' b:=@Sub=aProc;',
  28920. 'end;',
  28921. 'begin',
  28922. '']);
  28923. ConvertProgram;
  28924. CheckSource('TestProcType_Nested',
  28925. LinesToStr([ // statements
  28926. 'rtl.createClass(this, "TObject", null, function () {',
  28927. ' this.$init = function () {',
  28928. ' };',
  28929. ' this.$final = function () {',
  28930. ' };',
  28931. ' this.DoIt = function (vJ) {',
  28932. ' var $Self = this;',
  28933. ' var aProc = null;',
  28934. ' var b = false;',
  28935. ' function Sub(vK) {',
  28936. ' var aSub = null;',
  28937. ' function SubSub(vK) {',
  28938. ' var aSubSub = null;',
  28939. ' aProc = rtl.createCallback($Self, "DoIt");',
  28940. ' aSub = rtl.createCallback($Self, "DoIt");',
  28941. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28942. ' aProc = Sub;',
  28943. ' aSub = Sub;',
  28944. ' aSubSub = Sub;',
  28945. ' aProc = SubSub;',
  28946. ' aSub = SubSub;',
  28947. ' aSubSub = SubSub;',
  28948. ' };',
  28949. ' };',
  28950. ' aProc = Sub;',
  28951. ' b = rtl.eqCallback(aProc, Sub);',
  28952. ' b = rtl.eqCallback(Sub, aProc);',
  28953. ' };',
  28954. '});',
  28955. '']),
  28956. LinesToStr([ // $mod.$main
  28957. '']));
  28958. end;
  28959. procedure TTestModule.TestProcType_ReferenceToProc;
  28960. begin
  28961. StartProgram(false);
  28962. Add([
  28963. 'type',
  28964. ' TProcRef = reference to procedure(i: longint = 0);',
  28965. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28966. 'var',
  28967. ' p: TProcRef;',
  28968. ' f: TFuncRef;',
  28969. 'procedure DoIt(i: longint);',
  28970. 'begin',
  28971. 'end;',
  28972. 'function GetIt(i: longint): longint;',
  28973. 'begin',
  28974. ' p:=@DoIt;',
  28975. ' f:=@GetIt;',
  28976. ' f;',
  28977. ' f();',
  28978. ' f(1);',
  28979. 'end;',
  28980. 'begin',
  28981. ' p:=@DoIt;',
  28982. ' f:=@GetIt;',
  28983. ' f;',
  28984. ' f();',
  28985. ' f(1);',
  28986. ' p:=TProcRef(f);',
  28987. '']);
  28988. ConvertProgram;
  28989. CheckSource('TestProcType_ReferenceToProc',
  28990. LinesToStr([ // statements
  28991. 'this.p = null;',
  28992. 'this.f = null;',
  28993. 'this.DoIt = function (i) {',
  28994. '};',
  28995. 'this.GetIt = function (i) {',
  28996. ' var Result = 0;',
  28997. ' $mod.p = $mod.DoIt;',
  28998. ' $mod.f = $mod.GetIt;',
  28999. ' $mod.f(0);',
  29000. ' $mod.f(0);',
  29001. ' $mod.f(1);',
  29002. ' return Result;',
  29003. '};',
  29004. '']),
  29005. LinesToStr([ // $mod.$main
  29006. '$mod.p = $mod.DoIt;',
  29007. '$mod.f = $mod.GetIt;',
  29008. '$mod.f(0);',
  29009. '$mod.f(0);',
  29010. '$mod.f(1);',
  29011. '$mod.p = $mod.f;',
  29012. '']));
  29013. end;
  29014. procedure TTestModule.TestProcType_ReferenceToMethod;
  29015. begin
  29016. StartProgram(false);
  29017. Add([
  29018. 'type',
  29019. ' TFuncRef = reference to function(i: longint = 5): longint;',
  29020. ' TObject = class',
  29021. ' function Grow(s: longint): longint;',
  29022. ' end;',
  29023. 'var',
  29024. ' f: tfuncref;',
  29025. 'function tobject.grow(s: longint): longint;',
  29026. ' function GrowSub(i: longint): longint;',
  29027. ' begin',
  29028. ' f:=@grow;',
  29029. ' f:=@growsub;',
  29030. ' end;',
  29031. 'begin',
  29032. ' f:=@grow;',
  29033. ' f:=@growsub;',
  29034. 'end;',
  29035. 'begin',
  29036. '']);
  29037. ConvertProgram;
  29038. CheckSource('TestProcType_ReferenceToMethod',
  29039. LinesToStr([ // statements
  29040. 'rtl.createClass(this, "TObject", null, function () {',
  29041. ' this.$init = function () {',
  29042. ' };',
  29043. ' this.$final = function () {',
  29044. ' };',
  29045. ' this.Grow = function (s) {',
  29046. ' var $Self = this;',
  29047. ' var Result = 0;',
  29048. ' function GrowSub(i) {',
  29049. ' var Result = 0;',
  29050. ' $mod.f = rtl.createCallback($Self, "Grow");',
  29051. ' $mod.f = GrowSub;',
  29052. ' return Result;',
  29053. ' };',
  29054. ' $mod.f = rtl.createCallback($Self, "Grow");',
  29055. ' $mod.f = GrowSub;',
  29056. ' return Result;',
  29057. ' };',
  29058. '});',
  29059. 'this.f = null;',
  29060. '']),
  29061. LinesToStr([ // $mod.$main
  29062. '']));
  29063. end;
  29064. procedure TTestModule.TestProcType_Typecast;
  29065. begin
  29066. StartProgram(false);
  29067. Add([
  29068. 'type',
  29069. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  29070. ' TEvent = procedure of object;',
  29071. ' TGetter = function:longint of object;',
  29072. ' TProcA = procedure(i: longint);',
  29073. ' TFuncB = function(i, j: longint): longint;',
  29074. 'procedure DoIt(); varargs; begin end;',
  29075. 'var',
  29076. ' Notify: tnotifyevent;',
  29077. ' Event: tevent;',
  29078. ' Getter: tgetter;',
  29079. ' ProcA: tproca;',
  29080. ' FuncB: tfuncb;',
  29081. ' p: pointer;',
  29082. 'begin',
  29083. ' notify:=tnotifyevent(event);',
  29084. ' event:=tevent(event);',
  29085. ' event:=tevent(notify);',
  29086. ' event:=tevent(getter);',
  29087. ' event:=tevent(proca);',
  29088. ' proca:=tproca(funcb);',
  29089. ' funcb:=tfuncb(funcb);',
  29090. ' funcb:=tfuncb(proca);',
  29091. ' funcb:=tfuncb(getter);',
  29092. ' proca:=tproca(p);',
  29093. ' funcb:=tfuncb(p);',
  29094. ' getter:=tgetter(p);',
  29095. ' p:=pointer(notify);',
  29096. ' p:=notify;',
  29097. ' p:=pointer(proca);',
  29098. ' p:=proca;',
  29099. ' p:=pointer(funcb);',
  29100. ' p:=funcb;',
  29101. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  29102. '']);
  29103. ConvertProgram;
  29104. CheckSource('TestProcType_Typecast',
  29105. LinesToStr([ // statements
  29106. 'this.DoIt = function () {',
  29107. '};',
  29108. 'this.Notify = null;',
  29109. 'this.Event = null;',
  29110. 'this.Getter = null;',
  29111. 'this.ProcA = null;',
  29112. 'this.FuncB = null;',
  29113. 'this.p = null;',
  29114. '']),
  29115. LinesToStr([ // $mod.$main
  29116. '$mod.Notify = $mod.Event;',
  29117. '$mod.Event = $mod.Event;',
  29118. '$mod.Event = $mod.Notify;',
  29119. '$mod.Event = $mod.Getter;',
  29120. '$mod.Event = $mod.ProcA;',
  29121. '$mod.ProcA = $mod.FuncB;',
  29122. '$mod.FuncB = $mod.FuncB;',
  29123. '$mod.FuncB = $mod.ProcA;',
  29124. '$mod.FuncB = $mod.Getter;',
  29125. '$mod.ProcA = $mod.p;',
  29126. '$mod.FuncB = $mod.p;',
  29127. '$mod.Getter = $mod.p;',
  29128. '$mod.p = $mod.Notify;',
  29129. '$mod.p = $mod.Notify;',
  29130. '$mod.p = $mod.ProcA;',
  29131. '$mod.p = $mod.ProcA;',
  29132. '$mod.p = $mod.FuncB;',
  29133. '$mod.p = $mod.FuncB;',
  29134. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  29135. '']));
  29136. end;
  29137. procedure TTestModule.TestProcType_PassProcToUntyped;
  29138. begin
  29139. StartProgram(false);
  29140. Add([
  29141. 'type',
  29142. ' TEvent = procedure of object;',
  29143. ' TFunc = function: longint;',
  29144. 'procedure DoIt(); varargs; begin end;',
  29145. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  29146. 'var',
  29147. ' Event: tevent;',
  29148. ' Func: TFunc;',
  29149. 'begin',
  29150. ' doit(event,func);',
  29151. ' dosome(event,event,event);',
  29152. ' dosome(func,func,func);',
  29153. '']);
  29154. ConvertProgram;
  29155. CheckSource('TestProcType_PassProcToUntyped',
  29156. LinesToStr([ // statements
  29157. 'this.DoIt = function () {',
  29158. '};',
  29159. 'this.DoSome = function (a, b, p) {',
  29160. '};',
  29161. 'this.Event = null;',
  29162. 'this.Func = null;',
  29163. '']),
  29164. LinesToStr([ // $mod.$main
  29165. '$mod.DoIt($mod.Event, $mod.Func);',
  29166. '$mod.DoSome($mod.Event, {',
  29167. ' p: $mod,',
  29168. ' get: function () {',
  29169. ' return this.p.Event;',
  29170. ' },',
  29171. ' set: function (v) {',
  29172. ' this.p.Event = v;',
  29173. ' }',
  29174. '}, $mod.Event);',
  29175. '$mod.DoSome($mod.Func, {',
  29176. ' p: $mod,',
  29177. ' get: function () {',
  29178. ' return this.p.Func;',
  29179. ' },',
  29180. ' set: function (v) {',
  29181. ' this.p.Func = v;',
  29182. ' }',
  29183. '}, $mod.Func);',
  29184. '']));
  29185. end;
  29186. procedure TTestModule.TestProcType_PassProcToArray;
  29187. begin
  29188. StartProgram(false);
  29189. Add([
  29190. 'type',
  29191. ' TFunc = function: longint;',
  29192. ' TArrFunc = array of TFunc;',
  29193. 'procedure DoIt(Arr: TArrFunc); begin end;',
  29194. 'function GetIt: longint; begin end;',
  29195. 'var',
  29196. ' Func: tfunc;',
  29197. 'begin',
  29198. ' doit([]);',
  29199. ' doit([@GetIt]);',
  29200. ' doit([Func]);',
  29201. '']);
  29202. ConvertProgram;
  29203. CheckSource('TestProcType_PassProcToArray',
  29204. LinesToStr([ // statements
  29205. 'this.DoIt = function (Arr) {',
  29206. '};',
  29207. 'this.GetIt = function () {',
  29208. ' var Result = 0;',
  29209. ' return Result;',
  29210. '};',
  29211. 'this.Func = null;',
  29212. '']),
  29213. LinesToStr([ // $mod.$main
  29214. '$mod.DoIt([]);',
  29215. '$mod.DoIt([$mod.GetIt]);',
  29216. '$mod.DoIt([$mod.Func]);',
  29217. '']));
  29218. end;
  29219. procedure TTestModule.TestProcType_SafeCallObjFPC;
  29220. begin
  29221. StartProgram(false);
  29222. Add([
  29223. '{$modeswitch externalclass}',
  29224. 'type',
  29225. ' TProc = reference to procedure(i: longint); safecall;',
  29226. ' TEvent = procedure(i: longint) of object; safecall;',
  29227. ' TExtA = class external name ''ExtObj''',
  29228. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  29229. ' procedure DoSome(Id: longint = 1);',
  29230. ' procedure SetOnClick(const e: TEvent);',
  29231. ' property OnClick: TEvent write SetOnClick;',
  29232. ' class procedure Fly(Id: longint = 1); static;',
  29233. ' procedure SetOnShow(const p: TProc);',
  29234. ' property OnShow: TProc write SetOnShow;',
  29235. ' end;',
  29236. 'procedure Run(i: longint = 1);',
  29237. 'begin',
  29238. 'end;',
  29239. 'var',
  29240. ' Obj: texta;',
  29241. ' e: TEvent;',
  29242. ' p: TProc;',
  29243. 'begin',
  29244. ' e:=e;',
  29245. ' e:[email protected];',
  29246. ' e:[email protected];',
  29247. ' e:=TEvent(@obj.dosome);', // no safecall
  29248. ' obj.OnClick:[email protected];',
  29249. ' obj.OnClick:[email protected];',
  29250. ' obj.setonclick(@obj.doit);',
  29251. ' obj.setonclick(@obj.dosome);',
  29252. ' p:=@Run;',
  29253. ' p:[email protected];',
  29254. ' obj.OnShow:=@Run;',
  29255. ' obj.OnShow:[email protected];',
  29256. ' obj.setOnShow(@Run);',
  29257. ' obj.setOnShow(@TExtA.Fly);',
  29258. ' with obj do begin',
  29259. ' e:=@doit;',
  29260. ' e:=@dosome;',
  29261. ' OnClick:=@doit;',
  29262. ' OnClick:=@dosome;',
  29263. ' setonclick(@doit);',
  29264. ' setonclick(@dosome);',
  29265. ' OnShow:=@Run;',
  29266. ' setOnShow(@Run);',
  29267. ' end;']);
  29268. ConvertProgram;
  29269. CheckSource('TestProcType_SafeCallObjFPC',
  29270. LinesToStr([ // statements
  29271. 'this.Run = function (i) {',
  29272. '};',
  29273. 'this.Obj = null;',
  29274. 'this.e = null;',
  29275. 'this.p = null;',
  29276. '']),
  29277. LinesToStr([ // $mod.$main
  29278. '$mod.e = $mod.e;',
  29279. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  29280. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  29281. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  29282. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29283. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29284. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29285. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29286. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  29287. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  29288. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29289. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29290. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29291. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29292. 'var $with = $mod.Obj;',
  29293. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  29294. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  29295. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29296. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29297. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29298. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29299. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29300. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29301. '']));
  29302. end;
  29303. procedure TTestModule.TestProcType_SafeCallDelphi;
  29304. begin
  29305. StartProgram(false);
  29306. Add([
  29307. '{$mode delphi}',
  29308. '{$modeswitch externalclass}',
  29309. 'type',
  29310. ' TProc = reference to procedure(i: longint); safecall;',
  29311. ' TEvent = procedure(i: longint) of object; safecall;',
  29312. ' TExtA = class external name ''ExtObj''',
  29313. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  29314. ' procedure DoSome(Id: longint = 1);',
  29315. ' procedure SetOnClick(const e: TEvent);',
  29316. ' property OnClick: TEvent write SetOnClick;',
  29317. ' class procedure Fly(Id: longint = 1); static;',
  29318. ' procedure SetOnShow(const p: TProc);',
  29319. ' property OnShow: TProc write SetOnShow;',
  29320. ' end;',
  29321. 'procedure Run(i: longint = 1);',
  29322. 'begin',
  29323. 'end;',
  29324. 'var',
  29325. ' Obj: texta;',
  29326. ' e: TEvent;',
  29327. ' p: TProc;',
  29328. 'begin',
  29329. ' e:=e;',
  29330. ' e:=obj.doit;',
  29331. ' e:=obj.dosome;',
  29332. ' e:=TEvent(@obj.dosome);', // no safecall
  29333. ' obj.OnClick:=obj.doit;',
  29334. ' obj.OnClick:=obj.dosome;',
  29335. ' obj.setonclick(obj.doit);',
  29336. ' obj.setonclick(obj.dosome);',
  29337. ' p:=Run;',
  29338. ' p:=TExtA.Fly;',
  29339. ' obj.OnShow:=Run;',
  29340. ' obj.OnShow:=TExtA.Fly;',
  29341. ' obj.setOnShow(Run);',
  29342. ' obj.setOnShow(TExtA.Fly);',
  29343. ' with obj do begin',
  29344. ' e:=doit;',
  29345. ' e:=dosome;',
  29346. ' OnClick:=doit;',
  29347. ' OnClick:=dosome;',
  29348. ' setonclick(doit);',
  29349. ' setonclick(dosome);',
  29350. ' OnShow:=@Run;',
  29351. ' setOnShow(@Run);',
  29352. ' end;']);
  29353. ConvertProgram;
  29354. CheckSource('TestProcType_SafeCallDelphi',
  29355. LinesToStr([ // statements
  29356. 'this.Run = function (i) {',
  29357. '};',
  29358. 'this.Obj = null;',
  29359. 'this.e = null;',
  29360. 'this.p = null;',
  29361. '']),
  29362. LinesToStr([ // $mod.$main
  29363. '$mod.e = $mod.e;',
  29364. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  29365. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  29366. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  29367. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29368. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29369. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29370. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29371. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  29372. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  29373. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29374. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29375. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29376. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29377. 'var $with = $mod.Obj;',
  29378. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  29379. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  29380. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29381. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29382. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29383. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29384. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29385. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29386. '']));
  29387. end;
  29388. procedure TTestModule.TestProcType_SafeCall_Arg;
  29389. begin
  29390. StartProgram(false);
  29391. Add([
  29392. 'type',
  29393. ' TSafecallProc = reference to procedure; safecall;',
  29394. 'procedure Fly(const aHandler: TSafecallProc);',
  29395. 'var',
  29396. ' P: TSafecallProc;',
  29397. 'begin',
  29398. ' P := aHandler;',
  29399. ' Fly(P);',
  29400. ' Fly(aHandler);',
  29401. 'end;',
  29402. 'begin',
  29403. ' Fly(nil);',
  29404. '']);
  29405. ConvertProgram;
  29406. CheckSource('TestProcType_SafeCall_Arg',
  29407. LinesToStr([ // statements
  29408. 'this.Fly = function (aHandler) {',
  29409. ' var P = null;',
  29410. ' P = aHandler;',
  29411. ' $mod.Fly(P);',
  29412. ' $mod.Fly(aHandler);',
  29413. '};',
  29414. '']),
  29415. LinesToStr([ // $mod.$main
  29416. '$mod.Fly(null);',
  29417. '']));
  29418. end;
  29419. procedure TTestModule.TestPointer;
  29420. begin
  29421. StartProgram(false);
  29422. Add(['type',
  29423. ' TObject = class end;',
  29424. ' TClass = class of TObject;',
  29425. ' TArrInt = array of longint;',
  29426. 'const',
  29427. ' n = nil;',
  29428. 'var',
  29429. ' v: jsvalue;',
  29430. ' Obj: tobject;',
  29431. ' C: tclass;',
  29432. ' a: tarrint;',
  29433. ' p: Pointer = nil;',
  29434. ' s: string;',
  29435. 'begin',
  29436. ' p:=p;',
  29437. ' p:=nil;',
  29438. ' if p=nil then;',
  29439. ' if nil=p then;',
  29440. ' if Assigned(p) then;',
  29441. ' p:=Pointer(v);',
  29442. ' p:=obj;',
  29443. ' p:=c;',
  29444. ' p:=a;',
  29445. ' p:=tobject;',
  29446. ' obj:=TObject(p);',
  29447. ' c:=TClass(p);',
  29448. ' a:=TArrInt(p);',
  29449. ' p:=n;',
  29450. ' p:=Pointer(a);',
  29451. ' p:=pointer(s);',
  29452. ' s:=string(p);',
  29453. '']);
  29454. ConvertProgram;
  29455. CheckSource('TestPointer',
  29456. LinesToStr([ // statements
  29457. 'rtl.createClass(this, "TObject", null, function () {',
  29458. ' this.$init = function () {',
  29459. ' };',
  29460. ' this.$final = function () {',
  29461. ' };',
  29462. '});',
  29463. 'this.n = null;',
  29464. 'this.v = undefined;',
  29465. 'this.Obj = null;',
  29466. 'this.C = null;',
  29467. 'this.a = [];',
  29468. 'this.p = null;',
  29469. 'this.s = "";',
  29470. '']),
  29471. LinesToStr([ // $mod.$main
  29472. '$mod.p = $mod.p;',
  29473. '$mod.p = null;',
  29474. 'if ($mod.p === null) ;',
  29475. 'if (null === $mod.p) ;',
  29476. 'if ($mod.p != null) ;',
  29477. '$mod.p = $mod.v;',
  29478. '$mod.p = $mod.Obj;',
  29479. '$mod.p = $mod.C;',
  29480. '$mod.p = $mod.a;',
  29481. '$mod.p = $mod.TObject;',
  29482. '$mod.Obj = $mod.p;',
  29483. '$mod.C = $mod.p;',
  29484. '$mod.a = $mod.p;',
  29485. '$mod.p = null;',
  29486. '$mod.p = $mod.a;',
  29487. '$mod.p = $mod.s;',
  29488. '$mod.s = $mod.p;',
  29489. '']));
  29490. end;
  29491. procedure TTestModule.TestPointer_Proc;
  29492. begin
  29493. StartProgram(false);
  29494. Add('type');
  29495. Add(' TObject = class');
  29496. Add(' procedure DoIt; virtual; abstract;');
  29497. Add(' end;');
  29498. Add('procedure DoSome; begin end;');
  29499. Add('var');
  29500. Add(' o: TObject;');
  29501. Add(' p: Pointer;');
  29502. Add('begin');
  29503. Add(' p:=@DoSome;');
  29504. Add(' p:[email protected];');
  29505. ConvertProgram;
  29506. CheckSource('TestPointer_Proc',
  29507. LinesToStr([ // statements
  29508. 'rtl.createClass(this, "TObject", null, function () {',
  29509. ' this.$init = function () {',
  29510. ' };',
  29511. ' this.$final = function () {',
  29512. ' };',
  29513. '});',
  29514. 'this.DoSome = function () {',
  29515. '};',
  29516. 'this.o = null;',
  29517. 'this.p = null;',
  29518. '']),
  29519. LinesToStr([ // $mod.$main
  29520. '$mod.p = $mod.DoSome;',
  29521. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  29522. '']));
  29523. end;
  29524. procedure TTestModule.TestPointer_AssignRecordFail;
  29525. begin
  29526. StartProgram(false);
  29527. Add('type');
  29528. Add(' TRec = record end;');
  29529. Add('var');
  29530. Add(' p: Pointer;');
  29531. Add(' r: TRec;');
  29532. Add('begin');
  29533. Add(' p:=r;');
  29534. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  29535. nIncompatibleTypesGotExpected);
  29536. ConvertProgram;
  29537. end;
  29538. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  29539. begin
  29540. StartProgram(false);
  29541. Add('type');
  29542. Add(' TArr = array[boolean] of longint;');
  29543. Add('var');
  29544. Add(' p: Pointer;');
  29545. Add(' a: TArr;');
  29546. Add('begin');
  29547. Add(' p:=a;');
  29548. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  29549. nIncompatibleTypesGotExpected);
  29550. ConvertProgram;
  29551. end;
  29552. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  29553. begin
  29554. StartProgram(false);
  29555. Add([
  29556. 'procedure DoIt(args: array of jsvalue); begin end;',
  29557. 'procedure DoAll; varargs; begin end;',
  29558. 'var',
  29559. ' v: jsvalue;',
  29560. 'begin',
  29561. ' DoIt([pointer(v)]);',
  29562. ' DoAll(pointer(v));',
  29563. '']);
  29564. ConvertProgram;
  29565. CheckSource('TestPointer_TypeCastJSValueToPointer',
  29566. LinesToStr([ // statements
  29567. 'this.DoIt = function (args) {',
  29568. '};',
  29569. 'this.DoAll = function () {',
  29570. '};',
  29571. 'this.v = undefined;',
  29572. '']),
  29573. LinesToStr([ // $mod.$main
  29574. '$mod.DoIt([$mod.v]);',
  29575. '$mod.DoAll($mod.v);',
  29576. '']));
  29577. end;
  29578. procedure TTestModule.TestPointer_NonRecordFail;
  29579. begin
  29580. StartProgram(false);
  29581. Add([
  29582. 'type',
  29583. ' p = ^longint;',
  29584. 'begin',
  29585. '']);
  29586. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  29587. ConvertProgram;
  29588. end;
  29589. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  29590. begin
  29591. StartProgram(false);
  29592. Add([
  29593. 'procedure DoIt(p: ^longint); begin end;',
  29594. 'begin',
  29595. '']);
  29596. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 19',nParserParamsOrResultTypesNoLocalTypeDefs);
  29597. ConvertProgram;
  29598. end;
  29599. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  29600. begin
  29601. StartProgram(false);
  29602. Add([
  29603. 'var p: ^longint;',
  29604. 'begin',
  29605. '']);
  29606. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  29607. ConvertProgram;
  29608. end;
  29609. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  29610. begin
  29611. StartProgram(false);
  29612. Add([
  29613. 'function DoIt: ^longint; begin end;',
  29614. 'begin',
  29615. '']);
  29616. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 16',nParserParamsOrResultTypesNoLocalTypeDefs);
  29617. ConvertProgram;
  29618. end;
  29619. procedure TTestModule.TestPointer_AddrOperatorFail;
  29620. begin
  29621. StartProgram(false);
  29622. Add([
  29623. 'var i: longint;',
  29624. 'begin',
  29625. ' if @i=nil then ;',
  29626. '']);
  29627. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  29628. ConvertProgram;
  29629. end;
  29630. procedure TTestModule.TestPointer_ArrayParamsFail;
  29631. begin
  29632. StartProgram(false);
  29633. Add([
  29634. 'var',
  29635. ' p: Pointer;',
  29636. 'begin',
  29637. ' p:=p[1];',
  29638. '']);
  29639. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  29640. ConvertProgram;
  29641. end;
  29642. procedure TTestModule.TestPointer_PointerAddFail;
  29643. begin
  29644. StartProgram(false);
  29645. Add([
  29646. 'var',
  29647. ' p: Pointer;',
  29648. 'begin',
  29649. ' p:=p+1;',
  29650. '']);
  29651. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  29652. ConvertProgram;
  29653. end;
  29654. procedure TTestModule.TestPointer_IncPointerFail;
  29655. begin
  29656. StartProgram(false);
  29657. Add([
  29658. 'var',
  29659. ' p: Pointer;',
  29660. 'begin',
  29661. ' inc(p,1);',
  29662. '']);
  29663. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  29664. nIncompatibleTypeArgNo);
  29665. ConvertProgram;
  29666. end;
  29667. procedure TTestModule.TestPointer_Record;
  29668. begin
  29669. StartProgram(false);
  29670. Add([
  29671. 'type',
  29672. ' TRec = record x: longint; end;',
  29673. ' PRec = ^TRec;',
  29674. 'var',
  29675. ' r: TRec;',
  29676. ' p: PRec;',
  29677. ' q: ^TRec;',
  29678. ' Ptr: pointer;',
  29679. 'begin',
  29680. ' new(p);',
  29681. ' p:=@r;',
  29682. ' r:=p^;',
  29683. ' r.x:=p^.x;',
  29684. ' p^.x:=r.x;',
  29685. ' if p^.x=3 then ;',
  29686. ' if 4=p^.x then ;',
  29687. ' dispose(p);',
  29688. ' new(q);',
  29689. ' dispose(q);',
  29690. ' Ptr:=p;',
  29691. ' p:=PRec(ptr);',
  29692. '']);
  29693. ConvertProgram;
  29694. CheckSource('TestPointer_Record',
  29695. LinesToStr([ // statements
  29696. 'rtl.recNewT(this, "TRec", function () {',
  29697. ' this.x = 0;',
  29698. ' this.$eq = function (b) {',
  29699. ' return this.x === b.x;',
  29700. ' };',
  29701. ' this.$assign = function (s) {',
  29702. ' this.x = s.x;',
  29703. ' return this;',
  29704. ' };',
  29705. '});',
  29706. 'this.r = this.TRec.$new();',
  29707. 'this.p = null;',
  29708. 'this.q = null;',
  29709. 'this.Ptr = null;',
  29710. '']),
  29711. LinesToStr([ // $mod.$main
  29712. '$mod.p = $mod.TRec.$new();',
  29713. '$mod.p = $mod.r;',
  29714. '$mod.r.$assign($mod.p);',
  29715. '$mod.r.x = $mod.p.x;',
  29716. '$mod.p.x = $mod.r.x;',
  29717. 'if ($mod.p.x === 3) ;',
  29718. 'if (4 === $mod.p.x) ;',
  29719. '$mod.p = null;',
  29720. '$mod.q = $mod.TRec.$new();',
  29721. '$mod.q = null;',
  29722. '$mod.Ptr = $mod.p;',
  29723. '$mod.p = $mod.Ptr;',
  29724. '']));
  29725. end;
  29726. procedure TTestModule.TestPointer_RecordArg;
  29727. begin
  29728. StartProgram(false);
  29729. Add([
  29730. '{$modeswitch autoderef}',
  29731. 'type',
  29732. ' TRec = record x: longint; end;',
  29733. ' PRec = ^TRec;',
  29734. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  29735. 'begin',
  29736. ' a.x:=a.x;',
  29737. ' a^.x:=a^.x;',
  29738. ' with a^ do',
  29739. ' x:=x;',
  29740. 'end;',
  29741. 'function GetIt(p: PRec): PRec;',
  29742. 'begin',
  29743. ' p.x:=p.x;',
  29744. ' p^.x:=p^.x;',
  29745. ' with p^ do',
  29746. ' x:=x;',
  29747. 'end;',
  29748. 'var',
  29749. ' r: TRec;',
  29750. ' p: PRec;',
  29751. 'begin',
  29752. ' p:=GetIt(p);',
  29753. ' p^:=GetIt(@r)^;',
  29754. ' DoIt(p,p,p);',
  29755. ' DoIt(@r,p,p);',
  29756. '']);
  29757. ConvertProgram;
  29758. CheckSource('TestPointer_RecordArg',
  29759. LinesToStr([ // statements
  29760. 'rtl.recNewT(this, "TRec", function () {',
  29761. ' this.x = 0;',
  29762. ' this.$eq = function (b) {',
  29763. ' return this.x === b.x;',
  29764. ' };',
  29765. ' this.$assign = function (s) {',
  29766. ' this.x = s.x;',
  29767. ' return this;',
  29768. ' };',
  29769. '});',
  29770. 'this.DoIt = function (a, b, c) {',
  29771. ' var Result = $mod.TRec.$new();',
  29772. ' a.x = a.x;',
  29773. ' a.x = a.x;',
  29774. ' a.x = a.x;',
  29775. ' return Result;',
  29776. '};',
  29777. 'this.GetIt = function (p) {',
  29778. ' var Result = null;',
  29779. ' p.x = p.x;',
  29780. ' p.x = p.x;',
  29781. ' p.x = p.x;',
  29782. ' return Result;',
  29783. '};',
  29784. 'this.r = this.TRec.$new();',
  29785. 'this.p = null;',
  29786. '']),
  29787. LinesToStr([ // $mod.$main
  29788. '$mod.p = $mod.GetIt($mod.p);',
  29789. '$mod.p.$assign($mod.GetIt($mod.r));',
  29790. '$mod.DoIt($mod.p, {',
  29791. ' p: $mod,',
  29792. ' get: function () {',
  29793. ' return this.p.p;',
  29794. ' },',
  29795. ' set: function (v) {',
  29796. ' this.p.p = v;',
  29797. ' }',
  29798. '}, {',
  29799. ' p: $mod,',
  29800. ' get: function () {',
  29801. ' return this.p.p;',
  29802. ' },',
  29803. ' set: function (v) {',
  29804. ' this.p.p = v;',
  29805. ' }',
  29806. '});',
  29807. '$mod.DoIt($mod.r, {',
  29808. ' p: $mod,',
  29809. ' get: function () {',
  29810. ' return this.p.p;',
  29811. ' },',
  29812. ' set: function (v) {',
  29813. ' this.p.p = v;',
  29814. ' }',
  29815. '}, {',
  29816. ' p: $mod,',
  29817. ' get: function () {',
  29818. ' return this.p.p;',
  29819. ' },',
  29820. ' set: function (v) {',
  29821. ' this.p.p = v;',
  29822. ' }',
  29823. '});',
  29824. '']));
  29825. end;
  29826. procedure TTestModule.TestJSValue_AssignToJSValue;
  29827. begin
  29828. StartProgram(false);
  29829. Add('var');
  29830. Add(' v: jsvalue;');
  29831. Add(' i: longint;');
  29832. Add(' s: string;');
  29833. Add(' b: boolean;');
  29834. Add(' d: double;');
  29835. Add(' p: pointer;');
  29836. Add('begin');
  29837. Add(' v:=v;');
  29838. Add(' v:=1;');
  29839. Add(' v:=i;');
  29840. Add(' v:='''';');
  29841. Add(' v:=''c'';');
  29842. Add(' v:=''foo'';');
  29843. Add(' v:=s;');
  29844. Add(' v:=false;');
  29845. Add(' v:=true;');
  29846. Add(' v:=b;');
  29847. Add(' v:=0.1;');
  29848. Add(' v:=d;');
  29849. Add(' v:=nil;');
  29850. Add(' v:=p;');
  29851. ConvertProgram;
  29852. CheckSource('TestJSValue_AssignToJSValue',
  29853. LinesToStr([ // statements
  29854. 'this.v = undefined;',
  29855. 'this.i = 0;',
  29856. 'this.s = "";',
  29857. 'this.b = false;',
  29858. 'this.d = 0.0;',
  29859. 'this.p = null;',
  29860. '']),
  29861. LinesToStr([ // $mod.$main
  29862. '$mod.v = $mod.v;',
  29863. '$mod.v = 1;',
  29864. '$mod.v = $mod.i;',
  29865. '$mod.v = "";',
  29866. '$mod.v = "c";',
  29867. '$mod.v = "foo";',
  29868. '$mod.v = $mod.s;',
  29869. '$mod.v = false;',
  29870. '$mod.v = true;',
  29871. '$mod.v = $mod.b;',
  29872. '$mod.v = 0.1;',
  29873. '$mod.v = $mod.d;',
  29874. '$mod.v = null;',
  29875. '$mod.v = $mod.p;',
  29876. '']));
  29877. end;
  29878. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29879. begin
  29880. StartProgram(false);
  29881. Add('type');
  29882. Add(' integer = longint;');
  29883. Add(' TYesNo = boolean;');
  29884. Add(' TFloat = double;');
  29885. Add(' TCaption = string;');
  29886. Add(' TChar = char;');
  29887. Add('var');
  29888. Add(' v: jsvalue;');
  29889. Add(' i: integer;');
  29890. Add(' s: TCaption;');
  29891. Add(' b: TYesNo;');
  29892. Add(' d: TFloat;');
  29893. Add(' c: char;');
  29894. Add('begin');
  29895. Add(' i:=longint(v);');
  29896. Add(' i:=integer(v);');
  29897. Add(' s:=string(v);');
  29898. Add(' s:=TCaption(v);');
  29899. Add(' b:=boolean(v);');
  29900. Add(' b:=TYesNo(v);');
  29901. Add(' d:=double(v);');
  29902. Add(' d:=TFloat(v);');
  29903. Add(' c:=char(v);');
  29904. Add(' c:=TChar(v);');
  29905. ConvertProgram;
  29906. CheckSource('TestJSValue_TypeCastToBaseType',
  29907. LinesToStr([ // statements
  29908. 'this.v = undefined;',
  29909. 'this.i = 0;',
  29910. 'this.s = "";',
  29911. 'this.b = false;',
  29912. 'this.d = 0.0;',
  29913. 'this.c = "\x00";',
  29914. '']),
  29915. LinesToStr([ // $mod.$main
  29916. '$mod.i = rtl.trunc($mod.v);',
  29917. '$mod.i = rtl.trunc($mod.v);',
  29918. '$mod.s = "" + $mod.v;',
  29919. '$mod.s = "" + $mod.v;',
  29920. '$mod.b = !($mod.v == false);',
  29921. '$mod.b = !($mod.v == false);',
  29922. '$mod.d = rtl.getNumber($mod.v);',
  29923. '$mod.d = rtl.getNumber($mod.v);',
  29924. '$mod.c = rtl.getChar($mod.v);',
  29925. '$mod.c = rtl.getChar($mod.v);',
  29926. '']));
  29927. end;
  29928. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29929. begin
  29930. StartProgram(false);
  29931. Add([
  29932. 'type',
  29933. ' TArr = array of word;',
  29934. ' TRec = record end;',
  29935. ' TSet = set of boolean;',
  29936. 'procedure Fly(v: jsvalue);',
  29937. 'begin',
  29938. 'end;',
  29939. 'var',
  29940. ' a: TArr;',
  29941. ' r: TRec;',
  29942. ' s: TSet;',
  29943. 'begin',
  29944. ' Fly(jsvalue(a));',
  29945. ' Fly(jsvalue(r));',
  29946. ' Fly(jsvalue(s));',
  29947. '']);
  29948. ConvertProgram;
  29949. CheckSource('TestJSValue_TypecastToJSValue',
  29950. LinesToStr([ // statements
  29951. 'rtl.recNewT(this, "TRec", function () {',
  29952. ' this.$eq = function (b) {',
  29953. ' return true;',
  29954. ' };',
  29955. ' this.$assign = function (s) {',
  29956. ' return this;',
  29957. ' };',
  29958. '});',
  29959. 'this.Fly = function (v) {',
  29960. '};',
  29961. 'this.a = [];',
  29962. 'this.r = this.TRec.$new();',
  29963. 'this.s = {};',
  29964. '']),
  29965. LinesToStr([ // $mod.$main
  29966. '$mod.Fly($mod.a);',
  29967. '$mod.Fly($mod.r);',
  29968. '$mod.Fly($mod.s);',
  29969. '']));
  29970. end;
  29971. procedure TTestModule.TestJSValue_Equal;
  29972. begin
  29973. StartProgram(false);
  29974. Add('type');
  29975. Add(' integer = longint;');
  29976. Add(' TYesNo = boolean;');
  29977. Add(' TFloat = double;');
  29978. Add(' TCaption = string;');
  29979. Add(' TChar = char;');
  29980. Add(' TMulti = JSValue;');
  29981. Add('var');
  29982. Add(' v: jsvalue;');
  29983. Add(' i: integer;');
  29984. Add(' s: TCaption;');
  29985. Add(' b: TYesNo;');
  29986. Add(' d: TFloat;');
  29987. Add(' c: char;');
  29988. Add(' m: TMulti;');
  29989. Add('begin');
  29990. Add(' b:=v=v;');
  29991. Add(' b:=v<>v;');
  29992. Add(' b:=v=1;');
  29993. Add(' b:=v<>1;');
  29994. Add(' b:=2=v;');
  29995. Add(' b:=2<>v;');
  29996. Add(' b:=v=i;');
  29997. Add(' b:=i=v;');
  29998. Add(' b:=v=nil;');
  29999. Add(' b:=nil=v;');
  30000. Add(' b:=v=false;');
  30001. Add(' b:=true=v;');
  30002. Add(' b:=v=b;');
  30003. Add(' b:=b=v;');
  30004. Add(' b:=v=s;');
  30005. Add(' b:=s=v;');
  30006. Add(' b:=v=''foo'';');
  30007. Add(' b:=''''=v;');
  30008. Add(' b:=v=d;');
  30009. Add(' b:=d=v;');
  30010. Add(' b:=v=3.4;');
  30011. Add(' b:=5.6=v;');
  30012. Add(' b:=v=c;');
  30013. Add(' b:=c=v;');
  30014. Add(' b:=m=m;');
  30015. Add(' b:=v=m;');
  30016. Add(' b:=m=v;');
  30017. ConvertProgram;
  30018. CheckSource('TestJSValue_Equal',
  30019. LinesToStr([ // statements
  30020. 'this.v = undefined;',
  30021. 'this.i = 0;',
  30022. 'this.s = "";',
  30023. 'this.b = false;',
  30024. 'this.d = 0.0;',
  30025. 'this.c = "\x00";',
  30026. 'this.m = undefined;',
  30027. '']),
  30028. LinesToStr([ // $mod.$main
  30029. '$mod.b = $mod.v == $mod.v;',
  30030. '$mod.b = $mod.v != $mod.v;',
  30031. '$mod.b = $mod.v == 1;',
  30032. '$mod.b = $mod.v != 1;',
  30033. '$mod.b = 2 == $mod.v;',
  30034. '$mod.b = 2 != $mod.v;',
  30035. '$mod.b = $mod.v == $mod.i;',
  30036. '$mod.b = $mod.i == $mod.v;',
  30037. '$mod.b = $mod.v == null;',
  30038. '$mod.b = null == $mod.v;',
  30039. '$mod.b = $mod.v == false;',
  30040. '$mod.b = true == $mod.v;',
  30041. '$mod.b = $mod.v == $mod.b;',
  30042. '$mod.b = $mod.b == $mod.v;',
  30043. '$mod.b = $mod.v == $mod.s;',
  30044. '$mod.b = $mod.s == $mod.v;',
  30045. '$mod.b = $mod.v == "foo";',
  30046. '$mod.b = "" == $mod.v;',
  30047. '$mod.b = $mod.v == $mod.d;',
  30048. '$mod.b = $mod.d == $mod.v;',
  30049. '$mod.b = $mod.v == 3.4;',
  30050. '$mod.b = 5.6 == $mod.v;',
  30051. '$mod.b = $mod.v == $mod.c;',
  30052. '$mod.b = $mod.c == $mod.v;',
  30053. '$mod.b = $mod.m == $mod.m;',
  30054. '$mod.b = $mod.v == $mod.m;',
  30055. '$mod.b = $mod.m == $mod.v;',
  30056. '']));
  30057. end;
  30058. procedure TTestModule.TestJSValue_If;
  30059. begin
  30060. StartProgram(false);
  30061. Add([
  30062. 'procedure Fly(var u);',
  30063. 'begin',
  30064. ' if jsvalue(u) then ;',
  30065. 'end;',
  30066. 'var',
  30067. ' v: jsvalue;',
  30068. 'begin',
  30069. ' if v then ;',
  30070. ' while v do ;',
  30071. ' repeat until v;',
  30072. '']);
  30073. ConvertProgram;
  30074. CheckSource('TestJSValue_If',
  30075. LinesToStr([ // statements
  30076. 'this.Fly = function (u) {',
  30077. ' if (u.get()) ;',
  30078. '};',
  30079. 'this.v = undefined;',
  30080. '']),
  30081. LinesToStr([ // $mod.$main
  30082. 'if ($mod.v) ;',
  30083. 'while($mod.v){',
  30084. '};',
  30085. 'do{',
  30086. '} while(!$mod.v);',
  30087. '']));
  30088. end;
  30089. procedure TTestModule.TestJSValue_Not;
  30090. begin
  30091. StartProgram(false);
  30092. Add([
  30093. 'var',
  30094. ' v: jsvalue;',
  30095. ' b: boolean;',
  30096. 'begin',
  30097. ' b:=not v;',
  30098. ' if not v then ;',
  30099. ' while not v do ;',
  30100. ' repeat until not v;',
  30101. '']);
  30102. ConvertProgram;
  30103. CheckSource('TestJSValue_If',
  30104. LinesToStr([ // statements
  30105. 'this.v = undefined;',
  30106. 'this.b = false;',
  30107. '']),
  30108. LinesToStr([ // $mod.$main
  30109. '$mod.b=!$mod.v;',
  30110. 'if (!$mod.v) ;',
  30111. 'while(!$mod.v){',
  30112. '};',
  30113. 'do{',
  30114. '} while($mod.v);',
  30115. '']));
  30116. end;
  30117. procedure TTestModule.TestJSValue_Enum;
  30118. begin
  30119. StartProgram(false);
  30120. Add('type');
  30121. Add(' TColor = (red, blue);');
  30122. Add(' TRedBlue = TColor;');
  30123. Add('var');
  30124. Add(' v: jsvalue;');
  30125. Add(' e: TColor;');
  30126. Add('begin');
  30127. Add(' v:=e;');
  30128. Add(' v:=TColor(e);');
  30129. Add(' v:=TRedBlue(e);');
  30130. Add(' e:=TColor(v);');
  30131. Add(' e:=TRedBlue(v);');
  30132. ConvertProgram;
  30133. CheckSource('TestJSValue_Enum',
  30134. LinesToStr([ // statements
  30135. 'this.TColor = {',
  30136. ' "0": "red",',
  30137. ' red: 0,',
  30138. ' "1": "blue",',
  30139. ' blue: 1',
  30140. '};',
  30141. 'this.v = undefined;',
  30142. 'this.e = 0;',
  30143. '']),
  30144. LinesToStr([ // $mod.$main
  30145. '$mod.v = $mod.e;',
  30146. '$mod.v = $mod.e;',
  30147. '$mod.v = $mod.e;',
  30148. '$mod.e = $mod.v;',
  30149. '$mod.e = $mod.v;',
  30150. '']));
  30151. end;
  30152. procedure TTestModule.TestJSValue_ClassInstance;
  30153. begin
  30154. StartProgram(false);
  30155. Add([
  30156. 'type',
  30157. ' TObject = class',
  30158. ' end;',
  30159. ' TBirdObject = TObject;',
  30160. 'var',
  30161. ' v: jsvalue;',
  30162. ' o: TObject;',
  30163. 'begin',
  30164. ' v:=o;',
  30165. ' v:=TObject(o);',
  30166. ' v:=TBirdObject(o);',
  30167. ' o:=TObject(v);',
  30168. ' o:=TBirdObject(v);',
  30169. ' if v is TObject then ;',
  30170. '']);
  30171. ConvertProgram;
  30172. CheckSource('TestJSValue_ClassInstance',
  30173. LinesToStr([ // statements
  30174. 'rtl.createClass(this, "TObject", null, function () {',
  30175. ' this.$init = function () {',
  30176. ' };',
  30177. ' this.$final = function () {',
  30178. ' };',
  30179. '});',
  30180. 'this.v = undefined;',
  30181. 'this.o = null;',
  30182. '']),
  30183. LinesToStr([ // $mod.$main
  30184. '$mod.v = $mod.o;',
  30185. '$mod.v = $mod.o;',
  30186. '$mod.v = $mod.o;',
  30187. '$mod.o = rtl.getObject($mod.v);',
  30188. '$mod.o = rtl.getObject($mod.v);',
  30189. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  30190. '']));
  30191. end;
  30192. procedure TTestModule.TestJSValue_ClassOf;
  30193. begin
  30194. StartProgram(false);
  30195. Add([
  30196. 'type',
  30197. ' TClass = class of TObject;',
  30198. ' TObject = class',
  30199. ' end;',
  30200. ' TBirds = class of TBird;',
  30201. ' TBird = class(TObject) end;',
  30202. 'var',
  30203. ' v: jsvalue;',
  30204. ' c: TClass;',
  30205. 'begin',
  30206. ' v:=c;',
  30207. ' v:=TObject;',
  30208. ' v:=TClass(c);',
  30209. ' v:=TBirds(c);',
  30210. ' c:=TClass(v);',
  30211. ' c:=TBirds(v);',
  30212. ' if v is TClass then ;',
  30213. '']);
  30214. ConvertProgram;
  30215. CheckSource('TestJSValue_ClassOf',
  30216. LinesToStr([ // statements
  30217. 'rtl.createClass(this, "TObject", null, function () {',
  30218. ' this.$init = function () {',
  30219. ' };',
  30220. ' this.$final = function () {',
  30221. ' };',
  30222. '});',
  30223. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30224. '});',
  30225. 'this.v = undefined;',
  30226. 'this.c = null;',
  30227. '']),
  30228. LinesToStr([ // $mod.$main
  30229. '$mod.v = $mod.c;',
  30230. '$mod.v = $mod.TObject;',
  30231. '$mod.v = $mod.c;',
  30232. '$mod.v = $mod.c;',
  30233. '$mod.c = rtl.getObject($mod.v);',
  30234. '$mod.c = rtl.getObject($mod.v);',
  30235. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  30236. '']));
  30237. end;
  30238. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  30239. begin
  30240. StartProgram(false);
  30241. Add([
  30242. 'type',
  30243. ' integer = longint;',
  30244. ' TArray = array of JSValue;',
  30245. ' TArrgh = tarray;',
  30246. ' TArrInt = array of integer;',
  30247. 'var',
  30248. ' v: jsvalue;',
  30249. ' TheArray: tarray = (1,''2'');',
  30250. ' Arr: tarrgh;',
  30251. ' i: integer;',
  30252. ' ArrInt: tarrint;',
  30253. 'begin',
  30254. ' arr:=thearray;',
  30255. ' thearray:=arr;',
  30256. ' setlength(arr,2);',
  30257. ' setlength(thearray,3);',
  30258. ' arr[4]:=v;',
  30259. ' arr[5]:=length(thearray);',
  30260. ' arr[6]:=nil;',
  30261. ' arr[7]:=thearray[8];',
  30262. ' arr[low(arr)]:=high(thearray);',
  30263. ' arr:=arrint;',
  30264. ' arrInt:=tarrint(arr);',
  30265. ' if TheArray = nil then ;',
  30266. ' if nil = TheArray then ;',
  30267. ' if TheArray <> nil then ;',
  30268. ' if nil <> TheArray then ;',
  30269. '']);
  30270. ConvertProgram;
  30271. CheckSource('TestJSValue_ArrayOfJSValue',
  30272. LinesToStr([ // statements
  30273. 'this.v = undefined;',
  30274. 'this.TheArray = [1, "2"];',
  30275. 'this.Arr = [];',
  30276. 'this.i = 0;',
  30277. 'this.ArrInt = [];',
  30278. '']),
  30279. LinesToStr([ // $mod.$main
  30280. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  30281. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  30282. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  30283. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  30284. '$mod.Arr[4] = $mod.v;',
  30285. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  30286. '$mod.Arr[6] = null;',
  30287. '$mod.Arr[7] = $mod.TheArray[8];',
  30288. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  30289. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  30290. '$mod.ArrInt = $mod.Arr;',
  30291. 'if (rtl.length($mod.TheArray) === 0) ;',
  30292. 'if (rtl.length($mod.TheArray) === 0) ;',
  30293. 'if (rtl.length($mod.TheArray) > 0) ;',
  30294. 'if (rtl.length($mod.TheArray) > 0) ;',
  30295. '']));
  30296. end;
  30297. procedure TTestModule.TestJSValue_ArrayLit;
  30298. begin
  30299. StartProgram(false);
  30300. Add([
  30301. 'type',
  30302. ' TFlag = (big,small);',
  30303. ' TArray = array of JSValue;',
  30304. ' TObject = class end;',
  30305. ' TClass = class of TObject;',
  30306. 'var',
  30307. ' v: jsvalue;',
  30308. ' a: TArray;',
  30309. ' o: TObject;',
  30310. 'begin',
  30311. ' a:=[];',
  30312. ' a:=[1];',
  30313. ' a:=[1,2];',
  30314. ' a:=[big];',
  30315. ' a:=[1,big];',
  30316. ' a:=[o,nil];',
  30317. '']);
  30318. ConvertProgram;
  30319. CheckSource('TestJSValue_ArrayLit',
  30320. LinesToStr([ // statements
  30321. 'this.TFlag = {',
  30322. ' "0": "big",',
  30323. ' big: 0,',
  30324. ' "1": "small",',
  30325. ' small: 1',
  30326. '};',
  30327. 'rtl.createClass(this, "TObject", null, function () {',
  30328. ' this.$init = function () {',
  30329. ' };',
  30330. ' this.$final = function () {',
  30331. ' };',
  30332. '});',
  30333. 'this.v = undefined;',
  30334. 'this.a = [];',
  30335. 'this.o = null;',
  30336. '']),
  30337. LinesToStr([ // $mod.$main
  30338. '$mod.a = [];',
  30339. '$mod.a = [1];',
  30340. '$mod.a = [1, 2];',
  30341. '$mod.a = [$mod.TFlag.big];',
  30342. '$mod.a = [1, $mod.TFlag.big];',
  30343. '$mod.a = [$mod.o, null];',
  30344. '']));
  30345. end;
  30346. procedure TTestModule.TestJSValue_Params;
  30347. begin
  30348. StartProgram(false);
  30349. Add('type');
  30350. Add(' integer = longint;');
  30351. Add(' TYesNo = boolean;');
  30352. Add(' TFloat = double;');
  30353. Add(' TCaption = string;');
  30354. Add(' TChar = char;');
  30355. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  30356. Add('var');
  30357. Add(' l: jsvalue;');
  30358. Add('begin');
  30359. Add(' a:=a;');
  30360. Add(' l:=b;');
  30361. Add(' c:=c;');
  30362. Add(' d:=d;');
  30363. Add(' Result:=l;');
  30364. Add('end;');
  30365. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  30366. Add('var');
  30367. Add(' v: jsvalue;');
  30368. Add(' i: integer;');
  30369. Add(' b: TYesNo;');
  30370. Add(' d: TFloat;');
  30371. Add(' s: TCaption;');
  30372. Add(' c: TChar;');
  30373. Add('begin');
  30374. Add(' v:=doit(v,v,v,v);');
  30375. Add(' i:=integer(dosome(i,i));');
  30376. Add(' b:=TYesNo(dosome(b,b));');
  30377. Add(' d:=TFloat(dosome(d,d));');
  30378. Add(' s:=TCaption(dosome(s,s));');
  30379. Add(' c:=TChar(dosome(c,c));');
  30380. ConvertProgram;
  30381. CheckSource('TestJSValue_Params',
  30382. LinesToStr([ // statements
  30383. 'this.DoIt = function (a, b, c, d) {',
  30384. ' var Result = undefined;',
  30385. ' var l = undefined;',
  30386. ' a = a;',
  30387. ' l = b;',
  30388. ' c.set(c.get());',
  30389. ' d.set(d.get());',
  30390. ' Result = l;',
  30391. ' return Result;',
  30392. '};',
  30393. 'this.DoSome = function (a, b) {',
  30394. ' var Result = undefined;',
  30395. ' return Result;',
  30396. '};',
  30397. 'this.v = undefined;',
  30398. 'this.i = 0;',
  30399. 'this.b = false;',
  30400. 'this.d = 0.0;',
  30401. 'this.s = "";',
  30402. 'this.c = "\x00";',
  30403. '']),
  30404. LinesToStr([ // $mod.$main
  30405. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  30406. ' p: $mod,',
  30407. ' get: function () {',
  30408. ' return this.p.v;',
  30409. ' },',
  30410. ' set: function (v) {',
  30411. ' this.p.v = v;',
  30412. ' }',
  30413. '}, {',
  30414. ' p: $mod,',
  30415. ' get: function () {',
  30416. ' return this.p.v;',
  30417. ' },',
  30418. ' set: function (v) {',
  30419. ' this.p.v = v;',
  30420. ' }',
  30421. '});',
  30422. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  30423. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  30424. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  30425. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  30426. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  30427. '']));
  30428. end;
  30429. procedure TTestModule.TestJSValue_UntypedParam;
  30430. begin
  30431. StartProgram(false);
  30432. Add('function DoIt(const a; var b; out c): jsvalue;');
  30433. Add('begin');
  30434. Add(' Result:=a;');
  30435. Add(' Result:=b;');
  30436. Add(' Result:=c;');
  30437. Add(' b:=Result;');
  30438. Add(' c:=Result;');
  30439. Add('end;');
  30440. Add('var i: longint;');
  30441. Add('begin');
  30442. Add(' doit(i,i,i);');
  30443. ConvertProgram;
  30444. CheckSource('TestJSValue_UntypedParam',
  30445. LinesToStr([ // statements
  30446. 'this.DoIt = function (a, b, c) {',
  30447. ' var Result = undefined;',
  30448. ' Result = a;',
  30449. ' Result = b.get();',
  30450. ' Result = c.get();',
  30451. ' b.set(Result);',
  30452. ' c.set(Result);',
  30453. ' return Result;',
  30454. '};',
  30455. 'this.i = 0;',
  30456. '']),
  30457. LinesToStr([ // $mod.$main
  30458. '$mod.DoIt($mod.i, {',
  30459. ' p: $mod,',
  30460. ' get: function () {',
  30461. ' return this.p.i;',
  30462. ' },',
  30463. ' set: function (v) {',
  30464. ' this.p.i = v;',
  30465. ' }',
  30466. '}, {',
  30467. ' p: $mod,',
  30468. ' get: function () {',
  30469. ' return this.p.i;',
  30470. ' },',
  30471. ' set: function (v) {',
  30472. ' this.p.i = v;',
  30473. ' }',
  30474. '});',
  30475. '']));
  30476. end;
  30477. procedure TTestModule.TestJSValue_FuncResultType;
  30478. begin
  30479. StartProgram(false);
  30480. Add('type');
  30481. Add(' integer = longint;');
  30482. Add(' TJSValueArray = array of JSValue;');
  30483. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  30484. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  30485. Add('begin');
  30486. Add(' while Compare(P,aList[0])>0 do ;');
  30487. Add('end;');
  30488. Add('var');
  30489. Add(' Compare: TListSortCompare;');
  30490. Add(' V: JSValue;');
  30491. Add(' i: integer;');
  30492. Add('begin');
  30493. Add(' if Compare(V,V)>0 then ;');
  30494. Add(' if Compare(i,i)>1 then ;');
  30495. Add(' if Compare(nil,false)>2 then ;');
  30496. Add(' if Compare(1,true)>3 then ;');
  30497. ConvertProgram;
  30498. CheckSource('TestJSValue_UntypedParam',
  30499. LinesToStr([ // statements
  30500. 'this.Sort = function (P, aList, Compare) {',
  30501. ' while (Compare(P, aList[0]) > 0) {',
  30502. ' };',
  30503. '};',
  30504. 'this.Compare = null;',
  30505. 'this.V = undefined;',
  30506. 'this.i = 0;',
  30507. '']),
  30508. LinesToStr([ // $mod.$main
  30509. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  30510. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  30511. 'if ($mod.Compare(null, false) > 2) ;',
  30512. 'if ($mod.Compare(1, true) > 3) ;',
  30513. '']));
  30514. end;
  30515. procedure TTestModule.TestJSValue_ProcType_Assign;
  30516. begin
  30517. StartProgram(false);
  30518. Add('type');
  30519. Add(' integer = longint;');
  30520. Add(' TObject = class');
  30521. Add(' class function GetGlob: integer;');
  30522. Add(' function Getter: integer;');
  30523. Add(' end;');
  30524. Add('class function TObject.GetGlob: integer;');
  30525. Add('var v1: jsvalue;');
  30526. Add('begin');
  30527. Add(' v1:=@GetGlob;');
  30528. Add(' v1:[email protected];');
  30529. Add('end;');
  30530. Add('function TObject.Getter: integer;');
  30531. Add('var v2: jsvalue;');
  30532. Add('begin');
  30533. Add(' v2:=@Getter;');
  30534. Add(' v2:[email protected];');
  30535. Add(' v2:=@GetGlob;');
  30536. Add(' v2:[email protected];');
  30537. Add('end;');
  30538. Add('function GetIt(i: integer): integer;');
  30539. Add('var v3: jsvalue;');
  30540. Add('begin');
  30541. Add(' v3:=@GetIt;');
  30542. Add('end;');
  30543. Add('var');
  30544. Add(' V: JSValue;');
  30545. Add(' o: TObject;');
  30546. Add('begin');
  30547. Add(' v:=@GetIt;');
  30548. Add(' v:[email protected];');
  30549. Add(' v:[email protected];');
  30550. ConvertProgram;
  30551. CheckSource('TestJSValue_ProcType_Assign',
  30552. LinesToStr([ // statements
  30553. 'rtl.createClass(this, "TObject", null, function () {',
  30554. ' this.$init = function () {',
  30555. ' };',
  30556. ' this.$final = function () {',
  30557. ' };',
  30558. ' this.GetGlob = function () {',
  30559. ' var Result = 0;',
  30560. ' var v1 = undefined;',
  30561. ' v1 = rtl.createCallback(this, "GetGlob");',
  30562. ' v1 = rtl.createCallback(this, "GetGlob");',
  30563. ' return Result;',
  30564. ' };',
  30565. ' this.Getter = function () {',
  30566. ' var Result = 0;',
  30567. ' var v2 = undefined;',
  30568. ' v2 = rtl.createCallback(this, "Getter");',
  30569. ' v2 = rtl.createCallback(this, "Getter");',
  30570. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30571. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30572. ' return Result;',
  30573. ' };',
  30574. '});',
  30575. 'this.GetIt = function (i) {',
  30576. ' var Result = 0;',
  30577. ' var v3 = undefined;',
  30578. ' v3 = $mod.GetIt;',
  30579. ' return Result;',
  30580. '};',
  30581. 'this.V = undefined;',
  30582. 'this.o = null;',
  30583. '']),
  30584. LinesToStr([ // $mod.$main
  30585. '$mod.V = $mod.GetIt;',
  30586. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  30587. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  30588. '']));
  30589. end;
  30590. procedure TTestModule.TestJSValue_ProcType_Equal;
  30591. begin
  30592. StartProgram(false);
  30593. Add('type');
  30594. Add(' integer = longint;');
  30595. Add(' TObject = class');
  30596. Add(' class function GetGlob: integer;');
  30597. Add(' function Getter: integer;');
  30598. Add(' end;');
  30599. Add('class function TObject.GetGlob: integer;');
  30600. Add('var v1: jsvalue;');
  30601. Add('begin');
  30602. Add(' if v1=@GetGlob then;');
  30603. Add(' if [email protected] then ;');
  30604. Add('end;');
  30605. Add('function TObject.Getter: integer;');
  30606. Add('var v2: jsvalue;');
  30607. Add('begin');
  30608. Add(' if v2=@Getter then;');
  30609. Add(' if [email protected] then ;');
  30610. Add(' if v2=@GetGlob then;');
  30611. Add(' if [email protected] then;');
  30612. Add('end;');
  30613. Add('function GetIt(i: integer): integer;');
  30614. Add('var v3: jsvalue;');
  30615. Add('begin');
  30616. Add(' if v3=@GetIt then;');
  30617. Add('end;');
  30618. Add('var');
  30619. Add(' V: JSValue;');
  30620. Add(' o: TObject;');
  30621. Add('begin');
  30622. Add(' if v=@GetIt then;');
  30623. Add(' if [email protected] then;');
  30624. Add(' if [email protected] then;');
  30625. Add(' if @GetIt=v then;');
  30626. Add(' if @o.Getter=v then;');
  30627. Add(' if @o.GetGlob=v then;');
  30628. ConvertProgram;
  30629. CheckSource('TestJSValue_ProcType_Equal',
  30630. LinesToStr([ // statements
  30631. 'rtl.createClass(this, "TObject", null, function () {',
  30632. ' this.$init = function () {',
  30633. ' };',
  30634. ' this.$final = function () {',
  30635. ' };',
  30636. ' this.GetGlob = function () {',
  30637. ' var Result = 0;',
  30638. ' var v1 = undefined;',
  30639. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30640. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30641. ' return Result;',
  30642. ' };',
  30643. ' this.Getter = function () {',
  30644. ' var Result = 0;',
  30645. ' var v2 = undefined;',
  30646. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30647. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30648. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30649. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30650. ' return Result;',
  30651. ' };',
  30652. '});',
  30653. 'this.GetIt = function (i) {',
  30654. ' var Result = 0;',
  30655. ' var v3 = undefined;',
  30656. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  30657. ' return Result;',
  30658. '};',
  30659. 'this.V = undefined;',
  30660. 'this.o = null;',
  30661. '']),
  30662. LinesToStr([ // $mod.$main
  30663. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  30664. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  30665. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  30666. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  30667. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  30668. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  30669. '']));
  30670. end;
  30671. procedure TTestModule.TestJSValue_ProcType_Param;
  30672. begin
  30673. StartProgram(false);
  30674. Add([
  30675. 'type',
  30676. ' variant = jsvalue;',
  30677. ' TArrVariant = array of variant;',
  30678. ' TArrVar2 = TArrVariant;',
  30679. ' TFuncInt = function: longint;',
  30680. 'function GetIt: longint;',
  30681. 'begin',
  30682. 'end;',
  30683. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  30684. 'var v: variant;',
  30685. 'begin',
  30686. ' v:=arr[1];',
  30687. 'end;',
  30688. 'var s: string;',
  30689. 'begin',
  30690. ' DoIt(GetIt,[]);',
  30691. ' DoIt(@GetIt,[]);',
  30692. ' DoIt(1,[s,GetIt]);',
  30693. ' DoIt(1,[s,@GetIt]);',
  30694. '']);
  30695. ConvertProgram;
  30696. CheckSource('TestJSValue_ProcType_Param',
  30697. LinesToStr([ // statements
  30698. 'this.GetIt = function () {',
  30699. ' var Result = 0;',
  30700. ' return Result;',
  30701. '};',
  30702. 'this.DoIt = function (p, Arr) {',
  30703. ' var v = undefined;',
  30704. ' v = Arr[1];',
  30705. '};',
  30706. 'this.s = "";',
  30707. '']),
  30708. LinesToStr([ // $mod.$main
  30709. '$mod.DoIt($mod.GetIt(), []);',
  30710. '$mod.DoIt($mod.GetIt, []);',
  30711. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  30712. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  30713. '']));
  30714. end;
  30715. procedure TTestModule.TestJSValue_AssignToPointerFail;
  30716. begin
  30717. StartProgram(false);
  30718. Add([
  30719. 'var',
  30720. ' v: JSValue;',
  30721. ' p: Pointer;',
  30722. 'begin',
  30723. ' p:=v;',
  30724. '']);
  30725. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  30726. nIncompatibleTypesGotExpected);
  30727. ConvertProgram;
  30728. end;
  30729. procedure TTestModule.TestJSValue_OverloadDouble;
  30730. begin
  30731. StartProgram(false);
  30732. Add([
  30733. 'type',
  30734. ' integer = longint;',
  30735. ' tdatetime = double;',
  30736. 'procedure DoIt(d: double); begin end;',
  30737. 'procedure DoIt(v: jsvalue); begin end;',
  30738. 'var',
  30739. ' d: double;',
  30740. ' dt: tdatetime;',
  30741. ' i: integer;',
  30742. ' b: byte;',
  30743. ' shi: shortint;',
  30744. ' w: word;',
  30745. ' smi: smallint;',
  30746. ' lw: longword;',
  30747. ' li: longint;',
  30748. ' ni: nativeint;',
  30749. ' nu: nativeuint;',
  30750. 'begin',
  30751. ' DoIt(d);',
  30752. ' DoIt(dt);',
  30753. ' DoIt(i);',
  30754. ' DoIt(b);',
  30755. ' DoIt(shi);',
  30756. ' DoIt(w);',
  30757. ' DoIt(smi);',
  30758. ' DoIt(lw);',
  30759. ' DoIt(li);',
  30760. ' DoIt(ni);',
  30761. ' DoIt(nu);',
  30762. '']);
  30763. ConvertProgram;
  30764. CheckSource('TestJSValue_OverloadDouble',
  30765. LinesToStr([ // statements
  30766. 'this.DoIt = function (d) {',
  30767. '};',
  30768. 'this.DoIt$1 = function (v) {',
  30769. '};',
  30770. 'this.d = 0.0;',
  30771. 'this.dt = 0.0;',
  30772. 'this.i = 0;',
  30773. 'this.b = 0;',
  30774. 'this.shi = 0;',
  30775. 'this.w = 0;',
  30776. 'this.smi = 0;',
  30777. 'this.lw = 0;',
  30778. 'this.li = 0;',
  30779. 'this.ni = 0;',
  30780. 'this.nu = 0;',
  30781. '']),
  30782. LinesToStr([ // $mod.$main
  30783. '$mod.DoIt($mod.d);',
  30784. '$mod.DoIt($mod.dt);',
  30785. '$mod.DoIt$1($mod.i);',
  30786. '$mod.DoIt$1($mod.b);',
  30787. '$mod.DoIt$1($mod.shi);',
  30788. '$mod.DoIt$1($mod.w);',
  30789. '$mod.DoIt$1($mod.smi);',
  30790. '$mod.DoIt$1($mod.lw);',
  30791. '$mod.DoIt$1($mod.li);',
  30792. '$mod.DoIt$1($mod.ni);',
  30793. '$mod.DoIt$1($mod.nu);',
  30794. '']));
  30795. end;
  30796. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30797. begin
  30798. StartProgram(false);
  30799. Add([
  30800. 'type',
  30801. ' integer = longint;',
  30802. ' int53 = nativeint;',
  30803. ' tdatetime = double;',
  30804. 'procedure DoIt(n: nativeint); begin end;',
  30805. 'procedure DoIt(v: jsvalue); begin end;',
  30806. 'var',
  30807. ' d: double;',
  30808. ' dt: tdatetime;',
  30809. ' i: integer;',
  30810. ' b: byte;',
  30811. ' shi: shortint;',
  30812. ' w: word;',
  30813. ' smi: smallint;',
  30814. ' lw: longword;',
  30815. ' li: longint;',
  30816. ' ni: nativeint;',
  30817. ' nu: nativeuint;',
  30818. 'begin',
  30819. ' DoIt(d);',
  30820. ' DoIt(dt);',
  30821. ' DoIt(i);',
  30822. ' DoIt(b);',
  30823. ' DoIt(shi);',
  30824. ' DoIt(w);',
  30825. ' DoIt(smi);',
  30826. ' DoIt(lw);',
  30827. ' DoIt(li);',
  30828. ' DoIt(ni);',
  30829. ' DoIt(nu);',
  30830. '']);
  30831. ConvertProgram;
  30832. CheckSource('TestJSValue_OverloadNativeInt',
  30833. LinesToStr([ // statements
  30834. 'this.DoIt = function (n) {',
  30835. '};',
  30836. 'this.DoIt$1 = function (v) {',
  30837. '};',
  30838. 'this.d = 0.0;',
  30839. 'this.dt = 0.0;',
  30840. 'this.i = 0;',
  30841. 'this.b = 0;',
  30842. 'this.shi = 0;',
  30843. 'this.w = 0;',
  30844. 'this.smi = 0;',
  30845. 'this.lw = 0;',
  30846. 'this.li = 0;',
  30847. 'this.ni = 0;',
  30848. 'this.nu = 0;',
  30849. '']),
  30850. LinesToStr([ // $mod.$main
  30851. '$mod.DoIt$1($mod.d);',
  30852. '$mod.DoIt$1($mod.dt);',
  30853. '$mod.DoIt($mod.i);',
  30854. '$mod.DoIt($mod.b);',
  30855. '$mod.DoIt($mod.shi);',
  30856. '$mod.DoIt($mod.w);',
  30857. '$mod.DoIt($mod.smi);',
  30858. '$mod.DoIt($mod.lw);',
  30859. '$mod.DoIt($mod.li);',
  30860. '$mod.DoIt($mod.ni);',
  30861. '$mod.DoIt($mod.nu);',
  30862. '']));
  30863. end;
  30864. procedure TTestModule.TestJSValue_OverloadWord;
  30865. begin
  30866. StartProgram(false);
  30867. Add([
  30868. 'type',
  30869. ' integer = longint;',
  30870. ' int53 = nativeint;',
  30871. ' tdatetime = double;',
  30872. 'procedure DoIt(w: word); begin end;',
  30873. 'procedure DoIt(v: jsvalue); begin end;',
  30874. 'var',
  30875. ' d: double;',
  30876. ' dt: tdatetime;',
  30877. ' i: integer;',
  30878. ' b: byte;',
  30879. ' shi: shortint;',
  30880. ' w: word;',
  30881. ' smi: smallint;',
  30882. ' lw: longword;',
  30883. ' li: longint;',
  30884. ' ni: nativeint;',
  30885. ' nu: nativeuint;',
  30886. 'begin',
  30887. ' DoIt(d);',
  30888. ' DoIt(dt);',
  30889. ' DoIt(i);',
  30890. ' DoIt(b);',
  30891. ' DoIt(shi);',
  30892. ' DoIt(w);',
  30893. ' DoIt(smi);',
  30894. ' DoIt(lw);',
  30895. ' DoIt(li);',
  30896. ' DoIt(ni);',
  30897. ' DoIt(nu);',
  30898. '']);
  30899. ConvertProgram;
  30900. CheckSource('TestJSValue_OverloadWord',
  30901. LinesToStr([ // statements
  30902. 'this.DoIt = function (w) {',
  30903. '};',
  30904. 'this.DoIt$1 = function (v) {',
  30905. '};',
  30906. 'this.d = 0.0;',
  30907. 'this.dt = 0.0;',
  30908. 'this.i = 0;',
  30909. 'this.b = 0;',
  30910. 'this.shi = 0;',
  30911. 'this.w = 0;',
  30912. 'this.smi = 0;',
  30913. 'this.lw = 0;',
  30914. 'this.li = 0;',
  30915. 'this.ni = 0;',
  30916. 'this.nu = 0;',
  30917. '']),
  30918. LinesToStr([ // $mod.$main
  30919. '$mod.DoIt$1($mod.d);',
  30920. '$mod.DoIt$1($mod.dt);',
  30921. '$mod.DoIt$1($mod.i);',
  30922. '$mod.DoIt($mod.b);',
  30923. '$mod.DoIt($mod.shi);',
  30924. '$mod.DoIt($mod.w);',
  30925. '$mod.DoIt$1($mod.smi);',
  30926. '$mod.DoIt$1($mod.lw);',
  30927. '$mod.DoIt$1($mod.li);',
  30928. '$mod.DoIt$1($mod.ni);',
  30929. '$mod.DoIt$1($mod.nu);',
  30930. '']));
  30931. end;
  30932. procedure TTestModule.TestJSValue_OverloadString;
  30933. begin
  30934. StartProgram(false);
  30935. Add([
  30936. 'type',
  30937. ' uni = string;',
  30938. ' WChar = char;',
  30939. 'procedure DoIt(s: string); begin end;',
  30940. 'procedure DoIt(v: jsvalue); begin end;',
  30941. 'var',
  30942. ' s: string;',
  30943. ' c: char;',
  30944. ' u: uni;',
  30945. 'begin',
  30946. ' DoIt(s);',
  30947. ' DoIt(c);',
  30948. ' DoIt(u);',
  30949. '']);
  30950. ConvertProgram;
  30951. CheckSource('TestJSValue_OverloadString',
  30952. LinesToStr([ // statements
  30953. 'this.DoIt = function (s) {',
  30954. '};',
  30955. 'this.DoIt$1 = function (v) {',
  30956. '};',
  30957. 'this.s = "";',
  30958. 'this.c = "\x00";',
  30959. 'this.u = "";',
  30960. '']),
  30961. LinesToStr([ // $mod.$main
  30962. '$mod.DoIt($mod.s);',
  30963. '$mod.DoIt($mod.c);',
  30964. '$mod.DoIt($mod.u);',
  30965. '']));
  30966. end;
  30967. procedure TTestModule.TestJSValue_OverloadChar;
  30968. begin
  30969. StartProgram(false);
  30970. Add([
  30971. 'type',
  30972. ' uni = string;',
  30973. ' WChar = char;',
  30974. 'procedure DoIt(c: char); begin end;',
  30975. 'procedure DoIt(v: jsvalue); begin end;',
  30976. 'var',
  30977. ' s: string;',
  30978. ' c: char;',
  30979. ' u: uni;',
  30980. 'begin',
  30981. ' DoIt(s);',
  30982. ' DoIt(c);',
  30983. ' DoIt(u);',
  30984. '']);
  30985. ConvertProgram;
  30986. CheckSource('TestJSValue_OverloadChar',
  30987. LinesToStr([ // statements
  30988. 'this.DoIt = function (c) {',
  30989. '};',
  30990. 'this.DoIt$1 = function (v) {',
  30991. '};',
  30992. 'this.s = "";',
  30993. 'this.c = "\x00";',
  30994. 'this.u = "";',
  30995. '']),
  30996. LinesToStr([ // $mod.$main
  30997. '$mod.DoIt$1($mod.s);',
  30998. '$mod.DoIt($mod.c);',
  30999. '$mod.DoIt$1($mod.u);',
  31000. '']));
  31001. end;
  31002. procedure TTestModule.TestJSValue_OverloadPointer;
  31003. begin
  31004. StartProgram(false);
  31005. Add([
  31006. 'type',
  31007. ' TObject = class end;',
  31008. 'procedure DoIt(p: pointer); begin end;',
  31009. 'procedure DoIt(v: jsvalue); begin end;',
  31010. 'var',
  31011. ' o: TObject;',
  31012. 'begin',
  31013. ' DoIt(o);',
  31014. '']);
  31015. ConvertProgram;
  31016. CheckSource('TestJSValue_OverloadPointer',
  31017. LinesToStr([ // statements
  31018. 'rtl.createClass(this, "TObject", null, function () {',
  31019. ' this.$init = function () {',
  31020. ' };',
  31021. ' this.$final = function () {',
  31022. ' };',
  31023. '});',
  31024. 'this.DoIt = function (p) {',
  31025. '};',
  31026. 'this.DoIt$1 = function (v) {',
  31027. '};',
  31028. 'this.o = null;',
  31029. '']),
  31030. LinesToStr([ // $mod.$main
  31031. '$mod.DoIt($mod.o);',
  31032. '']));
  31033. end;
  31034. procedure TTestModule.TestJSValue_ForIn;
  31035. begin
  31036. StartProgram(false);
  31037. Add([
  31038. 'var',
  31039. ' v: JSValue;',
  31040. ' key: string;',
  31041. 'begin',
  31042. ' for key in v do begin',
  31043. ' if key=''abc'' then ;',
  31044. ' end;',
  31045. '']);
  31046. ConvertProgram;
  31047. CheckSource('TestJSValue_ForIn',
  31048. LinesToStr([ // statements
  31049. 'this.v = undefined;',
  31050. 'this.key = "";',
  31051. '']),
  31052. LinesToStr([ // $mod.$main
  31053. 'for ($mod.key in $mod.v) {',
  31054. ' if ($mod.key === "abc") ;',
  31055. '};',
  31056. '']));
  31057. end;
  31058. procedure TTestModule.TestRTTI_IntRange;
  31059. begin
  31060. WithTypeInfo:=true;
  31061. StartProgram(true,[supTypeInfo]);
  31062. Add([
  31063. '{$modeswitch externalclass}',
  31064. 'type',
  31065. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  31066. ' TColor = type TGraphicsColor;',
  31067. 'var',
  31068. ' p: TTypeInfo;',
  31069. ' k: TTypeKind;',
  31070. 'begin',
  31071. ' p:=typeinfo(TGraphicsColor);',
  31072. ' p:=typeinfo(TColor);',
  31073. ' k:=GetTypeKind(TGraphicsColor);',
  31074. ' k:=GetTypeKind(TColor);',
  31075. '']);
  31076. ConvertProgram;
  31077. CheckSource('TestRTTI_IntRange',
  31078. LinesToStr([ // statements
  31079. 'this.$rtti.$Int("TGraphicsColor", {',
  31080. ' minvalue: -2147483648,',
  31081. ' maxvalue: 2147483647,',
  31082. ' ordtype: 4',
  31083. '});',
  31084. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  31085. 'this.p = null;',
  31086. 'this.k = 0;',
  31087. '']),
  31088. LinesToStr([ // $mod.$main
  31089. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  31090. '$mod.p = $mod.$rtti["TColor"];',
  31091. '$mod.k = 1;',
  31092. '$mod.k = 1;',
  31093. '']));
  31094. end;
  31095. procedure TTestModule.TestRTTI_Double;
  31096. begin
  31097. WithTypeInfo:=true;
  31098. StartProgram(true,[supTypeInfo]);
  31099. Add([
  31100. '{$modeswitch externalclass}',
  31101. 'type',
  31102. ' TFloat = type double;',
  31103. 'var',
  31104. ' p: TTypeInfo;',
  31105. 'begin',
  31106. ' p:=typeinfo(double);',
  31107. ' p:=typeinfo(TFloat);',
  31108. '']);
  31109. ConvertProgram;
  31110. CheckSource('TestRTTI_Double',
  31111. LinesToStr([ // statements
  31112. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  31113. 'this.p = null;',
  31114. '']),
  31115. LinesToStr([ // $mod.$main
  31116. '$mod.p = rtl.double;',
  31117. '$mod.p = $mod.$rtti["TFloat"];',
  31118. '']));
  31119. end;
  31120. procedure TTestModule.TestRTTI_ProcType;
  31121. begin
  31122. WithTypeInfo:=true;
  31123. StartProgram(false);
  31124. Add('type');
  31125. Add(' TProcA = procedure;');
  31126. Add(' TMethodB = procedure of object;');
  31127. Add(' TProcC = procedure; varargs;');
  31128. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  31129. Add(' TProcE = function: nativeint;');
  31130. Add(' TProcF = function(const p: TProcA): nativeuint;');
  31131. Add('var p: pointer;');
  31132. Add('begin');
  31133. Add(' p:=typeinfo(tproca);');
  31134. ConvertProgram;
  31135. CheckSource('TestRTTI_ProcType',
  31136. LinesToStr([ // statements
  31137. 'this.$rtti.$ProcVar("TProcA", {',
  31138. ' procsig: rtl.newTIProcSig([])',
  31139. '});',
  31140. 'this.$rtti.$MethodVar("TMethodB", {',
  31141. ' procsig: rtl.newTIProcSig([]),',
  31142. ' methodkind: 0',
  31143. '});',
  31144. 'this.$rtti.$ProcVar("TProcC", {',
  31145. ' procsig: rtl.newTIProcSig([], null, 2)',
  31146. '});',
  31147. 'this.$rtti.$ProcVar("TProcD", {',
  31148. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  31149. '});',
  31150. 'this.$rtti.$ProcVar("TProcE", {',
  31151. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  31152. '});',
  31153. 'this.$rtti.$ProcVar("TProcF", {',
  31154. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  31155. '});',
  31156. 'this.p = null;',
  31157. '']),
  31158. LinesToStr([ // $mod.$main
  31159. '$mod.p = $mod.$rtti["TProcA"];',
  31160. '']));
  31161. end;
  31162. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  31163. begin
  31164. WithTypeInfo:=true;
  31165. AddModuleWithIntfImplSrc('unit2.pas',
  31166. LinesToStr([
  31167. 'type',
  31168. ' TObject = class end;'
  31169. ]),
  31170. '');
  31171. StartUnit(true);
  31172. Add('interface');
  31173. Add('uses unit2;');
  31174. Add('type');
  31175. Add(' TProcA = function(o: tobject): tobject;');
  31176. Add('implementation');
  31177. Add('type');
  31178. Add(' TProcB = function(o: tobject): tobject;');
  31179. Add('var p: Pointer;');
  31180. Add('initialization');
  31181. Add(' p:=typeinfo(tproca);');
  31182. Add(' p:=typeinfo(tprocb);');
  31183. ConvertUnit;
  31184. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  31185. LinesToStr([ // statements
  31186. 'var $impl = $mod.$impl;',
  31187. 'this.$rtti.$ProcVar("TProcA", {',
  31188. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  31189. '});',
  31190. '']),
  31191. LinesToStr([ // this.$init
  31192. '$impl.p = $mod.$rtti["TProcA"];',
  31193. '$impl.p = $mod.$rtti["TProcB"];',
  31194. '']),
  31195. LinesToStr([ // implementation
  31196. '$mod.$rtti.$ProcVar("TProcB", {',
  31197. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  31198. '});',
  31199. '$impl.p = null;',
  31200. '']) );
  31201. end;
  31202. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  31203. begin
  31204. WithTypeInfo:=true;
  31205. StartProgram(false);
  31206. Add(['var',
  31207. ' ProcA: procedure;',
  31208. ' MethodB: procedure of object;',
  31209. ' ProcC: procedure; varargs;',
  31210. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  31211. ' ProcE: function: nativeint;',
  31212. ' p: pointer;',
  31213. 'begin',
  31214. ' p:=typeinfo(proca);']);
  31215. ConvertProgram;
  31216. CheckSource('TestRTTI_ProcTypeAnonymous',
  31217. LinesToStr([ // statements
  31218. 'this.$rtti.$ProcVar("ProcA$a", {',
  31219. ' procsig: rtl.newTIProcSig([])',
  31220. '});',
  31221. 'this.ProcA = null;',
  31222. 'this.$rtti.$MethodVar("MethodB$a", {',
  31223. ' procsig: rtl.newTIProcSig([]),',
  31224. ' methodkind: 0',
  31225. '});',
  31226. 'this.MethodB = null;',
  31227. 'this.$rtti.$ProcVar("ProcC$a", {',
  31228. ' procsig: rtl.newTIProcSig([], null, 2)',
  31229. '});',
  31230. 'this.ProcC = null;',
  31231. 'this.$rtti.$ProcVar("ProcD$a", {',
  31232. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  31233. '});',
  31234. 'this.ProcD = null;',
  31235. 'this.$rtti.$ProcVar("ProcE$a", {',
  31236. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  31237. '});',
  31238. 'this.ProcE = null;',
  31239. 'this.p = null;',
  31240. '']),
  31241. LinesToStr([ // $mod.$main
  31242. '$mod.p = $mod.$rtti["ProcA$a"];',
  31243. '']));
  31244. end;
  31245. procedure TTestModule.TestRTTI_EnumAndSetType;
  31246. begin
  31247. WithTypeInfo:=true;
  31248. StartProgram(false);
  31249. Add('type');
  31250. Add(' TFlag = (light,dark);');
  31251. Add(' TFlags = set of TFlag;');
  31252. Add(' TProc = function(f: TFlags): TFlag;');
  31253. Add('var p: pointer;');
  31254. Add('begin');
  31255. Add(' p:=typeinfo(tflag);');
  31256. Add(' p:=typeinfo(tflags);');
  31257. ConvertProgram;
  31258. CheckSource('TestRTTI_EnumAndType',
  31259. LinesToStr([ // statements
  31260. 'this.TFlag = {',
  31261. ' "0": "light",',
  31262. ' light: 0,',
  31263. ' "1": "dark",',
  31264. ' dark: 1',
  31265. '};',
  31266. 'this.$rtti.$Enum("TFlag", {',
  31267. ' minvalue: 0,',
  31268. ' maxvalue: 1,',
  31269. ' ordtype: 1,',
  31270. ' enumtype: this.TFlag',
  31271. '});',
  31272. 'this.$rtti.$Set("TFlags", {',
  31273. ' comptype: this.$rtti["TFlag"]',
  31274. '});',
  31275. 'this.$rtti.$ProcVar("TProc", {',
  31276. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  31277. '});',
  31278. 'this.p = null;',
  31279. '']),
  31280. LinesToStr([ // $mod.$main
  31281. '$mod.p = $mod.$rtti["TFlag"];',
  31282. '$mod.p = $mod.$rtti["TFlags"];',
  31283. '']));
  31284. end;
  31285. procedure TTestModule.TestRTTI_EnumRange;
  31286. begin
  31287. WithTypeInfo:=true;
  31288. StartProgram(false);
  31289. Add([
  31290. 'type',
  31291. ' TCol = (red,green,blue);',
  31292. ' TColRg = green..blue;',
  31293. ' TSetOfColRg = set of TColRg;',
  31294. 'var p: pointer;',
  31295. 'begin',
  31296. ' p:=typeinfo(tcolrg);',
  31297. ' p:=typeinfo(tsetofcolrg);',
  31298. '']);
  31299. ConvertProgram;
  31300. end;
  31301. procedure TTestModule.TestRTTI_AnonymousEnumType;
  31302. begin
  31303. WithTypeInfo:=true;
  31304. StartProgram(false);
  31305. Add('type');
  31306. Add(' TFlags = set of (red, green);');
  31307. Add('var');
  31308. Add(' f: TFlags;');
  31309. Add('begin');
  31310. Add(' Include(f,red);');
  31311. ConvertProgram;
  31312. CheckSource('TestRTTI_AnonymousEnumType',
  31313. LinesToStr([ // statements
  31314. 'this.TFlags$a = {',
  31315. ' "0": "red",',
  31316. ' red: 0,',
  31317. ' "1": "green",',
  31318. ' green: 1',
  31319. '};',
  31320. 'this.$rtti.$Enum("TFlags$a", {',
  31321. ' minvalue: 0,',
  31322. ' maxvalue: 1,',
  31323. ' ordtype: 1,',
  31324. ' enumtype: this.TFlags$a',
  31325. '});',
  31326. 'this.$rtti.$Set("TFlags", {',
  31327. ' comptype: this.$rtti["TFlags$a"]',
  31328. '});',
  31329. 'this.f = {};',
  31330. '']),
  31331. LinesToStr([
  31332. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  31333. '']));
  31334. end;
  31335. procedure TTestModule.TestRTTI_StaticArray;
  31336. begin
  31337. WithTypeInfo:=true;
  31338. StartProgram(false);
  31339. Add('type');
  31340. Add(' TFlag = (light,dark);');
  31341. Add(' TFlagNames = array[TFlag] of string;');
  31342. Add(' TBoolNames = array[boolean] of string;');
  31343. Add(' TByteArray = array[1..32768] of byte;');
  31344. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  31345. Add('var p: pointer;');
  31346. Add('begin');
  31347. Add(' p:=typeinfo(TFlagNames);');
  31348. Add(' p:=typeinfo(TBoolNames);');
  31349. ConvertProgram;
  31350. CheckSource('TestRTTI_StaticArray',
  31351. LinesToStr([ // statements
  31352. 'this.TFlag = {',
  31353. ' "0": "light",',
  31354. ' light: 0,',
  31355. ' "1": "dark",',
  31356. ' dark: 1',
  31357. '};',
  31358. 'this.$rtti.$Enum("TFlag", {',
  31359. ' minvalue: 0,',
  31360. ' maxvalue: 1,',
  31361. ' ordtype: 1,',
  31362. ' enumtype: this.TFlag',
  31363. '});',
  31364. 'this.$rtti.$StaticArray("TFlagNames", {',
  31365. ' dims: [2],',
  31366. ' eltype: rtl.string',
  31367. '});',
  31368. 'this.$rtti.$StaticArray("TBoolNames", {',
  31369. ' dims: [2],',
  31370. ' eltype: rtl.string',
  31371. '});',
  31372. 'this.$rtti.$StaticArray("TByteArray", {',
  31373. ' dims: [32768],',
  31374. ' eltype: rtl.byte',
  31375. '});',
  31376. 'this.$rtti.$ProcVar("TProc", {',
  31377. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  31378. '});',
  31379. 'this.p = null;',
  31380. '']),
  31381. LinesToStr([ // $mod.$main
  31382. '$mod.p = $mod.$rtti["TFlagNames"];',
  31383. '$mod.p = $mod.$rtti["TBoolNames"];',
  31384. '']));
  31385. end;
  31386. procedure TTestModule.TestRTTI_DynArray;
  31387. begin
  31388. WithTypeInfo:=true;
  31389. StartProgram(false);
  31390. Add('type');
  31391. Add(' TArrStr = array of string;');
  31392. Add(' TArr2Dim = array of tarrstr;');
  31393. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  31394. Add('var p: pointer;');
  31395. Add('begin');
  31396. Add(' p:=typeinfo(tarrstr);');
  31397. Add(' p:=typeinfo(tarr2dim);');
  31398. ConvertProgram;
  31399. CheckSource('TestRTTI_DynArray',
  31400. LinesToStr([ // statements
  31401. 'this.$rtti.$DynArray("TArrStr", {',
  31402. ' eltype: rtl.string',
  31403. '});',
  31404. 'this.$rtti.$DynArray("TArr2Dim", {',
  31405. ' eltype: this.$rtti["TArrStr"]',
  31406. '});',
  31407. 'this.$rtti.$ProcVar("TProc", {',
  31408. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  31409. '});',
  31410. 'this.p = null;',
  31411. '']),
  31412. LinesToStr([ // $mod.$main
  31413. '$mod.p = $mod.$rtti["TArrStr"];',
  31414. '$mod.p = $mod.$rtti["TArr2Dim"];',
  31415. '']));
  31416. end;
  31417. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  31418. begin
  31419. WithTypeInfo:=true;
  31420. StartProgram(false);
  31421. Add('type');
  31422. Add(' TArr = array of array of longint;');
  31423. Add('var a: TArr;');
  31424. Add('begin');
  31425. ConvertProgram;
  31426. CheckSource('TestRTTI_ArrayNestedAnonymous',
  31427. LinesToStr([ // statements
  31428. 'this.$rtti.$DynArray("TArr$a", {',
  31429. ' eltype: rtl.longint',
  31430. '});',
  31431. 'this.$rtti.$DynArray("TArr", {',
  31432. ' eltype: this.$rtti["TArr$a"]',
  31433. '});',
  31434. 'this.a = [];',
  31435. '']),
  31436. LinesToStr([ // $mod.$main
  31437. ]));
  31438. end;
  31439. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  31440. begin
  31441. WithTypeInfo:=true;
  31442. StartProgram(false);
  31443. Add('type');
  31444. Add(' TObject = class');
  31445. Add(' published');
  31446. Add(' procedure Proc; virtual; abstract;');
  31447. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  31448. Add(' end;');
  31449. Add('begin');
  31450. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  31451. nDuplicatePublishedMethodXAtY);
  31452. ConvertProgram;
  31453. end;
  31454. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  31455. begin
  31456. WithTypeInfo:=true;
  31457. StartUnit(false);
  31458. Add([
  31459. 'interface',
  31460. 'type',
  31461. ' TObject = class',
  31462. ' end;',
  31463. ' {$M+}',
  31464. ' TBird = class',
  31465. ' procedure Fly;',
  31466. ' end;',
  31467. ' {$M-}',
  31468. 'type',
  31469. ' TEagle = class(TBird)',
  31470. ' procedure Fly;',
  31471. ' end;',
  31472. 'implementation',
  31473. 'procedure TBird.Fly;',
  31474. 'begin',
  31475. 'end;',
  31476. 'procedure TEagle.Fly;',
  31477. 'begin',
  31478. 'end;',
  31479. '']);
  31480. ConvertUnit;
  31481. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  31482. LinesToStr([ // statements
  31483. 'rtl.createClass(this, "TObject", null, function () {',
  31484. ' this.$init = function () {',
  31485. ' };',
  31486. ' this.$final = function () {',
  31487. ' };',
  31488. '});',
  31489. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31490. ' this.Fly = function () {',
  31491. ' };',
  31492. ' var $r = this.$rtti;',
  31493. ' $r.addMethod("Fly", 0, [], 4);',
  31494. '});',
  31495. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  31496. ' this.Fly = function () {',
  31497. ' };',
  31498. ' var $r = this.$rtti;',
  31499. ' $r.addMethod("Fly", 0, [], 4);',
  31500. '});',
  31501. '']),
  31502. LinesToStr([ // $mod.$main
  31503. ]));
  31504. CheckResolverUnexpectedHints(true);
  31505. end;
  31506. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  31507. begin
  31508. WithTypeInfo:=true;
  31509. StartProgram(false);
  31510. Add('type');
  31511. Add(' TObject = class');
  31512. Add(' published');
  31513. Add(' procedure Proc; external name ''foo'';');
  31514. Add(' end;');
  31515. Add('begin');
  31516. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  31517. nPublishedNameMustMatchExternal);
  31518. ConvertProgram;
  31519. end;
  31520. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  31521. begin
  31522. WithTypeInfo:=true;
  31523. StartProgram(false);
  31524. Add('type');
  31525. Add(' TObject = class');
  31526. Add(' class var FA: longint;');
  31527. Add(' published');
  31528. Add(' class property A: longint read FA;');
  31529. Add(' end;');
  31530. Add('begin');
  31531. SetExpectedPasResolverError('Invalid published property modifier "class"',
  31532. nInvalidXModifierY);
  31533. ConvertProgram;
  31534. end;
  31535. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  31536. begin
  31537. WithTypeInfo:=true;
  31538. StartProgram(false);
  31539. Add('type');
  31540. Add(' TObject = class');
  31541. Add(' published');
  31542. Add(' class var FA: longint;');
  31543. Add(' end;');
  31544. Add('begin');
  31545. SetExpectedPasResolverError(sSymbolCannotBePublished,
  31546. nSymbolCannotBePublished);
  31547. ConvertProgram;
  31548. end;
  31549. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  31550. begin
  31551. WithTypeInfo:=true;
  31552. StartProgram(false);
  31553. Add('{$modeswitch externalclass}');
  31554. Add('type');
  31555. Add(' TObject = class');
  31556. Add(' published');
  31557. Add(' V: longint; external name ''foo'';');
  31558. Add(' end;');
  31559. Add('begin');
  31560. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  31561. nPublishedNameMustMatchExternal);
  31562. ConvertProgram;
  31563. end;
  31564. procedure TTestModule.TestRTTI_Class_Field;
  31565. begin
  31566. WithTypeInfo:=true;
  31567. StartProgram(false);
  31568. Add('{$modeswitch externalclass}');
  31569. Add('type');
  31570. Add(' TObject = class');
  31571. Add(' private');
  31572. Add(' FPropA: string;');
  31573. Add(' published');
  31574. Add(' VarLI: longint;');
  31575. Add(' VarC: char;');
  31576. Add(' VarS: string;');
  31577. Add(' VarD: double;');
  31578. Add(' VarB: boolean;');
  31579. Add(' VarLW: longword;');
  31580. Add(' VarSmI: smallint;');
  31581. Add(' VarW: word;');
  31582. Add(' VarShI: shortint;');
  31583. Add(' VarBy: byte;');
  31584. Add(' VarExt: longint external name ''VarExt'';');
  31585. Add(' ArrA, ArrB: array of byte;');
  31586. Add(' end;');
  31587. Add('var p: pointer;');
  31588. Add(' Obj: tobject;');
  31589. Add('begin');
  31590. Add(' p:=typeinfo(tobject);');
  31591. Add(' p:=typeinfo(p);');
  31592. Add(' p:=typeinfo(obj);');
  31593. ConvertProgram;
  31594. CheckSource('TestRTTI_Class_Field',
  31595. LinesToStr([ // statements
  31596. 'rtl.createClass(this, "TObject", null, function () {',
  31597. ' this.$init = function () {',
  31598. ' this.FPropA = "";',
  31599. ' this.VarLI = 0;',
  31600. ' this.VarC = "\x00";',
  31601. ' this.VarS = "";',
  31602. ' this.VarD = 0.0;',
  31603. ' this.VarB = false;',
  31604. ' this.VarLW = 0;',
  31605. ' this.VarSmI = 0;',
  31606. ' this.VarW = 0;',
  31607. ' this.VarShI = 0;',
  31608. ' this.VarBy = 0;',
  31609. ' this.ArrA = [];',
  31610. ' this.ArrB = [];',
  31611. ' };',
  31612. ' this.$final = function () {',
  31613. ' this.ArrA = undefined;',
  31614. ' this.ArrB = undefined;',
  31615. ' };',
  31616. ' var $r = this.$rtti;',
  31617. ' $r.addField("VarLI", rtl.longint, 4);',
  31618. ' $r.addField("VarC", rtl.char, 4);',
  31619. ' $r.addField("VarS", rtl.string, 4);',
  31620. ' $r.addField("VarD", rtl.double, 4);',
  31621. ' $r.addField("VarB", rtl.boolean, 4);',
  31622. ' $r.addField("VarLW", rtl.longword, 4);',
  31623. ' $r.addField("VarSmI", rtl.smallint, 4);',
  31624. ' $r.addField("VarW", rtl.word, 4);',
  31625. ' $r.addField("VarShI", rtl.shortint, 4);',
  31626. ' $r.addField("VarBy", rtl.byte, 4);',
  31627. ' $r.addField("VarExt", rtl.longint, 4);',
  31628. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  31629. ' eltype: rtl.byte',
  31630. ' });',
  31631. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"], 4);',
  31632. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"], 4);',
  31633. '});',
  31634. 'this.p = null;',
  31635. 'this.Obj = null;',
  31636. '']),
  31637. LinesToStr([ // $mod.$main
  31638. '$mod.p = $mod.$rtti["TObject"];',
  31639. '$mod.p = rtl.pointer;',
  31640. '$mod.p = $mod.Obj.$rtti;',
  31641. '']));
  31642. end;
  31643. procedure TTestModule.TestRTTI_Class_FieldPrivate;
  31644. begin
  31645. WithTypeInfo:=true;
  31646. StartProgram(false);
  31647. Add('type');
  31648. Add('{$RTTI explicit fields([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31649. Add(' TObject = class');
  31650. Add(' strict private');
  31651. Add(' A1: word;');
  31652. Add(' private');
  31653. Add(' A2: word;');
  31654. Add(' strict protected');
  31655. Add(' B1: word;');
  31656. Add(' protected');
  31657. Add(' B2, B3: word;');
  31658. Add(' public');
  31659. Add(' C: word;');
  31660. Add(' published');
  31661. Add(' D: word;');
  31662. Add(' end;');
  31663. Add('begin');
  31664. ConvertProgram;
  31665. CheckSource('TestRTTI_Class_FieldPrivate',
  31666. LinesToStr([ // statements
  31667. 'rtl.createClass(this, "TObject", null, function () {',
  31668. ' this.$init = function () {',
  31669. ' this.A1 = 0;',
  31670. ' this.A2 = 0;',
  31671. ' this.B1 = 0;',
  31672. ' this.B2 = 0;',
  31673. ' this.B3 = 0;',
  31674. ' this.C = 0;',
  31675. ' this.D = 0;',
  31676. ' };',
  31677. ' this.$final = function () {',
  31678. ' };',
  31679. ' var $r = this.$rtti;',
  31680. ' $r.addField("A1", rtl.word, 5);',
  31681. ' $r.addField("A2", rtl.word, 0);',
  31682. ' $r.addField("B1", rtl.word, 6);',
  31683. ' $r.addField("B2", rtl.word, 1);',
  31684. ' $r.addField("B3", rtl.word, 1);',
  31685. ' $r.addField("C", rtl.word);',
  31686. ' $r.addField("D", rtl.word, 3);',
  31687. '});',
  31688. '']),
  31689. LinesToStr([ // $mod.$main
  31690. '']));
  31691. end;
  31692. procedure TTestModule.TestRTTI_Class_Method;
  31693. begin
  31694. WithTypeInfo:=true;
  31695. StartProgram(false);
  31696. Add([
  31697. 'type',
  31698. ' TObject = class',
  31699. ' private',
  31700. ' procedure Internal; external name ''$intern'';',
  31701. ' published',
  31702. ' procedure Click; virtual; abstract;',
  31703. ' procedure Notify(Sender: TObject); virtual; abstract;',
  31704. ' function GetNotify: boolean; external name ''GetNotify'';',
  31705. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  31706. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  31707. ' end;',
  31708. 'begin']);
  31709. ConvertProgram;
  31710. CheckSource('TestRTTI_Class_Method',
  31711. LinesToStr([ // statements
  31712. 'rtl.createClass(this, "TObject", null, function () {',
  31713. ' this.$init = function () {',
  31714. ' };',
  31715. ' this.$final = function () {',
  31716. ' };',
  31717. ' var $r = this.$rtti;',
  31718. ' $r.addMethod("Click", 0, [], 4);',
  31719. ' $r.addMethod("Notify", 0, [["Sender", $r]], 4);',
  31720. ' $r.addMethod("GetNotify", 1, [], 4, rtl.boolean, 4);',
  31721. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], 4, null, 2);',
  31722. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], 4, rtl.word, 20);',
  31723. '});',
  31724. '']),
  31725. LinesToStr([ // $mod.$main
  31726. '']));
  31727. end;
  31728. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  31729. begin
  31730. WithTypeInfo:=true;
  31731. StartProgram(false);
  31732. Add('type');
  31733. Add(' TObject = class');
  31734. Add(' published');
  31735. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  31736. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  31737. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  31738. Add(' end;');
  31739. Add('begin');
  31740. ConvertProgram;
  31741. CheckSource('TestRTTI_Class_MethodOpenArray',
  31742. LinesToStr([ // statements
  31743. 'rtl.createClass(this, "TObject", null, function () {',
  31744. ' this.$init = function () {',
  31745. ' };',
  31746. ' this.$final = function () {',
  31747. ' };',
  31748. ' var $r = this.$rtti;',
  31749. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]], 4);',
  31750. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]], 4);',
  31751. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]], 4);',
  31752. '});',
  31753. '']),
  31754. LinesToStr([ // $mod.$main
  31755. '']));
  31756. end;
  31757. procedure TTestModule.TestRTTI_Class_MethodPrivate;
  31758. begin
  31759. WithTypeInfo:=true;
  31760. StartProgram(false);
  31761. Add('type');
  31762. Add('{$RTTI explicit methods([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31763. Add(' TObject = class');
  31764. Add(' private');
  31765. Add(' procedure PrivateProc(a: word); virtual; abstract;');
  31766. Add(' protected');
  31767. Add(' class function ProtectedFunc: word; virtual; abstract;');
  31768. Add(' public');
  31769. Add(' class procedure PublicProc; virtual; abstract;');
  31770. Add(' constructor Create;');
  31771. Add(' destructor Destroy;');
  31772. Add(' published');
  31773. Add(' function PublishedProc: word; virtual; abstract;');
  31774. Add(' end;');
  31775. Add('constructor TObject.Create;');
  31776. Add('begin');
  31777. Add('end;');
  31778. Add('destructor TObject.Destroy;');
  31779. Add('begin');
  31780. Add('end;');
  31781. Add('begin');
  31782. ConvertProgram;
  31783. CheckSource('TestRTTI_Class_MethodPrivate',
  31784. LinesToStr([ // statements
  31785. 'rtl.createClass(this, "TObject", null, function () {',
  31786. ' this.$init = function () {',
  31787. ' };',
  31788. ' this.$final = function () {',
  31789. ' };',
  31790. ' this.Create = function () {',
  31791. ' return this;',
  31792. ' };',
  31793. ' this.Destroy = function () {',
  31794. ' };',
  31795. ' var $r = this.$rtti;',
  31796. ' $r.addMethod("PrivateProc", 0, [["a", rtl.word]], 0);',
  31797. ' $r.addMethod("ProtectedFunc", 5, [], 1, rtl.word);',
  31798. ' $r.addMethod("PublicProc", 4, []);',
  31799. ' $r.addMethod("Create", 2, []);',
  31800. ' $r.addMethod("Destroy", 3, []);',
  31801. ' $r.addMethod("PublishedProc", 1, [], 3, rtl.word);',
  31802. '});',
  31803. '']),
  31804. LinesToStr([ // $mod.$main
  31805. '']));
  31806. end;
  31807. procedure TTestModule.TestRTTI_Class_Property;
  31808. begin
  31809. WithTypeInfo:=true;
  31810. StartProgram(false);
  31811. Add('{$modeswitch externalclass}');
  31812. Add('type');
  31813. Add(' TObject = class');
  31814. Add(' private');
  31815. Add(' FColor: longint;');
  31816. Add(' FColorStored: boolean;');
  31817. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  31818. Add(' function GetColor: longint; virtual; abstract;');
  31819. Add(' function GetColorStored: boolean; virtual; abstract;');
  31820. Add(' FExtSize: longint external name ''$extSize'';');
  31821. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  31822. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  31823. Add(' function GetExtSize: longint; external name ''$getSize'';');
  31824. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  31825. Add(' published');
  31826. Add(' property ColorA: longint read FColor;');
  31827. Add(' property ColorB: longint write FColor;');
  31828. Add(' property ColorC: longint read GetColor write SetColor;');
  31829. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  31830. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  31831. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  31832. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  31833. Add(' end;');
  31834. Add('begin');
  31835. ConvertProgram;
  31836. CheckSource('TestRTTI_Class_Property',
  31837. LinesToStr([ // statements
  31838. 'rtl.createClass(this, "TObject", null, function () {',
  31839. ' this.$init = function () {',
  31840. ' this.FColor = 0;',
  31841. ' this.FColorStored = false;',
  31842. ' };',
  31843. ' this.$final = function () {',
  31844. ' };',
  31845. ' var $r = this.$rtti;',
  31846. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  31847. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  31848. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  31849. ' $r.addProperty(',
  31850. ' "ColorD",',
  31851. ' 8,',
  31852. ' rtl.longint,',
  31853. ' "FColor",',
  31854. ' "FColor",',
  31855. ' 4,',
  31856. ' {',
  31857. ' stored: "FColorStored"',
  31858. ' }',
  31859. ' );',
  31860. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  31861. ' $r.addProperty(',
  31862. ' "ExtSizeB",',
  31863. ' 11,',
  31864. ' rtl.longint,',
  31865. ' "$getSize",',
  31866. ' "$setSize",',
  31867. ' 4,',
  31868. ' {',
  31869. ' stored: "$extSizeStored"',
  31870. ' }',
  31871. ' );',
  31872. ' $r.addProperty(',
  31873. ' "ExtSizeC",',
  31874. ' 12,',
  31875. ' rtl.longint,',
  31876. ' "$extSize",',
  31877. ' "$extSize",',
  31878. ' 4,',
  31879. ' {',
  31880. ' stored: "$getExtSizeStored"',
  31881. ' }',
  31882. ' );',
  31883. '});',
  31884. '']),
  31885. LinesToStr([ // $mod.$main
  31886. '']));
  31887. end;
  31888. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31889. begin
  31890. WithTypeInfo:=true;
  31891. StartProgram(false);
  31892. Add('{$modeswitch externalclass}');
  31893. Add('type');
  31894. Add(' integer = longint;');
  31895. Add(' TObject = class');
  31896. Add(' private');
  31897. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31898. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31899. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31900. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31901. Add(' published');
  31902. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31903. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31904. Add(' end;');
  31905. Add('begin');
  31906. ConvertProgram;
  31907. CheckSource('TestRTTI_Class_PropertyParams',
  31908. LinesToStr([ // statements
  31909. 'rtl.createClass(this, "TObject", null, function () {',
  31910. ' this.$init = function () {',
  31911. ' };',
  31912. ' this.$final = function () {',
  31913. ' };',
  31914. ' var $r = this.$rtti;',
  31915. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31916. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31917. '});',
  31918. '']),
  31919. LinesToStr([ // $mod.$main
  31920. '']));
  31921. end;
  31922. procedure TTestModule.TestRTTI_Class_PropertyPrivate;
  31923. begin
  31924. WithTypeInfo:=true;
  31925. StartProgram(false);
  31926. Add('type');
  31927. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31928. Add(' TObject = class');
  31929. Add(' private');
  31930. Add(' FWord: word;');
  31931. Add(' function GetWord: word; virtual; abstract;');
  31932. Add(' procedure SetWord(Value: word); virtual; abstract;');
  31933. Add(' property PrivateWord: word read FWord write FWord;');
  31934. Add(' protected');
  31935. Add(' property ProtectedWord: word read FWord write SetWord;');
  31936. Add(' public');
  31937. Add(' property PublicWord: word read GetWord;');
  31938. Add(' published');
  31939. Add(' property PublishedWord: word read FWord;');
  31940. Add(' end;');
  31941. Add('begin');
  31942. ConvertProgram;
  31943. CheckSource('TestRTTI_Class_PropertyPrivate',
  31944. LinesToStr([ // statements
  31945. 'rtl.createClass(this, "TObject", null, function () {',
  31946. ' this.$init = function () {',
  31947. ' this.FWord = 0;',
  31948. ' };',
  31949. ' this.$final = function () {',
  31950. ' };',
  31951. ' var $r = this.$rtti;',
  31952. ' $r.addProperty(',
  31953. ' "PrivateWord",',
  31954. ' 0,',
  31955. ' rtl.word,',
  31956. ' "FWord",',
  31957. ' "FWord",',
  31958. ' 0',
  31959. ' );',
  31960. ' $r.addProperty(',
  31961. ' "ProtectedWord",',
  31962. ' 2,',
  31963. ' rtl.word,',
  31964. ' "FWord",',
  31965. ' "SetWord",',
  31966. ' 1',
  31967. ' );',
  31968. ' $r.addProperty("PublicWord", 1, rtl.word, "GetWord", "", 2);',
  31969. ' $r.addProperty(',
  31970. ' "PublishedWord",',
  31971. ' 0,',
  31972. ' rtl.word,',
  31973. ' "FWord",',
  31974. ' "",',
  31975. ' 3',
  31976. ' );',
  31977. '});',
  31978. '']),
  31979. LinesToStr([ // $mod.$main
  31980. '']));
  31981. end;
  31982. procedure TTestModule.TestRTTI_Class_ClassProperty;
  31983. begin
  31984. WithTypeInfo:=true;
  31985. StartProgram(false);
  31986. Add('type');
  31987. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31988. Add(' TObject = class');
  31989. Add(' private');
  31990. Add(' class var FWord: word;');
  31991. Add(' class function GetWord: word; virtual; abstract;');
  31992. Add(' class procedure SetWord(Value: word); virtual; abstract;');
  31993. Add(' class property PrivateWord: word read FWord write FWord;');
  31994. Add(' protected');
  31995. Add(' class property ProtectedWord: word read FWord write SetWord;');
  31996. Add(' public');
  31997. Add(' class property PublicWord: word read GetWord;');
  31998. Add(' end;');
  31999. Add('begin');
  32000. ConvertProgram;
  32001. CheckSource('TestRTTI_Class_ClassProperty',
  32002. LinesToStr([ // statements
  32003. 'rtl.createClass(this, "TObject", null, function () {',
  32004. ' this.FWord = 0;',
  32005. ' this.$init = function () {',
  32006. ' };',
  32007. ' this.$final = function () {',
  32008. ' };',
  32009. ' var $r = this.$rtti;',
  32010. ' $r.addProperty(',
  32011. ' "PrivateWord",',
  32012. ' 32,',
  32013. ' rtl.word,',
  32014. ' "FWord",',
  32015. ' "FWord",',
  32016. ' 0',
  32017. ' );',
  32018. ' $r.addProperty(',
  32019. ' "ProtectedWord",',
  32020. ' 34,',
  32021. ' rtl.word,',
  32022. ' "FWord",',
  32023. ' "SetWord",',
  32024. ' 1',
  32025. ' );',
  32026. ' $r.addProperty(',
  32027. ' "PublicWord",',
  32028. ' 33,',
  32029. ' rtl.word,',
  32030. ' "GetWord",',
  32031. ' "",',
  32032. ' 2',
  32033. ' );',
  32034. '});',
  32035. '']),
  32036. LinesToStr([ // $mod.$main
  32037. '']));
  32038. end;
  32039. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  32040. begin
  32041. WithTypeInfo:=true;
  32042. AddModuleWithIntfImplSrc('unit1.pas',
  32043. 'type TColor = -5..5;',
  32044. '');
  32045. StartProgram(true);
  32046. Add([
  32047. 'uses unit1;',
  32048. 'type',
  32049. ' TColorAlias = TColor;',
  32050. ' TColorTypeAlias = type TColor;',
  32051. ' TObject = class',
  32052. ' private',
  32053. ' fColor: TColor;',
  32054. ' fAlias: TColorAlias;',
  32055. ' fTypeAlias: TColorTypeAlias;',
  32056. ' published',
  32057. ' property Color: TColor read fcolor;',
  32058. ' property Alias: TColorAlias read falias;',
  32059. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  32060. ' end;',
  32061. 'begin',
  32062. '']);
  32063. ConvertProgram;
  32064. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  32065. LinesToStr([ // statements
  32066. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  32067. 'rtl.createClass(this, "TObject", null, function () {',
  32068. ' this.$init = function () {',
  32069. ' this.fColor = 0;',
  32070. ' this.fAlias = 0;',
  32071. ' this.fTypeAlias = 0;',
  32072. ' };',
  32073. ' this.$final = function () {',
  32074. ' };',
  32075. ' var $r = this.$rtti;',
  32076. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  32077. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  32078. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  32079. '});',
  32080. '']),
  32081. LinesToStr([ // $mod.$main
  32082. '']));
  32083. end;
  32084. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  32085. begin
  32086. WithTypeInfo:=true;
  32087. StartProgram(false);
  32088. Add([
  32089. '{$modeswitch omitrtti}',
  32090. 'type',
  32091. ' TObject = class',
  32092. ' private',
  32093. ' FA: byte;',
  32094. ' published',
  32095. ' property A: byte read FA write FA;',
  32096. ' end;',
  32097. 'begin']);
  32098. ConvertProgram;
  32099. CheckSource('TestRTTI_Class_OmitRTTI',
  32100. LinesToStr([ // statements
  32101. 'rtl.createClass(this, "TObject", null, function () {',
  32102. ' this.$init = function () {',
  32103. ' this.FA = 0;',
  32104. ' };',
  32105. ' this.$final = function () {',
  32106. ' };',
  32107. '});',
  32108. '']),
  32109. LinesToStr([ // $mod.$main
  32110. '']));
  32111. end;
  32112. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  32113. begin
  32114. WithTypeInfo:=true;
  32115. StartUnit(true,[supTObject]);
  32116. Add([
  32117. 'interface',
  32118. 'type',
  32119. ' {$M+}',
  32120. ' TBird = class',
  32121. ' published',
  32122. ' Swarm: array of TBird;',
  32123. ' end;',
  32124. 'implementation',
  32125. '']);
  32126. ConvertUnit;
  32127. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  32128. LinesToStr([ // statements
  32129. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  32130. ' this.$init = function () {',
  32131. ' pas.system.TObject.$init.call(this);',
  32132. ' this.Swarm = [];',
  32133. ' };',
  32134. ' this.$final = function () {',
  32135. ' this.Swarm = undefined;',
  32136. ' pas.system.TObject.$final.call(this);',
  32137. ' };',
  32138. ' var $r = this.$rtti;',
  32139. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  32140. ' eltype: $r',
  32141. ' });',
  32142. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"], 4);',
  32143. '});',
  32144. '']),
  32145. LinesToStr([ // $mod.$main
  32146. '']));
  32147. end;
  32148. procedure TTestModule.TestRTTI_IndexModifier;
  32149. begin
  32150. WithTypeInfo:=true;
  32151. StartProgram(false);
  32152. Add([
  32153. 'type',
  32154. ' TEnum = (red, blue);',
  32155. ' TObject = class',
  32156. ' FB: boolean;',
  32157. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  32158. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  32159. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  32160. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  32161. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  32162. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  32163. ' published',
  32164. ' property B1: boolean index 1 read FB write SetIntBool;',
  32165. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  32166. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  32167. ' end;',
  32168. 'begin']);
  32169. ConvertProgram;
  32170. CheckSource('TestRTTI_IndexModifier',
  32171. LinesToStr([ // statements
  32172. 'this.TEnum = {',
  32173. ' "0": "red",',
  32174. ' red: 0,',
  32175. ' "1": "blue",',
  32176. ' blue: 1',
  32177. '};',
  32178. 'this.$rtti.$Enum("TEnum", {',
  32179. ' minvalue: 0,',
  32180. ' maxvalue: 1,',
  32181. ' ordtype: 1,',
  32182. ' enumtype: this.TEnum',
  32183. '});',
  32184. 'rtl.createClass(this, "TObject", null, function () {',
  32185. ' this.$init = function () {',
  32186. ' this.FB = false;',
  32187. ' };',
  32188. ' this.$final = function () {',
  32189. ' };',
  32190. ' var $r = this.$rtti;',
  32191. ' $r.addProperty(',
  32192. ' "B1",',
  32193. ' 18,',
  32194. ' rtl.boolean,',
  32195. ' "FB",',
  32196. ' "SetIntBool",',
  32197. ' 4,',
  32198. ' {',
  32199. ' index: 1',
  32200. ' }',
  32201. ' );',
  32202. ' $r.addProperty(',
  32203. ' "B2",',
  32204. ' 17,',
  32205. ' rtl.boolean,',
  32206. ' "GetEnumBool",',
  32207. ' "FB",',
  32208. ' 4,',
  32209. ' {',
  32210. ' index: $mod.TEnum.blue',
  32211. ' }',
  32212. ' );',
  32213. ' $r.addProperty(',
  32214. ' "I1",',
  32215. ' 19,',
  32216. ' rtl.boolean,',
  32217. ' "GetStrIntBool",',
  32218. ' "SetStrIntBool",',
  32219. ' 4,',
  32220. ' {',
  32221. ' index: 2',
  32222. ' }',
  32223. ' );',
  32224. '});',
  32225. '']),
  32226. LinesToStr([ // $mod.$main
  32227. '']));
  32228. end;
  32229. procedure TTestModule.TestRTTI_StoredModifier;
  32230. begin
  32231. WithTypeInfo:=true;
  32232. StartProgram(false);
  32233. Add([
  32234. 'const',
  32235. ' ConstB = true;',
  32236. 'type',
  32237. ' TObject = class',
  32238. ' private',
  32239. ' FB: boolean;',
  32240. ' function IsBStored: boolean; virtual; abstract;',
  32241. ' published',
  32242. ' property BoolA: boolean read FB stored true;',
  32243. ' property BoolB: boolean read FB stored false;',
  32244. ' property BoolC: boolean read FB stored FB;',
  32245. ' property BoolD: boolean read FB stored ConstB;',
  32246. ' property BoolE: boolean read FB stored IsBStored;',
  32247. ' end;',
  32248. 'begin']);
  32249. ConvertProgram;
  32250. CheckSource('TestRTTI_StoredModifier',
  32251. LinesToStr([ // statements
  32252. 'this.ConstB = true;',
  32253. 'rtl.createClass(this, "TObject", null, function () {',
  32254. ' this.$init = function () {',
  32255. ' this.FB = false;',
  32256. ' };',
  32257. ' this.$final = function () {',
  32258. ' };',
  32259. ' var $r = this.$rtti;',
  32260. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  32261. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  32262. ' $r.addProperty(',
  32263. ' "BoolC",',
  32264. ' 8,',
  32265. ' rtl.boolean,',
  32266. ' "FB",',
  32267. ' "",',
  32268. ' 4,',
  32269. ' {',
  32270. ' stored: "FB"',
  32271. ' }',
  32272. ' );',
  32273. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  32274. ' $r.addProperty(',
  32275. ' "BoolE",',
  32276. ' 12,',
  32277. ' rtl.boolean,',
  32278. ' "FB",',
  32279. ' "",',
  32280. ' 4,',
  32281. ' {',
  32282. ' stored: "IsBStored"',
  32283. ' }',
  32284. ' );',
  32285. '});',
  32286. '']),
  32287. LinesToStr([ // $mod.$main
  32288. '']));
  32289. end;
  32290. procedure TTestModule.TestRTTI_DefaultValue;
  32291. begin
  32292. WithTypeInfo:=true;
  32293. StartProgram(false);
  32294. Add([
  32295. 'type',
  32296. ' TEnum = (red, blue);',
  32297. 'const',
  32298. ' CB = true or false;',
  32299. ' CI = 1+2;',
  32300. 'type',
  32301. ' TObject = class',
  32302. ' FB: boolean;',
  32303. ' FI: longint;',
  32304. ' FE: TEnum;',
  32305. ' published',
  32306. ' property B1: boolean read FB default true;',
  32307. ' property B2: boolean read FB default CB;',
  32308. ' property B3: boolean read FB default test1.cb;',
  32309. ' property I1: longint read FI default 2;',
  32310. ' property I2: longint read FI default CI;',
  32311. ' property E1: TEnum read FE default red;',
  32312. ' property E2: TEnum read FE default TEnum.blue;',
  32313. ' end;',
  32314. 'begin']);
  32315. ConvertProgram;
  32316. CheckSource('TestRTTI_DefaultValue',
  32317. LinesToStr([ // statements
  32318. 'this.TEnum = {',
  32319. ' "0": "red",',
  32320. ' red: 0,',
  32321. ' "1": "blue",',
  32322. ' blue: 1',
  32323. '};',
  32324. 'this.$rtti.$Enum("TEnum", {',
  32325. ' minvalue: 0,',
  32326. ' maxvalue: 1,',
  32327. ' ordtype: 1,',
  32328. ' enumtype: this.TEnum',
  32329. '});',
  32330. 'this.CB = true || false;',
  32331. 'this.CI = 1 + 2;',
  32332. 'rtl.createClass(this, "TObject", null, function () {',
  32333. ' this.$init = function () {',
  32334. ' this.FB = false;',
  32335. ' this.FI = 0;',
  32336. ' this.FE = 0;',
  32337. ' };',
  32338. ' this.$final = function () {',
  32339. ' };',
  32340. ' var $r = this.$rtti;',
  32341. ' $r.addProperty(',
  32342. ' "B1",',
  32343. ' 0,',
  32344. ' rtl.boolean,',
  32345. ' "FB",',
  32346. ' "",',
  32347. ' 4,',
  32348. ' {',
  32349. ' Default: true',
  32350. ' }',
  32351. ' );',
  32352. ' $r.addProperty(',
  32353. ' "B2",',
  32354. ' 0,',
  32355. ' rtl.boolean,',
  32356. ' "FB",',
  32357. ' "",',
  32358. ' 4,',
  32359. ' {',
  32360. ' Default: true',
  32361. ' }',
  32362. ' );',
  32363. ' $r.addProperty(',
  32364. ' "B3",',
  32365. ' 0,',
  32366. ' rtl.boolean,',
  32367. ' "FB",',
  32368. ' "",',
  32369. ' 4,',
  32370. ' {',
  32371. ' Default: true',
  32372. ' }',
  32373. ' );',
  32374. ' $r.addProperty(',
  32375. ' "I1",',
  32376. ' 0,',
  32377. ' rtl.longint,',
  32378. ' "FI",',
  32379. ' "",',
  32380. ' 4,',
  32381. ' {',
  32382. ' Default: 2',
  32383. ' }',
  32384. ' );',
  32385. ' $r.addProperty(',
  32386. ' "I2",',
  32387. ' 0,',
  32388. ' rtl.longint,',
  32389. ' "FI",',
  32390. ' "",',
  32391. ' 4,',
  32392. ' {',
  32393. ' Default: 3',
  32394. ' }',
  32395. ' );',
  32396. ' $r.addProperty(',
  32397. ' "E1",',
  32398. ' 0,',
  32399. ' $mod.$rtti["TEnum"],',
  32400. ' "FE",',
  32401. ' "",',
  32402. ' 4,',
  32403. ' {',
  32404. ' Default: $mod.TEnum.red',
  32405. ' }',
  32406. ' );',
  32407. ' $r.addProperty(',
  32408. ' "E2",',
  32409. ' 0,',
  32410. ' $mod.$rtti["TEnum"],',
  32411. ' "FE",',
  32412. ' "",',
  32413. ' 4,',
  32414. ' {',
  32415. ' Default: $mod.TEnum.blue',
  32416. ' }',
  32417. ' );',
  32418. '});',
  32419. '']),
  32420. LinesToStr([ // $mod.$main
  32421. '']));
  32422. end;
  32423. procedure TTestModule.TestRTTI_DefaultValueSet;
  32424. begin
  32425. WithTypeInfo:=true;
  32426. StartProgram(false);
  32427. Add([
  32428. 'type',
  32429. ' TEnum = (red, blue);',
  32430. ' TSet = set of TEnum;',
  32431. 'const',
  32432. ' CSet = [red,blue];',
  32433. 'type',
  32434. ' TObject = class',
  32435. ' FSet: TSet;',
  32436. ' published',
  32437. ' property Set1: TSet read FSet default [];',
  32438. ' property Set2: TSet read FSet default [red];',
  32439. ' property Set3: TSet read FSet default [red,blue];',
  32440. ' property Set4: TSet read FSet default CSet;',
  32441. ' end;',
  32442. 'begin']);
  32443. ConvertProgram;
  32444. CheckSource('TestRTTI_DefaultValueSet',
  32445. LinesToStr([ // statements
  32446. 'this.TEnum = {',
  32447. ' "0": "red",',
  32448. ' red: 0,',
  32449. ' "1": "blue",',
  32450. ' blue: 1',
  32451. '};',
  32452. 'this.$rtti.$Enum("TEnum", {',
  32453. ' minvalue: 0,',
  32454. ' maxvalue: 1,',
  32455. ' ordtype: 1,',
  32456. ' enumtype: this.TEnum',
  32457. '});',
  32458. 'this.$rtti.$Set("TSet", {',
  32459. ' comptype: this.$rtti["TEnum"]',
  32460. '});',
  32461. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  32462. 'rtl.createClass(this, "TObject", null, function () {',
  32463. ' this.$init = function () {',
  32464. ' this.FSet = {};',
  32465. ' };',
  32466. ' this.$final = function () {',
  32467. ' this.FSet = undefined;',
  32468. ' };',
  32469. ' var $r = this.$rtti;',
  32470. ' $r.addProperty(',
  32471. ' "Set1",',
  32472. ' 0,',
  32473. ' $mod.$rtti["TSet"],',
  32474. ' "FSet",',
  32475. ' "",',
  32476. ' 4,',
  32477. ' {',
  32478. ' Default: {}',
  32479. ' }',
  32480. ' );',
  32481. ' $r.addProperty(',
  32482. ' "Set2",',
  32483. ' 0,',
  32484. ' $mod.$rtti["TSet"],',
  32485. ' "FSet",',
  32486. ' "",',
  32487. ' 4,',
  32488. ' {',
  32489. ' Default: rtl.createSet($mod.TEnum.red)',
  32490. ' }',
  32491. ' );',
  32492. ' $r.addProperty(',
  32493. ' "Set3",',
  32494. ' 0,',
  32495. ' $mod.$rtti["TSet"],',
  32496. ' "FSet",',
  32497. ' "",',
  32498. ' 4,',
  32499. ' {',
  32500. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  32501. ' }',
  32502. ' );',
  32503. ' $r.addProperty(',
  32504. ' "Set4",',
  32505. ' 0,',
  32506. ' $mod.$rtti["TSet"],',
  32507. ' "FSet",',
  32508. ' "",',
  32509. ' 4,',
  32510. ' {',
  32511. ' Default: $mod.CSet',
  32512. ' }',
  32513. ' );',
  32514. '});',
  32515. '']),
  32516. LinesToStr([ // $mod.$main
  32517. '']));
  32518. end;
  32519. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  32520. begin
  32521. WithTypeInfo:=true;
  32522. StartProgram(false);
  32523. Add([
  32524. 'type',
  32525. ' TRg = -1..1;',
  32526. 'const',
  32527. ' l = low(TRg);',
  32528. ' h = high(TRg);',
  32529. 'type',
  32530. ' TObject = class',
  32531. ' FV: TRg;',
  32532. ' published',
  32533. ' property V1: TRg read FV default -1;',
  32534. ' end;',
  32535. 'begin']);
  32536. ConvertProgram;
  32537. CheckSource('TestRTTI_DefaultValueRangeType',
  32538. LinesToStr([ // statements
  32539. 'this.$rtti.$Int("TRg", {',
  32540. ' minvalue: -1,',
  32541. ' maxvalue: 1,',
  32542. ' ordtype: 0',
  32543. '});',
  32544. 'this.l = -1;',
  32545. 'this.h = 1;',
  32546. 'rtl.createClass(this, "TObject", null, function () {',
  32547. ' this.$init = function () {',
  32548. ' this.FV = 0;',
  32549. ' };',
  32550. ' this.$final = function () {',
  32551. ' };',
  32552. ' var $r = this.$rtti;',
  32553. ' $r.addProperty(',
  32554. ' "V1",',
  32555. ' 0,',
  32556. ' $mod.$rtti["TRg"],',
  32557. ' "FV",',
  32558. ' "",',
  32559. ' 4,',
  32560. ' {',
  32561. ' Default: -1',
  32562. ' }',
  32563. ' );',
  32564. '});',
  32565. '']),
  32566. LinesToStr([ // $mod.$main
  32567. '']));
  32568. end;
  32569. procedure TTestModule.TestRTTI_DefaultValueInherit;
  32570. begin
  32571. WithTypeInfo:=true;
  32572. StartProgram(false);
  32573. Add([
  32574. 'type',
  32575. ' TObject = class',
  32576. ' FA, FB: byte;',
  32577. ' property A: byte read FA default 1;',
  32578. ' property B: byte read FB default 2;',
  32579. ' end;',
  32580. ' TBird = class',
  32581. ' published',
  32582. ' property A;',
  32583. ' property B nodefault;',
  32584. ' end;',
  32585. 'begin']);
  32586. ConvertProgram;
  32587. CheckSource('TestRTTI_DefaultValueInherit',
  32588. LinesToStr([ // statements
  32589. 'rtl.createClass(this, "TObject", null, function () {',
  32590. ' this.$init = function () {',
  32591. ' this.FA = 0;',
  32592. ' this.FB = 0;',
  32593. ' };',
  32594. ' this.$final = function () {',
  32595. ' };',
  32596. '});',
  32597. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32598. ' var $r = this.$rtti;',
  32599. ' $r.addProperty(',
  32600. ' "A",',
  32601. ' 0,',
  32602. ' rtl.byte,',
  32603. ' "FA",',
  32604. ' "",',
  32605. ' 4,',
  32606. ' {',
  32607. ' Default: 1',
  32608. ' }',
  32609. ' );',
  32610. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  32611. '});',
  32612. '']),
  32613. LinesToStr([ // $mod.$main
  32614. '']));
  32615. end;
  32616. procedure TTestModule.TestRTTI_OverrideMethod;
  32617. begin
  32618. WithTypeInfo:=true;
  32619. StartProgram(false);
  32620. Add('type');
  32621. Add(' TObject = class');
  32622. Add(' published');
  32623. Add(' procedure DoIt; virtual; abstract;');
  32624. Add(' end;');
  32625. Add(' TSky = class');
  32626. Add(' published');
  32627. Add(' procedure DoIt; override;');
  32628. Add(' end;');
  32629. Add('procedure TSky.DoIt; begin end;');
  32630. Add('begin');
  32631. ConvertProgram;
  32632. CheckSource('TestRTTI_OverrideMethod',
  32633. LinesToStr([ // statements
  32634. 'rtl.createClass(this, "TObject", null, function () {',
  32635. ' this.$init = function () {',
  32636. ' };',
  32637. ' this.$final = function () {',
  32638. ' };',
  32639. ' var $r = this.$rtti;',
  32640. ' $r.addMethod("DoIt", 0, [], 4);',
  32641. '});',
  32642. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32643. ' this.DoIt = function () {',
  32644. ' };',
  32645. '});',
  32646. '']),
  32647. LinesToStr([ // $mod.$main
  32648. '']));
  32649. end;
  32650. procedure TTestModule.TestRTTI_ReintroduceMethod;
  32651. begin
  32652. WithTypeInfo:=true;
  32653. StartProgram(false);
  32654. Add([
  32655. 'type',
  32656. ' TObject = class',
  32657. ' published',
  32658. ' procedure DoIt;',
  32659. ' end;',
  32660. ' TSky = class',
  32661. ' published',
  32662. ' procedure DoIt; reintroduce;',
  32663. ' end;',
  32664. 'procedure TObject.DoIt; begin end;',
  32665. 'procedure TSky.DoIt;',
  32666. 'begin',
  32667. ' inherited DoIt;',
  32668. 'end;',
  32669. 'begin']);
  32670. ConvertProgram;
  32671. CheckSource('TestRTTI_ReintroduceMethod',
  32672. LinesToStr([ // statements
  32673. 'rtl.createClass(this, "TObject", null, function () {',
  32674. ' this.$init = function () {',
  32675. ' };',
  32676. ' this.$final = function () {',
  32677. ' };',
  32678. ' this.DoIt = function () {',
  32679. ' };',
  32680. ' var $r = this.$rtti;',
  32681. ' $r.addMethod("DoIt", 0, [], 4);',
  32682. '});',
  32683. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32684. ' this.DoIt = function () {',
  32685. ' $mod.TObject.DoIt.call(this);',
  32686. ' };',
  32687. ' var $r = this.$rtti;',
  32688. ' $r.addMethod("DoIt", 0, [], 4);',
  32689. '});',
  32690. '']),
  32691. LinesToStr([ // $mod.$main
  32692. '']));
  32693. end;
  32694. procedure TTestModule.TestRTTI_OverloadProperty;
  32695. begin
  32696. WithTypeInfo:=true;
  32697. StartProgram(false);
  32698. Add('type');
  32699. Add(' TObject = class');
  32700. Add(' protected');
  32701. Add(' FFlag: longint;');
  32702. Add(' published');
  32703. Add(' property Flag: longint read fflag;');
  32704. Add(' end;');
  32705. Add(' TSky = class');
  32706. Add(' published');
  32707. Add(' property FLAG: longint write fflag;');
  32708. Add(' end;');
  32709. Add('begin');
  32710. ConvertProgram;
  32711. CheckSource('TestRTTI_OverrideMethod',
  32712. LinesToStr([ // statements
  32713. 'rtl.createClass(this, "TObject", null, function () {',
  32714. ' this.$init = function () {',
  32715. ' this.FFlag = 0;',
  32716. ' };',
  32717. ' this.$final = function () {',
  32718. ' };',
  32719. ' var $r = this.$rtti;',
  32720. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  32721. '});',
  32722. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32723. ' var $r = this.$rtti;',
  32724. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  32725. '});',
  32726. '']),
  32727. LinesToStr([ // $mod.$main
  32728. '']));
  32729. end;
  32730. procedure TTestModule.TestRTTI_ClassForward;
  32731. begin
  32732. WithTypeInfo:=true;
  32733. StartProgram(false);
  32734. Add('type');
  32735. Add(' TObject = class end;');
  32736. Add(' tbridge = class;');
  32737. Add(' TProc = function: tbridge;');
  32738. Add(' TOger = class');
  32739. Add(' published');
  32740. Add(' FBridge: tbridge;');
  32741. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  32742. Add(' property Bridge: tbridge read fbridge write setbridge;');
  32743. Add(' end;');
  32744. Add(' TBridge = class');
  32745. Add(' FOger: toger;');
  32746. Add(' end;');
  32747. Add('var p: Pointer;');
  32748. Add(' b: tbridge;');
  32749. Add('begin');
  32750. Add(' p:=typeinfo(tbridge);');
  32751. Add(' p:=typeinfo(b);');
  32752. ConvertProgram;
  32753. CheckSource('TestRTTI_ClassForward',
  32754. LinesToStr([ // statements
  32755. 'rtl.createClass(this, "TObject", null, function () {',
  32756. ' this.$init = function () {',
  32757. ' };',
  32758. ' this.$final = function () {',
  32759. ' };',
  32760. '});',
  32761. 'this.$rtti.$Class("TBridge");',
  32762. 'this.$rtti.$ProcVar("TProc", {',
  32763. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  32764. '});',
  32765. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  32766. ' this.$init = function () {',
  32767. ' $mod.TObject.$init.call(this);',
  32768. ' this.FBridge = null;',
  32769. ' };',
  32770. ' this.$final = function () {',
  32771. ' this.FBridge = undefined;',
  32772. ' $mod.TObject.$final.call(this);',
  32773. ' };',
  32774. ' var $r = this.$rtti;',
  32775. ' $r.addField("FBridge", $mod.$rtti["TBridge"], 4);',
  32776. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]], 4);',
  32777. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  32778. '});',
  32779. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  32780. ' this.$init = function () {',
  32781. ' $mod.TObject.$init.call(this);',
  32782. ' this.FOger = null;',
  32783. ' };',
  32784. ' this.$final = function () {',
  32785. ' this.FOger = undefined;',
  32786. ' $mod.TObject.$final.call(this);',
  32787. ' };',
  32788. '});',
  32789. 'this.p = null;',
  32790. 'this.b = null;',
  32791. '']),
  32792. LinesToStr([ // $mod.$main
  32793. '$mod.p = $mod.$rtti["TBridge"];',
  32794. '$mod.p = $mod.b.$rtti;',
  32795. '']));
  32796. end;
  32797. procedure TTestModule.TestRTTI_ClassOf;
  32798. begin
  32799. WithTypeInfo:=true;
  32800. StartProgram(false);
  32801. Add('type');
  32802. Add(' TClass = class of tobject;');
  32803. Add(' TProcA = function: TClass;');
  32804. Add(' TObject = class');
  32805. Add(' published');
  32806. Add(' C: tclass;');
  32807. Add(' end;');
  32808. Add(' tfox = class;');
  32809. Add(' TBird = class end;');
  32810. Add(' TBirds = class of tbird;');
  32811. Add(' TFox = class end;');
  32812. Add(' TFoxes = class of tfox;');
  32813. Add(' TCows = class of TCow;');
  32814. Add(' TCow = class;');
  32815. Add(' TCow = class end;');
  32816. Add('begin');
  32817. ConvertProgram;
  32818. CheckSource('TestRTTI_ClassOf',
  32819. LinesToStr([ // statements
  32820. 'this.$rtti.$Class("TObject");',
  32821. 'this.$rtti.$ClassRef("TClass", {',
  32822. ' instancetype: this.$rtti["TObject"]',
  32823. '});',
  32824. 'this.$rtti.$ProcVar("TProcA", {',
  32825. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  32826. '});',
  32827. 'rtl.createClass(this, "TObject", null, function () {',
  32828. ' this.$init = function () {',
  32829. ' this.C = null;',
  32830. ' };',
  32831. ' this.$final = function () {',
  32832. ' this.C = undefined;',
  32833. ' };',
  32834. ' var $r = this.$rtti;',
  32835. ' $r.addField("C", $mod.$rtti["TClass"], 4);',
  32836. '});',
  32837. 'this.$rtti.$Class("TFox");',
  32838. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32839. '});',
  32840. 'this.$rtti.$ClassRef("TBirds", {',
  32841. ' instancetype: this.$rtti["TBird"]',
  32842. '});',
  32843. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  32844. '});',
  32845. 'this.$rtti.$ClassRef("TFoxes", {',
  32846. ' instancetype: this.$rtti["TFox"]',
  32847. '});',
  32848. 'this.$rtti.$Class("TCow");',
  32849. 'this.$rtti.$ClassRef("TCows", {',
  32850. ' instancetype: this.$rtti["TCow"]',
  32851. '});',
  32852. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  32853. '});',
  32854. '']),
  32855. LinesToStr([ // $mod.$main
  32856. '']));
  32857. end;
  32858. procedure TTestModule.TestRTTI_Record;
  32859. begin
  32860. WithTypeInfo:=true;
  32861. StartProgram(false);
  32862. Add('type');
  32863. Add(' integer = longint;');
  32864. Add(' TPoint = record');
  32865. Add(' x,y: integer;');
  32866. Add(' end;');
  32867. Add('var p: pointer;');
  32868. Add(' r: tpoint;');
  32869. Add('begin');
  32870. Add(' p:=typeinfo(tpoint);');
  32871. Add(' p:=typeinfo(r);');
  32872. Add(' p:=typeinfo(r.x);');
  32873. ConvertProgram;
  32874. CheckSource('TestRTTI_Record',
  32875. LinesToStr([ // statements
  32876. 'rtl.recNewT(this, "TPoint", function () {',
  32877. ' this.x = 0;',
  32878. ' this.y = 0;',
  32879. ' this.$eq = function (b) {',
  32880. ' return (this.x === b.x) && (this.y === b.y);',
  32881. ' };',
  32882. ' this.$assign = function (s) {',
  32883. ' this.x = s.x;',
  32884. ' this.y = s.y;',
  32885. ' return this;',
  32886. ' };',
  32887. ' var $r = $mod.$rtti.$Record("TPoint", {}, this);',
  32888. ' $r.addField("x", rtl.longint);',
  32889. ' $r.addField("y", rtl.longint);',
  32890. '});',
  32891. 'this.p = null;',
  32892. 'this.r = this.TPoint.$new();',
  32893. '']),
  32894. LinesToStr([ // $mod.$main
  32895. '$mod.p = $mod.$rtti["TPoint"];',
  32896. '$mod.p = $mod.$rtti["TPoint"];',
  32897. '$mod.p = rtl.longint;',
  32898. '']));
  32899. end;
  32900. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  32901. begin
  32902. WithTypeInfo:=true;
  32903. StartProgram(false);
  32904. Add('type');
  32905. Add(' TFloatRec = record');
  32906. Add(' c,d: array of char;');
  32907. // Add(' i: array of array of longint;');
  32908. Add(' end;');
  32909. Add('var p: pointer;');
  32910. Add(' r: tfloatrec;');
  32911. Add('begin');
  32912. Add(' p:=typeinfo(tfloatrec);');
  32913. Add(' p:=typeinfo(r);');
  32914. Add(' p:=typeinfo(r.d);');
  32915. ConvertProgram;
  32916. CheckSource('TestRTTI_Record',
  32917. LinesToStr([ // statements
  32918. 'rtl.recNewT(this, "TFloatRec", function () {',
  32919. ' this.$new = function () {',
  32920. ' var r = Object.create(this);',
  32921. ' r.c = [];',
  32922. ' r.d = [];',
  32923. ' return r;',
  32924. ' };',
  32925. ' this.$eq = function (b) {',
  32926. ' return (this.c === b.c) && (this.d === b.d);',
  32927. ' };',
  32928. ' this.$assign = function (s) {',
  32929. ' this.c = rtl.arrayRef(s.c);',
  32930. ' this.d = rtl.arrayRef(s.d);',
  32931. ' return this;',
  32932. ' };',
  32933. ' var $r = $mod.$rtti.$Record("TFloatRec", {}, this);',
  32934. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  32935. ' eltype: rtl.char',
  32936. ' });',
  32937. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  32938. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  32939. '});',
  32940. 'this.p = null;',
  32941. 'this.r = this.TFloatRec.$new();',
  32942. '']),
  32943. LinesToStr([ // $mod.$main
  32944. '$mod.p = $mod.$rtti["TFloatRec"];',
  32945. '$mod.p = $mod.$rtti["TFloatRec"];',
  32946. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  32947. '']));
  32948. end;
  32949. procedure TTestModule.TestRTTI_Record_ClassVarType;
  32950. begin
  32951. WithTypeInfo:=true;
  32952. StartProgram(false);
  32953. Add([
  32954. '{$modeswitch AdvancedRecords}',
  32955. 'type',
  32956. ' TPoint = record',
  32957. ' type TProc = procedure(w: word);',
  32958. ' class var p: TProc;',
  32959. ' end;',
  32960. 'begin',
  32961. '']);
  32962. ConvertProgram;
  32963. CheckSource('TestRTTI_Record_ClassVarType',
  32964. LinesToStr([ // statements
  32965. 'rtl.recNewT(this, "TPoint", function () {',
  32966. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  32967. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  32968. ' });',
  32969. ' this.p = null;',
  32970. ' this.$eq = function (b) {',
  32971. ' return true;',
  32972. ' };',
  32973. ' this.$assign = function (s) {',
  32974. ' return this;',
  32975. ' };',
  32976. ' var $r = $mod.$rtti.$Record("TPoint", {}, this);',
  32977. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  32978. '}, true);',
  32979. '']),
  32980. LinesToStr([ // $mod.$main
  32981. '']));
  32982. end;
  32983. procedure TTestModule.TestRTTI_LocalTypes;
  32984. begin
  32985. WithTypeInfo:=true;
  32986. StartProgram(false);
  32987. Add([
  32988. 'procedure DoIt;',
  32989. 'type',
  32990. ' integer = longint;',
  32991. ' TPoint = record',
  32992. ' x,y: integer;',
  32993. ' end;',
  32994. 'var p: TPoint;',
  32995. 'begin',
  32996. 'end;',
  32997. 'begin']);
  32998. ConvertProgram;
  32999. CheckSource('TestRTTI_LocalTypes',
  33000. LinesToStr([ // statements
  33001. 'var TPoint = rtl.recNewT(null, "", function () {',
  33002. ' this.x = 0;',
  33003. ' this.y = 0;',
  33004. ' this.$eq = function (b) {',
  33005. ' return (this.x === b.x) && (this.y === b.y);',
  33006. ' };',
  33007. ' this.$assign = function (s) {',
  33008. ' this.x = s.x;',
  33009. ' this.y = s.y;',
  33010. ' return this;',
  33011. ' };',
  33012. '});',
  33013. 'this.DoIt = function () {',
  33014. ' var p = TPoint.$new();',
  33015. '};',
  33016. '']),
  33017. LinesToStr([ // $mod.$main
  33018. '']));
  33019. end;
  33020. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  33021. begin
  33022. WithTypeInfo:=true;
  33023. StartProgram(false);
  33024. Add([
  33025. 'type',
  33026. ' TCaption = string;',
  33027. ' TYesNo = boolean;',
  33028. ' TLetter = char;',
  33029. ' TFloat = double;',
  33030. ' TPtr = pointer;',
  33031. ' TShortInt = shortint;',
  33032. ' TByte = byte;',
  33033. ' TSmallInt = smallint;',
  33034. ' TWord = word;',
  33035. ' TInt32 = longint;',
  33036. ' TDWord = longword;',
  33037. ' TValue = jsvalue;',
  33038. 'var p: TPtr;',
  33039. 'begin',
  33040. ' p:=typeinfo(string);',
  33041. ' p:=typeinfo(tcaption);',
  33042. ' p:=typeinfo(boolean);',
  33043. ' p:=typeinfo(tyesno);',
  33044. ' p:=typeinfo(char);',
  33045. ' p:=typeinfo(tletter);',
  33046. ' p:=typeinfo(double);',
  33047. ' p:=typeinfo(tfloat);',
  33048. ' p:=typeinfo(pointer);',
  33049. ' p:=typeinfo(tptr);',
  33050. ' p:=typeinfo(shortint);',
  33051. ' p:=typeinfo(tshortint);',
  33052. ' p:=typeinfo(byte);',
  33053. ' p:=typeinfo(tbyte);',
  33054. ' p:=typeinfo(smallint);',
  33055. ' p:=typeinfo(tsmallint);',
  33056. ' p:=typeinfo(word);',
  33057. ' p:=typeinfo(tword);',
  33058. ' p:=typeinfo(longword);',
  33059. ' p:=typeinfo(tdword);',
  33060. ' p:=typeinfo(jsvalue);',
  33061. ' p:=typeinfo(tvalue);',
  33062. '']);
  33063. ConvertProgram;
  33064. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  33065. LinesToStr([ // statements
  33066. 'this.p = null;',
  33067. '']),
  33068. LinesToStr([ // $mod.$main
  33069. '$mod.p = rtl.string;',
  33070. '$mod.p = rtl.string;',
  33071. '$mod.p = rtl.boolean;',
  33072. '$mod.p = rtl.boolean;',
  33073. '$mod.p = rtl.char;',
  33074. '$mod.p = rtl.char;',
  33075. '$mod.p = rtl.double;',
  33076. '$mod.p = rtl.double;',
  33077. '$mod.p = rtl.pointer;',
  33078. '$mod.p = rtl.pointer;',
  33079. '$mod.p = rtl.shortint;',
  33080. '$mod.p = rtl.shortint;',
  33081. '$mod.p = rtl.byte;',
  33082. '$mod.p = rtl.byte;',
  33083. '$mod.p = rtl.smallint;',
  33084. '$mod.p = rtl.smallint;',
  33085. '$mod.p = rtl.word;',
  33086. '$mod.p = rtl.word;',
  33087. '$mod.p = rtl.longword;',
  33088. '$mod.p = rtl.longword;',
  33089. '$mod.p = rtl.jsvalue;',
  33090. '$mod.p = rtl.jsvalue;',
  33091. '']));
  33092. end;
  33093. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  33094. begin
  33095. WithTypeInfo:=true;
  33096. StartProgram(false);
  33097. Add([
  33098. 'type',
  33099. ' TCaption = type string;',
  33100. ' TYesNo = type boolean;',
  33101. ' TLetter = type char;',
  33102. ' TFloat = type double;',
  33103. ' TPtr = type pointer;',
  33104. ' TShortInt = type shortint;',
  33105. ' TByte = type byte;',
  33106. ' TSmallInt = type smallint;',
  33107. ' TWord = type word;',
  33108. ' TInt32 = type longint;',
  33109. ' TDWord = type longword;',
  33110. ' TValue = type jsvalue;',
  33111. ' TAliasValue = type TValue;',
  33112. 'var',
  33113. ' p: TPtr;',
  33114. ' a: TAliasValue;',
  33115. 'begin',
  33116. ' p:=typeinfo(tcaption);',
  33117. ' p:=typeinfo(tyesno);',
  33118. ' p:=typeinfo(tletter);',
  33119. ' p:=typeinfo(tfloat);',
  33120. ' p:=typeinfo(tptr);',
  33121. ' p:=typeinfo(tshortint);',
  33122. ' p:=typeinfo(tbyte);',
  33123. ' p:=typeinfo(tsmallint);',
  33124. ' p:=typeinfo(tword);',
  33125. ' p:=typeinfo(tdword);',
  33126. ' p:=typeinfo(tvalue);',
  33127. ' p:=typeinfo(taliasvalue);',
  33128. ' p:=typeinfo(a);',
  33129. '']);
  33130. ConvertProgram;
  33131. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  33132. LinesToStr([ // statements
  33133. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  33134. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  33135. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  33136. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  33137. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  33138. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  33139. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  33140. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  33141. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  33142. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  33143. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  33144. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  33145. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  33146. 'this.p = null;',
  33147. 'this.a = undefined;',
  33148. '']),
  33149. LinesToStr([ // $mod.$main
  33150. '$mod.p = $mod.$rtti["TCaption"];',
  33151. '$mod.p = $mod.$rtti["TYesNo"];',
  33152. '$mod.p = $mod.$rtti["TLetter"];',
  33153. '$mod.p = $mod.$rtti["TFloat"];',
  33154. '$mod.p = $mod.$rtti["TPtr"];',
  33155. '$mod.p = $mod.$rtti["TShortInt"];',
  33156. '$mod.p = $mod.$rtti["TByte"];',
  33157. '$mod.p = $mod.$rtti["TSmallInt"];',
  33158. '$mod.p = $mod.$rtti["TWord"];',
  33159. '$mod.p = $mod.$rtti["TDWord"];',
  33160. '$mod.p = $mod.$rtti["TValue"];',
  33161. '$mod.p = $mod.$rtti["TAliasValue"];',
  33162. '$mod.p = $mod.$rtti["TAliasValue"];',
  33163. '']));
  33164. end;
  33165. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  33166. begin
  33167. WithTypeInfo:=true;
  33168. StartProgram(false);
  33169. Add('procedure DoIt;');
  33170. Add('type');
  33171. Add(' integer = longint;');
  33172. Add(' TPoint = record');
  33173. Add(' x,y: integer;');
  33174. Add(' end;');
  33175. Add('var p: pointer;');
  33176. Add('begin');
  33177. Add(' p:=typeinfo(tpoint);');
  33178. Add('end;');
  33179. Add('begin');
  33180. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  33181. ConvertProgram;
  33182. end;
  33183. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  33184. begin
  33185. WithTypeInfo:=true;
  33186. StartProgram(true,[supTypeInfo]);
  33187. Add([
  33188. '{$modeswitch externalclass}',
  33189. 'type',
  33190. ' TFlag = (up,down);',
  33191. ' TFlags = set of TFlag;',
  33192. 'var',
  33193. ' ti: TTypeInfo;',
  33194. ' tiInt: TTypeInfoInteger;',
  33195. ' tiEnum: TTypeInfoEnum;',
  33196. ' tiSet: TTypeInfoSet;',
  33197. 'begin',
  33198. ' ti:=typeinfo(string);',
  33199. ' ti:=typeinfo(boolean);',
  33200. ' ti:=typeinfo(char);',
  33201. ' ti:=typeinfo(double);',
  33202. ' tiInt:=typeinfo(shortint);',
  33203. ' tiInt:=typeinfo(byte);',
  33204. ' tiInt:=typeinfo(smallint);',
  33205. ' tiInt:=typeinfo(word);',
  33206. ' tiInt:=typeinfo(longint);',
  33207. ' tiInt:=typeinfo(longword);',
  33208. ' ti:=typeinfo(jsvalue);',
  33209. ' tiEnum:=typeinfo(tflag);',
  33210. ' tiSet:=typeinfo(tflags);']);
  33211. ConvertProgram;
  33212. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  33213. LinesToStr([ // statements
  33214. 'this.TFlag = {',
  33215. ' "0": "up",',
  33216. ' up: 0,',
  33217. ' "1": "down",',
  33218. ' down: 1',
  33219. '};',
  33220. 'this.$rtti.$Enum("TFlag", {',
  33221. ' minvalue: 0,',
  33222. ' maxvalue: 1,',
  33223. ' ordtype: 1,',
  33224. ' enumtype: this.TFlag',
  33225. '});',
  33226. 'this.$rtti.$Set("TFlags", {',
  33227. ' comptype: this.$rtti["TFlag"]',
  33228. '});',
  33229. 'this.ti = null;',
  33230. 'this.tiInt = null;',
  33231. 'this.tiEnum = null;',
  33232. 'this.tiSet = null;',
  33233. '']),
  33234. LinesToStr([ // $mod.$main
  33235. '$mod.ti = rtl.string;',
  33236. '$mod.ti = rtl.boolean;',
  33237. '$mod.ti = rtl.char;',
  33238. '$mod.ti = rtl.double;',
  33239. '$mod.tiInt = rtl.shortint;',
  33240. '$mod.tiInt = rtl.byte;',
  33241. '$mod.tiInt = rtl.smallint;',
  33242. '$mod.tiInt = rtl.word;',
  33243. '$mod.tiInt = rtl.longint;',
  33244. '$mod.tiInt = rtl.longword;',
  33245. '$mod.ti = rtl.jsvalue;',
  33246. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  33247. '$mod.tiSet = $mod.$rtti["TFlags"];',
  33248. '']));
  33249. end;
  33250. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  33251. begin
  33252. WithTypeInfo:=true;
  33253. StartProgram(true,[supTypeInfo]);
  33254. Add('{$modeswitch externalclass}');
  33255. Add('type');
  33256. Add(' TStaticArr = array[boolean] of string;');
  33257. Add(' TDynArr = array of string;');
  33258. Add(' TProc = procedure;');
  33259. Add(' TMethod = procedure of object;');
  33260. Add('var');
  33261. Add(' StaticArray: TStaticArr;');
  33262. Add(' tiStaticArray: TTypeInfoStaticArray;');
  33263. Add(' DynArray: TDynArr;');
  33264. Add(' tiDynArray: TTypeInfoDynArray;');
  33265. Add(' ProcVar: TProc;');
  33266. Add(' tiProcVar: TTypeInfoProcVar;');
  33267. Add(' MethodVar: TMethod;');
  33268. Add(' tiMethodVar: TTypeInfoMethodVar;');
  33269. Add('begin');
  33270. Add(' tiStaticArray:=typeinfo(StaticArray);');
  33271. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  33272. Add(' tiDynArray:=typeinfo(DynArray);');
  33273. Add(' tiDynArray:=typeinfo(TDynArr);');
  33274. Add(' tiProcVar:=typeinfo(ProcVar);');
  33275. Add(' tiProcVar:=typeinfo(TProc);');
  33276. Add(' tiMethodVar:=typeinfo(MethodVar);');
  33277. Add(' tiMethodVar:=typeinfo(TMethod);');
  33278. ConvertProgram;
  33279. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  33280. LinesToStr([ // statements
  33281. 'this.$rtti.$StaticArray("TStaticArr", {',
  33282. ' dims: [2],',
  33283. ' eltype: rtl.string',
  33284. '});',
  33285. 'this.$rtti.$DynArray("TDynArr", {',
  33286. ' eltype: rtl.string',
  33287. '});',
  33288. 'this.$rtti.$ProcVar("TProc", {',
  33289. ' procsig: rtl.newTIProcSig([])',
  33290. '});',
  33291. 'this.$rtti.$MethodVar("TMethod", {',
  33292. ' procsig: rtl.newTIProcSig([]),',
  33293. ' methodkind: 0',
  33294. '});',
  33295. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  33296. 'this.tiStaticArray = null;',
  33297. 'this.DynArray = [];',
  33298. 'this.tiDynArray = null;',
  33299. 'this.ProcVar = null;',
  33300. 'this.tiProcVar = null;',
  33301. 'this.MethodVar = null;',
  33302. 'this.tiMethodVar = null;',
  33303. '']),
  33304. LinesToStr([ // $mod.$main
  33305. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  33306. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  33307. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  33308. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  33309. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  33310. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  33311. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  33312. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  33313. '']));
  33314. end;
  33315. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  33316. begin
  33317. WithTypeInfo:=true;
  33318. StartProgram(true,[supTypeInfo]);
  33319. Add('{$modeswitch externalclass}');
  33320. Add('type');
  33321. Add(' TRec = record end;');
  33322. // ToDo: ^TRec
  33323. Add(' TObject = class end;');
  33324. Add(' TClass = class of tobject;');
  33325. Add('var');
  33326. Add(' Rec: trec;');
  33327. Add(' tiRecord: ttypeinforecord;');
  33328. Add(' Obj: tobject;');
  33329. Add(' tiClass: ttypeinfoclass;');
  33330. Add(' aClass: tclass;');
  33331. Add(' tiClassRef: ttypeinfoclassref;');
  33332. // ToDo: ^TRec
  33333. Add(' tiPointer: ttypeinfopointer;');
  33334. Add('begin');
  33335. Add(' tirecord:=typeinfo(trec);');
  33336. Add(' tirecord:=typeinfo(trec);');
  33337. Add(' ticlass:=typeinfo(obj);');
  33338. Add(' ticlass:=typeinfo(tobject);');
  33339. Add(' ticlass:=typeinfo(aclass);');
  33340. Add(' ticlassref:=typeinfo(tclass);');
  33341. ConvertProgram;
  33342. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  33343. LinesToStr([ // statements
  33344. 'rtl.recNewT(this, "TRec", function () {',
  33345. ' this.$eq = function (b) {',
  33346. ' return true;',
  33347. ' };',
  33348. ' this.$assign = function (s) {',
  33349. ' return this;',
  33350. ' };',
  33351. ' $mod.$rtti.$Record("TRec", {});',
  33352. '});',
  33353. 'rtl.createClass(this, "TObject", null, function () {',
  33354. ' this.$init = function () {',
  33355. ' };',
  33356. ' this.$final = function () {',
  33357. ' };',
  33358. '});',
  33359. 'this.$rtti.$ClassRef("TClass", {',
  33360. ' instancetype: this.$rtti["TObject"]',
  33361. '});',
  33362. 'this.Rec = this.TRec.$new();',
  33363. 'this.tiRecord = null;',
  33364. 'this.Obj = null;',
  33365. 'this.tiClass = null;',
  33366. 'this.aClass = null;',
  33367. 'this.tiClassRef = null;',
  33368. 'this.tiPointer = null;',
  33369. '']),
  33370. LinesToStr([ // $mod.$main
  33371. '$mod.tiRecord = $mod.$rtti["TRec"];',
  33372. '$mod.tiRecord = $mod.$rtti["TRec"];',
  33373. '$mod.tiClass = $mod.Obj.$rtti;',
  33374. '$mod.tiClass = $mod.$rtti["TObject"];',
  33375. '$mod.tiClass = $mod.aClass.$rtti;',
  33376. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  33377. '']));
  33378. end;
  33379. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  33380. begin
  33381. WithTypeInfo:=true;
  33382. StartProgram(true,[supTypeInfo]);
  33383. Add([
  33384. '{$modeswitch externalclass}',
  33385. 'type',
  33386. ' TClass = class of tobject;',
  33387. ' TObject = class',
  33388. ' function MyClass: TClass;',
  33389. ' class function ClassType: TClass;',
  33390. ' end;',
  33391. 'function TObject.MyClass: TClass;',
  33392. 'var t: TTypeInfoClass;',
  33393. 'begin',
  33394. ' t:=TypeInfo(Self);',
  33395. ' t:=TypeInfo(Result);',
  33396. ' t:=TypeInfo(TObject);',
  33397. 'end;',
  33398. 'class function TObject.ClassType: TClass;',
  33399. 'var t: TTypeInfoClass;',
  33400. 'begin',
  33401. ' t:=TypeInfo(Self);',
  33402. ' t:=TypeInfo(Result);',
  33403. 'end;',
  33404. 'var',
  33405. ' Obj: TObject;',
  33406. ' t: TTypeInfoClass;',
  33407. 'begin',
  33408. ' t:=TypeInfo(TObject.ClassType);',
  33409. ' t:=TypeInfo(Obj.ClassType);',
  33410. ' t:=TypeInfo(Obj.MyClass);',
  33411. '']);
  33412. ConvertProgram;
  33413. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  33414. LinesToStr([ // statements
  33415. 'this.$rtti.$Class("TObject");',
  33416. 'this.$rtti.$ClassRef("TClass", {',
  33417. ' instancetype: this.$rtti["TObject"]',
  33418. '});',
  33419. 'rtl.createClass(this, "TObject", null, function () {',
  33420. ' this.$init = function () {',
  33421. ' };',
  33422. ' this.$final = function () {',
  33423. ' };',
  33424. ' this.MyClass = function () {',
  33425. ' var Result = null;',
  33426. ' var t = null;',
  33427. ' t = this.$rtti;',
  33428. ' t = Result.$rtti;',
  33429. ' t = $mod.$rtti["TObject"];',
  33430. ' return Result;',
  33431. ' };',
  33432. ' this.ClassType = function () {',
  33433. ' var Result = null;',
  33434. ' var t = null;',
  33435. ' t = this.$rtti;',
  33436. ' t = Result.$rtti;',
  33437. ' return Result;',
  33438. ' };',
  33439. '});',
  33440. 'this.Obj = null;',
  33441. 'this.t = null;',
  33442. '']),
  33443. LinesToStr([ // $mod.$main
  33444. '$mod.t = $mod.TObject.ClassType().$rtti;',
  33445. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  33446. '$mod.t = $mod.Obj.MyClass().$rtti;',
  33447. '']));
  33448. end;
  33449. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  33450. begin
  33451. WithTypeInfo:=true;
  33452. AddModuleWithIntfImplSrc('typinfo.pas',
  33453. LinesToStr([
  33454. '{$modeswitch externalclass}',
  33455. 'type',
  33456. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  33457. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  33458. '']),
  33459. '');
  33460. AddModuleWithIntfImplSrc('unit2.pas',
  33461. LinesToStr([
  33462. 'uses typinfo;',
  33463. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  33464. 'procedure DoPtr(p: PTypeInfo);',
  33465. 'procedure DoInfo(t: TTypeInfo);',
  33466. 'procedure DoInt(t: TTypeInfoInteger);',
  33467. '']),
  33468. LinesToStr([
  33469. 'procedure DoPtr(p: PTypeInfo);',
  33470. 'begin end;',
  33471. 'procedure DoInfo(t: TTypeInfo);',
  33472. 'begin end;',
  33473. 'procedure DoInt(t: TTypeInfoInteger);',
  33474. 'begin end;',
  33475. '']));
  33476. StartUnit(true);
  33477. Add([
  33478. 'interface',
  33479. 'uses unit2;', // does not use unit typinfo
  33480. 'implementation',
  33481. 'var',
  33482. ' i: byte;',
  33483. ' p: pointer;',
  33484. ' t: PTypeInfo;',
  33485. 'initialization',
  33486. ' p:=typeinfo(i);',
  33487. ' t:=typeinfo(i);',
  33488. ' if p=t then ;',
  33489. ' if p=typeinfo(i) then ;',
  33490. ' if typeinfo(i)=p then ;',
  33491. ' if t=typeinfo(i) then ;',
  33492. ' if typeinfo(i)=t then ;',
  33493. ' DoPtr(p);',
  33494. ' DoPtr(t);',
  33495. ' DoPtr(typeinfo(i));',
  33496. ' DoInfo(p);',
  33497. ' DoInfo(t);',
  33498. ' DoInfo(typeinfo(i));',
  33499. ' DoInt(typeinfo(i));',
  33500. '']);
  33501. ConvertUnit;
  33502. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  33503. LinesToStr([ // statements
  33504. 'var $impl = $mod.$impl;',
  33505. '']),
  33506. LinesToStr([ // this.$init
  33507. '$impl.p = rtl.byte;',
  33508. '$impl.t = rtl.byte;',
  33509. 'if ($impl.p === $impl.t) ;',
  33510. 'if ($impl.p === rtl.byte) ;',
  33511. 'if (rtl.byte === $impl.p) ;',
  33512. 'if ($impl.t === rtl.byte) ;',
  33513. 'if (rtl.byte === $impl.t) ;',
  33514. 'pas.unit2.DoPtr($impl.p);',
  33515. 'pas.unit2.DoPtr($impl.t);',
  33516. 'pas.unit2.DoPtr(rtl.byte);',
  33517. 'pas.unit2.DoInfo($impl.p);',
  33518. 'pas.unit2.DoInfo($impl.t);',
  33519. 'pas.unit2.DoInfo(rtl.byte);',
  33520. 'pas.unit2.DoInt(rtl.byte);',
  33521. '']),
  33522. LinesToStr([ // implementation
  33523. '$impl.i = 0;',
  33524. '$impl.p = null;',
  33525. '$impl.t = null;',
  33526. '']) );
  33527. end;
  33528. procedure TTestModule.TestRTTI_Interface_Corba;
  33529. begin
  33530. WithTypeInfo:=true;
  33531. StartProgram(true,[supTypeInfo]);
  33532. Add([
  33533. '{$interfaces corba}',
  33534. '{$modeswitch externalclass}',
  33535. 'type',
  33536. ' IUnknown = interface',
  33537. ' end;',
  33538. ' IBird = interface',
  33539. ' function GetItem: longint;',
  33540. ' procedure SetItem(Value: longint);',
  33541. ' property Item: longint read GetItem write SetItem;',
  33542. ' end;',
  33543. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  33544. 'var',
  33545. ' i: IBird;',
  33546. ' t: TTypeInfoInterface;',
  33547. 'begin',
  33548. ' t:=TypeInfo(IBird);',
  33549. ' t:=TypeInfo(i);',
  33550. ' DoIt(t);',
  33551. ' DoIt(TypeInfo(IBird));',
  33552. '']);
  33553. ConvertProgram;
  33554. CheckSource('TestRTTI_Interface_Corba',
  33555. LinesToStr([ // statements
  33556. 'rtl.createInterface(',
  33557. ' this,',
  33558. ' "IUnknown",',
  33559. ' "{B92D5841-758A-322B-B800-000000000000}",',
  33560. ' [],',
  33561. ' null,',
  33562. ' function () {',
  33563. ' }',
  33564. ');',
  33565. 'rtl.createInterface(',
  33566. ' this,',
  33567. ' "IBird",',
  33568. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  33569. ' ["GetItem", "SetItem"],',
  33570. ' null,',
  33571. ' function () {',
  33572. ' var $r = this.$rtti;',
  33573. ' $r.addMethod("GetItem", 1, [], 2, rtl.longint);',
  33574. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33575. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33576. ' }',
  33577. ');',
  33578. 'this.DoIt = function (t) {',
  33579. '}; ',
  33580. 'this.i = null;',
  33581. 'this.t = null;',
  33582. '']),
  33583. LinesToStr([ // $mod.$main
  33584. '$mod.t = $mod.$rtti["IBird"];',
  33585. '$mod.t = $mod.i.$rtti;',
  33586. '$mod.DoIt($mod.t);',
  33587. '$mod.DoIt($mod.$rtti["IBird"]);',
  33588. '']));
  33589. end;
  33590. procedure TTestModule.TestRTTI_Interface_COM;
  33591. begin
  33592. WithTypeInfo:=true;
  33593. StartProgram(true,[supTypeInfo]);
  33594. Add([
  33595. '{$interfaces com}',
  33596. '{$modeswitch externalclass}',
  33597. 'type',
  33598. ' TGuid = record end;',
  33599. ' integer = longint;',
  33600. ' IUnknown = interface',
  33601. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  33602. ' function _AddRef: Integer;',
  33603. ' function _Release: Integer;',
  33604. ' end;',
  33605. ' IBird = interface',
  33606. ' function GetItem: longint;',
  33607. ' procedure SetItem(Value: longint);',
  33608. ' property Item: longint read GetItem write SetItem;',
  33609. ' end;',
  33610. 'var',
  33611. ' i: IBird;',
  33612. ' t: TTypeInfoInterface;',
  33613. 'begin',
  33614. ' t:=TypeInfo(IBird);',
  33615. ' t:=TypeInfo(i);',
  33616. '']);
  33617. ConvertProgram;
  33618. CheckSource('TestRTTI_Interface_COM',
  33619. LinesToStr([ // statements
  33620. 'rtl.recNewT(this, "TGuid", function () {',
  33621. ' this.$eq = function (b) {',
  33622. ' return true;',
  33623. ' };',
  33624. ' this.$assign = function (s) {',
  33625. ' return this;',
  33626. ' };',
  33627. ' $mod.$rtti.$Record("TGuid", {});',
  33628. '});',
  33629. 'rtl.createInterface(',
  33630. ' this,',
  33631. ' "IUnknown",',
  33632. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  33633. ' ["QueryInterface", "_AddRef", "_Release"],',
  33634. ' null,',
  33635. ' function () {',
  33636. ' this.$kind = "com";',
  33637. ' var $r = this.$rtti;',
  33638. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], 2, rtl.longint);',
  33639. ' $r.addMethod("_AddRef", 1, [], 2, rtl.longint);',
  33640. ' $r.addMethod("_Release", 1, [], 2, rtl.longint);',
  33641. ' }',
  33642. ');',
  33643. 'rtl.createInterface(',
  33644. ' this,',
  33645. ' "IBird",',
  33646. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  33647. ' ["GetItem", "SetItem"],',
  33648. ' this.IUnknown,',
  33649. ' function () {',
  33650. ' var $r = this.$rtti;',
  33651. ' $r.addMethod("GetItem", 1, [], 2, rtl.longint);',
  33652. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33653. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33654. ' }',
  33655. ');',
  33656. 'this.i = null;',
  33657. 'this.t = null;',
  33658. '']),
  33659. LinesToStr([ // $mod.$main
  33660. '$mod.t = $mod.$rtti["IBird"];',
  33661. '$mod.t = $mod.i.$rtti;',
  33662. '']));
  33663. end;
  33664. procedure TTestModule.TestRTTI_ClassHelper;
  33665. begin
  33666. WithTypeInfo:=true;
  33667. StartProgram(true,[supTypeInfo]);
  33668. Add([
  33669. '{$interfaces com}',
  33670. '{$modeswitch externalclass}',
  33671. 'type',
  33672. ' TObject = class',
  33673. ' end;',
  33674. ' THelper = class helper for TObject',
  33675. ' published',
  33676. ' function GetItem: longint;',
  33677. ' property Item: longint read GetItem;',
  33678. ' end;',
  33679. 'function THelper.GetItem: longint;',
  33680. 'begin',
  33681. 'end;',
  33682. 'var',
  33683. ' t: TTypeInfoHelper;',
  33684. 'begin',
  33685. ' t:=TypeInfo(THelper);',
  33686. '']);
  33687. ConvertProgram;
  33688. CheckSource('TestRTTI_ClassHelper',
  33689. LinesToStr([ // statements
  33690. 'rtl.createClass(this, "TObject", null, function () {',
  33691. ' this.$init = function () {',
  33692. ' };',
  33693. ' this.$final = function () {',
  33694. ' };',
  33695. '});',
  33696. 'rtl.createHelper(this, "THelper", null, function () {',
  33697. ' this.GetItem = function () {',
  33698. ' var Result = 0;',
  33699. ' return Result;',
  33700. ' };',
  33701. ' var $r = this.$rtti;',
  33702. ' $r.addMethod("GetItem", 1, [], 4, rtl.longint);',
  33703. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  33704. '});',
  33705. 'this.t = null;',
  33706. '']),
  33707. LinesToStr([ // $mod.$main
  33708. '$mod.t = $mod.$rtti["THelper"];',
  33709. '']));
  33710. end;
  33711. procedure TTestModule.TestRTTI_ExternalClass;
  33712. begin
  33713. WithTypeInfo:=true;
  33714. StartProgram(true,[supTypeInfo]);
  33715. Add([
  33716. '{$modeswitch externalclass}',
  33717. 'type',
  33718. ' TJSObject = class external name ''Object''',
  33719. ' end;',
  33720. ' TJSArray = class external name ''Array'' (TJSObject)',
  33721. ' end;',
  33722. 'var',
  33723. ' p: Pointer;',
  33724. ' tc: TTypeInfoExtClass;',
  33725. 'begin',
  33726. ' p:=typeinfo(TJSArray);']);
  33727. ConvertProgram;
  33728. CheckSource('TestRTTI_ExternalClass',
  33729. LinesToStr([ // statements
  33730. 'this.$rtti.$ExtClass("TJSObject", {',
  33731. ' jsclass: "Object"',
  33732. '});',
  33733. 'this.$rtti.$ExtClass("TJSArray", {',
  33734. ' ancestor: this.$rtti["TJSObject"],',
  33735. ' jsclass: "Array"',
  33736. '});',
  33737. 'this.p = null;',
  33738. 'this.tc = null;',
  33739. '']),
  33740. LinesToStr([ // $mod.$main
  33741. '$mod.p = $mod.$rtti["TJSArray"];',
  33742. '']));
  33743. end;
  33744. procedure TTestModule.TestRTTI_Unit;
  33745. begin
  33746. WithTypeInfo:=true;
  33747. AddModuleWithIntfImplSrc('unit2.pas',
  33748. LinesToStr([
  33749. '{$mode delphi}',
  33750. 'type',
  33751. ' TWordArray = array of word;',
  33752. ' TArray<T> = array of T;',
  33753. '']),
  33754. '');
  33755. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  33756. Add([
  33757. '{$mode delphi}',
  33758. 'interface',
  33759. 'uses unit2;',
  33760. 'type',
  33761. ' IBird = interface',
  33762. ' function Swoop: TWordArray;',
  33763. ' function Glide: TArray<word>;',
  33764. ' end;',
  33765. 'procedure Fly;',
  33766. 'implementation',
  33767. 'procedure Fly;',
  33768. 'var',
  33769. ' ta: tTypeInfoDynArray;',
  33770. ' ti: tTypeInfoInterface;',
  33771. 'begin',
  33772. ' ta:=typeinfo(TWordArray);',
  33773. ' ta:=typeinfo(TArray<word>);',
  33774. ' ti:=typeinfo(IBird);',
  33775. 'end;',
  33776. '']);
  33777. ConvertUnit;
  33778. CheckSource('TestRTTI_Unit',
  33779. LinesToStr([ // statements
  33780. 'rtl.createInterface(',
  33781. ' this,',
  33782. ' "IBird",',
  33783. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  33784. ' ["Swoop", "Glide"],',
  33785. ' pas.system.IUnknown,',
  33786. ' function () {',
  33787. ' var $r = this.$rtti;',
  33788. ' $r.addMethod("Swoop", 1, [], 2, pas.unit2.$rtti["TWordArray"]);',
  33789. ' $r.addMethod("Glide", 1, [], 2, pas.unit2.$rtti["TArray<System.Word>"]);',
  33790. ' }',
  33791. ');',
  33792. 'this.Fly = function () {',
  33793. ' var ta = null;',
  33794. ' var ti = null;',
  33795. ' ta = pas.unit2.$rtti["TWordArray"];',
  33796. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  33797. ' ti = $mod.$rtti["IBird"];',
  33798. '};',
  33799. '']),
  33800. LinesToStr([ // $mod.$main
  33801. '']));
  33802. end;
  33803. procedure TTestModule.TestResourcestringProgram;
  33804. begin
  33805. AddModuleWithIntfImplSrc('unit2.pas',
  33806. LinesToStr([
  33807. 'resourcestring Title = ''Nice'';',
  33808. '']),
  33809. '');
  33810. StartProgram(true);
  33811. Add([
  33812. 'uses unit2;',
  33813. 'const Bar = ''bar'';',
  33814. 'resourcestring',
  33815. ' Red = ''red'';',
  33816. ' Foobar = ''fOo''+bar;',
  33817. 'var s: string;',
  33818. ' c: char;',
  33819. 'begin',
  33820. ' s:=red;',
  33821. ' s:=test1.red;',
  33822. ' s:=Title;',
  33823. ' c:=red[1];',
  33824. ' c:=test1.red[2];',
  33825. ' if red=foobar then ;',
  33826. ' if red[3]=red[4] then ;']);
  33827. ConvertProgram;
  33828. CheckSource('TestResourcestringProgram',
  33829. LinesToStr([ // statements
  33830. 'this.Bar = "bar";',
  33831. 'this.s = "";',
  33832. 'this.c = "\x00";',
  33833. '$mod.$resourcestrings = {',
  33834. ' Red: {',
  33835. ' org: "red"',
  33836. ' },',
  33837. ' Foobar: {',
  33838. ' org: "fOobar"',
  33839. ' }',
  33840. '};',
  33841. '']),
  33842. LinesToStr([ // $mod.$main
  33843. '$mod.s = rtl.getResStr($mod, "Red");',
  33844. '$mod.s = rtl.getResStr($mod, "Red");',
  33845. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33846. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  33847. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  33848. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  33849. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  33850. '']));
  33851. end;
  33852. procedure TTestModule.TestResourcestringUnit;
  33853. begin
  33854. AddModuleWithIntfImplSrc('unit2.pas',
  33855. LinesToStr([
  33856. 'resourcestring Title = ''Nice'';',
  33857. '']),
  33858. '');
  33859. StartUnit(true);
  33860. Add([
  33861. 'interface',
  33862. 'uses unit2;',
  33863. 'const Red = ''rEd'';',
  33864. 'resourcestring',
  33865. ' Blue = ''blue'';',
  33866. ' NotRed = ''not''+Red;',
  33867. 'var s: string;',
  33868. 'implementation',
  33869. 'resourcestring',
  33870. ' ImplGreen = ''green'';',
  33871. 'initialization',
  33872. ' s:=blue+ImplGreen;',
  33873. ' s:=test1.blue+test1.implgreen;',
  33874. ' s:=blue[1]+implgreen[2];',
  33875. ' s:=Title;',
  33876. '']);
  33877. ConvertUnit;
  33878. CheckSource('TestResourcestringUnit',
  33879. LinesToStr([ // statements
  33880. 'this.Red = "rEd";',
  33881. 'this.s = "";',
  33882. '$mod.$resourcestrings = {',
  33883. ' Blue: {',
  33884. ' org: "blue"',
  33885. ' },',
  33886. ' NotRed: {',
  33887. ' org: "notrEd"',
  33888. ' },',
  33889. ' ImplGreen: {',
  33890. ' org: "green"',
  33891. ' }',
  33892. '};',
  33893. '']),
  33894. LinesToStr([ // $mod.$main
  33895. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33896. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33897. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  33898. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33899. '']));
  33900. end;
  33901. procedure TTestModule.TestResourcestringImplementation;
  33902. begin
  33903. StartUnit(false);
  33904. Add([
  33905. 'interface',
  33906. 'implementation',
  33907. 'resourcestring',
  33908. ' ImplRed = ''red'';']);
  33909. ConvertUnit;
  33910. CheckSource('TestResourcestringImplementation',
  33911. LinesToStr([ // intf statements
  33912. 'var $impl = $mod.$impl;']),
  33913. LinesToStr([ // $mod.$init
  33914. '']),
  33915. LinesToStr([ // impl statements
  33916. '$mod.$resourcestrings = {',
  33917. ' ImplRed: {',
  33918. ' org: "red"',
  33919. ' }',
  33920. '};',
  33921. '']));
  33922. end;
  33923. procedure TTestModule.TestAttributes_Members;
  33924. begin
  33925. WithTypeInfo:=true;
  33926. StartProgram(false);
  33927. Add([
  33928. '{$modeswitch PrefixedAttributes}',
  33929. 'type',
  33930. ' TObject = class',
  33931. ' constructor Create;',
  33932. ' end;',
  33933. ' TCustomAttribute = class',
  33934. ' constructor Create(Id: word);',
  33935. ' end;',
  33936. ' [Missing]',
  33937. ' TBird = class',
  33938. ' published',
  33939. ' [Tcustom]',
  33940. ' FField: word;',
  33941. ' [tcustom(14)]',
  33942. ' property Size: word read FField;',
  33943. ' [Tcustom(15)]',
  33944. ' procedure Fly; virtual; abstract;',
  33945. ' end;',
  33946. ' TRec = record',
  33947. ' [Tcustom,tcustom(14)]',
  33948. ' Size: word;',
  33949. ' [Tcustom(15)]',
  33950. ' Width, Height: word;',
  33951. ' end;',
  33952. 'constructor TObject.Create; begin end;',
  33953. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  33954. 'begin',
  33955. '']);
  33956. ConvertProgram;
  33957. CheckSource('TestAttributes_Members',
  33958. LinesToStr([ // statements
  33959. 'rtl.createClass(this, "TObject", null, function () {',
  33960. ' this.$init = function () {',
  33961. ' };',
  33962. ' this.$final = function () {',
  33963. ' };',
  33964. ' this.Create = function () {',
  33965. ' return this;',
  33966. ' };',
  33967. '});',
  33968. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33969. ' this.Create$1 = function (Id) {',
  33970. ' return this;',
  33971. ' };',
  33972. '});',
  33973. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33974. ' this.$init = function () {',
  33975. ' $mod.TObject.$init.call(this);',
  33976. ' this.FField = 0;',
  33977. ' };',
  33978. ' var $r = this.$rtti;',
  33979. ' $r.addField("FField", rtl.word, 4, {',
  33980. ' attr: [$mod.TCustomAttribute, "Create"]',
  33981. ' });',
  33982. ' $r.addProperty(',
  33983. ' "Size",',
  33984. ' 0,',
  33985. ' rtl.word,',
  33986. ' "FField",',
  33987. ' "",',
  33988. ' 4,',
  33989. ' {',
  33990. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  33991. ' }',
  33992. ' );',
  33993. ' $r.addMethod(',
  33994. ' "Fly",',
  33995. ' 0,',
  33996. ' [],',
  33997. ' 4,',
  33998. ' null,',
  33999. ' 0,',
  34000. ' {',
  34001. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  34002. ' });',
  34003. '});',
  34004. 'rtl.recNewT(this, "TRec", function () {',
  34005. ' this.Size = 0;',
  34006. ' this.Width = 0;',
  34007. ' this.Height = 0;',
  34008. ' this.$eq = function (b) {',
  34009. ' return (this.Size === b.Size) && (this.Width === b.Width) && (this.Height === b.Height);',
  34010. ' };',
  34011. ' this.$assign = function (s) {',
  34012. ' this.Size = s.Size;',
  34013. ' this.Width = s.Width;',
  34014. ' this.Height = s.Height;',
  34015. ' return this;',
  34016. ' };',
  34017. ' var $r = $mod.$rtti.$Record("TRec", {}, this);',
  34018. ' $r.addField("Size", rtl.word, 2, {',
  34019. ' attr: [',
  34020. ' $mod.TCustomAttribute,',
  34021. ' "Create",',
  34022. ' $mod.TCustomAttribute,',
  34023. ' "Create$1",',
  34024. ' [14]',
  34025. ' ]',
  34026. ' });',
  34027. ' $r.addField("Width", rtl.word, 2, {',
  34028. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  34029. ' });',
  34030. ' $r.addField("Height", rtl.word);',
  34031. '});',
  34032. '']),
  34033. LinesToStr([ // $mod.$main
  34034. '']));
  34035. end;
  34036. procedure TTestModule.TestAttributes_Types;
  34037. begin
  34038. WithTypeInfo:=true;
  34039. StartProgram(false);
  34040. Add([
  34041. '{$modeswitch PrefixedAttributes}',
  34042. 'type',
  34043. ' TObject = class',
  34044. ' constructor Create(Id: word);',
  34045. ' end;',
  34046. ' TCustomAttribute = class',
  34047. ' end;',
  34048. ' [TCustom(1)]',
  34049. ' TMyClass = class',
  34050. ' end;',
  34051. ' [TCustom(11)]',
  34052. ' TMyDescendant = class(TMyClass)',
  34053. ' end;',
  34054. ' [TCustom(2)]',
  34055. ' TRec = record',
  34056. ' end;',
  34057. ' [TCustom(3)]',
  34058. ' TInt = type word;',
  34059. 'constructor TObject.Create(Id: word);',
  34060. 'begin',
  34061. 'end;',
  34062. 'var p: pointer;',
  34063. 'begin',
  34064. ' p:=typeinfo(TMyClass);',
  34065. ' p:=typeinfo(TRec);',
  34066. ' p:=typeinfo(TInt);',
  34067. '']);
  34068. ConvertProgram;
  34069. CheckSource('TestAttributes_Types',
  34070. LinesToStr([ // statements
  34071. 'rtl.createClass(this, "TObject", null, function () {',
  34072. ' this.$init = function () {',
  34073. ' };',
  34074. ' this.$final = function () {',
  34075. ' };',
  34076. ' this.Create = function (Id) {',
  34077. ' return this;',
  34078. ' };',
  34079. '});',
  34080. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  34081. '});',
  34082. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  34083. ' var $r = this.$rtti;',
  34084. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  34085. '});',
  34086. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  34087. ' var $r = this.$rtti;',
  34088. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  34089. '});',
  34090. 'rtl.recNewT(this, "TRec", function () {',
  34091. ' this.$eq = function (b) {',
  34092. ' return true;',
  34093. ' };',
  34094. ' this.$assign = function (s) {',
  34095. ' return this;',
  34096. ' };',
  34097. ' $mod.$rtti.$Record("TRec", {',
  34098. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  34099. ' });',
  34100. '});',
  34101. 'this.$rtti.$inherited("TInt", rtl.word, {',
  34102. ' attr: [this.TCustomAttribute, "Create", [3]]',
  34103. '});',
  34104. 'this.p = null;',
  34105. '']),
  34106. LinesToStr([ // $mod.$main
  34107. '$mod.p = $mod.$rtti["TMyClass"];',
  34108. '$mod.p = $mod.$rtti["TRec"];',
  34109. '$mod.p = $mod.$rtti["TInt"];',
  34110. '']));
  34111. end;
  34112. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  34113. begin
  34114. WithTypeInfo:=true;
  34115. StartProgram(false);
  34116. Add([
  34117. '{$modeswitch PrefixedAttributes}',
  34118. 'type',
  34119. ' TObject = class',
  34120. ' constructor Create;',
  34121. ' end;',
  34122. ' TCustomAttribute = class',
  34123. ' end;',
  34124. ' THelper = class helper for TCustomAttribute',
  34125. ' constructor Create(Id: word);',
  34126. ' end;',
  34127. ' [TCustom(3)]',
  34128. ' TMyInt = word;',
  34129. 'constructor TObject.Create; begin end;',
  34130. 'constructor THelper.Create(Id: word); begin end;',
  34131. 'begin',
  34132. ' if typeinfo(TMyInt)=nil then ;']);
  34133. ConvertProgram;
  34134. end;
  34135. procedure TTestModule.TestAttributes_InterfacesList;
  34136. begin
  34137. WithTypeInfo:=true;
  34138. StartProgram(false);
  34139. Add([
  34140. '{$mode Delphi}',
  34141. 'type',
  34142. ' TObject = class',
  34143. ' constructor Create;',
  34144. ' end;',
  34145. ' IInterface = interface end;',
  34146. ' TCustomAttribute = class',
  34147. ' end;',
  34148. ' Red = class(TCustomAttribute);',
  34149. ' Blue = class(TCustomAttribute);',
  34150. ' [Red]',
  34151. ' IBird<T> = interface',
  34152. ' procedure Fly;',
  34153. ' end;',
  34154. ' [Blue]',
  34155. ' IEagle = interface(IBird<Word>)',
  34156. ' procedure Dive;',
  34157. ' end;',
  34158. ' TAnt = class(TObject, IEagle)',
  34159. ' procedure Fly; virtual; abstract;',
  34160. ' procedure Dive; virtual; abstract;',
  34161. ' end;',
  34162. 'constructor TObject.Create;',
  34163. 'begin',
  34164. 'end;',
  34165. 'begin',
  34166. '']);
  34167. ConvertProgram;
  34168. CheckSource('TestAttributes_InterfacesList',
  34169. LinesToStr([ // statements
  34170. '$mod.$rtti.$Interface("IBird<System.Word>");',
  34171. 'rtl.createClass(this, "TObject", null, function () {',
  34172. ' this.$init = function () {',
  34173. ' };',
  34174. ' this.$final = function () {',
  34175. ' };',
  34176. ' this.Create = function () {',
  34177. ' return this;',
  34178. ' };',
  34179. '});',
  34180. 'rtl.createInterface(',
  34181. ' this,',
  34182. ' "IInterface",',
  34183. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  34184. ' [],',
  34185. ' null,',
  34186. ' function () {',
  34187. ' this.$kind = "com";',
  34188. ' }',
  34189. ');',
  34190. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  34191. '});',
  34192. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  34193. '});',
  34194. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  34195. '});',
  34196. 'rtl.createInterface(',
  34197. ' this,',
  34198. ' "IBird$G1",',
  34199. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  34200. ' ["Fly"],',
  34201. ' this.IInterface,',
  34202. ' function () {',
  34203. ' var $r = this.$rtti;',
  34204. ' $r.addMethod("Fly", 0, []);',
  34205. ' $r.attr = [$mod.Red, "Create"];',
  34206. ' },',
  34207. ' "IBird<System.Word>"',
  34208. ');',
  34209. 'rtl.createInterface(',
  34210. ' this,',
  34211. ' "IEagle",',
  34212. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  34213. ' ["Dive"],',
  34214. ' this.IBird$G1,',
  34215. ' function () {',
  34216. ' var $r = this.$rtti;',
  34217. ' $r.addMethod("Dive", 0, []);',
  34218. ' $r.attr = [$mod.Blue, "Create"];',
  34219. ' }',
  34220. ');',
  34221. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  34222. ' rtl.addIntf(this, $mod.IEagle);',
  34223. '});',
  34224. '']),
  34225. LinesToStr([ // $mod.$main
  34226. '']));
  34227. end;
  34228. procedure TTestModule.TestAssert;
  34229. begin
  34230. StartProgram(false);
  34231. Add([
  34232. 'procedure DoIt;',
  34233. 'var',
  34234. ' b: boolean;',
  34235. ' s: string;',
  34236. 'begin',
  34237. ' {$Assertions on}',
  34238. ' Assert(b);',
  34239. 'end;',
  34240. 'begin',
  34241. ' DoIt;',
  34242. '']);
  34243. ConvertProgram;
  34244. CheckSource('TestAssert',
  34245. LinesToStr([ // statements
  34246. 'this.DoIt = function () {',
  34247. ' var b = false;',
  34248. ' var s = "";',
  34249. ' if (!b) throw "assert failed";',
  34250. '};',
  34251. '']),
  34252. LinesToStr([ // $mod.$main
  34253. '$mod.DoIt();',
  34254. '']));
  34255. end;
  34256. procedure TTestModule.TestAssert_SysUtils;
  34257. begin
  34258. AddModuleWithIntfImplSrc('SysUtils.pas',
  34259. LinesToStr([
  34260. 'type',
  34261. ' TObject = class',
  34262. ' constructor Create;',
  34263. ' end;',
  34264. ' EAssertionFailed = class',
  34265. ' constructor Create(s: string);',
  34266. ' end;',
  34267. '']),
  34268. LinesToStr([
  34269. 'constructor TObject.Create;',
  34270. 'begin end;',
  34271. 'constructor EAssertionFailed.Create(s: string);',
  34272. 'begin end;',
  34273. '']) );
  34274. StartProgram(true);
  34275. Add([
  34276. 'uses sysutils;',
  34277. 'procedure DoIt;',
  34278. 'var',
  34279. ' b: boolean;',
  34280. ' s: string;',
  34281. 'begin',
  34282. ' {$Assertions on}',
  34283. ' Assert(b);',
  34284. ' Assert(b,''msg'');',
  34285. 'end;',
  34286. 'begin',
  34287. ' DoIt;',
  34288. '']);
  34289. ConvertProgram;
  34290. CheckSource('TestAssert_SysUtils',
  34291. LinesToStr([ // statements
  34292. 'this.DoIt = function () {',
  34293. ' var b = false;',
  34294. ' var s = "";',
  34295. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  34296. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  34297. '};',
  34298. '']),
  34299. LinesToStr([ // $mod.$main
  34300. '$mod.DoIt();',
  34301. '']));
  34302. end;
  34303. procedure TTestModule.TestObjectChecks;
  34304. begin
  34305. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  34306. StartProgram(false);
  34307. Add([
  34308. 'type',
  34309. ' TObject = class',
  34310. ' procedure DoIt;',
  34311. ' end;',
  34312. ' TClass = class of tobject;',
  34313. ' TBird = class',
  34314. ' end;',
  34315. ' TBirdClass = class of TBird;',
  34316. 'var',
  34317. ' o : TObject;',
  34318. ' c: TClass;',
  34319. ' b: TBird;',
  34320. ' bc: TBirdClass;',
  34321. 'procedure TObject.DoIt;',
  34322. 'begin',
  34323. ' b:=TBird(o);',
  34324. 'end;',
  34325. 'begin',
  34326. ' o.DoIt;',
  34327. ' b:=TBird(o);',
  34328. ' bc:=TBirdClass(c);',
  34329. '']);
  34330. ConvertProgram;
  34331. CheckSource('TestCheckMethodCall',
  34332. LinesToStr([ // statements
  34333. 'rtl.createClass(this, "TObject", null, function () {',
  34334. ' this.$init = function () {',
  34335. ' };',
  34336. ' this.$final = function () {',
  34337. ' };',
  34338. ' this.DoIt = function () {',
  34339. ' rtl.checkMethodCall(this,$mod.TObject);',
  34340. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  34341. ' };',
  34342. '});',
  34343. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34344. '});',
  34345. 'this.o = null;',
  34346. 'this.c = null;',
  34347. 'this.b = null;',
  34348. 'this.bc = null;',
  34349. '']),
  34350. LinesToStr([ // $mod.$main
  34351. '$mod.o.DoIt();',
  34352. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  34353. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  34354. '']));
  34355. end;
  34356. procedure TTestModule.TestOverflowChecks_Int;
  34357. begin
  34358. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  34359. StartProgram(false);
  34360. Add([
  34361. 'procedure DoIt;',
  34362. 'var',
  34363. ' b: byte;',
  34364. ' n: nativeint;',
  34365. ' u: nativeuint;',
  34366. ' c: currency;',
  34367. 'begin',
  34368. ' n:=n+n;',
  34369. ' n:=n-n;',
  34370. ' n:=n+b;',
  34371. ' n:=b-n;',
  34372. ' n:=n*n;',
  34373. ' n:=n*u;',
  34374. ' c:=c+b;',
  34375. ' c:=b+c;',
  34376. ' c:=c*b;',
  34377. ' c:=b*c;',
  34378. 'end;',
  34379. 'begin',
  34380. '']);
  34381. ConvertProgram;
  34382. CheckSource('TestOverflowChecks_Int',
  34383. LinesToStr([ // statements
  34384. 'this.DoIt = function () {',
  34385. ' var b = 0;',
  34386. ' var n = 0;',
  34387. ' var u = 0;',
  34388. ' var c = 0;',
  34389. ' n = rtl.oc(n + n);',
  34390. ' n = rtl.oc(n - n);',
  34391. ' n = rtl.oc(n + b);',
  34392. ' n = rtl.oc(b - n);',
  34393. ' n = rtl.oc(n * n);',
  34394. ' n = rtl.oc(n * u);',
  34395. ' c = rtl.oc(c + (b * 10000));',
  34396. ' c = rtl.oc((b * 10000) + c);',
  34397. ' c = rtl.oc(c * b);',
  34398. ' c = rtl.oc(b * c);',
  34399. '};',
  34400. '']),
  34401. LinesToStr([ // $mod.$main
  34402. '']));
  34403. end;
  34404. procedure TTestModule.TestRangeChecks_AssignInt;
  34405. begin
  34406. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34407. StartProgram(false);
  34408. Add([
  34409. '{$R+}',
  34410. 'var',
  34411. ' b: byte = 2;',
  34412. ' w: word = 3;',
  34413. 'procedure DoIt(p: byte);',
  34414. 'begin',
  34415. ' b:=w;',
  34416. ' b+=w;',
  34417. ' b:=1;',
  34418. 'end;',
  34419. '{$R-}',
  34420. 'procedure DoSome;',
  34421. 'begin',
  34422. ' DoIt(w);',
  34423. ' b:=w;',
  34424. ' b:=2;',
  34425. 'end;',
  34426. 'begin',
  34427. '{$R+}',
  34428. '']);
  34429. ConvertProgram;
  34430. CheckSource('TestRangeChecks_AssignInt',
  34431. LinesToStr([ // statements
  34432. 'this.b = 2;',
  34433. 'this.w = 3;',
  34434. 'this.DoIt = function (p) {',
  34435. ' rtl.rc(p, 0, 255);',
  34436. ' $mod.b = rtl.rc($mod.w,0,255);',
  34437. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  34438. ' $mod.b = 1;',
  34439. '};',
  34440. 'this.DoSome = function () {',
  34441. ' $mod.DoIt($mod.w);',
  34442. ' $mod.b = $mod.w;',
  34443. ' $mod.b = 2;',
  34444. '};',
  34445. '']),
  34446. LinesToStr([ // $mod.$main
  34447. '']));
  34448. end;
  34449. procedure TTestModule.TestRangeChecks_AssignIntRange;
  34450. begin
  34451. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34452. StartProgram(false);
  34453. Add([
  34454. '{$R+}',
  34455. 'type Ten = 1..10;',
  34456. 'var',
  34457. ' b: Ten = 2;',
  34458. ' w: Ten = 3;',
  34459. 'procedure DoIt(p: Ten);',
  34460. 'begin',
  34461. ' b:=w;',
  34462. ' b+=w;',
  34463. ' b:=1;',
  34464. 'end;',
  34465. '{$R-}',
  34466. 'procedure DoSome;',
  34467. 'begin',
  34468. ' DoIt(w);',
  34469. ' b:=w;',
  34470. ' b:=2;',
  34471. 'end;',
  34472. 'begin',
  34473. '{$R+}',
  34474. '']);
  34475. ConvertProgram;
  34476. CheckSource('TestRangeChecks_AssignIntRange',
  34477. LinesToStr([ // statements
  34478. 'this.b = 2;',
  34479. 'this.w = 3;',
  34480. 'this.DoIt = function (p) {',
  34481. ' rtl.rc(p, 1, 10);',
  34482. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  34483. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  34484. ' $mod.b = 1;',
  34485. '};',
  34486. 'this.DoSome = function () {',
  34487. ' $mod.DoIt($mod.w);',
  34488. ' $mod.b = $mod.w;',
  34489. ' $mod.b = 2;',
  34490. '};',
  34491. '']),
  34492. LinesToStr([ // $mod.$main
  34493. '']));
  34494. end;
  34495. procedure TTestModule.TestRangeChecks_AssignEnum;
  34496. begin
  34497. StartProgram(false);
  34498. Add([
  34499. '{$R+}',
  34500. 'type TEnum = (red,green);',
  34501. 'var',
  34502. ' e: TEnum = red;',
  34503. 'procedure DoIt(p: TEnum);',
  34504. 'begin',
  34505. ' e:=p;',
  34506. ' p:=TEnum(0);',
  34507. ' p:=succ(e);',
  34508. 'end;',
  34509. '{$R-}',
  34510. 'procedure DoSome;',
  34511. 'begin',
  34512. ' DoIt(e);',
  34513. ' e:=TEnum(1);',
  34514. ' e:=pred(e);',
  34515. 'end;',
  34516. 'begin',
  34517. '{$R+}',
  34518. '']);
  34519. ConvertProgram;
  34520. CheckSource('TestRangeChecks_AssignEnum',
  34521. LinesToStr([ // statements
  34522. 'this.TEnum = {',
  34523. ' "0": "red",',
  34524. ' red: 0,',
  34525. ' "1": "green",',
  34526. ' green: 1',
  34527. '};',
  34528. 'this.e = this.TEnum.red;',
  34529. 'this.DoIt = function (p) {',
  34530. ' rtl.rc(p, 0, 1);',
  34531. ' $mod.e = rtl.rc(p, 0, 1);',
  34532. ' p = 0;',
  34533. ' p = rtl.rc($mod.e + 1, 0, 1);',
  34534. '};',
  34535. 'this.DoSome = function () {',
  34536. ' $mod.DoIt($mod.e);',
  34537. ' $mod.e = 1;',
  34538. ' $mod.e = $mod.e - 1;',
  34539. '};',
  34540. '']),
  34541. LinesToStr([ // $mod.$main
  34542. '']));
  34543. end;
  34544. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  34545. begin
  34546. StartProgram(false);
  34547. Add([
  34548. '{$R+}',
  34549. 'type',
  34550. ' TEnum = (red,green);',
  34551. ' TEnumRg = red..green;',
  34552. 'var',
  34553. ' e: TEnumRg = red;',
  34554. 'procedure DoIt(p: TEnumRg);',
  34555. 'begin',
  34556. ' e:=p;',
  34557. ' p:=TEnumRg(0);',
  34558. ' p:=succ(e);',
  34559. 'end;',
  34560. '{$R-}',
  34561. 'procedure DoSome;',
  34562. 'begin',
  34563. ' DoIt(e);',
  34564. ' e:=TEnum(1);',
  34565. ' e:=pred(e);',
  34566. 'end;',
  34567. 'begin',
  34568. '{$R+}',
  34569. '']);
  34570. ConvertProgram;
  34571. CheckSource('TestRangeChecks_AssignEnumRange',
  34572. LinesToStr([ // statements
  34573. 'this.TEnum = {',
  34574. ' "0": "red",',
  34575. ' red: 0,',
  34576. ' "1": "green",',
  34577. ' green: 1',
  34578. '};',
  34579. 'this.e = this.TEnum.red;',
  34580. 'this.DoIt = function (p) {',
  34581. ' rtl.rc(p, 0, 1);',
  34582. ' $mod.e = rtl.rc(p, 0, 1);',
  34583. ' p = 0;',
  34584. ' p = rtl.rc($mod.e + 1, 0, 1);',
  34585. '};',
  34586. 'this.DoSome = function () {',
  34587. ' $mod.DoIt($mod.e);',
  34588. ' $mod.e = 1;',
  34589. ' $mod.e = $mod.e - 1;',
  34590. '};',
  34591. '']),
  34592. LinesToStr([ // $mod.$main
  34593. '']));
  34594. end;
  34595. procedure TTestModule.TestRangeChecks_AssignChar;
  34596. begin
  34597. StartProgram(false);
  34598. Add([
  34599. '{$R+}',
  34600. 'type',
  34601. ' TLetter = char;',
  34602. 'var',
  34603. ' b: TLetter = ''2'';',
  34604. ' w: TLetter = ''3'';',
  34605. 'procedure DoIt(p: TLetter);',
  34606. 'begin',
  34607. ' b:=w;',
  34608. ' b:=''1'';',
  34609. 'end;',
  34610. '{$R-}',
  34611. 'procedure DoSome;',
  34612. 'begin',
  34613. ' DoIt(w);',
  34614. ' b:=w;',
  34615. ' b:=''2'';',
  34616. 'end;',
  34617. 'begin',
  34618. '{$R+}',
  34619. '']);
  34620. ConvertProgram;
  34621. CheckSource('TestRangeChecks_AssignChar',
  34622. LinesToStr([ // statements
  34623. 'this.b = "2";',
  34624. 'this.w = "3";',
  34625. 'this.DoIt = function (p) {',
  34626. ' rtl.rcc(p, 0, 65535);',
  34627. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  34628. ' $mod.b = "1";',
  34629. '};',
  34630. 'this.DoSome = function () {',
  34631. ' $mod.DoIt($mod.w);',
  34632. ' $mod.b = $mod.w;',
  34633. ' $mod.b = "2";',
  34634. '};',
  34635. '']),
  34636. LinesToStr([ // $mod.$main
  34637. '']));
  34638. end;
  34639. procedure TTestModule.TestRangeChecks_AssignCharRange;
  34640. begin
  34641. StartProgram(false);
  34642. Add([
  34643. '{$R+}',
  34644. 'type TDigit = ''0''..''9'';',
  34645. 'var',
  34646. ' b: TDigit = ''2'';',
  34647. ' w: TDigit = ''3'';',
  34648. 'procedure DoIt(p: TDigit);',
  34649. 'begin',
  34650. ' b:=w;',
  34651. ' b:=''1'';',
  34652. 'end;',
  34653. '{$R-}',
  34654. 'procedure DoSome;',
  34655. 'begin',
  34656. ' DoIt(w);',
  34657. ' b:=w;',
  34658. ' b:=''2'';',
  34659. 'end;',
  34660. 'begin',
  34661. '{$R+}',
  34662. '']);
  34663. ConvertProgram;
  34664. CheckSource('TestRangeChecks_AssignCharRange',
  34665. LinesToStr([ // statements
  34666. 'this.b = "2";',
  34667. 'this.w = "3";',
  34668. 'this.DoIt = function (p) {',
  34669. ' rtl.rcc(p, 48, 57);',
  34670. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  34671. ' $mod.b = "1";',
  34672. '};',
  34673. 'this.DoSome = function () {',
  34674. ' $mod.DoIt($mod.w);',
  34675. ' $mod.b = $mod.w;',
  34676. ' $mod.b = "2";',
  34677. '};',
  34678. '']),
  34679. LinesToStr([ // $mod.$main
  34680. '']));
  34681. end;
  34682. procedure TTestModule.TestRangeChecks_ArrayIndex;
  34683. begin
  34684. StartProgram(false);
  34685. Add([
  34686. '{$R+}',
  34687. 'type',
  34688. ' Ten = 1..10;',
  34689. ' TArr = array of Ten;',
  34690. ' TArrArr = array of TArr;',
  34691. ' TArrByte = array[byte] of Ten;',
  34692. ' TArrChar = array[''0''..''9''] of Ten;',
  34693. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  34694. ' TObject = class',
  34695. ' A: TArr;',
  34696. ' end;',
  34697. 'procedure DoIt;',
  34698. 'var',
  34699. ' Arr: TArr;',
  34700. ' ArrArr: TArrArr;',
  34701. ' ArrByte: TArrByte;',
  34702. ' ArrChar: TArrChar;',
  34703. ' ArrByteChar: TArrByteChar;',
  34704. ' i: Ten;',
  34705. ' c: char;',
  34706. ' o: tobject;',
  34707. 'begin',
  34708. ' i:=Arr[1];',
  34709. ' i:=ArrByteChar[1,''2''];',
  34710. ' Arr[1]:=Arr[1];',
  34711. ' Arr[i]:=Arr[i];',
  34712. ' ArrByte[3]:=ArrByte[3];',
  34713. ' ArrByte[i]:=ArrByte[i];',
  34714. ' ArrChar[''5'']:=ArrChar[''5''];',
  34715. ' ArrChar[c]:=ArrChar[c];',
  34716. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  34717. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  34718. ' o.a[i]:=o.a[i];',
  34719. 'end;',
  34720. 'begin',
  34721. '']);
  34722. ConvertProgram;
  34723. CheckSource('TestRangeChecks_ArrayIndex',
  34724. LinesToStr([ // statements
  34725. 'this.TArrByteChar$clone = function (a) {',
  34726. ' var b = [];',
  34727. ' b.length = 256;',
  34728. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  34729. ' return b;',
  34730. '};',
  34731. 'rtl.createClass(this, "TObject", null, function () {',
  34732. ' this.$init = function () {',
  34733. ' this.A = [];',
  34734. ' };',
  34735. ' this.$final = function () {',
  34736. ' this.A = undefined;',
  34737. ' };',
  34738. '});',
  34739. 'this.DoIt = function () {',
  34740. ' var Arr = [];',
  34741. ' var ArrArr = [];',
  34742. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  34743. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  34744. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  34745. ' var i = 0;',
  34746. ' var c = "\x00";',
  34747. ' var o = null;',
  34748. ' i = rtl.rc(Arr[1], 1, 10);',
  34749. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  34750. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  34751. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  34752. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  34753. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  34754. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  34755. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  34756. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  34757. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  34758. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  34759. '};',
  34760. '']),
  34761. LinesToStr([ // $mod.$main
  34762. '']));
  34763. end;
  34764. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  34765. begin
  34766. StartProgram(false);
  34767. Add([
  34768. '{$R+}',
  34769. 'type',
  34770. ' Ten = 1..10;',
  34771. ' TRec = record x: Ten end;',
  34772. ' TArr = array of TRec;',
  34773. ' TArrArr = array of TArr;',
  34774. ' TObject = class',
  34775. ' A: TArr;',
  34776. ' end;',
  34777. 'procedure DoIt;',
  34778. 'var',
  34779. ' Arr: TArr;',
  34780. ' ArrArr: TArrArr;',
  34781. ' i: Ten;',
  34782. ' o: tobject;',
  34783. 'begin',
  34784. ' Arr[1]:=Arr[1];',
  34785. ' Arr[i]:=Arr[i+1];',
  34786. ' o.a[i]:=o.a[i+2];',
  34787. 'end;',
  34788. 'begin',
  34789. '']);
  34790. ConvertProgram;
  34791. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  34792. LinesToStr([ // statements
  34793. 'rtl.recNewT(this, "TRec", function () {',
  34794. ' this.x = 0;',
  34795. ' this.$eq = function (b) {',
  34796. ' return this.x === b.x;',
  34797. ' };',
  34798. ' this.$assign = function (s) {',
  34799. ' this.x = s.x;',
  34800. ' return this;',
  34801. ' };',
  34802. '});',
  34803. 'rtl.createClass(this, "TObject", null, function () {',
  34804. ' this.$init = function () {',
  34805. ' this.A = [];',
  34806. ' };',
  34807. ' this.$final = function () {',
  34808. ' this.A = undefined;',
  34809. ' };',
  34810. '});',
  34811. 'this.DoIt = function () {',
  34812. ' var Arr = [];',
  34813. ' var ArrArr = [];',
  34814. ' var i = 0;',
  34815. ' var o = null;',
  34816. ' Arr[1].$assign(Arr[1]);',
  34817. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  34818. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  34819. '};',
  34820. '']),
  34821. LinesToStr([ // $mod.$main
  34822. '']));
  34823. end;
  34824. procedure TTestModule.TestRangeChecks_StringIndex;
  34825. begin
  34826. StartProgram(false);
  34827. Add([
  34828. 'type',
  34829. ' TObject = class',
  34830. ' S: string;',
  34831. ' end;',
  34832. '{$R+}',
  34833. 'procedure DoIt(var h: string);',
  34834. 'var',
  34835. ' s: string;',
  34836. ' i: longint;',
  34837. ' c: char;',
  34838. ' o: tobject;',
  34839. 'begin',
  34840. ' c:=s[1];',
  34841. ' s[i]:=s[i];',
  34842. ' h[i]:=h[i];',
  34843. ' c:=o.s[i];',
  34844. ' o.s[i]:=c;',
  34845. 'end;',
  34846. 'begin',
  34847. '']);
  34848. ConvertProgram;
  34849. CheckSource('TestRangeChecks_StringIndex',
  34850. LinesToStr([ // statements
  34851. 'rtl.createClass(this, "TObject", null, function () {',
  34852. ' this.$init = function () {',
  34853. ' this.S = "";',
  34854. ' };',
  34855. ' this.$final = function () {',
  34856. ' };',
  34857. '});',
  34858. 'this.DoIt = function (h) {',
  34859. ' var s = "";',
  34860. ' var i = 0;',
  34861. ' var c = "\x00";',
  34862. ' var o = null;',
  34863. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  34864. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  34865. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  34866. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  34867. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  34868. '};',
  34869. '']),
  34870. LinesToStr([ // $mod.$main
  34871. '']));
  34872. end;
  34873. procedure TTestModule.TestRangeChecks_TypecastInt;
  34874. begin
  34875. StartProgram(false);
  34876. Add([
  34877. '{$R+}',
  34878. 'var',
  34879. ' i: nativeint;',
  34880. ' b: byte;',
  34881. ' sh: shortint;',
  34882. ' w: word;',
  34883. ' sm: smallint;',
  34884. ' lw: longword;',
  34885. ' li: longint;',
  34886. 'begin',
  34887. ' b:=12+byte(i);',
  34888. ' sh:=12+shortint(i);',
  34889. ' w:=12+word(i);',
  34890. ' sm:=12+smallint(i);',
  34891. ' lw:=12+longword(i);',
  34892. ' li:=12+longint(i);',
  34893. '']);
  34894. ConvertProgram;
  34895. CheckSource('TestRangeChecks_TypecastInt',
  34896. LinesToStr([
  34897. 'this.i = 0;',
  34898. 'this.b = 0;',
  34899. 'this.sh = 0;',
  34900. 'this.w = 0;',
  34901. 'this.sm = 0;',
  34902. 'this.lw = 0;',
  34903. 'this.li = 0;',
  34904. '']),
  34905. LinesToStr([
  34906. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  34907. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  34908. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  34909. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  34910. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  34911. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  34912. '']));
  34913. end;
  34914. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  34915. begin
  34916. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34917. StartProgram(false);
  34918. Add([
  34919. '{$modeswitch typehelpers}',
  34920. '{$R+}',
  34921. 'type',
  34922. ' TObject = class',
  34923. ' FSize: byte;',
  34924. ' property Size: byte read FSize;',
  34925. ' end;',
  34926. ' THelper = type helper for byte',
  34927. ' procedure SetIt(w: word);',
  34928. ' end;',
  34929. 'procedure THelper.SetIt(w: word);',
  34930. 'begin',
  34931. ' Self:=w;',
  34932. 'end;',
  34933. 'function GetIt: byte;',
  34934. 'begin',
  34935. ' Result.SetIt(2);',
  34936. 'end;',
  34937. 'var',
  34938. ' b: byte = 3;',
  34939. ' o: TObject;',
  34940. 'begin',
  34941. ' b.SetIt(14);',
  34942. ' with b do SetIt(15);',
  34943. ' o.Size.SetIt(16);',
  34944. '']);
  34945. ConvertProgram;
  34946. CheckSource('TestRangeChecks_AssignInt',
  34947. LinesToStr([ // statements
  34948. 'rtl.createClass(this, "TObject", null, function () {',
  34949. ' this.$init = function () {',
  34950. ' this.FSize = 0;',
  34951. ' };',
  34952. ' this.$final = function () {',
  34953. ' };',
  34954. '});',
  34955. 'rtl.createHelper(this, "THelper", null, function () {',
  34956. ' this.SetIt = function (w) {',
  34957. ' rtl.rc(w, 0, 65535);',
  34958. ' this.set(w);',
  34959. ' };',
  34960. '});',
  34961. 'this.GetIt = function () {',
  34962. ' var Result = 0;',
  34963. ' $mod.THelper.SetIt.call({',
  34964. ' get: function () {',
  34965. ' return Result;',
  34966. ' },',
  34967. ' set: function (v) {',
  34968. ' rtl.rc(v, 0, 255);',
  34969. ' Result = v;',
  34970. ' }',
  34971. ' }, 2);',
  34972. ' return Result;',
  34973. '};',
  34974. 'this.b = 3;',
  34975. 'this.o = null;',
  34976. '']),
  34977. LinesToStr([ // $mod.$main
  34978. '$mod.THelper.SetIt.call({',
  34979. ' p: $mod,',
  34980. ' get: function () {',
  34981. ' return this.p.b;',
  34982. ' },',
  34983. ' set: function (v) {',
  34984. ' rtl.rc(v, 0, 255);',
  34985. ' this.p.b = v;',
  34986. ' }',
  34987. '}, 14);',
  34988. 'var $with = $mod.b;',
  34989. '$mod.THelper.SetIt.call({',
  34990. ' get: function () {',
  34991. ' return $with;',
  34992. ' },',
  34993. ' set: function (v) {',
  34994. ' rtl.rc(v, 0, 255);',
  34995. ' $with = v;',
  34996. ' }',
  34997. '}, 15);',
  34998. '$mod.THelper.SetIt.call({',
  34999. ' p: $mod.o,',
  35000. ' get: function () {',
  35001. ' return this.p.FSize;',
  35002. ' },',
  35003. ' set: function (v) {',
  35004. ' rtl.rc(v, 0, 255);',
  35005. ' this.p.FSize = v;',
  35006. ' }',
  35007. '}, 16);',
  35008. '']));
  35009. end;
  35010. procedure TTestModule.TestRangeChecks_AssignCurrency;
  35011. begin
  35012. Scanner.Options:=Scanner.Options+[po_CAssignments];
  35013. StartProgram(false);
  35014. Add([
  35015. '{$R+}',
  35016. 'var',
  35017. ' c: currency = 2.34;',
  35018. ' i: double;',
  35019. 'procedure DoIt(p: currency);',
  35020. 'begin',
  35021. ' c:=i;',
  35022. ' c+=i;',
  35023. ' c:=1;',
  35024. 'end;',
  35025. '{$R-}',
  35026. 'procedure DoSome;',
  35027. 'begin',
  35028. ' DoIt(i);',
  35029. ' c:=i;',
  35030. ' c:=2;',
  35031. 'end;',
  35032. 'begin',
  35033. '{$R+}',
  35034. '']);
  35035. ConvertProgram;
  35036. CheckSource('TestRangeChecks_AssignCurrency',
  35037. LinesToStr([ // statements
  35038. 'this.c = 2.34;',
  35039. 'this.i = 0.0;',
  35040. 'this.DoIt = function (p) {',
  35041. ' rtl.rc(p, -922337203685477, 922337203685477);',
  35042. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  35043. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  35044. ' $mod.c = 10000;',
  35045. '};',
  35046. 'this.DoSome = function () {',
  35047. ' $mod.DoIt($mod.i * 10000);',
  35048. ' $mod.c = rtl.trunc($mod.i * 10000);',
  35049. ' $mod.c = 20000;',
  35050. '};',
  35051. '']),
  35052. LinesToStr([ // $mod.$main
  35053. '']));
  35054. end;
  35055. procedure TTestModule.TestAsync_Proc;
  35056. begin
  35057. StartProgram(false);
  35058. Add([
  35059. 'procedure Fly(w: word = 1); async; forward;',
  35060. 'procedure Run(w: word = 2); async;',
  35061. 'begin',
  35062. ' Fly(w);',
  35063. ' Fly;',
  35064. ' await(Fly(w));',
  35065. ' await(Fly);',
  35066. 'end;',
  35067. 'procedure Fly(w: word); ',
  35068. 'begin',
  35069. 'end;',
  35070. 'begin',
  35071. ' Run;',
  35072. ' Run(3);',
  35073. '']);
  35074. CheckResolverUnexpectedHints();
  35075. ConvertProgram;
  35076. CheckSource('TestAsync_Proc',
  35077. LinesToStr([ // statements
  35078. 'this.Run = async function (w) {',
  35079. ' $mod.Fly(w);',
  35080. ' $mod.Fly(1);',
  35081. ' await $mod.Fly(w);',
  35082. ' await $mod.Fly(1);',
  35083. '};',
  35084. 'this.Fly = async function (w) {',
  35085. '};',
  35086. '']),
  35087. LinesToStr([
  35088. '$mod.Run(2);',
  35089. '$mod.Run(3);',
  35090. '']));
  35091. end;
  35092. procedure TTestModule.TestAsync_CallFuncResultIsPromise;
  35093. begin
  35094. StartProgram(false);
  35095. Add([
  35096. '{$modeswitch externalclass}',
  35097. 'type',
  35098. ' TObject = class',
  35099. ' end;',
  35100. ' TJSPromise = class external name ''Promise''',
  35101. ' end;',
  35102. ' TBird = class',
  35103. ' function Fly: word; async; ',
  35104. ' end;',
  35105. 'function TBird.Fly: word; async; ',
  35106. 'begin',
  35107. ' Result:=3;',
  35108. ' Fly:=4+Result;',
  35109. ' if Result=5 then ;',
  35110. ' exit(6);',
  35111. 'end;',
  35112. 'function Run: word; async;',
  35113. 'begin',
  35114. ' Result:=11+Result;',
  35115. ' inc(Result);',
  35116. 'end;',
  35117. 'var',
  35118. ' p: TJSPromise;',
  35119. ' o: TBird;',
  35120. 'begin',
  35121. ' p:=Run;',
  35122. ' p:=Run();',
  35123. ' if Run=p then ;',
  35124. ' if p=Run then ;',
  35125. ' if Run()=p then ;',
  35126. ' if p=Run() then ;',
  35127. ' p:=o.Fly;',
  35128. ' p:=o.Fly();',
  35129. ' if o.Fly=p then ;',
  35130. ' if o.Fly()=p then ;',
  35131. ' with o do begin',
  35132. ' p:=Fly;',
  35133. ' p:=Fly();',
  35134. ' if Fly=p then ;',
  35135. ' if Fly()=p then ;',
  35136. ' end;',
  35137. '']);
  35138. CheckResolverUnexpectedHints();
  35139. ConvertProgram;
  35140. CheckSource('TestAsync_CallFuncResultIsPromise',
  35141. LinesToStr([ // statements
  35142. 'rtl.createClass(this, "TObject", null, function () {',
  35143. ' this.$init = function () {',
  35144. ' };',
  35145. ' this.$final = function () {',
  35146. ' };',
  35147. '});',
  35148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35149. ' this.Fly = async function () {',
  35150. ' var Result = 0;',
  35151. ' Result = 3;',
  35152. ' Result = 4 + Result;',
  35153. ' if (Result === 5) ;',
  35154. ' return 6;',
  35155. ' return Result;',
  35156. ' };',
  35157. '});',
  35158. 'this.Run = async function () {',
  35159. ' var Result = 0;',
  35160. ' Result = 11 + Result;',
  35161. ' Result += 1;',
  35162. ' return Result;',
  35163. '};',
  35164. 'this.p = null;',
  35165. 'this.o = null;',
  35166. '']),
  35167. LinesToStr([
  35168. '$mod.p = $mod.Run();',
  35169. '$mod.p = $mod.Run();',
  35170. 'if ($mod.Run() === $mod.p) ;',
  35171. 'if ($mod.p === $mod.Run()) ;',
  35172. 'if ($mod.Run() === $mod.p) ;',
  35173. 'if ($mod.p === $mod.Run()) ;',
  35174. '$mod.p = $mod.o.Fly();',
  35175. '$mod.p = $mod.o.Fly();',
  35176. 'if ($mod.o.Fly() === $mod.p) ;',
  35177. 'if ($mod.o.Fly() === $mod.p) ;',
  35178. 'var $with = $mod.o;',
  35179. '$mod.p = $with.Fly();',
  35180. '$mod.p = $with.Fly();',
  35181. 'if ($with.Fly() === $mod.p) ;',
  35182. 'if ($with.Fly() === $mod.p) ;',
  35183. '']));
  35184. end;
  35185. procedure TTestModule.TestAsync_CallProcResultIsPromise;
  35186. begin
  35187. StartProgram(false);
  35188. Add([
  35189. '{$modeswitch externalclass}',
  35190. 'type',
  35191. ' TObject = class',
  35192. ' end;',
  35193. ' TJSPromise = class external name ''Promise''',
  35194. ' end;',
  35195. ' TBird = class',
  35196. ' procedure Fly; async; ',
  35197. ' end;',
  35198. 'procedure TBird.Fly; async; ',
  35199. 'begin',
  35200. 'end;',
  35201. 'procedure Run; async;',
  35202. 'begin',
  35203. 'end;',
  35204. 'var',
  35205. ' p: TJSPromise;',
  35206. ' o: TBird;',
  35207. 'begin',
  35208. ' p:=Run;',
  35209. ' p:=Run();',
  35210. ' if Run=p then ;',
  35211. ' if p=Run then ;',
  35212. ' if Run()=p then ;',
  35213. ' if p=Run() then ;',
  35214. ' p:=o.Fly;',
  35215. ' p:=o.Fly();',
  35216. ' if o.Fly=p then ;',
  35217. ' if o.Fly()=p then ;',
  35218. ' with o do begin',
  35219. ' p:=Fly;',
  35220. ' p:=Fly();',
  35221. ' if Fly=p then ;',
  35222. ' if Fly()=p then ;',
  35223. ' end;',
  35224. '']);
  35225. CheckResolverUnexpectedHints();
  35226. ConvertProgram;
  35227. CheckSource('TestAsync_CallProcResultIsPromise',
  35228. LinesToStr([ // statements
  35229. 'rtl.createClass(this, "TObject", null, function () {',
  35230. ' this.$init = function () {',
  35231. ' };',
  35232. ' this.$final = function () {',
  35233. ' };',
  35234. '});',
  35235. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35236. ' this.Fly = async function () {',
  35237. ' };',
  35238. '});',
  35239. 'this.Run = async function () {',
  35240. '};',
  35241. 'this.p = null;',
  35242. 'this.o = null;',
  35243. '']),
  35244. LinesToStr([
  35245. '$mod.p = $mod.Run();',
  35246. '$mod.p = $mod.Run();',
  35247. 'if ($mod.Run() === $mod.p) ;',
  35248. 'if ($mod.p === $mod.Run()) ;',
  35249. 'if ($mod.Run() === $mod.p) ;',
  35250. 'if ($mod.p === $mod.Run()) ;',
  35251. '$mod.p = $mod.o.Fly();',
  35252. '$mod.p = $mod.o.Fly();',
  35253. 'if ($mod.o.Fly() === $mod.p) ;',
  35254. 'if ($mod.o.Fly() === $mod.p) ;',
  35255. 'var $with = $mod.o;',
  35256. '$mod.p = $with.Fly();',
  35257. '$mod.p = $with.Fly();',
  35258. 'if ($with.Fly() === $mod.p) ;',
  35259. 'if ($with.Fly() === $mod.p) ;',
  35260. '']));
  35261. end;
  35262. procedure TTestModule.TestAsync_ConstructorFail;
  35263. begin
  35264. StartProgram(false);
  35265. Add([
  35266. 'type',
  35267. ' TObject = class',
  35268. ' end;',
  35269. ' TBird = class',
  35270. ' constructor Create; async;',
  35271. ' end;',
  35272. 'constructor TBird.Create; async;',
  35273. 'begin',
  35274. 'end;',
  35275. 'begin',
  35276. '']);
  35277. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  35278. ConvertProgram;
  35279. end;
  35280. procedure TTestModule.TestAsync_PropertyGetterFail;
  35281. begin
  35282. StartProgram(false);
  35283. Add([
  35284. 'type',
  35285. ' TObject = class',
  35286. ' end;',
  35287. ' TBird = class',
  35288. ' function GetSize: word; async;',
  35289. ' property Size: word read GetSize;',
  35290. ' end;',
  35291. 'function TBird.GetSize: word; async;',
  35292. 'begin',
  35293. 'end;',
  35294. 'begin',
  35295. '']);
  35296. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  35297. ConvertProgram;
  35298. end;
  35299. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  35300. begin
  35301. StartProgram(false);
  35302. Add([
  35303. 'procedure Run; async;',
  35304. 'begin',
  35305. ' await(word,1);',
  35306. 'end;',
  35307. 'begin',
  35308. '']);
  35309. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  35310. ConvertProgram;
  35311. end;
  35312. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  35313. begin
  35314. StartProgram(false);
  35315. Add([
  35316. 'type',
  35317. ' TObject = class',
  35318. ' end;',
  35319. ' TBird = class',
  35320. ' end;',
  35321. 'function Fly: TObject; async;',
  35322. 'begin',
  35323. 'end;',
  35324. 'procedure Run; async;',
  35325. 'begin',
  35326. ' await(TBird,Fly);',
  35327. 'end;',
  35328. 'begin',
  35329. '']);
  35330. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  35331. ConvertProgram;
  35332. end;
  35333. procedure TTestModule.TestAWait_OutsideAsyncFail;
  35334. begin
  35335. StartProgram(false);
  35336. Add([
  35337. 'procedure Crawl(w: double); ',
  35338. 'begin',
  35339. 'end;',
  35340. 'procedure Run(w: double);',
  35341. 'begin',
  35342. ' await(Crawl(w));',
  35343. 'end;',
  35344. 'begin',
  35345. ' Run(1);']);
  35346. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  35347. ConvertProgram;
  35348. end;
  35349. procedure TTestModule.TestAWait_IntegerFail;
  35350. begin
  35351. StartProgram(false);
  35352. Add([
  35353. 'function Run: word;',
  35354. 'begin',
  35355. 'end;',
  35356. 'procedure Fly(w: word); async;',
  35357. 'begin',
  35358. ' await(Run());',
  35359. 'end;',
  35360. 'begin',
  35361. ' Fly(1);']);
  35362. SetExpectedPasResolverError('async function or promise expected, but Result:Word found',nXExpectedButYFound);
  35363. ConvertProgram;
  35364. end;
  35365. procedure TTestModule.TestAWait_ExternalClassPromise;
  35366. begin
  35367. StartProgram(false);
  35368. Add([
  35369. '{$modeswitch externalclass}',
  35370. 'type',
  35371. ' TJSPromise = class external name ''Promise''',
  35372. ' end;',
  35373. ' TJSThenable = class external name ''Thenable''',
  35374. ' end;',
  35375. 'function Fly(w: word): TJSPromise;',
  35376. 'begin',
  35377. 'end;',
  35378. 'function Jump(w: word): word; async;',
  35379. 'begin',
  35380. 'end;',
  35381. 'function Eat(w: word): TJSPromise; async;',
  35382. 'begin',
  35383. 'end;',
  35384. 'function Run(d: double): word; async;',
  35385. 'var',
  35386. ' p: TJSPromise;',
  35387. 'begin',
  35388. ' Result:=await(word,p);', // promise needs type
  35389. ' Result:=await(word,Fly(3));', // promise needs type
  35390. ' Result:=await(Jump(4));', // async non promise can omit the type
  35391. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  35392. ' Result:=await(word,Eat(6));', // promise needs type
  35393. 'end;',
  35394. 'begin',
  35395. '']);
  35396. ConvertProgram;
  35397. CheckSource('TestAWait_ExternalClassPromise',
  35398. LinesToStr([ // statements
  35399. 'this.Fly = function (w) {',
  35400. ' var Result = null;',
  35401. ' return Result;',
  35402. '};',
  35403. 'this.Jump = async function (w) {',
  35404. ' var Result = 0;',
  35405. ' return Result;',
  35406. '};',
  35407. 'this.Eat = async function (w) {',
  35408. ' var Result = null;',
  35409. ' return Result;',
  35410. '};',
  35411. 'this.Run = async function (d) {',
  35412. ' var Result = 0;',
  35413. ' var p = null;',
  35414. ' Result = await p;',
  35415. ' Result = await $mod.Fly(3);',
  35416. ' Result = await $mod.Jump(4);',
  35417. ' Result = await $mod.Jump(5);',
  35418. ' Result = await $mod.Eat(6);',
  35419. ' return Result;',
  35420. '};',
  35421. '']),
  35422. LinesToStr([
  35423. ]));
  35424. CheckResolverUnexpectedHints();
  35425. end;
  35426. procedure TTestModule.TestAWait_JSValue;
  35427. begin
  35428. StartProgram(false);
  35429. Add([
  35430. '{$modeswitch externalclass}',
  35431. 'type',
  35432. ' TJSPromise = class external name ''Promise''',
  35433. ' end;',
  35434. 'function Fly(w: word): jsvalue; async;',
  35435. 'begin',
  35436. 'end;',
  35437. 'function Run(d: jsvalue; var e): word; async;',
  35438. 'begin',
  35439. ' Result:=await(word,d);', // promise needs type
  35440. ' d:=await(Fly(4));', // async non promise must omit the type
  35441. ' Result:=await(word,e);', // promise needs type
  35442. 'end;',
  35443. 'begin',
  35444. '']);
  35445. ConvertProgram;
  35446. CheckSource('TestAWait_JSValue',
  35447. LinesToStr([ // statements
  35448. 'this.Fly = async function (w) {',
  35449. ' var Result = undefined;',
  35450. ' return Result;',
  35451. '};',
  35452. 'this.Run = async function (d, e) {',
  35453. ' var Result = 0;',
  35454. ' Result = await d;',
  35455. ' d = await $mod.Fly(4);',
  35456. ' Result = await e.get();',
  35457. ' return Result;',
  35458. '};',
  35459. '']),
  35460. LinesToStr([
  35461. ]));
  35462. CheckResolverUnexpectedHints();
  35463. end;
  35464. procedure TTestModule.TestAWait_Result;
  35465. begin
  35466. StartProgram(false);
  35467. Add([
  35468. '{$modeswitch externalclass}',
  35469. 'type',
  35470. ' TJSPromise = class external name ''Promise''',
  35471. ' end;',
  35472. 'function Crawl(d: double = 1.3): TJSPromise; ',
  35473. 'begin',
  35474. 'end;',
  35475. 'function Run(d: double = 1.6): word; async;',
  35476. 'begin',
  35477. ' Result:=await(word,Crawl);',
  35478. ' Result:=await(word,Crawl(4.5));',
  35479. ' Result:=await(Run);',
  35480. ' Result:=await(Run(6.7));',
  35481. 'end;',
  35482. 'begin',
  35483. ' Run(1);']);
  35484. ConvertProgram;
  35485. CheckSource('TestAWait_Result',
  35486. LinesToStr([ // statements
  35487. 'this.Crawl = function (d) {',
  35488. ' var Result = null;',
  35489. ' return Result;',
  35490. '};',
  35491. 'this.Run = async function (d) {',
  35492. ' var Result = 0;',
  35493. ' Result = await $mod.Crawl(1.3);',
  35494. ' Result = await $mod.Crawl(4.5);',
  35495. ' Result = await $mod.Run(1.6);',
  35496. ' Result = await $mod.Run(6.7);',
  35497. ' return Result;',
  35498. '};',
  35499. '']),
  35500. LinesToStr([
  35501. '$mod.Run(1);'
  35502. ]));
  35503. CheckResolverUnexpectedHints();
  35504. end;
  35505. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  35506. begin
  35507. StartProgram(false);
  35508. Add([
  35509. '{$mode objfpc}',
  35510. '{$modeswitch externalclass}',
  35511. 'type',
  35512. ' TJSPromise = class external name ''Promise''',
  35513. ' end;',
  35514. 'function Run: TJSPromise; async;',
  35515. 'begin',
  35516. 'end;',
  35517. 'procedure Fly(w: word); async;',
  35518. 'begin',
  35519. ' await(Run());',
  35520. 'end;',
  35521. 'begin',
  35522. ' Fly(1);']);
  35523. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  35524. nWrongNumberOfParametersForCallTo);
  35525. ConvertProgram;
  35526. end;
  35527. procedure TTestModule.TestAsync_AnonymousProc;
  35528. begin
  35529. StartProgram(false);
  35530. Add([
  35531. '{$mode objfpc}',
  35532. '{$modeswitch externalclass}',
  35533. 'type',
  35534. ' TJSPromise = class external name ''Promise''',
  35535. ' end;',
  35536. 'type',
  35537. ' TFunc = reference to function(x: double): word; async;',
  35538. 'function Crawl(d: double = 1.3): word; async;',
  35539. 'begin',
  35540. 'end;',
  35541. 'var Func: TFunc;',
  35542. 'begin',
  35543. ' Func:=function(c:double):word async begin',
  35544. ' Result:=await(Crawl(c));',
  35545. ' end;',
  35546. ' Func:=function(c:double):word async assembler asm',
  35547. ' end;',
  35548. '']);
  35549. ConvertProgram;
  35550. CheckSource('TestAsync_AnonymousProc',
  35551. LinesToStr([ // statements
  35552. 'this.Crawl = async function (d) {',
  35553. ' var Result = 0;',
  35554. ' return Result;',
  35555. '};',
  35556. 'this.Func = null;',
  35557. '']),
  35558. LinesToStr([
  35559. '$mod.Func = async function (c) {',
  35560. ' var Result = 0;',
  35561. ' Result = await $mod.Crawl(c);',
  35562. ' return Result;',
  35563. '};',
  35564. '$mod.Func = async function (c) {',
  35565. '};',
  35566. '']));
  35567. CheckResolverUnexpectedHints();
  35568. end;
  35569. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  35570. begin
  35571. StartProgram(false);
  35572. Add([
  35573. '{$mode objfpc}',
  35574. '{$modeswitch externalclass}',
  35575. 'type',
  35576. ' TJSPromise = class external name ''Promise''',
  35577. ' end;',
  35578. ' TObject = class',
  35579. ' public',
  35580. ' procedure Fly(Prom: TJSPromise);',
  35581. ' end;',
  35582. ' TFunc = reference to procedure(Bird: TObject);',
  35583. 'procedure TObject.Fly(Prom: TJSPromise);',
  35584. 'begin',
  35585. 'end;',
  35586. 'function Crawl: jsvalue; async;',
  35587. 'begin',
  35588. 'end;',
  35589. 'procedure Add(Func: TFunc);',
  35590. 'begin',
  35591. 'end;',
  35592. 'begin',
  35593. ' Add(procedure(Bird: TObject)',
  35594. ' begin',
  35595. ' Bird.Fly(Crawl());',
  35596. ' end);',
  35597. '']);
  35598. ConvertProgram;
  35599. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  35600. LinesToStr([ // statements
  35601. 'rtl.createClass(this, "TObject", null, function () {',
  35602. ' this.$init = function () {',
  35603. ' };',
  35604. ' this.$final = function () {',
  35605. ' };',
  35606. ' this.Fly = function (Prom) {',
  35607. ' };',
  35608. '});',
  35609. 'this.Crawl = async function () {',
  35610. ' var Result = undefined;',
  35611. ' return Result;',
  35612. '};',
  35613. 'this.Add = function (Func) {',
  35614. '};',
  35615. '']),
  35616. LinesToStr([
  35617. '$mod.Add(function (Bird) {',
  35618. ' Bird.Fly($mod.Crawl());',
  35619. '});',
  35620. '']));
  35621. end;
  35622. procedure TTestModule.TestAsync_ProcType;
  35623. begin
  35624. StartProgram(false);
  35625. Add([
  35626. '{$mode objfpc}',
  35627. 'type',
  35628. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  35629. ' TFunc = function(x: double = 1.1): word; async;',
  35630. ' TProc = procedure(x: longint = 7); async;',
  35631. 'function Crawl(d: double): word; async;',
  35632. 'begin',
  35633. 'end;',
  35634. 'procedure Run(e:longint); async;',
  35635. 'begin',
  35636. 'end;',
  35637. 'procedure Fly(p: TProc); async;',
  35638. 'begin',
  35639. ' await(p);',
  35640. ' await(p());',
  35641. 'end;',
  35642. 'var',
  35643. ' RefFunc: TRefFunc;',
  35644. ' Func: TFunc;',
  35645. ' Proc, ProcB: TProc;',
  35646. 'begin',
  35647. ' Func:=@Crawl;',
  35648. ' RefFunc:=@Crawl;',
  35649. ' RefFunc:=function(c:double):word async begin',
  35650. ' Result:=await(RefFunc);',
  35651. ' Result:=await(RefFunc());',
  35652. ' Result:=await(Func);',
  35653. ' Result:=await(Func());',
  35654. ' await(Proc);',
  35655. ' await(Proc());',
  35656. ' await(Proc(13));',
  35657. ' end;',
  35658. ' Proc:=@Run;',
  35659. ' if Proc=ProcB then ;',
  35660. ' ']);
  35661. ConvertProgram;
  35662. CheckResolverUnexpectedHints();
  35663. CheckSource('TestAsync_ProcType',
  35664. LinesToStr([ // statements
  35665. 'this.Crawl = async function (d) {',
  35666. ' var Result = 0;',
  35667. ' return Result;',
  35668. '};',
  35669. 'this.Run = async function (e) {',
  35670. '};',
  35671. 'this.Fly = async function (p) {',
  35672. ' await p(7);',
  35673. ' await p(7);',
  35674. '};',
  35675. 'this.RefFunc = null;',
  35676. 'this.Func = null;',
  35677. 'this.Proc = null;',
  35678. 'this.ProcB = null;',
  35679. '']),
  35680. LinesToStr([
  35681. '$mod.Func = $mod.Crawl;',
  35682. '$mod.RefFunc = $mod.Crawl;',
  35683. '$mod.RefFunc = async function (c) {',
  35684. ' var Result = 0;',
  35685. ' Result = await $mod.RefFunc(1.3);',
  35686. ' Result = await $mod.RefFunc(1.3);',
  35687. ' Result = await $mod.Func(1.1);',
  35688. ' Result = await $mod.Func(1.1);',
  35689. ' await $mod.Proc(7);',
  35690. ' await $mod.Proc(7);',
  35691. ' await $mod.Proc(13);',
  35692. ' return Result;',
  35693. '};',
  35694. '$mod.Proc = $mod.Run;',
  35695. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  35696. '']));
  35697. end;
  35698. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  35699. begin
  35700. StartProgram(false);
  35701. Add([
  35702. '{$mode objfpc}',
  35703. 'type',
  35704. ' TRefFunc = reference to function(x: double = 1.3): word;',
  35705. 'function Crawl(d: double): word; async;',
  35706. 'begin',
  35707. 'end;',
  35708. 'var',
  35709. ' RefFunc: TRefFunc;',
  35710. 'begin',
  35711. ' RefFunc:=@Crawl;',
  35712. ' ']);
  35713. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35714. ConvertProgram;
  35715. end;
  35716. procedure TTestModule.TestAsync_ProcTypeDelphi_NoTJSPromise;
  35717. begin
  35718. StartProgram(false);
  35719. Add([
  35720. '{$mode delphi}',
  35721. 'type',
  35722. ' TRefProc = reference to procedure; async;',
  35723. 'procedure Run(p: TRefProc);',
  35724. 'begin',
  35725. 'end;',
  35726. 'procedure Fly; async;',
  35727. 'begin',
  35728. 'end;',
  35729. 'begin',
  35730. ' Run(Fly);',
  35731. ' ']);
  35732. ConvertProgram;
  35733. end;
  35734. procedure TTestModule.TestAsync_ProcTypeDelphi_TJSPromise;
  35735. begin
  35736. StartProgram(false);
  35737. Add([
  35738. '{$mode delphi}',
  35739. '{$modeswitch externalclass}',
  35740. 'type',
  35741. ' TJSPromise = class external name ''Promise''',
  35742. ' end;',
  35743. ' TRefProc = reference to procedure; async;',
  35744. 'procedure Run(p: TRefProc);',
  35745. 'begin',
  35746. 'end;',
  35747. 'procedure Fly; async;',
  35748. 'begin',
  35749. 'end;',
  35750. 'begin',
  35751. ' Run(Fly);',
  35752. ' ']);
  35753. ConvertProgram;
  35754. end;
  35755. procedure TTestModule.TestAsync_Inherited;
  35756. begin
  35757. StartProgram(false);
  35758. Add([
  35759. '{$mode objfpc}',
  35760. '{$modeswitch externalclass}',
  35761. 'type',
  35762. ' TJSPromise = class external name ''Promise''',
  35763. ' end;',
  35764. ' TObject = class',
  35765. ' function Run(w: word = 3): word; async; virtual;',
  35766. ' end;',
  35767. ' TBird = class',
  35768. ' function Run(w: word = 3): word; async; override;',
  35769. ' end;',
  35770. 'function TObject.Run(w: word = 3): word; async;',
  35771. 'begin',
  35772. 'end;',
  35773. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  35774. 'var p: TJSPromise;',
  35775. 'begin',
  35776. ' p:=inherited;',
  35777. ' p:=inherited Run;',
  35778. ' p:=inherited Run();',
  35779. ' p:=inherited Run(4);',
  35780. ' exit(p);',
  35781. ' exit(inherited);',
  35782. ' exit(inherited Run);',
  35783. ' exit(inherited Run(5));',
  35784. ' exit(6);',
  35785. 'end;',
  35786. 'begin',
  35787. ' ']);
  35788. ConvertProgram;
  35789. CheckSource('TestAsync_Inherited',
  35790. LinesToStr([ // statements
  35791. 'rtl.createClass(this, "TObject", null, function () {',
  35792. ' this.$init = function () {',
  35793. ' };',
  35794. ' this.$final = function () {',
  35795. ' };',
  35796. ' this.Run = async function (w) {',
  35797. ' var Result = 0;',
  35798. ' return Result;',
  35799. ' };',
  35800. '});',
  35801. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35802. ' this.Run = async function (w) {',
  35803. ' var Result = 0;',
  35804. ' var p = null;',
  35805. ' p = $mod.TObject.Run.apply(this, arguments);',
  35806. ' p = $mod.TObject.Run.call(this, 3);',
  35807. ' p = $mod.TObject.Run.call(this, 3);',
  35808. ' p = $mod.TObject.Run.call(this, 4);',
  35809. ' return p;',
  35810. ' return $mod.TObject.Run.apply(this, arguments);',
  35811. ' return $mod.TObject.Run.call(this, 3);',
  35812. ' return $mod.TObject.Run.call(this, 5);',
  35813. ' return 6;',
  35814. ' return Result;',
  35815. ' };',
  35816. '});',
  35817. '']),
  35818. LinesToStr([
  35819. '']));
  35820. CheckResolverUnexpectedHints();
  35821. end;
  35822. procedure TTestModule.TestAsync_ClassInterface;
  35823. begin
  35824. StartProgram(false);
  35825. Add([
  35826. '{$mode objfpc}',
  35827. '{$modeswitch externalclass}',
  35828. 'type',
  35829. ' TJSPromise = class external name ''Promise''',
  35830. ' end;',
  35831. ' IUnknown = interface',
  35832. ' function _AddRef: longint;',
  35833. ' function _Release: longint;',
  35834. ' end;',
  35835. 'function Say(i: IUnknown): IUnknown; async;',
  35836. 'begin',
  35837. 'end;',
  35838. 'function Run: IUnknown; async;',
  35839. 'begin',
  35840. ' Result:=await(Run);',
  35841. ' Result:=await(Run());',
  35842. ' Result:=await(Run) as IUnknown;',
  35843. ' Result:=await(Say(nil));',
  35844. ' Result:=await(Say(await(Run())));',
  35845. ' Result:=await(Say(await(Run()) as IUnknown));',
  35846. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  35847. 'end;',
  35848. 'procedure Fly;',
  35849. 'var p: TJSPromise;',
  35850. 'begin',
  35851. ' Run;',
  35852. ' Run();',
  35853. ' p:=Run;',
  35854. ' p:=Run();',
  35855. 'end;',
  35856. 'begin',
  35857. ' ']);
  35858. ConvertProgram;
  35859. CheckSource('TestAsync_ClassInterface',
  35860. LinesToStr([ // statements
  35861. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  35862. 'this.Say = async function (i) {',
  35863. ' var Result = null;',
  35864. ' return Result;',
  35865. '};',
  35866. 'this.Run = async function () {',
  35867. ' var Result = null;',
  35868. ' var $ok = false;',
  35869. ' try {',
  35870. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35871. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35872. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  35873. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  35874. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  35875. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  35876. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  35877. ' $ok = true;',
  35878. ' } finally {',
  35879. ' if (!$ok) rtl._Release(Result);',
  35880. ' };',
  35881. ' return Result;',
  35882. '};',
  35883. 'this.Fly = function () {',
  35884. ' var p = null;',
  35885. ' $mod.Run();',
  35886. ' $mod.Run();',
  35887. ' p = $mod.Run();',
  35888. ' p = $mod.Run();',
  35889. '};',
  35890. '']),
  35891. LinesToStr([
  35892. '']));
  35893. CheckResolverUnexpectedHints();
  35894. end;
  35895. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  35896. begin
  35897. StartProgram(true,[supTInterfacedObject]);
  35898. Add([
  35899. '{$mode objfpc}',
  35900. '{$modeswitch externalclass}',
  35901. 'type',
  35902. ' TJSPromise = class external name ''Promise''',
  35903. ' end;',
  35904. ' IBird = interface',
  35905. ' procedure Run;',
  35906. ' end;',
  35907. ' TBird = class(TInterfacedObject,IBird)',
  35908. ' procedure Run; async;',
  35909. ' end;',
  35910. 'procedure TBird.Run;',
  35911. 'begin',
  35912. 'end;',
  35913. 'begin',
  35914. ' ']);
  35915. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35916. ConvertProgram;
  35917. end;
  35918. procedure TTestModule.TestAWait_ClassAs;
  35919. begin
  35920. StartProgram(false);
  35921. Add([
  35922. '{$mode objfpc}',
  35923. '{$modeswitch externalclass}',
  35924. 'type',
  35925. ' TJSPromise = class external name ''Promise''',
  35926. ' end;',
  35927. ' TObject = class',
  35928. ' function Run: TObject; async;',
  35929. ' end;',
  35930. ' TBird = class',
  35931. ' function Fly: TBird; async;',
  35932. ' end;',
  35933. 'function TObject.Run: TObject; async;',
  35934. 'begin',
  35935. 'end;',
  35936. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  35937. 'var o: TObject;',
  35938. 'begin',
  35939. ' o:=await(TObject,Run);',
  35940. ' o:=await(TObject,Fly);',
  35941. ' o:=await(TBird,Fly);',
  35942. ' o:=await(TObject,inherited Run);',
  35943. ' o:=await(TObject,inherited Run) as TBird;',
  35944. 'end;',
  35945. 'begin',
  35946. ' ']);
  35947. ConvertProgram;
  35948. CheckSource('TestAWait_ClassAs',
  35949. LinesToStr([ // statements
  35950. 'rtl.createClass(this, "TObject", null, function () {',
  35951. ' this.$init = function () {',
  35952. ' };',
  35953. ' this.$final = function () {',
  35954. ' };',
  35955. ' this.Run = async function () {',
  35956. ' var Result = null;',
  35957. ' return Result;',
  35958. ' };',
  35959. '});',
  35960. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35961. ' this.Fly = async function () {',
  35962. ' var Result = null;',
  35963. ' var o = null;',
  35964. ' o = await this.Run();',
  35965. ' o = await this.Fly();',
  35966. ' o = await this.Fly();',
  35967. ' o = await $mod.TObject.Run.call(this);',
  35968. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  35969. ' return Result;',
  35970. ' };',
  35971. '});',
  35972. '']),
  35973. LinesToStr([
  35974. '']));
  35975. CheckResolverUnexpectedHints();
  35976. end;
  35977. procedure TTestModule.TestLibrary_Empty;
  35978. begin
  35979. StartLibrary(false);
  35980. Add([
  35981. '']);
  35982. ConvertLibrary;
  35983. CheckFullSource('TestLibrary_Empty',
  35984. LinesToStr([ // statements
  35985. 'rtl.module("library", [], function () {',
  35986. ' var $mod = this;',
  35987. ' $mod.$main = function () {',
  35988. ' };',
  35989. '});',
  35990. 'rtl.run("library");',
  35991. '']));
  35992. CheckResolverUnexpectedHints();
  35993. end;
  35994. procedure TTestModule.TestLibrary_ExportFunc;
  35995. begin
  35996. StartLibrary(false);
  35997. Add([
  35998. 'procedure Run(w: word);',
  35999. 'begin',
  36000. 'end;',
  36001. 'exports',
  36002. ' Run;',
  36003. ' run name ''Foo'';',
  36004. ' test1.run name ''Test1Run'';',
  36005. '']);
  36006. ConvertLibrary;
  36007. CheckFullSource('TestLibrary_ExportFunc',
  36008. LinesToStr([ // statements
  36009. 'rtl.module("library", [], function () {',
  36010. ' var $mod = this;',
  36011. ' this.Run = function (w) {',
  36012. ' };',
  36013. ' $mod.$main = function () {',
  36014. ' };',
  36015. '});',
  36016. 'rtl.run("library");',
  36017. 'export const Run = pas.library.Run;',
  36018. 'export const Foo = pas.library.Run;',
  36019. 'export const Test1Run = pas.library.Run;',
  36020. '']));
  36021. CheckResolverUnexpectedHints();
  36022. end;
  36023. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  36024. begin
  36025. StartLibrary(false);
  36026. Add([
  36027. 'procedure Run(w: word); overload;',
  36028. 'begin',
  36029. 'end;',
  36030. 'procedure Run(s: string); overload;',
  36031. 'begin',
  36032. 'end;',
  36033. 'exports',
  36034. ' Run;',
  36035. '']);
  36036. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  36037. nCantDetermineWhichOverloadedFunctionToCall);
  36038. ConvertLibrary;
  36039. end;
  36040. procedure TTestModule.TestLibrary_Export_Index_Fail;
  36041. begin
  36042. StartLibrary(false);
  36043. Add([
  36044. 'procedure Run(w: word);',
  36045. 'begin',
  36046. 'end;',
  36047. 'exports',
  36048. ' Run index 3;',
  36049. '']);
  36050. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  36051. ConvertLibrary;
  36052. end;
  36053. procedure TTestModule.TestLibrary_ExportVar;
  36054. begin
  36055. StartLibrary(false);
  36056. Add([
  36057. 'var Wing: word;',
  36058. 'exports',
  36059. ' Wing, wing name ''BirdArm'';',
  36060. '']);
  36061. ConvertLibrary;
  36062. CheckFullSource('TestLibrary_ExportVar',
  36063. LinesToStr([ // statements
  36064. 'rtl.module("library", [], function () {',
  36065. ' var $mod = this;',
  36066. ' this.Wing = 0;',
  36067. ' $mod.$main = function () {',
  36068. ' };',
  36069. '});',
  36070. 'rtl.run("library");',
  36071. 'export const vars = {};',
  36072. 'Object.defineProperties(vars, {',
  36073. ' Wing: {',
  36074. ' enumerable: true,',
  36075. ' get: function () {',
  36076. ' return pas.library.Wing;',
  36077. ' },',
  36078. ' set: function (v) {',
  36079. ' pas.library.Wing = v;',
  36080. ' }',
  36081. ' },',
  36082. ' BirdArm: {',
  36083. ' enumerable: true,',
  36084. ' get: function () {',
  36085. ' return pas.library.Wing;',
  36086. ' },',
  36087. ' set: function (v) {',
  36088. ' pas.library.Wing = v;',
  36089. ' }',
  36090. ' }',
  36091. '});',
  36092. '']));
  36093. CheckResolverUnexpectedHints();
  36094. end;
  36095. procedure TTestModule.TestLibrary_ExportUnitFunc;
  36096. begin
  36097. AddModuleWithIntfImplSrc('Unit1.pas',
  36098. LinesToStr([
  36099. 'type',
  36100. ' TAnt = class',
  36101. ' class function Crawl: word; static;',
  36102. ' end;',
  36103. 'function Fly: word;',
  36104. '']),
  36105. LinesToStr([
  36106. 'function Fly: word;',
  36107. 'begin',
  36108. 'end;',
  36109. 'class function TAnt.Crawl: word;',
  36110. 'begin',
  36111. 'end;',
  36112. '']));
  36113. StartLibrary(true,[supTObject]);
  36114. Add([
  36115. 'uses unit1;',
  36116. 'exports',
  36117. ' Fly;',
  36118. ' TAnt.Crawl;',
  36119. '']);
  36120. ConvertLibrary;
  36121. CheckFullSource('TestLibrary_ExportUnitFunc',
  36122. LinesToStr([ // statements
  36123. 'rtl.module("library", ["system", "Unit1"], function () {',
  36124. ' var $mod = this;',
  36125. ' $mod.$main = function () {',
  36126. ' };',
  36127. '});',
  36128. 'rtl.run("library");',
  36129. 'export const Fly = pas.Unit1.Fly;',
  36130. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  36131. '']));
  36132. CheckResolverUnexpectedHints();
  36133. end;
  36134. Initialization
  36135. RegisterTests([TTestModule]);
  36136. end.