tcmodules.pas 838 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754
  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 = [coNoTypeInfo];
  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. );
  47. TSystemUnitParts = set of TSystemUnitPart;
  48. { TTestHintMessage }
  49. TTestHintMessage = class
  50. public
  51. Id: int64;
  52. MsgType: TMessageType;
  53. MsgNumber: integer;
  54. Msg: string;
  55. SourcePos: TPasSourcePos;
  56. end;
  57. { TTestPasParser }
  58. TTestPasParser = Class(TPasParser)
  59. end;
  60. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  61. { TTestEnginePasResolver }
  62. TTestEnginePasResolver = class(TPas2JsResolver)
  63. private
  64. FFilename: string;
  65. FModule: TPasModule;
  66. FOnFindUnit: TOnFindUnit;
  67. FParser: TTestPasParser;
  68. FStreamResolver: TStreamResolver;
  69. FScanner: TPas2jsPasScanner;
  70. FSource: string;
  71. public
  72. destructor Destroy; override;
  73. function FindUnit(const AName, InFilename: String; NameExpr,
  74. InFileExpr: TPasExpr): TPasModule; override;
  75. procedure UsedInterfacesFinished(Section: TPasSection); override;
  76. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  77. property Filename: string read FFilename write FFilename;
  78. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  79. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  80. property Parser: TTestPasParser read FParser write FParser;
  81. property Source: string read FSource write FSource;
  82. property Module: TPasModule read FModule;
  83. end;
  84. { TCustomTestModule }
  85. TCustomTestModule = Class(TTestCase)
  86. private
  87. FConverter: TPasToJSConverter;
  88. FEngine: TTestEnginePasResolver;
  89. FExpectedErrorClass: ExceptClass;
  90. FExpectedErrorMsg: string;
  91. FExpectedErrorNumber: integer;
  92. FFilename: string;
  93. FFileResolver: TStreamResolver;
  94. FJSImplementationSrc: TJSSourceElements;
  95. FJSImplementationUses: TJSArrayLiteral;
  96. FJSInitBody: TJSFunctionBody;
  97. FJSImplentationUses: TJSArrayLiteral;
  98. FJSInterfaceUses: TJSArrayLiteral;
  99. FJSModule: TJSSourceElements;
  100. FJSModuleSrc: TJSSourceElements;
  101. FJSSource: TStringList;
  102. FModule: TPasModule;
  103. FJSModuleCallArgs: TJSArguments;
  104. FModules: TObjectList;// list of TTestEnginePasResolver
  105. FParser: TTestPasParser;
  106. FPasProgram: TPasProgram;
  107. FHintMsgs: TObjectList; // list of TTestHintMessage
  108. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  109. FJSRegModuleCall: TJSCallExpression;
  110. FScanner: TPas2jsPasScanner;
  111. FSkipTests: boolean;
  112. FSource: TStringList;
  113. FFirstPasStatement: TPasImplBlock;
  114. {$IFDEF EnablePasTreeGlobalRefCount}
  115. FElementRefCountAtSetup: int64;
  116. {$ENDIF}
  117. function GetMsgCount: integer;
  118. function GetMsgs(Index: integer): TTestHintMessage;
  119. function GetResolverCount: integer;
  120. function GetResolvers(Index: integer): TTestEnginePasResolver;
  121. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  122. procedure OnParserLog(Sender: TObject; const Msg: String);
  123. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  124. procedure OnScannerLog(Sender: TObject; const Msg: String);
  125. protected
  126. procedure SetUp; override;
  127. function CreateConverter: TPasToJSConverter; virtual;
  128. function LoadUnit(const aUnitName: String): TPasModule;
  129. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  130. procedure TearDown; override;
  131. Procedure Add(Line: string); virtual;
  132. Procedure Add(const Lines: array of string);
  133. Procedure StartParsing; virtual;
  134. procedure ParseModuleQueue; virtual;
  135. procedure ParseModule; virtual;
  136. procedure ParseProgram; virtual;
  137. procedure ParseUnit; virtual;
  138. protected
  139. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  140. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  141. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  142. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  143. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  144. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  145. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  146. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  147. procedure ConvertModule; virtual;
  148. procedure ConvertProgram; virtual;
  149. procedure ConvertUnit; virtual;
  150. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  151. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  152. function GetDottedIdentifier(El: TJSElement): string;
  153. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  154. ImplStatements: string = ''); virtual;
  155. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  156. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  157. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  158. Msg: string; Marker: PSrcMarker = nil); virtual;
  159. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  160. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  161. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  162. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  163. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  164. function IsErrorExpected(E: Exception): boolean;
  165. procedure HandleScannerError(E: EScannerError);
  166. procedure HandleParserError(E: EParserError);
  167. procedure HandlePasResolveError(E: EPasResolve);
  168. procedure HandlePas2JSError(E: EPas2JS);
  169. procedure HandleException(E: Exception);
  170. procedure FailException(E: Exception);
  171. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  172. function IndexOfResolver(const Filename: string): integer;
  173. function GetResolver(const Filename: string): TTestEnginePasResolver;
  174. function GetDefaultNamespace: string;
  175. property PasProgram: TPasProgram Read FPasProgram;
  176. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  177. property ResolverCount: integer read GetResolverCount;
  178. property Engine: TTestEnginePasResolver read FEngine;
  179. property Filename: string read FFilename;
  180. Property Module: TPasModule Read FModule;
  181. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  182. property Converter: TPasToJSConverter read FConverter;
  183. property JSSource: TStringList read FJSSource;
  184. property JSModule: TJSSourceElements read FJSModule;
  185. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  186. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  187. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  188. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  189. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  190. property JSInitBody: TJSFunctionBody read FJSInitBody;
  191. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  192. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  193. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  194. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  195. property SkipTests: boolean read FSkipTests write FSkipTests;
  196. public
  197. constructor Create; override;
  198. destructor Destroy; override;
  199. property Source: TStringList read FSource;
  200. property FileResolver: TStreamResolver read FFileResolver;
  201. property Scanner: TPas2jsPasScanner read FScanner;
  202. property Parser: TTestPasParser read FParser;
  203. property MsgCount: integer read GetMsgCount;
  204. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  205. end;
  206. { TTestModule }
  207. TTestModule = class(TCustomTestModule)
  208. Published
  209. Procedure TestReservedWords;
  210. // program/units
  211. Procedure TestEmptyProgram;
  212. Procedure TestEmptyProgramUseStrict;
  213. Procedure TestEmptyUnit;
  214. Procedure TestEmptyUnitUseStrict;
  215. Procedure TestDottedUnitNames;
  216. Procedure TestDottedUnitNameImpl;
  217. Procedure TestDottedUnitExpr;
  218. Procedure Test_ModeFPCFail;
  219. Procedure Test_ModeSwitchCBlocksFail;
  220. Procedure TestUnit_UseSystem;
  221. Procedure TestUnit_Intf1Impl2Intf1;
  222. Procedure TestIncludeVersion;
  223. // vars/const
  224. Procedure TestVarInt;
  225. Procedure TestVarBaseTypes;
  226. Procedure TestBaseTypeSingleFail;
  227. Procedure TestBaseTypeExtendedFail;
  228. Procedure TestConstBaseTypes;
  229. Procedure TestUnitImplVars;
  230. Procedure TestUnitImplConsts;
  231. Procedure TestUnitImplRecord;
  232. Procedure TestRenameJSNameConflict;
  233. Procedure TestLocalConst;
  234. Procedure TestVarExternal;
  235. Procedure TestVarExternalOtherUnit;
  236. Procedure TestVarAbsoluteFail;
  237. Procedure TestConstExternal;
  238. // numbers
  239. Procedure TestDouble;
  240. Procedure TestInteger;
  241. Procedure TestIntegerRange;
  242. Procedure TestIntegerTypecasts;
  243. Procedure TestInteger_BitwiseShrNativeInt;
  244. Procedure TestInteger_BitwiseShlNativeInt;
  245. Procedure TestCurrency;
  246. Procedure TestForBoolDo;
  247. Procedure TestForIntDo;
  248. Procedure TestForIntInDo;
  249. // strings
  250. Procedure TestCharConst;
  251. Procedure TestChar_Compare;
  252. Procedure TestChar_BuiltInProcs;
  253. Procedure TestStringConst;
  254. Procedure TestStringConstSurrogate;
  255. Procedure TestString_Length;
  256. Procedure TestString_Compare;
  257. Procedure TestString_SetLength;
  258. Procedure TestString_CharAt;
  259. Procedure TestStringHMinusFail;
  260. Procedure TestStr;
  261. Procedure TestBaseType_AnsiStringFail;
  262. Procedure TestBaseType_WideStringFail;
  263. Procedure TestBaseType_ShortStringFail;
  264. Procedure TestBaseType_RawByteStringFail;
  265. Procedure TestTypeShortstring_Fail;
  266. Procedure TestCharSet_Custom;
  267. Procedure TestForCharDo;
  268. Procedure TestForCharInDo;
  269. // alias types
  270. Procedure TestAliasTypeRef;
  271. Procedure TestTypeCast_BaseTypes;
  272. Procedure TestTypeCast_AliasBaseTypes;
  273. // functions
  274. Procedure TestEmptyProc;
  275. Procedure TestProcOneParam;
  276. Procedure TestFunctionWithoutParams;
  277. Procedure TestProcedureWithoutParams;
  278. Procedure TestPrgProcVar;
  279. Procedure TestProcTwoArgs;
  280. Procedure TestProc_DefaultValue;
  281. Procedure TestUnitProcVar;
  282. Procedure TestImplProc;
  283. Procedure TestFunctionResult;
  284. Procedure TestNestedProc;
  285. Procedure TestNestedProc_ResultString;
  286. Procedure TestForwardProc;
  287. Procedure TestNestedForwardProc;
  288. Procedure TestAssignFunctionResult;
  289. Procedure TestFunctionResultInCondition;
  290. Procedure TestFunctionResultInForLoop;
  291. Procedure TestFunctionResultInTypeCast;
  292. Procedure TestExit;
  293. Procedure TestBreak;
  294. Procedure TestBreakAsVar;
  295. Procedure TestContinue;
  296. Procedure TestProc_External;
  297. Procedure TestProc_ExternalOtherUnit;
  298. Procedure TestProc_Asm;
  299. Procedure TestProc_Assembler;
  300. Procedure TestProc_VarParam;
  301. Procedure TestProc_VarParamString;
  302. Procedure TestProc_VarParamV;
  303. Procedure TestProc_Overload;
  304. Procedure TestProc_OverloadForward;
  305. Procedure TestProc_OverloadIntfImpl;
  306. Procedure TestProc_OverloadNested;
  307. Procedure TestProc_OverloadNestedForward;
  308. Procedure TestProc_OverloadUnitCycle;
  309. Procedure TestProc_Varargs;
  310. Procedure TestProc_ConstOrder;
  311. Procedure TestProc_DuplicateConst;
  312. Procedure TestProc_LocalVarAbsolute;
  313. Procedure TestProc_LocalVarInit;
  314. Procedure TestProc_ReservedWords;
  315. Procedure TestProc_ConstRefWord;
  316. Procedure TestProc_Async;
  317. Procedure TestProc_AWaitOutsideAsyncFail;
  318. Procedure TestProc_AWait;
  319. Procedure TestProc_AWaitExternalClassPromise;
  320. // anonymous functions
  321. Procedure TestAnonymousProc_Assign_ObjFPC;
  322. Procedure TestAnonymousProc_Assign_Delphi;
  323. Procedure TestAnonymousProc_Arg;
  324. Procedure TestAnonymousProc_Typecast;
  325. Procedure TestAnonymousProc_With;
  326. Procedure TestAnonymousProc_ExceptOn;
  327. Procedure TestAnonymousProc_Nested;
  328. Procedure TestAnonymousProc_NestedAssignResult;
  329. Procedure TestAnonymousProc_Class;
  330. Procedure TestAnonymousProc_ForLoop;
  331. Procedure TestAnonymousProc_Async;
  332. // enums, sets
  333. Procedure TestEnum_Name;
  334. Procedure TestEnum_Number;
  335. Procedure TestEnum_ConstFail;
  336. Procedure TestEnum_Functions;
  337. Procedure TestEnum_AsParams;
  338. Procedure TestEnumRange_Array;
  339. Procedure TestEnum_ForIn;
  340. Procedure TestEnum_ScopedNumber;
  341. Procedure TestEnum_InFunction;
  342. Procedure TestSet_Enum;
  343. Procedure TestSet_Operators;
  344. Procedure TestSet_Operator_In;
  345. Procedure TestSet_Functions;
  346. Procedure TestSet_PassAsArgClone;
  347. Procedure TestSet_AsParams;
  348. Procedure TestSet_Property;
  349. Procedure TestSet_EnumConst;
  350. Procedure TestSet_IntConst;
  351. Procedure TestSet_AnonymousEnumType;
  352. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  353. Procedure TestSet_ConstEnum;
  354. Procedure TestSet_ConstChar;
  355. Procedure TestSet_ConstInt;
  356. Procedure TestSet_InFunction;
  357. Procedure TestSet_ForIn;
  358. // statements
  359. Procedure TestNestBegin;
  360. Procedure TestIncDec;
  361. Procedure TestLoHiFpcMode;
  362. Procedure TestLoHiDelphiMode;
  363. Procedure TestAssignments;
  364. Procedure TestArithmeticOperators1;
  365. Procedure TestLogicalOperators;
  366. Procedure TestBitwiseOperators;
  367. Procedure TestBitwiseOperatorsLongword;
  368. Procedure TestFunctionInt;
  369. Procedure TestFunctionString;
  370. Procedure TestIfThen;
  371. Procedure TestForLoop;
  372. Procedure TestForLoopInsideFunction;
  373. Procedure TestForLoop_ReadVarAfter;
  374. Procedure TestForLoop_Nested;
  375. Procedure TestRepeatUntil;
  376. Procedure TestAsmBlock;
  377. Procedure TestAsmPas_Impl; // ToDo
  378. Procedure TestTryFinally;
  379. Procedure TestTryExcept;
  380. Procedure TestTryExcept_ReservedWords;
  381. Procedure TestIfThenRaiseElse;
  382. Procedure TestCaseOf;
  383. Procedure TestCaseOf_UseSwitch;
  384. Procedure TestCaseOfNoElse;
  385. Procedure TestCaseOfNoElse_UseSwitch;
  386. Procedure TestCaseOfRange;
  387. Procedure TestCaseOfString;
  388. Procedure TestCaseOfChar;
  389. Procedure TestCaseOfExternalClassConst;
  390. Procedure TestDebugger;
  391. // arrays
  392. Procedure TestArray_Dynamic;
  393. Procedure TestArray_Dynamic_Nil;
  394. Procedure TestArray_DynMultiDimensional;
  395. Procedure TestArray_DynamicAssign;
  396. Procedure TestArray_StaticInt;
  397. Procedure TestArray_StaticBool;
  398. Procedure TestArray_StaticChar;
  399. Procedure TestArray_StaticMultiDim;
  400. Procedure TestArray_StaticInFunction;
  401. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  402. Procedure TestArrayOfRecord;
  403. Procedure TestArray_StaticRecord;
  404. Procedure TestArrayOfSet;
  405. Procedure TestArray_DynAsParam;
  406. Procedure TestArray_StaticAsParam;
  407. Procedure TestArrayElement_AsParams;
  408. Procedure TestArrayElementFromFuncResult_AsParams;
  409. Procedure TestArrayEnumTypeRange;
  410. Procedure TestArray_SetLengthOutArg;
  411. Procedure TestArray_SetLengthProperty;
  412. Procedure TestArray_SetLengthMultiDim;
  413. Procedure TestArray_SetLengthDynOfStatic;
  414. Procedure TestArray_OpenArrayOfString;
  415. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  416. Procedure TestArray_ConstRef;
  417. Procedure TestArray_Concat;
  418. Procedure TestArray_Copy;
  419. Procedure TestArray_InsertDelete;
  420. Procedure TestArray_DynArrayConstObjFPC;
  421. Procedure TestArray_DynArrayConstDelphi;
  422. Procedure TestArray_ArrayLitAsParam;
  423. Procedure TestArray_ArrayLitMultiDimAsParam;
  424. Procedure TestArray_ArrayLitStaticAsParam;
  425. Procedure TestArray_ForInArrOfString;
  426. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  427. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  428. Procedure TestArrayOfConst_TVarRec;
  429. Procedure TestArrayOfConst_PassBaseTypes;
  430. Procedure TestArrayOfConst_PassObj;
  431. // record
  432. Procedure TestRecord_Empty;
  433. Procedure TestRecord_Var;
  434. Procedure TestRecord_VarExternal;
  435. Procedure TestRecord_WithDo;
  436. Procedure TestRecord_Assign;
  437. Procedure TestRecord_AsParams;
  438. Procedure TestRecord_ConstRef;
  439. Procedure TestRecordElement_AsParams;
  440. Procedure TestRecordElementFromFuncResult_AsParams;
  441. Procedure TestRecordElementFromWith_AsParams;
  442. Procedure TestRecord_Equal;
  443. Procedure TestRecord_JSValue;
  444. Procedure TestRecord_VariantFail;
  445. Procedure TestRecord_FieldArray;
  446. Procedure TestRecord_Const;
  447. Procedure TestRecord_TypecastFail;
  448. Procedure TestRecord_InFunction;
  449. Procedure TestRecord_AnonymousFail;
  450. // ToDo: RTTI of local record
  451. // ToDo: pcu local record, name clash and rtti
  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_PropertyDefault2;
  487. Procedure TestClass_PropertyOverride;
  488. Procedure TestClass_PropertyIncVisibility;
  489. Procedure TestClass_Assigned;
  490. Procedure TestClass_WithClassDoCreate;
  491. Procedure TestClass_WithClassInstDoProperty;
  492. Procedure TestClass_WithClassInstDoPropertyWithParams;
  493. Procedure TestClass_WithClassInstDoFunc;
  494. Procedure TestClass_TypeCast;
  495. Procedure TestClass_TypeCastUntypedParam;
  496. Procedure TestClass_Overloads;
  497. Procedure TestClass_OverloadsAncestor;
  498. Procedure TestClass_OverloadConstructor;
  499. Procedure TestClass_OverloadDelphiOverride;
  500. Procedure TestClass_ReintroduceVarDelphi;
  501. Procedure TestClass_ReintroducedVar;
  502. Procedure TestClass_RaiseDescendant;
  503. Procedure TestClass_ExternalMethod;
  504. Procedure TestClass_ExternalVirtualNameMismatchFail;
  505. Procedure TestClass_ExternalOverrideFail;
  506. Procedure TestClass_ExternalVar;
  507. Procedure TestClass_Const;
  508. Procedure TestClass_LocalConstDuplicate_Prg;
  509. Procedure TestClass_LocalConstDuplicate_Unit;
  510. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  511. Procedure TestClass_LocalVarSelfFail;
  512. Procedure TestClass_ArgSelfFail;
  513. Procedure TestClass_NestedProcSelf;
  514. Procedure TestClass_NestedProcSelf2;
  515. Procedure TestClass_NestedProcClassSelf;
  516. Procedure TestClass_NestedProcCallInherited;
  517. Procedure TestClass_TObjectFree;
  518. Procedure TestClass_TObjectFree_VarArg;
  519. Procedure TestClass_TObjectFreeNewInstance;
  520. Procedure TestClass_TObjectFreeLowerCase;
  521. Procedure TestClass_TObjectFreeFunctionFail;
  522. Procedure TestClass_TObjectFreePropertyFail;
  523. Procedure TestClass_ForIn;
  524. Procedure TestClass_DispatchMessage;
  525. Procedure TestClass_Message_DuplicateIntFail;
  526. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  527. // class of
  528. Procedure TestClassOf_Create;
  529. Procedure TestClassOf_Call;
  530. Procedure TestClassOf_Assign;
  531. Procedure TestClassOf_Is;
  532. Procedure TestClassOf_Compare;
  533. Procedure TestClassOf_ClassVar;
  534. Procedure TestClassOf_ClassMethod;
  535. Procedure TestClassOf_ClassProperty;
  536. Procedure TestClassOf_ClassMethodSelf;
  537. Procedure TestClassOf_TypeCast;
  538. Procedure TestClassOf_ImplicitFunctionCall;
  539. Procedure TestClassOf_Const;
  540. // nested class
  541. Procedure TestNestedClass_Alias;
  542. Procedure TestNestedClass_Record;
  543. Procedure TestNestedClass_Class;
  544. // external class
  545. Procedure TestExternalClass_Var;
  546. Procedure TestExternalClass_Const;
  547. Procedure TestExternalClass_Dollar;
  548. Procedure TestExternalClass_DuplicateVarFail;
  549. Procedure TestExternalClass_Method;
  550. Procedure TestExternalClass_ClassMethod;
  551. Procedure TestExternalClass_ClassMethodStatic;
  552. Procedure TestExternalClass_FunctionResultInTypeCast;
  553. Procedure TestExternalClass_NonExternalOverride;
  554. Procedure TestExternalClass_OverloadHint;
  555. Procedure TestExternalClass_SameNamePublishedProperty;
  556. Procedure TestExternalClass_Property;
  557. Procedure TestExternalClass_PropertyDate;
  558. Procedure TestExternalClass_ClassProperty;
  559. Procedure TestExternalClass_ClassOf;
  560. Procedure TestExternalClass_ClassOtherUnit;
  561. Procedure TestExternalClass_Is;
  562. Procedure TestExternalClass_As;
  563. Procedure TestExternalClass_DestructorFail;
  564. Procedure TestExternalClass_New;
  565. Procedure TestExternalClass_ClassOf_New;
  566. Procedure TestExternalClass_FuncClassOf_New;
  567. Procedure TestExternalClass_New_PasClassFail;
  568. Procedure TestExternalClass_New_PasClassBracketsFail;
  569. Procedure TestExternalClass_NewExtName;
  570. Procedure TestExternalClass_Constructor;
  571. Procedure TestExternalClass_ConstructorBrackets;
  572. Procedure TestExternalClass_LocalConstSameName;
  573. Procedure TestExternalClass_ReintroduceOverload;
  574. Procedure TestExternalClass_Inherited;
  575. Procedure TestExternalClass_PascalAncestorFail;
  576. Procedure TestExternalClass_NewInstance;
  577. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  578. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  579. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  580. Procedure TestExternalClass_PascalProperty;
  581. Procedure TestExternalClass_TypeCastToRootClass;
  582. Procedure TestExternalClass_TypeCastToJSObject;
  583. Procedure TestExternalClass_TypeCastStringToExternalString;
  584. Procedure TestExternalClass_TypeCastToJSFunction;
  585. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  586. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  587. Procedure TestExternalClass_BracketAccessor;
  588. Procedure TestExternalClass_BracketAccessor_Call;
  589. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  590. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  591. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  592. Procedure TestExternalClass_BracketAccessor_MultiType;
  593. Procedure TestExternalClass_BracketAccessor_Index;
  594. Procedure TestExternalClass_ForInJSObject;
  595. Procedure TestExternalClass_ForInJSArray;
  596. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  597. // class interfaces
  598. Procedure TestClassInterface_Corba;
  599. Procedure TestClassInterface_ProcExternalFail;
  600. Procedure TestClassInterface_Overloads;
  601. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  602. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  603. Procedure TestClassInterface_AncestorImpl;
  604. Procedure TestClassInterface_ImplReintroduce;
  605. Procedure TestClassInterface_MethodResolution;
  606. Procedure TestClassInterface_AncestorMoreInterfaces;
  607. Procedure TestClassInterface_MethodOverride;
  608. Procedure TestClassInterface_Corba_Delegation;
  609. Procedure TestClassInterface_Corba_DelegationStatic;
  610. Procedure TestClassInterface_Corba_Operators;
  611. Procedure TestClassInterface_Corba_Args;
  612. Procedure TestClassInterface_Corba_ForIn;
  613. Procedure TestClassInterface_COM_AssignVar;
  614. Procedure TestClassInterface_COM_AssignArg;
  615. Procedure TestClassInterface_COM_FunctionResult;
  616. Procedure TestClassInterface_COM_InheritedFuncResult;
  617. Procedure TestClassInterface_COM_IsAsTypeCasts;
  618. Procedure TestClassInterface_COM_PassAsArg;
  619. Procedure TestClassInterface_COM_PassToUntypedParam;
  620. Procedure TestClassInterface_COM_FunctionInExpr;
  621. Procedure TestClassInterface_COM_Property;
  622. Procedure TestClassInterface_COM_IntfProperty;
  623. Procedure TestClassInterface_COM_Delegation;
  624. Procedure TestClassInterface_COM_With;
  625. Procedure TestClassInterface_COM_ForIn;
  626. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  627. Procedure TestClassInterface_COM_RecordIntfFail;
  628. Procedure TestClassInterface_COM_UnitInitialization;
  629. Procedure TestClassInterface_GUID;
  630. Procedure TestClassInterface_GUIDProperty;
  631. // helpers
  632. Procedure TestClassHelper_ClassVar;
  633. Procedure TestClassHelper_Method_AccessInstanceFields;
  634. Procedure TestClassHelper_Method_Call;
  635. Procedure TestClassHelper_Method_Nested_Call;
  636. Procedure TestClassHelper_ClassMethod_Call;
  637. Procedure TestClassHelper_ClassOf;
  638. Procedure TestClassHelper_MethodRefObjFPC;
  639. Procedure TestClassHelper_Constructor;
  640. Procedure TestClassHelper_InheritedObjFPC;
  641. Procedure TestClassHelper_Property;
  642. Procedure TestClassHelper_Property_Array;
  643. Procedure TestClassHelper_Property_Array_Default;
  644. Procedure TestClassHelper_Property_Array_DefaultDefault;
  645. Procedure TestClassHelper_ClassProperty;
  646. Procedure TestClassHelper_ClassPropertyStatic;
  647. Procedure TestClassHelper_ClassProperty_Array;
  648. Procedure TestClassHelper_ForIn;
  649. Procedure TestClassHelper_PassProperty;
  650. Procedure TestExtClassHelper_ClassVar;
  651. Procedure TestExtClassHelper_Method_Call;
  652. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  653. Procedure TestRecordHelper_ClassVar;
  654. Procedure TestRecordHelper_Method_Call;
  655. Procedure TestRecordHelper_Constructor;
  656. Procedure TestTypeHelper_ClassVar;
  657. Procedure TestTypeHelper_PassResultElement;
  658. Procedure TestTypeHelper_PassArgs;
  659. Procedure TestTypeHelper_PassVarConst;
  660. Procedure TestTypeHelper_PassFuncResult;
  661. Procedure TestTypeHelper_PassPropertyField;
  662. Procedure TestTypeHelper_PassPropertyGetter;
  663. Procedure TestTypeHelper_PassClassPropertyField;
  664. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  665. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  666. Procedure TestTypeHelper_Property;
  667. Procedure TestTypeHelper_Property_Array;
  668. Procedure TestTypeHelper_ClassProperty;
  669. Procedure TestTypeHelper_ClassProperty_Array;
  670. Procedure TestTypeHelper_ClassMethod;
  671. Procedure TestTypeHelper_ExtClassMethodFail;
  672. Procedure TestTypeHelper_Constructor;
  673. Procedure TestTypeHelper_Word;
  674. Procedure TestTypeHelper_Boolean;
  675. Procedure TestTypeHelper_WordBool;
  676. Procedure TestTypeHelper_Double;
  677. Procedure TestTypeHelper_NativeInt;
  678. Procedure TestTypeHelper_StringChar;
  679. Procedure TestTypeHelper_JSValue;
  680. Procedure TestTypeHelper_Array;
  681. Procedure TestTypeHelper_EnumType;
  682. Procedure TestTypeHelper_SetType;
  683. Procedure TestTypeHelper_InterfaceType;
  684. Procedure TestTypeHelper_NestedSelf;
  685. // proc types
  686. Procedure TestProcType;
  687. Procedure TestProcType_Arg;
  688. Procedure TestProcType_FunctionFPC;
  689. Procedure TestProcType_FunctionDelphi;
  690. Procedure TestProcType_ProcedureDelphi;
  691. Procedure TestProcType_AsParam;
  692. Procedure TestProcType_MethodFPC;
  693. Procedure TestProcType_MethodDelphi;
  694. Procedure TestProcType_PropertyFPC;
  695. Procedure TestProcType_PropertyDelphi;
  696. Procedure TestProcType_WithClassInstDoPropertyFPC;
  697. Procedure TestProcType_Nested;
  698. Procedure TestProcType_NestedOfObject;
  699. Procedure TestProcType_ReferenceToProc;
  700. Procedure TestProcType_ReferenceToMethod;
  701. Procedure TestProcType_Typecast;
  702. Procedure TestProcType_PassProcToUntyped;
  703. Procedure TestProcType_PassProcToArray;
  704. Procedure TestProcType_SafeCallObjFPC;
  705. Procedure TestProcType_SafeCallDelphi;
  706. // pointer
  707. Procedure TestPointer;
  708. Procedure TestPointer_Proc;
  709. Procedure TestPointer_AssignRecordFail;
  710. Procedure TestPointer_AssignStaticArrayFail;
  711. Procedure TestPointer_TypeCastJSValueToPointer;
  712. Procedure TestPointer_NonRecordFail;
  713. Procedure TestPointer_AnonymousArgTypeFail;
  714. Procedure TestPointer_AnonymousVarTypeFail;
  715. Procedure TestPointer_AnonymousResultTypeFail;
  716. Procedure TestPointer_AddrOperatorFail;
  717. Procedure TestPointer_ArrayParamsFail;
  718. Procedure TestPointer_PointerAddFail;
  719. Procedure TestPointer_IncPointerFail;
  720. Procedure TestPointer_Record;
  721. Procedure TestPointer_RecordArg;
  722. // jsvalue
  723. Procedure TestJSValue_AssignToJSValue;
  724. Procedure TestJSValue_TypeCastToBaseType;
  725. Procedure TestJSValue_TypecastToJSValue;
  726. Procedure TestJSValue_Equal;
  727. Procedure TestJSValue_If;
  728. Procedure TestJSValue_Not;
  729. Procedure TestJSValue_Enum;
  730. Procedure TestJSValue_ClassInstance;
  731. Procedure TestJSValue_ClassOf;
  732. Procedure TestJSValue_ArrayOfJSValue;
  733. Procedure TestJSValue_ArrayLit;
  734. Procedure TestJSValue_Params;
  735. Procedure TestJSValue_UntypedParam;
  736. Procedure TestJSValue_FuncResultType;
  737. Procedure TestJSValue_ProcType_Assign;
  738. Procedure TestJSValue_ProcType_Equal;
  739. Procedure TestJSValue_ProcType_Param;
  740. Procedure TestJSValue_AssignToPointerFail;
  741. Procedure TestJSValue_OverloadDouble;
  742. Procedure TestJSValue_OverloadNativeInt;
  743. Procedure TestJSValue_OverloadWord;
  744. Procedure TestJSValue_OverloadString;
  745. Procedure TestJSValue_OverloadChar;
  746. Procedure TestJSValue_OverloadPointer;
  747. Procedure TestJSValue_ForIn;
  748. // RTTI
  749. Procedure TestRTTI_IntRange;
  750. Procedure TestRTTI_Double;
  751. Procedure TestRTTI_ProcType;
  752. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  753. Procedure TestRTTI_EnumAndSetType;
  754. Procedure TestRTTI_EnumRange;
  755. Procedure TestRTTI_AnonymousEnumType;
  756. Procedure TestRTTI_StaticArray;
  757. Procedure TestRTTI_DynArray;
  758. Procedure TestRTTI_ArrayNestedAnonymous;
  759. Procedure TestRTTI_PublishedMethodOverloadFail;
  760. Procedure TestRTTI_PublishedMethodExternalFail;
  761. Procedure TestRTTI_PublishedClassPropertyFail;
  762. Procedure TestRTTI_PublishedClassFieldFail;
  763. Procedure TestRTTI_PublishedFieldExternalFail;
  764. Procedure TestRTTI_Class_Field;
  765. Procedure TestRTTI_Class_Method;
  766. Procedure TestRTTI_Class_MethodArgFlags;
  767. Procedure TestRTTI_Class_Property;
  768. Procedure TestRTTI_Class_PropertyParams;
  769. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  770. Procedure TestRTTI_Class_OmitRTTI;
  771. Procedure TestRTTI_IndexModifier;
  772. Procedure TestRTTI_StoredModifier;
  773. Procedure TestRTTI_DefaultValue;
  774. Procedure TestRTTI_DefaultValueSet;
  775. Procedure TestRTTI_DefaultValueRangeType;
  776. Procedure TestRTTI_DefaultValueInherit;
  777. Procedure TestRTTI_OverrideMethod;
  778. Procedure TestRTTI_ReintroduceMethod;
  779. Procedure TestRTTI_OverloadProperty;
  780. // ToDo: array argument
  781. Procedure TestRTTI_ClassForward;
  782. Procedure TestRTTI_ClassOf;
  783. Procedure TestRTTI_Record;
  784. Procedure TestRTTI_RecordAnonymousArray;
  785. Procedure TestRTTI_LocalTypes;
  786. Procedure TestRTTI_TypeInfo_BaseTypes;
  787. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  788. Procedure TestRTTI_TypeInfo_LocalFail;
  789. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  790. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  791. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  792. Procedure TestRTTI_TypeInfo_FunctionClassType;
  793. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  794. Procedure TestRTTI_Interface_Corba;
  795. Procedure TestRTTI_Interface_COM;
  796. Procedure TestRTTI_ClassHelper;
  797. Procedure TestRTTI_ExternalClass;
  798. // Resourcestring
  799. Procedure TestResourcestringProgram;
  800. Procedure TestResourcestringUnit;
  801. Procedure TestResourcestringImplementation;
  802. // Attributes
  803. Procedure TestAttributes_Members;
  804. Procedure TestAttributes_Types;
  805. Procedure TestAttributes_HelperConstructor_Fail;
  806. // Assertions, checks
  807. procedure TestAssert;
  808. procedure TestAssert_SysUtils;
  809. procedure TestObjectChecks;
  810. procedure TestOverflowChecks_Int;
  811. procedure TestRangeChecks_AssignInt;
  812. procedure TestRangeChecks_AssignIntRange;
  813. procedure TestRangeChecks_AssignEnum;
  814. procedure TestRangeChecks_AssignEnumRange;
  815. procedure TestRangeChecks_AssignChar;
  816. procedure TestRangeChecks_AssignCharRange;
  817. procedure TestRangeChecks_ArrayIndex;
  818. procedure TestRangeChecks_ArrayOfRecIndex;
  819. procedure TestRangeChecks_StringIndex;
  820. procedure TestRangeChecks_TypecastInt;
  821. procedure TestRangeChecks_TypeHelperInt;
  822. end;
  823. function LinesToStr(Args: array of const): string;
  824. function ExtractFileUnitName(aFilename: string): string;
  825. function JSToStr(El: TJSElement): string;
  826. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  827. implementation
  828. function LinesToStr(Args: array of const): string;
  829. var
  830. s: String;
  831. i: Integer;
  832. begin
  833. s:='';
  834. for i:=Low(Args) to High(Args) do
  835. case Args[i].VType of
  836. vtChar: s += Args[i].VChar+LineEnding;
  837. vtString: s += Args[i].VString^+LineEnding;
  838. vtPChar: s += Args[i].VPChar+LineEnding;
  839. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  840. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  841. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  842. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  843. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  844. end;
  845. Result:=s;
  846. end;
  847. function ExtractFileUnitName(aFilename: string): string;
  848. var
  849. p: Integer;
  850. begin
  851. Result:=ExtractFileName(aFilename);
  852. if Result='' then exit;
  853. for p:=length(Result) downto 1 do
  854. case Result[p] of
  855. '/','\': exit;
  856. '.':
  857. begin
  858. Delete(Result,p,length(Result));
  859. exit;
  860. end;
  861. end;
  862. end;
  863. function JSToStr(El: TJSElement): string;
  864. var
  865. aWriter: TBufferWriter;
  866. aJSWriter: TJSWriter;
  867. begin
  868. aJSWriter:=nil;
  869. aWriter:=TBufferWriter.Create(1000);
  870. try
  871. aJSWriter:=TJSWriter.Create(aWriter);
  872. aJSWriter.IndentSize:=2;
  873. aJSWriter.WriteJS(El);
  874. Result:=aWriter.AsString;
  875. finally
  876. aJSWriter.Free;
  877. aWriter.Free;
  878. end;
  879. end;
  880. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  881. // search diff, ignore changes in spaces
  882. const
  883. SpaceChars = [#9,#10,#13,' '];
  884. var
  885. ExpectedP, ActualP: PChar;
  886. function FindLineEnd(p: PChar): PChar;
  887. begin
  888. Result:=p;
  889. while not (Result^ in [#0,#10,#13]) do inc(Result);
  890. end;
  891. function FindLineStart(p, MinP: PChar): PChar;
  892. begin
  893. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  894. Result:=p;
  895. end;
  896. procedure SkipLineEnd(var p: PChar);
  897. begin
  898. if p^ in [#10,#13] then
  899. begin
  900. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  901. inc(p,2)
  902. else
  903. inc(p);
  904. end;
  905. end;
  906. procedure DiffFound;
  907. var
  908. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  909. ExpLine, ActLine: String;
  910. i, LineNo, DiffLineNo: Integer;
  911. begin
  912. writeln('Diff found "',Msg,'". Lines:');
  913. // write correct lines
  914. p:=PChar(Expected);
  915. LineNo:=0;
  916. DiffLineNo:=0;
  917. repeat
  918. StartPos:=p;
  919. while not (p^ in [#0,#10,#13]) do inc(p);
  920. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  921. SkipLineEnd(p);
  922. inc(LineNo);
  923. if (p<=ExpectedP) and (p^<>#0) then
  924. begin
  925. writeln('= ',ExpLine);
  926. end else begin
  927. // diff line
  928. if DiffLineNo=0 then DiffLineNo:=LineNo;
  929. // write actual line
  930. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  931. ActLineEndP:=FindLineEnd(ActualP);
  932. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  933. writeln('- ',ActLine);
  934. // write expected line
  935. writeln('+ ',ExpLine);
  936. // write empty line with pointer ^
  937. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  938. writeln('^');
  939. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  940. CheckSrcDiff:=false;
  941. // write up to three following actual lines to get some context
  942. for i:=1 to 3 do begin
  943. ActLineStartP:=ActLineEndP;
  944. SkipLineEnd(ActLineStartP);
  945. if ActLineStartP^=#0 then break;
  946. ActLineEndP:=FindLineEnd(ActLineStartP);
  947. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  948. writeln('~ ',ActLine);
  949. end;
  950. exit;
  951. end;
  952. until p^=#0;
  953. writeln('DiffFound Actual:-----------------------');
  954. writeln(Actual);
  955. writeln('DiffFound Expected:---------------------');
  956. writeln(Expected);
  957. writeln('DiffFound ------------------------------');
  958. Msg:='diff found, but lines are the same, internal error';
  959. CheckSrcDiff:=false;
  960. end;
  961. var
  962. IsSpaceNeeded: Boolean;
  963. LastChar, Quote: Char;
  964. begin
  965. Result:=true;
  966. Msg:='';
  967. if Expected='' then Expected:=' ';
  968. if Actual='' then Actual:=' ';
  969. ExpectedP:=PChar(Expected);
  970. ActualP:=PChar(Actual);
  971. repeat
  972. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  973. case ExpectedP^ of
  974. #0:
  975. begin
  976. // check that rest of Actual has only spaces
  977. while ActualP^ in SpaceChars do inc(ActualP);
  978. if ActualP^<>#0 then
  979. begin
  980. DiffFound;
  981. exit;
  982. end;
  983. exit(true);
  984. end;
  985. ' ',#9,#10,#13:
  986. begin
  987. // skip space in Expected
  988. IsSpaceNeeded:=false;
  989. if ExpectedP>PChar(Expected) then
  990. LastChar:=ExpectedP[-1]
  991. else
  992. LastChar:=#0;
  993. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  994. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  995. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  996. IsSpaceNeeded:=true;
  997. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  998. begin
  999. DiffFound;
  1000. exit;
  1001. end;
  1002. while ActualP^ in SpaceChars do inc(ActualP);
  1003. end;
  1004. '''','"':
  1005. begin
  1006. while ActualP^ in SpaceChars do inc(ActualP);
  1007. if ExpectedP^<>ActualP^ then
  1008. begin
  1009. DiffFound;
  1010. exit;
  1011. end;
  1012. Quote:=ExpectedP^;
  1013. repeat
  1014. inc(ExpectedP);
  1015. inc(ActualP);
  1016. if ExpectedP^<>ActualP^ then
  1017. begin
  1018. DiffFound;
  1019. exit;
  1020. end;
  1021. if (ExpectedP^ in [#0,#10,#13]) then
  1022. break
  1023. else if (ExpectedP^=Quote) then
  1024. begin
  1025. inc(ExpectedP);
  1026. inc(ActualP);
  1027. break;
  1028. end;
  1029. until false;
  1030. end;
  1031. else
  1032. while ActualP^ in SpaceChars do inc(ActualP);
  1033. if ExpectedP^<>ActualP^ then
  1034. begin
  1035. DiffFound;
  1036. exit;
  1037. end;
  1038. inc(ExpectedP);
  1039. inc(ActualP);
  1040. end;
  1041. until false;
  1042. end;
  1043. { TTestEnginePasResolver }
  1044. destructor TTestEnginePasResolver.Destroy;
  1045. begin
  1046. FreeAndNil(FStreamResolver);
  1047. FreeAndNil(FParser);
  1048. FreeAndNil(FScanner);
  1049. FreeAndNil(FStreamResolver);
  1050. if Module<>nil then
  1051. begin
  1052. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1053. FModule:=nil;
  1054. end;
  1055. inherited Destroy;
  1056. end;
  1057. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1058. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1059. begin
  1060. Result:=nil;
  1061. if InFilename<>'' then
  1062. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1063. if Assigned(OnFindUnit) then
  1064. Result:=OnFindUnit(AName);
  1065. if NameExpr=nil then ;
  1066. end;
  1067. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1068. begin
  1069. // do not parse recursively
  1070. // parse via the queue
  1071. if Section=nil then ;
  1072. end;
  1073. { TCustomTestModule }
  1074. function TCustomTestModule.GetMsgCount: integer;
  1075. begin
  1076. Result:=FHintMsgs.Count;
  1077. end;
  1078. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1079. begin
  1080. Result:=TTestHintMessage(FHintMsgs[Index]);
  1081. end;
  1082. function TCustomTestModule.GetResolverCount: integer;
  1083. begin
  1084. Result:=FModules.Count;
  1085. end;
  1086. function TCustomTestModule.GetResolvers(Index: integer
  1087. ): TTestEnginePasResolver;
  1088. begin
  1089. Result:=TTestEnginePasResolver(FModules[Index]);
  1090. end;
  1091. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1092. ): TPasModule;
  1093. var
  1094. DefNamespace: String;
  1095. begin
  1096. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1097. if (Pos('.',aUnitName)<1) then
  1098. begin
  1099. DefNamespace:=GetDefaultNamespace;
  1100. if DefNamespace<>'' then
  1101. begin
  1102. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1103. if Result<>nil then exit;
  1104. end;
  1105. end;
  1106. Result:=LoadUnit(aUnitName);
  1107. if Result<>nil then exit;
  1108. {$IFDEF VerbosePas2JS}
  1109. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1110. {$ENDIF}
  1111. Fail('can''t find unit "'+aUnitName+'"');
  1112. end;
  1113. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1114. var
  1115. aParser: TPasParser;
  1116. Item: TTestHintMessage;
  1117. begin
  1118. aParser:=Sender as TPasParser;
  1119. Item:=TTestHintMessage.Create;
  1120. Item.Id:=aParser.LastMsgNumber;
  1121. Item.MsgType:=aParser.LastMsgType;
  1122. Item.MsgNumber:=aParser.LastMsgNumber;
  1123. Item.Msg:=Msg;
  1124. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1125. {$IFDEF VerbosePas2JS}
  1126. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1127. {$ENDIF}
  1128. FHintMsgs.Add(Item);
  1129. end;
  1130. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1131. );
  1132. var
  1133. aResolver: TTestEnginePasResolver;
  1134. Item: TTestHintMessage;
  1135. begin
  1136. aResolver:=Sender as TTestEnginePasResolver;
  1137. Item:=TTestHintMessage.Create;
  1138. Item.Id:=aResolver.LastMsgId;
  1139. Item.MsgType:=aResolver.LastMsgType;
  1140. Item.MsgNumber:=aResolver.LastMsgNumber;
  1141. Item.Msg:=Msg;
  1142. Item.SourcePos:=aResolver.LastSourcePos;
  1143. {$IFDEF VerbosePas2JS}
  1144. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1145. {$ENDIF}
  1146. FHintMsgs.Add(Item);
  1147. end;
  1148. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1149. var
  1150. Item: TTestHintMessage;
  1151. aScanner: TPas2jsPasScanner;
  1152. begin
  1153. aScanner:=Sender as TPas2jsPasScanner;
  1154. Item:=TTestHintMessage.Create;
  1155. Item.Id:=aScanner.LastMsgNumber;
  1156. Item.MsgType:=aScanner.LastMsgType;
  1157. Item.MsgNumber:=aScanner.LastMsgNumber;
  1158. Item.Msg:=Msg;
  1159. Item.SourcePos:=aScanner.CurSourcePos;
  1160. {$IFDEF VerbosePas2JS}
  1161. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1162. {$ENDIF}
  1163. FHintMsgs.Add(Item);
  1164. end;
  1165. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1166. var
  1167. i: Integer;
  1168. CurEngine: TTestEnginePasResolver;
  1169. CurUnitName: String;
  1170. begin
  1171. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1172. Result:=nil;
  1173. if (Module.ClassType=TPasModule)
  1174. and (CompareText(Module.Name,aUnitName)=0) then
  1175. exit(Module);
  1176. for i:=0 to ResolverCount-1 do
  1177. begin
  1178. CurEngine:=Resolvers[i];
  1179. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1180. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1181. if CompareText(aUnitName,CurUnitName)=0 then
  1182. begin
  1183. Result:=CurEngine.Module;
  1184. if Result<>nil then exit;
  1185. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1186. FileResolver.FindSourceFile(aUnitName);
  1187. CurEngine.StreamResolver:=TStreamResolver.Create;
  1188. CurEngine.StreamResolver.OwnsStreams:=True;
  1189. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1190. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1191. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1192. InitScanner(CurEngine.Scanner);
  1193. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1194. CurEngine.Parser.Options:=po_tcmodules;
  1195. if CompareText(CurUnitName,'System')=0 then
  1196. CurEngine.Parser.ImplicitUses.Clear;
  1197. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1198. try
  1199. CurEngine.Parser.NextToken;
  1200. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1201. except
  1202. on E: Exception do
  1203. HandleException(E);
  1204. end;
  1205. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1206. Result:=CurEngine.Module;
  1207. exit;
  1208. end;
  1209. end;
  1210. end;
  1211. procedure TCustomTestModule.SetUp;
  1212. begin
  1213. {$IFDEF EnablePasTreeGlobalRefCount}
  1214. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1215. {$ENDIF}
  1216. if FModules<>nil then
  1217. begin
  1218. writeln('TCustomTestModule.SetUp FModules<>nil');
  1219. Halt;
  1220. end;
  1221. inherited SetUp;
  1222. FSkipTests:=false;
  1223. FSource:=TStringList.Create;
  1224. FModules:=TObjectList.Create(true);
  1225. FFilename:='test1.pp';
  1226. FFileResolver:=TStreamResolver.Create;
  1227. FFileResolver.OwnsStreams:=True;
  1228. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1229. InitScanner(FScanner);
  1230. FEngine:=AddModule(Filename);
  1231. FEngine.Scanner:=FScanner;
  1232. FScanner.Resolver:=FEngine;
  1233. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1234. FParser.OnLog:=@OnParserLog;
  1235. FEngine.Parser:=FParser;
  1236. Parser.Options:=po_tcmodules;
  1237. FModule:=Nil;
  1238. FConverter:=CreateConverter;
  1239. FExpectedErrorClass:=nil;
  1240. end;
  1241. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1242. begin
  1243. Result:=TPasToJSConverter.Create;
  1244. Result.Options:=co_tcmodules;
  1245. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1246. end;
  1247. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1248. begin
  1249. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1250. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1251. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1252. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1253. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1254. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1255. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1256. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1257. aScanner.OnLog:=@OnScannerLog;
  1258. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1259. end;
  1260. procedure TCustomTestModule.TearDown;
  1261. {$IFDEF CheckPasTreeRefCount}
  1262. var
  1263. El: TPasElement;
  1264. {$ENDIF}
  1265. var
  1266. i: Integer;
  1267. CurModule: TPasModule;
  1268. begin
  1269. FHintMsgs.Clear;
  1270. FHintMsgsGood.Clear;
  1271. FSkipTests:=false;
  1272. FJSRegModuleCall:=nil;
  1273. FJSModuleCallArgs:=nil;
  1274. FJSImplentationUses:=nil;
  1275. FJSInterfaceUses:=nil;
  1276. FJSModuleSrc:=nil;
  1277. FJSInitBody:=nil;
  1278. FreeAndNil(FJSSource);
  1279. FreeAndNil(FJSModule);
  1280. FreeAndNil(FConverter);
  1281. Engine.Clear;
  1282. FreeAndNil(FSource);
  1283. FreeAndNil(FFileResolver);
  1284. if FModules<>nil then
  1285. begin
  1286. for i:=0 to FModules.Count-1 do
  1287. begin
  1288. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1289. if CurModule=nil then continue;
  1290. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1291. CurModule.ReleaseUsedUnits;
  1292. end;
  1293. if FModule<>nil then
  1294. FModule.ReleaseUsedUnits;
  1295. for i:=0 to FModules.Count-1 do
  1296. begin
  1297. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1298. if CurModule=nil then continue;
  1299. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1300. end;
  1301. FreeAndNil(FModules);
  1302. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1303. FEngine:=nil;
  1304. end;
  1305. inherited TearDown;
  1306. {$IFDEF EnablePasTreeGlobalRefCount}
  1307. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1308. begin
  1309. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1310. {$IFDEF CheckPasTreeRefCount}
  1311. El:=TPasElement.FirstRefEl;
  1312. while El<>nil do
  1313. begin
  1314. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1315. for i:=0 to El.RefIds.Count-1 do
  1316. writeln(' ',El.RefIds[i]);
  1317. El:=El.NextRefEl;
  1318. end;
  1319. {$ENDIF}
  1320. Halt;
  1321. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1322. end;
  1323. {$ENDIF}
  1324. end;
  1325. procedure TCustomTestModule.Add(Line: string);
  1326. begin
  1327. Source.Add(Line);
  1328. end;
  1329. procedure TCustomTestModule.Add(const Lines: array of string);
  1330. var
  1331. i: Integer;
  1332. begin
  1333. for i:=low(Lines) to high(Lines) do
  1334. Add(Lines[i]);
  1335. end;
  1336. procedure TCustomTestModule.StartParsing;
  1337. var
  1338. Src: String;
  1339. begin
  1340. Src:=Source.Text;
  1341. FEngine.Source:=Src;
  1342. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1343. Scanner.OpenFile(FileName);
  1344. Writeln('// Test : ',Self.TestName);
  1345. Writeln(Src);
  1346. end;
  1347. procedure TCustomTestModule.ParseModuleQueue;
  1348. var
  1349. i: Integer;
  1350. CurResolver: TTestEnginePasResolver;
  1351. Found: Boolean;
  1352. Section: TPasSection;
  1353. begin
  1354. // parse til exception or all modules finished
  1355. while not SkipTests do
  1356. begin
  1357. Found:=false;
  1358. for i:=0 to ResolverCount-1 do
  1359. begin
  1360. CurResolver:=Resolvers[i];
  1361. if CurResolver.CurrentParser=nil then continue;
  1362. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1363. continue;
  1364. CurResolver.Parser.ParseContinue;
  1365. Found:=true;
  1366. break;
  1367. end;
  1368. if not Found then break;
  1369. end;
  1370. for i:=0 to ResolverCount-1 do
  1371. begin
  1372. CurResolver:=Resolvers[i];
  1373. if CurResolver.Parser=nil then
  1374. begin
  1375. if CurResolver.CurrentParser<>nil then
  1376. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1377. continue;
  1378. end;
  1379. if CurResolver.Parser.CurModule<>nil then
  1380. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1381. end;
  1382. end;
  1383. procedure TCustomTestModule.ParseModule;
  1384. begin
  1385. if SkipTests then exit;
  1386. FFirstPasStatement:=nil;
  1387. try
  1388. StartParsing;
  1389. Parser.ParseMain(FModule);
  1390. ParseModuleQueue;
  1391. except
  1392. on E: Exception do
  1393. HandleException(E);
  1394. end;
  1395. if SkipTests then exit;
  1396. AssertNotNull('Module resulted in Module',Module);
  1397. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1398. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1399. end;
  1400. procedure TCustomTestModule.ParseProgram;
  1401. begin
  1402. if SkipTests then exit;
  1403. ParseModule;
  1404. if SkipTests then exit;
  1405. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1406. FPasProgram:=TPasProgram(Module);
  1407. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1408. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1409. if (PasProgram.InitializationSection.Elements.Count>0) then
  1410. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1411. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1412. end;
  1413. procedure TCustomTestModule.ParseUnit;
  1414. begin
  1415. if SkipTests then exit;
  1416. ParseModule;
  1417. if SkipTests then exit;
  1418. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1419. AssertNotNull('Has interface section',Module.InterfaceSection);
  1420. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1421. if (Module.InitializationSection<>nil)
  1422. and (Module.InitializationSection.Elements.Count>0)
  1423. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1424. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1425. end;
  1426. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1427. ): TTestEnginePasResolver;
  1428. var
  1429. i: Integer;
  1430. begin
  1431. for i:=0 to ResolverCount-1 do
  1432. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1433. exit(Resolvers[i]);
  1434. Result:=nil;
  1435. end;
  1436. function TCustomTestModule.AddModule(aFilename: string
  1437. ): TTestEnginePasResolver;
  1438. begin
  1439. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1440. if FindModuleWithFilename(aFilename)<>nil then
  1441. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1442. Result:=TTestEnginePasResolver.Create;
  1443. Result.Filename:=aFilename;
  1444. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1445. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1446. Result.OnLog:=@OnPasResolverLog;
  1447. FModules.Add(Result);
  1448. end;
  1449. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1450. ): TTestEnginePasResolver;
  1451. begin
  1452. Result:=AddModule(aFilename);
  1453. Result.Source:=Src;
  1454. end;
  1455. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1456. ImplementationSrc: string): TTestEnginePasResolver;
  1457. var
  1458. Src: String;
  1459. begin
  1460. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1461. Src+=LineEnding;
  1462. Src+='interface'+LineEnding;
  1463. Src+=LineEnding;
  1464. Src+=InterfaceSrc;
  1465. Src+='implementation'+LineEnding;
  1466. Src+=LineEnding;
  1467. Src+=ImplementationSrc;
  1468. Src+='end.'+LineEnding;
  1469. Result:=AddModuleWithSrc(aFilename,Src);
  1470. end;
  1471. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1472. var
  1473. Intf, Impl: TStringList;
  1474. begin
  1475. Intf:=TStringList.Create;
  1476. // interface
  1477. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1478. Intf.Add('{$modeswitch externalclass}');
  1479. Intf.Add('type');
  1480. Intf.Add(' integer=longint;');
  1481. Intf.Add(' sizeint=nativeint;');
  1482. //'const',
  1483. //' LineEnding = #10;',
  1484. //' DirectorySeparator = ''/'';',
  1485. //' DriveSeparator = '''';',
  1486. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1487. //' AllowDriveSeparators : set of char = [];',
  1488. if supTObject in Parts then
  1489. Intf.AddStrings([
  1490. 'type',
  1491. ' TClass = class of TObject;',
  1492. ' TObject = class',
  1493. ' constructor Create;',
  1494. ' destructor Destroy; virtual;',
  1495. ' class function ClassType: TClass; assembler;',
  1496. ' class function ClassName: String; assembler;',
  1497. ' class function ClassNameIs(const Name: string): boolean;',
  1498. ' class function ClassParent: TClass; assembler;',
  1499. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1500. ' class function UnitName: String; assembler;',
  1501. ' procedure AfterConstruction; virtual;',
  1502. ' procedure BeforeDestruction;virtual;',
  1503. ' function Equals(Obj: TObject): boolean; virtual;',
  1504. ' function ToString: String; virtual;',
  1505. ' end;']);
  1506. if supTVarRec in Parts then
  1507. Intf.AddStrings([
  1508. 'const',
  1509. ' vtInteger = 0;',
  1510. ' vtBoolean = 1;',
  1511. ' vtJSValue = 19;',
  1512. 'type',
  1513. ' PVarRec = ^TVarRec;',
  1514. ' TVarRec = record',
  1515. ' VType : byte;',
  1516. ' VJSValue: JSValue;',
  1517. ' vInteger: longint external name ''VJSValue'';',
  1518. ' vBoolean: boolean external name ''VJSValue'';',
  1519. ' end;',
  1520. ' TVarRecArray = array of TVarRec;',
  1521. 'function VarRecs: TVarRecArray; varargs;',
  1522. '']);
  1523. if supTypeInfo in Parts then
  1524. begin
  1525. Intf.AddStrings([
  1526. 'type',
  1527. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1528. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1529. ' end;',
  1530. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1531. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1532. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1533. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1534. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1535. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1536. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1537. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1538. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1539. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1540. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1541. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1542. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1543. '']);
  1544. end;
  1545. Intf.Add('var');
  1546. Intf.Add(' ExitCode: Longint = 0;');
  1547. // implementation
  1548. Impl:=TStringList.Create;
  1549. if supTObject in Parts then
  1550. Impl.AddStrings([
  1551. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1552. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1553. 'asm',
  1554. 'end;',
  1555. 'constructor TObject.Create; begin end;',
  1556. 'destructor TObject.Destroy; begin end;',
  1557. 'class function TObject.ClassType: TClass; assembler;',
  1558. 'asm',
  1559. 'end;',
  1560. 'class function TObject.ClassName: String; assembler;',
  1561. 'asm',
  1562. 'end;',
  1563. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1564. 'begin',
  1565. ' Result:=SameText(Name,ClassName);',
  1566. 'end;',
  1567. 'class function TObject.ClassParent: TClass; assembler;',
  1568. 'asm',
  1569. 'end;',
  1570. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1571. 'asm',
  1572. 'end;',
  1573. 'class function TObject.UnitName: String; assembler;',
  1574. 'asm',
  1575. 'end;',
  1576. 'procedure TObject.AfterConstruction; begin end;',
  1577. 'procedure TObject.BeforeDestruction; begin end;',
  1578. 'function TObject.Equals(Obj: TObject): boolean;',
  1579. 'begin',
  1580. ' Result:=Obj=Self;',
  1581. 'end;',
  1582. 'function TObject.ToString: String;',
  1583. 'begin',
  1584. ' Result:=ClassName;',
  1585. 'end;'
  1586. ]);
  1587. if supTVarRec in Parts then
  1588. Impl.AddStrings([
  1589. 'function VarRecs: TVarRecArray; varargs;',
  1590. 'var',
  1591. ' v: PVarRec;',
  1592. 'begin',
  1593. ' v^.VType:=1;',
  1594. ' v^.VJSValue:=2;',
  1595. 'end;',
  1596. '']);
  1597. try
  1598. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1599. finally
  1600. Intf.Free;
  1601. Impl.Free;
  1602. end;
  1603. end;
  1604. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1605. SystemUnitParts: TSystemUnitParts);
  1606. begin
  1607. if NeedSystemUnit then
  1608. AddSystemUnit(SystemUnitParts)
  1609. else
  1610. Parser.ImplicitUses.Clear;
  1611. Add('program '+ExtractFileUnitName(Filename)+';');
  1612. Add('');
  1613. end;
  1614. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1615. SystemUnitParts: TSystemUnitParts);
  1616. begin
  1617. if NeedSystemUnit then
  1618. AddSystemUnit(SystemUnitParts)
  1619. else
  1620. Parser.ImplicitUses.Clear;
  1621. Add('unit Test1;');
  1622. Add('');
  1623. end;
  1624. procedure TCustomTestModule.ConvertModule;
  1625. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1626. out UsesLit: TJSArrayLiteral);
  1627. var
  1628. i: Integer;
  1629. Item: TJSElement;
  1630. Lit: TJSLiteral;
  1631. begin
  1632. UsesLit:=nil;
  1633. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1634. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1635. exit; // null is ok
  1636. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1637. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1638. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1639. begin
  1640. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1641. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1642. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1643. Lit:=TJSLiteral(Item);
  1644. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1645. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1646. end;
  1647. end;
  1648. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1649. out Src: TJSSourceElements);
  1650. var
  1651. FunDecl: TJSFunctionDeclarationStatement;
  1652. FunDef: TJSFuncDef;
  1653. FunBody: TJSFunctionBody;
  1654. begin
  1655. Src:=nil;
  1656. AssertNotNull(ParamName,Arg.Expr);
  1657. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1658. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1659. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1660. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1661. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1662. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1663. AssertNotNull(ParamName+' body',FunDef.Body);
  1664. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1665. FunBody:=FunDef.Body as TJSFunctionBody;
  1666. AssertNotNull(ParamName+' body.A',FunBody.A);
  1667. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1668. Src:=FunBody.A as TJSSourceElements;
  1669. end;
  1670. var
  1671. ModuleNameExpr: TJSLiteral;
  1672. InitFunction: TJSFunctionDeclarationStatement;
  1673. InitAssign: TJSSimpleAssignStatement;
  1674. InitName: String;
  1675. LastNode: TJSElement;
  1676. Arg: TJSArrayLiteralElement;
  1677. begin
  1678. if SkipTests then exit;
  1679. try
  1680. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1681. except
  1682. on E: Exception do
  1683. HandleException(E);
  1684. end;
  1685. if SkipTests then exit;
  1686. if ExpectedErrorClass<>nil then
  1687. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1688. FJSSource:=TStringList.Create;
  1689. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1690. {$IFDEF VerbosePas2JS}
  1691. writeln('TTestModule.ConvertModule JS:');
  1692. write(FJSSource.Text);
  1693. {$ENDIF}
  1694. // rtl.module(...
  1695. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1696. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1697. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1698. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1699. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1700. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1701. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1702. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1703. // parameter 'unitname'
  1704. if JSModuleCallArgs.Elements.Count<1 then
  1705. Fail('rtl.module first param unit missing');
  1706. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1707. AssertNotNull('module name param',Arg.Expr);
  1708. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1709. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1710. if Module is TPasProgram then
  1711. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1712. else
  1713. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1714. // main uses section
  1715. if JSModuleCallArgs.Elements.Count<2 then
  1716. Fail('rtl.module second param main uses missing');
  1717. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1718. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1719. // program/library/interface function()
  1720. if JSModuleCallArgs.Elements.Count<3 then
  1721. Fail('rtl.module third param intf-function missing');
  1722. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1723. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1724. // search for $mod.$init or $mod.$main - the last statement
  1725. if Module is TPasProgram then
  1726. begin
  1727. InitName:='$main';
  1728. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1729. end
  1730. else
  1731. InitName:='$init';
  1732. FJSInitBody:=nil;
  1733. if JSModuleSrc.Statements.Count>0 then
  1734. begin
  1735. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1736. if LastNode is TJSSimpleAssignStatement then
  1737. begin
  1738. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1739. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1740. begin
  1741. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1742. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1743. end
  1744. else if Module is TPasProgram then
  1745. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1746. end;
  1747. end;
  1748. // optional: implementation uses section
  1749. if JSModuleCallArgs.Elements.Count<4 then
  1750. exit;
  1751. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1752. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1753. // optional: implementation function()
  1754. if JSModuleCallArgs.Elements.Count<5 then
  1755. exit;
  1756. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1757. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1758. end;
  1759. procedure TCustomTestModule.ConvertProgram;
  1760. begin
  1761. Add('end.');
  1762. ParseProgram;
  1763. ConvertModule;
  1764. end;
  1765. procedure TCustomTestModule.ConvertUnit;
  1766. begin
  1767. Add('end.');
  1768. ParseUnit;
  1769. ConvertModule;
  1770. end;
  1771. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1772. begin
  1773. Result:=tcmodules.JSToStr(El);
  1774. end;
  1775. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1776. DottedName: string);
  1777. begin
  1778. if DottedName='' then
  1779. begin
  1780. AssertNull(Msg,El);
  1781. end
  1782. else
  1783. begin
  1784. AssertNotNull(Msg,El);
  1785. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1786. end;
  1787. end;
  1788. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1789. begin
  1790. if El=nil then
  1791. Result:=''
  1792. else if El is TJSPrimaryExpressionIdent then
  1793. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1794. else if El is TJSDotMemberExpression then
  1795. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1796. else
  1797. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1798. end;
  1799. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1800. InitStatements: string; ImplStatements: string);
  1801. var
  1802. ActualSrc, ExpectedSrc, InitName: String;
  1803. begin
  1804. ActualSrc:=JSToStr(JSModuleSrc);
  1805. ExpectedSrc:=
  1806. 'var $mod = this;'+LineEnding
  1807. +Statements;
  1808. if coUseStrict in Converter.Options then
  1809. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1810. if Module is TPasProgram then
  1811. InitName:='$main'
  1812. else
  1813. InitName:='$init';
  1814. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1815. ExpectedSrc:=ExpectedSrc+LineEnding
  1816. +'$mod.'+InitName+' = function () {'+LineEnding
  1817. +InitStatements
  1818. +'};'+LineEnding;
  1819. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1820. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1821. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1822. if (JSImplementationSrc<>nil) then
  1823. begin
  1824. ActualSrc:=JSToStr(JSImplementationSrc);
  1825. ExpectedSrc:=
  1826. 'var $mod = this;'+LineEnding
  1827. +'var $impl = $mod.$impl;'+LineEnding
  1828. +ImplStatements;
  1829. end
  1830. else
  1831. begin
  1832. ActualSrc:='';
  1833. ExpectedSrc:=ImplStatements;
  1834. end;
  1835. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1836. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1837. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1838. end;
  1839. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1840. // search diff, ignore changes in spaces
  1841. var
  1842. s: string;
  1843. begin
  1844. if CheckSrcDiff(Expected,Actual,s) then exit;
  1845. Fail(Msg+': '+s);
  1846. end;
  1847. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1848. var
  1849. aResolver: TTestEnginePasResolver;
  1850. aConverter: TPasToJSConverter;
  1851. aJSModule: TJSSourceElements;
  1852. ActualSrc: String;
  1853. begin
  1854. aResolver:=GetResolver(Filename);
  1855. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1856. {$IFDEF VerbosePas2JS}
  1857. writeln('CheckUnit '+Filename+' converting ...');
  1858. {$ENDIF}
  1859. aConverter:=CreateConverter;
  1860. aJSModule:=nil;
  1861. try
  1862. try
  1863. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1864. except
  1865. on E: Exception do
  1866. HandleException(E);
  1867. end;
  1868. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1869. {$IFDEF VerbosePas2JS}
  1870. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1871. write(aResolver.Source);
  1872. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1873. write(ActualSrc);
  1874. {$ENDIF}
  1875. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1876. finally
  1877. aJSModule.Free;
  1878. aConverter.Free;
  1879. end;
  1880. end;
  1881. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1882. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1883. var
  1884. i: Integer;
  1885. Item: TTestHintMessage;
  1886. Expected,Actual: string;
  1887. begin
  1888. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1889. for i:=0 to MsgCount-1 do
  1890. begin
  1891. Item:=Msgs[i];
  1892. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1893. if (Marker<>nil) then
  1894. begin
  1895. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1896. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1897. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1898. end;
  1899. // found
  1900. FHintMsgsGood.Add(Item);
  1901. str(Item.MsgType,Actual);
  1902. str(MsgType,Expected);
  1903. AssertEquals('MsgType',Expected,Actual);
  1904. exit;
  1905. end;
  1906. // needed message missing -> show emitted messages
  1907. WriteSources('',0,0);
  1908. for i:=0 to MsgCount-1 do
  1909. begin
  1910. Item:=Msgs[i];
  1911. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1912. ' ('+IntToStr(Item.MsgNumber),')');
  1913. if Marker<>nil then
  1914. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1915. writeln(' {',Item.Msg,'}');
  1916. end;
  1917. str(MsgType,Expected);
  1918. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1919. if Marker<>nil then
  1920. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1921. Actual:=Actual+' '+Msg;
  1922. Fail(Actual);
  1923. end;
  1924. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1925. );
  1926. var
  1927. i: Integer;
  1928. s, Txt: String;
  1929. Msg: TTestHintMessage;
  1930. begin
  1931. for i:=0 to MsgCount-1 do
  1932. begin
  1933. Msg:=Msgs[i];
  1934. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1935. s:='';
  1936. str(Msg.MsgType,s);
  1937. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1938. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1939. if WithSourcePos then
  1940. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1941. Txt:=Txt+' {'+Msg.Msg+'}';
  1942. Fail(Txt);
  1943. end;
  1944. end;
  1945. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1946. MsgNumber: integer);
  1947. begin
  1948. ExpectedErrorClass:=EScannerError;
  1949. ExpectedErrorMsg:=Msg;
  1950. ExpectedErrorNumber:=MsgNumber;
  1951. end;
  1952. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1953. MsgNumber: integer);
  1954. begin
  1955. ExpectedErrorClass:=EParserError;
  1956. ExpectedErrorMsg:=Msg;
  1957. ExpectedErrorNumber:=MsgNumber;
  1958. end;
  1959. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1960. MsgNumber: integer);
  1961. begin
  1962. ExpectedErrorClass:=EPasResolve;
  1963. ExpectedErrorMsg:=Msg;
  1964. ExpectedErrorNumber:=MsgNumber;
  1965. end;
  1966. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1967. MsgNumber: integer);
  1968. begin
  1969. ExpectedErrorClass:=EPas2JS;
  1970. ExpectedErrorMsg:=Msg;
  1971. ExpectedErrorNumber:=MsgNumber;
  1972. end;
  1973. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1974. var
  1975. MsgNumber: Integer;
  1976. Msg: String;
  1977. begin
  1978. Result:=false;
  1979. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1980. Msg:=E.Message;
  1981. if E is EPas2JS then
  1982. MsgNumber:=EPas2JS(E).MsgNumber
  1983. else if E is EPasResolve then
  1984. MsgNumber:=EPasResolve(E).MsgNumber
  1985. else if E is EParserError then
  1986. MsgNumber:=Parser.LastMsgNumber
  1987. else if E is EScannerError then
  1988. begin
  1989. MsgNumber:=Scanner.LastMsgNumber;
  1990. Msg:=Scanner.LastMsg;
  1991. end
  1992. else
  1993. MsgNumber:=0;
  1994. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1995. if Result then
  1996. SkipTests:=true;
  1997. end;
  1998. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1999. begin
  2000. if IsErrorExpected(E) then exit;
  2001. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2002. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2003. +' '+Scanner.CurFilename
  2004. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2005. FailException(E);
  2006. end;
  2007. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2008. begin
  2009. if IsErrorExpected(E) then exit;
  2010. WriteSources(E.Filename,E.Row,E.Column);
  2011. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2012. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2013. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2014. );
  2015. FailException(E);
  2016. end;
  2017. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2018. var
  2019. P: TPasSourcePos;
  2020. begin
  2021. if IsErrorExpected(E) then exit;
  2022. P:=E.SourcePos;
  2023. WriteSources(P.FileName,P.Row,P.Column);
  2024. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2025. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2026. FailException(E);
  2027. end;
  2028. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2029. var
  2030. Row, Col: integer;
  2031. begin
  2032. if IsErrorExpected(E) then exit;
  2033. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2034. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2035. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2036. +' '+E.PasElement.SourceFilename
  2037. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2038. FailException(E);
  2039. end;
  2040. procedure TCustomTestModule.HandleException(E: Exception);
  2041. begin
  2042. if E is EScannerError then
  2043. HandleScannerError(EScannerError(E))
  2044. else if E is EParserError then
  2045. HandleParserError(EParserError(E))
  2046. else if E is EPasResolve then
  2047. HandlePasResolveError(EPasResolve(E))
  2048. else if E is EPas2JS then
  2049. HandlePas2JSError(EPas2JS(E))
  2050. else
  2051. begin
  2052. if IsErrorExpected(E) then exit;
  2053. if not (E is EAssertionFailedError) then
  2054. begin
  2055. WriteSources('',0,0);
  2056. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2057. end;
  2058. FailException(E);
  2059. end;
  2060. end;
  2061. procedure TCustomTestModule.FailException(E: Exception);
  2062. var
  2063. MsgNumber: Integer;
  2064. begin
  2065. if ExpectedErrorClass<>nil then
  2066. begin
  2067. if FExpectedErrorClass=E.ClassType then
  2068. begin
  2069. if E is EPas2JS then
  2070. MsgNumber:=EPas2JS(E).MsgNumber
  2071. else if E is EPasResolve then
  2072. MsgNumber:=EPasResolve(E).MsgNumber
  2073. else if E is EParserError then
  2074. MsgNumber:=Parser.LastMsgNumber
  2075. else if E is EScannerError then
  2076. MsgNumber:=Scanner.LastMsgNumber
  2077. else
  2078. MsgNumber:=0;
  2079. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2080. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2081. ExpectedErrorNumber,MsgNumber);
  2082. end else begin
  2083. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2084. end;
  2085. end;
  2086. Fail(E.Message);
  2087. end;
  2088. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2089. aCol: integer);
  2090. var
  2091. IsSrc: Boolean;
  2092. i, j: Integer;
  2093. SrcLines: TStringList;
  2094. Line: string;
  2095. aModule: TTestEnginePasResolver;
  2096. begin
  2097. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2098. for i:=0 to ResolverCount-1 do
  2099. begin
  2100. aModule:=Resolvers[i];
  2101. SrcLines:=TStringList.Create;
  2102. try
  2103. SrcLines.Text:=aModule.Source;
  2104. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2105. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2106. for j:=1 to SrcLines.Count do
  2107. begin
  2108. Line:=SrcLines[j-1];
  2109. if IsSrc and (j=aRow) then
  2110. begin
  2111. write('*');
  2112. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2113. end;
  2114. writeln(Format('%:4d: ',[j]),Line);
  2115. end;
  2116. finally
  2117. SrcLines.Free;
  2118. end;
  2119. end;
  2120. end;
  2121. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2122. var
  2123. i: Integer;
  2124. begin
  2125. for i:=0 to ResolverCount-1 do
  2126. if Filename=Resolvers[i].Filename then exit(i);
  2127. Result:=-1;
  2128. end;
  2129. function TCustomTestModule.GetResolver(const Filename: string
  2130. ): TTestEnginePasResolver;
  2131. var
  2132. i: Integer;
  2133. begin
  2134. i:=IndexOfResolver(Filename);
  2135. if i<0 then exit(nil);
  2136. Result:=Resolvers[i];
  2137. end;
  2138. function TCustomTestModule.GetDefaultNamespace: string;
  2139. var
  2140. C: TClass;
  2141. begin
  2142. Result:='';
  2143. if FModule=nil then exit;
  2144. C:=FModule.ClassType;
  2145. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2146. Result:=Engine.DefaultNameSpace;
  2147. end;
  2148. constructor TCustomTestModule.Create;
  2149. begin
  2150. inherited Create;
  2151. FHintMsgs:=TObjectList.Create(true);
  2152. FHintMsgsGood:=TFPList.Create;
  2153. end;
  2154. destructor TCustomTestModule.Destroy;
  2155. begin
  2156. FreeAndNil(FHintMsgs);
  2157. FreeAndNil(FHintMsgsGood);
  2158. inherited Destroy;
  2159. end;
  2160. { TTestModule }
  2161. procedure TTestModule.TestReservedWords;
  2162. var
  2163. i: integer;
  2164. begin
  2165. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2166. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2167. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2168. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2169. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2170. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2171. end;
  2172. procedure TTestModule.TestEmptyProgram;
  2173. begin
  2174. StartProgram(false);
  2175. Add('begin');
  2176. ConvertProgram;
  2177. CheckSource('TestEmptyProgram','','');
  2178. end;
  2179. procedure TTestModule.TestEmptyProgramUseStrict;
  2180. begin
  2181. Converter.Options:=Converter.Options+[coUseStrict];
  2182. StartProgram(false);
  2183. Add('begin');
  2184. ConvertProgram;
  2185. CheckSource('TestEmptyProgramUseStrict','','');
  2186. end;
  2187. procedure TTestModule.TestEmptyUnit;
  2188. begin
  2189. StartUnit(false);
  2190. Add('interface');
  2191. Add('implementation');
  2192. ConvertUnit;
  2193. CheckSource('TestEmptyUnit',
  2194. LinesToStr([
  2195. ]),
  2196. '');
  2197. end;
  2198. procedure TTestModule.TestEmptyUnitUseStrict;
  2199. begin
  2200. Converter.Options:=Converter.Options+[coUseStrict];
  2201. StartUnit(false);
  2202. Add('interface');
  2203. Add('implementation');
  2204. ConvertUnit;
  2205. CheckSource('TestEmptyUnitUseStrict',
  2206. LinesToStr([
  2207. ''
  2208. ]),
  2209. '');
  2210. end;
  2211. procedure TTestModule.TestDottedUnitNames;
  2212. begin
  2213. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2214. LinesToStr([
  2215. 'var iV: longint;'
  2216. ]),
  2217. '');
  2218. FFilename:='ns1.test1.pp';
  2219. StartProgram(true);
  2220. Add('uses unIt2;');
  2221. Add('var');
  2222. Add(' i: longint;');
  2223. Add('begin');
  2224. Add(' i:=iv;');
  2225. Add(' i:=uNit2.iv;');
  2226. Add(' i:=Ns1.TEst1.i;');
  2227. ConvertProgram;
  2228. CheckSource('TestDottedUnitNames',
  2229. LinesToStr([
  2230. 'this.i = 0;',
  2231. '']),
  2232. LinesToStr([ // this.$init
  2233. '$mod.i = pas["NS1.Unit2"].iV;',
  2234. '$mod.i = pas["NS1.Unit2"].iV;',
  2235. '$mod.i = $mod.i;',
  2236. '']) );
  2237. end;
  2238. procedure TTestModule.TestDottedUnitNameImpl;
  2239. begin
  2240. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2241. LinesToStr([
  2242. 'type',
  2243. ' TObject = class end;',
  2244. ' TTestA = class',
  2245. ' end;'
  2246. ]),
  2247. LinesToStr(['uses TEST.UnitB;'])
  2248. );
  2249. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2250. LinesToStr([
  2251. 'uses TEST.UnitA;',
  2252. 'type TTestB = class(TTestA);'
  2253. ]),
  2254. ''
  2255. );
  2256. StartProgram(true);
  2257. Add('uses TEST.UnitA;');
  2258. Add('begin');
  2259. ConvertProgram;
  2260. CheckSource('TestDottedUnitNameImpl',
  2261. LinesToStr([
  2262. '']),
  2263. LinesToStr([ // this.$init
  2264. '']) );
  2265. CheckUnit('TEST.UnitA.pas',
  2266. LinesToStr([
  2267. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2268. ' var $mod = this;',
  2269. ' rtl.createClass($mod, "TObject", null, function () {',
  2270. ' this.$init = function () {',
  2271. ' };',
  2272. ' this.$final = function () {',
  2273. ' };',
  2274. ' });',
  2275. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2276. ' });',
  2277. '}, ["TEST.UnitB"]);'
  2278. ]));
  2279. CheckUnit('TEST.UnitB.pas',
  2280. LinesToStr([
  2281. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2282. ' var $mod = this;',
  2283. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2284. ' });',
  2285. '});'
  2286. ]));
  2287. end;
  2288. procedure TTestModule.TestDottedUnitExpr;
  2289. begin
  2290. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2291. LinesToStr([
  2292. 'procedure DoIt;'
  2293. ]),
  2294. 'procedure DoIt; begin end;');
  2295. FFilename:='Ns1.SubNs1.Test1.pp';
  2296. StartProgram(true);
  2297. Add('uses Ns2.sUbnS2.unIt2;');
  2298. Add('var');
  2299. Add(' i: longint;');
  2300. Add('begin');
  2301. Add(' ns2.subns2.unit2.doit;');
  2302. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2303. ConvertProgram;
  2304. CheckSource('TestDottedUnitExpr',
  2305. LinesToStr([
  2306. 'this.i = 0;',
  2307. '']),
  2308. LinesToStr([ // this.$init
  2309. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2310. '$mod.i = $mod.i;',
  2311. '']) );
  2312. end;
  2313. procedure TTestModule.Test_ModeFPCFail;
  2314. begin
  2315. StartProgram(false);
  2316. Add('{$mode FPC}');
  2317. Add('begin');
  2318. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2319. ConvertProgram;
  2320. end;
  2321. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2322. begin
  2323. StartProgram(false);
  2324. Add('{$modeswitch cblocks-}');
  2325. Add('begin');
  2326. ConvertProgram;
  2327. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2328. CheckResolverUnexpectedHints();
  2329. end;
  2330. procedure TTestModule.TestUnit_UseSystem;
  2331. begin
  2332. StartUnit(true);
  2333. Add([
  2334. 'interface',
  2335. 'var i: integer;',
  2336. 'implementation']);
  2337. ConvertUnit;
  2338. CheckSource('TestUnit_UseSystem',
  2339. LinesToStr([
  2340. 'this.i = 0;',
  2341. '']),
  2342. LinesToStr([
  2343. '']) );
  2344. end;
  2345. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2346. begin
  2347. AddModuleWithIntfImplSrc('unit1.pp',
  2348. LinesToStr([
  2349. 'type number = longint;']),
  2350. LinesToStr([
  2351. 'uses test1;',
  2352. 'procedure DoIt;',
  2353. 'begin',
  2354. ' i:=3;',
  2355. 'end;']));
  2356. StartUnit(true);
  2357. Add([
  2358. 'interface',
  2359. 'uses unit1;',
  2360. 'var i: number;',
  2361. 'implementation']);
  2362. ConvertUnit;
  2363. CheckSource('TestUnit_Intf1Impl2Intf1',
  2364. LinesToStr([
  2365. 'this.i = 0;',
  2366. '']),
  2367. LinesToStr([
  2368. '']) );
  2369. end;
  2370. procedure TTestModule.TestIncludeVersion;
  2371. begin
  2372. StartProgram(false);
  2373. Add([
  2374. 'var',
  2375. ' s: string;',
  2376. ' i: word;',
  2377. 'begin',
  2378. ' s:={$I %line%};',
  2379. ' i:={$I %linenum%};',
  2380. ' s:={$I %currentroutine%};',
  2381. ' s:={$I %pas2jsversion%};',
  2382. ' s:={$I %pas2jstarget%};',
  2383. ' s:={$I %pas2jstargetos%};',
  2384. ' s:={$I %pas2jstargetcpu%};',
  2385. ' s:={$I %file%};',
  2386. '']);
  2387. ConvertProgram;
  2388. CheckSource('TestIncludeVersion',
  2389. LinesToStr([
  2390. 'this.s="";',
  2391. 'this.i = 0;']),
  2392. LinesToStr([
  2393. '$mod.s = "7";',
  2394. '$mod.i = 8;',
  2395. '$mod.s = "<anonymous>";',
  2396. '$mod.s = "Comp.Ver.tcmodules";',
  2397. '$mod.s = "Browser";',
  2398. '$mod.s = "Browser";',
  2399. '$mod.s = "ECMAScript5";',
  2400. '$mod.s = "test1.pp";',
  2401. '']));
  2402. end;
  2403. procedure TTestModule.TestVarInt;
  2404. begin
  2405. StartProgram(false);
  2406. Add('var MyI: longint;');
  2407. Add('begin');
  2408. ConvertProgram;
  2409. CheckSource('TestVarInt','this.MyI=0;','');
  2410. end;
  2411. procedure TTestModule.TestVarBaseTypes;
  2412. begin
  2413. StartProgram(false);
  2414. Add('var');
  2415. Add(' i: longint;');
  2416. Add(' s: string;');
  2417. Add(' c: char;');
  2418. Add(' b: boolean;');
  2419. Add(' d: double;');
  2420. Add(' i2: longint = 3;');
  2421. Add(' s2: string = ''foo'';');
  2422. Add(' c2: char = ''4'';');
  2423. Add(' b2: boolean = true;');
  2424. Add(' d2: double = 5.6;');
  2425. Add(' i3: longint = $707;');
  2426. Add(' i4: nativeint = 9007199254740991;');
  2427. Add(' i5: nativeint = -9007199254740991-1;');
  2428. Add(' i6: nativeint = $fffffffffffff;');
  2429. Add(' i7: nativeint = -$fffffffffffff-1;');
  2430. Add(' i8: byte = 00;');
  2431. Add(' u8: nativeuint = $fffffffffffff;');
  2432. Add(' u9: nativeuint = $0000000000000;');
  2433. Add(' u10: nativeuint = $00ff00;');
  2434. Add('begin');
  2435. ConvertProgram;
  2436. CheckSource('TestVarBaseTypes',
  2437. LinesToStr([
  2438. 'this.i = 0;',
  2439. 'this.s = "";',
  2440. 'this.c = "";',
  2441. 'this.b = false;',
  2442. 'this.d = 0.0;',
  2443. 'this.i2 = 3;',
  2444. 'this.s2 = "foo";',
  2445. 'this.c2 = "4";',
  2446. 'this.b2 = true;',
  2447. 'this.d2 = 5.6;',
  2448. 'this.i3 = 0x707;',
  2449. 'this.i4 = 9007199254740991;',
  2450. 'this.i5 = -9007199254740991-1;',
  2451. 'this.i6 = 0xfffffffffffff;',
  2452. 'this.i7 =-0xfffffffffffff-1;',
  2453. 'this.i8 = 0;',
  2454. 'this.u8 = 0xfffffffffffff;',
  2455. 'this.u9 = 0x0;',
  2456. 'this.u10 = 0xff00;'
  2457. ]),
  2458. '');
  2459. end;
  2460. procedure TTestModule.TestBaseTypeSingleFail;
  2461. begin
  2462. StartProgram(false);
  2463. Add('var s: single;');
  2464. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2465. ConvertProgram;
  2466. end;
  2467. procedure TTestModule.TestBaseTypeExtendedFail;
  2468. begin
  2469. StartProgram(false);
  2470. Add('var e: extended;');
  2471. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2472. ConvertProgram;
  2473. end;
  2474. procedure TTestModule.TestConstBaseTypes;
  2475. begin
  2476. StartProgram(false);
  2477. Add('const');
  2478. Add(' i: longint = 3;');
  2479. Add(' s: string = ''foo'';');
  2480. Add(' c: char = ''4'';');
  2481. Add(' b: boolean = true;');
  2482. Add(' d: double = 5.6;');
  2483. Add(' e = low(word);');
  2484. Add(' f = high(word);');
  2485. Add('begin');
  2486. ConvertProgram;
  2487. CheckSource('TestVarBaseTypes',
  2488. LinesToStr([
  2489. 'this.i=3;',
  2490. 'this.s="foo";',
  2491. 'this.c="4";',
  2492. 'this.b=true;',
  2493. 'this.d=5.6;',
  2494. 'this.e = 0;',
  2495. 'this.f = 65535;'
  2496. ]),
  2497. '');
  2498. end;
  2499. procedure TTestModule.TestAliasTypeRef;
  2500. begin
  2501. StartProgram(false);
  2502. Add('type');
  2503. Add(' a=longint;');
  2504. Add(' b=a;');
  2505. Add('var');
  2506. Add(' c: A;');
  2507. Add(' d: B;');
  2508. Add('begin');
  2509. ConvertProgram;
  2510. CheckSource('TestAliasTypeRef',
  2511. LinesToStr([ // statements
  2512. 'this.c = 0;',
  2513. 'this.d = 0;'
  2514. ]),
  2515. LinesToStr([ // this.$main
  2516. ''
  2517. ]));
  2518. end;
  2519. procedure TTestModule.TestTypeCast_BaseTypes;
  2520. begin
  2521. StartProgram(false);
  2522. Add([
  2523. 'var',
  2524. ' i: longint;',
  2525. ' b: boolean;',
  2526. ' d: double;',
  2527. ' s: string;',
  2528. ' c: char;',
  2529. 'begin',
  2530. ' i:=longint(i);',
  2531. ' i:=longint(b);',
  2532. ' b:=boolean(b);',
  2533. ' b:=boolean(i);',
  2534. ' d:=double(d);',
  2535. ' d:=double(i);',
  2536. ' s:=string(s);',
  2537. ' s:=string(c);',
  2538. ' c:=char(c);',
  2539. ' c:=char(i);',
  2540. ' c:=char(65);',
  2541. ' c:=char(#10);',
  2542. ' c:=char(#$E000);',
  2543. '']);
  2544. ConvertProgram;
  2545. CheckSource('TestAliasTypeRef',
  2546. LinesToStr([ // statements
  2547. 'this.i = 0;',
  2548. 'this.b = false;',
  2549. 'this.d = 0.0;',
  2550. 'this.s = "";',
  2551. 'this.c = "";',
  2552. '']),
  2553. LinesToStr([ // this.$main
  2554. '$mod.i = $mod.i;',
  2555. '$mod.i = ($mod.b ? 1 : 0);',
  2556. '$mod.b = $mod.b;',
  2557. '$mod.b = $mod.i != 0;',
  2558. '$mod.d = $mod.d;',
  2559. '$mod.d = $mod.i;',
  2560. '$mod.s = $mod.s;',
  2561. '$mod.s = $mod.c;',
  2562. '$mod.c = $mod.c;',
  2563. '$mod.c = String.fromCharCode($mod.i);',
  2564. '$mod.c = "A";',
  2565. '$mod.c = "\n";',
  2566. '$mod.c = "";',
  2567. '']));
  2568. end;
  2569. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2570. begin
  2571. StartProgram(false);
  2572. Add('type');
  2573. Add(' integer = longint;');
  2574. Add(' TYesNo = boolean;');
  2575. Add(' TFloat = double;');
  2576. Add(' TCaption = string;');
  2577. Add(' TChar = char;');
  2578. Add('var');
  2579. Add(' i: integer;');
  2580. Add(' b: TYesNo;');
  2581. Add(' d: TFloat;');
  2582. Add(' s: TCaption;');
  2583. Add(' c: TChar;');
  2584. Add('begin');
  2585. Add(' i:=integer(i);');
  2586. Add(' i:=integer(b);');
  2587. Add(' b:=TYesNo(b);');
  2588. Add(' b:=TYesNo(i);');
  2589. Add(' d:=TFloat(d);');
  2590. Add(' d:=TFloat(i);');
  2591. Add(' s:=TCaption(s);');
  2592. Add(' s:=TCaption(c);');
  2593. Add(' c:=TChar(c);');
  2594. ConvertProgram;
  2595. CheckSource('TestAliasTypeRef',
  2596. LinesToStr([ // statements
  2597. 'this.i = 0;',
  2598. 'this.b = false;',
  2599. 'this.d = 0.0;',
  2600. 'this.s = "";',
  2601. 'this.c = "";',
  2602. '']),
  2603. LinesToStr([ // this.$main
  2604. '$mod.i = $mod.i;',
  2605. '$mod.i = ($mod.b ? 1 : 0);',
  2606. '$mod.b = $mod.b;',
  2607. '$mod.b = $mod.i != 0;',
  2608. '$mod.d = $mod.d;',
  2609. '$mod.d = $mod.i;',
  2610. '$mod.s = $mod.s;',
  2611. '$mod.s = $mod.c;',
  2612. '$mod.c = $mod.c;',
  2613. '']));
  2614. end;
  2615. procedure TTestModule.TestEmptyProc;
  2616. begin
  2617. StartProgram(false);
  2618. Add('procedure Test;');
  2619. Add('begin');
  2620. Add('end;');
  2621. Add('begin');
  2622. ConvertProgram;
  2623. CheckSource('TestEmptyProc',
  2624. LinesToStr([ // statements
  2625. 'this.Test = function () {',
  2626. '};'
  2627. ]),
  2628. LinesToStr([ // this.$main
  2629. ''
  2630. ]));
  2631. end;
  2632. procedure TTestModule.TestProcOneParam;
  2633. begin
  2634. StartProgram(false);
  2635. Add('procedure ProcA(i: longint);');
  2636. Add('begin');
  2637. Add('end;');
  2638. Add('begin');
  2639. Add(' PROCA(3);');
  2640. ConvertProgram;
  2641. CheckSource('TestProcOneParam',
  2642. LinesToStr([ // statements
  2643. 'this.ProcA = function (i) {',
  2644. '};'
  2645. ]),
  2646. LinesToStr([ // this.$main
  2647. '$mod.ProcA(3);'
  2648. ]));
  2649. end;
  2650. procedure TTestModule.TestFunctionWithoutParams;
  2651. begin
  2652. StartProgram(false);
  2653. Add('function FuncA: longint;');
  2654. Add('begin');
  2655. Add('end;');
  2656. Add('var i: longint;');
  2657. Add('begin');
  2658. Add(' I:=FUNCA();');
  2659. Add(' I:=FUNCA;');
  2660. Add(' FUNCA();');
  2661. Add(' FUNCA;');
  2662. ConvertProgram;
  2663. CheckSource('TestProcWithoutParams',
  2664. LinesToStr([ // statements
  2665. 'this.FuncA = function () {',
  2666. ' var Result = 0;',
  2667. ' return Result;',
  2668. '};',
  2669. 'this.i=0;'
  2670. ]),
  2671. LinesToStr([ // this.$main
  2672. '$mod.i=$mod.FuncA();',
  2673. '$mod.i=$mod.FuncA();',
  2674. '$mod.FuncA();',
  2675. '$mod.FuncA();'
  2676. ]));
  2677. end;
  2678. procedure TTestModule.TestProcedureWithoutParams;
  2679. begin
  2680. StartProgram(false);
  2681. Add('procedure ProcA;');
  2682. Add('begin');
  2683. Add('end;');
  2684. Add('begin');
  2685. Add(' PROCA();');
  2686. Add(' PROCA;');
  2687. ConvertProgram;
  2688. CheckSource('TestProcWithoutParams',
  2689. LinesToStr([ // statements
  2690. 'this.ProcA = function () {',
  2691. '};'
  2692. ]),
  2693. LinesToStr([ // this.$main
  2694. '$mod.ProcA();',
  2695. '$mod.ProcA();'
  2696. ]));
  2697. end;
  2698. procedure TTestModule.TestIncDec;
  2699. begin
  2700. StartProgram(false);
  2701. Add([
  2702. 'procedure DoIt(var i: longint);',
  2703. 'begin',
  2704. ' inc(i);',
  2705. ' inc(i,2);',
  2706. 'end;',
  2707. 'var',
  2708. ' Bar: longint;',
  2709. 'begin',
  2710. ' inc(bar);',
  2711. ' inc(bar,2);',
  2712. ' dec(bar);',
  2713. ' dec(bar,3);',
  2714. '']);
  2715. ConvertProgram;
  2716. CheckSource('TestIncDec',
  2717. LinesToStr([ // statements
  2718. 'this.DoIt = function (i) {',
  2719. ' i.set(i.get()+1);',
  2720. ' i.set(i.get()+2);',
  2721. '};',
  2722. 'this.Bar = 0;'
  2723. ]),
  2724. LinesToStr([ // this.$main
  2725. '$mod.Bar+=1;',
  2726. '$mod.Bar+=2;',
  2727. '$mod.Bar-=1;',
  2728. '$mod.Bar-=3;'
  2729. ]));
  2730. end;
  2731. procedure TTestModule.TestLoHiFpcMode;
  2732. begin
  2733. StartProgram(false);
  2734. Add([
  2735. '{$mode objfpc}',
  2736. 'const',
  2737. ' LoByte1 = Lo(Word($1234));',
  2738. ' HiByte1 = Hi(Word($1234));',
  2739. ' LoByte2 = Lo(SmallInt($1234));',
  2740. ' HiByte2 = Hi(SmallInt($1234));',
  2741. ' LoWord1 = Lo($1234CDEF);',
  2742. ' HiWord1 = Hi($1234CDEF);',
  2743. ' LoWord2 = Lo(-$1234CDEF);',
  2744. ' HiWord2 = Hi(-$1234CDEF);',
  2745. ' lo4:byte=lo(byte($34));',
  2746. ' hi4:byte=hi(byte($34));',
  2747. ' lo5:byte=lo(shortint(-$34));',
  2748. ' hi5:byte=hi(shortint(-$34));',
  2749. ' lo6:longword=lo($123456789ABCD);',
  2750. ' hi6:longword=hi($123456789ABCD);',
  2751. ' lo7:longword=lo(-$123456789ABCD);',
  2752. ' hi7:longword=hi(-$123456789ABCD);',
  2753. 'var',
  2754. ' b: Byte;',
  2755. ' ss: shortint;',
  2756. ' w: Word;',
  2757. ' si: SmallInt;',
  2758. ' lw: LongWord;',
  2759. ' li: LongInt;',
  2760. ' b2: Byte;',
  2761. ' ni: nativeint;',
  2762. 'begin',
  2763. ' w := $1234;',
  2764. ' ss := -$12;',
  2765. ' b := lo(ss);',
  2766. ' b := HI(ss);',
  2767. ' b := lo(w);',
  2768. ' b := HI(w);',
  2769. ' b2 := lo(b);',
  2770. ' b2 := hi(b);',
  2771. ' lw := $1234CDEF;',
  2772. ' w := lo(lw);',
  2773. ' w := hi(lw);',
  2774. ' ni := $123456789ABCD;',
  2775. ' lw := lo(ni);',
  2776. ' lw := hi(ni);',
  2777. '']);
  2778. ConvertProgram;
  2779. CheckSource('TestLoHiFpcMode',
  2780. LinesToStr([ // statements
  2781. 'this.LoByte1 = 0x1234 & 0xFF;',
  2782. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2783. 'this.LoByte2 = 0x1234 & 0xFF;',
  2784. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2785. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2786. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2787. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2788. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2789. 'this.lo4 = 0x34 & 0xF;',
  2790. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2791. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2792. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2793. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2794. 'this.hi6 = 74565 >>> 0;',
  2795. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2796. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2797. 'this.b = 0;',
  2798. 'this.ss = 0;',
  2799. 'this.w = 0;',
  2800. 'this.si = 0;',
  2801. 'this.lw = 0;',
  2802. 'this.li = 0;',
  2803. 'this.b2 = 0;',
  2804. 'this.ni = 0;',
  2805. '']),
  2806. LinesToStr([ // this.$main
  2807. '$mod.w = 0x1234;',
  2808. '$mod.ss = -0x12;',
  2809. '$mod.b = $mod.ss & 0xFF;',
  2810. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2811. '$mod.b = $mod.w & 0xFF;',
  2812. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2813. '$mod.b2 = $mod.b & 0xF;',
  2814. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2815. '$mod.lw = 0x1234CDEF;',
  2816. '$mod.w = $mod.lw & 0xFFFF;',
  2817. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2818. '$mod.ni = 0x123456789ABCD;',
  2819. '$mod.lw = $mod.ni >>> 0;',
  2820. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2821. '']));
  2822. end;
  2823. procedure TTestModule.TestLoHiDelphiMode;
  2824. begin
  2825. StartProgram(false);
  2826. Add([
  2827. '{$mode delphi}',
  2828. 'const',
  2829. ' LoByte1 = Lo(Word($1234));',
  2830. ' HiByte1 = Hi(Word($1234));',
  2831. ' LoByte2 = Lo(SmallInt($1234));',
  2832. ' HiByte2 = Hi(SmallInt($1234));',
  2833. ' LoByte3 = Lo($1234CDEF);',
  2834. ' HiByte3 = Hi($1234CDEF);',
  2835. ' LoByte4 = Lo(-$1234CDEF);',
  2836. ' HiByte4 = Hi(-$1234CDEF);',
  2837. 'var',
  2838. ' b: Byte;',
  2839. ' w: Word;',
  2840. ' si: SmallInt;',
  2841. ' lw: LongWord;',
  2842. ' li: LongInt;',
  2843. 'begin',
  2844. ' w := $1234;',
  2845. ' b := lo(w);',
  2846. ' b := HI(w);',
  2847. ' lw := $1234CDEF;',
  2848. ' b := lo(lw);',
  2849. ' b := hi(lw);',
  2850. '']);
  2851. ConvertProgram;
  2852. CheckSource('TestLoHiDelphiMode',
  2853. LinesToStr([ // statements
  2854. 'this.LoByte1 = 0x1234 & 0xFF;',
  2855. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2856. 'this.LoByte2 = 0x1234 & 0xFF;',
  2857. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2858. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2859. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2860. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2861. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2862. 'this.b = 0;',
  2863. 'this.w = 0;',
  2864. 'this.si = 0;',
  2865. 'this.lw = 0;',
  2866. 'this.li = 0;'
  2867. ]),
  2868. LinesToStr([ // this.$main
  2869. '$mod.w = 0x1234;',
  2870. '$mod.b = $mod.w & 0xFF;',
  2871. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2872. '$mod.lw = 0x1234CDEF;',
  2873. '$mod.b = $mod.lw & 0xFF;',
  2874. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2875. ]));
  2876. end;
  2877. procedure TTestModule.TestAssignments;
  2878. begin
  2879. StartProgram(false);
  2880. Parser.Options:=Parser.Options+[po_cassignments];
  2881. Add('var');
  2882. Add(' Bar:longint;');
  2883. Add('begin');
  2884. Add(' bar:=3;');
  2885. Add(' bar+=4;');
  2886. Add(' bar-=5;');
  2887. Add(' bar*=6;');
  2888. ConvertProgram;
  2889. CheckSource('TestAssignments',
  2890. LinesToStr([ // statements
  2891. 'this.Bar = 0;'
  2892. ]),
  2893. LinesToStr([ // this.$main
  2894. '$mod.Bar=3;',
  2895. '$mod.Bar+=4;',
  2896. '$mod.Bar-=5;',
  2897. '$mod.Bar*=6;'
  2898. ]));
  2899. end;
  2900. procedure TTestModule.TestArithmeticOperators1;
  2901. begin
  2902. StartProgram(false);
  2903. Add('var');
  2904. Add(' vA,vB,vC:longint;');
  2905. Add('begin');
  2906. Add(' va:=1;');
  2907. Add(' vb:=va+va;');
  2908. Add(' vb:=va div vb;');
  2909. Add(' vb:=va mod vb;');
  2910. Add(' vb:=va+va*vb+va div vb;');
  2911. Add(' vc:=-va;');
  2912. Add(' va:=va-vb;');
  2913. Add(' vb:=va;');
  2914. Add(' if va<vb then vc:=va else vc:=vb;');
  2915. ConvertProgram;
  2916. CheckSource('TestArithmeticOperators1',
  2917. LinesToStr([ // statements
  2918. 'this.vA = 0;',
  2919. 'this.vB = 0;',
  2920. 'this.vC = 0;'
  2921. ]),
  2922. LinesToStr([ // this.$main
  2923. '$mod.vA = 1;',
  2924. '$mod.vB = $mod.vA + $mod.vA;',
  2925. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2926. '$mod.vB = $mod.vA % $mod.vB;',
  2927. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2928. '$mod.vC = -$mod.vA;',
  2929. '$mod.vA = $mod.vA - $mod.vB;',
  2930. '$mod.vB = $mod.vA;',
  2931. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2932. ]));
  2933. end;
  2934. procedure TTestModule.TestLogicalOperators;
  2935. begin
  2936. StartProgram(false);
  2937. Add('var');
  2938. Add(' vA,vB,vC:boolean;');
  2939. Add('begin');
  2940. Add(' va:=vb and vc;');
  2941. Add(' va:=vb or vc;');
  2942. Add(' va:=vb xor vc;');
  2943. Add(' va:=true and vc;');
  2944. Add(' va:=(vb and vc) or (va and vb);');
  2945. Add(' va:=not vb;');
  2946. ConvertProgram;
  2947. CheckSource('TestLogicalOperators',
  2948. LinesToStr([ // statements
  2949. 'this.vA = false;',
  2950. 'this.vB = false;',
  2951. 'this.vC = false;'
  2952. ]),
  2953. LinesToStr([ // this.$main
  2954. '$mod.vA = $mod.vB && $mod.vC;',
  2955. '$mod.vA = $mod.vB || $mod.vC;',
  2956. '$mod.vA = $mod.vB ^ $mod.vC;',
  2957. '$mod.vA = true && $mod.vC;',
  2958. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2959. '$mod.vA = !$mod.vB;'
  2960. ]));
  2961. end;
  2962. procedure TTestModule.TestBitwiseOperators;
  2963. begin
  2964. StartProgram(false);
  2965. Add([
  2966. 'var',
  2967. ' vA,vB,vC:longint;',
  2968. ' X,Y,Z: nativeint;',
  2969. 'begin',
  2970. ' va:=vb and vc;',
  2971. ' va:=vb or vc;',
  2972. ' va:=vb xor vc;',
  2973. ' va:=vb shl vc;',
  2974. ' va:=vb shr vc;',
  2975. ' va:=3 and vc;',
  2976. ' va:=(vb and vc) or (va and vb);',
  2977. ' va:=not vb;',
  2978. ' X:=Y and Z;',
  2979. ' X:=Y and va;',
  2980. ' X:=Y or Z;',
  2981. ' X:=Y or va;',
  2982. ' X:=Y xor Z;',
  2983. ' X:=Y xor va;',
  2984. '']);
  2985. ConvertProgram;
  2986. CheckSource('TestBitwiseOperators',
  2987. LinesToStr([ // statements
  2988. 'this.vA = 0;',
  2989. 'this.vB = 0;',
  2990. 'this.vC = 0;',
  2991. 'this.X = 0;',
  2992. 'this.Y = 0;',
  2993. 'this.Z = 0;',
  2994. '']),
  2995. LinesToStr([ // this.$main
  2996. '$mod.vA = $mod.vB & $mod.vC;',
  2997. '$mod.vA = $mod.vB | $mod.vC;',
  2998. '$mod.vA = $mod.vB ^ $mod.vC;',
  2999. '$mod.vA = $mod.vB << $mod.vC;',
  3000. '$mod.vA = $mod.vB >>> $mod.vC;',
  3001. '$mod.vA = 3 & $mod.vC;',
  3002. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3003. '$mod.vA = ~$mod.vB;',
  3004. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3005. '$mod.X = $mod.Y & $mod.vA;',
  3006. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3007. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3008. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3009. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3010. '']));
  3011. end;
  3012. procedure TTestModule.TestBitwiseOperatorsLongword;
  3013. begin
  3014. StartProgram(false);
  3015. Add([
  3016. 'var',
  3017. ' a,b,c:longword;',
  3018. ' i: longint;',
  3019. 'begin',
  3020. ' a:=$12345678;',
  3021. ' b:=$EDCBA987;',
  3022. ' c:=not a;',
  3023. ' c:=a and b;',
  3024. ' c:=a and $ffff0000;',
  3025. ' c:=a or b;',
  3026. ' c:=a or $ff00ff00;',
  3027. ' c:=a xor b;',
  3028. ' c:=a xor $f0f0f0f0;',
  3029. ' c:=a shl 1;',
  3030. ' c:=a shl 16;',
  3031. ' c:=a shl 24;',
  3032. ' c:=a shl b;',
  3033. ' c:=a shr 1;',
  3034. ' c:=a shr 16;',
  3035. ' c:=a shr 24;',
  3036. ' c:=a shr b;',
  3037. ' c:=(b and c) or (a and b);',
  3038. ' c:=i and a;',
  3039. ' c:=i or a;',
  3040. ' c:=i xor a;',
  3041. '']);
  3042. ConvertProgram;
  3043. CheckSource('TestBitwiseOperatorsLongword',
  3044. LinesToStr([ // statements
  3045. 'this.a = 0;',
  3046. 'this.b = 0;',
  3047. 'this.c = 0;',
  3048. 'this.i = 0;',
  3049. '']),
  3050. LinesToStr([ // this.$main
  3051. '$mod.a = 0x12345678;',
  3052. '$mod.b = 0xEDCBA987;',
  3053. '$mod.c = rtl.lw(~$mod.a);',
  3054. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3055. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3056. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3057. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3058. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3059. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3060. '$mod.c = rtl.lw($mod.a << 1);',
  3061. '$mod.c = rtl.lw($mod.a << 16);',
  3062. '$mod.c = rtl.lw($mod.a << 24);',
  3063. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3064. '$mod.c = rtl.lw($mod.a >>> 1);',
  3065. '$mod.c = rtl.lw($mod.a >>> 16);',
  3066. '$mod.c = rtl.lw($mod.a >>> 24);',
  3067. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3068. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3069. '$mod.c = $mod.i & $mod.a;',
  3070. '$mod.c = $mod.i | $mod.a;',
  3071. '$mod.c = $mod.i ^ $mod.a;',
  3072. '']));
  3073. end;
  3074. procedure TTestModule.TestPrgProcVar;
  3075. begin
  3076. StartProgram(false);
  3077. Add('procedure Proc1;');
  3078. Add('type');
  3079. Add(' t1=longint;');
  3080. Add('var');
  3081. Add(' vA:t1;');
  3082. Add('begin');
  3083. Add('end;');
  3084. Add('begin');
  3085. ConvertProgram;
  3086. CheckSource('TestPrgProcVar',
  3087. LinesToStr([ // statements
  3088. 'this.Proc1 = function () {',
  3089. ' var vA=0;',
  3090. '};'
  3091. ]),
  3092. LinesToStr([ // this.$main
  3093. ''
  3094. ]));
  3095. end;
  3096. procedure TTestModule.TestUnitProcVar;
  3097. begin
  3098. StartUnit(false);
  3099. Add('interface');
  3100. Add('');
  3101. Add('type tA=string; // unit scope');
  3102. Add('procedure Proc1;');
  3103. Add('');
  3104. Add('implementation');
  3105. Add('');
  3106. Add('procedure Proc1;');
  3107. Add('type tA=longint; // local proc scope');
  3108. Add('var v1:tA; // using local tA');
  3109. Add('begin');
  3110. Add('end;');
  3111. Add('var v2:tA; // using interface tA');
  3112. ConvertUnit;
  3113. CheckSource('TestUnitProcVar',
  3114. LinesToStr([ // statements
  3115. 'var $impl = $mod.$impl;',
  3116. 'this.Proc1 = function () {',
  3117. ' var v1 = 0;',
  3118. '};',
  3119. '']),
  3120. // this.$init
  3121. '',
  3122. // implementation
  3123. LinesToStr([
  3124. '$impl.v2 = "";',
  3125. '']));
  3126. end;
  3127. procedure TTestModule.TestImplProc;
  3128. begin
  3129. StartUnit(false);
  3130. Add('interface');
  3131. Add('');
  3132. Add('procedure Proc1;');
  3133. Add('');
  3134. Add('implementation');
  3135. Add('');
  3136. Add('procedure Proc1; begin end;');
  3137. Add('procedure Proc2; begin end;');
  3138. Add('initialization');
  3139. Add(' Proc1;');
  3140. Add(' Proc2;');
  3141. ConvertUnit;
  3142. CheckSource('TestImplProc',
  3143. LinesToStr([ // statements
  3144. 'var $impl = $mod.$impl;',
  3145. 'this.Proc1 = function () {',
  3146. '};',
  3147. '']),
  3148. LinesToStr([ // this.$init
  3149. '$mod.Proc1();',
  3150. '$impl.Proc2();',
  3151. '']),
  3152. LinesToStr([ // implementation
  3153. '$impl.Proc2 = function () {',
  3154. '};',
  3155. ''])
  3156. );
  3157. end;
  3158. procedure TTestModule.TestFunctionResult;
  3159. begin
  3160. StartProgram(false);
  3161. Add('function Func1: longint;');
  3162. Add('begin');
  3163. Add(' Result:=3;');
  3164. Add(' Func1:=4;');
  3165. Add('end;');
  3166. Add('begin');
  3167. ConvertProgram;
  3168. CheckSource('TestFunctionResult',
  3169. LinesToStr([ // statements
  3170. 'this.Func1 = function () {',
  3171. ' var Result = 0;',
  3172. ' Result = 3;',
  3173. ' Result = 4;',
  3174. ' return Result;',
  3175. '};'
  3176. ]),
  3177. '');
  3178. end;
  3179. procedure TTestModule.TestNestedProc;
  3180. begin
  3181. StartProgram(false);
  3182. Add([
  3183. 'var vInUnit: longint;',
  3184. 'function DoIt(pA,pD: longint): longint;',
  3185. 'var',
  3186. ' vB: longint;',
  3187. ' vC: longint;',
  3188. ' function Nesty(pA: longint): longint; ',
  3189. ' var vB: longint;',
  3190. ' begin',
  3191. ' Result:=pa+vb+vc+pd+vInUnit;',
  3192. ' nesty:=3;',
  3193. ' doit:=4;',
  3194. ' exit;',
  3195. ' end;',
  3196. 'begin',
  3197. ' Result:=pa+vb+vc;',
  3198. ' doit:=6;',
  3199. ' exit;',
  3200. 'end;',
  3201. 'begin']);
  3202. ConvertProgram;
  3203. CheckSource('TestNestedProc',
  3204. LinesToStr([ // statements
  3205. 'this.vInUnit = 0;',
  3206. 'this.DoIt = function (pA, pD) {',
  3207. ' var Result = 0;',
  3208. ' var vB = 0;',
  3209. ' var vC = 0;',
  3210. ' function Nesty(pA) {',
  3211. ' var Result$1 = 0;',
  3212. ' var vB = 0;',
  3213. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3214. ' Result$1 = 3;',
  3215. ' Result = 4;',
  3216. ' return Result$1;',
  3217. ' return Result$1;',
  3218. ' };',
  3219. ' Result = pA + vB + vC;',
  3220. ' Result = 6;',
  3221. ' return Result;',
  3222. ' return Result;',
  3223. '};'
  3224. ]),
  3225. '');
  3226. end;
  3227. procedure TTestModule.TestNestedProc_ResultString;
  3228. begin
  3229. StartProgram(false);
  3230. Add([
  3231. 'function DoIt: string;',
  3232. ' function Nesty: string; ',
  3233. ' begin',
  3234. ' nesty:=#65#66;',
  3235. ' nesty[1]:=#67;',
  3236. ' doit:=#68;',
  3237. ' doit[2]:=#69;',
  3238. ' end;',
  3239. 'begin',
  3240. ' doit:=#70;',
  3241. ' doit[3]:=#71;',
  3242. 'end;',
  3243. 'begin']);
  3244. ConvertProgram;
  3245. CheckSource('TestNestedProc_ResultString',
  3246. LinesToStr([ // statements
  3247. 'this.DoIt = function () {',
  3248. ' var Result = "";',
  3249. ' function Nesty() {',
  3250. ' var Result$1 = "";',
  3251. ' Result$1 = "AB";',
  3252. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3253. ' Result = "D";',
  3254. ' Result = rtl.setCharAt(Result, 1, "E");',
  3255. ' return Result$1;',
  3256. ' };',
  3257. ' Result = "F";',
  3258. ' Result = rtl.setCharAt(Result, 2, "G");',
  3259. ' return Result;',
  3260. '};'
  3261. ]),
  3262. '');
  3263. end;
  3264. procedure TTestModule.TestForwardProc;
  3265. begin
  3266. StartProgram(false);
  3267. Add('procedure FuncA(Bar: longint); forward;');
  3268. Add('procedure FuncB(Bar: longint);');
  3269. Add('begin');
  3270. Add(' funca(bar);');
  3271. Add('end;');
  3272. Add('procedure funca(bar: longint);');
  3273. Add('begin');
  3274. Add(' if bar=3 then ;');
  3275. Add('end;');
  3276. Add('begin');
  3277. Add(' funca(4);');
  3278. Add(' funcb(5);');
  3279. ConvertProgram;
  3280. CheckSource('TestForwardProc',
  3281. LinesToStr([ // statements'
  3282. 'this.FuncB = function (Bar) {',
  3283. ' $mod.FuncA(Bar);',
  3284. '};',
  3285. 'this.FuncA = function (Bar) {',
  3286. ' if (Bar === 3);',
  3287. '};'
  3288. ]),
  3289. LinesToStr([
  3290. '$mod.FuncA(4);',
  3291. '$mod.FuncB(5);'
  3292. ])
  3293. );
  3294. end;
  3295. procedure TTestModule.TestNestedForwardProc;
  3296. begin
  3297. StartProgram(false);
  3298. Add('procedure FuncA;');
  3299. Add(' procedure FuncB(i: longint); forward;');
  3300. Add(' procedure FuncC(i: longint);');
  3301. Add(' begin');
  3302. Add(' funcb(i);');
  3303. Add(' end;');
  3304. Add(' procedure FuncB(i: longint);');
  3305. Add(' begin');
  3306. Add(' if i=3 then ;');
  3307. Add(' end;');
  3308. Add('begin');
  3309. Add(' funcc(4)');
  3310. Add('end;');
  3311. Add('begin');
  3312. Add(' funca;');
  3313. ConvertProgram;
  3314. CheckSource('TestNestedForwardProc',
  3315. LinesToStr([ // statements'
  3316. 'this.FuncA = function () {',
  3317. ' function FuncC(i) {',
  3318. ' FuncB(i);',
  3319. ' };',
  3320. ' function FuncB(i) {',
  3321. ' if (i === 3);',
  3322. ' };',
  3323. ' FuncC(4);',
  3324. '};'
  3325. ]),
  3326. LinesToStr([
  3327. '$mod.FuncA();'
  3328. ])
  3329. );
  3330. end;
  3331. procedure TTestModule.TestAssignFunctionResult;
  3332. begin
  3333. StartProgram(false);
  3334. Add('function Func1: longint;');
  3335. Add('begin');
  3336. Add('end;');
  3337. Add('var i: longint;');
  3338. Add('begin');
  3339. Add(' i:=func1();');
  3340. Add(' i:=func1()+func1();');
  3341. ConvertProgram;
  3342. CheckSource('TestAssignFunctionResult',
  3343. LinesToStr([ // statements
  3344. 'this.Func1 = function () {',
  3345. ' var Result = 0;',
  3346. ' return Result;',
  3347. '};',
  3348. 'this.i = 0;'
  3349. ]),
  3350. LinesToStr([
  3351. '$mod.i = $mod.Func1();',
  3352. '$mod.i = $mod.Func1() + $mod.Func1();'
  3353. ]));
  3354. end;
  3355. procedure TTestModule.TestFunctionResultInCondition;
  3356. begin
  3357. StartProgram(false);
  3358. Add('function Func1: longint;');
  3359. Add('begin');
  3360. Add('end;');
  3361. Add('function Func2: boolean;');
  3362. Add('begin');
  3363. Add('end;');
  3364. Add('var i: longint;');
  3365. Add('begin');
  3366. Add(' if func2 then ;');
  3367. Add(' if i=func1() then ;');
  3368. Add(' if i=func1 then ;');
  3369. ConvertProgram;
  3370. CheckSource('TestFunctionResultInCondition',
  3371. LinesToStr([ // statements
  3372. 'this.Func1 = function () {',
  3373. ' var Result = 0;',
  3374. ' return Result;',
  3375. '};',
  3376. 'this.Func2 = function () {',
  3377. ' var Result = false;',
  3378. ' return Result;',
  3379. '};',
  3380. 'this.i = 0;'
  3381. ]),
  3382. LinesToStr([
  3383. 'if ($mod.Func2());',
  3384. 'if ($mod.i === $mod.Func1());',
  3385. 'if ($mod.i === $mod.Func1());'
  3386. ]));
  3387. end;
  3388. procedure TTestModule.TestFunctionResultInForLoop;
  3389. begin
  3390. StartProgram(false);
  3391. Add([
  3392. 'function Func1(a: array of longint): longint;',
  3393. 'begin',
  3394. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3395. ' for Result in a do if a[Result]=0 then exit;',
  3396. 'end;',
  3397. 'begin',
  3398. ' Func1([1,2,3])']);
  3399. ConvertProgram;
  3400. CheckSource('TestFunctionResultInForLoop',
  3401. LinesToStr([ // statements
  3402. 'this.Func1 = function (a) {',
  3403. ' var Result = 0;',
  3404. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3405. ' Result = $l1;',
  3406. ' if (a[Result] === 0) return Result;',
  3407. ' };',
  3408. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3409. ' Result = $in2[$l3];',
  3410. ' if (a[Result] === 0) return Result;',
  3411. ' };',
  3412. ' return Result;',
  3413. '};',
  3414. '']),
  3415. LinesToStr([
  3416. '$mod.Func1([1, 2, 3]);'
  3417. ]));
  3418. end;
  3419. procedure TTestModule.TestFunctionResultInTypeCast;
  3420. begin
  3421. StartProgram(false);
  3422. Add([
  3423. 'function GetInt: longint;',
  3424. 'begin',
  3425. 'end;',
  3426. 'begin',
  3427. ' if Byte(GetInt)=0 then ;',
  3428. '']);
  3429. ConvertProgram;
  3430. CheckSource('TestFunctionResultInTypeCast',
  3431. LinesToStr([ // statements
  3432. 'this.GetInt = function () {',
  3433. ' var Result = 0;',
  3434. ' return Result;',
  3435. '};',
  3436. '']),
  3437. LinesToStr([
  3438. 'if (($mod.GetInt() & 255) === 0) ;'
  3439. ]));
  3440. end;
  3441. procedure TTestModule.TestExit;
  3442. begin
  3443. StartProgram(false);
  3444. Add('procedure ProcA;');
  3445. Add('begin');
  3446. Add(' exit;');
  3447. Add('end;');
  3448. Add('function FuncB: longint;');
  3449. Add('begin');
  3450. Add(' exit;');
  3451. Add(' exit(3);');
  3452. Add('end;');
  3453. Add('function FuncC: string;');
  3454. Add('begin');
  3455. Add(' exit;');
  3456. Add(' exit(''a'');');
  3457. Add(' exit(''abc'');');
  3458. Add('end;');
  3459. Add('begin');
  3460. Add(' exit;');
  3461. Add(' exit(1);');
  3462. ConvertProgram;
  3463. CheckSource('TestExit',
  3464. LinesToStr([ // statements
  3465. 'this.ProcA = function () {',
  3466. ' return;',
  3467. '};',
  3468. 'this.FuncB = function () {',
  3469. ' var Result = 0;',
  3470. ' return Result;',
  3471. ' return 3;',
  3472. ' return Result;',
  3473. '};',
  3474. 'this.FuncC = function () {',
  3475. ' var Result = "";',
  3476. ' return Result;',
  3477. ' return "a";',
  3478. ' return "abc";',
  3479. ' return Result;',
  3480. '};'
  3481. ]),
  3482. LinesToStr([
  3483. 'return;',
  3484. 'return 1;',
  3485. '']));
  3486. end;
  3487. procedure TTestModule.TestBreak;
  3488. begin
  3489. StartProgram(false);
  3490. Add([
  3491. 'var',
  3492. ' i: longint;',
  3493. 'begin',
  3494. ' repeat',
  3495. ' break;',
  3496. ' until true;',
  3497. ' while true do',
  3498. ' break;',
  3499. ' for i:=1 to 2 do',
  3500. ' break;']);
  3501. ConvertProgram;
  3502. CheckSource('TestBreak',
  3503. LinesToStr([ // statements
  3504. 'this.i = 0;'
  3505. ]),
  3506. LinesToStr([
  3507. 'do {',
  3508. ' break;',
  3509. '} while (!true);',
  3510. 'while (true) break;',
  3511. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3512. '']));
  3513. end;
  3514. procedure TTestModule.TestBreakAsVar;
  3515. begin
  3516. StartProgram(false);
  3517. Add([
  3518. 'procedure DoIt(break: boolean);',
  3519. 'begin',
  3520. ' if break then ;',
  3521. 'end;',
  3522. 'var',
  3523. ' break: boolean;',
  3524. 'begin',
  3525. ' if break then ;']);
  3526. ConvertProgram;
  3527. CheckSource('TestBreakAsVar',
  3528. LinesToStr([ // statements
  3529. 'this.DoIt = function (Break) {',
  3530. ' if (Break) ;',
  3531. '};',
  3532. 'this.Break = false;',
  3533. '']),
  3534. LinesToStr([
  3535. 'if($mod.Break) ;',
  3536. '']));
  3537. end;
  3538. procedure TTestModule.TestContinue;
  3539. begin
  3540. StartProgram(false);
  3541. Add('var i: longint;');
  3542. Add('begin');
  3543. Add(' repeat');
  3544. Add(' continue;');
  3545. Add(' until true;');
  3546. Add(' while true do');
  3547. Add(' continue;');
  3548. Add(' for i:=1 to 2 do');
  3549. Add(' continue;');
  3550. ConvertProgram;
  3551. CheckSource('TestContinue',
  3552. LinesToStr([ // statements
  3553. 'this.i = 0;'
  3554. ]),
  3555. LinesToStr([
  3556. 'do {',
  3557. ' continue;',
  3558. '} while (!true);',
  3559. 'while (true) continue;',
  3560. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3561. '']));
  3562. end;
  3563. procedure TTestModule.TestProc_External;
  3564. begin
  3565. StartProgram(false);
  3566. Add('procedure Foo; external name ''console.log'';');
  3567. Add('function Bar: longint; external name ''get.item'';');
  3568. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3569. Add('var');
  3570. Add(' i: longint;');
  3571. Add('begin');
  3572. Add(' Foo;');
  3573. Add(' i:=Bar;');
  3574. Add(' i:=Bla(''abc'');');
  3575. ConvertProgram;
  3576. CheckSource('TestProc_External',
  3577. LinesToStr([ // statements
  3578. 'this.i = 0;'
  3579. ]),
  3580. LinesToStr([
  3581. 'console.log();',
  3582. '$mod.i = get.item();',
  3583. '$mod.i = apply.something("abc");'
  3584. ]));
  3585. end;
  3586. procedure TTestModule.TestProc_ExternalOtherUnit;
  3587. begin
  3588. AddModuleWithIntfImplSrc('unit2.pas',
  3589. LinesToStr([
  3590. 'procedure Now; external name ''Date.now'';',
  3591. 'procedure DoIt;'
  3592. ]),
  3593. 'procedure doit; begin end;');
  3594. StartUnit(true);
  3595. Add('interface');
  3596. Add('uses unit2;');
  3597. Add('implementation');
  3598. Add('begin');
  3599. Add(' now;');
  3600. Add(' now();');
  3601. Add(' uNit2.now;');
  3602. Add(' uNit2.now();');
  3603. Add(' doit;');
  3604. Add(' uNit2.doit;');
  3605. ConvertUnit;
  3606. CheckSource('TestProc_ExternalOtherUnit',
  3607. LinesToStr([
  3608. '']),
  3609. LinesToStr([
  3610. 'Date.now();',
  3611. 'Date.now();',
  3612. 'Date.now();',
  3613. 'Date.now();',
  3614. 'pas.unit2.DoIt();',
  3615. 'pas.unit2.DoIt();',
  3616. '']));
  3617. end;
  3618. procedure TTestModule.TestProc_Asm;
  3619. begin
  3620. StartProgram(false);
  3621. Add([
  3622. '{$mode delphi}',
  3623. 'function DoIt: longint;',
  3624. 'begin;',
  3625. ' asm',
  3626. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3627. ' end;',
  3628. ' asm console.log(); end;',
  3629. ' asm',
  3630. ' s = "'' ";',
  3631. ' s = ''" '';',
  3632. ' s = s + "world" + "''";',
  3633. ' // end',
  3634. ' s = ''end'';',
  3635. ' s = "end";',
  3636. ' s = "foo\"bar";',
  3637. ' s = ''a\''b'';',
  3638. ' s = `${expr}\`-"-''-`;',
  3639. ' s = `multi',
  3640. 'line`;',
  3641. ' end;',
  3642. 'end;',
  3643. 'procedure Fly;',
  3644. 'asm',
  3645. ' return;',
  3646. 'end;',
  3647. 'begin']);
  3648. ConvertProgram;
  3649. CheckSource('TestProc_Asm',
  3650. LinesToStr([ // statements
  3651. 'this.DoIt = function () {',
  3652. ' var Result = 0;',
  3653. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3654. ' console.log();',
  3655. ' s = "'' ";',
  3656. ' s = ''" '';',
  3657. ' s = s + "world" + "''";',
  3658. ' // end',
  3659. ' s = ''end'';',
  3660. ' s = "end";',
  3661. ' s = "foo\"bar";',
  3662. ' s = ''a\''b'';',
  3663. ' s = `${expr}\`-"-''-`;',
  3664. ' s = `multi',
  3665. 'line`;',
  3666. ' return Result;',
  3667. '};',
  3668. 'this.Fly = function () {',
  3669. ' return;',
  3670. '};',
  3671. '']),
  3672. LinesToStr([
  3673. ''
  3674. ]));
  3675. end;
  3676. procedure TTestModule.TestProc_Assembler;
  3677. begin
  3678. StartProgram(false);
  3679. Add('function DoIt: longint; assembler;');
  3680. Add('asm');
  3681. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3682. Add('end;');
  3683. Add('begin');
  3684. ConvertProgram;
  3685. CheckSource('TestProc_Assembler',
  3686. LinesToStr([ // statements
  3687. 'this.DoIt = function () {',
  3688. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3689. '};'
  3690. ]),
  3691. LinesToStr([
  3692. ''
  3693. ]));
  3694. end;
  3695. procedure TTestModule.TestProc_VarParam;
  3696. begin
  3697. StartProgram(false);
  3698. Add('type integer = longint;');
  3699. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3700. Add('var vJ: integer;');
  3701. Add('begin');
  3702. Add(' vg:=vg+1;');
  3703. Add(' vj:=vh+2;');
  3704. Add(' vi:=vi+3;');
  3705. Add(' doit(vg,vg,vg);');
  3706. Add(' doit(vh,vh,vj);');
  3707. Add(' doit(vi,vi,vi);');
  3708. Add(' doit(vj,vj,vj);');
  3709. Add('end;');
  3710. Add('var i: integer;');
  3711. Add('begin');
  3712. Add(' doit(i,i,i);');
  3713. ConvertProgram;
  3714. CheckSource('TestProc_VarParam',
  3715. LinesToStr([ // statements
  3716. 'this.DoIt = function (vG,vH,vI) {',
  3717. ' var vJ = 0;',
  3718. ' vG = vG + 1;',
  3719. ' vJ = vH + 2;',
  3720. ' vI.set(vI.get()+3);',
  3721. ' $mod.DoIt(vG, vG, {',
  3722. ' get: function () {',
  3723. ' return vG;',
  3724. ' },',
  3725. ' set: function (v) {',
  3726. ' vG = v;',
  3727. ' }',
  3728. ' });',
  3729. ' $mod.DoIt(vH, vH, {',
  3730. ' get: function () {',
  3731. ' return vJ;',
  3732. ' },',
  3733. ' set: function (v) {',
  3734. ' vJ = v;',
  3735. ' }',
  3736. ' });',
  3737. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3738. ' $mod.DoIt(vJ, vJ, {',
  3739. ' get: function () {',
  3740. ' return vJ;',
  3741. ' },',
  3742. ' set: function (v) {',
  3743. ' vJ = v;',
  3744. ' }',
  3745. ' });',
  3746. '};',
  3747. 'this.i = 0;'
  3748. ]),
  3749. LinesToStr([
  3750. '$mod.DoIt($mod.i,$mod.i,{',
  3751. ' p: $mod,',
  3752. ' get: function () {',
  3753. ' return this.p.i;',
  3754. ' },',
  3755. ' set: function (v) {',
  3756. ' this.p.i = v;',
  3757. ' }',
  3758. '});'
  3759. ]));
  3760. end;
  3761. procedure TTestModule.TestProc_VarParamString;
  3762. begin
  3763. StartProgram(false);
  3764. Add(['type TCaption = string;',
  3765. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3766. 'var c: char;',
  3767. 'begin',
  3768. ' va[1]:=c;',
  3769. ' vb[2]:=c;',
  3770. ' vc[3]:=c;',
  3771. 'end;',
  3772. 'begin']);
  3773. ConvertProgram;
  3774. CheckSource('TestProc_VarParamString',
  3775. LinesToStr([ // statements
  3776. 'this.DoIt = function (vA,vB,vC) {',
  3777. ' var c = "";',
  3778. ' vA = rtl.setCharAt(vA, 0, c);',
  3779. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3780. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3781. '};',
  3782. '']),
  3783. LinesToStr([
  3784. ]));
  3785. end;
  3786. procedure TTestModule.TestProc_VarParamV;
  3787. begin
  3788. StartProgram(false);
  3789. Add([
  3790. 'procedure Inc2(var i: longint);',
  3791. 'begin',
  3792. ' i:=i+2;',
  3793. 'end;',
  3794. 'procedure DoIt(v: longint);',
  3795. 'var p: array of longint;',
  3796. 'begin',
  3797. ' Inc2(v);',
  3798. ' Inc2(p[v]);',
  3799. 'end;',
  3800. 'begin']);
  3801. ConvertProgram;
  3802. CheckSource('TestProc_VarParamV',
  3803. LinesToStr([ // statements
  3804. 'this.Inc2 = function (i) {',
  3805. ' i.set(i.get()+2);',
  3806. '};',
  3807. 'this.DoIt = function (v) {',
  3808. ' var p = [];',
  3809. ' $mod.Inc2({get: function () {',
  3810. ' return v;',
  3811. ' }, set: function (w) {',
  3812. ' v = w;',
  3813. ' }});',
  3814. ' $mod.Inc2({',
  3815. ' a: v,',
  3816. ' p: p,',
  3817. ' get: function () {',
  3818. ' return this.p[this.a];',
  3819. ' },',
  3820. ' set: function (v) {',
  3821. ' this.p[this.a] = v;',
  3822. ' }',
  3823. ' });',
  3824. '};',
  3825. '']),
  3826. LinesToStr([
  3827. '']));
  3828. end;
  3829. procedure TTestModule.TestProc_Overload;
  3830. begin
  3831. StartProgram(false);
  3832. Add('procedure DoIt(vI: longint); begin end;');
  3833. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3834. Add('procedure DoIt(vD: double); begin end;');
  3835. Add('begin');
  3836. Add(' DoIt(1);');
  3837. Add(' DoIt(2,3);');
  3838. Add(' DoIt(4.5);');
  3839. ConvertProgram;
  3840. CheckSource('TestProcedureOverload',
  3841. LinesToStr([ // statements
  3842. 'this.DoIt = function (vI) {',
  3843. '};',
  3844. 'this.DoIt$1 = function (vI, vJ) {',
  3845. '};',
  3846. 'this.DoIt$2 = function (vD) {',
  3847. '};',
  3848. '']),
  3849. LinesToStr([
  3850. '$mod.DoIt(1);',
  3851. '$mod.DoIt$1(2, 3);',
  3852. '$mod.DoIt$2(4.5);',
  3853. '']));
  3854. end;
  3855. procedure TTestModule.TestProc_OverloadForward;
  3856. begin
  3857. StartProgram(false);
  3858. Add('procedure DoIt(vI: longint); forward;');
  3859. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3860. Add('procedure doit(vi: longint); begin end;');
  3861. Add('begin');
  3862. Add(' doit(1);');
  3863. Add(' doit(2,3);');
  3864. ConvertProgram;
  3865. CheckSource('TestProcedureOverloadForward',
  3866. LinesToStr([ // statements
  3867. 'this.DoIt$1 = function (vI, vJ) {',
  3868. '};',
  3869. 'this.DoIt = function (vI) {',
  3870. '};',
  3871. '']),
  3872. LinesToStr([
  3873. '$mod.DoIt(1);',
  3874. '$mod.DoIt$1(2, 3);',
  3875. '']));
  3876. end;
  3877. procedure TTestModule.TestProc_OverloadIntfImpl;
  3878. begin
  3879. StartUnit(false);
  3880. Add('interface');
  3881. Add('procedure DoIt(vI: longint);');
  3882. Add('procedure DoIt(vI, vJ: longint);');
  3883. Add('implementation');
  3884. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3885. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3886. Add('procedure DoIt(vi: longint); begin end;');
  3887. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3888. Add('procedure DoIt(vi, vj: longint); begin end;');
  3889. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3890. Add('begin');
  3891. Add(' doit(1);');
  3892. Add(' doit(2,3);');
  3893. Add(' doit(4,5,6);');
  3894. Add(' doit(7,8,9,10);');
  3895. Add(' doit(11,12,13,14,15);');
  3896. ConvertUnit;
  3897. CheckSource('TestProcedureOverloadUnit',
  3898. LinesToStr([ // statements
  3899. 'var $impl = $mod.$impl;',
  3900. 'this.DoIt = function (vI) {',
  3901. '};',
  3902. 'this.DoIt$1 = function (vI, vJ) {',
  3903. '};',
  3904. '']),
  3905. LinesToStr([ // this.$init
  3906. '$mod.DoIt(1);',
  3907. '$mod.DoIt$1(2, 3);',
  3908. '$impl.DoIt$3(4,5,6);',
  3909. '$impl.DoIt$4(7,8,9,10);',
  3910. '$impl.DoIt$2(11,12,13,14,15);',
  3911. '']),
  3912. LinesToStr([ // implementation
  3913. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3914. '};',
  3915. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3916. '};',
  3917. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3918. '};',
  3919. '']));
  3920. end;
  3921. procedure TTestModule.TestProc_OverloadNested;
  3922. begin
  3923. StartProgram(false);
  3924. Add([
  3925. 'procedure doit(vA: longint);',
  3926. ' procedure DoIt(vA, vB: longint); overload;',
  3927. ' begin',
  3928. ' doit(1);',
  3929. ' doit(1,2);',
  3930. ' end;',
  3931. ' procedure doit(vA, vB, vC: longint);',
  3932. ' begin',
  3933. ' doit(1);',
  3934. ' doit(1,2);',
  3935. ' doit(1,2,3);',
  3936. ' end;',
  3937. 'begin',
  3938. ' doit(1);',
  3939. ' doit(1,2);',
  3940. ' doit(1,2,3);',
  3941. 'end;',
  3942. 'begin // main',
  3943. ' doit(1);']);
  3944. ConvertProgram;
  3945. CheckSource('TestProcedureOverloadNested',
  3946. LinesToStr([ // statements
  3947. 'this.doit = function (vA) {',
  3948. ' function DoIt$1(vA, vB) {',
  3949. ' $mod.doit(1);',
  3950. ' DoIt$1(1, 2);',
  3951. ' };',
  3952. ' function doit$2(vA, vB, vC) {',
  3953. ' $mod.doit(1);',
  3954. ' DoIt$1(1, 2);',
  3955. ' doit$2(1, 2, 3);',
  3956. ' };',
  3957. ' $mod.doit(1);',
  3958. ' DoIt$1(1, 2);',
  3959. ' doit$2(1, 2, 3);',
  3960. '};',
  3961. '']),
  3962. LinesToStr([
  3963. '$mod.doit(1);',
  3964. '']));
  3965. end;
  3966. procedure TTestModule.TestProc_OverloadNestedForward;
  3967. begin
  3968. StartProgram(false);
  3969. Add([
  3970. 'procedure DoIt(vA: longint); overload; forward;',
  3971. 'procedure DoIt(vB, vC: longint); overload;',
  3972. 'begin // 2 param overload',
  3973. ' doit(1);',
  3974. ' doit(1,2);',
  3975. 'end;',
  3976. 'procedure doit(vA: longint);',
  3977. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3978. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3979. ' begin // 4 param overload',
  3980. ' doit(1);',
  3981. ' doit(1,2);',
  3982. ' doit(1,2,3);',
  3983. ' doit(1,2,3,4);',
  3984. ' end;',
  3985. ' procedure doit(vA, vB, vC: longint);',
  3986. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3987. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3988. ' begin // 6 param overload',
  3989. ' doit(1);',
  3990. ' doit(1,2);',
  3991. ' doit(1,2,3);',
  3992. ' doit(1,2,3,4);',
  3993. ' doit(1,2,3,4,5);',
  3994. ' doit(1,2,3,4,5,6);',
  3995. ' end;',
  3996. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3997. ' begin // 5 param overload',
  3998. ' doit(1);',
  3999. ' doit(1,2);',
  4000. ' doit(1,2,3);',
  4001. ' doit(1,2,3,4);',
  4002. ' doit(1,2,3,4,5);',
  4003. ' doit(1,2,3,4,5,6);',
  4004. ' end;',
  4005. ' begin // 3 param overload',
  4006. ' doit(1);',
  4007. ' doit(1,2);',
  4008. ' doit(1,2,3);',
  4009. ' doit(1,2,3,4);',
  4010. ' doit(1,2,3,4,5);',
  4011. ' doit(1,2,3,4,5,6);',
  4012. ' end;',
  4013. 'begin // 1 param overload',
  4014. ' doit(1);',
  4015. ' doit(1,2);',
  4016. ' doit(1,2,3);',
  4017. ' doit(1,2,3,4);',
  4018. 'end;',
  4019. 'begin // main',
  4020. ' doit(1);',
  4021. ' doit(1,2);']);
  4022. ConvertProgram;
  4023. CheckSource('TestProc_OverloadNestedForward',
  4024. LinesToStr([ // statements
  4025. 'this.DoIt$1 = function (vB, vC) {',
  4026. ' $mod.DoIt(1);',
  4027. ' $mod.DoIt$1(1, 2);',
  4028. '};',
  4029. 'this.DoIt = function (vA) {',
  4030. ' function DoIt$3(vA, vB, vC, vD) {',
  4031. ' $mod.DoIt(1);',
  4032. ' $mod.DoIt$1(1, 2);',
  4033. ' DoIt$2(1, 2, 3);',
  4034. ' DoIt$3(1, 2, 3, 4);',
  4035. ' };',
  4036. ' function DoIt$2(vA, vB, vC) {',
  4037. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4038. ' $mod.DoIt(1);',
  4039. ' $mod.DoIt$1(1, 2);',
  4040. ' DoIt$2(1, 2, 3);',
  4041. ' DoIt$3(1, 2, 3, 4);',
  4042. ' DoIt$4(1, 2, 3, 4, 5);',
  4043. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4044. ' };',
  4045. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4046. ' $mod.DoIt(1);',
  4047. ' $mod.DoIt$1(1, 2);',
  4048. ' DoIt$2(1, 2, 3);',
  4049. ' DoIt$3(1, 2, 3, 4);',
  4050. ' DoIt$4(1, 2, 3, 4, 5);',
  4051. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4052. ' };',
  4053. ' $mod.DoIt(1);',
  4054. ' $mod.DoIt$1(1, 2);',
  4055. ' DoIt$2(1, 2, 3);',
  4056. ' DoIt$3(1, 2, 3, 4);',
  4057. ' DoIt$4(1, 2, 3, 4, 5);',
  4058. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4059. ' };',
  4060. ' $mod.DoIt(1);',
  4061. ' $mod.DoIt$1(1, 2);',
  4062. ' DoIt$2(1, 2, 3);',
  4063. ' DoIt$3(1, 2, 3, 4);',
  4064. '};',
  4065. '']),
  4066. LinesToStr([
  4067. '$mod.DoIt(1);',
  4068. '$mod.DoIt$1(1, 2);',
  4069. '']));
  4070. end;
  4071. procedure TTestModule.TestProc_OverloadUnitCycle;
  4072. begin
  4073. AddModuleWithIntfImplSrc('Unit2.pas',
  4074. LinesToStr([
  4075. 'type',
  4076. ' TObject = class',
  4077. ' procedure DoIt(b: boolean); virtual; abstract;',
  4078. ' procedure DoIt(i: longint); virtual; abstract;',
  4079. ' end;',
  4080. '']),
  4081. 'uses test1;');
  4082. StartUnit(true);
  4083. Add([
  4084. 'interface',
  4085. 'uses unit2;',
  4086. 'type',
  4087. ' TEagle = class(TObject)',
  4088. ' procedure DoIt(b: boolean); override;',
  4089. ' procedure DoIt(i: longint); override;',
  4090. ' end;',
  4091. 'implementation',
  4092. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4093. 'procedure TEagle.DoIt(i: longint); begin end;',
  4094. '']);
  4095. ConvertUnit;
  4096. CheckSource('TestProc_OverloadUnitCycle',
  4097. LinesToStr([ // statements
  4098. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4099. ' this.DoIt = function (b) {',
  4100. ' };',
  4101. ' this.DoIt$1 = function (i) {',
  4102. ' };',
  4103. '});',
  4104. '']),
  4105. '',
  4106. LinesToStr([
  4107. '']));
  4108. end;
  4109. procedure TTestModule.TestProc_Varargs;
  4110. begin
  4111. StartProgram(false);
  4112. Add([
  4113. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4114. 'procedure ProcB; varargs; external name ''ProcB'';',
  4115. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4116. 'function GetIt: longint; begin end;',
  4117. 'begin',
  4118. ' ProcA(1);',
  4119. ' ProcA(1,2);',
  4120. ' ProcA(1,2.0);',
  4121. ' ProcA(1,2,3);',
  4122. ' ProcA(1,''2'');',
  4123. ' ProcA(2,'''');',
  4124. ' ProcA(3,false);',
  4125. ' ProcB;',
  4126. ' ProcB();',
  4127. ' ProcB(4);',
  4128. ' ProcB(''foo'');',
  4129. ' ProcC;',
  4130. ' ProcC();',
  4131. ' ProcC(4);',
  4132. ' ProcC(5,''foo'');',
  4133. ' ProcB(GetIt);',
  4134. ' ProcB(GetIt());',
  4135. ' ProcB(GetIt,GetIt());']);
  4136. ConvertProgram;
  4137. CheckSource('TestProc_Varargs',
  4138. LinesToStr([ // statements
  4139. 'this.GetIt = function () {',
  4140. ' var Result = 0;',
  4141. ' return Result;',
  4142. '};',
  4143. '']),
  4144. LinesToStr([
  4145. 'ProcA(1);',
  4146. 'ProcA(1, 2);',
  4147. 'ProcA(1, 2.0);',
  4148. 'ProcA(1, 2, 3);',
  4149. 'ProcA(1, "2");',
  4150. 'ProcA(2, "");',
  4151. 'ProcA(3, false);',
  4152. 'ProcB();',
  4153. 'ProcB();',
  4154. 'ProcB(4);',
  4155. 'ProcB("foo");',
  4156. 'ProcC(17);',
  4157. 'ProcC(17);',
  4158. 'ProcC(4);',
  4159. 'ProcC(5, "foo");',
  4160. 'ProcB($mod.GetIt());',
  4161. 'ProcB($mod.GetIt());',
  4162. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4163. '']));
  4164. end;
  4165. procedure TTestModule.TestProc_ConstOrder;
  4166. begin
  4167. StartProgram(false);
  4168. Add([
  4169. 'const A = 3;',
  4170. 'const B = A+1;',
  4171. 'procedure DoIt;',
  4172. 'const C = A+1;',
  4173. 'const D = B+1;',
  4174. 'const E = D+C+B+A;',
  4175. 'begin',
  4176. 'end;',
  4177. 'begin'
  4178. ]);
  4179. ConvertProgram;
  4180. CheckSource('TestProc_ConstOrder',
  4181. LinesToStr([ // statements
  4182. 'this.A = 3;',
  4183. 'this.B = 3 + 1;',
  4184. 'var C = 3 + 1;',
  4185. 'var D = 4 + 1;',
  4186. 'var E = 5 + 4 + 4 + 3;',
  4187. 'this.DoIt = function () {',
  4188. '};',
  4189. '']),
  4190. LinesToStr([
  4191. ''
  4192. ]));
  4193. end;
  4194. procedure TTestModule.TestProc_DuplicateConst;
  4195. begin
  4196. StartProgram(false);
  4197. Add([
  4198. 'const A = 1;',
  4199. 'procedure DoIt;',
  4200. 'const A = 2;',
  4201. ' procedure SubIt;',
  4202. ' const A = 21;',
  4203. ' begin',
  4204. ' end;',
  4205. 'begin',
  4206. 'end;',
  4207. 'procedure DoSome;',
  4208. 'const A = 3;',
  4209. 'begin',
  4210. 'end;',
  4211. 'begin'
  4212. ]);
  4213. ConvertProgram;
  4214. CheckSource('TestProc_DuplicateConst',
  4215. LinesToStr([ // statements
  4216. 'this.A = 1;',
  4217. 'var A$1 = 2;',
  4218. 'var A$2 = 21;',
  4219. 'this.DoIt = function () {',
  4220. ' function SubIt() {',
  4221. ' };',
  4222. '};',
  4223. 'var A$3 = 3;',
  4224. 'this.DoSome = function () {',
  4225. '};',
  4226. '']),
  4227. LinesToStr([
  4228. ''
  4229. ]));
  4230. end;
  4231. procedure TTestModule.TestProc_LocalVarAbsolute;
  4232. begin
  4233. StartProgram(false);
  4234. Add([
  4235. 'type',
  4236. ' TObject = class',
  4237. ' Index: longint;',
  4238. ' procedure DoAbs(Item: pointer);',
  4239. ' end;',
  4240. 'procedure TObject.DoAbs(Item: pointer);',
  4241. 'var',
  4242. ' o: TObject absolute Item;',
  4243. 'begin',
  4244. ' if o.Index<o.Index then o.Index:=o.Index;',
  4245. 'end;',
  4246. 'procedure DoIt(i: longint; p: pointer);',
  4247. 'var',
  4248. ' d: double absolute i;',
  4249. ' s: string absolute d;',
  4250. ' oi: TObject absolute i;',
  4251. ' op: TObject absolute p;',
  4252. 'begin',
  4253. ' if d=d then d:=d;',
  4254. ' if s=s then s:=s;',
  4255. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4256. ' if op.Index=op.Index then op.Index:=op.Index;',
  4257. 'end;',
  4258. 'begin']);
  4259. ConvertProgram;
  4260. CheckSource('TestProc_LocalVarAbsolute',
  4261. LinesToStr([ // statements
  4262. 'rtl.createClass($mod, "TObject", null, function () {',
  4263. ' this.$init = function () {',
  4264. ' this.Index = 0;',
  4265. ' };',
  4266. ' this.$final = function () {',
  4267. ' };',
  4268. ' this.DoAbs = function (Item) {',
  4269. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4270. ' };',
  4271. '});',
  4272. 'this.DoIt = function (i, p) {',
  4273. ' if (i === i) i = i;',
  4274. ' if (i === i) i = i;',
  4275. ' if (i.Index < i.Index) i.Index = i.Index;',
  4276. ' if (p.Index === p.Index) p.Index = p.Index;',
  4277. '};'
  4278. ]),
  4279. LinesToStr([
  4280. ]));
  4281. end;
  4282. procedure TTestModule.TestProc_LocalVarInit;
  4283. begin
  4284. StartProgram(false);
  4285. Add([
  4286. 'type TBytes = array of byte;',
  4287. 'procedure DoIt;',
  4288. 'const c = 4;',
  4289. 'var',
  4290. ' b: byte = 1;',
  4291. ' w: word = 2+c;',
  4292. ' p: pointer = nil;',
  4293. ' Buffer: TBytes = nil;',
  4294. 'begin',
  4295. 'end;',
  4296. 'begin']);
  4297. ConvertProgram;
  4298. CheckSource('TestProc_LocalVarInit',
  4299. LinesToStr([ // statements
  4300. 'var c = 4;',
  4301. 'this.DoIt = function () {',
  4302. ' var b = 1;',
  4303. ' var w = 2 + 4;',
  4304. ' var p = null;',
  4305. ' var Buffer = [];',
  4306. '};',
  4307. '']),
  4308. LinesToStr([
  4309. ]));
  4310. end;
  4311. procedure TTestModule.TestProc_ReservedWords;
  4312. begin
  4313. StartProgram(false);
  4314. Add([
  4315. 'procedure Date(ArrayBuffer: longint);',
  4316. 'const',
  4317. ' NaN: longint = 3;',
  4318. 'var',
  4319. ' &Boolean: longint;',
  4320. ' procedure Error(ArrayBuffer: longint);',
  4321. ' begin',
  4322. ' end;',
  4323. 'begin',
  4324. ' Nan:=&bOolean;',
  4325. 'end;',
  4326. 'begin',
  4327. ' Date(1);']);
  4328. ConvertProgram;
  4329. CheckSource('TestProc_ReservedWords',
  4330. LinesToStr([ // statements
  4331. 'var naN = 3;',
  4332. 'this.Date = function (arrayBuffer) {',
  4333. ' var boolean = 0;',
  4334. ' function error(arrayBuffer) {',
  4335. ' };',
  4336. ' naN = boolean;',
  4337. '};',
  4338. '']),
  4339. LinesToStr([
  4340. ' $mod.Date(1);'
  4341. ]));
  4342. end;
  4343. procedure TTestModule.TestProc_ConstRefWord;
  4344. begin
  4345. StartProgram(false);
  4346. Add([
  4347. 'procedure Run(constref w: word);',
  4348. 'var l: word;',
  4349. 'begin',
  4350. ' l:=w;',
  4351. ' Run(w);',
  4352. ' Run(l);',
  4353. 'end;',
  4354. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4355. 'begin',
  4356. ' Run(a);',
  4357. ' Run(b);',
  4358. ' Run(c);',
  4359. ' Run(d);',
  4360. ' Run(e);',
  4361. 'end;',
  4362. 'begin',
  4363. ' Run(1);']);
  4364. ConvertProgram;
  4365. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4366. CheckSource('TestProc_ConstRefWord',
  4367. LinesToStr([ // statements
  4368. 'this.Run = function (w) {',
  4369. ' var l = 0;',
  4370. ' l = w;',
  4371. ' $mod.Run(w);',
  4372. ' $mod.Run(l);',
  4373. '};',
  4374. 'this.Fly = function (a, b, c, d, e) {',
  4375. ' $mod.Run(a);',
  4376. ' $mod.Run(b.get());',
  4377. ' $mod.Run(c.get());',
  4378. ' $mod.Run(d);',
  4379. ' $mod.Run(e);',
  4380. '};',
  4381. '']),
  4382. LinesToStr([
  4383. '$mod.Run(1);'
  4384. ]));
  4385. end;
  4386. procedure TTestModule.TestProc_Async;
  4387. begin
  4388. StartProgram(false);
  4389. Add([
  4390. 'procedure Fly(w: word); async; forward;',
  4391. 'procedure Run(w: word); async;',
  4392. 'begin',
  4393. 'end;',
  4394. 'procedure Fly(w: word); ',
  4395. 'begin',
  4396. 'end;',
  4397. 'begin',
  4398. ' Run(1);']);
  4399. ConvertProgram;
  4400. CheckSource('TestProc_Async',
  4401. LinesToStr([ // statements
  4402. 'this.Run = async function (w) {',
  4403. '};',
  4404. 'this.Fly = async function (w) {',
  4405. '};',
  4406. '']),
  4407. LinesToStr([
  4408. '$mod.Run(1);'
  4409. ]));
  4410. end;
  4411. procedure TTestModule.TestProc_AWaitOutsideAsyncFail;
  4412. begin
  4413. StartProgram(false);
  4414. Add([
  4415. 'function Crawl(w: double): word; ',
  4416. 'begin',
  4417. 'end;',
  4418. 'function Run(w: double): word;',
  4419. 'begin',
  4420. ' Result:=await(Crawl(w));',
  4421. 'end;',
  4422. 'begin',
  4423. ' Run(1);']);
  4424. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  4425. ConvertProgram;
  4426. end;
  4427. procedure TTestModule.TestProc_AWait;
  4428. begin
  4429. StartProgram(false);
  4430. Add([
  4431. 'function Crawl(d: double = 1.3): word; ',
  4432. 'begin',
  4433. 'end;',
  4434. 'function Run(d: double): word; async;',
  4435. 'begin',
  4436. ' Result:=await(1);',
  4437. ' Result:=await(Crawl);',
  4438. ' Result:=await(Crawl(4.5));',
  4439. 'end;',
  4440. 'begin',
  4441. ' Run(1);']);
  4442. ConvertProgram;
  4443. CheckSource('TestProc_AWait',
  4444. LinesToStr([ // statements
  4445. 'this.Crawl = function (d) {',
  4446. ' var Result = 0;',
  4447. ' return Result;',
  4448. '};',
  4449. 'this.Run = async function (d) {',
  4450. ' var Result = 0;',
  4451. ' Result = await 1;',
  4452. ' Result = await $mod.Crawl(1.3);',
  4453. ' Result = await $mod.Crawl(4.5);',
  4454. ' return Result;',
  4455. '};',
  4456. '']),
  4457. LinesToStr([
  4458. '$mod.Run(1);'
  4459. ]));
  4460. end;
  4461. procedure TTestModule.TestProc_AWaitExternalClassPromise;
  4462. begin
  4463. StartProgram(false);
  4464. Add([
  4465. '{$modeswitch externalclass}',
  4466. 'type',
  4467. ' TJSPromise = class external name ''Promise''',
  4468. ' end;',
  4469. 'function Run(d: double): word; async;',
  4470. 'var',
  4471. ' p: TJSPromise;',
  4472. 'begin',
  4473. ' Result:=await(word,p);',
  4474. 'end;',
  4475. 'begin',
  4476. ' Run(1);']);
  4477. ConvertProgram;
  4478. CheckSource('TestProc_AWaitExternalClassPromise',
  4479. LinesToStr([ // statements
  4480. 'this.Run = async function (d) {',
  4481. ' var Result = 0;',
  4482. ' var p = null;',
  4483. ' Result = await p;',
  4484. ' return Result;',
  4485. '};',
  4486. '']),
  4487. LinesToStr([
  4488. '$mod.Run(1);'
  4489. ]));
  4490. end;
  4491. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4492. begin
  4493. StartProgram(false);
  4494. Add([
  4495. '{$mode objfpc}',
  4496. 'type',
  4497. ' TFunc = reference to function(x: word): word;',
  4498. 'var Func: TFunc;',
  4499. 'procedure DoIt(a: word);',
  4500. 'begin',
  4501. ' Func:=function(b:word): word',
  4502. ' begin',
  4503. ' Result:=a+b;',
  4504. ' exit(b);',
  4505. ' exit(Result);',
  4506. ' end;',// test semicolon
  4507. ' a:=3;',
  4508. 'end;',
  4509. 'begin',
  4510. ' Func:=function(c:word):word begin',
  4511. ' Result:=3+c;',
  4512. ' exit(c);',
  4513. ' exit(Result);',
  4514. ' end;']);
  4515. ConvertProgram;
  4516. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4517. LinesToStr([ // statements
  4518. 'this.Func = null;',
  4519. 'this.DoIt = function (a) {',
  4520. ' $mod.Func = function (b) {',
  4521. ' var Result = 0;',
  4522. ' Result = a + b;',
  4523. ' return b;',
  4524. ' return Result;',
  4525. ' return Result;',
  4526. ' };',
  4527. ' a = 3;',
  4528. '};',
  4529. '']),
  4530. LinesToStr([
  4531. '$mod.Func = function (c) {',
  4532. ' var Result = 0;',
  4533. ' Result = 3 + c;',
  4534. ' return c;',
  4535. ' return Result;',
  4536. ' return Result;',
  4537. '};',
  4538. '']));
  4539. end;
  4540. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4541. begin
  4542. StartProgram(false);
  4543. Add([
  4544. '{$mode delphi}',
  4545. 'type',
  4546. ' TProc = reference to procedure(x: word);',
  4547. 'procedure DoIt(a: word);',
  4548. 'var Proc: TProc;',
  4549. 'begin',
  4550. ' Proc:=procedure(b:word) begin end;',
  4551. 'end;',
  4552. 'var Proc: TProc;',
  4553. 'begin',
  4554. ' Proc:=procedure(c:word) begin end;',
  4555. '']);
  4556. ConvertProgram;
  4557. CheckSource('TestAnonymousProc_Assign_Delphi',
  4558. LinesToStr([ // statements
  4559. 'this.DoIt = function (a) {',
  4560. ' var Proc = null;',
  4561. ' Proc = function (b) {',
  4562. ' };',
  4563. '};',
  4564. 'this.Proc = null;',
  4565. '']),
  4566. LinesToStr([
  4567. '$mod.Proc = function (c) {',
  4568. '};',
  4569. '']));
  4570. end;
  4571. procedure TTestModule.TestAnonymousProc_Arg;
  4572. begin
  4573. StartProgram(false);
  4574. Add([
  4575. 'type',
  4576. ' TProc = reference to procedure;',
  4577. ' TFunc = reference to function(x: word): word;',
  4578. 'procedure DoMore(f,g: TProc);',
  4579. 'begin',
  4580. 'end;',
  4581. 'procedure DoOdd(v: jsvalue);',
  4582. 'begin',
  4583. 'end;',
  4584. 'procedure DoIt(f: TFunc);',
  4585. 'begin',
  4586. ' DoIt(function(b:word): word',
  4587. ' begin',
  4588. ' Result:=1+b;',
  4589. ' end);',
  4590. ' DoMore(procedure begin end, procedure begin end);',
  4591. ' DoOdd(procedure begin end);',
  4592. 'end;',
  4593. 'begin',
  4594. ' DoMore(procedure begin end,',
  4595. ' procedure assembler asm',
  4596. ' console.log("c");',
  4597. ' end);',
  4598. '']);
  4599. ConvertProgram;
  4600. CheckSource('TestAnonymousProc_Arg',
  4601. LinesToStr([ // statements
  4602. 'this.DoMore = function (f, g) {',
  4603. '};',
  4604. 'this.DoOdd = function (v) {',
  4605. '};',
  4606. 'this.DoIt = function (f) {',
  4607. ' $mod.DoIt(function (b) {',
  4608. ' var Result = 0;',
  4609. ' Result = 1 + b;',
  4610. ' return Result;',
  4611. ' });',
  4612. ' $mod.DoMore(function () {',
  4613. ' }, function () {',
  4614. ' });',
  4615. ' $mod.DoOdd(function () {',
  4616. ' });',
  4617. '};',
  4618. '']),
  4619. LinesToStr([
  4620. '$mod.DoMore(function () {',
  4621. '}, function () {',
  4622. ' console.log("c");',
  4623. '});',
  4624. '']));
  4625. end;
  4626. procedure TTestModule.TestAnonymousProc_Typecast;
  4627. begin
  4628. StartProgram(false);
  4629. Add([
  4630. 'type',
  4631. ' TProc = reference to procedure(w: word);',
  4632. ' TArr = array of word;',
  4633. ' TFuncArr = reference to function: TArr;',
  4634. 'procedure DoIt(p: TProc);',
  4635. 'var',
  4636. ' w: word;',
  4637. ' a: TArr;',
  4638. 'begin',
  4639. ' p:=TProc(procedure(b: smallint) begin end);',
  4640. ' a:=TFuncArr(function: TArr begin end)();',
  4641. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4642. 'end;',
  4643. 'begin']);
  4644. ConvertProgram;
  4645. CheckSource('TestAnonymousProc_Typecast',
  4646. LinesToStr([ // statements
  4647. 'this.DoIt = function (p) {',
  4648. ' var w = 0;',
  4649. ' var a = [];',
  4650. ' p = function (b) {',
  4651. ' };',
  4652. ' a = function () {',
  4653. ' var Result = [];',
  4654. ' return Result;',
  4655. ' }();',
  4656. ' w = function () {',
  4657. ' var Result = [];',
  4658. ' return Result;',
  4659. ' }()[3];',
  4660. '};',
  4661. '']),
  4662. LinesToStr([
  4663. '']));
  4664. end;
  4665. procedure TTestModule.TestAnonymousProc_With;
  4666. begin
  4667. StartProgram(false);
  4668. Add([
  4669. 'type',
  4670. ' TProc = reference to procedure(w: word);',
  4671. ' TObject = class',
  4672. ' b: boolean;',
  4673. ' end;',
  4674. 'var',
  4675. ' p: TProc;',
  4676. ' bird: TObject;',
  4677. 'begin',
  4678. ' with bird do',
  4679. ' p:=procedure(w: word)',
  4680. ' begin',
  4681. ' b:=w>2;',
  4682. ' end;',
  4683. '']);
  4684. ConvertProgram;
  4685. CheckSource('TestAnonymousProc_With',
  4686. LinesToStr([ // statements
  4687. 'rtl.createClass($mod, "TObject", null, function () {',
  4688. ' this.$init = function () {',
  4689. ' this.b = false;',
  4690. ' };',
  4691. ' this.$final = function () {',
  4692. ' };',
  4693. '});',
  4694. 'this.p = null;',
  4695. 'this.bird = null;',
  4696. '']),
  4697. LinesToStr([
  4698. 'var $with1 = $mod.bird;',
  4699. '$mod.p = function (w) {',
  4700. ' $with1.b = w > 2;',
  4701. '};',
  4702. '']));
  4703. end;
  4704. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4705. begin
  4706. StartProgram(false);
  4707. Add([
  4708. 'type',
  4709. ' TProc = reference to procedure;',
  4710. ' TObject = class',
  4711. ' b: boolean;',
  4712. ' end;',
  4713. 'procedure DoIt;',
  4714. 'var',
  4715. ' p: TProc;',
  4716. 'begin',
  4717. ' try',
  4718. ' except',
  4719. ' on E: TObject do',
  4720. ' p:=procedure',
  4721. ' begin',
  4722. ' E.b:=true;',
  4723. ' end;',
  4724. ' end;',
  4725. 'end;',
  4726. 'begin']);
  4727. ConvertProgram;
  4728. CheckSource('TestAnonymousProc_ExceptOn',
  4729. LinesToStr([ // statements
  4730. 'rtl.createClass($mod, "TObject", null, function () {',
  4731. ' this.$init = function () {',
  4732. ' this.b = false;',
  4733. ' };',
  4734. ' this.$final = function () {',
  4735. ' };',
  4736. '});',
  4737. 'this.DoIt = function () {',
  4738. ' var p = null;',
  4739. ' try {} catch ($e) {',
  4740. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4741. ' var E = $e;',
  4742. ' p = function () {',
  4743. ' E.b = true;',
  4744. ' };',
  4745. ' } else throw $e',
  4746. ' };',
  4747. '};',
  4748. '']),
  4749. LinesToStr([
  4750. '']));
  4751. end;
  4752. procedure TTestModule.TestAnonymousProc_Nested;
  4753. begin
  4754. StartProgram(false);
  4755. Add([
  4756. 'type',
  4757. ' TProc = reference to procedure;',
  4758. ' TObject = class',
  4759. ' i: byte;',
  4760. ' procedure DoIt;',
  4761. ' end;',
  4762. 'procedure TObject.DoIt;',
  4763. 'var',
  4764. ' p: TProc;',
  4765. ' procedure Sub;',
  4766. ' begin',
  4767. ' p:=procedure',
  4768. ' begin',
  4769. ' i:=3;',
  4770. ' Self.i:=4;',
  4771. ' p:=procedure',
  4772. ' procedure SubSub;',
  4773. ' begin',
  4774. ' i:=13;',
  4775. ' Self.i:=14;',
  4776. ' end;',
  4777. ' begin',
  4778. ' i:=13;',
  4779. ' Self.i:=14;',
  4780. ' end;',
  4781. ' end;',
  4782. ' end;',
  4783. 'begin',
  4784. 'end;',
  4785. 'begin']);
  4786. ConvertProgram;
  4787. CheckSource('TestAnonymousProc_Nested',
  4788. LinesToStr([ // statements
  4789. 'rtl.createClass($mod, "TObject", null, function () {',
  4790. ' this.$init = function () {',
  4791. ' this.i = 0;',
  4792. ' };',
  4793. ' this.$final = function () {',
  4794. ' };',
  4795. ' this.DoIt = function () {',
  4796. ' var $Self = this;',
  4797. ' var p = null;',
  4798. ' function Sub() {',
  4799. ' p = function () {',
  4800. ' $Self.i = 3;',
  4801. ' $Self.i = 4;',
  4802. ' p = function () {',
  4803. ' function SubSub() {',
  4804. ' $Self.i = 13;',
  4805. ' $Self.i = 14;',
  4806. ' };',
  4807. ' $Self.i = 13;',
  4808. ' $Self.i = 14;',
  4809. ' };',
  4810. ' };',
  4811. ' };',
  4812. ' };',
  4813. '});',
  4814. '']),
  4815. LinesToStr([
  4816. '']));
  4817. end;
  4818. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4819. begin
  4820. StartProgram(false);
  4821. Add([
  4822. 'type',
  4823. ' TProc = reference to procedure;',
  4824. 'function DoIt: TProc;',
  4825. ' function Sub: TProc;',
  4826. ' begin',
  4827. ' Result:=procedure',
  4828. ' begin',
  4829. ' Sub:=procedure',
  4830. ' procedure SubSub;',
  4831. ' begin',
  4832. ' Result:=nil;',
  4833. ' Sub:=nil;',
  4834. ' DoIt:=nil;',
  4835. ' end;',
  4836. ' begin',
  4837. ' Result:=nil;',
  4838. ' Sub:=nil;',
  4839. ' DoIt:=nil;',
  4840. ' end;',
  4841. ' end;',
  4842. ' end;',
  4843. 'begin',
  4844. 'end;',
  4845. 'begin']);
  4846. ConvertProgram;
  4847. CheckSource('TestAnonymousProc_NestedAssignResult',
  4848. LinesToStr([ // statements
  4849. 'this.DoIt = function () {',
  4850. ' var Result = null;',
  4851. ' function Sub() {',
  4852. ' var Result$1 = null;',
  4853. ' Result$1 = function () {',
  4854. ' Result$1 = function () {',
  4855. ' function SubSub() {',
  4856. ' Result$1 = null;',
  4857. ' Result$1 = null;',
  4858. ' Result = null;',
  4859. ' };',
  4860. ' Result$1 = null;',
  4861. ' Result$1 = null;',
  4862. ' Result = null;',
  4863. ' };',
  4864. ' };',
  4865. ' return Result$1;',
  4866. ' };',
  4867. ' return Result;',
  4868. '};',
  4869. '']),
  4870. LinesToStr([
  4871. '']));
  4872. end;
  4873. procedure TTestModule.TestAnonymousProc_Class;
  4874. begin
  4875. StartProgram(false);
  4876. Add([
  4877. 'type',
  4878. ' TProc = reference to procedure;',
  4879. ' TEvent = procedure of object;',
  4880. ' TObject = class',
  4881. ' Size: word;',
  4882. ' function GetIt: TProc;',
  4883. ' procedure DoIt; virtual; abstract;',
  4884. ' end;',
  4885. 'function TObject.GetIt: TProc;',
  4886. 'begin',
  4887. ' Result:=procedure',
  4888. ' var p: TEvent;',
  4889. ' begin',
  4890. ' Size:=Size;',
  4891. ' Size:=Self.Size;',
  4892. ' p:=@DoIt;',
  4893. ' p:[email protected];',
  4894. ' end;',
  4895. 'end;',
  4896. 'begin']);
  4897. ConvertProgram;
  4898. CheckSource('TestAnonymousProc_Class',
  4899. LinesToStr([ // statements
  4900. 'rtl.createClass($mod, "TObject", null, function () {',
  4901. ' this.$init = function () {',
  4902. ' this.Size = 0;',
  4903. ' };',
  4904. ' this.$final = function () {',
  4905. ' };',
  4906. ' this.GetIt = function () {',
  4907. ' var $Self = this;',
  4908. ' var Result = null;',
  4909. ' Result = function () {',
  4910. ' var p = null;',
  4911. ' $Self.Size = $Self.Size;',
  4912. ' $Self.Size = $Self.Size;',
  4913. ' p = rtl.createCallback($Self, "DoIt");',
  4914. ' p = rtl.createCallback($Self, "DoIt");',
  4915. ' };',
  4916. ' return Result;',
  4917. ' };',
  4918. '});',
  4919. '']),
  4920. LinesToStr([
  4921. '']));
  4922. end;
  4923. procedure TTestModule.TestAnonymousProc_ForLoop;
  4924. begin
  4925. StartProgram(false);
  4926. Add([
  4927. 'type TProc = reference to procedure;',
  4928. 'procedure Foo(p: TProc);',
  4929. 'begin',
  4930. 'end;',
  4931. 'procedure DoIt;',
  4932. 'var i: word;',
  4933. ' a: word;',
  4934. 'begin',
  4935. ' for i:=1 to 10 do begin',
  4936. ' Foo(procedure begin a:=3; end);',
  4937. ' end;',
  4938. 'end;',
  4939. 'begin',
  4940. ' DoIt;']);
  4941. ConvertProgram;
  4942. CheckSource('TestAnonymousProc_ForLoop',
  4943. LinesToStr([ // statements
  4944. 'this.Foo = function (p) {',
  4945. '};',
  4946. 'this.DoIt = function () {',
  4947. ' var i = 0;',
  4948. ' var a = 0;',
  4949. ' for (i = 1; i <= 10; i++) {',
  4950. ' $mod.Foo(function () {',
  4951. ' a = 3;',
  4952. ' });',
  4953. ' };',
  4954. '};',
  4955. '']),
  4956. LinesToStr([
  4957. '$mod.DoIt();'
  4958. ]));
  4959. end;
  4960. procedure TTestModule.TestAnonymousProc_Async;
  4961. begin
  4962. StartProgram(false);
  4963. Add([
  4964. '{$mode objfpc}',
  4965. 'type',
  4966. ' TFunc = reference to function(x: double): word;',
  4967. 'function Crawl(d: double = 1.3): word; ',
  4968. 'begin',
  4969. 'end;',
  4970. 'var Func: TFunc;',
  4971. 'begin',
  4972. ' Func:=function(c:double):word async begin',
  4973. ' Result:=await(Crawl(c));',
  4974. ' end;',
  4975. ' Func:=function(c:double):word async assembler asm',
  4976. ' end;',
  4977. ' ']);
  4978. ConvertProgram;
  4979. CheckSource('TestAnonymousProc_Async',
  4980. LinesToStr([ // statements
  4981. 'this.Crawl = function (d) {',
  4982. ' var Result = 0;',
  4983. ' return Result;',
  4984. '};',
  4985. 'this.Func = null;',
  4986. '']),
  4987. LinesToStr([
  4988. '$mod.Func = async function (c) {',
  4989. ' var Result = 0;',
  4990. ' Result = await $mod.Crawl(c);',
  4991. ' return Result;',
  4992. '};',
  4993. '$mod.Func = async function (c) {',
  4994. '};',
  4995. '']));
  4996. end;
  4997. procedure TTestModule.TestEnum_Name;
  4998. begin
  4999. StartProgram(false);
  5000. Add('type TMyEnum = (Red, Green, Blue);');
  5001. Add('var e: TMyEnum;');
  5002. Add('var f: TMyEnum = Blue;');
  5003. Add('begin');
  5004. Add(' e:=green;');
  5005. Add(' e:=default(TMyEnum);');
  5006. ConvertProgram;
  5007. CheckSource('TestEnumName',
  5008. LinesToStr([ // statements
  5009. 'this.TMyEnum = {',
  5010. ' "0":"Red",',
  5011. ' Red:0,',
  5012. ' "1":"Green",',
  5013. ' Green:1,',
  5014. ' "2":"Blue",',
  5015. ' Blue:2',
  5016. ' };',
  5017. 'this.e = 0;',
  5018. 'this.f = $mod.TMyEnum.Blue;'
  5019. ]),
  5020. LinesToStr([
  5021. '$mod.e=$mod.TMyEnum.Green;',
  5022. '$mod.e=$mod.TMyEnum.Red;'
  5023. ]));
  5024. end;
  5025. procedure TTestModule.TestEnum_Number;
  5026. begin
  5027. Converter.Options:=Converter.Options+[coEnumNumbers];
  5028. StartProgram(false);
  5029. Add('type TMyEnum = (Red, Green);');
  5030. Add('var');
  5031. Add(' e: TMyEnum;');
  5032. Add(' f: TMyEnum = Green;');
  5033. Add(' i: longint;');
  5034. Add('begin');
  5035. Add(' e:=green;');
  5036. Add(' i:=longint(e);');
  5037. ConvertProgram;
  5038. CheckSource('TestEnumNumber',
  5039. LinesToStr([ // statements
  5040. 'this.TMyEnum = {',
  5041. ' "0":"Red",',
  5042. ' Red:0,',
  5043. ' "1":"Green",',
  5044. ' Green:1',
  5045. ' };',
  5046. 'this.e = 0;',
  5047. 'this.f = 1;',
  5048. 'this.i = 0;'
  5049. ]),
  5050. LinesToStr([
  5051. '$mod.e=1;',
  5052. '$mod.i=$mod.e;'
  5053. ]));
  5054. end;
  5055. procedure TTestModule.TestEnum_ConstFail;
  5056. begin
  5057. StartProgram(false);
  5058. Add([
  5059. 'type TMyEnum = (Red = 100, Green = 101);',
  5060. 'var',
  5061. ' e: TMyEnum;',
  5062. ' f: TMyEnum = Green;',
  5063. 'begin',
  5064. ' e:=green;']);
  5065. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5066. ConvertProgram;
  5067. end;
  5068. procedure TTestModule.TestEnum_Functions;
  5069. begin
  5070. StartProgram(false);
  5071. Add([
  5072. 'type TMyEnum = (Red, Green);',
  5073. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5074. 'var',
  5075. ' v: longint;',
  5076. ' s: string;',
  5077. 'begin',
  5078. ' val(s,e,v);',
  5079. ' val(s,e,i);',
  5080. 'end;',
  5081. 'var',
  5082. ' e: TMyEnum;',
  5083. ' i: longint;',
  5084. ' s: string;',
  5085. ' b: boolean;',
  5086. 'begin',
  5087. ' i:=ord(red);',
  5088. ' i:=ord(green);',
  5089. ' i:=ord(e);',
  5090. ' i:=ord(b);',
  5091. ' e:=low(tmyenum);',
  5092. ' e:=low(e);',
  5093. ' b:=low(boolean);',
  5094. ' e:=high(tmyenum);',
  5095. ' e:=high(e);',
  5096. ' b:=high(boolean);',
  5097. ' e:=pred(green);',
  5098. ' e:=pred(e);',
  5099. ' b:=pred(b);',
  5100. ' e:=succ(red);',
  5101. ' e:=succ(e);',
  5102. ' b:=succ(b);',
  5103. ' e:=tmyenum(1);',
  5104. ' e:=tmyenum(i);',
  5105. ' s:=str(e);',
  5106. ' str(e,s);',
  5107. ' str(red,s);',
  5108. ' s:=str(e:3);',
  5109. ' writestr(s,e:3,red);',
  5110. ' val(s,e,i);',
  5111. ' e:=TMyEnum(i);',
  5112. ' i:=longint(e);']);
  5113. ConvertProgram;
  5114. CheckSource('TestEnum_Functions',
  5115. LinesToStr([ // statements
  5116. 'this.TMyEnum = {',
  5117. ' "0":"Red",',
  5118. ' Red:0,',
  5119. ' "1":"Green",',
  5120. ' Green:1',
  5121. ' };',
  5122. 'this.DoIt = function (e, i) {',
  5123. ' var v = 0;',
  5124. ' var s = "";',
  5125. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5126. ' v = w;',
  5127. ' }));',
  5128. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5129. '};',
  5130. 'this.e = 0;',
  5131. 'this.i = 0;',
  5132. 'this.s = "";',
  5133. 'this.b = false;',
  5134. '']),
  5135. LinesToStr([
  5136. '$mod.i=$mod.TMyEnum.Red;',
  5137. '$mod.i=$mod.TMyEnum.Green;',
  5138. '$mod.i=$mod.e;',
  5139. '$mod.i=$mod.b+0;',
  5140. '$mod.e=$mod.TMyEnum.Red;',
  5141. '$mod.e=$mod.TMyEnum.Red;',
  5142. '$mod.b=false;',
  5143. '$mod.e=$mod.TMyEnum.Green;',
  5144. '$mod.e=$mod.TMyEnum.Green;',
  5145. '$mod.b=true;',
  5146. '$mod.e=$mod.TMyEnum.Green-1;',
  5147. '$mod.e=$mod.e-1;',
  5148. '$mod.b=false;',
  5149. '$mod.e=$mod.TMyEnum.Red+1;',
  5150. '$mod.e=$mod.e+1;',
  5151. '$mod.b=true;',
  5152. '$mod.e=1;',
  5153. '$mod.e=$mod.i;',
  5154. '$mod.s = $mod.TMyEnum[$mod.e];',
  5155. '$mod.s = $mod.TMyEnum[$mod.e];',
  5156. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5157. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5158. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5159. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5160. ' $mod.i = v;',
  5161. '});',
  5162. '$mod.e=$mod.i;',
  5163. '$mod.i=$mod.e;',
  5164. '']));
  5165. end;
  5166. procedure TTestModule.TestEnum_AsParams;
  5167. begin
  5168. StartProgram(false);
  5169. Add('type TEnum = (Red,Blue);');
  5170. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5171. Add('var vJ: TEnum;');
  5172. Add('begin');
  5173. Add(' vg:=vg;');
  5174. Add(' vj:=vh;');
  5175. Add(' vi:=vi;');
  5176. Add(' doit(vg,vg,vg);');
  5177. Add(' doit(vh,vh,vj);');
  5178. Add(' doit(vi,vi,vi);');
  5179. Add(' doit(vj,vj,vj);');
  5180. Add('end;');
  5181. Add('var i: TEnum;');
  5182. Add('begin');
  5183. Add(' doit(i,i,i);');
  5184. ConvertProgram;
  5185. CheckSource('TestEnum_AsParams',
  5186. LinesToStr([ // statements
  5187. 'this.TEnum = {',
  5188. ' "0": "Red",',
  5189. ' Red: 0,',
  5190. ' "1": "Blue",',
  5191. ' Blue: 1',
  5192. '};',
  5193. 'this.DoIt = function (vG,vH,vI) {',
  5194. ' var vJ = 0;',
  5195. ' vG = vG;',
  5196. ' vJ = vH;',
  5197. ' vI.set(vI.get());',
  5198. ' $mod.DoIt(vG, vG, {',
  5199. ' get: function () {',
  5200. ' return vG;',
  5201. ' },',
  5202. ' set: function (v) {',
  5203. ' vG = v;',
  5204. ' }',
  5205. ' });',
  5206. ' $mod.DoIt(vH, vH, {',
  5207. ' get: function () {',
  5208. ' return vJ;',
  5209. ' },',
  5210. ' set: function (v) {',
  5211. ' vJ = v;',
  5212. ' }',
  5213. ' });',
  5214. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5215. ' $mod.DoIt(vJ, vJ, {',
  5216. ' get: function () {',
  5217. ' return vJ;',
  5218. ' },',
  5219. ' set: function (v) {',
  5220. ' vJ = v;',
  5221. ' }',
  5222. ' });',
  5223. '};',
  5224. 'this.i = 0;'
  5225. ]),
  5226. LinesToStr([
  5227. '$mod.DoIt($mod.i,$mod.i,{',
  5228. ' p: $mod,',
  5229. ' get: function () {',
  5230. ' return this.p.i;',
  5231. ' },',
  5232. ' set: function (v) {',
  5233. ' this.p.i = v;',
  5234. ' }',
  5235. '});'
  5236. ]));
  5237. end;
  5238. procedure TTestModule.TestEnumRange_Array;
  5239. begin
  5240. StartProgram(false);
  5241. Add([
  5242. 'type',
  5243. ' TEnum = (Red, Green, Blue);',
  5244. ' TEnumRg = green..blue;',
  5245. ' TArr = array[TEnumRg] of byte;',
  5246. ' TArr2 = array[green..blue] of byte;',
  5247. 'var',
  5248. ' a: TArr;',
  5249. ' b: TArr = (3,4);',
  5250. ' c: TArr2 = (5,6);',
  5251. 'begin',
  5252. ' a[green] := b[blue];',
  5253. ' c[green] := c[blue];',
  5254. '']);
  5255. ConvertProgram;
  5256. CheckSource('TestEnumRange_Array',
  5257. LinesToStr([ // statements
  5258. 'this.TEnum = {',
  5259. ' "0": "Red",',
  5260. ' Red: 0,',
  5261. ' "1": "Green",',
  5262. ' Green: 1,',
  5263. ' "2": "Blue",',
  5264. ' Blue: 2',
  5265. '};',
  5266. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5267. 'this.b = [3, 4];',
  5268. 'this.c = [5, 6];',
  5269. '']),
  5270. LinesToStr([
  5271. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5272. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5273. '']));
  5274. end;
  5275. procedure TTestModule.TestEnum_ForIn;
  5276. begin
  5277. StartProgram(false);
  5278. Add([
  5279. 'type',
  5280. ' TEnum = (Red, Green, Blue);',
  5281. ' TEnumRg = green..blue;',
  5282. ' TArr = array[TEnum] of byte;',
  5283. ' TArrRg = array[TEnumRg] of byte;',
  5284. 'var',
  5285. ' e: TEnum;',
  5286. ' a1: TArr = (3,4,5);',
  5287. ' a2: TArrRg = (11,12);',
  5288. ' b: byte;',
  5289. 'begin',
  5290. ' for e in TEnum do ;',
  5291. ' for e in TEnumRg do ;',
  5292. ' for e in TArr do ;',
  5293. ' for e in TArrRg do ;',
  5294. ' for b in a1 do ;',
  5295. ' for b in a2 do ;',
  5296. '']);
  5297. ConvertProgram;
  5298. CheckSource('TestEnum_ForIn',
  5299. LinesToStr([ // statements
  5300. 'this.TEnum = {',
  5301. ' "0": "Red",',
  5302. ' Red: 0,',
  5303. ' "1": "Green",',
  5304. ' Green: 1,',
  5305. ' "2": "Blue",',
  5306. ' Blue: 2',
  5307. '};',
  5308. 'this.e = 0;',
  5309. 'this.a1 = [3, 4, 5];',
  5310. 'this.a2 = [11, 12];',
  5311. 'this.b = 0;',
  5312. '']),
  5313. LinesToStr([
  5314. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5315. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5316. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5317. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5318. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  5319. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  5320. '']));
  5321. end;
  5322. procedure TTestModule.TestEnum_ScopedNumber;
  5323. begin
  5324. Converter.Options:=Converter.Options+[coEnumNumbers];
  5325. StartProgram(false);
  5326. Add([
  5327. 'type',
  5328. ' TEnum = (Red, Green);',
  5329. 'var',
  5330. ' e: TEnum;',
  5331. 'begin',
  5332. ' e:=TEnum.Green;',
  5333. '']);
  5334. ConvertProgram;
  5335. CheckSource('TestEnum_ScopedNumber',
  5336. LinesToStr([ // statements
  5337. 'this.TEnum = {',
  5338. ' "0": "Red",',
  5339. ' Red: 0,',
  5340. ' "1": "Green",',
  5341. ' Green: 1',
  5342. '};',
  5343. 'this.e = 0;',
  5344. '']),
  5345. LinesToStr([
  5346. '$mod.e = 1;']));
  5347. end;
  5348. procedure TTestModule.TestEnum_InFunction;
  5349. begin
  5350. StartProgram(false);
  5351. Add([
  5352. 'const TEnum = 3;',
  5353. 'procedure DoIt;',
  5354. 'type',
  5355. ' TEnum = (Red, Green, Blue);',
  5356. ' procedure Sub;',
  5357. ' type',
  5358. ' TEnumSub = (Left, Right);',
  5359. ' var',
  5360. ' es: TEnumSub;',
  5361. ' begin',
  5362. ' es:=Left;',
  5363. ' end;',
  5364. 'var',
  5365. ' e, e2: TEnum;',
  5366. 'begin',
  5367. ' if e in [red,blue] then e2:=e;',
  5368. 'end;',
  5369. 'begin']);
  5370. ConvertProgram;
  5371. CheckSource('TestEnum_InFunction',
  5372. LinesToStr([ // statements
  5373. 'this.TEnum = 3;',
  5374. 'var TEnum$1 = {',
  5375. ' "0":"Red",',
  5376. ' Red:0,',
  5377. ' "1":"Green",',
  5378. ' Green:1,',
  5379. ' "2":"Blue",',
  5380. ' Blue:2',
  5381. ' };',
  5382. 'var TEnumSub = {',
  5383. ' "0": "Left",',
  5384. ' Left: 0,',
  5385. ' "1": "Right",',
  5386. ' Right: 1',
  5387. '};',
  5388. 'this.DoIt = function () {',
  5389. ' function Sub() {',
  5390. ' var es = 0;',
  5391. ' es = TEnumSub.Left;',
  5392. ' };',
  5393. ' var e = 0;',
  5394. ' var e2 = 0;',
  5395. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5396. '};',
  5397. '']),
  5398. LinesToStr([
  5399. '']));
  5400. end;
  5401. procedure TTestModule.TestSet_Enum;
  5402. begin
  5403. StartProgram(false);
  5404. Add([
  5405. 'type',
  5406. ' TColor = (Red, Green, Blue);',
  5407. ' TColors = set of TColor;',
  5408. 'var',
  5409. ' c: TColor;',
  5410. ' s: TColors;',
  5411. ' t: TColors = [];',
  5412. ' u: TColors = [Red];',
  5413. 'begin',
  5414. ' s:=[];',
  5415. ' s:=[Green];',
  5416. ' s:=[Green,Blue];',
  5417. ' s:=[Red..Blue];',
  5418. ' s:=[Red,Green..Blue];',
  5419. ' s:=[Red,c];',
  5420. ' s:=t;',
  5421. ' s:=default(TColors);',
  5422. '']);
  5423. ConvertProgram;
  5424. CheckSource('TestSet',
  5425. LinesToStr([ // statements
  5426. 'this.TColor = {',
  5427. ' "0":"Red",',
  5428. ' Red:0,',
  5429. ' "1":"Green",',
  5430. ' Green:1,',
  5431. ' "2":"Blue",',
  5432. ' Blue:2',
  5433. ' };',
  5434. 'this.c = 0;',
  5435. 'this.s = {};',
  5436. 'this.t = {};',
  5437. 'this.u = rtl.createSet($mod.TColor.Red);'
  5438. ]),
  5439. LinesToStr([
  5440. '$mod.s={};',
  5441. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5442. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5443. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5444. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5445. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5446. '$mod.s=rtl.refSet($mod.t);',
  5447. '$mod.s={};',
  5448. '']));
  5449. end;
  5450. procedure TTestModule.TestSet_Operators;
  5451. begin
  5452. StartProgram(false);
  5453. Add('type');
  5454. Add(' TColor = (Red, Green, Blue);');
  5455. Add(' TColors = set of tcolor;');
  5456. Add('var');
  5457. Add(' vC: TColor;');
  5458. Add(' vS: TColors;');
  5459. Add(' vT: TColors;');
  5460. Add(' vU: TColors;');
  5461. Add(' B: boolean;');
  5462. Add('begin');
  5463. Add(' include(vs,green);');
  5464. Add(' exclude(vs,vc);');
  5465. Add(' vs:=vt+vu;');
  5466. Add(' vs:=vt+[red];');
  5467. Add(' vs:=[red]+vt;');
  5468. Add(' vs:=[red]+[green];');
  5469. Add(' vs:=vt-vu;');
  5470. Add(' vs:=vt-[red];');
  5471. Add(' vs:=[red]-vt;');
  5472. Add(' vs:=[red]-[green];');
  5473. Add(' vs:=vt*vu;');
  5474. Add(' vs:=vt*[red];');
  5475. Add(' vs:=[red]*vt;');
  5476. Add(' vs:=[red]*[green];');
  5477. Add(' vs:=vt><vu;');
  5478. Add(' vs:=vt><[red];');
  5479. Add(' vs:=[red]><vt;');
  5480. Add(' vs:=[red]><[green];');
  5481. Add(' b:=vt=vu;');
  5482. Add(' b:=vt=[red];');
  5483. Add(' b:=[red]=vt;');
  5484. Add(' b:=[red]=[green];');
  5485. Add(' b:=vt<>vu;');
  5486. Add(' b:=vt<>[red];');
  5487. Add(' b:=[red]<>vt;');
  5488. Add(' b:=[red]<>[green];');
  5489. Add(' b:=vt<=vu;');
  5490. Add(' b:=vt<=[red];');
  5491. Add(' b:=[red]<=vt;');
  5492. Add(' b:=[red]<=[green];');
  5493. Add(' b:=vt>=vu;');
  5494. Add(' b:=vt>=[red];');
  5495. Add(' b:=[red]>=vt;');
  5496. Add(' b:=[red]>=[green];');
  5497. ConvertProgram;
  5498. CheckSource('TestSet_Operators',
  5499. LinesToStr([ // statements
  5500. 'this.TColor = {',
  5501. ' "0":"Red",',
  5502. ' Red:0,',
  5503. ' "1":"Green",',
  5504. ' Green:1,',
  5505. ' "2":"Blue",',
  5506. ' Blue:2',
  5507. ' };',
  5508. 'this.vC = 0;',
  5509. 'this.vS = {};',
  5510. 'this.vT = {};',
  5511. 'this.vU = {};',
  5512. 'this.B = false;'
  5513. ]),
  5514. LinesToStr([
  5515. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5516. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5517. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5518. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5519. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5520. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5521. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5522. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5523. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5524. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5525. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5526. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5527. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5528. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5529. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5530. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5531. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5532. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5533. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5534. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5535. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5536. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5537. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5538. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5539. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5540. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5541. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5542. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5543. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5544. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5545. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5546. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5547. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5548. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5549. '']));
  5550. end;
  5551. procedure TTestModule.TestSet_Operator_In;
  5552. begin
  5553. StartProgram(false);
  5554. Add('type');
  5555. Add(' TColor = (Red, Green, Blue);');
  5556. Add(' TColors = set of tcolor;');
  5557. Add('var');
  5558. Add(' vC: tcolor;');
  5559. Add(' vT: tcolors;');
  5560. Add(' B: boolean;');
  5561. Add('begin');
  5562. Add(' b:=red in vt;');
  5563. Add(' b:=vc in vt;');
  5564. Add(' b:=green in [red..blue];');
  5565. Add(' b:=vc in [red..blue];');
  5566. Add(' ');
  5567. Add(' if red in vt then ;');
  5568. Add(' while vC in vt do ;');
  5569. Add(' repeat');
  5570. Add(' until vC in vt;');
  5571. ConvertProgram;
  5572. CheckSource('TestSet_Operator_In',
  5573. LinesToStr([ // statements
  5574. 'this.TColor = {',
  5575. ' "0":"Red",',
  5576. ' Red:0,',
  5577. ' "1":"Green",',
  5578. ' Green:1,',
  5579. ' "2":"Blue",',
  5580. ' Blue:2',
  5581. ' };',
  5582. 'this.vC = 0;',
  5583. 'this.vT = {};',
  5584. 'this.B = false;'
  5585. ]),
  5586. LinesToStr([
  5587. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5588. '$mod.B = $mod.vC in $mod.vT;',
  5589. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5590. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5591. 'if ($mod.TColor.Red in $mod.vT) ;',
  5592. 'while ($mod.vC in $mod.vT) {',
  5593. '};',
  5594. 'do {',
  5595. '} while (!($mod.vC in $mod.vT));',
  5596. '']));
  5597. end;
  5598. procedure TTestModule.TestSet_Functions;
  5599. begin
  5600. StartProgram(false);
  5601. Add('type');
  5602. Add(' TMyEnum = (Red, Green);');
  5603. Add(' TMyEnums = set of TMyEnum;');
  5604. Add('var');
  5605. Add(' e: TMyEnum;');
  5606. Add(' s: TMyEnums;');
  5607. Add('begin');
  5608. Add(' e:=Low(TMyEnums);');
  5609. Add(' e:=Low(s);');
  5610. Add(' e:=High(TMyEnums);');
  5611. Add(' e:=High(s);');
  5612. ConvertProgram;
  5613. CheckSource('TestSetFunctions',
  5614. LinesToStr([ // statements
  5615. 'this.TMyEnum = {',
  5616. ' "0":"Red",',
  5617. ' Red:0,',
  5618. ' "1":"Green",',
  5619. ' Green:1',
  5620. ' };',
  5621. 'this.e = 0;',
  5622. 'this.s = {};'
  5623. ]),
  5624. LinesToStr([
  5625. '$mod.e=$mod.TMyEnum.Red;',
  5626. '$mod.e=$mod.TMyEnum.Red;',
  5627. '$mod.e=$mod.TMyEnum.Green;',
  5628. '$mod.e=$mod.TMyEnum.Green;',
  5629. '']));
  5630. end;
  5631. procedure TTestModule.TestSet_PassAsArgClone;
  5632. begin
  5633. StartProgram(false);
  5634. Add('type');
  5635. Add(' TMyEnum = (Red, Green);');
  5636. Add(' TMyEnums = set of TMyEnum;');
  5637. Add('procedure DoDefault(s: tmyenums); begin end;');
  5638. Add('procedure DoConst(const s: tmyenums); begin end;');
  5639. Add('var');
  5640. Add(' aSet: tmyenums;');
  5641. Add('begin');
  5642. Add(' dodefault(aset);');
  5643. Add(' doconst(aset);');
  5644. ConvertProgram;
  5645. CheckSource('TestSetFunctions',
  5646. LinesToStr([ // statements
  5647. 'this.TMyEnum = {',
  5648. ' "0":"Red",',
  5649. ' Red:0,',
  5650. ' "1":"Green",',
  5651. ' Green:1',
  5652. ' };',
  5653. 'this.DoDefault = function (s) {',
  5654. '};',
  5655. 'this.DoConst = function (s) {',
  5656. '};',
  5657. 'this.aSet = {};'
  5658. ]),
  5659. LinesToStr([
  5660. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5661. '$mod.DoConst($mod.aSet);',
  5662. '']));
  5663. end;
  5664. procedure TTestModule.TestSet_AsParams;
  5665. begin
  5666. StartProgram(false);
  5667. Add([
  5668. 'type TEnum = (Red,Blue);',
  5669. 'type TEnums = set of TEnum;',
  5670. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5671. 'var vJ: TEnums;',
  5672. 'begin',
  5673. ' Include(vg,red);',
  5674. ' Include(result,blue);',
  5675. ' vg:=vg;',
  5676. ' vj:=vh;',
  5677. ' vi:=vi;',
  5678. ' doit(vg,vg,vg);',
  5679. ' doit(vh,vh,vj);',
  5680. ' doit(vi,vi,vi);',
  5681. ' doit(vj,vj,vj);',
  5682. 'end;',
  5683. 'var i: TEnums;',
  5684. 'begin',
  5685. ' doit(i,i,i);']);
  5686. ConvertProgram;
  5687. CheckSource('TestSet_AsParams',
  5688. LinesToStr([ // statements
  5689. 'this.TEnum = {',
  5690. ' "0": "Red",',
  5691. ' Red: 0,',
  5692. ' "1": "Blue",',
  5693. ' Blue: 1',
  5694. '};',
  5695. 'this.DoIt = function (vG,vH,vI) {',
  5696. ' var Result = {};',
  5697. ' var vJ = {};',
  5698. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5699. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5700. ' vG = rtl.refSet(vG);',
  5701. ' vJ = rtl.refSet(vH);',
  5702. ' vI.set(rtl.refSet(vI.get()));',
  5703. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5704. ' get: function () {',
  5705. ' return vG;',
  5706. ' },',
  5707. ' set: function (v) {',
  5708. ' vG = v;',
  5709. ' }',
  5710. ' });',
  5711. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5712. ' get: function () {',
  5713. ' return vJ;',
  5714. ' },',
  5715. ' set: function (v) {',
  5716. ' vJ = v;',
  5717. ' }',
  5718. ' });',
  5719. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5720. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5721. ' get: function () {',
  5722. ' return vJ;',
  5723. ' },',
  5724. ' set: function (v) {',
  5725. ' vJ = v;',
  5726. ' }',
  5727. ' });',
  5728. ' return Result;',
  5729. '};',
  5730. 'this.i = {};'
  5731. ]),
  5732. LinesToStr([
  5733. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5734. ' p: $mod,',
  5735. ' get: function () {',
  5736. ' return this.p.i;',
  5737. ' },',
  5738. ' set: function (v) {',
  5739. ' this.p.i = v;',
  5740. ' }',
  5741. '});'
  5742. ]));
  5743. end;
  5744. procedure TTestModule.TestSet_Property;
  5745. begin
  5746. StartProgram(false);
  5747. Add('type');
  5748. Add(' TEnum = (Red,Blue);');
  5749. Add(' TEnums = set of TEnum;');
  5750. Add(' TObject = class');
  5751. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5752. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5753. Add(' property Colors: TEnums read GetColors write SetColors;');
  5754. Add(' end;');
  5755. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5756. Add('begin end;');
  5757. Add('var Obj: TObject;');
  5758. Add('begin');
  5759. Add(' Include(Obj.Colors,Red);');
  5760. Add(' Exclude(Obj.Colors,Red);');
  5761. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5762. ConvertProgram;
  5763. CheckSource('TestSet_Property',
  5764. LinesToStr([ // statements
  5765. 'this.TEnum = {',
  5766. ' "0": "Red",',
  5767. ' Red: 0,',
  5768. ' "1": "Blue",',
  5769. ' Blue: 1',
  5770. '};',
  5771. 'rtl.createClass($mod, "TObject", null, function () {',
  5772. ' this.$init = function () {',
  5773. ' };',
  5774. ' this.$final = function () {',
  5775. ' };',
  5776. '});',
  5777. 'this.DoIt = function (i, j, k, l) {',
  5778. '};',
  5779. 'this.Obj = null;',
  5780. '']),
  5781. LinesToStr([
  5782. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5783. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5784. '']));
  5785. end;
  5786. procedure TTestModule.TestSet_EnumConst;
  5787. begin
  5788. StartProgram(false);
  5789. Add([
  5790. 'type',
  5791. ' TEnum = (Red,Blue);',
  5792. ' TEnums = set of TEnum;',
  5793. 'const',
  5794. ' Orange = red;',
  5795. 'var',
  5796. ' Enum: tenum;',
  5797. ' Enums: tenums;',
  5798. 'begin',
  5799. ' Include(enums,orange);',
  5800. ' Exclude(enums,orange);',
  5801. ' if orange in enums then;',
  5802. ' if orange in [orange,red] then;']);
  5803. ConvertProgram;
  5804. CheckSource('TestSet_EnumConst',
  5805. LinesToStr([ // statements
  5806. 'this.TEnum = {',
  5807. ' "0": "Red",',
  5808. ' Red: 0,',
  5809. ' "1": "Blue",',
  5810. ' Blue: 1',
  5811. '};',
  5812. 'this.Orange = $mod.TEnum.Red;',
  5813. 'this.Enum = 0;',
  5814. 'this.Enums = {};',
  5815. '']),
  5816. LinesToStr([
  5817. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5818. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5819. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5820. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5821. '']));
  5822. end;
  5823. procedure TTestModule.TestSet_IntConst;
  5824. begin
  5825. StartProgram(false);
  5826. Add([
  5827. 'type',
  5828. ' TEnums = set of Byte;',
  5829. 'const',
  5830. ' Orange = 0;',
  5831. 'var',
  5832. ' Enum: byte;',
  5833. ' Enums: tenums;',
  5834. 'begin',
  5835. ' Enums:=[];',
  5836. ' Enums:=[0];',
  5837. ' Enums:=[1..2];',
  5838. //' Include(enums,orange);',
  5839. //' Exclude(enums,orange);',
  5840. ' if orange in enums then;',
  5841. ' if orange in [orange,1] then;']);
  5842. ConvertProgram;
  5843. CheckSource('TestSet_IntConst',
  5844. LinesToStr([ // statements
  5845. 'this.Orange = 0;',
  5846. 'this.Enum = 0;',
  5847. 'this.Enums = {};',
  5848. '']),
  5849. LinesToStr([
  5850. '$mod.Enums = {};',
  5851. '$mod.Enums = rtl.createSet(0);',
  5852. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5853. 'if (0 in $mod.Enums) ;',
  5854. 'if (0 in rtl.createSet(0, 1)) ;',
  5855. '']));
  5856. end;
  5857. procedure TTestModule.TestSet_AnonymousEnumType;
  5858. begin
  5859. StartProgram(false);
  5860. Add('type');
  5861. Add(' TFlags = set of (red, green);');
  5862. Add('const');
  5863. Add(' favorite = red;');
  5864. Add('var');
  5865. Add(' f: TFlags;');
  5866. Add(' i: longint;');
  5867. Add('begin');
  5868. Add(' Include(f,red);');
  5869. Add(' Include(f,favorite);');
  5870. Add(' i:=ord(red);');
  5871. Add(' i:=ord(favorite);');
  5872. Add(' i:=ord(low(TFlags));');
  5873. Add(' i:=ord(low(f));');
  5874. Add(' i:=ord(low(favorite));');
  5875. Add(' i:=ord(high(TFlags));');
  5876. Add(' i:=ord(high(f));');
  5877. Add(' i:=ord(high(favorite));');
  5878. Add(' f:=[green,favorite];');
  5879. ConvertProgram;
  5880. CheckSource('TestSet_AnonymousEnumType',
  5881. LinesToStr([ // statements
  5882. 'this.TFlags$a = {',
  5883. ' "0": "red",',
  5884. ' red: 0,',
  5885. ' "1": "green",',
  5886. ' green: 1',
  5887. '};',
  5888. 'this.favorite = $mod.TFlags$a.red;',
  5889. 'this.f = {};',
  5890. 'this.i = 0;',
  5891. '']),
  5892. LinesToStr([
  5893. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5894. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5895. '$mod.i = $mod.TFlags$a.red;',
  5896. '$mod.i = $mod.TFlags$a.red;',
  5897. '$mod.i = $mod.TFlags$a.red;',
  5898. '$mod.i = $mod.TFlags$a.red;',
  5899. '$mod.i = $mod.TFlags$a.red;',
  5900. '$mod.i = $mod.TFlags$a.green;',
  5901. '$mod.i = $mod.TFlags$a.green;',
  5902. '$mod.i = $mod.TFlags$a.green;',
  5903. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5904. '']));
  5905. end;
  5906. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5907. begin
  5908. exit;
  5909. StartProgram(false);
  5910. Add([
  5911. 'type',
  5912. ' TAtoZ = ''A''..''Z'';',
  5913. ' TSetOfAZ = set of TAtoZ;',
  5914. 'var',
  5915. ' c: char;',
  5916. ' a: TAtoZ;',
  5917. ' s: TSetOfAZ = [''P'',''A''];',
  5918. ' i: longint;',
  5919. 'begin',
  5920. ' Include(s,''S'');',
  5921. ' Include(s,c);',
  5922. ' Include(s,a);',
  5923. ' c:=low(TAtoZ);',
  5924. ' i:=ord(low(TAtoZ));',
  5925. ' a:=high(TAtoZ);',
  5926. ' a:=high(TSetOfAtoZ);',
  5927. ' s:=[a,c,''M''];',
  5928. '']);
  5929. ConvertProgram;
  5930. CheckSource('TestSet_AnonymousEnumTypeChar',
  5931. LinesToStr([ // statements
  5932. '']),
  5933. LinesToStr([
  5934. '']));
  5935. end;
  5936. procedure TTestModule.TestSet_ConstEnum;
  5937. begin
  5938. StartProgram(false);
  5939. Add([
  5940. 'type',
  5941. ' TEnum = (red,blue,green);',
  5942. ' TEnums = set of TEnum;',
  5943. 'const',
  5944. ' teAny = [low(TEnum)..high(TEnum)];',
  5945. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5946. 'var',
  5947. ' e: TEnum;',
  5948. ' s: TEnums;',
  5949. 'begin',
  5950. ' if blue in teAny then;',
  5951. ' if blue in teAny+[e] then;',
  5952. ' if blue in teAny+teRedBlue then;',
  5953. ' if e in [red,blue] then;',
  5954. ' s:=teAny;',
  5955. ' s:=teAny+[e];',
  5956. ' s:=[e]+teAny;',
  5957. ' s:=teAny+teRedBlue;',
  5958. ' s:=teAny+teRedBlue+[e];',
  5959. '']);
  5960. ConvertProgram;
  5961. CheckSource('TestSet_ConstEnum',
  5962. LinesToStr([ // statements
  5963. 'this.TEnum = {',
  5964. ' "0": "red",',
  5965. ' red: 0,',
  5966. ' "1": "blue",',
  5967. ' blue: 1,',
  5968. ' "2": "green",',
  5969. ' green: 2',
  5970. '};',
  5971. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5972. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5973. 'this.e = 0;',
  5974. 'this.s = {};',
  5975. '']),
  5976. LinesToStr([
  5977. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5978. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5979. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5980. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5981. '$mod.s = rtl.refSet($mod.teAny);',
  5982. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5983. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5984. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5985. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5986. '']));
  5987. end;
  5988. procedure TTestModule.TestSet_ConstChar;
  5989. begin
  5990. StartProgram(false);
  5991. Add([
  5992. 'const',
  5993. ' LowChars = [''a''..''z''];',
  5994. ' Chars = LowChars+[''A''..''Z''];',
  5995. ' sc = [''А'', ''Я''];',
  5996. 'var',
  5997. ' c: char;',
  5998. ' s: string;',
  5999. 'begin',
  6000. ' if c in lowchars then ;',
  6001. ' if ''a'' in lowchars then ;',
  6002. ' if s[1] in lowchars then ;',
  6003. ' if c in chars then ;',
  6004. ' if c in [''a''..''z'',''_''] then ;',
  6005. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6006. ' if ''Я'' in sc then ;',
  6007. ' if 3=ord('' '') then ;',
  6008. '']);
  6009. ConvertProgram;
  6010. CheckSource('TestSet_ConstChar',
  6011. LinesToStr([ // statements
  6012. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6013. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  6014. 'this.sc = rtl.createSet(1040, 1071);',
  6015. 'this.c = "";',
  6016. 'this.s = "";',
  6017. '']),
  6018. LinesToStr([
  6019. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6020. 'if (97 in $mod.LowChars) ;',
  6021. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6022. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6023. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6024. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6025. 'if (1071 in $mod.sc) ;',
  6026. 'if (3 === 32) ;',
  6027. '']));
  6028. end;
  6029. procedure TTestModule.TestSet_ConstInt;
  6030. begin
  6031. StartProgram(false);
  6032. Add([
  6033. 'const',
  6034. ' Months = [1..12];',
  6035. ' Mirror = [-12..-1]+Months;',
  6036. 'var',
  6037. ' i: smallint;',
  6038. 'begin',
  6039. ' if 3 in Months then;',
  6040. ' if i in Months+[i] then;',
  6041. ' if i in Months+Mirror then;',
  6042. ' if i in [4..6,8] then;',
  6043. '']);
  6044. ConvertProgram;
  6045. CheckSource('TestSet_ConstInt',
  6046. LinesToStr([ // statements
  6047. 'this.Months = rtl.createSet(null, 1, 12);',
  6048. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  6049. 'this.i = 0;',
  6050. '']),
  6051. LinesToStr([
  6052. 'if (3 in $mod.Months) ;',
  6053. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6054. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6055. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6056. '']));
  6057. end;
  6058. procedure TTestModule.TestSet_InFunction;
  6059. begin
  6060. StartProgram(false);
  6061. Add([
  6062. 'const',
  6063. ' TEnum = 3;',
  6064. ' TSetOfEnum = 4;',
  6065. ' TSetOfAno = 5;',
  6066. 'procedure DoIt;',
  6067. 'type',
  6068. ' TEnum = (red, blue);',
  6069. ' TSetOfEnum = set of TEnum;',
  6070. ' TSetOfAno = set of (up,down);',
  6071. 'var',
  6072. ' e: TEnum;',
  6073. ' se: TSetOfEnum;',
  6074. ' sa: TSetOfAno;',
  6075. 'begin',
  6076. ' se:=[e];',
  6077. ' sa:=[up];',
  6078. 'end;',
  6079. 'begin',
  6080. '']);
  6081. ConvertProgram;
  6082. CheckSource('TestSet_InFunction',
  6083. LinesToStr([ // statements
  6084. 'this.TEnum = 3;',
  6085. 'this.TSetOfEnum = 4;',
  6086. 'this.TSetOfAno = 5;',
  6087. 'var TEnum$1 = {',
  6088. ' "0": "red",',
  6089. ' red: 0,',
  6090. ' "1": "blue",',
  6091. ' blue: 1',
  6092. '};',
  6093. 'var TSetOfAno$a = {',
  6094. ' "0": "up",',
  6095. ' up: 0,',
  6096. ' "1": "down",',
  6097. ' down: 1',
  6098. '};',
  6099. 'this.DoIt = function () {',
  6100. ' var e = 0;',
  6101. ' var se = {};',
  6102. ' var sa = {};',
  6103. ' se = rtl.createSet(e);',
  6104. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6105. '};',
  6106. '']),
  6107. LinesToStr([
  6108. '']));
  6109. end;
  6110. procedure TTestModule.TestSet_ForIn;
  6111. begin
  6112. StartProgram(false);
  6113. Add([
  6114. 'type',
  6115. ' TEnum = (Red, Green, Blue);',
  6116. ' TEnumRg = green..blue;',
  6117. ' TSetOfEnum = set of TEnum;',
  6118. ' TSetOfEnumRg = set of TEnumRg;',
  6119. 'var',
  6120. ' e, e2: TEnum;',
  6121. ' er: TEnum;',
  6122. ' s: TSetOfEnum;',
  6123. 'begin',
  6124. ' for e in TSetOfEnum do ;',
  6125. ' for e in TSetOfEnumRg do ;',
  6126. ' for e in [] do e2:=e;',
  6127. ' for e in [red..green] do e2:=e;',
  6128. ' for e in [green,blue] do e2:=e;',
  6129. ' for e in [red,blue] do e2:=e;',
  6130. ' for e in s do e2:=e;',
  6131. ' for er in TSetOfEnumRg do ;',
  6132. '']);
  6133. ConvertProgram;
  6134. CheckSource('TestSet_ForIn',
  6135. LinesToStr([ // statements
  6136. 'this.TEnum = {',
  6137. ' "0":"Red",',
  6138. ' Red:0,',
  6139. ' "1":"Green",',
  6140. ' Green:1,',
  6141. ' "2":"Blue",',
  6142. ' Blue:2',
  6143. ' };',
  6144. 'this.e = 0;',
  6145. 'this.e2 = 0;',
  6146. 'this.er = 0;',
  6147. 'this.s = {};',
  6148. '']),
  6149. LinesToStr([
  6150. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6151. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6152. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6153. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6154. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6155. 'for (var $l1 in $mod.s){',
  6156. ' $mod.e = +$l1;',
  6157. ' $mod.e2 = $mod.e;',
  6158. '};',
  6159. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6160. '']));
  6161. end;
  6162. procedure TTestModule.TestNestBegin;
  6163. begin
  6164. StartProgram(false);
  6165. Add('begin');
  6166. Add(' begin');
  6167. Add(' begin');
  6168. Add(' end;');
  6169. Add(' begin');
  6170. Add(' if true then ;');
  6171. Add(' end;');
  6172. Add(' end;');
  6173. ConvertProgram;
  6174. CheckSource('TestNestBegin',
  6175. '',
  6176. 'if (true) ;');
  6177. end;
  6178. procedure TTestModule.TestUnitImplVars;
  6179. begin
  6180. StartUnit(false);
  6181. Add('interface');
  6182. Add('implementation');
  6183. Add('var');
  6184. Add(' V1:longint;');
  6185. Add(' V2:longint = 3;');
  6186. Add(' V3:string = ''abc'';');
  6187. ConvertUnit;
  6188. CheckSource('TestUnitImplVars',
  6189. LinesToStr([ // statements
  6190. 'var $impl = $mod.$impl;',
  6191. '']),
  6192. '', // this.$init
  6193. LinesToStr([ // implementation
  6194. '$impl.V1 = 0;',
  6195. '$impl.V2 = 3;',
  6196. '$impl.V3 = "abc";',
  6197. '']) );
  6198. end;
  6199. procedure TTestModule.TestUnitImplConsts;
  6200. begin
  6201. StartUnit(false);
  6202. Add('interface');
  6203. Add('implementation');
  6204. Add('const');
  6205. Add(' v1 = 3;');
  6206. Add(' v2:longint = 4;');
  6207. Add(' v3:string = ''abc'';');
  6208. ConvertUnit;
  6209. CheckSource('TestUnitImplConsts',
  6210. LinesToStr([ // statements
  6211. 'var $impl = $mod.$impl;',
  6212. '']),
  6213. '', // this.$init
  6214. LinesToStr([ // implementation
  6215. '$impl.v1 = 3;',
  6216. '$impl.v2 = 4;',
  6217. '$impl.v3 = "abc";',
  6218. '']) );
  6219. end;
  6220. procedure TTestModule.TestUnitImplRecord;
  6221. begin
  6222. StartUnit(false);
  6223. Add('interface');
  6224. Add('implementation');
  6225. Add('type');
  6226. Add(' TMyRecord = record');
  6227. Add(' i: longint;');
  6228. Add(' end;');
  6229. Add('var aRec: TMyRecord;');
  6230. Add('initialization');
  6231. Add(' arec.i:=3;');
  6232. ConvertUnit;
  6233. CheckSource('TestUnitImplRecord',
  6234. LinesToStr([ // statements
  6235. 'var $impl = $mod.$impl;',
  6236. '']),
  6237. // this.$init
  6238. '$impl.aRec.i = 3;',
  6239. LinesToStr([ // implementation
  6240. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6241. ' this.i = 0;',
  6242. ' this.$eq = function (b) {',
  6243. ' return this.i === b.i;',
  6244. ' };',
  6245. ' this.$assign = function (s) {',
  6246. ' this.i = s.i;',
  6247. ' return this;',
  6248. ' };',
  6249. '});',
  6250. '$impl.aRec = $impl.TMyRecord.$new();',
  6251. '']) );
  6252. end;
  6253. procedure TTestModule.TestRenameJSNameConflict;
  6254. begin
  6255. StartProgram(false);
  6256. Add('var apply: longint;');
  6257. Add('var bind: longint;');
  6258. Add('var call: longint;');
  6259. Add('begin');
  6260. ConvertProgram;
  6261. CheckSource('TestRenameJSNameConflict',
  6262. LinesToStr([ // statements
  6263. 'this.Apply = 0;',
  6264. 'this.Bind = 0;',
  6265. 'this.Call = 0;'
  6266. ]),
  6267. LinesToStr([ // this.$main
  6268. ''
  6269. ]));
  6270. end;
  6271. procedure TTestModule.TestLocalConst;
  6272. begin
  6273. StartProgram(false);
  6274. Add('procedure DoIt;');
  6275. Add('const');
  6276. Add(' cA: longint = 1;');
  6277. Add(' cB = 2;');
  6278. Add(' procedure Sub;');
  6279. Add(' const');
  6280. Add(' csA = 3;');
  6281. Add(' cB: double = 4;');
  6282. Add(' begin');
  6283. Add(' cb:=cb+csa;');
  6284. Add(' ca:=ca+csa+5;');
  6285. Add(' end;');
  6286. Add('begin');
  6287. Add(' ca:=ca+cb+6;');
  6288. Add('end;');
  6289. Add('begin');
  6290. ConvertProgram;
  6291. CheckSource('TestLocalConst',
  6292. LinesToStr([
  6293. 'var cA = 1;',
  6294. 'var cB = 2;',
  6295. 'var csA = 3;',
  6296. 'var cB$1 = 4;',
  6297. 'this.DoIt = function () {',
  6298. ' function Sub() {',
  6299. ' cB$1 = cB$1 + 3;',
  6300. ' cA = cA + 3 + 5;',
  6301. ' };',
  6302. ' cA = cA + 2 + 6;',
  6303. '};'
  6304. ]),
  6305. LinesToStr([
  6306. ]));
  6307. end;
  6308. procedure TTestModule.TestVarExternal;
  6309. begin
  6310. StartProgram(false);
  6311. Add('var');
  6312. Add(' NaN: double; external name ''Global.NaN'';');
  6313. Add(' d: double;');
  6314. Add('begin');
  6315. Add(' d:=NaN;');
  6316. ConvertProgram;
  6317. CheckSource('TestVarExternal',
  6318. LinesToStr([
  6319. 'this.d = 0.0;'
  6320. ]),
  6321. LinesToStr([
  6322. '$mod.d = Global.NaN;'
  6323. ]));
  6324. end;
  6325. procedure TTestModule.TestVarExternalOtherUnit;
  6326. begin
  6327. AddModuleWithIntfImplSrc('unit2.pas',
  6328. LinesToStr([
  6329. 'var NaN: double; external name ''Global.NaN'';',
  6330. 'var iV: longint;'
  6331. ]),
  6332. '');
  6333. StartUnit(true);
  6334. Add('interface');
  6335. Add('uses unit2;');
  6336. Add('implementation');
  6337. Add('var');
  6338. Add(' d: double;');
  6339. Add(' i: longint; external name ''$i'';');
  6340. Add('begin');
  6341. Add(' d:=nan;');
  6342. Add(' d:=uNit2.nan;');
  6343. Add(' d:=test1.d;');
  6344. Add(' i:=iv;');
  6345. Add(' i:=uNit2.iv;');
  6346. Add(' i:=test1.i;');
  6347. ConvertUnit;
  6348. CheckSource('TestVarExternalOtherUnit',
  6349. LinesToStr([
  6350. 'var $impl = $mod.$impl;',
  6351. '']),
  6352. LinesToStr([ // this.$init
  6353. '$impl.d = Global.NaN;',
  6354. '$impl.d = Global.NaN;',
  6355. '$impl.d = $impl.d;',
  6356. '$i = pas.unit2.iV;',
  6357. '$i = pas.unit2.iV;',
  6358. '$i = $i;',
  6359. '']),
  6360. LinesToStr([ // implementation
  6361. '$impl.d = 0.0;',
  6362. '']) );
  6363. end;
  6364. procedure TTestModule.TestVarAbsoluteFail;
  6365. begin
  6366. StartProgram(false);
  6367. Add([
  6368. 'var',
  6369. ' a: longint;',
  6370. ' b: longword absolute a;',
  6371. 'begin']);
  6372. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6373. ConvertProgram;
  6374. end;
  6375. procedure TTestModule.TestConstExternal;
  6376. begin
  6377. StartProgram(false);
  6378. Add([
  6379. 'const',
  6380. ' PI: double; external name ''Global.PI'';',
  6381. ' Tau = 2*pi;',
  6382. 'var d: double;',
  6383. 'begin',
  6384. ' d:=pi;',
  6385. ' d:=tau+pi;']);
  6386. ConvertProgram;
  6387. CheckSource('TestConstExternal',
  6388. LinesToStr([
  6389. 'this.Tau = 2*Global.PI;',
  6390. 'this.d = 0.0;'
  6391. ]),
  6392. LinesToStr([
  6393. '$mod.d = Global.PI;',
  6394. '$mod.d = $mod.Tau + Global.PI;'
  6395. ]));
  6396. end;
  6397. procedure TTestModule.TestDouble;
  6398. begin
  6399. StartProgram(false);
  6400. Add([
  6401. 'type',
  6402. ' TDateTime = double;',
  6403. 'const',
  6404. ' a = TDateTime(2.7);',
  6405. ' b = a + TDateTime(1.7);',
  6406. ' c = 0.9 + 0.1;',
  6407. ' f0_1 = 0.1;',
  6408. ' f0_3 = 0.3;',
  6409. ' fn0_1 = -0.1;',
  6410. ' fn0_3 = -0.3;',
  6411. ' fn0_003 = -0.003;',
  6412. ' fn0_123456789 = -0.123456789;',
  6413. ' fn300_0 = -300.0;',
  6414. ' fn123456_0 = -123456.0;',
  6415. ' fn1234567_8 = -1234567.8;',
  6416. ' fn12345678_9 = -12345678.9;',
  6417. ' f1_0En12 = 1E-12;',
  6418. ' fn1_0En12 = -1E-12;',
  6419. ' maxdouble = 1.7e+308;',
  6420. ' mindouble = -1.7e+308;',
  6421. ' MinSafeIntDouble = -$1fffffffffffff;',
  6422. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6423. ' MaxSafeIntDouble = $1fffffffffffff;',
  6424. ' DZeroResolution = 1E-12;',
  6425. ' Minus1 = -1E-12;',
  6426. ' EPS = 1E-9;',
  6427. ' DELTA = 0.001;',
  6428. ' Big = 129.789E+100;',
  6429. ' Test0_15 = 0.15;',
  6430. ' Test999 = 2.9999999999999;',
  6431. ' Test111999 = 211199999999999000.0;',
  6432. ' TestMinus111999 = -211199999999999000.0;',
  6433. 'var',
  6434. ' d: double = b;',
  6435. 'begin',
  6436. ' d:=1.0;',
  6437. ' d:=1.0/3.0;',
  6438. ' d:=1/3;',
  6439. ' d:=5.0E-324;',
  6440. ' d:=1.7E308;',
  6441. ' d:=001.00E00;',
  6442. ' d:=002.00E001;',
  6443. ' d:=003.000E000;',
  6444. ' d:=-004.00E-00;',
  6445. ' d:=-005.00E-001;',
  6446. ' d:=10**3;',
  6447. ' d:=10 mod 3;',
  6448. ' d:=10 div 3;',
  6449. ' d:=c;',
  6450. ' d:=f0_1;',
  6451. ' d:=f0_3;',
  6452. ' d:=fn0_1;',
  6453. ' d:=fn0_3;',
  6454. ' d:=fn0_003;',
  6455. ' d:=fn0_123456789;',
  6456. ' d:=fn300_0;',
  6457. ' d:=fn123456_0;',
  6458. ' d:=fn1234567_8;',
  6459. ' d:=fn12345678_9;',
  6460. ' d:=f1_0En12;',
  6461. ' d:=fn1_0En12;',
  6462. ' d:=maxdouble;',
  6463. ' d:=mindouble;',
  6464. ' d:=MinSafeIntDouble;',
  6465. ' d:=double(MinSafeIntDouble);',
  6466. ' d:=MinSafeIntDouble2;',
  6467. ' d:=double(MinSafeIntDouble2);',
  6468. ' d:=MaxSafeIntDouble;',
  6469. ' d:=default(double);',
  6470. '']);
  6471. ConvertProgram;
  6472. CheckSource('TestDouble',
  6473. LinesToStr([
  6474. 'this.a = 2.7;',
  6475. 'this.b = 2.7 + 1.7;',
  6476. 'this.c = 0.9 + 0.1;',
  6477. 'this.f0_1 = 0.1;',
  6478. 'this.f0_3 = 0.3;',
  6479. 'this.fn0_1 = -0.1;',
  6480. 'this.fn0_3 = -0.3;',
  6481. 'this.fn0_003 = -0.003;',
  6482. 'this.fn0_123456789 = -0.123456789;',
  6483. 'this.fn300_0 = -300.0;',
  6484. 'this.fn123456_0 = -123456.0;',
  6485. 'this.fn1234567_8 = -1234567.8;',
  6486. 'this.fn12345678_9 = -12345678.9;',
  6487. 'this.f1_0En12 = 1E-12;',
  6488. 'this.fn1_0En12 = -1E-12;',
  6489. 'this.maxdouble = 1.7e+308;',
  6490. 'this.mindouble = -1.7e+308;',
  6491. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6492. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6493. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6494. 'this.DZeroResolution = 1E-12;',
  6495. 'this.Minus1 = -1E-12;',
  6496. 'this.EPS = 1E-9;',
  6497. 'this.DELTA = 0.001;',
  6498. 'this.Big = 129.789E+100;',
  6499. 'this.Test0_15 = 0.15;',
  6500. 'this.Test999 = 2.9999999999999;',
  6501. 'this.Test111999 = 211199999999999000.0;',
  6502. 'this.TestMinus111999 = -211199999999999000.0;',
  6503. 'this.d = 4.4;'
  6504. ]),
  6505. LinesToStr([
  6506. '$mod.d = 1.0;',
  6507. '$mod.d = 1.0 / 3.0;',
  6508. '$mod.d = 1 / 3;',
  6509. '$mod.d = 5.0E-324;',
  6510. '$mod.d = 1.7E308;',
  6511. '$mod.d = 1.00E0;',
  6512. '$mod.d = 2.00E1;',
  6513. '$mod.d = 3.000E0;',
  6514. '$mod.d = -4.00E-0;',
  6515. '$mod.d = -5.00E-1;',
  6516. '$mod.d = Math.pow(10, 3);',
  6517. '$mod.d = 10 % 3;',
  6518. '$mod.d = Math.floor(10 / 3);',
  6519. '$mod.d = 1;',
  6520. '$mod.d = 0.1;',
  6521. '$mod.d = 0.3;',
  6522. '$mod.d = -0.1;',
  6523. '$mod.d = -0.3;',
  6524. '$mod.d = -0.003;',
  6525. '$mod.d = -0.123456789;',
  6526. '$mod.d = -300;',
  6527. '$mod.d = -123456;',
  6528. '$mod.d = -1234567.8;',
  6529. '$mod.d = -1.23456789E7;',
  6530. '$mod.d = 1E-12;',
  6531. '$mod.d = -1E-12;',
  6532. '$mod.d = 1.7E308;',
  6533. '$mod.d = -1.7E308;',
  6534. '$mod.d = -9007199254740991;',
  6535. '$mod.d = -9007199254740991;',
  6536. '$mod.d = -9.007199254740992E15;',
  6537. '$mod.d = -9.007199254740992E15;',
  6538. '$mod.d = 9007199254740991;',
  6539. '$mod.d = 0.0;',
  6540. '']));
  6541. end;
  6542. procedure TTestModule.TestInteger;
  6543. begin
  6544. StartProgram(false);
  6545. Add([
  6546. 'const',
  6547. ' MinInt = low(NativeInt);',
  6548. ' MaxInt = high(NativeInt);',
  6549. 'type',
  6550. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6551. 'const',
  6552. ' a = low(TMyInt)+High(TMyInt);',
  6553. 'var',
  6554. ' i: TMyInt;',
  6555. 'begin',
  6556. ' i:=-MinInt;',
  6557. ' i:=default(TMyInt);',
  6558. ' i:=low(i)+high(i);',
  6559. '']);
  6560. ConvertProgram;
  6561. CheckSource('TestIntegerRange',
  6562. LinesToStr([
  6563. 'this.MinInt = -9007199254740991;',
  6564. 'this.MaxInt = 9007199254740991;',
  6565. 'this.a = -9007199254740991 + 9007199254740991;',
  6566. 'this.i = 0;',
  6567. '']),
  6568. LinesToStr([
  6569. '$mod.i = - -9007199254740991;',
  6570. '$mod.i = -9007199254740991;',
  6571. '$mod.i = -9007199254740991 + 9007199254740991;',
  6572. '']));
  6573. end;
  6574. procedure TTestModule.TestIntegerRange;
  6575. begin
  6576. StartProgram(false);
  6577. Add([
  6578. 'const',
  6579. ' MinInt = -1;',
  6580. ' MaxInt = +1;',
  6581. 'type',
  6582. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6583. ' TInt2 = 1..3;',
  6584. 'const',
  6585. ' a = low(TMyInt)+High(TMyInt);',
  6586. ' b = low(TInt2)+High(TInt2);',
  6587. ' s1 = [1];',
  6588. ' s2 = [1,2];',
  6589. ' s3 = [1..3];',
  6590. ' s4 = [low(shortint)..high(shortint)];',
  6591. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6592. ' s6 = 1 in s2;',
  6593. 'var',
  6594. ' i: TMyInt;',
  6595. ' i2: TInt2;',
  6596. 'begin',
  6597. ' i:=i2;',
  6598. ' i:=default(TMyInt);',
  6599. ' if i=i2 then ;']);
  6600. ConvertProgram;
  6601. CheckSource('TestIntegerRange',
  6602. LinesToStr([
  6603. 'this.MinInt = -1;',
  6604. 'this.MaxInt = +1;',
  6605. 'this.a = -1 + 1;',
  6606. 'this.b = 1 + 3;',
  6607. 'this.s1 = rtl.createSet(1);',
  6608. 'this.s2 = rtl.createSet(1, 2);',
  6609. 'this.s3 = rtl.createSet(null, 1, 3);',
  6610. 'this.s4 = rtl.createSet(null, -128, 127);',
  6611. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6612. 'this.s6 = 1 in $mod.s2;',
  6613. 'this.i = 0;',
  6614. 'this.i2 = 0;',
  6615. '']),
  6616. LinesToStr([
  6617. '$mod.i = $mod.i2;',
  6618. '$mod.i = -1;',
  6619. 'if ($mod.i === $mod.i2) ;',
  6620. '']));
  6621. end;
  6622. procedure TTestModule.TestIntegerTypecasts;
  6623. begin
  6624. StartProgram(false);
  6625. Add([
  6626. 'var',
  6627. ' i: nativeint;',
  6628. ' b: byte;',
  6629. ' sh: shortint;',
  6630. ' w: word;',
  6631. ' sm: smallint;',
  6632. ' lw: longword;',
  6633. ' li: longint;',
  6634. 'begin',
  6635. ' b:=byte(i);',
  6636. ' sh:=shortint(i);',
  6637. ' w:=word(i);',
  6638. ' sm:=smallint(i);',
  6639. ' lw:=longword(i);',
  6640. ' li:=longint(i);',
  6641. '']);
  6642. ConvertProgram;
  6643. CheckSource('TestIntegerTypecasts',
  6644. LinesToStr([
  6645. 'this.i = 0;',
  6646. 'this.b = 0;',
  6647. 'this.sh = 0;',
  6648. 'this.w = 0;',
  6649. 'this.sm = 0;',
  6650. 'this.lw = 0;',
  6651. 'this.li = 0;',
  6652. '']),
  6653. LinesToStr([
  6654. '$mod.b = $mod.i & 255;',
  6655. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6656. '$mod.w = $mod.i & 65535;',
  6657. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6658. '$mod.lw = $mod.i >>> 0;',
  6659. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6660. '']));
  6661. end;
  6662. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6663. begin
  6664. StartProgram(false);
  6665. Add([
  6666. 'var',
  6667. ' i,j: nativeint;',
  6668. 'begin',
  6669. ' i:=i shr 0;',
  6670. ' i:=i shr 1;',
  6671. ' i:=i shr 3;',
  6672. ' i:=i shr 54;',
  6673. ' i:=j shr i;',
  6674. '']);
  6675. ConvertProgram;
  6676. CheckResolverUnexpectedHints;
  6677. CheckSource('TestInteger_BitwiseShrNativeInt',
  6678. LinesToStr([
  6679. 'this.i = 0;',
  6680. 'this.j = 0;',
  6681. '']),
  6682. LinesToStr([
  6683. '$mod.i = $mod.i;',
  6684. '$mod.i = Math.floor($mod.i / 2);',
  6685. '$mod.i = Math.floor($mod.i / 8);',
  6686. '$mod.i = 0;',
  6687. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6688. '']));
  6689. end;
  6690. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6691. begin
  6692. StartProgram(false);
  6693. Add([
  6694. 'var',
  6695. ' i: nativeint;',
  6696. 'begin',
  6697. ' i:=i shl 0;',
  6698. ' i:=i shl 54;',
  6699. ' i:=123456789012 shl 1;',
  6700. ' i:=i shl 1;',
  6701. '']);
  6702. ConvertProgram;
  6703. CheckResolverUnexpectedHints;
  6704. CheckSource('TestInteger_BitwiseShrNativeInt',
  6705. LinesToStr([
  6706. 'this.i = 0;',
  6707. '']),
  6708. LinesToStr([
  6709. '$mod.i = $mod.i;',
  6710. '$mod.i = 0;',
  6711. '$mod.i = 246913578024;',
  6712. '$mod.i = rtl.shl($mod.i, 1);',
  6713. '']));
  6714. end;
  6715. procedure TTestModule.TestCurrency;
  6716. begin
  6717. StartProgram(false);
  6718. Add([
  6719. 'type',
  6720. ' TCoin = currency;',
  6721. 'const',
  6722. ' a = TCoin(2.7);',
  6723. ' b = a + TCoin(1.7);',
  6724. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6725. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6726. 'var',
  6727. ' c: TCoin = b;',
  6728. ' i: nativeint;',
  6729. ' d: double;',
  6730. ' j: jsvalue;',
  6731. 'function DoIt(c: currency): currency; begin end;',
  6732. 'function GetIt(d: double): double; begin end;',
  6733. 'procedure Write(v: jsvalue); begin end;',
  6734. 'begin',
  6735. ' c:=1.0;',
  6736. ' c:=0.1;',
  6737. ' c:=1.0/3.0;',
  6738. ' c:=1/3;',
  6739. ' c:=a;',
  6740. ' d:=c;',
  6741. ' c:=d;',
  6742. ' c:=currency(c);',
  6743. ' c:=currency(d);',
  6744. ' d:=double(c);',
  6745. ' c:=i;',
  6746. ' c:=currency(i);',
  6747. //' i:=c;', not allowed
  6748. ' i:=nativeint(c);',
  6749. ' c:=c+a;',
  6750. ' c:=-c-a;',
  6751. ' c:=d+c;',
  6752. ' c:=c+d;',
  6753. ' c:=d-c;',
  6754. ' c:=c-d;',
  6755. ' c:=c*a;',
  6756. ' c:=a*c;',
  6757. ' c:=d*c;',
  6758. ' c:=c*d;',
  6759. ' c:=c/a;',
  6760. ' c:=a/c;',
  6761. ' c:=d/c;',
  6762. ' c:=c/d;',
  6763. ' c:=c**a;',
  6764. ' c:=a**c;',
  6765. ' c:=d**c;',
  6766. ' c:=c**d;',
  6767. ' if c=c then ;',
  6768. ' if c=a then ;',
  6769. ' if a=c then ;',
  6770. ' if d=c then ;',
  6771. ' if c=d then ;',
  6772. ' c:=DoIt(c);',
  6773. ' c:=DoIt(i);',
  6774. ' c:=DoIt(d);',
  6775. ' c:=GetIt(c);',
  6776. ' j:=c;',
  6777. ' Write(c);',
  6778. ' c:=default(currency);',
  6779. ' j:=str(c);',
  6780. ' j:=str(c:0:3);',
  6781. '']);
  6782. ConvertProgram;
  6783. CheckSource('TestCurrency',
  6784. LinesToStr([
  6785. 'this.a = 27000;',
  6786. 'this.b = $mod.a + 17000;',
  6787. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6788. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6789. 'this.c = $mod.b;',
  6790. 'this.i = 0;',
  6791. 'this.d = 0.0;',
  6792. 'this.j = undefined;',
  6793. 'this.DoIt = function (c) {',
  6794. ' var Result = 0;',
  6795. ' return Result;',
  6796. '};',
  6797. 'this.GetIt = function (d) {',
  6798. ' var Result = 0.0;',
  6799. ' return Result;',
  6800. '};',
  6801. 'this.Write = function (v) {',
  6802. '};',
  6803. '']),
  6804. LinesToStr([
  6805. '$mod.c = 10000;',
  6806. '$mod.c = 1000;',
  6807. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6808. '$mod.c = Math.floor((1 / 3) * 10000);',
  6809. '$mod.c = $mod.a;',
  6810. '$mod.d = $mod.c / 10000;',
  6811. '$mod.c = Math.floor($mod.d * 10000);',
  6812. '$mod.c = $mod.c;',
  6813. '$mod.c = $mod.d * 10000;',
  6814. '$mod.d = $mod.c / 10000;',
  6815. '$mod.c = $mod.i * 10000;',
  6816. '$mod.c = $mod.i * 10000;',
  6817. '$mod.i = Math.floor($mod.c / 10000);',
  6818. '$mod.c = $mod.c + $mod.a;',
  6819. '$mod.c = -$mod.c - $mod.a;',
  6820. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6821. '$mod.c = $mod.c + ($mod.d * 10000);',
  6822. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6823. '$mod.c = $mod.c - ($mod.d * 10000);',
  6824. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6825. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6826. '$mod.c = $mod.d * $mod.c;',
  6827. '$mod.c = $mod.c * $mod.d;',
  6828. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6829. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6830. '$mod.c = Math.floor($mod.d / $mod.c);',
  6831. '$mod.c = Math.floor($mod.c / $mod.d);',
  6832. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6833. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6834. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6835. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6836. 'if ($mod.c === $mod.c) ;',
  6837. 'if ($mod.c === $mod.a) ;',
  6838. 'if ($mod.a === $mod.c) ;',
  6839. 'if (($mod.d * 10000) === $mod.c) ;',
  6840. 'if ($mod.c === ($mod.d * 10000)) ;',
  6841. '$mod.c = $mod.DoIt($mod.c);',
  6842. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6843. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6844. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6845. '$mod.j = $mod.c / 10000;',
  6846. '$mod.Write($mod.c / 10000);',
  6847. '$mod.c = 0;',
  6848. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6849. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6850. '']));
  6851. end;
  6852. procedure TTestModule.TestForBoolDo;
  6853. begin
  6854. StartProgram(false);
  6855. Add([
  6856. 'var b: boolean;',
  6857. 'begin',
  6858. ' for b:=false to true do ;',
  6859. ' for b:=b downto false do ;',
  6860. ' for b in boolean do ;',
  6861. '']);
  6862. ConvertProgram;
  6863. CheckSource('TestForBoolDo',
  6864. LinesToStr([ // statements
  6865. 'this.b = false;']),
  6866. LinesToStr([ // this.$main
  6867. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6868. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6869. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6870. '']));
  6871. end;
  6872. procedure TTestModule.TestForIntDo;
  6873. begin
  6874. StartProgram(false);
  6875. Add([
  6876. 'var i: longint;',
  6877. 'begin',
  6878. ' for i:=3 to 5 do ;',
  6879. ' for i:=i downto 2 do ;',
  6880. ' for i in byte do ;',
  6881. '']);
  6882. ConvertProgram;
  6883. CheckSource('TestForIntDo',
  6884. LinesToStr([ // statements
  6885. 'this.i = 0;']),
  6886. LinesToStr([ // this.$main
  6887. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6888. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6889. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6890. '']));
  6891. end;
  6892. procedure TTestModule.TestForIntInDo;
  6893. begin
  6894. StartProgram(false);
  6895. Add([
  6896. 'type',
  6897. ' TSetOfInt = set of byte;',
  6898. ' TIntRg = 3..7;',
  6899. ' TSetOfIntRg = set of TIntRg;',
  6900. 'var',
  6901. ' i,i2: longint;',
  6902. ' a1: array of byte;',
  6903. ' a2: array[1..3] of byte;',
  6904. ' soi: TSetOfInt;',
  6905. ' soir: TSetOfIntRg;',
  6906. ' ir: TIntRg;',
  6907. 'begin',
  6908. ' for i in byte do ;',
  6909. ' for i in a1 do ;',
  6910. ' for i in a2 do ;',
  6911. ' for i in [11..13] do ;',
  6912. ' for i in TSetOfInt do ;',
  6913. ' for i in TIntRg do ;',
  6914. ' for i in soi do i2:=i;',
  6915. ' for i in TSetOfIntRg do ;',
  6916. ' for i in soir do ;',
  6917. ' for ir in TIntRg do ;',
  6918. ' for ir in TSetOfIntRg do ;',
  6919. ' for ir in soir do ;',
  6920. '']);
  6921. ConvertProgram;
  6922. CheckSource('TestForIntInDo',
  6923. LinesToStr([ // statements
  6924. 'this.i = 0;',
  6925. 'this.i2 = 0;',
  6926. 'this.a1 = [];',
  6927. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6928. 'this.soi = {};',
  6929. 'this.soir = {};',
  6930. 'this.ir = 0;',
  6931. '']),
  6932. LinesToStr([ // this.$main
  6933. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6934. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6935. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6936. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6937. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6938. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6939. 'for (var $l11 in $mod.soi) {',
  6940. ' $mod.i = +$l11;',
  6941. ' $mod.i2 = $mod.i;',
  6942. '};',
  6943. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6944. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6945. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6946. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6947. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6948. '']));
  6949. end;
  6950. procedure TTestModule.TestCharConst;
  6951. begin
  6952. StartProgram(false);
  6953. Add([
  6954. 'const',
  6955. ' a = #$00F3;',
  6956. ' c: char = ''1'';',
  6957. 'begin',
  6958. ' c:=#0;',
  6959. ' c:=#1;',
  6960. ' c:=#9;',
  6961. ' c:=#10;',
  6962. ' c:=#13;',
  6963. ' c:=#31;',
  6964. ' c:=#32;',
  6965. ' c:=#$A;',
  6966. ' c:=#$0A;',
  6967. ' c:=#$b;',
  6968. ' c:=#$0b;',
  6969. ' c:=^A;',
  6970. ' c:=''"'';',
  6971. ' c:=default(char);',
  6972. ' c:=#$00E4;', // ä
  6973. ' c:=''ä'';',
  6974. ' c:=#$E4;', // ä
  6975. ' c:=#$D800;', // invalid UTF-16
  6976. ' c:=#$DFFF;', // invalid UTF-16
  6977. ' c:=#$FFFF;', // last UCS-2
  6978. ' c:=high(c);', // last UCS-2
  6979. '']);
  6980. ConvertProgram;
  6981. CheckSource('TestCharConst',
  6982. LinesToStr([
  6983. 'this.a="ó";',
  6984. 'this.c="1";'
  6985. ]),
  6986. LinesToStr([
  6987. '$mod.c="\x00";',
  6988. '$mod.c="\x01";',
  6989. '$mod.c="\t";',
  6990. '$mod.c="\n";',
  6991. '$mod.c="\r";',
  6992. '$mod.c="\x1F";',
  6993. '$mod.c=" ";',
  6994. '$mod.c="\n";',
  6995. '$mod.c="\n";',
  6996. '$mod.c="\x0B";',
  6997. '$mod.c="\x0B";',
  6998. '$mod.c="\x01";',
  6999. '$mod.c=''"'';',
  7000. '$mod.c="\x00";',
  7001. '$mod.c = "ä";',
  7002. '$mod.c = "ä";',
  7003. '$mod.c = "ä";',
  7004. '$mod.c="\uD800";',
  7005. '$mod.c="\uDFFF";',
  7006. '$mod.c="\uFFFF";',
  7007. '$mod.c="\uFFFF";',
  7008. '']));
  7009. end;
  7010. procedure TTestModule.TestChar_Compare;
  7011. begin
  7012. StartProgram(false);
  7013. Add('var');
  7014. Add(' c: char;');
  7015. Add(' b: boolean;');
  7016. Add('begin');
  7017. Add(' b:=c=''1'';');
  7018. Add(' b:=''2''=c;');
  7019. Add(' b:=''3''=''4'';');
  7020. Add(' b:=c<>''5'';');
  7021. Add(' b:=''6''<>c;');
  7022. Add(' b:=c>''7'';');
  7023. Add(' b:=''8''>c;');
  7024. Add(' b:=c>=''9'';');
  7025. Add(' b:=''A''>=c;');
  7026. Add(' b:=c<''B'';');
  7027. Add(' b:=''C''<c;');
  7028. Add(' b:=c<=''D'';');
  7029. Add(' b:=''E''<=c;');
  7030. ConvertProgram;
  7031. CheckSource('TestChar_Compare',
  7032. LinesToStr([
  7033. 'this.c="";',
  7034. 'this.b = false;'
  7035. ]),
  7036. LinesToStr([
  7037. '$mod.b = $mod.c === "1";',
  7038. '$mod.b = "2" === $mod.c;',
  7039. '$mod.b = "3" === "4";',
  7040. '$mod.b = $mod.c !== "5";',
  7041. '$mod.b = "6" !== $mod.c;',
  7042. '$mod.b = $mod.c > "7";',
  7043. '$mod.b = "8" > $mod.c;',
  7044. '$mod.b = $mod.c >= "9";',
  7045. '$mod.b = "A" >= $mod.c;',
  7046. '$mod.b = $mod.c < "B";',
  7047. '$mod.b = "C" < $mod.c;',
  7048. '$mod.b = $mod.c <= "D";',
  7049. '$mod.b = "E" <= $mod.c;',
  7050. '']));
  7051. end;
  7052. procedure TTestModule.TestChar_BuiltInProcs;
  7053. begin
  7054. StartProgram(false);
  7055. Add([
  7056. 'var',
  7057. ' c: char;',
  7058. ' i: longint;',
  7059. ' s: string;',
  7060. 'begin',
  7061. ' i:=ord(c);',
  7062. ' i:=ord(s[i]);',
  7063. ' c:=chr(i);',
  7064. ' c:=pred(c);',
  7065. ' c:=succ(c);',
  7066. ' c:=low(c);',
  7067. ' c:=high(c);',
  7068. ' i:=byte(c);',
  7069. ' i:=word(c);',
  7070. ' i:=longint(c);',
  7071. '']);
  7072. ConvertProgram;
  7073. CheckSource('TestChar_BuiltInProcs',
  7074. LinesToStr([
  7075. 'this.c = "";',
  7076. 'this.i = 0;',
  7077. 'this.s = "";'
  7078. ]),
  7079. LinesToStr([
  7080. '$mod.i = $mod.c.charCodeAt();',
  7081. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7082. '$mod.c = String.fromCharCode($mod.i);',
  7083. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7084. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7085. '$mod.c = "\x00";',
  7086. '$mod.c = "\uFFFF";',
  7087. '$mod.i = $mod.c.charCodeAt() & 255;',
  7088. '$mod.i = $mod.c.charCodeAt();',
  7089. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7090. '']));
  7091. end;
  7092. procedure TTestModule.TestStringConst;
  7093. begin
  7094. StartProgram(false);
  7095. Add([
  7096. '{$H+}',
  7097. 'const',
  7098. ' a = #$00F3#$017C;', // first <256, then >=256
  7099. ' b = string(''a'');',
  7100. ' c = string(''ä'');',
  7101. ' d = UnicodeString(''b'');',
  7102. ' e = UnicodeString(''ö'');',
  7103. 'var',
  7104. ' s: string = ''abc'';',
  7105. 'begin',
  7106. ' s:='''';',
  7107. ' s:=#13#10;',
  7108. ' s:=#9''foo'';',
  7109. ' s:=#$A9;',
  7110. ' s:=''foo''#13''bar'';',
  7111. ' s:=''"'';',
  7112. ' s:=''"''''"'';',
  7113. ' s:=#$20AC;', // euro
  7114. ' s:=#$10437;', // outside BMP
  7115. ' s:=default(string);',
  7116. ' s:=concat(s);',
  7117. ' s:=concat(s,''a'',s)',
  7118. '']);
  7119. ConvertProgram;
  7120. CheckSource('TestStringConst',
  7121. LinesToStr([
  7122. 'this.a = "óż";',
  7123. 'this.b = "a";',
  7124. 'this.c = "ä";',
  7125. 'this.d = "b";',
  7126. 'this.e = "ö";',
  7127. 'this.s="abc";',
  7128. '']),
  7129. LinesToStr([
  7130. '$mod.s="";',
  7131. '$mod.s="\r\n";',
  7132. '$mod.s="\tfoo";',
  7133. '$mod.s="©";',
  7134. '$mod.s="foo\rbar";',
  7135. '$mod.s=''"'';',
  7136. '$mod.s=''"\''"'';',
  7137. '$mod.s="€";',
  7138. '$mod.s="'#$F0#$90#$90#$B7'";',
  7139. '$mod.s="";',
  7140. '$mod.s = $mod.s;',
  7141. '$mod.s = $mod.s.concat("a", $mod.s);',
  7142. '']));
  7143. end;
  7144. procedure TTestModule.TestStringConstSurrogate;
  7145. begin
  7146. StartProgram(false);
  7147. Add([
  7148. 'var',
  7149. ' s: string;',
  7150. 'begin',
  7151. ' s:=''😊'';', // 1F60A
  7152. '']);
  7153. ConvertProgram;
  7154. CheckSource('TestStringConstSurrogate',
  7155. LinesToStr([
  7156. 'this.s="";'
  7157. ]),
  7158. LinesToStr([
  7159. '$mod.s="😊";'
  7160. ]));
  7161. end;
  7162. procedure TTestModule.TestString_Length;
  7163. begin
  7164. StartProgram(false);
  7165. Add('const c = ''foo'';');
  7166. Add('var');
  7167. Add(' s: string;');
  7168. Add(' i: longint;');
  7169. Add('begin');
  7170. Add(' i:=length(s);');
  7171. Add(' i:=length(s+s);');
  7172. Add(' i:=length(''abc'');');
  7173. Add(' i:=length(c);');
  7174. ConvertProgram;
  7175. CheckSource('TestString_Length',
  7176. LinesToStr([
  7177. 'this.c = "foo";',
  7178. 'this.s = "";',
  7179. 'this.i = 0;',
  7180. '']),
  7181. LinesToStr([
  7182. '$mod.i = $mod.s.length;',
  7183. '$mod.i = ($mod.s+$mod.s).length;',
  7184. '$mod.i = "abc".length;',
  7185. '$mod.i = $mod.c.length;',
  7186. '']));
  7187. end;
  7188. procedure TTestModule.TestString_Compare;
  7189. begin
  7190. StartProgram(false);
  7191. Add('var');
  7192. Add(' s, t: string;');
  7193. Add(' b: boolean;');
  7194. Add('begin');
  7195. Add(' b:=s=t;');
  7196. Add(' b:=s<>t;');
  7197. Add(' b:=s>t;');
  7198. Add(' b:=s>=t;');
  7199. Add(' b:=s<t;');
  7200. Add(' b:=s<=t;');
  7201. ConvertProgram;
  7202. CheckSource('TestString_Compare',
  7203. LinesToStr([ // statements
  7204. 'this.s = "";',
  7205. 'this.t = "";',
  7206. 'this.b =false;'
  7207. ]),
  7208. LinesToStr([ // this.$main
  7209. '$mod.b = $mod.s === $mod.t;',
  7210. '$mod.b = $mod.s !== $mod.t;',
  7211. '$mod.b = $mod.s > $mod.t;',
  7212. '$mod.b = $mod.s >= $mod.t;',
  7213. '$mod.b = $mod.s < $mod.t;',
  7214. '$mod.b = $mod.s <= $mod.t;',
  7215. '']));
  7216. end;
  7217. procedure TTestModule.TestString_SetLength;
  7218. begin
  7219. StartProgram(false);
  7220. Add([
  7221. 'procedure DoIt(var s: string);',
  7222. 'begin',
  7223. ' SetLength(s,2);',
  7224. 'end;',
  7225. 'var s: string;',
  7226. 'begin',
  7227. ' SetLength(s,3);',
  7228. '']);
  7229. ConvertProgram;
  7230. CheckSource('TestString_SetLength',
  7231. LinesToStr([ // statements
  7232. 'this.DoIt = function (s) {',
  7233. ' s.set(rtl.strSetLength(s.get(), 2));',
  7234. '};',
  7235. 'this.s = "";',
  7236. '']),
  7237. LinesToStr([ // this.$main
  7238. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7239. ]));
  7240. end;
  7241. procedure TTestModule.TestString_CharAt;
  7242. begin
  7243. StartProgram(false);
  7244. Add([
  7245. 'var',
  7246. ' s: string;',
  7247. ' c: char;',
  7248. ' b: boolean;',
  7249. 'begin',
  7250. ' b:= s[1] = c;',
  7251. ' b:= c = s[1];',
  7252. ' b:= c <> s[1];',
  7253. ' b:= c > s[1];',
  7254. ' b:= c >= s[1];',
  7255. ' b:= c < s[2];',
  7256. ' b:= c <= s[1];',
  7257. ' s[1] := c;',
  7258. ' s[2+3] := c;']);
  7259. ConvertProgram;
  7260. CheckSource('TestString_CharAt',
  7261. LinesToStr([ // statements
  7262. 'this.s = "";',
  7263. 'this.c = "";',
  7264. 'this.b = false;'
  7265. ]),
  7266. LinesToStr([ // this.$main
  7267. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7268. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7269. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7270. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7271. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7272. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7273. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7274. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7275. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7276. '']));
  7277. end;
  7278. procedure TTestModule.TestStringHMinusFail;
  7279. begin
  7280. StartProgram(false);
  7281. Add([
  7282. '{$H-}',
  7283. 'var s: string;',
  7284. 'begin']);
  7285. ConvertProgram;
  7286. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7287. end;
  7288. procedure TTestModule.TestStr;
  7289. begin
  7290. StartProgram(false);
  7291. Add('var');
  7292. Add(' b: boolean;');
  7293. Add(' i: longint;');
  7294. Add(' d: double;');
  7295. Add(' s: string;');
  7296. Add('begin');
  7297. Add(' str(b,s);');
  7298. Add(' str(i,s);');
  7299. Add(' str(d,s);');
  7300. Add(' str(i:3,s);');
  7301. Add(' str(d:3:2,s);');
  7302. Add(' Str(12.456:12:1,s);');
  7303. Add(' Str(12.456:12,s);');
  7304. Add(' s:=str(b);');
  7305. Add(' s:=str(i);');
  7306. Add(' s:=str(d);');
  7307. Add(' s:=str(i,i);');
  7308. Add(' s:=str(i:3);');
  7309. Add(' s:=str(d:3:2);');
  7310. Add(' s:=str(i:4,i);');
  7311. Add(' s:=str(i,i:5);');
  7312. Add(' s:=str(i:4,i:5);');
  7313. Add(' s:=str(s,s);');
  7314. Add(' s:=str(s,''foo'');');
  7315. ConvertProgram;
  7316. CheckSource('TestStr',
  7317. LinesToStr([ // statements
  7318. 'this.b = false;',
  7319. 'this.i = 0;',
  7320. 'this.d = 0.0;',
  7321. 'this.s = "";',
  7322. '']),
  7323. LinesToStr([ // this.$main
  7324. '$mod.s = ""+$mod.b;',
  7325. '$mod.s = ""+$mod.i;',
  7326. '$mod.s = rtl.floatToStr($mod.d);',
  7327. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7328. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7329. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7330. '$mod.s = rtl.floatToStr(12.456,12);',
  7331. '$mod.s = ""+$mod.b;',
  7332. '$mod.s = ""+$mod.i;',
  7333. '$mod.s = rtl.floatToStr($mod.d);',
  7334. '$mod.s = ""+$mod.i+$mod.i;',
  7335. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7336. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7337. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7338. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7339. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7340. '$mod.s = $mod.s + $mod.s;',
  7341. '$mod.s = $mod.s + "foo";',
  7342. '']));
  7343. end;
  7344. procedure TTestModule.TestBaseType_AnsiStringFail;
  7345. begin
  7346. StartProgram(false);
  7347. Add('var s: AnsiString');
  7348. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7349. ConvertProgram;
  7350. end;
  7351. procedure TTestModule.TestBaseType_WideStringFail;
  7352. begin
  7353. StartProgram(false);
  7354. Add('var s: WideString');
  7355. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7356. ConvertProgram;
  7357. end;
  7358. procedure TTestModule.TestBaseType_ShortStringFail;
  7359. begin
  7360. StartProgram(false);
  7361. Add('var s: ShortString');
  7362. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7363. ConvertProgram;
  7364. end;
  7365. procedure TTestModule.TestBaseType_RawByteStringFail;
  7366. begin
  7367. StartProgram(false);
  7368. Add('var s: RawByteString');
  7369. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7370. ConvertProgram;
  7371. end;
  7372. procedure TTestModule.TestTypeShortstring_Fail;
  7373. begin
  7374. StartProgram(false);
  7375. Add('type t = string[12];');
  7376. Add('var s: t;');
  7377. Add('begin');
  7378. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7379. ConvertProgram;
  7380. end;
  7381. procedure TTestModule.TestCharSet_Custom;
  7382. begin
  7383. StartProgram(false);
  7384. Add([
  7385. 'type',
  7386. ' TCharRg = ''a''..''z'';',
  7387. ' TSetOfCharRg = set of TCharRg;',
  7388. ' TCharRg2 = ''m''..''p'';',
  7389. 'const',
  7390. ' crg: TCharRg = ''b'';',
  7391. 'var',
  7392. ' c: char;',
  7393. ' crg2: TCharRg2;',
  7394. ' s: TSetOfCharRg;',
  7395. 'begin',
  7396. ' c:=crg;',
  7397. ' crg:=c;',
  7398. ' crg2:=crg;',
  7399. ' if c=crg then ;',
  7400. ' if crg=c then ;',
  7401. ' if crg=crg2 then ;',
  7402. ' if c in s then ;',
  7403. ' if crg2 in s then ;',
  7404. ' c:=default(TCharRg);',
  7405. '']);
  7406. ConvertProgram;
  7407. CheckSource('TestCharSet_Custom',
  7408. LinesToStr([ // statements
  7409. 'this.crg = "b";',
  7410. 'this.c = "";',
  7411. 'this.crg2 = "m";',
  7412. 'this.s = {};',
  7413. '']),
  7414. LinesToStr([ // this.$main
  7415. '$mod.c = $mod.crg;',
  7416. '$mod.crg = $mod.c;',
  7417. '$mod.crg2 = $mod.crg;',
  7418. 'if ($mod.c === $mod.crg) ;',
  7419. 'if ($mod.crg === $mod.c) ;',
  7420. 'if ($mod.crg === $mod.crg2) ;',
  7421. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7422. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7423. '$mod.c = "a";',
  7424. '']));
  7425. end;
  7426. procedure TTestModule.TestForCharDo;
  7427. begin
  7428. StartProgram(false);
  7429. Add([
  7430. 'var c: char;',
  7431. 'begin',
  7432. ' for c:=''a'' to ''c'' do ;',
  7433. ' for c:=c downto ''a'' do ;',
  7434. ' for c:=''Б'' to ''Я'' do ;',
  7435. '']);
  7436. ConvertProgram;
  7437. CheckSource('TestForCharDo',
  7438. LinesToStr([ // statements
  7439. 'this.c = "";']),
  7440. LinesToStr([ // this.$main
  7441. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7442. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7443. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7444. '']));
  7445. end;
  7446. procedure TTestModule.TestForCharInDo;
  7447. begin
  7448. StartProgram(false);
  7449. Add([
  7450. 'type',
  7451. ' TSetOfChar = set of char;',
  7452. ' TCharRg = ''a''..''z'';',
  7453. ' TSetOfCharRg = set of TCharRg;',
  7454. 'const Foo = ''foo'';',
  7455. 'var',
  7456. ' c,c2: char;',
  7457. ' s: string;',
  7458. ' a1: array of char;',
  7459. ' a2: array[1..3] of char;',
  7460. ' soc: TSetOfChar;',
  7461. ' socr: TSetOfCharRg;',
  7462. ' cr: TCharRg;',
  7463. 'begin',
  7464. ' for c in foo do ;',
  7465. ' for c in s do ;',
  7466. ' for c in char do ;',
  7467. ' for c in a1 do ;',
  7468. ' for c in a2 do ;',
  7469. ' for c in [''1''..''3''] do ;',
  7470. ' for c in TSetOfChar do ;',
  7471. ' for c in TCharRg do ;',
  7472. ' for c in soc do c2:=c;',
  7473. ' for c in TSetOfCharRg do ;',
  7474. ' for c in socr do ;',
  7475. ' for cr in TCharRg do ;',
  7476. ' for cr in TSetOfCharRg do ;',
  7477. ' for cr in socr do ;',
  7478. '']);
  7479. ConvertProgram;
  7480. CheckSource('TestForCharInDo',
  7481. LinesToStr([ // statements
  7482. 'this.Foo = "foo";',
  7483. 'this.c = "";',
  7484. 'this.c2 = "";',
  7485. 'this.s = "";',
  7486. 'this.a1 = [];',
  7487. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7488. 'this.soc = {};',
  7489. 'this.socr = {};',
  7490. 'this.cr = "a";',
  7491. '']),
  7492. LinesToStr([ // this.$main
  7493. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7494. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7495. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7496. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7497. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7498. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7499. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7500. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7501. 'for (var $l17 in $mod.soc) {',
  7502. ' $mod.c = String.fromCharCode($l17);',
  7503. ' $mod.c2 = $mod.c;',
  7504. '};',
  7505. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7506. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7507. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7508. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7509. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7510. '']));
  7511. end;
  7512. procedure TTestModule.TestProcTwoArgs;
  7513. begin
  7514. StartProgram(false);
  7515. Add('procedure Test(a,b: longint);');
  7516. Add('begin');
  7517. Add('end;');
  7518. Add('begin');
  7519. ConvertProgram;
  7520. CheckSource('TestProcTwoArgs',
  7521. LinesToStr([ // statements
  7522. 'this.Test = function (a,b) {',
  7523. '};'
  7524. ]),
  7525. LinesToStr([ // this.$main
  7526. ''
  7527. ]));
  7528. end;
  7529. procedure TTestModule.TestProc_DefaultValue;
  7530. begin
  7531. StartProgram(false);
  7532. Add('procedure p1(i: longint = 1);');
  7533. Add('begin');
  7534. Add('end;');
  7535. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7536. Add('begin');
  7537. Add('end;');
  7538. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7539. Add('begin');
  7540. Add('end;');
  7541. Add('begin');
  7542. Add(' p1;');
  7543. Add(' p1();');
  7544. Add(' p1(11);');
  7545. Add(' p2;');
  7546. Add(' p2();');
  7547. Add(' p2(12);');
  7548. Add(' p2(13,''b'');');
  7549. Add(' p3();');
  7550. ConvertProgram;
  7551. CheckSource('TestProc_DefaultValue',
  7552. LinesToStr([ // statements
  7553. 'this.p1 = function (i) {',
  7554. '};',
  7555. 'this.p2 = function (i,c) {',
  7556. '};',
  7557. 'this.p3 = function (d,b,s) {',
  7558. '};'
  7559. ]),
  7560. LinesToStr([ // this.$main
  7561. ' $mod.p1(1);',
  7562. ' $mod.p1(1);',
  7563. ' $mod.p1(11);',
  7564. ' $mod.p2(1,"a");',
  7565. ' $mod.p2(1,"a");',
  7566. ' $mod.p2(12,"a");',
  7567. ' $mod.p2(13,"b");',
  7568. ' $mod.p3(1.0,false,"abc");'
  7569. ]));
  7570. end;
  7571. procedure TTestModule.TestFunctionInt;
  7572. begin
  7573. StartProgram(false);
  7574. Add('function MyTest(Bar: longint): longint;');
  7575. Add('begin');
  7576. Add(' Result:=2*bar');
  7577. Add('end;');
  7578. Add('begin');
  7579. ConvertProgram;
  7580. CheckSource('TestFunctionInt',
  7581. LinesToStr([ // statements
  7582. 'this.MyTest = function (Bar) {',
  7583. ' var Result = 0;',
  7584. ' Result = 2*Bar;',
  7585. ' return Result;',
  7586. '};'
  7587. ]),
  7588. LinesToStr([ // this.$main
  7589. ''
  7590. ]));
  7591. end;
  7592. procedure TTestModule.TestFunctionString;
  7593. begin
  7594. StartProgram(false);
  7595. Add('function Test(Bar: string): string;');
  7596. Add('begin');
  7597. Add(' Result:=bar+BAR');
  7598. Add('end;');
  7599. Add('begin');
  7600. ConvertProgram;
  7601. CheckSource('TestFunctionString',
  7602. LinesToStr([ // statements
  7603. 'this.Test = function (Bar) {',
  7604. ' var Result = "";',
  7605. ' Result = Bar+Bar;',
  7606. ' return Result;',
  7607. '};'
  7608. ]),
  7609. LinesToStr([ // this.$main
  7610. ''
  7611. ]));
  7612. end;
  7613. procedure TTestModule.TestIfThen;
  7614. begin
  7615. StartProgram(false);
  7616. Add([
  7617. 'var b: boolean;',
  7618. 'begin',
  7619. ' if b then ;',
  7620. ' if b then else ;']);
  7621. ConvertProgram;
  7622. CheckSource('TestIfThen',
  7623. LinesToStr([ // statements
  7624. 'this.b = false;',
  7625. '']),
  7626. LinesToStr([ // this.$main
  7627. 'if ($mod.b) ;',
  7628. 'if ($mod.b) ;',
  7629. '']));
  7630. end;
  7631. procedure TTestModule.TestForLoop;
  7632. begin
  7633. StartProgram(false);
  7634. Add('var');
  7635. Add(' vI, vJ, vN: longint;');
  7636. Add('begin');
  7637. Add(' VJ:=0;');
  7638. Add(' VN:=3;');
  7639. Add(' for VI:=1 to VN do');
  7640. Add(' begin');
  7641. Add(' VJ:=VJ+VI;');
  7642. Add(' end;');
  7643. ConvertProgram;
  7644. CheckSource('TestForLoop',
  7645. LinesToStr([ // statements
  7646. 'this.vI = 0;',
  7647. 'this.vJ = 0;',
  7648. 'this.vN = 0;'
  7649. ]),
  7650. LinesToStr([ // this.$main
  7651. ' $mod.vJ = 0;',
  7652. ' $mod.vN = 3;',
  7653. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7654. ' $mod.vI = $l1;',
  7655. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7656. ' };',
  7657. '']));
  7658. end;
  7659. procedure TTestModule.TestForLoopInsideFunction;
  7660. begin
  7661. StartProgram(false);
  7662. Add('function SumNumbers(Count: longint): longint;');
  7663. Add('var');
  7664. Add(' vI, vJ: longint;');
  7665. Add('begin');
  7666. Add(' vj:=0;');
  7667. Add(' for vi:=1 to count do');
  7668. Add(' begin');
  7669. Add(' vj:=vj+vi;');
  7670. Add(' end;');
  7671. Add('end;');
  7672. Add('begin');
  7673. Add(' sumnumbers(3);');
  7674. ConvertProgram;
  7675. CheckSource('TestForLoopInsideFunction',
  7676. LinesToStr([ // statements
  7677. 'this.SumNumbers = function (Count) {',
  7678. ' var Result = 0;',
  7679. ' var vI = 0;',
  7680. ' var vJ = 0;',
  7681. ' vJ = 0;',
  7682. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7683. ' vI = $l1;',
  7684. ' vJ = vJ + vI;',
  7685. ' };',
  7686. ' return Result;',
  7687. '};'
  7688. ]),
  7689. LinesToStr([ // $mod.$main
  7690. ' $mod.SumNumbers(3);'
  7691. ]));
  7692. end;
  7693. procedure TTestModule.TestForLoop_ReadVarAfter;
  7694. begin
  7695. StartProgram(false);
  7696. Add('var');
  7697. Add(' vI: longint;');
  7698. Add('begin');
  7699. Add(' for vi:=1 to 2 do ;');
  7700. Add(' if vi=3 then ;');
  7701. ConvertProgram;
  7702. CheckSource('TestForLoop',
  7703. LinesToStr([ // statements
  7704. 'this.vI = 0;'
  7705. ]),
  7706. LinesToStr([ // this.$main
  7707. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7708. ' if ($mod.vI===3) ;'
  7709. ]));
  7710. end;
  7711. procedure TTestModule.TestForLoop_Nested;
  7712. begin
  7713. StartProgram(false);
  7714. Add('function SumNumbers(Count: longint): longint;');
  7715. Add('var');
  7716. Add(' vI, vJ, vK: longint;');
  7717. Add('begin');
  7718. Add(' VK:=0;');
  7719. Add(' for VI:=1 to count do');
  7720. Add(' begin');
  7721. Add(' for vj:=1 to vi do');
  7722. Add(' begin');
  7723. Add(' vk:=VK+VI;');
  7724. Add(' end;');
  7725. Add(' end;');
  7726. Add('end;');
  7727. Add('begin');
  7728. Add(' sumnumbers(3);');
  7729. ConvertProgram;
  7730. CheckSource('TestForLoopInFunction',
  7731. LinesToStr([ // statements
  7732. 'this.SumNumbers = function (Count) {',
  7733. ' var Result = 0;',
  7734. ' var vI = 0;',
  7735. ' var vJ = 0;',
  7736. ' var vK = 0;',
  7737. ' vK = 0;',
  7738. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7739. ' vI = $l1;',
  7740. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7741. ' vJ = $l3;',
  7742. ' vK = vK + vI;',
  7743. ' };',
  7744. ' };',
  7745. ' return Result;',
  7746. '};'
  7747. ]),
  7748. LinesToStr([ // $mod.$main
  7749. ' $mod.SumNumbers(3);'
  7750. ]));
  7751. end;
  7752. procedure TTestModule.TestRepeatUntil;
  7753. begin
  7754. StartProgram(false);
  7755. Add('var');
  7756. Add(' vI, vJ, vN: longint;');
  7757. Add('begin');
  7758. Add(' vn:=3;');
  7759. Add(' vj:=0;');
  7760. Add(' VI:=0;');
  7761. Add(' repeat');
  7762. Add(' VI:=vi+1;');
  7763. Add(' vj:=VJ+vI;');
  7764. Add(' until vi>=vn');
  7765. ConvertProgram;
  7766. CheckSource('TestRepeatUntil',
  7767. LinesToStr([ // statements
  7768. 'this.vI = 0;',
  7769. 'this.vJ = 0;',
  7770. 'this.vN = 0;'
  7771. ]),
  7772. LinesToStr([ // $mod.$main
  7773. ' $mod.vN = 3;',
  7774. ' $mod.vJ = 0;',
  7775. ' $mod.vI = 0;',
  7776. ' do{',
  7777. ' $mod.vI = $mod.vI + 1;',
  7778. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7779. ' }while(!($mod.vI>=$mod.vN));'
  7780. ]));
  7781. end;
  7782. procedure TTestModule.TestAsmBlock;
  7783. begin
  7784. StartProgram(false);
  7785. Add([
  7786. 'var',
  7787. ' vI: longint;',
  7788. 'begin',
  7789. ' vi:=1;',
  7790. ' asm',
  7791. ' if (vI===1) {',
  7792. ' vI=2;',
  7793. //' console.log(''end;'');', ToDo
  7794. ' }',
  7795. ' if (vI===2){ vI=3; }',
  7796. ' end;',
  7797. ' VI:=4;']);
  7798. ConvertProgram;
  7799. CheckSource('TestAsmBlock',
  7800. LinesToStr([ // statements
  7801. 'this.vI = 0;'
  7802. ]),
  7803. LinesToStr([ // $mod.$main
  7804. '$mod.vI = 1;',
  7805. 'if (vI===1) {',
  7806. ' vI=2;',
  7807. '}',
  7808. 'if (vI===2){ vI=3; }',
  7809. ';',
  7810. '$mod.vI = 4;'
  7811. ]));
  7812. end;
  7813. procedure TTestModule.TestAsmPas_Impl;
  7814. begin
  7815. StartUnit(false);
  7816. Add('interface');
  7817. Add('const cIntf: longint = 1;');
  7818. Add('var vIntf: longint;');
  7819. Add('implementation');
  7820. Add('const cImpl: longint = 2;');
  7821. Add('var vImpl: longint;');
  7822. Add('procedure DoIt;');
  7823. Add('const cLoc: longint = 3;');
  7824. Add('var vLoc: longint;');
  7825. Add('begin;');
  7826. Add(' asm');
  7827. //Add(' pas(vIntf)=pas(cIntf);');
  7828. //Add(' pas(vImpl)=pas(cImpl);');
  7829. //Add(' pas(vLoc)=pas(cLoc);');
  7830. Add(' end;');
  7831. Add('end;');
  7832. ConvertUnit;
  7833. CheckSource('TestAsmPas_Impl',
  7834. LinesToStr([
  7835. 'var $impl = $mod.$impl;',
  7836. 'this.cIntf = 1;',
  7837. 'this.vIntf = 0;',
  7838. '']),
  7839. '', // this.$init
  7840. LinesToStr([ // implementation
  7841. '$impl.cImpl = 2;',
  7842. '$impl.vImpl = 0;',
  7843. 'var cLoc = 3;',
  7844. '$impl.DoIt = function () {',
  7845. ' var vLoc = 0;',
  7846. '};',
  7847. '']) );
  7848. end;
  7849. procedure TTestModule.TestTryFinally;
  7850. begin
  7851. StartProgram(false);
  7852. Add('var i: longint;');
  7853. Add('begin');
  7854. Add(' try');
  7855. Add(' i:=0; i:=2 div i;');
  7856. Add(' finally');
  7857. Add(' i:=3');
  7858. Add(' end;');
  7859. ConvertProgram;
  7860. CheckSource('TestTryFinally',
  7861. LinesToStr([ // statements
  7862. 'this.i = 0;'
  7863. ]),
  7864. LinesToStr([ // $mod.$main
  7865. 'try {',
  7866. ' $mod.i = 0;',
  7867. ' $mod.i = Math.floor(2 / $mod.i);',
  7868. '} finally {',
  7869. ' $mod.i = 3;',
  7870. '};'
  7871. ]));
  7872. end;
  7873. procedure TTestModule.TestTryExcept;
  7874. begin
  7875. StartProgram(false);
  7876. Add([
  7877. 'type',
  7878. ' TObject = class end;',
  7879. ' Exception = class Msg: string; end;',
  7880. ' EInvalidCast = class(Exception) end;',
  7881. 'var vI: longint;',
  7882. 'begin',
  7883. ' try',
  7884. ' vi:=1;',
  7885. ' except',
  7886. ' vi:=2',
  7887. ' end;',
  7888. ' try',
  7889. ' vi:=3;',
  7890. ' except',
  7891. ' raise;',
  7892. ' end;',
  7893. ' try',
  7894. ' VI:=4;',
  7895. ' except',
  7896. ' on einvalidcast do',
  7897. ' raise;',
  7898. ' on E: exception do',
  7899. ' if e.msg='''' then',
  7900. ' raise e;',
  7901. ' else',
  7902. ' vi:=5',
  7903. ' end;',
  7904. ' try',
  7905. ' VI:=6;',
  7906. ' except',
  7907. ' on einvalidcast do ;',
  7908. ' end;',
  7909. '']);
  7910. ConvertProgram;
  7911. CheckSource('TestTryExcept',
  7912. LinesToStr([ // statements
  7913. 'rtl.createClass($mod, "TObject", null, function () {',
  7914. ' this.$init = function () {',
  7915. ' };',
  7916. ' this.$final = function () {',
  7917. ' };',
  7918. '});',
  7919. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7920. ' this.$init = function () {',
  7921. ' $mod.TObject.$init.call(this);',
  7922. ' this.Msg = "";',
  7923. ' };',
  7924. '});',
  7925. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7926. '});',
  7927. 'this.vI = 0;'
  7928. ]),
  7929. LinesToStr([ // $mod.$main
  7930. 'try {',
  7931. ' $mod.vI = 1;',
  7932. '} catch ($e) {',
  7933. ' $mod.vI = 2;',
  7934. '};',
  7935. 'try {',
  7936. ' $mod.vI = 3;',
  7937. '} catch ($e) {',
  7938. ' throw $e;',
  7939. '};',
  7940. 'try {',
  7941. ' $mod.vI = 4;',
  7942. '} catch ($e) {',
  7943. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7944. ' throw $e',
  7945. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7946. ' var E = $e;',
  7947. ' if (E.Msg === "") throw E;',
  7948. ' } else {',
  7949. ' $mod.vI = 5;',
  7950. ' }',
  7951. '};',
  7952. 'try {',
  7953. ' $mod.vI = 6;',
  7954. '} catch ($e) {',
  7955. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7956. ' } else throw $e',
  7957. '};',
  7958. '']));
  7959. end;
  7960. procedure TTestModule.TestTryExcept_ReservedWords;
  7961. begin
  7962. StartProgram(false);
  7963. Add([
  7964. 'type',
  7965. ' TObject = class end;',
  7966. ' Exception = class',
  7967. ' Symbol: string;',
  7968. ' end;',
  7969. 'var &try: longint;',
  7970. 'begin',
  7971. ' try',
  7972. ' &try:=4;',
  7973. ' except',
  7974. ' on Error: exception do',
  7975. ' if errOR.symBol='''' then',
  7976. ' raise ERRor;',
  7977. ' end;',
  7978. '']);
  7979. ConvertProgram;
  7980. CheckSource('TestTryExcept_ReservedWords',
  7981. LinesToStr([ // statements
  7982. 'rtl.createClass($mod, "TObject", null, function () {',
  7983. ' this.$init = function () {',
  7984. ' };',
  7985. ' this.$final = function () {',
  7986. ' };',
  7987. '});',
  7988. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7989. ' this.$init = function () {',
  7990. ' $mod.TObject.$init.call(this);',
  7991. ' this.Symbol = "";',
  7992. ' };',
  7993. '});',
  7994. 'this.Try = 0;',
  7995. '']),
  7996. LinesToStr([ // $mod.$main
  7997. 'try {',
  7998. ' $mod.Try = 4;',
  7999. '} catch ($e) {',
  8000. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8001. ' var error = $e;',
  8002. ' if (error.Symbol === "") throw error;',
  8003. ' } else throw $e',
  8004. '};',
  8005. '']));
  8006. end;
  8007. procedure TTestModule.TestIfThenRaiseElse;
  8008. begin
  8009. StartProgram(false);
  8010. Add([
  8011. 'type',
  8012. ' TObject = class',
  8013. ' constructor Create;',
  8014. ' end;',
  8015. 'constructor TObject.Create;',
  8016. 'begin',
  8017. 'end;',
  8018. 'var b: boolean;',
  8019. 'begin',
  8020. ' if b then',
  8021. ' raise TObject.Create',
  8022. ' else',
  8023. ' b:=false;',
  8024. '']);
  8025. ConvertProgram;
  8026. CheckSource('TestIfThenRaiseElse',
  8027. LinesToStr([ // statements
  8028. 'rtl.createClass($mod, "TObject", null, function () {',
  8029. ' this.$init = function () {',
  8030. ' };',
  8031. ' this.$final = function () {',
  8032. ' };',
  8033. ' this.Create = function () {',
  8034. ' return this;',
  8035. ' };',
  8036. '});',
  8037. 'this.b = false;',
  8038. '']),
  8039. LinesToStr([ // $mod.$main
  8040. 'if ($mod.b) {',
  8041. ' throw $mod.TObject.$create("Create")}',
  8042. ' else $mod.b = false;',
  8043. '']));
  8044. end;
  8045. procedure TTestModule.TestCaseOf;
  8046. begin
  8047. StartProgram(false);
  8048. Add([
  8049. 'const e: longint; external name ''$e'';',
  8050. 'var vI: longint;',
  8051. 'begin',
  8052. ' case vi of',
  8053. ' 1: ;',
  8054. ' 2: vi:=3;',
  8055. ' e: ;',
  8056. ' else',
  8057. ' VI:=4',
  8058. ' end;']);
  8059. ConvertProgram;
  8060. CheckSource('TestCaseOf',
  8061. LinesToStr([ // statements
  8062. 'this.vI = 0;'
  8063. ]),
  8064. LinesToStr([ // $mod.$main
  8065. 'var $tmp1 = $mod.vI;',
  8066. 'if ($tmp1 === 1) {}',
  8067. 'else if ($tmp1 === 2) {',
  8068. ' $mod.vI = 3}',
  8069. ' else if ($tmp1 === $e) {}',
  8070. 'else {',
  8071. ' $mod.vI = 4;',
  8072. '};'
  8073. ]));
  8074. end;
  8075. procedure TTestModule.TestCaseOf_UseSwitch;
  8076. begin
  8077. StartProgram(false);
  8078. Converter.UseSwitchStatement:=true;
  8079. Add('var Vi: longint;');
  8080. Add('begin');
  8081. Add(' case vi of');
  8082. Add(' 1: ;');
  8083. Add(' 2: VI:=3;');
  8084. Add(' else');
  8085. Add(' vi:=4');
  8086. Add(' end;');
  8087. ConvertProgram;
  8088. CheckSource('TestCaseOf_UseSwitch',
  8089. LinesToStr([ // statements
  8090. 'this.Vi = 0;'
  8091. ]),
  8092. LinesToStr([ // $mod.$main
  8093. 'switch ($mod.Vi) {',
  8094. 'case 1:',
  8095. ' break;',
  8096. 'case 2:',
  8097. ' $mod.Vi = 3;',
  8098. ' break;',
  8099. 'default:',
  8100. ' $mod.Vi = 4;',
  8101. '};'
  8102. ]));
  8103. end;
  8104. procedure TTestModule.TestCaseOfNoElse;
  8105. begin
  8106. StartProgram(false);
  8107. Add('var Vi: longint;');
  8108. Add('begin');
  8109. Add(' case vi of');
  8110. Add(' 1: begin vi:=2; VI:=3; end;');
  8111. Add(' end;');
  8112. ConvertProgram;
  8113. CheckSource('TestCaseOfNoElse',
  8114. LinesToStr([ // statements
  8115. 'this.Vi = 0;'
  8116. ]),
  8117. LinesToStr([ // $mod.$main
  8118. 'var $tmp1 = $mod.Vi;',
  8119. 'if ($tmp1 === 1) {',
  8120. ' $mod.Vi = 2;',
  8121. ' $mod.Vi = 3;',
  8122. '};'
  8123. ]));
  8124. end;
  8125. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8126. begin
  8127. StartProgram(false);
  8128. Converter.UseSwitchStatement:=true;
  8129. Add('var vI: longint;');
  8130. Add('begin');
  8131. Add(' case vi of');
  8132. Add(' 1: begin VI:=2; vi:=3; end;');
  8133. Add(' end;');
  8134. ConvertProgram;
  8135. CheckSource('TestCaseOfNoElse_UseSwitch',
  8136. LinesToStr([ // statements
  8137. 'this.vI = 0;'
  8138. ]),
  8139. LinesToStr([ // $mod.$main
  8140. 'switch ($mod.vI) {',
  8141. 'case 1:',
  8142. ' $mod.vI = 2;',
  8143. ' $mod.vI = 3;',
  8144. ' break;',
  8145. '};'
  8146. ]));
  8147. end;
  8148. procedure TTestModule.TestCaseOfRange;
  8149. begin
  8150. StartProgram(false);
  8151. Add('var vI: longint;');
  8152. Add('begin');
  8153. Add(' case vi of');
  8154. Add(' 1..3: vi:=14;');
  8155. Add(' 4,5: vi:=16;');
  8156. Add(' 6..7,9..10: ;');
  8157. Add(' else ;');
  8158. Add(' end;');
  8159. ConvertProgram;
  8160. CheckSource('TestCaseOfRange',
  8161. LinesToStr([ // statements
  8162. 'this.vI = 0;'
  8163. ]),
  8164. LinesToStr([ // $mod.$main
  8165. 'var $tmp1 = $mod.vI;',
  8166. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  8167. ' $mod.vI = 14',
  8168. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  8169. ' $mod.vI = 16',
  8170. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  8171. ]));
  8172. end;
  8173. procedure TTestModule.TestCaseOfString;
  8174. begin
  8175. StartProgram(false);
  8176. Add([
  8177. 'var s,h: string;',
  8178. 'begin',
  8179. ' case s of',
  8180. ' ''foo'': s:=h;',
  8181. ' ''a''..''z'': h:=s;',
  8182. ' ''ў'', ''ё'': ;',
  8183. ' ''Б''..''Я'': ;',
  8184. ' end;',
  8185. '']);
  8186. ConvertProgram;
  8187. CheckSource('TestCaseOfString',
  8188. LinesToStr([ // statements
  8189. 'this.s = "";',
  8190. 'this.h = "";',
  8191. '']),
  8192. LinesToStr([ // $mod.$main
  8193. 'var $tmp1 = $mod.s;',
  8194. 'if ($tmp1 === "foo") {',
  8195. ' $mod.s = $mod.h}',
  8196. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8197. ' $mod.h = $mod.s}',
  8198. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8199. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8200. '']));
  8201. end;
  8202. procedure TTestModule.TestCaseOfChar;
  8203. begin
  8204. StartProgram(false);
  8205. Add([
  8206. 'var s,h: char;',
  8207. 'begin',
  8208. ' case s of',
  8209. ' ''a''..''z'': h:=s;',
  8210. ' ''ä'': ;',
  8211. ' ''ў'', ''ё'': ;',
  8212. ' ''Б''..''Я'': ;',
  8213. ' end;',
  8214. '']);
  8215. ConvertProgram;
  8216. CheckSource('TestCaseOfString',
  8217. LinesToStr([ // statements
  8218. 'this.s = "";',
  8219. 'this.h = "";',
  8220. '']),
  8221. LinesToStr([ // $mod.$main
  8222. 'var $tmp1 = $mod.s;',
  8223. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8224. ' $mod.h = $mod.s}',
  8225. ' else if ($tmp1 === "ä") {}',
  8226. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8227. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8228. '']));
  8229. end;
  8230. procedure TTestModule.TestCaseOfExternalClassConst;
  8231. begin
  8232. StartProgram(false);
  8233. Add([
  8234. '{$modeswitch externalclass}',
  8235. 'type',
  8236. ' TBird = class external name ''Bird''',
  8237. ' const e: longint;',
  8238. ' end;',
  8239. 'var vI: longint;',
  8240. 'begin',
  8241. ' case vi of',
  8242. ' 1: vi:=3;',
  8243. ' TBird.e: ;',
  8244. ' end;']);
  8245. ConvertProgram;
  8246. CheckSource('TestCaseOfExternalClassConst',
  8247. LinesToStr([ // statements
  8248. 'this.vI = 0;'
  8249. ]),
  8250. LinesToStr([ // $mod.$main
  8251. 'var $tmp1 = $mod.vI;',
  8252. 'if ($tmp1 === 1) {',
  8253. ' $mod.vI = 3}',
  8254. ' else if ($tmp1 === Bird.e) ;'
  8255. ]));
  8256. end;
  8257. procedure TTestModule.TestDebugger;
  8258. begin
  8259. StartProgram(false);
  8260. Add([
  8261. 'procedure DoIt;',
  8262. 'begin',
  8263. ' deBugger;',
  8264. ' DeBugger();',
  8265. 'end;',
  8266. 'begin',
  8267. ' Debugger;']);
  8268. ConvertProgram;
  8269. CheckSource('TestDebugger',
  8270. LinesToStr([ // statements
  8271. 'this.DoIt = function () {',
  8272. ' debugger;',
  8273. ' debugger;',
  8274. '};',
  8275. '']),
  8276. LinesToStr([ // $mod.$main
  8277. 'debugger;',
  8278. '']));
  8279. end;
  8280. procedure TTestModule.TestArray_Dynamic;
  8281. begin
  8282. StartProgram(false);
  8283. Add([
  8284. 'type',
  8285. ' TArrayInt = array of longint;',
  8286. 'var',
  8287. ' Arr: TArrayInt;',
  8288. ' i: longint;',
  8289. ' b: boolean;',
  8290. 'begin',
  8291. ' SetLength(arr,3);',
  8292. ' arr[0]:=4;',
  8293. ' arr[1]:=length(arr)+arr[0];',
  8294. ' arr[i]:=5;',
  8295. ' arr[arr[i]]:=arr[6];',
  8296. ' i:=low(arr);',
  8297. ' i:=high(arr);',
  8298. ' b:=Assigned(arr);',
  8299. ' Arr:=default(TArrayInt);']);
  8300. ConvertProgram;
  8301. CheckSource('TestArray_Dynamic',
  8302. LinesToStr([ // statements
  8303. 'this.Arr = [];',
  8304. 'this.i = 0;',
  8305. 'this.b = false;'
  8306. ]),
  8307. LinesToStr([ // $mod.$main
  8308. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8309. '$mod.Arr[0] = 4;',
  8310. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8311. '$mod.Arr[$mod.i] = 5;',
  8312. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8313. '$mod.i = 0;',
  8314. '$mod.i = rtl.length($mod.Arr) - 1;',
  8315. '$mod.b = rtl.length($mod.Arr) > 0;',
  8316. '$mod.Arr = [];',
  8317. '']));
  8318. end;
  8319. procedure TTestModule.TestArray_Dynamic_Nil;
  8320. begin
  8321. StartProgram(false);
  8322. Add('type');
  8323. Add(' TArrayInt = array of longint;');
  8324. Add('var');
  8325. Add(' Arr: TArrayInt;');
  8326. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8327. Add('begin');
  8328. Add(' arr:=nil;');
  8329. Add(' if arr=nil then;');
  8330. Add(' if nil=arr then;');
  8331. Add(' if arr<>nil then;');
  8332. Add(' if nil<>arr then;');
  8333. Add(' DoIt(nil,nil);');
  8334. ConvertProgram;
  8335. CheckSource('TestArray_Dynamic',
  8336. LinesToStr([ // statements
  8337. 'this.Arr = [];',
  8338. 'this.DoIt = function(i,j){',
  8339. '};'
  8340. ]),
  8341. LinesToStr([ // $mod.$main
  8342. '$mod.Arr = [];',
  8343. 'if (rtl.length($mod.Arr) === 0) ;',
  8344. 'if (rtl.length($mod.Arr) === 0) ;',
  8345. 'if (rtl.length($mod.Arr) > 0) ;',
  8346. 'if (rtl.length($mod.Arr) > 0) ;',
  8347. '$mod.DoIt([],[]);',
  8348. '']));
  8349. end;
  8350. procedure TTestModule.TestArray_DynMultiDimensional;
  8351. begin
  8352. StartProgram(false);
  8353. Add([
  8354. 'type',
  8355. ' TArrayInt = array of longint;',
  8356. ' TArrayArrayInt = array of TArrayInt;',
  8357. 'var',
  8358. ' Arr: TArrayInt;',
  8359. ' Arr2: TArrayArrayInt;',
  8360. ' i: longint;',
  8361. 'begin',
  8362. ' arr2:=nil;',
  8363. ' if arr2=nil then;',
  8364. ' if nil=arr2 then;',
  8365. ' i:=low(arr2);',
  8366. ' i:=low(arr2[1]);',
  8367. ' i:=high(arr2);',
  8368. ' i:=high(arr2[2]);',
  8369. ' arr2[3]:=arr;',
  8370. ' arr2[4][5]:=i;',
  8371. ' i:=arr2[6][7];',
  8372. ' arr2[8,9]:=i;',
  8373. ' i:=arr2[10,11];',
  8374. ' SetLength(arr2,14);',
  8375. ' SetLength(arr2[15],16);']);
  8376. ConvertProgram;
  8377. CheckSource('TestArray_Dynamic',
  8378. LinesToStr([ // statements
  8379. 'this.Arr = [];',
  8380. 'this.Arr2 = [];',
  8381. 'this.i = 0;'
  8382. ]),
  8383. LinesToStr([ // $mod.$main
  8384. '$mod.Arr2 = [];',
  8385. 'if (rtl.length($mod.Arr2) === 0) ;',
  8386. 'if (rtl.length($mod.Arr2) === 0) ;',
  8387. '$mod.i = 0;',
  8388. '$mod.i = 0;',
  8389. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8390. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8391. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8392. '$mod.Arr2[4][5] = $mod.i;',
  8393. '$mod.i = $mod.Arr2[6][7];',
  8394. '$mod.Arr2[8][9] = $mod.i;',
  8395. '$mod.i = $mod.Arr2[10][11];',
  8396. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8397. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8398. '']));
  8399. end;
  8400. procedure TTestModule.TestArray_DynamicAssign;
  8401. begin
  8402. StartProgram(false);
  8403. Add([
  8404. 'type',
  8405. ' TArrayInt = array of longint;',
  8406. ' TArrayArrayInt = array of TArrayInt;',
  8407. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8408. 'begin',
  8409. 'end;',
  8410. 'procedure Fly(var a: TArrayInt);',
  8411. 'begin',
  8412. 'end;',
  8413. 'var',
  8414. ' Arr: TArrayInt;',
  8415. ' Arr2: TArrayArrayInt;',
  8416. 'begin',
  8417. ' arr:=nil;',
  8418. ' arr2:=nil;',
  8419. ' arr2[1]:=nil;',
  8420. ' arr2[2]:=arr;',
  8421. ' Run(arr,arr,arr);',
  8422. ' Fly(arr);',
  8423. ' Run(arr2[4],arr2[5],arr2[6]);',
  8424. ' Fly(arr2[7]);',
  8425. '']);
  8426. ConvertProgram;
  8427. CheckSource('TestArray_DynamicAssign',
  8428. LinesToStr([ // statements
  8429. 'this.Run = function (a, b, c) {',
  8430. '};',
  8431. 'this.Fly = function (a) {',
  8432. '};',
  8433. 'this.Arr = [];',
  8434. 'this.Arr2 = [];',
  8435. '']),
  8436. LinesToStr([ // $mod.$main
  8437. '$mod.Arr = [];',
  8438. '$mod.Arr2 = [];',
  8439. '$mod.Arr2[1] = [];',
  8440. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8441. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8442. '$mod.Fly({',
  8443. ' p: $mod,',
  8444. ' get: function () {',
  8445. ' return this.p.Arr;',
  8446. ' },',
  8447. ' set: function (v) {',
  8448. ' this.p.Arr = v;',
  8449. ' }',
  8450. '});',
  8451. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8452. '$mod.Fly({',
  8453. ' a: 7,',
  8454. ' p: $mod.Arr2,',
  8455. ' get: function () {',
  8456. ' return this.p[this.a];',
  8457. ' },',
  8458. ' set: function (v) {',
  8459. ' this.p[this.a] = v;',
  8460. ' }',
  8461. '});',
  8462. '']));
  8463. end;
  8464. procedure TTestModule.TestArray_StaticInt;
  8465. begin
  8466. StartProgram(false);
  8467. Add('type');
  8468. Add(' TArrayInt = array[2..4] of longint;');
  8469. Add('var');
  8470. Add(' Arr: TArrayInt;');
  8471. Add(' Arr2: TArrayInt = (5,6,7);');
  8472. Add(' i: longint;');
  8473. Add(' b: boolean;');
  8474. Add('begin');
  8475. Add(' arr[2]:=4;');
  8476. Add(' arr[3]:=arr[2]+arr[3];');
  8477. Add(' arr[i]:=5;');
  8478. Add(' arr[arr[i]]:=arr[high(arr)];');
  8479. Add(' i:=low(arr);');
  8480. Add(' i:=high(arr);');
  8481. Add(' b:=arr[2]=arr[3];');
  8482. Add(' arr:=default(TArrayInt);');
  8483. ConvertProgram;
  8484. CheckSource('TestArray_StaticInt',
  8485. LinesToStr([ // statements
  8486. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8487. 'this.Arr2 = [5, 6, 7];',
  8488. 'this.i = 0;',
  8489. 'this.b = false;'
  8490. ]),
  8491. LinesToStr([ // $mod.$main
  8492. '$mod.Arr[0] = 4;',
  8493. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8494. '$mod.Arr[$mod.i-2] = 5;',
  8495. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8496. '$mod.i = 2;',
  8497. '$mod.i = 4;',
  8498. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8499. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8500. '']));
  8501. end;
  8502. procedure TTestModule.TestArray_StaticBool;
  8503. begin
  8504. StartProgram(false);
  8505. Add('type');
  8506. Add(' TBools = array[boolean] of boolean;');
  8507. Add(' TBool2 = array[true..true] of boolean;');
  8508. Add('var');
  8509. Add(' Arr: TBools;');
  8510. Add(' Arr2: TBool2;');
  8511. Add(' Arr3: TBools = (true,false);');
  8512. Add(' b: boolean;');
  8513. Add('begin');
  8514. Add(' b:=low(arr);');
  8515. Add(' b:=high(arr);');
  8516. Add(' arr[true]:=false;');
  8517. Add(' arr[false]:=arr[b] or arr[true];');
  8518. Add(' arr[b]:=true;');
  8519. Add(' arr[arr[b]]:=arr[high(arr)];');
  8520. Add(' b:=arr[false]=arr[true];');
  8521. Add(' b:=low(arr2);');
  8522. Add(' b:=high(arr2);');
  8523. Add(' arr2[true]:=true;');
  8524. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8525. Add(' arr2[b]:=false;');
  8526. ConvertProgram;
  8527. CheckSource('TestArray_StaticBool',
  8528. LinesToStr([ // statements
  8529. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8530. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8531. 'this.Arr3 = [true, false];',
  8532. 'this.b = false;'
  8533. ]),
  8534. LinesToStr([ // $mod.$main
  8535. '$mod.b = false;',
  8536. '$mod.b = true;',
  8537. '$mod.Arr[1] = false;',
  8538. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8539. '$mod.Arr[+$mod.b] = true;',
  8540. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8541. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8542. '$mod.b = true;',
  8543. '$mod.b = true;',
  8544. '$mod.Arr2[0] = true;',
  8545. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8546. '$mod.Arr2[1-$mod.b] = false;',
  8547. '']));
  8548. end;
  8549. procedure TTestModule.TestArray_StaticChar;
  8550. begin
  8551. StartProgram(false);
  8552. Add([
  8553. 'type',
  8554. ' TChars = array[char] of char;',
  8555. ' TChars2 = array[''a''..''z''] of char;',
  8556. 'var',
  8557. ' Arr: TChars;',
  8558. ' Arr2: TChars2;',
  8559. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8560. ' Arr4: array[11..13] of char = ''pas'';',
  8561. ' Arr5: array[21..22] of char = ''äö'';',
  8562. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8563. ' c: char;',
  8564. ' b: boolean;',
  8565. 'begin',
  8566. ' c:=low(arr);',
  8567. ' c:=high(arr);',
  8568. ' arr[''B'']:=''a'';',
  8569. ' arr[''D'']:=arr[c];',
  8570. ' arr[c]:=arr[''d''];',
  8571. ' arr[arr[c]]:=arr[high(arr)];',
  8572. ' b:=arr[low(arr)]=arr[''e''];',
  8573. ' c:=low(arr2);',
  8574. ' c:=high(arr2);',
  8575. ' arr2[''b'']:=''f'';',
  8576. ' arr2[''a'']:=arr2[c];',
  8577. ' arr2[c]:=arr2[''g''];']);
  8578. ConvertProgram;
  8579. CheckSource('TestArray_StaticChar',
  8580. LinesToStr([ // statements
  8581. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8582. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8583. 'this.Arr3 = ["p", "a", "s"];',
  8584. 'this.Arr4 = ["p", "a", "s"];',
  8585. 'this.Arr5 = ["ä", "ö"];',
  8586. 'this.Arr6 = ["ä", "ö"];',
  8587. 'this.c = "";',
  8588. 'this.b = false;',
  8589. '']),
  8590. LinesToStr([ // $mod.$main
  8591. '$mod.c = "\x00";',
  8592. '$mod.c = "\uFFFF";',
  8593. '$mod.Arr[66] = "a";',
  8594. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8595. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8596. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8597. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8598. '$mod.c = "a";',
  8599. '$mod.c = "z";',
  8600. '$mod.Arr2[1] = "f";',
  8601. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8602. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8603. '']));
  8604. end;
  8605. procedure TTestModule.TestArray_StaticMultiDim;
  8606. begin
  8607. StartProgram(false);
  8608. Add([
  8609. 'type',
  8610. ' TArrayInt = array[1..3] of longint;',
  8611. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8612. 'var',
  8613. ' Arr: TArrayInt;',
  8614. ' Arr2: TArrayArrayInt;',
  8615. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8616. ' i: longint;',
  8617. 'begin',
  8618. ' i:=low(arr);',
  8619. ' i:=low(arr2);',
  8620. ' i:=low(arr2[5]);',
  8621. ' i:=high(arr);',
  8622. ' i:=high(arr2);',
  8623. ' i:=high(arr2[6]);',
  8624. ' arr2[5]:=arr;',
  8625. ' arr2[6][2]:=i;',
  8626. ' i:=arr2[6][3];',
  8627. ' arr2[6,3]:=i;',
  8628. ' i:=arr2[5,2];',
  8629. ' arr2:=arr2;',// clone multi dim static array
  8630. //' arr3:=arr3;',// clone anonymous multi dim static array
  8631. '']);
  8632. ConvertProgram;
  8633. CheckSource('TestArray_StaticMultiDim',
  8634. LinesToStr([ // statements
  8635. 'this.TArrayArrayInt$clone = function (a) {',
  8636. ' var r = [];',
  8637. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8638. ' return r;',
  8639. '};',
  8640. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8641. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8642. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8643. 'this.i = 0;'
  8644. ]),
  8645. LinesToStr([ // $mod.$main
  8646. '$mod.i = 1;',
  8647. '$mod.i = 5;',
  8648. '$mod.i = 1;',
  8649. '$mod.i = 3;',
  8650. '$mod.i = 6;',
  8651. '$mod.i = 3;',
  8652. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8653. '$mod.Arr2[1][1] = $mod.i;',
  8654. '$mod.i = $mod.Arr2[1][2];',
  8655. '$mod.Arr2[1][2] = $mod.i;',
  8656. '$mod.i = $mod.Arr2[0][1];',
  8657. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8658. '']));
  8659. end;
  8660. procedure TTestModule.TestArray_StaticInFunction;
  8661. begin
  8662. StartProgram(false);
  8663. Add([
  8664. 'const TArrayInt = 3;',
  8665. 'const TArrayArrayInt = 4;',
  8666. 'procedure DoIt;',
  8667. 'type',
  8668. ' TArrayInt = array[1..3] of longint;',
  8669. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8670. 'var',
  8671. ' Arr: TArrayInt;',
  8672. ' Arr2: TArrayArrayInt;',
  8673. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8674. ' i: longint;',
  8675. 'begin',
  8676. ' arr2[5]:=arr;',
  8677. ' arr2:=arr2;',// clone multi dim static array
  8678. 'end;',
  8679. 'begin',
  8680. '']);
  8681. ConvertProgram;
  8682. CheckSource('TestArray_StaticInFunction',
  8683. LinesToStr([ // statements
  8684. 'this.TArrayInt = 3;',
  8685. 'this.TArrayArrayInt = 4;',
  8686. 'var TArrayArrayInt$1$clone = function (a) {',
  8687. ' var r = [];',
  8688. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8689. ' return r;',
  8690. '};',
  8691. 'this.DoIt = function () {',
  8692. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8693. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8694. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8695. ' var i = 0;',
  8696. ' Arr2[0] = Arr.slice(0);',
  8697. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8698. '};',
  8699. '']),
  8700. LinesToStr([ // $mod.$main
  8701. '']));
  8702. end;
  8703. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8704. begin
  8705. StartProgram(false);
  8706. Add([
  8707. 'type',
  8708. ' TArrayInt = array[1..3,1..2] of longint;',
  8709. 'var',
  8710. ' a,b: TArrayInt;',
  8711. 'begin',
  8712. ' if a=b then ;',
  8713. '']);
  8714. SetExpectedPasResolverError('compare static array is not supported',
  8715. nXIsNotSupported);
  8716. ConvertProgram;
  8717. end;
  8718. procedure TTestModule.TestArrayOfRecord;
  8719. begin
  8720. StartProgram(false);
  8721. Add([
  8722. 'type',
  8723. ' TRec = record',
  8724. ' Int: longint;',
  8725. ' end;',
  8726. ' TArrayRec = array of TRec;',
  8727. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8728. 'begin',
  8729. 'end;',
  8730. 'var',
  8731. ' Arr: TArrayRec;',
  8732. ' r: TRec;',
  8733. ' i: longint;',
  8734. 'begin',
  8735. ' SetLength(arr,3);',
  8736. ' arr[0].int:=4;',
  8737. ' arr[1].int:=length(arr)+arr[2].int;',
  8738. ' arr[arr[i].int].int:=arr[5].int;',
  8739. ' arr[7]:=r;',
  8740. ' r:=arr[8];',
  8741. ' i:=low(arr);',
  8742. ' i:=high(arr);',
  8743. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8744. ConvertProgram;
  8745. CheckSource('TestArrayOfRecord',
  8746. LinesToStr([ // statements
  8747. 'rtl.recNewT($mod, "TRec", function () {',
  8748. ' this.Int = 0;',
  8749. ' this.$eq = function (b) {',
  8750. ' return this.Int === b.Int;',
  8751. ' };',
  8752. ' this.$assign = function (s) {',
  8753. ' this.Int = s.Int;',
  8754. ' return this;',
  8755. ' };',
  8756. '});',
  8757. 'this.DoIt = function (vd, vc, vv) {',
  8758. '};',
  8759. 'this.Arr = [];',
  8760. 'this.r = $mod.TRec.$new();',
  8761. 'this.i = 0;'
  8762. ]),
  8763. LinesToStr([ // $mod.$main
  8764. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8765. '$mod.Arr[0].Int = 4;',
  8766. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8767. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8768. '$mod.Arr[7].$assign($mod.r);',
  8769. '$mod.r.$assign($mod.Arr[8]);',
  8770. '$mod.i = 0;',
  8771. '$mod.i = rtl.length($mod.Arr)-1;',
  8772. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8773. '']));
  8774. end;
  8775. procedure TTestModule.TestArray_StaticRecord;
  8776. begin
  8777. StartProgram(false);
  8778. Add([
  8779. 'type',
  8780. ' TRec = record',
  8781. ' Int: longint;',
  8782. ' end;',
  8783. ' TArrayRec = array[1..2] of TRec;',
  8784. 'var',
  8785. ' Arr: TArrayRec;',
  8786. 'begin',
  8787. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8788. '']);
  8789. ConvertProgram;
  8790. CheckSource('TestArray_StaticRecord',
  8791. LinesToStr([ // statements
  8792. 'rtl.recNewT($mod, "TRec", function () {',
  8793. ' this.Int = 0;',
  8794. ' this.$eq = function (b) {',
  8795. ' return this.Int === b.Int;',
  8796. ' };',
  8797. ' this.$assign = function (s) {',
  8798. ' this.Int = s.Int;',
  8799. ' return this;',
  8800. ' };',
  8801. '});',
  8802. 'this.TArrayRec$clone = function (a) {',
  8803. ' var r = [];',
  8804. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8805. ' return r;',
  8806. '};',
  8807. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8808. '']),
  8809. LinesToStr([ // $mod.$main
  8810. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8811. end;
  8812. procedure TTestModule.TestArrayOfSet;
  8813. begin
  8814. StartProgram(false);
  8815. Add([
  8816. 'type',
  8817. ' TFlag = (big,small);',
  8818. ' TSetOfFlag = set of tflag;',
  8819. ' TArrayFlag = array of TSetOfFlag;',
  8820. 'procedure DoIt(const a: Tarrayflag);',
  8821. 'begin',
  8822. 'end;',
  8823. 'var',
  8824. ' f: TFlag;',
  8825. ' s: TSetOfFlag;',
  8826. ' Arr: TArrayFlag;',
  8827. ' i: longint;',
  8828. 'begin',
  8829. ' SetLength(arr,3);',
  8830. ' arr[0]:=s;',
  8831. ' arr[1]:=[big];',
  8832. ' arr[2]:=[big]+s;',
  8833. ' arr[3]:=s+[big];',
  8834. ' arr[4]:=arr[5];',
  8835. ' s:=arr[6];',
  8836. ' i:=low(arr);',
  8837. ' i:=high(arr);',
  8838. ' DoIt(arr);',
  8839. ' DoIt([s]);',
  8840. ' DoIt([[],s]);',
  8841. ' DoIt([s,[]]);',
  8842. '']);
  8843. ConvertProgram;
  8844. CheckSource('TestArrayOfSet',
  8845. LinesToStr([ // statements
  8846. 'this.TFlag = {',
  8847. ' "0": "big",',
  8848. ' big: 0,',
  8849. ' "1": "small",',
  8850. ' small: 1',
  8851. '};',
  8852. 'this.DoIt = function (a) {',
  8853. '};',
  8854. 'this.f = 0;',
  8855. 'this.s = {};',
  8856. 'this.Arr = [];',
  8857. 'this.i = 0;',
  8858. '']),
  8859. LinesToStr([ // $mod.$main
  8860. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8861. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8862. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8863. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8864. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8865. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8866. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8867. '$mod.i = 0;',
  8868. '$mod.i = rtl.length($mod.Arr) - 1;',
  8869. '$mod.DoIt($mod.Arr);',
  8870. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8871. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8872. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8873. '']));
  8874. end;
  8875. procedure TTestModule.TestArray_DynAsParam;
  8876. begin
  8877. StartProgram(false);
  8878. Add([
  8879. 'type integer = longint;',
  8880. 'type TArrInt = array of integer;',
  8881. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8882. 'var vJ: TArrInt;',
  8883. 'begin',
  8884. ' vg:=vg;',
  8885. ' vj:=vh;',
  8886. ' vi:=vi;',
  8887. ' doit(vg,vg,vg);',
  8888. ' doit(vh,vh,vj);',
  8889. ' doit(vi,vi,vi);',
  8890. ' doit(vj,vj,vj);',
  8891. 'end;',
  8892. 'var i: TArrInt;',
  8893. 'begin',
  8894. ' doit(i,i,i);']);
  8895. ConvertProgram;
  8896. CheckSource('TestArray_DynAsParams',
  8897. LinesToStr([ // statements
  8898. 'this.DoIt = function (vG,vH,vI) {',
  8899. ' var vJ = [];',
  8900. ' vG = rtl.arrayRef(vG);',
  8901. ' vJ = rtl.arrayRef(vH);',
  8902. ' vI.set(rtl.arrayRef(vI.get()));',
  8903. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  8904. ' get: function () {',
  8905. ' return vG;',
  8906. ' },',
  8907. ' set: function (v) {',
  8908. ' vG = v;',
  8909. ' }',
  8910. ' });',
  8911. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  8912. ' get: function () {',
  8913. ' return vJ;',
  8914. ' },',
  8915. ' set: function (v) {',
  8916. ' vJ = v;',
  8917. ' }',
  8918. ' });',
  8919. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  8920. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  8921. ' get: function () {',
  8922. ' return vJ;',
  8923. ' },',
  8924. ' set: function (v) {',
  8925. ' vJ = v;',
  8926. ' }',
  8927. ' });',
  8928. '};',
  8929. 'this.i = [];'
  8930. ]),
  8931. LinesToStr([
  8932. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  8933. ' p: $mod,',
  8934. ' get: function () {',
  8935. ' return this.p.i;',
  8936. ' },',
  8937. ' set: function (v) {',
  8938. ' this.p.i = v;',
  8939. ' }',
  8940. '});'
  8941. ]));
  8942. end;
  8943. procedure TTestModule.TestArray_StaticAsParam;
  8944. begin
  8945. StartProgram(false);
  8946. Add([
  8947. 'type integer = longint;',
  8948. 'type TArrInt = array[1..2] of integer;',
  8949. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8950. 'var vJ: TArrInt;',
  8951. 'begin',
  8952. ' vg:=vg;',
  8953. ' vj:=vh;',
  8954. ' vi:=vi;',
  8955. ' doit(vg,vg,vg);',
  8956. ' doit(vh,vh,vj);',
  8957. ' doit(vi,vi,vi);',
  8958. ' doit(vj,vj,vj);',
  8959. 'end;',
  8960. 'var i: TArrInt;',
  8961. 'begin',
  8962. ' doit(i,i,i);']);
  8963. ConvertProgram;
  8964. CheckSource('TestArray_StaticAsParams',
  8965. LinesToStr([ // statements
  8966. 'this.DoIt = function (vG,vH,vI) {',
  8967. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8968. ' vG = vG.slice(0);',
  8969. ' vJ = vH.slice(0);',
  8970. ' vI.set(vI.get().slice(0));',
  8971. ' $mod.DoIt(vG.slice(0), vG, {',
  8972. ' get: function () {',
  8973. ' return vG;',
  8974. ' },',
  8975. ' set: function (v) {',
  8976. ' vG = v;',
  8977. ' }',
  8978. ' });',
  8979. ' $mod.DoIt(vH.slice(0), vH, {',
  8980. ' get: function () {',
  8981. ' return vJ;',
  8982. ' },',
  8983. ' set: function (v) {',
  8984. ' vJ = v;',
  8985. ' }',
  8986. ' });',
  8987. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8988. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8989. ' get: function () {',
  8990. ' return vJ;',
  8991. ' },',
  8992. ' set: function (v) {',
  8993. ' vJ = v;',
  8994. ' }',
  8995. ' });',
  8996. '};',
  8997. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8998. ]),
  8999. LinesToStr([
  9000. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9001. ' p: $mod,',
  9002. ' get: function () {',
  9003. ' return this.p.i;',
  9004. ' },',
  9005. ' set: function (v) {',
  9006. ' this.p.i = v;',
  9007. ' }',
  9008. '});'
  9009. ]));
  9010. end;
  9011. procedure TTestModule.TestArrayElement_AsParams;
  9012. begin
  9013. StartProgram(false);
  9014. Add('type integer = longint;');
  9015. Add('type TArrayInt = array of integer;');
  9016. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9017. Add('var vJ: tarrayint;');
  9018. Add('begin');
  9019. Add(' vi:=vi;');
  9020. Add(' doit(vi,vi,vi);');
  9021. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9022. Add('end;');
  9023. Add('var a: TArrayInt;');
  9024. Add('begin');
  9025. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9026. ConvertProgram;
  9027. CheckSource('TestArrayElement_AsParams',
  9028. LinesToStr([ // statements
  9029. 'this.DoIt = function (vG,vH,vI) {',
  9030. ' var vJ = [];',
  9031. ' vI.set(vI.get());',
  9032. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9033. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9034. ' a:1+3,',
  9035. ' p:vJ,',
  9036. ' get: function () {',
  9037. ' return this.p[this.a];',
  9038. ' },',
  9039. ' set: function (v) {',
  9040. ' this.p[this.a] = v;',
  9041. ' }',
  9042. ' });',
  9043. '};',
  9044. 'this.a = [];'
  9045. ]),
  9046. LinesToStr([
  9047. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9048. ' a: 1+6,',
  9049. ' p: $mod.a,',
  9050. ' get: function () {',
  9051. ' return this.p[this.a];',
  9052. ' },',
  9053. ' set: function (v) {',
  9054. ' this.p[this.a] = v;',
  9055. ' }',
  9056. '});'
  9057. ]));
  9058. end;
  9059. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9060. begin
  9061. StartProgram(false);
  9062. Add('type Integer = longint;');
  9063. Add('type TArrayInt = array of integer;');
  9064. Add('function GetArr(vB: integer = 0): tarrayint;');
  9065. Add('begin');
  9066. Add('end;');
  9067. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9068. Add('begin');
  9069. Add('end;');
  9070. Add('begin');
  9071. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9072. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9073. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9074. ConvertProgram;
  9075. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9076. LinesToStr([ // statements
  9077. 'this.GetArr = function (vB) {',
  9078. ' var Result = [];',
  9079. ' return Result;',
  9080. '};',
  9081. 'this.DoIt = function (vG,vH,vI) {',
  9082. '};'
  9083. ]),
  9084. LinesToStr([
  9085. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9086. ' a: 1+3,',
  9087. ' p: $mod.GetArr(0),',
  9088. ' get: function () {',
  9089. ' return this.p[this.a];',
  9090. ' },',
  9091. ' set: function (v) {',
  9092. ' this.p[this.a] = v;',
  9093. ' }',
  9094. '});',
  9095. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9096. ' a: 2+3,',
  9097. ' p: $mod.GetArr(0),',
  9098. ' get: function () {',
  9099. ' return this.p[this.a];',
  9100. ' },',
  9101. ' set: function (v) {',
  9102. ' this.p[this.a] = v;',
  9103. ' }',
  9104. '});',
  9105. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9106. ' a: 3+3,',
  9107. ' p: $mod.GetArr(9),',
  9108. ' get: function () {',
  9109. ' return this.p[this.a];',
  9110. ' },',
  9111. ' set: function (v) {',
  9112. ' this.p[this.a] = v;',
  9113. ' }',
  9114. '});',
  9115. '']));
  9116. end;
  9117. procedure TTestModule.TestArrayEnumTypeRange;
  9118. begin
  9119. StartProgram(false);
  9120. Add([
  9121. 'type',
  9122. ' TEnum = (red,blue);',
  9123. ' TEnumArray = array[TEnum] of longint;',
  9124. 'var',
  9125. ' e: TEnum;',
  9126. ' i: longint;',
  9127. ' a: TEnumArray;',
  9128. ' numbers: TEnumArray = (1,2);',
  9129. ' names: array[TEnum] of string = (''red'',''blue'');',
  9130. 'begin',
  9131. ' e:=low(a);',
  9132. ' e:=high(a);',
  9133. ' i:=a[red];',
  9134. ' a[e]:=a[e];']);
  9135. ConvertProgram;
  9136. CheckSource('TestArrayEnumTypeRange',
  9137. LinesToStr([ // statements
  9138. ' this.TEnum = {',
  9139. ' "0": "red",',
  9140. ' red: 0,',
  9141. ' "1": "blue",',
  9142. ' blue: 1',
  9143. '};',
  9144. 'this.e = 0;',
  9145. 'this.i = 0;',
  9146. 'this.a = rtl.arraySetLength(null,0,2);',
  9147. 'this.numbers = [1, 2];',
  9148. 'this.names = ["red", "blue"];',
  9149. '']),
  9150. LinesToStr([ // $mod.$main
  9151. '$mod.e = $mod.TEnum.red;',
  9152. '$mod.e = $mod.TEnum.blue;',
  9153. '$mod.i = $mod.a[$mod.TEnum.red];',
  9154. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9155. '']));
  9156. end;
  9157. procedure TTestModule.TestArray_SetLengthOutArg;
  9158. begin
  9159. StartProgram(false);
  9160. Add([
  9161. 'type TArrInt = array of longint;',
  9162. 'procedure DoIt(out a: TArrInt);',
  9163. 'begin',
  9164. ' SetLength(a,2);',
  9165. 'end;',
  9166. 'begin',
  9167. '']);
  9168. ConvertProgram;
  9169. CheckSource('TestArray_SetLengthOutArg',
  9170. LinesToStr([ // statements
  9171. 'this.DoIt = function (a) {',
  9172. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9173. '};',
  9174. '']),
  9175. LinesToStr([
  9176. '']));
  9177. end;
  9178. procedure TTestModule.TestArray_SetLengthProperty;
  9179. begin
  9180. StartProgram(false);
  9181. Add('type');
  9182. Add(' TArrInt = array of longint;');
  9183. Add(' TObject = class');
  9184. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9185. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9186. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9187. Add(' end;');
  9188. Add('var Obj: TObject;');
  9189. Add('begin');
  9190. Add(' SetLength(Obj.Colors,2);');
  9191. ConvertProgram;
  9192. CheckSource('TestArray_SetLengthProperty',
  9193. LinesToStr([ // statements
  9194. 'rtl.createClass($mod, "TObject", null, function () {',
  9195. ' this.$init = function () {',
  9196. ' };',
  9197. ' this.$final = function () {',
  9198. ' };',
  9199. '});',
  9200. 'this.Obj = null;',
  9201. '']),
  9202. LinesToStr([
  9203. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9204. '']));
  9205. end;
  9206. procedure TTestModule.TestArray_SetLengthMultiDim;
  9207. begin
  9208. StartProgram(false);
  9209. Add([
  9210. 'type',
  9211. ' TArrArrInt = array of array of longint;',
  9212. ' TArrStaInt = array of array[1..2] of longint;',
  9213. 'var',
  9214. ' a: TArrArrInt;',
  9215. ' b: TArrStaInt;',
  9216. 'begin',
  9217. ' SetLength(a,2);',
  9218. ' SetLength(a,3,4);',
  9219. ' SetLength(b,5);',
  9220. '']);
  9221. ConvertProgram;
  9222. CheckSource('TestArray_SetLengthMultiDim',
  9223. LinesToStr([ // statements
  9224. 'this.a = [];',
  9225. 'this.b = [];',
  9226. '']),
  9227. LinesToStr([
  9228. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9229. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9230. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9231. '']));
  9232. end;
  9233. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9234. begin
  9235. StartProgram(false);
  9236. Add([
  9237. 'type',
  9238. ' TStaArr1 = array[1..3] of boolean;',
  9239. //' TStaArr2 = array[5..6] of TStaArr1;',
  9240. ' TDynArr1StaArr1 = array of TStaArr1;',
  9241. //' TDynArr1StaArr2 = array of TStaArr2;',
  9242. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9243. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9244. 'var',
  9245. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9246. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9247. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9248. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9249. 'begin',
  9250. ' SetLength(DynArr1StaArr1,11);',
  9251. ' SetLength(DynArr2StaArr1,12);',
  9252. ' SetLength(DynArr2StaArr1[13],14);',
  9253. ' SetLength(DynArr2StaArr1,15,16);',
  9254. //' SetLength(DynArr1StaArr2,21);',
  9255. //' SetLength(DynArr2StaArr2,22);',
  9256. //' SetLength(DynArr2StaArr2[23],24);',
  9257. //' SetLength(DynArr2StaArr2,25,26);',
  9258. '']);
  9259. ConvertProgram;
  9260. CheckSource('TestArray_DynOfStatic',
  9261. LinesToStr([ // statements
  9262. 'this.DynArr1StaArr1 = [];',
  9263. 'this.DynArr2StaArr1 = [];',
  9264. '']),
  9265. LinesToStr([ // $mod.$main
  9266. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9267. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9268. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9269. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9270. ' $mod.DynArr2StaArr1,',
  9271. ' false,',
  9272. ' 15,',
  9273. ' 16,',
  9274. ' "s",',
  9275. ' 3',
  9276. ');',
  9277. '']));
  9278. end;
  9279. procedure TTestModule.TestArray_OpenArrayOfString;
  9280. begin
  9281. StartProgram(false);
  9282. Add('procedure DoIt(const a: array of String);');
  9283. Add('var');
  9284. Add(' i: longint;');
  9285. Add(' s: string;');
  9286. Add('begin');
  9287. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9288. Add('end;');
  9289. Add('var s: string;');
  9290. Add('begin');
  9291. Add(' DoIt([]);');
  9292. Add(' DoIt([s,''foo'','''',s+s]);');
  9293. ConvertProgram;
  9294. CheckSource('TestArray_OpenArrayOfString',
  9295. LinesToStr([ // statements
  9296. 'this.DoIt = function (a) {',
  9297. ' var i = 0;',
  9298. ' var s = "";',
  9299. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  9300. ' i = $l1;',
  9301. ' s = a[rtl.length(a) - i - 1];',
  9302. ' };',
  9303. '};',
  9304. 'this.s = "";',
  9305. '']),
  9306. LinesToStr([
  9307. '$mod.DoIt([]);',
  9308. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9309. '']));
  9310. end;
  9311. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9312. begin
  9313. StartProgram(false);
  9314. Add([
  9315. 'type TArr = array of char;',
  9316. 'var',
  9317. ' c: char;',
  9318. ' s: string;',
  9319. ' a: TArr;',
  9320. 'procedure Run(const a: array of char);',
  9321. 'begin',
  9322. ' Run(c);',
  9323. ' Run(s);',
  9324. 'end;',
  9325. 'begin',
  9326. ' a:=c;',
  9327. ' a:=s;',
  9328. ' a:=#13;',
  9329. ' a:=''Foo'';',
  9330. ' Run(c);',
  9331. ' Run(s);',
  9332. '']);
  9333. ConvertProgram;
  9334. CheckSource('TestArray_ArrayOfCharAssignString',
  9335. LinesToStr([ // statements
  9336. 'this.c = "";',
  9337. 'this.s = "";',
  9338. 'this.a = [];',
  9339. 'this.Run = function (a) {',
  9340. ' $mod.Run($mod.c.split(""));',
  9341. ' $mod.Run($mod.s.split(""));',
  9342. '};',
  9343. '']),
  9344. LinesToStr([
  9345. '$mod.a = $mod.c.split("");',
  9346. '$mod.a = $mod.s.split("");',
  9347. '$mod.a = "\r".split("");',
  9348. '$mod.a = "Foo".split("");',
  9349. '$mod.Run($mod.c.split(""));',
  9350. '$mod.Run($mod.s.split(""));',
  9351. '']));
  9352. end;
  9353. procedure TTestModule.TestArray_ConstRef;
  9354. begin
  9355. StartProgram(false);
  9356. Add([
  9357. 'type TArr = array of word;',
  9358. 'procedure Run(constref a: TArr);',
  9359. 'begin',
  9360. 'end;',
  9361. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9362. 'var l: TArr;',
  9363. 'begin',
  9364. ' Run(l);',
  9365. ' Run(a);',
  9366. ' Run(b);',
  9367. ' Run(c);',
  9368. ' Run(d);',
  9369. ' Run(e);',
  9370. 'end;',
  9371. 'begin',
  9372. '']);
  9373. ConvertProgram;
  9374. CheckResolverUnexpectedHints();
  9375. CheckSource('TestArray_ConstRef',
  9376. LinesToStr([ // statements
  9377. 'this.Run = function (a) {',
  9378. '};',
  9379. 'this.Fly = function (a, b, c, d, e) {',
  9380. ' var l = [];',
  9381. ' $mod.Run(l);',
  9382. ' $mod.Run(a);',
  9383. ' $mod.Run(b.get());',
  9384. ' $mod.Run(c.get());',
  9385. ' $mod.Run(d);',
  9386. ' $mod.Run(e);',
  9387. '};',
  9388. '']),
  9389. LinesToStr([
  9390. '']));
  9391. end;
  9392. procedure TTestModule.TestArray_Concat;
  9393. begin
  9394. StartProgram(false);
  9395. Add([
  9396. 'type',
  9397. ' integer = longint;',
  9398. ' TFlag = (big,small);',
  9399. ' TFlags = set of TFlag;',
  9400. ' TRec = record',
  9401. ' i: integer;',
  9402. ' end;',
  9403. ' TArrInt = array of integer;',
  9404. ' TArrRec = array of TRec;',
  9405. ' TArrFlag = array of TFlag;',
  9406. ' TArrSet = array of TFlags;',
  9407. ' TArrJSValue = array of jsvalue;',
  9408. 'var',
  9409. ' ArrInt: tarrint;',
  9410. ' ArrRec: tarrrec;',
  9411. ' ArrFlag: tarrflag;',
  9412. ' ArrSet: tarrset;',
  9413. ' ArrJSValue: tarrjsvalue;',
  9414. 'begin',
  9415. ' arrint:=concat(arrint);',
  9416. ' arrint:=concat(arrint,arrint);',
  9417. ' arrint:=concat(arrint,arrint,arrint);',
  9418. ' arrrec:=concat(arrrec);',
  9419. ' arrrec:=concat(arrrec,arrrec);',
  9420. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9421. ' arrset:=concat(arrset);',
  9422. ' arrset:=concat(arrset,arrset);',
  9423. ' arrset:=concat(arrset,arrset,arrset);',
  9424. ' arrjsvalue:=concat(arrjsvalue);',
  9425. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9426. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9427. ' arrint:=concat([1],arrint);',
  9428. ' arrflag:=concat([big]);',
  9429. ' arrflag:=concat([big],arrflag);',
  9430. ' arrflag:=concat(arrflag,[small]);',
  9431. '']);
  9432. ConvertProgram;
  9433. CheckSource('TestArray_Concat',
  9434. LinesToStr([ // statements
  9435. 'this.TFlag = {',
  9436. ' "0": "big",',
  9437. ' big: 0,',
  9438. ' "1": "small",',
  9439. ' small: 1',
  9440. '};',
  9441. 'rtl.recNewT($mod, "TRec", function () {',
  9442. ' this.i = 0;',
  9443. ' this.$eq = function (b) {',
  9444. ' return this.i === b.i;',
  9445. ' };',
  9446. ' this.$assign = function (s) {',
  9447. ' this.i = s.i;',
  9448. ' return this;',
  9449. ' };',
  9450. '});',
  9451. 'this.ArrInt = [];',
  9452. 'this.ArrRec = [];',
  9453. 'this.ArrFlag = [];',
  9454. 'this.ArrSet = [];',
  9455. 'this.ArrJSValue = [];',
  9456. '']),
  9457. LinesToStr([ // $mod.$main
  9458. '$mod.ArrInt = $mod.ArrInt;',
  9459. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9460. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9461. '$mod.ArrRec = $mod.ArrRec;',
  9462. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9463. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9464. '$mod.ArrSet = $mod.ArrSet;',
  9465. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9466. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9467. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9468. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9469. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9470. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9471. '$mod.ArrFlag = [$mod.TFlag.big];',
  9472. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9473. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9474. '']));
  9475. end;
  9476. procedure TTestModule.TestArray_Copy;
  9477. begin
  9478. StartProgram(false);
  9479. Add([
  9480. 'type',
  9481. ' integer = longint;',
  9482. ' TFlag = (big,small);',
  9483. ' TFlags = set of TFlag;',
  9484. ' TRec = record',
  9485. ' i: integer;',
  9486. ' end;',
  9487. ' TArrInt = array of integer;',
  9488. ' TArrRec = array of TRec;',
  9489. ' TArrSet = array of TFlags;',
  9490. ' TArrJSValue = array of jsvalue;',
  9491. 'var',
  9492. ' ArrInt: tarrint;',
  9493. ' ArrRec: tarrrec;',
  9494. ' ArrSet: tarrset;',
  9495. ' ArrJSValue: tarrjsvalue;',
  9496. 'begin',
  9497. ' arrint:=copy(arrint);',
  9498. ' arrint:=copy(arrint,2);',
  9499. ' arrint:=copy(arrint,3,4);',
  9500. ' arrint:=copy([1,1],1,2);',
  9501. ' arrrec:=copy(arrrec);',
  9502. ' arrrec:=copy(arrrec,5);',
  9503. ' arrrec:=copy(arrrec,6,7);',
  9504. ' arrset:=copy(arrset);',
  9505. ' arrset:=copy(arrset,8);',
  9506. ' arrset:=copy(arrset,9,10);',
  9507. ' arrjsvalue:=copy(arrjsvalue);',
  9508. ' arrjsvalue:=copy(arrjsvalue,11);',
  9509. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9510. ' ']);
  9511. ConvertProgram;
  9512. CheckSource('TestArray_Copy',
  9513. LinesToStr([ // statements
  9514. 'this.TFlag = {',
  9515. ' "0": "big",',
  9516. ' big: 0,',
  9517. ' "1": "small",',
  9518. ' small: 1',
  9519. '};',
  9520. 'rtl.recNewT($mod, "TRec", function () {',
  9521. ' this.i = 0;',
  9522. ' this.$eq = function (b) {',
  9523. ' return this.i === b.i;',
  9524. ' };',
  9525. ' this.$assign = function (s) {',
  9526. ' this.i = s.i;',
  9527. ' return this;',
  9528. ' };',
  9529. '});',
  9530. 'this.ArrInt = [];',
  9531. 'this.ArrRec = [];',
  9532. 'this.ArrSet = [];',
  9533. 'this.ArrJSValue = [];',
  9534. '']),
  9535. LinesToStr([ // $mod.$main
  9536. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9537. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9538. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9539. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9540. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9541. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9542. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9543. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9544. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9545. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9546. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9547. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9548. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9549. '']));
  9550. end;
  9551. procedure TTestModule.TestArray_InsertDelete;
  9552. begin
  9553. StartProgram(false);
  9554. Add([
  9555. 'type',
  9556. ' integer = longint;',
  9557. ' TFlag = (big,small);',
  9558. ' TFlags = set of TFlag;',
  9559. ' TRec = record',
  9560. ' i: integer;',
  9561. ' end;',
  9562. ' TArrInt = array of integer;',
  9563. ' TArrRec = array of TRec;',
  9564. ' TArrSet = array of TFlags;',
  9565. ' TArrJSValue = array of jsvalue;',
  9566. ' TArrArrInt = array of TArrInt;',
  9567. 'var',
  9568. ' ArrInt: tarrint;',
  9569. ' ArrRec: tarrrec;',
  9570. ' ArrSet: tarrset;',
  9571. ' ArrJSValue: tarrjsvalue;',
  9572. ' ArrArrInt: TArrArrInt;',
  9573. 'begin',
  9574. ' Insert(1,arrint,2);',
  9575. ' Insert(arrint[3],arrint,4);',
  9576. ' Insert(arrrec[5],arrrec,6);',
  9577. ' Insert(arrset[7],arrset,7);',
  9578. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9579. ' Insert(10,arrjsvalue,11);',
  9580. ' Insert([23],arrarrint,22);',
  9581. ' Delete(arrint,12,13);',
  9582. ' Delete(arrrec,14,15);',
  9583. ' Delete(arrset,17,18);',
  9584. ' Delete(arrjsvalue,19,10);']);
  9585. ConvertProgram;
  9586. CheckSource('TestArray_InsertDelete',
  9587. LinesToStr([ // statements
  9588. 'this.TFlag = {',
  9589. ' "0": "big",',
  9590. ' big: 0,',
  9591. ' "1": "small",',
  9592. ' small: 1',
  9593. '};',
  9594. 'rtl.recNewT($mod, "TRec", function () {',
  9595. ' this.i = 0;',
  9596. ' this.$eq = function (b) {',
  9597. ' return this.i === b.i;',
  9598. ' };',
  9599. ' this.$assign = function (s) {',
  9600. ' this.i = s.i;',
  9601. ' return this;',
  9602. ' };',
  9603. '});',
  9604. 'this.ArrInt = [];',
  9605. 'this.ArrRec = [];',
  9606. 'this.ArrSet = [];',
  9607. 'this.ArrJSValue = [];',
  9608. 'this.ArrArrInt = [];',
  9609. '']),
  9610. LinesToStr([ // $mod.$main
  9611. '$mod.ArrInt.splice(2, 0, 1);',
  9612. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9613. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9614. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9615. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9616. '$mod.ArrJSValue.splice(11, 0, 10);',
  9617. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9618. '$mod.ArrInt.splice(12, 13);',
  9619. '$mod.ArrRec.splice(14, 15);',
  9620. '$mod.ArrSet.splice(17, 18);',
  9621. '$mod.ArrJSValue.splice(19, 10);',
  9622. '']));
  9623. end;
  9624. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9625. begin
  9626. StartProgram(false);
  9627. Add([
  9628. '{$modeswitch arrayoperators}',
  9629. 'type',
  9630. ' integer = longint;',
  9631. ' TArrInt = array of integer;',
  9632. ' TArrStr = array of string;',
  9633. 'const',
  9634. ' Ints: TArrInt = (1,2,3);',
  9635. ' Aliases: TarrStr = (''foo'',''b'');',
  9636. ' OneInt: TArrInt = (7);',
  9637. ' OneStr: array of integer = (7);',
  9638. ' Chars: array of char = ''aoc'';',
  9639. ' Names: array of string = (''a'',''foo'');',
  9640. ' NameCount = low(Names)+high(Names)+length(Names);',
  9641. 'var i: integer;',
  9642. 'begin',
  9643. ' Ints:=[];',
  9644. ' Ints:=[1,1];',
  9645. ' Ints:=[1]+[2];',
  9646. ' Ints:=[2];',
  9647. ' Ints:=[]+ints;',
  9648. ' Ints:=Ints+[];',
  9649. ' Ints:=Ints+OneInt;',
  9650. ' Ints:=Ints+[1,1];',
  9651. ' Ints:=[i,i]+Ints;',
  9652. ' Ints:=[1]+[i]+[3];',
  9653. '']);
  9654. ConvertProgram;
  9655. CheckSource('TestArray_DynArrayConstObjFPC',
  9656. LinesToStr([ // statements
  9657. 'this.Ints = [1, 2, 3];',
  9658. 'this.Aliases = ["foo", "b"];',
  9659. 'this.OneInt = [7];',
  9660. 'this.OneStr = [7];',
  9661. 'this.Chars = ["a", "o", "c"];',
  9662. 'this.Names = ["a", "foo"];',
  9663. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9664. 'this.i = 0;',
  9665. '']),
  9666. LinesToStr([ // $mod.$main
  9667. '$mod.Ints = [];',
  9668. '$mod.Ints = [1, 1];',
  9669. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9670. '$mod.Ints = [2];',
  9671. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9672. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9673. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9674. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9675. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9676. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9677. '']));
  9678. end;
  9679. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9680. begin
  9681. StartProgram(false);
  9682. // Note: const c = [1,1]; defines a set!
  9683. Add([
  9684. '{$mode delphi}',
  9685. 'type',
  9686. ' integer = longint;',
  9687. ' TArrInt = array of integer;',
  9688. ' TArrStr = array of string;',
  9689. 'const',
  9690. ' Ints: TArrInt = [1,1,2];',
  9691. ' Aliases: TarrStr = [''foo'',''b''];',
  9692. ' OneInt: TArrInt = [7];',
  9693. ' OneStr: array of integer = [7]+[8];',
  9694. ' Chars: array of char = ''aoc'';',
  9695. ' Names: array of string = [''a'',''a''];',
  9696. ' NameCount = low(Names)+high(Names)+length(Names);',
  9697. 'begin',
  9698. '']);
  9699. ConvertProgram;
  9700. CheckSource('TestArray_DynArrayConstDelphi',
  9701. LinesToStr([ // statements
  9702. 'this.Ints = [1, 1, 2];',
  9703. 'this.Aliases = ["foo", "b"];',
  9704. 'this.OneInt = [7];',
  9705. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9706. 'this.Chars = ["a", "o", "c"];',
  9707. 'this.Names = ["a", "a"];',
  9708. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9709. '']),
  9710. LinesToStr([ // $mod.$main
  9711. '']));
  9712. end;
  9713. procedure TTestModule.TestArray_ArrayLitAsParam;
  9714. begin
  9715. StartProgram(false);
  9716. Add([
  9717. '{$modeswitch arrayoperators}',
  9718. 'type',
  9719. ' integer = longint;',
  9720. ' TArrInt = array of integer;',
  9721. ' TArrSet = array of (red,green,blue);',
  9722. 'procedure DoOpenInt(const a: array of integer); forward;',
  9723. 'procedure DoInt(const a: TArrInt);',
  9724. 'begin',
  9725. ' DoInt(a+[1]);',
  9726. ' DoInt([1]+a);',
  9727. ' DoOpenInt(a);',
  9728. ' DoOpenInt(a+[1]);',
  9729. ' DoOpenInt([1]+a);',
  9730. 'end;',
  9731. 'procedure DoOpenInt(const a: array of integer);',
  9732. 'begin',
  9733. ' DoOpenInt(a+[1]);',
  9734. ' DoOpenInt([1]+a);',
  9735. ' DoInt(a);',
  9736. ' DoInt(a+[1]);',
  9737. ' DoInt([1]+a);',
  9738. 'end;',
  9739. 'procedure DoSet(const a: TArrSet);',
  9740. 'begin',
  9741. ' DoSet(a+[red]);',
  9742. ' DoSet([blue]+a);',
  9743. 'end;',
  9744. 'var',
  9745. ' i: TArrInt;',
  9746. ' s: TArrSet;',
  9747. 'begin',
  9748. ' DoInt([1]);',
  9749. ' DoInt([1]+[2]);',
  9750. ' DoInt(i+[1]);',
  9751. ' DoInt([1]+i);',
  9752. ' DoOpenInt([1]);',
  9753. ' DoOpenInt([1]+[2]);',
  9754. ' DoOpenInt(i+[1]);',
  9755. ' DoOpenInt([1]+i);',
  9756. ' DoSet([red]);',
  9757. ' DoSet([blue]+[green]);',
  9758. ' DoSet(s+[blue]);',
  9759. ' DoSet([red]+s);',
  9760. '']);
  9761. ConvertProgram;
  9762. CheckSource('TestArray_ArrayLitAsParam',
  9763. LinesToStr([ // statements
  9764. 'this.TArrSet$a = {',
  9765. ' "0": "red",',
  9766. ' red: 0,',
  9767. ' "1": "green",',
  9768. ' green: 1,',
  9769. ' "2": "blue",',
  9770. ' blue: 2',
  9771. '};',
  9772. 'this.DoInt = function (a) {',
  9773. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9774. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9775. ' $mod.DoOpenInt(a);',
  9776. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9777. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9778. '};',
  9779. 'this.DoOpenInt = function (a) {',
  9780. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9781. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9782. ' $mod.DoInt(a);',
  9783. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9784. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9785. '};',
  9786. 'this.DoSet = function (a) {',
  9787. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9788. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9789. '};',
  9790. 'this.i = [];',
  9791. 'this.s = [];',
  9792. '']),
  9793. LinesToStr([ // $mod.$main
  9794. '$mod.DoInt([1]);',
  9795. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9796. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9797. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9798. '$mod.DoOpenInt([1]);',
  9799. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9800. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9801. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9802. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9803. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9804. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9805. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9806. '']));
  9807. end;
  9808. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9809. begin
  9810. StartProgram(false);
  9811. Add([
  9812. '{$modeswitch arrayoperators}',
  9813. 'type',
  9814. ' integer = longint;',
  9815. ' TArrInt = array of integer;',
  9816. ' TArrArrInt = array of TArrInt;',
  9817. 'procedure DoInt(const a: TArrArrInt);',
  9818. 'begin',
  9819. ' DoInt(a+[[1]]);',
  9820. ' DoInt([[1]]+a);',
  9821. ' DoInt(a);',
  9822. 'end;',
  9823. 'var',
  9824. ' i: TArrInt;',
  9825. ' a: TArrArrInt;',
  9826. 'begin',
  9827. ' a:=[[1]];',
  9828. ' a:=[i];',
  9829. ' a:=a+[i];',
  9830. ' a:=[i]+a;',
  9831. ' a:=[[1]+i];',
  9832. ' a:=[[1]+[2]];',
  9833. ' a:=[i+[2]];',
  9834. ' DoInt([[1]]);',
  9835. ' DoInt([[1]+[2],[3,4],[5]]);',
  9836. ' DoInt([i+[1]]+a);',
  9837. ' DoInt([i]+a);',
  9838. '']);
  9839. ConvertProgram;
  9840. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9841. LinesToStr([ // statements
  9842. 'this.DoInt = function (a) {',
  9843. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9844. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9845. ' $mod.DoInt(a);',
  9846. '};',
  9847. 'this.i = [];',
  9848. 'this.a = [];',
  9849. '']),
  9850. LinesToStr([ // $mod.$main
  9851. '$mod.a = [[1]];',
  9852. '$mod.a = [$mod.i];',
  9853. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9854. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9855. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9856. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9857. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9858. '$mod.DoInt([[1]]);',
  9859. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9860. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9861. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9862. '']));
  9863. end;
  9864. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9865. begin
  9866. StartProgram(false);
  9867. Add([
  9868. '{$modeswitch arrayoperators}',
  9869. 'type',
  9870. ' integer = longint;',
  9871. ' TArrInt = array[1..2] of integer;',
  9872. ' TArrArrInt = array of TArrInt;',
  9873. 'procedure DoInt(const a: TArrArrInt);',
  9874. 'begin',
  9875. ' DoInt(a+[[1,2]]);',
  9876. ' DoInt([[1,2]]+a);',
  9877. ' DoInt(a);',
  9878. 'end;',
  9879. 'var',
  9880. ' i: TArrInt;',
  9881. ' a: TArrArrInt;',
  9882. 'begin',
  9883. ' a:=[[1,1]];',
  9884. ' a:=[i];',
  9885. ' a:=a+[i];',
  9886. ' a:=[i]+a;',
  9887. ' DoInt([[1,1]]);',
  9888. ' DoInt([[1,2],[3,4]]);',
  9889. '']);
  9890. ConvertProgram;
  9891. CheckSource('TestArray_ArrayLitStaticAsParam',
  9892. LinesToStr([ // statements
  9893. 'this.DoInt = function (a) {',
  9894. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9895. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9896. ' $mod.DoInt(a);',
  9897. '};',
  9898. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9899. 'this.a = [];',
  9900. '']),
  9901. LinesToStr([ // $mod.$main
  9902. '$mod.a = [[1, 1]];',
  9903. '$mod.a = [$mod.i.slice(0)];',
  9904. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9905. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9906. '$mod.DoInt([[1, 1]]);',
  9907. '$mod.DoInt([[1, 2], [3, 4]]);',
  9908. '']));
  9909. end;
  9910. procedure TTestModule.TestArray_ForInArrOfString;
  9911. begin
  9912. StartProgram(false);
  9913. Add([
  9914. 'type',
  9915. 'type',
  9916. ' TMonthNameArray = array [1..12] of string;',
  9917. ' TMonthNames = TMonthNameArray;',
  9918. ' TObject = class',
  9919. ' private',
  9920. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9921. ' public',
  9922. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9923. ' end;',
  9924. 'var',
  9925. ' f: TObject;',
  9926. ' Month: string;',
  9927. ' Names: array of string = (''a'',''foo'',''bar'');',
  9928. ' i: longint;',
  9929. 'begin',
  9930. ' for Month in f.LongMonthNames do ;',
  9931. ' for Month in Names do ;',
  9932. ' for i:=low(Names) to high(Names) do ;',
  9933. '']);
  9934. ConvertProgram;
  9935. CheckSource('TestArray_ForInArrOfString',
  9936. LinesToStr([ // statements
  9937. 'rtl.createClass($mod, "TObject", null, function () {',
  9938. ' this.$init = function () {',
  9939. ' };',
  9940. ' this.$final = function () {',
  9941. ' };',
  9942. '});',
  9943. 'this.f = null;',
  9944. 'this.Month = "";',
  9945. 'this.Names = ["a", "foo", "bar"];',
  9946. 'this.i = 0;',
  9947. '']),
  9948. LinesToStr([ // $mod.$main
  9949. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9950. 'for (var $in4 = $mod.Names, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.Month = $in4[$l5];',
  9951. 'for (var $l7 = 0, $end8 = rtl.length($mod.Names) - 1; $l7 <= $end8; $l7++) $mod.i = $l7;',
  9952. '']));
  9953. end;
  9954. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9955. begin
  9956. StartProgram(false);
  9957. Add([
  9958. '{$modeswitch externalclass}',
  9959. 'type',
  9960. ' TJSObject = class external name ''Object''',
  9961. ' end;',
  9962. ' TJSArray = class external name ''Array''',
  9963. ' class function isArray(Value: JSValue) : boolean;',
  9964. ' function concat() : TJSArray; varargs;',
  9965. ' end;',
  9966. 'var',
  9967. ' aObj: TJSArray;',
  9968. ' a: array of longint;',
  9969. ' o: TJSObject;',
  9970. 'begin',
  9971. ' if TJSArray.isArray(65) then ;',
  9972. ' aObj:=TJSArray(a).concat(a);',
  9973. ' o:=TJSObject(a);']);
  9974. ConvertProgram;
  9975. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9976. LinesToStr([ // statements
  9977. 'this.aObj = null;',
  9978. 'this.a = [];',
  9979. 'this.o = null;',
  9980. '']),
  9981. LinesToStr([ // $mod.$main
  9982. 'if (Array.isArray(65)) ;',
  9983. '$mod.aObj = $mod.a.concat($mod.a);',
  9984. '$mod.o = $mod.a;',
  9985. '']));
  9986. end;
  9987. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9988. begin
  9989. StartProgram(false);
  9990. Add([
  9991. '{$modeswitch externalclass}',
  9992. 'type',
  9993. ' TArrStr = array of string;',
  9994. ' TJSArray = class external name ''Array''',
  9995. ' end;',
  9996. ' TJSObject = class external name ''Object''',
  9997. ' end;',
  9998. 'var',
  9999. ' aObj: TJSArray;',
  10000. ' a: TArrStr;',
  10001. ' jo: TJSObject;',
  10002. 'begin',
  10003. ' a:=TArrStr(aObj);',
  10004. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10005. ' a:=TarrStr(jo);',
  10006. '']);
  10007. ConvertProgram;
  10008. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10009. LinesToStr([ // statements
  10010. 'this.aObj = null;',
  10011. 'this.a = [];',
  10012. 'this.jo = null;',
  10013. '']),
  10014. LinesToStr([ // $mod.$main
  10015. '$mod.a = $mod.aObj;',
  10016. '$mod.aObj[1] = $mod.aObj[2];',
  10017. '$mod.a = $mod.jo;',
  10018. '']));
  10019. end;
  10020. procedure TTestModule.TestArrayOfConst_TVarRec;
  10021. begin
  10022. StartProgram(true,[supTVarRec]);
  10023. Add([
  10024. 'procedure Say(args: array of const);',
  10025. 'var',
  10026. ' i: longint;',
  10027. ' v: TVarRec;',
  10028. 'begin',
  10029. ' for i:=low(args) to high(args) do begin',
  10030. ' v:=args[i];',
  10031. ' case v.vtype of',
  10032. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10033. ' end;',
  10034. ' end;',
  10035. ' for v in args do ;',
  10036. ' args:=nil;',
  10037. ' SetLength(args,2);',
  10038. 'end;',
  10039. 'begin']);
  10040. ConvertProgram;
  10041. CheckSource('TestArrayOfConst_TVarRec',
  10042. LinesToStr([ // statements
  10043. 'this.Say = function (args) {',
  10044. ' var i = 0;',
  10045. ' var v = pas.system.TVarRec.$new();',
  10046. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  10047. ' i = $l1;',
  10048. ' v.$assign(args[i]);',
  10049. ' var $tmp3 = v.VType;',
  10050. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10051. ' };',
  10052. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  10053. ' args = [];',
  10054. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10055. '};',
  10056. '']),
  10057. LinesToStr([ // $mod.$main
  10058. ]));
  10059. end;
  10060. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10061. begin
  10062. StartProgram(true,[supTVarRec]);
  10063. Add([
  10064. 'procedure Say(args: array of const);',
  10065. 'begin',
  10066. ' Say(args);',
  10067. 'end;',
  10068. 'var',
  10069. ' p: Pointer;',
  10070. ' j: jsvalue;',
  10071. ' c: currency;',
  10072. 'begin',
  10073. ' Say([]);',
  10074. ' Say([1]);',
  10075. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10076. '']);
  10077. ConvertProgram;
  10078. CheckSource('TestArrayOfConst_PassBaseTypes',
  10079. LinesToStr([ // statements
  10080. 'this.Say = function (args) {',
  10081. ' $mod.Say(args);',
  10082. '};',
  10083. 'this.p = null;',
  10084. 'this.j = undefined;',
  10085. 'this.c = 0;',
  10086. '']),
  10087. LinesToStr([ // $mod.$main
  10088. '$mod.Say([]);',
  10089. '$mod.Say(pas.system.VarRecs(0, 1));',
  10090. '$mod.Say(pas.system.VarRecs(',
  10091. ' 9,',
  10092. ' "c",',
  10093. ' 18,',
  10094. ' "foo",',
  10095. ' 5,',
  10096. ' null,',
  10097. ' 1,',
  10098. ' true,',
  10099. ' 3,',
  10100. ' 1.3,',
  10101. ' 5,',
  10102. ' $mod.p,',
  10103. ' 20,',
  10104. ' $mod.j,',
  10105. ' 12,',
  10106. ' $mod.c',
  10107. ' ));',
  10108. '']));
  10109. end;
  10110. procedure TTestModule.TestArrayOfConst_PassObj;
  10111. begin
  10112. StartProgram(true,[supTVarRec]);
  10113. Add([
  10114. '{$interfaces corba}',
  10115. 'type',
  10116. ' TObject = class',
  10117. ' end;',
  10118. ' TClass = class of TObject;',
  10119. ' IUnknown = interface',
  10120. ' end;',
  10121. 'procedure Say(args: array of const);',
  10122. 'begin',
  10123. 'end;',
  10124. 'var',
  10125. ' o: TObject;',
  10126. ' c: TClass;',
  10127. ' i: IUnknown;',
  10128. 'begin',
  10129. ' Say([o,c,TObject]);',
  10130. ' Say([nil,i]);',
  10131. '']);
  10132. ConvertProgram;
  10133. CheckSource('TestArrayOfConst_PassObj',
  10134. LinesToStr([ // statements
  10135. 'rtl.createClass($mod, "TObject", null, function () {',
  10136. ' this.$init = function () {',
  10137. ' };',
  10138. ' this.$final = function () {',
  10139. ' };',
  10140. '});',
  10141. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10142. 'this.Say = function (args) {',
  10143. '};',
  10144. 'this.o = null;',
  10145. 'this.c = null;',
  10146. 'this.i = null;',
  10147. '']),
  10148. LinesToStr([ // $mod.$main
  10149. '$mod.Say(pas.system.VarRecs(',
  10150. ' 7,',
  10151. ' $mod.o,',
  10152. ' 8,',
  10153. ' $mod.c,',
  10154. ' 8,',
  10155. ' $mod.TObject',
  10156. '));',
  10157. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10158. '']));
  10159. end;
  10160. procedure TTestModule.TestRecord_Empty;
  10161. begin
  10162. StartProgram(false);
  10163. Add([
  10164. 'type',
  10165. ' TRecA = record',
  10166. ' end;',
  10167. 'var a,b: TRecA;',
  10168. 'begin',
  10169. ' if a=b then ;']);
  10170. ConvertProgram;
  10171. CheckSource('TestRecord_Empty',
  10172. LinesToStr([ // statements
  10173. 'rtl.recNewT($mod, "TRecA", function () {',
  10174. ' this.$eq = function (b) {',
  10175. ' return true;',
  10176. ' };',
  10177. ' this.$assign = function (s) {',
  10178. ' return this;',
  10179. ' };',
  10180. '});',
  10181. 'this.a = $mod.TRecA.$new();',
  10182. 'this.b = $mod.TRecA.$new();',
  10183. '']),
  10184. LinesToStr([ // $mod.$main
  10185. 'if ($mod.a.$eq($mod.b)) ;'
  10186. ]));
  10187. end;
  10188. procedure TTestModule.TestRecord_Var;
  10189. begin
  10190. StartProgram(false);
  10191. Add('type');
  10192. Add(' TRecA = record');
  10193. Add(' Bold: longint;');
  10194. Add(' end;');
  10195. Add('var Rec: TRecA;');
  10196. Add('begin');
  10197. Add(' rec.bold:=123');
  10198. ConvertProgram;
  10199. CheckSource('TestRecord_Var',
  10200. LinesToStr([ // statements
  10201. 'rtl.recNewT($mod, "TRecA", function () {',
  10202. ' this.Bold = 0;',
  10203. ' this.$eq = function (b) {',
  10204. ' return this.Bold === b.Bold;',
  10205. ' };',
  10206. ' this.$assign = function (s) {',
  10207. ' this.Bold = s.Bold;',
  10208. ' return this;',
  10209. ' };',
  10210. '});',
  10211. 'this.Rec = $mod.TRecA.$new();',
  10212. '']),
  10213. LinesToStr([ // $mod.$main
  10214. '$mod.Rec.Bold = 123;'
  10215. ]));
  10216. end;
  10217. procedure TTestModule.TestRecord_VarExternal;
  10218. begin
  10219. StartProgram(false);
  10220. Add([
  10221. '{$modeswitch externalclass}',
  10222. 'type',
  10223. ' TRecA = record',
  10224. ' i: byte;',
  10225. ' length_: longint external name ''length'';',
  10226. ' end;',
  10227. 'var Rec: TRecA;',
  10228. 'begin',
  10229. ' rec.length_ := rec.length_',
  10230. '']);
  10231. ConvertProgram;
  10232. CheckSource('TestRecord_VarExternal',
  10233. LinesToStr([ // statements
  10234. 'rtl.recNewT($mod, "TRecA", function () {',
  10235. ' this.i = 0;',
  10236. ' this.$eq = function (b) {',
  10237. ' return (this.i === b.i) && (this.length === b.length);',
  10238. ' };',
  10239. ' this.$assign = function (s) {',
  10240. ' this.i = s.i;',
  10241. ' this.length = s.length;',
  10242. ' return this;',
  10243. ' };',
  10244. '});',
  10245. 'this.Rec = $mod.TRecA.$new();',
  10246. '']),
  10247. LinesToStr([ // $mod.$main
  10248. '$mod.Rec.length = $mod.Rec.length;'
  10249. ]));
  10250. end;
  10251. procedure TTestModule.TestRecord_WithDo;
  10252. begin
  10253. StartProgram(false);
  10254. Add('type');
  10255. Add(' TRec = record');
  10256. Add(' vI: longint;');
  10257. Add(' end;');
  10258. Add('var');
  10259. Add(' Int: longint;');
  10260. Add(' r: TRec;');
  10261. Add('begin');
  10262. Add(' with r do');
  10263. Add(' int:=vi;');
  10264. Add(' with r do begin');
  10265. Add(' int:=vi;');
  10266. Add(' vi:=int;');
  10267. Add(' end;');
  10268. ConvertProgram;
  10269. CheckSource('TestWithRecordDo',
  10270. LinesToStr([ // statements
  10271. 'rtl.recNewT($mod, "TRec", function () {',
  10272. ' this.vI = 0;',
  10273. ' this.$eq = function (b) {',
  10274. ' return this.vI === b.vI;',
  10275. ' };',
  10276. ' this.$assign = function (s) {',
  10277. ' this.vI = s.vI;',
  10278. ' return this;',
  10279. ' };',
  10280. '});',
  10281. 'this.Int = 0;',
  10282. 'this.r = $mod.TRec.$new();',
  10283. '']),
  10284. LinesToStr([ // $mod.$main
  10285. 'var $with1 = $mod.r;',
  10286. '$mod.Int = $with1.vI;',
  10287. 'var $with2 = $mod.r;',
  10288. '$mod.Int = $with2.vI;',
  10289. '$with2.vI = $mod.Int;'
  10290. ]));
  10291. end;
  10292. procedure TTestModule.TestRecord_Assign;
  10293. begin
  10294. StartProgram(false);
  10295. Add('type');
  10296. Add(' TEnum = (red,green);');
  10297. Add(' TEnums = set of TEnum;');
  10298. Add(' TSmallRec = record');
  10299. Add(' N: longint;');
  10300. Add(' end;');
  10301. Add(' TBigRec = record');
  10302. Add(' Int: longint;');
  10303. Add(' D: double;');
  10304. Add(' Arr: array of longint;');
  10305. Add(' Arr2: array[1..2] of longint;');
  10306. Add(' Small: TSmallRec;');
  10307. Add(' Enums: TEnums;');
  10308. Add(' end;');
  10309. Add('var');
  10310. Add(' r, s: TBigRec;');
  10311. Add('begin');
  10312. Add(' r:=s;');
  10313. Add(' r:=default(TBigRec);');
  10314. Add(' r:=default(s);');
  10315. ConvertProgram;
  10316. CheckSource('TestRecord_Assign',
  10317. LinesToStr([ // statements
  10318. 'this.TEnum = {',
  10319. ' "0": "red",',
  10320. ' red: 0,',
  10321. ' "1": "green",',
  10322. ' green: 1',
  10323. '};',
  10324. 'rtl.recNewT($mod, "TSmallRec", function () {',
  10325. ' this.N = 0;',
  10326. ' this.$eq = function (b) {',
  10327. ' return this.N === b.N;',
  10328. ' };',
  10329. ' this.$assign = function (s) {',
  10330. ' this.N = s.N;',
  10331. ' return this;',
  10332. ' };',
  10333. '});',
  10334. 'rtl.recNewT($mod, "TBigRec", function () {',
  10335. ' this.Int = 0;',
  10336. ' this.D = 0.0;',
  10337. ' this.Arr = [];',
  10338. ' this.$new = function () {',
  10339. ' var r = Object.create(this);',
  10340. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10341. ' r.Small = $mod.TSmallRec.$new();',
  10342. ' r.Enums = {};',
  10343. ' return r;',
  10344. ' };',
  10345. ' this.$eq = function (b) {',
  10346. ' 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);',
  10347. ' };',
  10348. ' this.$assign = function (s) {',
  10349. ' this.Int = s.Int;',
  10350. ' this.D = s.D;',
  10351. ' this.Arr = s.Arr;',
  10352. ' this.Arr2 = s.Arr2.slice(0);',
  10353. ' this.Small.$assign(s.Small);',
  10354. ' this.Enums = rtl.refSet(s.Enums);',
  10355. ' return this;',
  10356. ' };',
  10357. '});',
  10358. 'this.r = $mod.TBigRec.$new();',
  10359. 'this.s = $mod.TBigRec.$new();',
  10360. '']),
  10361. LinesToStr([ // $mod.$main
  10362. '$mod.r.$assign($mod.s);',
  10363. '$mod.r.$assign($mod.TBigRec.$new());',
  10364. '$mod.r.$assign($mod.TBigRec.$new());',
  10365. '']));
  10366. end;
  10367. procedure TTestModule.TestRecord_AsParams;
  10368. begin
  10369. StartProgram(false);
  10370. Add([
  10371. 'type',
  10372. ' integer = longint;',
  10373. ' TRecord = record',
  10374. ' i: integer;',
  10375. ' end;',
  10376. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10377. 'var vL: TRecord;',
  10378. 'begin',
  10379. ' vd:=vd;',
  10380. ' vd.i:=vd.i;',
  10381. ' vl:=vc;',
  10382. ' vv:=vv;',
  10383. ' vv.i:=vv.i;',
  10384. ' U:=vl;',
  10385. ' U:=vd;',
  10386. ' U:=vc;',
  10387. ' U:=vv;',
  10388. ' vl:=TRecord(U);',
  10389. ' vd:=TRecord(U);',
  10390. ' vv:=TRecord(U);',
  10391. ' doit(vd,vd,vd,vd);',
  10392. ' doit(vc,vc,vl,vl);',
  10393. ' doit(vv,vv,vv,vv);',
  10394. ' doit(vl,vl,vl,vl);',
  10395. ' TRecord(U).i:=3;',
  10396. 'end;',
  10397. 'var i: TRecord;',
  10398. 'begin',
  10399. ' doit(i,i,i,i);',
  10400. '']);
  10401. ConvertProgram;
  10402. CheckSource('TestRecord_AsParams',
  10403. LinesToStr([ // statements
  10404. 'rtl.recNewT($mod, "TRecord", function () {',
  10405. ' this.i = 0;',
  10406. ' this.$eq = function (b) {',
  10407. ' return this.i === b.i;',
  10408. ' };',
  10409. ' this.$assign = function (s) {',
  10410. ' this.i = s.i;',
  10411. ' return this;',
  10412. ' };',
  10413. '});',
  10414. 'this.DoIt = function (vD, vC, vV, U) {',
  10415. ' var vL = $mod.TRecord.$new();',
  10416. ' vD.$assign(vD);',
  10417. ' vD.i = vD.i;',
  10418. ' vL.$assign(vC);',
  10419. ' vV.$assign(vV);',
  10420. ' vV.i = vV.i;',
  10421. ' U.$assign(vL);',
  10422. ' U.$assign(vD);',
  10423. ' U.$assign(vC);',
  10424. ' U.$assign(vV);',
  10425. ' vL.$assign(U);',
  10426. ' vD.$assign(U);',
  10427. ' vV.$assign(U);',
  10428. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10429. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10430. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10431. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10432. ' U.i = 3;',
  10433. '};',
  10434. 'this.i = $mod.TRecord.$new();'
  10435. ]),
  10436. LinesToStr([
  10437. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10438. '']));
  10439. end;
  10440. procedure TTestModule.TestRecord_ConstRef;
  10441. begin
  10442. StartProgram(false);
  10443. Add([
  10444. 'type TRec = record i: word; end;',
  10445. 'procedure Run(constref a: TRec);',
  10446. 'begin',
  10447. 'end;',
  10448. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10449. 'var l: TRec;',
  10450. 'begin',
  10451. ' Run(l);',
  10452. ' Run(a);',
  10453. ' Run(b);',
  10454. ' Run(c);',
  10455. ' Run(d);',
  10456. ' Run(e);',
  10457. 'end;',
  10458. 'begin',
  10459. '']);
  10460. ConvertProgram;
  10461. CheckResolverUnexpectedHints();
  10462. CheckSource('TestRecord_ConstRef',
  10463. LinesToStr([ // statements
  10464. 'rtl.recNewT($mod, "TRec", function () {',
  10465. ' this.i = 0;',
  10466. ' this.$eq = function (b) {',
  10467. ' return this.i === b.i;',
  10468. ' };',
  10469. ' this.$assign = function (s) {',
  10470. ' this.i = s.i;',
  10471. ' return this;',
  10472. ' };',
  10473. '});',
  10474. 'this.Run = function (a) {',
  10475. '};',
  10476. 'this.Fly = function (a, b, c, d, e) {',
  10477. ' var l = $mod.TRec.$new();',
  10478. ' $mod.Run(l);',
  10479. ' $mod.Run(a);',
  10480. ' $mod.Run(b);',
  10481. ' $mod.Run(c);',
  10482. ' $mod.Run(d);',
  10483. ' $mod.Run(e);',
  10484. '};',
  10485. '']),
  10486. LinesToStr([
  10487. '']));
  10488. end;
  10489. procedure TTestModule.TestRecordElement_AsParams;
  10490. begin
  10491. StartProgram(false);
  10492. Add('type');
  10493. Add(' integer = longint;');
  10494. Add(' TRecord = record');
  10495. Add(' i: integer;');
  10496. Add(' end;');
  10497. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10498. Add('var vJ: TRecord;');
  10499. Add('begin');
  10500. Add(' doit(vj.i,vj.i,vj.i);');
  10501. Add('end;');
  10502. Add('var r: TRecord;');
  10503. Add('begin');
  10504. Add(' doit(r.i,r.i,r.i);');
  10505. ConvertProgram;
  10506. CheckSource('TestRecordElement_AsParams',
  10507. LinesToStr([ // statements
  10508. 'rtl.recNewT($mod, "TRecord", function () {',
  10509. ' this.i = 0;',
  10510. ' this.$eq = function (b) {',
  10511. ' return this.i === b.i;',
  10512. ' };',
  10513. ' this.$assign = function (s) {',
  10514. ' this.i = s.i;',
  10515. ' return this;',
  10516. ' };',
  10517. '});',
  10518. 'this.DoIt = function (vG,vH,vI) {',
  10519. ' var vJ = $mod.TRecord.$new();',
  10520. ' $mod.DoIt(vJ.i, vJ.i, {',
  10521. ' p: vJ,',
  10522. ' get: function () {',
  10523. ' return this.p.i;',
  10524. ' },',
  10525. ' set: function (v) {',
  10526. ' this.p.i = v;',
  10527. ' }',
  10528. ' });',
  10529. '};',
  10530. 'this.r = $mod.TRecord.$new();'
  10531. ]),
  10532. LinesToStr([
  10533. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10534. ' p: $mod.r,',
  10535. ' get: function () {',
  10536. ' return this.p.i;',
  10537. ' },',
  10538. ' set: function (v) {',
  10539. ' this.p.i = v;',
  10540. ' }',
  10541. '});'
  10542. ]));
  10543. end;
  10544. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10545. begin
  10546. StartProgram(false);
  10547. Add('type');
  10548. Add(' integer = longint;');
  10549. Add(' TRecord = record');
  10550. Add(' i: integer;');
  10551. Add(' end;');
  10552. Add('function GetRec(vB: integer = 0): TRecord;');
  10553. Add('begin');
  10554. Add('end;');
  10555. Add('procedure DoIt(vG: integer; const vH: integer);');
  10556. Add('begin');
  10557. Add('end;');
  10558. Add('begin');
  10559. Add(' doit(getrec.i,getrec.i);');
  10560. Add(' doit(getrec().i,getrec().i);');
  10561. Add(' doit(getrec(1).i,getrec(2).i);');
  10562. ConvertProgram;
  10563. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10564. LinesToStr([ // statements
  10565. 'rtl.recNewT($mod, "TRecord", function () {',
  10566. ' this.i = 0;',
  10567. ' this.$eq = function (b) {',
  10568. ' return this.i === b.i;',
  10569. ' };',
  10570. ' this.$assign = function (s) {',
  10571. ' this.i = s.i;',
  10572. ' return this;',
  10573. ' };',
  10574. '});',
  10575. 'this.GetRec = function (vB) {',
  10576. ' var Result = $mod.TRecord.$new();',
  10577. ' return Result;',
  10578. '};',
  10579. 'this.DoIt = function (vG, vH) {',
  10580. '};',
  10581. '']),
  10582. LinesToStr([
  10583. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10584. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10585. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10586. '']));
  10587. end;
  10588. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10589. begin
  10590. StartProgram(false);
  10591. Add('type');
  10592. Add(' integer = longint;');
  10593. Add(' TRecord = record');
  10594. Add(' i: integer;');
  10595. Add(' end;');
  10596. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10597. Add('begin');
  10598. Add('end;');
  10599. Add('var r: trecord;');
  10600. Add('begin');
  10601. Add(' with r do ');
  10602. Add(' doit(i,i,i);');
  10603. ConvertProgram;
  10604. CheckSource('TestRecordElementFromWith_AsParams',
  10605. LinesToStr([ // statements
  10606. 'rtl.recNewT($mod, "TRecord", function () {',
  10607. ' this.i = 0;',
  10608. ' this.$eq = function (b) {',
  10609. ' return this.i === b.i;',
  10610. ' };',
  10611. ' this.$assign = function (s) {',
  10612. ' this.i = s.i;',
  10613. ' return this;',
  10614. ' };',
  10615. '});',
  10616. 'this.DoIt = function (vG,vH,vI) {',
  10617. '};',
  10618. 'this.r = $mod.TRecord.$new();'
  10619. ]),
  10620. LinesToStr([
  10621. 'var $with1 = $mod.r;',
  10622. '$mod.DoIt($with1.i,$with1.i,{',
  10623. ' p: $with1,',
  10624. ' get: function () {',
  10625. ' return this.p.i;',
  10626. ' },',
  10627. ' set: function (v) {',
  10628. ' this.p.i = v;',
  10629. ' }',
  10630. '});',
  10631. '']));
  10632. end;
  10633. procedure TTestModule.TestRecord_Equal;
  10634. begin
  10635. StartProgram(false);
  10636. Add('type');
  10637. Add(' integer = longint;');
  10638. Add(' TFlag = (red,blue);');
  10639. Add(' TFlags = set of TFlag;');
  10640. Add(' TProc = procedure;');
  10641. Add(' TRecord = record');
  10642. Add(' i: integer;');
  10643. Add(' Event: TProc;');
  10644. Add(' f: TFlags;');
  10645. Add(' end;');
  10646. Add(' TNested = record');
  10647. Add(' r: TRecord;');
  10648. Add(' end;');
  10649. Add('var');
  10650. Add(' b: boolean;');
  10651. Add(' r,s: trecord;');
  10652. Add('begin');
  10653. Add(' b:=r=s;');
  10654. Add(' b:=r<>s;');
  10655. ConvertProgram;
  10656. CheckSource('TestRecord_Equal',
  10657. LinesToStr([ // statements
  10658. 'this.TFlag = {',
  10659. ' "0": "red",',
  10660. ' red: 0,',
  10661. ' "1": "blue",',
  10662. ' blue: 1',
  10663. '};',
  10664. 'rtl.recNewT($mod, "TRecord", function () {',
  10665. ' this.i = 0;',
  10666. ' this.Event = null;',
  10667. ' this.$new = function () {',
  10668. ' var r = Object.create(this);',
  10669. ' r.f = {};',
  10670. ' return r;',
  10671. ' };',
  10672. ' this.$eq = function (b) {',
  10673. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10674. ' };',
  10675. ' this.$assign = function (s) {',
  10676. ' this.i = s.i;',
  10677. ' this.Event = s.Event;',
  10678. ' this.f = rtl.refSet(s.f);',
  10679. ' return this;',
  10680. ' };',
  10681. '});',
  10682. 'rtl.recNewT($mod, "TNested", function () {',
  10683. ' this.$new = function () {',
  10684. ' var r = Object.create(this);',
  10685. ' r.r = $mod.TRecord.$new();',
  10686. ' return r;',
  10687. ' };',
  10688. ' this.$eq = function (b) {',
  10689. ' return this.r.$eq(b.r);',
  10690. ' };',
  10691. ' this.$assign = function (s) {',
  10692. ' this.r.$assign(s.r);',
  10693. ' return this;',
  10694. ' };',
  10695. '});',
  10696. 'this.b = false;',
  10697. 'this.r = $mod.TRecord.$new();',
  10698. 'this.s = $mod.TRecord.$new();',
  10699. '']),
  10700. LinesToStr([
  10701. '$mod.b = $mod.r.$eq($mod.s);',
  10702. '$mod.b = !$mod.r.$eq($mod.s);',
  10703. '']));
  10704. end;
  10705. procedure TTestModule.TestRecord_JSValue;
  10706. begin
  10707. StartProgram(false);
  10708. Add([
  10709. 'type',
  10710. ' TRecord = record',
  10711. ' i: longint;',
  10712. ' end;',
  10713. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10714. 'begin',
  10715. 'end;',
  10716. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10717. 'begin',
  10718. ' if jsvalue(d) then ;',
  10719. ' if jsvalue(c) then ;',
  10720. ' if jsvalue(v) then ;',
  10721. 'end;',
  10722. 'var',
  10723. ' Jv: jsvalue;',
  10724. ' Rec: trecord;',
  10725. 'begin',
  10726. ' rec:=trecord(jv);',
  10727. ' jv:=rec;',
  10728. ' Fly(rec,rec);',
  10729. ' Fly(@rec,@rec);',
  10730. ' if jsvalue(Rec) then ;',
  10731. ' Run(trecord(jv),trecord(jv),rec);',
  10732. '']);
  10733. ConvertProgram;
  10734. CheckSource('TestRecord_JSValue',
  10735. LinesToStr([ // statements
  10736. 'rtl.recNewT($mod, "TRecord", function () {',
  10737. ' this.i = 0;',
  10738. ' this.$eq = function (b) {',
  10739. ' return this.i === b.i;',
  10740. ' };',
  10741. ' this.$assign = function (s) {',
  10742. ' this.i = s.i;',
  10743. ' return this;',
  10744. ' };',
  10745. '});',
  10746. 'this.Fly = function (d, c) {',
  10747. '};',
  10748. 'this.Run = function (d, c, v) {',
  10749. ' if (d) ;',
  10750. ' if (c) ;',
  10751. ' if (v) ;',
  10752. '};',
  10753. 'this.Jv = undefined;',
  10754. 'this.Rec = $mod.TRecord.$new();',
  10755. '']),
  10756. LinesToStr([
  10757. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10758. '$mod.Jv = $mod.Rec;',
  10759. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10760. '$mod.Fly($mod.Rec, $mod.Rec);',
  10761. 'if ($mod.Rec) ;',
  10762. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10763. '']));
  10764. end;
  10765. procedure TTestModule.TestRecord_VariantFail;
  10766. begin
  10767. StartProgram(false);
  10768. Add([
  10769. 'type',
  10770. ' TRec = record',
  10771. ' case word of',
  10772. ' 0: (b0, b1: Byte);',
  10773. ' 1: (i: word);',
  10774. ' end;',
  10775. 'begin']);
  10776. SetExpectedPasResolverError('variant record is not supported',
  10777. nXIsNotSupported);
  10778. ConvertProgram;
  10779. end;
  10780. procedure TTestModule.TestRecord_FieldArray;
  10781. begin
  10782. StartProgram(false);
  10783. Add([
  10784. 'type',
  10785. ' TArrInt = array[3..4] of longint;',
  10786. ' TArrArrInt = array[3..4] of longint;',
  10787. ' TRec = record',
  10788. ' a: array of longint;',
  10789. ' s: array[1..2] of longint;',
  10790. ' m: array[1..2,3..4] of longint;',
  10791. ' o: TArrArrInt;',
  10792. ' end;',
  10793. 'begin']);
  10794. ConvertProgram;
  10795. CheckSource('TestRecord_FieldArray',
  10796. LinesToStr([ // statements
  10797. 'rtl.recNewT($mod, "TRec", function () {',
  10798. ' this.a = [];',
  10799. ' this.$new = function () {',
  10800. ' var r = Object.create(this);',
  10801. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10802. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10803. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10804. ' return r;',
  10805. ' };',
  10806. ' this.$eq = function (b) {',
  10807. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10808. ' };',
  10809. ' this.$assign = function (s) {',
  10810. ' this.a = s.a;',
  10811. ' this.s = s.s.slice(0);',
  10812. ' this.m = s.m.slice(0);',
  10813. ' this.o = s.o.slice(0);',
  10814. ' return this;',
  10815. ' };',
  10816. '});',
  10817. '']),
  10818. LinesToStr([ // $mod.$main
  10819. '']));
  10820. end;
  10821. procedure TTestModule.TestRecord_Const;
  10822. begin
  10823. StartProgram(false);
  10824. Add([
  10825. 'type',
  10826. ' TArrInt = array[3..4] of longint;',
  10827. ' TPoint = record x,y: longint; end;',
  10828. ' TRec = record',
  10829. ' i: longint;',
  10830. ' a: array of longint;',
  10831. ' s: array[1..2] of longint;',
  10832. ' m: array[1..2,3..4] of longint;',
  10833. ' p: TPoint;',
  10834. ' end;',
  10835. ' TPoints = array of TPoint;',
  10836. 'const',
  10837. ' r: TRec = (',
  10838. ' i:1;',
  10839. ' a:(2,3);',
  10840. ' s:(4,5);',
  10841. ' m:( (11,12), (13,14) );',
  10842. ' p: (x:21; y:22)',
  10843. ' );',
  10844. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10845. 'begin']);
  10846. ConvertProgram;
  10847. CheckSource('TestRecord_Const',
  10848. LinesToStr([ // statements
  10849. 'rtl.recNewT($mod, "TPoint", function () {',
  10850. ' this.x = 0;',
  10851. ' this.y = 0;',
  10852. ' this.$eq = function (b) {',
  10853. ' return (this.x === b.x) && (this.y === b.y);',
  10854. ' };',
  10855. ' this.$assign = function (s) {',
  10856. ' this.x = s.x;',
  10857. ' this.y = s.y;',
  10858. ' return this;',
  10859. ' };',
  10860. '});',
  10861. 'rtl.recNewT($mod, "TRec", function () {',
  10862. ' this.i = 0;',
  10863. ' this.a = [];',
  10864. ' this.$new = function () {',
  10865. ' var r = Object.create(this);',
  10866. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10867. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10868. ' r.p = $mod.TPoint.$new();',
  10869. ' return r;',
  10870. ' };',
  10871. ' this.$eq = function (b) {',
  10872. ' 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);',
  10873. ' };',
  10874. ' this.$assign = function (s) {',
  10875. ' this.i = s.i;',
  10876. ' this.a = s.a;',
  10877. ' this.s = s.s.slice(0);',
  10878. ' this.m = s.m.slice(0);',
  10879. ' this.p.$assign(s.p);',
  10880. ' return this;',
  10881. ' };',
  10882. '});',
  10883. 'this.r = $mod.TRec.$clone({',
  10884. ' i: 1,',
  10885. ' a: [2, 3],',
  10886. ' s: [4, 5],',
  10887. ' m: [[11, 12], [13, 14]],',
  10888. ' p: $mod.TPoint.$clone({',
  10889. ' x: 21,',
  10890. ' y: 22',
  10891. ' })',
  10892. '});',
  10893. 'this.p = [$mod.TPoint.$clone({',
  10894. ' x: 1,',
  10895. ' y: 2',
  10896. '}), $mod.TPoint.$clone({',
  10897. ' x: 3,',
  10898. ' y: 4',
  10899. '})];',
  10900. '']),
  10901. LinesToStr([ // $mod.$main
  10902. '']));
  10903. end;
  10904. procedure TTestModule.TestRecord_TypecastFail;
  10905. begin
  10906. StartProgram(false);
  10907. Add([
  10908. 'type',
  10909. ' TPoint = record x,y: longint; end;',
  10910. ' TRec = record l: longint end;',
  10911. 'var p: TPoint;',
  10912. 'begin',
  10913. ' if TRec(p).l=2 then ;']);
  10914. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10915. nIllegalTypeConversionTo);
  10916. ConvertProgram;
  10917. end;
  10918. procedure TTestModule.TestRecord_InFunction;
  10919. begin
  10920. StartProgram(false);
  10921. Add([
  10922. 'var TPoint: longint = 3;',
  10923. 'procedure DoIt;',
  10924. 'type',
  10925. ' TPoint = record x,y: longint; end;',
  10926. ' TPoints = array of TPoint;',
  10927. 'var',
  10928. ' r: TPoint;',
  10929. ' p: TPoints;',
  10930. 'begin',
  10931. ' SetLength(p,2);',
  10932. 'end;',
  10933. 'begin']);
  10934. ConvertProgram;
  10935. CheckSource('TestRecord_InFunction',
  10936. LinesToStr([ // statements
  10937. 'this.TPoint = 3;',
  10938. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10939. ' this.x = 0;',
  10940. ' this.y = 0;',
  10941. ' this.$eq = function (b) {',
  10942. ' return (this.x === b.x) && (this.y === b.y);',
  10943. ' };',
  10944. ' this.$assign = function (s) {',
  10945. ' this.x = s.x;',
  10946. ' this.y = s.y;',
  10947. ' return this;',
  10948. ' };',
  10949. '});',
  10950. 'this.DoIt = function () {',
  10951. ' var r = TPoint$1.$new();',
  10952. ' var p = [];',
  10953. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10954. '};',
  10955. '']),
  10956. LinesToStr([ // $mod.$main
  10957. '']));
  10958. end;
  10959. procedure TTestModule.TestRecord_AnonymousFail;
  10960. begin
  10961. StartProgram(false);
  10962. Add([
  10963. 'var',
  10964. ' r: record x: word end;',
  10965. 'begin']);
  10966. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  10967. nNotYetImplemented);
  10968. ConvertProgram;
  10969. end;
  10970. procedure TTestModule.TestAdvRecord_Function;
  10971. begin
  10972. StartProgram(false);
  10973. Parser.Options:=Parser.Options+[po_cassignments];
  10974. Add([
  10975. '{$modeswitch AdvancedRecords}',
  10976. 'type',
  10977. ' TPoint = record',
  10978. ' x,y: word;',
  10979. ' function Add(const apt: TPoint): TPoint;',
  10980. ' end;',
  10981. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10982. 'begin',
  10983. ' Result:=Self;',
  10984. ' Result.x+=apt.x;',
  10985. ' Result.y:=Result.y+apt.y;',
  10986. ' Self:=apt;',
  10987. 'end;',
  10988. 'var p,q: TPoint;',
  10989. 'begin',
  10990. ' p.add(q);',
  10991. ' p:=default(TPoint);',
  10992. ' p:=q;',
  10993. '']);
  10994. ConvertProgram;
  10995. CheckSource('TestAdvRecord_Function',
  10996. LinesToStr([ // statements
  10997. 'rtl.recNewT($mod, "TPoint", function () {',
  10998. ' this.x = 0;',
  10999. ' this.y = 0;',
  11000. ' this.$eq = function (b) {',
  11001. ' return (this.x === b.x) && (this.y === b.y);',
  11002. ' };',
  11003. ' this.$assign = function (s) {',
  11004. ' this.x = s.x;',
  11005. ' this.y = s.y;',
  11006. ' return this;',
  11007. ' };',
  11008. ' this.Add = function (apt) {',
  11009. ' var Result = $mod.TPoint.$new();',
  11010. ' Result.$assign(this);',
  11011. ' Result.x += apt.x;',
  11012. ' Result.y = Result.y + apt.y;',
  11013. ' this.$assign(apt);',
  11014. ' return Result;',
  11015. ' };',
  11016. '});',
  11017. 'this.p = $mod.TPoint.$new();',
  11018. 'this.q = $mod.TPoint.$new();',
  11019. '']),
  11020. LinesToStr([ // $mod.$main
  11021. '$mod.p.Add($mod.q);',
  11022. '$mod.p.$assign($mod.TPoint.$new());',
  11023. '$mod.p.$assign($mod.q);',
  11024. '']));
  11025. end;
  11026. procedure TTestModule.TestAdvRecord_Property;
  11027. begin
  11028. StartProgram(false);
  11029. Add([
  11030. '{$modeswitch AdvancedRecords}',
  11031. 'type',
  11032. ' TPoint = record',
  11033. ' x,y: word;',
  11034. ' strict private',
  11035. ' function GetSize: longword;',
  11036. ' procedure SetSize(Value: longword);',
  11037. ' public',
  11038. ' property Size: longword read GetSize write SetSize;',
  11039. ' property Left: word read x write y;',
  11040. ' end;',
  11041. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11042. 'function TPoint.GetSize: longword;',
  11043. 'begin',
  11044. ' x:=y;',
  11045. ' Size:=Size;',
  11046. ' Left:=Left;',
  11047. 'end;',
  11048. 'procedure TPoint.SetSize(Value: longword);',
  11049. 'begin',
  11050. 'end;',
  11051. 'var p,q: TPoint;',
  11052. 'begin',
  11053. ' p.Size:=q.Size;',
  11054. ' p.Left:=q.Left;',
  11055. '']);
  11056. ConvertProgram;
  11057. CheckSource('TestAdvRecord_Property',
  11058. LinesToStr([ // statements
  11059. 'rtl.recNewT($mod, "TPoint", function () {',
  11060. ' this.x = 0;',
  11061. ' this.y = 0;',
  11062. ' this.$eq = function (b) {',
  11063. ' return (this.x === b.x) && (this.y === b.y);',
  11064. ' };',
  11065. ' this.$assign = function (s) {',
  11066. ' this.x = s.x;',
  11067. ' this.y = s.y;',
  11068. ' return this;',
  11069. ' };',
  11070. ' this.GetSize = function () {',
  11071. ' var Result = 0;',
  11072. ' this.x = this.y;',
  11073. ' this.SetSize(this.GetSize());',
  11074. ' this.y = this.x;',
  11075. ' return Result;',
  11076. ' };',
  11077. ' this.SetSize = function (Value) {',
  11078. ' };',
  11079. '});',
  11080. 'this.SetSize = function (Value) {',
  11081. '};',
  11082. 'this.p = $mod.TPoint.$new();',
  11083. 'this.q = $mod.TPoint.$new();',
  11084. '']),
  11085. LinesToStr([ // $mod.$main
  11086. '$mod.p.SetSize($mod.q.GetSize());',
  11087. '$mod.p.y = $mod.q.x;',
  11088. '']));
  11089. end;
  11090. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11091. begin
  11092. StartProgram(false);
  11093. Add([
  11094. '{$modeswitch AdvancedRecords}',
  11095. 'type',
  11096. ' TPoint = record',
  11097. ' strict private',
  11098. ' function GetItems(Index: word): word;',
  11099. ' procedure SetItems(Index: word; Value: word);',
  11100. ' public',
  11101. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11102. ' end;',
  11103. 'function TPoint.GetItems(Index: word): word;',
  11104. 'begin',
  11105. ' Items[index]:=Items[index];',
  11106. ' self.Items[index]:=self.Items[index];',
  11107. 'end;',
  11108. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11109. 'begin',
  11110. 'end;',
  11111. 'var p: TPoint;',
  11112. 'begin',
  11113. ' p[1]:=p[2];',
  11114. ' p.Items[3]:=p.Items[4];',
  11115. '']);
  11116. ConvertProgram;
  11117. CheckSource('TestAdvRecord_PropertyDefault',
  11118. LinesToStr([ // statements
  11119. 'rtl.recNewT($mod, "TPoint", function () {',
  11120. ' this.$eq = function (b) {',
  11121. ' return true;',
  11122. ' };',
  11123. ' this.$assign = function (s) {',
  11124. ' return this;',
  11125. ' };',
  11126. ' this.GetItems = function (Index) {',
  11127. ' var Result = 0;',
  11128. ' this.SetItems(Index, this.GetItems(Index));',
  11129. ' this.SetItems(Index, this.GetItems(Index));',
  11130. ' return Result;',
  11131. ' };',
  11132. ' this.SetItems = function (Index, Value) {',
  11133. ' };',
  11134. '});',
  11135. 'this.p = $mod.TPoint.$new();',
  11136. '']),
  11137. LinesToStr([ // $mod.$main
  11138. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11139. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11140. '']));
  11141. end;
  11142. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11143. begin
  11144. StartProgram(false);
  11145. Add([
  11146. '{$modeswitch AdvancedRecords}',
  11147. 'type',
  11148. ' TRec = record',
  11149. ' class var',
  11150. ' Fx: longint;',
  11151. ' Fy: longint;',
  11152. ' class function GetInt: longint; static;',
  11153. ' class procedure SetInt(Value: longint); static;',
  11154. ' class procedure DoIt; static;',
  11155. ' class property IntA: longint read Fx write Fy;',
  11156. ' class property IntB: longint read GetInt write SetInt;',
  11157. ' end;',
  11158. 'class function trec.getint: longint;',
  11159. 'begin',
  11160. ' result:=fx;',
  11161. 'end;',
  11162. 'class procedure trec.setint(value: longint);',
  11163. 'begin',
  11164. 'end;',
  11165. 'class procedure trec.doit;',
  11166. 'begin',
  11167. ' IntA:=IntA+1;',
  11168. ' IntB:=IntB+1;',
  11169. 'end;',
  11170. 'var r: trec;',
  11171. 'begin',
  11172. ' trec.inta:=trec.inta+1;',
  11173. ' if trec.intb=2 then;',
  11174. ' trec.intb:=trec.intb+2;',
  11175. ' trec.setint(trec.inta);',
  11176. ' r.inta:=r.inta+1;',
  11177. ' if r.intb=2 then;',
  11178. ' r.intb:=r.intb+2;',
  11179. ' r.setint(r.inta);']);
  11180. ConvertProgram;
  11181. CheckSource('TestAdvRecord_Property_ClassMethod',
  11182. LinesToStr([ // statements
  11183. 'rtl.recNewT($mod, "TRec", function () {',
  11184. ' this.Fx = 0;',
  11185. ' this.Fy = 0;',
  11186. ' this.$eq = function (b) {',
  11187. ' return true;',
  11188. ' };',
  11189. ' this.$assign = function (s) {',
  11190. ' return this;',
  11191. ' };',
  11192. ' this.GetInt = function () {',
  11193. ' var Result = 0;',
  11194. ' Result = $mod.TRec.Fx;',
  11195. ' return Result;',
  11196. ' };',
  11197. ' this.SetInt = function (Value) {',
  11198. ' };',
  11199. ' this.DoIt = function () {',
  11200. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11201. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11202. ' };',
  11203. '}, true);',
  11204. 'this.r = $mod.TRec.$new();',
  11205. '']),
  11206. LinesToStr([ // $mod.$main
  11207. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11208. 'if ($mod.TRec.GetInt() === 2) ;',
  11209. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11210. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11211. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11212. 'if ($mod.r.GetInt() === 2) ;',
  11213. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11214. '$mod.r.SetInt($mod.r.Fx);',
  11215. '']));
  11216. end;
  11217. procedure TTestModule.TestAdvRecord_Const;
  11218. begin
  11219. StartProgram(false);
  11220. Add([
  11221. '{$modeswitch AdvancedRecords}',
  11222. 'type',
  11223. ' TArrInt = array[3..4] of longint;',
  11224. ' TPoint = record',
  11225. ' x,y: longint;',
  11226. ' class var Count: nativeint;',
  11227. ' end;',
  11228. ' TRec = record',
  11229. ' i: longint;',
  11230. ' a: array of longint;',
  11231. ' s: array[1..2] of longint;',
  11232. ' m: array[1..2,3..4] of longint;',
  11233. ' p: TPoint;',
  11234. ' end;',
  11235. ' TPoints = array of TPoint;',
  11236. 'const',
  11237. ' r: TRec = (',
  11238. ' i:1;',
  11239. ' a:(2,3);',
  11240. ' s:(4,5);',
  11241. ' m:( (11,12), (13,14) );',
  11242. ' p: (x:21)',
  11243. ' );',
  11244. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11245. 'begin']);
  11246. ConvertProgram;
  11247. CheckSource('TestAdvRecord_Const',
  11248. LinesToStr([ // statements
  11249. 'rtl.recNewT($mod, "TPoint", function () {',
  11250. ' this.x = 0;',
  11251. ' this.y = 0;',
  11252. ' this.Count = 0;',
  11253. ' this.$eq = function (b) {',
  11254. ' return (this.x === b.x) && (this.y === b.y);',
  11255. ' };',
  11256. ' this.$assign = function (s) {',
  11257. ' this.x = s.x;',
  11258. ' this.y = s.y;',
  11259. ' return this;',
  11260. ' };',
  11261. '}, true);',
  11262. 'rtl.recNewT($mod, "TRec", function () {',
  11263. ' this.i = 0;',
  11264. ' this.a = [];',
  11265. ' this.$new = function () {',
  11266. ' var r = Object.create(this);',
  11267. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11268. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11269. ' r.p = $mod.TPoint.$new();',
  11270. ' return r;',
  11271. ' };',
  11272. ' this.$eq = function (b) {',
  11273. ' 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);',
  11274. ' };',
  11275. ' this.$assign = function (s) {',
  11276. ' this.i = s.i;',
  11277. ' this.a = s.a;',
  11278. ' this.s = s.s.slice(0);',
  11279. ' this.m = s.m.slice(0);',
  11280. ' this.p.$assign(s.p);',
  11281. ' return this;',
  11282. ' };',
  11283. '});',
  11284. 'this.r = $mod.TRec.$clone({',
  11285. ' i: 1,',
  11286. ' a: [2, 3],',
  11287. ' s: [4, 5],',
  11288. ' m: [[11, 12], [13, 14]],',
  11289. ' p: $mod.TPoint.$clone({',
  11290. ' x: 21,',
  11291. ' y: 0',
  11292. ' })',
  11293. '});',
  11294. 'this.p = [$mod.TPoint.$clone({',
  11295. ' x: 1,',
  11296. ' y: 2',
  11297. '}), $mod.TPoint.$clone({',
  11298. ' x: 3,',
  11299. ' y: 4',
  11300. '})];',
  11301. '']),
  11302. LinesToStr([ // $mod.$main
  11303. '']));
  11304. end;
  11305. procedure TTestModule.TestAdvRecord_ExternalField;
  11306. begin
  11307. StartProgram(false);
  11308. Add([
  11309. '{$modeswitch AdvancedRecords}',
  11310. '{$modeswitch externalclass}',
  11311. 'type',
  11312. ' TCar = record',
  11313. ' public',
  11314. ' Intern: longint external name ''$Intern'';',
  11315. ' Intern2: longint external name ''$Intern2'';',
  11316. ' Bracket: longint external name ''["A B"]'';',
  11317. ' procedure DoIt;',
  11318. ' end;',
  11319. 'procedure tcar.doit;',
  11320. 'begin',
  11321. ' Intern:=Intern+1;',
  11322. ' Intern2:=Intern2+2;',
  11323. ' Bracket:=Bracket+3;',
  11324. 'end;',
  11325. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11326. 'begin',
  11327. ' Rec.intern:=Rec.intern+1;',
  11328. ' Rec.intern2:=Rec.intern2+2;',
  11329. ' Rec.Bracket:=Rec.Bracket+3;',
  11330. ' with Rec do begin',
  11331. ' intern:=intern+1;',
  11332. ' intern2:=intern2+2;',
  11333. ' Bracket:=Bracket+3;',
  11334. ' end;']);
  11335. ConvertProgram;
  11336. CheckSource('TestAdvRecord_ExternalField',
  11337. LinesToStr([ // statements
  11338. 'rtl.recNewT($mod, "TCar", function () {',
  11339. ' this.$eq = function (b) {',
  11340. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11341. ' };',
  11342. ' this.$assign = function (s) {',
  11343. ' this.$Intern = s.$Intern;',
  11344. ' this.$Intern2 = s.$Intern2;',
  11345. ' this["A B"] = s["A B"];',
  11346. ' return this;',
  11347. ' };',
  11348. ' this.DoIt = function () {',
  11349. ' this.$Intern = this.$Intern + 1;',
  11350. ' this.$Intern2 = this.$Intern2 + 2;',
  11351. ' this["A B"] = this["A B"] + 3;',
  11352. ' };',
  11353. '});',
  11354. 'this.Rec = $mod.TCar.$clone({',
  11355. ' $Intern: 11,',
  11356. ' $Intern2: 12,',
  11357. ' "A B": 13',
  11358. '});',
  11359. '']),
  11360. LinesToStr([ // $mod.$main
  11361. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11362. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11363. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11364. 'var $with1 = $mod.Rec;',
  11365. '$with1.$Intern = $with1.$Intern + 1;',
  11366. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  11367. '$with1["A B"] = $with1["A B"] + 3;',
  11368. '']));
  11369. end;
  11370. procedure TTestModule.TestAdvRecord_SubRecord;
  11371. begin
  11372. StartProgram(false);
  11373. Add([
  11374. '{$modeswitch AdvancedRecords}',
  11375. 'type',
  11376. ' TRec = record',
  11377. ' type',
  11378. ' TPoint = record',
  11379. ' x,y: longint;',
  11380. ' class var Count: nativeint;',
  11381. ' procedure DoIt;',
  11382. ' class procedure DoThat; static;',
  11383. ' end;',
  11384. ' var',
  11385. ' i: longint;',
  11386. ' p: TPoint;',
  11387. ' procedure DoSome;',
  11388. ' end;',
  11389. 'const',
  11390. ' r: TRec = (',
  11391. ' i:1;',
  11392. ' p: (x:21;y:22)',
  11393. ' );',
  11394. 'procedure TRec.DoSome;',
  11395. 'begin',
  11396. ' p.x:=p.y+1;',
  11397. ' p.Count:=p.Count+2;',
  11398. 'end;',
  11399. 'procedure TRec.TPoint.DoIt;',
  11400. 'begin',
  11401. ' Count:=Count+3;',
  11402. 'end;',
  11403. 'class procedure TRec.TPoint.DoThat;',
  11404. 'begin',
  11405. ' Count:=Count+4;',
  11406. 'end;',
  11407. 'begin']);
  11408. ConvertProgram;
  11409. CheckSource('TestAdvRecord_SubRecord',
  11410. LinesToStr([ // statements
  11411. 'rtl.recNewT($mod, "TRec", function () {',
  11412. ' rtl.recNewT(this, "TPoint", function () {',
  11413. ' this.x = 0;',
  11414. ' this.y = 0;',
  11415. ' this.Count = 0;',
  11416. ' this.$eq = function (b) {',
  11417. ' return (this.x === b.x) && (this.y === b.y);',
  11418. ' };',
  11419. ' this.$assign = function (s) {',
  11420. ' this.x = s.x;',
  11421. ' this.y = s.y;',
  11422. ' return this;',
  11423. ' };',
  11424. ' this.DoIt = function () {',
  11425. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11426. ' };',
  11427. ' this.DoThat = function () {',
  11428. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11429. ' };',
  11430. ' }, true);',
  11431. ' this.i = 0;',
  11432. ' this.$new = function () {',
  11433. ' var r = Object.create(this);',
  11434. ' r.p = this.TPoint.$new();',
  11435. ' return r;',
  11436. ' };',
  11437. ' this.$eq = function (b) {',
  11438. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11439. ' };',
  11440. ' this.$assign = function (s) {',
  11441. ' this.i = s.i;',
  11442. ' this.p.$assign(s.p);',
  11443. ' return this;',
  11444. ' };',
  11445. ' this.DoSome = function () {',
  11446. ' this.p.x = this.p.y + 1;',
  11447. ' this.TPoint.Count = this.p.Count + 2;',
  11448. ' };',
  11449. '}, true);',
  11450. 'this.r = $mod.TRec.$clone({',
  11451. ' i: 1,',
  11452. ' p: $mod.TRec.TPoint.$clone({',
  11453. ' x: 21,',
  11454. ' y: 22',
  11455. ' })',
  11456. '});',
  11457. '']),
  11458. LinesToStr([ // $mod.$main
  11459. '']));
  11460. end;
  11461. procedure TTestModule.TestAdvRecord_SubClass;
  11462. begin
  11463. StartProgram(false);
  11464. Add([
  11465. '{$modeswitch AdvancedRecords}',
  11466. 'type',
  11467. ' TObject = class end;',
  11468. ' TPoint = record',
  11469. ' type',
  11470. ' TBird = class',
  11471. ' procedure DoIt;',
  11472. ' class procedure Glob;',
  11473. ' end;',
  11474. ' procedure DoIt(b: TBird);',
  11475. ' end;',
  11476. 'procedure TPoint.TBird.DoIt;',
  11477. 'begin',
  11478. ' doit;',
  11479. ' self.doit;',
  11480. ' glob;',
  11481. ' self.glob;',
  11482. 'end;',
  11483. 'class procedure TPoint.TBird.Glob;',
  11484. 'begin',
  11485. ' glob;',
  11486. ' self.glob;',
  11487. 'end;',
  11488. 'procedure TPoint.DoIt(b: TBird);',
  11489. 'begin',
  11490. ' b.doit;',
  11491. ' b.glob;',
  11492. ' TBird.glob;',
  11493. 'end;',
  11494. 'begin',
  11495. '']);
  11496. ConvertProgram;
  11497. CheckSource('TestAdvRecord_SubClass',
  11498. LinesToStr([ // statements
  11499. 'rtl.createClass($mod, "TObject", null, function () {',
  11500. ' this.$init = function () {',
  11501. ' };',
  11502. ' this.$final = function () {',
  11503. ' };',
  11504. '});',
  11505. 'rtl.recNewT($mod, "TPoint", function () {',
  11506. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11507. ' this.DoIt = function () {',
  11508. ' this.DoIt();',
  11509. ' this.DoIt();',
  11510. ' this.$class.Glob();',
  11511. ' this.$class.Glob();',
  11512. ' };',
  11513. ' this.Glob = function () {',
  11514. ' this.Glob();',
  11515. ' this.Glob();',
  11516. ' };',
  11517. ' });',
  11518. ' this.$eq = function (b) {',
  11519. ' return true;',
  11520. ' };',
  11521. ' this.$assign = function (s) {',
  11522. ' return this;',
  11523. ' };',
  11524. ' this.DoIt = function (b) {',
  11525. ' b.DoIt();',
  11526. ' b.$class.Glob();',
  11527. ' this.TBird.Glob();',
  11528. ' };',
  11529. '}, true);',
  11530. '']),
  11531. LinesToStr([ // $mod.$main
  11532. '']));
  11533. end;
  11534. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11535. begin
  11536. StartProgram(false);
  11537. Add([
  11538. '{$modeswitch AdvancedRecords}',
  11539. 'type',
  11540. ' IUnknown = interface end;',
  11541. ' TPoint = record',
  11542. ' type IBird = interface end;',
  11543. ' end;',
  11544. 'begin',
  11545. '']);
  11546. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11547. nNotYetImplemented);
  11548. ParseProgram;
  11549. end;
  11550. procedure TTestModule.TestAdvRecord_Constructor;
  11551. begin
  11552. StartProgram(false);
  11553. Add([
  11554. '{$modeswitch AdvancedRecords}',
  11555. 'type',
  11556. ' TPoint = record',
  11557. ' x,y: longint;',
  11558. ' constructor Create(ax: longint; ay: longint = -1);',
  11559. ' end;',
  11560. 'constructor tpoint.create(ax,ay: longint);',
  11561. 'begin',
  11562. ' x:=ax;',
  11563. ' self.y:=ay;',
  11564. 'end;',
  11565. 'var r: TPoint;',
  11566. 'begin',
  11567. ' r:=TPoint.Create(1,2);',
  11568. ' with TPoint do r:=Create(1,2);',
  11569. ' r.Create(3);',
  11570. ' r:=r.Create(4);',
  11571. '']);
  11572. ConvertProgram;
  11573. CheckSource('TestAdvRecord_Constructor',
  11574. LinesToStr([ // statements
  11575. 'rtl.recNewT($mod, "TPoint", function () {',
  11576. ' this.x = 0;',
  11577. ' this.y = 0;',
  11578. ' this.$eq = function (b) {',
  11579. ' return (this.x === b.x) && (this.y === b.y);',
  11580. ' };',
  11581. ' this.$assign = function (s) {',
  11582. ' this.x = s.x;',
  11583. ' this.y = s.y;',
  11584. ' return this;',
  11585. ' };',
  11586. ' this.Create = function (ax, ay) {',
  11587. ' this.x = ax;',
  11588. ' this.y = ay;',
  11589. ' return this;',
  11590. ' };',
  11591. '}, true);',
  11592. 'this.r = $mod.TPoint.$new();',
  11593. '']),
  11594. LinesToStr([ // $mod.$main
  11595. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11596. 'var $with1 = $mod.TPoint;',
  11597. '$mod.r.$assign($with1.$new().Create(1, 2));',
  11598. '$mod.r.Create(3, -1);',
  11599. '$mod.r.$assign($mod.r.Create(4, -1));',
  11600. '']));
  11601. end;
  11602. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11603. begin
  11604. StartProgram(false);
  11605. Add([
  11606. '{$modeswitch AdvancedRecords}',
  11607. 'type',
  11608. ' TPoint = record',
  11609. ' class var x: longint;',
  11610. ' class procedure Fly; static;',
  11611. ' class constructor Init;',
  11612. ' end;',
  11613. 'var count: word;',
  11614. 'class procedure Tpoint.Fly;',
  11615. 'begin',
  11616. 'end;',
  11617. 'class constructor tpoint.init;',
  11618. 'begin',
  11619. ' count:=count+1;',
  11620. ' x:=x+3;',
  11621. ' tpoint.x:=tpoint.x+4;',
  11622. ' fly;',
  11623. ' tpoint.fly;',
  11624. 'end;',
  11625. 'var r: TPoint;',
  11626. 'begin',
  11627. ' r.x:=r.x+10;',
  11628. ' r.Fly;',
  11629. ' r.Fly();',
  11630. '']);
  11631. ConvertProgram;
  11632. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11633. LinesToStr([ // statements
  11634. 'rtl.recNewT($mod, "TPoint", function () {',
  11635. ' this.x = 0;',
  11636. ' this.$eq = function (b) {',
  11637. ' return true;',
  11638. ' };',
  11639. ' this.$assign = function (s) {',
  11640. ' return this;',
  11641. ' };',
  11642. ' this.Fly = function () {',
  11643. ' };',
  11644. '}, true);',
  11645. 'this.count = 0;',
  11646. 'this.r = $mod.TPoint.$new();',
  11647. '']),
  11648. LinesToStr([ // $mod.$main
  11649. '(function () {',
  11650. ' $mod.count = $mod.count + 1;',
  11651. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11652. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11653. ' $mod.TPoint.Fly();',
  11654. ' $mod.TPoint.Fly();',
  11655. '})();',
  11656. '$mod.TPoint.x = $mod.r.x + 10;',
  11657. '$mod.r.Fly();',
  11658. '$mod.r.Fly();',
  11659. '']));
  11660. end;
  11661. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11662. begin
  11663. StartUnit(false);
  11664. Add([
  11665. 'interface',
  11666. '{$modeswitch AdvancedRecords}',
  11667. 'type',
  11668. ' TPoint = record',
  11669. ' class var x: longint;',
  11670. ' class procedure Fly; static;',
  11671. ' class constructor Init;',
  11672. ' end;',
  11673. 'implementation',
  11674. 'var count: word;',
  11675. 'class procedure Tpoint.Fly;',
  11676. 'begin',
  11677. 'end;',
  11678. 'class constructor tpoint.init;',
  11679. 'begin',
  11680. ' count:=count+1;',
  11681. ' x:=3;',
  11682. ' tpoint.x:=4;',
  11683. ' fly;',
  11684. ' tpoint.fly;',
  11685. 'end;',
  11686. '']);
  11687. ConvertUnit;
  11688. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11689. LinesToStr([ // statements
  11690. 'var $impl = $mod.$impl;',
  11691. 'rtl.recNewT($mod, "TPoint", function () {',
  11692. ' this.x = 0;',
  11693. ' this.$eq = function (b) {',
  11694. ' return true;',
  11695. ' };',
  11696. ' this.$assign = function (s) {',
  11697. ' return this;',
  11698. ' };',
  11699. ' this.Fly = function () {',
  11700. ' };',
  11701. '}, true);',
  11702. '']),
  11703. LinesToStr([ // $mod.$init
  11704. '(function () {',
  11705. ' $impl.count = $impl.count + 1;',
  11706. ' $mod.TPoint.x = 3;',
  11707. ' $mod.TPoint.x = 4;',
  11708. ' $mod.TPoint.Fly();',
  11709. ' $mod.TPoint.Fly();',
  11710. '})();',
  11711. '']),
  11712. LinesToStr([ // $mod.$main
  11713. '$impl.count = 0;',
  11714. '']));
  11715. end;
  11716. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11717. begin
  11718. StartProgram(false);
  11719. Add(['type',
  11720. ' TObject = class',
  11721. ' public',
  11722. ' constructor Create;',
  11723. ' destructor Destroy;',
  11724. ' end;',
  11725. ' TBird = TObject;',
  11726. 'constructor tobject.create;',
  11727. 'begin end;',
  11728. 'destructor tobject.destroy;',
  11729. 'begin end;',
  11730. 'var Obj: tobject;',
  11731. 'begin',
  11732. ' obj:=tobject.create;',
  11733. ' obj:=tobject.create();',
  11734. ' obj:=tbird.create;',
  11735. ' obj:=tbird.create();',
  11736. ' obj:=obj.create();',
  11737. ' obj.destroy;',
  11738. '']);
  11739. ConvertProgram;
  11740. CheckSource('TestClass_TObjectDefaultConstructor',
  11741. LinesToStr([ // statements
  11742. 'rtl.createClass($mod,"TObject",null,function(){',
  11743. ' this.$init = function () {',
  11744. ' };',
  11745. ' this.$final = function () {',
  11746. ' };',
  11747. ' this.Create = function(){',
  11748. ' return this;',
  11749. ' };',
  11750. ' this.Destroy = function(){',
  11751. ' };',
  11752. '});',
  11753. 'this.Obj = null;'
  11754. ]),
  11755. LinesToStr([ // $mod.$main
  11756. '$mod.Obj = $mod.TObject.$create("Create");',
  11757. '$mod.Obj = $mod.TObject.$create("Create");',
  11758. '$mod.Obj = $mod.TObject.$create("Create");',
  11759. '$mod.Obj = $mod.TObject.$create("Create");',
  11760. '$mod.Obj = $mod.Obj.Create();',
  11761. '$mod.Obj.$destroy("Destroy");',
  11762. '']));
  11763. end;
  11764. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11765. begin
  11766. StartProgram(false);
  11767. Add('type');
  11768. Add(' TObject = class');
  11769. Add(' public');
  11770. Add(' constructor Create(Par: longint);');
  11771. Add(' end;');
  11772. Add('constructor tobject.create(par: longint);');
  11773. Add('begin end;');
  11774. Add('var Obj: tobject;');
  11775. Add('begin');
  11776. Add(' obj:=tobject.create(3);');
  11777. ConvertProgram;
  11778. CheckSource('TestClass_TObjectConstructorWithParams',
  11779. LinesToStr([ // statements
  11780. 'rtl.createClass($mod,"TObject",null,function(){',
  11781. ' this.$init = function () {',
  11782. ' };',
  11783. ' this.$final = function () {',
  11784. ' };',
  11785. ' this.Create = function(Par){',
  11786. ' return this;',
  11787. ' };',
  11788. '});',
  11789. 'this.Obj = null;'
  11790. ]),
  11791. LinesToStr([ // $mod.$main
  11792. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11793. ]));
  11794. end;
  11795. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11796. begin
  11797. StartProgram(false);
  11798. Add('type');
  11799. Add(' TObject = class');
  11800. Add(' public');
  11801. Add(' constructor Create;');
  11802. Add(' end;');
  11803. Add(' TTest = class(TObject)');
  11804. Add(' public');
  11805. Add(' constructor Create(const Par: longint = 1);');
  11806. Add(' end;');
  11807. Add('constructor tobject.create;');
  11808. Add('begin end;');
  11809. Add('constructor ttest.create(const par: longint);');
  11810. Add('begin end;');
  11811. Add('var t: ttest;');
  11812. Add('begin');
  11813. Add(' t:=ttest.create;');
  11814. Add(' t:=ttest.create(2);');
  11815. ConvertProgram;
  11816. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11817. LinesToStr([ // statements
  11818. 'rtl.createClass($mod,"TObject",null,function(){',
  11819. ' this.$init = function () {',
  11820. ' };',
  11821. ' this.$final = function () {',
  11822. ' };',
  11823. ' this.Create = function(){',
  11824. ' return this;',
  11825. ' };',
  11826. '});',
  11827. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11828. ' this.Create$1 = function (Par) {',
  11829. ' return this;',
  11830. ' };',
  11831. '});',
  11832. 'this.t = null;'
  11833. ]),
  11834. LinesToStr([ // $mod.$main
  11835. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11836. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11837. ]));
  11838. end;
  11839. procedure TTestModule.TestClass_Var;
  11840. begin
  11841. StartProgram(false);
  11842. Add([
  11843. 'type',
  11844. ' TObject = class',
  11845. ' public',
  11846. ' vI: longint;',
  11847. ' constructor Create(Par: longint);',
  11848. ' end;',
  11849. 'constructor tobject.create(par: longint);',
  11850. 'begin',
  11851. ' vi:=par+3',
  11852. 'end;',
  11853. 'var Obj: tobject;',
  11854. 'begin',
  11855. ' obj:=tobject.create(4);',
  11856. ' obj.vi:=obj.VI+5;']);
  11857. ConvertProgram;
  11858. CheckSource('TestClass_Var',
  11859. LinesToStr([ // statements
  11860. 'rtl.createClass($mod,"TObject",null,function(){',
  11861. ' this.$init = function () {',
  11862. ' this.vI = 0;',
  11863. ' };',
  11864. ' this.$final = function () {',
  11865. ' };',
  11866. ' this.Create = function(Par){',
  11867. ' this.vI = Par+3;',
  11868. ' return this;',
  11869. ' };',
  11870. '});',
  11871. 'this.Obj = null;'
  11872. ]),
  11873. LinesToStr([ // $mod.$main
  11874. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11875. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11876. ]));
  11877. end;
  11878. procedure TTestModule.TestClass_Method;
  11879. begin
  11880. StartProgram(false);
  11881. Add('type');
  11882. Add(' TObject = class');
  11883. Add(' public');
  11884. Add(' vI: longint;');
  11885. Add(' Sub: TObject;');
  11886. Add(' constructor Create;');
  11887. Add(' function GetIt(Par: longint): tobject;');
  11888. Add(' end;');
  11889. Add('constructor tobject.create; begin end;');
  11890. Add('function tobject.getit(par: longint): tobject;');
  11891. Add('begin');
  11892. Add(' Self.vi:=par+3;');
  11893. Add(' Result:=self.sub;');
  11894. Add('end;');
  11895. Add('var Obj: tobject;');
  11896. Add('begin');
  11897. Add(' obj:=tobject.create;');
  11898. Add(' obj.getit(4);');
  11899. Add(' obj.sub.sub:=nil;');
  11900. Add(' obj.sub.getit(5);');
  11901. Add(' obj.sub.getit(6).SUB:=nil;');
  11902. Add(' obj.sub.getit(7).GETIT(8);');
  11903. Add(' obj.sub.getit(9).SuB.getit(10);');
  11904. ConvertProgram;
  11905. CheckSource('TestClass_Method',
  11906. LinesToStr([ // statements
  11907. 'rtl.createClass($mod,"TObject",null,function(){',
  11908. ' this.$init = function () {',
  11909. ' this.vI = 0;',
  11910. ' this.Sub = null;',
  11911. ' };',
  11912. ' this.$final = function () {',
  11913. ' this.Sub = undefined;',
  11914. ' };',
  11915. ' this.Create = function(){',
  11916. ' return this;',
  11917. ' };',
  11918. ' this.GetIt = function(Par){',
  11919. ' var Result = null;',
  11920. ' this.vI = Par + 3;',
  11921. ' Result = this.Sub;',
  11922. ' return Result;',
  11923. ' };',
  11924. '});',
  11925. 'this.Obj = null;'
  11926. ]),
  11927. LinesToStr([ // $mod.$main
  11928. '$mod.Obj = $mod.TObject.$create("Create");',
  11929. '$mod.Obj.GetIt(4);',
  11930. '$mod.Obj.Sub.Sub=null;',
  11931. '$mod.Obj.Sub.GetIt(5);',
  11932. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11933. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11934. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11935. ]));
  11936. end;
  11937. procedure TTestModule.TestClass_Implementation;
  11938. begin
  11939. StartUnit(false);
  11940. Add([
  11941. 'interface',
  11942. 'type',
  11943. ' TObject = class',
  11944. ' constructor Create;',
  11945. ' end;',
  11946. 'implementation',
  11947. 'type',
  11948. ' TIntClass = class',
  11949. ' constructor Create; reintroduce;',
  11950. ' class procedure DoGlob;',
  11951. ' end;',
  11952. 'constructor tintclass.create;',
  11953. 'begin',
  11954. ' inherited;',
  11955. ' inherited create;',
  11956. ' doglob;',
  11957. 'end;',
  11958. 'class procedure tintclass.doglob;',
  11959. 'begin',
  11960. 'end;',
  11961. 'constructor tobject.create;',
  11962. 'var',
  11963. ' iC: tintclass;',
  11964. 'begin',
  11965. ' ic:=tintclass.create;',
  11966. ' tintclass.doglob;',
  11967. ' ic.doglob;',
  11968. 'end;',
  11969. 'initialization',
  11970. ' tintclass.doglob;',
  11971. '']);
  11972. ConvertUnit;
  11973. CheckSource('TestClass_Implementation',
  11974. LinesToStr([ // statements
  11975. 'var $impl = $mod.$impl;',
  11976. 'rtl.createClass($mod, "TObject", null, function () {',
  11977. ' this.$init = function () {',
  11978. ' };',
  11979. ' this.$final = function () {',
  11980. ' };',
  11981. ' this.Create = function () {',
  11982. ' var iC = null;',
  11983. ' iC = $impl.TIntClass.$create("Create$1");',
  11984. ' $impl.TIntClass.DoGlob();',
  11985. ' iC.$class.DoGlob();',
  11986. ' return this;',
  11987. ' };',
  11988. '});',
  11989. '']),
  11990. LinesToStr([ // $mod.$main
  11991. '$impl.TIntClass.DoGlob();',
  11992. '']),
  11993. LinesToStr([
  11994. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11995. ' this.Create$1 = function () {',
  11996. ' $mod.TObject.Create.call(this);',
  11997. ' $mod.TObject.Create.call(this);',
  11998. ' this.$class.DoGlob();',
  11999. ' return this;',
  12000. ' };',
  12001. ' this.DoGlob = function () {',
  12002. ' };',
  12003. '});',
  12004. '']));
  12005. end;
  12006. procedure TTestModule.TestClass_Inheritance;
  12007. begin
  12008. StartProgram(false);
  12009. Add('type');
  12010. Add(' TObject = class');
  12011. Add(' public');
  12012. Add(' constructor Create;');
  12013. Add(' end;');
  12014. Add(' TClassA = class');
  12015. Add(' end;');
  12016. Add(' TClassB = class(TObject)');
  12017. Add(' procedure ProcB;');
  12018. Add(' end;');
  12019. Add('constructor tobject.create; begin end;');
  12020. Add('procedure tclassb.procb; begin end;');
  12021. Add('var');
  12022. Add(' oO: TObject;');
  12023. Add(' oA: TClassA;');
  12024. Add(' oB: TClassB;');
  12025. Add('begin');
  12026. Add(' oO:=tobject.Create;');
  12027. Add(' oA:=tclassa.Create;');
  12028. Add(' ob:=tclassb.Create;');
  12029. Add(' if oo is tclassa then ;');
  12030. Add(' ob:=oo as tclassb;');
  12031. Add(' (oo as tclassb).procb;');
  12032. ConvertProgram;
  12033. CheckSource('TestClass_Inheritance',
  12034. LinesToStr([ // statements
  12035. 'rtl.createClass($mod,"TObject",null,function(){',
  12036. ' this.$init = function () {',
  12037. ' };',
  12038. ' this.$final = function () {',
  12039. ' };',
  12040. ' this.Create = function () {',
  12041. ' return this;',
  12042. ' };',
  12043. '});',
  12044. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  12045. '});',
  12046. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  12047. ' this.ProcB = function () {',
  12048. ' };',
  12049. '});',
  12050. 'this.oO = null;',
  12051. 'this.oA = null;',
  12052. 'this.oB = null;'
  12053. ]),
  12054. LinesToStr([ // $mod.$main
  12055. '$mod.oO = $mod.TObject.$create("Create");',
  12056. '$mod.oA = $mod.TClassA.$create("Create");',
  12057. '$mod.oB = $mod.TClassB.$create("Create");',
  12058. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12059. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12060. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12061. ]));
  12062. end;
  12063. procedure TTestModule.TestClass_TypeAlias;
  12064. begin
  12065. StartProgram(false);
  12066. Add([
  12067. '{$interfaces corba}',
  12068. 'type',
  12069. ' IObject = interface',
  12070. ' end;',
  12071. ' IBird = type IObject;',
  12072. ' TObject = class',
  12073. ' end;',
  12074. ' TBird = type TObject;',
  12075. 'var',
  12076. ' oObj: TObject;',
  12077. ' oBird: TBird;',
  12078. ' IntfObj: IObject;',
  12079. ' IntfBird: IBird;',
  12080. 'begin',
  12081. ' oObj:=oBird;',
  12082. '']);
  12083. ConvertProgram;
  12084. CheckSource('TestClass_TypeAlias',
  12085. LinesToStr([ // statements
  12086. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12087. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  12088. 'rtl.createClass($mod, "TObject", null, function () {',
  12089. ' this.$init = function () {',
  12090. ' };',
  12091. ' this.$final = function () {',
  12092. ' };',
  12093. '});',
  12094. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12095. '});',
  12096. 'this.oObj = null;',
  12097. 'this.oBird = null;',
  12098. 'this.IntfObj = null;',
  12099. 'this.IntfBird = null;',
  12100. '']),
  12101. LinesToStr([ // $mod.$main
  12102. '$mod.oObj = $mod.oBird;',
  12103. '']));
  12104. end;
  12105. procedure TTestModule.TestClass_AbstractMethod;
  12106. begin
  12107. StartProgram(false);
  12108. Add('type');
  12109. Add(' TObject = class');
  12110. Add(' public');
  12111. Add(' procedure DoIt; virtual; abstract;');
  12112. Add(' end;');
  12113. Add('begin');
  12114. ConvertProgram;
  12115. CheckSource('TestClass_AbstractMethod',
  12116. LinesToStr([ // statements
  12117. 'rtl.createClass($mod,"TObject",null,function(){',
  12118. ' this.$init = function () {',
  12119. ' };',
  12120. ' this.$final = function () {',
  12121. ' };',
  12122. '});'
  12123. ]),
  12124. LinesToStr([ // this.$main
  12125. ''
  12126. ]));
  12127. end;
  12128. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12129. begin
  12130. StartProgram(false);
  12131. Add([
  12132. 'type',
  12133. ' TObject = class',
  12134. ' procedure DoAbstract; virtual; abstract;',
  12135. ' procedure DoVirtual; virtual;',
  12136. ' procedure DoIt;',
  12137. ' end;',
  12138. ' TA = class',
  12139. ' procedure doabstract; override;',
  12140. ' procedure dovirtual; override;',
  12141. ' procedure DoSome;',
  12142. ' end;',
  12143. 'procedure tobject.dovirtual;',
  12144. 'begin',
  12145. ' inherited; // call non existing ancestor -> ignore silently',
  12146. 'end;',
  12147. 'procedure tobject.doit;',
  12148. 'begin',
  12149. 'end;',
  12150. 'procedure ta.doabstract;',
  12151. 'begin',
  12152. ' inherited dovirtual; // call TObject.DoVirtual',
  12153. 'end;',
  12154. 'procedure ta.dovirtual;',
  12155. 'begin',
  12156. ' inherited; // call TObject.DoVirtual',
  12157. ' inherited dovirtual; // call TObject.DoVirtual',
  12158. ' inherited dovirtual(); // call TObject.DoVirtual',
  12159. ' doit;',
  12160. ' doit();',
  12161. 'end;',
  12162. 'procedure ta.dosome;',
  12163. 'begin',
  12164. ' inherited; // call non existing ancestor method -> silently ignore',
  12165. 'end;',
  12166. 'begin']);
  12167. ConvertProgram;
  12168. CheckSource('TestClass_CallInherited_ProcNoParams',
  12169. LinesToStr([ // statements
  12170. 'rtl.createClass($mod,"TObject",null,function(){',
  12171. ' this.$init = function () {',
  12172. ' };',
  12173. ' this.$final = function () {',
  12174. ' };',
  12175. ' this.DoVirtual = function () {',
  12176. ' };',
  12177. ' this.DoIt = function () {',
  12178. ' };',
  12179. '});',
  12180. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12181. ' this.DoAbstract = function () {',
  12182. ' $mod.TObject.DoVirtual.call(this);',
  12183. ' };',
  12184. ' this.DoVirtual = function () {',
  12185. ' $mod.TObject.DoVirtual.call(this);',
  12186. ' $mod.TObject.DoVirtual.call(this);',
  12187. ' $mod.TObject.DoVirtual.call(this);',
  12188. ' this.DoIt();',
  12189. ' this.DoIt();',
  12190. ' };',
  12191. ' this.DoSome = function () {',
  12192. ' };',
  12193. '});'
  12194. ]),
  12195. LinesToStr([ // this.$main
  12196. ''
  12197. ]));
  12198. end;
  12199. procedure TTestModule.TestClass_CallInherited_WithParams;
  12200. begin
  12201. StartProgram(false);
  12202. Add([
  12203. 'type',
  12204. ' TObject = class',
  12205. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12206. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12207. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12208. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12209. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12210. ' end;',
  12211. ' TClassA = class',
  12212. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12213. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12214. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12215. ' end;',
  12216. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12217. 'begin',
  12218. 'end;',
  12219. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12220. 'begin',
  12221. 'end;',
  12222. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12223. 'begin',
  12224. 'end;',
  12225. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12226. 'begin',
  12227. 'end;',
  12228. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12229. 'begin',
  12230. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12231. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12232. 'end;',
  12233. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12234. 'begin',
  12235. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12236. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12237. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12238. ' doit(pa,pb);',
  12239. ' doit(pa);',
  12240. ' doit2(pa);',
  12241. ' doit2;',
  12242. 'end;',
  12243. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12244. 'begin',
  12245. ' pa:=inherited;',
  12246. 'end;',
  12247. 'begin']);
  12248. ConvertProgram;
  12249. CheckSource('TestClass_CallInherited_WithParams',
  12250. LinesToStr([ // statements
  12251. 'rtl.createClass($mod,"TObject",null,function(){',
  12252. ' this.$init = function () {',
  12253. ' };',
  12254. ' this.$final = function () {',
  12255. ' };',
  12256. ' this.DoVirtual = function (pA,pB) {',
  12257. ' };',
  12258. ' this.DoIt = function (pA,pB) {',
  12259. ' };',
  12260. ' this.DoIt2 = function (pA,pB) {',
  12261. ' };',
  12262. ' this.GetIt = function (pA, pB) {',
  12263. ' var Result = 0;',
  12264. ' return Result;',
  12265. ' };',
  12266. '});',
  12267. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  12268. ' this.DoAbstract = function (pA,pB) {',
  12269. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12270. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12271. ' };',
  12272. ' this.DoVirtual = function (pA,pB) {',
  12273. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12274. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12275. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12276. ' this.DoIt(pA,pB);',
  12277. ' this.DoIt(pA,0);',
  12278. ' this.DoIt2(pA,2);',
  12279. ' this.DoIt2(1,2);',
  12280. ' };',
  12281. ' this.GetIt$1 = function (pA, pB) {',
  12282. ' var Result = 0;',
  12283. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12284. ' return Result;',
  12285. ' };',
  12286. '});'
  12287. ]),
  12288. LinesToStr([ // this.$main
  12289. ''
  12290. ]));
  12291. end;
  12292. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12293. begin
  12294. StartProgram(false);
  12295. Add('type');
  12296. Add(' TObject = class');
  12297. Add(' constructor Create; virtual;');
  12298. Add(' constructor CreateWithB(b: boolean);');
  12299. Add(' end;');
  12300. Add(' TA = class');
  12301. Add(' constructor Create; override;');
  12302. Add(' constructor CreateWithC(c: char);');
  12303. Add(' procedure DoIt;');
  12304. Add(' class function DoSome: TObject;');
  12305. Add(' end;');
  12306. Add('constructor tobject.create;');
  12307. Add('begin');
  12308. Add(' inherited; // call non existing ancestor -> ignore silently');
  12309. Add('end;');
  12310. Add('constructor tobject.createwithb(b: boolean);');
  12311. Add('begin');
  12312. Add(' inherited; // call non existing ancestor -> ignore silently');
  12313. Add(' create; // normal call');
  12314. Add('end;');
  12315. Add('constructor ta.create;');
  12316. Add('begin');
  12317. Add(' inherited; // normal call TObject.Create');
  12318. Add(' inherited create; // normal call TObject.Create');
  12319. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12320. Add('end;');
  12321. Add('constructor ta.createwithc(c: char);');
  12322. Add('begin');
  12323. Add(' inherited create; // call TObject.Create');
  12324. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12325. Add(' doit;');
  12326. Add(' doit();');
  12327. Add(' dosome;');
  12328. Add('end;');
  12329. Add('procedure ta.doit;');
  12330. Add('begin');
  12331. Add(' create; // normal call');
  12332. Add(' createwithb(false); // normal call');
  12333. Add(' createwithc(''c''); // normal call');
  12334. Add('end;');
  12335. Add('class function ta.dosome: TObject;');
  12336. Add('begin');
  12337. Add(' Result:=create; // constructor');
  12338. Add(' Result:=createwithb(true); // constructor');
  12339. Add(' Result:=createwithc(''c''); // constructor');
  12340. Add('end;');
  12341. Add('begin');
  12342. ConvertProgram;
  12343. CheckSource('TestClass_CallInheritedConstructor',
  12344. LinesToStr([ // statements
  12345. 'rtl.createClass($mod,"TObject",null,function(){',
  12346. ' this.$init = function () {',
  12347. ' };',
  12348. ' this.$final = function () {',
  12349. ' };',
  12350. ' this.Create = function () {',
  12351. ' return this;',
  12352. ' };',
  12353. ' this.CreateWithB = function (b) {',
  12354. ' this.Create();',
  12355. ' return this;',
  12356. ' };',
  12357. '});',
  12358. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12359. ' this.Create = function () {',
  12360. ' $mod.TObject.Create.call(this);',
  12361. ' $mod.TObject.Create.call(this);',
  12362. ' $mod.TObject.CreateWithB.call(this, false);',
  12363. ' return this;',
  12364. ' };',
  12365. ' this.CreateWithC = function (c) {',
  12366. ' $mod.TObject.Create.call(this);',
  12367. ' $mod.TObject.CreateWithB.call(this, true);',
  12368. ' this.DoIt();',
  12369. ' this.DoIt();',
  12370. ' this.$class.DoSome();',
  12371. ' return this;',
  12372. ' };',
  12373. ' this.DoIt = function () {',
  12374. ' this.Create();',
  12375. ' this.CreateWithB(false);',
  12376. ' this.CreateWithC("c");',
  12377. ' };',
  12378. ' this.DoSome = function () {',
  12379. ' var Result = null;',
  12380. ' Result = this.$create("Create");',
  12381. ' Result = this.$create("CreateWithB", [true]);',
  12382. ' Result = this.$create("CreateWithC", ["c"]);',
  12383. ' return Result;',
  12384. ' };',
  12385. '});'
  12386. ]),
  12387. LinesToStr([ // this.$main
  12388. ''
  12389. ]));
  12390. end;
  12391. procedure TTestModule.TestClass_ClassVar_Assign;
  12392. begin
  12393. StartProgram(false);
  12394. Add([
  12395. 'type',
  12396. ' TObject = class',
  12397. ' public',
  12398. ' class var vI: longint;',
  12399. ' class var Sub: TObject;',
  12400. ' constructor Create;',
  12401. ' class function GetIt(var Par: longint): tobject;',
  12402. ' end;',
  12403. 'constructor tobject.create;',
  12404. 'begin',
  12405. ' vi:=vi+1;',
  12406. ' Self.vi:=Self.vi+1;',
  12407. ' inc(vi);',
  12408. 'end;',
  12409. 'class function tobject.getit(var par: longint): tobject;',
  12410. 'begin',
  12411. ' vi:=vi+3;',
  12412. ' Self.vi:=Self.vi+4;',
  12413. ' inc(vi);',
  12414. ' Result:=self.sub;',
  12415. ' GetIt(vi);',
  12416. 'end;',
  12417. 'var Obj: tobject;',
  12418. 'begin',
  12419. ' obj:=tobject.create;',
  12420. ' tobject.vi:=3;',
  12421. ' if tobject.vi=4 then ;',
  12422. ' tobject.sub:=nil;',
  12423. ' obj.sub:=nil;',
  12424. ' obj.sub.sub:=nil;']);
  12425. ConvertProgram;
  12426. CheckSource('TestClass_ClassVar_Assign',
  12427. LinesToStr([ // statements
  12428. 'rtl.createClass($mod,"TObject",null,function(){',
  12429. ' this.vI = 0;',
  12430. ' this.Sub = null;',
  12431. ' this.$init = function () {',
  12432. ' };',
  12433. ' this.$final = function () {',
  12434. ' };',
  12435. ' this.Create = function(){',
  12436. ' $mod.TObject.vI = this.vI+1;',
  12437. ' $mod.TObject.vI = this.vI+1;',
  12438. ' $mod.TObject.vI += 1;',
  12439. ' return this;',
  12440. ' };',
  12441. ' this.GetIt = function(Par){',
  12442. ' var Result = null;',
  12443. ' $mod.TObject.vI = this.vI + 3;',
  12444. ' $mod.TObject.vI = this.vI + 4;',
  12445. ' $mod.TObject.vI += 1;',
  12446. ' Result = this.Sub;',
  12447. ' this.GetIt({',
  12448. ' p: $mod.TObject,',
  12449. ' get: function () {',
  12450. ' return this.p.vI;',
  12451. ' },',
  12452. ' set: function (v) {',
  12453. ' this.p.vI = v;',
  12454. ' }',
  12455. ' });',
  12456. ' return Result;',
  12457. ' };',
  12458. '});',
  12459. 'this.Obj = null;'
  12460. ]),
  12461. LinesToStr([ // $mod.$main
  12462. '$mod.Obj = $mod.TObject.$create("Create");',
  12463. '$mod.TObject.vI = 3;',
  12464. 'if ($mod.TObject.vI === 4);',
  12465. '$mod.TObject.Sub=null;',
  12466. '$mod.TObject.Sub=null;',
  12467. '$mod.TObject.Sub=null;',
  12468. '']));
  12469. end;
  12470. procedure TTestModule.TestClass_CallClassMethod;
  12471. begin
  12472. StartProgram(false);
  12473. Add('type');
  12474. Add(' TObject = class');
  12475. Add(' public');
  12476. Add(' class var vI: longint;');
  12477. Add(' class var Sub: TObject;');
  12478. Add(' constructor Create;');
  12479. Add(' function GetMore(Par: longint): longint;');
  12480. Add(' class function GetIt(Par: longint): tobject;');
  12481. Add(' end;');
  12482. Add('constructor tobject.create;');
  12483. Add('begin');
  12484. Add(' sub:=getit(3);');
  12485. Add(' vi:=getmore(4);');
  12486. Add(' sub:=Self.getit(5);');
  12487. Add(' vi:=Self.getmore(6);');
  12488. Add('end;');
  12489. Add('function tobject.getmore(par: longint): longint;');
  12490. Add('begin');
  12491. Add(' sub:=getit(11);');
  12492. Add(' vi:=getmore(12);');
  12493. Add(' sub:=self.getit(13);');
  12494. Add(' vi:=self.getmore(14);');
  12495. Add('end;');
  12496. Add('class function tobject.getit(par: longint): tobject;');
  12497. Add('begin');
  12498. Add(' sub:=getit(21);');
  12499. Add(' vi:=sub.getmore(22);');
  12500. Add(' sub:=self.getit(23);');
  12501. Add(' vi:=self.sub.getmore(24);');
  12502. Add('end;');
  12503. Add('var Obj: tobject;');
  12504. Add('begin');
  12505. Add(' obj:=tobject.create;');
  12506. Add(' tobject.getit(5);');
  12507. Add(' obj.getit(6);');
  12508. Add(' obj.sub.getit(7);');
  12509. Add(' obj.sub.getit(8).SUB:=nil;');
  12510. Add(' obj.sub.getit(9).GETIT(10);');
  12511. Add(' obj.sub.getit(11).SuB.getit(12);');
  12512. ConvertProgram;
  12513. CheckSource('TestClass_CallClassMethod',
  12514. LinesToStr([ // statements
  12515. 'rtl.createClass($mod,"TObject",null,function(){',
  12516. ' this.vI = 0;',
  12517. ' this.Sub = null;',
  12518. ' this.$init = function () {',
  12519. ' };',
  12520. ' this.$final = function () {',
  12521. ' };',
  12522. ' this.Create = function(){',
  12523. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12524. ' $mod.TObject.vI = this.GetMore(4);',
  12525. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12526. ' $mod.TObject.vI = this.GetMore(6);',
  12527. ' return this;',
  12528. ' };',
  12529. ' this.GetMore = function(Par){',
  12530. ' var Result = 0;',
  12531. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12532. ' $mod.TObject.vI = this.GetMore(12);',
  12533. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12534. ' $mod.TObject.vI = this.GetMore(14);',
  12535. ' return Result;',
  12536. ' };',
  12537. ' this.GetIt = function(Par){',
  12538. ' var Result = null;',
  12539. ' $mod.TObject.Sub = this.GetIt(21);',
  12540. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12541. ' $mod.TObject.Sub = this.GetIt(23);',
  12542. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12543. ' return Result;',
  12544. ' };',
  12545. '});',
  12546. 'this.Obj = null;'
  12547. ]),
  12548. LinesToStr([ // $mod.$main
  12549. '$mod.Obj = $mod.TObject.$create("Create");',
  12550. '$mod.TObject.GetIt(5);',
  12551. '$mod.Obj.$class.GetIt(6);',
  12552. '$mod.Obj.Sub.$class.GetIt(7);',
  12553. '$mod.TObject.Sub=null;',
  12554. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12555. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12556. '']));
  12557. end;
  12558. procedure TTestModule.TestClass_Property;
  12559. begin
  12560. StartProgram(false);
  12561. Add('type');
  12562. Add(' TObject = class');
  12563. Add(' Fx: longint;');
  12564. Add(' Fy: longint;');
  12565. Add(' function GetInt: longint;');
  12566. Add(' procedure SetInt(Value: longint);');
  12567. Add(' procedure DoIt;');
  12568. Add(' property IntA: longint read Fx write Fy;');
  12569. Add(' property IntB: longint read GetInt write SetInt;');
  12570. Add(' end;');
  12571. Add('function tobject.getint: longint;');
  12572. Add('begin');
  12573. Add(' result:=fx;');
  12574. Add('end;');
  12575. Add('procedure tobject.setint(value: longint);');
  12576. Add('begin');
  12577. Add(' if value=fy then exit;');
  12578. Add(' fy:=value;');
  12579. Add('end;');
  12580. Add('procedure tobject.doit;');
  12581. Add('begin');
  12582. Add(' IntA:=IntA+1;');
  12583. Add(' Self.IntA:=Self.IntA+1;');
  12584. Add(' IntB:=IntB+1;');
  12585. Add(' Self.IntB:=Self.IntB+1;');
  12586. Add('end;');
  12587. Add('var Obj: tobject;');
  12588. Add('begin');
  12589. Add(' obj.inta:=obj.inta+1;');
  12590. Add(' if obj.intb=2 then;');
  12591. Add(' obj.intb:=obj.intb+2;');
  12592. Add(' obj.setint(obj.inta);');
  12593. ConvertProgram;
  12594. CheckSource('TestClass_Property',
  12595. LinesToStr([ // statements
  12596. 'rtl.createClass($mod, "TObject", null, function () {',
  12597. ' this.$init = function () {',
  12598. ' this.Fx = 0;',
  12599. ' this.Fy = 0;',
  12600. ' };',
  12601. ' this.$final = function () {',
  12602. ' };',
  12603. ' this.GetInt = function () {',
  12604. ' var Result = 0;',
  12605. ' Result = this.Fx;',
  12606. ' return Result;',
  12607. ' };',
  12608. ' this.SetInt = function (Value) {',
  12609. ' if (Value === this.Fy) return;',
  12610. ' this.Fy = Value;',
  12611. ' };',
  12612. ' this.DoIt = function () {',
  12613. ' this.Fy = this.Fx + 1;',
  12614. ' this.Fy = this.Fx + 1;',
  12615. ' this.SetInt(this.GetInt() + 1);',
  12616. ' this.SetInt(this.GetInt() + 1);',
  12617. ' };',
  12618. '});',
  12619. 'this.Obj = null;'
  12620. ]),
  12621. LinesToStr([ // $mod.$main
  12622. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12623. 'if ($mod.Obj.GetInt() === 2);',
  12624. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12625. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12626. ]));
  12627. end;
  12628. procedure TTestModule.TestClass_Property_ClassMethod;
  12629. begin
  12630. StartProgram(false);
  12631. Add([
  12632. 'type',
  12633. ' TObject = class',
  12634. ' class var Fx: longint;',
  12635. ' class var Fy: longint;',
  12636. ' class function GetInt: longint;',
  12637. ' class procedure SetInt(Value: longint);',
  12638. ' end;',
  12639. ' TBird = class',
  12640. ' class procedure DoIt;',
  12641. ' class property IntA: longint read Fx write Fy;',
  12642. ' class property IntB: longint read GetInt write SetInt;',
  12643. ' end;',
  12644. 'class function tobject.getint: longint;',
  12645. 'begin',
  12646. ' result:=fx;',
  12647. 'end;',
  12648. 'class procedure tobject.setint(value: longint);',
  12649. 'begin',
  12650. 'end;',
  12651. 'class procedure tbird.doit;',
  12652. 'begin',
  12653. ' FX:=3;',
  12654. ' IntA:=IntA+1;',
  12655. ' Self.IntA:=Self.IntA+1;',
  12656. ' IntB:=IntB+1;',
  12657. ' Self.IntB:=Self.IntB+1;',
  12658. ' with Self do begin',
  12659. ' FX:=11;',
  12660. ' IntA:=IntA+12;',
  12661. ' IntB:=IntB+13;',
  12662. ' end;',
  12663. 'end;',
  12664. 'var Obj: tbird;',
  12665. 'begin',
  12666. ' tbird.fx:=tbird.fx+1;',
  12667. ' tbird.inta:=tbird.inta+1;',
  12668. ' if tbird.intb=2 then;',
  12669. ' tbird.intb:=tbird.intb+2;',
  12670. ' tbird.setint(tbird.inta);',
  12671. ' obj.inta:=obj.inta+1;',
  12672. ' if obj.intb=2 then;',
  12673. ' obj.intb:=obj.intb+2;',
  12674. ' obj.setint(obj.inta);',
  12675. ' with Tbird do begin',
  12676. ' FX:=FY+1;',
  12677. ' inta:=inta+2;',
  12678. ' intb:=intb+3;',
  12679. ' end;',
  12680. ' with Obj do begin',
  12681. ' FX:=FY+1;',
  12682. ' inta:=inta+2;',
  12683. ' intb:=intb+3;',
  12684. ' end;',
  12685. '']);
  12686. ConvertProgram;
  12687. CheckSource('TestClass_Property_ClassMethod',
  12688. LinesToStr([ // statements
  12689. 'rtl.createClass($mod, "TObject", null, function () {',
  12690. ' this.Fx = 0;',
  12691. ' this.Fy = 0;',
  12692. ' this.$init = function () {',
  12693. ' };',
  12694. ' this.$final = function () {',
  12695. ' };',
  12696. ' this.GetInt = function () {',
  12697. ' var Result = 0;',
  12698. ' Result = this.Fx;',
  12699. ' return Result;',
  12700. ' };',
  12701. ' this.SetInt = function (Value) {',
  12702. ' };',
  12703. '});',
  12704. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12705. ' this.DoIt = function () {',
  12706. ' $mod.TObject.Fx = 3;',
  12707. ' $mod.TObject.Fy = this.Fx + 1;',
  12708. ' $mod.TObject.Fy = this.Fx + 1;',
  12709. ' this.SetInt(this.GetInt() + 1);',
  12710. ' this.SetInt(this.GetInt() + 1);',
  12711. ' $mod.TObject.Fx = 11;',
  12712. ' $mod.TObject.Fy = this.Fx + 12;',
  12713. ' this.SetInt(this.GetInt() + 13);',
  12714. ' };',
  12715. '});',
  12716. 'this.Obj = null;'
  12717. ]),
  12718. LinesToStr([ // $mod.$main
  12719. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12720. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12721. 'if ($mod.TBird.GetInt() === 2);',
  12722. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12723. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12724. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12725. 'if ($mod.Obj.$class.GetInt() === 2);',
  12726. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12727. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12728. 'var $with1 = $mod.TBird;',
  12729. '$mod.TObject.Fx = $with1.Fy + 1;',
  12730. '$mod.TObject.Fy = $with1.Fx + 2;',
  12731. '$with1.SetInt($with1.GetInt() + 3);',
  12732. 'var $with2 = $mod.Obj;',
  12733. '$mod.TObject.Fx = $with2.Fy + 1;',
  12734. '$mod.TObject.Fy = $with2.Fx + 2;',
  12735. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12736. '']));
  12737. end;
  12738. procedure TTestModule.TestClass_Property_Indexed;
  12739. begin
  12740. StartProgram(false);
  12741. Add('type');
  12742. Add(' TObject = class');
  12743. Add(' FItems: array of longint;');
  12744. Add(' function GetItems(Index: longint): longint;');
  12745. Add(' procedure SetItems(Index: longint; Value: longint);');
  12746. Add(' procedure DoIt;');
  12747. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12748. Add(' end;');
  12749. Add('function tobject.getitems(index: longint): longint;');
  12750. Add('begin');
  12751. Add(' Result:=fitems[index];');
  12752. Add('end;');
  12753. Add('procedure tobject.setitems(index: longint; value: longint);');
  12754. Add('begin');
  12755. Add(' fitems[index]:=value;');
  12756. Add('end;');
  12757. Add('procedure tobject.doit;');
  12758. Add('begin');
  12759. Add(' items[1]:=2;');
  12760. Add(' items[3]:=items[4];');
  12761. Add(' self.items[5]:=self.items[6];');
  12762. Add(' items[items[7]]:=items[items[8]];');
  12763. Add('end;');
  12764. Add('var Obj: tobject;');
  12765. Add('begin');
  12766. Add(' obj.Items[11]:=obj.Items[12];');
  12767. ConvertProgram;
  12768. CheckSource('TestClass_Property_Indexed',
  12769. LinesToStr([ // statements
  12770. 'rtl.createClass($mod, "TObject", null, function () {',
  12771. ' this.$init = function () {',
  12772. ' this.FItems = [];',
  12773. ' };',
  12774. ' this.$final = function () {',
  12775. ' this.FItems = undefined;',
  12776. ' };',
  12777. ' this.GetItems = function (Index) {',
  12778. ' var Result = 0;',
  12779. ' Result = this.FItems[Index];',
  12780. ' return Result;',
  12781. ' };',
  12782. ' this.SetItems = function (Index, Value) {',
  12783. ' this.FItems[Index] = Value;',
  12784. ' };',
  12785. ' this.DoIt = function () {',
  12786. ' this.SetItems(1, 2);',
  12787. ' this.SetItems(3,this.GetItems(4));',
  12788. ' this.SetItems(5,this.GetItems(6));',
  12789. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12790. ' };',
  12791. '});',
  12792. 'this.Obj = null;'
  12793. ]),
  12794. LinesToStr([ // $mod.$main
  12795. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12796. ]));
  12797. end;
  12798. procedure TTestModule.TestClass_Property_IndexSpec;
  12799. begin
  12800. StartProgram(false);
  12801. Add([
  12802. 'type',
  12803. ' TEnum = (red, blue);',
  12804. ' TObject = class',
  12805. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12806. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12807. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12808. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12809. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12810. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12811. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12812. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12813. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12814. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12815. ' end;',
  12816. 'procedure DoIt(b: boolean); begin end;',
  12817. 'var',
  12818. ' o: TObject;',
  12819. 'begin',
  12820. ' o.B1:=o.B1;',
  12821. ' o.B2:=o.B2;',
  12822. ' o.B3:=o.B3;',
  12823. ' o.I1[''a'']:=o.I1[''b''];',
  12824. ' doit(o.b1);',
  12825. ' doit(o.b2);',
  12826. ' doit(o.i1[''c'']);',
  12827. '']);
  12828. ConvertProgram;
  12829. CheckSource('TestClass_Property_IndexSpec',
  12830. LinesToStr([ // statements
  12831. 'this.TEnum = {',
  12832. ' "0": "red",',
  12833. ' red: 0,',
  12834. ' "1": "blue",',
  12835. ' blue: 1',
  12836. '};',
  12837. 'rtl.createClass($mod, "TObject", null, function () {',
  12838. ' this.$init = function () {',
  12839. ' };',
  12840. ' this.$final = function () {',
  12841. ' };',
  12842. '});',
  12843. 'this.DoIt = function (b) {',
  12844. '};',
  12845. 'this.o = null;',
  12846. '']),
  12847. LinesToStr([ // $mod.$main
  12848. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12849. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12850. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12851. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12852. '$mod.DoIt($mod.o.GetIntBool(1));',
  12853. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12854. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12855. '']));
  12856. end;
  12857. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12858. begin
  12859. StartProgram(false);
  12860. Add('type');
  12861. Add(' TArray = array of longint;');
  12862. Add(' TObject = class');
  12863. Add(' FItems: TArray;');
  12864. Add(' function GetItems: tarray;');
  12865. Add(' procedure SetItems(Value: tarray);');
  12866. Add(' property Items: tarray read getitems write setitems;');
  12867. Add(' procedure SetNumbers(const Value: tarray);');
  12868. Add(' property Numbers: tarray write setnumbers;');
  12869. Add(' end;');
  12870. Add('function tobject.getitems: tarray;');
  12871. Add('begin');
  12872. Add(' Result:=fitems;');
  12873. Add('end;');
  12874. Add('procedure tobject.setitems(value: tarray);');
  12875. Add('begin');
  12876. Add(' fitems:=value;');
  12877. Add(' fitems:=nil;');
  12878. Add(' Items:=nil;');
  12879. Add(' Items:=Items;');
  12880. Add(' Items[1]:=2;');
  12881. Add(' fitems[3]:=Items[4];');
  12882. Add(' Items[5]:=Items[6];');
  12883. Add(' Self.Items[7]:=8;');
  12884. Add(' Self.Items[9]:=Self.Items[10];');
  12885. Add(' Items[Items[11]]:=Items[Items[12]];');
  12886. Add('end;');
  12887. Add('procedure tobject.SetNumbers(const Value: tarray);');
  12888. Add('begin;');
  12889. Add(' Numbers:=nil;');
  12890. Add(' Numbers:=Value;');
  12891. Add(' Self.Numbers:=Value;');
  12892. Add('end;');
  12893. Add('var Obj: tobject;');
  12894. Add('begin');
  12895. Add(' obj.items:=nil;');
  12896. Add(' obj.items:=obj.items;');
  12897. Add(' obj.items[11]:=obj.items[12];');
  12898. ConvertProgram;
  12899. CheckSource('TestClass_PropertyOfTypeArray',
  12900. LinesToStr([ // statements
  12901. 'rtl.createClass($mod, "TObject", null, function () {',
  12902. ' this.$init = function () {',
  12903. ' this.FItems = [];',
  12904. ' };',
  12905. ' this.$final = function () {',
  12906. ' this.FItems = undefined;',
  12907. ' };',
  12908. ' this.GetItems = function () {',
  12909. ' var Result = [];',
  12910. ' Result = rtl.arrayRef(this.FItems);',
  12911. ' return Result;',
  12912. ' };',
  12913. ' this.SetItems = function (Value) {',
  12914. ' this.FItems = rtl.arrayRef(Value);',
  12915. ' this.FItems = [];',
  12916. ' this.SetItems([]);',
  12917. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  12918. ' this.GetItems()[1] = 2;',
  12919. ' this.FItems[3] = this.GetItems()[4];',
  12920. ' this.GetItems()[5] = this.GetItems()[6];',
  12921. ' this.GetItems()[7] = 8;',
  12922. ' this.GetItems()[9] = this.GetItems()[10];',
  12923. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12924. ' };',
  12925. ' this.SetNumbers = function (Value) {',
  12926. ' this.SetNumbers([]);',
  12927. ' this.SetNumbers(Value);',
  12928. ' this.SetNumbers(Value);',
  12929. ' };',
  12930. '});',
  12931. 'this.Obj = null;'
  12932. ]),
  12933. LinesToStr([ // $mod.$main
  12934. '$mod.Obj.SetItems([]);',
  12935. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12936. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12937. ]));
  12938. end;
  12939. procedure TTestModule.TestClass_PropertyDefault;
  12940. begin
  12941. StartProgram(false);
  12942. Add([
  12943. 'type',
  12944. ' TArray = array of longint;',
  12945. ' TObject = class',
  12946. ' FItems: TArray;',
  12947. ' function GetItems(Index: longint): longint;',
  12948. ' procedure SetItems(Index, Value: longint);',
  12949. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12950. ' end;',
  12951. 'function tobject.getitems(index: longint): longint;',
  12952. 'begin',
  12953. 'end;',
  12954. 'procedure tobject.setitems(index, value: longint);',
  12955. 'begin',
  12956. ' Self[1]:=2;',
  12957. ' Self[3]:=Self[index];',
  12958. ' Self[index]:=Self[Self[value]];',
  12959. ' Self[Self[4]]:=value;',
  12960. 'end;',
  12961. 'var Obj: tobject;',
  12962. 'begin',
  12963. ' obj[11]:=12;',
  12964. ' obj[13]:=obj[14];',
  12965. ' obj[obj[15]]:=obj[obj[15]];',
  12966. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12967. ConvertProgram;
  12968. CheckSource('TestClass_PropertyDefault',
  12969. LinesToStr([ // statements
  12970. 'rtl.createClass($mod, "TObject", null, function () {',
  12971. ' this.$init = function () {',
  12972. ' this.FItems = [];',
  12973. ' };',
  12974. ' this.$final = function () {',
  12975. ' this.FItems = undefined;',
  12976. ' };',
  12977. ' this.GetItems = function (Index) {',
  12978. ' var Result = 0;',
  12979. ' return Result;',
  12980. ' };',
  12981. ' this.SetItems = function (Index, Value) {',
  12982. ' this.SetItems(1, 2);',
  12983. ' this.SetItems(3, this.GetItems(Index));',
  12984. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12985. ' this.SetItems(this.GetItems(4), Value);',
  12986. ' };',
  12987. '});',
  12988. 'this.Obj = null;'
  12989. ]),
  12990. LinesToStr([ // $mod.$main
  12991. '$mod.Obj.SetItems(11, 12);',
  12992. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12993. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12994. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12995. '']));
  12996. end;
  12997. procedure TTestModule.TestClass_PropertyDefault2;
  12998. begin
  12999. StartProgram(false);
  13000. Add([
  13001. 'type',
  13002. ' TObject = class end;',
  13003. ' TAlphaList = class',
  13004. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13005. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13006. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13007. ' end;',
  13008. ' TBetaList = class',
  13009. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13010. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13011. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13012. ' end;',
  13013. ' TBird = class',
  13014. ' procedure DoIt;',
  13015. ' end;',
  13016. 'procedure TBird.DoIt;',
  13017. 'var',
  13018. ' List: TAlphaList;',
  13019. 'begin',
  13020. ' if TBetaList(List[true])[3]=nil then ;',
  13021. ' TBetaList(List[false])[5]:=nil;',
  13022. 'end;',
  13023. 'var',
  13024. ' List: TAlphaList;',
  13025. 'begin',
  13026. ' if TBetaList(List[true])[3]=nil then ;',
  13027. ' TBetaList(List[false])[5]:=nil;',
  13028. '']);
  13029. ConvertProgram;
  13030. CheckSource('TestClass_PropertyDefault2',
  13031. LinesToStr([ // statements
  13032. 'rtl.createClass($mod, "TObject", null, function () {',
  13033. ' this.$init = function () {',
  13034. ' };',
  13035. ' this.$final = function () {',
  13036. ' };',
  13037. '});',
  13038. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  13039. '});',
  13040. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  13041. '});',
  13042. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13043. ' this.DoIt = function () {',
  13044. ' var List = null;',
  13045. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13046. ' List.GetAlphas(false).SetBetas(5, null);',
  13047. ' };',
  13048. '});',
  13049. 'this.List = null;',
  13050. '']),
  13051. LinesToStr([ // $mod.$main
  13052. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13053. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13054. '']));
  13055. end;
  13056. procedure TTestModule.TestClass_PropertyOverride;
  13057. begin
  13058. StartProgram(false);
  13059. Add('type');
  13060. Add(' integer = longint;');
  13061. Add(' TObject = class');
  13062. Add(' FItem: integer;');
  13063. Add(' function GetItem: integer; external name ''GetItem'';');
  13064. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13065. Add(' property Item: integer read getitem write setitem;');
  13066. Add(' end;');
  13067. Add(' TCar = class');
  13068. Add(' FBag: integer;');
  13069. Add(' function GetBag: integer; external name ''GetBag'';');
  13070. Add(' property Item read getbag;');
  13071. Add(' end;');
  13072. Add('var');
  13073. Add(' Obj: tobject;');
  13074. Add(' Car: tcar;');
  13075. Add('begin');
  13076. Add(' Obj.Item:=Obj.Item;');
  13077. Add(' Car.Item:=Car.Item;');
  13078. ConvertProgram;
  13079. CheckSource('TestClass_PropertyOverride',
  13080. LinesToStr([ // statements
  13081. 'rtl.createClass($mod, "TObject", null, function () {',
  13082. ' this.$init = function () {',
  13083. ' this.FItem = 0;',
  13084. ' };',
  13085. ' this.$final = function () {',
  13086. ' };',
  13087. '});',
  13088. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13089. ' this.$init = function () {',
  13090. ' $mod.TObject.$init.call(this);',
  13091. ' this.FBag = 0;',
  13092. ' };',
  13093. '});',
  13094. 'this.Obj = null;',
  13095. 'this.Car = null;',
  13096. '']),
  13097. LinesToStr([ // $mod.$main
  13098. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13099. '$mod.Car.SetItem($mod.Car.GetBag());',
  13100. '']));
  13101. end;
  13102. procedure TTestModule.TestClass_PropertyIncVisibility;
  13103. begin
  13104. AddModuleWithIntfImplSrc('unit1.pp',
  13105. LinesToStr([
  13106. 'type',
  13107. ' TNumber = longint;',
  13108. ' TInteger = longint;',
  13109. ' TObject = class',
  13110. ' private',
  13111. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13112. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13113. ' protected',
  13114. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13115. ' end;']),
  13116. LinesToStr([
  13117. '']));
  13118. StartProgram(true);
  13119. Add([
  13120. 'uses unit1;',
  13121. 'type',
  13122. ' TBird = class',
  13123. ' public',
  13124. ' property Items;',
  13125. ' end;',
  13126. 'procedure DoIt(i: TInteger);',
  13127. 'begin',
  13128. 'end;',
  13129. 'var b: TBird;',
  13130. 'begin',
  13131. ' b.Items[1]:=2;',
  13132. ' b.Items[3]:=b.Items[4];',
  13133. ' DoIt(b.Items[5]);',
  13134. '']);
  13135. ConvertProgram;
  13136. CheckSource('TestClass_PropertyIncVisibility',
  13137. LinesToStr([ // statements
  13138. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  13139. '});',
  13140. 'this.DoIt = function (i) {',
  13141. '};',
  13142. 'this.b = null;'
  13143. ]),
  13144. LinesToStr([ // $mod.$main
  13145. '$mod.b.SetItems(1, 2);',
  13146. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13147. '$mod.DoIt($mod.b.GetItems(5));'
  13148. ]));
  13149. end;
  13150. procedure TTestModule.TestClass_Assigned;
  13151. begin
  13152. StartProgram(false);
  13153. Add('type');
  13154. Add(' TObject = class');
  13155. Add(' end;');
  13156. Add('var');
  13157. Add(' Obj: tobject;');
  13158. Add(' b: boolean;');
  13159. Add('begin');
  13160. Add(' if Assigned(obj) then ;');
  13161. Add(' b:=Assigned(obj) or false;');
  13162. ConvertProgram;
  13163. CheckSource('TestClass_Assigned',
  13164. LinesToStr([ // statements
  13165. 'rtl.createClass($mod, "TObject", null, function () {',
  13166. ' this.$init = function () {',
  13167. ' };',
  13168. ' this.$final = function () {',
  13169. ' };',
  13170. '});',
  13171. 'this.Obj = null;',
  13172. 'this.b = false;'
  13173. ]),
  13174. LinesToStr([ // $mod.$main
  13175. 'if ($mod.Obj != null);',
  13176. '$mod.b = ($mod.Obj != null) || false;'
  13177. ]));
  13178. end;
  13179. procedure TTestModule.TestClass_WithClassDoCreate;
  13180. begin
  13181. StartProgram(false);
  13182. Add('type');
  13183. Add(' TObject = class');
  13184. Add(' aBool: boolean;');
  13185. Add(' Arr: array of boolean;');
  13186. Add(' constructor Create;');
  13187. Add(' end;');
  13188. Add('constructor TObject.Create; begin end;');
  13189. Add('var');
  13190. Add(' Obj: tobject;');
  13191. Add(' b: boolean;');
  13192. Add('begin');
  13193. Add(' with tobject.create do begin');
  13194. Add(' b:=abool;');
  13195. Add(' abool:=b;');
  13196. Add(' b:=arr[1];');
  13197. Add(' arr[2]:=b;');
  13198. Add(' end;');
  13199. Add(' with tobject do');
  13200. Add(' obj:=create;');
  13201. Add(' with obj do begin');
  13202. Add(' create;');
  13203. Add(' b:=abool;');
  13204. Add(' abool:=b;');
  13205. Add(' b:=arr[3];');
  13206. Add(' arr[4]:=b;');
  13207. Add(' end;');
  13208. ConvertProgram;
  13209. CheckSource('TestClass_WithClassDoCreate',
  13210. LinesToStr([ // statements
  13211. 'rtl.createClass($mod, "TObject", null, function () {',
  13212. ' this.$init = function () {',
  13213. ' this.aBool = false;',
  13214. ' this.Arr = [];',
  13215. ' };',
  13216. ' this.$final = function () {',
  13217. ' this.Arr = undefined;',
  13218. ' };',
  13219. ' this.Create = function () {',
  13220. ' return this;',
  13221. ' };',
  13222. '});',
  13223. 'this.Obj = null;',
  13224. 'this.b = false;'
  13225. ]),
  13226. LinesToStr([ // $mod.$main
  13227. 'var $with1 = $mod.TObject.$create("Create");',
  13228. '$mod.b = $with1.aBool;',
  13229. '$with1.aBool = $mod.b;',
  13230. '$mod.b = $with1.Arr[1];',
  13231. '$with1.Arr[2] = $mod.b;',
  13232. 'var $with2 = $mod.TObject;',
  13233. '$mod.Obj = $with2.$create("Create");',
  13234. 'var $with3 = $mod.Obj;',
  13235. '$with3.Create();',
  13236. '$mod.b = $with3.aBool;',
  13237. '$with3.aBool = $mod.b;',
  13238. '$mod.b = $with3.Arr[3];',
  13239. '$with3.Arr[4] = $mod.b;',
  13240. '']));
  13241. end;
  13242. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13243. begin
  13244. StartProgram(false);
  13245. Add('type');
  13246. Add(' TObject = class');
  13247. Add(' FInt: longint;');
  13248. Add(' constructor Create;');
  13249. Add(' function GetSize: longint;');
  13250. Add(' procedure SetSize(Value: longint);');
  13251. Add(' property Int: longint read FInt write FInt;');
  13252. Add(' property Size: longint read GetSize write SetSize;');
  13253. Add(' end;');
  13254. Add('constructor TObject.Create; begin end;');
  13255. Add('function TObject.GetSize: longint; begin; end;');
  13256. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13257. Add('var');
  13258. Add(' Obj: tobject;');
  13259. Add(' i: longint;');
  13260. Add('begin');
  13261. Add(' with TObject.Create do begin');
  13262. Add(' i:=int;');
  13263. Add(' int:=i;');
  13264. Add(' i:=size;');
  13265. Add(' size:=i;');
  13266. Add(' end;');
  13267. Add(' with obj do begin');
  13268. Add(' i:=int;');
  13269. Add(' int:=i;');
  13270. Add(' i:=size;');
  13271. Add(' size:=i;');
  13272. Add(' end;');
  13273. ConvertProgram;
  13274. CheckSource('TestClass_WithClassInstDoProperty',
  13275. LinesToStr([ // statements
  13276. 'rtl.createClass($mod, "TObject", null, function () {',
  13277. ' this.$init = function () {',
  13278. ' this.FInt = 0;',
  13279. ' };',
  13280. ' this.$final = function () {',
  13281. ' };',
  13282. ' this.Create = function () {',
  13283. ' return this;',
  13284. ' };',
  13285. ' this.GetSize = function () {',
  13286. ' var Result = 0;',
  13287. ' return Result;',
  13288. ' };',
  13289. ' this.SetSize = function (Value) {',
  13290. ' };',
  13291. '});',
  13292. 'this.Obj = null;',
  13293. 'this.i = 0;'
  13294. ]),
  13295. LinesToStr([ // $mod.$main
  13296. 'var $with1 = $mod.TObject.$create("Create");',
  13297. '$mod.i = $with1.FInt;',
  13298. '$with1.FInt = $mod.i;',
  13299. '$mod.i = $with1.GetSize();',
  13300. '$with1.SetSize($mod.i);',
  13301. 'var $with2 = $mod.Obj;',
  13302. '$mod.i = $with2.FInt;',
  13303. '$with2.FInt = $mod.i;',
  13304. '$mod.i = $with2.GetSize();',
  13305. '$with2.SetSize($mod.i);',
  13306. '']));
  13307. end;
  13308. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13309. begin
  13310. StartProgram(false);
  13311. Add('type');
  13312. Add(' TObject = class');
  13313. Add(' constructor Create;');
  13314. Add(' function GetItems(Index: longint): longint;');
  13315. Add(' procedure SetItems(Index, Value: longint);');
  13316. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13317. Add(' end;');
  13318. Add('constructor TObject.Create; begin end;');
  13319. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13320. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13321. Add('var');
  13322. Add(' Obj: tobject;');
  13323. Add(' i: longint;');
  13324. Add('begin');
  13325. Add(' with TObject.Create do begin');
  13326. Add(' i:=Items[1];');
  13327. Add(' Items[2]:=i;');
  13328. Add(' end;');
  13329. Add(' with obj do begin');
  13330. Add(' i:=Items[3];');
  13331. Add(' Items[4]:=i;');
  13332. Add(' end;');
  13333. ConvertProgram;
  13334. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13335. LinesToStr([ // statements
  13336. 'rtl.createClass($mod, "TObject", null, function () {',
  13337. ' this.$init = function () {',
  13338. ' };',
  13339. ' this.$final = function () {',
  13340. ' };',
  13341. ' this.Create = function () {',
  13342. ' return this;',
  13343. ' };',
  13344. ' this.GetItems = function (Index) {',
  13345. ' var Result = 0;',
  13346. ' return Result;',
  13347. ' };',
  13348. ' this.SetItems = function (Index, Value) {',
  13349. ' };',
  13350. '});',
  13351. 'this.Obj = null;',
  13352. 'this.i = 0;'
  13353. ]),
  13354. LinesToStr([ // $mod.$main
  13355. 'var $with1 = $mod.TObject.$create("Create");',
  13356. '$mod.i = $with1.GetItems(1);',
  13357. '$with1.SetItems(2, $mod.i);',
  13358. 'var $with2 = $mod.Obj;',
  13359. '$mod.i = $with2.GetItems(3);',
  13360. '$with2.SetItems(4, $mod.i);',
  13361. '']));
  13362. end;
  13363. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13364. begin
  13365. StartProgram(false);
  13366. Add('type');
  13367. Add(' TObject = class');
  13368. Add(' constructor Create;');
  13369. Add(' function GetSize: longint;');
  13370. Add(' procedure SetSize(Value: longint);');
  13371. Add(' end;');
  13372. Add('constructor TObject.Create; begin end;');
  13373. Add('function TObject.GetSize: longint; begin; end;');
  13374. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13375. Add('var');
  13376. Add(' Obj: tobject;');
  13377. Add(' i: longint;');
  13378. Add('begin');
  13379. Add(' with TObject.Create do begin');
  13380. Add(' i:=GetSize;');
  13381. Add(' i:=GetSize();');
  13382. Add(' SetSize(i);');
  13383. Add(' end;');
  13384. Add(' with obj do begin');
  13385. Add(' i:=GetSize;');
  13386. Add(' i:=GetSize();');
  13387. Add(' SetSize(i);');
  13388. Add(' end;');
  13389. ConvertProgram;
  13390. CheckSource('TestClass_WithClassInstDoFunc',
  13391. LinesToStr([ // statements
  13392. 'rtl.createClass($mod, "TObject", null, function () {',
  13393. ' this.$init = function () {',
  13394. ' };',
  13395. ' this.$final = function () {',
  13396. ' };',
  13397. ' this.Create = function () {',
  13398. ' return this;',
  13399. ' };',
  13400. ' this.GetSize = function () {',
  13401. ' var Result = 0;',
  13402. ' return Result;',
  13403. ' };',
  13404. ' this.SetSize = function (Value) {',
  13405. ' };',
  13406. '});',
  13407. 'this.Obj = null;',
  13408. 'this.i = 0;'
  13409. ]),
  13410. LinesToStr([ // $mod.$main
  13411. 'var $with1 = $mod.TObject.$create("Create");',
  13412. '$mod.i = $with1.GetSize();',
  13413. '$mod.i = $with1.GetSize();',
  13414. '$with1.SetSize($mod.i);',
  13415. 'var $with2 = $mod.Obj;',
  13416. '$mod.i = $with2.GetSize();',
  13417. '$mod.i = $with2.GetSize();',
  13418. '$with2.SetSize($mod.i);',
  13419. '']));
  13420. end;
  13421. procedure TTestModule.TestClass_TypeCast;
  13422. begin
  13423. StartProgram(false);
  13424. Add('type');
  13425. Add(' TObject = class');
  13426. Add(' Next: TObject;');
  13427. Add(' constructor Create;');
  13428. Add(' end;');
  13429. Add(' TControl = class(TObject)');
  13430. Add(' Arr: array of TObject;');
  13431. Add(' function GetIt(vI: longint = 0): TObject;');
  13432. Add(' end;');
  13433. Add('constructor tobject.create; begin end;');
  13434. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13435. Add('var');
  13436. Add(' Obj: tobject;');
  13437. Add('begin');
  13438. Add(' obj:=tcontrol(obj).next;');
  13439. Add(' tcontrol(obj):=nil;');
  13440. Add(' obj:=tcontrol(obj);');
  13441. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13442. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13443. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13444. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13445. Add(' obj:=tcontrol(nil);');
  13446. ConvertProgram;
  13447. CheckSource('TestClass_TypeCast',
  13448. LinesToStr([ // statements
  13449. 'rtl.createClass($mod, "TObject", null, function () {',
  13450. ' this.$init = function () {',
  13451. ' this.Next = null;',
  13452. ' };',
  13453. ' this.$final = function () {',
  13454. ' this.Next = undefined;',
  13455. ' };',
  13456. ' this.Create = function () {',
  13457. ' return this;',
  13458. ' };',
  13459. '});',
  13460. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  13461. ' this.$init = function () {',
  13462. ' $mod.TObject.$init.call(this);',
  13463. ' this.Arr = [];',
  13464. ' };',
  13465. ' this.$final = function () {',
  13466. ' this.Arr = undefined;',
  13467. ' $mod.TObject.$final.call(this);',
  13468. ' };',
  13469. ' this.GetIt = function (vI) {',
  13470. ' var Result = null;',
  13471. ' return Result;',
  13472. ' };',
  13473. '});',
  13474. 'this.Obj = null;'
  13475. ]),
  13476. LinesToStr([ // $mod.$main
  13477. '$mod.Obj = $mod.Obj.Next;',
  13478. '$mod.Obj = null;',
  13479. '$mod.Obj = $mod.Obj;',
  13480. '$mod.Obj = $mod.Obj.GetIt(0);',
  13481. '$mod.Obj = $mod.Obj.GetIt(0);',
  13482. '$mod.Obj = $mod.Obj.GetIt(1);',
  13483. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13484. '$mod.Obj = null;',
  13485. '']));
  13486. end;
  13487. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13488. begin
  13489. StartProgram(false);
  13490. Add('type');
  13491. Add(' TObject = class end;');
  13492. Add('procedure ProcA(var A);');
  13493. Add('begin');
  13494. Add(' TObject(A):=nil;');
  13495. Add(' TObject(A):=TObject(A);');
  13496. Add(' if TObject(A)=nil then ;');
  13497. Add(' if nil=TObject(A) then ;');
  13498. Add('end;');
  13499. Add('procedure ProcB(out A);');
  13500. Add('begin');
  13501. Add(' TObject(A):=nil;');
  13502. Add(' TObject(A):=TObject(A);');
  13503. Add(' if TObject(A)=nil then ;');
  13504. Add(' if nil=TObject(A) then ;');
  13505. Add('end;');
  13506. Add('procedure ProcC(const A);');
  13507. Add('begin');
  13508. Add(' if TObject(A)=nil then ;');
  13509. Add(' if nil=TObject(A) then ;');
  13510. Add('end;');
  13511. Add('var o: TObject;');
  13512. Add('begin');
  13513. Add(' ProcA(o);');
  13514. Add(' ProcB(o);');
  13515. Add(' ProcC(o);');
  13516. ConvertProgram;
  13517. CheckSource('TestClass_TypeCastUntypedParam',
  13518. LinesToStr([ // statements
  13519. 'rtl.createClass($mod, "TObject", null, function () {',
  13520. ' this.$init = function () {',
  13521. ' };',
  13522. ' this.$final = function () {',
  13523. ' };',
  13524. '});',
  13525. 'this.ProcA = function (A) {',
  13526. ' A.set(null);',
  13527. ' A.set(A.get());',
  13528. ' if (A.get() === null);',
  13529. ' if (null === A.get());',
  13530. '};',
  13531. 'this.ProcB = function (A) {',
  13532. ' A.set(null);',
  13533. ' A.set(A.get());',
  13534. ' if (A.get() === null);',
  13535. ' if (null === A.get());',
  13536. '};',
  13537. 'this.ProcC = function (A) {',
  13538. ' if (A === null);',
  13539. ' if (null === A);',
  13540. '};',
  13541. 'this.o = null;',
  13542. '']),
  13543. LinesToStr([ // $mod.$main
  13544. '$mod.ProcA({',
  13545. ' p: $mod,',
  13546. ' get: function () {',
  13547. ' return this.p.o;',
  13548. ' },',
  13549. ' set: function (v) {',
  13550. ' this.p.o = v;',
  13551. ' }',
  13552. '});',
  13553. '$mod.ProcB({',
  13554. ' p: $mod,',
  13555. ' get: function () {',
  13556. ' return this.p.o;',
  13557. ' },',
  13558. ' set: function (v) {',
  13559. ' this.p.o = v;',
  13560. ' }',
  13561. '});',
  13562. '$mod.ProcC($mod.o);',
  13563. '']));
  13564. end;
  13565. procedure TTestModule.TestClass_Overloads;
  13566. begin
  13567. StartProgram(false);
  13568. Add('type');
  13569. Add(' TObject = class');
  13570. Add(' procedure DoIt;');
  13571. Add(' procedure DoIt(vI: longint);');
  13572. Add(' end;');
  13573. Add('procedure TObject.DoIt;');
  13574. Add('begin');
  13575. Add(' DoIt;');
  13576. Add(' DoIt(1);');
  13577. Add('end;');
  13578. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13579. Add('begin');
  13580. ConvertProgram;
  13581. CheckSource('TestClass_Overloads',
  13582. LinesToStr([ // statements
  13583. 'rtl.createClass($mod, "TObject", null, function () {',
  13584. ' this.$init = function () {',
  13585. ' };',
  13586. ' this.$final = function () {',
  13587. ' };',
  13588. ' this.DoIt = function () {',
  13589. ' this.DoIt();',
  13590. ' this.DoIt$1(1);',
  13591. ' };',
  13592. ' this.DoIt$1 = function (vI) {',
  13593. ' };',
  13594. '});',
  13595. '']),
  13596. LinesToStr([ // $mod.$main
  13597. '']));
  13598. end;
  13599. procedure TTestModule.TestClass_OverloadsAncestor;
  13600. begin
  13601. StartProgram(false);
  13602. Add('type');
  13603. Add(' TObject = class;');
  13604. Add(' TObject = class');
  13605. Add(' procedure DoIt(vA: longint);');
  13606. Add(' procedure DoIt(vA, vB: longint);');
  13607. Add(' end;');
  13608. Add(' TCar = class;');
  13609. Add(' TCar = class');
  13610. Add(' procedure DoIt(vA: longint);');
  13611. Add(' procedure DoIt(vA, vB: longint);');
  13612. Add(' end;');
  13613. Add('procedure tobject.doit(va: longint);');
  13614. Add('begin');
  13615. Add(' doit(1);');
  13616. Add(' doit(1,2);');
  13617. Add('end;');
  13618. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13619. Add('procedure tcar.doit(va: longint);');
  13620. Add('begin');
  13621. Add(' doit(1);');
  13622. Add(' doit(1,2);');
  13623. Add(' inherited doit(1);');
  13624. Add(' inherited doit(1,2);');
  13625. Add('end;');
  13626. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13627. Add('begin');
  13628. ConvertProgram;
  13629. CheckSource('TestClass_OverloadsAncestor',
  13630. LinesToStr([ // statements
  13631. 'rtl.createClass($mod, "TObject", null, function () {',
  13632. ' this.$init = function () {',
  13633. ' };',
  13634. ' this.$final = function () {',
  13635. ' };',
  13636. ' this.DoIt = function (vA) {',
  13637. ' this.DoIt(1);',
  13638. ' this.DoIt$1(1,2);',
  13639. ' };',
  13640. ' this.DoIt$1 = function (vA, vB) {',
  13641. ' };',
  13642. '});',
  13643. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13644. ' this.DoIt$2 = function (vA) {',
  13645. ' this.DoIt$2(1);',
  13646. ' this.DoIt$3(1, 2);',
  13647. ' $mod.TObject.DoIt.call(this, 1);',
  13648. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13649. ' };',
  13650. ' this.DoIt$3 = function (vA, vB) {',
  13651. ' };',
  13652. '});',
  13653. '']),
  13654. LinesToStr([ // $mod.$main
  13655. '']));
  13656. end;
  13657. procedure TTestModule.TestClass_OverloadConstructor;
  13658. begin
  13659. StartProgram(false);
  13660. Add('type');
  13661. Add(' TObject = class');
  13662. Add(' constructor Create(vA: longint);');
  13663. Add(' constructor Create(vA, vB: longint);');
  13664. Add(' end;');
  13665. Add(' TCar = class');
  13666. Add(' constructor Create(vA: longint);');
  13667. Add(' constructor Create(vA, vB: longint);');
  13668. Add(' end;');
  13669. Add('constructor tobject.create(va: longint);');
  13670. Add('begin');
  13671. Add(' create(1);');
  13672. Add(' create(1,2);');
  13673. Add('end;');
  13674. Add('constructor tobject.create(va, vb: longint); begin end;');
  13675. Add('constructor tcar.create(va: longint);');
  13676. Add('begin');
  13677. Add(' create(1);');
  13678. Add(' create(1,2);');
  13679. Add(' inherited create(1);');
  13680. Add(' inherited create(1,2);');
  13681. Add('end;');
  13682. Add('constructor tcar.create(va, vb: longint); begin end;');
  13683. Add('begin');
  13684. Add(' tobject.create(1);');
  13685. Add(' tobject.create(1,2);');
  13686. Add(' tcar.create(1);');
  13687. Add(' tcar.create(1,2);');
  13688. ConvertProgram;
  13689. CheckSource('TestClass_OverloadConstructor',
  13690. LinesToStr([ // statements
  13691. 'rtl.createClass($mod, "TObject", null, function () {',
  13692. ' this.$init = function () {',
  13693. ' };',
  13694. ' this.$final = function () {',
  13695. ' };',
  13696. ' this.Create = function (vA) {',
  13697. ' this.Create(1);',
  13698. ' this.Create$1(1,2);',
  13699. ' return this;',
  13700. ' };',
  13701. ' this.Create$1 = function (vA, vB) {',
  13702. ' return this;',
  13703. ' };',
  13704. '});',
  13705. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13706. ' this.Create$2 = function (vA) {',
  13707. ' this.Create$2(1);',
  13708. ' this.Create$3(1, 2);',
  13709. ' $mod.TObject.Create.call(this, 1);',
  13710. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13711. ' return this;',
  13712. ' };',
  13713. ' this.Create$3 = function (vA, vB) {',
  13714. ' return this;',
  13715. ' };',
  13716. '});',
  13717. '']),
  13718. LinesToStr([ // $mod.$main
  13719. '$mod.TObject.$create("Create", [1]);',
  13720. '$mod.TObject.$create("Create$1", [1, 2]);',
  13721. '$mod.TCar.$create("Create$2", [1]);',
  13722. '$mod.TCar.$create("Create$3", [1, 2]);',
  13723. '']));
  13724. end;
  13725. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13726. begin
  13727. StartProgram(false);
  13728. Add([
  13729. '{$mode delphi}',
  13730. 'type',
  13731. ' TObject = class end;',
  13732. ' TBird = class',
  13733. ' function {#a}GetValue: longint; overload; virtual;',
  13734. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13735. ' end;',
  13736. ' TEagle = class(TBird)',
  13737. ' function {#c}GetValue: longint; overload; override;',
  13738. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13739. ' end;',
  13740. 'function TBird.GetValue: longint;',
  13741. 'begin',
  13742. ' if 3={@a}GetValue then ;',
  13743. ' if 4={@b}GetValue(5) then ;',
  13744. 'end;',
  13745. 'function TBird.GetValue(AValue: longint): longint;',
  13746. 'begin',
  13747. 'end;',
  13748. 'function TEagle.GetValue: longint;',
  13749. 'begin',
  13750. ' if 13={@c}GetValue then ;',
  13751. ' if 14={@d}GetValue(15) then ;',
  13752. ' if 15=inherited {@a}GetValue then ;',
  13753. ' if 16=inherited {@b}GetValue(17) then ;',
  13754. 'end;',
  13755. 'function TEagle.GetValue(AValue: longint): longint;',
  13756. 'begin',
  13757. 'end;',
  13758. 'var',
  13759. ' e: TEagle;',
  13760. 'begin',
  13761. ' if 23=e.{@c}GetValue then ;',
  13762. ' if 24=e.{@d}GetValue(25) then ;']);
  13763. ConvertProgram;
  13764. CheckSource('TestClass_OverloadDelphiOverride',
  13765. LinesToStr([ // statements
  13766. 'rtl.createClass($mod, "TObject", null, function () {',
  13767. ' this.$init = function () {',
  13768. ' };',
  13769. ' this.$final = function () {',
  13770. ' };',
  13771. '});',
  13772. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13773. ' this.GetValue = function () {',
  13774. ' var Result = 0;',
  13775. ' if (3 === this.GetValue()) ;',
  13776. ' if (4 === this.GetValue$1(5)) ;',
  13777. ' return Result;',
  13778. ' };',
  13779. ' this.GetValue$1 = function (AValue) {',
  13780. ' var Result = 0;',
  13781. ' return Result;',
  13782. ' };',
  13783. '});',
  13784. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13785. ' this.GetValue = function () {',
  13786. ' var Result = 0;',
  13787. ' if (13 === this.GetValue()) ;',
  13788. ' if (14 === this.GetValue$1(15)) ;',
  13789. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13790. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13791. ' return Result;',
  13792. ' };',
  13793. ' this.GetValue$1 = function (AValue) {',
  13794. ' var Result = 0;',
  13795. ' return Result;',
  13796. ' };',
  13797. '});',
  13798. 'this.e = null;',
  13799. '']),
  13800. LinesToStr([ // $mod.$main
  13801. 'if (23 === $mod.e.GetValue()) ;',
  13802. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13803. '']));
  13804. end;
  13805. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13806. begin
  13807. StartProgram(false);
  13808. Add([
  13809. '{$mode delphi}',
  13810. 'type',
  13811. ' TObject = class end;',
  13812. ' TAnimal = class',
  13813. ' public',
  13814. ' {#animal_a}A: longint;',
  13815. ' function {#animal_b}B: longint;',
  13816. ' end;',
  13817. ' TBird = class(TAnimal)',
  13818. ' public',
  13819. ' {#bird_a}A: double;',
  13820. ' {#bird_b}B: boolean;',
  13821. ' end;',
  13822. ' TEagle = class(TBird)',
  13823. ' public',
  13824. ' function {#eagle_a}A: boolean;',
  13825. ' {#eagle_b}B: double;',
  13826. ' end;',
  13827. 'function TAnimal.B: longint;',
  13828. 'begin',
  13829. 'end;',
  13830. 'function TEagle.A: boolean;',
  13831. 'begin',
  13832. ' {@eagle_b}B:=3.3;',
  13833. ' {@eagle_a}A();',
  13834. ' TBird(Self).{@bird_b}B:=true;',
  13835. ' TAnimal(Self).{@animal_a}A:=17;',
  13836. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13837. 'end;',
  13838. 'var',
  13839. ' e: TEagle;',
  13840. 'begin',
  13841. ' e.{@eagle_b}B:=5.3;',
  13842. ' if e.{@eagle_a}A then ;',
  13843. '']);
  13844. ConvertProgram;
  13845. CheckSource('TestClass_ReintroduceVarDelphi',
  13846. LinesToStr([ // statements
  13847. 'rtl.createClass($mod, "TObject", null, function () {',
  13848. ' this.$init = function () {',
  13849. ' };',
  13850. ' this.$final = function () {',
  13851. ' };',
  13852. '});',
  13853. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13854. ' this.$init = function () {',
  13855. ' $mod.TObject.$init.call(this);',
  13856. ' this.A = 0;',
  13857. ' };',
  13858. ' this.B = function () {',
  13859. ' var Result = 0;',
  13860. ' return Result;',
  13861. ' };',
  13862. '});',
  13863. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13864. ' this.$init = function () {',
  13865. ' $mod.TAnimal.$init.call(this);',
  13866. ' this.A$1 = 0.0;',
  13867. ' this.B$1 = false;',
  13868. ' };',
  13869. '});',
  13870. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13871. ' this.$init = function () {',
  13872. ' $mod.TBird.$init.call(this);',
  13873. ' this.B$2 = 0.0;',
  13874. ' };',
  13875. ' this.A$2 = function () {',
  13876. ' var Result = false;',
  13877. ' this.B$2 = 3.3;',
  13878. ' this.A$2();',
  13879. ' this.B$1 = true;',
  13880. ' this.A = 17;',
  13881. ' this.B$1 = this.A$1 > 1;',
  13882. ' return Result;',
  13883. ' };',
  13884. '});',
  13885. 'this.e = null;',
  13886. '']),
  13887. LinesToStr([ // $mod.$main
  13888. '$mod.e.B$2 = 5.3;',
  13889. 'if ($mod.e.A$2()) ;',
  13890. '']));
  13891. end;
  13892. procedure TTestModule.TestClass_ReintroducedVar;
  13893. begin
  13894. StartProgram(false);
  13895. Add('type');
  13896. Add(' TObject = class');
  13897. Add(' strict private');
  13898. Add(' Some: longint;');
  13899. Add(' end;');
  13900. Add(' TMobile = class');
  13901. Add(' strict private');
  13902. Add(' Some: string;');
  13903. Add(' end;');
  13904. Add(' TCar = class(tmobile)');
  13905. Add(' procedure Some;');
  13906. Add(' procedure Some(vA: longint);');
  13907. Add(' end;');
  13908. Add('procedure tcar.some;');
  13909. Add('begin');
  13910. Add(' Some;');
  13911. Add(' Some(1);');
  13912. Add('end;');
  13913. Add('procedure tcar.some(va: longint); begin end;');
  13914. Add('begin');
  13915. ConvertProgram;
  13916. CheckSource('TestClass_ReintroducedVar',
  13917. LinesToStr([ // statements
  13918. 'rtl.createClass($mod, "TObject", null, function () {',
  13919. ' this.$init = function () {',
  13920. ' this.Some = 0;',
  13921. ' };',
  13922. ' this.$final = function () {',
  13923. ' };',
  13924. '});',
  13925. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13926. ' this.$init = function () {',
  13927. ' $mod.TObject.$init.call(this);',
  13928. ' this.Some$1 = "";',
  13929. ' };',
  13930. '});',
  13931. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13932. ' this.Some$2 = function () {',
  13933. ' this.Some$2();',
  13934. ' this.Some$3(1);',
  13935. ' };',
  13936. ' this.Some$3 = function (vA) {',
  13937. ' };',
  13938. '});',
  13939. '']),
  13940. LinesToStr([ // $mod.$main
  13941. '']));
  13942. end;
  13943. procedure TTestModule.TestClass_RaiseDescendant;
  13944. begin
  13945. StartProgram(false);
  13946. Add([
  13947. 'type',
  13948. ' TObject = class',
  13949. ' constructor Create(Msg: string);',
  13950. ' end;',
  13951. ' Exception = class',
  13952. ' end;',
  13953. ' EConvertError = class(Exception)',
  13954. ' end;',
  13955. 'constructor TObject.Create(Msg: string); begin end;',
  13956. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13957. 'begin',
  13958. ' raise Exception.Create(''Bar1'');',
  13959. ' raise EConvertError.Create(''Bar2'');',
  13960. ' raise AssertConv(''Bar2'');',
  13961. ' raise AssertConv;',
  13962. '']);
  13963. ConvertProgram;
  13964. CheckSource('TestClass_RaiseDescendant',
  13965. LinesToStr([ // statements
  13966. 'rtl.createClass($mod, "TObject", null, function () {',
  13967. ' this.$init = function () {',
  13968. ' };',
  13969. ' this.$final = function () {',
  13970. ' };',
  13971. ' this.Create = function (Msg) {',
  13972. ' return this;',
  13973. ' };',
  13974. '});',
  13975. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13976. '});',
  13977. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13978. '});',
  13979. 'this.AssertConv = function (Msg) {',
  13980. ' var Result = null;',
  13981. ' return Result;',
  13982. '};',
  13983. '']),
  13984. LinesToStr([ // $mod.$main
  13985. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13986. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13987. 'throw $mod.AssertConv("Bar2");',
  13988. 'throw $mod.AssertConv("def");',
  13989. '']));
  13990. end;
  13991. procedure TTestModule.TestClass_ExternalMethod;
  13992. begin
  13993. AddModuleWithIntfImplSrc('unit2.pas',
  13994. LinesToStr([
  13995. 'type',
  13996. ' TObject = class',
  13997. ' public',
  13998. ' procedure Intern; external name ''$DoIntern'';',
  13999. ' end;',
  14000. '']),
  14001. LinesToStr([
  14002. '']));
  14003. StartUnit(true);
  14004. Add('interface');
  14005. Add('uses unit2;');
  14006. Add('type');
  14007. Add(' TCar = class(TObject)');
  14008. Add(' public');
  14009. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14010. Add(' procedure DoIt;');
  14011. Add(' end;');
  14012. Add('implementation');
  14013. Add('procedure tcar.doit;');
  14014. Add('begin');
  14015. Add(' Intern;');
  14016. Add(' Intern();');
  14017. Add(' Intern2;');
  14018. Add(' Intern2();');
  14019. Add('end;');
  14020. Add('var Obj: TCar;');
  14021. Add('begin');
  14022. Add(' obj.intern;');
  14023. Add(' obj.intern();');
  14024. Add(' obj.intern2;');
  14025. Add(' obj.intern2();');
  14026. Add(' obj.doit;');
  14027. Add(' obj.doit();');
  14028. Add(' with obj do begin');
  14029. Add(' Intern;');
  14030. Add(' Intern();');
  14031. Add(' Intern2;');
  14032. Add(' Intern2();');
  14033. Add(' end;');
  14034. ConvertUnit;
  14035. CheckSource('TestClass_ExternalMethod',
  14036. LinesToStr([
  14037. 'var $impl = $mod.$impl;',
  14038. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14039. ' this.DoIt = function () {',
  14040. ' this.$DoIntern();',
  14041. ' this.$DoIntern();',
  14042. ' this.$DoIntern2();',
  14043. ' this.$DoIntern2();',
  14044. ' };',
  14045. ' });',
  14046. '']),
  14047. LinesToStr([ // this.$init
  14048. '$impl.Obj.$DoIntern();',
  14049. '$impl.Obj.$DoIntern();',
  14050. '$impl.Obj.$DoIntern2();',
  14051. '$impl.Obj.$DoIntern2();',
  14052. '$impl.Obj.DoIt();',
  14053. '$impl.Obj.DoIt();',
  14054. 'var $with1 = $impl.Obj;',
  14055. '$with1.$DoIntern();',
  14056. '$with1.$DoIntern();',
  14057. '$with1.$DoIntern2();',
  14058. '$with1.$DoIntern2();',
  14059. '']),
  14060. LinesToStr([ // implementation
  14061. '$impl.Obj = null;',
  14062. '']) );
  14063. end;
  14064. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14065. begin
  14066. StartProgram(false);
  14067. Add('type');
  14068. Add(' TObject = class');
  14069. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14070. Add(' end;');
  14071. Add('begin');
  14072. SetExpectedPasResolverError('Virtual method name must match external',
  14073. nVirtualMethodNameMustMatchExternal);
  14074. ConvertProgram;
  14075. end;
  14076. procedure TTestModule.TestClass_ExternalOverrideFail;
  14077. begin
  14078. StartProgram(false);
  14079. Add('type');
  14080. Add(' TObject = class');
  14081. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14082. Add(' end;');
  14083. Add(' TCar = class');
  14084. Add(' procedure DoIt; override; external name ''DoIt'';');
  14085. Add(' end;');
  14086. Add('begin');
  14087. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14088. nInvalidXModifierY);
  14089. ConvertProgram;
  14090. end;
  14091. procedure TTestModule.TestClass_ExternalVar;
  14092. begin
  14093. AddModuleWithIntfImplSrc('unit2.pas',
  14094. LinesToStr([
  14095. '{$modeswitch externalclass}',
  14096. 'type',
  14097. ' TObject = class',
  14098. ' public',
  14099. ' Intern: longint external name ''$Intern'';',
  14100. ' Bracket: longint external name ''["A B"]'';',
  14101. ' end;',
  14102. '']),
  14103. LinesToStr([
  14104. '']));
  14105. StartUnit(true);
  14106. Add([
  14107. 'interface',
  14108. 'uses unit2;',
  14109. '{$modeswitch externalclass}',
  14110. 'type',
  14111. ' TCar = class(tobject)',
  14112. ' public',
  14113. ' Intern2: longint external name ''$Intern2'';',
  14114. ' procedure DoIt;',
  14115. ' end;',
  14116. 'implementation',
  14117. 'procedure tcar.doit;',
  14118. 'begin',
  14119. ' Intern:=Intern+1;',
  14120. ' Intern2:=Intern2+2;',
  14121. ' Bracket:=Bracket+3;',
  14122. 'end;',
  14123. 'var Obj: TCar;',
  14124. 'begin',
  14125. ' obj.intern:=obj.intern+1;',
  14126. ' obj.intern2:=obj.intern2+2;',
  14127. ' obj.Bracket:=obj.Bracket+3;',
  14128. ' with obj do begin',
  14129. ' intern:=intern+1;',
  14130. ' intern2:=intern2+2;',
  14131. ' Bracket:=Bracket+3;',
  14132. ' end;']);
  14133. ConvertUnit;
  14134. CheckSource('TestClass_ExternalVar',
  14135. LinesToStr([
  14136. 'var $impl = $mod.$impl;',
  14137. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14138. ' this.DoIt = function () {',
  14139. ' this.$Intern = this.$Intern + 1;',
  14140. ' this.$Intern2 = this.$Intern2 + 2;',
  14141. ' this["A B"] = this["A B"] + 3;',
  14142. ' };',
  14143. ' });',
  14144. '']),
  14145. LinesToStr([
  14146. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14147. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14148. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14149. 'var $with1 = $impl.Obj;',
  14150. '$with1.$Intern = $with1.$Intern + 1;',
  14151. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  14152. '$with1["A B"] = $with1["A B"] + 3;',
  14153. '']),
  14154. LinesToStr([ // implementation
  14155. '$impl.Obj = null;',
  14156. '']));
  14157. end;
  14158. procedure TTestModule.TestClass_Const;
  14159. begin
  14160. StartProgram(false);
  14161. Add('type');
  14162. Add(' integer = longint;');
  14163. Add(' TClass = class of TObject;');
  14164. Add(' TObject = class');
  14165. Add(' public');
  14166. Add(' const cI: integer = 3;');
  14167. Add(' procedure DoIt;');
  14168. Add(' class procedure DoMore;');
  14169. Add(' end;');
  14170. Add('procedure tobject.doit;');
  14171. Add('begin');
  14172. Add(' if cI=4 then;');
  14173. Add(' if 5=cI then;');
  14174. Add(' if Self.cI=6 then;');
  14175. Add(' if 7=Self.cI then;');
  14176. Add(' with Self do begin');
  14177. Add(' if cI=11 then;');
  14178. Add(' if 12=cI then;');
  14179. Add(' end;');
  14180. Add('end;');
  14181. Add('class procedure tobject.domore;');
  14182. Add('begin');
  14183. Add(' if cI=8 then;');
  14184. Add(' if Self.cI=9 then;');
  14185. Add(' if 10=cI then;');
  14186. Add(' if 11=Self.cI then;');
  14187. Add(' with Self do begin');
  14188. Add(' if cI=13 then;');
  14189. Add(' if 14=cI then;');
  14190. Add(' end;');
  14191. Add('end;');
  14192. Add('var');
  14193. Add(' Obj: TObject;');
  14194. Add(' Cla: TClass;');
  14195. Add('begin');
  14196. Add(' if TObject.cI=21 then ;');
  14197. Add(' if Obj.cI=22 then ;');
  14198. Add(' if Cla.cI=23 then ;');
  14199. Add(' with obj do if ci=24 then;');
  14200. Add(' with TObject do if ci=25 then;');
  14201. Add(' with Cla do if ci=26 then;');
  14202. ConvertProgram;
  14203. CheckSource('TestClass_Const',
  14204. LinesToStr([
  14205. 'rtl.createClass($mod, "TObject", null, function () {',
  14206. ' this.cI = 3;',
  14207. ' this.$init = function () {',
  14208. ' };',
  14209. ' this.$final = function () {',
  14210. ' };',
  14211. ' this.DoIt = function () {',
  14212. ' if (this.cI === 4) ;',
  14213. ' if (5 === this.cI) ;',
  14214. ' if (this.cI === 6) ;',
  14215. ' if (7 === this.cI) ;',
  14216. ' if (this.cI === 11) ;',
  14217. ' if (12 === this.cI) ;',
  14218. ' };',
  14219. ' this.DoMore = function () {',
  14220. ' if (this.cI === 8) ;',
  14221. ' if (this.cI === 9) ;',
  14222. ' if (10 === this.cI) ;',
  14223. ' if (11 === this.cI) ;',
  14224. ' if (this.cI === 13) ;',
  14225. ' if (14 === this.cI) ;',
  14226. ' };',
  14227. '});',
  14228. 'this.Obj = null;',
  14229. 'this.Cla = null;',
  14230. '']),
  14231. LinesToStr([
  14232. 'if ($mod.TObject.cI === 21) ;',
  14233. 'if ($mod.Obj.cI === 22) ;',
  14234. 'if ($mod.Cla.cI === 23) ;',
  14235. 'var $with1 = $mod.Obj;',
  14236. 'if ($with1.cI === 24) ;',
  14237. 'var $with2 = $mod.TObject;',
  14238. 'if ($with2.cI === 25) ;',
  14239. 'var $with3 = $mod.Cla;',
  14240. 'if ($with3.cI === 26) ;',
  14241. '']));
  14242. end;
  14243. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14244. begin
  14245. StartProgram(false);
  14246. Add([
  14247. 'type',
  14248. ' TObject = class',
  14249. ' const cI: longint = 3;',
  14250. ' procedure Fly;',
  14251. ' procedure Run;',
  14252. ' end;',
  14253. ' TBird = class',
  14254. ' procedure Go;',
  14255. ' end;',
  14256. 'procedure tobject.fly;',
  14257. 'const cI: word = 4;',
  14258. 'begin',
  14259. ' if cI=Self.cI then ;',
  14260. 'end;',
  14261. 'procedure tobject.run;',
  14262. 'const cI: word = 5;',
  14263. 'begin',
  14264. ' if cI=Self.cI then ;',
  14265. 'end;',
  14266. 'procedure tbird.go;',
  14267. 'const cI: word = 6;',
  14268. 'begin',
  14269. ' if cI=Self.cI then ;',
  14270. 'end;',
  14271. 'begin',
  14272. '']);
  14273. ConvertProgram;
  14274. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14275. LinesToStr([
  14276. 'rtl.createClass($mod, "TObject", null, function () {',
  14277. ' this.cI = 3;',
  14278. ' this.$init = function () {',
  14279. ' };',
  14280. ' this.$final = function () {',
  14281. ' };',
  14282. ' var cI$1 = 4;',
  14283. ' this.Fly = function () {',
  14284. ' if (cI$1 === this.cI) ;',
  14285. ' };',
  14286. ' var cI$2 = 5;',
  14287. ' this.Run = function () {',
  14288. ' if (cI$2 === this.cI) ;',
  14289. ' };',
  14290. '});',
  14291. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14292. ' var cI$3 = 6;',
  14293. ' this.Go = function () {',
  14294. ' if (cI$3 === this.cI) ;',
  14295. ' };',
  14296. '});',
  14297. '']),
  14298. LinesToStr([
  14299. '']));
  14300. end;
  14301. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14302. begin
  14303. StartUnit(false);
  14304. Add([
  14305. 'interface',
  14306. 'type',
  14307. ' TObject = class',
  14308. ' const cI: longint = 3;',
  14309. ' procedure Fly;',
  14310. ' procedure Run;',
  14311. ' end;',
  14312. ' TBird = class',
  14313. ' procedure Go;',
  14314. ' end;',
  14315. 'implementation',
  14316. 'procedure tobject.fly;',
  14317. 'const cI: word = 4;',
  14318. 'begin',
  14319. ' if cI=Self.cI then ;',
  14320. 'end;',
  14321. 'procedure tobject.run;',
  14322. 'const cI: word = 5;',
  14323. 'begin',
  14324. ' if cI=Self.cI then ;',
  14325. 'end;',
  14326. 'procedure tbird.go;',
  14327. 'const cI: word = 6;',
  14328. 'begin',
  14329. ' if cI=Self.cI then ;',
  14330. 'end;',
  14331. '']);
  14332. ConvertUnit;
  14333. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14334. LinesToStr([
  14335. 'rtl.createClass($mod, "TObject", null, function () {',
  14336. ' this.cI = 3;',
  14337. ' this.$init = function () {',
  14338. ' };',
  14339. ' this.$final = function () {',
  14340. ' };',
  14341. ' var cI$1 = 4;',
  14342. ' this.Fly = function () {',
  14343. ' if (cI$1 === this.cI) ;',
  14344. ' };',
  14345. ' var cI$2 = 5;',
  14346. ' this.Run = function () {',
  14347. ' if (cI$2 === this.cI) ;',
  14348. ' };',
  14349. '});',
  14350. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14351. ' var cI$3 = 6;',
  14352. ' this.Go = function () {',
  14353. ' if (cI$3 === this.cI) ;',
  14354. ' };',
  14355. '});',
  14356. '']),
  14357. '',
  14358. '');
  14359. end;
  14360. procedure TTestModule.TestClass_LocalVarSelfFail;
  14361. begin
  14362. StartProgram(false);
  14363. Add([
  14364. 'type',
  14365. ' TObject = class',
  14366. ' constructor Create;',
  14367. ' end;',
  14368. 'constructor tobject.create;',
  14369. 'var self: longint;',
  14370. 'begin',
  14371. 'end',
  14372. 'begin',
  14373. '']);
  14374. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14375. ConvertProgram;
  14376. end;
  14377. procedure TTestModule.TestClass_ArgSelfFail;
  14378. begin
  14379. StartProgram(false);
  14380. Add([
  14381. 'type',
  14382. ' TObject = class',
  14383. ' procedure DoIt(Self: longint);',
  14384. ' end;',
  14385. 'procedure tobject.doit(self: longint);',
  14386. 'begin',
  14387. 'end',
  14388. 'begin',
  14389. '']);
  14390. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14391. ConvertProgram;
  14392. end;
  14393. procedure TTestModule.TestClass_NestedProcSelf;
  14394. begin
  14395. StartProgram(false);
  14396. Add([
  14397. 'type',
  14398. ' TObject = class',
  14399. ' Key: longint;',
  14400. ' class var State: longint;',
  14401. ' procedure DoIt;',
  14402. ' function GetSize: longint; virtual; abstract;',
  14403. ' procedure SetSize(Value: longint); virtual; abstract;',
  14404. ' property Size: longint read GetSize write SetSize;',
  14405. ' end;',
  14406. 'procedure tobject.doit;',
  14407. ' procedure Sub;',
  14408. ' begin',
  14409. ' key:=key+2;',
  14410. ' self.key:=self.key+3;',
  14411. ' state:=state+4;',
  14412. ' self.state:=self.state+5;',
  14413. ' tobject.state:=tobject.state+6;',
  14414. ' size:=size+7;',
  14415. ' self.size:=self.size+8;',
  14416. ' end;',
  14417. 'begin',
  14418. ' sub;',
  14419. ' key:=key+12;',
  14420. ' self.key:=self.key+13;',
  14421. ' state:=state+14;',
  14422. ' self.state:=self.state+15;',
  14423. ' tobject.state:=tobject.state+16;',
  14424. ' size:=size+17;',
  14425. ' self.size:=self.size+18;',
  14426. 'end;',
  14427. 'begin',
  14428. '']);
  14429. ConvertProgram;
  14430. CheckSource('TestClass_NestedProcSelf',
  14431. LinesToStr([ // statements
  14432. 'rtl.createClass($mod, "TObject", null, function () {',
  14433. ' this.State = 0;',
  14434. ' this.$init = function () {',
  14435. ' this.Key = 0;',
  14436. ' };',
  14437. ' this.$final = function () {',
  14438. ' };',
  14439. ' this.DoIt = function () {',
  14440. ' var $Self = this;',
  14441. ' function Sub() {',
  14442. ' $Self.Key = $Self.Key + 2;',
  14443. ' $Self.Key = $Self.Key + 3;',
  14444. ' $mod.TObject.State = $Self.State + 4;',
  14445. ' $mod.TObject.State = $Self.State + 5;',
  14446. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14447. ' $Self.SetSize($Self.GetSize() + 7);',
  14448. ' $Self.SetSize($Self.GetSize() + 8);',
  14449. ' };',
  14450. ' Sub();',
  14451. ' $Self.Key = $Self.Key + 12;',
  14452. ' $Self.Key = $Self.Key + 13;',
  14453. ' $mod.TObject.State = $Self.State + 14;',
  14454. ' $mod.TObject.State = $Self.State + 15;',
  14455. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14456. ' $Self.SetSize($Self.GetSize() + 17);',
  14457. ' $Self.SetSize($Self.GetSize() + 18);',
  14458. ' };',
  14459. '});',
  14460. '']),
  14461. LinesToStr([ // $mod.$main
  14462. '']));
  14463. end;
  14464. procedure TTestModule.TestClass_NestedProcSelf2;
  14465. begin
  14466. StartProgram(false);
  14467. Add([
  14468. 'type',
  14469. ' TObject = class',
  14470. ' Key: longint;',
  14471. ' class var State: longint;',
  14472. ' function GetSize: longint; virtual; abstract;',
  14473. ' procedure SetSize(Value: longint); virtual; abstract;',
  14474. ' property Size: longint read GetSize write SetSize;',
  14475. ' end;',
  14476. ' TBird = class',
  14477. ' procedure DoIt;',
  14478. ' end;',
  14479. 'procedure tbird.doit;',
  14480. ' procedure Sub;',
  14481. ' begin',
  14482. ' key:=key+2;',
  14483. ' self.key:=self.key+3;',
  14484. ' state:=state+4;',
  14485. ' self.state:=self.state+5;',
  14486. ' tobject.state:=tobject.state+6;',
  14487. ' size:=size+7;',
  14488. ' self.size:=self.size+8;',
  14489. ' end;',
  14490. 'begin',
  14491. ' sub;',
  14492. ' key:=key+12;',
  14493. ' self.key:=self.key+13;',
  14494. ' state:=state+14;',
  14495. ' self.state:=self.state+15;',
  14496. ' tobject.state:=tobject.state+16;',
  14497. ' size:=size+17;',
  14498. ' self.size:=self.size+18;',
  14499. 'end;',
  14500. 'begin',
  14501. '']);
  14502. ConvertProgram;
  14503. CheckSource('TestClass_NestedProcSelf2',
  14504. LinesToStr([ // statements
  14505. 'rtl.createClass($mod, "TObject", null, function () {',
  14506. ' this.State = 0;',
  14507. ' this.$init = function () {',
  14508. ' this.Key = 0;',
  14509. ' };',
  14510. ' this.$final = function () {',
  14511. ' };',
  14512. '});',
  14513. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14514. ' this.DoIt = function () {',
  14515. ' var $Self = this;',
  14516. ' function Sub() {',
  14517. ' $Self.Key = $Self.Key + 2;',
  14518. ' $Self.Key = $Self.Key + 3;',
  14519. ' $mod.TObject.State = $Self.State + 4;',
  14520. ' $mod.TObject.State = $Self.State + 5;',
  14521. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14522. ' $Self.SetSize($Self.GetSize() + 7);',
  14523. ' $Self.SetSize($Self.GetSize() + 8);',
  14524. ' };',
  14525. ' Sub();',
  14526. ' $Self.Key = $Self.Key + 12;',
  14527. ' $Self.Key = $Self.Key + 13;',
  14528. ' $mod.TObject.State = $Self.State + 14;',
  14529. ' $mod.TObject.State = $Self.State + 15;',
  14530. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14531. ' $Self.SetSize($Self.GetSize() + 17);',
  14532. ' $Self.SetSize($Self.GetSize() + 18);',
  14533. ' };',
  14534. '});',
  14535. '']),
  14536. LinesToStr([ // $mod.$main
  14537. '']));
  14538. end;
  14539. procedure TTestModule.TestClass_NestedProcClassSelf;
  14540. begin
  14541. StartProgram(false);
  14542. Add([
  14543. 'type',
  14544. ' TObject = class',
  14545. ' class var State: longint;',
  14546. ' class procedure DoIt;',
  14547. ' class function GetSize: longint; virtual; abstract;',
  14548. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14549. ' class property Size: longint read GetSize write SetSize;',
  14550. ' end;',
  14551. 'class procedure tobject.doit;',
  14552. ' procedure Sub;',
  14553. ' begin',
  14554. ' state:=state+2;',
  14555. ' self.state:=self.state+3;',
  14556. ' tobject.state:=tobject.state+4;',
  14557. ' size:=size+5;',
  14558. ' self.size:=self.size+6;',
  14559. ' tobject.size:=tobject.size+7;',
  14560. ' end;',
  14561. 'begin',
  14562. ' sub;',
  14563. ' state:=state+12;',
  14564. ' self.state:=self.state+13;',
  14565. ' tobject.state:=tobject.state+14;',
  14566. ' size:=size+15;',
  14567. ' self.size:=self.size+16;',
  14568. ' tobject.size:=tobject.size+17;',
  14569. 'end;',
  14570. 'begin',
  14571. '']);
  14572. ConvertProgram;
  14573. CheckSource('TestClass_NestedProcClassSelf',
  14574. LinesToStr([ // statements
  14575. 'rtl.createClass($mod, "TObject", null, function () {',
  14576. ' this.State = 0;',
  14577. ' this.$init = function () {',
  14578. ' };',
  14579. ' this.$final = function () {',
  14580. ' };',
  14581. ' this.DoIt = function () {',
  14582. ' var $Self = this;',
  14583. ' function Sub() {',
  14584. ' $mod.TObject.State = $Self.State + 2;',
  14585. ' $mod.TObject.State = $Self.State + 3;',
  14586. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14587. ' $Self.SetSize($Self.GetSize() + 5);',
  14588. ' $Self.SetSize($Self.GetSize() + 6);',
  14589. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14590. ' };',
  14591. ' Sub();',
  14592. ' $mod.TObject.State = $Self.State + 12;',
  14593. ' $mod.TObject.State = $Self.State + 13;',
  14594. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14595. ' $Self.SetSize($Self.GetSize() + 15);',
  14596. ' $Self.SetSize($Self.GetSize() + 16);',
  14597. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14598. ' };',
  14599. '});',
  14600. '']),
  14601. LinesToStr([ // $mod.$main
  14602. '']));
  14603. end;
  14604. procedure TTestModule.TestClass_NestedProcCallInherited;
  14605. begin
  14606. StartProgram(false);
  14607. Add([
  14608. 'type',
  14609. ' TObject = class',
  14610. ' function DoIt(k: boolean): longint; virtual;',
  14611. ' end;',
  14612. ' TBird = class',
  14613. ' function DoIt(k: boolean): longint; override;',
  14614. ' end;',
  14615. 'function tobject.doit(k: boolean): longint;',
  14616. 'begin',
  14617. 'end;',
  14618. 'function tbird.doit(k: boolean): longint;',
  14619. ' procedure Sub;',
  14620. ' begin',
  14621. ' inherited DoIt(true);',
  14622. //' if inherited DoIt(false)=4 then ;',
  14623. ' end;',
  14624. 'begin',
  14625. ' Sub;',
  14626. ' inherited;',
  14627. ' inherited DoIt(true);',
  14628. //' if inherited DoIt(false)=14 then ;',
  14629. 'end;',
  14630. 'begin',
  14631. '']);
  14632. ConvertProgram;
  14633. CheckSource('TestClass_NestedProcCallInherited',
  14634. LinesToStr([ // statements
  14635. 'rtl.createClass($mod, "TObject", null, function () {',
  14636. ' this.$init = function () {',
  14637. ' };',
  14638. ' this.$final = function () {',
  14639. ' };',
  14640. ' this.DoIt = function (k) {',
  14641. ' var Result = 0;',
  14642. ' return Result;',
  14643. ' };',
  14644. '});',
  14645. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14646. ' this.DoIt = function (k) {',
  14647. ' var $Self = this;',
  14648. ' var Result = 0;',
  14649. ' function Sub() {',
  14650. ' $mod.TObject.DoIt.call($Self, true);',
  14651. ' };',
  14652. ' Sub();',
  14653. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14654. ' $mod.TObject.DoIt.call($Self, true);',
  14655. ' return Result;',
  14656. ' };',
  14657. '});',
  14658. '']),
  14659. LinesToStr([ // $mod.$main
  14660. '']));
  14661. end;
  14662. procedure TTestModule.TestClass_TObjectFree;
  14663. begin
  14664. StartProgram(false);
  14665. Add([
  14666. 'type',
  14667. ' TObject = class',
  14668. ' Obj: tobject;',
  14669. ' procedure Free;',
  14670. ' procedure Release;',
  14671. ' end;',
  14672. 'procedure tobject.free;',
  14673. 'begin',
  14674. 'end;',
  14675. 'procedure tobject.release;',
  14676. 'begin',
  14677. ' free;',
  14678. ' if true then free;',
  14679. 'end;',
  14680. 'function DoIt(o: tobject): tobject;',
  14681. 'var l: tobject;',
  14682. 'begin',
  14683. ' o.free;',
  14684. ' o.free();',
  14685. ' l.free;',
  14686. ' l.free();',
  14687. ' o.obj.free;',
  14688. ' o.obj.free();',
  14689. ' with o do obj.free;',
  14690. ' with o do obj.free();',
  14691. ' result.Free;',
  14692. ' result.Free();',
  14693. 'end;',
  14694. 'var o: tobject;',
  14695. ' a: array of tobject;',
  14696. 'begin',
  14697. ' o.free;',
  14698. ' o.obj.free;',
  14699. ' a[1+2].free;',
  14700. '']);
  14701. ConvertProgram;
  14702. CheckSource('TestClass_TObjectFree',
  14703. LinesToStr([ // statements
  14704. 'rtl.createClass($mod, "TObject", null, function () {',
  14705. ' this.$init = function () {',
  14706. ' this.Obj = null;',
  14707. ' };',
  14708. ' this.$final = function () {',
  14709. ' this.Obj = undefined;',
  14710. ' };',
  14711. ' this.Free = function () {',
  14712. ' };',
  14713. ' this.Release = function () {',
  14714. ' this.Free();',
  14715. ' if (true) this.Free();',
  14716. ' };',
  14717. '});',
  14718. 'this.DoIt = function (o) {',
  14719. ' var Result = null;',
  14720. ' var l = null;',
  14721. ' o = rtl.freeLoc(o);',
  14722. ' o = rtl.freeLoc(o);',
  14723. ' l = rtl.freeLoc(l);',
  14724. ' l = rtl.freeLoc(l);',
  14725. ' rtl.free(o, "Obj");',
  14726. ' rtl.free(o, "Obj");',
  14727. ' rtl.free(o, "Obj");',
  14728. ' rtl.free(o, "Obj");',
  14729. ' Result = rtl.freeLoc(Result);',
  14730. ' Result = rtl.freeLoc(Result);',
  14731. ' return Result;',
  14732. '};',
  14733. 'this.o = null;',
  14734. 'this.a = [];',
  14735. '']),
  14736. LinesToStr([ // $mod.$main
  14737. 'rtl.free($mod, "o");',
  14738. 'rtl.free($mod.o, "Obj");',
  14739. 'rtl.free($mod.a, 1 + 2);',
  14740. '']));
  14741. end;
  14742. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14743. begin
  14744. StartProgram(false);
  14745. Add([
  14746. 'type',
  14747. ' TObject = class',
  14748. ' Obj: tobject;',
  14749. ' procedure Free;',
  14750. ' end;',
  14751. 'procedure tobject.free;',
  14752. 'begin',
  14753. 'end;',
  14754. 'procedure DoIt(var o: tobject);',
  14755. 'begin',
  14756. ' o.free;',
  14757. ' o.free();',
  14758. 'end;',
  14759. 'begin',
  14760. '']);
  14761. ConvertProgram;
  14762. CheckSource('TestClass_TObjectFree_VarArg',
  14763. LinesToStr([ // statements
  14764. 'rtl.createClass($mod, "TObject", null, function () {',
  14765. ' this.$init = function () {',
  14766. ' this.Obj = null;',
  14767. ' };',
  14768. ' this.$final = function () {',
  14769. ' this.Obj = undefined;',
  14770. ' };',
  14771. ' this.Free = function () {',
  14772. ' };',
  14773. '});',
  14774. 'this.DoIt = function (o) {',
  14775. ' o.set(rtl.freeLoc(o.get()));',
  14776. ' o.set(rtl.freeLoc(o.get()));',
  14777. '};',
  14778. '']),
  14779. LinesToStr([ // $mod.$main
  14780. '']));
  14781. end;
  14782. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14783. begin
  14784. StartProgram(false);
  14785. Add([
  14786. 'type',
  14787. ' TObject = class',
  14788. ' constructor Create;',
  14789. ' procedure Free;',
  14790. ' end;',
  14791. 'constructor TObject.Create; begin end;',
  14792. 'procedure tobject.free; begin end;',
  14793. 'begin',
  14794. ' with tobject.create do free;',
  14795. '']);
  14796. ConvertProgram;
  14797. CheckSource('TestClass_TObjectFreeNewInstance',
  14798. LinesToStr([ // statements
  14799. 'rtl.createClass($mod, "TObject", null, function () {',
  14800. ' this.$init = function () {',
  14801. ' };',
  14802. ' this.$final = function () {',
  14803. ' };',
  14804. ' this.Create = function () {',
  14805. ' return this;',
  14806. ' };',
  14807. ' this.Free = function () {',
  14808. ' };',
  14809. '});',
  14810. '']),
  14811. LinesToStr([ // $mod.$main
  14812. 'var $with1 = $mod.TObject.$create("Create");',
  14813. '$with1=rtl.freeLoc($with1);',
  14814. '']));
  14815. end;
  14816. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14817. begin
  14818. StartProgram(false);
  14819. Add([
  14820. 'type',
  14821. ' TObject = class',
  14822. ' destructor Destroy;',
  14823. ' procedure Free;',
  14824. ' end;',
  14825. 'destructor TObject.Destroy; begin end;',
  14826. 'procedure tobject.free; begin end;',
  14827. 'var o: tobject;',
  14828. 'begin',
  14829. ' o.free;',
  14830. '']);
  14831. Converter.UseLowerCase:=true;
  14832. ConvertProgram;
  14833. CheckSource('TestClass_TObjectFreeLowerCase',
  14834. LinesToStr([ // statements
  14835. 'rtl.createClass($mod, "tobject", null, function () {',
  14836. ' this.$init = function () {',
  14837. ' };',
  14838. ' this.$final = function () {',
  14839. ' };',
  14840. ' rtl.tObjectDestroy = "destroy";',
  14841. ' this.destroy = function () {',
  14842. ' };',
  14843. ' this.free = function () {',
  14844. ' };',
  14845. '});',
  14846. 'this.o = null;',
  14847. '']),
  14848. LinesToStr([ // $mod.$main
  14849. 'rtl.free($mod, "o");',
  14850. '']));
  14851. end;
  14852. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14853. begin
  14854. StartProgram(false);
  14855. Add([
  14856. 'type',
  14857. ' TObject = class',
  14858. ' procedure Free;',
  14859. ' function GetObj: tobject; virtual; abstract;',
  14860. ' end;',
  14861. 'procedure tobject.free;',
  14862. 'begin',
  14863. 'end;',
  14864. 'var o: tobject;',
  14865. 'begin',
  14866. ' o.getobj.free;',
  14867. '']);
  14868. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14869. ConvertProgram;
  14870. end;
  14871. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14872. begin
  14873. StartProgram(false);
  14874. Add([
  14875. 'type',
  14876. ' TObject = class',
  14877. ' procedure Free;',
  14878. ' FObj: TObject;',
  14879. ' property Obj: tobject read FObj write FObj;',
  14880. ' end;',
  14881. 'procedure tobject.free;',
  14882. 'begin',
  14883. 'end;',
  14884. 'var o: tobject;',
  14885. 'begin',
  14886. ' o.obj.free;',
  14887. '']);
  14888. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14889. ConvertProgram;
  14890. end;
  14891. procedure TTestModule.TestClass_ForIn;
  14892. begin
  14893. StartProgram(false);
  14894. Add([
  14895. 'type',
  14896. ' TObject = class end;',
  14897. ' TItem = TObject;',
  14898. ' TEnumerator = class',
  14899. ' FCurrent: TItem;',
  14900. ' property Current: TItem read FCurrent;',
  14901. ' function MoveNext: boolean;',
  14902. ' end;',
  14903. ' TBird = class',
  14904. ' function GetEnumerator: TEnumerator;',
  14905. ' end;',
  14906. 'function TEnumerator.MoveNext: boolean;',
  14907. 'begin',
  14908. 'end;',
  14909. 'function TBird.GetEnumerator: TEnumerator;',
  14910. 'begin',
  14911. 'end;',
  14912. 'var',
  14913. ' b: TBird;',
  14914. ' i, i2: TItem;',
  14915. 'begin',
  14916. ' for i in b do i2:=i;']);
  14917. ConvertProgram;
  14918. CheckSource('TestClass_ForIn',
  14919. LinesToStr([ // statements
  14920. 'rtl.createClass($mod, "TObject", null, function () {',
  14921. ' this.$init = function () {',
  14922. ' };',
  14923. ' this.$final = function () {',
  14924. ' };',
  14925. '});',
  14926. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14927. ' this.$init = function () {',
  14928. ' $mod.TObject.$init.call(this);',
  14929. ' this.FCurrent = null;',
  14930. ' };',
  14931. ' this.$final = function () {',
  14932. ' this.FCurrent = undefined;',
  14933. ' $mod.TObject.$final.call(this);',
  14934. ' };',
  14935. ' this.MoveNext = function () {',
  14936. ' var Result = false;',
  14937. ' return Result;',
  14938. ' };',
  14939. '});',
  14940. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14941. ' this.GetEnumerator = function () {',
  14942. ' var Result = null;',
  14943. ' return Result;',
  14944. ' };',
  14945. '});',
  14946. 'this.b = null;',
  14947. 'this.i = null;',
  14948. 'this.i2 = null;'
  14949. ]),
  14950. LinesToStr([ // $mod.$main
  14951. 'var $in1 = $mod.b.GetEnumerator();',
  14952. 'try {',
  14953. ' while ($in1.MoveNext()){',
  14954. ' $mod.i = $in1.FCurrent;',
  14955. ' $mod.i2 = $mod.i;',
  14956. ' }',
  14957. '} finally {',
  14958. ' $in1 = rtl.freeLoc($in1)',
  14959. '};',
  14960. '']));
  14961. end;
  14962. procedure TTestModule.TestClass_DispatchMessage;
  14963. begin
  14964. StartProgram(false);
  14965. Add([
  14966. 'type',
  14967. ' TObject = class',
  14968. ' {$DispatchField DispInt}',
  14969. ' procedure Dispatch(var Msg); virtual; abstract;',
  14970. ' {$DispatchStrField DispStr}',
  14971. ' procedure DispatchStr(var Msg); virtual; abstract;',
  14972. ' end;',
  14973. ' THopMsg = record',
  14974. ' DispInt: longint;',
  14975. ' end;',
  14976. ' TPutMsg = record',
  14977. ' DispStr: string;',
  14978. ' end;',
  14979. ' TBird = class',
  14980. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  14981. ' procedure Run; overload; virtual; abstract;',
  14982. ' procedure Run(var Msg); overload; message ''Fast'';',
  14983. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  14984. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  14985. ' end;',
  14986. 'procedure TBird.Run(var Msg);',
  14987. 'begin',
  14988. 'end;',
  14989. 'begin',
  14990. '']);
  14991. ConvertProgram;
  14992. CheckSource('TestClass_Message',
  14993. LinesToStr([ // statements
  14994. 'rtl.createClass($mod, "TObject", null, function () {',
  14995. ' this.$init = function () {',
  14996. ' };',
  14997. ' this.$final = function () {',
  14998. ' };',
  14999. '});',
  15000. 'rtl.recNewT($mod, "THopMsg", function () {',
  15001. ' this.DispInt = 0;',
  15002. ' this.$eq = function (b) {',
  15003. ' return this.DispInt === b.DispInt;',
  15004. ' };',
  15005. ' this.$assign = function (s) {',
  15006. ' this.DispInt = s.DispInt;',
  15007. ' return this;',
  15008. ' };',
  15009. '});',
  15010. 'rtl.recNewT($mod, "TPutMsg", function () {',
  15011. ' this.DispStr = "";',
  15012. ' this.$eq = function (b) {',
  15013. ' return this.DispStr === b.DispStr;',
  15014. ' };',
  15015. ' this.$assign = function (s) {',
  15016. ' this.DispStr = s.DispStr;',
  15017. ' return this;',
  15018. ' };',
  15019. '});',
  15020. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15021. ' this.Run$1 = function (Msg) {',
  15022. ' };',
  15023. ' this.$msgint = {',
  15024. ' "2": "Fly",',
  15025. ' "3": "Hop"',
  15026. ' };',
  15027. ' this.$msgstr = {',
  15028. ' Fast: "Run$1",',
  15029. ' foo: "Put"',
  15030. ' };',
  15031. '});',
  15032. '']),
  15033. LinesToStr([ // $mod.$main
  15034. '']));
  15035. end;
  15036. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15037. begin
  15038. StartProgram(false);
  15039. Add([
  15040. 'type',
  15041. ' TObject = class',
  15042. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15043. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15044. ' end;',
  15045. 'begin',
  15046. '']);
  15047. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15048. ConvertProgram;
  15049. end;
  15050. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15051. begin
  15052. StartProgram(false);
  15053. Add([
  15054. 'type',
  15055. ' TObject = class',
  15056. ' {$dispatchfield Msg}',
  15057. ' procedure Dispatch(var Msg); virtual; abstract;',
  15058. ' end;',
  15059. ' TFlyMsg = record',
  15060. ' FlyId: longint;',
  15061. ' end;',
  15062. ' TBird = class',
  15063. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15064. ' end;',
  15065. 'begin',
  15066. '']);
  15067. ConvertProgram;
  15068. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15069. end;
  15070. procedure TTestModule.TestClassOf_Create;
  15071. begin
  15072. StartProgram(false);
  15073. Add('type');
  15074. Add(' TObject = class');
  15075. Add(' constructor Create;');
  15076. Add(' end;');
  15077. Add(' TClass = class of TObject;');
  15078. Add('constructor tobject.create; begin end;');
  15079. Add('var');
  15080. Add(' Obj: tobject;');
  15081. Add(' C: tclass;');
  15082. Add('begin');
  15083. Add(' obj:=C.create;');
  15084. Add(' with c do obj:=create;');
  15085. ConvertProgram;
  15086. CheckSource('TestClassOf_Create',
  15087. LinesToStr([ // statements
  15088. 'rtl.createClass($mod, "TObject", null, function () {',
  15089. ' this.$init = function () {',
  15090. ' };',
  15091. ' this.$final = function () {',
  15092. ' };',
  15093. ' this.Create = function () {',
  15094. ' return this;',
  15095. ' };',
  15096. '});',
  15097. 'this.Obj = null;',
  15098. 'this.C = null;'
  15099. ]),
  15100. LinesToStr([ // $mod.$main
  15101. '$mod.Obj = $mod.C.$create("Create");',
  15102. 'var $with1 = $mod.C;',
  15103. '$mod.Obj = $with1.$create("Create");',
  15104. '']));
  15105. end;
  15106. procedure TTestModule.TestClassOf_Call;
  15107. begin
  15108. StartProgram(false);
  15109. Add('type');
  15110. Add(' TObject = class');
  15111. Add(' class procedure DoIt;');
  15112. Add(' end;');
  15113. Add(' TClass = class of TObject;');
  15114. Add('class procedure tobject.doit; begin end;');
  15115. Add('var');
  15116. Add(' C: tclass;');
  15117. Add('begin');
  15118. Add(' c.doit;');
  15119. Add(' with c do doit;');
  15120. ConvertProgram;
  15121. CheckSource('TestClassOf_Call',
  15122. LinesToStr([ // statements
  15123. 'rtl.createClass($mod, "TObject", null, function () {',
  15124. ' this.$init = function () {',
  15125. ' };',
  15126. ' this.$final = function () {',
  15127. ' };',
  15128. ' this.DoIt = function () {',
  15129. ' };',
  15130. '});',
  15131. 'this.C = null;'
  15132. ]),
  15133. LinesToStr([ // $mod.$main
  15134. '$mod.C.DoIt();',
  15135. 'var $with1 = $mod.C;',
  15136. '$with1.DoIt();',
  15137. '']));
  15138. end;
  15139. procedure TTestModule.TestClassOf_Assign;
  15140. begin
  15141. StartProgram(false);
  15142. Add('type');
  15143. Add(' TClass = class of TObject;');
  15144. Add(' TObject = class');
  15145. Add(' ClassType: TClass; ');
  15146. Add(' end;');
  15147. Add('var');
  15148. Add(' Obj: tobject;');
  15149. Add(' C: tclass;');
  15150. Add('begin');
  15151. Add(' c:=nil;');
  15152. Add(' c:=obj.classtype;');
  15153. ConvertProgram;
  15154. CheckSource('TestClassOf_Assign',
  15155. LinesToStr([ // statements
  15156. 'rtl.createClass($mod, "TObject", null, function () {',
  15157. ' this.$init = function () {',
  15158. ' this.ClassType = null;',
  15159. ' };',
  15160. ' this.$final = function () {',
  15161. ' this.ClassType = undefined;',
  15162. ' };',
  15163. '});',
  15164. 'this.Obj = null;',
  15165. 'this.C = null;'
  15166. ]),
  15167. LinesToStr([ // $mod.$main
  15168. '$mod.C = null;',
  15169. '$mod.C = $mod.Obj.ClassType;',
  15170. '']));
  15171. end;
  15172. procedure TTestModule.TestClassOf_Is;
  15173. begin
  15174. StartProgram(false);
  15175. Add('type');
  15176. Add(' TClass = class of TObject;');
  15177. Add(' TObject = class');
  15178. Add(' end;');
  15179. Add(' TCar = class');
  15180. Add(' end;');
  15181. Add(' TCars = class of TCar;');
  15182. Add('var');
  15183. Add(' Obj: tobject;');
  15184. Add(' C: tclass;');
  15185. Add(' Cars: tcars;');
  15186. Add('begin');
  15187. Add(' if c is tcar then ;');
  15188. Add(' if c is tcars then ;');
  15189. ConvertProgram;
  15190. CheckSource('TestClassOf_Is',
  15191. LinesToStr([ // statements
  15192. 'rtl.createClass($mod, "TObject", null, function () {',
  15193. ' this.$init = function () {',
  15194. ' };',
  15195. ' this.$final = function () {',
  15196. ' };',
  15197. '});',
  15198. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  15199. '});',
  15200. 'this.Obj = null;',
  15201. 'this.C = null;',
  15202. 'this.Cars = null;'
  15203. ]),
  15204. LinesToStr([ // $mod.$main
  15205. 'if(rtl.is($mod.C,$mod.TCar));',
  15206. 'if(rtl.is($mod.C,$mod.TCar));',
  15207. '']));
  15208. end;
  15209. procedure TTestModule.TestClassOf_Compare;
  15210. begin
  15211. StartProgram(false);
  15212. Add('type');
  15213. Add(' TClass = class of TObject;');
  15214. Add(' TObject = class');
  15215. Add(' ClassType: TClass; ');
  15216. Add(' end;');
  15217. Add('var');
  15218. Add(' b: boolean;');
  15219. Add(' Obj: tobject;');
  15220. Add(' C: tclass;');
  15221. Add('begin');
  15222. Add(' b:=c=nil;');
  15223. Add(' b:=nil=c;');
  15224. Add(' b:=c=obj.classtype;');
  15225. Add(' b:=obj.classtype=c;');
  15226. Add(' b:=c=TObject;');
  15227. Add(' b:=TObject=c;');
  15228. Add(' b:=c<>nil;');
  15229. Add(' b:=nil<>c;');
  15230. Add(' b:=c<>obj.classtype;');
  15231. Add(' b:=obj.classtype<>c;');
  15232. Add(' b:=c<>TObject;');
  15233. Add(' b:=TObject<>c;');
  15234. ConvertProgram;
  15235. CheckSource('TestClassOf_Compare',
  15236. LinesToStr([ // statements
  15237. 'rtl.createClass($mod, "TObject", null, function () {',
  15238. ' this.$init = function () {',
  15239. ' this.ClassType = null;',
  15240. ' };',
  15241. ' this.$final = function () {',
  15242. ' this.ClassType = undefined;',
  15243. ' };',
  15244. '});',
  15245. 'this.b = false;',
  15246. 'this.Obj = null;',
  15247. 'this.C = null;'
  15248. ]),
  15249. LinesToStr([ // $mod.$main
  15250. '$mod.b = $mod.C === null;',
  15251. '$mod.b = null === $mod.C;',
  15252. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15253. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15254. '$mod.b = $mod.C === $mod.TObject;',
  15255. '$mod.b = $mod.TObject === $mod.C;',
  15256. '$mod.b = $mod.C !== null;',
  15257. '$mod.b = null !== $mod.C;',
  15258. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15259. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15260. '$mod.b = $mod.C !== $mod.TObject;',
  15261. '$mod.b = $mod.TObject !== $mod.C;',
  15262. '']));
  15263. end;
  15264. procedure TTestModule.TestClassOf_ClassVar;
  15265. begin
  15266. StartProgram(false);
  15267. Add('type');
  15268. Add(' TObject = class');
  15269. Add(' class var id: longint;');
  15270. Add(' end;');
  15271. Add(' TClass = class of TObject;');
  15272. Add('var');
  15273. Add(' C: tclass;');
  15274. Add('begin');
  15275. Add(' C.id:=C.id;');
  15276. ConvertProgram;
  15277. CheckSource('TestClassOf_ClassVar',
  15278. LinesToStr([ // statements
  15279. 'rtl.createClass($mod, "TObject", null, function () {',
  15280. ' this.id = 0;',
  15281. ' this.$init = function () {',
  15282. ' };',
  15283. ' this.$final = function () {',
  15284. ' };',
  15285. '});',
  15286. 'this.C = null;'
  15287. ]),
  15288. LinesToStr([ // $mod.$main
  15289. '$mod.TObject.id = $mod.C.id;',
  15290. '']));
  15291. end;
  15292. procedure TTestModule.TestClassOf_ClassMethod;
  15293. begin
  15294. StartProgram(false);
  15295. Add('type');
  15296. Add(' TObject = class');
  15297. Add(' class function DoIt(i: longint = 0): longint;');
  15298. Add(' end;');
  15299. Add(' TClass = class of TObject;');
  15300. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15301. Add('var');
  15302. Add(' i: longint;');
  15303. Add(' C: tclass;');
  15304. Add('begin');
  15305. Add(' C.DoIt;');
  15306. Add(' C.DoIt();');
  15307. Add(' i:=C.DoIt;');
  15308. Add(' i:=C.DoIt();');
  15309. ConvertProgram;
  15310. CheckSource('TestClassOf_ClassMethod',
  15311. LinesToStr([ // statements
  15312. 'rtl.createClass($mod, "TObject", null, function () {',
  15313. ' this.$init = function () {',
  15314. ' };',
  15315. ' this.$final = function () {',
  15316. ' };',
  15317. ' this.DoIt = function (i) {',
  15318. ' var Result = 0;',
  15319. ' return Result;',
  15320. ' };',
  15321. '});',
  15322. 'this.i = 0;',
  15323. 'this.C = null;'
  15324. ]),
  15325. LinesToStr([ // $mod.$main
  15326. '$mod.C.DoIt(0);',
  15327. '$mod.C.DoIt(0);',
  15328. '$mod.i = $mod.C.DoIt(0);',
  15329. '$mod.i = $mod.C.DoIt(0);',
  15330. '']));
  15331. end;
  15332. procedure TTestModule.TestClassOf_ClassProperty;
  15333. begin
  15334. StartProgram(false);
  15335. Add([
  15336. 'type',
  15337. ' TObject = class',
  15338. ' class var FA: longint;',
  15339. ' class function GetA: longint;',
  15340. ' class procedure SetA(Value: longint);',
  15341. ' class property pA: longint read fa write fa;',
  15342. ' class property pB: longint read geta write seta;',
  15343. ' end;',
  15344. ' TObjectClass = class of tobject;',
  15345. 'class function tobject.geta: longint; begin end;',
  15346. 'class procedure tobject.seta(value: longint); begin end;',
  15347. 'var',
  15348. ' b: boolean;',
  15349. ' Obj: tobject;',
  15350. ' Cla: tobjectclass;',
  15351. 'begin',
  15352. ' obj.pa:=obj.pa;',
  15353. ' obj.pb:=obj.pb;',
  15354. ' b:=obj.pa=4;',
  15355. ' b:=obj.pb=obj.pb;',
  15356. ' b:=5=obj.pa;',
  15357. ' cla.pa:=6;',
  15358. ' cla.pa:=cla.pa;',
  15359. ' cla.pb:=cla.pb;',
  15360. ' b:=cla.pa=7;',
  15361. ' b:=cla.pb=cla.pb;',
  15362. ' b:=8=cla.pa;',
  15363. ' tobject.pa:=9;',
  15364. ' tobject.pb:=tobject.pb;',
  15365. ' b:=tobject.pa=10;',
  15366. ' b:=11=tobject.pa;',
  15367. '']);
  15368. ConvertProgram;
  15369. CheckSource('TestClassOf_ClassProperty',
  15370. LinesToStr([ // statements
  15371. 'rtl.createClass($mod, "TObject", null, function () {',
  15372. ' this.FA = 0;',
  15373. ' this.$init = function () {',
  15374. ' };',
  15375. ' this.$final = function () {',
  15376. ' };',
  15377. ' this.GetA = function () {',
  15378. ' var Result = 0;',
  15379. ' return Result;',
  15380. ' };',
  15381. ' this.SetA = function (Value) {',
  15382. ' };',
  15383. '});',
  15384. 'this.b = false;',
  15385. 'this.Obj = null;',
  15386. 'this.Cla = null;'
  15387. ]),
  15388. LinesToStr([ // $mod.$main
  15389. '$mod.TObject.FA = $mod.Obj.FA;',
  15390. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15391. '$mod.b = $mod.Obj.FA === 4;',
  15392. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15393. '$mod.b = 5 === $mod.Obj.FA;',
  15394. '$mod.TObject.FA = 6;',
  15395. '$mod.TObject.FA = $mod.Cla.FA;',
  15396. '$mod.Cla.SetA($mod.Cla.GetA());',
  15397. '$mod.b = $mod.Cla.FA === 7;',
  15398. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15399. '$mod.b = 8 === $mod.Cla.FA;',
  15400. '$mod.TObject.FA = 9;',
  15401. '$mod.TObject.SetA($mod.TObject.GetA());',
  15402. '$mod.b = $mod.TObject.FA === 10;',
  15403. '$mod.b = 11 === $mod.TObject.FA;',
  15404. '']));
  15405. end;
  15406. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15407. begin
  15408. StartProgram(false);
  15409. Add('type');
  15410. Add(' TObject = class');
  15411. Add(' class var GlobalId: longint;');
  15412. Add(' class procedure ProcA;');
  15413. Add(' end;');
  15414. Add('class procedure tobject.proca;');
  15415. Add('var b: boolean;');
  15416. Add('begin');
  15417. Add(' b:=self=nil;');
  15418. Add(' b:=self.globalid=3;');
  15419. Add(' b:=4=self.globalid;');
  15420. Add(' self.globalid:=5;');
  15421. Add(' self.proca;');
  15422. Add('end;');
  15423. Add('begin');
  15424. ConvertProgram;
  15425. CheckSource('TestClassOf_ClassMethodSelf',
  15426. LinesToStr([ // statements
  15427. 'rtl.createClass($mod, "TObject", null, function () {',
  15428. ' this.GlobalId = 0;',
  15429. ' this.$init = function () {',
  15430. ' };',
  15431. ' this.$final = function () {',
  15432. ' };',
  15433. ' this.ProcA = function () {',
  15434. ' var b = false;',
  15435. ' b = this === null;',
  15436. ' b = this.GlobalId === 3;',
  15437. ' b = 4 === this.GlobalId;',
  15438. ' $mod.TObject.GlobalId = 5;',
  15439. ' this.ProcA();',
  15440. ' };',
  15441. '});'
  15442. ]),
  15443. LinesToStr([ // $mod.$main
  15444. '']));
  15445. end;
  15446. procedure TTestModule.TestClassOf_TypeCast;
  15447. begin
  15448. StartProgram(false);
  15449. Add('type');
  15450. Add(' TObject = class');
  15451. Add(' class procedure {#TObject_DoIt}DoIt;');
  15452. Add(' end;');
  15453. Add(' TClass = class of TObject;');
  15454. Add(' TMobile = class');
  15455. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15456. Add(' end;');
  15457. Add(' TMobileClass = class of TMobile;');
  15458. Add(' TCar = class(TMobile)');
  15459. Add(' class procedure {#TCar_DoIt}DoIt;');
  15460. Add(' end;');
  15461. Add(' TCarClass = class of TCar;');
  15462. Add('class procedure TObject.DoIt;');
  15463. Add('begin');
  15464. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15465. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15466. Add('end;');
  15467. Add('class procedure TMobile.DoIt;');
  15468. Add('begin');
  15469. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15470. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15471. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15472. Add('end;');
  15473. Add('class procedure TCar.DoIt; begin end;');
  15474. Add('var');
  15475. Add(' ObjC: TClass;');
  15476. Add(' MobileC: TMobileClass;');
  15477. Add(' CarC: TCarClass;');
  15478. Add('begin');
  15479. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15480. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15481. Add(' CarC.{@TCar_DoIt}DoIt;');
  15482. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15483. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15484. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15485. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15486. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15487. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15488. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15489. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15490. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15491. ConvertProgram;
  15492. CheckSource('TestClassOf_TypeCast',
  15493. LinesToStr([ // statements
  15494. 'rtl.createClass($mod, "TObject", null, function () {',
  15495. ' this.$init = function () {',
  15496. ' };',
  15497. ' this.$final = function () {',
  15498. ' };',
  15499. ' this.DoIt = function () {',
  15500. ' this.DoIt();',
  15501. ' this.DoIt$1();',
  15502. ' };',
  15503. '});',
  15504. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  15505. ' this.DoIt$1 = function () {',
  15506. ' this.DoIt();',
  15507. ' this.DoIt$1();',
  15508. ' this.DoIt$2();',
  15509. ' };',
  15510. '});',
  15511. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  15512. ' this.DoIt$2 = function () {',
  15513. ' };',
  15514. '});',
  15515. 'this.ObjC = null;',
  15516. 'this.MobileC = null;',
  15517. 'this.CarC = null;',
  15518. '']),
  15519. LinesToStr([ // $mod.$main
  15520. '$mod.ObjC.DoIt();',
  15521. '$mod.MobileC.DoIt$1();',
  15522. '$mod.CarC.DoIt$2();',
  15523. '$mod.ObjC.DoIt();',
  15524. '$mod.ObjC.DoIt$1();',
  15525. '$mod.ObjC.DoIt$2();',
  15526. '$mod.MobileC.DoIt();',
  15527. '$mod.MobileC.DoIt$1();',
  15528. '$mod.MobileC.DoIt$2();',
  15529. '$mod.CarC.DoIt();',
  15530. '$mod.CarC.DoIt$1();',
  15531. '$mod.CarC.DoIt$2();',
  15532. '']));
  15533. end;
  15534. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15535. begin
  15536. StartProgram(false);
  15537. Add('type');
  15538. Add(' TObject = class');
  15539. Add(' function CurNow: longint; ');
  15540. Add(' class function Now: longint; ');
  15541. Add(' end;');
  15542. Add('function TObject.CurNow: longint; begin end;');
  15543. Add('class function TObject.Now: longint; begin end;');
  15544. Add('var');
  15545. Add(' Obj: tobject;');
  15546. Add(' vI: longint;');
  15547. Add('begin');
  15548. Add(' obj.curnow;');
  15549. Add(' vi:=obj.curnow;');
  15550. Add(' tobject.now;');
  15551. Add(' vi:=tobject.now;');
  15552. ConvertProgram;
  15553. CheckSource('TestClassOf_ImplicitFunctionCall',
  15554. LinesToStr([ // statements
  15555. 'rtl.createClass($mod, "TObject", null, function () {',
  15556. ' this.$init = function () {',
  15557. ' };',
  15558. ' this.$final = function () {',
  15559. ' };',
  15560. ' this.CurNow = function () {',
  15561. ' var Result = 0;',
  15562. ' return Result;',
  15563. ' };',
  15564. ' this.Now = function () {',
  15565. ' var Result = 0;',
  15566. ' return Result;',
  15567. ' };',
  15568. '});',
  15569. 'this.Obj = null;',
  15570. 'this.vI = 0;',
  15571. '']),
  15572. LinesToStr([ // $mod.$main
  15573. '$mod.Obj.CurNow();',
  15574. '$mod.vI = $mod.Obj.CurNow();',
  15575. '$mod.TObject.Now();',
  15576. '$mod.vI = $mod.TObject.Now();',
  15577. '']));
  15578. end;
  15579. procedure TTestModule.TestClassOf_Const;
  15580. begin
  15581. StartProgram(false);
  15582. Add([
  15583. 'type',
  15584. ' TObject = class',
  15585. ' end;',
  15586. ' TBird = TObject;',
  15587. ' TBirds = class of TBird;',
  15588. ' TEagles = TBirds;',
  15589. ' THawk = class(TBird);',
  15590. 'const',
  15591. ' Hawk: TEagles = THawk;',
  15592. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15593. ' TBird,',
  15594. ' THawk',
  15595. ' );',
  15596. 'begin']);
  15597. ConvertProgram;
  15598. CheckSource('TestClassOf_Const',
  15599. LinesToStr([ // statements
  15600. 'rtl.createClass($mod, "TObject", null, function () {',
  15601. ' this.$init = function () {',
  15602. ' };',
  15603. ' this.$final = function () {',
  15604. ' };',
  15605. '});',
  15606. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  15607. '});',
  15608. 'this.Hawk = $mod.THawk;',
  15609. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  15610. '']),
  15611. LinesToStr([ // $mod.$main
  15612. '']));
  15613. end;
  15614. procedure TTestModule.TestNestedClass_Alias;
  15615. begin
  15616. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15617. StartProgram(false);
  15618. Add([
  15619. 'type',
  15620. ' TObject = class',
  15621. ' type TNested = type longint;',
  15622. ' end;',
  15623. 'type TAlias = type tobject.tnested;',
  15624. 'var i: tobject.tnested = 3;',
  15625. 'var j: TAlias = 4;',
  15626. 'begin',
  15627. ' if typeinfo(TAlias)=nil then ;',
  15628. ' if typeinfo(tobject.tnested)=nil then ;',
  15629. '']);
  15630. ConvertProgram;
  15631. CheckSource('TestNestedClass_Alias',
  15632. LinesToStr([ // statements
  15633. 'rtl.createClass($mod, "TObject", null, function () {',
  15634. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15635. ' this.$init = function () {',
  15636. ' };',
  15637. ' this.$final = function () {',
  15638. ' };',
  15639. '});',
  15640. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15641. 'this.i = 3;',
  15642. 'this.j = 4;',
  15643. '']),
  15644. LinesToStr([ // $mod.$main
  15645. 'if ($mod.$rtti["TAlias"] === null) ;',
  15646. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15647. '']));
  15648. end;
  15649. procedure TTestModule.TestNestedClass_Record;
  15650. begin
  15651. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15652. StartProgram(false);
  15653. Add([
  15654. 'type',
  15655. ' TObject = class',
  15656. ' type TPoint = record',
  15657. ' x,y: byte;',
  15658. ' end;',
  15659. ' procedure DoIt(t: TPoint);',
  15660. ' end;',
  15661. 'procedure tobject.DoIt(t: TPoint);',
  15662. 'var p: TPoint;',
  15663. 'begin',
  15664. ' t.x:=t.y;',
  15665. ' p:=t;',
  15666. 'end;',
  15667. 'var',
  15668. ' p: tobject.tpoint = (x:2; y:4);',
  15669. ' o: TObject;',
  15670. 'begin',
  15671. ' p:=p;',
  15672. ' o.doit(p);',
  15673. '']);
  15674. ConvertProgram;
  15675. CheckSource('TestNestedClass_Record',
  15676. LinesToStr([ // statements
  15677. 'rtl.createClass($mod, "TObject", null, function () {',
  15678. ' rtl.recNewT(this, "TPoint", function () {',
  15679. ' this.x = 0;',
  15680. ' this.y = 0;',
  15681. ' this.$eq = function (b) {',
  15682. ' return (this.x === b.x) && (this.y === b.y);',
  15683. ' };',
  15684. ' this.$assign = function (s) {',
  15685. ' this.x = s.x;',
  15686. ' this.y = s.y;',
  15687. ' return this;',
  15688. ' };',
  15689. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15690. ' $r.addField("x", rtl.byte);',
  15691. ' $r.addField("y", rtl.byte);',
  15692. ' });',
  15693. ' this.$init = function () {',
  15694. ' };',
  15695. ' this.$final = function () {',
  15696. ' };',
  15697. ' this.DoIt = function (t) {',
  15698. ' var p = this.TPoint.$new();',
  15699. ' t.x = t.y;',
  15700. ' p.$assign(t);',
  15701. ' };',
  15702. '});',
  15703. 'this.p = $mod.TObject.TPoint.$clone({',
  15704. ' x: 2,',
  15705. ' y: 4',
  15706. '});',
  15707. 'this.o = null;',
  15708. '']),
  15709. LinesToStr([ // $mod.$main
  15710. '$mod.p.$assign($mod.p);',
  15711. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15712. '']));
  15713. end;
  15714. procedure TTestModule.TestNestedClass_Class;
  15715. begin
  15716. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15717. StartProgram(false);
  15718. Add([
  15719. 'type',
  15720. ' TObject = class end;',
  15721. ' TBird = class',
  15722. ' type TLeg = class',
  15723. ' FId: longint;',
  15724. ' constructor Create;',
  15725. ' function Create(i: longint): TLeg;',
  15726. ' end;',
  15727. ' function DoIt(b: TBird): Tleg;',
  15728. ' end;',
  15729. 'constructor tbird.tleg.create;',
  15730. 'begin',
  15731. ' FId:=3;',
  15732. 'end;',
  15733. 'function tbird.tleg.Create(i: longint): TLeg;',
  15734. 'begin',
  15735. ' Create;',
  15736. ' Result:=TLeg.Create;',
  15737. ' Result:=TBird.TLeg.Create;',
  15738. ' Result:=Create(3);',
  15739. ' FId:=i;',
  15740. 'end;',
  15741. 'function tbird.DoIt(b: tbird): tleg;',
  15742. 'begin',
  15743. ' Result.Create;',
  15744. ' Result:=TLeg.Create;',
  15745. ' Result:=TBird.TLeg.Create;',
  15746. ' Result:=Result.Create(3);',
  15747. 'end;',
  15748. 'var',
  15749. ' b: Tbird.tleg;',
  15750. 'begin',
  15751. ' b.Create;',
  15752. ' b:=TBird.TLeg.Create;',
  15753. ' b:=b.Create(3);',
  15754. '']);
  15755. ConvertProgram;
  15756. CheckSource('TestNestedClass_Class',
  15757. LinesToStr([ // statements
  15758. 'rtl.createClass($mod, "TObject", null, function () {',
  15759. ' this.$init = function () {',
  15760. ' };',
  15761. ' this.$final = function () {',
  15762. ' };',
  15763. '});',
  15764. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15765. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15766. ' this.$init = function () {',
  15767. ' $mod.TObject.$init.call(this);',
  15768. ' this.FId = 0;',
  15769. ' };',
  15770. ' this.Create = function () {',
  15771. ' this.FId = 3;',
  15772. ' return this;',
  15773. ' };',
  15774. ' this.Create$1 = function (i) {',
  15775. ' var Result = null;',
  15776. ' this.Create();',
  15777. ' Result = $mod.TBird.TLeg.$create("Create");',
  15778. ' Result = $mod.TBird.TLeg.$create("Create");',
  15779. ' Result = this.Create$1(3);',
  15780. ' this.FId = i;',
  15781. ' return Result;',
  15782. ' };',
  15783. ' });',
  15784. ' this.DoIt = function (b) {',
  15785. ' var Result = null;',
  15786. ' Result.Create();',
  15787. ' Result = this.TLeg.$create("Create");',
  15788. ' Result = $mod.TBird.TLeg.$create("Create");',
  15789. ' Result = Result.Create$1(3);',
  15790. ' return Result;',
  15791. ' };',
  15792. '});',
  15793. 'this.b = null;',
  15794. '']),
  15795. LinesToStr([ // $mod.$main
  15796. '$mod.b.Create();',
  15797. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15798. '$mod.b = $mod.b.Create$1(3);',
  15799. '']));
  15800. end;
  15801. procedure TTestModule.TestExternalClass_Var;
  15802. begin
  15803. StartProgram(false);
  15804. Add([
  15805. '{$modeswitch externalclass}',
  15806. 'type',
  15807. ' TExtA = class external name ''ExtObj''',
  15808. ' Id: longint external name ''$Id'';',
  15809. ' B: longint;',
  15810. ' end;',
  15811. 'var Obj: TExtA;',
  15812. 'begin',
  15813. ' obj.id:=obj.id+1;',
  15814. ' obj.B:=obj.B+1;']);
  15815. ConvertProgram;
  15816. CheckSource('TestExternalClass_Var',
  15817. LinesToStr([ // statements
  15818. 'this.Obj = null;',
  15819. '']),
  15820. LinesToStr([ // $mod.$main
  15821. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15822. '$mod.Obj.B = $mod.Obj.B + 1;',
  15823. '']));
  15824. end;
  15825. procedure TTestModule.TestExternalClass_Const;
  15826. begin
  15827. StartProgram(false);
  15828. Add([
  15829. '{$modeswitch externalclass}',
  15830. 'type',
  15831. ' TExtA = class external name ''ExtObj''',
  15832. ' const Two: longint = 2;',
  15833. ' const Three = 3;',
  15834. ' const Id: longint;',
  15835. ' end;',
  15836. ' TExtB = class external name ''ExtB''',
  15837. ' A: TExtA;',
  15838. ' end;',
  15839. 'var',
  15840. ' A: texta;',
  15841. ' B: textb;',
  15842. ' i: longint;',
  15843. 'begin',
  15844. ' i:=a.two;',
  15845. ' i:=texta.two;',
  15846. ' i:=a.three;',
  15847. ' i:=texta.three;',
  15848. ' i:=a.id;',
  15849. ' i:=texta.id;',
  15850. '']);
  15851. ConvertProgram;
  15852. CheckSource('TestExternalClass_Const',
  15853. LinesToStr([ // statements
  15854. 'this.A = null;',
  15855. 'this.B = null;',
  15856. 'this.i = 0;',
  15857. '']),
  15858. LinesToStr([ // $mod.$main
  15859. '$mod.i = 2;',
  15860. '$mod.i = 2;',
  15861. '$mod.i = 3;',
  15862. '$mod.i = 3;',
  15863. '$mod.i = $mod.A.Id;',
  15864. '$mod.i = ExtObj.Id;',
  15865. '']));
  15866. end;
  15867. procedure TTestModule.TestExternalClass_Dollar;
  15868. begin
  15869. StartProgram(false);
  15870. Add([
  15871. '{$modeswitch externalclass}',
  15872. 'type',
  15873. ' TExtA = class external name ''$''',
  15874. ' Id: longint external name ''$'';',
  15875. ' function Bla(i: longint): longint; external name ''$'';',
  15876. ' end;',
  15877. 'function dollar(k: longint): longint; external name ''$'';',
  15878. 'var Obj: TExtA;',
  15879. 'begin',
  15880. ' dollar(1);',
  15881. ' obj.id:=obj.id+2;',
  15882. ' obj.Bla(3);',
  15883. '']);
  15884. ConvertProgram;
  15885. CheckSource('TestExternalClass_Dollar',
  15886. LinesToStr([ // statements
  15887. 'this.Obj = null;',
  15888. '']),
  15889. LinesToStr([ // $mod.$main
  15890. '$(1);',
  15891. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15892. '$mod.Obj.$(3);',
  15893. '']));
  15894. end;
  15895. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15896. begin
  15897. StartProgram(false);
  15898. Add('{$modeswitch externalclass}');
  15899. Add('type');
  15900. Add(' TExtA = class external name ''ExtA''');
  15901. Add(' Id: longint external name ''$Id'';');
  15902. Add(' end;');
  15903. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15904. Add(' Id: longint;');
  15905. Add(' end;');
  15906. Add('begin');
  15907. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15908. ConvertProgram;
  15909. end;
  15910. procedure TTestModule.TestExternalClass_Method;
  15911. begin
  15912. StartProgram(false);
  15913. Add(['{$modeswitch externalclass}',
  15914. 'type',
  15915. ' TExtA = class external name ''ExtObj''',
  15916. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15917. ' procedure DoSome(Id: longint = 1);',
  15918. ' end;',
  15919. 'var Obj: texta;',
  15920. 'begin',
  15921. ' obj.doit;',
  15922. ' obj.doit();',
  15923. ' obj.doit(2);',
  15924. ' with obj do begin',
  15925. ' doit;',
  15926. ' doit();',
  15927. ' doit(3);',
  15928. ' end;']);
  15929. ConvertProgram;
  15930. CheckSource('TestExternalClass_Method',
  15931. LinesToStr([ // statements
  15932. 'this.Obj = null;',
  15933. '']),
  15934. LinesToStr([ // $mod.$main
  15935. '$mod.Obj.$Execute(1);',
  15936. '$mod.Obj.$Execute(1);',
  15937. '$mod.Obj.$Execute(2);',
  15938. 'var $with1 = $mod.Obj;',
  15939. '$with1.$Execute(1);',
  15940. '$with1.$Execute(1);',
  15941. '$with1.$Execute(3);',
  15942. '']));
  15943. end;
  15944. procedure TTestModule.TestExternalClass_ClassMethod;
  15945. begin
  15946. StartProgram(false);
  15947. Add([
  15948. '{$modeswitch externalclass}',
  15949. 'type',
  15950. ' TExtA = class external name ''ExtObj''',
  15951. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15952. ' end;',
  15953. ' TExtB = TExtA;',
  15954. 'var p: Pointer;',
  15955. 'begin',
  15956. ' texta.doit;',
  15957. ' texta.doit();',
  15958. ' texta.doit(2);',
  15959. ' p:[email protected];',
  15960. ' with texta do begin',
  15961. ' doit;',
  15962. ' doit();',
  15963. ' doit(3);',
  15964. ' p:=@DoIt;',
  15965. ' end;',
  15966. ' textb.doit;',
  15967. ' textb.doit();',
  15968. ' textb.doit(4);',
  15969. ' with textb do begin',
  15970. ' doit;',
  15971. ' doit();',
  15972. ' doit(5);',
  15973. ' end;',
  15974. '']);
  15975. ConvertProgram;
  15976. CheckSource('TestExternalClass_ClassMethod',
  15977. LinesToStr([ // statements
  15978. 'this.p = null;',
  15979. '']),
  15980. LinesToStr([ // $mod.$main
  15981. 'ExtObj.$Execute(1);',
  15982. 'ExtObj.$Execute(1);',
  15983. 'ExtObj.$Execute(2);',
  15984. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15985. 'ExtObj.$Execute(1);',
  15986. 'ExtObj.$Execute(1);',
  15987. 'ExtObj.$Execute(3);',
  15988. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15989. 'ExtObj.$Execute(1);',
  15990. 'ExtObj.$Execute(1);',
  15991. 'ExtObj.$Execute(4);',
  15992. 'ExtObj.$Execute(1);',
  15993. 'ExtObj.$Execute(1);',
  15994. 'ExtObj.$Execute(5);',
  15995. '']));
  15996. end;
  15997. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  15998. begin
  15999. StartProgram(false);
  16000. Add([
  16001. '{$modeswitch externalclass}',
  16002. 'type',
  16003. ' TExtA = class external name ''ExtObj''',
  16004. ' class procedure DoIt(Id: longint = 1); static;',
  16005. ' end;',
  16006. 'var p: Pointer;',
  16007. 'begin',
  16008. ' texta.doit;',
  16009. ' texta.doit();',
  16010. ' texta.doit(2);',
  16011. ' p:[email protected];',
  16012. ' with texta do begin',
  16013. ' doit;',
  16014. ' doit();',
  16015. ' doit(3);',
  16016. ' p:=@DoIt;',
  16017. ' end;',
  16018. '']);
  16019. ConvertProgram;
  16020. CheckSource('TestExternalClass_ClassMethodStatic',
  16021. LinesToStr([ // statements
  16022. 'this.p = null;',
  16023. '']),
  16024. LinesToStr([ // $mod.$main
  16025. 'ExtObj.DoIt(1);',
  16026. 'ExtObj.DoIt(1);',
  16027. 'ExtObj.DoIt(2);',
  16028. '$mod.p = ExtObj.DoIt;',
  16029. 'ExtObj.DoIt(1);',
  16030. 'ExtObj.DoIt(1);',
  16031. 'ExtObj.DoIt(3);',
  16032. '$mod.p = ExtObj.DoIt;',
  16033. '']));
  16034. end;
  16035. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16036. begin
  16037. StartProgram(false);
  16038. Add([
  16039. '{$modeswitch externalclass}',
  16040. 'type',
  16041. ' TBird = class external name ''Array''',
  16042. ' end;',
  16043. 'function GetPtr: Pointer;',
  16044. 'begin',
  16045. 'end;',
  16046. 'procedure Write(const p);',
  16047. 'begin',
  16048. 'end;',
  16049. 'procedure WriteLn; varargs;',
  16050. 'begin',
  16051. 'end;',
  16052. 'begin',
  16053. ' if TBird(GetPtr)=nil then ;',
  16054. ' Write(GetPtr);',
  16055. ' WriteLn(GetPtr);',
  16056. ' Write(TBird(GetPtr));',
  16057. ' WriteLn(TBird(GetPtr));',
  16058. '']);
  16059. ConvertProgram;
  16060. CheckSource('TestFunctionResultInTypeCast',
  16061. LinesToStr([ // statements
  16062. 'this.GetPtr = function () {',
  16063. ' var Result = null;',
  16064. ' return Result;',
  16065. '};',
  16066. 'this.Write = function (p) {',
  16067. '};',
  16068. 'this.WriteLn = function () {',
  16069. '};',
  16070. '']),
  16071. LinesToStr([
  16072. 'if ($mod.GetPtr() === null) ;',
  16073. '$mod.Write($mod.GetPtr());',
  16074. '$mod.WriteLn($mod.GetPtr());',
  16075. '$mod.Write($mod.GetPtr());',
  16076. '$mod.WriteLn($mod.GetPtr());',
  16077. '']));
  16078. end;
  16079. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16080. begin
  16081. StartProgram(false);
  16082. Add([
  16083. '{$modeswitch externalclass}',
  16084. 'type',
  16085. ' TExtA = class external name ''ExtObjA''',
  16086. ' procedure ProcA; virtual;',
  16087. ' procedure ProcB; virtual;',
  16088. ' end;',
  16089. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16090. ' end;',
  16091. ' TExtC = class (TExtB)',
  16092. ' procedure ProcA; override;',
  16093. ' end;',
  16094. 'procedure TExtC.ProcA;',
  16095. 'begin',
  16096. ' ProcA;',
  16097. ' Self.ProcA;',
  16098. ' ProcB;',
  16099. ' Self.ProcB;',
  16100. 'end;',
  16101. 'var',
  16102. ' A: texta;',
  16103. ' B: textb;',
  16104. ' C: textc;',
  16105. 'begin',
  16106. ' a.proca;',
  16107. ' b.proca;',
  16108. ' c.proca;']);
  16109. ConvertProgram;
  16110. CheckSource('TestExternalClass_NonExternalOverride',
  16111. LinesToStr([ // statements
  16112. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  16113. ' this.$init = function () {',
  16114. ' };',
  16115. ' this.$final = function () {',
  16116. ' };',
  16117. ' this.ProcA = function () {',
  16118. ' this.ProcA();',
  16119. ' this.ProcA();',
  16120. ' this.ProcB();',
  16121. ' this.ProcB();',
  16122. ' };',
  16123. '});',
  16124. 'this.A = null;',
  16125. 'this.B = null;',
  16126. 'this.C = null;',
  16127. '']),
  16128. LinesToStr([ // $mod.$main
  16129. '$mod.A.ProcA();',
  16130. '$mod.B.ProcA();',
  16131. '$mod.C.ProcA();',
  16132. '']));
  16133. end;
  16134. procedure TTestModule.TestExternalClass_OverloadHint;
  16135. begin
  16136. StartProgram(false);
  16137. Add([
  16138. '{$modeswitch externalclass}',
  16139. 'type',
  16140. ' TExtA = class external name ''ExtObjA''',
  16141. ' procedure DoIt;',
  16142. ' procedure DoIt(i: longint);',
  16143. ' end;',
  16144. 'begin',
  16145. '']);
  16146. ConvertProgram;
  16147. CheckResolverUnexpectedHints(true);
  16148. CheckSource('TestExternalClass_OverloadHint',
  16149. LinesToStr([ // statements
  16150. '']),
  16151. LinesToStr([ // $mod.$main
  16152. '']));
  16153. end;
  16154. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16155. begin
  16156. StartProgram(false);
  16157. Add([
  16158. '{$modeswitch externalclass}',
  16159. 'type',
  16160. ' JSwiper = class external name ''Swiper''',
  16161. ' constructor New;',
  16162. ' end;',
  16163. ' TObject = class',
  16164. ' private',
  16165. ' FSwiper: JSwiper;',
  16166. ' published',
  16167. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16168. ' end;',
  16169. 'begin',
  16170. ' JSwiper.new;',
  16171. '']);
  16172. ConvertProgram;
  16173. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16174. LinesToStr([ // statements
  16175. 'rtl.createClass($mod, "TObject", null, function () {',
  16176. ' this.$init = function () {',
  16177. ' this.FSwiper = null;',
  16178. ' };',
  16179. ' this.$final = function () {',
  16180. ' this.FSwiper = undefined;',
  16181. ' };',
  16182. ' var $r = this.$rtti;',
  16183. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16184. '});',
  16185. '']),
  16186. LinesToStr([ // $mod.$main
  16187. 'new Swiper();',
  16188. '']));
  16189. end;
  16190. procedure TTestModule.TestExternalClass_Property;
  16191. begin
  16192. StartProgram(false);
  16193. Add([
  16194. '{$modeswitch externalclass}',
  16195. 'type',
  16196. ' TExtA = class external name ''ExtA''',
  16197. ' function getYear: longint;',
  16198. ' procedure setYear(Value: longint);',
  16199. ' property Year: longint read getyear write setyear;',
  16200. ' end;',
  16201. ' TExtB = class (TExtA)',
  16202. ' procedure OtherSetYear(Value: longint);',
  16203. ' property year write othersetyear;',
  16204. ' end;',
  16205. 'procedure textb.othersetyear(value: longint);',
  16206. 'begin',
  16207. ' setYear(Value+4);',
  16208. 'end;',
  16209. 'var',
  16210. ' A: texta;',
  16211. ' B: textb;',
  16212. 'begin',
  16213. ' a.year:=a.year+1;',
  16214. ' b.year:=b.year+2;']);
  16215. ConvertProgram;
  16216. CheckSource('TestExternalClass_NonExternalOverride',
  16217. LinesToStr([ // statements
  16218. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16219. ' this.$init = function () {',
  16220. ' };',
  16221. ' this.$final = function () {',
  16222. ' };',
  16223. ' this.OtherSetYear = function (Value) {',
  16224. ' this.setYear(Value+4);',
  16225. ' };',
  16226. '});',
  16227. 'this.A = null;',
  16228. 'this.B = null;',
  16229. '']),
  16230. LinesToStr([ // $mod.$main
  16231. '$mod.A.setYear($mod.A.getYear()+1);',
  16232. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16233. '']));
  16234. end;
  16235. procedure TTestModule.TestExternalClass_PropertyDate;
  16236. begin
  16237. StartProgram(false);
  16238. Add([
  16239. '{$modeswitch externalclass}',
  16240. 'type',
  16241. ' TExtA = class external name ''ExtA''',
  16242. ' end;',
  16243. ' TExtB = class (TExtA)',
  16244. ' FDate: string;',
  16245. ' property Date: string read FDate write FDate;',
  16246. ' property ExtA: string read FDate write FDate;',
  16247. ' end;',
  16248. ' {$M+}',
  16249. ' TObject = class',
  16250. ' FDate: string;',
  16251. ' published',
  16252. ' property Date: string read FDate write FDate;',
  16253. ' property ExtA: string read FDate write FDate;',
  16254. ' end;',
  16255. 'var',
  16256. ' B: textb;',
  16257. ' o: TObject;',
  16258. 'begin',
  16259. ' b.date:=b.exta;',
  16260. ' o.date:=o.exta;']);
  16261. ConvertProgram;
  16262. CheckSource('TestExternalClass_PropertyDate',
  16263. LinesToStr([ // statements
  16264. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16265. ' this.$init = function () {',
  16266. ' this.FDate = "";',
  16267. ' };',
  16268. ' this.$final = function () {',
  16269. ' };',
  16270. '});',
  16271. 'rtl.createClass($mod, "TObject", null, function () {',
  16272. ' this.$init = function () {',
  16273. ' this.FDate = "";',
  16274. ' };',
  16275. ' this.$final = function () {',
  16276. ' };',
  16277. ' var $r = this.$rtti;',
  16278. ' $r.addField("FDate", rtl.string);',
  16279. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16280. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16281. '});',
  16282. 'this.B = null;',
  16283. 'this.o = null;',
  16284. '']),
  16285. LinesToStr([ // $mod.$main
  16286. '$mod.B.FDate = $mod.B.FDate;',
  16287. '$mod.o.FDate = $mod.o.FDate;',
  16288. '']));
  16289. end;
  16290. procedure TTestModule.TestExternalClass_ClassProperty;
  16291. begin
  16292. StartProgram(false);
  16293. Add('{$modeswitch externalclass}');
  16294. Add('type');
  16295. Add(' TExtA = class external name ''ExtA''');
  16296. Add(' class function getYear: longint;');
  16297. Add(' class procedure setYear(Value: longint);');
  16298. Add(' class property Year: longint read getyear write setyear;');
  16299. Add(' end;');
  16300. Add(' TExtB = class (TExtA)');
  16301. Add(' class function GetCentury: longint;');
  16302. Add(' class procedure SetCentury(Value: longint);');
  16303. Add(' class property Century: longint read getcentury write setcentury;');
  16304. Add(' end;');
  16305. Add('class function textb.getcentury: longint;');
  16306. Add('begin');
  16307. Add('end;');
  16308. Add('class procedure textb.setcentury(value: longint);');
  16309. Add('begin');
  16310. Add(' setyear(value+11);');
  16311. Add(' texta.year:=texta.year+12;');
  16312. Add(' year:=year+13;');
  16313. Add(' textb.century:=textb.century+14;');
  16314. Add(' century:=century+15;');
  16315. Add('end;');
  16316. Add('var');
  16317. Add(' A: texta;');
  16318. Add(' B: textb;');
  16319. Add('begin');
  16320. Add(' texta.year:=texta.year+1;');
  16321. Add(' textb.year:=textb.year+2;');
  16322. Add(' TextA.year:=TextA.year+3;');
  16323. Add(' b.year:=b.year+4;');
  16324. Add(' textb.century:=textb.century+5;');
  16325. Add(' b.century:=b.century+6;');
  16326. ConvertProgram;
  16327. CheckSource('TestExternalClass_ClassProperty',
  16328. LinesToStr([ // statements
  16329. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16330. ' this.$init = function () {',
  16331. ' };',
  16332. ' this.$final = function () {',
  16333. ' };',
  16334. ' this.GetCentury = function () {',
  16335. ' var Result = 0;',
  16336. ' return Result;',
  16337. ' };',
  16338. ' this.SetCentury = function (Value) {',
  16339. ' this.setYear(Value + 11);',
  16340. ' ExtA.setYear(ExtA.getYear() + 12);',
  16341. ' this.setYear(this.getYear() + 13);',
  16342. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16343. ' this.SetCentury(this.GetCentury() + 15);',
  16344. ' };',
  16345. '});',
  16346. 'this.A = null;',
  16347. 'this.B = null;',
  16348. '']),
  16349. LinesToStr([ // $mod.$main
  16350. 'ExtA.setYear(ExtA.getYear() + 1);',
  16351. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16352. 'ExtA.setYear(ExtA.getYear() + 3);',
  16353. '$mod.B.setYear($mod.B.getYear() + 4);',
  16354. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16355. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16356. '']));
  16357. end;
  16358. procedure TTestModule.TestExternalClass_ClassOf;
  16359. begin
  16360. StartProgram(false);
  16361. Add('{$modeswitch externalclass}');
  16362. Add('type');
  16363. Add(' TExtA = class external name ''ExtA''');
  16364. Add(' procedure ProcA; virtual;');
  16365. Add(' procedure ProcB; virtual;');
  16366. Add(' end;');
  16367. Add(' TExtAClass = class of TExtA;');
  16368. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16369. Add(' end;');
  16370. Add(' TExtBClass = class of TExtB;');
  16371. Add(' TExtC = class (TExtB)');
  16372. Add(' procedure ProcA; override;');
  16373. Add(' end;');
  16374. Add(' TExtCClass = class of TExtC;');
  16375. Add('procedure TExtC.ProcA; begin end;');
  16376. Add('var');
  16377. Add(' A: texta; ClA: TExtAClass;');
  16378. Add(' B: textb; ClB: TExtBClass;');
  16379. Add(' C: textc; ClC: TExtCClass;');
  16380. Add('begin');
  16381. Add(' ClA:=texta;');
  16382. Add(' ClA:=textb;');
  16383. Add(' ClA:=textc;');
  16384. Add(' ClB:=textb;');
  16385. Add(' ClB:=textc;');
  16386. Add(' ClC:=textc;');
  16387. ConvertProgram;
  16388. CheckSource('TestExternalClass_ClassOf',
  16389. LinesToStr([ // statements
  16390. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16391. ' this.$init = function () {',
  16392. ' };',
  16393. ' this.$final = function () {',
  16394. ' };',
  16395. ' this.ProcA = function () {',
  16396. ' };',
  16397. '});',
  16398. 'this.A = null;',
  16399. 'this.ClA = null;',
  16400. 'this.B = null;',
  16401. 'this.ClB = null;',
  16402. 'this.C = null;',
  16403. 'this.ClC = null;',
  16404. '']),
  16405. LinesToStr([ // $mod.$main
  16406. '$mod.ClA = ExtA;',
  16407. '$mod.ClA = ExtB;',
  16408. '$mod.ClA = $mod.TExtC;',
  16409. '$mod.ClB = ExtB;',
  16410. '$mod.ClB = $mod.TExtC;',
  16411. '$mod.ClC = $mod.TExtC;',
  16412. '']));
  16413. end;
  16414. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16415. begin
  16416. AddModuleWithIntfImplSrc('unit2.pas',
  16417. LinesToStr([
  16418. '{$modeswitch externalclass}',
  16419. 'type',
  16420. ' TExtA = class external name ''ExtA''',
  16421. ' class var Id: longint;',
  16422. ' end;',
  16423. '']),
  16424. '');
  16425. StartUnit(true);
  16426. Add('interface');
  16427. Add('uses unit2;');
  16428. Add('implementation');
  16429. Add('begin');
  16430. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16431. ConvertUnit;
  16432. CheckSource('TestExternalClass_ClassOtherUnit',
  16433. LinesToStr([
  16434. '']),
  16435. LinesToStr([
  16436. 'ExtA.Id = ExtA.Id + 1;',
  16437. '']));
  16438. end;
  16439. procedure TTestModule.TestExternalClass_Is;
  16440. begin
  16441. StartProgram(false);
  16442. Add('{$modeswitch externalclass}');
  16443. Add('type');
  16444. Add(' TExtA = class external name ''ExtA''');
  16445. Add(' end;');
  16446. Add(' TExtAClass = class of TExtA;');
  16447. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16448. Add(' end;');
  16449. Add(' TExtBClass = class of TExtB;');
  16450. Add(' TExtC = class (TExtB)');
  16451. Add(' end;');
  16452. Add(' TExtCClass = class of TExtC;');
  16453. Add('var');
  16454. Add(' A: texta; ClA: TExtAClass;');
  16455. Add(' B: textb; ClB: TExtBClass;');
  16456. Add(' C: textc; ClC: TExtCClass;');
  16457. Add('begin');
  16458. Add(' if a is textb then ;');
  16459. Add(' if a is textc then ;');
  16460. Add(' if b is textc then ;');
  16461. Add(' if cla is textb then ;');
  16462. Add(' if cla is textc then ;');
  16463. Add(' if clb is textc then ;');
  16464. ConvertProgram;
  16465. CheckSource('TestExternalClass_Is',
  16466. LinesToStr([ // statements
  16467. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16468. ' this.$init = function () {',
  16469. ' };',
  16470. ' this.$final = function () {',
  16471. ' };',
  16472. '});',
  16473. 'this.A = null;',
  16474. 'this.ClA = null;',
  16475. 'this.B = null;',
  16476. 'this.ClB = null;',
  16477. 'this.C = null;',
  16478. 'this.ClC = null;',
  16479. '']),
  16480. LinesToStr([ // $mod.$main
  16481. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16482. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16483. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16484. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16485. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16486. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16487. '']));
  16488. end;
  16489. procedure TTestModule.TestExternalClass_As;
  16490. begin
  16491. StartProgram(false);
  16492. Add('{$modeswitch externalclass}');
  16493. Add('type');
  16494. Add(' TExtA = class external name ''ExtA''');
  16495. Add(' end;');
  16496. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16497. Add(' end;');
  16498. Add(' TExtC = class (TExtB)');
  16499. Add(' end;');
  16500. Add('var');
  16501. Add(' A: texta;');
  16502. Add(' B: textb;');
  16503. Add(' C: textc;');
  16504. Add('begin');
  16505. Add(' b:=a as textb;');
  16506. Add(' c:=a as textc;');
  16507. Add(' c:=b as textc;');
  16508. ConvertProgram;
  16509. CheckSource('TestExternalClass_Is',
  16510. LinesToStr([ // statements
  16511. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16512. ' this.$init = function () {',
  16513. ' };',
  16514. ' this.$final = function () {',
  16515. ' };',
  16516. '});',
  16517. 'this.A = null;',
  16518. 'this.B = null;',
  16519. 'this.C = null;',
  16520. '']),
  16521. LinesToStr([ // $mod.$main
  16522. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16523. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16524. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16525. '']));
  16526. end;
  16527. procedure TTestModule.TestExternalClass_DestructorFail;
  16528. begin
  16529. StartProgram(false);
  16530. Add('{$modeswitch externalclass}');
  16531. Add('type');
  16532. Add(' TExtA = class external name ''ExtA''');
  16533. Add(' destructor Free;');
  16534. Add(' end;');
  16535. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16536. nPasElementNotSupported);
  16537. ConvertProgram;
  16538. end;
  16539. procedure TTestModule.TestExternalClass_New;
  16540. begin
  16541. StartProgram(false);
  16542. Add([
  16543. '{$modeswitch externalclass}',
  16544. 'type',
  16545. ' TExtA = class external name ''ExtA''',
  16546. ' constructor New;',
  16547. ' constructor New(i: longint; j: longint = 2);',
  16548. ' end;',
  16549. 'var',
  16550. ' A: texta;',
  16551. 'begin',
  16552. ' a:=texta.new;',
  16553. ' a:=texta(texta.new);',
  16554. ' a:=texta.new();',
  16555. ' a:=texta.new(1);',
  16556. ' with texta do begin',
  16557. ' a:=new;',
  16558. ' a:=new();',
  16559. ' a:=new(2);',
  16560. ' end;',
  16561. ' a:=test1.texta.new;',
  16562. ' a:=test1.texta.new();',
  16563. ' a:=test1.texta.new(3);',
  16564. '']);
  16565. ConvertProgram;
  16566. CheckSource('TestExternalClass_New',
  16567. LinesToStr([ // statements
  16568. 'this.A = null;',
  16569. '']),
  16570. LinesToStr([ // $mod.$main
  16571. '$mod.A = new ExtA();',
  16572. '$mod.A = new ExtA();',
  16573. '$mod.A = new ExtA();',
  16574. '$mod.A = new ExtA(1,2);',
  16575. '$mod.A = new ExtA();',
  16576. '$mod.A = new ExtA();',
  16577. '$mod.A = new ExtA(2,2);',
  16578. '$mod.A = new ExtA();',
  16579. '$mod.A = new ExtA();',
  16580. '$mod.A = new ExtA(3,2);',
  16581. '']));
  16582. end;
  16583. procedure TTestModule.TestExternalClass_ClassOf_New;
  16584. begin
  16585. StartProgram(false);
  16586. Add('{$modeswitch externalclass}');
  16587. Add('type');
  16588. Add(' TExtAClass = class of TExtA;');
  16589. Add(' TExtA = class external name ''ExtA''');
  16590. Add(' C: TExtAClass;');
  16591. Add(' constructor New;');
  16592. Add(' end;');
  16593. Add('var');
  16594. Add(' A: texta;');
  16595. Add(' C: textaclass;');
  16596. Add('begin');
  16597. Add(' a:=c.new;');
  16598. Add(' a:=c.new();');
  16599. Add(' with C do begin');
  16600. Add(' a:=new;');
  16601. Add(' a:=new();');
  16602. Add(' end;');
  16603. Add(' a:=test1.c.new;');
  16604. Add(' a:=test1.c.new();');
  16605. Add(' a:=A.c.new();');
  16606. ConvertProgram;
  16607. CheckSource('TestExternalClass_ClassOf_New',
  16608. LinesToStr([ // statements
  16609. 'this.A = null;',
  16610. 'this.C = null;',
  16611. '']),
  16612. LinesToStr([ // $mod.$main
  16613. '$mod.A = new $mod.C();',
  16614. '$mod.A = new $mod.C();',
  16615. 'var $with1 = $mod.C;',
  16616. '$mod.A = new $with1();',
  16617. '$mod.A = new $with1();',
  16618. '$mod.A = new $mod.C();',
  16619. '$mod.A = new $mod.C();',
  16620. '$mod.A = new $mod.A.C();',
  16621. '']));
  16622. end;
  16623. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16624. begin
  16625. StartProgram(false);
  16626. Add([
  16627. '{$modeswitch externalclass}',
  16628. 'type',
  16629. ' TExtAClass = class of TExtA;',
  16630. ' TExtA = class external name ''ExtA''',
  16631. ' constructor New;',
  16632. ' end;',
  16633. 'function GetCreator: TExtAClass;',
  16634. 'begin',
  16635. ' Result:=TExtA;',
  16636. 'end;',
  16637. 'var',
  16638. ' A: texta;',
  16639. 'begin',
  16640. ' a:=getcreator.new;',
  16641. ' a:=getcreator().new;',
  16642. ' a:=getcreator().new();',
  16643. ' a:=getcreator.new();',
  16644. ' with getcreator do begin',
  16645. ' a:=new;',
  16646. ' a:=new();',
  16647. ' end;']);
  16648. ConvertProgram;
  16649. CheckSource('TestExternalClass_FuncClassOf_New',
  16650. LinesToStr([ // statements
  16651. 'this.GetCreator = function () {',
  16652. ' var Result = null;',
  16653. ' Result = ExtA;',
  16654. ' return Result;',
  16655. '};',
  16656. 'this.A = null;',
  16657. '']),
  16658. LinesToStr([ // $mod.$main
  16659. '$mod.A = new ($mod.GetCreator())();',
  16660. '$mod.A = new ($mod.GetCreator())();',
  16661. '$mod.A = new ($mod.GetCreator())();',
  16662. '$mod.A = new ($mod.GetCreator())();',
  16663. 'var $with1 = $mod.GetCreator();',
  16664. '$mod.A = new $with1();',
  16665. '$mod.A = new $with1();',
  16666. '']));
  16667. end;
  16668. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16669. begin
  16670. StartProgram(false);
  16671. Add([
  16672. '{$modeswitch externalclass}',
  16673. 'type',
  16674. ' TExtA = class external name ''ExtA''',
  16675. ' constructor New;',
  16676. ' end;',
  16677. ' TBird = class(TExtA)',
  16678. ' end;',
  16679. 'begin',
  16680. ' TBird.new;',
  16681. '']);
  16682. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16683. ConvertProgram;
  16684. end;
  16685. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16686. begin
  16687. StartProgram(false);
  16688. Add([
  16689. '{$modeswitch externalclass}',
  16690. 'type',
  16691. ' TExtA = class external name ''ExtA''',
  16692. ' constructor New;',
  16693. ' end;',
  16694. ' TBird = class(TExtA)',
  16695. ' end;',
  16696. 'begin',
  16697. ' TBird.new();',
  16698. '']);
  16699. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16700. ConvertProgram;
  16701. end;
  16702. procedure TTestModule.TestExternalClass_NewExtName;
  16703. begin
  16704. StartProgram(false);
  16705. Add([
  16706. '{$modeswitch externalclass}',
  16707. 'type',
  16708. ' TExtA = class external name ''ExtA''',
  16709. ' constructor New; external name ''Other'';',
  16710. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16711. ' end;',
  16712. 'var',
  16713. ' A: texta;',
  16714. 'begin',
  16715. ' a:=texta.new;',
  16716. ' a:=texta(texta.new);',
  16717. ' a:=texta.new();',
  16718. ' a:=texta.new(1);',
  16719. ' with texta do begin',
  16720. ' a:=new;',
  16721. ' a:=new();',
  16722. ' a:=new(2);',
  16723. ' end;',
  16724. ' a:=test1.texta.new;',
  16725. ' a:=test1.texta.new();',
  16726. ' a:=test1.texta.new(3);',
  16727. '']);
  16728. ConvertProgram;
  16729. CheckSource('TestExternalClass_NewExtName',
  16730. LinesToStr([ // statements
  16731. 'this.A = null;',
  16732. '']),
  16733. LinesToStr([ // $mod.$main
  16734. '$mod.A = new Other();',
  16735. '$mod.A = new Other();',
  16736. '$mod.A = new Other();',
  16737. '$mod.A = new A.B(1,2);',
  16738. '$mod.A = new Other();',
  16739. '$mod.A = new Other();',
  16740. '$mod.A = new A.B(2,2);',
  16741. '$mod.A = new Other();',
  16742. '$mod.A = new Other();',
  16743. '$mod.A = new A.B(3,2);',
  16744. '']));
  16745. end;
  16746. procedure TTestModule.TestExternalClass_Constructor;
  16747. begin
  16748. StartProgram(false);
  16749. Add([
  16750. '{$modeswitch externalclass}',
  16751. 'type',
  16752. ' TExtA = class external name ''ExtA''',
  16753. ' constructor Create;',
  16754. ' constructor Create(i: longint; j: longint = 2);',
  16755. ' end;',
  16756. 'var',
  16757. ' A: texta;',
  16758. 'begin',
  16759. ' a:=texta.create;',
  16760. ' a:=texta(texta.create);',
  16761. ' a:=texta.create();',
  16762. ' a:=texta.create(1);',
  16763. ' with texta do begin',
  16764. ' a:=create;',
  16765. ' a:=create();',
  16766. ' a:=create(2);',
  16767. ' end;',
  16768. ' a:=test1.texta.create;',
  16769. ' a:=test1.texta.create();',
  16770. ' a:=test1.texta.create(3);',
  16771. '']);
  16772. ConvertProgram;
  16773. CheckSource('TestExternalClass_Constructor',
  16774. LinesToStr([ // statements
  16775. 'this.A = null;',
  16776. '']),
  16777. LinesToStr([ // $mod.$main
  16778. '$mod.A = new ExtA.Create();',
  16779. '$mod.A = new ExtA.Create();',
  16780. '$mod.A = new ExtA.Create();',
  16781. '$mod.A = new ExtA.Create(1,2);',
  16782. '$mod.A = new ExtA.Create();',
  16783. '$mod.A = new ExtA.Create();',
  16784. '$mod.A = new ExtA.Create(2,2);',
  16785. '$mod.A = new ExtA.Create();',
  16786. '$mod.A = new ExtA.Create();',
  16787. '$mod.A = new ExtA.Create(3,2);',
  16788. '']));
  16789. end;
  16790. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  16791. begin
  16792. StartProgram(false);
  16793. Add([
  16794. '{$modeswitch externalclass}',
  16795. 'type',
  16796. ' TExtA = class external name ''ExtA''',
  16797. ' constructor Create; external name ''{}'';',
  16798. ' end;',
  16799. 'var',
  16800. ' A: texta;',
  16801. 'begin',
  16802. ' a:=texta.create;',
  16803. ' a:=texta(texta.create);',
  16804. ' a:=texta.create();',
  16805. ' with texta do begin',
  16806. ' a:=create;',
  16807. ' a:=create();',
  16808. ' end;',
  16809. ' a:=test1.texta.create;',
  16810. ' a:=test1.texta.create();',
  16811. '']);
  16812. ConvertProgram;
  16813. CheckSource('TestExternalClass_ConstructorBrackets',
  16814. LinesToStr([ // statements
  16815. 'this.A = null;',
  16816. '']),
  16817. LinesToStr([ // $mod.$main
  16818. '$mod.A = {};',
  16819. '$mod.A = {};',
  16820. '$mod.A = {};',
  16821. '$mod.A = {};',
  16822. '$mod.A = {};',
  16823. '$mod.A = {};',
  16824. '$mod.A = {};',
  16825. '']));
  16826. end;
  16827. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16828. begin
  16829. StartProgram(false);
  16830. Add('{$modeswitch externalclass}');
  16831. Add('type');
  16832. Add(' TExtA = class external name ''ExtA''');
  16833. Add(' constructor New;');
  16834. Add(' end;');
  16835. Add('function DoIt: longint;');
  16836. Add('const ExtA: longint = 3;');
  16837. Add('begin');
  16838. Add(' Result:=ExtA;');
  16839. Add('end;');
  16840. Add('var');
  16841. Add(' A: texta;');
  16842. Add('begin');
  16843. Add(' a:=texta.new;');
  16844. ConvertProgram;
  16845. CheckSource('TestExternalClass_LocalConstSameName',
  16846. LinesToStr([ // statements
  16847. 'var ExtA$1 = 3;',
  16848. 'this.DoIt = function () {',
  16849. ' var Result = 0;',
  16850. ' Result = ExtA$1;',
  16851. ' return Result;',
  16852. '};',
  16853. 'this.A = null;',
  16854. '']),
  16855. LinesToStr([ // $mod.$main
  16856. '$mod.A = new ExtA();',
  16857. '']));
  16858. end;
  16859. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16860. begin
  16861. StartProgram(false);
  16862. Add('{$modeswitch externalclass}');
  16863. Add('type');
  16864. Add(' TExtA = class external name ''ExtA''');
  16865. Add(' procedure DoIt;');
  16866. Add(' end;');
  16867. Add(' TMyA = class(TExtA)');
  16868. Add(' procedure DoIt;');
  16869. Add(' end;');
  16870. Add('procedure TMyA.DoIt; begin end;');
  16871. Add('begin');
  16872. ConvertProgram;
  16873. CheckSource('TestExternalClass_ReintroduceOverload',
  16874. LinesToStr([ // statements
  16875. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16876. ' this.$init = function () {',
  16877. ' };',
  16878. ' this.$final = function () {',
  16879. ' };',
  16880. ' this.DoIt$1 = function () {',
  16881. ' };',
  16882. '});',
  16883. '']),
  16884. LinesToStr([ // $mod.$main
  16885. '']));
  16886. end;
  16887. procedure TTestModule.TestExternalClass_Inherited;
  16888. begin
  16889. StartProgram(false);
  16890. Add('{$modeswitch externalclass}');
  16891. Add('type');
  16892. Add(' TExtA = class external name ''ExtA''');
  16893. Add(' procedure DoIt(i: longint = 1); virtual;');
  16894. Add(' procedure DoSome(j: longint = 2);');
  16895. Add(' end;');
  16896. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16897. Add(' end;');
  16898. Add(' TMyC = class(TExtB)');
  16899. Add(' procedure DoIt(i: longint = 1); override;');
  16900. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16901. Add(' end;');
  16902. Add('procedure TMyC.DoIt(i: longint);');
  16903. Add('begin');
  16904. Add(' inherited;');
  16905. Add(' inherited DoIt;');
  16906. Add(' inherited DoIt();');
  16907. Add(' inherited DoIt(3);');
  16908. Add(' inherited DoSome;');
  16909. Add(' inherited DoSome();');
  16910. Add(' inherited DoSome(4);');
  16911. Add('end;');
  16912. Add('procedure TMyC.DoSome(j: longint);');
  16913. Add('begin');
  16914. Add(' inherited;');
  16915. Add('end;');
  16916. Add('begin');
  16917. ConvertProgram;
  16918. CheckSource('TestExternalClass_ReintroduceOverload',
  16919. LinesToStr([ // statements
  16920. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16921. ' this.$init = function () {',
  16922. ' };',
  16923. ' this.$final = function () {',
  16924. ' };',
  16925. ' this.DoIt = function (i) {',
  16926. ' ExtB.DoIt.apply(this, arguments);',
  16927. ' ExtB.DoIt.call(this, 1);',
  16928. ' ExtB.DoIt.call(this, 1);',
  16929. ' ExtB.DoIt.call(this, 3);',
  16930. ' ExtB.DoSome.call(this, 2);',
  16931. ' ExtB.DoSome.call(this, 2);',
  16932. ' ExtB.DoSome.call(this, 4);',
  16933. ' };',
  16934. ' this.DoSome$1 = function (j) {',
  16935. ' ExtB.DoSome.apply(this, arguments);',
  16936. ' };',
  16937. '});',
  16938. '']),
  16939. LinesToStr([ // $mod.$main
  16940. '']));
  16941. end;
  16942. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16943. begin
  16944. StartProgram(false);
  16945. Add('{$modeswitch externalclass}');
  16946. Add('type');
  16947. Add(' TObject = class');
  16948. Add(' end;');
  16949. Add(' TExtA = class external name ''ExtA''(TObject)');
  16950. Add(' end;');
  16951. Add('begin');
  16952. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16953. ConvertProgram;
  16954. end;
  16955. procedure TTestModule.TestExternalClass_NewInstance;
  16956. begin
  16957. StartProgram(false);
  16958. Add('{$modeswitch externalclass}');
  16959. Add('type');
  16960. Add(' TExtA = class external name ''ExtA''');
  16961. Add(' end;');
  16962. Add(' TMyB = class(TExtA)');
  16963. Add(' protected');
  16964. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  16965. Add(' end;');
  16966. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16967. Add('begin end;');
  16968. Add('begin');
  16969. ConvertProgram;
  16970. CheckSource('TestExternalClass_NewInstance',
  16971. LinesToStr([ // statements
  16972. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  16973. ' this.$init = function () {',
  16974. ' };',
  16975. ' this.$final = function () {',
  16976. ' };',
  16977. ' this.NewInstance = function (fnname, paramarray) {',
  16978. ' var Result = null;',
  16979. ' return Result;',
  16980. ' };',
  16981. '});',
  16982. '']),
  16983. LinesToStr([ // $mod.$main
  16984. '']));
  16985. end;
  16986. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  16987. begin
  16988. StartProgram(false);
  16989. Add('{$modeswitch externalclass}');
  16990. Add('type');
  16991. Add(' TExtA = class external name ''ExtA''');
  16992. Add(' end;');
  16993. Add(' TMyB = class(TExtA)');
  16994. Add(' protected');
  16995. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  16996. Add(' end;');
  16997. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16998. Add('begin end;');
  16999. Add('begin');
  17000. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17001. ConvertProgram;
  17002. end;
  17003. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17004. begin
  17005. StartProgram(false);
  17006. Add('{$modeswitch externalclass}');
  17007. Add('type');
  17008. Add(' TExtA = class external name ''ExtA''');
  17009. Add(' end;');
  17010. Add(' TMyB = class(TExtA)');
  17011. Add(' protected');
  17012. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17013. Add(' end;');
  17014. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17015. Add('begin end;');
  17016. Add('begin');
  17017. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17018. nIncompatibleTypeArgNo);
  17019. ConvertProgram;
  17020. end;
  17021. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17022. begin
  17023. StartProgram(false);
  17024. Add('{$modeswitch externalclass}');
  17025. Add('type');
  17026. Add(' TExtA = class external name ''ExtA''');
  17027. Add(' end;');
  17028. Add(' TMyB = class(TExtA)');
  17029. Add(' protected');
  17030. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17031. Add(' end;');
  17032. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17033. Add('begin end;');
  17034. Add('begin');
  17035. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17036. nIncompatibleTypeArgNo);
  17037. ConvertProgram;
  17038. end;
  17039. procedure TTestModule.TestExternalClass_PascalProperty;
  17040. begin
  17041. StartProgram(false);
  17042. Add('{$modeswitch externalclass}');
  17043. Add('type');
  17044. Add(' TJSElement = class;');
  17045. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17046. Add(' TJSElement = class external name ''ExtA''');
  17047. Add(' end;');
  17048. Add(' TControl = class(TJSElement)');
  17049. Add(' private');
  17050. Add(' FOnClick: TJSNotifyEvent;');
  17051. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17052. Add(' procedure Click(Sender: TJSElement);');
  17053. Add(' end;');
  17054. Add('procedure TControl.Click(Sender: TJSElement);');
  17055. Add('begin');
  17056. Add(' OnClick(Self);');
  17057. Add('end;');
  17058. Add('var');
  17059. Add(' Ctrl: TControl;');
  17060. Add('begin');
  17061. Add(' Ctrl.OnClick:[email protected];');
  17062. Add(' Ctrl.OnClick(Ctrl);');
  17063. ConvertProgram;
  17064. CheckSource('TestExternalClass_PascalProperty',
  17065. LinesToStr([ // statements
  17066. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  17067. ' this.$init = function () {',
  17068. ' this.FOnClick = null;',
  17069. ' };',
  17070. ' this.$final = function () {',
  17071. ' this.FOnClick = undefined;',
  17072. ' };',
  17073. ' this.Click = function (Sender) {',
  17074. ' this.FOnClick(this);',
  17075. ' };',
  17076. '});',
  17077. 'this.Ctrl = null;',
  17078. '']),
  17079. LinesToStr([ // $mod.$main
  17080. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17081. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17082. '']));
  17083. end;
  17084. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17085. begin
  17086. StartProgram(false);
  17087. Add([
  17088. '{$modeswitch externalclass}',
  17089. 'type',
  17090. ' IUnknown = interface end;',
  17091. ' TObject = class',
  17092. ' end;',
  17093. ' TChild = class',
  17094. ' end;',
  17095. ' TExtRootA = class external name ''ExtRootA''',
  17096. ' end;',
  17097. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17098. ' end;',
  17099. ' TExtRootB = class external name ''ExtRootB''',
  17100. ' end;',
  17101. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17102. ' end;',
  17103. 'var',
  17104. ' Obj: TObject;',
  17105. ' Child: TChild;',
  17106. ' RootA: TExtRootA;',
  17107. ' ChildA: TExtChildA;',
  17108. ' RootB: TExtRootB;',
  17109. ' ChildB: TExtChildB;',
  17110. ' i: IUnknown;',
  17111. 'begin',
  17112. ' obj:=tobject(roota);',
  17113. ' obj:=tobject(childa);',
  17114. ' child:=tchild(tobject(roota));',
  17115. ' roota:=textroota(obj);',
  17116. ' roota:=textroota(child);',
  17117. ' roota:=textroota(rootb);',
  17118. ' roota:=textroota(childb);',
  17119. ' childa:=textchilda(textroota(obj));',
  17120. ' roota:=TExtRootA(i)',
  17121. '']);
  17122. ConvertProgram;
  17123. CheckSource('TestExternalClass_TypeCastToRootClass',
  17124. LinesToStr([ // statements
  17125. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17126. 'rtl.createClass($mod, "TObject", null, function () {',
  17127. ' this.$init = function () {',
  17128. ' };',
  17129. ' this.$final = function () {',
  17130. ' };',
  17131. '});',
  17132. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17133. '});',
  17134. 'this.Obj = null;',
  17135. 'this.Child = null;',
  17136. 'this.RootA = null;',
  17137. 'this.ChildA = null;',
  17138. 'this.RootB = null;',
  17139. 'this.ChildB = null;',
  17140. 'this.i = null;',
  17141. '']),
  17142. LinesToStr([ // $mod.$main
  17143. '$mod.Obj = $mod.RootA;',
  17144. '$mod.Obj = $mod.ChildA;',
  17145. '$mod.Child = $mod.RootA;',
  17146. '$mod.RootA = $mod.Obj;',
  17147. '$mod.RootA = $mod.Child;',
  17148. '$mod.RootA = $mod.RootB;',
  17149. '$mod.RootA = $mod.ChildB;',
  17150. '$mod.ChildA = $mod.Obj;',
  17151. '$mod.RootA = $mod.i;',
  17152. '']));
  17153. end;
  17154. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17155. begin
  17156. StartProgram(false);
  17157. Add([
  17158. '{$modeswitch externalclass}',
  17159. 'type',
  17160. ' IUnknown = interface end;',
  17161. ' IBird = interface(IUnknown) end;',
  17162. ' TClass = class of TObject;',
  17163. ' TObject = class',
  17164. ' end;',
  17165. ' TChild = class',
  17166. ' end;',
  17167. ' TJSObject = class external name ''Object''',
  17168. ' end;',
  17169. ' TRec = record end;',
  17170. 'var',
  17171. ' Obj: TObject;',
  17172. ' Child: TChild;',
  17173. ' i: IUnknown;',
  17174. ' Bird: IBird;',
  17175. ' j: TJSObject;',
  17176. ' r: TRec;',
  17177. ' c: TClass;',
  17178. 'begin',
  17179. ' j:=tjsobject(IUnknown);',
  17180. ' j:=tjsobject(IBird);',
  17181. ' j:=tjsobject(TObject);',
  17182. ' j:=tjsobject(TChild);',
  17183. ' j:=tjsobject(TRec);',
  17184. ' j:=tjsobject(Obj);',
  17185. ' j:=tjsobject(Child);',
  17186. ' j:=tjsobject(i);',
  17187. ' j:=tjsobject(Bird);',
  17188. ' j:=tjsobject(r);',
  17189. ' j:=tjsobject(c);',
  17190. '']);
  17191. ConvertProgram;
  17192. CheckSource('TestExternalClass_TypeCastToJSObject',
  17193. LinesToStr([ // statements
  17194. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17195. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17196. 'rtl.createClass($mod, "TObject", null, function () {',
  17197. ' this.$init = function () {',
  17198. ' };',
  17199. ' this.$final = function () {',
  17200. ' };',
  17201. '});',
  17202. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17203. '});',
  17204. 'rtl.recNewT($mod, "TRec", function () {',
  17205. ' this.$eq = function (b) {',
  17206. ' return true;',
  17207. ' };',
  17208. ' this.$assign = function (s) {',
  17209. ' return this;',
  17210. ' };',
  17211. '});',
  17212. 'this.Obj = null;',
  17213. 'this.Child = null;',
  17214. 'this.i = null;',
  17215. 'this.Bird = null;',
  17216. 'this.j = null;',
  17217. 'this.r = $mod.TRec.$new();',
  17218. 'this.c = null;',
  17219. '']),
  17220. LinesToStr([ // $mod.$main
  17221. '$mod.j = $mod.IUnknown;',
  17222. '$mod.j = $mod.IBird;',
  17223. '$mod.j = $mod.TObject;',
  17224. '$mod.j = $mod.TChild;',
  17225. '$mod.j = $mod.TRec;',
  17226. '$mod.j = $mod.Obj;',
  17227. '$mod.j = $mod.Child;',
  17228. '$mod.j = $mod.i;',
  17229. '$mod.j = $mod.Bird;',
  17230. '$mod.j = $mod.r;',
  17231. '$mod.j = $mod.c;',
  17232. '']));
  17233. end;
  17234. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17235. begin
  17236. StartProgram(false);
  17237. Add('{$modeswitch externalclass}');
  17238. Add('type');
  17239. Add(' TJSString = class external name ''String''');
  17240. Add(' class function fromCharCode() : string; varargs;');
  17241. Add(' function anchor(const aName : string) : string;');
  17242. Add(' end;');
  17243. Add('var');
  17244. Add(' s: string;');
  17245. Add('begin');
  17246. Add(' s:=TJSString.fromCharCode(65,66);');
  17247. Add(' s:=TJSString(s).anchor(s);');
  17248. Add(' s:=TJSString(''foo'').anchor(s);');
  17249. ConvertProgram;
  17250. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17251. LinesToStr([ // statements
  17252. 'this.s = "";',
  17253. '']),
  17254. LinesToStr([ // $mod.$main
  17255. '$mod.s = String.fromCharCode(65, 66);',
  17256. '$mod.s = $mod.s.anchor($mod.s);',
  17257. '$mod.s = "foo".anchor($mod.s);',
  17258. '']));
  17259. end;
  17260. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17261. begin
  17262. StartProgram(false);
  17263. Add([
  17264. '{$modeswitch externalclass}',
  17265. 'type',
  17266. ' TJSObject = class external name ''Object'' end;',
  17267. ' TJSFunction = class external name ''Function''',
  17268. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17269. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17270. ' end;',
  17271. ' TObject = class',
  17272. ' procedure DoIt(i: longint);',
  17273. ' end;',
  17274. ' TFuncInt = function(o: TObject): longint;',
  17275. 'function GetIt(o: TObject): longint;',
  17276. ' procedure Sub; begin end;',
  17277. 'var',
  17278. ' f: TJSFunction;',
  17279. ' fi: TFuncInt;',
  17280. 'begin',
  17281. ' fi:=TFuncInt(f);',
  17282. ' f:=TJSFunction(fi);',
  17283. ' f:=TJSFunction(@GetIt);',
  17284. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17285. ' f:=TJSFunction(@Sub);',
  17286. ' f:=TJSFunction(@o.doit);',
  17287. ' f:=TJSFunction(fi).bind(nil,4)',
  17288. 'end;',
  17289. 'procedure TObject.DoIt(i: longint);',
  17290. ' procedure Sub; begin end;',
  17291. 'var f: TJSFunction;',
  17292. 'begin',
  17293. ' f:=TJSFunction(@DoIt);',
  17294. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17295. ' f:=TJSFunction(@Sub);',
  17296. ' f:=TJSFunction(@GetIt);',
  17297. 'end;',
  17298. 'begin']);
  17299. ConvertProgram;
  17300. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17301. LinesToStr([ // statements
  17302. 'rtl.createClass($mod, "TObject", null, function () {',
  17303. ' this.$init = function () {',
  17304. ' };',
  17305. ' this.$final = function () {',
  17306. ' };',
  17307. ' this.DoIt = function (i) {',
  17308. ' var $Self = this;',
  17309. ' function Sub() {',
  17310. ' };',
  17311. ' var f = null;',
  17312. ' f = $Self.DoIt;',
  17313. ' f = $Self.DoIt.bind(null, 13);',
  17314. ' f = Sub;',
  17315. ' f = $mod.GetIt;',
  17316. ' };',
  17317. '});',
  17318. 'this.GetIt = function (o) {',
  17319. ' var Result = 0;',
  17320. ' function Sub() {',
  17321. ' };',
  17322. ' var f = null;',
  17323. ' var fi = null;',
  17324. ' fi = f;',
  17325. ' f = fi;',
  17326. ' f = $mod.GetIt;',
  17327. ' f = $mod.GetIt.bind(null, 3);',
  17328. ' f = Sub;',
  17329. ' f = $mod.TObject.DoIt;',
  17330. ' f = fi.bind(null, 4);',
  17331. ' return Result;',
  17332. '};',
  17333. '']),
  17334. LinesToStr([ // $mod.$main
  17335. '']));
  17336. end;
  17337. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17338. begin
  17339. StartProgram(false);
  17340. Add([
  17341. '{$mode delphi}',
  17342. '{$modeswitch externalclass}',
  17343. 'type',
  17344. ' TJSObject = class external name ''Object'' end;',
  17345. ' TJSWindow = class external name ''Window''(TJSObject)',
  17346. ' procedure Open;',
  17347. ' end;',
  17348. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17349. ' procedure Execute;',
  17350. ' end;',
  17351. 'procedure Fly;',
  17352. 'var',
  17353. ' w: TJSWindow;',
  17354. ' e: TJSEventTarget;',
  17355. 'begin',
  17356. ' w:=TJSWindow(e);',
  17357. ' e:=TJSEventTarget(w);',
  17358. 'end;',
  17359. 'begin']);
  17360. ConvertProgram;
  17361. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17362. LinesToStr([ // statements
  17363. 'this.Fly = function () {',
  17364. ' var w = null;',
  17365. ' var e = null;',
  17366. ' w = e;',
  17367. ' e = w;',
  17368. '};',
  17369. '']),
  17370. LinesToStr([ // $mod.$main
  17371. '']));
  17372. end;
  17373. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17374. begin
  17375. StartProgram(false);
  17376. Add('{$modeswitch externalclass}');
  17377. Add('type');
  17378. Add(' TJSString = class external name ''String''');
  17379. Add(' class function fromCharCode() : string; varargs;');
  17380. Add(' end;');
  17381. Add('var');
  17382. Add(' s: string;');
  17383. Add(' sObj: TJSString;');
  17384. Add('begin');
  17385. Add(' s:=sObj.fromCharCode(65,66);');
  17386. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17387. nExternalClassInstanceCannotAccessStaticX);
  17388. ConvertProgram;
  17389. end;
  17390. procedure TTestModule.TestExternalClass_BracketAccessor;
  17391. begin
  17392. StartProgram(false);
  17393. Add([
  17394. '{$modeswitch externalclass}',
  17395. 'type',
  17396. ' TJSArray = class external name ''Array2''',
  17397. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17398. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17399. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17400. ' end;',
  17401. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17402. 'begin end;',
  17403. 'var',
  17404. ' Arr: tjsarray;',
  17405. ' s: string;',
  17406. ' i: longint;',
  17407. ' v: jsvalue;',
  17408. 'begin',
  17409. ' v:=arr[0];',
  17410. ' v:=arr.items[1];',
  17411. ' arr[2]:=s;',
  17412. ' arr.items[3]:=s;',
  17413. ' arr[4]:=i;',
  17414. ' arr[5]:=arr[6];',
  17415. ' arr.items[7]:=arr.items[8];',
  17416. ' with arr do items[9]:=items[10];',
  17417. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17418. ' with arr do begin',
  17419. ' v:=GetItems(14);',
  17420. ' setitems(15,16);',
  17421. ' end;',
  17422. ' v:=test1.arr.items[17];',
  17423. ' test1.arr.items[18]:=v;',
  17424. '']);
  17425. ConvertProgram;
  17426. CheckSource('TestExternalClass_BracketAccessor',
  17427. LinesToStr([ // statements
  17428. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17429. '};',
  17430. 'this.Arr = null;',
  17431. 'this.s = "";',
  17432. 'this.i = 0;',
  17433. 'this.v = undefined;',
  17434. '']),
  17435. LinesToStr([ // $mod.$main
  17436. '$mod.v = $mod.Arr[0];',
  17437. '$mod.v = $mod.Arr[1];',
  17438. '$mod.Arr[2] = $mod.s;',
  17439. '$mod.Arr[3] = $mod.s;',
  17440. '$mod.Arr[4] = $mod.i;',
  17441. '$mod.Arr[5] = $mod.Arr[6];',
  17442. '$mod.Arr[7] = $mod.Arr[8];',
  17443. 'var $with1 = $mod.Arr;',
  17444. '$with1[9] = $with1[10];',
  17445. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17446. ' a: 9,',
  17447. ' p: $mod.Arr,',
  17448. ' get: function () {',
  17449. ' return this.p[this.a];',
  17450. ' },',
  17451. ' set: function (v) {',
  17452. ' this.p[this.a] = v;',
  17453. ' }',
  17454. '}, {',
  17455. ' a: 10,',
  17456. ' p: $mod.Arr,',
  17457. ' get: function () {',
  17458. ' return this.p[this.a];',
  17459. ' },',
  17460. ' set: function (v) {',
  17461. ' this.p[this.a] = v;',
  17462. ' }',
  17463. '});',
  17464. 'var $with2 = $mod.Arr;',
  17465. '$mod.v = $with2[14];',
  17466. '$with2[15] = 16;',
  17467. '$mod.v = $mod.Arr[17];',
  17468. '$mod.Arr[18] = $mod.v;',
  17469. '']));
  17470. end;
  17471. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17472. begin
  17473. StartProgram(false);
  17474. Add([
  17475. '{$modeswitch externalclass}',
  17476. 'type',
  17477. ' TJSArray = class external name ''Array2''',
  17478. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17479. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17480. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17481. ' end;',
  17482. ' TMyArr = class(TJSArray)',
  17483. ' procedure DoIt;',
  17484. ' end;',
  17485. 'procedure tmyarr.DoIt;',
  17486. 'begin',
  17487. ' Items[1]:=Items[2];',
  17488. ' SetItems(3,getItems(4));',
  17489. 'end;',
  17490. 'var',
  17491. ' Arr: tmyarr;',
  17492. ' s: string;',
  17493. ' i: longint;',
  17494. ' v: jsvalue;',
  17495. 'begin',
  17496. ' v:=arr[0];',
  17497. ' v:=arr.items[1];',
  17498. ' arr[2]:=s;',
  17499. ' arr.items[3]:=s;',
  17500. ' arr[4]:=i;',
  17501. ' arr[5]:=arr[6];',
  17502. ' arr.items[7]:=arr.items[8];',
  17503. ' with arr do items[9]:=items[10];',
  17504. ' with arr do begin',
  17505. ' v:=GetItems(14);',
  17506. ' setitems(15,16);',
  17507. ' end;',
  17508. '']);
  17509. ConvertProgram;
  17510. CheckSource('TestExternalClass_BracketAccessor_Call',
  17511. LinesToStr([ // statements
  17512. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  17513. ' this.$init = function () {',
  17514. ' };',
  17515. ' this.$final = function () {',
  17516. ' };',
  17517. ' this.DoIt = function () {',
  17518. ' this[1] = this[2];',
  17519. ' this[3] = this[4];',
  17520. ' };',
  17521. '});',
  17522. 'this.Arr = null;',
  17523. 'this.s = "";',
  17524. 'this.i = 0;',
  17525. 'this.v = undefined;',
  17526. '']),
  17527. LinesToStr([ // $mod.$main
  17528. '$mod.v = $mod.Arr[0];',
  17529. '$mod.v = $mod.Arr[1];',
  17530. '$mod.Arr[2] = $mod.s;',
  17531. '$mod.Arr[3] = $mod.s;',
  17532. '$mod.Arr[4] = $mod.i;',
  17533. '$mod.Arr[5] = $mod.Arr[6];',
  17534. '$mod.Arr[7] = $mod.Arr[8];',
  17535. 'var $with1 = $mod.Arr;',
  17536. '$with1[9] = $with1[10];',
  17537. 'var $with2 = $mod.Arr;',
  17538. '$mod.v = $with2[14];',
  17539. '$with2[15] = 16;',
  17540. '']));
  17541. end;
  17542. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17543. begin
  17544. StartProgram(false);
  17545. Add('{$modeswitch externalclass}');
  17546. Add('type');
  17547. Add(' TJSArray = class external name ''Array2''');
  17548. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17549. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17550. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17551. Add(' end;');
  17552. Add('begin');
  17553. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17554. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17555. ConvertProgram;
  17556. end;
  17557. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17558. begin
  17559. StartProgram(false);
  17560. Add('{$modeswitch externalclass}');
  17561. Add('type');
  17562. Add(' TJSArray = class external name ''Array2''');
  17563. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17564. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17565. Add(' end;');
  17566. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17567. Add('begin end;');
  17568. Add('var');
  17569. Add(' Arr: tjsarray;');
  17570. Add(' v: jsvalue;');
  17571. Add('begin');
  17572. Add(' v:=arr[0];');
  17573. Add(' v:=arr.items[1];');
  17574. Add(' with arr do v:=items[2];');
  17575. Add(' doit(arr[3],arr[4]);');
  17576. ConvertProgram;
  17577. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17578. LinesToStr([ // statements
  17579. 'this.DoIt = function (vI, vJ) {',
  17580. '};',
  17581. 'this.Arr = null;',
  17582. 'this.v = undefined;',
  17583. '']),
  17584. LinesToStr([ // $mod.$main
  17585. '$mod.v = $mod.Arr[0];',
  17586. '$mod.v = $mod.Arr[1];',
  17587. 'var $with1 = $mod.Arr;',
  17588. '$mod.v = $with1[2];',
  17589. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  17590. '']));
  17591. end;
  17592. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  17593. begin
  17594. StartProgram(false);
  17595. Add('{$modeswitch externalclass}');
  17596. Add('type');
  17597. Add(' TJSArray = class external name ''Array2''');
  17598. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17599. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17600. Add(' end;');
  17601. Add('var');
  17602. Add(' Arr: tjsarray;');
  17603. Add(' s: string;');
  17604. Add(' i: longint;');
  17605. Add(' v: jsvalue;');
  17606. Add('begin');
  17607. Add(' arr[2]:=s;');
  17608. Add(' arr.items[3]:=s;');
  17609. Add(' arr[4]:=i;');
  17610. Add(' with arr do items[5]:=i;');
  17611. ConvertProgram;
  17612. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  17613. LinesToStr([ // statements
  17614. 'this.Arr = null;',
  17615. 'this.s = "";',
  17616. 'this.i = 0;',
  17617. 'this.v = undefined;',
  17618. '']),
  17619. LinesToStr([ // $mod.$main
  17620. '$mod.Arr[2] = $mod.s;',
  17621. '$mod.Arr[3] = $mod.s;',
  17622. '$mod.Arr[4] = $mod.i;',
  17623. 'var $with1 = $mod.Arr;',
  17624. '$with1[5] = $mod.i;',
  17625. '']));
  17626. end;
  17627. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17628. begin
  17629. StartProgram(false);
  17630. Add('{$modeswitch externalclass}');
  17631. Add('type');
  17632. Add(' TJSArray = class external name ''Array2''');
  17633. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17634. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17635. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17636. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17637. Add(' end;');
  17638. Add('var');
  17639. Add(' Arr: tjsarray;');
  17640. Add(' s: string;');
  17641. Add(' i: longint;');
  17642. Add(' v: jsvalue;');
  17643. Add('begin');
  17644. Add(' arr[2]:=s;');
  17645. Add(' arr.items[3]:=s;');
  17646. Add(' arr.numbers[4]:=i;');
  17647. Add(' with arr do items[5]:=i;');
  17648. Add(' with arr do numbers[6]:=i;');
  17649. ConvertProgram;
  17650. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17651. LinesToStr([ // statements
  17652. 'this.Arr = null;',
  17653. 'this.s = "";',
  17654. 'this.i = 0;',
  17655. 'this.v = undefined;',
  17656. '']),
  17657. LinesToStr([ // $mod.$main
  17658. '$mod.Arr[2] = $mod.s;',
  17659. '$mod.Arr[3] = $mod.s;',
  17660. '$mod.Arr[4] = $mod.i;',
  17661. 'var $with1 = $mod.Arr;',
  17662. '$with1[5] = $mod.i;',
  17663. 'var $with2 = $mod.Arr;',
  17664. '$with2[6] = $mod.i;',
  17665. '']));
  17666. end;
  17667. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17668. begin
  17669. StartProgram(false);
  17670. Add('{$modeswitch externalclass}');
  17671. Add('type');
  17672. Add(' TJSArray = class external name ''Array2''');
  17673. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17674. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17675. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17676. Add(' end;');
  17677. Add('var');
  17678. Add(' Arr: tjsarray;');
  17679. Add(' i: longint;');
  17680. Add(' IntArr: array of longint;');
  17681. Add(' v: jsvalue;');
  17682. Add('begin');
  17683. Add(' v:=arr.items[i];');
  17684. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17685. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17686. ConvertProgram;
  17687. CheckSource('TestExternalClass_BracketAccessor_Index',
  17688. LinesToStr([ // statements
  17689. 'this.Arr = null;',
  17690. 'this.i = 0;',
  17691. 'this.IntArr = [];',
  17692. 'this.v = undefined;',
  17693. '']),
  17694. LinesToStr([ // $mod.$main
  17695. '$mod.v = $mod.Arr[$mod.i];',
  17696. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17697. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17698. '']));
  17699. end;
  17700. procedure TTestModule.TestExternalClass_ForInJSObject;
  17701. begin
  17702. StartProgram(false);
  17703. Add([
  17704. '{$modeswitch externalclass}',
  17705. 'type',
  17706. ' TJSObject = class external name ''Object''',
  17707. ' end;',
  17708. 'var',
  17709. ' o: TJSObject;',
  17710. ' key: string;',
  17711. 'begin',
  17712. ' for key in o do',
  17713. ' if key=''abc'' then ;',
  17714. '']);
  17715. ConvertProgram;
  17716. CheckSource('TestExternalClass_ForInJSObject',
  17717. LinesToStr([ // statements
  17718. 'this.o = null;',
  17719. 'this.key = "";',
  17720. '']),
  17721. LinesToStr([ // $mod.$main
  17722. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17723. '']));
  17724. end;
  17725. procedure TTestModule.TestExternalClass_ForInJSArray;
  17726. begin
  17727. StartProgram(false);
  17728. Add([
  17729. '{$modeswitch externalclass}',
  17730. 'type',
  17731. ' TJSInt8Array = class external name ''Int8Array''',
  17732. ' private',
  17733. ' flength: NativeInt external name ''length'';',
  17734. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17735. ' public',
  17736. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17737. ' property Length: NativeInt read flength;',
  17738. ' end;',
  17739. 'var',
  17740. ' a: TJSInt8Array;',
  17741. ' value: shortint;',
  17742. 'begin',
  17743. ' for value in a do',
  17744. ' if value=3 then ;',
  17745. '']);
  17746. ConvertProgram;
  17747. CheckSource('TestExternalClass_ForInJSArray',
  17748. LinesToStr([ // statements
  17749. 'this.a = null;',
  17750. 'this.value = 0;',
  17751. '']),
  17752. LinesToStr([ // $mod.$main
  17753. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  17754. ' $mod.value = $in1[$l2];',
  17755. ' if ($mod.value === 3) ;',
  17756. '};',
  17757. '']));
  17758. end;
  17759. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17760. begin
  17761. AddModuleWithIntfImplSrc('unit2.pas',
  17762. LinesToStr([
  17763. '{$modeswitch externalclass}',
  17764. 'type',
  17765. ' TJSBufferSource = class external name ''BufferSource''',
  17766. ' end;',
  17767. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17768. '']),
  17769. '');
  17770. AddModuleWithIntfImplSrc('unit3.pas',
  17771. LinesToStr([
  17772. '{$modeswitch externalclass}',
  17773. 'type',
  17774. ' TJSBufferSource = class external name ''BufferSource''',
  17775. ' end;',
  17776. '']),
  17777. '');
  17778. StartUnit(true);
  17779. Add([
  17780. 'interface',
  17781. 'uses unit2, unit3;',
  17782. 'procedure DoSome(s: TJSBufferSource);',
  17783. 'implementation',
  17784. 'procedure DoSome(s: TJSBufferSource);',
  17785. 'begin',
  17786. ' DoIt(s);',
  17787. 'end;',
  17788. '']);
  17789. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17790. nIncompatibleTypeArgNo);
  17791. ConvertUnit;
  17792. end;
  17793. procedure TTestModule.TestClassInterface_Corba;
  17794. begin
  17795. StartProgram(false);
  17796. Add([
  17797. '{$interfaces corba}',
  17798. 'type',
  17799. ' IUnknown = interface;',
  17800. ' IUnknown = interface',
  17801. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17802. ' end;',
  17803. ' IInterface = IUnknown;',
  17804. ' IBird = interface(IInterface)',
  17805. ' function GetSize: longint;',
  17806. ' procedure SetSize(i: longint);',
  17807. ' property Size: longint read GetSize write SetSize;',
  17808. ' procedure DoIt(i: longint);',
  17809. ' end;',
  17810. ' TObject = class',
  17811. ' end;',
  17812. ' TBird = class(TObject,IBird)',
  17813. ' function GetSize: longint; virtual; abstract;',
  17814. ' procedure SetSize(i: longint); virtual; abstract;',
  17815. ' procedure DoIt(i: longint); virtual; abstract;',
  17816. ' end;',
  17817. 'var',
  17818. ' BirdIntf: IBird;',
  17819. 'begin',
  17820. ' BirdIntf.Size:=BirdIntf.Size;',
  17821. '']);
  17822. ConvertProgram;
  17823. CheckSource('TestClassInterface_Corba',
  17824. LinesToStr([ // statements
  17825. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17826. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17827. 'rtl.createClass($mod, "TObject", null, function () {',
  17828. ' this.$init = function () {',
  17829. ' };',
  17830. ' this.$final = function () {',
  17831. ' };',
  17832. '});',
  17833. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17834. ' rtl.addIntf(this, $mod.IBird);',
  17835. '});',
  17836. 'this.BirdIntf = null;',
  17837. '']),
  17838. LinesToStr([ // $mod.$main
  17839. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17840. '']));
  17841. end;
  17842. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17843. begin
  17844. StartProgram(false);
  17845. Add([
  17846. '{$interfaces corba}',
  17847. 'type',
  17848. ' IUnknown = interface',
  17849. ' procedure DoIt; external name ''foo'';',
  17850. ' end;',
  17851. 'begin']);
  17852. SetExpectedParserError(
  17853. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  17854. nParserNoFieldsAllowed);
  17855. ConvertProgram;
  17856. end;
  17857. procedure TTestModule.TestClassInterface_Overloads;
  17858. begin
  17859. StartProgram(false);
  17860. Add([
  17861. '{$interfaces corba}',
  17862. 'type',
  17863. ' integer = longint;',
  17864. ' IUnknown = interface',
  17865. ' procedure DoIt(i: integer);',
  17866. ' procedure DoIt(s: string);',
  17867. ' end;',
  17868. ' IBird = interface(IUnknown)',
  17869. ' procedure DoIt(b: boolean); overload;',
  17870. ' end;',
  17871. ' TObject = class',
  17872. ' end;',
  17873. ' TBird = class(TObject,IBird)',
  17874. ' procedure DoIt(o: TObject);',
  17875. ' procedure DoIt(s: string);',
  17876. ' procedure DoIt(i: integer);',
  17877. ' procedure DoIt(b: boolean);',
  17878. ' end;',
  17879. 'procedure TBird.DoIt(o: TObject); begin end;',
  17880. 'procedure TBird.DoIt(s: string); begin end;',
  17881. 'procedure TBird.DoIt(i: integer); begin end;',
  17882. 'procedure TBird.DoIt(b: boolean); begin end;',
  17883. 'var',
  17884. ' BirdIntf: IBird;',
  17885. 'begin',
  17886. ' BirdIntf.DoIt(3);',
  17887. ' BirdIntf.DoIt(''abc'');',
  17888. ' BirdIntf.DoIt(true);',
  17889. '']);
  17890. ConvertProgram;
  17891. CheckSource('TestClassInterface_Overloads',
  17892. LinesToStr([ // statements
  17893. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  17894. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  17895. 'rtl.createClass($mod, "TObject", null, function () {',
  17896. ' this.$init = function () {',
  17897. ' };',
  17898. ' this.$final = function () {',
  17899. ' };',
  17900. '});',
  17901. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17902. ' this.DoIt = function (o) {',
  17903. ' };',
  17904. ' this.DoIt$1 = function (s) {',
  17905. ' };',
  17906. ' this.DoIt$2 = function (i) {',
  17907. ' };',
  17908. ' this.DoIt$3 = function (b) {',
  17909. ' };',
  17910. ' rtl.addIntf(this, $mod.IBird, {',
  17911. ' DoIt$2: "DoIt$3",',
  17912. ' DoIt: "DoIt$2"',
  17913. ' });',
  17914. '});',
  17915. 'this.BirdIntf = null;',
  17916. '']),
  17917. LinesToStr([ // $mod.$main
  17918. '$mod.BirdIntf.DoIt(3);',
  17919. '$mod.BirdIntf.DoIt$1("abc");',
  17920. '$mod.BirdIntf.DoIt$2(true);',
  17921. '']));
  17922. end;
  17923. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  17924. begin
  17925. StartProgram(false);
  17926. Add([
  17927. '{$interfaces corba}',
  17928. 'type',
  17929. ' IBird = interface',
  17930. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17931. ' end;',
  17932. ' IDog = interface',
  17933. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17934. ' end;',
  17935. ' TObject = class(IBird,IDog)',
  17936. ' end;',
  17937. 'begin']);
  17938. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  17939. nDuplicateGUIDXInYZ);
  17940. ConvertProgram;
  17941. end;
  17942. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  17943. begin
  17944. StartProgram(false);
  17945. Add([
  17946. '{$interfaces corba}',
  17947. 'type',
  17948. ' IAnimal = interface',
  17949. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17950. ' end;',
  17951. ' IBird = interface(IAnimal)',
  17952. ' end;',
  17953. ' IHawk = interface(IBird)',
  17954. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17955. ' end;',
  17956. 'begin']);
  17957. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  17958. nDuplicateGUIDXInYZ);
  17959. ConvertProgram;
  17960. end;
  17961. procedure TTestModule.TestClassInterface_AncestorImpl;
  17962. begin
  17963. StartProgram(false);
  17964. Add([
  17965. '{$interfaces corba}',
  17966. 'type',
  17967. ' integer = longint;',
  17968. ' IUnknown = interface',
  17969. ' procedure DoIt(i: integer);',
  17970. ' end;',
  17971. ' IBird = interface',
  17972. ' procedure Fly(i: integer);',
  17973. ' end;',
  17974. ' TObject = class(IUnknown)',
  17975. ' procedure DoIt(i: integer);',
  17976. ' end;',
  17977. ' TBird = class(IBird)',
  17978. ' procedure Fly(i: integer);',
  17979. ' end;',
  17980. 'procedure TObject.DoIt(i: integer); begin end;',
  17981. 'procedure TBird.Fly(i: integer); begin end;',
  17982. 'begin',
  17983. '']);
  17984. ConvertProgram;
  17985. CheckSource('TestClassInterface_AncestorIntf',
  17986. LinesToStr([ // statements
  17987. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  17988. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  17989. 'rtl.createClass($mod, "TObject", null, function () {',
  17990. ' this.$init = function () {',
  17991. ' };',
  17992. ' this.$final = function () {',
  17993. ' };',
  17994. ' this.DoIt = function (i) {',
  17995. ' };',
  17996. ' rtl.addIntf(this, $mod.IUnknown);',
  17997. '});',
  17998. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17999. ' this.Fly = function (i) {',
  18000. ' };',
  18001. ' rtl.addIntf(this, $mod.IBird);',
  18002. ' rtl.addIntf(this, $mod.IUnknown);',
  18003. '});',
  18004. '']),
  18005. LinesToStr([ // $mod.$main
  18006. '']));
  18007. end;
  18008. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18009. begin
  18010. StartProgram(false);
  18011. Add([
  18012. '{$interfaces corba}',
  18013. 'type',
  18014. ' integer = longint;',
  18015. ' IBird = interface',
  18016. ' procedure DoIt(i: integer);',
  18017. ' end;',
  18018. ' TObject = class',
  18019. ' procedure DoIt(i: integer);',
  18020. ' end;',
  18021. ' TBird = class(IBird)',
  18022. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18023. ' end;',
  18024. 'procedure TObject.DoIt(i: integer); begin end;',
  18025. 'procedure TBird.DoIt(i: integer); begin end;',
  18026. 'begin',
  18027. '']);
  18028. ConvertProgram;
  18029. CheckSource('TestClassInterface_ImplReintroduce',
  18030. LinesToStr([ // statements
  18031. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18032. 'rtl.createClass($mod, "TObject", null, function () {',
  18033. ' this.$init = function () {',
  18034. ' };',
  18035. ' this.$final = function () {',
  18036. ' };',
  18037. ' this.DoIt = function (i) {',
  18038. ' };',
  18039. '});',
  18040. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18041. ' this.DoIt$1 = function (i) {',
  18042. ' };',
  18043. ' rtl.addIntf(this, $mod.IBird, {',
  18044. ' DoIt: "DoIt$1"',
  18045. ' });',
  18046. '});',
  18047. '']),
  18048. LinesToStr([ // $mod.$main
  18049. '']));
  18050. end;
  18051. procedure TTestModule.TestClassInterface_MethodResolution;
  18052. begin
  18053. StartProgram(false);
  18054. Add([
  18055. '{$interfaces corba}',
  18056. 'type',
  18057. ' IUnknown = interface',
  18058. ' procedure Walk(i: longint);',
  18059. ' end;',
  18060. ' IBird = interface(IUnknown)',
  18061. ' procedure Walk(b: boolean); overload;',
  18062. ' procedure Fly(s: string);',
  18063. ' end;',
  18064. ' TObject = class',
  18065. ' end;',
  18066. ' TBird = class(TObject,IBird)',
  18067. ' procedure IBird.Fly = Move;',
  18068. ' procedure IBird.Walk = Hop;',
  18069. ' procedure Hop(i: longint);',
  18070. ' procedure Move(s: string);',
  18071. ' procedure Hop(b: boolean);',
  18072. ' end;',
  18073. 'procedure TBird.Move(s: string); begin end;',
  18074. 'procedure TBird.Hop(i: longint); begin end;',
  18075. 'procedure TBird.Hop(b: boolean); begin end;',
  18076. 'var',
  18077. ' BirdIntf: IBird;',
  18078. 'begin',
  18079. ' BirdIntf.Walk(3);',
  18080. ' BirdIntf.Walk(true);',
  18081. ' BirdIntf.Fly(''abc'');',
  18082. '']);
  18083. ConvertProgram;
  18084. CheckSource('TestClassInterface_MethodResolution',
  18085. LinesToStr([ // statements
  18086. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18087. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  18088. 'rtl.createClass($mod, "TObject", null, function () {',
  18089. ' this.$init = function () {',
  18090. ' };',
  18091. ' this.$final = function () {',
  18092. ' };',
  18093. '});',
  18094. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18095. ' this.Hop = function (i) {',
  18096. ' };',
  18097. ' this.Move = function (s) {',
  18098. ' };',
  18099. ' this.Hop$1 = function (b) {',
  18100. ' };',
  18101. ' rtl.addIntf(this, $mod.IBird, {',
  18102. ' Walk$1: "Hop$1",',
  18103. ' Fly: "Move",',
  18104. ' Walk: "Hop"',
  18105. ' });',
  18106. '});',
  18107. 'this.BirdIntf = null;',
  18108. '']),
  18109. LinesToStr([ // $mod.$main
  18110. '$mod.BirdIntf.Walk(3);',
  18111. '$mod.BirdIntf.Walk$1(true);',
  18112. '$mod.BirdIntf.Fly("abc");',
  18113. '']));
  18114. end;
  18115. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18116. begin
  18117. StartProgram(false);
  18118. Add([
  18119. '{$interfaces com}',
  18120. 'type',
  18121. ' IUnknown = interface',
  18122. ' function _AddRef: longint;',
  18123. ' procedure Walk;',
  18124. ' end;',
  18125. ' IBird = interface end;',
  18126. ' IDog = interface end;',
  18127. ' TObject = class(IBird,IDog)',
  18128. ' function _AddRef: longint; virtual; abstract;',
  18129. ' procedure Walk; virtual; abstract;',
  18130. ' end;',
  18131. ' TBird = class(IUnknown)',
  18132. ' end;',
  18133. 'begin',
  18134. '']);
  18135. ConvertProgram;
  18136. CheckSource('TestClassInterface_COM_AncestorLess',
  18137. LinesToStr([ // statements
  18138. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18139. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  18140. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  18141. 'rtl.createClass($mod, "TObject", null, function () {',
  18142. ' this.$init = function () {',
  18143. ' };',
  18144. ' this.$final = function () {',
  18145. ' };',
  18146. ' rtl.addIntf(this, $mod.IBird);',
  18147. ' rtl.addIntf(this, $mod.IDog);',
  18148. '});',
  18149. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18150. ' rtl.addIntf(this, $mod.IUnknown);',
  18151. ' rtl.addIntf(this, $mod.IBird);',
  18152. ' rtl.addIntf(this, $mod.IDog);',
  18153. '});',
  18154. '']),
  18155. LinesToStr([ // $mod.$main
  18156. '']));
  18157. end;
  18158. procedure TTestModule.TestClassInterface_MethodOverride;
  18159. begin
  18160. StartProgram(false);
  18161. Add([
  18162. '{$interfaces corba}',
  18163. 'type',
  18164. ' IUnknown = interface',
  18165. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18166. ' procedure Go;',
  18167. ' end;',
  18168. ' TObject = class(IUnknown)',
  18169. ' procedure Go; virtual; abstract;',
  18170. ' end;',
  18171. ' TBird = class',
  18172. ' procedure Go; override;',
  18173. ' end;',
  18174. ' TCat = class(TObject)',
  18175. ' procedure Go; override;',
  18176. ' end;',
  18177. ' TDog = class(TObject, IUnknown)',
  18178. ' procedure Go; override;',
  18179. ' end;',
  18180. 'procedure TBird.Go; begin end;',
  18181. 'procedure TCat.Go; begin end;',
  18182. 'procedure TDog.Go; begin end;',
  18183. 'begin',
  18184. '']);
  18185. ConvertProgram;
  18186. CheckSource('TestClassInterface_MethodOverride',
  18187. LinesToStr([ // statements
  18188. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18189. 'rtl.createClass($mod, "TObject", null, function () {',
  18190. ' this.$init = function () {',
  18191. ' };',
  18192. ' this.$final = function () {',
  18193. ' };',
  18194. ' rtl.addIntf(this, $mod.IUnknown);',
  18195. '});',
  18196. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18197. ' this.Go = function () {',
  18198. ' };',
  18199. ' rtl.addIntf(this, $mod.IUnknown);',
  18200. '});',
  18201. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  18202. ' this.Go = function () {',
  18203. ' };',
  18204. ' rtl.addIntf(this, $mod.IUnknown);',
  18205. '});',
  18206. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  18207. ' this.Go = function () {',
  18208. ' };',
  18209. ' rtl.addIntf(this, $mod.IUnknown);',
  18210. '});',
  18211. '']),
  18212. LinesToStr([ // $mod.$main
  18213. '']));
  18214. end;
  18215. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18216. begin
  18217. StartProgram(false);
  18218. Add([
  18219. '{$interfaces corba}',
  18220. 'type',
  18221. ' IUnknown = interface',
  18222. ' end;',
  18223. ' IBird = interface(IUnknown)',
  18224. ' procedure Fly(s: string);',
  18225. ' end;',
  18226. ' IEagle = interface(IBird)',
  18227. ' end;',
  18228. ' IDove = interface(IBird)',
  18229. ' end;',
  18230. ' ISwallow = interface(IBird)',
  18231. ' end;',
  18232. ' TObject = class',
  18233. ' end;',
  18234. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18235. ' procedure Fly(s: string); virtual; abstract;',
  18236. ' end;',
  18237. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18238. ' FBirdIntf: IBird;',
  18239. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18240. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18241. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18242. ' FDoveObj: TBird;',
  18243. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18244. ' function GetSwallowObj: TBird; virtual; abstract;',
  18245. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18246. ' end;',
  18247. 'begin',
  18248. '']);
  18249. ConvertProgram;
  18250. CheckSource('TestClassInterface_Delegation',
  18251. LinesToStr([ // statements
  18252. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18253. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18254. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18255. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18256. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18257. 'rtl.createClass($mod, "TObject", null, function () {',
  18258. ' this.$init = function () {',
  18259. ' };',
  18260. ' this.$final = function () {',
  18261. ' };',
  18262. '});',
  18263. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18264. ' rtl.addIntf(this, $mod.IBird);',
  18265. ' rtl.addIntf(this, $mod.IEagle);',
  18266. ' rtl.addIntf(this, $mod.IDove);',
  18267. ' rtl.addIntf(this, $mod.ISwallow);',
  18268. '});',
  18269. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18270. ' this.$init = function () {',
  18271. ' $mod.TObject.$init.call(this);',
  18272. ' this.FBirdIntf = null;',
  18273. ' this.FDoveObj = null;',
  18274. ' };',
  18275. ' this.$final = function () {',
  18276. ' this.FBirdIntf = undefined;',
  18277. ' this.FDoveObj = undefined;',
  18278. ' $mod.TObject.$final.call(this);',
  18279. ' };',
  18280. ' this.$intfmaps = {',
  18281. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18282. ' return this.FBirdIntf;',
  18283. ' },',
  18284. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18285. ' return this.GetEagleIntf();',
  18286. ' },',
  18287. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18288. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18289. ' },',
  18290. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18291. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18292. ' }',
  18293. ' };',
  18294. '});',
  18295. '']),
  18296. LinesToStr([ // $mod.$main
  18297. '']));
  18298. end;
  18299. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18300. begin
  18301. StartProgram(false);
  18302. Add([
  18303. '{$interfaces corba}',
  18304. 'type',
  18305. ' IUnknown = interface',
  18306. ' end;',
  18307. ' IBird = interface(IUnknown)',
  18308. ' procedure Fly(s: string);',
  18309. ' end;',
  18310. ' IEagle = interface(IBird)',
  18311. ' end;',
  18312. ' IDove = interface(IBird)',
  18313. ' end;',
  18314. ' ISwallow = interface(IBird)',
  18315. ' end;',
  18316. ' TObject = class',
  18317. ' end;',
  18318. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18319. ' procedure Fly(s: string); virtual; abstract;',
  18320. ' end;',
  18321. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18322. ' private',
  18323. ' class var FBirdIntf: IBird;',
  18324. ' class var FDoveObj: TBird;',
  18325. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18326. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18327. ' protected',
  18328. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18329. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18330. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18331. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18332. ' end;',
  18333. 'begin',
  18334. '']);
  18335. ConvertProgram;
  18336. CheckSource('TestClassInterface_DelegationStatic',
  18337. LinesToStr([ // statements
  18338. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18339. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18340. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18341. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18342. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18343. 'rtl.createClass($mod, "TObject", null, function () {',
  18344. ' this.$init = function () {',
  18345. ' };',
  18346. ' this.$final = function () {',
  18347. ' };',
  18348. '});',
  18349. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18350. ' rtl.addIntf(this, $mod.IBird);',
  18351. ' rtl.addIntf(this, $mod.IEagle);',
  18352. ' rtl.addIntf(this, $mod.IDove);',
  18353. ' rtl.addIntf(this, $mod.ISwallow);',
  18354. '});',
  18355. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18356. ' this.FBirdIntf = null;',
  18357. ' this.FDoveObj = null;',
  18358. ' this.$intfmaps = {',
  18359. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18360. ' return this.FBirdIntf;',
  18361. ' },',
  18362. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18363. ' return this.GetEagleIntf();',
  18364. ' },',
  18365. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18366. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18367. ' },',
  18368. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18369. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18370. ' }',
  18371. ' };',
  18372. '});',
  18373. '']),
  18374. LinesToStr([ // $mod.$main
  18375. '']));
  18376. end;
  18377. procedure TTestModule.TestClassInterface_Corba_Operators;
  18378. begin
  18379. StartProgram(false);
  18380. Add([
  18381. '{$interfaces corba}',
  18382. 'type',
  18383. ' IUnknown = interface',
  18384. ' end;',
  18385. ' IBird = interface(IUnknown)',
  18386. ' function GetItems(Index: longint): longint;',
  18387. ' procedure SetItems(Index: longint; Value: longint);',
  18388. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18389. ' end;',
  18390. ' TObject = class',
  18391. ' end;',
  18392. ' TBird = class(TObject,IBird)',
  18393. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18394. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18395. ' end;',
  18396. 'var',
  18397. ' IntfVar: IBird = nil;',
  18398. ' IntfVar2: IBird;',
  18399. ' ObjVar: TBird;',
  18400. ' v: JSValue;',
  18401. 'begin',
  18402. ' IntfVar:=nil;',
  18403. ' IntfVar[3]:=IntfVar[4];',
  18404. ' if Assigned(IntfVar) then ;',
  18405. ' IntfVar:=IntfVar2;',
  18406. ' IntfVar:=ObjVar;',
  18407. ' if IntfVar=IntfVar2 then ;',
  18408. ' if IntfVar<>IntfVar2 then ;',
  18409. ' if IntfVar is IBird then ;',
  18410. ' if IntfVar is TBird then ;',
  18411. ' if ObjVar is IBird then ;',
  18412. ' IntfVar:=IntfVar2 as IBird;',
  18413. ' ObjVar:=IntfVar2 as TBird;',
  18414. ' IntfVar:=ObjVar as IBird;',
  18415. ' IntfVar:=IBird(IntfVar2);',
  18416. ' ObjVar:=TBird(IntfVar);',
  18417. ' IntfVar:=IBird(ObjVar);',
  18418. ' v:=IntfVar;',
  18419. ' IntfVar:=IBird(v);',
  18420. ' if v is IBird then ;',
  18421. ' v:=JSValue(IntfVar);',
  18422. ' v:=IBird;',
  18423. '']);
  18424. ConvertProgram;
  18425. CheckSource('TestClassInterface_Corba_Operators',
  18426. LinesToStr([ // statements
  18427. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18428. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  18429. 'rtl.createClass($mod, "TObject", null, function () {',
  18430. ' this.$init = function () {',
  18431. ' };',
  18432. ' this.$final = function () {',
  18433. ' };',
  18434. '});',
  18435. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18436. ' rtl.addIntf(this, $mod.IBird);',
  18437. '});',
  18438. 'this.IntfVar = null;',
  18439. 'this.IntfVar2 = null;',
  18440. 'this.ObjVar = null;',
  18441. 'this.v = undefined;',
  18442. '']),
  18443. LinesToStr([ // $mod.$main
  18444. '$mod.IntfVar = null;',
  18445. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18446. 'if ($mod.IntfVar != null) ;',
  18447. '$mod.IntfVar = $mod.IntfVar2;',
  18448. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18449. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18450. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18451. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18452. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18453. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18454. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18455. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18456. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18457. '$mod.IntfVar = $mod.IntfVar2;',
  18458. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18459. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18460. '$mod.v = $mod.IntfVar;',
  18461. '$mod.IntfVar = rtl.getObject($mod.v);',
  18462. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18463. '$mod.v = $mod.IntfVar;',
  18464. '$mod.v = $mod.IBird;',
  18465. '']));
  18466. end;
  18467. procedure TTestModule.TestClassInterface_Corba_Args;
  18468. begin
  18469. StartProgram(false);
  18470. Add([
  18471. '{$interfaces corba}',
  18472. 'type',
  18473. ' IUnknown = interface',
  18474. ' end;',
  18475. ' IBird = interface(IUnknown)',
  18476. ' end;',
  18477. ' TObject = class',
  18478. ' end;',
  18479. ' TBird = class(TObject,IBird)',
  18480. ' end;',
  18481. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18482. 'begin',
  18483. ' DoIt(i,i,i);',
  18484. 'end;',
  18485. 'procedure Change(var i: IBird; out j: IBird);',
  18486. 'begin',
  18487. ' DoIt(i,i,i);',
  18488. ' Change(i,i);',
  18489. 'end;',
  18490. 'var',
  18491. ' i: IBird;',
  18492. ' o: TBird;',
  18493. 'begin',
  18494. ' DoIt(i,i,i);',
  18495. ' Change(i,i);',
  18496. ' DoIt(o,o,o);',
  18497. '']);
  18498. ConvertProgram;
  18499. CheckSource('TestClassInterface_Corba_Args',
  18500. LinesToStr([ // statements
  18501. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18502. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  18503. 'rtl.createClass($mod, "TObject", null, function () {',
  18504. ' this.$init = function () {',
  18505. ' };',
  18506. ' this.$final = function () {',
  18507. ' };',
  18508. '});',
  18509. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18510. ' rtl.addIntf(this, $mod.IBird);',
  18511. '});',
  18512. 'this.DoIt = function (u, i, j) {',
  18513. ' $mod.DoIt({',
  18514. ' get: function () {',
  18515. ' return i;',
  18516. ' },',
  18517. ' set: function (v) {',
  18518. ' i = v;',
  18519. ' }',
  18520. ' }, i, i);',
  18521. '};',
  18522. 'this.Change = function (i, j) {',
  18523. ' $mod.DoIt(i, i.get(), i.get());',
  18524. ' $mod.Change(i, i);',
  18525. '};',
  18526. 'this.i = null;',
  18527. 'this.o = null;',
  18528. '']),
  18529. LinesToStr([ // $mod.$main
  18530. '$mod.DoIt({',
  18531. ' p: $mod,',
  18532. ' get: function () {',
  18533. ' return this.p.i;',
  18534. ' },',
  18535. ' set: function (v) {',
  18536. ' this.p.i = v;',
  18537. ' }',
  18538. '}, $mod.i, $mod.i);',
  18539. '$mod.Change({',
  18540. ' p: $mod,',
  18541. ' get: function () {',
  18542. ' return this.p.i;',
  18543. ' },',
  18544. ' set: function (v) {',
  18545. ' this.p.i = v;',
  18546. ' }',
  18547. '}, {',
  18548. ' p: $mod,',
  18549. ' get: function () {',
  18550. ' return this.p.i;',
  18551. ' },',
  18552. ' set: function (v) {',
  18553. ' this.p.i = v;',
  18554. ' }',
  18555. '});',
  18556. '$mod.DoIt({',
  18557. ' p: $mod,',
  18558. ' get: function () {',
  18559. ' return this.p.o;',
  18560. ' },',
  18561. ' set: function (v) {',
  18562. ' this.p.o = v;',
  18563. ' }',
  18564. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18565. '']));
  18566. end;
  18567. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18568. begin
  18569. StartProgram(false);
  18570. Add([
  18571. '{$interfaces corba}',
  18572. 'type',
  18573. ' IUnknown = interface end;',
  18574. ' TObject = class',
  18575. ' Id: longint;',
  18576. ' end;',
  18577. ' IEnumerator = interface(IUnknown)',
  18578. ' function GetCurrent: TObject;',
  18579. ' function MoveNext: Boolean;',
  18580. ' property Current: TObject read GetCurrent;',
  18581. ' end;',
  18582. ' IEnumerable = interface(IUnknown)',
  18583. ' function GetEnumerator: IEnumerator;',
  18584. ' end;',
  18585. 'var',
  18586. ' o: TObject;',
  18587. ' i: IEnumerable;',
  18588. 'begin',
  18589. ' for o in i do o.Id:=3;',
  18590. '']);
  18591. ConvertProgram;
  18592. CheckSource('TestClassInterface_Corba_ForIn',
  18593. LinesToStr([ // statements
  18594. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18595. 'rtl.createClass($mod, "TObject", null, function () {',
  18596. ' this.$init = function () {',
  18597. ' this.Id = 0;',
  18598. ' };',
  18599. ' this.$final = function () {',
  18600. ' };',
  18601. '});',
  18602. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18603. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18604. 'this.o = null;',
  18605. 'this.i = null;',
  18606. '']),
  18607. LinesToStr([ // $mod.$main
  18608. 'var $in1 = $mod.i.GetEnumerator();',
  18609. 'while ($in1.MoveNext()) {',
  18610. ' $mod.o = $in1.GetCurrent();',
  18611. ' $mod.o.Id = 3;',
  18612. '};',
  18613. '']));
  18614. end;
  18615. procedure TTestModule.TestClassInterface_COM_AssignVar;
  18616. begin
  18617. StartProgram(false);
  18618. Add([
  18619. '{$interfaces com}',
  18620. 'type',
  18621. ' IUnknown = interface',
  18622. ' function _AddRef: longint;',
  18623. ' function _Release: longint;',
  18624. ' end;',
  18625. ' TObject = class(IUnknown)',
  18626. ' function _AddRef: longint; virtual; abstract;',
  18627. ' function _Release: longint; virtual; abstract;',
  18628. ' end;',
  18629. 'var',
  18630. ' i: IUnknown;',
  18631. 'procedure DoGlobal(o: TObject);',
  18632. 'begin',
  18633. ' i:=nil;',
  18634. ' i:=o;',
  18635. ' i:=i;',
  18636. 'end;',
  18637. 'procedure DoLocal(o: TObject);',
  18638. 'const k: IUnknown = nil;',
  18639. 'var j: IUnknown;',
  18640. 'begin',
  18641. ' k:=o;',
  18642. ' k:=i;',
  18643. ' j:=o;',
  18644. ' j:=i;',
  18645. 'end;',
  18646. 'var o: TObject;',
  18647. 'begin',
  18648. ' i:=nil;',
  18649. ' i:=o;',
  18650. '']);
  18651. ConvertProgram;
  18652. CheckSource('TestClassInterface_COM_AssignVar',
  18653. LinesToStr([ // statements
  18654. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18655. 'rtl.createClass($mod, "TObject", null, function () {',
  18656. ' this.$init = function () {',
  18657. ' };',
  18658. ' this.$final = function () {',
  18659. ' };',
  18660. ' rtl.addIntf(this, $mod.IUnknown);',
  18661. '});',
  18662. 'this.i = null;',
  18663. 'this.DoGlobal = function (o) {',
  18664. ' rtl.setIntfP($mod, "i", null);',
  18665. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18666. ' rtl.setIntfP($mod, "i", $mod.i);',
  18667. '};',
  18668. 'var k = null;',
  18669. 'this.DoLocal = function (o) {',
  18670. ' var j = null;',
  18671. ' try{',
  18672. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18673. ' k = rtl.setIntfL(k, $mod.i);',
  18674. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18675. ' j = rtl.setIntfL(j, $mod.i);',
  18676. ' }finally{',
  18677. ' rtl._Release(j);',
  18678. ' };',
  18679. '};',
  18680. 'this.o = null;',
  18681. '']),
  18682. LinesToStr([ // $mod.$main
  18683. 'rtl.setIntfP($mod, "i", null);',
  18684. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18685. '']));
  18686. end;
  18687. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18688. begin
  18689. StartProgram(false);
  18690. Add([
  18691. '{$interfaces com}',
  18692. 'type',
  18693. ' IUnknown = interface',
  18694. ' function _AddRef: longint;',
  18695. ' function _Release: longint;',
  18696. ' end;',
  18697. ' TObject = class(IUnknown)',
  18698. ' function _AddRef: longint; virtual; abstract;',
  18699. ' function _Release: longint; virtual; abstract;',
  18700. ' end;',
  18701. 'procedure DoDefault(i, j: IUnknown);',
  18702. 'begin',
  18703. ' i:=nil;',
  18704. ' i:=j;',
  18705. 'end;',
  18706. 'begin',
  18707. '']);
  18708. ConvertProgram;
  18709. CheckSource('TestClassInterface_COM_AssignArg',
  18710. LinesToStr([ // statements
  18711. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18712. 'rtl.createClass($mod, "TObject", null, function () {',
  18713. ' this.$init = function () {',
  18714. ' };',
  18715. ' this.$final = function () {',
  18716. ' };',
  18717. ' rtl.addIntf(this, $mod.IUnknown);',
  18718. '});',
  18719. 'this.DoDefault = function (i, j) {',
  18720. ' rtl._AddRef(i);',
  18721. ' try {',
  18722. ' i = rtl.setIntfL(i, null);',
  18723. ' i = rtl.setIntfL(i, j);',
  18724. ' } finally {',
  18725. ' rtl._Release(i);',
  18726. ' };',
  18727. '};',
  18728. '']),
  18729. LinesToStr([ // $mod.$main
  18730. '']));
  18731. end;
  18732. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18733. begin
  18734. StartProgram(false);
  18735. Add([
  18736. '{$interfaces com}',
  18737. 'type',
  18738. ' IUnknown = interface',
  18739. ' function _AddRef: longint;',
  18740. ' function _Release: longint;',
  18741. ' end;',
  18742. ' TObject = class(IUnknown)',
  18743. ' function _AddRef: longint; virtual; abstract;',
  18744. ' function _Release: longint; virtual; abstract;',
  18745. ' end;',
  18746. 'function DoDefault(i: IUnknown): IUnknown;',
  18747. 'begin',
  18748. ' Result:=i;',
  18749. ' if Result<>nil then exit;',
  18750. 'end;',
  18751. 'begin',
  18752. '']);
  18753. ConvertProgram;
  18754. CheckSource('TestClassInterface_COM_FunctionResult',
  18755. LinesToStr([ // statements
  18756. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18757. 'rtl.createClass($mod, "TObject", null, function () {',
  18758. ' this.$init = function () {',
  18759. ' };',
  18760. ' this.$final = function () {',
  18761. ' };',
  18762. ' rtl.addIntf(this, $mod.IUnknown);',
  18763. '});',
  18764. 'this.DoDefault = function (i) {',
  18765. ' var Result = null;',
  18766. ' var $ok = false;',
  18767. ' try {',
  18768. ' Result = rtl.setIntfL(Result, i);',
  18769. ' if(Result !== null){',
  18770. ' $ok = true;',
  18771. ' return Result;',
  18772. ' };',
  18773. ' $ok = true;',
  18774. ' } finally {',
  18775. ' if(!$ok) rtl._Release(Result);',
  18776. ' };',
  18777. ' return Result;',
  18778. '};',
  18779. '']),
  18780. LinesToStr([ // $mod.$main
  18781. '']));
  18782. end;
  18783. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18784. begin
  18785. StartProgram(false);
  18786. Add([
  18787. '{$interfaces com}',
  18788. 'type',
  18789. ' IUnknown = interface',
  18790. ' function _AddRef: longint;',
  18791. ' function _Release: longint;',
  18792. ' end;',
  18793. ' TObject = class(IUnknown)',
  18794. ' function _AddRef: longint; virtual; abstract;',
  18795. ' function _Release: longint; virtual; abstract;',
  18796. ' function GetIntf: IUnknown; virtual;',
  18797. ' end;',
  18798. ' TMouse = class',
  18799. ' function GetIntf: IUnknown; override;',
  18800. ' end;',
  18801. 'function TObject.GetIntf: IUnknown; begin end;',
  18802. 'function TMouse.GetIntf: IUnknown;',
  18803. 'var i: IUnknown;',
  18804. 'begin',
  18805. ' inherited;',
  18806. ' inherited GetIntf;',
  18807. ' inherited GetIntf();',
  18808. ' Result:=inherited GetIntf;',
  18809. ' Result:=inherited GetIntf();',
  18810. ' i:=inherited GetIntf;',
  18811. ' i:=inherited GetIntf();',
  18812. 'end;',
  18813. 'begin',
  18814. '']);
  18815. ConvertProgram;
  18816. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18817. LinesToStr([ // statements
  18818. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18819. 'rtl.createClass($mod, "TObject", null, function () {',
  18820. ' this.$init = function () {',
  18821. ' };',
  18822. ' this.$final = function () {',
  18823. ' };',
  18824. ' this.GetIntf = function () {',
  18825. ' var Result = null;',
  18826. ' return Result;',
  18827. ' };',
  18828. ' rtl.addIntf(this, $mod.IUnknown);',
  18829. '});',
  18830. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18831. ' this.GetIntf = function () {',
  18832. ' var Result = null;',
  18833. ' var i = null;',
  18834. ' var $ir = rtl.createIntfRefs();',
  18835. ' var $ok = false;',
  18836. ' try {',
  18837. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18838. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18839. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18840. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18841. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18842. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18843. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18844. ' $ok = true;',
  18845. ' } finally {',
  18846. ' $ir.free();',
  18847. ' rtl._Release(i);',
  18848. ' if (!$ok) rtl._Release(Result);',
  18849. ' };',
  18850. ' return Result;',
  18851. ' };',
  18852. ' rtl.addIntf(this, $mod.IUnknown);',
  18853. '});',
  18854. '']),
  18855. LinesToStr([ // $mod.$main
  18856. '']));
  18857. end;
  18858. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18859. begin
  18860. StartProgram(false);
  18861. Add([
  18862. '{$interfaces com}',
  18863. 'type',
  18864. ' IUnknown = interface',
  18865. ' function _AddRef: longint;',
  18866. ' function _Release: longint;',
  18867. ' end;',
  18868. ' TObject = class(IUnknown)',
  18869. ' function _AddRef: longint; virtual; abstract;',
  18870. ' function _Release: longint; virtual; abstract;',
  18871. ' end;',
  18872. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18873. 'begin',
  18874. ' if i is IUnknown then ;',
  18875. ' if o is IUnknown then ;',
  18876. ' if i is TObject then ;',
  18877. ' i:=j as IUnknown;',
  18878. ' i:=o as IUnknown;',
  18879. ' o:=j as TObject;',
  18880. ' i:=IUnknown(j);',
  18881. ' i:=IUnknown(o);',
  18882. ' o:=TObject(i);',
  18883. 'end;',
  18884. 'begin',
  18885. '']);
  18886. ConvertProgram;
  18887. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  18888. LinesToStr([ // statements
  18889. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18890. 'rtl.createClass($mod, "TObject", null, function () {',
  18891. ' this.$init = function () {',
  18892. ' };',
  18893. ' this.$final = function () {',
  18894. ' };',
  18895. ' rtl.addIntf(this, $mod.IUnknown);',
  18896. '});',
  18897. 'this.DoDefault = function (i, j, o) {',
  18898. ' rtl._AddRef(i);',
  18899. ' try {',
  18900. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  18901. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  18902. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  18903. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  18904. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18905. ' o = rtl.intfAsClass(j, $mod.TObject);',
  18906. ' i = rtl.setIntfL(i, j);',
  18907. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18908. ' o = rtl.intfToClass(i, $mod.TObject);',
  18909. ' } finally {',
  18910. ' rtl._Release(i);',
  18911. ' };',
  18912. '};',
  18913. '']),
  18914. LinesToStr([ // $mod.$main
  18915. '']));
  18916. end;
  18917. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  18918. begin
  18919. StartProgram(false);
  18920. Add([
  18921. '{$interfaces com}',
  18922. 'type',
  18923. ' IUnknown = interface',
  18924. ' function _AddRef: longint;',
  18925. ' function _Release: longint;',
  18926. ' end;',
  18927. ' TObject = class(IUnknown)',
  18928. ' function _AddRef: longint; virtual; abstract;',
  18929. ' function _Release: longint; virtual; abstract;',
  18930. ' end;',
  18931. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  18932. 'var o: TObject;',
  18933. 'begin',
  18934. ' DoIt(v,v,v,v);',
  18935. ' DoIt(o,o,k,k);',
  18936. 'end;',
  18937. 'procedure DoSome;',
  18938. 'var v: IUnknown;',
  18939. 'begin',
  18940. ' DoIt(v,v,v,v);',
  18941. 'end;',
  18942. 'var i: IUnknown;',
  18943. 'begin',
  18944. ' DoIt(i,i,i,i);',
  18945. '']);
  18946. ConvertProgram;
  18947. CheckSource('TestClassInterface_COM_PassAsArg',
  18948. LinesToStr([ // statements
  18949. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18950. 'rtl.createClass($mod, "TObject", null, function () {',
  18951. ' this.$init = function () {',
  18952. ' };',
  18953. ' this.$final = function () {',
  18954. ' };',
  18955. ' rtl.addIntf(this, $mod.IUnknown);',
  18956. '});',
  18957. 'this.DoIt = function (v, j, k, l) {',
  18958. ' var o = null;',
  18959. ' var $ir = rtl.createIntfRefs();',
  18960. ' rtl._AddRef(v);',
  18961. ' try {',
  18962. ' $mod.DoIt(v, v, {',
  18963. ' get: function () {',
  18964. ' return v;',
  18965. ' },',
  18966. ' set: function (w) {',
  18967. ' v = rtl.setIntfL(v, w);',
  18968. ' }',
  18969. ' }, {',
  18970. ' get: function () {',
  18971. ' return v;',
  18972. ' },',
  18973. ' set: function (w) {',
  18974. ' v = rtl.setIntfL(v, w);',
  18975. ' }',
  18976. ' });',
  18977. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  18978. ' } finally {',
  18979. ' $ir.free();',
  18980. ' rtl._Release(v);',
  18981. ' };',
  18982. '};',
  18983. 'this.DoSome = function () {',
  18984. ' var v = null;',
  18985. ' try {',
  18986. ' $mod.DoIt(v, v, {',
  18987. ' get: function () {',
  18988. ' return v;',
  18989. ' },',
  18990. ' set: function (w) {',
  18991. ' v = rtl.setIntfL(v, w);',
  18992. ' }',
  18993. ' }, {',
  18994. ' get: function () {',
  18995. ' return v;',
  18996. ' },',
  18997. ' set: function (w) {',
  18998. ' v = rtl.setIntfL(v, w);',
  18999. ' }',
  19000. ' });',
  19001. ' } finally {',
  19002. ' rtl._Release(v);',
  19003. ' };',
  19004. '};',
  19005. 'this.i = null;',
  19006. '']),
  19007. LinesToStr([ // $mod.$main
  19008. '$mod.DoIt($mod.i, $mod.i, {',
  19009. ' p: $mod,',
  19010. ' get: function () {',
  19011. ' return this.p.i;',
  19012. ' },',
  19013. ' set: function (v) {',
  19014. ' rtl.setIntfP(this.p, "i", v);',
  19015. ' }',
  19016. '}, {',
  19017. ' p: $mod,',
  19018. ' get: function () {',
  19019. ' return this.p.i;',
  19020. ' },',
  19021. ' set: function (v) {',
  19022. ' rtl.setIntfP(this.p, "i", v);',
  19023. ' }',
  19024. '});',
  19025. '']));
  19026. end;
  19027. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19028. begin
  19029. StartProgram(false);
  19030. Add([
  19031. '{$interfaces com}',
  19032. 'type',
  19033. ' IUnknown = interface',
  19034. ' function _AddRef: longint;',
  19035. ' function _Release: longint;',
  19036. ' end;',
  19037. ' TObject = class(IUnknown)',
  19038. ' function _AddRef: longint; virtual; abstract;',
  19039. ' function _Release: longint; virtual; abstract;',
  19040. ' end;',
  19041. 'procedure DoIt(out i);',
  19042. 'begin end;',
  19043. 'procedure DoSome;',
  19044. 'var v: IUnknown;',
  19045. 'begin',
  19046. ' DoIt(v);',
  19047. 'end;',
  19048. 'function GetIt: IUnknown;',
  19049. 'begin',
  19050. ' DoIt(Result);',
  19051. 'end;',
  19052. 'var i: IUnknown;',
  19053. 'begin',
  19054. ' DoIt(i);',
  19055. '']);
  19056. ConvertProgram;
  19057. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19058. LinesToStr([ // statements
  19059. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19060. 'rtl.createClass($mod, "TObject", null, function () {',
  19061. ' this.$init = function () {',
  19062. ' };',
  19063. ' this.$final = function () {',
  19064. ' };',
  19065. ' rtl.addIntf(this, $mod.IUnknown);',
  19066. '});',
  19067. 'this.DoIt = function (i) {',
  19068. '};',
  19069. 'this.DoSome = function () {',
  19070. ' var v = null;',
  19071. ' try {',
  19072. ' $mod.DoIt({',
  19073. ' get: function () {',
  19074. ' return v;',
  19075. ' },',
  19076. ' set: function (w) {',
  19077. ' v = w;',
  19078. ' }',
  19079. ' });',
  19080. ' } finally {',
  19081. ' rtl._Release(v);',
  19082. ' };',
  19083. '};',
  19084. 'this.GetIt = function () {',
  19085. ' var Result = null;',
  19086. ' var $ok = false;',
  19087. ' try {',
  19088. ' $mod.DoIt({',
  19089. ' get: function () {',
  19090. ' return Result;',
  19091. ' },',
  19092. ' set: function (v) {',
  19093. ' Result = v;',
  19094. ' }',
  19095. ' });',
  19096. ' $ok = true;',
  19097. ' } finally {',
  19098. ' if (!$ok) rtl._Release(Result);',
  19099. ' };',
  19100. ' return Result;',
  19101. '};',
  19102. 'this.i = null;',
  19103. '']),
  19104. LinesToStr([ // $mod.$main
  19105. 'try {',
  19106. ' $mod.DoIt({',
  19107. ' p: $mod,',
  19108. ' get: function () {',
  19109. ' return this.p.i;',
  19110. ' },',
  19111. ' set: function (v) {',
  19112. ' this.p.i = v;',
  19113. ' }',
  19114. ' });',
  19115. '} finally {',
  19116. ' rtl._Release($mod.i);',
  19117. '};',
  19118. '']));
  19119. end;
  19120. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19121. begin
  19122. StartProgram(false);
  19123. Add([
  19124. '{$interfaces com}',
  19125. 'type',
  19126. ' IUnknown = interface',
  19127. ' function _AddRef: longint;',
  19128. ' function _Release: longint;',
  19129. ' end;',
  19130. ' TObject = class(IUnknown)',
  19131. ' function _AddRef: longint; virtual; abstract;',
  19132. ' function _Release: longint; virtual; abstract;',
  19133. ' end;',
  19134. 'function GetIt: IUnknown;',
  19135. 'begin',
  19136. 'end;',
  19137. 'procedure DoSome;',
  19138. 'var v: IUnknown;',
  19139. ' i: longint;',
  19140. 'begin',
  19141. ' v:=GetIt;',
  19142. ' v:=GetIt();',
  19143. ' GetIt()._AddRef;',
  19144. ' i:=GetIt()._AddRef;',
  19145. 'end;',
  19146. 'var v: IUnknown;',
  19147. ' i: longint;',
  19148. 'begin',
  19149. ' v:=GetIt;',
  19150. ' v:=GetIt();',
  19151. ' GetIt()._AddRef;',
  19152. ' i:=GetIt()._AddRef;',
  19153. '']);
  19154. ConvertProgram;
  19155. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19156. LinesToStr([ // statements
  19157. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19158. 'rtl.createClass($mod, "TObject", null, function () {',
  19159. ' this.$init = function () {',
  19160. ' };',
  19161. ' this.$final = function () {',
  19162. ' };',
  19163. ' rtl.addIntf(this, $mod.IUnknown);',
  19164. '});',
  19165. 'this.GetIt = function () {',
  19166. ' var Result = null;',
  19167. ' return Result;',
  19168. '};',
  19169. 'this.DoSome = function () {',
  19170. ' var v = null;',
  19171. ' var i = 0;',
  19172. ' var $ir = rtl.createIntfRefs();',
  19173. ' try {',
  19174. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19175. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19176. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19177. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19178. ' } finally {',
  19179. ' $ir.free();',
  19180. ' rtl._Release(v);',
  19181. ' };',
  19182. '};',
  19183. 'this.v = null;',
  19184. 'this.i = 0;',
  19185. '']),
  19186. LinesToStr([ // $mod.$main
  19187. 'var $ir = rtl.createIntfRefs();',
  19188. 'try {',
  19189. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19190. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19191. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19192. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19193. '} finally {',
  19194. ' $ir.free();',
  19195. '};',
  19196. '']));
  19197. end;
  19198. procedure TTestModule.TestClassInterface_COM_Property;
  19199. begin
  19200. StartProgram(false);
  19201. Add([
  19202. '{$interfaces com}',
  19203. 'type',
  19204. ' IUnknown = interface',
  19205. ' function _AddRef: longint;',
  19206. ' function _Release: longint;',
  19207. ' end;',
  19208. ' TObject = class(IUnknown)',
  19209. ' FAnt: IUnknown;',
  19210. ' function _AddRef: longint; virtual; abstract;',
  19211. ' function _Release: longint; virtual; abstract;',
  19212. ' function GetBird: IUnknown; virtual; abstract;',
  19213. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19214. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19215. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19216. ' property Ant: IUnknown read FAnt write FAnt;',
  19217. ' property Bird: IUnknown read GetBird write SetBird;',
  19218. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19219. ' end;',
  19220. 'procedure DoIt;',
  19221. 'var',
  19222. ' o: TObject;',
  19223. ' v: IUnknown;',
  19224. 'begin',
  19225. ' v:=o.Ant;',
  19226. ' o.Ant:=v;',
  19227. ' o.Ant:=o.Ant;',
  19228. ' v:=o.Bird;',
  19229. ' o.Bird:=v;',
  19230. ' o.Bird:=o.Bird;',
  19231. ' v:=o.Items[1];',
  19232. ' o.Items[2]:=v;',
  19233. ' o.Items[3]:=o.Items[4];',
  19234. ' v:=o[5];',
  19235. ' o[6]:=v;',
  19236. ' o[7]:=o[8];',
  19237. 'end;',
  19238. 'begin',
  19239. '']);
  19240. ConvertProgram;
  19241. CheckSource('TestClassInterface_COM_Property',
  19242. LinesToStr([ // statements
  19243. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19244. 'rtl.createClass($mod, "TObject", null, function () {',
  19245. ' this.$init = function () {',
  19246. ' this.FAnt = null;',
  19247. ' };',
  19248. ' this.$final = function () {',
  19249. ' this.FAnt = undefined;',
  19250. ' };',
  19251. ' rtl.addIntf(this, $mod.IUnknown);',
  19252. '});',
  19253. 'this.DoIt = function () {',
  19254. ' var o = null;',
  19255. ' var v = null;',
  19256. ' var $ir = rtl.createIntfRefs();',
  19257. ' try {',
  19258. ' v = rtl.setIntfL(v, o.FAnt);',
  19259. ' rtl.setIntfP(o, "FAnt", v);',
  19260. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19261. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19262. ' o.SetBird(v);',
  19263. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19264. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19265. ' o.SetItems(2, v);',
  19266. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19267. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19268. ' o.SetItems(6, v);',
  19269. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19270. ' } finally {',
  19271. ' $ir.free();',
  19272. ' rtl._Release(v);',
  19273. ' };',
  19274. '};',
  19275. '']),
  19276. LinesToStr([ // $mod.$main
  19277. '']));
  19278. end;
  19279. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19280. begin
  19281. StartProgram(false);
  19282. Add([
  19283. '{$interfaces com}',
  19284. 'type',
  19285. ' IUnknown = interface',
  19286. ' function _AddRef: longint;',
  19287. ' function _Release: longint;',
  19288. ' function GetBird: IUnknown;',
  19289. ' procedure SetBird(Value: IUnknown);',
  19290. ' function GetItems(Index: longint): IUnknown;',
  19291. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19292. ' property Bird: IUnknown read GetBird write SetBird;',
  19293. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19294. ' end;',
  19295. ' TObject = class(IUnknown)',
  19296. ' function _AddRef: longint; virtual; abstract;',
  19297. ' function _Release: longint; virtual; abstract;',
  19298. ' function GetBird: IUnknown; virtual; abstract;',
  19299. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19300. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19301. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19302. ' end;',
  19303. 'procedure DoIt;',
  19304. 'var',
  19305. ' o: TObject;',
  19306. ' v: IUnknown;',
  19307. 'begin',
  19308. ' v:=v.Items[1];',
  19309. ' v.Items[2]:=v;',
  19310. ' v.Items[3]:=v.Items[4];',
  19311. ' v:=v[5];',
  19312. ' v[6]:=v;',
  19313. ' v[7]:=v[8];',
  19314. ' v[9].Bird.Bird:=v;',
  19315. ' v:=v.Bird[10].Bird',
  19316. 'end;',
  19317. 'begin',
  19318. '']);
  19319. ConvertProgram;
  19320. CheckSource('TestClassInterface_COM_IntfProperty',
  19321. LinesToStr([ // statements
  19322. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19323. ' "_AddRef",',
  19324. ' "_Release",',
  19325. ' "GetBird",',
  19326. ' "SetBird",',
  19327. ' "GetItems",',
  19328. ' "SetItems"',
  19329. '], null);',
  19330. 'rtl.createClass($mod, "TObject", null, function () {',
  19331. ' this.$init = function () {',
  19332. ' };',
  19333. ' this.$final = function () {',
  19334. ' };',
  19335. ' rtl.addIntf(this, $mod.IUnknown);',
  19336. '});',
  19337. 'this.DoIt = function () {',
  19338. ' var o = null;',
  19339. ' var v = null;',
  19340. ' var $ir = rtl.createIntfRefs();',
  19341. ' try {',
  19342. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19343. ' v.SetItems(2, v);',
  19344. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19345. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19346. ' v.SetItems(6, v);',
  19347. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19348. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19349. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19350. ' } finally {',
  19351. ' $ir.free();',
  19352. ' rtl._Release(v);',
  19353. ' };',
  19354. '};',
  19355. '']),
  19356. LinesToStr([ // $mod.$main
  19357. '']));
  19358. end;
  19359. procedure TTestModule.TestClassInterface_COM_Delegation;
  19360. begin
  19361. StartProgram(false);
  19362. Add([
  19363. '{$interfaces com}',
  19364. 'type',
  19365. ' IUnknown = interface',
  19366. ' function _AddRef: longint;',
  19367. ' function _Release: longint;',
  19368. ' end;',
  19369. ' IBird = interface(IUnknown)',
  19370. ' procedure Fly(s: string);',
  19371. ' end;',
  19372. ' IEagle = interface(IBird) end;',
  19373. ' IDove = interface(IBird) end;',
  19374. ' ISwallow = interface(IBird) end;',
  19375. ' TObject = class',
  19376. ' end;',
  19377. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19378. ' function _AddRef: longint; virtual; abstract;',
  19379. ' function _Release: longint; virtual; abstract;',
  19380. ' procedure Fly(s: string); virtual; abstract;',
  19381. ' end;',
  19382. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19383. ' function _AddRef: longint; virtual; abstract;',
  19384. ' function _Release: longint; virtual; abstract;',
  19385. ' FBirdIntf: IBird;',
  19386. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19387. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19388. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19389. ' FDoveObj: TBird;',
  19390. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19391. ' function GetSwallowObj: TBird; virtual; abstract;',
  19392. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19393. ' end;',
  19394. 'begin',
  19395. '']);
  19396. ConvertProgram;
  19397. CheckSource('TestClassInterface_COM_Delegation',
  19398. LinesToStr([ // statements
  19399. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19400. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  19401. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  19402. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  19403. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  19404. 'rtl.createClass($mod, "TObject", null, function () {',
  19405. ' this.$init = function () {',
  19406. ' };',
  19407. ' this.$final = function () {',
  19408. ' };',
  19409. '});',
  19410. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19411. ' rtl.addIntf(this, $mod.IBird);',
  19412. ' rtl.addIntf(this, $mod.IEagle);',
  19413. ' rtl.addIntf(this, $mod.IDove);',
  19414. ' rtl.addIntf(this, $mod.ISwallow);',
  19415. '});',
  19416. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  19417. ' this.$init = function () {',
  19418. ' $mod.TObject.$init.call(this);',
  19419. ' this.FBirdIntf = null;',
  19420. ' this.FDoveObj = null;',
  19421. ' };',
  19422. ' this.$final = function () {',
  19423. ' this.FBirdIntf = undefined;',
  19424. ' this.FDoveObj = undefined;',
  19425. ' $mod.TObject.$final.call(this);',
  19426. ' };',
  19427. ' this.$intfmaps = {',
  19428. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19429. ' return rtl._AddRef(this.FBirdIntf);',
  19430. ' },',
  19431. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19432. ' return this.GetEagleIntf();',
  19433. ' },',
  19434. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19435. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19436. ' },',
  19437. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19438. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19439. ' }',
  19440. ' };',
  19441. '});',
  19442. '']),
  19443. LinesToStr([ // $mod.$main
  19444. '']));
  19445. end;
  19446. procedure TTestModule.TestClassInterface_COM_With;
  19447. begin
  19448. StartProgram(false);
  19449. Add([
  19450. '{$interfaces com}',
  19451. 'type',
  19452. ' IUnknown = interface',
  19453. ' function _AddRef: longint;',
  19454. ' function _Release: longint;',
  19455. ' function GetAnt: IUnknown;',
  19456. ' property Ant: IUnknown read GetAnt;',
  19457. ' end;',
  19458. ' TObject = class(IUnknown)',
  19459. ' function _AddRef: longint; virtual; abstract;',
  19460. ' function _Release: longint; virtual; abstract;',
  19461. ' function GetAnt: IUnknown; virtual; abstract;',
  19462. ' property Ant: IUnknown read GetAnt;',
  19463. ' end;',
  19464. 'procedure DoIt;',
  19465. 'var',
  19466. ' i: IUnknown;',
  19467. 'begin',
  19468. ' with i do ',
  19469. ' GetAnt;',
  19470. ' with i.Ant, Ant do ',
  19471. ' GetAnt;',
  19472. 'end;',
  19473. 'begin',
  19474. '']);
  19475. ConvertProgram;
  19476. CheckSource('TestClassInterface_COM_With',
  19477. LinesToStr([ // statements
  19478. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19479. 'rtl.createClass($mod, "TObject", null, function () {',
  19480. ' this.$init = function () {',
  19481. ' };',
  19482. ' this.$final = function () {',
  19483. ' };',
  19484. ' rtl.addIntf(this, $mod.IUnknown);',
  19485. '});',
  19486. 'this.DoIt = function () {',
  19487. ' var i = null;',
  19488. ' var $ir = rtl.createIntfRefs();',
  19489. ' try {',
  19490. ' $ir.ref(1, i.GetAnt());',
  19491. ' var $with1 = $ir.ref(2, i.GetAnt());',
  19492. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  19493. ' $ir.ref(4, $with2.GetAnt());',
  19494. ' } finally {',
  19495. ' $ir.free();',
  19496. ' };',
  19497. '};',
  19498. '']),
  19499. LinesToStr([ // $mod.$main
  19500. '']));
  19501. end;
  19502. procedure TTestModule.TestClassInterface_COM_ForIn;
  19503. begin
  19504. StartProgram(false);
  19505. Add([
  19506. '{$interfaces com}',
  19507. 'type',
  19508. ' IUnknown = interface end;',
  19509. ' TObject = class',
  19510. ' Id: longint;',
  19511. ' end;',
  19512. ' IEnumerator = interface(IUnknown)',
  19513. ' function GetCurrent: TObject;',
  19514. ' function MoveNext: Boolean;',
  19515. ' property Current: TObject read GetCurrent;',
  19516. ' end;',
  19517. ' IEnumerable = interface(IUnknown)',
  19518. ' function GetEnumerator: IEnumerator;',
  19519. ' end;',
  19520. 'var',
  19521. ' o: TObject;',
  19522. ' i: IEnumerable;',
  19523. 'begin',
  19524. ' for o in i do o.Id:=3;',
  19525. '']);
  19526. ConvertProgram;
  19527. CheckSource('TestClassInterface_COM_ForIn',
  19528. LinesToStr([ // statements
  19529. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19530. 'rtl.createClass($mod, "TObject", null, function () {',
  19531. ' this.$init = function () {',
  19532. ' this.Id = 0;',
  19533. ' };',
  19534. ' this.$final = function () {',
  19535. ' };',
  19536. '});',
  19537. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  19538. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  19539. 'this.o = null;',
  19540. 'this.i = null;',
  19541. '']),
  19542. LinesToStr([ // $mod.$main
  19543. 'var $in1 = $mod.i.GetEnumerator();',
  19544. 'try {',
  19545. ' while ($in1.MoveNext()) {',
  19546. ' $mod.o = $in1.GetCurrent();',
  19547. ' $mod.o.Id = 3;',
  19548. ' }',
  19549. '} finally {',
  19550. ' rtl._Release($in1)',
  19551. '};',
  19552. '']));
  19553. end;
  19554. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19555. begin
  19556. StartProgram(false);
  19557. Add([
  19558. '{$interfaces com}',
  19559. 'type',
  19560. ' IUnknown = interface',
  19561. ' function _AddRef: longint;',
  19562. ' function _Release: longint;',
  19563. ' end;',
  19564. ' TObject = class',
  19565. ' end;',
  19566. ' TArrOfIntf = array of IUnknown;',
  19567. 'begin',
  19568. '']);
  19569. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19570. ConvertProgram;
  19571. end;
  19572. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19573. begin
  19574. StartProgram(false);
  19575. Add([
  19576. '{$interfaces com}',
  19577. 'type',
  19578. ' IUnknown = interface',
  19579. ' function _AddRef: longint;',
  19580. ' function _Release: longint;',
  19581. ' end;',
  19582. ' TRec = record',
  19583. ' i: IUnknown;',
  19584. ' end;',
  19585. 'begin',
  19586. '']);
  19587. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  19588. ConvertProgram;
  19589. end;
  19590. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  19591. begin
  19592. StartUnit(false);
  19593. Add([
  19594. '{$interfaces com}',
  19595. 'interface',
  19596. 'implementation',
  19597. 'type',
  19598. ' IUnknown = interface',
  19599. ' function _AddRef: longint;',
  19600. ' end;',
  19601. ' TObject = class(IUnknown)',
  19602. ' function _AddRef: longint;',
  19603. ' end;',
  19604. 'function TObject._AddRef: longint; begin end;',
  19605. 'var i: IUnknown;',
  19606. ' o: TObject;',
  19607. 'initialization',
  19608. ' i:=nil;',
  19609. ' i:=i;',
  19610. ' i:=o;',
  19611. ' if (o as IUnknown)=nil then ;',
  19612. '']);
  19613. ConvertUnit;
  19614. CheckSource('TestClassInterface_COM_UnitInitialization',
  19615. LinesToStr([ // statements
  19616. 'var $impl = $mod.$impl;',
  19617. '']),
  19618. LinesToStr([ // this.$init
  19619. 'var $ir = rtl.createIntfRefs();',
  19620. 'try {',
  19621. ' rtl.setIntfP($impl, "i", null);',
  19622. ' rtl.setIntfP($impl, "i", $impl.i);',
  19623. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19624. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19625. '} finally {',
  19626. ' $ir.free();',
  19627. '};',
  19628. '']),
  19629. LinesToStr([ // implementation
  19630. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19631. 'rtl.createClass($impl, "TObject", null, function () {',
  19632. ' this.$init = function () {',
  19633. ' };',
  19634. ' this.$final = function () {',
  19635. ' };',
  19636. ' this._AddRef = function () {',
  19637. ' var Result = 0;',
  19638. ' return Result;',
  19639. ' };',
  19640. ' rtl.addIntf(this, $impl.IUnknown);',
  19641. '});',
  19642. '$impl.i = null;',
  19643. '$impl.o = null;',
  19644. ''])
  19645. );
  19646. end;
  19647. procedure TTestModule.TestClassInterface_GUID;
  19648. begin
  19649. StartProgram(false);
  19650. Add([
  19651. '{$interfaces corba}',
  19652. 'type',
  19653. ' IUnknown = interface',
  19654. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19655. ' end;',
  19656. ' TObject = class end;',
  19657. ' TGUID = record D1, D2, D3, D4: word; end;',
  19658. ' TAliasGUID = TGUID;',
  19659. ' TGUIDString = type string;',
  19660. ' TAliasGUIDString = TGUIDString;',
  19661. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19662. 'begin end;',
  19663. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19664. 'begin end;',
  19665. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19666. 'begin end;',
  19667. 'var',
  19668. ' i: IUnknown;',
  19669. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19670. ' s: TAliasGUIDString;',
  19671. 'begin',
  19672. ' DoConstGUIDIt(IUnknown);',
  19673. ' DoDefGUID(IUnknown);',
  19674. ' DoStr(IUnknown);',
  19675. ' DoConstGUIDIt(i);',
  19676. ' DoDefGUID(i);',
  19677. ' DoStr(i);',
  19678. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19679. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19680. ' DoStr(g);',
  19681. ' g:=i;',
  19682. ' g:=IUnknown;',
  19683. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19684. ' s:=i;',
  19685. ' s:=IUnknown;',
  19686. ' s:=g;',
  19687. ' if g=i then ;',
  19688. ' if i=g then ;',
  19689. ' if g=IUnknown then ;',
  19690. ' if IUnknown=g then ;',
  19691. ' if s=i then ;',
  19692. ' if i=s then ;',
  19693. ' if s=IUnknown then ;',
  19694. ' if IUnknown=s then ;',
  19695. ' if s=g then ;',
  19696. ' if g=s then ;',
  19697. '']);
  19698. ConvertProgram;
  19699. CheckSource('TestClassInterface_GUID',
  19700. LinesToStr([ // statements
  19701. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19702. 'rtl.createClass($mod, "TObject", null, function () {',
  19703. ' this.$init = function () {',
  19704. ' };',
  19705. ' this.$final = function () {',
  19706. ' };',
  19707. '});',
  19708. 'rtl.recNewT($mod, "TGUID", function () {',
  19709. ' this.D1 = 0;',
  19710. ' this.D2 = 0;',
  19711. ' this.D3 = 0;',
  19712. ' this.D4 = 0;',
  19713. ' this.$eq = function (b) {',
  19714. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19715. ' };',
  19716. ' this.$assign = function (s) {',
  19717. ' this.D1 = s.D1;',
  19718. ' this.D2 = s.D2;',
  19719. ' this.D3 = s.D3;',
  19720. ' this.D4 = s.D4;',
  19721. ' return this;',
  19722. ' };',
  19723. '});',
  19724. 'this.DoConstGUIDIt = function (g) {',
  19725. '};',
  19726. 'this.DoDefGUID = function (g) {',
  19727. '};',
  19728. 'this.DoStr = function (s) {',
  19729. '};',
  19730. 'this.i = null;',
  19731. 'this.g = $mod.TGUID.$clone({',
  19732. ' D1: 0xD91C9AF4,',
  19733. ' D2: 0x3C93,',
  19734. ' D3: 0x420F,',
  19735. ' D4: [',
  19736. ' 0xA3,',
  19737. ' 0x03,',
  19738. ' 0xBF,',
  19739. ' 0x5B,',
  19740. ' 0xA8,',
  19741. ' 0x2B,',
  19742. ' 0xFD,',
  19743. ' 0x23',
  19744. ' ]',
  19745. '});',
  19746. 'this.s = "";',
  19747. '']),
  19748. LinesToStr([ // $mod.$main
  19749. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19750. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19751. '$mod.DoStr($mod.IUnknown.$guid);',
  19752. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19753. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19754. '$mod.DoStr($mod.i.$guid);',
  19755. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19756. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19757. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19758. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19759. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19760. '$mod.g.$assign({',
  19761. ' D1: 0xD91C9AF4,',
  19762. ' D2: 0x3C93,',
  19763. ' D3: 0x420F,',
  19764. ' D4: [',
  19765. ' 0xA3,',
  19766. ' 0x03,',
  19767. ' 0xBF,',
  19768. ' 0x5B,',
  19769. ' 0xA8,',
  19770. ' 0x2B,',
  19771. ' 0xFD,',
  19772. ' 0x23',
  19773. ' ]',
  19774. '});',
  19775. '$mod.s = $mod.i.$guid;',
  19776. '$mod.s = $mod.IUnknown.$guid;',
  19777. '$mod.s = rtl.guidrToStr($mod.g);',
  19778. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19779. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19780. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19781. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19782. 'if ($mod.s === $mod.i.$guid) ;',
  19783. 'if ($mod.i.$guid === $mod.s) ;',
  19784. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19785. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19786. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19787. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19788. '']));
  19789. end;
  19790. procedure TTestModule.TestClassInterface_GUIDProperty;
  19791. begin
  19792. StartProgram(false);
  19793. Add([
  19794. '{$interfaces corba}',
  19795. 'type',
  19796. ' IUnknown = interface',
  19797. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19798. ' end;',
  19799. ' TGUID = record D1, D2, D3, D4: word; end;',
  19800. ' TAliasGUID = TGUID;',
  19801. ' TGUIDString = type string;',
  19802. ' TAliasGUIDString = TGUIDString;',
  19803. ' TObject = class',
  19804. ' function GetG: TAliasGUID; virtual; abstract;',
  19805. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19806. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19807. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19808. ' property g: TAliasGUID read GetG write SetG;',
  19809. ' property s: TAliasGUIDString read GetS write SetS;',
  19810. ' end;',
  19811. 'var o: TObject;',
  19812. 'begin',
  19813. ' o.g:=IUnknown;',
  19814. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19815. ' o.s:=IUnknown;',
  19816. ' o.s:=o.g;',
  19817. '']);
  19818. ConvertProgram;
  19819. CheckSource('TestClassInterface_GUIDProperty',
  19820. LinesToStr([ // statements
  19821. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19822. 'rtl.recNewT($mod, "TGUID", function () {',
  19823. ' this.D1 = 0;',
  19824. ' this.D2 = 0;',
  19825. ' this.D3 = 0;',
  19826. ' this.D4 = 0;',
  19827. ' this.$eq = function (b) {',
  19828. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19829. ' };',
  19830. ' this.$assign = function (s) {',
  19831. ' this.D1 = s.D1;',
  19832. ' this.D2 = s.D2;',
  19833. ' this.D3 = s.D3;',
  19834. ' this.D4 = s.D4;',
  19835. ' return this;',
  19836. ' };',
  19837. '});',
  19838. 'rtl.createClass($mod, "TObject", null, function () {',
  19839. ' this.$init = function () {',
  19840. ' };',
  19841. ' this.$final = function () {',
  19842. ' };',
  19843. '});',
  19844. 'this.o = null;',
  19845. '']),
  19846. LinesToStr([ // $mod.$main
  19847. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19848. '$mod.o.SetG({',
  19849. ' D1: 0xD91C9AF4,',
  19850. ' D2: 0x3C93,',
  19851. ' D3: 0x420F,',
  19852. ' D4: [',
  19853. ' 0xA3,',
  19854. ' 0x03,',
  19855. ' 0xBF,',
  19856. ' 0x5B,',
  19857. ' 0xA8,',
  19858. ' 0x2B,',
  19859. ' 0xFD,',
  19860. ' 0x23',
  19861. ' ]',
  19862. '});',
  19863. '$mod.o.SetS($mod.IUnknown.$guid);',
  19864. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19865. '']));
  19866. end;
  19867. procedure TTestModule.TestClassHelper_ClassVar;
  19868. begin
  19869. StartProgram(false);
  19870. Add([
  19871. 'type',
  19872. ' TObject = class',
  19873. ' end;',
  19874. ' THelper = class helper for TObject',
  19875. ' const',
  19876. ' One = 1;',
  19877. ' Two: word = 2;',
  19878. ' class var',
  19879. ' Glob: word;',
  19880. ' function Foo(w: word): word;',
  19881. ' class function Bar(w: word): word;',
  19882. ' end;',
  19883. 'function THelper.foo(w: word): word;',
  19884. 'begin',
  19885. ' Result:=w;',
  19886. ' Two:=One+w;',
  19887. ' Glob:=Glob;',
  19888. ' Result:=Self.Glob;',
  19889. ' Self.Glob:=Self.Glob;',
  19890. ' with Self do Glob:=Glob;',
  19891. 'end;',
  19892. 'class function THelper.bar(w: word): word;',
  19893. 'begin',
  19894. ' Result:=w;',
  19895. ' Two:=One;',
  19896. ' Glob:=Glob;',
  19897. ' Self.Glob:=Self.Glob;',
  19898. ' with Self do Glob:=Glob;',
  19899. 'end;',
  19900. 'var o: TObject;',
  19901. 'begin',
  19902. ' tobject.two:=tobject.one;',
  19903. ' tobject.Glob:=tobject.Glob;',
  19904. ' with tobject do begin',
  19905. ' two:=one;',
  19906. ' Glob:=Glob;',
  19907. ' end;',
  19908. ' o.two:=o.one;',
  19909. ' o.Glob:=o.Glob;',
  19910. ' with o do begin',
  19911. ' two:=one;',
  19912. ' Glob:=Glob;',
  19913. ' end;',
  19914. '']);
  19915. ConvertProgram;
  19916. CheckSource('TestClassHelper_ClassVar',
  19917. LinesToStr([ // statements
  19918. 'rtl.createClass($mod, "TObject", null, function () {',
  19919. ' this.$init = function () {',
  19920. ' };',
  19921. ' this.$final = function () {',
  19922. ' };',
  19923. '});',
  19924. 'rtl.createHelper($mod, "THelper", null, function () {',
  19925. ' this.One = 1;',
  19926. ' this.Two = 2;',
  19927. ' this.Glob = 0;',
  19928. ' this.Foo = function (w) {',
  19929. ' var Result = 0;',
  19930. ' Result = w;',
  19931. ' $mod.THelper.Two = 1 + w;',
  19932. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19933. ' Result = $mod.THelper.Glob;',
  19934. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19935. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19936. ' return Result;',
  19937. ' };',
  19938. ' this.Bar = function (w) {',
  19939. ' var Result = 0;',
  19940. ' Result = w;',
  19941. ' $mod.THelper.Two = 1;',
  19942. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19943. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19944. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19945. ' return Result;',
  19946. ' };',
  19947. '});',
  19948. 'this.o = null;',
  19949. '']),
  19950. LinesToStr([ // $mod.$main
  19951. '$mod.THelper.Two = 1;',
  19952. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19953. 'var $with1 = $mod.TObject;',
  19954. '$mod.THelper.Two = 1;',
  19955. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19956. '$mod.THelper.Two = 1;',
  19957. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19958. 'var $with2 = $mod.o;',
  19959. '$mod.THelper.Two = 1;',
  19960. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19961. '']));
  19962. end;
  19963. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  19964. begin
  19965. StartProgram(false);
  19966. Add([
  19967. 'type',
  19968. ' TObject = class',
  19969. ' FSize: word;',
  19970. ' property Size: word read FSize write FSize;',
  19971. ' end;',
  19972. ' THelper = class helper for TObject',
  19973. ' function Foo(w: word = 1): word;',
  19974. ' end;',
  19975. 'function THelper.foo(w: word): word;',
  19976. 'begin',
  19977. ' Result:=Size;',
  19978. ' Size:=Size+2;',
  19979. ' Self.Size:=Self.Size+3;',
  19980. ' FSize:=FSize+4;',
  19981. ' Self.FSize:=Self.FSize+5;',
  19982. ' with Self do begin',
  19983. ' Size:=Size+6;',
  19984. ' FSize:=FSize+7;',
  19985. ' FSize:=FSize+8;',
  19986. ' end;',
  19987. 'end;',
  19988. 'begin',
  19989. '']);
  19990. ConvertProgram;
  19991. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  19992. LinesToStr([ // statements
  19993. 'rtl.createClass($mod, "TObject", null, function () {',
  19994. ' this.$init = function () {',
  19995. ' this.FSize = 0;',
  19996. ' };',
  19997. ' this.$final = function () {',
  19998. ' };',
  19999. '});',
  20000. 'rtl.createHelper($mod, "THelper", null, function () {',
  20001. ' this.Foo = function (w) {',
  20002. ' var Result = 0;',
  20003. ' Result = this.FSize;',
  20004. ' this.FSize = this.FSize + 2;',
  20005. ' this.FSize = this.FSize + 3;',
  20006. ' this.FSize = this.FSize + 4;',
  20007. ' this.FSize = this.FSize + 5;',
  20008. ' this.FSize = this.FSize + 6;',
  20009. ' this.FSize = this.FSize + 7;',
  20010. ' this.FSize = this.FSize + 8;',
  20011. ' return Result;',
  20012. ' };',
  20013. '});',
  20014. '']),
  20015. LinesToStr([ // $mod.$main
  20016. '']));
  20017. end;
  20018. procedure TTestModule.TestClassHelper_Method_Call;
  20019. begin
  20020. StartProgram(false);
  20021. Add([
  20022. 'type',
  20023. ' TObject = class',
  20024. ' procedure Run(w: word = 10);',
  20025. ' end;',
  20026. ' THelper = class helper for TObject',
  20027. ' function Foo(w: word = 1): word;',
  20028. ' end;',
  20029. 'procedure TObject.Run(w: word);',
  20030. 'var o: TObject;',
  20031. 'begin',
  20032. ' Foo;',
  20033. ' Foo();',
  20034. ' Foo(2);',
  20035. ' Self.Foo;',
  20036. ' Self.Foo();',
  20037. ' Self.Foo(3);',
  20038. ' with Self do begin',
  20039. ' Foo;',
  20040. ' Foo();',
  20041. ' Foo(4);',
  20042. ' end;',
  20043. ' with o do Foo(5);',
  20044. 'end;',
  20045. 'function THelper.foo(w: word): word;',
  20046. 'begin',
  20047. ' Run;',
  20048. ' Run();',
  20049. ' Run(11);',
  20050. ' Foo;',
  20051. ' Foo();',
  20052. ' Foo(12);',
  20053. ' Self.Foo;',
  20054. ' Self.Foo();',
  20055. ' Self.Foo(13);',
  20056. ' with Self do begin',
  20057. ' Foo;',
  20058. ' Foo();',
  20059. ' Foo(14);',
  20060. ' end;',
  20061. 'end;',
  20062. 'var Obj: TObject;',
  20063. 'begin',
  20064. ' obj.Foo;',
  20065. ' obj.Foo();',
  20066. ' obj.Foo(21);',
  20067. ' with obj do begin',
  20068. ' Foo;',
  20069. ' Foo();',
  20070. ' Foo(22);',
  20071. ' end;',
  20072. '']);
  20073. ConvertProgram;
  20074. CheckSource('TestClassHelper_Method_Call',
  20075. LinesToStr([ // statements
  20076. 'rtl.createClass($mod, "TObject", null, function () {',
  20077. ' this.$init = function () {',
  20078. ' };',
  20079. ' this.$final = function () {',
  20080. ' };',
  20081. ' this.Run = function (w) {',
  20082. ' var o = null;',
  20083. ' $mod.THelper.Foo.call(this, 1);',
  20084. ' $mod.THelper.Foo.call(this, 1);',
  20085. ' $mod.THelper.Foo.call(this, 2);',
  20086. ' $mod.THelper.Foo.call(this, 1);',
  20087. ' $mod.THelper.Foo.call(this, 1);',
  20088. ' $mod.THelper.Foo.call(this, 3);',
  20089. ' $mod.THelper.Foo.call(this, 1);',
  20090. ' $mod.THelper.Foo.call(this, 1);',
  20091. ' $mod.THelper.Foo.call(this, 4);',
  20092. ' $mod.THelper.Foo.call(o, 5);',
  20093. ' };',
  20094. '});',
  20095. 'rtl.createHelper($mod, "THelper", null, function () {',
  20096. ' this.Foo = function (w) {',
  20097. ' var Result = 0;',
  20098. ' this.Run(10);',
  20099. ' this.Run(10);',
  20100. ' this.Run(11);',
  20101. ' $mod.THelper.Foo.call(this, 1);',
  20102. ' $mod.THelper.Foo.call(this, 1);',
  20103. ' $mod.THelper.Foo.call(this, 12);',
  20104. ' $mod.THelper.Foo.call(this, 1);',
  20105. ' $mod.THelper.Foo.call(this, 1);',
  20106. ' $mod.THelper.Foo.call(this, 13);',
  20107. ' $mod.THelper.Foo.call(this, 1);',
  20108. ' $mod.THelper.Foo.call(this, 1);',
  20109. ' $mod.THelper.Foo.call(this, 14);',
  20110. ' return Result;',
  20111. ' };',
  20112. '});',
  20113. 'this.Obj = null;',
  20114. '']),
  20115. LinesToStr([ // $mod.$main
  20116. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20117. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20118. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20119. 'var $with1 = $mod.Obj;',
  20120. '$mod.THelper.Foo.call($with1, 1);',
  20121. '$mod.THelper.Foo.call($with1, 1);',
  20122. '$mod.THelper.Foo.call($with1, 22);',
  20123. '']));
  20124. end;
  20125. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20126. begin
  20127. StartProgram(false);
  20128. Add([
  20129. 'type',
  20130. ' TObject = class',
  20131. ' procedure Run(w: word = 10);',
  20132. ' end;',
  20133. ' THelper = class helper for TObject',
  20134. ' function Foo(w: word = 1): word;',
  20135. ' end;',
  20136. 'procedure TObject.Run(w: word);',
  20137. ' procedure Sub(Self: TObject);',
  20138. ' begin',
  20139. ' Foo;',
  20140. ' Foo();',
  20141. ' Self.Foo;',
  20142. ' Self.Foo();',
  20143. ' with Self do begin',
  20144. ' Foo;',
  20145. ' Foo();',
  20146. ' end;',
  20147. ' end;',
  20148. 'begin',
  20149. 'end;',
  20150. 'function THelper.foo(w: word): word;',
  20151. ' procedure Sub(Self: TObject);',
  20152. ' begin',
  20153. ' Run;',
  20154. ' Run();',
  20155. ' Foo;',
  20156. ' Foo();',
  20157. ' Self.Foo;',
  20158. ' Self.Foo();',
  20159. ' with Self do begin',
  20160. ' Foo;',
  20161. ' Foo();',
  20162. ' end;',
  20163. ' end;',
  20164. 'begin',
  20165. 'end;',
  20166. 'begin',
  20167. '']);
  20168. ConvertProgram;
  20169. CheckSource('TestClassHelper_Method_Nested_Call',
  20170. LinesToStr([ // statements
  20171. 'rtl.createClass($mod, "TObject", null, function () {',
  20172. ' this.$init = function () {',
  20173. ' };',
  20174. ' this.$final = function () {',
  20175. ' };',
  20176. ' this.Run = function (w) {',
  20177. ' var $Self = this;',
  20178. ' function Sub(Self) {',
  20179. ' $mod.THelper.Foo.call($Self, 1);',
  20180. ' $mod.THelper.Foo.call($Self, 1);',
  20181. ' $mod.THelper.Foo.call(Self, 1);',
  20182. ' $mod.THelper.Foo.call(Self, 1);',
  20183. ' $mod.THelper.Foo.call(Self, 1);',
  20184. ' $mod.THelper.Foo.call(Self, 1);',
  20185. ' };',
  20186. ' };',
  20187. '});',
  20188. 'rtl.createHelper($mod, "THelper", null, function () {',
  20189. ' this.Foo = function (w) {',
  20190. ' var $Self = this;',
  20191. ' var Result = 0;',
  20192. ' function Sub(Self) {',
  20193. ' $Self.Run(10);',
  20194. ' $Self.Run(10);',
  20195. ' $mod.THelper.Foo.call($Self, 1);',
  20196. ' $mod.THelper.Foo.call($Self, 1);',
  20197. ' $mod.THelper.Foo.call(Self, 1);',
  20198. ' $mod.THelper.Foo.call(Self, 1);',
  20199. ' $mod.THelper.Foo.call(Self, 1);',
  20200. ' $mod.THelper.Foo.call(Self, 1);',
  20201. ' };',
  20202. ' return Result;',
  20203. ' };',
  20204. '});',
  20205. '']),
  20206. LinesToStr([ // $mod.$main
  20207. '']));
  20208. end;
  20209. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20210. begin
  20211. StartProgram(false);
  20212. Add([
  20213. 'type',
  20214. ' TObject = class',
  20215. ' class procedure Run(w: word = 10);',
  20216. ' end;',
  20217. ' THelper = class helper for TObject',
  20218. ' class function Foo(w: word = 1): word;',
  20219. ' end;',
  20220. 'class procedure TObject.Run(w: word);',
  20221. 'begin',
  20222. ' Foo;',
  20223. ' Foo();',
  20224. ' Self.Foo;',
  20225. ' Self.Foo();',
  20226. ' with Self do begin',
  20227. ' Foo;',
  20228. ' Foo();',
  20229. ' end;',
  20230. 'end;',
  20231. 'class function THelper.foo(w: word): word;',
  20232. 'begin',
  20233. ' Run;',
  20234. ' Run();',
  20235. ' Foo;',
  20236. ' Foo();',
  20237. ' Self.Foo;',
  20238. ' Self.Foo();',
  20239. ' with Self do begin',
  20240. ' Foo;',
  20241. ' Foo();',
  20242. ' end;',
  20243. 'end;',
  20244. 'var',
  20245. ' Obj: TObject;',
  20246. 'begin',
  20247. ' obj.Foo;',
  20248. ' obj.Foo();',
  20249. ' with obj do begin',
  20250. ' Foo;',
  20251. ' Foo();',
  20252. ' end;',
  20253. ' tobject.Foo;',
  20254. ' tobject.Foo();',
  20255. ' with tobject do begin',
  20256. ' Foo;',
  20257. ' Foo();',
  20258. ' end;',
  20259. '']);
  20260. ConvertProgram;
  20261. CheckSource('TestClassHelper_ClassMethod_Call',
  20262. LinesToStr([ // statements
  20263. 'rtl.createClass($mod, "TObject", null, function () {',
  20264. ' this.$init = function () {',
  20265. ' };',
  20266. ' this.$final = function () {',
  20267. ' };',
  20268. ' this.Run = function (w) {',
  20269. ' $mod.THelper.Foo.call(this, 1);',
  20270. ' $mod.THelper.Foo.call(this, 1);',
  20271. ' $mod.THelper.Foo.call(this, 1);',
  20272. ' $mod.THelper.Foo.call(this, 1);',
  20273. ' $mod.THelper.Foo.call(this, 1);',
  20274. ' $mod.THelper.Foo.call(this, 1);',
  20275. ' };',
  20276. '});',
  20277. 'rtl.createHelper($mod, "THelper", null, function () {',
  20278. ' this.Foo = function (w) {',
  20279. ' var Result = 0;',
  20280. ' this.Run(10);',
  20281. ' this.Run(10);',
  20282. ' $mod.THelper.Foo.call(this, 1);',
  20283. ' $mod.THelper.Foo.call(this, 1);',
  20284. ' $mod.THelper.Foo.call(this, 1);',
  20285. ' $mod.THelper.Foo.call(this, 1);',
  20286. ' $mod.THelper.Foo.call(this, 1);',
  20287. ' $mod.THelper.Foo.call(this, 1);',
  20288. ' return Result;',
  20289. ' };',
  20290. '});',
  20291. 'this.Obj = null;',
  20292. '']),
  20293. LinesToStr([ // $mod.$main
  20294. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20295. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20296. 'var $with1 = $mod.Obj;',
  20297. '$mod.THelper.Foo.call($with1.$class, 1);',
  20298. '$mod.THelper.Foo.call($with1.$class, 1);',
  20299. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20300. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20301. 'var $with2 = $mod.TObject;',
  20302. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20303. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20304. '']));
  20305. end;
  20306. procedure TTestModule.TestClassHelper_ClassOf;
  20307. begin
  20308. StartProgram(false);
  20309. Add([
  20310. 'type',
  20311. ' TObject = class',
  20312. ' end;',
  20313. ' TClass = class of TObject;',
  20314. ' THelper = class helper for TObject',
  20315. ' class function Foo(w: word = 1): word;',
  20316. ' end;',
  20317. 'class function THelper.foo(w: word): word;',
  20318. 'begin',
  20319. 'end;',
  20320. 'var',
  20321. ' c: TClass;',
  20322. 'begin',
  20323. ' c.Foo;',
  20324. ' c.Foo();',
  20325. ' with c do begin',
  20326. ' Foo;',
  20327. ' Foo();',
  20328. ' end;',
  20329. '']);
  20330. ConvertProgram;
  20331. CheckSource('TestClassHelper_ClassOf',
  20332. LinesToStr([ // statements
  20333. 'rtl.createClass($mod, "TObject", null, function () {',
  20334. ' this.$init = function () {',
  20335. ' };',
  20336. ' this.$final = function () {',
  20337. ' };',
  20338. '});',
  20339. 'rtl.createHelper($mod, "THelper", null, function () {',
  20340. ' this.Foo = function (w) {',
  20341. ' var Result = 0;',
  20342. ' return Result;',
  20343. ' };',
  20344. '});',
  20345. 'this.c = null;',
  20346. '']),
  20347. LinesToStr([ // $mod.$main
  20348. '$mod.THelper.Foo.call($mod.c, 1);',
  20349. '$mod.THelper.Foo.call($mod.c, 1);',
  20350. 'var $with1 = $mod.c;',
  20351. '$mod.THelper.Foo.call($with1, 1);',
  20352. '$mod.THelper.Foo.call($with1, 1);',
  20353. '']));
  20354. end;
  20355. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20356. begin
  20357. StartProgram(false);
  20358. Add([
  20359. '{$mode objfpc}',
  20360. 'type',
  20361. ' TObject = class',
  20362. ' procedure DoIt;',
  20363. ' end;',
  20364. ' THelper = class helper for TObject',
  20365. ' procedure Fly(w: word = 1);',
  20366. ' class procedure Glide(w: word = 1);',
  20367. ' class procedure Run(w: word = 1); static;',
  20368. ' end;',
  20369. ' TFly = procedure(w: word) of object;',
  20370. ' TGlide = TFly;',
  20371. ' TRun = procedure(w: word);',
  20372. 'var',
  20373. ' f: TFly;',
  20374. ' g: TGlide;',
  20375. ' r: TRun;',
  20376. 'procedure TObject.DoIt;',
  20377. 'begin',
  20378. ' f:=@fly;',
  20379. ' g:=@glide;',
  20380. ' r:=@run;',
  20381. ' f:[email protected];',
  20382. ' g:[email protected];',
  20383. ' r:[email protected];',
  20384. ' with self do begin',
  20385. ' f:=@fly;',
  20386. ' g:=@glide;',
  20387. ' r:=@run;',
  20388. ' end;',
  20389. 'end;',
  20390. 'procedure THelper.fly(w: word);',
  20391. 'begin',
  20392. ' f:=@fly;',
  20393. ' g:=@glide;',
  20394. ' r:=@run;',
  20395. 'end;',
  20396. 'class procedure THelper.glide(w: word);',
  20397. 'begin',
  20398. ' g:=@glide;',
  20399. ' r:=@run;',
  20400. 'end;',
  20401. 'class procedure THelper.run(w: word);',
  20402. 'begin',
  20403. ' g:=@glide;',
  20404. ' r:=@run;',
  20405. 'end;',
  20406. 'var',
  20407. ' Obj: TObject;',
  20408. 'begin',
  20409. ' f:[email protected];',
  20410. ' g:[email protected];',
  20411. ' r:[email protected];',
  20412. ' with obj do begin',
  20413. ' f:=@fly;',
  20414. ' g:=@glide;',
  20415. ' r:=@run;',
  20416. ' end;',
  20417. ' g:[email protected];',
  20418. ' r:[email protected];',
  20419. ' with tobject do begin',
  20420. ' g:=@glide;',
  20421. ' r:=@run;',
  20422. ' end;',
  20423. '']);
  20424. ConvertProgram;
  20425. CheckSource('TestClassHelper_MethodRefObjFPC',
  20426. LinesToStr([ // statements
  20427. 'rtl.createClass($mod, "TObject", null, function () {',
  20428. ' this.$init = function () {',
  20429. ' };',
  20430. ' this.$final = function () {',
  20431. ' };',
  20432. ' this.DoIt = function () {',
  20433. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20434. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20435. ' $mod.r = $mod.THelper.Run;',
  20436. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20437. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20438. ' $mod.r = $mod.THelper.Run;',
  20439. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20440. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20441. ' $mod.r = $mod.THelper.Run;',
  20442. ' };',
  20443. '});',
  20444. 'rtl.createHelper($mod, "THelper", null, function () {',
  20445. ' this.Fly = function (w) {',
  20446. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20447. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20448. ' $mod.r = $mod.THelper.Run;',
  20449. ' };',
  20450. ' this.Glide = function (w) {',
  20451. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20452. ' $mod.r = $mod.THelper.Run;',
  20453. ' };',
  20454. ' this.Run = function (w) {',
  20455. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20456. ' $mod.r = $mod.THelper.Run;',
  20457. ' };',
  20458. '});',
  20459. 'this.f = null;',
  20460. 'this.g = null;',
  20461. 'this.r = null;',
  20462. 'this.Obj = null;',
  20463. '']),
  20464. LinesToStr([ // $mod.$main
  20465. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20466. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20467. '$mod.r = $mod.THelper.Run;',
  20468. 'var $with1 = $mod.Obj;',
  20469. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  20470. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  20471. '$mod.r = $mod.THelper.Run;',
  20472. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20473. '$mod.r = $mod.THelper.Run;',
  20474. 'var $with2 = $mod.TObject;',
  20475. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  20476. '$mod.r = $mod.THelper.Run;',
  20477. '']));
  20478. end;
  20479. procedure TTestModule.TestClassHelper_Constructor;
  20480. begin
  20481. StartProgram(false);
  20482. Add([
  20483. 'type',
  20484. ' TObject = class',
  20485. ' constructor Create;',
  20486. ' end;',
  20487. ' TClass = class of TObject;',
  20488. ' THelper = class helper for TObject',
  20489. ' constructor NewHlp(w: word);',
  20490. ' end;',
  20491. 'var',
  20492. ' obj: TObject;',
  20493. ' c: TClass;',
  20494. 'constructor TObject.Create;',
  20495. 'begin',
  20496. ' NewHlp(2);', // normal call
  20497. ' tobject.NewHlp(3);', // new instance
  20498. ' c.newhlp(4);', // new instance
  20499. 'end;',
  20500. 'constructor THelper.NewHlp(w: word);',
  20501. 'begin',
  20502. ' create;', // normal call
  20503. ' tobject.create;', // new instance
  20504. ' NewHlp(2);', // normal call
  20505. ' tobject.NewHlp(3);', // new instance
  20506. ' c.newhlp(4);', // new instance
  20507. 'end;',
  20508. 'begin',
  20509. ' obj.newhlp(2);', // normal call
  20510. ' with Obj do newhlp(12);', // normal call
  20511. ' tobject.newhlp(3);', // new instance
  20512. ' with tobject do newhlp(13);', // new instance
  20513. ' c.newhlp(4);', // new instance
  20514. ' with c do newhlp(14);', // new instance
  20515. '']);
  20516. ConvertProgram;
  20517. CheckSource('TestClassHelper_Constructor',
  20518. LinesToStr([ // statements
  20519. 'rtl.createClass($mod, "TObject", null, function () {',
  20520. ' this.$init = function () {',
  20521. ' };',
  20522. ' this.$final = function () {',
  20523. ' };',
  20524. ' this.Create = function () {',
  20525. ' $mod.THelper.NewHlp.call(this, 2);',
  20526. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20527. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20528. ' return this;',
  20529. ' };',
  20530. '});',
  20531. 'rtl.createHelper($mod, "THelper", null, function () {',
  20532. ' this.NewHlp = function (w) {',
  20533. ' this.Create();',
  20534. ' $mod.TObject.$create("Create");',
  20535. ' $mod.THelper.NewHlp.call(this, 2);',
  20536. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20537. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20538. ' return this;',
  20539. ' };',
  20540. '});',
  20541. 'this.obj = null;',
  20542. 'this.c = null;',
  20543. '']),
  20544. LinesToStr([ // $mod.$main
  20545. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20546. 'var $with1 = $mod.obj;',
  20547. '$mod.THelper.NewHlp.call($with1, 12);',
  20548. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20549. 'var $with2 = $mod.TObject;',
  20550. '$with2.$create($mod.THelper.NewHlp, [13]);',
  20551. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20552. 'var $with3 = $mod.c;',
  20553. '$with3.$create($mod.THelper.NewHlp, [14]);',
  20554. '']));
  20555. end;
  20556. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20557. begin
  20558. StartProgram(false);
  20559. Add([
  20560. 'type',
  20561. ' TObject = class',
  20562. ' procedure Fly;',
  20563. ' end;',
  20564. ' TObjHelper = class helper for TObject',
  20565. ' procedure Fly;',
  20566. ' end;',
  20567. ' TBird = class',
  20568. ' procedure Fly;',
  20569. ' end;',
  20570. ' TBirdHelper = class helper for TBird',
  20571. ' procedure Fly;',
  20572. ' procedure Walk(w: word);',
  20573. ' end;',
  20574. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20575. ' procedure Fly;',
  20576. ' procedure Walk(w: word);',
  20577. ' end;',
  20578. 'procedure Tobject.fly;',
  20579. 'begin',
  20580. ' inherited;', // ignore
  20581. 'end;',
  20582. 'procedure Tobjhelper.fly;',
  20583. 'begin',
  20584. ' {@TObject_Fly}inherited;',
  20585. ' inherited {@TObject_Fly}Fly;',
  20586. 'end;',
  20587. 'procedure Tbird.fly;',
  20588. 'begin',
  20589. ' {@TObjHelper_Fly}inherited;',
  20590. ' inherited {@TObjHelper_Fly}Fly;',
  20591. 'end;',
  20592. 'procedure Tbirdhelper.fly;',
  20593. 'begin',
  20594. ' {@TBird_Fly}inherited;',
  20595. ' inherited {@TBird_Fly}Fly;',
  20596. 'end;',
  20597. 'procedure Tbirdhelper.walk(w: word);',
  20598. 'begin',
  20599. 'end;',
  20600. 'procedure teagleHelper.fly;',
  20601. 'begin',
  20602. ' {@TBird_Fly}inherited;',
  20603. ' inherited {@TBird_Fly}Fly;',
  20604. 'end;',
  20605. 'procedure teagleHelper.walk(w: word);',
  20606. 'begin',
  20607. ' {@TBirdHelper_Walk}inherited;',
  20608. ' inherited {@TBirdHelper_Walk}Walk(3);',
  20609. 'end;',
  20610. 'begin',
  20611. '']);
  20612. ConvertProgram;
  20613. CheckSource('TestClassHelper_InheritedObjFPC',
  20614. LinesToStr([ // statements
  20615. 'rtl.createClass($mod, "TObject", null, function () {',
  20616. ' this.$init = function () {',
  20617. ' };',
  20618. ' this.$final = function () {',
  20619. ' };',
  20620. ' this.Fly = function () {',
  20621. ' };',
  20622. '});',
  20623. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20624. ' this.Fly = function () {',
  20625. ' $mod.TObject.Fly.call(this);',
  20626. ' $mod.TObject.Fly.call(this);',
  20627. ' };',
  20628. '});',
  20629. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20630. ' this.Fly$1 = function () {',
  20631. ' $mod.TObjHelper.Fly.call(this);',
  20632. ' $mod.TObjHelper.Fly.call(this);',
  20633. ' };',
  20634. '});',
  20635. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20636. ' this.Fly = function () {',
  20637. ' $mod.TBird.Fly$1.call(this);',
  20638. ' $mod.TBird.Fly$1.call(this);',
  20639. ' };',
  20640. ' this.Walk = function (w) {',
  20641. ' };',
  20642. '});',
  20643. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  20644. ' this.Fly$1 = function () {',
  20645. ' $mod.TBird.Fly$1.call(this);',
  20646. ' $mod.TBird.Fly$1.call(this);',
  20647. ' };',
  20648. ' this.Walk$1 = function (w) {',
  20649. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20650. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20651. ' };',
  20652. '});',
  20653. '']),
  20654. LinesToStr([ // $mod.$main
  20655. '']));
  20656. end;
  20657. procedure TTestModule.TestClassHelper_Property;
  20658. begin
  20659. StartProgram(false);
  20660. Add([
  20661. 'type',
  20662. ' TObject = class',
  20663. ' FSize: word;',
  20664. ' function GetSpeed: word;',
  20665. ' procedure SetSpeed(Value: word);',
  20666. ' end;',
  20667. ' TObjHelper = class helper for TObject',
  20668. ' function GetLeft: word;',
  20669. ' procedure SetLeft(Value: word);',
  20670. ' property Size: word read FSize write FSize;',
  20671. ' property Speed: word read GetSpeed write SetSpeed;',
  20672. ' property Left: word read GetLeft write SetLeft;',
  20673. ' end;',
  20674. ' TBird = class',
  20675. ' property NotRight: word read GetLeft write SetLeft;',
  20676. ' procedure DoIt;',
  20677. ' end;',
  20678. 'var',
  20679. ' b: TBird;',
  20680. 'function Tobject.GetSpeed: word;',
  20681. 'begin',
  20682. ' Size:=Size+11;',
  20683. ' Speed:=Speed+12;',
  20684. ' Result:=Left+13;',
  20685. ' Left:=13;',
  20686. ' Left:=Left+13;',
  20687. ' Self.Size:=Self.Size+21;',
  20688. ' Self.Speed:=Self.Speed+22;',
  20689. ' Self.Left:=Self.Left+23;',
  20690. ' with Self do begin',
  20691. ' Size:=Size+31;',
  20692. ' Speed:=Speed+32;',
  20693. ' Left:=Left+33;',
  20694. ' end;',
  20695. 'end;',
  20696. 'procedure Tobject.SetSpeed(Value: word);',
  20697. 'begin',
  20698. 'end;',
  20699. 'function TObjHelper.GetLeft: word;',
  20700. 'begin',
  20701. ' Size:=Size+11;',
  20702. ' Speed:=Speed+12;',
  20703. ' Left:=Left+13;',
  20704. ' Self.Size:=Self.Size+21;',
  20705. ' Self.Speed:=Self.Speed+22;',
  20706. ' Self.Left:=Self.Left+23;',
  20707. ' with Self do begin',
  20708. ' Size:=Size+31;',
  20709. ' Speed:=Speed+32;',
  20710. ' Left:=Left+33;',
  20711. ' end;',
  20712. 'end;',
  20713. 'procedure TObjHelper.SetLeft(Value: word);',
  20714. 'begin',
  20715. 'end;',
  20716. 'procedure TBird.DoIt;',
  20717. 'begin',
  20718. ' NotRight:=NotRight+11;',
  20719. ' Self.NotRight:=Self.NotRight+21;',
  20720. ' with Self do begin',
  20721. ' NotRight:=NotRight+31;',
  20722. ' end;',
  20723. 'end;',
  20724. 'begin',
  20725. ' b.Size:=b.Size+11;',
  20726. ' b.Speed:=b.Speed+12;',
  20727. ' b.Left:=b.Left+13;',
  20728. ' b.NotRight:=b.NotRight+14;',
  20729. ' with b do begin',
  20730. ' Size:=Size+31;',
  20731. ' Speed:=Speed+32;',
  20732. ' Left:=Left+33;',
  20733. ' NotRight:=NotRight+34;',
  20734. ' end;',
  20735. '']);
  20736. ConvertProgram;
  20737. CheckSource('TestClassHelper_Property',
  20738. LinesToStr([ // statements
  20739. 'rtl.createClass($mod, "TObject", null, function () {',
  20740. ' this.$init = function () {',
  20741. ' this.FSize = 0;',
  20742. ' };',
  20743. ' this.$final = function () {',
  20744. ' };',
  20745. ' this.GetSpeed = function () {',
  20746. ' var Result = 0;',
  20747. ' this.FSize = this.FSize + 11;',
  20748. ' this.SetSpeed(this.GetSpeed() + 12);',
  20749. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20750. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20751. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20752. ' this.FSize = this.FSize + 21;',
  20753. ' this.SetSpeed(this.GetSpeed() + 22);',
  20754. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20755. ' this.FSize = this.FSize + 31;',
  20756. ' this.SetSpeed(this.GetSpeed() + 32);',
  20757. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20758. ' return Result;',
  20759. ' };',
  20760. ' this.SetSpeed = function (Value) {',
  20761. ' };',
  20762. '});',
  20763. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20764. ' this.GetLeft = function () {',
  20765. ' var Result = 0;',
  20766. ' this.FSize = this.FSize + 11;',
  20767. ' this.SetSpeed(this.GetSpeed() + 12);',
  20768. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20769. ' this.FSize = this.FSize + 21;',
  20770. ' this.SetSpeed(this.GetSpeed() + 22);',
  20771. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20772. ' this.FSize = this.FSize + 31;',
  20773. ' this.SetSpeed(this.GetSpeed() + 32);',
  20774. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20775. ' return Result;',
  20776. ' };',
  20777. ' this.SetLeft = function (Value) {',
  20778. ' };',
  20779. '});',
  20780. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20781. ' this.DoIt = function () {',
  20782. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20783. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20784. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20785. ' };',
  20786. '});',
  20787. 'this.b = null;',
  20788. '']),
  20789. LinesToStr([ // $mod.$main
  20790. '$mod.b.FSize = $mod.b.FSize + 11;',
  20791. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20792. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20793. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20794. 'var $with1 = $mod.b;',
  20795. '$with1.FSize = $with1.FSize + 31;',
  20796. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20797. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  20798. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  20799. '']));
  20800. end;
  20801. procedure TTestModule.TestClassHelper_Property_Array;
  20802. begin
  20803. StartProgram(false);
  20804. Add([
  20805. 'type',
  20806. ' TObject = class',
  20807. ' function GetSpeed(Index: boolean): word;',
  20808. ' procedure SetSpeed(Index: boolean; Value: word);',
  20809. ' end;',
  20810. ' TObjHelper = class helper for TObject',
  20811. ' function GetSize(Index: boolean): word;',
  20812. ' procedure SetSize(Index: boolean; Value: word);',
  20813. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20814. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20815. ' end;',
  20816. ' TBird = class',
  20817. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20818. ' procedure DoIt;',
  20819. ' end;',
  20820. 'var',
  20821. ' b: TBird;',
  20822. 'function Tobject.GetSpeed(Index: boolean): word;',
  20823. 'begin',
  20824. ' Result:=Size[false];',
  20825. ' Size[true]:=Size[false]+11;',
  20826. ' Speed[true]:=Speed[false]+12;',
  20827. ' Self.Size[true]:=Self.Size[false]+21;',
  20828. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20829. ' with Self do begin',
  20830. ' Size[true]:=Size[false]+31;',
  20831. ' Speed[true]:=Speed[false]+32;',
  20832. ' end;',
  20833. 'end;',
  20834. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20835. 'begin',
  20836. 'end;',
  20837. 'function TObjHelper.GetSize(Index: boolean): word;',
  20838. 'begin',
  20839. ' Size[true]:=Size[false]+11;',
  20840. ' Speed[true]:=Speed[false]+12;',
  20841. ' Self.Size[true]:=Self.Size[false]+21;',
  20842. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20843. ' with Self do begin',
  20844. ' Size[true]:=Size[false]+31;',
  20845. ' Speed[true]:=Speed[false]+32;',
  20846. ' end;',
  20847. 'end;',
  20848. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20849. 'begin',
  20850. 'end;',
  20851. 'procedure TBird.DoIt;',
  20852. 'begin',
  20853. ' Items[true]:=Items[false]+11;',
  20854. ' Self.Items[true]:=Self.Items[false]+21;',
  20855. ' with Self do Items[true]:=Items[false]+31;',
  20856. 'end;',
  20857. 'begin',
  20858. ' b.Size[true]:=b.Size[false]+11;',
  20859. ' b.Speed[true]:=b.Speed[false]+12;',
  20860. ' b.Items[true]:=b.Items[false]+13;',
  20861. ' with b do begin',
  20862. ' Size[true]:=Size[false]+21;',
  20863. ' Speed[true]:=Speed[false]+22;',
  20864. ' Items[true]:=Items[false]+23;',
  20865. ' end;',
  20866. '']);
  20867. ConvertProgram;
  20868. CheckSource('TestClassHelper_Property_Array',
  20869. LinesToStr([ // statements
  20870. 'rtl.createClass($mod, "TObject", null, function () {',
  20871. ' this.$init = function () {',
  20872. ' };',
  20873. ' this.$final = function () {',
  20874. ' };',
  20875. ' this.GetSpeed = function (Index) {',
  20876. ' var Result = 0;',
  20877. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20878. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20879. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20880. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20881. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20882. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20883. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20884. ' return Result;',
  20885. ' };',
  20886. ' this.SetSpeed = function (Index, Value) {',
  20887. ' };',
  20888. '});',
  20889. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20890. ' this.GetSize = function (Index) {',
  20891. ' var Result = 0;',
  20892. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20893. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20894. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20895. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20896. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20897. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20898. ' return Result;',
  20899. ' };',
  20900. ' this.SetSize = function (Index, Value) {',
  20901. ' };',
  20902. '});',
  20903. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20904. ' this.DoIt = function () {',
  20905. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20906. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20907. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20908. ' };',
  20909. '});',
  20910. 'this.b = null;',
  20911. '']),
  20912. LinesToStr([ // $mod.$main
  20913. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  20914. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  20915. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  20916. 'var $with1 = $mod.b;',
  20917. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  20918. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  20919. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  20920. '']));
  20921. end;
  20922. procedure TTestModule.TestClassHelper_Property_Array_Default;
  20923. begin
  20924. StartProgram(false);
  20925. Add([
  20926. 'type',
  20927. ' TObject = class',
  20928. ' function GetSpeed(Index: boolean): word;',
  20929. ' procedure SetSpeed(Index: boolean; Value: word);',
  20930. ' end;',
  20931. ' TObjHelper = class helper for TObject',
  20932. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  20933. ' end;',
  20934. ' TBird = class',
  20935. ' end;',
  20936. ' TBirdHelper = class helper for TBird',
  20937. ' function GetSize(Index: word): boolean;',
  20938. ' procedure SetSize(Index: word; Value: boolean);',
  20939. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  20940. ' end;',
  20941. 'function Tobject.GetSpeed(Index: boolean): word;',
  20942. 'begin',
  20943. ' Self[true]:=Self[false]+1;',
  20944. 'end;',
  20945. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20946. 'begin',
  20947. 'end;',
  20948. 'function TBirdHelper.GetSize(Index: word): boolean;',
  20949. 'begin',
  20950. ' Self[1]:=not Self[2];',
  20951. 'end;',
  20952. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  20953. 'begin',
  20954. 'end;',
  20955. 'var',
  20956. ' o: TObject;',
  20957. ' b: TBird;',
  20958. 'begin',
  20959. ' o[true]:=o[false]+1;',
  20960. ' b[3]:=not b[4];',
  20961. '']);
  20962. ConvertProgram;
  20963. CheckSource('TestClassHelper_Property_Array_Default',
  20964. LinesToStr([ // statements
  20965. 'rtl.createClass($mod, "TObject", null, function () {',
  20966. ' this.$init = function () {',
  20967. ' };',
  20968. ' this.$final = function () {',
  20969. ' };',
  20970. ' this.GetSpeed = function (Index) {',
  20971. ' var Result = 0;',
  20972. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  20973. ' return Result;',
  20974. ' };',
  20975. ' this.SetSpeed = function (Index, Value) {',
  20976. ' };',
  20977. '});',
  20978. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20979. '});',
  20980. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20981. '});',
  20982. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20983. ' this.GetSize = function (Index) {',
  20984. ' var Result = false;',
  20985. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  20986. ' return Result;',
  20987. ' };',
  20988. ' this.SetSize = function (Index, Value) {',
  20989. ' };',
  20990. '});',
  20991. 'this.o = null;',
  20992. 'this.b = null;',
  20993. '']),
  20994. LinesToStr([ // $mod.$main
  20995. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  20996. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  20997. '']));
  20998. end;
  20999. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21000. begin
  21001. StartProgram(false);
  21002. Add([
  21003. 'type',
  21004. ' TObject = class',
  21005. ' end;',
  21006. ' TObjHelper = class helper for TObject',
  21007. ' function GetItems(Index: word): TObject;',
  21008. ' procedure SetItems(Index: word; Value: TObject);',
  21009. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21010. ' end;',
  21011. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21012. 'begin',
  21013. ' Self[1][2]:=Self[3][4];',
  21014. 'end;',
  21015. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21016. 'begin',
  21017. 'end;',
  21018. 'var',
  21019. ' o: TObject;',
  21020. 'begin',
  21021. ' o[1][2]:=o[3][4];',
  21022. '']);
  21023. ConvertProgram;
  21024. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21025. LinesToStr([ // statements
  21026. 'rtl.createClass($mod, "TObject", null, function () {',
  21027. ' this.$init = function () {',
  21028. ' };',
  21029. ' this.$final = function () {',
  21030. ' };',
  21031. '});',
  21032. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21033. ' this.GetItems = function (Index) {',
  21034. ' var Result = null;',
  21035. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21036. ' return Result;',
  21037. ' };',
  21038. ' this.SetItems = function (Index, Value) {',
  21039. ' };',
  21040. '});',
  21041. 'this.o = null;',
  21042. '']),
  21043. LinesToStr([ // $mod.$main
  21044. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21045. '']));
  21046. end;
  21047. procedure TTestModule.TestClassHelper_ClassProperty;
  21048. begin
  21049. StartProgram(false);
  21050. Add([
  21051. 'type',
  21052. ' TObject = class',
  21053. ' class var FSize: word;',
  21054. ' class function GetSpeed: word;',
  21055. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21056. ' end;',
  21057. ' TObjHelper = class helper for TObject',
  21058. ' class function GetLeft: word;',
  21059. ' class procedure SetLeft(Value: word);',
  21060. ' class property Size: word read FSize write FSize;',
  21061. ' class property Speed: word read GetSpeed write SetSpeed;',
  21062. ' class property Left: word read GetLeft write SetLeft;',
  21063. ' end;',
  21064. ' TBird = class',
  21065. ' class property NotRight: word read GetLeft write SetLeft;',
  21066. ' class procedure DoIt;',
  21067. ' end;',
  21068. ' TBirdClass = class of TBird;',
  21069. 'class function Tobject.GetSpeed: word;',
  21070. 'begin',
  21071. ' Size:=Size+11;',
  21072. ' Speed:=Speed+12;',
  21073. ' Left:=Left+13;',
  21074. ' Self.Size:=Self.Size+21;',
  21075. ' Self.Speed:=Self.Speed+22;',
  21076. ' Self.Left:=Self.Left+23;',
  21077. ' with Self do begin',
  21078. ' Size:=Size+31;',
  21079. ' Speed:=Speed+32;',
  21080. ' Left:=Left+33;',
  21081. ' end;',
  21082. 'end;',
  21083. 'class function TObjHelper.GetLeft: word;',
  21084. 'begin',
  21085. ' Size:=Size+11;',
  21086. ' Speed:=Speed+12;',
  21087. ' Left:=Left+13;',
  21088. ' Self.Size:=Self.Size+21;',
  21089. ' Self.Speed:=Self.Speed+22;',
  21090. ' Self.Left:=Self.Left+23;',
  21091. ' with Self do begin',
  21092. ' Size:=Size+31;',
  21093. ' Speed:=Speed+32;',
  21094. ' Left:=Left+33;',
  21095. ' end;',
  21096. 'end;',
  21097. 'class procedure TObjHelper.SetLeft(Value: word);',
  21098. 'begin',
  21099. 'end;',
  21100. 'class procedure TBird.DoIt;',
  21101. 'begin',
  21102. ' NotRight:=NotRight+11;',
  21103. ' Self.NotRight:=Self.NotRight+21;',
  21104. ' with Self do NotRight:=NotRight+31;',
  21105. 'end;',
  21106. 'var',
  21107. ' b: TBird;',
  21108. ' c: TBirdClass;',
  21109. 'begin',
  21110. ' b.Size:=b.Size+11;',
  21111. ' b.Speed:=b.Speed+12;',
  21112. ' b.Left:=b.Left+13;',
  21113. ' b.NotRight:=b.NotRight+14;',
  21114. ' with b do begin',
  21115. ' Size:=Size+31;',
  21116. ' Speed:=Speed+32;',
  21117. ' Left:=Left+33;',
  21118. ' NotRight:=NotRight+34;',
  21119. ' end;',
  21120. ' c.Size:=c.Size+11;',
  21121. ' c.Speed:=c.Speed+12;',
  21122. ' c.Left:=c.Left+13;',
  21123. ' c.NotRight:=c.NotRight+14;',
  21124. ' with c do begin',
  21125. ' Size:=Size+31;',
  21126. ' Speed:=Speed+32;',
  21127. ' Left:=Left+33;',
  21128. ' NotRight:=NotRight+34;',
  21129. ' end;',
  21130. ' tbird.Size:=tbird.Size+11;',
  21131. ' tbird.Speed:=tbird.Speed+12;',
  21132. ' tbird.Left:=tbird.Left+13;',
  21133. ' tbird.NotRight:=tbird.NotRight+14;',
  21134. ' with tbird do begin',
  21135. ' Size:=Size+31;',
  21136. ' Speed:=Speed+32;',
  21137. ' Left:=Left+33;',
  21138. ' NotRight:=NotRight+34;',
  21139. ' end;',
  21140. '']);
  21141. ConvertProgram;
  21142. CheckSource('TestClassHelper_ClassProperty',
  21143. LinesToStr([ // statements
  21144. 'rtl.createClass($mod, "TObject", null, function () {',
  21145. ' this.FSize = 0;',
  21146. ' this.$init = function () {',
  21147. ' };',
  21148. ' this.$final = function () {',
  21149. ' };',
  21150. ' this.GetSpeed = function () {',
  21151. ' var Result = 0;',
  21152. ' $mod.TObject.FSize = this.FSize + 11;',
  21153. ' this.SetSpeed(this.GetSpeed() + 12);',
  21154. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21155. ' $mod.TObject.FSize = this.FSize + 21;',
  21156. ' this.SetSpeed(this.GetSpeed() + 22);',
  21157. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21158. ' $mod.TObject.FSize = this.FSize + 31;',
  21159. ' this.SetSpeed(this.GetSpeed() + 32);',
  21160. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21161. ' return Result;',
  21162. ' };',
  21163. '});',
  21164. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21165. ' this.GetLeft = function () {',
  21166. ' var Result = 0;',
  21167. ' $mod.TObject.FSize = this.FSize + 11;',
  21168. ' this.SetSpeed(this.GetSpeed() + 12);',
  21169. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21170. ' $mod.TObject.FSize = this.FSize + 21;',
  21171. ' this.SetSpeed(this.GetSpeed() + 22);',
  21172. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21173. ' $mod.TObject.FSize = this.FSize + 31;',
  21174. ' this.SetSpeed(this.GetSpeed() + 32);',
  21175. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21176. ' return Result;',
  21177. ' };',
  21178. ' this.SetLeft = function (Value) {',
  21179. ' };',
  21180. '});',
  21181. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21182. ' this.DoIt = function () {',
  21183. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21184. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21185. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21186. ' };',
  21187. '});',
  21188. 'this.b = null;',
  21189. 'this.c = null;',
  21190. '']),
  21191. LinesToStr([ // $mod.$main
  21192. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21193. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21194. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21195. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21196. 'var $with1 = $mod.b;',
  21197. '$mod.TObject.FSize = $with1.FSize + 31;',
  21198. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  21199. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  21200. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  21201. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21202. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21203. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21204. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21205. 'var $with2 = $mod.c;',
  21206. '$mod.TObject.FSize = $with2.FSize + 31;',
  21207. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21208. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  21209. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  21210. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21211. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21212. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21213. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21214. 'var $with3 = $mod.TBird;',
  21215. '$mod.TObject.FSize = $with3.FSize + 31;',
  21216. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  21217. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21218. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21219. '']));
  21220. end;
  21221. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21222. begin
  21223. StartProgram(false);
  21224. Add([
  21225. 'type',
  21226. ' TObject = class',
  21227. ' class function GetSpeed: word; static;',
  21228. ' class procedure SetSpeed(Value: word); static;',
  21229. ' end;',
  21230. ' TObjHelper = class helper for TObject',
  21231. ' class function GetLeft: word; static;',
  21232. ' class procedure SetLeft(Value: word); static;',
  21233. ' class property Speed: word read GetSpeed write SetSpeed;',
  21234. ' class property Left: word read GetLeft write SetLeft;',
  21235. ' end;',
  21236. ' TBird = class',
  21237. ' class property NotRight: word read GetLeft write SetLeft;',
  21238. ' class procedure DoIt; static;',
  21239. ' class procedure DoSome;',
  21240. ' end;',
  21241. ' TBirdClass = class of TBird;',
  21242. 'class function Tobject.GetSpeed: word;',
  21243. 'begin',
  21244. ' Speed:=Speed+12;',
  21245. ' Left:=Left+13;',
  21246. 'end;',
  21247. 'class procedure TObject.SetSpeed(Value: word);',
  21248. 'begin',
  21249. 'end;',
  21250. 'class function TObjHelper.GetLeft: word;',
  21251. 'begin',
  21252. ' Speed:=Speed+12;',
  21253. ' Left:=Left+13;',
  21254. 'end;',
  21255. 'class procedure TObjHelper.SetLeft(Value: word);',
  21256. 'begin',
  21257. 'end;',
  21258. 'class procedure TBird.DoIt;',
  21259. 'begin',
  21260. ' NotRight:=NotRight+11;',
  21261. 'end;',
  21262. 'class procedure TBird.DoSome;',
  21263. 'begin',
  21264. ' Speed:=Speed+12;',
  21265. ' Left:=Left+13;',
  21266. ' Self.Speed:=Self.Speed+22;',
  21267. ' Self.Left:=Self.Left+23;',
  21268. ' with Self do begin',
  21269. ' Speed:=Speed+32;',
  21270. ' Left:=Left+33;',
  21271. ' end;',
  21272. ' NotRight:=NotRight+11;',
  21273. ' Self.NotRight:=Self.NotRight+21;',
  21274. ' with Self do NotRight:=NotRight+31;',
  21275. 'end;',
  21276. 'var',
  21277. ' b: TBird;',
  21278. ' c: TBirdClass;',
  21279. 'begin',
  21280. ' b.Speed:=b.Speed+12;',
  21281. ' b.Left:=b.Left+13;',
  21282. ' b.NotRight:=b.NotRight+14;',
  21283. ' with b do begin',
  21284. ' Speed:=Speed+32;',
  21285. ' Left:=Left+33;',
  21286. ' NotRight:=NotRight+34;',
  21287. ' end;',
  21288. ' c.Speed:=c.Speed+12;',
  21289. ' c.Left:=c.Left+13;',
  21290. ' c.NotRight:=c.NotRight+14;',
  21291. ' with c do begin',
  21292. ' Speed:=Speed+32;',
  21293. ' Left:=Left+33;',
  21294. ' NotRight:=NotRight+34;',
  21295. ' end;',
  21296. ' tbird.Speed:=tbird.Speed+12;',
  21297. ' tbird.Left:=tbird.Left+13;',
  21298. ' tbird.NotRight:=tbird.NotRight+14;',
  21299. ' with tbird do begin',
  21300. ' Speed:=Speed+32;',
  21301. ' Left:=Left+33;',
  21302. ' NotRight:=NotRight+34;',
  21303. ' end;',
  21304. '']);
  21305. ConvertProgram;
  21306. CheckSource('TestClassHelper_ClassPropertyStatic',
  21307. LinesToStr([ // statements
  21308. 'rtl.createClass($mod, "TObject", null, function () {',
  21309. ' this.$init = function () {',
  21310. ' };',
  21311. ' this.$final = function () {',
  21312. ' };',
  21313. ' this.GetSpeed = function () {',
  21314. ' var Result = 0;',
  21315. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21316. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21317. ' return Result;',
  21318. ' };',
  21319. ' this.SetSpeed = function (Value) {',
  21320. ' };',
  21321. '});',
  21322. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21323. ' this.GetLeft = function () {',
  21324. ' var Result = 0;',
  21325. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21326. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21327. ' return Result;',
  21328. ' };',
  21329. ' this.SetLeft = function (Value) {',
  21330. ' };',
  21331. '});',
  21332. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21333. ' this.DoIt = function () {',
  21334. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21335. ' };',
  21336. ' this.DoSome = function () {',
  21337. ' this.SetSpeed(this.GetSpeed() + 12);',
  21338. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21339. ' this.SetSpeed(this.GetSpeed() + 22);',
  21340. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21341. ' this.SetSpeed(this.GetSpeed() + 32);',
  21342. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21343. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21344. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21345. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21346. ' };',
  21347. '});',
  21348. 'this.b = null;',
  21349. 'this.c = null;',
  21350. '']),
  21351. LinesToStr([ // $mod.$main
  21352. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21353. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21354. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21355. 'var $with1 = $mod.b;',
  21356. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21357. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21358. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21359. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21360. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21361. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21362. 'var $with2 = $mod.c;',
  21363. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21364. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21365. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21366. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21367. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21368. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21369. 'var $with3 = $mod.TBird;',
  21370. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  21371. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21372. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21373. '']));
  21374. end;
  21375. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21376. begin
  21377. StartProgram(false);
  21378. Add([
  21379. 'type',
  21380. ' TObject = class',
  21381. ' class function GetSpeed(Index: boolean): word;',
  21382. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21383. ' end;',
  21384. ' TObjHelper = class helper for TObject',
  21385. ' class function GetSize(Index: boolean): word;',
  21386. ' class procedure SetSize(Index: boolean; Value: word);',
  21387. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21388. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21389. ' end;',
  21390. ' TBird = class',
  21391. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21392. ' class procedure DoIt;',
  21393. ' end;',
  21394. ' TBirdClass = class of TBird;',
  21395. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21396. 'begin',
  21397. ' Size[true]:=Size[false]+11;',
  21398. ' Speed[true]:=Speed[false]+12;',
  21399. ' Self.Size[true]:=Self.Size[false]+21;',
  21400. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21401. ' with Self do begin',
  21402. ' Size[true]:=Size[false]+31;',
  21403. ' Speed[true]:=Speed[false]+32;',
  21404. ' end;',
  21405. 'end;',
  21406. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21407. 'begin',
  21408. ' Size[true]:=Size[false]+11;',
  21409. ' Speed[true]:=Speed[false]+12;',
  21410. ' Self.Size[true]:=Self.Size[false]+21;',
  21411. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21412. ' with Self do begin',
  21413. ' Size[true]:=Size[false]+31;',
  21414. ' Speed[true]:=Speed[false]+32;',
  21415. ' end;',
  21416. 'end;',
  21417. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21418. 'begin',
  21419. 'end;',
  21420. 'class procedure TBird.DoIt;',
  21421. 'begin',
  21422. ' Items[true]:=Items[false]+11;',
  21423. ' Self.Items[true]:=Self.Items[false]+21;',
  21424. ' with Self do Items[true]:=Items[false]+31;',
  21425. 'end;',
  21426. 'var',
  21427. ' b: TBird;',
  21428. ' c: TBirdClass;',
  21429. 'begin',
  21430. ' b.Size[true]:=b.Size[false]+11;',
  21431. ' b.Speed[true]:=b.Speed[false]+12;',
  21432. ' b.Items[true]:=b.Items[false]+13;',
  21433. ' with b do begin',
  21434. ' Size[true]:=Size[false]+21;',
  21435. ' Speed[true]:=Speed[false]+22;',
  21436. ' Items[true]:=Items[false]+23;',
  21437. ' end;',
  21438. ' c.Size[true]:=c.Size[false]+11;',
  21439. ' c.Speed[true]:=c.Speed[false]+12;',
  21440. ' c.Items[true]:=c.Items[false]+13;',
  21441. ' with c do begin',
  21442. ' Size[true]:=Size[false]+21;',
  21443. ' Speed[true]:=Speed[false]+22;',
  21444. ' Items[true]:=Items[false]+23;',
  21445. ' end;',
  21446. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21447. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21448. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21449. ' with TBird do begin',
  21450. ' Size[true]:=Size[false]+21;',
  21451. ' Speed[true]:=Speed[false]+22;',
  21452. ' Items[true]:=Items[false]+23;',
  21453. ' end;',
  21454. '']);
  21455. ConvertProgram;
  21456. CheckSource('TestClassHelper_ClassProperty_Array',
  21457. LinesToStr([ // statements
  21458. 'rtl.createClass($mod, "TObject", null, function () {',
  21459. ' this.$init = function () {',
  21460. ' };',
  21461. ' this.$final = function () {',
  21462. ' };',
  21463. ' this.GetSpeed = function (Index) {',
  21464. ' var Result = 0;',
  21465. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21466. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21467. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21468. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21469. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21470. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21471. ' return Result;',
  21472. ' };',
  21473. '});',
  21474. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21475. ' this.GetSize = function (Index) {',
  21476. ' var Result = 0;',
  21477. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21478. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21479. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21480. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21481. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21482. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21483. ' return Result;',
  21484. ' };',
  21485. ' this.SetSize = function (Index, Value) {',
  21486. ' };',
  21487. '});',
  21488. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21489. ' this.DoIt = function () {',
  21490. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21491. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21492. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21493. ' };',
  21494. '});',
  21495. 'this.b = null;',
  21496. 'this.c = null;',
  21497. '']),
  21498. LinesToStr([ // $mod.$main
  21499. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21500. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21501. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21502. 'var $with1 = $mod.b;',
  21503. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  21504. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  21505. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  21506. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21507. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21508. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21509. 'var $with2 = $mod.c;',
  21510. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  21511. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21512. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  21513. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21514. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21515. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21516. 'var $with3 = $mod.TBird;',
  21517. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21518. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  21519. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21520. '']));
  21521. end;
  21522. procedure TTestModule.TestClassHelper_ForIn;
  21523. begin
  21524. StartProgram(false);
  21525. Add([
  21526. 'type',
  21527. ' TObject = class end;',
  21528. ' TItem = TObject;',
  21529. ' TEnumerator = class',
  21530. ' FCurrent: TItem;',
  21531. ' property Current: TItem read FCurrent;',
  21532. ' function MoveNext: boolean;',
  21533. ' end;',
  21534. ' TBird = class',
  21535. ' end;',
  21536. ' TBirdHelper = class helper for TBird',
  21537. ' function GetEnumerator: TEnumerator;',
  21538. ' end;',
  21539. 'function TEnumerator.MoveNext: boolean;',
  21540. 'begin',
  21541. 'end;',
  21542. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21543. 'begin',
  21544. 'end;',
  21545. 'var',
  21546. ' b: TBird;',
  21547. ' i, i2: TItem;',
  21548. 'begin',
  21549. ' for i in b do i2:=i;']);
  21550. ConvertProgram;
  21551. CheckSource('TestClassHelper_ForIn',
  21552. LinesToStr([ // statements
  21553. 'rtl.createClass($mod, "TObject", null, function () {',
  21554. ' this.$init = function () {',
  21555. ' };',
  21556. ' this.$final = function () {',
  21557. ' };',
  21558. '});',
  21559. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  21560. ' this.$init = function () {',
  21561. ' $mod.TObject.$init.call(this);',
  21562. ' this.FCurrent = null;',
  21563. ' };',
  21564. ' this.$final = function () {',
  21565. ' this.FCurrent = undefined;',
  21566. ' $mod.TObject.$final.call(this);',
  21567. ' };',
  21568. ' this.MoveNext = function () {',
  21569. ' var Result = false;',
  21570. ' return Result;',
  21571. ' };',
  21572. '});',
  21573. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21574. '});',
  21575. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21576. ' this.GetEnumerator = function () {',
  21577. ' var Result = null;',
  21578. ' return Result;',
  21579. ' };',
  21580. '});',
  21581. 'this.b = null;',
  21582. 'this.i = null;',
  21583. 'this.i2 = null;'
  21584. ]),
  21585. LinesToStr([ // $mod.$main
  21586. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  21587. 'try {',
  21588. ' while ($in1.MoveNext()){',
  21589. ' $mod.i = $in1.FCurrent;',
  21590. ' $mod.i2 = $mod.i;',
  21591. ' }',
  21592. '} finally {',
  21593. ' $in1 = rtl.freeLoc($in1)',
  21594. '};',
  21595. '']));
  21596. end;
  21597. procedure TTestModule.TestClassHelper_PassProperty;
  21598. begin
  21599. StartProgram(false);
  21600. Add([
  21601. 'type',
  21602. ' TObject = class',
  21603. ' FField: TObject;',
  21604. ' property Field: TObject read FField write FField;',
  21605. ' end;',
  21606. ' THelper = class helper for TObject',
  21607. ' procedure Fly;',
  21608. ' class procedure Run;',
  21609. ' class procedure Jump; static;',
  21610. ' end;',
  21611. 'procedure THelper.Fly;',
  21612. 'begin',
  21613. ' Field.Fly;',
  21614. ' Field.Run;',
  21615. ' Field.Jump;',
  21616. ' with Field do begin',
  21617. ' Fly;',
  21618. ' Run;',
  21619. ' Jump;',
  21620. ' end;',
  21621. 'end;',
  21622. 'class procedure THelper.Run;',
  21623. 'begin',
  21624. 'end;',
  21625. 'class procedure THelper.Jump;',
  21626. 'begin',
  21627. 'end;',
  21628. 'var',
  21629. ' b: TObject;',
  21630. 'begin',
  21631. ' b.Field.Fly;',
  21632. ' b.Field.Run;',
  21633. ' b.Field.Jump;',
  21634. ' with b do begin',
  21635. ' Field.Run;',
  21636. ' Field.Fly;',
  21637. ' Field.Jump;',
  21638. ' end;',
  21639. ' with b.Field do begin',
  21640. ' Run;',
  21641. ' Fly;',
  21642. ' Jump;',
  21643. ' end;',
  21644. '']);
  21645. ConvertProgram;
  21646. CheckSource('TestClassHelper_PassProperty',
  21647. LinesToStr([ // statements
  21648. 'rtl.createClass($mod, "TObject", null, function () {',
  21649. ' this.$init = function () {',
  21650. ' this.FField = null;',
  21651. ' };',
  21652. ' this.$final = function () {',
  21653. ' this.FField = undefined;',
  21654. ' };',
  21655. '});',
  21656. 'rtl.createHelper($mod, "THelper", null, function () {',
  21657. ' this.Fly = function () {',
  21658. ' $mod.THelper.Fly.call(this.FField);',
  21659. ' $mod.THelper.Run.call(this.FField.$class);',
  21660. ' $mod.THelper.Jump();',
  21661. ' var $with1 = this.FField;',
  21662. ' $mod.THelper.Fly.call($with1);',
  21663. ' $mod.THelper.Run.call($with1.$class);',
  21664. ' $mod.THelper.Jump();',
  21665. ' };',
  21666. ' this.Run = function () {',
  21667. ' };',
  21668. ' this.Jump = function () {',
  21669. ' };',
  21670. '});',
  21671. 'this.b = null;',
  21672. '']),
  21673. LinesToStr([ // $mod.$main
  21674. '$mod.THelper.Fly.call($mod.b.FField);',
  21675. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21676. '$mod.THelper.Jump();',
  21677. 'var $with1 = $mod.b;',
  21678. '$mod.THelper.Run.call($with1.FField.$class);',
  21679. '$mod.THelper.Fly.call($with1.FField);',
  21680. '$mod.THelper.Jump();',
  21681. 'var $with2 = $mod.b.FField;',
  21682. '$mod.THelper.Run.call($with2.$class);',
  21683. '$mod.THelper.Fly.call($with2);',
  21684. '$mod.THelper.Jump();',
  21685. '']));
  21686. end;
  21687. procedure TTestModule.TestExtClassHelper_ClassVar;
  21688. begin
  21689. StartProgram(false);
  21690. Add([
  21691. '{$modeswitch externalclass}',
  21692. 'type',
  21693. ' TExtA = class external name ''ExtObj''',
  21694. ' end;',
  21695. ' THelper = class helper for TExtA',
  21696. ' const',
  21697. ' One = 1;',
  21698. ' Two: word = 2;',
  21699. ' class var',
  21700. ' Glob: word;',
  21701. ' function Foo(w: word): word;',
  21702. ' class function Bar(w: word): word; static;',
  21703. ' end;',
  21704. 'function THelper.foo(w: word): word;',
  21705. 'begin',
  21706. ' Result:=w;',
  21707. ' Two:=One+w;',
  21708. ' Glob:=Glob;',
  21709. ' Result:=Self.Glob;',
  21710. ' Self.Glob:=Self.Glob;',
  21711. ' with Self do Glob:=Glob;',
  21712. 'end;',
  21713. 'class function THelper.bar(w: word): word;',
  21714. 'begin',
  21715. ' Result:=w;',
  21716. ' Two:=One;',
  21717. ' Glob:=Glob;',
  21718. 'end;',
  21719. 'var o: TExtA;',
  21720. 'begin',
  21721. ' texta.two:=texta.one;',
  21722. ' texta.Glob:=texta.Glob;',
  21723. ' with texta do begin',
  21724. ' two:=one;',
  21725. ' Glob:=Glob;',
  21726. ' end;',
  21727. ' o.two:=o.one;',
  21728. ' o.Glob:=o.Glob;',
  21729. ' with o do begin',
  21730. ' two:=one;',
  21731. ' Glob:=Glob;',
  21732. ' end;',
  21733. '']);
  21734. ConvertProgram;
  21735. CheckSource('TestExtClassHelper_ClassVar',
  21736. LinesToStr([ // statements
  21737. 'rtl.createHelper($mod, "THelper", null, function () {',
  21738. ' this.One = 1;',
  21739. ' this.Two = 2;',
  21740. ' this.Glob = 0;',
  21741. ' this.Foo = function (w) {',
  21742. ' var Result = 0;',
  21743. ' Result = w;',
  21744. ' $mod.THelper.Two = 1 + w;',
  21745. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21746. ' Result = $mod.THelper.Glob;',
  21747. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21748. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21749. ' return Result;',
  21750. ' };',
  21751. ' this.Bar = function (w) {',
  21752. ' var Result = 0;',
  21753. ' Result = w;',
  21754. ' $mod.THelper.Two = 1;',
  21755. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21756. ' return Result;',
  21757. ' };',
  21758. '});',
  21759. 'this.o = null;',
  21760. '']),
  21761. LinesToStr([ // $mod.$main
  21762. '$mod.THelper.Two = 1;',
  21763. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21764. '$mod.THelper.Two = 1;',
  21765. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21766. '$mod.THelper.Two = 1;',
  21767. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21768. 'var $with1 = $mod.o;',
  21769. '$mod.THelper.Two = 1;',
  21770. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21771. '']));
  21772. end;
  21773. procedure TTestModule.TestExtClassHelper_Method_Call;
  21774. begin
  21775. StartProgram(false);
  21776. Add([
  21777. '{$modeswitch externalclass}',
  21778. 'type',
  21779. ' TFly = function(w: word): word of object;',
  21780. ' TExtA = class external name ''ExtObj''',
  21781. ' procedure Run(w: word = 10);',
  21782. ' end;',
  21783. ' THelper = class helper for TExtA',
  21784. ' function Foo(w: word = 1): word;',
  21785. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21786. ' end;',
  21787. 'var p: TFly;',
  21788. 'function THelper.foo(w: word): word;',
  21789. 'begin',
  21790. ' Run;',
  21791. ' Run();',
  21792. ' Run(11);',
  21793. ' Foo;',
  21794. ' Foo();',
  21795. ' Foo(12);',
  21796. ' Self.Foo;',
  21797. ' Self.Foo();',
  21798. ' Self.Foo(13);',
  21799. ' Fly;',
  21800. ' Fly();',
  21801. ' with Self do begin',
  21802. ' Foo;',
  21803. ' Foo();',
  21804. ' Foo(14);',
  21805. ' Fly;',
  21806. ' Fly();',
  21807. ' end;',
  21808. ' p:=@Fly;',
  21809. 'end;',
  21810. 'var Obj: TExtA;',
  21811. 'begin',
  21812. ' obj.Foo;',
  21813. ' obj.Foo();',
  21814. ' obj.Foo(21);',
  21815. ' obj.Fly;',
  21816. ' obj.Fly();',
  21817. ' with obj do begin',
  21818. ' Foo;',
  21819. ' Foo();',
  21820. ' Foo(22);',
  21821. ' Fly;',
  21822. ' Fly();',
  21823. ' end;',
  21824. ' p:[email protected];',
  21825. '']);
  21826. ConvertProgram;
  21827. CheckSource('TestExtClassHelper_Method_Call',
  21828. LinesToStr([ // statements
  21829. 'rtl.createHelper($mod, "THelper", null, function () {',
  21830. ' this.Foo = function (w) {',
  21831. ' var Result = 0;',
  21832. ' this.Run(10);',
  21833. ' this.Run(10);',
  21834. ' this.Run(11);',
  21835. ' $mod.THelper.Foo.call(this, 1);',
  21836. ' $mod.THelper.Foo.call(this, 1);',
  21837. ' $mod.THelper.Foo.call(this, 12);',
  21838. ' $mod.THelper.Foo.call(this, 1);',
  21839. ' $mod.THelper.Foo.call(this, 1);',
  21840. ' $mod.THelper.Foo.call(this, 13);',
  21841. ' this.Fly(2);',
  21842. ' this.Fly(2);',
  21843. ' $mod.THelper.Foo.call(this, 1);',
  21844. ' $mod.THelper.Foo.call(this, 1);',
  21845. ' $mod.THelper.Foo.call(this, 14);',
  21846. ' this.Fly(2);',
  21847. ' this.Fly(2);',
  21848. ' $mod.p = rtl.createCallback(this, "Fly");',
  21849. ' return Result;',
  21850. ' };',
  21851. '});',
  21852. 'this.p = null;',
  21853. 'this.Obj = null;',
  21854. '']),
  21855. LinesToStr([ // $mod.$main
  21856. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21857. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21858. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21859. '$mod.Obj.Fly(2);',
  21860. '$mod.Obj.Fly(2);',
  21861. 'var $with1 = $mod.Obj;',
  21862. '$mod.THelper.Foo.call($with1, 1);',
  21863. '$mod.THelper.Foo.call($with1, 1);',
  21864. '$mod.THelper.Foo.call($with1, 22);',
  21865. '$with1.Fly(2);',
  21866. '$with1.Fly(2);',
  21867. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21868. '']));
  21869. end;
  21870. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21871. begin
  21872. StartProgram(false);
  21873. Add([
  21874. '{$modeswitch externalclass}',
  21875. 'type',
  21876. ' TExtA = class external name ''ExtObj''',
  21877. ' procedure Run(w: word = 10);',
  21878. ' end;',
  21879. ' THelper = class helper for TExtA',
  21880. ' class procedure Fly;',
  21881. ' end;',
  21882. 'class procedure THelper.Fly;',
  21883. 'begin end;',
  21884. 'begin',
  21885. '']);
  21886. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  21887. nHelperClassMethodForExtClassMustBeStatic);
  21888. ConvertProgram;
  21889. end;
  21890. procedure TTestModule.TestRecordHelper_ClassVar;
  21891. begin
  21892. StartProgram(false);
  21893. Add([
  21894. 'type',
  21895. ' TRec = record',
  21896. ' end;',
  21897. ' THelper = record helper for TRec',
  21898. ' const',
  21899. ' One = 1;',
  21900. ' Two: word = 2;',
  21901. ' class var',
  21902. ' Glob: word;',
  21903. ' function Foo(w: word): word;',
  21904. ' class function Bar(w: word): word; static;',
  21905. ' end;',
  21906. 'function THelper.foo(w: word): word;',
  21907. 'begin',
  21908. ' Result:=w;',
  21909. ' Two:=One+w;',
  21910. ' Glob:=Glob;',
  21911. ' Result:=Self.Glob;',
  21912. ' Self.Glob:=Self.Glob;',
  21913. ' with Self do Glob:=Glob;',
  21914. ' Self:=Self;',
  21915. 'end;',
  21916. 'class function THelper.bar(w: word): word;',
  21917. 'begin',
  21918. ' Result:=w;',
  21919. ' Two:=One;',
  21920. ' Glob:=Glob;',
  21921. 'end;',
  21922. 'var r: TRec;',
  21923. 'begin',
  21924. ' trec.two:=trec.one;',
  21925. ' trec.Glob:=trec.Glob;',
  21926. ' with trec do begin',
  21927. ' two:=one;',
  21928. ' Glob:=Glob;',
  21929. ' end;',
  21930. ' r.two:=r.one;',
  21931. ' r.Glob:=r.Glob;',
  21932. ' with r do begin',
  21933. ' two:=one;',
  21934. ' Glob:=Glob;',
  21935. ' end;',
  21936. '']);
  21937. ConvertProgram;
  21938. CheckSource('TestRecordHelper_ClassVar',
  21939. LinesToStr([ // statements
  21940. 'rtl.recNewT($mod, "TRec", function () {',
  21941. ' this.$eq = function (b) {',
  21942. ' return true;',
  21943. ' };',
  21944. ' this.$assign = function (s) {',
  21945. ' return this;',
  21946. ' };',
  21947. '});',
  21948. 'rtl.createHelper($mod, "THelper", null, function () {',
  21949. ' this.One = 1;',
  21950. ' this.Two = 2;',
  21951. ' this.Glob = 0;',
  21952. ' this.Foo = function (w) {',
  21953. ' var Result = 0;',
  21954. ' Result = w;',
  21955. ' $mod.THelper.Two = 1 + w;',
  21956. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21957. ' Result = $mod.THelper.Glob;',
  21958. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21959. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21960. ' this.$assign(this);',
  21961. ' return Result;',
  21962. ' };',
  21963. ' this.Bar = function (w) {',
  21964. ' var Result = 0;',
  21965. ' Result = w;',
  21966. ' $mod.THelper.Two = 1;',
  21967. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21968. ' return Result;',
  21969. ' };',
  21970. '});',
  21971. 'this.r = $mod.TRec.$new();',
  21972. '']),
  21973. LinesToStr([ // $mod.$main
  21974. '$mod.THelper.Two = 1;',
  21975. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21976. 'var $with1 = $mod.TRec;',
  21977. '$mod.THelper.Two = 1;',
  21978. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21979. '$mod.THelper.Two = 1;',
  21980. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21981. 'var $with2 = $mod.r;',
  21982. '$mod.THelper.Two = 1;',
  21983. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21984. '']));
  21985. end;
  21986. procedure TTestModule.TestRecordHelper_Method_Call;
  21987. begin
  21988. StartProgram(false);
  21989. Add([
  21990. '{$modeswitch AdvancedRecords}',
  21991. 'type',
  21992. ' TRec = record',
  21993. ' procedure Run(w: word = 10);',
  21994. ' end;',
  21995. ' THelper = record helper for TRec',
  21996. ' function Foo(w: word = 1): word;',
  21997. ' end;',
  21998. 'procedure TRec.Run(w: word);',
  21999. 'begin',
  22000. ' Foo;',
  22001. ' Foo();',
  22002. ' Foo(2);',
  22003. ' Self.Foo;',
  22004. ' Self.Foo();',
  22005. ' Self.Foo(3);',
  22006. ' with Self do begin',
  22007. ' Foo;',
  22008. ' Foo();',
  22009. ' Foo(4);',
  22010. ' end;',
  22011. 'end;',
  22012. 'function THelper.foo(w: word): word;',
  22013. 'begin',
  22014. ' Run;',
  22015. ' Run();',
  22016. ' Run(11);',
  22017. ' Foo;',
  22018. ' Foo();',
  22019. ' Foo(12);',
  22020. ' Self.Foo;',
  22021. ' Self.Foo();',
  22022. ' Self.Foo(13);',
  22023. ' with Self do begin',
  22024. ' Foo;',
  22025. ' Foo();',
  22026. ' Foo(14);',
  22027. ' end;',
  22028. 'end;',
  22029. 'var Rec: TRec;',
  22030. 'begin',
  22031. ' Rec.Foo;',
  22032. ' Rec.Foo();',
  22033. ' Rec.Foo(21);',
  22034. ' with Rec do begin',
  22035. ' Foo;',
  22036. ' Foo();',
  22037. ' Foo(22);',
  22038. ' end;',
  22039. '']);
  22040. ConvertProgram;
  22041. CheckSource('TestRecordHelper_Method_Call',
  22042. LinesToStr([ // statements
  22043. 'rtl.recNewT($mod, "TRec", function () {',
  22044. ' this.$eq = function (b) {',
  22045. ' return true;',
  22046. ' };',
  22047. ' this.$assign = function (s) {',
  22048. ' return this;',
  22049. ' };',
  22050. ' this.Run = function (w) {',
  22051. ' $mod.THelper.Foo.call(this, 1);',
  22052. ' $mod.THelper.Foo.call(this, 1);',
  22053. ' $mod.THelper.Foo.call(this, 2);',
  22054. ' $mod.THelper.Foo.call(this, 1);',
  22055. ' $mod.THelper.Foo.call(this, 1);',
  22056. ' $mod.THelper.Foo.call(this, 3);',
  22057. ' $mod.THelper.Foo.call(this, 1);',
  22058. ' $mod.THelper.Foo.call(this, 1);',
  22059. ' $mod.THelper.Foo.call(this, 4);',
  22060. ' };',
  22061. '});',
  22062. 'rtl.createHelper($mod, "THelper", null, function () {',
  22063. ' this.Foo = function (w) {',
  22064. ' var Result = 0;',
  22065. ' this.Run(10);',
  22066. ' this.Run(10);',
  22067. ' this.Run(11);',
  22068. ' $mod.THelper.Foo.call(this, 1);',
  22069. ' $mod.THelper.Foo.call(this, 1);',
  22070. ' $mod.THelper.Foo.call(this, 12);',
  22071. ' $mod.THelper.Foo.call(this, 1);',
  22072. ' $mod.THelper.Foo.call(this, 1);',
  22073. ' $mod.THelper.Foo.call(this, 13);',
  22074. ' $mod.THelper.Foo.call(this, 1);',
  22075. ' $mod.THelper.Foo.call(this, 1);',
  22076. ' $mod.THelper.Foo.call(this, 14);',
  22077. ' return Result;',
  22078. ' };',
  22079. '});',
  22080. 'this.Rec = $mod.TRec.$new();',
  22081. '']),
  22082. LinesToStr([ // $mod.$main
  22083. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22084. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22085. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22086. 'var $with1 = $mod.Rec;',
  22087. '$mod.THelper.Foo.call($with1, 1);',
  22088. '$mod.THelper.Foo.call($with1, 1);',
  22089. '$mod.THelper.Foo.call($with1, 22);',
  22090. '']));
  22091. end;
  22092. procedure TTestModule.TestRecordHelper_Constructor;
  22093. begin
  22094. StartProgram(false);
  22095. Add([
  22096. '{$modeswitch AdvancedRecords}',
  22097. 'type',
  22098. ' TRec = record',
  22099. ' constructor Create(w: word);',
  22100. ' end;',
  22101. ' THelper = record helper for TRec',
  22102. ' constructor NewHlp(w: word);',
  22103. ' end;',
  22104. 'var',
  22105. ' Rec: TRec;',
  22106. 'constructor TRec.Create(w: word);',
  22107. 'begin',
  22108. ' NewHlp(2);', // normal call
  22109. ' trec.NewHlp(3);', // new instance
  22110. 'end;',
  22111. 'constructor THelper.NewHlp(w: word);',
  22112. 'begin',
  22113. ' create(2);', // normal call
  22114. ' trec.create(3);', // new instance
  22115. ' NewHlp(4);', // normal call
  22116. ' trec.NewHlp(5);', // new instance
  22117. 'end;',
  22118. 'begin',
  22119. ' rec.newhlp(2);', // normal call
  22120. ' with rec do newhlp(12);', // normal call
  22121. ' trec.newhlp(3);', // new instance
  22122. ' with trec do newhlp(13);', // new instance
  22123. '']);
  22124. ConvertProgram;
  22125. CheckSource('TestRecordHelper_Constructor',
  22126. LinesToStr([ // statements
  22127. 'rtl.recNewT($mod, "TRec", function () {',
  22128. ' this.$eq = function (b) {',
  22129. ' return true;',
  22130. ' };',
  22131. ' this.$assign = function (s) {',
  22132. ' return this;',
  22133. ' };',
  22134. ' this.Create = function (w) {',
  22135. ' $mod.THelper.NewHlp.call(this, 2);',
  22136. ' $mod.THelper.$new("NewHlp", [3]);',
  22137. ' return this;',
  22138. ' };',
  22139. '}, true);',
  22140. 'rtl.createHelper($mod, "THelper", null, function () {',
  22141. ' this.NewHlp = function (w) {',
  22142. ' this.Create(2);',
  22143. ' $mod.TRec.$new().Create(3);',
  22144. ' $mod.THelper.NewHlp.call(this, 4);',
  22145. ' $mod.THelper.$new("NewHlp", [5]);',
  22146. ' return this;',
  22147. ' };',
  22148. ' this.$new = function (fn, args) {',
  22149. ' return this[fn].apply($mod.TRec.$new(), args);',
  22150. ' };',
  22151. '});',
  22152. 'this.Rec = $mod.TRec.$new();',
  22153. '']),
  22154. LinesToStr([ // $mod.$main
  22155. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22156. 'var $with1 = $mod.Rec;',
  22157. '$mod.THelper.NewHlp.call($with1, 12);',
  22158. '$mod.THelper.$new("NewHlp", [3]);',
  22159. 'var $with2 = $mod.TRec;',
  22160. '$mod.THelper.$new("NewHlp", [13]);',
  22161. '']));
  22162. end;
  22163. procedure TTestModule.TestTypeHelper_ClassVar;
  22164. begin
  22165. StartProgram(false);
  22166. Add([
  22167. '{$modeswitch typehelpers}',
  22168. 'type',
  22169. ' THelper = type helper for byte',
  22170. ' const',
  22171. ' One = 1;',
  22172. ' Two: word = 2;',
  22173. ' class var',
  22174. ' Glob: word;',
  22175. ' function Foo(w: word): word;',
  22176. ' class function Bar(w: word): word; static;',
  22177. ' end;',
  22178. 'function THelper.foo(w: word): word;',
  22179. 'begin',
  22180. ' Result:=w;',
  22181. ' Two:=One+w;',
  22182. ' Glob:=Glob;',
  22183. ' Result:=Self.Glob;',
  22184. ' Self.Glob:=Self.Glob;',
  22185. ' with Self do Glob:=Glob;',
  22186. 'end;',
  22187. 'class function THelper.bar(w: word): word;',
  22188. 'begin',
  22189. ' Result:=w;',
  22190. ' Two:=One;',
  22191. ' Glob:=Glob;',
  22192. 'end;',
  22193. 'var b: byte;',
  22194. 'begin',
  22195. ' byte.two:=byte.one;',
  22196. ' byte.Glob:=byte.Glob;',
  22197. ' with byte do begin',
  22198. ' two:=one;',
  22199. ' Glob:=Glob;',
  22200. ' end;',
  22201. ' b.two:=b.one;',
  22202. ' b.Glob:=b.Glob;',
  22203. ' with b do begin',
  22204. ' two:=one;',
  22205. ' Glob:=Glob;',
  22206. ' end;',
  22207. '']);
  22208. ConvertProgram;
  22209. CheckSource('TestTypeHelper_ClassVar',
  22210. LinesToStr([ // statements
  22211. 'rtl.createHelper($mod, "THelper", null, function () {',
  22212. ' this.One = 1;',
  22213. ' this.Two = 2;',
  22214. ' this.Glob = 0;',
  22215. ' this.Foo = function (w) {',
  22216. ' var Result = 0;',
  22217. ' Result = w;',
  22218. ' $mod.THelper.Two = 1 + w;',
  22219. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22220. ' Result = $mod.THelper.Glob;',
  22221. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22222. ' var $with1 = this.get();',
  22223. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22224. ' return Result;',
  22225. ' };',
  22226. ' this.Bar = function (w) {',
  22227. ' var Result = 0;',
  22228. ' Result = w;',
  22229. ' $mod.THelper.Two = 1;',
  22230. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22231. ' return Result;',
  22232. ' };',
  22233. '});',
  22234. 'this.b = 0;',
  22235. '']),
  22236. LinesToStr([ // $mod.$main
  22237. '$mod.THelper.Two = 1;',
  22238. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22239. '$mod.THelper.Two = 1;',
  22240. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22241. '$mod.THelper.Two = 1;',
  22242. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22243. 'var $with1 = $mod.b;',
  22244. '$mod.THelper.Two = 1;',
  22245. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22246. '']));
  22247. end;
  22248. procedure TTestModule.TestTypeHelper_PassResultElement;
  22249. begin
  22250. StartProgram(false);
  22251. Add([
  22252. '{$modeswitch typehelpers}',
  22253. 'type',
  22254. ' THelper = type helper for word',
  22255. ' procedure DoIt(e: byte = 123);',
  22256. ' class procedure DoSome(e: byte = 456); static;',
  22257. ' end;',
  22258. 'procedure THelper.DoIt(e: byte);',
  22259. 'begin',
  22260. 'end;',
  22261. 'class procedure THelper.DoSome(e: byte);',
  22262. 'begin',
  22263. 'end;',
  22264. 'function Foo(w: word): word;',
  22265. 'begin',
  22266. ' Result.DoIt;',
  22267. ' Result.DoIt();',
  22268. ' Result.DoSome;',
  22269. ' Result.DoSome();',
  22270. ' with Result do begin',
  22271. ' DoIt;',
  22272. ' DoIt();',
  22273. ' DoSome;',
  22274. ' DoSome();',
  22275. ' end;',
  22276. 'end;',
  22277. 'begin',
  22278. '']);
  22279. ConvertProgram;
  22280. CheckSource('TestTypeHelper_PassResultElement',
  22281. LinesToStr([ // statements
  22282. 'rtl.createHelper($mod, "THelper", null, function () {',
  22283. ' this.DoIt = function (e) {',
  22284. ' };',
  22285. ' this.DoSome = function (e) {',
  22286. ' };',
  22287. '});',
  22288. 'this.Foo = function (w) {',
  22289. ' var Result = 0;',
  22290. ' $mod.THelper.DoIt.call({',
  22291. ' get: function () {',
  22292. ' return Result;',
  22293. ' },',
  22294. ' set: function (v) {',
  22295. ' Result = v;',
  22296. ' }',
  22297. ' }, 123);',
  22298. ' $mod.THelper.DoIt.call({',
  22299. ' get: function () {',
  22300. ' return Result;',
  22301. ' },',
  22302. ' set: function (v) {',
  22303. ' Result = v;',
  22304. ' }',
  22305. ' }, 123);',
  22306. ' $mod.THelper.DoSome(456);',
  22307. ' $mod.THelper.DoSome(456);',
  22308. ' $mod.THelper.DoIt.call({',
  22309. ' get: function () {',
  22310. ' return Result;',
  22311. ' },',
  22312. ' set: function (v) {',
  22313. ' Result = v;',
  22314. ' }',
  22315. ' }, 123);',
  22316. ' $mod.THelper.DoIt.call({',
  22317. ' get: function () {',
  22318. ' return Result;',
  22319. ' },',
  22320. ' set: function (v) {',
  22321. ' Result = v;',
  22322. ' }',
  22323. ' }, 123);',
  22324. ' $mod.THelper.DoSome(456);',
  22325. ' $mod.THelper.DoSome(456);',
  22326. ' return Result;',
  22327. '};',
  22328. '']),
  22329. LinesToStr([ // $mod.$main
  22330. '']));
  22331. end;
  22332. procedure TTestModule.TestTypeHelper_PassArgs;
  22333. begin
  22334. StartProgram(false);
  22335. Add([
  22336. '{$modeswitch typehelpers}',
  22337. 'type',
  22338. ' THelper = type helper for word',
  22339. ' procedure DoIt(e: byte = 123);',
  22340. ' end;',
  22341. 'procedure THelper.DoIt(e: byte);',
  22342. 'begin',
  22343. 'end;',
  22344. 'procedure FooDefault(a: word);',
  22345. 'begin',
  22346. ' a.DoIt;',
  22347. ' with a do DoIt;',
  22348. 'end;',
  22349. 'procedure FooConst(const a: word);',
  22350. 'begin',
  22351. ' a.DoIt;',
  22352. ' with a do DoIt;',
  22353. 'end;',
  22354. 'procedure FooVar(var a: word);',
  22355. 'begin',
  22356. ' a.DoIt;',
  22357. ' with a do DoIt;',
  22358. 'end;',
  22359. 'begin',
  22360. '']);
  22361. ConvertProgram;
  22362. CheckSource('TestTypeHelper_PassArgs',
  22363. LinesToStr([ // statements
  22364. 'rtl.createHelper($mod, "THelper", null, function () {',
  22365. ' this.DoIt = function (e) {',
  22366. ' };',
  22367. '});',
  22368. 'this.FooDefault = function (a) {',
  22369. ' $mod.THelper.DoIt.call({',
  22370. ' get: function () {',
  22371. ' return a;',
  22372. ' },',
  22373. ' set: function (v) {',
  22374. ' a = v;',
  22375. ' }',
  22376. ' }, 123);',
  22377. ' $mod.THelper.DoIt.call({',
  22378. ' get: function () {',
  22379. ' return a;',
  22380. ' },',
  22381. ' set: function (v) {',
  22382. ' a = v;',
  22383. ' }',
  22384. ' }, 123);',
  22385. '};',
  22386. 'this.FooConst = function (a) {',
  22387. ' $mod.THelper.DoIt.call({',
  22388. ' get: function () {',
  22389. ' return a;',
  22390. ' },',
  22391. ' set: function (v) {',
  22392. ' rtl.raiseE("EPropReadOnly");',
  22393. ' }',
  22394. ' }, 123);',
  22395. ' $mod.THelper.DoIt.call({',
  22396. ' get: function () {',
  22397. ' return a;',
  22398. ' },',
  22399. ' set: function () {',
  22400. ' rtl.raiseE("EPropReadOnly");',
  22401. ' }',
  22402. ' }, 123);',
  22403. '};',
  22404. 'this.FooVar = function (a) {',
  22405. ' $mod.THelper.DoIt.call(a, 123);',
  22406. ' var $with1 = a.get();',
  22407. ' $mod.THelper.DoIt.call(a, 123);',
  22408. '};',
  22409. '']),
  22410. LinesToStr([ // $mod.$main
  22411. '']));
  22412. end;
  22413. procedure TTestModule.TestTypeHelper_PassVarConst;
  22414. begin
  22415. StartProgram(false);
  22416. Add([
  22417. '{$modeswitch typehelpers}',
  22418. 'type',
  22419. ' THelper = type helper for word',
  22420. ' procedure DoIt(e: byte = 123);',
  22421. ' end;',
  22422. 'procedure THelper.DoIt(e: byte);',
  22423. 'begin',
  22424. 'end;',
  22425. 'var a: word;',
  22426. 'const c: word = 2;',
  22427. '{$writeableconst off}',
  22428. 'const r: word = 3;',
  22429. 'begin',
  22430. ' a.DoIt;',
  22431. ' with a do DoIt;',
  22432. ' c.DoIt;',
  22433. ' with c do DoIt;',
  22434. ' r.DoIt;',
  22435. ' with r do DoIt;',
  22436. '']);
  22437. ConvertProgram;
  22438. CheckSource('TestTypeHelper_PassVarConst',
  22439. LinesToStr([ // statements
  22440. 'rtl.createHelper($mod, "THelper", null, function () {',
  22441. ' this.DoIt = function (e) {',
  22442. ' };',
  22443. '});',
  22444. 'this.a = 0;',
  22445. 'this.c = 2;',
  22446. 'this.r = 3;',
  22447. '']),
  22448. LinesToStr([ // $mod.$main
  22449. '$mod.THelper.DoIt.call({',
  22450. ' p: $mod,',
  22451. ' get: function () {',
  22452. ' return this.p.a;',
  22453. ' },',
  22454. ' set: function (v) {',
  22455. ' this.p.a = v;',
  22456. ' }',
  22457. '}, 123);',
  22458. 'var $with1 = $mod.a;',
  22459. '$mod.THelper.DoIt.call({',
  22460. ' get: function () {',
  22461. ' return $with1;',
  22462. ' },',
  22463. ' set: function (v) {',
  22464. ' $with1 = v;',
  22465. ' }',
  22466. '}, 123);',
  22467. '$mod.THelper.DoIt.call({',
  22468. ' p: $mod,',
  22469. ' get: function () {',
  22470. ' return this.p.c;',
  22471. ' },',
  22472. ' set: function (v) {',
  22473. ' this.p.c = v;',
  22474. ' }',
  22475. '}, 123);',
  22476. 'var $with2 = $mod.c;',
  22477. '$mod.THelper.DoIt.call({',
  22478. ' get: function () {',
  22479. ' return $with2;',
  22480. ' },',
  22481. ' set: function (v) {',
  22482. ' $with2 = v;',
  22483. ' }',
  22484. '}, 123);',
  22485. '$mod.THelper.DoIt.call({',
  22486. ' get: function () {',
  22487. ' return 3;',
  22488. ' },',
  22489. ' set: function (v) {',
  22490. ' rtl.raiseE("EPropReadOnly");',
  22491. ' }',
  22492. '}, 123);',
  22493. 'var $with3 = 3;',
  22494. ' $mod.THelper.DoIt.call({',
  22495. ' get: function () {',
  22496. ' return $with3;',
  22497. ' },',
  22498. ' set: function () {',
  22499. ' rtl.raiseE("EPropReadOnly");',
  22500. ' }',
  22501. ' }, 123);',
  22502. '']));
  22503. end;
  22504. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22505. begin
  22506. StartProgram(false);
  22507. Add([
  22508. '{$modeswitch typehelpers}',
  22509. 'type',
  22510. ' THelper = type helper for word',
  22511. ' procedure DoIt(e: byte = 123);',
  22512. ' end;',
  22513. 'procedure THelper.DoIt(e: byte);',
  22514. 'begin',
  22515. 'end;',
  22516. 'function Foo(b: byte = 1): word;',
  22517. 'begin',
  22518. 'end;',
  22519. 'begin',
  22520. ' Foo.DoIt;',
  22521. ' Foo().DoIt;',
  22522. ' with Foo do DoIt;',
  22523. ' with Foo() do DoIt;',
  22524. '']);
  22525. ConvertProgram;
  22526. CheckSource('TestTypeHelper_PassFuncResult',
  22527. LinesToStr([ // statements
  22528. 'rtl.createHelper($mod, "THelper", null, function () {',
  22529. ' this.DoIt = function (e) {',
  22530. ' };',
  22531. '});',
  22532. 'this.Foo = function (b) {',
  22533. ' var Result = 0;',
  22534. ' return Result;',
  22535. '};',
  22536. '']),
  22537. LinesToStr([ // $mod.$main
  22538. '$mod.THelper.DoIt.call({',
  22539. ' a: $mod.Foo(1),',
  22540. ' get: function () {',
  22541. ' return this.a;',
  22542. ' },',
  22543. ' set: function (v) {',
  22544. ' this.a = v;',
  22545. ' }',
  22546. '}, 123);',
  22547. '$mod.THelper.DoIt.call({',
  22548. ' a: $mod.Foo(1),',
  22549. ' get: function () {',
  22550. ' return this.a;',
  22551. ' },',
  22552. ' set: function (v) {',
  22553. ' this.a = v;',
  22554. ' }',
  22555. '}, 123);',
  22556. 'var $with1 = $mod.Foo(1);',
  22557. '$mod.THelper.DoIt.call({',
  22558. ' get: function () {',
  22559. ' return $with1;',
  22560. ' },',
  22561. ' set: function (v) {',
  22562. ' $with1 = v;',
  22563. ' }',
  22564. '}, 123);',
  22565. 'var $with2 = $mod.Foo(1);',
  22566. '$mod.THelper.DoIt.call({',
  22567. ' get: function () {',
  22568. ' return $with2;',
  22569. ' },',
  22570. ' set: function (v) {',
  22571. ' $with2 = v;',
  22572. ' }',
  22573. '}, 123);',
  22574. '']));
  22575. end;
  22576. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22577. begin
  22578. StartProgram(false);
  22579. Add([
  22580. '{$modeswitch typehelpers}',
  22581. 'type',
  22582. ' TObject = class',
  22583. ' FField: word;',
  22584. ' procedure SetField(Value: word);',
  22585. ' property Field: word read FField write SetField;',
  22586. ' end;',
  22587. ' THelper = type helper for word',
  22588. ' procedure Fly;',
  22589. ' class procedure Run; static;',
  22590. ' end;',
  22591. 'procedure TObject.SetField(Value: word);',
  22592. 'begin',
  22593. ' Field.Fly;',
  22594. ' Field.Run;',
  22595. ' Self.Field.Fly;',
  22596. ' Self.Field.Run;',
  22597. ' with Self do begin',
  22598. ' Field.Fly;',
  22599. ' Field.Run;',
  22600. ' end;',
  22601. ' with Self.Field do begin',
  22602. ' Fly;',
  22603. ' Run;',
  22604. ' end;',
  22605. 'end;',
  22606. 'procedure THelper.Fly;',
  22607. 'begin',
  22608. 'end;',
  22609. 'class procedure THelper.Run;',
  22610. 'begin',
  22611. 'end;',
  22612. 'var',
  22613. ' o: TObject;',
  22614. 'begin',
  22615. ' o.Field.Fly;',
  22616. ' o.Field.Run;',
  22617. ' with o do begin',
  22618. ' Field.Fly;',
  22619. ' Field.Run;',
  22620. ' end;',
  22621. ' with o.Field do begin',
  22622. ' Fly;',
  22623. ' Run;',
  22624. ' end;',
  22625. '']);
  22626. ConvertProgram;
  22627. CheckSource('TestTypeHelper_PassPropertyField',
  22628. LinesToStr([ // statements
  22629. 'rtl.createClass($mod, "TObject", null, function () {',
  22630. ' this.$init = function () {',
  22631. ' this.FField = 0;',
  22632. ' };',
  22633. ' this.$final = function () {',
  22634. ' };',
  22635. ' this.SetField = function (Value) {',
  22636. ' $mod.THelper.Fly.call({',
  22637. ' p: this,',
  22638. ' get: function () {',
  22639. ' return this.p.FField;',
  22640. ' },',
  22641. ' set: function (v) {',
  22642. ' this.p.FField = v;',
  22643. ' }',
  22644. ' });',
  22645. ' $mod.THelper.Run();',
  22646. ' $mod.THelper.Fly.call({',
  22647. ' p: this,',
  22648. ' get: function () {',
  22649. ' return this.p.FField;',
  22650. ' },',
  22651. ' set: function (v) {',
  22652. ' this.p.FField = v;',
  22653. ' }',
  22654. ' });',
  22655. ' $mod.THelper.Run();',
  22656. ' $mod.THelper.Fly.call({',
  22657. ' p: this,',
  22658. ' get: function () {',
  22659. ' return this.p.FField;',
  22660. ' },',
  22661. ' set: function (v) {',
  22662. ' this.p.FField = v;',
  22663. ' }',
  22664. ' });',
  22665. ' $mod.THelper.Run();',
  22666. ' var $with1 = this.FField;',
  22667. ' $mod.THelper.Fly.call({',
  22668. ' get: function () {',
  22669. ' return $with1;',
  22670. ' },',
  22671. ' set: function (v) {',
  22672. ' $with1 = v;',
  22673. ' }',
  22674. ' });',
  22675. ' $mod.THelper.Run();',
  22676. ' };',
  22677. '});',
  22678. 'rtl.createHelper($mod, "THelper", null, function () {',
  22679. ' this.Fly = function () {',
  22680. ' };',
  22681. ' this.Run = function () {',
  22682. ' };',
  22683. '});',
  22684. 'this.o = null;',
  22685. '']),
  22686. LinesToStr([ // $mod.$main
  22687. '$mod.THelper.Fly.call({',
  22688. ' p: $mod.o,',
  22689. ' get: function () {',
  22690. ' return this.p.FField;',
  22691. ' },',
  22692. ' set: function (v) {',
  22693. ' this.p.FField = v;',
  22694. ' }',
  22695. '});',
  22696. '$mod.THelper.Run();',
  22697. 'var $with1 = $mod.o;',
  22698. '$mod.THelper.Fly.call({',
  22699. ' p: $with1,',
  22700. ' get: function () {',
  22701. ' return this.p.FField;',
  22702. ' },',
  22703. ' set: function (v) {',
  22704. ' this.p.FField = v;',
  22705. ' }',
  22706. '});',
  22707. '$mod.THelper.Run();',
  22708. 'var $with2 = $mod.o.FField;',
  22709. '$mod.THelper.Fly.call({',
  22710. ' get: function () {',
  22711. ' return $with2;',
  22712. ' },',
  22713. ' set: function (v) {',
  22714. ' $with2 = v;',
  22715. ' }',
  22716. '});',
  22717. '$mod.THelper.Run();',
  22718. '']));
  22719. end;
  22720. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22721. begin
  22722. StartProgram(false);
  22723. Add([
  22724. '{$modeswitch typehelpers}',
  22725. 'type',
  22726. ' TObject = class',
  22727. ' FField: word;',
  22728. ' function GetField: word;',
  22729. ' property Field: word read GetField write FField;',
  22730. ' end;',
  22731. ' THelper = type helper for word',
  22732. ' procedure Fly;',
  22733. ' class procedure Run; static;',
  22734. ' end;',
  22735. 'function TObject.GetField: word;',
  22736. 'begin',
  22737. ' Field.Fly;',
  22738. ' Field.Run;',
  22739. ' Self.Field.Fly;',
  22740. ' Self.Field.Run;',
  22741. ' with Self do begin',
  22742. ' Field.Fly;',
  22743. ' Field.Run;',
  22744. ' end;',
  22745. ' with Self.Field do begin',
  22746. ' Fly;',
  22747. ' Run;',
  22748. ' end;',
  22749. 'end;',
  22750. 'procedure THelper.Fly;',
  22751. 'begin',
  22752. 'end;',
  22753. 'class procedure THelper.Run;',
  22754. 'begin',
  22755. 'end;',
  22756. 'var',
  22757. ' o: TObject;',
  22758. 'begin',
  22759. ' o.Field.Fly;',
  22760. ' o.Field.Run;',
  22761. ' with o do begin',
  22762. ' Field.Fly;',
  22763. ' Field.Run;',
  22764. ' end;',
  22765. ' with o.Field do begin',
  22766. ' Fly;',
  22767. ' Run;',
  22768. ' end;',
  22769. '']);
  22770. ConvertProgram;
  22771. CheckSource('TestTypeHelper_PassPropertyGetter',
  22772. LinesToStr([ // statements
  22773. 'rtl.createClass($mod, "TObject", null, function () {',
  22774. ' this.$init = function () {',
  22775. ' this.FField = 0;',
  22776. ' };',
  22777. ' this.$final = function () {',
  22778. ' };',
  22779. ' this.GetField = function () {',
  22780. ' var Result = 0;',
  22781. ' $mod.THelper.Fly.call({',
  22782. ' p: this.GetField(),',
  22783. ' get: function () {',
  22784. ' return this.p;',
  22785. ' },',
  22786. ' set: function (v) {',
  22787. ' this.p = v;',
  22788. ' }',
  22789. ' });',
  22790. ' $mod.THelper.Run();',
  22791. ' $mod.THelper.Fly.call({',
  22792. ' p: this.GetField(),',
  22793. ' get: function () {',
  22794. ' return this.p;',
  22795. ' },',
  22796. ' set: function (v) {',
  22797. ' this.p = v;',
  22798. ' }',
  22799. ' });',
  22800. ' $mod.THelper.Run();',
  22801. ' $mod.THelper.Fly.call({',
  22802. ' p: this.GetField(),',
  22803. ' get: function () {',
  22804. ' return this.p;',
  22805. ' },',
  22806. ' set: function (v) {',
  22807. ' this.p = v;',
  22808. ' }',
  22809. ' });',
  22810. ' $mod.THelper.Run();',
  22811. ' var $with1 = this.GetField();',
  22812. ' $mod.THelper.Fly.call({',
  22813. ' get: function () {',
  22814. ' return $with1;',
  22815. ' },',
  22816. ' set: function (v) {',
  22817. ' $with1 = v;',
  22818. ' }',
  22819. ' });',
  22820. ' $mod.THelper.Run();',
  22821. ' return Result;',
  22822. ' };',
  22823. '});',
  22824. 'rtl.createHelper($mod, "THelper", null, function () {',
  22825. ' this.Fly = function () {',
  22826. ' };',
  22827. ' this.Run = function () {',
  22828. ' };',
  22829. '});',
  22830. 'this.o = null;',
  22831. '']),
  22832. LinesToStr([ // $mod.$main
  22833. '$mod.THelper.Fly.call({',
  22834. ' p: $mod.o.GetField(),',
  22835. ' get: function () {',
  22836. ' return this.p;',
  22837. ' },',
  22838. ' set: function (v) {',
  22839. ' this.p = v;',
  22840. ' }',
  22841. '});',
  22842. '$mod.THelper.Run();',
  22843. 'var $with1 = $mod.o;',
  22844. '$mod.THelper.Fly.call({',
  22845. ' p: $with1.GetField(),',
  22846. ' get: function () {',
  22847. ' return this.p;',
  22848. ' },',
  22849. ' set: function (v) {',
  22850. ' this.p = v;',
  22851. ' }',
  22852. '});',
  22853. '$mod.THelper.Run();',
  22854. 'var $with2 = $mod.o.GetField();',
  22855. '$mod.THelper.Fly.call({',
  22856. ' get: function () {',
  22857. ' return $with2;',
  22858. ' },',
  22859. ' set: function (v) {',
  22860. ' $with2 = v;',
  22861. ' }',
  22862. '});',
  22863. '$mod.THelper.Run();',
  22864. '']));
  22865. end;
  22866. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22867. begin
  22868. StartProgram(false);
  22869. Add([
  22870. '{$modeswitch typehelpers}',
  22871. 'type',
  22872. ' TObject = class',
  22873. ' class var FField: word;',
  22874. ' class procedure SetField(Value: word);',
  22875. ' class property Field: word read FField write SetField;',
  22876. ' end;',
  22877. ' THelper = type helper for word',
  22878. ' procedure Fly(n: byte);',
  22879. ' end;',
  22880. 'class procedure TObject.SetField(Value: word);',
  22881. 'begin',
  22882. ' Field.Fly(1);',
  22883. ' Self.Field.Fly(2);',
  22884. ' with Self do Field.Fly(3);',
  22885. ' with Self.Field do Fly(4);',
  22886. ' TObject.Field.Fly(5);',
  22887. ' with TObject do Field.Fly(6);',
  22888. ' with TObject.Field do Fly(7);',
  22889. 'end;',
  22890. 'procedure THelper.Fly(n: byte);',
  22891. 'begin',
  22892. 'end;',
  22893. 'var',
  22894. ' o: TObject;',
  22895. 'begin',
  22896. ' o.Field.Fly(11);',
  22897. ' with o do Field.Fly(12);',
  22898. ' with o.Field do Fly(13);',
  22899. ' TObject.Field.Fly(14);',
  22900. ' with TObject do Field.Fly(15);',
  22901. ' with TObject.Field do Fly(16);',
  22902. '']);
  22903. ConvertProgram;
  22904. CheckSource('TestTypeHelper_PassClassPropertyField',
  22905. LinesToStr([ // statements
  22906. 'rtl.createClass($mod, "TObject", null, function () {',
  22907. ' this.FField = 0;',
  22908. ' this.$init = function () {',
  22909. ' };',
  22910. ' this.$final = function () {',
  22911. ' };',
  22912. ' this.SetField = function (Value) {',
  22913. ' $mod.THelper.Fly.call({',
  22914. ' p: this,',
  22915. ' get: function () {',
  22916. ' return this.p.FField;',
  22917. ' },',
  22918. ' set: function (v) {',
  22919. ' $mod.TObject.FField = v;',
  22920. ' }',
  22921. ' }, 1);',
  22922. ' $mod.THelper.Fly.call({',
  22923. ' p: this,',
  22924. ' get: function () {',
  22925. ' return this.p.FField;',
  22926. ' },',
  22927. ' set: function (v) {',
  22928. ' $mod.TObject.FField = v;',
  22929. ' }',
  22930. ' }, 2);',
  22931. ' $mod.THelper.Fly.call({',
  22932. ' p: this,',
  22933. ' get: function () {',
  22934. ' return this.p.FField;',
  22935. ' },',
  22936. ' set: function (v) {',
  22937. ' $mod.TObject.FField = v;',
  22938. ' }',
  22939. ' }, 3);',
  22940. ' var $with1 = this.FField;',
  22941. ' $mod.THelper.Fly.call({',
  22942. ' get: function () {',
  22943. ' return $with1;',
  22944. ' },',
  22945. ' set: function (v) {',
  22946. ' $with1 = v;',
  22947. ' }',
  22948. ' }, 4);',
  22949. ' $mod.THelper.Fly.call({',
  22950. ' p: $mod.TObject,',
  22951. ' get: function () {',
  22952. ' return this.p.FField;',
  22953. ' },',
  22954. ' set: function (v) {',
  22955. ' $mod.TObject.FField = v;',
  22956. ' }',
  22957. ' }, 5);',
  22958. ' var $with2 = $mod.TObject;',
  22959. ' $mod.THelper.Fly.call({',
  22960. ' p: $with2,',
  22961. ' get: function () {',
  22962. ' return this.p.FField;',
  22963. ' },',
  22964. ' set: function (v) {',
  22965. ' $mod.TObject.FField = v;',
  22966. ' }',
  22967. ' }, 6);',
  22968. ' var $with3 = $mod.TObject.FField;',
  22969. ' $mod.THelper.Fly.call({',
  22970. ' get: function () {',
  22971. ' return $with3;',
  22972. ' },',
  22973. ' set: function (v) {',
  22974. ' $with3 = v;',
  22975. ' }',
  22976. ' }, 7);',
  22977. ' };',
  22978. '});',
  22979. 'rtl.createHelper($mod, "THelper", null, function () {',
  22980. ' this.Fly = function (n) {',
  22981. ' };',
  22982. '});',
  22983. 'this.o = null;',
  22984. '']),
  22985. LinesToStr([ // $mod.$main
  22986. '$mod.THelper.Fly.call({',
  22987. ' p: $mod.o,',
  22988. ' get: function () {',
  22989. ' return this.p.FField;',
  22990. ' },',
  22991. ' set: function (v) {',
  22992. ' $mod.TObject.FField = v;',
  22993. ' }',
  22994. '}, 11);',
  22995. 'var $with1 = $mod.o;',
  22996. '$mod.THelper.Fly.call({',
  22997. ' p: $with1,',
  22998. ' get: function () {',
  22999. ' return this.p.FField;',
  23000. ' },',
  23001. ' set: function (v) {',
  23002. ' $mod.TObject.FField = v;',
  23003. ' }',
  23004. '}, 12);',
  23005. 'var $with2 = $mod.o.FField;',
  23006. '$mod.THelper.Fly.call({',
  23007. ' get: function () {',
  23008. ' return $with2;',
  23009. ' },',
  23010. ' set: function (v) {',
  23011. ' $with2 = v;',
  23012. ' }',
  23013. '}, 13);',
  23014. '$mod.THelper.Fly.call({',
  23015. ' p: $mod.TObject,',
  23016. ' get: function () {',
  23017. ' return this.p.FField;',
  23018. ' },',
  23019. ' set: function (v) {',
  23020. ' $mod.TObject.FField = v;',
  23021. ' }',
  23022. '}, 14);',
  23023. 'var $with3 = $mod.TObject;',
  23024. '$mod.THelper.Fly.call({',
  23025. ' p: $with3,',
  23026. ' get: function () {',
  23027. ' return this.p.FField;',
  23028. ' },',
  23029. ' set: function (v) {',
  23030. ' $mod.TObject.FField = v;',
  23031. ' }',
  23032. '}, 15);',
  23033. 'var $with4 = $mod.TObject.FField;',
  23034. '$mod.THelper.Fly.call({',
  23035. ' get: function () {',
  23036. ' return $with4;',
  23037. ' },',
  23038. ' set: function (v) {',
  23039. ' $with4 = v;',
  23040. ' }',
  23041. '}, 16);',
  23042. '']));
  23043. end;
  23044. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23045. begin
  23046. StartProgram(false);
  23047. Add([
  23048. '{$modeswitch typehelpers}',
  23049. 'type',
  23050. ' TObject = class',
  23051. ' class var FField: word;',
  23052. ' class function GetField: word; static;',
  23053. ' class property Field: word read GetField write FField;',
  23054. ' end;',
  23055. ' THelper = type helper for word',
  23056. ' procedure Fly(n: byte);',
  23057. ' end;',
  23058. 'class function TObject.GetField: word;',
  23059. 'begin',
  23060. ' Field.Fly(1);',
  23061. ' TObject.Field.Fly(5);',
  23062. ' with TObject do Field.Fly(6);',
  23063. ' with TObject.Field do Fly(7);',
  23064. 'end;',
  23065. 'procedure THelper.Fly(n: byte);',
  23066. 'begin',
  23067. 'end;',
  23068. 'var',
  23069. ' o: TObject;',
  23070. 'begin',
  23071. ' o.Field.Fly(11);',
  23072. ' with o do Field.Fly(12);',
  23073. ' with o.Field do Fly(13);',
  23074. '']);
  23075. ConvertProgram;
  23076. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23077. LinesToStr([ // statements
  23078. 'rtl.createClass($mod, "TObject", null, function () {',
  23079. ' this.FField = 0;',
  23080. ' this.$init = function () {',
  23081. ' };',
  23082. ' this.$final = function () {',
  23083. ' };',
  23084. ' this.GetField = function () {',
  23085. ' var Result = 0;',
  23086. ' $mod.THelper.Fly.call({',
  23087. ' p: $mod.TObject.GetField(),',
  23088. ' get: function () {',
  23089. ' return this.p;',
  23090. ' },',
  23091. ' set: function (v) {',
  23092. ' this.p = v;',
  23093. ' }',
  23094. ' }, 1);',
  23095. ' $mod.THelper.Fly.call({',
  23096. ' p: $mod.TObject.GetField(),',
  23097. ' get: function () {',
  23098. ' return this.p;',
  23099. ' },',
  23100. ' set: function (v) {',
  23101. ' this.p = v;',
  23102. ' }',
  23103. ' }, 5);',
  23104. ' var $with1 = $mod.TObject;',
  23105. ' $mod.THelper.Fly.call({',
  23106. ' p: $with1.GetField(),',
  23107. ' get: function () {',
  23108. ' return this.p;',
  23109. ' },',
  23110. ' set: function (v) {',
  23111. ' this.p = v;',
  23112. ' }',
  23113. ' }, 6);',
  23114. ' var $with2 = $mod.TObject.GetField();',
  23115. ' $mod.THelper.Fly.call({',
  23116. ' get: function () {',
  23117. ' return $with2;',
  23118. ' },',
  23119. ' set: function (v) {',
  23120. ' $with2 = v;',
  23121. ' }',
  23122. ' }, 7);',
  23123. ' return Result;',
  23124. ' };',
  23125. '});',
  23126. 'rtl.createHelper($mod, "THelper", null, function () {',
  23127. ' this.Fly = function (n) {',
  23128. ' };',
  23129. '});',
  23130. 'this.o = null;',
  23131. '']),
  23132. LinesToStr([ // $mod.$main
  23133. '$mod.THelper.Fly.call({',
  23134. ' p: $mod.o.GetField(),',
  23135. ' get: function () {',
  23136. ' return this.p;',
  23137. ' },',
  23138. ' set: function (v) {',
  23139. ' this.p = v;',
  23140. ' }',
  23141. '}, 11);',
  23142. 'var $with1 = $mod.o;',
  23143. '$mod.THelper.Fly.call({',
  23144. ' p: $with1.GetField(),',
  23145. ' get: function () {',
  23146. ' return this.p;',
  23147. ' },',
  23148. ' set: function (v) {',
  23149. ' this.p = v;',
  23150. ' }',
  23151. '}, 12);',
  23152. 'var $with2 = $mod.o.GetField();',
  23153. '$mod.THelper.Fly.call({',
  23154. ' get: function () {',
  23155. ' return $with2;',
  23156. ' },',
  23157. ' set: function (v) {',
  23158. ' $with2 = v;',
  23159. ' }',
  23160. '}, 13);',
  23161. '']));
  23162. end;
  23163. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23164. begin
  23165. StartProgram(false);
  23166. Add([
  23167. '{$modeswitch typehelpers}',
  23168. 'type',
  23169. ' TObject = class',
  23170. ' class var FField: word;',
  23171. ' class function GetField: word;',
  23172. ' class property Field: word read GetField write FField;',
  23173. ' end;',
  23174. ' TClass = class of TObject;',
  23175. ' THelper = type helper for word',
  23176. ' procedure Fly(n: byte);',
  23177. ' end;',
  23178. 'class function TObject.GetField: word;',
  23179. 'begin',
  23180. ' Field.Fly(1);',
  23181. ' Self.Field.Fly(5);',
  23182. ' with Self do Field.Fly(6);',
  23183. ' with Self.Field do Fly(7);',
  23184. 'end;',
  23185. 'procedure THelper.Fly(n: byte);',
  23186. 'begin',
  23187. 'end;',
  23188. 'var',
  23189. ' o: TObject;',
  23190. ' c: TClass;',
  23191. 'begin',
  23192. ' o.Field.Fly(11);',
  23193. ' with o do Field.Fly(12);',
  23194. ' with o.Field do Fly(13);',
  23195. ' c.Field.Fly(14);',
  23196. ' with c do Field.Fly(15);',
  23197. ' with c.Field do Fly(16);',
  23198. '']);
  23199. ConvertProgram;
  23200. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23201. LinesToStr([ // statements
  23202. 'rtl.createClass($mod, "TObject", null, function () {',
  23203. ' this.FField = 0;',
  23204. ' this.$init = function () {',
  23205. ' };',
  23206. ' this.$final = function () {',
  23207. ' };',
  23208. ' this.GetField = function () {',
  23209. ' var Result = 0;',
  23210. ' $mod.THelper.Fly.call({',
  23211. ' p: this.GetField(),',
  23212. ' get: function () {',
  23213. ' return this.p;',
  23214. ' },',
  23215. ' set: function (v) {',
  23216. ' this.p = v;',
  23217. ' }',
  23218. ' }, 1);',
  23219. ' $mod.THelper.Fly.call({',
  23220. ' p: this.GetField(),',
  23221. ' get: function () {',
  23222. ' return this.p;',
  23223. ' },',
  23224. ' set: function (v) {',
  23225. ' this.p = v;',
  23226. ' }',
  23227. ' }, 5);',
  23228. ' $mod.THelper.Fly.call({',
  23229. ' p: this.GetField(),',
  23230. ' get: function () {',
  23231. ' return this.p;',
  23232. ' },',
  23233. ' set: function (v) {',
  23234. ' this.p = v;',
  23235. ' }',
  23236. ' }, 6);',
  23237. ' var $with1 = this.GetField();',
  23238. ' $mod.THelper.Fly.call({',
  23239. ' get: function () {',
  23240. ' return $with1;',
  23241. ' },',
  23242. ' set: function (v) {',
  23243. ' $with1 = v;',
  23244. ' }',
  23245. ' }, 7);',
  23246. ' return Result;',
  23247. ' };',
  23248. '});',
  23249. 'rtl.createHelper($mod, "THelper", null, function () {',
  23250. ' this.Fly = function (n) {',
  23251. ' };',
  23252. '});',
  23253. 'this.o = null;',
  23254. 'this.c = null;',
  23255. '']),
  23256. LinesToStr([ // $mod.$main
  23257. '$mod.THelper.Fly.call({',
  23258. ' p: $mod.o.$class.GetField(),',
  23259. ' get: function () {',
  23260. ' return this.p;',
  23261. ' },',
  23262. ' set: function (v) {',
  23263. ' this.p = v;',
  23264. ' }',
  23265. '}, 11);',
  23266. 'var $with1 = $mod.o;',
  23267. '$mod.THelper.Fly.call({',
  23268. ' p: $with1.$class.GetField(),',
  23269. ' get: function () {',
  23270. ' return this.p;',
  23271. ' },',
  23272. ' set: function (v) {',
  23273. ' this.p = v;',
  23274. ' }',
  23275. '}, 12);',
  23276. 'var $with2 = $mod.o.$class.GetField();',
  23277. '$mod.THelper.Fly.call({',
  23278. ' get: function () {',
  23279. ' return $with2;',
  23280. ' },',
  23281. ' set: function (v) {',
  23282. ' $with2 = v;',
  23283. ' }',
  23284. '}, 13);',
  23285. '$mod.THelper.Fly.call({',
  23286. ' p: $mod.c.GetField(),',
  23287. ' get: function () {',
  23288. ' return this.p;',
  23289. ' },',
  23290. ' set: function (v) {',
  23291. ' this.p = v;',
  23292. ' }',
  23293. '}, 14);',
  23294. 'var $with3 = $mod.c;',
  23295. '$mod.THelper.Fly.call({',
  23296. ' p: $with3.GetField(),',
  23297. ' get: function () {',
  23298. ' return this.p;',
  23299. ' },',
  23300. ' set: function (v) {',
  23301. ' this.p = v;',
  23302. ' }',
  23303. '}, 15);',
  23304. 'var $with4 = $mod.c.GetField();',
  23305. '$mod.THelper.Fly.call({',
  23306. ' get: function () {',
  23307. ' return $with4;',
  23308. ' },',
  23309. ' set: function (v) {',
  23310. ' $with4 = v;',
  23311. ' }',
  23312. '}, 16);',
  23313. '']));
  23314. end;
  23315. procedure TTestModule.TestTypeHelper_Property;
  23316. begin
  23317. StartProgram(false);
  23318. Add([
  23319. '{$modeswitch typehelpers}',
  23320. 'type',
  23321. ' THelper = type helper for word',
  23322. ' function GetSize: longint;',
  23323. ' procedure SetSize(Value: longint);',
  23324. ' property Size: longint read GetSize write SetSize;',
  23325. ' end;',
  23326. 'function THelper.GetSize: longint;',
  23327. 'begin',
  23328. ' Result:=Size+1;',
  23329. ' Size:=2;',
  23330. ' Result:=Self.Size+3;',
  23331. ' Self.Size:=4;',
  23332. ' with Self do begin',
  23333. ' Result:=Size+5;',
  23334. ' Size:=6;',
  23335. ' end;',
  23336. 'end;',
  23337. 'procedure THelper.SetSize(Value: longint);',
  23338. 'begin',
  23339. 'end;',
  23340. 'var w: word;',
  23341. 'begin',
  23342. ' w:=w.Size+7;',
  23343. ' w.Size:=w+8;',
  23344. ' with w do begin',
  23345. ' w:=Size+9;',
  23346. ' Size:=w+10;',
  23347. ' end;',
  23348. '']);
  23349. ConvertProgram;
  23350. CheckSource('TestTypeHelper_Property',
  23351. LinesToStr([ // statements
  23352. 'rtl.createHelper($mod, "THelper", null, function () {',
  23353. ' this.GetSize = function () {',
  23354. ' var Result = 0;',
  23355. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23356. ' $mod.THelper.SetSize.call(this, 2);',
  23357. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23358. ' $mod.THelper.SetSize.call(this, 4);',
  23359. ' var $with1 = this.get();',
  23360. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23361. ' $mod.THelper.SetSize.call(this, 6);',
  23362. ' return Result;',
  23363. ' };',
  23364. ' this.SetSize = function (Value) {',
  23365. ' };',
  23366. '});',
  23367. 'this.w = 0;',
  23368. '']),
  23369. LinesToStr([ // $mod.$main
  23370. '$mod.w = $mod.THelper.GetSize.call({',
  23371. ' p: $mod,',
  23372. ' get: function () {',
  23373. ' return this.p.w;',
  23374. ' },',
  23375. ' set: function (v) {',
  23376. ' this.p.w = v;',
  23377. ' }',
  23378. '}) + 7;',
  23379. '$mod.THelper.SetSize.call({',
  23380. ' p: $mod,',
  23381. ' get: function () {',
  23382. ' return this.p.w;',
  23383. ' },',
  23384. ' set: function (v) {',
  23385. ' this.p.w = v;',
  23386. ' }',
  23387. '}, $mod.w + 8);',
  23388. 'var $with1 = $mod.w;',
  23389. '$mod.w = $mod.THelper.GetSize.call({',
  23390. ' get: function () {',
  23391. ' return $with1;',
  23392. ' },',
  23393. ' set: function (v) {',
  23394. ' $with1 = v;',
  23395. ' }',
  23396. '}) + 9;',
  23397. '$mod.THelper.SetSize.call({',
  23398. ' get: function () {',
  23399. ' return $with1;',
  23400. ' },',
  23401. ' set: function (v) {',
  23402. ' $with1 = v;',
  23403. ' }',
  23404. '}, $mod.w + 10);',
  23405. '']));
  23406. end;
  23407. procedure TTestModule.TestTypeHelper_Property_Array;
  23408. begin
  23409. StartProgram(false);
  23410. Add([
  23411. '{$modeswitch typehelpers}',
  23412. 'type',
  23413. ' THelper = type helper for word',
  23414. ' function GetItems(Index: byte): boolean;',
  23415. ' procedure SetItems(Index: byte; Value: boolean);',
  23416. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23417. ' end;',
  23418. 'function THelper.GetItems(Index: byte): boolean;',
  23419. 'begin',
  23420. ' Result:=Items[1];',
  23421. ' Items[2]:=false;',
  23422. ' Result:=Self.Items[3];',
  23423. ' Self.Items[4]:=true;',
  23424. ' with Self do begin',
  23425. ' Result:=Items[5];',
  23426. ' Items[6]:=false;',
  23427. ' end;',
  23428. 'end;',
  23429. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23430. 'begin',
  23431. 'end;',
  23432. 'var',
  23433. ' w: word;',
  23434. ' b: boolean;',
  23435. 'begin',
  23436. ' b:=w.Items[1];',
  23437. ' w.Items[2]:=b;',
  23438. ' with w do begin',
  23439. ' b:=Items[3];',
  23440. ' Items[4]:=b;',
  23441. ' end;',
  23442. '']);
  23443. ConvertProgram;
  23444. CheckSource('TestTypeHelper_Property_Array',
  23445. LinesToStr([ // statements
  23446. 'rtl.createHelper($mod, "THelper", null, function () {',
  23447. ' this.GetItems = function (Index) {',
  23448. ' var Result = false;',
  23449. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23450. ' $mod.THelper.SetItems.call(this, 2, false);',
  23451. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23452. ' $mod.THelper.SetItems.call(this, 4, true);',
  23453. ' var $with1 = this.get();',
  23454. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23455. ' $mod.THelper.SetItems.call(this, 6, false);',
  23456. ' return Result;',
  23457. ' };',
  23458. ' this.SetItems = function (Index, Value) {',
  23459. ' };',
  23460. '});',
  23461. 'this.w = 0;',
  23462. 'this.b = false;',
  23463. '']),
  23464. LinesToStr([ // $mod.$main
  23465. '$mod.b = $mod.THelper.GetItems.call({',
  23466. ' p: $mod,',
  23467. ' get: function () {',
  23468. ' return this.p.w;',
  23469. ' },',
  23470. ' set: function (v) {',
  23471. ' this.p.w = v;',
  23472. ' }',
  23473. '}, 1);',
  23474. '$mod.THelper.SetItems.call({',
  23475. ' p: $mod,',
  23476. ' get: function () {',
  23477. ' return this.p.w;',
  23478. ' },',
  23479. ' set: function (v) {',
  23480. ' this.p.w = v;',
  23481. ' }',
  23482. '}, 2, $mod.b);',
  23483. 'var $with1 = $mod.w;',
  23484. '$mod.b = $mod.THelper.GetItems.call({',
  23485. ' get: function () {',
  23486. ' return $with1;',
  23487. ' },',
  23488. ' set: function (v) {',
  23489. ' $with1 = v;',
  23490. ' }',
  23491. '}, 3);',
  23492. '$mod.THelper.SetItems.call({',
  23493. ' get: function () {',
  23494. ' return $with1;',
  23495. ' },',
  23496. ' set: function (v) {',
  23497. ' $with1 = v;',
  23498. ' }',
  23499. '}, 4, $mod.b);',
  23500. '']));
  23501. end;
  23502. procedure TTestModule.TestTypeHelper_ClassProperty;
  23503. begin
  23504. StartProgram(false);
  23505. Add([
  23506. '{$modeswitch typehelpers}',
  23507. 'type',
  23508. ' THelper = type helper for word',
  23509. ' class function GetSize: longint; static;',
  23510. ' class procedure SetSize(Value: longint); static;',
  23511. ' class property Size: longint read GetSize write SetSize;',
  23512. ' end;',
  23513. 'class function THelper.GetSize: longint;',
  23514. 'begin',
  23515. ' Result:=Size+1;',
  23516. ' Size:=2;',
  23517. 'end;',
  23518. 'class procedure THelper.SetSize(Value: longint);',
  23519. 'begin',
  23520. 'end;',
  23521. 'begin',
  23522. '']);
  23523. ConvertProgram;
  23524. CheckSource('TestTypeHelper_ClassProperty',
  23525. LinesToStr([ // statements
  23526. 'rtl.createHelper($mod, "THelper", null, function () {',
  23527. ' this.GetSize = function () {',
  23528. ' var Result = 0;',
  23529. ' Result = $mod.THelper.GetSize() + 1;',
  23530. ' $mod.THelper.SetSize(2);',
  23531. ' return Result;',
  23532. ' };',
  23533. ' this.SetSize = function (Value) {',
  23534. ' };',
  23535. '});',
  23536. '']),
  23537. LinesToStr([ // $mod.$main
  23538. '']));
  23539. end;
  23540. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23541. begin
  23542. StartProgram(false);
  23543. Add([
  23544. '{$modeswitch typehelpers}',
  23545. 'type',
  23546. ' THelper = type helper for word',
  23547. ' class function GetItems(Index: byte): boolean; static;',
  23548. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23549. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23550. ' end;',
  23551. 'class function THelper.GetItems(Index: byte): boolean;',
  23552. 'begin',
  23553. ' Result:=Items[1];',
  23554. ' Items[2]:=false;',
  23555. 'end;',
  23556. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23557. 'begin',
  23558. 'end;',
  23559. 'var',
  23560. ' w: word;',
  23561. ' b: boolean;',
  23562. 'begin',
  23563. ' b:=w.Items[1];',
  23564. ' w.Items[2]:=b;',
  23565. ' with w do begin',
  23566. ' b:=Items[3];',
  23567. ' Items[4]:=b;',
  23568. ' end;',
  23569. '']);
  23570. ConvertProgram;
  23571. CheckSource('TestTypeHelper_ClassProperty_Array',
  23572. LinesToStr([ // statements
  23573. 'rtl.createHelper($mod, "THelper", null, function () {',
  23574. ' this.GetItems = function (Index) {',
  23575. ' var Result = false;',
  23576. ' Result = $mod.THelper.GetItems(1);',
  23577. ' $mod.THelper.SetItems(2, false);',
  23578. ' return Result;',
  23579. ' };',
  23580. ' this.SetItems = function (Index, Value) {',
  23581. ' };',
  23582. '});',
  23583. 'this.w = 0;',
  23584. 'this.b = false;',
  23585. '']),
  23586. LinesToStr([ // $mod.$main
  23587. '$mod.b = $mod.THelper.GetItems(1);',
  23588. '$mod.THelper.SetItems(2, $mod.b);',
  23589. 'var $with1 = $mod.w;',
  23590. '$mod.b = $mod.THelper.GetItems(3);',
  23591. '$mod.THelper.SetItems(4, $mod.b);',
  23592. '']));
  23593. end;
  23594. procedure TTestModule.TestTypeHelper_ClassMethod;
  23595. begin
  23596. StartProgram(false);
  23597. Add([
  23598. '{$modeswitch typehelpers}',
  23599. 'type',
  23600. ' THelper = type helper for word',
  23601. ' class procedure DoStatic; static;',
  23602. ' end;',
  23603. 'class procedure THelper.DoStatic;',
  23604. 'begin',
  23605. ' DoStatic;',
  23606. ' DoStatic();',
  23607. 'end;',
  23608. 'var w: word;',
  23609. 'begin',
  23610. ' w.DoStatic;',
  23611. ' w.DoStatic();',
  23612. '']);
  23613. ConvertProgram;
  23614. CheckSource('TestTypeHelper_ClassMethod',
  23615. LinesToStr([ // statements
  23616. 'rtl.createHelper($mod, "THelper", null, function () {',
  23617. ' this.DoStatic = function () {',
  23618. ' $mod.THelper.DoStatic();',
  23619. ' $mod.THelper.DoStatic();',
  23620. ' };',
  23621. '});',
  23622. 'this.w = 0;',
  23623. '']),
  23624. LinesToStr([ // $mod.$main
  23625. '$mod.THelper.DoStatic();',
  23626. '$mod.THelper.DoStatic();',
  23627. '']));
  23628. end;
  23629. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23630. begin
  23631. StartProgram(false);
  23632. Add([
  23633. '{$modeswitch typehelpers}',
  23634. 'type',
  23635. ' THelper = type helper for word',
  23636. ' procedure Run; external name ''Run'';',
  23637. ' end;',
  23638. 'var w: word;',
  23639. 'begin',
  23640. ' w.Run;',
  23641. '']);
  23642. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23643. ConvertProgram;
  23644. end;
  23645. procedure TTestModule.TestTypeHelper_Constructor;
  23646. begin
  23647. StartProgram(false);
  23648. Add([
  23649. '{$modeswitch typehelpers}',
  23650. 'type',
  23651. ' THelper = type helper for word',
  23652. ' constructor Init(e: longint);',
  23653. ' end;',
  23654. 'constructor THelper.Init(e: longint);',
  23655. 'begin',
  23656. ' Self:=e;',
  23657. ' Init(e+1);',
  23658. 'end;',
  23659. 'var w: word;',
  23660. 'begin',
  23661. ' w:=word.Init(2);',
  23662. ' w:=w.Init(3);',
  23663. ' with word do w:=Init(4);',
  23664. ' with w do w:=Init(5);',
  23665. '']);
  23666. ConvertProgram;
  23667. CheckSource('TestTypeHelper_Constructor',
  23668. LinesToStr([ // statements
  23669. 'rtl.createHelper($mod, "THelper", null, function () {',
  23670. ' this.Init = function (e) {',
  23671. ' this.set(e);',
  23672. ' $mod.THelper.Init.call(this, e + 1);',
  23673. ' return this.get();',
  23674. ' };',
  23675. ' this.$new = function (fn, args) {',
  23676. ' return this[fn].apply({',
  23677. ' p: 0,',
  23678. ' get: function () {',
  23679. ' return this.p;',
  23680. ' },',
  23681. ' set: function (v) {',
  23682. ' this.p = v;',
  23683. ' }',
  23684. ' }, args);',
  23685. ' };',
  23686. '});',
  23687. 'this.w = 0;',
  23688. '']),
  23689. LinesToStr([ // $mod.$main
  23690. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23691. '$mod.w = $mod.THelper.Init.call({',
  23692. ' p: $mod,',
  23693. ' get: function () {',
  23694. ' return this.p.w;',
  23695. ' },',
  23696. ' set: function (v) {',
  23697. ' this.p.w = v;',
  23698. ' }',
  23699. '}, 3);',
  23700. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23701. 'var $with1 = $mod.w;',
  23702. '$mod.w = $mod.THelper.Init.call({',
  23703. ' get: function () {',
  23704. ' return $with1;',
  23705. ' },',
  23706. ' set: function (v) {',
  23707. ' $with1 = v;',
  23708. ' }',
  23709. '}, 5);',
  23710. '']));
  23711. end;
  23712. procedure TTestModule.TestTypeHelper_Word;
  23713. begin
  23714. StartProgram(false);
  23715. Add([
  23716. '{$modeswitch typehelpers}',
  23717. 'type',
  23718. ' THelper = type helper for word',
  23719. ' procedure DoIt(e: byte = 123);',
  23720. ' end;',
  23721. 'procedure THelper.DoIt(e: byte);',
  23722. 'begin',
  23723. ' Self:=e;',
  23724. ' Self:=Self+1;',
  23725. ' with Self do Doit;',
  23726. 'end;',
  23727. 'begin',
  23728. ' word(3).DoIt;',
  23729. '']);
  23730. ConvertProgram;
  23731. CheckSource('TestTypeHelper_Word',
  23732. LinesToStr([ // statements
  23733. 'rtl.createHelper($mod, "THelper", null, function () {',
  23734. ' this.DoIt = function (e) {',
  23735. ' this.set(e);',
  23736. ' this.set(this.get() + 1);',
  23737. ' var $with1 = this.get();',
  23738. ' $mod.THelper.DoIt.call(this, 123);',
  23739. ' };',
  23740. '});',
  23741. '']),
  23742. LinesToStr([ // $mod.$main
  23743. '$mod.THelper.DoIt.call({',
  23744. ' get: function () {',
  23745. ' return 3;',
  23746. ' },',
  23747. ' set: function (v) {',
  23748. ' rtl.raiseE("EPropReadOnly");',
  23749. ' }',
  23750. '}, 123);',
  23751. '']));
  23752. end;
  23753. procedure TTestModule.TestTypeHelper_Boolean;
  23754. begin
  23755. StartProgram(false);
  23756. Add([
  23757. '{$modeswitch typehelpers}',
  23758. 'type',
  23759. ' Integer = longint;',
  23760. ' THelper = type helper for boolean',
  23761. ' procedure Run(e: wordbool = true);',
  23762. ' end;',
  23763. 'procedure THelper.Run(e: wordbool);',
  23764. 'begin',
  23765. ' Self:=e;',
  23766. ' Self:=not Self;',
  23767. ' with Self do Run;',
  23768. ' if Integer(Self)=0 then ;',
  23769. 'end;',
  23770. 'begin',
  23771. ' boolean(3).Run;',
  23772. '']);
  23773. ConvertProgram;
  23774. CheckSource('TestTypeHelper_Boolean',
  23775. LinesToStr([ // statements
  23776. 'rtl.createHelper($mod, "THelper", null, function () {',
  23777. ' this.Run = function (e) {',
  23778. ' this.set(e);',
  23779. ' this.set(!this.get());',
  23780. ' var $with1 = this.get();',
  23781. ' $mod.THelper.Run.call(this, true);',
  23782. ' if ((this.get() ? 1 : 0) === 0) ;',
  23783. ' };',
  23784. '});',
  23785. '']),
  23786. LinesToStr([ // $mod.$main
  23787. '$mod.THelper.Run.call({',
  23788. ' a: 3 != 0,',
  23789. ' get: function () {',
  23790. ' return this.a;',
  23791. ' },',
  23792. ' set: function (v) {',
  23793. ' rtl.raiseE("EPropReadOnly");',
  23794. ' }',
  23795. '}, true);',
  23796. '']));
  23797. end;
  23798. procedure TTestModule.TestTypeHelper_WordBool;
  23799. begin
  23800. StartProgram(false);
  23801. Add([
  23802. '{$modeswitch typehelpers}',
  23803. 'type',
  23804. ' Integer = longint;',
  23805. ' THelper = type helper for WordBool',
  23806. ' procedure Run(e: wordbool = true);',
  23807. ' end;',
  23808. 'procedure THelper.Run(e: wordbool);',
  23809. 'var i: integer;',
  23810. 'begin',
  23811. ' i:=Integer(Self);',
  23812. 'end;',
  23813. 'var w: wordbool;',
  23814. 'begin',
  23815. ' w.Run;',
  23816. ' wordbool(3).Run;',
  23817. '']);
  23818. ConvertProgram;
  23819. CheckSource('TestTypeHelper_WordBool',
  23820. LinesToStr([ // statements
  23821. 'rtl.createHelper($mod, "THelper", null, function () {',
  23822. ' this.Run = function (e) {',
  23823. ' var i = 0;',
  23824. ' i = (this.get() ? 1 : 0);',
  23825. ' };',
  23826. '});',
  23827. 'this.w = false;',
  23828. '']),
  23829. LinesToStr([ // $mod.$main
  23830. '$mod.THelper.Run.call({',
  23831. ' p: $mod,',
  23832. ' get: function () {',
  23833. ' return this.p.w;',
  23834. ' },',
  23835. ' set: function (v) {',
  23836. ' this.p.w = v;',
  23837. ' }',
  23838. '}, true);',
  23839. '$mod.THelper.Run.call({',
  23840. ' a: 3 != 0,',
  23841. ' get: function () {',
  23842. ' return this.a;',
  23843. ' },',
  23844. ' set: function (v) {',
  23845. ' rtl.raiseE("EPropReadOnly");',
  23846. ' }',
  23847. '}, true);',
  23848. '']));
  23849. end;
  23850. procedure TTestModule.TestTypeHelper_Double;
  23851. begin
  23852. StartProgram(false);
  23853. Add([
  23854. '{$modeswitch typehelpers}',
  23855. 'type',
  23856. ' Float = type double;',
  23857. ' THelper = type helper for Float',
  23858. ' const NPI = 3.141592;',
  23859. ' function ToStr: String;',
  23860. ' end;',
  23861. 'function THelper.ToStr: String;',
  23862. 'begin',
  23863. 'end;',
  23864. 'procedure DoIt(s: string);',
  23865. 'begin',
  23866. 'end;',
  23867. 'var f: Float;',
  23868. 'begin',
  23869. ' DoIt(f.toStr);',
  23870. ' DoIt(f.toStr());',
  23871. ' (f*f).toStr;',
  23872. ' DoIt((f*f).toStr);',
  23873. '']);
  23874. ConvertProgram;
  23875. CheckSource('TestTypeHelper_Double',
  23876. LinesToStr([ // statements
  23877. 'rtl.createHelper($mod, "THelper", null, function () {',
  23878. ' this.NPI = 3.141592;',
  23879. ' this.ToStr = function () {',
  23880. ' var Result = "";',
  23881. ' return Result;',
  23882. ' };',
  23883. '});',
  23884. 'this.DoIt = function (s) {',
  23885. '};',
  23886. 'this.f = 0.0;',
  23887. '']),
  23888. LinesToStr([ // $mod.$main
  23889. '$mod.DoIt($mod.THelper.ToStr.call({',
  23890. ' p: $mod,',
  23891. ' get: function () {',
  23892. ' return this.p.f;',
  23893. ' },',
  23894. ' set: function (v) {',
  23895. ' this.p.f = v;',
  23896. ' }',
  23897. '}));',
  23898. '$mod.DoIt($mod.THelper.ToStr.call({',
  23899. ' p: $mod,',
  23900. ' get: function () {',
  23901. ' return this.p.f;',
  23902. ' },',
  23903. ' set: function (v) {',
  23904. ' this.p.f = v;',
  23905. ' }',
  23906. '}));',
  23907. '$mod.THelper.ToStr.call({',
  23908. ' a: $mod.f * $mod.f,',
  23909. ' get: function () {',
  23910. ' return this.a;',
  23911. ' },',
  23912. ' set: function (v) {',
  23913. ' rtl.raiseE("EPropReadOnly");',
  23914. ' }',
  23915. '});',
  23916. '$mod.DoIt($mod.THelper.ToStr.call({',
  23917. ' a: $mod.f * $mod.f,',
  23918. ' get: function () {',
  23919. ' return this.a;',
  23920. ' },',
  23921. ' set: function (v) {',
  23922. ' rtl.raiseE("EPropReadOnly");',
  23923. ' }',
  23924. '}));',
  23925. '']));
  23926. end;
  23927. procedure TTestModule.TestTypeHelper_NativeInt;
  23928. begin
  23929. StartProgram(false);
  23930. Add([
  23931. '{$modeswitch typehelpers}',
  23932. 'type',
  23933. ' MaxInt = type nativeint;',
  23934. ' THelperI = type helper for MaxInt',
  23935. ' function ToStr: String;',
  23936. ' end;',
  23937. ' MaxUInt = type nativeuint;',
  23938. ' THelperU = type helper for MaxUInt',
  23939. ' function ToStr: String;',
  23940. ' end;',
  23941. 'function THelperI.ToStr: String;',
  23942. 'begin',
  23943. ' Result:=str(Self);',
  23944. 'end;',
  23945. 'function THelperU.ToStr: String;',
  23946. 'begin',
  23947. ' Result:=str(Self);',
  23948. 'end;',
  23949. 'procedure DoIt(s: string);',
  23950. 'begin',
  23951. 'end;',
  23952. 'var i: MaxInt;',
  23953. 'begin',
  23954. ' DoIt(i.toStr);',
  23955. ' DoIt(i.toStr());',
  23956. ' (i*i).toStr;',
  23957. ' DoIt((i*i).toStr);',
  23958. '']);
  23959. ConvertProgram;
  23960. CheckSource('TestTypeHelper_NativeInt',
  23961. LinesToStr([ // statements
  23962. 'rtl.createHelper($mod, "THelperI", null, function () {',
  23963. ' this.ToStr = function () {',
  23964. ' var Result = "";',
  23965. ' Result = "" + this.get();',
  23966. ' return Result;',
  23967. ' };',
  23968. '});',
  23969. 'rtl.createHelper($mod, "THelperU", null, function () {',
  23970. ' this.ToStr = function () {',
  23971. ' var Result = "";',
  23972. ' Result = "" + this.get();',
  23973. ' return Result;',
  23974. ' };',
  23975. '});',
  23976. 'this.DoIt = function (s) {',
  23977. '};',
  23978. 'this.i = 0;',
  23979. '']),
  23980. LinesToStr([ // $mod.$main
  23981. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23982. ' p: $mod,',
  23983. ' get: function () {',
  23984. ' return this.p.i;',
  23985. ' },',
  23986. ' set: function (v) {',
  23987. ' this.p.i = v;',
  23988. ' }',
  23989. '}));',
  23990. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23991. ' p: $mod,',
  23992. ' get: function () {',
  23993. ' return this.p.i;',
  23994. ' },',
  23995. ' set: function (v) {',
  23996. ' this.p.i = v;',
  23997. ' }',
  23998. '}));',
  23999. '$mod.THelperI.ToStr.call({',
  24000. ' a: $mod.i * $mod.i,',
  24001. ' get: function () {',
  24002. ' return this.a;',
  24003. ' },',
  24004. ' set: function (v) {',
  24005. ' rtl.raiseE("EPropReadOnly");',
  24006. ' }',
  24007. '});',
  24008. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24009. ' a: $mod.i * $mod.i,',
  24010. ' get: function () {',
  24011. ' return this.a;',
  24012. ' },',
  24013. ' set: function (v) {',
  24014. ' rtl.raiseE("EPropReadOnly");',
  24015. ' }',
  24016. '}));',
  24017. '']));
  24018. end;
  24019. procedure TTestModule.TestTypeHelper_StringChar;
  24020. begin
  24021. StartProgram(false);
  24022. Add([
  24023. '{$modeswitch typehelpers}',
  24024. 'type',
  24025. ' TStringHelper = type helper for string',
  24026. ' procedure DoIt(e: byte = 123);',
  24027. ' end;',
  24028. ' TCharHelper = type helper for char',
  24029. ' procedure Fly;',
  24030. ' end;',
  24031. 'procedure TStringHelper.DoIt(e: byte);',
  24032. 'begin',
  24033. ' Self[1]:=''c'';',
  24034. ' Self[2]:=Self[3];',
  24035. 'end;',
  24036. 'procedure TCharHelper.Fly;',
  24037. 'begin',
  24038. ' Self:=''c'';',
  24039. 'end;',
  24040. 'begin',
  24041. ' ''abc''.DoIt;',
  24042. ' ''xyz''.DoIt();',
  24043. ' ''c''.Fly();',
  24044. '']);
  24045. ConvertProgram;
  24046. CheckSource('TestTypeHelper_StringChar',
  24047. LinesToStr([ // statements
  24048. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  24049. ' this.DoIt = function (e) {',
  24050. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24051. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24052. ' };',
  24053. '});',
  24054. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  24055. ' this.Fly = function () {',
  24056. ' this.set("c");',
  24057. ' };',
  24058. '});',
  24059. '']),
  24060. LinesToStr([ // $mod.$main
  24061. '$mod.TStringHelper.DoIt.call({',
  24062. ' get: function () {',
  24063. ' return "abc";',
  24064. ' },',
  24065. ' set: function (v) {',
  24066. ' rtl.raiseE("EPropReadOnly");',
  24067. ' }',
  24068. '}, 123);',
  24069. '$mod.TStringHelper.DoIt.call({',
  24070. ' get: function () {',
  24071. ' return "xyz";',
  24072. ' },',
  24073. ' set: function (v) {',
  24074. ' rtl.raiseE("EPropReadOnly");',
  24075. ' }',
  24076. '}, 123);',
  24077. '$mod.TCharHelper.Fly.call({',
  24078. ' get: function () {',
  24079. ' return "c";',
  24080. ' },',
  24081. ' set: function (v) {',
  24082. ' rtl.raiseE("EPropReadOnly");',
  24083. ' }',
  24084. '});',
  24085. '']));
  24086. end;
  24087. procedure TTestModule.TestTypeHelper_JSValue;
  24088. begin
  24089. StartProgram(false);
  24090. Add([
  24091. '{$modeswitch typehelpers}',
  24092. 'type',
  24093. ' TExtValue = type jsvalue;',
  24094. ' THelper = type helper for TExtValue',
  24095. ' function ToStr: String;',
  24096. ' end;',
  24097. 'function THelper.ToStr: String;',
  24098. 'begin',
  24099. 'end;',
  24100. 'var',
  24101. ' s: string;',
  24102. ' v: TExtValue;',
  24103. 'begin',
  24104. ' s:=v.toStr;',
  24105. ' s:=v.toStr();',
  24106. ' TExtValue(s).toStr;',
  24107. '']);
  24108. ConvertProgram;
  24109. CheckSource('TestTypeHelper_JSValue',
  24110. LinesToStr([ // statements
  24111. 'rtl.createHelper($mod, "THelper", null, function () {',
  24112. ' this.ToStr = function () {',
  24113. ' var Result = "";',
  24114. ' return Result;',
  24115. ' };',
  24116. '});',
  24117. 'this.s = "";',
  24118. 'this.v = undefined;',
  24119. '']),
  24120. LinesToStr([ // $mod.$main
  24121. '$mod.s = $mod.THelper.ToStr.call({',
  24122. ' p: $mod,',
  24123. ' get: function () {',
  24124. ' return this.p.v;',
  24125. ' },',
  24126. ' set: function (v) {',
  24127. ' this.p.v = v;',
  24128. ' }',
  24129. '});',
  24130. '$mod.s = $mod.THelper.ToStr.call({',
  24131. ' p: $mod,',
  24132. ' get: function () {',
  24133. ' return this.p.v;',
  24134. ' },',
  24135. ' set: function (v) {',
  24136. ' this.p.v = v;',
  24137. ' }',
  24138. '});',
  24139. '$mod.THelper.ToStr.call({',
  24140. ' p: $mod,',
  24141. ' get: function () {',
  24142. ' return this.p.s;',
  24143. ' },',
  24144. ' set: function (v) {',
  24145. ' rtl.raiseE("EPropReadOnly");',
  24146. ' }',
  24147. '});',
  24148. '']));
  24149. end;
  24150. procedure TTestModule.TestTypeHelper_Array;
  24151. begin
  24152. StartProgram(false);
  24153. Add([
  24154. '{$modeswitch typehelpers}',
  24155. 'type',
  24156. ' TArrOfBool = array of boolean;',
  24157. ' TArrOfJS = array of jsvalue;',
  24158. ' THelper = type helper for TArrOfBool',
  24159. ' procedure DoIt(e: byte = 123);',
  24160. ' end;',
  24161. 'procedure THelper.DoIt(e: byte);',
  24162. 'begin',
  24163. ' Self[1]:=true;',
  24164. ' Self[2]:=not Self[3];',
  24165. ' SetLength(Self,4);',
  24166. 'end;',
  24167. 'var',
  24168. ' b: TArrOfBool;',
  24169. ' j: TArrOfJS;',
  24170. 'begin',
  24171. ' b.DoIt;',
  24172. ' TArrOfBool(j).DoIt();',
  24173. '']);
  24174. ConvertProgram;
  24175. CheckSource('TestTypeHelper_Array',
  24176. LinesToStr([ // statements
  24177. 'rtl.createHelper($mod, "THelper", null, function () {',
  24178. ' this.DoIt = function (e) {',
  24179. ' this.get()[1] = true;',
  24180. ' this.get()[2] = !this.get()[3];',
  24181. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24182. ' };',
  24183. '});',
  24184. 'this.b = [];',
  24185. 'this.j = [];',
  24186. '']),
  24187. LinesToStr([ // $mod.$main
  24188. '$mod.THelper.DoIt.call({',
  24189. ' p: $mod,',
  24190. ' get: function () {',
  24191. ' return this.p.b;',
  24192. ' },',
  24193. ' set: function (v) {',
  24194. ' this.p.b = v;',
  24195. ' }',
  24196. '}, 123);',
  24197. '$mod.THelper.DoIt.call({',
  24198. ' p: $mod,',
  24199. ' get: function () {',
  24200. ' return this.p.j;',
  24201. ' },',
  24202. ' set: function (v) {',
  24203. ' this.p.j = v;',
  24204. ' }',
  24205. '}, 123);',
  24206. '']));
  24207. end;
  24208. procedure TTestModule.TestTypeHelper_EnumType;
  24209. begin
  24210. StartProgram(false);
  24211. Add([
  24212. '{$modeswitch typehelpers}',
  24213. 'type',
  24214. ' TEnum = (red,blue);',
  24215. ' THelper = type helper for TEnum',
  24216. ' procedure DoIt(e: byte = 123);',
  24217. ' class procedure Swing(w: word); static;',
  24218. ' end;',
  24219. 'procedure THelper.DoIt(e: byte);',
  24220. 'begin',
  24221. ' Self:=red;',
  24222. ' Self:=succ(Self);',
  24223. ' with Self do Doit;',
  24224. 'end;',
  24225. 'class procedure THelper.Swing(w: word);',
  24226. 'begin',
  24227. 'end;',
  24228. 'var e: TEnum;',
  24229. 'begin',
  24230. ' e.DoIt;',
  24231. ' red.DoIt;',
  24232. ' TEnum.blue.DoIt;',
  24233. ' TEnum(1).DoIt;',
  24234. ' TEnum.Swing(3);',
  24235. '']);
  24236. ConvertProgram;
  24237. CheckSource('TestTypeHelper_EnumType',
  24238. LinesToStr([ // statements
  24239. 'this.TEnum = {',
  24240. ' "0": "red",',
  24241. ' red: 0,',
  24242. ' "1": "blue",',
  24243. ' blue: 1',
  24244. '};',
  24245. 'rtl.createHelper($mod, "THelper", null, function () {',
  24246. ' this.DoIt = function (e) {',
  24247. ' this.set($mod.TEnum.red);',
  24248. ' this.set(this.get() + 1);',
  24249. ' var $with1 = this.get();',
  24250. ' $mod.THelper.DoIt.call(this, 123);',
  24251. ' };',
  24252. ' this.Swing = function (w) {',
  24253. ' };',
  24254. '});',
  24255. 'this.e = 0;',
  24256. '']),
  24257. LinesToStr([ // $mod.$main
  24258. '$mod.THelper.DoIt.call({',
  24259. ' p: $mod,',
  24260. ' get: function () {',
  24261. ' return this.p.e;',
  24262. ' },',
  24263. ' set: function (v) {',
  24264. ' this.p.e = v;',
  24265. ' }',
  24266. '}, 123);',
  24267. '$mod.THelper.DoIt.call({',
  24268. ' p: $mod.TEnum,',
  24269. ' get: function () {',
  24270. ' return this.p.red;',
  24271. ' },',
  24272. ' set: function (v) {',
  24273. ' rtl.raiseE("EPropReadOnly");',
  24274. ' }',
  24275. '}, 123);',
  24276. '$mod.THelper.DoIt.call({',
  24277. ' p: $mod.TEnum,',
  24278. ' get: function () {',
  24279. ' return this.p.blue;',
  24280. ' },',
  24281. ' set: function (v) {',
  24282. ' rtl.raiseE("EPropReadOnly");',
  24283. ' }',
  24284. '}, 123);',
  24285. '$mod.THelper.DoIt.call({',
  24286. ' get: function () {',
  24287. ' return 1;',
  24288. ' },',
  24289. ' set: function (v) {',
  24290. ' rtl.raiseE("EPropReadOnly");',
  24291. ' }',
  24292. '}, 123);',
  24293. '$mod.THelper.Swing(3);',
  24294. '']));
  24295. end;
  24296. procedure TTestModule.TestTypeHelper_SetType;
  24297. begin
  24298. StartProgram(false);
  24299. Add([
  24300. '{$modeswitch typehelpers}',
  24301. 'type',
  24302. ' TEnum = (red,blue);',
  24303. ' TSetOfEnum = set of TEnum;',
  24304. ' THelper = type helper for TSetOfEnum',
  24305. ' procedure DoIt(e: byte = 123);',
  24306. ' constructor Init(e: TEnum);',
  24307. ' constructor InitEmpty;',
  24308. ' end;',
  24309. 'procedure THelper.DoIt(e: byte);',
  24310. 'begin',
  24311. ' Self:=[];',
  24312. ' Self:=[red];',
  24313. ' Include(Self,blue);',
  24314. 'end;',
  24315. 'constructor THelper.Init(e: TEnum);',
  24316. 'begin',
  24317. ' Self:=[];',
  24318. ' Self:=[e];',
  24319. ' Include(Self,blue);',
  24320. 'end;',
  24321. 'constructor THelper.InitEmpty;',
  24322. 'begin',
  24323. 'end;',
  24324. 'var s: TSetOfEnum;',
  24325. 'begin',
  24326. ' s.DoIt;',
  24327. //' [red].DoIt;',
  24328. //' with s do DoIt;',
  24329. //' with [red,blue] do DoIt;',
  24330. ' s:=TSetOfEnum.Init(blue);',
  24331. ' s:=s.Init(blue);',
  24332. '']);
  24333. ConvertProgram;
  24334. CheckSource('TestTypeHelper_SetType',
  24335. LinesToStr([ // statements
  24336. 'this.TEnum = {',
  24337. ' "0": "red",',
  24338. ' red: 0,',
  24339. ' "1": "blue",',
  24340. ' blue: 1',
  24341. '};',
  24342. 'rtl.createHelper($mod, "THelper", null, function () {',
  24343. ' this.DoIt = function (e) {',
  24344. ' this.set({});',
  24345. ' this.set(rtl.createSet($mod.TEnum.red));',
  24346. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24347. ' };',
  24348. ' this.Init = function (e) {',
  24349. ' this.set({});',
  24350. ' this.set(rtl.createSet(e));',
  24351. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24352. ' return this.get();',
  24353. ' };',
  24354. ' this.InitEmpty = function () {',
  24355. ' return this.get();',
  24356. ' };',
  24357. ' this.$new = function (fn, args) {',
  24358. ' return this[fn].apply({',
  24359. ' p: {},',
  24360. ' get: function () {',
  24361. ' return this.p;',
  24362. ' },',
  24363. ' set: function (v) {',
  24364. ' this.p = v;',
  24365. ' }',
  24366. ' }, args);',
  24367. ' };',
  24368. '});',
  24369. 'this.s = {};',
  24370. '']),
  24371. LinesToStr([ // $mod.$main
  24372. '$mod.THelper.DoIt.call({',
  24373. ' p: $mod,',
  24374. ' get: function () {',
  24375. ' return this.p.s;',
  24376. ' },',
  24377. ' set: function (v) {',
  24378. ' this.p.s = v;',
  24379. ' }',
  24380. '}, 123);',
  24381. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24382. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24383. ' p: $mod,',
  24384. ' get: function () {',
  24385. ' return this.p.s;',
  24386. ' },',
  24387. ' set: function (v) {',
  24388. ' this.p.s = v;',
  24389. ' }',
  24390. '}, $mod.TEnum.blue));',
  24391. '']));
  24392. end;
  24393. procedure TTestModule.TestTypeHelper_InterfaceType;
  24394. begin
  24395. StartProgram(false);
  24396. Add([
  24397. '{$interfaces com}',
  24398. '{$modeswitch typehelpers}',
  24399. 'type',
  24400. ' IUnknown = interface',
  24401. ' function _AddRef: longint;',
  24402. ' function _Release: longint;',
  24403. ' end;',
  24404. ' TObject = class(IUnknown)',
  24405. ' function _AddRef: longint; virtual; abstract;',
  24406. ' function _Release: longint; virtual; abstract;',
  24407. ' end;',
  24408. ' THelper = type helper for IUnknown',
  24409. ' procedure Fly(e: byte = 123);',
  24410. ' class procedure Run; static;',
  24411. ' end;',
  24412. 'var',
  24413. ' i: IUnknown;',
  24414. ' o: TObject;',
  24415. 'procedure THelper.Fly(e: byte);',
  24416. 'begin',
  24417. ' i:=Self;',
  24418. ' o:=Self as TObject;',
  24419. ' Self:=nil;',
  24420. ' Self:=i;',
  24421. ' Self:=o;',
  24422. ' with Self do begin',
  24423. ' Fly;',
  24424. ' Fly();',
  24425. ' end;',
  24426. 'end;',
  24427. 'class procedure THelper.Run;',
  24428. 'var l: IUnknown;',
  24429. 'begin',
  24430. ' l.Fly;',
  24431. ' l.Fly();',
  24432. 'end;',
  24433. 'begin',
  24434. ' i.Fly;',
  24435. ' i.Fly();',
  24436. ' i.Run;',
  24437. ' i.Run();',
  24438. ' IUnknown.Run;',
  24439. ' IUnknown.Run();',
  24440. '']);
  24441. ConvertProgram;
  24442. CheckSource('TestTypeHelper_InterfaceType',
  24443. LinesToStr([ // statements
  24444. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24445. 'rtl.createClass($mod, "TObject", null, function () {',
  24446. ' this.$init = function () {',
  24447. ' };',
  24448. ' this.$final = function () {',
  24449. ' };',
  24450. ' rtl.addIntf(this, $mod.IUnknown);',
  24451. '});',
  24452. 'rtl.createHelper($mod, "THelper", null, function () {',
  24453. ' this.Fly = function (e) {',
  24454. ' var $ir = rtl.createIntfRefs();',
  24455. ' try {',
  24456. ' rtl.setIntfP($mod, "i", this.get());',
  24457. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24458. ' this.set(null);',
  24459. ' this.set($mod.i);',
  24460. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24461. ' var $with1 = this.get();',
  24462. ' $mod.THelper.Fly.call(this, 123);',
  24463. ' $mod.THelper.Fly.call(this, 123);',
  24464. ' } finally {',
  24465. ' $ir.free();',
  24466. ' };',
  24467. ' };',
  24468. ' this.Run = function () {',
  24469. ' var l = null;',
  24470. ' try {',
  24471. ' $mod.THelper.Fly.call({',
  24472. ' get: function () {',
  24473. ' return l;',
  24474. ' },',
  24475. ' set: function (v) {',
  24476. ' l = rtl.setIntfL(l, v);',
  24477. ' }',
  24478. ' }, 123);',
  24479. ' $mod.THelper.Fly.call({',
  24480. ' get: function () {',
  24481. ' return l;',
  24482. ' },',
  24483. ' set: function (v) {',
  24484. ' l = rtl.setIntfL(l, v);',
  24485. ' }',
  24486. ' }, 123);',
  24487. ' } finally {',
  24488. ' rtl._Release(l);',
  24489. ' };',
  24490. ' };',
  24491. '});',
  24492. 'this.i = null;',
  24493. 'this.o = null;',
  24494. '']),
  24495. LinesToStr([ // $mod.$main
  24496. '$mod.THelper.Fly.call({',
  24497. ' p: $mod,',
  24498. ' get: function () {',
  24499. ' return this.p.i;',
  24500. ' },',
  24501. ' set: function (v) {',
  24502. ' rtl.setIntfP(this.p, "i", v);',
  24503. ' }',
  24504. '}, 123);',
  24505. '$mod.THelper.Fly.call({',
  24506. ' p: $mod,',
  24507. ' get: function () {',
  24508. ' return this.p.i;',
  24509. ' },',
  24510. ' set: function (v) {',
  24511. ' rtl.setIntfP(this.p, "i", v);',
  24512. ' }',
  24513. '}, 123);',
  24514. '$mod.THelper.Run();',
  24515. '$mod.THelper.Run();',
  24516. '$mod.THelper.Run();',
  24517. '$mod.THelper.Run();',
  24518. '']));
  24519. end;
  24520. procedure TTestModule.TestTypeHelper_NestedSelf;
  24521. begin
  24522. StartProgram(false);
  24523. Add([
  24524. '{$modeswitch typehelpers}',
  24525. 'type',
  24526. ' THelper = type helper for string',
  24527. ' procedure Run(Value: string);',
  24528. ' end;',
  24529. 'procedure THelper.Run(Value: string);',
  24530. ' function Sub(i: nativeint): boolean;',
  24531. ' begin',
  24532. ' Result:=Self[i+1]=Value[i];',
  24533. ' end;',
  24534. 'begin',
  24535. ' if Self[3]=Value[4] then ;',
  24536. 'end;',
  24537. 'begin',
  24538. '']);
  24539. ConvertProgram;
  24540. CheckSource('TestTypeHelper_NestedSelf',
  24541. LinesToStr([ // statements
  24542. 'rtl.createHelper($mod, "THelper", null, function () {',
  24543. ' this.Run = function (Value) {',
  24544. ' var $Self = this;',
  24545. ' function Sub(i) {',
  24546. ' var Result = false;',
  24547. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  24548. ' return Result;',
  24549. ' };',
  24550. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  24551. ' };',
  24552. '});',
  24553. '']),
  24554. LinesToStr([ // $mod.$main
  24555. '']));
  24556. end;
  24557. procedure TTestModule.TestProcType;
  24558. begin
  24559. StartProgram(false);
  24560. Add([
  24561. 'type',
  24562. ' TProcInt = procedure(vI: longint = 1);',
  24563. 'procedure DoIt(vJ: longint);',
  24564. 'begin end;',
  24565. 'var',
  24566. ' b: boolean;',
  24567. ' vP, vQ: tprocint;',
  24568. 'begin',
  24569. ' vp:=nil;',
  24570. ' vp:=vp;',
  24571. ' vp:=@doit;',
  24572. ' vp;',
  24573. ' vp();',
  24574. ' vp(2);',
  24575. ' b:=vp=nil;',
  24576. ' b:=nil=vp;',
  24577. ' b:=vp=vq;',
  24578. ' b:=vp=@doit;',
  24579. ' b:=@doit=vp;',
  24580. ' b:=vp<>nil;',
  24581. ' b:=nil<>vp;',
  24582. ' b:=vp<>vq;',
  24583. ' b:=vp<>@doit;',
  24584. ' b:=@doit<>vp;',
  24585. ' b:=Assigned(vp);',
  24586. ' if Assigned(vp) then ;']);
  24587. ConvertProgram;
  24588. CheckSource('TestProcType',
  24589. LinesToStr([ // statements
  24590. 'this.DoIt = function(vJ) {',
  24591. '};',
  24592. 'this.b = false;',
  24593. 'this.vP = null;',
  24594. 'this.vQ = null;'
  24595. ]),
  24596. LinesToStr([ // $mod.$main
  24597. '$mod.vP = null;',
  24598. '$mod.vP = $mod.vP;',
  24599. '$mod.vP = $mod.DoIt;',
  24600. '$mod.vP(1);',
  24601. '$mod.vP(1);',
  24602. '$mod.vP(2);',
  24603. '$mod.b = $mod.vP === null;',
  24604. '$mod.b = null === $mod.vP;',
  24605. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24606. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24607. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24608. '$mod.b = $mod.vP !== null;',
  24609. '$mod.b = null !== $mod.vP;',
  24610. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24611. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24612. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24613. '$mod.b = $mod.vP != null;',
  24614. 'if ($mod.vP != null) ;',
  24615. '']));
  24616. end;
  24617. procedure TTestModule.TestProcType_Arg;
  24618. begin
  24619. StartProgram(false);
  24620. Add([
  24621. 'type',
  24622. ' TProcInt = procedure(vI: longint = 1);',
  24623. 'procedure DoIt(vJ: longint); begin end;',
  24624. 'procedure DoSome(vP, vQ: TProcInt);',
  24625. 'var',
  24626. ' b: boolean;',
  24627. 'begin',
  24628. ' vp:=nil;',
  24629. ' vp:=vp;',
  24630. ' vp:=@doit;',
  24631. ' vp;',
  24632. ' vp();',
  24633. ' vp(2);',
  24634. ' b:=vp=nil;',
  24635. ' b:=nil=vp;',
  24636. ' b:=vp=vq;',
  24637. ' b:=vp=@doit;',
  24638. ' b:=@doit=vp;',
  24639. ' b:=vp<>nil;',
  24640. ' b:=nil<>vp;',
  24641. ' b:=vp<>vq;',
  24642. ' b:=vp<>@doit;',
  24643. ' b:=@doit<>vp;',
  24644. ' b:=Assigned(vp);',
  24645. ' if Assigned(vp) then ;',
  24646. 'end;',
  24647. 'begin',
  24648. ' DoSome(@DoIt,nil);']);
  24649. ConvertProgram;
  24650. CheckSource('TestProcType_Arg',
  24651. LinesToStr([ // statements
  24652. 'this.DoIt = function(vJ) {',
  24653. '};',
  24654. 'this.DoSome = function(vP, vQ) {',
  24655. ' var b = false;',
  24656. ' vP = null;',
  24657. ' vP = vP;',
  24658. ' vP = $mod.DoIt;',
  24659. ' vP(1);',
  24660. ' vP(1);',
  24661. ' vP(2);',
  24662. ' b = vP === null;',
  24663. ' b = null === vP;',
  24664. ' b = rtl.eqCallback(vP,vQ);',
  24665. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  24666. ' b = rtl.eqCallback($mod.DoIt, vP);',
  24667. ' b = vP !== null;',
  24668. ' b = null !== vP;',
  24669. ' b = !rtl.eqCallback(vP, vQ);',
  24670. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  24671. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  24672. ' b = vP != null;',
  24673. ' if (vP != null) ;',
  24674. '};',
  24675. '']),
  24676. LinesToStr([ // $mod.$main
  24677. '$mod.DoSome($mod.DoIt,null);',
  24678. '']));
  24679. end;
  24680. procedure TTestModule.TestProcType_FunctionFPC;
  24681. begin
  24682. StartProgram(false);
  24683. Add('type');
  24684. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24685. Add('function DoIt(vI: longint): longint;');
  24686. Add('begin end;');
  24687. Add('var');
  24688. Add(' b: boolean;');
  24689. Add(' vP, vQ: tfuncint;');
  24690. Add('begin');
  24691. Add(' vp:=nil;');
  24692. Add(' vp:=vp;');
  24693. Add(' vp:=@doit;'); // ok in fpc and delphi
  24694. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24695. Add(' vp;'); // ok in fpc and delphi
  24696. Add(' vp();');
  24697. Add(' vp(2);');
  24698. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24699. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24700. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24701. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24702. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24703. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24704. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24705. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24706. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24707. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24708. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24709. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24710. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24711. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24712. Add(' b:=Assigned(vp);');
  24713. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24714. Add(' doit(vp());'); // ok in fpc and delphi
  24715. Add(' doit(vp(2));'); // ok in fpc and delphi
  24716. ConvertProgram;
  24717. CheckSource('TestProcType_FunctionFPC',
  24718. LinesToStr([ // statements
  24719. 'this.DoIt = function(vI) {',
  24720. ' var Result = 0;',
  24721. ' return Result;',
  24722. '};',
  24723. 'this.b = false;',
  24724. 'this.vP = null;',
  24725. 'this.vQ = null;'
  24726. ]),
  24727. LinesToStr([ // $mod.$main
  24728. '$mod.vP = null;',
  24729. '$mod.vP = $mod.vP;',
  24730. '$mod.vP = $mod.DoIt;',
  24731. '$mod.vP(1);',
  24732. '$mod.vP(1);',
  24733. '$mod.vP(2);',
  24734. '$mod.b = $mod.vP === null;',
  24735. '$mod.b = null === $mod.vP;',
  24736. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24737. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24738. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24739. '$mod.b = 4 === $mod.vP(1);',
  24740. '$mod.b = $mod.vP !== null;',
  24741. '$mod.b = null !== $mod.vP;',
  24742. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24743. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24744. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24745. '$mod.b = 6 !== $mod.vP(1);',
  24746. '$mod.b = $mod.vP != null;',
  24747. '$mod.DoIt($mod.vP(1));',
  24748. '$mod.DoIt($mod.vP(2));',
  24749. '']));
  24750. end;
  24751. procedure TTestModule.TestProcType_FunctionDelphi;
  24752. begin
  24753. StartProgram(false);
  24754. Add('{$mode Delphi}');
  24755. Add('type');
  24756. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24757. Add('function DoIt(vI: longint): longint;');
  24758. Add('begin end;');
  24759. Add('var');
  24760. Add(' b: boolean;');
  24761. Add(' vP, vQ: tfuncint;');
  24762. Add('begin');
  24763. Add(' vp:=nil;');
  24764. Add(' vp:=vp;');
  24765. Add(' vp:=@doit;'); // ok in fpc and delphi
  24766. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24767. Add(' vp;'); // ok in fpc and delphi
  24768. Add(' vp();');
  24769. Add(' vp(2);');
  24770. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24771. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24772. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24773. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24774. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24775. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24776. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24777. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24778. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24779. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24780. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24781. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24782. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24783. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24784. Add(' b:=Assigned(vp);');
  24785. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24786. Add(' doit(vp());'); // ok in fpc and delphi
  24787. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  24788. ConvertProgram;
  24789. CheckSource('TestProcType_FunctionDelphi',
  24790. LinesToStr([ // statements
  24791. 'this.DoIt = function(vI) {',
  24792. ' var Result = 0;',
  24793. ' return Result;',
  24794. '};',
  24795. 'this.b = false;',
  24796. 'this.vP = null;',
  24797. 'this.vQ = null;'
  24798. ]),
  24799. LinesToStr([ // $mod.$main
  24800. '$mod.vP = null;',
  24801. '$mod.vP = $mod.vP;',
  24802. '$mod.vP = $mod.DoIt;',
  24803. '$mod.vP = $mod.DoIt;',
  24804. '$mod.vP(1);',
  24805. '$mod.vP(1);',
  24806. '$mod.vP(2);',
  24807. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  24808. '$mod.b = $mod.vP(1) === 3;',
  24809. '$mod.b = 4 === $mod.vP(1);',
  24810. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  24811. '$mod.b = $mod.vP(1) !== 5;',
  24812. '$mod.b = 6 !== $mod.vP(1);',
  24813. '$mod.b = $mod.vP != null;',
  24814. '$mod.DoIt($mod.vP(1));',
  24815. '$mod.DoIt($mod.vP(1));',
  24816. '$mod.DoIt($mod.vP(2));',
  24817. '']));
  24818. end;
  24819. procedure TTestModule.TestProcType_ProcedureDelphi;
  24820. begin
  24821. StartProgram(false);
  24822. Add('{$mode Delphi}');
  24823. Add('type');
  24824. Add(' TProc = procedure;');
  24825. Add('procedure DoIt;');
  24826. Add('begin end;');
  24827. Add('var');
  24828. Add(' b: boolean;');
  24829. Add(' vP, vQ: tproc;');
  24830. Add('begin');
  24831. Add(' vp:=nil;');
  24832. Add(' vp:=vp;');
  24833. Add(' vp:=vq;');
  24834. 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
  24835. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24836. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  24837. Add(' vp;'); // ok in fpc and delphi
  24838. Add(' vp();');
  24839. // equal
  24840. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24841. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  24842. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24843. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24844. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  24845. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24846. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24847. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  24848. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24849. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24850. // unequal
  24851. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24852. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  24853. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24854. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  24855. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24856. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  24857. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24858. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  24859. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24860. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  24861. Add(' b:=Assigned(vp);');
  24862. ConvertProgram;
  24863. CheckSource('TestProcType_ProcedureDelphi',
  24864. LinesToStr([ // statements
  24865. 'this.DoIt = function() {',
  24866. '};',
  24867. 'this.b = false;',
  24868. 'this.vP = null;',
  24869. 'this.vQ = null;'
  24870. ]),
  24871. LinesToStr([ // $mod.$main
  24872. '$mod.vP = null;',
  24873. '$mod.vP = $mod.vP;',
  24874. '$mod.vP = $mod.vQ;',
  24875. '$mod.vP = $mod.DoIt;',
  24876. '$mod.vP = $mod.DoIt;',
  24877. '$mod.vP();',
  24878. '$mod.vP();',
  24879. '$mod.b = $mod.vP === null;',
  24880. '$mod.b = null === $mod.vP;',
  24881. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  24882. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24883. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24884. '$mod.b = $mod.vP !== null;',
  24885. '$mod.b = null !== $mod.vP;',
  24886. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  24887. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24888. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24889. '$mod.b = $mod.vP != null;',
  24890. '']));
  24891. end;
  24892. procedure TTestModule.TestProcType_AsParam;
  24893. begin
  24894. StartProgram(false);
  24895. Add('type');
  24896. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24897. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  24898. Add('var vJ: tfuncint;');
  24899. Add('begin');
  24900. Add(' vg:=vg;');
  24901. Add(' vj:=vh;');
  24902. Add(' vi:=vi;');
  24903. Add(' doit(vg,vg,vg);');
  24904. Add(' doit(vh,vh,vj);');
  24905. Add(' doit(vi,vi,vi);');
  24906. Add(' doit(vj,vj,vj);');
  24907. Add('end;');
  24908. Add('var i: tfuncint;');
  24909. Add('begin');
  24910. Add(' doit(i,i,i);');
  24911. ConvertProgram;
  24912. CheckSource('TestProcType_AsParam',
  24913. LinesToStr([ // statements
  24914. 'this.DoIt = function (vG,vH,vI) {',
  24915. ' var vJ = null;',
  24916. ' vG = vG;',
  24917. ' vJ = vH;',
  24918. ' vI.set(vI.get());',
  24919. ' $mod.DoIt(vG, vG, {',
  24920. ' get: function () {',
  24921. ' return vG;',
  24922. ' },',
  24923. ' set: function (v) {',
  24924. ' vG = v;',
  24925. ' }',
  24926. ' });',
  24927. ' $mod.DoIt(vH, vH, {',
  24928. ' get: function () {',
  24929. ' return vJ;',
  24930. ' },',
  24931. ' set: function (v) {',
  24932. ' vJ = v;',
  24933. ' }',
  24934. ' });',
  24935. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  24936. ' $mod.DoIt(vJ, vJ, {',
  24937. ' get: function () {',
  24938. ' return vJ;',
  24939. ' },',
  24940. ' set: function (v) {',
  24941. ' vJ = v;',
  24942. ' }',
  24943. ' });',
  24944. '};',
  24945. 'this.i = null;'
  24946. ]),
  24947. LinesToStr([
  24948. '$mod.DoIt($mod.i,$mod.i,{',
  24949. ' p: $mod,',
  24950. ' get: function () {',
  24951. ' return this.p.i;',
  24952. ' },',
  24953. ' set: function (v) {',
  24954. ' this.p.i = v;',
  24955. ' }',
  24956. '});'
  24957. ]));
  24958. end;
  24959. procedure TTestModule.TestProcType_MethodFPC;
  24960. begin
  24961. StartProgram(false);
  24962. Add('type');
  24963. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24964. Add(' TObject = class');
  24965. Add(' function DoIt(vA: longint = 1): longint;');
  24966. Add(' end;');
  24967. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24968. Add('begin');
  24969. Add('end;');
  24970. Add('var');
  24971. Add(' Obj: TObject;');
  24972. Add(' vP: tfuncint;');
  24973. Add(' b: boolean;');
  24974. Add('begin');
  24975. Add(' vp:[email protected];'); // ok in fpc and delphi
  24976. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24977. Add(' vp;'); // ok in fpc and delphi
  24978. Add(' vp();');
  24979. Add(' vp(2);');
  24980. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24981. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24982. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24983. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24984. ConvertProgram;
  24985. CheckSource('TestProcType_MethodFPC',
  24986. LinesToStr([ // statements
  24987. 'rtl.createClass($mod, "TObject", null, function () {',
  24988. ' this.$init = function () {',
  24989. ' };',
  24990. ' this.$final = function () {',
  24991. ' };',
  24992. ' this.DoIt = function (vA) {',
  24993. ' var Result = 0;',
  24994. ' return Result;',
  24995. ' };',
  24996. '});',
  24997. 'this.Obj = null;',
  24998. 'this.vP = null;',
  24999. 'this.b = false;'
  25000. ]),
  25001. LinesToStr([
  25002. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25003. '$mod.vP(1);',
  25004. '$mod.vP(1);',
  25005. '$mod.vP(2);',
  25006. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25007. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25008. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25009. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25010. '']));
  25011. end;
  25012. procedure TTestModule.TestProcType_MethodDelphi;
  25013. begin
  25014. StartProgram(false);
  25015. Add([
  25016. '{$mode delphi}',
  25017. 'type',
  25018. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25019. ' TObject = class',
  25020. ' function DoIt(vA: longint = 1): longint;',
  25021. ' end;',
  25022. 'function TObject.DoIt(vA: longint = 1): longint;',
  25023. 'begin',
  25024. 'end;',
  25025. 'var',
  25026. ' Obj: TObject;',
  25027. ' vP: tfuncint;',
  25028. ' b: boolean;',
  25029. 'begin',
  25030. ' vp:[email protected];', // ok in fpc and delphi
  25031. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25032. ' vp;', // ok in fpc and delphi
  25033. ' vp();',
  25034. ' vp(2);',
  25035. //' b:[email protected];', // ok in fpc, illegal in delphi
  25036. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25037. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25038. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25039. '']);
  25040. ConvertProgram;
  25041. CheckSource('TestProcType_MethodDelphi',
  25042. LinesToStr([ // statements
  25043. 'rtl.createClass($mod, "TObject", null, function () {',
  25044. ' this.$init = function () {',
  25045. ' };',
  25046. ' this.$final = function () {',
  25047. ' };',
  25048. ' this.DoIt = function (vA) {',
  25049. ' var Result = 0;',
  25050. ' return Result;',
  25051. ' };',
  25052. '});',
  25053. 'this.Obj = null;',
  25054. 'this.vP = null;',
  25055. 'this.b = false;'
  25056. ]),
  25057. LinesToStr([
  25058. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25059. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25060. '$mod.vP(1);',
  25061. '$mod.vP(1);',
  25062. '$mod.vP(2);',
  25063. '']));
  25064. end;
  25065. procedure TTestModule.TestProcType_PropertyFPC;
  25066. begin
  25067. StartProgram(false);
  25068. Add('type');
  25069. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25070. Add(' TObject = class');
  25071. Add(' FOnFoo: TFuncInt;');
  25072. Add(' function DoIt(vA: longint = 1): longint;');
  25073. Add(' function GetFoo: TFuncInt;');
  25074. Add(' procedure SetFoo(const Value: TFuncInt);');
  25075. Add(' function GetEvents(Index: longint): TFuncInt;');
  25076. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25077. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25078. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25079. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25080. Add(' end;');
  25081. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25082. Add('function tobject.getfoo: tfuncint; begin end;');
  25083. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25084. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25085. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25086. Add('var');
  25087. Add(' Obj: TObject;');
  25088. Add(' vP: tfuncint;');
  25089. Add(' b: boolean;');
  25090. Add('begin');
  25091. Add(' obj.onfoo:=nil;');
  25092. Add(' obj.onbar:=nil;');
  25093. Add(' obj.events[1]:=nil;');
  25094. Add(' obj.onfoo:=obj.onfoo;');
  25095. Add(' obj.onbar:=obj.onbar;');
  25096. Add(' obj.events[2]:=obj.events[3];');
  25097. Add(' obj.onfoo:[email protected];');
  25098. Add(' obj.onbar:[email protected];');
  25099. Add(' obj.events[4]:[email protected];');
  25100. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25101. //Add(' obj.onbar:=obj.doit;'); // delphi
  25102. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25103. Add(' obj.onfoo;');
  25104. Add(' obj.onbar;');
  25105. //Add(' obj.events[5];'); ToDo in pasresolver
  25106. Add(' obj.onfoo();');
  25107. Add(' obj.onbar();');
  25108. Add(' obj.events[6]();');
  25109. Add(' b:=obj.onfoo=nil;');
  25110. Add(' b:=obj.onbar=nil;');
  25111. Add(' b:=obj.events[7]=nil;');
  25112. Add(' b:=obj.onfoo<>nil;');
  25113. Add(' b:=obj.onbar<>nil;');
  25114. Add(' b:=obj.events[8]<>nil;');
  25115. Add(' b:=obj.onfoo=vp;');
  25116. Add(' b:=obj.onbar=vp;');
  25117. Add(' b:=obj.events[9]=vp;');
  25118. Add(' b:=obj.onfoo=obj.onfoo;');
  25119. Add(' b:=obj.onbar=obj.onfoo;');
  25120. Add(' b:=obj.events[10]=obj.onfoo;');
  25121. Add(' b:=obj.onfoo<>obj.onfoo;');
  25122. Add(' b:=obj.onbar<>obj.onfoo;');
  25123. Add(' b:=obj.events[11]<>obj.onfoo;');
  25124. Add(' b:[email protected];');
  25125. Add(' b:[email protected];');
  25126. Add(' b:=obj.events[12][email protected];');
  25127. Add(' b:=obj.onfoo<>@obj.doit;');
  25128. Add(' b:=obj.onbar<>@obj.doit;');
  25129. Add(' b:=obj.events[12]<>@obj.doit;');
  25130. Add(' b:=Assigned(obj.onfoo);');
  25131. Add(' b:=Assigned(obj.onbar);');
  25132. Add(' b:=Assigned(obj.events[13]);');
  25133. ConvertProgram;
  25134. CheckSource('TestProcType_PropertyFPC',
  25135. LinesToStr([ // statements
  25136. 'rtl.createClass($mod, "TObject", null, function () {',
  25137. ' this.$init = function () {',
  25138. ' this.FOnFoo = null;',
  25139. ' };',
  25140. ' this.$final = function () {',
  25141. ' this.FOnFoo = undefined;',
  25142. ' };',
  25143. ' this.DoIt = function (vA) {',
  25144. ' var Result = 0;',
  25145. ' return Result;',
  25146. ' };',
  25147. 'this.GetFoo = function () {',
  25148. ' var Result = null;',
  25149. ' return Result;',
  25150. '};',
  25151. 'this.SetFoo = function (Value) {',
  25152. '};',
  25153. 'this.GetEvents = function (Index) {',
  25154. ' var Result = null;',
  25155. ' return Result;',
  25156. '};',
  25157. 'this.SetEvents = function (Index, Value) {',
  25158. '};',
  25159. '});',
  25160. 'this.Obj = null;',
  25161. 'this.vP = null;',
  25162. 'this.b = false;'
  25163. ]),
  25164. LinesToStr([
  25165. '$mod.Obj.FOnFoo = null;',
  25166. '$mod.Obj.SetFoo(null);',
  25167. '$mod.Obj.SetEvents(1, null);',
  25168. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25169. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25170. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25171. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25172. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25173. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25174. '$mod.Obj.FOnFoo(1);',
  25175. '$mod.Obj.GetFoo();',
  25176. '$mod.Obj.FOnFoo(1);',
  25177. '$mod.Obj.GetFoo()(1);',
  25178. '$mod.Obj.GetEvents(6)(1);',
  25179. '$mod.b = $mod.Obj.FOnFoo === null;',
  25180. '$mod.b = $mod.Obj.GetFoo() === null;',
  25181. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25182. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25183. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25184. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25185. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25186. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25187. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25188. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25189. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25190. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25191. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25192. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25193. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25194. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25195. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25196. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25197. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25198. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25199. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25200. '$mod.b = $mod.Obj.FOnFoo != null;',
  25201. '$mod.b = $mod.Obj.GetFoo() != null;',
  25202. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25203. '']));
  25204. end;
  25205. procedure TTestModule.TestProcType_PropertyDelphi;
  25206. begin
  25207. StartProgram(false);
  25208. Add('{$mode delphi}');
  25209. Add('type');
  25210. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25211. Add(' TObject = class');
  25212. Add(' FOnFoo: TFuncInt;');
  25213. Add(' function DoIt(vA: longint = 1): longint;');
  25214. Add(' function GetFoo: TFuncInt;');
  25215. Add(' procedure SetFoo(const Value: TFuncInt);');
  25216. Add(' function GetEvents(Index: longint): TFuncInt;');
  25217. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25218. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25219. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25220. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25221. Add(' end;');
  25222. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25223. Add('function tobject.getfoo: tfuncint; begin end;');
  25224. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25225. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25226. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25227. Add('var');
  25228. Add(' Obj: TObject;');
  25229. Add(' vP: tfuncint;');
  25230. Add(' b: boolean;');
  25231. Add('begin');
  25232. Add(' obj.onfoo:=nil;');
  25233. Add(' obj.onbar:=nil;');
  25234. Add(' obj.events[1]:=nil;');
  25235. Add(' obj.onfoo:=obj.onfoo;');
  25236. Add(' obj.onbar:=obj.onbar;');
  25237. Add(' obj.events[2]:=obj.events[3];');
  25238. Add(' obj.onfoo:[email protected];');
  25239. Add(' obj.onbar:[email protected];');
  25240. Add(' obj.events[4]:[email protected];');
  25241. Add(' obj.onfoo:=obj.doit;'); // delphi
  25242. Add(' obj.onbar:=obj.doit;'); // delphi
  25243. Add(' obj.events[4]:=obj.doit;'); // delphi
  25244. Add(' obj.onfoo;');
  25245. Add(' obj.onbar;');
  25246. //Add(' obj.events[5];'); ToDo in pasresolver
  25247. Add(' obj.onfoo();');
  25248. Add(' obj.onbar();');
  25249. Add(' obj.events[6]();');
  25250. //Add(' b:=obj.onfoo=nil;'); // fpc
  25251. //Add(' b:=obj.onbar=nil;'); // fpc
  25252. //Add(' b:=obj.events[7]=nil;'); // fpc
  25253. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25254. //Add(' b:=obj.onbar<>nil;'); // fpc
  25255. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25256. Add(' b:=obj.onfoo=vp;');
  25257. Add(' b:=obj.onbar=vp;');
  25258. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25259. Add(' b:=obj.onfoo=obj.onfoo;');
  25260. Add(' b:=obj.onbar=obj.onfoo;');
  25261. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25262. Add(' b:=obj.onfoo<>obj.onfoo;');
  25263. Add(' b:=obj.onbar<>obj.onfoo;');
  25264. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25265. //Add(' b:[email protected];'); // fpc
  25266. //Add(' b:[email protected];'); // fpc
  25267. //Add(' b:=obj.events[12][email protected];'); // fpc
  25268. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25269. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25270. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25271. Add(' b:=Assigned(obj.onfoo);');
  25272. Add(' b:=Assigned(obj.onbar);');
  25273. Add(' b:=Assigned(obj.events[13]);');
  25274. ConvertProgram;
  25275. CheckSource('TestProcType_PropertyDelphi',
  25276. LinesToStr([ // statements
  25277. 'rtl.createClass($mod, "TObject", null, function () {',
  25278. ' this.$init = function () {',
  25279. ' this.FOnFoo = null;',
  25280. ' };',
  25281. ' this.$final = function () {',
  25282. ' this.FOnFoo = undefined;',
  25283. ' };',
  25284. ' this.DoIt = function (vA) {',
  25285. ' var Result = 0;',
  25286. ' return Result;',
  25287. ' };',
  25288. 'this.GetFoo = function () {',
  25289. ' var Result = null;',
  25290. ' return Result;',
  25291. '};',
  25292. 'this.SetFoo = function (Value) {',
  25293. '};',
  25294. 'this.GetEvents = function (Index) {',
  25295. ' var Result = null;',
  25296. ' return Result;',
  25297. '};',
  25298. 'this.SetEvents = function (Index, Value) {',
  25299. '};',
  25300. '});',
  25301. 'this.Obj = null;',
  25302. 'this.vP = null;',
  25303. 'this.b = false;'
  25304. ]),
  25305. LinesToStr([
  25306. '$mod.Obj.FOnFoo = null;',
  25307. '$mod.Obj.SetFoo(null);',
  25308. '$mod.Obj.SetEvents(1, null);',
  25309. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25310. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25311. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25312. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25313. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25314. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25315. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25316. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25317. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25318. '$mod.Obj.FOnFoo(1);',
  25319. '$mod.Obj.GetFoo();',
  25320. '$mod.Obj.FOnFoo(1);',
  25321. '$mod.Obj.GetFoo()(1);',
  25322. '$mod.Obj.GetEvents(6)(1);',
  25323. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25324. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25325. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25326. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25327. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25328. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25329. '$mod.b = $mod.Obj.FOnFoo != null;',
  25330. '$mod.b = $mod.Obj.GetFoo() != null;',
  25331. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25332. '']));
  25333. end;
  25334. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25335. begin
  25336. StartProgram(false);
  25337. Add('type');
  25338. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25339. Add(' TObject = class');
  25340. Add(' FOnFoo: TFuncInt;');
  25341. Add(' function DoIt(vA: longint = 1): longint;');
  25342. Add(' function GetFoo: TFuncInt;');
  25343. Add(' procedure SetFoo(const Value: TFuncInt);');
  25344. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25345. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25346. Add(' end;');
  25347. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25348. Add('function tobject.getfoo: tfuncint; begin end;');
  25349. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25350. Add('var');
  25351. Add(' Obj: TObject;');
  25352. Add(' vP: tfuncint;');
  25353. Add(' b: boolean;');
  25354. Add('begin');
  25355. Add('with obj do begin');
  25356. Add(' fonfoo:=nil;');
  25357. Add(' onfoo:=nil;');
  25358. Add(' onbar:=nil;');
  25359. Add(' fonfoo:=fonfoo;');
  25360. Add(' onfoo:=onfoo;');
  25361. Add(' onbar:=onbar;');
  25362. Add(' fonfoo:=@doit;');
  25363. Add(' onfoo:=@doit;');
  25364. Add(' onbar:=@doit;');
  25365. //Add(' fonfoo:=doit;'); // delphi
  25366. //Add(' onfoo:=doit;'); // delphi
  25367. //Add(' onbar:=doit;'); // delphi
  25368. Add(' fonfoo;');
  25369. Add(' onfoo;');
  25370. Add(' onbar;');
  25371. Add(' fonfoo();');
  25372. Add(' onfoo();');
  25373. Add(' onbar();');
  25374. Add(' b:=fonfoo=nil;');
  25375. Add(' b:=onfoo=nil;');
  25376. Add(' b:=onbar=nil;');
  25377. Add(' b:=fonfoo<>nil;');
  25378. Add(' b:=onfoo<>nil;');
  25379. Add(' b:=onbar<>nil;');
  25380. Add(' b:=fonfoo=vp;');
  25381. Add(' b:=onfoo=vp;');
  25382. Add(' b:=onbar=vp;');
  25383. Add(' b:=fonfoo=fonfoo;');
  25384. Add(' b:=onfoo=onfoo;');
  25385. Add(' b:=onbar=onfoo;');
  25386. Add(' b:=fonfoo<>fonfoo;');
  25387. Add(' b:=onfoo<>onfoo;');
  25388. Add(' b:=onbar<>onfoo;');
  25389. Add(' b:=fonfoo=@doit;');
  25390. Add(' b:=onfoo=@doit;');
  25391. Add(' b:=onbar=@doit;');
  25392. Add(' b:=fonfoo<>@doit;');
  25393. Add(' b:=onfoo<>@doit;');
  25394. Add(' b:=onbar<>@doit;');
  25395. Add(' b:=Assigned(fonfoo);');
  25396. Add(' b:=Assigned(onfoo);');
  25397. Add(' b:=Assigned(onbar);');
  25398. Add('end;');
  25399. ConvertProgram;
  25400. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25401. LinesToStr([ // statements
  25402. 'rtl.createClass($mod, "TObject", null, function () {',
  25403. ' this.$init = function () {',
  25404. ' this.FOnFoo = null;',
  25405. ' };',
  25406. ' this.$final = function () {',
  25407. ' this.FOnFoo = undefined;',
  25408. ' };',
  25409. ' this.DoIt = function (vA) {',
  25410. ' var Result = 0;',
  25411. ' return Result;',
  25412. ' };',
  25413. ' this.GetFoo = function () {',
  25414. ' var Result = null;',
  25415. ' return Result;',
  25416. ' };',
  25417. ' this.SetFoo = function (Value) {',
  25418. ' };',
  25419. '});',
  25420. 'this.Obj = null;',
  25421. 'this.vP = null;',
  25422. 'this.b = false;'
  25423. ]),
  25424. LinesToStr([
  25425. 'var $with1 = $mod.Obj;',
  25426. '$with1.FOnFoo = null;',
  25427. '$with1.FOnFoo = null;',
  25428. '$with1.SetFoo(null);',
  25429. '$with1.FOnFoo = $with1.FOnFoo;',
  25430. '$with1.FOnFoo = $with1.FOnFoo;',
  25431. '$with1.SetFoo($with1.GetFoo());',
  25432. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  25433. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  25434. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  25435. '$with1.FOnFoo(1);',
  25436. '$with1.FOnFoo(1);',
  25437. '$with1.GetFoo();',
  25438. '$with1.FOnFoo(1);',
  25439. '$with1.FOnFoo(1);',
  25440. '$with1.GetFoo()(1);',
  25441. '$mod.b = $with1.FOnFoo === null;',
  25442. '$mod.b = $with1.FOnFoo === null;',
  25443. '$mod.b = $with1.GetFoo() === null;',
  25444. '$mod.b = $with1.FOnFoo !== null;',
  25445. '$mod.b = $with1.FOnFoo !== null;',
  25446. '$mod.b = $with1.GetFoo() !== null;',
  25447. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  25448. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  25449. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  25450. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  25451. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  25452. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  25453. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  25454. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  25455. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  25456. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  25457. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  25458. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  25459. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  25460. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  25461. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  25462. '$mod.b = $with1.FOnFoo != null;',
  25463. '$mod.b = $with1.FOnFoo != null;',
  25464. '$mod.b = $with1.GetFoo() != null;',
  25465. '']));
  25466. end;
  25467. procedure TTestModule.TestProcType_Nested;
  25468. begin
  25469. StartProgram(false);
  25470. Add([
  25471. 'type',
  25472. ' TProcInt = procedure(vI: longint = 1);',
  25473. 'procedure DoIt(vJ: longint);',
  25474. 'var aProc: TProcInt;',
  25475. ' b: boolean;',
  25476. ' procedure Sub(vK: longint);',
  25477. ' var aSub: TProcInt;',
  25478. ' procedure SubSub(vK: longint);',
  25479. ' var aSubSub: TProcInt;',
  25480. ' begin;',
  25481. ' aProc:=@DoIt;',
  25482. ' aSub:=@DoIt;',
  25483. ' aSubSub:=@DoIt;',
  25484. ' aProc:=@Sub;',
  25485. ' aSub:=@Sub;',
  25486. ' aSubSub:=@Sub;',
  25487. ' aProc:=@SubSub;',
  25488. ' aSub:=@SubSub;',
  25489. ' aSubSub:=@SubSub;',
  25490. ' end;',
  25491. ' begin;',
  25492. ' end;',
  25493. 'begin;',
  25494. ' aProc:=@Sub;',
  25495. ' b:=aProc=@Sub;',
  25496. ' b:=@Sub=aProc;',
  25497. 'end;',
  25498. 'begin',
  25499. '']);
  25500. ConvertProgram;
  25501. CheckSource('TestProcType_Nested',
  25502. LinesToStr([ // statements
  25503. 'this.DoIt = function (vJ) {',
  25504. ' var aProc = null;',
  25505. ' var b = false;',
  25506. ' function Sub(vK) {',
  25507. ' var aSub = null;',
  25508. ' function SubSub(vK) {',
  25509. ' var aSubSub = null;',
  25510. ' aProc = $mod.DoIt;',
  25511. ' aSub = $mod.DoIt;',
  25512. ' aSubSub = $mod.DoIt;',
  25513. ' aProc = Sub;',
  25514. ' aSub = Sub;',
  25515. ' aSubSub = Sub;',
  25516. ' aProc = SubSub;',
  25517. ' aSub = SubSub;',
  25518. ' aSubSub = SubSub;',
  25519. ' };',
  25520. ' };',
  25521. ' aProc = Sub;',
  25522. ' b = rtl.eqCallback(aProc, Sub);',
  25523. ' b = rtl.eqCallback(Sub, aProc);',
  25524. '};',
  25525. '']),
  25526. LinesToStr([ // $mod.$main
  25527. '']));
  25528. end;
  25529. procedure TTestModule.TestProcType_NestedOfObject;
  25530. begin
  25531. StartProgram(false);
  25532. Add([
  25533. 'type',
  25534. ' TProcInt = procedure(vI: longint = 1) of object;',
  25535. ' TObject = class',
  25536. ' procedure DoIt(vJ: longint);',
  25537. ' end;',
  25538. 'procedure TObject.DoIt(vJ: longint);',
  25539. 'var aProc: TProcInt;',
  25540. ' b: boolean;',
  25541. ' procedure Sub(vK: longint);',
  25542. ' var aSub: TProcInt;',
  25543. ' procedure SubSub(vK: longint);',
  25544. ' var aSubSub: TProcInt;',
  25545. ' begin;',
  25546. ' aProc:=@DoIt;',
  25547. ' aSub:=@DoIt;',
  25548. ' aSubSub:=@DoIt;',
  25549. ' aProc:=@Sub;',
  25550. ' aSub:=@Sub;',
  25551. ' aSubSub:=@Sub;',
  25552. ' aProc:=@SubSub;',
  25553. ' aSub:=@SubSub;',
  25554. ' aSubSub:=@SubSub;',
  25555. ' end;',
  25556. ' begin;',
  25557. ' end;',
  25558. 'begin;',
  25559. ' aProc:=@Sub;',
  25560. ' b:=aProc=@Sub;',
  25561. ' b:=@Sub=aProc;',
  25562. 'end;',
  25563. 'begin',
  25564. '']);
  25565. ConvertProgram;
  25566. CheckSource('TestProcType_Nested',
  25567. LinesToStr([ // statements
  25568. 'rtl.createClass($mod, "TObject", null, function () {',
  25569. ' this.$init = function () {',
  25570. ' };',
  25571. ' this.$final = function () {',
  25572. ' };',
  25573. ' this.DoIt = function (vJ) {',
  25574. ' var $Self = this;',
  25575. ' var aProc = null;',
  25576. ' var b = false;',
  25577. ' function Sub(vK) {',
  25578. ' var aSub = null;',
  25579. ' function SubSub(vK) {',
  25580. ' var aSubSub = null;',
  25581. ' aProc = rtl.createCallback($Self, "DoIt");',
  25582. ' aSub = rtl.createCallback($Self, "DoIt");',
  25583. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  25584. ' aProc = Sub;',
  25585. ' aSub = Sub;',
  25586. ' aSubSub = Sub;',
  25587. ' aProc = SubSub;',
  25588. ' aSub = SubSub;',
  25589. ' aSubSub = SubSub;',
  25590. ' };',
  25591. ' };',
  25592. ' aProc = Sub;',
  25593. ' b = rtl.eqCallback(aProc, Sub);',
  25594. ' b = rtl.eqCallback(Sub, aProc);',
  25595. ' };',
  25596. '});',
  25597. '']),
  25598. LinesToStr([ // $mod.$main
  25599. '']));
  25600. end;
  25601. procedure TTestModule.TestProcType_ReferenceToProc;
  25602. begin
  25603. StartProgram(false);
  25604. Add([
  25605. 'type',
  25606. ' TProcRef = reference to procedure(i: longint = 0);',
  25607. ' TFuncRef = reference to function(i: longint = 0): longint;',
  25608. 'var',
  25609. ' p: TProcRef;',
  25610. ' f: TFuncRef;',
  25611. 'procedure DoIt(i: longint);',
  25612. 'begin',
  25613. 'end;',
  25614. 'function GetIt(i: longint): longint;',
  25615. 'begin',
  25616. ' p:=@DoIt;',
  25617. ' f:=@GetIt;',
  25618. ' f;',
  25619. ' f();',
  25620. ' f(1);',
  25621. 'end;',
  25622. 'begin',
  25623. ' p:=@DoIt;',
  25624. ' f:=@GetIt;',
  25625. ' f;',
  25626. ' f();',
  25627. ' f(1);',
  25628. ' p:=TProcRef(f);',
  25629. '']);
  25630. ConvertProgram;
  25631. CheckSource('TestProcType_ReferenceToProc',
  25632. LinesToStr([ // statements
  25633. 'this.p = null;',
  25634. 'this.f = null;',
  25635. 'this.DoIt = function (i) {',
  25636. '};',
  25637. 'this.GetIt = function (i) {',
  25638. ' var Result = 0;',
  25639. ' $mod.p = $mod.DoIt;',
  25640. ' $mod.f = $mod.GetIt;',
  25641. ' $mod.f(0);',
  25642. ' $mod.f(0);',
  25643. ' $mod.f(1);',
  25644. ' return Result;',
  25645. '};',
  25646. '']),
  25647. LinesToStr([ // $mod.$main
  25648. '$mod.p = $mod.DoIt;',
  25649. '$mod.f = $mod.GetIt;',
  25650. '$mod.f(0);',
  25651. '$mod.f(0);',
  25652. '$mod.f(1);',
  25653. '$mod.p = $mod.f;',
  25654. '']));
  25655. end;
  25656. procedure TTestModule.TestProcType_ReferenceToMethod;
  25657. begin
  25658. StartProgram(false);
  25659. Add([
  25660. 'type',
  25661. ' TFuncRef = reference to function(i: longint = 5): longint;',
  25662. ' TObject = class',
  25663. ' function Grow(s: longint): longint;',
  25664. ' end;',
  25665. 'var',
  25666. ' f: tfuncref;',
  25667. 'function tobject.grow(s: longint): longint;',
  25668. ' function GrowSub(i: longint): longint;',
  25669. ' begin',
  25670. ' f:=@grow;',
  25671. ' f:=@growsub;',
  25672. ' end;',
  25673. 'begin',
  25674. ' f:=@grow;',
  25675. ' f:=@growsub;',
  25676. 'end;',
  25677. 'begin',
  25678. '']);
  25679. ConvertProgram;
  25680. CheckSource('TestProcType_ReferenceToMethod',
  25681. LinesToStr([ // statements
  25682. 'rtl.createClass($mod, "TObject", null, function () {',
  25683. ' this.$init = function () {',
  25684. ' };',
  25685. ' this.$final = function () {',
  25686. ' };',
  25687. ' this.Grow = function (s) {',
  25688. ' var $Self = this;',
  25689. ' var Result = 0;',
  25690. ' function GrowSub(i) {',
  25691. ' var Result = 0;',
  25692. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25693. ' $mod.f = GrowSub;',
  25694. ' return Result;',
  25695. ' };',
  25696. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25697. ' $mod.f = GrowSub;',
  25698. ' return Result;',
  25699. ' };',
  25700. '});',
  25701. 'this.f = null;',
  25702. '']),
  25703. LinesToStr([ // $mod.$main
  25704. '']));
  25705. end;
  25706. procedure TTestModule.TestProcType_Typecast;
  25707. begin
  25708. StartProgram(false);
  25709. Add([
  25710. 'type',
  25711. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  25712. ' TEvent = procedure of object;',
  25713. ' TGetter = function:longint of object;',
  25714. ' TProcA = procedure(i: longint);',
  25715. ' TFuncB = function(i, j: longint): longint;',
  25716. 'procedure DoIt(); varargs; begin end;',
  25717. 'var',
  25718. ' Notify: tnotifyevent;',
  25719. ' Event: tevent;',
  25720. ' Getter: tgetter;',
  25721. ' ProcA: tproca;',
  25722. ' FuncB: tfuncb;',
  25723. ' p: pointer;',
  25724. 'begin',
  25725. ' notify:=tnotifyevent(event);',
  25726. ' event:=tevent(event);',
  25727. ' event:=tevent(notify);',
  25728. ' event:=tevent(getter);',
  25729. ' event:=tevent(proca);',
  25730. ' proca:=tproca(funcb);',
  25731. ' funcb:=tfuncb(funcb);',
  25732. ' funcb:=tfuncb(proca);',
  25733. ' funcb:=tfuncb(getter);',
  25734. ' proca:=tproca(p);',
  25735. ' funcb:=tfuncb(p);',
  25736. ' getter:=tgetter(p);',
  25737. ' p:=pointer(notify);',
  25738. ' p:=notify;',
  25739. ' p:=pointer(proca);',
  25740. ' p:=proca;',
  25741. ' p:=pointer(funcb);',
  25742. ' p:=funcb;',
  25743. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  25744. '']);
  25745. ConvertProgram;
  25746. CheckSource('TestProcType_Typecast',
  25747. LinesToStr([ // statements
  25748. 'this.DoIt = function () {',
  25749. '};',
  25750. 'this.Notify = null;',
  25751. 'this.Event = null;',
  25752. 'this.Getter = null;',
  25753. 'this.ProcA = null;',
  25754. 'this.FuncB = null;',
  25755. 'this.p = null;',
  25756. '']),
  25757. LinesToStr([ // $mod.$main
  25758. '$mod.Notify = $mod.Event;',
  25759. '$mod.Event = $mod.Event;',
  25760. '$mod.Event = $mod.Notify;',
  25761. '$mod.Event = $mod.Getter;',
  25762. '$mod.Event = $mod.ProcA;',
  25763. '$mod.ProcA = $mod.FuncB;',
  25764. '$mod.FuncB = $mod.FuncB;',
  25765. '$mod.FuncB = $mod.ProcA;',
  25766. '$mod.FuncB = $mod.Getter;',
  25767. '$mod.ProcA = $mod.p;',
  25768. '$mod.FuncB = $mod.p;',
  25769. '$mod.Getter = $mod.p;',
  25770. '$mod.p = $mod.Notify;',
  25771. '$mod.p = $mod.Notify;',
  25772. '$mod.p = $mod.ProcA;',
  25773. '$mod.p = $mod.ProcA;',
  25774. '$mod.p = $mod.FuncB;',
  25775. '$mod.p = $mod.FuncB;',
  25776. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  25777. '']));
  25778. end;
  25779. procedure TTestModule.TestProcType_PassProcToUntyped;
  25780. begin
  25781. StartProgram(false);
  25782. Add([
  25783. 'type',
  25784. ' TEvent = procedure of object;',
  25785. ' TFunc = function: longint;',
  25786. 'procedure DoIt(); varargs; begin end;',
  25787. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  25788. 'var',
  25789. ' Event: tevent;',
  25790. ' Func: TFunc;',
  25791. 'begin',
  25792. ' doit(event,func);',
  25793. ' dosome(event,event,event);',
  25794. ' dosome(func,func,func);',
  25795. '']);
  25796. ConvertProgram;
  25797. CheckSource('TestProcType_PassProcToUntyped',
  25798. LinesToStr([ // statements
  25799. 'this.DoIt = function () {',
  25800. '};',
  25801. 'this.DoSome = function (a, b, p) {',
  25802. '};',
  25803. 'this.Event = null;',
  25804. 'this.Func = null;',
  25805. '']),
  25806. LinesToStr([ // $mod.$main
  25807. '$mod.DoIt($mod.Event, $mod.Func);',
  25808. '$mod.DoSome($mod.Event, {',
  25809. ' p: $mod,',
  25810. ' get: function () {',
  25811. ' return this.p.Event;',
  25812. ' },',
  25813. ' set: function (v) {',
  25814. ' this.p.Event = v;',
  25815. ' }',
  25816. '}, $mod.Event);',
  25817. '$mod.DoSome($mod.Func, {',
  25818. ' p: $mod,',
  25819. ' get: function () {',
  25820. ' return this.p.Func;',
  25821. ' },',
  25822. ' set: function (v) {',
  25823. ' this.p.Func = v;',
  25824. ' }',
  25825. '}, $mod.Func);',
  25826. '']));
  25827. end;
  25828. procedure TTestModule.TestProcType_PassProcToArray;
  25829. begin
  25830. StartProgram(false);
  25831. Add([
  25832. 'type',
  25833. ' TFunc = function: longint;',
  25834. ' TArrFunc = array of TFunc;',
  25835. 'procedure DoIt(Arr: TArrFunc); begin end;',
  25836. 'function GetIt: longint; begin end;',
  25837. 'var',
  25838. ' Func: tfunc;',
  25839. 'begin',
  25840. ' doit([]);',
  25841. ' doit([@GetIt]);',
  25842. ' doit([Func]);',
  25843. '']);
  25844. ConvertProgram;
  25845. CheckSource('TestProcType_PassProcToArray',
  25846. LinesToStr([ // statements
  25847. 'this.DoIt = function (Arr) {',
  25848. '};',
  25849. 'this.GetIt = function () {',
  25850. ' var Result = 0;',
  25851. ' return Result;',
  25852. '};',
  25853. 'this.Func = null;',
  25854. '']),
  25855. LinesToStr([ // $mod.$main
  25856. '$mod.DoIt([]);',
  25857. '$mod.DoIt([$mod.GetIt]);',
  25858. '$mod.DoIt([$mod.Func]);',
  25859. '']));
  25860. end;
  25861. procedure TTestModule.TestProcType_SafeCallObjFPC;
  25862. begin
  25863. StartProgram(false);
  25864. Add([
  25865. '{$modeswitch externalclass}',
  25866. 'type',
  25867. ' TProc = reference to procedure(i: longint); safecall;',
  25868. ' TEvent = procedure(i: longint) of object; safecall;',
  25869. ' TExtA = class external name ''ExtObj''',
  25870. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  25871. ' procedure DoSome(Id: longint = 1);',
  25872. ' procedure SetOnClick(const e: TEvent);',
  25873. ' property OnClick: TEvent write SetOnClick;',
  25874. ' class procedure Fly(Id: longint = 1); static;',
  25875. ' procedure SetOnShow(const p: TProc);',
  25876. ' property OnShow: TProc write SetOnShow;',
  25877. ' end;',
  25878. 'procedure Run(i: longint = 1);',
  25879. 'begin',
  25880. 'end;',
  25881. 'var',
  25882. ' Obj: texta;',
  25883. ' e: TEvent;',
  25884. ' p: TProc;',
  25885. 'begin',
  25886. ' e:=e;',
  25887. ' e:[email protected];',
  25888. ' e:[email protected];',
  25889. ' e:=TEvent(@obj.dosome);', // no safecall
  25890. ' obj.OnClick:[email protected];',
  25891. ' obj.OnClick:[email protected];',
  25892. ' obj.setonclick(@obj.doit);',
  25893. ' obj.setonclick(@obj.dosome);',
  25894. ' p:=@Run;',
  25895. ' p:[email protected];',
  25896. ' obj.OnShow:=@Run;',
  25897. ' obj.OnShow:[email protected];',
  25898. ' obj.setOnShow(@Run);',
  25899. ' obj.setOnShow(@TExtA.Fly);',
  25900. ' with obj do begin',
  25901. ' e:=@doit;',
  25902. ' e:=@dosome;',
  25903. ' OnClick:=@doit;',
  25904. ' OnClick:=@dosome;',
  25905. ' setonclick(@doit);',
  25906. ' setonclick(@dosome);',
  25907. ' OnShow:=@Run;',
  25908. ' setOnShow(@Run);',
  25909. ' end;']);
  25910. ConvertProgram;
  25911. CheckSource('TestProcType_SafeCallObjFPC',
  25912. LinesToStr([ // statements
  25913. 'this.Run = function (i) {',
  25914. '};',
  25915. 'this.Obj = null;',
  25916. 'this.e = null;',
  25917. 'this.p = null;',
  25918. '']),
  25919. LinesToStr([ // $mod.$main
  25920. '$mod.e = $mod.e;',
  25921. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  25922. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  25923. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  25924. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  25925. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  25926. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  25927. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  25928. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  25929. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  25930. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  25931. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  25932. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  25933. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  25934. 'var $with1 = $mod.Obj;',
  25935. '$mod.e = rtl.createSafeCallback($with1, "$Execute");',
  25936. '$mod.e = rtl.createSafeCallback($with1, "DoSome");',
  25937. '$with1.SetOnClick(rtl.createSafeCallback($with1, "$Execute"));',
  25938. '$with1.SetOnClick(rtl.createSafeCallback($with1, "DoSome"));',
  25939. '$with1.SetOnClick(rtl.createSafeCallback($with1, "$Execute"));',
  25940. '$with1.SetOnClick(rtl.createSafeCallback($with1, "DoSome"));',
  25941. '$with1.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  25942. '$with1.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  25943. '']));
  25944. end;
  25945. procedure TTestModule.TestProcType_SafeCallDelphi;
  25946. begin
  25947. StartProgram(false);
  25948. Add([
  25949. '{$mode delphi}',
  25950. '{$modeswitch externalclass}',
  25951. 'type',
  25952. ' TProc = reference to procedure(i: longint); safecall;',
  25953. ' TEvent = procedure(i: longint) of object; safecall;',
  25954. ' TExtA = class external name ''ExtObj''',
  25955. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  25956. ' procedure DoSome(Id: longint = 1);',
  25957. ' procedure SetOnClick(const e: TEvent);',
  25958. ' property OnClick: TEvent write SetOnClick;',
  25959. ' class procedure Fly(Id: longint = 1); static;',
  25960. ' procedure SetOnShow(const p: TProc);',
  25961. ' property OnShow: TProc write SetOnShow;',
  25962. ' end;',
  25963. 'procedure Run(i: longint = 1);',
  25964. 'begin',
  25965. 'end;',
  25966. 'var',
  25967. ' Obj: texta;',
  25968. ' e: TEvent;',
  25969. ' p: TProc;',
  25970. 'begin',
  25971. ' e:=e;',
  25972. ' e:=obj.doit;',
  25973. ' e:=obj.dosome;',
  25974. ' e:=TEvent(@obj.dosome);', // no safecall
  25975. ' obj.OnClick:=obj.doit;',
  25976. ' obj.OnClick:=obj.dosome;',
  25977. ' obj.setonclick(obj.doit);',
  25978. ' obj.setonclick(obj.dosome);',
  25979. ' p:=Run;',
  25980. ' p:=TExtA.Fly;',
  25981. ' obj.OnShow:=Run;',
  25982. ' obj.OnShow:=TExtA.Fly;',
  25983. ' obj.setOnShow(Run);',
  25984. ' obj.setOnShow(TExtA.Fly);',
  25985. ' with obj do begin',
  25986. ' e:=doit;',
  25987. ' e:=dosome;',
  25988. ' OnClick:=doit;',
  25989. ' OnClick:=dosome;',
  25990. ' setonclick(doit);',
  25991. ' setonclick(dosome);',
  25992. ' OnShow:=@Run;',
  25993. ' setOnShow(@Run);',
  25994. ' end;']);
  25995. ConvertProgram;
  25996. CheckSource('TestProcType_SafeCallDelphi',
  25997. LinesToStr([ // statements
  25998. 'this.Run = function (i) {',
  25999. '};',
  26000. 'this.Obj = null;',
  26001. 'this.e = null;',
  26002. 'this.p = null;',
  26003. '']),
  26004. LinesToStr([ // $mod.$main
  26005. '$mod.e = $mod.e;',
  26006. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26007. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26008. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26009. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26010. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26011. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26012. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26013. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26014. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26015. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26016. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26017. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26018. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26019. 'var $with1 = $mod.Obj;',
  26020. '$mod.e = rtl.createSafeCallback($with1, "$Execute");',
  26021. '$mod.e = rtl.createSafeCallback($with1, "DoSome");',
  26022. '$with1.SetOnClick(rtl.createSafeCallback($with1, "$Execute"));',
  26023. '$with1.SetOnClick(rtl.createSafeCallback($with1, "DoSome"));',
  26024. '$with1.SetOnClick(rtl.createSafeCallback($with1, "$Execute"));',
  26025. '$with1.SetOnClick(rtl.createSafeCallback($with1, "DoSome"));',
  26026. '$with1.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26027. '$with1.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26028. '']));
  26029. end;
  26030. procedure TTestModule.TestPointer;
  26031. begin
  26032. StartProgram(false);
  26033. Add(['type',
  26034. ' TObject = class end;',
  26035. ' TClass = class of TObject;',
  26036. ' TArrInt = array of longint;',
  26037. 'const',
  26038. ' n = nil;',
  26039. 'var',
  26040. ' v: jsvalue;',
  26041. ' Obj: tobject;',
  26042. ' C: tclass;',
  26043. ' a: tarrint;',
  26044. ' p: Pointer = nil;',
  26045. ' s: string;',
  26046. 'begin',
  26047. ' p:=p;',
  26048. ' p:=nil;',
  26049. ' if p=nil then;',
  26050. ' if nil=p then;',
  26051. ' if Assigned(p) then;',
  26052. ' p:=Pointer(v);',
  26053. ' p:=obj;',
  26054. ' p:=c;',
  26055. ' p:=a;',
  26056. ' p:=tobject;',
  26057. ' obj:=TObject(p);',
  26058. ' c:=TClass(p);',
  26059. ' a:=TArrInt(p);',
  26060. ' p:=n;',
  26061. ' p:=Pointer(a);',
  26062. ' p:=pointer(s);',
  26063. ' s:=string(p);',
  26064. '']);
  26065. ConvertProgram;
  26066. CheckSource('TestPointer',
  26067. LinesToStr([ // statements
  26068. 'rtl.createClass($mod, "TObject", null, function () {',
  26069. ' this.$init = function () {',
  26070. ' };',
  26071. ' this.$final = function () {',
  26072. ' };',
  26073. '});',
  26074. 'this.n = null;',
  26075. 'this.v = undefined;',
  26076. 'this.Obj = null;',
  26077. 'this.C = null;',
  26078. 'this.a = [];',
  26079. 'this.p = null;',
  26080. 'this.s = "";',
  26081. '']),
  26082. LinesToStr([ // $mod.$main
  26083. '$mod.p = $mod.p;',
  26084. '$mod.p = null;',
  26085. 'if ($mod.p === null) ;',
  26086. 'if (null === $mod.p) ;',
  26087. 'if ($mod.p != null) ;',
  26088. '$mod.p = $mod.v;',
  26089. '$mod.p = $mod.Obj;',
  26090. '$mod.p = $mod.C;',
  26091. '$mod.p = $mod.a;',
  26092. '$mod.p = $mod.TObject;',
  26093. '$mod.Obj = $mod.p;',
  26094. '$mod.C = $mod.p;',
  26095. '$mod.a = $mod.p;',
  26096. '$mod.p = null;',
  26097. '$mod.p = $mod.a;',
  26098. '$mod.p = $mod.s;',
  26099. '$mod.s = $mod.p;',
  26100. '']));
  26101. end;
  26102. procedure TTestModule.TestPointer_Proc;
  26103. begin
  26104. StartProgram(false);
  26105. Add('type');
  26106. Add(' TObject = class');
  26107. Add(' procedure DoIt; virtual; abstract;');
  26108. Add(' end;');
  26109. Add('procedure DoSome; begin end;');
  26110. Add('var');
  26111. Add(' o: TObject;');
  26112. Add(' p: Pointer;');
  26113. Add('begin');
  26114. Add(' p:=@DoSome;');
  26115. Add(' p:[email protected];');
  26116. ConvertProgram;
  26117. CheckSource('TestPointer_Proc',
  26118. LinesToStr([ // statements
  26119. 'rtl.createClass($mod, "TObject", null, function () {',
  26120. ' this.$init = function () {',
  26121. ' };',
  26122. ' this.$final = function () {',
  26123. ' };',
  26124. '});',
  26125. 'this.DoSome = function () {',
  26126. '};',
  26127. 'this.o = null;',
  26128. 'this.p = null;',
  26129. '']),
  26130. LinesToStr([ // $mod.$main
  26131. '$mod.p = $mod.DoSome;',
  26132. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26133. '']));
  26134. end;
  26135. procedure TTestModule.TestPointer_AssignRecordFail;
  26136. begin
  26137. StartProgram(false);
  26138. Add('type');
  26139. Add(' TRec = record end;');
  26140. Add('var');
  26141. Add(' p: Pointer;');
  26142. Add(' r: TRec;');
  26143. Add('begin');
  26144. Add(' p:=r;');
  26145. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26146. nIncompatibleTypesGotExpected);
  26147. ConvertProgram;
  26148. end;
  26149. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26150. begin
  26151. StartProgram(false);
  26152. Add('type');
  26153. Add(' TArr = array[boolean] of longint;');
  26154. Add('var');
  26155. Add(' p: Pointer;');
  26156. Add(' a: TArr;');
  26157. Add('begin');
  26158. Add(' p:=a;');
  26159. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26160. nIncompatibleTypesGotExpected);
  26161. ConvertProgram;
  26162. end;
  26163. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26164. begin
  26165. StartProgram(false);
  26166. Add([
  26167. 'procedure DoIt(args: array of jsvalue); begin end;',
  26168. 'procedure DoAll; varargs; begin end;',
  26169. 'var',
  26170. ' v: jsvalue;',
  26171. 'begin',
  26172. ' DoIt([pointer(v)]);',
  26173. ' DoAll(pointer(v));',
  26174. '']);
  26175. ConvertProgram;
  26176. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26177. LinesToStr([ // statements
  26178. 'this.DoIt = function (args) {',
  26179. '};',
  26180. 'this.DoAll = function () {',
  26181. '};',
  26182. 'this.v = undefined;',
  26183. '']),
  26184. LinesToStr([ // $mod.$main
  26185. '$mod.DoIt([$mod.v]);',
  26186. '$mod.DoAll($mod.v);',
  26187. '']));
  26188. end;
  26189. procedure TTestModule.TestPointer_NonRecordFail;
  26190. begin
  26191. StartProgram(false);
  26192. Add([
  26193. 'type',
  26194. ' p = ^longint;',
  26195. 'begin',
  26196. '']);
  26197. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26198. ConvertProgram;
  26199. end;
  26200. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26201. begin
  26202. StartProgram(false);
  26203. Add([
  26204. 'procedure DoIt(p: ^longint); begin end;',
  26205. 'begin',
  26206. '']);
  26207. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26208. ConvertProgram;
  26209. end;
  26210. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26211. begin
  26212. StartProgram(false);
  26213. Add([
  26214. 'var p: ^longint;',
  26215. 'begin',
  26216. '']);
  26217. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26218. ConvertProgram;
  26219. end;
  26220. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26221. begin
  26222. StartProgram(false);
  26223. Add([
  26224. 'function DoIt: ^longint; begin end;',
  26225. 'begin',
  26226. '']);
  26227. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26228. ConvertProgram;
  26229. end;
  26230. procedure TTestModule.TestPointer_AddrOperatorFail;
  26231. begin
  26232. StartProgram(false);
  26233. Add([
  26234. 'var i: longint;',
  26235. 'begin',
  26236. ' if @i=nil then ;',
  26237. '']);
  26238. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26239. ConvertProgram;
  26240. end;
  26241. procedure TTestModule.TestPointer_ArrayParamsFail;
  26242. begin
  26243. StartProgram(false);
  26244. Add([
  26245. 'var',
  26246. ' p: Pointer;',
  26247. 'begin',
  26248. ' p:=p[1];',
  26249. '']);
  26250. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26251. ConvertProgram;
  26252. end;
  26253. procedure TTestModule.TestPointer_PointerAddFail;
  26254. begin
  26255. StartProgram(false);
  26256. Add([
  26257. 'var',
  26258. ' p: Pointer;',
  26259. 'begin',
  26260. ' p:=p+1;',
  26261. '']);
  26262. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26263. ConvertProgram;
  26264. end;
  26265. procedure TTestModule.TestPointer_IncPointerFail;
  26266. begin
  26267. StartProgram(false);
  26268. Add([
  26269. 'var',
  26270. ' p: Pointer;',
  26271. 'begin',
  26272. ' inc(p,1);',
  26273. '']);
  26274. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26275. nIncompatibleTypeArgNo);
  26276. ConvertProgram;
  26277. end;
  26278. procedure TTestModule.TestPointer_Record;
  26279. begin
  26280. StartProgram(false);
  26281. Add([
  26282. 'type',
  26283. ' TRec = record x: longint; end;',
  26284. ' PRec = ^TRec;',
  26285. 'var',
  26286. ' r: TRec;',
  26287. ' p: PRec;',
  26288. ' q: ^TRec;',
  26289. ' Ptr: pointer;',
  26290. 'begin',
  26291. ' new(p);',
  26292. ' p:=@r;',
  26293. ' r:=p^;',
  26294. ' r.x:=p^.x;',
  26295. ' p^.x:=r.x;',
  26296. ' if p^.x=3 then ;',
  26297. ' if 4=p^.x then ;',
  26298. ' dispose(p);',
  26299. ' new(q);',
  26300. ' dispose(q);',
  26301. ' Ptr:=p;',
  26302. ' p:=PRec(ptr);',
  26303. '']);
  26304. ConvertProgram;
  26305. CheckSource('TestPointer_Record',
  26306. LinesToStr([ // statements
  26307. 'rtl.recNewT($mod, "TRec", function () {',
  26308. ' this.x = 0;',
  26309. ' this.$eq = function (b) {',
  26310. ' return this.x === b.x;',
  26311. ' };',
  26312. ' this.$assign = function (s) {',
  26313. ' this.x = s.x;',
  26314. ' return this;',
  26315. ' };',
  26316. '});',
  26317. 'this.r = $mod.TRec.$new();',
  26318. 'this.p = null;',
  26319. 'this.q = null;',
  26320. 'this.Ptr = null;',
  26321. '']),
  26322. LinesToStr([ // $mod.$main
  26323. '$mod.p = $mod.TRec.$new();',
  26324. '$mod.p = $mod.r;',
  26325. '$mod.r.$assign($mod.p);',
  26326. '$mod.r.x = $mod.p.x;',
  26327. '$mod.p.x = $mod.r.x;',
  26328. 'if ($mod.p.x === 3) ;',
  26329. 'if (4 === $mod.p.x) ;',
  26330. '$mod.p = null;',
  26331. '$mod.q = $mod.TRec.$new();',
  26332. '$mod.q = null;',
  26333. '$mod.Ptr = $mod.p;',
  26334. '$mod.p = $mod.Ptr;',
  26335. '']));
  26336. end;
  26337. procedure TTestModule.TestPointer_RecordArg;
  26338. begin
  26339. StartProgram(false);
  26340. Add([
  26341. '{$modeswitch autoderef}',
  26342. 'type',
  26343. ' TRec = record x: longint; end;',
  26344. ' PRec = ^TRec;',
  26345. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26346. 'begin',
  26347. ' a.x:=a.x;',
  26348. ' a^.x:=a^.x;',
  26349. ' with a^ do',
  26350. ' x:=x;',
  26351. 'end;',
  26352. 'function GetIt(p: PRec): PRec;',
  26353. 'begin',
  26354. ' p.x:=p.x;',
  26355. ' p^.x:=p^.x;',
  26356. ' with p^ do',
  26357. ' x:=x;',
  26358. 'end;',
  26359. 'var',
  26360. ' r: TRec;',
  26361. ' p: PRec;',
  26362. 'begin',
  26363. ' p:=GetIt(p);',
  26364. ' p^:=GetIt(@r)^;',
  26365. ' DoIt(p,p,p);',
  26366. ' DoIt(@r,p,p);',
  26367. '']);
  26368. ConvertProgram;
  26369. CheckSource('TestPointer_Record',
  26370. LinesToStr([ // statements
  26371. 'rtl.recNewT($mod, "TRec", function () {',
  26372. ' this.x = 0;',
  26373. ' this.$eq = function (b) {',
  26374. ' return this.x === b.x;',
  26375. ' };',
  26376. ' this.$assign = function (s) {',
  26377. ' this.x = s.x;',
  26378. ' return this;',
  26379. ' };',
  26380. '});',
  26381. 'this.DoIt = function (a, b, c) {',
  26382. ' var Result = $mod.TRec.$new();',
  26383. ' a.x = a.x;',
  26384. ' a.x = a.x;',
  26385. ' a.x = a.x;',
  26386. ' return Result;',
  26387. '};',
  26388. 'this.GetIt = function (p) {',
  26389. ' var Result = null;',
  26390. ' p.x = p.x;',
  26391. ' p.x = p.x;',
  26392. ' p.x = p.x;',
  26393. ' return Result;',
  26394. '};',
  26395. 'this.r = $mod.TRec.$new();',
  26396. 'this.p = null;',
  26397. '']),
  26398. LinesToStr([ // $mod.$main
  26399. '$mod.p = $mod.GetIt($mod.p);',
  26400. '$mod.p.$assign($mod.GetIt($mod.r));',
  26401. '$mod.DoIt($mod.p, {',
  26402. ' p: $mod,',
  26403. ' get: function () {',
  26404. ' return this.p.p;',
  26405. ' },',
  26406. ' set: function (v) {',
  26407. ' this.p.p = v;',
  26408. ' }',
  26409. '}, {',
  26410. ' p: $mod,',
  26411. ' get: function () {',
  26412. ' return this.p.p;',
  26413. ' },',
  26414. ' set: function (v) {',
  26415. ' this.p.p = v;',
  26416. ' }',
  26417. '});',
  26418. '$mod.DoIt($mod.r, {',
  26419. ' p: $mod,',
  26420. ' get: function () {',
  26421. ' return this.p.p;',
  26422. ' },',
  26423. ' set: function (v) {',
  26424. ' this.p.p = v;',
  26425. ' }',
  26426. '}, {',
  26427. ' p: $mod,',
  26428. ' get: function () {',
  26429. ' return this.p.p;',
  26430. ' },',
  26431. ' set: function (v) {',
  26432. ' this.p.p = v;',
  26433. ' }',
  26434. '});',
  26435. '']));
  26436. end;
  26437. procedure TTestModule.TestJSValue_AssignToJSValue;
  26438. begin
  26439. StartProgram(false);
  26440. Add('var');
  26441. Add(' v: jsvalue;');
  26442. Add(' i: longint;');
  26443. Add(' s: string;');
  26444. Add(' b: boolean;');
  26445. Add(' d: double;');
  26446. Add(' p: pointer;');
  26447. Add('begin');
  26448. Add(' v:=v;');
  26449. Add(' v:=1;');
  26450. Add(' v:=i;');
  26451. Add(' v:='''';');
  26452. Add(' v:=''c'';');
  26453. Add(' v:=''foo'';');
  26454. Add(' v:=s;');
  26455. Add(' v:=false;');
  26456. Add(' v:=true;');
  26457. Add(' v:=b;');
  26458. Add(' v:=0.1;');
  26459. Add(' v:=d;');
  26460. Add(' v:=nil;');
  26461. Add(' v:=p;');
  26462. ConvertProgram;
  26463. CheckSource('TestJSValue_AssignToJSValue',
  26464. LinesToStr([ // statements
  26465. 'this.v = undefined;',
  26466. 'this.i = 0;',
  26467. 'this.s = "";',
  26468. 'this.b = false;',
  26469. 'this.d = 0.0;',
  26470. 'this.p = null;',
  26471. '']),
  26472. LinesToStr([ // $mod.$main
  26473. '$mod.v = $mod.v;',
  26474. '$mod.v = 1;',
  26475. '$mod.v = $mod.i;',
  26476. '$mod.v = "";',
  26477. '$mod.v = "c";',
  26478. '$mod.v = "foo";',
  26479. '$mod.v = $mod.s;',
  26480. '$mod.v = false;',
  26481. '$mod.v = true;',
  26482. '$mod.v = $mod.b;',
  26483. '$mod.v = 0.1;',
  26484. '$mod.v = $mod.d;',
  26485. '$mod.v = null;',
  26486. '$mod.v = $mod.p;',
  26487. '']));
  26488. end;
  26489. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26490. begin
  26491. StartProgram(false);
  26492. Add('type');
  26493. Add(' integer = longint;');
  26494. Add(' TYesNo = boolean;');
  26495. Add(' TFloat = double;');
  26496. Add(' TCaption = string;');
  26497. Add(' TChar = char;');
  26498. Add('var');
  26499. Add(' v: jsvalue;');
  26500. Add(' i: integer;');
  26501. Add(' s: TCaption;');
  26502. Add(' b: TYesNo;');
  26503. Add(' d: TFloat;');
  26504. Add(' c: char;');
  26505. Add('begin');
  26506. Add(' i:=longint(v);');
  26507. Add(' i:=integer(v);');
  26508. Add(' s:=string(v);');
  26509. Add(' s:=TCaption(v);');
  26510. Add(' b:=boolean(v);');
  26511. Add(' b:=TYesNo(v);');
  26512. Add(' d:=double(v);');
  26513. Add(' d:=TFloat(v);');
  26514. Add(' c:=char(v);');
  26515. Add(' c:=TChar(v);');
  26516. ConvertProgram;
  26517. CheckSource('TestJSValue_TypeCastToBaseType',
  26518. LinesToStr([ // statements
  26519. 'this.v = undefined;',
  26520. 'this.i = 0;',
  26521. 'this.s = "";',
  26522. 'this.b = false;',
  26523. 'this.d = 0.0;',
  26524. 'this.c = "";',
  26525. '']),
  26526. LinesToStr([ // $mod.$main
  26527. '$mod.i = Math.floor($mod.v);',
  26528. '$mod.i = Math.floor($mod.v);',
  26529. '$mod.s = "" + $mod.v;',
  26530. '$mod.s = "" + $mod.v;',
  26531. '$mod.b = !($mod.v == false);',
  26532. '$mod.b = !($mod.v == false);',
  26533. '$mod.d = rtl.getNumber($mod.v);',
  26534. '$mod.d = rtl.getNumber($mod.v);',
  26535. '$mod.c = rtl.getChar($mod.v);',
  26536. '$mod.c = rtl.getChar($mod.v);',
  26537. '']));
  26538. end;
  26539. procedure TTestModule.TestJSValue_TypecastToJSValue;
  26540. begin
  26541. StartProgram(false);
  26542. Add([
  26543. 'type',
  26544. ' TArr = array of word;',
  26545. ' TRec = record end;',
  26546. ' TSet = set of boolean;',
  26547. 'procedure Fly(v: jsvalue);',
  26548. 'begin',
  26549. 'end;',
  26550. 'var',
  26551. ' a: TArr;',
  26552. ' r: TRec;',
  26553. ' s: TSet;',
  26554. 'begin',
  26555. ' Fly(jsvalue(a));',
  26556. ' Fly(jsvalue(r));',
  26557. ' Fly(jsvalue(s));',
  26558. '']);
  26559. ConvertProgram;
  26560. CheckSource('TestJSValue_TypecastToJSValue',
  26561. LinesToStr([ // statements
  26562. 'rtl.recNewT($mod, "TRec", function () {',
  26563. ' this.$eq = function (b) {',
  26564. ' return true;',
  26565. ' };',
  26566. ' this.$assign = function (s) {',
  26567. ' return this;',
  26568. ' };',
  26569. '});',
  26570. 'this.Fly = function (v) {',
  26571. '};',
  26572. 'this.a = [];',
  26573. 'this.r = $mod.TRec.$new();',
  26574. 'this.s = {};',
  26575. '']),
  26576. LinesToStr([ // $mod.$main
  26577. '$mod.Fly($mod.a);',
  26578. '$mod.Fly($mod.r);',
  26579. '$mod.Fly($mod.s);',
  26580. '']));
  26581. end;
  26582. procedure TTestModule.TestJSValue_Equal;
  26583. begin
  26584. StartProgram(false);
  26585. Add('type');
  26586. Add(' integer = longint;');
  26587. Add(' TYesNo = boolean;');
  26588. Add(' TFloat = double;');
  26589. Add(' TCaption = string;');
  26590. Add(' TChar = char;');
  26591. Add(' TMulti = JSValue;');
  26592. Add('var');
  26593. Add(' v: jsvalue;');
  26594. Add(' i: integer;');
  26595. Add(' s: TCaption;');
  26596. Add(' b: TYesNo;');
  26597. Add(' d: TFloat;');
  26598. Add(' c: char;');
  26599. Add(' m: TMulti;');
  26600. Add('begin');
  26601. Add(' b:=v=v;');
  26602. Add(' b:=v<>v;');
  26603. Add(' b:=v=1;');
  26604. Add(' b:=v<>1;');
  26605. Add(' b:=2=v;');
  26606. Add(' b:=2<>v;');
  26607. Add(' b:=v=i;');
  26608. Add(' b:=i=v;');
  26609. Add(' b:=v=nil;');
  26610. Add(' b:=nil=v;');
  26611. Add(' b:=v=false;');
  26612. Add(' b:=true=v;');
  26613. Add(' b:=v=b;');
  26614. Add(' b:=b=v;');
  26615. Add(' b:=v=s;');
  26616. Add(' b:=s=v;');
  26617. Add(' b:=v=''foo'';');
  26618. Add(' b:=''''=v;');
  26619. Add(' b:=v=d;');
  26620. Add(' b:=d=v;');
  26621. Add(' b:=v=3.4;');
  26622. Add(' b:=5.6=v;');
  26623. Add(' b:=v=c;');
  26624. Add(' b:=c=v;');
  26625. Add(' b:=m=m;');
  26626. Add(' b:=v=m;');
  26627. Add(' b:=m=v;');
  26628. ConvertProgram;
  26629. CheckSource('TestJSValue_Equal',
  26630. LinesToStr([ // statements
  26631. 'this.v = undefined;',
  26632. 'this.i = 0;',
  26633. 'this.s = "";',
  26634. 'this.b = false;',
  26635. 'this.d = 0.0;',
  26636. 'this.c = "";',
  26637. 'this.m = undefined;',
  26638. '']),
  26639. LinesToStr([ // $mod.$main
  26640. '$mod.b = $mod.v == $mod.v;',
  26641. '$mod.b = $mod.v != $mod.v;',
  26642. '$mod.b = $mod.v == 1;',
  26643. '$mod.b = $mod.v != 1;',
  26644. '$mod.b = 2 == $mod.v;',
  26645. '$mod.b = 2 != $mod.v;',
  26646. '$mod.b = $mod.v == $mod.i;',
  26647. '$mod.b = $mod.i == $mod.v;',
  26648. '$mod.b = $mod.v == null;',
  26649. '$mod.b = null == $mod.v;',
  26650. '$mod.b = $mod.v == false;',
  26651. '$mod.b = true == $mod.v;',
  26652. '$mod.b = $mod.v == $mod.b;',
  26653. '$mod.b = $mod.b == $mod.v;',
  26654. '$mod.b = $mod.v == $mod.s;',
  26655. '$mod.b = $mod.s == $mod.v;',
  26656. '$mod.b = $mod.v == "foo";',
  26657. '$mod.b = "" == $mod.v;',
  26658. '$mod.b = $mod.v == $mod.d;',
  26659. '$mod.b = $mod.d == $mod.v;',
  26660. '$mod.b = $mod.v == 3.4;',
  26661. '$mod.b = 5.6 == $mod.v;',
  26662. '$mod.b = $mod.v == $mod.c;',
  26663. '$mod.b = $mod.c == $mod.v;',
  26664. '$mod.b = $mod.m == $mod.m;',
  26665. '$mod.b = $mod.v == $mod.m;',
  26666. '$mod.b = $mod.m == $mod.v;',
  26667. '']));
  26668. end;
  26669. procedure TTestModule.TestJSValue_If;
  26670. begin
  26671. StartProgram(false);
  26672. Add([
  26673. 'procedure Fly(var u);',
  26674. 'begin',
  26675. ' if jsvalue(u) then ;',
  26676. 'end;',
  26677. 'var',
  26678. ' v: jsvalue;',
  26679. 'begin',
  26680. ' if v then ;',
  26681. ' while v do ;',
  26682. ' repeat until v;',
  26683. '']);
  26684. ConvertProgram;
  26685. CheckSource('TestJSValue_If',
  26686. LinesToStr([ // statements
  26687. 'this.Fly = function (u) {',
  26688. ' if (u.get()) ;',
  26689. '};',
  26690. 'this.v = undefined;',
  26691. '']),
  26692. LinesToStr([ // $mod.$main
  26693. 'if ($mod.v) ;',
  26694. 'while($mod.v){',
  26695. '};',
  26696. 'do{',
  26697. '} while(!$mod.v);',
  26698. '']));
  26699. end;
  26700. procedure TTestModule.TestJSValue_Not;
  26701. begin
  26702. StartProgram(false);
  26703. Add([
  26704. 'var',
  26705. ' v: jsvalue;',
  26706. ' b: boolean;',
  26707. 'begin',
  26708. ' b:=not v;',
  26709. ' if not v then ;',
  26710. ' while not v do ;',
  26711. ' repeat until not v;',
  26712. '']);
  26713. ConvertProgram;
  26714. CheckSource('TestJSValue_If',
  26715. LinesToStr([ // statements
  26716. 'this.v = undefined;',
  26717. 'this.b = false;',
  26718. '']),
  26719. LinesToStr([ // $mod.$main
  26720. '$mod.b=!$mod.v;',
  26721. 'if (!$mod.v) ;',
  26722. 'while(!$mod.v){',
  26723. '};',
  26724. 'do{',
  26725. '} while($mod.v);',
  26726. '']));
  26727. end;
  26728. procedure TTestModule.TestJSValue_Enum;
  26729. begin
  26730. StartProgram(false);
  26731. Add('type');
  26732. Add(' TColor = (red, blue);');
  26733. Add(' TRedBlue = TColor;');
  26734. Add('var');
  26735. Add(' v: jsvalue;');
  26736. Add(' e: TColor;');
  26737. Add('begin');
  26738. Add(' v:=e;');
  26739. Add(' v:=TColor(e);');
  26740. Add(' v:=TRedBlue(e);');
  26741. Add(' e:=TColor(v);');
  26742. Add(' e:=TRedBlue(v);');
  26743. ConvertProgram;
  26744. CheckSource('TestJSValue_Enum',
  26745. LinesToStr([ // statements
  26746. 'this.TColor = {',
  26747. ' "0": "red",',
  26748. ' red: 0,',
  26749. ' "1": "blue",',
  26750. ' blue: 1',
  26751. '};',
  26752. 'this.v = undefined;',
  26753. 'this.e = 0;',
  26754. '']),
  26755. LinesToStr([ // $mod.$main
  26756. '$mod.v = $mod.e;',
  26757. '$mod.v = $mod.e;',
  26758. '$mod.v = $mod.e;',
  26759. '$mod.e = $mod.v;',
  26760. '$mod.e = $mod.v;',
  26761. '']));
  26762. end;
  26763. procedure TTestModule.TestJSValue_ClassInstance;
  26764. begin
  26765. StartProgram(false);
  26766. Add([
  26767. 'type',
  26768. ' TObject = class',
  26769. ' end;',
  26770. ' TBirdObject = TObject;',
  26771. 'var',
  26772. ' v: jsvalue;',
  26773. ' o: TObject;',
  26774. 'begin',
  26775. ' v:=o;',
  26776. ' v:=TObject(o);',
  26777. ' v:=TBirdObject(o);',
  26778. ' o:=TObject(v);',
  26779. ' o:=TBirdObject(v);',
  26780. ' if v is TObject then ;',
  26781. '']);
  26782. ConvertProgram;
  26783. CheckSource('TestJSValue_ClassInstance',
  26784. LinesToStr([ // statements
  26785. 'rtl.createClass($mod, "TObject", null, function () {',
  26786. ' this.$init = function () {',
  26787. ' };',
  26788. ' this.$final = function () {',
  26789. ' };',
  26790. '});',
  26791. 'this.v = undefined;',
  26792. 'this.o = null;',
  26793. '']),
  26794. LinesToStr([ // $mod.$main
  26795. '$mod.v = $mod.o;',
  26796. '$mod.v = $mod.o;',
  26797. '$mod.v = $mod.o;',
  26798. '$mod.o = rtl.getObject($mod.v);',
  26799. '$mod.o = rtl.getObject($mod.v);',
  26800. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  26801. '']));
  26802. end;
  26803. procedure TTestModule.TestJSValue_ClassOf;
  26804. begin
  26805. StartProgram(false);
  26806. Add([
  26807. 'type',
  26808. ' TClass = class of TObject;',
  26809. ' TObject = class',
  26810. ' end;',
  26811. ' TBirds = class of TBird;',
  26812. ' TBird = class(TObject) end;',
  26813. 'var',
  26814. ' v: jsvalue;',
  26815. ' c: TClass;',
  26816. 'begin',
  26817. ' v:=c;',
  26818. ' v:=TObject;',
  26819. ' v:=TClass(c);',
  26820. ' v:=TBirds(c);',
  26821. ' c:=TClass(v);',
  26822. ' c:=TBirds(v);',
  26823. ' if v is TClass then ;',
  26824. '']);
  26825. ConvertProgram;
  26826. CheckSource('TestJSValue_ClassOf',
  26827. LinesToStr([ // statements
  26828. 'rtl.createClass($mod, "TObject", null, function () {',
  26829. ' this.$init = function () {',
  26830. ' };',
  26831. ' this.$final = function () {',
  26832. ' };',
  26833. '});',
  26834. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26835. '});',
  26836. 'this.v = undefined;',
  26837. 'this.c = null;',
  26838. '']),
  26839. LinesToStr([ // $mod.$main
  26840. '$mod.v = $mod.c;',
  26841. '$mod.v = $mod.TObject;',
  26842. '$mod.v = $mod.c;',
  26843. '$mod.v = $mod.c;',
  26844. '$mod.c = rtl.getObject($mod.v);',
  26845. '$mod.c = rtl.getObject($mod.v);',
  26846. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  26847. '']));
  26848. end;
  26849. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  26850. begin
  26851. StartProgram(false);
  26852. Add([
  26853. 'type',
  26854. ' integer = longint;',
  26855. ' TArray = array of JSValue;',
  26856. ' TArrgh = tarray;',
  26857. ' TArrInt = array of integer;',
  26858. 'var',
  26859. ' v: jsvalue;',
  26860. ' TheArray: tarray = (1,''2'');',
  26861. ' Arr: tarrgh;',
  26862. ' i: integer;',
  26863. ' ArrInt: tarrint;',
  26864. 'begin',
  26865. ' arr:=thearray;',
  26866. ' thearray:=arr;',
  26867. ' setlength(arr,2);',
  26868. ' setlength(thearray,3);',
  26869. ' arr[4]:=v;',
  26870. ' arr[5]:=length(thearray);',
  26871. ' arr[6]:=nil;',
  26872. ' arr[7]:=thearray[8];',
  26873. ' arr[low(arr)]:=high(thearray);',
  26874. ' arr:=arrint;',
  26875. ' arrInt:=tarrint(arr);',
  26876. ' if TheArray = nil then ;',
  26877. ' if nil = TheArray then ;',
  26878. ' if TheArray <> nil then ;',
  26879. ' if nil <> TheArray then ;',
  26880. '']);
  26881. ConvertProgram;
  26882. CheckSource('TestJSValue_ArrayOfJSValue',
  26883. LinesToStr([ // statements
  26884. 'this.v = undefined;',
  26885. 'this.TheArray = [1, "2"];',
  26886. 'this.Arr = [];',
  26887. 'this.i = 0;',
  26888. 'this.ArrInt = [];',
  26889. '']),
  26890. LinesToStr([ // $mod.$main
  26891. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  26892. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  26893. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  26894. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  26895. '$mod.Arr[4] = $mod.v;',
  26896. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  26897. '$mod.Arr[6] = null;',
  26898. '$mod.Arr[7] = $mod.TheArray[8];',
  26899. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  26900. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  26901. '$mod.ArrInt = $mod.Arr;',
  26902. 'if (rtl.length($mod.TheArray) === 0) ;',
  26903. 'if (rtl.length($mod.TheArray) === 0) ;',
  26904. 'if (rtl.length($mod.TheArray) > 0) ;',
  26905. 'if (rtl.length($mod.TheArray) > 0) ;',
  26906. '']));
  26907. end;
  26908. procedure TTestModule.TestJSValue_ArrayLit;
  26909. begin
  26910. StartProgram(false);
  26911. Add([
  26912. 'type',
  26913. ' TFlag = (big,small);',
  26914. ' TArray = array of JSValue;',
  26915. ' TObject = class end;',
  26916. ' TClass = class of TObject;',
  26917. 'var',
  26918. ' v: jsvalue;',
  26919. ' a: TArray;',
  26920. ' o: TObject;',
  26921. 'begin',
  26922. ' a:=[];',
  26923. ' a:=[1];',
  26924. ' a:=[1,2];',
  26925. ' a:=[big];',
  26926. ' a:=[1,big];',
  26927. ' a:=[o,nil];',
  26928. '']);
  26929. ConvertProgram;
  26930. CheckSource('TestJSValue_ArrayLit',
  26931. LinesToStr([ // statements
  26932. 'this.TFlag = {',
  26933. ' "0": "big",',
  26934. ' big: 0,',
  26935. ' "1": "small",',
  26936. ' small: 1',
  26937. '};',
  26938. 'rtl.createClass($mod, "TObject", null, function () {',
  26939. ' this.$init = function () {',
  26940. ' };',
  26941. ' this.$final = function () {',
  26942. ' };',
  26943. '});',
  26944. 'this.v = undefined;',
  26945. 'this.a = [];',
  26946. 'this.o = null;',
  26947. '']),
  26948. LinesToStr([ // $mod.$main
  26949. '$mod.a = [];',
  26950. '$mod.a = [1];',
  26951. '$mod.a = [1, 2];',
  26952. '$mod.a = [$mod.TFlag.big];',
  26953. '$mod.a = [1, $mod.TFlag.big];',
  26954. '$mod.a = [$mod.o, null];',
  26955. '']));
  26956. end;
  26957. procedure TTestModule.TestJSValue_Params;
  26958. begin
  26959. StartProgram(false);
  26960. Add('type');
  26961. Add(' integer = longint;');
  26962. Add(' TYesNo = boolean;');
  26963. Add(' TFloat = double;');
  26964. Add(' TCaption = string;');
  26965. Add(' TChar = char;');
  26966. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  26967. Add('var');
  26968. Add(' l: jsvalue;');
  26969. Add('begin');
  26970. Add(' a:=a;');
  26971. Add(' l:=b;');
  26972. Add(' c:=c;');
  26973. Add(' d:=d;');
  26974. Add(' Result:=l;');
  26975. Add('end;');
  26976. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  26977. Add('var');
  26978. Add(' v: jsvalue;');
  26979. Add(' i: integer;');
  26980. Add(' b: TYesNo;');
  26981. Add(' d: TFloat;');
  26982. Add(' s: TCaption;');
  26983. Add(' c: TChar;');
  26984. Add('begin');
  26985. Add(' v:=doit(v,v,v,v);');
  26986. Add(' i:=integer(dosome(i,i));');
  26987. Add(' b:=TYesNo(dosome(b,b));');
  26988. Add(' d:=TFloat(dosome(d,d));');
  26989. Add(' s:=TCaption(dosome(s,s));');
  26990. Add(' c:=TChar(dosome(c,c));');
  26991. ConvertProgram;
  26992. CheckSource('TestJSValue_Params',
  26993. LinesToStr([ // statements
  26994. 'this.DoIt = function (a, b, c, d) {',
  26995. ' var Result = undefined;',
  26996. ' var l = undefined;',
  26997. ' a = a;',
  26998. ' l = b;',
  26999. ' c.set(c.get());',
  27000. ' d.set(d.get());',
  27001. ' Result = l;',
  27002. ' return Result;',
  27003. '};',
  27004. 'this.DoSome = function (a, b) {',
  27005. ' var Result = undefined;',
  27006. ' return Result;',
  27007. '};',
  27008. 'this.v = undefined;',
  27009. 'this.i = 0;',
  27010. 'this.b = false;',
  27011. 'this.d = 0.0;',
  27012. 'this.s = "";',
  27013. 'this.c = "";',
  27014. '']),
  27015. LinesToStr([ // $mod.$main
  27016. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27017. ' p: $mod,',
  27018. ' get: function () {',
  27019. ' return this.p.v;',
  27020. ' },',
  27021. ' set: function (v) {',
  27022. ' this.p.v = v;',
  27023. ' }',
  27024. '}, {',
  27025. ' p: $mod,',
  27026. ' get: function () {',
  27027. ' return this.p.v;',
  27028. ' },',
  27029. ' set: function (v) {',
  27030. ' this.p.v = v;',
  27031. ' }',
  27032. '});',
  27033. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  27034. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27035. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27036. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27037. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27038. '']));
  27039. end;
  27040. procedure TTestModule.TestJSValue_UntypedParam;
  27041. begin
  27042. StartProgram(false);
  27043. Add('function DoIt(const a; var b; out c): jsvalue;');
  27044. Add('begin');
  27045. Add(' Result:=a;');
  27046. Add(' Result:=b;');
  27047. Add(' Result:=c;');
  27048. Add(' b:=Result;');
  27049. Add(' c:=Result;');
  27050. Add('end;');
  27051. Add('var i: longint;');
  27052. Add('begin');
  27053. Add(' doit(i,i,i);');
  27054. ConvertProgram;
  27055. CheckSource('TestJSValue_UntypedParam',
  27056. LinesToStr([ // statements
  27057. 'this.DoIt = function (a, b, c) {',
  27058. ' var Result = undefined;',
  27059. ' Result = a;',
  27060. ' Result = b.get();',
  27061. ' Result = c.get();',
  27062. ' b.set(Result);',
  27063. ' c.set(Result);',
  27064. ' return Result;',
  27065. '};',
  27066. 'this.i = 0;',
  27067. '']),
  27068. LinesToStr([ // $mod.$main
  27069. '$mod.DoIt($mod.i, {',
  27070. ' p: $mod,',
  27071. ' get: function () {',
  27072. ' return this.p.i;',
  27073. ' },',
  27074. ' set: function (v) {',
  27075. ' this.p.i = v;',
  27076. ' }',
  27077. '}, {',
  27078. ' p: $mod,',
  27079. ' get: function () {',
  27080. ' return this.p.i;',
  27081. ' },',
  27082. ' set: function (v) {',
  27083. ' this.p.i = v;',
  27084. ' }',
  27085. '});',
  27086. '']));
  27087. end;
  27088. procedure TTestModule.TestJSValue_FuncResultType;
  27089. begin
  27090. StartProgram(false);
  27091. Add('type');
  27092. Add(' integer = longint;');
  27093. Add(' TJSValueArray = array of JSValue;');
  27094. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27095. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27096. Add('begin');
  27097. Add(' while Compare(P,aList[0])>0 do ;');
  27098. Add('end;');
  27099. Add('var');
  27100. Add(' Compare: TListSortCompare;');
  27101. Add(' V: JSValue;');
  27102. Add(' i: integer;');
  27103. Add('begin');
  27104. Add(' if Compare(V,V)>0 then ;');
  27105. Add(' if Compare(i,i)>1 then ;');
  27106. Add(' if Compare(nil,false)>2 then ;');
  27107. Add(' if Compare(1,true)>3 then ;');
  27108. ConvertProgram;
  27109. CheckSource('TestJSValue_UntypedParam',
  27110. LinesToStr([ // statements
  27111. 'this.Sort = function (P, aList, Compare) {',
  27112. ' while (Compare(P, aList[0]) > 0) {',
  27113. ' };',
  27114. '};',
  27115. 'this.Compare = null;',
  27116. 'this.V = undefined;',
  27117. 'this.i = 0;',
  27118. '']),
  27119. LinesToStr([ // $mod.$main
  27120. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27121. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27122. 'if ($mod.Compare(null, false) > 2) ;',
  27123. 'if ($mod.Compare(1, true) > 3) ;',
  27124. '']));
  27125. end;
  27126. procedure TTestModule.TestJSValue_ProcType_Assign;
  27127. begin
  27128. StartProgram(false);
  27129. Add('type');
  27130. Add(' integer = longint;');
  27131. Add(' TObject = class');
  27132. Add(' class function GetGlob: integer;');
  27133. Add(' function Getter: integer;');
  27134. Add(' end;');
  27135. Add('class function TObject.GetGlob: integer;');
  27136. Add('var v1: jsvalue;');
  27137. Add('begin');
  27138. Add(' v1:=@GetGlob;');
  27139. Add(' v1:[email protected];');
  27140. Add('end;');
  27141. Add('function TObject.Getter: integer;');
  27142. Add('var v2: jsvalue;');
  27143. Add('begin');
  27144. Add(' v2:=@Getter;');
  27145. Add(' v2:[email protected];');
  27146. Add(' v2:=@GetGlob;');
  27147. Add(' v2:[email protected];');
  27148. Add('end;');
  27149. Add('function GetIt(i: integer): integer;');
  27150. Add('var v3: jsvalue;');
  27151. Add('begin');
  27152. Add(' v3:=@GetIt;');
  27153. Add('end;');
  27154. Add('var');
  27155. Add(' V: JSValue;');
  27156. Add(' o: TObject;');
  27157. Add('begin');
  27158. Add(' v:=@GetIt;');
  27159. Add(' v:[email protected];');
  27160. Add(' v:[email protected];');
  27161. ConvertProgram;
  27162. CheckSource('TestJSValue_ProcType_Assign',
  27163. LinesToStr([ // statements
  27164. 'rtl.createClass($mod, "TObject", null, function () {',
  27165. ' this.$init = function () {',
  27166. ' };',
  27167. ' this.$final = function () {',
  27168. ' };',
  27169. ' this.GetGlob = function () {',
  27170. ' var Result = 0;',
  27171. ' var v1 = undefined;',
  27172. ' v1 = rtl.createCallback(this, "GetGlob");',
  27173. ' v1 = rtl.createCallback(this, "GetGlob");',
  27174. ' return Result;',
  27175. ' };',
  27176. ' this.Getter = function () {',
  27177. ' var Result = 0;',
  27178. ' var v2 = undefined;',
  27179. ' v2 = rtl.createCallback(this, "Getter");',
  27180. ' v2 = rtl.createCallback(this, "Getter");',
  27181. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27182. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27183. ' return Result;',
  27184. ' };',
  27185. '});',
  27186. 'this.GetIt = function (i) {',
  27187. ' var Result = 0;',
  27188. ' var v3 = undefined;',
  27189. ' v3 = $mod.GetIt;',
  27190. ' return Result;',
  27191. '};',
  27192. 'this.V = undefined;',
  27193. 'this.o = null;',
  27194. '']),
  27195. LinesToStr([ // $mod.$main
  27196. '$mod.V = $mod.GetIt;',
  27197. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27198. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27199. '']));
  27200. end;
  27201. procedure TTestModule.TestJSValue_ProcType_Equal;
  27202. begin
  27203. StartProgram(false);
  27204. Add('type');
  27205. Add(' integer = longint;');
  27206. Add(' TObject = class');
  27207. Add(' class function GetGlob: integer;');
  27208. Add(' function Getter: integer;');
  27209. Add(' end;');
  27210. Add('class function TObject.GetGlob: integer;');
  27211. Add('var v1: jsvalue;');
  27212. Add('begin');
  27213. Add(' if v1=@GetGlob then;');
  27214. Add(' if [email protected] then ;');
  27215. Add('end;');
  27216. Add('function TObject.Getter: integer;');
  27217. Add('var v2: jsvalue;');
  27218. Add('begin');
  27219. Add(' if v2=@Getter then;');
  27220. Add(' if [email protected] then ;');
  27221. Add(' if v2=@GetGlob then;');
  27222. Add(' if [email protected] then;');
  27223. Add('end;');
  27224. Add('function GetIt(i: integer): integer;');
  27225. Add('var v3: jsvalue;');
  27226. Add('begin');
  27227. Add(' if v3=@GetIt then;');
  27228. Add('end;');
  27229. Add('var');
  27230. Add(' V: JSValue;');
  27231. Add(' o: TObject;');
  27232. Add('begin');
  27233. Add(' if v=@GetIt then;');
  27234. Add(' if [email protected] then;');
  27235. Add(' if [email protected] then;');
  27236. Add(' if @GetIt=v then;');
  27237. Add(' if @o.Getter=v then;');
  27238. Add(' if @o.GetGlob=v then;');
  27239. ConvertProgram;
  27240. CheckSource('TestJSValue_ProcType_Equal',
  27241. LinesToStr([ // statements
  27242. 'rtl.createClass($mod, "TObject", null, function () {',
  27243. ' this.$init = function () {',
  27244. ' };',
  27245. ' this.$final = function () {',
  27246. ' };',
  27247. ' this.GetGlob = function () {',
  27248. ' var Result = 0;',
  27249. ' var v1 = undefined;',
  27250. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27251. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27252. ' return Result;',
  27253. ' };',
  27254. ' this.Getter = function () {',
  27255. ' var Result = 0;',
  27256. ' var v2 = undefined;',
  27257. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27258. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27259. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27260. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27261. ' return Result;',
  27262. ' };',
  27263. '});',
  27264. 'this.GetIt = function (i) {',
  27265. ' var Result = 0;',
  27266. ' var v3 = undefined;',
  27267. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27268. ' return Result;',
  27269. '};',
  27270. 'this.V = undefined;',
  27271. 'this.o = null;',
  27272. '']),
  27273. LinesToStr([ // $mod.$main
  27274. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27275. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27276. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27277. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27278. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27279. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27280. '']));
  27281. end;
  27282. procedure TTestModule.TestJSValue_ProcType_Param;
  27283. begin
  27284. StartProgram(false);
  27285. Add([
  27286. 'type',
  27287. ' variant = jsvalue;',
  27288. ' TArrVariant = array of variant;',
  27289. ' TArrVar2 = TArrVariant;',
  27290. ' TFuncInt = function: longint;',
  27291. 'function GetIt: longint;',
  27292. 'begin',
  27293. 'end;',
  27294. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27295. 'var v: variant;',
  27296. 'begin',
  27297. ' v:=arr[1];',
  27298. 'end;',
  27299. 'var s: string;',
  27300. 'begin',
  27301. ' DoIt(GetIt,[]);',
  27302. ' DoIt(@GetIt,[]);',
  27303. ' DoIt(1,[s,GetIt]);',
  27304. ' DoIt(1,[s,@GetIt]);',
  27305. '']);
  27306. ConvertProgram;
  27307. CheckSource('TestJSValue_ProcType_Param',
  27308. LinesToStr([ // statements
  27309. 'this.GetIt = function () {',
  27310. ' var Result = 0;',
  27311. ' return Result;',
  27312. '};',
  27313. 'this.DoIt = function (p, Arr) {',
  27314. ' var v = undefined;',
  27315. ' v = Arr[1];',
  27316. '};',
  27317. 'this.s = "";',
  27318. '']),
  27319. LinesToStr([ // $mod.$main
  27320. '$mod.DoIt($mod.GetIt(), []);',
  27321. '$mod.DoIt($mod.GetIt, []);',
  27322. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27323. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27324. '']));
  27325. end;
  27326. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27327. begin
  27328. StartProgram(false);
  27329. Add([
  27330. 'var',
  27331. ' v: JSValue;',
  27332. ' p: Pointer;',
  27333. 'begin',
  27334. ' p:=v;',
  27335. '']);
  27336. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27337. nIncompatibleTypesGotExpected);
  27338. ConvertProgram;
  27339. end;
  27340. procedure TTestModule.TestJSValue_OverloadDouble;
  27341. begin
  27342. StartProgram(false);
  27343. Add([
  27344. 'type',
  27345. ' integer = longint;',
  27346. ' tdatetime = double;',
  27347. 'procedure DoIt(d: double); begin end;',
  27348. 'procedure DoIt(v: jsvalue); begin end;',
  27349. 'var',
  27350. ' d: double;',
  27351. ' dt: tdatetime;',
  27352. ' i: integer;',
  27353. ' b: byte;',
  27354. ' shi: shortint;',
  27355. ' w: word;',
  27356. ' smi: smallint;',
  27357. ' lw: longword;',
  27358. ' li: longint;',
  27359. ' ni: nativeint;',
  27360. ' nu: nativeuint;',
  27361. 'begin',
  27362. ' DoIt(d);',
  27363. ' DoIt(dt);',
  27364. ' DoIt(i);',
  27365. ' DoIt(b);',
  27366. ' DoIt(shi);',
  27367. ' DoIt(w);',
  27368. ' DoIt(smi);',
  27369. ' DoIt(lw);',
  27370. ' DoIt(li);',
  27371. ' DoIt(ni);',
  27372. ' DoIt(nu);',
  27373. '']);
  27374. ConvertProgram;
  27375. CheckSource('TestJSValue_OverloadDouble',
  27376. LinesToStr([ // statements
  27377. 'this.DoIt = function (d) {',
  27378. '};',
  27379. 'this.DoIt$1 = function (v) {',
  27380. '};',
  27381. 'this.d = 0.0;',
  27382. 'this.dt = 0.0;',
  27383. 'this.i = 0;',
  27384. 'this.b = 0;',
  27385. 'this.shi = 0;',
  27386. 'this.w = 0;',
  27387. 'this.smi = 0;',
  27388. 'this.lw = 0;',
  27389. 'this.li = 0;',
  27390. 'this.ni = 0;',
  27391. 'this.nu = 0;',
  27392. '']),
  27393. LinesToStr([ // $mod.$main
  27394. '$mod.DoIt($mod.d);',
  27395. '$mod.DoIt($mod.dt);',
  27396. '$mod.DoIt$1($mod.i);',
  27397. '$mod.DoIt$1($mod.b);',
  27398. '$mod.DoIt$1($mod.shi);',
  27399. '$mod.DoIt$1($mod.w);',
  27400. '$mod.DoIt$1($mod.smi);',
  27401. '$mod.DoIt$1($mod.lw);',
  27402. '$mod.DoIt$1($mod.li);',
  27403. '$mod.DoIt$1($mod.ni);',
  27404. '$mod.DoIt$1($mod.nu);',
  27405. '']));
  27406. end;
  27407. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27408. begin
  27409. StartProgram(false);
  27410. Add([
  27411. 'type',
  27412. ' integer = longint;',
  27413. ' int53 = nativeint;',
  27414. ' tdatetime = double;',
  27415. 'procedure DoIt(n: nativeint); begin end;',
  27416. 'procedure DoIt(v: jsvalue); begin end;',
  27417. 'var',
  27418. ' d: double;',
  27419. ' dt: tdatetime;',
  27420. ' i: integer;',
  27421. ' b: byte;',
  27422. ' shi: shortint;',
  27423. ' w: word;',
  27424. ' smi: smallint;',
  27425. ' lw: longword;',
  27426. ' li: longint;',
  27427. ' ni: nativeint;',
  27428. ' nu: nativeuint;',
  27429. 'begin',
  27430. ' DoIt(d);',
  27431. ' DoIt(dt);',
  27432. ' DoIt(i);',
  27433. ' DoIt(b);',
  27434. ' DoIt(shi);',
  27435. ' DoIt(w);',
  27436. ' DoIt(smi);',
  27437. ' DoIt(lw);',
  27438. ' DoIt(li);',
  27439. ' DoIt(ni);',
  27440. ' DoIt(nu);',
  27441. '']);
  27442. ConvertProgram;
  27443. CheckSource('TestJSValue_OverloadNativeInt',
  27444. LinesToStr([ // statements
  27445. 'this.DoIt = function (n) {',
  27446. '};',
  27447. 'this.DoIt$1 = function (v) {',
  27448. '};',
  27449. 'this.d = 0.0;',
  27450. 'this.dt = 0.0;',
  27451. 'this.i = 0;',
  27452. 'this.b = 0;',
  27453. 'this.shi = 0;',
  27454. 'this.w = 0;',
  27455. 'this.smi = 0;',
  27456. 'this.lw = 0;',
  27457. 'this.li = 0;',
  27458. 'this.ni = 0;',
  27459. 'this.nu = 0;',
  27460. '']),
  27461. LinesToStr([ // $mod.$main
  27462. '$mod.DoIt$1($mod.d);',
  27463. '$mod.DoIt$1($mod.dt);',
  27464. '$mod.DoIt($mod.i);',
  27465. '$mod.DoIt($mod.b);',
  27466. '$mod.DoIt($mod.shi);',
  27467. '$mod.DoIt($mod.w);',
  27468. '$mod.DoIt($mod.smi);',
  27469. '$mod.DoIt($mod.lw);',
  27470. '$mod.DoIt($mod.li);',
  27471. '$mod.DoIt($mod.ni);',
  27472. '$mod.DoIt($mod.nu);',
  27473. '']));
  27474. end;
  27475. procedure TTestModule.TestJSValue_OverloadWord;
  27476. begin
  27477. StartProgram(false);
  27478. Add([
  27479. 'type',
  27480. ' integer = longint;',
  27481. ' int53 = nativeint;',
  27482. ' tdatetime = double;',
  27483. 'procedure DoIt(w: word); begin end;',
  27484. 'procedure DoIt(v: jsvalue); begin end;',
  27485. 'var',
  27486. ' d: double;',
  27487. ' dt: tdatetime;',
  27488. ' i: integer;',
  27489. ' b: byte;',
  27490. ' shi: shortint;',
  27491. ' w: word;',
  27492. ' smi: smallint;',
  27493. ' lw: longword;',
  27494. ' li: longint;',
  27495. ' ni: nativeint;',
  27496. ' nu: nativeuint;',
  27497. 'begin',
  27498. ' DoIt(d);',
  27499. ' DoIt(dt);',
  27500. ' DoIt(i);',
  27501. ' DoIt(b);',
  27502. ' DoIt(shi);',
  27503. ' DoIt(w);',
  27504. ' DoIt(smi);',
  27505. ' DoIt(lw);',
  27506. ' DoIt(li);',
  27507. ' DoIt(ni);',
  27508. ' DoIt(nu);',
  27509. '']);
  27510. ConvertProgram;
  27511. CheckSource('TestJSValue_OverloadWord',
  27512. LinesToStr([ // statements
  27513. 'this.DoIt = function (w) {',
  27514. '};',
  27515. 'this.DoIt$1 = function (v) {',
  27516. '};',
  27517. 'this.d = 0.0;',
  27518. 'this.dt = 0.0;',
  27519. 'this.i = 0;',
  27520. 'this.b = 0;',
  27521. 'this.shi = 0;',
  27522. 'this.w = 0;',
  27523. 'this.smi = 0;',
  27524. 'this.lw = 0;',
  27525. 'this.li = 0;',
  27526. 'this.ni = 0;',
  27527. 'this.nu = 0;',
  27528. '']),
  27529. LinesToStr([ // $mod.$main
  27530. '$mod.DoIt$1($mod.d);',
  27531. '$mod.DoIt$1($mod.dt);',
  27532. '$mod.DoIt$1($mod.i);',
  27533. '$mod.DoIt($mod.b);',
  27534. '$mod.DoIt($mod.shi);',
  27535. '$mod.DoIt($mod.w);',
  27536. '$mod.DoIt$1($mod.smi);',
  27537. '$mod.DoIt$1($mod.lw);',
  27538. '$mod.DoIt$1($mod.li);',
  27539. '$mod.DoIt$1($mod.ni);',
  27540. '$mod.DoIt$1($mod.nu);',
  27541. '']));
  27542. end;
  27543. procedure TTestModule.TestJSValue_OverloadString;
  27544. begin
  27545. StartProgram(false);
  27546. Add([
  27547. 'type',
  27548. ' uni = string;',
  27549. ' WChar = char;',
  27550. 'procedure DoIt(s: string); begin end;',
  27551. 'procedure DoIt(v: jsvalue); begin end;',
  27552. 'var',
  27553. ' s: string;',
  27554. ' c: char;',
  27555. ' u: uni;',
  27556. 'begin',
  27557. ' DoIt(s);',
  27558. ' DoIt(c);',
  27559. ' DoIt(u);',
  27560. '']);
  27561. ConvertProgram;
  27562. CheckSource('TestJSValue_OverloadString',
  27563. LinesToStr([ // statements
  27564. 'this.DoIt = function (s) {',
  27565. '};',
  27566. 'this.DoIt$1 = function (v) {',
  27567. '};',
  27568. 'this.s = "";',
  27569. 'this.c = "";',
  27570. 'this.u = "";',
  27571. '']),
  27572. LinesToStr([ // $mod.$main
  27573. '$mod.DoIt($mod.s);',
  27574. '$mod.DoIt($mod.c);',
  27575. '$mod.DoIt($mod.u);',
  27576. '']));
  27577. end;
  27578. procedure TTestModule.TestJSValue_OverloadChar;
  27579. begin
  27580. StartProgram(false);
  27581. Add([
  27582. 'type',
  27583. ' uni = string;',
  27584. ' WChar = char;',
  27585. 'procedure DoIt(c: char); begin end;',
  27586. 'procedure DoIt(v: jsvalue); begin end;',
  27587. 'var',
  27588. ' s: string;',
  27589. ' c: char;',
  27590. ' u: uni;',
  27591. 'begin',
  27592. ' DoIt(s);',
  27593. ' DoIt(c);',
  27594. ' DoIt(u);',
  27595. '']);
  27596. ConvertProgram;
  27597. CheckSource('TestJSValue_OverloadChar',
  27598. LinesToStr([ // statements
  27599. 'this.DoIt = function (c) {',
  27600. '};',
  27601. 'this.DoIt$1 = function (v) {',
  27602. '};',
  27603. 'this.s = "";',
  27604. 'this.c = "";',
  27605. 'this.u = "";',
  27606. '']),
  27607. LinesToStr([ // $mod.$main
  27608. '$mod.DoIt$1($mod.s);',
  27609. '$mod.DoIt($mod.c);',
  27610. '$mod.DoIt$1($mod.u);',
  27611. '']));
  27612. end;
  27613. procedure TTestModule.TestJSValue_OverloadPointer;
  27614. begin
  27615. StartProgram(false);
  27616. Add([
  27617. 'type',
  27618. ' TObject = class end;',
  27619. 'procedure DoIt(p: pointer); begin end;',
  27620. 'procedure DoIt(v: jsvalue); begin end;',
  27621. 'var',
  27622. ' o: TObject;',
  27623. 'begin',
  27624. ' DoIt(o);',
  27625. '']);
  27626. ConvertProgram;
  27627. CheckSource('TestJSValue_OverloadPointer',
  27628. LinesToStr([ // statements
  27629. 'rtl.createClass($mod, "TObject", null, function () {',
  27630. ' this.$init = function () {',
  27631. ' };',
  27632. ' this.$final = function () {',
  27633. ' };',
  27634. '});',
  27635. 'this.DoIt = function (p) {',
  27636. '};',
  27637. 'this.DoIt$1 = function (v) {',
  27638. '};',
  27639. 'this.o = null;',
  27640. '']),
  27641. LinesToStr([ // $mod.$main
  27642. '$mod.DoIt($mod.o);',
  27643. '']));
  27644. end;
  27645. procedure TTestModule.TestJSValue_ForIn;
  27646. begin
  27647. StartProgram(false);
  27648. Add([
  27649. 'var',
  27650. ' v: JSValue;',
  27651. ' key: string;',
  27652. 'begin',
  27653. ' for key in v do begin',
  27654. ' if key=''abc'' then ;',
  27655. ' end;',
  27656. '']);
  27657. ConvertProgram;
  27658. CheckSource('TestJSValue_ForIn',
  27659. LinesToStr([ // statements
  27660. 'this.v = undefined;',
  27661. 'this.key = "";',
  27662. '']),
  27663. LinesToStr([ // $mod.$main
  27664. 'for ($mod.key in $mod.v) {',
  27665. ' if ($mod.key === "abc") ;',
  27666. '};',
  27667. '']));
  27668. end;
  27669. procedure TTestModule.TestRTTI_IntRange;
  27670. begin
  27671. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27672. StartProgram(true,[supTypeInfo]);
  27673. Add([
  27674. '{$modeswitch externalclass}',
  27675. 'type',
  27676. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  27677. ' TColor = type TGraphicsColor;',
  27678. 'var',
  27679. ' p: TTypeInfo;',
  27680. 'begin',
  27681. ' p:=typeinfo(TGraphicsColor);',
  27682. ' p:=typeinfo(TColor);',
  27683. '']);
  27684. ConvertProgram;
  27685. CheckSource('TestRTTI_IntRange',
  27686. LinesToStr([ // statements
  27687. '$mod.$rtti.$Int("TGraphicsColor", {',
  27688. ' minvalue: -2147483648,',
  27689. ' maxvalue: 2147483647,',
  27690. ' ordtype: 4',
  27691. '});',
  27692. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  27693. 'this.p = null;',
  27694. '']),
  27695. LinesToStr([ // $mod.$main
  27696. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  27697. '$mod.p = $mod.$rtti["TColor"];',
  27698. '']));
  27699. end;
  27700. procedure TTestModule.TestRTTI_Double;
  27701. begin
  27702. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27703. StartProgram(true,[supTypeInfo]);
  27704. Add([
  27705. '{$modeswitch externalclass}',
  27706. 'type',
  27707. ' TFloat = type double;',
  27708. 'var',
  27709. ' p: TTypeInfo;',
  27710. 'begin',
  27711. ' p:=typeinfo(double);',
  27712. ' p:=typeinfo(TFloat);',
  27713. '']);
  27714. ConvertProgram;
  27715. CheckSource('TestRTTI_Double',
  27716. LinesToStr([ // statements
  27717. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27718. 'this.p = null;',
  27719. '']),
  27720. LinesToStr([ // $mod.$main
  27721. '$mod.p = rtl.double;',
  27722. '$mod.p = $mod.$rtti["TFloat"];',
  27723. '']));
  27724. end;
  27725. procedure TTestModule.TestRTTI_ProcType;
  27726. begin
  27727. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27728. StartProgram(false);
  27729. Add('type');
  27730. Add(' TProcA = procedure;');
  27731. Add(' TMethodB = procedure of object;');
  27732. Add(' TProcC = procedure; varargs;');
  27733. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  27734. Add(' TProcE = function: nativeint;');
  27735. Add(' TProcF = function(const p: TProcA): nativeuint;');
  27736. Add('var p: pointer;');
  27737. Add('begin');
  27738. Add(' p:=typeinfo(tproca);');
  27739. ConvertProgram;
  27740. CheckSource('TestRTTI_ProcType',
  27741. LinesToStr([ // statements
  27742. '$mod.$rtti.$ProcVar("TProcA", {',
  27743. ' procsig: rtl.newTIProcSig(null)',
  27744. '});',
  27745. '$mod.$rtti.$MethodVar("TMethodB", {',
  27746. ' procsig: rtl.newTIProcSig(null),',
  27747. ' methodkind: 0',
  27748. '});',
  27749. '$mod.$rtti.$ProcVar("TProcC", {',
  27750. ' procsig: rtl.newTIProcSig(null, 2)',
  27751. '});',
  27752. '$mod.$rtti.$ProcVar("TProcD", {',
  27753. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  27754. '});',
  27755. '$mod.$rtti.$ProcVar("TProcE", {',
  27756. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  27757. '});',
  27758. '$mod.$rtti.$ProcVar("TProcF", {',
  27759. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  27760. '});',
  27761. 'this.p = null;',
  27762. '']),
  27763. LinesToStr([ // $mod.$main
  27764. '$mod.p = $mod.$rtti["TProcA"];',
  27765. '']));
  27766. end;
  27767. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  27768. begin
  27769. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27770. AddModuleWithIntfImplSrc('unit2.pas',
  27771. LinesToStr([
  27772. 'type',
  27773. ' TObject = class end;'
  27774. ]),
  27775. '');
  27776. StartUnit(true);
  27777. Add('interface');
  27778. Add('uses unit2;');
  27779. Add('type');
  27780. Add(' TProcA = function(o: tobject): tobject;');
  27781. Add('implementation');
  27782. Add('type');
  27783. Add(' TProcB = function(o: tobject): tobject;');
  27784. Add('var p: Pointer;');
  27785. Add('initialization');
  27786. Add(' p:=typeinfo(tproca);');
  27787. Add(' p:=typeinfo(tprocb);');
  27788. ConvertUnit;
  27789. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  27790. LinesToStr([ // statements
  27791. 'var $impl = $mod.$impl;',
  27792. '$mod.$rtti.$ProcVar("TProcA", {',
  27793. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27794. '});',
  27795. '']),
  27796. LinesToStr([ // this.$init
  27797. '$impl.p = $mod.$rtti["TProcA"];',
  27798. '$impl.p = $mod.$rtti["TProcB"];',
  27799. '']),
  27800. LinesToStr([ // implementation
  27801. '$mod.$rtti.$ProcVar("TProcB", {',
  27802. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27803. '});',
  27804. '$impl.p = null;',
  27805. '']) );
  27806. end;
  27807. procedure TTestModule.TestRTTI_EnumAndSetType;
  27808. begin
  27809. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27810. StartProgram(false);
  27811. Add('type');
  27812. Add(' TFlag = (light,dark);');
  27813. Add(' TFlags = set of TFlag;');
  27814. Add(' TProc = function(f: TFlags): TFlag;');
  27815. Add('var p: pointer;');
  27816. Add('begin');
  27817. Add(' p:=typeinfo(tflag);');
  27818. Add(' p:=typeinfo(tflags);');
  27819. ConvertProgram;
  27820. CheckSource('TestRTTI_EnumAndType',
  27821. LinesToStr([ // statements
  27822. 'this.TFlag = {',
  27823. ' "0": "light",',
  27824. ' light: 0,',
  27825. ' "1": "dark",',
  27826. ' dark: 1',
  27827. '};',
  27828. '$mod.$rtti.$Enum("TFlag", {',
  27829. ' minvalue: 0,',
  27830. ' maxvalue: 1,',
  27831. ' ordtype: 1,',
  27832. ' enumtype: this.TFlag',
  27833. '});',
  27834. '$mod.$rtti.$Set("TFlags", {',
  27835. ' comptype: $mod.$rtti["TFlag"]',
  27836. '});',
  27837. '$mod.$rtti.$ProcVar("TProc", {',
  27838. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  27839. '});',
  27840. 'this.p = null;',
  27841. '']),
  27842. LinesToStr([ // $mod.$main
  27843. '$mod.p = $mod.$rtti["TFlag"];',
  27844. '$mod.p = $mod.$rtti["TFlags"];',
  27845. '']));
  27846. end;
  27847. procedure TTestModule.TestRTTI_EnumRange;
  27848. begin
  27849. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27850. StartProgram(false);
  27851. Add([
  27852. 'type',
  27853. ' TCol = (red,green,blue);',
  27854. ' TColRg = green..blue;',
  27855. ' TSetOfColRg = set of TColRg;',
  27856. 'var p: pointer;',
  27857. 'begin',
  27858. ' p:=typeinfo(tcolrg);',
  27859. ' p:=typeinfo(tsetofcolrg);',
  27860. '']);
  27861. ConvertProgram;
  27862. end;
  27863. procedure TTestModule.TestRTTI_AnonymousEnumType;
  27864. begin
  27865. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27866. StartProgram(false);
  27867. Add('type');
  27868. Add(' TFlags = set of (red, green);');
  27869. Add('var');
  27870. Add(' f: TFlags;');
  27871. Add('begin');
  27872. Add(' Include(f,red);');
  27873. ConvertProgram;
  27874. CheckSource('TestRTTI_AnonymousEnumType',
  27875. LinesToStr([ // statements
  27876. 'this.TFlags$a = {',
  27877. ' "0": "red",',
  27878. ' red: 0,',
  27879. ' "1": "green",',
  27880. ' green: 1',
  27881. '};',
  27882. '$mod.$rtti.$Enum("TFlags$a", {',
  27883. ' minvalue: 0,',
  27884. ' maxvalue: 1,',
  27885. ' ordtype: 1,',
  27886. ' enumtype: this.TFlags$a',
  27887. '});',
  27888. '$mod.$rtti.$Set("TFlags", {',
  27889. ' comptype: $mod.$rtti["TFlags$a"]',
  27890. '});',
  27891. 'this.f = {};',
  27892. '']),
  27893. LinesToStr([
  27894. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  27895. '']));
  27896. end;
  27897. procedure TTestModule.TestRTTI_StaticArray;
  27898. begin
  27899. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27900. StartProgram(false);
  27901. Add('type');
  27902. Add(' TFlag = (light,dark);');
  27903. Add(' TFlagNames = array[TFlag] of string;');
  27904. Add(' TBoolNames = array[boolean] of string;');
  27905. Add(' TByteArray = array[1..32768] of byte;');
  27906. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  27907. Add('var p: pointer;');
  27908. Add('begin');
  27909. Add(' p:=typeinfo(TFlagNames);');
  27910. Add(' p:=typeinfo(TBoolNames);');
  27911. ConvertProgram;
  27912. CheckSource('TestRTTI_StaticArray',
  27913. LinesToStr([ // statements
  27914. 'this.TFlag = {',
  27915. ' "0": "light",',
  27916. ' light: 0,',
  27917. ' "1": "dark",',
  27918. ' dark: 1',
  27919. '};',
  27920. '$mod.$rtti.$Enum("TFlag", {',
  27921. ' minvalue: 0,',
  27922. ' maxvalue: 1,',
  27923. ' ordtype: 1,',
  27924. ' enumtype: this.TFlag',
  27925. '});',
  27926. '$mod.$rtti.$StaticArray("TFlagNames", {',
  27927. ' dims: [2],',
  27928. ' eltype: rtl.string',
  27929. '});',
  27930. '$mod.$rtti.$StaticArray("TBoolNames", {',
  27931. ' dims: [2],',
  27932. ' eltype: rtl.string',
  27933. '});',
  27934. '$mod.$rtti.$StaticArray("TByteArray", {',
  27935. ' dims: [32768],',
  27936. ' eltype: rtl.byte',
  27937. '});',
  27938. '$mod.$rtti.$ProcVar("TProc", {',
  27939. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  27940. '});',
  27941. 'this.p = null;',
  27942. '']),
  27943. LinesToStr([ // $mod.$main
  27944. '$mod.p = $mod.$rtti["TFlagNames"];',
  27945. '$mod.p = $mod.$rtti["TBoolNames"];',
  27946. '']));
  27947. end;
  27948. procedure TTestModule.TestRTTI_DynArray;
  27949. begin
  27950. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27951. StartProgram(false);
  27952. Add('type');
  27953. Add(' TArrStr = array of string;');
  27954. Add(' TArr2Dim = array of tarrstr;');
  27955. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  27956. Add('var p: pointer;');
  27957. Add('begin');
  27958. Add(' p:=typeinfo(tarrstr);');
  27959. Add(' p:=typeinfo(tarr2dim);');
  27960. ConvertProgram;
  27961. CheckSource('TestRTTI_DynArray',
  27962. LinesToStr([ // statements
  27963. '$mod.$rtti.$DynArray("TArrStr", {',
  27964. ' eltype: rtl.string',
  27965. '});',
  27966. '$mod.$rtti.$DynArray("TArr2Dim", {',
  27967. ' eltype: $mod.$rtti["TArrStr"]',
  27968. '});',
  27969. '$mod.$rtti.$ProcVar("TProc", {',
  27970. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  27971. '});',
  27972. 'this.p = null;',
  27973. '']),
  27974. LinesToStr([ // $mod.$main
  27975. '$mod.p = $mod.$rtti["TArrStr"];',
  27976. '$mod.p = $mod.$rtti["TArr2Dim"];',
  27977. '']));
  27978. end;
  27979. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  27980. begin
  27981. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27982. StartProgram(false);
  27983. Add('type');
  27984. Add(' TArr = array of array of longint;');
  27985. Add('var a: TArr;');
  27986. Add('begin');
  27987. ConvertProgram;
  27988. CheckSource('TestRTTI_ArrayNestedAnonymous',
  27989. LinesToStr([ // statements
  27990. '$mod.$rtti.$DynArray("TArr$a", {',
  27991. ' eltype: rtl.longint',
  27992. '});',
  27993. '$mod.$rtti.$DynArray("TArr", {',
  27994. ' eltype: $mod.$rtti["TArr$a"]',
  27995. '});',
  27996. 'this.a = [];',
  27997. '']),
  27998. LinesToStr([ // $mod.$main
  27999. ]));
  28000. end;
  28001. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28002. begin
  28003. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28004. StartProgram(false);
  28005. Add('type');
  28006. Add(' TObject = class');
  28007. Add(' published');
  28008. Add(' procedure Proc; virtual; abstract;');
  28009. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28010. Add(' end;');
  28011. Add('begin');
  28012. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28013. nDuplicatePublishedMethodXAtY);
  28014. ConvertProgram;
  28015. end;
  28016. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28017. begin
  28018. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28019. StartProgram(false);
  28020. Add('type');
  28021. Add(' TObject = class');
  28022. Add(' published');
  28023. Add(' procedure Proc; external name ''foo'';');
  28024. Add(' end;');
  28025. Add('begin');
  28026. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28027. nPublishedNameMustMatchExternal);
  28028. ConvertProgram;
  28029. end;
  28030. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28031. begin
  28032. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28033. StartProgram(false);
  28034. Add('type');
  28035. Add(' TObject = class');
  28036. Add(' class var FA: longint;');
  28037. Add(' published');
  28038. Add(' class property A: longint read FA;');
  28039. Add(' end;');
  28040. Add('begin');
  28041. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28042. nInvalidXModifierY);
  28043. ConvertProgram;
  28044. end;
  28045. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28046. begin
  28047. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28048. StartProgram(false);
  28049. Add('type');
  28050. Add(' TObject = class');
  28051. Add(' published');
  28052. Add(' class var FA: longint;');
  28053. Add(' end;');
  28054. Add('begin');
  28055. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28056. nSymbolCannotBePublished);
  28057. ConvertProgram;
  28058. end;
  28059. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28060. begin
  28061. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28062. StartProgram(false);
  28063. Add('{$modeswitch externalclass}');
  28064. Add('type');
  28065. Add(' TObject = class');
  28066. Add(' published');
  28067. Add(' V: longint; external name ''foo'';');
  28068. Add(' end;');
  28069. Add('begin');
  28070. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28071. nPublishedNameMustMatchExternal);
  28072. ConvertProgram;
  28073. end;
  28074. procedure TTestModule.TestRTTI_Class_Field;
  28075. begin
  28076. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28077. StartProgram(false);
  28078. Add('{$modeswitch externalclass}');
  28079. Add('type');
  28080. Add(' TObject = class');
  28081. Add(' private');
  28082. Add(' FPropA: string;');
  28083. Add(' published');
  28084. Add(' VarLI: longint;');
  28085. Add(' VarC: char;');
  28086. Add(' VarS: string;');
  28087. Add(' VarD: double;');
  28088. Add(' VarB: boolean;');
  28089. Add(' VarLW: longword;');
  28090. Add(' VarSmI: smallint;');
  28091. Add(' VarW: word;');
  28092. Add(' VarShI: shortint;');
  28093. Add(' VarBy: byte;');
  28094. Add(' VarExt: longint external name ''VarExt'';');
  28095. Add(' end;');
  28096. Add('var p: pointer;');
  28097. Add(' Obj: tobject;');
  28098. Add('begin');
  28099. Add(' p:=typeinfo(tobject);');
  28100. Add(' p:=typeinfo(p);');
  28101. Add(' p:=typeinfo(obj);');
  28102. ConvertProgram;
  28103. CheckSource('TestRTTI_Class_Field',
  28104. LinesToStr([ // statements
  28105. 'rtl.createClass($mod, "TObject", null, function () {',
  28106. ' this.$init = function () {',
  28107. ' this.FPropA = "";',
  28108. ' this.VarLI = 0;',
  28109. ' this.VarC = "";',
  28110. ' this.VarS = "";',
  28111. ' this.VarD = 0.0;',
  28112. ' this.VarB = false;',
  28113. ' this.VarLW = 0;',
  28114. ' this.VarSmI = 0;',
  28115. ' this.VarW = 0;',
  28116. ' this.VarShI = 0;',
  28117. ' this.VarBy = 0;',
  28118. ' };',
  28119. ' this.$final = function () {',
  28120. ' };',
  28121. ' var $r = this.$rtti;',
  28122. ' $r.addField("VarLI", rtl.longint);',
  28123. ' $r.addField("VarC", rtl.char);',
  28124. ' $r.addField("VarS", rtl.string);',
  28125. ' $r.addField("VarD", rtl.double);',
  28126. ' $r.addField("VarB", rtl.boolean);',
  28127. ' $r.addField("VarLW", rtl.longword);',
  28128. ' $r.addField("VarSmI", rtl.smallint);',
  28129. ' $r.addField("VarW", rtl.word);',
  28130. ' $r.addField("VarShI", rtl.shortint);',
  28131. ' $r.addField("VarBy", rtl.byte);',
  28132. ' $r.addField("VarExt", rtl.longint);',
  28133. '});',
  28134. 'this.p = null;',
  28135. 'this.Obj = null;',
  28136. '']),
  28137. LinesToStr([ // $mod.$main
  28138. '$mod.p = $mod.$rtti["TObject"];',
  28139. '$mod.p = rtl.pointer;',
  28140. '$mod.p = $mod.Obj.$rtti;',
  28141. '']));
  28142. end;
  28143. procedure TTestModule.TestRTTI_Class_Method;
  28144. begin
  28145. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28146. StartProgram(false);
  28147. Add('type');
  28148. Add(' TObject = class');
  28149. Add(' private');
  28150. Add(' procedure Internal; external name ''$intern'';');
  28151. Add(' published');
  28152. Add(' procedure Click; virtual; abstract;');
  28153. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28154. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28155. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28156. Add(' end;');
  28157. Add('begin');
  28158. ConvertProgram;
  28159. CheckSource('TestRTTI_Class_Method',
  28160. LinesToStr([ // statements
  28161. 'rtl.createClass($mod, "TObject", null, function () {',
  28162. ' this.$init = function () {',
  28163. ' };',
  28164. ' this.$final = function () {',
  28165. ' };',
  28166. ' var $r = this.$rtti;',
  28167. ' $r.addMethod("Click", 0, null);',
  28168. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28169. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28170. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28171. ' flags: 2',
  28172. ' });',
  28173. '});',
  28174. '']),
  28175. LinesToStr([ // $mod.$main
  28176. '']));
  28177. end;
  28178. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28179. begin
  28180. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28181. StartProgram(false);
  28182. Add('type');
  28183. Add(' TObject = class');
  28184. Add(' published');
  28185. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28186. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28187. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28188. Add(' end;');
  28189. Add('begin');
  28190. ConvertProgram;
  28191. CheckSource('TestRTTI_Class_MethodOpenArray',
  28192. LinesToStr([ // statements
  28193. 'rtl.createClass($mod, "TObject", null, function () {',
  28194. ' this.$init = function () {',
  28195. ' };',
  28196. ' this.$final = function () {',
  28197. ' };',
  28198. ' var $r = this.$rtti;',
  28199. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28200. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28201. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28202. '});',
  28203. '']),
  28204. LinesToStr([ // $mod.$main
  28205. '']));
  28206. end;
  28207. procedure TTestModule.TestRTTI_Class_Property;
  28208. begin
  28209. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28210. StartProgram(false);
  28211. Add('{$modeswitch externalclass}');
  28212. Add('type');
  28213. Add(' TObject = class');
  28214. Add(' private');
  28215. Add(' FColor: longint;');
  28216. Add(' FColorStored: boolean;');
  28217. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28218. Add(' function GetColor: longint; virtual; abstract;');
  28219. Add(' function GetColorStored: boolean; virtual; abstract;');
  28220. Add(' FExtSize: longint external name ''$extSize'';');
  28221. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28222. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28223. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28224. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28225. Add(' published');
  28226. Add(' property ColorA: longint read FColor;');
  28227. Add(' property ColorB: longint write FColor;');
  28228. Add(' property ColorC: longint read GetColor write SetColor;');
  28229. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28230. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28231. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28232. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28233. Add(' end;');
  28234. Add('begin');
  28235. ConvertProgram;
  28236. CheckSource('TestRTTI_Class_Property',
  28237. LinesToStr([ // statements
  28238. 'rtl.createClass($mod, "TObject", null, function () {',
  28239. ' this.$init = function () {',
  28240. ' this.FColor = 0;',
  28241. ' this.FColorStored = false;',
  28242. ' };',
  28243. ' this.$final = function () {',
  28244. ' };',
  28245. ' var $r = this.$rtti;',
  28246. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28247. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28248. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28249. ' $r.addProperty(',
  28250. ' "ColorD",',
  28251. ' 8,',
  28252. ' rtl.longint,',
  28253. ' "FColor",',
  28254. ' "FColor",',
  28255. ' {',
  28256. ' stored: "FColorStored"',
  28257. ' }',
  28258. ' );',
  28259. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28260. ' $r.addProperty(',
  28261. ' "ExtSizeB",',
  28262. ' 11,',
  28263. ' rtl.longint,',
  28264. ' "$getSize",',
  28265. ' "$setSize",',
  28266. ' {',
  28267. ' stored: "$extSizeStored"',
  28268. ' }',
  28269. ' );',
  28270. ' $r.addProperty(',
  28271. ' "ExtSizeC",',
  28272. ' 12,',
  28273. ' rtl.longint,',
  28274. ' "$extSize",',
  28275. ' "$extSize",',
  28276. ' {',
  28277. ' stored: "$getExtSizeStored"',
  28278. ' }',
  28279. ' );',
  28280. '});',
  28281. '']),
  28282. LinesToStr([ // $mod.$main
  28283. '']));
  28284. end;
  28285. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28286. begin
  28287. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28288. StartProgram(false);
  28289. Add('{$modeswitch externalclass}');
  28290. Add('type');
  28291. Add(' integer = longint;');
  28292. Add(' TObject = class');
  28293. Add(' private');
  28294. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28295. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28296. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28297. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28298. Add(' published');
  28299. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28300. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28301. Add(' end;');
  28302. Add('begin');
  28303. ConvertProgram;
  28304. CheckSource('TestRTTI_Class_PropertyParams',
  28305. LinesToStr([ // statements
  28306. 'rtl.createClass($mod, "TObject", null, function () {',
  28307. ' this.$init = function () {',
  28308. ' };',
  28309. ' this.$final = function () {',
  28310. ' };',
  28311. ' var $r = this.$rtti;',
  28312. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28313. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28314. '});',
  28315. '']),
  28316. LinesToStr([ // $mod.$main
  28317. '']));
  28318. end;
  28319. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28320. begin
  28321. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28322. AddModuleWithIntfImplSrc('unit1.pas',
  28323. 'type TColor = -5..5;',
  28324. '');
  28325. StartProgram(true);
  28326. Add([
  28327. 'uses unit1;',
  28328. 'type',
  28329. ' TColorAlias = TColor;',
  28330. ' TColorTypeAlias = type TColor;',
  28331. ' TObject = class',
  28332. ' private',
  28333. ' fColor: TColor;',
  28334. ' fAlias: TColorAlias;',
  28335. ' fTypeAlias: TColorTypeAlias;',
  28336. ' published',
  28337. ' property Color: TColor read fcolor;',
  28338. ' property Alias: TColorAlias read falias;',
  28339. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28340. ' end;',
  28341. 'begin',
  28342. '']);
  28343. ConvertProgram;
  28344. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28345. LinesToStr([ // statements
  28346. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28347. 'rtl.createClass($mod, "TObject", null, function () {',
  28348. ' this.$init = function () {',
  28349. ' this.fColor = 0;',
  28350. ' this.fAlias = 0;',
  28351. ' this.fTypeAlias = 0;',
  28352. ' };',
  28353. ' this.$final = function () {',
  28354. ' };',
  28355. ' var $r = this.$rtti;',
  28356. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28357. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28358. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28359. '});',
  28360. '']),
  28361. LinesToStr([ // $mod.$main
  28362. '']));
  28363. end;
  28364. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28365. begin
  28366. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28367. StartProgram(false);
  28368. Add([
  28369. '{$modeswitch omitrtti}',
  28370. 'type',
  28371. ' TObject = class',
  28372. ' private',
  28373. ' FA: byte;',
  28374. ' published',
  28375. ' property A: byte read FA write FA;',
  28376. ' end;',
  28377. 'begin']);
  28378. ConvertProgram;
  28379. CheckSource('TestRTTI_Class_OmitRTTI',
  28380. LinesToStr([ // statements
  28381. 'rtl.createClass($mod, "TObject", null, function () {',
  28382. ' this.$init = function () {',
  28383. ' this.FA = 0;',
  28384. ' };',
  28385. ' this.$final = function () {',
  28386. ' };',
  28387. '});',
  28388. '']),
  28389. LinesToStr([ // $mod.$main
  28390. '']));
  28391. end;
  28392. procedure TTestModule.TestRTTI_IndexModifier;
  28393. begin
  28394. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28395. StartProgram(false);
  28396. Add([
  28397. 'type',
  28398. ' TEnum = (red, blue);',
  28399. ' TObject = class',
  28400. ' FB: boolean;',
  28401. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28402. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28403. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28404. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28405. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28406. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28407. ' published',
  28408. ' property B1: boolean index 1 read FB write SetIntBool;',
  28409. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28410. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28411. ' end;',
  28412. 'begin']);
  28413. ConvertProgram;
  28414. CheckSource('TestRTTI_IndexModifier',
  28415. LinesToStr([ // statements
  28416. 'this.TEnum = {',
  28417. ' "0": "red",',
  28418. ' red: 0,',
  28419. ' "1": "blue",',
  28420. ' blue: 1',
  28421. '};',
  28422. '$mod.$rtti.$Enum("TEnum", {',
  28423. ' minvalue: 0,',
  28424. ' maxvalue: 1,',
  28425. ' ordtype: 1,',
  28426. ' enumtype: this.TEnum',
  28427. '});',
  28428. 'rtl.createClass($mod, "TObject", null, function () {',
  28429. ' this.$init = function () {',
  28430. ' this.FB = false;',
  28431. ' };',
  28432. ' this.$final = function () {',
  28433. ' };',
  28434. ' var $r = this.$rtti;',
  28435. ' $r.addProperty(',
  28436. ' "B1",',
  28437. ' 18,',
  28438. ' rtl.boolean,',
  28439. ' "FB",',
  28440. ' "SetIntBool",',
  28441. ' {',
  28442. ' index: 1',
  28443. ' }',
  28444. ' );',
  28445. ' $r.addProperty(',
  28446. ' "B2",',
  28447. ' 17,',
  28448. ' rtl.boolean,',
  28449. ' "GetEnumBool",',
  28450. ' "FB",',
  28451. ' {',
  28452. ' index: $mod.TEnum.blue',
  28453. ' }',
  28454. ' );',
  28455. ' $r.addProperty(',
  28456. ' "I1",',
  28457. ' 19,',
  28458. ' rtl.boolean,',
  28459. ' "GetStrIntBool",',
  28460. ' "SetStrIntBool",',
  28461. ' {',
  28462. ' index: 2',
  28463. ' }',
  28464. ' );',
  28465. '});',
  28466. '']),
  28467. LinesToStr([ // $mod.$main
  28468. '']));
  28469. end;
  28470. procedure TTestModule.TestRTTI_StoredModifier;
  28471. begin
  28472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28473. StartProgram(false);
  28474. Add([
  28475. 'const',
  28476. ' ConstB = true;',
  28477. 'type',
  28478. ' TObject = class',
  28479. ' private',
  28480. ' FB: boolean;',
  28481. ' function IsBStored: boolean; virtual; abstract;',
  28482. ' published',
  28483. ' property BoolA: boolean read FB stored true;',
  28484. ' property BoolB: boolean read FB stored false;',
  28485. ' property BoolC: boolean read FB stored FB;',
  28486. ' property BoolD: boolean read FB stored ConstB;',
  28487. ' property BoolE: boolean read FB stored IsBStored;',
  28488. ' end;',
  28489. 'begin']);
  28490. ConvertProgram;
  28491. CheckSource('TestRTTI_StoredModifier',
  28492. LinesToStr([ // statements
  28493. 'this.ConstB = true;',
  28494. 'rtl.createClass($mod, "TObject", null, function () {',
  28495. ' this.$init = function () {',
  28496. ' this.FB = false;',
  28497. ' };',
  28498. ' this.$final = function () {',
  28499. ' };',
  28500. ' var $r = this.$rtti;',
  28501. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  28502. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  28503. ' $r.addProperty(',
  28504. ' "BoolC",',
  28505. ' 8,',
  28506. ' rtl.boolean,',
  28507. ' "FB",',
  28508. ' "",',
  28509. ' {',
  28510. ' stored: "FB"',
  28511. ' }',
  28512. ' );',
  28513. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  28514. ' $r.addProperty(',
  28515. ' "BoolE",',
  28516. ' 12,',
  28517. ' rtl.boolean,',
  28518. ' "FB",',
  28519. ' "",',
  28520. ' {',
  28521. ' stored: "IsBStored"',
  28522. ' }',
  28523. ' );',
  28524. '});',
  28525. '']),
  28526. LinesToStr([ // $mod.$main
  28527. '']));
  28528. end;
  28529. procedure TTestModule.TestRTTI_DefaultValue;
  28530. begin
  28531. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28532. StartProgram(false);
  28533. Add([
  28534. 'type',
  28535. ' TEnum = (red, blue);',
  28536. 'const',
  28537. ' CB = true or false;',
  28538. ' CI = 1+2;',
  28539. 'type',
  28540. ' TObject = class',
  28541. ' FB: boolean;',
  28542. ' FI: longint;',
  28543. ' FE: TEnum;',
  28544. ' published',
  28545. ' property B1: boolean read FB default true;',
  28546. ' property B2: boolean read FB default CB;',
  28547. ' property B3: boolean read FB default test1.cb;',
  28548. ' property I1: longint read FI default 2;',
  28549. ' property I2: longint read FI default CI;',
  28550. ' property E1: TEnum read FE default red;',
  28551. ' property E2: TEnum read FE default TEnum.blue;',
  28552. ' end;',
  28553. 'begin']);
  28554. ConvertProgram;
  28555. CheckSource('TestRTTI_DefaultValue',
  28556. LinesToStr([ // statements
  28557. 'this.TEnum = {',
  28558. ' "0": "red",',
  28559. ' red: 0,',
  28560. ' "1": "blue",',
  28561. ' blue: 1',
  28562. '};',
  28563. '$mod.$rtti.$Enum("TEnum", {',
  28564. ' minvalue: 0,',
  28565. ' maxvalue: 1,',
  28566. ' ordtype: 1,',
  28567. ' enumtype: this.TEnum',
  28568. '});',
  28569. 'this.CB = true || false;',
  28570. 'this.CI = 1 + 2;',
  28571. 'rtl.createClass($mod, "TObject", null, function () {',
  28572. ' this.$init = function () {',
  28573. ' this.FB = false;',
  28574. ' this.FI = 0;',
  28575. ' this.FE = 0;',
  28576. ' };',
  28577. ' this.$final = function () {',
  28578. ' };',
  28579. ' var $r = this.$rtti;',
  28580. ' $r.addProperty(',
  28581. ' "B1",',
  28582. ' 0,',
  28583. ' rtl.boolean,',
  28584. ' "FB",',
  28585. ' "",',
  28586. ' {',
  28587. ' Default: true',
  28588. ' }',
  28589. ' );',
  28590. ' $r.addProperty(',
  28591. ' "B2",',
  28592. ' 0,',
  28593. ' rtl.boolean,',
  28594. ' "FB",',
  28595. ' "",',
  28596. ' {',
  28597. ' Default: true',
  28598. ' }',
  28599. ' );',
  28600. ' $r.addProperty(',
  28601. ' "B3",',
  28602. ' 0,',
  28603. ' rtl.boolean,',
  28604. ' "FB",',
  28605. ' "",',
  28606. ' {',
  28607. ' Default: true',
  28608. ' }',
  28609. ' );',
  28610. ' $r.addProperty(',
  28611. ' "I1",',
  28612. ' 0,',
  28613. ' rtl.longint,',
  28614. ' "FI",',
  28615. ' "",',
  28616. ' {',
  28617. ' Default: 2',
  28618. ' }',
  28619. ' );',
  28620. ' $r.addProperty(',
  28621. ' "I2",',
  28622. ' 0,',
  28623. ' rtl.longint,',
  28624. ' "FI",',
  28625. ' "",',
  28626. ' {',
  28627. ' Default: 3',
  28628. ' }',
  28629. ' );',
  28630. ' $r.addProperty(',
  28631. ' "E1",',
  28632. ' 0,',
  28633. ' $mod.$rtti["TEnum"],',
  28634. ' "FE",',
  28635. ' "",',
  28636. ' {',
  28637. ' Default: $mod.TEnum.red',
  28638. ' }',
  28639. ' );',
  28640. ' $r.addProperty(',
  28641. ' "E2",',
  28642. ' 0,',
  28643. ' $mod.$rtti["TEnum"],',
  28644. ' "FE",',
  28645. ' "",',
  28646. ' {',
  28647. ' Default: $mod.TEnum.blue',
  28648. ' }',
  28649. ' );',
  28650. '});',
  28651. '']),
  28652. LinesToStr([ // $mod.$main
  28653. '']));
  28654. end;
  28655. procedure TTestModule.TestRTTI_DefaultValueSet;
  28656. begin
  28657. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28658. StartProgram(false);
  28659. Add([
  28660. 'type',
  28661. ' TEnum = (red, blue);',
  28662. ' TSet = set of TEnum;',
  28663. 'const',
  28664. ' CSet = [red,blue];',
  28665. 'type',
  28666. ' TObject = class',
  28667. ' FSet: TSet;',
  28668. ' published',
  28669. ' property Set1: TSet read FSet default [];',
  28670. ' property Set2: TSet read FSet default [red];',
  28671. ' property Set3: TSet read FSet default [red,blue];',
  28672. ' property Set4: TSet read FSet default CSet;',
  28673. ' end;',
  28674. 'begin']);
  28675. ConvertProgram;
  28676. CheckSource('TestRTTI_DefaultValueSet',
  28677. LinesToStr([ // statements
  28678. 'this.TEnum = {',
  28679. ' "0": "red",',
  28680. ' red: 0,',
  28681. ' "1": "blue",',
  28682. ' blue: 1',
  28683. '};',
  28684. '$mod.$rtti.$Enum("TEnum", {',
  28685. ' minvalue: 0,',
  28686. ' maxvalue: 1,',
  28687. ' ordtype: 1,',
  28688. ' enumtype: this.TEnum',
  28689. '});',
  28690. '$mod.$rtti.$Set("TSet", {',
  28691. ' comptype: $mod.$rtti["TEnum"]',
  28692. '});',
  28693. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  28694. 'rtl.createClass($mod, "TObject", null, function () {',
  28695. ' this.$init = function () {',
  28696. ' this.FSet = {};',
  28697. ' };',
  28698. ' this.$final = function () {',
  28699. ' this.FSet = undefined;',
  28700. ' };',
  28701. ' var $r = this.$rtti;',
  28702. ' $r.addProperty(',
  28703. ' "Set1",',
  28704. ' 0,',
  28705. ' $mod.$rtti["TSet"],',
  28706. ' "FSet",',
  28707. ' "",',
  28708. ' {',
  28709. ' Default: {}',
  28710. ' }',
  28711. ' );',
  28712. ' $r.addProperty(',
  28713. ' "Set2",',
  28714. ' 0,',
  28715. ' $mod.$rtti["TSet"],',
  28716. ' "FSet",',
  28717. ' "",',
  28718. ' {',
  28719. ' Default: rtl.createSet($mod.TEnum.red)',
  28720. ' }',
  28721. ' );',
  28722. ' $r.addProperty(',
  28723. ' "Set3",',
  28724. ' 0,',
  28725. ' $mod.$rtti["TSet"],',
  28726. ' "FSet",',
  28727. ' "",',
  28728. ' {',
  28729. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  28730. ' }',
  28731. ' );',
  28732. ' $r.addProperty(',
  28733. ' "Set4",',
  28734. ' 0,',
  28735. ' $mod.$rtti["TSet"],',
  28736. ' "FSet",',
  28737. ' "",',
  28738. ' {',
  28739. ' Default: $mod.CSet',
  28740. ' }',
  28741. ' );',
  28742. '});',
  28743. '']),
  28744. LinesToStr([ // $mod.$main
  28745. '']));
  28746. end;
  28747. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  28748. begin
  28749. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28750. StartProgram(false);
  28751. Add([
  28752. 'type',
  28753. ' TRg = -1..1;',
  28754. 'const',
  28755. ' l = low(TRg);',
  28756. ' h = high(TRg);',
  28757. 'type',
  28758. ' TObject = class',
  28759. ' FV: TRg;',
  28760. ' published',
  28761. ' property V1: TRg read FV default -1;',
  28762. ' end;',
  28763. 'begin']);
  28764. ConvertProgram;
  28765. CheckSource('TestRTTI_DefaultValueRangeType',
  28766. LinesToStr([ // statements
  28767. '$mod.$rtti.$Int("TRg", {',
  28768. ' minvalue: -1,',
  28769. ' maxvalue: 1,',
  28770. ' ordtype: 0',
  28771. '});',
  28772. 'this.l = -1;',
  28773. 'this.h = 1;',
  28774. 'rtl.createClass($mod, "TObject", null, function () {',
  28775. ' this.$init = function () {',
  28776. ' this.FV = 0;',
  28777. ' };',
  28778. ' this.$final = function () {',
  28779. ' };',
  28780. ' var $r = this.$rtti;',
  28781. ' $r.addProperty(',
  28782. ' "V1",',
  28783. ' 0,',
  28784. ' $mod.$rtti["TRg"],',
  28785. ' "FV",',
  28786. ' "",',
  28787. ' {',
  28788. ' Default: -1',
  28789. ' }',
  28790. ' );',
  28791. '});',
  28792. '']),
  28793. LinesToStr([ // $mod.$main
  28794. '']));
  28795. end;
  28796. procedure TTestModule.TestRTTI_DefaultValueInherit;
  28797. begin
  28798. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28799. StartProgram(false);
  28800. Add([
  28801. 'type',
  28802. ' TObject = class',
  28803. ' FA, FB: byte;',
  28804. ' property A: byte read FA default 1;',
  28805. ' property B: byte read FB default 2;',
  28806. ' end;',
  28807. ' TBird = class',
  28808. ' published',
  28809. ' property A;',
  28810. ' property B nodefault;',
  28811. ' end;',
  28812. 'begin']);
  28813. ConvertProgram;
  28814. CheckSource('TestRTTI_DefaultValueInherit',
  28815. LinesToStr([ // statements
  28816. 'rtl.createClass($mod, "TObject", null, function () {',
  28817. ' this.$init = function () {',
  28818. ' this.FA = 0;',
  28819. ' this.FB = 0;',
  28820. ' };',
  28821. ' this.$final = function () {',
  28822. ' };',
  28823. '});',
  28824. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28825. ' var $r = this.$rtti;',
  28826. ' $r.addProperty(',
  28827. ' "A",',
  28828. ' 0,',
  28829. ' rtl.byte,',
  28830. ' "FA",',
  28831. ' "",',
  28832. ' {',
  28833. ' Default: 1',
  28834. ' }',
  28835. ' );',
  28836. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  28837. '});',
  28838. '']),
  28839. LinesToStr([ // $mod.$main
  28840. '']));
  28841. end;
  28842. procedure TTestModule.TestRTTI_OverrideMethod;
  28843. begin
  28844. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28845. StartProgram(false);
  28846. Add('type');
  28847. Add(' TObject = class');
  28848. Add(' published');
  28849. Add(' procedure DoIt; virtual; abstract;');
  28850. Add(' end;');
  28851. Add(' TSky = class');
  28852. Add(' published');
  28853. Add(' procedure DoIt; override;');
  28854. Add(' end;');
  28855. Add('procedure TSky.DoIt; begin end;');
  28856. Add('begin');
  28857. ConvertProgram;
  28858. CheckSource('TestRTTI_OverrideMethod',
  28859. LinesToStr([ // statements
  28860. 'rtl.createClass($mod, "TObject", null, function () {',
  28861. ' this.$init = function () {',
  28862. ' };',
  28863. ' this.$final = function () {',
  28864. ' };',
  28865. ' var $r = this.$rtti;',
  28866. ' $r.addMethod("DoIt", 0, null);',
  28867. '});',
  28868. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28869. ' this.DoIt = function () {',
  28870. ' };',
  28871. '});',
  28872. '']),
  28873. LinesToStr([ // $mod.$main
  28874. '']));
  28875. end;
  28876. procedure TTestModule.TestRTTI_ReintroduceMethod;
  28877. begin
  28878. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28879. StartProgram(false);
  28880. Add([
  28881. 'type',
  28882. ' TObject = class',
  28883. ' published',
  28884. ' procedure DoIt;',
  28885. ' end;',
  28886. ' TSky = class',
  28887. ' published',
  28888. ' procedure DoIt; reintroduce;',
  28889. ' end;',
  28890. 'procedure TObject.DoIt; begin end;',
  28891. 'procedure TSky.DoIt;',
  28892. 'begin',
  28893. ' inherited DoIt;',
  28894. 'end;',
  28895. 'begin']);
  28896. ConvertProgram;
  28897. CheckSource('TestRTTI_ReintroduceMethod',
  28898. LinesToStr([ // statements
  28899. 'rtl.createClass($mod, "TObject", null, function () {',
  28900. ' this.$init = function () {',
  28901. ' };',
  28902. ' this.$final = function () {',
  28903. ' };',
  28904. ' this.DoIt = function () {',
  28905. ' };',
  28906. ' var $r = this.$rtti;',
  28907. ' $r.addMethod("DoIt", 0, null);',
  28908. '});',
  28909. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28910. ' this.DoIt = function () {',
  28911. ' $mod.TObject.DoIt.call(this);',
  28912. ' };',
  28913. ' var $r = this.$rtti;',
  28914. ' $r.addMethod("DoIt", 0, null);',
  28915. '});',
  28916. '']),
  28917. LinesToStr([ // $mod.$main
  28918. '']));
  28919. end;
  28920. procedure TTestModule.TestRTTI_OverloadProperty;
  28921. begin
  28922. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28923. StartProgram(false);
  28924. Add('type');
  28925. Add(' TObject = class');
  28926. Add(' protected');
  28927. Add(' FFlag: longint;');
  28928. Add(' published');
  28929. Add(' property Flag: longint read fflag;');
  28930. Add(' end;');
  28931. Add(' TSky = class');
  28932. Add(' published');
  28933. Add(' property FLAG: longint write fflag;');
  28934. Add(' end;');
  28935. Add('begin');
  28936. ConvertProgram;
  28937. CheckSource('TestRTTI_OverrideMethod',
  28938. LinesToStr([ // statements
  28939. 'rtl.createClass($mod, "TObject", null, function () {',
  28940. ' this.$init = function () {',
  28941. ' this.FFlag = 0;',
  28942. ' };',
  28943. ' this.$final = function () {',
  28944. ' };',
  28945. ' var $r = this.$rtti;',
  28946. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  28947. '});',
  28948. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28949. ' var $r = this.$rtti;',
  28950. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  28951. '});',
  28952. '']),
  28953. LinesToStr([ // $mod.$main
  28954. '']));
  28955. end;
  28956. procedure TTestModule.TestRTTI_ClassForward;
  28957. begin
  28958. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28959. StartProgram(false);
  28960. Add('type');
  28961. Add(' TObject = class end;');
  28962. Add(' tbridge = class;');
  28963. Add(' TProc = function: tbridge;');
  28964. Add(' TOger = class');
  28965. Add(' published');
  28966. Add(' FBridge: tbridge;');
  28967. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  28968. Add(' property Bridge: tbridge read fbridge write setbridge;');
  28969. Add(' end;');
  28970. Add(' TBridge = class');
  28971. Add(' FOger: toger;');
  28972. Add(' end;');
  28973. Add('var p: Pointer;');
  28974. Add(' b: tbridge;');
  28975. Add('begin');
  28976. Add(' p:=typeinfo(tbridge);');
  28977. Add(' p:=typeinfo(b);');
  28978. ConvertProgram;
  28979. CheckSource('TestRTTI_ClassForward',
  28980. LinesToStr([ // statements
  28981. 'rtl.createClass($mod, "TObject", null, function () {',
  28982. ' this.$init = function () {',
  28983. ' };',
  28984. ' this.$final = function () {',
  28985. ' };',
  28986. '});',
  28987. '$mod.$rtti.$Class("TBridge");',
  28988. '$mod.$rtti.$ProcVar("TProc", {',
  28989. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  28990. '});',
  28991. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  28992. ' this.$init = function () {',
  28993. ' $mod.TObject.$init.call(this);',
  28994. ' this.FBridge = null;',
  28995. ' };',
  28996. ' this.$final = function () {',
  28997. ' this.FBridge = undefined;',
  28998. ' $mod.TObject.$final.call(this);',
  28999. ' };',
  29000. ' var $r = this.$rtti;',
  29001. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29002. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29003. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29004. '});',
  29005. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  29006. ' this.$init = function () {',
  29007. ' $mod.TObject.$init.call(this);',
  29008. ' this.FOger = null;',
  29009. ' };',
  29010. ' this.$final = function () {',
  29011. ' this.FOger = undefined;',
  29012. ' $mod.TObject.$final.call(this);',
  29013. ' };',
  29014. '});',
  29015. 'this.p = null;',
  29016. 'this.b = null;',
  29017. '']),
  29018. LinesToStr([ // $mod.$main
  29019. '$mod.p = $mod.$rtti["TBridge"];',
  29020. '$mod.p = $mod.b.$rtti;',
  29021. '']));
  29022. end;
  29023. procedure TTestModule.TestRTTI_ClassOf;
  29024. begin
  29025. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29026. StartProgram(false);
  29027. Add('type');
  29028. Add(' TClass = class of tobject;');
  29029. Add(' TProcA = function: TClass;');
  29030. Add(' TObject = class');
  29031. Add(' published');
  29032. Add(' C: tclass;');
  29033. Add(' end;');
  29034. Add(' tfox = class;');
  29035. Add(' TBird = class end;');
  29036. Add(' TBirds = class of tbird;');
  29037. Add(' TFox = class end;');
  29038. Add(' TFoxes = class of tfox;');
  29039. Add(' TCows = class of TCow;');
  29040. Add(' TCow = class;');
  29041. Add(' TCow = class end;');
  29042. Add('begin');
  29043. ConvertProgram;
  29044. CheckSource('TestRTTI_ClassOf',
  29045. LinesToStr([ // statements
  29046. '$mod.$rtti.$Class("TObject");',
  29047. '$mod.$rtti.$ClassRef("TClass", {',
  29048. ' instancetype: $mod.$rtti["TObject"]',
  29049. '});',
  29050. '$mod.$rtti.$ProcVar("TProcA", {',
  29051. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  29052. '});',
  29053. 'rtl.createClass($mod, "TObject", null, function () {',
  29054. ' this.$init = function () {',
  29055. ' this.C = null;',
  29056. ' };',
  29057. ' this.$final = function () {',
  29058. ' this.C = undefined;',
  29059. ' };',
  29060. ' var $r = this.$rtti;',
  29061. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29062. '});',
  29063. '$mod.$rtti.$Class("TFox");',
  29064. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29065. '});',
  29066. '$mod.$rtti.$ClassRef("TBirds", {',
  29067. ' instancetype: $mod.$rtti["TBird"]',
  29068. '});',
  29069. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  29070. '});',
  29071. '$mod.$rtti.$ClassRef("TFoxes", {',
  29072. ' instancetype: $mod.$rtti["TFox"]',
  29073. '});',
  29074. '$mod.$rtti.$Class("TCow");',
  29075. '$mod.$rtti.$ClassRef("TCows", {',
  29076. ' instancetype: $mod.$rtti["TCow"]',
  29077. '});',
  29078. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  29079. '});',
  29080. '']),
  29081. LinesToStr([ // $mod.$main
  29082. '']));
  29083. end;
  29084. procedure TTestModule.TestRTTI_Record;
  29085. begin
  29086. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29087. StartProgram(false);
  29088. Add('type');
  29089. Add(' integer = longint;');
  29090. Add(' TPoint = record');
  29091. Add(' x,y: integer;');
  29092. Add(' end;');
  29093. Add('var p: pointer;');
  29094. Add(' r: tpoint;');
  29095. Add('begin');
  29096. Add(' p:=typeinfo(tpoint);');
  29097. Add(' p:=typeinfo(r);');
  29098. Add(' p:=typeinfo(r.x);');
  29099. ConvertProgram;
  29100. CheckSource('TestRTTI_Record',
  29101. LinesToStr([ // statements
  29102. 'rtl.recNewT($mod, "TPoint", function () {',
  29103. ' this.x = 0;',
  29104. ' this.y = 0;',
  29105. ' this.$eq = function (b) {',
  29106. ' return (this.x === b.x) && (this.y === b.y);',
  29107. ' };',
  29108. ' this.$assign = function (s) {',
  29109. ' this.x = s.x;',
  29110. ' this.y = s.y;',
  29111. ' return this;',
  29112. ' };',
  29113. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29114. ' $r.addField("x", rtl.longint);',
  29115. ' $r.addField("y", rtl.longint);',
  29116. '});',
  29117. 'this.p = null;',
  29118. 'this.r = $mod.TPoint.$new();',
  29119. '']),
  29120. LinesToStr([ // $mod.$main
  29121. '$mod.p = $mod.$rtti["TPoint"];',
  29122. '$mod.p = $mod.$rtti["TPoint"];',
  29123. '$mod.p = rtl.longint;',
  29124. '']));
  29125. end;
  29126. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29127. begin
  29128. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29129. StartProgram(false);
  29130. Add('type');
  29131. Add(' TFloatRec = record');
  29132. Add(' d: array of char;');
  29133. // Add(' i: array of array of longint;');
  29134. Add(' end;');
  29135. Add('var p: pointer;');
  29136. Add(' r: tfloatrec;');
  29137. Add('begin');
  29138. Add(' p:=typeinfo(tfloatrec);');
  29139. Add(' p:=typeinfo(r);');
  29140. Add(' p:=typeinfo(r.d);');
  29141. ConvertProgram;
  29142. CheckSource('TestRTTI_Record',
  29143. LinesToStr([ // statements
  29144. 'rtl.recNewT($mod, "TFloatRec", function () {',
  29145. ' this.d = [];',
  29146. ' this.$eq = function (b) {',
  29147. ' return this.d === b.d;',
  29148. ' };',
  29149. ' this.$assign = function (s) {',
  29150. ' this.d = s.d;',
  29151. ' return this;',
  29152. ' };',
  29153. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29154. ' eltype: rtl.char',
  29155. ' });',
  29156. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29157. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29158. '});',
  29159. 'this.p = null;',
  29160. 'this.r = $mod.TFloatRec.$new();',
  29161. '']),
  29162. LinesToStr([ // $mod.$main
  29163. '$mod.p = $mod.$rtti["TFloatRec"];',
  29164. '$mod.p = $mod.$rtti["TFloatRec"];',
  29165. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29166. '']));
  29167. end;
  29168. procedure TTestModule.TestRTTI_LocalTypes;
  29169. begin
  29170. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29171. StartProgram(false);
  29172. Add([
  29173. 'procedure DoIt;',
  29174. 'type',
  29175. ' integer = longint;',
  29176. ' TPoint = record',
  29177. ' x,y: integer;',
  29178. ' end;',
  29179. 'var p: TPoint;',
  29180. 'begin',
  29181. 'end;',
  29182. 'begin']);
  29183. ConvertProgram;
  29184. CheckSource('TestRTTI_LocalTypes',
  29185. LinesToStr([ // statements
  29186. 'var TPoint = rtl.recNewT(null, "", function () {',
  29187. ' this.x = 0;',
  29188. ' this.y = 0;',
  29189. ' this.$eq = function (b) {',
  29190. ' return (this.x === b.x) && (this.y === b.y);',
  29191. ' };',
  29192. ' this.$assign = function (s) {',
  29193. ' this.x = s.x;',
  29194. ' this.y = s.y;',
  29195. ' return this;',
  29196. ' };',
  29197. '});',
  29198. 'this.DoIt = function () {',
  29199. ' var p = TPoint.$new();',
  29200. '};',
  29201. '']),
  29202. LinesToStr([ // $mod.$main
  29203. '']));
  29204. end;
  29205. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29206. begin
  29207. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29208. StartProgram(false);
  29209. Add([
  29210. 'type',
  29211. ' TCaption = string;',
  29212. ' TYesNo = boolean;',
  29213. ' TLetter = char;',
  29214. ' TFloat = double;',
  29215. ' TPtr = pointer;',
  29216. ' TShortInt = shortint;',
  29217. ' TByte = byte;',
  29218. ' TSmallInt = smallint;',
  29219. ' TWord = word;',
  29220. ' TInt32 = longint;',
  29221. ' TDWord = longword;',
  29222. ' TValue = jsvalue;',
  29223. 'var p: TPtr;',
  29224. 'begin',
  29225. ' p:=typeinfo(string);',
  29226. ' p:=typeinfo(tcaption);',
  29227. ' p:=typeinfo(boolean);',
  29228. ' p:=typeinfo(tyesno);',
  29229. ' p:=typeinfo(char);',
  29230. ' p:=typeinfo(tletter);',
  29231. ' p:=typeinfo(double);',
  29232. ' p:=typeinfo(tfloat);',
  29233. ' p:=typeinfo(pointer);',
  29234. ' p:=typeinfo(tptr);',
  29235. ' p:=typeinfo(shortint);',
  29236. ' p:=typeinfo(tshortint);',
  29237. ' p:=typeinfo(byte);',
  29238. ' p:=typeinfo(tbyte);',
  29239. ' p:=typeinfo(smallint);',
  29240. ' p:=typeinfo(tsmallint);',
  29241. ' p:=typeinfo(word);',
  29242. ' p:=typeinfo(tword);',
  29243. ' p:=typeinfo(longword);',
  29244. ' p:=typeinfo(tdword);',
  29245. ' p:=typeinfo(jsvalue);',
  29246. ' p:=typeinfo(tvalue);',
  29247. '']);
  29248. ConvertProgram;
  29249. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29250. LinesToStr([ // statements
  29251. 'this.p = null;',
  29252. '']),
  29253. LinesToStr([ // $mod.$main
  29254. '$mod.p = rtl.string;',
  29255. '$mod.p = rtl.string;',
  29256. '$mod.p = rtl.boolean;',
  29257. '$mod.p = rtl.boolean;',
  29258. '$mod.p = rtl.char;',
  29259. '$mod.p = rtl.char;',
  29260. '$mod.p = rtl.double;',
  29261. '$mod.p = rtl.double;',
  29262. '$mod.p = rtl.pointer;',
  29263. '$mod.p = rtl.pointer;',
  29264. '$mod.p = rtl.shortint;',
  29265. '$mod.p = rtl.shortint;',
  29266. '$mod.p = rtl.byte;',
  29267. '$mod.p = rtl.byte;',
  29268. '$mod.p = rtl.smallint;',
  29269. '$mod.p = rtl.smallint;',
  29270. '$mod.p = rtl.word;',
  29271. '$mod.p = rtl.word;',
  29272. '$mod.p = rtl.longword;',
  29273. '$mod.p = rtl.longword;',
  29274. '$mod.p = rtl.jsvalue;',
  29275. '$mod.p = rtl.jsvalue;',
  29276. '']));
  29277. end;
  29278. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29279. begin
  29280. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29281. StartProgram(false);
  29282. Add([
  29283. 'type',
  29284. ' TCaption = type string;',
  29285. ' TYesNo = type boolean;',
  29286. ' TLetter = type char;',
  29287. ' TFloat = type double;',
  29288. ' TPtr = type pointer;',
  29289. ' TShortInt = type shortint;',
  29290. ' TByte = type byte;',
  29291. ' TSmallInt = type smallint;',
  29292. ' TWord = type word;',
  29293. ' TInt32 = type longint;',
  29294. ' TDWord = type longword;',
  29295. ' TValue = type jsvalue;',
  29296. ' TAliasValue = type TValue;',
  29297. 'var',
  29298. ' p: TPtr;',
  29299. ' a: TAliasValue;',
  29300. 'begin',
  29301. ' p:=typeinfo(tcaption);',
  29302. ' p:=typeinfo(tyesno);',
  29303. ' p:=typeinfo(tletter);',
  29304. ' p:=typeinfo(tfloat);',
  29305. ' p:=typeinfo(tptr);',
  29306. ' p:=typeinfo(tshortint);',
  29307. ' p:=typeinfo(tbyte);',
  29308. ' p:=typeinfo(tsmallint);',
  29309. ' p:=typeinfo(tword);',
  29310. ' p:=typeinfo(tdword);',
  29311. ' p:=typeinfo(tvalue);',
  29312. ' p:=typeinfo(taliasvalue);',
  29313. ' p:=typeinfo(a);',
  29314. '']);
  29315. ConvertProgram;
  29316. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29317. LinesToStr([ // statements
  29318. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  29319. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29320. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  29321. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  29322. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29323. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29324. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  29325. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29326. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  29327. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  29328. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  29329. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29330. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  29331. 'this.p = null;',
  29332. 'this.a = undefined;',
  29333. '']),
  29334. LinesToStr([ // $mod.$main
  29335. '$mod.p = $mod.$rtti["TCaption"];',
  29336. '$mod.p = $mod.$rtti["TYesNo"];',
  29337. '$mod.p = $mod.$rtti["TLetter"];',
  29338. '$mod.p = $mod.$rtti["TFloat"];',
  29339. '$mod.p = $mod.$rtti["TPtr"];',
  29340. '$mod.p = $mod.$rtti["TShortInt"];',
  29341. '$mod.p = $mod.$rtti["TByte"];',
  29342. '$mod.p = $mod.$rtti["TSmallInt"];',
  29343. '$mod.p = $mod.$rtti["TWord"];',
  29344. '$mod.p = $mod.$rtti["TDWord"];',
  29345. '$mod.p = $mod.$rtti["TValue"];',
  29346. '$mod.p = $mod.$rtti["TAliasValue"];',
  29347. '$mod.p = $mod.$rtti["TAliasValue"];',
  29348. '']));
  29349. end;
  29350. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29351. begin
  29352. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29353. StartProgram(false);
  29354. Add('procedure DoIt;');
  29355. Add('type');
  29356. Add(' integer = longint;');
  29357. Add(' TPoint = record');
  29358. Add(' x,y: integer;');
  29359. Add(' end;');
  29360. Add('var p: pointer;');
  29361. Add('begin');
  29362. Add(' p:=typeinfo(tpoint);');
  29363. Add('end;');
  29364. Add('begin');
  29365. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29366. ConvertProgram;
  29367. end;
  29368. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29369. begin
  29370. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29371. StartProgram(true,[supTypeInfo]);
  29372. Add([
  29373. '{$modeswitch externalclass}',
  29374. 'type',
  29375. ' TFlag = (up,down);',
  29376. ' TFlags = set of TFlag;',
  29377. 'var',
  29378. ' ti: TTypeInfo;',
  29379. ' tiInt: TTypeInfoInteger;',
  29380. ' tiEnum: TTypeInfoEnum;',
  29381. ' tiSet: TTypeInfoSet;',
  29382. 'begin',
  29383. ' ti:=typeinfo(string);',
  29384. ' ti:=typeinfo(boolean);',
  29385. ' ti:=typeinfo(char);',
  29386. ' ti:=typeinfo(double);',
  29387. ' tiInt:=typeinfo(shortint);',
  29388. ' tiInt:=typeinfo(byte);',
  29389. ' tiInt:=typeinfo(smallint);',
  29390. ' tiInt:=typeinfo(word);',
  29391. ' tiInt:=typeinfo(longint);',
  29392. ' tiInt:=typeinfo(longword);',
  29393. ' ti:=typeinfo(jsvalue);',
  29394. ' tiEnum:=typeinfo(tflag);',
  29395. ' tiSet:=typeinfo(tflags);']);
  29396. ConvertProgram;
  29397. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29398. LinesToStr([ // statements
  29399. 'this.TFlag = {',
  29400. ' "0": "up",',
  29401. ' up: 0,',
  29402. ' "1": "down",',
  29403. ' down: 1',
  29404. '};',
  29405. '$mod.$rtti.$Enum("TFlag", {',
  29406. ' minvalue: 0,',
  29407. ' maxvalue: 1,',
  29408. ' ordtype: 1,',
  29409. ' enumtype: this.TFlag',
  29410. '});',
  29411. '$mod.$rtti.$Set("TFlags", {',
  29412. ' comptype: $mod.$rtti["TFlag"]',
  29413. '});',
  29414. 'this.ti = null;',
  29415. 'this.tiInt = null;',
  29416. 'this.tiEnum = null;',
  29417. 'this.tiSet = null;',
  29418. '']),
  29419. LinesToStr([ // $mod.$main
  29420. '$mod.ti = rtl.string;',
  29421. '$mod.ti = rtl.boolean;',
  29422. '$mod.ti = rtl.char;',
  29423. '$mod.ti = rtl.double;',
  29424. '$mod.tiInt = rtl.shortint;',
  29425. '$mod.tiInt = rtl.byte;',
  29426. '$mod.tiInt = rtl.smallint;',
  29427. '$mod.tiInt = rtl.word;',
  29428. '$mod.tiInt = rtl.longint;',
  29429. '$mod.tiInt = rtl.longword;',
  29430. '$mod.ti = rtl.jsvalue;',
  29431. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29432. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29433. '']));
  29434. end;
  29435. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29436. begin
  29437. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29438. StartProgram(true,[supTypeInfo]);
  29439. Add('{$modeswitch externalclass}');
  29440. Add('type');
  29441. Add(' TStaticArr = array[boolean] of string;');
  29442. Add(' TDynArr = array of string;');
  29443. Add(' TProc = procedure;');
  29444. Add(' TMethod = procedure of object;');
  29445. Add('var');
  29446. Add(' StaticArray: TStaticArr;');
  29447. Add(' tiStaticArray: TTypeInfoStaticArray;');
  29448. Add(' DynArray: TDynArr;');
  29449. Add(' tiDynArray: TTypeInfoDynArray;');
  29450. Add(' ProcVar: TProc;');
  29451. Add(' tiProcVar: TTypeInfoProcVar;');
  29452. Add(' MethodVar: TMethod;');
  29453. Add(' tiMethodVar: TTypeInfoMethodVar;');
  29454. Add('begin');
  29455. Add(' tiStaticArray:=typeinfo(StaticArray);');
  29456. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  29457. Add(' tiDynArray:=typeinfo(DynArray);');
  29458. Add(' tiDynArray:=typeinfo(TDynArr);');
  29459. Add(' tiProcVar:=typeinfo(ProcVar);');
  29460. Add(' tiProcVar:=typeinfo(TProc);');
  29461. Add(' tiMethodVar:=typeinfo(MethodVar);');
  29462. Add(' tiMethodVar:=typeinfo(TMethod);');
  29463. ConvertProgram;
  29464. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  29465. LinesToStr([ // statements
  29466. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  29467. ' dims: [2],',
  29468. ' eltype: rtl.string',
  29469. '});',
  29470. '$mod.$rtti.$DynArray("TDynArr", {',
  29471. ' eltype: rtl.string',
  29472. '});',
  29473. '$mod.$rtti.$ProcVar("TProc", {',
  29474. ' procsig: rtl.newTIProcSig(null)',
  29475. '});',
  29476. '$mod.$rtti.$MethodVar("TMethod", {',
  29477. ' procsig: rtl.newTIProcSig(null),',
  29478. ' methodkind: 0',
  29479. '});',
  29480. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  29481. 'this.tiStaticArray = null;',
  29482. 'this.DynArray = [];',
  29483. 'this.tiDynArray = null;',
  29484. 'this.ProcVar = null;',
  29485. 'this.tiProcVar = null;',
  29486. 'this.MethodVar = null;',
  29487. 'this.tiMethodVar = null;',
  29488. '']),
  29489. LinesToStr([ // $mod.$main
  29490. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29491. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29492. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29493. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29494. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29495. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29496. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29497. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29498. '']));
  29499. end;
  29500. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  29501. begin
  29502. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29503. StartProgram(true,[supTypeInfo]);
  29504. Add('{$modeswitch externalclass}');
  29505. Add('type');
  29506. Add(' TRec = record end;');
  29507. // ToDo: ^PRec
  29508. Add(' TObject = class end;');
  29509. Add(' TClass = class of tobject;');
  29510. Add('var');
  29511. Add(' Rec: trec;');
  29512. Add(' tiRecord: ttypeinforecord;');
  29513. Add(' Obj: tobject;');
  29514. Add(' tiClass: ttypeinfoclass;');
  29515. Add(' aClass: tclass;');
  29516. Add(' tiClassRef: ttypeinfoclassref;');
  29517. // ToDo: ^PRec
  29518. Add(' tiPointer: ttypeinfopointer;');
  29519. Add('begin');
  29520. Add(' tirecord:=typeinfo(trec);');
  29521. Add(' tirecord:=typeinfo(trec);');
  29522. Add(' ticlass:=typeinfo(obj);');
  29523. Add(' ticlass:=typeinfo(tobject);');
  29524. Add(' ticlass:=typeinfo(aclass);');
  29525. Add(' ticlassref:=typeinfo(tclass);');
  29526. ConvertProgram;
  29527. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  29528. LinesToStr([ // statements
  29529. 'rtl.recNewT($mod, "TRec", function () {',
  29530. ' this.$eq = function (b) {',
  29531. ' return true;',
  29532. ' };',
  29533. ' this.$assign = function (s) {',
  29534. ' return this;',
  29535. ' };',
  29536. ' $mod.$rtti.$Record("TRec", {});',
  29537. '});',
  29538. 'rtl.createClass($mod, "TObject", null, function () {',
  29539. ' this.$init = function () {',
  29540. ' };',
  29541. ' this.$final = function () {',
  29542. ' };',
  29543. '});',
  29544. '$mod.$rtti.$ClassRef("TClass", {',
  29545. ' instancetype: $mod.$rtti["TObject"]',
  29546. '});',
  29547. 'this.Rec = $mod.TRec.$new();',
  29548. 'this.tiRecord = null;',
  29549. 'this.Obj = null;',
  29550. 'this.tiClass = null;',
  29551. 'this.aClass = null;',
  29552. 'this.tiClassRef = null;',
  29553. 'this.tiPointer = null;',
  29554. '']),
  29555. LinesToStr([ // $mod.$main
  29556. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29557. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29558. '$mod.tiClass = $mod.Obj.$rtti;',
  29559. '$mod.tiClass = $mod.$rtti["TObject"];',
  29560. '$mod.tiClass = $mod.aClass.$rtti;',
  29561. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  29562. '']));
  29563. end;
  29564. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  29565. begin
  29566. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29567. StartProgram(true,[supTypeInfo]);
  29568. Add([
  29569. '{$modeswitch externalclass}',
  29570. 'type',
  29571. ' TClass = class of tobject;',
  29572. ' TObject = class',
  29573. ' function MyClass: TClass;',
  29574. ' class function ClassType: TClass;',
  29575. ' end;',
  29576. 'function TObject.MyClass: TClass;',
  29577. 'var t: TTypeInfoClass;',
  29578. 'begin',
  29579. ' t:=TypeInfo(Self);',
  29580. ' t:=TypeInfo(Result);',
  29581. ' t:=TypeInfo(TObject);',
  29582. 'end;',
  29583. 'class function TObject.ClassType: TClass;',
  29584. 'var t: TTypeInfoClass;',
  29585. 'begin',
  29586. ' t:=TypeInfo(Self);',
  29587. ' t:=TypeInfo(Result);',
  29588. 'end;',
  29589. 'var',
  29590. ' Obj: TObject;',
  29591. ' t: TTypeInfoClass;',
  29592. 'begin',
  29593. ' t:=TypeInfo(TObject.ClassType);',
  29594. ' t:=TypeInfo(Obj.ClassType);',
  29595. ' t:=TypeInfo(Obj.MyClass);',
  29596. '']);
  29597. ConvertProgram;
  29598. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  29599. LinesToStr([ // statements
  29600. '$mod.$rtti.$Class("TObject");',
  29601. '$mod.$rtti.$ClassRef("TClass", {',
  29602. ' instancetype: $mod.$rtti["TObject"]',
  29603. '});',
  29604. 'rtl.createClass($mod, "TObject", null, function () {',
  29605. ' this.$init = function () {',
  29606. ' };',
  29607. ' this.$final = function () {',
  29608. ' };',
  29609. ' this.MyClass = function () {',
  29610. ' var Result = null;',
  29611. ' var t = null;',
  29612. ' t = this.$rtti;',
  29613. ' t = Result.$rtti;',
  29614. ' t = $mod.$rtti["TObject"];',
  29615. ' return Result;',
  29616. ' };',
  29617. ' this.ClassType = function () {',
  29618. ' var Result = null;',
  29619. ' var t = null;',
  29620. ' t = this.$rtti;',
  29621. ' t = Result.$rtti;',
  29622. ' return Result;',
  29623. ' };',
  29624. '});',
  29625. 'this.Obj = null;',
  29626. 'this.t = null;',
  29627. '']),
  29628. LinesToStr([ // $mod.$main
  29629. '$mod.t = $mod.TObject.ClassType().$rtti;',
  29630. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  29631. '$mod.t = $mod.Obj.MyClass().$rtti;',
  29632. '']));
  29633. end;
  29634. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  29635. begin
  29636. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29637. AddModuleWithIntfImplSrc('typinfo.pas',
  29638. LinesToStr([
  29639. '{$modeswitch externalclass}',
  29640. 'type',
  29641. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  29642. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  29643. '']),
  29644. '');
  29645. AddModuleWithIntfImplSrc('unit2.pas',
  29646. LinesToStr([
  29647. 'uses typinfo;',
  29648. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  29649. 'procedure DoPtr(p: PTypeInfo);',
  29650. 'procedure DoInfo(t: TTypeInfo);',
  29651. 'procedure DoInt(t: TTypeInfoInteger);',
  29652. '']),
  29653. LinesToStr([
  29654. 'procedure DoPtr(p: PTypeInfo);',
  29655. 'begin end;',
  29656. 'procedure DoInfo(t: TTypeInfo);',
  29657. 'begin end;',
  29658. 'procedure DoInt(t: TTypeInfoInteger);',
  29659. 'begin end;',
  29660. '']));
  29661. StartUnit(true);
  29662. Add([
  29663. 'interface',
  29664. 'uses unit2;', // does not use unit typinfo
  29665. 'implementation',
  29666. 'var',
  29667. ' i: byte;',
  29668. ' p: pointer;',
  29669. ' t: PTypeInfo;',
  29670. 'initialization',
  29671. ' p:=typeinfo(i);',
  29672. ' t:=typeinfo(i);',
  29673. ' if p=t then ;',
  29674. ' if p=typeinfo(i) then ;',
  29675. ' if typeinfo(i)=p then ;',
  29676. ' if t=typeinfo(i) then ;',
  29677. ' if typeinfo(i)=t then ;',
  29678. ' DoPtr(p);',
  29679. ' DoPtr(t);',
  29680. ' DoPtr(typeinfo(i));',
  29681. ' DoInfo(p);',
  29682. ' DoInfo(t);',
  29683. ' DoInfo(typeinfo(i));',
  29684. ' DoInt(typeinfo(i));',
  29685. '']);
  29686. ConvertUnit;
  29687. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  29688. LinesToStr([ // statements
  29689. 'var $impl = $mod.$impl;',
  29690. '']),
  29691. LinesToStr([ // this.$init
  29692. '$impl.p = rtl.byte;',
  29693. '$impl.t = rtl.byte;',
  29694. 'if ($impl.p === $impl.t) ;',
  29695. 'if ($impl.p === rtl.byte) ;',
  29696. 'if (rtl.byte === $impl.p) ;',
  29697. 'if ($impl.t === rtl.byte) ;',
  29698. 'if (rtl.byte === $impl.t) ;',
  29699. 'pas.unit2.DoPtr($impl.p);',
  29700. 'pas.unit2.DoPtr($impl.t);',
  29701. 'pas.unit2.DoPtr(rtl.byte);',
  29702. 'pas.unit2.DoInfo($impl.p);',
  29703. 'pas.unit2.DoInfo($impl.t);',
  29704. 'pas.unit2.DoInfo(rtl.byte);',
  29705. 'pas.unit2.DoInt(rtl.byte);',
  29706. '']),
  29707. LinesToStr([ // implementation
  29708. '$impl.i = 0;',
  29709. '$impl.p = null;',
  29710. '$impl.t = null;',
  29711. '']) );
  29712. end;
  29713. procedure TTestModule.TestRTTI_Interface_Corba;
  29714. begin
  29715. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29716. StartProgram(true,[supTypeInfo]);
  29717. Add([
  29718. '{$interfaces corba}',
  29719. '{$modeswitch externalclass}',
  29720. 'type',
  29721. ' IUnknown = interface',
  29722. ' end;',
  29723. ' IBird = interface',
  29724. ' function GetItem: longint;',
  29725. ' procedure SetItem(Value: longint);',
  29726. ' property Item: longint read GetItem write SetItem;',
  29727. ' end;',
  29728. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  29729. 'var',
  29730. ' i: IBird;',
  29731. ' t: TTypeInfoInterface;',
  29732. 'begin',
  29733. ' t:=TypeInfo(IBird);',
  29734. ' t:=TypeInfo(i);',
  29735. ' DoIt(t);',
  29736. ' DoIt(TypeInfo(IBird));',
  29737. '']);
  29738. ConvertProgram;
  29739. CheckSource('TestRTTI_Interface_Corba',
  29740. LinesToStr([ // statements
  29741. 'rtl.createInterface(',
  29742. ' $mod,',
  29743. ' "IUnknown",',
  29744. ' "{B92D5841-758A-322B-B800-000000000000}",',
  29745. ' [],',
  29746. ' null,',
  29747. ' function () {',
  29748. ' }',
  29749. ');',
  29750. 'rtl.createInterface(',
  29751. ' $mod,',
  29752. ' "IBird",',
  29753. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  29754. ' ["GetItem", "SetItem"],',
  29755. ' null,',
  29756. ' function () {',
  29757. ' var $r = this.$rtti;',
  29758. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29759. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29760. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29761. ' }',
  29762. ');',
  29763. 'this.DoIt = function (t) {',
  29764. '}; ',
  29765. 'this.i = null;',
  29766. 'this.t = null;',
  29767. '']),
  29768. LinesToStr([ // $mod.$main
  29769. '$mod.t = $mod.$rtti["IBird"];',
  29770. '$mod.t = $mod.i.$rtti;',
  29771. '$mod.DoIt($mod.t);',
  29772. '$mod.DoIt($mod.$rtti["IBird"]);',
  29773. '']));
  29774. end;
  29775. procedure TTestModule.TestRTTI_Interface_COM;
  29776. begin
  29777. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29778. StartProgram(true,[supTypeInfo]);
  29779. Add([
  29780. '{$interfaces com}',
  29781. '{$modeswitch externalclass}',
  29782. 'type',
  29783. ' TGuid = record end;',
  29784. ' integer = longint;',
  29785. ' IUnknown = interface',
  29786. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  29787. ' function _AddRef: Integer;',
  29788. ' function _Release: Integer;',
  29789. ' end;',
  29790. ' IBird = interface',
  29791. ' function GetItem: longint;',
  29792. ' procedure SetItem(Value: longint);',
  29793. ' property Item: longint read GetItem write SetItem;',
  29794. ' end;',
  29795. 'var',
  29796. ' i: IBird;',
  29797. ' t: TTypeInfoInterface;',
  29798. 'begin',
  29799. ' t:=TypeInfo(IBird);',
  29800. ' t:=TypeInfo(i);',
  29801. '']);
  29802. ConvertProgram;
  29803. CheckSource('TestRTTI_Interface_COM',
  29804. LinesToStr([ // statements
  29805. 'rtl.recNewT($mod, "TGuid", function () {',
  29806. ' this.$eq = function (b) {',
  29807. ' return true;',
  29808. ' };',
  29809. ' this.$assign = function (s) {',
  29810. ' return this;',
  29811. ' };',
  29812. ' $mod.$rtti.$Record("TGuid", {});',
  29813. '});',
  29814. 'rtl.createInterface(',
  29815. ' $mod,',
  29816. ' "IUnknown",',
  29817. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  29818. ' ["QueryInterface", "_AddRef", "_Release"],',
  29819. ' null,',
  29820. ' function () {',
  29821. ' this.$kind = "com";',
  29822. ' var $r = this.$rtti;',
  29823. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  29824. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  29825. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  29826. ' }',
  29827. ');',
  29828. 'rtl.createInterface(',
  29829. ' $mod,',
  29830. ' "IBird",',
  29831. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  29832. ' ["GetItem", "SetItem"],',
  29833. ' $mod.IUnknown,',
  29834. ' function () {',
  29835. ' var $r = this.$rtti;',
  29836. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29837. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29838. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29839. ' }',
  29840. ');',
  29841. 'this.i = null;',
  29842. 'this.t = null;',
  29843. '']),
  29844. LinesToStr([ // $mod.$main
  29845. '$mod.t = $mod.$rtti["IBird"];',
  29846. '$mod.t = $mod.i.$rtti;',
  29847. '']));
  29848. end;
  29849. procedure TTestModule.TestRTTI_ClassHelper;
  29850. begin
  29851. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29852. StartProgram(true,[supTypeInfo]);
  29853. Add([
  29854. '{$interfaces com}',
  29855. '{$modeswitch externalclass}',
  29856. 'type',
  29857. ' TObject = class',
  29858. ' end;',
  29859. ' THelper = class helper for TObject',
  29860. ' published',
  29861. ' function GetItem: longint;',
  29862. ' property Item: longint read GetItem;',
  29863. ' end;',
  29864. 'function THelper.GetItem: longint;',
  29865. 'begin',
  29866. 'end;',
  29867. 'var',
  29868. ' t: TTypeInfoHelper;',
  29869. 'begin',
  29870. ' t:=TypeInfo(THelper);',
  29871. '']);
  29872. ConvertProgram;
  29873. CheckSource('TestRTTI_ClassHelper',
  29874. LinesToStr([ // statements
  29875. 'rtl.createClass($mod, "TObject", null, function () {',
  29876. ' this.$init = function () {',
  29877. ' };',
  29878. ' this.$final = function () {',
  29879. ' };',
  29880. '});',
  29881. 'rtl.createHelper($mod, "THelper", null, function () {',
  29882. ' this.GetItem = function () {',
  29883. ' var Result = 0;',
  29884. ' return Result;',
  29885. ' };',
  29886. ' var $r = this.$rtti;',
  29887. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29888. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  29889. '});',
  29890. 'this.t = null;',
  29891. '']),
  29892. LinesToStr([ // $mod.$main
  29893. '$mod.t = $mod.$rtti["THelper"];',
  29894. '']));
  29895. end;
  29896. procedure TTestModule.TestRTTI_ExternalClass;
  29897. begin
  29898. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29899. StartProgram(true,[supTypeInfo]);
  29900. Add([
  29901. '{$modeswitch externalclass}',
  29902. 'type',
  29903. ' TJSObject = class external name ''Object''',
  29904. ' end;',
  29905. ' TJSArray = class external name ''Array'' (TJSObject)',
  29906. ' end;',
  29907. 'var',
  29908. ' p: Pointer;',
  29909. ' tc: TTypeInfoExtClass;',
  29910. 'begin',
  29911. ' p:=typeinfo(TJSArray);']);
  29912. ConvertProgram;
  29913. CheckSource('TestRTTI_ExternalClass',
  29914. LinesToStr([ // statements
  29915. '$mod.$rtti.$ExtClass("TJSObject", {',
  29916. ' jsclass: "Object"',
  29917. '});',
  29918. '$mod.$rtti.$ExtClass("TJSArray", {',
  29919. ' ancestor: $mod.$rtti["TJSObject"],',
  29920. ' jsclass: "Array"',
  29921. '});',
  29922. 'this.p = null;',
  29923. 'this.tc = null;',
  29924. '']),
  29925. LinesToStr([ // $mod.$main
  29926. '$mod.p = $mod.$rtti["TJSArray"];',
  29927. '']));
  29928. end;
  29929. procedure TTestModule.TestResourcestringProgram;
  29930. begin
  29931. StartProgram(false);
  29932. Add([
  29933. 'const Bar = ''bar'';',
  29934. 'resourcestring',
  29935. ' Red = ''red'';',
  29936. ' Foobar = ''fOo''+bar;',
  29937. 'var s: string;',
  29938. ' c: char;',
  29939. 'begin',
  29940. ' s:=red;',
  29941. ' s:=test1.red;',
  29942. ' c:=red[1];',
  29943. ' c:=test1.red[2];',
  29944. ' if red=foobar then ;',
  29945. ' if red[3]=red[4] then ;']);
  29946. ConvertProgram;
  29947. CheckSource('TestResourcestringProgram',
  29948. LinesToStr([ // statements
  29949. 'this.Bar = "bar";',
  29950. 'this.s = "";',
  29951. 'this.c = "";',
  29952. '$mod.$resourcestrings = {',
  29953. ' Red: {',
  29954. ' org: "red"',
  29955. ' },',
  29956. ' Foobar: {',
  29957. ' org: "fOobar"',
  29958. ' }',
  29959. '};',
  29960. '']),
  29961. LinesToStr([ // $mod.$main
  29962. '$mod.s = rtl.getResStr(pas.program, "Red");',
  29963. '$mod.s = rtl.getResStr(pas.program, "Red");',
  29964. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  29965. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  29966. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  29967. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  29968. '']));
  29969. end;
  29970. procedure TTestModule.TestResourcestringUnit;
  29971. begin
  29972. StartUnit(false);
  29973. Add([
  29974. 'interface',
  29975. 'const Red = ''rEd'';',
  29976. 'resourcestring',
  29977. ' Blue = ''blue'';',
  29978. ' NotRed = ''not''+Red;',
  29979. 'var s: string;',
  29980. 'implementation',
  29981. 'resourcestring',
  29982. ' ImplGreen = ''green'';',
  29983. 'initialization',
  29984. ' s:=blue+ImplGreen;',
  29985. ' s:=test1.blue+test1.implgreen;',
  29986. ' s:=blue[1]+implgreen[2];']);
  29987. ConvertUnit;
  29988. CheckSource('TestResourcestringUnit',
  29989. LinesToStr([ // statements
  29990. 'this.Red = "rEd";',
  29991. 'this.s = "";',
  29992. '$mod.$resourcestrings = {',
  29993. ' Blue: {',
  29994. ' org: "blue"',
  29995. ' },',
  29996. ' NotRed: {',
  29997. ' org: "notrEd"',
  29998. ' },',
  29999. ' ImplGreen: {',
  30000. ' org: "green"',
  30001. ' }',
  30002. '};',
  30003. '']),
  30004. LinesToStr([ // $mod.$main
  30005. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  30006. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  30007. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  30008. '']));
  30009. end;
  30010. procedure TTestModule.TestResourcestringImplementation;
  30011. begin
  30012. StartUnit(false);
  30013. Add([
  30014. 'interface',
  30015. 'implementation',
  30016. 'resourcestring',
  30017. ' ImplRed = ''red'';']);
  30018. ConvertUnit;
  30019. CheckSource('TestResourcestringImplementation',
  30020. LinesToStr([ // intf statements
  30021. 'var $impl = $mod.$impl;']),
  30022. LinesToStr([ // $mod.$init
  30023. '']),
  30024. LinesToStr([ // impl statements
  30025. '$mod.$resourcestrings = {',
  30026. ' ImplRed: {',
  30027. ' org: "red"',
  30028. ' }',
  30029. '};',
  30030. '']));
  30031. end;
  30032. procedure TTestModule.TestAttributes_Members;
  30033. begin
  30034. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30035. StartProgram(false);
  30036. Add([
  30037. '{$modeswitch PrefixedAttributes}',
  30038. 'type',
  30039. ' TObject = class',
  30040. ' constructor Create;',
  30041. ' end;',
  30042. ' TCustomAttribute = class',
  30043. ' constructor Create(Id: word);',
  30044. ' end;',
  30045. ' [Missing]',
  30046. ' TBird = class',
  30047. ' published',
  30048. ' [Tcustom]',
  30049. ' FField: word;',
  30050. ' [tcustom(14)]',
  30051. ' property Size: word read FField;',
  30052. ' [Tcustom(15)]',
  30053. ' procedure Fly; virtual; abstract;',
  30054. ' end;',
  30055. ' TRec = record',
  30056. ' [Tcustom,tcustom(14)]',
  30057. ' Size: word;',
  30058. ' end;',
  30059. 'constructor TObject.Create; begin end;',
  30060. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30061. 'begin',
  30062. '']);
  30063. ConvertProgram;
  30064. CheckSource('TestAttributes_Members',
  30065. LinesToStr([ // statements
  30066. 'rtl.createClass($mod, "TObject", null, function () {',
  30067. ' this.$init = function () {',
  30068. ' };',
  30069. ' this.$final = function () {',
  30070. ' };',
  30071. ' this.Create = function () {',
  30072. ' return this;',
  30073. ' };',
  30074. '});',
  30075. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30076. ' this.Create$1 = function (Id) {',
  30077. ' return this;',
  30078. ' };',
  30079. '});',
  30080. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30081. ' this.$init = function () {',
  30082. ' $mod.TObject.$init.call(this);',
  30083. ' this.FField = 0;',
  30084. ' };',
  30085. ' var $r = this.$rtti;',
  30086. ' $r.addField("FField", rtl.word, {',
  30087. ' attr: [$mod.TCustomAttribute, "Create"]',
  30088. ' });',
  30089. ' $r.addProperty(',
  30090. ' "Size",',
  30091. ' 0,',
  30092. ' rtl.word,',
  30093. ' "FField",',
  30094. ' "",',
  30095. ' {',
  30096. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30097. ' }',
  30098. ' );',
  30099. ' $r.addMethod("Fly", 0, null, null, {',
  30100. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30101. ' });',
  30102. '});',
  30103. 'rtl.recNewT($mod, "TRec", function () {',
  30104. ' this.Size = 0;',
  30105. ' this.$eq = function (b) {',
  30106. ' return this.Size === b.Size;',
  30107. ' };',
  30108. ' this.$assign = function (s) {',
  30109. ' this.Size = s.Size;',
  30110. ' return this;',
  30111. ' };',
  30112. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30113. ' $r.addField("Size", rtl.word, {',
  30114. ' attr: [',
  30115. ' $mod.TCustomAttribute,',
  30116. ' "Create",',
  30117. ' $mod.TCustomAttribute,',
  30118. ' "Create$1",',
  30119. ' [14]',
  30120. ' ]',
  30121. ' });',
  30122. '});',
  30123. '']),
  30124. LinesToStr([ // $mod.$main
  30125. '']));
  30126. end;
  30127. procedure TTestModule.TestAttributes_Types;
  30128. begin
  30129. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30130. StartProgram(false);
  30131. Add([
  30132. '{$modeswitch PrefixedAttributes}',
  30133. 'type',
  30134. ' TObject = class',
  30135. ' constructor Create(Id: word);',
  30136. ' end;',
  30137. ' TCustomAttribute = class',
  30138. ' end;',
  30139. ' [TCustom(1)]',
  30140. ' TMyClass = class',
  30141. ' end;',
  30142. ' [TCustom(2)]',
  30143. ' TRec = record',
  30144. ' end;',
  30145. ' [TCustom(3)]',
  30146. ' TInt = type word;',
  30147. 'constructor TObject.Create(Id: word);',
  30148. 'begin',
  30149. 'end;',
  30150. 'var p: pointer;',
  30151. 'begin',
  30152. ' p:=typeinfo(TMyClass);',
  30153. ' p:=typeinfo(TRec);',
  30154. ' p:=typeinfo(TInt);',
  30155. '']);
  30156. ConvertProgram;
  30157. CheckSource('TestAttributes_Types',
  30158. LinesToStr([ // statements
  30159. 'rtl.createClass($mod, "TObject", null, function () {',
  30160. ' this.$init = function () {',
  30161. ' };',
  30162. ' this.$final = function () {',
  30163. ' };',
  30164. ' this.Create = function (Id) {',
  30165. ' return this;',
  30166. ' };',
  30167. '});',
  30168. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30169. '});',
  30170. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  30171. ' var $r = this.$rtti;',
  30172. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30173. '});',
  30174. 'rtl.recNewT($mod, "TRec", function () {',
  30175. ' this.$eq = function (b) {',
  30176. ' return true;',
  30177. ' };',
  30178. ' this.$assign = function (s) {',
  30179. ' return this;',
  30180. ' };',
  30181. ' $mod.$rtti.$Record("TRec", {',
  30182. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30183. ' });',
  30184. '});',
  30185. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  30186. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  30187. '});',
  30188. 'this.p = null;',
  30189. '']),
  30190. LinesToStr([ // $mod.$main
  30191. '$mod.p = $mod.$rtti["TMyClass"];',
  30192. '$mod.p = $mod.$rtti["TRec"];',
  30193. '$mod.p = $mod.$rtti["TInt"];',
  30194. '']));
  30195. end;
  30196. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30197. begin
  30198. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30199. StartProgram(false);
  30200. Add([
  30201. '{$modeswitch PrefixedAttributes}',
  30202. 'type',
  30203. ' TObject = class',
  30204. ' constructor Create;',
  30205. ' end;',
  30206. ' TCustomAttribute = class',
  30207. ' end;',
  30208. ' THelper = class helper for TCustomAttribute',
  30209. ' constructor Create(Id: word);',
  30210. ' end;',
  30211. ' [TCustom(3)]',
  30212. ' TMyInt = word;',
  30213. 'constructor TObject.Create; begin end;',
  30214. 'constructor THelper.Create(Id: word); begin end;',
  30215. 'begin',
  30216. ' if typeinfo(TMyInt)=nil then ;']);
  30217. ConvertProgram;
  30218. end;
  30219. procedure TTestModule.TestAssert;
  30220. begin
  30221. StartProgram(false);
  30222. Add([
  30223. 'procedure DoIt;',
  30224. 'var',
  30225. ' b: boolean;',
  30226. ' s: string;',
  30227. 'begin',
  30228. ' {$Assertions on}',
  30229. ' Assert(b);',
  30230. 'end;',
  30231. 'begin',
  30232. ' DoIt;',
  30233. '']);
  30234. ConvertProgram;
  30235. CheckSource('TestAssert',
  30236. LinesToStr([ // statements
  30237. 'this.DoIt = function () {',
  30238. ' var b = false;',
  30239. ' var s = "";',
  30240. ' if (!b) throw "assert failed";',
  30241. '};',
  30242. '']),
  30243. LinesToStr([ // $mod.$main
  30244. '$mod.DoIt();',
  30245. '']));
  30246. end;
  30247. procedure TTestModule.TestAssert_SysUtils;
  30248. begin
  30249. AddModuleWithIntfImplSrc('SysUtils.pas',
  30250. LinesToStr([
  30251. 'type',
  30252. ' TObject = class',
  30253. ' constructor Create;',
  30254. ' end;',
  30255. ' EAssertionFailed = class',
  30256. ' constructor Create(s: string);',
  30257. ' end;',
  30258. '']),
  30259. LinesToStr([
  30260. 'constructor TObject.Create;',
  30261. 'begin end;',
  30262. 'constructor EAssertionFailed.Create(s: string);',
  30263. 'begin end;',
  30264. '']) );
  30265. StartProgram(true);
  30266. Add([
  30267. 'uses sysutils;',
  30268. 'procedure DoIt;',
  30269. 'var',
  30270. ' b: boolean;',
  30271. ' s: string;',
  30272. 'begin',
  30273. ' {$Assertions on}',
  30274. ' Assert(b);',
  30275. ' Assert(b,''msg'');',
  30276. 'end;',
  30277. 'begin',
  30278. ' DoIt;',
  30279. '']);
  30280. ConvertProgram;
  30281. CheckSource('TestAssert_SysUtils',
  30282. LinesToStr([ // statements
  30283. 'this.DoIt = function () {',
  30284. ' var b = false;',
  30285. ' var s = "";',
  30286. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30287. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30288. '};',
  30289. '']),
  30290. LinesToStr([ // $mod.$main
  30291. '$mod.DoIt();',
  30292. '']));
  30293. end;
  30294. procedure TTestModule.TestObjectChecks;
  30295. begin
  30296. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30297. StartProgram(false);
  30298. Add([
  30299. 'type',
  30300. ' TObject = class',
  30301. ' procedure DoIt;',
  30302. ' end;',
  30303. ' TClass = class of tobject;',
  30304. ' TBird = class',
  30305. ' end;',
  30306. ' TBirdClass = class of TBird;',
  30307. 'var',
  30308. ' o : TObject;',
  30309. ' c: TClass;',
  30310. ' b: TBird;',
  30311. ' bc: TBirdClass;',
  30312. 'procedure TObject.DoIt;',
  30313. 'begin',
  30314. ' b:=TBird(o);',
  30315. 'end;',
  30316. 'begin',
  30317. ' o.DoIt;',
  30318. ' b:=TBird(o);',
  30319. ' bc:=TBirdClass(c);',
  30320. '']);
  30321. ConvertProgram;
  30322. CheckSource('TestCheckMethodCall',
  30323. LinesToStr([ // statements
  30324. 'rtl.createClass($mod, "TObject", null, function () {',
  30325. ' this.$init = function () {',
  30326. ' };',
  30327. ' this.$final = function () {',
  30328. ' };',
  30329. ' this.DoIt = function () {',
  30330. ' rtl.checkMethodCall(this,$mod.TObject);',
  30331. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30332. ' };',
  30333. '});',
  30334. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30335. '});',
  30336. 'this.o = null;',
  30337. 'this.c = null;',
  30338. 'this.b = null;',
  30339. 'this.bc = null;',
  30340. '']),
  30341. LinesToStr([ // $mod.$main
  30342. '$mod.o.DoIt();',
  30343. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30344. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30345. '']));
  30346. end;
  30347. procedure TTestModule.TestOverflowChecks_Int;
  30348. begin
  30349. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30350. StartProgram(false);
  30351. Add([
  30352. 'procedure DoIt;',
  30353. 'var',
  30354. ' b: byte;',
  30355. ' n: nativeint;',
  30356. ' u: nativeuint;',
  30357. ' c: currency;',
  30358. 'begin',
  30359. ' n:=n+n;',
  30360. ' n:=n-n;',
  30361. ' n:=n+b;',
  30362. ' n:=b-n;',
  30363. ' n:=n*n;',
  30364. ' n:=n*u;',
  30365. ' c:=c+b;',
  30366. ' c:=b+c;',
  30367. ' c:=c*b;',
  30368. ' c:=b*c;',
  30369. 'end;',
  30370. 'begin',
  30371. '']);
  30372. ConvertProgram;
  30373. CheckSource('TestOverflowChecks_Int',
  30374. LinesToStr([ // statements
  30375. 'this.DoIt = function () {',
  30376. ' var b = 0;',
  30377. ' var n = 0;',
  30378. ' var u = 0;',
  30379. ' var c = 0;',
  30380. ' n = rtl.oc(n + n);',
  30381. ' n = rtl.oc(n - n);',
  30382. ' n = rtl.oc(n + b);',
  30383. ' n = rtl.oc(b - n);',
  30384. ' n = rtl.oc(n * n);',
  30385. ' n = rtl.oc(n * u);',
  30386. ' c = rtl.oc(c + (b * 10000));',
  30387. ' c = rtl.oc((b * 10000) + c);',
  30388. ' c = rtl.oc(c * b);',
  30389. ' c = rtl.oc(b * c);',
  30390. '};',
  30391. '']),
  30392. LinesToStr([ // $mod.$main
  30393. '']));
  30394. end;
  30395. procedure TTestModule.TestRangeChecks_AssignInt;
  30396. begin
  30397. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30398. StartProgram(false);
  30399. Add([
  30400. '{$R+}',
  30401. 'var',
  30402. ' b: byte = 2;',
  30403. ' w: word = 3;',
  30404. 'procedure DoIt(p: byte);',
  30405. 'begin',
  30406. ' b:=w;',
  30407. ' b+=w;',
  30408. ' b:=1;',
  30409. 'end;',
  30410. '{$R-}',
  30411. 'procedure DoSome;',
  30412. 'begin',
  30413. ' DoIt(w);',
  30414. ' b:=w;',
  30415. ' b:=2;',
  30416. 'end;',
  30417. 'begin',
  30418. '{$R+}',
  30419. '']);
  30420. ConvertProgram;
  30421. CheckSource('TestRangeChecks_AssignInt',
  30422. LinesToStr([ // statements
  30423. 'this.b = 2;',
  30424. 'this.w = 3;',
  30425. 'this.DoIt = function (p) {',
  30426. ' rtl.rc(p, 0, 255);',
  30427. ' $mod.b = rtl.rc($mod.w,0,255);',
  30428. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  30429. ' $mod.b = 1;',
  30430. '};',
  30431. 'this.DoSome = function () {',
  30432. ' $mod.DoIt($mod.w);',
  30433. ' $mod.b = $mod.w;',
  30434. ' $mod.b = 2;',
  30435. '};',
  30436. '']),
  30437. LinesToStr([ // $mod.$main
  30438. '']));
  30439. end;
  30440. procedure TTestModule.TestRangeChecks_AssignIntRange;
  30441. begin
  30442. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30443. StartProgram(false);
  30444. Add([
  30445. '{$R+}',
  30446. 'type Ten = 1..10;',
  30447. 'var',
  30448. ' b: Ten = 2;',
  30449. ' w: Ten = 3;',
  30450. 'procedure DoIt(p: Ten);',
  30451. 'begin',
  30452. ' b:=w;',
  30453. ' b+=w;',
  30454. ' b:=1;',
  30455. 'end;',
  30456. '{$R-}',
  30457. 'procedure DoSome;',
  30458. 'begin',
  30459. ' DoIt(w);',
  30460. ' b:=w;',
  30461. ' b:=2;',
  30462. 'end;',
  30463. 'begin',
  30464. '{$R+}',
  30465. '']);
  30466. ConvertProgram;
  30467. CheckSource('TestRangeChecks_AssignIntRange',
  30468. LinesToStr([ // statements
  30469. 'this.b = 2;',
  30470. 'this.w = 3;',
  30471. 'this.DoIt = function (p) {',
  30472. ' rtl.rc(p, 1, 10);',
  30473. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  30474. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  30475. ' $mod.b = 1;',
  30476. '};',
  30477. 'this.DoSome = function () {',
  30478. ' $mod.DoIt($mod.w);',
  30479. ' $mod.b = $mod.w;',
  30480. ' $mod.b = 2;',
  30481. '};',
  30482. '']),
  30483. LinesToStr([ // $mod.$main
  30484. '']));
  30485. end;
  30486. procedure TTestModule.TestRangeChecks_AssignEnum;
  30487. begin
  30488. StartProgram(false);
  30489. Add([
  30490. '{$R+}',
  30491. 'type TEnum = (red,green);',
  30492. 'var',
  30493. ' e: TEnum = red;',
  30494. 'procedure DoIt(p: TEnum);',
  30495. 'begin',
  30496. ' e:=p;',
  30497. ' p:=TEnum(0);',
  30498. ' p:=succ(e);',
  30499. 'end;',
  30500. '{$R-}',
  30501. 'procedure DoSome;',
  30502. 'begin',
  30503. ' DoIt(e);',
  30504. ' e:=TEnum(1);',
  30505. ' e:=pred(e);',
  30506. 'end;',
  30507. 'begin',
  30508. '{$R+}',
  30509. '']);
  30510. ConvertProgram;
  30511. CheckSource('TestRangeChecks_AssignEnum',
  30512. LinesToStr([ // statements
  30513. 'this.TEnum = {',
  30514. ' "0": "red",',
  30515. ' red: 0,',
  30516. ' "1": "green",',
  30517. ' green: 1',
  30518. '};',
  30519. 'this.e = $mod.TEnum.red;',
  30520. 'this.DoIt = function (p) {',
  30521. ' rtl.rc(p, 0, 1);',
  30522. ' $mod.e = rtl.rc(p, 0, 1);',
  30523. ' p = 0;',
  30524. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30525. '};',
  30526. 'this.DoSome = function () {',
  30527. ' $mod.DoIt($mod.e);',
  30528. ' $mod.e = 1;',
  30529. ' $mod.e = $mod.e - 1;',
  30530. '};',
  30531. '']),
  30532. LinesToStr([ // $mod.$main
  30533. '']));
  30534. end;
  30535. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  30536. begin
  30537. StartProgram(false);
  30538. Add([
  30539. '{$R+}',
  30540. 'type',
  30541. ' TEnum = (red,green);',
  30542. ' TEnumRg = red..green;',
  30543. 'var',
  30544. ' e: TEnumRg = red;',
  30545. 'procedure DoIt(p: TEnumRg);',
  30546. 'begin',
  30547. ' e:=p;',
  30548. ' p:=TEnumRg(0);',
  30549. ' p:=succ(e);',
  30550. 'end;',
  30551. '{$R-}',
  30552. 'procedure DoSome;',
  30553. 'begin',
  30554. ' DoIt(e);',
  30555. ' e:=TEnum(1);',
  30556. ' e:=pred(e);',
  30557. 'end;',
  30558. 'begin',
  30559. '{$R+}',
  30560. '']);
  30561. ConvertProgram;
  30562. CheckSource('TestRangeChecks_AssignEnumRange',
  30563. LinesToStr([ // statements
  30564. 'this.TEnum = {',
  30565. ' "0": "red",',
  30566. ' red: 0,',
  30567. ' "1": "green",',
  30568. ' green: 1',
  30569. '};',
  30570. 'this.e = $mod.TEnum.red;',
  30571. 'this.DoIt = function (p) {',
  30572. ' rtl.rc(p, 0, 1);',
  30573. ' $mod.e = rtl.rc(p, 0, 1);',
  30574. ' p = 0;',
  30575. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30576. '};',
  30577. 'this.DoSome = function () {',
  30578. ' $mod.DoIt($mod.e);',
  30579. ' $mod.e = 1;',
  30580. ' $mod.e = $mod.e - 1;',
  30581. '};',
  30582. '']),
  30583. LinesToStr([ // $mod.$main
  30584. '']));
  30585. end;
  30586. procedure TTestModule.TestRangeChecks_AssignChar;
  30587. begin
  30588. StartProgram(false);
  30589. Add([
  30590. '{$R+}',
  30591. 'type',
  30592. ' TLetter = char;',
  30593. 'var',
  30594. ' b: TLetter = ''2'';',
  30595. ' w: TLetter = ''3'';',
  30596. 'procedure DoIt(p: TLetter);',
  30597. 'begin',
  30598. ' b:=w;',
  30599. ' b:=''1'';',
  30600. 'end;',
  30601. '{$R-}',
  30602. 'procedure DoSome;',
  30603. 'begin',
  30604. ' DoIt(w);',
  30605. ' b:=w;',
  30606. ' b:=''2'';',
  30607. 'end;',
  30608. 'begin',
  30609. '{$R+}',
  30610. '']);
  30611. ConvertProgram;
  30612. CheckSource('TestRangeChecks_AssignChar',
  30613. LinesToStr([ // statements
  30614. 'this.b = "2";',
  30615. 'this.w = "3";',
  30616. 'this.DoIt = function (p) {',
  30617. ' rtl.rcc(p, 0, 65535);',
  30618. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  30619. ' $mod.b = "1";',
  30620. '};',
  30621. 'this.DoSome = function () {',
  30622. ' $mod.DoIt($mod.w);',
  30623. ' $mod.b = $mod.w;',
  30624. ' $mod.b = "2";',
  30625. '};',
  30626. '']),
  30627. LinesToStr([ // $mod.$main
  30628. '']));
  30629. end;
  30630. procedure TTestModule.TestRangeChecks_AssignCharRange;
  30631. begin
  30632. StartProgram(false);
  30633. Add([
  30634. '{$R+}',
  30635. 'type TDigit = ''0''..''9'';',
  30636. 'var',
  30637. ' b: TDigit = ''2'';',
  30638. ' w: TDigit = ''3'';',
  30639. 'procedure DoIt(p: TDigit);',
  30640. 'begin',
  30641. ' b:=w;',
  30642. ' b:=''1'';',
  30643. 'end;',
  30644. '{$R-}',
  30645. 'procedure DoSome;',
  30646. 'begin',
  30647. ' DoIt(w);',
  30648. ' b:=w;',
  30649. ' b:=''2'';',
  30650. 'end;',
  30651. 'begin',
  30652. '{$R+}',
  30653. '']);
  30654. ConvertProgram;
  30655. CheckSource('TestRangeChecks_AssignCharRange',
  30656. LinesToStr([ // statements
  30657. 'this.b = "2";',
  30658. 'this.w = "3";',
  30659. 'this.DoIt = function (p) {',
  30660. ' rtl.rcc(p, 48, 57);',
  30661. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  30662. ' $mod.b = "1";',
  30663. '};',
  30664. 'this.DoSome = function () {',
  30665. ' $mod.DoIt($mod.w);',
  30666. ' $mod.b = $mod.w;',
  30667. ' $mod.b = "2";',
  30668. '};',
  30669. '']),
  30670. LinesToStr([ // $mod.$main
  30671. '']));
  30672. end;
  30673. procedure TTestModule.TestRangeChecks_ArrayIndex;
  30674. begin
  30675. StartProgram(false);
  30676. Add([
  30677. '{$R+}',
  30678. 'type',
  30679. ' Ten = 1..10;',
  30680. ' TArr = array of Ten;',
  30681. ' TArrArr = array of TArr;',
  30682. ' TArrByte = array[byte] of Ten;',
  30683. ' TArrChar = array[''0''..''9''] of Ten;',
  30684. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  30685. ' TObject = class',
  30686. ' A: TArr;',
  30687. ' end;',
  30688. 'procedure DoIt;',
  30689. 'var',
  30690. ' Arr: TArr;',
  30691. ' ArrArr: TArrArr;',
  30692. ' ArrByte: TArrByte;',
  30693. ' ArrChar: TArrChar;',
  30694. ' ArrByteChar: TArrByteChar;',
  30695. ' i: Ten;',
  30696. ' c: char;',
  30697. ' o: tobject;',
  30698. 'begin',
  30699. ' i:=Arr[1];',
  30700. ' i:=ArrByteChar[1,''2''];',
  30701. ' Arr[1]:=Arr[1];',
  30702. ' Arr[i]:=Arr[i];',
  30703. ' ArrByte[3]:=ArrByte[3];',
  30704. ' ArrByte[i]:=ArrByte[i];',
  30705. ' ArrChar[''5'']:=ArrChar[''5''];',
  30706. ' ArrChar[c]:=ArrChar[c];',
  30707. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  30708. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  30709. ' o.a[i]:=o.a[i];',
  30710. 'end;',
  30711. 'begin',
  30712. '']);
  30713. ConvertProgram;
  30714. CheckSource('TestRangeChecks_ArrayIndex',
  30715. LinesToStr([ // statements
  30716. 'rtl.createClass($mod, "TObject", null, function () {',
  30717. ' this.$init = function () {',
  30718. ' this.A = [];',
  30719. ' };',
  30720. ' this.$final = function () {',
  30721. ' this.A = undefined;',
  30722. ' };',
  30723. '});',
  30724. 'this.DoIt = function () {',
  30725. ' var Arr = [];',
  30726. ' var ArrArr = [];',
  30727. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  30728. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  30729. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  30730. ' var i = 0;',
  30731. ' var c = "";',
  30732. ' var o = null;',
  30733. ' i = rtl.rc(Arr[1], 1, 10);',
  30734. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  30735. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  30736. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  30737. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  30738. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  30739. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  30740. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  30741. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  30742. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  30743. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  30744. '};',
  30745. '']),
  30746. LinesToStr([ // $mod.$main
  30747. '']));
  30748. end;
  30749. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  30750. begin
  30751. StartProgram(false);
  30752. Add([
  30753. '{$R+}',
  30754. 'type',
  30755. ' Ten = 1..10;',
  30756. ' TRec = record x: Ten end;',
  30757. ' TArr = array of TRec;',
  30758. ' TArrArr = array of TArr;',
  30759. ' TObject = class',
  30760. ' A: TArr;',
  30761. ' end;',
  30762. 'procedure DoIt;',
  30763. 'var',
  30764. ' Arr: TArr;',
  30765. ' ArrArr: TArrArr;',
  30766. ' i: Ten;',
  30767. ' o: tobject;',
  30768. 'begin',
  30769. ' Arr[1]:=Arr[1];',
  30770. ' Arr[i]:=Arr[i+1];',
  30771. ' o.a[i]:=o.a[i+2];',
  30772. 'end;',
  30773. 'begin',
  30774. '']);
  30775. ConvertProgram;
  30776. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  30777. LinesToStr([ // statements
  30778. 'rtl.recNewT($mod, "TRec", function () {',
  30779. ' this.x = 0;',
  30780. ' this.$eq = function (b) {',
  30781. ' return this.x === b.x;',
  30782. ' };',
  30783. ' this.$assign = function (s) {',
  30784. ' this.x = s.x;',
  30785. ' return this;',
  30786. ' };',
  30787. '});',
  30788. 'rtl.createClass($mod, "TObject", null, function () {',
  30789. ' this.$init = function () {',
  30790. ' this.A = [];',
  30791. ' };',
  30792. ' this.$final = function () {',
  30793. ' this.A = undefined;',
  30794. ' };',
  30795. '});',
  30796. 'this.DoIt = function () {',
  30797. ' var Arr = [];',
  30798. ' var ArrArr = [];',
  30799. ' var i = 0;',
  30800. ' var o = null;',
  30801. ' Arr[1].$assign(Arr[1]);',
  30802. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  30803. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  30804. '};',
  30805. '']),
  30806. LinesToStr([ // $mod.$main
  30807. '']));
  30808. end;
  30809. procedure TTestModule.TestRangeChecks_StringIndex;
  30810. begin
  30811. StartProgram(false);
  30812. Add([
  30813. 'type',
  30814. ' TObject = class',
  30815. ' S: string;',
  30816. ' end;',
  30817. '{$R+}',
  30818. 'procedure DoIt(var h: string);',
  30819. 'var',
  30820. ' s: string;',
  30821. ' i: longint;',
  30822. ' c: char;',
  30823. ' o: tobject;',
  30824. 'begin',
  30825. ' c:=s[1];',
  30826. ' s[i]:=s[i];',
  30827. ' h[i]:=h[i];',
  30828. ' c:=o.s[i];',
  30829. ' o.s[i]:=c;',
  30830. 'end;',
  30831. 'begin',
  30832. '']);
  30833. ConvertProgram;
  30834. CheckSource('TestRangeChecks_StringIndex',
  30835. LinesToStr([ // statements
  30836. 'rtl.createClass($mod, "TObject", null, function () {',
  30837. ' this.$init = function () {',
  30838. ' this.S = "";',
  30839. ' };',
  30840. ' this.$final = function () {',
  30841. ' };',
  30842. '});',
  30843. 'this.DoIt = function (h) {',
  30844. ' var s = "";',
  30845. ' var i = 0;',
  30846. ' var c = "";',
  30847. ' var o = null;',
  30848. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  30849. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  30850. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  30851. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  30852. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  30853. '};',
  30854. '']),
  30855. LinesToStr([ // $mod.$main
  30856. '']));
  30857. end;
  30858. procedure TTestModule.TestRangeChecks_TypecastInt;
  30859. begin
  30860. StartProgram(false);
  30861. Add([
  30862. '{$R+}',
  30863. 'var',
  30864. ' i: nativeint;',
  30865. ' b: byte;',
  30866. ' sh: shortint;',
  30867. ' w: word;',
  30868. ' sm: smallint;',
  30869. ' lw: longword;',
  30870. ' li: longint;',
  30871. 'begin',
  30872. ' b:=12+byte(i);',
  30873. ' sh:=12+shortint(i);',
  30874. ' w:=12+word(i);',
  30875. ' sm:=12+smallint(i);',
  30876. ' lw:=12+longword(i);',
  30877. ' li:=12+longint(i);',
  30878. '']);
  30879. ConvertProgram;
  30880. CheckSource('TestRangeChecks_TypecastInt',
  30881. LinesToStr([
  30882. 'this.i = 0;',
  30883. 'this.b = 0;',
  30884. 'this.sh = 0;',
  30885. 'this.w = 0;',
  30886. 'this.sm = 0;',
  30887. 'this.lw = 0;',
  30888. 'this.li = 0;',
  30889. '']),
  30890. LinesToStr([
  30891. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  30892. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  30893. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  30894. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  30895. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  30896. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  30897. '']));
  30898. end;
  30899. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  30900. begin
  30901. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30902. StartProgram(false);
  30903. Add([
  30904. '{$modeswitch typehelpers}',
  30905. '{$R+}',
  30906. 'type',
  30907. ' TObject = class',
  30908. ' FSize: byte;',
  30909. ' property Size: byte read FSize;',
  30910. ' end;',
  30911. ' THelper = type helper for byte',
  30912. ' procedure SetIt(w: word);',
  30913. ' end;',
  30914. 'procedure THelper.SetIt(w: word);',
  30915. 'begin',
  30916. ' Self:=w;',
  30917. 'end;',
  30918. 'function GetIt: byte;',
  30919. 'begin',
  30920. ' Result.SetIt(2);',
  30921. 'end;',
  30922. 'var',
  30923. ' b: byte = 3;',
  30924. ' o: TObject;',
  30925. 'begin',
  30926. ' b.SetIt(14);',
  30927. ' with b do SetIt(15);',
  30928. ' o.Size.SetIt(16);',
  30929. '']);
  30930. ConvertProgram;
  30931. CheckSource('TestRangeChecks_AssignInt',
  30932. LinesToStr([ // statements
  30933. 'rtl.createClass($mod, "TObject", null, function () {',
  30934. ' this.$init = function () {',
  30935. ' this.FSize = 0;',
  30936. ' };',
  30937. ' this.$final = function () {',
  30938. ' };',
  30939. '});',
  30940. 'rtl.createHelper($mod, "THelper", null, function () {',
  30941. ' this.SetIt = function (w) {',
  30942. ' rtl.rc(w, 0, 65535);',
  30943. ' this.set(w);',
  30944. ' };',
  30945. '});',
  30946. 'this.GetIt = function () {',
  30947. ' var Result = 0;',
  30948. ' $mod.THelper.SetIt.call({',
  30949. ' get: function () {',
  30950. ' return Result;',
  30951. ' },',
  30952. ' set: function (v) {',
  30953. ' rtl.rc(v, 0, 255);',
  30954. ' Result = v;',
  30955. ' }',
  30956. ' }, 2);',
  30957. ' return Result;',
  30958. '};',
  30959. 'this.b = 3;',
  30960. 'this.o = null;',
  30961. '']),
  30962. LinesToStr([ // $mod.$main
  30963. '$mod.THelper.SetIt.call({',
  30964. ' p: $mod,',
  30965. ' get: function () {',
  30966. ' return this.p.b;',
  30967. ' },',
  30968. ' set: function (v) {',
  30969. ' rtl.rc(v, 0, 255);',
  30970. ' this.p.b = v;',
  30971. ' }',
  30972. '}, 14);',
  30973. 'var $with1 = $mod.b;',
  30974. '$mod.THelper.SetIt.call({',
  30975. ' get: function () {',
  30976. ' return $with1;',
  30977. ' },',
  30978. ' set: function (v) {',
  30979. ' rtl.rc(v, 0, 255);',
  30980. ' $with1 = v;',
  30981. ' }',
  30982. '}, 15);',
  30983. '$mod.THelper.SetIt.call({',
  30984. ' p: $mod.o,',
  30985. ' get: function () {',
  30986. ' return this.p.FSize;',
  30987. ' },',
  30988. ' set: function (v) {',
  30989. ' rtl.rc(v, 0, 255);',
  30990. ' this.p.FSize = v;',
  30991. ' }',
  30992. '}, 16);',
  30993. '']));
  30994. end;
  30995. Initialization
  30996. RegisterTests([TTestModule]);
  30997. end.