tcmodules.pas 856 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject
  47. );
  48. TSystemUnitParts = set of TSystemUnitPart;
  49. { TTestHintMessage }
  50. TTestHintMessage = class
  51. public
  52. Id: int64;
  53. MsgType: TMessageType;
  54. MsgNumber: integer;
  55. Msg: string;
  56. SourcePos: TPasSourcePos;
  57. end;
  58. { TTestPasParser }
  59. TTestPasParser = Class(TPasParser)
  60. end;
  61. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  62. { TTestEnginePasResolver }
  63. TTestEnginePasResolver = class(TPas2JsResolver)
  64. private
  65. FFilename: string;
  66. FModule: TPasModule;
  67. FOnFindUnit: TOnFindUnit;
  68. FParser: TTestPasParser;
  69. FStreamResolver: TStreamResolver;
  70. FScanner: TPas2jsPasScanner;
  71. FSource: string;
  72. public
  73. destructor Destroy; override;
  74. function FindUnit(const AName, InFilename: String; NameExpr,
  75. InFileExpr: TPasExpr): TPasModule; override;
  76. procedure UsedInterfacesFinished(Section: TPasSection); override;
  77. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  78. property Filename: string read FFilename write FFilename;
  79. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  80. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  81. property Parser: TTestPasParser read FParser write FParser;
  82. property Source: string read FSource write FSource;
  83. property Module: TPasModule read FModule;
  84. end;
  85. { TCustomTestModule }
  86. TCustomTestModule = Class(TTestCase)
  87. private
  88. FConverter: TPasToJSConverter;
  89. FEngine: TTestEnginePasResolver;
  90. FExpectedErrorClass: ExceptClass;
  91. FExpectedErrorMsg: string;
  92. FExpectedErrorNumber: integer;
  93. FFilename: string;
  94. FFileResolver: TStreamResolver;
  95. FHub: TPas2JSResolverHub;
  96. FJSImplementationUses: TJSArrayLiteral;
  97. FJSInitBody: TJSFunctionBody;
  98. FJSImplentationUses: TJSArrayLiteral;
  99. FJSInterfaceUses: TJSArrayLiteral;
  100. FJSModule: TJSSourceElements;
  101. FJSModuleSrc: TJSSourceElements;
  102. FJSSource: TStringList;
  103. FModule: TPasModule;
  104. FJSModuleCallArgs: TJSArguments;
  105. FModules: TObjectList;// list of TTestEnginePasResolver
  106. FParser: TTestPasParser;
  107. FPasProgram: TPasProgram;
  108. FHintMsgs: TObjectList; // list of TTestHintMessage
  109. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  110. FJSRegModuleCall: TJSCallExpression;
  111. FScanner: TPas2jsPasScanner;
  112. FSkipTests: boolean;
  113. FSource: TStringList;
  114. FFirstPasStatement: TPasImplBlock;
  115. FWithTypeInfo: boolean;
  116. {$IFDEF EnablePasTreeGlobalRefCount}
  117. FElementRefCountAtSetup: int64;
  118. {$ENDIF}
  119. function GetMsgCount: integer;
  120. function GetMsgs(Index: integer): TTestHintMessage;
  121. function GetResolverCount: integer;
  122. function GetResolvers(Index: integer): TTestEnginePasResolver;
  123. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  124. procedure OnParserLog(Sender: TObject; const Msg: String);
  125. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  126. procedure OnScannerLog(Sender: TObject; const Msg: String);
  127. procedure SetWithTypeInfo(const AValue: boolean);
  128. protected
  129. procedure SetUp; override;
  130. function CreateConverter: TPasToJSConverter; virtual;
  131. function LoadUnit(const aUnitName: String): TPasModule;
  132. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  133. procedure TearDown; override;
  134. Procedure Add(Line: string); virtual;
  135. Procedure Add(const Lines: array of string);
  136. Procedure StartParsing; virtual;
  137. procedure ParseModuleQueue; virtual;
  138. procedure ParseModule; virtual;
  139. procedure ParseProgram; virtual;
  140. procedure ParseUnit; virtual;
  141. protected
  142. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  143. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  146. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  147. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  148. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  149. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure ConvertModule; virtual;
  151. procedure ConvertProgram; virtual;
  152. procedure ConvertUnit; virtual;
  153. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  154. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  155. function GetDottedIdentifier(El: TJSElement): string;
  156. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  157. ImplStatements: string = ''); virtual;
  158. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  159. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  160. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  161. Msg: string; Marker: PSrcMarker = nil); virtual;
  162. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  163. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  164. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  167. function IsErrorExpected(E: Exception): boolean;
  168. procedure HandleScannerError(E: EScannerError);
  169. procedure HandleParserError(E: EParserError);
  170. procedure HandlePasResolveError(E: EPasResolve);
  171. procedure HandlePas2JSError(E: EPas2JS);
  172. procedure HandleException(E: Exception);
  173. procedure FailException(E: Exception);
  174. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  175. function IndexOfResolver(const Filename: string): integer;
  176. function GetResolver(const Filename: string): TTestEnginePasResolver;
  177. function GetDefaultNamespace: string;
  178. property PasProgram: TPasProgram Read FPasProgram;
  179. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  180. property ResolverCount: integer read GetResolverCount;
  181. property Engine: TTestEnginePasResolver read FEngine;
  182. property Filename: string read FFilename;
  183. Property Module: TPasModule Read FModule;
  184. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  185. property Converter: TPasToJSConverter read FConverter;
  186. property JSSource: TStringList read FJSSource;
  187. property JSModule: TJSSourceElements read FJSModule;
  188. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  189. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  190. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  191. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  192. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  193. property JSInitBody: TJSFunctionBody read FJSInitBody;
  194. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  195. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  196. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  197. property SkipTests: boolean read FSkipTests write FSkipTests;
  198. public
  199. constructor Create; override;
  200. destructor Destroy; override;
  201. property Hub: TPas2JSResolverHub read FHub;
  202. property Source: TStringList read FSource;
  203. property FileResolver: TStreamResolver read FFileResolver;
  204. property Scanner: TPas2jsPasScanner read FScanner;
  205. property Parser: TTestPasParser read FParser;
  206. property MsgCount: integer read GetMsgCount;
  207. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  208. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  209. end;
  210. { TTestModule }
  211. TTestModule = class(TCustomTestModule)
  212. Published
  213. Procedure TestReservedWords;
  214. // program/units
  215. Procedure TestEmptyProgram;
  216. Procedure TestEmptyProgramUseStrict;
  217. Procedure TestEmptyUnit;
  218. Procedure TestEmptyUnitUseStrict;
  219. Procedure TestDottedUnitNames;
  220. Procedure TestDottedUnitNameImpl;
  221. Procedure TestDottedUnitExpr;
  222. Procedure Test_ModeFPCFail;
  223. Procedure Test_ModeSwitchCBlocksFail;
  224. Procedure TestUnit_UseSystem;
  225. Procedure TestUnit_Intf1Impl2Intf1;
  226. Procedure TestIncludeVersion;
  227. // vars/const
  228. Procedure TestVarInt;
  229. Procedure TestVarBaseTypes;
  230. Procedure TestBaseTypeSingleFail;
  231. Procedure TestBaseTypeExtendedFail;
  232. Procedure TestConstBaseTypes;
  233. Procedure TestUnitImplVars;
  234. Procedure TestUnitImplConsts;
  235. Procedure TestUnitImplRecord;
  236. Procedure TestRenameJSNameConflict;
  237. Procedure TestLocalConst;
  238. Procedure TestVarExternal;
  239. Procedure TestVarExternalOtherUnit;
  240. Procedure TestVarAbsoluteFail;
  241. Procedure TestConstExternal;
  242. // numbers
  243. Procedure TestDouble;
  244. Procedure TestInteger;
  245. Procedure TestIntegerRange;
  246. Procedure TestIntegerTypecasts;
  247. Procedure TestInteger_BitwiseShrNativeInt;
  248. Procedure TestInteger_BitwiseShlNativeInt;
  249. Procedure TestInteger_SystemFunc;
  250. Procedure TestCurrency;
  251. Procedure TestForBoolDo;
  252. Procedure TestForIntDo;
  253. Procedure TestForIntInDo;
  254. // strings
  255. Procedure TestCharConst;
  256. Procedure TestChar_Compare;
  257. Procedure TestChar_BuiltInProcs;
  258. Procedure TestStringConst;
  259. Procedure TestStringConstSurrogate;
  260. Procedure TestString_Length;
  261. Procedure TestString_Compare;
  262. Procedure TestString_SetLength;
  263. Procedure TestString_CharAt;
  264. Procedure TestStringHMinusFail;
  265. Procedure TestStr;
  266. Procedure TestBaseType_AnsiStringFail;
  267. Procedure TestBaseType_WideStringFail;
  268. Procedure TestBaseType_ShortStringFail;
  269. Procedure TestBaseType_RawByteStringFail;
  270. Procedure TestTypeShortstring_Fail;
  271. Procedure TestCharSet_Custom;
  272. Procedure TestForCharDo;
  273. Procedure TestForCharInDo;
  274. // alias types
  275. Procedure TestAliasTypeRef;
  276. Procedure TestTypeCast_BaseTypes;
  277. Procedure TestTypeCast_AliasBaseTypes;
  278. // functions
  279. Procedure TestEmptyProc;
  280. Procedure TestProcOneParam;
  281. Procedure TestFunctionWithoutParams;
  282. Procedure TestProcedureWithoutParams;
  283. Procedure TestPrgProcVar;
  284. Procedure TestProcTwoArgs;
  285. Procedure TestProc_DefaultValue;
  286. Procedure TestUnitProcVar;
  287. Procedure TestImplProc;
  288. Procedure TestFunctionResult;
  289. Procedure TestNestedProc;
  290. Procedure TestNestedProc_ResultString;
  291. Procedure TestForwardProc;
  292. Procedure TestNestedForwardProc;
  293. Procedure TestAssignFunctionResult;
  294. Procedure TestFunctionResultInCondition;
  295. Procedure TestFunctionResultInForLoop;
  296. Procedure TestFunctionResultInTypeCast;
  297. Procedure TestExit;
  298. Procedure TestExit_ResultInFinally;
  299. Procedure TestBreak;
  300. Procedure TestBreakAsVar;
  301. Procedure TestContinue;
  302. Procedure TestProc_External;
  303. Procedure TestProc_ExternalOtherUnit;
  304. Procedure TestProc_Asm;
  305. Procedure TestProc_Assembler;
  306. Procedure TestProc_VarParam;
  307. Procedure TestProc_VarParamString;
  308. Procedure TestProc_VarParamV;
  309. Procedure TestProc_Overload;
  310. Procedure TestProc_OverloadForward;
  311. Procedure TestProc_OverloadIntfImpl;
  312. Procedure TestProc_OverloadNested;
  313. Procedure TestProc_OverloadNestedForward;
  314. Procedure TestProc_OverloadUnitCycle;
  315. Procedure TestProc_Varargs;
  316. Procedure TestProc_ConstOrder;
  317. Procedure TestProc_DuplicateConst;
  318. Procedure TestProc_LocalVarAbsolute;
  319. Procedure TestProc_LocalVarInit;
  320. Procedure TestProc_ReservedWords;
  321. Procedure TestProc_ConstRefWord;
  322. // anonymous functions
  323. Procedure TestAnonymousProc_Assign_ObjFPC;
  324. Procedure TestAnonymousProc_Assign_Delphi;
  325. Procedure TestAnonymousProc_Arg;
  326. Procedure TestAnonymousProc_Typecast;
  327. Procedure TestAnonymousProc_With;
  328. Procedure TestAnonymousProc_ExceptOn;
  329. Procedure TestAnonymousProc_Nested;
  330. Procedure TestAnonymousProc_NestedAssignResult;
  331. Procedure TestAnonymousProc_Class;
  332. Procedure TestAnonymousProc_ForLoop;
  333. // enums, sets
  334. Procedure TestEnum_Name;
  335. Procedure TestEnum_Number;
  336. Procedure TestEnum_ConstFail;
  337. Procedure TestEnum_Functions;
  338. Procedure TestEnumRg_Functions;
  339. Procedure TestEnum_AsParams;
  340. Procedure TestEnumRange_Array;
  341. Procedure TestEnum_ForIn;
  342. Procedure TestEnum_ScopedNumber;
  343. Procedure TestEnum_InFunction;
  344. Procedure TestSet_Enum;
  345. Procedure TestSet_Operators;
  346. Procedure TestSet_Operator_In;
  347. Procedure TestSet_Functions;
  348. Procedure TestSet_PassAsArgClone;
  349. Procedure TestSet_AsParams;
  350. Procedure TestSet_Property;
  351. Procedure TestSet_EnumConst;
  352. Procedure TestSet_IntConst;
  353. Procedure TestSet_AnonymousEnumType;
  354. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  355. Procedure TestSet_ConstEnum;
  356. Procedure TestSet_ConstChar;
  357. Procedure TestSet_ConstInt;
  358. Procedure TestSet_InFunction;
  359. Procedure TestSet_ForIn;
  360. // statements
  361. Procedure TestNestBegin;
  362. Procedure TestIncDec;
  363. Procedure TestLoHiFpcMode;
  364. Procedure TestLoHiDelphiMode;
  365. Procedure TestAssignments;
  366. Procedure TestArithmeticOperators1;
  367. Procedure TestLogicalOperators;
  368. Procedure TestBitwiseOperators;
  369. Procedure TestBitwiseOperatorsLongword;
  370. Procedure TestFunctionInt;
  371. Procedure TestFunctionString;
  372. Procedure TestIfThen;
  373. Procedure TestForLoop;
  374. Procedure TestForLoopInsideFunction;
  375. Procedure TestForLoop_ReadVarAfter;
  376. Procedure TestForLoop_Nested;
  377. Procedure TestRepeatUntil;
  378. Procedure TestAsmBlock;
  379. Procedure TestAsmPas_Impl; // ToDo
  380. Procedure TestTryFinally;
  381. Procedure TestTryExcept;
  382. Procedure TestTryExcept_ReservedWords;
  383. Procedure TestIfThenRaiseElse;
  384. Procedure TestCaseOf;
  385. Procedure TestCaseOf_UseSwitch;
  386. Procedure TestCaseOfNoElse;
  387. Procedure TestCaseOfNoElse_UseSwitch;
  388. Procedure TestCaseOfRange;
  389. Procedure TestCaseOfString;
  390. Procedure TestCaseOfChar;
  391. Procedure TestCaseOfExternalClassConst;
  392. Procedure TestDebugger;
  393. // arrays
  394. Procedure TestArray_Dynamic;
  395. Procedure TestArray_Dynamic_Nil;
  396. Procedure TestArray_DynMultiDimensional;
  397. Procedure TestArray_DynamicAssign;
  398. Procedure TestArray_StaticInt;
  399. Procedure TestArray_StaticBool;
  400. Procedure TestArray_StaticChar;
  401. Procedure TestArray_StaticMultiDim;
  402. Procedure TestArray_StaticInFunction;
  403. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  404. Procedure TestArrayOfRecord;
  405. Procedure TestArray_StaticRecord;
  406. Procedure TestArrayOfSet;
  407. Procedure TestArray_DynAsParam;
  408. Procedure TestArray_StaticAsParam;
  409. Procedure TestArrayElement_AsParams;
  410. Procedure TestArrayElementFromFuncResult_AsParams;
  411. Procedure TestArrayEnumTypeRange;
  412. Procedure TestArray_SetLengthOutArg;
  413. Procedure TestArray_SetLengthProperty;
  414. Procedure TestArray_SetLengthMultiDim;
  415. Procedure TestArray_SetLengthDynOfStatic;
  416. Procedure TestArray_OpenArrayOfString;
  417. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  418. Procedure TestArray_ConstRef;
  419. Procedure TestArray_Concat;
  420. Procedure TestArray_Copy;
  421. Procedure TestArray_InsertDelete;
  422. Procedure TestArray_DynArrayConstObjFPC;
  423. Procedure TestArray_DynArrayConstDelphi;
  424. Procedure TestArray_ArrayLitAsParam;
  425. Procedure TestArray_ArrayLitMultiDimAsParam;
  426. Procedure TestArray_ArrayLitStaticAsParam;
  427. Procedure TestArray_ForInArrOfString;
  428. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  429. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  430. Procedure TestArrayOfConst_TVarRec;
  431. Procedure TestArrayOfConst_PassBaseTypes;
  432. Procedure TestArrayOfConst_PassObj;
  433. // record
  434. Procedure TestRecord_Empty;
  435. Procedure TestRecord_Var;
  436. Procedure TestRecord_VarExternal;
  437. Procedure TestRecord_WithDo;
  438. Procedure TestRecord_Assign;
  439. Procedure TestRecord_AsParams;
  440. Procedure TestRecord_ConstRef;
  441. Procedure TestRecordElement_AsParams;
  442. Procedure TestRecordElementFromFuncResult_AsParams;
  443. Procedure TestRecordElementFromWith_AsParams;
  444. Procedure TestRecord_Equal;
  445. Procedure TestRecord_JSValue;
  446. Procedure TestRecord_VariantFail;
  447. Procedure TestRecord_FieldArray;
  448. Procedure TestRecord_Const;
  449. Procedure TestRecord_TypecastFail;
  450. Procedure TestRecord_InFunction;
  451. Procedure TestRecord_AnonymousFail;
  452. // advanced record
  453. Procedure TestAdvRecord_Function;
  454. Procedure TestAdvRecord_Property;
  455. Procedure TestAdvRecord_PropertyDefault;
  456. Procedure TestAdvRecord_Property_ClassMethod;
  457. Procedure TestAdvRecord_Const;
  458. Procedure TestAdvRecord_ExternalField;
  459. Procedure TestAdvRecord_SubRecord;
  460. Procedure TestAdvRecord_SubClass;
  461. Procedure TestAdvRecord_SubInterfaceFail;
  462. Procedure TestAdvRecord_Constructor;
  463. Procedure TestAdvRecord_ClassConstructor_Program;
  464. Procedure TestAdvRecord_ClassConstructor_Unit;
  465. // classes
  466. Procedure TestClass_TObjectDefaultConstructor;
  467. Procedure TestClass_TObjectConstructorWithParams;
  468. Procedure TestClass_TObjectConstructorWithDefaultParam;
  469. Procedure TestClass_Var;
  470. Procedure TestClass_Method;
  471. Procedure TestClass_Implementation;
  472. Procedure TestClass_Inheritance;
  473. Procedure TestClass_TypeAlias;
  474. Procedure TestClass_AbstractMethod;
  475. Procedure TestClass_CallInherited_ProcNoParams;
  476. Procedure TestClass_CallInherited_WithParams;
  477. Procedure TestClasS_CallInheritedConstructor;
  478. Procedure TestClass_ClassVar_Assign;
  479. Procedure TestClass_CallClassMethod;
  480. Procedure TestClass_Property;
  481. Procedure TestClass_Property_ClassMethod;
  482. Procedure TestClass_Property_Indexed;
  483. Procedure TestClass_Property_IndexSpec;
  484. Procedure TestClass_PropertyOfTypeArray;
  485. Procedure TestClass_PropertyDefault;
  486. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  487. //Procedure TestClass_PropertyDefault;
  488. Procedure TestClass_PropertyOverride;
  489. Procedure TestClass_PropertyIncVisibility;
  490. Procedure TestClass_Assigned;
  491. Procedure TestClass_WithClassDoCreate;
  492. Procedure TestClass_WithClassInstDoProperty;
  493. Procedure TestClass_WithClassInstDoPropertyWithParams;
  494. Procedure TestClass_WithClassInstDoFunc;
  495. Procedure TestClass_TypeCast;
  496. Procedure TestClass_TypeCastUntypedParam;
  497. Procedure TestClass_Overloads;
  498. Procedure TestClass_OverloadsAncestor;
  499. Procedure TestClass_OverloadConstructor;
  500. Procedure TestClass_OverloadDelphiOverride;
  501. Procedure TestClass_ReintroduceVarDelphi;
  502. Procedure TestClass_ReintroducedVar;
  503. Procedure TestClass_RaiseDescendant;
  504. Procedure TestClass_ExternalMethod;
  505. Procedure TestClass_ExternalVirtualNameMismatchFail;
  506. Procedure TestClass_ExternalOverrideFail;
  507. Procedure TestClass_ExternalVar;
  508. Procedure TestClass_Const;
  509. Procedure TestClass_ConstEnum;
  510. Procedure TestClass_LocalConstDuplicate_Prg;
  511. Procedure TestClass_LocalConstDuplicate_Unit;
  512. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  513. Procedure TestClass_LocalVarSelfFail;
  514. Procedure TestClass_ArgSelfFail;
  515. Procedure TestClass_NestedProcSelf;
  516. Procedure TestClass_NestedProcSelf2;
  517. Procedure TestClass_NestedProcClassSelf;
  518. Procedure TestClass_NestedProcCallInherited;
  519. Procedure TestClass_TObjectFree;
  520. Procedure TestClass_TObjectFree_VarArg;
  521. Procedure TestClass_TObjectFreeNewInstance;
  522. Procedure TestClass_TObjectFreeLowerCase;
  523. Procedure TestClass_TObjectFreeFunctionFail;
  524. Procedure TestClass_TObjectFreePropertyFail;
  525. Procedure TestClass_ForIn;
  526. Procedure TestClass_DispatchMessage;
  527. Procedure TestClass_Message_DuplicateIntFail;
  528. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  529. // class of
  530. Procedure TestClassOf_Create;
  531. Procedure TestClassOf_Call;
  532. Procedure TestClassOf_Assign;
  533. Procedure TestClassOf_Is;
  534. Procedure TestClassOf_Compare;
  535. Procedure TestClassOf_ClassVar;
  536. Procedure TestClassOf_ClassMethod;
  537. Procedure TestClassOf_ClassProperty;
  538. Procedure TestClassOf_ClassMethodSelf;
  539. Procedure TestClassOf_TypeCast;
  540. Procedure TestClassOf_ImplicitFunctionCall;
  541. Procedure TestClassOf_Const;
  542. // nested class
  543. Procedure TestNestedClass_Alias;
  544. Procedure TestNestedClass_Record;
  545. Procedure TestNestedClass_Class;
  546. // external class
  547. Procedure TestExternalClass_Var;
  548. Procedure TestExternalClass_Const;
  549. Procedure TestExternalClass_Dollar;
  550. Procedure TestExternalClass_DuplicateVarFail;
  551. Procedure TestExternalClass_Method;
  552. Procedure TestExternalClass_ClassMethod;
  553. Procedure TestExternalClass_ClassMethodStatic;
  554. Procedure TestExternalClass_FunctionResultInTypeCast;
  555. Procedure TestExternalClass_NonExternalOverride;
  556. Procedure TestExternalClass_OverloadHint;
  557. Procedure TestExternalClass_SameNamePublishedProperty;
  558. Procedure TestExternalClass_Property;
  559. Procedure TestExternalClass_PropertyDate;
  560. Procedure TestExternalClass_ClassProperty;
  561. Procedure TestExternalClass_ClassOf;
  562. Procedure TestExternalClass_ClassOtherUnit;
  563. Procedure TestExternalClass_Is;
  564. Procedure TestExternalClass_As;
  565. Procedure TestExternalClass_DestructorFail;
  566. Procedure TestExternalClass_New;
  567. Procedure TestExternalClass_ClassOf_New;
  568. Procedure TestExternalClass_FuncClassOf_New;
  569. Procedure TestExternalClass_New_PasClassFail;
  570. Procedure TestExternalClass_New_PasClassBracketsFail;
  571. Procedure TestExternalClass_NewExtName;
  572. Procedure TestExternalClass_Constructor;
  573. Procedure TestExternalClass_ConstructorBrackets;
  574. Procedure TestExternalClass_LocalConstSameName;
  575. Procedure TestExternalClass_ReintroduceOverload;
  576. Procedure TestExternalClass_Inherited;
  577. Procedure TestExternalClass_PascalAncestorFail;
  578. Procedure TestExternalClass_NewInstance;
  579. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  580. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  581. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  582. Procedure TestExternalClass_JSFunctionPasDescendant;
  583. Procedure TestExternalClass_PascalProperty;
  584. Procedure TestExternalClass_TypeCastToRootClass;
  585. Procedure TestExternalClass_TypeCastToJSObject;
  586. Procedure TestExternalClass_TypeCastStringToExternalString;
  587. Procedure TestExternalClass_TypeCastToJSFunction;
  588. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  589. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  590. Procedure TestExternalClass_BracketAccessor;
  591. Procedure TestExternalClass_BracketAccessor_Call;
  592. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  593. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  594. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  595. Procedure TestExternalClass_BracketAccessor_MultiType;
  596. Procedure TestExternalClass_BracketAccessor_Index;
  597. Procedure TestExternalClass_ForInJSObject;
  598. Procedure TestExternalClass_ForInJSArray;
  599. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  600. // class interfaces
  601. Procedure TestClassInterface_Corba;
  602. Procedure TestClassInterface_ProcExternalFail;
  603. Procedure TestClassInterface_Overloads;
  604. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  605. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  606. Procedure TestClassInterface_AncestorImpl;
  607. Procedure TestClassInterface_ImplReintroduce;
  608. Procedure TestClassInterface_MethodResolution;
  609. Procedure TestClassInterface_AncestorMoreInterfaces;
  610. Procedure TestClassInterface_MethodOverride;
  611. Procedure TestClassInterface_Corba_Delegation;
  612. Procedure TestClassInterface_Corba_DelegationStatic;
  613. Procedure TestClassInterface_Corba_Operators;
  614. Procedure TestClassInterface_Corba_Args;
  615. Procedure TestClassInterface_Corba_ForIn;
  616. Procedure TestClassInterface_COM_AssignVar;
  617. Procedure TestClassInterface_COM_AssignArg;
  618. Procedure TestClassInterface_COM_FunctionResult;
  619. Procedure TestClassInterface_COM_InheritedFuncResult;
  620. Procedure TestClassInterface_COM_IsAsTypeCasts;
  621. Procedure TestClassInterface_COM_PassAsArg;
  622. Procedure TestClassInterface_COM_PassToUntypedParam;
  623. Procedure TestClassInterface_COM_FunctionInExpr;
  624. Procedure TestClassInterface_COM_Property;
  625. Procedure TestClassInterface_COM_IntfProperty;
  626. Procedure TestClassInterface_COM_Delegation;
  627. Procedure TestClassInterface_COM_With;
  628. Procedure TestClassInterface_COM_ForIn;
  629. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  630. Procedure TestClassInterface_COM_RecordIntfFail;
  631. Procedure TestClassInterface_COM_UnitInitialization;
  632. Procedure TestClassInterface_GUID;
  633. Procedure TestClassInterface_GUIDProperty;
  634. // helpers
  635. Procedure TestClassHelper_ClassVar;
  636. Procedure TestClassHelper_Method_AccessInstanceFields;
  637. Procedure TestClassHelper_Method_Call;
  638. Procedure TestClassHelper_Method_Nested_Call;
  639. Procedure TestClassHelper_ClassMethod_Call;
  640. Procedure TestClassHelper_ClassOf;
  641. Procedure TestClassHelper_MethodRefObjFPC;
  642. Procedure TestClassHelper_Constructor;
  643. Procedure TestClassHelper_InheritedObjFPC;
  644. Procedure TestClassHelper_Property;
  645. Procedure TestClassHelper_Property_Array;
  646. Procedure TestClassHelper_Property_Array_Default;
  647. Procedure TestClassHelper_Property_Array_DefaultDefault;
  648. Procedure TestClassHelper_ClassProperty;
  649. Procedure TestClassHelper_ClassPropertyStatic;
  650. Procedure TestClassHelper_ClassProperty_Array;
  651. Procedure TestClassHelper_ForIn;
  652. Procedure TestClassHelper_PassProperty;
  653. Procedure TestExtClassHelper_ClassVar;
  654. Procedure TestExtClassHelper_Method_Call;
  655. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  656. Procedure TestRecordHelper_ClassVar;
  657. Procedure TestRecordHelper_Method_Call;
  658. Procedure TestRecordHelper_Constructor;
  659. Procedure TestTypeHelper_ClassVar;
  660. Procedure TestTypeHelper_PassResultElement;
  661. Procedure TestTypeHelper_PassArgs;
  662. Procedure TestTypeHelper_PassVarConst;
  663. Procedure TestTypeHelper_PassFuncResult;
  664. Procedure TestTypeHelper_PassPropertyField;
  665. Procedure TestTypeHelper_PassPropertyGetter;
  666. Procedure TestTypeHelper_PassClassPropertyField;
  667. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  668. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  669. Procedure TestTypeHelper_Property;
  670. Procedure TestTypeHelper_Property_Array;
  671. Procedure TestTypeHelper_ClassProperty;
  672. Procedure TestTypeHelper_ClassProperty_Array;
  673. Procedure TestTypeHelper_ClassMethod;
  674. Procedure TestTypeHelper_ExtClassMethodFail;
  675. Procedure TestTypeHelper_Constructor;
  676. Procedure TestTypeHelper_Word;
  677. Procedure TestTypeHelper_Boolean;
  678. Procedure TestTypeHelper_WordBool;
  679. Procedure TestTypeHelper_Double;
  680. Procedure TestTypeHelper_NativeInt;
  681. Procedure TestTypeHelper_StringChar;
  682. Procedure TestTypeHelper_JSValue;
  683. Procedure TestTypeHelper_Array;
  684. Procedure TestTypeHelper_EnumType;
  685. Procedure TestTypeHelper_SetType;
  686. Procedure TestTypeHelper_InterfaceType;
  687. Procedure TestTypeHelper_NestedSelf;
  688. // proc types
  689. Procedure TestProcType;
  690. Procedure TestProcType_Arg;
  691. Procedure TestProcType_FunctionFPC;
  692. Procedure TestProcType_FunctionDelphi;
  693. Procedure TestProcType_ProcedureDelphi;
  694. Procedure TestProcType_AsParam;
  695. Procedure TestProcType_MethodFPC;
  696. Procedure TestProcType_MethodDelphi;
  697. Procedure TestProcType_PropertyFPC;
  698. Procedure TestProcType_PropertyDelphi;
  699. Procedure TestProcType_WithClassInstDoPropertyFPC;
  700. Procedure TestProcType_Nested;
  701. Procedure TestProcType_NestedOfObject;
  702. Procedure TestProcType_ReferenceToProc;
  703. Procedure TestProcType_ReferenceToMethod;
  704. Procedure TestProcType_Typecast;
  705. Procedure TestProcType_PassProcToUntyped;
  706. Procedure TestProcType_PassProcToArray;
  707. Procedure TestProcType_SafeCallObjFPC;
  708. Procedure TestProcType_SafeCallDelphi;
  709. // pointer
  710. Procedure TestPointer;
  711. Procedure TestPointer_Proc;
  712. Procedure TestPointer_AssignRecordFail;
  713. Procedure TestPointer_AssignStaticArrayFail;
  714. Procedure TestPointer_TypeCastJSValueToPointer;
  715. Procedure TestPointer_NonRecordFail;
  716. Procedure TestPointer_AnonymousArgTypeFail;
  717. Procedure TestPointer_AnonymousVarTypeFail;
  718. Procedure TestPointer_AnonymousResultTypeFail;
  719. Procedure TestPointer_AddrOperatorFail;
  720. Procedure TestPointer_ArrayParamsFail;
  721. Procedure TestPointer_PointerAddFail;
  722. Procedure TestPointer_IncPointerFail;
  723. Procedure TestPointer_Record;
  724. Procedure TestPointer_RecordArg;
  725. // jsvalue
  726. Procedure TestJSValue_AssignToJSValue;
  727. Procedure TestJSValue_TypeCastToBaseType;
  728. Procedure TestJSValue_TypecastToJSValue;
  729. Procedure TestJSValue_Equal;
  730. Procedure TestJSValue_If;
  731. Procedure TestJSValue_Not;
  732. Procedure TestJSValue_Enum;
  733. Procedure TestJSValue_ClassInstance;
  734. Procedure TestJSValue_ClassOf;
  735. Procedure TestJSValue_ArrayOfJSValue;
  736. Procedure TestJSValue_ArrayLit;
  737. Procedure TestJSValue_Params;
  738. Procedure TestJSValue_UntypedParam;
  739. Procedure TestJSValue_FuncResultType;
  740. Procedure TestJSValue_ProcType_Assign;
  741. Procedure TestJSValue_ProcType_Equal;
  742. Procedure TestJSValue_ProcType_Param;
  743. Procedure TestJSValue_AssignToPointerFail;
  744. Procedure TestJSValue_OverloadDouble;
  745. Procedure TestJSValue_OverloadNativeInt;
  746. Procedure TestJSValue_OverloadWord;
  747. Procedure TestJSValue_OverloadString;
  748. Procedure TestJSValue_OverloadChar;
  749. Procedure TestJSValue_OverloadPointer;
  750. Procedure TestJSValue_ForIn;
  751. // RTTI
  752. Procedure TestRTTI_IntRange;
  753. Procedure TestRTTI_Double;
  754. Procedure TestRTTI_ProcType;
  755. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  756. Procedure TestRTTI_EnumAndSetType;
  757. Procedure TestRTTI_EnumRange;
  758. Procedure TestRTTI_AnonymousEnumType;
  759. Procedure TestRTTI_StaticArray;
  760. Procedure TestRTTI_DynArray;
  761. Procedure TestRTTI_ArrayNestedAnonymous;
  762. Procedure TestRTTI_PublishedMethodOverloadFail;
  763. Procedure TestRTTI_PublishedMethodExternalFail;
  764. Procedure TestRTTI_PublishedClassPropertyFail;
  765. Procedure TestRTTI_PublishedClassFieldFail;
  766. Procedure TestRTTI_PublishedFieldExternalFail;
  767. Procedure TestRTTI_Class_Field;
  768. Procedure TestRTTI_Class_Method;
  769. Procedure TestRTTI_Class_MethodArgFlags;
  770. Procedure TestRTTI_Class_Property;
  771. Procedure TestRTTI_Class_PropertyParams;
  772. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  773. Procedure TestRTTI_Class_OmitRTTI;
  774. Procedure TestRTTI_IndexModifier;
  775. Procedure TestRTTI_StoredModifier;
  776. Procedure TestRTTI_DefaultValue;
  777. Procedure TestRTTI_DefaultValueSet;
  778. Procedure TestRTTI_DefaultValueRangeType;
  779. Procedure TestRTTI_DefaultValueInherit;
  780. Procedure TestRTTI_OverrideMethod;
  781. Procedure TestRTTI_ReintroduceMethod;
  782. Procedure TestRTTI_OverloadProperty;
  783. // ToDo: array argument
  784. Procedure TestRTTI_ClassForward;
  785. Procedure TestRTTI_ClassOf;
  786. Procedure TestRTTI_Record;
  787. Procedure TestRTTI_RecordAnonymousArray;
  788. Procedure TestRTTI_LocalTypes;
  789. Procedure TestRTTI_TypeInfo_BaseTypes;
  790. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  791. Procedure TestRTTI_TypeInfo_LocalFail;
  792. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  793. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  794. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  795. Procedure TestRTTI_TypeInfo_FunctionClassType;
  796. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  797. Procedure TestRTTI_Interface_Corba;
  798. Procedure TestRTTI_Interface_COM;
  799. Procedure TestRTTI_ClassHelper;
  800. Procedure TestRTTI_ExternalClass;
  801. // Resourcestring
  802. Procedure TestResourcestringProgram;
  803. Procedure TestResourcestringUnit;
  804. Procedure TestResourcestringImplementation;
  805. // Attributes
  806. Procedure TestAttributes_Members;
  807. Procedure TestAttributes_Types;
  808. Procedure TestAttributes_HelperConstructor_Fail;
  809. // Assertions, checks
  810. procedure TestAssert;
  811. procedure TestAssert_SysUtils;
  812. procedure TestObjectChecks;
  813. procedure TestOverflowChecks_Int;
  814. procedure TestRangeChecks_AssignInt;
  815. procedure TestRangeChecks_AssignIntRange;
  816. procedure TestRangeChecks_AssignEnum;
  817. procedure TestRangeChecks_AssignEnumRange;
  818. procedure TestRangeChecks_AssignChar;
  819. procedure TestRangeChecks_AssignCharRange;
  820. procedure TestRangeChecks_ArrayIndex;
  821. procedure TestRangeChecks_ArrayOfRecIndex;
  822. procedure TestRangeChecks_StringIndex;
  823. procedure TestRangeChecks_TypecastInt;
  824. procedure TestRangeChecks_TypeHelperInt;
  825. // Async/AWait
  826. Procedure TestAsync_Proc;
  827. Procedure TestAsync_CallResultIsPromise;
  828. Procedure TestAsync_ConstructorFail;
  829. Procedure TestAsync_PropertyGetterFail;
  830. Procedure TestAwait_NonPromiseWithTypeFail;
  831. Procedure TestAWait_OutsideAsyncFail;
  832. Procedure TestAWait_Result;
  833. Procedure TestAWait_ExternalClassPromise;
  834. Procedure TestAsync_AnonymousProc;
  835. Procedure TestAsync_ProcType;
  836. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  837. Procedure TestAsync_Inherited;
  838. end;
  839. function LinesToStr(Args: array of const): string;
  840. function ExtractFileUnitName(aFilename: string): string;
  841. function JSToStr(El: TJSElement): string;
  842. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  843. implementation
  844. function LinesToStr(Args: array of const): string;
  845. var
  846. s: String;
  847. i: Integer;
  848. begin
  849. s:='';
  850. for i:=Low(Args) to High(Args) do
  851. case Args[i].VType of
  852. vtChar: s += Args[i].VChar+LineEnding;
  853. vtString: s += Args[i].VString^+LineEnding;
  854. vtPChar: s += Args[i].VPChar+LineEnding;
  855. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  856. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  857. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  858. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  859. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  860. end;
  861. Result:=s;
  862. end;
  863. function ExtractFileUnitName(aFilename: string): string;
  864. var
  865. p: Integer;
  866. begin
  867. Result:=ExtractFileName(aFilename);
  868. if Result='' then exit;
  869. for p:=length(Result) downto 1 do
  870. case Result[p] of
  871. '/','\': exit;
  872. '.':
  873. begin
  874. Delete(Result,p,length(Result));
  875. exit;
  876. end;
  877. end;
  878. end;
  879. function JSToStr(El: TJSElement): string;
  880. var
  881. aWriter: TBufferWriter;
  882. aJSWriter: TJSWriter;
  883. begin
  884. aJSWriter:=nil;
  885. aWriter:=TBufferWriter.Create(1000);
  886. try
  887. aJSWriter:=TJSWriter.Create(aWriter);
  888. aJSWriter.IndentSize:=2;
  889. aJSWriter.WriteJS(El);
  890. Result:=aWriter.AsString;
  891. finally
  892. aJSWriter.Free;
  893. aWriter.Free;
  894. end;
  895. end;
  896. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  897. // search diff, ignore changes in spaces
  898. const
  899. SpaceChars = [#9,#10,#13,' '];
  900. var
  901. ExpectedP, ActualP: PChar;
  902. function FindLineEnd(p: PChar): PChar;
  903. begin
  904. Result:=p;
  905. while not (Result^ in [#0,#10,#13]) do inc(Result);
  906. end;
  907. function FindLineStart(p, MinP: PChar): PChar;
  908. begin
  909. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  910. Result:=p;
  911. end;
  912. procedure SkipLineEnd(var p: PChar);
  913. begin
  914. if p^ in [#10,#13] then
  915. begin
  916. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  917. inc(p,2)
  918. else
  919. inc(p);
  920. end;
  921. end;
  922. procedure DiffFound;
  923. var
  924. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  925. ExpLine, ActLine: String;
  926. i, LineNo, DiffLineNo: Integer;
  927. begin
  928. writeln('Diff found "',Msg,'". Lines:');
  929. // write correct lines
  930. p:=PChar(Expected);
  931. LineNo:=0;
  932. DiffLineNo:=0;
  933. repeat
  934. StartPos:=p;
  935. while not (p^ in [#0,#10,#13]) do inc(p);
  936. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  937. SkipLineEnd(p);
  938. inc(LineNo);
  939. if (p<=ExpectedP) and (p^<>#0) then
  940. begin
  941. writeln('= ',ExpLine);
  942. end else begin
  943. // diff line
  944. if DiffLineNo=0 then DiffLineNo:=LineNo;
  945. // write actual line
  946. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  947. ActLineEndP:=FindLineEnd(ActualP);
  948. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  949. writeln('- ',ActLine);
  950. // write expected line
  951. writeln('+ ',ExpLine);
  952. // write empty line with pointer ^
  953. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  954. writeln('^');
  955. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  956. CheckSrcDiff:=false;
  957. // write up to three following actual lines to get some context
  958. for i:=1 to 3 do begin
  959. ActLineStartP:=ActLineEndP;
  960. SkipLineEnd(ActLineStartP);
  961. if ActLineStartP^=#0 then break;
  962. ActLineEndP:=FindLineEnd(ActLineStartP);
  963. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  964. writeln('~ ',ActLine);
  965. end;
  966. exit;
  967. end;
  968. until p^=#0;
  969. writeln('DiffFound Actual:-----------------------');
  970. writeln(Actual);
  971. writeln('DiffFound Expected:---------------------');
  972. writeln(Expected);
  973. writeln('DiffFound ------------------------------');
  974. Msg:='diff found, but lines are the same, internal error';
  975. CheckSrcDiff:=false;
  976. end;
  977. var
  978. IsSpaceNeeded: Boolean;
  979. LastChar, Quote: Char;
  980. begin
  981. Result:=true;
  982. Msg:='';
  983. if Expected='' then Expected:=' ';
  984. if Actual='' then Actual:=' ';
  985. ExpectedP:=PChar(Expected);
  986. ActualP:=PChar(Actual);
  987. repeat
  988. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  989. case ExpectedP^ of
  990. #0:
  991. begin
  992. // check that rest of Actual has only spaces
  993. while ActualP^ in SpaceChars do inc(ActualP);
  994. if ActualP^<>#0 then
  995. begin
  996. DiffFound;
  997. exit;
  998. end;
  999. exit(true);
  1000. end;
  1001. ' ',#9,#10,#13:
  1002. begin
  1003. // skip space in Expected
  1004. IsSpaceNeeded:=false;
  1005. if ExpectedP>PChar(Expected) then
  1006. LastChar:=ExpectedP[-1]
  1007. else
  1008. LastChar:=#0;
  1009. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1010. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1011. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1012. IsSpaceNeeded:=true;
  1013. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1014. begin
  1015. DiffFound;
  1016. exit;
  1017. end;
  1018. while ActualP^ in SpaceChars do inc(ActualP);
  1019. end;
  1020. '''','"':
  1021. begin
  1022. while ActualP^ in SpaceChars do inc(ActualP);
  1023. if ExpectedP^<>ActualP^ then
  1024. begin
  1025. DiffFound;
  1026. exit;
  1027. end;
  1028. Quote:=ExpectedP^;
  1029. repeat
  1030. inc(ExpectedP);
  1031. inc(ActualP);
  1032. if ExpectedP^<>ActualP^ then
  1033. begin
  1034. DiffFound;
  1035. exit;
  1036. end;
  1037. if (ExpectedP^ in [#0,#10,#13]) then
  1038. break
  1039. else if (ExpectedP^=Quote) then
  1040. begin
  1041. inc(ExpectedP);
  1042. inc(ActualP);
  1043. break;
  1044. end;
  1045. until false;
  1046. end;
  1047. else
  1048. while ActualP^ in SpaceChars do inc(ActualP);
  1049. if ExpectedP^<>ActualP^ then
  1050. begin
  1051. DiffFound;
  1052. exit;
  1053. end;
  1054. inc(ExpectedP);
  1055. inc(ActualP);
  1056. end;
  1057. until false;
  1058. end;
  1059. { TTestEnginePasResolver }
  1060. destructor TTestEnginePasResolver.Destroy;
  1061. begin
  1062. FreeAndNil(FStreamResolver);
  1063. FreeAndNil(FParser);
  1064. FreeAndNil(FScanner);
  1065. FreeAndNil(FStreamResolver);
  1066. if Module<>nil then
  1067. begin
  1068. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1069. FModule:=nil;
  1070. end;
  1071. inherited Destroy;
  1072. end;
  1073. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1074. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1075. begin
  1076. Result:=nil;
  1077. if InFilename<>'' then
  1078. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1079. if Assigned(OnFindUnit) then
  1080. Result:=OnFindUnit(AName);
  1081. if NameExpr=nil then ;
  1082. end;
  1083. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1084. begin
  1085. // do not parse recursively
  1086. // parse via the queue
  1087. if Section=nil then ;
  1088. end;
  1089. { TCustomTestModule }
  1090. function TCustomTestModule.GetMsgCount: integer;
  1091. begin
  1092. Result:=FHintMsgs.Count;
  1093. end;
  1094. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1095. begin
  1096. Result:=TTestHintMessage(FHintMsgs[Index]);
  1097. end;
  1098. function TCustomTestModule.GetResolverCount: integer;
  1099. begin
  1100. Result:=FModules.Count;
  1101. end;
  1102. function TCustomTestModule.GetResolvers(Index: integer
  1103. ): TTestEnginePasResolver;
  1104. begin
  1105. Result:=TTestEnginePasResolver(FModules[Index]);
  1106. end;
  1107. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1108. ): TPasModule;
  1109. var
  1110. DefNamespace: String;
  1111. begin
  1112. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1113. if (Pos('.',aUnitName)<1) then
  1114. begin
  1115. DefNamespace:=GetDefaultNamespace;
  1116. if DefNamespace<>'' then
  1117. begin
  1118. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1119. if Result<>nil then exit;
  1120. end;
  1121. end;
  1122. Result:=LoadUnit(aUnitName);
  1123. if Result<>nil then exit;
  1124. {$IFDEF VerbosePas2JS}
  1125. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1126. {$ENDIF}
  1127. Fail('can''t find unit "'+aUnitName+'"');
  1128. end;
  1129. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1130. var
  1131. aParser: TPasParser;
  1132. Item: TTestHintMessage;
  1133. begin
  1134. aParser:=Sender as TPasParser;
  1135. Item:=TTestHintMessage.Create;
  1136. Item.Id:=aParser.LastMsgNumber;
  1137. Item.MsgType:=aParser.LastMsgType;
  1138. Item.MsgNumber:=aParser.LastMsgNumber;
  1139. Item.Msg:=Msg;
  1140. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1141. {$IFDEF VerbosePas2JS}
  1142. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1143. {$ENDIF}
  1144. FHintMsgs.Add(Item);
  1145. end;
  1146. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1147. );
  1148. var
  1149. aResolver: TTestEnginePasResolver;
  1150. Item: TTestHintMessage;
  1151. begin
  1152. aResolver:=Sender as TTestEnginePasResolver;
  1153. Item:=TTestHintMessage.Create;
  1154. Item.Id:=aResolver.LastMsgId;
  1155. Item.MsgType:=aResolver.LastMsgType;
  1156. Item.MsgNumber:=aResolver.LastMsgNumber;
  1157. Item.Msg:=Msg;
  1158. Item.SourcePos:=aResolver.LastSourcePos;
  1159. {$IFDEF VerbosePas2JS}
  1160. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1161. {$ENDIF}
  1162. FHintMsgs.Add(Item);
  1163. end;
  1164. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1165. var
  1166. Item: TTestHintMessage;
  1167. aScanner: TPas2jsPasScanner;
  1168. begin
  1169. aScanner:=Sender as TPas2jsPasScanner;
  1170. Item:=TTestHintMessage.Create;
  1171. Item.Id:=aScanner.LastMsgNumber;
  1172. Item.MsgType:=aScanner.LastMsgType;
  1173. Item.MsgNumber:=aScanner.LastMsgNumber;
  1174. Item.Msg:=Msg;
  1175. Item.SourcePos:=aScanner.CurSourcePos;
  1176. {$IFDEF VerbosePas2JS}
  1177. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1178. {$ENDIF}
  1179. FHintMsgs.Add(Item);
  1180. end;
  1181. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1182. begin
  1183. if FWithTypeInfo=AValue then Exit;
  1184. FWithTypeInfo:=AValue;
  1185. if AValue then
  1186. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1187. else
  1188. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1189. end;
  1190. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1191. var
  1192. i: Integer;
  1193. CurEngine: TTestEnginePasResolver;
  1194. CurUnitName: String;
  1195. begin
  1196. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1197. Result:=nil;
  1198. if (Module.ClassType=TPasModule)
  1199. and (CompareText(Module.Name,aUnitName)=0) then
  1200. exit(Module);
  1201. for i:=0 to ResolverCount-1 do
  1202. begin
  1203. CurEngine:=Resolvers[i];
  1204. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1205. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1206. if CompareText(aUnitName,CurUnitName)=0 then
  1207. begin
  1208. Result:=CurEngine.Module;
  1209. if Result<>nil then exit;
  1210. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1211. FileResolver.FindSourceFile(aUnitName);
  1212. CurEngine.StreamResolver:=TStreamResolver.Create;
  1213. CurEngine.StreamResolver.OwnsStreams:=True;
  1214. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1215. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1216. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1217. InitScanner(CurEngine.Scanner);
  1218. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1219. CurEngine.Parser.Options:=po_tcmodules;
  1220. if CompareText(CurUnitName,'System')=0 then
  1221. CurEngine.Parser.ImplicitUses.Clear;
  1222. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1223. try
  1224. CurEngine.Parser.NextToken;
  1225. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1226. except
  1227. on E: Exception do
  1228. HandleException(E);
  1229. end;
  1230. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1231. Result:=CurEngine.Module;
  1232. exit;
  1233. end;
  1234. end;
  1235. end;
  1236. procedure TCustomTestModule.SetUp;
  1237. begin
  1238. {$IFDEF EnablePasTreeGlobalRefCount}
  1239. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1240. {$ENDIF}
  1241. if FModules<>nil then
  1242. begin
  1243. writeln('TCustomTestModule.SetUp FModules<>nil');
  1244. Halt;
  1245. end;
  1246. inherited SetUp;
  1247. FSkipTests:=false;
  1248. FWithTypeInfo:=false;
  1249. FSource:=TStringList.Create;
  1250. FHub:=TPas2JSResolverHub.Create(Self);
  1251. FModules:=TObjectList.Create(true);
  1252. FFilename:='test1.pp';
  1253. FFileResolver:=TStreamResolver.Create;
  1254. FFileResolver.OwnsStreams:=True;
  1255. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1256. InitScanner(FScanner);
  1257. FEngine:=AddModule(Filename);
  1258. FEngine.Scanner:=FScanner;
  1259. FScanner.Resolver:=FEngine;
  1260. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1261. FParser.OnLog:=@OnParserLog;
  1262. FEngine.Parser:=FParser;
  1263. Parser.Options:=po_tcmodules;
  1264. FModule:=Nil;
  1265. FConverter:=CreateConverter;
  1266. FExpectedErrorClass:=nil;
  1267. end;
  1268. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1269. var
  1270. Options: TPasToJsConverterOptions;
  1271. begin
  1272. Result:=TPasToJSConverter.Create;
  1273. Options:=co_tcmodules;
  1274. if WithTypeInfo then
  1275. Exclude(Options,coNoTypeInfo)
  1276. else
  1277. Include(Options,coNoTypeInfo);
  1278. Result.Options:=Options;
  1279. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1280. end;
  1281. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1282. begin
  1283. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1284. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1285. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1286. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1287. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1288. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1289. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1290. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1291. aScanner.OnLog:=@OnScannerLog;
  1292. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1293. end;
  1294. procedure TCustomTestModule.TearDown;
  1295. {$IFDEF CheckPasTreeRefCount}
  1296. var
  1297. El: TPasElement;
  1298. {$ENDIF}
  1299. var
  1300. i: Integer;
  1301. CurModule: TPasModule;
  1302. begin
  1303. FHintMsgs.Clear;
  1304. FHintMsgsGood.Clear;
  1305. FSkipTests:=false;
  1306. FWithTypeInfo:=false;
  1307. FJSRegModuleCall:=nil;
  1308. FJSModuleCallArgs:=nil;
  1309. FJSImplentationUses:=nil;
  1310. FJSInterfaceUses:=nil;
  1311. FJSModuleSrc:=nil;
  1312. FJSInitBody:=nil;
  1313. FreeAndNil(FJSSource);
  1314. FreeAndNil(FJSModule);
  1315. FreeAndNil(FConverter);
  1316. Engine.Clear;
  1317. FreeAndNil(FSource);
  1318. FreeAndNil(FFileResolver);
  1319. if FModules<>nil then
  1320. begin
  1321. for i:=0 to FModules.Count-1 do
  1322. begin
  1323. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1324. if CurModule=nil then continue;
  1325. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1326. CurModule.ReleaseUsedUnits;
  1327. end;
  1328. if FModule<>nil then
  1329. FModule.ReleaseUsedUnits;
  1330. for i:=0 to FModules.Count-1 do
  1331. begin
  1332. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1333. if CurModule=nil then continue;
  1334. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1335. end;
  1336. FreeAndNil(FModules);
  1337. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1338. FEngine:=nil;
  1339. end;
  1340. FreeAndNil(FHub);
  1341. inherited TearDown;
  1342. {$IFDEF EnablePasTreeGlobalRefCount}
  1343. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1344. begin
  1345. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1346. {$IFDEF CheckPasTreeRefCount}
  1347. El:=TPasElement.FirstRefEl;
  1348. while El<>nil do
  1349. begin
  1350. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1351. for i:=0 to El.RefIds.Count-1 do
  1352. writeln(' ',El.RefIds[i]);
  1353. El:=El.NextRefEl;
  1354. end;
  1355. {$ENDIF}
  1356. Halt;
  1357. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1358. end;
  1359. {$ENDIF}
  1360. end;
  1361. procedure TCustomTestModule.Add(Line: string);
  1362. begin
  1363. Source.Add(Line);
  1364. end;
  1365. procedure TCustomTestModule.Add(const Lines: array of string);
  1366. var
  1367. i: Integer;
  1368. begin
  1369. for i:=low(Lines) to high(Lines) do
  1370. Add(Lines[i]);
  1371. end;
  1372. procedure TCustomTestModule.StartParsing;
  1373. var
  1374. Src: String;
  1375. begin
  1376. Src:=Source.Text;
  1377. FEngine.Source:=Src;
  1378. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1379. Scanner.OpenFile(FileName);
  1380. Writeln('// Test : ',Self.TestName);
  1381. Writeln(Src);
  1382. end;
  1383. procedure TCustomTestModule.ParseModuleQueue;
  1384. var
  1385. i: Integer;
  1386. CurResolver: TTestEnginePasResolver;
  1387. Found: Boolean;
  1388. Section: TPasSection;
  1389. begin
  1390. // parse til exception or all modules finished
  1391. while not SkipTests do
  1392. begin
  1393. Found:=false;
  1394. for i:=0 to ResolverCount-1 do
  1395. begin
  1396. CurResolver:=Resolvers[i];
  1397. if CurResolver.CurrentParser=nil then continue;
  1398. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1399. continue;
  1400. CurResolver.Parser.ParseContinue;
  1401. Found:=true;
  1402. break;
  1403. end;
  1404. if not Found then break;
  1405. end;
  1406. for i:=0 to ResolverCount-1 do
  1407. begin
  1408. CurResolver:=Resolvers[i];
  1409. if CurResolver.Parser=nil then
  1410. begin
  1411. if CurResolver.CurrentParser<>nil then
  1412. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1413. continue;
  1414. end;
  1415. if CurResolver.Parser.CurModule<>nil then
  1416. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1417. end;
  1418. end;
  1419. procedure TCustomTestModule.ParseModule;
  1420. begin
  1421. if SkipTests then exit;
  1422. FFirstPasStatement:=nil;
  1423. try
  1424. StartParsing;
  1425. Parser.ParseMain(FModule);
  1426. ParseModuleQueue;
  1427. except
  1428. on E: Exception do
  1429. HandleException(E);
  1430. end;
  1431. if SkipTests then exit;
  1432. AssertNotNull('Module resulted in Module',Module);
  1433. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1434. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1435. end;
  1436. procedure TCustomTestModule.ParseProgram;
  1437. begin
  1438. if SkipTests then exit;
  1439. ParseModule;
  1440. if SkipTests then exit;
  1441. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1442. FPasProgram:=TPasProgram(Module);
  1443. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1444. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1445. if (PasProgram.InitializationSection.Elements.Count>0) then
  1446. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1447. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1448. end;
  1449. procedure TCustomTestModule.ParseUnit;
  1450. begin
  1451. if SkipTests then exit;
  1452. ParseModule;
  1453. if SkipTests then exit;
  1454. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1455. AssertNotNull('Has interface section',Module.InterfaceSection);
  1456. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1457. if (Module.InitializationSection<>nil)
  1458. and (Module.InitializationSection.Elements.Count>0)
  1459. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1460. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1461. end;
  1462. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1463. ): TTestEnginePasResolver;
  1464. var
  1465. i: Integer;
  1466. begin
  1467. for i:=0 to ResolverCount-1 do
  1468. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1469. exit(Resolvers[i]);
  1470. Result:=nil;
  1471. end;
  1472. function TCustomTestModule.AddModule(aFilename: string
  1473. ): TTestEnginePasResolver;
  1474. begin
  1475. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1476. if FindModuleWithFilename(aFilename)<>nil then
  1477. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1478. Result:=TTestEnginePasResolver.Create;
  1479. Result.Filename:=aFilename;
  1480. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1481. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1482. Result.OnLog:=@OnPasResolverLog;
  1483. Result.Hub:=Hub;
  1484. FModules.Add(Result);
  1485. end;
  1486. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1487. ): TTestEnginePasResolver;
  1488. begin
  1489. Result:=AddModule(aFilename);
  1490. Result.Source:=Src;
  1491. end;
  1492. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1493. ImplementationSrc: string): TTestEnginePasResolver;
  1494. var
  1495. Src: String;
  1496. begin
  1497. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1498. Src+=LineEnding;
  1499. Src+='interface'+LineEnding;
  1500. Src+=LineEnding;
  1501. Src+=InterfaceSrc;
  1502. Src+='implementation'+LineEnding;
  1503. Src+=LineEnding;
  1504. Src+=ImplementationSrc;
  1505. Src+='end.'+LineEnding;
  1506. Result:=AddModuleWithSrc(aFilename,Src);
  1507. end;
  1508. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1509. var
  1510. Intf, Impl: TStringList;
  1511. begin
  1512. Intf:=TStringList.Create;
  1513. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1514. // unit interface
  1515. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1516. Intf.Add('{$modeswitch externalclass}');
  1517. Intf.Add('type');
  1518. Intf.Add(' integer=longint;');
  1519. Intf.Add(' sizeint=nativeint;');
  1520. //'const',
  1521. //' LineEnding = #10;',
  1522. //' DirectorySeparator = ''/'';',
  1523. //' DriveSeparator = '''';',
  1524. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1525. //' AllowDriveSeparators : set of char = [];',
  1526. if supTObject in Parts then
  1527. Intf.AddStrings([
  1528. 'type',
  1529. ' TClass = class of TObject;',
  1530. ' TObject = class',
  1531. ' constructor Create;',
  1532. ' destructor Destroy; virtual;',
  1533. ' class function ClassType: TClass; assembler;',
  1534. ' class function ClassName: String; assembler;',
  1535. ' class function ClassNameIs(const Name: string): boolean;',
  1536. ' class function ClassParent: TClass; assembler;',
  1537. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1538. ' class function UnitName: String; assembler;',
  1539. ' procedure AfterConstruction; virtual;',
  1540. ' procedure BeforeDestruction;virtual;',
  1541. ' function Equals(Obj: TObject): boolean; virtual;',
  1542. ' function ToString: String; virtual;',
  1543. ' end;']);
  1544. if supTInterfacedObject in Parts then
  1545. Intf.AddStrings([
  1546. ' {$Interfaces COM}',
  1547. ' IUnknown = interface',
  1548. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1549. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1550. ' function _AddRef: Integer;',
  1551. ' function _Release: Integer;',
  1552. ' end;',
  1553. ' IInterface = IUnknown;',
  1554. ' TInterfacedObject = class(TObject,IUnknown)',
  1555. ' protected',
  1556. ' fRefCount: Integer;',
  1557. ' { implement methods of IUnknown }',
  1558. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1559. ' function _AddRef: Integer; virtual;',
  1560. ' function _Release: Integer; virtual;',
  1561. ' end;',
  1562. ' TInterfacedClass = class of TInterfacedObject;',
  1563. '',
  1564. '']);
  1565. if supTVarRec in Parts then
  1566. Intf.AddStrings([
  1567. 'const',
  1568. ' vtInteger = 0;',
  1569. ' vtBoolean = 1;',
  1570. ' vtJSValue = 19;',
  1571. 'type',
  1572. ' PVarRec = ^TVarRec;',
  1573. ' TVarRec = record',
  1574. ' VType : byte;',
  1575. ' VJSValue: JSValue;',
  1576. ' vInteger: longint external name ''VJSValue'';',
  1577. ' vBoolean: boolean external name ''VJSValue'';',
  1578. ' end;',
  1579. ' TVarRecArray = array of TVarRec;',
  1580. 'function VarRecs: TVarRecArray; varargs;',
  1581. '']);
  1582. if supTypeInfo in Parts then
  1583. begin
  1584. Intf.AddStrings([
  1585. 'type',
  1586. ' TTypeKind = (',
  1587. ' tkUnknown, // 0',
  1588. ' tkInteger, // 1',
  1589. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1590. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1591. ' tkEnumeration, // 4',
  1592. ' tkSet, // 5',
  1593. ' tkDouble, // 6',
  1594. ' tkBool, // 7',
  1595. ' tkProcVar, // 8 function or procedure',
  1596. ' tkMethod, // 9 proc var of object',
  1597. ' tkArray, // 10 static array',
  1598. ' tkDynArray, // 11',
  1599. ' tkRecord, // 12',
  1600. ' tkClass, // 13',
  1601. ' tkClassRef, // 14',
  1602. ' tkPointer, // 15',
  1603. ' tkJSValue, // 16',
  1604. ' tkRefToProcVar, // 17 variable of procedure type',
  1605. ' tkInterface, // 18',
  1606. ' //tkObject,',
  1607. ' //tkSString,tkLString,tkAString,tkWString,',
  1608. ' //tkVariant,',
  1609. ' //tkWChar,',
  1610. ' //tkInt64,',
  1611. ' //tkQWord,',
  1612. ' //tkInterfaceRaw,',
  1613. ' //tkUString,tkUChar,',
  1614. ' tkHelper, // 19',
  1615. ' //tkFile,',
  1616. ' tkExtClass // 20',
  1617. ' );',
  1618. ' TTypeKinds = set of TTypeKind;',
  1619. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1620. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1621. ' end;',
  1622. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1623. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1624. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1625. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1626. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1627. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1628. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1629. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1630. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1631. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1632. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1633. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1634. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1635. '']);
  1636. end;
  1637. Intf.Add('var');
  1638. Intf.Add(' ExitCode: Longint = 0;');
  1639. // unit implementation
  1640. Impl:=TStringList.Create;
  1641. if supTObject in Parts then
  1642. Impl.AddStrings([
  1643. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1644. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1645. 'asm',
  1646. 'end;',
  1647. 'constructor TObject.Create; begin end;',
  1648. 'destructor TObject.Destroy; begin end;',
  1649. 'class function TObject.ClassType: TClass; assembler;',
  1650. 'asm',
  1651. 'end;',
  1652. 'class function TObject.ClassName: String; assembler;',
  1653. 'asm',
  1654. 'end;',
  1655. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1656. 'begin',
  1657. ' Result:=SameText(Name,ClassName);',
  1658. 'end;',
  1659. 'class function TObject.ClassParent: TClass; assembler;',
  1660. 'asm',
  1661. 'end;',
  1662. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1663. 'asm',
  1664. 'end;',
  1665. 'class function TObject.UnitName: String; assembler;',
  1666. 'asm',
  1667. 'end;',
  1668. 'procedure TObject.AfterConstruction; begin end;',
  1669. 'procedure TObject.BeforeDestruction; begin end;',
  1670. 'function TObject.Equals(Obj: TObject): boolean;',
  1671. 'begin',
  1672. ' Result:=Obj=Self;',
  1673. 'end;',
  1674. 'function TObject.ToString: String;',
  1675. 'begin',
  1676. ' Result:=ClassName;',
  1677. 'end;'
  1678. ]);
  1679. if supTInterfacedObject in Parts then
  1680. Impl.AddStrings([
  1681. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1682. //'begin',
  1683. //'end;',
  1684. 'function TInterfacedObject._AddRef: Integer;',
  1685. 'begin',
  1686. 'end;',
  1687. 'function TInterfacedObject._Release: Integer;',
  1688. 'begin',
  1689. 'end;',
  1690. '']);
  1691. if supTVarRec in Parts then
  1692. Impl.AddStrings([
  1693. 'function VarRecs: TVarRecArray; varargs;',
  1694. 'var',
  1695. ' v: PVarRec;',
  1696. 'begin',
  1697. ' v^.VType:=1;',
  1698. ' v^.VJSValue:=2;',
  1699. 'end;',
  1700. '']);
  1701. try
  1702. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1703. finally
  1704. Intf.Free;
  1705. Impl.Free;
  1706. end;
  1707. end;
  1708. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1709. SystemUnitParts: TSystemUnitParts);
  1710. begin
  1711. if NeedSystemUnit then
  1712. AddSystemUnit(SystemUnitParts)
  1713. else
  1714. Parser.ImplicitUses.Clear;
  1715. Add('program '+ExtractFileUnitName(Filename)+';');
  1716. Add('');
  1717. end;
  1718. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1719. SystemUnitParts: TSystemUnitParts);
  1720. begin
  1721. if NeedSystemUnit then
  1722. AddSystemUnit(SystemUnitParts)
  1723. else
  1724. Parser.ImplicitUses.Clear;
  1725. Add('unit Test1;');
  1726. Add('');
  1727. end;
  1728. procedure TCustomTestModule.ConvertModule;
  1729. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1730. out UsesLit: TJSArrayLiteral);
  1731. var
  1732. i: Integer;
  1733. Item: TJSElement;
  1734. Lit: TJSLiteral;
  1735. begin
  1736. UsesLit:=nil;
  1737. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1738. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1739. exit; // null is ok
  1740. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1741. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1742. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1743. begin
  1744. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1745. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1746. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1747. Lit:=TJSLiteral(Item);
  1748. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1749. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1750. end;
  1751. end;
  1752. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1753. out Src: TJSSourceElements);
  1754. var
  1755. FunDecl: TJSFunctionDeclarationStatement;
  1756. FunDef: TJSFuncDef;
  1757. FunBody: TJSFunctionBody;
  1758. begin
  1759. Src:=nil;
  1760. AssertNotNull(ParamName,Arg.Expr);
  1761. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1762. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1763. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1764. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1765. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1766. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1767. AssertNotNull(ParamName+' body',FunDef.Body);
  1768. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1769. FunBody:=FunDef.Body as TJSFunctionBody;
  1770. AssertNotNull(ParamName+' body.A',FunBody.A);
  1771. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1772. Src:=FunBody.A as TJSSourceElements;
  1773. end;
  1774. var
  1775. ModuleNameExpr: TJSLiteral;
  1776. InitFunction: TJSFunctionDeclarationStatement;
  1777. InitAssign: TJSSimpleAssignStatement;
  1778. InitName: String;
  1779. LastNode: TJSElement;
  1780. Arg: TJSArrayLiteralElement;
  1781. begin
  1782. if SkipTests then exit;
  1783. try
  1784. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1785. except
  1786. on E: Exception do
  1787. HandleException(E);
  1788. end;
  1789. if SkipTests then exit;
  1790. if ExpectedErrorClass<>nil then
  1791. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1792. FJSSource:=TStringList.Create;
  1793. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1794. {$IFDEF VerbosePas2JS}
  1795. writeln('TTestModule.ConvertModule JS:');
  1796. write(FJSSource.Text);
  1797. {$ENDIF}
  1798. // rtl.module(...
  1799. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1800. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1801. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1802. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1803. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1804. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1805. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1806. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1807. // parameter 'unitname'
  1808. if JSModuleCallArgs.Elements.Count<1 then
  1809. Fail('rtl.module first param unit missing');
  1810. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1811. AssertNotNull('module name param',Arg.Expr);
  1812. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1813. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1814. if Module is TPasProgram then
  1815. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1816. else
  1817. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1818. // main uses section
  1819. if JSModuleCallArgs.Elements.Count<2 then
  1820. Fail('rtl.module second param main uses missing');
  1821. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1822. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1823. // program/library/interface function()
  1824. if JSModuleCallArgs.Elements.Count<3 then
  1825. Fail('rtl.module third param intf-function missing');
  1826. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1827. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1828. // search for $mod.$init or $mod.$main - the last statement
  1829. if Module is TPasProgram then
  1830. begin
  1831. InitName:='$main';
  1832. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1833. end
  1834. else
  1835. InitName:='$init';
  1836. FJSInitBody:=nil;
  1837. if JSModuleSrc.Statements.Count>0 then
  1838. begin
  1839. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1840. if LastNode is TJSSimpleAssignStatement then
  1841. begin
  1842. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1843. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1844. begin
  1845. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1846. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1847. end
  1848. else if Module is TPasProgram then
  1849. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1850. end;
  1851. end;
  1852. // optional: implementation uses section
  1853. if JSModuleCallArgs.Elements.Count<4 then
  1854. exit;
  1855. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1856. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1857. end;
  1858. procedure TCustomTestModule.ConvertProgram;
  1859. begin
  1860. Add('end.');
  1861. ParseProgram;
  1862. ConvertModule;
  1863. end;
  1864. procedure TCustomTestModule.ConvertUnit;
  1865. begin
  1866. Add('end.');
  1867. ParseUnit;
  1868. ConvertModule;
  1869. end;
  1870. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1871. begin
  1872. Result:=tcmodules.JSToStr(El);
  1873. end;
  1874. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1875. DottedName: string);
  1876. begin
  1877. if DottedName='' then
  1878. begin
  1879. AssertNull(Msg,El);
  1880. end
  1881. else
  1882. begin
  1883. AssertNotNull(Msg,El);
  1884. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1885. end;
  1886. end;
  1887. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1888. begin
  1889. if El=nil then
  1890. Result:=''
  1891. else if El is TJSPrimaryExpressionIdent then
  1892. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1893. else if El is TJSDotMemberExpression then
  1894. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1895. else
  1896. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1897. end;
  1898. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1899. InitStatements: string; ImplStatements: string);
  1900. var
  1901. ActualSrc, ExpectedSrc, InitName: String;
  1902. begin
  1903. ActualSrc:=JSToStr(JSModuleSrc);
  1904. if coUseStrict in Converter.Options then
  1905. ExpectedSrc:='"use strict";'+LineEnding
  1906. else
  1907. ExpectedSrc:='';
  1908. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1909. ExpectedSrc:=ExpectedSrc+Statements;
  1910. // unit implementation
  1911. if (Trim(ImplStatements)<>'') then
  1912. ExpectedSrc:=ExpectedSrc+LineEnding
  1913. +'$mod.$implcode = function () {'+LineEnding
  1914. +ImplStatements
  1915. +'};'+LineEnding;
  1916. // program main or unit initialization
  1917. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1918. begin
  1919. if Module is TPasProgram then
  1920. InitName:='$main'
  1921. else
  1922. InitName:='$init';
  1923. ExpectedSrc:=ExpectedSrc+LineEnding
  1924. +'$mod.'+InitName+' = function () {'+LineEnding
  1925. +InitStatements
  1926. +'};'+LineEnding;
  1927. end;
  1928. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1929. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1930. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1931. end;
  1932. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1933. // search diff, ignore changes in spaces
  1934. var
  1935. s: string;
  1936. begin
  1937. if CheckSrcDiff(Expected,Actual,s) then exit;
  1938. Fail(Msg+': '+s);
  1939. end;
  1940. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1941. var
  1942. aResolver: TTestEnginePasResolver;
  1943. aConverter: TPasToJSConverter;
  1944. aJSModule: TJSSourceElements;
  1945. ActualSrc: String;
  1946. begin
  1947. aResolver:=GetResolver(Filename);
  1948. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1949. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1950. {$IFDEF VerbosePas2JS}
  1951. writeln('CheckUnit '+Filename+' converting ...');
  1952. {$ENDIF}
  1953. aConverter:=CreateConverter;
  1954. aJSModule:=nil;
  1955. try
  1956. try
  1957. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1958. except
  1959. on E: Exception do
  1960. HandleException(E);
  1961. end;
  1962. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1963. {$IFDEF VerbosePas2JS}
  1964. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1965. write(aResolver.Source);
  1966. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1967. write(ActualSrc);
  1968. {$ENDIF}
  1969. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1970. finally
  1971. aJSModule.Free;
  1972. aConverter.Free;
  1973. end;
  1974. end;
  1975. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1976. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1977. var
  1978. i: Integer;
  1979. Item: TTestHintMessage;
  1980. Expected,Actual: string;
  1981. begin
  1982. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1983. for i:=0 to MsgCount-1 do
  1984. begin
  1985. Item:=Msgs[i];
  1986. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1987. if (Marker<>nil) then
  1988. begin
  1989. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1990. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1991. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1992. end;
  1993. // found
  1994. FHintMsgsGood.Add(Item);
  1995. str(Item.MsgType,Actual);
  1996. str(MsgType,Expected);
  1997. AssertEquals('MsgType',Expected,Actual);
  1998. exit;
  1999. end;
  2000. // needed message missing -> show emitted messages
  2001. WriteSources('',0,0);
  2002. for i:=0 to MsgCount-1 do
  2003. begin
  2004. Item:=Msgs[i];
  2005. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2006. ' ('+IntToStr(Item.MsgNumber),')');
  2007. if Marker<>nil then
  2008. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2009. writeln(' {',Item.Msg,'}');
  2010. end;
  2011. str(MsgType,Expected);
  2012. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2013. if Marker<>nil then
  2014. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2015. Actual:=Actual+' '+Msg;
  2016. Fail(Actual);
  2017. end;
  2018. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2019. );
  2020. var
  2021. i: Integer;
  2022. s, Txt: String;
  2023. Msg: TTestHintMessage;
  2024. begin
  2025. for i:=0 to MsgCount-1 do
  2026. begin
  2027. Msg:=Msgs[i];
  2028. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2029. s:='';
  2030. str(Msg.MsgType,s);
  2031. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2032. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2033. if WithSourcePos then
  2034. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2035. Txt:=Txt+' {'+Msg.Msg+'}';
  2036. Fail(Txt);
  2037. end;
  2038. end;
  2039. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2040. MsgNumber: integer);
  2041. begin
  2042. ExpectedErrorClass:=EScannerError;
  2043. ExpectedErrorMsg:=Msg;
  2044. ExpectedErrorNumber:=MsgNumber;
  2045. end;
  2046. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2047. MsgNumber: integer);
  2048. begin
  2049. ExpectedErrorClass:=EParserError;
  2050. ExpectedErrorMsg:=Msg;
  2051. ExpectedErrorNumber:=MsgNumber;
  2052. end;
  2053. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2054. MsgNumber: integer);
  2055. begin
  2056. ExpectedErrorClass:=EPasResolve;
  2057. ExpectedErrorMsg:=Msg;
  2058. ExpectedErrorNumber:=MsgNumber;
  2059. end;
  2060. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2061. MsgNumber: integer);
  2062. begin
  2063. ExpectedErrorClass:=EPas2JS;
  2064. ExpectedErrorMsg:=Msg;
  2065. ExpectedErrorNumber:=MsgNumber;
  2066. end;
  2067. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2068. var
  2069. MsgNumber: Integer;
  2070. Msg: String;
  2071. begin
  2072. Result:=false;
  2073. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2074. Msg:=E.Message;
  2075. if E is EPas2JS then
  2076. MsgNumber:=EPas2JS(E).MsgNumber
  2077. else if E is EPasResolve then
  2078. MsgNumber:=EPasResolve(E).MsgNumber
  2079. else if E is EParserError then
  2080. MsgNumber:=Parser.LastMsgNumber
  2081. else if E is EScannerError then
  2082. begin
  2083. MsgNumber:=Scanner.LastMsgNumber;
  2084. Msg:=Scanner.LastMsg;
  2085. end
  2086. else
  2087. MsgNumber:=0;
  2088. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2089. if Result then
  2090. SkipTests:=true;
  2091. end;
  2092. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2093. begin
  2094. if IsErrorExpected(E) then exit;
  2095. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2096. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2097. +' '+Scanner.CurFilename
  2098. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2099. FailException(E);
  2100. end;
  2101. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2102. begin
  2103. if IsErrorExpected(E) then exit;
  2104. WriteSources(E.Filename,E.Row,E.Column);
  2105. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2106. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2107. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2108. );
  2109. FailException(E);
  2110. end;
  2111. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2112. var
  2113. P: TPasSourcePos;
  2114. begin
  2115. if IsErrorExpected(E) then exit;
  2116. P:=E.SourcePos;
  2117. WriteSources(P.FileName,P.Row,P.Column);
  2118. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2119. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2120. FailException(E);
  2121. end;
  2122. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2123. var
  2124. Row, Col: integer;
  2125. begin
  2126. if IsErrorExpected(E) then exit;
  2127. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2128. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2129. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2130. +' '+E.PasElement.SourceFilename
  2131. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2132. FailException(E);
  2133. end;
  2134. procedure TCustomTestModule.HandleException(E: Exception);
  2135. begin
  2136. if E is EScannerError then
  2137. HandleScannerError(EScannerError(E))
  2138. else if E is EParserError then
  2139. HandleParserError(EParserError(E))
  2140. else if E is EPasResolve then
  2141. HandlePasResolveError(EPasResolve(E))
  2142. else if E is EPas2JS then
  2143. HandlePas2JSError(EPas2JS(E))
  2144. else
  2145. begin
  2146. if IsErrorExpected(E) then exit;
  2147. if not (E is EAssertionFailedError) then
  2148. begin
  2149. WriteSources('',0,0);
  2150. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2151. end;
  2152. FailException(E);
  2153. end;
  2154. end;
  2155. procedure TCustomTestModule.FailException(E: Exception);
  2156. var
  2157. MsgNumber: Integer;
  2158. begin
  2159. if ExpectedErrorClass<>nil then
  2160. begin
  2161. if FExpectedErrorClass=E.ClassType then
  2162. begin
  2163. if E is EPas2JS then
  2164. MsgNumber:=EPas2JS(E).MsgNumber
  2165. else if E is EPasResolve then
  2166. MsgNumber:=EPasResolve(E).MsgNumber
  2167. else if E is EParserError then
  2168. MsgNumber:=Parser.LastMsgNumber
  2169. else if E is EScannerError then
  2170. MsgNumber:=Scanner.LastMsgNumber
  2171. else
  2172. MsgNumber:=0;
  2173. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2174. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2175. ExpectedErrorNumber,MsgNumber);
  2176. end else begin
  2177. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2178. end;
  2179. end;
  2180. Fail(E.Message);
  2181. end;
  2182. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2183. aCol: integer);
  2184. var
  2185. IsSrc: Boolean;
  2186. i, j: Integer;
  2187. SrcLines: TStringList;
  2188. Line: string;
  2189. aModule: TTestEnginePasResolver;
  2190. begin
  2191. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2192. for i:=0 to ResolverCount-1 do
  2193. begin
  2194. aModule:=Resolvers[i];
  2195. SrcLines:=TStringList.Create;
  2196. try
  2197. SrcLines.Text:=aModule.Source;
  2198. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2199. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2200. for j:=1 to SrcLines.Count do
  2201. begin
  2202. Line:=SrcLines[j-1];
  2203. if IsSrc and (j=aRow) then
  2204. begin
  2205. write('*');
  2206. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2207. end;
  2208. writeln(Format('%:4d: ',[j]),Line);
  2209. end;
  2210. finally
  2211. SrcLines.Free;
  2212. end;
  2213. end;
  2214. end;
  2215. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2216. var
  2217. i: Integer;
  2218. begin
  2219. for i:=0 to ResolverCount-1 do
  2220. if Filename=Resolvers[i].Filename then exit(i);
  2221. Result:=-1;
  2222. end;
  2223. function TCustomTestModule.GetResolver(const Filename: string
  2224. ): TTestEnginePasResolver;
  2225. var
  2226. i: Integer;
  2227. begin
  2228. i:=IndexOfResolver(Filename);
  2229. if i<0 then exit(nil);
  2230. Result:=Resolvers[i];
  2231. end;
  2232. function TCustomTestModule.GetDefaultNamespace: string;
  2233. var
  2234. C: TClass;
  2235. begin
  2236. Result:='';
  2237. if FModule=nil then exit;
  2238. C:=FModule.ClassType;
  2239. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2240. Result:=Engine.DefaultNameSpace;
  2241. end;
  2242. constructor TCustomTestModule.Create;
  2243. begin
  2244. inherited Create;
  2245. FHintMsgs:=TObjectList.Create(true);
  2246. FHintMsgsGood:=TFPList.Create;
  2247. end;
  2248. destructor TCustomTestModule.Destroy;
  2249. begin
  2250. FreeAndNil(FHintMsgs);
  2251. FreeAndNil(FHintMsgsGood);
  2252. inherited Destroy;
  2253. end;
  2254. { TTestModule }
  2255. procedure TTestModule.TestReservedWords;
  2256. var
  2257. i: integer;
  2258. begin
  2259. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2260. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2261. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2262. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2263. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2264. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2265. end;
  2266. procedure TTestModule.TestEmptyProgram;
  2267. begin
  2268. StartProgram(false);
  2269. Add('begin');
  2270. ConvertProgram;
  2271. CheckSource('TestEmptyProgram','','');
  2272. end;
  2273. procedure TTestModule.TestEmptyProgramUseStrict;
  2274. begin
  2275. Converter.Options:=Converter.Options+[coUseStrict];
  2276. StartProgram(false);
  2277. Add('begin');
  2278. ConvertProgram;
  2279. CheckSource('TestEmptyProgramUseStrict','','');
  2280. end;
  2281. procedure TTestModule.TestEmptyUnit;
  2282. begin
  2283. StartUnit(false);
  2284. Add('interface');
  2285. Add('implementation');
  2286. ConvertUnit;
  2287. CheckSource('TestEmptyUnit',
  2288. LinesToStr([
  2289. ]),
  2290. '');
  2291. end;
  2292. procedure TTestModule.TestEmptyUnitUseStrict;
  2293. begin
  2294. Converter.Options:=Converter.Options+[coUseStrict];
  2295. StartUnit(false);
  2296. Add('interface');
  2297. Add('implementation');
  2298. ConvertUnit;
  2299. CheckSource('TestEmptyUnitUseStrict',
  2300. LinesToStr([
  2301. ''
  2302. ]),
  2303. '');
  2304. end;
  2305. procedure TTestModule.TestDottedUnitNames;
  2306. begin
  2307. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2308. LinesToStr([
  2309. 'var iV: longint;'
  2310. ]),
  2311. '');
  2312. FFilename:='ns1.test1.pp';
  2313. StartProgram(true);
  2314. Add('uses unIt2;');
  2315. Add('var');
  2316. Add(' i: longint;');
  2317. Add('begin');
  2318. Add(' i:=iv;');
  2319. Add(' i:=uNit2.iv;');
  2320. Add(' i:=Ns1.TEst1.i;');
  2321. ConvertProgram;
  2322. CheckSource('TestDottedUnitNames',
  2323. LinesToStr([
  2324. 'this.i = 0;',
  2325. '']),
  2326. LinesToStr([ // this.$init
  2327. '$mod.i = pas["NS1.Unit2"].iV;',
  2328. '$mod.i = pas["NS1.Unit2"].iV;',
  2329. '$mod.i = $mod.i;',
  2330. '']) );
  2331. end;
  2332. procedure TTestModule.TestDottedUnitNameImpl;
  2333. begin
  2334. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2335. LinesToStr([
  2336. 'type',
  2337. ' TObject = class end;',
  2338. ' TTestA = class',
  2339. ' end;'
  2340. ]),
  2341. LinesToStr(['uses TEST.UnitB;'])
  2342. );
  2343. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2344. LinesToStr([
  2345. 'uses TEST.UnitA;',
  2346. 'type TTestB = class(TTestA);'
  2347. ]),
  2348. ''
  2349. );
  2350. StartProgram(true);
  2351. Add('uses TEST.UnitA;');
  2352. Add('begin');
  2353. ConvertProgram;
  2354. CheckSource('TestDottedUnitNameImpl',
  2355. LinesToStr([
  2356. '']),
  2357. LinesToStr([ // this.$init
  2358. '']) );
  2359. CheckUnit('TEST.UnitA.pas',
  2360. LinesToStr([
  2361. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2362. ' var $mod = this;',
  2363. ' rtl.createClass(this, "TObject", null, function () {',
  2364. ' this.$init = function () {',
  2365. ' };',
  2366. ' this.$final = function () {',
  2367. ' };',
  2368. ' });',
  2369. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2370. ' });',
  2371. '}, ["TEST.UnitB"]);'
  2372. ]));
  2373. CheckUnit('TEST.UnitB.pas',
  2374. LinesToStr([
  2375. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2376. ' var $mod = this;',
  2377. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2378. ' });',
  2379. '});'
  2380. ]));
  2381. end;
  2382. procedure TTestModule.TestDottedUnitExpr;
  2383. begin
  2384. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2385. LinesToStr([
  2386. 'procedure DoIt;'
  2387. ]),
  2388. 'procedure DoIt; begin end;');
  2389. FFilename:='Ns1.SubNs1.Test1.pp';
  2390. StartProgram(true);
  2391. Add('uses Ns2.sUbnS2.unIt2;');
  2392. Add('var');
  2393. Add(' i: longint;');
  2394. Add('begin');
  2395. Add(' ns2.subns2.unit2.doit;');
  2396. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2397. ConvertProgram;
  2398. CheckSource('TestDottedUnitExpr',
  2399. LinesToStr([
  2400. 'this.i = 0;',
  2401. '']),
  2402. LinesToStr([ // this.$init
  2403. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2404. '$mod.i = $mod.i;',
  2405. '']) );
  2406. end;
  2407. procedure TTestModule.Test_ModeFPCFail;
  2408. begin
  2409. StartProgram(false);
  2410. Add('{$mode FPC}');
  2411. Add('begin');
  2412. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2413. ConvertProgram;
  2414. end;
  2415. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2416. begin
  2417. StartProgram(false);
  2418. Add('{$modeswitch cblocks-}');
  2419. Add('begin');
  2420. ConvertProgram;
  2421. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2422. CheckResolverUnexpectedHints();
  2423. end;
  2424. procedure TTestModule.TestUnit_UseSystem;
  2425. begin
  2426. StartUnit(true);
  2427. Add([
  2428. 'interface',
  2429. 'var i: integer;',
  2430. 'implementation']);
  2431. ConvertUnit;
  2432. CheckSource('TestUnit_UseSystem',
  2433. LinesToStr([
  2434. 'this.i = 0;',
  2435. '']),
  2436. LinesToStr([
  2437. '']) );
  2438. end;
  2439. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2440. begin
  2441. AddModuleWithIntfImplSrc('unit1.pp',
  2442. LinesToStr([
  2443. 'type number = longint;']),
  2444. LinesToStr([
  2445. 'uses test1;',
  2446. 'procedure DoIt;',
  2447. 'begin',
  2448. ' i:=3;',
  2449. 'end;']));
  2450. StartUnit(true);
  2451. Add([
  2452. 'interface',
  2453. 'uses unit1;',
  2454. 'var i: number;',
  2455. 'implementation']);
  2456. ConvertUnit;
  2457. CheckSource('TestUnit_Intf1Impl2Intf1',
  2458. LinesToStr([
  2459. 'this.i = 0;',
  2460. '']),
  2461. LinesToStr([
  2462. '']) );
  2463. end;
  2464. procedure TTestModule.TestIncludeVersion;
  2465. begin
  2466. StartProgram(false);
  2467. Add([
  2468. 'var',
  2469. ' s: string;',
  2470. ' i: word;',
  2471. 'begin',
  2472. ' s:={$I %line%};',
  2473. ' i:={$I %linenum%};',
  2474. ' s:={$I %currentroutine%};',
  2475. ' s:={$I %pas2jsversion%};',
  2476. ' s:={$I %pas2jstarget%};',
  2477. ' s:={$I %pas2jstargetos%};',
  2478. ' s:={$I %pas2jstargetcpu%};',
  2479. ' s:={$I %file%};',
  2480. '']);
  2481. ConvertProgram;
  2482. CheckSource('TestIncludeVersion',
  2483. LinesToStr([
  2484. 'this.s="";',
  2485. 'this.i = 0;']),
  2486. LinesToStr([
  2487. '$mod.s = "7";',
  2488. '$mod.i = 8;',
  2489. '$mod.s = "<anonymous>";',
  2490. '$mod.s = "Comp.Ver.tcmodules";',
  2491. '$mod.s = "Browser";',
  2492. '$mod.s = "Browser";',
  2493. '$mod.s = "ECMAScript5";',
  2494. '$mod.s = "test1.pp";',
  2495. '']));
  2496. end;
  2497. procedure TTestModule.TestVarInt;
  2498. begin
  2499. StartProgram(false);
  2500. Add('var MyI: longint;');
  2501. Add('begin');
  2502. ConvertProgram;
  2503. CheckSource('TestVarInt','this.MyI=0;','');
  2504. end;
  2505. procedure TTestModule.TestVarBaseTypes;
  2506. begin
  2507. StartProgram(false);
  2508. Add('var');
  2509. Add(' i: longint;');
  2510. Add(' s: string;');
  2511. Add(' c: char;');
  2512. Add(' b: boolean;');
  2513. Add(' d: double;');
  2514. Add(' i2: longint = 3;');
  2515. Add(' s2: string = ''foo'';');
  2516. Add(' c2: char = ''4'';');
  2517. Add(' b2: boolean = true;');
  2518. Add(' d2: double = 5.6;');
  2519. Add(' i3: longint = $707;');
  2520. Add(' i4: nativeint = 9007199254740991;');
  2521. Add(' i5: nativeint = -9007199254740991-1;');
  2522. Add(' i6: nativeint = $fffffffffffff;');
  2523. Add(' i7: nativeint = -$fffffffffffff-1;');
  2524. Add(' i8: byte = 00;');
  2525. Add(' u8: nativeuint = $fffffffffffff;');
  2526. Add(' u9: nativeuint = $0000000000000;');
  2527. Add(' u10: nativeuint = $00ff00;');
  2528. Add('begin');
  2529. ConvertProgram;
  2530. CheckSource('TestVarBaseTypes',
  2531. LinesToStr([
  2532. 'this.i = 0;',
  2533. 'this.s = "";',
  2534. 'this.c = "";',
  2535. 'this.b = false;',
  2536. 'this.d = 0.0;',
  2537. 'this.i2 = 3;',
  2538. 'this.s2 = "foo";',
  2539. 'this.c2 = "4";',
  2540. 'this.b2 = true;',
  2541. 'this.d2 = 5.6;',
  2542. 'this.i3 = 0x707;',
  2543. 'this.i4 = 9007199254740991;',
  2544. 'this.i5 = -9007199254740991-1;',
  2545. 'this.i6 = 0xfffffffffffff;',
  2546. 'this.i7 =-0xfffffffffffff-1;',
  2547. 'this.i8 = 0;',
  2548. 'this.u8 = 0xfffffffffffff;',
  2549. 'this.u9 = 0x0;',
  2550. 'this.u10 = 0xff00;'
  2551. ]),
  2552. '');
  2553. end;
  2554. procedure TTestModule.TestBaseTypeSingleFail;
  2555. begin
  2556. StartProgram(false);
  2557. Add('var s: single;');
  2558. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2559. ConvertProgram;
  2560. end;
  2561. procedure TTestModule.TestBaseTypeExtendedFail;
  2562. begin
  2563. StartProgram(false);
  2564. Add('var e: extended;');
  2565. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2566. ConvertProgram;
  2567. end;
  2568. procedure TTestModule.TestConstBaseTypes;
  2569. begin
  2570. StartProgram(false);
  2571. Add('const');
  2572. Add(' i: longint = 3;');
  2573. Add(' s: string = ''foo'';');
  2574. Add(' c: char = ''4'';');
  2575. Add(' b: boolean = true;');
  2576. Add(' d: double = 5.6;');
  2577. Add(' e = low(word);');
  2578. Add(' f = high(word);');
  2579. Add('begin');
  2580. ConvertProgram;
  2581. CheckSource('TestVarBaseTypes',
  2582. LinesToStr([
  2583. 'this.i=3;',
  2584. 'this.s="foo";',
  2585. 'this.c="4";',
  2586. 'this.b=true;',
  2587. 'this.d=5.6;',
  2588. 'this.e = 0;',
  2589. 'this.f = 65535;'
  2590. ]),
  2591. '');
  2592. end;
  2593. procedure TTestModule.TestAliasTypeRef;
  2594. begin
  2595. StartProgram(false);
  2596. Add('type');
  2597. Add(' a=longint;');
  2598. Add(' b=a;');
  2599. Add('var');
  2600. Add(' c: A;');
  2601. Add(' d: B;');
  2602. Add('begin');
  2603. ConvertProgram;
  2604. CheckSource('TestAliasTypeRef',
  2605. LinesToStr([ // statements
  2606. 'this.c = 0;',
  2607. 'this.d = 0;'
  2608. ]),
  2609. LinesToStr([ // this.$main
  2610. ''
  2611. ]));
  2612. end;
  2613. procedure TTestModule.TestTypeCast_BaseTypes;
  2614. begin
  2615. StartProgram(false);
  2616. Add([
  2617. 'var',
  2618. ' i: longint;',
  2619. ' b: boolean;',
  2620. ' d: double;',
  2621. ' s: string;',
  2622. ' c: char;',
  2623. 'begin',
  2624. ' i:=longint(i);',
  2625. ' i:=longint(b);',
  2626. ' b:=boolean(b);',
  2627. ' b:=boolean(i);',
  2628. ' d:=double(d);',
  2629. ' d:=double(i);',
  2630. ' s:=string(s);',
  2631. ' s:=string(c);',
  2632. ' c:=char(c);',
  2633. ' c:=char(i);',
  2634. ' c:=char(65);',
  2635. ' c:=char(#10);',
  2636. ' c:=char(#$E000);',
  2637. '']);
  2638. ConvertProgram;
  2639. CheckSource('TestAliasTypeRef',
  2640. LinesToStr([ // statements
  2641. 'this.i = 0;',
  2642. 'this.b = false;',
  2643. 'this.d = 0.0;',
  2644. 'this.s = "";',
  2645. 'this.c = "";',
  2646. '']),
  2647. LinesToStr([ // this.$main
  2648. '$mod.i = $mod.i;',
  2649. '$mod.i = ($mod.b ? 1 : 0);',
  2650. '$mod.b = $mod.b;',
  2651. '$mod.b = $mod.i != 0;',
  2652. '$mod.d = $mod.d;',
  2653. '$mod.d = $mod.i;',
  2654. '$mod.s = $mod.s;',
  2655. '$mod.s = $mod.c;',
  2656. '$mod.c = $mod.c;',
  2657. '$mod.c = String.fromCharCode($mod.i);',
  2658. '$mod.c = "A";',
  2659. '$mod.c = "\n";',
  2660. '$mod.c = "";',
  2661. '']));
  2662. end;
  2663. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2664. begin
  2665. StartProgram(false);
  2666. Add('type');
  2667. Add(' integer = longint;');
  2668. Add(' TYesNo = boolean;');
  2669. Add(' TFloat = double;');
  2670. Add(' TCaption = string;');
  2671. Add(' TChar = char;');
  2672. Add('var');
  2673. Add(' i: integer;');
  2674. Add(' b: TYesNo;');
  2675. Add(' d: TFloat;');
  2676. Add(' s: TCaption;');
  2677. Add(' c: TChar;');
  2678. Add('begin');
  2679. Add(' i:=integer(i);');
  2680. Add(' i:=integer(b);');
  2681. Add(' b:=TYesNo(b);');
  2682. Add(' b:=TYesNo(i);');
  2683. Add(' d:=TFloat(d);');
  2684. Add(' d:=TFloat(i);');
  2685. Add(' s:=TCaption(s);');
  2686. Add(' s:=TCaption(c);');
  2687. Add(' c:=TChar(c);');
  2688. ConvertProgram;
  2689. CheckSource('TestAliasTypeRef',
  2690. LinesToStr([ // statements
  2691. 'this.i = 0;',
  2692. 'this.b = false;',
  2693. 'this.d = 0.0;',
  2694. 'this.s = "";',
  2695. 'this.c = "";',
  2696. '']),
  2697. LinesToStr([ // this.$main
  2698. '$mod.i = $mod.i;',
  2699. '$mod.i = ($mod.b ? 1 : 0);',
  2700. '$mod.b = $mod.b;',
  2701. '$mod.b = $mod.i != 0;',
  2702. '$mod.d = $mod.d;',
  2703. '$mod.d = $mod.i;',
  2704. '$mod.s = $mod.s;',
  2705. '$mod.s = $mod.c;',
  2706. '$mod.c = $mod.c;',
  2707. '']));
  2708. end;
  2709. procedure TTestModule.TestEmptyProc;
  2710. begin
  2711. StartProgram(false);
  2712. Add('procedure Test;');
  2713. Add('begin');
  2714. Add('end;');
  2715. Add('begin');
  2716. ConvertProgram;
  2717. CheckSource('TestEmptyProc',
  2718. LinesToStr([ // statements
  2719. 'this.Test = function () {',
  2720. '};'
  2721. ]),
  2722. LinesToStr([ // this.$main
  2723. ''
  2724. ]));
  2725. end;
  2726. procedure TTestModule.TestProcOneParam;
  2727. begin
  2728. StartProgram(false);
  2729. Add('procedure ProcA(i: longint);');
  2730. Add('begin');
  2731. Add('end;');
  2732. Add('begin');
  2733. Add(' PROCA(3);');
  2734. ConvertProgram;
  2735. CheckSource('TestProcOneParam',
  2736. LinesToStr([ // statements
  2737. 'this.ProcA = function (i) {',
  2738. '};'
  2739. ]),
  2740. LinesToStr([ // this.$main
  2741. '$mod.ProcA(3);'
  2742. ]));
  2743. end;
  2744. procedure TTestModule.TestFunctionWithoutParams;
  2745. begin
  2746. StartProgram(false);
  2747. Add('function FuncA: longint;');
  2748. Add('begin');
  2749. Add('end;');
  2750. Add('var i: longint;');
  2751. Add('begin');
  2752. Add(' I:=FUNCA();');
  2753. Add(' I:=FUNCA;');
  2754. Add(' FUNCA();');
  2755. Add(' FUNCA;');
  2756. ConvertProgram;
  2757. CheckSource('TestProcWithoutParams',
  2758. LinesToStr([ // statements
  2759. 'this.FuncA = function () {',
  2760. ' var Result = 0;',
  2761. ' return Result;',
  2762. '};',
  2763. 'this.i=0;'
  2764. ]),
  2765. LinesToStr([ // this.$main
  2766. '$mod.i=$mod.FuncA();',
  2767. '$mod.i=$mod.FuncA();',
  2768. '$mod.FuncA();',
  2769. '$mod.FuncA();'
  2770. ]));
  2771. end;
  2772. procedure TTestModule.TestProcedureWithoutParams;
  2773. begin
  2774. StartProgram(false);
  2775. Add('procedure ProcA;');
  2776. Add('begin');
  2777. Add('end;');
  2778. Add('begin');
  2779. Add(' PROCA();');
  2780. Add(' PROCA;');
  2781. ConvertProgram;
  2782. CheckSource('TestProcWithoutParams',
  2783. LinesToStr([ // statements
  2784. 'this.ProcA = function () {',
  2785. '};'
  2786. ]),
  2787. LinesToStr([ // this.$main
  2788. '$mod.ProcA();',
  2789. '$mod.ProcA();'
  2790. ]));
  2791. end;
  2792. procedure TTestModule.TestIncDec;
  2793. begin
  2794. StartProgram(false);
  2795. Add([
  2796. 'procedure DoIt(var i: longint);',
  2797. 'begin',
  2798. ' inc(i);',
  2799. ' inc(i,2);',
  2800. 'end;',
  2801. 'var',
  2802. ' Bar: longint;',
  2803. 'begin',
  2804. ' inc(bar);',
  2805. ' inc(bar,2);',
  2806. ' dec(bar);',
  2807. ' dec(bar,3);',
  2808. '']);
  2809. ConvertProgram;
  2810. CheckSource('TestIncDec',
  2811. LinesToStr([ // statements
  2812. 'this.DoIt = function (i) {',
  2813. ' i.set(i.get()+1);',
  2814. ' i.set(i.get()+2);',
  2815. '};',
  2816. 'this.Bar = 0;'
  2817. ]),
  2818. LinesToStr([ // this.$main
  2819. '$mod.Bar+=1;',
  2820. '$mod.Bar+=2;',
  2821. '$mod.Bar-=1;',
  2822. '$mod.Bar-=3;'
  2823. ]));
  2824. end;
  2825. procedure TTestModule.TestLoHiFpcMode;
  2826. begin
  2827. StartProgram(false);
  2828. Add([
  2829. '{$mode objfpc}',
  2830. 'const',
  2831. ' LoByte1 = Lo(Word($1234));',
  2832. ' HiByte1 = Hi(Word($1234));',
  2833. ' LoByte2 = Lo(SmallInt($1234));',
  2834. ' HiByte2 = Hi(SmallInt($1234));',
  2835. ' LoWord1 = Lo($1234CDEF);',
  2836. ' HiWord1 = Hi($1234CDEF);',
  2837. ' LoWord2 = Lo(-$1234CDEF);',
  2838. ' HiWord2 = Hi(-$1234CDEF);',
  2839. ' lo4:byte=lo(byte($34));',
  2840. ' hi4:byte=hi(byte($34));',
  2841. ' lo5:byte=lo(shortint(-$34));',
  2842. ' hi5:byte=hi(shortint(-$34));',
  2843. ' lo6:longword=lo($123456789ABCD);',
  2844. ' hi6:longword=hi($123456789ABCD);',
  2845. ' lo7:longword=lo(-$123456789ABCD);',
  2846. ' hi7:longword=hi(-$123456789ABCD);',
  2847. 'var',
  2848. ' b: Byte;',
  2849. ' ss: shortint;',
  2850. ' w: Word;',
  2851. ' si: SmallInt;',
  2852. ' lw: LongWord;',
  2853. ' li: LongInt;',
  2854. ' b2: Byte;',
  2855. ' ni: nativeint;',
  2856. 'begin',
  2857. ' w := $1234;',
  2858. ' ss := -$12;',
  2859. ' b := lo(ss);',
  2860. ' b := HI(ss);',
  2861. ' b := lo(w);',
  2862. ' b := HI(w);',
  2863. ' b2 := lo(b);',
  2864. ' b2 := hi(b);',
  2865. ' lw := $1234CDEF;',
  2866. ' w := lo(lw);',
  2867. ' w := hi(lw);',
  2868. ' ni := $123456789ABCD;',
  2869. ' lw := lo(ni);',
  2870. ' lw := hi(ni);',
  2871. '']);
  2872. ConvertProgram;
  2873. CheckSource('TestLoHiFpcMode',
  2874. LinesToStr([ // statements
  2875. 'this.LoByte1 = 0x1234 & 0xFF;',
  2876. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2877. 'this.LoByte2 = 0x1234 & 0xFF;',
  2878. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2879. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2880. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2881. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2882. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2883. 'this.lo4 = 0x34 & 0xF;',
  2884. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2885. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2886. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2887. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2888. 'this.hi6 = 74565 >>> 0;',
  2889. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2890. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2891. 'this.b = 0;',
  2892. 'this.ss = 0;',
  2893. 'this.w = 0;',
  2894. 'this.si = 0;',
  2895. 'this.lw = 0;',
  2896. 'this.li = 0;',
  2897. 'this.b2 = 0;',
  2898. 'this.ni = 0;',
  2899. '']),
  2900. LinesToStr([ // this.$main
  2901. '$mod.w = 0x1234;',
  2902. '$mod.ss = -0x12;',
  2903. '$mod.b = $mod.ss & 0xFF;',
  2904. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2905. '$mod.b = $mod.w & 0xFF;',
  2906. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2907. '$mod.b2 = $mod.b & 0xF;',
  2908. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2909. '$mod.lw = 0x1234CDEF;',
  2910. '$mod.w = $mod.lw & 0xFFFF;',
  2911. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2912. '$mod.ni = 0x123456789ABCD;',
  2913. '$mod.lw = $mod.ni >>> 0;',
  2914. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2915. '']));
  2916. end;
  2917. procedure TTestModule.TestLoHiDelphiMode;
  2918. begin
  2919. StartProgram(false);
  2920. Add([
  2921. '{$mode delphi}',
  2922. 'const',
  2923. ' LoByte1 = Lo(Word($1234));',
  2924. ' HiByte1 = Hi(Word($1234));',
  2925. ' LoByte2 = Lo(SmallInt($1234));',
  2926. ' HiByte2 = Hi(SmallInt($1234));',
  2927. ' LoByte3 = Lo($1234CDEF);',
  2928. ' HiByte3 = Hi($1234CDEF);',
  2929. ' LoByte4 = Lo(-$1234CDEF);',
  2930. ' HiByte4 = Hi(-$1234CDEF);',
  2931. 'var',
  2932. ' b: Byte;',
  2933. ' w: Word;',
  2934. ' si: SmallInt;',
  2935. ' lw: LongWord;',
  2936. ' li: LongInt;',
  2937. 'begin',
  2938. ' w := $1234;',
  2939. ' b := lo(w);',
  2940. ' b := HI(w);',
  2941. ' lw := $1234CDEF;',
  2942. ' b := lo(lw);',
  2943. ' b := hi(lw);',
  2944. '']);
  2945. ConvertProgram;
  2946. CheckSource('TestLoHiDelphiMode',
  2947. LinesToStr([ // statements
  2948. 'this.LoByte1 = 0x1234 & 0xFF;',
  2949. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2950. 'this.LoByte2 = 0x1234 & 0xFF;',
  2951. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2952. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2953. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2954. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2955. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2956. 'this.b = 0;',
  2957. 'this.w = 0;',
  2958. 'this.si = 0;',
  2959. 'this.lw = 0;',
  2960. 'this.li = 0;'
  2961. ]),
  2962. LinesToStr([ // this.$main
  2963. '$mod.w = 0x1234;',
  2964. '$mod.b = $mod.w & 0xFF;',
  2965. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2966. '$mod.lw = 0x1234CDEF;',
  2967. '$mod.b = $mod.lw & 0xFF;',
  2968. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2969. ]));
  2970. end;
  2971. procedure TTestModule.TestAssignments;
  2972. begin
  2973. StartProgram(false);
  2974. Parser.Options:=Parser.Options+[po_cassignments];
  2975. Add('var');
  2976. Add(' Bar:longint;');
  2977. Add('begin');
  2978. Add(' bar:=3;');
  2979. Add(' bar+=4;');
  2980. Add(' bar-=5;');
  2981. Add(' bar*=6;');
  2982. ConvertProgram;
  2983. CheckSource('TestAssignments',
  2984. LinesToStr([ // statements
  2985. 'this.Bar = 0;'
  2986. ]),
  2987. LinesToStr([ // this.$main
  2988. '$mod.Bar=3;',
  2989. '$mod.Bar+=4;',
  2990. '$mod.Bar-=5;',
  2991. '$mod.Bar*=6;'
  2992. ]));
  2993. end;
  2994. procedure TTestModule.TestArithmeticOperators1;
  2995. begin
  2996. StartProgram(false);
  2997. Add('var');
  2998. Add(' vA,vB,vC:longint;');
  2999. Add('begin');
  3000. Add(' va:=1;');
  3001. Add(' vb:=va+va;');
  3002. Add(' vb:=va div vb;');
  3003. Add(' vb:=va mod vb;');
  3004. Add(' vb:=va+va*vb+va div vb;');
  3005. Add(' vc:=-va;');
  3006. Add(' va:=va-vb;');
  3007. Add(' vb:=va;');
  3008. Add(' if va<vb then vc:=va else vc:=vb;');
  3009. ConvertProgram;
  3010. CheckSource('TestArithmeticOperators1',
  3011. LinesToStr([ // statements
  3012. 'this.vA = 0;',
  3013. 'this.vB = 0;',
  3014. 'this.vC = 0;'
  3015. ]),
  3016. LinesToStr([ // this.$main
  3017. '$mod.vA = 1;',
  3018. '$mod.vB = $mod.vA + $mod.vA;',
  3019. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  3020. '$mod.vB = $mod.vA % $mod.vB;',
  3021. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  3022. '$mod.vC = -$mod.vA;',
  3023. '$mod.vA = $mod.vA - $mod.vB;',
  3024. '$mod.vB = $mod.vA;',
  3025. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3026. ]));
  3027. end;
  3028. procedure TTestModule.TestLogicalOperators;
  3029. begin
  3030. StartProgram(false);
  3031. Add('var');
  3032. Add(' vA,vB,vC:boolean;');
  3033. Add('begin');
  3034. Add(' va:=vb and vc;');
  3035. Add(' va:=vb or vc;');
  3036. Add(' va:=vb xor vc;');
  3037. Add(' va:=true and vc;');
  3038. Add(' va:=(vb and vc) or (va and vb);');
  3039. Add(' va:=not vb;');
  3040. ConvertProgram;
  3041. CheckSource('TestLogicalOperators',
  3042. LinesToStr([ // statements
  3043. 'this.vA = false;',
  3044. 'this.vB = false;',
  3045. 'this.vC = false;'
  3046. ]),
  3047. LinesToStr([ // this.$main
  3048. '$mod.vA = $mod.vB && $mod.vC;',
  3049. '$mod.vA = $mod.vB || $mod.vC;',
  3050. '$mod.vA = $mod.vB ^ $mod.vC;',
  3051. '$mod.vA = true && $mod.vC;',
  3052. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3053. '$mod.vA = !$mod.vB;'
  3054. ]));
  3055. end;
  3056. procedure TTestModule.TestBitwiseOperators;
  3057. begin
  3058. StartProgram(false);
  3059. Add([
  3060. 'var',
  3061. ' vA,vB,vC:longint;',
  3062. ' X,Y,Z: nativeint;',
  3063. 'begin',
  3064. ' va:=vb and vc;',
  3065. ' va:=vb or vc;',
  3066. ' va:=vb xor vc;',
  3067. ' va:=vb shl vc;',
  3068. ' va:=vb shr vc;',
  3069. ' va:=3 and vc;',
  3070. ' va:=(vb and vc) or (va and vb);',
  3071. ' va:=not vb;',
  3072. ' X:=Y and Z;',
  3073. ' X:=Y and va;',
  3074. ' X:=Y or Z;',
  3075. ' X:=Y or va;',
  3076. ' X:=Y xor Z;',
  3077. ' X:=Y xor va;',
  3078. '']);
  3079. ConvertProgram;
  3080. CheckSource('TestBitwiseOperators',
  3081. LinesToStr([ // statements
  3082. 'this.vA = 0;',
  3083. 'this.vB = 0;',
  3084. 'this.vC = 0;',
  3085. 'this.X = 0;',
  3086. 'this.Y = 0;',
  3087. 'this.Z = 0;',
  3088. '']),
  3089. LinesToStr([ // this.$main
  3090. '$mod.vA = $mod.vB & $mod.vC;',
  3091. '$mod.vA = $mod.vB | $mod.vC;',
  3092. '$mod.vA = $mod.vB ^ $mod.vC;',
  3093. '$mod.vA = $mod.vB << $mod.vC;',
  3094. '$mod.vA = $mod.vB >>> $mod.vC;',
  3095. '$mod.vA = 3 & $mod.vC;',
  3096. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3097. '$mod.vA = ~$mod.vB;',
  3098. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3099. '$mod.X = $mod.Y & $mod.vA;',
  3100. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3101. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3102. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3103. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3104. '']));
  3105. end;
  3106. procedure TTestModule.TestBitwiseOperatorsLongword;
  3107. begin
  3108. StartProgram(false);
  3109. Add([
  3110. 'var',
  3111. ' a,b,c:longword;',
  3112. ' i: longint;',
  3113. 'begin',
  3114. ' a:=$12345678;',
  3115. ' b:=$EDCBA987;',
  3116. ' c:=not a;',
  3117. ' c:=a and b;',
  3118. ' c:=a and $ffff0000;',
  3119. ' c:=a or b;',
  3120. ' c:=a or $ff00ff00;',
  3121. ' c:=a xor b;',
  3122. ' c:=a xor $f0f0f0f0;',
  3123. ' c:=a shl 1;',
  3124. ' c:=a shl 16;',
  3125. ' c:=a shl 24;',
  3126. ' c:=a shl b;',
  3127. ' c:=a shr 1;',
  3128. ' c:=a shr 16;',
  3129. ' c:=a shr 24;',
  3130. ' c:=a shr b;',
  3131. ' c:=(b and c) or (a and b);',
  3132. ' c:=i and a;',
  3133. ' c:=i or a;',
  3134. ' c:=i xor a;',
  3135. '']);
  3136. ConvertProgram;
  3137. CheckSource('TestBitwiseOperatorsLongword',
  3138. LinesToStr([ // statements
  3139. 'this.a = 0;',
  3140. 'this.b = 0;',
  3141. 'this.c = 0;',
  3142. 'this.i = 0;',
  3143. '']),
  3144. LinesToStr([ // this.$main
  3145. '$mod.a = 0x12345678;',
  3146. '$mod.b = 0xEDCBA987;',
  3147. '$mod.c = rtl.lw(~$mod.a);',
  3148. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3149. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3150. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3151. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3152. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3153. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3154. '$mod.c = rtl.lw($mod.a << 1);',
  3155. '$mod.c = rtl.lw($mod.a << 16);',
  3156. '$mod.c = rtl.lw($mod.a << 24);',
  3157. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3158. '$mod.c = rtl.lw($mod.a >>> 1);',
  3159. '$mod.c = rtl.lw($mod.a >>> 16);',
  3160. '$mod.c = rtl.lw($mod.a >>> 24);',
  3161. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3162. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3163. '$mod.c = $mod.i & $mod.a;',
  3164. '$mod.c = $mod.i | $mod.a;',
  3165. '$mod.c = $mod.i ^ $mod.a;',
  3166. '']));
  3167. end;
  3168. procedure TTestModule.TestPrgProcVar;
  3169. begin
  3170. StartProgram(false);
  3171. Add('procedure Proc1;');
  3172. Add('type');
  3173. Add(' t1=longint;');
  3174. Add('var');
  3175. Add(' vA:t1;');
  3176. Add('begin');
  3177. Add('end;');
  3178. Add('begin');
  3179. ConvertProgram;
  3180. CheckSource('TestPrgProcVar',
  3181. LinesToStr([ // statements
  3182. 'this.Proc1 = function () {',
  3183. ' var vA=0;',
  3184. '};'
  3185. ]),
  3186. LinesToStr([ // this.$main
  3187. ''
  3188. ]));
  3189. end;
  3190. procedure TTestModule.TestUnitProcVar;
  3191. begin
  3192. StartUnit(false);
  3193. Add('interface');
  3194. Add('');
  3195. Add('type tA=string; // unit scope');
  3196. Add('procedure Proc1;');
  3197. Add('');
  3198. Add('implementation');
  3199. Add('');
  3200. Add('procedure Proc1;');
  3201. Add('type tA=longint; // local proc scope');
  3202. Add('var v1:tA; // using local tA');
  3203. Add('begin');
  3204. Add('end;');
  3205. Add('var v2:tA; // using interface tA');
  3206. ConvertUnit;
  3207. CheckSource('TestUnitProcVar',
  3208. LinesToStr([ // statements
  3209. 'var $impl = $mod.$impl;',
  3210. 'this.Proc1 = function () {',
  3211. ' var v1 = 0;',
  3212. '};',
  3213. '']),
  3214. // this.$init
  3215. '',
  3216. // implementation
  3217. LinesToStr([
  3218. '$impl.v2 = "";',
  3219. '']));
  3220. end;
  3221. procedure TTestModule.TestImplProc;
  3222. begin
  3223. StartUnit(false);
  3224. Add('interface');
  3225. Add('');
  3226. Add('procedure Proc1;');
  3227. Add('');
  3228. Add('implementation');
  3229. Add('');
  3230. Add('procedure Proc1; begin end;');
  3231. Add('procedure Proc2; begin end;');
  3232. Add('initialization');
  3233. Add(' Proc1;');
  3234. Add(' Proc2;');
  3235. ConvertUnit;
  3236. CheckSource('TestImplProc',
  3237. LinesToStr([ // statements
  3238. 'var $impl = $mod.$impl;',
  3239. 'this.Proc1 = function () {',
  3240. '};',
  3241. '']),
  3242. LinesToStr([ // this.$init
  3243. '$mod.Proc1();',
  3244. '$impl.Proc2();',
  3245. '']),
  3246. LinesToStr([ // implementation
  3247. '$impl.Proc2 = function () {',
  3248. '};',
  3249. ''])
  3250. );
  3251. end;
  3252. procedure TTestModule.TestFunctionResult;
  3253. begin
  3254. StartProgram(false);
  3255. Add('function Func1: longint;');
  3256. Add('begin');
  3257. Add(' Result:=3;');
  3258. Add(' Func1:=4;');
  3259. Add('end;');
  3260. Add('begin');
  3261. ConvertProgram;
  3262. CheckSource('TestFunctionResult',
  3263. LinesToStr([ // statements
  3264. 'this.Func1 = function () {',
  3265. ' var Result = 0;',
  3266. ' Result = 3;',
  3267. ' Result = 4;',
  3268. ' return Result;',
  3269. '};'
  3270. ]),
  3271. '');
  3272. end;
  3273. procedure TTestModule.TestNestedProc;
  3274. begin
  3275. StartProgram(false);
  3276. Add([
  3277. 'var vInUnit: longint;',
  3278. 'function DoIt(pA,pD: longint): longint;',
  3279. 'var',
  3280. ' vB: longint;',
  3281. ' vC: longint;',
  3282. ' function Nesty(pA: longint): longint; ',
  3283. ' var vB: longint;',
  3284. ' begin',
  3285. ' Result:=pa+vb+vc+pd+vInUnit;',
  3286. ' nesty:=3;',
  3287. ' doit:=4;',
  3288. ' exit;',
  3289. ' end;',
  3290. 'begin',
  3291. ' Result:=pa+vb+vc;',
  3292. ' doit:=6;',
  3293. ' exit;',
  3294. 'end;',
  3295. 'begin']);
  3296. ConvertProgram;
  3297. CheckSource('TestNestedProc',
  3298. LinesToStr([ // statements
  3299. 'this.vInUnit = 0;',
  3300. 'this.DoIt = function (pA, pD) {',
  3301. ' var Result = 0;',
  3302. ' var vB = 0;',
  3303. ' var vC = 0;',
  3304. ' function Nesty(pA) {',
  3305. ' var Result$1 = 0;',
  3306. ' var vB = 0;',
  3307. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3308. ' Result$1 = 3;',
  3309. ' Result = 4;',
  3310. ' return Result$1;',
  3311. ' return Result$1;',
  3312. ' };',
  3313. ' Result = pA + vB + vC;',
  3314. ' Result = 6;',
  3315. ' return Result;',
  3316. ' return Result;',
  3317. '};'
  3318. ]),
  3319. '');
  3320. end;
  3321. procedure TTestModule.TestNestedProc_ResultString;
  3322. begin
  3323. StartProgram(false);
  3324. Add([
  3325. 'function DoIt: string;',
  3326. ' function Nesty: string; ',
  3327. ' begin',
  3328. ' nesty:=#65#66;',
  3329. ' nesty[1]:=#67;',
  3330. ' doit:=#68;',
  3331. ' doit[2]:=#69;',
  3332. ' end;',
  3333. 'begin',
  3334. ' doit:=#70;',
  3335. ' doit[3]:=#71;',
  3336. 'end;',
  3337. 'begin']);
  3338. ConvertProgram;
  3339. CheckSource('TestNestedProc_ResultString',
  3340. LinesToStr([ // statements
  3341. 'this.DoIt = function () {',
  3342. ' var Result = "";',
  3343. ' function Nesty() {',
  3344. ' var Result$1 = "";',
  3345. ' Result$1 = "AB";',
  3346. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3347. ' Result = "D";',
  3348. ' Result = rtl.setCharAt(Result, 1, "E");',
  3349. ' return Result$1;',
  3350. ' };',
  3351. ' Result = "F";',
  3352. ' Result = rtl.setCharAt(Result, 2, "G");',
  3353. ' return Result;',
  3354. '};'
  3355. ]),
  3356. '');
  3357. end;
  3358. procedure TTestModule.TestForwardProc;
  3359. begin
  3360. StartProgram(false);
  3361. Add('procedure FuncA(Bar: longint); forward;');
  3362. Add('procedure FuncB(Bar: longint);');
  3363. Add('begin');
  3364. Add(' funca(bar);');
  3365. Add('end;');
  3366. Add('procedure funca(bar: longint);');
  3367. Add('begin');
  3368. Add(' if bar=3 then ;');
  3369. Add('end;');
  3370. Add('begin');
  3371. Add(' funca(4);');
  3372. Add(' funcb(5);');
  3373. ConvertProgram;
  3374. CheckSource('TestForwardProc',
  3375. LinesToStr([ // statements'
  3376. 'this.FuncB = function (Bar) {',
  3377. ' $mod.FuncA(Bar);',
  3378. '};',
  3379. 'this.FuncA = function (Bar) {',
  3380. ' if (Bar === 3);',
  3381. '};'
  3382. ]),
  3383. LinesToStr([
  3384. '$mod.FuncA(4);',
  3385. '$mod.FuncB(5);'
  3386. ])
  3387. );
  3388. end;
  3389. procedure TTestModule.TestNestedForwardProc;
  3390. begin
  3391. StartProgram(false);
  3392. Add('procedure FuncA;');
  3393. Add(' procedure FuncB(i: longint); forward;');
  3394. Add(' procedure FuncC(i: longint);');
  3395. Add(' begin');
  3396. Add(' funcb(i);');
  3397. Add(' end;');
  3398. Add(' procedure FuncB(i: longint);');
  3399. Add(' begin');
  3400. Add(' if i=3 then ;');
  3401. Add(' end;');
  3402. Add('begin');
  3403. Add(' funcc(4)');
  3404. Add('end;');
  3405. Add('begin');
  3406. Add(' funca;');
  3407. ConvertProgram;
  3408. CheckSource('TestNestedForwardProc',
  3409. LinesToStr([ // statements'
  3410. 'this.FuncA = function () {',
  3411. ' function FuncC(i) {',
  3412. ' FuncB(i);',
  3413. ' };',
  3414. ' function FuncB(i) {',
  3415. ' if (i === 3);',
  3416. ' };',
  3417. ' FuncC(4);',
  3418. '};'
  3419. ]),
  3420. LinesToStr([
  3421. '$mod.FuncA();'
  3422. ])
  3423. );
  3424. end;
  3425. procedure TTestModule.TestAssignFunctionResult;
  3426. begin
  3427. StartProgram(false);
  3428. Add('function Func1: longint;');
  3429. Add('begin');
  3430. Add('end;');
  3431. Add('var i: longint;');
  3432. Add('begin');
  3433. Add(' i:=func1();');
  3434. Add(' i:=func1()+func1();');
  3435. ConvertProgram;
  3436. CheckSource('TestAssignFunctionResult',
  3437. LinesToStr([ // statements
  3438. 'this.Func1 = function () {',
  3439. ' var Result = 0;',
  3440. ' return Result;',
  3441. '};',
  3442. 'this.i = 0;'
  3443. ]),
  3444. LinesToStr([
  3445. '$mod.i = $mod.Func1();',
  3446. '$mod.i = $mod.Func1() + $mod.Func1();'
  3447. ]));
  3448. end;
  3449. procedure TTestModule.TestFunctionResultInCondition;
  3450. begin
  3451. StartProgram(false);
  3452. Add('function Func1: longint;');
  3453. Add('begin');
  3454. Add('end;');
  3455. Add('function Func2: boolean;');
  3456. Add('begin');
  3457. Add('end;');
  3458. Add('var i: longint;');
  3459. Add('begin');
  3460. Add(' if func2 then ;');
  3461. Add(' if i=func1() then ;');
  3462. Add(' if i=func1 then ;');
  3463. ConvertProgram;
  3464. CheckSource('TestFunctionResultInCondition',
  3465. LinesToStr([ // statements
  3466. 'this.Func1 = function () {',
  3467. ' var Result = 0;',
  3468. ' return Result;',
  3469. '};',
  3470. 'this.Func2 = function () {',
  3471. ' var Result = false;',
  3472. ' return Result;',
  3473. '};',
  3474. 'this.i = 0;'
  3475. ]),
  3476. LinesToStr([
  3477. 'if ($mod.Func2());',
  3478. 'if ($mod.i === $mod.Func1());',
  3479. 'if ($mod.i === $mod.Func1());'
  3480. ]));
  3481. end;
  3482. procedure TTestModule.TestFunctionResultInForLoop;
  3483. begin
  3484. StartProgram(false);
  3485. Add([
  3486. 'function Func1(a: array of longint): longint;',
  3487. 'begin',
  3488. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3489. ' for Result in a do if a[Result]=0 then exit;',
  3490. 'end;',
  3491. 'begin',
  3492. ' Func1([1,2,3])']);
  3493. ConvertProgram;
  3494. CheckSource('TestFunctionResultInForLoop',
  3495. LinesToStr([ // statements
  3496. 'this.Func1 = function (a) {',
  3497. ' var Result = 0;',
  3498. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3499. ' Result = $l;',
  3500. ' if (a[Result] === 0) return Result;',
  3501. ' };',
  3502. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3503. ' Result = $in[$l1];',
  3504. ' if (a[Result] === 0) return Result;',
  3505. ' };',
  3506. ' return Result;',
  3507. '};',
  3508. '']),
  3509. LinesToStr([
  3510. '$mod.Func1([1, 2, 3]);'
  3511. ]));
  3512. end;
  3513. procedure TTestModule.TestFunctionResultInTypeCast;
  3514. begin
  3515. StartProgram(false);
  3516. Add([
  3517. 'function GetInt: longint;',
  3518. 'begin',
  3519. 'end;',
  3520. 'begin',
  3521. ' if Byte(GetInt)=0 then ;',
  3522. '']);
  3523. ConvertProgram;
  3524. CheckSource('TestFunctionResultInTypeCast',
  3525. LinesToStr([ // statements
  3526. 'this.GetInt = function () {',
  3527. ' var Result = 0;',
  3528. ' return Result;',
  3529. '};',
  3530. '']),
  3531. LinesToStr([
  3532. 'if (($mod.GetInt() & 255) === 0) ;'
  3533. ]));
  3534. end;
  3535. procedure TTestModule.TestExit;
  3536. begin
  3537. StartProgram(false);
  3538. Add('procedure ProcA;');
  3539. Add('begin');
  3540. Add(' exit;');
  3541. Add('end;');
  3542. Add('function FuncB: longint;');
  3543. Add('begin');
  3544. Add(' exit;');
  3545. Add(' exit(3);');
  3546. Add('end;');
  3547. Add('function FuncC: string;');
  3548. Add('begin');
  3549. Add(' exit;');
  3550. Add(' exit(''a'');');
  3551. Add(' exit(''abc'');');
  3552. Add('end;');
  3553. Add('begin');
  3554. Add(' exit;');
  3555. Add(' exit(1);');
  3556. ConvertProgram;
  3557. CheckSource('TestExit',
  3558. LinesToStr([ // statements
  3559. 'this.ProcA = function () {',
  3560. ' return;',
  3561. '};',
  3562. 'this.FuncB = function () {',
  3563. ' var Result = 0;',
  3564. ' return Result;',
  3565. ' return 3;',
  3566. ' return Result;',
  3567. '};',
  3568. 'this.FuncC = function () {',
  3569. ' var Result = "";',
  3570. ' return Result;',
  3571. ' return "a";',
  3572. ' return "abc";',
  3573. ' return Result;',
  3574. '};'
  3575. ]),
  3576. LinesToStr([
  3577. 'return;',
  3578. 'return 1;',
  3579. '']));
  3580. end;
  3581. procedure TTestModule.TestExit_ResultInFinally;
  3582. begin
  3583. StartProgram(false);
  3584. Add([
  3585. 'function Run: word;',
  3586. 'begin',
  3587. ' try',
  3588. ' exit(3);', // no Result in finally -> use return 3
  3589. ' finally',
  3590. ' end;',
  3591. 'end;',
  3592. 'function Fly: word;',
  3593. 'begin',
  3594. ' try',
  3595. ' exit(3);',
  3596. ' finally',
  3597. ' if Result>0 then ;',
  3598. ' end;',
  3599. 'end;',
  3600. 'function Jump: word;',
  3601. 'begin',
  3602. ' try',
  3603. ' try',
  3604. ' exit(4);',
  3605. ' finally',
  3606. ' end;',
  3607. ' finally',
  3608. ' if Result>0 then ;',
  3609. ' end;',
  3610. 'end;',
  3611. 'begin',
  3612. '']);
  3613. ConvertProgram;
  3614. CheckSource('TestExit_ResultInFinally',
  3615. LinesToStr([ // statements
  3616. 'this.Run = function () {',
  3617. ' var Result = 0;',
  3618. ' try {',
  3619. ' return 3;',
  3620. ' } finally {',
  3621. ' };',
  3622. ' return Result;',
  3623. '};',
  3624. 'this.Fly = function () {',
  3625. ' var Result = 0;',
  3626. ' try {',
  3627. ' Result = 3;',
  3628. ' return Result;',
  3629. ' } finally {',
  3630. ' if (Result > 0) ;',
  3631. ' };',
  3632. ' return Result;',
  3633. '};',
  3634. 'this.Jump = function () {',
  3635. ' var Result = 0;',
  3636. ' try {',
  3637. ' try {',
  3638. ' Result = 4;',
  3639. ' return Result;',
  3640. ' } finally {',
  3641. ' };',
  3642. ' } finally {',
  3643. ' if (Result > 0) ;',
  3644. ' };',
  3645. ' return Result;',
  3646. '};',
  3647. '']),
  3648. LinesToStr([
  3649. '']));
  3650. end;
  3651. procedure TTestModule.TestBreak;
  3652. begin
  3653. StartProgram(false);
  3654. Add([
  3655. 'var',
  3656. ' i: longint;',
  3657. 'begin',
  3658. ' repeat',
  3659. ' break;',
  3660. ' until true;',
  3661. ' while true do',
  3662. ' break;',
  3663. ' for i:=1 to 2 do',
  3664. ' break;']);
  3665. ConvertProgram;
  3666. CheckSource('TestBreak',
  3667. LinesToStr([ // statements
  3668. 'this.i = 0;'
  3669. ]),
  3670. LinesToStr([
  3671. 'do {',
  3672. ' break;',
  3673. '} while (!true);',
  3674. 'while (true) break;',
  3675. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3676. '']));
  3677. end;
  3678. procedure TTestModule.TestBreakAsVar;
  3679. begin
  3680. StartProgram(false);
  3681. Add([
  3682. 'procedure DoIt(break: boolean);',
  3683. 'begin',
  3684. ' if break then ;',
  3685. 'end;',
  3686. 'var',
  3687. ' break: boolean;',
  3688. 'begin',
  3689. ' if break then ;']);
  3690. ConvertProgram;
  3691. CheckSource('TestBreakAsVar',
  3692. LinesToStr([ // statements
  3693. 'this.DoIt = function (Break) {',
  3694. ' if (Break) ;',
  3695. '};',
  3696. 'this.Break = false;',
  3697. '']),
  3698. LinesToStr([
  3699. 'if($mod.Break) ;',
  3700. '']));
  3701. end;
  3702. procedure TTestModule.TestContinue;
  3703. begin
  3704. StartProgram(false);
  3705. Add('var i: longint;');
  3706. Add('begin');
  3707. Add(' repeat');
  3708. Add(' continue;');
  3709. Add(' until true;');
  3710. Add(' while true do');
  3711. Add(' continue;');
  3712. Add(' for i:=1 to 2 do');
  3713. Add(' continue;');
  3714. ConvertProgram;
  3715. CheckSource('TestContinue',
  3716. LinesToStr([ // statements
  3717. 'this.i = 0;'
  3718. ]),
  3719. LinesToStr([
  3720. 'do {',
  3721. ' continue;',
  3722. '} while (!true);',
  3723. 'while (true) continue;',
  3724. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3725. '']));
  3726. end;
  3727. procedure TTestModule.TestProc_External;
  3728. begin
  3729. StartProgram(false);
  3730. Add('procedure Foo; external name ''console.log'';');
  3731. Add('function Bar: longint; external name ''get.item'';');
  3732. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3733. Add('var');
  3734. Add(' i: longint;');
  3735. Add('begin');
  3736. Add(' Foo;');
  3737. Add(' i:=Bar;');
  3738. Add(' i:=Bla(''abc'');');
  3739. ConvertProgram;
  3740. CheckSource('TestProc_External',
  3741. LinesToStr([ // statements
  3742. 'this.i = 0;'
  3743. ]),
  3744. LinesToStr([
  3745. 'console.log();',
  3746. '$mod.i = get.item();',
  3747. '$mod.i = apply.something("abc");'
  3748. ]));
  3749. end;
  3750. procedure TTestModule.TestProc_ExternalOtherUnit;
  3751. begin
  3752. AddModuleWithIntfImplSrc('unit2.pas',
  3753. LinesToStr([
  3754. 'procedure Now; external name ''Date.now'';',
  3755. 'procedure DoIt;'
  3756. ]),
  3757. 'procedure doit; begin end;');
  3758. StartUnit(true);
  3759. Add('interface');
  3760. Add('uses unit2;');
  3761. Add('implementation');
  3762. Add('begin');
  3763. Add(' now;');
  3764. Add(' now();');
  3765. Add(' uNit2.now;');
  3766. Add(' uNit2.now();');
  3767. Add(' doit;');
  3768. Add(' uNit2.doit;');
  3769. ConvertUnit;
  3770. CheckSource('TestProc_ExternalOtherUnit',
  3771. LinesToStr([
  3772. '']),
  3773. LinesToStr([
  3774. 'Date.now();',
  3775. 'Date.now();',
  3776. 'Date.now();',
  3777. 'Date.now();',
  3778. 'pas.unit2.DoIt();',
  3779. 'pas.unit2.DoIt();',
  3780. '']));
  3781. end;
  3782. procedure TTestModule.TestProc_Asm;
  3783. begin
  3784. StartProgram(false);
  3785. Add([
  3786. '{$mode delphi}',
  3787. 'function DoIt: longint;',
  3788. 'begin;',
  3789. ' asm',
  3790. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3791. ' end;',
  3792. ' asm console.log(); end;',
  3793. ' asm',
  3794. ' s = "'' ";',
  3795. ' s = ''" '';',
  3796. ' s = s + "world" + "''";',
  3797. ' // end',
  3798. ' s = ''end'';',
  3799. ' s = "end";',
  3800. ' s = "foo\"bar";',
  3801. ' s = ''a\''b'';',
  3802. ' s = `${expr}\`-"-''-`;',
  3803. ' s = `multi',
  3804. 'line`;',
  3805. ' end;',
  3806. 'end;',
  3807. 'procedure Fly;',
  3808. 'asm',
  3809. ' return;',
  3810. 'end;',
  3811. 'begin']);
  3812. ConvertProgram;
  3813. CheckSource('TestProc_Asm',
  3814. LinesToStr([ // statements
  3815. 'this.DoIt = function () {',
  3816. ' var Result = 0;',
  3817. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3818. ' console.log();',
  3819. ' s = "'' ";',
  3820. ' s = ''" '';',
  3821. ' s = s + "world" + "''";',
  3822. ' // end',
  3823. ' s = ''end'';',
  3824. ' s = "end";',
  3825. ' s = "foo\"bar";',
  3826. ' s = ''a\''b'';',
  3827. ' s = `${expr}\`-"-''-`;',
  3828. ' s = `multi',
  3829. 'line`;',
  3830. ' return Result;',
  3831. '};',
  3832. 'this.Fly = function () {',
  3833. ' return;',
  3834. '};',
  3835. '']),
  3836. LinesToStr([
  3837. ''
  3838. ]));
  3839. end;
  3840. procedure TTestModule.TestProc_Assembler;
  3841. begin
  3842. StartProgram(false);
  3843. Add('function DoIt: longint; assembler;');
  3844. Add('asm');
  3845. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3846. Add('end;');
  3847. Add('begin');
  3848. ConvertProgram;
  3849. CheckSource('TestProc_Assembler',
  3850. LinesToStr([ // statements
  3851. 'this.DoIt = function () {',
  3852. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3853. '};'
  3854. ]),
  3855. LinesToStr([
  3856. ''
  3857. ]));
  3858. end;
  3859. procedure TTestModule.TestProc_VarParam;
  3860. begin
  3861. StartProgram(false);
  3862. Add('type integer = longint;');
  3863. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3864. Add('var vJ: integer;');
  3865. Add('begin');
  3866. Add(' vg:=vg+1;');
  3867. Add(' vj:=vh+2;');
  3868. Add(' vi:=vi+3;');
  3869. Add(' doit(vg,vg,vg);');
  3870. Add(' doit(vh,vh,vj);');
  3871. Add(' doit(vi,vi,vi);');
  3872. Add(' doit(vj,vj,vj);');
  3873. Add('end;');
  3874. Add('var i: integer;');
  3875. Add('begin');
  3876. Add(' doit(i,i,i);');
  3877. ConvertProgram;
  3878. CheckSource('TestProc_VarParam',
  3879. LinesToStr([ // statements
  3880. 'this.DoIt = function (vG,vH,vI) {',
  3881. ' var vJ = 0;',
  3882. ' vG = vG + 1;',
  3883. ' vJ = vH + 2;',
  3884. ' vI.set(vI.get()+3);',
  3885. ' $mod.DoIt(vG, vG, {',
  3886. ' get: function () {',
  3887. ' return vG;',
  3888. ' },',
  3889. ' set: function (v) {',
  3890. ' vG = v;',
  3891. ' }',
  3892. ' });',
  3893. ' $mod.DoIt(vH, vH, {',
  3894. ' get: function () {',
  3895. ' return vJ;',
  3896. ' },',
  3897. ' set: function (v) {',
  3898. ' vJ = v;',
  3899. ' }',
  3900. ' });',
  3901. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3902. ' $mod.DoIt(vJ, vJ, {',
  3903. ' get: function () {',
  3904. ' return vJ;',
  3905. ' },',
  3906. ' set: function (v) {',
  3907. ' vJ = v;',
  3908. ' }',
  3909. ' });',
  3910. '};',
  3911. 'this.i = 0;'
  3912. ]),
  3913. LinesToStr([
  3914. '$mod.DoIt($mod.i,$mod.i,{',
  3915. ' p: $mod,',
  3916. ' get: function () {',
  3917. ' return this.p.i;',
  3918. ' },',
  3919. ' set: function (v) {',
  3920. ' this.p.i = v;',
  3921. ' }',
  3922. '});'
  3923. ]));
  3924. end;
  3925. procedure TTestModule.TestProc_VarParamString;
  3926. begin
  3927. StartProgram(false);
  3928. Add(['type TCaption = string;',
  3929. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3930. 'var c: char;',
  3931. 'begin',
  3932. ' va[1]:=c;',
  3933. ' vb[2]:=c;',
  3934. ' vc[3]:=c;',
  3935. 'end;',
  3936. 'begin']);
  3937. ConvertProgram;
  3938. CheckSource('TestProc_VarParamString',
  3939. LinesToStr([ // statements
  3940. 'this.DoIt = function (vA,vB,vC) {',
  3941. ' var c = "";',
  3942. ' vA = rtl.setCharAt(vA, 0, c);',
  3943. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3944. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3945. '};',
  3946. '']),
  3947. LinesToStr([
  3948. ]));
  3949. end;
  3950. procedure TTestModule.TestProc_VarParamV;
  3951. begin
  3952. StartProgram(false);
  3953. Add([
  3954. 'procedure Inc2(var i: longint);',
  3955. 'begin',
  3956. ' i:=i+2;',
  3957. 'end;',
  3958. 'procedure DoIt(v: longint);',
  3959. 'var p: array of longint;',
  3960. 'begin',
  3961. ' Inc2(v);',
  3962. ' Inc2(p[v]);',
  3963. 'end;',
  3964. 'begin']);
  3965. ConvertProgram;
  3966. CheckSource('TestProc_VarParamV',
  3967. LinesToStr([ // statements
  3968. 'this.Inc2 = function (i) {',
  3969. ' i.set(i.get()+2);',
  3970. '};',
  3971. 'this.DoIt = function (v) {',
  3972. ' var p = [];',
  3973. ' $mod.Inc2({get: function () {',
  3974. ' return v;',
  3975. ' }, set: function (w) {',
  3976. ' v = w;',
  3977. ' }});',
  3978. ' $mod.Inc2({',
  3979. ' a: v,',
  3980. ' p: p,',
  3981. ' get: function () {',
  3982. ' return this.p[this.a];',
  3983. ' },',
  3984. ' set: function (v) {',
  3985. ' this.p[this.a] = v;',
  3986. ' }',
  3987. ' });',
  3988. '};',
  3989. '']),
  3990. LinesToStr([
  3991. '']));
  3992. end;
  3993. procedure TTestModule.TestProc_Overload;
  3994. begin
  3995. StartProgram(false);
  3996. Add('procedure DoIt(vI: longint); begin end;');
  3997. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3998. Add('procedure DoIt(vD: double); begin end;');
  3999. Add('begin');
  4000. Add(' DoIt(1);');
  4001. Add(' DoIt(2,3);');
  4002. Add(' DoIt(4.5);');
  4003. ConvertProgram;
  4004. CheckSource('TestProcedureOverload',
  4005. LinesToStr([ // statements
  4006. 'this.DoIt = function (vI) {',
  4007. '};',
  4008. 'this.DoIt$1 = function (vI, vJ) {',
  4009. '};',
  4010. 'this.DoIt$2 = function (vD) {',
  4011. '};',
  4012. '']),
  4013. LinesToStr([
  4014. '$mod.DoIt(1);',
  4015. '$mod.DoIt$1(2, 3);',
  4016. '$mod.DoIt$2(4.5);',
  4017. '']));
  4018. end;
  4019. procedure TTestModule.TestProc_OverloadForward;
  4020. begin
  4021. StartProgram(false);
  4022. Add('procedure DoIt(vI: longint); forward;');
  4023. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4024. Add('procedure doit(vi: longint); begin end;');
  4025. Add('begin');
  4026. Add(' doit(1);');
  4027. Add(' doit(2,3);');
  4028. ConvertProgram;
  4029. CheckSource('TestProcedureOverloadForward',
  4030. LinesToStr([ // statements
  4031. 'this.DoIt$1 = function (vI, vJ) {',
  4032. '};',
  4033. 'this.DoIt = function (vI) {',
  4034. '};',
  4035. '']),
  4036. LinesToStr([
  4037. '$mod.DoIt(1);',
  4038. '$mod.DoIt$1(2, 3);',
  4039. '']));
  4040. end;
  4041. procedure TTestModule.TestProc_OverloadIntfImpl;
  4042. begin
  4043. StartUnit(false);
  4044. Add('interface');
  4045. Add('procedure DoIt(vI: longint);');
  4046. Add('procedure DoIt(vI, vJ: longint);');
  4047. Add('implementation');
  4048. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4049. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4050. Add('procedure DoIt(vi: longint); begin end;');
  4051. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4052. Add('procedure DoIt(vi, vj: longint); begin end;');
  4053. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4054. Add('begin');
  4055. Add(' doit(1);');
  4056. Add(' doit(2,3);');
  4057. Add(' doit(4,5,6);');
  4058. Add(' doit(7,8,9,10);');
  4059. Add(' doit(11,12,13,14,15);');
  4060. ConvertUnit;
  4061. CheckSource('TestProcedureOverloadUnit',
  4062. LinesToStr([ // statements
  4063. 'var $impl = $mod.$impl;',
  4064. 'this.DoIt = function (vI) {',
  4065. '};',
  4066. 'this.DoIt$1 = function (vI, vJ) {',
  4067. '};',
  4068. '']),
  4069. LinesToStr([ // this.$init
  4070. '$mod.DoIt(1);',
  4071. '$mod.DoIt$1(2, 3);',
  4072. '$impl.DoIt$3(4,5,6);',
  4073. '$impl.DoIt$4(7,8,9,10);',
  4074. '$impl.DoIt$2(11,12,13,14,15);',
  4075. '']),
  4076. LinesToStr([ // implementation
  4077. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4078. '};',
  4079. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4080. '};',
  4081. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4082. '};',
  4083. '']));
  4084. end;
  4085. procedure TTestModule.TestProc_OverloadNested;
  4086. begin
  4087. StartProgram(false);
  4088. Add([
  4089. 'procedure doit(vA: longint);',
  4090. ' procedure DoIt(vA, vB: longint); overload;',
  4091. ' begin',
  4092. ' doit(1);',
  4093. ' doit(1,2);',
  4094. ' end;',
  4095. ' procedure doit(vA, vB, vC: longint);',
  4096. ' begin',
  4097. ' doit(1);',
  4098. ' doit(1,2);',
  4099. ' doit(1,2,3);',
  4100. ' end;',
  4101. 'begin',
  4102. ' doit(1);',
  4103. ' doit(1,2);',
  4104. ' doit(1,2,3);',
  4105. 'end;',
  4106. 'begin // main',
  4107. ' doit(1);']);
  4108. ConvertProgram;
  4109. CheckSource('TestProcedureOverloadNested',
  4110. LinesToStr([ // statements
  4111. 'this.doit = function (vA) {',
  4112. ' function DoIt$1(vA, vB) {',
  4113. ' $mod.doit(1);',
  4114. ' DoIt$1(1, 2);',
  4115. ' };',
  4116. ' function doit$2(vA, vB, vC) {',
  4117. ' $mod.doit(1);',
  4118. ' DoIt$1(1, 2);',
  4119. ' doit$2(1, 2, 3);',
  4120. ' };',
  4121. ' $mod.doit(1);',
  4122. ' DoIt$1(1, 2);',
  4123. ' doit$2(1, 2, 3);',
  4124. '};',
  4125. '']),
  4126. LinesToStr([
  4127. '$mod.doit(1);',
  4128. '']));
  4129. end;
  4130. procedure TTestModule.TestProc_OverloadNestedForward;
  4131. begin
  4132. StartProgram(false);
  4133. Add([
  4134. 'procedure DoIt(vA: longint); overload; forward;',
  4135. 'procedure DoIt(vB, vC: longint); overload;',
  4136. 'begin // 2 param overload',
  4137. ' doit(1);',
  4138. ' doit(1,2);',
  4139. 'end;',
  4140. 'procedure doit(vA: longint);',
  4141. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4142. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4143. ' begin // 4 param overload',
  4144. ' doit(1);',
  4145. ' doit(1,2);',
  4146. ' doit(1,2,3);',
  4147. ' doit(1,2,3,4);',
  4148. ' end;',
  4149. ' procedure doit(vA, vB, vC: longint);',
  4150. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4151. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4152. ' begin // 6 param overload',
  4153. ' doit(1);',
  4154. ' doit(1,2);',
  4155. ' doit(1,2,3);',
  4156. ' doit(1,2,3,4);',
  4157. ' doit(1,2,3,4,5);',
  4158. ' doit(1,2,3,4,5,6);',
  4159. ' end;',
  4160. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4161. ' begin // 5 param overload',
  4162. ' doit(1);',
  4163. ' doit(1,2);',
  4164. ' doit(1,2,3);',
  4165. ' doit(1,2,3,4);',
  4166. ' doit(1,2,3,4,5);',
  4167. ' doit(1,2,3,4,5,6);',
  4168. ' end;',
  4169. ' begin // 3 param overload',
  4170. ' doit(1);',
  4171. ' doit(1,2);',
  4172. ' doit(1,2,3);',
  4173. ' doit(1,2,3,4);',
  4174. ' doit(1,2,3,4,5);',
  4175. ' doit(1,2,3,4,5,6);',
  4176. ' end;',
  4177. 'begin // 1 param overload',
  4178. ' doit(1);',
  4179. ' doit(1,2);',
  4180. ' doit(1,2,3);',
  4181. ' doit(1,2,3,4);',
  4182. 'end;',
  4183. 'begin // main',
  4184. ' doit(1);',
  4185. ' doit(1,2);']);
  4186. ConvertProgram;
  4187. CheckSource('TestProc_OverloadNestedForward',
  4188. LinesToStr([ // statements
  4189. 'this.DoIt$1 = function (vB, vC) {',
  4190. ' $mod.DoIt(1);',
  4191. ' $mod.DoIt$1(1, 2);',
  4192. '};',
  4193. 'this.DoIt = function (vA) {',
  4194. ' function DoIt$3(vA, vB, vC, vD) {',
  4195. ' $mod.DoIt(1);',
  4196. ' $mod.DoIt$1(1, 2);',
  4197. ' DoIt$2(1, 2, 3);',
  4198. ' DoIt$3(1, 2, 3, 4);',
  4199. ' };',
  4200. ' function DoIt$2(vA, vB, vC) {',
  4201. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4202. ' $mod.DoIt(1);',
  4203. ' $mod.DoIt$1(1, 2);',
  4204. ' DoIt$2(1, 2, 3);',
  4205. ' DoIt$3(1, 2, 3, 4);',
  4206. ' DoIt$4(1, 2, 3, 4, 5);',
  4207. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4208. ' };',
  4209. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4210. ' $mod.DoIt(1);',
  4211. ' $mod.DoIt$1(1, 2);',
  4212. ' DoIt$2(1, 2, 3);',
  4213. ' DoIt$3(1, 2, 3, 4);',
  4214. ' DoIt$4(1, 2, 3, 4, 5);',
  4215. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4216. ' };',
  4217. ' $mod.DoIt(1);',
  4218. ' $mod.DoIt$1(1, 2);',
  4219. ' DoIt$2(1, 2, 3);',
  4220. ' DoIt$3(1, 2, 3, 4);',
  4221. ' DoIt$4(1, 2, 3, 4, 5);',
  4222. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4223. ' };',
  4224. ' $mod.DoIt(1);',
  4225. ' $mod.DoIt$1(1, 2);',
  4226. ' DoIt$2(1, 2, 3);',
  4227. ' DoIt$3(1, 2, 3, 4);',
  4228. '};',
  4229. '']),
  4230. LinesToStr([
  4231. '$mod.DoIt(1);',
  4232. '$mod.DoIt$1(1, 2);',
  4233. '']));
  4234. end;
  4235. procedure TTestModule.TestProc_OverloadUnitCycle;
  4236. begin
  4237. AddModuleWithIntfImplSrc('Unit2.pas',
  4238. LinesToStr([
  4239. 'type',
  4240. ' TObject = class',
  4241. ' procedure DoIt(b: boolean); virtual; abstract;',
  4242. ' procedure DoIt(i: longint); virtual; abstract;',
  4243. ' end;',
  4244. '']),
  4245. 'uses test1;');
  4246. StartUnit(true);
  4247. Add([
  4248. 'interface',
  4249. 'uses unit2;',
  4250. 'type',
  4251. ' TEagle = class(TObject)',
  4252. ' procedure DoIt(b: boolean); override;',
  4253. ' procedure DoIt(i: longint); override;',
  4254. ' end;',
  4255. 'implementation',
  4256. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4257. 'procedure TEagle.DoIt(i: longint); begin end;',
  4258. '']);
  4259. ConvertUnit;
  4260. CheckSource('TestProc_OverloadUnitCycle',
  4261. LinesToStr([ // statements
  4262. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4263. ' this.DoIt = function (b) {',
  4264. ' };',
  4265. ' this.DoIt$1 = function (i) {',
  4266. ' };',
  4267. '});',
  4268. '']),
  4269. '',
  4270. LinesToStr([
  4271. '']));
  4272. end;
  4273. procedure TTestModule.TestProc_Varargs;
  4274. begin
  4275. StartProgram(false);
  4276. Add([
  4277. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4278. 'procedure ProcB; varargs; external name ''ProcB'';',
  4279. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4280. 'function GetIt: longint; begin end;',
  4281. 'begin',
  4282. ' ProcA(1);',
  4283. ' ProcA(1,2);',
  4284. ' ProcA(1,2.0);',
  4285. ' ProcA(1,2,3);',
  4286. ' ProcA(1,''2'');',
  4287. ' ProcA(2,'''');',
  4288. ' ProcA(3,false);',
  4289. ' ProcB;',
  4290. ' ProcB();',
  4291. ' ProcB(4);',
  4292. ' ProcB(''foo'');',
  4293. ' ProcC;',
  4294. ' ProcC();',
  4295. ' ProcC(4);',
  4296. ' ProcC(5,''foo'');',
  4297. ' ProcB(GetIt);',
  4298. ' ProcB(GetIt());',
  4299. ' ProcB(GetIt,GetIt());']);
  4300. ConvertProgram;
  4301. CheckSource('TestProc_Varargs',
  4302. LinesToStr([ // statements
  4303. 'this.GetIt = function () {',
  4304. ' var Result = 0;',
  4305. ' return Result;',
  4306. '};',
  4307. '']),
  4308. LinesToStr([
  4309. 'ProcA(1);',
  4310. 'ProcA(1, 2);',
  4311. 'ProcA(1, 2.0);',
  4312. 'ProcA(1, 2, 3);',
  4313. 'ProcA(1, "2");',
  4314. 'ProcA(2, "");',
  4315. 'ProcA(3, false);',
  4316. 'ProcB();',
  4317. 'ProcB();',
  4318. 'ProcB(4);',
  4319. 'ProcB("foo");',
  4320. 'ProcC(17);',
  4321. 'ProcC(17);',
  4322. 'ProcC(4);',
  4323. 'ProcC(5, "foo");',
  4324. 'ProcB($mod.GetIt());',
  4325. 'ProcB($mod.GetIt());',
  4326. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4327. '']));
  4328. end;
  4329. procedure TTestModule.TestProc_ConstOrder;
  4330. begin
  4331. StartProgram(false);
  4332. Add([
  4333. 'const A = 3;',
  4334. 'const B = A+1;',
  4335. 'procedure DoIt;',
  4336. 'const C = A+1;',
  4337. 'const D = B+1;',
  4338. 'const E = D+C+B+A;',
  4339. 'begin',
  4340. 'end;',
  4341. 'begin'
  4342. ]);
  4343. ConvertProgram;
  4344. CheckSource('TestProc_ConstOrder',
  4345. LinesToStr([ // statements
  4346. 'this.A = 3;',
  4347. 'this.B = 3 + 1;',
  4348. 'var C = 3 + 1;',
  4349. 'var D = 4 + 1;',
  4350. 'var E = 5 + 4 + 4 + 3;',
  4351. 'this.DoIt = function () {',
  4352. '};',
  4353. '']),
  4354. LinesToStr([
  4355. ''
  4356. ]));
  4357. end;
  4358. procedure TTestModule.TestProc_DuplicateConst;
  4359. begin
  4360. StartProgram(false);
  4361. Add([
  4362. 'const A = 1;',
  4363. 'procedure DoIt;',
  4364. 'const A = 2;',
  4365. ' procedure SubIt;',
  4366. ' const A = 21;',
  4367. ' begin',
  4368. ' end;',
  4369. 'begin',
  4370. 'end;',
  4371. 'procedure DoSome;',
  4372. 'const A = 3;',
  4373. 'begin',
  4374. 'end;',
  4375. 'begin'
  4376. ]);
  4377. ConvertProgram;
  4378. CheckSource('TestProc_DuplicateConst',
  4379. LinesToStr([ // statements
  4380. 'this.A = 1;',
  4381. 'var A$1 = 2;',
  4382. 'var A$2 = 21;',
  4383. 'this.DoIt = function () {',
  4384. ' function SubIt() {',
  4385. ' };',
  4386. '};',
  4387. 'var A$3 = 3;',
  4388. 'this.DoSome = function () {',
  4389. '};',
  4390. '']),
  4391. LinesToStr([
  4392. ''
  4393. ]));
  4394. end;
  4395. procedure TTestModule.TestProc_LocalVarAbsolute;
  4396. begin
  4397. StartProgram(false);
  4398. Add([
  4399. 'type',
  4400. ' TObject = class',
  4401. ' Index: longint;',
  4402. ' procedure DoAbs(Item: pointer);',
  4403. ' end;',
  4404. 'procedure TObject.DoAbs(Item: pointer);',
  4405. 'var',
  4406. ' o: TObject absolute Item;',
  4407. 'begin',
  4408. ' if o.Index<o.Index then o.Index:=o.Index;',
  4409. 'end;',
  4410. 'procedure DoIt(i: longint; p: pointer);',
  4411. 'var',
  4412. ' d: double absolute i;',
  4413. ' s: string absolute d;',
  4414. ' oi: TObject absolute i;',
  4415. ' op: TObject absolute p;',
  4416. 'begin',
  4417. ' if d=d then d:=d;',
  4418. ' if s=s then s:=s;',
  4419. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4420. ' if op.Index=op.Index then op.Index:=op.Index;',
  4421. 'end;',
  4422. 'begin']);
  4423. ConvertProgram;
  4424. CheckSource('TestProc_LocalVarAbsolute',
  4425. LinesToStr([ // statements
  4426. 'rtl.createClass(this, "TObject", null, function () {',
  4427. ' this.$init = function () {',
  4428. ' this.Index = 0;',
  4429. ' };',
  4430. ' this.$final = function () {',
  4431. ' };',
  4432. ' this.DoAbs = function (Item) {',
  4433. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4434. ' };',
  4435. '});',
  4436. 'this.DoIt = function (i, p) {',
  4437. ' if (i === i) i = i;',
  4438. ' if (i === i) i = i;',
  4439. ' if (i.Index < i.Index) i.Index = i.Index;',
  4440. ' if (p.Index === p.Index) p.Index = p.Index;',
  4441. '};'
  4442. ]),
  4443. LinesToStr([
  4444. ]));
  4445. end;
  4446. procedure TTestModule.TestProc_LocalVarInit;
  4447. begin
  4448. StartProgram(false);
  4449. Add([
  4450. 'type TBytes = array of byte;',
  4451. 'procedure DoIt;',
  4452. 'const c = 4;',
  4453. 'var',
  4454. ' b: byte = 1;',
  4455. ' w: word = 2+c;',
  4456. ' p: pointer = nil;',
  4457. ' Buffer: TBytes = nil;',
  4458. 'begin',
  4459. 'end;',
  4460. 'begin']);
  4461. ConvertProgram;
  4462. CheckSource('TestProc_LocalVarInit',
  4463. LinesToStr([ // statements
  4464. 'var c = 4;',
  4465. 'this.DoIt = function () {',
  4466. ' var b = 1;',
  4467. ' var w = 2 + 4;',
  4468. ' var p = null;',
  4469. ' var Buffer = [];',
  4470. '};',
  4471. '']),
  4472. LinesToStr([
  4473. ]));
  4474. end;
  4475. procedure TTestModule.TestProc_ReservedWords;
  4476. begin
  4477. StartProgram(false);
  4478. Add([
  4479. 'procedure Date(ArrayBuffer: longint);',
  4480. 'const',
  4481. ' NaN: longint = 3;',
  4482. 'var',
  4483. ' &Boolean: longint;',
  4484. ' procedure Error(ArrayBuffer: longint);',
  4485. ' begin',
  4486. ' end;',
  4487. 'begin',
  4488. ' Nan:=&bOolean;',
  4489. 'end;',
  4490. 'begin',
  4491. ' Date(1);']);
  4492. ConvertProgram;
  4493. CheckSource('TestProc_ReservedWords',
  4494. LinesToStr([ // statements
  4495. 'var naN = 3;',
  4496. 'this.Date = function (arrayBuffer) {',
  4497. ' var boolean = 0;',
  4498. ' function error(arrayBuffer) {',
  4499. ' };',
  4500. ' naN = boolean;',
  4501. '};',
  4502. '']),
  4503. LinesToStr([
  4504. ' $mod.Date(1);'
  4505. ]));
  4506. end;
  4507. procedure TTestModule.TestProc_ConstRefWord;
  4508. begin
  4509. StartProgram(false);
  4510. Add([
  4511. 'procedure Run(constref w: word);',
  4512. 'var l: word;',
  4513. 'begin',
  4514. ' l:=w;',
  4515. ' Run(w);',
  4516. ' Run(l);',
  4517. 'end;',
  4518. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4519. 'begin',
  4520. ' Run(a);',
  4521. ' Run(b);',
  4522. ' Run(c);',
  4523. ' Run(d);',
  4524. ' Run(e);',
  4525. 'end;',
  4526. 'begin',
  4527. ' Run(1);']);
  4528. ConvertProgram;
  4529. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4530. CheckSource('TestProc_ConstRefWord',
  4531. LinesToStr([ // statements
  4532. 'this.Run = function (w) {',
  4533. ' var l = 0;',
  4534. ' l = w;',
  4535. ' $mod.Run(w);',
  4536. ' $mod.Run(l);',
  4537. '};',
  4538. 'this.Fly = function (a, b, c, d, e) {',
  4539. ' $mod.Run(a);',
  4540. ' $mod.Run(b.get());',
  4541. ' $mod.Run(c.get());',
  4542. ' $mod.Run(d);',
  4543. ' $mod.Run(e);',
  4544. '};',
  4545. '']),
  4546. LinesToStr([
  4547. '$mod.Run(1);'
  4548. ]));
  4549. end;
  4550. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4551. begin
  4552. StartProgram(false);
  4553. Add([
  4554. '{$mode objfpc}',
  4555. 'type',
  4556. ' TFunc = reference to function(x: word): word;',
  4557. 'var Func: TFunc;',
  4558. 'procedure DoIt(a: word);',
  4559. 'begin',
  4560. ' Func:=function(b:word): word',
  4561. ' begin',
  4562. ' Result:=a+b;',
  4563. ' exit(b);',
  4564. ' exit(Result);',
  4565. ' end;',// test semicolon
  4566. ' a:=3;',
  4567. 'end;',
  4568. 'begin',
  4569. ' Func:=function(c:word):word begin',
  4570. ' Result:=3+c;',
  4571. ' exit(c);',
  4572. ' exit(Result);',
  4573. ' end;']);
  4574. ConvertProgram;
  4575. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4576. LinesToStr([ // statements
  4577. 'this.Func = null;',
  4578. 'this.DoIt = function (a) {',
  4579. ' $mod.Func = function (b) {',
  4580. ' var Result = 0;',
  4581. ' Result = a + b;',
  4582. ' return b;',
  4583. ' return Result;',
  4584. ' return Result;',
  4585. ' };',
  4586. ' a = 3;',
  4587. '};',
  4588. '']),
  4589. LinesToStr([
  4590. '$mod.Func = function (c) {',
  4591. ' var Result = 0;',
  4592. ' Result = 3 + c;',
  4593. ' return c;',
  4594. ' return Result;',
  4595. ' return Result;',
  4596. '};',
  4597. '']));
  4598. end;
  4599. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4600. begin
  4601. StartProgram(false);
  4602. Add([
  4603. '{$mode delphi}',
  4604. 'type',
  4605. ' TProc = reference to procedure(x: word);',
  4606. 'procedure DoIt(a: word);',
  4607. 'var Proc: TProc;',
  4608. 'begin',
  4609. ' Proc:=procedure(b:word) begin end;',
  4610. 'end;',
  4611. 'var Proc: TProc;',
  4612. 'begin',
  4613. ' Proc:=procedure(c:word) begin end;',
  4614. '']);
  4615. ConvertProgram;
  4616. CheckSource('TestAnonymousProc_Assign_Delphi',
  4617. LinesToStr([ // statements
  4618. 'this.DoIt = function (a) {',
  4619. ' var Proc = null;',
  4620. ' Proc = function (b) {',
  4621. ' };',
  4622. '};',
  4623. 'this.Proc = null;',
  4624. '']),
  4625. LinesToStr([
  4626. '$mod.Proc = function (c) {',
  4627. '};',
  4628. '']));
  4629. end;
  4630. procedure TTestModule.TestAnonymousProc_Arg;
  4631. begin
  4632. StartProgram(false);
  4633. Add([
  4634. 'type',
  4635. ' TProc = reference to procedure;',
  4636. ' TFunc = reference to function(x: word): word;',
  4637. 'procedure DoMore(f,g: TProc);',
  4638. 'begin',
  4639. 'end;',
  4640. 'procedure DoOdd(v: jsvalue);',
  4641. 'begin',
  4642. 'end;',
  4643. 'procedure DoIt(f: TFunc);',
  4644. 'begin',
  4645. ' DoIt(function(b:word): word',
  4646. ' begin',
  4647. ' Result:=1+b;',
  4648. ' end);',
  4649. ' DoMore(procedure begin end, procedure begin end);',
  4650. ' DoOdd(procedure begin end);',
  4651. 'end;',
  4652. 'begin',
  4653. ' DoMore(procedure begin end,',
  4654. ' procedure assembler asm',
  4655. ' console.log("c");',
  4656. ' end);',
  4657. '']);
  4658. ConvertProgram;
  4659. CheckSource('TestAnonymousProc_Arg',
  4660. LinesToStr([ // statements
  4661. 'this.DoMore = function (f, g) {',
  4662. '};',
  4663. 'this.DoOdd = function (v) {',
  4664. '};',
  4665. 'this.DoIt = function (f) {',
  4666. ' $mod.DoIt(function (b) {',
  4667. ' var Result = 0;',
  4668. ' Result = 1 + b;',
  4669. ' return Result;',
  4670. ' });',
  4671. ' $mod.DoMore(function () {',
  4672. ' }, function () {',
  4673. ' });',
  4674. ' $mod.DoOdd(function () {',
  4675. ' });',
  4676. '};',
  4677. '']),
  4678. LinesToStr([
  4679. '$mod.DoMore(function () {',
  4680. '}, function () {',
  4681. ' console.log("c");',
  4682. '});',
  4683. '']));
  4684. end;
  4685. procedure TTestModule.TestAnonymousProc_Typecast;
  4686. begin
  4687. StartProgram(false);
  4688. Add([
  4689. 'type',
  4690. ' TProc = reference to procedure(w: word);',
  4691. ' TArr = array of word;',
  4692. ' TFuncArr = reference to function: TArr;',
  4693. 'procedure DoIt(p: TProc);',
  4694. 'var',
  4695. ' w: word;',
  4696. ' a: TArr;',
  4697. 'begin',
  4698. ' p:=TProc(procedure(b: smallint) begin end);',
  4699. ' a:=TFuncArr(function: TArr begin end)();',
  4700. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4701. 'end;',
  4702. 'begin']);
  4703. ConvertProgram;
  4704. CheckSource('TestAnonymousProc_Typecast',
  4705. LinesToStr([ // statements
  4706. 'this.DoIt = function (p) {',
  4707. ' var w = 0;',
  4708. ' var a = [];',
  4709. ' p = function (b) {',
  4710. ' };',
  4711. ' a = function () {',
  4712. ' var Result = [];',
  4713. ' return Result;',
  4714. ' }();',
  4715. ' w = function () {',
  4716. ' var Result = [];',
  4717. ' return Result;',
  4718. ' }()[3];',
  4719. '};',
  4720. '']),
  4721. LinesToStr([
  4722. '']));
  4723. end;
  4724. procedure TTestModule.TestAnonymousProc_With;
  4725. begin
  4726. StartProgram(false);
  4727. Add([
  4728. 'type',
  4729. ' TProc = reference to procedure(w: word);',
  4730. ' TObject = class',
  4731. ' b: boolean;',
  4732. ' end;',
  4733. 'var',
  4734. ' p: TProc;',
  4735. ' bird: TObject;',
  4736. 'begin',
  4737. ' with bird do',
  4738. ' p:=procedure(w: word)',
  4739. ' begin',
  4740. ' b:=w>2;',
  4741. ' end;',
  4742. '']);
  4743. ConvertProgram;
  4744. CheckSource('TestAnonymousProc_With',
  4745. LinesToStr([ // statements
  4746. 'rtl.createClass(this, "TObject", null, function () {',
  4747. ' this.$init = function () {',
  4748. ' this.b = false;',
  4749. ' };',
  4750. ' this.$final = function () {',
  4751. ' };',
  4752. '});',
  4753. 'this.p = null;',
  4754. 'this.bird = null;',
  4755. '']),
  4756. LinesToStr([
  4757. 'var $with = $mod.bird;',
  4758. '$mod.p = function (w) {',
  4759. ' $with.b = w > 2;',
  4760. '};',
  4761. '']));
  4762. end;
  4763. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4764. begin
  4765. StartProgram(false);
  4766. Add([
  4767. 'type',
  4768. ' TProc = reference to procedure;',
  4769. ' TObject = class',
  4770. ' b: boolean;',
  4771. ' end;',
  4772. 'procedure DoIt;',
  4773. 'var',
  4774. ' p: TProc;',
  4775. 'begin',
  4776. ' try',
  4777. ' except',
  4778. ' on E: TObject do',
  4779. ' p:=procedure',
  4780. ' begin',
  4781. ' E.b:=true;',
  4782. ' end;',
  4783. ' end;',
  4784. 'end;',
  4785. 'begin']);
  4786. ConvertProgram;
  4787. CheckSource('TestAnonymousProc_ExceptOn',
  4788. LinesToStr([ // statements
  4789. 'rtl.createClass(this, "TObject", null, function () {',
  4790. ' this.$init = function () {',
  4791. ' this.b = false;',
  4792. ' };',
  4793. ' this.$final = function () {',
  4794. ' };',
  4795. '});',
  4796. 'this.DoIt = function () {',
  4797. ' var p = null;',
  4798. ' try {} catch ($e) {',
  4799. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4800. ' var E = $e;',
  4801. ' p = function () {',
  4802. ' E.b = true;',
  4803. ' };',
  4804. ' } else throw $e',
  4805. ' };',
  4806. '};',
  4807. '']),
  4808. LinesToStr([
  4809. '']));
  4810. end;
  4811. procedure TTestModule.TestAnonymousProc_Nested;
  4812. begin
  4813. StartProgram(false);
  4814. Add([
  4815. 'type',
  4816. ' TProc = reference to procedure;',
  4817. ' TObject = class',
  4818. ' i: byte;',
  4819. ' procedure DoIt;',
  4820. ' end;',
  4821. 'procedure TObject.DoIt;',
  4822. 'var',
  4823. ' p: TProc;',
  4824. ' procedure Sub;',
  4825. ' begin',
  4826. ' p:=procedure',
  4827. ' begin',
  4828. ' i:=3;',
  4829. ' Self.i:=4;',
  4830. ' p:=procedure',
  4831. ' procedure SubSub;',
  4832. ' begin',
  4833. ' i:=13;',
  4834. ' Self.i:=14;',
  4835. ' end;',
  4836. ' begin',
  4837. ' i:=13;',
  4838. ' Self.i:=14;',
  4839. ' end;',
  4840. ' end;',
  4841. ' end;',
  4842. 'begin',
  4843. 'end;',
  4844. 'begin']);
  4845. ConvertProgram;
  4846. CheckSource('TestAnonymousProc_Nested',
  4847. LinesToStr([ // statements
  4848. 'rtl.createClass(this, "TObject", null, function () {',
  4849. ' this.$init = function () {',
  4850. ' this.i = 0;',
  4851. ' };',
  4852. ' this.$final = function () {',
  4853. ' };',
  4854. ' this.DoIt = function () {',
  4855. ' var $Self = this;',
  4856. ' var p = null;',
  4857. ' function Sub() {',
  4858. ' p = function () {',
  4859. ' $Self.i = 3;',
  4860. ' $Self.i = 4;',
  4861. ' p = function () {',
  4862. ' function SubSub() {',
  4863. ' $Self.i = 13;',
  4864. ' $Self.i = 14;',
  4865. ' };',
  4866. ' $Self.i = 13;',
  4867. ' $Self.i = 14;',
  4868. ' };',
  4869. ' };',
  4870. ' };',
  4871. ' };',
  4872. '});',
  4873. '']),
  4874. LinesToStr([
  4875. '']));
  4876. end;
  4877. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4878. begin
  4879. StartProgram(false);
  4880. Add([
  4881. 'type',
  4882. ' TProc = reference to procedure;',
  4883. 'function DoIt: TProc;',
  4884. ' function Sub: TProc;',
  4885. ' begin',
  4886. ' Result:=procedure',
  4887. ' begin',
  4888. ' Sub:=procedure',
  4889. ' procedure SubSub;',
  4890. ' begin',
  4891. ' Result:=nil;',
  4892. ' Sub:=nil;',
  4893. ' DoIt:=nil;',
  4894. ' end;',
  4895. ' begin',
  4896. ' Result:=nil;',
  4897. ' Sub:=nil;',
  4898. ' DoIt:=nil;',
  4899. ' end;',
  4900. ' end;',
  4901. ' end;',
  4902. 'begin',
  4903. 'end;',
  4904. 'begin']);
  4905. ConvertProgram;
  4906. CheckSource('TestAnonymousProc_NestedAssignResult',
  4907. LinesToStr([ // statements
  4908. 'this.DoIt = function () {',
  4909. ' var Result = null;',
  4910. ' function Sub() {',
  4911. ' var Result$1 = null;',
  4912. ' Result$1 = function () {',
  4913. ' Result$1 = function () {',
  4914. ' function SubSub() {',
  4915. ' Result$1 = null;',
  4916. ' Result$1 = null;',
  4917. ' Result = null;',
  4918. ' };',
  4919. ' Result$1 = null;',
  4920. ' Result$1 = null;',
  4921. ' Result = null;',
  4922. ' };',
  4923. ' };',
  4924. ' return Result$1;',
  4925. ' };',
  4926. ' return Result;',
  4927. '};',
  4928. '']),
  4929. LinesToStr([
  4930. '']));
  4931. end;
  4932. procedure TTestModule.TestAnonymousProc_Class;
  4933. begin
  4934. StartProgram(false);
  4935. Add([
  4936. 'type',
  4937. ' TProc = reference to procedure;',
  4938. ' TEvent = procedure of object;',
  4939. ' TObject = class',
  4940. ' Size: word;',
  4941. ' function GetIt: TProc;',
  4942. ' procedure DoIt; virtual; abstract;',
  4943. ' end;',
  4944. 'function TObject.GetIt: TProc;',
  4945. 'begin',
  4946. ' Result:=procedure',
  4947. ' var p: TEvent;',
  4948. ' begin',
  4949. ' Size:=Size;',
  4950. ' Size:=Self.Size;',
  4951. ' p:=@DoIt;',
  4952. ' p:[email protected];',
  4953. ' end;',
  4954. 'end;',
  4955. 'begin']);
  4956. ConvertProgram;
  4957. CheckSource('TestAnonymousProc_Class',
  4958. LinesToStr([ // statements
  4959. 'rtl.createClass(this, "TObject", null, function () {',
  4960. ' this.$init = function () {',
  4961. ' this.Size = 0;',
  4962. ' };',
  4963. ' this.$final = function () {',
  4964. ' };',
  4965. ' this.GetIt = function () {',
  4966. ' var $Self = this;',
  4967. ' var Result = null;',
  4968. ' Result = function () {',
  4969. ' var p = null;',
  4970. ' $Self.Size = $Self.Size;',
  4971. ' $Self.Size = $Self.Size;',
  4972. ' p = rtl.createCallback($Self, "DoIt");',
  4973. ' p = rtl.createCallback($Self, "DoIt");',
  4974. ' };',
  4975. ' return Result;',
  4976. ' };',
  4977. '});',
  4978. '']),
  4979. LinesToStr([
  4980. '']));
  4981. end;
  4982. procedure TTestModule.TestAnonymousProc_ForLoop;
  4983. begin
  4984. StartProgram(false);
  4985. Add([
  4986. 'type TProc = reference to procedure;',
  4987. 'procedure Foo(p: TProc);',
  4988. 'begin',
  4989. 'end;',
  4990. 'procedure DoIt;',
  4991. 'var i: word;',
  4992. ' a: word;',
  4993. 'begin',
  4994. ' for i:=1 to 10 do begin',
  4995. ' Foo(procedure begin a:=3; end);',
  4996. ' end;',
  4997. 'end;',
  4998. 'begin',
  4999. ' DoIt;']);
  5000. ConvertProgram;
  5001. CheckSource('TestAnonymousProc_ForLoop',
  5002. LinesToStr([ // statements
  5003. 'this.Foo = function (p) {',
  5004. '};',
  5005. 'this.DoIt = function () {',
  5006. ' var i = 0;',
  5007. ' var a = 0;',
  5008. ' for (i = 1; i <= 10; i++) {',
  5009. ' $mod.Foo(function () {',
  5010. ' a = 3;',
  5011. ' });',
  5012. ' };',
  5013. '};',
  5014. '']),
  5015. LinesToStr([
  5016. '$mod.DoIt();'
  5017. ]));
  5018. end;
  5019. procedure TTestModule.TestEnum_Name;
  5020. begin
  5021. StartProgram(false);
  5022. Add('type TMyEnum = (Red, Green, Blue);');
  5023. Add('var e: TMyEnum;');
  5024. Add('var f: TMyEnum = Blue;');
  5025. Add('begin');
  5026. Add(' e:=green;');
  5027. Add(' e:=default(TMyEnum);');
  5028. ConvertProgram;
  5029. CheckSource('TestEnum_Name',
  5030. LinesToStr([ // statements
  5031. 'this.TMyEnum = {',
  5032. ' "0":"Red",',
  5033. ' Red:0,',
  5034. ' "1":"Green",',
  5035. ' Green:1,',
  5036. ' "2":"Blue",',
  5037. ' Blue:2',
  5038. ' };',
  5039. 'this.e = 0;',
  5040. 'this.f = this.TMyEnum.Blue;'
  5041. ]),
  5042. LinesToStr([
  5043. '$mod.e=$mod.TMyEnum.Green;',
  5044. '$mod.e=$mod.TMyEnum.Red;'
  5045. ]));
  5046. end;
  5047. procedure TTestModule.TestEnum_Number;
  5048. begin
  5049. Converter.Options:=Converter.Options+[coEnumNumbers];
  5050. StartProgram(false);
  5051. Add('type TMyEnum = (Red, Green);');
  5052. Add('var');
  5053. Add(' e: TMyEnum;');
  5054. Add(' f: TMyEnum = Green;');
  5055. Add(' i: longint;');
  5056. Add('begin');
  5057. Add(' e:=green;');
  5058. Add(' i:=longint(e);');
  5059. ConvertProgram;
  5060. CheckSource('TestEnumNumber',
  5061. LinesToStr([ // statements
  5062. 'this.TMyEnum = {',
  5063. ' "0":"Red",',
  5064. ' Red:0,',
  5065. ' "1":"Green",',
  5066. ' Green:1',
  5067. ' };',
  5068. 'this.e = 0;',
  5069. 'this.f = 1;',
  5070. 'this.i = 0;'
  5071. ]),
  5072. LinesToStr([
  5073. '$mod.e=1;',
  5074. '$mod.i=$mod.e;'
  5075. ]));
  5076. end;
  5077. procedure TTestModule.TestEnum_ConstFail;
  5078. begin
  5079. StartProgram(false);
  5080. Add([
  5081. 'type TMyEnum = (Red = 100, Green = 101);',
  5082. 'var',
  5083. ' e: TMyEnum;',
  5084. ' f: TMyEnum = Green;',
  5085. 'begin',
  5086. ' e:=green;']);
  5087. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5088. ConvertProgram;
  5089. end;
  5090. procedure TTestModule.TestEnum_Functions;
  5091. begin
  5092. StartProgram(false);
  5093. Add([
  5094. 'type TMyEnum = (Red, Green);',
  5095. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5096. 'var',
  5097. ' v: longint;',
  5098. ' s: string;',
  5099. 'begin',
  5100. ' val(s,e,v);',
  5101. ' val(s,e,i);',
  5102. 'end;',
  5103. 'var',
  5104. ' e: TMyEnum;',
  5105. ' i: longint;',
  5106. ' s: string;',
  5107. ' b: boolean;',
  5108. 'begin',
  5109. ' i:=ord(red);',
  5110. ' i:=ord(green);',
  5111. ' i:=ord(e);',
  5112. ' i:=ord(b);',
  5113. ' e:=low(tmyenum);',
  5114. ' e:=low(e);',
  5115. ' b:=low(boolean);',
  5116. ' e:=high(tmyenum);',
  5117. ' e:=high(e);',
  5118. ' b:=high(boolean);',
  5119. ' e:=pred(green);',
  5120. ' e:=pred(e);',
  5121. ' b:=pred(b);',
  5122. ' e:=succ(red);',
  5123. ' e:=succ(e);',
  5124. ' b:=succ(b);',
  5125. ' e:=tmyenum(1);',
  5126. ' e:=tmyenum(i);',
  5127. ' s:=str(e);',
  5128. ' str(e,s);',
  5129. ' str(red,s);',
  5130. ' s:=str(e:3);',
  5131. ' writestr(s,e:3,red);',
  5132. ' val(s,e,i);',
  5133. ' i:=longint(e);']);
  5134. ConvertProgram;
  5135. CheckSource('TestEnum_Functions',
  5136. LinesToStr([ // statements
  5137. 'this.TMyEnum = {',
  5138. ' "0":"Red",',
  5139. ' Red:0,',
  5140. ' "1":"Green",',
  5141. ' Green:1',
  5142. ' };',
  5143. 'this.DoIt = function (e, i) {',
  5144. ' var v = 0;',
  5145. ' var s = "";',
  5146. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5147. ' v = w;',
  5148. ' }));',
  5149. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5150. '};',
  5151. 'this.e = 0;',
  5152. 'this.i = 0;',
  5153. 'this.s = "";',
  5154. 'this.b = false;',
  5155. '']),
  5156. LinesToStr([
  5157. '$mod.i=$mod.TMyEnum.Red;',
  5158. '$mod.i=$mod.TMyEnum.Green;',
  5159. '$mod.i=$mod.e;',
  5160. '$mod.i=$mod.b+0;',
  5161. '$mod.e=$mod.TMyEnum.Red;',
  5162. '$mod.e=$mod.TMyEnum.Red;',
  5163. '$mod.b=false;',
  5164. '$mod.e=$mod.TMyEnum.Green;',
  5165. '$mod.e=$mod.TMyEnum.Green;',
  5166. '$mod.b=true;',
  5167. '$mod.e=$mod.TMyEnum.Green-1;',
  5168. '$mod.e=$mod.e-1;',
  5169. '$mod.b=false;',
  5170. '$mod.e=$mod.TMyEnum.Red+1;',
  5171. '$mod.e=$mod.e+1;',
  5172. '$mod.b=true;',
  5173. '$mod.e=1;',
  5174. '$mod.e=$mod.i;',
  5175. '$mod.s = $mod.TMyEnum[$mod.e];',
  5176. '$mod.s = $mod.TMyEnum[$mod.e];',
  5177. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5178. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5179. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5180. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5181. ' $mod.i = v;',
  5182. '});',
  5183. '$mod.i=$mod.e;',
  5184. '']));
  5185. end;
  5186. procedure TTestModule.TestEnumRg_Functions;
  5187. begin
  5188. StartProgram(false);
  5189. Add([
  5190. 'type',
  5191. ' TEnum = (Red, Green, Blue);',
  5192. ' TEnumRg = Green..Blue;',
  5193. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5194. 'var',
  5195. ' v: longint;',
  5196. ' s: string;',
  5197. 'begin',
  5198. ' val(s,e,v);',
  5199. ' val(s,e,i);',
  5200. 'end;',
  5201. 'var',
  5202. ' e: TEnumRg;',
  5203. ' i: longint;',
  5204. ' s: string;',
  5205. 'begin',
  5206. ' i:=ord(green);',
  5207. ' i:=ord(e);',
  5208. ' e:=low(tenumrg);',
  5209. ' e:=low(e);',
  5210. ' e:=high(tenumrg);',
  5211. ' e:=high(e);',
  5212. ' e:=pred(blue);',
  5213. ' e:=pred(e);',
  5214. ' e:=succ(green);',
  5215. ' e:=succ(e);',
  5216. ' e:=tenumrg(1);',
  5217. ' e:=tenumrg(i);',
  5218. ' s:=str(e);',
  5219. ' str(e,s);',
  5220. ' str(red,s);',
  5221. ' s:=str(e:3);',
  5222. ' writestr(s,e:3,blue);',
  5223. ' val(s,e,i);',
  5224. ' i:=longint(e);']);
  5225. ConvertProgram;
  5226. CheckSource('TestEnumRg_Functions',
  5227. LinesToStr([ // statements
  5228. 'this.TEnum = {',
  5229. ' "0":"Red",',
  5230. ' Red:0,',
  5231. ' "1":"Green",',
  5232. ' Green:1,',
  5233. ' "2":"Blue",',
  5234. ' Blue:2',
  5235. ' };',
  5236. 'this.DoIt = function (e, i) {',
  5237. ' var v = 0;',
  5238. ' var s = "";',
  5239. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5240. ' v = w;',
  5241. ' }));',
  5242. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5243. '};',
  5244. 'this.e = this.TEnum.Green;',
  5245. 'this.i = 0;',
  5246. 'this.s = "";',
  5247. '']),
  5248. LinesToStr([
  5249. '$mod.i=$mod.TEnum.Green;',
  5250. '$mod.i=$mod.e;',
  5251. '$mod.e=$mod.TEnum.Green;',
  5252. '$mod.e=$mod.TEnum.Green;',
  5253. '$mod.e=$mod.TEnum.Blue;',
  5254. '$mod.e=$mod.TEnum.Blue;',
  5255. '$mod.e=$mod.TEnum.Blue-1;',
  5256. '$mod.e=$mod.e-1;',
  5257. '$mod.e=$mod.TEnum.Green+1;',
  5258. '$mod.e=$mod.e+1;',
  5259. '$mod.e=1;',
  5260. '$mod.e=$mod.i;',
  5261. '$mod.s = $mod.TEnum[$mod.e];',
  5262. '$mod.s = $mod.TEnum[$mod.e];',
  5263. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5264. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5265. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5266. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5267. ' $mod.i = v;',
  5268. '});',
  5269. '$mod.i=$mod.e;',
  5270. '']));
  5271. end;
  5272. procedure TTestModule.TestEnum_AsParams;
  5273. begin
  5274. StartProgram(false);
  5275. Add('type TEnum = (Red,Blue);');
  5276. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5277. Add('var vJ: TEnum;');
  5278. Add('begin');
  5279. Add(' vg:=vg;');
  5280. Add(' vj:=vh;');
  5281. Add(' vi:=vi;');
  5282. Add(' doit(vg,vg,vg);');
  5283. Add(' doit(vh,vh,vj);');
  5284. Add(' doit(vi,vi,vi);');
  5285. Add(' doit(vj,vj,vj);');
  5286. Add('end;');
  5287. Add('var i: TEnum;');
  5288. Add('begin');
  5289. Add(' doit(i,i,i);');
  5290. ConvertProgram;
  5291. CheckSource('TestEnum_AsParams',
  5292. LinesToStr([ // statements
  5293. 'this.TEnum = {',
  5294. ' "0": "Red",',
  5295. ' Red: 0,',
  5296. ' "1": "Blue",',
  5297. ' Blue: 1',
  5298. '};',
  5299. 'this.DoIt = function (vG,vH,vI) {',
  5300. ' var vJ = 0;',
  5301. ' vG = vG;',
  5302. ' vJ = vH;',
  5303. ' vI.set(vI.get());',
  5304. ' $mod.DoIt(vG, vG, {',
  5305. ' get: function () {',
  5306. ' return vG;',
  5307. ' },',
  5308. ' set: function (v) {',
  5309. ' vG = v;',
  5310. ' }',
  5311. ' });',
  5312. ' $mod.DoIt(vH, vH, {',
  5313. ' get: function () {',
  5314. ' return vJ;',
  5315. ' },',
  5316. ' set: function (v) {',
  5317. ' vJ = v;',
  5318. ' }',
  5319. ' });',
  5320. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5321. ' $mod.DoIt(vJ, vJ, {',
  5322. ' get: function () {',
  5323. ' return vJ;',
  5324. ' },',
  5325. ' set: function (v) {',
  5326. ' vJ = v;',
  5327. ' }',
  5328. ' });',
  5329. '};',
  5330. 'this.i = 0;'
  5331. ]),
  5332. LinesToStr([
  5333. '$mod.DoIt($mod.i,$mod.i,{',
  5334. ' p: $mod,',
  5335. ' get: function () {',
  5336. ' return this.p.i;',
  5337. ' },',
  5338. ' set: function (v) {',
  5339. ' this.p.i = v;',
  5340. ' }',
  5341. '});'
  5342. ]));
  5343. end;
  5344. procedure TTestModule.TestEnumRange_Array;
  5345. begin
  5346. StartProgram(false);
  5347. Add([
  5348. 'type',
  5349. ' TEnum = (Red, Green, Blue);',
  5350. ' TEnumRg = green..blue;',
  5351. ' TArr = array[TEnumRg] of byte;',
  5352. ' TArr2 = array[green..blue] of byte;',
  5353. 'var',
  5354. ' a: TArr;',
  5355. ' b: TArr = (3,4);',
  5356. ' c: TArr2 = (5,6);',
  5357. 'begin',
  5358. ' a[green] := b[blue];',
  5359. ' c[green] := c[blue];',
  5360. '']);
  5361. ConvertProgram;
  5362. CheckSource('TestEnumRange_Array',
  5363. LinesToStr([ // statements
  5364. 'this.TEnum = {',
  5365. ' "0": "Red",',
  5366. ' Red: 0,',
  5367. ' "1": "Green",',
  5368. ' Green: 1,',
  5369. ' "2": "Blue",',
  5370. ' Blue: 2',
  5371. '};',
  5372. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5373. 'this.b = [3, 4];',
  5374. 'this.c = [5, 6];',
  5375. '']),
  5376. LinesToStr([
  5377. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5378. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5379. '']));
  5380. end;
  5381. procedure TTestModule.TestEnum_ForIn;
  5382. begin
  5383. StartProgram(false);
  5384. Add([
  5385. 'type',
  5386. ' TEnum = (Red, Green, Blue);',
  5387. ' TEnumRg = green..blue;',
  5388. ' TArr = array[TEnum] of byte;',
  5389. ' TArrRg = array[TEnumRg] of byte;',
  5390. 'var',
  5391. ' e: TEnum;',
  5392. ' a1: TArr = (3,4,5);',
  5393. ' a2: TArrRg = (11,12);',
  5394. ' b: byte;',
  5395. 'begin',
  5396. ' for e in TEnum do ;',
  5397. ' for e in TEnumRg do ;',
  5398. ' for e in TArr do ;',
  5399. ' for e in TArrRg do ;',
  5400. ' for b in a1 do ;',
  5401. ' for b in a2 do ;',
  5402. '']);
  5403. ConvertProgram;
  5404. CheckSource('TestEnum_ForIn',
  5405. LinesToStr([ // statements
  5406. 'this.TEnum = {',
  5407. ' "0": "Red",',
  5408. ' Red: 0,',
  5409. ' "1": "Green",',
  5410. ' Green: 1,',
  5411. ' "2": "Blue",',
  5412. ' Blue: 2',
  5413. '};',
  5414. 'this.e = 0;',
  5415. 'this.a1 = [3, 4, 5];',
  5416. 'this.a2 = [11, 12];',
  5417. 'this.b = 0;',
  5418. '']),
  5419. LinesToStr([
  5420. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5421. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5422. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5423. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5424. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5425. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5426. '']));
  5427. end;
  5428. procedure TTestModule.TestEnum_ScopedNumber;
  5429. begin
  5430. Converter.Options:=Converter.Options+[coEnumNumbers];
  5431. StartProgram(false);
  5432. Add([
  5433. 'type',
  5434. ' TEnum = (Red, Green);',
  5435. 'var',
  5436. ' e: TEnum;',
  5437. 'begin',
  5438. ' e:=TEnum.Green;',
  5439. '']);
  5440. ConvertProgram;
  5441. CheckSource('TestEnum_ScopedNumber',
  5442. LinesToStr([ // statements
  5443. 'this.TEnum = {',
  5444. ' "0": "Red",',
  5445. ' Red: 0,',
  5446. ' "1": "Green",',
  5447. ' Green: 1',
  5448. '};',
  5449. 'this.e = 0;',
  5450. '']),
  5451. LinesToStr([
  5452. '$mod.e = 1;']));
  5453. end;
  5454. procedure TTestModule.TestEnum_InFunction;
  5455. begin
  5456. StartProgram(false);
  5457. Add([
  5458. 'const TEnum = 3;',
  5459. 'procedure DoIt;',
  5460. 'type',
  5461. ' TEnum = (Red, Green, Blue);',
  5462. ' procedure Sub;',
  5463. ' type',
  5464. ' TEnumSub = (Left, Right);',
  5465. ' var',
  5466. ' es: TEnumSub;',
  5467. ' begin',
  5468. ' es:=Left;',
  5469. ' end;',
  5470. 'var',
  5471. ' e, e2: TEnum;',
  5472. 'begin',
  5473. ' if e in [red,blue] then e2:=e;',
  5474. 'end;',
  5475. 'begin']);
  5476. ConvertProgram;
  5477. CheckSource('TestEnum_InFunction',
  5478. LinesToStr([ // statements
  5479. 'this.TEnum = 3;',
  5480. 'var TEnum$1 = {',
  5481. ' "0":"Red",',
  5482. ' Red:0,',
  5483. ' "1":"Green",',
  5484. ' Green:1,',
  5485. ' "2":"Blue",',
  5486. ' Blue:2',
  5487. ' };',
  5488. 'var TEnumSub = {',
  5489. ' "0": "Left",',
  5490. ' Left: 0,',
  5491. ' "1": "Right",',
  5492. ' Right: 1',
  5493. '};',
  5494. 'this.DoIt = function () {',
  5495. ' function Sub() {',
  5496. ' var es = 0;',
  5497. ' es = TEnumSub.Left;',
  5498. ' };',
  5499. ' var e = 0;',
  5500. ' var e2 = 0;',
  5501. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5502. '};',
  5503. '']),
  5504. LinesToStr([
  5505. '']));
  5506. end;
  5507. procedure TTestModule.TestSet_Enum;
  5508. begin
  5509. StartProgram(false);
  5510. Add([
  5511. 'type',
  5512. ' TColor = (Red, Green, Blue);',
  5513. ' TColors = set of TColor;',
  5514. 'var',
  5515. ' c: TColor;',
  5516. ' s: TColors;',
  5517. ' t: TColors = [];',
  5518. ' u: TColors = [Red];',
  5519. 'begin',
  5520. ' s:=[];',
  5521. ' s:=[Green];',
  5522. ' s:=[Green,Blue];',
  5523. ' s:=[Red..Blue];',
  5524. ' s:=[Red,Green..Blue];',
  5525. ' s:=[Red,c];',
  5526. ' s:=t;',
  5527. ' s:=default(TColors);',
  5528. '']);
  5529. ConvertProgram;
  5530. CheckSource('TestSet',
  5531. LinesToStr([ // statements
  5532. 'this.TColor = {',
  5533. ' "0":"Red",',
  5534. ' Red:0,',
  5535. ' "1":"Green",',
  5536. ' Green:1,',
  5537. ' "2":"Blue",',
  5538. ' Blue:2',
  5539. ' };',
  5540. 'this.c = 0;',
  5541. 'this.s = {};',
  5542. 'this.t = {};',
  5543. 'this.u = rtl.createSet(this.TColor.Red);'
  5544. ]),
  5545. LinesToStr([
  5546. '$mod.s={};',
  5547. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5548. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5549. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5550. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5551. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5552. '$mod.s=rtl.refSet($mod.t);',
  5553. '$mod.s={};',
  5554. '']));
  5555. end;
  5556. procedure TTestModule.TestSet_Operators;
  5557. begin
  5558. StartProgram(false);
  5559. Add('type');
  5560. Add(' TColor = (Red, Green, Blue);');
  5561. Add(' TColors = set of tcolor;');
  5562. Add('var');
  5563. Add(' vC: TColor;');
  5564. Add(' vS: TColors;');
  5565. Add(' vT: TColors;');
  5566. Add(' vU: TColors;');
  5567. Add(' B: boolean;');
  5568. Add('begin');
  5569. Add(' include(vs,green);');
  5570. Add(' exclude(vs,vc);');
  5571. Add(' vs:=vt+vu;');
  5572. Add(' vs:=vt+[red];');
  5573. Add(' vs:=[red]+vt;');
  5574. Add(' vs:=[red]+[green];');
  5575. Add(' vs:=vt-vu;');
  5576. Add(' vs:=vt-[red];');
  5577. Add(' vs:=[red]-vt;');
  5578. Add(' vs:=[red]-[green];');
  5579. Add(' vs:=vt*vu;');
  5580. Add(' vs:=vt*[red];');
  5581. Add(' vs:=[red]*vt;');
  5582. Add(' vs:=[red]*[green];');
  5583. Add(' vs:=vt><vu;');
  5584. Add(' vs:=vt><[red];');
  5585. Add(' vs:=[red]><vt;');
  5586. Add(' vs:=[red]><[green];');
  5587. Add(' b:=vt=vu;');
  5588. Add(' b:=vt=[red];');
  5589. Add(' b:=[red]=vt;');
  5590. Add(' b:=[red]=[green];');
  5591. Add(' b:=vt<>vu;');
  5592. Add(' b:=vt<>[red];');
  5593. Add(' b:=[red]<>vt;');
  5594. Add(' b:=[red]<>[green];');
  5595. Add(' b:=vt<=vu;');
  5596. Add(' b:=vt<=[red];');
  5597. Add(' b:=[red]<=vt;');
  5598. Add(' b:=[red]<=[green];');
  5599. Add(' b:=vt>=vu;');
  5600. Add(' b:=vt>=[red];');
  5601. Add(' b:=[red]>=vt;');
  5602. Add(' b:=[red]>=[green];');
  5603. ConvertProgram;
  5604. CheckSource('TestSet_Operators',
  5605. LinesToStr([ // statements
  5606. 'this.TColor = {',
  5607. ' "0":"Red",',
  5608. ' Red:0,',
  5609. ' "1":"Green",',
  5610. ' Green:1,',
  5611. ' "2":"Blue",',
  5612. ' Blue:2',
  5613. ' };',
  5614. 'this.vC = 0;',
  5615. 'this.vS = {};',
  5616. 'this.vT = {};',
  5617. 'this.vU = {};',
  5618. 'this.B = false;'
  5619. ]),
  5620. LinesToStr([
  5621. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5622. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5623. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5624. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5625. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5626. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5627. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5628. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5629. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5630. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5631. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5632. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5633. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5634. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5635. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5636. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5637. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5638. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5639. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5640. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5641. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5642. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5643. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5644. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5645. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5646. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5647. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5648. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5649. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5650. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5651. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5652. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5653. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5654. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5655. '']));
  5656. end;
  5657. procedure TTestModule.TestSet_Operator_In;
  5658. begin
  5659. StartProgram(false);
  5660. Add('type');
  5661. Add(' TColor = (Red, Green, Blue);');
  5662. Add(' TColors = set of tcolor;');
  5663. Add('var');
  5664. Add(' vC: tcolor;');
  5665. Add(' vT: tcolors;');
  5666. Add(' B: boolean;');
  5667. Add('begin');
  5668. Add(' b:=red in vt;');
  5669. Add(' b:=vc in vt;');
  5670. Add(' b:=green in [red..blue];');
  5671. Add(' b:=vc in [red..blue];');
  5672. Add(' ');
  5673. Add(' if red in vt then ;');
  5674. Add(' while vC in vt do ;');
  5675. Add(' repeat');
  5676. Add(' until vC in vt;');
  5677. ConvertProgram;
  5678. CheckSource('TestSet_Operator_In',
  5679. LinesToStr([ // statements
  5680. 'this.TColor = {',
  5681. ' "0":"Red",',
  5682. ' Red:0,',
  5683. ' "1":"Green",',
  5684. ' Green:1,',
  5685. ' "2":"Blue",',
  5686. ' Blue:2',
  5687. ' };',
  5688. 'this.vC = 0;',
  5689. 'this.vT = {};',
  5690. 'this.B = false;'
  5691. ]),
  5692. LinesToStr([
  5693. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5694. '$mod.B = $mod.vC in $mod.vT;',
  5695. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5696. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5697. 'if ($mod.TColor.Red in $mod.vT) ;',
  5698. 'while ($mod.vC in $mod.vT) {',
  5699. '};',
  5700. 'do {',
  5701. '} while (!($mod.vC in $mod.vT));',
  5702. '']));
  5703. end;
  5704. procedure TTestModule.TestSet_Functions;
  5705. begin
  5706. StartProgram(false);
  5707. Add('type');
  5708. Add(' TMyEnum = (Red, Green);');
  5709. Add(' TMyEnums = set of TMyEnum;');
  5710. Add('var');
  5711. Add(' e: TMyEnum;');
  5712. Add(' s: TMyEnums;');
  5713. Add('begin');
  5714. Add(' e:=Low(TMyEnums);');
  5715. Add(' e:=Low(s);');
  5716. Add(' e:=High(TMyEnums);');
  5717. Add(' e:=High(s);');
  5718. ConvertProgram;
  5719. CheckSource('TestSetFunctions',
  5720. LinesToStr([ // statements
  5721. 'this.TMyEnum = {',
  5722. ' "0":"Red",',
  5723. ' Red:0,',
  5724. ' "1":"Green",',
  5725. ' Green:1',
  5726. ' };',
  5727. 'this.e = 0;',
  5728. 'this.s = {};'
  5729. ]),
  5730. LinesToStr([
  5731. '$mod.e=$mod.TMyEnum.Red;',
  5732. '$mod.e=$mod.TMyEnum.Red;',
  5733. '$mod.e=$mod.TMyEnum.Green;',
  5734. '$mod.e=$mod.TMyEnum.Green;',
  5735. '']));
  5736. end;
  5737. procedure TTestModule.TestSet_PassAsArgClone;
  5738. begin
  5739. StartProgram(false);
  5740. Add('type');
  5741. Add(' TMyEnum = (Red, Green);');
  5742. Add(' TMyEnums = set of TMyEnum;');
  5743. Add('procedure DoDefault(s: tmyenums); begin end;');
  5744. Add('procedure DoConst(const s: tmyenums); begin end;');
  5745. Add('var');
  5746. Add(' aSet: tmyenums;');
  5747. Add('begin');
  5748. Add(' dodefault(aset);');
  5749. Add(' doconst(aset);');
  5750. ConvertProgram;
  5751. CheckSource('TestSetFunctions',
  5752. LinesToStr([ // statements
  5753. 'this.TMyEnum = {',
  5754. ' "0":"Red",',
  5755. ' Red:0,',
  5756. ' "1":"Green",',
  5757. ' Green:1',
  5758. ' };',
  5759. 'this.DoDefault = function (s) {',
  5760. '};',
  5761. 'this.DoConst = function (s) {',
  5762. '};',
  5763. 'this.aSet = {};'
  5764. ]),
  5765. LinesToStr([
  5766. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5767. '$mod.DoConst($mod.aSet);',
  5768. '']));
  5769. end;
  5770. procedure TTestModule.TestSet_AsParams;
  5771. begin
  5772. StartProgram(false);
  5773. Add([
  5774. 'type TEnum = (Red,Blue);',
  5775. 'type TEnums = set of TEnum;',
  5776. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5777. 'var vJ: TEnums;',
  5778. 'begin',
  5779. ' Include(vg,red);',
  5780. ' Include(result,blue);',
  5781. ' vg:=vg;',
  5782. ' vj:=vh;',
  5783. ' vi:=vi;',
  5784. ' doit(vg,vg,vg);',
  5785. ' doit(vh,vh,vj);',
  5786. ' doit(vi,vi,vi);',
  5787. ' doit(vj,vj,vj);',
  5788. 'end;',
  5789. 'var i: TEnums;',
  5790. 'begin',
  5791. ' doit(i,i,i);']);
  5792. ConvertProgram;
  5793. CheckSource('TestSet_AsParams',
  5794. LinesToStr([ // statements
  5795. 'this.TEnum = {',
  5796. ' "0": "Red",',
  5797. ' Red: 0,',
  5798. ' "1": "Blue",',
  5799. ' Blue: 1',
  5800. '};',
  5801. 'this.DoIt = function (vG,vH,vI) {',
  5802. ' var Result = {};',
  5803. ' var vJ = {};',
  5804. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5805. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5806. ' vG = rtl.refSet(vG);',
  5807. ' vJ = rtl.refSet(vH);',
  5808. ' vI.set(rtl.refSet(vI.get()));',
  5809. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5810. ' get: function () {',
  5811. ' return vG;',
  5812. ' },',
  5813. ' set: function (v) {',
  5814. ' vG = v;',
  5815. ' }',
  5816. ' });',
  5817. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5818. ' get: function () {',
  5819. ' return vJ;',
  5820. ' },',
  5821. ' set: function (v) {',
  5822. ' vJ = v;',
  5823. ' }',
  5824. ' });',
  5825. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5826. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5827. ' get: function () {',
  5828. ' return vJ;',
  5829. ' },',
  5830. ' set: function (v) {',
  5831. ' vJ = v;',
  5832. ' }',
  5833. ' });',
  5834. ' return Result;',
  5835. '};',
  5836. 'this.i = {};'
  5837. ]),
  5838. LinesToStr([
  5839. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5840. ' p: $mod,',
  5841. ' get: function () {',
  5842. ' return this.p.i;',
  5843. ' },',
  5844. ' set: function (v) {',
  5845. ' this.p.i = v;',
  5846. ' }',
  5847. '});'
  5848. ]));
  5849. end;
  5850. procedure TTestModule.TestSet_Property;
  5851. begin
  5852. StartProgram(false);
  5853. Add('type');
  5854. Add(' TEnum = (Red,Blue);');
  5855. Add(' TEnums = set of TEnum;');
  5856. Add(' TObject = class');
  5857. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5858. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5859. Add(' property Colors: TEnums read GetColors write SetColors;');
  5860. Add(' end;');
  5861. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5862. Add('begin end;');
  5863. Add('var Obj: TObject;');
  5864. Add('begin');
  5865. Add(' Include(Obj.Colors,Red);');
  5866. Add(' Exclude(Obj.Colors,Red);');
  5867. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5868. ConvertProgram;
  5869. CheckSource('TestSet_Property',
  5870. LinesToStr([ // statements
  5871. 'this.TEnum = {',
  5872. ' "0": "Red",',
  5873. ' Red: 0,',
  5874. ' "1": "Blue",',
  5875. ' Blue: 1',
  5876. '};',
  5877. 'rtl.createClass(this, "TObject", null, function () {',
  5878. ' this.$init = function () {',
  5879. ' };',
  5880. ' this.$final = function () {',
  5881. ' };',
  5882. '});',
  5883. 'this.DoIt = function (i, j, k, l) {',
  5884. '};',
  5885. 'this.Obj = null;',
  5886. '']),
  5887. LinesToStr([
  5888. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5889. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5890. '']));
  5891. end;
  5892. procedure TTestModule.TestSet_EnumConst;
  5893. begin
  5894. StartProgram(false);
  5895. Add([
  5896. 'type',
  5897. ' TEnum = (Red,Blue);',
  5898. ' TEnums = set of TEnum;',
  5899. 'const',
  5900. ' Orange = red;',
  5901. 'var',
  5902. ' Enum: tenum;',
  5903. ' Enums: tenums;',
  5904. 'begin',
  5905. ' Include(enums,orange);',
  5906. ' Exclude(enums,orange);',
  5907. ' if orange in enums then;',
  5908. ' if orange in [orange,red] then;']);
  5909. ConvertProgram;
  5910. CheckSource('TestSet_EnumConst',
  5911. LinesToStr([ // statements
  5912. 'this.TEnum = {',
  5913. ' "0": "Red",',
  5914. ' Red: 0,',
  5915. ' "1": "Blue",',
  5916. ' Blue: 1',
  5917. '};',
  5918. 'this.Orange = this.TEnum.Red;',
  5919. 'this.Enum = 0;',
  5920. 'this.Enums = {};',
  5921. '']),
  5922. LinesToStr([
  5923. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5924. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5925. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5926. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5927. '']));
  5928. end;
  5929. procedure TTestModule.TestSet_IntConst;
  5930. begin
  5931. StartProgram(false);
  5932. Add([
  5933. 'type',
  5934. ' TEnums = set of Byte;',
  5935. 'const',
  5936. ' Orange = 0;',
  5937. 'var',
  5938. ' Enum: byte;',
  5939. ' Enums: tenums;',
  5940. 'begin',
  5941. ' Enums:=[];',
  5942. ' Enums:=[0];',
  5943. ' Enums:=[1..2];',
  5944. //' Include(enums,orange);',
  5945. //' Exclude(enums,orange);',
  5946. ' if orange in enums then;',
  5947. ' if orange in [orange,1] then;']);
  5948. ConvertProgram;
  5949. CheckSource('TestSet_IntConst',
  5950. LinesToStr([ // statements
  5951. 'this.Orange = 0;',
  5952. 'this.Enum = 0;',
  5953. 'this.Enums = {};',
  5954. '']),
  5955. LinesToStr([
  5956. '$mod.Enums = {};',
  5957. '$mod.Enums = rtl.createSet(0);',
  5958. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5959. 'if (0 in $mod.Enums) ;',
  5960. 'if (0 in rtl.createSet(0, 1)) ;',
  5961. '']));
  5962. end;
  5963. procedure TTestModule.TestSet_AnonymousEnumType;
  5964. begin
  5965. StartProgram(false);
  5966. Add('type');
  5967. Add(' TFlags = set of (red, green);');
  5968. Add('const');
  5969. Add(' favorite = red;');
  5970. Add('var');
  5971. Add(' f: TFlags;');
  5972. Add(' i: longint;');
  5973. Add('begin');
  5974. Add(' Include(f,red);');
  5975. Add(' Include(f,favorite);');
  5976. Add(' i:=ord(red);');
  5977. Add(' i:=ord(favorite);');
  5978. Add(' i:=ord(low(TFlags));');
  5979. Add(' i:=ord(low(f));');
  5980. Add(' i:=ord(low(favorite));');
  5981. Add(' i:=ord(high(TFlags));');
  5982. Add(' i:=ord(high(f));');
  5983. Add(' i:=ord(high(favorite));');
  5984. Add(' f:=[green,favorite];');
  5985. ConvertProgram;
  5986. CheckSource('TestSet_AnonymousEnumType',
  5987. LinesToStr([ // statements
  5988. 'this.TFlags$a = {',
  5989. ' "0": "red",',
  5990. ' red: 0,',
  5991. ' "1": "green",',
  5992. ' green: 1',
  5993. '};',
  5994. 'this.favorite = this.TFlags$a.red;',
  5995. 'this.f = {};',
  5996. 'this.i = 0;',
  5997. '']),
  5998. LinesToStr([
  5999. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6000. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6001. '$mod.i = $mod.TFlags$a.red;',
  6002. '$mod.i = $mod.TFlags$a.red;',
  6003. '$mod.i = $mod.TFlags$a.red;',
  6004. '$mod.i = $mod.TFlags$a.red;',
  6005. '$mod.i = $mod.TFlags$a.red;',
  6006. '$mod.i = $mod.TFlags$a.green;',
  6007. '$mod.i = $mod.TFlags$a.green;',
  6008. '$mod.i = $mod.TFlags$a.green;',
  6009. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6010. '']));
  6011. end;
  6012. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6013. begin
  6014. exit;
  6015. StartProgram(false);
  6016. Add([
  6017. 'type',
  6018. ' TAtoZ = ''A''..''Z'';',
  6019. ' TSetOfAZ = set of TAtoZ;',
  6020. 'var',
  6021. ' c: char;',
  6022. ' a: TAtoZ;',
  6023. ' s: TSetOfAZ = [''P'',''A''];',
  6024. ' i: longint;',
  6025. 'begin',
  6026. ' Include(s,''S'');',
  6027. ' Include(s,c);',
  6028. ' Include(s,a);',
  6029. ' c:=low(TAtoZ);',
  6030. ' i:=ord(low(TAtoZ));',
  6031. ' a:=high(TAtoZ);',
  6032. ' a:=high(TSetOfAtoZ);',
  6033. ' s:=[a,c,''M''];',
  6034. '']);
  6035. ConvertProgram;
  6036. CheckSource('TestSet_AnonymousEnumTypeChar',
  6037. LinesToStr([ // statements
  6038. '']),
  6039. LinesToStr([
  6040. '']));
  6041. end;
  6042. procedure TTestModule.TestSet_ConstEnum;
  6043. begin
  6044. StartProgram(false);
  6045. Add([
  6046. 'type',
  6047. ' TEnum = (red,blue,green);',
  6048. ' TEnums = set of TEnum;',
  6049. 'const',
  6050. ' teAny = [low(TEnum)..high(TEnum)];',
  6051. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6052. 'var',
  6053. ' e: TEnum;',
  6054. ' s: TEnums;',
  6055. 'begin',
  6056. ' if blue in teAny then;',
  6057. ' if blue in teAny+[e] then;',
  6058. ' if blue in teAny+teRedBlue then;',
  6059. ' if e in [red,blue] then;',
  6060. ' s:=teAny;',
  6061. ' s:=teAny+[e];',
  6062. ' s:=[e]+teAny;',
  6063. ' s:=teAny+teRedBlue;',
  6064. ' s:=teAny+teRedBlue+[e];',
  6065. '']);
  6066. ConvertProgram;
  6067. CheckSource('TestSet_ConstEnum',
  6068. LinesToStr([ // statements
  6069. 'this.TEnum = {',
  6070. ' "0": "red",',
  6071. ' red: 0,',
  6072. ' "1": "blue",',
  6073. ' blue: 1,',
  6074. ' "2": "green",',
  6075. ' green: 2',
  6076. '};',
  6077. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6078. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6079. 'this.e = 0;',
  6080. 'this.s = {};',
  6081. '']),
  6082. LinesToStr([
  6083. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6084. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6085. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6086. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6087. '$mod.s = rtl.refSet($mod.teAny);',
  6088. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6089. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6090. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6091. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6092. '']));
  6093. end;
  6094. procedure TTestModule.TestSet_ConstChar;
  6095. begin
  6096. StartProgram(false);
  6097. Add([
  6098. 'const',
  6099. ' LowChars = [''a''..''z''];',
  6100. ' Chars = LowChars+[''A''..''Z''];',
  6101. ' sc = [''А'', ''Я''];',
  6102. 'var',
  6103. ' c: char;',
  6104. ' s: string;',
  6105. 'begin',
  6106. ' if c in lowchars then ;',
  6107. ' if ''a'' in lowchars then ;',
  6108. ' if s[1] in lowchars then ;',
  6109. ' if c in chars then ;',
  6110. ' if c in [''a''..''z'',''_''] then ;',
  6111. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6112. ' if ''Я'' in sc then ;',
  6113. ' if 3=ord('' '') then ;',
  6114. '']);
  6115. ConvertProgram;
  6116. CheckSource('TestSet_ConstChar',
  6117. LinesToStr([ // statements
  6118. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6119. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6120. 'this.sc = rtl.createSet(1040, 1071);',
  6121. 'this.c = "";',
  6122. 'this.s = "";',
  6123. '']),
  6124. LinesToStr([
  6125. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6126. 'if (97 in $mod.LowChars) ;',
  6127. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6128. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6129. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6130. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6131. 'if (1071 in $mod.sc) ;',
  6132. 'if (3 === 32) ;',
  6133. '']));
  6134. end;
  6135. procedure TTestModule.TestSet_ConstInt;
  6136. begin
  6137. StartProgram(false);
  6138. Add([
  6139. 'const',
  6140. ' Months = [1..12];',
  6141. ' Mirror = [-12..-1]+Months;',
  6142. 'var',
  6143. ' i: smallint;',
  6144. 'begin',
  6145. ' if 3 in Months then;',
  6146. ' if i in Months+[i] then;',
  6147. ' if i in Months+Mirror then;',
  6148. ' if i in [4..6,8] then;',
  6149. '']);
  6150. ConvertProgram;
  6151. CheckSource('TestSet_ConstInt',
  6152. LinesToStr([ // statements
  6153. 'this.Months = rtl.createSet(null, 1, 12);',
  6154. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6155. 'this.i = 0;',
  6156. '']),
  6157. LinesToStr([
  6158. 'if (3 in $mod.Months) ;',
  6159. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6160. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6161. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6162. '']));
  6163. end;
  6164. procedure TTestModule.TestSet_InFunction;
  6165. begin
  6166. StartProgram(false);
  6167. Add([
  6168. 'const',
  6169. ' TEnum = 3;',
  6170. ' TSetOfEnum = 4;',
  6171. ' TSetOfAno = 5;',
  6172. 'procedure DoIt;',
  6173. 'type',
  6174. ' TEnum = (red, blue);',
  6175. ' TSetOfEnum = set of TEnum;',
  6176. ' TSetOfAno = set of (up,down);',
  6177. 'var',
  6178. ' e: TEnum;',
  6179. ' se: TSetOfEnum;',
  6180. ' sa: TSetOfAno;',
  6181. 'begin',
  6182. ' se:=[e];',
  6183. ' sa:=[up];',
  6184. 'end;',
  6185. 'begin',
  6186. '']);
  6187. ConvertProgram;
  6188. CheckSource('TestSet_InFunction',
  6189. LinesToStr([ // statements
  6190. 'this.TEnum = 3;',
  6191. 'this.TSetOfEnum = 4;',
  6192. 'this.TSetOfAno = 5;',
  6193. 'var TEnum$1 = {',
  6194. ' "0": "red",',
  6195. ' red: 0,',
  6196. ' "1": "blue",',
  6197. ' blue: 1',
  6198. '};',
  6199. 'var TSetOfAno$a = {',
  6200. ' "0": "up",',
  6201. ' up: 0,',
  6202. ' "1": "down",',
  6203. ' down: 1',
  6204. '};',
  6205. 'this.DoIt = function () {',
  6206. ' var e = 0;',
  6207. ' var se = {};',
  6208. ' var sa = {};',
  6209. ' se = rtl.createSet(e);',
  6210. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6211. '};',
  6212. '']),
  6213. LinesToStr([
  6214. '']));
  6215. end;
  6216. procedure TTestModule.TestSet_ForIn;
  6217. begin
  6218. StartProgram(false);
  6219. Add([
  6220. 'type',
  6221. ' TEnum = (Red, Green, Blue);',
  6222. ' TEnumRg = green..blue;',
  6223. ' TSetOfEnum = set of TEnum;',
  6224. ' TSetOfEnumRg = set of TEnumRg;',
  6225. 'var',
  6226. ' e, e2: TEnum;',
  6227. ' er: TEnum;',
  6228. ' s: TSetOfEnum;',
  6229. 'begin',
  6230. ' for e in TSetOfEnum do ;',
  6231. ' for e in TSetOfEnumRg do ;',
  6232. ' for e in [] do e2:=e;',
  6233. ' for e in [red..green] do e2:=e;',
  6234. ' for e in [green,blue] do e2:=e;',
  6235. ' for e in [red,blue] do e2:=e;',
  6236. ' for e in s do e2:=e;',
  6237. ' for er in TSetOfEnumRg do ;',
  6238. '']);
  6239. ConvertProgram;
  6240. CheckSource('TestSet_ForIn',
  6241. LinesToStr([ // statements
  6242. 'this.TEnum = {',
  6243. ' "0":"Red",',
  6244. ' Red:0,',
  6245. ' "1":"Green",',
  6246. ' Green:1,',
  6247. ' "2":"Blue",',
  6248. ' Blue:2',
  6249. ' };',
  6250. 'this.e = 0;',
  6251. 'this.e2 = 0;',
  6252. 'this.er = 0;',
  6253. 'this.s = {};',
  6254. '']),
  6255. LinesToStr([
  6256. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6257. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6258. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6259. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6260. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6261. 'for (var $l in $mod.s){',
  6262. ' $mod.e = +$l;',
  6263. ' $mod.e2 = $mod.e;',
  6264. '};',
  6265. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6266. '']));
  6267. end;
  6268. procedure TTestModule.TestNestBegin;
  6269. begin
  6270. StartProgram(false);
  6271. Add('begin');
  6272. Add(' begin');
  6273. Add(' begin');
  6274. Add(' end;');
  6275. Add(' begin');
  6276. Add(' if true then ;');
  6277. Add(' end;');
  6278. Add(' end;');
  6279. ConvertProgram;
  6280. CheckSource('TestNestBegin',
  6281. '',
  6282. 'if (true) ;');
  6283. end;
  6284. procedure TTestModule.TestUnitImplVars;
  6285. begin
  6286. StartUnit(false);
  6287. Add('interface');
  6288. Add('implementation');
  6289. Add('var');
  6290. Add(' V1:longint;');
  6291. Add(' V2:longint = 3;');
  6292. Add(' V3:string = ''abc'';');
  6293. ConvertUnit;
  6294. CheckSource('TestUnitImplVars',
  6295. LinesToStr([ // statements
  6296. 'var $impl = $mod.$impl;',
  6297. '']),
  6298. '', // this.$init
  6299. LinesToStr([ // implementation
  6300. '$impl.V1 = 0;',
  6301. '$impl.V2 = 3;',
  6302. '$impl.V3 = "abc";',
  6303. '']) );
  6304. end;
  6305. procedure TTestModule.TestUnitImplConsts;
  6306. begin
  6307. StartUnit(false);
  6308. Add('interface');
  6309. Add('implementation');
  6310. Add('const');
  6311. Add(' v1 = 3;');
  6312. Add(' v2:longint = 4;');
  6313. Add(' v3:string = ''abc'';');
  6314. ConvertUnit;
  6315. CheckSource('TestUnitImplConsts',
  6316. LinesToStr([ // statements
  6317. 'var $impl = $mod.$impl;',
  6318. '']),
  6319. '', // this.$init
  6320. LinesToStr([ // implementation
  6321. '$impl.v1 = 3;',
  6322. '$impl.v2 = 4;',
  6323. '$impl.v3 = "abc";',
  6324. '']) );
  6325. end;
  6326. procedure TTestModule.TestUnitImplRecord;
  6327. begin
  6328. StartUnit(false);
  6329. Add('interface');
  6330. Add('implementation');
  6331. Add('type');
  6332. Add(' TMyRecord = record');
  6333. Add(' i: longint;');
  6334. Add(' end;');
  6335. Add('var aRec: TMyRecord;');
  6336. Add('initialization');
  6337. Add(' arec.i:=3;');
  6338. ConvertUnit;
  6339. CheckSource('TestUnitImplRecord',
  6340. LinesToStr([ // statements
  6341. 'var $impl = $mod.$impl;',
  6342. '']),
  6343. // this.$init
  6344. '$impl.aRec.i = 3;',
  6345. LinesToStr([ // implementation
  6346. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6347. ' this.i = 0;',
  6348. ' this.$eq = function (b) {',
  6349. ' return this.i === b.i;',
  6350. ' };',
  6351. ' this.$assign = function (s) {',
  6352. ' this.i = s.i;',
  6353. ' return this;',
  6354. ' };',
  6355. '});',
  6356. '$impl.aRec = $impl.TMyRecord.$new();',
  6357. '']) );
  6358. end;
  6359. procedure TTestModule.TestRenameJSNameConflict;
  6360. begin
  6361. StartProgram(false);
  6362. Add('var apply: longint;');
  6363. Add('var bind: longint;');
  6364. Add('var call: longint;');
  6365. Add('begin');
  6366. ConvertProgram;
  6367. CheckSource('TestRenameJSNameConflict',
  6368. LinesToStr([ // statements
  6369. 'this.Apply = 0;',
  6370. 'this.Bind = 0;',
  6371. 'this.Call = 0;'
  6372. ]),
  6373. LinesToStr([ // this.$main
  6374. ''
  6375. ]));
  6376. end;
  6377. procedure TTestModule.TestLocalConst;
  6378. begin
  6379. StartProgram(false);
  6380. Add('procedure DoIt;');
  6381. Add('const');
  6382. Add(' cA: longint = 1;');
  6383. Add(' cB = 2;');
  6384. Add(' procedure Sub;');
  6385. Add(' const');
  6386. Add(' csA = 3;');
  6387. Add(' cB: double = 4;');
  6388. Add(' begin');
  6389. Add(' cb:=cb+csa;');
  6390. Add(' ca:=ca+csa+5;');
  6391. Add(' end;');
  6392. Add('begin');
  6393. Add(' ca:=ca+cb+6;');
  6394. Add('end;');
  6395. Add('begin');
  6396. ConvertProgram;
  6397. CheckSource('TestLocalConst',
  6398. LinesToStr([
  6399. 'var cA = 1;',
  6400. 'var cB = 2;',
  6401. 'var csA = 3;',
  6402. 'var cB$1 = 4;',
  6403. 'this.DoIt = function () {',
  6404. ' function Sub() {',
  6405. ' cB$1 = cB$1 + 3;',
  6406. ' cA = cA + 3 + 5;',
  6407. ' };',
  6408. ' cA = cA + 2 + 6;',
  6409. '};'
  6410. ]),
  6411. LinesToStr([
  6412. ]));
  6413. end;
  6414. procedure TTestModule.TestVarExternal;
  6415. begin
  6416. StartProgram(false);
  6417. Add('var');
  6418. Add(' NaN: double; external name ''Global.NaN'';');
  6419. Add(' d: double;');
  6420. Add('begin');
  6421. Add(' d:=NaN;');
  6422. ConvertProgram;
  6423. CheckSource('TestVarExternal',
  6424. LinesToStr([
  6425. 'this.d = 0.0;'
  6426. ]),
  6427. LinesToStr([
  6428. '$mod.d = Global.NaN;'
  6429. ]));
  6430. end;
  6431. procedure TTestModule.TestVarExternalOtherUnit;
  6432. begin
  6433. AddModuleWithIntfImplSrc('unit2.pas',
  6434. LinesToStr([
  6435. 'var NaN: double; external name ''Global.NaN'';',
  6436. 'var iV: longint;'
  6437. ]),
  6438. '');
  6439. StartUnit(true);
  6440. Add('interface');
  6441. Add('uses unit2;');
  6442. Add('implementation');
  6443. Add('var');
  6444. Add(' d: double;');
  6445. Add(' i: longint; external name ''$i'';');
  6446. Add('begin');
  6447. Add(' d:=nan;');
  6448. Add(' d:=uNit2.nan;');
  6449. Add(' d:=test1.d;');
  6450. Add(' i:=iv;');
  6451. Add(' i:=uNit2.iv;');
  6452. Add(' i:=test1.i;');
  6453. ConvertUnit;
  6454. CheckSource('TestVarExternalOtherUnit',
  6455. LinesToStr([
  6456. 'var $impl = $mod.$impl;',
  6457. '']),
  6458. LinesToStr([ // this.$init
  6459. '$impl.d = Global.NaN;',
  6460. '$impl.d = Global.NaN;',
  6461. '$impl.d = $impl.d;',
  6462. '$i = pas.unit2.iV;',
  6463. '$i = pas.unit2.iV;',
  6464. '$i = $i;',
  6465. '']),
  6466. LinesToStr([ // implementation
  6467. '$impl.d = 0.0;',
  6468. '']) );
  6469. end;
  6470. procedure TTestModule.TestVarAbsoluteFail;
  6471. begin
  6472. StartProgram(false);
  6473. Add([
  6474. 'var',
  6475. ' a: longint;',
  6476. ' b: longword absolute a;',
  6477. 'begin']);
  6478. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6479. ConvertProgram;
  6480. end;
  6481. procedure TTestModule.TestConstExternal;
  6482. begin
  6483. StartProgram(false);
  6484. Add([
  6485. 'const',
  6486. ' PI: double; external name ''Global.PI'';',
  6487. ' Tau = 2*pi;',
  6488. 'var d: double;',
  6489. 'begin',
  6490. ' d:=pi;',
  6491. ' d:=tau+pi;']);
  6492. ConvertProgram;
  6493. CheckSource('TestConstExternal',
  6494. LinesToStr([
  6495. 'this.Tau = 2*Global.PI;',
  6496. 'this.d = 0.0;'
  6497. ]),
  6498. LinesToStr([
  6499. '$mod.d = Global.PI;',
  6500. '$mod.d = $mod.Tau + Global.PI;'
  6501. ]));
  6502. end;
  6503. procedure TTestModule.TestDouble;
  6504. begin
  6505. StartProgram(false);
  6506. Add([
  6507. 'type',
  6508. ' TDateTime = double;',
  6509. 'const',
  6510. ' a = TDateTime(2.7);',
  6511. ' b = a + TDateTime(1.7);',
  6512. ' c = 0.9 + 0.1;',
  6513. ' f0_1 = 0.1;',
  6514. ' f0_3 = 0.3;',
  6515. ' fn0_1 = -0.1;',
  6516. ' fn0_3 = -0.3;',
  6517. ' fn0_003 = -0.003;',
  6518. ' fn0_123456789 = -0.123456789;',
  6519. ' fn300_0 = -300.0;',
  6520. ' fn123456_0 = -123456.0;',
  6521. ' fn1234567_8 = -1234567.8;',
  6522. ' fn12345678_9 = -12345678.9;',
  6523. ' f1_0En12 = 1E-12;',
  6524. ' fn1_0En12 = -1E-12;',
  6525. ' maxdouble = 1.7e+308;',
  6526. ' mindouble = -1.7e+308;',
  6527. ' MinSafeIntDouble = -$1fffffffffffff;',
  6528. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6529. ' MaxSafeIntDouble = $1fffffffffffff;',
  6530. ' DZeroResolution = 1E-12;',
  6531. ' Minus1 = -1E-12;',
  6532. ' EPS = 1E-9;',
  6533. ' DELTA = 0.001;',
  6534. ' Big = 129.789E+100;',
  6535. ' Test0_15 = 0.15;',
  6536. ' Test999 = 2.9999999999999;',
  6537. ' Test111999 = 211199999999999000.0;',
  6538. ' TestMinus111999 = -211199999999999000.0;',
  6539. 'var',
  6540. ' d: double = b;',
  6541. 'begin',
  6542. ' d:=1.0;',
  6543. ' d:=1.0/3.0;',
  6544. ' d:=1/3;',
  6545. ' d:=5.0E-324;',
  6546. ' d:=1.7E308;',
  6547. ' d:=001.00E00;',
  6548. ' d:=002.00E001;',
  6549. ' d:=003.000E000;',
  6550. ' d:=-004.00E-00;',
  6551. ' d:=-005.00E-001;',
  6552. ' d:=10**3;',
  6553. ' d:=10 mod 3;',
  6554. ' d:=10 div 3;',
  6555. ' d:=c;',
  6556. ' d:=f0_1;',
  6557. ' d:=f0_3;',
  6558. ' d:=fn0_1;',
  6559. ' d:=fn0_3;',
  6560. ' d:=fn0_003;',
  6561. ' d:=fn0_123456789;',
  6562. ' d:=fn300_0;',
  6563. ' d:=fn123456_0;',
  6564. ' d:=fn1234567_8;',
  6565. ' d:=fn12345678_9;',
  6566. ' d:=f1_0En12;',
  6567. ' d:=fn1_0En12;',
  6568. ' d:=maxdouble;',
  6569. ' d:=mindouble;',
  6570. ' d:=MinSafeIntDouble;',
  6571. ' d:=double(MinSafeIntDouble);',
  6572. ' d:=MinSafeIntDouble2;',
  6573. ' d:=double(MinSafeIntDouble2);',
  6574. ' d:=MaxSafeIntDouble;',
  6575. ' d:=default(double);',
  6576. '']);
  6577. ConvertProgram;
  6578. CheckSource('TestDouble',
  6579. LinesToStr([
  6580. 'this.a = 2.7;',
  6581. 'this.b = 2.7 + 1.7;',
  6582. 'this.c = 0.9 + 0.1;',
  6583. 'this.f0_1 = 0.1;',
  6584. 'this.f0_3 = 0.3;',
  6585. 'this.fn0_1 = -0.1;',
  6586. 'this.fn0_3 = -0.3;',
  6587. 'this.fn0_003 = -0.003;',
  6588. 'this.fn0_123456789 = -0.123456789;',
  6589. 'this.fn300_0 = -300.0;',
  6590. 'this.fn123456_0 = -123456.0;',
  6591. 'this.fn1234567_8 = -1234567.8;',
  6592. 'this.fn12345678_9 = -12345678.9;',
  6593. 'this.f1_0En12 = 1E-12;',
  6594. 'this.fn1_0En12 = -1E-12;',
  6595. 'this.maxdouble = 1.7e+308;',
  6596. 'this.mindouble = -1.7e+308;',
  6597. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6598. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6599. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6600. 'this.DZeroResolution = 1E-12;',
  6601. 'this.Minus1 = -1E-12;',
  6602. 'this.EPS = 1E-9;',
  6603. 'this.DELTA = 0.001;',
  6604. 'this.Big = 129.789E+100;',
  6605. 'this.Test0_15 = 0.15;',
  6606. 'this.Test999 = 2.9999999999999;',
  6607. 'this.Test111999 = 211199999999999000.0;',
  6608. 'this.TestMinus111999 = -211199999999999000.0;',
  6609. 'this.d = 4.4;'
  6610. ]),
  6611. LinesToStr([
  6612. '$mod.d = 1.0;',
  6613. '$mod.d = 1.0 / 3.0;',
  6614. '$mod.d = 1 / 3;',
  6615. '$mod.d = 5.0E-324;',
  6616. '$mod.d = 1.7E308;',
  6617. '$mod.d = 1.00E0;',
  6618. '$mod.d = 2.00E1;',
  6619. '$mod.d = 3.000E0;',
  6620. '$mod.d = -4.00E-0;',
  6621. '$mod.d = -5.00E-1;',
  6622. '$mod.d = Math.pow(10, 3);',
  6623. '$mod.d = 10 % 3;',
  6624. '$mod.d = Math.floor(10 / 3);',
  6625. '$mod.d = 1;',
  6626. '$mod.d = 0.1;',
  6627. '$mod.d = 0.3;',
  6628. '$mod.d = -0.1;',
  6629. '$mod.d = -0.3;',
  6630. '$mod.d = -0.003;',
  6631. '$mod.d = -0.123456789;',
  6632. '$mod.d = -300;',
  6633. '$mod.d = -123456;',
  6634. '$mod.d = -1234567.8;',
  6635. '$mod.d = -1.23456789E7;',
  6636. '$mod.d = 1E-12;',
  6637. '$mod.d = -1E-12;',
  6638. '$mod.d = 1.7E308;',
  6639. '$mod.d = -1.7E308;',
  6640. '$mod.d = -9007199254740991;',
  6641. '$mod.d = -9007199254740991;',
  6642. '$mod.d = -9.007199254740992E15;',
  6643. '$mod.d = -9.007199254740992E15;',
  6644. '$mod.d = 9007199254740991;',
  6645. '$mod.d = 0.0;',
  6646. '']));
  6647. end;
  6648. procedure TTestModule.TestInteger;
  6649. begin
  6650. StartProgram(false);
  6651. Add([
  6652. 'const',
  6653. ' MinInt = low(NativeInt);',
  6654. ' MaxInt = high(NativeInt);',
  6655. 'type',
  6656. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6657. 'const',
  6658. ' a = low(TMyInt)+High(TMyInt);',
  6659. 'var',
  6660. ' i: TMyInt;',
  6661. 'begin',
  6662. ' i:=-MinInt;',
  6663. ' i:=default(TMyInt);',
  6664. ' i:=low(i)+high(i);',
  6665. '']);
  6666. ConvertProgram;
  6667. CheckSource('TestIntegerRange',
  6668. LinesToStr([
  6669. 'this.MinInt = -9007199254740991;',
  6670. 'this.MaxInt = 9007199254740991;',
  6671. 'this.a = -9007199254740991 + 9007199254740991;',
  6672. 'this.i = 0;',
  6673. '']),
  6674. LinesToStr([
  6675. '$mod.i = - -9007199254740991;',
  6676. '$mod.i = -9007199254740991;',
  6677. '$mod.i = -9007199254740991 + 9007199254740991;',
  6678. '']));
  6679. end;
  6680. procedure TTestModule.TestIntegerRange;
  6681. begin
  6682. StartProgram(false);
  6683. Add([
  6684. 'const',
  6685. ' MinInt = -1;',
  6686. ' MaxInt = +1;',
  6687. 'type',
  6688. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6689. ' TInt2 = 1..3;',
  6690. 'const',
  6691. ' a = low(TMyInt)+High(TMyInt);',
  6692. ' b = low(TInt2)+High(TInt2);',
  6693. ' s1 = [1];',
  6694. ' s2 = [1,2];',
  6695. ' s3 = [1..3];',
  6696. ' s4 = [low(shortint)..high(shortint)];',
  6697. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6698. ' s6 = 1 in s2;',
  6699. 'var',
  6700. ' i: TMyInt;',
  6701. ' i2: TInt2;',
  6702. 'begin',
  6703. ' i:=i2;',
  6704. ' i:=default(TMyInt);',
  6705. ' if i=i2 then ;']);
  6706. ConvertProgram;
  6707. CheckSource('TestIntegerRange',
  6708. LinesToStr([
  6709. 'this.MinInt = -1;',
  6710. 'this.MaxInt = +1;',
  6711. 'this.a = -1 + 1;',
  6712. 'this.b = 1 + 3;',
  6713. 'this.s1 = rtl.createSet(1);',
  6714. 'this.s2 = rtl.createSet(1, 2);',
  6715. 'this.s3 = rtl.createSet(null, 1, 3);',
  6716. 'this.s4 = rtl.createSet(null, -128, 127);',
  6717. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6718. 'this.s6 = 1 in this.s2;',
  6719. 'this.i = 0;',
  6720. 'this.i2 = 0;',
  6721. '']),
  6722. LinesToStr([
  6723. '$mod.i = $mod.i2;',
  6724. '$mod.i = -1;',
  6725. 'if ($mod.i === $mod.i2) ;',
  6726. '']));
  6727. end;
  6728. procedure TTestModule.TestIntegerTypecasts;
  6729. begin
  6730. StartProgram(false);
  6731. Add([
  6732. 'var',
  6733. ' i: nativeint;',
  6734. ' b: byte;',
  6735. ' sh: shortint;',
  6736. ' w: word;',
  6737. ' sm: smallint;',
  6738. ' lw: longword;',
  6739. ' li: longint;',
  6740. 'begin',
  6741. ' b:=byte(i);',
  6742. ' sh:=shortint(i);',
  6743. ' w:=word(i);',
  6744. ' sm:=smallint(i);',
  6745. ' lw:=longword(i);',
  6746. ' li:=longint(i);',
  6747. '']);
  6748. ConvertProgram;
  6749. CheckSource('TestIntegerTypecasts',
  6750. LinesToStr([
  6751. 'this.i = 0;',
  6752. 'this.b = 0;',
  6753. 'this.sh = 0;',
  6754. 'this.w = 0;',
  6755. 'this.sm = 0;',
  6756. 'this.lw = 0;',
  6757. 'this.li = 0;',
  6758. '']),
  6759. LinesToStr([
  6760. '$mod.b = $mod.i & 255;',
  6761. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6762. '$mod.w = $mod.i & 65535;',
  6763. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6764. '$mod.lw = $mod.i >>> 0;',
  6765. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6766. '']));
  6767. end;
  6768. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6769. begin
  6770. StartProgram(false);
  6771. Add([
  6772. 'var',
  6773. ' i,j: nativeint;',
  6774. 'begin',
  6775. ' i:=i shr 0;',
  6776. ' i:=i shr 1;',
  6777. ' i:=i shr 3;',
  6778. ' i:=i shr 54;',
  6779. ' i:=j shr i;',
  6780. '']);
  6781. ConvertProgram;
  6782. CheckResolverUnexpectedHints;
  6783. CheckSource('TestInteger_BitwiseShrNativeInt',
  6784. LinesToStr([
  6785. 'this.i = 0;',
  6786. 'this.j = 0;',
  6787. '']),
  6788. LinesToStr([
  6789. '$mod.i = $mod.i;',
  6790. '$mod.i = Math.floor($mod.i / 2);',
  6791. '$mod.i = Math.floor($mod.i / 8);',
  6792. '$mod.i = 0;',
  6793. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6794. '']));
  6795. end;
  6796. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6797. begin
  6798. StartProgram(false);
  6799. Add([
  6800. 'var',
  6801. ' i: nativeint;',
  6802. 'begin',
  6803. ' i:=i shl 0;',
  6804. ' i:=i shl 54;',
  6805. ' i:=123456789012 shl 1;',
  6806. ' i:=i shl 1;',
  6807. '']);
  6808. ConvertProgram;
  6809. CheckResolverUnexpectedHints;
  6810. CheckSource('TestInteger_BitwiseShrNativeInt',
  6811. LinesToStr([
  6812. 'this.i = 0;',
  6813. '']),
  6814. LinesToStr([
  6815. '$mod.i = $mod.i;',
  6816. '$mod.i = 0;',
  6817. '$mod.i = 246913578024;',
  6818. '$mod.i = rtl.shl($mod.i, 1);',
  6819. '']));
  6820. end;
  6821. procedure TTestModule.TestInteger_SystemFunc;
  6822. begin
  6823. StartProgram(true);
  6824. Add([
  6825. 'var',
  6826. ' i: byte;',
  6827. ' s: string;',
  6828. 'begin',
  6829. ' system.inc(i);',
  6830. ' system.str(i,s);',
  6831. ' s:=system.str(i);',
  6832. ' i:=system.low(i);',
  6833. ' i:=system.high(i);',
  6834. ' i:=system.pred(i);',
  6835. ' i:=system.succ(i);',
  6836. '']);
  6837. ConvertProgram;
  6838. CheckResolverUnexpectedHints;
  6839. CheckSource('TestInteger_SystemFunc',
  6840. LinesToStr([
  6841. 'this.i = 0;',
  6842. 'this.s = "";',
  6843. '']),
  6844. LinesToStr([
  6845. '$mod.i += 1;',
  6846. '$mod.s = "" + $mod.i;',
  6847. '$mod.s = "" + $mod.i;',
  6848. '$mod.i = 0;',
  6849. '$mod.i = 255;',
  6850. '$mod.i = $mod.i - 1;',
  6851. '$mod.i = $mod.i + 1;',
  6852. '']));
  6853. end;
  6854. procedure TTestModule.TestCurrency;
  6855. begin
  6856. StartProgram(false);
  6857. Add([
  6858. 'type',
  6859. ' TCoin = currency;',
  6860. 'const',
  6861. ' a = TCoin(2.7);',
  6862. ' b = a + TCoin(1.7);',
  6863. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6864. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6865. 'var',
  6866. ' c: TCoin = b;',
  6867. ' i: nativeint;',
  6868. ' d: double;',
  6869. ' j: jsvalue;',
  6870. 'function DoIt(c: currency): currency; begin end;',
  6871. 'function GetIt(d: double): double; begin end;',
  6872. 'procedure Write(v: jsvalue); begin end;',
  6873. 'begin',
  6874. ' c:=1.0;',
  6875. ' c:=0.1;',
  6876. ' c:=1.0/3.0;',
  6877. ' c:=1/3;',
  6878. ' c:=a;',
  6879. ' d:=c;',
  6880. ' c:=d;',
  6881. ' c:=currency(c);',
  6882. ' c:=currency(d);',
  6883. ' d:=double(c);',
  6884. ' c:=i;',
  6885. ' c:=currency(i);',
  6886. //' i:=c;', not allowed
  6887. ' i:=nativeint(c);',
  6888. ' c:=c+a;',
  6889. ' c:=-c-a;',
  6890. ' c:=d+c;',
  6891. ' c:=c+d;',
  6892. ' c:=d-c;',
  6893. ' c:=c-d;',
  6894. ' c:=c*a;',
  6895. ' c:=a*c;',
  6896. ' c:=d*c;',
  6897. ' c:=c*d;',
  6898. ' c:=c/a;',
  6899. ' c:=a/c;',
  6900. ' c:=d/c;',
  6901. ' c:=c/d;',
  6902. ' c:=c**a;',
  6903. ' c:=a**c;',
  6904. ' c:=d**c;',
  6905. ' c:=c**d;',
  6906. ' if c=c then ;',
  6907. ' if c=a then ;',
  6908. ' if a=c then ;',
  6909. ' if d=c then ;',
  6910. ' if c=d then ;',
  6911. ' c:=DoIt(c);',
  6912. ' c:=DoIt(i);',
  6913. ' c:=DoIt(d);',
  6914. ' c:=GetIt(c);',
  6915. ' j:=c;',
  6916. ' Write(c);',
  6917. ' c:=default(currency);',
  6918. ' j:=str(c);',
  6919. ' j:=str(c:0:3);',
  6920. '']);
  6921. ConvertProgram;
  6922. CheckSource('TestCurrency',
  6923. LinesToStr([
  6924. 'this.a = 27000;',
  6925. 'this.b = this.a + 17000;',
  6926. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6927. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6928. 'this.c = this.b;',
  6929. 'this.i = 0;',
  6930. 'this.d = 0.0;',
  6931. 'this.j = undefined;',
  6932. 'this.DoIt = function (c) {',
  6933. ' var Result = 0;',
  6934. ' return Result;',
  6935. '};',
  6936. 'this.GetIt = function (d) {',
  6937. ' var Result = 0.0;',
  6938. ' return Result;',
  6939. '};',
  6940. 'this.Write = function (v) {',
  6941. '};',
  6942. '']),
  6943. LinesToStr([
  6944. '$mod.c = 10000;',
  6945. '$mod.c = 1000;',
  6946. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6947. '$mod.c = Math.floor((1 / 3) * 10000);',
  6948. '$mod.c = $mod.a;',
  6949. '$mod.d = $mod.c / 10000;',
  6950. '$mod.c = Math.floor($mod.d * 10000);',
  6951. '$mod.c = $mod.c;',
  6952. '$mod.c = $mod.d * 10000;',
  6953. '$mod.d = $mod.c / 10000;',
  6954. '$mod.c = $mod.i * 10000;',
  6955. '$mod.c = $mod.i * 10000;',
  6956. '$mod.i = Math.floor($mod.c / 10000);',
  6957. '$mod.c = $mod.c + $mod.a;',
  6958. '$mod.c = -$mod.c - $mod.a;',
  6959. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6960. '$mod.c = $mod.c + ($mod.d * 10000);',
  6961. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6962. '$mod.c = $mod.c - ($mod.d * 10000);',
  6963. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6964. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6965. '$mod.c = $mod.d * $mod.c;',
  6966. '$mod.c = $mod.c * $mod.d;',
  6967. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6968. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6969. '$mod.c = Math.floor($mod.d / $mod.c);',
  6970. '$mod.c = Math.floor($mod.c / $mod.d);',
  6971. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6972. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6973. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6974. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6975. 'if ($mod.c === $mod.c) ;',
  6976. 'if ($mod.c === $mod.a) ;',
  6977. 'if ($mod.a === $mod.c) ;',
  6978. 'if (($mod.d * 10000) === $mod.c) ;',
  6979. 'if ($mod.c === ($mod.d * 10000)) ;',
  6980. '$mod.c = $mod.DoIt($mod.c);',
  6981. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6982. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6983. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6984. '$mod.j = $mod.c / 10000;',
  6985. '$mod.Write($mod.c / 10000);',
  6986. '$mod.c = 0;',
  6987. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6988. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6989. '']));
  6990. end;
  6991. procedure TTestModule.TestForBoolDo;
  6992. begin
  6993. StartProgram(false);
  6994. Add([
  6995. 'var b: boolean;',
  6996. 'begin',
  6997. ' for b:=false to true do ;',
  6998. ' for b:=b downto false do ;',
  6999. ' for b in boolean do ;',
  7000. '']);
  7001. ConvertProgram;
  7002. CheckSource('TestForBoolDo',
  7003. LinesToStr([ // statements
  7004. 'this.b = false;']),
  7005. LinesToStr([ // this.$main
  7006. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7007. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7008. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7009. '']));
  7010. end;
  7011. procedure TTestModule.TestForIntDo;
  7012. begin
  7013. StartProgram(false);
  7014. Add([
  7015. 'var i: longint;',
  7016. 'begin',
  7017. ' for i:=3 to 5 do ;',
  7018. ' for i:=i downto 2 do ;',
  7019. ' for i in byte do ;',
  7020. '']);
  7021. ConvertProgram;
  7022. CheckSource('TestForIntDo',
  7023. LinesToStr([ // statements
  7024. 'this.i = 0;']),
  7025. LinesToStr([ // this.$main
  7026. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7027. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7028. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7029. '']));
  7030. end;
  7031. procedure TTestModule.TestForIntInDo;
  7032. begin
  7033. StartProgram(false);
  7034. Add([
  7035. 'type',
  7036. ' TSetOfInt = set of byte;',
  7037. ' TIntRg = 3..7;',
  7038. ' TSetOfIntRg = set of TIntRg;',
  7039. 'var',
  7040. ' i,i2: longint;',
  7041. ' a1: array of byte;',
  7042. ' a2: array[1..3] of byte;',
  7043. ' soi: TSetOfInt;',
  7044. ' soir: TSetOfIntRg;',
  7045. ' ir: TIntRg;',
  7046. 'begin',
  7047. ' for i in byte do ;',
  7048. ' for i in a1 do ;',
  7049. ' for i in a2 do ;',
  7050. ' for i in [11..13] do ;',
  7051. ' for i in TSetOfInt do ;',
  7052. ' for i in TIntRg do ;',
  7053. ' for i in soi do i2:=i;',
  7054. ' for i in TSetOfIntRg do ;',
  7055. ' for i in soir do ;',
  7056. ' for ir in TIntRg do ;',
  7057. ' for ir in TSetOfIntRg do ;',
  7058. ' for ir in soir do ;',
  7059. '']);
  7060. ConvertProgram;
  7061. CheckSource('TestForIntInDo',
  7062. LinesToStr([ // statements
  7063. 'this.i = 0;',
  7064. 'this.i2 = 0;',
  7065. 'this.a1 = [];',
  7066. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7067. 'this.soi = {};',
  7068. 'this.soir = {};',
  7069. 'this.ir = 0;',
  7070. '']),
  7071. LinesToStr([ // this.$main
  7072. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7073. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7074. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7075. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7076. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7077. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7078. 'for (var $l6 in $mod.soi) {',
  7079. ' $mod.i = +$l6;',
  7080. ' $mod.i2 = $mod.i;',
  7081. '};',
  7082. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7083. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7084. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7085. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7086. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7087. '']));
  7088. end;
  7089. procedure TTestModule.TestCharConst;
  7090. begin
  7091. StartProgram(false);
  7092. Add([
  7093. 'const',
  7094. ' a = #$00F3;',
  7095. ' c: char = ''1'';',
  7096. 'begin',
  7097. ' c:=#0;',
  7098. ' c:=#1;',
  7099. ' c:=#9;',
  7100. ' c:=#10;',
  7101. ' c:=#13;',
  7102. ' c:=#31;',
  7103. ' c:=#32;',
  7104. ' c:=#$A;',
  7105. ' c:=#$0A;',
  7106. ' c:=#$b;',
  7107. ' c:=#$0b;',
  7108. ' c:=^A;',
  7109. ' c:=''"'';',
  7110. ' c:=default(char);',
  7111. ' c:=#$00E4;', // ä
  7112. ' c:=''ä'';',
  7113. ' c:=#$E4;', // ä
  7114. ' c:=#$D800;', // invalid UTF-16
  7115. ' c:=#$DFFF;', // invalid UTF-16
  7116. ' c:=#$FFFF;', // last UCS-2
  7117. ' c:=high(c);', // last UCS-2
  7118. '']);
  7119. ConvertProgram;
  7120. CheckSource('TestCharConst',
  7121. LinesToStr([
  7122. 'this.a="ó";',
  7123. 'this.c="1";'
  7124. ]),
  7125. LinesToStr([
  7126. '$mod.c="\x00";',
  7127. '$mod.c="\x01";',
  7128. '$mod.c="\t";',
  7129. '$mod.c="\n";',
  7130. '$mod.c="\r";',
  7131. '$mod.c="\x1F";',
  7132. '$mod.c=" ";',
  7133. '$mod.c="\n";',
  7134. '$mod.c="\n";',
  7135. '$mod.c="\x0B";',
  7136. '$mod.c="\x0B";',
  7137. '$mod.c="\x01";',
  7138. '$mod.c=''"'';',
  7139. '$mod.c="\x00";',
  7140. '$mod.c = "ä";',
  7141. '$mod.c = "ä";',
  7142. '$mod.c = "ä";',
  7143. '$mod.c="\uD800";',
  7144. '$mod.c="\uDFFF";',
  7145. '$mod.c="\uFFFF";',
  7146. '$mod.c="\uFFFF";',
  7147. '']));
  7148. end;
  7149. procedure TTestModule.TestChar_Compare;
  7150. begin
  7151. StartProgram(false);
  7152. Add('var');
  7153. Add(' c: char;');
  7154. Add(' b: boolean;');
  7155. Add('begin');
  7156. Add(' b:=c=''1'';');
  7157. Add(' b:=''2''=c;');
  7158. Add(' b:=''3''=''4'';');
  7159. Add(' b:=c<>''5'';');
  7160. Add(' b:=''6''<>c;');
  7161. Add(' b:=c>''7'';');
  7162. Add(' b:=''8''>c;');
  7163. Add(' b:=c>=''9'';');
  7164. Add(' b:=''A''>=c;');
  7165. Add(' b:=c<''B'';');
  7166. Add(' b:=''C''<c;');
  7167. Add(' b:=c<=''D'';');
  7168. Add(' b:=''E''<=c;');
  7169. ConvertProgram;
  7170. CheckSource('TestChar_Compare',
  7171. LinesToStr([
  7172. 'this.c="";',
  7173. 'this.b = false;'
  7174. ]),
  7175. LinesToStr([
  7176. '$mod.b = $mod.c === "1";',
  7177. '$mod.b = "2" === $mod.c;',
  7178. '$mod.b = "3" === "4";',
  7179. '$mod.b = $mod.c !== "5";',
  7180. '$mod.b = "6" !== $mod.c;',
  7181. '$mod.b = $mod.c > "7";',
  7182. '$mod.b = "8" > $mod.c;',
  7183. '$mod.b = $mod.c >= "9";',
  7184. '$mod.b = "A" >= $mod.c;',
  7185. '$mod.b = $mod.c < "B";',
  7186. '$mod.b = "C" < $mod.c;',
  7187. '$mod.b = $mod.c <= "D";',
  7188. '$mod.b = "E" <= $mod.c;',
  7189. '']));
  7190. end;
  7191. procedure TTestModule.TestChar_BuiltInProcs;
  7192. begin
  7193. StartProgram(false);
  7194. Add([
  7195. 'var',
  7196. ' c: char;',
  7197. ' i: longint;',
  7198. ' s: string;',
  7199. 'begin',
  7200. ' i:=ord(c);',
  7201. ' i:=ord(s[i]);',
  7202. ' c:=chr(i);',
  7203. ' c:=pred(c);',
  7204. ' c:=succ(c);',
  7205. ' c:=low(c);',
  7206. ' c:=high(c);',
  7207. ' i:=byte(c);',
  7208. ' i:=word(c);',
  7209. ' i:=longint(c);',
  7210. '']);
  7211. ConvertProgram;
  7212. CheckSource('TestChar_BuiltInProcs',
  7213. LinesToStr([
  7214. 'this.c = "";',
  7215. 'this.i = 0;',
  7216. 'this.s = "";'
  7217. ]),
  7218. LinesToStr([
  7219. '$mod.i = $mod.c.charCodeAt();',
  7220. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7221. '$mod.c = String.fromCharCode($mod.i);',
  7222. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7223. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7224. '$mod.c = "\x00";',
  7225. '$mod.c = "\uFFFF";',
  7226. '$mod.i = $mod.c.charCodeAt() & 255;',
  7227. '$mod.i = $mod.c.charCodeAt();',
  7228. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7229. '']));
  7230. end;
  7231. procedure TTestModule.TestStringConst;
  7232. begin
  7233. StartProgram(false);
  7234. Add([
  7235. '{$H+}',
  7236. 'const',
  7237. ' a = #$00F3#$017C;', // first <256, then >=256
  7238. ' b = string(''a'');',
  7239. ' c = string(''ä'');',
  7240. ' d = UnicodeString(''b'');',
  7241. ' e = UnicodeString(''ö'');',
  7242. 'var',
  7243. ' s: string = ''abc'';',
  7244. 'begin',
  7245. ' s:='''';',
  7246. ' s:=#13#10;',
  7247. ' s:=#9''foo'';',
  7248. ' s:=#$A9;',
  7249. ' s:=''foo''#13''bar'';',
  7250. ' s:=''"'';',
  7251. ' s:=''"''''"'';',
  7252. ' s:=#$20AC;', // euro
  7253. ' s:=#$10437;', // outside BMP
  7254. ' s:=default(string);',
  7255. ' s:=concat(s);',
  7256. ' s:=concat(s,''a'',s)',
  7257. '']);
  7258. ConvertProgram;
  7259. CheckSource('TestStringConst',
  7260. LinesToStr([
  7261. 'this.a = "óż";',
  7262. 'this.b = "a";',
  7263. 'this.c = "ä";',
  7264. 'this.d = "b";',
  7265. 'this.e = "ö";',
  7266. 'this.s="abc";',
  7267. '']),
  7268. LinesToStr([
  7269. '$mod.s="";',
  7270. '$mod.s="\r\n";',
  7271. '$mod.s="\tfoo";',
  7272. '$mod.s="©";',
  7273. '$mod.s="foo\rbar";',
  7274. '$mod.s=''"'';',
  7275. '$mod.s=''"\''"'';',
  7276. '$mod.s="€";',
  7277. '$mod.s="'#$F0#$90#$90#$B7'";',
  7278. '$mod.s="";',
  7279. '$mod.s = $mod.s;',
  7280. '$mod.s = $mod.s.concat("a", $mod.s);',
  7281. '']));
  7282. end;
  7283. procedure TTestModule.TestStringConstSurrogate;
  7284. begin
  7285. StartProgram(false);
  7286. Add([
  7287. 'var',
  7288. ' s: string;',
  7289. 'begin',
  7290. ' s:=''😊'';', // 1F60A
  7291. '']);
  7292. ConvertProgram;
  7293. CheckSource('TestStringConstSurrogate',
  7294. LinesToStr([
  7295. 'this.s="";'
  7296. ]),
  7297. LinesToStr([
  7298. '$mod.s="😊";'
  7299. ]));
  7300. end;
  7301. procedure TTestModule.TestString_Length;
  7302. begin
  7303. StartProgram(false);
  7304. Add('const c = ''foo'';');
  7305. Add('var');
  7306. Add(' s: string;');
  7307. Add(' i: longint;');
  7308. Add('begin');
  7309. Add(' i:=length(s);');
  7310. Add(' i:=length(s+s);');
  7311. Add(' i:=length(''abc'');');
  7312. Add(' i:=length(c);');
  7313. ConvertProgram;
  7314. CheckSource('TestString_Length',
  7315. LinesToStr([
  7316. 'this.c = "foo";',
  7317. 'this.s = "";',
  7318. 'this.i = 0;',
  7319. '']),
  7320. LinesToStr([
  7321. '$mod.i = $mod.s.length;',
  7322. '$mod.i = ($mod.s+$mod.s).length;',
  7323. '$mod.i = "abc".length;',
  7324. '$mod.i = $mod.c.length;',
  7325. '']));
  7326. end;
  7327. procedure TTestModule.TestString_Compare;
  7328. begin
  7329. StartProgram(false);
  7330. Add('var');
  7331. Add(' s, t: string;');
  7332. Add(' b: boolean;');
  7333. Add('begin');
  7334. Add(' b:=s=t;');
  7335. Add(' b:=s<>t;');
  7336. Add(' b:=s>t;');
  7337. Add(' b:=s>=t;');
  7338. Add(' b:=s<t;');
  7339. Add(' b:=s<=t;');
  7340. ConvertProgram;
  7341. CheckSource('TestString_Compare',
  7342. LinesToStr([ // statements
  7343. 'this.s = "";',
  7344. 'this.t = "";',
  7345. 'this.b =false;'
  7346. ]),
  7347. LinesToStr([ // this.$main
  7348. '$mod.b = $mod.s === $mod.t;',
  7349. '$mod.b = $mod.s !== $mod.t;',
  7350. '$mod.b = $mod.s > $mod.t;',
  7351. '$mod.b = $mod.s >= $mod.t;',
  7352. '$mod.b = $mod.s < $mod.t;',
  7353. '$mod.b = $mod.s <= $mod.t;',
  7354. '']));
  7355. end;
  7356. procedure TTestModule.TestString_SetLength;
  7357. begin
  7358. StartProgram(false);
  7359. Add([
  7360. 'procedure DoIt(var s: string);',
  7361. 'begin',
  7362. ' SetLength(s,2);',
  7363. 'end;',
  7364. 'var s: string;',
  7365. 'begin',
  7366. ' SetLength(s,3);',
  7367. '']);
  7368. ConvertProgram;
  7369. CheckSource('TestString_SetLength',
  7370. LinesToStr([ // statements
  7371. 'this.DoIt = function (s) {',
  7372. ' s.set(rtl.strSetLength(s.get(), 2));',
  7373. '};',
  7374. 'this.s = "";',
  7375. '']),
  7376. LinesToStr([ // this.$main
  7377. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7378. ]));
  7379. end;
  7380. procedure TTestModule.TestString_CharAt;
  7381. begin
  7382. StartProgram(false);
  7383. Add([
  7384. 'var',
  7385. ' s: string;',
  7386. ' c: char;',
  7387. ' b: boolean;',
  7388. 'begin',
  7389. ' b:= s[1] = c;',
  7390. ' b:= c = s[1];',
  7391. ' b:= c <> s[1];',
  7392. ' b:= c > s[1];',
  7393. ' b:= c >= s[1];',
  7394. ' b:= c < s[2];',
  7395. ' b:= c <= s[1];',
  7396. ' s[1] := c;',
  7397. ' s[2+3] := c;']);
  7398. ConvertProgram;
  7399. CheckSource('TestString_CharAt',
  7400. LinesToStr([ // statements
  7401. 'this.s = "";',
  7402. 'this.c = "";',
  7403. 'this.b = false;'
  7404. ]),
  7405. LinesToStr([ // this.$main
  7406. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7407. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7408. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7409. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7410. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7411. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7412. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7413. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7414. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7415. '']));
  7416. end;
  7417. procedure TTestModule.TestStringHMinusFail;
  7418. begin
  7419. StartProgram(false);
  7420. Add([
  7421. '{$H-}',
  7422. 'var s: string;',
  7423. 'begin']);
  7424. ConvertProgram;
  7425. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7426. end;
  7427. procedure TTestModule.TestStr;
  7428. begin
  7429. StartProgram(false);
  7430. Add('var');
  7431. Add(' b: boolean;');
  7432. Add(' i: longint;');
  7433. Add(' d: double;');
  7434. Add(' s: string;');
  7435. Add('begin');
  7436. Add(' str(b,s);');
  7437. Add(' str(i,s);');
  7438. Add(' str(d,s);');
  7439. Add(' str(i:3,s);');
  7440. Add(' str(d:3:2,s);');
  7441. Add(' Str(12.456:12:1,s);');
  7442. Add(' Str(12.456:12,s);');
  7443. Add(' s:=str(b);');
  7444. Add(' s:=str(i);');
  7445. Add(' s:=str(d);');
  7446. Add(' s:=str(i,i);');
  7447. Add(' s:=str(i:3);');
  7448. Add(' s:=str(d:3:2);');
  7449. Add(' s:=str(i:4,i);');
  7450. Add(' s:=str(i,i:5);');
  7451. Add(' s:=str(i:4,i:5);');
  7452. Add(' s:=str(s,s);');
  7453. Add(' s:=str(s,''foo'');');
  7454. ConvertProgram;
  7455. CheckSource('TestStr',
  7456. LinesToStr([ // statements
  7457. 'this.b = false;',
  7458. 'this.i = 0;',
  7459. 'this.d = 0.0;',
  7460. 'this.s = "";',
  7461. '']),
  7462. LinesToStr([ // this.$main
  7463. '$mod.s = ""+$mod.b;',
  7464. '$mod.s = ""+$mod.i;',
  7465. '$mod.s = rtl.floatToStr($mod.d);',
  7466. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7467. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7468. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7469. '$mod.s = rtl.floatToStr(12.456,12);',
  7470. '$mod.s = ""+$mod.b;',
  7471. '$mod.s = ""+$mod.i;',
  7472. '$mod.s = rtl.floatToStr($mod.d);',
  7473. '$mod.s = ""+$mod.i+$mod.i;',
  7474. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7475. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7476. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7477. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7478. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7479. '$mod.s = $mod.s + $mod.s;',
  7480. '$mod.s = $mod.s + "foo";',
  7481. '']));
  7482. end;
  7483. procedure TTestModule.TestBaseType_AnsiStringFail;
  7484. begin
  7485. StartProgram(false);
  7486. Add('var s: AnsiString');
  7487. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7488. ConvertProgram;
  7489. end;
  7490. procedure TTestModule.TestBaseType_WideStringFail;
  7491. begin
  7492. StartProgram(false);
  7493. Add('var s: WideString');
  7494. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7495. ConvertProgram;
  7496. end;
  7497. procedure TTestModule.TestBaseType_ShortStringFail;
  7498. begin
  7499. StartProgram(false);
  7500. Add('var s: ShortString');
  7501. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7502. ConvertProgram;
  7503. end;
  7504. procedure TTestModule.TestBaseType_RawByteStringFail;
  7505. begin
  7506. StartProgram(false);
  7507. Add('var s: RawByteString');
  7508. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7509. ConvertProgram;
  7510. end;
  7511. procedure TTestModule.TestTypeShortstring_Fail;
  7512. begin
  7513. StartProgram(false);
  7514. Add('type t = string[12];');
  7515. Add('var s: t;');
  7516. Add('begin');
  7517. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7518. ConvertProgram;
  7519. end;
  7520. procedure TTestModule.TestCharSet_Custom;
  7521. begin
  7522. StartProgram(false);
  7523. Add([
  7524. 'type',
  7525. ' TCharRg = ''a''..''z'';',
  7526. ' TSetOfCharRg = set of TCharRg;',
  7527. ' TCharRg2 = ''m''..''p'';',
  7528. 'const',
  7529. ' crg: TCharRg = ''b'';',
  7530. 'var',
  7531. ' c: char;',
  7532. ' crg2: TCharRg2;',
  7533. ' s: TSetOfCharRg;',
  7534. 'begin',
  7535. ' c:=crg;',
  7536. ' crg:=c;',
  7537. ' crg2:=crg;',
  7538. ' if c=crg then ;',
  7539. ' if crg=c then ;',
  7540. ' if crg=crg2 then ;',
  7541. ' if c in s then ;',
  7542. ' if crg2 in s then ;',
  7543. ' c:=default(TCharRg);',
  7544. '']);
  7545. ConvertProgram;
  7546. CheckSource('TestCharSet_Custom',
  7547. LinesToStr([ // statements
  7548. 'this.crg = "b";',
  7549. 'this.c = "";',
  7550. 'this.crg2 = "m";',
  7551. 'this.s = {};',
  7552. '']),
  7553. LinesToStr([ // this.$main
  7554. '$mod.c = $mod.crg;',
  7555. '$mod.crg = $mod.c;',
  7556. '$mod.crg2 = $mod.crg;',
  7557. 'if ($mod.c === $mod.crg) ;',
  7558. 'if ($mod.crg === $mod.c) ;',
  7559. 'if ($mod.crg === $mod.crg2) ;',
  7560. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7561. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7562. '$mod.c = "a";',
  7563. '']));
  7564. end;
  7565. procedure TTestModule.TestForCharDo;
  7566. begin
  7567. StartProgram(false);
  7568. Add([
  7569. 'var c: char;',
  7570. 'begin',
  7571. ' for c:=''a'' to ''c'' do ;',
  7572. ' for c:=c downto ''a'' do ;',
  7573. ' for c:=''Б'' to ''Я'' do ;',
  7574. '']);
  7575. ConvertProgram;
  7576. CheckSource('TestForCharDo',
  7577. LinesToStr([ // statements
  7578. 'this.c = "";']),
  7579. LinesToStr([ // this.$main
  7580. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7581. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7582. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7583. '']));
  7584. end;
  7585. procedure TTestModule.TestForCharInDo;
  7586. begin
  7587. StartProgram(false);
  7588. Add([
  7589. 'type',
  7590. ' TSetOfChar = set of char;',
  7591. ' TCharRg = ''a''..''z'';',
  7592. ' TSetOfCharRg = set of TCharRg;',
  7593. 'const Foo = ''foo'';',
  7594. 'var',
  7595. ' c,c2: char;',
  7596. ' s: string;',
  7597. ' a1: array of char;',
  7598. ' a2: array[1..3] of char;',
  7599. ' soc: TSetOfChar;',
  7600. ' socr: TSetOfCharRg;',
  7601. ' cr: TCharRg;',
  7602. 'begin',
  7603. ' for c in foo do ;',
  7604. ' for c in s do ;',
  7605. ' for c in char do ;',
  7606. ' for c in a1 do ;',
  7607. ' for c in a2 do ;',
  7608. ' for c in [''1''..''3''] do ;',
  7609. ' for c in TSetOfChar do ;',
  7610. ' for c in TCharRg do ;',
  7611. ' for c in soc do c2:=c;',
  7612. ' for c in TSetOfCharRg do ;',
  7613. ' for c in socr do ;',
  7614. ' for cr in TCharRg do ;',
  7615. ' for cr in TSetOfCharRg do ;',
  7616. ' for cr in socr do ;',
  7617. '']);
  7618. ConvertProgram;
  7619. CheckSource('TestForCharInDo',
  7620. LinesToStr([ // statements
  7621. 'this.Foo = "foo";',
  7622. 'this.c = "";',
  7623. 'this.c2 = "";',
  7624. 'this.s = "";',
  7625. 'this.a1 = [];',
  7626. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7627. 'this.soc = {};',
  7628. 'this.socr = {};',
  7629. 'this.cr = "a";',
  7630. '']),
  7631. LinesToStr([ // this.$main
  7632. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7633. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7634. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7635. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7636. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7637. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7638. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7639. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7640. 'for (var $l8 in $mod.soc) {',
  7641. ' $mod.c = String.fromCharCode($l8);',
  7642. ' $mod.c2 = $mod.c;',
  7643. '};',
  7644. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7645. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7646. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7647. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7648. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7649. '']));
  7650. end;
  7651. procedure TTestModule.TestProcTwoArgs;
  7652. begin
  7653. StartProgram(false);
  7654. Add('procedure Test(a,b: longint);');
  7655. Add('begin');
  7656. Add('end;');
  7657. Add('begin');
  7658. ConvertProgram;
  7659. CheckSource('TestProcTwoArgs',
  7660. LinesToStr([ // statements
  7661. 'this.Test = function (a,b) {',
  7662. '};'
  7663. ]),
  7664. LinesToStr([ // this.$main
  7665. ''
  7666. ]));
  7667. end;
  7668. procedure TTestModule.TestProc_DefaultValue;
  7669. begin
  7670. StartProgram(false);
  7671. Add('procedure p1(i: longint = 1);');
  7672. Add('begin');
  7673. Add('end;');
  7674. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7675. Add('begin');
  7676. Add('end;');
  7677. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7678. Add('begin');
  7679. Add('end;');
  7680. Add('begin');
  7681. Add(' p1;');
  7682. Add(' p1();');
  7683. Add(' p1(11);');
  7684. Add(' p2;');
  7685. Add(' p2();');
  7686. Add(' p2(12);');
  7687. Add(' p2(13,''b'');');
  7688. Add(' p3();');
  7689. ConvertProgram;
  7690. CheckSource('TestProc_DefaultValue',
  7691. LinesToStr([ // statements
  7692. 'this.p1 = function (i) {',
  7693. '};',
  7694. 'this.p2 = function (i,c) {',
  7695. '};',
  7696. 'this.p3 = function (d,b,s) {',
  7697. '};'
  7698. ]),
  7699. LinesToStr([ // this.$main
  7700. ' $mod.p1(1);',
  7701. ' $mod.p1(1);',
  7702. ' $mod.p1(11);',
  7703. ' $mod.p2(1,"a");',
  7704. ' $mod.p2(1,"a");',
  7705. ' $mod.p2(12,"a");',
  7706. ' $mod.p2(13,"b");',
  7707. ' $mod.p3(1.0,false,"abc");'
  7708. ]));
  7709. end;
  7710. procedure TTestModule.TestFunctionInt;
  7711. begin
  7712. StartProgram(false);
  7713. Add('function MyTest(Bar: longint): longint;');
  7714. Add('begin');
  7715. Add(' Result:=2*bar');
  7716. Add('end;');
  7717. Add('begin');
  7718. ConvertProgram;
  7719. CheckSource('TestFunctionInt',
  7720. LinesToStr([ // statements
  7721. 'this.MyTest = function (Bar) {',
  7722. ' var Result = 0;',
  7723. ' Result = 2*Bar;',
  7724. ' return Result;',
  7725. '};'
  7726. ]),
  7727. LinesToStr([ // this.$main
  7728. ''
  7729. ]));
  7730. end;
  7731. procedure TTestModule.TestFunctionString;
  7732. begin
  7733. StartProgram(false);
  7734. Add('function Test(Bar: string): string;');
  7735. Add('begin');
  7736. Add(' Result:=bar+BAR');
  7737. Add('end;');
  7738. Add('begin');
  7739. ConvertProgram;
  7740. CheckSource('TestFunctionString',
  7741. LinesToStr([ // statements
  7742. 'this.Test = function (Bar) {',
  7743. ' var Result = "";',
  7744. ' Result = Bar+Bar;',
  7745. ' return Result;',
  7746. '};'
  7747. ]),
  7748. LinesToStr([ // this.$main
  7749. ''
  7750. ]));
  7751. end;
  7752. procedure TTestModule.TestIfThen;
  7753. begin
  7754. StartProgram(false);
  7755. Add([
  7756. 'var b: boolean;',
  7757. 'begin',
  7758. ' if b then ;',
  7759. ' if b then else ;']);
  7760. ConvertProgram;
  7761. CheckSource('TestIfThen',
  7762. LinesToStr([ // statements
  7763. 'this.b = false;',
  7764. '']),
  7765. LinesToStr([ // this.$main
  7766. 'if ($mod.b) ;',
  7767. 'if ($mod.b) ;',
  7768. '']));
  7769. end;
  7770. procedure TTestModule.TestForLoop;
  7771. begin
  7772. StartProgram(false);
  7773. Add('var');
  7774. Add(' vI, vJ, vN: longint;');
  7775. Add('begin');
  7776. Add(' VJ:=0;');
  7777. Add(' VN:=3;');
  7778. Add(' for VI:=1 to VN do');
  7779. Add(' begin');
  7780. Add(' VJ:=VJ+VI;');
  7781. Add(' end;');
  7782. ConvertProgram;
  7783. CheckSource('TestForLoop',
  7784. LinesToStr([ // statements
  7785. 'this.vI = 0;',
  7786. 'this.vJ = 0;',
  7787. 'this.vN = 0;'
  7788. ]),
  7789. LinesToStr([ // this.$main
  7790. ' $mod.vJ = 0;',
  7791. ' $mod.vN = 3;',
  7792. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7793. ' $mod.vI = $l;',
  7794. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7795. ' };',
  7796. '']));
  7797. end;
  7798. procedure TTestModule.TestForLoopInsideFunction;
  7799. begin
  7800. StartProgram(false);
  7801. Add('function SumNumbers(Count: longint): longint;');
  7802. Add('var');
  7803. Add(' vI, vJ: longint;');
  7804. Add('begin');
  7805. Add(' vj:=0;');
  7806. Add(' for vi:=1 to count do');
  7807. Add(' begin');
  7808. Add(' vj:=vj+vi;');
  7809. Add(' end;');
  7810. Add('end;');
  7811. Add('begin');
  7812. Add(' sumnumbers(3);');
  7813. ConvertProgram;
  7814. CheckSource('TestForLoopInsideFunction',
  7815. LinesToStr([ // statements
  7816. 'this.SumNumbers = function (Count) {',
  7817. ' var Result = 0;',
  7818. ' var vI = 0;',
  7819. ' var vJ = 0;',
  7820. ' vJ = 0;',
  7821. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7822. ' vI = $l;',
  7823. ' vJ = vJ + vI;',
  7824. ' };',
  7825. ' return Result;',
  7826. '};'
  7827. ]),
  7828. LinesToStr([ // $mod.$main
  7829. ' $mod.SumNumbers(3);'
  7830. ]));
  7831. end;
  7832. procedure TTestModule.TestForLoop_ReadVarAfter;
  7833. begin
  7834. StartProgram(false);
  7835. Add('var');
  7836. Add(' vI: longint;');
  7837. Add('begin');
  7838. Add(' for vi:=1 to 2 do ;');
  7839. Add(' if vi=3 then ;');
  7840. ConvertProgram;
  7841. CheckSource('TestForLoop',
  7842. LinesToStr([ // statements
  7843. 'this.vI = 0;'
  7844. ]),
  7845. LinesToStr([ // this.$main
  7846. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7847. ' if ($mod.vI===3) ;'
  7848. ]));
  7849. end;
  7850. procedure TTestModule.TestForLoop_Nested;
  7851. begin
  7852. StartProgram(false);
  7853. Add('function SumNumbers(Count: longint): longint;');
  7854. Add('var');
  7855. Add(' vI, vJ, vK: longint;');
  7856. Add('begin');
  7857. Add(' VK:=0;');
  7858. Add(' for VI:=1 to count do');
  7859. Add(' begin');
  7860. Add(' for vj:=1 to vi do');
  7861. Add(' begin');
  7862. Add(' vk:=VK+VI;');
  7863. Add(' end;');
  7864. Add(' end;');
  7865. Add('end;');
  7866. Add('begin');
  7867. Add(' sumnumbers(3);');
  7868. ConvertProgram;
  7869. CheckSource('TestForLoopInFunction',
  7870. LinesToStr([ // statements
  7871. 'this.SumNumbers = function (Count) {',
  7872. ' var Result = 0;',
  7873. ' var vI = 0;',
  7874. ' var vJ = 0;',
  7875. ' var vK = 0;',
  7876. ' vK = 0;',
  7877. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7878. ' vI = $l;',
  7879. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  7880. ' vJ = $l1;',
  7881. ' vK = vK + vI;',
  7882. ' };',
  7883. ' };',
  7884. ' return Result;',
  7885. '};'
  7886. ]),
  7887. LinesToStr([ // $mod.$main
  7888. ' $mod.SumNumbers(3);'
  7889. ]));
  7890. end;
  7891. procedure TTestModule.TestRepeatUntil;
  7892. begin
  7893. StartProgram(false);
  7894. Add('var');
  7895. Add(' vI, vJ, vN: longint;');
  7896. Add('begin');
  7897. Add(' vn:=3;');
  7898. Add(' vj:=0;');
  7899. Add(' VI:=0;');
  7900. Add(' repeat');
  7901. Add(' VI:=vi+1;');
  7902. Add(' vj:=VJ+vI;');
  7903. Add(' until vi>=vn');
  7904. ConvertProgram;
  7905. CheckSource('TestRepeatUntil',
  7906. LinesToStr([ // statements
  7907. 'this.vI = 0;',
  7908. 'this.vJ = 0;',
  7909. 'this.vN = 0;'
  7910. ]),
  7911. LinesToStr([ // $mod.$main
  7912. ' $mod.vN = 3;',
  7913. ' $mod.vJ = 0;',
  7914. ' $mod.vI = 0;',
  7915. ' do{',
  7916. ' $mod.vI = $mod.vI + 1;',
  7917. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7918. ' }while(!($mod.vI>=$mod.vN));'
  7919. ]));
  7920. end;
  7921. procedure TTestModule.TestAsmBlock;
  7922. begin
  7923. StartProgram(false);
  7924. Add([
  7925. 'var',
  7926. ' vI: longint;',
  7927. 'begin',
  7928. ' vi:=1;',
  7929. ' asm',
  7930. ' if (vI===1) {',
  7931. ' vI=2;',
  7932. //' console.log(''end;'');', ToDo
  7933. ' }',
  7934. ' if (vI===2){ vI=3; }',
  7935. ' end;',
  7936. ' VI:=4;']);
  7937. ConvertProgram;
  7938. CheckSource('TestAsmBlock',
  7939. LinesToStr([ // statements
  7940. 'this.vI = 0;'
  7941. ]),
  7942. LinesToStr([ // $mod.$main
  7943. '$mod.vI = 1;',
  7944. 'if (vI===1) {',
  7945. ' vI=2;',
  7946. '}',
  7947. 'if (vI===2){ vI=3; }',
  7948. ';',
  7949. '$mod.vI = 4;'
  7950. ]));
  7951. end;
  7952. procedure TTestModule.TestAsmPas_Impl;
  7953. begin
  7954. StartUnit(false);
  7955. Add('interface');
  7956. Add('const cIntf: longint = 1;');
  7957. Add('var vIntf: longint;');
  7958. Add('implementation');
  7959. Add('const cImpl: longint = 2;');
  7960. Add('var vImpl: longint;');
  7961. Add('procedure DoIt;');
  7962. Add('const cLoc: longint = 3;');
  7963. Add('var vLoc: longint;');
  7964. Add('begin;');
  7965. Add(' asm');
  7966. //Add(' pas(vIntf)=pas(cIntf);');
  7967. //Add(' pas(vImpl)=pas(cImpl);');
  7968. //Add(' pas(vLoc)=pas(cLoc);');
  7969. Add(' end;');
  7970. Add('end;');
  7971. ConvertUnit;
  7972. CheckSource('TestAsmPas_Impl',
  7973. LinesToStr([
  7974. 'var $impl = $mod.$impl;',
  7975. 'this.cIntf = 1;',
  7976. 'this.vIntf = 0;',
  7977. '']),
  7978. '', // this.$init
  7979. LinesToStr([ // implementation
  7980. '$impl.cImpl = 2;',
  7981. '$impl.vImpl = 0;',
  7982. 'var cLoc = 3;',
  7983. '$impl.DoIt = function () {',
  7984. ' var vLoc = 0;',
  7985. '};',
  7986. '']) );
  7987. end;
  7988. procedure TTestModule.TestTryFinally;
  7989. begin
  7990. StartProgram(false);
  7991. Add('var i: longint;');
  7992. Add('begin');
  7993. Add(' try');
  7994. Add(' i:=0; i:=2 div i;');
  7995. Add(' finally');
  7996. Add(' i:=3');
  7997. Add(' end;');
  7998. ConvertProgram;
  7999. CheckSource('TestTryFinally',
  8000. LinesToStr([ // statements
  8001. 'this.i = 0;'
  8002. ]),
  8003. LinesToStr([ // $mod.$main
  8004. 'try {',
  8005. ' $mod.i = 0;',
  8006. ' $mod.i = Math.floor(2 / $mod.i);',
  8007. '} finally {',
  8008. ' $mod.i = 3;',
  8009. '};'
  8010. ]));
  8011. end;
  8012. procedure TTestModule.TestTryExcept;
  8013. begin
  8014. StartProgram(false);
  8015. Add([
  8016. 'type',
  8017. ' TObject = class end;',
  8018. ' Exception = class Msg: string; end;',
  8019. ' EInvalidCast = class(Exception) end;',
  8020. 'var vI: longint;',
  8021. 'begin',
  8022. ' try',
  8023. ' vi:=1;',
  8024. ' except',
  8025. ' vi:=2',
  8026. ' end;',
  8027. ' try',
  8028. ' vi:=3;',
  8029. ' except',
  8030. ' raise;',
  8031. ' end;',
  8032. ' try',
  8033. ' VI:=4;',
  8034. ' except',
  8035. ' on einvalidcast do',
  8036. ' raise;',
  8037. ' on E: exception do',
  8038. ' if e.msg='''' then',
  8039. ' raise e;',
  8040. ' else',
  8041. ' vi:=5',
  8042. ' end;',
  8043. ' try',
  8044. ' VI:=6;',
  8045. ' except',
  8046. ' on einvalidcast do ;',
  8047. ' end;',
  8048. '']);
  8049. ConvertProgram;
  8050. CheckSource('TestTryExcept',
  8051. LinesToStr([ // statements
  8052. 'rtl.createClass(this, "TObject", null, function () {',
  8053. ' this.$init = function () {',
  8054. ' };',
  8055. ' this.$final = function () {',
  8056. ' };',
  8057. '});',
  8058. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8059. ' this.$init = function () {',
  8060. ' $mod.TObject.$init.call(this);',
  8061. ' this.Msg = "";',
  8062. ' };',
  8063. '});',
  8064. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8065. '});',
  8066. 'this.vI = 0;'
  8067. ]),
  8068. LinesToStr([ // $mod.$main
  8069. 'try {',
  8070. ' $mod.vI = 1;',
  8071. '} catch ($e) {',
  8072. ' $mod.vI = 2;',
  8073. '};',
  8074. 'try {',
  8075. ' $mod.vI = 3;',
  8076. '} catch ($e) {',
  8077. ' throw $e;',
  8078. '};',
  8079. 'try {',
  8080. ' $mod.vI = 4;',
  8081. '} catch ($e) {',
  8082. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8083. ' throw $e',
  8084. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8085. ' var E = $e;',
  8086. ' if (E.Msg === "") throw E;',
  8087. ' } else {',
  8088. ' $mod.vI = 5;',
  8089. ' }',
  8090. '};',
  8091. 'try {',
  8092. ' $mod.vI = 6;',
  8093. '} catch ($e) {',
  8094. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8095. ' } else throw $e',
  8096. '};',
  8097. '']));
  8098. end;
  8099. procedure TTestModule.TestTryExcept_ReservedWords;
  8100. begin
  8101. StartProgram(false);
  8102. Add([
  8103. 'type',
  8104. ' TObject = class end;',
  8105. ' Exception = class',
  8106. ' Symbol: string;',
  8107. ' end;',
  8108. 'var &try: longint;',
  8109. 'begin',
  8110. ' try',
  8111. ' &try:=4;',
  8112. ' except',
  8113. ' on Error: exception do',
  8114. ' if errOR.symBol='''' then',
  8115. ' raise ERRor;',
  8116. ' end;',
  8117. '']);
  8118. ConvertProgram;
  8119. CheckSource('TestTryExcept_ReservedWords',
  8120. LinesToStr([ // statements
  8121. 'rtl.createClass(this, "TObject", null, function () {',
  8122. ' this.$init = function () {',
  8123. ' };',
  8124. ' this.$final = function () {',
  8125. ' };',
  8126. '});',
  8127. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8128. ' this.$init = function () {',
  8129. ' $mod.TObject.$init.call(this);',
  8130. ' this.Symbol = "";',
  8131. ' };',
  8132. '});',
  8133. 'this.Try = 0;',
  8134. '']),
  8135. LinesToStr([ // $mod.$main
  8136. 'try {',
  8137. ' $mod.Try = 4;',
  8138. '} catch ($e) {',
  8139. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8140. ' var error = $e;',
  8141. ' if (error.Symbol === "") throw error;',
  8142. ' } else throw $e',
  8143. '};',
  8144. '']));
  8145. end;
  8146. procedure TTestModule.TestIfThenRaiseElse;
  8147. begin
  8148. StartProgram(false);
  8149. Add([
  8150. 'type',
  8151. ' TObject = class',
  8152. ' constructor Create;',
  8153. ' end;',
  8154. 'constructor TObject.Create;',
  8155. 'begin',
  8156. 'end;',
  8157. 'var b: boolean;',
  8158. 'begin',
  8159. ' if b then',
  8160. ' raise TObject.Create',
  8161. ' else',
  8162. ' b:=false;',
  8163. '']);
  8164. ConvertProgram;
  8165. CheckSource('TestIfThenRaiseElse',
  8166. LinesToStr([ // statements
  8167. 'rtl.createClass(this, "TObject", null, function () {',
  8168. ' this.$init = function () {',
  8169. ' };',
  8170. ' this.$final = function () {',
  8171. ' };',
  8172. ' this.Create = function () {',
  8173. ' return this;',
  8174. ' };',
  8175. '});',
  8176. 'this.b = false;',
  8177. '']),
  8178. LinesToStr([ // $mod.$main
  8179. 'if ($mod.b) {',
  8180. ' throw $mod.TObject.$create("Create")}',
  8181. ' else $mod.b = false;',
  8182. '']));
  8183. end;
  8184. procedure TTestModule.TestCaseOf;
  8185. begin
  8186. StartProgram(false);
  8187. Add([
  8188. 'const e: longint; external name ''$e'';',
  8189. 'var vI: longint;',
  8190. 'begin',
  8191. ' case vi of',
  8192. ' 1: ;',
  8193. ' 2: vi:=3;',
  8194. ' e: ;',
  8195. ' else',
  8196. ' VI:=4',
  8197. ' end;']);
  8198. ConvertProgram;
  8199. CheckSource('TestCaseOf',
  8200. LinesToStr([ // statements
  8201. 'this.vI = 0;'
  8202. ]),
  8203. LinesToStr([ // $mod.$main
  8204. 'var $tmp = $mod.vI;',
  8205. 'if ($tmp === 1) {}',
  8206. 'else if ($tmp === 2) {',
  8207. ' $mod.vI = 3}',
  8208. ' else if ($tmp === $e) {}',
  8209. 'else {',
  8210. ' $mod.vI = 4;',
  8211. '};'
  8212. ]));
  8213. end;
  8214. procedure TTestModule.TestCaseOf_UseSwitch;
  8215. begin
  8216. StartProgram(false);
  8217. Converter.UseSwitchStatement:=true;
  8218. Add('var Vi: longint;');
  8219. Add('begin');
  8220. Add(' case vi of');
  8221. Add(' 1: ;');
  8222. Add(' 2: VI:=3;');
  8223. Add(' else');
  8224. Add(' vi:=4');
  8225. Add(' end;');
  8226. ConvertProgram;
  8227. CheckSource('TestCaseOf_UseSwitch',
  8228. LinesToStr([ // statements
  8229. 'this.Vi = 0;'
  8230. ]),
  8231. LinesToStr([ // $mod.$main
  8232. 'switch ($mod.Vi) {',
  8233. 'case 1:',
  8234. ' break;',
  8235. 'case 2:',
  8236. ' $mod.Vi = 3;',
  8237. ' break;',
  8238. 'default:',
  8239. ' $mod.Vi = 4;',
  8240. '};'
  8241. ]));
  8242. end;
  8243. procedure TTestModule.TestCaseOfNoElse;
  8244. begin
  8245. StartProgram(false);
  8246. Add('var Vi: longint;');
  8247. Add('begin');
  8248. Add(' case vi of');
  8249. Add(' 1: begin vi:=2; VI:=3; end;');
  8250. Add(' end;');
  8251. ConvertProgram;
  8252. CheckSource('TestCaseOfNoElse',
  8253. LinesToStr([ // statements
  8254. 'this.Vi = 0;'
  8255. ]),
  8256. LinesToStr([ // $mod.$main
  8257. 'var $tmp = $mod.Vi;',
  8258. 'if ($tmp === 1) {',
  8259. ' $mod.Vi = 2;',
  8260. ' $mod.Vi = 3;',
  8261. '};'
  8262. ]));
  8263. end;
  8264. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8265. begin
  8266. StartProgram(false);
  8267. Converter.UseSwitchStatement:=true;
  8268. Add('var vI: longint;');
  8269. Add('begin');
  8270. Add(' case vi of');
  8271. Add(' 1: begin VI:=2; vi:=3; end;');
  8272. Add(' end;');
  8273. ConvertProgram;
  8274. CheckSource('TestCaseOfNoElse_UseSwitch',
  8275. LinesToStr([ // statements
  8276. 'this.vI = 0;'
  8277. ]),
  8278. LinesToStr([ // $mod.$main
  8279. 'switch ($mod.vI) {',
  8280. 'case 1:',
  8281. ' $mod.vI = 2;',
  8282. ' $mod.vI = 3;',
  8283. ' break;',
  8284. '};'
  8285. ]));
  8286. end;
  8287. procedure TTestModule.TestCaseOfRange;
  8288. begin
  8289. StartProgram(false);
  8290. Add('var vI: longint;');
  8291. Add('begin');
  8292. Add(' case vi of');
  8293. Add(' 1..3: vi:=14;');
  8294. Add(' 4,5: vi:=16;');
  8295. Add(' 6..7,9..10: ;');
  8296. Add(' else ;');
  8297. Add(' end;');
  8298. ConvertProgram;
  8299. CheckSource('TestCaseOfRange',
  8300. LinesToStr([ // statements
  8301. 'this.vI = 0;'
  8302. ]),
  8303. LinesToStr([ // $mod.$main
  8304. 'var $tmp = $mod.vI;',
  8305. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8306. ' $mod.vI = 14',
  8307. '} else if (($tmp === 4) || ($tmp === 5)){',
  8308. ' $mod.vI = 16',
  8309. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8310. ]));
  8311. end;
  8312. procedure TTestModule.TestCaseOfString;
  8313. begin
  8314. StartProgram(false);
  8315. Add([
  8316. 'var s,h: string;',
  8317. 'begin',
  8318. ' case s of',
  8319. ' ''foo'': s:=h;',
  8320. ' ''a''..''z'': h:=s;',
  8321. ' ''ў'', ''ё'': ;',
  8322. ' ''Б''..''Я'': ;',
  8323. ' end;',
  8324. '']);
  8325. ConvertProgram;
  8326. CheckSource('TestCaseOfString',
  8327. LinesToStr([ // statements
  8328. 'this.s = "";',
  8329. 'this.h = "";',
  8330. '']),
  8331. LinesToStr([ // $mod.$main
  8332. 'var $tmp = $mod.s;',
  8333. 'if ($tmp === "foo") {',
  8334. ' $mod.s = $mod.h}',
  8335. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8336. ' $mod.h = $mod.s}',
  8337. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8338. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8339. '']));
  8340. end;
  8341. procedure TTestModule.TestCaseOfChar;
  8342. begin
  8343. StartProgram(false);
  8344. Add([
  8345. 'var s,h: char;',
  8346. 'begin',
  8347. ' case s of',
  8348. ' ''a''..''z'': h:=s;',
  8349. ' ''ä'': ;',
  8350. ' ''ў'', ''ё'': ;',
  8351. ' ''Б''..''Я'': ;',
  8352. ' end;',
  8353. '']);
  8354. ConvertProgram;
  8355. CheckSource('TestCaseOfString',
  8356. LinesToStr([ // statements
  8357. 'this.s = "";',
  8358. 'this.h = "";',
  8359. '']),
  8360. LinesToStr([ // $mod.$main
  8361. 'var $tmp = $mod.s;',
  8362. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8363. ' $mod.h = $mod.s}',
  8364. ' else if ($tmp === "ä") {}',
  8365. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8366. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8367. '']));
  8368. end;
  8369. procedure TTestModule.TestCaseOfExternalClassConst;
  8370. begin
  8371. StartProgram(false);
  8372. Add([
  8373. '{$modeswitch externalclass}',
  8374. 'type',
  8375. ' TBird = class external name ''Bird''',
  8376. ' const e: longint;',
  8377. ' end;',
  8378. 'var vI: longint;',
  8379. 'begin',
  8380. ' case vi of',
  8381. ' 1: vi:=3;',
  8382. ' TBird.e: ;',
  8383. ' end;']);
  8384. ConvertProgram;
  8385. CheckSource('TestCaseOfExternalClassConst',
  8386. LinesToStr([ // statements
  8387. 'this.vI = 0;'
  8388. ]),
  8389. LinesToStr([ // $mod.$main
  8390. 'var $tmp = $mod.vI;',
  8391. 'if ($tmp === 1) {',
  8392. ' $mod.vI = 3}',
  8393. ' else if ($tmp === Bird.e) ;'
  8394. ]));
  8395. end;
  8396. procedure TTestModule.TestDebugger;
  8397. begin
  8398. StartProgram(false);
  8399. Add([
  8400. 'procedure DoIt;',
  8401. 'begin',
  8402. ' deBugger;',
  8403. ' DeBugger();',
  8404. 'end;',
  8405. 'begin',
  8406. ' Debugger;']);
  8407. ConvertProgram;
  8408. CheckSource('TestDebugger',
  8409. LinesToStr([ // statements
  8410. 'this.DoIt = function () {',
  8411. ' debugger;',
  8412. ' debugger;',
  8413. '};',
  8414. '']),
  8415. LinesToStr([ // $mod.$main
  8416. 'debugger;',
  8417. '']));
  8418. end;
  8419. procedure TTestModule.TestArray_Dynamic;
  8420. begin
  8421. StartProgram(false);
  8422. Add([
  8423. 'type',
  8424. ' TArrayInt = array of longint;',
  8425. 'var',
  8426. ' Arr: TArrayInt;',
  8427. ' i: longint;',
  8428. ' b: boolean;',
  8429. 'begin',
  8430. ' SetLength(arr,3);',
  8431. ' arr[0]:=4;',
  8432. ' arr[1]:=length(arr)+arr[0];',
  8433. ' arr[i]:=5;',
  8434. ' arr[arr[i]]:=arr[6];',
  8435. ' i:=low(arr);',
  8436. ' i:=high(arr);',
  8437. ' b:=Assigned(arr);',
  8438. ' Arr:=default(TArrayInt);']);
  8439. ConvertProgram;
  8440. CheckSource('TestArray_Dynamic',
  8441. LinesToStr([ // statements
  8442. 'this.Arr = [];',
  8443. 'this.i = 0;',
  8444. 'this.b = false;'
  8445. ]),
  8446. LinesToStr([ // $mod.$main
  8447. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8448. '$mod.Arr[0] = 4;',
  8449. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8450. '$mod.Arr[$mod.i] = 5;',
  8451. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8452. '$mod.i = 0;',
  8453. '$mod.i = rtl.length($mod.Arr) - 1;',
  8454. '$mod.b = rtl.length($mod.Arr) > 0;',
  8455. '$mod.Arr = [];',
  8456. '']));
  8457. end;
  8458. procedure TTestModule.TestArray_Dynamic_Nil;
  8459. begin
  8460. StartProgram(false);
  8461. Add('type');
  8462. Add(' TArrayInt = array of longint;');
  8463. Add('var');
  8464. Add(' Arr: TArrayInt;');
  8465. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8466. Add('begin');
  8467. Add(' arr:=nil;');
  8468. Add(' if arr=nil then;');
  8469. Add(' if nil=arr then;');
  8470. Add(' if arr<>nil then;');
  8471. Add(' if nil<>arr then;');
  8472. Add(' DoIt(nil,nil);');
  8473. ConvertProgram;
  8474. CheckSource('TestArray_Dynamic',
  8475. LinesToStr([ // statements
  8476. 'this.Arr = [];',
  8477. 'this.DoIt = function(i,j){',
  8478. '};'
  8479. ]),
  8480. LinesToStr([ // $mod.$main
  8481. '$mod.Arr = [];',
  8482. 'if (rtl.length($mod.Arr) === 0) ;',
  8483. 'if (rtl.length($mod.Arr) === 0) ;',
  8484. 'if (rtl.length($mod.Arr) > 0) ;',
  8485. 'if (rtl.length($mod.Arr) > 0) ;',
  8486. '$mod.DoIt([],[]);',
  8487. '']));
  8488. end;
  8489. procedure TTestModule.TestArray_DynMultiDimensional;
  8490. begin
  8491. StartProgram(false);
  8492. Add([
  8493. 'type',
  8494. ' TArrayInt = array of longint;',
  8495. ' TArrayArrayInt = array of TArrayInt;',
  8496. 'var',
  8497. ' Arr: TArrayInt;',
  8498. ' Arr2: TArrayArrayInt;',
  8499. ' i: longint;',
  8500. 'begin',
  8501. ' arr2:=nil;',
  8502. ' if arr2=nil then;',
  8503. ' if nil=arr2 then;',
  8504. ' i:=low(arr2);',
  8505. ' i:=low(arr2[1]);',
  8506. ' i:=high(arr2);',
  8507. ' i:=high(arr2[2]);',
  8508. ' arr2[3]:=arr;',
  8509. ' arr2[4][5]:=i;',
  8510. ' i:=arr2[6][7];',
  8511. ' arr2[8,9]:=i;',
  8512. ' i:=arr2[10,11];',
  8513. ' SetLength(arr2,14);',
  8514. ' SetLength(arr2[15],16);']);
  8515. ConvertProgram;
  8516. CheckSource('TestArray_Dynamic',
  8517. LinesToStr([ // statements
  8518. 'this.Arr = [];',
  8519. 'this.Arr2 = [];',
  8520. 'this.i = 0;'
  8521. ]),
  8522. LinesToStr([ // $mod.$main
  8523. '$mod.Arr2 = [];',
  8524. 'if (rtl.length($mod.Arr2) === 0) ;',
  8525. 'if (rtl.length($mod.Arr2) === 0) ;',
  8526. '$mod.i = 0;',
  8527. '$mod.i = 0;',
  8528. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8529. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8530. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8531. '$mod.Arr2[4][5] = $mod.i;',
  8532. '$mod.i = $mod.Arr2[6][7];',
  8533. '$mod.Arr2[8][9] = $mod.i;',
  8534. '$mod.i = $mod.Arr2[10][11];',
  8535. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8536. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8537. '']));
  8538. end;
  8539. procedure TTestModule.TestArray_DynamicAssign;
  8540. begin
  8541. StartProgram(false);
  8542. Add([
  8543. 'type',
  8544. ' TArrayInt = array of longint;',
  8545. ' TArrayArrayInt = array of TArrayInt;',
  8546. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8547. 'begin',
  8548. 'end;',
  8549. 'procedure Fly(var a: TArrayInt);',
  8550. 'begin',
  8551. 'end;',
  8552. 'var',
  8553. ' Arr: TArrayInt;',
  8554. ' Arr2: TArrayArrayInt;',
  8555. 'begin',
  8556. ' arr:=nil;',
  8557. ' arr2:=nil;',
  8558. ' arr2[1]:=nil;',
  8559. ' arr2[2]:=arr;',
  8560. ' Run(arr,arr,arr);',
  8561. ' Fly(arr);',
  8562. ' Run(arr2[4],arr2[5],arr2[6]);',
  8563. ' Fly(arr2[7]);',
  8564. '']);
  8565. ConvertProgram;
  8566. CheckSource('TestArray_DynamicAssign',
  8567. LinesToStr([ // statements
  8568. 'this.Run = function (a, b, c) {',
  8569. '};',
  8570. 'this.Fly = function (a) {',
  8571. '};',
  8572. 'this.Arr = [];',
  8573. 'this.Arr2 = [];',
  8574. '']),
  8575. LinesToStr([ // $mod.$main
  8576. '$mod.Arr = [];',
  8577. '$mod.Arr2 = [];',
  8578. '$mod.Arr2[1] = [];',
  8579. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8580. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8581. '$mod.Fly({',
  8582. ' p: $mod,',
  8583. ' get: function () {',
  8584. ' return this.p.Arr;',
  8585. ' },',
  8586. ' set: function (v) {',
  8587. ' this.p.Arr = v;',
  8588. ' }',
  8589. '});',
  8590. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8591. '$mod.Fly({',
  8592. ' a: 7,',
  8593. ' p: $mod.Arr2,',
  8594. ' get: function () {',
  8595. ' return this.p[this.a];',
  8596. ' },',
  8597. ' set: function (v) {',
  8598. ' this.p[this.a] = v;',
  8599. ' }',
  8600. '});',
  8601. '']));
  8602. end;
  8603. procedure TTestModule.TestArray_StaticInt;
  8604. begin
  8605. StartProgram(false);
  8606. Add('type');
  8607. Add(' TArrayInt = array[2..4] of longint;');
  8608. Add('var');
  8609. Add(' Arr: TArrayInt;');
  8610. Add(' Arr2: TArrayInt = (5,6,7);');
  8611. Add(' i: longint;');
  8612. Add(' b: boolean;');
  8613. Add('begin');
  8614. Add(' arr[2]:=4;');
  8615. Add(' arr[3]:=arr[2]+arr[3];');
  8616. Add(' arr[i]:=5;');
  8617. Add(' arr[arr[i]]:=arr[high(arr)];');
  8618. Add(' i:=low(arr);');
  8619. Add(' i:=high(arr);');
  8620. Add(' b:=arr[2]=arr[3];');
  8621. Add(' arr:=default(TArrayInt);');
  8622. ConvertProgram;
  8623. CheckSource('TestArray_StaticInt',
  8624. LinesToStr([ // statements
  8625. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8626. 'this.Arr2 = [5, 6, 7];',
  8627. 'this.i = 0;',
  8628. 'this.b = false;'
  8629. ]),
  8630. LinesToStr([ // $mod.$main
  8631. '$mod.Arr[0] = 4;',
  8632. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8633. '$mod.Arr[$mod.i-2] = 5;',
  8634. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8635. '$mod.i = 2;',
  8636. '$mod.i = 4;',
  8637. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8638. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8639. '']));
  8640. end;
  8641. procedure TTestModule.TestArray_StaticBool;
  8642. begin
  8643. StartProgram(false);
  8644. Add('type');
  8645. Add(' TBools = array[boolean] of boolean;');
  8646. Add(' TBool2 = array[true..true] of boolean;');
  8647. Add('var');
  8648. Add(' Arr: TBools;');
  8649. Add(' Arr2: TBool2;');
  8650. Add(' Arr3: TBools = (true,false);');
  8651. Add(' b: boolean;');
  8652. Add('begin');
  8653. Add(' b:=low(arr);');
  8654. Add(' b:=high(arr);');
  8655. Add(' arr[true]:=false;');
  8656. Add(' arr[false]:=arr[b] or arr[true];');
  8657. Add(' arr[b]:=true;');
  8658. Add(' arr[arr[b]]:=arr[high(arr)];');
  8659. Add(' b:=arr[false]=arr[true];');
  8660. Add(' b:=low(arr2);');
  8661. Add(' b:=high(arr2);');
  8662. Add(' arr2[true]:=true;');
  8663. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8664. Add(' arr2[b]:=false;');
  8665. ConvertProgram;
  8666. CheckSource('TestArray_StaticBool',
  8667. LinesToStr([ // statements
  8668. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8669. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8670. 'this.Arr3 = [true, false];',
  8671. 'this.b = false;'
  8672. ]),
  8673. LinesToStr([ // $mod.$main
  8674. '$mod.b = false;',
  8675. '$mod.b = true;',
  8676. '$mod.Arr[1] = false;',
  8677. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8678. '$mod.Arr[+$mod.b] = true;',
  8679. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8680. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8681. '$mod.b = true;',
  8682. '$mod.b = true;',
  8683. '$mod.Arr2[0] = true;',
  8684. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8685. '$mod.Arr2[1-$mod.b] = false;',
  8686. '']));
  8687. end;
  8688. procedure TTestModule.TestArray_StaticChar;
  8689. begin
  8690. StartProgram(false);
  8691. Add([
  8692. 'type',
  8693. ' TChars = array[char] of char;',
  8694. ' TChars2 = array[''a''..''z''] of char;',
  8695. 'var',
  8696. ' Arr: TChars;',
  8697. ' Arr2: TChars2;',
  8698. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8699. ' Arr4: array[11..13] of char = ''pas'';',
  8700. ' Arr5: array[21..22] of char = ''äö'';',
  8701. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8702. ' c: char;',
  8703. ' b: boolean;',
  8704. 'begin',
  8705. ' c:=low(arr);',
  8706. ' c:=high(arr);',
  8707. ' arr[''B'']:=''a'';',
  8708. ' arr[''D'']:=arr[c];',
  8709. ' arr[c]:=arr[''d''];',
  8710. ' arr[arr[c]]:=arr[high(arr)];',
  8711. ' b:=arr[low(arr)]=arr[''e''];',
  8712. ' c:=low(arr2);',
  8713. ' c:=high(arr2);',
  8714. ' arr2[''b'']:=''f'';',
  8715. ' arr2[''a'']:=arr2[c];',
  8716. ' arr2[c]:=arr2[''g''];']);
  8717. ConvertProgram;
  8718. CheckSource('TestArray_StaticChar',
  8719. LinesToStr([ // statements
  8720. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8721. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8722. 'this.Arr3 = ["p", "a", "s"];',
  8723. 'this.Arr4 = ["p", "a", "s"];',
  8724. 'this.Arr5 = ["ä", "ö"];',
  8725. 'this.Arr6 = ["ä", "ö"];',
  8726. 'this.c = "";',
  8727. 'this.b = false;',
  8728. '']),
  8729. LinesToStr([ // $mod.$main
  8730. '$mod.c = "\x00";',
  8731. '$mod.c = "\uFFFF";',
  8732. '$mod.Arr[66] = "a";',
  8733. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8734. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8735. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8736. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8737. '$mod.c = "a";',
  8738. '$mod.c = "z";',
  8739. '$mod.Arr2[1] = "f";',
  8740. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8741. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8742. '']));
  8743. end;
  8744. procedure TTestModule.TestArray_StaticMultiDim;
  8745. begin
  8746. StartProgram(false);
  8747. Add([
  8748. 'type',
  8749. ' TArrayInt = array[1..3] of longint;',
  8750. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8751. 'var',
  8752. ' Arr: TArrayInt;',
  8753. ' Arr2: TArrayArrayInt;',
  8754. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8755. ' i: longint;',
  8756. 'begin',
  8757. ' i:=low(arr);',
  8758. ' i:=low(arr2);',
  8759. ' i:=low(arr2[5]);',
  8760. ' i:=high(arr);',
  8761. ' i:=high(arr2);',
  8762. ' i:=high(arr2[6]);',
  8763. ' arr2[5]:=arr;',
  8764. ' arr2[6][2]:=i;',
  8765. ' i:=arr2[6][3];',
  8766. ' arr2[6,3]:=i;',
  8767. ' i:=arr2[5,2];',
  8768. ' arr2:=arr2;',// clone multi dim static array
  8769. //' arr3:=arr3;',// clone anonymous multi dim static array
  8770. '']);
  8771. ConvertProgram;
  8772. CheckSource('TestArray_StaticMultiDim',
  8773. LinesToStr([ // statements
  8774. 'this.TArrayArrayInt$clone = function (a) {',
  8775. ' var r = [];',
  8776. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8777. ' return r;',
  8778. '};',
  8779. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8780. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8781. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8782. 'this.i = 0;'
  8783. ]),
  8784. LinesToStr([ // $mod.$main
  8785. '$mod.i = 1;',
  8786. '$mod.i = 5;',
  8787. '$mod.i = 1;',
  8788. '$mod.i = 3;',
  8789. '$mod.i = 6;',
  8790. '$mod.i = 3;',
  8791. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8792. '$mod.Arr2[1][1] = $mod.i;',
  8793. '$mod.i = $mod.Arr2[1][2];',
  8794. '$mod.Arr2[1][2] = $mod.i;',
  8795. '$mod.i = $mod.Arr2[0][1];',
  8796. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8797. '']));
  8798. end;
  8799. procedure TTestModule.TestArray_StaticInFunction;
  8800. begin
  8801. StartProgram(false);
  8802. Add([
  8803. 'const TArrayInt = 3;',
  8804. 'const TArrayArrayInt = 4;',
  8805. 'procedure DoIt;',
  8806. 'type',
  8807. ' TArrayInt = array[1..3] of longint;',
  8808. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8809. 'var',
  8810. ' Arr: TArrayInt;',
  8811. ' Arr2: TArrayArrayInt;',
  8812. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8813. ' i: longint;',
  8814. 'begin',
  8815. ' arr2[5]:=arr;',
  8816. ' arr2:=arr2;',// clone multi dim static array
  8817. 'end;',
  8818. 'begin',
  8819. '']);
  8820. ConvertProgram;
  8821. CheckSource('TestArray_StaticInFunction',
  8822. LinesToStr([ // statements
  8823. 'this.TArrayInt = 3;',
  8824. 'this.TArrayArrayInt = 4;',
  8825. 'var TArrayArrayInt$1$clone = function (a) {',
  8826. ' var r = [];',
  8827. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8828. ' return r;',
  8829. '};',
  8830. 'this.DoIt = function () {',
  8831. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8832. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8833. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8834. ' var i = 0;',
  8835. ' Arr2[0] = Arr.slice(0);',
  8836. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8837. '};',
  8838. '']),
  8839. LinesToStr([ // $mod.$main
  8840. '']));
  8841. end;
  8842. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8843. begin
  8844. StartProgram(false);
  8845. Add([
  8846. 'type',
  8847. ' TArrayInt = array[1..3,1..2] of longint;',
  8848. 'var',
  8849. ' a,b: TArrayInt;',
  8850. 'begin',
  8851. ' if a=b then ;',
  8852. '']);
  8853. SetExpectedPasResolverError('compare static array is not supported',
  8854. nXIsNotSupported);
  8855. ConvertProgram;
  8856. end;
  8857. procedure TTestModule.TestArrayOfRecord;
  8858. begin
  8859. StartProgram(false);
  8860. Add([
  8861. 'type',
  8862. ' TRec = record',
  8863. ' Int: longint;',
  8864. ' end;',
  8865. ' TArrayRec = array of TRec;',
  8866. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8867. 'begin',
  8868. 'end;',
  8869. 'var',
  8870. ' Arr: TArrayRec;',
  8871. ' r: TRec;',
  8872. ' i: longint;',
  8873. 'begin',
  8874. ' SetLength(arr,3);',
  8875. ' arr[0].int:=4;',
  8876. ' arr[1].int:=length(arr)+arr[2].int;',
  8877. ' arr[arr[i].int].int:=arr[5].int;',
  8878. ' arr[7]:=r;',
  8879. ' r:=arr[8];',
  8880. ' i:=low(arr);',
  8881. ' i:=high(arr);',
  8882. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8883. ConvertProgram;
  8884. CheckSource('TestArrayOfRecord',
  8885. LinesToStr([ // statements
  8886. 'rtl.recNewT(this, "TRec", function () {',
  8887. ' this.Int = 0;',
  8888. ' this.$eq = function (b) {',
  8889. ' return this.Int === b.Int;',
  8890. ' };',
  8891. ' this.$assign = function (s) {',
  8892. ' this.Int = s.Int;',
  8893. ' return this;',
  8894. ' };',
  8895. '});',
  8896. 'this.DoIt = function (vd, vc, vv) {',
  8897. '};',
  8898. 'this.Arr = [];',
  8899. 'this.r = this.TRec.$new();',
  8900. 'this.i = 0;'
  8901. ]),
  8902. LinesToStr([ // $mod.$main
  8903. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8904. '$mod.Arr[0].Int = 4;',
  8905. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8906. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8907. '$mod.Arr[7].$assign($mod.r);',
  8908. '$mod.r.$assign($mod.Arr[8]);',
  8909. '$mod.i = 0;',
  8910. '$mod.i = rtl.length($mod.Arr)-1;',
  8911. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8912. '']));
  8913. end;
  8914. procedure TTestModule.TestArray_StaticRecord;
  8915. begin
  8916. StartProgram(false);
  8917. Add([
  8918. 'type',
  8919. ' TRec = record',
  8920. ' Int: longint;',
  8921. ' end;',
  8922. ' TArrayRec = array[1..2] of TRec;',
  8923. 'var',
  8924. ' Arr: TArrayRec;',
  8925. 'begin',
  8926. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8927. '']);
  8928. ConvertProgram;
  8929. CheckSource('TestArray_StaticRecord',
  8930. LinesToStr([ // statements
  8931. 'rtl.recNewT(this, "TRec", function () {',
  8932. ' this.Int = 0;',
  8933. ' this.$eq = function (b) {',
  8934. ' return this.Int === b.Int;',
  8935. ' };',
  8936. ' this.$assign = function (s) {',
  8937. ' this.Int = s.Int;',
  8938. ' return this;',
  8939. ' };',
  8940. '});',
  8941. 'this.TArrayRec$clone = function (a) {',
  8942. ' var r = [];',
  8943. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8944. ' return r;',
  8945. '};',
  8946. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  8947. '']),
  8948. LinesToStr([ // $mod.$main
  8949. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8950. end;
  8951. procedure TTestModule.TestArrayOfSet;
  8952. begin
  8953. StartProgram(false);
  8954. Add([
  8955. 'type',
  8956. ' TFlag = (big,small);',
  8957. ' TSetOfFlag = set of tflag;',
  8958. ' TArrayFlag = array of TSetOfFlag;',
  8959. 'procedure DoIt(const a: Tarrayflag);',
  8960. 'begin',
  8961. 'end;',
  8962. 'var',
  8963. ' f: TFlag;',
  8964. ' s: TSetOfFlag;',
  8965. ' Arr: TArrayFlag;',
  8966. ' i: longint;',
  8967. 'begin',
  8968. ' SetLength(arr,3);',
  8969. ' arr[0]:=s;',
  8970. ' arr[1]:=[big];',
  8971. ' arr[2]:=[big]+s;',
  8972. ' arr[3]:=s+[big];',
  8973. ' arr[4]:=arr[5];',
  8974. ' s:=arr[6];',
  8975. ' i:=low(arr);',
  8976. ' i:=high(arr);',
  8977. ' DoIt(arr);',
  8978. ' DoIt([s]);',
  8979. ' DoIt([[],s]);',
  8980. ' DoIt([s,[]]);',
  8981. '']);
  8982. ConvertProgram;
  8983. CheckSource('TestArrayOfSet',
  8984. LinesToStr([ // statements
  8985. 'this.TFlag = {',
  8986. ' "0": "big",',
  8987. ' big: 0,',
  8988. ' "1": "small",',
  8989. ' small: 1',
  8990. '};',
  8991. 'this.DoIt = function (a) {',
  8992. '};',
  8993. 'this.f = 0;',
  8994. 'this.s = {};',
  8995. 'this.Arr = [];',
  8996. 'this.i = 0;',
  8997. '']),
  8998. LinesToStr([ // $mod.$main
  8999. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9000. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9001. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9002. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9003. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9004. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9005. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9006. '$mod.i = 0;',
  9007. '$mod.i = rtl.length($mod.Arr) - 1;',
  9008. '$mod.DoIt($mod.Arr);',
  9009. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9010. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9011. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9012. '']));
  9013. end;
  9014. procedure TTestModule.TestArray_DynAsParam;
  9015. begin
  9016. StartProgram(false);
  9017. Add([
  9018. 'type integer = longint;',
  9019. 'type TArrInt = array of integer;',
  9020. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9021. 'var vJ: TArrInt;',
  9022. 'begin',
  9023. ' vg:=vg;',
  9024. ' vj:=vh;',
  9025. ' vi:=vi;',
  9026. ' doit(vg,vg,vg);',
  9027. ' doit(vh,vh,vj);',
  9028. ' doit(vi,vi,vi);',
  9029. ' doit(vj,vj,vj);',
  9030. 'end;',
  9031. 'var i: TArrInt;',
  9032. 'begin',
  9033. ' doit(i,i,i);']);
  9034. ConvertProgram;
  9035. CheckSource('TestArray_DynAsParams',
  9036. LinesToStr([ // statements
  9037. 'this.DoIt = function (vG,vH,vI) {',
  9038. ' var vJ = [];',
  9039. ' vG = rtl.arrayRef(vG);',
  9040. ' vJ = rtl.arrayRef(vH);',
  9041. ' vI.set(rtl.arrayRef(vI.get()));',
  9042. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9043. ' get: function () {',
  9044. ' return vG;',
  9045. ' },',
  9046. ' set: function (v) {',
  9047. ' vG = v;',
  9048. ' }',
  9049. ' });',
  9050. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9051. ' get: function () {',
  9052. ' return vJ;',
  9053. ' },',
  9054. ' set: function (v) {',
  9055. ' vJ = v;',
  9056. ' }',
  9057. ' });',
  9058. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9059. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9060. ' get: function () {',
  9061. ' return vJ;',
  9062. ' },',
  9063. ' set: function (v) {',
  9064. ' vJ = v;',
  9065. ' }',
  9066. ' });',
  9067. '};',
  9068. 'this.i = [];'
  9069. ]),
  9070. LinesToStr([
  9071. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9072. ' p: $mod,',
  9073. ' get: function () {',
  9074. ' return this.p.i;',
  9075. ' },',
  9076. ' set: function (v) {',
  9077. ' this.p.i = v;',
  9078. ' }',
  9079. '});'
  9080. ]));
  9081. end;
  9082. procedure TTestModule.TestArray_StaticAsParam;
  9083. begin
  9084. StartProgram(false);
  9085. Add([
  9086. 'type integer = longint;',
  9087. 'type TArrInt = array[1..2] of integer;',
  9088. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9089. 'var vJ: TArrInt;',
  9090. 'begin',
  9091. ' vg:=vg;',
  9092. ' vj:=vh;',
  9093. ' vi:=vi;',
  9094. ' doit(vg,vg,vg);',
  9095. ' doit(vh,vh,vj);',
  9096. ' doit(vi,vi,vi);',
  9097. ' doit(vj,vj,vj);',
  9098. 'end;',
  9099. 'var i: TArrInt;',
  9100. 'begin',
  9101. ' doit(i,i,i);']);
  9102. ConvertProgram;
  9103. CheckSource('TestArray_StaticAsParams',
  9104. LinesToStr([ // statements
  9105. 'this.DoIt = function (vG,vH,vI) {',
  9106. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9107. ' vG = vG.slice(0);',
  9108. ' vJ = vH.slice(0);',
  9109. ' vI.set(vI.get().slice(0));',
  9110. ' $mod.DoIt(vG.slice(0), vG, {',
  9111. ' get: function () {',
  9112. ' return vG;',
  9113. ' },',
  9114. ' set: function (v) {',
  9115. ' vG = v;',
  9116. ' }',
  9117. ' });',
  9118. ' $mod.DoIt(vH.slice(0), vH, {',
  9119. ' get: function () {',
  9120. ' return vJ;',
  9121. ' },',
  9122. ' set: function (v) {',
  9123. ' vJ = v;',
  9124. ' }',
  9125. ' });',
  9126. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9127. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9128. ' get: function () {',
  9129. ' return vJ;',
  9130. ' },',
  9131. ' set: function (v) {',
  9132. ' vJ = v;',
  9133. ' }',
  9134. ' });',
  9135. '};',
  9136. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9137. ]),
  9138. LinesToStr([
  9139. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9140. ' p: $mod,',
  9141. ' get: function () {',
  9142. ' return this.p.i;',
  9143. ' },',
  9144. ' set: function (v) {',
  9145. ' this.p.i = v;',
  9146. ' }',
  9147. '});'
  9148. ]));
  9149. end;
  9150. procedure TTestModule.TestArrayElement_AsParams;
  9151. begin
  9152. StartProgram(false);
  9153. Add('type integer = longint;');
  9154. Add('type TArrayInt = array of integer;');
  9155. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9156. Add('var vJ: tarrayint;');
  9157. Add('begin');
  9158. Add(' vi:=vi;');
  9159. Add(' doit(vi,vi,vi);');
  9160. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9161. Add('end;');
  9162. Add('var a: TArrayInt;');
  9163. Add('begin');
  9164. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9165. ConvertProgram;
  9166. CheckSource('TestArrayElement_AsParams',
  9167. LinesToStr([ // statements
  9168. 'this.DoIt = function (vG,vH,vI) {',
  9169. ' var vJ = [];',
  9170. ' vI.set(vI.get());',
  9171. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9172. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9173. ' a:1+3,',
  9174. ' p:vJ,',
  9175. ' get: function () {',
  9176. ' return this.p[this.a];',
  9177. ' },',
  9178. ' set: function (v) {',
  9179. ' this.p[this.a] = v;',
  9180. ' }',
  9181. ' });',
  9182. '};',
  9183. 'this.a = [];'
  9184. ]),
  9185. LinesToStr([
  9186. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9187. ' a: 1+6,',
  9188. ' p: $mod.a,',
  9189. ' get: function () {',
  9190. ' return this.p[this.a];',
  9191. ' },',
  9192. ' set: function (v) {',
  9193. ' this.p[this.a] = v;',
  9194. ' }',
  9195. '});'
  9196. ]));
  9197. end;
  9198. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9199. begin
  9200. StartProgram(false);
  9201. Add('type Integer = longint;');
  9202. Add('type TArrayInt = array of integer;');
  9203. Add('function GetArr(vB: integer = 0): tarrayint;');
  9204. Add('begin');
  9205. Add('end;');
  9206. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9207. Add('begin');
  9208. Add('end;');
  9209. Add('begin');
  9210. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9211. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9212. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9213. ConvertProgram;
  9214. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9215. LinesToStr([ // statements
  9216. 'this.GetArr = function (vB) {',
  9217. ' var Result = [];',
  9218. ' return Result;',
  9219. '};',
  9220. 'this.DoIt = function (vG,vH,vI) {',
  9221. '};'
  9222. ]),
  9223. LinesToStr([
  9224. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9225. ' a: 1+3,',
  9226. ' p: $mod.GetArr(0),',
  9227. ' get: function () {',
  9228. ' return this.p[this.a];',
  9229. ' },',
  9230. ' set: function (v) {',
  9231. ' this.p[this.a] = v;',
  9232. ' }',
  9233. '});',
  9234. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9235. ' a: 2+3,',
  9236. ' p: $mod.GetArr(0),',
  9237. ' get: function () {',
  9238. ' return this.p[this.a];',
  9239. ' },',
  9240. ' set: function (v) {',
  9241. ' this.p[this.a] = v;',
  9242. ' }',
  9243. '});',
  9244. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9245. ' a: 3+3,',
  9246. ' p: $mod.GetArr(9),',
  9247. ' get: function () {',
  9248. ' return this.p[this.a];',
  9249. ' },',
  9250. ' set: function (v) {',
  9251. ' this.p[this.a] = v;',
  9252. ' }',
  9253. '});',
  9254. '']));
  9255. end;
  9256. procedure TTestModule.TestArrayEnumTypeRange;
  9257. begin
  9258. StartProgram(false);
  9259. Add([
  9260. 'type',
  9261. ' TEnum = (red,blue);',
  9262. ' TEnumArray = array[TEnum] of longint;',
  9263. 'var',
  9264. ' e: TEnum;',
  9265. ' i: longint;',
  9266. ' a: TEnumArray;',
  9267. ' numbers: TEnumArray = (1,2);',
  9268. ' names: array[TEnum] of string = (''red'',''blue'');',
  9269. 'begin',
  9270. ' e:=low(a);',
  9271. ' e:=high(a);',
  9272. ' i:=a[red];',
  9273. ' a[e]:=a[e];']);
  9274. ConvertProgram;
  9275. CheckSource('TestArrayEnumTypeRange',
  9276. LinesToStr([ // statements
  9277. ' this.TEnum = {',
  9278. ' "0": "red",',
  9279. ' red: 0,',
  9280. ' "1": "blue",',
  9281. ' blue: 1',
  9282. '};',
  9283. 'this.e = 0;',
  9284. 'this.i = 0;',
  9285. 'this.a = rtl.arraySetLength(null,0,2);',
  9286. 'this.numbers = [1, 2];',
  9287. 'this.names = ["red", "blue"];',
  9288. '']),
  9289. LinesToStr([ // $mod.$main
  9290. '$mod.e = $mod.TEnum.red;',
  9291. '$mod.e = $mod.TEnum.blue;',
  9292. '$mod.i = $mod.a[$mod.TEnum.red];',
  9293. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9294. '']));
  9295. end;
  9296. procedure TTestModule.TestArray_SetLengthOutArg;
  9297. begin
  9298. StartProgram(false);
  9299. Add([
  9300. 'type TArrInt = array of longint;',
  9301. 'procedure DoIt(out a: TArrInt);',
  9302. 'begin',
  9303. ' SetLength(a,2);',
  9304. 'end;',
  9305. 'begin',
  9306. '']);
  9307. ConvertProgram;
  9308. CheckSource('TestArray_SetLengthOutArg',
  9309. LinesToStr([ // statements
  9310. 'this.DoIt = function (a) {',
  9311. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9312. '};',
  9313. '']),
  9314. LinesToStr([
  9315. '']));
  9316. end;
  9317. procedure TTestModule.TestArray_SetLengthProperty;
  9318. begin
  9319. StartProgram(false);
  9320. Add('type');
  9321. Add(' TArrInt = array of longint;');
  9322. Add(' TObject = class');
  9323. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9324. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9325. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9326. Add(' end;');
  9327. Add('var Obj: TObject;');
  9328. Add('begin');
  9329. Add(' SetLength(Obj.Colors,2);');
  9330. ConvertProgram;
  9331. CheckSource('TestArray_SetLengthProperty',
  9332. LinesToStr([ // statements
  9333. 'rtl.createClass(this, "TObject", null, function () {',
  9334. ' this.$init = function () {',
  9335. ' };',
  9336. ' this.$final = function () {',
  9337. ' };',
  9338. '});',
  9339. 'this.Obj = null;',
  9340. '']),
  9341. LinesToStr([
  9342. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9343. '']));
  9344. end;
  9345. procedure TTestModule.TestArray_SetLengthMultiDim;
  9346. begin
  9347. StartProgram(false);
  9348. Add([
  9349. 'type',
  9350. ' TArrArrInt = array of array of longint;',
  9351. ' TArrStaInt = array of array[1..2] of longint;',
  9352. 'var',
  9353. ' a: TArrArrInt;',
  9354. ' b: TArrStaInt;',
  9355. 'begin',
  9356. ' SetLength(a,2);',
  9357. ' SetLength(a,3,4);',
  9358. ' SetLength(b,5);',
  9359. '']);
  9360. ConvertProgram;
  9361. CheckSource('TestArray_SetLengthMultiDim',
  9362. LinesToStr([ // statements
  9363. 'this.a = [];',
  9364. 'this.b = [];',
  9365. '']),
  9366. LinesToStr([
  9367. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9368. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9369. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9370. '']));
  9371. end;
  9372. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9373. begin
  9374. StartProgram(false);
  9375. Add([
  9376. 'type',
  9377. ' TStaArr1 = array[1..3] of boolean;',
  9378. //' TStaArr2 = array[5..6] of TStaArr1;',
  9379. ' TDynArr1StaArr1 = array of TStaArr1;',
  9380. //' TDynArr1StaArr2 = array of TStaArr2;',
  9381. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9382. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9383. 'var',
  9384. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9385. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9386. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9387. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9388. 'begin',
  9389. ' SetLength(DynArr1StaArr1,11);',
  9390. ' SetLength(DynArr2StaArr1,12);',
  9391. ' SetLength(DynArr2StaArr1[13],14);',
  9392. ' SetLength(DynArr2StaArr1,15,16);',
  9393. //' SetLength(DynArr1StaArr2,21);',
  9394. //' SetLength(DynArr2StaArr2,22);',
  9395. //' SetLength(DynArr2StaArr2[23],24);',
  9396. //' SetLength(DynArr2StaArr2,25,26);',
  9397. '']);
  9398. ConvertProgram;
  9399. CheckSource('TestArray_DynOfStatic',
  9400. LinesToStr([ // statements
  9401. 'this.DynArr1StaArr1 = [];',
  9402. 'this.DynArr2StaArr1 = [];',
  9403. '']),
  9404. LinesToStr([ // $mod.$main
  9405. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9406. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9407. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9408. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9409. ' $mod.DynArr2StaArr1,',
  9410. ' false,',
  9411. ' 15,',
  9412. ' 16,',
  9413. ' "s",',
  9414. ' 3',
  9415. ');',
  9416. '']));
  9417. end;
  9418. procedure TTestModule.TestArray_OpenArrayOfString;
  9419. begin
  9420. StartProgram(false);
  9421. Add('procedure DoIt(const a: array of String);');
  9422. Add('var');
  9423. Add(' i: longint;');
  9424. Add(' s: string;');
  9425. Add('begin');
  9426. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9427. Add('end;');
  9428. Add('var s: string;');
  9429. Add('begin');
  9430. Add(' DoIt([]);');
  9431. Add(' DoIt([s,''foo'','''',s+s]);');
  9432. ConvertProgram;
  9433. CheckSource('TestArray_OpenArrayOfString',
  9434. LinesToStr([ // statements
  9435. 'this.DoIt = function (a) {',
  9436. ' var i = 0;',
  9437. ' var s = "";',
  9438. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9439. ' i = $l;',
  9440. ' s = a[rtl.length(a) - i - 1];',
  9441. ' };',
  9442. '};',
  9443. 'this.s = "";',
  9444. '']),
  9445. LinesToStr([
  9446. '$mod.DoIt([]);',
  9447. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9448. '']));
  9449. end;
  9450. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9451. begin
  9452. StartProgram(false);
  9453. Add([
  9454. 'type TArr = array of char;',
  9455. 'var',
  9456. ' c: char;',
  9457. ' s: string;',
  9458. ' a: TArr;',
  9459. 'procedure Run(const a: array of char);',
  9460. 'begin',
  9461. ' Run(c);',
  9462. ' Run(s);',
  9463. 'end;',
  9464. 'begin',
  9465. ' a:=c;',
  9466. ' a:=s;',
  9467. ' a:=#13;',
  9468. ' a:=''Foo'';',
  9469. ' Run(c);',
  9470. ' Run(s);',
  9471. '']);
  9472. ConvertProgram;
  9473. CheckSource('TestArray_ArrayOfCharAssignString',
  9474. LinesToStr([ // statements
  9475. 'this.c = "";',
  9476. 'this.s = "";',
  9477. 'this.a = [];',
  9478. 'this.Run = function (a) {',
  9479. ' $mod.Run($mod.c.split(""));',
  9480. ' $mod.Run($mod.s.split(""));',
  9481. '};',
  9482. '']),
  9483. LinesToStr([
  9484. '$mod.a = $mod.c.split("");',
  9485. '$mod.a = $mod.s.split("");',
  9486. '$mod.a = "\r".split("");',
  9487. '$mod.a = "Foo".split("");',
  9488. '$mod.Run($mod.c.split(""));',
  9489. '$mod.Run($mod.s.split(""));',
  9490. '']));
  9491. end;
  9492. procedure TTestModule.TestArray_ConstRef;
  9493. begin
  9494. StartProgram(false);
  9495. Add([
  9496. 'type TArr = array of word;',
  9497. 'procedure Run(constref a: TArr);',
  9498. 'begin',
  9499. 'end;',
  9500. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9501. 'var l: TArr;',
  9502. 'begin',
  9503. ' Run(l);',
  9504. ' Run(a);',
  9505. ' Run(b);',
  9506. ' Run(c);',
  9507. ' Run(d);',
  9508. ' Run(e);',
  9509. 'end;',
  9510. 'begin',
  9511. '']);
  9512. ConvertProgram;
  9513. CheckResolverUnexpectedHints();
  9514. CheckSource('TestArray_ConstRef',
  9515. LinesToStr([ // statements
  9516. 'this.Run = function (a) {',
  9517. '};',
  9518. 'this.Fly = function (a, b, c, d, e) {',
  9519. ' var l = [];',
  9520. ' $mod.Run(l);',
  9521. ' $mod.Run(a);',
  9522. ' $mod.Run(b.get());',
  9523. ' $mod.Run(c.get());',
  9524. ' $mod.Run(d);',
  9525. ' $mod.Run(e);',
  9526. '};',
  9527. '']),
  9528. LinesToStr([
  9529. '']));
  9530. end;
  9531. procedure TTestModule.TestArray_Concat;
  9532. begin
  9533. StartProgram(false);
  9534. Add([
  9535. 'type',
  9536. ' integer = longint;',
  9537. ' TFlag = (big,small);',
  9538. ' TFlags = set of TFlag;',
  9539. ' TRec = record',
  9540. ' i: integer;',
  9541. ' end;',
  9542. ' TArrInt = array of integer;',
  9543. ' TArrRec = array of TRec;',
  9544. ' TArrFlag = array of TFlag;',
  9545. ' TArrSet = array of TFlags;',
  9546. ' TArrJSValue = array of jsvalue;',
  9547. 'var',
  9548. ' ArrInt: tarrint;',
  9549. ' ArrRec: tarrrec;',
  9550. ' ArrFlag: tarrflag;',
  9551. ' ArrSet: tarrset;',
  9552. ' ArrJSValue: tarrjsvalue;',
  9553. 'begin',
  9554. ' arrint:=concat(arrint);',
  9555. ' arrint:=concat(arrint,arrint);',
  9556. ' arrint:=concat(arrint,arrint,arrint);',
  9557. ' arrrec:=concat(arrrec);',
  9558. ' arrrec:=concat(arrrec,arrrec);',
  9559. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9560. ' arrset:=concat(arrset);',
  9561. ' arrset:=concat(arrset,arrset);',
  9562. ' arrset:=concat(arrset,arrset,arrset);',
  9563. ' arrjsvalue:=concat(arrjsvalue);',
  9564. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9565. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9566. ' arrint:=concat([1],arrint);',
  9567. ' arrflag:=concat([big]);',
  9568. ' arrflag:=concat([big],arrflag);',
  9569. ' arrflag:=concat(arrflag,[small]);',
  9570. '']);
  9571. ConvertProgram;
  9572. CheckSource('TestArray_Concat',
  9573. LinesToStr([ // statements
  9574. 'this.TFlag = {',
  9575. ' "0": "big",',
  9576. ' big: 0,',
  9577. ' "1": "small",',
  9578. ' small: 1',
  9579. '};',
  9580. 'rtl.recNewT(this, "TRec", function () {',
  9581. ' this.i = 0;',
  9582. ' this.$eq = function (b) {',
  9583. ' return this.i === b.i;',
  9584. ' };',
  9585. ' this.$assign = function (s) {',
  9586. ' this.i = s.i;',
  9587. ' return this;',
  9588. ' };',
  9589. '});',
  9590. 'this.ArrInt = [];',
  9591. 'this.ArrRec = [];',
  9592. 'this.ArrFlag = [];',
  9593. 'this.ArrSet = [];',
  9594. 'this.ArrJSValue = [];',
  9595. '']),
  9596. LinesToStr([ // $mod.$main
  9597. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9598. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9599. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9600. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9601. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9602. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9603. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9604. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9605. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9606. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9607. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9608. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9609. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9610. '$mod.ArrFlag = [$mod.TFlag.big];',
  9611. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9612. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9613. '']));
  9614. end;
  9615. procedure TTestModule.TestArray_Copy;
  9616. begin
  9617. StartProgram(false);
  9618. Add([
  9619. 'type',
  9620. ' integer = longint;',
  9621. ' TFlag = (big,small);',
  9622. ' TFlags = set of TFlag;',
  9623. ' TRec = record',
  9624. ' i: integer;',
  9625. ' end;',
  9626. ' TArrInt = array of integer;',
  9627. ' TArrRec = array of TRec;',
  9628. ' TArrSet = array of TFlags;',
  9629. ' TArrJSValue = array of jsvalue;',
  9630. 'var',
  9631. ' ArrInt: tarrint;',
  9632. ' ArrRec: tarrrec;',
  9633. ' ArrSet: tarrset;',
  9634. ' ArrJSValue: tarrjsvalue;',
  9635. 'begin',
  9636. ' arrint:=copy(arrint);',
  9637. ' arrint:=copy(arrint,2);',
  9638. ' arrint:=copy(arrint,3,4);',
  9639. ' arrint:=copy([1,1],1,2);',
  9640. ' arrrec:=copy(arrrec);',
  9641. ' arrrec:=copy(arrrec,5);',
  9642. ' arrrec:=copy(arrrec,6,7);',
  9643. ' arrset:=copy(arrset);',
  9644. ' arrset:=copy(arrset,8);',
  9645. ' arrset:=copy(arrset,9,10);',
  9646. ' arrjsvalue:=copy(arrjsvalue);',
  9647. ' arrjsvalue:=copy(arrjsvalue,11);',
  9648. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9649. ' ']);
  9650. ConvertProgram;
  9651. CheckSource('TestArray_Copy',
  9652. LinesToStr([ // statements
  9653. 'this.TFlag = {',
  9654. ' "0": "big",',
  9655. ' big: 0,',
  9656. ' "1": "small",',
  9657. ' small: 1',
  9658. '};',
  9659. 'rtl.recNewT(this, "TRec", function () {',
  9660. ' this.i = 0;',
  9661. ' this.$eq = function (b) {',
  9662. ' return this.i === b.i;',
  9663. ' };',
  9664. ' this.$assign = function (s) {',
  9665. ' this.i = s.i;',
  9666. ' return this;',
  9667. ' };',
  9668. '});',
  9669. 'this.ArrInt = [];',
  9670. 'this.ArrRec = [];',
  9671. 'this.ArrSet = [];',
  9672. 'this.ArrJSValue = [];',
  9673. '']),
  9674. LinesToStr([ // $mod.$main
  9675. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9676. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9677. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9678. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9679. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9680. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9681. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9682. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9683. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9684. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9685. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9686. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9687. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9688. '']));
  9689. end;
  9690. procedure TTestModule.TestArray_InsertDelete;
  9691. begin
  9692. StartProgram(false);
  9693. Add([
  9694. 'type',
  9695. ' integer = longint;',
  9696. ' TFlag = (big,small);',
  9697. ' TFlags = set of TFlag;',
  9698. ' TRec = record',
  9699. ' i: integer;',
  9700. ' end;',
  9701. ' TArrInt = array of integer;',
  9702. ' TArrRec = array of TRec;',
  9703. ' TArrSet = array of TFlags;',
  9704. ' TArrJSValue = array of jsvalue;',
  9705. ' TArrArrInt = array of TArrInt;',
  9706. 'var',
  9707. ' ArrInt: tarrint;',
  9708. ' ArrRec: tarrrec;',
  9709. ' ArrSet: tarrset;',
  9710. ' ArrJSValue: tarrjsvalue;',
  9711. ' ArrArrInt: TArrArrInt;',
  9712. 'begin',
  9713. ' Insert(1,arrint,2);',
  9714. ' Insert(arrint[3],arrint,4);',
  9715. ' Insert(arrrec[5],arrrec,6);',
  9716. ' Insert(arrset[7],arrset,7);',
  9717. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9718. ' Insert(10,arrjsvalue,11);',
  9719. ' Insert([23],arrarrint,22);',
  9720. ' Delete(arrint,12,13);',
  9721. ' Delete(arrrec,14,15);',
  9722. ' Delete(arrset,17,18);',
  9723. ' Delete(arrjsvalue,19,10);']);
  9724. ConvertProgram;
  9725. CheckSource('TestArray_InsertDelete',
  9726. LinesToStr([ // statements
  9727. 'this.TFlag = {',
  9728. ' "0": "big",',
  9729. ' big: 0,',
  9730. ' "1": "small",',
  9731. ' small: 1',
  9732. '};',
  9733. 'rtl.recNewT(this, "TRec", function () {',
  9734. ' this.i = 0;',
  9735. ' this.$eq = function (b) {',
  9736. ' return this.i === b.i;',
  9737. ' };',
  9738. ' this.$assign = function (s) {',
  9739. ' this.i = s.i;',
  9740. ' return this;',
  9741. ' };',
  9742. '});',
  9743. 'this.ArrInt = [];',
  9744. 'this.ArrRec = [];',
  9745. 'this.ArrSet = [];',
  9746. 'this.ArrJSValue = [];',
  9747. 'this.ArrArrInt = [];',
  9748. '']),
  9749. LinesToStr([ // $mod.$main
  9750. '$mod.ArrInt.splice(2, 0, 1);',
  9751. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9752. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9753. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9754. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9755. '$mod.ArrJSValue.splice(11, 0, 10);',
  9756. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9757. '$mod.ArrInt.splice(12, 13);',
  9758. '$mod.ArrRec.splice(14, 15);',
  9759. '$mod.ArrSet.splice(17, 18);',
  9760. '$mod.ArrJSValue.splice(19, 10);',
  9761. '']));
  9762. end;
  9763. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9764. begin
  9765. StartProgram(false);
  9766. Add([
  9767. '{$modeswitch arrayoperators}',
  9768. 'type',
  9769. ' integer = longint;',
  9770. ' TArrInt = array of integer;',
  9771. ' TArrStr = array of string;',
  9772. 'const',
  9773. ' Ints: TArrInt = (1,2,3);',
  9774. ' Aliases: TarrStr = (''foo'',''b'');',
  9775. ' OneInt: TArrInt = (7);',
  9776. ' OneStr: array of integer = (7);',
  9777. ' Chars: array of char = ''aoc'';',
  9778. ' Names: array of string = (''a'',''foo'');',
  9779. ' NameCount = low(Names)+high(Names)+length(Names);',
  9780. 'var i: integer;',
  9781. 'begin',
  9782. ' Ints:=[];',
  9783. ' Ints:=[1,1];',
  9784. ' Ints:=[1]+[2];',
  9785. ' Ints:=[2];',
  9786. ' Ints:=[]+ints;',
  9787. ' Ints:=Ints+[];',
  9788. ' Ints:=Ints+OneInt;',
  9789. ' Ints:=Ints+[1,1];',
  9790. ' Ints:=[i,i]+Ints;',
  9791. ' Ints:=[1]+[i]+[3];',
  9792. '']);
  9793. ConvertProgram;
  9794. CheckSource('TestArray_DynArrayConstObjFPC',
  9795. LinesToStr([ // statements
  9796. 'this.Ints = [1, 2, 3];',
  9797. 'this.Aliases = ["foo", "b"];',
  9798. 'this.OneInt = [7];',
  9799. 'this.OneStr = [7];',
  9800. 'this.Chars = ["a", "o", "c"];',
  9801. 'this.Names = ["a", "foo"];',
  9802. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9803. 'this.i = 0;',
  9804. '']),
  9805. LinesToStr([ // $mod.$main
  9806. '$mod.Ints = [];',
  9807. '$mod.Ints = [1, 1];',
  9808. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9809. '$mod.Ints = [2];',
  9810. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9811. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9812. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9813. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9814. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9815. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9816. '']));
  9817. end;
  9818. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9819. begin
  9820. StartProgram(false);
  9821. // Note: const c = [1,1]; defines a set!
  9822. Add([
  9823. '{$mode delphi}',
  9824. 'type',
  9825. ' integer = longint;',
  9826. ' TArrInt = array of integer;',
  9827. ' TArrStr = array of string;',
  9828. 'const',
  9829. ' Ints: TArrInt = [1,1,2];',
  9830. ' Aliases: TarrStr = [''foo'',''b''];',
  9831. ' OneInt: TArrInt = [7];',
  9832. ' OneStr: array of integer = [7]+[8];',
  9833. ' Chars: array of char = ''aoc'';',
  9834. ' Names: array of string = [''a'',''a''];',
  9835. ' NameCount = low(Names)+high(Names)+length(Names);',
  9836. 'begin',
  9837. '']);
  9838. ConvertProgram;
  9839. CheckSource('TestArray_DynArrayConstDelphi',
  9840. LinesToStr([ // statements
  9841. 'this.Ints = [1, 1, 2];',
  9842. 'this.Aliases = ["foo", "b"];',
  9843. 'this.OneInt = [7];',
  9844. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9845. 'this.Chars = ["a", "o", "c"];',
  9846. 'this.Names = ["a", "a"];',
  9847. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9848. '']),
  9849. LinesToStr([ // $mod.$main
  9850. '']));
  9851. end;
  9852. procedure TTestModule.TestArray_ArrayLitAsParam;
  9853. begin
  9854. StartProgram(false);
  9855. Add([
  9856. '{$modeswitch arrayoperators}',
  9857. 'type',
  9858. ' integer = longint;',
  9859. ' TArrInt = array of integer;',
  9860. ' TArrSet = array of (red,green,blue);',
  9861. 'procedure DoOpenInt(const a: array of integer); forward;',
  9862. 'procedure DoInt(const a: TArrInt);',
  9863. 'begin',
  9864. ' DoInt(a+[1]);',
  9865. ' DoInt([1]+a);',
  9866. ' DoOpenInt(a);',
  9867. ' DoOpenInt(a+[1]);',
  9868. ' DoOpenInt([1]+a);',
  9869. 'end;',
  9870. 'procedure DoOpenInt(const a: array of integer);',
  9871. 'begin',
  9872. ' DoOpenInt(a+[1]);',
  9873. ' DoOpenInt([1]+a);',
  9874. ' DoInt(a);',
  9875. ' DoInt(a+[1]);',
  9876. ' DoInt([1]+a);',
  9877. 'end;',
  9878. 'procedure DoSet(const a: TArrSet);',
  9879. 'begin',
  9880. ' DoSet(a+[red]);',
  9881. ' DoSet([blue]+a);',
  9882. 'end;',
  9883. 'var',
  9884. ' i: TArrInt;',
  9885. ' s: TArrSet;',
  9886. 'begin',
  9887. ' DoInt([1]);',
  9888. ' DoInt([1]+[2]);',
  9889. ' DoInt(i+[1]);',
  9890. ' DoInt([1]+i);',
  9891. ' DoOpenInt([1]);',
  9892. ' DoOpenInt([1]+[2]);',
  9893. ' DoOpenInt(i+[1]);',
  9894. ' DoOpenInt([1]+i);',
  9895. ' DoSet([red]);',
  9896. ' DoSet([blue]+[green]);',
  9897. ' DoSet(s+[blue]);',
  9898. ' DoSet([red]+s);',
  9899. '']);
  9900. ConvertProgram;
  9901. CheckSource('TestArray_ArrayLitAsParam',
  9902. LinesToStr([ // statements
  9903. 'this.TArrSet$a = {',
  9904. ' "0": "red",',
  9905. ' red: 0,',
  9906. ' "1": "green",',
  9907. ' green: 1,',
  9908. ' "2": "blue",',
  9909. ' blue: 2',
  9910. '};',
  9911. 'this.DoInt = function (a) {',
  9912. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9913. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9914. ' $mod.DoOpenInt(a);',
  9915. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9916. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9917. '};',
  9918. 'this.DoOpenInt = function (a) {',
  9919. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9920. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9921. ' $mod.DoInt(a);',
  9922. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9923. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9924. '};',
  9925. 'this.DoSet = function (a) {',
  9926. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9927. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9928. '};',
  9929. 'this.i = [];',
  9930. 'this.s = [];',
  9931. '']),
  9932. LinesToStr([ // $mod.$main
  9933. '$mod.DoInt([1]);',
  9934. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9935. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9936. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9937. '$mod.DoOpenInt([1]);',
  9938. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9939. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9940. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9941. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9942. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9943. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9944. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9945. '']));
  9946. end;
  9947. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9948. begin
  9949. StartProgram(false);
  9950. Add([
  9951. '{$modeswitch arrayoperators}',
  9952. 'type',
  9953. ' integer = longint;',
  9954. ' TArrInt = array of integer;',
  9955. ' TArrArrInt = array of TArrInt;',
  9956. 'procedure DoInt(const a: TArrArrInt);',
  9957. 'begin',
  9958. ' DoInt(a+[[1]]);',
  9959. ' DoInt([[1]]+a);',
  9960. ' DoInt(a);',
  9961. 'end;',
  9962. 'var',
  9963. ' i: TArrInt;',
  9964. ' a: TArrArrInt;',
  9965. 'begin',
  9966. ' a:=[[1]];',
  9967. ' a:=[i];',
  9968. ' a:=a+[i];',
  9969. ' a:=[i]+a;',
  9970. ' a:=[[1]+i];',
  9971. ' a:=[[1]+[2]];',
  9972. ' a:=[i+[2]];',
  9973. ' DoInt([[1]]);',
  9974. ' DoInt([[1]+[2],[3,4],[5]]);',
  9975. ' DoInt([i+[1]]+a);',
  9976. ' DoInt([i]+a);',
  9977. '']);
  9978. ConvertProgram;
  9979. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9980. LinesToStr([ // statements
  9981. 'this.DoInt = function (a) {',
  9982. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9983. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9984. ' $mod.DoInt(a);',
  9985. '};',
  9986. 'this.i = [];',
  9987. 'this.a = [];',
  9988. '']),
  9989. LinesToStr([ // $mod.$main
  9990. '$mod.a = [[1]];',
  9991. '$mod.a = [$mod.i];',
  9992. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9993. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9994. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9995. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9996. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9997. '$mod.DoInt([[1]]);',
  9998. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9999. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10000. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10001. '']));
  10002. end;
  10003. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10004. begin
  10005. StartProgram(false);
  10006. Add([
  10007. '{$modeswitch arrayoperators}',
  10008. 'type',
  10009. ' integer = longint;',
  10010. ' TArrInt = array[1..2] of integer;',
  10011. ' TArrArrInt = array of TArrInt;',
  10012. 'procedure DoInt(const a: TArrArrInt);',
  10013. 'begin',
  10014. ' DoInt(a+[[1,2]]);',
  10015. ' DoInt([[1,2]]+a);',
  10016. ' DoInt(a);',
  10017. 'end;',
  10018. 'var',
  10019. ' i: TArrInt;',
  10020. ' a: TArrArrInt;',
  10021. 'begin',
  10022. ' a:=[[1,1]];',
  10023. ' a:=[i];',
  10024. ' a:=a+[i];',
  10025. ' a:=[i]+a;',
  10026. ' DoInt([[1,1]]);',
  10027. ' DoInt([[1,2],[3,4]]);',
  10028. '']);
  10029. ConvertProgram;
  10030. CheckSource('TestArray_ArrayLitStaticAsParam',
  10031. LinesToStr([ // statements
  10032. 'this.DoInt = function (a) {',
  10033. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10034. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10035. ' $mod.DoInt(a);',
  10036. '};',
  10037. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10038. 'this.a = [];',
  10039. '']),
  10040. LinesToStr([ // $mod.$main
  10041. '$mod.a = [[1, 1]];',
  10042. '$mod.a = [$mod.i.slice(0)];',
  10043. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10044. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10045. '$mod.DoInt([[1, 1]]);',
  10046. '$mod.DoInt([[1, 2], [3, 4]]);',
  10047. '']));
  10048. end;
  10049. procedure TTestModule.TestArray_ForInArrOfString;
  10050. begin
  10051. StartProgram(false);
  10052. Add([
  10053. 'type',
  10054. 'type',
  10055. ' TMonthNameArray = array [1..12] of string;',
  10056. ' TMonthNames = TMonthNameArray;',
  10057. ' TObject = class',
  10058. ' private',
  10059. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10060. ' public',
  10061. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10062. ' end;',
  10063. 'var',
  10064. ' f: TObject;',
  10065. ' Month: string;',
  10066. ' Names: array of string = (''a'',''foo'',''bar'');',
  10067. ' i: longint;',
  10068. 'begin',
  10069. ' for Month in f.LongMonthNames do ;',
  10070. ' for Month in Names do ;',
  10071. ' for i:=low(Names) to high(Names) do ;',
  10072. '']);
  10073. ConvertProgram;
  10074. CheckSource('TestArray_ForInArrOfString',
  10075. LinesToStr([ // statements
  10076. 'rtl.createClass(this, "TObject", null, function () {',
  10077. ' this.$init = function () {',
  10078. ' };',
  10079. ' this.$final = function () {',
  10080. ' };',
  10081. '});',
  10082. 'this.f = null;',
  10083. 'this.Month = "";',
  10084. 'this.Names = ["a", "foo", "bar"];',
  10085. 'this.i = 0;',
  10086. '']),
  10087. LinesToStr([ // $mod.$main
  10088. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10089. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10090. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10091. '']));
  10092. end;
  10093. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10094. begin
  10095. StartProgram(false);
  10096. Add([
  10097. '{$modeswitch externalclass}',
  10098. 'type',
  10099. ' TJSObject = class external name ''Object''',
  10100. ' end;',
  10101. ' TJSArray = class external name ''Array''',
  10102. ' class function isArray(Value: JSValue) : boolean;',
  10103. ' function concat() : TJSArray; varargs;',
  10104. ' end;',
  10105. 'var',
  10106. ' aObj: TJSArray;',
  10107. ' a: array of longint;',
  10108. ' o: TJSObject;',
  10109. 'begin',
  10110. ' if TJSArray.isArray(65) then ;',
  10111. ' aObj:=TJSArray(a).concat(a);',
  10112. ' o:=TJSObject(a);']);
  10113. ConvertProgram;
  10114. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10115. LinesToStr([ // statements
  10116. 'this.aObj = null;',
  10117. 'this.a = [];',
  10118. 'this.o = null;',
  10119. '']),
  10120. LinesToStr([ // $mod.$main
  10121. 'if (Array.isArray(65)) ;',
  10122. '$mod.aObj = $mod.a.concat($mod.a);',
  10123. '$mod.o = $mod.a;',
  10124. '']));
  10125. end;
  10126. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10127. begin
  10128. StartProgram(false);
  10129. Add([
  10130. '{$modeswitch externalclass}',
  10131. 'type',
  10132. ' TArrStr = array of string;',
  10133. ' TJSArray = class external name ''Array''',
  10134. ' end;',
  10135. ' TJSObject = class external name ''Object''',
  10136. ' end;',
  10137. 'var',
  10138. ' aObj: TJSArray;',
  10139. ' a: TArrStr;',
  10140. ' jo: TJSObject;',
  10141. 'begin',
  10142. ' a:=TArrStr(aObj);',
  10143. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10144. ' a:=TarrStr(jo);',
  10145. '']);
  10146. ConvertProgram;
  10147. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10148. LinesToStr([ // statements
  10149. 'this.aObj = null;',
  10150. 'this.a = [];',
  10151. 'this.jo = null;',
  10152. '']),
  10153. LinesToStr([ // $mod.$main
  10154. '$mod.a = $mod.aObj;',
  10155. '$mod.aObj[1] = $mod.aObj[2];',
  10156. '$mod.a = $mod.jo;',
  10157. '']));
  10158. end;
  10159. procedure TTestModule.TestArrayOfConst_TVarRec;
  10160. begin
  10161. StartProgram(true,[supTVarRec]);
  10162. Add([
  10163. 'procedure Say(args: array of const);',
  10164. 'var',
  10165. ' i: longint;',
  10166. ' v: TVarRec;',
  10167. 'begin',
  10168. ' for i:=low(args) to high(args) do begin',
  10169. ' v:=args[i];',
  10170. ' case v.vtype of',
  10171. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10172. ' end;',
  10173. ' end;',
  10174. ' for v in args do ;',
  10175. ' args:=nil;',
  10176. ' SetLength(args,2);',
  10177. 'end;',
  10178. 'begin']);
  10179. ConvertProgram;
  10180. CheckSource('TestArrayOfConst_TVarRec',
  10181. LinesToStr([ // statements
  10182. 'this.Say = function (args) {',
  10183. ' var i = 0;',
  10184. ' var v = pas.system.TVarRec.$new();',
  10185. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10186. ' i = $l;',
  10187. ' v.$assign(args[i]);',
  10188. ' var $tmp = v.VType;',
  10189. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10190. ' };',
  10191. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10192. ' args = [];',
  10193. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10194. '};',
  10195. '']),
  10196. LinesToStr([ // $mod.$main
  10197. ]));
  10198. end;
  10199. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10200. begin
  10201. StartProgram(true,[supTVarRec]);
  10202. Add([
  10203. 'procedure Say(args: array of const);',
  10204. 'begin',
  10205. ' Say(args);',
  10206. 'end;',
  10207. 'var',
  10208. ' p: Pointer;',
  10209. ' j: jsvalue;',
  10210. ' c: currency;',
  10211. 'begin',
  10212. ' Say([]);',
  10213. ' Say([1]);',
  10214. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10215. '']);
  10216. ConvertProgram;
  10217. CheckSource('TestArrayOfConst_PassBaseTypes',
  10218. LinesToStr([ // statements
  10219. 'this.Say = function (args) {',
  10220. ' $mod.Say(args);',
  10221. '};',
  10222. 'this.p = null;',
  10223. 'this.j = undefined;',
  10224. 'this.c = 0;',
  10225. '']),
  10226. LinesToStr([ // $mod.$main
  10227. '$mod.Say([]);',
  10228. '$mod.Say(pas.system.VarRecs(0, 1));',
  10229. '$mod.Say(pas.system.VarRecs(',
  10230. ' 9,',
  10231. ' "c",',
  10232. ' 18,',
  10233. ' "foo",',
  10234. ' 5,',
  10235. ' null,',
  10236. ' 1,',
  10237. ' true,',
  10238. ' 3,',
  10239. ' 1.3,',
  10240. ' 5,',
  10241. ' $mod.p,',
  10242. ' 20,',
  10243. ' $mod.j,',
  10244. ' 12,',
  10245. ' $mod.c',
  10246. ' ));',
  10247. '']));
  10248. end;
  10249. procedure TTestModule.TestArrayOfConst_PassObj;
  10250. begin
  10251. StartProgram(true,[supTVarRec]);
  10252. Add([
  10253. '{$interfaces corba}',
  10254. 'type',
  10255. ' TObject = class',
  10256. ' end;',
  10257. ' TClass = class of TObject;',
  10258. ' IUnknown = interface',
  10259. ' end;',
  10260. 'procedure Say(args: array of const);',
  10261. 'begin',
  10262. 'end;',
  10263. 'var',
  10264. ' o: TObject;',
  10265. ' c: TClass;',
  10266. ' i: IUnknown;',
  10267. 'begin',
  10268. ' Say([o,c,TObject]);',
  10269. ' Say([nil,i]);',
  10270. '']);
  10271. ConvertProgram;
  10272. CheckSource('TestArrayOfConst_PassObj',
  10273. LinesToStr([ // statements
  10274. 'rtl.createClass(this, "TObject", null, function () {',
  10275. ' this.$init = function () {',
  10276. ' };',
  10277. ' this.$final = function () {',
  10278. ' };',
  10279. '});',
  10280. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10281. 'this.Say = function (args) {',
  10282. '};',
  10283. 'this.o = null;',
  10284. 'this.c = null;',
  10285. 'this.i = null;',
  10286. '']),
  10287. LinesToStr([ // $mod.$main
  10288. '$mod.Say(pas.system.VarRecs(',
  10289. ' 7,',
  10290. ' $mod.o,',
  10291. ' 8,',
  10292. ' $mod.c,',
  10293. ' 8,',
  10294. ' $mod.TObject',
  10295. '));',
  10296. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10297. '']));
  10298. end;
  10299. procedure TTestModule.TestRecord_Empty;
  10300. begin
  10301. StartProgram(false);
  10302. Add([
  10303. 'type',
  10304. ' TRecA = record',
  10305. ' end;',
  10306. 'var a,b: TRecA;',
  10307. 'begin',
  10308. ' if a=b then ;']);
  10309. ConvertProgram;
  10310. CheckSource('TestRecord_Empty',
  10311. LinesToStr([ // statements
  10312. 'rtl.recNewT(this, "TRecA", function () {',
  10313. ' this.$eq = function (b) {',
  10314. ' return true;',
  10315. ' };',
  10316. ' this.$assign = function (s) {',
  10317. ' return this;',
  10318. ' };',
  10319. '});',
  10320. 'this.a = this.TRecA.$new();',
  10321. 'this.b = this.TRecA.$new();',
  10322. '']),
  10323. LinesToStr([ // $mod.$main
  10324. 'if ($mod.a.$eq($mod.b)) ;'
  10325. ]));
  10326. end;
  10327. procedure TTestModule.TestRecord_Var;
  10328. begin
  10329. StartProgram(false);
  10330. Add('type');
  10331. Add(' TRecA = record');
  10332. Add(' Bold: longint;');
  10333. Add(' end;');
  10334. Add('var Rec: TRecA;');
  10335. Add('begin');
  10336. Add(' rec.bold:=123');
  10337. ConvertProgram;
  10338. CheckSource('TestRecord_Var',
  10339. LinesToStr([ // statements
  10340. 'rtl.recNewT(this, "TRecA", function () {',
  10341. ' this.Bold = 0;',
  10342. ' this.$eq = function (b) {',
  10343. ' return this.Bold === b.Bold;',
  10344. ' };',
  10345. ' this.$assign = function (s) {',
  10346. ' this.Bold = s.Bold;',
  10347. ' return this;',
  10348. ' };',
  10349. '});',
  10350. 'this.Rec = this.TRecA.$new();',
  10351. '']),
  10352. LinesToStr([ // $mod.$main
  10353. '$mod.Rec.Bold = 123;'
  10354. ]));
  10355. end;
  10356. procedure TTestModule.TestRecord_VarExternal;
  10357. begin
  10358. StartProgram(false);
  10359. Add([
  10360. '{$modeswitch externalclass}',
  10361. 'type',
  10362. ' TRecA = record',
  10363. ' i: byte;',
  10364. ' length_: longint external name ''length'';',
  10365. ' end;',
  10366. 'var Rec: TRecA;',
  10367. 'begin',
  10368. ' rec.length_ := rec.length_',
  10369. '']);
  10370. ConvertProgram;
  10371. CheckSource('TestRecord_VarExternal',
  10372. LinesToStr([ // statements
  10373. 'rtl.recNewT(this, "TRecA", function () {',
  10374. ' this.i = 0;',
  10375. ' this.$eq = function (b) {',
  10376. ' return (this.i === b.i) && (this.length === b.length);',
  10377. ' };',
  10378. ' this.$assign = function (s) {',
  10379. ' this.i = s.i;',
  10380. ' this.length = s.length;',
  10381. ' return this;',
  10382. ' };',
  10383. '});',
  10384. 'this.Rec = this.TRecA.$new();',
  10385. '']),
  10386. LinesToStr([ // $mod.$main
  10387. '$mod.Rec.length = $mod.Rec.length;'
  10388. ]));
  10389. end;
  10390. procedure TTestModule.TestRecord_WithDo;
  10391. begin
  10392. StartProgram(false);
  10393. Add('type');
  10394. Add(' TRec = record');
  10395. Add(' vI: longint;');
  10396. Add(' end;');
  10397. Add('var');
  10398. Add(' Int: longint;');
  10399. Add(' r: TRec;');
  10400. Add('begin');
  10401. Add(' with r do');
  10402. Add(' int:=vi;');
  10403. Add(' with r do begin');
  10404. Add(' int:=vi;');
  10405. Add(' vi:=int;');
  10406. Add(' end;');
  10407. ConvertProgram;
  10408. CheckSource('TestWithRecordDo',
  10409. LinesToStr([ // statements
  10410. 'rtl.recNewT(this, "TRec", function () {',
  10411. ' this.vI = 0;',
  10412. ' this.$eq = function (b) {',
  10413. ' return this.vI === b.vI;',
  10414. ' };',
  10415. ' this.$assign = function (s) {',
  10416. ' this.vI = s.vI;',
  10417. ' return this;',
  10418. ' };',
  10419. '});',
  10420. 'this.Int = 0;',
  10421. 'this.r = this.TRec.$new();',
  10422. '']),
  10423. LinesToStr([ // $mod.$main
  10424. 'var $with = $mod.r;',
  10425. '$mod.Int = $with.vI;',
  10426. 'var $with1 = $mod.r;',
  10427. '$mod.Int = $with1.vI;',
  10428. '$with1.vI = $mod.Int;'
  10429. ]));
  10430. end;
  10431. procedure TTestModule.TestRecord_Assign;
  10432. begin
  10433. StartProgram(false);
  10434. Add('type');
  10435. Add(' TEnum = (red,green);');
  10436. Add(' TEnums = set of TEnum;');
  10437. Add(' TSmallRec = record');
  10438. Add(' N: longint;');
  10439. Add(' end;');
  10440. Add(' TBigRec = record');
  10441. Add(' Int: longint;');
  10442. Add(' D: double;');
  10443. Add(' Arr: array of longint;');
  10444. Add(' Arr2: array[1..2] of longint;');
  10445. Add(' Small: TSmallRec;');
  10446. Add(' Enums: TEnums;');
  10447. Add(' end;');
  10448. Add('var');
  10449. Add(' r, s: TBigRec;');
  10450. Add('begin');
  10451. Add(' r:=s;');
  10452. Add(' r:=default(TBigRec);');
  10453. Add(' r:=default(s);');
  10454. ConvertProgram;
  10455. CheckSource('TestRecord_Assign',
  10456. LinesToStr([ // statements
  10457. 'this.TEnum = {',
  10458. ' "0": "red",',
  10459. ' red: 0,',
  10460. ' "1": "green",',
  10461. ' green: 1',
  10462. '};',
  10463. 'rtl.recNewT(this, "TSmallRec", function () {',
  10464. ' this.N = 0;',
  10465. ' this.$eq = function (b) {',
  10466. ' return this.N === b.N;',
  10467. ' };',
  10468. ' this.$assign = function (s) {',
  10469. ' this.N = s.N;',
  10470. ' return this;',
  10471. ' };',
  10472. '});',
  10473. 'rtl.recNewT(this, "TBigRec", function () {',
  10474. ' this.Int = 0;',
  10475. ' this.D = 0.0;',
  10476. ' this.$new = function () {',
  10477. ' var r = Object.create(this);',
  10478. ' r.Arr = [];',
  10479. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10480. ' r.Small = $mod.TSmallRec.$new();',
  10481. ' r.Enums = {};',
  10482. ' return r;',
  10483. ' };',
  10484. ' this.$eq = function (b) {',
  10485. ' 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);',
  10486. ' };',
  10487. ' this.$assign = function (s) {',
  10488. ' this.Int = s.Int;',
  10489. ' this.D = s.D;',
  10490. ' this.Arr = rtl.arrayRef(s.Arr);',
  10491. ' this.Arr2 = s.Arr2.slice(0);',
  10492. ' this.Small.$assign(s.Small);',
  10493. ' this.Enums = rtl.refSet(s.Enums);',
  10494. ' return this;',
  10495. ' };',
  10496. '});',
  10497. 'this.r = this.TBigRec.$new();',
  10498. 'this.s = this.TBigRec.$new();',
  10499. '']),
  10500. LinesToStr([ // $mod.$main
  10501. '$mod.r.$assign($mod.s);',
  10502. '$mod.r.$assign($mod.TBigRec.$new());',
  10503. '$mod.r.$assign($mod.TBigRec.$new());',
  10504. '']));
  10505. end;
  10506. procedure TTestModule.TestRecord_AsParams;
  10507. begin
  10508. StartProgram(false);
  10509. Add([
  10510. 'type',
  10511. ' integer = longint;',
  10512. ' TRecord = record',
  10513. ' i: integer;',
  10514. ' end;',
  10515. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10516. 'var vL: TRecord;',
  10517. 'begin',
  10518. ' vd:=vd;',
  10519. ' vd.i:=vd.i;',
  10520. ' vl:=vc;',
  10521. ' vv:=vv;',
  10522. ' vv.i:=vv.i;',
  10523. ' U:=vl;',
  10524. ' U:=vd;',
  10525. ' U:=vc;',
  10526. ' U:=vv;',
  10527. ' vl:=TRecord(U);',
  10528. ' vd:=TRecord(U);',
  10529. ' vv:=TRecord(U);',
  10530. ' doit(vd,vd,vd,vd);',
  10531. ' doit(vc,vc,vl,vl);',
  10532. ' doit(vv,vv,vv,vv);',
  10533. ' doit(vl,vl,vl,vl);',
  10534. ' TRecord(U).i:=3;',
  10535. 'end;',
  10536. 'var i: TRecord;',
  10537. 'begin',
  10538. ' doit(i,i,i,i);',
  10539. '']);
  10540. ConvertProgram;
  10541. CheckSource('TestRecord_AsParams',
  10542. LinesToStr([ // statements
  10543. 'rtl.recNewT(this, "TRecord", function () {',
  10544. ' this.i = 0;',
  10545. ' this.$eq = function (b) {',
  10546. ' return this.i === b.i;',
  10547. ' };',
  10548. ' this.$assign = function (s) {',
  10549. ' this.i = s.i;',
  10550. ' return this;',
  10551. ' };',
  10552. '});',
  10553. 'this.DoIt = function (vD, vC, vV, U) {',
  10554. ' var vL = $mod.TRecord.$new();',
  10555. ' vD.$assign(vD);',
  10556. ' vD.i = vD.i;',
  10557. ' vL.$assign(vC);',
  10558. ' vV.$assign(vV);',
  10559. ' vV.i = vV.i;',
  10560. ' U.$assign(vL);',
  10561. ' U.$assign(vD);',
  10562. ' U.$assign(vC);',
  10563. ' U.$assign(vV);',
  10564. ' vL.$assign(U);',
  10565. ' vD.$assign(U);',
  10566. ' vV.$assign(U);',
  10567. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10568. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10569. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10570. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10571. ' U.i = 3;',
  10572. '};',
  10573. 'this.i = this.TRecord.$new();'
  10574. ]),
  10575. LinesToStr([
  10576. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10577. '']));
  10578. end;
  10579. procedure TTestModule.TestRecord_ConstRef;
  10580. begin
  10581. StartProgram(false);
  10582. Add([
  10583. 'type TRec = record i: word; end;',
  10584. 'procedure Run(constref a: TRec);',
  10585. 'begin',
  10586. 'end;',
  10587. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10588. 'var l: TRec;',
  10589. 'begin',
  10590. ' Run(l);',
  10591. ' Run(a);',
  10592. ' Run(b);',
  10593. ' Run(c);',
  10594. ' Run(d);',
  10595. ' Run(e);',
  10596. 'end;',
  10597. 'begin',
  10598. '']);
  10599. ConvertProgram;
  10600. CheckResolverUnexpectedHints();
  10601. CheckSource('TestRecord_ConstRef',
  10602. LinesToStr([ // statements
  10603. 'rtl.recNewT(this, "TRec", function () {',
  10604. ' this.i = 0;',
  10605. ' this.$eq = function (b) {',
  10606. ' return this.i === b.i;',
  10607. ' };',
  10608. ' this.$assign = function (s) {',
  10609. ' this.i = s.i;',
  10610. ' return this;',
  10611. ' };',
  10612. '});',
  10613. 'this.Run = function (a) {',
  10614. '};',
  10615. 'this.Fly = function (a, b, c, d, e) {',
  10616. ' var l = $mod.TRec.$new();',
  10617. ' $mod.Run(l);',
  10618. ' $mod.Run(a);',
  10619. ' $mod.Run(b);',
  10620. ' $mod.Run(c);',
  10621. ' $mod.Run(d);',
  10622. ' $mod.Run(e);',
  10623. '};',
  10624. '']),
  10625. LinesToStr([
  10626. '']));
  10627. end;
  10628. procedure TTestModule.TestRecordElement_AsParams;
  10629. begin
  10630. StartProgram(false);
  10631. Add('type');
  10632. Add(' integer = longint;');
  10633. Add(' TRecord = record');
  10634. Add(' i: integer;');
  10635. Add(' end;');
  10636. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10637. Add('var vJ: TRecord;');
  10638. Add('begin');
  10639. Add(' doit(vj.i,vj.i,vj.i);');
  10640. Add('end;');
  10641. Add('var r: TRecord;');
  10642. Add('begin');
  10643. Add(' doit(r.i,r.i,r.i);');
  10644. ConvertProgram;
  10645. CheckSource('TestRecordElement_AsParams',
  10646. LinesToStr([ // statements
  10647. 'rtl.recNewT(this, "TRecord", function () {',
  10648. ' this.i = 0;',
  10649. ' this.$eq = function (b) {',
  10650. ' return this.i === b.i;',
  10651. ' };',
  10652. ' this.$assign = function (s) {',
  10653. ' this.i = s.i;',
  10654. ' return this;',
  10655. ' };',
  10656. '});',
  10657. 'this.DoIt = function (vG,vH,vI) {',
  10658. ' var vJ = $mod.TRecord.$new();',
  10659. ' $mod.DoIt(vJ.i, vJ.i, {',
  10660. ' p: vJ,',
  10661. ' get: function () {',
  10662. ' return this.p.i;',
  10663. ' },',
  10664. ' set: function (v) {',
  10665. ' this.p.i = v;',
  10666. ' }',
  10667. ' });',
  10668. '};',
  10669. 'this.r = this.TRecord.$new();'
  10670. ]),
  10671. LinesToStr([
  10672. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10673. ' p: $mod.r,',
  10674. ' get: function () {',
  10675. ' return this.p.i;',
  10676. ' },',
  10677. ' set: function (v) {',
  10678. ' this.p.i = v;',
  10679. ' }',
  10680. '});'
  10681. ]));
  10682. end;
  10683. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10684. begin
  10685. StartProgram(false);
  10686. Add('type');
  10687. Add(' integer = longint;');
  10688. Add(' TRecord = record');
  10689. Add(' i: integer;');
  10690. Add(' end;');
  10691. Add('function GetRec(vB: integer = 0): TRecord;');
  10692. Add('begin');
  10693. Add('end;');
  10694. Add('procedure DoIt(vG: integer; const vH: integer);');
  10695. Add('begin');
  10696. Add('end;');
  10697. Add('begin');
  10698. Add(' doit(getrec.i,getrec.i);');
  10699. Add(' doit(getrec().i,getrec().i);');
  10700. Add(' doit(getrec(1).i,getrec(2).i);');
  10701. ConvertProgram;
  10702. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10703. LinesToStr([ // statements
  10704. 'rtl.recNewT(this, "TRecord", function () {',
  10705. ' this.i = 0;',
  10706. ' this.$eq = function (b) {',
  10707. ' return this.i === b.i;',
  10708. ' };',
  10709. ' this.$assign = function (s) {',
  10710. ' this.i = s.i;',
  10711. ' return this;',
  10712. ' };',
  10713. '});',
  10714. 'this.GetRec = function (vB) {',
  10715. ' var Result = $mod.TRecord.$new();',
  10716. ' return Result;',
  10717. '};',
  10718. 'this.DoIt = function (vG, vH) {',
  10719. '};',
  10720. '']),
  10721. LinesToStr([
  10722. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10723. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10724. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10725. '']));
  10726. end;
  10727. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10728. begin
  10729. StartProgram(false);
  10730. Add('type');
  10731. Add(' integer = longint;');
  10732. Add(' TRecord = record');
  10733. Add(' i: integer;');
  10734. Add(' end;');
  10735. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10736. Add('begin');
  10737. Add('end;');
  10738. Add('var r: trecord;');
  10739. Add('begin');
  10740. Add(' with r do ');
  10741. Add(' doit(i,i,i);');
  10742. ConvertProgram;
  10743. CheckSource('TestRecordElementFromWith_AsParams',
  10744. LinesToStr([ // statements
  10745. 'rtl.recNewT(this, "TRecord", function () {',
  10746. ' this.i = 0;',
  10747. ' this.$eq = function (b) {',
  10748. ' return this.i === b.i;',
  10749. ' };',
  10750. ' this.$assign = function (s) {',
  10751. ' this.i = s.i;',
  10752. ' return this;',
  10753. ' };',
  10754. '});',
  10755. 'this.DoIt = function (vG,vH,vI) {',
  10756. '};',
  10757. 'this.r = this.TRecord.$new();'
  10758. ]),
  10759. LinesToStr([
  10760. 'var $with = $mod.r;',
  10761. '$mod.DoIt($with.i,$with.i,{',
  10762. ' p: $with,',
  10763. ' get: function () {',
  10764. ' return this.p.i;',
  10765. ' },',
  10766. ' set: function (v) {',
  10767. ' this.p.i = v;',
  10768. ' }',
  10769. '});',
  10770. '']));
  10771. end;
  10772. procedure TTestModule.TestRecord_Equal;
  10773. begin
  10774. StartProgram(false);
  10775. Add('type');
  10776. Add(' integer = longint;');
  10777. Add(' TFlag = (red,blue);');
  10778. Add(' TFlags = set of TFlag;');
  10779. Add(' TProc = procedure;');
  10780. Add(' TRecord = record');
  10781. Add(' i: integer;');
  10782. Add(' Event: TProc;');
  10783. Add(' f: TFlags;');
  10784. Add(' end;');
  10785. Add(' TNested = record');
  10786. Add(' r: TRecord;');
  10787. Add(' end;');
  10788. Add('var');
  10789. Add(' b: boolean;');
  10790. Add(' r,s: trecord;');
  10791. Add('begin');
  10792. Add(' b:=r=s;');
  10793. Add(' b:=r<>s;');
  10794. ConvertProgram;
  10795. CheckSource('TestRecord_Equal',
  10796. LinesToStr([ // statements
  10797. 'this.TFlag = {',
  10798. ' "0": "red",',
  10799. ' red: 0,',
  10800. ' "1": "blue",',
  10801. ' blue: 1',
  10802. '};',
  10803. 'rtl.recNewT(this, "TRecord", function () {',
  10804. ' this.i = 0;',
  10805. ' this.Event = null;',
  10806. ' this.$new = function () {',
  10807. ' var r = Object.create(this);',
  10808. ' r.f = {};',
  10809. ' return r;',
  10810. ' };',
  10811. ' this.$eq = function (b) {',
  10812. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10813. ' };',
  10814. ' this.$assign = function (s) {',
  10815. ' this.i = s.i;',
  10816. ' this.Event = s.Event;',
  10817. ' this.f = rtl.refSet(s.f);',
  10818. ' return this;',
  10819. ' };',
  10820. '});',
  10821. 'rtl.recNewT(this, "TNested", function () {',
  10822. ' this.$new = function () {',
  10823. ' var r = Object.create(this);',
  10824. ' r.r = $mod.TRecord.$new();',
  10825. ' return r;',
  10826. ' };',
  10827. ' this.$eq = function (b) {',
  10828. ' return this.r.$eq(b.r);',
  10829. ' };',
  10830. ' this.$assign = function (s) {',
  10831. ' this.r.$assign(s.r);',
  10832. ' return this;',
  10833. ' };',
  10834. '});',
  10835. 'this.b = false;',
  10836. 'this.r = this.TRecord.$new();',
  10837. 'this.s = this.TRecord.$new();',
  10838. '']),
  10839. LinesToStr([
  10840. '$mod.b = $mod.r.$eq($mod.s);',
  10841. '$mod.b = !$mod.r.$eq($mod.s);',
  10842. '']));
  10843. end;
  10844. procedure TTestModule.TestRecord_JSValue;
  10845. begin
  10846. StartProgram(false);
  10847. Add([
  10848. 'type',
  10849. ' TRecord = record',
  10850. ' i: longint;',
  10851. ' end;',
  10852. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10853. 'begin',
  10854. 'end;',
  10855. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10856. 'begin',
  10857. ' if jsvalue(d) then ;',
  10858. ' if jsvalue(c) then ;',
  10859. ' if jsvalue(v) then ;',
  10860. 'end;',
  10861. 'var',
  10862. ' Jv: jsvalue;',
  10863. ' Rec: trecord;',
  10864. 'begin',
  10865. ' rec:=trecord(jv);',
  10866. ' jv:=rec;',
  10867. ' Fly(rec,rec);',
  10868. ' Fly(@rec,@rec);',
  10869. ' if jsvalue(Rec) then ;',
  10870. ' Run(trecord(jv),trecord(jv),rec);',
  10871. '']);
  10872. ConvertProgram;
  10873. CheckSource('TestRecord_JSValue',
  10874. LinesToStr([ // statements
  10875. 'rtl.recNewT(this, "TRecord", function () {',
  10876. ' this.i = 0;',
  10877. ' this.$eq = function (b) {',
  10878. ' return this.i === b.i;',
  10879. ' };',
  10880. ' this.$assign = function (s) {',
  10881. ' this.i = s.i;',
  10882. ' return this;',
  10883. ' };',
  10884. '});',
  10885. 'this.Fly = function (d, c) {',
  10886. '};',
  10887. 'this.Run = function (d, c, v) {',
  10888. ' if (d) ;',
  10889. ' if (c) ;',
  10890. ' if (v) ;',
  10891. '};',
  10892. 'this.Jv = undefined;',
  10893. 'this.Rec = this.TRecord.$new();',
  10894. '']),
  10895. LinesToStr([
  10896. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10897. '$mod.Jv = $mod.Rec;',
  10898. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10899. '$mod.Fly($mod.Rec, $mod.Rec);',
  10900. 'if ($mod.Rec) ;',
  10901. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10902. '']));
  10903. end;
  10904. procedure TTestModule.TestRecord_VariantFail;
  10905. begin
  10906. StartProgram(false);
  10907. Add([
  10908. 'type',
  10909. ' TRec = record',
  10910. ' case word of',
  10911. ' 0: (b0, b1: Byte);',
  10912. ' 1: (i: word);',
  10913. ' end;',
  10914. 'begin']);
  10915. SetExpectedPasResolverError('variant record is not supported',
  10916. nXIsNotSupported);
  10917. ConvertProgram;
  10918. end;
  10919. procedure TTestModule.TestRecord_FieldArray;
  10920. begin
  10921. StartProgram(false);
  10922. Add([
  10923. 'type',
  10924. ' TArrInt = array[3..4] of longint;',
  10925. ' TArrArrInt = array[3..4] of longint;',
  10926. ' TRec = record',
  10927. ' a: array of longint;',
  10928. ' s: array[1..2] of longint;',
  10929. ' m: array[1..2,3..4] of longint;',
  10930. ' o: TArrArrInt;',
  10931. ' end;',
  10932. 'begin']);
  10933. ConvertProgram;
  10934. CheckSource('TestRecord_FieldArray',
  10935. LinesToStr([ // statements
  10936. 'rtl.recNewT(this, "TRec", function () {',
  10937. ' this.$new = function () {',
  10938. ' var r = Object.create(this);',
  10939. ' r.a = [];',
  10940. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10941. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10942. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10943. ' return r;',
  10944. ' };',
  10945. ' this.$eq = function (b) {',
  10946. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10947. ' };',
  10948. ' this.$assign = function (s) {',
  10949. ' this.a = rtl.arrayRef(s.a);',
  10950. ' this.s = s.s.slice(0);',
  10951. ' this.m = s.m.slice(0);',
  10952. ' this.o = s.o.slice(0);',
  10953. ' return this;',
  10954. ' };',
  10955. '});',
  10956. '']),
  10957. LinesToStr([ // $mod.$main
  10958. '']));
  10959. end;
  10960. procedure TTestModule.TestRecord_Const;
  10961. begin
  10962. StartProgram(false);
  10963. Add([
  10964. 'type',
  10965. ' TArrInt = array[3..4] of longint;',
  10966. ' TPoint = record x,y: longint; end;',
  10967. ' TRec = record',
  10968. ' i: longint;',
  10969. ' a: array of longint;',
  10970. ' s: array[1..2] of longint;',
  10971. ' m: array[1..2,3..4] of longint;',
  10972. ' p: TPoint;',
  10973. ' end;',
  10974. ' TPoints = array of TPoint;',
  10975. 'const',
  10976. ' r: TRec = (',
  10977. ' i:1;',
  10978. ' a:(2,3);',
  10979. ' s:(4,5);',
  10980. ' m:( (11,12), (13,14) );',
  10981. ' p: (x:21; y:22)',
  10982. ' );',
  10983. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10984. 'begin']);
  10985. ConvertProgram;
  10986. CheckSource('TestRecord_Const',
  10987. LinesToStr([ // statements
  10988. 'rtl.recNewT(this, "TPoint", function () {',
  10989. ' this.x = 0;',
  10990. ' this.y = 0;',
  10991. ' this.$eq = function (b) {',
  10992. ' return (this.x === b.x) && (this.y === b.y);',
  10993. ' };',
  10994. ' this.$assign = function (s) {',
  10995. ' this.x = s.x;',
  10996. ' this.y = s.y;',
  10997. ' return this;',
  10998. ' };',
  10999. '});',
  11000. 'rtl.recNewT(this, "TRec", function () {',
  11001. ' this.i = 0;',
  11002. ' this.$new = function () {',
  11003. ' var r = Object.create(this);',
  11004. ' r.a = [];',
  11005. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11006. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11007. ' r.p = $mod.TPoint.$new();',
  11008. ' return r;',
  11009. ' };',
  11010. ' this.$eq = function (b) {',
  11011. ' 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);',
  11012. ' };',
  11013. ' this.$assign = function (s) {',
  11014. ' this.i = s.i;',
  11015. ' this.a = rtl.arrayRef(s.a);',
  11016. ' this.s = s.s.slice(0);',
  11017. ' this.m = s.m.slice(0);',
  11018. ' this.p.$assign(s.p);',
  11019. ' return this;',
  11020. ' };',
  11021. '});',
  11022. 'this.r = this.TRec.$clone({',
  11023. ' i: 1,',
  11024. ' a: [2, 3],',
  11025. ' s: [4, 5],',
  11026. ' m: [[11, 12], [13, 14]],',
  11027. ' p: this.TPoint.$clone({',
  11028. ' x: 21,',
  11029. ' y: 22',
  11030. ' })',
  11031. '});',
  11032. 'this.p = [this.TPoint.$clone({',
  11033. ' x: 1,',
  11034. ' y: 2',
  11035. '}), this.TPoint.$clone({',
  11036. ' x: 3,',
  11037. ' y: 4',
  11038. '})];',
  11039. '']),
  11040. LinesToStr([ // $mod.$main
  11041. '']));
  11042. end;
  11043. procedure TTestModule.TestRecord_TypecastFail;
  11044. begin
  11045. StartProgram(false);
  11046. Add([
  11047. 'type',
  11048. ' TPoint = record x,y: longint; end;',
  11049. ' TRec = record l: longint end;',
  11050. 'var p: TPoint;',
  11051. 'begin',
  11052. ' if TRec(p).l=2 then ;']);
  11053. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11054. nIllegalTypeConversionTo);
  11055. ConvertProgram;
  11056. end;
  11057. procedure TTestModule.TestRecord_InFunction;
  11058. begin
  11059. StartProgram(false);
  11060. Add([
  11061. 'var TPoint: longint = 3;',
  11062. 'procedure DoIt;',
  11063. 'type',
  11064. ' TPoint = record x,y: longint; end;',
  11065. ' TPoints = array of TPoint;',
  11066. 'var',
  11067. ' r: TPoint;',
  11068. ' p: TPoints;',
  11069. 'begin',
  11070. ' SetLength(p,2);',
  11071. 'end;',
  11072. 'begin']);
  11073. ConvertProgram;
  11074. CheckSource('TestRecord_InFunction',
  11075. LinesToStr([ // statements
  11076. 'this.TPoint = 3;',
  11077. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11078. ' this.x = 0;',
  11079. ' this.y = 0;',
  11080. ' this.$eq = function (b) {',
  11081. ' return (this.x === b.x) && (this.y === b.y);',
  11082. ' };',
  11083. ' this.$assign = function (s) {',
  11084. ' this.x = s.x;',
  11085. ' this.y = s.y;',
  11086. ' return this;',
  11087. ' };',
  11088. '});',
  11089. 'this.DoIt = function () {',
  11090. ' var r = TPoint$1.$new();',
  11091. ' var p = [];',
  11092. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11093. '};',
  11094. '']),
  11095. LinesToStr([ // $mod.$main
  11096. '']));
  11097. end;
  11098. procedure TTestModule.TestRecord_AnonymousFail;
  11099. begin
  11100. StartProgram(false);
  11101. Add([
  11102. 'var',
  11103. ' r: record x: word end;',
  11104. 'begin']);
  11105. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11106. nNotYetImplemented);
  11107. ConvertProgram;
  11108. end;
  11109. procedure TTestModule.TestAdvRecord_Function;
  11110. begin
  11111. StartProgram(false);
  11112. Parser.Options:=Parser.Options+[po_cassignments];
  11113. Add([
  11114. '{$modeswitch AdvancedRecords}',
  11115. 'type',
  11116. ' TPoint = record',
  11117. ' x,y: word;',
  11118. ' function Add(const apt: TPoint): TPoint;',
  11119. ' end;',
  11120. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11121. 'begin',
  11122. ' Result:=Self;',
  11123. ' Result.x+=apt.x;',
  11124. ' Result.y:=Result.y+apt.y;',
  11125. ' Self:=apt;',
  11126. 'end;',
  11127. 'var p,q: TPoint;',
  11128. 'begin',
  11129. ' p.add(q);',
  11130. ' p:=default(TPoint);',
  11131. ' p:=q;',
  11132. '']);
  11133. ConvertProgram;
  11134. CheckSource('TestAdvRecord_Function',
  11135. LinesToStr([ // statements
  11136. 'rtl.recNewT(this, "TPoint", function () {',
  11137. ' this.x = 0;',
  11138. ' this.y = 0;',
  11139. ' this.$eq = function (b) {',
  11140. ' return (this.x === b.x) && (this.y === b.y);',
  11141. ' };',
  11142. ' this.$assign = function (s) {',
  11143. ' this.x = s.x;',
  11144. ' this.y = s.y;',
  11145. ' return this;',
  11146. ' };',
  11147. ' this.Add = function (apt) {',
  11148. ' var Result = $mod.TPoint.$new();',
  11149. ' Result.$assign(this);',
  11150. ' Result.x += apt.x;',
  11151. ' Result.y = Result.y + apt.y;',
  11152. ' this.$assign(apt);',
  11153. ' return Result;',
  11154. ' };',
  11155. '});',
  11156. 'this.p = this.TPoint.$new();',
  11157. 'this.q = this.TPoint.$new();',
  11158. '']),
  11159. LinesToStr([ // $mod.$main
  11160. '$mod.p.Add($mod.q);',
  11161. '$mod.p.$assign($mod.TPoint.$new());',
  11162. '$mod.p.$assign($mod.q);',
  11163. '']));
  11164. end;
  11165. procedure TTestModule.TestAdvRecord_Property;
  11166. begin
  11167. StartProgram(false);
  11168. Add([
  11169. '{$modeswitch AdvancedRecords}',
  11170. 'type',
  11171. ' TPoint = record',
  11172. ' x,y: word;',
  11173. ' strict private',
  11174. ' function GetSize: longword;',
  11175. ' procedure SetSize(Value: longword);',
  11176. ' public',
  11177. ' property Size: longword read GetSize write SetSize;',
  11178. ' property Left: word read x write y;',
  11179. ' end;',
  11180. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11181. 'function TPoint.GetSize: longword;',
  11182. 'begin',
  11183. ' x:=y;',
  11184. ' Size:=Size;',
  11185. ' Left:=Left;',
  11186. 'end;',
  11187. 'procedure TPoint.SetSize(Value: longword);',
  11188. 'begin',
  11189. 'end;',
  11190. 'var p,q: TPoint;',
  11191. 'begin',
  11192. ' p.Size:=q.Size;',
  11193. ' p.Left:=q.Left;',
  11194. '']);
  11195. ConvertProgram;
  11196. CheckSource('TestAdvRecord_Property',
  11197. LinesToStr([ // statements
  11198. 'rtl.recNewT(this, "TPoint", function () {',
  11199. ' this.x = 0;',
  11200. ' this.y = 0;',
  11201. ' this.$eq = function (b) {',
  11202. ' return (this.x === b.x) && (this.y === b.y);',
  11203. ' };',
  11204. ' this.$assign = function (s) {',
  11205. ' this.x = s.x;',
  11206. ' this.y = s.y;',
  11207. ' return this;',
  11208. ' };',
  11209. ' this.GetSize = function () {',
  11210. ' var Result = 0;',
  11211. ' this.x = this.y;',
  11212. ' this.SetSize(this.GetSize());',
  11213. ' this.y = this.x;',
  11214. ' return Result;',
  11215. ' };',
  11216. ' this.SetSize = function (Value) {',
  11217. ' };',
  11218. '});',
  11219. 'this.SetSize = function (Value) {',
  11220. '};',
  11221. 'this.p = this.TPoint.$new();',
  11222. 'this.q = this.TPoint.$new();',
  11223. '']),
  11224. LinesToStr([ // $mod.$main
  11225. '$mod.p.SetSize($mod.q.GetSize());',
  11226. '$mod.p.y = $mod.q.x;',
  11227. '']));
  11228. end;
  11229. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11230. begin
  11231. StartProgram(false);
  11232. Add([
  11233. '{$modeswitch AdvancedRecords}',
  11234. 'type',
  11235. ' TPoint = record',
  11236. ' strict private',
  11237. ' function GetItems(Index: word): word;',
  11238. ' procedure SetItems(Index: word; Value: word);',
  11239. ' public',
  11240. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11241. ' end;',
  11242. 'function TPoint.GetItems(Index: word): word;',
  11243. 'begin',
  11244. ' Items[index]:=Items[index];',
  11245. ' self.Items[index]:=self.Items[index];',
  11246. 'end;',
  11247. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11248. 'begin',
  11249. 'end;',
  11250. 'var p: TPoint;',
  11251. 'begin',
  11252. ' p[1]:=p[2];',
  11253. ' p.Items[3]:=p.Items[4];',
  11254. '']);
  11255. ConvertProgram;
  11256. CheckSource('TestAdvRecord_PropertyDefault',
  11257. LinesToStr([ // statements
  11258. 'rtl.recNewT(this, "TPoint", function () {',
  11259. ' this.$eq = function (b) {',
  11260. ' return true;',
  11261. ' };',
  11262. ' this.$assign = function (s) {',
  11263. ' return this;',
  11264. ' };',
  11265. ' this.GetItems = function (Index) {',
  11266. ' var Result = 0;',
  11267. ' this.SetItems(Index, this.GetItems(Index));',
  11268. ' this.SetItems(Index, this.GetItems(Index));',
  11269. ' return Result;',
  11270. ' };',
  11271. ' this.SetItems = function (Index, Value) {',
  11272. ' };',
  11273. '});',
  11274. 'this.p = this.TPoint.$new();',
  11275. '']),
  11276. LinesToStr([ // $mod.$main
  11277. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11278. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11279. '']));
  11280. end;
  11281. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11282. begin
  11283. StartProgram(false);
  11284. Add([
  11285. '{$modeswitch AdvancedRecords}',
  11286. 'type',
  11287. ' TRec = record',
  11288. ' class var',
  11289. ' Fx: longint;',
  11290. ' Fy: longint;',
  11291. ' class function GetInt: longint; static;',
  11292. ' class procedure SetInt(Value: longint); static;',
  11293. ' class procedure DoIt; static;',
  11294. ' class property IntA: longint read Fx write Fy;',
  11295. ' class property IntB: longint read GetInt write SetInt;',
  11296. ' end;',
  11297. 'class function trec.getint: longint;',
  11298. 'begin',
  11299. ' result:=fx;',
  11300. 'end;',
  11301. 'class procedure trec.setint(value: longint);',
  11302. 'begin',
  11303. 'end;',
  11304. 'class procedure trec.doit;',
  11305. 'begin',
  11306. ' IntA:=IntA+1;',
  11307. ' IntB:=IntB+1;',
  11308. 'end;',
  11309. 'var r: trec;',
  11310. 'begin',
  11311. ' trec.inta:=trec.inta+1;',
  11312. ' if trec.intb=2 then;',
  11313. ' trec.intb:=trec.intb+2;',
  11314. ' trec.setint(trec.inta);',
  11315. ' r.inta:=r.inta+1;',
  11316. ' if r.intb=2 then;',
  11317. ' r.intb:=r.intb+2;',
  11318. ' r.setint(r.inta);']);
  11319. ConvertProgram;
  11320. CheckSource('TestAdvRecord_Property_ClassMethod',
  11321. LinesToStr([ // statements
  11322. 'rtl.recNewT(this, "TRec", function () {',
  11323. ' this.Fx = 0;',
  11324. ' this.Fy = 0;',
  11325. ' this.$eq = function (b) {',
  11326. ' return true;',
  11327. ' };',
  11328. ' this.$assign = function (s) {',
  11329. ' return this;',
  11330. ' };',
  11331. ' this.GetInt = function () {',
  11332. ' var Result = 0;',
  11333. ' Result = $mod.TRec.Fx;',
  11334. ' return Result;',
  11335. ' };',
  11336. ' this.SetInt = function (Value) {',
  11337. ' };',
  11338. ' this.DoIt = function () {',
  11339. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11340. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11341. ' };',
  11342. '}, true);',
  11343. 'this.r = this.TRec.$new();',
  11344. '']),
  11345. LinesToStr([ // $mod.$main
  11346. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11347. 'if ($mod.TRec.GetInt() === 2) ;',
  11348. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11349. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11350. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11351. 'if ($mod.r.GetInt() === 2) ;',
  11352. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11353. '$mod.r.SetInt($mod.r.Fx);',
  11354. '']));
  11355. end;
  11356. procedure TTestModule.TestAdvRecord_Const;
  11357. begin
  11358. StartProgram(false);
  11359. Add([
  11360. '{$modeswitch AdvancedRecords}',
  11361. 'type',
  11362. ' TArrInt = array[3..4] of longint;',
  11363. ' TPoint = record',
  11364. ' x,y: longint;',
  11365. ' class var Count: nativeint;',
  11366. ' end;',
  11367. ' TRec = record',
  11368. ' i: longint;',
  11369. ' a: array of longint;',
  11370. ' s: array[1..2] of longint;',
  11371. ' m: array[1..2,3..4] of longint;',
  11372. ' p: TPoint;',
  11373. ' end;',
  11374. ' TPoints = array of TPoint;',
  11375. 'const',
  11376. ' r: TRec = (',
  11377. ' i:1;',
  11378. ' a:(2,3);',
  11379. ' s:(4,5);',
  11380. ' m:( (11,12), (13,14) );',
  11381. ' p: (x:21)',
  11382. ' );',
  11383. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11384. 'begin']);
  11385. ConvertProgram;
  11386. CheckSource('TestAdvRecord_Const',
  11387. LinesToStr([ // statements
  11388. 'rtl.recNewT(this, "TPoint", function () {',
  11389. ' this.x = 0;',
  11390. ' this.y = 0;',
  11391. ' this.Count = 0;',
  11392. ' this.$eq = function (b) {',
  11393. ' return (this.x === b.x) && (this.y === b.y);',
  11394. ' };',
  11395. ' this.$assign = function (s) {',
  11396. ' this.x = s.x;',
  11397. ' this.y = s.y;',
  11398. ' return this;',
  11399. ' };',
  11400. '}, true);',
  11401. 'rtl.recNewT(this, "TRec", function () {',
  11402. ' this.i = 0;',
  11403. ' this.$new = function () {',
  11404. ' var r = Object.create(this);',
  11405. ' r.a = [];',
  11406. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11407. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11408. ' r.p = $mod.TPoint.$new();',
  11409. ' return r;',
  11410. ' };',
  11411. ' this.$eq = function (b) {',
  11412. ' 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);',
  11413. ' };',
  11414. ' this.$assign = function (s) {',
  11415. ' this.i = s.i;',
  11416. ' this.a = rtl.arrayRef(s.a);',
  11417. ' this.s = s.s.slice(0);',
  11418. ' this.m = s.m.slice(0);',
  11419. ' this.p.$assign(s.p);',
  11420. ' return this;',
  11421. ' };',
  11422. '});',
  11423. 'this.r = this.TRec.$clone({',
  11424. ' i: 1,',
  11425. ' a: [2, 3],',
  11426. ' s: [4, 5],',
  11427. ' m: [[11, 12], [13, 14]],',
  11428. ' p: this.TPoint.$clone({',
  11429. ' x: 21,',
  11430. ' y: 0',
  11431. ' })',
  11432. '});',
  11433. 'this.p = [this.TPoint.$clone({',
  11434. ' x: 1,',
  11435. ' y: 2',
  11436. '}), this.TPoint.$clone({',
  11437. ' x: 3,',
  11438. ' y: 4',
  11439. '})];',
  11440. '']),
  11441. LinesToStr([ // $mod.$main
  11442. '']));
  11443. end;
  11444. procedure TTestModule.TestAdvRecord_ExternalField;
  11445. begin
  11446. StartProgram(false);
  11447. Add([
  11448. '{$modeswitch AdvancedRecords}',
  11449. '{$modeswitch externalclass}',
  11450. 'type',
  11451. ' TCar = record',
  11452. ' public',
  11453. ' Intern: longint external name ''$Intern'';',
  11454. ' Intern2: longint external name ''$Intern2'';',
  11455. ' Bracket: longint external name ''["A B"]'';',
  11456. ' procedure DoIt;',
  11457. ' end;',
  11458. 'procedure tcar.doit;',
  11459. 'begin',
  11460. ' Intern:=Intern+1;',
  11461. ' Intern2:=Intern2+2;',
  11462. ' Bracket:=Bracket+3;',
  11463. 'end;',
  11464. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11465. 'begin',
  11466. ' Rec.intern:=Rec.intern+1;',
  11467. ' Rec.intern2:=Rec.intern2+2;',
  11468. ' Rec.Bracket:=Rec.Bracket+3;',
  11469. ' with Rec do begin',
  11470. ' intern:=intern+1;',
  11471. ' intern2:=intern2+2;',
  11472. ' Bracket:=Bracket+3;',
  11473. ' end;']);
  11474. ConvertProgram;
  11475. CheckSource('TestAdvRecord_ExternalField',
  11476. LinesToStr([ // statements
  11477. 'rtl.recNewT(this, "TCar", function () {',
  11478. ' this.$eq = function (b) {',
  11479. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11480. ' };',
  11481. ' this.$assign = function (s) {',
  11482. ' this.$Intern = s.$Intern;',
  11483. ' this.$Intern2 = s.$Intern2;',
  11484. ' this["A B"] = s["A B"];',
  11485. ' return this;',
  11486. ' };',
  11487. ' this.DoIt = function () {',
  11488. ' this.$Intern = this.$Intern + 1;',
  11489. ' this.$Intern2 = this.$Intern2 + 2;',
  11490. ' this["A B"] = this["A B"] + 3;',
  11491. ' };',
  11492. '});',
  11493. 'this.Rec = this.TCar.$clone({',
  11494. ' $Intern: 11,',
  11495. ' $Intern2: 12,',
  11496. ' "A B": 13',
  11497. '});',
  11498. '']),
  11499. LinesToStr([ // $mod.$main
  11500. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11501. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11502. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11503. 'var $with = $mod.Rec;',
  11504. '$with.$Intern = $with.$Intern + 1;',
  11505. '$with.$Intern2 = $with.$Intern2 + 2;',
  11506. '$with["A B"] = $with["A B"] + 3;',
  11507. '']));
  11508. end;
  11509. procedure TTestModule.TestAdvRecord_SubRecord;
  11510. begin
  11511. StartProgram(false);
  11512. Add([
  11513. '{$modeswitch AdvancedRecords}',
  11514. 'type',
  11515. ' TRec = record',
  11516. ' type',
  11517. ' TPoint = record',
  11518. ' x,y: longint;',
  11519. ' class var Count: nativeint;',
  11520. ' procedure DoIt;',
  11521. ' class procedure DoThat; static;',
  11522. ' end;',
  11523. ' var',
  11524. ' i: longint;',
  11525. ' p: TPoint;',
  11526. ' procedure DoSome;',
  11527. ' end;',
  11528. 'const',
  11529. ' r: TRec = (',
  11530. ' i:1;',
  11531. ' p: (x:21;y:22)',
  11532. ' );',
  11533. 'procedure TRec.DoSome;',
  11534. 'begin',
  11535. ' p.x:=p.y+1;',
  11536. ' p.Count:=p.Count+2;',
  11537. 'end;',
  11538. 'procedure TRec.TPoint.DoIt;',
  11539. 'begin',
  11540. ' Count:=Count+3;',
  11541. 'end;',
  11542. 'class procedure TRec.TPoint.DoThat;',
  11543. 'begin',
  11544. ' Count:=Count+4;',
  11545. 'end;',
  11546. 'begin']);
  11547. ConvertProgram;
  11548. CheckSource('TestAdvRecord_SubRecord',
  11549. LinesToStr([ // statements
  11550. 'rtl.recNewT(this, "TRec", function () {',
  11551. ' rtl.recNewT(this, "TPoint", function () {',
  11552. ' this.x = 0;',
  11553. ' this.y = 0;',
  11554. ' this.Count = 0;',
  11555. ' this.$eq = function (b) {',
  11556. ' return (this.x === b.x) && (this.y === b.y);',
  11557. ' };',
  11558. ' this.$assign = function (s) {',
  11559. ' this.x = s.x;',
  11560. ' this.y = s.y;',
  11561. ' return this;',
  11562. ' };',
  11563. ' this.DoIt = function () {',
  11564. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11565. ' };',
  11566. ' this.DoThat = function () {',
  11567. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11568. ' };',
  11569. ' }, true);',
  11570. ' this.i = 0;',
  11571. ' this.$new = function () {',
  11572. ' var r = Object.create(this);',
  11573. ' r.p = this.TPoint.$new();',
  11574. ' return r;',
  11575. ' };',
  11576. ' this.$eq = function (b) {',
  11577. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11578. ' };',
  11579. ' this.$assign = function (s) {',
  11580. ' this.i = s.i;',
  11581. ' this.p.$assign(s.p);',
  11582. ' return this;',
  11583. ' };',
  11584. ' this.DoSome = function () {',
  11585. ' this.p.x = this.p.y + 1;',
  11586. ' this.TPoint.Count = this.p.Count + 2;',
  11587. ' };',
  11588. '}, true);',
  11589. 'this.r = this.TRec.$clone({',
  11590. ' i: 1,',
  11591. ' p: this.TRec.TPoint.$clone({',
  11592. ' x: 21,',
  11593. ' y: 22',
  11594. ' })',
  11595. '});',
  11596. '']),
  11597. LinesToStr([ // $mod.$main
  11598. '']));
  11599. end;
  11600. procedure TTestModule.TestAdvRecord_SubClass;
  11601. begin
  11602. StartProgram(false);
  11603. Add([
  11604. '{$modeswitch AdvancedRecords}',
  11605. 'type',
  11606. ' TObject = class end;',
  11607. ' TPoint = record',
  11608. ' type',
  11609. ' TBird = class',
  11610. ' procedure DoIt;',
  11611. ' class procedure Glob;',
  11612. ' end;',
  11613. ' procedure DoIt(b: TBird);',
  11614. ' end;',
  11615. 'procedure TPoint.TBird.DoIt;',
  11616. 'begin',
  11617. ' doit;',
  11618. ' self.doit;',
  11619. ' glob;',
  11620. ' self.glob;',
  11621. 'end;',
  11622. 'class procedure TPoint.TBird.Glob;',
  11623. 'begin',
  11624. ' glob;',
  11625. ' self.glob;',
  11626. 'end;',
  11627. 'procedure TPoint.DoIt(b: TBird);',
  11628. 'begin',
  11629. ' b.doit;',
  11630. ' b.glob;',
  11631. ' TBird.glob;',
  11632. 'end;',
  11633. 'begin',
  11634. '']);
  11635. ConvertProgram;
  11636. CheckSource('TestAdvRecord_SubClass',
  11637. LinesToStr([ // statements
  11638. 'rtl.createClass(this, "TObject", null, function () {',
  11639. ' this.$init = function () {',
  11640. ' };',
  11641. ' this.$final = function () {',
  11642. ' };',
  11643. '});',
  11644. 'rtl.recNewT(this, "TPoint", function () {',
  11645. ' rtl.createClass(this, "TBird", this.TObject, function () {',
  11646. ' this.DoIt = function () {',
  11647. ' this.DoIt();',
  11648. ' this.DoIt();',
  11649. ' this.$class.Glob();',
  11650. ' this.$class.Glob();',
  11651. ' };',
  11652. ' this.Glob = function () {',
  11653. ' this.Glob();',
  11654. ' this.Glob();',
  11655. ' };',
  11656. ' }, "TPoint.TBird");',
  11657. ' this.$eq = function (b) {',
  11658. ' return true;',
  11659. ' };',
  11660. ' this.$assign = function (s) {',
  11661. ' return this;',
  11662. ' };',
  11663. ' this.DoIt = function (b) {',
  11664. ' b.DoIt();',
  11665. ' b.$class.Glob();',
  11666. ' this.TBird.Glob();',
  11667. ' };',
  11668. '}, true);',
  11669. '']),
  11670. LinesToStr([ // $mod.$main
  11671. '']));
  11672. end;
  11673. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11674. begin
  11675. StartProgram(false);
  11676. Add([
  11677. '{$modeswitch AdvancedRecords}',
  11678. 'type',
  11679. ' IUnknown = interface end;',
  11680. ' TPoint = record',
  11681. ' type IBird = interface end;',
  11682. ' end;',
  11683. 'begin',
  11684. '']);
  11685. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11686. nNotYetImplemented);
  11687. ParseProgram;
  11688. end;
  11689. procedure TTestModule.TestAdvRecord_Constructor;
  11690. begin
  11691. StartProgram(false);
  11692. Add([
  11693. '{$modeswitch AdvancedRecords}',
  11694. 'type',
  11695. ' TPoint = record',
  11696. ' x,y: longint;',
  11697. ' constructor Create(ax: longint; ay: longint = -1);',
  11698. ' end;',
  11699. 'constructor tpoint.create(ax,ay: longint);',
  11700. 'begin',
  11701. ' x:=ax;',
  11702. ' self.y:=ay;',
  11703. 'end;',
  11704. 'var r: TPoint;',
  11705. 'begin',
  11706. ' r:=TPoint.Create(1,2);',
  11707. ' with TPoint do r:=Create(1,2);',
  11708. ' r.Create(3);',
  11709. ' r:=r.Create(4);',
  11710. '']);
  11711. ConvertProgram;
  11712. CheckSource('TestAdvRecord_Constructor',
  11713. LinesToStr([ // statements
  11714. 'rtl.recNewT(this, "TPoint", function () {',
  11715. ' this.x = 0;',
  11716. ' this.y = 0;',
  11717. ' this.$eq = function (b) {',
  11718. ' return (this.x === b.x) && (this.y === b.y);',
  11719. ' };',
  11720. ' this.$assign = function (s) {',
  11721. ' this.x = s.x;',
  11722. ' this.y = s.y;',
  11723. ' return this;',
  11724. ' };',
  11725. ' this.Create = function (ax, ay) {',
  11726. ' this.x = ax;',
  11727. ' this.y = ay;',
  11728. ' return this;',
  11729. ' };',
  11730. '}, true);',
  11731. 'this.r = this.TPoint.$new();',
  11732. '']),
  11733. LinesToStr([ // $mod.$main
  11734. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11735. 'var $with = $mod.TPoint;',
  11736. '$mod.r.$assign($with.$new().Create(1, 2));',
  11737. '$mod.r.Create(3, -1);',
  11738. '$mod.r.$assign($mod.r.Create(4, -1));',
  11739. '']));
  11740. end;
  11741. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11742. begin
  11743. StartProgram(false);
  11744. Add([
  11745. '{$modeswitch AdvancedRecords}',
  11746. 'type',
  11747. ' TPoint = record',
  11748. ' class var x: longint;',
  11749. ' class procedure Fly; static;',
  11750. ' class constructor Init;',
  11751. ' end;',
  11752. 'var count: word;',
  11753. 'class procedure Tpoint.Fly;',
  11754. 'begin',
  11755. 'end;',
  11756. 'class constructor tpoint.init;',
  11757. 'begin',
  11758. ' count:=count+1;',
  11759. ' x:=x+3;',
  11760. ' tpoint.x:=tpoint.x+4;',
  11761. ' fly;',
  11762. ' tpoint.fly;',
  11763. 'end;',
  11764. 'var r: TPoint;',
  11765. 'begin',
  11766. ' r.x:=r.x+10;',
  11767. ' r.Fly;',
  11768. ' r.Fly();',
  11769. '']);
  11770. ConvertProgram;
  11771. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11772. LinesToStr([ // statements
  11773. 'rtl.recNewT(this, "TPoint", function () {',
  11774. ' this.x = 0;',
  11775. ' this.$eq = function (b) {',
  11776. ' return true;',
  11777. ' };',
  11778. ' this.$assign = function (s) {',
  11779. ' return this;',
  11780. ' };',
  11781. ' this.Fly = function () {',
  11782. ' };',
  11783. '}, true);',
  11784. 'this.count = 0;',
  11785. 'this.r = this.TPoint.$new();',
  11786. '']),
  11787. LinesToStr([ // $mod.$main
  11788. '(function () {',
  11789. ' $mod.count = $mod.count + 1;',
  11790. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11791. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11792. ' $mod.TPoint.Fly();',
  11793. ' $mod.TPoint.Fly();',
  11794. '})();',
  11795. '$mod.TPoint.x = $mod.r.x + 10;',
  11796. '$mod.r.Fly();',
  11797. '$mod.r.Fly();',
  11798. '']));
  11799. end;
  11800. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11801. begin
  11802. StartUnit(false);
  11803. Add([
  11804. 'interface',
  11805. '{$modeswitch AdvancedRecords}',
  11806. 'type',
  11807. ' TPoint = record',
  11808. ' class var x: longint;',
  11809. ' class procedure Fly; static;',
  11810. ' class constructor Init;',
  11811. ' end;',
  11812. 'implementation',
  11813. 'var count: word;',
  11814. 'class procedure Tpoint.Fly;',
  11815. 'begin',
  11816. 'end;',
  11817. 'class constructor tpoint.init;',
  11818. 'begin',
  11819. ' count:=count+1;',
  11820. ' x:=3;',
  11821. ' tpoint.x:=4;',
  11822. ' fly;',
  11823. ' tpoint.fly;',
  11824. 'end;',
  11825. '']);
  11826. ConvertUnit;
  11827. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11828. LinesToStr([ // statements
  11829. 'var $impl = $mod.$impl;',
  11830. 'rtl.recNewT(this, "TPoint", function () {',
  11831. ' this.x = 0;',
  11832. ' this.$eq = function (b) {',
  11833. ' return true;',
  11834. ' };',
  11835. ' this.$assign = function (s) {',
  11836. ' return this;',
  11837. ' };',
  11838. ' this.Fly = function () {',
  11839. ' };',
  11840. '}, true);',
  11841. '']),
  11842. LinesToStr([ // $mod.$init
  11843. '(function () {',
  11844. ' $impl.count = $impl.count + 1;',
  11845. ' $mod.TPoint.x = 3;',
  11846. ' $mod.TPoint.x = 4;',
  11847. ' $mod.TPoint.Fly();',
  11848. ' $mod.TPoint.Fly();',
  11849. '})();',
  11850. '']),
  11851. LinesToStr([ // $mod.$main
  11852. '$impl.count = 0;',
  11853. '']));
  11854. end;
  11855. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11856. begin
  11857. StartProgram(false);
  11858. Add(['type',
  11859. ' TObject = class',
  11860. ' public',
  11861. ' constructor Create;',
  11862. ' destructor Destroy;',
  11863. ' end;',
  11864. ' TBird = TObject;',
  11865. 'constructor tobject.create;',
  11866. 'begin end;',
  11867. 'destructor tobject.destroy;',
  11868. 'begin end;',
  11869. 'var Obj: tobject;',
  11870. 'begin',
  11871. ' obj:=tobject.create;',
  11872. ' obj:=tobject.create();',
  11873. ' obj:=tbird.create;',
  11874. ' obj:=tbird.create();',
  11875. ' obj:=obj.create();',
  11876. ' obj.destroy;',
  11877. '']);
  11878. ConvertProgram;
  11879. CheckSource('TestClass_TObjectDefaultConstructor',
  11880. LinesToStr([ // statements
  11881. 'rtl.createClass(this,"TObject",null,function(){',
  11882. ' this.$init = function () {',
  11883. ' };',
  11884. ' this.$final = function () {',
  11885. ' };',
  11886. ' this.Create = function(){',
  11887. ' return this;',
  11888. ' };',
  11889. ' this.Destroy = function(){',
  11890. ' };',
  11891. '});',
  11892. 'this.Obj = null;'
  11893. ]),
  11894. LinesToStr([ // $mod.$main
  11895. '$mod.Obj = $mod.TObject.$create("Create");',
  11896. '$mod.Obj = $mod.TObject.$create("Create");',
  11897. '$mod.Obj = $mod.TObject.$create("Create");',
  11898. '$mod.Obj = $mod.TObject.$create("Create");',
  11899. '$mod.Obj = $mod.Obj.Create();',
  11900. '$mod.Obj.$destroy("Destroy");',
  11901. '']));
  11902. end;
  11903. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11904. begin
  11905. StartProgram(false);
  11906. Add('type');
  11907. Add(' TObject = class');
  11908. Add(' public');
  11909. Add(' constructor Create(Par: longint);');
  11910. Add(' end;');
  11911. Add('constructor tobject.create(par: longint);');
  11912. Add('begin end;');
  11913. Add('var Obj: tobject;');
  11914. Add('begin');
  11915. Add(' obj:=tobject.create(3);');
  11916. ConvertProgram;
  11917. CheckSource('TestClass_TObjectConstructorWithParams',
  11918. LinesToStr([ // statements
  11919. 'rtl.createClass(this,"TObject",null,function(){',
  11920. ' this.$init = function () {',
  11921. ' };',
  11922. ' this.$final = function () {',
  11923. ' };',
  11924. ' this.Create = function(Par){',
  11925. ' return this;',
  11926. ' };',
  11927. '});',
  11928. 'this.Obj = null;'
  11929. ]),
  11930. LinesToStr([ // $mod.$main
  11931. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11932. ]));
  11933. end;
  11934. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11935. begin
  11936. StartProgram(false);
  11937. Add('type');
  11938. Add(' TObject = class');
  11939. Add(' public');
  11940. Add(' constructor Create;');
  11941. Add(' end;');
  11942. Add(' TTest = class(TObject)');
  11943. Add(' public');
  11944. Add(' constructor Create(const Par: longint = 1);');
  11945. Add(' end;');
  11946. Add('constructor tobject.create;');
  11947. Add('begin end;');
  11948. Add('constructor ttest.create(const par: longint);');
  11949. Add('begin end;');
  11950. Add('var t: ttest;');
  11951. Add('begin');
  11952. Add(' t:=ttest.create;');
  11953. Add(' t:=ttest.create(2);');
  11954. ConvertProgram;
  11955. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11956. LinesToStr([ // statements
  11957. 'rtl.createClass(this,"TObject",null,function(){',
  11958. ' this.$init = function () {',
  11959. ' };',
  11960. ' this.$final = function () {',
  11961. ' };',
  11962. ' this.Create = function(){',
  11963. ' return this;',
  11964. ' };',
  11965. '});',
  11966. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  11967. ' this.Create$1 = function (Par) {',
  11968. ' return this;',
  11969. ' };',
  11970. '});',
  11971. 'this.t = null;'
  11972. ]),
  11973. LinesToStr([ // $mod.$main
  11974. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11975. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11976. ]));
  11977. end;
  11978. procedure TTestModule.TestClass_Var;
  11979. begin
  11980. StartProgram(false);
  11981. Add([
  11982. 'type',
  11983. ' TObject = class',
  11984. ' public',
  11985. ' vI: longint;',
  11986. ' constructor Create(Par: longint);',
  11987. ' end;',
  11988. 'constructor tobject.create(par: longint);',
  11989. 'begin',
  11990. ' vi:=par+3',
  11991. 'end;',
  11992. 'var Obj: tobject;',
  11993. 'begin',
  11994. ' obj:=tobject.create(4);',
  11995. ' obj.vi:=obj.VI+5;']);
  11996. ConvertProgram;
  11997. CheckSource('TestClass_Var',
  11998. LinesToStr([ // statements
  11999. 'rtl.createClass(this,"TObject",null,function(){',
  12000. ' this.$init = function () {',
  12001. ' this.vI = 0;',
  12002. ' };',
  12003. ' this.$final = function () {',
  12004. ' };',
  12005. ' this.Create = function(Par){',
  12006. ' this.vI = Par+3;',
  12007. ' return this;',
  12008. ' };',
  12009. '});',
  12010. 'this.Obj = null;'
  12011. ]),
  12012. LinesToStr([ // $mod.$main
  12013. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12014. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12015. ]));
  12016. end;
  12017. procedure TTestModule.TestClass_Method;
  12018. begin
  12019. StartProgram(false);
  12020. Add('type');
  12021. Add(' TObject = class');
  12022. Add(' public');
  12023. Add(' vI: longint;');
  12024. Add(' Sub: TObject;');
  12025. Add(' constructor Create;');
  12026. Add(' function GetIt(Par: longint): tobject;');
  12027. Add(' end;');
  12028. Add('constructor tobject.create; begin end;');
  12029. Add('function tobject.getit(par: longint): tobject;');
  12030. Add('begin');
  12031. Add(' Self.vi:=par+3;');
  12032. Add(' Result:=self.sub;');
  12033. Add('end;');
  12034. Add('var Obj: tobject;');
  12035. Add('begin');
  12036. Add(' obj:=tobject.create;');
  12037. Add(' obj.getit(4);');
  12038. Add(' obj.sub.sub:=nil;');
  12039. Add(' obj.sub.getit(5);');
  12040. Add(' obj.sub.getit(6).SUB:=nil;');
  12041. Add(' obj.sub.getit(7).GETIT(8);');
  12042. Add(' obj.sub.getit(9).SuB.getit(10);');
  12043. ConvertProgram;
  12044. CheckSource('TestClass_Method',
  12045. LinesToStr([ // statements
  12046. 'rtl.createClass(this,"TObject",null,function(){',
  12047. ' this.$init = function () {',
  12048. ' this.vI = 0;',
  12049. ' this.Sub = null;',
  12050. ' };',
  12051. ' this.$final = function () {',
  12052. ' this.Sub = undefined;',
  12053. ' };',
  12054. ' this.Create = function(){',
  12055. ' return this;',
  12056. ' };',
  12057. ' this.GetIt = function(Par){',
  12058. ' var Result = null;',
  12059. ' this.vI = Par + 3;',
  12060. ' Result = this.Sub;',
  12061. ' return Result;',
  12062. ' };',
  12063. '});',
  12064. 'this.Obj = null;'
  12065. ]),
  12066. LinesToStr([ // $mod.$main
  12067. '$mod.Obj = $mod.TObject.$create("Create");',
  12068. '$mod.Obj.GetIt(4);',
  12069. '$mod.Obj.Sub.Sub=null;',
  12070. '$mod.Obj.Sub.GetIt(5);',
  12071. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12072. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12073. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12074. ]));
  12075. end;
  12076. procedure TTestModule.TestClass_Implementation;
  12077. begin
  12078. StartUnit(false);
  12079. Add([
  12080. 'interface',
  12081. 'type',
  12082. ' TObject = class',
  12083. ' constructor Create;',
  12084. ' end;',
  12085. 'implementation',
  12086. 'type',
  12087. ' TIntClass = class',
  12088. ' constructor Create; reintroduce;',
  12089. ' class procedure DoGlob;',
  12090. ' end;',
  12091. 'constructor tintclass.create;',
  12092. 'begin',
  12093. ' inherited;',
  12094. ' inherited create;',
  12095. ' doglob;',
  12096. 'end;',
  12097. 'class procedure tintclass.doglob;',
  12098. 'begin',
  12099. 'end;',
  12100. 'constructor tobject.create;',
  12101. 'var',
  12102. ' iC: tintclass;',
  12103. 'begin',
  12104. ' ic:=tintclass.create;',
  12105. ' tintclass.doglob;',
  12106. ' ic.doglob;',
  12107. 'end;',
  12108. 'initialization',
  12109. ' tintclass.doglob;',
  12110. '']);
  12111. ConvertUnit;
  12112. CheckSource('TestClass_Implementation',
  12113. LinesToStr([ // statements
  12114. 'var $impl = $mod.$impl;',
  12115. 'rtl.createClass(this, "TObject", null, function () {',
  12116. ' this.$init = function () {',
  12117. ' };',
  12118. ' this.$final = function () {',
  12119. ' };',
  12120. ' this.Create = function () {',
  12121. ' var iC = null;',
  12122. ' iC = $impl.TIntClass.$create("Create$1");',
  12123. ' $impl.TIntClass.DoGlob();',
  12124. ' iC.$class.DoGlob();',
  12125. ' return this;',
  12126. ' };',
  12127. '});',
  12128. '']),
  12129. LinesToStr([ // $mod.$main
  12130. '$impl.TIntClass.DoGlob();',
  12131. '']),
  12132. LinesToStr([
  12133. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12134. ' this.Create$1 = function () {',
  12135. ' $mod.TObject.Create.call(this);',
  12136. ' $mod.TObject.Create.call(this);',
  12137. ' this.$class.DoGlob();',
  12138. ' return this;',
  12139. ' };',
  12140. ' this.DoGlob = function () {',
  12141. ' };',
  12142. '});',
  12143. '']));
  12144. end;
  12145. procedure TTestModule.TestClass_Inheritance;
  12146. begin
  12147. StartProgram(false);
  12148. Add('type');
  12149. Add(' TObject = class');
  12150. Add(' public');
  12151. Add(' constructor Create;');
  12152. Add(' end;');
  12153. Add(' TClassA = class');
  12154. Add(' end;');
  12155. Add(' TClassB = class(TObject)');
  12156. Add(' procedure ProcB;');
  12157. Add(' end;');
  12158. Add('constructor tobject.create; begin end;');
  12159. Add('procedure tclassb.procb; begin end;');
  12160. Add('var');
  12161. Add(' oO: TObject;');
  12162. Add(' oA: TClassA;');
  12163. Add(' oB: TClassB;');
  12164. Add('begin');
  12165. Add(' oO:=tobject.Create;');
  12166. Add(' oA:=tclassa.Create;');
  12167. Add(' ob:=tclassb.Create;');
  12168. Add(' if oo is tclassa then ;');
  12169. Add(' ob:=oo as tclassb;');
  12170. Add(' (oo as tclassb).procb;');
  12171. ConvertProgram;
  12172. CheckSource('TestClass_Inheritance',
  12173. LinesToStr([ // statements
  12174. 'rtl.createClass(this,"TObject",null,function(){',
  12175. ' this.$init = function () {',
  12176. ' };',
  12177. ' this.$final = function () {',
  12178. ' };',
  12179. ' this.Create = function () {',
  12180. ' return this;',
  12181. ' };',
  12182. '});',
  12183. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12184. '});',
  12185. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12186. ' this.ProcB = function () {',
  12187. ' };',
  12188. '});',
  12189. 'this.oO = null;',
  12190. 'this.oA = null;',
  12191. 'this.oB = null;'
  12192. ]),
  12193. LinesToStr([ // $mod.$main
  12194. '$mod.oO = $mod.TObject.$create("Create");',
  12195. '$mod.oA = $mod.TClassA.$create("Create");',
  12196. '$mod.oB = $mod.TClassB.$create("Create");',
  12197. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12198. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12199. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12200. ]));
  12201. end;
  12202. procedure TTestModule.TestClass_TypeAlias;
  12203. begin
  12204. StartProgram(false);
  12205. Add([
  12206. '{$interfaces corba}',
  12207. 'type',
  12208. ' IObject = interface',
  12209. ' end;',
  12210. ' IBird = type IObject;',
  12211. ' TObject = class',
  12212. ' end;',
  12213. ' TBird = type TObject;',
  12214. 'var',
  12215. ' oObj: TObject;',
  12216. ' oBird: TBird;',
  12217. ' IntfObj: IObject;',
  12218. ' IntfBird: IBird;',
  12219. 'begin',
  12220. ' oObj:=oBird;',
  12221. '']);
  12222. ConvertProgram;
  12223. CheckSource('TestClass_TypeAlias',
  12224. LinesToStr([ // statements
  12225. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12226. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12227. 'rtl.createClass(this, "TObject", null, function () {',
  12228. ' this.$init = function () {',
  12229. ' };',
  12230. ' this.$final = function () {',
  12231. ' };',
  12232. '});',
  12233. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12234. '});',
  12235. 'this.oObj = null;',
  12236. 'this.oBird = null;',
  12237. 'this.IntfObj = null;',
  12238. 'this.IntfBird = null;',
  12239. '']),
  12240. LinesToStr([ // $mod.$main
  12241. '$mod.oObj = $mod.oBird;',
  12242. '']));
  12243. end;
  12244. procedure TTestModule.TestClass_AbstractMethod;
  12245. begin
  12246. StartProgram(false);
  12247. Add('type');
  12248. Add(' TObject = class');
  12249. Add(' public');
  12250. Add(' procedure DoIt; virtual; abstract;');
  12251. Add(' end;');
  12252. Add('begin');
  12253. ConvertProgram;
  12254. CheckSource('TestClass_AbstractMethod',
  12255. LinesToStr([ // statements
  12256. 'rtl.createClass(this,"TObject",null,function(){',
  12257. ' this.$init = function () {',
  12258. ' };',
  12259. ' this.$final = function () {',
  12260. ' };',
  12261. '});'
  12262. ]),
  12263. LinesToStr([ // this.$main
  12264. ''
  12265. ]));
  12266. end;
  12267. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12268. begin
  12269. StartProgram(false);
  12270. Add([
  12271. 'type',
  12272. ' TObject = class',
  12273. ' procedure DoAbstract; virtual; abstract;',
  12274. ' procedure DoVirtual; virtual;',
  12275. ' procedure DoIt;',
  12276. ' end;',
  12277. ' TA = class',
  12278. ' procedure doabstract; override;',
  12279. ' procedure dovirtual; override;',
  12280. ' procedure DoSome;',
  12281. ' end;',
  12282. 'procedure tobject.dovirtual;',
  12283. 'begin',
  12284. ' inherited; // call non existing ancestor -> ignore silently',
  12285. 'end;',
  12286. 'procedure tobject.doit;',
  12287. 'begin',
  12288. 'end;',
  12289. 'procedure ta.doabstract;',
  12290. 'begin',
  12291. ' inherited dovirtual; // call TObject.DoVirtual',
  12292. 'end;',
  12293. 'procedure ta.dovirtual;',
  12294. 'begin',
  12295. ' inherited; // call TObject.DoVirtual',
  12296. ' inherited dovirtual; // call TObject.DoVirtual',
  12297. ' inherited dovirtual(); // call TObject.DoVirtual',
  12298. ' doit;',
  12299. ' doit();',
  12300. 'end;',
  12301. 'procedure ta.dosome;',
  12302. 'begin',
  12303. ' inherited; // call non existing ancestor method -> silently ignore',
  12304. 'end;',
  12305. 'begin']);
  12306. ConvertProgram;
  12307. CheckSource('TestClass_CallInherited_ProcNoParams',
  12308. LinesToStr([ // statements
  12309. 'rtl.createClass(this,"TObject",null,function(){',
  12310. ' this.$init = function () {',
  12311. ' };',
  12312. ' this.$final = function () {',
  12313. ' };',
  12314. ' this.DoVirtual = function () {',
  12315. ' };',
  12316. ' this.DoIt = function () {',
  12317. ' };',
  12318. '});',
  12319. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12320. ' this.DoAbstract = function () {',
  12321. ' $mod.TObject.DoVirtual.call(this);',
  12322. ' };',
  12323. ' this.DoVirtual = function () {',
  12324. ' $mod.TObject.DoVirtual.call(this);',
  12325. ' $mod.TObject.DoVirtual.call(this);',
  12326. ' $mod.TObject.DoVirtual.call(this);',
  12327. ' this.DoIt();',
  12328. ' this.DoIt();',
  12329. ' };',
  12330. ' this.DoSome = function () {',
  12331. ' };',
  12332. '});'
  12333. ]),
  12334. LinesToStr([ // this.$main
  12335. ''
  12336. ]));
  12337. end;
  12338. procedure TTestModule.TestClass_CallInherited_WithParams;
  12339. begin
  12340. StartProgram(false);
  12341. Add([
  12342. 'type',
  12343. ' TObject = class',
  12344. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12345. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12346. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12347. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12348. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12349. ' end;',
  12350. ' TClassA = class',
  12351. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12352. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12353. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12354. ' end;',
  12355. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12356. 'begin',
  12357. 'end;',
  12358. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12359. 'begin',
  12360. 'end;',
  12361. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12362. 'begin',
  12363. 'end;',
  12364. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12365. 'begin',
  12366. 'end;',
  12367. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12368. 'begin',
  12369. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12370. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12371. 'end;',
  12372. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12373. 'begin',
  12374. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12375. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12376. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12377. ' doit(pa,pb);',
  12378. ' doit(pa);',
  12379. ' doit2(pa);',
  12380. ' doit2;',
  12381. 'end;',
  12382. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12383. 'begin',
  12384. ' pa:=inherited;',
  12385. 'end;',
  12386. 'begin']);
  12387. ConvertProgram;
  12388. CheckSource('TestClass_CallInherited_WithParams',
  12389. LinesToStr([ // statements
  12390. 'rtl.createClass(this,"TObject",null,function(){',
  12391. ' this.$init = function () {',
  12392. ' };',
  12393. ' this.$final = function () {',
  12394. ' };',
  12395. ' this.DoVirtual = function (pA,pB) {',
  12396. ' };',
  12397. ' this.DoIt = function (pA,pB) {',
  12398. ' };',
  12399. ' this.DoIt2 = function (pA,pB) {',
  12400. ' };',
  12401. ' this.GetIt = function (pA, pB) {',
  12402. ' var Result = 0;',
  12403. ' return Result;',
  12404. ' };',
  12405. '});',
  12406. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12407. ' this.DoAbstract = function (pA,pB) {',
  12408. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12409. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12410. ' };',
  12411. ' this.DoVirtual = function (pA,pB) {',
  12412. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12413. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12414. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12415. ' this.DoIt(pA,pB);',
  12416. ' this.DoIt(pA,0);',
  12417. ' this.DoIt2(pA,2);',
  12418. ' this.DoIt2(1,2);',
  12419. ' };',
  12420. ' this.GetIt$1 = function (pA, pB) {',
  12421. ' var Result = 0;',
  12422. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12423. ' return Result;',
  12424. ' };',
  12425. '});'
  12426. ]),
  12427. LinesToStr([ // this.$main
  12428. ''
  12429. ]));
  12430. end;
  12431. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12432. begin
  12433. StartProgram(false);
  12434. Add('type');
  12435. Add(' TObject = class');
  12436. Add(' constructor Create; virtual;');
  12437. Add(' constructor CreateWithB(b: boolean);');
  12438. Add(' end;');
  12439. Add(' TA = class');
  12440. Add(' constructor Create; override;');
  12441. Add(' constructor CreateWithC(c: char);');
  12442. Add(' procedure DoIt;');
  12443. Add(' class function DoSome: TObject;');
  12444. Add(' end;');
  12445. Add('constructor tobject.create;');
  12446. Add('begin');
  12447. Add(' inherited; // call non existing ancestor -> ignore silently');
  12448. Add('end;');
  12449. Add('constructor tobject.createwithb(b: boolean);');
  12450. Add('begin');
  12451. Add(' inherited; // call non existing ancestor -> ignore silently');
  12452. Add(' create; // normal call');
  12453. Add('end;');
  12454. Add('constructor ta.create;');
  12455. Add('begin');
  12456. Add(' inherited; // normal call TObject.Create');
  12457. Add(' inherited create; // normal call TObject.Create');
  12458. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12459. Add('end;');
  12460. Add('constructor ta.createwithc(c: char);');
  12461. Add('begin');
  12462. Add(' inherited create; // call TObject.Create');
  12463. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12464. Add(' doit;');
  12465. Add(' doit();');
  12466. Add(' dosome;');
  12467. Add('end;');
  12468. Add('procedure ta.doit;');
  12469. Add('begin');
  12470. Add(' create; // normal call');
  12471. Add(' createwithb(false); // normal call');
  12472. Add(' createwithc(''c''); // normal call');
  12473. Add('end;');
  12474. Add('class function ta.dosome: TObject;');
  12475. Add('begin');
  12476. Add(' Result:=create; // constructor');
  12477. Add(' Result:=createwithb(true); // constructor');
  12478. Add(' Result:=createwithc(''c''); // constructor');
  12479. Add('end;');
  12480. Add('begin');
  12481. ConvertProgram;
  12482. CheckSource('TestClass_CallInheritedConstructor',
  12483. LinesToStr([ // statements
  12484. 'rtl.createClass(this,"TObject",null,function(){',
  12485. ' this.$init = function () {',
  12486. ' };',
  12487. ' this.$final = function () {',
  12488. ' };',
  12489. ' this.Create = function () {',
  12490. ' return this;',
  12491. ' };',
  12492. ' this.CreateWithB = function (b) {',
  12493. ' this.Create();',
  12494. ' return this;',
  12495. ' };',
  12496. '});',
  12497. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12498. ' this.Create = function () {',
  12499. ' $mod.TObject.Create.call(this);',
  12500. ' $mod.TObject.Create.call(this);',
  12501. ' $mod.TObject.CreateWithB.call(this, false);',
  12502. ' return this;',
  12503. ' };',
  12504. ' this.CreateWithC = function (c) {',
  12505. ' $mod.TObject.Create.call(this);',
  12506. ' $mod.TObject.CreateWithB.call(this, true);',
  12507. ' this.DoIt();',
  12508. ' this.DoIt();',
  12509. ' this.$class.DoSome();',
  12510. ' return this;',
  12511. ' };',
  12512. ' this.DoIt = function () {',
  12513. ' this.Create();',
  12514. ' this.CreateWithB(false);',
  12515. ' this.CreateWithC("c");',
  12516. ' };',
  12517. ' this.DoSome = function () {',
  12518. ' var Result = null;',
  12519. ' Result = this.$create("Create");',
  12520. ' Result = this.$create("CreateWithB", [true]);',
  12521. ' Result = this.$create("CreateWithC", ["c"]);',
  12522. ' return Result;',
  12523. ' };',
  12524. '});'
  12525. ]),
  12526. LinesToStr([ // this.$main
  12527. ''
  12528. ]));
  12529. end;
  12530. procedure TTestModule.TestClass_ClassVar_Assign;
  12531. begin
  12532. StartProgram(false);
  12533. Add([
  12534. 'type',
  12535. ' TObject = class',
  12536. ' public',
  12537. ' class var vI: longint;',
  12538. ' class var Sub: TObject;',
  12539. ' constructor Create;',
  12540. ' class function GetIt(var Par: longint): tobject;',
  12541. ' end;',
  12542. 'constructor tobject.create;',
  12543. 'begin',
  12544. ' vi:=vi+1;',
  12545. ' Self.vi:=Self.vi+1;',
  12546. ' inc(vi);',
  12547. 'end;',
  12548. 'class function tobject.getit(var par: longint): tobject;',
  12549. 'begin',
  12550. ' vi:=vi+3;',
  12551. ' Self.vi:=Self.vi+4;',
  12552. ' inc(vi);',
  12553. ' Result:=self.sub;',
  12554. ' GetIt(vi);',
  12555. 'end;',
  12556. 'var Obj: tobject;',
  12557. 'begin',
  12558. ' obj:=tobject.create;',
  12559. ' tobject.vi:=3;',
  12560. ' if tobject.vi=4 then ;',
  12561. ' tobject.sub:=nil;',
  12562. ' obj.sub:=nil;',
  12563. ' obj.sub.sub:=nil;']);
  12564. ConvertProgram;
  12565. CheckSource('TestClass_ClassVar_Assign',
  12566. LinesToStr([ // statements
  12567. 'rtl.createClass(this,"TObject",null,function(){',
  12568. ' this.vI = 0;',
  12569. ' this.Sub = null;',
  12570. ' this.$init = function () {',
  12571. ' };',
  12572. ' this.$final = function () {',
  12573. ' };',
  12574. ' this.Create = function(){',
  12575. ' $mod.TObject.vI = this.vI+1;',
  12576. ' $mod.TObject.vI = this.vI+1;',
  12577. ' $mod.TObject.vI += 1;',
  12578. ' return this;',
  12579. ' };',
  12580. ' this.GetIt = function(Par){',
  12581. ' var Result = null;',
  12582. ' $mod.TObject.vI = this.vI + 3;',
  12583. ' $mod.TObject.vI = this.vI + 4;',
  12584. ' $mod.TObject.vI += 1;',
  12585. ' Result = this.Sub;',
  12586. ' this.GetIt({',
  12587. ' p: $mod.TObject,',
  12588. ' get: function () {',
  12589. ' return this.p.vI;',
  12590. ' },',
  12591. ' set: function (v) {',
  12592. ' this.p.vI = v;',
  12593. ' }',
  12594. ' });',
  12595. ' return Result;',
  12596. ' };',
  12597. '});',
  12598. 'this.Obj = null;'
  12599. ]),
  12600. LinesToStr([ // $mod.$main
  12601. '$mod.Obj = $mod.TObject.$create("Create");',
  12602. '$mod.TObject.vI = 3;',
  12603. 'if ($mod.TObject.vI === 4);',
  12604. '$mod.TObject.Sub=null;',
  12605. '$mod.TObject.Sub=null;',
  12606. '$mod.TObject.Sub=null;',
  12607. '']));
  12608. end;
  12609. procedure TTestModule.TestClass_CallClassMethod;
  12610. begin
  12611. StartProgram(false);
  12612. Add('type');
  12613. Add(' TObject = class');
  12614. Add(' public');
  12615. Add(' class var vI: longint;');
  12616. Add(' class var Sub: TObject;');
  12617. Add(' constructor Create;');
  12618. Add(' function GetMore(Par: longint): longint;');
  12619. Add(' class function GetIt(Par: longint): tobject;');
  12620. Add(' end;');
  12621. Add('constructor tobject.create;');
  12622. Add('begin');
  12623. Add(' sub:=getit(3);');
  12624. Add(' vi:=getmore(4);');
  12625. Add(' sub:=Self.getit(5);');
  12626. Add(' vi:=Self.getmore(6);');
  12627. Add('end;');
  12628. Add('function tobject.getmore(par: longint): longint;');
  12629. Add('begin');
  12630. Add(' sub:=getit(11);');
  12631. Add(' vi:=getmore(12);');
  12632. Add(' sub:=self.getit(13);');
  12633. Add(' vi:=self.getmore(14);');
  12634. Add('end;');
  12635. Add('class function tobject.getit(par: longint): tobject;');
  12636. Add('begin');
  12637. Add(' sub:=getit(21);');
  12638. Add(' vi:=sub.getmore(22);');
  12639. Add(' sub:=self.getit(23);');
  12640. Add(' vi:=self.sub.getmore(24);');
  12641. Add('end;');
  12642. Add('var Obj: tobject;');
  12643. Add('begin');
  12644. Add(' obj:=tobject.create;');
  12645. Add(' tobject.getit(5);');
  12646. Add(' obj.getit(6);');
  12647. Add(' obj.sub.getit(7);');
  12648. Add(' obj.sub.getit(8).SUB:=nil;');
  12649. Add(' obj.sub.getit(9).GETIT(10);');
  12650. Add(' obj.sub.getit(11).SuB.getit(12);');
  12651. ConvertProgram;
  12652. CheckSource('TestClass_CallClassMethod',
  12653. LinesToStr([ // statements
  12654. 'rtl.createClass(this,"TObject",null,function(){',
  12655. ' this.vI = 0;',
  12656. ' this.Sub = null;',
  12657. ' this.$init = function () {',
  12658. ' };',
  12659. ' this.$final = function () {',
  12660. ' };',
  12661. ' this.Create = function(){',
  12662. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12663. ' $mod.TObject.vI = this.GetMore(4);',
  12664. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12665. ' $mod.TObject.vI = this.GetMore(6);',
  12666. ' return this;',
  12667. ' };',
  12668. ' this.GetMore = function(Par){',
  12669. ' var Result = 0;',
  12670. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12671. ' $mod.TObject.vI = this.GetMore(12);',
  12672. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12673. ' $mod.TObject.vI = this.GetMore(14);',
  12674. ' return Result;',
  12675. ' };',
  12676. ' this.GetIt = function(Par){',
  12677. ' var Result = null;',
  12678. ' $mod.TObject.Sub = this.GetIt(21);',
  12679. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12680. ' $mod.TObject.Sub = this.GetIt(23);',
  12681. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12682. ' return Result;',
  12683. ' };',
  12684. '});',
  12685. 'this.Obj = null;'
  12686. ]),
  12687. LinesToStr([ // $mod.$main
  12688. '$mod.Obj = $mod.TObject.$create("Create");',
  12689. '$mod.TObject.GetIt(5);',
  12690. '$mod.Obj.$class.GetIt(6);',
  12691. '$mod.Obj.Sub.$class.GetIt(7);',
  12692. '$mod.TObject.Sub=null;',
  12693. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12694. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12695. '']));
  12696. end;
  12697. procedure TTestModule.TestClass_Property;
  12698. begin
  12699. StartProgram(false);
  12700. Add('type');
  12701. Add(' TObject = class');
  12702. Add(' Fx: longint;');
  12703. Add(' Fy: longint;');
  12704. Add(' function GetInt: longint;');
  12705. Add(' procedure SetInt(Value: longint);');
  12706. Add(' procedure DoIt;');
  12707. Add(' property IntA: longint read Fx write Fy;');
  12708. Add(' property IntB: longint read GetInt write SetInt;');
  12709. Add(' end;');
  12710. Add('function tobject.getint: longint;');
  12711. Add('begin');
  12712. Add(' result:=fx;');
  12713. Add('end;');
  12714. Add('procedure tobject.setint(value: longint);');
  12715. Add('begin');
  12716. Add(' if value=fy then exit;');
  12717. Add(' fy:=value;');
  12718. Add('end;');
  12719. Add('procedure tobject.doit;');
  12720. Add('begin');
  12721. Add(' IntA:=IntA+1;');
  12722. Add(' Self.IntA:=Self.IntA+1;');
  12723. Add(' IntB:=IntB+1;');
  12724. Add(' Self.IntB:=Self.IntB+1;');
  12725. Add('end;');
  12726. Add('var Obj: tobject;');
  12727. Add('begin');
  12728. Add(' obj.inta:=obj.inta+1;');
  12729. Add(' if obj.intb=2 then;');
  12730. Add(' obj.intb:=obj.intb+2;');
  12731. Add(' obj.setint(obj.inta);');
  12732. ConvertProgram;
  12733. CheckSource('TestClass_Property',
  12734. LinesToStr([ // statements
  12735. 'rtl.createClass(this, "TObject", null, function () {',
  12736. ' this.$init = function () {',
  12737. ' this.Fx = 0;',
  12738. ' this.Fy = 0;',
  12739. ' };',
  12740. ' this.$final = function () {',
  12741. ' };',
  12742. ' this.GetInt = function () {',
  12743. ' var Result = 0;',
  12744. ' Result = this.Fx;',
  12745. ' return Result;',
  12746. ' };',
  12747. ' this.SetInt = function (Value) {',
  12748. ' if (Value === this.Fy) return;',
  12749. ' this.Fy = Value;',
  12750. ' };',
  12751. ' this.DoIt = function () {',
  12752. ' this.Fy = this.Fx + 1;',
  12753. ' this.Fy = this.Fx + 1;',
  12754. ' this.SetInt(this.GetInt() + 1);',
  12755. ' this.SetInt(this.GetInt() + 1);',
  12756. ' };',
  12757. '});',
  12758. 'this.Obj = null;'
  12759. ]),
  12760. LinesToStr([ // $mod.$main
  12761. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12762. 'if ($mod.Obj.GetInt() === 2);',
  12763. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12764. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12765. ]));
  12766. end;
  12767. procedure TTestModule.TestClass_Property_ClassMethod;
  12768. begin
  12769. StartProgram(false);
  12770. Add([
  12771. 'type',
  12772. ' TObject = class',
  12773. ' class var Fx: longint;',
  12774. ' class var Fy: longint;',
  12775. ' class function GetInt: longint;',
  12776. ' class procedure SetInt(Value: longint);',
  12777. ' end;',
  12778. ' TBird = class',
  12779. ' class procedure DoIt;',
  12780. ' class property IntA: longint read Fx write Fy;',
  12781. ' class property IntB: longint read GetInt write SetInt;',
  12782. ' end;',
  12783. 'class function tobject.getint: longint;',
  12784. 'begin',
  12785. ' result:=fx;',
  12786. 'end;',
  12787. 'class procedure tobject.setint(value: longint);',
  12788. 'begin',
  12789. 'end;',
  12790. 'class procedure tbird.doit;',
  12791. 'begin',
  12792. ' FX:=3;',
  12793. ' IntA:=IntA+1;',
  12794. ' Self.IntA:=Self.IntA+1;',
  12795. ' IntB:=IntB+1;',
  12796. ' Self.IntB:=Self.IntB+1;',
  12797. ' with Self do begin',
  12798. ' FX:=11;',
  12799. ' IntA:=IntA+12;',
  12800. ' IntB:=IntB+13;',
  12801. ' end;',
  12802. 'end;',
  12803. 'var Obj: tbird;',
  12804. 'begin',
  12805. ' tbird.fx:=tbird.fx+1;',
  12806. ' tbird.inta:=tbird.inta+1;',
  12807. ' if tbird.intb=2 then;',
  12808. ' tbird.intb:=tbird.intb+2;',
  12809. ' tbird.setint(tbird.inta);',
  12810. ' obj.inta:=obj.inta+1;',
  12811. ' if obj.intb=2 then;',
  12812. ' obj.intb:=obj.intb+2;',
  12813. ' obj.setint(obj.inta);',
  12814. ' with Tbird do begin',
  12815. ' FX:=FY+1;',
  12816. ' inta:=inta+2;',
  12817. ' intb:=intb+3;',
  12818. ' end;',
  12819. ' with Obj do begin',
  12820. ' FX:=FY+1;',
  12821. ' inta:=inta+2;',
  12822. ' intb:=intb+3;',
  12823. ' end;',
  12824. '']);
  12825. ConvertProgram;
  12826. CheckSource('TestClass_Property_ClassMethod',
  12827. LinesToStr([ // statements
  12828. 'rtl.createClass(this, "TObject", null, function () {',
  12829. ' this.Fx = 0;',
  12830. ' this.Fy = 0;',
  12831. ' this.$init = function () {',
  12832. ' };',
  12833. ' this.$final = function () {',
  12834. ' };',
  12835. ' this.GetInt = function () {',
  12836. ' var Result = 0;',
  12837. ' Result = this.Fx;',
  12838. ' return Result;',
  12839. ' };',
  12840. ' this.SetInt = function (Value) {',
  12841. ' };',
  12842. '});',
  12843. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12844. ' this.DoIt = function () {',
  12845. ' $mod.TObject.Fx = 3;',
  12846. ' $mod.TObject.Fy = this.Fx + 1;',
  12847. ' $mod.TObject.Fy = this.Fx + 1;',
  12848. ' this.SetInt(this.GetInt() + 1);',
  12849. ' this.SetInt(this.GetInt() + 1);',
  12850. ' $mod.TObject.Fx = 11;',
  12851. ' $mod.TObject.Fy = this.Fx + 12;',
  12852. ' this.SetInt(this.GetInt() + 13);',
  12853. ' };',
  12854. '});',
  12855. 'this.Obj = null;'
  12856. ]),
  12857. LinesToStr([ // $mod.$main
  12858. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12859. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12860. 'if ($mod.TBird.GetInt() === 2);',
  12861. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12862. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12863. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12864. 'if ($mod.Obj.$class.GetInt() === 2);',
  12865. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12866. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12867. 'var $with = $mod.TBird;',
  12868. '$mod.TObject.Fx = $with.Fy + 1;',
  12869. '$mod.TObject.Fy = $with.Fx + 2;',
  12870. '$with.SetInt($with.GetInt() + 3);',
  12871. 'var $with1 = $mod.Obj;',
  12872. '$mod.TObject.Fx = $with1.Fy + 1;',
  12873. '$mod.TObject.Fy = $with1.Fx + 2;',
  12874. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  12875. '']));
  12876. end;
  12877. procedure TTestModule.TestClass_Property_Indexed;
  12878. begin
  12879. StartProgram(false);
  12880. Add([
  12881. 'type',
  12882. ' TObject = class',
  12883. ' FItems: array of longint;',
  12884. ' function GetItems(Index: longint): longint;',
  12885. ' procedure SetItems(Index: longint; Value: longint);',
  12886. ' procedure DoIt;',
  12887. ' property Items[Index: longint]: longint read getitems write setitems;',
  12888. ' end;',
  12889. 'function tobject.getitems(index: longint): longint;',
  12890. 'begin',
  12891. ' Result:=fitems[index];',
  12892. 'end;',
  12893. 'procedure tobject.setitems(index: longint; value: longint);',
  12894. 'begin',
  12895. ' fitems[index]:=value;',
  12896. 'end;',
  12897. 'procedure tobject.doit;',
  12898. 'begin',
  12899. ' items[1]:=2;',
  12900. ' items[3]:=items[4];',
  12901. ' self.items[5]:=self.items[6];',
  12902. ' items[items[7]]:=items[items[8]];',
  12903. 'end;',
  12904. 'var Obj: tobject;',
  12905. 'begin',
  12906. ' obj.Items[11]:=obj.Items[12];',
  12907. '']);
  12908. ConvertProgram;
  12909. CheckSource('TestClass_Property_Indexed',
  12910. LinesToStr([ // statements
  12911. 'rtl.createClass(this, "TObject", null, function () {',
  12912. ' this.$init = function () {',
  12913. ' this.FItems = [];',
  12914. ' };',
  12915. ' this.$final = function () {',
  12916. ' this.FItems = undefined;',
  12917. ' };',
  12918. ' this.GetItems = function (Index) {',
  12919. ' var Result = 0;',
  12920. ' Result = this.FItems[Index];',
  12921. ' return Result;',
  12922. ' };',
  12923. ' this.SetItems = function (Index, Value) {',
  12924. ' this.FItems[Index] = Value;',
  12925. ' };',
  12926. ' this.DoIt = function () {',
  12927. ' this.SetItems(1, 2);',
  12928. ' this.SetItems(3,this.GetItems(4));',
  12929. ' this.SetItems(5,this.GetItems(6));',
  12930. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12931. ' };',
  12932. '});',
  12933. 'this.Obj = null;'
  12934. ]),
  12935. LinesToStr([ // $mod.$main
  12936. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12937. ]));
  12938. end;
  12939. procedure TTestModule.TestClass_Property_IndexSpec;
  12940. begin
  12941. StartProgram(false);
  12942. Add([
  12943. 'type',
  12944. ' TEnum = (red, blue);',
  12945. ' TObject = class',
  12946. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12947. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12948. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12949. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12950. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12951. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12952. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12953. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12954. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12955. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12956. ' end;',
  12957. 'procedure DoIt(b: boolean); begin end;',
  12958. 'var',
  12959. ' o: TObject;',
  12960. 'begin',
  12961. ' o.B1:=o.B1;',
  12962. ' o.B2:=o.B2;',
  12963. ' o.B3:=o.B3;',
  12964. ' o.I1[''a'']:=o.I1[''b''];',
  12965. ' doit(o.b1);',
  12966. ' doit(o.b2);',
  12967. ' doit(o.i1[''c'']);',
  12968. '']);
  12969. ConvertProgram;
  12970. CheckSource('TestClass_Property_IndexSpec',
  12971. LinesToStr([ // statements
  12972. 'this.TEnum = {',
  12973. ' "0": "red",',
  12974. ' red: 0,',
  12975. ' "1": "blue",',
  12976. ' blue: 1',
  12977. '};',
  12978. 'rtl.createClass(this, "TObject", null, function () {',
  12979. ' this.$init = function () {',
  12980. ' };',
  12981. ' this.$final = function () {',
  12982. ' };',
  12983. '});',
  12984. 'this.DoIt = function (b) {',
  12985. '};',
  12986. 'this.o = null;',
  12987. '']),
  12988. LinesToStr([ // $mod.$main
  12989. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12990. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12991. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12992. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12993. '$mod.DoIt($mod.o.GetIntBool(1));',
  12994. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12995. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12996. '']));
  12997. end;
  12998. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12999. begin
  13000. StartProgram(false);
  13001. Add('type');
  13002. Add(' TArray = array of longint;');
  13003. Add(' TObject = class');
  13004. Add(' FItems: TArray;');
  13005. Add(' function GetItems: tarray;');
  13006. Add(' procedure SetItems(Value: tarray);');
  13007. Add(' property Items: tarray read getitems write setitems;');
  13008. Add(' procedure SetNumbers(const Value: tarray);');
  13009. Add(' property Numbers: tarray write setnumbers;');
  13010. Add(' end;');
  13011. Add('function tobject.getitems: tarray;');
  13012. Add('begin');
  13013. Add(' Result:=fitems;');
  13014. Add('end;');
  13015. Add('procedure tobject.setitems(value: tarray);');
  13016. Add('begin');
  13017. Add(' fitems:=value;');
  13018. Add(' fitems:=nil;');
  13019. Add(' Items:=nil;');
  13020. Add(' Items:=Items;');
  13021. Add(' Items[1]:=2;');
  13022. Add(' fitems[3]:=Items[4];');
  13023. Add(' Items[5]:=Items[6];');
  13024. Add(' Self.Items[7]:=8;');
  13025. Add(' Self.Items[9]:=Self.Items[10];');
  13026. Add(' Items[Items[11]]:=Items[Items[12]];');
  13027. Add('end;');
  13028. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13029. Add('begin;');
  13030. Add(' Numbers:=nil;');
  13031. Add(' Numbers:=Value;');
  13032. Add(' Self.Numbers:=Value;');
  13033. Add('end;');
  13034. Add('var Obj: tobject;');
  13035. Add('begin');
  13036. Add(' obj.items:=nil;');
  13037. Add(' obj.items:=obj.items;');
  13038. Add(' obj.items[11]:=obj.items[12];');
  13039. ConvertProgram;
  13040. CheckSource('TestClass_PropertyOfTypeArray',
  13041. LinesToStr([ // statements
  13042. 'rtl.createClass(this, "TObject", null, function () {',
  13043. ' this.$init = function () {',
  13044. ' this.FItems = [];',
  13045. ' };',
  13046. ' this.$final = function () {',
  13047. ' this.FItems = undefined;',
  13048. ' };',
  13049. ' this.GetItems = function () {',
  13050. ' var Result = [];',
  13051. ' Result = rtl.arrayRef(this.FItems);',
  13052. ' return Result;',
  13053. ' };',
  13054. ' this.SetItems = function (Value) {',
  13055. ' this.FItems = rtl.arrayRef(Value);',
  13056. ' this.FItems = [];',
  13057. ' this.SetItems([]);',
  13058. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13059. ' this.GetItems()[1] = 2;',
  13060. ' this.FItems[3] = this.GetItems()[4];',
  13061. ' this.GetItems()[5] = this.GetItems()[6];',
  13062. ' this.GetItems()[7] = 8;',
  13063. ' this.GetItems()[9] = this.GetItems()[10];',
  13064. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13065. ' };',
  13066. ' this.SetNumbers = function (Value) {',
  13067. ' this.SetNumbers([]);',
  13068. ' this.SetNumbers(Value);',
  13069. ' this.SetNumbers(Value);',
  13070. ' };',
  13071. '});',
  13072. 'this.Obj = null;'
  13073. ]),
  13074. LinesToStr([ // $mod.$main
  13075. '$mod.Obj.SetItems([]);',
  13076. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13077. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13078. ]));
  13079. end;
  13080. procedure TTestModule.TestClass_PropertyDefault;
  13081. begin
  13082. StartProgram(false);
  13083. Add([
  13084. 'type',
  13085. ' TArray = array of longint;',
  13086. ' TObject = class',
  13087. ' end;',
  13088. ' TBird = class',
  13089. ' FItems: TArray;',
  13090. ' function GetItems(Index: longint): longint;',
  13091. ' procedure SetItems(Index, Value: longint);',
  13092. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13093. ' end;',
  13094. 'function TBird.getitems(index: longint): longint;',
  13095. 'begin',
  13096. 'end;',
  13097. 'procedure TBird.setitems(index, value: longint);',
  13098. 'begin',
  13099. ' Self[1]:=2;',
  13100. ' Self[3]:=Self[index];',
  13101. ' Self[index]:=Self[Self[value]];',
  13102. ' Self[Self[4]]:=value;',
  13103. 'end;',
  13104. 'var',
  13105. ' Bird: TBird;',
  13106. ' Obj: TObject;',
  13107. 'begin',
  13108. ' bird[11]:=12;',
  13109. ' bird[13]:=bird[14];',
  13110. ' bird[Bird[15]]:=bird[Bird[15]];',
  13111. ' TBird(obj)[16]:=TBird(obj)[17];',
  13112. ' (obj as tbird)[18]:=19;',
  13113. '']);
  13114. ConvertProgram;
  13115. CheckSource('TestClass_PropertyDefault',
  13116. LinesToStr([ // statements
  13117. 'rtl.createClass(this, "TObject", null, function () {',
  13118. ' this.$init = function () {',
  13119. ' };',
  13120. ' this.$final = function () {',
  13121. ' };',
  13122. '});',
  13123. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13124. ' this.$init = function () {',
  13125. ' $mod.TObject.$init.call(this);',
  13126. ' this.FItems = [];',
  13127. ' };',
  13128. ' this.$final = function () {',
  13129. ' this.FItems = undefined;',
  13130. ' $mod.TObject.$final.call(this);',
  13131. ' };',
  13132. ' this.GetItems = function (Index) {',
  13133. ' var Result = 0;',
  13134. ' return Result;',
  13135. ' };',
  13136. ' this.SetItems = function (Index, Value) {',
  13137. ' this.SetItems(1, 2);',
  13138. ' this.SetItems(3, this.GetItems(Index));',
  13139. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13140. ' this.SetItems(this.GetItems(4), Value);',
  13141. ' };',
  13142. '});',
  13143. 'this.Bird = null;',
  13144. 'this.Obj = null;',
  13145. '']),
  13146. LinesToStr([ // $mod.$main
  13147. '$mod.Bird.SetItems(11, 12);',
  13148. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13149. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13150. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13151. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13152. '']));
  13153. end;
  13154. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13155. begin
  13156. StartProgram(false);
  13157. Add([
  13158. 'type',
  13159. ' TObject = class end;',
  13160. ' TAlphaList = class',
  13161. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13162. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13163. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13164. ' end;',
  13165. ' TBetaList = class',
  13166. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13167. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13168. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13169. ' end;',
  13170. ' TBird = class',
  13171. ' procedure DoIt;',
  13172. ' end;',
  13173. 'procedure TBird.DoIt;',
  13174. 'var',
  13175. ' List: TAlphaList;',
  13176. 'begin',
  13177. ' if TBetaList(List[true])[3]=nil then ;',
  13178. ' TBetaList(List[false])[5]:=nil;',
  13179. 'end;',
  13180. 'var',
  13181. ' List: TAlphaList;',
  13182. 'begin',
  13183. ' if TBetaList(List[true])[3]=nil then ;',
  13184. ' TBetaList(List[false])[5]:=nil;',
  13185. '']);
  13186. ConvertProgram;
  13187. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13188. LinesToStr([ // statements
  13189. 'rtl.createClass(this, "TObject", null, function () {',
  13190. ' this.$init = function () {',
  13191. ' };',
  13192. ' this.$final = function () {',
  13193. ' };',
  13194. '});',
  13195. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13196. '});',
  13197. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13198. '});',
  13199. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13200. ' this.DoIt = function () {',
  13201. ' var List = null;',
  13202. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13203. ' List.GetAlphas(false).SetBetas(5, null);',
  13204. ' };',
  13205. '});',
  13206. 'this.List = null;',
  13207. '']),
  13208. LinesToStr([ // $mod.$main
  13209. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13210. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13211. '']));
  13212. end;
  13213. procedure TTestModule.TestClass_PropertyOverride;
  13214. begin
  13215. StartProgram(false);
  13216. Add('type');
  13217. Add(' integer = longint;');
  13218. Add(' TObject = class');
  13219. Add(' FItem: integer;');
  13220. Add(' function GetItem: integer; external name ''GetItem'';');
  13221. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13222. Add(' property Item: integer read getitem write setitem;');
  13223. Add(' end;');
  13224. Add(' TCar = class');
  13225. Add(' FBag: integer;');
  13226. Add(' function GetBag: integer; external name ''GetBag'';');
  13227. Add(' property Item read getbag;');
  13228. Add(' end;');
  13229. Add('var');
  13230. Add(' Obj: tobject;');
  13231. Add(' Car: tcar;');
  13232. Add('begin');
  13233. Add(' Obj.Item:=Obj.Item;');
  13234. Add(' Car.Item:=Car.Item;');
  13235. ConvertProgram;
  13236. CheckSource('TestClass_PropertyOverride',
  13237. LinesToStr([ // statements
  13238. 'rtl.createClass(this, "TObject", null, function () {',
  13239. ' this.$init = function () {',
  13240. ' this.FItem = 0;',
  13241. ' };',
  13242. ' this.$final = function () {',
  13243. ' };',
  13244. '});',
  13245. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13246. ' this.$init = function () {',
  13247. ' $mod.TObject.$init.call(this);',
  13248. ' this.FBag = 0;',
  13249. ' };',
  13250. '});',
  13251. 'this.Obj = null;',
  13252. 'this.Car = null;',
  13253. '']),
  13254. LinesToStr([ // $mod.$main
  13255. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13256. '$mod.Car.SetItem($mod.Car.GetBag());',
  13257. '']));
  13258. end;
  13259. procedure TTestModule.TestClass_PropertyIncVisibility;
  13260. begin
  13261. AddModuleWithIntfImplSrc('unit1.pp',
  13262. LinesToStr([
  13263. 'type',
  13264. ' TNumber = longint;',
  13265. ' TInteger = longint;',
  13266. ' TObject = class',
  13267. ' private',
  13268. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13269. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13270. ' protected',
  13271. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13272. ' end;']),
  13273. LinesToStr([
  13274. '']));
  13275. StartProgram(true);
  13276. Add([
  13277. 'uses unit1;',
  13278. 'type',
  13279. ' TBird = class',
  13280. ' public',
  13281. ' property Items;',
  13282. ' end;',
  13283. 'procedure DoIt(i: TInteger);',
  13284. 'begin',
  13285. 'end;',
  13286. 'var b: TBird;',
  13287. 'begin',
  13288. ' b.Items[1]:=2;',
  13289. ' b.Items[3]:=b.Items[4];',
  13290. ' DoIt(b.Items[5]);',
  13291. '']);
  13292. ConvertProgram;
  13293. CheckSource('TestClass_PropertyIncVisibility',
  13294. LinesToStr([ // statements
  13295. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13296. '});',
  13297. 'this.DoIt = function (i) {',
  13298. '};',
  13299. 'this.b = null;'
  13300. ]),
  13301. LinesToStr([ // $mod.$main
  13302. '$mod.b.SetItems(1, 2);',
  13303. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13304. '$mod.DoIt($mod.b.GetItems(5));'
  13305. ]));
  13306. end;
  13307. procedure TTestModule.TestClass_Assigned;
  13308. begin
  13309. StartProgram(false);
  13310. Add('type');
  13311. Add(' TObject = class');
  13312. Add(' end;');
  13313. Add('var');
  13314. Add(' Obj: tobject;');
  13315. Add(' b: boolean;');
  13316. Add('begin');
  13317. Add(' if Assigned(obj) then ;');
  13318. Add(' b:=Assigned(obj) or false;');
  13319. ConvertProgram;
  13320. CheckSource('TestClass_Assigned',
  13321. LinesToStr([ // statements
  13322. 'rtl.createClass(this, "TObject", null, function () {',
  13323. ' this.$init = function () {',
  13324. ' };',
  13325. ' this.$final = function () {',
  13326. ' };',
  13327. '});',
  13328. 'this.Obj = null;',
  13329. 'this.b = false;'
  13330. ]),
  13331. LinesToStr([ // $mod.$main
  13332. 'if ($mod.Obj != null);',
  13333. '$mod.b = ($mod.Obj != null) || false;'
  13334. ]));
  13335. end;
  13336. procedure TTestModule.TestClass_WithClassDoCreate;
  13337. begin
  13338. StartProgram(false);
  13339. Add('type');
  13340. Add(' TObject = class');
  13341. Add(' aBool: boolean;');
  13342. Add(' Arr: array of boolean;');
  13343. Add(' constructor Create;');
  13344. Add(' end;');
  13345. Add('constructor TObject.Create; begin end;');
  13346. Add('var');
  13347. Add(' Obj: tobject;');
  13348. Add(' b: boolean;');
  13349. Add('begin');
  13350. Add(' with tobject.create do begin');
  13351. Add(' b:=abool;');
  13352. Add(' abool:=b;');
  13353. Add(' b:=arr[1];');
  13354. Add(' arr[2]:=b;');
  13355. Add(' end;');
  13356. Add(' with tobject do');
  13357. Add(' obj:=create;');
  13358. Add(' with obj do begin');
  13359. Add(' create;');
  13360. Add(' b:=abool;');
  13361. Add(' abool:=b;');
  13362. Add(' b:=arr[3];');
  13363. Add(' arr[4]:=b;');
  13364. Add(' end;');
  13365. ConvertProgram;
  13366. CheckSource('TestClass_WithClassDoCreate',
  13367. LinesToStr([ // statements
  13368. 'rtl.createClass(this, "TObject", null, function () {',
  13369. ' this.$init = function () {',
  13370. ' this.aBool = false;',
  13371. ' this.Arr = [];',
  13372. ' };',
  13373. ' this.$final = function () {',
  13374. ' this.Arr = undefined;',
  13375. ' };',
  13376. ' this.Create = function () {',
  13377. ' return this;',
  13378. ' };',
  13379. '});',
  13380. 'this.Obj = null;',
  13381. 'this.b = false;'
  13382. ]),
  13383. LinesToStr([ // $mod.$main
  13384. 'var $with = $mod.TObject.$create("Create");',
  13385. '$mod.b = $with.aBool;',
  13386. '$with.aBool = $mod.b;',
  13387. '$mod.b = $with.Arr[1];',
  13388. '$with.Arr[2] = $mod.b;',
  13389. 'var $with1 = $mod.TObject;',
  13390. '$mod.Obj = $with1.$create("Create");',
  13391. 'var $with2 = $mod.Obj;',
  13392. '$with2.Create();',
  13393. '$mod.b = $with2.aBool;',
  13394. '$with2.aBool = $mod.b;',
  13395. '$mod.b = $with2.Arr[3];',
  13396. '$with2.Arr[4] = $mod.b;',
  13397. '']));
  13398. end;
  13399. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13400. begin
  13401. StartProgram(false);
  13402. Add('type');
  13403. Add(' TObject = class');
  13404. Add(' FInt: longint;');
  13405. Add(' constructor Create;');
  13406. Add(' function GetSize: longint;');
  13407. Add(' procedure SetSize(Value: longint);');
  13408. Add(' property Int: longint read FInt write FInt;');
  13409. Add(' property Size: longint read GetSize write SetSize;');
  13410. Add(' end;');
  13411. Add('constructor TObject.Create; begin end;');
  13412. Add('function TObject.GetSize: longint; begin; end;');
  13413. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13414. Add('var');
  13415. Add(' Obj: tobject;');
  13416. Add(' i: longint;');
  13417. Add('begin');
  13418. Add(' with TObject.Create do begin');
  13419. Add(' i:=int;');
  13420. Add(' int:=i;');
  13421. Add(' i:=size;');
  13422. Add(' size:=i;');
  13423. Add(' end;');
  13424. Add(' with obj do begin');
  13425. Add(' i:=int;');
  13426. Add(' int:=i;');
  13427. Add(' i:=size;');
  13428. Add(' size:=i;');
  13429. Add(' end;');
  13430. ConvertProgram;
  13431. CheckSource('TestClass_WithClassInstDoProperty',
  13432. LinesToStr([ // statements
  13433. 'rtl.createClass(this, "TObject", null, function () {',
  13434. ' this.$init = function () {',
  13435. ' this.FInt = 0;',
  13436. ' };',
  13437. ' this.$final = function () {',
  13438. ' };',
  13439. ' this.Create = function () {',
  13440. ' return this;',
  13441. ' };',
  13442. ' this.GetSize = function () {',
  13443. ' var Result = 0;',
  13444. ' return Result;',
  13445. ' };',
  13446. ' this.SetSize = function (Value) {',
  13447. ' };',
  13448. '});',
  13449. 'this.Obj = null;',
  13450. 'this.i = 0;'
  13451. ]),
  13452. LinesToStr([ // $mod.$main
  13453. 'var $with = $mod.TObject.$create("Create");',
  13454. '$mod.i = $with.FInt;',
  13455. '$with.FInt = $mod.i;',
  13456. '$mod.i = $with.GetSize();',
  13457. '$with.SetSize($mod.i);',
  13458. 'var $with1 = $mod.Obj;',
  13459. '$mod.i = $with1.FInt;',
  13460. '$with1.FInt = $mod.i;',
  13461. '$mod.i = $with1.GetSize();',
  13462. '$with1.SetSize($mod.i);',
  13463. '']));
  13464. end;
  13465. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13466. begin
  13467. StartProgram(false);
  13468. Add('type');
  13469. Add(' TObject = class');
  13470. Add(' constructor Create;');
  13471. Add(' function GetItems(Index: longint): longint;');
  13472. Add(' procedure SetItems(Index, Value: longint);');
  13473. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13474. Add(' end;');
  13475. Add('constructor TObject.Create; begin end;');
  13476. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13477. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13478. Add('var');
  13479. Add(' Obj: tobject;');
  13480. Add(' i: longint;');
  13481. Add('begin');
  13482. Add(' with TObject.Create do begin');
  13483. Add(' i:=Items[1];');
  13484. Add(' Items[2]:=i;');
  13485. Add(' end;');
  13486. Add(' with obj do begin');
  13487. Add(' i:=Items[3];');
  13488. Add(' Items[4]:=i;');
  13489. Add(' end;');
  13490. ConvertProgram;
  13491. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13492. LinesToStr([ // statements
  13493. 'rtl.createClass(this, "TObject", null, function () {',
  13494. ' this.$init = function () {',
  13495. ' };',
  13496. ' this.$final = function () {',
  13497. ' };',
  13498. ' this.Create = function () {',
  13499. ' return this;',
  13500. ' };',
  13501. ' this.GetItems = function (Index) {',
  13502. ' var Result = 0;',
  13503. ' return Result;',
  13504. ' };',
  13505. ' this.SetItems = function (Index, Value) {',
  13506. ' };',
  13507. '});',
  13508. 'this.Obj = null;',
  13509. 'this.i = 0;'
  13510. ]),
  13511. LinesToStr([ // $mod.$main
  13512. 'var $with = $mod.TObject.$create("Create");',
  13513. '$mod.i = $with.GetItems(1);',
  13514. '$with.SetItems(2, $mod.i);',
  13515. 'var $with1 = $mod.Obj;',
  13516. '$mod.i = $with1.GetItems(3);',
  13517. '$with1.SetItems(4, $mod.i);',
  13518. '']));
  13519. end;
  13520. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13521. begin
  13522. StartProgram(false);
  13523. Add('type');
  13524. Add(' TObject = class');
  13525. Add(' constructor Create;');
  13526. Add(' function GetSize: longint;');
  13527. Add(' procedure SetSize(Value: longint);');
  13528. Add(' end;');
  13529. Add('constructor TObject.Create; begin end;');
  13530. Add('function TObject.GetSize: longint; begin; end;');
  13531. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13532. Add('var');
  13533. Add(' Obj: tobject;');
  13534. Add(' i: longint;');
  13535. Add('begin');
  13536. Add(' with TObject.Create do begin');
  13537. Add(' i:=GetSize;');
  13538. Add(' i:=GetSize();');
  13539. Add(' SetSize(i);');
  13540. Add(' end;');
  13541. Add(' with obj do begin');
  13542. Add(' i:=GetSize;');
  13543. Add(' i:=GetSize();');
  13544. Add(' SetSize(i);');
  13545. Add(' end;');
  13546. ConvertProgram;
  13547. CheckSource('TestClass_WithClassInstDoFunc',
  13548. LinesToStr([ // statements
  13549. 'rtl.createClass(this, "TObject", null, function () {',
  13550. ' this.$init = function () {',
  13551. ' };',
  13552. ' this.$final = function () {',
  13553. ' };',
  13554. ' this.Create = function () {',
  13555. ' return this;',
  13556. ' };',
  13557. ' this.GetSize = function () {',
  13558. ' var Result = 0;',
  13559. ' return Result;',
  13560. ' };',
  13561. ' this.SetSize = function (Value) {',
  13562. ' };',
  13563. '});',
  13564. 'this.Obj = null;',
  13565. 'this.i = 0;'
  13566. ]),
  13567. LinesToStr([ // $mod.$main
  13568. 'var $with = $mod.TObject.$create("Create");',
  13569. '$mod.i = $with.GetSize();',
  13570. '$mod.i = $with.GetSize();',
  13571. '$with.SetSize($mod.i);',
  13572. 'var $with1 = $mod.Obj;',
  13573. '$mod.i = $with1.GetSize();',
  13574. '$mod.i = $with1.GetSize();',
  13575. '$with1.SetSize($mod.i);',
  13576. '']));
  13577. end;
  13578. procedure TTestModule.TestClass_TypeCast;
  13579. begin
  13580. StartProgram(false);
  13581. Add('type');
  13582. Add(' TObject = class');
  13583. Add(' Next: TObject;');
  13584. Add(' constructor Create;');
  13585. Add(' end;');
  13586. Add(' TControl = class(TObject)');
  13587. Add(' Arr: array of TObject;');
  13588. Add(' function GetIt(vI: longint = 0): TObject;');
  13589. Add(' end;');
  13590. Add('constructor tobject.create; begin end;');
  13591. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13592. Add('var');
  13593. Add(' Obj: tobject;');
  13594. Add('begin');
  13595. Add(' obj:=tcontrol(obj).next;');
  13596. Add(' tcontrol(obj):=nil;');
  13597. Add(' obj:=tcontrol(obj);');
  13598. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13599. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13600. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13601. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13602. Add(' obj:=tcontrol(nil);');
  13603. ConvertProgram;
  13604. CheckSource('TestClass_TypeCast',
  13605. LinesToStr([ // statements
  13606. 'rtl.createClass(this, "TObject", null, function () {',
  13607. ' this.$init = function () {',
  13608. ' this.Next = null;',
  13609. ' };',
  13610. ' this.$final = function () {',
  13611. ' this.Next = undefined;',
  13612. ' };',
  13613. ' this.Create = function () {',
  13614. ' return this;',
  13615. ' };',
  13616. '});',
  13617. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  13618. ' this.$init = function () {',
  13619. ' $mod.TObject.$init.call(this);',
  13620. ' this.Arr = [];',
  13621. ' };',
  13622. ' this.$final = function () {',
  13623. ' this.Arr = undefined;',
  13624. ' $mod.TObject.$final.call(this);',
  13625. ' };',
  13626. ' this.GetIt = function (vI) {',
  13627. ' var Result = null;',
  13628. ' return Result;',
  13629. ' };',
  13630. '});',
  13631. 'this.Obj = null;'
  13632. ]),
  13633. LinesToStr([ // $mod.$main
  13634. '$mod.Obj = $mod.Obj.Next;',
  13635. '$mod.Obj = null;',
  13636. '$mod.Obj = $mod.Obj;',
  13637. '$mod.Obj = $mod.Obj.GetIt(0);',
  13638. '$mod.Obj = $mod.Obj.GetIt(0);',
  13639. '$mod.Obj = $mod.Obj.GetIt(1);',
  13640. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13641. '$mod.Obj = null;',
  13642. '']));
  13643. end;
  13644. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13645. begin
  13646. StartProgram(false);
  13647. Add('type');
  13648. Add(' TObject = class end;');
  13649. Add('procedure ProcA(var A);');
  13650. Add('begin');
  13651. Add(' TObject(A):=nil;');
  13652. Add(' TObject(A):=TObject(A);');
  13653. Add(' if TObject(A)=nil then ;');
  13654. Add(' if nil=TObject(A) then ;');
  13655. Add('end;');
  13656. Add('procedure ProcB(out A);');
  13657. Add('begin');
  13658. Add(' TObject(A):=nil;');
  13659. Add(' TObject(A):=TObject(A);');
  13660. Add(' if TObject(A)=nil then ;');
  13661. Add(' if nil=TObject(A) then ;');
  13662. Add('end;');
  13663. Add('procedure ProcC(const A);');
  13664. Add('begin');
  13665. Add(' if TObject(A)=nil then ;');
  13666. Add(' if nil=TObject(A) then ;');
  13667. Add('end;');
  13668. Add('var o: TObject;');
  13669. Add('begin');
  13670. Add(' ProcA(o);');
  13671. Add(' ProcB(o);');
  13672. Add(' ProcC(o);');
  13673. ConvertProgram;
  13674. CheckSource('TestClass_TypeCastUntypedParam',
  13675. LinesToStr([ // statements
  13676. 'rtl.createClass(this, "TObject", null, function () {',
  13677. ' this.$init = function () {',
  13678. ' };',
  13679. ' this.$final = function () {',
  13680. ' };',
  13681. '});',
  13682. 'this.ProcA = function (A) {',
  13683. ' A.set(null);',
  13684. ' A.set(A.get());',
  13685. ' if (A.get() === null);',
  13686. ' if (null === A.get());',
  13687. '};',
  13688. 'this.ProcB = function (A) {',
  13689. ' A.set(null);',
  13690. ' A.set(A.get());',
  13691. ' if (A.get() === null);',
  13692. ' if (null === A.get());',
  13693. '};',
  13694. 'this.ProcC = function (A) {',
  13695. ' if (A === null);',
  13696. ' if (null === A);',
  13697. '};',
  13698. 'this.o = null;',
  13699. '']),
  13700. LinesToStr([ // $mod.$main
  13701. '$mod.ProcA({',
  13702. ' p: $mod,',
  13703. ' get: function () {',
  13704. ' return this.p.o;',
  13705. ' },',
  13706. ' set: function (v) {',
  13707. ' this.p.o = v;',
  13708. ' }',
  13709. '});',
  13710. '$mod.ProcB({',
  13711. ' p: $mod,',
  13712. ' get: function () {',
  13713. ' return this.p.o;',
  13714. ' },',
  13715. ' set: function (v) {',
  13716. ' this.p.o = v;',
  13717. ' }',
  13718. '});',
  13719. '$mod.ProcC($mod.o);',
  13720. '']));
  13721. end;
  13722. procedure TTestModule.TestClass_Overloads;
  13723. begin
  13724. StartProgram(false);
  13725. Add('type');
  13726. Add(' TObject = class');
  13727. Add(' procedure DoIt;');
  13728. Add(' procedure DoIt(vI: longint);');
  13729. Add(' end;');
  13730. Add('procedure TObject.DoIt;');
  13731. Add('begin');
  13732. Add(' DoIt;');
  13733. Add(' DoIt(1);');
  13734. Add('end;');
  13735. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13736. Add('begin');
  13737. ConvertProgram;
  13738. CheckSource('TestClass_Overloads',
  13739. LinesToStr([ // statements
  13740. 'rtl.createClass(this, "TObject", null, function () {',
  13741. ' this.$init = function () {',
  13742. ' };',
  13743. ' this.$final = function () {',
  13744. ' };',
  13745. ' this.DoIt = function () {',
  13746. ' this.DoIt();',
  13747. ' this.DoIt$1(1);',
  13748. ' };',
  13749. ' this.DoIt$1 = function (vI) {',
  13750. ' };',
  13751. '});',
  13752. '']),
  13753. LinesToStr([ // $mod.$main
  13754. '']));
  13755. end;
  13756. procedure TTestModule.TestClass_OverloadsAncestor;
  13757. begin
  13758. StartProgram(false);
  13759. Add('type');
  13760. Add(' TObject = class;');
  13761. Add(' TObject = class');
  13762. Add(' procedure DoIt(vA: longint);');
  13763. Add(' procedure DoIt(vA, vB: longint);');
  13764. Add(' end;');
  13765. Add(' TCar = class;');
  13766. Add(' TCar = class');
  13767. Add(' procedure DoIt(vA: longint);');
  13768. Add(' procedure DoIt(vA, vB: longint);');
  13769. Add(' end;');
  13770. Add('procedure tobject.doit(va: longint);');
  13771. Add('begin');
  13772. Add(' doit(1);');
  13773. Add(' doit(1,2);');
  13774. Add('end;');
  13775. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13776. Add('procedure tcar.doit(va: longint);');
  13777. Add('begin');
  13778. Add(' doit(1);');
  13779. Add(' doit(1,2);');
  13780. Add(' inherited doit(1);');
  13781. Add(' inherited doit(1,2);');
  13782. Add('end;');
  13783. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13784. Add('begin');
  13785. ConvertProgram;
  13786. CheckSource('TestClass_OverloadsAncestor',
  13787. LinesToStr([ // statements
  13788. 'rtl.createClass(this, "TObject", null, function () {',
  13789. ' this.$init = function () {',
  13790. ' };',
  13791. ' this.$final = function () {',
  13792. ' };',
  13793. ' this.DoIt = function (vA) {',
  13794. ' this.DoIt(1);',
  13795. ' this.DoIt$1(1,2);',
  13796. ' };',
  13797. ' this.DoIt$1 = function (vA, vB) {',
  13798. ' };',
  13799. '});',
  13800. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13801. ' this.DoIt$2 = function (vA) {',
  13802. ' this.DoIt$2(1);',
  13803. ' this.DoIt$3(1, 2);',
  13804. ' $mod.TObject.DoIt.call(this, 1);',
  13805. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13806. ' };',
  13807. ' this.DoIt$3 = function (vA, vB) {',
  13808. ' };',
  13809. '});',
  13810. '']),
  13811. LinesToStr([ // $mod.$main
  13812. '']));
  13813. end;
  13814. procedure TTestModule.TestClass_OverloadConstructor;
  13815. begin
  13816. StartProgram(false);
  13817. Add('type');
  13818. Add(' TObject = class');
  13819. Add(' constructor Create(vA: longint);');
  13820. Add(' constructor Create(vA, vB: longint);');
  13821. Add(' end;');
  13822. Add(' TCar = class');
  13823. Add(' constructor Create(vA: longint);');
  13824. Add(' constructor Create(vA, vB: longint);');
  13825. Add(' end;');
  13826. Add('constructor tobject.create(va: longint);');
  13827. Add('begin');
  13828. Add(' create(1);');
  13829. Add(' create(1,2);');
  13830. Add('end;');
  13831. Add('constructor tobject.create(va, vb: longint); begin end;');
  13832. Add('constructor tcar.create(va: longint);');
  13833. Add('begin');
  13834. Add(' create(1);');
  13835. Add(' create(1,2);');
  13836. Add(' inherited create(1);');
  13837. Add(' inherited create(1,2);');
  13838. Add('end;');
  13839. Add('constructor tcar.create(va, vb: longint); begin end;');
  13840. Add('begin');
  13841. Add(' tobject.create(1);');
  13842. Add(' tobject.create(1,2);');
  13843. Add(' tcar.create(1);');
  13844. Add(' tcar.create(1,2);');
  13845. ConvertProgram;
  13846. CheckSource('TestClass_OverloadConstructor',
  13847. LinesToStr([ // statements
  13848. 'rtl.createClass(this, "TObject", null, function () {',
  13849. ' this.$init = function () {',
  13850. ' };',
  13851. ' this.$final = function () {',
  13852. ' };',
  13853. ' this.Create = function (vA) {',
  13854. ' this.Create(1);',
  13855. ' this.Create$1(1,2);',
  13856. ' return this;',
  13857. ' };',
  13858. ' this.Create$1 = function (vA, vB) {',
  13859. ' return this;',
  13860. ' };',
  13861. '});',
  13862. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13863. ' this.Create$2 = function (vA) {',
  13864. ' this.Create$2(1);',
  13865. ' this.Create$3(1, 2);',
  13866. ' $mod.TObject.Create.call(this, 1);',
  13867. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13868. ' return this;',
  13869. ' };',
  13870. ' this.Create$3 = function (vA, vB) {',
  13871. ' return this;',
  13872. ' };',
  13873. '});',
  13874. '']),
  13875. LinesToStr([ // $mod.$main
  13876. '$mod.TObject.$create("Create", [1]);',
  13877. '$mod.TObject.$create("Create$1", [1, 2]);',
  13878. '$mod.TCar.$create("Create$2", [1]);',
  13879. '$mod.TCar.$create("Create$3", [1, 2]);',
  13880. '']));
  13881. end;
  13882. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13883. begin
  13884. StartProgram(false);
  13885. Add([
  13886. '{$mode delphi}',
  13887. 'type',
  13888. ' TObject = class end;',
  13889. ' TBird = class',
  13890. ' function {#a}GetValue: longint; overload; virtual;',
  13891. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13892. ' end;',
  13893. ' TEagle = class(TBird)',
  13894. ' function {#c}GetValue: longint; overload; override;',
  13895. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13896. ' end;',
  13897. 'function TBird.GetValue: longint;',
  13898. 'begin',
  13899. ' if 3={@a}GetValue then ;',
  13900. ' if 4={@b}GetValue(5) then ;',
  13901. 'end;',
  13902. 'function TBird.GetValue(AValue: longint): longint;',
  13903. 'begin',
  13904. 'end;',
  13905. 'function TEagle.GetValue: longint;',
  13906. 'begin',
  13907. ' if 13={@c}GetValue then ;',
  13908. ' if 14={@d}GetValue(15) then ;',
  13909. ' if 15=inherited {@a}GetValue then ;',
  13910. ' if 16=inherited {@b}GetValue(17) then ;',
  13911. 'end;',
  13912. 'function TEagle.GetValue(AValue: longint): longint;',
  13913. 'begin',
  13914. 'end;',
  13915. 'var',
  13916. ' e: TEagle;',
  13917. 'begin',
  13918. ' if 23=e.{@c}GetValue then ;',
  13919. ' if 24=e.{@d}GetValue(25) then ;']);
  13920. ConvertProgram;
  13921. CheckSource('TestClass_OverloadDelphiOverride',
  13922. LinesToStr([ // statements
  13923. 'rtl.createClass(this, "TObject", null, function () {',
  13924. ' this.$init = function () {',
  13925. ' };',
  13926. ' this.$final = function () {',
  13927. ' };',
  13928. '});',
  13929. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13930. ' this.GetValue = function () {',
  13931. ' var Result = 0;',
  13932. ' if (3 === this.GetValue()) ;',
  13933. ' if (4 === this.GetValue$1(5)) ;',
  13934. ' return Result;',
  13935. ' };',
  13936. ' this.GetValue$1 = function (AValue) {',
  13937. ' var Result = 0;',
  13938. ' return Result;',
  13939. ' };',
  13940. '});',
  13941. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  13942. ' this.GetValue = function () {',
  13943. ' var Result = 0;',
  13944. ' if (13 === this.GetValue()) ;',
  13945. ' if (14 === this.GetValue$1(15)) ;',
  13946. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13947. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13948. ' return Result;',
  13949. ' };',
  13950. ' this.GetValue$1 = function (AValue) {',
  13951. ' var Result = 0;',
  13952. ' return Result;',
  13953. ' };',
  13954. '});',
  13955. 'this.e = null;',
  13956. '']),
  13957. LinesToStr([ // $mod.$main
  13958. 'if (23 === $mod.e.GetValue()) ;',
  13959. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13960. '']));
  13961. end;
  13962. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13963. begin
  13964. StartProgram(false);
  13965. Add([
  13966. '{$mode delphi}',
  13967. 'type',
  13968. ' TObject = class end;',
  13969. ' TAnimal = class',
  13970. ' public',
  13971. ' {#animal_a}A: longint;',
  13972. ' function {#animal_b}B: longint;',
  13973. ' end;',
  13974. ' TBird = class(TAnimal)',
  13975. ' public',
  13976. ' {#bird_a}A: double;',
  13977. ' {#bird_b}B: boolean;',
  13978. ' end;',
  13979. ' TEagle = class(TBird)',
  13980. ' public',
  13981. ' function {#eagle_a}A: boolean;',
  13982. ' {#eagle_b}B: double;',
  13983. ' end;',
  13984. 'function TAnimal.B: longint;',
  13985. 'begin',
  13986. 'end;',
  13987. 'function TEagle.A: boolean;',
  13988. 'begin',
  13989. ' {@eagle_b}B:=3.3;',
  13990. ' {@eagle_a}A();',
  13991. ' TBird(Self).{@bird_b}B:=true;',
  13992. ' TAnimal(Self).{@animal_a}A:=17;',
  13993. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13994. 'end;',
  13995. 'var',
  13996. ' e: TEagle;',
  13997. 'begin',
  13998. ' e.{@eagle_b}B:=5.3;',
  13999. ' if e.{@eagle_a}A then ;',
  14000. '']);
  14001. ConvertProgram;
  14002. CheckSource('TestClass_ReintroduceVarDelphi',
  14003. LinesToStr([ // statements
  14004. 'rtl.createClass(this, "TObject", null, function () {',
  14005. ' this.$init = function () {',
  14006. ' };',
  14007. ' this.$final = function () {',
  14008. ' };',
  14009. '});',
  14010. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14011. ' this.$init = function () {',
  14012. ' $mod.TObject.$init.call(this);',
  14013. ' this.A = 0;',
  14014. ' };',
  14015. ' this.B = function () {',
  14016. ' var Result = 0;',
  14017. ' return Result;',
  14018. ' };',
  14019. '});',
  14020. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14021. ' this.$init = function () {',
  14022. ' $mod.TAnimal.$init.call(this);',
  14023. ' this.A$1 = 0.0;',
  14024. ' this.B$1 = false;',
  14025. ' };',
  14026. '});',
  14027. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14028. ' this.$init = function () {',
  14029. ' $mod.TBird.$init.call(this);',
  14030. ' this.B$2 = 0.0;',
  14031. ' };',
  14032. ' this.A$2 = function () {',
  14033. ' var Result = false;',
  14034. ' this.B$2 = 3.3;',
  14035. ' this.A$2();',
  14036. ' this.B$1 = true;',
  14037. ' this.A = 17;',
  14038. ' this.B$1 = this.A$1 > 1;',
  14039. ' return Result;',
  14040. ' };',
  14041. '});',
  14042. 'this.e = null;',
  14043. '']),
  14044. LinesToStr([ // $mod.$main
  14045. '$mod.e.B$2 = 5.3;',
  14046. 'if ($mod.e.A$2()) ;',
  14047. '']));
  14048. end;
  14049. procedure TTestModule.TestClass_ReintroducedVar;
  14050. begin
  14051. StartProgram(false);
  14052. Add('type');
  14053. Add(' TObject = class');
  14054. Add(' strict private');
  14055. Add(' Some: longint;');
  14056. Add(' end;');
  14057. Add(' TMobile = class');
  14058. Add(' strict private');
  14059. Add(' Some: string;');
  14060. Add(' end;');
  14061. Add(' TCar = class(tmobile)');
  14062. Add(' procedure Some;');
  14063. Add(' procedure Some(vA: longint);');
  14064. Add(' end;');
  14065. Add('procedure tcar.some;');
  14066. Add('begin');
  14067. Add(' Some;');
  14068. Add(' Some(1);');
  14069. Add('end;');
  14070. Add('procedure tcar.some(va: longint); begin end;');
  14071. Add('begin');
  14072. ConvertProgram;
  14073. CheckSource('TestClass_ReintroducedVar',
  14074. LinesToStr([ // statements
  14075. 'rtl.createClass(this, "TObject", null, function () {',
  14076. ' this.$init = function () {',
  14077. ' this.Some = 0;',
  14078. ' };',
  14079. ' this.$final = function () {',
  14080. ' };',
  14081. '});',
  14082. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14083. ' this.$init = function () {',
  14084. ' $mod.TObject.$init.call(this);',
  14085. ' this.Some$1 = "";',
  14086. ' };',
  14087. '});',
  14088. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14089. ' this.Some$2 = function () {',
  14090. ' this.Some$2();',
  14091. ' this.Some$3(1);',
  14092. ' };',
  14093. ' this.Some$3 = function (vA) {',
  14094. ' };',
  14095. '});',
  14096. '']),
  14097. LinesToStr([ // $mod.$main
  14098. '']));
  14099. end;
  14100. procedure TTestModule.TestClass_RaiseDescendant;
  14101. begin
  14102. StartProgram(false);
  14103. Add([
  14104. 'type',
  14105. ' TObject = class',
  14106. ' constructor Create(Msg: string);',
  14107. ' end;',
  14108. ' Exception = class',
  14109. ' end;',
  14110. ' EConvertError = class(Exception)',
  14111. ' end;',
  14112. 'constructor TObject.Create(Msg: string); begin end;',
  14113. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14114. 'begin',
  14115. ' raise Exception.Create(''Bar1'');',
  14116. ' raise EConvertError.Create(''Bar2'');',
  14117. ' raise AssertConv(''Bar2'');',
  14118. ' raise AssertConv;',
  14119. '']);
  14120. ConvertProgram;
  14121. CheckSource('TestClass_RaiseDescendant',
  14122. LinesToStr([ // statements
  14123. 'rtl.createClass(this, "TObject", null, function () {',
  14124. ' this.$init = function () {',
  14125. ' };',
  14126. ' this.$final = function () {',
  14127. ' };',
  14128. ' this.Create = function (Msg) {',
  14129. ' return this;',
  14130. ' };',
  14131. '});',
  14132. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14133. '});',
  14134. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14135. '});',
  14136. 'this.AssertConv = function (Msg) {',
  14137. ' var Result = null;',
  14138. ' return Result;',
  14139. '};',
  14140. '']),
  14141. LinesToStr([ // $mod.$main
  14142. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14143. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14144. 'throw $mod.AssertConv("Bar2");',
  14145. 'throw $mod.AssertConv("def");',
  14146. '']));
  14147. end;
  14148. procedure TTestModule.TestClass_ExternalMethod;
  14149. begin
  14150. AddModuleWithIntfImplSrc('unit2.pas',
  14151. LinesToStr([
  14152. 'type',
  14153. ' TObject = class',
  14154. ' public',
  14155. ' procedure Intern; external name ''$DoIntern'';',
  14156. ' end;',
  14157. '']),
  14158. LinesToStr([
  14159. '']));
  14160. StartUnit(true);
  14161. Add('interface');
  14162. Add('uses unit2;');
  14163. Add('type');
  14164. Add(' TCar = class(TObject)');
  14165. Add(' public');
  14166. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14167. Add(' procedure DoIt;');
  14168. Add(' end;');
  14169. Add('implementation');
  14170. Add('procedure tcar.doit;');
  14171. Add('begin');
  14172. Add(' Intern;');
  14173. Add(' Intern();');
  14174. Add(' Intern2;');
  14175. Add(' Intern2();');
  14176. Add('end;');
  14177. Add('var Obj: TCar;');
  14178. Add('begin');
  14179. Add(' obj.intern;');
  14180. Add(' obj.intern();');
  14181. Add(' obj.intern2;');
  14182. Add(' obj.intern2();');
  14183. Add(' obj.doit;');
  14184. Add(' obj.doit();');
  14185. Add(' with obj do begin');
  14186. Add(' Intern;');
  14187. Add(' Intern();');
  14188. Add(' Intern2;');
  14189. Add(' Intern2();');
  14190. Add(' end;');
  14191. ConvertUnit;
  14192. CheckSource('TestClass_ExternalMethod',
  14193. LinesToStr([
  14194. 'var $impl = $mod.$impl;',
  14195. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14196. ' this.DoIt = function () {',
  14197. ' this.$DoIntern();',
  14198. ' this.$DoIntern();',
  14199. ' this.$DoIntern2();',
  14200. ' this.$DoIntern2();',
  14201. ' };',
  14202. ' });',
  14203. '']),
  14204. LinesToStr([ // this.$init
  14205. '$impl.Obj.$DoIntern();',
  14206. '$impl.Obj.$DoIntern();',
  14207. '$impl.Obj.$DoIntern2();',
  14208. '$impl.Obj.$DoIntern2();',
  14209. '$impl.Obj.DoIt();',
  14210. '$impl.Obj.DoIt();',
  14211. 'var $with = $impl.Obj;',
  14212. '$with.$DoIntern();',
  14213. '$with.$DoIntern();',
  14214. '$with.$DoIntern2();',
  14215. '$with.$DoIntern2();',
  14216. '']),
  14217. LinesToStr([ // implementation
  14218. '$impl.Obj = null;',
  14219. '']) );
  14220. end;
  14221. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14222. begin
  14223. StartProgram(false);
  14224. Add('type');
  14225. Add(' TObject = class');
  14226. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14227. Add(' end;');
  14228. Add('begin');
  14229. SetExpectedPasResolverError('Virtual method name must match external',
  14230. nVirtualMethodNameMustMatchExternal);
  14231. ConvertProgram;
  14232. end;
  14233. procedure TTestModule.TestClass_ExternalOverrideFail;
  14234. begin
  14235. StartProgram(false);
  14236. Add('type');
  14237. Add(' TObject = class');
  14238. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14239. Add(' end;');
  14240. Add(' TCar = class');
  14241. Add(' procedure DoIt; override; external name ''DoIt'';');
  14242. Add(' end;');
  14243. Add('begin');
  14244. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14245. nInvalidXModifierY);
  14246. ConvertProgram;
  14247. end;
  14248. procedure TTestModule.TestClass_ExternalVar;
  14249. begin
  14250. AddModuleWithIntfImplSrc('unit2.pas',
  14251. LinesToStr([
  14252. '{$modeswitch externalclass}',
  14253. 'type',
  14254. ' TObject = class',
  14255. ' public',
  14256. ' Intern: longint external name ''$Intern'';',
  14257. ' Bracket: longint external name ''["A B"]'';',
  14258. ' end;',
  14259. '']),
  14260. LinesToStr([
  14261. '']));
  14262. StartUnit(true);
  14263. Add([
  14264. 'interface',
  14265. 'uses unit2;',
  14266. '{$modeswitch externalclass}',
  14267. 'type',
  14268. ' TCar = class(tobject)',
  14269. ' public',
  14270. ' Intern2: longint external name ''$Intern2'';',
  14271. ' procedure DoIt;',
  14272. ' end;',
  14273. 'implementation',
  14274. 'procedure tcar.doit;',
  14275. 'begin',
  14276. ' Intern:=Intern+1;',
  14277. ' Intern2:=Intern2+2;',
  14278. ' Bracket:=Bracket+3;',
  14279. 'end;',
  14280. 'var Obj: TCar;',
  14281. 'begin',
  14282. ' obj.intern:=obj.intern+1;',
  14283. ' obj.intern2:=obj.intern2+2;',
  14284. ' obj.Bracket:=obj.Bracket+3;',
  14285. ' with obj do begin',
  14286. ' intern:=intern+1;',
  14287. ' intern2:=intern2+2;',
  14288. ' Bracket:=Bracket+3;',
  14289. ' end;']);
  14290. ConvertUnit;
  14291. CheckSource('TestClass_ExternalVar',
  14292. LinesToStr([
  14293. 'var $impl = $mod.$impl;',
  14294. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14295. ' this.DoIt = function () {',
  14296. ' this.$Intern = this.$Intern + 1;',
  14297. ' this.$Intern2 = this.$Intern2 + 2;',
  14298. ' this["A B"] = this["A B"] + 3;',
  14299. ' };',
  14300. ' });',
  14301. '']),
  14302. LinesToStr([
  14303. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14304. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14305. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14306. 'var $with = $impl.Obj;',
  14307. '$with.$Intern = $with.$Intern + 1;',
  14308. '$with.$Intern2 = $with.$Intern2 + 2;',
  14309. '$with["A B"] = $with["A B"] + 3;',
  14310. '']),
  14311. LinesToStr([ // implementation
  14312. '$impl.Obj = null;',
  14313. '']));
  14314. end;
  14315. procedure TTestModule.TestClass_Const;
  14316. begin
  14317. StartProgram(false);
  14318. Add([
  14319. 'type',
  14320. ' integer = longint;',
  14321. ' TClass = class of TObject;',
  14322. ' TObject = class',
  14323. ' public',
  14324. ' const cI: integer = 3;',
  14325. ' procedure DoIt;',
  14326. ' class procedure DoMore;',
  14327. ' end;',
  14328. 'procedure tobject.doit;',
  14329. 'begin',
  14330. ' if cI=4 then;',
  14331. ' if 5=cI then;',
  14332. ' if Self.cI=6 then;',
  14333. ' if 7=Self.cI then;',
  14334. ' with Self do begin',
  14335. ' if cI=11 then;',
  14336. ' if 12=cI then;',
  14337. ' end;',
  14338. 'end;',
  14339. 'class procedure tobject.domore;',
  14340. 'begin',
  14341. ' if cI=8 then;',
  14342. ' if Self.cI=9 then;',
  14343. ' if 10=cI then;',
  14344. ' if 11=Self.cI then;',
  14345. ' with Self do begin',
  14346. ' if cI=13 then;',
  14347. ' if 14=cI then;',
  14348. ' end;',
  14349. 'end;',
  14350. 'var',
  14351. ' Obj: TObject;',
  14352. ' Cla: TClass;',
  14353. 'begin',
  14354. ' if TObject.cI=21 then ;',
  14355. ' if Obj.cI=22 then ;',
  14356. ' if Cla.cI=23 then ;',
  14357. ' with obj do if ci=24 then;',
  14358. ' with TObject do if ci=25 then;',
  14359. ' with Cla do if ci=26 then;']);
  14360. ConvertProgram;
  14361. CheckSource('TestClass_Const',
  14362. LinesToStr([
  14363. 'rtl.createClass(this, "TObject", null, function () {',
  14364. ' this.cI = 3;',
  14365. ' this.$init = function () {',
  14366. ' };',
  14367. ' this.$final = function () {',
  14368. ' };',
  14369. ' this.DoIt = function () {',
  14370. ' if (this.cI === 4) ;',
  14371. ' if (5 === this.cI) ;',
  14372. ' if (this.cI === 6) ;',
  14373. ' if (7 === this.cI) ;',
  14374. ' if (this.cI === 11) ;',
  14375. ' if (12 === this.cI) ;',
  14376. ' };',
  14377. ' this.DoMore = function () {',
  14378. ' if (this.cI === 8) ;',
  14379. ' if (this.cI === 9) ;',
  14380. ' if (10 === this.cI) ;',
  14381. ' if (11 === this.cI) ;',
  14382. ' if (this.cI === 13) ;',
  14383. ' if (14 === this.cI) ;',
  14384. ' };',
  14385. '});',
  14386. 'this.Obj = null;',
  14387. 'this.Cla = null;',
  14388. '']),
  14389. LinesToStr([
  14390. 'if ($mod.TObject.cI === 21) ;',
  14391. 'if ($mod.Obj.cI === 22) ;',
  14392. 'if ($mod.Cla.cI === 23) ;',
  14393. 'var $with = $mod.Obj;',
  14394. 'if ($with.cI === 24) ;',
  14395. 'var $with1 = $mod.TObject;',
  14396. 'if ($with1.cI === 25) ;',
  14397. 'var $with2 = $mod.Cla;',
  14398. 'if ($with2.cI === 26) ;',
  14399. '']));
  14400. end;
  14401. procedure TTestModule.TestClass_ConstEnum;
  14402. begin
  14403. StartProgram(false);
  14404. Add([
  14405. 'type',
  14406. ' TEnum = (red,blue);',
  14407. ' TObject = class',
  14408. ' end;',
  14409. ' TAnimal = class',
  14410. ' public',
  14411. ' type TSubEnum = (light,dark);',
  14412. ' const a = high(TEnum);',
  14413. ' const b = high(TSubEnum);',
  14414. ' end;',
  14415. ' TBird = class(TAnimal)',
  14416. ' public',
  14417. ' const c = high(TEnum);',
  14418. ' const d = high(TSubEnum);',
  14419. ' end;',
  14420. ' TAnt = class',
  14421. ' public',
  14422. ' const e = high(TEnum);',
  14423. ' const f = high(TBird.TSubEnum);',
  14424. ' end;',
  14425. 'begin',
  14426. '']);
  14427. ConvertProgram;
  14428. CheckSource('TestClass_ConstEnum',
  14429. LinesToStr([
  14430. 'this.TEnum = {',
  14431. ' "0": "red",',
  14432. ' red: 0,',
  14433. ' "1": "blue",',
  14434. ' blue: 1',
  14435. '};',
  14436. 'rtl.createClass(this, "TObject", null, function () {',
  14437. ' this.$init = function () {',
  14438. ' };',
  14439. ' this.$final = function () {',
  14440. ' };',
  14441. '});',
  14442. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14443. ' this.TSubEnum = {',
  14444. ' "0": "light",',
  14445. ' light: 0,',
  14446. ' "1": "dark",',
  14447. ' dark: 1',
  14448. ' };',
  14449. ' this.a = $mod.TEnum.blue;',
  14450. ' this.b = this.TSubEnum.dark;',
  14451. '});',
  14452. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14453. ' this.c = $mod.TEnum.blue;',
  14454. ' this.d = this.TSubEnum.dark;',
  14455. '});',
  14456. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14457. ' this.e = $mod.TEnum.blue;',
  14458. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14459. '});',
  14460. '']),
  14461. LinesToStr([
  14462. '']));
  14463. end;
  14464. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14465. begin
  14466. StartProgram(false);
  14467. Add([
  14468. 'type',
  14469. ' TObject = class',
  14470. ' const cI: longint = 3;',
  14471. ' procedure Fly;',
  14472. ' procedure Run;',
  14473. ' end;',
  14474. ' TBird = class',
  14475. ' procedure Go;',
  14476. ' end;',
  14477. 'procedure tobject.fly;',
  14478. 'const cI: word = 4;',
  14479. 'begin',
  14480. ' if cI=Self.cI then ;',
  14481. 'end;',
  14482. 'procedure tobject.run;',
  14483. 'const cI: word = 5;',
  14484. 'begin',
  14485. ' if cI=Self.cI then ;',
  14486. 'end;',
  14487. 'procedure tbird.go;',
  14488. 'const cI: word = 6;',
  14489. 'begin',
  14490. ' if cI=Self.cI then ;',
  14491. 'end;',
  14492. 'begin',
  14493. '']);
  14494. ConvertProgram;
  14495. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14496. LinesToStr([
  14497. 'rtl.createClass(this, "TObject", null, function () {',
  14498. ' this.cI = 3;',
  14499. ' this.$init = function () {',
  14500. ' };',
  14501. ' this.$final = function () {',
  14502. ' };',
  14503. ' var cI$1 = 4;',
  14504. ' this.Fly = function () {',
  14505. ' if (cI$1 === this.cI) ;',
  14506. ' };',
  14507. ' var cI$2 = 5;',
  14508. ' this.Run = function () {',
  14509. ' if (cI$2 === this.cI) ;',
  14510. ' };',
  14511. '});',
  14512. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14513. ' var cI$3 = 6;',
  14514. ' this.Go = function () {',
  14515. ' if (cI$3 === this.cI) ;',
  14516. ' };',
  14517. '});',
  14518. '']),
  14519. LinesToStr([
  14520. '']));
  14521. end;
  14522. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14523. begin
  14524. StartUnit(false);
  14525. Add([
  14526. 'interface',
  14527. 'type',
  14528. ' TObject = class',
  14529. ' const cI: longint = 3;',
  14530. ' procedure Fly;',
  14531. ' procedure Run;',
  14532. ' end;',
  14533. ' TBird = class',
  14534. ' procedure Go;',
  14535. ' end;',
  14536. 'implementation',
  14537. 'procedure tobject.fly;',
  14538. 'const cI: word = 4;',
  14539. 'begin',
  14540. ' if cI=Self.cI then ;',
  14541. 'end;',
  14542. 'procedure tobject.run;',
  14543. 'const cI: word = 5;',
  14544. 'begin',
  14545. ' if cI=Self.cI then ;',
  14546. 'end;',
  14547. 'procedure tbird.go;',
  14548. 'const cI: word = 6;',
  14549. 'begin',
  14550. ' if cI=Self.cI then ;',
  14551. 'end;',
  14552. '']);
  14553. ConvertUnit;
  14554. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14555. LinesToStr([
  14556. 'rtl.createClass(this, "TObject", null, function () {',
  14557. ' this.cI = 3;',
  14558. ' this.$init = function () {',
  14559. ' };',
  14560. ' this.$final = function () {',
  14561. ' };',
  14562. ' var cI$1 = 4;',
  14563. ' this.Fly = function () {',
  14564. ' if (cI$1 === this.cI) ;',
  14565. ' };',
  14566. ' var cI$2 = 5;',
  14567. ' this.Run = function () {',
  14568. ' if (cI$2 === this.cI) ;',
  14569. ' };',
  14570. '});',
  14571. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14572. ' var cI$3 = 6;',
  14573. ' this.Go = function () {',
  14574. ' if (cI$3 === this.cI) ;',
  14575. ' };',
  14576. '});',
  14577. '']),
  14578. '',
  14579. '');
  14580. end;
  14581. procedure TTestModule.TestClass_LocalVarSelfFail;
  14582. begin
  14583. StartProgram(false);
  14584. Add([
  14585. 'type',
  14586. ' TObject = class',
  14587. ' constructor Create;',
  14588. ' end;',
  14589. 'constructor tobject.create;',
  14590. 'var self: longint;',
  14591. 'begin',
  14592. 'end',
  14593. 'begin',
  14594. '']);
  14595. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14596. ConvertProgram;
  14597. end;
  14598. procedure TTestModule.TestClass_ArgSelfFail;
  14599. begin
  14600. StartProgram(false);
  14601. Add([
  14602. 'type',
  14603. ' TObject = class',
  14604. ' procedure DoIt(Self: longint);',
  14605. ' end;',
  14606. 'procedure tobject.doit(self: longint);',
  14607. 'begin',
  14608. 'end',
  14609. 'begin',
  14610. '']);
  14611. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14612. ConvertProgram;
  14613. end;
  14614. procedure TTestModule.TestClass_NestedProcSelf;
  14615. begin
  14616. StartProgram(false);
  14617. Add([
  14618. 'type',
  14619. ' TObject = class',
  14620. ' Key: longint;',
  14621. ' class var State: longint;',
  14622. ' procedure DoIt;',
  14623. ' function GetSize: longint; virtual; abstract;',
  14624. ' procedure SetSize(Value: longint); virtual; abstract;',
  14625. ' property Size: longint read GetSize write SetSize;',
  14626. ' end;',
  14627. 'procedure tobject.doit;',
  14628. ' procedure Sub;',
  14629. ' begin',
  14630. ' key:=key+2;',
  14631. ' self.key:=self.key+3;',
  14632. ' state:=state+4;',
  14633. ' self.state:=self.state+5;',
  14634. ' tobject.state:=tobject.state+6;',
  14635. ' size:=size+7;',
  14636. ' self.size:=self.size+8;',
  14637. ' end;',
  14638. 'begin',
  14639. ' sub;',
  14640. ' key:=key+12;',
  14641. ' self.key:=self.key+13;',
  14642. ' state:=state+14;',
  14643. ' self.state:=self.state+15;',
  14644. ' tobject.state:=tobject.state+16;',
  14645. ' size:=size+17;',
  14646. ' self.size:=self.size+18;',
  14647. 'end;',
  14648. 'begin',
  14649. '']);
  14650. ConvertProgram;
  14651. CheckSource('TestClass_NestedProcSelf',
  14652. LinesToStr([ // statements
  14653. 'rtl.createClass(this, "TObject", null, function () {',
  14654. ' this.State = 0;',
  14655. ' this.$init = function () {',
  14656. ' this.Key = 0;',
  14657. ' };',
  14658. ' this.$final = function () {',
  14659. ' };',
  14660. ' this.DoIt = function () {',
  14661. ' var $Self = this;',
  14662. ' function Sub() {',
  14663. ' $Self.Key = $Self.Key + 2;',
  14664. ' $Self.Key = $Self.Key + 3;',
  14665. ' $mod.TObject.State = $Self.State + 4;',
  14666. ' $mod.TObject.State = $Self.State + 5;',
  14667. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14668. ' $Self.SetSize($Self.GetSize() + 7);',
  14669. ' $Self.SetSize($Self.GetSize() + 8);',
  14670. ' };',
  14671. ' Sub();',
  14672. ' this.Key = this.Key + 12;',
  14673. ' $Self.Key = $Self.Key + 13;',
  14674. ' $mod.TObject.State = this.State + 14;',
  14675. ' $mod.TObject.State = $Self.State + 15;',
  14676. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14677. ' this.SetSize(this.GetSize() + 17);',
  14678. ' $Self.SetSize($Self.GetSize() + 18);',
  14679. ' };',
  14680. '});',
  14681. '']),
  14682. LinesToStr([ // $mod.$main
  14683. '']));
  14684. end;
  14685. procedure TTestModule.TestClass_NestedProcSelf2;
  14686. begin
  14687. StartProgram(false);
  14688. Add([
  14689. 'type',
  14690. ' TObject = class',
  14691. ' Key: longint;',
  14692. ' class var State: longint;',
  14693. ' function GetSize: longint; virtual; abstract;',
  14694. ' procedure SetSize(Value: longint); virtual; abstract;',
  14695. ' property Size: longint read GetSize write SetSize;',
  14696. ' end;',
  14697. ' TBird = class',
  14698. ' procedure DoIt;',
  14699. ' end;',
  14700. 'procedure tbird.doit;',
  14701. ' procedure Sub;',
  14702. ' begin',
  14703. ' key:=key+2;',
  14704. ' self.key:=self.key+3;',
  14705. ' state:=state+4;',
  14706. ' self.state:=self.state+5;',
  14707. ' tobject.state:=tobject.state+6;',
  14708. ' size:=size+7;',
  14709. ' self.size:=self.size+8;',
  14710. ' end;',
  14711. 'begin',
  14712. ' sub;',
  14713. ' key:=key+12;',
  14714. ' self.key:=self.key+13;',
  14715. ' state:=state+14;',
  14716. ' self.state:=self.state+15;',
  14717. ' tobject.state:=tobject.state+16;',
  14718. ' size:=size+17;',
  14719. ' self.size:=self.size+18;',
  14720. 'end;',
  14721. 'begin',
  14722. '']);
  14723. ConvertProgram;
  14724. CheckSource('TestClass_NestedProcSelf2',
  14725. LinesToStr([ // statements
  14726. 'rtl.createClass(this, "TObject", null, function () {',
  14727. ' this.State = 0;',
  14728. ' this.$init = function () {',
  14729. ' this.Key = 0;',
  14730. ' };',
  14731. ' this.$final = function () {',
  14732. ' };',
  14733. '});',
  14734. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14735. ' this.DoIt = function () {',
  14736. ' var $Self = this;',
  14737. ' function Sub() {',
  14738. ' $Self.Key = $Self.Key + 2;',
  14739. ' $Self.Key = $Self.Key + 3;',
  14740. ' $mod.TObject.State = $Self.State + 4;',
  14741. ' $mod.TObject.State = $Self.State + 5;',
  14742. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14743. ' $Self.SetSize($Self.GetSize() + 7);',
  14744. ' $Self.SetSize($Self.GetSize() + 8);',
  14745. ' };',
  14746. ' Sub();',
  14747. ' this.Key = this.Key + 12;',
  14748. ' $Self.Key = $Self.Key + 13;',
  14749. ' $mod.TObject.State = this.State + 14;',
  14750. ' $mod.TObject.State = $Self.State + 15;',
  14751. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14752. ' this.SetSize(this.GetSize() + 17);',
  14753. ' $Self.SetSize($Self.GetSize() + 18);',
  14754. ' };',
  14755. '});',
  14756. '']),
  14757. LinesToStr([ // $mod.$main
  14758. '']));
  14759. end;
  14760. procedure TTestModule.TestClass_NestedProcClassSelf;
  14761. begin
  14762. StartProgram(false);
  14763. Add([
  14764. 'type',
  14765. ' TObject = class',
  14766. ' class var State: longint;',
  14767. ' class procedure DoIt;',
  14768. ' class function GetSize: longint; virtual; abstract;',
  14769. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14770. ' class property Size: longint read GetSize write SetSize;',
  14771. ' end;',
  14772. 'class procedure tobject.doit;',
  14773. ' procedure Sub;',
  14774. ' begin',
  14775. ' state:=state+2;',
  14776. ' self.state:=self.state+3;',
  14777. ' tobject.state:=tobject.state+4;',
  14778. ' size:=size+5;',
  14779. ' self.size:=self.size+6;',
  14780. ' tobject.size:=tobject.size+7;',
  14781. ' end;',
  14782. 'begin',
  14783. ' sub;',
  14784. ' state:=state+12;',
  14785. ' self.state:=self.state+13;',
  14786. ' tobject.state:=tobject.state+14;',
  14787. ' size:=size+15;',
  14788. ' self.size:=self.size+16;',
  14789. ' tobject.size:=tobject.size+17;',
  14790. 'end;',
  14791. 'begin',
  14792. '']);
  14793. ConvertProgram;
  14794. CheckSource('TestClass_NestedProcClassSelf',
  14795. LinesToStr([ // statements
  14796. 'rtl.createClass(this, "TObject", null, function () {',
  14797. ' this.State = 0;',
  14798. ' this.$init = function () {',
  14799. ' };',
  14800. ' this.$final = function () {',
  14801. ' };',
  14802. ' this.DoIt = function () {',
  14803. ' var $Self = this;',
  14804. ' function Sub() {',
  14805. ' $mod.TObject.State = $Self.State + 2;',
  14806. ' $mod.TObject.State = $Self.State + 3;',
  14807. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14808. ' $Self.SetSize($Self.GetSize() + 5);',
  14809. ' $Self.SetSize($Self.GetSize() + 6);',
  14810. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14811. ' };',
  14812. ' Sub();',
  14813. ' $mod.TObject.State = this.State + 12;',
  14814. ' $mod.TObject.State = $Self.State + 13;',
  14815. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14816. ' this.SetSize(this.GetSize() + 15);',
  14817. ' $Self.SetSize($Self.GetSize() + 16);',
  14818. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14819. ' };',
  14820. '});',
  14821. '']),
  14822. LinesToStr([ // $mod.$main
  14823. '']));
  14824. end;
  14825. procedure TTestModule.TestClass_NestedProcCallInherited;
  14826. begin
  14827. StartProgram(false);
  14828. Add([
  14829. 'type',
  14830. ' TObject = class',
  14831. ' function DoIt(k: boolean): longint; virtual;',
  14832. ' end;',
  14833. ' TBird = class',
  14834. ' function DoIt(k: boolean): longint; override;',
  14835. ' end;',
  14836. 'function tobject.doit(k: boolean): longint;',
  14837. 'begin',
  14838. 'end;',
  14839. 'function tbird.doit(k: boolean): longint;',
  14840. ' procedure Sub;',
  14841. ' begin',
  14842. ' inherited DoIt(true);',
  14843. //' if inherited DoIt(false)=4 then ;',
  14844. ' end;',
  14845. 'begin',
  14846. ' Sub;',
  14847. ' inherited;',
  14848. ' inherited DoIt(true);',
  14849. //' if inherited DoIt(false)=14 then ;',
  14850. 'end;',
  14851. 'begin',
  14852. '']);
  14853. ConvertProgram;
  14854. CheckSource('TestClass_NestedProcCallInherited',
  14855. LinesToStr([ // statements
  14856. 'rtl.createClass(this, "TObject", null, function () {',
  14857. ' this.$init = function () {',
  14858. ' };',
  14859. ' this.$final = function () {',
  14860. ' };',
  14861. ' this.DoIt = function (k) {',
  14862. ' var Result = 0;',
  14863. ' return Result;',
  14864. ' };',
  14865. '});',
  14866. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14867. ' this.DoIt = function (k) {',
  14868. ' var $Self = this;',
  14869. ' var Result = 0;',
  14870. ' function Sub() {',
  14871. ' $mod.TObject.DoIt.call($Self, true);',
  14872. ' };',
  14873. ' Sub();',
  14874. ' $mod.TObject.DoIt.apply(this, arguments);',
  14875. ' $mod.TObject.DoIt.call(this, true);',
  14876. ' return Result;',
  14877. ' };',
  14878. '});',
  14879. '']),
  14880. LinesToStr([ // $mod.$main
  14881. '']));
  14882. end;
  14883. procedure TTestModule.TestClass_TObjectFree;
  14884. begin
  14885. StartProgram(false);
  14886. Add([
  14887. 'type',
  14888. ' TObject = class',
  14889. ' Obj: tobject;',
  14890. ' procedure Free;',
  14891. ' procedure Release;',
  14892. ' end;',
  14893. 'procedure tobject.free;',
  14894. 'begin',
  14895. 'end;',
  14896. 'procedure tobject.release;',
  14897. 'begin',
  14898. ' free;',
  14899. ' if true then free;',
  14900. 'end;',
  14901. 'function DoIt(o: tobject): tobject;',
  14902. 'var l: tobject;',
  14903. 'begin',
  14904. ' o.free;',
  14905. ' o.free();',
  14906. ' l.free;',
  14907. ' l.free();',
  14908. ' o.obj.free;',
  14909. ' o.obj.free();',
  14910. ' with o do obj.free;',
  14911. ' with o do obj.free();',
  14912. ' result.Free;',
  14913. ' result.Free();',
  14914. 'end;',
  14915. 'var o: tobject;',
  14916. ' a: array of tobject;',
  14917. 'begin',
  14918. ' o.free;',
  14919. ' o.obj.free;',
  14920. ' a[1+2].free;',
  14921. '']);
  14922. ConvertProgram;
  14923. CheckSource('TestClass_TObjectFree',
  14924. LinesToStr([ // statements
  14925. 'rtl.createClass(this, "TObject", null, function () {',
  14926. ' this.$init = function () {',
  14927. ' this.Obj = null;',
  14928. ' };',
  14929. ' this.$final = function () {',
  14930. ' this.Obj = undefined;',
  14931. ' };',
  14932. ' this.Free = function () {',
  14933. ' };',
  14934. ' this.Release = function () {',
  14935. ' this.Free();',
  14936. ' if (true) this.Free();',
  14937. ' };',
  14938. '});',
  14939. 'this.DoIt = function (o) {',
  14940. ' var Result = null;',
  14941. ' var l = null;',
  14942. ' o = rtl.freeLoc(o);',
  14943. ' o = rtl.freeLoc(o);',
  14944. ' l = rtl.freeLoc(l);',
  14945. ' l = rtl.freeLoc(l);',
  14946. ' rtl.free(o, "Obj");',
  14947. ' rtl.free(o, "Obj");',
  14948. ' rtl.free(o, "Obj");',
  14949. ' rtl.free(o, "Obj");',
  14950. ' Result = rtl.freeLoc(Result);',
  14951. ' Result = rtl.freeLoc(Result);',
  14952. ' return Result;',
  14953. '};',
  14954. 'this.o = null;',
  14955. 'this.a = [];',
  14956. '']),
  14957. LinesToStr([ // $mod.$main
  14958. 'rtl.free($mod, "o");',
  14959. 'rtl.free($mod.o, "Obj");',
  14960. 'rtl.free($mod.a, 1 + 2);',
  14961. '']));
  14962. end;
  14963. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14964. begin
  14965. StartProgram(false);
  14966. Add([
  14967. 'type',
  14968. ' TObject = class',
  14969. ' Obj: tobject;',
  14970. ' procedure Free;',
  14971. ' end;',
  14972. 'procedure tobject.free;',
  14973. 'begin',
  14974. 'end;',
  14975. 'procedure DoIt(var o: tobject);',
  14976. 'begin',
  14977. ' o.free;',
  14978. ' o.free();',
  14979. 'end;',
  14980. 'begin',
  14981. '']);
  14982. ConvertProgram;
  14983. CheckSource('TestClass_TObjectFree_VarArg',
  14984. LinesToStr([ // statements
  14985. 'rtl.createClass(this, "TObject", null, function () {',
  14986. ' this.$init = function () {',
  14987. ' this.Obj = null;',
  14988. ' };',
  14989. ' this.$final = function () {',
  14990. ' this.Obj = undefined;',
  14991. ' };',
  14992. ' this.Free = function () {',
  14993. ' };',
  14994. '});',
  14995. 'this.DoIt = function (o) {',
  14996. ' o.set(rtl.freeLoc(o.get()));',
  14997. ' o.set(rtl.freeLoc(o.get()));',
  14998. '};',
  14999. '']),
  15000. LinesToStr([ // $mod.$main
  15001. '']));
  15002. end;
  15003. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15004. begin
  15005. StartProgram(false);
  15006. Add([
  15007. 'type',
  15008. ' TObject = class',
  15009. ' constructor Create;',
  15010. ' procedure Free;',
  15011. ' end;',
  15012. 'constructor TObject.Create; begin end;',
  15013. 'procedure tobject.free; begin end;',
  15014. 'begin',
  15015. ' with tobject.create do free;',
  15016. '']);
  15017. ConvertProgram;
  15018. CheckSource('TestClass_TObjectFreeNewInstance',
  15019. LinesToStr([ // statements
  15020. 'rtl.createClass(this, "TObject", null, function () {',
  15021. ' this.$init = function () {',
  15022. ' };',
  15023. ' this.$final = function () {',
  15024. ' };',
  15025. ' this.Create = function () {',
  15026. ' return this;',
  15027. ' };',
  15028. ' this.Free = function () {',
  15029. ' };',
  15030. '});',
  15031. '']),
  15032. LinesToStr([ // $mod.$main
  15033. 'var $with = $mod.TObject.$create("Create");',
  15034. '$with=rtl.freeLoc($with);',
  15035. '']));
  15036. end;
  15037. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15038. begin
  15039. StartProgram(false);
  15040. Add([
  15041. 'type',
  15042. ' TObject = class',
  15043. ' destructor Destroy;',
  15044. ' procedure Free;',
  15045. ' end;',
  15046. 'destructor TObject.Destroy; begin end;',
  15047. 'procedure tobject.free; begin end;',
  15048. 'var o: tobject;',
  15049. 'begin',
  15050. ' o.free;',
  15051. '']);
  15052. Converter.UseLowerCase:=true;
  15053. ConvertProgram;
  15054. CheckSource('TestClass_TObjectFreeLowerCase',
  15055. LinesToStr([ // statements
  15056. 'rtl.createClass(this, "tobject", null, function () {',
  15057. ' this.$init = function () {',
  15058. ' };',
  15059. ' this.$final = function () {',
  15060. ' };',
  15061. ' rtl.tObjectDestroy = "destroy";',
  15062. ' this.destroy = function () {',
  15063. ' };',
  15064. ' this.free = function () {',
  15065. ' };',
  15066. '});',
  15067. 'this.o = null;',
  15068. '']),
  15069. LinesToStr([ // $mod.$main
  15070. 'rtl.free($mod, "o");',
  15071. '']));
  15072. end;
  15073. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15074. begin
  15075. StartProgram(false);
  15076. Add([
  15077. 'type',
  15078. ' TObject = class',
  15079. ' procedure Free;',
  15080. ' function GetObj: tobject; virtual; abstract;',
  15081. ' end;',
  15082. 'procedure tobject.free;',
  15083. 'begin',
  15084. 'end;',
  15085. 'var o: tobject;',
  15086. 'begin',
  15087. ' o.getobj.free;',
  15088. '']);
  15089. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15090. ConvertProgram;
  15091. end;
  15092. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15093. begin
  15094. StartProgram(false);
  15095. Add([
  15096. 'type',
  15097. ' TObject = class',
  15098. ' procedure Free;',
  15099. ' FObj: TObject;',
  15100. ' property Obj: tobject read FObj write FObj;',
  15101. ' end;',
  15102. 'procedure tobject.free;',
  15103. 'begin',
  15104. 'end;',
  15105. 'var o: tobject;',
  15106. 'begin',
  15107. ' o.obj.free;',
  15108. '']);
  15109. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15110. ConvertProgram;
  15111. end;
  15112. procedure TTestModule.TestClass_ForIn;
  15113. begin
  15114. StartProgram(false);
  15115. Add([
  15116. 'type',
  15117. ' TObject = class end;',
  15118. ' TItem = TObject;',
  15119. ' TEnumerator = class',
  15120. ' FCurrent: TItem;',
  15121. ' property Current: TItem read FCurrent;',
  15122. ' function MoveNext: boolean;',
  15123. ' end;',
  15124. ' TBird = class',
  15125. ' function GetEnumerator: TEnumerator;',
  15126. ' end;',
  15127. 'function TEnumerator.MoveNext: boolean;',
  15128. 'begin',
  15129. 'end;',
  15130. 'function TBird.GetEnumerator: TEnumerator;',
  15131. 'begin',
  15132. 'end;',
  15133. 'var',
  15134. ' b: TBird;',
  15135. ' i, i2: TItem;',
  15136. 'begin',
  15137. ' for i in b do i2:=i;']);
  15138. ConvertProgram;
  15139. CheckSource('TestClass_ForIn',
  15140. LinesToStr([ // statements
  15141. 'rtl.createClass(this, "TObject", null, function () {',
  15142. ' this.$init = function () {',
  15143. ' };',
  15144. ' this.$final = function () {',
  15145. ' };',
  15146. '});',
  15147. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15148. ' this.$init = function () {',
  15149. ' $mod.TObject.$init.call(this);',
  15150. ' this.FCurrent = null;',
  15151. ' };',
  15152. ' this.$final = function () {',
  15153. ' this.FCurrent = undefined;',
  15154. ' $mod.TObject.$final.call(this);',
  15155. ' };',
  15156. ' this.MoveNext = function () {',
  15157. ' var Result = false;',
  15158. ' return Result;',
  15159. ' };',
  15160. '});',
  15161. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15162. ' this.GetEnumerator = function () {',
  15163. ' var Result = null;',
  15164. ' return Result;',
  15165. ' };',
  15166. '});',
  15167. 'this.b = null;',
  15168. 'this.i = null;',
  15169. 'this.i2 = null;'
  15170. ]),
  15171. LinesToStr([ // $mod.$main
  15172. 'var $in = $mod.b.GetEnumerator();',
  15173. 'try {',
  15174. ' while ($in.MoveNext()){',
  15175. ' $mod.i = $in.FCurrent;',
  15176. ' $mod.i2 = $mod.i;',
  15177. ' }',
  15178. '} finally {',
  15179. ' $in = rtl.freeLoc($in)',
  15180. '};',
  15181. '']));
  15182. end;
  15183. procedure TTestModule.TestClass_DispatchMessage;
  15184. begin
  15185. StartProgram(false);
  15186. Add([
  15187. 'type',
  15188. ' TObject = class',
  15189. ' {$DispatchField DispInt}',
  15190. ' procedure Dispatch(var Msg); virtual; abstract;',
  15191. ' {$DispatchStrField DispStr}',
  15192. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15193. ' end;',
  15194. ' THopMsg = record',
  15195. ' DispInt: longint;',
  15196. ' end;',
  15197. ' TPutMsg = record',
  15198. ' DispStr: string;',
  15199. ' end;',
  15200. ' TBird = class',
  15201. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15202. ' procedure Run; overload; virtual; abstract;',
  15203. ' procedure Run(var Msg); overload; message ''Fast'';',
  15204. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15205. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15206. ' end;',
  15207. 'procedure TBird.Run(var Msg);',
  15208. 'begin',
  15209. 'end;',
  15210. 'begin',
  15211. '']);
  15212. ConvertProgram;
  15213. CheckSource('TestClass_Message',
  15214. LinesToStr([ // statements
  15215. 'rtl.createClass(this, "TObject", null, function () {',
  15216. ' this.$init = function () {',
  15217. ' };',
  15218. ' this.$final = function () {',
  15219. ' };',
  15220. '});',
  15221. 'rtl.recNewT(this, "THopMsg", function () {',
  15222. ' this.DispInt = 0;',
  15223. ' this.$eq = function (b) {',
  15224. ' return this.DispInt === b.DispInt;',
  15225. ' };',
  15226. ' this.$assign = function (s) {',
  15227. ' this.DispInt = s.DispInt;',
  15228. ' return this;',
  15229. ' };',
  15230. '});',
  15231. 'rtl.recNewT(this, "TPutMsg", function () {',
  15232. ' this.DispStr = "";',
  15233. ' this.$eq = function (b) {',
  15234. ' return this.DispStr === b.DispStr;',
  15235. ' };',
  15236. ' this.$assign = function (s) {',
  15237. ' this.DispStr = s.DispStr;',
  15238. ' return this;',
  15239. ' };',
  15240. '});',
  15241. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15242. ' this.Run$1 = function (Msg) {',
  15243. ' };',
  15244. ' this.$msgint = {',
  15245. ' "2": "Fly",',
  15246. ' "3": "Hop"',
  15247. ' };',
  15248. ' this.$msgstr = {',
  15249. ' Fast: "Run$1",',
  15250. ' foo: "Put"',
  15251. ' };',
  15252. '});',
  15253. '']),
  15254. LinesToStr([ // $mod.$main
  15255. '']));
  15256. end;
  15257. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15258. begin
  15259. StartProgram(false);
  15260. Add([
  15261. 'type',
  15262. ' TObject = class',
  15263. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15264. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15265. ' end;',
  15266. 'begin',
  15267. '']);
  15268. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15269. ConvertProgram;
  15270. end;
  15271. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15272. begin
  15273. StartProgram(false);
  15274. Add([
  15275. 'type',
  15276. ' TObject = class',
  15277. ' {$dispatchfield Msg}',
  15278. ' procedure Dispatch(var Msg); virtual; abstract;',
  15279. ' end;',
  15280. ' TFlyMsg = record',
  15281. ' FlyId: longint;',
  15282. ' end;',
  15283. ' TBird = class',
  15284. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15285. ' end;',
  15286. 'begin',
  15287. '']);
  15288. ConvertProgram;
  15289. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15290. end;
  15291. procedure TTestModule.TestClassOf_Create;
  15292. begin
  15293. StartProgram(false);
  15294. Add('type');
  15295. Add(' TObject = class');
  15296. Add(' constructor Create;');
  15297. Add(' end;');
  15298. Add(' TClass = class of TObject;');
  15299. Add('constructor tobject.create; begin end;');
  15300. Add('var');
  15301. Add(' Obj: tobject;');
  15302. Add(' C: tclass;');
  15303. Add('begin');
  15304. Add(' obj:=C.create;');
  15305. Add(' with c do obj:=create;');
  15306. ConvertProgram;
  15307. CheckSource('TestClassOf_Create',
  15308. LinesToStr([ // statements
  15309. 'rtl.createClass(this, "TObject", null, function () {',
  15310. ' this.$init = function () {',
  15311. ' };',
  15312. ' this.$final = function () {',
  15313. ' };',
  15314. ' this.Create = function () {',
  15315. ' return this;',
  15316. ' };',
  15317. '});',
  15318. 'this.Obj = null;',
  15319. 'this.C = null;'
  15320. ]),
  15321. LinesToStr([ // $mod.$main
  15322. '$mod.Obj = $mod.C.$create("Create");',
  15323. 'var $with = $mod.C;',
  15324. '$mod.Obj = $with.$create("Create");',
  15325. '']));
  15326. end;
  15327. procedure TTestModule.TestClassOf_Call;
  15328. begin
  15329. StartProgram(false);
  15330. Add('type');
  15331. Add(' TObject = class');
  15332. Add(' class procedure DoIt;');
  15333. Add(' end;');
  15334. Add(' TClass = class of TObject;');
  15335. Add('class procedure tobject.doit; begin end;');
  15336. Add('var');
  15337. Add(' C: tclass;');
  15338. Add('begin');
  15339. Add(' c.doit;');
  15340. Add(' with c do doit;');
  15341. ConvertProgram;
  15342. CheckSource('TestClassOf_Call',
  15343. LinesToStr([ // statements
  15344. 'rtl.createClass(this, "TObject", null, function () {',
  15345. ' this.$init = function () {',
  15346. ' };',
  15347. ' this.$final = function () {',
  15348. ' };',
  15349. ' this.DoIt = function () {',
  15350. ' };',
  15351. '});',
  15352. 'this.C = null;'
  15353. ]),
  15354. LinesToStr([ // $mod.$main
  15355. '$mod.C.DoIt();',
  15356. 'var $with = $mod.C;',
  15357. '$with.DoIt();',
  15358. '']));
  15359. end;
  15360. procedure TTestModule.TestClassOf_Assign;
  15361. begin
  15362. StartProgram(false);
  15363. Add('type');
  15364. Add(' TClass = class of TObject;');
  15365. Add(' TObject = class');
  15366. Add(' ClassType: TClass; ');
  15367. Add(' end;');
  15368. Add('var');
  15369. Add(' Obj: tobject;');
  15370. Add(' C: tclass;');
  15371. Add('begin');
  15372. Add(' c:=nil;');
  15373. Add(' c:=obj.classtype;');
  15374. ConvertProgram;
  15375. CheckSource('TestClassOf_Assign',
  15376. LinesToStr([ // statements
  15377. 'rtl.createClass(this, "TObject", null, function () {',
  15378. ' this.$init = function () {',
  15379. ' this.ClassType = null;',
  15380. ' };',
  15381. ' this.$final = function () {',
  15382. ' this.ClassType = undefined;',
  15383. ' };',
  15384. '});',
  15385. 'this.Obj = null;',
  15386. 'this.C = null;'
  15387. ]),
  15388. LinesToStr([ // $mod.$main
  15389. '$mod.C = null;',
  15390. '$mod.C = $mod.Obj.ClassType;',
  15391. '']));
  15392. end;
  15393. procedure TTestModule.TestClassOf_Is;
  15394. begin
  15395. StartProgram(false);
  15396. Add('type');
  15397. Add(' TClass = class of TObject;');
  15398. Add(' TObject = class');
  15399. Add(' end;');
  15400. Add(' TCar = class');
  15401. Add(' end;');
  15402. Add(' TCars = class of TCar;');
  15403. Add('var');
  15404. Add(' Obj: tobject;');
  15405. Add(' C: tclass;');
  15406. Add(' Cars: tcars;');
  15407. Add('begin');
  15408. Add(' if c is tcar then ;');
  15409. Add(' if c is tcars then ;');
  15410. ConvertProgram;
  15411. CheckSource('TestClassOf_Is',
  15412. LinesToStr([ // statements
  15413. 'rtl.createClass(this, "TObject", null, function () {',
  15414. ' this.$init = function () {',
  15415. ' };',
  15416. ' this.$final = function () {',
  15417. ' };',
  15418. '});',
  15419. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15420. '});',
  15421. 'this.Obj = null;',
  15422. 'this.C = null;',
  15423. 'this.Cars = null;'
  15424. ]),
  15425. LinesToStr([ // $mod.$main
  15426. 'if(rtl.is($mod.C,$mod.TCar));',
  15427. 'if(rtl.is($mod.C,$mod.TCar));',
  15428. '']));
  15429. end;
  15430. procedure TTestModule.TestClassOf_Compare;
  15431. begin
  15432. StartProgram(false);
  15433. Add('type');
  15434. Add(' TClass = class of TObject;');
  15435. Add(' TObject = class');
  15436. Add(' ClassType: TClass; ');
  15437. Add(' end;');
  15438. Add('var');
  15439. Add(' b: boolean;');
  15440. Add(' Obj: tobject;');
  15441. Add(' C: tclass;');
  15442. Add('begin');
  15443. Add(' b:=c=nil;');
  15444. Add(' b:=nil=c;');
  15445. Add(' b:=c=obj.classtype;');
  15446. Add(' b:=obj.classtype=c;');
  15447. Add(' b:=c=TObject;');
  15448. Add(' b:=TObject=c;');
  15449. Add(' b:=c<>nil;');
  15450. Add(' b:=nil<>c;');
  15451. Add(' b:=c<>obj.classtype;');
  15452. Add(' b:=obj.classtype<>c;');
  15453. Add(' b:=c<>TObject;');
  15454. Add(' b:=TObject<>c;');
  15455. ConvertProgram;
  15456. CheckSource('TestClassOf_Compare',
  15457. LinesToStr([ // statements
  15458. 'rtl.createClass(this, "TObject", null, function () {',
  15459. ' this.$init = function () {',
  15460. ' this.ClassType = null;',
  15461. ' };',
  15462. ' this.$final = function () {',
  15463. ' this.ClassType = undefined;',
  15464. ' };',
  15465. '});',
  15466. 'this.b = false;',
  15467. 'this.Obj = null;',
  15468. 'this.C = null;'
  15469. ]),
  15470. LinesToStr([ // $mod.$main
  15471. '$mod.b = $mod.C === null;',
  15472. '$mod.b = null === $mod.C;',
  15473. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15474. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15475. '$mod.b = $mod.C === $mod.TObject;',
  15476. '$mod.b = $mod.TObject === $mod.C;',
  15477. '$mod.b = $mod.C !== null;',
  15478. '$mod.b = null !== $mod.C;',
  15479. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15480. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15481. '$mod.b = $mod.C !== $mod.TObject;',
  15482. '$mod.b = $mod.TObject !== $mod.C;',
  15483. '']));
  15484. end;
  15485. procedure TTestModule.TestClassOf_ClassVar;
  15486. begin
  15487. StartProgram(false);
  15488. Add('type');
  15489. Add(' TObject = class');
  15490. Add(' class var id: longint;');
  15491. Add(' end;');
  15492. Add(' TClass = class of TObject;');
  15493. Add('var');
  15494. Add(' C: tclass;');
  15495. Add('begin');
  15496. Add(' C.id:=C.id;');
  15497. ConvertProgram;
  15498. CheckSource('TestClassOf_ClassVar',
  15499. LinesToStr([ // statements
  15500. 'rtl.createClass(this, "TObject", null, function () {',
  15501. ' this.id = 0;',
  15502. ' this.$init = function () {',
  15503. ' };',
  15504. ' this.$final = function () {',
  15505. ' };',
  15506. '});',
  15507. 'this.C = null;'
  15508. ]),
  15509. LinesToStr([ // $mod.$main
  15510. '$mod.TObject.id = $mod.C.id;',
  15511. '']));
  15512. end;
  15513. procedure TTestModule.TestClassOf_ClassMethod;
  15514. begin
  15515. StartProgram(false);
  15516. Add('type');
  15517. Add(' TObject = class');
  15518. Add(' class function DoIt(i: longint = 0): longint;');
  15519. Add(' end;');
  15520. Add(' TClass = class of TObject;');
  15521. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15522. Add('var');
  15523. Add(' i: longint;');
  15524. Add(' C: tclass;');
  15525. Add('begin');
  15526. Add(' C.DoIt;');
  15527. Add(' C.DoIt();');
  15528. Add(' i:=C.DoIt;');
  15529. Add(' i:=C.DoIt();');
  15530. ConvertProgram;
  15531. CheckSource('TestClassOf_ClassMethod',
  15532. LinesToStr([ // statements
  15533. 'rtl.createClass(this, "TObject", null, function () {',
  15534. ' this.$init = function () {',
  15535. ' };',
  15536. ' this.$final = function () {',
  15537. ' };',
  15538. ' this.DoIt = function (i) {',
  15539. ' var Result = 0;',
  15540. ' return Result;',
  15541. ' };',
  15542. '});',
  15543. 'this.i = 0;',
  15544. 'this.C = null;'
  15545. ]),
  15546. LinesToStr([ // $mod.$main
  15547. '$mod.C.DoIt(0);',
  15548. '$mod.C.DoIt(0);',
  15549. '$mod.i = $mod.C.DoIt(0);',
  15550. '$mod.i = $mod.C.DoIt(0);',
  15551. '']));
  15552. end;
  15553. procedure TTestModule.TestClassOf_ClassProperty;
  15554. begin
  15555. StartProgram(false);
  15556. Add([
  15557. 'type',
  15558. ' TObject = class',
  15559. ' class var FA: longint;',
  15560. ' class function GetA: longint;',
  15561. ' class procedure SetA(Value: longint);',
  15562. ' class property pA: longint read fa write fa;',
  15563. ' class property pB: longint read geta write seta;',
  15564. ' end;',
  15565. ' TObjectClass = class of tobject;',
  15566. 'class function tobject.geta: longint; begin end;',
  15567. 'class procedure tobject.seta(value: longint); begin end;',
  15568. 'var',
  15569. ' b: boolean;',
  15570. ' Obj: tobject;',
  15571. ' Cla: tobjectclass;',
  15572. 'begin',
  15573. ' obj.pa:=obj.pa;',
  15574. ' obj.pb:=obj.pb;',
  15575. ' b:=obj.pa=4;',
  15576. ' b:=obj.pb=obj.pb;',
  15577. ' b:=5=obj.pa;',
  15578. ' cla.pa:=6;',
  15579. ' cla.pa:=cla.pa;',
  15580. ' cla.pb:=cla.pb;',
  15581. ' b:=cla.pa=7;',
  15582. ' b:=cla.pb=cla.pb;',
  15583. ' b:=8=cla.pa;',
  15584. ' tobject.pa:=9;',
  15585. ' tobject.pb:=tobject.pb;',
  15586. ' b:=tobject.pa=10;',
  15587. ' b:=11=tobject.pa;',
  15588. '']);
  15589. ConvertProgram;
  15590. CheckSource('TestClassOf_ClassProperty',
  15591. LinesToStr([ // statements
  15592. 'rtl.createClass(this, "TObject", null, function () {',
  15593. ' this.FA = 0;',
  15594. ' this.$init = function () {',
  15595. ' };',
  15596. ' this.$final = function () {',
  15597. ' };',
  15598. ' this.GetA = function () {',
  15599. ' var Result = 0;',
  15600. ' return Result;',
  15601. ' };',
  15602. ' this.SetA = function (Value) {',
  15603. ' };',
  15604. '});',
  15605. 'this.b = false;',
  15606. 'this.Obj = null;',
  15607. 'this.Cla = null;'
  15608. ]),
  15609. LinesToStr([ // $mod.$main
  15610. '$mod.TObject.FA = $mod.Obj.FA;',
  15611. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15612. '$mod.b = $mod.Obj.FA === 4;',
  15613. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15614. '$mod.b = 5 === $mod.Obj.FA;',
  15615. '$mod.TObject.FA = 6;',
  15616. '$mod.TObject.FA = $mod.Cla.FA;',
  15617. '$mod.Cla.SetA($mod.Cla.GetA());',
  15618. '$mod.b = $mod.Cla.FA === 7;',
  15619. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15620. '$mod.b = 8 === $mod.Cla.FA;',
  15621. '$mod.TObject.FA = 9;',
  15622. '$mod.TObject.SetA($mod.TObject.GetA());',
  15623. '$mod.b = $mod.TObject.FA === 10;',
  15624. '$mod.b = 11 === $mod.TObject.FA;',
  15625. '']));
  15626. end;
  15627. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15628. begin
  15629. StartProgram(false);
  15630. Add('type');
  15631. Add(' TObject = class');
  15632. Add(' class var GlobalId: longint;');
  15633. Add(' class procedure ProcA;');
  15634. Add(' end;');
  15635. Add('class procedure tobject.proca;');
  15636. Add('var b: boolean;');
  15637. Add('begin');
  15638. Add(' b:=self=nil;');
  15639. Add(' b:=self.globalid=3;');
  15640. Add(' b:=4=self.globalid;');
  15641. Add(' self.globalid:=5;');
  15642. Add(' self.proca;');
  15643. Add('end;');
  15644. Add('begin');
  15645. ConvertProgram;
  15646. CheckSource('TestClassOf_ClassMethodSelf',
  15647. LinesToStr([ // statements
  15648. 'rtl.createClass(this, "TObject", null, function () {',
  15649. ' this.GlobalId = 0;',
  15650. ' this.$init = function () {',
  15651. ' };',
  15652. ' this.$final = function () {',
  15653. ' };',
  15654. ' this.ProcA = function () {',
  15655. ' var b = false;',
  15656. ' b = this === null;',
  15657. ' b = this.GlobalId === 3;',
  15658. ' b = 4 === this.GlobalId;',
  15659. ' $mod.TObject.GlobalId = 5;',
  15660. ' this.ProcA();',
  15661. ' };',
  15662. '});'
  15663. ]),
  15664. LinesToStr([ // $mod.$main
  15665. '']));
  15666. end;
  15667. procedure TTestModule.TestClassOf_TypeCast;
  15668. begin
  15669. StartProgram(false);
  15670. Add('type');
  15671. Add(' TObject = class');
  15672. Add(' class procedure {#TObject_DoIt}DoIt;');
  15673. Add(' end;');
  15674. Add(' TClass = class of TObject;');
  15675. Add(' TMobile = class');
  15676. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15677. Add(' end;');
  15678. Add(' TMobileClass = class of TMobile;');
  15679. Add(' TCar = class(TMobile)');
  15680. Add(' class procedure {#TCar_DoIt}DoIt;');
  15681. Add(' end;');
  15682. Add(' TCarClass = class of TCar;');
  15683. Add('class procedure TObject.DoIt;');
  15684. Add('begin');
  15685. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15686. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15687. Add('end;');
  15688. Add('class procedure TMobile.DoIt;');
  15689. Add('begin');
  15690. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15691. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15692. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15693. Add('end;');
  15694. Add('class procedure TCar.DoIt; begin end;');
  15695. Add('var');
  15696. Add(' ObjC: TClass;');
  15697. Add(' MobileC: TMobileClass;');
  15698. Add(' CarC: TCarClass;');
  15699. Add('begin');
  15700. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15701. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15702. Add(' CarC.{@TCar_DoIt}DoIt;');
  15703. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15704. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15705. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15706. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15707. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15708. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15709. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15710. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15711. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15712. ConvertProgram;
  15713. CheckSource('TestClassOf_TypeCast',
  15714. LinesToStr([ // statements
  15715. 'rtl.createClass(this, "TObject", null, function () {',
  15716. ' this.$init = function () {',
  15717. ' };',
  15718. ' this.$final = function () {',
  15719. ' };',
  15720. ' this.DoIt = function () {',
  15721. ' this.DoIt();',
  15722. ' this.DoIt$1();',
  15723. ' };',
  15724. '});',
  15725. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15726. ' this.DoIt$1 = function () {',
  15727. ' this.DoIt();',
  15728. ' this.DoIt$1();',
  15729. ' this.DoIt$2();',
  15730. ' };',
  15731. '});',
  15732. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15733. ' this.DoIt$2 = function () {',
  15734. ' };',
  15735. '});',
  15736. 'this.ObjC = null;',
  15737. 'this.MobileC = null;',
  15738. 'this.CarC = null;',
  15739. '']),
  15740. LinesToStr([ // $mod.$main
  15741. '$mod.ObjC.DoIt();',
  15742. '$mod.MobileC.DoIt$1();',
  15743. '$mod.CarC.DoIt$2();',
  15744. '$mod.ObjC.DoIt();',
  15745. '$mod.ObjC.DoIt$1();',
  15746. '$mod.ObjC.DoIt$2();',
  15747. '$mod.MobileC.DoIt();',
  15748. '$mod.MobileC.DoIt$1();',
  15749. '$mod.MobileC.DoIt$2();',
  15750. '$mod.CarC.DoIt();',
  15751. '$mod.CarC.DoIt$1();',
  15752. '$mod.CarC.DoIt$2();',
  15753. '']));
  15754. end;
  15755. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15756. begin
  15757. StartProgram(false);
  15758. Add('type');
  15759. Add(' TObject = class');
  15760. Add(' function CurNow: longint; ');
  15761. Add(' class function Now: longint; ');
  15762. Add(' end;');
  15763. Add('function TObject.CurNow: longint; begin end;');
  15764. Add('class function TObject.Now: longint; begin end;');
  15765. Add('var');
  15766. Add(' Obj: tobject;');
  15767. Add(' vI: longint;');
  15768. Add('begin');
  15769. Add(' obj.curnow;');
  15770. Add(' vi:=obj.curnow;');
  15771. Add(' tobject.now;');
  15772. Add(' vi:=tobject.now;');
  15773. ConvertProgram;
  15774. CheckSource('TestClassOf_ImplicitFunctionCall',
  15775. LinesToStr([ // statements
  15776. 'rtl.createClass(this, "TObject", null, function () {',
  15777. ' this.$init = function () {',
  15778. ' };',
  15779. ' this.$final = function () {',
  15780. ' };',
  15781. ' this.CurNow = function () {',
  15782. ' var Result = 0;',
  15783. ' return Result;',
  15784. ' };',
  15785. ' this.Now = function () {',
  15786. ' var Result = 0;',
  15787. ' return Result;',
  15788. ' };',
  15789. '});',
  15790. 'this.Obj = null;',
  15791. 'this.vI = 0;',
  15792. '']),
  15793. LinesToStr([ // $mod.$main
  15794. '$mod.Obj.CurNow();',
  15795. '$mod.vI = $mod.Obj.CurNow();',
  15796. '$mod.TObject.Now();',
  15797. '$mod.vI = $mod.TObject.Now();',
  15798. '']));
  15799. end;
  15800. procedure TTestModule.TestClassOf_Const;
  15801. begin
  15802. StartProgram(false);
  15803. Add([
  15804. 'type',
  15805. ' TObject = class',
  15806. ' end;',
  15807. ' TBird = TObject;',
  15808. ' TBirds = class of TBird;',
  15809. ' TEagles = TBirds;',
  15810. ' THawk = class(TBird);',
  15811. 'const',
  15812. ' Hawk: TEagles = THawk;',
  15813. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15814. ' TBird,',
  15815. ' THawk',
  15816. ' );',
  15817. 'begin']);
  15818. ConvertProgram;
  15819. CheckSource('TestClassOf_Const',
  15820. LinesToStr([ // statements
  15821. 'rtl.createClass(this, "TObject", null, function () {',
  15822. ' this.$init = function () {',
  15823. ' };',
  15824. ' this.$final = function () {',
  15825. ' };',
  15826. '});',
  15827. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  15828. '});',
  15829. 'this.Hawk = this.THawk;',
  15830. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  15831. '']),
  15832. LinesToStr([ // $mod.$main
  15833. '']));
  15834. end;
  15835. procedure TTestModule.TestNestedClass_Alias;
  15836. begin
  15837. WithTypeInfo:=true;
  15838. StartProgram(false);
  15839. Add([
  15840. 'type',
  15841. ' TObject = class',
  15842. ' type TNested = type longint;',
  15843. ' end;',
  15844. 'type TAlias = type tobject.tnested;',
  15845. 'var i: tobject.tnested = 3;',
  15846. 'var j: TAlias = 4;',
  15847. 'begin',
  15848. ' if typeinfo(TAlias)=nil then ;',
  15849. ' if typeinfo(tobject.tnested)=nil then ;',
  15850. '']);
  15851. ConvertProgram;
  15852. CheckSource('TestNestedClass_Alias',
  15853. LinesToStr([ // statements
  15854. 'rtl.createClass(this, "TObject", null, function () {',
  15855. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15856. ' this.$init = function () {',
  15857. ' };',
  15858. ' this.$final = function () {',
  15859. ' };',
  15860. '});',
  15861. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  15862. 'this.i = 3;',
  15863. 'this.j = 4;',
  15864. '']),
  15865. LinesToStr([ // $mod.$main
  15866. 'if ($mod.$rtti["TAlias"] === null) ;',
  15867. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15868. '']));
  15869. end;
  15870. procedure TTestModule.TestNestedClass_Record;
  15871. begin
  15872. WithTypeInfo:=true;
  15873. StartProgram(false);
  15874. Add([
  15875. 'type',
  15876. ' TObject = class',
  15877. ' type TPoint = record',
  15878. ' x,y: byte;',
  15879. ' end;',
  15880. ' procedure DoIt(t: TPoint);',
  15881. ' end;',
  15882. 'procedure tobject.DoIt(t: TPoint);',
  15883. 'var p: TPoint;',
  15884. 'begin',
  15885. ' t.x:=t.y;',
  15886. ' p:=t;',
  15887. 'end;',
  15888. 'var',
  15889. ' p: tobject.tpoint = (x:2; y:4);',
  15890. ' o: TObject;',
  15891. 'begin',
  15892. ' p:=p;',
  15893. ' o.doit(p);',
  15894. '']);
  15895. ConvertProgram;
  15896. CheckSource('TestNestedClass_Record',
  15897. LinesToStr([ // statements
  15898. 'rtl.createClass(this, "TObject", null, function () {',
  15899. ' rtl.recNewT(this, "TPoint", function () {',
  15900. ' this.x = 0;',
  15901. ' this.y = 0;',
  15902. ' this.$eq = function (b) {',
  15903. ' return (this.x === b.x) && (this.y === b.y);',
  15904. ' };',
  15905. ' this.$assign = function (s) {',
  15906. ' this.x = s.x;',
  15907. ' this.y = s.y;',
  15908. ' return this;',
  15909. ' };',
  15910. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15911. ' $r.addField("x", rtl.byte);',
  15912. ' $r.addField("y", rtl.byte);',
  15913. ' });',
  15914. ' this.$init = function () {',
  15915. ' };',
  15916. ' this.$final = function () {',
  15917. ' };',
  15918. ' this.DoIt = function (t) {',
  15919. ' var p = this.TPoint.$new();',
  15920. ' t.x = t.y;',
  15921. ' p.$assign(t);',
  15922. ' };',
  15923. '});',
  15924. 'this.p = this.TObject.TPoint.$clone({',
  15925. ' x: 2,',
  15926. ' y: 4',
  15927. '});',
  15928. 'this.o = null;',
  15929. '']),
  15930. LinesToStr([ // $mod.$main
  15931. '$mod.p.$assign($mod.p);',
  15932. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15933. '']));
  15934. end;
  15935. procedure TTestModule.TestNestedClass_Class;
  15936. begin
  15937. WithTypeInfo:=true;
  15938. StartProgram(false);
  15939. Add([
  15940. 'type',
  15941. ' TObject = class end;',
  15942. ' TBird = class',
  15943. ' type TLeg = class',
  15944. ' FId: longint;',
  15945. ' constructor Create;',
  15946. ' function Create(i: longint): TLeg;',
  15947. ' end;',
  15948. ' function DoIt(b: TBird): Tleg;',
  15949. ' end;',
  15950. 'constructor tbird.tleg.create;',
  15951. 'begin',
  15952. ' FId:=3;',
  15953. 'end;',
  15954. 'function tbird.tleg.Create(i: longint): TLeg;',
  15955. 'begin',
  15956. ' Create;',
  15957. ' Result:=TLeg.Create;',
  15958. ' Result:=TBird.TLeg.Create;',
  15959. ' Result:=Create(3);',
  15960. ' FId:=i;',
  15961. 'end;',
  15962. 'function tbird.DoIt(b: tbird): tleg;',
  15963. 'begin',
  15964. ' Result.Create;',
  15965. ' Result:=TLeg.Create;',
  15966. ' Result:=TBird.TLeg.Create;',
  15967. ' Result:=Result.Create(3);',
  15968. 'end;',
  15969. 'var',
  15970. ' b: Tbird.tleg;',
  15971. 'begin',
  15972. ' b.Create;',
  15973. ' b:=TBird.TLeg.Create;',
  15974. ' b:=b.Create(3);',
  15975. '']);
  15976. ConvertProgram;
  15977. CheckSource('TestNestedClass_Class',
  15978. LinesToStr([ // statements
  15979. 'rtl.createClass(this, "TObject", null, function () {',
  15980. ' this.$init = function () {',
  15981. ' };',
  15982. ' this.$final = function () {',
  15983. ' };',
  15984. '});',
  15985. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15986. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15987. ' this.$init = function () {',
  15988. ' $mod.TObject.$init.call(this);',
  15989. ' this.FId = 0;',
  15990. ' };',
  15991. ' this.Create = function () {',
  15992. ' this.FId = 3;',
  15993. ' return this;',
  15994. ' };',
  15995. ' this.Create$1 = function (i) {',
  15996. ' var Result = null;',
  15997. ' this.Create();',
  15998. ' Result = $mod.TBird.TLeg.$create("Create");',
  15999. ' Result = $mod.TBird.TLeg.$create("Create");',
  16000. ' Result = this.Create$1(3);',
  16001. ' this.FId = i;',
  16002. ' return Result;',
  16003. ' };',
  16004. ' }, "TBird.TLeg");',
  16005. ' this.DoIt = function (b) {',
  16006. ' var Result = null;',
  16007. ' Result.Create();',
  16008. ' Result = this.TLeg.$create("Create");',
  16009. ' Result = $mod.TBird.TLeg.$create("Create");',
  16010. ' Result = Result.Create$1(3);',
  16011. ' return Result;',
  16012. ' };',
  16013. '});',
  16014. 'this.b = null;',
  16015. '']),
  16016. LinesToStr([ // $mod.$main
  16017. '$mod.b.Create();',
  16018. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16019. '$mod.b = $mod.b.Create$1(3);',
  16020. '']));
  16021. end;
  16022. procedure TTestModule.TestExternalClass_Var;
  16023. begin
  16024. StartProgram(false);
  16025. Add([
  16026. '{$modeswitch externalclass}',
  16027. 'type',
  16028. ' TExtA = class external name ''ExtObj''',
  16029. ' Id: longint external name ''$Id'';',
  16030. ' B: longint;',
  16031. ' end;',
  16032. 'var Obj: TExtA;',
  16033. 'begin',
  16034. ' obj.id:=obj.id+1;',
  16035. ' obj.B:=obj.B+1;']);
  16036. ConvertProgram;
  16037. CheckSource('TestExternalClass_Var',
  16038. LinesToStr([ // statements
  16039. 'this.Obj = null;',
  16040. '']),
  16041. LinesToStr([ // $mod.$main
  16042. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16043. '$mod.Obj.B = $mod.Obj.B + 1;',
  16044. '']));
  16045. end;
  16046. procedure TTestModule.TestExternalClass_Const;
  16047. begin
  16048. StartProgram(false);
  16049. Add([
  16050. '{$modeswitch externalclass}',
  16051. 'type',
  16052. ' TExtA = class external name ''ExtObj''',
  16053. ' const Two: longint = 2;',
  16054. ' const Three = 3;',
  16055. ' const Id: longint;',
  16056. ' end;',
  16057. ' TExtB = class external name ''ExtB''',
  16058. ' A: TExtA;',
  16059. ' end;',
  16060. 'var',
  16061. ' A: texta;',
  16062. ' B: textb;',
  16063. ' i: longint;',
  16064. 'begin',
  16065. ' i:=a.two;',
  16066. ' i:=texta.two;',
  16067. ' i:=a.three;',
  16068. ' i:=texta.three;',
  16069. ' i:=a.id;',
  16070. ' i:=texta.id;',
  16071. '']);
  16072. ConvertProgram;
  16073. CheckSource('TestExternalClass_Const',
  16074. LinesToStr([ // statements
  16075. 'this.A = null;',
  16076. 'this.B = null;',
  16077. 'this.i = 0;',
  16078. '']),
  16079. LinesToStr([ // $mod.$main
  16080. '$mod.i = 2;',
  16081. '$mod.i = 2;',
  16082. '$mod.i = 3;',
  16083. '$mod.i = 3;',
  16084. '$mod.i = $mod.A.Id;',
  16085. '$mod.i = ExtObj.Id;',
  16086. '']));
  16087. end;
  16088. procedure TTestModule.TestExternalClass_Dollar;
  16089. begin
  16090. StartProgram(false);
  16091. Add([
  16092. '{$modeswitch externalclass}',
  16093. 'type',
  16094. ' TExtA = class external name ''$''',
  16095. ' Id: longint external name ''$'';',
  16096. ' function Bla(i: longint): longint; external name ''$'';',
  16097. ' end;',
  16098. 'function dollar(k: longint): longint; external name ''$'';',
  16099. 'var Obj: TExtA;',
  16100. 'begin',
  16101. ' dollar(1);',
  16102. ' obj.id:=obj.id+2;',
  16103. ' obj.Bla(3);',
  16104. '']);
  16105. ConvertProgram;
  16106. CheckSource('TestExternalClass_Dollar',
  16107. LinesToStr([ // statements
  16108. 'this.Obj = null;',
  16109. '']),
  16110. LinesToStr([ // $mod.$main
  16111. '$(1);',
  16112. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16113. '$mod.Obj.$(3);',
  16114. '']));
  16115. end;
  16116. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16117. begin
  16118. StartProgram(false);
  16119. Add('{$modeswitch externalclass}');
  16120. Add('type');
  16121. Add(' TExtA = class external name ''ExtA''');
  16122. Add(' Id: longint external name ''$Id'';');
  16123. Add(' end;');
  16124. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16125. Add(' Id: longint;');
  16126. Add(' end;');
  16127. Add('begin');
  16128. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16129. ConvertProgram;
  16130. end;
  16131. procedure TTestModule.TestExternalClass_Method;
  16132. begin
  16133. StartProgram(false);
  16134. Add(['{$modeswitch externalclass}',
  16135. 'type',
  16136. ' TExtA = class external name ''ExtObj''',
  16137. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16138. ' procedure DoSome(Id: longint = 1);',
  16139. ' end;',
  16140. 'var Obj: texta;',
  16141. 'begin',
  16142. ' obj.doit;',
  16143. ' obj.doit();',
  16144. ' obj.doit(2);',
  16145. ' with obj do begin',
  16146. ' doit;',
  16147. ' doit();',
  16148. ' doit(3);',
  16149. ' end;']);
  16150. ConvertProgram;
  16151. CheckSource('TestExternalClass_Method',
  16152. LinesToStr([ // statements
  16153. 'this.Obj = null;',
  16154. '']),
  16155. LinesToStr([ // $mod.$main
  16156. '$mod.Obj.$Execute(1);',
  16157. '$mod.Obj.$Execute(1);',
  16158. '$mod.Obj.$Execute(2);',
  16159. 'var $with = $mod.Obj;',
  16160. '$with.$Execute(1);',
  16161. '$with.$Execute(1);',
  16162. '$with.$Execute(3);',
  16163. '']));
  16164. end;
  16165. procedure TTestModule.TestExternalClass_ClassMethod;
  16166. begin
  16167. StartProgram(false);
  16168. Add([
  16169. '{$modeswitch externalclass}',
  16170. 'type',
  16171. ' TExtA = class external name ''ExtObj''',
  16172. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16173. ' end;',
  16174. ' TExtB = TExtA;',
  16175. 'var p: Pointer;',
  16176. 'begin',
  16177. ' texta.doit;',
  16178. ' texta.doit();',
  16179. ' texta.doit(2);',
  16180. ' p:[email protected];',
  16181. ' with texta do begin',
  16182. ' doit;',
  16183. ' doit();',
  16184. ' doit(3);',
  16185. ' p:=@DoIt;',
  16186. ' end;',
  16187. ' textb.doit;',
  16188. ' textb.doit();',
  16189. ' textb.doit(4);',
  16190. ' with textb do begin',
  16191. ' doit;',
  16192. ' doit();',
  16193. ' doit(5);',
  16194. ' end;',
  16195. '']);
  16196. ConvertProgram;
  16197. CheckSource('TestExternalClass_ClassMethod',
  16198. LinesToStr([ // statements
  16199. 'this.p = null;',
  16200. '']),
  16201. LinesToStr([ // $mod.$main
  16202. 'ExtObj.$Execute(1);',
  16203. 'ExtObj.$Execute(1);',
  16204. 'ExtObj.$Execute(2);',
  16205. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16206. 'ExtObj.$Execute(1);',
  16207. 'ExtObj.$Execute(1);',
  16208. 'ExtObj.$Execute(3);',
  16209. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16210. 'ExtObj.$Execute(1);',
  16211. 'ExtObj.$Execute(1);',
  16212. 'ExtObj.$Execute(4);',
  16213. 'ExtObj.$Execute(1);',
  16214. 'ExtObj.$Execute(1);',
  16215. 'ExtObj.$Execute(5);',
  16216. '']));
  16217. end;
  16218. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16219. begin
  16220. StartProgram(false);
  16221. Add([
  16222. '{$modeswitch externalclass}',
  16223. 'type',
  16224. ' TExtA = class external name ''ExtObj''',
  16225. ' class procedure DoIt(Id: longint = 1); static;',
  16226. ' end;',
  16227. 'var p: Pointer;',
  16228. 'begin',
  16229. ' texta.doit;',
  16230. ' texta.doit();',
  16231. ' texta.doit(2);',
  16232. ' p:[email protected];',
  16233. ' with texta do begin',
  16234. ' doit;',
  16235. ' doit();',
  16236. ' doit(3);',
  16237. ' p:=@DoIt;',
  16238. ' end;',
  16239. '']);
  16240. ConvertProgram;
  16241. CheckSource('TestExternalClass_ClassMethodStatic',
  16242. LinesToStr([ // statements
  16243. 'this.p = null;',
  16244. '']),
  16245. LinesToStr([ // $mod.$main
  16246. 'ExtObj.DoIt(1);',
  16247. 'ExtObj.DoIt(1);',
  16248. 'ExtObj.DoIt(2);',
  16249. '$mod.p = ExtObj.DoIt;',
  16250. 'ExtObj.DoIt(1);',
  16251. 'ExtObj.DoIt(1);',
  16252. 'ExtObj.DoIt(3);',
  16253. '$mod.p = ExtObj.DoIt;',
  16254. '']));
  16255. end;
  16256. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16257. begin
  16258. StartProgram(false);
  16259. Add([
  16260. '{$modeswitch externalclass}',
  16261. 'type',
  16262. ' TBird = class external name ''Array''',
  16263. ' end;',
  16264. 'function GetPtr: Pointer;',
  16265. 'begin',
  16266. 'end;',
  16267. 'procedure Write(const p);',
  16268. 'begin',
  16269. 'end;',
  16270. 'procedure WriteLn; varargs;',
  16271. 'begin',
  16272. 'end;',
  16273. 'begin',
  16274. ' if TBird(GetPtr)=nil then ;',
  16275. ' Write(GetPtr);',
  16276. ' WriteLn(GetPtr);',
  16277. ' Write(TBird(GetPtr));',
  16278. ' WriteLn(TBird(GetPtr));',
  16279. '']);
  16280. ConvertProgram;
  16281. CheckSource('TestFunctionResultInTypeCast',
  16282. LinesToStr([ // statements
  16283. 'this.GetPtr = function () {',
  16284. ' var Result = null;',
  16285. ' return Result;',
  16286. '};',
  16287. 'this.Write = function (p) {',
  16288. '};',
  16289. 'this.WriteLn = function () {',
  16290. '};',
  16291. '']),
  16292. LinesToStr([
  16293. 'if ($mod.GetPtr() === null) ;',
  16294. '$mod.Write($mod.GetPtr());',
  16295. '$mod.WriteLn($mod.GetPtr());',
  16296. '$mod.Write($mod.GetPtr());',
  16297. '$mod.WriteLn($mod.GetPtr());',
  16298. '']));
  16299. end;
  16300. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16301. begin
  16302. StartProgram(false);
  16303. Add([
  16304. '{$modeswitch externalclass}',
  16305. 'type',
  16306. ' TExtA = class external name ''ExtObjA''',
  16307. ' procedure ProcA; virtual;',
  16308. ' procedure ProcB; virtual;',
  16309. ' end;',
  16310. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16311. ' end;',
  16312. ' TExtC = class (TExtB)',
  16313. ' procedure ProcA; override;',
  16314. ' end;',
  16315. 'procedure TExtC.ProcA;',
  16316. 'begin',
  16317. ' ProcA;',
  16318. ' Self.ProcA;',
  16319. ' ProcB;',
  16320. ' Self.ProcB;',
  16321. 'end;',
  16322. 'var',
  16323. ' A: texta;',
  16324. ' B: textb;',
  16325. ' C: textc;',
  16326. 'begin',
  16327. ' a.proca;',
  16328. ' b.proca;',
  16329. ' c.proca;']);
  16330. ConvertProgram;
  16331. CheckSource('TestExternalClass_NonExternalOverride',
  16332. LinesToStr([ // statements
  16333. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16334. ' this.$init = function () {',
  16335. ' };',
  16336. ' this.$final = function () {',
  16337. ' };',
  16338. ' this.ProcA = function () {',
  16339. ' this.ProcA();',
  16340. ' this.ProcA();',
  16341. ' this.ProcB();',
  16342. ' this.ProcB();',
  16343. ' };',
  16344. '});',
  16345. 'this.A = null;',
  16346. 'this.B = null;',
  16347. 'this.C = null;',
  16348. '']),
  16349. LinesToStr([ // $mod.$main
  16350. '$mod.A.ProcA();',
  16351. '$mod.B.ProcA();',
  16352. '$mod.C.ProcA();',
  16353. '']));
  16354. end;
  16355. procedure TTestModule.TestExternalClass_OverloadHint;
  16356. begin
  16357. StartProgram(false);
  16358. Add([
  16359. '{$modeswitch externalclass}',
  16360. 'type',
  16361. ' TExtA = class external name ''ExtObjA''',
  16362. ' procedure DoIt;',
  16363. ' procedure DoIt(i: longint);',
  16364. ' end;',
  16365. 'begin',
  16366. '']);
  16367. ConvertProgram;
  16368. CheckResolverUnexpectedHints(true);
  16369. CheckSource('TestExternalClass_OverloadHint',
  16370. LinesToStr([ // statements
  16371. '']),
  16372. LinesToStr([ // $mod.$main
  16373. '']));
  16374. end;
  16375. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16376. begin
  16377. StartProgram(false);
  16378. Add([
  16379. '{$modeswitch externalclass}',
  16380. 'type',
  16381. ' JSwiper = class external name ''Swiper''',
  16382. ' constructor New;',
  16383. ' end;',
  16384. ' TObject = class',
  16385. ' private',
  16386. ' FSwiper: JSwiper;',
  16387. ' published',
  16388. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16389. ' end;',
  16390. 'begin',
  16391. ' JSwiper.new;',
  16392. '']);
  16393. ConvertProgram;
  16394. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16395. LinesToStr([ // statements
  16396. 'rtl.createClass(this, "TObject", null, function () {',
  16397. ' this.$init = function () {',
  16398. ' this.FSwiper = null;',
  16399. ' };',
  16400. ' this.$final = function () {',
  16401. ' this.FSwiper = undefined;',
  16402. ' };',
  16403. ' var $r = this.$rtti;',
  16404. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16405. '});',
  16406. '']),
  16407. LinesToStr([ // $mod.$main
  16408. 'new Swiper();',
  16409. '']));
  16410. end;
  16411. procedure TTestModule.TestExternalClass_Property;
  16412. begin
  16413. StartProgram(false);
  16414. Add([
  16415. '{$modeswitch externalclass}',
  16416. 'type',
  16417. ' TExtA = class external name ''ExtA''',
  16418. ' function getYear: longint;',
  16419. ' procedure setYear(Value: longint);',
  16420. ' property Year: longint read getyear write setyear;',
  16421. ' end;',
  16422. ' TExtB = class (TExtA)',
  16423. ' procedure OtherSetYear(Value: longint);',
  16424. ' property year write othersetyear;',
  16425. ' end;',
  16426. 'procedure textb.othersetyear(value: longint);',
  16427. 'begin',
  16428. ' setYear(Value+4);',
  16429. 'end;',
  16430. 'var',
  16431. ' A: texta;',
  16432. ' B: textb;',
  16433. 'begin',
  16434. ' a.year:=a.year+1;',
  16435. ' b.year:=b.year+2;']);
  16436. ConvertProgram;
  16437. CheckSource('TestExternalClass_NonExternalOverride',
  16438. LinesToStr([ // statements
  16439. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16440. ' this.$init = function () {',
  16441. ' };',
  16442. ' this.$final = function () {',
  16443. ' };',
  16444. ' this.OtherSetYear = function (Value) {',
  16445. ' this.setYear(Value+4);',
  16446. ' };',
  16447. '});',
  16448. 'this.A = null;',
  16449. 'this.B = null;',
  16450. '']),
  16451. LinesToStr([ // $mod.$main
  16452. '$mod.A.setYear($mod.A.getYear()+1);',
  16453. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16454. '']));
  16455. end;
  16456. procedure TTestModule.TestExternalClass_PropertyDate;
  16457. begin
  16458. StartProgram(false);
  16459. Add([
  16460. '{$modeswitch externalclass}',
  16461. 'type',
  16462. ' TExtA = class external name ''ExtA''',
  16463. ' end;',
  16464. ' TExtB = class (TExtA)',
  16465. ' FDate: string;',
  16466. ' property Date: string read FDate write FDate;',
  16467. ' property ExtA: string read FDate write FDate;',
  16468. ' end;',
  16469. ' {$M+}',
  16470. ' TObject = class',
  16471. ' FDate: string;',
  16472. ' published',
  16473. ' property Date: string read FDate write FDate;',
  16474. ' property ExtA: string read FDate write FDate;',
  16475. ' end;',
  16476. 'var',
  16477. ' B: textb;',
  16478. ' o: TObject;',
  16479. 'begin',
  16480. ' b.date:=b.exta;',
  16481. ' o.date:=o.exta;']);
  16482. ConvertProgram;
  16483. CheckSource('TestExternalClass_PropertyDate',
  16484. LinesToStr([ // statements
  16485. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16486. ' this.$init = function () {',
  16487. ' this.FDate = "";',
  16488. ' };',
  16489. ' this.$final = function () {',
  16490. ' };',
  16491. '});',
  16492. 'rtl.createClass(this, "TObject", null, function () {',
  16493. ' this.$init = function () {',
  16494. ' this.FDate = "";',
  16495. ' };',
  16496. ' this.$final = function () {',
  16497. ' };',
  16498. ' var $r = this.$rtti;',
  16499. ' $r.addField("FDate", rtl.string);',
  16500. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16501. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16502. '});',
  16503. 'this.B = null;',
  16504. 'this.o = null;',
  16505. '']),
  16506. LinesToStr([ // $mod.$main
  16507. '$mod.B.FDate = $mod.B.FDate;',
  16508. '$mod.o.FDate = $mod.o.FDate;',
  16509. '']));
  16510. end;
  16511. procedure TTestModule.TestExternalClass_ClassProperty;
  16512. begin
  16513. StartProgram(false);
  16514. Add('{$modeswitch externalclass}');
  16515. Add('type');
  16516. Add(' TExtA = class external name ''ExtA''');
  16517. Add(' class function getYear: longint;');
  16518. Add(' class procedure setYear(Value: longint);');
  16519. Add(' class property Year: longint read getyear write setyear;');
  16520. Add(' end;');
  16521. Add(' TExtB = class (TExtA)');
  16522. Add(' class function GetCentury: longint;');
  16523. Add(' class procedure SetCentury(Value: longint);');
  16524. Add(' class property Century: longint read getcentury write setcentury;');
  16525. Add(' end;');
  16526. Add('class function textb.getcentury: longint;');
  16527. Add('begin');
  16528. Add('end;');
  16529. Add('class procedure textb.setcentury(value: longint);');
  16530. Add('begin');
  16531. Add(' setyear(value+11);');
  16532. Add(' texta.year:=texta.year+12;');
  16533. Add(' year:=year+13;');
  16534. Add(' textb.century:=textb.century+14;');
  16535. Add(' century:=century+15;');
  16536. Add('end;');
  16537. Add('var');
  16538. Add(' A: texta;');
  16539. Add(' B: textb;');
  16540. Add('begin');
  16541. Add(' texta.year:=texta.year+1;');
  16542. Add(' textb.year:=textb.year+2;');
  16543. Add(' TextA.year:=TextA.year+3;');
  16544. Add(' b.year:=b.year+4;');
  16545. Add(' textb.century:=textb.century+5;');
  16546. Add(' b.century:=b.century+6;');
  16547. ConvertProgram;
  16548. CheckSource('TestExternalClass_ClassProperty',
  16549. LinesToStr([ // statements
  16550. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16551. ' this.$init = function () {',
  16552. ' };',
  16553. ' this.$final = function () {',
  16554. ' };',
  16555. ' this.GetCentury = function () {',
  16556. ' var Result = 0;',
  16557. ' return Result;',
  16558. ' };',
  16559. ' this.SetCentury = function (Value) {',
  16560. ' this.setYear(Value + 11);',
  16561. ' ExtA.setYear(ExtA.getYear() + 12);',
  16562. ' this.setYear(this.getYear() + 13);',
  16563. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16564. ' this.SetCentury(this.GetCentury() + 15);',
  16565. ' };',
  16566. '});',
  16567. 'this.A = null;',
  16568. 'this.B = null;',
  16569. '']),
  16570. LinesToStr([ // $mod.$main
  16571. 'ExtA.setYear(ExtA.getYear() + 1);',
  16572. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16573. 'ExtA.setYear(ExtA.getYear() + 3);',
  16574. '$mod.B.setYear($mod.B.getYear() + 4);',
  16575. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16576. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16577. '']));
  16578. end;
  16579. procedure TTestModule.TestExternalClass_ClassOf;
  16580. begin
  16581. StartProgram(false);
  16582. Add('{$modeswitch externalclass}');
  16583. Add('type');
  16584. Add(' TExtA = class external name ''ExtA''');
  16585. Add(' procedure ProcA; virtual;');
  16586. Add(' procedure ProcB; virtual;');
  16587. Add(' end;');
  16588. Add(' TExtAClass = class of TExtA;');
  16589. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16590. Add(' end;');
  16591. Add(' TExtBClass = class of TExtB;');
  16592. Add(' TExtC = class (TExtB)');
  16593. Add(' procedure ProcA; override;');
  16594. Add(' end;');
  16595. Add(' TExtCClass = class of TExtC;');
  16596. Add('procedure TExtC.ProcA; begin end;');
  16597. Add('var');
  16598. Add(' A: texta; ClA: TExtAClass;');
  16599. Add(' B: textb; ClB: TExtBClass;');
  16600. Add(' C: textc; ClC: TExtCClass;');
  16601. Add('begin');
  16602. Add(' ClA:=texta;');
  16603. Add(' ClA:=textb;');
  16604. Add(' ClA:=textc;');
  16605. Add(' ClB:=textb;');
  16606. Add(' ClB:=textc;');
  16607. Add(' ClC:=textc;');
  16608. ConvertProgram;
  16609. CheckSource('TestExternalClass_ClassOf',
  16610. LinesToStr([ // statements
  16611. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16612. ' this.$init = function () {',
  16613. ' };',
  16614. ' this.$final = function () {',
  16615. ' };',
  16616. ' this.ProcA = function () {',
  16617. ' };',
  16618. '});',
  16619. 'this.A = null;',
  16620. 'this.ClA = null;',
  16621. 'this.B = null;',
  16622. 'this.ClB = null;',
  16623. 'this.C = null;',
  16624. 'this.ClC = null;',
  16625. '']),
  16626. LinesToStr([ // $mod.$main
  16627. '$mod.ClA = ExtA;',
  16628. '$mod.ClA = ExtB;',
  16629. '$mod.ClA = $mod.TExtC;',
  16630. '$mod.ClB = ExtB;',
  16631. '$mod.ClB = $mod.TExtC;',
  16632. '$mod.ClC = $mod.TExtC;',
  16633. '']));
  16634. end;
  16635. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16636. begin
  16637. AddModuleWithIntfImplSrc('unit2.pas',
  16638. LinesToStr([
  16639. '{$modeswitch externalclass}',
  16640. 'type',
  16641. ' TExtA = class external name ''ExtA''',
  16642. ' class var Id: longint;',
  16643. ' end;',
  16644. '']),
  16645. '');
  16646. StartUnit(true);
  16647. Add('interface');
  16648. Add('uses unit2;');
  16649. Add('implementation');
  16650. Add('begin');
  16651. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16652. ConvertUnit;
  16653. CheckSource('TestExternalClass_ClassOtherUnit',
  16654. LinesToStr([
  16655. '']),
  16656. LinesToStr([
  16657. 'ExtA.Id = ExtA.Id + 1;',
  16658. '']));
  16659. end;
  16660. procedure TTestModule.TestExternalClass_Is;
  16661. begin
  16662. StartProgram(false);
  16663. Add([
  16664. '{$modeswitch externalclass}',
  16665. 'type',
  16666. ' TExtA = class external name ''ExtA''',
  16667. ' end;',
  16668. ' TExtAClass = class of TExtA;',
  16669. ' TExtB = class external name ''ExtB'' (TExtA)',
  16670. ' end;',
  16671. ' TExtBClass = class of TExtB;',
  16672. ' TExtC = class (TExtB)',
  16673. ' end;',
  16674. ' TExtCClass = class of TExtC;',
  16675. 'var',
  16676. ' A: texta; ClA: TExtAClass;',
  16677. ' B: textb; ClB: TExtBClass;',
  16678. ' C: textc; ClC: TExtCClass;',
  16679. 'begin',
  16680. ' if a is textb then ;',
  16681. ' if a is textc then ;',
  16682. ' if b is textc then ;',
  16683. ' if cla is textb then ;',
  16684. ' if cla is textc then ;',
  16685. ' if clb is textc then ;',
  16686. ' try',
  16687. ' except',
  16688. ' on TExtA do ;',
  16689. ' on e: TExtB do ;',
  16690. ' end;',
  16691. '']);
  16692. ConvertProgram;
  16693. CheckSource('TestExternalClass_Is',
  16694. LinesToStr([ // statements
  16695. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16696. ' this.$init = function () {',
  16697. ' };',
  16698. ' this.$final = function () {',
  16699. ' };',
  16700. '});',
  16701. 'this.A = null;',
  16702. 'this.ClA = null;',
  16703. 'this.B = null;',
  16704. 'this.ClB = null;',
  16705. 'this.C = null;',
  16706. 'this.ClC = null;',
  16707. '']),
  16708. LinesToStr([ // $mod.$main
  16709. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16710. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16711. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16712. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16713. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16714. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16715. 'try {} catch ($e) {',
  16716. ' if (rtl.isExt($e,ExtA)) {}',
  16717. ' else if (rtl.isExt($e,ExtB)) {',
  16718. ' var e = $e;',
  16719. ' } else throw $e',
  16720. '};',
  16721. '']));
  16722. end;
  16723. procedure TTestModule.TestExternalClass_As;
  16724. begin
  16725. StartProgram(false);
  16726. Add('{$modeswitch externalclass}');
  16727. Add('type');
  16728. Add(' TExtA = class external name ''ExtA''');
  16729. Add(' end;');
  16730. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16731. Add(' end;');
  16732. Add(' TExtC = class (TExtB)');
  16733. Add(' end;');
  16734. Add('var');
  16735. Add(' A: texta;');
  16736. Add(' B: textb;');
  16737. Add(' C: textc;');
  16738. Add('begin');
  16739. Add(' b:=a as textb;');
  16740. Add(' c:=a as textc;');
  16741. Add(' c:=b as textc;');
  16742. ConvertProgram;
  16743. CheckSource('TestExternalClass_Is',
  16744. LinesToStr([ // statements
  16745. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16746. ' this.$init = function () {',
  16747. ' };',
  16748. ' this.$final = function () {',
  16749. ' };',
  16750. '});',
  16751. 'this.A = null;',
  16752. 'this.B = null;',
  16753. 'this.C = null;',
  16754. '']),
  16755. LinesToStr([ // $mod.$main
  16756. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16757. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16758. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16759. '']));
  16760. end;
  16761. procedure TTestModule.TestExternalClass_DestructorFail;
  16762. begin
  16763. StartProgram(false);
  16764. Add('{$modeswitch externalclass}');
  16765. Add('type');
  16766. Add(' TExtA = class external name ''ExtA''');
  16767. Add(' destructor Free;');
  16768. Add(' end;');
  16769. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16770. nPasElementNotSupported);
  16771. ConvertProgram;
  16772. end;
  16773. procedure TTestModule.TestExternalClass_New;
  16774. begin
  16775. StartProgram(false);
  16776. Add([
  16777. '{$modeswitch externalclass}',
  16778. 'type',
  16779. ' TExtA = class external name ''ExtA''',
  16780. ' constructor New;',
  16781. ' constructor New(i: longint; j: longint = 2);',
  16782. ' end;',
  16783. 'var',
  16784. ' A: texta;',
  16785. 'begin',
  16786. ' a:=texta.new;',
  16787. ' a:=texta(texta.new);',
  16788. ' a:=texta.new();',
  16789. ' a:=texta.new(1);',
  16790. ' with texta do begin',
  16791. ' a:=new;',
  16792. ' a:=new();',
  16793. ' a:=new(2);',
  16794. ' end;',
  16795. ' a:=test1.texta.new;',
  16796. ' a:=test1.texta.new();',
  16797. ' a:=test1.texta.new(3);',
  16798. '']);
  16799. ConvertProgram;
  16800. CheckSource('TestExternalClass_New',
  16801. LinesToStr([ // statements
  16802. 'this.A = null;',
  16803. '']),
  16804. LinesToStr([ // $mod.$main
  16805. '$mod.A = new ExtA();',
  16806. '$mod.A = new ExtA();',
  16807. '$mod.A = new ExtA();',
  16808. '$mod.A = new ExtA(1,2);',
  16809. '$mod.A = new ExtA();',
  16810. '$mod.A = new ExtA();',
  16811. '$mod.A = new ExtA(2,2);',
  16812. '$mod.A = new ExtA();',
  16813. '$mod.A = new ExtA();',
  16814. '$mod.A = new ExtA(3,2);',
  16815. '']));
  16816. end;
  16817. procedure TTestModule.TestExternalClass_ClassOf_New;
  16818. begin
  16819. StartProgram(false);
  16820. Add('{$modeswitch externalclass}');
  16821. Add('type');
  16822. Add(' TExtAClass = class of TExtA;');
  16823. Add(' TExtA = class external name ''ExtA''');
  16824. Add(' C: TExtAClass;');
  16825. Add(' constructor New;');
  16826. Add(' end;');
  16827. Add('var');
  16828. Add(' A: texta;');
  16829. Add(' C: textaclass;');
  16830. Add('begin');
  16831. Add(' a:=c.new;');
  16832. Add(' a:=c.new();');
  16833. Add(' with C do begin');
  16834. Add(' a:=new;');
  16835. Add(' a:=new();');
  16836. Add(' end;');
  16837. Add(' a:=test1.c.new;');
  16838. Add(' a:=test1.c.new();');
  16839. Add(' a:=A.c.new();');
  16840. ConvertProgram;
  16841. CheckSource('TestExternalClass_ClassOf_New',
  16842. LinesToStr([ // statements
  16843. 'this.A = null;',
  16844. 'this.C = null;',
  16845. '']),
  16846. LinesToStr([ // $mod.$main
  16847. '$mod.A = new $mod.C();',
  16848. '$mod.A = new $mod.C();',
  16849. 'var $with = $mod.C;',
  16850. '$mod.A = new $with();',
  16851. '$mod.A = new $with();',
  16852. '$mod.A = new $mod.C();',
  16853. '$mod.A = new $mod.C();',
  16854. '$mod.A = new $mod.A.C();',
  16855. '']));
  16856. end;
  16857. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16858. begin
  16859. StartProgram(false);
  16860. Add([
  16861. '{$modeswitch externalclass}',
  16862. 'type',
  16863. ' TExtAClass = class of TExtA;',
  16864. ' TExtA = class external name ''ExtA''',
  16865. ' constructor New;',
  16866. ' end;',
  16867. 'function GetCreator: TExtAClass;',
  16868. 'begin',
  16869. ' Result:=TExtA;',
  16870. 'end;',
  16871. 'var',
  16872. ' A: texta;',
  16873. 'begin',
  16874. ' a:=getcreator.new;',
  16875. ' a:=getcreator().new;',
  16876. ' a:=getcreator().new();',
  16877. ' a:=getcreator.new();',
  16878. ' with getcreator do begin',
  16879. ' a:=new;',
  16880. ' a:=new();',
  16881. ' end;']);
  16882. ConvertProgram;
  16883. CheckSource('TestExternalClass_FuncClassOf_New',
  16884. LinesToStr([ // statements
  16885. 'this.GetCreator = function () {',
  16886. ' var Result = null;',
  16887. ' Result = ExtA;',
  16888. ' return Result;',
  16889. '};',
  16890. 'this.A = null;',
  16891. '']),
  16892. LinesToStr([ // $mod.$main
  16893. '$mod.A = new ($mod.GetCreator())();',
  16894. '$mod.A = new ($mod.GetCreator())();',
  16895. '$mod.A = new ($mod.GetCreator())();',
  16896. '$mod.A = new ($mod.GetCreator())();',
  16897. 'var $with = $mod.GetCreator();',
  16898. '$mod.A = new $with();',
  16899. '$mod.A = new $with();',
  16900. '']));
  16901. end;
  16902. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16903. begin
  16904. StartProgram(false);
  16905. Add([
  16906. '{$modeswitch externalclass}',
  16907. 'type',
  16908. ' TExtA = class external name ''ExtA''',
  16909. ' constructor New;',
  16910. ' end;',
  16911. ' TBird = class(TExtA)',
  16912. ' end;',
  16913. 'begin',
  16914. ' TBird.new;',
  16915. '']);
  16916. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16917. ConvertProgram;
  16918. end;
  16919. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16920. begin
  16921. StartProgram(false);
  16922. Add([
  16923. '{$modeswitch externalclass}',
  16924. 'type',
  16925. ' TExtA = class external name ''ExtA''',
  16926. ' constructor New;',
  16927. ' end;',
  16928. ' TBird = class(TExtA)',
  16929. ' end;',
  16930. 'begin',
  16931. ' TBird.new();',
  16932. '']);
  16933. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16934. ConvertProgram;
  16935. end;
  16936. procedure TTestModule.TestExternalClass_NewExtName;
  16937. begin
  16938. StartProgram(false);
  16939. Add([
  16940. '{$modeswitch externalclass}',
  16941. 'type',
  16942. ' TExtA = class external name ''ExtA''',
  16943. ' constructor New; external name ''Other'';',
  16944. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16945. ' end;',
  16946. 'var',
  16947. ' A: texta;',
  16948. 'begin',
  16949. ' a:=texta.new;',
  16950. ' a:=texta(texta.new);',
  16951. ' a:=texta.new();',
  16952. ' a:=texta.new(1);',
  16953. ' with texta do begin',
  16954. ' a:=new;',
  16955. ' a:=new();',
  16956. ' a:=new(2);',
  16957. ' end;',
  16958. ' a:=test1.texta.new;',
  16959. ' a:=test1.texta.new();',
  16960. ' a:=test1.texta.new(3);',
  16961. '']);
  16962. ConvertProgram;
  16963. CheckSource('TestExternalClass_NewExtName',
  16964. LinesToStr([ // statements
  16965. 'this.A = null;',
  16966. '']),
  16967. LinesToStr([ // $mod.$main
  16968. '$mod.A = new Other();',
  16969. '$mod.A = new Other();',
  16970. '$mod.A = new Other();',
  16971. '$mod.A = new A.B(1,2);',
  16972. '$mod.A = new Other();',
  16973. '$mod.A = new Other();',
  16974. '$mod.A = new A.B(2,2);',
  16975. '$mod.A = new Other();',
  16976. '$mod.A = new Other();',
  16977. '$mod.A = new A.B(3,2);',
  16978. '']));
  16979. end;
  16980. procedure TTestModule.TestExternalClass_Constructor;
  16981. begin
  16982. StartProgram(false);
  16983. Add([
  16984. '{$modeswitch externalclass}',
  16985. 'type',
  16986. ' TExtA = class external name ''ExtA''',
  16987. ' constructor Create;',
  16988. ' constructor Create(i: longint; j: longint = 2);',
  16989. ' end;',
  16990. 'var',
  16991. ' A: texta;',
  16992. 'begin',
  16993. ' a:=texta.create;',
  16994. ' a:=texta(texta.create);',
  16995. ' a:=texta.create();',
  16996. ' a:=texta.create(1);',
  16997. ' with texta do begin',
  16998. ' a:=create;',
  16999. ' a:=create();',
  17000. ' a:=create(2);',
  17001. ' end;',
  17002. ' a:=test1.texta.create;',
  17003. ' a:=test1.texta.create();',
  17004. ' a:=test1.texta.create(3);',
  17005. '']);
  17006. ConvertProgram;
  17007. CheckSource('TestExternalClass_Constructor',
  17008. LinesToStr([ // statements
  17009. 'this.A = null;',
  17010. '']),
  17011. LinesToStr([ // $mod.$main
  17012. '$mod.A = new ExtA.Create();',
  17013. '$mod.A = new ExtA.Create();',
  17014. '$mod.A = new ExtA.Create();',
  17015. '$mod.A = new ExtA.Create(1,2);',
  17016. '$mod.A = new ExtA.Create();',
  17017. '$mod.A = new ExtA.Create();',
  17018. '$mod.A = new ExtA.Create(2,2);',
  17019. '$mod.A = new ExtA.Create();',
  17020. '$mod.A = new ExtA.Create();',
  17021. '$mod.A = new ExtA.Create(3,2);',
  17022. '']));
  17023. end;
  17024. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17025. begin
  17026. StartProgram(false);
  17027. Add([
  17028. '{$modeswitch externalclass}',
  17029. 'type',
  17030. ' TExtA = class external name ''ExtA''',
  17031. ' constructor Create; external name ''{}'';',
  17032. ' end;',
  17033. 'var',
  17034. ' A: texta;',
  17035. 'begin',
  17036. ' a:=texta.create;',
  17037. ' a:=texta(texta.create);',
  17038. ' a:=texta.create();',
  17039. ' with texta do begin',
  17040. ' a:=create;',
  17041. ' a:=create();',
  17042. ' end;',
  17043. ' a:=test1.texta.create;',
  17044. ' a:=test1.texta.create();',
  17045. '']);
  17046. ConvertProgram;
  17047. CheckSource('TestExternalClass_ConstructorBrackets',
  17048. LinesToStr([ // statements
  17049. 'this.A = null;',
  17050. '']),
  17051. LinesToStr([ // $mod.$main
  17052. '$mod.A = {};',
  17053. '$mod.A = {};',
  17054. '$mod.A = {};',
  17055. '$mod.A = {};',
  17056. '$mod.A = {};',
  17057. '$mod.A = {};',
  17058. '$mod.A = {};',
  17059. '']));
  17060. end;
  17061. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17062. begin
  17063. StartProgram(false);
  17064. Add('{$modeswitch externalclass}');
  17065. Add('type');
  17066. Add(' TExtA = class external name ''ExtA''');
  17067. Add(' constructor New;');
  17068. Add(' end;');
  17069. Add('function DoIt: longint;');
  17070. Add('const ExtA: longint = 3;');
  17071. Add('begin');
  17072. Add(' Result:=ExtA;');
  17073. Add('end;');
  17074. Add('var');
  17075. Add(' A: texta;');
  17076. Add('begin');
  17077. Add(' a:=texta.new;');
  17078. ConvertProgram;
  17079. CheckSource('TestExternalClass_LocalConstSameName',
  17080. LinesToStr([ // statements
  17081. 'var ExtA$1 = 3;',
  17082. 'this.DoIt = function () {',
  17083. ' var Result = 0;',
  17084. ' Result = ExtA$1;',
  17085. ' return Result;',
  17086. '};',
  17087. 'this.A = null;',
  17088. '']),
  17089. LinesToStr([ // $mod.$main
  17090. '$mod.A = new ExtA();',
  17091. '']));
  17092. end;
  17093. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17094. begin
  17095. StartProgram(false);
  17096. Add('{$modeswitch externalclass}');
  17097. Add('type');
  17098. Add(' TExtA = class external name ''ExtA''');
  17099. Add(' procedure DoIt;');
  17100. Add(' end;');
  17101. Add(' TMyA = class(TExtA)');
  17102. Add(' procedure DoIt;');
  17103. Add(' end;');
  17104. Add('procedure TMyA.DoIt; begin end;');
  17105. Add('begin');
  17106. ConvertProgram;
  17107. CheckSource('TestExternalClass_ReintroduceOverload',
  17108. LinesToStr([ // statements
  17109. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17110. ' this.$init = function () {',
  17111. ' };',
  17112. ' this.$final = function () {',
  17113. ' };',
  17114. ' this.DoIt$1 = function () {',
  17115. ' };',
  17116. '});',
  17117. '']),
  17118. LinesToStr([ // $mod.$main
  17119. '']));
  17120. end;
  17121. procedure TTestModule.TestExternalClass_Inherited;
  17122. begin
  17123. StartProgram(false);
  17124. Add('{$modeswitch externalclass}');
  17125. Add('type');
  17126. Add(' TExtA = class external name ''ExtA''');
  17127. Add(' procedure DoIt(i: longint = 1); virtual;');
  17128. Add(' procedure DoSome(j: longint = 2);');
  17129. Add(' end;');
  17130. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17131. Add(' end;');
  17132. Add(' TMyC = class(TExtB)');
  17133. Add(' procedure DoIt(i: longint = 1); override;');
  17134. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17135. Add(' end;');
  17136. Add('procedure TMyC.DoIt(i: longint);');
  17137. Add('begin');
  17138. Add(' inherited;');
  17139. Add(' inherited DoIt;');
  17140. Add(' inherited DoIt();');
  17141. Add(' inherited DoIt(3);');
  17142. Add(' inherited DoSome;');
  17143. Add(' inherited DoSome();');
  17144. Add(' inherited DoSome(4);');
  17145. Add('end;');
  17146. Add('procedure TMyC.DoSome(j: longint);');
  17147. Add('begin');
  17148. Add(' inherited;');
  17149. Add('end;');
  17150. Add('begin');
  17151. ConvertProgram;
  17152. CheckSource('TestExternalClass_ReintroduceOverload',
  17153. LinesToStr([ // statements
  17154. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17155. ' this.$init = function () {',
  17156. ' };',
  17157. ' this.$final = function () {',
  17158. ' };',
  17159. ' this.DoIt = function (i) {',
  17160. ' ExtB.DoIt.apply(this, arguments);',
  17161. ' ExtB.DoIt.call(this, 1);',
  17162. ' ExtB.DoIt.call(this, 1);',
  17163. ' ExtB.DoIt.call(this, 3);',
  17164. ' ExtB.DoSome.call(this, 2);',
  17165. ' ExtB.DoSome.call(this, 2);',
  17166. ' ExtB.DoSome.call(this, 4);',
  17167. ' };',
  17168. ' this.DoSome$1 = function (j) {',
  17169. ' ExtB.DoSome.apply(this, arguments);',
  17170. ' };',
  17171. '});',
  17172. '']),
  17173. LinesToStr([ // $mod.$main
  17174. '']));
  17175. end;
  17176. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17177. begin
  17178. StartProgram(false);
  17179. Add('{$modeswitch externalclass}');
  17180. Add('type');
  17181. Add(' TObject = class');
  17182. Add(' end;');
  17183. Add(' TExtA = class external name ''ExtA''(TObject)');
  17184. Add(' end;');
  17185. Add('begin');
  17186. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17187. ConvertProgram;
  17188. end;
  17189. procedure TTestModule.TestExternalClass_NewInstance;
  17190. begin
  17191. StartProgram(false);
  17192. Add('{$modeswitch externalclass}');
  17193. Add('type');
  17194. Add(' TExtA = class external name ''ExtA''');
  17195. Add(' end;');
  17196. Add(' TMyB = class(TExtA)');
  17197. Add(' protected');
  17198. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17199. Add(' end;');
  17200. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17201. Add('begin end;');
  17202. Add('begin');
  17203. ConvertProgram;
  17204. CheckSource('TestExternalClass_NewInstance',
  17205. LinesToStr([ // statements
  17206. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17207. ' this.$init = function () {',
  17208. ' };',
  17209. ' this.$final = function () {',
  17210. ' };',
  17211. ' this.NewInstance = function (fnname, paramarray) {',
  17212. ' var Result = null;',
  17213. ' return Result;',
  17214. ' };',
  17215. '});',
  17216. '']),
  17217. LinesToStr([ // $mod.$main
  17218. '']));
  17219. end;
  17220. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17221. begin
  17222. StartProgram(false);
  17223. Add('{$modeswitch externalclass}');
  17224. Add('type');
  17225. Add(' TExtA = class external name ''ExtA''');
  17226. Add(' end;');
  17227. Add(' TMyB = class(TExtA)');
  17228. Add(' protected');
  17229. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17230. Add(' end;');
  17231. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17232. Add('begin end;');
  17233. Add('begin');
  17234. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17235. ConvertProgram;
  17236. end;
  17237. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17238. begin
  17239. StartProgram(false);
  17240. Add('{$modeswitch externalclass}');
  17241. Add('type');
  17242. Add(' TExtA = class external name ''ExtA''');
  17243. Add(' end;');
  17244. Add(' TMyB = class(TExtA)');
  17245. Add(' protected');
  17246. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17247. Add(' end;');
  17248. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17249. Add('begin end;');
  17250. Add('begin');
  17251. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17252. nIncompatibleTypeArgNo);
  17253. ConvertProgram;
  17254. end;
  17255. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17256. begin
  17257. StartProgram(false);
  17258. Add('{$modeswitch externalclass}');
  17259. Add('type');
  17260. Add(' TExtA = class external name ''ExtA''');
  17261. Add(' end;');
  17262. Add(' TMyB = class(TExtA)');
  17263. Add(' protected');
  17264. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17265. Add(' end;');
  17266. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17267. Add('begin end;');
  17268. Add('begin');
  17269. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17270. nIncompatibleTypeArgNo);
  17271. ConvertProgram;
  17272. end;
  17273. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17274. begin
  17275. StartProgram(false);
  17276. Add([
  17277. '{$modeswitch externalclass}',
  17278. 'type',
  17279. ' TJSFunction = class external name ''Function''',
  17280. ' end;',
  17281. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17282. ' constructor New(w: word);',
  17283. ' end;',
  17284. ' TBird = class (TExtA)',
  17285. ' public',
  17286. ' Size: word;',
  17287. ' class var Legs: word;',
  17288. ' constructor Create(a: word);',
  17289. ' end;',
  17290. ' TEagle = class (TBird)',
  17291. ' public',
  17292. ' constructor Create(b: word); reintroduce;',
  17293. ' end;',
  17294. 'constructor TBird.Create(a: word);',
  17295. 'begin',
  17296. ' inherited;', // silently ignored
  17297. ' inherited New(a);', // this.$func(a)
  17298. 'end;',
  17299. 'constructor TEagle.Create(b: word);',
  17300. 'begin',
  17301. ' inherited Create(b);',
  17302. 'end;',
  17303. 'var',
  17304. ' Bird: TBird;',
  17305. ' Eagle: TEagle;',
  17306. 'begin',
  17307. ' Bird:=TBird.Create(3);',
  17308. ' Eagle:=TEagle.Create(4);',
  17309. ' Bird.Size:=Bird.Size+5;',
  17310. ' Bird.Legs:=Bird.Legs+6;',
  17311. ' Eagle.Size:=Eagle.Size+5;',
  17312. ' Eagle.Legs:=Eagle.Legs+6;',
  17313. '']);
  17314. ConvertProgram;
  17315. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17316. LinesToStr([ // statements
  17317. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17318. ' this.Legs = 0;',
  17319. ' this.$init = function () {',
  17320. ' this.Size = 0;',
  17321. ' };',
  17322. ' this.$final = function () {',
  17323. ' };',
  17324. ' this.Create = function (a) {',
  17325. ' this.$ancestorfunc(a);',
  17326. ' return this;',
  17327. ' };',
  17328. '});',
  17329. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17330. ' this.Create$1 = function (b) {',
  17331. ' $mod.TBird.Create.call(this, b);',
  17332. ' return this;',
  17333. ' };',
  17334. '});',
  17335. 'this.Bird = null;',
  17336. 'this.Eagle = null;',
  17337. '']),
  17338. LinesToStr([ // $mod.$main
  17339. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17340. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17341. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17342. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17343. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17344. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17345. '']));
  17346. end;
  17347. procedure TTestModule.TestExternalClass_PascalProperty;
  17348. begin
  17349. StartProgram(false);
  17350. Add('{$modeswitch externalclass}');
  17351. Add('type');
  17352. Add(' TJSElement = class;');
  17353. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17354. Add(' TJSElement = class external name ''ExtA''');
  17355. Add(' end;');
  17356. Add(' TControl = class(TJSElement)');
  17357. Add(' private');
  17358. Add(' FOnClick: TJSNotifyEvent;');
  17359. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17360. Add(' procedure Click(Sender: TJSElement);');
  17361. Add(' end;');
  17362. Add('procedure TControl.Click(Sender: TJSElement);');
  17363. Add('begin');
  17364. Add(' OnClick(Self);');
  17365. Add('end;');
  17366. Add('var');
  17367. Add(' Ctrl: TControl;');
  17368. Add('begin');
  17369. Add(' Ctrl.OnClick:[email protected];');
  17370. Add(' Ctrl.OnClick(Ctrl);');
  17371. ConvertProgram;
  17372. CheckSource('TestExternalClass_PascalProperty',
  17373. LinesToStr([ // statements
  17374. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17375. ' this.$init = function () {',
  17376. ' this.FOnClick = null;',
  17377. ' };',
  17378. ' this.$final = function () {',
  17379. ' this.FOnClick = undefined;',
  17380. ' };',
  17381. ' this.Click = function (Sender) {',
  17382. ' this.FOnClick(this);',
  17383. ' };',
  17384. '});',
  17385. 'this.Ctrl = null;',
  17386. '']),
  17387. LinesToStr([ // $mod.$main
  17388. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17389. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17390. '']));
  17391. end;
  17392. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17393. begin
  17394. StartProgram(false);
  17395. Add([
  17396. '{$modeswitch externalclass}',
  17397. 'type',
  17398. ' IUnknown = interface end;',
  17399. ' TObject = class',
  17400. ' end;',
  17401. ' TChild = class',
  17402. ' end;',
  17403. ' TExtRootA = class external name ''ExtRootA''',
  17404. ' end;',
  17405. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17406. ' end;',
  17407. ' TExtRootB = class external name ''ExtRootB''',
  17408. ' end;',
  17409. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17410. ' end;',
  17411. 'var',
  17412. ' Obj: TObject;',
  17413. ' Child: TChild;',
  17414. ' RootA: TExtRootA;',
  17415. ' ChildA: TExtChildA;',
  17416. ' RootB: TExtRootB;',
  17417. ' ChildB: TExtChildB;',
  17418. ' i: IUnknown;',
  17419. 'begin',
  17420. ' obj:=tobject(roota);',
  17421. ' obj:=tobject(childa);',
  17422. ' child:=tchild(tobject(roota));',
  17423. ' roota:=textroota(obj);',
  17424. ' roota:=textroota(child);',
  17425. ' roota:=textroota(rootb);',
  17426. ' roota:=textroota(childb);',
  17427. ' childa:=textchilda(textroota(obj));',
  17428. ' roota:=TExtRootA(i)',
  17429. '']);
  17430. ConvertProgram;
  17431. CheckSource('TestExternalClass_TypeCastToRootClass',
  17432. LinesToStr([ // statements
  17433. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17434. 'rtl.createClass(this, "TObject", null, function () {',
  17435. ' this.$init = function () {',
  17436. ' };',
  17437. ' this.$final = function () {',
  17438. ' };',
  17439. '});',
  17440. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17441. '});',
  17442. 'this.Obj = null;',
  17443. 'this.Child = null;',
  17444. 'this.RootA = null;',
  17445. 'this.ChildA = null;',
  17446. 'this.RootB = null;',
  17447. 'this.ChildB = null;',
  17448. 'this.i = null;',
  17449. '']),
  17450. LinesToStr([ // $mod.$main
  17451. '$mod.Obj = $mod.RootA;',
  17452. '$mod.Obj = $mod.ChildA;',
  17453. '$mod.Child = $mod.RootA;',
  17454. '$mod.RootA = $mod.Obj;',
  17455. '$mod.RootA = $mod.Child;',
  17456. '$mod.RootA = $mod.RootB;',
  17457. '$mod.RootA = $mod.ChildB;',
  17458. '$mod.ChildA = $mod.Obj;',
  17459. '$mod.RootA = $mod.i;',
  17460. '']));
  17461. end;
  17462. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17463. begin
  17464. StartProgram(false);
  17465. Add([
  17466. '{$modeswitch externalclass}',
  17467. 'type',
  17468. ' IUnknown = interface end;',
  17469. ' IBird = interface(IUnknown) end;',
  17470. ' TClass = class of TObject;',
  17471. ' TObject = class',
  17472. ' end;',
  17473. ' TChild = class',
  17474. ' end;',
  17475. ' TJSObject = class external name ''Object''',
  17476. ' end;',
  17477. ' TRec = record end;',
  17478. 'var',
  17479. ' Obj: TObject;',
  17480. ' Child: TChild;',
  17481. ' i: IUnknown;',
  17482. ' Bird: IBird;',
  17483. ' j: TJSObject;',
  17484. ' r: TRec;',
  17485. ' c: TClass;',
  17486. 'begin',
  17487. ' j:=tjsobject(IUnknown);',
  17488. ' j:=tjsobject(IBird);',
  17489. ' j:=tjsobject(TObject);',
  17490. ' j:=tjsobject(TChild);',
  17491. ' j:=tjsobject(TRec);',
  17492. ' j:=tjsobject(Obj);',
  17493. ' j:=tjsobject(Child);',
  17494. ' j:=tjsobject(i);',
  17495. ' j:=tjsobject(Bird);',
  17496. ' j:=tjsobject(r);',
  17497. ' j:=tjsobject(c);',
  17498. '']);
  17499. ConvertProgram;
  17500. CheckSource('TestExternalClass_TypeCastToJSObject',
  17501. LinesToStr([ // statements
  17502. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17503. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17504. 'rtl.createClass(this, "TObject", null, function () {',
  17505. ' this.$init = function () {',
  17506. ' };',
  17507. ' this.$final = function () {',
  17508. ' };',
  17509. '});',
  17510. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17511. '});',
  17512. 'rtl.recNewT(this, "TRec", function () {',
  17513. ' this.$eq = function (b) {',
  17514. ' return true;',
  17515. ' };',
  17516. ' this.$assign = function (s) {',
  17517. ' return this;',
  17518. ' };',
  17519. '});',
  17520. 'this.Obj = null;',
  17521. 'this.Child = null;',
  17522. 'this.i = null;',
  17523. 'this.Bird = null;',
  17524. 'this.j = null;',
  17525. 'this.r = this.TRec.$new();',
  17526. 'this.c = null;',
  17527. '']),
  17528. LinesToStr([ // $mod.$main
  17529. '$mod.j = $mod.IUnknown;',
  17530. '$mod.j = $mod.IBird;',
  17531. '$mod.j = $mod.TObject;',
  17532. '$mod.j = $mod.TChild;',
  17533. '$mod.j = $mod.TRec;',
  17534. '$mod.j = $mod.Obj;',
  17535. '$mod.j = $mod.Child;',
  17536. '$mod.j = $mod.i;',
  17537. '$mod.j = $mod.Bird;',
  17538. '$mod.j = $mod.r;',
  17539. '$mod.j = $mod.c;',
  17540. '']));
  17541. end;
  17542. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17543. begin
  17544. StartProgram(false);
  17545. Add('{$modeswitch externalclass}');
  17546. Add('type');
  17547. Add(' TJSString = class external name ''String''');
  17548. Add(' class function fromCharCode() : string; varargs;');
  17549. Add(' function anchor(const aName : string) : string;');
  17550. Add(' end;');
  17551. Add('var');
  17552. Add(' s: string;');
  17553. Add('begin');
  17554. Add(' s:=TJSString.fromCharCode(65,66);');
  17555. Add(' s:=TJSString(s).anchor(s);');
  17556. Add(' s:=TJSString(''foo'').anchor(s);');
  17557. ConvertProgram;
  17558. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17559. LinesToStr([ // statements
  17560. 'this.s = "";',
  17561. '']),
  17562. LinesToStr([ // $mod.$main
  17563. '$mod.s = String.fromCharCode(65, 66);',
  17564. '$mod.s = $mod.s.anchor($mod.s);',
  17565. '$mod.s = "foo".anchor($mod.s);',
  17566. '']));
  17567. end;
  17568. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17569. begin
  17570. StartProgram(false);
  17571. Add([
  17572. '{$modeswitch externalclass}',
  17573. 'type',
  17574. ' TJSObject = class external name ''Object'' end;',
  17575. ' TJSFunction = class external name ''Function''',
  17576. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17577. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17578. ' end;',
  17579. ' TObject = class',
  17580. ' procedure DoIt(i: longint);',
  17581. ' end;',
  17582. ' TFuncInt = function(o: TObject): longint;',
  17583. 'function GetIt(o: TObject): longint;',
  17584. ' procedure Sub; begin end;',
  17585. 'var',
  17586. ' f: TJSFunction;',
  17587. ' fi: TFuncInt;',
  17588. 'begin',
  17589. ' fi:=TFuncInt(f);',
  17590. ' f:=TJSFunction(fi);',
  17591. ' f:=TJSFunction(@GetIt);',
  17592. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17593. ' f:=TJSFunction(@Sub);',
  17594. ' f:=TJSFunction(@o.doit);',
  17595. ' f:=TJSFunction(fi).bind(nil,4)',
  17596. 'end;',
  17597. 'procedure TObject.DoIt(i: longint);',
  17598. ' procedure Sub; begin end;',
  17599. 'var f: TJSFunction;',
  17600. 'begin',
  17601. ' f:=TJSFunction(@DoIt);',
  17602. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17603. ' f:=TJSFunction(@Sub);',
  17604. ' f:=TJSFunction(@GetIt);',
  17605. 'end;',
  17606. 'begin']);
  17607. ConvertProgram;
  17608. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17609. LinesToStr([ // statements
  17610. 'rtl.createClass(this, "TObject", null, function () {',
  17611. ' this.$init = function () {',
  17612. ' };',
  17613. ' this.$final = function () {',
  17614. ' };',
  17615. ' this.DoIt = function (i) {',
  17616. ' var $Self = this;',
  17617. ' function Sub() {',
  17618. ' };',
  17619. ' var f = null;',
  17620. ' f = this.DoIt;',
  17621. ' f = this.DoIt.bind(null, 13);',
  17622. ' f = Sub;',
  17623. ' f = $mod.GetIt;',
  17624. ' };',
  17625. '});',
  17626. 'this.GetIt = function (o) {',
  17627. ' var Result = 0;',
  17628. ' function Sub() {',
  17629. ' };',
  17630. ' var f = null;',
  17631. ' var fi = null;',
  17632. ' fi = f;',
  17633. ' f = fi;',
  17634. ' f = $mod.GetIt;',
  17635. ' f = $mod.GetIt.bind(null, 3);',
  17636. ' f = Sub;',
  17637. ' f = $mod.TObject.DoIt;',
  17638. ' f = fi.bind(null, 4);',
  17639. ' return Result;',
  17640. '};',
  17641. '']),
  17642. LinesToStr([ // $mod.$main
  17643. '']));
  17644. end;
  17645. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17646. begin
  17647. StartProgram(false);
  17648. Add([
  17649. '{$mode delphi}',
  17650. '{$modeswitch externalclass}',
  17651. 'type',
  17652. ' TJSObject = class external name ''Object'' end;',
  17653. ' TJSWindow = class external name ''Window''(TJSObject)',
  17654. ' procedure Open;',
  17655. ' end;',
  17656. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17657. ' procedure Execute;',
  17658. ' end;',
  17659. 'procedure Fly;',
  17660. 'var',
  17661. ' w: TJSWindow;',
  17662. ' e: TJSEventTarget;',
  17663. 'begin',
  17664. ' w:=TJSWindow(e);',
  17665. ' e:=TJSEventTarget(w);',
  17666. 'end;',
  17667. 'begin']);
  17668. ConvertProgram;
  17669. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17670. LinesToStr([ // statements
  17671. 'this.Fly = function () {',
  17672. ' var w = null;',
  17673. ' var e = null;',
  17674. ' w = e;',
  17675. ' e = w;',
  17676. '};',
  17677. '']),
  17678. LinesToStr([ // $mod.$main
  17679. '']));
  17680. end;
  17681. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17682. begin
  17683. StartProgram(false);
  17684. Add('{$modeswitch externalclass}');
  17685. Add('type');
  17686. Add(' TJSString = class external name ''String''');
  17687. Add(' class function fromCharCode() : string; varargs;');
  17688. Add(' end;');
  17689. Add('var');
  17690. Add(' s: string;');
  17691. Add(' sObj: TJSString;');
  17692. Add('begin');
  17693. Add(' s:=sObj.fromCharCode(65,66);');
  17694. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17695. nExternalClassInstanceCannotAccessStaticX);
  17696. ConvertProgram;
  17697. end;
  17698. procedure TTestModule.TestExternalClass_BracketAccessor;
  17699. begin
  17700. StartProgram(false);
  17701. Add([
  17702. '{$modeswitch externalclass}',
  17703. 'type',
  17704. ' TJSArray = class external name ''Array2''',
  17705. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17706. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17707. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17708. ' end;',
  17709. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17710. 'begin end;',
  17711. 'var',
  17712. ' Arr: tjsarray;',
  17713. ' s: string;',
  17714. ' i: longint;',
  17715. ' v: jsvalue;',
  17716. 'begin',
  17717. ' v:=arr[0];',
  17718. ' v:=arr.items[1];',
  17719. ' arr[2]:=s;',
  17720. ' arr.items[3]:=s;',
  17721. ' arr[4]:=i;',
  17722. ' arr[5]:=arr[6];',
  17723. ' arr.items[7]:=arr.items[8];',
  17724. ' with arr do items[9]:=items[10];',
  17725. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17726. ' with arr do begin',
  17727. ' v:=GetItems(14);',
  17728. ' setitems(15,16);',
  17729. ' end;',
  17730. ' v:=test1.arr.items[17];',
  17731. ' test1.arr.items[18]:=v;',
  17732. '']);
  17733. ConvertProgram;
  17734. CheckSource('TestExternalClass_BracketAccessor',
  17735. LinesToStr([ // statements
  17736. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17737. '};',
  17738. 'this.Arr = null;',
  17739. 'this.s = "";',
  17740. 'this.i = 0;',
  17741. 'this.v = undefined;',
  17742. '']),
  17743. LinesToStr([ // $mod.$main
  17744. '$mod.v = $mod.Arr[0];',
  17745. '$mod.v = $mod.Arr[1];',
  17746. '$mod.Arr[2] = $mod.s;',
  17747. '$mod.Arr[3] = $mod.s;',
  17748. '$mod.Arr[4] = $mod.i;',
  17749. '$mod.Arr[5] = $mod.Arr[6];',
  17750. '$mod.Arr[7] = $mod.Arr[8];',
  17751. 'var $with = $mod.Arr;',
  17752. '$with[9] = $with[10];',
  17753. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17754. ' a: 9,',
  17755. ' p: $mod.Arr,',
  17756. ' get: function () {',
  17757. ' return this.p[this.a];',
  17758. ' },',
  17759. ' set: function (v) {',
  17760. ' this.p[this.a] = v;',
  17761. ' }',
  17762. '}, {',
  17763. ' a: 10,',
  17764. ' p: $mod.Arr,',
  17765. ' get: function () {',
  17766. ' return this.p[this.a];',
  17767. ' },',
  17768. ' set: function (v) {',
  17769. ' this.p[this.a] = v;',
  17770. ' }',
  17771. '});',
  17772. 'var $with1 = $mod.Arr;',
  17773. '$mod.v = $with1[14];',
  17774. '$with1[15] = 16;',
  17775. '$mod.v = $mod.Arr[17];',
  17776. '$mod.Arr[18] = $mod.v;',
  17777. '']));
  17778. end;
  17779. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17780. begin
  17781. StartProgram(false);
  17782. Add([
  17783. '{$modeswitch externalclass}',
  17784. 'type',
  17785. ' TJSArray = class external name ''Array2''',
  17786. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17787. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17788. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17789. ' end;',
  17790. ' TMyArr = class(TJSArray)',
  17791. ' procedure DoIt;',
  17792. ' end;',
  17793. 'procedure tmyarr.DoIt;',
  17794. 'begin',
  17795. ' Items[1]:=Items[2];',
  17796. ' SetItems(3,getItems(4));',
  17797. 'end;',
  17798. 'var',
  17799. ' Arr: tmyarr;',
  17800. ' s: string;',
  17801. ' i: longint;',
  17802. ' v: jsvalue;',
  17803. 'begin',
  17804. ' v:=arr[0];',
  17805. ' v:=arr.items[1];',
  17806. ' arr[2]:=s;',
  17807. ' arr.items[3]:=s;',
  17808. ' arr[4]:=i;',
  17809. ' arr[5]:=arr[6];',
  17810. ' arr.items[7]:=arr.items[8];',
  17811. ' with arr do items[9]:=items[10];',
  17812. ' with arr do begin',
  17813. ' v:=GetItems(14);',
  17814. ' setitems(15,16);',
  17815. ' end;',
  17816. '']);
  17817. ConvertProgram;
  17818. CheckSource('TestExternalClass_BracketAccessor_Call',
  17819. LinesToStr([ // statements
  17820. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  17821. ' this.$init = function () {',
  17822. ' };',
  17823. ' this.$final = function () {',
  17824. ' };',
  17825. ' this.DoIt = function () {',
  17826. ' this[1] = this[2];',
  17827. ' this[3] = this[4];',
  17828. ' };',
  17829. '});',
  17830. 'this.Arr = null;',
  17831. 'this.s = "";',
  17832. 'this.i = 0;',
  17833. 'this.v = undefined;',
  17834. '']),
  17835. LinesToStr([ // $mod.$main
  17836. '$mod.v = $mod.Arr[0];',
  17837. '$mod.v = $mod.Arr[1];',
  17838. '$mod.Arr[2] = $mod.s;',
  17839. '$mod.Arr[3] = $mod.s;',
  17840. '$mod.Arr[4] = $mod.i;',
  17841. '$mod.Arr[5] = $mod.Arr[6];',
  17842. '$mod.Arr[7] = $mod.Arr[8];',
  17843. 'var $with = $mod.Arr;',
  17844. '$with[9] = $with[10];',
  17845. 'var $with1 = $mod.Arr;',
  17846. '$mod.v = $with1[14];',
  17847. '$with1[15] = 16;',
  17848. '']));
  17849. end;
  17850. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17851. begin
  17852. StartProgram(false);
  17853. Add('{$modeswitch externalclass}');
  17854. Add('type');
  17855. Add(' TJSArray = class external name ''Array2''');
  17856. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17857. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17858. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17859. Add(' end;');
  17860. Add('begin');
  17861. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17862. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17863. ConvertProgram;
  17864. end;
  17865. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17866. begin
  17867. StartProgram(false);
  17868. Add('{$modeswitch externalclass}');
  17869. Add('type');
  17870. Add(' TJSArray = class external name ''Array2''');
  17871. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17872. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17873. Add(' end;');
  17874. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17875. Add('begin end;');
  17876. Add('var');
  17877. Add(' Arr: tjsarray;');
  17878. Add(' v: jsvalue;');
  17879. Add('begin');
  17880. Add(' v:=arr[0];');
  17881. Add(' v:=arr.items[1];');
  17882. Add(' with arr do v:=items[2];');
  17883. Add(' doit(arr[3],arr[4]);');
  17884. ConvertProgram;
  17885. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17886. LinesToStr([ // statements
  17887. 'this.DoIt = function (vI, vJ) {',
  17888. '};',
  17889. 'this.Arr = null;',
  17890. 'this.v = undefined;',
  17891. '']),
  17892. LinesToStr([ // $mod.$main
  17893. '$mod.v = $mod.Arr[0];',
  17894. '$mod.v = $mod.Arr[1];',
  17895. 'var $with = $mod.Arr;',
  17896. '$mod.v = $with[2];',
  17897. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  17898. '']));
  17899. end;
  17900. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  17901. begin
  17902. StartProgram(false);
  17903. Add('{$modeswitch externalclass}');
  17904. Add('type');
  17905. Add(' TJSArray = class external name ''Array2''');
  17906. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17907. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17908. Add(' end;');
  17909. Add('var');
  17910. Add(' Arr: tjsarray;');
  17911. Add(' s: string;');
  17912. Add(' i: longint;');
  17913. Add(' v: jsvalue;');
  17914. Add('begin');
  17915. Add(' arr[2]:=s;');
  17916. Add(' arr.items[3]:=s;');
  17917. Add(' arr[4]:=i;');
  17918. Add(' with arr do items[5]:=i;');
  17919. ConvertProgram;
  17920. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  17921. LinesToStr([ // statements
  17922. 'this.Arr = null;',
  17923. 'this.s = "";',
  17924. 'this.i = 0;',
  17925. 'this.v = undefined;',
  17926. '']),
  17927. LinesToStr([ // $mod.$main
  17928. '$mod.Arr[2] = $mod.s;',
  17929. '$mod.Arr[3] = $mod.s;',
  17930. '$mod.Arr[4] = $mod.i;',
  17931. 'var $with = $mod.Arr;',
  17932. '$with[5] = $mod.i;',
  17933. '']));
  17934. end;
  17935. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17936. begin
  17937. StartProgram(false);
  17938. Add('{$modeswitch externalclass}');
  17939. Add('type');
  17940. Add(' TJSArray = class external name ''Array2''');
  17941. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17942. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17943. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17944. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17945. Add(' end;');
  17946. Add('var');
  17947. Add(' Arr: tjsarray;');
  17948. Add(' s: string;');
  17949. Add(' i: longint;');
  17950. Add(' v: jsvalue;');
  17951. Add('begin');
  17952. Add(' arr[2]:=s;');
  17953. Add(' arr.items[3]:=s;');
  17954. Add(' arr.numbers[4]:=i;');
  17955. Add(' with arr do items[5]:=i;');
  17956. Add(' with arr do numbers[6]:=i;');
  17957. ConvertProgram;
  17958. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17959. LinesToStr([ // statements
  17960. 'this.Arr = null;',
  17961. 'this.s = "";',
  17962. 'this.i = 0;',
  17963. 'this.v = undefined;',
  17964. '']),
  17965. LinesToStr([ // $mod.$main
  17966. '$mod.Arr[2] = $mod.s;',
  17967. '$mod.Arr[3] = $mod.s;',
  17968. '$mod.Arr[4] = $mod.i;',
  17969. 'var $with = $mod.Arr;',
  17970. '$with[5] = $mod.i;',
  17971. 'var $with1 = $mod.Arr;',
  17972. '$with1[6] = $mod.i;',
  17973. '']));
  17974. end;
  17975. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17976. begin
  17977. StartProgram(false);
  17978. Add('{$modeswitch externalclass}');
  17979. Add('type');
  17980. Add(' TJSArray = class external name ''Array2''');
  17981. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17982. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17983. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17984. Add(' end;');
  17985. Add('var');
  17986. Add(' Arr: tjsarray;');
  17987. Add(' i: longint;');
  17988. Add(' IntArr: array of longint;');
  17989. Add(' v: jsvalue;');
  17990. Add('begin');
  17991. Add(' v:=arr.items[i];');
  17992. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17993. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17994. ConvertProgram;
  17995. CheckSource('TestExternalClass_BracketAccessor_Index',
  17996. LinesToStr([ // statements
  17997. 'this.Arr = null;',
  17998. 'this.i = 0;',
  17999. 'this.IntArr = [];',
  18000. 'this.v = undefined;',
  18001. '']),
  18002. LinesToStr([ // $mod.$main
  18003. '$mod.v = $mod.Arr[$mod.i];',
  18004. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18005. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18006. '']));
  18007. end;
  18008. procedure TTestModule.TestExternalClass_ForInJSObject;
  18009. begin
  18010. StartProgram(false);
  18011. Add([
  18012. '{$modeswitch externalclass}',
  18013. 'type',
  18014. ' TJSObject = class external name ''Object''',
  18015. ' end;',
  18016. 'var',
  18017. ' o: TJSObject;',
  18018. ' key: string;',
  18019. 'begin',
  18020. ' for key in o do',
  18021. ' if key=''abc'' then ;',
  18022. '']);
  18023. ConvertProgram;
  18024. CheckSource('TestExternalClass_ForInJSObject',
  18025. LinesToStr([ // statements
  18026. 'this.o = null;',
  18027. 'this.key = "";',
  18028. '']),
  18029. LinesToStr([ // $mod.$main
  18030. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18031. '']));
  18032. end;
  18033. procedure TTestModule.TestExternalClass_ForInJSArray;
  18034. begin
  18035. StartProgram(false);
  18036. Add([
  18037. '{$modeswitch externalclass}',
  18038. 'type',
  18039. ' TJSInt8Array = class external name ''Int8Array''',
  18040. ' private',
  18041. ' flength: NativeInt external name ''length'';',
  18042. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18043. ' public',
  18044. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18045. ' property Length: NativeInt read flength;',
  18046. ' end;',
  18047. 'var',
  18048. ' a: TJSInt8Array;',
  18049. ' value: shortint;',
  18050. 'begin',
  18051. ' for value in a do',
  18052. ' if value=3 then ;',
  18053. '']);
  18054. ConvertProgram;
  18055. CheckSource('TestExternalClass_ForInJSArray',
  18056. LinesToStr([ // statements
  18057. 'this.a = null;',
  18058. 'this.value = 0;',
  18059. '']),
  18060. LinesToStr([ // $mod.$main
  18061. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18062. ' $mod.value = $in[$l];',
  18063. ' if ($mod.value === 3) ;',
  18064. '};',
  18065. '']));
  18066. end;
  18067. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18068. begin
  18069. AddModuleWithIntfImplSrc('unit2.pas',
  18070. LinesToStr([
  18071. '{$modeswitch externalclass}',
  18072. 'type',
  18073. ' TJSBufferSource = class external name ''BufferSource''',
  18074. ' end;',
  18075. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18076. '']),
  18077. '');
  18078. AddModuleWithIntfImplSrc('unit3.pas',
  18079. LinesToStr([
  18080. '{$modeswitch externalclass}',
  18081. 'type',
  18082. ' TJSBufferSource = class external name ''BufferSource''',
  18083. ' end;',
  18084. '']),
  18085. '');
  18086. StartUnit(true);
  18087. Add([
  18088. 'interface',
  18089. 'uses unit2, unit3;',
  18090. 'procedure DoSome(s: TJSBufferSource);',
  18091. 'implementation',
  18092. 'procedure DoSome(s: TJSBufferSource);',
  18093. 'begin',
  18094. ' DoIt(s);',
  18095. 'end;',
  18096. '']);
  18097. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18098. nIncompatibleTypeArgNo);
  18099. ConvertUnit;
  18100. end;
  18101. procedure TTestModule.TestClassInterface_Corba;
  18102. begin
  18103. StartProgram(false);
  18104. Add([
  18105. '{$interfaces corba}',
  18106. 'type',
  18107. ' IUnknown = interface;',
  18108. ' IUnknown = interface',
  18109. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18110. ' end;',
  18111. ' IInterface = IUnknown;',
  18112. ' IBird = interface(IInterface)',
  18113. ' function GetSize: longint;',
  18114. ' procedure SetSize(i: longint);',
  18115. ' property Size: longint read GetSize write SetSize;',
  18116. ' procedure DoIt(i: longint);',
  18117. ' end;',
  18118. ' TObject = class',
  18119. ' end;',
  18120. ' TBird = class(TObject,IBird)',
  18121. ' function GetSize: longint; virtual; abstract;',
  18122. ' procedure SetSize(i: longint); virtual; abstract;',
  18123. ' procedure DoIt(i: longint); virtual; abstract;',
  18124. ' end;',
  18125. 'var',
  18126. ' BirdIntf: IBird;',
  18127. 'begin',
  18128. ' BirdIntf.Size:=BirdIntf.Size;',
  18129. '']);
  18130. ConvertProgram;
  18131. CheckSource('TestClassInterface_Corba',
  18132. LinesToStr([ // statements
  18133. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18134. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18135. 'rtl.createClass(this, "TObject", null, function () {',
  18136. ' this.$init = function () {',
  18137. ' };',
  18138. ' this.$final = function () {',
  18139. ' };',
  18140. '});',
  18141. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18142. ' rtl.addIntf(this, $mod.IBird);',
  18143. '});',
  18144. 'this.BirdIntf = null;',
  18145. '']),
  18146. LinesToStr([ // $mod.$main
  18147. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18148. '']));
  18149. end;
  18150. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18151. begin
  18152. StartProgram(false);
  18153. Add([
  18154. '{$interfaces corba}',
  18155. 'type',
  18156. ' IUnknown = interface',
  18157. ' procedure DoIt; external name ''foo'';',
  18158. ' end;',
  18159. 'begin']);
  18160. SetExpectedParserError(
  18161. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18162. nParserNoFieldsAllowed);
  18163. ConvertProgram;
  18164. end;
  18165. procedure TTestModule.TestClassInterface_Overloads;
  18166. begin
  18167. StartProgram(false);
  18168. Add([
  18169. '{$interfaces corba}',
  18170. 'type',
  18171. ' integer = longint;',
  18172. ' IUnknown = interface',
  18173. ' procedure DoIt(i: integer);',
  18174. ' procedure DoIt(s: string);',
  18175. ' end;',
  18176. ' IBird = interface(IUnknown)',
  18177. ' procedure DoIt(b: boolean); overload;',
  18178. ' end;',
  18179. ' TObject = class',
  18180. ' end;',
  18181. ' TBird = class(TObject,IBird)',
  18182. ' procedure DoIt(o: TObject);',
  18183. ' procedure DoIt(s: string);',
  18184. ' procedure DoIt(i: integer);',
  18185. ' procedure DoIt(b: boolean);',
  18186. ' end;',
  18187. 'procedure TBird.DoIt(o: TObject); begin end;',
  18188. 'procedure TBird.DoIt(s: string); begin end;',
  18189. 'procedure TBird.DoIt(i: integer); begin end;',
  18190. 'procedure TBird.DoIt(b: boolean); begin end;',
  18191. 'var',
  18192. ' BirdIntf: IBird;',
  18193. 'begin',
  18194. ' BirdIntf.DoIt(3);',
  18195. ' BirdIntf.DoIt(''abc'');',
  18196. ' BirdIntf.DoIt(true);',
  18197. '']);
  18198. ConvertProgram;
  18199. CheckSource('TestClassInterface_Overloads',
  18200. LinesToStr([ // statements
  18201. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18202. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18203. 'rtl.createClass(this, "TObject", null, function () {',
  18204. ' this.$init = function () {',
  18205. ' };',
  18206. ' this.$final = function () {',
  18207. ' };',
  18208. '});',
  18209. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18210. ' this.DoIt = function (o) {',
  18211. ' };',
  18212. ' this.DoIt$1 = function (s) {',
  18213. ' };',
  18214. ' this.DoIt$2 = function (i) {',
  18215. ' };',
  18216. ' this.DoIt$3 = function (b) {',
  18217. ' };',
  18218. ' rtl.addIntf(this, $mod.IBird, {',
  18219. ' DoIt$2: "DoIt$3",',
  18220. ' DoIt: "DoIt$2"',
  18221. ' });',
  18222. '});',
  18223. 'this.BirdIntf = null;',
  18224. '']),
  18225. LinesToStr([ // $mod.$main
  18226. '$mod.BirdIntf.DoIt(3);',
  18227. '$mod.BirdIntf.DoIt$1("abc");',
  18228. '$mod.BirdIntf.DoIt$2(true);',
  18229. '']));
  18230. end;
  18231. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18232. begin
  18233. StartProgram(false);
  18234. Add([
  18235. '{$interfaces corba}',
  18236. 'type',
  18237. ' IBird = interface',
  18238. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18239. ' end;',
  18240. ' IDog = interface',
  18241. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18242. ' end;',
  18243. ' TObject = class(IBird,IDog)',
  18244. ' end;',
  18245. 'begin']);
  18246. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18247. nDuplicateGUIDXInYZ);
  18248. ConvertProgram;
  18249. end;
  18250. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18251. begin
  18252. StartProgram(false);
  18253. Add([
  18254. '{$interfaces corba}',
  18255. 'type',
  18256. ' IAnimal = interface',
  18257. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18258. ' end;',
  18259. ' IBird = interface(IAnimal)',
  18260. ' end;',
  18261. ' IHawk = interface(IBird)',
  18262. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18263. ' end;',
  18264. 'begin']);
  18265. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18266. nDuplicateGUIDXInYZ);
  18267. ConvertProgram;
  18268. end;
  18269. procedure TTestModule.TestClassInterface_AncestorImpl;
  18270. begin
  18271. StartProgram(false);
  18272. Add([
  18273. '{$interfaces corba}',
  18274. 'type',
  18275. ' integer = longint;',
  18276. ' IUnknown = interface',
  18277. ' procedure DoIt(i: integer);',
  18278. ' end;',
  18279. ' IBird = interface',
  18280. ' procedure Fly(i: integer);',
  18281. ' end;',
  18282. ' TObject = class(IUnknown)',
  18283. ' procedure DoIt(i: integer);',
  18284. ' end;',
  18285. ' TBird = class(IBird)',
  18286. ' procedure Fly(i: integer);',
  18287. ' end;',
  18288. 'procedure TObject.DoIt(i: integer); begin end;',
  18289. 'procedure TBird.Fly(i: integer); begin end;',
  18290. 'begin',
  18291. '']);
  18292. ConvertProgram;
  18293. CheckSource('TestClassInterface_AncestorIntf',
  18294. LinesToStr([ // statements
  18295. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18296. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18297. 'rtl.createClass(this, "TObject", null, function () {',
  18298. ' this.$init = function () {',
  18299. ' };',
  18300. ' this.$final = function () {',
  18301. ' };',
  18302. ' this.DoIt = function (i) {',
  18303. ' };',
  18304. ' rtl.addIntf(this, $mod.IUnknown);',
  18305. '});',
  18306. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18307. ' this.Fly = function (i) {',
  18308. ' };',
  18309. ' rtl.addIntf(this, $mod.IBird);',
  18310. ' rtl.addIntf(this, $mod.IUnknown);',
  18311. '});',
  18312. '']),
  18313. LinesToStr([ // $mod.$main
  18314. '']));
  18315. end;
  18316. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18317. begin
  18318. StartProgram(false);
  18319. Add([
  18320. '{$interfaces corba}',
  18321. 'type',
  18322. ' integer = longint;',
  18323. ' IBird = interface',
  18324. ' procedure DoIt(i: integer);',
  18325. ' end;',
  18326. ' TObject = class',
  18327. ' procedure DoIt(i: integer);',
  18328. ' end;',
  18329. ' TBird = class(IBird)',
  18330. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18331. ' end;',
  18332. 'procedure TObject.DoIt(i: integer); begin end;',
  18333. 'procedure TBird.DoIt(i: integer); begin end;',
  18334. 'begin',
  18335. '']);
  18336. ConvertProgram;
  18337. CheckSource('TestClassInterface_ImplReintroduce',
  18338. LinesToStr([ // statements
  18339. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18340. 'rtl.createClass(this, "TObject", null, function () {',
  18341. ' this.$init = function () {',
  18342. ' };',
  18343. ' this.$final = function () {',
  18344. ' };',
  18345. ' this.DoIt = function (i) {',
  18346. ' };',
  18347. '});',
  18348. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18349. ' this.DoIt$1 = function (i) {',
  18350. ' };',
  18351. ' rtl.addIntf(this, $mod.IBird, {',
  18352. ' DoIt: "DoIt$1"',
  18353. ' });',
  18354. '});',
  18355. '']),
  18356. LinesToStr([ // $mod.$main
  18357. '']));
  18358. end;
  18359. procedure TTestModule.TestClassInterface_MethodResolution;
  18360. begin
  18361. StartProgram(false);
  18362. Add([
  18363. '{$interfaces corba}',
  18364. 'type',
  18365. ' IUnknown = interface',
  18366. ' procedure Walk(i: longint);',
  18367. ' end;',
  18368. ' IBird = interface(IUnknown)',
  18369. ' procedure Walk(b: boolean); overload;',
  18370. ' procedure Fly(s: string);',
  18371. ' end;',
  18372. ' TObject = class',
  18373. ' end;',
  18374. ' TBird = class(TObject,IBird)',
  18375. ' procedure IBird.Fly = Move;',
  18376. ' procedure IBird.Walk = Hop;',
  18377. ' procedure Hop(i: longint);',
  18378. ' procedure Move(s: string);',
  18379. ' procedure Hop(b: boolean);',
  18380. ' end;',
  18381. 'procedure TBird.Move(s: string); begin end;',
  18382. 'procedure TBird.Hop(i: longint); begin end;',
  18383. 'procedure TBird.Hop(b: boolean); begin end;',
  18384. 'var',
  18385. ' BirdIntf: IBird;',
  18386. 'begin',
  18387. ' BirdIntf.Walk(3);',
  18388. ' BirdIntf.Walk(true);',
  18389. ' BirdIntf.Fly(''abc'');',
  18390. '']);
  18391. ConvertProgram;
  18392. CheckSource('TestClassInterface_MethodResolution',
  18393. LinesToStr([ // statements
  18394. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18395. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18396. 'rtl.createClass(this, "TObject", null, function () {',
  18397. ' this.$init = function () {',
  18398. ' };',
  18399. ' this.$final = function () {',
  18400. ' };',
  18401. '});',
  18402. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18403. ' this.Hop = function (i) {',
  18404. ' };',
  18405. ' this.Move = function (s) {',
  18406. ' };',
  18407. ' this.Hop$1 = function (b) {',
  18408. ' };',
  18409. ' rtl.addIntf(this, $mod.IBird, {',
  18410. ' Walk$1: "Hop$1",',
  18411. ' Fly: "Move",',
  18412. ' Walk: "Hop"',
  18413. ' });',
  18414. '});',
  18415. 'this.BirdIntf = null;',
  18416. '']),
  18417. LinesToStr([ // $mod.$main
  18418. '$mod.BirdIntf.Walk(3);',
  18419. '$mod.BirdIntf.Walk$1(true);',
  18420. '$mod.BirdIntf.Fly("abc");',
  18421. '']));
  18422. end;
  18423. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18424. begin
  18425. StartProgram(false);
  18426. Add([
  18427. '{$interfaces com}',
  18428. 'type',
  18429. ' IUnknown = interface',
  18430. ' function _AddRef: longint;',
  18431. ' procedure Walk;',
  18432. ' end;',
  18433. ' IBird = interface end;',
  18434. ' IDog = interface end;',
  18435. ' TObject = class(IBird,IDog)',
  18436. ' function _AddRef: longint; virtual; abstract;',
  18437. ' procedure Walk; virtual; abstract;',
  18438. ' end;',
  18439. ' TBird = class(IUnknown)',
  18440. ' end;',
  18441. 'begin',
  18442. '']);
  18443. ConvertProgram;
  18444. CheckSource('TestClassInterface_COM_AncestorLess',
  18445. LinesToStr([ // statements
  18446. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18447. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18448. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18449. 'rtl.createClass(this, "TObject", null, function () {',
  18450. ' this.$init = function () {',
  18451. ' };',
  18452. ' this.$final = function () {',
  18453. ' };',
  18454. ' rtl.addIntf(this, $mod.IBird);',
  18455. ' rtl.addIntf(this, $mod.IDog);',
  18456. '});',
  18457. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18458. ' rtl.addIntf(this, $mod.IUnknown);',
  18459. ' rtl.addIntf(this, $mod.IBird);',
  18460. ' rtl.addIntf(this, $mod.IDog);',
  18461. '});',
  18462. '']),
  18463. LinesToStr([ // $mod.$main
  18464. '']));
  18465. end;
  18466. procedure TTestModule.TestClassInterface_MethodOverride;
  18467. begin
  18468. StartProgram(false);
  18469. Add([
  18470. '{$interfaces corba}',
  18471. 'type',
  18472. ' IUnknown = interface',
  18473. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18474. ' procedure Go;',
  18475. ' end;',
  18476. ' TObject = class(IUnknown)',
  18477. ' procedure Go; virtual; abstract;',
  18478. ' end;',
  18479. ' TBird = class',
  18480. ' procedure Go; override;',
  18481. ' end;',
  18482. ' TCat = class(TObject)',
  18483. ' procedure Go; override;',
  18484. ' end;',
  18485. ' TDog = class(TObject, IUnknown)',
  18486. ' procedure Go; override;',
  18487. ' end;',
  18488. 'procedure TBird.Go; begin end;',
  18489. 'procedure TCat.Go; begin end;',
  18490. 'procedure TDog.Go; begin end;',
  18491. 'begin',
  18492. '']);
  18493. ConvertProgram;
  18494. CheckSource('TestClassInterface_MethodOverride',
  18495. LinesToStr([ // statements
  18496. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18497. 'rtl.createClass(this, "TObject", null, function () {',
  18498. ' this.$init = function () {',
  18499. ' };',
  18500. ' this.$final = function () {',
  18501. ' };',
  18502. ' rtl.addIntf(this, $mod.IUnknown);',
  18503. '});',
  18504. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18505. ' this.Go = function () {',
  18506. ' };',
  18507. ' rtl.addIntf(this, $mod.IUnknown);',
  18508. '});',
  18509. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18510. ' this.Go = function () {',
  18511. ' };',
  18512. ' rtl.addIntf(this, $mod.IUnknown);',
  18513. '});',
  18514. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18515. ' this.Go = function () {',
  18516. ' };',
  18517. ' rtl.addIntf(this, $mod.IUnknown);',
  18518. '});',
  18519. '']),
  18520. LinesToStr([ // $mod.$main
  18521. '']));
  18522. end;
  18523. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18524. begin
  18525. StartProgram(false);
  18526. Add([
  18527. '{$interfaces corba}',
  18528. 'type',
  18529. ' IUnknown = interface',
  18530. ' end;',
  18531. ' IBird = interface(IUnknown)',
  18532. ' procedure Fly(s: string);',
  18533. ' end;',
  18534. ' IEagle = interface(IBird)',
  18535. ' end;',
  18536. ' IDove = interface(IBird)',
  18537. ' end;',
  18538. ' ISwallow = interface(IBird)',
  18539. ' end;',
  18540. ' TObject = class',
  18541. ' end;',
  18542. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18543. ' procedure Fly(s: string); virtual; abstract;',
  18544. ' end;',
  18545. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18546. ' FBirdIntf: IBird;',
  18547. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18548. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18549. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18550. ' FDoveObj: TBird;',
  18551. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18552. ' function GetSwallowObj: TBird; virtual; abstract;',
  18553. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18554. ' end;',
  18555. 'begin',
  18556. '']);
  18557. ConvertProgram;
  18558. CheckSource('TestClassInterface_Corba_Delegation',
  18559. LinesToStr([ // statements
  18560. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18561. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18562. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18563. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18564. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18565. 'rtl.createClass(this, "TObject", null, function () {',
  18566. ' this.$init = function () {',
  18567. ' };',
  18568. ' this.$final = function () {',
  18569. ' };',
  18570. '});',
  18571. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18572. ' rtl.addIntf(this, $mod.IBird);',
  18573. ' rtl.addIntf(this, $mod.IEagle);',
  18574. ' rtl.addIntf(this, $mod.IDove);',
  18575. ' rtl.addIntf(this, $mod.ISwallow);',
  18576. '});',
  18577. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18578. ' this.$init = function () {',
  18579. ' $mod.TObject.$init.call(this);',
  18580. ' this.FBirdIntf = null;',
  18581. ' this.FDoveObj = null;',
  18582. ' };',
  18583. ' this.$final = function () {',
  18584. ' this.FBirdIntf = undefined;',
  18585. ' this.FDoveObj = undefined;',
  18586. ' $mod.TObject.$final.call(this);',
  18587. ' };',
  18588. ' this.$intfmaps = {',
  18589. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18590. ' return this.FBirdIntf;',
  18591. ' },',
  18592. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18593. ' return this.GetEagleIntf();',
  18594. ' },',
  18595. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18596. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18597. ' },',
  18598. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18599. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18600. ' }',
  18601. ' };',
  18602. '});',
  18603. '']),
  18604. LinesToStr([ // $mod.$main
  18605. '']));
  18606. end;
  18607. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18608. begin
  18609. StartProgram(false);
  18610. Add([
  18611. '{$interfaces corba}',
  18612. 'type',
  18613. ' IUnknown = interface',
  18614. ' end;',
  18615. ' IBird = interface(IUnknown)',
  18616. ' procedure Fly(s: string);',
  18617. ' end;',
  18618. ' IEagle = interface(IBird)',
  18619. ' end;',
  18620. ' IDove = interface(IBird)',
  18621. ' end;',
  18622. ' ISwallow = interface(IBird)',
  18623. ' end;',
  18624. ' TObject = class',
  18625. ' end;',
  18626. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18627. ' procedure Fly(s: string); virtual; abstract;',
  18628. ' end;',
  18629. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18630. ' private',
  18631. ' class var FBirdIntf: IBird;',
  18632. ' class var FDoveObj: TBird;',
  18633. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18634. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18635. ' protected',
  18636. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18637. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18638. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18639. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18640. ' end;',
  18641. 'begin',
  18642. '']);
  18643. ConvertProgram;
  18644. CheckSource('TestClassInterface_Corba_DelegationStatic',
  18645. LinesToStr([ // statements
  18646. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18647. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18648. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18649. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18650. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18651. 'rtl.createClass(this, "TObject", null, function () {',
  18652. ' this.$init = function () {',
  18653. ' };',
  18654. ' this.$final = function () {',
  18655. ' };',
  18656. '});',
  18657. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18658. ' rtl.addIntf(this, $mod.IBird);',
  18659. ' rtl.addIntf(this, $mod.IEagle);',
  18660. ' rtl.addIntf(this, $mod.IDove);',
  18661. ' rtl.addIntf(this, $mod.ISwallow);',
  18662. '});',
  18663. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18664. ' this.FBirdIntf = null;',
  18665. ' this.FDoveObj = null;',
  18666. ' this.$intfmaps = {',
  18667. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18668. ' return this.FBirdIntf;',
  18669. ' },',
  18670. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18671. ' return this.GetEagleIntf();',
  18672. ' },',
  18673. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18674. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18675. ' },',
  18676. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18677. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18678. ' }',
  18679. ' };',
  18680. '});',
  18681. '']),
  18682. LinesToStr([ // $mod.$main
  18683. '']));
  18684. end;
  18685. procedure TTestModule.TestClassInterface_Corba_Operators;
  18686. begin
  18687. StartProgram(false);
  18688. Add([
  18689. '{$interfaces corba}',
  18690. 'type',
  18691. ' IUnknown = interface',
  18692. ' end;',
  18693. ' IBird = interface(IUnknown)',
  18694. ' function GetItems(Index: longint): longint;',
  18695. ' procedure SetItems(Index: longint; Value: longint);',
  18696. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18697. ' end;',
  18698. ' TObject = class',
  18699. ' end;',
  18700. ' TBird = class(TObject,IBird)',
  18701. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18702. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18703. ' end;',
  18704. 'var',
  18705. ' IntfVar: IBird = nil;',
  18706. ' IntfVar2: IBird;',
  18707. ' ObjVar: TBird;',
  18708. ' v: JSValue;',
  18709. 'begin',
  18710. ' IntfVar:=nil;',
  18711. ' IntfVar[3]:=IntfVar[4];',
  18712. ' if Assigned(IntfVar) then ;',
  18713. ' IntfVar:=IntfVar2;',
  18714. ' IntfVar:=ObjVar;',
  18715. ' if IntfVar=IntfVar2 then ;',
  18716. ' if IntfVar<>IntfVar2 then ;',
  18717. ' if IntfVar is IBird then ;',
  18718. ' if IntfVar is TBird then ;',
  18719. ' if ObjVar is IBird then ;',
  18720. ' IntfVar:=IntfVar2 as IBird;',
  18721. ' ObjVar:=IntfVar2 as TBird;',
  18722. ' IntfVar:=ObjVar as IBird;',
  18723. ' IntfVar:=IBird(IntfVar2);',
  18724. ' ObjVar:=TBird(IntfVar);',
  18725. ' IntfVar:=IBird(ObjVar);',
  18726. ' v:=IntfVar;',
  18727. ' IntfVar:=IBird(v);',
  18728. ' if v is IBird then ;',
  18729. ' v:=JSValue(IntfVar);',
  18730. ' v:=IBird;',
  18731. '']);
  18732. ConvertProgram;
  18733. CheckSource('TestClassInterface_Corba_Operators',
  18734. LinesToStr([ // statements
  18735. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18736. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  18737. 'rtl.createClass(this, "TObject", null, function () {',
  18738. ' this.$init = function () {',
  18739. ' };',
  18740. ' this.$final = function () {',
  18741. ' };',
  18742. '});',
  18743. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18744. ' rtl.addIntf(this, $mod.IBird);',
  18745. '});',
  18746. 'this.IntfVar = null;',
  18747. 'this.IntfVar2 = null;',
  18748. 'this.ObjVar = null;',
  18749. 'this.v = undefined;',
  18750. '']),
  18751. LinesToStr([ // $mod.$main
  18752. '$mod.IntfVar = null;',
  18753. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18754. 'if ($mod.IntfVar != null) ;',
  18755. '$mod.IntfVar = $mod.IntfVar2;',
  18756. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18757. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18758. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18759. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18760. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18761. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18762. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18763. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18764. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18765. '$mod.IntfVar = $mod.IntfVar2;',
  18766. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18767. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18768. '$mod.v = $mod.IntfVar;',
  18769. '$mod.IntfVar = rtl.getObject($mod.v);',
  18770. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18771. '$mod.v = $mod.IntfVar;',
  18772. '$mod.v = $mod.IBird;',
  18773. '']));
  18774. end;
  18775. procedure TTestModule.TestClassInterface_Corba_Args;
  18776. begin
  18777. StartProgram(false);
  18778. Add([
  18779. '{$interfaces corba}',
  18780. 'type',
  18781. ' IUnknown = interface',
  18782. ' end;',
  18783. ' IBird = interface(IUnknown)',
  18784. ' end;',
  18785. ' TObject = class',
  18786. ' end;',
  18787. ' TBird = class(TObject,IBird)',
  18788. ' end;',
  18789. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18790. 'begin',
  18791. ' DoIt(i,i,i);',
  18792. 'end;',
  18793. 'procedure Change(var i: IBird; out j: IBird);',
  18794. 'begin',
  18795. ' DoIt(i,i,i);',
  18796. ' Change(i,i);',
  18797. 'end;',
  18798. 'var',
  18799. ' i: IBird;',
  18800. ' o: TBird;',
  18801. 'begin',
  18802. ' DoIt(i,i,i);',
  18803. ' Change(i,i);',
  18804. ' DoIt(o,o,o);',
  18805. '']);
  18806. ConvertProgram;
  18807. CheckSource('TestClassInterface_Corba_Args',
  18808. LinesToStr([ // statements
  18809. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18810. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18811. 'rtl.createClass(this, "TObject", null, function () {',
  18812. ' this.$init = function () {',
  18813. ' };',
  18814. ' this.$final = function () {',
  18815. ' };',
  18816. '});',
  18817. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18818. ' rtl.addIntf(this, $mod.IBird);',
  18819. '});',
  18820. 'this.DoIt = function (u, i, j) {',
  18821. ' $mod.DoIt({',
  18822. ' get: function () {',
  18823. ' return i;',
  18824. ' },',
  18825. ' set: function (v) {',
  18826. ' i = v;',
  18827. ' }',
  18828. ' }, i, i);',
  18829. '};',
  18830. 'this.Change = function (i, j) {',
  18831. ' $mod.DoIt(i, i.get(), i.get());',
  18832. ' $mod.Change(i, i);',
  18833. '};',
  18834. 'this.i = null;',
  18835. 'this.o = null;',
  18836. '']),
  18837. LinesToStr([ // $mod.$main
  18838. '$mod.DoIt({',
  18839. ' p: $mod,',
  18840. ' get: function () {',
  18841. ' return this.p.i;',
  18842. ' },',
  18843. ' set: function (v) {',
  18844. ' this.p.i = v;',
  18845. ' }',
  18846. '}, $mod.i, $mod.i);',
  18847. '$mod.Change({',
  18848. ' p: $mod,',
  18849. ' get: function () {',
  18850. ' return this.p.i;',
  18851. ' },',
  18852. ' set: function (v) {',
  18853. ' this.p.i = v;',
  18854. ' }',
  18855. '}, {',
  18856. ' p: $mod,',
  18857. ' get: function () {',
  18858. ' return this.p.i;',
  18859. ' },',
  18860. ' set: function (v) {',
  18861. ' this.p.i = v;',
  18862. ' }',
  18863. '});',
  18864. '$mod.DoIt({',
  18865. ' p: $mod,',
  18866. ' get: function () {',
  18867. ' return this.p.o;',
  18868. ' },',
  18869. ' set: function (v) {',
  18870. ' this.p.o = v;',
  18871. ' }',
  18872. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18873. '']));
  18874. end;
  18875. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18876. begin
  18877. StartProgram(false);
  18878. Add([
  18879. '{$interfaces corba}',
  18880. 'type',
  18881. ' IUnknown = interface end;',
  18882. ' TObject = class',
  18883. ' Id: longint;',
  18884. ' end;',
  18885. ' IEnumerator = interface(IUnknown)',
  18886. ' function GetCurrent: TObject;',
  18887. ' function MoveNext: Boolean;',
  18888. ' property Current: TObject read GetCurrent;',
  18889. ' end;',
  18890. ' IEnumerable = interface(IUnknown)',
  18891. ' function GetEnumerator: IEnumerator;',
  18892. ' end;',
  18893. 'var',
  18894. ' o: TObject;',
  18895. ' i: IEnumerable;',
  18896. 'begin',
  18897. ' for o in i do o.Id:=3;',
  18898. '']);
  18899. ConvertProgram;
  18900. CheckSource('TestClassInterface_Corba_ForIn',
  18901. LinesToStr([ // statements
  18902. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18903. 'rtl.createClass(this, "TObject", null, function () {',
  18904. ' this.$init = function () {',
  18905. ' this.Id = 0;',
  18906. ' };',
  18907. ' this.$final = function () {',
  18908. ' };',
  18909. '});',
  18910. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  18911. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  18912. 'this.o = null;',
  18913. 'this.i = null;',
  18914. '']),
  18915. LinesToStr([ // $mod.$main
  18916. 'var $in = $mod.i.GetEnumerator();',
  18917. 'while ($in.MoveNext()) {',
  18918. ' $mod.o = $in.GetCurrent();',
  18919. ' $mod.o.Id = 3;',
  18920. '};',
  18921. '']));
  18922. end;
  18923. procedure TTestModule.TestClassInterface_COM_AssignVar;
  18924. begin
  18925. StartProgram(false);
  18926. Add([
  18927. '{$interfaces com}',
  18928. 'type',
  18929. ' IUnknown = interface',
  18930. ' function _AddRef: longint;',
  18931. ' function _Release: longint;',
  18932. ' end;',
  18933. ' TObject = class(IUnknown)',
  18934. ' function _AddRef: longint; virtual; abstract;',
  18935. ' function _Release: longint; virtual; abstract;',
  18936. ' end;',
  18937. 'var',
  18938. ' i: IUnknown;',
  18939. 'procedure DoGlobal(o: TObject);',
  18940. 'begin',
  18941. ' i:=nil;',
  18942. ' i:=o;',
  18943. ' i:=i;',
  18944. 'end;',
  18945. 'procedure DoLocal(o: TObject);',
  18946. 'const k: IUnknown = nil;',
  18947. 'var j: IUnknown;',
  18948. 'begin',
  18949. ' k:=o;',
  18950. ' k:=i;',
  18951. ' j:=o;',
  18952. ' j:=i;',
  18953. 'end;',
  18954. 'var o: TObject;',
  18955. 'begin',
  18956. ' i:=nil;',
  18957. ' i:=o;',
  18958. '']);
  18959. ConvertProgram;
  18960. CheckSource('TestClassInterface_COM_AssignVar',
  18961. LinesToStr([ // statements
  18962. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18963. 'rtl.createClass(this, "TObject", null, function () {',
  18964. ' this.$init = function () {',
  18965. ' };',
  18966. ' this.$final = function () {',
  18967. ' };',
  18968. ' rtl.addIntf(this, $mod.IUnknown);',
  18969. '});',
  18970. 'this.i = null;',
  18971. 'this.DoGlobal = function (o) {',
  18972. ' rtl.setIntfP($mod, "i", null);',
  18973. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18974. ' rtl.setIntfP($mod, "i", $mod.i);',
  18975. '};',
  18976. 'var k = null;',
  18977. 'this.DoLocal = function (o) {',
  18978. ' var j = null;',
  18979. ' try{',
  18980. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18981. ' k = rtl.setIntfL(k, $mod.i);',
  18982. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18983. ' j = rtl.setIntfL(j, $mod.i);',
  18984. ' }finally{',
  18985. ' rtl._Release(j);',
  18986. ' };',
  18987. '};',
  18988. 'this.o = null;',
  18989. '']),
  18990. LinesToStr([ // $mod.$main
  18991. 'rtl.setIntfP($mod, "i", null);',
  18992. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18993. '']));
  18994. end;
  18995. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18996. begin
  18997. StartProgram(false);
  18998. Add([
  18999. '{$interfaces com}',
  19000. 'type',
  19001. ' IUnknown = interface',
  19002. ' function _AddRef: longint;',
  19003. ' function _Release: longint;',
  19004. ' end;',
  19005. ' TObject = class(IUnknown)',
  19006. ' function _AddRef: longint; virtual; abstract;',
  19007. ' function _Release: longint; virtual; abstract;',
  19008. ' end;',
  19009. 'procedure DoDefault(i, j: IUnknown);',
  19010. 'begin',
  19011. ' i:=nil;',
  19012. ' i:=j;',
  19013. 'end;',
  19014. 'begin',
  19015. '']);
  19016. ConvertProgram;
  19017. CheckSource('TestClassInterface_COM_AssignArg',
  19018. LinesToStr([ // statements
  19019. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19020. 'rtl.createClass(this, "TObject", null, function () {',
  19021. ' this.$init = function () {',
  19022. ' };',
  19023. ' this.$final = function () {',
  19024. ' };',
  19025. ' rtl.addIntf(this, $mod.IUnknown);',
  19026. '});',
  19027. 'this.DoDefault = function (i, j) {',
  19028. ' rtl._AddRef(i);',
  19029. ' try {',
  19030. ' i = rtl.setIntfL(i, null);',
  19031. ' i = rtl.setIntfL(i, j);',
  19032. ' } finally {',
  19033. ' rtl._Release(i);',
  19034. ' };',
  19035. '};',
  19036. '']),
  19037. LinesToStr([ // $mod.$main
  19038. '']));
  19039. end;
  19040. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19041. begin
  19042. StartProgram(false);
  19043. Add([
  19044. '{$interfaces com}',
  19045. 'type',
  19046. ' IUnknown = interface',
  19047. ' function _AddRef: longint;',
  19048. ' function _Release: longint;',
  19049. ' end;',
  19050. ' TObject = class(IUnknown)',
  19051. ' function _AddRef: longint; virtual; abstract;',
  19052. ' function _Release: longint; virtual; abstract;',
  19053. ' end;',
  19054. 'function DoDefault(i: IUnknown): IUnknown;',
  19055. 'begin',
  19056. ' Result:=i;',
  19057. ' if Result<>nil then exit;',
  19058. 'end;',
  19059. 'begin',
  19060. '']);
  19061. ConvertProgram;
  19062. CheckSource('TestClassInterface_COM_FunctionResult',
  19063. LinesToStr([ // statements
  19064. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19065. 'rtl.createClass(this, "TObject", null, function () {',
  19066. ' this.$init = function () {',
  19067. ' };',
  19068. ' this.$final = function () {',
  19069. ' };',
  19070. ' rtl.addIntf(this, $mod.IUnknown);',
  19071. '});',
  19072. 'this.DoDefault = function (i) {',
  19073. ' var Result = null;',
  19074. ' var $ok = false;',
  19075. ' try {',
  19076. ' Result = rtl.setIntfL(Result, i);',
  19077. ' if(Result !== null){',
  19078. ' $ok = true;',
  19079. ' return Result;',
  19080. ' };',
  19081. ' $ok = true;',
  19082. ' } finally {',
  19083. ' if(!$ok) rtl._Release(Result);',
  19084. ' };',
  19085. ' return Result;',
  19086. '};',
  19087. '']),
  19088. LinesToStr([ // $mod.$main
  19089. '']));
  19090. end;
  19091. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19092. begin
  19093. StartProgram(false);
  19094. Add([
  19095. '{$interfaces com}',
  19096. 'type',
  19097. ' IUnknown = interface',
  19098. ' function _AddRef: longint;',
  19099. ' function _Release: longint;',
  19100. ' end;',
  19101. ' TObject = class(IUnknown)',
  19102. ' function _AddRef: longint; virtual; abstract;',
  19103. ' function _Release: longint; virtual; abstract;',
  19104. ' function GetIntf: IUnknown; virtual;',
  19105. ' end;',
  19106. ' TMouse = class',
  19107. ' function GetIntf: IUnknown; override;',
  19108. ' end;',
  19109. 'function TObject.GetIntf: IUnknown; begin end;',
  19110. 'function TMouse.GetIntf: IUnknown;',
  19111. 'var i: IUnknown;',
  19112. 'begin',
  19113. ' inherited;',
  19114. ' inherited GetIntf;',
  19115. ' inherited GetIntf();',
  19116. ' Result:=inherited GetIntf;',
  19117. ' Result:=inherited GetIntf();',
  19118. ' i:=inherited GetIntf;',
  19119. ' i:=inherited GetIntf();',
  19120. 'end;',
  19121. 'begin',
  19122. '']);
  19123. ConvertProgram;
  19124. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19125. LinesToStr([ // statements
  19126. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19127. 'rtl.createClass(this, "TObject", null, function () {',
  19128. ' this.$init = function () {',
  19129. ' };',
  19130. ' this.$final = function () {',
  19131. ' };',
  19132. ' this.GetIntf = function () {',
  19133. ' var Result = null;',
  19134. ' return Result;',
  19135. ' };',
  19136. ' rtl.addIntf(this, $mod.IUnknown);',
  19137. '});',
  19138. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19139. ' this.GetIntf = function () {',
  19140. ' var Result = null;',
  19141. ' var i = null;',
  19142. ' var $ir = rtl.createIntfRefs();',
  19143. ' var $ok = false;',
  19144. ' try {',
  19145. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19146. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19147. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19148. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19149. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19150. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19151. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19152. ' $ok = true;',
  19153. ' } finally {',
  19154. ' $ir.free();',
  19155. ' rtl._Release(i);',
  19156. ' if (!$ok) rtl._Release(Result);',
  19157. ' };',
  19158. ' return Result;',
  19159. ' };',
  19160. ' rtl.addIntf(this, $mod.IUnknown);',
  19161. '});',
  19162. '']),
  19163. LinesToStr([ // $mod.$main
  19164. '']));
  19165. end;
  19166. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19167. begin
  19168. StartProgram(false);
  19169. Add([
  19170. '{$interfaces com}',
  19171. 'type',
  19172. ' IUnknown = interface',
  19173. ' function _AddRef: longint;',
  19174. ' function _Release: longint;',
  19175. ' end;',
  19176. ' TObject = class(IUnknown)',
  19177. ' function _AddRef: longint; virtual; abstract;',
  19178. ' function _Release: longint; virtual; abstract;',
  19179. ' end;',
  19180. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19181. 'begin',
  19182. ' if i is IUnknown then ;',
  19183. ' if o is IUnknown then ;',
  19184. ' if i is TObject then ;',
  19185. ' i:=j as IUnknown;',
  19186. ' i:=o as IUnknown;',
  19187. ' o:=j as TObject;',
  19188. ' i:=IUnknown(j);',
  19189. ' i:=IUnknown(o);',
  19190. ' o:=TObject(i);',
  19191. 'end;',
  19192. 'begin',
  19193. '']);
  19194. ConvertProgram;
  19195. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19196. LinesToStr([ // statements
  19197. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19198. 'rtl.createClass(this, "TObject", null, function () {',
  19199. ' this.$init = function () {',
  19200. ' };',
  19201. ' this.$final = function () {',
  19202. ' };',
  19203. ' rtl.addIntf(this, $mod.IUnknown);',
  19204. '});',
  19205. 'this.DoDefault = function (i, j, o) {',
  19206. ' rtl._AddRef(i);',
  19207. ' try {',
  19208. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19209. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19210. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19211. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19212. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19213. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19214. ' i = rtl.setIntfL(i, j);',
  19215. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19216. ' o = rtl.intfToClass(i, $mod.TObject);',
  19217. ' } finally {',
  19218. ' rtl._Release(i);',
  19219. ' };',
  19220. '};',
  19221. '']),
  19222. LinesToStr([ // $mod.$main
  19223. '']));
  19224. end;
  19225. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19226. begin
  19227. StartProgram(false);
  19228. Add([
  19229. '{$interfaces com}',
  19230. 'type',
  19231. ' IUnknown = interface',
  19232. ' function _AddRef: longint;',
  19233. ' function _Release: longint;',
  19234. ' end;',
  19235. ' TObject = class(IUnknown)',
  19236. ' function _AddRef: longint; virtual; abstract;',
  19237. ' function _Release: longint; virtual; abstract;',
  19238. ' end;',
  19239. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19240. 'var o: TObject;',
  19241. 'begin',
  19242. ' DoIt(v,v,v,v);',
  19243. ' DoIt(o,o,k,k);',
  19244. 'end;',
  19245. 'procedure DoSome;',
  19246. 'var v: IUnknown;',
  19247. 'begin',
  19248. ' DoIt(v,v,v,v);',
  19249. 'end;',
  19250. 'var i: IUnknown;',
  19251. 'begin',
  19252. ' DoIt(i,i,i,i);',
  19253. '']);
  19254. ConvertProgram;
  19255. CheckSource('TestClassInterface_COM_PassAsArg',
  19256. LinesToStr([ // statements
  19257. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19258. 'rtl.createClass(this, "TObject", null, function () {',
  19259. ' this.$init = function () {',
  19260. ' };',
  19261. ' this.$final = function () {',
  19262. ' };',
  19263. ' rtl.addIntf(this, $mod.IUnknown);',
  19264. '});',
  19265. 'this.DoIt = function (v, j, k, l) {',
  19266. ' var o = null;',
  19267. ' var $ir = rtl.createIntfRefs();',
  19268. ' rtl._AddRef(v);',
  19269. ' try {',
  19270. ' $mod.DoIt(v, v, {',
  19271. ' get: function () {',
  19272. ' return v;',
  19273. ' },',
  19274. ' set: function (w) {',
  19275. ' v = rtl.setIntfL(v, w);',
  19276. ' }',
  19277. ' }, {',
  19278. ' get: function () {',
  19279. ' return v;',
  19280. ' },',
  19281. ' set: function (w) {',
  19282. ' v = rtl.setIntfL(v, w);',
  19283. ' }',
  19284. ' });',
  19285. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19286. ' } finally {',
  19287. ' $ir.free();',
  19288. ' rtl._Release(v);',
  19289. ' };',
  19290. '};',
  19291. 'this.DoSome = function () {',
  19292. ' var v = null;',
  19293. ' try {',
  19294. ' $mod.DoIt(v, v, {',
  19295. ' get: function () {',
  19296. ' return v;',
  19297. ' },',
  19298. ' set: function (w) {',
  19299. ' v = rtl.setIntfL(v, w);',
  19300. ' }',
  19301. ' }, {',
  19302. ' get: function () {',
  19303. ' return v;',
  19304. ' },',
  19305. ' set: function (w) {',
  19306. ' v = rtl.setIntfL(v, w);',
  19307. ' }',
  19308. ' });',
  19309. ' } finally {',
  19310. ' rtl._Release(v);',
  19311. ' };',
  19312. '};',
  19313. 'this.i = null;',
  19314. '']),
  19315. LinesToStr([ // $mod.$main
  19316. '$mod.DoIt($mod.i, $mod.i, {',
  19317. ' p: $mod,',
  19318. ' get: function () {',
  19319. ' return this.p.i;',
  19320. ' },',
  19321. ' set: function (v) {',
  19322. ' rtl.setIntfP(this.p, "i", v);',
  19323. ' }',
  19324. '}, {',
  19325. ' p: $mod,',
  19326. ' get: function () {',
  19327. ' return this.p.i;',
  19328. ' },',
  19329. ' set: function (v) {',
  19330. ' rtl.setIntfP(this.p, "i", v);',
  19331. ' }',
  19332. '});',
  19333. '']));
  19334. end;
  19335. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19336. begin
  19337. StartProgram(false);
  19338. Add([
  19339. '{$interfaces com}',
  19340. 'type',
  19341. ' IUnknown = interface',
  19342. ' function _AddRef: longint;',
  19343. ' function _Release: longint;',
  19344. ' end;',
  19345. ' TObject = class(IUnknown)',
  19346. ' function _AddRef: longint; virtual; abstract;',
  19347. ' function _Release: longint; virtual; abstract;',
  19348. ' end;',
  19349. 'procedure DoIt(out i);',
  19350. 'begin end;',
  19351. 'procedure DoSome;',
  19352. 'var v: IUnknown;',
  19353. 'begin',
  19354. ' DoIt(v);',
  19355. 'end;',
  19356. 'function GetIt: IUnknown;',
  19357. 'begin',
  19358. ' DoIt(Result);',
  19359. 'end;',
  19360. 'var i: IUnknown;',
  19361. 'begin',
  19362. ' DoIt(i);',
  19363. '']);
  19364. ConvertProgram;
  19365. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19366. LinesToStr([ // statements
  19367. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19368. 'rtl.createClass(this, "TObject", null, function () {',
  19369. ' this.$init = function () {',
  19370. ' };',
  19371. ' this.$final = function () {',
  19372. ' };',
  19373. ' rtl.addIntf(this, $mod.IUnknown);',
  19374. '});',
  19375. 'this.DoIt = function (i) {',
  19376. '};',
  19377. 'this.DoSome = function () {',
  19378. ' var v = null;',
  19379. ' try {',
  19380. ' $mod.DoIt({',
  19381. ' get: function () {',
  19382. ' return v;',
  19383. ' },',
  19384. ' set: function (w) {',
  19385. ' v = w;',
  19386. ' }',
  19387. ' });',
  19388. ' } finally {',
  19389. ' rtl._Release(v);',
  19390. ' };',
  19391. '};',
  19392. 'this.GetIt = function () {',
  19393. ' var Result = null;',
  19394. ' var $ok = false;',
  19395. ' try {',
  19396. ' $mod.DoIt({',
  19397. ' get: function () {',
  19398. ' return Result;',
  19399. ' },',
  19400. ' set: function (v) {',
  19401. ' Result = v;',
  19402. ' }',
  19403. ' });',
  19404. ' $ok = true;',
  19405. ' } finally {',
  19406. ' if (!$ok) rtl._Release(Result);',
  19407. ' };',
  19408. ' return Result;',
  19409. '};',
  19410. 'this.i = null;',
  19411. '']),
  19412. LinesToStr([ // $mod.$main
  19413. 'try {',
  19414. ' $mod.DoIt({',
  19415. ' p: $mod,',
  19416. ' get: function () {',
  19417. ' return this.p.i;',
  19418. ' },',
  19419. ' set: function (v) {',
  19420. ' this.p.i = v;',
  19421. ' }',
  19422. ' });',
  19423. '} finally {',
  19424. ' rtl._Release($mod.i);',
  19425. '};',
  19426. '']));
  19427. end;
  19428. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19429. begin
  19430. StartProgram(false);
  19431. Add([
  19432. '{$interfaces com}',
  19433. 'type',
  19434. ' IUnknown = interface',
  19435. ' function _AddRef: longint;',
  19436. ' function _Release: longint;',
  19437. ' end;',
  19438. ' TObject = class(IUnknown)',
  19439. ' function _AddRef: longint; virtual; abstract;',
  19440. ' function _Release: longint; virtual; abstract;',
  19441. ' end;',
  19442. 'function GetIt: IUnknown;',
  19443. 'begin',
  19444. 'end;',
  19445. 'procedure DoSome;',
  19446. 'var v: IUnknown;',
  19447. ' i: longint;',
  19448. 'begin',
  19449. ' v:=GetIt;',
  19450. ' v:=GetIt();',
  19451. ' GetIt()._AddRef;',
  19452. ' i:=GetIt()._AddRef;',
  19453. 'end;',
  19454. 'var v: IUnknown;',
  19455. ' i: longint;',
  19456. 'begin',
  19457. ' v:=GetIt;',
  19458. ' v:=GetIt();',
  19459. ' GetIt()._AddRef;',
  19460. ' i:=GetIt()._AddRef;',
  19461. '']);
  19462. ConvertProgram;
  19463. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19464. LinesToStr([ // statements
  19465. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19466. 'rtl.createClass(this, "TObject", null, function () {',
  19467. ' this.$init = function () {',
  19468. ' };',
  19469. ' this.$final = function () {',
  19470. ' };',
  19471. ' rtl.addIntf(this, $mod.IUnknown);',
  19472. '});',
  19473. 'this.GetIt = function () {',
  19474. ' var Result = null;',
  19475. ' return Result;',
  19476. '};',
  19477. 'this.DoSome = function () {',
  19478. ' var v = null;',
  19479. ' var i = 0;',
  19480. ' var $ir = rtl.createIntfRefs();',
  19481. ' try {',
  19482. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19483. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19484. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19485. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19486. ' } finally {',
  19487. ' $ir.free();',
  19488. ' rtl._Release(v);',
  19489. ' };',
  19490. '};',
  19491. 'this.v = null;',
  19492. 'this.i = 0;',
  19493. '']),
  19494. LinesToStr([ // $mod.$main
  19495. 'var $ir = rtl.createIntfRefs();',
  19496. 'try {',
  19497. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19498. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19499. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19500. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19501. '} finally {',
  19502. ' $ir.free();',
  19503. '};',
  19504. '']));
  19505. end;
  19506. procedure TTestModule.TestClassInterface_COM_Property;
  19507. begin
  19508. StartProgram(false);
  19509. Add([
  19510. '{$interfaces com}',
  19511. 'type',
  19512. ' IUnknown = interface',
  19513. ' function _AddRef: longint;',
  19514. ' function _Release: longint;',
  19515. ' end;',
  19516. ' TObject = class(IUnknown)',
  19517. ' FAnt: IUnknown;',
  19518. ' function _AddRef: longint; virtual; abstract;',
  19519. ' function _Release: longint; virtual; abstract;',
  19520. ' function GetBird: IUnknown; virtual; abstract;',
  19521. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19522. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19523. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19524. ' property Ant: IUnknown read FAnt write FAnt;',
  19525. ' property Bird: IUnknown read GetBird write SetBird;',
  19526. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19527. ' end;',
  19528. 'procedure DoIt;',
  19529. 'var',
  19530. ' o: TObject;',
  19531. ' v: IUnknown;',
  19532. 'begin',
  19533. ' v:=o.Ant;',
  19534. ' o.Ant:=v;',
  19535. ' o.Ant:=o.Ant;',
  19536. ' v:=o.Bird;',
  19537. ' o.Bird:=v;',
  19538. ' o.Bird:=o.Bird;',
  19539. ' v:=o.Items[1];',
  19540. ' o.Items[2]:=v;',
  19541. ' o.Items[3]:=o.Items[4];',
  19542. ' v:=o[5];',
  19543. ' o[6]:=v;',
  19544. ' o[7]:=o[8];',
  19545. 'end;',
  19546. 'begin',
  19547. '']);
  19548. ConvertProgram;
  19549. CheckSource('TestClassInterface_COM_Property',
  19550. LinesToStr([ // statements
  19551. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19552. 'rtl.createClass(this, "TObject", null, function () {',
  19553. ' this.$init = function () {',
  19554. ' this.FAnt = null;',
  19555. ' };',
  19556. ' this.$final = function () {',
  19557. ' this.FAnt = undefined;',
  19558. ' };',
  19559. ' rtl.addIntf(this, $mod.IUnknown);',
  19560. '});',
  19561. 'this.DoIt = function () {',
  19562. ' var o = null;',
  19563. ' var v = null;',
  19564. ' var $ir = rtl.createIntfRefs();',
  19565. ' try {',
  19566. ' v = rtl.setIntfL(v, o.FAnt);',
  19567. ' rtl.setIntfP(o, "FAnt", v);',
  19568. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19569. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19570. ' o.SetBird(v);',
  19571. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19572. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19573. ' o.SetItems(2, v);',
  19574. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19575. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19576. ' o.SetItems(6, v);',
  19577. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19578. ' } finally {',
  19579. ' $ir.free();',
  19580. ' rtl._Release(v);',
  19581. ' };',
  19582. '};',
  19583. '']),
  19584. LinesToStr([ // $mod.$main
  19585. '']));
  19586. end;
  19587. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19588. begin
  19589. StartProgram(false);
  19590. Add([
  19591. '{$interfaces com}',
  19592. 'type',
  19593. ' IUnknown = interface',
  19594. ' function _AddRef: longint;',
  19595. ' function _Release: longint;',
  19596. ' function GetBird: IUnknown;',
  19597. ' procedure SetBird(Value: IUnknown);',
  19598. ' function GetItems(Index: longint): IUnknown;',
  19599. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19600. ' property Bird: IUnknown read GetBird write SetBird;',
  19601. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19602. ' end;',
  19603. ' TObject = class(IUnknown)',
  19604. ' function _AddRef: longint; virtual; abstract;',
  19605. ' function _Release: longint; virtual; abstract;',
  19606. ' function GetBird: IUnknown; virtual; abstract;',
  19607. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19608. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19609. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19610. ' end;',
  19611. 'procedure DoIt;',
  19612. 'var',
  19613. ' o: TObject;',
  19614. ' v: IUnknown;',
  19615. 'begin',
  19616. ' v:=v.Items[1];',
  19617. ' v.Items[2]:=v;',
  19618. ' v.Items[3]:=v.Items[4];',
  19619. ' v:=v[5];',
  19620. ' v[6]:=v;',
  19621. ' v[7]:=v[8];',
  19622. ' v[9].Bird.Bird:=v;',
  19623. ' v:=v.Bird[10].Bird',
  19624. 'end;',
  19625. 'begin',
  19626. '']);
  19627. ConvertProgram;
  19628. CheckSource('TestClassInterface_COM_IntfProperty',
  19629. LinesToStr([ // statements
  19630. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19631. ' "_AddRef",',
  19632. ' "_Release",',
  19633. ' "GetBird",',
  19634. ' "SetBird",',
  19635. ' "GetItems",',
  19636. ' "SetItems"',
  19637. '], null);',
  19638. 'rtl.createClass(this, "TObject", null, function () {',
  19639. ' this.$init = function () {',
  19640. ' };',
  19641. ' this.$final = function () {',
  19642. ' };',
  19643. ' rtl.addIntf(this, $mod.IUnknown);',
  19644. '});',
  19645. 'this.DoIt = function () {',
  19646. ' var o = null;',
  19647. ' var v = null;',
  19648. ' var $ir = rtl.createIntfRefs();',
  19649. ' try {',
  19650. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19651. ' v.SetItems(2, v);',
  19652. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19653. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19654. ' v.SetItems(6, v);',
  19655. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19656. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19657. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19658. ' } finally {',
  19659. ' $ir.free();',
  19660. ' rtl._Release(v);',
  19661. ' };',
  19662. '};',
  19663. '']),
  19664. LinesToStr([ // $mod.$main
  19665. '']));
  19666. end;
  19667. procedure TTestModule.TestClassInterface_COM_Delegation;
  19668. begin
  19669. StartProgram(false);
  19670. Add([
  19671. '{$interfaces com}',
  19672. 'type',
  19673. ' IUnknown = interface',
  19674. ' function _AddRef: longint;',
  19675. ' function _Release: longint;',
  19676. ' end;',
  19677. ' IBird = interface(IUnknown)',
  19678. ' procedure Fly(s: string);',
  19679. ' end;',
  19680. ' IEagle = interface(IBird) end;',
  19681. ' IDove = interface(IBird) end;',
  19682. ' ISwallow = interface(IBird) end;',
  19683. ' TObject = class',
  19684. ' end;',
  19685. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19686. ' function _AddRef: longint; virtual; abstract;',
  19687. ' function _Release: longint; virtual; abstract;',
  19688. ' procedure Fly(s: string); virtual; abstract;',
  19689. ' end;',
  19690. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19691. ' function _AddRef: longint; virtual; abstract;',
  19692. ' function _Release: longint; virtual; abstract;',
  19693. ' FBirdIntf: IBird;',
  19694. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19695. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19696. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19697. ' FDoveObj: TBird;',
  19698. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19699. ' function GetSwallowObj: TBird; virtual; abstract;',
  19700. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19701. ' end;',
  19702. 'begin',
  19703. '']);
  19704. ConvertProgram;
  19705. CheckSource('TestClassInterface_COM_Delegation',
  19706. LinesToStr([ // statements
  19707. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19708. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  19709. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  19710. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  19711. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  19712. 'rtl.createClass(this, "TObject", null, function () {',
  19713. ' this.$init = function () {',
  19714. ' };',
  19715. ' this.$final = function () {',
  19716. ' };',
  19717. '});',
  19718. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19719. ' rtl.addIntf(this, $mod.IBird);',
  19720. ' rtl.addIntf(this, $mod.IEagle);',
  19721. ' rtl.addIntf(this, $mod.IDove);',
  19722. ' rtl.addIntf(this, $mod.ISwallow);',
  19723. '});',
  19724. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19725. ' this.$init = function () {',
  19726. ' $mod.TObject.$init.call(this);',
  19727. ' this.FBirdIntf = null;',
  19728. ' this.FDoveObj = null;',
  19729. ' };',
  19730. ' this.$final = function () {',
  19731. ' this.FBirdIntf = undefined;',
  19732. ' this.FDoveObj = undefined;',
  19733. ' $mod.TObject.$final.call(this);',
  19734. ' };',
  19735. ' this.$intfmaps = {',
  19736. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19737. ' return rtl._AddRef(this.FBirdIntf);',
  19738. ' },',
  19739. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19740. ' return this.GetEagleIntf();',
  19741. ' },',
  19742. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19743. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19744. ' },',
  19745. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19746. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19747. ' }',
  19748. ' };',
  19749. '});',
  19750. '']),
  19751. LinesToStr([ // $mod.$main
  19752. '']));
  19753. end;
  19754. procedure TTestModule.TestClassInterface_COM_With;
  19755. begin
  19756. StartProgram(false);
  19757. Add([
  19758. '{$interfaces com}',
  19759. 'type',
  19760. ' IUnknown = interface',
  19761. ' function _AddRef: longint;',
  19762. ' function _Release: longint;',
  19763. ' function GetAnt: IUnknown;',
  19764. ' property Ant: IUnknown read GetAnt;',
  19765. ' end;',
  19766. ' TObject = class(IUnknown)',
  19767. ' function _AddRef: longint; virtual; abstract;',
  19768. ' function _Release: longint; virtual; abstract;',
  19769. ' function GetAnt: IUnknown; virtual; abstract;',
  19770. ' property Ant: IUnknown read GetAnt;',
  19771. ' end;',
  19772. 'procedure DoIt;',
  19773. 'var',
  19774. ' i: IUnknown;',
  19775. 'begin',
  19776. ' with i do ',
  19777. ' GetAnt;',
  19778. ' with i.Ant, Ant do ',
  19779. ' GetAnt;',
  19780. 'end;',
  19781. 'begin',
  19782. '']);
  19783. ConvertProgram;
  19784. CheckSource('TestClassInterface_COM_With',
  19785. LinesToStr([ // statements
  19786. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19787. 'rtl.createClass(this, "TObject", null, function () {',
  19788. ' this.$init = function () {',
  19789. ' };',
  19790. ' this.$final = function () {',
  19791. ' };',
  19792. ' rtl.addIntf(this, $mod.IUnknown);',
  19793. '});',
  19794. 'this.DoIt = function () {',
  19795. ' var i = null;',
  19796. ' var $ir = rtl.createIntfRefs();',
  19797. ' try {',
  19798. ' $ir.ref(1, i.GetAnt());',
  19799. ' var $with = $ir.ref(2, i.GetAnt());',
  19800. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  19801. ' $ir.ref(4, $with1.GetAnt());',
  19802. ' } finally {',
  19803. ' $ir.free();',
  19804. ' };',
  19805. '};',
  19806. '']),
  19807. LinesToStr([ // $mod.$main
  19808. '']));
  19809. end;
  19810. procedure TTestModule.TestClassInterface_COM_ForIn;
  19811. begin
  19812. StartProgram(false);
  19813. Add([
  19814. '{$interfaces com}',
  19815. 'type',
  19816. ' IUnknown = interface end;',
  19817. ' TObject = class',
  19818. ' Id: longint;',
  19819. ' end;',
  19820. ' IEnumerator = interface(IUnknown)',
  19821. ' function GetCurrent: TObject;',
  19822. ' function MoveNext: Boolean;',
  19823. ' property Current: TObject read GetCurrent;',
  19824. ' end;',
  19825. ' IEnumerable = interface(IUnknown)',
  19826. ' function GetEnumerator: IEnumerator;',
  19827. ' end;',
  19828. 'var',
  19829. ' o: TObject;',
  19830. ' i: IEnumerable;',
  19831. 'begin',
  19832. ' for o in i do o.Id:=3;',
  19833. '']);
  19834. ConvertProgram;
  19835. CheckSource('TestClassInterface_COM_ForIn',
  19836. LinesToStr([ // statements
  19837. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19838. 'rtl.createClass(this, "TObject", null, function () {',
  19839. ' this.$init = function () {',
  19840. ' this.Id = 0;',
  19841. ' };',
  19842. ' this.$final = function () {',
  19843. ' };',
  19844. '});',
  19845. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19846. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19847. 'this.o = null;',
  19848. 'this.i = null;',
  19849. '']),
  19850. LinesToStr([ // $mod.$main
  19851. 'var $in = $mod.i.GetEnumerator();',
  19852. 'try {',
  19853. ' while ($in.MoveNext()) {',
  19854. ' $mod.o = $in.GetCurrent();',
  19855. ' $mod.o.Id = 3;',
  19856. ' }',
  19857. '} finally {',
  19858. ' rtl._Release($in)',
  19859. '};',
  19860. '']));
  19861. end;
  19862. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19863. begin
  19864. StartProgram(false);
  19865. Add([
  19866. '{$interfaces com}',
  19867. 'type',
  19868. ' IUnknown = interface',
  19869. ' function _AddRef: longint;',
  19870. ' function _Release: longint;',
  19871. ' end;',
  19872. ' TObject = class',
  19873. ' end;',
  19874. ' TArrOfIntf = array of IUnknown;',
  19875. 'begin',
  19876. '']);
  19877. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19878. ConvertProgram;
  19879. end;
  19880. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19881. begin
  19882. StartProgram(false);
  19883. Add([
  19884. '{$interfaces com}',
  19885. 'type',
  19886. ' IUnknown = interface',
  19887. ' function _AddRef: longint;',
  19888. ' function _Release: longint;',
  19889. ' end;',
  19890. ' TRec = record',
  19891. ' i: IUnknown;',
  19892. ' end;',
  19893. 'begin',
  19894. '']);
  19895. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  19896. ConvertProgram;
  19897. end;
  19898. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  19899. begin
  19900. StartUnit(false);
  19901. Add([
  19902. '{$interfaces com}',
  19903. 'interface',
  19904. 'implementation',
  19905. 'type',
  19906. ' IUnknown = interface',
  19907. ' function _AddRef: longint;',
  19908. ' end;',
  19909. ' TObject = class(IUnknown)',
  19910. ' function _AddRef: longint;',
  19911. ' end;',
  19912. 'function TObject._AddRef: longint; begin end;',
  19913. 'var i: IUnknown;',
  19914. ' o: TObject;',
  19915. 'initialization',
  19916. ' i:=nil;',
  19917. ' i:=i;',
  19918. ' i:=o;',
  19919. ' if (o as IUnknown)=nil then ;',
  19920. '']);
  19921. ConvertUnit;
  19922. CheckSource('TestClassInterface_COM_UnitInitialization',
  19923. LinesToStr([ // statements
  19924. 'var $impl = $mod.$impl;',
  19925. '']),
  19926. LinesToStr([ // this.$init
  19927. 'var $ir = rtl.createIntfRefs();',
  19928. 'try {',
  19929. ' rtl.setIntfP($impl, "i", null);',
  19930. ' rtl.setIntfP($impl, "i", $impl.i);',
  19931. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19932. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19933. '} finally {',
  19934. ' $ir.free();',
  19935. '};',
  19936. '']),
  19937. LinesToStr([ // implementation
  19938. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19939. 'rtl.createClass($impl, "TObject", null, function () {',
  19940. ' this.$init = function () {',
  19941. ' };',
  19942. ' this.$final = function () {',
  19943. ' };',
  19944. ' this._AddRef = function () {',
  19945. ' var Result = 0;',
  19946. ' return Result;',
  19947. ' };',
  19948. ' rtl.addIntf(this, $impl.IUnknown);',
  19949. '});',
  19950. '$impl.i = null;',
  19951. '$impl.o = null;',
  19952. ''])
  19953. );
  19954. end;
  19955. procedure TTestModule.TestClassInterface_GUID;
  19956. begin
  19957. StartProgram(false);
  19958. Add([
  19959. '{$interfaces corba}',
  19960. 'type',
  19961. ' IUnknown = interface',
  19962. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19963. ' end;',
  19964. ' TObject = class end;',
  19965. ' TGUID = record D1, D2, D3, D4: word; end;',
  19966. ' TAliasGUID = TGUID;',
  19967. ' TGUIDString = type string;',
  19968. ' TAliasGUIDString = TGUIDString;',
  19969. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19970. 'begin end;',
  19971. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19972. 'begin end;',
  19973. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19974. 'begin end;',
  19975. 'var',
  19976. ' i: IUnknown;',
  19977. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19978. ' s: TAliasGUIDString;',
  19979. 'begin',
  19980. ' DoConstGUIDIt(IUnknown);',
  19981. ' DoDefGUID(IUnknown);',
  19982. ' DoStr(IUnknown);',
  19983. ' DoConstGUIDIt(i);',
  19984. ' DoDefGUID(i);',
  19985. ' DoStr(i);',
  19986. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19987. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19988. ' DoStr(g);',
  19989. ' g:=i;',
  19990. ' g:=IUnknown;',
  19991. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19992. ' s:=i;',
  19993. ' s:=IUnknown;',
  19994. ' s:=g;',
  19995. ' if g=i then ;',
  19996. ' if i=g then ;',
  19997. ' if g=IUnknown then ;',
  19998. ' if IUnknown=g then ;',
  19999. ' if s=i then ;',
  20000. ' if i=s then ;',
  20001. ' if s=IUnknown then ;',
  20002. ' if IUnknown=s then ;',
  20003. ' if s=g then ;',
  20004. ' if g=s then ;',
  20005. '']);
  20006. ConvertProgram;
  20007. CheckSource('TestClassInterface_GUID',
  20008. LinesToStr([ // statements
  20009. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20010. 'rtl.createClass(this, "TObject", null, function () {',
  20011. ' this.$init = function () {',
  20012. ' };',
  20013. ' this.$final = function () {',
  20014. ' };',
  20015. '});',
  20016. 'rtl.recNewT(this, "TGUID", function () {',
  20017. ' this.D1 = 0;',
  20018. ' this.D2 = 0;',
  20019. ' this.D3 = 0;',
  20020. ' this.D4 = 0;',
  20021. ' this.$eq = function (b) {',
  20022. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20023. ' };',
  20024. ' this.$assign = function (s) {',
  20025. ' this.D1 = s.D1;',
  20026. ' this.D2 = s.D2;',
  20027. ' this.D3 = s.D3;',
  20028. ' this.D4 = s.D4;',
  20029. ' return this;',
  20030. ' };',
  20031. '});',
  20032. 'this.DoConstGUIDIt = function (g) {',
  20033. '};',
  20034. 'this.DoDefGUID = function (g) {',
  20035. '};',
  20036. 'this.DoStr = function (s) {',
  20037. '};',
  20038. 'this.i = null;',
  20039. 'this.g = this.TGUID.$clone({',
  20040. ' D1: 0xD91C9AF4,',
  20041. ' D2: 0x3C93,',
  20042. ' D3: 0x420F,',
  20043. ' D4: [',
  20044. ' 0xA3,',
  20045. ' 0x03,',
  20046. ' 0xBF,',
  20047. ' 0x5B,',
  20048. ' 0xA8,',
  20049. ' 0x2B,',
  20050. ' 0xFD,',
  20051. ' 0x23',
  20052. ' ]',
  20053. '});',
  20054. 'this.s = "";',
  20055. '']),
  20056. LinesToStr([ // $mod.$main
  20057. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20058. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20059. '$mod.DoStr($mod.IUnknown.$guid);',
  20060. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20061. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20062. '$mod.DoStr($mod.i.$guid);',
  20063. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20064. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20065. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20066. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20067. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20068. '$mod.g.$assign({',
  20069. ' D1: 0xD91C9AF4,',
  20070. ' D2: 0x3C93,',
  20071. ' D3: 0x420F,',
  20072. ' D4: [',
  20073. ' 0xA3,',
  20074. ' 0x03,',
  20075. ' 0xBF,',
  20076. ' 0x5B,',
  20077. ' 0xA8,',
  20078. ' 0x2B,',
  20079. ' 0xFD,',
  20080. ' 0x23',
  20081. ' ]',
  20082. '});',
  20083. '$mod.s = $mod.i.$guid;',
  20084. '$mod.s = $mod.IUnknown.$guid;',
  20085. '$mod.s = rtl.guidrToStr($mod.g);',
  20086. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20087. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20088. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20089. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20090. 'if ($mod.s === $mod.i.$guid) ;',
  20091. 'if ($mod.i.$guid === $mod.s) ;',
  20092. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20093. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20094. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20095. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20096. '']));
  20097. end;
  20098. procedure TTestModule.TestClassInterface_GUIDProperty;
  20099. begin
  20100. StartProgram(false);
  20101. Add([
  20102. '{$interfaces corba}',
  20103. 'type',
  20104. ' IUnknown = interface',
  20105. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20106. ' end;',
  20107. ' TGUID = record D1, D2, D3, D4: word; end;',
  20108. ' TAliasGUID = TGUID;',
  20109. ' TGUIDString = type string;',
  20110. ' TAliasGUIDString = TGUIDString;',
  20111. ' TObject = class',
  20112. ' function GetG: TAliasGUID; virtual; abstract;',
  20113. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20114. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20115. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20116. ' property g: TAliasGUID read GetG write SetG;',
  20117. ' property s: TAliasGUIDString read GetS write SetS;',
  20118. ' end;',
  20119. 'var o: TObject;',
  20120. 'begin',
  20121. ' o.g:=IUnknown;',
  20122. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20123. ' o.s:=IUnknown;',
  20124. ' o.s:=o.g;',
  20125. '']);
  20126. ConvertProgram;
  20127. CheckSource('TestClassInterface_GUIDProperty',
  20128. LinesToStr([ // statements
  20129. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20130. 'rtl.recNewT(this, "TGUID", function () {',
  20131. ' this.D1 = 0;',
  20132. ' this.D2 = 0;',
  20133. ' this.D3 = 0;',
  20134. ' this.D4 = 0;',
  20135. ' this.$eq = function (b) {',
  20136. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20137. ' };',
  20138. ' this.$assign = function (s) {',
  20139. ' this.D1 = s.D1;',
  20140. ' this.D2 = s.D2;',
  20141. ' this.D3 = s.D3;',
  20142. ' this.D4 = s.D4;',
  20143. ' return this;',
  20144. ' };',
  20145. '});',
  20146. 'rtl.createClass(this, "TObject", null, function () {',
  20147. ' this.$init = function () {',
  20148. ' };',
  20149. ' this.$final = function () {',
  20150. ' };',
  20151. '});',
  20152. 'this.o = null;',
  20153. '']),
  20154. LinesToStr([ // $mod.$main
  20155. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20156. '$mod.o.SetG({',
  20157. ' D1: 0xD91C9AF4,',
  20158. ' D2: 0x3C93,',
  20159. ' D3: 0x420F,',
  20160. ' D4: [',
  20161. ' 0xA3,',
  20162. ' 0x03,',
  20163. ' 0xBF,',
  20164. ' 0x5B,',
  20165. ' 0xA8,',
  20166. ' 0x2B,',
  20167. ' 0xFD,',
  20168. ' 0x23',
  20169. ' ]',
  20170. '});',
  20171. '$mod.o.SetS($mod.IUnknown.$guid);',
  20172. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20173. '']));
  20174. end;
  20175. procedure TTestModule.TestClassHelper_ClassVar;
  20176. begin
  20177. StartProgram(false);
  20178. Add([
  20179. 'type',
  20180. ' TObject = class',
  20181. ' end;',
  20182. ' THelper = class helper for TObject',
  20183. ' const',
  20184. ' One = 1;',
  20185. ' Two: word = 2;',
  20186. ' class var',
  20187. ' Glob: word;',
  20188. ' function Foo(w: word): word;',
  20189. ' class function Bar(w: word): word;',
  20190. ' end;',
  20191. 'function THelper.foo(w: word): word;',
  20192. 'begin',
  20193. ' Result:=w;',
  20194. ' Two:=One+w;',
  20195. ' Glob:=Glob;',
  20196. ' Result:=Self.Glob;',
  20197. ' Self.Glob:=Self.Glob;',
  20198. ' with Self do Glob:=Glob;',
  20199. 'end;',
  20200. 'class function THelper.bar(w: word): word;',
  20201. 'begin',
  20202. ' Result:=w;',
  20203. ' Two:=One;',
  20204. ' Glob:=Glob;',
  20205. ' Self.Glob:=Self.Glob;',
  20206. ' with Self do Glob:=Glob;',
  20207. 'end;',
  20208. 'var o: TObject;',
  20209. 'begin',
  20210. ' tobject.two:=tobject.one;',
  20211. ' tobject.Glob:=tobject.Glob;',
  20212. ' with tobject do begin',
  20213. ' two:=one;',
  20214. ' Glob:=Glob;',
  20215. ' end;',
  20216. ' o.two:=o.one;',
  20217. ' o.Glob:=o.Glob;',
  20218. ' with o do begin',
  20219. ' two:=one;',
  20220. ' Glob:=Glob;',
  20221. ' end;',
  20222. '']);
  20223. ConvertProgram;
  20224. CheckSource('TestClassHelper_ClassVar',
  20225. LinesToStr([ // statements
  20226. 'rtl.createClass(this, "TObject", null, function () {',
  20227. ' this.$init = function () {',
  20228. ' };',
  20229. ' this.$final = function () {',
  20230. ' };',
  20231. '});',
  20232. 'rtl.createHelper(this, "THelper", null, function () {',
  20233. ' this.One = 1;',
  20234. ' this.Two = 2;',
  20235. ' this.Glob = 0;',
  20236. ' this.Foo = function (w) {',
  20237. ' var Result = 0;',
  20238. ' Result = w;',
  20239. ' $mod.THelper.Two = 1 + w;',
  20240. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20241. ' Result = $mod.THelper.Glob;',
  20242. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20243. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20244. ' return Result;',
  20245. ' };',
  20246. ' this.Bar = function (w) {',
  20247. ' var Result = 0;',
  20248. ' Result = w;',
  20249. ' $mod.THelper.Two = 1;',
  20250. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20251. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20252. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20253. ' return Result;',
  20254. ' };',
  20255. '});',
  20256. 'this.o = null;',
  20257. '']),
  20258. LinesToStr([ // $mod.$main
  20259. '$mod.THelper.Two = 1;',
  20260. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20261. 'var $with = $mod.TObject;',
  20262. '$mod.THelper.Two = 1;',
  20263. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20264. '$mod.THelper.Two = 1;',
  20265. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20266. 'var $with1 = $mod.o;',
  20267. '$mod.THelper.Two = 1;',
  20268. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20269. '']));
  20270. end;
  20271. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20272. begin
  20273. StartProgram(false);
  20274. Add([
  20275. 'type',
  20276. ' TObject = class',
  20277. ' FSize: word;',
  20278. ' property Size: word read FSize write FSize;',
  20279. ' end;',
  20280. ' THelper = class helper for TObject',
  20281. ' function Foo(w: word = 1): word;',
  20282. ' end;',
  20283. 'function THelper.foo(w: word): word;',
  20284. 'begin',
  20285. ' Result:=Size;',
  20286. ' Size:=Size+2;',
  20287. ' Self.Size:=Self.Size+3;',
  20288. ' FSize:=FSize+4;',
  20289. ' Self.FSize:=Self.FSize+5;',
  20290. ' with Self do begin',
  20291. ' Size:=Size+6;',
  20292. ' FSize:=FSize+7;',
  20293. ' FSize:=FSize+8;',
  20294. ' end;',
  20295. 'end;',
  20296. 'begin',
  20297. '']);
  20298. ConvertProgram;
  20299. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20300. LinesToStr([ // statements
  20301. 'rtl.createClass(this, "TObject", null, function () {',
  20302. ' this.$init = function () {',
  20303. ' this.FSize = 0;',
  20304. ' };',
  20305. ' this.$final = function () {',
  20306. ' };',
  20307. '});',
  20308. 'rtl.createHelper(this, "THelper", null, function () {',
  20309. ' this.Foo = function (w) {',
  20310. ' var Result = 0;',
  20311. ' Result = this.FSize;',
  20312. ' this.FSize = this.FSize + 2;',
  20313. ' this.FSize = this.FSize + 3;',
  20314. ' this.FSize = this.FSize + 4;',
  20315. ' this.FSize = this.FSize + 5;',
  20316. ' this.FSize = this.FSize + 6;',
  20317. ' this.FSize = this.FSize + 7;',
  20318. ' this.FSize = this.FSize + 8;',
  20319. ' return Result;',
  20320. ' };',
  20321. '});',
  20322. '']),
  20323. LinesToStr([ // $mod.$main
  20324. '']));
  20325. end;
  20326. procedure TTestModule.TestClassHelper_Method_Call;
  20327. begin
  20328. StartProgram(false);
  20329. Add([
  20330. 'type',
  20331. ' TObject = class',
  20332. ' procedure Run(w: word = 10);',
  20333. ' end;',
  20334. ' THelper = class helper for TObject',
  20335. ' function Foo(w: word = 1): word;',
  20336. ' end;',
  20337. 'procedure TObject.Run(w: word);',
  20338. 'var o: TObject;',
  20339. 'begin',
  20340. ' Foo;',
  20341. ' Foo();',
  20342. ' Foo(2);',
  20343. ' Self.Foo;',
  20344. ' Self.Foo();',
  20345. ' Self.Foo(3);',
  20346. ' with Self do begin',
  20347. ' Foo;',
  20348. ' Foo();',
  20349. ' Foo(4);',
  20350. ' end;',
  20351. ' with o do Foo(5);',
  20352. 'end;',
  20353. 'function THelper.foo(w: word): word;',
  20354. 'begin',
  20355. ' Run;',
  20356. ' Run();',
  20357. ' Run(11);',
  20358. ' Foo;',
  20359. ' Foo();',
  20360. ' Foo(12);',
  20361. ' Self.Foo;',
  20362. ' Self.Foo();',
  20363. ' Self.Foo(13);',
  20364. ' with Self do begin',
  20365. ' Foo;',
  20366. ' Foo();',
  20367. ' Foo(14);',
  20368. ' end;',
  20369. 'end;',
  20370. 'var Obj: TObject;',
  20371. 'begin',
  20372. ' obj.Foo;',
  20373. ' obj.Foo();',
  20374. ' obj.Foo(21);',
  20375. ' with obj do begin',
  20376. ' Foo;',
  20377. ' Foo();',
  20378. ' Foo(22);',
  20379. ' end;',
  20380. '']);
  20381. ConvertProgram;
  20382. CheckSource('TestClassHelper_Method_Call',
  20383. LinesToStr([ // statements
  20384. 'rtl.createClass(this, "TObject", null, function () {',
  20385. ' this.$init = function () {',
  20386. ' };',
  20387. ' this.$final = function () {',
  20388. ' };',
  20389. ' this.Run = function (w) {',
  20390. ' var o = null;',
  20391. ' $mod.THelper.Foo.call(this, 1);',
  20392. ' $mod.THelper.Foo.call(this, 1);',
  20393. ' $mod.THelper.Foo.call(this, 2);',
  20394. ' $mod.THelper.Foo.call(this, 1);',
  20395. ' $mod.THelper.Foo.call(this, 1);',
  20396. ' $mod.THelper.Foo.call(this, 3);',
  20397. ' $mod.THelper.Foo.call(this, 1);',
  20398. ' $mod.THelper.Foo.call(this, 1);',
  20399. ' $mod.THelper.Foo.call(this, 4);',
  20400. ' $mod.THelper.Foo.call(o, 5);',
  20401. ' };',
  20402. '});',
  20403. 'rtl.createHelper(this, "THelper", null, function () {',
  20404. ' this.Foo = function (w) {',
  20405. ' var Result = 0;',
  20406. ' this.Run(10);',
  20407. ' this.Run(10);',
  20408. ' this.Run(11);',
  20409. ' $mod.THelper.Foo.call(this, 1);',
  20410. ' $mod.THelper.Foo.call(this, 1);',
  20411. ' $mod.THelper.Foo.call(this, 12);',
  20412. ' $mod.THelper.Foo.call(this, 1);',
  20413. ' $mod.THelper.Foo.call(this, 1);',
  20414. ' $mod.THelper.Foo.call(this, 13);',
  20415. ' $mod.THelper.Foo.call(this, 1);',
  20416. ' $mod.THelper.Foo.call(this, 1);',
  20417. ' $mod.THelper.Foo.call(this, 14);',
  20418. ' return Result;',
  20419. ' };',
  20420. '});',
  20421. 'this.Obj = null;',
  20422. '']),
  20423. LinesToStr([ // $mod.$main
  20424. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20425. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20426. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20427. 'var $with = $mod.Obj;',
  20428. '$mod.THelper.Foo.call($with, 1);',
  20429. '$mod.THelper.Foo.call($with, 1);',
  20430. '$mod.THelper.Foo.call($with, 22);',
  20431. '']));
  20432. end;
  20433. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20434. begin
  20435. StartProgram(false);
  20436. Add([
  20437. 'type',
  20438. ' TObject = class',
  20439. ' procedure Run(w: word = 10);',
  20440. ' end;',
  20441. ' THelper = class helper for TObject',
  20442. ' function Foo(w: word = 1): word;',
  20443. ' end;',
  20444. 'procedure TObject.Run(w: word);',
  20445. ' procedure Sub(Self: TObject);',
  20446. ' begin',
  20447. ' Foo;',
  20448. ' Foo();',
  20449. ' Self.Foo;',
  20450. ' Self.Foo();',
  20451. ' with Self do begin',
  20452. ' Foo;',
  20453. ' Foo();',
  20454. ' end;',
  20455. ' end;',
  20456. 'begin',
  20457. 'end;',
  20458. 'function THelper.foo(w: word): word;',
  20459. ' procedure Sub(Self: TObject);',
  20460. ' begin',
  20461. ' Run;',
  20462. ' Run();',
  20463. ' Foo;',
  20464. ' Foo();',
  20465. ' Self.Foo;',
  20466. ' Self.Foo();',
  20467. ' with Self do begin',
  20468. ' Foo;',
  20469. ' Foo();',
  20470. ' end;',
  20471. ' end;',
  20472. 'begin',
  20473. 'end;',
  20474. 'begin',
  20475. '']);
  20476. ConvertProgram;
  20477. CheckSource('TestClassHelper_Method_Nested_Call',
  20478. LinesToStr([ // statements
  20479. 'rtl.createClass(this, "TObject", null, function () {',
  20480. ' this.$init = function () {',
  20481. ' };',
  20482. ' this.$final = function () {',
  20483. ' };',
  20484. ' this.Run = function (w) {',
  20485. ' var $Self = this;',
  20486. ' function Sub(Self) {',
  20487. ' $mod.THelper.Foo.call($Self, 1);',
  20488. ' $mod.THelper.Foo.call($Self, 1);',
  20489. ' $mod.THelper.Foo.call(Self, 1);',
  20490. ' $mod.THelper.Foo.call(Self, 1);',
  20491. ' $mod.THelper.Foo.call(Self, 1);',
  20492. ' $mod.THelper.Foo.call(Self, 1);',
  20493. ' };',
  20494. ' };',
  20495. '});',
  20496. 'rtl.createHelper(this, "THelper", null, function () {',
  20497. ' this.Foo = function (w) {',
  20498. ' var $Self = this;',
  20499. ' var Result = 0;',
  20500. ' function Sub(Self) {',
  20501. ' $Self.Run(10);',
  20502. ' $Self.Run(10);',
  20503. ' $mod.THelper.Foo.call($Self, 1);',
  20504. ' $mod.THelper.Foo.call($Self, 1);',
  20505. ' $mod.THelper.Foo.call(Self, 1);',
  20506. ' $mod.THelper.Foo.call(Self, 1);',
  20507. ' $mod.THelper.Foo.call(Self, 1);',
  20508. ' $mod.THelper.Foo.call(Self, 1);',
  20509. ' };',
  20510. ' return Result;',
  20511. ' };',
  20512. '});',
  20513. '']),
  20514. LinesToStr([ // $mod.$main
  20515. '']));
  20516. end;
  20517. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20518. begin
  20519. StartProgram(false);
  20520. Add([
  20521. 'type',
  20522. ' TObject = class',
  20523. ' class procedure Run(w: word = 10);',
  20524. ' end;',
  20525. ' THelper = class helper for TObject',
  20526. ' class function Foo(w: word = 1): word;',
  20527. ' end;',
  20528. 'class procedure TObject.Run(w: word);',
  20529. 'begin',
  20530. ' Foo;',
  20531. ' Foo();',
  20532. ' Self.Foo;',
  20533. ' Self.Foo();',
  20534. ' with Self do begin',
  20535. ' Foo;',
  20536. ' Foo();',
  20537. ' end;',
  20538. 'end;',
  20539. 'class function THelper.foo(w: word): word;',
  20540. 'begin',
  20541. ' Run;',
  20542. ' Run();',
  20543. ' Foo;',
  20544. ' Foo();',
  20545. ' Self.Foo;',
  20546. ' Self.Foo();',
  20547. ' with Self do begin',
  20548. ' Foo;',
  20549. ' Foo();',
  20550. ' end;',
  20551. 'end;',
  20552. 'var',
  20553. ' Obj: TObject;',
  20554. 'begin',
  20555. ' obj.Foo;',
  20556. ' obj.Foo();',
  20557. ' with obj do begin',
  20558. ' Foo;',
  20559. ' Foo();',
  20560. ' end;',
  20561. ' tobject.Foo;',
  20562. ' tobject.Foo();',
  20563. ' with tobject do begin',
  20564. ' Foo;',
  20565. ' Foo();',
  20566. ' end;',
  20567. '']);
  20568. ConvertProgram;
  20569. CheckSource('TestClassHelper_ClassMethod_Call',
  20570. LinesToStr([ // statements
  20571. 'rtl.createClass(this, "TObject", null, function () {',
  20572. ' this.$init = function () {',
  20573. ' };',
  20574. ' this.$final = function () {',
  20575. ' };',
  20576. ' this.Run = function (w) {',
  20577. ' $mod.THelper.Foo.call(this, 1);',
  20578. ' $mod.THelper.Foo.call(this, 1);',
  20579. ' $mod.THelper.Foo.call(this, 1);',
  20580. ' $mod.THelper.Foo.call(this, 1);',
  20581. ' $mod.THelper.Foo.call(this, 1);',
  20582. ' $mod.THelper.Foo.call(this, 1);',
  20583. ' };',
  20584. '});',
  20585. 'rtl.createHelper(this, "THelper", null, function () {',
  20586. ' this.Foo = function (w) {',
  20587. ' var Result = 0;',
  20588. ' this.Run(10);',
  20589. ' this.Run(10);',
  20590. ' $mod.THelper.Foo.call(this, 1);',
  20591. ' $mod.THelper.Foo.call(this, 1);',
  20592. ' $mod.THelper.Foo.call(this, 1);',
  20593. ' $mod.THelper.Foo.call(this, 1);',
  20594. ' $mod.THelper.Foo.call(this, 1);',
  20595. ' $mod.THelper.Foo.call(this, 1);',
  20596. ' return Result;',
  20597. ' };',
  20598. '});',
  20599. 'this.Obj = null;',
  20600. '']),
  20601. LinesToStr([ // $mod.$main
  20602. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20603. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20604. 'var $with = $mod.Obj;',
  20605. '$mod.THelper.Foo.call($with.$class, 1);',
  20606. '$mod.THelper.Foo.call($with.$class, 1);',
  20607. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20608. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20609. 'var $with1 = $mod.TObject;',
  20610. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20611. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20612. '']));
  20613. end;
  20614. procedure TTestModule.TestClassHelper_ClassOf;
  20615. begin
  20616. StartProgram(false);
  20617. Add([
  20618. 'type',
  20619. ' TObject = class',
  20620. ' end;',
  20621. ' TClass = class of TObject;',
  20622. ' THelper = class helper for TObject',
  20623. ' class function Foo(w: word = 1): word;',
  20624. ' end;',
  20625. 'class function THelper.foo(w: word): word;',
  20626. 'begin',
  20627. 'end;',
  20628. 'var',
  20629. ' c: TClass;',
  20630. 'begin',
  20631. ' c.Foo;',
  20632. ' c.Foo();',
  20633. ' with c do begin',
  20634. ' Foo;',
  20635. ' Foo();',
  20636. ' end;',
  20637. '']);
  20638. ConvertProgram;
  20639. CheckSource('TestClassHelper_ClassOf',
  20640. LinesToStr([ // statements
  20641. 'rtl.createClass(this, "TObject", null, function () {',
  20642. ' this.$init = function () {',
  20643. ' };',
  20644. ' this.$final = function () {',
  20645. ' };',
  20646. '});',
  20647. 'rtl.createHelper(this, "THelper", null, function () {',
  20648. ' this.Foo = function (w) {',
  20649. ' var Result = 0;',
  20650. ' return Result;',
  20651. ' };',
  20652. '});',
  20653. 'this.c = null;',
  20654. '']),
  20655. LinesToStr([ // $mod.$main
  20656. '$mod.THelper.Foo.call($mod.c, 1);',
  20657. '$mod.THelper.Foo.call($mod.c, 1);',
  20658. 'var $with = $mod.c;',
  20659. '$mod.THelper.Foo.call($with, 1);',
  20660. '$mod.THelper.Foo.call($with, 1);',
  20661. '']));
  20662. end;
  20663. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20664. begin
  20665. StartProgram(false);
  20666. Add([
  20667. '{$mode objfpc}',
  20668. 'type',
  20669. ' TObject = class',
  20670. ' procedure DoIt;',
  20671. ' end;',
  20672. ' THelper = class helper for TObject',
  20673. ' procedure Fly(w: word = 1);',
  20674. ' class procedure Glide(w: word = 1);',
  20675. ' class procedure Run(w: word = 1); static;',
  20676. ' end;',
  20677. ' TFly = procedure(w: word) of object;',
  20678. ' TGlide = TFly;',
  20679. ' TRun = procedure(w: word);',
  20680. 'var',
  20681. ' f: TFly;',
  20682. ' g: TGlide;',
  20683. ' r: TRun;',
  20684. 'procedure TObject.DoIt;',
  20685. 'begin',
  20686. ' f:=@fly;',
  20687. ' g:=@glide;',
  20688. ' r:=@run;',
  20689. ' f:[email protected];',
  20690. ' g:[email protected];',
  20691. ' r:[email protected];',
  20692. ' with self do begin',
  20693. ' f:=@fly;',
  20694. ' g:=@glide;',
  20695. ' r:=@run;',
  20696. ' end;',
  20697. 'end;',
  20698. 'procedure THelper.fly(w: word);',
  20699. 'begin',
  20700. ' f:=@fly;',
  20701. ' g:=@glide;',
  20702. ' r:=@run;',
  20703. 'end;',
  20704. 'class procedure THelper.glide(w: word);',
  20705. 'begin',
  20706. ' g:=@glide;',
  20707. ' r:=@run;',
  20708. 'end;',
  20709. 'class procedure THelper.run(w: word);',
  20710. 'begin',
  20711. ' g:=@glide;',
  20712. ' r:=@run;',
  20713. 'end;',
  20714. 'var',
  20715. ' Obj: TObject;',
  20716. 'begin',
  20717. ' f:[email protected];',
  20718. ' g:[email protected];',
  20719. ' r:[email protected];',
  20720. ' with obj do begin',
  20721. ' f:=@fly;',
  20722. ' g:=@glide;',
  20723. ' r:=@run;',
  20724. ' end;',
  20725. ' g:[email protected];',
  20726. ' r:[email protected];',
  20727. ' with tobject do begin',
  20728. ' g:=@glide;',
  20729. ' r:=@run;',
  20730. ' end;',
  20731. '']);
  20732. ConvertProgram;
  20733. CheckSource('TestClassHelper_MethodRefObjFPC',
  20734. LinesToStr([ // statements
  20735. 'rtl.createClass(this, "TObject", null, function () {',
  20736. ' this.$init = function () {',
  20737. ' };',
  20738. ' this.$final = function () {',
  20739. ' };',
  20740. ' this.DoIt = function () {',
  20741. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20742. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20743. ' $mod.r = $mod.THelper.Run;',
  20744. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20745. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20746. ' $mod.r = $mod.THelper.Run;',
  20747. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20748. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20749. ' $mod.r = $mod.THelper.Run;',
  20750. ' };',
  20751. '});',
  20752. 'rtl.createHelper(this, "THelper", null, function () {',
  20753. ' this.Fly = function (w) {',
  20754. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20755. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20756. ' $mod.r = $mod.THelper.Run;',
  20757. ' };',
  20758. ' this.Glide = function (w) {',
  20759. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20760. ' $mod.r = $mod.THelper.Run;',
  20761. ' };',
  20762. ' this.Run = function (w) {',
  20763. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20764. ' $mod.r = $mod.THelper.Run;',
  20765. ' };',
  20766. '});',
  20767. 'this.f = null;',
  20768. 'this.g = null;',
  20769. 'this.r = null;',
  20770. 'this.Obj = null;',
  20771. '']),
  20772. LinesToStr([ // $mod.$main
  20773. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20774. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20775. '$mod.r = $mod.THelper.Run;',
  20776. 'var $with = $mod.Obj;',
  20777. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20778. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20779. '$mod.r = $mod.THelper.Run;',
  20780. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20781. '$mod.r = $mod.THelper.Run;',
  20782. 'var $with1 = $mod.TObject;',
  20783. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20784. '$mod.r = $mod.THelper.Run;',
  20785. '']));
  20786. end;
  20787. procedure TTestModule.TestClassHelper_Constructor;
  20788. begin
  20789. StartProgram(false);
  20790. Add([
  20791. 'type',
  20792. ' TObject = class',
  20793. ' constructor Create;',
  20794. ' end;',
  20795. ' TClass = class of TObject;',
  20796. ' THelper = class helper for TObject',
  20797. ' constructor NewHlp(w: word);',
  20798. ' end;',
  20799. 'var',
  20800. ' obj: TObject;',
  20801. ' c: TClass;',
  20802. 'constructor TObject.Create;',
  20803. 'begin',
  20804. ' NewHlp(2);', // normal call
  20805. ' tobject.NewHlp(3);', // new instance
  20806. ' c.newhlp(4);', // new instance
  20807. 'end;',
  20808. 'constructor THelper.NewHlp(w: word);',
  20809. 'begin',
  20810. ' create;', // normal call
  20811. ' tobject.create;', // new instance
  20812. ' NewHlp(2);', // normal call
  20813. ' tobject.NewHlp(3);', // new instance
  20814. ' c.newhlp(4);', // new instance
  20815. 'end;',
  20816. 'begin',
  20817. ' obj.newhlp(2);', // normal call
  20818. ' with Obj do newhlp(12);', // normal call
  20819. ' tobject.newhlp(3);', // new instance
  20820. ' with tobject do newhlp(13);', // new instance
  20821. ' c.newhlp(4);', // new instance
  20822. ' with c do newhlp(14);', // new instance
  20823. '']);
  20824. ConvertProgram;
  20825. CheckSource('TestClassHelper_Constructor',
  20826. LinesToStr([ // statements
  20827. 'rtl.createClass(this, "TObject", null, function () {',
  20828. ' this.$init = function () {',
  20829. ' };',
  20830. ' this.$final = function () {',
  20831. ' };',
  20832. ' this.Create = function () {',
  20833. ' $mod.THelper.NewHlp.call(this, 2);',
  20834. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20835. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20836. ' return this;',
  20837. ' };',
  20838. '});',
  20839. 'rtl.createHelper(this, "THelper", null, function () {',
  20840. ' this.NewHlp = function (w) {',
  20841. ' this.Create();',
  20842. ' $mod.TObject.$create("Create");',
  20843. ' $mod.THelper.NewHlp.call(this, 2);',
  20844. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20845. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20846. ' return this;',
  20847. ' };',
  20848. '});',
  20849. 'this.obj = null;',
  20850. 'this.c = null;',
  20851. '']),
  20852. LinesToStr([ // $mod.$main
  20853. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20854. 'var $with = $mod.obj;',
  20855. '$mod.THelper.NewHlp.call($with, 12);',
  20856. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20857. 'var $with1 = $mod.TObject;',
  20858. '$with1.$create($mod.THelper.NewHlp, [13]);',
  20859. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20860. 'var $with2 = $mod.c;',
  20861. '$with2.$create($mod.THelper.NewHlp, [14]);',
  20862. '']));
  20863. end;
  20864. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20865. begin
  20866. StartProgram(false);
  20867. Add([
  20868. 'type',
  20869. ' TObject = class',
  20870. ' procedure Fly;',
  20871. ' end;',
  20872. ' TObjHelper = class helper for TObject',
  20873. ' procedure Fly;',
  20874. ' end;',
  20875. ' TBird = class',
  20876. ' procedure Fly;',
  20877. ' end;',
  20878. ' TBirdHelper = class helper for TBird',
  20879. ' procedure Fly;',
  20880. ' procedure Walk(w: word);',
  20881. ' end;',
  20882. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20883. ' procedure Fly;',
  20884. ' procedure Walk(w: word);',
  20885. ' end;',
  20886. 'procedure Tobject.fly;',
  20887. 'begin',
  20888. ' inherited;', // ignore
  20889. 'end;',
  20890. 'procedure Tobjhelper.fly;',
  20891. 'begin',
  20892. ' {@TObject_Fly}inherited;',
  20893. ' inherited {@TObject_Fly}Fly;',
  20894. 'end;',
  20895. 'procedure Tbird.fly;',
  20896. 'begin',
  20897. ' {@TObjHelper_Fly}inherited;',
  20898. ' inherited {@TObjHelper_Fly}Fly;',
  20899. 'end;',
  20900. 'procedure Tbirdhelper.fly;',
  20901. 'begin',
  20902. ' {@TBird_Fly}inherited;',
  20903. ' inherited {@TBird_Fly}Fly;',
  20904. 'end;',
  20905. 'procedure Tbirdhelper.walk(w: word);',
  20906. 'begin',
  20907. 'end;',
  20908. 'procedure teagleHelper.fly;',
  20909. 'begin',
  20910. ' {@TBird_Fly}inherited;',
  20911. ' inherited {@TBird_Fly}Fly;',
  20912. 'end;',
  20913. 'procedure teagleHelper.walk(w: word);',
  20914. 'begin',
  20915. ' {@TBirdHelper_Walk}inherited;',
  20916. ' inherited {@TBirdHelper_Walk}Walk(3);',
  20917. 'end;',
  20918. 'begin',
  20919. '']);
  20920. ConvertProgram;
  20921. CheckSource('TestClassHelper_InheritedObjFPC',
  20922. LinesToStr([ // statements
  20923. 'rtl.createClass(this, "TObject", null, function () {',
  20924. ' this.$init = function () {',
  20925. ' };',
  20926. ' this.$final = function () {',
  20927. ' };',
  20928. ' this.Fly = function () {',
  20929. ' };',
  20930. '});',
  20931. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  20932. ' this.Fly = function () {',
  20933. ' $mod.TObject.Fly.call(this);',
  20934. ' $mod.TObject.Fly.call(this);',
  20935. ' };',
  20936. '});',
  20937. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20938. ' this.Fly$1 = function () {',
  20939. ' $mod.TObjHelper.Fly.call(this);',
  20940. ' $mod.TObjHelper.Fly.call(this);',
  20941. ' };',
  20942. '});',
  20943. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  20944. ' this.Fly = function () {',
  20945. ' $mod.TBird.Fly$1.call(this);',
  20946. ' $mod.TBird.Fly$1.call(this);',
  20947. ' };',
  20948. ' this.Walk = function (w) {',
  20949. ' };',
  20950. '});',
  20951. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  20952. ' this.Fly$1 = function () {',
  20953. ' $mod.TBird.Fly$1.call(this);',
  20954. ' $mod.TBird.Fly$1.call(this);',
  20955. ' };',
  20956. ' this.Walk$1 = function (w) {',
  20957. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20958. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20959. ' };',
  20960. '});',
  20961. '']),
  20962. LinesToStr([ // $mod.$main
  20963. '']));
  20964. end;
  20965. procedure TTestModule.TestClassHelper_Property;
  20966. begin
  20967. StartProgram(false);
  20968. Add([
  20969. 'type',
  20970. ' TObject = class',
  20971. ' FSize: word;',
  20972. ' function GetSpeed: word;',
  20973. ' procedure SetSpeed(Value: word);',
  20974. ' end;',
  20975. ' TObjHelper = class helper for TObject',
  20976. ' function GetLeft: word;',
  20977. ' procedure SetLeft(Value: word);',
  20978. ' property Size: word read FSize write FSize;',
  20979. ' property Speed: word read GetSpeed write SetSpeed;',
  20980. ' property Left: word read GetLeft write SetLeft;',
  20981. ' end;',
  20982. ' TBird = class',
  20983. ' property NotRight: word read GetLeft write SetLeft;',
  20984. ' procedure DoIt;',
  20985. ' end;',
  20986. 'var',
  20987. ' b: TBird;',
  20988. 'function Tobject.GetSpeed: word;',
  20989. 'begin',
  20990. ' Size:=Size+11;',
  20991. ' Speed:=Speed+12;',
  20992. ' Result:=Left+13;',
  20993. ' Left:=13;',
  20994. ' Left:=Left+13;',
  20995. ' Self.Size:=Self.Size+21;',
  20996. ' Self.Speed:=Self.Speed+22;',
  20997. ' Self.Left:=Self.Left+23;',
  20998. ' with Self do begin',
  20999. ' Size:=Size+31;',
  21000. ' Speed:=Speed+32;',
  21001. ' Left:=Left+33;',
  21002. ' end;',
  21003. 'end;',
  21004. 'procedure Tobject.SetSpeed(Value: word);',
  21005. 'begin',
  21006. 'end;',
  21007. 'function TObjHelper.GetLeft: word;',
  21008. 'begin',
  21009. ' Size:=Size+11;',
  21010. ' Speed:=Speed+12;',
  21011. ' Left:=Left+13;',
  21012. ' Self.Size:=Self.Size+21;',
  21013. ' Self.Speed:=Self.Speed+22;',
  21014. ' Self.Left:=Self.Left+23;',
  21015. ' with Self do begin',
  21016. ' Size:=Size+31;',
  21017. ' Speed:=Speed+32;',
  21018. ' Left:=Left+33;',
  21019. ' end;',
  21020. 'end;',
  21021. 'procedure TObjHelper.SetLeft(Value: word);',
  21022. 'begin',
  21023. 'end;',
  21024. 'procedure TBird.DoIt;',
  21025. 'begin',
  21026. ' NotRight:=NotRight+11;',
  21027. ' Self.NotRight:=Self.NotRight+21;',
  21028. ' with Self do begin',
  21029. ' NotRight:=NotRight+31;',
  21030. ' end;',
  21031. 'end;',
  21032. 'begin',
  21033. ' b.Size:=b.Size+11;',
  21034. ' b.Speed:=b.Speed+12;',
  21035. ' b.Left:=b.Left+13;',
  21036. ' b.NotRight:=b.NotRight+14;',
  21037. ' with b do begin',
  21038. ' Size:=Size+31;',
  21039. ' Speed:=Speed+32;',
  21040. ' Left:=Left+33;',
  21041. ' NotRight:=NotRight+34;',
  21042. ' end;',
  21043. '']);
  21044. ConvertProgram;
  21045. CheckSource('TestClassHelper_Property',
  21046. LinesToStr([ // statements
  21047. 'rtl.createClass(this, "TObject", null, function () {',
  21048. ' this.$init = function () {',
  21049. ' this.FSize = 0;',
  21050. ' };',
  21051. ' this.$final = function () {',
  21052. ' };',
  21053. ' this.GetSpeed = function () {',
  21054. ' var Result = 0;',
  21055. ' this.FSize = this.FSize + 11;',
  21056. ' this.SetSpeed(this.GetSpeed() + 12);',
  21057. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21058. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21059. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21060. ' this.FSize = this.FSize + 21;',
  21061. ' this.SetSpeed(this.GetSpeed() + 22);',
  21062. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21063. ' this.FSize = this.FSize + 31;',
  21064. ' this.SetSpeed(this.GetSpeed() + 32);',
  21065. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21066. ' return Result;',
  21067. ' };',
  21068. ' this.SetSpeed = function (Value) {',
  21069. ' };',
  21070. '});',
  21071. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21072. ' this.GetLeft = function () {',
  21073. ' var Result = 0;',
  21074. ' this.FSize = this.FSize + 11;',
  21075. ' this.SetSpeed(this.GetSpeed() + 12);',
  21076. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21077. ' this.FSize = this.FSize + 21;',
  21078. ' this.SetSpeed(this.GetSpeed() + 22);',
  21079. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21080. ' this.FSize = this.FSize + 31;',
  21081. ' this.SetSpeed(this.GetSpeed() + 32);',
  21082. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21083. ' return Result;',
  21084. ' };',
  21085. ' this.SetLeft = function (Value) {',
  21086. ' };',
  21087. '});',
  21088. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21089. ' this.DoIt = function () {',
  21090. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21091. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21092. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21093. ' };',
  21094. '});',
  21095. 'this.b = null;',
  21096. '']),
  21097. LinesToStr([ // $mod.$main
  21098. '$mod.b.FSize = $mod.b.FSize + 11;',
  21099. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21100. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21101. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21102. 'var $with = $mod.b;',
  21103. '$with.FSize = $with.FSize + 31;',
  21104. '$with.SetSpeed($with.GetSpeed() + 32);',
  21105. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21106. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21107. '']));
  21108. end;
  21109. procedure TTestModule.TestClassHelper_Property_Array;
  21110. begin
  21111. StartProgram(false);
  21112. Add([
  21113. 'type',
  21114. ' TObject = class',
  21115. ' function GetSpeed(Index: boolean): word;',
  21116. ' procedure SetSpeed(Index: boolean; Value: word);',
  21117. ' end;',
  21118. ' TObjHelper = class helper for TObject',
  21119. ' function GetSize(Index: boolean): word;',
  21120. ' procedure SetSize(Index: boolean; Value: word);',
  21121. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21122. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21123. ' end;',
  21124. ' TBird = class',
  21125. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21126. ' procedure DoIt;',
  21127. ' end;',
  21128. 'var',
  21129. ' b: TBird;',
  21130. 'function Tobject.GetSpeed(Index: boolean): word;',
  21131. 'begin',
  21132. ' Result:=Size[false];',
  21133. ' Size[true]:=Size[false]+11;',
  21134. ' Speed[true]:=Speed[false]+12;',
  21135. ' Self.Size[true]:=Self.Size[false]+21;',
  21136. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21137. ' with Self do begin',
  21138. ' Size[true]:=Size[false]+31;',
  21139. ' Speed[true]:=Speed[false]+32;',
  21140. ' end;',
  21141. 'end;',
  21142. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21143. 'begin',
  21144. 'end;',
  21145. 'function TObjHelper.GetSize(Index: boolean): word;',
  21146. 'begin',
  21147. ' Size[true]:=Size[false]+11;',
  21148. ' Speed[true]:=Speed[false]+12;',
  21149. ' Self.Size[true]:=Self.Size[false]+21;',
  21150. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21151. ' with Self do begin',
  21152. ' Size[true]:=Size[false]+31;',
  21153. ' Speed[true]:=Speed[false]+32;',
  21154. ' end;',
  21155. 'end;',
  21156. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21157. 'begin',
  21158. 'end;',
  21159. 'procedure TBird.DoIt;',
  21160. 'begin',
  21161. ' Items[true]:=Items[false]+11;',
  21162. ' Self.Items[true]:=Self.Items[false]+21;',
  21163. ' with Self do Items[true]:=Items[false]+31;',
  21164. 'end;',
  21165. 'begin',
  21166. ' b.Size[true]:=b.Size[false]+11;',
  21167. ' b.Speed[true]:=b.Speed[false]+12;',
  21168. ' b.Items[true]:=b.Items[false]+13;',
  21169. ' with b do begin',
  21170. ' Size[true]:=Size[false]+21;',
  21171. ' Speed[true]:=Speed[false]+22;',
  21172. ' Items[true]:=Items[false]+23;',
  21173. ' end;',
  21174. '']);
  21175. ConvertProgram;
  21176. CheckSource('TestClassHelper_Property_Array',
  21177. LinesToStr([ // statements
  21178. 'rtl.createClass(this, "TObject", null, function () {',
  21179. ' this.$init = function () {',
  21180. ' };',
  21181. ' this.$final = function () {',
  21182. ' };',
  21183. ' this.GetSpeed = function (Index) {',
  21184. ' var Result = 0;',
  21185. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21186. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21187. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21188. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21189. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21190. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21191. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21192. ' return Result;',
  21193. ' };',
  21194. ' this.SetSpeed = function (Index, Value) {',
  21195. ' };',
  21196. '});',
  21197. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21198. ' this.GetSize = function (Index) {',
  21199. ' var Result = 0;',
  21200. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21201. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21202. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21203. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21204. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21205. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21206. ' return Result;',
  21207. ' };',
  21208. ' this.SetSize = function (Index, Value) {',
  21209. ' };',
  21210. '});',
  21211. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21212. ' this.DoIt = function () {',
  21213. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21214. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21215. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21216. ' };',
  21217. '});',
  21218. 'this.b = null;',
  21219. '']),
  21220. LinesToStr([ // $mod.$main
  21221. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21222. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21223. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21224. 'var $with = $mod.b;',
  21225. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21226. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21227. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21228. '']));
  21229. end;
  21230. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21231. begin
  21232. StartProgram(false);
  21233. Add([
  21234. 'type',
  21235. ' TObject = class',
  21236. ' function GetSpeed(Index: boolean): word;',
  21237. ' procedure SetSpeed(Index: boolean; Value: word);',
  21238. ' end;',
  21239. ' TObjHelper = class helper for TObject',
  21240. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21241. ' end;',
  21242. ' TBird = class',
  21243. ' end;',
  21244. ' TBirdHelper = class helper for TBird',
  21245. ' function GetSize(Index: word): boolean;',
  21246. ' procedure SetSize(Index: word; Value: boolean);',
  21247. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21248. ' end;',
  21249. 'function Tobject.GetSpeed(Index: boolean): word;',
  21250. 'begin',
  21251. ' Self[true]:=Self[false]+1;',
  21252. 'end;',
  21253. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21254. 'begin',
  21255. 'end;',
  21256. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21257. 'begin',
  21258. ' Self[1]:=not Self[2];',
  21259. 'end;',
  21260. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21261. 'begin',
  21262. 'end;',
  21263. 'var',
  21264. ' o: TObject;',
  21265. ' b: TBird;',
  21266. 'begin',
  21267. ' o[true]:=o[false]+1;',
  21268. ' b[3]:=not b[4];',
  21269. '']);
  21270. ConvertProgram;
  21271. CheckSource('TestClassHelper_Property_Array_Default',
  21272. LinesToStr([ // statements
  21273. 'rtl.createClass(this, "TObject", null, function () {',
  21274. ' this.$init = function () {',
  21275. ' };',
  21276. ' this.$final = function () {',
  21277. ' };',
  21278. ' this.GetSpeed = function (Index) {',
  21279. ' var Result = 0;',
  21280. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21281. ' return Result;',
  21282. ' };',
  21283. ' this.SetSpeed = function (Index, Value) {',
  21284. ' };',
  21285. '});',
  21286. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21287. '});',
  21288. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21289. '});',
  21290. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21291. ' this.GetSize = function (Index) {',
  21292. ' var Result = false;',
  21293. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21294. ' return Result;',
  21295. ' };',
  21296. ' this.SetSize = function (Index, Value) {',
  21297. ' };',
  21298. '});',
  21299. 'this.o = null;',
  21300. 'this.b = null;',
  21301. '']),
  21302. LinesToStr([ // $mod.$main
  21303. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21304. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21305. '']));
  21306. end;
  21307. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21308. begin
  21309. StartProgram(false);
  21310. Add([
  21311. 'type',
  21312. ' TObject = class',
  21313. ' end;',
  21314. ' TObjHelper = class helper for TObject',
  21315. ' function GetItems(Index: word): TObject;',
  21316. ' procedure SetItems(Index: word; Value: TObject);',
  21317. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21318. ' end;',
  21319. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21320. 'begin',
  21321. ' Self[1][2]:=Self[3][4];',
  21322. 'end;',
  21323. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21324. 'begin',
  21325. 'end;',
  21326. 'var',
  21327. ' o: TObject;',
  21328. 'begin',
  21329. ' o[1][2]:=o[3][4];',
  21330. '']);
  21331. ConvertProgram;
  21332. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21333. LinesToStr([ // statements
  21334. 'rtl.createClass(this, "TObject", null, function () {',
  21335. ' this.$init = function () {',
  21336. ' };',
  21337. ' this.$final = function () {',
  21338. ' };',
  21339. '});',
  21340. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21341. ' this.GetItems = function (Index) {',
  21342. ' var Result = null;',
  21343. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21344. ' return Result;',
  21345. ' };',
  21346. ' this.SetItems = function (Index, Value) {',
  21347. ' };',
  21348. '});',
  21349. 'this.o = null;',
  21350. '']),
  21351. LinesToStr([ // $mod.$main
  21352. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21353. '']));
  21354. end;
  21355. procedure TTestModule.TestClassHelper_ClassProperty;
  21356. begin
  21357. StartProgram(false);
  21358. Add([
  21359. 'type',
  21360. ' TObject = class',
  21361. ' class var FSize: word;',
  21362. ' class function GetSpeed: word;',
  21363. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21364. ' end;',
  21365. ' TObjHelper = class helper for TObject',
  21366. ' class function GetLeft: word;',
  21367. ' class procedure SetLeft(Value: word);',
  21368. ' class property Size: word read FSize write FSize;',
  21369. ' class property Speed: word read GetSpeed write SetSpeed;',
  21370. ' class property Left: word read GetLeft write SetLeft;',
  21371. ' end;',
  21372. ' TBird = class',
  21373. ' class property NotRight: word read GetLeft write SetLeft;',
  21374. ' class procedure DoIt;',
  21375. ' end;',
  21376. ' TBirdClass = class of TBird;',
  21377. 'class function Tobject.GetSpeed: word;',
  21378. 'begin',
  21379. ' Size:=Size+11;',
  21380. ' Speed:=Speed+12;',
  21381. ' Left:=Left+13;',
  21382. ' Self.Size:=Self.Size+21;',
  21383. ' Self.Speed:=Self.Speed+22;',
  21384. ' Self.Left:=Self.Left+23;',
  21385. ' with Self do begin',
  21386. ' Size:=Size+31;',
  21387. ' Speed:=Speed+32;',
  21388. ' Left:=Left+33;',
  21389. ' end;',
  21390. 'end;',
  21391. 'class function TObjHelper.GetLeft: word;',
  21392. 'begin',
  21393. ' Size:=Size+11;',
  21394. ' Speed:=Speed+12;',
  21395. ' Left:=Left+13;',
  21396. ' Self.Size:=Self.Size+21;',
  21397. ' Self.Speed:=Self.Speed+22;',
  21398. ' Self.Left:=Self.Left+23;',
  21399. ' with Self do begin',
  21400. ' Size:=Size+31;',
  21401. ' Speed:=Speed+32;',
  21402. ' Left:=Left+33;',
  21403. ' end;',
  21404. 'end;',
  21405. 'class procedure TObjHelper.SetLeft(Value: word);',
  21406. 'begin',
  21407. 'end;',
  21408. 'class procedure TBird.DoIt;',
  21409. 'begin',
  21410. ' NotRight:=NotRight+11;',
  21411. ' Self.NotRight:=Self.NotRight+21;',
  21412. ' with Self do NotRight:=NotRight+31;',
  21413. 'end;',
  21414. 'var',
  21415. ' b: TBird;',
  21416. ' c: TBirdClass;',
  21417. 'begin',
  21418. ' b.Size:=b.Size+11;',
  21419. ' b.Speed:=b.Speed+12;',
  21420. ' b.Left:=b.Left+13;',
  21421. ' b.NotRight:=b.NotRight+14;',
  21422. ' with b do begin',
  21423. ' Size:=Size+31;',
  21424. ' Speed:=Speed+32;',
  21425. ' Left:=Left+33;',
  21426. ' NotRight:=NotRight+34;',
  21427. ' end;',
  21428. ' c.Size:=c.Size+11;',
  21429. ' c.Speed:=c.Speed+12;',
  21430. ' c.Left:=c.Left+13;',
  21431. ' c.NotRight:=c.NotRight+14;',
  21432. ' with c do begin',
  21433. ' Size:=Size+31;',
  21434. ' Speed:=Speed+32;',
  21435. ' Left:=Left+33;',
  21436. ' NotRight:=NotRight+34;',
  21437. ' end;',
  21438. ' tbird.Size:=tbird.Size+11;',
  21439. ' tbird.Speed:=tbird.Speed+12;',
  21440. ' tbird.Left:=tbird.Left+13;',
  21441. ' tbird.NotRight:=tbird.NotRight+14;',
  21442. ' with tbird do begin',
  21443. ' Size:=Size+31;',
  21444. ' Speed:=Speed+32;',
  21445. ' Left:=Left+33;',
  21446. ' NotRight:=NotRight+34;',
  21447. ' end;',
  21448. '']);
  21449. ConvertProgram;
  21450. CheckSource('TestClassHelper_ClassProperty',
  21451. LinesToStr([ // statements
  21452. 'rtl.createClass(this, "TObject", null, function () {',
  21453. ' this.FSize = 0;',
  21454. ' this.$init = function () {',
  21455. ' };',
  21456. ' this.$final = function () {',
  21457. ' };',
  21458. ' this.GetSpeed = function () {',
  21459. ' var Result = 0;',
  21460. ' $mod.TObject.FSize = this.FSize + 11;',
  21461. ' this.SetSpeed(this.GetSpeed() + 12);',
  21462. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21463. ' $mod.TObject.FSize = this.FSize + 21;',
  21464. ' this.SetSpeed(this.GetSpeed() + 22);',
  21465. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21466. ' $mod.TObject.FSize = this.FSize + 31;',
  21467. ' this.SetSpeed(this.GetSpeed() + 32);',
  21468. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21469. ' return Result;',
  21470. ' };',
  21471. '});',
  21472. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21473. ' this.GetLeft = function () {',
  21474. ' var Result = 0;',
  21475. ' $mod.TObject.FSize = this.FSize + 11;',
  21476. ' this.SetSpeed(this.GetSpeed() + 12);',
  21477. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21478. ' $mod.TObject.FSize = this.FSize + 21;',
  21479. ' this.SetSpeed(this.GetSpeed() + 22);',
  21480. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21481. ' $mod.TObject.FSize = this.FSize + 31;',
  21482. ' this.SetSpeed(this.GetSpeed() + 32);',
  21483. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21484. ' return Result;',
  21485. ' };',
  21486. ' this.SetLeft = function (Value) {',
  21487. ' };',
  21488. '});',
  21489. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21490. ' this.DoIt = function () {',
  21491. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21492. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21493. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21494. ' };',
  21495. '});',
  21496. 'this.b = null;',
  21497. 'this.c = null;',
  21498. '']),
  21499. LinesToStr([ // $mod.$main
  21500. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21501. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21502. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21503. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21504. 'var $with = $mod.b;',
  21505. '$mod.TObject.FSize = $with.FSize + 31;',
  21506. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21507. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21508. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21509. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21510. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21511. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21512. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21513. 'var $with1 = $mod.c;',
  21514. '$mod.TObject.FSize = $with1.FSize + 31;',
  21515. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21516. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21517. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21518. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21519. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21520. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21521. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21522. 'var $with2 = $mod.TBird;',
  21523. '$mod.TObject.FSize = $with2.FSize + 31;',
  21524. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21525. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21526. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21527. '']));
  21528. end;
  21529. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21530. begin
  21531. StartProgram(false);
  21532. Add([
  21533. 'type',
  21534. ' TObject = class',
  21535. ' class function GetSpeed: word; static;',
  21536. ' class procedure SetSpeed(Value: word); static;',
  21537. ' end;',
  21538. ' TObjHelper = class helper for TObject',
  21539. ' class function GetLeft: word; static;',
  21540. ' class procedure SetLeft(Value: word); static;',
  21541. ' class property Speed: word read GetSpeed write SetSpeed;',
  21542. ' class property Left: word read GetLeft write SetLeft;',
  21543. ' end;',
  21544. ' TBird = class',
  21545. ' class property NotRight: word read GetLeft write SetLeft;',
  21546. ' class procedure DoIt; static;',
  21547. ' class procedure DoSome;',
  21548. ' end;',
  21549. ' TBirdClass = class of TBird;',
  21550. 'class function Tobject.GetSpeed: word;',
  21551. 'begin',
  21552. ' Speed:=Speed+12;',
  21553. ' Left:=Left+13;',
  21554. 'end;',
  21555. 'class procedure TObject.SetSpeed(Value: word);',
  21556. 'begin',
  21557. 'end;',
  21558. 'class function TObjHelper.GetLeft: word;',
  21559. 'begin',
  21560. ' Speed:=Speed+12;',
  21561. ' Left:=Left+13;',
  21562. 'end;',
  21563. 'class procedure TObjHelper.SetLeft(Value: word);',
  21564. 'begin',
  21565. 'end;',
  21566. 'class procedure TBird.DoIt;',
  21567. 'begin',
  21568. ' NotRight:=NotRight+11;',
  21569. 'end;',
  21570. 'class procedure TBird.DoSome;',
  21571. 'begin',
  21572. ' Speed:=Speed+12;',
  21573. ' Left:=Left+13;',
  21574. ' Self.Speed:=Self.Speed+22;',
  21575. ' Self.Left:=Self.Left+23;',
  21576. ' with Self do begin',
  21577. ' Speed:=Speed+32;',
  21578. ' Left:=Left+33;',
  21579. ' end;',
  21580. ' NotRight:=NotRight+11;',
  21581. ' Self.NotRight:=Self.NotRight+21;',
  21582. ' with Self do NotRight:=NotRight+31;',
  21583. 'end;',
  21584. 'var',
  21585. ' b: TBird;',
  21586. ' c: TBirdClass;',
  21587. 'begin',
  21588. ' b.Speed:=b.Speed+12;',
  21589. ' b.Left:=b.Left+13;',
  21590. ' b.NotRight:=b.NotRight+14;',
  21591. ' with b do begin',
  21592. ' Speed:=Speed+32;',
  21593. ' Left:=Left+33;',
  21594. ' NotRight:=NotRight+34;',
  21595. ' end;',
  21596. ' c.Speed:=c.Speed+12;',
  21597. ' c.Left:=c.Left+13;',
  21598. ' c.NotRight:=c.NotRight+14;',
  21599. ' with c do begin',
  21600. ' Speed:=Speed+32;',
  21601. ' Left:=Left+33;',
  21602. ' NotRight:=NotRight+34;',
  21603. ' end;',
  21604. ' tbird.Speed:=tbird.Speed+12;',
  21605. ' tbird.Left:=tbird.Left+13;',
  21606. ' tbird.NotRight:=tbird.NotRight+14;',
  21607. ' with tbird do begin',
  21608. ' Speed:=Speed+32;',
  21609. ' Left:=Left+33;',
  21610. ' NotRight:=NotRight+34;',
  21611. ' end;',
  21612. '']);
  21613. ConvertProgram;
  21614. CheckSource('TestClassHelper_ClassPropertyStatic',
  21615. LinesToStr([ // statements
  21616. 'rtl.createClass(this, "TObject", null, function () {',
  21617. ' this.$init = function () {',
  21618. ' };',
  21619. ' this.$final = function () {',
  21620. ' };',
  21621. ' this.GetSpeed = function () {',
  21622. ' var Result = 0;',
  21623. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21624. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21625. ' return Result;',
  21626. ' };',
  21627. ' this.SetSpeed = function (Value) {',
  21628. ' };',
  21629. '});',
  21630. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21631. ' this.GetLeft = function () {',
  21632. ' var Result = 0;',
  21633. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21634. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21635. ' return Result;',
  21636. ' };',
  21637. ' this.SetLeft = function (Value) {',
  21638. ' };',
  21639. '});',
  21640. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21641. ' this.DoIt = function () {',
  21642. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21643. ' };',
  21644. ' this.DoSome = function () {',
  21645. ' this.SetSpeed(this.GetSpeed() + 12);',
  21646. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21647. ' this.SetSpeed(this.GetSpeed() + 22);',
  21648. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21649. ' this.SetSpeed(this.GetSpeed() + 32);',
  21650. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21651. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21652. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21653. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21654. ' };',
  21655. '});',
  21656. 'this.b = null;',
  21657. 'this.c = null;',
  21658. '']),
  21659. LinesToStr([ // $mod.$main
  21660. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21661. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21662. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21663. 'var $with = $mod.b;',
  21664. '$with.SetSpeed($with.GetSpeed() + 32);',
  21665. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21666. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21667. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21668. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21669. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21670. 'var $with1 = $mod.c;',
  21671. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21672. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21673. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21674. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21675. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21676. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21677. 'var $with2 = $mod.TBird;',
  21678. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21679. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21680. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21681. '']));
  21682. end;
  21683. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21684. begin
  21685. StartProgram(false);
  21686. Add([
  21687. 'type',
  21688. ' TObject = class',
  21689. ' class function GetSpeed(Index: boolean): word;',
  21690. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21691. ' end;',
  21692. ' TObjHelper = class helper for TObject',
  21693. ' class function GetSize(Index: boolean): word;',
  21694. ' class procedure SetSize(Index: boolean; Value: word);',
  21695. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21696. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21697. ' end;',
  21698. ' TBird = class',
  21699. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21700. ' class procedure DoIt;',
  21701. ' end;',
  21702. ' TBirdClass = class of TBird;',
  21703. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21704. 'begin',
  21705. ' Size[true]:=Size[false]+11;',
  21706. ' Speed[true]:=Speed[false]+12;',
  21707. ' Self.Size[true]:=Self.Size[false]+21;',
  21708. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21709. ' with Self do begin',
  21710. ' Size[true]:=Size[false]+31;',
  21711. ' Speed[true]:=Speed[false]+32;',
  21712. ' end;',
  21713. 'end;',
  21714. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21715. 'begin',
  21716. ' Size[true]:=Size[false]+11;',
  21717. ' Speed[true]:=Speed[false]+12;',
  21718. ' Self.Size[true]:=Self.Size[false]+21;',
  21719. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21720. ' with Self do begin',
  21721. ' Size[true]:=Size[false]+31;',
  21722. ' Speed[true]:=Speed[false]+32;',
  21723. ' end;',
  21724. 'end;',
  21725. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21726. 'begin',
  21727. 'end;',
  21728. 'class procedure TBird.DoIt;',
  21729. 'begin',
  21730. ' Items[true]:=Items[false]+11;',
  21731. ' Self.Items[true]:=Self.Items[false]+21;',
  21732. ' with Self do Items[true]:=Items[false]+31;',
  21733. 'end;',
  21734. 'var',
  21735. ' b: TBird;',
  21736. ' c: TBirdClass;',
  21737. 'begin',
  21738. ' b.Size[true]:=b.Size[false]+11;',
  21739. ' b.Speed[true]:=b.Speed[false]+12;',
  21740. ' b.Items[true]:=b.Items[false]+13;',
  21741. ' with b do begin',
  21742. ' Size[true]:=Size[false]+21;',
  21743. ' Speed[true]:=Speed[false]+22;',
  21744. ' Items[true]:=Items[false]+23;',
  21745. ' end;',
  21746. ' c.Size[true]:=c.Size[false]+11;',
  21747. ' c.Speed[true]:=c.Speed[false]+12;',
  21748. ' c.Items[true]:=c.Items[false]+13;',
  21749. ' with c do begin',
  21750. ' Size[true]:=Size[false]+21;',
  21751. ' Speed[true]:=Speed[false]+22;',
  21752. ' Items[true]:=Items[false]+23;',
  21753. ' end;',
  21754. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21755. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21756. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21757. ' with TBird do begin',
  21758. ' Size[true]:=Size[false]+21;',
  21759. ' Speed[true]:=Speed[false]+22;',
  21760. ' Items[true]:=Items[false]+23;',
  21761. ' end;',
  21762. '']);
  21763. ConvertProgram;
  21764. CheckSource('TestClassHelper_ClassProperty_Array',
  21765. LinesToStr([ // statements
  21766. 'rtl.createClass(this, "TObject", null, function () {',
  21767. ' this.$init = function () {',
  21768. ' };',
  21769. ' this.$final = function () {',
  21770. ' };',
  21771. ' this.GetSpeed = function (Index) {',
  21772. ' var Result = 0;',
  21773. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21774. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21775. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21776. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21777. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21778. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21779. ' return Result;',
  21780. ' };',
  21781. '});',
  21782. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21783. ' this.GetSize = function (Index) {',
  21784. ' var Result = 0;',
  21785. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21786. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21787. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21788. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21789. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21790. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21791. ' return Result;',
  21792. ' };',
  21793. ' this.SetSize = function (Index, Value) {',
  21794. ' };',
  21795. '});',
  21796. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21797. ' this.DoIt = function () {',
  21798. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21799. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21800. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21801. ' };',
  21802. '});',
  21803. 'this.b = null;',
  21804. 'this.c = null;',
  21805. '']),
  21806. LinesToStr([ // $mod.$main
  21807. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21808. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21809. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21810. 'var $with = $mod.b;',
  21811. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  21812. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  21813. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  21814. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21815. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21816. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21817. 'var $with1 = $mod.c;',
  21818. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  21819. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  21820. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  21821. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21822. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21823. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21824. 'var $with2 = $mod.TBird;',
  21825. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21826. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21827. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21828. '']));
  21829. end;
  21830. procedure TTestModule.TestClassHelper_ForIn;
  21831. begin
  21832. StartProgram(false);
  21833. Add([
  21834. 'type',
  21835. ' TObject = class end;',
  21836. ' TItem = TObject;',
  21837. ' TEnumerator = class',
  21838. ' FCurrent: TItem;',
  21839. ' property Current: TItem read FCurrent;',
  21840. ' function MoveNext: boolean;',
  21841. ' end;',
  21842. ' TBird = class',
  21843. ' end;',
  21844. ' TBirdHelper = class helper for TBird',
  21845. ' function GetEnumerator: TEnumerator;',
  21846. ' end;',
  21847. 'function TEnumerator.MoveNext: boolean;',
  21848. 'begin',
  21849. 'end;',
  21850. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21851. 'begin',
  21852. 'end;',
  21853. 'var',
  21854. ' b: TBird;',
  21855. ' i, i2: TItem;',
  21856. 'begin',
  21857. ' for i in b do i2:=i;']);
  21858. ConvertProgram;
  21859. CheckSource('TestClassHelper_ForIn',
  21860. LinesToStr([ // statements
  21861. 'rtl.createClass(this, "TObject", null, function () {',
  21862. ' this.$init = function () {',
  21863. ' };',
  21864. ' this.$final = function () {',
  21865. ' };',
  21866. '});',
  21867. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  21868. ' this.$init = function () {',
  21869. ' $mod.TObject.$init.call(this);',
  21870. ' this.FCurrent = null;',
  21871. ' };',
  21872. ' this.$final = function () {',
  21873. ' this.FCurrent = undefined;',
  21874. ' $mod.TObject.$final.call(this);',
  21875. ' };',
  21876. ' this.MoveNext = function () {',
  21877. ' var Result = false;',
  21878. ' return Result;',
  21879. ' };',
  21880. '});',
  21881. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21882. '});',
  21883. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21884. ' this.GetEnumerator = function () {',
  21885. ' var Result = null;',
  21886. ' return Result;',
  21887. ' };',
  21888. '});',
  21889. 'this.b = null;',
  21890. 'this.i = null;',
  21891. 'this.i2 = null;'
  21892. ]),
  21893. LinesToStr([ // $mod.$main
  21894. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  21895. 'try {',
  21896. ' while ($in.MoveNext()){',
  21897. ' $mod.i = $in.FCurrent;',
  21898. ' $mod.i2 = $mod.i;',
  21899. ' }',
  21900. '} finally {',
  21901. ' $in = rtl.freeLoc($in)',
  21902. '};',
  21903. '']));
  21904. end;
  21905. procedure TTestModule.TestClassHelper_PassProperty;
  21906. begin
  21907. StartProgram(false);
  21908. Add([
  21909. 'type',
  21910. ' TObject = class',
  21911. ' FField: TObject;',
  21912. ' property Field: TObject read FField write FField;',
  21913. ' end;',
  21914. ' THelper = class helper for TObject',
  21915. ' procedure Fly;',
  21916. ' class procedure Run;',
  21917. ' class procedure Jump; static;',
  21918. ' end;',
  21919. 'procedure THelper.Fly;',
  21920. 'begin',
  21921. ' Field.Fly;',
  21922. ' Field.Run;',
  21923. ' Field.Jump;',
  21924. ' with Field do begin',
  21925. ' Fly;',
  21926. ' Run;',
  21927. ' Jump;',
  21928. ' end;',
  21929. 'end;',
  21930. 'class procedure THelper.Run;',
  21931. 'begin',
  21932. 'end;',
  21933. 'class procedure THelper.Jump;',
  21934. 'begin',
  21935. 'end;',
  21936. 'var',
  21937. ' b: TObject;',
  21938. 'begin',
  21939. ' b.Field.Fly;',
  21940. ' b.Field.Run;',
  21941. ' b.Field.Jump;',
  21942. ' with b do begin',
  21943. ' Field.Run;',
  21944. ' Field.Fly;',
  21945. ' Field.Jump;',
  21946. ' end;',
  21947. ' with b.Field do begin',
  21948. ' Run;',
  21949. ' Fly;',
  21950. ' Jump;',
  21951. ' end;',
  21952. '']);
  21953. ConvertProgram;
  21954. CheckSource('TestClassHelper_PassProperty',
  21955. LinesToStr([ // statements
  21956. 'rtl.createClass(this, "TObject", null, function () {',
  21957. ' this.$init = function () {',
  21958. ' this.FField = null;',
  21959. ' };',
  21960. ' this.$final = function () {',
  21961. ' this.FField = undefined;',
  21962. ' };',
  21963. '});',
  21964. 'rtl.createHelper(this, "THelper", null, function () {',
  21965. ' this.Fly = function () {',
  21966. ' $mod.THelper.Fly.call(this.FField);',
  21967. ' $mod.THelper.Run.call(this.FField.$class);',
  21968. ' $mod.THelper.Jump();',
  21969. ' var $with = this.FField;',
  21970. ' $mod.THelper.Fly.call($with);',
  21971. ' $mod.THelper.Run.call($with.$class);',
  21972. ' $mod.THelper.Jump();',
  21973. ' };',
  21974. ' this.Run = function () {',
  21975. ' };',
  21976. ' this.Jump = function () {',
  21977. ' };',
  21978. '});',
  21979. 'this.b = null;',
  21980. '']),
  21981. LinesToStr([ // $mod.$main
  21982. '$mod.THelper.Fly.call($mod.b.FField);',
  21983. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21984. '$mod.THelper.Jump();',
  21985. 'var $with = $mod.b;',
  21986. '$mod.THelper.Run.call($with.FField.$class);',
  21987. '$mod.THelper.Fly.call($with.FField);',
  21988. '$mod.THelper.Jump();',
  21989. 'var $with1 = $mod.b.FField;',
  21990. '$mod.THelper.Run.call($with1.$class);',
  21991. '$mod.THelper.Fly.call($with1);',
  21992. '$mod.THelper.Jump();',
  21993. '']));
  21994. end;
  21995. procedure TTestModule.TestExtClassHelper_ClassVar;
  21996. begin
  21997. StartProgram(false);
  21998. Add([
  21999. '{$modeswitch externalclass}',
  22000. 'type',
  22001. ' TExtA = class external name ''ExtObj''',
  22002. ' end;',
  22003. ' THelper = class helper for TExtA',
  22004. ' const',
  22005. ' One = 1;',
  22006. ' Two: word = 2;',
  22007. ' class var',
  22008. ' Glob: word;',
  22009. ' function Foo(w: word): word;',
  22010. ' class function Bar(w: word): word; static;',
  22011. ' end;',
  22012. 'function THelper.foo(w: word): word;',
  22013. 'begin',
  22014. ' Result:=w;',
  22015. ' Two:=One+w;',
  22016. ' Glob:=Glob;',
  22017. ' Result:=Self.Glob;',
  22018. ' Self.Glob:=Self.Glob;',
  22019. ' with Self do Glob:=Glob;',
  22020. 'end;',
  22021. 'class function THelper.bar(w: word): word;',
  22022. 'begin',
  22023. ' Result:=w;',
  22024. ' Two:=One;',
  22025. ' Glob:=Glob;',
  22026. 'end;',
  22027. 'var o: TExtA;',
  22028. 'begin',
  22029. ' texta.two:=texta.one;',
  22030. ' texta.Glob:=texta.Glob;',
  22031. ' with texta do begin',
  22032. ' two:=one;',
  22033. ' Glob:=Glob;',
  22034. ' end;',
  22035. ' o.two:=o.one;',
  22036. ' o.Glob:=o.Glob;',
  22037. ' with o do begin',
  22038. ' two:=one;',
  22039. ' Glob:=Glob;',
  22040. ' end;',
  22041. '']);
  22042. ConvertProgram;
  22043. CheckSource('TestExtClassHelper_ClassVar',
  22044. LinesToStr([ // statements
  22045. 'rtl.createHelper(this, "THelper", null, function () {',
  22046. ' this.One = 1;',
  22047. ' this.Two = 2;',
  22048. ' this.Glob = 0;',
  22049. ' this.Foo = function (w) {',
  22050. ' var Result = 0;',
  22051. ' Result = w;',
  22052. ' $mod.THelper.Two = 1 + w;',
  22053. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22054. ' Result = $mod.THelper.Glob;',
  22055. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22056. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22057. ' return Result;',
  22058. ' };',
  22059. ' this.Bar = function (w) {',
  22060. ' var Result = 0;',
  22061. ' Result = w;',
  22062. ' $mod.THelper.Two = 1;',
  22063. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22064. ' return Result;',
  22065. ' };',
  22066. '});',
  22067. 'this.o = null;',
  22068. '']),
  22069. LinesToStr([ // $mod.$main
  22070. '$mod.THelper.Two = 1;',
  22071. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22072. '$mod.THelper.Two = 1;',
  22073. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22074. '$mod.THelper.Two = 1;',
  22075. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22076. 'var $with = $mod.o;',
  22077. '$mod.THelper.Two = 1;',
  22078. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22079. '']));
  22080. end;
  22081. procedure TTestModule.TestExtClassHelper_Method_Call;
  22082. begin
  22083. StartProgram(false);
  22084. Add([
  22085. '{$modeswitch externalclass}',
  22086. 'type',
  22087. ' TFly = function(w: word): word of object;',
  22088. ' TExtA = class external name ''ExtObj''',
  22089. ' procedure Run(w: word = 10);',
  22090. ' end;',
  22091. ' THelper = class helper for TExtA',
  22092. ' function Foo(w: word = 1): word;',
  22093. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22094. ' end;',
  22095. 'var p: TFly;',
  22096. 'function THelper.foo(w: word): word;',
  22097. 'begin',
  22098. ' Run;',
  22099. ' Run();',
  22100. ' Run(11);',
  22101. ' Foo;',
  22102. ' Foo();',
  22103. ' Foo(12);',
  22104. ' Self.Foo;',
  22105. ' Self.Foo();',
  22106. ' Self.Foo(13);',
  22107. ' Fly;',
  22108. ' Fly();',
  22109. ' with Self do begin',
  22110. ' Foo;',
  22111. ' Foo();',
  22112. ' Foo(14);',
  22113. ' Fly;',
  22114. ' Fly();',
  22115. ' end;',
  22116. ' p:=@Fly;',
  22117. 'end;',
  22118. 'var Obj: TExtA;',
  22119. 'begin',
  22120. ' obj.Foo;',
  22121. ' obj.Foo();',
  22122. ' obj.Foo(21);',
  22123. ' obj.Fly;',
  22124. ' obj.Fly();',
  22125. ' with obj do begin',
  22126. ' Foo;',
  22127. ' Foo();',
  22128. ' Foo(22);',
  22129. ' Fly;',
  22130. ' Fly();',
  22131. ' end;',
  22132. ' p:[email protected];',
  22133. '']);
  22134. ConvertProgram;
  22135. CheckSource('TestExtClassHelper_Method_Call',
  22136. LinesToStr([ // statements
  22137. 'rtl.createHelper(this, "THelper", null, function () {',
  22138. ' this.Foo = function (w) {',
  22139. ' var Result = 0;',
  22140. ' this.Run(10);',
  22141. ' this.Run(10);',
  22142. ' this.Run(11);',
  22143. ' $mod.THelper.Foo.call(this, 1);',
  22144. ' $mod.THelper.Foo.call(this, 1);',
  22145. ' $mod.THelper.Foo.call(this, 12);',
  22146. ' $mod.THelper.Foo.call(this, 1);',
  22147. ' $mod.THelper.Foo.call(this, 1);',
  22148. ' $mod.THelper.Foo.call(this, 13);',
  22149. ' this.Fly(2);',
  22150. ' this.Fly(2);',
  22151. ' $mod.THelper.Foo.call(this, 1);',
  22152. ' $mod.THelper.Foo.call(this, 1);',
  22153. ' $mod.THelper.Foo.call(this, 14);',
  22154. ' this.Fly(2);',
  22155. ' this.Fly(2);',
  22156. ' $mod.p = rtl.createCallback(this, "Fly");',
  22157. ' return Result;',
  22158. ' };',
  22159. '});',
  22160. 'this.p = null;',
  22161. 'this.Obj = null;',
  22162. '']),
  22163. LinesToStr([ // $mod.$main
  22164. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22165. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22166. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22167. '$mod.Obj.Fly(2);',
  22168. '$mod.Obj.Fly(2);',
  22169. 'var $with = $mod.Obj;',
  22170. '$mod.THelper.Foo.call($with, 1);',
  22171. '$mod.THelper.Foo.call($with, 1);',
  22172. '$mod.THelper.Foo.call($with, 22);',
  22173. '$with.Fly(2);',
  22174. '$with.Fly(2);',
  22175. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22176. '']));
  22177. end;
  22178. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22179. begin
  22180. StartProgram(false);
  22181. Add([
  22182. '{$modeswitch externalclass}',
  22183. 'type',
  22184. ' TExtA = class external name ''ExtObj''',
  22185. ' procedure Run(w: word = 10);',
  22186. ' end;',
  22187. ' THelper = class helper for TExtA',
  22188. ' class procedure Fly;',
  22189. ' end;',
  22190. 'class procedure THelper.Fly;',
  22191. 'begin end;',
  22192. 'begin',
  22193. '']);
  22194. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22195. nHelperClassMethodForExtClassMustBeStatic);
  22196. ConvertProgram;
  22197. end;
  22198. procedure TTestModule.TestRecordHelper_ClassVar;
  22199. begin
  22200. StartProgram(false);
  22201. Add([
  22202. 'type',
  22203. ' TRec = record',
  22204. ' end;',
  22205. ' THelper = record helper for TRec',
  22206. ' const',
  22207. ' One = 1;',
  22208. ' Two: word = 2;',
  22209. ' class var',
  22210. ' Glob: word;',
  22211. ' function Foo(w: word): word;',
  22212. ' class function Bar(w: word): word; static;',
  22213. ' end;',
  22214. 'function THelper.foo(w: word): word;',
  22215. 'begin',
  22216. ' Result:=w;',
  22217. ' Two:=One+w;',
  22218. ' Glob:=Glob;',
  22219. ' Result:=Self.Glob;',
  22220. ' Self.Glob:=Self.Glob;',
  22221. ' with Self do Glob:=Glob;',
  22222. ' Self:=Self;',
  22223. 'end;',
  22224. 'class function THelper.bar(w: word): word;',
  22225. 'begin',
  22226. ' Result:=w;',
  22227. ' Two:=One;',
  22228. ' Glob:=Glob;',
  22229. 'end;',
  22230. 'var r: TRec;',
  22231. 'begin',
  22232. ' trec.two:=trec.one;',
  22233. ' trec.Glob:=trec.Glob;',
  22234. ' with trec do begin',
  22235. ' two:=one;',
  22236. ' Glob:=Glob;',
  22237. ' end;',
  22238. ' r.two:=r.one;',
  22239. ' r.Glob:=r.Glob;',
  22240. ' with r do begin',
  22241. ' two:=one;',
  22242. ' Glob:=Glob;',
  22243. ' end;',
  22244. '']);
  22245. ConvertProgram;
  22246. CheckSource('TestRecordHelper_ClassVar',
  22247. LinesToStr([ // statements
  22248. 'rtl.recNewT(this, "TRec", function () {',
  22249. ' this.$eq = function (b) {',
  22250. ' return true;',
  22251. ' };',
  22252. ' this.$assign = function (s) {',
  22253. ' return this;',
  22254. ' };',
  22255. '});',
  22256. 'rtl.createHelper(this, "THelper", null, function () {',
  22257. ' this.One = 1;',
  22258. ' this.Two = 2;',
  22259. ' this.Glob = 0;',
  22260. ' this.Foo = function (w) {',
  22261. ' var Result = 0;',
  22262. ' Result = w;',
  22263. ' $mod.THelper.Two = 1 + w;',
  22264. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22265. ' Result = $mod.THelper.Glob;',
  22266. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22267. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22268. ' this.$assign(this);',
  22269. ' return Result;',
  22270. ' };',
  22271. ' this.Bar = function (w) {',
  22272. ' var Result = 0;',
  22273. ' Result = w;',
  22274. ' $mod.THelper.Two = 1;',
  22275. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22276. ' return Result;',
  22277. ' };',
  22278. '});',
  22279. 'this.r = this.TRec.$new();',
  22280. '']),
  22281. LinesToStr([ // $mod.$main
  22282. '$mod.THelper.Two = 1;',
  22283. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22284. 'var $with = $mod.TRec;',
  22285. '$mod.THelper.Two = 1;',
  22286. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22287. '$mod.THelper.Two = 1;',
  22288. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22289. 'var $with1 = $mod.r;',
  22290. '$mod.THelper.Two = 1;',
  22291. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22292. '']));
  22293. end;
  22294. procedure TTestModule.TestRecordHelper_Method_Call;
  22295. begin
  22296. StartProgram(false);
  22297. Add([
  22298. '{$modeswitch AdvancedRecords}',
  22299. 'type',
  22300. ' TRec = record',
  22301. ' procedure Run(w: word = 10);',
  22302. ' end;',
  22303. ' THelper = record helper for TRec',
  22304. ' function Foo(w: word = 1): word;',
  22305. ' end;',
  22306. 'procedure TRec.Run(w: word);',
  22307. 'begin',
  22308. ' Foo;',
  22309. ' Foo();',
  22310. ' Foo(2);',
  22311. ' Self.Foo;',
  22312. ' Self.Foo();',
  22313. ' Self.Foo(3);',
  22314. ' with Self do begin',
  22315. ' Foo;',
  22316. ' Foo();',
  22317. ' Foo(4);',
  22318. ' end;',
  22319. 'end;',
  22320. 'function THelper.foo(w: word): word;',
  22321. 'begin',
  22322. ' Run;',
  22323. ' Run();',
  22324. ' Run(11);',
  22325. ' Foo;',
  22326. ' Foo();',
  22327. ' Foo(12);',
  22328. ' Self.Foo;',
  22329. ' Self.Foo();',
  22330. ' Self.Foo(13);',
  22331. ' with Self do begin',
  22332. ' Foo;',
  22333. ' Foo();',
  22334. ' Foo(14);',
  22335. ' end;',
  22336. 'end;',
  22337. 'var Rec: TRec;',
  22338. 'begin',
  22339. ' Rec.Foo;',
  22340. ' Rec.Foo();',
  22341. ' Rec.Foo(21);',
  22342. ' with Rec do begin',
  22343. ' Foo;',
  22344. ' Foo();',
  22345. ' Foo(22);',
  22346. ' end;',
  22347. '']);
  22348. ConvertProgram;
  22349. CheckSource('TestRecordHelper_Method_Call',
  22350. LinesToStr([ // statements
  22351. 'rtl.recNewT(this, "TRec", function () {',
  22352. ' this.$eq = function (b) {',
  22353. ' return true;',
  22354. ' };',
  22355. ' this.$assign = function (s) {',
  22356. ' return this;',
  22357. ' };',
  22358. ' this.Run = function (w) {',
  22359. ' $mod.THelper.Foo.call(this, 1);',
  22360. ' $mod.THelper.Foo.call(this, 1);',
  22361. ' $mod.THelper.Foo.call(this, 2);',
  22362. ' $mod.THelper.Foo.call(this, 1);',
  22363. ' $mod.THelper.Foo.call(this, 1);',
  22364. ' $mod.THelper.Foo.call(this, 3);',
  22365. ' $mod.THelper.Foo.call(this, 1);',
  22366. ' $mod.THelper.Foo.call(this, 1);',
  22367. ' $mod.THelper.Foo.call(this, 4);',
  22368. ' };',
  22369. '});',
  22370. 'rtl.createHelper(this, "THelper", null, function () {',
  22371. ' this.Foo = function (w) {',
  22372. ' var Result = 0;',
  22373. ' this.Run(10);',
  22374. ' this.Run(10);',
  22375. ' this.Run(11);',
  22376. ' $mod.THelper.Foo.call(this, 1);',
  22377. ' $mod.THelper.Foo.call(this, 1);',
  22378. ' $mod.THelper.Foo.call(this, 12);',
  22379. ' $mod.THelper.Foo.call(this, 1);',
  22380. ' $mod.THelper.Foo.call(this, 1);',
  22381. ' $mod.THelper.Foo.call(this, 13);',
  22382. ' $mod.THelper.Foo.call(this, 1);',
  22383. ' $mod.THelper.Foo.call(this, 1);',
  22384. ' $mod.THelper.Foo.call(this, 14);',
  22385. ' return Result;',
  22386. ' };',
  22387. '});',
  22388. 'this.Rec = this.TRec.$new();',
  22389. '']),
  22390. LinesToStr([ // $mod.$main
  22391. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22392. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22393. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22394. 'var $with = $mod.Rec;',
  22395. '$mod.THelper.Foo.call($with, 1);',
  22396. '$mod.THelper.Foo.call($with, 1);',
  22397. '$mod.THelper.Foo.call($with, 22);',
  22398. '']));
  22399. end;
  22400. procedure TTestModule.TestRecordHelper_Constructor;
  22401. begin
  22402. StartProgram(false);
  22403. Add([
  22404. '{$modeswitch AdvancedRecords}',
  22405. 'type',
  22406. ' TRec = record',
  22407. ' constructor Create(w: word);',
  22408. ' end;',
  22409. ' THelper = record helper for TRec',
  22410. ' constructor NewHlp(w: word);',
  22411. ' end;',
  22412. 'var',
  22413. ' Rec: TRec;',
  22414. 'constructor TRec.Create(w: word);',
  22415. 'begin',
  22416. ' NewHlp(2);', // normal call
  22417. ' trec.NewHlp(3);', // new instance
  22418. 'end;',
  22419. 'constructor THelper.NewHlp(w: word);',
  22420. 'begin',
  22421. ' create(2);', // normal call
  22422. ' trec.create(3);', // new instance
  22423. ' NewHlp(4);', // normal call
  22424. ' trec.NewHlp(5);', // new instance
  22425. 'end;',
  22426. 'begin',
  22427. ' rec.newhlp(2);', // normal call
  22428. ' with rec do newhlp(12);', // normal call
  22429. ' trec.newhlp(3);', // new instance
  22430. ' with trec do newhlp(13);', // new instance
  22431. '']);
  22432. ConvertProgram;
  22433. CheckSource('TestRecordHelper_Constructor',
  22434. LinesToStr([ // statements
  22435. 'rtl.recNewT(this, "TRec", function () {',
  22436. ' this.$eq = function (b) {',
  22437. ' return true;',
  22438. ' };',
  22439. ' this.$assign = function (s) {',
  22440. ' return this;',
  22441. ' };',
  22442. ' this.Create = function (w) {',
  22443. ' $mod.THelper.NewHlp.call(this, 2);',
  22444. ' $mod.THelper.$new("NewHlp", [3]);',
  22445. ' return this;',
  22446. ' };',
  22447. '}, true);',
  22448. 'rtl.createHelper(this, "THelper", null, function () {',
  22449. ' this.NewHlp = function (w) {',
  22450. ' this.Create(2);',
  22451. ' $mod.TRec.$new().Create(3);',
  22452. ' $mod.THelper.NewHlp.call(this, 4);',
  22453. ' $mod.THelper.$new("NewHlp", [5]);',
  22454. ' return this;',
  22455. ' };',
  22456. ' this.$new = function (fn, args) {',
  22457. ' return this[fn].apply($mod.TRec.$new(), args);',
  22458. ' };',
  22459. '});',
  22460. 'this.Rec = this.TRec.$new();',
  22461. '']),
  22462. LinesToStr([ // $mod.$main
  22463. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22464. 'var $with = $mod.Rec;',
  22465. '$mod.THelper.NewHlp.call($with, 12);',
  22466. '$mod.THelper.$new("NewHlp", [3]);',
  22467. 'var $with1 = $mod.TRec;',
  22468. '$mod.THelper.$new("NewHlp", [13]);',
  22469. '']));
  22470. end;
  22471. procedure TTestModule.TestTypeHelper_ClassVar;
  22472. begin
  22473. StartProgram(false);
  22474. Add([
  22475. '{$modeswitch typehelpers}',
  22476. 'type',
  22477. ' THelper = type helper for byte',
  22478. ' const',
  22479. ' One = 1;',
  22480. ' Two: word = 2;',
  22481. ' class var',
  22482. ' Glob: word;',
  22483. ' function Foo(w: word): word;',
  22484. ' class function Bar(w: word): word; static;',
  22485. ' end;',
  22486. 'function THelper.foo(w: word): word;',
  22487. 'begin',
  22488. ' Result:=w;',
  22489. ' Two:=One+w;',
  22490. ' Glob:=Glob;',
  22491. ' Result:=Self.Glob;',
  22492. ' Self.Glob:=Self.Glob;',
  22493. ' with Self do Glob:=Glob;',
  22494. 'end;',
  22495. 'class function THelper.bar(w: word): word;',
  22496. 'begin',
  22497. ' Result:=w;',
  22498. ' Two:=One;',
  22499. ' Glob:=Glob;',
  22500. 'end;',
  22501. 'var b: byte;',
  22502. 'begin',
  22503. ' byte.two:=byte.one;',
  22504. ' byte.Glob:=byte.Glob;',
  22505. ' with byte do begin',
  22506. ' two:=one;',
  22507. ' Glob:=Glob;',
  22508. ' end;',
  22509. ' b.two:=b.one;',
  22510. ' b.Glob:=b.Glob;',
  22511. ' with b do begin',
  22512. ' two:=one;',
  22513. ' Glob:=Glob;',
  22514. ' end;',
  22515. '']);
  22516. ConvertProgram;
  22517. CheckSource('TestTypeHelper_ClassVar',
  22518. LinesToStr([ // statements
  22519. 'rtl.createHelper(this, "THelper", null, function () {',
  22520. ' this.One = 1;',
  22521. ' this.Two = 2;',
  22522. ' this.Glob = 0;',
  22523. ' this.Foo = function (w) {',
  22524. ' var Result = 0;',
  22525. ' Result = w;',
  22526. ' $mod.THelper.Two = 1 + w;',
  22527. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22528. ' Result = $mod.THelper.Glob;',
  22529. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22530. ' var $with = this.get();',
  22531. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22532. ' return Result;',
  22533. ' };',
  22534. ' this.Bar = function (w) {',
  22535. ' var Result = 0;',
  22536. ' Result = w;',
  22537. ' $mod.THelper.Two = 1;',
  22538. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22539. ' return Result;',
  22540. ' };',
  22541. '});',
  22542. 'this.b = 0;',
  22543. '']),
  22544. LinesToStr([ // $mod.$main
  22545. '$mod.THelper.Two = 1;',
  22546. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22547. '$mod.THelper.Two = 1;',
  22548. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22549. '$mod.THelper.Two = 1;',
  22550. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22551. 'var $with = $mod.b;',
  22552. '$mod.THelper.Two = 1;',
  22553. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22554. '']));
  22555. end;
  22556. procedure TTestModule.TestTypeHelper_PassResultElement;
  22557. begin
  22558. StartProgram(false);
  22559. Add([
  22560. '{$modeswitch typehelpers}',
  22561. 'type',
  22562. ' THelper = type helper for word',
  22563. ' procedure DoIt(e: byte = 123);',
  22564. ' class procedure DoSome(e: byte = 456); static;',
  22565. ' end;',
  22566. 'procedure THelper.DoIt(e: byte);',
  22567. 'begin',
  22568. 'end;',
  22569. 'class procedure THelper.DoSome(e: byte);',
  22570. 'begin',
  22571. 'end;',
  22572. 'function Foo(w: word): word;',
  22573. 'begin',
  22574. ' Result.DoIt;',
  22575. ' Result.DoIt();',
  22576. ' Result.DoSome;',
  22577. ' Result.DoSome();',
  22578. ' with Result do begin',
  22579. ' DoIt;',
  22580. ' DoIt();',
  22581. ' DoSome;',
  22582. ' DoSome();',
  22583. ' end;',
  22584. 'end;',
  22585. 'begin',
  22586. '']);
  22587. ConvertProgram;
  22588. CheckSource('TestTypeHelper_PassResultElement',
  22589. LinesToStr([ // statements
  22590. 'rtl.createHelper(this, "THelper", null, function () {',
  22591. ' this.DoIt = function (e) {',
  22592. ' };',
  22593. ' this.DoSome = function (e) {',
  22594. ' };',
  22595. '});',
  22596. 'this.Foo = function (w) {',
  22597. ' var Result = 0;',
  22598. ' $mod.THelper.DoIt.call({',
  22599. ' get: function () {',
  22600. ' return Result;',
  22601. ' },',
  22602. ' set: function (v) {',
  22603. ' Result = v;',
  22604. ' }',
  22605. ' }, 123);',
  22606. ' $mod.THelper.DoIt.call({',
  22607. ' get: function () {',
  22608. ' return Result;',
  22609. ' },',
  22610. ' set: function (v) {',
  22611. ' Result = v;',
  22612. ' }',
  22613. ' }, 123);',
  22614. ' $mod.THelper.DoSome(456);',
  22615. ' $mod.THelper.DoSome(456);',
  22616. ' $mod.THelper.DoIt.call({',
  22617. ' get: function () {',
  22618. ' return Result;',
  22619. ' },',
  22620. ' set: function (v) {',
  22621. ' Result = v;',
  22622. ' }',
  22623. ' }, 123);',
  22624. ' $mod.THelper.DoIt.call({',
  22625. ' get: function () {',
  22626. ' return Result;',
  22627. ' },',
  22628. ' set: function (v) {',
  22629. ' Result = v;',
  22630. ' }',
  22631. ' }, 123);',
  22632. ' $mod.THelper.DoSome(456);',
  22633. ' $mod.THelper.DoSome(456);',
  22634. ' return Result;',
  22635. '};',
  22636. '']),
  22637. LinesToStr([ // $mod.$main
  22638. '']));
  22639. end;
  22640. procedure TTestModule.TestTypeHelper_PassArgs;
  22641. begin
  22642. StartProgram(false);
  22643. Add([
  22644. '{$modeswitch typehelpers}',
  22645. 'type',
  22646. ' THelper = type helper for word',
  22647. ' procedure DoIt(e: byte = 123);',
  22648. ' end;',
  22649. 'procedure THelper.DoIt(e: byte);',
  22650. 'begin',
  22651. 'end;',
  22652. 'procedure FooDefault(a: word);',
  22653. 'begin',
  22654. ' a.DoIt;',
  22655. ' with a do DoIt;',
  22656. 'end;',
  22657. 'procedure FooConst(const a: word);',
  22658. 'begin',
  22659. ' a.DoIt;',
  22660. ' with a do DoIt;',
  22661. 'end;',
  22662. 'procedure FooVar(var a: word);',
  22663. 'begin',
  22664. ' a.DoIt;',
  22665. ' with a do DoIt;',
  22666. 'end;',
  22667. 'begin',
  22668. '']);
  22669. ConvertProgram;
  22670. CheckSource('TestTypeHelper_PassArgs',
  22671. LinesToStr([ // statements
  22672. 'rtl.createHelper(this, "THelper", null, function () {',
  22673. ' this.DoIt = function (e) {',
  22674. ' };',
  22675. '});',
  22676. 'this.FooDefault = function (a) {',
  22677. ' $mod.THelper.DoIt.call({',
  22678. ' get: function () {',
  22679. ' return a;',
  22680. ' },',
  22681. ' set: function (v) {',
  22682. ' a = v;',
  22683. ' }',
  22684. ' }, 123);',
  22685. ' $mod.THelper.DoIt.call({',
  22686. ' get: function () {',
  22687. ' return a;',
  22688. ' },',
  22689. ' set: function (v) {',
  22690. ' a = v;',
  22691. ' }',
  22692. ' }, 123);',
  22693. '};',
  22694. 'this.FooConst = function (a) {',
  22695. ' $mod.THelper.DoIt.call({',
  22696. ' get: function () {',
  22697. ' return a;',
  22698. ' },',
  22699. ' set: function (v) {',
  22700. ' rtl.raiseE("EPropReadOnly");',
  22701. ' }',
  22702. ' }, 123);',
  22703. ' $mod.THelper.DoIt.call({',
  22704. ' get: function () {',
  22705. ' return a;',
  22706. ' },',
  22707. ' set: function () {',
  22708. ' rtl.raiseE("EPropReadOnly");',
  22709. ' }',
  22710. ' }, 123);',
  22711. '};',
  22712. 'this.FooVar = function (a) {',
  22713. ' $mod.THelper.DoIt.call(a, 123);',
  22714. ' var $with = a.get();',
  22715. ' $mod.THelper.DoIt.call(a, 123);',
  22716. '};',
  22717. '']),
  22718. LinesToStr([ // $mod.$main
  22719. '']));
  22720. end;
  22721. procedure TTestModule.TestTypeHelper_PassVarConst;
  22722. begin
  22723. StartProgram(false);
  22724. Add([
  22725. '{$modeswitch typehelpers}',
  22726. 'type',
  22727. ' THelper = type helper for word',
  22728. ' procedure DoIt(e: byte = 123);',
  22729. ' end;',
  22730. 'procedure THelper.DoIt(e: byte);',
  22731. 'begin',
  22732. 'end;',
  22733. 'var a: word;',
  22734. 'const c: word = 2;',
  22735. '{$writeableconst off}',
  22736. 'const r: word = 3;',
  22737. 'begin',
  22738. ' a.DoIt;',
  22739. ' with a do DoIt;',
  22740. ' c.DoIt;',
  22741. ' with c do DoIt;',
  22742. ' r.DoIt;',
  22743. ' with r do DoIt;',
  22744. '']);
  22745. ConvertProgram;
  22746. CheckSource('TestTypeHelper_PassVarConst',
  22747. LinesToStr([ // statements
  22748. 'rtl.createHelper(this, "THelper", null, function () {',
  22749. ' this.DoIt = function (e) {',
  22750. ' };',
  22751. '});',
  22752. 'this.a = 0;',
  22753. 'this.c = 2;',
  22754. 'this.r = 3;',
  22755. '']),
  22756. LinesToStr([ // $mod.$main
  22757. '$mod.THelper.DoIt.call({',
  22758. ' p: $mod,',
  22759. ' get: function () {',
  22760. ' return this.p.a;',
  22761. ' },',
  22762. ' set: function (v) {',
  22763. ' this.p.a = v;',
  22764. ' }',
  22765. '}, 123);',
  22766. 'var $with = $mod.a;',
  22767. '$mod.THelper.DoIt.call({',
  22768. ' get: function () {',
  22769. ' return $with;',
  22770. ' },',
  22771. ' set: function (v) {',
  22772. ' $with = v;',
  22773. ' }',
  22774. '}, 123);',
  22775. '$mod.THelper.DoIt.call({',
  22776. ' p: $mod,',
  22777. ' get: function () {',
  22778. ' return this.p.c;',
  22779. ' },',
  22780. ' set: function (v) {',
  22781. ' this.p.c = v;',
  22782. ' }',
  22783. '}, 123);',
  22784. 'var $with1 = $mod.c;',
  22785. '$mod.THelper.DoIt.call({',
  22786. ' get: function () {',
  22787. ' return $with1;',
  22788. ' },',
  22789. ' set: function (v) {',
  22790. ' $with1 = v;',
  22791. ' }',
  22792. '}, 123);',
  22793. '$mod.THelper.DoIt.call({',
  22794. ' get: function () {',
  22795. ' return 3;',
  22796. ' },',
  22797. ' set: function (v) {',
  22798. ' rtl.raiseE("EPropReadOnly");',
  22799. ' }',
  22800. '}, 123);',
  22801. 'var $with2 = 3;',
  22802. ' $mod.THelper.DoIt.call({',
  22803. ' get: function () {',
  22804. ' return $with2;',
  22805. ' },',
  22806. ' set: function () {',
  22807. ' rtl.raiseE("EPropReadOnly");',
  22808. ' }',
  22809. ' }, 123);',
  22810. '']));
  22811. end;
  22812. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22813. begin
  22814. StartProgram(false);
  22815. Add([
  22816. '{$modeswitch typehelpers}',
  22817. 'type',
  22818. ' THelper = type helper for word',
  22819. ' procedure DoIt(e: byte = 123);',
  22820. ' end;',
  22821. 'procedure THelper.DoIt(e: byte);',
  22822. 'begin',
  22823. 'end;',
  22824. 'function Foo(b: byte = 1): word;',
  22825. 'begin',
  22826. 'end;',
  22827. 'begin',
  22828. ' Foo.DoIt;',
  22829. ' Foo().DoIt;',
  22830. ' with Foo do DoIt;',
  22831. ' with Foo() do DoIt;',
  22832. '']);
  22833. ConvertProgram;
  22834. CheckSource('TestTypeHelper_PassFuncResult',
  22835. LinesToStr([ // statements
  22836. 'rtl.createHelper(this, "THelper", null, function () {',
  22837. ' this.DoIt = function (e) {',
  22838. ' };',
  22839. '});',
  22840. 'this.Foo = function (b) {',
  22841. ' var Result = 0;',
  22842. ' return Result;',
  22843. '};',
  22844. '']),
  22845. LinesToStr([ // $mod.$main
  22846. '$mod.THelper.DoIt.call({',
  22847. ' a: $mod.Foo(1),',
  22848. ' get: function () {',
  22849. ' return this.a;',
  22850. ' },',
  22851. ' set: function (v) {',
  22852. ' this.a = v;',
  22853. ' }',
  22854. '}, 123);',
  22855. '$mod.THelper.DoIt.call({',
  22856. ' a: $mod.Foo(1),',
  22857. ' get: function () {',
  22858. ' return this.a;',
  22859. ' },',
  22860. ' set: function (v) {',
  22861. ' this.a = v;',
  22862. ' }',
  22863. '}, 123);',
  22864. 'var $with = $mod.Foo(1);',
  22865. '$mod.THelper.DoIt.call({',
  22866. ' get: function () {',
  22867. ' return $with;',
  22868. ' },',
  22869. ' set: function (v) {',
  22870. ' $with = v;',
  22871. ' }',
  22872. '}, 123);',
  22873. 'var $with1 = $mod.Foo(1);',
  22874. '$mod.THelper.DoIt.call({',
  22875. ' get: function () {',
  22876. ' return $with1;',
  22877. ' },',
  22878. ' set: function (v) {',
  22879. ' $with1 = v;',
  22880. ' }',
  22881. '}, 123);',
  22882. '']));
  22883. end;
  22884. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22885. begin
  22886. StartProgram(false);
  22887. Add([
  22888. '{$modeswitch typehelpers}',
  22889. 'type',
  22890. ' TObject = class',
  22891. ' FField: word;',
  22892. ' procedure SetField(Value: word);',
  22893. ' property Field: word read FField write SetField;',
  22894. ' end;',
  22895. ' THelper = type helper for word',
  22896. ' procedure Fly;',
  22897. ' class procedure Run; static;',
  22898. ' end;',
  22899. 'procedure TObject.SetField(Value: word);',
  22900. 'begin',
  22901. ' Field.Fly;',
  22902. ' Field.Run;',
  22903. ' Self.Field.Fly;',
  22904. ' Self.Field.Run;',
  22905. ' with Self do begin',
  22906. ' Field.Fly;',
  22907. ' Field.Run;',
  22908. ' end;',
  22909. ' with Self.Field do begin',
  22910. ' Fly;',
  22911. ' Run;',
  22912. ' end;',
  22913. 'end;',
  22914. 'procedure THelper.Fly;',
  22915. 'begin',
  22916. 'end;',
  22917. 'class procedure THelper.Run;',
  22918. 'begin',
  22919. 'end;',
  22920. 'var',
  22921. ' o: TObject;',
  22922. 'begin',
  22923. ' o.Field.Fly;',
  22924. ' o.Field.Run;',
  22925. ' with o do begin',
  22926. ' Field.Fly;',
  22927. ' Field.Run;',
  22928. ' end;',
  22929. ' with o.Field do begin',
  22930. ' Fly;',
  22931. ' Run;',
  22932. ' end;',
  22933. '']);
  22934. ConvertProgram;
  22935. CheckSource('TestTypeHelper_PassPropertyField',
  22936. LinesToStr([ // statements
  22937. 'rtl.createClass(this, "TObject", null, function () {',
  22938. ' this.$init = function () {',
  22939. ' this.FField = 0;',
  22940. ' };',
  22941. ' this.$final = function () {',
  22942. ' };',
  22943. ' this.SetField = function (Value) {',
  22944. ' $mod.THelper.Fly.call({',
  22945. ' p: this,',
  22946. ' get: function () {',
  22947. ' return this.p.FField;',
  22948. ' },',
  22949. ' set: function (v) {',
  22950. ' this.p.FField = v;',
  22951. ' }',
  22952. ' });',
  22953. ' $mod.THelper.Run();',
  22954. ' $mod.THelper.Fly.call({',
  22955. ' p: this,',
  22956. ' get: function () {',
  22957. ' return this.p.FField;',
  22958. ' },',
  22959. ' set: function (v) {',
  22960. ' this.p.FField = v;',
  22961. ' }',
  22962. ' });',
  22963. ' $mod.THelper.Run();',
  22964. ' $mod.THelper.Fly.call({',
  22965. ' p: this,',
  22966. ' get: function () {',
  22967. ' return this.p.FField;',
  22968. ' },',
  22969. ' set: function (v) {',
  22970. ' this.p.FField = v;',
  22971. ' }',
  22972. ' });',
  22973. ' $mod.THelper.Run();',
  22974. ' var $with = this.FField;',
  22975. ' $mod.THelper.Fly.call({',
  22976. ' get: function () {',
  22977. ' return $with;',
  22978. ' },',
  22979. ' set: function (v) {',
  22980. ' $with = v;',
  22981. ' }',
  22982. ' });',
  22983. ' $mod.THelper.Run();',
  22984. ' };',
  22985. '});',
  22986. 'rtl.createHelper(this, "THelper", null, function () {',
  22987. ' this.Fly = function () {',
  22988. ' };',
  22989. ' this.Run = function () {',
  22990. ' };',
  22991. '});',
  22992. 'this.o = null;',
  22993. '']),
  22994. LinesToStr([ // $mod.$main
  22995. '$mod.THelper.Fly.call({',
  22996. ' p: $mod.o,',
  22997. ' get: function () {',
  22998. ' return this.p.FField;',
  22999. ' },',
  23000. ' set: function (v) {',
  23001. ' this.p.FField = v;',
  23002. ' }',
  23003. '});',
  23004. '$mod.THelper.Run();',
  23005. 'var $with = $mod.o;',
  23006. '$mod.THelper.Fly.call({',
  23007. ' p: $with,',
  23008. ' get: function () {',
  23009. ' return this.p.FField;',
  23010. ' },',
  23011. ' set: function (v) {',
  23012. ' this.p.FField = v;',
  23013. ' }',
  23014. '});',
  23015. '$mod.THelper.Run();',
  23016. 'var $with1 = $mod.o.FField;',
  23017. '$mod.THelper.Fly.call({',
  23018. ' get: function () {',
  23019. ' return $with1;',
  23020. ' },',
  23021. ' set: function (v) {',
  23022. ' $with1 = v;',
  23023. ' }',
  23024. '});',
  23025. '$mod.THelper.Run();',
  23026. '']));
  23027. end;
  23028. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23029. begin
  23030. StartProgram(false);
  23031. Add([
  23032. '{$modeswitch typehelpers}',
  23033. 'type',
  23034. ' TObject = class',
  23035. ' FField: word;',
  23036. ' function GetField: word;',
  23037. ' property Field: word read GetField write FField;',
  23038. ' end;',
  23039. ' THelper = type helper for word',
  23040. ' procedure Fly;',
  23041. ' class procedure Run; static;',
  23042. ' end;',
  23043. 'function TObject.GetField: word;',
  23044. 'begin',
  23045. ' Field.Fly;',
  23046. ' Field.Run;',
  23047. ' Self.Field.Fly;',
  23048. ' Self.Field.Run;',
  23049. ' with Self do begin',
  23050. ' Field.Fly;',
  23051. ' Field.Run;',
  23052. ' end;',
  23053. ' with Self.Field do begin',
  23054. ' Fly;',
  23055. ' Run;',
  23056. ' end;',
  23057. 'end;',
  23058. 'procedure THelper.Fly;',
  23059. 'begin',
  23060. 'end;',
  23061. 'class procedure THelper.Run;',
  23062. 'begin',
  23063. 'end;',
  23064. 'var',
  23065. ' o: TObject;',
  23066. 'begin',
  23067. ' o.Field.Fly;',
  23068. ' o.Field.Run;',
  23069. ' with o do begin',
  23070. ' Field.Fly;',
  23071. ' Field.Run;',
  23072. ' end;',
  23073. ' with o.Field do begin',
  23074. ' Fly;',
  23075. ' Run;',
  23076. ' end;',
  23077. '']);
  23078. ConvertProgram;
  23079. CheckSource('TestTypeHelper_PassPropertyGetter',
  23080. LinesToStr([ // statements
  23081. 'rtl.createClass(this, "TObject", null, function () {',
  23082. ' this.$init = function () {',
  23083. ' this.FField = 0;',
  23084. ' };',
  23085. ' this.$final = function () {',
  23086. ' };',
  23087. ' this.GetField = function () {',
  23088. ' var Result = 0;',
  23089. ' $mod.THelper.Fly.call({',
  23090. ' p: this.GetField(),',
  23091. ' get: function () {',
  23092. ' return this.p;',
  23093. ' },',
  23094. ' set: function (v) {',
  23095. ' this.p = v;',
  23096. ' }',
  23097. ' });',
  23098. ' $mod.THelper.Run();',
  23099. ' $mod.THelper.Fly.call({',
  23100. ' p: this.GetField(),',
  23101. ' get: function () {',
  23102. ' return this.p;',
  23103. ' },',
  23104. ' set: function (v) {',
  23105. ' this.p = v;',
  23106. ' }',
  23107. ' });',
  23108. ' $mod.THelper.Run();',
  23109. ' $mod.THelper.Fly.call({',
  23110. ' p: this.GetField(),',
  23111. ' get: function () {',
  23112. ' return this.p;',
  23113. ' },',
  23114. ' set: function (v) {',
  23115. ' this.p = v;',
  23116. ' }',
  23117. ' });',
  23118. ' $mod.THelper.Run();',
  23119. ' var $with = this.GetField();',
  23120. ' $mod.THelper.Fly.call({',
  23121. ' get: function () {',
  23122. ' return $with;',
  23123. ' },',
  23124. ' set: function (v) {',
  23125. ' $with = v;',
  23126. ' }',
  23127. ' });',
  23128. ' $mod.THelper.Run();',
  23129. ' return Result;',
  23130. ' };',
  23131. '});',
  23132. 'rtl.createHelper(this, "THelper", null, function () {',
  23133. ' this.Fly = function () {',
  23134. ' };',
  23135. ' this.Run = function () {',
  23136. ' };',
  23137. '});',
  23138. 'this.o = null;',
  23139. '']),
  23140. LinesToStr([ // $mod.$main
  23141. '$mod.THelper.Fly.call({',
  23142. ' p: $mod.o.GetField(),',
  23143. ' get: function () {',
  23144. ' return this.p;',
  23145. ' },',
  23146. ' set: function (v) {',
  23147. ' this.p = v;',
  23148. ' }',
  23149. '});',
  23150. '$mod.THelper.Run();',
  23151. 'var $with = $mod.o;',
  23152. '$mod.THelper.Fly.call({',
  23153. ' p: $with.GetField(),',
  23154. ' get: function () {',
  23155. ' return this.p;',
  23156. ' },',
  23157. ' set: function (v) {',
  23158. ' this.p = v;',
  23159. ' }',
  23160. '});',
  23161. '$mod.THelper.Run();',
  23162. 'var $with1 = $mod.o.GetField();',
  23163. '$mod.THelper.Fly.call({',
  23164. ' get: function () {',
  23165. ' return $with1;',
  23166. ' },',
  23167. ' set: function (v) {',
  23168. ' $with1 = v;',
  23169. ' }',
  23170. '});',
  23171. '$mod.THelper.Run();',
  23172. '']));
  23173. end;
  23174. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23175. begin
  23176. StartProgram(false);
  23177. Add([
  23178. '{$modeswitch typehelpers}',
  23179. 'type',
  23180. ' TObject = class',
  23181. ' class var FField: word;',
  23182. ' class procedure SetField(Value: word);',
  23183. ' class property Field: word read FField write SetField;',
  23184. ' end;',
  23185. ' THelper = type helper for word',
  23186. ' procedure Fly(n: byte);',
  23187. ' end;',
  23188. 'class procedure TObject.SetField(Value: word);',
  23189. 'begin',
  23190. ' Field.Fly(1);',
  23191. ' Self.Field.Fly(2);',
  23192. ' with Self do Field.Fly(3);',
  23193. ' with Self.Field do Fly(4);',
  23194. ' TObject.Field.Fly(5);',
  23195. ' with TObject do Field.Fly(6);',
  23196. ' with TObject.Field do Fly(7);',
  23197. 'end;',
  23198. 'procedure THelper.Fly(n: byte);',
  23199. 'begin',
  23200. 'end;',
  23201. 'var',
  23202. ' o: TObject;',
  23203. 'begin',
  23204. ' o.Field.Fly(11);',
  23205. ' with o do Field.Fly(12);',
  23206. ' with o.Field do Fly(13);',
  23207. ' TObject.Field.Fly(14);',
  23208. ' with TObject do Field.Fly(15);',
  23209. ' with TObject.Field do Fly(16);',
  23210. '']);
  23211. ConvertProgram;
  23212. CheckSource('TestTypeHelper_PassClassPropertyField',
  23213. LinesToStr([ // statements
  23214. 'rtl.createClass(this, "TObject", null, function () {',
  23215. ' this.FField = 0;',
  23216. ' this.$init = function () {',
  23217. ' };',
  23218. ' this.$final = function () {',
  23219. ' };',
  23220. ' this.SetField = function (Value) {',
  23221. ' $mod.THelper.Fly.call({',
  23222. ' p: this,',
  23223. ' get: function () {',
  23224. ' return this.p.FField;',
  23225. ' },',
  23226. ' set: function (v) {',
  23227. ' $mod.TObject.FField = v;',
  23228. ' }',
  23229. ' }, 1);',
  23230. ' $mod.THelper.Fly.call({',
  23231. ' p: this,',
  23232. ' get: function () {',
  23233. ' return this.p.FField;',
  23234. ' },',
  23235. ' set: function (v) {',
  23236. ' $mod.TObject.FField = v;',
  23237. ' }',
  23238. ' }, 2);',
  23239. ' $mod.THelper.Fly.call({',
  23240. ' p: this,',
  23241. ' get: function () {',
  23242. ' return this.p.FField;',
  23243. ' },',
  23244. ' set: function (v) {',
  23245. ' $mod.TObject.FField = v;',
  23246. ' }',
  23247. ' }, 3);',
  23248. ' var $with = this.FField;',
  23249. ' $mod.THelper.Fly.call({',
  23250. ' get: function () {',
  23251. ' return $with;',
  23252. ' },',
  23253. ' set: function (v) {',
  23254. ' $with = v;',
  23255. ' }',
  23256. ' }, 4);',
  23257. ' $mod.THelper.Fly.call({',
  23258. ' p: $mod.TObject,',
  23259. ' get: function () {',
  23260. ' return this.p.FField;',
  23261. ' },',
  23262. ' set: function (v) {',
  23263. ' $mod.TObject.FField = v;',
  23264. ' }',
  23265. ' }, 5);',
  23266. ' var $with1 = $mod.TObject;',
  23267. ' $mod.THelper.Fly.call({',
  23268. ' p: $with1,',
  23269. ' get: function () {',
  23270. ' return this.p.FField;',
  23271. ' },',
  23272. ' set: function (v) {',
  23273. ' $mod.TObject.FField = v;',
  23274. ' }',
  23275. ' }, 6);',
  23276. ' var $with2 = $mod.TObject.FField;',
  23277. ' $mod.THelper.Fly.call({',
  23278. ' get: function () {',
  23279. ' return $with2;',
  23280. ' },',
  23281. ' set: function (v) {',
  23282. ' $with2 = v;',
  23283. ' }',
  23284. ' }, 7);',
  23285. ' };',
  23286. '});',
  23287. 'rtl.createHelper(this, "THelper", null, function () {',
  23288. ' this.Fly = function (n) {',
  23289. ' };',
  23290. '});',
  23291. 'this.o = null;',
  23292. '']),
  23293. LinesToStr([ // $mod.$main
  23294. '$mod.THelper.Fly.call({',
  23295. ' p: $mod.o,',
  23296. ' get: function () {',
  23297. ' return this.p.FField;',
  23298. ' },',
  23299. ' set: function (v) {',
  23300. ' $mod.TObject.FField = v;',
  23301. ' }',
  23302. '}, 11);',
  23303. 'var $with = $mod.o;',
  23304. '$mod.THelper.Fly.call({',
  23305. ' p: $with,',
  23306. ' get: function () {',
  23307. ' return this.p.FField;',
  23308. ' },',
  23309. ' set: function (v) {',
  23310. ' $mod.TObject.FField = v;',
  23311. ' }',
  23312. '}, 12);',
  23313. 'var $with1 = $mod.o.FField;',
  23314. '$mod.THelper.Fly.call({',
  23315. ' get: function () {',
  23316. ' return $with1;',
  23317. ' },',
  23318. ' set: function (v) {',
  23319. ' $with1 = v;',
  23320. ' }',
  23321. '}, 13);',
  23322. '$mod.THelper.Fly.call({',
  23323. ' p: $mod.TObject,',
  23324. ' get: function () {',
  23325. ' return this.p.FField;',
  23326. ' },',
  23327. ' set: function (v) {',
  23328. ' $mod.TObject.FField = v;',
  23329. ' }',
  23330. '}, 14);',
  23331. 'var $with2 = $mod.TObject;',
  23332. '$mod.THelper.Fly.call({',
  23333. ' p: $with2,',
  23334. ' get: function () {',
  23335. ' return this.p.FField;',
  23336. ' },',
  23337. ' set: function (v) {',
  23338. ' $mod.TObject.FField = v;',
  23339. ' }',
  23340. '}, 15);',
  23341. 'var $with3 = $mod.TObject.FField;',
  23342. '$mod.THelper.Fly.call({',
  23343. ' get: function () {',
  23344. ' return $with3;',
  23345. ' },',
  23346. ' set: function (v) {',
  23347. ' $with3 = v;',
  23348. ' }',
  23349. '}, 16);',
  23350. '']));
  23351. end;
  23352. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23353. begin
  23354. StartProgram(false);
  23355. Add([
  23356. '{$modeswitch typehelpers}',
  23357. 'type',
  23358. ' TObject = class',
  23359. ' class var FField: word;',
  23360. ' class function GetField: word; static;',
  23361. ' class property Field: word read GetField write FField;',
  23362. ' end;',
  23363. ' THelper = type helper for word',
  23364. ' procedure Fly(n: byte);',
  23365. ' end;',
  23366. 'class function TObject.GetField: word;',
  23367. 'begin',
  23368. ' Field.Fly(1);',
  23369. ' TObject.Field.Fly(5);',
  23370. ' with TObject do Field.Fly(6);',
  23371. ' with TObject.Field do Fly(7);',
  23372. 'end;',
  23373. 'procedure THelper.Fly(n: byte);',
  23374. 'begin',
  23375. 'end;',
  23376. 'var',
  23377. ' o: TObject;',
  23378. 'begin',
  23379. ' o.Field.Fly(11);',
  23380. ' with o do Field.Fly(12);',
  23381. ' with o.Field do Fly(13);',
  23382. '']);
  23383. ConvertProgram;
  23384. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23385. LinesToStr([ // statements
  23386. 'rtl.createClass(this, "TObject", null, function () {',
  23387. ' this.FField = 0;',
  23388. ' this.$init = function () {',
  23389. ' };',
  23390. ' this.$final = function () {',
  23391. ' };',
  23392. ' this.GetField = function () {',
  23393. ' var Result = 0;',
  23394. ' $mod.THelper.Fly.call({',
  23395. ' p: $mod.TObject.GetField(),',
  23396. ' get: function () {',
  23397. ' return this.p;',
  23398. ' },',
  23399. ' set: function (v) {',
  23400. ' this.p = v;',
  23401. ' }',
  23402. ' }, 1);',
  23403. ' $mod.THelper.Fly.call({',
  23404. ' p: $mod.TObject.GetField(),',
  23405. ' get: function () {',
  23406. ' return this.p;',
  23407. ' },',
  23408. ' set: function (v) {',
  23409. ' this.p = v;',
  23410. ' }',
  23411. ' }, 5);',
  23412. ' var $with = $mod.TObject;',
  23413. ' $mod.THelper.Fly.call({',
  23414. ' p: $with.GetField(),',
  23415. ' get: function () {',
  23416. ' return this.p;',
  23417. ' },',
  23418. ' set: function (v) {',
  23419. ' this.p = v;',
  23420. ' }',
  23421. ' }, 6);',
  23422. ' var $with1 = $mod.TObject.GetField();',
  23423. ' $mod.THelper.Fly.call({',
  23424. ' get: function () {',
  23425. ' return $with1;',
  23426. ' },',
  23427. ' set: function (v) {',
  23428. ' $with1 = v;',
  23429. ' }',
  23430. ' }, 7);',
  23431. ' return Result;',
  23432. ' };',
  23433. '});',
  23434. 'rtl.createHelper(this, "THelper", null, function () {',
  23435. ' this.Fly = function (n) {',
  23436. ' };',
  23437. '});',
  23438. 'this.o = null;',
  23439. '']),
  23440. LinesToStr([ // $mod.$main
  23441. '$mod.THelper.Fly.call({',
  23442. ' p: $mod.o.GetField(),',
  23443. ' get: function () {',
  23444. ' return this.p;',
  23445. ' },',
  23446. ' set: function (v) {',
  23447. ' this.p = v;',
  23448. ' }',
  23449. '}, 11);',
  23450. 'var $with = $mod.o;',
  23451. '$mod.THelper.Fly.call({',
  23452. ' p: $with.GetField(),',
  23453. ' get: function () {',
  23454. ' return this.p;',
  23455. ' },',
  23456. ' set: function (v) {',
  23457. ' this.p = v;',
  23458. ' }',
  23459. '}, 12);',
  23460. 'var $with1 = $mod.o.GetField();',
  23461. '$mod.THelper.Fly.call({',
  23462. ' get: function () {',
  23463. ' return $with1;',
  23464. ' },',
  23465. ' set: function (v) {',
  23466. ' $with1 = v;',
  23467. ' }',
  23468. '}, 13);',
  23469. '']));
  23470. end;
  23471. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23472. begin
  23473. StartProgram(false);
  23474. Add([
  23475. '{$modeswitch typehelpers}',
  23476. 'type',
  23477. ' TObject = class',
  23478. ' class var FField: word;',
  23479. ' class function GetField: word;',
  23480. ' class property Field: word read GetField write FField;',
  23481. ' end;',
  23482. ' TClass = class of TObject;',
  23483. ' THelper = type helper for word',
  23484. ' procedure Fly(n: byte);',
  23485. ' end;',
  23486. 'class function TObject.GetField: word;',
  23487. 'begin',
  23488. ' Field.Fly(1);',
  23489. ' Self.Field.Fly(5);',
  23490. ' with Self do Field.Fly(6);',
  23491. ' with Self.Field do Fly(7);',
  23492. 'end;',
  23493. 'procedure THelper.Fly(n: byte);',
  23494. 'begin',
  23495. 'end;',
  23496. 'var',
  23497. ' o: TObject;',
  23498. ' c: TClass;',
  23499. 'begin',
  23500. ' o.Field.Fly(11);',
  23501. ' with o do Field.Fly(12);',
  23502. ' with o.Field do Fly(13);',
  23503. ' c.Field.Fly(14);',
  23504. ' with c do Field.Fly(15);',
  23505. ' with c.Field do Fly(16);',
  23506. '']);
  23507. ConvertProgram;
  23508. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23509. LinesToStr([ // statements
  23510. 'rtl.createClass(this, "TObject", null, function () {',
  23511. ' this.FField = 0;',
  23512. ' this.$init = function () {',
  23513. ' };',
  23514. ' this.$final = function () {',
  23515. ' };',
  23516. ' this.GetField = function () {',
  23517. ' var Result = 0;',
  23518. ' $mod.THelper.Fly.call({',
  23519. ' p: this.GetField(),',
  23520. ' get: function () {',
  23521. ' return this.p;',
  23522. ' },',
  23523. ' set: function (v) {',
  23524. ' this.p = v;',
  23525. ' }',
  23526. ' }, 1);',
  23527. ' $mod.THelper.Fly.call({',
  23528. ' p: this.GetField(),',
  23529. ' get: function () {',
  23530. ' return this.p;',
  23531. ' },',
  23532. ' set: function (v) {',
  23533. ' this.p = v;',
  23534. ' }',
  23535. ' }, 5);',
  23536. ' $mod.THelper.Fly.call({',
  23537. ' p: this.GetField(),',
  23538. ' get: function () {',
  23539. ' return this.p;',
  23540. ' },',
  23541. ' set: function (v) {',
  23542. ' this.p = v;',
  23543. ' }',
  23544. ' }, 6);',
  23545. ' var $with = this.GetField();',
  23546. ' $mod.THelper.Fly.call({',
  23547. ' get: function () {',
  23548. ' return $with;',
  23549. ' },',
  23550. ' set: function (v) {',
  23551. ' $with = v;',
  23552. ' }',
  23553. ' }, 7);',
  23554. ' return Result;',
  23555. ' };',
  23556. '});',
  23557. 'rtl.createHelper(this, "THelper", null, function () {',
  23558. ' this.Fly = function (n) {',
  23559. ' };',
  23560. '});',
  23561. 'this.o = null;',
  23562. 'this.c = null;',
  23563. '']),
  23564. LinesToStr([ // $mod.$main
  23565. '$mod.THelper.Fly.call({',
  23566. ' p: $mod.o.$class.GetField(),',
  23567. ' get: function () {',
  23568. ' return this.p;',
  23569. ' },',
  23570. ' set: function (v) {',
  23571. ' this.p = v;',
  23572. ' }',
  23573. '}, 11);',
  23574. 'var $with = $mod.o;',
  23575. '$mod.THelper.Fly.call({',
  23576. ' p: $with.$class.GetField(),',
  23577. ' get: function () {',
  23578. ' return this.p;',
  23579. ' },',
  23580. ' set: function (v) {',
  23581. ' this.p = v;',
  23582. ' }',
  23583. '}, 12);',
  23584. 'var $with1 = $mod.o.$class.GetField();',
  23585. '$mod.THelper.Fly.call({',
  23586. ' get: function () {',
  23587. ' return $with1;',
  23588. ' },',
  23589. ' set: function (v) {',
  23590. ' $with1 = v;',
  23591. ' }',
  23592. '}, 13);',
  23593. '$mod.THelper.Fly.call({',
  23594. ' p: $mod.c.GetField(),',
  23595. ' get: function () {',
  23596. ' return this.p;',
  23597. ' },',
  23598. ' set: function (v) {',
  23599. ' this.p = v;',
  23600. ' }',
  23601. '}, 14);',
  23602. 'var $with2 = $mod.c;',
  23603. '$mod.THelper.Fly.call({',
  23604. ' p: $with2.GetField(),',
  23605. ' get: function () {',
  23606. ' return this.p;',
  23607. ' },',
  23608. ' set: function (v) {',
  23609. ' this.p = v;',
  23610. ' }',
  23611. '}, 15);',
  23612. 'var $with3 = $mod.c.GetField();',
  23613. '$mod.THelper.Fly.call({',
  23614. ' get: function () {',
  23615. ' return $with3;',
  23616. ' },',
  23617. ' set: function (v) {',
  23618. ' $with3 = v;',
  23619. ' }',
  23620. '}, 16);',
  23621. '']));
  23622. end;
  23623. procedure TTestModule.TestTypeHelper_Property;
  23624. begin
  23625. StartProgram(false);
  23626. Add([
  23627. '{$modeswitch typehelpers}',
  23628. 'type',
  23629. ' THelper = type helper for word',
  23630. ' function GetSize: longint;',
  23631. ' procedure SetSize(Value: longint);',
  23632. ' property Size: longint read GetSize write SetSize;',
  23633. ' end;',
  23634. 'function THelper.GetSize: longint;',
  23635. 'begin',
  23636. ' Result:=Size+1;',
  23637. ' Size:=2;',
  23638. ' Result:=Self.Size+3;',
  23639. ' Self.Size:=4;',
  23640. ' with Self do begin',
  23641. ' Result:=Size+5;',
  23642. ' Size:=6;',
  23643. ' end;',
  23644. 'end;',
  23645. 'procedure THelper.SetSize(Value: longint);',
  23646. 'begin',
  23647. 'end;',
  23648. 'var w: word;',
  23649. 'begin',
  23650. ' w:=w.Size+7;',
  23651. ' w.Size:=w+8;',
  23652. ' with w do begin',
  23653. ' w:=Size+9;',
  23654. ' Size:=w+10;',
  23655. ' end;',
  23656. '']);
  23657. ConvertProgram;
  23658. CheckSource('TestTypeHelper_Property',
  23659. LinesToStr([ // statements
  23660. 'rtl.createHelper(this, "THelper", null, function () {',
  23661. ' this.GetSize = function () {',
  23662. ' var Result = 0;',
  23663. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23664. ' $mod.THelper.SetSize.call(this, 2);',
  23665. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23666. ' $mod.THelper.SetSize.call(this, 4);',
  23667. ' var $with = this.get();',
  23668. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23669. ' $mod.THelper.SetSize.call(this, 6);',
  23670. ' return Result;',
  23671. ' };',
  23672. ' this.SetSize = function (Value) {',
  23673. ' };',
  23674. '});',
  23675. 'this.w = 0;',
  23676. '']),
  23677. LinesToStr([ // $mod.$main
  23678. '$mod.w = $mod.THelper.GetSize.call({',
  23679. ' p: $mod,',
  23680. ' get: function () {',
  23681. ' return this.p.w;',
  23682. ' },',
  23683. ' set: function (v) {',
  23684. ' this.p.w = v;',
  23685. ' }',
  23686. '}) + 7;',
  23687. '$mod.THelper.SetSize.call({',
  23688. ' p: $mod,',
  23689. ' get: function () {',
  23690. ' return this.p.w;',
  23691. ' },',
  23692. ' set: function (v) {',
  23693. ' this.p.w = v;',
  23694. ' }',
  23695. '}, $mod.w + 8);',
  23696. 'var $with = $mod.w;',
  23697. '$mod.w = $mod.THelper.GetSize.call({',
  23698. ' get: function () {',
  23699. ' return $with;',
  23700. ' },',
  23701. ' set: function (v) {',
  23702. ' $with = v;',
  23703. ' }',
  23704. '}) + 9;',
  23705. '$mod.THelper.SetSize.call({',
  23706. ' get: function () {',
  23707. ' return $with;',
  23708. ' },',
  23709. ' set: function (v) {',
  23710. ' $with = v;',
  23711. ' }',
  23712. '}, $mod.w + 10);',
  23713. '']));
  23714. end;
  23715. procedure TTestModule.TestTypeHelper_Property_Array;
  23716. begin
  23717. StartProgram(false);
  23718. Add([
  23719. '{$modeswitch typehelpers}',
  23720. 'type',
  23721. ' THelper = type helper for word',
  23722. ' function GetItems(Index: byte): boolean;',
  23723. ' procedure SetItems(Index: byte; Value: boolean);',
  23724. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23725. ' end;',
  23726. 'function THelper.GetItems(Index: byte): boolean;',
  23727. 'begin',
  23728. ' Result:=Items[1];',
  23729. ' Items[2]:=false;',
  23730. ' Result:=Self.Items[3];',
  23731. ' Self.Items[4]:=true;',
  23732. ' with Self do begin',
  23733. ' Result:=Items[5];',
  23734. ' Items[6]:=false;',
  23735. ' end;',
  23736. 'end;',
  23737. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23738. 'begin',
  23739. 'end;',
  23740. 'var',
  23741. ' w: word;',
  23742. ' b: boolean;',
  23743. 'begin',
  23744. ' b:=w.Items[1];',
  23745. ' w.Items[2]:=b;',
  23746. ' with w do begin',
  23747. ' b:=Items[3];',
  23748. ' Items[4]:=b;',
  23749. ' end;',
  23750. '']);
  23751. ConvertProgram;
  23752. CheckSource('TestTypeHelper_Property_Array',
  23753. LinesToStr([ // statements
  23754. 'rtl.createHelper(this, "THelper", null, function () {',
  23755. ' this.GetItems = function (Index) {',
  23756. ' var Result = false;',
  23757. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23758. ' $mod.THelper.SetItems.call(this, 2, false);',
  23759. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23760. ' $mod.THelper.SetItems.call(this, 4, true);',
  23761. ' var $with = this.get();',
  23762. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23763. ' $mod.THelper.SetItems.call(this, 6, false);',
  23764. ' return Result;',
  23765. ' };',
  23766. ' this.SetItems = function (Index, Value) {',
  23767. ' };',
  23768. '});',
  23769. 'this.w = 0;',
  23770. 'this.b = false;',
  23771. '']),
  23772. LinesToStr([ // $mod.$main
  23773. '$mod.b = $mod.THelper.GetItems.call({',
  23774. ' p: $mod,',
  23775. ' get: function () {',
  23776. ' return this.p.w;',
  23777. ' },',
  23778. ' set: function (v) {',
  23779. ' this.p.w = v;',
  23780. ' }',
  23781. '}, 1);',
  23782. '$mod.THelper.SetItems.call({',
  23783. ' p: $mod,',
  23784. ' get: function () {',
  23785. ' return this.p.w;',
  23786. ' },',
  23787. ' set: function (v) {',
  23788. ' this.p.w = v;',
  23789. ' }',
  23790. '}, 2, $mod.b);',
  23791. 'var $with = $mod.w;',
  23792. '$mod.b = $mod.THelper.GetItems.call({',
  23793. ' get: function () {',
  23794. ' return $with;',
  23795. ' },',
  23796. ' set: function (v) {',
  23797. ' $with = v;',
  23798. ' }',
  23799. '}, 3);',
  23800. '$mod.THelper.SetItems.call({',
  23801. ' get: function () {',
  23802. ' return $with;',
  23803. ' },',
  23804. ' set: function (v) {',
  23805. ' $with = v;',
  23806. ' }',
  23807. '}, 4, $mod.b);',
  23808. '']));
  23809. end;
  23810. procedure TTestModule.TestTypeHelper_ClassProperty;
  23811. begin
  23812. StartProgram(false);
  23813. Add([
  23814. '{$modeswitch typehelpers}',
  23815. 'type',
  23816. ' THelper = type helper for word',
  23817. ' class function GetSize: longint; static;',
  23818. ' class procedure SetSize(Value: longint); static;',
  23819. ' class property Size: longint read GetSize write SetSize;',
  23820. ' end;',
  23821. 'class function THelper.GetSize: longint;',
  23822. 'begin',
  23823. ' Result:=Size+1;',
  23824. ' Size:=2;',
  23825. 'end;',
  23826. 'class procedure THelper.SetSize(Value: longint);',
  23827. 'begin',
  23828. 'end;',
  23829. 'begin',
  23830. '']);
  23831. ConvertProgram;
  23832. CheckSource('TestTypeHelper_ClassProperty',
  23833. LinesToStr([ // statements
  23834. 'rtl.createHelper(this, "THelper", null, function () {',
  23835. ' this.GetSize = function () {',
  23836. ' var Result = 0;',
  23837. ' Result = $mod.THelper.GetSize() + 1;',
  23838. ' $mod.THelper.SetSize(2);',
  23839. ' return Result;',
  23840. ' };',
  23841. ' this.SetSize = function (Value) {',
  23842. ' };',
  23843. '});',
  23844. '']),
  23845. LinesToStr([ // $mod.$main
  23846. '']));
  23847. end;
  23848. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23849. begin
  23850. StartProgram(false);
  23851. Add([
  23852. '{$modeswitch typehelpers}',
  23853. 'type',
  23854. ' THelper = type helper for word',
  23855. ' class function GetItems(Index: byte): boolean; static;',
  23856. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23857. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23858. ' end;',
  23859. 'class function THelper.GetItems(Index: byte): boolean;',
  23860. 'begin',
  23861. ' Result:=Items[1];',
  23862. ' Items[2]:=false;',
  23863. 'end;',
  23864. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23865. 'begin',
  23866. 'end;',
  23867. 'var',
  23868. ' w: word;',
  23869. ' b: boolean;',
  23870. 'begin',
  23871. ' b:=w.Items[1];',
  23872. ' w.Items[2]:=b;',
  23873. ' with w do begin',
  23874. ' b:=Items[3];',
  23875. ' Items[4]:=b;',
  23876. ' end;',
  23877. '']);
  23878. ConvertProgram;
  23879. CheckSource('TestTypeHelper_ClassProperty_Array',
  23880. LinesToStr([ // statements
  23881. 'rtl.createHelper(this, "THelper", null, function () {',
  23882. ' this.GetItems = function (Index) {',
  23883. ' var Result = false;',
  23884. ' Result = $mod.THelper.GetItems(1);',
  23885. ' $mod.THelper.SetItems(2, false);',
  23886. ' return Result;',
  23887. ' };',
  23888. ' this.SetItems = function (Index, Value) {',
  23889. ' };',
  23890. '});',
  23891. 'this.w = 0;',
  23892. 'this.b = false;',
  23893. '']),
  23894. LinesToStr([ // $mod.$main
  23895. '$mod.b = $mod.THelper.GetItems(1);',
  23896. '$mod.THelper.SetItems(2, $mod.b);',
  23897. 'var $with = $mod.w;',
  23898. '$mod.b = $mod.THelper.GetItems(3);',
  23899. '$mod.THelper.SetItems(4, $mod.b);',
  23900. '']));
  23901. end;
  23902. procedure TTestModule.TestTypeHelper_ClassMethod;
  23903. begin
  23904. StartProgram(false);
  23905. Add([
  23906. '{$modeswitch typehelpers}',
  23907. 'type',
  23908. ' THelper = type helper for word',
  23909. ' class procedure DoStatic; static;',
  23910. ' end;',
  23911. 'class procedure THelper.DoStatic;',
  23912. 'begin',
  23913. ' DoStatic;',
  23914. ' DoStatic();',
  23915. 'end;',
  23916. 'var w: word;',
  23917. 'begin',
  23918. ' w.DoStatic;',
  23919. ' w.DoStatic();',
  23920. '']);
  23921. ConvertProgram;
  23922. CheckSource('TestTypeHelper_ClassMethod',
  23923. LinesToStr([ // statements
  23924. 'rtl.createHelper(this, "THelper", null, function () {',
  23925. ' this.DoStatic = function () {',
  23926. ' $mod.THelper.DoStatic();',
  23927. ' $mod.THelper.DoStatic();',
  23928. ' };',
  23929. '});',
  23930. 'this.w = 0;',
  23931. '']),
  23932. LinesToStr([ // $mod.$main
  23933. '$mod.THelper.DoStatic();',
  23934. '$mod.THelper.DoStatic();',
  23935. '']));
  23936. end;
  23937. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23938. begin
  23939. StartProgram(false);
  23940. Add([
  23941. '{$modeswitch typehelpers}',
  23942. 'type',
  23943. ' THelper = type helper for word',
  23944. ' procedure Run; external name ''Run'';',
  23945. ' end;',
  23946. 'var w: word;',
  23947. 'begin',
  23948. ' w.Run;',
  23949. '']);
  23950. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23951. ConvertProgram;
  23952. end;
  23953. procedure TTestModule.TestTypeHelper_Constructor;
  23954. begin
  23955. StartProgram(false);
  23956. Add([
  23957. '{$modeswitch typehelpers}',
  23958. 'type',
  23959. ' THelper = type helper for word',
  23960. ' constructor Init(e: longint);',
  23961. ' end;',
  23962. 'constructor THelper.Init(e: longint);',
  23963. 'begin',
  23964. ' Self:=e;',
  23965. ' Init(e+1);',
  23966. 'end;',
  23967. 'var w: word;',
  23968. 'begin',
  23969. ' w:=word.Init(2);',
  23970. ' w:=w.Init(3);',
  23971. ' with word do w:=Init(4);',
  23972. ' with w do w:=Init(5);',
  23973. '']);
  23974. ConvertProgram;
  23975. CheckSource('TestTypeHelper_Constructor',
  23976. LinesToStr([ // statements
  23977. 'rtl.createHelper(this, "THelper", null, function () {',
  23978. ' this.Init = function (e) {',
  23979. ' this.set(e);',
  23980. ' $mod.THelper.Init.call(this, e + 1);',
  23981. ' return this.get();',
  23982. ' };',
  23983. ' this.$new = function (fn, args) {',
  23984. ' return this[fn].apply({',
  23985. ' p: 0,',
  23986. ' get: function () {',
  23987. ' return this.p;',
  23988. ' },',
  23989. ' set: function (v) {',
  23990. ' this.p = v;',
  23991. ' }',
  23992. ' }, args);',
  23993. ' };',
  23994. '});',
  23995. 'this.w = 0;',
  23996. '']),
  23997. LinesToStr([ // $mod.$main
  23998. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23999. '$mod.w = $mod.THelper.Init.call({',
  24000. ' p: $mod,',
  24001. ' get: function () {',
  24002. ' return this.p.w;',
  24003. ' },',
  24004. ' set: function (v) {',
  24005. ' this.p.w = v;',
  24006. ' }',
  24007. '}, 3);',
  24008. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24009. 'var $with = $mod.w;',
  24010. '$mod.w = $mod.THelper.Init.call({',
  24011. ' get: function () {',
  24012. ' return $with;',
  24013. ' },',
  24014. ' set: function (v) {',
  24015. ' $with = v;',
  24016. ' }',
  24017. '}, 5);',
  24018. '']));
  24019. end;
  24020. procedure TTestModule.TestTypeHelper_Word;
  24021. begin
  24022. StartProgram(false);
  24023. Add([
  24024. '{$modeswitch typehelpers}',
  24025. 'type',
  24026. ' THelper = type helper for word',
  24027. ' procedure DoIt(e: byte = 123);',
  24028. ' end;',
  24029. 'procedure THelper.DoIt(e: byte);',
  24030. 'begin',
  24031. ' Self:=e;',
  24032. ' Self:=Self+1;',
  24033. ' with Self do Doit;',
  24034. 'end;',
  24035. 'begin',
  24036. ' word(3).DoIt;',
  24037. '']);
  24038. ConvertProgram;
  24039. CheckSource('TestTypeHelper_Word',
  24040. LinesToStr([ // statements
  24041. 'rtl.createHelper(this, "THelper", null, function () {',
  24042. ' this.DoIt = function (e) {',
  24043. ' this.set(e);',
  24044. ' this.set(this.get() + 1);',
  24045. ' var $with = this.get();',
  24046. ' $mod.THelper.DoIt.call(this, 123);',
  24047. ' };',
  24048. '});',
  24049. '']),
  24050. LinesToStr([ // $mod.$main
  24051. '$mod.THelper.DoIt.call({',
  24052. ' get: function () {',
  24053. ' return 3;',
  24054. ' },',
  24055. ' set: function (v) {',
  24056. ' rtl.raiseE("EPropReadOnly");',
  24057. ' }',
  24058. '}, 123);',
  24059. '']));
  24060. end;
  24061. procedure TTestModule.TestTypeHelper_Boolean;
  24062. begin
  24063. StartProgram(false);
  24064. Add([
  24065. '{$modeswitch typehelpers}',
  24066. 'type',
  24067. ' Integer = longint;',
  24068. ' THelper = type helper for boolean',
  24069. ' procedure Run(e: wordbool = true);',
  24070. ' end;',
  24071. 'procedure THelper.Run(e: wordbool);',
  24072. 'begin',
  24073. ' Self:=e;',
  24074. ' Self:=not Self;',
  24075. ' with Self do Run;',
  24076. ' if Integer(Self)=0 then ;',
  24077. 'end;',
  24078. 'begin',
  24079. ' boolean(3).Run;',
  24080. '']);
  24081. ConvertProgram;
  24082. CheckSource('TestTypeHelper_Boolean',
  24083. LinesToStr([ // statements
  24084. 'rtl.createHelper(this, "THelper", null, function () {',
  24085. ' this.Run = function (e) {',
  24086. ' this.set(e);',
  24087. ' this.set(!this.get());',
  24088. ' var $with = this.get();',
  24089. ' $mod.THelper.Run.call(this, true);',
  24090. ' if ((this.get() ? 1 : 0) === 0) ;',
  24091. ' };',
  24092. '});',
  24093. '']),
  24094. LinesToStr([ // $mod.$main
  24095. '$mod.THelper.Run.call({',
  24096. ' a: 3 != 0,',
  24097. ' get: function () {',
  24098. ' return this.a;',
  24099. ' },',
  24100. ' set: function (v) {',
  24101. ' rtl.raiseE("EPropReadOnly");',
  24102. ' }',
  24103. '}, true);',
  24104. '']));
  24105. end;
  24106. procedure TTestModule.TestTypeHelper_WordBool;
  24107. begin
  24108. StartProgram(false);
  24109. Add([
  24110. '{$modeswitch typehelpers}',
  24111. 'type',
  24112. ' Integer = longint;',
  24113. ' THelper = type helper for WordBool',
  24114. ' procedure Run(e: wordbool = true);',
  24115. ' end;',
  24116. 'procedure THelper.Run(e: wordbool);',
  24117. 'var i: integer;',
  24118. 'begin',
  24119. ' i:=Integer(Self);',
  24120. 'end;',
  24121. 'var w: wordbool;',
  24122. 'begin',
  24123. ' w.Run;',
  24124. ' wordbool(3).Run;',
  24125. '']);
  24126. ConvertProgram;
  24127. CheckSource('TestTypeHelper_WordBool',
  24128. LinesToStr([ // statements
  24129. 'rtl.createHelper(this, "THelper", null, function () {',
  24130. ' this.Run = function (e) {',
  24131. ' var i = 0;',
  24132. ' i = (this.get() ? 1 : 0);',
  24133. ' };',
  24134. '});',
  24135. 'this.w = false;',
  24136. '']),
  24137. LinesToStr([ // $mod.$main
  24138. '$mod.THelper.Run.call({',
  24139. ' p: $mod,',
  24140. ' get: function () {',
  24141. ' return this.p.w;',
  24142. ' },',
  24143. ' set: function (v) {',
  24144. ' this.p.w = v;',
  24145. ' }',
  24146. '}, true);',
  24147. '$mod.THelper.Run.call({',
  24148. ' a: 3 != 0,',
  24149. ' get: function () {',
  24150. ' return this.a;',
  24151. ' },',
  24152. ' set: function (v) {',
  24153. ' rtl.raiseE("EPropReadOnly");',
  24154. ' }',
  24155. '}, true);',
  24156. '']));
  24157. end;
  24158. procedure TTestModule.TestTypeHelper_Double;
  24159. begin
  24160. StartProgram(false);
  24161. Add([
  24162. '{$modeswitch typehelpers}',
  24163. 'type',
  24164. ' Float = type double;',
  24165. ' THelper = type helper for Float',
  24166. ' const NPI = 3.141592;',
  24167. ' function ToStr: String;',
  24168. ' end;',
  24169. 'function THelper.ToStr: String;',
  24170. 'begin',
  24171. 'end;',
  24172. 'procedure DoIt(s: string);',
  24173. 'begin',
  24174. 'end;',
  24175. 'var f: Float;',
  24176. 'begin',
  24177. ' DoIt(f.toStr);',
  24178. ' DoIt(f.toStr());',
  24179. ' (f*f).toStr;',
  24180. ' DoIt((f*f).toStr);',
  24181. '']);
  24182. ConvertProgram;
  24183. CheckSource('TestTypeHelper_Double',
  24184. LinesToStr([ // statements
  24185. 'rtl.createHelper(this, "THelper", null, function () {',
  24186. ' this.NPI = 3.141592;',
  24187. ' this.ToStr = function () {',
  24188. ' var Result = "";',
  24189. ' return Result;',
  24190. ' };',
  24191. '});',
  24192. 'this.DoIt = function (s) {',
  24193. '};',
  24194. 'this.f = 0.0;',
  24195. '']),
  24196. LinesToStr([ // $mod.$main
  24197. '$mod.DoIt($mod.THelper.ToStr.call({',
  24198. ' p: $mod,',
  24199. ' get: function () {',
  24200. ' return this.p.f;',
  24201. ' },',
  24202. ' set: function (v) {',
  24203. ' this.p.f = v;',
  24204. ' }',
  24205. '}));',
  24206. '$mod.DoIt($mod.THelper.ToStr.call({',
  24207. ' p: $mod,',
  24208. ' get: function () {',
  24209. ' return this.p.f;',
  24210. ' },',
  24211. ' set: function (v) {',
  24212. ' this.p.f = v;',
  24213. ' }',
  24214. '}));',
  24215. '$mod.THelper.ToStr.call({',
  24216. ' a: $mod.f * $mod.f,',
  24217. ' get: function () {',
  24218. ' return this.a;',
  24219. ' },',
  24220. ' set: function (v) {',
  24221. ' rtl.raiseE("EPropReadOnly");',
  24222. ' }',
  24223. '});',
  24224. '$mod.DoIt($mod.THelper.ToStr.call({',
  24225. ' a: $mod.f * $mod.f,',
  24226. ' get: function () {',
  24227. ' return this.a;',
  24228. ' },',
  24229. ' set: function (v) {',
  24230. ' rtl.raiseE("EPropReadOnly");',
  24231. ' }',
  24232. '}));',
  24233. '']));
  24234. end;
  24235. procedure TTestModule.TestTypeHelper_NativeInt;
  24236. begin
  24237. StartProgram(false);
  24238. Add([
  24239. '{$modeswitch typehelpers}',
  24240. 'type',
  24241. ' MaxInt = type nativeint;',
  24242. ' THelperI = type helper for MaxInt',
  24243. ' function ToStr: String;',
  24244. ' end;',
  24245. ' MaxUInt = type nativeuint;',
  24246. ' THelperU = type helper for MaxUInt',
  24247. ' function ToStr: String;',
  24248. ' end;',
  24249. 'function THelperI.ToStr: String;',
  24250. 'begin',
  24251. ' Result:=str(Self);',
  24252. 'end;',
  24253. 'function THelperU.ToStr: String;',
  24254. 'begin',
  24255. ' Result:=str(Self);',
  24256. 'end;',
  24257. 'procedure DoIt(s: string);',
  24258. 'begin',
  24259. 'end;',
  24260. 'var i: MaxInt;',
  24261. 'begin',
  24262. ' DoIt(i.toStr);',
  24263. ' DoIt(i.toStr());',
  24264. ' (i*i).toStr;',
  24265. ' DoIt((i*i).toStr);',
  24266. '']);
  24267. ConvertProgram;
  24268. CheckSource('TestTypeHelper_NativeInt',
  24269. LinesToStr([ // statements
  24270. 'rtl.createHelper(this, "THelperI", null, function () {',
  24271. ' this.ToStr = function () {',
  24272. ' var Result = "";',
  24273. ' Result = "" + this.get();',
  24274. ' return Result;',
  24275. ' };',
  24276. '});',
  24277. 'rtl.createHelper(this, "THelperU", null, function () {',
  24278. ' this.ToStr = function () {',
  24279. ' var Result = "";',
  24280. ' Result = "" + this.get();',
  24281. ' return Result;',
  24282. ' };',
  24283. '});',
  24284. 'this.DoIt = function (s) {',
  24285. '};',
  24286. 'this.i = 0;',
  24287. '']),
  24288. LinesToStr([ // $mod.$main
  24289. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24290. ' p: $mod,',
  24291. ' get: function () {',
  24292. ' return this.p.i;',
  24293. ' },',
  24294. ' set: function (v) {',
  24295. ' this.p.i = v;',
  24296. ' }',
  24297. '}));',
  24298. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24299. ' p: $mod,',
  24300. ' get: function () {',
  24301. ' return this.p.i;',
  24302. ' },',
  24303. ' set: function (v) {',
  24304. ' this.p.i = v;',
  24305. ' }',
  24306. '}));',
  24307. '$mod.THelperI.ToStr.call({',
  24308. ' a: $mod.i * $mod.i,',
  24309. ' get: function () {',
  24310. ' return this.a;',
  24311. ' },',
  24312. ' set: function (v) {',
  24313. ' rtl.raiseE("EPropReadOnly");',
  24314. ' }',
  24315. '});',
  24316. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24317. ' a: $mod.i * $mod.i,',
  24318. ' get: function () {',
  24319. ' return this.a;',
  24320. ' },',
  24321. ' set: function (v) {',
  24322. ' rtl.raiseE("EPropReadOnly");',
  24323. ' }',
  24324. '}));',
  24325. '']));
  24326. end;
  24327. procedure TTestModule.TestTypeHelper_StringChar;
  24328. begin
  24329. StartProgram(false);
  24330. Add([
  24331. '{$modeswitch typehelpers}',
  24332. 'type',
  24333. ' TStringHelper = type helper for string',
  24334. ' procedure DoIt(e: byte = 123);',
  24335. ' end;',
  24336. ' TCharHelper = type helper for char',
  24337. ' procedure Fly;',
  24338. ' end;',
  24339. 'procedure TStringHelper.DoIt(e: byte);',
  24340. 'begin',
  24341. ' Self[1]:=''c'';',
  24342. ' Self[2]:=Self[3];',
  24343. 'end;',
  24344. 'procedure TCharHelper.Fly;',
  24345. 'begin',
  24346. ' Self:=''c'';',
  24347. 'end;',
  24348. 'begin',
  24349. ' ''abc''.DoIt;',
  24350. ' ''xyz''.DoIt();',
  24351. ' ''c''.Fly();',
  24352. '']);
  24353. ConvertProgram;
  24354. CheckSource('TestTypeHelper_StringChar',
  24355. LinesToStr([ // statements
  24356. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24357. ' this.DoIt = function (e) {',
  24358. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24359. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24360. ' };',
  24361. '});',
  24362. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24363. ' this.Fly = function () {',
  24364. ' this.set("c");',
  24365. ' };',
  24366. '});',
  24367. '']),
  24368. LinesToStr([ // $mod.$main
  24369. '$mod.TStringHelper.DoIt.call({',
  24370. ' get: function () {',
  24371. ' return "abc";',
  24372. ' },',
  24373. ' set: function (v) {',
  24374. ' rtl.raiseE("EPropReadOnly");',
  24375. ' }',
  24376. '}, 123);',
  24377. '$mod.TStringHelper.DoIt.call({',
  24378. ' get: function () {',
  24379. ' return "xyz";',
  24380. ' },',
  24381. ' set: function (v) {',
  24382. ' rtl.raiseE("EPropReadOnly");',
  24383. ' }',
  24384. '}, 123);',
  24385. '$mod.TCharHelper.Fly.call({',
  24386. ' get: function () {',
  24387. ' return "c";',
  24388. ' },',
  24389. ' set: function (v) {',
  24390. ' rtl.raiseE("EPropReadOnly");',
  24391. ' }',
  24392. '});',
  24393. '']));
  24394. end;
  24395. procedure TTestModule.TestTypeHelper_JSValue;
  24396. begin
  24397. StartProgram(false);
  24398. Add([
  24399. '{$modeswitch typehelpers}',
  24400. 'type',
  24401. ' TExtValue = type jsvalue;',
  24402. ' THelper = type helper for TExtValue',
  24403. ' function ToStr: String;',
  24404. ' end;',
  24405. 'function THelper.ToStr: String;',
  24406. 'begin',
  24407. 'end;',
  24408. 'var',
  24409. ' s: string;',
  24410. ' v: TExtValue;',
  24411. 'begin',
  24412. ' s:=v.toStr;',
  24413. ' s:=v.toStr();',
  24414. ' TExtValue(s).toStr;',
  24415. '']);
  24416. ConvertProgram;
  24417. CheckSource('TestTypeHelper_JSValue',
  24418. LinesToStr([ // statements
  24419. 'rtl.createHelper(this, "THelper", null, function () {',
  24420. ' this.ToStr = function () {',
  24421. ' var Result = "";',
  24422. ' return Result;',
  24423. ' };',
  24424. '});',
  24425. 'this.s = "";',
  24426. 'this.v = undefined;',
  24427. '']),
  24428. LinesToStr([ // $mod.$main
  24429. '$mod.s = $mod.THelper.ToStr.call({',
  24430. ' p: $mod,',
  24431. ' get: function () {',
  24432. ' return this.p.v;',
  24433. ' },',
  24434. ' set: function (v) {',
  24435. ' this.p.v = v;',
  24436. ' }',
  24437. '});',
  24438. '$mod.s = $mod.THelper.ToStr.call({',
  24439. ' p: $mod,',
  24440. ' get: function () {',
  24441. ' return this.p.v;',
  24442. ' },',
  24443. ' set: function (v) {',
  24444. ' this.p.v = v;',
  24445. ' }',
  24446. '});',
  24447. '$mod.THelper.ToStr.call({',
  24448. ' p: $mod,',
  24449. ' get: function () {',
  24450. ' return this.p.s;',
  24451. ' },',
  24452. ' set: function (v) {',
  24453. ' rtl.raiseE("EPropReadOnly");',
  24454. ' }',
  24455. '});',
  24456. '']));
  24457. end;
  24458. procedure TTestModule.TestTypeHelper_Array;
  24459. begin
  24460. StartProgram(false);
  24461. Add([
  24462. '{$modeswitch typehelpers}',
  24463. 'type',
  24464. ' TArrOfBool = array of boolean;',
  24465. ' TArrOfJS = array of jsvalue;',
  24466. ' THelper = type helper for TArrOfBool',
  24467. ' procedure DoIt(e: byte = 123);',
  24468. ' end;',
  24469. 'procedure THelper.DoIt(e: byte);',
  24470. 'begin',
  24471. ' Self[1]:=true;',
  24472. ' Self[2]:=not Self[3];',
  24473. ' SetLength(Self,4);',
  24474. 'end;',
  24475. 'var',
  24476. ' b: TArrOfBool;',
  24477. ' j: TArrOfJS;',
  24478. 'begin',
  24479. ' b.DoIt;',
  24480. ' TArrOfBool(j).DoIt();',
  24481. '']);
  24482. ConvertProgram;
  24483. CheckSource('TestTypeHelper_Array',
  24484. LinesToStr([ // statements
  24485. 'rtl.createHelper(this, "THelper", null, function () {',
  24486. ' this.DoIt = function (e) {',
  24487. ' this.get()[1] = true;',
  24488. ' this.get()[2] = !this.get()[3];',
  24489. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24490. ' };',
  24491. '});',
  24492. 'this.b = [];',
  24493. 'this.j = [];',
  24494. '']),
  24495. LinesToStr([ // $mod.$main
  24496. '$mod.THelper.DoIt.call({',
  24497. ' p: $mod,',
  24498. ' get: function () {',
  24499. ' return this.p.b;',
  24500. ' },',
  24501. ' set: function (v) {',
  24502. ' this.p.b = v;',
  24503. ' }',
  24504. '}, 123);',
  24505. '$mod.THelper.DoIt.call({',
  24506. ' p: $mod,',
  24507. ' get: function () {',
  24508. ' return this.p.j;',
  24509. ' },',
  24510. ' set: function (v) {',
  24511. ' this.p.j = v;',
  24512. ' }',
  24513. '}, 123);',
  24514. '']));
  24515. end;
  24516. procedure TTestModule.TestTypeHelper_EnumType;
  24517. begin
  24518. StartProgram(false);
  24519. Add([
  24520. '{$modeswitch typehelpers}',
  24521. 'type',
  24522. ' TEnum = (red,blue);',
  24523. ' THelper = type helper for TEnum',
  24524. ' procedure DoIt(e: byte = 123);',
  24525. ' class procedure Swing(w: word); static;',
  24526. ' end;',
  24527. 'procedure THelper.DoIt(e: byte);',
  24528. 'begin',
  24529. ' Self:=red;',
  24530. ' Self:=succ(Self);',
  24531. ' with Self do Doit;',
  24532. 'end;',
  24533. 'class procedure THelper.Swing(w: word);',
  24534. 'begin',
  24535. 'end;',
  24536. 'var e: TEnum;',
  24537. 'begin',
  24538. ' e.DoIt;',
  24539. ' red.DoIt;',
  24540. ' TEnum.blue.DoIt;',
  24541. ' TEnum(1).DoIt;',
  24542. ' TEnum.Swing(3);',
  24543. '']);
  24544. ConvertProgram;
  24545. CheckSource('TestTypeHelper_EnumType',
  24546. LinesToStr([ // statements
  24547. 'this.TEnum = {',
  24548. ' "0": "red",',
  24549. ' red: 0,',
  24550. ' "1": "blue",',
  24551. ' blue: 1',
  24552. '};',
  24553. 'rtl.createHelper(this, "THelper", null, function () {',
  24554. ' this.DoIt = function (e) {',
  24555. ' this.set($mod.TEnum.red);',
  24556. ' this.set(this.get() + 1);',
  24557. ' var $with = this.get();',
  24558. ' $mod.THelper.DoIt.call(this, 123);',
  24559. ' };',
  24560. ' this.Swing = function (w) {',
  24561. ' };',
  24562. '});',
  24563. 'this.e = 0;',
  24564. '']),
  24565. LinesToStr([ // $mod.$main
  24566. '$mod.THelper.DoIt.call({',
  24567. ' p: $mod,',
  24568. ' get: function () {',
  24569. ' return this.p.e;',
  24570. ' },',
  24571. ' set: function (v) {',
  24572. ' this.p.e = v;',
  24573. ' }',
  24574. '}, 123);',
  24575. '$mod.THelper.DoIt.call({',
  24576. ' p: $mod.TEnum,',
  24577. ' get: function () {',
  24578. ' return this.p.red;',
  24579. ' },',
  24580. ' set: function (v) {',
  24581. ' rtl.raiseE("EPropReadOnly");',
  24582. ' }',
  24583. '}, 123);',
  24584. '$mod.THelper.DoIt.call({',
  24585. ' p: $mod.TEnum,',
  24586. ' get: function () {',
  24587. ' return this.p.blue;',
  24588. ' },',
  24589. ' set: function (v) {',
  24590. ' rtl.raiseE("EPropReadOnly");',
  24591. ' }',
  24592. '}, 123);',
  24593. '$mod.THelper.DoIt.call({',
  24594. ' get: function () {',
  24595. ' return 1;',
  24596. ' },',
  24597. ' set: function (v) {',
  24598. ' rtl.raiseE("EPropReadOnly");',
  24599. ' }',
  24600. '}, 123);',
  24601. '$mod.THelper.Swing(3);',
  24602. '']));
  24603. end;
  24604. procedure TTestModule.TestTypeHelper_SetType;
  24605. begin
  24606. StartProgram(false);
  24607. Add([
  24608. '{$modeswitch typehelpers}',
  24609. 'type',
  24610. ' TEnum = (red,blue);',
  24611. ' TSetOfEnum = set of TEnum;',
  24612. ' THelper = type helper for TSetOfEnum',
  24613. ' procedure DoIt(e: byte = 123);',
  24614. ' constructor Init(e: TEnum);',
  24615. ' constructor InitEmpty;',
  24616. ' end;',
  24617. 'procedure THelper.DoIt(e: byte);',
  24618. 'begin',
  24619. ' Self:=[];',
  24620. ' Self:=[red];',
  24621. ' Include(Self,blue);',
  24622. 'end;',
  24623. 'constructor THelper.Init(e: TEnum);',
  24624. 'begin',
  24625. ' Self:=[];',
  24626. ' Self:=[e];',
  24627. ' Include(Self,blue);',
  24628. 'end;',
  24629. 'constructor THelper.InitEmpty;',
  24630. 'begin',
  24631. 'end;',
  24632. 'var s: TSetOfEnum;',
  24633. 'begin',
  24634. ' s.DoIt;',
  24635. //' [red].DoIt;',
  24636. //' with s do DoIt;',
  24637. //' with [red,blue] do DoIt;',
  24638. ' s:=TSetOfEnum.Init(blue);',
  24639. ' s:=s.Init(blue);',
  24640. '']);
  24641. ConvertProgram;
  24642. CheckSource('TestTypeHelper_SetType',
  24643. LinesToStr([ // statements
  24644. 'this.TEnum = {',
  24645. ' "0": "red",',
  24646. ' red: 0,',
  24647. ' "1": "blue",',
  24648. ' blue: 1',
  24649. '};',
  24650. 'rtl.createHelper(this, "THelper", null, function () {',
  24651. ' this.DoIt = function (e) {',
  24652. ' this.set({});',
  24653. ' this.set(rtl.createSet($mod.TEnum.red));',
  24654. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24655. ' };',
  24656. ' this.Init = function (e) {',
  24657. ' this.set({});',
  24658. ' this.set(rtl.createSet(e));',
  24659. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24660. ' return this.get();',
  24661. ' };',
  24662. ' this.InitEmpty = function () {',
  24663. ' return this.get();',
  24664. ' };',
  24665. ' this.$new = function (fn, args) {',
  24666. ' return this[fn].apply({',
  24667. ' p: {},',
  24668. ' get: function () {',
  24669. ' return this.p;',
  24670. ' },',
  24671. ' set: function (v) {',
  24672. ' this.p = v;',
  24673. ' }',
  24674. ' }, args);',
  24675. ' };',
  24676. '});',
  24677. 'this.s = {};',
  24678. '']),
  24679. LinesToStr([ // $mod.$main
  24680. '$mod.THelper.DoIt.call({',
  24681. ' p: $mod,',
  24682. ' get: function () {',
  24683. ' return this.p.s;',
  24684. ' },',
  24685. ' set: function (v) {',
  24686. ' this.p.s = v;',
  24687. ' }',
  24688. '}, 123);',
  24689. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24690. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24691. ' p: $mod,',
  24692. ' get: function () {',
  24693. ' return this.p.s;',
  24694. ' },',
  24695. ' set: function (v) {',
  24696. ' this.p.s = v;',
  24697. ' }',
  24698. '}, $mod.TEnum.blue));',
  24699. '']));
  24700. end;
  24701. procedure TTestModule.TestTypeHelper_InterfaceType;
  24702. begin
  24703. StartProgram(false);
  24704. Add([
  24705. '{$interfaces com}',
  24706. '{$modeswitch typehelpers}',
  24707. 'type',
  24708. ' IUnknown = interface',
  24709. ' function _AddRef: longint;',
  24710. ' function _Release: longint;',
  24711. ' end;',
  24712. ' TObject = class(IUnknown)',
  24713. ' function _AddRef: longint; virtual; abstract;',
  24714. ' function _Release: longint; virtual; abstract;',
  24715. ' end;',
  24716. ' THelper = type helper for IUnknown',
  24717. ' procedure Fly(e: byte = 123);',
  24718. ' class procedure Run; static;',
  24719. ' end;',
  24720. 'var',
  24721. ' i: IUnknown;',
  24722. ' o: TObject;',
  24723. 'procedure THelper.Fly(e: byte);',
  24724. 'begin',
  24725. ' i:=Self;',
  24726. ' o:=Self as TObject;',
  24727. ' Self:=nil;',
  24728. ' Self:=i;',
  24729. ' Self:=o;',
  24730. ' with Self do begin',
  24731. ' Fly;',
  24732. ' Fly();',
  24733. ' end;',
  24734. 'end;',
  24735. 'class procedure THelper.Run;',
  24736. 'var l: IUnknown;',
  24737. 'begin',
  24738. ' l.Fly;',
  24739. ' l.Fly();',
  24740. 'end;',
  24741. 'begin',
  24742. ' i.Fly;',
  24743. ' i.Fly();',
  24744. ' i.Run;',
  24745. ' i.Run();',
  24746. ' IUnknown.Run;',
  24747. ' IUnknown.Run();',
  24748. '']);
  24749. ConvertProgram;
  24750. CheckSource('TestTypeHelper_InterfaceType',
  24751. LinesToStr([ // statements
  24752. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24753. 'rtl.createClass(this, "TObject", null, function () {',
  24754. ' this.$init = function () {',
  24755. ' };',
  24756. ' this.$final = function () {',
  24757. ' };',
  24758. ' rtl.addIntf(this, $mod.IUnknown);',
  24759. '});',
  24760. 'rtl.createHelper(this, "THelper", null, function () {',
  24761. ' this.Fly = function (e) {',
  24762. ' var $ir = rtl.createIntfRefs();',
  24763. ' try {',
  24764. ' rtl.setIntfP($mod, "i", this.get());',
  24765. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24766. ' this.set(null);',
  24767. ' this.set($mod.i);',
  24768. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24769. ' var $with = this.get();',
  24770. ' $mod.THelper.Fly.call(this, 123);',
  24771. ' $mod.THelper.Fly.call(this, 123);',
  24772. ' } finally {',
  24773. ' $ir.free();',
  24774. ' };',
  24775. ' };',
  24776. ' this.Run = function () {',
  24777. ' var l = null;',
  24778. ' try {',
  24779. ' $mod.THelper.Fly.call({',
  24780. ' get: function () {',
  24781. ' return l;',
  24782. ' },',
  24783. ' set: function (v) {',
  24784. ' l = rtl.setIntfL(l, v);',
  24785. ' }',
  24786. ' }, 123);',
  24787. ' $mod.THelper.Fly.call({',
  24788. ' get: function () {',
  24789. ' return l;',
  24790. ' },',
  24791. ' set: function (v) {',
  24792. ' l = rtl.setIntfL(l, v);',
  24793. ' }',
  24794. ' }, 123);',
  24795. ' } finally {',
  24796. ' rtl._Release(l);',
  24797. ' };',
  24798. ' };',
  24799. '});',
  24800. 'this.i = null;',
  24801. 'this.o = null;',
  24802. '']),
  24803. LinesToStr([ // $mod.$main
  24804. '$mod.THelper.Fly.call({',
  24805. ' p: $mod,',
  24806. ' get: function () {',
  24807. ' return this.p.i;',
  24808. ' },',
  24809. ' set: function (v) {',
  24810. ' rtl.setIntfP(this.p, "i", v);',
  24811. ' }',
  24812. '}, 123);',
  24813. '$mod.THelper.Fly.call({',
  24814. ' p: $mod,',
  24815. ' get: function () {',
  24816. ' return this.p.i;',
  24817. ' },',
  24818. ' set: function (v) {',
  24819. ' rtl.setIntfP(this.p, "i", v);',
  24820. ' }',
  24821. '}, 123);',
  24822. '$mod.THelper.Run();',
  24823. '$mod.THelper.Run();',
  24824. '$mod.THelper.Run();',
  24825. '$mod.THelper.Run();',
  24826. '']));
  24827. end;
  24828. procedure TTestModule.TestTypeHelper_NestedSelf;
  24829. begin
  24830. StartProgram(false);
  24831. Add([
  24832. '{$modeswitch typehelpers}',
  24833. 'type',
  24834. ' THelper = type helper for string',
  24835. ' procedure Run(Value: string);',
  24836. ' end;',
  24837. 'procedure THelper.Run(Value: string);',
  24838. ' function Sub(i: nativeint): boolean;',
  24839. ' begin',
  24840. ' Result:=Self[i+1]=Value[i];',
  24841. ' end;',
  24842. 'begin',
  24843. ' if Self[3]=Value[4] then ;',
  24844. 'end;',
  24845. 'begin',
  24846. '']);
  24847. ConvertProgram;
  24848. CheckSource('TestTypeHelper_NestedSelf',
  24849. LinesToStr([ // statements
  24850. 'rtl.createHelper(this, "THelper", null, function () {',
  24851. ' this.Run = function (Value) {',
  24852. ' var $Self = this;',
  24853. ' function Sub(i) {',
  24854. ' var Result = false;',
  24855. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  24856. ' return Result;',
  24857. ' };',
  24858. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  24859. ' };',
  24860. '});',
  24861. '']),
  24862. LinesToStr([ // $mod.$main
  24863. '']));
  24864. end;
  24865. procedure TTestModule.TestProcType;
  24866. begin
  24867. StartProgram(false);
  24868. Add([
  24869. 'type',
  24870. ' TProcInt = procedure(vI: longint = 1);',
  24871. 'procedure DoIt(vJ: longint);',
  24872. 'begin end;',
  24873. 'var',
  24874. ' b: boolean;',
  24875. ' vP, vQ: tprocint;',
  24876. 'begin',
  24877. ' vp:=nil;',
  24878. ' vp:=vp;',
  24879. ' vp:=@doit;',
  24880. ' vp;',
  24881. ' vp();',
  24882. ' vp(2);',
  24883. ' b:=vp=nil;',
  24884. ' b:=nil=vp;',
  24885. ' b:=vp=vq;',
  24886. ' b:=vp=@doit;',
  24887. ' b:=@doit=vp;',
  24888. ' b:=vp<>nil;',
  24889. ' b:=nil<>vp;',
  24890. ' b:=vp<>vq;',
  24891. ' b:=vp<>@doit;',
  24892. ' b:=@doit<>vp;',
  24893. ' b:=Assigned(vp);',
  24894. ' if Assigned(vp) then ;']);
  24895. ConvertProgram;
  24896. CheckSource('TestProcType',
  24897. LinesToStr([ // statements
  24898. 'this.DoIt = function(vJ) {',
  24899. '};',
  24900. 'this.b = false;',
  24901. 'this.vP = null;',
  24902. 'this.vQ = null;'
  24903. ]),
  24904. LinesToStr([ // $mod.$main
  24905. '$mod.vP = null;',
  24906. '$mod.vP = $mod.vP;',
  24907. '$mod.vP = $mod.DoIt;',
  24908. '$mod.vP(1);',
  24909. '$mod.vP(1);',
  24910. '$mod.vP(2);',
  24911. '$mod.b = $mod.vP === null;',
  24912. '$mod.b = null === $mod.vP;',
  24913. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24914. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24915. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24916. '$mod.b = $mod.vP !== null;',
  24917. '$mod.b = null !== $mod.vP;',
  24918. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24919. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24920. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24921. '$mod.b = $mod.vP != null;',
  24922. 'if ($mod.vP != null) ;',
  24923. '']));
  24924. end;
  24925. procedure TTestModule.TestProcType_Arg;
  24926. begin
  24927. StartProgram(false);
  24928. Add([
  24929. 'type',
  24930. ' TProcInt = procedure(vI: longint = 1);',
  24931. 'procedure DoIt(vJ: longint); begin end;',
  24932. 'procedure DoSome(vP, vQ: TProcInt);',
  24933. 'var',
  24934. ' b: boolean;',
  24935. 'begin',
  24936. ' vp:=nil;',
  24937. ' vp:=vp;',
  24938. ' vp:=@doit;',
  24939. ' vp;',
  24940. ' vp();',
  24941. ' vp(2);',
  24942. ' b:=vp=nil;',
  24943. ' b:=nil=vp;',
  24944. ' b:=vp=vq;',
  24945. ' b:=vp=@doit;',
  24946. ' b:=@doit=vp;',
  24947. ' b:=vp<>nil;',
  24948. ' b:=nil<>vp;',
  24949. ' b:=vp<>vq;',
  24950. ' b:=vp<>@doit;',
  24951. ' b:=@doit<>vp;',
  24952. ' b:=Assigned(vp);',
  24953. ' if Assigned(vp) then ;',
  24954. 'end;',
  24955. 'begin',
  24956. ' DoSome(@DoIt,nil);']);
  24957. ConvertProgram;
  24958. CheckSource('TestProcType_Arg',
  24959. LinesToStr([ // statements
  24960. 'this.DoIt = function(vJ) {',
  24961. '};',
  24962. 'this.DoSome = function(vP, vQ) {',
  24963. ' var b = false;',
  24964. ' vP = null;',
  24965. ' vP = vP;',
  24966. ' vP = $mod.DoIt;',
  24967. ' vP(1);',
  24968. ' vP(1);',
  24969. ' vP(2);',
  24970. ' b = vP === null;',
  24971. ' b = null === vP;',
  24972. ' b = rtl.eqCallback(vP,vQ);',
  24973. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  24974. ' b = rtl.eqCallback($mod.DoIt, vP);',
  24975. ' b = vP !== null;',
  24976. ' b = null !== vP;',
  24977. ' b = !rtl.eqCallback(vP, vQ);',
  24978. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  24979. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  24980. ' b = vP != null;',
  24981. ' if (vP != null) ;',
  24982. '};',
  24983. '']),
  24984. LinesToStr([ // $mod.$main
  24985. '$mod.DoSome($mod.DoIt,null);',
  24986. '']));
  24987. end;
  24988. procedure TTestModule.TestProcType_FunctionFPC;
  24989. begin
  24990. StartProgram(false);
  24991. Add('type');
  24992. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24993. Add('function DoIt(vI: longint): longint;');
  24994. Add('begin end;');
  24995. Add('var');
  24996. Add(' b: boolean;');
  24997. Add(' vP, vQ: tfuncint;');
  24998. Add('begin');
  24999. Add(' vp:=nil;');
  25000. Add(' vp:=vp;');
  25001. Add(' vp:=@doit;'); // ok in fpc and delphi
  25002. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25003. Add(' vp;'); // ok in fpc and delphi
  25004. Add(' vp();');
  25005. Add(' vp(2);');
  25006. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25007. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25008. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25009. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25010. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25011. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25012. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25013. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25014. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25015. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25016. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25017. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25018. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25019. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25020. Add(' b:=Assigned(vp);');
  25021. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25022. Add(' doit(vp());'); // ok in fpc and delphi
  25023. Add(' doit(vp(2));'); // ok in fpc and delphi
  25024. ConvertProgram;
  25025. CheckSource('TestProcType_FunctionFPC',
  25026. LinesToStr([ // statements
  25027. 'this.DoIt = function(vI) {',
  25028. ' var Result = 0;',
  25029. ' return Result;',
  25030. '};',
  25031. 'this.b = false;',
  25032. 'this.vP = null;',
  25033. 'this.vQ = null;'
  25034. ]),
  25035. LinesToStr([ // $mod.$main
  25036. '$mod.vP = null;',
  25037. '$mod.vP = $mod.vP;',
  25038. '$mod.vP = $mod.DoIt;',
  25039. '$mod.vP(1);',
  25040. '$mod.vP(1);',
  25041. '$mod.vP(2);',
  25042. '$mod.b = $mod.vP === null;',
  25043. '$mod.b = null === $mod.vP;',
  25044. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25045. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25046. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25047. '$mod.b = 4 === $mod.vP(1);',
  25048. '$mod.b = $mod.vP !== null;',
  25049. '$mod.b = null !== $mod.vP;',
  25050. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25051. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25052. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25053. '$mod.b = 6 !== $mod.vP(1);',
  25054. '$mod.b = $mod.vP != null;',
  25055. '$mod.DoIt($mod.vP(1));',
  25056. '$mod.DoIt($mod.vP(2));',
  25057. '']));
  25058. end;
  25059. procedure TTestModule.TestProcType_FunctionDelphi;
  25060. begin
  25061. StartProgram(false);
  25062. Add('{$mode Delphi}');
  25063. Add('type');
  25064. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25065. Add('function DoIt(vI: longint): longint;');
  25066. Add('begin end;');
  25067. Add('var');
  25068. Add(' b: boolean;');
  25069. Add(' vP, vQ: tfuncint;');
  25070. Add('begin');
  25071. Add(' vp:=nil;');
  25072. Add(' vp:=vp;');
  25073. Add(' vp:=@doit;'); // ok in fpc and delphi
  25074. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25075. Add(' vp;'); // ok in fpc and delphi
  25076. Add(' vp();');
  25077. Add(' vp(2);');
  25078. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25079. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25080. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25081. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25082. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25083. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25084. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25085. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25086. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25087. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25088. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25089. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25090. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25091. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25092. Add(' b:=Assigned(vp);');
  25093. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25094. Add(' doit(vp());'); // ok in fpc and delphi
  25095. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25096. ConvertProgram;
  25097. CheckSource('TestProcType_FunctionDelphi',
  25098. LinesToStr([ // statements
  25099. 'this.DoIt = function(vI) {',
  25100. ' var Result = 0;',
  25101. ' return Result;',
  25102. '};',
  25103. 'this.b = false;',
  25104. 'this.vP = null;',
  25105. 'this.vQ = null;'
  25106. ]),
  25107. LinesToStr([ // $mod.$main
  25108. '$mod.vP = null;',
  25109. '$mod.vP = $mod.vP;',
  25110. '$mod.vP = $mod.DoIt;',
  25111. '$mod.vP = $mod.DoIt;',
  25112. '$mod.vP(1);',
  25113. '$mod.vP(1);',
  25114. '$mod.vP(2);',
  25115. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25116. '$mod.b = $mod.vP(1) === 3;',
  25117. '$mod.b = 4 === $mod.vP(1);',
  25118. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25119. '$mod.b = $mod.vP(1) !== 5;',
  25120. '$mod.b = 6 !== $mod.vP(1);',
  25121. '$mod.b = $mod.vP != null;',
  25122. '$mod.DoIt($mod.vP(1));',
  25123. '$mod.DoIt($mod.vP(1));',
  25124. '$mod.DoIt($mod.vP(2));',
  25125. '']));
  25126. end;
  25127. procedure TTestModule.TestProcType_ProcedureDelphi;
  25128. begin
  25129. StartProgram(false);
  25130. Add('{$mode Delphi}');
  25131. Add('type');
  25132. Add(' TProc = procedure;');
  25133. Add('procedure DoIt;');
  25134. Add('begin end;');
  25135. Add('var');
  25136. Add(' b: boolean;');
  25137. Add(' vP, vQ: tproc;');
  25138. Add('begin');
  25139. Add(' vp:=nil;');
  25140. Add(' vp:=vp;');
  25141. Add(' vp:=vq;');
  25142. 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
  25143. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25144. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25145. Add(' vp;'); // ok in fpc and delphi
  25146. Add(' vp();');
  25147. // equal
  25148. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25149. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25150. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25151. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25152. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25153. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25154. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25155. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25156. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25157. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25158. // unequal
  25159. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25160. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25161. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25162. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25163. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25164. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25165. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25166. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25167. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25168. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25169. Add(' b:=Assigned(vp);');
  25170. ConvertProgram;
  25171. CheckSource('TestProcType_ProcedureDelphi',
  25172. LinesToStr([ // statements
  25173. 'this.DoIt = function() {',
  25174. '};',
  25175. 'this.b = false;',
  25176. 'this.vP = null;',
  25177. 'this.vQ = null;'
  25178. ]),
  25179. LinesToStr([ // $mod.$main
  25180. '$mod.vP = null;',
  25181. '$mod.vP = $mod.vP;',
  25182. '$mod.vP = $mod.vQ;',
  25183. '$mod.vP = $mod.DoIt;',
  25184. '$mod.vP = $mod.DoIt;',
  25185. '$mod.vP();',
  25186. '$mod.vP();',
  25187. '$mod.b = $mod.vP === null;',
  25188. '$mod.b = null === $mod.vP;',
  25189. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25190. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25191. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25192. '$mod.b = $mod.vP !== null;',
  25193. '$mod.b = null !== $mod.vP;',
  25194. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25195. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25196. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25197. '$mod.b = $mod.vP != null;',
  25198. '']));
  25199. end;
  25200. procedure TTestModule.TestProcType_AsParam;
  25201. begin
  25202. StartProgram(false);
  25203. Add('type');
  25204. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25205. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25206. Add('var vJ: tfuncint;');
  25207. Add('begin');
  25208. Add(' vg:=vg;');
  25209. Add(' vj:=vh;');
  25210. Add(' vi:=vi;');
  25211. Add(' doit(vg,vg,vg);');
  25212. Add(' doit(vh,vh,vj);');
  25213. Add(' doit(vi,vi,vi);');
  25214. Add(' doit(vj,vj,vj);');
  25215. Add('end;');
  25216. Add('var i: tfuncint;');
  25217. Add('begin');
  25218. Add(' doit(i,i,i);');
  25219. ConvertProgram;
  25220. CheckSource('TestProcType_AsParam',
  25221. LinesToStr([ // statements
  25222. 'this.DoIt = function (vG,vH,vI) {',
  25223. ' var vJ = null;',
  25224. ' vG = vG;',
  25225. ' vJ = vH;',
  25226. ' vI.set(vI.get());',
  25227. ' $mod.DoIt(vG, vG, {',
  25228. ' get: function () {',
  25229. ' return vG;',
  25230. ' },',
  25231. ' set: function (v) {',
  25232. ' vG = v;',
  25233. ' }',
  25234. ' });',
  25235. ' $mod.DoIt(vH, vH, {',
  25236. ' get: function () {',
  25237. ' return vJ;',
  25238. ' },',
  25239. ' set: function (v) {',
  25240. ' vJ = v;',
  25241. ' }',
  25242. ' });',
  25243. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25244. ' $mod.DoIt(vJ, vJ, {',
  25245. ' get: function () {',
  25246. ' return vJ;',
  25247. ' },',
  25248. ' set: function (v) {',
  25249. ' vJ = v;',
  25250. ' }',
  25251. ' });',
  25252. '};',
  25253. 'this.i = null;'
  25254. ]),
  25255. LinesToStr([
  25256. '$mod.DoIt($mod.i,$mod.i,{',
  25257. ' p: $mod,',
  25258. ' get: function () {',
  25259. ' return this.p.i;',
  25260. ' },',
  25261. ' set: function (v) {',
  25262. ' this.p.i = v;',
  25263. ' }',
  25264. '});'
  25265. ]));
  25266. end;
  25267. procedure TTestModule.TestProcType_MethodFPC;
  25268. begin
  25269. StartProgram(false);
  25270. Add('type');
  25271. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25272. Add(' TObject = class');
  25273. Add(' function DoIt(vA: longint = 1): longint;');
  25274. Add(' end;');
  25275. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25276. Add('begin');
  25277. Add('end;');
  25278. Add('var');
  25279. Add(' Obj: TObject;');
  25280. Add(' vP: tfuncint;');
  25281. Add(' b: boolean;');
  25282. Add('begin');
  25283. Add(' vp:[email protected];'); // ok in fpc and delphi
  25284. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25285. Add(' vp;'); // ok in fpc and delphi
  25286. Add(' vp();');
  25287. Add(' vp(2);');
  25288. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25289. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25290. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25291. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25292. ConvertProgram;
  25293. CheckSource('TestProcType_MethodFPC',
  25294. LinesToStr([ // statements
  25295. 'rtl.createClass(this, "TObject", null, function () {',
  25296. ' this.$init = function () {',
  25297. ' };',
  25298. ' this.$final = function () {',
  25299. ' };',
  25300. ' this.DoIt = function (vA) {',
  25301. ' var Result = 0;',
  25302. ' return Result;',
  25303. ' };',
  25304. '});',
  25305. 'this.Obj = null;',
  25306. 'this.vP = null;',
  25307. 'this.b = false;'
  25308. ]),
  25309. LinesToStr([
  25310. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25311. '$mod.vP(1);',
  25312. '$mod.vP(1);',
  25313. '$mod.vP(2);',
  25314. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25315. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25316. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25317. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25318. '']));
  25319. end;
  25320. procedure TTestModule.TestProcType_MethodDelphi;
  25321. begin
  25322. StartProgram(false);
  25323. Add([
  25324. '{$mode delphi}',
  25325. 'type',
  25326. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25327. ' TObject = class',
  25328. ' function DoIt(vA: longint = 1): longint;',
  25329. ' end;',
  25330. 'function TObject.DoIt(vA: longint = 1): longint;',
  25331. 'begin',
  25332. 'end;',
  25333. 'var',
  25334. ' Obj: TObject;',
  25335. ' vP: tfuncint;',
  25336. ' b: boolean;',
  25337. 'begin',
  25338. ' vp:[email protected];', // ok in fpc and delphi
  25339. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25340. ' vp;', // ok in fpc and delphi
  25341. ' vp();',
  25342. ' vp(2);',
  25343. //' b:[email protected];', // ok in fpc, illegal in delphi
  25344. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25345. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25346. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25347. '']);
  25348. ConvertProgram;
  25349. CheckSource('TestProcType_MethodDelphi',
  25350. LinesToStr([ // statements
  25351. 'rtl.createClass(this, "TObject", null, function () {',
  25352. ' this.$init = function () {',
  25353. ' };',
  25354. ' this.$final = function () {',
  25355. ' };',
  25356. ' this.DoIt = function (vA) {',
  25357. ' var Result = 0;',
  25358. ' return Result;',
  25359. ' };',
  25360. '});',
  25361. 'this.Obj = null;',
  25362. 'this.vP = null;',
  25363. 'this.b = false;'
  25364. ]),
  25365. LinesToStr([
  25366. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25367. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25368. '$mod.vP(1);',
  25369. '$mod.vP(1);',
  25370. '$mod.vP(2);',
  25371. '']));
  25372. end;
  25373. procedure TTestModule.TestProcType_PropertyFPC;
  25374. begin
  25375. StartProgram(false);
  25376. Add('type');
  25377. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25378. Add(' TObject = class');
  25379. Add(' FOnFoo: TFuncInt;');
  25380. Add(' function DoIt(vA: longint = 1): longint;');
  25381. Add(' function GetFoo: TFuncInt;');
  25382. Add(' procedure SetFoo(const Value: TFuncInt);');
  25383. Add(' function GetEvents(Index: longint): TFuncInt;');
  25384. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25385. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25386. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25387. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25388. Add(' end;');
  25389. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25390. Add('function tobject.getfoo: tfuncint; begin end;');
  25391. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25392. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25393. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25394. Add('var');
  25395. Add(' Obj: TObject;');
  25396. Add(' vP: tfuncint;');
  25397. Add(' b: boolean;');
  25398. Add('begin');
  25399. Add(' obj.onfoo:=nil;');
  25400. Add(' obj.onbar:=nil;');
  25401. Add(' obj.events[1]:=nil;');
  25402. Add(' obj.onfoo:=obj.onfoo;');
  25403. Add(' obj.onbar:=obj.onbar;');
  25404. Add(' obj.events[2]:=obj.events[3];');
  25405. Add(' obj.onfoo:[email protected];');
  25406. Add(' obj.onbar:[email protected];');
  25407. Add(' obj.events[4]:[email protected];');
  25408. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25409. //Add(' obj.onbar:=obj.doit;'); // delphi
  25410. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25411. Add(' obj.onfoo;');
  25412. Add(' obj.onbar;');
  25413. //Add(' obj.events[5];'); ToDo in pasresolver
  25414. Add(' obj.onfoo();');
  25415. Add(' obj.onbar();');
  25416. Add(' obj.events[6]();');
  25417. Add(' b:=obj.onfoo=nil;');
  25418. Add(' b:=obj.onbar=nil;');
  25419. Add(' b:=obj.events[7]=nil;');
  25420. Add(' b:=obj.onfoo<>nil;');
  25421. Add(' b:=obj.onbar<>nil;');
  25422. Add(' b:=obj.events[8]<>nil;');
  25423. Add(' b:=obj.onfoo=vp;');
  25424. Add(' b:=obj.onbar=vp;');
  25425. Add(' b:=obj.events[9]=vp;');
  25426. Add(' b:=obj.onfoo=obj.onfoo;');
  25427. Add(' b:=obj.onbar=obj.onfoo;');
  25428. Add(' b:=obj.events[10]=obj.onfoo;');
  25429. Add(' b:=obj.onfoo<>obj.onfoo;');
  25430. Add(' b:=obj.onbar<>obj.onfoo;');
  25431. Add(' b:=obj.events[11]<>obj.onfoo;');
  25432. Add(' b:[email protected];');
  25433. Add(' b:[email protected];');
  25434. Add(' b:=obj.events[12][email protected];');
  25435. Add(' b:=obj.onfoo<>@obj.doit;');
  25436. Add(' b:=obj.onbar<>@obj.doit;');
  25437. Add(' b:=obj.events[12]<>@obj.doit;');
  25438. Add(' b:=Assigned(obj.onfoo);');
  25439. Add(' b:=Assigned(obj.onbar);');
  25440. Add(' b:=Assigned(obj.events[13]);');
  25441. ConvertProgram;
  25442. CheckSource('TestProcType_PropertyFPC',
  25443. LinesToStr([ // statements
  25444. 'rtl.createClass(this, "TObject", null, function () {',
  25445. ' this.$init = function () {',
  25446. ' this.FOnFoo = null;',
  25447. ' };',
  25448. ' this.$final = function () {',
  25449. ' this.FOnFoo = undefined;',
  25450. ' };',
  25451. ' this.DoIt = function (vA) {',
  25452. ' var Result = 0;',
  25453. ' return Result;',
  25454. ' };',
  25455. 'this.GetFoo = function () {',
  25456. ' var Result = null;',
  25457. ' return Result;',
  25458. '};',
  25459. 'this.SetFoo = function (Value) {',
  25460. '};',
  25461. 'this.GetEvents = function (Index) {',
  25462. ' var Result = null;',
  25463. ' return Result;',
  25464. '};',
  25465. 'this.SetEvents = function (Index, Value) {',
  25466. '};',
  25467. '});',
  25468. 'this.Obj = null;',
  25469. 'this.vP = null;',
  25470. 'this.b = false;'
  25471. ]),
  25472. LinesToStr([
  25473. '$mod.Obj.FOnFoo = null;',
  25474. '$mod.Obj.SetFoo(null);',
  25475. '$mod.Obj.SetEvents(1, null);',
  25476. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25477. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25478. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25479. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25480. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25481. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25482. '$mod.Obj.FOnFoo(1);',
  25483. '$mod.Obj.GetFoo();',
  25484. '$mod.Obj.FOnFoo(1);',
  25485. '$mod.Obj.GetFoo()(1);',
  25486. '$mod.Obj.GetEvents(6)(1);',
  25487. '$mod.b = $mod.Obj.FOnFoo === null;',
  25488. '$mod.b = $mod.Obj.GetFoo() === null;',
  25489. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25490. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25491. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25492. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25493. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25494. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25495. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25496. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25497. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25498. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25499. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25500. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25501. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25502. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25503. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25504. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25505. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25506. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25507. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25508. '$mod.b = $mod.Obj.FOnFoo != null;',
  25509. '$mod.b = $mod.Obj.GetFoo() != null;',
  25510. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25511. '']));
  25512. end;
  25513. procedure TTestModule.TestProcType_PropertyDelphi;
  25514. begin
  25515. StartProgram(false);
  25516. Add('{$mode delphi}');
  25517. Add('type');
  25518. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25519. Add(' TObject = class');
  25520. Add(' FOnFoo: TFuncInt;');
  25521. Add(' function DoIt(vA: longint = 1): longint;');
  25522. Add(' function GetFoo: TFuncInt;');
  25523. Add(' procedure SetFoo(const Value: TFuncInt);');
  25524. Add(' function GetEvents(Index: longint): TFuncInt;');
  25525. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25526. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25527. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25528. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25529. Add(' end;');
  25530. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25531. Add('function tobject.getfoo: tfuncint; begin end;');
  25532. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25533. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25534. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25535. Add('var');
  25536. Add(' Obj: TObject;');
  25537. Add(' vP: tfuncint;');
  25538. Add(' b: boolean;');
  25539. Add('begin');
  25540. Add(' obj.onfoo:=nil;');
  25541. Add(' obj.onbar:=nil;');
  25542. Add(' obj.events[1]:=nil;');
  25543. Add(' obj.onfoo:=obj.onfoo;');
  25544. Add(' obj.onbar:=obj.onbar;');
  25545. Add(' obj.events[2]:=obj.events[3];');
  25546. Add(' obj.onfoo:[email protected];');
  25547. Add(' obj.onbar:[email protected];');
  25548. Add(' obj.events[4]:[email protected];');
  25549. Add(' obj.onfoo:=obj.doit;'); // delphi
  25550. Add(' obj.onbar:=obj.doit;'); // delphi
  25551. Add(' obj.events[4]:=obj.doit;'); // delphi
  25552. Add(' obj.onfoo;');
  25553. Add(' obj.onbar;');
  25554. //Add(' obj.events[5];'); ToDo in pasresolver
  25555. Add(' obj.onfoo();');
  25556. Add(' obj.onbar();');
  25557. Add(' obj.events[6]();');
  25558. //Add(' b:=obj.onfoo=nil;'); // fpc
  25559. //Add(' b:=obj.onbar=nil;'); // fpc
  25560. //Add(' b:=obj.events[7]=nil;'); // fpc
  25561. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25562. //Add(' b:=obj.onbar<>nil;'); // fpc
  25563. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25564. Add(' b:=obj.onfoo=vp;');
  25565. Add(' b:=obj.onbar=vp;');
  25566. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25567. Add(' b:=obj.onfoo=obj.onfoo;');
  25568. Add(' b:=obj.onbar=obj.onfoo;');
  25569. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25570. Add(' b:=obj.onfoo<>obj.onfoo;');
  25571. Add(' b:=obj.onbar<>obj.onfoo;');
  25572. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25573. //Add(' b:[email protected];'); // fpc
  25574. //Add(' b:[email protected];'); // fpc
  25575. //Add(' b:=obj.events[12][email protected];'); // fpc
  25576. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25577. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25578. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25579. Add(' b:=Assigned(obj.onfoo);');
  25580. Add(' b:=Assigned(obj.onbar);');
  25581. Add(' b:=Assigned(obj.events[13]);');
  25582. ConvertProgram;
  25583. CheckSource('TestProcType_PropertyDelphi',
  25584. LinesToStr([ // statements
  25585. 'rtl.createClass(this, "TObject", null, function () {',
  25586. ' this.$init = function () {',
  25587. ' this.FOnFoo = null;',
  25588. ' };',
  25589. ' this.$final = function () {',
  25590. ' this.FOnFoo = undefined;',
  25591. ' };',
  25592. ' this.DoIt = function (vA) {',
  25593. ' var Result = 0;',
  25594. ' return Result;',
  25595. ' };',
  25596. 'this.GetFoo = function () {',
  25597. ' var Result = null;',
  25598. ' return Result;',
  25599. '};',
  25600. 'this.SetFoo = function (Value) {',
  25601. '};',
  25602. 'this.GetEvents = function (Index) {',
  25603. ' var Result = null;',
  25604. ' return Result;',
  25605. '};',
  25606. 'this.SetEvents = function (Index, Value) {',
  25607. '};',
  25608. '});',
  25609. 'this.Obj = null;',
  25610. 'this.vP = null;',
  25611. 'this.b = false;'
  25612. ]),
  25613. LinesToStr([
  25614. '$mod.Obj.FOnFoo = null;',
  25615. '$mod.Obj.SetFoo(null);',
  25616. '$mod.Obj.SetEvents(1, null);',
  25617. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25618. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25619. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25620. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25621. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25622. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25623. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25624. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25625. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25626. '$mod.Obj.FOnFoo(1);',
  25627. '$mod.Obj.GetFoo();',
  25628. '$mod.Obj.FOnFoo(1);',
  25629. '$mod.Obj.GetFoo()(1);',
  25630. '$mod.Obj.GetEvents(6)(1);',
  25631. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25632. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25633. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25634. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25635. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25636. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25637. '$mod.b = $mod.Obj.FOnFoo != null;',
  25638. '$mod.b = $mod.Obj.GetFoo() != null;',
  25639. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25640. '']));
  25641. end;
  25642. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25643. begin
  25644. StartProgram(false);
  25645. Add('type');
  25646. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25647. Add(' TObject = class');
  25648. Add(' FOnFoo: TFuncInt;');
  25649. Add(' function DoIt(vA: longint = 1): longint;');
  25650. Add(' function GetFoo: TFuncInt;');
  25651. Add(' procedure SetFoo(const Value: TFuncInt);');
  25652. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25653. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25654. Add(' end;');
  25655. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25656. Add('function tobject.getfoo: tfuncint; begin end;');
  25657. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25658. Add('var');
  25659. Add(' Obj: TObject;');
  25660. Add(' vP: tfuncint;');
  25661. Add(' b: boolean;');
  25662. Add('begin');
  25663. Add('with obj do begin');
  25664. Add(' fonfoo:=nil;');
  25665. Add(' onfoo:=nil;');
  25666. Add(' onbar:=nil;');
  25667. Add(' fonfoo:=fonfoo;');
  25668. Add(' onfoo:=onfoo;');
  25669. Add(' onbar:=onbar;');
  25670. Add(' fonfoo:=@doit;');
  25671. Add(' onfoo:=@doit;');
  25672. Add(' onbar:=@doit;');
  25673. //Add(' fonfoo:=doit;'); // delphi
  25674. //Add(' onfoo:=doit;'); // delphi
  25675. //Add(' onbar:=doit;'); // delphi
  25676. Add(' fonfoo;');
  25677. Add(' onfoo;');
  25678. Add(' onbar;');
  25679. Add(' fonfoo();');
  25680. Add(' onfoo();');
  25681. Add(' onbar();');
  25682. Add(' b:=fonfoo=nil;');
  25683. Add(' b:=onfoo=nil;');
  25684. Add(' b:=onbar=nil;');
  25685. Add(' b:=fonfoo<>nil;');
  25686. Add(' b:=onfoo<>nil;');
  25687. Add(' b:=onbar<>nil;');
  25688. Add(' b:=fonfoo=vp;');
  25689. Add(' b:=onfoo=vp;');
  25690. Add(' b:=onbar=vp;');
  25691. Add(' b:=fonfoo=fonfoo;');
  25692. Add(' b:=onfoo=onfoo;');
  25693. Add(' b:=onbar=onfoo;');
  25694. Add(' b:=fonfoo<>fonfoo;');
  25695. Add(' b:=onfoo<>onfoo;');
  25696. Add(' b:=onbar<>onfoo;');
  25697. Add(' b:=fonfoo=@doit;');
  25698. Add(' b:=onfoo=@doit;');
  25699. Add(' b:=onbar=@doit;');
  25700. Add(' b:=fonfoo<>@doit;');
  25701. Add(' b:=onfoo<>@doit;');
  25702. Add(' b:=onbar<>@doit;');
  25703. Add(' b:=Assigned(fonfoo);');
  25704. Add(' b:=Assigned(onfoo);');
  25705. Add(' b:=Assigned(onbar);');
  25706. Add('end;');
  25707. ConvertProgram;
  25708. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25709. LinesToStr([ // statements
  25710. 'rtl.createClass(this, "TObject", null, function () {',
  25711. ' this.$init = function () {',
  25712. ' this.FOnFoo = null;',
  25713. ' };',
  25714. ' this.$final = function () {',
  25715. ' this.FOnFoo = undefined;',
  25716. ' };',
  25717. ' this.DoIt = function (vA) {',
  25718. ' var Result = 0;',
  25719. ' return Result;',
  25720. ' };',
  25721. ' this.GetFoo = function () {',
  25722. ' var Result = null;',
  25723. ' return Result;',
  25724. ' };',
  25725. ' this.SetFoo = function (Value) {',
  25726. ' };',
  25727. '});',
  25728. 'this.Obj = null;',
  25729. 'this.vP = null;',
  25730. 'this.b = false;'
  25731. ]),
  25732. LinesToStr([
  25733. 'var $with = $mod.Obj;',
  25734. '$with.FOnFoo = null;',
  25735. '$with.FOnFoo = null;',
  25736. '$with.SetFoo(null);',
  25737. '$with.FOnFoo = $with.FOnFoo;',
  25738. '$with.FOnFoo = $with.FOnFoo;',
  25739. '$with.SetFoo($with.GetFoo());',
  25740. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25741. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25742. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25743. '$with.FOnFoo(1);',
  25744. '$with.FOnFoo(1);',
  25745. '$with.GetFoo();',
  25746. '$with.FOnFoo(1);',
  25747. '$with.FOnFoo(1);',
  25748. '$with.GetFoo()(1);',
  25749. '$mod.b = $with.FOnFoo === null;',
  25750. '$mod.b = $with.FOnFoo === null;',
  25751. '$mod.b = $with.GetFoo() === null;',
  25752. '$mod.b = $with.FOnFoo !== null;',
  25753. '$mod.b = $with.FOnFoo !== null;',
  25754. '$mod.b = $with.GetFoo() !== null;',
  25755. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25756. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25757. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25758. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25759. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25760. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25761. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25762. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25763. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25764. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25765. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25766. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25767. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25768. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25769. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25770. '$mod.b = $with.FOnFoo != null;',
  25771. '$mod.b = $with.FOnFoo != null;',
  25772. '$mod.b = $with.GetFoo() != null;',
  25773. '']));
  25774. end;
  25775. procedure TTestModule.TestProcType_Nested;
  25776. begin
  25777. StartProgram(false);
  25778. Add([
  25779. 'type',
  25780. ' TProcInt = procedure(vI: longint = 1);',
  25781. 'procedure DoIt(vJ: longint);',
  25782. 'var aProc: TProcInt;',
  25783. ' b: boolean;',
  25784. ' procedure Sub(vK: longint);',
  25785. ' var aSub: TProcInt;',
  25786. ' procedure SubSub(vK: longint);',
  25787. ' var aSubSub: TProcInt;',
  25788. ' begin;',
  25789. ' aProc:=@DoIt;',
  25790. ' aSub:=@DoIt;',
  25791. ' aSubSub:=@DoIt;',
  25792. ' aProc:=@Sub;',
  25793. ' aSub:=@Sub;',
  25794. ' aSubSub:=@Sub;',
  25795. ' aProc:=@SubSub;',
  25796. ' aSub:=@SubSub;',
  25797. ' aSubSub:=@SubSub;',
  25798. ' end;',
  25799. ' begin;',
  25800. ' end;',
  25801. 'begin;',
  25802. ' aProc:=@Sub;',
  25803. ' b:=aProc=@Sub;',
  25804. ' b:=@Sub=aProc;',
  25805. 'end;',
  25806. 'begin',
  25807. '']);
  25808. ConvertProgram;
  25809. CheckSource('TestProcType_Nested',
  25810. LinesToStr([ // statements
  25811. 'this.DoIt = function (vJ) {',
  25812. ' var aProc = null;',
  25813. ' var b = false;',
  25814. ' function Sub(vK) {',
  25815. ' var aSub = null;',
  25816. ' function SubSub(vK) {',
  25817. ' var aSubSub = null;',
  25818. ' aProc = $mod.DoIt;',
  25819. ' aSub = $mod.DoIt;',
  25820. ' aSubSub = $mod.DoIt;',
  25821. ' aProc = Sub;',
  25822. ' aSub = Sub;',
  25823. ' aSubSub = Sub;',
  25824. ' aProc = SubSub;',
  25825. ' aSub = SubSub;',
  25826. ' aSubSub = SubSub;',
  25827. ' };',
  25828. ' };',
  25829. ' aProc = Sub;',
  25830. ' b = rtl.eqCallback(aProc, Sub);',
  25831. ' b = rtl.eqCallback(Sub, aProc);',
  25832. '};',
  25833. '']),
  25834. LinesToStr([ // $mod.$main
  25835. '']));
  25836. end;
  25837. procedure TTestModule.TestProcType_NestedOfObject;
  25838. begin
  25839. StartProgram(false);
  25840. Add([
  25841. 'type',
  25842. ' TProcInt = procedure(vI: longint = 1) of object;',
  25843. ' TObject = class',
  25844. ' procedure DoIt(vJ: longint);',
  25845. ' end;',
  25846. 'procedure TObject.DoIt(vJ: longint);',
  25847. 'var aProc: TProcInt;',
  25848. ' b: boolean;',
  25849. ' procedure Sub(vK: longint);',
  25850. ' var aSub: TProcInt;',
  25851. ' procedure SubSub(vK: longint);',
  25852. ' var aSubSub: TProcInt;',
  25853. ' begin;',
  25854. ' aProc:=@DoIt;',
  25855. ' aSub:=@DoIt;',
  25856. ' aSubSub:=@DoIt;',
  25857. ' aProc:=@Sub;',
  25858. ' aSub:=@Sub;',
  25859. ' aSubSub:=@Sub;',
  25860. ' aProc:=@SubSub;',
  25861. ' aSub:=@SubSub;',
  25862. ' aSubSub:=@SubSub;',
  25863. ' end;',
  25864. ' begin;',
  25865. ' end;',
  25866. 'begin;',
  25867. ' aProc:=@Sub;',
  25868. ' b:=aProc=@Sub;',
  25869. ' b:=@Sub=aProc;',
  25870. 'end;',
  25871. 'begin',
  25872. '']);
  25873. ConvertProgram;
  25874. CheckSource('TestProcType_Nested',
  25875. LinesToStr([ // statements
  25876. 'rtl.createClass(this, "TObject", null, function () {',
  25877. ' this.$init = function () {',
  25878. ' };',
  25879. ' this.$final = function () {',
  25880. ' };',
  25881. ' this.DoIt = function (vJ) {',
  25882. ' var $Self = this;',
  25883. ' var aProc = null;',
  25884. ' var b = false;',
  25885. ' function Sub(vK) {',
  25886. ' var aSub = null;',
  25887. ' function SubSub(vK) {',
  25888. ' var aSubSub = null;',
  25889. ' aProc = rtl.createCallback($Self, "DoIt");',
  25890. ' aSub = rtl.createCallback($Self, "DoIt");',
  25891. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  25892. ' aProc = Sub;',
  25893. ' aSub = Sub;',
  25894. ' aSubSub = Sub;',
  25895. ' aProc = SubSub;',
  25896. ' aSub = SubSub;',
  25897. ' aSubSub = SubSub;',
  25898. ' };',
  25899. ' };',
  25900. ' aProc = Sub;',
  25901. ' b = rtl.eqCallback(aProc, Sub);',
  25902. ' b = rtl.eqCallback(Sub, aProc);',
  25903. ' };',
  25904. '});',
  25905. '']),
  25906. LinesToStr([ // $mod.$main
  25907. '']));
  25908. end;
  25909. procedure TTestModule.TestProcType_ReferenceToProc;
  25910. begin
  25911. StartProgram(false);
  25912. Add([
  25913. 'type',
  25914. ' TProcRef = reference to procedure(i: longint = 0);',
  25915. ' TFuncRef = reference to function(i: longint = 0): longint;',
  25916. 'var',
  25917. ' p: TProcRef;',
  25918. ' f: TFuncRef;',
  25919. 'procedure DoIt(i: longint);',
  25920. 'begin',
  25921. 'end;',
  25922. 'function GetIt(i: longint): longint;',
  25923. 'begin',
  25924. ' p:=@DoIt;',
  25925. ' f:=@GetIt;',
  25926. ' f;',
  25927. ' f();',
  25928. ' f(1);',
  25929. 'end;',
  25930. 'begin',
  25931. ' p:=@DoIt;',
  25932. ' f:=@GetIt;',
  25933. ' f;',
  25934. ' f();',
  25935. ' f(1);',
  25936. ' p:=TProcRef(f);',
  25937. '']);
  25938. ConvertProgram;
  25939. CheckSource('TestProcType_ReferenceToProc',
  25940. LinesToStr([ // statements
  25941. 'this.p = null;',
  25942. 'this.f = null;',
  25943. 'this.DoIt = function (i) {',
  25944. '};',
  25945. 'this.GetIt = function (i) {',
  25946. ' var Result = 0;',
  25947. ' $mod.p = $mod.DoIt;',
  25948. ' $mod.f = $mod.GetIt;',
  25949. ' $mod.f(0);',
  25950. ' $mod.f(0);',
  25951. ' $mod.f(1);',
  25952. ' return Result;',
  25953. '};',
  25954. '']),
  25955. LinesToStr([ // $mod.$main
  25956. '$mod.p = $mod.DoIt;',
  25957. '$mod.f = $mod.GetIt;',
  25958. '$mod.f(0);',
  25959. '$mod.f(0);',
  25960. '$mod.f(1);',
  25961. '$mod.p = $mod.f;',
  25962. '']));
  25963. end;
  25964. procedure TTestModule.TestProcType_ReferenceToMethod;
  25965. begin
  25966. StartProgram(false);
  25967. Add([
  25968. 'type',
  25969. ' TFuncRef = reference to function(i: longint = 5): longint;',
  25970. ' TObject = class',
  25971. ' function Grow(s: longint): longint;',
  25972. ' end;',
  25973. 'var',
  25974. ' f: tfuncref;',
  25975. 'function tobject.grow(s: longint): longint;',
  25976. ' function GrowSub(i: longint): longint;',
  25977. ' begin',
  25978. ' f:=@grow;',
  25979. ' f:=@growsub;',
  25980. ' end;',
  25981. 'begin',
  25982. ' f:=@grow;',
  25983. ' f:=@growsub;',
  25984. 'end;',
  25985. 'begin',
  25986. '']);
  25987. ConvertProgram;
  25988. CheckSource('TestProcType_ReferenceToMethod',
  25989. LinesToStr([ // statements
  25990. 'rtl.createClass(this, "TObject", null, function () {',
  25991. ' this.$init = function () {',
  25992. ' };',
  25993. ' this.$final = function () {',
  25994. ' };',
  25995. ' this.Grow = function (s) {',
  25996. ' var $Self = this;',
  25997. ' var Result = 0;',
  25998. ' function GrowSub(i) {',
  25999. ' var Result = 0;',
  26000. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26001. ' $mod.f = GrowSub;',
  26002. ' return Result;',
  26003. ' };',
  26004. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26005. ' $mod.f = GrowSub;',
  26006. ' return Result;',
  26007. ' };',
  26008. '});',
  26009. 'this.f = null;',
  26010. '']),
  26011. LinesToStr([ // $mod.$main
  26012. '']));
  26013. end;
  26014. procedure TTestModule.TestProcType_Typecast;
  26015. begin
  26016. StartProgram(false);
  26017. Add([
  26018. 'type',
  26019. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26020. ' TEvent = procedure of object;',
  26021. ' TGetter = function:longint of object;',
  26022. ' TProcA = procedure(i: longint);',
  26023. ' TFuncB = function(i, j: longint): longint;',
  26024. 'procedure DoIt(); varargs; begin end;',
  26025. 'var',
  26026. ' Notify: tnotifyevent;',
  26027. ' Event: tevent;',
  26028. ' Getter: tgetter;',
  26029. ' ProcA: tproca;',
  26030. ' FuncB: tfuncb;',
  26031. ' p: pointer;',
  26032. 'begin',
  26033. ' notify:=tnotifyevent(event);',
  26034. ' event:=tevent(event);',
  26035. ' event:=tevent(notify);',
  26036. ' event:=tevent(getter);',
  26037. ' event:=tevent(proca);',
  26038. ' proca:=tproca(funcb);',
  26039. ' funcb:=tfuncb(funcb);',
  26040. ' funcb:=tfuncb(proca);',
  26041. ' funcb:=tfuncb(getter);',
  26042. ' proca:=tproca(p);',
  26043. ' funcb:=tfuncb(p);',
  26044. ' getter:=tgetter(p);',
  26045. ' p:=pointer(notify);',
  26046. ' p:=notify;',
  26047. ' p:=pointer(proca);',
  26048. ' p:=proca;',
  26049. ' p:=pointer(funcb);',
  26050. ' p:=funcb;',
  26051. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26052. '']);
  26053. ConvertProgram;
  26054. CheckSource('TestProcType_Typecast',
  26055. LinesToStr([ // statements
  26056. 'this.DoIt = function () {',
  26057. '};',
  26058. 'this.Notify = null;',
  26059. 'this.Event = null;',
  26060. 'this.Getter = null;',
  26061. 'this.ProcA = null;',
  26062. 'this.FuncB = null;',
  26063. 'this.p = null;',
  26064. '']),
  26065. LinesToStr([ // $mod.$main
  26066. '$mod.Notify = $mod.Event;',
  26067. '$mod.Event = $mod.Event;',
  26068. '$mod.Event = $mod.Notify;',
  26069. '$mod.Event = $mod.Getter;',
  26070. '$mod.Event = $mod.ProcA;',
  26071. '$mod.ProcA = $mod.FuncB;',
  26072. '$mod.FuncB = $mod.FuncB;',
  26073. '$mod.FuncB = $mod.ProcA;',
  26074. '$mod.FuncB = $mod.Getter;',
  26075. '$mod.ProcA = $mod.p;',
  26076. '$mod.FuncB = $mod.p;',
  26077. '$mod.Getter = $mod.p;',
  26078. '$mod.p = $mod.Notify;',
  26079. '$mod.p = $mod.Notify;',
  26080. '$mod.p = $mod.ProcA;',
  26081. '$mod.p = $mod.ProcA;',
  26082. '$mod.p = $mod.FuncB;',
  26083. '$mod.p = $mod.FuncB;',
  26084. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26085. '']));
  26086. end;
  26087. procedure TTestModule.TestProcType_PassProcToUntyped;
  26088. begin
  26089. StartProgram(false);
  26090. Add([
  26091. 'type',
  26092. ' TEvent = procedure of object;',
  26093. ' TFunc = function: longint;',
  26094. 'procedure DoIt(); varargs; begin end;',
  26095. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26096. 'var',
  26097. ' Event: tevent;',
  26098. ' Func: TFunc;',
  26099. 'begin',
  26100. ' doit(event,func);',
  26101. ' dosome(event,event,event);',
  26102. ' dosome(func,func,func);',
  26103. '']);
  26104. ConvertProgram;
  26105. CheckSource('TestProcType_PassProcToUntyped',
  26106. LinesToStr([ // statements
  26107. 'this.DoIt = function () {',
  26108. '};',
  26109. 'this.DoSome = function (a, b, p) {',
  26110. '};',
  26111. 'this.Event = null;',
  26112. 'this.Func = null;',
  26113. '']),
  26114. LinesToStr([ // $mod.$main
  26115. '$mod.DoIt($mod.Event, $mod.Func);',
  26116. '$mod.DoSome($mod.Event, {',
  26117. ' p: $mod,',
  26118. ' get: function () {',
  26119. ' return this.p.Event;',
  26120. ' },',
  26121. ' set: function (v) {',
  26122. ' this.p.Event = v;',
  26123. ' }',
  26124. '}, $mod.Event);',
  26125. '$mod.DoSome($mod.Func, {',
  26126. ' p: $mod,',
  26127. ' get: function () {',
  26128. ' return this.p.Func;',
  26129. ' },',
  26130. ' set: function (v) {',
  26131. ' this.p.Func = v;',
  26132. ' }',
  26133. '}, $mod.Func);',
  26134. '']));
  26135. end;
  26136. procedure TTestModule.TestProcType_PassProcToArray;
  26137. begin
  26138. StartProgram(false);
  26139. Add([
  26140. 'type',
  26141. ' TFunc = function: longint;',
  26142. ' TArrFunc = array of TFunc;',
  26143. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26144. 'function GetIt: longint; begin end;',
  26145. 'var',
  26146. ' Func: tfunc;',
  26147. 'begin',
  26148. ' doit([]);',
  26149. ' doit([@GetIt]);',
  26150. ' doit([Func]);',
  26151. '']);
  26152. ConvertProgram;
  26153. CheckSource('TestProcType_PassProcToArray',
  26154. LinesToStr([ // statements
  26155. 'this.DoIt = function (Arr) {',
  26156. '};',
  26157. 'this.GetIt = function () {',
  26158. ' var Result = 0;',
  26159. ' return Result;',
  26160. '};',
  26161. 'this.Func = null;',
  26162. '']),
  26163. LinesToStr([ // $mod.$main
  26164. '$mod.DoIt([]);',
  26165. '$mod.DoIt([$mod.GetIt]);',
  26166. '$mod.DoIt([$mod.Func]);',
  26167. '']));
  26168. end;
  26169. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26170. begin
  26171. StartProgram(false);
  26172. Add([
  26173. '{$modeswitch externalclass}',
  26174. 'type',
  26175. ' TProc = reference to procedure(i: longint); safecall;',
  26176. ' TEvent = procedure(i: longint) of object; safecall;',
  26177. ' TExtA = class external name ''ExtObj''',
  26178. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26179. ' procedure DoSome(Id: longint = 1);',
  26180. ' procedure SetOnClick(const e: TEvent);',
  26181. ' property OnClick: TEvent write SetOnClick;',
  26182. ' class procedure Fly(Id: longint = 1); static;',
  26183. ' procedure SetOnShow(const p: TProc);',
  26184. ' property OnShow: TProc write SetOnShow;',
  26185. ' end;',
  26186. 'procedure Run(i: longint = 1);',
  26187. 'begin',
  26188. 'end;',
  26189. 'var',
  26190. ' Obj: texta;',
  26191. ' e: TEvent;',
  26192. ' p: TProc;',
  26193. 'begin',
  26194. ' e:=e;',
  26195. ' e:[email protected];',
  26196. ' e:[email protected];',
  26197. ' e:=TEvent(@obj.dosome);', // no safecall
  26198. ' obj.OnClick:[email protected];',
  26199. ' obj.OnClick:[email protected];',
  26200. ' obj.setonclick(@obj.doit);',
  26201. ' obj.setonclick(@obj.dosome);',
  26202. ' p:=@Run;',
  26203. ' p:[email protected];',
  26204. ' obj.OnShow:=@Run;',
  26205. ' obj.OnShow:[email protected];',
  26206. ' obj.setOnShow(@Run);',
  26207. ' obj.setOnShow(@TExtA.Fly);',
  26208. ' with obj do begin',
  26209. ' e:=@doit;',
  26210. ' e:=@dosome;',
  26211. ' OnClick:=@doit;',
  26212. ' OnClick:=@dosome;',
  26213. ' setonclick(@doit);',
  26214. ' setonclick(@dosome);',
  26215. ' OnShow:=@Run;',
  26216. ' setOnShow(@Run);',
  26217. ' end;']);
  26218. ConvertProgram;
  26219. CheckSource('TestProcType_SafeCallObjFPC',
  26220. LinesToStr([ // statements
  26221. 'this.Run = function (i) {',
  26222. '};',
  26223. 'this.Obj = null;',
  26224. 'this.e = null;',
  26225. 'this.p = null;',
  26226. '']),
  26227. LinesToStr([ // $mod.$main
  26228. '$mod.e = $mod.e;',
  26229. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26230. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26231. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26232. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26233. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26234. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26235. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26236. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26237. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26238. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26239. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26240. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26241. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26242. 'var $with = $mod.Obj;',
  26243. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26244. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26245. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26246. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26247. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26248. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26249. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26250. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26251. '']));
  26252. end;
  26253. procedure TTestModule.TestProcType_SafeCallDelphi;
  26254. begin
  26255. StartProgram(false);
  26256. Add([
  26257. '{$mode delphi}',
  26258. '{$modeswitch externalclass}',
  26259. 'type',
  26260. ' TProc = reference to procedure(i: longint); safecall;',
  26261. ' TEvent = procedure(i: longint) of object; safecall;',
  26262. ' TExtA = class external name ''ExtObj''',
  26263. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26264. ' procedure DoSome(Id: longint = 1);',
  26265. ' procedure SetOnClick(const e: TEvent);',
  26266. ' property OnClick: TEvent write SetOnClick;',
  26267. ' class procedure Fly(Id: longint = 1); static;',
  26268. ' procedure SetOnShow(const p: TProc);',
  26269. ' property OnShow: TProc write SetOnShow;',
  26270. ' end;',
  26271. 'procedure Run(i: longint = 1);',
  26272. 'begin',
  26273. 'end;',
  26274. 'var',
  26275. ' Obj: texta;',
  26276. ' e: TEvent;',
  26277. ' p: TProc;',
  26278. 'begin',
  26279. ' e:=e;',
  26280. ' e:=obj.doit;',
  26281. ' e:=obj.dosome;',
  26282. ' e:=TEvent(@obj.dosome);', // no safecall
  26283. ' obj.OnClick:=obj.doit;',
  26284. ' obj.OnClick:=obj.dosome;',
  26285. ' obj.setonclick(obj.doit);',
  26286. ' obj.setonclick(obj.dosome);',
  26287. ' p:=Run;',
  26288. ' p:=TExtA.Fly;',
  26289. ' obj.OnShow:=Run;',
  26290. ' obj.OnShow:=TExtA.Fly;',
  26291. ' obj.setOnShow(Run);',
  26292. ' obj.setOnShow(TExtA.Fly);',
  26293. ' with obj do begin',
  26294. ' e:=doit;',
  26295. ' e:=dosome;',
  26296. ' OnClick:=doit;',
  26297. ' OnClick:=dosome;',
  26298. ' setonclick(doit);',
  26299. ' setonclick(dosome);',
  26300. ' OnShow:=@Run;',
  26301. ' setOnShow(@Run);',
  26302. ' end;']);
  26303. ConvertProgram;
  26304. CheckSource('TestProcType_SafeCallDelphi',
  26305. LinesToStr([ // statements
  26306. 'this.Run = function (i) {',
  26307. '};',
  26308. 'this.Obj = null;',
  26309. 'this.e = null;',
  26310. 'this.p = null;',
  26311. '']),
  26312. LinesToStr([ // $mod.$main
  26313. '$mod.e = $mod.e;',
  26314. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26315. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26316. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26317. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26318. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26319. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26320. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26321. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26322. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26323. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26324. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26325. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26326. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26327. 'var $with = $mod.Obj;',
  26328. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26329. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26330. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26331. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26332. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26333. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26334. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26335. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26336. '']));
  26337. end;
  26338. procedure TTestModule.TestPointer;
  26339. begin
  26340. StartProgram(false);
  26341. Add(['type',
  26342. ' TObject = class end;',
  26343. ' TClass = class of TObject;',
  26344. ' TArrInt = array of longint;',
  26345. 'const',
  26346. ' n = nil;',
  26347. 'var',
  26348. ' v: jsvalue;',
  26349. ' Obj: tobject;',
  26350. ' C: tclass;',
  26351. ' a: tarrint;',
  26352. ' p: Pointer = nil;',
  26353. ' s: string;',
  26354. 'begin',
  26355. ' p:=p;',
  26356. ' p:=nil;',
  26357. ' if p=nil then;',
  26358. ' if nil=p then;',
  26359. ' if Assigned(p) then;',
  26360. ' p:=Pointer(v);',
  26361. ' p:=obj;',
  26362. ' p:=c;',
  26363. ' p:=a;',
  26364. ' p:=tobject;',
  26365. ' obj:=TObject(p);',
  26366. ' c:=TClass(p);',
  26367. ' a:=TArrInt(p);',
  26368. ' p:=n;',
  26369. ' p:=Pointer(a);',
  26370. ' p:=pointer(s);',
  26371. ' s:=string(p);',
  26372. '']);
  26373. ConvertProgram;
  26374. CheckSource('TestPointer',
  26375. LinesToStr([ // statements
  26376. 'rtl.createClass(this, "TObject", null, function () {',
  26377. ' this.$init = function () {',
  26378. ' };',
  26379. ' this.$final = function () {',
  26380. ' };',
  26381. '});',
  26382. 'this.n = null;',
  26383. 'this.v = undefined;',
  26384. 'this.Obj = null;',
  26385. 'this.C = null;',
  26386. 'this.a = [];',
  26387. 'this.p = null;',
  26388. 'this.s = "";',
  26389. '']),
  26390. LinesToStr([ // $mod.$main
  26391. '$mod.p = $mod.p;',
  26392. '$mod.p = null;',
  26393. 'if ($mod.p === null) ;',
  26394. 'if (null === $mod.p) ;',
  26395. 'if ($mod.p != null) ;',
  26396. '$mod.p = $mod.v;',
  26397. '$mod.p = $mod.Obj;',
  26398. '$mod.p = $mod.C;',
  26399. '$mod.p = $mod.a;',
  26400. '$mod.p = $mod.TObject;',
  26401. '$mod.Obj = $mod.p;',
  26402. '$mod.C = $mod.p;',
  26403. '$mod.a = $mod.p;',
  26404. '$mod.p = null;',
  26405. '$mod.p = $mod.a;',
  26406. '$mod.p = $mod.s;',
  26407. '$mod.s = $mod.p;',
  26408. '']));
  26409. end;
  26410. procedure TTestModule.TestPointer_Proc;
  26411. begin
  26412. StartProgram(false);
  26413. Add('type');
  26414. Add(' TObject = class');
  26415. Add(' procedure DoIt; virtual; abstract;');
  26416. Add(' end;');
  26417. Add('procedure DoSome; begin end;');
  26418. Add('var');
  26419. Add(' o: TObject;');
  26420. Add(' p: Pointer;');
  26421. Add('begin');
  26422. Add(' p:=@DoSome;');
  26423. Add(' p:[email protected];');
  26424. ConvertProgram;
  26425. CheckSource('TestPointer_Proc',
  26426. LinesToStr([ // statements
  26427. 'rtl.createClass(this, "TObject", null, function () {',
  26428. ' this.$init = function () {',
  26429. ' };',
  26430. ' this.$final = function () {',
  26431. ' };',
  26432. '});',
  26433. 'this.DoSome = function () {',
  26434. '};',
  26435. 'this.o = null;',
  26436. 'this.p = null;',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '$mod.p = $mod.DoSome;',
  26440. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26441. '']));
  26442. end;
  26443. procedure TTestModule.TestPointer_AssignRecordFail;
  26444. begin
  26445. StartProgram(false);
  26446. Add('type');
  26447. Add(' TRec = record end;');
  26448. Add('var');
  26449. Add(' p: Pointer;');
  26450. Add(' r: TRec;');
  26451. Add('begin');
  26452. Add(' p:=r;');
  26453. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26454. nIncompatibleTypesGotExpected);
  26455. ConvertProgram;
  26456. end;
  26457. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26458. begin
  26459. StartProgram(false);
  26460. Add('type');
  26461. Add(' TArr = array[boolean] of longint;');
  26462. Add('var');
  26463. Add(' p: Pointer;');
  26464. Add(' a: TArr;');
  26465. Add('begin');
  26466. Add(' p:=a;');
  26467. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26468. nIncompatibleTypesGotExpected);
  26469. ConvertProgram;
  26470. end;
  26471. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26472. begin
  26473. StartProgram(false);
  26474. Add([
  26475. 'procedure DoIt(args: array of jsvalue); begin end;',
  26476. 'procedure DoAll; varargs; begin end;',
  26477. 'var',
  26478. ' v: jsvalue;',
  26479. 'begin',
  26480. ' DoIt([pointer(v)]);',
  26481. ' DoAll(pointer(v));',
  26482. '']);
  26483. ConvertProgram;
  26484. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26485. LinesToStr([ // statements
  26486. 'this.DoIt = function (args) {',
  26487. '};',
  26488. 'this.DoAll = function () {',
  26489. '};',
  26490. 'this.v = undefined;',
  26491. '']),
  26492. LinesToStr([ // $mod.$main
  26493. '$mod.DoIt([$mod.v]);',
  26494. '$mod.DoAll($mod.v);',
  26495. '']));
  26496. end;
  26497. procedure TTestModule.TestPointer_NonRecordFail;
  26498. begin
  26499. StartProgram(false);
  26500. Add([
  26501. 'type',
  26502. ' p = ^longint;',
  26503. 'begin',
  26504. '']);
  26505. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26506. ConvertProgram;
  26507. end;
  26508. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26509. begin
  26510. StartProgram(false);
  26511. Add([
  26512. 'procedure DoIt(p: ^longint); begin end;',
  26513. 'begin',
  26514. '']);
  26515. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26516. ConvertProgram;
  26517. end;
  26518. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26519. begin
  26520. StartProgram(false);
  26521. Add([
  26522. 'var p: ^longint;',
  26523. 'begin',
  26524. '']);
  26525. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26526. ConvertProgram;
  26527. end;
  26528. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26529. begin
  26530. StartProgram(false);
  26531. Add([
  26532. 'function DoIt: ^longint; begin end;',
  26533. 'begin',
  26534. '']);
  26535. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26536. ConvertProgram;
  26537. end;
  26538. procedure TTestModule.TestPointer_AddrOperatorFail;
  26539. begin
  26540. StartProgram(false);
  26541. Add([
  26542. 'var i: longint;',
  26543. 'begin',
  26544. ' if @i=nil then ;',
  26545. '']);
  26546. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26547. ConvertProgram;
  26548. end;
  26549. procedure TTestModule.TestPointer_ArrayParamsFail;
  26550. begin
  26551. StartProgram(false);
  26552. Add([
  26553. 'var',
  26554. ' p: Pointer;',
  26555. 'begin',
  26556. ' p:=p[1];',
  26557. '']);
  26558. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26559. ConvertProgram;
  26560. end;
  26561. procedure TTestModule.TestPointer_PointerAddFail;
  26562. begin
  26563. StartProgram(false);
  26564. Add([
  26565. 'var',
  26566. ' p: Pointer;',
  26567. 'begin',
  26568. ' p:=p+1;',
  26569. '']);
  26570. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26571. ConvertProgram;
  26572. end;
  26573. procedure TTestModule.TestPointer_IncPointerFail;
  26574. begin
  26575. StartProgram(false);
  26576. Add([
  26577. 'var',
  26578. ' p: Pointer;',
  26579. 'begin',
  26580. ' inc(p,1);',
  26581. '']);
  26582. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26583. nIncompatibleTypeArgNo);
  26584. ConvertProgram;
  26585. end;
  26586. procedure TTestModule.TestPointer_Record;
  26587. begin
  26588. StartProgram(false);
  26589. Add([
  26590. 'type',
  26591. ' TRec = record x: longint; end;',
  26592. ' PRec = ^TRec;',
  26593. 'var',
  26594. ' r: TRec;',
  26595. ' p: PRec;',
  26596. ' q: ^TRec;',
  26597. ' Ptr: pointer;',
  26598. 'begin',
  26599. ' new(p);',
  26600. ' p:=@r;',
  26601. ' r:=p^;',
  26602. ' r.x:=p^.x;',
  26603. ' p^.x:=r.x;',
  26604. ' if p^.x=3 then ;',
  26605. ' if 4=p^.x then ;',
  26606. ' dispose(p);',
  26607. ' new(q);',
  26608. ' dispose(q);',
  26609. ' Ptr:=p;',
  26610. ' p:=PRec(ptr);',
  26611. '']);
  26612. ConvertProgram;
  26613. CheckSource('TestPointer_Record',
  26614. LinesToStr([ // statements
  26615. 'rtl.recNewT(this, "TRec", function () {',
  26616. ' this.x = 0;',
  26617. ' this.$eq = function (b) {',
  26618. ' return this.x === b.x;',
  26619. ' };',
  26620. ' this.$assign = function (s) {',
  26621. ' this.x = s.x;',
  26622. ' return this;',
  26623. ' };',
  26624. '});',
  26625. 'this.r = this.TRec.$new();',
  26626. 'this.p = null;',
  26627. 'this.q = null;',
  26628. 'this.Ptr = null;',
  26629. '']),
  26630. LinesToStr([ // $mod.$main
  26631. '$mod.p = $mod.TRec.$new();',
  26632. '$mod.p = $mod.r;',
  26633. '$mod.r.$assign($mod.p);',
  26634. '$mod.r.x = $mod.p.x;',
  26635. '$mod.p.x = $mod.r.x;',
  26636. 'if ($mod.p.x === 3) ;',
  26637. 'if (4 === $mod.p.x) ;',
  26638. '$mod.p = null;',
  26639. '$mod.q = $mod.TRec.$new();',
  26640. '$mod.q = null;',
  26641. '$mod.Ptr = $mod.p;',
  26642. '$mod.p = $mod.Ptr;',
  26643. '']));
  26644. end;
  26645. procedure TTestModule.TestPointer_RecordArg;
  26646. begin
  26647. StartProgram(false);
  26648. Add([
  26649. '{$modeswitch autoderef}',
  26650. 'type',
  26651. ' TRec = record x: longint; end;',
  26652. ' PRec = ^TRec;',
  26653. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26654. 'begin',
  26655. ' a.x:=a.x;',
  26656. ' a^.x:=a^.x;',
  26657. ' with a^ do',
  26658. ' x:=x;',
  26659. 'end;',
  26660. 'function GetIt(p: PRec): PRec;',
  26661. 'begin',
  26662. ' p.x:=p.x;',
  26663. ' p^.x:=p^.x;',
  26664. ' with p^ do',
  26665. ' x:=x;',
  26666. 'end;',
  26667. 'var',
  26668. ' r: TRec;',
  26669. ' p: PRec;',
  26670. 'begin',
  26671. ' p:=GetIt(p);',
  26672. ' p^:=GetIt(@r)^;',
  26673. ' DoIt(p,p,p);',
  26674. ' DoIt(@r,p,p);',
  26675. '']);
  26676. ConvertProgram;
  26677. CheckSource('TestPointer_RecordArg',
  26678. LinesToStr([ // statements
  26679. 'rtl.recNewT(this, "TRec", function () {',
  26680. ' this.x = 0;',
  26681. ' this.$eq = function (b) {',
  26682. ' return this.x === b.x;',
  26683. ' };',
  26684. ' this.$assign = function (s) {',
  26685. ' this.x = s.x;',
  26686. ' return this;',
  26687. ' };',
  26688. '});',
  26689. 'this.DoIt = function (a, b, c) {',
  26690. ' var Result = $mod.TRec.$new();',
  26691. ' a.x = a.x;',
  26692. ' a.x = a.x;',
  26693. ' a.x = a.x;',
  26694. ' return Result;',
  26695. '};',
  26696. 'this.GetIt = function (p) {',
  26697. ' var Result = null;',
  26698. ' p.x = p.x;',
  26699. ' p.x = p.x;',
  26700. ' p.x = p.x;',
  26701. ' return Result;',
  26702. '};',
  26703. 'this.r = this.TRec.$new();',
  26704. 'this.p = null;',
  26705. '']),
  26706. LinesToStr([ // $mod.$main
  26707. '$mod.p = $mod.GetIt($mod.p);',
  26708. '$mod.p.$assign($mod.GetIt($mod.r));',
  26709. '$mod.DoIt($mod.p, {',
  26710. ' p: $mod,',
  26711. ' get: function () {',
  26712. ' return this.p.p;',
  26713. ' },',
  26714. ' set: function (v) {',
  26715. ' this.p.p = v;',
  26716. ' }',
  26717. '}, {',
  26718. ' p: $mod,',
  26719. ' get: function () {',
  26720. ' return this.p.p;',
  26721. ' },',
  26722. ' set: function (v) {',
  26723. ' this.p.p = v;',
  26724. ' }',
  26725. '});',
  26726. '$mod.DoIt($mod.r, {',
  26727. ' p: $mod,',
  26728. ' get: function () {',
  26729. ' return this.p.p;',
  26730. ' },',
  26731. ' set: function (v) {',
  26732. ' this.p.p = v;',
  26733. ' }',
  26734. '}, {',
  26735. ' p: $mod,',
  26736. ' get: function () {',
  26737. ' return this.p.p;',
  26738. ' },',
  26739. ' set: function (v) {',
  26740. ' this.p.p = v;',
  26741. ' }',
  26742. '});',
  26743. '']));
  26744. end;
  26745. procedure TTestModule.TestJSValue_AssignToJSValue;
  26746. begin
  26747. StartProgram(false);
  26748. Add('var');
  26749. Add(' v: jsvalue;');
  26750. Add(' i: longint;');
  26751. Add(' s: string;');
  26752. Add(' b: boolean;');
  26753. Add(' d: double;');
  26754. Add(' p: pointer;');
  26755. Add('begin');
  26756. Add(' v:=v;');
  26757. Add(' v:=1;');
  26758. Add(' v:=i;');
  26759. Add(' v:='''';');
  26760. Add(' v:=''c'';');
  26761. Add(' v:=''foo'';');
  26762. Add(' v:=s;');
  26763. Add(' v:=false;');
  26764. Add(' v:=true;');
  26765. Add(' v:=b;');
  26766. Add(' v:=0.1;');
  26767. Add(' v:=d;');
  26768. Add(' v:=nil;');
  26769. Add(' v:=p;');
  26770. ConvertProgram;
  26771. CheckSource('TestJSValue_AssignToJSValue',
  26772. LinesToStr([ // statements
  26773. 'this.v = undefined;',
  26774. 'this.i = 0;',
  26775. 'this.s = "";',
  26776. 'this.b = false;',
  26777. 'this.d = 0.0;',
  26778. 'this.p = null;',
  26779. '']),
  26780. LinesToStr([ // $mod.$main
  26781. '$mod.v = $mod.v;',
  26782. '$mod.v = 1;',
  26783. '$mod.v = $mod.i;',
  26784. '$mod.v = "";',
  26785. '$mod.v = "c";',
  26786. '$mod.v = "foo";',
  26787. '$mod.v = $mod.s;',
  26788. '$mod.v = false;',
  26789. '$mod.v = true;',
  26790. '$mod.v = $mod.b;',
  26791. '$mod.v = 0.1;',
  26792. '$mod.v = $mod.d;',
  26793. '$mod.v = null;',
  26794. '$mod.v = $mod.p;',
  26795. '']));
  26796. end;
  26797. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26798. begin
  26799. StartProgram(false);
  26800. Add('type');
  26801. Add(' integer = longint;');
  26802. Add(' TYesNo = boolean;');
  26803. Add(' TFloat = double;');
  26804. Add(' TCaption = string;');
  26805. Add(' TChar = char;');
  26806. Add('var');
  26807. Add(' v: jsvalue;');
  26808. Add(' i: integer;');
  26809. Add(' s: TCaption;');
  26810. Add(' b: TYesNo;');
  26811. Add(' d: TFloat;');
  26812. Add(' c: char;');
  26813. Add('begin');
  26814. Add(' i:=longint(v);');
  26815. Add(' i:=integer(v);');
  26816. Add(' s:=string(v);');
  26817. Add(' s:=TCaption(v);');
  26818. Add(' b:=boolean(v);');
  26819. Add(' b:=TYesNo(v);');
  26820. Add(' d:=double(v);');
  26821. Add(' d:=TFloat(v);');
  26822. Add(' c:=char(v);');
  26823. Add(' c:=TChar(v);');
  26824. ConvertProgram;
  26825. CheckSource('TestJSValue_TypeCastToBaseType',
  26826. LinesToStr([ // statements
  26827. 'this.v = undefined;',
  26828. 'this.i = 0;',
  26829. 'this.s = "";',
  26830. 'this.b = false;',
  26831. 'this.d = 0.0;',
  26832. 'this.c = "";',
  26833. '']),
  26834. LinesToStr([ // $mod.$main
  26835. '$mod.i = Math.floor($mod.v);',
  26836. '$mod.i = Math.floor($mod.v);',
  26837. '$mod.s = "" + $mod.v;',
  26838. '$mod.s = "" + $mod.v;',
  26839. '$mod.b = !($mod.v == false);',
  26840. '$mod.b = !($mod.v == false);',
  26841. '$mod.d = rtl.getNumber($mod.v);',
  26842. '$mod.d = rtl.getNumber($mod.v);',
  26843. '$mod.c = rtl.getChar($mod.v);',
  26844. '$mod.c = rtl.getChar($mod.v);',
  26845. '']));
  26846. end;
  26847. procedure TTestModule.TestJSValue_TypecastToJSValue;
  26848. begin
  26849. StartProgram(false);
  26850. Add([
  26851. 'type',
  26852. ' TArr = array of word;',
  26853. ' TRec = record end;',
  26854. ' TSet = set of boolean;',
  26855. 'procedure Fly(v: jsvalue);',
  26856. 'begin',
  26857. 'end;',
  26858. 'var',
  26859. ' a: TArr;',
  26860. ' r: TRec;',
  26861. ' s: TSet;',
  26862. 'begin',
  26863. ' Fly(jsvalue(a));',
  26864. ' Fly(jsvalue(r));',
  26865. ' Fly(jsvalue(s));',
  26866. '']);
  26867. ConvertProgram;
  26868. CheckSource('TestJSValue_TypecastToJSValue',
  26869. LinesToStr([ // statements
  26870. 'rtl.recNewT(this, "TRec", function () {',
  26871. ' this.$eq = function (b) {',
  26872. ' return true;',
  26873. ' };',
  26874. ' this.$assign = function (s) {',
  26875. ' return this;',
  26876. ' };',
  26877. '});',
  26878. 'this.Fly = function (v) {',
  26879. '};',
  26880. 'this.a = [];',
  26881. 'this.r = this.TRec.$new();',
  26882. 'this.s = {};',
  26883. '']),
  26884. LinesToStr([ // $mod.$main
  26885. '$mod.Fly($mod.a);',
  26886. '$mod.Fly($mod.r);',
  26887. '$mod.Fly($mod.s);',
  26888. '']));
  26889. end;
  26890. procedure TTestModule.TestJSValue_Equal;
  26891. begin
  26892. StartProgram(false);
  26893. Add('type');
  26894. Add(' integer = longint;');
  26895. Add(' TYesNo = boolean;');
  26896. Add(' TFloat = double;');
  26897. Add(' TCaption = string;');
  26898. Add(' TChar = char;');
  26899. Add(' TMulti = JSValue;');
  26900. Add('var');
  26901. Add(' v: jsvalue;');
  26902. Add(' i: integer;');
  26903. Add(' s: TCaption;');
  26904. Add(' b: TYesNo;');
  26905. Add(' d: TFloat;');
  26906. Add(' c: char;');
  26907. Add(' m: TMulti;');
  26908. Add('begin');
  26909. Add(' b:=v=v;');
  26910. Add(' b:=v<>v;');
  26911. Add(' b:=v=1;');
  26912. Add(' b:=v<>1;');
  26913. Add(' b:=2=v;');
  26914. Add(' b:=2<>v;');
  26915. Add(' b:=v=i;');
  26916. Add(' b:=i=v;');
  26917. Add(' b:=v=nil;');
  26918. Add(' b:=nil=v;');
  26919. Add(' b:=v=false;');
  26920. Add(' b:=true=v;');
  26921. Add(' b:=v=b;');
  26922. Add(' b:=b=v;');
  26923. Add(' b:=v=s;');
  26924. Add(' b:=s=v;');
  26925. Add(' b:=v=''foo'';');
  26926. Add(' b:=''''=v;');
  26927. Add(' b:=v=d;');
  26928. Add(' b:=d=v;');
  26929. Add(' b:=v=3.4;');
  26930. Add(' b:=5.6=v;');
  26931. Add(' b:=v=c;');
  26932. Add(' b:=c=v;');
  26933. Add(' b:=m=m;');
  26934. Add(' b:=v=m;');
  26935. Add(' b:=m=v;');
  26936. ConvertProgram;
  26937. CheckSource('TestJSValue_Equal',
  26938. LinesToStr([ // statements
  26939. 'this.v = undefined;',
  26940. 'this.i = 0;',
  26941. 'this.s = "";',
  26942. 'this.b = false;',
  26943. 'this.d = 0.0;',
  26944. 'this.c = "";',
  26945. 'this.m = undefined;',
  26946. '']),
  26947. LinesToStr([ // $mod.$main
  26948. '$mod.b = $mod.v == $mod.v;',
  26949. '$mod.b = $mod.v != $mod.v;',
  26950. '$mod.b = $mod.v == 1;',
  26951. '$mod.b = $mod.v != 1;',
  26952. '$mod.b = 2 == $mod.v;',
  26953. '$mod.b = 2 != $mod.v;',
  26954. '$mod.b = $mod.v == $mod.i;',
  26955. '$mod.b = $mod.i == $mod.v;',
  26956. '$mod.b = $mod.v == null;',
  26957. '$mod.b = null == $mod.v;',
  26958. '$mod.b = $mod.v == false;',
  26959. '$mod.b = true == $mod.v;',
  26960. '$mod.b = $mod.v == $mod.b;',
  26961. '$mod.b = $mod.b == $mod.v;',
  26962. '$mod.b = $mod.v == $mod.s;',
  26963. '$mod.b = $mod.s == $mod.v;',
  26964. '$mod.b = $mod.v == "foo";',
  26965. '$mod.b = "" == $mod.v;',
  26966. '$mod.b = $mod.v == $mod.d;',
  26967. '$mod.b = $mod.d == $mod.v;',
  26968. '$mod.b = $mod.v == 3.4;',
  26969. '$mod.b = 5.6 == $mod.v;',
  26970. '$mod.b = $mod.v == $mod.c;',
  26971. '$mod.b = $mod.c == $mod.v;',
  26972. '$mod.b = $mod.m == $mod.m;',
  26973. '$mod.b = $mod.v == $mod.m;',
  26974. '$mod.b = $mod.m == $mod.v;',
  26975. '']));
  26976. end;
  26977. procedure TTestModule.TestJSValue_If;
  26978. begin
  26979. StartProgram(false);
  26980. Add([
  26981. 'procedure Fly(var u);',
  26982. 'begin',
  26983. ' if jsvalue(u) then ;',
  26984. 'end;',
  26985. 'var',
  26986. ' v: jsvalue;',
  26987. 'begin',
  26988. ' if v then ;',
  26989. ' while v do ;',
  26990. ' repeat until v;',
  26991. '']);
  26992. ConvertProgram;
  26993. CheckSource('TestJSValue_If',
  26994. LinesToStr([ // statements
  26995. 'this.Fly = function (u) {',
  26996. ' if (u.get()) ;',
  26997. '};',
  26998. 'this.v = undefined;',
  26999. '']),
  27000. LinesToStr([ // $mod.$main
  27001. 'if ($mod.v) ;',
  27002. 'while($mod.v){',
  27003. '};',
  27004. 'do{',
  27005. '} while(!$mod.v);',
  27006. '']));
  27007. end;
  27008. procedure TTestModule.TestJSValue_Not;
  27009. begin
  27010. StartProgram(false);
  27011. Add([
  27012. 'var',
  27013. ' v: jsvalue;',
  27014. ' b: boolean;',
  27015. 'begin',
  27016. ' b:=not v;',
  27017. ' if not v then ;',
  27018. ' while not v do ;',
  27019. ' repeat until not v;',
  27020. '']);
  27021. ConvertProgram;
  27022. CheckSource('TestJSValue_If',
  27023. LinesToStr([ // statements
  27024. 'this.v = undefined;',
  27025. 'this.b = false;',
  27026. '']),
  27027. LinesToStr([ // $mod.$main
  27028. '$mod.b=!$mod.v;',
  27029. 'if (!$mod.v) ;',
  27030. 'while(!$mod.v){',
  27031. '};',
  27032. 'do{',
  27033. '} while($mod.v);',
  27034. '']));
  27035. end;
  27036. procedure TTestModule.TestJSValue_Enum;
  27037. begin
  27038. StartProgram(false);
  27039. Add('type');
  27040. Add(' TColor = (red, blue);');
  27041. Add(' TRedBlue = TColor;');
  27042. Add('var');
  27043. Add(' v: jsvalue;');
  27044. Add(' e: TColor;');
  27045. Add('begin');
  27046. Add(' v:=e;');
  27047. Add(' v:=TColor(e);');
  27048. Add(' v:=TRedBlue(e);');
  27049. Add(' e:=TColor(v);');
  27050. Add(' e:=TRedBlue(v);');
  27051. ConvertProgram;
  27052. CheckSource('TestJSValue_Enum',
  27053. LinesToStr([ // statements
  27054. 'this.TColor = {',
  27055. ' "0": "red",',
  27056. ' red: 0,',
  27057. ' "1": "blue",',
  27058. ' blue: 1',
  27059. '};',
  27060. 'this.v = undefined;',
  27061. 'this.e = 0;',
  27062. '']),
  27063. LinesToStr([ // $mod.$main
  27064. '$mod.v = $mod.e;',
  27065. '$mod.v = $mod.e;',
  27066. '$mod.v = $mod.e;',
  27067. '$mod.e = $mod.v;',
  27068. '$mod.e = $mod.v;',
  27069. '']));
  27070. end;
  27071. procedure TTestModule.TestJSValue_ClassInstance;
  27072. begin
  27073. StartProgram(false);
  27074. Add([
  27075. 'type',
  27076. ' TObject = class',
  27077. ' end;',
  27078. ' TBirdObject = TObject;',
  27079. 'var',
  27080. ' v: jsvalue;',
  27081. ' o: TObject;',
  27082. 'begin',
  27083. ' v:=o;',
  27084. ' v:=TObject(o);',
  27085. ' v:=TBirdObject(o);',
  27086. ' o:=TObject(v);',
  27087. ' o:=TBirdObject(v);',
  27088. ' if v is TObject then ;',
  27089. '']);
  27090. ConvertProgram;
  27091. CheckSource('TestJSValue_ClassInstance',
  27092. LinesToStr([ // statements
  27093. 'rtl.createClass(this, "TObject", null, function () {',
  27094. ' this.$init = function () {',
  27095. ' };',
  27096. ' this.$final = function () {',
  27097. ' };',
  27098. '});',
  27099. 'this.v = undefined;',
  27100. 'this.o = null;',
  27101. '']),
  27102. LinesToStr([ // $mod.$main
  27103. '$mod.v = $mod.o;',
  27104. '$mod.v = $mod.o;',
  27105. '$mod.v = $mod.o;',
  27106. '$mod.o = rtl.getObject($mod.v);',
  27107. '$mod.o = rtl.getObject($mod.v);',
  27108. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27109. '']));
  27110. end;
  27111. procedure TTestModule.TestJSValue_ClassOf;
  27112. begin
  27113. StartProgram(false);
  27114. Add([
  27115. 'type',
  27116. ' TClass = class of TObject;',
  27117. ' TObject = class',
  27118. ' end;',
  27119. ' TBirds = class of TBird;',
  27120. ' TBird = class(TObject) end;',
  27121. 'var',
  27122. ' v: jsvalue;',
  27123. ' c: TClass;',
  27124. 'begin',
  27125. ' v:=c;',
  27126. ' v:=TObject;',
  27127. ' v:=TClass(c);',
  27128. ' v:=TBirds(c);',
  27129. ' c:=TClass(v);',
  27130. ' c:=TBirds(v);',
  27131. ' if v is TClass then ;',
  27132. '']);
  27133. ConvertProgram;
  27134. CheckSource('TestJSValue_ClassOf',
  27135. LinesToStr([ // statements
  27136. 'rtl.createClass(this, "TObject", null, function () {',
  27137. ' this.$init = function () {',
  27138. ' };',
  27139. ' this.$final = function () {',
  27140. ' };',
  27141. '});',
  27142. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27143. '});',
  27144. 'this.v = undefined;',
  27145. 'this.c = null;',
  27146. '']),
  27147. LinesToStr([ // $mod.$main
  27148. '$mod.v = $mod.c;',
  27149. '$mod.v = $mod.TObject;',
  27150. '$mod.v = $mod.c;',
  27151. '$mod.v = $mod.c;',
  27152. '$mod.c = rtl.getObject($mod.v);',
  27153. '$mod.c = rtl.getObject($mod.v);',
  27154. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27155. '']));
  27156. end;
  27157. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27158. begin
  27159. StartProgram(false);
  27160. Add([
  27161. 'type',
  27162. ' integer = longint;',
  27163. ' TArray = array of JSValue;',
  27164. ' TArrgh = tarray;',
  27165. ' TArrInt = array of integer;',
  27166. 'var',
  27167. ' v: jsvalue;',
  27168. ' TheArray: tarray = (1,''2'');',
  27169. ' Arr: tarrgh;',
  27170. ' i: integer;',
  27171. ' ArrInt: tarrint;',
  27172. 'begin',
  27173. ' arr:=thearray;',
  27174. ' thearray:=arr;',
  27175. ' setlength(arr,2);',
  27176. ' setlength(thearray,3);',
  27177. ' arr[4]:=v;',
  27178. ' arr[5]:=length(thearray);',
  27179. ' arr[6]:=nil;',
  27180. ' arr[7]:=thearray[8];',
  27181. ' arr[low(arr)]:=high(thearray);',
  27182. ' arr:=arrint;',
  27183. ' arrInt:=tarrint(arr);',
  27184. ' if TheArray = nil then ;',
  27185. ' if nil = TheArray then ;',
  27186. ' if TheArray <> nil then ;',
  27187. ' if nil <> TheArray then ;',
  27188. '']);
  27189. ConvertProgram;
  27190. CheckSource('TestJSValue_ArrayOfJSValue',
  27191. LinesToStr([ // statements
  27192. 'this.v = undefined;',
  27193. 'this.TheArray = [1, "2"];',
  27194. 'this.Arr = [];',
  27195. 'this.i = 0;',
  27196. 'this.ArrInt = [];',
  27197. '']),
  27198. LinesToStr([ // $mod.$main
  27199. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27200. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27201. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27202. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27203. '$mod.Arr[4] = $mod.v;',
  27204. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27205. '$mod.Arr[6] = null;',
  27206. '$mod.Arr[7] = $mod.TheArray[8];',
  27207. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27208. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27209. '$mod.ArrInt = $mod.Arr;',
  27210. 'if (rtl.length($mod.TheArray) === 0) ;',
  27211. 'if (rtl.length($mod.TheArray) === 0) ;',
  27212. 'if (rtl.length($mod.TheArray) > 0) ;',
  27213. 'if (rtl.length($mod.TheArray) > 0) ;',
  27214. '']));
  27215. end;
  27216. procedure TTestModule.TestJSValue_ArrayLit;
  27217. begin
  27218. StartProgram(false);
  27219. Add([
  27220. 'type',
  27221. ' TFlag = (big,small);',
  27222. ' TArray = array of JSValue;',
  27223. ' TObject = class end;',
  27224. ' TClass = class of TObject;',
  27225. 'var',
  27226. ' v: jsvalue;',
  27227. ' a: TArray;',
  27228. ' o: TObject;',
  27229. 'begin',
  27230. ' a:=[];',
  27231. ' a:=[1];',
  27232. ' a:=[1,2];',
  27233. ' a:=[big];',
  27234. ' a:=[1,big];',
  27235. ' a:=[o,nil];',
  27236. '']);
  27237. ConvertProgram;
  27238. CheckSource('TestJSValue_ArrayLit',
  27239. LinesToStr([ // statements
  27240. 'this.TFlag = {',
  27241. ' "0": "big",',
  27242. ' big: 0,',
  27243. ' "1": "small",',
  27244. ' small: 1',
  27245. '};',
  27246. 'rtl.createClass(this, "TObject", null, function () {',
  27247. ' this.$init = function () {',
  27248. ' };',
  27249. ' this.$final = function () {',
  27250. ' };',
  27251. '});',
  27252. 'this.v = undefined;',
  27253. 'this.a = [];',
  27254. 'this.o = null;',
  27255. '']),
  27256. LinesToStr([ // $mod.$main
  27257. '$mod.a = [];',
  27258. '$mod.a = [1];',
  27259. '$mod.a = [1, 2];',
  27260. '$mod.a = [$mod.TFlag.big];',
  27261. '$mod.a = [1, $mod.TFlag.big];',
  27262. '$mod.a = [$mod.o, null];',
  27263. '']));
  27264. end;
  27265. procedure TTestModule.TestJSValue_Params;
  27266. begin
  27267. StartProgram(false);
  27268. Add('type');
  27269. Add(' integer = longint;');
  27270. Add(' TYesNo = boolean;');
  27271. Add(' TFloat = double;');
  27272. Add(' TCaption = string;');
  27273. Add(' TChar = char;');
  27274. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27275. Add('var');
  27276. Add(' l: jsvalue;');
  27277. Add('begin');
  27278. Add(' a:=a;');
  27279. Add(' l:=b;');
  27280. Add(' c:=c;');
  27281. Add(' d:=d;');
  27282. Add(' Result:=l;');
  27283. Add('end;');
  27284. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27285. Add('var');
  27286. Add(' v: jsvalue;');
  27287. Add(' i: integer;');
  27288. Add(' b: TYesNo;');
  27289. Add(' d: TFloat;');
  27290. Add(' s: TCaption;');
  27291. Add(' c: TChar;');
  27292. Add('begin');
  27293. Add(' v:=doit(v,v,v,v);');
  27294. Add(' i:=integer(dosome(i,i));');
  27295. Add(' b:=TYesNo(dosome(b,b));');
  27296. Add(' d:=TFloat(dosome(d,d));');
  27297. Add(' s:=TCaption(dosome(s,s));');
  27298. Add(' c:=TChar(dosome(c,c));');
  27299. ConvertProgram;
  27300. CheckSource('TestJSValue_Params',
  27301. LinesToStr([ // statements
  27302. 'this.DoIt = function (a, b, c, d) {',
  27303. ' var Result = undefined;',
  27304. ' var l = undefined;',
  27305. ' a = a;',
  27306. ' l = b;',
  27307. ' c.set(c.get());',
  27308. ' d.set(d.get());',
  27309. ' Result = l;',
  27310. ' return Result;',
  27311. '};',
  27312. 'this.DoSome = function (a, b) {',
  27313. ' var Result = undefined;',
  27314. ' return Result;',
  27315. '};',
  27316. 'this.v = undefined;',
  27317. 'this.i = 0;',
  27318. 'this.b = false;',
  27319. 'this.d = 0.0;',
  27320. 'this.s = "";',
  27321. 'this.c = "";',
  27322. '']),
  27323. LinesToStr([ // $mod.$main
  27324. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27325. ' p: $mod,',
  27326. ' get: function () {',
  27327. ' return this.p.v;',
  27328. ' },',
  27329. ' set: function (v) {',
  27330. ' this.p.v = v;',
  27331. ' }',
  27332. '}, {',
  27333. ' p: $mod,',
  27334. ' get: function () {',
  27335. ' return this.p.v;',
  27336. ' },',
  27337. ' set: function (v) {',
  27338. ' this.p.v = v;',
  27339. ' }',
  27340. '});',
  27341. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  27342. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27343. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27344. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27345. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27346. '']));
  27347. end;
  27348. procedure TTestModule.TestJSValue_UntypedParam;
  27349. begin
  27350. StartProgram(false);
  27351. Add('function DoIt(const a; var b; out c): jsvalue;');
  27352. Add('begin');
  27353. Add(' Result:=a;');
  27354. Add(' Result:=b;');
  27355. Add(' Result:=c;');
  27356. Add(' b:=Result;');
  27357. Add(' c:=Result;');
  27358. Add('end;');
  27359. Add('var i: longint;');
  27360. Add('begin');
  27361. Add(' doit(i,i,i);');
  27362. ConvertProgram;
  27363. CheckSource('TestJSValue_UntypedParam',
  27364. LinesToStr([ // statements
  27365. 'this.DoIt = function (a, b, c) {',
  27366. ' var Result = undefined;',
  27367. ' Result = a;',
  27368. ' Result = b.get();',
  27369. ' Result = c.get();',
  27370. ' b.set(Result);',
  27371. ' c.set(Result);',
  27372. ' return Result;',
  27373. '};',
  27374. 'this.i = 0;',
  27375. '']),
  27376. LinesToStr([ // $mod.$main
  27377. '$mod.DoIt($mod.i, {',
  27378. ' p: $mod,',
  27379. ' get: function () {',
  27380. ' return this.p.i;',
  27381. ' },',
  27382. ' set: function (v) {',
  27383. ' this.p.i = v;',
  27384. ' }',
  27385. '}, {',
  27386. ' p: $mod,',
  27387. ' get: function () {',
  27388. ' return this.p.i;',
  27389. ' },',
  27390. ' set: function (v) {',
  27391. ' this.p.i = v;',
  27392. ' }',
  27393. '});',
  27394. '']));
  27395. end;
  27396. procedure TTestModule.TestJSValue_FuncResultType;
  27397. begin
  27398. StartProgram(false);
  27399. Add('type');
  27400. Add(' integer = longint;');
  27401. Add(' TJSValueArray = array of JSValue;');
  27402. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27403. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27404. Add('begin');
  27405. Add(' while Compare(P,aList[0])>0 do ;');
  27406. Add('end;');
  27407. Add('var');
  27408. Add(' Compare: TListSortCompare;');
  27409. Add(' V: JSValue;');
  27410. Add(' i: integer;');
  27411. Add('begin');
  27412. Add(' if Compare(V,V)>0 then ;');
  27413. Add(' if Compare(i,i)>1 then ;');
  27414. Add(' if Compare(nil,false)>2 then ;');
  27415. Add(' if Compare(1,true)>3 then ;');
  27416. ConvertProgram;
  27417. CheckSource('TestJSValue_UntypedParam',
  27418. LinesToStr([ // statements
  27419. 'this.Sort = function (P, aList, Compare) {',
  27420. ' while (Compare(P, aList[0]) > 0) {',
  27421. ' };',
  27422. '};',
  27423. 'this.Compare = null;',
  27424. 'this.V = undefined;',
  27425. 'this.i = 0;',
  27426. '']),
  27427. LinesToStr([ // $mod.$main
  27428. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27429. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27430. 'if ($mod.Compare(null, false) > 2) ;',
  27431. 'if ($mod.Compare(1, true) > 3) ;',
  27432. '']));
  27433. end;
  27434. procedure TTestModule.TestJSValue_ProcType_Assign;
  27435. begin
  27436. StartProgram(false);
  27437. Add('type');
  27438. Add(' integer = longint;');
  27439. Add(' TObject = class');
  27440. Add(' class function GetGlob: integer;');
  27441. Add(' function Getter: integer;');
  27442. Add(' end;');
  27443. Add('class function TObject.GetGlob: integer;');
  27444. Add('var v1: jsvalue;');
  27445. Add('begin');
  27446. Add(' v1:=@GetGlob;');
  27447. Add(' v1:[email protected];');
  27448. Add('end;');
  27449. Add('function TObject.Getter: integer;');
  27450. Add('var v2: jsvalue;');
  27451. Add('begin');
  27452. Add(' v2:=@Getter;');
  27453. Add(' v2:[email protected];');
  27454. Add(' v2:=@GetGlob;');
  27455. Add(' v2:[email protected];');
  27456. Add('end;');
  27457. Add('function GetIt(i: integer): integer;');
  27458. Add('var v3: jsvalue;');
  27459. Add('begin');
  27460. Add(' v3:=@GetIt;');
  27461. Add('end;');
  27462. Add('var');
  27463. Add(' V: JSValue;');
  27464. Add(' o: TObject;');
  27465. Add('begin');
  27466. Add(' v:=@GetIt;');
  27467. Add(' v:[email protected];');
  27468. Add(' v:[email protected];');
  27469. ConvertProgram;
  27470. CheckSource('TestJSValue_ProcType_Assign',
  27471. LinesToStr([ // statements
  27472. 'rtl.createClass(this, "TObject", null, function () {',
  27473. ' this.$init = function () {',
  27474. ' };',
  27475. ' this.$final = function () {',
  27476. ' };',
  27477. ' this.GetGlob = function () {',
  27478. ' var Result = 0;',
  27479. ' var v1 = undefined;',
  27480. ' v1 = rtl.createCallback(this, "GetGlob");',
  27481. ' v1 = rtl.createCallback(this, "GetGlob");',
  27482. ' return Result;',
  27483. ' };',
  27484. ' this.Getter = function () {',
  27485. ' var Result = 0;',
  27486. ' var v2 = undefined;',
  27487. ' v2 = rtl.createCallback(this, "Getter");',
  27488. ' v2 = rtl.createCallback(this, "Getter");',
  27489. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27490. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27491. ' return Result;',
  27492. ' };',
  27493. '});',
  27494. 'this.GetIt = function (i) {',
  27495. ' var Result = 0;',
  27496. ' var v3 = undefined;',
  27497. ' v3 = $mod.GetIt;',
  27498. ' return Result;',
  27499. '};',
  27500. 'this.V = undefined;',
  27501. 'this.o = null;',
  27502. '']),
  27503. LinesToStr([ // $mod.$main
  27504. '$mod.V = $mod.GetIt;',
  27505. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27506. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27507. '']));
  27508. end;
  27509. procedure TTestModule.TestJSValue_ProcType_Equal;
  27510. begin
  27511. StartProgram(false);
  27512. Add('type');
  27513. Add(' integer = longint;');
  27514. Add(' TObject = class');
  27515. Add(' class function GetGlob: integer;');
  27516. Add(' function Getter: integer;');
  27517. Add(' end;');
  27518. Add('class function TObject.GetGlob: integer;');
  27519. Add('var v1: jsvalue;');
  27520. Add('begin');
  27521. Add(' if v1=@GetGlob then;');
  27522. Add(' if [email protected] then ;');
  27523. Add('end;');
  27524. Add('function TObject.Getter: integer;');
  27525. Add('var v2: jsvalue;');
  27526. Add('begin');
  27527. Add(' if v2=@Getter then;');
  27528. Add(' if [email protected] then ;');
  27529. Add(' if v2=@GetGlob then;');
  27530. Add(' if [email protected] then;');
  27531. Add('end;');
  27532. Add('function GetIt(i: integer): integer;');
  27533. Add('var v3: jsvalue;');
  27534. Add('begin');
  27535. Add(' if v3=@GetIt then;');
  27536. Add('end;');
  27537. Add('var');
  27538. Add(' V: JSValue;');
  27539. Add(' o: TObject;');
  27540. Add('begin');
  27541. Add(' if v=@GetIt then;');
  27542. Add(' if [email protected] then;');
  27543. Add(' if [email protected] then;');
  27544. Add(' if @GetIt=v then;');
  27545. Add(' if @o.Getter=v then;');
  27546. Add(' if @o.GetGlob=v then;');
  27547. ConvertProgram;
  27548. CheckSource('TestJSValue_ProcType_Equal',
  27549. LinesToStr([ // statements
  27550. 'rtl.createClass(this, "TObject", null, function () {',
  27551. ' this.$init = function () {',
  27552. ' };',
  27553. ' this.$final = function () {',
  27554. ' };',
  27555. ' this.GetGlob = function () {',
  27556. ' var Result = 0;',
  27557. ' var v1 = undefined;',
  27558. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27559. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27560. ' return Result;',
  27561. ' };',
  27562. ' this.Getter = function () {',
  27563. ' var Result = 0;',
  27564. ' var v2 = undefined;',
  27565. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27566. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27567. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27568. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27569. ' return Result;',
  27570. ' };',
  27571. '});',
  27572. 'this.GetIt = function (i) {',
  27573. ' var Result = 0;',
  27574. ' var v3 = undefined;',
  27575. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27576. ' return Result;',
  27577. '};',
  27578. 'this.V = undefined;',
  27579. 'this.o = null;',
  27580. '']),
  27581. LinesToStr([ // $mod.$main
  27582. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27583. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27584. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27585. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27586. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27587. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27588. '']));
  27589. end;
  27590. procedure TTestModule.TestJSValue_ProcType_Param;
  27591. begin
  27592. StartProgram(false);
  27593. Add([
  27594. 'type',
  27595. ' variant = jsvalue;',
  27596. ' TArrVariant = array of variant;',
  27597. ' TArrVar2 = TArrVariant;',
  27598. ' TFuncInt = function: longint;',
  27599. 'function GetIt: longint;',
  27600. 'begin',
  27601. 'end;',
  27602. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27603. 'var v: variant;',
  27604. 'begin',
  27605. ' v:=arr[1];',
  27606. 'end;',
  27607. 'var s: string;',
  27608. 'begin',
  27609. ' DoIt(GetIt,[]);',
  27610. ' DoIt(@GetIt,[]);',
  27611. ' DoIt(1,[s,GetIt]);',
  27612. ' DoIt(1,[s,@GetIt]);',
  27613. '']);
  27614. ConvertProgram;
  27615. CheckSource('TestJSValue_ProcType_Param',
  27616. LinesToStr([ // statements
  27617. 'this.GetIt = function () {',
  27618. ' var Result = 0;',
  27619. ' return Result;',
  27620. '};',
  27621. 'this.DoIt = function (p, Arr) {',
  27622. ' var v = undefined;',
  27623. ' v = Arr[1];',
  27624. '};',
  27625. 'this.s = "";',
  27626. '']),
  27627. LinesToStr([ // $mod.$main
  27628. '$mod.DoIt($mod.GetIt(), []);',
  27629. '$mod.DoIt($mod.GetIt, []);',
  27630. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27631. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27632. '']));
  27633. end;
  27634. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27635. begin
  27636. StartProgram(false);
  27637. Add([
  27638. 'var',
  27639. ' v: JSValue;',
  27640. ' p: Pointer;',
  27641. 'begin',
  27642. ' p:=v;',
  27643. '']);
  27644. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27645. nIncompatibleTypesGotExpected);
  27646. ConvertProgram;
  27647. end;
  27648. procedure TTestModule.TestJSValue_OverloadDouble;
  27649. begin
  27650. StartProgram(false);
  27651. Add([
  27652. 'type',
  27653. ' integer = longint;',
  27654. ' tdatetime = double;',
  27655. 'procedure DoIt(d: double); begin end;',
  27656. 'procedure DoIt(v: jsvalue); begin end;',
  27657. 'var',
  27658. ' d: double;',
  27659. ' dt: tdatetime;',
  27660. ' i: integer;',
  27661. ' b: byte;',
  27662. ' shi: shortint;',
  27663. ' w: word;',
  27664. ' smi: smallint;',
  27665. ' lw: longword;',
  27666. ' li: longint;',
  27667. ' ni: nativeint;',
  27668. ' nu: nativeuint;',
  27669. 'begin',
  27670. ' DoIt(d);',
  27671. ' DoIt(dt);',
  27672. ' DoIt(i);',
  27673. ' DoIt(b);',
  27674. ' DoIt(shi);',
  27675. ' DoIt(w);',
  27676. ' DoIt(smi);',
  27677. ' DoIt(lw);',
  27678. ' DoIt(li);',
  27679. ' DoIt(ni);',
  27680. ' DoIt(nu);',
  27681. '']);
  27682. ConvertProgram;
  27683. CheckSource('TestJSValue_OverloadDouble',
  27684. LinesToStr([ // statements
  27685. 'this.DoIt = function (d) {',
  27686. '};',
  27687. 'this.DoIt$1 = function (v) {',
  27688. '};',
  27689. 'this.d = 0.0;',
  27690. 'this.dt = 0.0;',
  27691. 'this.i = 0;',
  27692. 'this.b = 0;',
  27693. 'this.shi = 0;',
  27694. 'this.w = 0;',
  27695. 'this.smi = 0;',
  27696. 'this.lw = 0;',
  27697. 'this.li = 0;',
  27698. 'this.ni = 0;',
  27699. 'this.nu = 0;',
  27700. '']),
  27701. LinesToStr([ // $mod.$main
  27702. '$mod.DoIt($mod.d);',
  27703. '$mod.DoIt($mod.dt);',
  27704. '$mod.DoIt$1($mod.i);',
  27705. '$mod.DoIt$1($mod.b);',
  27706. '$mod.DoIt$1($mod.shi);',
  27707. '$mod.DoIt$1($mod.w);',
  27708. '$mod.DoIt$1($mod.smi);',
  27709. '$mod.DoIt$1($mod.lw);',
  27710. '$mod.DoIt$1($mod.li);',
  27711. '$mod.DoIt$1($mod.ni);',
  27712. '$mod.DoIt$1($mod.nu);',
  27713. '']));
  27714. end;
  27715. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27716. begin
  27717. StartProgram(false);
  27718. Add([
  27719. 'type',
  27720. ' integer = longint;',
  27721. ' int53 = nativeint;',
  27722. ' tdatetime = double;',
  27723. 'procedure DoIt(n: nativeint); begin end;',
  27724. 'procedure DoIt(v: jsvalue); begin end;',
  27725. 'var',
  27726. ' d: double;',
  27727. ' dt: tdatetime;',
  27728. ' i: integer;',
  27729. ' b: byte;',
  27730. ' shi: shortint;',
  27731. ' w: word;',
  27732. ' smi: smallint;',
  27733. ' lw: longword;',
  27734. ' li: longint;',
  27735. ' ni: nativeint;',
  27736. ' nu: nativeuint;',
  27737. 'begin',
  27738. ' DoIt(d);',
  27739. ' DoIt(dt);',
  27740. ' DoIt(i);',
  27741. ' DoIt(b);',
  27742. ' DoIt(shi);',
  27743. ' DoIt(w);',
  27744. ' DoIt(smi);',
  27745. ' DoIt(lw);',
  27746. ' DoIt(li);',
  27747. ' DoIt(ni);',
  27748. ' DoIt(nu);',
  27749. '']);
  27750. ConvertProgram;
  27751. CheckSource('TestJSValue_OverloadNativeInt',
  27752. LinesToStr([ // statements
  27753. 'this.DoIt = function (n) {',
  27754. '};',
  27755. 'this.DoIt$1 = function (v) {',
  27756. '};',
  27757. 'this.d = 0.0;',
  27758. 'this.dt = 0.0;',
  27759. 'this.i = 0;',
  27760. 'this.b = 0;',
  27761. 'this.shi = 0;',
  27762. 'this.w = 0;',
  27763. 'this.smi = 0;',
  27764. 'this.lw = 0;',
  27765. 'this.li = 0;',
  27766. 'this.ni = 0;',
  27767. 'this.nu = 0;',
  27768. '']),
  27769. LinesToStr([ // $mod.$main
  27770. '$mod.DoIt$1($mod.d);',
  27771. '$mod.DoIt$1($mod.dt);',
  27772. '$mod.DoIt($mod.i);',
  27773. '$mod.DoIt($mod.b);',
  27774. '$mod.DoIt($mod.shi);',
  27775. '$mod.DoIt($mod.w);',
  27776. '$mod.DoIt($mod.smi);',
  27777. '$mod.DoIt($mod.lw);',
  27778. '$mod.DoIt($mod.li);',
  27779. '$mod.DoIt($mod.ni);',
  27780. '$mod.DoIt($mod.nu);',
  27781. '']));
  27782. end;
  27783. procedure TTestModule.TestJSValue_OverloadWord;
  27784. begin
  27785. StartProgram(false);
  27786. Add([
  27787. 'type',
  27788. ' integer = longint;',
  27789. ' int53 = nativeint;',
  27790. ' tdatetime = double;',
  27791. 'procedure DoIt(w: word); begin end;',
  27792. 'procedure DoIt(v: jsvalue); begin end;',
  27793. 'var',
  27794. ' d: double;',
  27795. ' dt: tdatetime;',
  27796. ' i: integer;',
  27797. ' b: byte;',
  27798. ' shi: shortint;',
  27799. ' w: word;',
  27800. ' smi: smallint;',
  27801. ' lw: longword;',
  27802. ' li: longint;',
  27803. ' ni: nativeint;',
  27804. ' nu: nativeuint;',
  27805. 'begin',
  27806. ' DoIt(d);',
  27807. ' DoIt(dt);',
  27808. ' DoIt(i);',
  27809. ' DoIt(b);',
  27810. ' DoIt(shi);',
  27811. ' DoIt(w);',
  27812. ' DoIt(smi);',
  27813. ' DoIt(lw);',
  27814. ' DoIt(li);',
  27815. ' DoIt(ni);',
  27816. ' DoIt(nu);',
  27817. '']);
  27818. ConvertProgram;
  27819. CheckSource('TestJSValue_OverloadWord',
  27820. LinesToStr([ // statements
  27821. 'this.DoIt = function (w) {',
  27822. '};',
  27823. 'this.DoIt$1 = function (v) {',
  27824. '};',
  27825. 'this.d = 0.0;',
  27826. 'this.dt = 0.0;',
  27827. 'this.i = 0;',
  27828. 'this.b = 0;',
  27829. 'this.shi = 0;',
  27830. 'this.w = 0;',
  27831. 'this.smi = 0;',
  27832. 'this.lw = 0;',
  27833. 'this.li = 0;',
  27834. 'this.ni = 0;',
  27835. 'this.nu = 0;',
  27836. '']),
  27837. LinesToStr([ // $mod.$main
  27838. '$mod.DoIt$1($mod.d);',
  27839. '$mod.DoIt$1($mod.dt);',
  27840. '$mod.DoIt$1($mod.i);',
  27841. '$mod.DoIt($mod.b);',
  27842. '$mod.DoIt($mod.shi);',
  27843. '$mod.DoIt($mod.w);',
  27844. '$mod.DoIt$1($mod.smi);',
  27845. '$mod.DoIt$1($mod.lw);',
  27846. '$mod.DoIt$1($mod.li);',
  27847. '$mod.DoIt$1($mod.ni);',
  27848. '$mod.DoIt$1($mod.nu);',
  27849. '']));
  27850. end;
  27851. procedure TTestModule.TestJSValue_OverloadString;
  27852. begin
  27853. StartProgram(false);
  27854. Add([
  27855. 'type',
  27856. ' uni = string;',
  27857. ' WChar = char;',
  27858. 'procedure DoIt(s: string); begin end;',
  27859. 'procedure DoIt(v: jsvalue); begin end;',
  27860. 'var',
  27861. ' s: string;',
  27862. ' c: char;',
  27863. ' u: uni;',
  27864. 'begin',
  27865. ' DoIt(s);',
  27866. ' DoIt(c);',
  27867. ' DoIt(u);',
  27868. '']);
  27869. ConvertProgram;
  27870. CheckSource('TestJSValue_OverloadString',
  27871. LinesToStr([ // statements
  27872. 'this.DoIt = function (s) {',
  27873. '};',
  27874. 'this.DoIt$1 = function (v) {',
  27875. '};',
  27876. 'this.s = "";',
  27877. 'this.c = "";',
  27878. 'this.u = "";',
  27879. '']),
  27880. LinesToStr([ // $mod.$main
  27881. '$mod.DoIt($mod.s);',
  27882. '$mod.DoIt($mod.c);',
  27883. '$mod.DoIt($mod.u);',
  27884. '']));
  27885. end;
  27886. procedure TTestModule.TestJSValue_OverloadChar;
  27887. begin
  27888. StartProgram(false);
  27889. Add([
  27890. 'type',
  27891. ' uni = string;',
  27892. ' WChar = char;',
  27893. 'procedure DoIt(c: char); begin end;',
  27894. 'procedure DoIt(v: jsvalue); begin end;',
  27895. 'var',
  27896. ' s: string;',
  27897. ' c: char;',
  27898. ' u: uni;',
  27899. 'begin',
  27900. ' DoIt(s);',
  27901. ' DoIt(c);',
  27902. ' DoIt(u);',
  27903. '']);
  27904. ConvertProgram;
  27905. CheckSource('TestJSValue_OverloadChar',
  27906. LinesToStr([ // statements
  27907. 'this.DoIt = function (c) {',
  27908. '};',
  27909. 'this.DoIt$1 = function (v) {',
  27910. '};',
  27911. 'this.s = "";',
  27912. 'this.c = "";',
  27913. 'this.u = "";',
  27914. '']),
  27915. LinesToStr([ // $mod.$main
  27916. '$mod.DoIt$1($mod.s);',
  27917. '$mod.DoIt($mod.c);',
  27918. '$mod.DoIt$1($mod.u);',
  27919. '']));
  27920. end;
  27921. procedure TTestModule.TestJSValue_OverloadPointer;
  27922. begin
  27923. StartProgram(false);
  27924. Add([
  27925. 'type',
  27926. ' TObject = class end;',
  27927. 'procedure DoIt(p: pointer); begin end;',
  27928. 'procedure DoIt(v: jsvalue); begin end;',
  27929. 'var',
  27930. ' o: TObject;',
  27931. 'begin',
  27932. ' DoIt(o);',
  27933. '']);
  27934. ConvertProgram;
  27935. CheckSource('TestJSValue_OverloadPointer',
  27936. LinesToStr([ // statements
  27937. 'rtl.createClass(this, "TObject", null, function () {',
  27938. ' this.$init = function () {',
  27939. ' };',
  27940. ' this.$final = function () {',
  27941. ' };',
  27942. '});',
  27943. 'this.DoIt = function (p) {',
  27944. '};',
  27945. 'this.DoIt$1 = function (v) {',
  27946. '};',
  27947. 'this.o = null;',
  27948. '']),
  27949. LinesToStr([ // $mod.$main
  27950. '$mod.DoIt($mod.o);',
  27951. '']));
  27952. end;
  27953. procedure TTestModule.TestJSValue_ForIn;
  27954. begin
  27955. StartProgram(false);
  27956. Add([
  27957. 'var',
  27958. ' v: JSValue;',
  27959. ' key: string;',
  27960. 'begin',
  27961. ' for key in v do begin',
  27962. ' if key=''abc'' then ;',
  27963. ' end;',
  27964. '']);
  27965. ConvertProgram;
  27966. CheckSource('TestJSValue_ForIn',
  27967. LinesToStr([ // statements
  27968. 'this.v = undefined;',
  27969. 'this.key = "";',
  27970. '']),
  27971. LinesToStr([ // $mod.$main
  27972. 'for ($mod.key in $mod.v) {',
  27973. ' if ($mod.key === "abc") ;',
  27974. '};',
  27975. '']));
  27976. end;
  27977. procedure TTestModule.TestRTTI_IntRange;
  27978. begin
  27979. WithTypeInfo:=true;
  27980. StartProgram(true,[supTypeInfo]);
  27981. Add([
  27982. '{$modeswitch externalclass}',
  27983. 'type',
  27984. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  27985. ' TColor = type TGraphicsColor;',
  27986. 'var',
  27987. ' p: TTypeInfo;',
  27988. ' k: TTypeKind;',
  27989. 'begin',
  27990. ' p:=typeinfo(TGraphicsColor);',
  27991. ' p:=typeinfo(TColor);',
  27992. ' k:=GetTypeKind(TGraphicsColor);',
  27993. ' k:=GetTypeKind(TColor);',
  27994. '']);
  27995. ConvertProgram;
  27996. CheckSource('TestRTTI_IntRange',
  27997. LinesToStr([ // statements
  27998. 'this.$rtti.$Int("TGraphicsColor", {',
  27999. ' minvalue: -2147483648,',
  28000. ' maxvalue: 2147483647,',
  28001. ' ordtype: 4',
  28002. '});',
  28003. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28004. 'this.p = null;',
  28005. 'this.k = 0;',
  28006. '']),
  28007. LinesToStr([ // $mod.$main
  28008. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28009. '$mod.p = $mod.$rtti["TColor"];',
  28010. '$mod.k = 1;',
  28011. '$mod.k = 1;',
  28012. '']));
  28013. end;
  28014. procedure TTestModule.TestRTTI_Double;
  28015. begin
  28016. WithTypeInfo:=true;
  28017. StartProgram(true,[supTypeInfo]);
  28018. Add([
  28019. '{$modeswitch externalclass}',
  28020. 'type',
  28021. ' TFloat = type double;',
  28022. 'var',
  28023. ' p: TTypeInfo;',
  28024. 'begin',
  28025. ' p:=typeinfo(double);',
  28026. ' p:=typeinfo(TFloat);',
  28027. '']);
  28028. ConvertProgram;
  28029. CheckSource('TestRTTI_Double',
  28030. LinesToStr([ // statements
  28031. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28032. 'this.p = null;',
  28033. '']),
  28034. LinesToStr([ // $mod.$main
  28035. '$mod.p = rtl.double;',
  28036. '$mod.p = $mod.$rtti["TFloat"];',
  28037. '']));
  28038. end;
  28039. procedure TTestModule.TestRTTI_ProcType;
  28040. begin
  28041. WithTypeInfo:=true;
  28042. StartProgram(false);
  28043. Add('type');
  28044. Add(' TProcA = procedure;');
  28045. Add(' TMethodB = procedure of object;');
  28046. Add(' TProcC = procedure; varargs;');
  28047. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28048. Add(' TProcE = function: nativeint;');
  28049. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28050. Add('var p: pointer;');
  28051. Add('begin');
  28052. Add(' p:=typeinfo(tproca);');
  28053. ConvertProgram;
  28054. CheckSource('TestRTTI_ProcType',
  28055. LinesToStr([ // statements
  28056. 'this.$rtti.$ProcVar("TProcA", {',
  28057. ' procsig: rtl.newTIProcSig(null)',
  28058. '});',
  28059. 'this.$rtti.$MethodVar("TMethodB", {',
  28060. ' procsig: rtl.newTIProcSig(null),',
  28061. ' methodkind: 0',
  28062. '});',
  28063. 'this.$rtti.$ProcVar("TProcC", {',
  28064. ' procsig: rtl.newTIProcSig(null, 2)',
  28065. '});',
  28066. 'this.$rtti.$ProcVar("TProcD", {',
  28067. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28068. '});',
  28069. 'this.$rtti.$ProcVar("TProcE", {',
  28070. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28071. '});',
  28072. 'this.$rtti.$ProcVar("TProcF", {',
  28073. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28074. '});',
  28075. 'this.p = null;',
  28076. '']),
  28077. LinesToStr([ // $mod.$main
  28078. '$mod.p = $mod.$rtti["TProcA"];',
  28079. '']));
  28080. end;
  28081. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28082. begin
  28083. WithTypeInfo:=true;
  28084. AddModuleWithIntfImplSrc('unit2.pas',
  28085. LinesToStr([
  28086. 'type',
  28087. ' TObject = class end;'
  28088. ]),
  28089. '');
  28090. StartUnit(true);
  28091. Add('interface');
  28092. Add('uses unit2;');
  28093. Add('type');
  28094. Add(' TProcA = function(o: tobject): tobject;');
  28095. Add('implementation');
  28096. Add('type');
  28097. Add(' TProcB = function(o: tobject): tobject;');
  28098. Add('var p: Pointer;');
  28099. Add('initialization');
  28100. Add(' p:=typeinfo(tproca);');
  28101. Add(' p:=typeinfo(tprocb);');
  28102. ConvertUnit;
  28103. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28104. LinesToStr([ // statements
  28105. 'var $impl = $mod.$impl;',
  28106. 'this.$rtti.$ProcVar("TProcA", {',
  28107. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28108. '});',
  28109. '']),
  28110. LinesToStr([ // this.$init
  28111. '$impl.p = $mod.$rtti["TProcA"];',
  28112. '$impl.p = $mod.$rtti["TProcB"];',
  28113. '']),
  28114. LinesToStr([ // implementation
  28115. '$mod.$rtti.$ProcVar("TProcB", {',
  28116. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28117. '});',
  28118. '$impl.p = null;',
  28119. '']) );
  28120. end;
  28121. procedure TTestModule.TestRTTI_EnumAndSetType;
  28122. begin
  28123. WithTypeInfo:=true;
  28124. StartProgram(false);
  28125. Add('type');
  28126. Add(' TFlag = (light,dark);');
  28127. Add(' TFlags = set of TFlag;');
  28128. Add(' TProc = function(f: TFlags): TFlag;');
  28129. Add('var p: pointer;');
  28130. Add('begin');
  28131. Add(' p:=typeinfo(tflag);');
  28132. Add(' p:=typeinfo(tflags);');
  28133. ConvertProgram;
  28134. CheckSource('TestRTTI_EnumAndType',
  28135. LinesToStr([ // statements
  28136. 'this.TFlag = {',
  28137. ' "0": "light",',
  28138. ' light: 0,',
  28139. ' "1": "dark",',
  28140. ' dark: 1',
  28141. '};',
  28142. 'this.$rtti.$Enum("TFlag", {',
  28143. ' minvalue: 0,',
  28144. ' maxvalue: 1,',
  28145. ' ordtype: 1,',
  28146. ' enumtype: this.TFlag',
  28147. '});',
  28148. 'this.$rtti.$Set("TFlags", {',
  28149. ' comptype: this.$rtti["TFlag"]',
  28150. '});',
  28151. 'this.$rtti.$ProcVar("TProc", {',
  28152. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28153. '});',
  28154. 'this.p = null;',
  28155. '']),
  28156. LinesToStr([ // $mod.$main
  28157. '$mod.p = $mod.$rtti["TFlag"];',
  28158. '$mod.p = $mod.$rtti["TFlags"];',
  28159. '']));
  28160. end;
  28161. procedure TTestModule.TestRTTI_EnumRange;
  28162. begin
  28163. WithTypeInfo:=true;
  28164. StartProgram(false);
  28165. Add([
  28166. 'type',
  28167. ' TCol = (red,green,blue);',
  28168. ' TColRg = green..blue;',
  28169. ' TSetOfColRg = set of TColRg;',
  28170. 'var p: pointer;',
  28171. 'begin',
  28172. ' p:=typeinfo(tcolrg);',
  28173. ' p:=typeinfo(tsetofcolrg);',
  28174. '']);
  28175. ConvertProgram;
  28176. end;
  28177. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28178. begin
  28179. WithTypeInfo:=true;
  28180. StartProgram(false);
  28181. Add('type');
  28182. Add(' TFlags = set of (red, green);');
  28183. Add('var');
  28184. Add(' f: TFlags;');
  28185. Add('begin');
  28186. Add(' Include(f,red);');
  28187. ConvertProgram;
  28188. CheckSource('TestRTTI_AnonymousEnumType',
  28189. LinesToStr([ // statements
  28190. 'this.TFlags$a = {',
  28191. ' "0": "red",',
  28192. ' red: 0,',
  28193. ' "1": "green",',
  28194. ' green: 1',
  28195. '};',
  28196. 'this.$rtti.$Enum("TFlags$a", {',
  28197. ' minvalue: 0,',
  28198. ' maxvalue: 1,',
  28199. ' ordtype: 1,',
  28200. ' enumtype: this.TFlags$a',
  28201. '});',
  28202. 'this.$rtti.$Set("TFlags", {',
  28203. ' comptype: this.$rtti["TFlags$a"]',
  28204. '});',
  28205. 'this.f = {};',
  28206. '']),
  28207. LinesToStr([
  28208. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28209. '']));
  28210. end;
  28211. procedure TTestModule.TestRTTI_StaticArray;
  28212. begin
  28213. WithTypeInfo:=true;
  28214. StartProgram(false);
  28215. Add('type');
  28216. Add(' TFlag = (light,dark);');
  28217. Add(' TFlagNames = array[TFlag] of string;');
  28218. Add(' TBoolNames = array[boolean] of string;');
  28219. Add(' TByteArray = array[1..32768] of byte;');
  28220. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28221. Add('var p: pointer;');
  28222. Add('begin');
  28223. Add(' p:=typeinfo(TFlagNames);');
  28224. Add(' p:=typeinfo(TBoolNames);');
  28225. ConvertProgram;
  28226. CheckSource('TestRTTI_StaticArray',
  28227. LinesToStr([ // statements
  28228. 'this.TFlag = {',
  28229. ' "0": "light",',
  28230. ' light: 0,',
  28231. ' "1": "dark",',
  28232. ' dark: 1',
  28233. '};',
  28234. 'this.$rtti.$Enum("TFlag", {',
  28235. ' minvalue: 0,',
  28236. ' maxvalue: 1,',
  28237. ' ordtype: 1,',
  28238. ' enumtype: this.TFlag',
  28239. '});',
  28240. 'this.$rtti.$StaticArray("TFlagNames", {',
  28241. ' dims: [2],',
  28242. ' eltype: rtl.string',
  28243. '});',
  28244. 'this.$rtti.$StaticArray("TBoolNames", {',
  28245. ' dims: [2],',
  28246. ' eltype: rtl.string',
  28247. '});',
  28248. 'this.$rtti.$StaticArray("TByteArray", {',
  28249. ' dims: [32768],',
  28250. ' eltype: rtl.byte',
  28251. '});',
  28252. 'this.$rtti.$ProcVar("TProc", {',
  28253. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28254. '});',
  28255. 'this.p = null;',
  28256. '']),
  28257. LinesToStr([ // $mod.$main
  28258. '$mod.p = $mod.$rtti["TFlagNames"];',
  28259. '$mod.p = $mod.$rtti["TBoolNames"];',
  28260. '']));
  28261. end;
  28262. procedure TTestModule.TestRTTI_DynArray;
  28263. begin
  28264. WithTypeInfo:=true;
  28265. StartProgram(false);
  28266. Add('type');
  28267. Add(' TArrStr = array of string;');
  28268. Add(' TArr2Dim = array of tarrstr;');
  28269. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28270. Add('var p: pointer;');
  28271. Add('begin');
  28272. Add(' p:=typeinfo(tarrstr);');
  28273. Add(' p:=typeinfo(tarr2dim);');
  28274. ConvertProgram;
  28275. CheckSource('TestRTTI_DynArray',
  28276. LinesToStr([ // statements
  28277. 'this.$rtti.$DynArray("TArrStr", {',
  28278. ' eltype: rtl.string',
  28279. '});',
  28280. 'this.$rtti.$DynArray("TArr2Dim", {',
  28281. ' eltype: this.$rtti["TArrStr"]',
  28282. '});',
  28283. 'this.$rtti.$ProcVar("TProc", {',
  28284. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28285. '});',
  28286. 'this.p = null;',
  28287. '']),
  28288. LinesToStr([ // $mod.$main
  28289. '$mod.p = $mod.$rtti["TArrStr"];',
  28290. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28291. '']));
  28292. end;
  28293. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28294. begin
  28295. WithTypeInfo:=true;
  28296. StartProgram(false);
  28297. Add('type');
  28298. Add(' TArr = array of array of longint;');
  28299. Add('var a: TArr;');
  28300. Add('begin');
  28301. ConvertProgram;
  28302. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28303. LinesToStr([ // statements
  28304. 'this.$rtti.$DynArray("TArr$a", {',
  28305. ' eltype: rtl.longint',
  28306. '});',
  28307. 'this.$rtti.$DynArray("TArr", {',
  28308. ' eltype: this.$rtti["TArr$a"]',
  28309. '});',
  28310. 'this.a = [];',
  28311. '']),
  28312. LinesToStr([ // $mod.$main
  28313. ]));
  28314. end;
  28315. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28316. begin
  28317. WithTypeInfo:=true;
  28318. StartProgram(false);
  28319. Add('type');
  28320. Add(' TObject = class');
  28321. Add(' published');
  28322. Add(' procedure Proc; virtual; abstract;');
  28323. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28324. Add(' end;');
  28325. Add('begin');
  28326. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28327. nDuplicatePublishedMethodXAtY);
  28328. ConvertProgram;
  28329. end;
  28330. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28331. begin
  28332. WithTypeInfo:=true;
  28333. StartProgram(false);
  28334. Add('type');
  28335. Add(' TObject = class');
  28336. Add(' published');
  28337. Add(' procedure Proc; external name ''foo'';');
  28338. Add(' end;');
  28339. Add('begin');
  28340. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28341. nPublishedNameMustMatchExternal);
  28342. ConvertProgram;
  28343. end;
  28344. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28345. begin
  28346. WithTypeInfo:=true;
  28347. StartProgram(false);
  28348. Add('type');
  28349. Add(' TObject = class');
  28350. Add(' class var FA: longint;');
  28351. Add(' published');
  28352. Add(' class property A: longint read FA;');
  28353. Add(' end;');
  28354. Add('begin');
  28355. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28356. nInvalidXModifierY);
  28357. ConvertProgram;
  28358. end;
  28359. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28360. begin
  28361. WithTypeInfo:=true;
  28362. StartProgram(false);
  28363. Add('type');
  28364. Add(' TObject = class');
  28365. Add(' published');
  28366. Add(' class var FA: longint;');
  28367. Add(' end;');
  28368. Add('begin');
  28369. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28370. nSymbolCannotBePublished);
  28371. ConvertProgram;
  28372. end;
  28373. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28374. begin
  28375. WithTypeInfo:=true;
  28376. StartProgram(false);
  28377. Add('{$modeswitch externalclass}');
  28378. Add('type');
  28379. Add(' TObject = class');
  28380. Add(' published');
  28381. Add(' V: longint; external name ''foo'';');
  28382. Add(' end;');
  28383. Add('begin');
  28384. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28385. nPublishedNameMustMatchExternal);
  28386. ConvertProgram;
  28387. end;
  28388. procedure TTestModule.TestRTTI_Class_Field;
  28389. begin
  28390. WithTypeInfo:=true;
  28391. StartProgram(false);
  28392. Add('{$modeswitch externalclass}');
  28393. Add('type');
  28394. Add(' TObject = class');
  28395. Add(' private');
  28396. Add(' FPropA: string;');
  28397. Add(' published');
  28398. Add(' VarLI: longint;');
  28399. Add(' VarC: char;');
  28400. Add(' VarS: string;');
  28401. Add(' VarD: double;');
  28402. Add(' VarB: boolean;');
  28403. Add(' VarLW: longword;');
  28404. Add(' VarSmI: smallint;');
  28405. Add(' VarW: word;');
  28406. Add(' VarShI: shortint;');
  28407. Add(' VarBy: byte;');
  28408. Add(' VarExt: longint external name ''VarExt'';');
  28409. Add(' ArrA, ArrB: array of byte;');
  28410. Add(' end;');
  28411. Add('var p: pointer;');
  28412. Add(' Obj: tobject;');
  28413. Add('begin');
  28414. Add(' p:=typeinfo(tobject);');
  28415. Add(' p:=typeinfo(p);');
  28416. Add(' p:=typeinfo(obj);');
  28417. ConvertProgram;
  28418. CheckSource('TestRTTI_Class_Field',
  28419. LinesToStr([ // statements
  28420. 'rtl.createClass(this, "TObject", null, function () {',
  28421. ' this.$init = function () {',
  28422. ' this.FPropA = "";',
  28423. ' this.VarLI = 0;',
  28424. ' this.VarC = "";',
  28425. ' this.VarS = "";',
  28426. ' this.VarD = 0.0;',
  28427. ' this.VarB = false;',
  28428. ' this.VarLW = 0;',
  28429. ' this.VarSmI = 0;',
  28430. ' this.VarW = 0;',
  28431. ' this.VarShI = 0;',
  28432. ' this.VarBy = 0;',
  28433. ' this.ArrA = [];',
  28434. ' this.ArrB = [];',
  28435. ' };',
  28436. ' this.$final = function () {',
  28437. ' this.ArrA = undefined;',
  28438. ' this.ArrB = undefined;',
  28439. ' };',
  28440. ' var $r = this.$rtti;',
  28441. ' $r.addField("VarLI", rtl.longint);',
  28442. ' $r.addField("VarC", rtl.char);',
  28443. ' $r.addField("VarS", rtl.string);',
  28444. ' $r.addField("VarD", rtl.double);',
  28445. ' $r.addField("VarB", rtl.boolean);',
  28446. ' $r.addField("VarLW", rtl.longword);',
  28447. ' $r.addField("VarSmI", rtl.smallint);',
  28448. ' $r.addField("VarW", rtl.word);',
  28449. ' $r.addField("VarShI", rtl.shortint);',
  28450. ' $r.addField("VarBy", rtl.byte);',
  28451. ' $r.addField("VarExt", rtl.longint);',
  28452. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28453. ' eltype: rtl.byte',
  28454. ' });',
  28455. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28456. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28457. '});',
  28458. 'this.p = null;',
  28459. 'this.Obj = null;',
  28460. '']),
  28461. LinesToStr([ // $mod.$main
  28462. '$mod.p = $mod.$rtti["TObject"];',
  28463. '$mod.p = rtl.pointer;',
  28464. '$mod.p = $mod.Obj.$rtti;',
  28465. '']));
  28466. end;
  28467. procedure TTestModule.TestRTTI_Class_Method;
  28468. begin
  28469. WithTypeInfo:=true;
  28470. StartProgram(false);
  28471. Add('type');
  28472. Add(' TObject = class');
  28473. Add(' private');
  28474. Add(' procedure Internal; external name ''$intern'';');
  28475. Add(' published');
  28476. Add(' procedure Click; virtual; abstract;');
  28477. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28478. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28479. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28480. Add(' end;');
  28481. Add('begin');
  28482. ConvertProgram;
  28483. CheckSource('TestRTTI_Class_Method',
  28484. LinesToStr([ // statements
  28485. 'rtl.createClass(this, "TObject", null, function () {',
  28486. ' this.$init = function () {',
  28487. ' };',
  28488. ' this.$final = function () {',
  28489. ' };',
  28490. ' var $r = this.$rtti;',
  28491. ' $r.addMethod("Click", 0, null);',
  28492. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28493. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28494. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28495. ' flags: 2',
  28496. ' });',
  28497. '});',
  28498. '']),
  28499. LinesToStr([ // $mod.$main
  28500. '']));
  28501. end;
  28502. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28503. begin
  28504. WithTypeInfo:=true;
  28505. StartProgram(false);
  28506. Add('type');
  28507. Add(' TObject = class');
  28508. Add(' published');
  28509. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28510. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28511. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28512. Add(' end;');
  28513. Add('begin');
  28514. ConvertProgram;
  28515. CheckSource('TestRTTI_Class_MethodOpenArray',
  28516. LinesToStr([ // statements
  28517. 'rtl.createClass(this, "TObject", null, function () {',
  28518. ' this.$init = function () {',
  28519. ' };',
  28520. ' this.$final = function () {',
  28521. ' };',
  28522. ' var $r = this.$rtti;',
  28523. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28524. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28525. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28526. '});',
  28527. '']),
  28528. LinesToStr([ // $mod.$main
  28529. '']));
  28530. end;
  28531. procedure TTestModule.TestRTTI_Class_Property;
  28532. begin
  28533. WithTypeInfo:=true;
  28534. StartProgram(false);
  28535. Add('{$modeswitch externalclass}');
  28536. Add('type');
  28537. Add(' TObject = class');
  28538. Add(' private');
  28539. Add(' FColor: longint;');
  28540. Add(' FColorStored: boolean;');
  28541. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28542. Add(' function GetColor: longint; virtual; abstract;');
  28543. Add(' function GetColorStored: boolean; virtual; abstract;');
  28544. Add(' FExtSize: longint external name ''$extSize'';');
  28545. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28546. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28547. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28548. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28549. Add(' published');
  28550. Add(' property ColorA: longint read FColor;');
  28551. Add(' property ColorB: longint write FColor;');
  28552. Add(' property ColorC: longint read GetColor write SetColor;');
  28553. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28554. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28555. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28556. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28557. Add(' end;');
  28558. Add('begin');
  28559. ConvertProgram;
  28560. CheckSource('TestRTTI_Class_Property',
  28561. LinesToStr([ // statements
  28562. 'rtl.createClass(this, "TObject", null, function () {',
  28563. ' this.$init = function () {',
  28564. ' this.FColor = 0;',
  28565. ' this.FColorStored = false;',
  28566. ' };',
  28567. ' this.$final = function () {',
  28568. ' };',
  28569. ' var $r = this.$rtti;',
  28570. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28571. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28572. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28573. ' $r.addProperty(',
  28574. ' "ColorD",',
  28575. ' 8,',
  28576. ' rtl.longint,',
  28577. ' "FColor",',
  28578. ' "FColor",',
  28579. ' {',
  28580. ' stored: "FColorStored"',
  28581. ' }',
  28582. ' );',
  28583. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28584. ' $r.addProperty(',
  28585. ' "ExtSizeB",',
  28586. ' 11,',
  28587. ' rtl.longint,',
  28588. ' "$getSize",',
  28589. ' "$setSize",',
  28590. ' {',
  28591. ' stored: "$extSizeStored"',
  28592. ' }',
  28593. ' );',
  28594. ' $r.addProperty(',
  28595. ' "ExtSizeC",',
  28596. ' 12,',
  28597. ' rtl.longint,',
  28598. ' "$extSize",',
  28599. ' "$extSize",',
  28600. ' {',
  28601. ' stored: "$getExtSizeStored"',
  28602. ' }',
  28603. ' );',
  28604. '});',
  28605. '']),
  28606. LinesToStr([ // $mod.$main
  28607. '']));
  28608. end;
  28609. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28610. begin
  28611. WithTypeInfo:=true;
  28612. StartProgram(false);
  28613. Add('{$modeswitch externalclass}');
  28614. Add('type');
  28615. Add(' integer = longint;');
  28616. Add(' TObject = class');
  28617. Add(' private');
  28618. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28619. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28620. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28621. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28622. Add(' published');
  28623. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28624. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28625. Add(' end;');
  28626. Add('begin');
  28627. ConvertProgram;
  28628. CheckSource('TestRTTI_Class_PropertyParams',
  28629. LinesToStr([ // statements
  28630. 'rtl.createClass(this, "TObject", null, function () {',
  28631. ' this.$init = function () {',
  28632. ' };',
  28633. ' this.$final = function () {',
  28634. ' };',
  28635. ' var $r = this.$rtti;',
  28636. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28637. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28638. '});',
  28639. '']),
  28640. LinesToStr([ // $mod.$main
  28641. '']));
  28642. end;
  28643. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28644. begin
  28645. WithTypeInfo:=true;
  28646. AddModuleWithIntfImplSrc('unit1.pas',
  28647. 'type TColor = -5..5;',
  28648. '');
  28649. StartProgram(true);
  28650. Add([
  28651. 'uses unit1;',
  28652. 'type',
  28653. ' TColorAlias = TColor;',
  28654. ' TColorTypeAlias = type TColor;',
  28655. ' TObject = class',
  28656. ' private',
  28657. ' fColor: TColor;',
  28658. ' fAlias: TColorAlias;',
  28659. ' fTypeAlias: TColorTypeAlias;',
  28660. ' published',
  28661. ' property Color: TColor read fcolor;',
  28662. ' property Alias: TColorAlias read falias;',
  28663. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28664. ' end;',
  28665. 'begin',
  28666. '']);
  28667. ConvertProgram;
  28668. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28669. LinesToStr([ // statements
  28670. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28671. 'rtl.createClass(this, "TObject", null, function () {',
  28672. ' this.$init = function () {',
  28673. ' this.fColor = 0;',
  28674. ' this.fAlias = 0;',
  28675. ' this.fTypeAlias = 0;',
  28676. ' };',
  28677. ' this.$final = function () {',
  28678. ' };',
  28679. ' var $r = this.$rtti;',
  28680. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28681. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28682. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28683. '});',
  28684. '']),
  28685. LinesToStr([ // $mod.$main
  28686. '']));
  28687. end;
  28688. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28689. begin
  28690. WithTypeInfo:=true;
  28691. StartProgram(false);
  28692. Add([
  28693. '{$modeswitch omitrtti}',
  28694. 'type',
  28695. ' TObject = class',
  28696. ' private',
  28697. ' FA: byte;',
  28698. ' published',
  28699. ' property A: byte read FA write FA;',
  28700. ' end;',
  28701. 'begin']);
  28702. ConvertProgram;
  28703. CheckSource('TestRTTI_Class_OmitRTTI',
  28704. LinesToStr([ // statements
  28705. 'rtl.createClass(this, "TObject", null, function () {',
  28706. ' this.$init = function () {',
  28707. ' this.FA = 0;',
  28708. ' };',
  28709. ' this.$final = function () {',
  28710. ' };',
  28711. '});',
  28712. '']),
  28713. LinesToStr([ // $mod.$main
  28714. '']));
  28715. end;
  28716. procedure TTestModule.TestRTTI_IndexModifier;
  28717. begin
  28718. WithTypeInfo:=true;
  28719. StartProgram(false);
  28720. Add([
  28721. 'type',
  28722. ' TEnum = (red, blue);',
  28723. ' TObject = class',
  28724. ' FB: boolean;',
  28725. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28726. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28727. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28728. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28729. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28730. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28731. ' published',
  28732. ' property B1: boolean index 1 read FB write SetIntBool;',
  28733. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28734. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28735. ' end;',
  28736. 'begin']);
  28737. ConvertProgram;
  28738. CheckSource('TestRTTI_IndexModifier',
  28739. LinesToStr([ // statements
  28740. 'this.TEnum = {',
  28741. ' "0": "red",',
  28742. ' red: 0,',
  28743. ' "1": "blue",',
  28744. ' blue: 1',
  28745. '};',
  28746. 'this.$rtti.$Enum("TEnum", {',
  28747. ' minvalue: 0,',
  28748. ' maxvalue: 1,',
  28749. ' ordtype: 1,',
  28750. ' enumtype: this.TEnum',
  28751. '});',
  28752. 'rtl.createClass(this, "TObject", null, function () {',
  28753. ' this.$init = function () {',
  28754. ' this.FB = false;',
  28755. ' };',
  28756. ' this.$final = function () {',
  28757. ' };',
  28758. ' var $r = this.$rtti;',
  28759. ' $r.addProperty(',
  28760. ' "B1",',
  28761. ' 18,',
  28762. ' rtl.boolean,',
  28763. ' "FB",',
  28764. ' "SetIntBool",',
  28765. ' {',
  28766. ' index: 1',
  28767. ' }',
  28768. ' );',
  28769. ' $r.addProperty(',
  28770. ' "B2",',
  28771. ' 17,',
  28772. ' rtl.boolean,',
  28773. ' "GetEnumBool",',
  28774. ' "FB",',
  28775. ' {',
  28776. ' index: $mod.TEnum.blue',
  28777. ' }',
  28778. ' );',
  28779. ' $r.addProperty(',
  28780. ' "I1",',
  28781. ' 19,',
  28782. ' rtl.boolean,',
  28783. ' "GetStrIntBool",',
  28784. ' "SetStrIntBool",',
  28785. ' {',
  28786. ' index: 2',
  28787. ' }',
  28788. ' );',
  28789. '});',
  28790. '']),
  28791. LinesToStr([ // $mod.$main
  28792. '']));
  28793. end;
  28794. procedure TTestModule.TestRTTI_StoredModifier;
  28795. begin
  28796. WithTypeInfo:=true;
  28797. StartProgram(false);
  28798. Add([
  28799. 'const',
  28800. ' ConstB = true;',
  28801. 'type',
  28802. ' TObject = class',
  28803. ' private',
  28804. ' FB: boolean;',
  28805. ' function IsBStored: boolean; virtual; abstract;',
  28806. ' published',
  28807. ' property BoolA: boolean read FB stored true;',
  28808. ' property BoolB: boolean read FB stored false;',
  28809. ' property BoolC: boolean read FB stored FB;',
  28810. ' property BoolD: boolean read FB stored ConstB;',
  28811. ' property BoolE: boolean read FB stored IsBStored;',
  28812. ' end;',
  28813. 'begin']);
  28814. ConvertProgram;
  28815. CheckSource('TestRTTI_StoredModifier',
  28816. LinesToStr([ // statements
  28817. 'this.ConstB = true;',
  28818. 'rtl.createClass(this, "TObject", null, function () {',
  28819. ' this.$init = function () {',
  28820. ' this.FB = false;',
  28821. ' };',
  28822. ' this.$final = function () {',
  28823. ' };',
  28824. ' var $r = this.$rtti;',
  28825. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  28826. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  28827. ' $r.addProperty(',
  28828. ' "BoolC",',
  28829. ' 8,',
  28830. ' rtl.boolean,',
  28831. ' "FB",',
  28832. ' "",',
  28833. ' {',
  28834. ' stored: "FB"',
  28835. ' }',
  28836. ' );',
  28837. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  28838. ' $r.addProperty(',
  28839. ' "BoolE",',
  28840. ' 12,',
  28841. ' rtl.boolean,',
  28842. ' "FB",',
  28843. ' "",',
  28844. ' {',
  28845. ' stored: "IsBStored"',
  28846. ' }',
  28847. ' );',
  28848. '});',
  28849. '']),
  28850. LinesToStr([ // $mod.$main
  28851. '']));
  28852. end;
  28853. procedure TTestModule.TestRTTI_DefaultValue;
  28854. begin
  28855. WithTypeInfo:=true;
  28856. StartProgram(false);
  28857. Add([
  28858. 'type',
  28859. ' TEnum = (red, blue);',
  28860. 'const',
  28861. ' CB = true or false;',
  28862. ' CI = 1+2;',
  28863. 'type',
  28864. ' TObject = class',
  28865. ' FB: boolean;',
  28866. ' FI: longint;',
  28867. ' FE: TEnum;',
  28868. ' published',
  28869. ' property B1: boolean read FB default true;',
  28870. ' property B2: boolean read FB default CB;',
  28871. ' property B3: boolean read FB default test1.cb;',
  28872. ' property I1: longint read FI default 2;',
  28873. ' property I2: longint read FI default CI;',
  28874. ' property E1: TEnum read FE default red;',
  28875. ' property E2: TEnum read FE default TEnum.blue;',
  28876. ' end;',
  28877. 'begin']);
  28878. ConvertProgram;
  28879. CheckSource('TestRTTI_DefaultValue',
  28880. LinesToStr([ // statements
  28881. 'this.TEnum = {',
  28882. ' "0": "red",',
  28883. ' red: 0,',
  28884. ' "1": "blue",',
  28885. ' blue: 1',
  28886. '};',
  28887. 'this.$rtti.$Enum("TEnum", {',
  28888. ' minvalue: 0,',
  28889. ' maxvalue: 1,',
  28890. ' ordtype: 1,',
  28891. ' enumtype: this.TEnum',
  28892. '});',
  28893. 'this.CB = true || false;',
  28894. 'this.CI = 1 + 2;',
  28895. 'rtl.createClass(this, "TObject", null, function () {',
  28896. ' this.$init = function () {',
  28897. ' this.FB = false;',
  28898. ' this.FI = 0;',
  28899. ' this.FE = 0;',
  28900. ' };',
  28901. ' this.$final = function () {',
  28902. ' };',
  28903. ' var $r = this.$rtti;',
  28904. ' $r.addProperty(',
  28905. ' "B1",',
  28906. ' 0,',
  28907. ' rtl.boolean,',
  28908. ' "FB",',
  28909. ' "",',
  28910. ' {',
  28911. ' Default: true',
  28912. ' }',
  28913. ' );',
  28914. ' $r.addProperty(',
  28915. ' "B2",',
  28916. ' 0,',
  28917. ' rtl.boolean,',
  28918. ' "FB",',
  28919. ' "",',
  28920. ' {',
  28921. ' Default: true',
  28922. ' }',
  28923. ' );',
  28924. ' $r.addProperty(',
  28925. ' "B3",',
  28926. ' 0,',
  28927. ' rtl.boolean,',
  28928. ' "FB",',
  28929. ' "",',
  28930. ' {',
  28931. ' Default: true',
  28932. ' }',
  28933. ' );',
  28934. ' $r.addProperty(',
  28935. ' "I1",',
  28936. ' 0,',
  28937. ' rtl.longint,',
  28938. ' "FI",',
  28939. ' "",',
  28940. ' {',
  28941. ' Default: 2',
  28942. ' }',
  28943. ' );',
  28944. ' $r.addProperty(',
  28945. ' "I2",',
  28946. ' 0,',
  28947. ' rtl.longint,',
  28948. ' "FI",',
  28949. ' "",',
  28950. ' {',
  28951. ' Default: 3',
  28952. ' }',
  28953. ' );',
  28954. ' $r.addProperty(',
  28955. ' "E1",',
  28956. ' 0,',
  28957. ' $mod.$rtti["TEnum"],',
  28958. ' "FE",',
  28959. ' "",',
  28960. ' {',
  28961. ' Default: $mod.TEnum.red',
  28962. ' }',
  28963. ' );',
  28964. ' $r.addProperty(',
  28965. ' "E2",',
  28966. ' 0,',
  28967. ' $mod.$rtti["TEnum"],',
  28968. ' "FE",',
  28969. ' "",',
  28970. ' {',
  28971. ' Default: $mod.TEnum.blue',
  28972. ' }',
  28973. ' );',
  28974. '});',
  28975. '']),
  28976. LinesToStr([ // $mod.$main
  28977. '']));
  28978. end;
  28979. procedure TTestModule.TestRTTI_DefaultValueSet;
  28980. begin
  28981. WithTypeInfo:=true;
  28982. StartProgram(false);
  28983. Add([
  28984. 'type',
  28985. ' TEnum = (red, blue);',
  28986. ' TSet = set of TEnum;',
  28987. 'const',
  28988. ' CSet = [red,blue];',
  28989. 'type',
  28990. ' TObject = class',
  28991. ' FSet: TSet;',
  28992. ' published',
  28993. ' property Set1: TSet read FSet default [];',
  28994. ' property Set2: TSet read FSet default [red];',
  28995. ' property Set3: TSet read FSet default [red,blue];',
  28996. ' property Set4: TSet read FSet default CSet;',
  28997. ' end;',
  28998. 'begin']);
  28999. ConvertProgram;
  29000. CheckSource('TestRTTI_DefaultValueSet',
  29001. LinesToStr([ // statements
  29002. 'this.TEnum = {',
  29003. ' "0": "red",',
  29004. ' red: 0,',
  29005. ' "1": "blue",',
  29006. ' blue: 1',
  29007. '};',
  29008. 'this.$rtti.$Enum("TEnum", {',
  29009. ' minvalue: 0,',
  29010. ' maxvalue: 1,',
  29011. ' ordtype: 1,',
  29012. ' enumtype: this.TEnum',
  29013. '});',
  29014. 'this.$rtti.$Set("TSet", {',
  29015. ' comptype: this.$rtti["TEnum"]',
  29016. '});',
  29017. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29018. 'rtl.createClass(this, "TObject", null, function () {',
  29019. ' this.$init = function () {',
  29020. ' this.FSet = {};',
  29021. ' };',
  29022. ' this.$final = function () {',
  29023. ' this.FSet = undefined;',
  29024. ' };',
  29025. ' var $r = this.$rtti;',
  29026. ' $r.addProperty(',
  29027. ' "Set1",',
  29028. ' 0,',
  29029. ' $mod.$rtti["TSet"],',
  29030. ' "FSet",',
  29031. ' "",',
  29032. ' {',
  29033. ' Default: {}',
  29034. ' }',
  29035. ' );',
  29036. ' $r.addProperty(',
  29037. ' "Set2",',
  29038. ' 0,',
  29039. ' $mod.$rtti["TSet"],',
  29040. ' "FSet",',
  29041. ' "",',
  29042. ' {',
  29043. ' Default: rtl.createSet($mod.TEnum.red)',
  29044. ' }',
  29045. ' );',
  29046. ' $r.addProperty(',
  29047. ' "Set3",',
  29048. ' 0,',
  29049. ' $mod.$rtti["TSet"],',
  29050. ' "FSet",',
  29051. ' "",',
  29052. ' {',
  29053. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29054. ' }',
  29055. ' );',
  29056. ' $r.addProperty(',
  29057. ' "Set4",',
  29058. ' 0,',
  29059. ' $mod.$rtti["TSet"],',
  29060. ' "FSet",',
  29061. ' "",',
  29062. ' {',
  29063. ' Default: $mod.CSet',
  29064. ' }',
  29065. ' );',
  29066. '});',
  29067. '']),
  29068. LinesToStr([ // $mod.$main
  29069. '']));
  29070. end;
  29071. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29072. begin
  29073. WithTypeInfo:=true;
  29074. StartProgram(false);
  29075. Add([
  29076. 'type',
  29077. ' TRg = -1..1;',
  29078. 'const',
  29079. ' l = low(TRg);',
  29080. ' h = high(TRg);',
  29081. 'type',
  29082. ' TObject = class',
  29083. ' FV: TRg;',
  29084. ' published',
  29085. ' property V1: TRg read FV default -1;',
  29086. ' end;',
  29087. 'begin']);
  29088. ConvertProgram;
  29089. CheckSource('TestRTTI_DefaultValueRangeType',
  29090. LinesToStr([ // statements
  29091. 'this.$rtti.$Int("TRg", {',
  29092. ' minvalue: -1,',
  29093. ' maxvalue: 1,',
  29094. ' ordtype: 0',
  29095. '});',
  29096. 'this.l = -1;',
  29097. 'this.h = 1;',
  29098. 'rtl.createClass(this, "TObject", null, function () {',
  29099. ' this.$init = function () {',
  29100. ' this.FV = 0;',
  29101. ' };',
  29102. ' this.$final = function () {',
  29103. ' };',
  29104. ' var $r = this.$rtti;',
  29105. ' $r.addProperty(',
  29106. ' "V1",',
  29107. ' 0,',
  29108. ' $mod.$rtti["TRg"],',
  29109. ' "FV",',
  29110. ' "",',
  29111. ' {',
  29112. ' Default: -1',
  29113. ' }',
  29114. ' );',
  29115. '});',
  29116. '']),
  29117. LinesToStr([ // $mod.$main
  29118. '']));
  29119. end;
  29120. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29121. begin
  29122. WithTypeInfo:=true;
  29123. StartProgram(false);
  29124. Add([
  29125. 'type',
  29126. ' TObject = class',
  29127. ' FA, FB: byte;',
  29128. ' property A: byte read FA default 1;',
  29129. ' property B: byte read FB default 2;',
  29130. ' end;',
  29131. ' TBird = class',
  29132. ' published',
  29133. ' property A;',
  29134. ' property B nodefault;',
  29135. ' end;',
  29136. 'begin']);
  29137. ConvertProgram;
  29138. CheckSource('TestRTTI_DefaultValueInherit',
  29139. LinesToStr([ // statements
  29140. 'rtl.createClass(this, "TObject", null, function () {',
  29141. ' this.$init = function () {',
  29142. ' this.FA = 0;',
  29143. ' this.FB = 0;',
  29144. ' };',
  29145. ' this.$final = function () {',
  29146. ' };',
  29147. '});',
  29148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29149. ' var $r = this.$rtti;',
  29150. ' $r.addProperty(',
  29151. ' "A",',
  29152. ' 0,',
  29153. ' rtl.byte,',
  29154. ' "FA",',
  29155. ' "",',
  29156. ' {',
  29157. ' Default: 1',
  29158. ' }',
  29159. ' );',
  29160. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29161. '});',
  29162. '']),
  29163. LinesToStr([ // $mod.$main
  29164. '']));
  29165. end;
  29166. procedure TTestModule.TestRTTI_OverrideMethod;
  29167. begin
  29168. WithTypeInfo:=true;
  29169. StartProgram(false);
  29170. Add('type');
  29171. Add(' TObject = class');
  29172. Add(' published');
  29173. Add(' procedure DoIt; virtual; abstract;');
  29174. Add(' end;');
  29175. Add(' TSky = class');
  29176. Add(' published');
  29177. Add(' procedure DoIt; override;');
  29178. Add(' end;');
  29179. Add('procedure TSky.DoIt; begin end;');
  29180. Add('begin');
  29181. ConvertProgram;
  29182. CheckSource('TestRTTI_OverrideMethod',
  29183. LinesToStr([ // statements
  29184. 'rtl.createClass(this, "TObject", null, function () {',
  29185. ' this.$init = function () {',
  29186. ' };',
  29187. ' this.$final = function () {',
  29188. ' };',
  29189. ' var $r = this.$rtti;',
  29190. ' $r.addMethod("DoIt", 0, null);',
  29191. '});',
  29192. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29193. ' this.DoIt = function () {',
  29194. ' };',
  29195. '});',
  29196. '']),
  29197. LinesToStr([ // $mod.$main
  29198. '']));
  29199. end;
  29200. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29201. begin
  29202. WithTypeInfo:=true;
  29203. StartProgram(false);
  29204. Add([
  29205. 'type',
  29206. ' TObject = class',
  29207. ' published',
  29208. ' procedure DoIt;',
  29209. ' end;',
  29210. ' TSky = class',
  29211. ' published',
  29212. ' procedure DoIt; reintroduce;',
  29213. ' end;',
  29214. 'procedure TObject.DoIt; begin end;',
  29215. 'procedure TSky.DoIt;',
  29216. 'begin',
  29217. ' inherited DoIt;',
  29218. 'end;',
  29219. 'begin']);
  29220. ConvertProgram;
  29221. CheckSource('TestRTTI_ReintroduceMethod',
  29222. LinesToStr([ // statements
  29223. 'rtl.createClass(this, "TObject", null, function () {',
  29224. ' this.$init = function () {',
  29225. ' };',
  29226. ' this.$final = function () {',
  29227. ' };',
  29228. ' this.DoIt = function () {',
  29229. ' };',
  29230. ' var $r = this.$rtti;',
  29231. ' $r.addMethod("DoIt", 0, null);',
  29232. '});',
  29233. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29234. ' this.DoIt = function () {',
  29235. ' $mod.TObject.DoIt.call(this);',
  29236. ' };',
  29237. ' var $r = this.$rtti;',
  29238. ' $r.addMethod("DoIt", 0, null);',
  29239. '});',
  29240. '']),
  29241. LinesToStr([ // $mod.$main
  29242. '']));
  29243. end;
  29244. procedure TTestModule.TestRTTI_OverloadProperty;
  29245. begin
  29246. WithTypeInfo:=true;
  29247. StartProgram(false);
  29248. Add('type');
  29249. Add(' TObject = class');
  29250. Add(' protected');
  29251. Add(' FFlag: longint;');
  29252. Add(' published');
  29253. Add(' property Flag: longint read fflag;');
  29254. Add(' end;');
  29255. Add(' TSky = class');
  29256. Add(' published');
  29257. Add(' property FLAG: longint write fflag;');
  29258. Add(' end;');
  29259. Add('begin');
  29260. ConvertProgram;
  29261. CheckSource('TestRTTI_OverrideMethod',
  29262. LinesToStr([ // statements
  29263. 'rtl.createClass(this, "TObject", null, function () {',
  29264. ' this.$init = function () {',
  29265. ' this.FFlag = 0;',
  29266. ' };',
  29267. ' this.$final = function () {',
  29268. ' };',
  29269. ' var $r = this.$rtti;',
  29270. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29271. '});',
  29272. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29273. ' var $r = this.$rtti;',
  29274. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29275. '});',
  29276. '']),
  29277. LinesToStr([ // $mod.$main
  29278. '']));
  29279. end;
  29280. procedure TTestModule.TestRTTI_ClassForward;
  29281. begin
  29282. WithTypeInfo:=true;
  29283. StartProgram(false);
  29284. Add('type');
  29285. Add(' TObject = class end;');
  29286. Add(' tbridge = class;');
  29287. Add(' TProc = function: tbridge;');
  29288. Add(' TOger = class');
  29289. Add(' published');
  29290. Add(' FBridge: tbridge;');
  29291. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29292. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29293. Add(' end;');
  29294. Add(' TBridge = class');
  29295. Add(' FOger: toger;');
  29296. Add(' end;');
  29297. Add('var p: Pointer;');
  29298. Add(' b: tbridge;');
  29299. Add('begin');
  29300. Add(' p:=typeinfo(tbridge);');
  29301. Add(' p:=typeinfo(b);');
  29302. ConvertProgram;
  29303. CheckSource('TestRTTI_ClassForward',
  29304. LinesToStr([ // statements
  29305. 'rtl.createClass(this, "TObject", null, function () {',
  29306. ' this.$init = function () {',
  29307. ' };',
  29308. ' this.$final = function () {',
  29309. ' };',
  29310. '});',
  29311. 'this.$rtti.$Class("TBridge");',
  29312. 'this.$rtti.$ProcVar("TProc", {',
  29313. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29314. '});',
  29315. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29316. ' this.$init = function () {',
  29317. ' $mod.TObject.$init.call(this);',
  29318. ' this.FBridge = null;',
  29319. ' };',
  29320. ' this.$final = function () {',
  29321. ' this.FBridge = undefined;',
  29322. ' $mod.TObject.$final.call(this);',
  29323. ' };',
  29324. ' var $r = this.$rtti;',
  29325. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29326. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29327. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29328. '});',
  29329. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29330. ' this.$init = function () {',
  29331. ' $mod.TObject.$init.call(this);',
  29332. ' this.FOger = null;',
  29333. ' };',
  29334. ' this.$final = function () {',
  29335. ' this.FOger = undefined;',
  29336. ' $mod.TObject.$final.call(this);',
  29337. ' };',
  29338. '});',
  29339. 'this.p = null;',
  29340. 'this.b = null;',
  29341. '']),
  29342. LinesToStr([ // $mod.$main
  29343. '$mod.p = $mod.$rtti["TBridge"];',
  29344. '$mod.p = $mod.b.$rtti;',
  29345. '']));
  29346. end;
  29347. procedure TTestModule.TestRTTI_ClassOf;
  29348. begin
  29349. WithTypeInfo:=true;
  29350. StartProgram(false);
  29351. Add('type');
  29352. Add(' TClass = class of tobject;');
  29353. Add(' TProcA = function: TClass;');
  29354. Add(' TObject = class');
  29355. Add(' published');
  29356. Add(' C: tclass;');
  29357. Add(' end;');
  29358. Add(' tfox = class;');
  29359. Add(' TBird = class end;');
  29360. Add(' TBirds = class of tbird;');
  29361. Add(' TFox = class end;');
  29362. Add(' TFoxes = class of tfox;');
  29363. Add(' TCows = class of TCow;');
  29364. Add(' TCow = class;');
  29365. Add(' TCow = class end;');
  29366. Add('begin');
  29367. ConvertProgram;
  29368. CheckSource('TestRTTI_ClassOf',
  29369. LinesToStr([ // statements
  29370. 'this.$rtti.$Class("TObject");',
  29371. 'this.$rtti.$ClassRef("TClass", {',
  29372. ' instancetype: this.$rtti["TObject"]',
  29373. '});',
  29374. 'this.$rtti.$ProcVar("TProcA", {',
  29375. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29376. '});',
  29377. 'rtl.createClass(this, "TObject", null, function () {',
  29378. ' this.$init = function () {',
  29379. ' this.C = null;',
  29380. ' };',
  29381. ' this.$final = function () {',
  29382. ' this.C = undefined;',
  29383. ' };',
  29384. ' var $r = this.$rtti;',
  29385. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29386. '});',
  29387. 'this.$rtti.$Class("TFox");',
  29388. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29389. '});',
  29390. 'this.$rtti.$ClassRef("TBirds", {',
  29391. ' instancetype: this.$rtti["TBird"]',
  29392. '});',
  29393. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29394. '});',
  29395. 'this.$rtti.$ClassRef("TFoxes", {',
  29396. ' instancetype: this.$rtti["TFox"]',
  29397. '});',
  29398. 'this.$rtti.$Class("TCow");',
  29399. 'this.$rtti.$ClassRef("TCows", {',
  29400. ' instancetype: this.$rtti["TCow"]',
  29401. '});',
  29402. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29403. '});',
  29404. '']),
  29405. LinesToStr([ // $mod.$main
  29406. '']));
  29407. end;
  29408. procedure TTestModule.TestRTTI_Record;
  29409. begin
  29410. WithTypeInfo:=true;
  29411. StartProgram(false);
  29412. Add('type');
  29413. Add(' integer = longint;');
  29414. Add(' TPoint = record');
  29415. Add(' x,y: integer;');
  29416. Add(' end;');
  29417. Add('var p: pointer;');
  29418. Add(' r: tpoint;');
  29419. Add('begin');
  29420. Add(' p:=typeinfo(tpoint);');
  29421. Add(' p:=typeinfo(r);');
  29422. Add(' p:=typeinfo(r.x);');
  29423. ConvertProgram;
  29424. CheckSource('TestRTTI_Record',
  29425. LinesToStr([ // statements
  29426. 'rtl.recNewT(this, "TPoint", function () {',
  29427. ' this.x = 0;',
  29428. ' this.y = 0;',
  29429. ' this.$eq = function (b) {',
  29430. ' return (this.x === b.x) && (this.y === b.y);',
  29431. ' };',
  29432. ' this.$assign = function (s) {',
  29433. ' this.x = s.x;',
  29434. ' this.y = s.y;',
  29435. ' return this;',
  29436. ' };',
  29437. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29438. ' $r.addField("x", rtl.longint);',
  29439. ' $r.addField("y", rtl.longint);',
  29440. '});',
  29441. 'this.p = null;',
  29442. 'this.r = this.TPoint.$new();',
  29443. '']),
  29444. LinesToStr([ // $mod.$main
  29445. '$mod.p = $mod.$rtti["TPoint"];',
  29446. '$mod.p = $mod.$rtti["TPoint"];',
  29447. '$mod.p = rtl.longint;',
  29448. '']));
  29449. end;
  29450. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29451. begin
  29452. WithTypeInfo:=true;
  29453. StartProgram(false);
  29454. Add('type');
  29455. Add(' TFloatRec = record');
  29456. Add(' c,d: array of char;');
  29457. // Add(' i: array of array of longint;');
  29458. Add(' end;');
  29459. Add('var p: pointer;');
  29460. Add(' r: tfloatrec;');
  29461. Add('begin');
  29462. Add(' p:=typeinfo(tfloatrec);');
  29463. Add(' p:=typeinfo(r);');
  29464. Add(' p:=typeinfo(r.d);');
  29465. ConvertProgram;
  29466. CheckSource('TestRTTI_Record',
  29467. LinesToStr([ // statements
  29468. 'rtl.recNewT(this, "TFloatRec", function () {',
  29469. ' this.$new = function () {',
  29470. ' var r = Object.create(this);',
  29471. ' r.c = [];',
  29472. ' r.d = [];',
  29473. ' return r;',
  29474. ' };',
  29475. ' this.$eq = function (b) {',
  29476. ' return (this.c === b.c) && (this.d === b.d);',
  29477. ' };',
  29478. ' this.$assign = function (s) {',
  29479. ' this.c = rtl.arrayRef(s.c);',
  29480. ' this.d = rtl.arrayRef(s.d);',
  29481. ' return this;',
  29482. ' };',
  29483. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29484. ' eltype: rtl.char',
  29485. ' });',
  29486. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29487. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29488. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29489. '});',
  29490. 'this.p = null;',
  29491. 'this.r = this.TFloatRec.$new();',
  29492. '']),
  29493. LinesToStr([ // $mod.$main
  29494. '$mod.p = $mod.$rtti["TFloatRec"];',
  29495. '$mod.p = $mod.$rtti["TFloatRec"];',
  29496. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29497. '']));
  29498. end;
  29499. procedure TTestModule.TestRTTI_LocalTypes;
  29500. begin
  29501. WithTypeInfo:=true;
  29502. StartProgram(false);
  29503. Add([
  29504. 'procedure DoIt;',
  29505. 'type',
  29506. ' integer = longint;',
  29507. ' TPoint = record',
  29508. ' x,y: integer;',
  29509. ' end;',
  29510. 'var p: TPoint;',
  29511. 'begin',
  29512. 'end;',
  29513. 'begin']);
  29514. ConvertProgram;
  29515. CheckSource('TestRTTI_LocalTypes',
  29516. LinesToStr([ // statements
  29517. 'var TPoint = rtl.recNewT(null, "", function () {',
  29518. ' this.x = 0;',
  29519. ' this.y = 0;',
  29520. ' this.$eq = function (b) {',
  29521. ' return (this.x === b.x) && (this.y === b.y);',
  29522. ' };',
  29523. ' this.$assign = function (s) {',
  29524. ' this.x = s.x;',
  29525. ' this.y = s.y;',
  29526. ' return this;',
  29527. ' };',
  29528. '});',
  29529. 'this.DoIt = function () {',
  29530. ' var p = TPoint.$new();',
  29531. '};',
  29532. '']),
  29533. LinesToStr([ // $mod.$main
  29534. '']));
  29535. end;
  29536. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29537. begin
  29538. WithTypeInfo:=true;
  29539. StartProgram(false);
  29540. Add([
  29541. 'type',
  29542. ' TCaption = string;',
  29543. ' TYesNo = boolean;',
  29544. ' TLetter = char;',
  29545. ' TFloat = double;',
  29546. ' TPtr = pointer;',
  29547. ' TShortInt = shortint;',
  29548. ' TByte = byte;',
  29549. ' TSmallInt = smallint;',
  29550. ' TWord = word;',
  29551. ' TInt32 = longint;',
  29552. ' TDWord = longword;',
  29553. ' TValue = jsvalue;',
  29554. 'var p: TPtr;',
  29555. 'begin',
  29556. ' p:=typeinfo(string);',
  29557. ' p:=typeinfo(tcaption);',
  29558. ' p:=typeinfo(boolean);',
  29559. ' p:=typeinfo(tyesno);',
  29560. ' p:=typeinfo(char);',
  29561. ' p:=typeinfo(tletter);',
  29562. ' p:=typeinfo(double);',
  29563. ' p:=typeinfo(tfloat);',
  29564. ' p:=typeinfo(pointer);',
  29565. ' p:=typeinfo(tptr);',
  29566. ' p:=typeinfo(shortint);',
  29567. ' p:=typeinfo(tshortint);',
  29568. ' p:=typeinfo(byte);',
  29569. ' p:=typeinfo(tbyte);',
  29570. ' p:=typeinfo(smallint);',
  29571. ' p:=typeinfo(tsmallint);',
  29572. ' p:=typeinfo(word);',
  29573. ' p:=typeinfo(tword);',
  29574. ' p:=typeinfo(longword);',
  29575. ' p:=typeinfo(tdword);',
  29576. ' p:=typeinfo(jsvalue);',
  29577. ' p:=typeinfo(tvalue);',
  29578. '']);
  29579. ConvertProgram;
  29580. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29581. LinesToStr([ // statements
  29582. 'this.p = null;',
  29583. '']),
  29584. LinesToStr([ // $mod.$main
  29585. '$mod.p = rtl.string;',
  29586. '$mod.p = rtl.string;',
  29587. '$mod.p = rtl.boolean;',
  29588. '$mod.p = rtl.boolean;',
  29589. '$mod.p = rtl.char;',
  29590. '$mod.p = rtl.char;',
  29591. '$mod.p = rtl.double;',
  29592. '$mod.p = rtl.double;',
  29593. '$mod.p = rtl.pointer;',
  29594. '$mod.p = rtl.pointer;',
  29595. '$mod.p = rtl.shortint;',
  29596. '$mod.p = rtl.shortint;',
  29597. '$mod.p = rtl.byte;',
  29598. '$mod.p = rtl.byte;',
  29599. '$mod.p = rtl.smallint;',
  29600. '$mod.p = rtl.smallint;',
  29601. '$mod.p = rtl.word;',
  29602. '$mod.p = rtl.word;',
  29603. '$mod.p = rtl.longword;',
  29604. '$mod.p = rtl.longword;',
  29605. '$mod.p = rtl.jsvalue;',
  29606. '$mod.p = rtl.jsvalue;',
  29607. '']));
  29608. end;
  29609. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29610. begin
  29611. WithTypeInfo:=true;
  29612. StartProgram(false);
  29613. Add([
  29614. 'type',
  29615. ' TCaption = type string;',
  29616. ' TYesNo = type boolean;',
  29617. ' TLetter = type char;',
  29618. ' TFloat = type double;',
  29619. ' TPtr = type pointer;',
  29620. ' TShortInt = type shortint;',
  29621. ' TByte = type byte;',
  29622. ' TSmallInt = type smallint;',
  29623. ' TWord = type word;',
  29624. ' TInt32 = type longint;',
  29625. ' TDWord = type longword;',
  29626. ' TValue = type jsvalue;',
  29627. ' TAliasValue = type TValue;',
  29628. 'var',
  29629. ' p: TPtr;',
  29630. ' a: TAliasValue;',
  29631. 'begin',
  29632. ' p:=typeinfo(tcaption);',
  29633. ' p:=typeinfo(tyesno);',
  29634. ' p:=typeinfo(tletter);',
  29635. ' p:=typeinfo(tfloat);',
  29636. ' p:=typeinfo(tptr);',
  29637. ' p:=typeinfo(tshortint);',
  29638. ' p:=typeinfo(tbyte);',
  29639. ' p:=typeinfo(tsmallint);',
  29640. ' p:=typeinfo(tword);',
  29641. ' p:=typeinfo(tdword);',
  29642. ' p:=typeinfo(tvalue);',
  29643. ' p:=typeinfo(taliasvalue);',
  29644. ' p:=typeinfo(a);',
  29645. '']);
  29646. ConvertProgram;
  29647. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29648. LinesToStr([ // statements
  29649. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  29650. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29651. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  29652. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29653. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29654. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29655. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  29656. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29657. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  29658. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  29659. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  29660. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29661. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  29662. 'this.p = null;',
  29663. 'this.a = undefined;',
  29664. '']),
  29665. LinesToStr([ // $mod.$main
  29666. '$mod.p = $mod.$rtti["TCaption"];',
  29667. '$mod.p = $mod.$rtti["TYesNo"];',
  29668. '$mod.p = $mod.$rtti["TLetter"];',
  29669. '$mod.p = $mod.$rtti["TFloat"];',
  29670. '$mod.p = $mod.$rtti["TPtr"];',
  29671. '$mod.p = $mod.$rtti["TShortInt"];',
  29672. '$mod.p = $mod.$rtti["TByte"];',
  29673. '$mod.p = $mod.$rtti["TSmallInt"];',
  29674. '$mod.p = $mod.$rtti["TWord"];',
  29675. '$mod.p = $mod.$rtti["TDWord"];',
  29676. '$mod.p = $mod.$rtti["TValue"];',
  29677. '$mod.p = $mod.$rtti["TAliasValue"];',
  29678. '$mod.p = $mod.$rtti["TAliasValue"];',
  29679. '']));
  29680. end;
  29681. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29682. begin
  29683. WithTypeInfo:=true;
  29684. StartProgram(false);
  29685. Add('procedure DoIt;');
  29686. Add('type');
  29687. Add(' integer = longint;');
  29688. Add(' TPoint = record');
  29689. Add(' x,y: integer;');
  29690. Add(' end;');
  29691. Add('var p: pointer;');
  29692. Add('begin');
  29693. Add(' p:=typeinfo(tpoint);');
  29694. Add('end;');
  29695. Add('begin');
  29696. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29697. ConvertProgram;
  29698. end;
  29699. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29700. begin
  29701. WithTypeInfo:=true;
  29702. StartProgram(true,[supTypeInfo]);
  29703. Add([
  29704. '{$modeswitch externalclass}',
  29705. 'type',
  29706. ' TFlag = (up,down);',
  29707. ' TFlags = set of TFlag;',
  29708. 'var',
  29709. ' ti: TTypeInfo;',
  29710. ' tiInt: TTypeInfoInteger;',
  29711. ' tiEnum: TTypeInfoEnum;',
  29712. ' tiSet: TTypeInfoSet;',
  29713. 'begin',
  29714. ' ti:=typeinfo(string);',
  29715. ' ti:=typeinfo(boolean);',
  29716. ' ti:=typeinfo(char);',
  29717. ' ti:=typeinfo(double);',
  29718. ' tiInt:=typeinfo(shortint);',
  29719. ' tiInt:=typeinfo(byte);',
  29720. ' tiInt:=typeinfo(smallint);',
  29721. ' tiInt:=typeinfo(word);',
  29722. ' tiInt:=typeinfo(longint);',
  29723. ' tiInt:=typeinfo(longword);',
  29724. ' ti:=typeinfo(jsvalue);',
  29725. ' tiEnum:=typeinfo(tflag);',
  29726. ' tiSet:=typeinfo(tflags);']);
  29727. ConvertProgram;
  29728. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29729. LinesToStr([ // statements
  29730. 'this.TFlag = {',
  29731. ' "0": "up",',
  29732. ' up: 0,',
  29733. ' "1": "down",',
  29734. ' down: 1',
  29735. '};',
  29736. 'this.$rtti.$Enum("TFlag", {',
  29737. ' minvalue: 0,',
  29738. ' maxvalue: 1,',
  29739. ' ordtype: 1,',
  29740. ' enumtype: this.TFlag',
  29741. '});',
  29742. 'this.$rtti.$Set("TFlags", {',
  29743. ' comptype: this.$rtti["TFlag"]',
  29744. '});',
  29745. 'this.ti = null;',
  29746. 'this.tiInt = null;',
  29747. 'this.tiEnum = null;',
  29748. 'this.tiSet = null;',
  29749. '']),
  29750. LinesToStr([ // $mod.$main
  29751. '$mod.ti = rtl.string;',
  29752. '$mod.ti = rtl.boolean;',
  29753. '$mod.ti = rtl.char;',
  29754. '$mod.ti = rtl.double;',
  29755. '$mod.tiInt = rtl.shortint;',
  29756. '$mod.tiInt = rtl.byte;',
  29757. '$mod.tiInt = rtl.smallint;',
  29758. '$mod.tiInt = rtl.word;',
  29759. '$mod.tiInt = rtl.longint;',
  29760. '$mod.tiInt = rtl.longword;',
  29761. '$mod.ti = rtl.jsvalue;',
  29762. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29763. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29764. '']));
  29765. end;
  29766. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29767. begin
  29768. WithTypeInfo:=true;
  29769. StartProgram(true,[supTypeInfo]);
  29770. Add('{$modeswitch externalclass}');
  29771. Add('type');
  29772. Add(' TStaticArr = array[boolean] of string;');
  29773. Add(' TDynArr = array of string;');
  29774. Add(' TProc = procedure;');
  29775. Add(' TMethod = procedure of object;');
  29776. Add('var');
  29777. Add(' StaticArray: TStaticArr;');
  29778. Add(' tiStaticArray: TTypeInfoStaticArray;');
  29779. Add(' DynArray: TDynArr;');
  29780. Add(' tiDynArray: TTypeInfoDynArray;');
  29781. Add(' ProcVar: TProc;');
  29782. Add(' tiProcVar: TTypeInfoProcVar;');
  29783. Add(' MethodVar: TMethod;');
  29784. Add(' tiMethodVar: TTypeInfoMethodVar;');
  29785. Add('begin');
  29786. Add(' tiStaticArray:=typeinfo(StaticArray);');
  29787. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  29788. Add(' tiDynArray:=typeinfo(DynArray);');
  29789. Add(' tiDynArray:=typeinfo(TDynArr);');
  29790. Add(' tiProcVar:=typeinfo(ProcVar);');
  29791. Add(' tiProcVar:=typeinfo(TProc);');
  29792. Add(' tiMethodVar:=typeinfo(MethodVar);');
  29793. Add(' tiMethodVar:=typeinfo(TMethod);');
  29794. ConvertProgram;
  29795. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  29796. LinesToStr([ // statements
  29797. 'this.$rtti.$StaticArray("TStaticArr", {',
  29798. ' dims: [2],',
  29799. ' eltype: rtl.string',
  29800. '});',
  29801. 'this.$rtti.$DynArray("TDynArr", {',
  29802. ' eltype: rtl.string',
  29803. '});',
  29804. 'this.$rtti.$ProcVar("TProc", {',
  29805. ' procsig: rtl.newTIProcSig(null)',
  29806. '});',
  29807. 'this.$rtti.$MethodVar("TMethod", {',
  29808. ' procsig: rtl.newTIProcSig(null),',
  29809. ' methodkind: 0',
  29810. '});',
  29811. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  29812. 'this.tiStaticArray = null;',
  29813. 'this.DynArray = [];',
  29814. 'this.tiDynArray = null;',
  29815. 'this.ProcVar = null;',
  29816. 'this.tiProcVar = null;',
  29817. 'this.MethodVar = null;',
  29818. 'this.tiMethodVar = null;',
  29819. '']),
  29820. LinesToStr([ // $mod.$main
  29821. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29822. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29823. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29824. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29825. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29826. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29827. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29828. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29829. '']));
  29830. end;
  29831. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  29832. begin
  29833. WithTypeInfo:=true;
  29834. StartProgram(true,[supTypeInfo]);
  29835. Add('{$modeswitch externalclass}');
  29836. Add('type');
  29837. Add(' TRec = record end;');
  29838. // ToDo: ^PRec
  29839. Add(' TObject = class end;');
  29840. Add(' TClass = class of tobject;');
  29841. Add('var');
  29842. Add(' Rec: trec;');
  29843. Add(' tiRecord: ttypeinforecord;');
  29844. Add(' Obj: tobject;');
  29845. Add(' tiClass: ttypeinfoclass;');
  29846. Add(' aClass: tclass;');
  29847. Add(' tiClassRef: ttypeinfoclassref;');
  29848. // ToDo: ^PRec
  29849. Add(' tiPointer: ttypeinfopointer;');
  29850. Add('begin');
  29851. Add(' tirecord:=typeinfo(trec);');
  29852. Add(' tirecord:=typeinfo(trec);');
  29853. Add(' ticlass:=typeinfo(obj);');
  29854. Add(' ticlass:=typeinfo(tobject);');
  29855. Add(' ticlass:=typeinfo(aclass);');
  29856. Add(' ticlassref:=typeinfo(tclass);');
  29857. ConvertProgram;
  29858. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  29859. LinesToStr([ // statements
  29860. 'rtl.recNewT(this, "TRec", function () {',
  29861. ' this.$eq = function (b) {',
  29862. ' return true;',
  29863. ' };',
  29864. ' this.$assign = function (s) {',
  29865. ' return this;',
  29866. ' };',
  29867. ' $mod.$rtti.$Record("TRec", {});',
  29868. '});',
  29869. 'rtl.createClass(this, "TObject", null, function () {',
  29870. ' this.$init = function () {',
  29871. ' };',
  29872. ' this.$final = function () {',
  29873. ' };',
  29874. '});',
  29875. 'this.$rtti.$ClassRef("TClass", {',
  29876. ' instancetype: this.$rtti["TObject"]',
  29877. '});',
  29878. 'this.Rec = this.TRec.$new();',
  29879. 'this.tiRecord = null;',
  29880. 'this.Obj = null;',
  29881. 'this.tiClass = null;',
  29882. 'this.aClass = null;',
  29883. 'this.tiClassRef = null;',
  29884. 'this.tiPointer = null;',
  29885. '']),
  29886. LinesToStr([ // $mod.$main
  29887. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29888. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29889. '$mod.tiClass = $mod.Obj.$rtti;',
  29890. '$mod.tiClass = $mod.$rtti["TObject"];',
  29891. '$mod.tiClass = $mod.aClass.$rtti;',
  29892. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  29893. '']));
  29894. end;
  29895. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  29896. begin
  29897. WithTypeInfo:=true;
  29898. StartProgram(true,[supTypeInfo]);
  29899. Add([
  29900. '{$modeswitch externalclass}',
  29901. 'type',
  29902. ' TClass = class of tobject;',
  29903. ' TObject = class',
  29904. ' function MyClass: TClass;',
  29905. ' class function ClassType: TClass;',
  29906. ' end;',
  29907. 'function TObject.MyClass: TClass;',
  29908. 'var t: TTypeInfoClass;',
  29909. 'begin',
  29910. ' t:=TypeInfo(Self);',
  29911. ' t:=TypeInfo(Result);',
  29912. ' t:=TypeInfo(TObject);',
  29913. 'end;',
  29914. 'class function TObject.ClassType: TClass;',
  29915. 'var t: TTypeInfoClass;',
  29916. 'begin',
  29917. ' t:=TypeInfo(Self);',
  29918. ' t:=TypeInfo(Result);',
  29919. 'end;',
  29920. 'var',
  29921. ' Obj: TObject;',
  29922. ' t: TTypeInfoClass;',
  29923. 'begin',
  29924. ' t:=TypeInfo(TObject.ClassType);',
  29925. ' t:=TypeInfo(Obj.ClassType);',
  29926. ' t:=TypeInfo(Obj.MyClass);',
  29927. '']);
  29928. ConvertProgram;
  29929. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  29930. LinesToStr([ // statements
  29931. 'this.$rtti.$Class("TObject");',
  29932. 'this.$rtti.$ClassRef("TClass", {',
  29933. ' instancetype: this.$rtti["TObject"]',
  29934. '});',
  29935. 'rtl.createClass(this, "TObject", null, function () {',
  29936. ' this.$init = function () {',
  29937. ' };',
  29938. ' this.$final = function () {',
  29939. ' };',
  29940. ' this.MyClass = function () {',
  29941. ' var Result = null;',
  29942. ' var t = null;',
  29943. ' t = this.$rtti;',
  29944. ' t = Result.$rtti;',
  29945. ' t = $mod.$rtti["TObject"];',
  29946. ' return Result;',
  29947. ' };',
  29948. ' this.ClassType = function () {',
  29949. ' var Result = null;',
  29950. ' var t = null;',
  29951. ' t = this.$rtti;',
  29952. ' t = Result.$rtti;',
  29953. ' return Result;',
  29954. ' };',
  29955. '});',
  29956. 'this.Obj = null;',
  29957. 'this.t = null;',
  29958. '']),
  29959. LinesToStr([ // $mod.$main
  29960. '$mod.t = $mod.TObject.ClassType().$rtti;',
  29961. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  29962. '$mod.t = $mod.Obj.MyClass().$rtti;',
  29963. '']));
  29964. end;
  29965. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  29966. begin
  29967. WithTypeInfo:=true;
  29968. AddModuleWithIntfImplSrc('typinfo.pas',
  29969. LinesToStr([
  29970. '{$modeswitch externalclass}',
  29971. 'type',
  29972. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  29973. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  29974. '']),
  29975. '');
  29976. AddModuleWithIntfImplSrc('unit2.pas',
  29977. LinesToStr([
  29978. 'uses typinfo;',
  29979. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  29980. 'procedure DoPtr(p: PTypeInfo);',
  29981. 'procedure DoInfo(t: TTypeInfo);',
  29982. 'procedure DoInt(t: TTypeInfoInteger);',
  29983. '']),
  29984. LinesToStr([
  29985. 'procedure DoPtr(p: PTypeInfo);',
  29986. 'begin end;',
  29987. 'procedure DoInfo(t: TTypeInfo);',
  29988. 'begin end;',
  29989. 'procedure DoInt(t: TTypeInfoInteger);',
  29990. 'begin end;',
  29991. '']));
  29992. StartUnit(true);
  29993. Add([
  29994. 'interface',
  29995. 'uses unit2;', // does not use unit typinfo
  29996. 'implementation',
  29997. 'var',
  29998. ' i: byte;',
  29999. ' p: pointer;',
  30000. ' t: PTypeInfo;',
  30001. 'initialization',
  30002. ' p:=typeinfo(i);',
  30003. ' t:=typeinfo(i);',
  30004. ' if p=t then ;',
  30005. ' if p=typeinfo(i) then ;',
  30006. ' if typeinfo(i)=p then ;',
  30007. ' if t=typeinfo(i) then ;',
  30008. ' if typeinfo(i)=t then ;',
  30009. ' DoPtr(p);',
  30010. ' DoPtr(t);',
  30011. ' DoPtr(typeinfo(i));',
  30012. ' DoInfo(p);',
  30013. ' DoInfo(t);',
  30014. ' DoInfo(typeinfo(i));',
  30015. ' DoInt(typeinfo(i));',
  30016. '']);
  30017. ConvertUnit;
  30018. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30019. LinesToStr([ // statements
  30020. 'var $impl = $mod.$impl;',
  30021. '']),
  30022. LinesToStr([ // this.$init
  30023. '$impl.p = rtl.byte;',
  30024. '$impl.t = rtl.byte;',
  30025. 'if ($impl.p === $impl.t) ;',
  30026. 'if ($impl.p === rtl.byte) ;',
  30027. 'if (rtl.byte === $impl.p) ;',
  30028. 'if ($impl.t === rtl.byte) ;',
  30029. 'if (rtl.byte === $impl.t) ;',
  30030. 'pas.unit2.DoPtr($impl.p);',
  30031. 'pas.unit2.DoPtr($impl.t);',
  30032. 'pas.unit2.DoPtr(rtl.byte);',
  30033. 'pas.unit2.DoInfo($impl.p);',
  30034. 'pas.unit2.DoInfo($impl.t);',
  30035. 'pas.unit2.DoInfo(rtl.byte);',
  30036. 'pas.unit2.DoInt(rtl.byte);',
  30037. '']),
  30038. LinesToStr([ // implementation
  30039. '$impl.i = 0;',
  30040. '$impl.p = null;',
  30041. '$impl.t = null;',
  30042. '']) );
  30043. end;
  30044. procedure TTestModule.TestRTTI_Interface_Corba;
  30045. begin
  30046. WithTypeInfo:=true;
  30047. StartProgram(true,[supTypeInfo]);
  30048. Add([
  30049. '{$interfaces corba}',
  30050. '{$modeswitch externalclass}',
  30051. 'type',
  30052. ' IUnknown = interface',
  30053. ' end;',
  30054. ' IBird = interface',
  30055. ' function GetItem: longint;',
  30056. ' procedure SetItem(Value: longint);',
  30057. ' property Item: longint read GetItem write SetItem;',
  30058. ' end;',
  30059. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30060. 'var',
  30061. ' i: IBird;',
  30062. ' t: TTypeInfoInterface;',
  30063. 'begin',
  30064. ' t:=TypeInfo(IBird);',
  30065. ' t:=TypeInfo(i);',
  30066. ' DoIt(t);',
  30067. ' DoIt(TypeInfo(IBird));',
  30068. '']);
  30069. ConvertProgram;
  30070. CheckSource('TestRTTI_Interface_Corba',
  30071. LinesToStr([ // statements
  30072. 'rtl.createInterface(',
  30073. ' this,',
  30074. ' "IUnknown",',
  30075. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30076. ' [],',
  30077. ' null,',
  30078. ' function () {',
  30079. ' }',
  30080. ');',
  30081. 'rtl.createInterface(',
  30082. ' this,',
  30083. ' "IBird",',
  30084. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30085. ' ["GetItem", "SetItem"],',
  30086. ' null,',
  30087. ' function () {',
  30088. ' var $r = this.$rtti;',
  30089. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30090. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30091. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30092. ' }',
  30093. ');',
  30094. 'this.DoIt = function (t) {',
  30095. '}; ',
  30096. 'this.i = null;',
  30097. 'this.t = null;',
  30098. '']),
  30099. LinesToStr([ // $mod.$main
  30100. '$mod.t = $mod.$rtti["IBird"];',
  30101. '$mod.t = $mod.i.$rtti;',
  30102. '$mod.DoIt($mod.t);',
  30103. '$mod.DoIt($mod.$rtti["IBird"]);',
  30104. '']));
  30105. end;
  30106. procedure TTestModule.TestRTTI_Interface_COM;
  30107. begin
  30108. WithTypeInfo:=true;
  30109. StartProgram(true,[supTypeInfo]);
  30110. Add([
  30111. '{$interfaces com}',
  30112. '{$modeswitch externalclass}',
  30113. 'type',
  30114. ' TGuid = record end;',
  30115. ' integer = longint;',
  30116. ' IUnknown = interface',
  30117. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30118. ' function _AddRef: Integer;',
  30119. ' function _Release: Integer;',
  30120. ' end;',
  30121. ' IBird = interface',
  30122. ' function GetItem: longint;',
  30123. ' procedure SetItem(Value: longint);',
  30124. ' property Item: longint read GetItem write SetItem;',
  30125. ' end;',
  30126. 'var',
  30127. ' i: IBird;',
  30128. ' t: TTypeInfoInterface;',
  30129. 'begin',
  30130. ' t:=TypeInfo(IBird);',
  30131. ' t:=TypeInfo(i);',
  30132. '']);
  30133. ConvertProgram;
  30134. CheckSource('TestRTTI_Interface_COM',
  30135. LinesToStr([ // statements
  30136. 'rtl.recNewT(this, "TGuid", function () {',
  30137. ' this.$eq = function (b) {',
  30138. ' return true;',
  30139. ' };',
  30140. ' this.$assign = function (s) {',
  30141. ' return this;',
  30142. ' };',
  30143. ' $mod.$rtti.$Record("TGuid", {});',
  30144. '});',
  30145. 'rtl.createInterface(',
  30146. ' this,',
  30147. ' "IUnknown",',
  30148. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30149. ' ["QueryInterface", "_AddRef", "_Release"],',
  30150. ' null,',
  30151. ' function () {',
  30152. ' this.$kind = "com";',
  30153. ' var $r = this.$rtti;',
  30154. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30155. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30156. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30157. ' }',
  30158. ');',
  30159. 'rtl.createInterface(',
  30160. ' this,',
  30161. ' "IBird",',
  30162. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30163. ' ["GetItem", "SetItem"],',
  30164. ' this.IUnknown,',
  30165. ' function () {',
  30166. ' var $r = this.$rtti;',
  30167. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30168. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30169. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30170. ' }',
  30171. ');',
  30172. 'this.i = null;',
  30173. 'this.t = null;',
  30174. '']),
  30175. LinesToStr([ // $mod.$main
  30176. '$mod.t = $mod.$rtti["IBird"];',
  30177. '$mod.t = $mod.i.$rtti;',
  30178. '']));
  30179. end;
  30180. procedure TTestModule.TestRTTI_ClassHelper;
  30181. begin
  30182. WithTypeInfo:=true;
  30183. StartProgram(true,[supTypeInfo]);
  30184. Add([
  30185. '{$interfaces com}',
  30186. '{$modeswitch externalclass}',
  30187. 'type',
  30188. ' TObject = class',
  30189. ' end;',
  30190. ' THelper = class helper for TObject',
  30191. ' published',
  30192. ' function GetItem: longint;',
  30193. ' property Item: longint read GetItem;',
  30194. ' end;',
  30195. 'function THelper.GetItem: longint;',
  30196. 'begin',
  30197. 'end;',
  30198. 'var',
  30199. ' t: TTypeInfoHelper;',
  30200. 'begin',
  30201. ' t:=TypeInfo(THelper);',
  30202. '']);
  30203. ConvertProgram;
  30204. CheckSource('TestRTTI_ClassHelper',
  30205. LinesToStr([ // statements
  30206. 'rtl.createClass(this, "TObject", null, function () {',
  30207. ' this.$init = function () {',
  30208. ' };',
  30209. ' this.$final = function () {',
  30210. ' };',
  30211. '});',
  30212. 'rtl.createHelper(this, "THelper", null, function () {',
  30213. ' this.GetItem = function () {',
  30214. ' var Result = 0;',
  30215. ' return Result;',
  30216. ' };',
  30217. ' var $r = this.$rtti;',
  30218. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30219. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30220. '});',
  30221. 'this.t = null;',
  30222. '']),
  30223. LinesToStr([ // $mod.$main
  30224. '$mod.t = $mod.$rtti["THelper"];',
  30225. '']));
  30226. end;
  30227. procedure TTestModule.TestRTTI_ExternalClass;
  30228. begin
  30229. WithTypeInfo:=true;
  30230. StartProgram(true,[supTypeInfo]);
  30231. Add([
  30232. '{$modeswitch externalclass}',
  30233. 'type',
  30234. ' TJSObject = class external name ''Object''',
  30235. ' end;',
  30236. ' TJSArray = class external name ''Array'' (TJSObject)',
  30237. ' end;',
  30238. 'var',
  30239. ' p: Pointer;',
  30240. ' tc: TTypeInfoExtClass;',
  30241. 'begin',
  30242. ' p:=typeinfo(TJSArray);']);
  30243. ConvertProgram;
  30244. CheckSource('TestRTTI_ExternalClass',
  30245. LinesToStr([ // statements
  30246. 'this.$rtti.$ExtClass("TJSObject", {',
  30247. ' jsclass: "Object"',
  30248. '});',
  30249. 'this.$rtti.$ExtClass("TJSArray", {',
  30250. ' ancestor: this.$rtti["TJSObject"],',
  30251. ' jsclass: "Array"',
  30252. '});',
  30253. 'this.p = null;',
  30254. 'this.tc = null;',
  30255. '']),
  30256. LinesToStr([ // $mod.$main
  30257. '$mod.p = $mod.$rtti["TJSArray"];',
  30258. '']));
  30259. end;
  30260. procedure TTestModule.TestResourcestringProgram;
  30261. begin
  30262. AddModuleWithIntfImplSrc('unit2.pas',
  30263. LinesToStr([
  30264. 'resourcestring Title = ''Nice'';',
  30265. '']),
  30266. '');
  30267. StartProgram(true);
  30268. Add([
  30269. 'uses unit2;',
  30270. 'const Bar = ''bar'';',
  30271. 'resourcestring',
  30272. ' Red = ''red'';',
  30273. ' Foobar = ''fOo''+bar;',
  30274. 'var s: string;',
  30275. ' c: char;',
  30276. 'begin',
  30277. ' s:=red;',
  30278. ' s:=test1.red;',
  30279. ' s:=Title;',
  30280. ' c:=red[1];',
  30281. ' c:=test1.red[2];',
  30282. ' if red=foobar then ;',
  30283. ' if red[3]=red[4] then ;']);
  30284. ConvertProgram;
  30285. CheckSource('TestResourcestringProgram',
  30286. LinesToStr([ // statements
  30287. 'this.Bar = "bar";',
  30288. 'this.s = "";',
  30289. 'this.c = "";',
  30290. '$mod.$resourcestrings = {',
  30291. ' Red: {',
  30292. ' org: "red"',
  30293. ' },',
  30294. ' Foobar: {',
  30295. ' org: "fOobar"',
  30296. ' }',
  30297. '};',
  30298. '']),
  30299. LinesToStr([ // $mod.$main
  30300. '$mod.s = rtl.getResStr($mod, "Red");',
  30301. '$mod.s = rtl.getResStr($mod, "Red");',
  30302. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30303. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30304. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30305. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30306. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30307. '']));
  30308. end;
  30309. procedure TTestModule.TestResourcestringUnit;
  30310. begin
  30311. AddModuleWithIntfImplSrc('unit2.pas',
  30312. LinesToStr([
  30313. 'resourcestring Title = ''Nice'';',
  30314. '']),
  30315. '');
  30316. StartUnit(true);
  30317. Add([
  30318. 'interface',
  30319. 'uses unit2;',
  30320. 'const Red = ''rEd'';',
  30321. 'resourcestring',
  30322. ' Blue = ''blue'';',
  30323. ' NotRed = ''not''+Red;',
  30324. 'var s: string;',
  30325. 'implementation',
  30326. 'resourcestring',
  30327. ' ImplGreen = ''green'';',
  30328. 'initialization',
  30329. ' s:=blue+ImplGreen;',
  30330. ' s:=test1.blue+test1.implgreen;',
  30331. ' s:=blue[1]+implgreen[2];',
  30332. ' s:=Title;',
  30333. '']);
  30334. ConvertUnit;
  30335. CheckSource('TestResourcestringUnit',
  30336. LinesToStr([ // statements
  30337. 'this.Red = "rEd";',
  30338. 'this.s = "";',
  30339. '$mod.$resourcestrings = {',
  30340. ' Blue: {',
  30341. ' org: "blue"',
  30342. ' },',
  30343. ' NotRed: {',
  30344. ' org: "notrEd"',
  30345. ' },',
  30346. ' ImplGreen: {',
  30347. ' org: "green"',
  30348. ' }',
  30349. '};',
  30350. '']),
  30351. LinesToStr([ // $mod.$main
  30352. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30353. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30354. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30355. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30356. '']));
  30357. end;
  30358. procedure TTestModule.TestResourcestringImplementation;
  30359. begin
  30360. StartUnit(false);
  30361. Add([
  30362. 'interface',
  30363. 'implementation',
  30364. 'resourcestring',
  30365. ' ImplRed = ''red'';']);
  30366. ConvertUnit;
  30367. CheckSource('TestResourcestringImplementation',
  30368. LinesToStr([ // intf statements
  30369. 'var $impl = $mod.$impl;']),
  30370. LinesToStr([ // $mod.$init
  30371. '']),
  30372. LinesToStr([ // impl statements
  30373. '$mod.$resourcestrings = {',
  30374. ' ImplRed: {',
  30375. ' org: "red"',
  30376. ' }',
  30377. '};',
  30378. '']));
  30379. end;
  30380. procedure TTestModule.TestAttributes_Members;
  30381. begin
  30382. WithTypeInfo:=true;
  30383. StartProgram(false);
  30384. Add([
  30385. '{$modeswitch PrefixedAttributes}',
  30386. 'type',
  30387. ' TObject = class',
  30388. ' constructor Create;',
  30389. ' end;',
  30390. ' TCustomAttribute = class',
  30391. ' constructor Create(Id: word);',
  30392. ' end;',
  30393. ' [Missing]',
  30394. ' TBird = class',
  30395. ' published',
  30396. ' [Tcustom]',
  30397. ' FField: word;',
  30398. ' [tcustom(14)]',
  30399. ' property Size: word read FField;',
  30400. ' [Tcustom(15)]',
  30401. ' procedure Fly; virtual; abstract;',
  30402. ' end;',
  30403. ' TRec = record',
  30404. ' [Tcustom,tcustom(14)]',
  30405. ' Size: word;',
  30406. ' end;',
  30407. 'constructor TObject.Create; begin end;',
  30408. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30409. 'begin',
  30410. '']);
  30411. ConvertProgram;
  30412. CheckSource('TestAttributes_Members',
  30413. LinesToStr([ // statements
  30414. 'rtl.createClass(this, "TObject", null, function () {',
  30415. ' this.$init = function () {',
  30416. ' };',
  30417. ' this.$final = function () {',
  30418. ' };',
  30419. ' this.Create = function () {',
  30420. ' return this;',
  30421. ' };',
  30422. '});',
  30423. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30424. ' this.Create$1 = function (Id) {',
  30425. ' return this;',
  30426. ' };',
  30427. '});',
  30428. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30429. ' this.$init = function () {',
  30430. ' $mod.TObject.$init.call(this);',
  30431. ' this.FField = 0;',
  30432. ' };',
  30433. ' var $r = this.$rtti;',
  30434. ' $r.addField("FField", rtl.word, {',
  30435. ' attr: [$mod.TCustomAttribute, "Create"]',
  30436. ' });',
  30437. ' $r.addProperty(',
  30438. ' "Size",',
  30439. ' 0,',
  30440. ' rtl.word,',
  30441. ' "FField",',
  30442. ' "",',
  30443. ' {',
  30444. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30445. ' }',
  30446. ' );',
  30447. ' $r.addMethod("Fly", 0, null, null, {',
  30448. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30449. ' });',
  30450. '});',
  30451. 'rtl.recNewT(this, "TRec", function () {',
  30452. ' this.Size = 0;',
  30453. ' this.$eq = function (b) {',
  30454. ' return this.Size === b.Size;',
  30455. ' };',
  30456. ' this.$assign = function (s) {',
  30457. ' this.Size = s.Size;',
  30458. ' return this;',
  30459. ' };',
  30460. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30461. ' $r.addField("Size", rtl.word, {',
  30462. ' attr: [',
  30463. ' $mod.TCustomAttribute,',
  30464. ' "Create",',
  30465. ' $mod.TCustomAttribute,',
  30466. ' "Create$1",',
  30467. ' [14]',
  30468. ' ]',
  30469. ' });',
  30470. '});',
  30471. '']),
  30472. LinesToStr([ // $mod.$main
  30473. '']));
  30474. end;
  30475. procedure TTestModule.TestAttributes_Types;
  30476. begin
  30477. WithTypeInfo:=true;
  30478. StartProgram(false);
  30479. Add([
  30480. '{$modeswitch PrefixedAttributes}',
  30481. 'type',
  30482. ' TObject = class',
  30483. ' constructor Create(Id: word);',
  30484. ' end;',
  30485. ' TCustomAttribute = class',
  30486. ' end;',
  30487. ' [TCustom(1)]',
  30488. ' TMyClass = class',
  30489. ' end;',
  30490. ' [TCustom(2)]',
  30491. ' TRec = record',
  30492. ' end;',
  30493. ' [TCustom(3)]',
  30494. ' TInt = type word;',
  30495. 'constructor TObject.Create(Id: word);',
  30496. 'begin',
  30497. 'end;',
  30498. 'var p: pointer;',
  30499. 'begin',
  30500. ' p:=typeinfo(TMyClass);',
  30501. ' p:=typeinfo(TRec);',
  30502. ' p:=typeinfo(TInt);',
  30503. '']);
  30504. ConvertProgram;
  30505. CheckSource('TestAttributes_Types',
  30506. LinesToStr([ // statements
  30507. 'rtl.createClass(this, "TObject", null, function () {',
  30508. ' this.$init = function () {',
  30509. ' };',
  30510. ' this.$final = function () {',
  30511. ' };',
  30512. ' this.Create = function (Id) {',
  30513. ' return this;',
  30514. ' };',
  30515. '});',
  30516. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30517. '});',
  30518. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  30519. ' var $r = this.$rtti;',
  30520. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30521. '});',
  30522. 'rtl.recNewT(this, "TRec", function () {',
  30523. ' this.$eq = function (b) {',
  30524. ' return true;',
  30525. ' };',
  30526. ' this.$assign = function (s) {',
  30527. ' return this;',
  30528. ' };',
  30529. ' $mod.$rtti.$Record("TRec", {',
  30530. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30531. ' });',
  30532. '});',
  30533. 'this.$rtti.$inherited("TInt", rtl.word, {',
  30534. ' attr: [this.TCustomAttribute, "Create", [3]]',
  30535. '});',
  30536. 'this.p = null;',
  30537. '']),
  30538. LinesToStr([ // $mod.$main
  30539. '$mod.p = $mod.$rtti["TMyClass"];',
  30540. '$mod.p = $mod.$rtti["TRec"];',
  30541. '$mod.p = $mod.$rtti["TInt"];',
  30542. '']));
  30543. end;
  30544. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30545. begin
  30546. WithTypeInfo:=true;
  30547. StartProgram(false);
  30548. Add([
  30549. '{$modeswitch PrefixedAttributes}',
  30550. 'type',
  30551. ' TObject = class',
  30552. ' constructor Create;',
  30553. ' end;',
  30554. ' TCustomAttribute = class',
  30555. ' end;',
  30556. ' THelper = class helper for TCustomAttribute',
  30557. ' constructor Create(Id: word);',
  30558. ' end;',
  30559. ' [TCustom(3)]',
  30560. ' TMyInt = word;',
  30561. 'constructor TObject.Create; begin end;',
  30562. 'constructor THelper.Create(Id: word); begin end;',
  30563. 'begin',
  30564. ' if typeinfo(TMyInt)=nil then ;']);
  30565. ConvertProgram;
  30566. end;
  30567. procedure TTestModule.TestAssert;
  30568. begin
  30569. StartProgram(false);
  30570. Add([
  30571. 'procedure DoIt;',
  30572. 'var',
  30573. ' b: boolean;',
  30574. ' s: string;',
  30575. 'begin',
  30576. ' {$Assertions on}',
  30577. ' Assert(b);',
  30578. 'end;',
  30579. 'begin',
  30580. ' DoIt;',
  30581. '']);
  30582. ConvertProgram;
  30583. CheckSource('TestAssert',
  30584. LinesToStr([ // statements
  30585. 'this.DoIt = function () {',
  30586. ' var b = false;',
  30587. ' var s = "";',
  30588. ' if (!b) throw "assert failed";',
  30589. '};',
  30590. '']),
  30591. LinesToStr([ // $mod.$main
  30592. '$mod.DoIt();',
  30593. '']));
  30594. end;
  30595. procedure TTestModule.TestAssert_SysUtils;
  30596. begin
  30597. AddModuleWithIntfImplSrc('SysUtils.pas',
  30598. LinesToStr([
  30599. 'type',
  30600. ' TObject = class',
  30601. ' constructor Create;',
  30602. ' end;',
  30603. ' EAssertionFailed = class',
  30604. ' constructor Create(s: string);',
  30605. ' end;',
  30606. '']),
  30607. LinesToStr([
  30608. 'constructor TObject.Create;',
  30609. 'begin end;',
  30610. 'constructor EAssertionFailed.Create(s: string);',
  30611. 'begin end;',
  30612. '']) );
  30613. StartProgram(true);
  30614. Add([
  30615. 'uses sysutils;',
  30616. 'procedure DoIt;',
  30617. 'var',
  30618. ' b: boolean;',
  30619. ' s: string;',
  30620. 'begin',
  30621. ' {$Assertions on}',
  30622. ' Assert(b);',
  30623. ' Assert(b,''msg'');',
  30624. 'end;',
  30625. 'begin',
  30626. ' DoIt;',
  30627. '']);
  30628. ConvertProgram;
  30629. CheckSource('TestAssert_SysUtils',
  30630. LinesToStr([ // statements
  30631. 'this.DoIt = function () {',
  30632. ' var b = false;',
  30633. ' var s = "";',
  30634. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30635. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30636. '};',
  30637. '']),
  30638. LinesToStr([ // $mod.$main
  30639. '$mod.DoIt();',
  30640. '']));
  30641. end;
  30642. procedure TTestModule.TestObjectChecks;
  30643. begin
  30644. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30645. StartProgram(false);
  30646. Add([
  30647. 'type',
  30648. ' TObject = class',
  30649. ' procedure DoIt;',
  30650. ' end;',
  30651. ' TClass = class of tobject;',
  30652. ' TBird = class',
  30653. ' end;',
  30654. ' TBirdClass = class of TBird;',
  30655. 'var',
  30656. ' o : TObject;',
  30657. ' c: TClass;',
  30658. ' b: TBird;',
  30659. ' bc: TBirdClass;',
  30660. 'procedure TObject.DoIt;',
  30661. 'begin',
  30662. ' b:=TBird(o);',
  30663. 'end;',
  30664. 'begin',
  30665. ' o.DoIt;',
  30666. ' b:=TBird(o);',
  30667. ' bc:=TBirdClass(c);',
  30668. '']);
  30669. ConvertProgram;
  30670. CheckSource('TestCheckMethodCall',
  30671. LinesToStr([ // statements
  30672. 'rtl.createClass(this, "TObject", null, function () {',
  30673. ' this.$init = function () {',
  30674. ' };',
  30675. ' this.$final = function () {',
  30676. ' };',
  30677. ' this.DoIt = function () {',
  30678. ' rtl.checkMethodCall(this,$mod.TObject);',
  30679. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30680. ' };',
  30681. '});',
  30682. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30683. '});',
  30684. 'this.o = null;',
  30685. 'this.c = null;',
  30686. 'this.b = null;',
  30687. 'this.bc = null;',
  30688. '']),
  30689. LinesToStr([ // $mod.$main
  30690. '$mod.o.DoIt();',
  30691. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30692. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30693. '']));
  30694. end;
  30695. procedure TTestModule.TestOverflowChecks_Int;
  30696. begin
  30697. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30698. StartProgram(false);
  30699. Add([
  30700. 'procedure DoIt;',
  30701. 'var',
  30702. ' b: byte;',
  30703. ' n: nativeint;',
  30704. ' u: nativeuint;',
  30705. ' c: currency;',
  30706. 'begin',
  30707. ' n:=n+n;',
  30708. ' n:=n-n;',
  30709. ' n:=n+b;',
  30710. ' n:=b-n;',
  30711. ' n:=n*n;',
  30712. ' n:=n*u;',
  30713. ' c:=c+b;',
  30714. ' c:=b+c;',
  30715. ' c:=c*b;',
  30716. ' c:=b*c;',
  30717. 'end;',
  30718. 'begin',
  30719. '']);
  30720. ConvertProgram;
  30721. CheckSource('TestOverflowChecks_Int',
  30722. LinesToStr([ // statements
  30723. 'this.DoIt = function () {',
  30724. ' var b = 0;',
  30725. ' var n = 0;',
  30726. ' var u = 0;',
  30727. ' var c = 0;',
  30728. ' n = rtl.oc(n + n);',
  30729. ' n = rtl.oc(n - n);',
  30730. ' n = rtl.oc(n + b);',
  30731. ' n = rtl.oc(b - n);',
  30732. ' n = rtl.oc(n * n);',
  30733. ' n = rtl.oc(n * u);',
  30734. ' c = rtl.oc(c + (b * 10000));',
  30735. ' c = rtl.oc((b * 10000) + c);',
  30736. ' c = rtl.oc(c * b);',
  30737. ' c = rtl.oc(b * c);',
  30738. '};',
  30739. '']),
  30740. LinesToStr([ // $mod.$main
  30741. '']));
  30742. end;
  30743. procedure TTestModule.TestRangeChecks_AssignInt;
  30744. begin
  30745. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30746. StartProgram(false);
  30747. Add([
  30748. '{$R+}',
  30749. 'var',
  30750. ' b: byte = 2;',
  30751. ' w: word = 3;',
  30752. 'procedure DoIt(p: byte);',
  30753. 'begin',
  30754. ' b:=w;',
  30755. ' b+=w;',
  30756. ' b:=1;',
  30757. 'end;',
  30758. '{$R-}',
  30759. 'procedure DoSome;',
  30760. 'begin',
  30761. ' DoIt(w);',
  30762. ' b:=w;',
  30763. ' b:=2;',
  30764. 'end;',
  30765. 'begin',
  30766. '{$R+}',
  30767. '']);
  30768. ConvertProgram;
  30769. CheckSource('TestRangeChecks_AssignInt',
  30770. LinesToStr([ // statements
  30771. 'this.b = 2;',
  30772. 'this.w = 3;',
  30773. 'this.DoIt = function (p) {',
  30774. ' rtl.rc(p, 0, 255);',
  30775. ' $mod.b = rtl.rc($mod.w,0,255);',
  30776. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  30777. ' $mod.b = 1;',
  30778. '};',
  30779. 'this.DoSome = function () {',
  30780. ' $mod.DoIt($mod.w);',
  30781. ' $mod.b = $mod.w;',
  30782. ' $mod.b = 2;',
  30783. '};',
  30784. '']),
  30785. LinesToStr([ // $mod.$main
  30786. '']));
  30787. end;
  30788. procedure TTestModule.TestRangeChecks_AssignIntRange;
  30789. begin
  30790. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30791. StartProgram(false);
  30792. Add([
  30793. '{$R+}',
  30794. 'type Ten = 1..10;',
  30795. 'var',
  30796. ' b: Ten = 2;',
  30797. ' w: Ten = 3;',
  30798. 'procedure DoIt(p: Ten);',
  30799. 'begin',
  30800. ' b:=w;',
  30801. ' b+=w;',
  30802. ' b:=1;',
  30803. 'end;',
  30804. '{$R-}',
  30805. 'procedure DoSome;',
  30806. 'begin',
  30807. ' DoIt(w);',
  30808. ' b:=w;',
  30809. ' b:=2;',
  30810. 'end;',
  30811. 'begin',
  30812. '{$R+}',
  30813. '']);
  30814. ConvertProgram;
  30815. CheckSource('TestRangeChecks_AssignIntRange',
  30816. LinesToStr([ // statements
  30817. 'this.b = 2;',
  30818. 'this.w = 3;',
  30819. 'this.DoIt = function (p) {',
  30820. ' rtl.rc(p, 1, 10);',
  30821. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  30822. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  30823. ' $mod.b = 1;',
  30824. '};',
  30825. 'this.DoSome = function () {',
  30826. ' $mod.DoIt($mod.w);',
  30827. ' $mod.b = $mod.w;',
  30828. ' $mod.b = 2;',
  30829. '};',
  30830. '']),
  30831. LinesToStr([ // $mod.$main
  30832. '']));
  30833. end;
  30834. procedure TTestModule.TestRangeChecks_AssignEnum;
  30835. begin
  30836. StartProgram(false);
  30837. Add([
  30838. '{$R+}',
  30839. 'type TEnum = (red,green);',
  30840. 'var',
  30841. ' e: TEnum = red;',
  30842. 'procedure DoIt(p: TEnum);',
  30843. 'begin',
  30844. ' e:=p;',
  30845. ' p:=TEnum(0);',
  30846. ' p:=succ(e);',
  30847. 'end;',
  30848. '{$R-}',
  30849. 'procedure DoSome;',
  30850. 'begin',
  30851. ' DoIt(e);',
  30852. ' e:=TEnum(1);',
  30853. ' e:=pred(e);',
  30854. 'end;',
  30855. 'begin',
  30856. '{$R+}',
  30857. '']);
  30858. ConvertProgram;
  30859. CheckSource('TestRangeChecks_AssignEnum',
  30860. LinesToStr([ // statements
  30861. 'this.TEnum = {',
  30862. ' "0": "red",',
  30863. ' red: 0,',
  30864. ' "1": "green",',
  30865. ' green: 1',
  30866. '};',
  30867. 'this.e = this.TEnum.red;',
  30868. 'this.DoIt = function (p) {',
  30869. ' rtl.rc(p, 0, 1);',
  30870. ' $mod.e = rtl.rc(p, 0, 1);',
  30871. ' p = 0;',
  30872. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30873. '};',
  30874. 'this.DoSome = function () {',
  30875. ' $mod.DoIt($mod.e);',
  30876. ' $mod.e = 1;',
  30877. ' $mod.e = $mod.e - 1;',
  30878. '};',
  30879. '']),
  30880. LinesToStr([ // $mod.$main
  30881. '']));
  30882. end;
  30883. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  30884. begin
  30885. StartProgram(false);
  30886. Add([
  30887. '{$R+}',
  30888. 'type',
  30889. ' TEnum = (red,green);',
  30890. ' TEnumRg = red..green;',
  30891. 'var',
  30892. ' e: TEnumRg = red;',
  30893. 'procedure DoIt(p: TEnumRg);',
  30894. 'begin',
  30895. ' e:=p;',
  30896. ' p:=TEnumRg(0);',
  30897. ' p:=succ(e);',
  30898. 'end;',
  30899. '{$R-}',
  30900. 'procedure DoSome;',
  30901. 'begin',
  30902. ' DoIt(e);',
  30903. ' e:=TEnum(1);',
  30904. ' e:=pred(e);',
  30905. 'end;',
  30906. 'begin',
  30907. '{$R+}',
  30908. '']);
  30909. ConvertProgram;
  30910. CheckSource('TestRangeChecks_AssignEnumRange',
  30911. LinesToStr([ // statements
  30912. 'this.TEnum = {',
  30913. ' "0": "red",',
  30914. ' red: 0,',
  30915. ' "1": "green",',
  30916. ' green: 1',
  30917. '};',
  30918. 'this.e = this.TEnum.red;',
  30919. 'this.DoIt = function (p) {',
  30920. ' rtl.rc(p, 0, 1);',
  30921. ' $mod.e = rtl.rc(p, 0, 1);',
  30922. ' p = 0;',
  30923. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30924. '};',
  30925. 'this.DoSome = function () {',
  30926. ' $mod.DoIt($mod.e);',
  30927. ' $mod.e = 1;',
  30928. ' $mod.e = $mod.e - 1;',
  30929. '};',
  30930. '']),
  30931. LinesToStr([ // $mod.$main
  30932. '']));
  30933. end;
  30934. procedure TTestModule.TestRangeChecks_AssignChar;
  30935. begin
  30936. StartProgram(false);
  30937. Add([
  30938. '{$R+}',
  30939. 'type',
  30940. ' TLetter = char;',
  30941. 'var',
  30942. ' b: TLetter = ''2'';',
  30943. ' w: TLetter = ''3'';',
  30944. 'procedure DoIt(p: TLetter);',
  30945. 'begin',
  30946. ' b:=w;',
  30947. ' b:=''1'';',
  30948. 'end;',
  30949. '{$R-}',
  30950. 'procedure DoSome;',
  30951. 'begin',
  30952. ' DoIt(w);',
  30953. ' b:=w;',
  30954. ' b:=''2'';',
  30955. 'end;',
  30956. 'begin',
  30957. '{$R+}',
  30958. '']);
  30959. ConvertProgram;
  30960. CheckSource('TestRangeChecks_AssignChar',
  30961. LinesToStr([ // statements
  30962. 'this.b = "2";',
  30963. 'this.w = "3";',
  30964. 'this.DoIt = function (p) {',
  30965. ' rtl.rcc(p, 0, 65535);',
  30966. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  30967. ' $mod.b = "1";',
  30968. '};',
  30969. 'this.DoSome = function () {',
  30970. ' $mod.DoIt($mod.w);',
  30971. ' $mod.b = $mod.w;',
  30972. ' $mod.b = "2";',
  30973. '};',
  30974. '']),
  30975. LinesToStr([ // $mod.$main
  30976. '']));
  30977. end;
  30978. procedure TTestModule.TestRangeChecks_AssignCharRange;
  30979. begin
  30980. StartProgram(false);
  30981. Add([
  30982. '{$R+}',
  30983. 'type TDigit = ''0''..''9'';',
  30984. 'var',
  30985. ' b: TDigit = ''2'';',
  30986. ' w: TDigit = ''3'';',
  30987. 'procedure DoIt(p: TDigit);',
  30988. 'begin',
  30989. ' b:=w;',
  30990. ' b:=''1'';',
  30991. 'end;',
  30992. '{$R-}',
  30993. 'procedure DoSome;',
  30994. 'begin',
  30995. ' DoIt(w);',
  30996. ' b:=w;',
  30997. ' b:=''2'';',
  30998. 'end;',
  30999. 'begin',
  31000. '{$R+}',
  31001. '']);
  31002. ConvertProgram;
  31003. CheckSource('TestRangeChecks_AssignCharRange',
  31004. LinesToStr([ // statements
  31005. 'this.b = "2";',
  31006. 'this.w = "3";',
  31007. 'this.DoIt = function (p) {',
  31008. ' rtl.rcc(p, 48, 57);',
  31009. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31010. ' $mod.b = "1";',
  31011. '};',
  31012. 'this.DoSome = function () {',
  31013. ' $mod.DoIt($mod.w);',
  31014. ' $mod.b = $mod.w;',
  31015. ' $mod.b = "2";',
  31016. '};',
  31017. '']),
  31018. LinesToStr([ // $mod.$main
  31019. '']));
  31020. end;
  31021. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31022. begin
  31023. StartProgram(false);
  31024. Add([
  31025. '{$R+}',
  31026. 'type',
  31027. ' Ten = 1..10;',
  31028. ' TArr = array of Ten;',
  31029. ' TArrArr = array of TArr;',
  31030. ' TArrByte = array[byte] of Ten;',
  31031. ' TArrChar = array[''0''..''9''] of Ten;',
  31032. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31033. ' TObject = class',
  31034. ' A: TArr;',
  31035. ' end;',
  31036. 'procedure DoIt;',
  31037. 'var',
  31038. ' Arr: TArr;',
  31039. ' ArrArr: TArrArr;',
  31040. ' ArrByte: TArrByte;',
  31041. ' ArrChar: TArrChar;',
  31042. ' ArrByteChar: TArrByteChar;',
  31043. ' i: Ten;',
  31044. ' c: char;',
  31045. ' o: tobject;',
  31046. 'begin',
  31047. ' i:=Arr[1];',
  31048. ' i:=ArrByteChar[1,''2''];',
  31049. ' Arr[1]:=Arr[1];',
  31050. ' Arr[i]:=Arr[i];',
  31051. ' ArrByte[3]:=ArrByte[3];',
  31052. ' ArrByte[i]:=ArrByte[i];',
  31053. ' ArrChar[''5'']:=ArrChar[''5''];',
  31054. ' ArrChar[c]:=ArrChar[c];',
  31055. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31056. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31057. ' o.a[i]:=o.a[i];',
  31058. 'end;',
  31059. 'begin',
  31060. '']);
  31061. ConvertProgram;
  31062. CheckSource('TestRangeChecks_ArrayIndex',
  31063. LinesToStr([ // statements
  31064. 'rtl.createClass(this, "TObject", null, function () {',
  31065. ' this.$init = function () {',
  31066. ' this.A = [];',
  31067. ' };',
  31068. ' this.$final = function () {',
  31069. ' this.A = undefined;',
  31070. ' };',
  31071. '});',
  31072. 'this.DoIt = function () {',
  31073. ' var Arr = [];',
  31074. ' var ArrArr = [];',
  31075. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31076. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31077. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31078. ' var i = 0;',
  31079. ' var c = "";',
  31080. ' var o = null;',
  31081. ' i = rtl.rc(Arr[1], 1, 10);',
  31082. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31083. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31084. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31085. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31086. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31087. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31088. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31089. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31090. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31091. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31092. '};',
  31093. '']),
  31094. LinesToStr([ // $mod.$main
  31095. '']));
  31096. end;
  31097. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31098. begin
  31099. StartProgram(false);
  31100. Add([
  31101. '{$R+}',
  31102. 'type',
  31103. ' Ten = 1..10;',
  31104. ' TRec = record x: Ten end;',
  31105. ' TArr = array of TRec;',
  31106. ' TArrArr = array of TArr;',
  31107. ' TObject = class',
  31108. ' A: TArr;',
  31109. ' end;',
  31110. 'procedure DoIt;',
  31111. 'var',
  31112. ' Arr: TArr;',
  31113. ' ArrArr: TArrArr;',
  31114. ' i: Ten;',
  31115. ' o: tobject;',
  31116. 'begin',
  31117. ' Arr[1]:=Arr[1];',
  31118. ' Arr[i]:=Arr[i+1];',
  31119. ' o.a[i]:=o.a[i+2];',
  31120. 'end;',
  31121. 'begin',
  31122. '']);
  31123. ConvertProgram;
  31124. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31125. LinesToStr([ // statements
  31126. 'rtl.recNewT(this, "TRec", function () {',
  31127. ' this.x = 0;',
  31128. ' this.$eq = function (b) {',
  31129. ' return this.x === b.x;',
  31130. ' };',
  31131. ' this.$assign = function (s) {',
  31132. ' this.x = s.x;',
  31133. ' return this;',
  31134. ' };',
  31135. '});',
  31136. 'rtl.createClass(this, "TObject", null, function () {',
  31137. ' this.$init = function () {',
  31138. ' this.A = [];',
  31139. ' };',
  31140. ' this.$final = function () {',
  31141. ' this.A = undefined;',
  31142. ' };',
  31143. '});',
  31144. 'this.DoIt = function () {',
  31145. ' var Arr = [];',
  31146. ' var ArrArr = [];',
  31147. ' var i = 0;',
  31148. ' var o = null;',
  31149. ' Arr[1].$assign(Arr[1]);',
  31150. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31151. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31152. '};',
  31153. '']),
  31154. LinesToStr([ // $mod.$main
  31155. '']));
  31156. end;
  31157. procedure TTestModule.TestRangeChecks_StringIndex;
  31158. begin
  31159. StartProgram(false);
  31160. Add([
  31161. 'type',
  31162. ' TObject = class',
  31163. ' S: string;',
  31164. ' end;',
  31165. '{$R+}',
  31166. 'procedure DoIt(var h: string);',
  31167. 'var',
  31168. ' s: string;',
  31169. ' i: longint;',
  31170. ' c: char;',
  31171. ' o: tobject;',
  31172. 'begin',
  31173. ' c:=s[1];',
  31174. ' s[i]:=s[i];',
  31175. ' h[i]:=h[i];',
  31176. ' c:=o.s[i];',
  31177. ' o.s[i]:=c;',
  31178. 'end;',
  31179. 'begin',
  31180. '']);
  31181. ConvertProgram;
  31182. CheckSource('TestRangeChecks_StringIndex',
  31183. LinesToStr([ // statements
  31184. 'rtl.createClass(this, "TObject", null, function () {',
  31185. ' this.$init = function () {',
  31186. ' this.S = "";',
  31187. ' };',
  31188. ' this.$final = function () {',
  31189. ' };',
  31190. '});',
  31191. 'this.DoIt = function (h) {',
  31192. ' var s = "";',
  31193. ' var i = 0;',
  31194. ' var c = "";',
  31195. ' var o = null;',
  31196. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31197. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31198. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31199. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31200. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31201. '};',
  31202. '']),
  31203. LinesToStr([ // $mod.$main
  31204. '']));
  31205. end;
  31206. procedure TTestModule.TestRangeChecks_TypecastInt;
  31207. begin
  31208. StartProgram(false);
  31209. Add([
  31210. '{$R+}',
  31211. 'var',
  31212. ' i: nativeint;',
  31213. ' b: byte;',
  31214. ' sh: shortint;',
  31215. ' w: word;',
  31216. ' sm: smallint;',
  31217. ' lw: longword;',
  31218. ' li: longint;',
  31219. 'begin',
  31220. ' b:=12+byte(i);',
  31221. ' sh:=12+shortint(i);',
  31222. ' w:=12+word(i);',
  31223. ' sm:=12+smallint(i);',
  31224. ' lw:=12+longword(i);',
  31225. ' li:=12+longint(i);',
  31226. '']);
  31227. ConvertProgram;
  31228. CheckSource('TestRangeChecks_TypecastInt',
  31229. LinesToStr([
  31230. 'this.i = 0;',
  31231. 'this.b = 0;',
  31232. 'this.sh = 0;',
  31233. 'this.w = 0;',
  31234. 'this.sm = 0;',
  31235. 'this.lw = 0;',
  31236. 'this.li = 0;',
  31237. '']),
  31238. LinesToStr([
  31239. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31240. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31241. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31242. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31243. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31244. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31245. '']));
  31246. end;
  31247. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31248. begin
  31249. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31250. StartProgram(false);
  31251. Add([
  31252. '{$modeswitch typehelpers}',
  31253. '{$R+}',
  31254. 'type',
  31255. ' TObject = class',
  31256. ' FSize: byte;',
  31257. ' property Size: byte read FSize;',
  31258. ' end;',
  31259. ' THelper = type helper for byte',
  31260. ' procedure SetIt(w: word);',
  31261. ' end;',
  31262. 'procedure THelper.SetIt(w: word);',
  31263. 'begin',
  31264. ' Self:=w;',
  31265. 'end;',
  31266. 'function GetIt: byte;',
  31267. 'begin',
  31268. ' Result.SetIt(2);',
  31269. 'end;',
  31270. 'var',
  31271. ' b: byte = 3;',
  31272. ' o: TObject;',
  31273. 'begin',
  31274. ' b.SetIt(14);',
  31275. ' with b do SetIt(15);',
  31276. ' o.Size.SetIt(16);',
  31277. '']);
  31278. ConvertProgram;
  31279. CheckSource('TestRangeChecks_AssignInt',
  31280. LinesToStr([ // statements
  31281. 'rtl.createClass(this, "TObject", null, function () {',
  31282. ' this.$init = function () {',
  31283. ' this.FSize = 0;',
  31284. ' };',
  31285. ' this.$final = function () {',
  31286. ' };',
  31287. '});',
  31288. 'rtl.createHelper(this, "THelper", null, function () {',
  31289. ' this.SetIt = function (w) {',
  31290. ' rtl.rc(w, 0, 65535);',
  31291. ' this.set(w);',
  31292. ' };',
  31293. '});',
  31294. 'this.GetIt = function () {',
  31295. ' var Result = 0;',
  31296. ' $mod.THelper.SetIt.call({',
  31297. ' get: function () {',
  31298. ' return Result;',
  31299. ' },',
  31300. ' set: function (v) {',
  31301. ' rtl.rc(v, 0, 255);',
  31302. ' Result = v;',
  31303. ' }',
  31304. ' }, 2);',
  31305. ' return Result;',
  31306. '};',
  31307. 'this.b = 3;',
  31308. 'this.o = null;',
  31309. '']),
  31310. LinesToStr([ // $mod.$main
  31311. '$mod.THelper.SetIt.call({',
  31312. ' p: $mod,',
  31313. ' get: function () {',
  31314. ' return this.p.b;',
  31315. ' },',
  31316. ' set: function (v) {',
  31317. ' rtl.rc(v, 0, 255);',
  31318. ' this.p.b = v;',
  31319. ' }',
  31320. '}, 14);',
  31321. 'var $with = $mod.b;',
  31322. '$mod.THelper.SetIt.call({',
  31323. ' get: function () {',
  31324. ' return $with;',
  31325. ' },',
  31326. ' set: function (v) {',
  31327. ' rtl.rc(v, 0, 255);',
  31328. ' $with = v;',
  31329. ' }',
  31330. '}, 15);',
  31331. '$mod.THelper.SetIt.call({',
  31332. ' p: $mod.o,',
  31333. ' get: function () {',
  31334. ' return this.p.FSize;',
  31335. ' },',
  31336. ' set: function (v) {',
  31337. ' rtl.rc(v, 0, 255);',
  31338. ' this.p.FSize = v;',
  31339. ' }',
  31340. '}, 16);',
  31341. '']));
  31342. end;
  31343. procedure TTestModule.TestAsync_Proc;
  31344. begin
  31345. StartProgram(false);
  31346. Add([
  31347. 'procedure Fly(w: word = 1); async; forward;',
  31348. 'procedure Run(w: word = 2); async;',
  31349. 'begin',
  31350. ' Fly(w);',
  31351. ' Fly;',
  31352. ' await(Fly(w));',
  31353. ' await(Fly);',
  31354. 'end;',
  31355. 'procedure Fly(w: word); ',
  31356. 'begin',
  31357. 'end;',
  31358. 'begin',
  31359. ' Run;',
  31360. ' Run(3);',
  31361. '']);
  31362. ConvertProgram;
  31363. CheckSource('TestAsync_Proc',
  31364. LinesToStr([ // statements
  31365. 'this.Run = async function (w) {',
  31366. ' $mod.Fly(w);',
  31367. ' $mod.Fly(1);',
  31368. ' await $mod.Fly(w);',
  31369. ' await $mod.Fly(1);',
  31370. '};',
  31371. 'this.Fly = async function (w) {',
  31372. '};',
  31373. '']),
  31374. LinesToStr([
  31375. '$mod.Run(2);',
  31376. '$mod.Run(3);',
  31377. '']));
  31378. end;
  31379. procedure TTestModule.TestAsync_CallResultIsPromise;
  31380. begin
  31381. StartProgram(false);
  31382. Add([
  31383. '{$modeswitch externalclass}',
  31384. 'type',
  31385. ' TObject = class',
  31386. ' end;',
  31387. ' TJSPromise = class external name ''Promise''',
  31388. ' end;',
  31389. ' TBird = class',
  31390. ' function Fly: word; async; ',
  31391. ' end;',
  31392. 'function TBird.Fly: word; async; ',
  31393. 'begin',
  31394. ' Result:=3;',
  31395. ' Fly:=4+Result;',
  31396. ' if Result=5 then ;',
  31397. ' exit(6);',
  31398. 'end;',
  31399. 'function Run: word; async;',
  31400. 'begin',
  31401. ' Result:=11+Result;',
  31402. ' inc(Result);',
  31403. 'end;',
  31404. 'var',
  31405. ' p: TJSPromise;',
  31406. ' o: TBird;',
  31407. 'begin',
  31408. ' p:=Run;',
  31409. ' p:=Run();',
  31410. ' if Run=p then ;',
  31411. ' if p=Run then ;',
  31412. ' if Run()=p then ;',
  31413. ' if p=Run() then ;',
  31414. ' p:=o.Fly;',
  31415. ' p:=o.Fly();',
  31416. ' if o.Fly=p then ;',
  31417. ' if o.Fly()=p then ;',
  31418. ' with o do begin',
  31419. ' p:=Fly;',
  31420. ' p:=Fly();',
  31421. ' if Fly=p then ;',
  31422. ' if Fly()=p then ;',
  31423. ' end;',
  31424. '']);
  31425. ConvertProgram;
  31426. CheckSource('TestAsync_CallResultIsPromise',
  31427. LinesToStr([ // statements
  31428. 'rtl.createClass(this, "TObject", null, function () {',
  31429. ' this.$init = function () {',
  31430. ' };',
  31431. ' this.$final = function () {',
  31432. ' };',
  31433. '});',
  31434. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31435. ' this.Fly = async function () {',
  31436. ' var Result = 0;',
  31437. ' Result = 3;',
  31438. ' Result = 4 + Result;',
  31439. ' if (Result === 5) ;',
  31440. ' return 6;',
  31441. ' return Result;',
  31442. ' };',
  31443. '});',
  31444. 'this.Run = async function () {',
  31445. ' var Result = 0;',
  31446. ' Result = 11 + Result;',
  31447. ' Result += 1;',
  31448. ' return Result;',
  31449. '};',
  31450. 'this.p = null;',
  31451. 'this.o = null;',
  31452. '']),
  31453. LinesToStr([
  31454. '$mod.p = $mod.Run();',
  31455. '$mod.p = $mod.Run();',
  31456. 'if ($mod.Run() === $mod.p) ;',
  31457. 'if ($mod.p === $mod.Run()) ;',
  31458. 'if ($mod.Run() === $mod.p) ;',
  31459. 'if ($mod.p === $mod.Run()) ;',
  31460. '$mod.p = $mod.o.Fly();',
  31461. '$mod.p = $mod.o.Fly();',
  31462. 'if ($mod.o.Fly() === $mod.p) ;',
  31463. 'if ($mod.o.Fly() === $mod.p) ;',
  31464. 'var $with = $mod.o;',
  31465. '$mod.p = $with.Fly();',
  31466. '$mod.p = $with.Fly();',
  31467. 'if ($with.Fly() === $mod.p) ;',
  31468. 'if ($with.Fly() === $mod.p) ;',
  31469. '']));
  31470. end;
  31471. procedure TTestModule.TestAsync_ConstructorFail;
  31472. begin
  31473. StartProgram(false);
  31474. Add([
  31475. 'type',
  31476. ' TObject = class',
  31477. ' end;',
  31478. ' TBird = class',
  31479. ' constructor Create; async;',
  31480. ' end;',
  31481. 'constructor TBird.Create; async;',
  31482. 'begin',
  31483. 'end;',
  31484. 'begin',
  31485. '']);
  31486. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31487. ConvertProgram;
  31488. end;
  31489. procedure TTestModule.TestAsync_PropertyGetterFail;
  31490. begin
  31491. StartProgram(false);
  31492. Add([
  31493. 'type',
  31494. ' TObject = class',
  31495. ' end;',
  31496. ' TBird = class',
  31497. ' function GetSize: word; async;',
  31498. ' property Size: word read GetSize;',
  31499. ' end;',
  31500. 'function TBird.GetSize: word; async;',
  31501. 'begin',
  31502. 'end;',
  31503. 'begin',
  31504. '']);
  31505. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31506. ConvertProgram;
  31507. end;
  31508. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31509. begin
  31510. StartProgram(false);
  31511. Add([
  31512. 'procedure Run; async;',
  31513. 'begin',
  31514. ' await(word,1);',
  31515. 'end;',
  31516. 'begin',
  31517. '']);
  31518. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31519. ConvertProgram;
  31520. end;
  31521. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31522. begin
  31523. StartProgram(false);
  31524. Add([
  31525. 'procedure Crawl(w: double); ',
  31526. 'begin',
  31527. 'end;',
  31528. 'procedure Run(w: double);',
  31529. 'begin',
  31530. ' await(Crawl(w));',
  31531. 'end;',
  31532. 'begin',
  31533. ' Run(1);']);
  31534. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31535. ConvertProgram;
  31536. end;
  31537. procedure TTestModule.TestAWait_Result;
  31538. begin
  31539. StartProgram(false);
  31540. Add([
  31541. '{$modeswitch externalclass}',
  31542. 'type',
  31543. ' TJSPromise = class external name ''Promise''',
  31544. ' end;',
  31545. 'function Crawl(d: double = 1.3): word; ',
  31546. 'begin',
  31547. 'end;',
  31548. 'function Run(d: double = 1.6): word; async;',
  31549. 'begin',
  31550. ' Result:=await(1);',
  31551. ' Result:=await(Crawl);',
  31552. ' Result:=await(Crawl(4.5));',
  31553. ' Result:=await(Run);',
  31554. ' Result:=await(Run(6.7));',
  31555. 'end;',
  31556. 'begin',
  31557. ' Run(1);']);
  31558. ConvertProgram;
  31559. CheckSource('TestAWait_Result',
  31560. LinesToStr([ // statements
  31561. 'this.Crawl = function (d) {',
  31562. ' var Result = 0;',
  31563. ' return Result;',
  31564. '};',
  31565. 'this.Run = async function (d) {',
  31566. ' var Result = 0;',
  31567. ' Result = await 1;',
  31568. ' Result = await $mod.Crawl(1.3);',
  31569. ' Result = await $mod.Crawl(4.5);',
  31570. ' Result = await $mod.Run(1.6);',
  31571. ' Result = await $mod.Run(6.7);',
  31572. ' return Result;',
  31573. '};',
  31574. '']),
  31575. LinesToStr([
  31576. '$mod.Run(1);'
  31577. ]));
  31578. end;
  31579. procedure TTestModule.TestAWait_ExternalClassPromise;
  31580. begin
  31581. StartProgram(false);
  31582. Add([
  31583. '{$modeswitch externalclass}',
  31584. 'type',
  31585. ' TJSPromise = class external name ''Promise''',
  31586. ' end;',
  31587. 'function Fly(w: word): TJSPromise; async;',
  31588. 'begin',
  31589. 'end;',
  31590. 'function Jump(w: word): word; async;',
  31591. 'begin',
  31592. 'end;',
  31593. 'function Run(d: double): word; async;',
  31594. 'var',
  31595. ' p: TJSPromise;',
  31596. 'begin',
  31597. ' Result:=await(word,p);', // promise needs type
  31598. ' Result:=await(word,Fly(3));', // promise needs type
  31599. ' Result:=await(Jump(4));', // async non promise must omit the type
  31600. 'end;',
  31601. 'begin',
  31602. '']);
  31603. ConvertProgram;
  31604. CheckSource('TestAWait_ExternalClassPromise',
  31605. LinesToStr([ // statements
  31606. 'this.Fly = async function (w) {',
  31607. ' var Result = null;',
  31608. ' return Result;',
  31609. '};',
  31610. 'this.Jump = async function (w) {',
  31611. ' var Result = 0;',
  31612. ' return Result;',
  31613. '};',
  31614. 'this.Run = async function (d) {',
  31615. ' var Result = 0;',
  31616. ' var p = null;',
  31617. ' Result = await p;',
  31618. ' Result = await $mod.Fly(3);',
  31619. ' Result = await $mod.Jump(4);',
  31620. ' return Result;',
  31621. '};',
  31622. '']),
  31623. LinesToStr([
  31624. ]));
  31625. end;
  31626. procedure TTestModule.TestAsync_AnonymousProc;
  31627. begin
  31628. StartProgram(false);
  31629. Add([
  31630. '{$modeswitch externalclass}',
  31631. 'type',
  31632. ' TJSPromise = class external name ''Promise''',
  31633. ' end;',
  31634. '{$mode objfpc}',
  31635. 'type',
  31636. ' TFunc = reference to function(x: double): word; async;',
  31637. 'function Crawl(d: double = 1.3): word; async;',
  31638. 'begin',
  31639. 'end;',
  31640. 'var Func: TFunc;',
  31641. 'begin',
  31642. ' Func:=function(c:double):word async begin',
  31643. ' Result:=await(Crawl(c));',
  31644. ' end;',
  31645. ' Func:=function(c:double):word async assembler asm',
  31646. ' end;',
  31647. ' ']);
  31648. ConvertProgram;
  31649. CheckSource('TestAsync_AnonymousProc',
  31650. LinesToStr([ // statements
  31651. 'this.Crawl = async function (d) {',
  31652. ' var Result = 0;',
  31653. ' return Result;',
  31654. '};',
  31655. 'this.Func = null;',
  31656. '']),
  31657. LinesToStr([
  31658. '$mod.Func = async function (c) {',
  31659. ' var Result = 0;',
  31660. ' Result = await $mod.Crawl(c);',
  31661. ' return Result;',
  31662. '};',
  31663. '$mod.Func = async function (c) {',
  31664. '};',
  31665. '']));
  31666. end;
  31667. procedure TTestModule.TestAsync_ProcType;
  31668. begin
  31669. StartProgram(false);
  31670. Add([
  31671. '{$mode objfpc}',
  31672. 'type',
  31673. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  31674. ' TFunc = function(x: double = 1.1): word; async;',
  31675. ' TProc = procedure(x: longint = 7); async;',
  31676. 'function Crawl(d: double): word; async;',
  31677. 'begin',
  31678. 'end;',
  31679. 'procedure Run(e:longint); async;',
  31680. 'begin',
  31681. 'end;',
  31682. 'var',
  31683. ' RefFunc: TRefFunc;',
  31684. ' Func: TFunc;',
  31685. ' Proc, ProcB: TProc;',
  31686. 'begin',
  31687. ' Func:=@Crawl;',
  31688. ' RefFunc:=@Crawl;',
  31689. ' RefFunc:=function(c:double):word async begin',
  31690. ' Result:=await(RefFunc);',
  31691. ' Result:=await(RefFunc());',
  31692. ' Result:=await(Func);',
  31693. ' Result:=await(Func());',
  31694. ' await(Proc);',
  31695. ' await(Proc());',
  31696. ' await(Proc(13));',
  31697. ' end;',
  31698. ' Proc:=@Run;',
  31699. ' if Proc=ProcB then ;',
  31700. ' ']);
  31701. ConvertProgram;
  31702. CheckSource('TestAsync_ProcType',
  31703. LinesToStr([ // statements
  31704. 'this.Crawl = async function (d) {',
  31705. ' var Result = 0;',
  31706. ' return Result;',
  31707. '};',
  31708. 'this.Run = async function (e) {',
  31709. '};',
  31710. 'this.RefFunc = null;',
  31711. 'this.Func = null;',
  31712. 'this.Proc = null;',
  31713. 'this.ProcB = null;',
  31714. '']),
  31715. LinesToStr([
  31716. '$mod.Func = $mod.Crawl;',
  31717. '$mod.RefFunc = $mod.Crawl;',
  31718. '$mod.RefFunc = async function (c) {',
  31719. ' var Result = 0;',
  31720. ' Result = await $mod.RefFunc(1.3);',
  31721. ' Result = await $mod.RefFunc(1.3);',
  31722. ' Result = await $mod.Func(1.1);',
  31723. ' Result = await $mod.Func(1.1);',
  31724. ' await $mod.Proc(7);',
  31725. ' await $mod.Proc(7);',
  31726. ' await $mod.Proc(13);',
  31727. ' return Result;',
  31728. '};',
  31729. '$mod.Proc = $mod.Run;',
  31730. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  31731. '']));
  31732. end;
  31733. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  31734. begin
  31735. StartProgram(false);
  31736. Add([
  31737. '{$mode objfpc}',
  31738. 'type',
  31739. ' TRefFunc = reference to function(x: double = 1.3): word;',
  31740. 'function Crawl(d: double): word; async;',
  31741. 'begin',
  31742. 'end;',
  31743. 'var',
  31744. ' RefFunc: TRefFunc;',
  31745. 'begin',
  31746. ' RefFunc:=@Crawl;',
  31747. ' ']);
  31748. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  31749. ConvertProgram;
  31750. end;
  31751. procedure TTestModule.TestAsync_Inherited;
  31752. begin
  31753. StartProgram(false);
  31754. Add([
  31755. '{$mode objfpc}',
  31756. '{$modeswitch externalclass}',
  31757. 'type',
  31758. ' TJSPromise = class external name ''Promise''',
  31759. ' end;',
  31760. ' TObject = class',
  31761. ' function Run(w: word = 3): word; async; virtual;',
  31762. ' end;',
  31763. ' TBird = class',
  31764. ' function Run(w: word = 3): word; async; override;',
  31765. ' end;',
  31766. 'function TObject.Run(w: word = 3): word; async;',
  31767. 'begin',
  31768. 'end;',
  31769. 'function TBird.Run(w: word = 3): word; async;',
  31770. 'var p: TJSPromise;',
  31771. 'begin',
  31772. ' p:=inherited;',
  31773. ' p:=inherited Run;',
  31774. ' p:=inherited Run();',
  31775. ' p:=inherited Run(4);',
  31776. ' exit(p);',
  31777. ' exit(inherited);',
  31778. ' exit(inherited Run);',
  31779. ' exit(inherited Run(5));',
  31780. ' exit(6);',
  31781. 'end;',
  31782. 'begin',
  31783. ' ']);
  31784. ConvertProgram;
  31785. CheckSource('TestAsync_Inherited',
  31786. LinesToStr([ // statements
  31787. 'rtl.createClass(this, "TObject", null, function () {',
  31788. ' this.$init = function () {',
  31789. ' };',
  31790. ' this.$final = function () {',
  31791. ' };',
  31792. ' this.Run = async function (w) {',
  31793. ' var Result = 0;',
  31794. ' return Result;',
  31795. ' };',
  31796. '});',
  31797. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31798. ' this.Run = async function (w) {',
  31799. ' var Result = 0;',
  31800. ' var p = null;',
  31801. ' p = $mod.TObject.Run.apply(this, arguments);',
  31802. ' p = $mod.TObject.Run.call(this, 3);',
  31803. ' p = $mod.TObject.Run.call(this, 3);',
  31804. ' p = $mod.TObject.Run.call(this, 4);',
  31805. ' return p;',
  31806. ' return $mod.TObject.Run.apply(this, arguments);',
  31807. ' return $mod.TObject.Run.call(this, 3);',
  31808. ' return $mod.TObject.Run.call(this, 5);',
  31809. ' return 6;',
  31810. ' return Result;',
  31811. ' };',
  31812. '});',
  31813. '']),
  31814. LinesToStr([
  31815. '']));
  31816. end;
  31817. Initialization
  31818. RegisterTests([TTestModule]);
  31819. end.