tcmodules.pas 874 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FHintMsgs: TObjectList; // list of TTestHintMessage
  110. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  111. FJSRegModuleCall: TJSCallExpression;
  112. FScanner: TPas2jsPasScanner;
  113. FSkipTests: boolean;
  114. FSource: TStringList;
  115. FFirstPasStatement: TPasImplBlock;
  116. FWithTypeInfo: boolean;
  117. {$IFDEF EnablePasTreeGlobalRefCount}
  118. FElementRefCountAtSetup: int64;
  119. {$ENDIF}
  120. function GetMsgCount: integer;
  121. function GetMsgs(Index: integer): TTestHintMessage;
  122. function GetResolverCount: integer;
  123. function GetResolvers(Index: integer): TTestEnginePasResolver;
  124. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  125. procedure OnParserLog(Sender: TObject; const Msg: String);
  126. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  127. procedure OnScannerLog(Sender: TObject; const Msg: String);
  128. procedure SetWithTypeInfo(const AValue: boolean);
  129. protected
  130. procedure SetUp; override;
  131. function CreateConverter: TPasToJSConverter; virtual;
  132. function LoadUnit(const aUnitName: String): TPasModule;
  133. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  134. procedure TearDown; override;
  135. Procedure Add(Line: string); virtual;
  136. Procedure Add(const Lines: array of string);
  137. Procedure StartParsing; virtual;
  138. procedure ParseModuleQueue; virtual;
  139. procedure ParseModule; virtual;
  140. procedure ParseProgram; virtual;
  141. procedure ParseUnit; virtual;
  142. protected
  143. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  146. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  147. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  148. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  149. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  151. procedure ConvertModule; virtual;
  152. procedure ConvertProgram; virtual;
  153. procedure ConvertUnit; virtual;
  154. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  155. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  156. function GetDottedIdentifier(El: TJSElement): string;
  157. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  158. ImplStatements: string = ''); virtual;
  159. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  160. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  161. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  162. Msg: string; Marker: PSrcMarker = nil); virtual;
  163. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  164. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  167. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  168. function IsErrorExpected(E: Exception): boolean;
  169. procedure HandleScannerError(E: EScannerError);
  170. procedure HandleParserError(E: EParserError);
  171. procedure HandlePasResolveError(E: EPasResolve);
  172. procedure HandlePas2JSError(E: EPas2JS);
  173. procedure HandleException(E: Exception);
  174. procedure FailException(E: Exception);
  175. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  176. function IndexOfResolver(const Filename: string): integer;
  177. function GetResolver(const Filename: string): TTestEnginePasResolver;
  178. function GetDefaultNamespace: string;
  179. property PasProgram: TPasProgram Read FPasProgram;
  180. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  181. property ResolverCount: integer read GetResolverCount;
  182. property Engine: TTestEnginePasResolver read FEngine;
  183. property Filename: string read FFilename;
  184. Property Module: TPasModule Read FModule;
  185. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  186. property Converter: TPasToJSConverter read FConverter;
  187. property JSSource: TStringList read FJSSource;
  188. property JSModule: TJSSourceElements read FJSModule;
  189. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  190. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  191. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  192. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  193. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  194. property JSInitBody: TJSFunctionBody read FJSInitBody;
  195. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  196. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  197. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  198. property SkipTests: boolean read FSkipTests write FSkipTests;
  199. public
  200. constructor Create; override;
  201. destructor Destroy; override;
  202. property Hub: TPas2JSResolverHub read FHub;
  203. property Source: TStringList read FSource;
  204. property FileResolver: TStreamResolver read FFileResolver;
  205. property Scanner: TPas2jsPasScanner read FScanner;
  206. property Parser: TTestPasParser read FParser;
  207. property MsgCount: integer read GetMsgCount;
  208. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  209. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  210. end;
  211. { TTestModule }
  212. TTestModule = class(TCustomTestModule)
  213. Published
  214. Procedure TestReservedWords;
  215. // program, units, includes
  216. Procedure TestEmptyProgram;
  217. Procedure TestEmptyProgramUseStrict;
  218. Procedure TestEmptyUnit;
  219. Procedure TestEmptyUnitUseStrict;
  220. Procedure TestDottedUnitNames;
  221. Procedure TestDottedUnitNameImpl;
  222. Procedure TestDottedUnitExpr;
  223. Procedure Test_ModeFPCFail;
  224. Procedure Test_ModeSwitchCBlocksFail;
  225. Procedure TestUnit_UseSystem;
  226. Procedure TestUnit_Intf1Impl2Intf1;
  227. Procedure TestIncludeVersion;
  228. // vars/const
  229. Procedure TestVarInt;
  230. Procedure TestVarBaseTypes;
  231. Procedure TestBaseTypeSingleFail;
  232. Procedure TestBaseTypeExtendedFail;
  233. Procedure TestConstBaseTypes;
  234. Procedure TestUnitImplVars;
  235. Procedure TestUnitImplConsts;
  236. Procedure TestUnitImplRecord;
  237. Procedure TestRenameJSNameConflict;
  238. Procedure TestLocalConst;
  239. Procedure TestVarExternal;
  240. Procedure TestVarExternalOtherUnit;
  241. Procedure TestVarAbsoluteFail;
  242. Procedure TestConstExternal;
  243. // numbers
  244. Procedure TestDouble;
  245. Procedure TestInteger;
  246. Procedure TestIntegerRange;
  247. Procedure TestIntegerTypecasts;
  248. Procedure TestInteger_BitwiseShrNativeInt;
  249. Procedure TestInteger_BitwiseShlNativeInt;
  250. Procedure TestInteger_SystemFunc;
  251. Procedure TestCurrency;
  252. Procedure TestForBoolDo;
  253. Procedure TestForIntDo;
  254. Procedure TestForIntInDo;
  255. // strings
  256. Procedure TestCharConst;
  257. Procedure TestChar_Compare;
  258. Procedure TestChar_BuiltInProcs;
  259. Procedure TestStringConst;
  260. Procedure TestStringConst_InvalidUTF16;
  261. Procedure TestStringConstSurrogate;
  262. Procedure TestString_Length;
  263. Procedure TestString_Compare;
  264. Procedure TestString_SetLength;
  265. Procedure TestString_CharAt;
  266. Procedure TestStringHMinusFail;
  267. Procedure TestStr;
  268. Procedure TestBaseType_AnsiStringFail;
  269. Procedure TestBaseType_WideStringFail;
  270. Procedure TestBaseType_ShortStringFail;
  271. Procedure TestBaseType_RawByteStringFail;
  272. Procedure TestTypeShortstring_Fail;
  273. Procedure TestCharSet_Custom;
  274. Procedure TestWideChar;
  275. Procedure TestForCharDo;
  276. Procedure TestForCharInDo;
  277. // alias types
  278. Procedure TestAliasTypeRef;
  279. Procedure TestTypeCast_BaseTypes;
  280. Procedure TestTypeCast_AliasBaseTypes;
  281. // functions
  282. Procedure TestEmptyProc;
  283. Procedure TestProcOneParam;
  284. Procedure TestFunctionWithoutParams;
  285. Procedure TestProcedureWithoutParams;
  286. Procedure TestPrgProcVar;
  287. Procedure TestProcTwoArgs;
  288. Procedure TestProc_DefaultValue;
  289. Procedure TestUnitProcVar;
  290. Procedure TestImplProc;
  291. Procedure TestFunctionResult;
  292. Procedure TestNestedProc;
  293. Procedure TestNestedProc_ResultString;
  294. Procedure TestForwardProc;
  295. Procedure TestNestedForwardProc;
  296. Procedure TestAssignFunctionResult;
  297. Procedure TestFunctionResultInCondition;
  298. Procedure TestFunctionResultInForLoop;
  299. Procedure TestFunctionResultInTypeCast;
  300. Procedure TestExit;
  301. Procedure TestExit_ResultInFinally;
  302. Procedure TestBreak;
  303. Procedure TestBreakAsVar;
  304. Procedure TestContinue;
  305. Procedure TestProc_External;
  306. Procedure TestProc_ExternalOtherUnit;
  307. Procedure TestProc_Asm;
  308. Procedure TestProc_AsmSubBlock;
  309. Procedure TestProc_Assembler;
  310. Procedure TestProc_VarParam;
  311. Procedure TestProc_VarParamString;
  312. Procedure TestProc_VarParamV;
  313. Procedure TestProc_Overload;
  314. Procedure TestProc_OverloadForward;
  315. Procedure TestProc_OverloadIntfImpl;
  316. Procedure TestProc_OverloadNested;
  317. Procedure TestProc_OverloadNestedForward;
  318. Procedure TestProc_OverloadUnitCycle;
  319. Procedure TestProc_Varargs;
  320. Procedure TestProc_ConstOrder;
  321. Procedure TestProc_DuplicateConst;
  322. Procedure TestProc_LocalVarAbsolute;
  323. Procedure TestProc_LocalVarInit;
  324. Procedure TestProc_ReservedWords;
  325. Procedure TestProc_ConstRefWord;
  326. // anonymous functions
  327. Procedure TestAnonymousProc_Assign_ObjFPC;
  328. Procedure TestAnonymousProc_Assign_Delphi;
  329. Procedure TestAnonymousProc_Arg;
  330. Procedure TestAnonymousProc_Typecast;
  331. Procedure TestAnonymousProc_With;
  332. Procedure TestAnonymousProc_ExceptOn;
  333. Procedure TestAnonymousProc_Nested;
  334. Procedure TestAnonymousProc_NestedAssignResult;
  335. Procedure TestAnonymousProc_Class;
  336. Procedure TestAnonymousProc_ForLoop;
  337. Procedure TestAnonymousProc_AsmDelphi;
  338. // enums, sets
  339. Procedure TestEnum_Name;
  340. Procedure TestEnum_Number;
  341. Procedure TestEnum_ConstFail;
  342. Procedure TestEnum_Functions;
  343. Procedure TestEnumRg_Functions;
  344. Procedure TestEnum_AsParams;
  345. Procedure TestEnumRange_Array;
  346. Procedure TestEnum_ForIn;
  347. Procedure TestEnum_ScopedNumber;
  348. Procedure TestEnum_InFunction;
  349. Procedure TestEnum_Name_Anonymous_Unit;
  350. Procedure TestSet_Enum;
  351. Procedure TestSet_Operators;
  352. Procedure TestSet_Operator_In;
  353. Procedure TestSet_Functions;
  354. Procedure TestSet_PassAsArgClone;
  355. Procedure TestSet_AsParams;
  356. Procedure TestSet_Property;
  357. Procedure TestSet_EnumConst;
  358. Procedure TestSet_IntConst;
  359. Procedure TestSet_IntRange;
  360. Procedure TestSet_AnonymousEnumType;
  361. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  362. Procedure TestSet_ConstEnum;
  363. Procedure TestSet_ConstChar;
  364. Procedure TestSet_ConstInt;
  365. Procedure TestSet_InFunction;
  366. Procedure TestSet_ForIn;
  367. // statements
  368. Procedure TestNestBegin;
  369. Procedure TestIncDec;
  370. Procedure TestLoHiFpcMode;
  371. Procedure TestLoHiDelphiMode;
  372. Procedure TestAssignments;
  373. Procedure TestArithmeticOperators1;
  374. Procedure TestLogicalOperators;
  375. Procedure TestBitwiseOperators;
  376. Procedure TestBitwiseOperatorsLongword;
  377. Procedure TestFunctionInt;
  378. Procedure TestFunctionString;
  379. Procedure TestIfThen;
  380. Procedure TestForLoop;
  381. Procedure TestForLoopInsideFunction;
  382. Procedure TestForLoop_ReadVarAfter;
  383. Procedure TestForLoop_Nested;
  384. Procedure TestRepeatUntil;
  385. Procedure TestAsmBlock;
  386. Procedure TestAsmPas_Impl; // ToDo
  387. Procedure TestTryFinally;
  388. Procedure TestTryExcept;
  389. Procedure TestTryExcept_ReservedWords;
  390. Procedure TestIfThenRaiseElse;
  391. Procedure TestCaseOf;
  392. Procedure TestCaseOf_UseSwitch;
  393. Procedure TestCaseOfNoElse;
  394. Procedure TestCaseOfNoElse_UseSwitch;
  395. Procedure TestCaseOfRange;
  396. Procedure TestCaseOfString;
  397. Procedure TestCaseOfChar;
  398. Procedure TestCaseOfExternalClassConst;
  399. Procedure TestDebugger;
  400. // arrays
  401. Procedure TestArray_Dynamic;
  402. Procedure TestArray_Dynamic_Nil;
  403. Procedure TestArray_DynMultiDimensional;
  404. Procedure TestArray_DynamicAssign;
  405. Procedure TestArray_StaticInt;
  406. Procedure TestArray_StaticBool;
  407. Procedure TestArray_StaticChar;
  408. Procedure TestArray_StaticMultiDim;
  409. Procedure TestArray_StaticInFunction;
  410. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  411. Procedure TestArrayOfRecord;
  412. Procedure TestArray_StaticRecord;
  413. Procedure TestArrayOfSet;
  414. Procedure TestArray_DynAsParam;
  415. Procedure TestArray_StaticAsParam;
  416. Procedure TestArrayElement_AsParams;
  417. Procedure TestArrayElementFromFuncResult_AsParams;
  418. Procedure TestArrayEnumTypeRange;
  419. Procedure TestArray_SetLengthOutArg;
  420. Procedure TestArray_SetLengthProperty;
  421. Procedure TestArray_SetLengthMultiDim;
  422. Procedure TestArray_SetLengthDynOfStatic;
  423. Procedure TestArray_OpenArrayOfString;
  424. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  425. Procedure TestArray_ConstRef;
  426. Procedure TestArray_Concat;
  427. Procedure TestArray_Copy;
  428. Procedure TestArray_InsertDelete;
  429. Procedure TestArray_DynArrayConstObjFPC;
  430. Procedure TestArray_DynArrayConstDelphi;
  431. Procedure TestArray_ArrayLitAsParam;
  432. Procedure TestArray_ArrayLitMultiDimAsParam;
  433. Procedure TestArray_ArrayLitStaticAsParam;
  434. Procedure TestArray_ForInArrOfString;
  435. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  436. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  437. Procedure TestArrayOfConst_TVarRec;
  438. Procedure TestArrayOfConst_PassBaseTypes;
  439. Procedure TestArrayOfConst_PassObj;
  440. // record
  441. Procedure TestRecord_Empty;
  442. Procedure TestRecord_Var;
  443. Procedure TestRecord_VarExternal;
  444. Procedure TestRecord_WithDo;
  445. Procedure TestRecord_Assign;
  446. Procedure TestRecord_AsParams;
  447. Procedure TestRecord_ConstRef;
  448. Procedure TestRecordElement_AsParams;
  449. Procedure TestRecordElementFromFuncResult_AsParams;
  450. Procedure TestRecordElementFromWith_AsParams;
  451. Procedure TestRecord_Equal;
  452. Procedure TestRecord_JSValue;
  453. Procedure TestRecord_VariantFail;
  454. Procedure TestRecord_FieldArray;
  455. Procedure TestRecord_Const;
  456. Procedure TestRecord_TypecastFail;
  457. Procedure TestRecord_InFunction;
  458. Procedure TestRecord_AnonymousFail;
  459. // advanced record
  460. Procedure TestAdvRecord_Function;
  461. Procedure TestAdvRecord_Property;
  462. Procedure TestAdvRecord_PropertyDefault;
  463. Procedure TestAdvRecord_Property_ClassMethod;
  464. Procedure TestAdvRecord_Const;
  465. Procedure TestAdvRecord_ExternalField;
  466. Procedure TestAdvRecord_SubRecord;
  467. Procedure TestAdvRecord_SubClass;
  468. Procedure TestAdvRecord_SubInterfaceFail;
  469. Procedure TestAdvRecord_Constructor;
  470. Procedure TestAdvRecord_ClassConstructor_Program;
  471. Procedure TestAdvRecord_ClassConstructor_Unit;
  472. // classes
  473. Procedure TestClass_TObjectDefaultConstructor;
  474. Procedure TestClass_TObjectConstructorWithParams;
  475. Procedure TestClass_TObjectConstructorWithDefaultParam;
  476. Procedure TestClass_Var;
  477. Procedure TestClass_Method;
  478. Procedure TestClass_Implementation;
  479. Procedure TestClass_Inheritance;
  480. Procedure TestClass_TypeAlias;
  481. Procedure TestClass_AbstractMethod;
  482. Procedure TestClass_CallInherited_ProcNoParams;
  483. Procedure TestClass_CallInherited_WithParams;
  484. Procedure TestClasS_CallInheritedConstructor;
  485. Procedure TestClass_ClassVar_Assign;
  486. Procedure TestClass_CallClassMethod;
  487. Procedure TestClass_CallClassMethodStatic; // ToDo
  488. Procedure TestClass_Property;
  489. Procedure TestClass_Property_ClassMethod;
  490. Procedure TestClass_Property_Indexed;
  491. Procedure TestClass_Property_IndexSpec;
  492. Procedure TestClass_PropertyOfTypeArray;
  493. Procedure TestClass_PropertyDefault;
  494. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  495. //Procedure TestClass_PropertyDefault;
  496. Procedure TestClass_PropertyOverride;
  497. Procedure TestClass_PropertyIncVisibility;
  498. Procedure TestClass_Assigned;
  499. Procedure TestClass_WithClassDoCreate;
  500. Procedure TestClass_WithClassInstDoProperty;
  501. Procedure TestClass_WithClassInstDoPropertyWithParams;
  502. Procedure TestClass_WithClassInstDoFunc;
  503. Procedure TestClass_TypeCast;
  504. Procedure TestClass_TypeCastUntypedParam;
  505. Procedure TestClass_Overloads;
  506. Procedure TestClass_OverloadsAncestor;
  507. Procedure TestClass_OverloadConstructor;
  508. Procedure TestClass_OverloadDelphiOverride;
  509. Procedure TestClass_ReintroduceVarDelphi;
  510. Procedure TestClass_ReintroducedVar;
  511. Procedure TestClass_RaiseDescendant;
  512. Procedure TestClass_ExternalMethod;
  513. Procedure TestClass_ExternalVirtualNameMismatchFail;
  514. Procedure TestClass_ExternalOverrideFail;
  515. Procedure TestClass_ExternalVar;
  516. Procedure TestClass_Const;
  517. Procedure TestClass_ConstEnum;
  518. Procedure TestClass_LocalConstDuplicate_Prg;
  519. Procedure TestClass_LocalConstDuplicate_Unit;
  520. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  521. Procedure TestClass_LocalVarSelfFail;
  522. Procedure TestClass_ArgSelfFail;
  523. Procedure TestClass_NestedProcSelf;
  524. Procedure TestClass_NestedProcSelf2;
  525. Procedure TestClass_NestedProcClassSelf;
  526. Procedure TestClass_NestedProcCallInherited;
  527. Procedure TestClass_TObjectFree;
  528. Procedure TestClass_TObjectFree_VarArg;
  529. Procedure TestClass_TObjectFreeNewInstance;
  530. Procedure TestClass_TObjectFreeLowerCase;
  531. Procedure TestClass_TObjectFreeFunctionFail;
  532. Procedure TestClass_TObjectFreePropertyFail;
  533. Procedure TestClass_ForIn;
  534. Procedure TestClass_DispatchMessage;
  535. Procedure TestClass_Message_DuplicateIntFail;
  536. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  537. // class of
  538. Procedure TestClassOf_Create;
  539. Procedure TestClassOf_Call;
  540. Procedure TestClassOf_Assign;
  541. Procedure TestClassOf_Is;
  542. Procedure TestClassOf_Compare;
  543. Procedure TestClassOf_ClassVar;
  544. Procedure TestClassOf_ClassMethod;
  545. Procedure TestClassOf_ClassProperty;
  546. Procedure TestClassOf_ClassMethodSelf;
  547. Procedure TestClassOf_TypeCast;
  548. Procedure TestClassOf_ImplicitFunctionCall;
  549. Procedure TestClassOf_Const;
  550. // nested class
  551. Procedure TestNestedClass_Alias;
  552. Procedure TestNestedClass_Record;
  553. Procedure TestNestedClass_Class;
  554. // external class
  555. Procedure TestExternalClass_Var;
  556. Procedure TestExternalClass_Const;
  557. Procedure TestExternalClass_Dollar;
  558. Procedure TestExternalClass_DuplicateVarFail;
  559. Procedure TestExternalClass_Method;
  560. Procedure TestExternalClass_ClassMethod;
  561. Procedure TestExternalClass_ClassMethodStatic;
  562. Procedure TestExternalClass_FunctionResultInTypeCast;
  563. Procedure TestExternalClass_NonExternalOverride;
  564. Procedure TestExternalClass_OverloadHint;
  565. Procedure TestExternalClass_SameNamePublishedProperty;
  566. Procedure TestExternalClass_Property;
  567. Procedure TestExternalClass_PropertyDate;
  568. Procedure TestExternalClass_ClassProperty;
  569. Procedure TestExternalClass_ClassOf;
  570. Procedure TestExternalClass_ClassOtherUnit;
  571. Procedure TestExternalClass_Is;
  572. Procedure TestExternalClass_As;
  573. Procedure TestExternalClass_DestructorFail;
  574. Procedure TestExternalClass_New;
  575. Procedure TestExternalClass_ClassOf_New;
  576. Procedure TestExternalClass_FuncClassOf_New;
  577. Procedure TestExternalClass_New_PasClassFail;
  578. Procedure TestExternalClass_New_PasClassBracketsFail;
  579. Procedure TestExternalClass_NewExtName;
  580. Procedure TestExternalClass_Constructor;
  581. Procedure TestExternalClass_ConstructorBrackets;
  582. Procedure TestExternalClass_LocalConstSameName;
  583. Procedure TestExternalClass_ReintroduceOverload;
  584. Procedure TestExternalClass_Inherited;
  585. Procedure TestExternalClass_PascalAncestorFail;
  586. Procedure TestExternalClass_NewInstance;
  587. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  588. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  589. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  590. Procedure TestExternalClass_JSFunctionPasDescendant;
  591. Procedure TestExternalClass_PascalProperty;
  592. Procedure TestExternalClass_TypeCastToRootClass;
  593. Procedure TestExternalClass_TypeCastToJSObject;
  594. Procedure TestExternalClass_TypeCastStringToExternalString;
  595. Procedure TestExternalClass_TypeCastToJSFunction;
  596. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  597. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  598. Procedure TestExternalClass_BracketAccessor;
  599. Procedure TestExternalClass_BracketAccessor_Call;
  600. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  601. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  602. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  603. Procedure TestExternalClass_BracketAccessor_MultiType;
  604. Procedure TestExternalClass_BracketAccessor_Index;
  605. Procedure TestExternalClass_ForInJSObject;
  606. Procedure TestExternalClass_ForInJSArray;
  607. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  608. // class interfaces
  609. Procedure TestClassInterface_Corba;
  610. Procedure TestClassInterface_ProcExternalFail;
  611. Procedure TestClassInterface_Overloads;
  612. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  613. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  614. Procedure TestClassInterface_AncestorImpl;
  615. Procedure TestClassInterface_ImplReintroduce;
  616. Procedure TestClassInterface_MethodResolution;
  617. Procedure TestClassInterface_AncestorMoreInterfaces;
  618. Procedure TestClassInterface_MethodOverride;
  619. Procedure TestClassInterface_Corba_Delegation;
  620. Procedure TestClassInterface_Corba_DelegationStatic;
  621. Procedure TestClassInterface_Corba_Operators;
  622. Procedure TestClassInterface_Corba_Args;
  623. Procedure TestClassInterface_Corba_ForIn;
  624. Procedure TestClassInterface_COM_AssignVar;
  625. Procedure TestClassInterface_COM_AssignArg;
  626. Procedure TestClassInterface_COM_FunctionResult;
  627. Procedure TestClassInterface_COM_InheritedFuncResult;
  628. Procedure TestClassInterface_COM_IsAsTypeCasts;
  629. Procedure TestClassInterface_COM_PassAsArg;
  630. Procedure TestClassInterface_COM_PassToUntypedParam;
  631. Procedure TestClassInterface_COM_FunctionInExpr;
  632. Procedure TestClassInterface_COM_Property;
  633. Procedure TestClassInterface_COM_IntfProperty;
  634. Procedure TestClassInterface_COM_Delegation;
  635. Procedure TestClassInterface_COM_With;
  636. Procedure TestClassInterface_COM_ForIn;
  637. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  638. Procedure TestClassInterface_COM_RecordIntfFail;
  639. Procedure TestClassInterface_COM_UnitInitialization;
  640. Procedure TestClassInterface_GUID;
  641. Procedure TestClassInterface_GUIDProperty;
  642. // helpers
  643. Procedure TestClassHelper_ClassVar;
  644. Procedure TestClassHelper_Method_AccessInstanceFields;
  645. Procedure TestClassHelper_Method_Call;
  646. Procedure TestClassHelper_Method_Nested_Call;
  647. Procedure TestClassHelper_ClassMethod_Call;
  648. Procedure TestClassHelper_ClassOf;
  649. Procedure TestClassHelper_MethodRefObjFPC;
  650. Procedure TestClassHelper_Constructor;
  651. Procedure TestClassHelper_InheritedObjFPC;
  652. Procedure TestClassHelper_Property;
  653. Procedure TestClassHelper_Property_Array;
  654. Procedure TestClassHelper_Property_Array_Default;
  655. Procedure TestClassHelper_Property_Array_DefaultDefault;
  656. Procedure TestClassHelper_ClassProperty;
  657. Procedure TestClassHelper_ClassPropertyStatic;
  658. Procedure TestClassHelper_ClassProperty_Array;
  659. Procedure TestClassHelper_ForIn;
  660. Procedure TestClassHelper_PassProperty;
  661. Procedure TestExtClassHelper_ClassVar;
  662. Procedure TestExtClassHelper_Method_Call;
  663. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  664. Procedure TestRecordHelper_ClassVar;
  665. Procedure TestRecordHelper_Method_Call;
  666. Procedure TestRecordHelper_Constructor;
  667. Procedure TestTypeHelper_ClassVar;
  668. Procedure TestTypeHelper_PassResultElement;
  669. Procedure TestTypeHelper_PassArgs;
  670. Procedure TestTypeHelper_PassVarConst;
  671. Procedure TestTypeHelper_PassFuncResult;
  672. Procedure TestTypeHelper_PassPropertyField;
  673. Procedure TestTypeHelper_PassPropertyGetter;
  674. Procedure TestTypeHelper_PassClassPropertyField;
  675. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  676. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  677. Procedure TestTypeHelper_Property;
  678. Procedure TestTypeHelper_Property_Array;
  679. Procedure TestTypeHelper_ClassProperty;
  680. Procedure TestTypeHelper_ClassProperty_Array;
  681. Procedure TestTypeHelper_ClassMethod;
  682. Procedure TestTypeHelper_ExtClassMethodFail;
  683. Procedure TestTypeHelper_Constructor;
  684. Procedure TestTypeHelper_Word;
  685. Procedure TestTypeHelper_Boolean;
  686. Procedure TestTypeHelper_WordBool;
  687. Procedure TestTypeHelper_Double;
  688. Procedure TestTypeHelper_NativeInt;
  689. Procedure TestTypeHelper_StringChar;
  690. Procedure TestTypeHelper_JSValue;
  691. Procedure TestTypeHelper_Array;
  692. Procedure TestTypeHelper_EnumType;
  693. Procedure TestTypeHelper_SetType;
  694. Procedure TestTypeHelper_InterfaceType;
  695. Procedure TestTypeHelper_NestedSelf;
  696. // proc types
  697. Procedure TestProcType;
  698. Procedure TestProcType_Arg;
  699. Procedure TestProcType_FunctionFPC;
  700. Procedure TestProcType_FunctionDelphi;
  701. Procedure TestProcType_ProcedureDelphi;
  702. Procedure TestProcType_AsParam;
  703. Procedure TestProcType_MethodFPC;
  704. Procedure TestProcType_MethodDelphi;
  705. Procedure TestProcType_PropertyFPC;
  706. Procedure TestProcType_PropertyDelphi;
  707. Procedure TestProcType_WithClassInstDoPropertyFPC;
  708. Procedure TestProcType_Nested;
  709. Procedure TestProcType_NestedOfObject;
  710. Procedure TestProcType_ReferenceToProc;
  711. Procedure TestProcType_ReferenceToMethod;
  712. Procedure TestProcType_Typecast;
  713. Procedure TestProcType_PassProcToUntyped;
  714. Procedure TestProcType_PassProcToArray;
  715. Procedure TestProcType_SafeCallObjFPC;
  716. Procedure TestProcType_SafeCallDelphi;
  717. // pointer
  718. Procedure TestPointer;
  719. Procedure TestPointer_Proc;
  720. Procedure TestPointer_AssignRecordFail;
  721. Procedure TestPointer_AssignStaticArrayFail;
  722. Procedure TestPointer_TypeCastJSValueToPointer;
  723. Procedure TestPointer_NonRecordFail;
  724. Procedure TestPointer_AnonymousArgTypeFail;
  725. Procedure TestPointer_AnonymousVarTypeFail;
  726. Procedure TestPointer_AnonymousResultTypeFail;
  727. Procedure TestPointer_AddrOperatorFail;
  728. Procedure TestPointer_ArrayParamsFail;
  729. Procedure TestPointer_PointerAddFail;
  730. Procedure TestPointer_IncPointerFail;
  731. Procedure TestPointer_Record;
  732. Procedure TestPointer_RecordArg;
  733. // jsvalue
  734. Procedure TestJSValue_AssignToJSValue;
  735. Procedure TestJSValue_TypeCastToBaseType;
  736. Procedure TestJSValue_TypecastToJSValue;
  737. Procedure TestJSValue_Equal;
  738. Procedure TestJSValue_If;
  739. Procedure TestJSValue_Not;
  740. Procedure TestJSValue_Enum;
  741. Procedure TestJSValue_ClassInstance;
  742. Procedure TestJSValue_ClassOf;
  743. Procedure TestJSValue_ArrayOfJSValue;
  744. Procedure TestJSValue_ArrayLit;
  745. Procedure TestJSValue_Params;
  746. Procedure TestJSValue_UntypedParam;
  747. Procedure TestJSValue_FuncResultType;
  748. Procedure TestJSValue_ProcType_Assign;
  749. Procedure TestJSValue_ProcType_Equal;
  750. Procedure TestJSValue_ProcType_Param;
  751. Procedure TestJSValue_AssignToPointerFail;
  752. Procedure TestJSValue_OverloadDouble;
  753. Procedure TestJSValue_OverloadNativeInt;
  754. Procedure TestJSValue_OverloadWord;
  755. Procedure TestJSValue_OverloadString;
  756. Procedure TestJSValue_OverloadChar;
  757. Procedure TestJSValue_OverloadPointer;
  758. Procedure TestJSValue_ForIn;
  759. // RTTI
  760. Procedure TestRTTI_IntRange;
  761. Procedure TestRTTI_Double;
  762. Procedure TestRTTI_ProcType;
  763. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  764. Procedure TestRTTI_EnumAndSetType;
  765. Procedure TestRTTI_EnumRange;
  766. Procedure TestRTTI_AnonymousEnumType;
  767. Procedure TestRTTI_StaticArray;
  768. Procedure TestRTTI_DynArray;
  769. Procedure TestRTTI_ArrayNestedAnonymous;
  770. Procedure TestRTTI_PublishedMethodOverloadFail;
  771. Procedure TestRTTI_PublishedMethodExternalFail;
  772. Procedure TestRTTI_PublishedClassPropertyFail;
  773. Procedure TestRTTI_PublishedClassFieldFail;
  774. Procedure TestRTTI_PublishedFieldExternalFail;
  775. Procedure TestRTTI_Class_Field;
  776. Procedure TestRTTI_Class_Method;
  777. Procedure TestRTTI_Class_MethodArgFlags;
  778. Procedure TestRTTI_Class_Property;
  779. Procedure TestRTTI_Class_PropertyParams;
  780. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  781. Procedure TestRTTI_Class_OmitRTTI;
  782. Procedure TestRTTI_IndexModifier;
  783. Procedure TestRTTI_StoredModifier;
  784. Procedure TestRTTI_DefaultValue;
  785. Procedure TestRTTI_DefaultValueSet;
  786. Procedure TestRTTI_DefaultValueRangeType;
  787. Procedure TestRTTI_DefaultValueInherit;
  788. Procedure TestRTTI_OverrideMethod;
  789. Procedure TestRTTI_ReintroduceMethod;
  790. Procedure TestRTTI_OverloadProperty;
  791. // ToDo: array argument
  792. Procedure TestRTTI_ClassForward;
  793. Procedure TestRTTI_ClassOf;
  794. Procedure TestRTTI_Record;
  795. Procedure TestRTTI_RecordAnonymousArray;
  796. Procedure TestRTTI_Record_ClassVarType;
  797. Procedure TestRTTI_LocalTypes;
  798. Procedure TestRTTI_TypeInfo_BaseTypes;
  799. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  800. Procedure TestRTTI_TypeInfo_LocalFail;
  801. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  802. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  803. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  804. Procedure TestRTTI_TypeInfo_FunctionClassType;
  805. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  806. Procedure TestRTTI_Interface_Corba;
  807. Procedure TestRTTI_Interface_COM;
  808. Procedure TestRTTI_ClassHelper;
  809. Procedure TestRTTI_ExternalClass;
  810. Procedure TestRTTI_Unit;
  811. // Resourcestring
  812. Procedure TestResourcestringProgram;
  813. Procedure TestResourcestringUnit;
  814. Procedure TestResourcestringImplementation;
  815. // Attributes
  816. Procedure TestAttributes_Members;
  817. Procedure TestAttributes_Types;
  818. Procedure TestAttributes_HelperConstructor_Fail;
  819. // Assertions, checks
  820. procedure TestAssert;
  821. procedure TestAssert_SysUtils;
  822. procedure TestObjectChecks;
  823. procedure TestOverflowChecks_Int;
  824. procedure TestRangeChecks_AssignInt;
  825. procedure TestRangeChecks_AssignIntRange;
  826. procedure TestRangeChecks_AssignEnum;
  827. procedure TestRangeChecks_AssignEnumRange;
  828. procedure TestRangeChecks_AssignChar;
  829. procedure TestRangeChecks_AssignCharRange;
  830. procedure TestRangeChecks_ArrayIndex;
  831. procedure TestRangeChecks_ArrayOfRecIndex;
  832. procedure TestRangeChecks_StringIndex;
  833. procedure TestRangeChecks_TypecastInt;
  834. procedure TestRangeChecks_TypeHelperInt;
  835. // Async/AWait
  836. Procedure TestAsync_Proc;
  837. Procedure TestAsync_CallResultIsPromise;
  838. Procedure TestAsync_ConstructorFail;
  839. Procedure TestAsync_PropertyGetterFail;
  840. Procedure TestAwait_NonPromiseWithTypeFail;
  841. Procedure TestAwait_AsyncCallTypeMismatch;
  842. Procedure TestAWait_OutsideAsyncFail;
  843. Procedure TestAWait_IntegerFail;
  844. Procedure TestAWait_ExternalClassPromise;
  845. Procedure TestAWait_JSValue;
  846. Procedure TestAWait_Result;
  847. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  848. Procedure TestAsync_AnonymousProc;
  849. Procedure TestAsync_ProcType;
  850. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  851. Procedure TestAsync_Inherited;
  852. Procedure TestAsync_ClassInterface;
  853. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  854. end;
  855. function LinesToStr(Args: array of const): string;
  856. function ExtractFileUnitName(aFilename: string): string;
  857. function JSToStr(El: TJSElement): string;
  858. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  859. implementation
  860. function LinesToStr(Args: array of const): string;
  861. var
  862. s: String;
  863. i: Integer;
  864. begin
  865. s:='';
  866. for i:=Low(Args) to High(Args) do
  867. case Args[i].VType of
  868. vtChar: s += Args[i].VChar+LineEnding;
  869. vtString: s += Args[i].VString^+LineEnding;
  870. vtPChar: s += Args[i].VPChar+LineEnding;
  871. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  872. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  873. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  874. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  875. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  876. end;
  877. Result:=s;
  878. end;
  879. function ExtractFileUnitName(aFilename: string): string;
  880. var
  881. p: Integer;
  882. begin
  883. Result:=ExtractFileName(aFilename);
  884. if Result='' then exit;
  885. for p:=length(Result) downto 1 do
  886. case Result[p] of
  887. '/','\': exit;
  888. '.':
  889. begin
  890. Delete(Result,p,length(Result));
  891. exit;
  892. end;
  893. end;
  894. end;
  895. function JSToStr(El: TJSElement): string;
  896. var
  897. aWriter: TBufferWriter;
  898. aJSWriter: TJSWriter;
  899. begin
  900. aJSWriter:=nil;
  901. aWriter:=TBufferWriter.Create(1000);
  902. try
  903. aJSWriter:=TJSWriter.Create(aWriter);
  904. aJSWriter.IndentSize:=2;
  905. aJSWriter.WriteJS(El);
  906. Result:=aWriter.AsString;
  907. finally
  908. aJSWriter.Free;
  909. aWriter.Free;
  910. end;
  911. end;
  912. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  913. // search diff, ignore changes in spaces
  914. const
  915. SpaceChars = [#9,#10,#13,' '];
  916. var
  917. ExpectedP, ActualP: PChar;
  918. function FindLineEnd(p: PChar): PChar;
  919. begin
  920. Result:=p;
  921. while not (Result^ in [#0,#10,#13]) do inc(Result);
  922. end;
  923. function FindLineStart(p, MinP: PChar): PChar;
  924. begin
  925. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  926. Result:=p;
  927. end;
  928. procedure SkipLineEnd(var p: PChar);
  929. begin
  930. if p^ in [#10,#13] then
  931. begin
  932. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  933. inc(p,2)
  934. else
  935. inc(p);
  936. end;
  937. end;
  938. function HasSpecialChar(s: string): boolean;
  939. var
  940. i: Integer;
  941. begin
  942. for i:=1 to length(s) do
  943. if s[i] in [#0..#31,#127..#255] then
  944. exit(true);
  945. Result:=false;
  946. end;
  947. function HashSpecialChars(s: string): string;
  948. var
  949. i: Integer;
  950. begin
  951. Result:='';
  952. for i:=1 to length(s) do
  953. if s[i] in [#0..#31,#127..#255] then
  954. Result:=Result+'#'+hexstr(ord(s[i]),2)
  955. else
  956. Result:=Result+s[i];
  957. end;
  958. procedure DiffFound;
  959. var
  960. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  961. ExpLine, ActLine: String;
  962. i, LineNo, DiffLineNo: Integer;
  963. begin
  964. writeln('Diff found "',Msg,'". Lines:');
  965. // write correct lines
  966. p:=PChar(Expected);
  967. LineNo:=0;
  968. DiffLineNo:=0;
  969. repeat
  970. StartPos:=p;
  971. while not (p^ in [#0,#10,#13]) do inc(p);
  972. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  973. SkipLineEnd(p);
  974. inc(LineNo);
  975. if (p<=ExpectedP) and (p^<>#0) then
  976. begin
  977. writeln('= ',ExpLine);
  978. end else begin
  979. // diff line
  980. if DiffLineNo=0 then DiffLineNo:=LineNo;
  981. // write actual line
  982. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  983. ActLineEndP:=FindLineEnd(ActualP);
  984. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  985. writeln('- ',ActLine);
  986. if HasSpecialChar(ActLine) then
  987. writeln('- ',HashSpecialChars(ActLine));
  988. // write expected line
  989. writeln('+ ',ExpLine);
  990. if HasSpecialChar(ExpLine) then
  991. writeln('- ',HashSpecialChars(ExpLine));
  992. // write empty line with pointer ^
  993. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  994. writeln('^');
  995. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  996. CheckSrcDiff:=false;
  997. // write up to three following actual lines to get some context
  998. for i:=1 to 3 do begin
  999. ActLineStartP:=ActLineEndP;
  1000. SkipLineEnd(ActLineStartP);
  1001. if ActLineStartP^=#0 then break;
  1002. ActLineEndP:=FindLineEnd(ActLineStartP);
  1003. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1004. writeln('~ ',ActLine);
  1005. end;
  1006. exit;
  1007. end;
  1008. until p^=#0;
  1009. writeln('DiffFound Actual:-----------------------');
  1010. writeln(Actual);
  1011. writeln('DiffFound Expected:---------------------');
  1012. writeln(Expected);
  1013. writeln('DiffFound ------------------------------');
  1014. Msg:='diff found, but lines are the same, internal error';
  1015. CheckSrcDiff:=false;
  1016. end;
  1017. var
  1018. IsSpaceNeeded: Boolean;
  1019. LastChar, Quote: Char;
  1020. begin
  1021. Result:=true;
  1022. Msg:='';
  1023. if Expected='' then Expected:=' ';
  1024. if Actual='' then Actual:=' ';
  1025. ExpectedP:=PChar(Expected);
  1026. ActualP:=PChar(Actual);
  1027. repeat
  1028. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1029. case ExpectedP^ of
  1030. #0:
  1031. begin
  1032. // check that rest of Actual has only spaces
  1033. while ActualP^ in SpaceChars do inc(ActualP);
  1034. if ActualP^<>#0 then
  1035. begin
  1036. DiffFound;
  1037. exit;
  1038. end;
  1039. exit(true);
  1040. end;
  1041. ' ',#9,#10,#13:
  1042. begin
  1043. // skip space in Expected
  1044. IsSpaceNeeded:=false;
  1045. if ExpectedP>PChar(Expected) then
  1046. LastChar:=ExpectedP[-1]
  1047. else
  1048. LastChar:=#0;
  1049. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1050. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1051. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1052. IsSpaceNeeded:=true;
  1053. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1054. begin
  1055. DiffFound;
  1056. exit;
  1057. end;
  1058. while ActualP^ in SpaceChars do inc(ActualP);
  1059. end;
  1060. '''','"':
  1061. begin
  1062. while ActualP^ in SpaceChars do inc(ActualP);
  1063. if ExpectedP^<>ActualP^ then
  1064. begin
  1065. DiffFound;
  1066. exit;
  1067. end;
  1068. Quote:=ExpectedP^;
  1069. repeat
  1070. inc(ExpectedP);
  1071. inc(ActualP);
  1072. if ExpectedP^<>ActualP^ then
  1073. begin
  1074. DiffFound;
  1075. exit;
  1076. end;
  1077. if (ExpectedP^ in [#0,#10,#13]) then
  1078. break
  1079. else if (ExpectedP^=Quote) then
  1080. begin
  1081. inc(ExpectedP);
  1082. inc(ActualP);
  1083. break;
  1084. end;
  1085. until false;
  1086. end;
  1087. else
  1088. while ActualP^ in SpaceChars do inc(ActualP);
  1089. if ExpectedP^<>ActualP^ then
  1090. begin
  1091. DiffFound;
  1092. exit;
  1093. end;
  1094. inc(ExpectedP);
  1095. inc(ActualP);
  1096. end;
  1097. until false;
  1098. end;
  1099. { TTestEnginePasResolver }
  1100. destructor TTestEnginePasResolver.Destroy;
  1101. begin
  1102. FreeAndNil(FStreamResolver);
  1103. FreeAndNil(FParser);
  1104. FreeAndNil(FScanner);
  1105. FreeAndNil(FStreamResolver);
  1106. if Module<>nil then
  1107. begin
  1108. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1109. FModule:=nil;
  1110. end;
  1111. inherited Destroy;
  1112. end;
  1113. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1114. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1115. begin
  1116. Result:=nil;
  1117. if InFilename<>'' then
  1118. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1119. if Assigned(OnFindUnit) then
  1120. Result:=OnFindUnit(AName);
  1121. if NameExpr=nil then ;
  1122. end;
  1123. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1124. begin
  1125. // do not parse recursively
  1126. // parse via the queue
  1127. if Section=nil then ;
  1128. end;
  1129. { TCustomTestModule }
  1130. function TCustomTestModule.GetMsgCount: integer;
  1131. begin
  1132. Result:=FHintMsgs.Count;
  1133. end;
  1134. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1135. begin
  1136. Result:=TTestHintMessage(FHintMsgs[Index]);
  1137. end;
  1138. function TCustomTestModule.GetResolverCount: integer;
  1139. begin
  1140. Result:=FModules.Count;
  1141. end;
  1142. function TCustomTestModule.GetResolvers(Index: integer
  1143. ): TTestEnginePasResolver;
  1144. begin
  1145. Result:=TTestEnginePasResolver(FModules[Index]);
  1146. end;
  1147. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1148. ): TPasModule;
  1149. var
  1150. DefNamespace: String;
  1151. begin
  1152. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1153. if (Pos('.',aUnitName)<1) then
  1154. begin
  1155. DefNamespace:=GetDefaultNamespace;
  1156. if DefNamespace<>'' then
  1157. begin
  1158. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1159. if Result<>nil then exit;
  1160. end;
  1161. end;
  1162. Result:=LoadUnit(aUnitName);
  1163. if Result<>nil then exit;
  1164. {$IFDEF VerbosePas2JS}
  1165. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1166. {$ENDIF}
  1167. Fail('can''t find unit "'+aUnitName+'"');
  1168. end;
  1169. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1170. var
  1171. aParser: TPasParser;
  1172. Item: TTestHintMessage;
  1173. begin
  1174. aParser:=Sender as TPasParser;
  1175. Item:=TTestHintMessage.Create;
  1176. Item.Id:=aParser.LastMsgNumber;
  1177. Item.MsgType:=aParser.LastMsgType;
  1178. Item.MsgNumber:=aParser.LastMsgNumber;
  1179. Item.Msg:=Msg;
  1180. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1181. {$IFDEF VerbosePas2JS}
  1182. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1183. {$ENDIF}
  1184. FHintMsgs.Add(Item);
  1185. end;
  1186. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1187. );
  1188. var
  1189. aResolver: TTestEnginePasResolver;
  1190. Item: TTestHintMessage;
  1191. begin
  1192. aResolver:=Sender as TTestEnginePasResolver;
  1193. Item:=TTestHintMessage.Create;
  1194. Item.Id:=aResolver.LastMsgId;
  1195. Item.MsgType:=aResolver.LastMsgType;
  1196. Item.MsgNumber:=aResolver.LastMsgNumber;
  1197. Item.Msg:=Msg;
  1198. Item.SourcePos:=aResolver.LastSourcePos;
  1199. {$IFDEF VerbosePas2JS}
  1200. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1201. {$ENDIF}
  1202. FHintMsgs.Add(Item);
  1203. end;
  1204. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1205. var
  1206. Item: TTestHintMessage;
  1207. aScanner: TPas2jsPasScanner;
  1208. begin
  1209. aScanner:=Sender as TPas2jsPasScanner;
  1210. Item:=TTestHintMessage.Create;
  1211. Item.Id:=aScanner.LastMsgNumber;
  1212. Item.MsgType:=aScanner.LastMsgType;
  1213. Item.MsgNumber:=aScanner.LastMsgNumber;
  1214. Item.Msg:=Msg;
  1215. Item.SourcePos:=aScanner.CurSourcePos;
  1216. {$IFDEF VerbosePas2JS}
  1217. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1218. {$ENDIF}
  1219. FHintMsgs.Add(Item);
  1220. end;
  1221. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1222. begin
  1223. if FWithTypeInfo=AValue then Exit;
  1224. FWithTypeInfo:=AValue;
  1225. if AValue then
  1226. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1227. else
  1228. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1229. end;
  1230. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1231. var
  1232. i: Integer;
  1233. CurEngine: TTestEnginePasResolver;
  1234. CurUnitName: String;
  1235. begin
  1236. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1237. Result:=nil;
  1238. if (Module.ClassType=TPasModule)
  1239. and (CompareText(Module.Name,aUnitName)=0) then
  1240. exit(Module);
  1241. for i:=0 to ResolverCount-1 do
  1242. begin
  1243. CurEngine:=Resolvers[i];
  1244. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1245. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1246. if CompareText(aUnitName,CurUnitName)=0 then
  1247. begin
  1248. Result:=CurEngine.Module;
  1249. if Result<>nil then exit;
  1250. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1251. FileResolver.FindSourceFile(aUnitName);
  1252. CurEngine.StreamResolver:=TStreamResolver.Create;
  1253. CurEngine.StreamResolver.OwnsStreams:=True;
  1254. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1255. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1256. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1257. InitScanner(CurEngine.Scanner);
  1258. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1259. CurEngine.Parser.Options:=po_tcmodules;
  1260. if CompareText(CurUnitName,'System')=0 then
  1261. CurEngine.Parser.ImplicitUses.Clear;
  1262. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1263. try
  1264. CurEngine.Parser.NextToken;
  1265. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1266. except
  1267. on E: Exception do
  1268. HandleException(E);
  1269. end;
  1270. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1271. Result:=CurEngine.Module;
  1272. exit;
  1273. end;
  1274. end;
  1275. end;
  1276. procedure TCustomTestModule.SetUp;
  1277. begin
  1278. {$IFDEF EnablePasTreeGlobalRefCount}
  1279. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1280. {$ENDIF}
  1281. if FModules<>nil then
  1282. begin
  1283. writeln('TCustomTestModule.SetUp FModules<>nil');
  1284. Halt;
  1285. end;
  1286. inherited SetUp;
  1287. FSkipTests:=false;
  1288. FWithTypeInfo:=false;
  1289. FSource:=TStringList.Create;
  1290. FHub:=TPas2JSResolverHub.Create(Self);
  1291. FModules:=TObjectList.Create(true);
  1292. FFilename:='test1.pp';
  1293. FFileResolver:=TStreamResolver.Create;
  1294. FFileResolver.OwnsStreams:=True;
  1295. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1296. InitScanner(FScanner);
  1297. FEngine:=AddModule(Filename);
  1298. FEngine.Scanner:=FScanner;
  1299. FScanner.Resolver:=FEngine;
  1300. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1301. FParser.OnLog:=@OnParserLog;
  1302. FEngine.Parser:=FParser;
  1303. Parser.Options:=po_tcmodules;
  1304. FModule:=Nil;
  1305. FConverter:=CreateConverter;
  1306. FExpectedErrorClass:=nil;
  1307. end;
  1308. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1309. var
  1310. Options: TPasToJsConverterOptions;
  1311. begin
  1312. Result:=TPasToJSConverter.Create;
  1313. Options:=co_tcmodules;
  1314. if WithTypeInfo then
  1315. Exclude(Options,coNoTypeInfo)
  1316. else
  1317. Include(Options,coNoTypeInfo);
  1318. Result.Options:=Options;
  1319. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1320. end;
  1321. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1322. begin
  1323. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1324. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1325. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1326. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1327. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1328. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1329. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1330. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1331. aScanner.OnLog:=@OnScannerLog;
  1332. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1333. end;
  1334. procedure TCustomTestModule.TearDown;
  1335. {$IFDEF CheckPasTreeRefCount}
  1336. var
  1337. El: TPasElement;
  1338. {$ENDIF}
  1339. var
  1340. i: Integer;
  1341. CurModule: TPasModule;
  1342. begin
  1343. FHintMsgs.Clear;
  1344. FHintMsgsGood.Clear;
  1345. FSkipTests:=false;
  1346. FWithTypeInfo:=false;
  1347. FJSRegModuleCall:=nil;
  1348. FJSModuleCallArgs:=nil;
  1349. FJSImplentationUses:=nil;
  1350. FJSInterfaceUses:=nil;
  1351. FJSModuleSrc:=nil;
  1352. FJSInitBody:=nil;
  1353. FreeAndNil(FJSSource);
  1354. FreeAndNil(FJSModule);
  1355. FreeAndNil(FConverter);
  1356. Engine.Clear;
  1357. FreeAndNil(FSource);
  1358. FreeAndNil(FFileResolver);
  1359. if FModules<>nil then
  1360. begin
  1361. for i:=0 to FModules.Count-1 do
  1362. begin
  1363. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1364. if CurModule=nil then continue;
  1365. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1366. CurModule.ReleaseUsedUnits;
  1367. end;
  1368. if FModule<>nil then
  1369. FModule.ReleaseUsedUnits;
  1370. for i:=0 to FModules.Count-1 do
  1371. begin
  1372. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1373. if CurModule=nil then continue;
  1374. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1375. end;
  1376. FreeAndNil(FModules);
  1377. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1378. FEngine:=nil;
  1379. end;
  1380. FreeAndNil(FHub);
  1381. inherited TearDown;
  1382. {$IFDEF EnablePasTreeGlobalRefCount}
  1383. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1384. begin
  1385. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1386. {$IFDEF CheckPasTreeRefCount}
  1387. El:=TPasElement.FirstRefEl;
  1388. while El<>nil do
  1389. begin
  1390. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1391. for i:=0 to El.RefIds.Count-1 do
  1392. writeln(' ',El.RefIds[i]);
  1393. El:=El.NextRefEl;
  1394. end;
  1395. {$ENDIF}
  1396. Halt;
  1397. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1398. end;
  1399. {$ENDIF}
  1400. end;
  1401. procedure TCustomTestModule.Add(Line: string);
  1402. begin
  1403. Source.Add(Line);
  1404. end;
  1405. procedure TCustomTestModule.Add(const Lines: array of string);
  1406. var
  1407. i: Integer;
  1408. begin
  1409. for i:=low(Lines) to high(Lines) do
  1410. Add(Lines[i]);
  1411. end;
  1412. procedure TCustomTestModule.StartParsing;
  1413. var
  1414. Src: String;
  1415. begin
  1416. Src:=Source.Text;
  1417. FEngine.Source:=Src;
  1418. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1419. Scanner.OpenFile(FileName);
  1420. Writeln('// Test : ',Self.TestName);
  1421. Writeln(Src);
  1422. end;
  1423. procedure TCustomTestModule.ParseModuleQueue;
  1424. var
  1425. i: Integer;
  1426. CurResolver: TTestEnginePasResolver;
  1427. Found: Boolean;
  1428. Section: TPasSection;
  1429. begin
  1430. // parse til exception or all modules finished
  1431. while not SkipTests do
  1432. begin
  1433. Found:=false;
  1434. for i:=0 to ResolverCount-1 do
  1435. begin
  1436. CurResolver:=Resolvers[i];
  1437. if CurResolver.CurrentParser=nil then continue;
  1438. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1439. continue;
  1440. CurResolver.Parser.ParseContinue;
  1441. Found:=true;
  1442. break;
  1443. end;
  1444. if not Found then break;
  1445. end;
  1446. for i:=0 to ResolverCount-1 do
  1447. begin
  1448. CurResolver:=Resolvers[i];
  1449. if CurResolver.Parser=nil then
  1450. begin
  1451. if CurResolver.CurrentParser<>nil then
  1452. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1453. continue;
  1454. end;
  1455. if CurResolver.Parser.CurModule<>nil then
  1456. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1457. end;
  1458. end;
  1459. procedure TCustomTestModule.ParseModule;
  1460. begin
  1461. if SkipTests then exit;
  1462. FFirstPasStatement:=nil;
  1463. try
  1464. StartParsing;
  1465. Parser.ParseMain(FModule);
  1466. ParseModuleQueue;
  1467. except
  1468. on E: Exception do
  1469. HandleException(E);
  1470. end;
  1471. if SkipTests then exit;
  1472. AssertNotNull('Module resulted in Module',Module);
  1473. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1474. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1475. end;
  1476. procedure TCustomTestModule.ParseProgram;
  1477. begin
  1478. if SkipTests then exit;
  1479. ParseModule;
  1480. if SkipTests then exit;
  1481. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1482. FPasProgram:=TPasProgram(Module);
  1483. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1484. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1485. if (PasProgram.InitializationSection.Elements.Count>0) then
  1486. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1487. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1488. end;
  1489. procedure TCustomTestModule.ParseUnit;
  1490. begin
  1491. if SkipTests then exit;
  1492. ParseModule;
  1493. if SkipTests then exit;
  1494. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1495. AssertNotNull('Has interface section',Module.InterfaceSection);
  1496. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1497. if (Module.InitializationSection<>nil)
  1498. and (Module.InitializationSection.Elements.Count>0)
  1499. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1500. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1501. end;
  1502. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1503. ): TTestEnginePasResolver;
  1504. var
  1505. i: Integer;
  1506. begin
  1507. for i:=0 to ResolverCount-1 do
  1508. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1509. exit(Resolvers[i]);
  1510. Result:=nil;
  1511. end;
  1512. function TCustomTestModule.AddModule(aFilename: string
  1513. ): TTestEnginePasResolver;
  1514. begin
  1515. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1516. if FindModuleWithFilename(aFilename)<>nil then
  1517. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1518. Result:=TTestEnginePasResolver.Create;
  1519. Result.Filename:=aFilename;
  1520. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1521. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1522. Result.OnLog:=@OnPasResolverLog;
  1523. Result.Hub:=Hub;
  1524. FModules.Add(Result);
  1525. end;
  1526. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1527. ): TTestEnginePasResolver;
  1528. begin
  1529. Result:=AddModule(aFilename);
  1530. Result.Source:=Src;
  1531. end;
  1532. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1533. ImplementationSrc: string): TTestEnginePasResolver;
  1534. var
  1535. Src: String;
  1536. begin
  1537. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1538. Src+=LineEnding;
  1539. Src+='interface'+LineEnding;
  1540. Src+=LineEnding;
  1541. Src+=InterfaceSrc;
  1542. Src+='implementation'+LineEnding;
  1543. Src+=LineEnding;
  1544. Src+=ImplementationSrc;
  1545. Src+='end.'+LineEnding;
  1546. Result:=AddModuleWithSrc(aFilename,Src);
  1547. end;
  1548. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1549. var
  1550. Intf, Impl: TStringList;
  1551. begin
  1552. Intf:=TStringList.Create;
  1553. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1554. // unit interface
  1555. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1556. Intf.Add('{$modeswitch externalclass}');
  1557. Intf.Add('type');
  1558. Intf.Add(' integer=longint;');
  1559. Intf.Add(' sizeint=nativeint;');
  1560. //'const',
  1561. //' LineEnding = #10;',
  1562. //' DirectorySeparator = ''/'';',
  1563. //' DriveSeparator = '''';',
  1564. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1565. //' AllowDriveSeparators : set of char = [];',
  1566. if supTObject in Parts then
  1567. Intf.AddStrings([
  1568. 'type',
  1569. ' TClass = class of TObject;',
  1570. ' TObject = class',
  1571. ' constructor Create;',
  1572. ' destructor Destroy; virtual;',
  1573. ' class function ClassType: TClass; assembler;',
  1574. ' class function ClassName: String; assembler;',
  1575. ' class function ClassNameIs(const Name: string): boolean;',
  1576. ' class function ClassParent: TClass; assembler;',
  1577. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1578. ' class function UnitName: String; assembler;',
  1579. ' procedure AfterConstruction; virtual;',
  1580. ' procedure BeforeDestruction;virtual;',
  1581. ' function Equals(Obj: TObject): boolean; virtual;',
  1582. ' function ToString: String; virtual;',
  1583. ' end;']);
  1584. if supTInterfacedObject in Parts then
  1585. Intf.AddStrings([
  1586. ' {$Interfaces COM}',
  1587. ' IUnknown = interface',
  1588. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1589. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1590. ' function _AddRef: Integer;',
  1591. ' function _Release: Integer;',
  1592. ' end;',
  1593. ' IInterface = IUnknown;',
  1594. ' TInterfacedObject = class(TObject,IUnknown)',
  1595. ' protected',
  1596. ' fRefCount: Integer;',
  1597. ' { implement methods of IUnknown }',
  1598. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1599. ' function _AddRef: Integer; virtual;',
  1600. ' function _Release: Integer; virtual;',
  1601. ' end;',
  1602. ' TInterfacedClass = class of TInterfacedObject;',
  1603. '',
  1604. '']);
  1605. if supTVarRec in Parts then
  1606. Intf.AddStrings([
  1607. 'const',
  1608. ' vtInteger = 0;',
  1609. ' vtBoolean = 1;',
  1610. ' vtJSValue = 19;',
  1611. 'type',
  1612. ' PVarRec = ^TVarRec;',
  1613. ' TVarRec = record',
  1614. ' VType : byte;',
  1615. ' VJSValue: JSValue;',
  1616. ' vInteger: longint external name ''VJSValue'';',
  1617. ' vBoolean: boolean external name ''VJSValue'';',
  1618. ' end;',
  1619. ' TVarRecArray = array of TVarRec;',
  1620. 'function VarRecs: TVarRecArray; varargs;',
  1621. '']);
  1622. if supTypeInfo in Parts then
  1623. begin
  1624. Intf.AddStrings([
  1625. 'type',
  1626. ' TTypeKind = (',
  1627. ' tkUnknown, // 0',
  1628. ' tkInteger, // 1',
  1629. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1630. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1631. ' tkEnumeration, // 4',
  1632. ' tkSet, // 5',
  1633. ' tkDouble, // 6',
  1634. ' tkBool, // 7',
  1635. ' tkProcVar, // 8 function or procedure',
  1636. ' tkMethod, // 9 proc var of object',
  1637. ' tkArray, // 10 static array',
  1638. ' tkDynArray, // 11',
  1639. ' tkRecord, // 12',
  1640. ' tkClass, // 13',
  1641. ' tkClassRef, // 14',
  1642. ' tkPointer, // 15',
  1643. ' tkJSValue, // 16',
  1644. ' tkRefToProcVar, // 17 variable of procedure type',
  1645. ' tkInterface, // 18',
  1646. ' //tkObject,',
  1647. ' //tkSString,tkLString,tkAString,tkWString,',
  1648. ' //tkVariant,',
  1649. ' //tkWChar,',
  1650. ' //tkInt64,',
  1651. ' //tkQWord,',
  1652. ' //tkInterfaceRaw,',
  1653. ' //tkUString,tkUChar,',
  1654. ' tkHelper, // 19',
  1655. ' //tkFile,',
  1656. ' tkExtClass // 20',
  1657. ' );',
  1658. ' TTypeKinds = set of TTypeKind;',
  1659. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1660. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1661. ' end;',
  1662. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1663. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1664. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1665. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1666. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1667. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1668. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1669. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1670. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1671. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1672. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1673. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1674. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1675. '']);
  1676. end;
  1677. if supWriteln in Parts then
  1678. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1679. Intf.Add('var');
  1680. Intf.Add(' ExitCode: Longint = 0;');
  1681. // unit implementation
  1682. Impl:=TStringList.Create;
  1683. if supTObject in Parts then
  1684. Impl.AddStrings([
  1685. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1686. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1687. 'asm',
  1688. 'end;',
  1689. 'constructor TObject.Create; begin end;',
  1690. 'destructor TObject.Destroy; begin end;',
  1691. 'class function TObject.ClassType: TClass; assembler;',
  1692. 'asm',
  1693. 'end;',
  1694. 'class function TObject.ClassName: String; assembler;',
  1695. 'asm',
  1696. 'end;',
  1697. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1698. 'begin',
  1699. ' Result:=SameText(Name,ClassName);',
  1700. 'end;',
  1701. 'class function TObject.ClassParent: TClass; assembler;',
  1702. 'asm',
  1703. 'end;',
  1704. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1705. 'asm',
  1706. 'end;',
  1707. 'class function TObject.UnitName: String; assembler;',
  1708. 'asm',
  1709. 'end;',
  1710. 'procedure TObject.AfterConstruction; begin end;',
  1711. 'procedure TObject.BeforeDestruction; begin end;',
  1712. 'function TObject.Equals(Obj: TObject): boolean;',
  1713. 'begin',
  1714. ' Result:=Obj=Self;',
  1715. 'end;',
  1716. 'function TObject.ToString: String;',
  1717. 'begin',
  1718. ' Result:=ClassName;',
  1719. 'end;'
  1720. ]);
  1721. if supTInterfacedObject in Parts then
  1722. Impl.AddStrings([
  1723. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1724. //'begin',
  1725. //'end;',
  1726. 'function TInterfacedObject._AddRef: Integer;',
  1727. 'begin',
  1728. 'end;',
  1729. 'function TInterfacedObject._Release: Integer;',
  1730. 'begin',
  1731. 'end;',
  1732. '']);
  1733. if supTVarRec in Parts then
  1734. Impl.AddStrings([
  1735. 'function VarRecs: TVarRecArray; varargs;',
  1736. 'var',
  1737. ' v: PVarRec;',
  1738. 'begin',
  1739. ' v^.VType:=1;',
  1740. ' v^.VJSValue:=2;',
  1741. 'end;',
  1742. '']);
  1743. try
  1744. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1745. finally
  1746. Intf.Free;
  1747. Impl.Free;
  1748. end;
  1749. end;
  1750. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1751. SystemUnitParts: TSystemUnitParts);
  1752. begin
  1753. if NeedSystemUnit then
  1754. AddSystemUnit(SystemUnitParts)
  1755. else
  1756. Parser.ImplicitUses.Clear;
  1757. Add('program '+ExtractFileUnitName(Filename)+';');
  1758. Add('');
  1759. end;
  1760. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1761. SystemUnitParts: TSystemUnitParts);
  1762. begin
  1763. if NeedSystemUnit then
  1764. AddSystemUnit(SystemUnitParts)
  1765. else
  1766. Parser.ImplicitUses.Clear;
  1767. Add('unit Test1;');
  1768. Add('');
  1769. end;
  1770. procedure TCustomTestModule.ConvertModule;
  1771. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1772. out UsesLit: TJSArrayLiteral);
  1773. var
  1774. i: Integer;
  1775. Item: TJSElement;
  1776. Lit: TJSLiteral;
  1777. begin
  1778. UsesLit:=nil;
  1779. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1780. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1781. exit; // null is ok
  1782. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1783. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1784. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1785. begin
  1786. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1787. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1788. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1789. Lit:=TJSLiteral(Item);
  1790. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1791. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1792. end;
  1793. end;
  1794. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1795. out Src: TJSSourceElements);
  1796. var
  1797. FunDecl: TJSFunctionDeclarationStatement;
  1798. FunDef: TJSFuncDef;
  1799. FunBody: TJSFunctionBody;
  1800. begin
  1801. Src:=nil;
  1802. AssertNotNull(ParamName,Arg.Expr);
  1803. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1804. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1805. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1806. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1807. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1808. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1809. AssertNotNull(ParamName+' body',FunDef.Body);
  1810. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1811. FunBody:=FunDef.Body as TJSFunctionBody;
  1812. AssertNotNull(ParamName+' body.A',FunBody.A);
  1813. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1814. Src:=FunBody.A as TJSSourceElements;
  1815. end;
  1816. var
  1817. ModuleNameExpr: TJSLiteral;
  1818. InitFunction: TJSFunctionDeclarationStatement;
  1819. InitAssign: TJSSimpleAssignStatement;
  1820. InitName: String;
  1821. LastNode: TJSElement;
  1822. Arg: TJSArrayLiteralElement;
  1823. begin
  1824. if SkipTests then exit;
  1825. try
  1826. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1827. except
  1828. on E: Exception do
  1829. HandleException(E);
  1830. end;
  1831. if SkipTests then exit;
  1832. if ExpectedErrorClass<>nil then
  1833. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1834. FJSSource:=TStringList.Create;
  1835. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1836. {$IFDEF VerbosePas2JS}
  1837. writeln('TTestModule.ConvertModule JS:');
  1838. write(FJSSource.Text);
  1839. {$ENDIF}
  1840. // rtl.module(...
  1841. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1842. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1843. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1844. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1845. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1846. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1847. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1848. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1849. // parameter 'unitname'
  1850. if JSModuleCallArgs.Elements.Count<1 then
  1851. Fail('rtl.module first param unit missing');
  1852. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1853. AssertNotNull('module name param',Arg.Expr);
  1854. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1855. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1856. if Module is TPasProgram then
  1857. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1858. else
  1859. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1860. // main uses section
  1861. if JSModuleCallArgs.Elements.Count<2 then
  1862. Fail('rtl.module second param main uses missing');
  1863. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1864. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1865. // program/library/interface function()
  1866. if JSModuleCallArgs.Elements.Count<3 then
  1867. Fail('rtl.module third param intf-function missing');
  1868. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1869. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1870. // search for $mod.$init or $mod.$main - the last statement
  1871. if Module is TPasProgram then
  1872. begin
  1873. InitName:='$main';
  1874. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1875. end
  1876. else
  1877. InitName:='$init';
  1878. FJSInitBody:=nil;
  1879. if JSModuleSrc.Statements.Count>0 then
  1880. begin
  1881. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1882. if LastNode is TJSSimpleAssignStatement then
  1883. begin
  1884. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1885. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1886. begin
  1887. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1888. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1889. end
  1890. else if Module is TPasProgram then
  1891. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1892. end;
  1893. end;
  1894. // optional: implementation uses section
  1895. if JSModuleCallArgs.Elements.Count<4 then
  1896. exit;
  1897. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1898. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1899. end;
  1900. procedure TCustomTestModule.ConvertProgram;
  1901. begin
  1902. Add('end.');
  1903. ParseProgram;
  1904. ConvertModule;
  1905. end;
  1906. procedure TCustomTestModule.ConvertUnit;
  1907. begin
  1908. Add('end.');
  1909. ParseUnit;
  1910. ConvertModule;
  1911. end;
  1912. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1913. begin
  1914. Result:=tcmodules.JSToStr(El);
  1915. end;
  1916. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1917. DottedName: string);
  1918. begin
  1919. if DottedName='' then
  1920. begin
  1921. AssertNull(Msg,El);
  1922. end
  1923. else
  1924. begin
  1925. AssertNotNull(Msg,El);
  1926. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1927. end;
  1928. end;
  1929. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1930. begin
  1931. if El=nil then
  1932. Result:=''
  1933. else if El is TJSPrimaryExpressionIdent then
  1934. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1935. else if El is TJSDotMemberExpression then
  1936. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1937. else
  1938. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1939. end;
  1940. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1941. InitStatements: string; ImplStatements: string);
  1942. var
  1943. ActualSrc, ExpectedSrc, InitName: String;
  1944. begin
  1945. ActualSrc:=JSToStr(JSModuleSrc);
  1946. if coUseStrict in Converter.Options then
  1947. ExpectedSrc:='"use strict";'+LineEnding
  1948. else
  1949. ExpectedSrc:='';
  1950. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1951. ExpectedSrc:=ExpectedSrc+Statements;
  1952. // unit implementation
  1953. if (Trim(ImplStatements)<>'') then
  1954. ExpectedSrc:=ExpectedSrc+LineEnding
  1955. +'$mod.$implcode = function () {'+LineEnding
  1956. +ImplStatements
  1957. +'};'+LineEnding;
  1958. // program main or unit initialization
  1959. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1960. begin
  1961. if Module is TPasProgram then
  1962. InitName:='$main'
  1963. else
  1964. InitName:='$init';
  1965. ExpectedSrc:=ExpectedSrc+LineEnding
  1966. +'$mod.'+InitName+' = function () {'+LineEnding
  1967. +InitStatements
  1968. +'};'+LineEnding;
  1969. end;
  1970. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1971. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1972. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1973. end;
  1974. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1975. // search diff, ignore changes in spaces
  1976. var
  1977. s: string;
  1978. begin
  1979. if CheckSrcDiff(Expected,Actual,s) then exit;
  1980. Fail(Msg+': '+s);
  1981. end;
  1982. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1983. var
  1984. aResolver: TTestEnginePasResolver;
  1985. aConverter: TPasToJSConverter;
  1986. aJSModule: TJSSourceElements;
  1987. ActualSrc: String;
  1988. begin
  1989. aResolver:=GetResolver(Filename);
  1990. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1991. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1992. {$IFDEF VerbosePas2JS}
  1993. writeln('CheckUnit '+Filename+' converting ...');
  1994. {$ENDIF}
  1995. aConverter:=CreateConverter;
  1996. aJSModule:=nil;
  1997. try
  1998. try
  1999. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2000. except
  2001. on E: Exception do
  2002. HandleException(E);
  2003. end;
  2004. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2005. {$IFDEF VerbosePas2JS}
  2006. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2007. write(aResolver.Source);
  2008. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2009. write(ActualSrc);
  2010. {$ENDIF}
  2011. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2012. finally
  2013. aJSModule.Free;
  2014. aConverter.Free;
  2015. end;
  2016. end;
  2017. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2018. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2019. var
  2020. i: Integer;
  2021. Item: TTestHintMessage;
  2022. Expected,Actual: string;
  2023. begin
  2024. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2025. for i:=0 to MsgCount-1 do
  2026. begin
  2027. Item:=Msgs[i];
  2028. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2029. if (Marker<>nil) then
  2030. begin
  2031. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2032. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2033. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2034. end;
  2035. // found
  2036. FHintMsgsGood.Add(Item);
  2037. str(Item.MsgType,Actual);
  2038. str(MsgType,Expected);
  2039. AssertEquals('MsgType',Expected,Actual);
  2040. exit;
  2041. end;
  2042. // needed message missing -> show emitted messages
  2043. WriteSources('',0,0);
  2044. for i:=0 to MsgCount-1 do
  2045. begin
  2046. Item:=Msgs[i];
  2047. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2048. ' ('+IntToStr(Item.MsgNumber),')');
  2049. if Marker<>nil then
  2050. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2051. writeln(' {',Item.Msg,'}');
  2052. end;
  2053. str(MsgType,Expected);
  2054. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2055. if Marker<>nil then
  2056. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2057. Actual:=Actual+' '+Msg;
  2058. Fail(Actual);
  2059. end;
  2060. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2061. );
  2062. var
  2063. i: Integer;
  2064. s, Txt: String;
  2065. Msg: TTestHintMessage;
  2066. begin
  2067. for i:=0 to MsgCount-1 do
  2068. begin
  2069. Msg:=Msgs[i];
  2070. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2071. s:='';
  2072. str(Msg.MsgType,s);
  2073. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2074. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2075. if WithSourcePos then
  2076. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2077. Txt:=Txt+' {'+Msg.Msg+'}';
  2078. Fail(Txt);
  2079. end;
  2080. end;
  2081. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2082. MsgNumber: integer);
  2083. begin
  2084. ExpectedErrorClass:=EScannerError;
  2085. ExpectedErrorMsg:=Msg;
  2086. ExpectedErrorNumber:=MsgNumber;
  2087. end;
  2088. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2089. MsgNumber: integer);
  2090. begin
  2091. ExpectedErrorClass:=EParserError;
  2092. ExpectedErrorMsg:=Msg;
  2093. ExpectedErrorNumber:=MsgNumber;
  2094. end;
  2095. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2096. MsgNumber: integer);
  2097. begin
  2098. ExpectedErrorClass:=EPasResolve;
  2099. ExpectedErrorMsg:=Msg;
  2100. ExpectedErrorNumber:=MsgNumber;
  2101. end;
  2102. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2103. MsgNumber: integer);
  2104. begin
  2105. ExpectedErrorClass:=EPas2JS;
  2106. ExpectedErrorMsg:=Msg;
  2107. ExpectedErrorNumber:=MsgNumber;
  2108. end;
  2109. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2110. var
  2111. MsgNumber: Integer;
  2112. Msg: String;
  2113. begin
  2114. Result:=false;
  2115. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2116. Msg:=E.Message;
  2117. if E is EPas2JS then
  2118. MsgNumber:=EPas2JS(E).MsgNumber
  2119. else if E is EPasResolve then
  2120. MsgNumber:=EPasResolve(E).MsgNumber
  2121. else if E is EParserError then
  2122. MsgNumber:=Parser.LastMsgNumber
  2123. else if E is EScannerError then
  2124. begin
  2125. MsgNumber:=Scanner.LastMsgNumber;
  2126. Msg:=Scanner.LastMsg;
  2127. end
  2128. else
  2129. MsgNumber:=0;
  2130. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2131. if Result then
  2132. SkipTests:=true;
  2133. end;
  2134. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2135. begin
  2136. if IsErrorExpected(E) then exit;
  2137. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2138. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2139. +' '+Scanner.CurFilename
  2140. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2141. FailException(E);
  2142. end;
  2143. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2144. begin
  2145. if IsErrorExpected(E) then exit;
  2146. WriteSources(E.Filename,E.Row,E.Column);
  2147. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2148. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2149. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2150. );
  2151. FailException(E);
  2152. end;
  2153. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2154. var
  2155. P: TPasSourcePos;
  2156. begin
  2157. if IsErrorExpected(E) then exit;
  2158. P:=E.SourcePos;
  2159. WriteSources(P.FileName,P.Row,P.Column);
  2160. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2161. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2162. FailException(E);
  2163. end;
  2164. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2165. var
  2166. Row, Col: integer;
  2167. begin
  2168. if IsErrorExpected(E) then exit;
  2169. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2170. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2171. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2172. +' '+E.PasElement.SourceFilename
  2173. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2174. FailException(E);
  2175. end;
  2176. procedure TCustomTestModule.HandleException(E: Exception);
  2177. begin
  2178. if E is EScannerError then
  2179. HandleScannerError(EScannerError(E))
  2180. else if E is EParserError then
  2181. HandleParserError(EParserError(E))
  2182. else if E is EPasResolve then
  2183. HandlePasResolveError(EPasResolve(E))
  2184. else if E is EPas2JS then
  2185. HandlePas2JSError(EPas2JS(E))
  2186. else
  2187. begin
  2188. if IsErrorExpected(E) then exit;
  2189. if not (E is EAssertionFailedError) then
  2190. begin
  2191. WriteSources('',0,0);
  2192. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2193. end;
  2194. FailException(E);
  2195. end;
  2196. end;
  2197. procedure TCustomTestModule.FailException(E: Exception);
  2198. var
  2199. MsgNumber: Integer;
  2200. begin
  2201. if ExpectedErrorClass<>nil then
  2202. begin
  2203. if FExpectedErrorClass=E.ClassType then
  2204. begin
  2205. if E is EPas2JS then
  2206. MsgNumber:=EPas2JS(E).MsgNumber
  2207. else if E is EPasResolve then
  2208. MsgNumber:=EPasResolve(E).MsgNumber
  2209. else if E is EParserError then
  2210. MsgNumber:=Parser.LastMsgNumber
  2211. else if E is EScannerError then
  2212. MsgNumber:=Scanner.LastMsgNumber
  2213. else
  2214. MsgNumber:=0;
  2215. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2216. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2217. ExpectedErrorNumber,MsgNumber);
  2218. end else begin
  2219. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2220. end;
  2221. end;
  2222. Fail(E.Message);
  2223. end;
  2224. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2225. aCol: integer);
  2226. var
  2227. IsSrc: Boolean;
  2228. i, j: Integer;
  2229. SrcLines: TStringList;
  2230. Line: string;
  2231. aModule: TTestEnginePasResolver;
  2232. begin
  2233. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2234. for i:=0 to ResolverCount-1 do
  2235. begin
  2236. aModule:=Resolvers[i];
  2237. SrcLines:=TStringList.Create;
  2238. try
  2239. SrcLines.Text:=aModule.Source;
  2240. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2241. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2242. for j:=1 to SrcLines.Count do
  2243. begin
  2244. Line:=SrcLines[j-1];
  2245. if IsSrc and (j=aRow) then
  2246. begin
  2247. write('*');
  2248. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2249. end;
  2250. writeln(Format('%:4d: ',[j]),Line);
  2251. end;
  2252. finally
  2253. SrcLines.Free;
  2254. end;
  2255. end;
  2256. end;
  2257. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2258. var
  2259. i: Integer;
  2260. begin
  2261. for i:=0 to ResolverCount-1 do
  2262. if Filename=Resolvers[i].Filename then exit(i);
  2263. Result:=-1;
  2264. end;
  2265. function TCustomTestModule.GetResolver(const Filename: string
  2266. ): TTestEnginePasResolver;
  2267. var
  2268. i: Integer;
  2269. begin
  2270. i:=IndexOfResolver(Filename);
  2271. if i<0 then exit(nil);
  2272. Result:=Resolvers[i];
  2273. end;
  2274. function TCustomTestModule.GetDefaultNamespace: string;
  2275. var
  2276. C: TClass;
  2277. begin
  2278. Result:='';
  2279. if FModule=nil then exit;
  2280. C:=FModule.ClassType;
  2281. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2282. Result:=Engine.DefaultNameSpace;
  2283. end;
  2284. constructor TCustomTestModule.Create;
  2285. begin
  2286. inherited Create;
  2287. FHintMsgs:=TObjectList.Create(true);
  2288. FHintMsgsGood:=TFPList.Create;
  2289. end;
  2290. destructor TCustomTestModule.Destroy;
  2291. begin
  2292. FreeAndNil(FHintMsgs);
  2293. FreeAndNil(FHintMsgsGood);
  2294. inherited Destroy;
  2295. end;
  2296. { TTestModule }
  2297. procedure TTestModule.TestReservedWords;
  2298. var
  2299. i: integer;
  2300. begin
  2301. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2302. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2303. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2304. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2305. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2306. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2307. end;
  2308. procedure TTestModule.TestEmptyProgram;
  2309. begin
  2310. StartProgram(false);
  2311. Add('begin');
  2312. ConvertProgram;
  2313. CheckSource('TestEmptyProgram','','');
  2314. end;
  2315. procedure TTestModule.TestEmptyProgramUseStrict;
  2316. begin
  2317. Converter.Options:=Converter.Options+[coUseStrict];
  2318. StartProgram(false);
  2319. Add('begin');
  2320. ConvertProgram;
  2321. CheckSource('TestEmptyProgramUseStrict','','');
  2322. end;
  2323. procedure TTestModule.TestEmptyUnit;
  2324. begin
  2325. StartUnit(false);
  2326. Add('interface');
  2327. Add('implementation');
  2328. ConvertUnit;
  2329. CheckSource('TestEmptyUnit',
  2330. LinesToStr([
  2331. ]),
  2332. '');
  2333. end;
  2334. procedure TTestModule.TestEmptyUnitUseStrict;
  2335. begin
  2336. Converter.Options:=Converter.Options+[coUseStrict];
  2337. StartUnit(false);
  2338. Add('interface');
  2339. Add('implementation');
  2340. ConvertUnit;
  2341. CheckSource('TestEmptyUnitUseStrict',
  2342. LinesToStr([
  2343. ''
  2344. ]),
  2345. '');
  2346. end;
  2347. procedure TTestModule.TestDottedUnitNames;
  2348. begin
  2349. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2350. LinesToStr([
  2351. 'var iV: longint;'
  2352. ]),
  2353. '');
  2354. FFilename:='ns1.test1.pp';
  2355. StartProgram(true);
  2356. Add('uses unIt2;');
  2357. Add('var');
  2358. Add(' i: longint;');
  2359. Add('begin');
  2360. Add(' i:=iv;');
  2361. Add(' i:=uNit2.iv;');
  2362. Add(' i:=Ns1.TEst1.i;');
  2363. ConvertProgram;
  2364. CheckSource('TestDottedUnitNames',
  2365. LinesToStr([
  2366. 'this.i = 0;',
  2367. '']),
  2368. LinesToStr([ // this.$init
  2369. '$mod.i = pas["NS1.Unit2"].iV;',
  2370. '$mod.i = pas["NS1.Unit2"].iV;',
  2371. '$mod.i = $mod.i;',
  2372. '']) );
  2373. end;
  2374. procedure TTestModule.TestDottedUnitNameImpl;
  2375. begin
  2376. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2377. LinesToStr([
  2378. 'type',
  2379. ' TObject = class end;',
  2380. ' TTestA = class',
  2381. ' end;'
  2382. ]),
  2383. LinesToStr(['uses TEST.UnitB;'])
  2384. );
  2385. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2386. LinesToStr([
  2387. 'uses TEST.UnitA;',
  2388. 'type TTestB = class(TTestA);'
  2389. ]),
  2390. ''
  2391. );
  2392. StartProgram(true);
  2393. Add('uses TEST.UnitA;');
  2394. Add('begin');
  2395. ConvertProgram;
  2396. CheckSource('TestDottedUnitNameImpl',
  2397. LinesToStr([
  2398. '']),
  2399. LinesToStr([ // this.$init
  2400. '']) );
  2401. CheckUnit('TEST.UnitA.pas',
  2402. LinesToStr([
  2403. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2404. ' var $mod = this;',
  2405. ' rtl.createClass(this, "TObject", null, function () {',
  2406. ' this.$init = function () {',
  2407. ' };',
  2408. ' this.$final = function () {',
  2409. ' };',
  2410. ' });',
  2411. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2412. ' });',
  2413. '}, ["TEST.UnitB"]);'
  2414. ]));
  2415. CheckUnit('TEST.UnitB.pas',
  2416. LinesToStr([
  2417. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2418. ' var $mod = this;',
  2419. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2420. ' });',
  2421. '});'
  2422. ]));
  2423. end;
  2424. procedure TTestModule.TestDottedUnitExpr;
  2425. begin
  2426. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2427. LinesToStr([
  2428. 'procedure DoIt;'
  2429. ]),
  2430. 'procedure DoIt; begin end;');
  2431. FFilename:='Ns1.SubNs1.Test1.pp';
  2432. StartProgram(true);
  2433. Add('uses Ns2.sUbnS2.unIt2;');
  2434. Add('var');
  2435. Add(' i: longint;');
  2436. Add('begin');
  2437. Add(' ns2.subns2.unit2.doit;');
  2438. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2439. ConvertProgram;
  2440. CheckSource('TestDottedUnitExpr',
  2441. LinesToStr([
  2442. 'this.i = 0;',
  2443. '']),
  2444. LinesToStr([ // this.$init
  2445. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2446. '$mod.i = $mod.i;',
  2447. '']) );
  2448. end;
  2449. procedure TTestModule.Test_ModeFPCFail;
  2450. begin
  2451. StartProgram(false);
  2452. Add('{$mode FPC}');
  2453. Add('begin');
  2454. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2455. ConvertProgram;
  2456. end;
  2457. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2458. begin
  2459. StartProgram(false);
  2460. Add('{$modeswitch cblocks-}');
  2461. Add('begin');
  2462. ConvertProgram;
  2463. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2464. CheckResolverUnexpectedHints();
  2465. end;
  2466. procedure TTestModule.TestUnit_UseSystem;
  2467. begin
  2468. StartUnit(true);
  2469. Add([
  2470. 'interface',
  2471. 'var i: integer;',
  2472. 'implementation']);
  2473. ConvertUnit;
  2474. CheckSource('TestUnit_UseSystem',
  2475. LinesToStr([
  2476. 'this.i = 0;',
  2477. '']),
  2478. LinesToStr([
  2479. '']) );
  2480. end;
  2481. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2482. begin
  2483. AddModuleWithIntfImplSrc('unit1.pp',
  2484. LinesToStr([
  2485. 'type number = longint;']),
  2486. LinesToStr([
  2487. 'uses test1;',
  2488. 'procedure DoIt;',
  2489. 'begin',
  2490. ' i:=3;',
  2491. 'end;']));
  2492. StartUnit(true);
  2493. Add([
  2494. 'interface',
  2495. 'uses unit1;',
  2496. 'var i: number;',
  2497. 'implementation']);
  2498. ConvertUnit;
  2499. CheckSource('TestUnit_Intf1Impl2Intf1',
  2500. LinesToStr([
  2501. 'this.i = 0;',
  2502. '']),
  2503. LinesToStr([
  2504. '']) );
  2505. end;
  2506. procedure TTestModule.TestIncludeVersion;
  2507. begin
  2508. StartProgram(false);
  2509. Add([
  2510. 'var',
  2511. ' s: string;',
  2512. ' i: word;',
  2513. 'begin',
  2514. ' s:={$I %line%};',
  2515. ' i:={$I %linenum%};',
  2516. ' s:={$I %currentroutine%};',
  2517. ' s:={$I %pas2jsversion%};',
  2518. ' s:={$I %pas2jstarget%};',
  2519. ' s:={$I %pas2jstargetos%};',
  2520. ' s:={$I %pas2jstargetcpu%};',
  2521. ' s:={$I %file%};',
  2522. '']);
  2523. ConvertProgram;
  2524. CheckSource('TestIncludeVersion',
  2525. LinesToStr([
  2526. 'this.s="";',
  2527. 'this.i = 0;']),
  2528. LinesToStr([
  2529. '$mod.s = "7";',
  2530. '$mod.i = 8;',
  2531. '$mod.s = "<anonymous>";',
  2532. '$mod.s = "Comp.Ver.tcmodules";',
  2533. '$mod.s = "Browser";',
  2534. '$mod.s = "Browser";',
  2535. '$mod.s = "ECMAScript5";',
  2536. '$mod.s = "test1.pp";',
  2537. '']));
  2538. end;
  2539. procedure TTestModule.TestVarInt;
  2540. begin
  2541. StartProgram(false);
  2542. Add('var MyI: longint;');
  2543. Add('begin');
  2544. ConvertProgram;
  2545. CheckSource('TestVarInt','this.MyI=0;','');
  2546. end;
  2547. procedure TTestModule.TestVarBaseTypes;
  2548. begin
  2549. StartProgram(false);
  2550. Add('var');
  2551. Add(' i: longint;');
  2552. Add(' s: string;');
  2553. Add(' c: char;');
  2554. Add(' b: boolean;');
  2555. Add(' d: double;');
  2556. Add(' i2: longint = 3;');
  2557. Add(' s2: string = ''foo'';');
  2558. Add(' c2: char = ''4'';');
  2559. Add(' b2: boolean = true;');
  2560. Add(' d2: double = 5.6;');
  2561. Add(' i3: longint = $707;');
  2562. Add(' i4: nativeint = 9007199254740991;');
  2563. Add(' i5: nativeint = -9007199254740991-1;');
  2564. Add(' i6: nativeint = $fffffffffffff;');
  2565. Add(' i7: nativeint = -$fffffffffffff-1;');
  2566. Add(' i8: byte = 00;');
  2567. Add(' u8: nativeuint = $fffffffffffff;');
  2568. Add(' u9: nativeuint = $0000000000000;');
  2569. Add(' u10: nativeuint = $00ff00;');
  2570. Add('begin');
  2571. ConvertProgram;
  2572. CheckSource('TestVarBaseTypes',
  2573. LinesToStr([
  2574. 'this.i = 0;',
  2575. 'this.s = "";',
  2576. 'this.c = "";',
  2577. 'this.b = false;',
  2578. 'this.d = 0.0;',
  2579. 'this.i2 = 3;',
  2580. 'this.s2 = "foo";',
  2581. 'this.c2 = "4";',
  2582. 'this.b2 = true;',
  2583. 'this.d2 = 5.6;',
  2584. 'this.i3 = 0x707;',
  2585. 'this.i4 = 9007199254740991;',
  2586. 'this.i5 = -9007199254740991-1;',
  2587. 'this.i6 = 0xfffffffffffff;',
  2588. 'this.i7 =-0xfffffffffffff-1;',
  2589. 'this.i8 = 0;',
  2590. 'this.u8 = 0xfffffffffffff;',
  2591. 'this.u9 = 0x0;',
  2592. 'this.u10 = 0xff00;'
  2593. ]),
  2594. '');
  2595. end;
  2596. procedure TTestModule.TestBaseTypeSingleFail;
  2597. begin
  2598. StartProgram(false);
  2599. Add('var s: single;');
  2600. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2601. ConvertProgram;
  2602. end;
  2603. procedure TTestModule.TestBaseTypeExtendedFail;
  2604. begin
  2605. StartProgram(false);
  2606. Add('var e: extended;');
  2607. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2608. ConvertProgram;
  2609. end;
  2610. procedure TTestModule.TestConstBaseTypes;
  2611. begin
  2612. StartProgram(false);
  2613. Add('const');
  2614. Add(' i: longint = 3;');
  2615. Add(' s: string = ''foo'';');
  2616. Add(' c: char = ''4'';');
  2617. Add(' b: boolean = true;');
  2618. Add(' d: double = 5.6;');
  2619. Add(' e = low(word);');
  2620. Add(' f = high(word);');
  2621. Add('begin');
  2622. ConvertProgram;
  2623. CheckSource('TestVarBaseTypes',
  2624. LinesToStr([
  2625. 'this.i=3;',
  2626. 'this.s="foo";',
  2627. 'this.c="4";',
  2628. 'this.b=true;',
  2629. 'this.d=5.6;',
  2630. 'this.e = 0;',
  2631. 'this.f = 65535;'
  2632. ]),
  2633. '');
  2634. end;
  2635. procedure TTestModule.TestAliasTypeRef;
  2636. begin
  2637. StartProgram(false);
  2638. Add('type');
  2639. Add(' a=longint;');
  2640. Add(' b=a;');
  2641. Add('var');
  2642. Add(' c: A;');
  2643. Add(' d: B;');
  2644. Add('begin');
  2645. ConvertProgram;
  2646. CheckSource('TestAliasTypeRef',
  2647. LinesToStr([ // statements
  2648. 'this.c = 0;',
  2649. 'this.d = 0;'
  2650. ]),
  2651. LinesToStr([ // this.$main
  2652. ''
  2653. ]));
  2654. end;
  2655. procedure TTestModule.TestTypeCast_BaseTypes;
  2656. begin
  2657. StartProgram(false);
  2658. Add([
  2659. 'var',
  2660. ' i: longint;',
  2661. ' b: boolean;',
  2662. ' d: double;',
  2663. ' s: string;',
  2664. ' c: char;',
  2665. 'begin',
  2666. ' i:=longint(i);',
  2667. ' i:=longint(b);',
  2668. ' b:=boolean(b);',
  2669. ' b:=boolean(i);',
  2670. ' d:=double(d);',
  2671. ' d:=double(i);',
  2672. ' s:=string(s);',
  2673. ' s:=string(c);',
  2674. ' c:=char(c);',
  2675. ' c:=char(i);',
  2676. ' c:=char(65);',
  2677. ' c:=char(#10);',
  2678. ' c:=char(#$E000);',
  2679. '']);
  2680. ConvertProgram;
  2681. CheckSource('TestAliasTypeRef',
  2682. LinesToStr([ // statements
  2683. 'this.i = 0;',
  2684. 'this.b = false;',
  2685. 'this.d = 0.0;',
  2686. 'this.s = "";',
  2687. 'this.c = "";',
  2688. '']),
  2689. LinesToStr([ // this.$main
  2690. '$mod.i = $mod.i;',
  2691. '$mod.i = ($mod.b ? 1 : 0);',
  2692. '$mod.b = $mod.b;',
  2693. '$mod.b = $mod.i != 0;',
  2694. '$mod.d = $mod.d;',
  2695. '$mod.d = $mod.i;',
  2696. '$mod.s = $mod.s;',
  2697. '$mod.s = $mod.c;',
  2698. '$mod.c = $mod.c;',
  2699. '$mod.c = String.fromCharCode($mod.i);',
  2700. '$mod.c = "A";',
  2701. '$mod.c = "\n";',
  2702. '$mod.c = "";',
  2703. '']));
  2704. end;
  2705. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2706. begin
  2707. StartProgram(false);
  2708. Add('type');
  2709. Add(' integer = longint;');
  2710. Add(' TYesNo = boolean;');
  2711. Add(' TFloat = double;');
  2712. Add(' TCaption = string;');
  2713. Add(' TChar = char;');
  2714. Add('var');
  2715. Add(' i: integer;');
  2716. Add(' b: TYesNo;');
  2717. Add(' d: TFloat;');
  2718. Add(' s: TCaption;');
  2719. Add(' c: TChar;');
  2720. Add('begin');
  2721. Add(' i:=integer(i);');
  2722. Add(' i:=integer(b);');
  2723. Add(' b:=TYesNo(b);');
  2724. Add(' b:=TYesNo(i);');
  2725. Add(' d:=TFloat(d);');
  2726. Add(' d:=TFloat(i);');
  2727. Add(' s:=TCaption(s);');
  2728. Add(' s:=TCaption(c);');
  2729. Add(' c:=TChar(c);');
  2730. ConvertProgram;
  2731. CheckSource('TestAliasTypeRef',
  2732. LinesToStr([ // statements
  2733. 'this.i = 0;',
  2734. 'this.b = false;',
  2735. 'this.d = 0.0;',
  2736. 'this.s = "";',
  2737. 'this.c = "";',
  2738. '']),
  2739. LinesToStr([ // this.$main
  2740. '$mod.i = $mod.i;',
  2741. '$mod.i = ($mod.b ? 1 : 0);',
  2742. '$mod.b = $mod.b;',
  2743. '$mod.b = $mod.i != 0;',
  2744. '$mod.d = $mod.d;',
  2745. '$mod.d = $mod.i;',
  2746. '$mod.s = $mod.s;',
  2747. '$mod.s = $mod.c;',
  2748. '$mod.c = $mod.c;',
  2749. '']));
  2750. end;
  2751. procedure TTestModule.TestEmptyProc;
  2752. begin
  2753. StartProgram(false);
  2754. Add('procedure Test;');
  2755. Add('begin');
  2756. Add('end;');
  2757. Add('begin');
  2758. ConvertProgram;
  2759. CheckSource('TestEmptyProc',
  2760. LinesToStr([ // statements
  2761. 'this.Test = function () {',
  2762. '};'
  2763. ]),
  2764. LinesToStr([ // this.$main
  2765. ''
  2766. ]));
  2767. end;
  2768. procedure TTestModule.TestProcOneParam;
  2769. begin
  2770. StartProgram(false);
  2771. Add('procedure ProcA(i: longint);');
  2772. Add('begin');
  2773. Add('end;');
  2774. Add('begin');
  2775. Add(' PROCA(3);');
  2776. ConvertProgram;
  2777. CheckSource('TestProcOneParam',
  2778. LinesToStr([ // statements
  2779. 'this.ProcA = function (i) {',
  2780. '};'
  2781. ]),
  2782. LinesToStr([ // this.$main
  2783. '$mod.ProcA(3);'
  2784. ]));
  2785. end;
  2786. procedure TTestModule.TestFunctionWithoutParams;
  2787. begin
  2788. StartProgram(false);
  2789. Add('function FuncA: longint;');
  2790. Add('begin');
  2791. Add('end;');
  2792. Add('var i: longint;');
  2793. Add('begin');
  2794. Add(' I:=FUNCA();');
  2795. Add(' I:=FUNCA;');
  2796. Add(' FUNCA();');
  2797. Add(' FUNCA;');
  2798. ConvertProgram;
  2799. CheckSource('TestProcWithoutParams',
  2800. LinesToStr([ // statements
  2801. 'this.FuncA = function () {',
  2802. ' var Result = 0;',
  2803. ' return Result;',
  2804. '};',
  2805. 'this.i=0;'
  2806. ]),
  2807. LinesToStr([ // this.$main
  2808. '$mod.i=$mod.FuncA();',
  2809. '$mod.i=$mod.FuncA();',
  2810. '$mod.FuncA();',
  2811. '$mod.FuncA();'
  2812. ]));
  2813. end;
  2814. procedure TTestModule.TestProcedureWithoutParams;
  2815. begin
  2816. StartProgram(false);
  2817. Add('procedure ProcA;');
  2818. Add('begin');
  2819. Add('end;');
  2820. Add('begin');
  2821. Add(' PROCA();');
  2822. Add(' PROCA;');
  2823. ConvertProgram;
  2824. CheckSource('TestProcWithoutParams',
  2825. LinesToStr([ // statements
  2826. 'this.ProcA = function () {',
  2827. '};'
  2828. ]),
  2829. LinesToStr([ // this.$main
  2830. '$mod.ProcA();',
  2831. '$mod.ProcA();'
  2832. ]));
  2833. end;
  2834. procedure TTestModule.TestIncDec;
  2835. begin
  2836. StartProgram(false);
  2837. Add([
  2838. 'procedure DoIt(var i: longint);',
  2839. 'begin',
  2840. ' inc(i);',
  2841. ' inc(i,2);',
  2842. 'end;',
  2843. 'var',
  2844. ' Bar: longint;',
  2845. 'begin',
  2846. ' inc(bar);',
  2847. ' inc(bar,2);',
  2848. ' dec(bar);',
  2849. ' dec(bar,3);',
  2850. '']);
  2851. ConvertProgram;
  2852. CheckSource('TestIncDec',
  2853. LinesToStr([ // statements
  2854. 'this.DoIt = function (i) {',
  2855. ' i.set(i.get()+1);',
  2856. ' i.set(i.get()+2);',
  2857. '};',
  2858. 'this.Bar = 0;'
  2859. ]),
  2860. LinesToStr([ // this.$main
  2861. '$mod.Bar+=1;',
  2862. '$mod.Bar+=2;',
  2863. '$mod.Bar-=1;',
  2864. '$mod.Bar-=3;'
  2865. ]));
  2866. end;
  2867. procedure TTestModule.TestLoHiFpcMode;
  2868. begin
  2869. StartProgram(false);
  2870. Add([
  2871. '{$mode objfpc}',
  2872. 'const',
  2873. ' LoByte1 = Lo(Word($1234));',
  2874. ' HiByte1 = Hi(Word($1234));',
  2875. ' LoByte2 = Lo(SmallInt($1234));',
  2876. ' HiByte2 = Hi(SmallInt($1234));',
  2877. ' LoWord1 = Lo($1234CDEF);',
  2878. ' HiWord1 = Hi($1234CDEF);',
  2879. ' LoWord2 = Lo(-$1234CDEF);',
  2880. ' HiWord2 = Hi(-$1234CDEF);',
  2881. ' lo4:byte=lo(byte($34));',
  2882. ' hi4:byte=hi(byte($34));',
  2883. ' lo5:byte=lo(shortint(-$34));',
  2884. ' hi5:byte=hi(shortint(-$34));',
  2885. ' lo6:longword=lo($123456789ABCD);',
  2886. ' hi6:longword=hi($123456789ABCD);',
  2887. ' lo7:longword=lo(-$123456789ABCD);',
  2888. ' hi7:longword=hi(-$123456789ABCD);',
  2889. 'var',
  2890. ' b: Byte;',
  2891. ' ss: shortint;',
  2892. ' w: Word;',
  2893. ' si: SmallInt;',
  2894. ' lw: LongWord;',
  2895. ' li: LongInt;',
  2896. ' b2: Byte;',
  2897. ' ni: nativeint;',
  2898. 'begin',
  2899. ' w := $1234;',
  2900. ' ss := -$12;',
  2901. ' b := lo(ss);',
  2902. ' b := HI(ss);',
  2903. ' b := lo(w);',
  2904. ' b := HI(w);',
  2905. ' b2 := lo(b);',
  2906. ' b2 := hi(b);',
  2907. ' lw := $1234CDEF;',
  2908. ' w := lo(lw);',
  2909. ' w := hi(lw);',
  2910. ' ni := $123456789ABCD;',
  2911. ' lw := lo(ni);',
  2912. ' lw := hi(ni);',
  2913. '']);
  2914. ConvertProgram;
  2915. CheckSource('TestLoHiFpcMode',
  2916. LinesToStr([ // statements
  2917. 'this.LoByte1 = 0x1234 & 0xFF;',
  2918. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2919. 'this.LoByte2 = 0x1234 & 0xFF;',
  2920. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2921. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2922. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2923. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2924. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2925. 'this.lo4 = 0x34 & 0xF;',
  2926. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2927. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2928. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2929. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2930. 'this.hi6 = 74565 >>> 0;',
  2931. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2932. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2933. 'this.b = 0;',
  2934. 'this.ss = 0;',
  2935. 'this.w = 0;',
  2936. 'this.si = 0;',
  2937. 'this.lw = 0;',
  2938. 'this.li = 0;',
  2939. 'this.b2 = 0;',
  2940. 'this.ni = 0;',
  2941. '']),
  2942. LinesToStr([ // this.$main
  2943. '$mod.w = 0x1234;',
  2944. '$mod.ss = -0x12;',
  2945. '$mod.b = $mod.ss & 0xFF;',
  2946. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2947. '$mod.b = $mod.w & 0xFF;',
  2948. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2949. '$mod.b2 = $mod.b & 0xF;',
  2950. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2951. '$mod.lw = 0x1234CDEF;',
  2952. '$mod.w = $mod.lw & 0xFFFF;',
  2953. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2954. '$mod.ni = 0x123456789ABCD;',
  2955. '$mod.lw = $mod.ni >>> 0;',
  2956. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2957. '']));
  2958. end;
  2959. procedure TTestModule.TestLoHiDelphiMode;
  2960. begin
  2961. StartProgram(false);
  2962. Add([
  2963. '{$mode delphi}',
  2964. 'const',
  2965. ' LoByte1 = Lo(Word($1234));',
  2966. ' HiByte1 = Hi(Word($1234));',
  2967. ' LoByte2 = Lo(SmallInt($1234));',
  2968. ' HiByte2 = Hi(SmallInt($1234));',
  2969. ' LoByte3 = Lo($1234CDEF);',
  2970. ' HiByte3 = Hi($1234CDEF);',
  2971. ' LoByte4 = Lo(-$1234CDEF);',
  2972. ' HiByte4 = Hi(-$1234CDEF);',
  2973. 'var',
  2974. ' b: Byte;',
  2975. ' w: Word;',
  2976. ' si: SmallInt;',
  2977. ' lw: LongWord;',
  2978. ' li: LongInt;',
  2979. 'begin',
  2980. ' w := $1234;',
  2981. ' b := lo(w);',
  2982. ' b := HI(w);',
  2983. ' lw := $1234CDEF;',
  2984. ' b := lo(lw);',
  2985. ' b := hi(lw);',
  2986. '']);
  2987. ConvertProgram;
  2988. CheckSource('TestLoHiDelphiMode',
  2989. LinesToStr([ // statements
  2990. 'this.LoByte1 = 0x1234 & 0xFF;',
  2991. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2992. 'this.LoByte2 = 0x1234 & 0xFF;',
  2993. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2994. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2995. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2996. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2997. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2998. 'this.b = 0;',
  2999. 'this.w = 0;',
  3000. 'this.si = 0;',
  3001. 'this.lw = 0;',
  3002. 'this.li = 0;'
  3003. ]),
  3004. LinesToStr([ // this.$main
  3005. '$mod.w = 0x1234;',
  3006. '$mod.b = $mod.w & 0xFF;',
  3007. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3008. '$mod.lw = 0x1234CDEF;',
  3009. '$mod.b = $mod.lw & 0xFF;',
  3010. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3011. ]));
  3012. end;
  3013. procedure TTestModule.TestAssignments;
  3014. begin
  3015. StartProgram(false);
  3016. Parser.Options:=Parser.Options+[po_cassignments];
  3017. Add('var');
  3018. Add(' Bar:longint;');
  3019. Add('begin');
  3020. Add(' bar:=3;');
  3021. Add(' bar+=4;');
  3022. Add(' bar-=5;');
  3023. Add(' bar*=6;');
  3024. ConvertProgram;
  3025. CheckSource('TestAssignments',
  3026. LinesToStr([ // statements
  3027. 'this.Bar = 0;'
  3028. ]),
  3029. LinesToStr([ // this.$main
  3030. '$mod.Bar=3;',
  3031. '$mod.Bar+=4;',
  3032. '$mod.Bar-=5;',
  3033. '$mod.Bar*=6;'
  3034. ]));
  3035. end;
  3036. procedure TTestModule.TestArithmeticOperators1;
  3037. begin
  3038. StartProgram(false);
  3039. Add('var');
  3040. Add(' vA,vB,vC:longint;');
  3041. Add('begin');
  3042. Add(' va:=1;');
  3043. Add(' vb:=va+va;');
  3044. Add(' vb:=va div vb;');
  3045. Add(' vb:=va mod vb;');
  3046. Add(' vb:=va+va*vb+va div vb;');
  3047. Add(' vc:=-va;');
  3048. Add(' va:=va-vb;');
  3049. Add(' vb:=va;');
  3050. Add(' if va<vb then vc:=va else vc:=vb;');
  3051. ConvertProgram;
  3052. CheckSource('TestArithmeticOperators1',
  3053. LinesToStr([ // statements
  3054. 'this.vA = 0;',
  3055. 'this.vB = 0;',
  3056. 'this.vC = 0;'
  3057. ]),
  3058. LinesToStr([ // this.$main
  3059. '$mod.vA = 1;',
  3060. '$mod.vB = $mod.vA + $mod.vA;',
  3061. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3062. '$mod.vB = $mod.vA % $mod.vB;',
  3063. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3064. '$mod.vC = -$mod.vA;',
  3065. '$mod.vA = $mod.vA - $mod.vB;',
  3066. '$mod.vB = $mod.vA;',
  3067. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3068. ]));
  3069. end;
  3070. procedure TTestModule.TestLogicalOperators;
  3071. begin
  3072. StartProgram(false);
  3073. Add('var');
  3074. Add(' vA,vB,vC:boolean;');
  3075. Add('begin');
  3076. Add(' va:=vb and vc;');
  3077. Add(' va:=vb or vc;');
  3078. Add(' va:=vb xor vc;');
  3079. Add(' va:=true and vc;');
  3080. Add(' va:=(vb and vc) or (va and vb);');
  3081. Add(' va:=not vb;');
  3082. ConvertProgram;
  3083. CheckSource('TestLogicalOperators',
  3084. LinesToStr([ // statements
  3085. 'this.vA = false;',
  3086. 'this.vB = false;',
  3087. 'this.vC = false;'
  3088. ]),
  3089. LinesToStr([ // this.$main
  3090. '$mod.vA = $mod.vB && $mod.vC;',
  3091. '$mod.vA = $mod.vB || $mod.vC;',
  3092. '$mod.vA = $mod.vB ^ $mod.vC;',
  3093. '$mod.vA = true && $mod.vC;',
  3094. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3095. '$mod.vA = !$mod.vB;'
  3096. ]));
  3097. end;
  3098. procedure TTestModule.TestBitwiseOperators;
  3099. begin
  3100. StartProgram(false);
  3101. Add([
  3102. 'var',
  3103. ' vA,vB,vC:longint;',
  3104. ' X,Y,Z: nativeint;',
  3105. 'begin',
  3106. ' va:=vb and vc;',
  3107. ' va:=vb or vc;',
  3108. ' va:=vb xor vc;',
  3109. ' va:=vb shl vc;',
  3110. ' va:=vb shr vc;',
  3111. ' va:=3 and vc;',
  3112. ' va:=(vb and vc) or (va and vb);',
  3113. ' va:=not vb;',
  3114. ' X:=Y and Z;',
  3115. ' X:=Y and va;',
  3116. ' X:=Y or Z;',
  3117. ' X:=Y or va;',
  3118. ' X:=Y xor Z;',
  3119. ' X:=Y xor va;',
  3120. '']);
  3121. ConvertProgram;
  3122. CheckSource('TestBitwiseOperators',
  3123. LinesToStr([ // statements
  3124. 'this.vA = 0;',
  3125. 'this.vB = 0;',
  3126. 'this.vC = 0;',
  3127. 'this.X = 0;',
  3128. 'this.Y = 0;',
  3129. 'this.Z = 0;',
  3130. '']),
  3131. LinesToStr([ // this.$main
  3132. '$mod.vA = $mod.vB & $mod.vC;',
  3133. '$mod.vA = $mod.vB | $mod.vC;',
  3134. '$mod.vA = $mod.vB ^ $mod.vC;',
  3135. '$mod.vA = $mod.vB << $mod.vC;',
  3136. '$mod.vA = $mod.vB >>> $mod.vC;',
  3137. '$mod.vA = 3 & $mod.vC;',
  3138. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3139. '$mod.vA = ~$mod.vB;',
  3140. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3141. '$mod.X = $mod.Y & $mod.vA;',
  3142. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3143. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3144. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3145. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3146. '']));
  3147. end;
  3148. procedure TTestModule.TestBitwiseOperatorsLongword;
  3149. begin
  3150. StartProgram(false);
  3151. Add([
  3152. 'var',
  3153. ' a,b,c:longword;',
  3154. ' i: longint;',
  3155. 'begin',
  3156. ' a:=$12345678;',
  3157. ' b:=$EDCBA987;',
  3158. ' c:=not a;',
  3159. ' c:=a and b;',
  3160. ' c:=a and $ffff0000;',
  3161. ' c:=a or b;',
  3162. ' c:=a or $ff00ff00;',
  3163. ' c:=a xor b;',
  3164. ' c:=a xor $f0f0f0f0;',
  3165. ' c:=a shl 1;',
  3166. ' c:=a shl 16;',
  3167. ' c:=a shl 24;',
  3168. ' c:=a shl b;',
  3169. ' c:=a shr 1;',
  3170. ' c:=a shr 16;',
  3171. ' c:=a shr 24;',
  3172. ' c:=a shr b;',
  3173. ' c:=(b and c) or (a and b);',
  3174. ' c:=i and a;',
  3175. ' c:=i or a;',
  3176. ' c:=i xor a;',
  3177. '']);
  3178. ConvertProgram;
  3179. CheckSource('TestBitwiseOperatorsLongword',
  3180. LinesToStr([ // statements
  3181. 'this.a = 0;',
  3182. 'this.b = 0;',
  3183. 'this.c = 0;',
  3184. 'this.i = 0;',
  3185. '']),
  3186. LinesToStr([ // this.$main
  3187. '$mod.a = 0x12345678;',
  3188. '$mod.b = 0xEDCBA987;',
  3189. '$mod.c = rtl.lw(~$mod.a);',
  3190. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3191. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3192. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3193. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3194. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3195. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3196. '$mod.c = rtl.lw($mod.a << 1);',
  3197. '$mod.c = rtl.lw($mod.a << 16);',
  3198. '$mod.c = rtl.lw($mod.a << 24);',
  3199. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3200. '$mod.c = rtl.lw($mod.a >>> 1);',
  3201. '$mod.c = rtl.lw($mod.a >>> 16);',
  3202. '$mod.c = rtl.lw($mod.a >>> 24);',
  3203. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3204. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3205. '$mod.c = $mod.i & $mod.a;',
  3206. '$mod.c = $mod.i | $mod.a;',
  3207. '$mod.c = $mod.i ^ $mod.a;',
  3208. '']));
  3209. end;
  3210. procedure TTestModule.TestPrgProcVar;
  3211. begin
  3212. StartProgram(false);
  3213. Add('procedure Proc1;');
  3214. Add('type');
  3215. Add(' t1=longint;');
  3216. Add('var');
  3217. Add(' vA:t1;');
  3218. Add('begin');
  3219. Add('end;');
  3220. Add('begin');
  3221. ConvertProgram;
  3222. CheckSource('TestPrgProcVar',
  3223. LinesToStr([ // statements
  3224. 'this.Proc1 = function () {',
  3225. ' var vA=0;',
  3226. '};'
  3227. ]),
  3228. LinesToStr([ // this.$main
  3229. ''
  3230. ]));
  3231. end;
  3232. procedure TTestModule.TestUnitProcVar;
  3233. begin
  3234. StartUnit(false);
  3235. Add('interface');
  3236. Add('');
  3237. Add('type tA=string; // unit scope');
  3238. Add('procedure Proc1;');
  3239. Add('');
  3240. Add('implementation');
  3241. Add('');
  3242. Add('procedure Proc1;');
  3243. Add('type tA=longint; // local proc scope');
  3244. Add('var v1:tA; // using local tA');
  3245. Add('begin');
  3246. Add('end;');
  3247. Add('var v2:tA; // using interface tA');
  3248. ConvertUnit;
  3249. CheckSource('TestUnitProcVar',
  3250. LinesToStr([ // statements
  3251. 'var $impl = $mod.$impl;',
  3252. 'this.Proc1 = function () {',
  3253. ' var v1 = 0;',
  3254. '};',
  3255. '']),
  3256. // this.$init
  3257. '',
  3258. // implementation
  3259. LinesToStr([
  3260. '$impl.v2 = "";',
  3261. '']));
  3262. end;
  3263. procedure TTestModule.TestImplProc;
  3264. begin
  3265. StartUnit(false);
  3266. Add('interface');
  3267. Add('');
  3268. Add('procedure Proc1;');
  3269. Add('');
  3270. Add('implementation');
  3271. Add('');
  3272. Add('procedure Proc1; begin end;');
  3273. Add('procedure Proc2; begin end;');
  3274. Add('initialization');
  3275. Add(' Proc1;');
  3276. Add(' Proc2;');
  3277. ConvertUnit;
  3278. CheckSource('TestImplProc',
  3279. LinesToStr([ // statements
  3280. 'var $impl = $mod.$impl;',
  3281. 'this.Proc1 = function () {',
  3282. '};',
  3283. '']),
  3284. LinesToStr([ // this.$init
  3285. '$mod.Proc1();',
  3286. '$impl.Proc2();',
  3287. '']),
  3288. LinesToStr([ // implementation
  3289. '$impl.Proc2 = function () {',
  3290. '};',
  3291. ''])
  3292. );
  3293. end;
  3294. procedure TTestModule.TestFunctionResult;
  3295. begin
  3296. StartProgram(false);
  3297. Add('function Func1: longint;');
  3298. Add('begin');
  3299. Add(' Result:=3;');
  3300. Add(' Func1:=4;');
  3301. Add('end;');
  3302. Add('begin');
  3303. ConvertProgram;
  3304. CheckSource('TestFunctionResult',
  3305. LinesToStr([ // statements
  3306. 'this.Func1 = function () {',
  3307. ' var Result = 0;',
  3308. ' Result = 3;',
  3309. ' Result = 4;',
  3310. ' return Result;',
  3311. '};'
  3312. ]),
  3313. '');
  3314. end;
  3315. procedure TTestModule.TestNestedProc;
  3316. begin
  3317. StartProgram(false);
  3318. Add([
  3319. 'var vInUnit: longint;',
  3320. 'function DoIt(pA,pD: longint): longint;',
  3321. 'var',
  3322. ' vB: longint;',
  3323. ' vC: longint;',
  3324. ' function Nesty(pA: longint): longint; ',
  3325. ' var vB: longint;',
  3326. ' begin',
  3327. ' Result:=pa+vb+vc+pd+vInUnit;',
  3328. ' nesty:=3;',
  3329. ' doit:=4;',
  3330. ' exit;',
  3331. ' end;',
  3332. 'begin',
  3333. ' Result:=pa+vb+vc;',
  3334. ' doit:=6;',
  3335. ' exit;',
  3336. 'end;',
  3337. 'begin']);
  3338. ConvertProgram;
  3339. CheckSource('TestNestedProc',
  3340. LinesToStr([ // statements
  3341. 'this.vInUnit = 0;',
  3342. 'this.DoIt = function (pA, pD) {',
  3343. ' var Result = 0;',
  3344. ' var vB = 0;',
  3345. ' var vC = 0;',
  3346. ' function Nesty(pA) {',
  3347. ' var Result$1 = 0;',
  3348. ' var vB = 0;',
  3349. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3350. ' Result$1 = 3;',
  3351. ' Result = 4;',
  3352. ' return Result$1;',
  3353. ' return Result$1;',
  3354. ' };',
  3355. ' Result = pA + vB + vC;',
  3356. ' Result = 6;',
  3357. ' return Result;',
  3358. ' return Result;',
  3359. '};'
  3360. ]),
  3361. '');
  3362. end;
  3363. procedure TTestModule.TestNestedProc_ResultString;
  3364. begin
  3365. StartProgram(false);
  3366. Add([
  3367. 'function DoIt: string;',
  3368. ' function Nesty: string; ',
  3369. ' begin',
  3370. ' nesty:=#65#66;',
  3371. ' nesty[1]:=#67;',
  3372. ' doit:=#68;',
  3373. ' doit[2]:=#69;',
  3374. ' end;',
  3375. 'begin',
  3376. ' doit:=#70;',
  3377. ' doit[3]:=#71;',
  3378. 'end;',
  3379. 'begin']);
  3380. ConvertProgram;
  3381. CheckSource('TestNestedProc_ResultString',
  3382. LinesToStr([ // statements
  3383. 'this.DoIt = function () {',
  3384. ' var Result = "";',
  3385. ' function Nesty() {',
  3386. ' var Result$1 = "";',
  3387. ' Result$1 = "AB";',
  3388. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3389. ' Result = "D";',
  3390. ' Result = rtl.setCharAt(Result, 1, "E");',
  3391. ' return Result$1;',
  3392. ' };',
  3393. ' Result = "F";',
  3394. ' Result = rtl.setCharAt(Result, 2, "G");',
  3395. ' return Result;',
  3396. '};'
  3397. ]),
  3398. '');
  3399. end;
  3400. procedure TTestModule.TestForwardProc;
  3401. begin
  3402. StartProgram(false);
  3403. Add('procedure FuncA(Bar: longint); forward;');
  3404. Add('procedure FuncB(Bar: longint);');
  3405. Add('begin');
  3406. Add(' funca(bar);');
  3407. Add('end;');
  3408. Add('procedure funca(bar: longint);');
  3409. Add('begin');
  3410. Add(' if bar=3 then ;');
  3411. Add('end;');
  3412. Add('begin');
  3413. Add(' funca(4);');
  3414. Add(' funcb(5);');
  3415. ConvertProgram;
  3416. CheckSource('TestForwardProc',
  3417. LinesToStr([ // statements'
  3418. 'this.FuncB = function (Bar) {',
  3419. ' $mod.FuncA(Bar);',
  3420. '};',
  3421. 'this.FuncA = function (Bar) {',
  3422. ' if (Bar === 3);',
  3423. '};'
  3424. ]),
  3425. LinesToStr([
  3426. '$mod.FuncA(4);',
  3427. '$mod.FuncB(5);'
  3428. ])
  3429. );
  3430. end;
  3431. procedure TTestModule.TestNestedForwardProc;
  3432. begin
  3433. StartProgram(false);
  3434. Add('procedure FuncA;');
  3435. Add(' procedure FuncB(i: longint); forward;');
  3436. Add(' procedure FuncC(i: longint);');
  3437. Add(' begin');
  3438. Add(' funcb(i);');
  3439. Add(' end;');
  3440. Add(' procedure FuncB(i: longint);');
  3441. Add(' begin');
  3442. Add(' if i=3 then ;');
  3443. Add(' end;');
  3444. Add('begin');
  3445. Add(' funcc(4)');
  3446. Add('end;');
  3447. Add('begin');
  3448. Add(' funca;');
  3449. ConvertProgram;
  3450. CheckSource('TestNestedForwardProc',
  3451. LinesToStr([ // statements'
  3452. 'this.FuncA = function () {',
  3453. ' function FuncC(i) {',
  3454. ' FuncB(i);',
  3455. ' };',
  3456. ' function FuncB(i) {',
  3457. ' if (i === 3);',
  3458. ' };',
  3459. ' FuncC(4);',
  3460. '};'
  3461. ]),
  3462. LinesToStr([
  3463. '$mod.FuncA();'
  3464. ])
  3465. );
  3466. end;
  3467. procedure TTestModule.TestAssignFunctionResult;
  3468. begin
  3469. StartProgram(false);
  3470. Add('function Func1: longint;');
  3471. Add('begin');
  3472. Add('end;');
  3473. Add('var i: longint;');
  3474. Add('begin');
  3475. Add(' i:=func1();');
  3476. Add(' i:=func1()+func1();');
  3477. ConvertProgram;
  3478. CheckSource('TestAssignFunctionResult',
  3479. LinesToStr([ // statements
  3480. 'this.Func1 = function () {',
  3481. ' var Result = 0;',
  3482. ' return Result;',
  3483. '};',
  3484. 'this.i = 0;'
  3485. ]),
  3486. LinesToStr([
  3487. '$mod.i = $mod.Func1();',
  3488. '$mod.i = $mod.Func1() + $mod.Func1();'
  3489. ]));
  3490. end;
  3491. procedure TTestModule.TestFunctionResultInCondition;
  3492. begin
  3493. StartProgram(false);
  3494. Add('function Func1: longint;');
  3495. Add('begin');
  3496. Add('end;');
  3497. Add('function Func2: boolean;');
  3498. Add('begin');
  3499. Add('end;');
  3500. Add('var i: longint;');
  3501. Add('begin');
  3502. Add(' if func2 then ;');
  3503. Add(' if i=func1() then ;');
  3504. Add(' if i=func1 then ;');
  3505. ConvertProgram;
  3506. CheckSource('TestFunctionResultInCondition',
  3507. LinesToStr([ // statements
  3508. 'this.Func1 = function () {',
  3509. ' var Result = 0;',
  3510. ' return Result;',
  3511. '};',
  3512. 'this.Func2 = function () {',
  3513. ' var Result = false;',
  3514. ' return Result;',
  3515. '};',
  3516. 'this.i = 0;'
  3517. ]),
  3518. LinesToStr([
  3519. 'if ($mod.Func2());',
  3520. 'if ($mod.i === $mod.Func1());',
  3521. 'if ($mod.i === $mod.Func1());'
  3522. ]));
  3523. end;
  3524. procedure TTestModule.TestFunctionResultInForLoop;
  3525. begin
  3526. StartProgram(false);
  3527. Add([
  3528. 'function Func1(a: array of longint): longint;',
  3529. 'begin',
  3530. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3531. ' for Result in a do if a[Result]=0 then exit;',
  3532. 'end;',
  3533. 'begin',
  3534. ' Func1([1,2,3])']);
  3535. ConvertProgram;
  3536. CheckSource('TestFunctionResultInForLoop',
  3537. LinesToStr([ // statements
  3538. 'this.Func1 = function (a) {',
  3539. ' var Result = 0;',
  3540. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3541. ' Result = $l;',
  3542. ' if (a[Result] === 0) return Result;',
  3543. ' };',
  3544. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3545. ' Result = $in[$l1];',
  3546. ' if (a[Result] === 0) return Result;',
  3547. ' };',
  3548. ' return Result;',
  3549. '};',
  3550. '']),
  3551. LinesToStr([
  3552. '$mod.Func1([1, 2, 3]);'
  3553. ]));
  3554. end;
  3555. procedure TTestModule.TestFunctionResultInTypeCast;
  3556. begin
  3557. StartProgram(false);
  3558. Add([
  3559. 'function GetInt: longint;',
  3560. 'begin',
  3561. 'end;',
  3562. 'begin',
  3563. ' if Byte(GetInt)=0 then ;',
  3564. '']);
  3565. ConvertProgram;
  3566. CheckSource('TestFunctionResultInTypeCast',
  3567. LinesToStr([ // statements
  3568. 'this.GetInt = function () {',
  3569. ' var Result = 0;',
  3570. ' return Result;',
  3571. '};',
  3572. '']),
  3573. LinesToStr([
  3574. 'if (($mod.GetInt() & 255) === 0) ;'
  3575. ]));
  3576. end;
  3577. procedure TTestModule.TestExit;
  3578. begin
  3579. StartProgram(false);
  3580. Add('procedure ProcA;');
  3581. Add('begin');
  3582. Add(' exit;');
  3583. Add('end;');
  3584. Add('function FuncB: longint;');
  3585. Add('begin');
  3586. Add(' exit;');
  3587. Add(' exit(3);');
  3588. Add('end;');
  3589. Add('function FuncC: string;');
  3590. Add('begin');
  3591. Add(' exit;');
  3592. Add(' exit(''a'');');
  3593. Add(' exit(''abc'');');
  3594. Add('end;');
  3595. Add('begin');
  3596. Add(' exit;');
  3597. Add(' exit(1);');
  3598. ConvertProgram;
  3599. CheckSource('TestExit',
  3600. LinesToStr([ // statements
  3601. 'this.ProcA = function () {',
  3602. ' return;',
  3603. '};',
  3604. 'this.FuncB = function () {',
  3605. ' var Result = 0;',
  3606. ' return Result;',
  3607. ' return 3;',
  3608. ' return Result;',
  3609. '};',
  3610. 'this.FuncC = function () {',
  3611. ' var Result = "";',
  3612. ' return Result;',
  3613. ' return "a";',
  3614. ' return "abc";',
  3615. ' return Result;',
  3616. '};'
  3617. ]),
  3618. LinesToStr([
  3619. 'return;',
  3620. 'return 1;',
  3621. '']));
  3622. end;
  3623. procedure TTestModule.TestExit_ResultInFinally;
  3624. begin
  3625. StartProgram(false);
  3626. Add([
  3627. 'function Run: word;',
  3628. 'begin',
  3629. ' try',
  3630. ' exit(3);', // no Result in finally -> use return 3
  3631. ' finally',
  3632. ' end;',
  3633. 'end;',
  3634. 'function Fly: word;',
  3635. 'begin',
  3636. ' try',
  3637. ' exit(3);',
  3638. ' finally',
  3639. ' if Result>0 then ;',
  3640. ' end;',
  3641. 'end;',
  3642. 'function Jump: word;',
  3643. 'begin',
  3644. ' try',
  3645. ' try',
  3646. ' exit(4);',
  3647. ' finally',
  3648. ' end;',
  3649. ' finally',
  3650. ' if Result>0 then ;',
  3651. ' end;',
  3652. 'end;',
  3653. 'begin',
  3654. '']);
  3655. ConvertProgram;
  3656. CheckSource('TestExit_ResultInFinally',
  3657. LinesToStr([ // statements
  3658. 'this.Run = function () {',
  3659. ' var Result = 0;',
  3660. ' try {',
  3661. ' return 3;',
  3662. ' } finally {',
  3663. ' };',
  3664. ' return Result;',
  3665. '};',
  3666. 'this.Fly = function () {',
  3667. ' var Result = 0;',
  3668. ' try {',
  3669. ' Result = 3;',
  3670. ' return Result;',
  3671. ' } finally {',
  3672. ' if (Result > 0) ;',
  3673. ' };',
  3674. ' return Result;',
  3675. '};',
  3676. 'this.Jump = function () {',
  3677. ' var Result = 0;',
  3678. ' try {',
  3679. ' try {',
  3680. ' Result = 4;',
  3681. ' return Result;',
  3682. ' } finally {',
  3683. ' };',
  3684. ' } finally {',
  3685. ' if (Result > 0) ;',
  3686. ' };',
  3687. ' return Result;',
  3688. '};',
  3689. '']),
  3690. LinesToStr([
  3691. '']));
  3692. end;
  3693. procedure TTestModule.TestBreak;
  3694. begin
  3695. StartProgram(false);
  3696. Add([
  3697. 'var',
  3698. ' i: longint;',
  3699. 'begin',
  3700. ' repeat',
  3701. ' break;',
  3702. ' until true;',
  3703. ' while true do',
  3704. ' break;',
  3705. ' for i:=1 to 2 do',
  3706. ' break;']);
  3707. ConvertProgram;
  3708. CheckSource('TestBreak',
  3709. LinesToStr([ // statements
  3710. 'this.i = 0;'
  3711. ]),
  3712. LinesToStr([
  3713. 'do {',
  3714. ' break;',
  3715. '} while (!true);',
  3716. 'while (true) break;',
  3717. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3718. '']));
  3719. end;
  3720. procedure TTestModule.TestBreakAsVar;
  3721. begin
  3722. StartProgram(false);
  3723. Add([
  3724. 'procedure DoIt(break: boolean);',
  3725. 'begin',
  3726. ' if break then ;',
  3727. 'end;',
  3728. 'var',
  3729. ' break: boolean;',
  3730. 'begin',
  3731. ' if break then ;']);
  3732. ConvertProgram;
  3733. CheckSource('TestBreakAsVar',
  3734. LinesToStr([ // statements
  3735. 'this.DoIt = function (Break) {',
  3736. ' if (Break) ;',
  3737. '};',
  3738. 'this.Break = false;',
  3739. '']),
  3740. LinesToStr([
  3741. 'if($mod.Break) ;',
  3742. '']));
  3743. end;
  3744. procedure TTestModule.TestContinue;
  3745. begin
  3746. StartProgram(false);
  3747. Add('var i: longint;');
  3748. Add('begin');
  3749. Add(' repeat');
  3750. Add(' continue;');
  3751. Add(' until true;');
  3752. Add(' while true do');
  3753. Add(' continue;');
  3754. Add(' for i:=1 to 2 do');
  3755. Add(' continue;');
  3756. ConvertProgram;
  3757. CheckSource('TestContinue',
  3758. LinesToStr([ // statements
  3759. 'this.i = 0;'
  3760. ]),
  3761. LinesToStr([
  3762. 'do {',
  3763. ' continue;',
  3764. '} while (!true);',
  3765. 'while (true) continue;',
  3766. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3767. '']));
  3768. end;
  3769. procedure TTestModule.TestProc_External;
  3770. begin
  3771. StartProgram(false);
  3772. Add('procedure Foo; external name ''console.log'';');
  3773. Add('function Bar: longint; external name ''get.item'';');
  3774. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3775. Add('var');
  3776. Add(' i: longint;');
  3777. Add('begin');
  3778. Add(' Foo;');
  3779. Add(' i:=Bar;');
  3780. Add(' i:=Bla(''abc'');');
  3781. ConvertProgram;
  3782. CheckSource('TestProc_External',
  3783. LinesToStr([ // statements
  3784. 'this.i = 0;'
  3785. ]),
  3786. LinesToStr([
  3787. 'console.log();',
  3788. '$mod.i = get.item();',
  3789. '$mod.i = apply.something("abc");'
  3790. ]));
  3791. end;
  3792. procedure TTestModule.TestProc_ExternalOtherUnit;
  3793. begin
  3794. AddModuleWithIntfImplSrc('unit2.pas',
  3795. LinesToStr([
  3796. 'procedure Now; external name ''Date.now'';',
  3797. 'procedure DoIt;'
  3798. ]),
  3799. 'procedure doit; begin end;');
  3800. StartUnit(true);
  3801. Add('interface');
  3802. Add('uses unit2;');
  3803. Add('implementation');
  3804. Add('begin');
  3805. Add(' now;');
  3806. Add(' now();');
  3807. Add(' uNit2.now;');
  3808. Add(' uNit2.now();');
  3809. Add(' doit;');
  3810. Add(' uNit2.doit;');
  3811. ConvertUnit;
  3812. CheckSource('TestProc_ExternalOtherUnit',
  3813. LinesToStr([
  3814. '']),
  3815. LinesToStr([
  3816. 'Date.now();',
  3817. 'Date.now();',
  3818. 'Date.now();',
  3819. 'Date.now();',
  3820. 'pas.unit2.DoIt();',
  3821. 'pas.unit2.DoIt();',
  3822. '']));
  3823. end;
  3824. procedure TTestModule.TestProc_Asm;
  3825. begin
  3826. StartProgram(false);
  3827. Add([
  3828. '{$mode delphi}',
  3829. 'function DoIt: longint;',
  3830. 'begin;',
  3831. ' asm',
  3832. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3833. ' end;',
  3834. ' asm console.log(); end;',
  3835. ' asm',
  3836. ' s = "'' ";',
  3837. ' s = ''" '';',
  3838. ' s = s + "world" + "''";',
  3839. ' // end',
  3840. ' s = ''end'';',
  3841. ' s = "end";',
  3842. ' s = "foo\"bar";',
  3843. ' s = ''a\''b'';',
  3844. ' s = `${expr}\`-"-''-`;',
  3845. ' s = `multi',
  3846. 'line`;',
  3847. ' end;',
  3848. 'end;',
  3849. 'procedure Fly;',
  3850. 'asm',
  3851. ' return;',
  3852. 'end;',
  3853. 'begin']);
  3854. ConvertProgram;
  3855. CheckSource('TestProc_Asm',
  3856. LinesToStr([ // statements
  3857. 'this.DoIt = function () {',
  3858. ' var Result = 0;',
  3859. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3860. ' console.log();',
  3861. ' s = "'' ";',
  3862. ' s = ''" '';',
  3863. ' s = s + "world" + "''";',
  3864. ' // end',
  3865. ' s = ''end'';',
  3866. ' s = "end";',
  3867. ' s = "foo\"bar";',
  3868. ' s = ''a\''b'';',
  3869. ' s = `${expr}\`-"-''-`;',
  3870. ' s = `multi',
  3871. 'line`;',
  3872. ' return Result;',
  3873. '};',
  3874. 'this.Fly = function () {',
  3875. ' return;',
  3876. '};',
  3877. '']),
  3878. LinesToStr([
  3879. ''
  3880. ]));
  3881. end;
  3882. procedure TTestModule.TestProc_AsmSubBlock;
  3883. begin
  3884. StartProgram(true,[supTObject]);
  3885. Add([
  3886. '{$mode delphi}',
  3887. 'type',
  3888. ' TBird = class end;',
  3889. 'procedure Run(w: word);',
  3890. 'begin;',
  3891. ' if true then asm console.log(); end;',
  3892. ' if w>3 then asm',
  3893. ' var a = w+1;',
  3894. ' w = a+3;',
  3895. ' end;',
  3896. ' while (w>7) do asm',
  3897. ' w+=3; w*=2;',
  3898. ' end;',
  3899. ' try',
  3900. ' except',
  3901. ' on E: TBird do',
  3902. ' asm console.log(E); end;',
  3903. ' on E: TObject do',
  3904. ' asm var i=3; i--; end;',
  3905. ' else asm Fly; High; end;',
  3906. ' end;',
  3907. 'end;',
  3908. 'begin']);
  3909. ConvertProgram;
  3910. CheckSource('TestProc_AsmSubBlock',
  3911. LinesToStr([ // statements
  3912. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3913. '});',
  3914. 'this.Run = function (w) {',
  3915. ' if (true) console.log();',
  3916. ' if (w > 3) {',
  3917. ' var a = w+1;',
  3918. ' w = a+3;',
  3919. ' };',
  3920. ' while (w > 7) {',
  3921. ' w+=3; w*=2;',
  3922. ' };',
  3923. ' try {} catch ($e) {',
  3924. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3925. ' var E = $e;',
  3926. ' console.log(E);',
  3927. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3928. ' var E = $e;',
  3929. ' var i=3; i--;',
  3930. ' } else {',
  3931. ' Fly; High;',
  3932. ' }',
  3933. ' };',
  3934. '};',
  3935. '']),
  3936. LinesToStr([
  3937. ''
  3938. ]));
  3939. end;
  3940. procedure TTestModule.TestProc_Assembler;
  3941. begin
  3942. StartProgram(false);
  3943. Add('function DoIt: longint; assembler;');
  3944. Add('asm');
  3945. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3946. Add('end;');
  3947. Add('begin');
  3948. ConvertProgram;
  3949. CheckSource('TestProc_Assembler',
  3950. LinesToStr([ // statements
  3951. 'this.DoIt = function () {',
  3952. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3953. '};'
  3954. ]),
  3955. LinesToStr([
  3956. ''
  3957. ]));
  3958. end;
  3959. procedure TTestModule.TestProc_VarParam;
  3960. begin
  3961. StartProgram(false);
  3962. Add('type integer = longint;');
  3963. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3964. Add('var vJ: integer;');
  3965. Add('begin');
  3966. Add(' vg:=vg+1;');
  3967. Add(' vj:=vh+2;');
  3968. Add(' vi:=vi+3;');
  3969. Add(' doit(vg,vg,vg);');
  3970. Add(' doit(vh,vh,vj);');
  3971. Add(' doit(vi,vi,vi);');
  3972. Add(' doit(vj,vj,vj);');
  3973. Add('end;');
  3974. Add('var i: integer;');
  3975. Add('begin');
  3976. Add(' doit(i,i,i);');
  3977. ConvertProgram;
  3978. CheckSource('TestProc_VarParam',
  3979. LinesToStr([ // statements
  3980. 'this.DoIt = function (vG,vH,vI) {',
  3981. ' var vJ = 0;',
  3982. ' vG = vG + 1;',
  3983. ' vJ = vH + 2;',
  3984. ' vI.set(vI.get()+3);',
  3985. ' $mod.DoIt(vG, vG, {',
  3986. ' get: function () {',
  3987. ' return vG;',
  3988. ' },',
  3989. ' set: function (v) {',
  3990. ' vG = v;',
  3991. ' }',
  3992. ' });',
  3993. ' $mod.DoIt(vH, vH, {',
  3994. ' get: function () {',
  3995. ' return vJ;',
  3996. ' },',
  3997. ' set: function (v) {',
  3998. ' vJ = v;',
  3999. ' }',
  4000. ' });',
  4001. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4002. ' $mod.DoIt(vJ, vJ, {',
  4003. ' get: function () {',
  4004. ' return vJ;',
  4005. ' },',
  4006. ' set: function (v) {',
  4007. ' vJ = v;',
  4008. ' }',
  4009. ' });',
  4010. '};',
  4011. 'this.i = 0;'
  4012. ]),
  4013. LinesToStr([
  4014. '$mod.DoIt($mod.i,$mod.i,{',
  4015. ' p: $mod,',
  4016. ' get: function () {',
  4017. ' return this.p.i;',
  4018. ' },',
  4019. ' set: function (v) {',
  4020. ' this.p.i = v;',
  4021. ' }',
  4022. '});'
  4023. ]));
  4024. end;
  4025. procedure TTestModule.TestProc_VarParamString;
  4026. begin
  4027. StartProgram(false);
  4028. Add(['type TCaption = string;',
  4029. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4030. 'var c: char;',
  4031. 'begin',
  4032. ' va[1]:=c;',
  4033. ' vb[2]:=c;',
  4034. ' vc[3]:=c;',
  4035. 'end;',
  4036. 'begin']);
  4037. ConvertProgram;
  4038. CheckSource('TestProc_VarParamString',
  4039. LinesToStr([ // statements
  4040. 'this.DoIt = function (vA,vB,vC) {',
  4041. ' var c = "";',
  4042. ' vA = rtl.setCharAt(vA, 0, c);',
  4043. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4044. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4045. '};',
  4046. '']),
  4047. LinesToStr([
  4048. ]));
  4049. end;
  4050. procedure TTestModule.TestProc_VarParamV;
  4051. begin
  4052. StartProgram(false);
  4053. Add([
  4054. 'procedure Inc2(var i: longint);',
  4055. 'begin',
  4056. ' i:=i+2;',
  4057. 'end;',
  4058. 'procedure DoIt(v: longint);',
  4059. 'var p: array of longint;',
  4060. 'begin',
  4061. ' Inc2(v);',
  4062. ' Inc2(p[v]);',
  4063. 'end;',
  4064. 'begin']);
  4065. ConvertProgram;
  4066. CheckSource('TestProc_VarParamV',
  4067. LinesToStr([ // statements
  4068. 'this.Inc2 = function (i) {',
  4069. ' i.set(i.get()+2);',
  4070. '};',
  4071. 'this.DoIt = function (v) {',
  4072. ' var p = [];',
  4073. ' $mod.Inc2({get: function () {',
  4074. ' return v;',
  4075. ' }, set: function (w) {',
  4076. ' v = w;',
  4077. ' }});',
  4078. ' $mod.Inc2({',
  4079. ' a: v,',
  4080. ' p: p,',
  4081. ' get: function () {',
  4082. ' return this.p[this.a];',
  4083. ' },',
  4084. ' set: function (v) {',
  4085. ' this.p[this.a] = v;',
  4086. ' }',
  4087. ' });',
  4088. '};',
  4089. '']),
  4090. LinesToStr([
  4091. '']));
  4092. end;
  4093. procedure TTestModule.TestProc_Overload;
  4094. begin
  4095. StartProgram(false);
  4096. Add('procedure DoIt(vI: longint); begin end;');
  4097. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4098. Add('procedure DoIt(vD: double); begin end;');
  4099. Add('begin');
  4100. Add(' DoIt(1);');
  4101. Add(' DoIt(2,3);');
  4102. Add(' DoIt(4.5);');
  4103. ConvertProgram;
  4104. CheckSource('TestProcedureOverload',
  4105. LinesToStr([ // statements
  4106. 'this.DoIt = function (vI) {',
  4107. '};',
  4108. 'this.DoIt$1 = function (vI, vJ) {',
  4109. '};',
  4110. 'this.DoIt$2 = function (vD) {',
  4111. '};',
  4112. '']),
  4113. LinesToStr([
  4114. '$mod.DoIt(1);',
  4115. '$mod.DoIt$1(2, 3);',
  4116. '$mod.DoIt$2(4.5);',
  4117. '']));
  4118. end;
  4119. procedure TTestModule.TestProc_OverloadForward;
  4120. begin
  4121. StartProgram(false);
  4122. Add('procedure DoIt(vI: longint); forward;');
  4123. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4124. Add('procedure doit(vi: longint); begin end;');
  4125. Add('begin');
  4126. Add(' doit(1);');
  4127. Add(' doit(2,3);');
  4128. ConvertProgram;
  4129. CheckSource('TestProcedureOverloadForward',
  4130. LinesToStr([ // statements
  4131. 'this.DoIt$1 = function (vI, vJ) {',
  4132. '};',
  4133. 'this.DoIt = function (vI) {',
  4134. '};',
  4135. '']),
  4136. LinesToStr([
  4137. '$mod.DoIt(1);',
  4138. '$mod.DoIt$1(2, 3);',
  4139. '']));
  4140. end;
  4141. procedure TTestModule.TestProc_OverloadIntfImpl;
  4142. begin
  4143. StartUnit(false);
  4144. Add('interface');
  4145. Add('procedure DoIt(vI: longint);');
  4146. Add('procedure DoIt(vI, vJ: longint);');
  4147. Add('implementation');
  4148. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4149. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4150. Add('procedure DoIt(vi: longint); begin end;');
  4151. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4152. Add('procedure DoIt(vi, vj: longint); begin end;');
  4153. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4154. Add('begin');
  4155. Add(' doit(1);');
  4156. Add(' doit(2,3);');
  4157. Add(' doit(4,5,6);');
  4158. Add(' doit(7,8,9,10);');
  4159. Add(' doit(11,12,13,14,15);');
  4160. ConvertUnit;
  4161. CheckSource('TestProcedureOverloadUnit',
  4162. LinesToStr([ // statements
  4163. 'var $impl = $mod.$impl;',
  4164. 'this.DoIt = function (vI) {',
  4165. '};',
  4166. 'this.DoIt$1 = function (vI, vJ) {',
  4167. '};',
  4168. '']),
  4169. LinesToStr([ // this.$init
  4170. '$mod.DoIt(1);',
  4171. '$mod.DoIt$1(2, 3);',
  4172. '$impl.DoIt$3(4,5,6);',
  4173. '$impl.DoIt$4(7,8,9,10);',
  4174. '$impl.DoIt$2(11,12,13,14,15);',
  4175. '']),
  4176. LinesToStr([ // implementation
  4177. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4178. '};',
  4179. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4180. '};',
  4181. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4182. '};',
  4183. '']));
  4184. end;
  4185. procedure TTestModule.TestProc_OverloadNested;
  4186. begin
  4187. StartProgram(false);
  4188. Add([
  4189. 'procedure doit(vA: longint);',
  4190. ' procedure DoIt(vA, vB: longint); overload;',
  4191. ' begin',
  4192. ' doit(1);',
  4193. ' doit(1,2);',
  4194. ' end;',
  4195. ' procedure doit(vA, vB, vC: longint);',
  4196. ' begin',
  4197. ' doit(1);',
  4198. ' doit(1,2);',
  4199. ' doit(1,2,3);',
  4200. ' end;',
  4201. 'begin',
  4202. ' doit(1);',
  4203. ' doit(1,2);',
  4204. ' doit(1,2,3);',
  4205. 'end;',
  4206. 'begin // main',
  4207. ' doit(1);']);
  4208. ConvertProgram;
  4209. CheckSource('TestProcedureOverloadNested',
  4210. LinesToStr([ // statements
  4211. 'this.doit = function (vA) {',
  4212. ' function DoIt$1(vA, vB) {',
  4213. ' $mod.doit(1);',
  4214. ' DoIt$1(1, 2);',
  4215. ' };',
  4216. ' function doit$2(vA, vB, vC) {',
  4217. ' $mod.doit(1);',
  4218. ' DoIt$1(1, 2);',
  4219. ' doit$2(1, 2, 3);',
  4220. ' };',
  4221. ' $mod.doit(1);',
  4222. ' DoIt$1(1, 2);',
  4223. ' doit$2(1, 2, 3);',
  4224. '};',
  4225. '']),
  4226. LinesToStr([
  4227. '$mod.doit(1);',
  4228. '']));
  4229. end;
  4230. procedure TTestModule.TestProc_OverloadNestedForward;
  4231. begin
  4232. StartProgram(false);
  4233. Add([
  4234. 'procedure DoIt(vA: longint); overload; forward;',
  4235. 'procedure DoIt(vB, vC: longint); overload;',
  4236. 'begin // 2 param overload',
  4237. ' doit(1);',
  4238. ' doit(1,2);',
  4239. 'end;',
  4240. 'procedure doit(vA: longint);',
  4241. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4242. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4243. ' begin // 4 param overload',
  4244. ' doit(1);',
  4245. ' doit(1,2);',
  4246. ' doit(1,2,3);',
  4247. ' doit(1,2,3,4);',
  4248. ' end;',
  4249. ' procedure doit(vA, vB, vC: longint);',
  4250. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4251. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4252. ' begin // 6 param overload',
  4253. ' doit(1);',
  4254. ' doit(1,2);',
  4255. ' doit(1,2,3);',
  4256. ' doit(1,2,3,4);',
  4257. ' doit(1,2,3,4,5);',
  4258. ' doit(1,2,3,4,5,6);',
  4259. ' end;',
  4260. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4261. ' begin // 5 param overload',
  4262. ' doit(1);',
  4263. ' doit(1,2);',
  4264. ' doit(1,2,3);',
  4265. ' doit(1,2,3,4);',
  4266. ' doit(1,2,3,4,5);',
  4267. ' doit(1,2,3,4,5,6);',
  4268. ' end;',
  4269. ' begin // 3 param overload',
  4270. ' doit(1);',
  4271. ' doit(1,2);',
  4272. ' doit(1,2,3);',
  4273. ' doit(1,2,3,4);',
  4274. ' doit(1,2,3,4,5);',
  4275. ' doit(1,2,3,4,5,6);',
  4276. ' end;',
  4277. 'begin // 1 param overload',
  4278. ' doit(1);',
  4279. ' doit(1,2);',
  4280. ' doit(1,2,3);',
  4281. ' doit(1,2,3,4);',
  4282. 'end;',
  4283. 'begin // main',
  4284. ' doit(1);',
  4285. ' doit(1,2);']);
  4286. ConvertProgram;
  4287. CheckSource('TestProc_OverloadNestedForward',
  4288. LinesToStr([ // statements
  4289. 'this.DoIt$1 = function (vB, vC) {',
  4290. ' $mod.DoIt(1);',
  4291. ' $mod.DoIt$1(1, 2);',
  4292. '};',
  4293. 'this.DoIt = function (vA) {',
  4294. ' function DoIt$3(vA, vB, vC, vD) {',
  4295. ' $mod.DoIt(1);',
  4296. ' $mod.DoIt$1(1, 2);',
  4297. ' DoIt$2(1, 2, 3);',
  4298. ' DoIt$3(1, 2, 3, 4);',
  4299. ' };',
  4300. ' function DoIt$2(vA, vB, vC) {',
  4301. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4302. ' $mod.DoIt(1);',
  4303. ' $mod.DoIt$1(1, 2);',
  4304. ' DoIt$2(1, 2, 3);',
  4305. ' DoIt$3(1, 2, 3, 4);',
  4306. ' DoIt$4(1, 2, 3, 4, 5);',
  4307. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4308. ' };',
  4309. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4310. ' $mod.DoIt(1);',
  4311. ' $mod.DoIt$1(1, 2);',
  4312. ' DoIt$2(1, 2, 3);',
  4313. ' DoIt$3(1, 2, 3, 4);',
  4314. ' DoIt$4(1, 2, 3, 4, 5);',
  4315. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4316. ' };',
  4317. ' $mod.DoIt(1);',
  4318. ' $mod.DoIt$1(1, 2);',
  4319. ' DoIt$2(1, 2, 3);',
  4320. ' DoIt$3(1, 2, 3, 4);',
  4321. ' DoIt$4(1, 2, 3, 4, 5);',
  4322. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4323. ' };',
  4324. ' $mod.DoIt(1);',
  4325. ' $mod.DoIt$1(1, 2);',
  4326. ' DoIt$2(1, 2, 3);',
  4327. ' DoIt$3(1, 2, 3, 4);',
  4328. '};',
  4329. '']),
  4330. LinesToStr([
  4331. '$mod.DoIt(1);',
  4332. '$mod.DoIt$1(1, 2);',
  4333. '']));
  4334. end;
  4335. procedure TTestModule.TestProc_OverloadUnitCycle;
  4336. begin
  4337. AddModuleWithIntfImplSrc('Unit2.pas',
  4338. LinesToStr([
  4339. 'type',
  4340. ' TObject = class',
  4341. ' procedure DoIt(b: boolean); virtual; abstract;',
  4342. ' procedure DoIt(i: longint); virtual; abstract;',
  4343. ' end;',
  4344. '']),
  4345. 'uses test1;');
  4346. StartUnit(true);
  4347. Add([
  4348. 'interface',
  4349. 'uses unit2;',
  4350. 'type',
  4351. ' TEagle = class(TObject)',
  4352. ' procedure DoIt(b: boolean); override;',
  4353. ' procedure DoIt(i: longint); override;',
  4354. ' end;',
  4355. 'implementation',
  4356. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4357. 'procedure TEagle.DoIt(i: longint); begin end;',
  4358. '']);
  4359. ConvertUnit;
  4360. CheckSource('TestProc_OverloadUnitCycle',
  4361. LinesToStr([ // statements
  4362. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4363. ' this.DoIt = function (b) {',
  4364. ' };',
  4365. ' this.DoIt$1 = function (i) {',
  4366. ' };',
  4367. '});',
  4368. '']),
  4369. '',
  4370. LinesToStr([
  4371. '']));
  4372. end;
  4373. procedure TTestModule.TestProc_Varargs;
  4374. begin
  4375. StartProgram(false);
  4376. Add([
  4377. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4378. 'procedure ProcB; varargs; external name ''ProcB'';',
  4379. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4380. 'function GetIt: longint; begin end;',
  4381. 'begin',
  4382. ' ProcA(1);',
  4383. ' ProcA(1,2);',
  4384. ' ProcA(1,2.0);',
  4385. ' ProcA(1,2,3);',
  4386. ' ProcA(1,''2'');',
  4387. ' ProcA(2,'''');',
  4388. ' ProcA(3,false);',
  4389. ' ProcB;',
  4390. ' ProcB();',
  4391. ' ProcB(4);',
  4392. ' ProcB(''foo'');',
  4393. ' ProcC;',
  4394. ' ProcC();',
  4395. ' ProcC(4);',
  4396. ' ProcC(5,''foo'');',
  4397. ' ProcB(GetIt);',
  4398. ' ProcB(GetIt());',
  4399. ' ProcB(GetIt,GetIt());']);
  4400. ConvertProgram;
  4401. CheckSource('TestProc_Varargs',
  4402. LinesToStr([ // statements
  4403. 'this.GetIt = function () {',
  4404. ' var Result = 0;',
  4405. ' return Result;',
  4406. '};',
  4407. '']),
  4408. LinesToStr([
  4409. 'ProcA(1);',
  4410. 'ProcA(1, 2);',
  4411. 'ProcA(1, 2.0);',
  4412. 'ProcA(1, 2, 3);',
  4413. 'ProcA(1, "2");',
  4414. 'ProcA(2, "");',
  4415. 'ProcA(3, false);',
  4416. 'ProcB();',
  4417. 'ProcB();',
  4418. 'ProcB(4);',
  4419. 'ProcB("foo");',
  4420. 'ProcC(17);',
  4421. 'ProcC(17);',
  4422. 'ProcC(4);',
  4423. 'ProcC(5, "foo");',
  4424. 'ProcB($mod.GetIt());',
  4425. 'ProcB($mod.GetIt());',
  4426. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4427. '']));
  4428. end;
  4429. procedure TTestModule.TestProc_ConstOrder;
  4430. begin
  4431. StartProgram(false);
  4432. Add([
  4433. 'const A = 3;',
  4434. 'const B = A+1;',
  4435. 'procedure DoIt;',
  4436. 'const C = A+1;',
  4437. 'const D = B+1;',
  4438. 'const E = D+C+B+A;',
  4439. 'begin',
  4440. 'end;',
  4441. 'begin'
  4442. ]);
  4443. ConvertProgram;
  4444. CheckSource('TestProc_ConstOrder',
  4445. LinesToStr([ // statements
  4446. 'this.A = 3;',
  4447. 'this.B = 3 + 1;',
  4448. 'var C = 3 + 1;',
  4449. 'var D = 4 + 1;',
  4450. 'var E = 5 + 4 + 4 + 3;',
  4451. 'this.DoIt = function () {',
  4452. '};',
  4453. '']),
  4454. LinesToStr([
  4455. ''
  4456. ]));
  4457. end;
  4458. procedure TTestModule.TestProc_DuplicateConst;
  4459. begin
  4460. StartProgram(false);
  4461. Add([
  4462. 'const A = 1;',
  4463. 'procedure DoIt;',
  4464. 'const A = 2;',
  4465. ' procedure SubIt;',
  4466. ' const A = 21;',
  4467. ' begin',
  4468. ' end;',
  4469. 'begin',
  4470. 'end;',
  4471. 'procedure DoSome;',
  4472. 'const A = 3;',
  4473. 'begin',
  4474. 'end;',
  4475. 'begin'
  4476. ]);
  4477. ConvertProgram;
  4478. CheckSource('TestProc_DuplicateConst',
  4479. LinesToStr([ // statements
  4480. 'this.A = 1;',
  4481. 'var A$1 = 2;',
  4482. 'var A$2 = 21;',
  4483. 'this.DoIt = function () {',
  4484. ' function SubIt() {',
  4485. ' };',
  4486. '};',
  4487. 'var A$3 = 3;',
  4488. 'this.DoSome = function () {',
  4489. '};',
  4490. '']),
  4491. LinesToStr([
  4492. ''
  4493. ]));
  4494. end;
  4495. procedure TTestModule.TestProc_LocalVarAbsolute;
  4496. begin
  4497. StartProgram(false);
  4498. Add([
  4499. 'type',
  4500. ' TObject = class',
  4501. ' Index: longint;',
  4502. ' procedure DoAbs(Item: pointer);',
  4503. ' end;',
  4504. 'procedure TObject.DoAbs(Item: pointer);',
  4505. 'var',
  4506. ' o: TObject absolute Item;',
  4507. 'begin',
  4508. ' if o.Index<o.Index then o.Index:=o.Index;',
  4509. 'end;',
  4510. 'procedure DoIt(i: longint; p: pointer);',
  4511. 'var',
  4512. ' d: double absolute i;',
  4513. ' s: string absolute d;',
  4514. ' oi: TObject absolute i;',
  4515. ' op: TObject absolute p;',
  4516. 'begin',
  4517. ' if d=d then d:=d;',
  4518. ' if s=s then s:=s;',
  4519. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4520. ' if op.Index=op.Index then op.Index:=op.Index;',
  4521. 'end;',
  4522. 'begin']);
  4523. ConvertProgram;
  4524. CheckSource('TestProc_LocalVarAbsolute',
  4525. LinesToStr([ // statements
  4526. 'rtl.createClass(this, "TObject", null, function () {',
  4527. ' this.$init = function () {',
  4528. ' this.Index = 0;',
  4529. ' };',
  4530. ' this.$final = function () {',
  4531. ' };',
  4532. ' this.DoAbs = function (Item) {',
  4533. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4534. ' };',
  4535. '});',
  4536. 'this.DoIt = function (i, p) {',
  4537. ' if (i === i) i = i;',
  4538. ' if (i === i) i = i;',
  4539. ' if (i.Index < i.Index) i.Index = i.Index;',
  4540. ' if (p.Index === p.Index) p.Index = p.Index;',
  4541. '};'
  4542. ]),
  4543. LinesToStr([
  4544. ]));
  4545. end;
  4546. procedure TTestModule.TestProc_LocalVarInit;
  4547. begin
  4548. StartProgram(false);
  4549. Add([
  4550. 'type TBytes = array of byte;',
  4551. 'procedure DoIt;',
  4552. 'const c = 4;',
  4553. 'var',
  4554. ' b: byte = 1;',
  4555. ' w: word = 2+c;',
  4556. ' p: pointer = nil;',
  4557. ' Buffer: TBytes = nil;',
  4558. 'begin',
  4559. 'end;',
  4560. 'begin']);
  4561. ConvertProgram;
  4562. CheckSource('TestProc_LocalVarInit',
  4563. LinesToStr([ // statements
  4564. 'var c = 4;',
  4565. 'this.DoIt = function () {',
  4566. ' var b = 1;',
  4567. ' var w = 2 + 4;',
  4568. ' var p = null;',
  4569. ' var Buffer = [];',
  4570. '};',
  4571. '']),
  4572. LinesToStr([
  4573. ]));
  4574. end;
  4575. procedure TTestModule.TestProc_ReservedWords;
  4576. begin
  4577. StartProgram(false);
  4578. Add([
  4579. 'procedure Date(ArrayBuffer: longint);',
  4580. 'const',
  4581. ' NaN: longint = 3;',
  4582. 'var',
  4583. ' &Boolean: longint;',
  4584. ' procedure Error(ArrayBuffer: longint);',
  4585. ' begin',
  4586. ' end;',
  4587. 'begin',
  4588. ' Nan:=&bOolean;',
  4589. 'end;',
  4590. 'begin',
  4591. ' Date(1);']);
  4592. ConvertProgram;
  4593. CheckSource('TestProc_ReservedWords',
  4594. LinesToStr([ // statements
  4595. 'var naN = 3;',
  4596. 'this.Date = function (arrayBuffer) {',
  4597. ' var boolean = 0;',
  4598. ' function error(arrayBuffer) {',
  4599. ' };',
  4600. ' naN = boolean;',
  4601. '};',
  4602. '']),
  4603. LinesToStr([
  4604. ' $mod.Date(1);'
  4605. ]));
  4606. end;
  4607. procedure TTestModule.TestProc_ConstRefWord;
  4608. begin
  4609. StartProgram(false);
  4610. Add([
  4611. 'procedure Run(constref w: word);',
  4612. 'var l: word;',
  4613. 'begin',
  4614. ' l:=w;',
  4615. ' Run(w);',
  4616. ' Run(l);',
  4617. 'end;',
  4618. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4619. 'begin',
  4620. ' Run(a);',
  4621. ' Run(b);',
  4622. ' Run(c);',
  4623. ' Run(d);',
  4624. ' Run(e);',
  4625. 'end;',
  4626. 'begin',
  4627. ' Run(1);']);
  4628. ConvertProgram;
  4629. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4630. CheckSource('TestProc_ConstRefWord',
  4631. LinesToStr([ // statements
  4632. 'this.Run = function (w) {',
  4633. ' var l = 0;',
  4634. ' l = w;',
  4635. ' $mod.Run(w);',
  4636. ' $mod.Run(l);',
  4637. '};',
  4638. 'this.Fly = function (a, b, c, d, e) {',
  4639. ' $mod.Run(a);',
  4640. ' $mod.Run(b.get());',
  4641. ' $mod.Run(c.get());',
  4642. ' $mod.Run(d);',
  4643. ' $mod.Run(e);',
  4644. '};',
  4645. '']),
  4646. LinesToStr([
  4647. '$mod.Run(1);'
  4648. ]));
  4649. end;
  4650. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4651. begin
  4652. StartProgram(false);
  4653. Add([
  4654. '{$mode objfpc}',
  4655. 'type',
  4656. ' TFunc = reference to function(x: word): word;',
  4657. 'var Func: TFunc;',
  4658. 'procedure DoIt(a: word);',
  4659. 'begin',
  4660. ' Func:=function(b:word): word',
  4661. ' begin',
  4662. ' Result:=a+b;',
  4663. ' exit(b);',
  4664. ' exit(Result);',
  4665. ' end;',// test semicolon
  4666. ' a:=3;',
  4667. 'end;',
  4668. 'begin',
  4669. ' Func:=function(c:word):word begin',
  4670. ' Result:=3+c;',
  4671. ' exit(c);',
  4672. ' exit(Result);',
  4673. ' end;']);
  4674. ConvertProgram;
  4675. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4676. LinesToStr([ // statements
  4677. 'this.Func = null;',
  4678. 'this.DoIt = function (a) {',
  4679. ' $mod.Func = function (b) {',
  4680. ' var Result = 0;',
  4681. ' Result = a + b;',
  4682. ' return b;',
  4683. ' return Result;',
  4684. ' return Result;',
  4685. ' };',
  4686. ' a = 3;',
  4687. '};',
  4688. '']),
  4689. LinesToStr([
  4690. '$mod.Func = function (c) {',
  4691. ' var Result = 0;',
  4692. ' Result = 3 + c;',
  4693. ' return c;',
  4694. ' return Result;',
  4695. ' return Result;',
  4696. '};',
  4697. '']));
  4698. end;
  4699. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4700. begin
  4701. StartProgram(false);
  4702. Add([
  4703. '{$mode delphi}',
  4704. 'type',
  4705. ' TProc = reference to procedure(x: word);',
  4706. 'procedure DoIt(a: word);',
  4707. 'var Proc: TProc;',
  4708. 'begin',
  4709. ' Proc:=procedure(b:word) begin end;',
  4710. 'end;',
  4711. 'var Proc: TProc;',
  4712. 'begin',
  4713. ' Proc:=procedure(c:word) begin end;',
  4714. '']);
  4715. ConvertProgram;
  4716. CheckSource('TestAnonymousProc_Assign_Delphi',
  4717. LinesToStr([ // statements
  4718. 'this.DoIt = function (a) {',
  4719. ' var Proc = null;',
  4720. ' Proc = function (b) {',
  4721. ' };',
  4722. '};',
  4723. 'this.Proc = null;',
  4724. '']),
  4725. LinesToStr([
  4726. '$mod.Proc = function (c) {',
  4727. '};',
  4728. '']));
  4729. end;
  4730. procedure TTestModule.TestAnonymousProc_Arg;
  4731. begin
  4732. StartProgram(false);
  4733. Add([
  4734. 'type',
  4735. ' TProc = reference to procedure;',
  4736. ' TFunc = reference to function(x: word): word;',
  4737. 'procedure DoMore(f,g: TProc);',
  4738. 'begin',
  4739. 'end;',
  4740. 'procedure DoOdd(v: jsvalue);',
  4741. 'begin',
  4742. 'end;',
  4743. 'procedure DoIt(f: TFunc);',
  4744. 'begin',
  4745. ' DoIt(function(b:word): word',
  4746. ' begin',
  4747. ' Result:=1+b;',
  4748. ' end);',
  4749. ' DoMore(procedure begin end, procedure begin end);',
  4750. ' DoOdd(procedure begin end);',
  4751. 'end;',
  4752. 'begin',
  4753. ' DoMore(procedure begin end,',
  4754. ' procedure assembler asm',
  4755. ' console.log("c");',
  4756. ' end);',
  4757. '']);
  4758. ConvertProgram;
  4759. CheckSource('TestAnonymousProc_Arg',
  4760. LinesToStr([ // statements
  4761. 'this.DoMore = function (f, g) {',
  4762. '};',
  4763. 'this.DoOdd = function (v) {',
  4764. '};',
  4765. 'this.DoIt = function (f) {',
  4766. ' $mod.DoIt(function (b) {',
  4767. ' var Result = 0;',
  4768. ' Result = 1 + b;',
  4769. ' return Result;',
  4770. ' });',
  4771. ' $mod.DoMore(function () {',
  4772. ' }, function () {',
  4773. ' });',
  4774. ' $mod.DoOdd(function () {',
  4775. ' });',
  4776. '};',
  4777. '']),
  4778. LinesToStr([
  4779. '$mod.DoMore(function () {',
  4780. '}, function () {',
  4781. ' console.log("c");',
  4782. '});',
  4783. '']));
  4784. end;
  4785. procedure TTestModule.TestAnonymousProc_Typecast;
  4786. begin
  4787. StartProgram(false);
  4788. Add([
  4789. 'type',
  4790. ' TProc = reference to procedure(w: word);',
  4791. ' TArr = array of word;',
  4792. ' TFuncArr = reference to function: TArr;',
  4793. 'procedure DoIt(p: TProc);',
  4794. 'var',
  4795. ' w: word;',
  4796. ' a: TArr;',
  4797. 'begin',
  4798. ' p:=TProc(procedure(b: smallint) begin end);',
  4799. ' a:=TFuncArr(function: TArr begin end)();',
  4800. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4801. 'end;',
  4802. 'begin']);
  4803. ConvertProgram;
  4804. CheckSource('TestAnonymousProc_Typecast',
  4805. LinesToStr([ // statements
  4806. 'this.DoIt = function (p) {',
  4807. ' var w = 0;',
  4808. ' var a = [];',
  4809. ' p = function (b) {',
  4810. ' };',
  4811. ' a = function () {',
  4812. ' var Result = [];',
  4813. ' return Result;',
  4814. ' }();',
  4815. ' w = function () {',
  4816. ' var Result = [];',
  4817. ' return Result;',
  4818. ' }()[3];',
  4819. '};',
  4820. '']),
  4821. LinesToStr([
  4822. '']));
  4823. end;
  4824. procedure TTestModule.TestAnonymousProc_With;
  4825. begin
  4826. StartProgram(false);
  4827. Add([
  4828. 'type',
  4829. ' TProc = reference to procedure(w: word);',
  4830. ' TObject = class',
  4831. ' b: boolean;',
  4832. ' end;',
  4833. 'var',
  4834. ' p: TProc;',
  4835. ' bird: TObject;',
  4836. 'begin',
  4837. ' with bird do',
  4838. ' p:=procedure(w: word)',
  4839. ' begin',
  4840. ' b:=w>2;',
  4841. ' end;',
  4842. '']);
  4843. ConvertProgram;
  4844. CheckSource('TestAnonymousProc_With',
  4845. LinesToStr([ // statements
  4846. 'rtl.createClass(this, "TObject", null, function () {',
  4847. ' this.$init = function () {',
  4848. ' this.b = false;',
  4849. ' };',
  4850. ' this.$final = function () {',
  4851. ' };',
  4852. '});',
  4853. 'this.p = null;',
  4854. 'this.bird = null;',
  4855. '']),
  4856. LinesToStr([
  4857. 'var $with = $mod.bird;',
  4858. '$mod.p = function (w) {',
  4859. ' $with.b = w > 2;',
  4860. '};',
  4861. '']));
  4862. end;
  4863. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4864. begin
  4865. StartProgram(false);
  4866. Add([
  4867. 'type',
  4868. ' TProc = reference to procedure;',
  4869. ' TObject = class',
  4870. ' b: boolean;',
  4871. ' end;',
  4872. 'procedure DoIt;',
  4873. 'var',
  4874. ' p: TProc;',
  4875. 'begin',
  4876. ' try',
  4877. ' except',
  4878. ' on E: TObject do',
  4879. ' p:=procedure',
  4880. ' begin',
  4881. ' E.b:=true;',
  4882. ' end;',
  4883. ' end;',
  4884. 'end;',
  4885. 'begin']);
  4886. ConvertProgram;
  4887. CheckSource('TestAnonymousProc_ExceptOn',
  4888. LinesToStr([ // statements
  4889. 'rtl.createClass(this, "TObject", null, function () {',
  4890. ' this.$init = function () {',
  4891. ' this.b = false;',
  4892. ' };',
  4893. ' this.$final = function () {',
  4894. ' };',
  4895. '});',
  4896. 'this.DoIt = function () {',
  4897. ' var p = null;',
  4898. ' try {} catch ($e) {',
  4899. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4900. ' var E = $e;',
  4901. ' p = function () {',
  4902. ' E.b = true;',
  4903. ' };',
  4904. ' } else throw $e',
  4905. ' };',
  4906. '};',
  4907. '']),
  4908. LinesToStr([
  4909. '']));
  4910. end;
  4911. procedure TTestModule.TestAnonymousProc_Nested;
  4912. begin
  4913. StartProgram(false);
  4914. Add([
  4915. 'type',
  4916. ' TProc = reference to procedure;',
  4917. ' TObject = class',
  4918. ' i: byte;',
  4919. ' procedure DoIt;',
  4920. ' end;',
  4921. 'procedure TObject.DoIt;',
  4922. 'var',
  4923. ' p: TProc;',
  4924. ' procedure Sub;',
  4925. ' begin',
  4926. ' p:=procedure',
  4927. ' begin',
  4928. ' i:=3;',
  4929. ' Self.i:=4;',
  4930. ' p:=procedure',
  4931. ' procedure SubSub;',
  4932. ' begin',
  4933. ' i:=13;',
  4934. ' Self.i:=14;',
  4935. ' end;',
  4936. ' begin',
  4937. ' i:=13;',
  4938. ' Self.i:=14;',
  4939. ' end;',
  4940. ' end;',
  4941. ' end;',
  4942. 'begin',
  4943. 'end;',
  4944. 'begin']);
  4945. ConvertProgram;
  4946. CheckSource('TestAnonymousProc_Nested',
  4947. LinesToStr([ // statements
  4948. 'rtl.createClass(this, "TObject", null, function () {',
  4949. ' this.$init = function () {',
  4950. ' this.i = 0;',
  4951. ' };',
  4952. ' this.$final = function () {',
  4953. ' };',
  4954. ' this.DoIt = function () {',
  4955. ' var $Self = this;',
  4956. ' var p = null;',
  4957. ' function Sub() {',
  4958. ' p = function () {',
  4959. ' $Self.i = 3;',
  4960. ' $Self.i = 4;',
  4961. ' p = function () {',
  4962. ' function SubSub() {',
  4963. ' $Self.i = 13;',
  4964. ' $Self.i = 14;',
  4965. ' };',
  4966. ' $Self.i = 13;',
  4967. ' $Self.i = 14;',
  4968. ' };',
  4969. ' };',
  4970. ' };',
  4971. ' };',
  4972. '});',
  4973. '']),
  4974. LinesToStr([
  4975. '']));
  4976. end;
  4977. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4978. begin
  4979. StartProgram(false);
  4980. Add([
  4981. 'type',
  4982. ' TProc = reference to procedure;',
  4983. 'function DoIt: TProc;',
  4984. ' function Sub: TProc;',
  4985. ' begin',
  4986. ' Result:=procedure',
  4987. ' begin',
  4988. ' Sub:=procedure',
  4989. ' procedure SubSub;',
  4990. ' begin',
  4991. ' Result:=nil;',
  4992. ' Sub:=nil;',
  4993. ' DoIt:=nil;',
  4994. ' end;',
  4995. ' begin',
  4996. ' Result:=nil;',
  4997. ' Sub:=nil;',
  4998. ' DoIt:=nil;',
  4999. ' end;',
  5000. ' end;',
  5001. ' end;',
  5002. 'begin',
  5003. 'end;',
  5004. 'begin']);
  5005. ConvertProgram;
  5006. CheckSource('TestAnonymousProc_NestedAssignResult',
  5007. LinesToStr([ // statements
  5008. 'this.DoIt = function () {',
  5009. ' var Result = null;',
  5010. ' function Sub() {',
  5011. ' var Result$1 = null;',
  5012. ' Result$1 = function () {',
  5013. ' Result$1 = function () {',
  5014. ' function SubSub() {',
  5015. ' Result$1 = null;',
  5016. ' Result$1 = null;',
  5017. ' Result = null;',
  5018. ' };',
  5019. ' Result$1 = null;',
  5020. ' Result$1 = null;',
  5021. ' Result = null;',
  5022. ' };',
  5023. ' };',
  5024. ' return Result$1;',
  5025. ' };',
  5026. ' return Result;',
  5027. '};',
  5028. '']),
  5029. LinesToStr([
  5030. '']));
  5031. end;
  5032. procedure TTestModule.TestAnonymousProc_Class;
  5033. begin
  5034. StartProgram(false);
  5035. Add([
  5036. 'type',
  5037. ' TProc = reference to procedure;',
  5038. ' TEvent = procedure of object;',
  5039. ' TObject = class',
  5040. ' Size: word;',
  5041. ' function GetIt: TProc;',
  5042. ' procedure DoIt; virtual; abstract;',
  5043. ' end;',
  5044. 'function TObject.GetIt: TProc;',
  5045. 'begin',
  5046. ' Result:=procedure',
  5047. ' var p: TEvent;',
  5048. ' begin',
  5049. ' Size:=Size;',
  5050. ' Size:=Self.Size;',
  5051. ' p:=@DoIt;',
  5052. ' p:[email protected];',
  5053. ' end;',
  5054. 'end;',
  5055. 'begin']);
  5056. ConvertProgram;
  5057. CheckSource('TestAnonymousProc_Class',
  5058. LinesToStr([ // statements
  5059. 'rtl.createClass(this, "TObject", null, function () {',
  5060. ' this.$init = function () {',
  5061. ' this.Size = 0;',
  5062. ' };',
  5063. ' this.$final = function () {',
  5064. ' };',
  5065. ' this.GetIt = function () {',
  5066. ' var $Self = this;',
  5067. ' var Result = null;',
  5068. ' Result = function () {',
  5069. ' var p = null;',
  5070. ' $Self.Size = $Self.Size;',
  5071. ' $Self.Size = $Self.Size;',
  5072. ' p = rtl.createCallback($Self, "DoIt");',
  5073. ' p = rtl.createCallback($Self, "DoIt");',
  5074. ' };',
  5075. ' return Result;',
  5076. ' };',
  5077. '});',
  5078. '']),
  5079. LinesToStr([
  5080. '']));
  5081. end;
  5082. procedure TTestModule.TestAnonymousProc_ForLoop;
  5083. begin
  5084. StartProgram(false);
  5085. Add([
  5086. 'type TProc = reference to procedure;',
  5087. 'procedure Foo(p: TProc);',
  5088. 'begin',
  5089. 'end;',
  5090. 'procedure DoIt;',
  5091. 'var i: word;',
  5092. ' a: word;',
  5093. 'begin',
  5094. ' for i:=1 to 10 do begin',
  5095. ' Foo(procedure begin a:=3; end);',
  5096. ' end;',
  5097. 'end;',
  5098. 'begin',
  5099. ' DoIt;']);
  5100. ConvertProgram;
  5101. CheckSource('TestAnonymousProc_ForLoop',
  5102. LinesToStr([ // statements
  5103. 'this.Foo = function (p) {',
  5104. '};',
  5105. 'this.DoIt = function () {',
  5106. ' var i = 0;',
  5107. ' var a = 0;',
  5108. ' for (i = 1; i <= 10; i++) {',
  5109. ' $mod.Foo(function () {',
  5110. ' a = 3;',
  5111. ' });',
  5112. ' };',
  5113. '};',
  5114. '']),
  5115. LinesToStr([
  5116. '$mod.DoIt();'
  5117. ]));
  5118. end;
  5119. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5120. begin
  5121. StartProgram(false);
  5122. Add([
  5123. '{$mode delphi}',
  5124. 'type',
  5125. ' TProc = reference to procedure;',
  5126. ' TFunc = reference to function(x: word): word;',
  5127. 'procedure Run;',
  5128. 'asm',
  5129. 'end;',
  5130. 'procedure Walk(p: TProc; f: TFunc);',
  5131. 'begin',
  5132. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5133. 'end;',
  5134. 'begin',
  5135. ' Walk(procedure',
  5136. ' asm',
  5137. ' console.log("a");',
  5138. ' end,',
  5139. ' function(x: word): word asm',
  5140. ' console.log("c");',
  5141. ' end);',
  5142. '']);
  5143. ConvertProgram;
  5144. CheckSource('TestAnonymousProc_AsmDelphi',
  5145. LinesToStr([ // statements
  5146. 'this.Run = function () {',
  5147. '};',
  5148. 'this.Walk = function (p, f) {',
  5149. ' $mod.Walk(function () {',
  5150. ' }, function (b) {',
  5151. ' return 1+b;',
  5152. ' });',
  5153. '};',
  5154. '']),
  5155. LinesToStr([
  5156. '$mod.Walk(function () {',
  5157. ' console.log("a");',
  5158. '}, function (x) {',
  5159. ' console.log("c");',
  5160. '});',
  5161. '']));
  5162. end;
  5163. procedure TTestModule.TestEnum_Name;
  5164. begin
  5165. StartProgram(false);
  5166. Add('type TMyEnum = (Red, Green, Blue);');
  5167. Add('var e: TMyEnum;');
  5168. Add('var f: TMyEnum = Blue;');
  5169. Add('begin');
  5170. Add(' e:=green;');
  5171. Add(' e:=default(TMyEnum);');
  5172. ConvertProgram;
  5173. CheckSource('TestEnum_Name',
  5174. LinesToStr([ // statements
  5175. 'this.TMyEnum = {',
  5176. ' "0":"Red",',
  5177. ' Red:0,',
  5178. ' "1":"Green",',
  5179. ' Green:1,',
  5180. ' "2":"Blue",',
  5181. ' Blue:2',
  5182. ' };',
  5183. 'this.e = 0;',
  5184. 'this.f = this.TMyEnum.Blue;'
  5185. ]),
  5186. LinesToStr([
  5187. '$mod.e=$mod.TMyEnum.Green;',
  5188. '$mod.e=$mod.TMyEnum.Red;'
  5189. ]));
  5190. end;
  5191. procedure TTestModule.TestEnum_Number;
  5192. begin
  5193. Converter.Options:=Converter.Options+[coEnumNumbers];
  5194. StartProgram(false);
  5195. Add('type TMyEnum = (Red, Green);');
  5196. Add('var');
  5197. Add(' e: TMyEnum;');
  5198. Add(' f: TMyEnum = Green;');
  5199. Add(' i: longint;');
  5200. Add('begin');
  5201. Add(' e:=green;');
  5202. Add(' i:=longint(e);');
  5203. ConvertProgram;
  5204. CheckSource('TestEnumNumber',
  5205. LinesToStr([ // statements
  5206. 'this.TMyEnum = {',
  5207. ' "0":"Red",',
  5208. ' Red:0,',
  5209. ' "1":"Green",',
  5210. ' Green:1',
  5211. ' };',
  5212. 'this.e = 0;',
  5213. 'this.f = 1;',
  5214. 'this.i = 0;'
  5215. ]),
  5216. LinesToStr([
  5217. '$mod.e=1;',
  5218. '$mod.i=$mod.e;'
  5219. ]));
  5220. end;
  5221. procedure TTestModule.TestEnum_ConstFail;
  5222. begin
  5223. StartProgram(false);
  5224. Add([
  5225. 'type TMyEnum = (Red = 100, Green = 101);',
  5226. 'var',
  5227. ' e: TMyEnum;',
  5228. ' f: TMyEnum = Green;',
  5229. 'begin',
  5230. ' e:=green;']);
  5231. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5232. ConvertProgram;
  5233. end;
  5234. procedure TTestModule.TestEnum_Functions;
  5235. begin
  5236. StartProgram(false);
  5237. Add([
  5238. 'type TMyEnum = (Red, Green);',
  5239. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5240. 'var',
  5241. ' v: longint;',
  5242. ' s: string;',
  5243. 'begin',
  5244. ' val(s,e,v);',
  5245. ' val(s,e,i);',
  5246. 'end;',
  5247. 'var',
  5248. ' e: TMyEnum;',
  5249. ' i: longint;',
  5250. ' s: string;',
  5251. ' b: boolean;',
  5252. 'begin',
  5253. ' i:=ord(red);',
  5254. ' i:=ord(green);',
  5255. ' i:=ord(e);',
  5256. ' i:=ord(b);',
  5257. ' e:=low(tmyenum);',
  5258. ' e:=low(e);',
  5259. ' b:=low(boolean);',
  5260. ' e:=high(tmyenum);',
  5261. ' e:=high(e);',
  5262. ' b:=high(boolean);',
  5263. ' e:=pred(green);',
  5264. ' e:=pred(e);',
  5265. ' b:=pred(b);',
  5266. ' e:=succ(red);',
  5267. ' e:=succ(e);',
  5268. ' b:=succ(b);',
  5269. ' e:=tmyenum(1);',
  5270. ' e:=tmyenum(i);',
  5271. ' s:=str(e);',
  5272. ' str(e,s);',
  5273. ' str(red,s);',
  5274. ' s:=str(e:3);',
  5275. ' writestr(s,e:3,red);',
  5276. ' val(s,e,i);',
  5277. ' i:=longint(e);']);
  5278. ConvertProgram;
  5279. CheckSource('TestEnum_Functions',
  5280. LinesToStr([ // statements
  5281. 'this.TMyEnum = {',
  5282. ' "0":"Red",',
  5283. ' Red:0,',
  5284. ' "1":"Green",',
  5285. ' Green:1',
  5286. ' };',
  5287. 'this.DoIt = function (e, i) {',
  5288. ' var v = 0;',
  5289. ' var s = "";',
  5290. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5291. ' v = w;',
  5292. ' }));',
  5293. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5294. '};',
  5295. 'this.e = 0;',
  5296. 'this.i = 0;',
  5297. 'this.s = "";',
  5298. 'this.b = false;',
  5299. '']),
  5300. LinesToStr([
  5301. '$mod.i=$mod.TMyEnum.Red;',
  5302. '$mod.i=$mod.TMyEnum.Green;',
  5303. '$mod.i=$mod.e;',
  5304. '$mod.i=$mod.b+0;',
  5305. '$mod.e=$mod.TMyEnum.Red;',
  5306. '$mod.e=$mod.TMyEnum.Red;',
  5307. '$mod.b=false;',
  5308. '$mod.e=$mod.TMyEnum.Green;',
  5309. '$mod.e=$mod.TMyEnum.Green;',
  5310. '$mod.b=true;',
  5311. '$mod.e=$mod.TMyEnum.Green-1;',
  5312. '$mod.e=$mod.e-1;',
  5313. '$mod.b=false;',
  5314. '$mod.e=$mod.TMyEnum.Red+1;',
  5315. '$mod.e=$mod.e+1;',
  5316. '$mod.b=true;',
  5317. '$mod.e=1;',
  5318. '$mod.e=$mod.i;',
  5319. '$mod.s = $mod.TMyEnum[$mod.e];',
  5320. '$mod.s = $mod.TMyEnum[$mod.e];',
  5321. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5322. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5323. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5324. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5325. ' $mod.i = v;',
  5326. '});',
  5327. '$mod.i=$mod.e;',
  5328. '']));
  5329. end;
  5330. procedure TTestModule.TestEnumRg_Functions;
  5331. begin
  5332. StartProgram(false);
  5333. Add([
  5334. 'type',
  5335. ' TEnum = (Red, Green, Blue);',
  5336. ' TEnumRg = Green..Blue;',
  5337. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5338. 'var',
  5339. ' v: longint;',
  5340. ' s: string;',
  5341. 'begin',
  5342. ' val(s,e,v);',
  5343. ' val(s,e,i);',
  5344. 'end;',
  5345. 'var',
  5346. ' e: TEnumRg;',
  5347. ' i: longint;',
  5348. ' s: string;',
  5349. 'begin',
  5350. ' i:=ord(green);',
  5351. ' i:=ord(e);',
  5352. ' e:=low(tenumrg);',
  5353. ' e:=low(e);',
  5354. ' e:=high(tenumrg);',
  5355. ' e:=high(e);',
  5356. ' e:=pred(blue);',
  5357. ' e:=pred(e);',
  5358. ' e:=succ(green);',
  5359. ' e:=succ(e);',
  5360. ' e:=tenumrg(1);',
  5361. ' e:=tenumrg(i);',
  5362. ' s:=str(e);',
  5363. ' str(e,s);',
  5364. ' str(red,s);',
  5365. ' s:=str(e:3);',
  5366. ' writestr(s,e:3,blue);',
  5367. ' val(s,e,i);',
  5368. ' i:=longint(e);']);
  5369. ConvertProgram;
  5370. CheckSource('TestEnumRg_Functions',
  5371. LinesToStr([ // statements
  5372. 'this.TEnum = {',
  5373. ' "0":"Red",',
  5374. ' Red:0,',
  5375. ' "1":"Green",',
  5376. ' Green:1,',
  5377. ' "2":"Blue",',
  5378. ' Blue:2',
  5379. ' };',
  5380. 'this.DoIt = function (e, i) {',
  5381. ' var v = 0;',
  5382. ' var s = "";',
  5383. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5384. ' v = w;',
  5385. ' }));',
  5386. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5387. '};',
  5388. 'this.e = this.TEnum.Green;',
  5389. 'this.i = 0;',
  5390. 'this.s = "";',
  5391. '']),
  5392. LinesToStr([
  5393. '$mod.i=$mod.TEnum.Green;',
  5394. '$mod.i=$mod.e;',
  5395. '$mod.e=$mod.TEnum.Green;',
  5396. '$mod.e=$mod.TEnum.Green;',
  5397. '$mod.e=$mod.TEnum.Blue;',
  5398. '$mod.e=$mod.TEnum.Blue;',
  5399. '$mod.e=$mod.TEnum.Blue-1;',
  5400. '$mod.e=$mod.e-1;',
  5401. '$mod.e=$mod.TEnum.Green+1;',
  5402. '$mod.e=$mod.e+1;',
  5403. '$mod.e=1;',
  5404. '$mod.e=$mod.i;',
  5405. '$mod.s = $mod.TEnum[$mod.e];',
  5406. '$mod.s = $mod.TEnum[$mod.e];',
  5407. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5408. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5409. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5410. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5411. ' $mod.i = v;',
  5412. '});',
  5413. '$mod.i=$mod.e;',
  5414. '']));
  5415. end;
  5416. procedure TTestModule.TestEnum_AsParams;
  5417. begin
  5418. StartProgram(false);
  5419. Add('type TEnum = (Red,Blue);');
  5420. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5421. Add('var vJ: TEnum;');
  5422. Add('begin');
  5423. Add(' vg:=vg;');
  5424. Add(' vj:=vh;');
  5425. Add(' vi:=vi;');
  5426. Add(' doit(vg,vg,vg);');
  5427. Add(' doit(vh,vh,vj);');
  5428. Add(' doit(vi,vi,vi);');
  5429. Add(' doit(vj,vj,vj);');
  5430. Add('end;');
  5431. Add('var i: TEnum;');
  5432. Add('begin');
  5433. Add(' doit(i,i,i);');
  5434. ConvertProgram;
  5435. CheckSource('TestEnum_AsParams',
  5436. LinesToStr([ // statements
  5437. 'this.TEnum = {',
  5438. ' "0": "Red",',
  5439. ' Red: 0,',
  5440. ' "1": "Blue",',
  5441. ' Blue: 1',
  5442. '};',
  5443. 'this.DoIt = function (vG,vH,vI) {',
  5444. ' var vJ = 0;',
  5445. ' vG = vG;',
  5446. ' vJ = vH;',
  5447. ' vI.set(vI.get());',
  5448. ' $mod.DoIt(vG, vG, {',
  5449. ' get: function () {',
  5450. ' return vG;',
  5451. ' },',
  5452. ' set: function (v) {',
  5453. ' vG = v;',
  5454. ' }',
  5455. ' });',
  5456. ' $mod.DoIt(vH, vH, {',
  5457. ' get: function () {',
  5458. ' return vJ;',
  5459. ' },',
  5460. ' set: function (v) {',
  5461. ' vJ = v;',
  5462. ' }',
  5463. ' });',
  5464. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5465. ' $mod.DoIt(vJ, vJ, {',
  5466. ' get: function () {',
  5467. ' return vJ;',
  5468. ' },',
  5469. ' set: function (v) {',
  5470. ' vJ = v;',
  5471. ' }',
  5472. ' });',
  5473. '};',
  5474. 'this.i = 0;'
  5475. ]),
  5476. LinesToStr([
  5477. '$mod.DoIt($mod.i,$mod.i,{',
  5478. ' p: $mod,',
  5479. ' get: function () {',
  5480. ' return this.p.i;',
  5481. ' },',
  5482. ' set: function (v) {',
  5483. ' this.p.i = v;',
  5484. ' }',
  5485. '});'
  5486. ]));
  5487. end;
  5488. procedure TTestModule.TestEnumRange_Array;
  5489. begin
  5490. StartProgram(false);
  5491. Add([
  5492. 'type',
  5493. ' TEnum = (Red, Green, Blue);',
  5494. ' TEnumRg = green..blue;',
  5495. ' TArr = array[TEnumRg] of byte;',
  5496. ' TArr2 = array[green..blue] of byte;',
  5497. 'var',
  5498. ' a: TArr;',
  5499. ' b: TArr = (3,4);',
  5500. ' c: TArr2 = (5,6);',
  5501. 'begin',
  5502. ' a[green] := b[blue];',
  5503. ' c[green] := c[blue];',
  5504. '']);
  5505. ConvertProgram;
  5506. CheckSource('TestEnumRange_Array',
  5507. LinesToStr([ // statements
  5508. 'this.TEnum = {',
  5509. ' "0": "Red",',
  5510. ' Red: 0,',
  5511. ' "1": "Green",',
  5512. ' Green: 1,',
  5513. ' "2": "Blue",',
  5514. ' Blue: 2',
  5515. '};',
  5516. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5517. 'this.b = [3, 4];',
  5518. 'this.c = [5, 6];',
  5519. '']),
  5520. LinesToStr([
  5521. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5522. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5523. '']));
  5524. end;
  5525. procedure TTestModule.TestEnum_ForIn;
  5526. begin
  5527. StartProgram(false);
  5528. Add([
  5529. 'type',
  5530. ' TEnum = (Red, Green, Blue);',
  5531. ' TEnumRg = green..blue;',
  5532. ' TArr = array[TEnum] of byte;',
  5533. ' TArrRg = array[TEnumRg] of byte;',
  5534. 'var',
  5535. ' e: TEnum;',
  5536. ' a1: TArr = (3,4,5);',
  5537. ' a2: TArrRg = (11,12);',
  5538. ' b: byte;',
  5539. 'begin',
  5540. ' for e in TEnum do ;',
  5541. ' for e in TEnumRg do ;',
  5542. ' for e in TArr do ;',
  5543. ' for e in TArrRg do ;',
  5544. ' for b in a1 do ;',
  5545. ' for b in a2 do ;',
  5546. '']);
  5547. ConvertProgram;
  5548. CheckSource('TestEnum_ForIn',
  5549. LinesToStr([ // statements
  5550. 'this.TEnum = {',
  5551. ' "0": "Red",',
  5552. ' Red: 0,',
  5553. ' "1": "Green",',
  5554. ' Green: 1,',
  5555. ' "2": "Blue",',
  5556. ' Blue: 2',
  5557. '};',
  5558. 'this.e = 0;',
  5559. 'this.a1 = [3, 4, 5];',
  5560. 'this.a2 = [11, 12];',
  5561. 'this.b = 0;',
  5562. '']),
  5563. LinesToStr([
  5564. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5565. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5566. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5567. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5568. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5569. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5570. '']));
  5571. end;
  5572. procedure TTestModule.TestEnum_ScopedNumber;
  5573. begin
  5574. Converter.Options:=Converter.Options+[coEnumNumbers];
  5575. StartProgram(false);
  5576. Add([
  5577. 'type',
  5578. ' TEnum = (Red, Green);',
  5579. 'var',
  5580. ' e: TEnum;',
  5581. 'begin',
  5582. ' e:=TEnum.Green;',
  5583. '']);
  5584. ConvertProgram;
  5585. CheckSource('TestEnum_ScopedNumber',
  5586. LinesToStr([ // statements
  5587. 'this.TEnum = {',
  5588. ' "0": "Red",',
  5589. ' Red: 0,',
  5590. ' "1": "Green",',
  5591. ' Green: 1',
  5592. '};',
  5593. 'this.e = 0;',
  5594. '']),
  5595. LinesToStr([
  5596. '$mod.e = 1;']));
  5597. end;
  5598. procedure TTestModule.TestEnum_InFunction;
  5599. begin
  5600. StartProgram(false);
  5601. Add([
  5602. 'const TEnum = 3;',
  5603. 'procedure DoIt;',
  5604. 'type',
  5605. ' TEnum = (Red, Green, Blue);',
  5606. ' procedure Sub;',
  5607. ' type',
  5608. ' TEnumSub = (Left, Right);',
  5609. ' var',
  5610. ' es: TEnumSub;',
  5611. ' begin',
  5612. ' es:=Left;',
  5613. ' end;',
  5614. 'var',
  5615. ' e, e2: TEnum;',
  5616. 'begin',
  5617. ' if e in [red,blue] then e2:=e;',
  5618. 'end;',
  5619. 'begin']);
  5620. ConvertProgram;
  5621. CheckSource('TestEnum_InFunction',
  5622. LinesToStr([ // statements
  5623. 'this.TEnum = 3;',
  5624. 'var TEnum$1 = {',
  5625. ' "0":"Red",',
  5626. ' Red:0,',
  5627. ' "1":"Green",',
  5628. ' Green:1,',
  5629. ' "2":"Blue",',
  5630. ' Blue:2',
  5631. ' };',
  5632. 'var TEnumSub = {',
  5633. ' "0": "Left",',
  5634. ' Left: 0,',
  5635. ' "1": "Right",',
  5636. ' Right: 1',
  5637. '};',
  5638. 'this.DoIt = function () {',
  5639. ' function Sub() {',
  5640. ' var es = 0;',
  5641. ' es = TEnumSub.Left;',
  5642. ' };',
  5643. ' var e = 0;',
  5644. ' var e2 = 0;',
  5645. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5646. '};',
  5647. '']),
  5648. LinesToStr([
  5649. '']));
  5650. end;
  5651. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5652. begin
  5653. StartUnit(true);
  5654. Add([
  5655. 'interface',
  5656. 'var color: (red, green);',
  5657. 'implementation',
  5658. 'initialization',
  5659. ' color:=green;',
  5660. '']);
  5661. ConvertUnit;
  5662. CheckSource('TestEnum_Name_Anonymous_Unit',
  5663. LinesToStr([
  5664. 'this.color$a = {',
  5665. ' "0": "red",',
  5666. ' red: 0,',
  5667. ' "1": "green",',
  5668. ' green: 1',
  5669. '};',
  5670. 'this.color = 0;',
  5671. '']),
  5672. LinesToStr([ // this.$init
  5673. '$mod.color = $mod.color$a.green;',
  5674. '']),
  5675. LinesToStr([ // implementation
  5676. '']) );
  5677. end;
  5678. procedure TTestModule.TestSet_Enum;
  5679. begin
  5680. StartProgram(false);
  5681. Add([
  5682. 'type',
  5683. ' TColor = (Red, Green, Blue);',
  5684. ' TColors = set of TColor;',
  5685. 'var',
  5686. ' c: TColor;',
  5687. ' s: TColors;',
  5688. ' t: TColors = [];',
  5689. ' u: TColors = [Red];',
  5690. 'begin',
  5691. ' s:=[];',
  5692. ' s:=[Green];',
  5693. ' s:=[Green,Blue];',
  5694. ' s:=[Red..Blue];',
  5695. ' s:=[Red,Green..Blue];',
  5696. ' s:=[Red,c];',
  5697. ' s:=t;',
  5698. ' s:=default(TColors);',
  5699. '']);
  5700. ConvertProgram;
  5701. CheckSource('TestSet',
  5702. LinesToStr([ // statements
  5703. 'this.TColor = {',
  5704. ' "0":"Red",',
  5705. ' Red:0,',
  5706. ' "1":"Green",',
  5707. ' Green:1,',
  5708. ' "2":"Blue",',
  5709. ' Blue:2',
  5710. ' };',
  5711. 'this.c = 0;',
  5712. 'this.s = {};',
  5713. 'this.t = {};',
  5714. 'this.u = rtl.createSet(this.TColor.Red);'
  5715. ]),
  5716. LinesToStr([
  5717. '$mod.s={};',
  5718. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5719. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5720. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5721. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5722. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5723. '$mod.s=rtl.refSet($mod.t);',
  5724. '$mod.s={};',
  5725. '']));
  5726. end;
  5727. procedure TTestModule.TestSet_Operators;
  5728. begin
  5729. StartProgram(false);
  5730. Add('type');
  5731. Add(' TColor = (Red, Green, Blue);');
  5732. Add(' TColors = set of tcolor;');
  5733. Add('var');
  5734. Add(' vC: TColor;');
  5735. Add(' vS: TColors;');
  5736. Add(' vT: TColors;');
  5737. Add(' vU: TColors;');
  5738. Add(' B: boolean;');
  5739. Add('begin');
  5740. Add(' include(vs,green);');
  5741. Add(' exclude(vs,vc);');
  5742. Add(' vs:=vt+vu;');
  5743. Add(' vs:=vt+[red];');
  5744. Add(' vs:=[red]+vt;');
  5745. Add(' vs:=[red]+[green];');
  5746. Add(' vs:=vt-vu;');
  5747. Add(' vs:=vt-[red];');
  5748. Add(' vs:=[red]-vt;');
  5749. Add(' vs:=[red]-[green];');
  5750. Add(' vs:=vt*vu;');
  5751. Add(' vs:=vt*[red];');
  5752. Add(' vs:=[red]*vt;');
  5753. Add(' vs:=[red]*[green];');
  5754. Add(' vs:=vt><vu;');
  5755. Add(' vs:=vt><[red];');
  5756. Add(' vs:=[red]><vt;');
  5757. Add(' vs:=[red]><[green];');
  5758. Add(' b:=vt=vu;');
  5759. Add(' b:=vt=[red];');
  5760. Add(' b:=[red]=vt;');
  5761. Add(' b:=[red]=[green];');
  5762. Add(' b:=vt<>vu;');
  5763. Add(' b:=vt<>[red];');
  5764. Add(' b:=[red]<>vt;');
  5765. Add(' b:=[red]<>[green];');
  5766. Add(' b:=vt<=vu;');
  5767. Add(' b:=vt<=[red];');
  5768. Add(' b:=[red]<=vt;');
  5769. Add(' b:=[red]<=[green];');
  5770. Add(' b:=vt>=vu;');
  5771. Add(' b:=vt>=[red];');
  5772. Add(' b:=[red]>=vt;');
  5773. Add(' b:=[red]>=[green];');
  5774. ConvertProgram;
  5775. CheckSource('TestSet_Operators',
  5776. LinesToStr([ // statements
  5777. 'this.TColor = {',
  5778. ' "0":"Red",',
  5779. ' Red:0,',
  5780. ' "1":"Green",',
  5781. ' Green:1,',
  5782. ' "2":"Blue",',
  5783. ' Blue:2',
  5784. ' };',
  5785. 'this.vC = 0;',
  5786. 'this.vS = {};',
  5787. 'this.vT = {};',
  5788. 'this.vU = {};',
  5789. 'this.B = false;'
  5790. ]),
  5791. LinesToStr([
  5792. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5793. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5794. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5795. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5796. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5797. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5798. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5799. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5800. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5801. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5802. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5803. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5804. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5805. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5806. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5807. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5808. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5809. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5810. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5811. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5812. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5813. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5814. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5815. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5816. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5817. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5818. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5819. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5820. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5821. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5822. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5823. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5824. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5825. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5826. '']));
  5827. end;
  5828. procedure TTestModule.TestSet_Operator_In;
  5829. begin
  5830. StartProgram(false);
  5831. Add([
  5832. 'type',
  5833. ' TColor = (Red, Green, Blue);',
  5834. ' TColors = set of tcolor;',
  5835. ' TColorRg = green..blue;',
  5836. 'var',
  5837. ' vC: tcolor;',
  5838. ' vT: tcolors;',
  5839. ' B: boolean;',
  5840. ' rg: TColorRg;',
  5841. 'begin',
  5842. ' b:=red in vt;',
  5843. ' b:=vc in vt;',
  5844. ' b:=green in [red..blue];',
  5845. ' b:=vc in [red..blue];',
  5846. ' ',
  5847. ' if red in vt then ;',
  5848. ' while vC in vt do ;',
  5849. ' repeat',
  5850. ' until vC in vt;',
  5851. ' if rg in [green..blue] then ;',
  5852. '']);
  5853. ConvertProgram;
  5854. CheckSource('TestSet_Operator_In',
  5855. LinesToStr([ // statements
  5856. 'this.TColor = {',
  5857. ' "0":"Red",',
  5858. ' Red:0,',
  5859. ' "1":"Green",',
  5860. ' Green:1,',
  5861. ' "2":"Blue",',
  5862. ' Blue:2',
  5863. ' };',
  5864. 'this.vC = 0;',
  5865. 'this.vT = {};',
  5866. 'this.B = false;',
  5867. 'this.rg = this.TColor.Green;',
  5868. '']),
  5869. LinesToStr([
  5870. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5871. '$mod.B = $mod.vC in $mod.vT;',
  5872. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5873. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5874. 'if ($mod.TColor.Red in $mod.vT) ;',
  5875. 'while ($mod.vC in $mod.vT) {',
  5876. '};',
  5877. 'do {',
  5878. '} while (!($mod.vC in $mod.vT));',
  5879. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5880. '']));
  5881. end;
  5882. procedure TTestModule.TestSet_Functions;
  5883. begin
  5884. StartProgram(false);
  5885. Add('type');
  5886. Add(' TMyEnum = (Red, Green);');
  5887. Add(' TMyEnums = set of TMyEnum;');
  5888. Add('var');
  5889. Add(' e: TMyEnum;');
  5890. Add(' s: TMyEnums;');
  5891. Add('begin');
  5892. Add(' e:=Low(TMyEnums);');
  5893. Add(' e:=Low(s);');
  5894. Add(' e:=High(TMyEnums);');
  5895. Add(' e:=High(s);');
  5896. ConvertProgram;
  5897. CheckSource('TestSetFunctions',
  5898. LinesToStr([ // statements
  5899. 'this.TMyEnum = {',
  5900. ' "0":"Red",',
  5901. ' Red:0,',
  5902. ' "1":"Green",',
  5903. ' Green:1',
  5904. ' };',
  5905. 'this.e = 0;',
  5906. 'this.s = {};'
  5907. ]),
  5908. LinesToStr([
  5909. '$mod.e=$mod.TMyEnum.Red;',
  5910. '$mod.e=$mod.TMyEnum.Red;',
  5911. '$mod.e=$mod.TMyEnum.Green;',
  5912. '$mod.e=$mod.TMyEnum.Green;',
  5913. '']));
  5914. end;
  5915. procedure TTestModule.TestSet_PassAsArgClone;
  5916. begin
  5917. StartProgram(false);
  5918. Add('type');
  5919. Add(' TMyEnum = (Red, Green);');
  5920. Add(' TMyEnums = set of TMyEnum;');
  5921. Add('procedure DoDefault(s: tmyenums); begin end;');
  5922. Add('procedure DoConst(const s: tmyenums); begin end;');
  5923. Add('var');
  5924. Add(' aSet: tmyenums;');
  5925. Add('begin');
  5926. Add(' dodefault(aset);');
  5927. Add(' doconst(aset);');
  5928. ConvertProgram;
  5929. CheckSource('TestSetFunctions',
  5930. LinesToStr([ // statements
  5931. 'this.TMyEnum = {',
  5932. ' "0":"Red",',
  5933. ' Red:0,',
  5934. ' "1":"Green",',
  5935. ' Green:1',
  5936. ' };',
  5937. 'this.DoDefault = function (s) {',
  5938. '};',
  5939. 'this.DoConst = function (s) {',
  5940. '};',
  5941. 'this.aSet = {};'
  5942. ]),
  5943. LinesToStr([
  5944. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5945. '$mod.DoConst($mod.aSet);',
  5946. '']));
  5947. end;
  5948. procedure TTestModule.TestSet_AsParams;
  5949. begin
  5950. StartProgram(false);
  5951. Add([
  5952. 'type TEnum = (Red,Blue);',
  5953. 'type TEnums = set of TEnum;',
  5954. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5955. 'var vJ: TEnums;',
  5956. 'begin',
  5957. ' Include(vg,red);',
  5958. ' Include(result,blue);',
  5959. ' vg:=vg;',
  5960. ' vj:=vh;',
  5961. ' vi:=vi;',
  5962. ' doit(vg,vg,vg);',
  5963. ' doit(vh,vh,vj);',
  5964. ' doit(vi,vi,vi);',
  5965. ' doit(vj,vj,vj);',
  5966. 'end;',
  5967. 'var i: TEnums;',
  5968. 'begin',
  5969. ' doit(i,i,i);']);
  5970. ConvertProgram;
  5971. CheckSource('TestSet_AsParams',
  5972. LinesToStr([ // statements
  5973. 'this.TEnum = {',
  5974. ' "0": "Red",',
  5975. ' Red: 0,',
  5976. ' "1": "Blue",',
  5977. ' Blue: 1',
  5978. '};',
  5979. 'this.DoIt = function (vG,vH,vI) {',
  5980. ' var Result = {};',
  5981. ' var vJ = {};',
  5982. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5983. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5984. ' vG = rtl.refSet(vG);',
  5985. ' vJ = rtl.refSet(vH);',
  5986. ' vI.set(rtl.refSet(vI.get()));',
  5987. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5988. ' get: function () {',
  5989. ' return vG;',
  5990. ' },',
  5991. ' set: function (v) {',
  5992. ' vG = v;',
  5993. ' }',
  5994. ' });',
  5995. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5996. ' get: function () {',
  5997. ' return vJ;',
  5998. ' },',
  5999. ' set: function (v) {',
  6000. ' vJ = v;',
  6001. ' }',
  6002. ' });',
  6003. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6004. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6005. ' get: function () {',
  6006. ' return vJ;',
  6007. ' },',
  6008. ' set: function (v) {',
  6009. ' vJ = v;',
  6010. ' }',
  6011. ' });',
  6012. ' return Result;',
  6013. '};',
  6014. 'this.i = {};'
  6015. ]),
  6016. LinesToStr([
  6017. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6018. ' p: $mod,',
  6019. ' get: function () {',
  6020. ' return this.p.i;',
  6021. ' },',
  6022. ' set: function (v) {',
  6023. ' this.p.i = v;',
  6024. ' }',
  6025. '});'
  6026. ]));
  6027. end;
  6028. procedure TTestModule.TestSet_Property;
  6029. begin
  6030. StartProgram(false);
  6031. Add('type');
  6032. Add(' TEnum = (Red,Blue);');
  6033. Add(' TEnums = set of TEnum;');
  6034. Add(' TObject = class');
  6035. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6036. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6037. Add(' property Colors: TEnums read GetColors write SetColors;');
  6038. Add(' end;');
  6039. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6040. Add('begin end;');
  6041. Add('var Obj: TObject;');
  6042. Add('begin');
  6043. Add(' Include(Obj.Colors,Red);');
  6044. Add(' Exclude(Obj.Colors,Red);');
  6045. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6046. ConvertProgram;
  6047. CheckSource('TestSet_Property',
  6048. LinesToStr([ // statements
  6049. 'this.TEnum = {',
  6050. ' "0": "Red",',
  6051. ' Red: 0,',
  6052. ' "1": "Blue",',
  6053. ' Blue: 1',
  6054. '};',
  6055. 'rtl.createClass(this, "TObject", null, function () {',
  6056. ' this.$init = function () {',
  6057. ' };',
  6058. ' this.$final = function () {',
  6059. ' };',
  6060. '});',
  6061. 'this.DoIt = function (i, j, k, l) {',
  6062. '};',
  6063. 'this.Obj = null;',
  6064. '']),
  6065. LinesToStr([
  6066. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6067. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6068. '']));
  6069. end;
  6070. procedure TTestModule.TestSet_EnumConst;
  6071. begin
  6072. StartProgram(false);
  6073. Add([
  6074. 'type',
  6075. ' TEnum = (Red,Blue);',
  6076. ' TEnums = set of TEnum;',
  6077. 'const',
  6078. ' Orange = red;',
  6079. 'var',
  6080. ' Enum: tenum;',
  6081. ' Enums: tenums;',
  6082. 'begin',
  6083. ' Include(enums,orange);',
  6084. ' Exclude(enums,orange);',
  6085. ' if orange in enums then;',
  6086. ' if orange in [orange,red] then;']);
  6087. ConvertProgram;
  6088. CheckSource('TestSet_EnumConst',
  6089. LinesToStr([ // statements
  6090. 'this.TEnum = {',
  6091. ' "0": "Red",',
  6092. ' Red: 0,',
  6093. ' "1": "Blue",',
  6094. ' Blue: 1',
  6095. '};',
  6096. 'this.Orange = this.TEnum.Red;',
  6097. 'this.Enum = 0;',
  6098. 'this.Enums = {};',
  6099. '']),
  6100. LinesToStr([
  6101. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6102. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6103. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6104. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6105. '']));
  6106. end;
  6107. procedure TTestModule.TestSet_IntConst;
  6108. begin
  6109. StartProgram(false);
  6110. Add([
  6111. 'type',
  6112. ' TEnums = set of Byte;',
  6113. 'const',
  6114. ' Orange = 0;',
  6115. 'var',
  6116. ' Enum: byte;',
  6117. ' Enums: tenums;',
  6118. 'begin',
  6119. ' Enums:=[];',
  6120. ' Enums:=[0];',
  6121. ' Enums:=[1..2];',
  6122. //' Include(enums,orange);',
  6123. //' Exclude(enums,orange);',
  6124. ' if orange in enums then;',
  6125. ' if orange in [orange,1] then;']);
  6126. ConvertProgram;
  6127. CheckSource('TestSet_IntConst',
  6128. LinesToStr([ // statements
  6129. 'this.Orange = 0;',
  6130. 'this.Enum = 0;',
  6131. 'this.Enums = {};',
  6132. '']),
  6133. LinesToStr([
  6134. '$mod.Enums = {};',
  6135. '$mod.Enums = rtl.createSet(0);',
  6136. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6137. 'if (0 in $mod.Enums) ;',
  6138. 'if (0 in rtl.createSet(0, 1)) ;',
  6139. '']));
  6140. end;
  6141. procedure TTestModule.TestSet_IntRange;
  6142. begin
  6143. StartProgram(false);
  6144. Add([
  6145. 'type',
  6146. ' TRange = 1..3;',
  6147. ' TEnums = set of TRange;',
  6148. 'const',
  6149. ' Orange = 2;',
  6150. 'var',
  6151. ' Enum: byte;',
  6152. ' Enums: TEnums;',
  6153. 'begin',
  6154. ' Enums:=[];',
  6155. ' Enums:=[1];',
  6156. ' Enums:=[2..3];',
  6157. ' Include(enums,orange);',
  6158. ' Exclude(enums,orange);',
  6159. ' if orange in enums then;',
  6160. ' if orange in [orange,1] then;']);
  6161. ConvertProgram;
  6162. CheckSource('TestSet_IntRange',
  6163. LinesToStr([ // statements
  6164. 'this.Orange = 2;',
  6165. 'this.Enum = 0;',
  6166. 'this.Enums = {};',
  6167. '']),
  6168. LinesToStr([
  6169. '$mod.Enums = {};',
  6170. '$mod.Enums = rtl.createSet(1);',
  6171. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6172. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6173. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6174. 'if (2 in $mod.Enums) ;',
  6175. 'if (2 in rtl.createSet(2, 1)) ;',
  6176. '']));
  6177. end;
  6178. procedure TTestModule.TestSet_AnonymousEnumType;
  6179. begin
  6180. StartProgram(false);
  6181. Add('type');
  6182. Add(' TFlags = set of (red, green);');
  6183. Add('const');
  6184. Add(' favorite = red;');
  6185. Add('var');
  6186. Add(' f: TFlags;');
  6187. Add(' i: longint;');
  6188. Add('begin');
  6189. Add(' Include(f,red);');
  6190. Add(' Include(f,favorite);');
  6191. Add(' i:=ord(red);');
  6192. Add(' i:=ord(favorite);');
  6193. Add(' i:=ord(low(TFlags));');
  6194. Add(' i:=ord(low(f));');
  6195. Add(' i:=ord(low(favorite));');
  6196. Add(' i:=ord(high(TFlags));');
  6197. Add(' i:=ord(high(f));');
  6198. Add(' i:=ord(high(favorite));');
  6199. Add(' f:=[green,favorite];');
  6200. ConvertProgram;
  6201. CheckSource('TestSet_AnonymousEnumType',
  6202. LinesToStr([ // statements
  6203. 'this.TFlags$a = {',
  6204. ' "0": "red",',
  6205. ' red: 0,',
  6206. ' "1": "green",',
  6207. ' green: 1',
  6208. '};',
  6209. 'this.favorite = this.TFlags$a.red;',
  6210. 'this.f = {};',
  6211. 'this.i = 0;',
  6212. '']),
  6213. LinesToStr([
  6214. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6215. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6216. '$mod.i = $mod.TFlags$a.red;',
  6217. '$mod.i = $mod.TFlags$a.red;',
  6218. '$mod.i = $mod.TFlags$a.red;',
  6219. '$mod.i = $mod.TFlags$a.red;',
  6220. '$mod.i = $mod.TFlags$a.red;',
  6221. '$mod.i = $mod.TFlags$a.green;',
  6222. '$mod.i = $mod.TFlags$a.green;',
  6223. '$mod.i = $mod.TFlags$a.green;',
  6224. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6225. '']));
  6226. end;
  6227. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6228. begin
  6229. exit;
  6230. StartProgram(false);
  6231. Add([
  6232. 'type',
  6233. ' TAtoZ = ''A''..''Z'';',
  6234. ' TSetOfAZ = set of TAtoZ;',
  6235. 'var',
  6236. ' c: char;',
  6237. ' a: TAtoZ;',
  6238. ' s: TSetOfAZ = [''P'',''A''];',
  6239. ' i: longint;',
  6240. 'begin',
  6241. ' Include(s,''S'');',
  6242. ' Include(s,c);',
  6243. ' Include(s,a);',
  6244. ' c:=low(TAtoZ);',
  6245. ' i:=ord(low(TAtoZ));',
  6246. ' a:=high(TAtoZ);',
  6247. ' a:=high(TSetOfAtoZ);',
  6248. ' s:=[a,c,''M''];',
  6249. '']);
  6250. ConvertProgram;
  6251. CheckSource('TestSet_AnonymousEnumTypeChar',
  6252. LinesToStr([ // statements
  6253. '']),
  6254. LinesToStr([
  6255. '']));
  6256. end;
  6257. procedure TTestModule.TestSet_ConstEnum;
  6258. begin
  6259. StartProgram(false);
  6260. Add([
  6261. 'type',
  6262. ' TEnum = (red,blue,green);',
  6263. ' TEnums = set of TEnum;',
  6264. 'const',
  6265. ' teAny = [low(TEnum)..high(TEnum)];',
  6266. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6267. 'var',
  6268. ' e: TEnum;',
  6269. ' s: TEnums;',
  6270. 'begin',
  6271. ' if blue in teAny then;',
  6272. ' if blue in teAny+[e] then;',
  6273. ' if blue in teAny+teRedBlue then;',
  6274. ' if e in [red,blue] then;',
  6275. ' s:=teAny;',
  6276. ' s:=teAny+[e];',
  6277. ' s:=[e]+teAny;',
  6278. ' s:=teAny+teRedBlue;',
  6279. ' s:=teAny+teRedBlue+[e];',
  6280. '']);
  6281. ConvertProgram;
  6282. CheckSource('TestSet_ConstEnum',
  6283. LinesToStr([ // statements
  6284. 'this.TEnum = {',
  6285. ' "0": "red",',
  6286. ' red: 0,',
  6287. ' "1": "blue",',
  6288. ' blue: 1,',
  6289. ' "2": "green",',
  6290. ' green: 2',
  6291. '};',
  6292. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6293. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6294. 'this.e = 0;',
  6295. 'this.s = {};',
  6296. '']),
  6297. LinesToStr([
  6298. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6299. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6300. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6301. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6302. '$mod.s = rtl.refSet($mod.teAny);',
  6303. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6304. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6305. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6306. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6307. '']));
  6308. end;
  6309. procedure TTestModule.TestSet_ConstChar;
  6310. begin
  6311. StartProgram(false);
  6312. Add([
  6313. 'const',
  6314. ' LowChars = [''a''..''z''];',
  6315. ' Chars = LowChars+[''A''..''Z''];',
  6316. ' sc = [''А'', ''Я''];',
  6317. 'var',
  6318. ' c: char;',
  6319. ' s: string;',
  6320. 'begin',
  6321. ' if c in lowchars then ;',
  6322. ' if ''a'' in lowchars then ;',
  6323. ' if s[1] in lowchars then ;',
  6324. ' if c in chars then ;',
  6325. ' if c in [''a''..''z'',''_''] then ;',
  6326. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6327. ' if ''Я'' in sc then ;',
  6328. ' if 3=ord('' '') then ;',
  6329. '']);
  6330. ConvertProgram;
  6331. CheckSource('TestSet_ConstChar',
  6332. LinesToStr([ // statements
  6333. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6334. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6335. 'this.sc = rtl.createSet(1040, 1071);',
  6336. 'this.c = "";',
  6337. 'this.s = "";',
  6338. '']),
  6339. LinesToStr([
  6340. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6341. 'if (97 in $mod.LowChars) ;',
  6342. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6343. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6344. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6345. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6346. 'if (1071 in $mod.sc) ;',
  6347. 'if (3 === 32) ;',
  6348. '']));
  6349. end;
  6350. procedure TTestModule.TestSet_ConstInt;
  6351. begin
  6352. StartProgram(false);
  6353. Add([
  6354. 'const',
  6355. ' Months = [1..12];',
  6356. ' Mirror = [-12..-1]+Months;',
  6357. 'var',
  6358. ' i: smallint;',
  6359. 'begin',
  6360. ' if 3 in Months then;',
  6361. ' if i in Months+[i] then;',
  6362. ' if i in Months+Mirror then;',
  6363. ' if i in [4..6,8] then;',
  6364. '']);
  6365. ConvertProgram;
  6366. CheckSource('TestSet_ConstInt',
  6367. LinesToStr([ // statements
  6368. 'this.Months = rtl.createSet(null, 1, 12);',
  6369. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6370. 'this.i = 0;',
  6371. '']),
  6372. LinesToStr([
  6373. 'if (3 in $mod.Months) ;',
  6374. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6375. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6376. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6377. '']));
  6378. end;
  6379. procedure TTestModule.TestSet_InFunction;
  6380. begin
  6381. StartProgram(false);
  6382. Add([
  6383. 'const',
  6384. ' TEnum = 3;',
  6385. ' TSetOfEnum = 4;',
  6386. ' TSetOfAno = 5;',
  6387. 'procedure DoIt;',
  6388. 'type',
  6389. ' TEnum = (red, blue);',
  6390. ' TSetOfEnum = set of TEnum;',
  6391. ' TSetOfAno = set of (up,down);',
  6392. 'var',
  6393. ' e: TEnum;',
  6394. ' se: TSetOfEnum;',
  6395. ' sa: TSetOfAno;',
  6396. 'begin',
  6397. ' se:=[e];',
  6398. ' sa:=[up];',
  6399. 'end;',
  6400. 'begin',
  6401. '']);
  6402. ConvertProgram;
  6403. CheckSource('TestSet_InFunction',
  6404. LinesToStr([ // statements
  6405. 'this.TEnum = 3;',
  6406. 'this.TSetOfEnum = 4;',
  6407. 'this.TSetOfAno = 5;',
  6408. 'var TEnum$1 = {',
  6409. ' "0": "red",',
  6410. ' red: 0,',
  6411. ' "1": "blue",',
  6412. ' blue: 1',
  6413. '};',
  6414. 'var TSetOfAno$a = {',
  6415. ' "0": "up",',
  6416. ' up: 0,',
  6417. ' "1": "down",',
  6418. ' down: 1',
  6419. '};',
  6420. 'this.DoIt = function () {',
  6421. ' var e = 0;',
  6422. ' var se = {};',
  6423. ' var sa = {};',
  6424. ' se = rtl.createSet(e);',
  6425. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6426. '};',
  6427. '']),
  6428. LinesToStr([
  6429. '']));
  6430. end;
  6431. procedure TTestModule.TestSet_ForIn;
  6432. begin
  6433. StartProgram(false);
  6434. Add([
  6435. 'type',
  6436. ' TEnum = (Red, Green, Blue);',
  6437. ' TEnumRg = green..blue;',
  6438. ' TSetOfEnum = set of TEnum;',
  6439. ' TSetOfEnumRg = set of TEnumRg;',
  6440. 'var',
  6441. ' e, e2: TEnum;',
  6442. ' er: TEnum;',
  6443. ' s: TSetOfEnum;',
  6444. 'begin',
  6445. ' for e in TSetOfEnum do ;',
  6446. ' for e in TSetOfEnumRg do ;',
  6447. ' for e in [] do e2:=e;',
  6448. ' for e in [red..green] do e2:=e;',
  6449. ' for e in [green,blue] do e2:=e;',
  6450. ' for e in [red,blue] do e2:=e;',
  6451. ' for e in s do e2:=e;',
  6452. ' for er in TSetOfEnumRg do ;',
  6453. '']);
  6454. ConvertProgram;
  6455. CheckSource('TestSet_ForIn',
  6456. LinesToStr([ // statements
  6457. 'this.TEnum = {',
  6458. ' "0":"Red",',
  6459. ' Red:0,',
  6460. ' "1":"Green",',
  6461. ' Green:1,',
  6462. ' "2":"Blue",',
  6463. ' Blue:2',
  6464. ' };',
  6465. 'this.e = 0;',
  6466. 'this.e2 = 0;',
  6467. 'this.er = 0;',
  6468. 'this.s = {};',
  6469. '']),
  6470. LinesToStr([
  6471. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6472. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6473. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6474. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6475. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6476. 'for (var $l in $mod.s){',
  6477. ' $mod.e = +$l;',
  6478. ' $mod.e2 = $mod.e;',
  6479. '};',
  6480. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6481. '']));
  6482. end;
  6483. procedure TTestModule.TestNestBegin;
  6484. begin
  6485. StartProgram(false);
  6486. Add('begin');
  6487. Add(' begin');
  6488. Add(' begin');
  6489. Add(' end;');
  6490. Add(' begin');
  6491. Add(' if true then ;');
  6492. Add(' end;');
  6493. Add(' end;');
  6494. ConvertProgram;
  6495. CheckSource('TestNestBegin',
  6496. '',
  6497. 'if (true) ;');
  6498. end;
  6499. procedure TTestModule.TestUnitImplVars;
  6500. begin
  6501. StartUnit(false);
  6502. Add('interface');
  6503. Add('implementation');
  6504. Add('var');
  6505. Add(' V1:longint;');
  6506. Add(' V2:longint = 3;');
  6507. Add(' V3:string = ''abc'';');
  6508. ConvertUnit;
  6509. CheckSource('TestUnitImplVars',
  6510. LinesToStr([ // statements
  6511. 'var $impl = $mod.$impl;',
  6512. '']),
  6513. '', // this.$init
  6514. LinesToStr([ // implementation
  6515. '$impl.V1 = 0;',
  6516. '$impl.V2 = 3;',
  6517. '$impl.V3 = "abc";',
  6518. '']) );
  6519. end;
  6520. procedure TTestModule.TestUnitImplConsts;
  6521. begin
  6522. StartUnit(false);
  6523. Add('interface');
  6524. Add('implementation');
  6525. Add('const');
  6526. Add(' v1 = 3;');
  6527. Add(' v2:longint = 4;');
  6528. Add(' v3:string = ''abc'';');
  6529. ConvertUnit;
  6530. CheckSource('TestUnitImplConsts',
  6531. LinesToStr([ // statements
  6532. 'var $impl = $mod.$impl;',
  6533. '']),
  6534. '', // this.$init
  6535. LinesToStr([ // implementation
  6536. '$impl.v1 = 3;',
  6537. '$impl.v2 = 4;',
  6538. '$impl.v3 = "abc";',
  6539. '']) );
  6540. end;
  6541. procedure TTestModule.TestUnitImplRecord;
  6542. begin
  6543. StartUnit(false);
  6544. Add('interface');
  6545. Add('implementation');
  6546. Add('type');
  6547. Add(' TMyRecord = record');
  6548. Add(' i: longint;');
  6549. Add(' end;');
  6550. Add('var aRec: TMyRecord;');
  6551. Add('initialization');
  6552. Add(' arec.i:=3;');
  6553. ConvertUnit;
  6554. CheckSource('TestUnitImplRecord',
  6555. LinesToStr([ // statements
  6556. 'var $impl = $mod.$impl;',
  6557. '']),
  6558. // this.$init
  6559. '$impl.aRec.i = 3;',
  6560. LinesToStr([ // implementation
  6561. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6562. ' this.i = 0;',
  6563. ' this.$eq = function (b) {',
  6564. ' return this.i === b.i;',
  6565. ' };',
  6566. ' this.$assign = function (s) {',
  6567. ' this.i = s.i;',
  6568. ' return this;',
  6569. ' };',
  6570. '});',
  6571. '$impl.aRec = $impl.TMyRecord.$new();',
  6572. '']) );
  6573. end;
  6574. procedure TTestModule.TestRenameJSNameConflict;
  6575. begin
  6576. StartProgram(false);
  6577. Add('var apply: longint;');
  6578. Add('var bind: longint;');
  6579. Add('var call: longint;');
  6580. Add('begin');
  6581. ConvertProgram;
  6582. CheckSource('TestRenameJSNameConflict',
  6583. LinesToStr([ // statements
  6584. 'this.Apply = 0;',
  6585. 'this.Bind = 0;',
  6586. 'this.Call = 0;'
  6587. ]),
  6588. LinesToStr([ // this.$main
  6589. ''
  6590. ]));
  6591. end;
  6592. procedure TTestModule.TestLocalConst;
  6593. begin
  6594. StartProgram(false);
  6595. Add('procedure DoIt;');
  6596. Add('const');
  6597. Add(' cA: longint = 1;');
  6598. Add(' cB = 2;');
  6599. Add(' procedure Sub;');
  6600. Add(' const');
  6601. Add(' csA = 3;');
  6602. Add(' cB: double = 4;');
  6603. Add(' begin');
  6604. Add(' cb:=cb+csa;');
  6605. Add(' ca:=ca+csa+5;');
  6606. Add(' end;');
  6607. Add('begin');
  6608. Add(' ca:=ca+cb+6;');
  6609. Add('end;');
  6610. Add('begin');
  6611. ConvertProgram;
  6612. CheckSource('TestLocalConst',
  6613. LinesToStr([
  6614. 'var cA = 1;',
  6615. 'var cB = 2;',
  6616. 'var csA = 3;',
  6617. 'var cB$1 = 4;',
  6618. 'this.DoIt = function () {',
  6619. ' function Sub() {',
  6620. ' cB$1 = cB$1 + 3;',
  6621. ' cA = cA + 3 + 5;',
  6622. ' };',
  6623. ' cA = cA + 2 + 6;',
  6624. '};'
  6625. ]),
  6626. LinesToStr([
  6627. ]));
  6628. end;
  6629. procedure TTestModule.TestVarExternal;
  6630. begin
  6631. StartProgram(false);
  6632. Add('var');
  6633. Add(' NaN: double; external name ''Global.NaN'';');
  6634. Add(' d: double;');
  6635. Add('begin');
  6636. Add(' d:=NaN;');
  6637. ConvertProgram;
  6638. CheckSource('TestVarExternal',
  6639. LinesToStr([
  6640. 'this.d = 0.0;'
  6641. ]),
  6642. LinesToStr([
  6643. '$mod.d = Global.NaN;'
  6644. ]));
  6645. end;
  6646. procedure TTestModule.TestVarExternalOtherUnit;
  6647. begin
  6648. AddModuleWithIntfImplSrc('unit2.pas',
  6649. LinesToStr([
  6650. 'var NaN: double; external name ''Global.NaN'';',
  6651. 'var iV: longint;'
  6652. ]),
  6653. '');
  6654. StartUnit(true);
  6655. Add('interface');
  6656. Add('uses unit2;');
  6657. Add('implementation');
  6658. Add('var');
  6659. Add(' d: double;');
  6660. Add(' i: longint; external name ''$i'';');
  6661. Add('begin');
  6662. Add(' d:=nan;');
  6663. Add(' d:=uNit2.nan;');
  6664. Add(' d:=test1.d;');
  6665. Add(' i:=iv;');
  6666. Add(' i:=uNit2.iv;');
  6667. Add(' i:=test1.i;');
  6668. ConvertUnit;
  6669. CheckSource('TestVarExternalOtherUnit',
  6670. LinesToStr([
  6671. 'var $impl = $mod.$impl;',
  6672. '']),
  6673. LinesToStr([ // this.$init
  6674. '$impl.d = Global.NaN;',
  6675. '$impl.d = Global.NaN;',
  6676. '$impl.d = $impl.d;',
  6677. '$i = pas.unit2.iV;',
  6678. '$i = pas.unit2.iV;',
  6679. '$i = $i;',
  6680. '']),
  6681. LinesToStr([ // implementation
  6682. '$impl.d = 0.0;',
  6683. '']) );
  6684. end;
  6685. procedure TTestModule.TestVarAbsoluteFail;
  6686. begin
  6687. StartProgram(false);
  6688. Add([
  6689. 'var',
  6690. ' a: longint;',
  6691. ' b: longword absolute a;',
  6692. 'begin']);
  6693. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6694. ConvertProgram;
  6695. end;
  6696. procedure TTestModule.TestConstExternal;
  6697. begin
  6698. StartProgram(false);
  6699. Add([
  6700. 'const',
  6701. ' PI: double; external name ''Global.PI'';',
  6702. ' Tau = 2*pi;',
  6703. 'var d: double;',
  6704. 'begin',
  6705. ' d:=pi;',
  6706. ' d:=tau+pi;']);
  6707. ConvertProgram;
  6708. CheckSource('TestConstExternal',
  6709. LinesToStr([
  6710. 'this.Tau = 2*Global.PI;',
  6711. 'this.d = 0.0;'
  6712. ]),
  6713. LinesToStr([
  6714. '$mod.d = Global.PI;',
  6715. '$mod.d = $mod.Tau + Global.PI;'
  6716. ]));
  6717. end;
  6718. procedure TTestModule.TestDouble;
  6719. begin
  6720. StartProgram(false);
  6721. Add([
  6722. 'type',
  6723. ' TDateTime = double;',
  6724. 'const',
  6725. ' a = TDateTime(2.7);',
  6726. ' b = a + TDateTime(1.7);',
  6727. ' c = 0.9 + 0.1;',
  6728. ' f0_1 = 0.1;',
  6729. ' f0_3 = 0.3;',
  6730. ' fn0_1 = -0.1;',
  6731. ' fn0_3 = -0.3;',
  6732. ' fn0_003 = -0.003;',
  6733. ' fn0_123456789 = -0.123456789;',
  6734. ' fn300_0 = -300.0;',
  6735. ' fn123456_0 = -123456.0;',
  6736. ' fn1234567_8 = -1234567.8;',
  6737. ' fn12345678_9 = -12345678.9;',
  6738. ' f1_0En12 = 1E-12;',
  6739. ' fn1_0En12 = -1E-12;',
  6740. ' maxdouble = 1.7e+308;',
  6741. ' mindouble = -1.7e+308;',
  6742. ' MinSafeIntDouble = -$1fffffffffffff;',
  6743. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6744. ' MaxSafeIntDouble = $1fffffffffffff;',
  6745. ' DZeroResolution = 1E-12;',
  6746. ' Minus1 = -1E-12;',
  6747. ' EPS = 1E-9;',
  6748. ' DELTA = 0.001;',
  6749. ' Big = 129.789E+100;',
  6750. ' Test0_15 = 0.15;',
  6751. ' Test999 = 2.9999999999999;',
  6752. ' Test111999 = 211199999999999000.0;',
  6753. ' TestMinus111999 = -211199999999999000.0;',
  6754. 'var',
  6755. ' d: double = b;',
  6756. 'begin',
  6757. ' d:=1.0;',
  6758. ' d:=1.0/3.0;',
  6759. ' d:=1/3;',
  6760. ' d:=5.0E-324;',
  6761. ' d:=1.7E308;',
  6762. ' d:=001.00E00;',
  6763. ' d:=002.00E001;',
  6764. ' d:=003.000E000;',
  6765. ' d:=-004.00E-00;',
  6766. ' d:=-005.00E-001;',
  6767. ' d:=10**3;',
  6768. ' d:=10 mod 3;',
  6769. ' d:=10 div 3;',
  6770. ' d:=c;',
  6771. ' d:=f0_1;',
  6772. ' d:=f0_3;',
  6773. ' d:=fn0_1;',
  6774. ' d:=fn0_3;',
  6775. ' d:=fn0_003;',
  6776. ' d:=fn0_123456789;',
  6777. ' d:=fn300_0;',
  6778. ' d:=fn123456_0;',
  6779. ' d:=fn1234567_8;',
  6780. ' d:=fn12345678_9;',
  6781. ' d:=f1_0En12;',
  6782. ' d:=fn1_0En12;',
  6783. ' d:=maxdouble;',
  6784. ' d:=mindouble;',
  6785. ' d:=MinSafeIntDouble;',
  6786. ' d:=double(MinSafeIntDouble);',
  6787. ' d:=MinSafeIntDouble2;',
  6788. ' d:=double(MinSafeIntDouble2);',
  6789. ' d:=MaxSafeIntDouble;',
  6790. ' d:=default(double);',
  6791. '']);
  6792. ConvertProgram;
  6793. CheckSource('TestDouble',
  6794. LinesToStr([
  6795. 'this.a = 2.7;',
  6796. 'this.b = 2.7 + 1.7;',
  6797. 'this.c = 0.9 + 0.1;',
  6798. 'this.f0_1 = 0.1;',
  6799. 'this.f0_3 = 0.3;',
  6800. 'this.fn0_1 = -0.1;',
  6801. 'this.fn0_3 = -0.3;',
  6802. 'this.fn0_003 = -0.003;',
  6803. 'this.fn0_123456789 = -0.123456789;',
  6804. 'this.fn300_0 = -300.0;',
  6805. 'this.fn123456_0 = -123456.0;',
  6806. 'this.fn1234567_8 = -1234567.8;',
  6807. 'this.fn12345678_9 = -12345678.9;',
  6808. 'this.f1_0En12 = 1E-12;',
  6809. 'this.fn1_0En12 = -1E-12;',
  6810. 'this.maxdouble = 1.7e+308;',
  6811. 'this.mindouble = -1.7e+308;',
  6812. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6813. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6814. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6815. 'this.DZeroResolution = 1E-12;',
  6816. 'this.Minus1 = -1E-12;',
  6817. 'this.EPS = 1E-9;',
  6818. 'this.DELTA = 0.001;',
  6819. 'this.Big = 129.789E+100;',
  6820. 'this.Test0_15 = 0.15;',
  6821. 'this.Test999 = 2.9999999999999;',
  6822. 'this.Test111999 = 211199999999999000.0;',
  6823. 'this.TestMinus111999 = -211199999999999000.0;',
  6824. 'this.d = 4.4;'
  6825. ]),
  6826. LinesToStr([
  6827. '$mod.d = 1.0;',
  6828. '$mod.d = 1.0 / 3.0;',
  6829. '$mod.d = 1 / 3;',
  6830. '$mod.d = 5.0E-324;',
  6831. '$mod.d = 1.7E308;',
  6832. '$mod.d = 1.00E0;',
  6833. '$mod.d = 2.00E1;',
  6834. '$mod.d = 3.000E0;',
  6835. '$mod.d = -4.00E-0;',
  6836. '$mod.d = -5.00E-1;',
  6837. '$mod.d = Math.pow(10, 3);',
  6838. '$mod.d = 10 % 3;',
  6839. '$mod.d = rtl.trunc(10 / 3);',
  6840. '$mod.d = 1;',
  6841. '$mod.d = 0.1;',
  6842. '$mod.d = 0.3;',
  6843. '$mod.d = -0.1;',
  6844. '$mod.d = -0.3;',
  6845. '$mod.d = -0.003;',
  6846. '$mod.d = -0.123456789;',
  6847. '$mod.d = -300;',
  6848. '$mod.d = -123456;',
  6849. '$mod.d = -1234567.8;',
  6850. '$mod.d = -1.23456789E7;',
  6851. '$mod.d = 1E-12;',
  6852. '$mod.d = -1E-12;',
  6853. '$mod.d = 1.7E308;',
  6854. '$mod.d = -1.7E308;',
  6855. '$mod.d = -9007199254740991;',
  6856. '$mod.d = -9007199254740991;',
  6857. '$mod.d = -9.007199254740992E15;',
  6858. '$mod.d = -9.007199254740992E15;',
  6859. '$mod.d = 9007199254740991;',
  6860. '$mod.d = 0.0;',
  6861. '']));
  6862. end;
  6863. procedure TTestModule.TestInteger;
  6864. begin
  6865. StartProgram(false);
  6866. Add([
  6867. 'const',
  6868. ' MinInt = low(NativeInt);',
  6869. ' MaxInt = high(NativeInt);',
  6870. 'type',
  6871. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6872. 'const',
  6873. ' a = low(TMyInt)+High(TMyInt);',
  6874. 'var',
  6875. ' i: TMyInt;',
  6876. 'begin',
  6877. ' i:=-MinInt;',
  6878. ' i:=default(TMyInt);',
  6879. ' i:=low(i)+high(i);',
  6880. '']);
  6881. ConvertProgram;
  6882. CheckSource('TestIntegerRange',
  6883. LinesToStr([
  6884. 'this.MinInt = -9007199254740991;',
  6885. 'this.MaxInt = 9007199254740991;',
  6886. 'this.a = -9007199254740991 + 9007199254740991;',
  6887. 'this.i = 0;',
  6888. '']),
  6889. LinesToStr([
  6890. '$mod.i = - -9007199254740991;',
  6891. '$mod.i = -9007199254740991;',
  6892. '$mod.i = -9007199254740991 + 9007199254740991;',
  6893. '']));
  6894. end;
  6895. procedure TTestModule.TestIntegerRange;
  6896. begin
  6897. StartProgram(false);
  6898. Add([
  6899. 'const',
  6900. ' MinInt = -1;',
  6901. ' MaxInt = +1;',
  6902. 'type',
  6903. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6904. ' TInt2 = 1..3;',
  6905. 'const',
  6906. ' a = low(TMyInt)+High(TMyInt);',
  6907. ' b = low(TInt2)+High(TInt2);',
  6908. ' s1 = [1];',
  6909. ' s2 = [1,2];',
  6910. ' s3 = [1..3];',
  6911. ' s4 = [low(shortint)..high(shortint)];',
  6912. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6913. ' s6 = 1 in s2;',
  6914. 'var',
  6915. ' i: TMyInt;',
  6916. ' i2: TInt2;',
  6917. 'begin',
  6918. ' i:=i2;',
  6919. ' i:=default(TMyInt);',
  6920. ' if i=i2 then ;',
  6921. ' i:=ord(i2);',
  6922. '']);
  6923. ConvertProgram;
  6924. CheckSource('TestIntegerRange',
  6925. LinesToStr([
  6926. 'this.MinInt = -1;',
  6927. 'this.MaxInt = +1;',
  6928. 'this.a = -1 + 1;',
  6929. 'this.b = 1 + 3;',
  6930. 'this.s1 = rtl.createSet(1);',
  6931. 'this.s2 = rtl.createSet(1, 2);',
  6932. 'this.s3 = rtl.createSet(null, 1, 3);',
  6933. 'this.s4 = rtl.createSet(null, -128, 127);',
  6934. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6935. 'this.s6 = 1 in this.s2;',
  6936. 'this.i = 0;',
  6937. 'this.i2 = 0;',
  6938. '']),
  6939. LinesToStr([
  6940. '$mod.i = $mod.i2;',
  6941. '$mod.i = -1;',
  6942. 'if ($mod.i === $mod.i2) ;',
  6943. '$mod.i = $mod.i2;',
  6944. '']));
  6945. end;
  6946. procedure TTestModule.TestIntegerTypecasts;
  6947. begin
  6948. StartProgram(false);
  6949. Add([
  6950. 'var',
  6951. ' i: nativeint;',
  6952. ' b: byte;',
  6953. ' sh: shortint;',
  6954. ' w: word;',
  6955. ' sm: smallint;',
  6956. ' lw: longword;',
  6957. ' li: longint;',
  6958. 'begin',
  6959. ' b:=byte(i);',
  6960. ' sh:=shortint(i);',
  6961. ' w:=word(i);',
  6962. ' sm:=smallint(i);',
  6963. ' lw:=longword(i);',
  6964. ' li:=longint(i);',
  6965. '']);
  6966. ConvertProgram;
  6967. CheckSource('TestIntegerTypecasts',
  6968. LinesToStr([
  6969. 'this.i = 0;',
  6970. 'this.b = 0;',
  6971. 'this.sh = 0;',
  6972. 'this.w = 0;',
  6973. 'this.sm = 0;',
  6974. 'this.lw = 0;',
  6975. 'this.li = 0;',
  6976. '']),
  6977. LinesToStr([
  6978. '$mod.b = $mod.i & 255;',
  6979. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6980. '$mod.w = $mod.i & 65535;',
  6981. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6982. '$mod.lw = $mod.i >>> 0;',
  6983. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6984. '']));
  6985. end;
  6986. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6987. begin
  6988. StartProgram(false);
  6989. Add([
  6990. 'var',
  6991. ' i,j: nativeint;',
  6992. 'begin',
  6993. ' i:=i shr 0;',
  6994. ' i:=i shr 1;',
  6995. ' i:=i shr 3;',
  6996. ' i:=i shr 54;',
  6997. ' i:=j shr i;',
  6998. '']);
  6999. ConvertProgram;
  7000. CheckResolverUnexpectedHints;
  7001. CheckSource('TestInteger_BitwiseShrNativeInt',
  7002. LinesToStr([
  7003. 'this.i = 0;',
  7004. 'this.j = 0;',
  7005. '']),
  7006. LinesToStr([
  7007. '$mod.i = $mod.i;',
  7008. '$mod.i = Math.floor($mod.i / 2);',
  7009. '$mod.i = Math.floor($mod.i / 8);',
  7010. '$mod.i = 0;',
  7011. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7012. '']));
  7013. end;
  7014. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7015. begin
  7016. StartProgram(false);
  7017. Add([
  7018. 'var',
  7019. ' i: nativeint;',
  7020. 'begin',
  7021. ' i:=i shl 0;',
  7022. ' i:=i shl 54;',
  7023. ' i:=123456789012 shl 1;',
  7024. ' i:=i shl 1;',
  7025. '']);
  7026. ConvertProgram;
  7027. CheckResolverUnexpectedHints;
  7028. CheckSource('TestInteger_BitwiseShrNativeInt',
  7029. LinesToStr([
  7030. 'this.i = 0;',
  7031. '']),
  7032. LinesToStr([
  7033. '$mod.i = $mod.i;',
  7034. '$mod.i = 0;',
  7035. '$mod.i = 246913578024;',
  7036. '$mod.i = rtl.shl($mod.i, 1);',
  7037. '']));
  7038. end;
  7039. procedure TTestModule.TestInteger_SystemFunc;
  7040. begin
  7041. StartProgram(true);
  7042. Add([
  7043. 'var',
  7044. ' i: byte;',
  7045. ' s: string;',
  7046. 'begin',
  7047. ' system.inc(i);',
  7048. ' system.str(i,s);',
  7049. ' s:=system.str(i);',
  7050. ' i:=system.low(i);',
  7051. ' i:=system.high(i);',
  7052. ' i:=system.pred(i);',
  7053. ' i:=system.succ(i);',
  7054. ' i:=system.ord(i);',
  7055. '']);
  7056. ConvertProgram;
  7057. CheckResolverUnexpectedHints;
  7058. CheckSource('TestInteger_SystemFunc',
  7059. LinesToStr([
  7060. 'this.i = 0;',
  7061. 'this.s = "";',
  7062. '']),
  7063. LinesToStr([
  7064. '$mod.i += 1;',
  7065. '$mod.s = "" + $mod.i;',
  7066. '$mod.s = "" + $mod.i;',
  7067. '$mod.i = 0;',
  7068. '$mod.i = 255;',
  7069. '$mod.i = $mod.i - 1;',
  7070. '$mod.i = $mod.i + 1;',
  7071. '$mod.i = $mod.i;',
  7072. '']));
  7073. end;
  7074. procedure TTestModule.TestCurrency;
  7075. begin
  7076. StartProgram(false);
  7077. Add([
  7078. 'type',
  7079. ' TCoin = currency;',
  7080. 'const',
  7081. ' a = TCoin(2.7);',
  7082. ' b = a + TCoin(1.7);',
  7083. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7084. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7085. 'var',
  7086. ' c: TCoin = b;',
  7087. ' i: nativeint;',
  7088. ' d: double;',
  7089. ' j: jsvalue;',
  7090. 'function DoIt(c: currency): currency; begin end;',
  7091. 'function GetIt(d: double): double; begin end;',
  7092. 'procedure Write(v: jsvalue); begin end;',
  7093. 'begin',
  7094. ' c:=1.0;',
  7095. ' c:=0.1;',
  7096. ' c:=1.0/3.0;',
  7097. ' c:=1/3;',
  7098. ' c:=a;',
  7099. ' d:=c;',
  7100. ' c:=d;',
  7101. ' c:=currency(c);',
  7102. ' c:=currency(d);',
  7103. ' d:=double(c);',
  7104. ' c:=i;',
  7105. ' c:=currency(i);',
  7106. //' i:=c;', not allowed
  7107. ' i:=nativeint(c);',
  7108. ' c:=c+a;',
  7109. ' c:=-c-a;',
  7110. ' c:=d+c;',
  7111. ' c:=c+d;',
  7112. ' c:=d-c;',
  7113. ' c:=c-d;',
  7114. ' c:=c*a;',
  7115. ' c:=a*c;',
  7116. ' c:=d*c;',
  7117. ' c:=c*d;',
  7118. ' c:=c/a;',
  7119. ' c:=a/c;',
  7120. ' c:=d/c;',
  7121. ' c:=c/d;',
  7122. ' c:=c**a;',
  7123. ' c:=a**c;',
  7124. ' c:=d**c;',
  7125. ' c:=c**d;',
  7126. ' if c=c then ;',
  7127. ' if c=a then ;',
  7128. ' if a=c then ;',
  7129. ' if d=c then ;',
  7130. ' if c=d then ;',
  7131. ' c:=DoIt(c);',
  7132. ' c:=DoIt(i);',
  7133. ' c:=DoIt(d);',
  7134. ' c:=GetIt(c);',
  7135. ' j:=c;',
  7136. ' Write(c);',
  7137. ' c:=default(currency);',
  7138. ' j:=str(c);',
  7139. ' j:=str(c:0:3);',
  7140. '']);
  7141. ConvertProgram;
  7142. CheckSource('TestCurrency',
  7143. LinesToStr([
  7144. 'this.a = 27000;',
  7145. 'this.b = this.a + 17000;',
  7146. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7147. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7148. 'this.c = this.b;',
  7149. 'this.i = 0;',
  7150. 'this.d = 0.0;',
  7151. 'this.j = undefined;',
  7152. 'this.DoIt = function (c) {',
  7153. ' var Result = 0;',
  7154. ' return Result;',
  7155. '};',
  7156. 'this.GetIt = function (d) {',
  7157. ' var Result = 0.0;',
  7158. ' return Result;',
  7159. '};',
  7160. 'this.Write = function (v) {',
  7161. '};',
  7162. '']),
  7163. LinesToStr([
  7164. '$mod.c = 10000;',
  7165. '$mod.c = 1000;',
  7166. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7167. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7168. '$mod.c = $mod.a;',
  7169. '$mod.d = $mod.c / 10000;',
  7170. '$mod.c = rtl.trunc($mod.d * 10000);',
  7171. '$mod.c = $mod.c;',
  7172. '$mod.c = $mod.d * 10000;',
  7173. '$mod.d = $mod.c / 10000;',
  7174. '$mod.c = $mod.i * 10000;',
  7175. '$mod.c = $mod.i * 10000;',
  7176. '$mod.i = rtl.trunc($mod.c / 10000);',
  7177. '$mod.c = $mod.c + $mod.a;',
  7178. '$mod.c = -$mod.c - $mod.a;',
  7179. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7180. '$mod.c = $mod.c + ($mod.d * 10000);',
  7181. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7182. '$mod.c = $mod.c - ($mod.d * 10000);',
  7183. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7184. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7185. '$mod.c = $mod.d * $mod.c;',
  7186. '$mod.c = $mod.c * $mod.d;',
  7187. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7188. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7189. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7190. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7191. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7192. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7193. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7194. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7195. 'if ($mod.c === $mod.c) ;',
  7196. 'if ($mod.c === $mod.a) ;',
  7197. 'if ($mod.a === $mod.c) ;',
  7198. 'if (($mod.d * 10000) === $mod.c) ;',
  7199. 'if ($mod.c === ($mod.d * 10000)) ;',
  7200. '$mod.c = $mod.DoIt($mod.c);',
  7201. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7202. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7203. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7204. '$mod.j = $mod.c / 10000;',
  7205. '$mod.Write($mod.c / 10000);',
  7206. '$mod.c = 0;',
  7207. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7208. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7209. '']));
  7210. end;
  7211. procedure TTestModule.TestForBoolDo;
  7212. begin
  7213. StartProgram(false);
  7214. Add([
  7215. 'var b: boolean;',
  7216. 'begin',
  7217. ' for b:=false to true do ;',
  7218. ' for b:=b downto false do ;',
  7219. ' for b in boolean do ;',
  7220. '']);
  7221. ConvertProgram;
  7222. CheckSource('TestForBoolDo',
  7223. LinesToStr([ // statements
  7224. 'this.b = false;']),
  7225. LinesToStr([ // this.$main
  7226. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7227. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7228. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7229. '']));
  7230. end;
  7231. procedure TTestModule.TestForIntDo;
  7232. begin
  7233. StartProgram(false);
  7234. Add([
  7235. 'var i: longint;',
  7236. 'begin',
  7237. ' for i:=3 to 5 do ;',
  7238. ' for i:=i downto 2 do ;',
  7239. ' for i in byte do ;',
  7240. '']);
  7241. ConvertProgram;
  7242. CheckSource('TestForIntDo',
  7243. LinesToStr([ // statements
  7244. 'this.i = 0;']),
  7245. LinesToStr([ // this.$main
  7246. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7247. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7248. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7249. '']));
  7250. end;
  7251. procedure TTestModule.TestForIntInDo;
  7252. begin
  7253. StartProgram(false);
  7254. Add([
  7255. 'type',
  7256. ' TSetOfInt = set of byte;',
  7257. ' TIntRg = 3..7;',
  7258. ' TSetOfIntRg = set of TIntRg;',
  7259. 'var',
  7260. ' i,i2: longint;',
  7261. ' a1: array of byte;',
  7262. ' a2: array[1..3] of byte;',
  7263. ' soi: TSetOfInt;',
  7264. ' soir: TSetOfIntRg;',
  7265. ' ir: TIntRg;',
  7266. 'begin',
  7267. ' for i in byte do ;',
  7268. ' for i in a1 do ;',
  7269. ' for i in a2 do ;',
  7270. ' for i in [11..13] do ;',
  7271. ' for i in TSetOfInt do ;',
  7272. ' for i in TIntRg do ;',
  7273. ' for i in soi do i2:=i;',
  7274. ' for i in TSetOfIntRg do ;',
  7275. ' for i in soir do ;',
  7276. ' for ir in TIntRg do ;',
  7277. ' for ir in TSetOfIntRg do ;',
  7278. ' for ir in soir do ;',
  7279. '']);
  7280. ConvertProgram;
  7281. CheckSource('TestForIntInDo',
  7282. LinesToStr([ // statements
  7283. 'this.i = 0;',
  7284. 'this.i2 = 0;',
  7285. 'this.a1 = [];',
  7286. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7287. 'this.soi = {};',
  7288. 'this.soir = {};',
  7289. 'this.ir = 0;',
  7290. '']),
  7291. LinesToStr([ // this.$main
  7292. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7293. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7294. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7295. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7296. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7297. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7298. 'for (var $l6 in $mod.soi) {',
  7299. ' $mod.i = +$l6;',
  7300. ' $mod.i2 = $mod.i;',
  7301. '};',
  7302. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7303. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7304. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7305. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7306. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7307. '']));
  7308. end;
  7309. procedure TTestModule.TestCharConst;
  7310. begin
  7311. StartProgram(false);
  7312. Add([
  7313. 'const',
  7314. ' a = #$00F3;',
  7315. ' c: char = ''1'';',
  7316. ' wc: widechar = ''ä'';',
  7317. 'begin',
  7318. ' c:=#0;',
  7319. ' c:=#1;',
  7320. ' c:=#9;',
  7321. ' c:=#10;',
  7322. ' c:=#13;',
  7323. ' c:=#31;',
  7324. ' c:=#32;',
  7325. ' c:=#$A;',
  7326. ' c:=#$0A;',
  7327. ' c:=#$b;',
  7328. ' c:=#$0b;',
  7329. ' c:=^A;',
  7330. ' c:=''"'';',
  7331. ' c:=default(char);',
  7332. ' c:=#$00E4;', // ä
  7333. ' c:=''ä'';',
  7334. ' c:=#$E4;', // ä
  7335. ' c:=#$D800;', // invalid UTF-16
  7336. ' c:=#$DFFF;', // invalid UTF-16
  7337. ' c:=#$FFFF;', // last UCS-2
  7338. ' c:=high(c);', // last UCS-2
  7339. ' c:=#269;',
  7340. '']);
  7341. ConvertProgram;
  7342. CheckSource('TestCharConst',
  7343. LinesToStr([
  7344. 'this.a="ó";',
  7345. 'this.c="1";',
  7346. 'this.wc="ä";'
  7347. ]),
  7348. LinesToStr([
  7349. '$mod.c="\x00";',
  7350. '$mod.c="\x01";',
  7351. '$mod.c="\t";',
  7352. '$mod.c="\n";',
  7353. '$mod.c="\r";',
  7354. '$mod.c="\x1F";',
  7355. '$mod.c=" ";',
  7356. '$mod.c="\n";',
  7357. '$mod.c="\n";',
  7358. '$mod.c="\x0B";',
  7359. '$mod.c="\x0B";',
  7360. '$mod.c="\x01";',
  7361. '$mod.c=''"'';',
  7362. '$mod.c="\x00";',
  7363. '$mod.c = "ä";',
  7364. '$mod.c = "ä";',
  7365. '$mod.c = "ä";',
  7366. '$mod.c="\uD800";',
  7367. '$mod.c="\uDFFF";',
  7368. '$mod.c="\uFFFF";',
  7369. '$mod.c="\uFFFF";',
  7370. '$mod.c = "č";',
  7371. '']));
  7372. end;
  7373. procedure TTestModule.TestChar_Compare;
  7374. begin
  7375. StartProgram(false);
  7376. Add('var');
  7377. Add(' c: char;');
  7378. Add(' b: boolean;');
  7379. Add('begin');
  7380. Add(' b:=c=''1'';');
  7381. Add(' b:=''2''=c;');
  7382. Add(' b:=''3''=''4'';');
  7383. Add(' b:=c<>''5'';');
  7384. Add(' b:=''6''<>c;');
  7385. Add(' b:=c>''7'';');
  7386. Add(' b:=''8''>c;');
  7387. Add(' b:=c>=''9'';');
  7388. Add(' b:=''A''>=c;');
  7389. Add(' b:=c<''B'';');
  7390. Add(' b:=''C''<c;');
  7391. Add(' b:=c<=''D'';');
  7392. Add(' b:=''E''<=c;');
  7393. ConvertProgram;
  7394. CheckSource('TestChar_Compare',
  7395. LinesToStr([
  7396. 'this.c="";',
  7397. 'this.b = false;'
  7398. ]),
  7399. LinesToStr([
  7400. '$mod.b = $mod.c === "1";',
  7401. '$mod.b = "2" === $mod.c;',
  7402. '$mod.b = "3" === "4";',
  7403. '$mod.b = $mod.c !== "5";',
  7404. '$mod.b = "6" !== $mod.c;',
  7405. '$mod.b = $mod.c > "7";',
  7406. '$mod.b = "8" > $mod.c;',
  7407. '$mod.b = $mod.c >= "9";',
  7408. '$mod.b = "A" >= $mod.c;',
  7409. '$mod.b = $mod.c < "B";',
  7410. '$mod.b = "C" < $mod.c;',
  7411. '$mod.b = $mod.c <= "D";',
  7412. '$mod.b = "E" <= $mod.c;',
  7413. '']));
  7414. end;
  7415. procedure TTestModule.TestChar_BuiltInProcs;
  7416. begin
  7417. StartProgram(false);
  7418. Add([
  7419. 'var',
  7420. ' c: char;',
  7421. ' i: longint;',
  7422. ' s: string;',
  7423. 'begin',
  7424. ' i:=ord(c);',
  7425. ' i:=ord(s[i]);',
  7426. ' c:=chr(i);',
  7427. ' c:=pred(c);',
  7428. ' c:=succ(c);',
  7429. ' c:=low(c);',
  7430. ' c:=high(c);',
  7431. ' i:=byte(c);',
  7432. ' i:=word(c);',
  7433. ' i:=longint(c);',
  7434. '']);
  7435. ConvertProgram;
  7436. CheckSource('TestChar_BuiltInProcs',
  7437. LinesToStr([
  7438. 'this.c = "";',
  7439. 'this.i = 0;',
  7440. 'this.s = "";'
  7441. ]),
  7442. LinesToStr([
  7443. '$mod.i = $mod.c.charCodeAt();',
  7444. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7445. '$mod.c = String.fromCharCode($mod.i);',
  7446. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7447. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7448. '$mod.c = "\x00";',
  7449. '$mod.c = "\uFFFF";',
  7450. '$mod.i = $mod.c.charCodeAt() & 255;',
  7451. '$mod.i = $mod.c.charCodeAt();',
  7452. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7453. '']));
  7454. end;
  7455. procedure TTestModule.TestStringConst;
  7456. begin
  7457. StartProgram(false);
  7458. Add([
  7459. '{$H+}',
  7460. 'const',
  7461. ' a = #$00F3#$017C;', // first <256, then >=256
  7462. ' b = string(''a'');',
  7463. ' c = string(''ä'');',
  7464. ' d = UnicodeString(''b'');',
  7465. ' e = UnicodeString(''ö'');',
  7466. 'var',
  7467. ' s: string = ''abc'';',
  7468. 'begin',
  7469. ' s:='''';',
  7470. ' s:=#13#10;',
  7471. ' s:=#9''foo'';',
  7472. ' s:=#$A9;',
  7473. ' s:=''foo''#13''bar'';',
  7474. ' s:=''"'';',
  7475. ' s:=''"''''"'';',
  7476. ' s:=#$20AC;', // euro
  7477. ' s:=#$10437;', // outside BMP
  7478. ' s:=''abc''#$20AC;', // ascii,#
  7479. ' s:=''ä''#$20AC;', // non ascii,#
  7480. ' s:=#$20AC''abc'';', // #, ascii
  7481. ' s:=#$20AC''ä'';', // #, non ascii
  7482. ' s:=default(string);',
  7483. ' s:=concat(s);',
  7484. ' s:=concat(s,''a'',s);',
  7485. ' s:=#250#269;',
  7486. //' s:=#$2F804;',
  7487. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7488. '']);
  7489. ConvertProgram;
  7490. CheckSource('TestStringConst',
  7491. LinesToStr([
  7492. 'this.a = "óż";',
  7493. 'this.b = "a";',
  7494. 'this.c = "ä";',
  7495. 'this.d = "b";',
  7496. 'this.e = "ö";',
  7497. 'this.s="abc";',
  7498. '']),
  7499. LinesToStr([
  7500. '$mod.s="";',
  7501. '$mod.s="\r\n";',
  7502. '$mod.s="\tfoo";',
  7503. '$mod.s="©";',
  7504. '$mod.s="foo\rbar";',
  7505. '$mod.s=''"'';',
  7506. '$mod.s=''"\''"'';',
  7507. '$mod.s="€";',
  7508. '$mod.s="'#$F0#$90#$90#$B7'";',
  7509. '$mod.s = "abc€";',
  7510. '$mod.s = "ä€";',
  7511. '$mod.s = "€abc";',
  7512. '$mod.s = "ۊ";',
  7513. '$mod.s="";',
  7514. '$mod.s = $mod.s;',
  7515. '$mod.s = $mod.s.concat("a", $mod.s);',
  7516. '$mod.s = "úč";',
  7517. '']));
  7518. end;
  7519. procedure TTestModule.TestStringConst_InvalidUTF16;
  7520. begin
  7521. StartProgram(false);
  7522. Add([
  7523. 'const',
  7524. ' a: char = #$D87E;',
  7525. ' b: string = #$D87E;',
  7526. ' c: string = #$D87E#43;',
  7527. 'begin',
  7528. ' c:=''abc''#$D87E;',
  7529. ' c:=#0#1#2;',
  7530. ' c:=#127;',
  7531. ' c:=#128;',
  7532. ' c:=#255;',
  7533. ' c:=#256;',
  7534. '']);
  7535. ConvertProgram;
  7536. CheckSource('TestStringConst',
  7537. LinesToStr([
  7538. 'this.a = "\uD87E";',
  7539. 'this.b = "\uD87E";',
  7540. 'this.c = "\uD87E+";',
  7541. '']),
  7542. LinesToStr([
  7543. '$mod.c = "abc\uD87E";',
  7544. '$mod.c = "\x00\x01\x02";',
  7545. '$mod.c = "'#127'";',
  7546. '$mod.c = "'#$c2#$80'";',
  7547. '$mod.c = "'#$c3#$BF'";',
  7548. '$mod.c = "'#$c4#$80'";',
  7549. '']));
  7550. end;
  7551. procedure TTestModule.TestStringConstSurrogate;
  7552. begin
  7553. StartProgram(false);
  7554. Add([
  7555. 'var',
  7556. ' s: string;',
  7557. 'begin',
  7558. ' s:=''😊'';', // 1F60A
  7559. '']);
  7560. ConvertProgram;
  7561. CheckSource('TestStringConstSurrogate',
  7562. LinesToStr([
  7563. 'this.s="";'
  7564. ]),
  7565. LinesToStr([
  7566. '$mod.s="😊";'
  7567. ]));
  7568. end;
  7569. procedure TTestModule.TestString_Length;
  7570. begin
  7571. StartProgram(false);
  7572. Add('const c = ''foo'';');
  7573. Add('var');
  7574. Add(' s: string;');
  7575. Add(' i: longint;');
  7576. Add('begin');
  7577. Add(' i:=length(s);');
  7578. Add(' i:=length(s+s);');
  7579. Add(' i:=length(''abc'');');
  7580. Add(' i:=length(c);');
  7581. ConvertProgram;
  7582. CheckSource('TestString_Length',
  7583. LinesToStr([
  7584. 'this.c = "foo";',
  7585. 'this.s = "";',
  7586. 'this.i = 0;',
  7587. '']),
  7588. LinesToStr([
  7589. '$mod.i = $mod.s.length;',
  7590. '$mod.i = ($mod.s+$mod.s).length;',
  7591. '$mod.i = "abc".length;',
  7592. '$mod.i = $mod.c.length;',
  7593. '']));
  7594. end;
  7595. procedure TTestModule.TestString_Compare;
  7596. begin
  7597. StartProgram(false);
  7598. Add('var');
  7599. Add(' s, t: string;');
  7600. Add(' b: boolean;');
  7601. Add('begin');
  7602. Add(' b:=s=t;');
  7603. Add(' b:=s<>t;');
  7604. Add(' b:=s>t;');
  7605. Add(' b:=s>=t;');
  7606. Add(' b:=s<t;');
  7607. Add(' b:=s<=t;');
  7608. ConvertProgram;
  7609. CheckSource('TestString_Compare',
  7610. LinesToStr([ // statements
  7611. 'this.s = "";',
  7612. 'this.t = "";',
  7613. 'this.b =false;'
  7614. ]),
  7615. LinesToStr([ // this.$main
  7616. '$mod.b = $mod.s === $mod.t;',
  7617. '$mod.b = $mod.s !== $mod.t;',
  7618. '$mod.b = $mod.s > $mod.t;',
  7619. '$mod.b = $mod.s >= $mod.t;',
  7620. '$mod.b = $mod.s < $mod.t;',
  7621. '$mod.b = $mod.s <= $mod.t;',
  7622. '']));
  7623. end;
  7624. procedure TTestModule.TestString_SetLength;
  7625. begin
  7626. StartProgram(false);
  7627. Add([
  7628. 'procedure DoIt(var s: string);',
  7629. 'begin',
  7630. ' SetLength(s,2);',
  7631. 'end;',
  7632. 'var s: string;',
  7633. 'begin',
  7634. ' SetLength(s,3);',
  7635. '']);
  7636. ConvertProgram;
  7637. CheckSource('TestString_SetLength',
  7638. LinesToStr([ // statements
  7639. 'this.DoIt = function (s) {',
  7640. ' s.set(rtl.strSetLength(s.get(), 2));',
  7641. '};',
  7642. 'this.s = "";',
  7643. '']),
  7644. LinesToStr([ // this.$main
  7645. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7646. ]));
  7647. end;
  7648. procedure TTestModule.TestString_CharAt;
  7649. begin
  7650. StartProgram(false);
  7651. Add([
  7652. 'var',
  7653. ' s: string;',
  7654. ' c: char;',
  7655. ' b: boolean;',
  7656. 'begin',
  7657. ' b:= s[1] = c;',
  7658. ' b:= c = s[1];',
  7659. ' b:= c <> s[1];',
  7660. ' b:= c > s[1];',
  7661. ' b:= c >= s[1];',
  7662. ' b:= c < s[2];',
  7663. ' b:= c <= s[1];',
  7664. ' s[1] := c;',
  7665. ' s[2+3] := c;']);
  7666. ConvertProgram;
  7667. CheckSource('TestString_CharAt',
  7668. LinesToStr([ // statements
  7669. 'this.s = "";',
  7670. 'this.c = "";',
  7671. 'this.b = false;'
  7672. ]),
  7673. LinesToStr([ // this.$main
  7674. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7675. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7676. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7677. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7678. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7679. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7680. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7681. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7682. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7683. '']));
  7684. end;
  7685. procedure TTestModule.TestStringHMinusFail;
  7686. begin
  7687. StartProgram(false);
  7688. Add([
  7689. '{$H-}',
  7690. 'var s: string;',
  7691. 'begin']);
  7692. ConvertProgram;
  7693. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7694. end;
  7695. procedure TTestModule.TestStr;
  7696. begin
  7697. StartProgram(false);
  7698. Add('var');
  7699. Add(' b: boolean;');
  7700. Add(' i: longint;');
  7701. Add(' d: double;');
  7702. Add(' s: string;');
  7703. Add('begin');
  7704. Add(' str(b,s);');
  7705. Add(' str(i,s);');
  7706. Add(' str(d,s);');
  7707. Add(' str(i:3,s);');
  7708. Add(' str(d:3:2,s);');
  7709. Add(' Str(12.456:12:1,s);');
  7710. Add(' Str(12.456:12,s);');
  7711. Add(' s:=str(b);');
  7712. Add(' s:=str(i);');
  7713. Add(' s:=str(d);');
  7714. Add(' s:=str(i,i);');
  7715. Add(' s:=str(i:3);');
  7716. Add(' s:=str(d:3:2);');
  7717. Add(' s:=str(i:4,i);');
  7718. Add(' s:=str(i,i:5);');
  7719. Add(' s:=str(i:4,i:5);');
  7720. Add(' s:=str(s,s);');
  7721. Add(' s:=str(s,''foo'');');
  7722. ConvertProgram;
  7723. CheckSource('TestStr',
  7724. LinesToStr([ // statements
  7725. 'this.b = false;',
  7726. 'this.i = 0;',
  7727. 'this.d = 0.0;',
  7728. 'this.s = "";',
  7729. '']),
  7730. LinesToStr([ // this.$main
  7731. '$mod.s = ""+$mod.b;',
  7732. '$mod.s = ""+$mod.i;',
  7733. '$mod.s = rtl.floatToStr($mod.d);',
  7734. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7735. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7736. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7737. '$mod.s = rtl.floatToStr(12.456,12);',
  7738. '$mod.s = ""+$mod.b;',
  7739. '$mod.s = ""+$mod.i;',
  7740. '$mod.s = rtl.floatToStr($mod.d);',
  7741. '$mod.s = ""+$mod.i+$mod.i;',
  7742. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7743. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7744. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7745. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7746. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7747. '$mod.s = $mod.s + $mod.s;',
  7748. '$mod.s = $mod.s + "foo";',
  7749. '']));
  7750. end;
  7751. procedure TTestModule.TestBaseType_AnsiStringFail;
  7752. begin
  7753. StartProgram(false);
  7754. Add('var s: AnsiString');
  7755. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7756. ConvertProgram;
  7757. end;
  7758. procedure TTestModule.TestBaseType_WideStringFail;
  7759. begin
  7760. StartProgram(false);
  7761. Add('var s: WideString');
  7762. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7763. ConvertProgram;
  7764. end;
  7765. procedure TTestModule.TestBaseType_ShortStringFail;
  7766. begin
  7767. StartProgram(false);
  7768. Add('var s: ShortString');
  7769. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7770. ConvertProgram;
  7771. end;
  7772. procedure TTestModule.TestBaseType_RawByteStringFail;
  7773. begin
  7774. StartProgram(false);
  7775. Add('var s: RawByteString');
  7776. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7777. ConvertProgram;
  7778. end;
  7779. procedure TTestModule.TestTypeShortstring_Fail;
  7780. begin
  7781. StartProgram(false);
  7782. Add('type t = string[12];');
  7783. Add('var s: t;');
  7784. Add('begin');
  7785. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7786. ConvertProgram;
  7787. end;
  7788. procedure TTestModule.TestCharSet_Custom;
  7789. begin
  7790. StartProgram(false);
  7791. Add([
  7792. 'type',
  7793. ' TCharRg = ''a''..''z'';',
  7794. ' TSetOfCharRg = set of TCharRg;',
  7795. ' TCharRg2 = ''m''..''p'';',
  7796. 'const',
  7797. ' crg: TCharRg = ''b'';',
  7798. 'var',
  7799. ' c: char;',
  7800. ' crg2: TCharRg2;',
  7801. ' s: TSetOfCharRg;',
  7802. 'begin',
  7803. ' c:=crg;',
  7804. ' crg:=c;',
  7805. ' crg2:=crg;',
  7806. ' if c=crg then ;',
  7807. ' if crg=c then ;',
  7808. ' if crg=crg2 then ;',
  7809. ' if c in s then ;',
  7810. ' if crg2 in s then ;',
  7811. ' c:=default(TCharRg);',
  7812. '']);
  7813. ConvertProgram;
  7814. CheckSource('TestCharSet_Custom',
  7815. LinesToStr([ // statements
  7816. 'this.crg = "b";',
  7817. 'this.c = "";',
  7818. 'this.crg2 = "m";',
  7819. 'this.s = {};',
  7820. '']),
  7821. LinesToStr([ // this.$main
  7822. '$mod.c = $mod.crg;',
  7823. '$mod.crg = $mod.c;',
  7824. '$mod.crg2 = $mod.crg;',
  7825. 'if ($mod.c === $mod.crg) ;',
  7826. 'if ($mod.crg === $mod.c) ;',
  7827. 'if ($mod.crg === $mod.crg2) ;',
  7828. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7829. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7830. '$mod.c = "a";',
  7831. '']));
  7832. end;
  7833. procedure TTestModule.TestWideChar;
  7834. begin
  7835. StartProgram(false);
  7836. Add([
  7837. 'procedure Fly(var c: char);',
  7838. 'begin',
  7839. 'end;',
  7840. 'procedure Run(var c: widechar);',
  7841. 'begin',
  7842. 'end;',
  7843. 'var',
  7844. ' c: char;',
  7845. ' wc: widechar;',
  7846. ' w: word;',
  7847. 'begin',
  7848. ' Fly(wc);',
  7849. ' Run(c);',
  7850. ' wc:=WideChar(w);',
  7851. ' w:=ord(wc);',
  7852. '']);
  7853. ConvertProgram;
  7854. CheckSource('TestWideChar_VarArg',
  7855. LinesToStr([ // statements
  7856. 'this.Fly = function (c) {',
  7857. '};',
  7858. 'this.Run = function (c) {',
  7859. '};',
  7860. 'this.c = "";',
  7861. 'this.wc = "";',
  7862. 'this.w = 0;',
  7863. '']),
  7864. LinesToStr([ // this.$main
  7865. '$mod.Fly({',
  7866. ' p: $mod,',
  7867. ' get: function () {',
  7868. ' return this.p.wc;',
  7869. ' },',
  7870. ' set: function (v) {',
  7871. ' this.p.wc = v;',
  7872. ' }',
  7873. '});',
  7874. '$mod.Run({',
  7875. ' p: $mod,',
  7876. ' get: function () {',
  7877. ' return this.p.c;',
  7878. ' },',
  7879. ' set: function (v) {',
  7880. ' this.p.c = v;',
  7881. ' }',
  7882. '});',
  7883. '$mod.wc = String.fromCharCode($mod.w);',
  7884. '$mod.w = $mod.wc.charCodeAt();',
  7885. '',
  7886. '']));
  7887. end;
  7888. procedure TTestModule.TestForCharDo;
  7889. begin
  7890. StartProgram(false);
  7891. Add([
  7892. 'var c: char;',
  7893. 'begin',
  7894. ' for c:=''a'' to ''c'' do ;',
  7895. ' for c:=c downto ''a'' do ;',
  7896. ' for c:=''Б'' to ''Я'' do ;',
  7897. '']);
  7898. ConvertProgram;
  7899. CheckSource('TestForCharDo',
  7900. LinesToStr([ // statements
  7901. 'this.c = "";']),
  7902. LinesToStr([ // this.$main
  7903. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7904. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7905. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7906. '']));
  7907. end;
  7908. procedure TTestModule.TestForCharInDo;
  7909. begin
  7910. StartProgram(false);
  7911. Add([
  7912. 'type',
  7913. ' TSetOfChar = set of char;',
  7914. ' TCharRg = ''a''..''z'';',
  7915. ' TSetOfCharRg = set of TCharRg;',
  7916. 'const Foo = ''foo'';',
  7917. 'var',
  7918. ' c,c2: char;',
  7919. ' s: string;',
  7920. ' a1: array of char;',
  7921. ' a2: array[1..3] of char;',
  7922. ' soc: TSetOfChar;',
  7923. ' socr: TSetOfCharRg;',
  7924. ' cr: TCharRg;',
  7925. 'begin',
  7926. ' for c in foo do ;',
  7927. ' for c in s do ;',
  7928. ' for c in char do ;',
  7929. ' for c in a1 do ;',
  7930. ' for c in a2 do ;',
  7931. ' for c in [''1''..''3''] do ;',
  7932. ' for c in TSetOfChar do ;',
  7933. ' for c in TCharRg do ;',
  7934. ' for c in soc do c2:=c;',
  7935. ' for c in TSetOfCharRg do ;',
  7936. ' for c in socr do ;',
  7937. ' for cr in TCharRg do ;',
  7938. ' for cr in TSetOfCharRg do ;',
  7939. ' for cr in socr do ;',
  7940. '']);
  7941. ConvertProgram;
  7942. CheckSource('TestForCharInDo',
  7943. LinesToStr([ // statements
  7944. 'this.Foo = "foo";',
  7945. 'this.c = "";',
  7946. 'this.c2 = "";',
  7947. 'this.s = "";',
  7948. 'this.a1 = [];',
  7949. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7950. 'this.soc = {};',
  7951. 'this.socr = {};',
  7952. 'this.cr = "a";',
  7953. '']),
  7954. LinesToStr([ // this.$main
  7955. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7956. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7957. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7958. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7959. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7960. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7961. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7962. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7963. 'for (var $l8 in $mod.soc) {',
  7964. ' $mod.c = String.fromCharCode($l8);',
  7965. ' $mod.c2 = $mod.c;',
  7966. '};',
  7967. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7968. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7969. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7970. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7971. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7972. '']));
  7973. end;
  7974. procedure TTestModule.TestProcTwoArgs;
  7975. begin
  7976. StartProgram(false);
  7977. Add('procedure Test(a,b: longint);');
  7978. Add('begin');
  7979. Add('end;');
  7980. Add('begin');
  7981. ConvertProgram;
  7982. CheckSource('TestProcTwoArgs',
  7983. LinesToStr([ // statements
  7984. 'this.Test = function (a,b) {',
  7985. '};'
  7986. ]),
  7987. LinesToStr([ // this.$main
  7988. ''
  7989. ]));
  7990. end;
  7991. procedure TTestModule.TestProc_DefaultValue;
  7992. begin
  7993. StartProgram(false);
  7994. Add('procedure p1(i: longint = 1);');
  7995. Add('begin');
  7996. Add('end;');
  7997. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7998. Add('begin');
  7999. Add('end;');
  8000. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8001. Add('begin');
  8002. Add('end;');
  8003. Add('begin');
  8004. Add(' p1;');
  8005. Add(' p1();');
  8006. Add(' p1(11);');
  8007. Add(' p2;');
  8008. Add(' p2();');
  8009. Add(' p2(12);');
  8010. Add(' p2(13,''b'');');
  8011. Add(' p3();');
  8012. ConvertProgram;
  8013. CheckSource('TestProc_DefaultValue',
  8014. LinesToStr([ // statements
  8015. 'this.p1 = function (i) {',
  8016. '};',
  8017. 'this.p2 = function (i,c) {',
  8018. '};',
  8019. 'this.p3 = function (d,b,s) {',
  8020. '};'
  8021. ]),
  8022. LinesToStr([ // this.$main
  8023. ' $mod.p1(1);',
  8024. ' $mod.p1(1);',
  8025. ' $mod.p1(11);',
  8026. ' $mod.p2(1,"a");',
  8027. ' $mod.p2(1,"a");',
  8028. ' $mod.p2(12,"a");',
  8029. ' $mod.p2(13,"b");',
  8030. ' $mod.p3(1.0,false,"abc");'
  8031. ]));
  8032. end;
  8033. procedure TTestModule.TestFunctionInt;
  8034. begin
  8035. StartProgram(false);
  8036. Add('function MyTest(Bar: longint): longint;');
  8037. Add('begin');
  8038. Add(' Result:=2*bar');
  8039. Add('end;');
  8040. Add('begin');
  8041. ConvertProgram;
  8042. CheckSource('TestFunctionInt',
  8043. LinesToStr([ // statements
  8044. 'this.MyTest = function (Bar) {',
  8045. ' var Result = 0;',
  8046. ' Result = 2*Bar;',
  8047. ' return Result;',
  8048. '};'
  8049. ]),
  8050. LinesToStr([ // this.$main
  8051. ''
  8052. ]));
  8053. end;
  8054. procedure TTestModule.TestFunctionString;
  8055. begin
  8056. StartProgram(false);
  8057. Add('function Test(Bar: string): string;');
  8058. Add('begin');
  8059. Add(' Result:=bar+BAR');
  8060. Add('end;');
  8061. Add('begin');
  8062. ConvertProgram;
  8063. CheckSource('TestFunctionString',
  8064. LinesToStr([ // statements
  8065. 'this.Test = function (Bar) {',
  8066. ' var Result = "";',
  8067. ' Result = Bar+Bar;',
  8068. ' return Result;',
  8069. '};'
  8070. ]),
  8071. LinesToStr([ // this.$main
  8072. ''
  8073. ]));
  8074. end;
  8075. procedure TTestModule.TestIfThen;
  8076. begin
  8077. StartProgram(false);
  8078. Add([
  8079. 'var b: boolean;',
  8080. 'begin',
  8081. ' if b then ;',
  8082. ' if b then else ;']);
  8083. ConvertProgram;
  8084. CheckSource('TestIfThen',
  8085. LinesToStr([ // statements
  8086. 'this.b = false;',
  8087. '']),
  8088. LinesToStr([ // this.$main
  8089. 'if ($mod.b) ;',
  8090. 'if ($mod.b) ;',
  8091. '']));
  8092. end;
  8093. procedure TTestModule.TestForLoop;
  8094. begin
  8095. StartProgram(false);
  8096. Add('var');
  8097. Add(' vI, vJ, vN: longint;');
  8098. Add('begin');
  8099. Add(' VJ:=0;');
  8100. Add(' VN:=3;');
  8101. Add(' for VI:=1 to VN do');
  8102. Add(' begin');
  8103. Add(' VJ:=VJ+VI;');
  8104. Add(' end;');
  8105. ConvertProgram;
  8106. CheckSource('TestForLoop',
  8107. LinesToStr([ // statements
  8108. 'this.vI = 0;',
  8109. 'this.vJ = 0;',
  8110. 'this.vN = 0;'
  8111. ]),
  8112. LinesToStr([ // this.$main
  8113. ' $mod.vJ = 0;',
  8114. ' $mod.vN = 3;',
  8115. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8116. ' $mod.vI = $l;',
  8117. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8118. ' };',
  8119. '']));
  8120. end;
  8121. procedure TTestModule.TestForLoopInsideFunction;
  8122. begin
  8123. StartProgram(false);
  8124. Add('function SumNumbers(Count: longint): longint;');
  8125. Add('var');
  8126. Add(' vI, vJ: longint;');
  8127. Add('begin');
  8128. Add(' vj:=0;');
  8129. Add(' for vi:=1 to count do');
  8130. Add(' begin');
  8131. Add(' vj:=vj+vi;');
  8132. Add(' end;');
  8133. Add('end;');
  8134. Add('begin');
  8135. Add(' sumnumbers(3);');
  8136. ConvertProgram;
  8137. CheckSource('TestForLoopInsideFunction',
  8138. LinesToStr([ // statements
  8139. 'this.SumNumbers = function (Count) {',
  8140. ' var Result = 0;',
  8141. ' var vI = 0;',
  8142. ' var vJ = 0;',
  8143. ' vJ = 0;',
  8144. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8145. ' vI = $l;',
  8146. ' vJ = vJ + vI;',
  8147. ' };',
  8148. ' return Result;',
  8149. '};'
  8150. ]),
  8151. LinesToStr([ // $mod.$main
  8152. ' $mod.SumNumbers(3);'
  8153. ]));
  8154. end;
  8155. procedure TTestModule.TestForLoop_ReadVarAfter;
  8156. begin
  8157. StartProgram(false);
  8158. Add('var');
  8159. Add(' vI: longint;');
  8160. Add('begin');
  8161. Add(' for vi:=1 to 2 do ;');
  8162. Add(' if vi=3 then ;');
  8163. ConvertProgram;
  8164. CheckSource('TestForLoop',
  8165. LinesToStr([ // statements
  8166. 'this.vI = 0;'
  8167. ]),
  8168. LinesToStr([ // this.$main
  8169. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8170. ' if ($mod.vI===3) ;'
  8171. ]));
  8172. end;
  8173. procedure TTestModule.TestForLoop_Nested;
  8174. begin
  8175. StartProgram(false);
  8176. Add('function SumNumbers(Count: longint): longint;');
  8177. Add('var');
  8178. Add(' vI, vJ, vK: longint;');
  8179. Add('begin');
  8180. Add(' VK:=0;');
  8181. Add(' for VI:=1 to count do');
  8182. Add(' begin');
  8183. Add(' for vj:=1 to vi do');
  8184. Add(' begin');
  8185. Add(' vk:=VK+VI;');
  8186. Add(' end;');
  8187. Add(' end;');
  8188. Add('end;');
  8189. Add('begin');
  8190. Add(' sumnumbers(3);');
  8191. ConvertProgram;
  8192. CheckSource('TestForLoopInFunction',
  8193. LinesToStr([ // statements
  8194. 'this.SumNumbers = function (Count) {',
  8195. ' var Result = 0;',
  8196. ' var vI = 0;',
  8197. ' var vJ = 0;',
  8198. ' var vK = 0;',
  8199. ' vK = 0;',
  8200. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8201. ' vI = $l;',
  8202. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8203. ' vJ = $l1;',
  8204. ' vK = vK + vI;',
  8205. ' };',
  8206. ' };',
  8207. ' return Result;',
  8208. '};'
  8209. ]),
  8210. LinesToStr([ // $mod.$main
  8211. ' $mod.SumNumbers(3);'
  8212. ]));
  8213. end;
  8214. procedure TTestModule.TestRepeatUntil;
  8215. begin
  8216. StartProgram(false);
  8217. Add('var');
  8218. Add(' vI, vJ, vN: longint;');
  8219. Add('begin');
  8220. Add(' vn:=3;');
  8221. Add(' vj:=0;');
  8222. Add(' VI:=0;');
  8223. Add(' repeat');
  8224. Add(' VI:=vi+1;');
  8225. Add(' vj:=VJ+vI;');
  8226. Add(' until vi>=vn');
  8227. ConvertProgram;
  8228. CheckSource('TestRepeatUntil',
  8229. LinesToStr([ // statements
  8230. 'this.vI = 0;',
  8231. 'this.vJ = 0;',
  8232. 'this.vN = 0;'
  8233. ]),
  8234. LinesToStr([ // $mod.$main
  8235. ' $mod.vN = 3;',
  8236. ' $mod.vJ = 0;',
  8237. ' $mod.vI = 0;',
  8238. ' do{',
  8239. ' $mod.vI = $mod.vI + 1;',
  8240. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8241. ' }while(!($mod.vI>=$mod.vN));'
  8242. ]));
  8243. end;
  8244. procedure TTestModule.TestAsmBlock;
  8245. begin
  8246. StartProgram(false);
  8247. Add([
  8248. 'var',
  8249. ' vI: longint;',
  8250. 'begin',
  8251. ' vi:=1;',
  8252. ' asm',
  8253. ' if (vI===1) {',
  8254. ' vI=2;',
  8255. //' console.log(''end;'');', ToDo
  8256. ' }',
  8257. ' if (vI===2){ vI=3; }',
  8258. ' end;',
  8259. ' VI:=4;']);
  8260. ConvertProgram;
  8261. CheckSource('TestAsmBlock',
  8262. LinesToStr([ // statements
  8263. 'this.vI = 0;'
  8264. ]),
  8265. LinesToStr([ // $mod.$main
  8266. '$mod.vI = 1;',
  8267. 'if (vI===1) {',
  8268. ' vI=2;',
  8269. '}',
  8270. 'if (vI===2){ vI=3; }',
  8271. ';',
  8272. '$mod.vI = 4;'
  8273. ]));
  8274. end;
  8275. procedure TTestModule.TestAsmPas_Impl;
  8276. begin
  8277. StartUnit(false);
  8278. Add('interface');
  8279. Add('const cIntf: longint = 1;');
  8280. Add('var vIntf: longint;');
  8281. Add('implementation');
  8282. Add('const cImpl: longint = 2;');
  8283. Add('var vImpl: longint;');
  8284. Add('procedure DoIt;');
  8285. Add('const cLoc: longint = 3;');
  8286. Add('var vLoc: longint;');
  8287. Add('begin;');
  8288. Add(' asm');
  8289. //Add(' pas(vIntf)=pas(cIntf);');
  8290. //Add(' pas(vImpl)=pas(cImpl);');
  8291. //Add(' pas(vLoc)=pas(cLoc);');
  8292. Add(' end;');
  8293. Add('end;');
  8294. ConvertUnit;
  8295. CheckSource('TestAsmPas_Impl',
  8296. LinesToStr([
  8297. 'var $impl = $mod.$impl;',
  8298. 'this.cIntf = 1;',
  8299. 'this.vIntf = 0;',
  8300. '']),
  8301. '', // this.$init
  8302. LinesToStr([ // implementation
  8303. '$impl.cImpl = 2;',
  8304. '$impl.vImpl = 0;',
  8305. 'var cLoc = 3;',
  8306. '$impl.DoIt = function () {',
  8307. ' var vLoc = 0;',
  8308. '};',
  8309. '']) );
  8310. end;
  8311. procedure TTestModule.TestTryFinally;
  8312. begin
  8313. StartProgram(false);
  8314. Add('var i: longint;');
  8315. Add('begin');
  8316. Add(' try');
  8317. Add(' i:=0; i:=2 div i;');
  8318. Add(' finally');
  8319. Add(' i:=3');
  8320. Add(' end;');
  8321. ConvertProgram;
  8322. CheckSource('TestTryFinally',
  8323. LinesToStr([ // statements
  8324. 'this.i = 0;'
  8325. ]),
  8326. LinesToStr([ // $mod.$main
  8327. 'try {',
  8328. ' $mod.i = 0;',
  8329. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8330. '} finally {',
  8331. ' $mod.i = 3;',
  8332. '};'
  8333. ]));
  8334. end;
  8335. procedure TTestModule.TestTryExcept;
  8336. begin
  8337. StartProgram(false);
  8338. Add([
  8339. 'type',
  8340. ' TObject = class end;',
  8341. ' Exception = class Msg: string; end;',
  8342. ' EInvalidCast = class(Exception) end;',
  8343. 'var vI: longint;',
  8344. 'begin',
  8345. ' try',
  8346. ' vi:=1;',
  8347. ' except',
  8348. ' vi:=2',
  8349. ' end;',
  8350. ' try',
  8351. ' vi:=3;',
  8352. ' except',
  8353. ' raise;',
  8354. ' end;',
  8355. ' try',
  8356. ' VI:=4;',
  8357. ' except',
  8358. ' on einvalidcast do',
  8359. ' raise;',
  8360. ' on E: exception do',
  8361. ' if e.msg='''' then',
  8362. ' raise e;',
  8363. ' else',
  8364. ' vi:=5',
  8365. ' end;',
  8366. ' try',
  8367. ' VI:=6;',
  8368. ' except',
  8369. ' on einvalidcast do ;',
  8370. ' end;',
  8371. '']);
  8372. ConvertProgram;
  8373. CheckSource('TestTryExcept',
  8374. LinesToStr([ // statements
  8375. 'rtl.createClass(this, "TObject", null, function () {',
  8376. ' this.$init = function () {',
  8377. ' };',
  8378. ' this.$final = function () {',
  8379. ' };',
  8380. '});',
  8381. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8382. ' this.$init = function () {',
  8383. ' $mod.TObject.$init.call(this);',
  8384. ' this.Msg = "";',
  8385. ' };',
  8386. '});',
  8387. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8388. '});',
  8389. 'this.vI = 0;'
  8390. ]),
  8391. LinesToStr([ // $mod.$main
  8392. 'try {',
  8393. ' $mod.vI = 1;',
  8394. '} catch ($e) {',
  8395. ' $mod.vI = 2;',
  8396. '};',
  8397. 'try {',
  8398. ' $mod.vI = 3;',
  8399. '} catch ($e) {',
  8400. ' throw $e;',
  8401. '};',
  8402. 'try {',
  8403. ' $mod.vI = 4;',
  8404. '} catch ($e) {',
  8405. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8406. ' throw $e',
  8407. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8408. ' var E = $e;',
  8409. ' if (E.Msg === "") throw E;',
  8410. ' } else {',
  8411. ' $mod.vI = 5;',
  8412. ' }',
  8413. '};',
  8414. 'try {',
  8415. ' $mod.vI = 6;',
  8416. '} catch ($e) {',
  8417. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8418. ' } else throw $e',
  8419. '};',
  8420. '']));
  8421. end;
  8422. procedure TTestModule.TestTryExcept_ReservedWords;
  8423. begin
  8424. StartProgram(false);
  8425. Add([
  8426. 'type',
  8427. ' TObject = class end;',
  8428. ' Exception = class',
  8429. ' Symbol: string;',
  8430. ' end;',
  8431. 'var &try: longint;',
  8432. 'begin',
  8433. ' try',
  8434. ' &try:=4;',
  8435. ' except',
  8436. ' on Error: exception do',
  8437. ' if errOR.symBol='''' then',
  8438. ' raise ERRor;',
  8439. ' end;',
  8440. '']);
  8441. ConvertProgram;
  8442. CheckSource('TestTryExcept_ReservedWords',
  8443. LinesToStr([ // statements
  8444. 'rtl.createClass(this, "TObject", null, function () {',
  8445. ' this.$init = function () {',
  8446. ' };',
  8447. ' this.$final = function () {',
  8448. ' };',
  8449. '});',
  8450. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8451. ' this.$init = function () {',
  8452. ' $mod.TObject.$init.call(this);',
  8453. ' this.Symbol = "";',
  8454. ' };',
  8455. '});',
  8456. 'this.Try = 0;',
  8457. '']),
  8458. LinesToStr([ // $mod.$main
  8459. 'try {',
  8460. ' $mod.Try = 4;',
  8461. '} catch ($e) {',
  8462. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8463. ' var error = $e;',
  8464. ' if (error.Symbol === "") throw error;',
  8465. ' } else throw $e',
  8466. '};',
  8467. '']));
  8468. end;
  8469. procedure TTestModule.TestIfThenRaiseElse;
  8470. begin
  8471. StartProgram(false);
  8472. Add([
  8473. 'type',
  8474. ' TObject = class',
  8475. ' constructor Create;',
  8476. ' end;',
  8477. 'constructor TObject.Create;',
  8478. 'begin',
  8479. 'end;',
  8480. 'var b: boolean;',
  8481. 'begin',
  8482. ' if b then',
  8483. ' raise TObject.Create',
  8484. ' else',
  8485. ' b:=false;',
  8486. '']);
  8487. ConvertProgram;
  8488. CheckSource('TestIfThenRaiseElse',
  8489. LinesToStr([ // statements
  8490. 'rtl.createClass(this, "TObject", null, function () {',
  8491. ' this.$init = function () {',
  8492. ' };',
  8493. ' this.$final = function () {',
  8494. ' };',
  8495. ' this.Create = function () {',
  8496. ' return this;',
  8497. ' };',
  8498. '});',
  8499. 'this.b = false;',
  8500. '']),
  8501. LinesToStr([ // $mod.$main
  8502. 'if ($mod.b) {',
  8503. ' throw $mod.TObject.$create("Create")}',
  8504. ' else $mod.b = false;',
  8505. '']));
  8506. end;
  8507. procedure TTestModule.TestCaseOf;
  8508. begin
  8509. StartProgram(false);
  8510. Add([
  8511. 'const e: longint; external name ''$e'';',
  8512. 'var vI: longint;',
  8513. 'begin',
  8514. ' case vi of',
  8515. ' 1: ;',
  8516. ' 2: vi:=3;',
  8517. ' e: ;',
  8518. ' else',
  8519. ' VI:=4',
  8520. ' end;']);
  8521. ConvertProgram;
  8522. CheckSource('TestCaseOf',
  8523. LinesToStr([ // statements
  8524. 'this.vI = 0;'
  8525. ]),
  8526. LinesToStr([ // $mod.$main
  8527. 'var $tmp = $mod.vI;',
  8528. 'if ($tmp === 1) {}',
  8529. 'else if ($tmp === 2) {',
  8530. ' $mod.vI = 3}',
  8531. ' else if ($tmp === $e) {}',
  8532. 'else {',
  8533. ' $mod.vI = 4;',
  8534. '};'
  8535. ]));
  8536. end;
  8537. procedure TTestModule.TestCaseOf_UseSwitch;
  8538. begin
  8539. StartProgram(false);
  8540. Converter.UseSwitchStatement:=true;
  8541. Add('var Vi: longint;');
  8542. Add('begin');
  8543. Add(' case vi of');
  8544. Add(' 1: ;');
  8545. Add(' 2: VI:=3;');
  8546. Add(' else');
  8547. Add(' vi:=4');
  8548. Add(' end;');
  8549. ConvertProgram;
  8550. CheckSource('TestCaseOf_UseSwitch',
  8551. LinesToStr([ // statements
  8552. 'this.Vi = 0;'
  8553. ]),
  8554. LinesToStr([ // $mod.$main
  8555. 'switch ($mod.Vi) {',
  8556. 'case 1:',
  8557. ' break;',
  8558. 'case 2:',
  8559. ' $mod.Vi = 3;',
  8560. ' break;',
  8561. 'default:',
  8562. ' $mod.Vi = 4;',
  8563. '};'
  8564. ]));
  8565. end;
  8566. procedure TTestModule.TestCaseOfNoElse;
  8567. begin
  8568. StartProgram(false);
  8569. Add('var Vi: longint;');
  8570. Add('begin');
  8571. Add(' case vi of');
  8572. Add(' 1: begin vi:=2; VI:=3; end;');
  8573. Add(' end;');
  8574. ConvertProgram;
  8575. CheckSource('TestCaseOfNoElse',
  8576. LinesToStr([ // statements
  8577. 'this.Vi = 0;'
  8578. ]),
  8579. LinesToStr([ // $mod.$main
  8580. 'var $tmp = $mod.Vi;',
  8581. 'if ($tmp === 1) {',
  8582. ' $mod.Vi = 2;',
  8583. ' $mod.Vi = 3;',
  8584. '};'
  8585. ]));
  8586. end;
  8587. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8588. begin
  8589. StartProgram(false);
  8590. Converter.UseSwitchStatement:=true;
  8591. Add('var vI: longint;');
  8592. Add('begin');
  8593. Add(' case vi of');
  8594. Add(' 1: begin VI:=2; vi:=3; end;');
  8595. Add(' end;');
  8596. ConvertProgram;
  8597. CheckSource('TestCaseOfNoElse_UseSwitch',
  8598. LinesToStr([ // statements
  8599. 'this.vI = 0;'
  8600. ]),
  8601. LinesToStr([ // $mod.$main
  8602. 'switch ($mod.vI) {',
  8603. 'case 1:',
  8604. ' $mod.vI = 2;',
  8605. ' $mod.vI = 3;',
  8606. ' break;',
  8607. '};'
  8608. ]));
  8609. end;
  8610. procedure TTestModule.TestCaseOfRange;
  8611. begin
  8612. StartProgram(false);
  8613. Add('var vI: longint;');
  8614. Add('begin');
  8615. Add(' case vi of');
  8616. Add(' 1..3: vi:=14;');
  8617. Add(' 4,5: vi:=16;');
  8618. Add(' 6..7,9..10: ;');
  8619. Add(' else ;');
  8620. Add(' end;');
  8621. ConvertProgram;
  8622. CheckSource('TestCaseOfRange',
  8623. LinesToStr([ // statements
  8624. 'this.vI = 0;'
  8625. ]),
  8626. LinesToStr([ // $mod.$main
  8627. 'var $tmp = $mod.vI;',
  8628. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8629. ' $mod.vI = 14',
  8630. '} else if (($tmp === 4) || ($tmp === 5)){',
  8631. ' $mod.vI = 16',
  8632. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8633. ]));
  8634. end;
  8635. procedure TTestModule.TestCaseOfString;
  8636. begin
  8637. StartProgram(false);
  8638. Add([
  8639. 'var s,h: string;',
  8640. 'begin',
  8641. ' case s of',
  8642. ' ''foo'': s:=h;',
  8643. ' ''a''..''z'': h:=s;',
  8644. ' ''ў'', ''ё'': ;',
  8645. ' ''Б''..''Я'': ;',
  8646. ' end;',
  8647. '']);
  8648. ConvertProgram;
  8649. CheckSource('TestCaseOfString',
  8650. LinesToStr([ // statements
  8651. 'this.s = "";',
  8652. 'this.h = "";',
  8653. '']),
  8654. LinesToStr([ // $mod.$main
  8655. 'var $tmp = $mod.s;',
  8656. 'if ($tmp === "foo") {',
  8657. ' $mod.s = $mod.h}',
  8658. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8659. ' $mod.h = $mod.s}',
  8660. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8661. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8662. '']));
  8663. end;
  8664. procedure TTestModule.TestCaseOfChar;
  8665. begin
  8666. StartProgram(false);
  8667. Add([
  8668. 'var s,h: char;',
  8669. 'begin',
  8670. ' case s of',
  8671. ' ''a''..''z'': h:=s;',
  8672. ' ''ä'': ;',
  8673. ' ''ў'', ''ё'': ;',
  8674. ' ''Б''..''Я'': ;',
  8675. ' end;',
  8676. '']);
  8677. ConvertProgram;
  8678. CheckSource('TestCaseOfString',
  8679. LinesToStr([ // statements
  8680. 'this.s = "";',
  8681. 'this.h = "";',
  8682. '']),
  8683. LinesToStr([ // $mod.$main
  8684. 'var $tmp = $mod.s;',
  8685. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8686. ' $mod.h = $mod.s}',
  8687. ' else if ($tmp === "ä") {}',
  8688. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8689. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8690. '']));
  8691. end;
  8692. procedure TTestModule.TestCaseOfExternalClassConst;
  8693. begin
  8694. StartProgram(false);
  8695. Add([
  8696. '{$modeswitch externalclass}',
  8697. 'type',
  8698. ' TBird = class external name ''Bird''',
  8699. ' const e: longint;',
  8700. ' end;',
  8701. 'var vI: longint;',
  8702. 'begin',
  8703. ' case vi of',
  8704. ' 1: vi:=3;',
  8705. ' TBird.e: ;',
  8706. ' end;']);
  8707. ConvertProgram;
  8708. CheckSource('TestCaseOfExternalClassConst',
  8709. LinesToStr([ // statements
  8710. 'this.vI = 0;'
  8711. ]),
  8712. LinesToStr([ // $mod.$main
  8713. 'var $tmp = $mod.vI;',
  8714. 'if ($tmp === 1) {',
  8715. ' $mod.vI = 3}',
  8716. ' else if ($tmp === Bird.e) ;'
  8717. ]));
  8718. end;
  8719. procedure TTestModule.TestDebugger;
  8720. begin
  8721. StartProgram(false);
  8722. Add([
  8723. 'procedure DoIt;',
  8724. 'begin',
  8725. ' deBugger;',
  8726. ' DeBugger();',
  8727. 'end;',
  8728. 'begin',
  8729. ' Debugger;']);
  8730. ConvertProgram;
  8731. CheckSource('TestDebugger',
  8732. LinesToStr([ // statements
  8733. 'this.DoIt = function () {',
  8734. ' debugger;',
  8735. ' debugger;',
  8736. '};',
  8737. '']),
  8738. LinesToStr([ // $mod.$main
  8739. 'debugger;',
  8740. '']));
  8741. end;
  8742. procedure TTestModule.TestArray_Dynamic;
  8743. begin
  8744. StartProgram(false);
  8745. Add([
  8746. 'type',
  8747. ' TArrayInt = array of longint;',
  8748. 'var',
  8749. ' Arr: TArrayInt;',
  8750. ' i: longint;',
  8751. ' b: boolean;',
  8752. 'begin',
  8753. ' SetLength(arr,3);',
  8754. ' arr[0]:=4;',
  8755. ' arr[1]:=length(arr)+arr[0];',
  8756. ' arr[i]:=5;',
  8757. ' arr[arr[i]]:=arr[6];',
  8758. ' i:=low(arr);',
  8759. ' i:=high(arr);',
  8760. ' b:=Assigned(arr);',
  8761. ' Arr:=default(TArrayInt);']);
  8762. ConvertProgram;
  8763. CheckSource('TestArray_Dynamic',
  8764. LinesToStr([ // statements
  8765. 'this.Arr = [];',
  8766. 'this.i = 0;',
  8767. 'this.b = false;'
  8768. ]),
  8769. LinesToStr([ // $mod.$main
  8770. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8771. '$mod.Arr[0] = 4;',
  8772. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8773. '$mod.Arr[$mod.i] = 5;',
  8774. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8775. '$mod.i = 0;',
  8776. '$mod.i = rtl.length($mod.Arr) - 1;',
  8777. '$mod.b = rtl.length($mod.Arr) > 0;',
  8778. '$mod.Arr = [];',
  8779. '']));
  8780. end;
  8781. procedure TTestModule.TestArray_Dynamic_Nil;
  8782. begin
  8783. StartProgram(false);
  8784. Add('type');
  8785. Add(' TArrayInt = array of longint;');
  8786. Add('var');
  8787. Add(' Arr: TArrayInt;');
  8788. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8789. Add('begin');
  8790. Add(' arr:=nil;');
  8791. Add(' if arr=nil then;');
  8792. Add(' if nil=arr then;');
  8793. Add(' if arr<>nil then;');
  8794. Add(' if nil<>arr then;');
  8795. Add(' DoIt(nil,nil);');
  8796. ConvertProgram;
  8797. CheckSource('TestArray_Dynamic',
  8798. LinesToStr([ // statements
  8799. 'this.Arr = [];',
  8800. 'this.DoIt = function(i,j){',
  8801. '};'
  8802. ]),
  8803. LinesToStr([ // $mod.$main
  8804. '$mod.Arr = [];',
  8805. 'if (rtl.length($mod.Arr) === 0) ;',
  8806. 'if (rtl.length($mod.Arr) === 0) ;',
  8807. 'if (rtl.length($mod.Arr) > 0) ;',
  8808. 'if (rtl.length($mod.Arr) > 0) ;',
  8809. '$mod.DoIt([],[]);',
  8810. '']));
  8811. end;
  8812. procedure TTestModule.TestArray_DynMultiDimensional;
  8813. begin
  8814. StartProgram(false);
  8815. Add([
  8816. 'type',
  8817. ' TArrayInt = array of longint;',
  8818. ' TArrayArrayInt = array of TArrayInt;',
  8819. 'var',
  8820. ' Arr: TArrayInt;',
  8821. ' Arr2: TArrayArrayInt;',
  8822. ' i: longint;',
  8823. 'begin',
  8824. ' arr2:=nil;',
  8825. ' if arr2=nil then;',
  8826. ' if nil=arr2 then;',
  8827. ' i:=low(arr2);',
  8828. ' i:=low(arr2[1]);',
  8829. ' i:=high(arr2);',
  8830. ' i:=high(arr2[2]);',
  8831. ' arr2[3]:=arr;',
  8832. ' arr2[4][5]:=i;',
  8833. ' i:=arr2[6][7];',
  8834. ' arr2[8,9]:=i;',
  8835. ' i:=arr2[10,11];',
  8836. ' SetLength(arr2,14);',
  8837. ' SetLength(arr2[15],16);']);
  8838. ConvertProgram;
  8839. CheckSource('TestArray_Dynamic',
  8840. LinesToStr([ // statements
  8841. 'this.Arr = [];',
  8842. 'this.Arr2 = [];',
  8843. 'this.i = 0;'
  8844. ]),
  8845. LinesToStr([ // $mod.$main
  8846. '$mod.Arr2 = [];',
  8847. 'if (rtl.length($mod.Arr2) === 0) ;',
  8848. 'if (rtl.length($mod.Arr2) === 0) ;',
  8849. '$mod.i = 0;',
  8850. '$mod.i = 0;',
  8851. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8852. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8853. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8854. '$mod.Arr2[4][5] = $mod.i;',
  8855. '$mod.i = $mod.Arr2[6][7];',
  8856. '$mod.Arr2[8][9] = $mod.i;',
  8857. '$mod.i = $mod.Arr2[10][11];',
  8858. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8859. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8860. '']));
  8861. end;
  8862. procedure TTestModule.TestArray_DynamicAssign;
  8863. begin
  8864. StartProgram(false);
  8865. Add([
  8866. 'type',
  8867. ' TArrayInt = array of longint;',
  8868. ' TArrayArrayInt = array of TArrayInt;',
  8869. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8870. 'begin',
  8871. 'end;',
  8872. 'procedure Fly(var a: TArrayInt);',
  8873. 'begin',
  8874. 'end;',
  8875. 'var',
  8876. ' Arr: TArrayInt;',
  8877. ' Arr2: TArrayArrayInt;',
  8878. 'begin',
  8879. ' arr:=nil;',
  8880. ' arr2:=nil;',
  8881. ' arr2[1]:=nil;',
  8882. ' arr2[2]:=arr;',
  8883. ' Run(arr,arr,arr);',
  8884. ' Fly(arr);',
  8885. ' Run(arr2[4],arr2[5],arr2[6]);',
  8886. ' Fly(arr2[7]);',
  8887. '']);
  8888. ConvertProgram;
  8889. CheckSource('TestArray_DynamicAssign',
  8890. LinesToStr([ // statements
  8891. 'this.Run = function (a, b, c) {',
  8892. '};',
  8893. 'this.Fly = function (a) {',
  8894. '};',
  8895. 'this.Arr = [];',
  8896. 'this.Arr2 = [];',
  8897. '']),
  8898. LinesToStr([ // $mod.$main
  8899. '$mod.Arr = [];',
  8900. '$mod.Arr2 = [];',
  8901. '$mod.Arr2[1] = [];',
  8902. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8903. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8904. '$mod.Fly({',
  8905. ' p: $mod,',
  8906. ' get: function () {',
  8907. ' return this.p.Arr;',
  8908. ' },',
  8909. ' set: function (v) {',
  8910. ' this.p.Arr = v;',
  8911. ' }',
  8912. '});',
  8913. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8914. '$mod.Fly({',
  8915. ' a: 7,',
  8916. ' p: $mod.Arr2,',
  8917. ' get: function () {',
  8918. ' return this.p[this.a];',
  8919. ' },',
  8920. ' set: function (v) {',
  8921. ' this.p[this.a] = v;',
  8922. ' }',
  8923. '});',
  8924. '']));
  8925. end;
  8926. procedure TTestModule.TestArray_StaticInt;
  8927. begin
  8928. StartProgram(false);
  8929. Add('type');
  8930. Add(' TArrayInt = array[2..4] of longint;');
  8931. Add('var');
  8932. Add(' Arr: TArrayInt;');
  8933. Add(' Arr2: TArrayInt = (5,6,7);');
  8934. Add(' i: longint;');
  8935. Add(' b: boolean;');
  8936. Add('begin');
  8937. Add(' arr[2]:=4;');
  8938. Add(' arr[3]:=arr[2]+arr[3];');
  8939. Add(' arr[i]:=5;');
  8940. Add(' arr[arr[i]]:=arr[high(arr)];');
  8941. Add(' i:=low(arr);');
  8942. Add(' i:=high(arr);');
  8943. Add(' b:=arr[2]=arr[3];');
  8944. Add(' arr:=default(TArrayInt);');
  8945. ConvertProgram;
  8946. CheckSource('TestArray_StaticInt',
  8947. LinesToStr([ // statements
  8948. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8949. 'this.Arr2 = [5, 6, 7];',
  8950. 'this.i = 0;',
  8951. 'this.b = false;'
  8952. ]),
  8953. LinesToStr([ // $mod.$main
  8954. '$mod.Arr[0] = 4;',
  8955. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8956. '$mod.Arr[$mod.i-2] = 5;',
  8957. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8958. '$mod.i = 2;',
  8959. '$mod.i = 4;',
  8960. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8961. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8962. '']));
  8963. end;
  8964. procedure TTestModule.TestArray_StaticBool;
  8965. begin
  8966. StartProgram(false);
  8967. Add('type');
  8968. Add(' TBools = array[boolean] of boolean;');
  8969. Add(' TBool2 = array[true..true] of boolean;');
  8970. Add('var');
  8971. Add(' Arr: TBools;');
  8972. Add(' Arr2: TBool2;');
  8973. Add(' Arr3: TBools = (true,false);');
  8974. Add(' b: boolean;');
  8975. Add('begin');
  8976. Add(' b:=low(arr);');
  8977. Add(' b:=high(arr);');
  8978. Add(' arr[true]:=false;');
  8979. Add(' arr[false]:=arr[b] or arr[true];');
  8980. Add(' arr[b]:=true;');
  8981. Add(' arr[arr[b]]:=arr[high(arr)];');
  8982. Add(' b:=arr[false]=arr[true];');
  8983. Add(' b:=low(arr2);');
  8984. Add(' b:=high(arr2);');
  8985. Add(' arr2[true]:=true;');
  8986. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8987. Add(' arr2[b]:=false;');
  8988. ConvertProgram;
  8989. CheckSource('TestArray_StaticBool',
  8990. LinesToStr([ // statements
  8991. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8992. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8993. 'this.Arr3 = [true, false];',
  8994. 'this.b = false;'
  8995. ]),
  8996. LinesToStr([ // $mod.$main
  8997. '$mod.b = false;',
  8998. '$mod.b = true;',
  8999. '$mod.Arr[1] = false;',
  9000. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9001. '$mod.Arr[+$mod.b] = true;',
  9002. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9003. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9004. '$mod.b = true;',
  9005. '$mod.b = true;',
  9006. '$mod.Arr2[0] = true;',
  9007. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9008. '$mod.Arr2[1-$mod.b] = false;',
  9009. '']));
  9010. end;
  9011. procedure TTestModule.TestArray_StaticChar;
  9012. begin
  9013. StartProgram(false);
  9014. Add([
  9015. 'type',
  9016. ' TChars = array[char] of char;',
  9017. ' TChars2 = array[''a''..''z''] of char;',
  9018. 'var',
  9019. ' Arr: TChars;',
  9020. ' Arr2: TChars2;',
  9021. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9022. ' Arr4: array[11..13] of char = ''pas'';',
  9023. ' Arr5: array[21..22] of char = ''äö'';',
  9024. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9025. ' c: char;',
  9026. ' b: boolean;',
  9027. 'begin',
  9028. ' c:=low(arr);',
  9029. ' c:=high(arr);',
  9030. ' arr[''B'']:=''a'';',
  9031. ' arr[''D'']:=arr[c];',
  9032. ' arr[c]:=arr[''d''];',
  9033. ' arr[arr[c]]:=arr[high(arr)];',
  9034. ' b:=arr[low(arr)]=arr[''e''];',
  9035. ' c:=low(arr2);',
  9036. ' c:=high(arr2);',
  9037. ' arr2[''b'']:=''f'';',
  9038. ' arr2[''a'']:=arr2[c];',
  9039. ' arr2[c]:=arr2[''g''];']);
  9040. ConvertProgram;
  9041. CheckSource('TestArray_StaticChar',
  9042. LinesToStr([ // statements
  9043. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9044. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9045. 'this.Arr3 = ["p", "a", "s"];',
  9046. 'this.Arr4 = ["p", "a", "s"];',
  9047. 'this.Arr5 = ["ä", "ö"];',
  9048. 'this.Arr6 = ["ä", "ö"];',
  9049. 'this.c = "";',
  9050. 'this.b = false;',
  9051. '']),
  9052. LinesToStr([ // $mod.$main
  9053. '$mod.c = "\x00";',
  9054. '$mod.c = "\uFFFF";',
  9055. '$mod.Arr[66] = "a";',
  9056. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9057. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9058. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9059. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9060. '$mod.c = "a";',
  9061. '$mod.c = "z";',
  9062. '$mod.Arr2[1] = "f";',
  9063. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9064. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9065. '']));
  9066. end;
  9067. procedure TTestModule.TestArray_StaticMultiDim;
  9068. begin
  9069. StartProgram(false);
  9070. Add([
  9071. 'type',
  9072. ' TArrayInt = array[1..3] of longint;',
  9073. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9074. 'var',
  9075. ' Arr: TArrayInt;',
  9076. ' Arr2: TArrayArrayInt;',
  9077. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9078. ' i: longint;',
  9079. 'begin',
  9080. ' i:=low(arr);',
  9081. ' i:=low(arr2);',
  9082. ' i:=low(arr2[5]);',
  9083. ' i:=high(arr);',
  9084. ' i:=high(arr2);',
  9085. ' i:=high(arr2[6]);',
  9086. ' arr2[5]:=arr;',
  9087. ' arr2[6][2]:=i;',
  9088. ' i:=arr2[6][3];',
  9089. ' arr2[6,3]:=i;',
  9090. ' i:=arr2[5,2];',
  9091. ' arr2:=arr2;',// clone multi dim static array
  9092. ' arr3:=arr3;',// clone anonymous multi dim static array
  9093. '']);
  9094. ConvertProgram;
  9095. CheckSource('TestArray_StaticMultiDim',
  9096. LinesToStr([ // statements
  9097. 'this.TArrayArrayInt$clone = function (a) {',
  9098. ' var r = [];',
  9099. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9100. ' return r;',
  9101. '};',
  9102. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9103. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9104. 'this.Arr3$a$clone = function (a) {',
  9105. ' var r = [];',
  9106. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9107. ' return r;',
  9108. '};',
  9109. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9110. 'this.i = 0;'
  9111. ]),
  9112. LinesToStr([ // $mod.$main
  9113. '$mod.i = 1;',
  9114. '$mod.i = 5;',
  9115. '$mod.i = 1;',
  9116. '$mod.i = 3;',
  9117. '$mod.i = 6;',
  9118. '$mod.i = 3;',
  9119. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9120. '$mod.Arr2[1][1] = $mod.i;',
  9121. '$mod.i = $mod.Arr2[1][2];',
  9122. '$mod.Arr2[1][2] = $mod.i;',
  9123. '$mod.i = $mod.Arr2[0][1];',
  9124. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9125. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9126. '']));
  9127. end;
  9128. procedure TTestModule.TestArray_StaticInFunction;
  9129. begin
  9130. StartProgram(false);
  9131. Add([
  9132. 'const TArrayInt = 3;',
  9133. 'const TArrayArrayInt = 4;',
  9134. 'procedure DoIt;',
  9135. 'type',
  9136. ' TArrayInt = array[1..3] of longint;',
  9137. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9138. 'var',
  9139. ' Arr: TArrayInt;',
  9140. ' Arr2: TArrayArrayInt;',
  9141. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9142. ' i: longint;',
  9143. 'begin',
  9144. ' arr2[5]:=arr;',
  9145. ' arr2:=arr2;',// clone multi dim static array
  9146. ' arr3:=arr3;',// clone multi dim anonymous static array
  9147. 'end;',
  9148. 'begin',
  9149. '']);
  9150. ConvertProgram;
  9151. CheckSource('TestArray_StaticInFunction',
  9152. LinesToStr([ // statements
  9153. 'this.TArrayInt = 3;',
  9154. 'this.TArrayArrayInt = 4;',
  9155. 'var TArrayArrayInt$1$clone = function (a) {',
  9156. ' var r = [];',
  9157. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9158. ' return r;',
  9159. '};',
  9160. 'var Arr3$a$clone = function (a) {',
  9161. ' var r = [];',
  9162. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9163. ' return r;',
  9164. '};',
  9165. 'this.DoIt = function () {',
  9166. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9167. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9168. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9169. ' var i = 0;',
  9170. ' Arr2[0] = Arr.slice(0);',
  9171. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9172. ' Arr3 = Arr3$a$clone(Arr3);',
  9173. '};',
  9174. '']),
  9175. LinesToStr([ // $mod.$main
  9176. '']));
  9177. end;
  9178. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9179. begin
  9180. StartProgram(false);
  9181. Add([
  9182. 'type',
  9183. ' TArrayInt = array[1..3,1..2] of longint;',
  9184. 'var',
  9185. ' a,b: TArrayInt;',
  9186. 'begin',
  9187. ' if a=b then ;',
  9188. '']);
  9189. SetExpectedPasResolverError('compare static array is not supported',
  9190. nXIsNotSupported);
  9191. ConvertProgram;
  9192. end;
  9193. procedure TTestModule.TestArrayOfRecord;
  9194. begin
  9195. StartProgram(false);
  9196. Add([
  9197. 'type',
  9198. ' TRec = record',
  9199. ' Int: longint;',
  9200. ' end;',
  9201. ' TArrayRec = array of TRec;',
  9202. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9203. 'begin',
  9204. 'end;',
  9205. 'var',
  9206. ' Arr: TArrayRec;',
  9207. ' r: TRec;',
  9208. ' i: longint;',
  9209. 'begin',
  9210. ' SetLength(arr,3);',
  9211. ' arr[0].int:=4;',
  9212. ' arr[1].int:=length(arr)+arr[2].int;',
  9213. ' arr[arr[i].int].int:=arr[5].int;',
  9214. ' arr[7]:=r;',
  9215. ' r:=arr[8];',
  9216. ' i:=low(arr);',
  9217. ' i:=high(arr);',
  9218. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9219. ConvertProgram;
  9220. CheckSource('TestArrayOfRecord',
  9221. LinesToStr([ // statements
  9222. 'rtl.recNewT(this, "TRec", function () {',
  9223. ' this.Int = 0;',
  9224. ' this.$eq = function (b) {',
  9225. ' return this.Int === b.Int;',
  9226. ' };',
  9227. ' this.$assign = function (s) {',
  9228. ' this.Int = s.Int;',
  9229. ' return this;',
  9230. ' };',
  9231. '});',
  9232. 'this.DoIt = function (vd, vc, vv) {',
  9233. '};',
  9234. 'this.Arr = [];',
  9235. 'this.r = this.TRec.$new();',
  9236. 'this.i = 0;'
  9237. ]),
  9238. LinesToStr([ // $mod.$main
  9239. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9240. '$mod.Arr[0].Int = 4;',
  9241. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9242. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9243. '$mod.Arr[7].$assign($mod.r);',
  9244. '$mod.r.$assign($mod.Arr[8]);',
  9245. '$mod.i = 0;',
  9246. '$mod.i = rtl.length($mod.Arr)-1;',
  9247. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9248. '']));
  9249. end;
  9250. procedure TTestModule.TestArray_StaticRecord;
  9251. begin
  9252. StartProgram(false);
  9253. Add([
  9254. 'type',
  9255. ' TRec = record',
  9256. ' Int: longint;',
  9257. ' end;',
  9258. ' TArrayRec = array[1..2] of TRec;',
  9259. 'var',
  9260. ' Arr: TArrayRec;',
  9261. 'begin',
  9262. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9263. '']);
  9264. ConvertProgram;
  9265. CheckSource('TestArray_StaticRecord',
  9266. LinesToStr([ // statements
  9267. 'rtl.recNewT(this, "TRec", function () {',
  9268. ' this.Int = 0;',
  9269. ' this.$eq = function (b) {',
  9270. ' return this.Int === b.Int;',
  9271. ' };',
  9272. ' this.$assign = function (s) {',
  9273. ' this.Int = s.Int;',
  9274. ' return this;',
  9275. ' };',
  9276. '});',
  9277. 'this.TArrayRec$clone = function (a) {',
  9278. ' var r = [];',
  9279. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9280. ' return r;',
  9281. '};',
  9282. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9283. '']),
  9284. LinesToStr([ // $mod.$main
  9285. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9286. end;
  9287. procedure TTestModule.TestArrayOfSet;
  9288. begin
  9289. StartProgram(false);
  9290. Add([
  9291. 'type',
  9292. ' TFlag = (big,small);',
  9293. ' TSetOfFlag = set of tflag;',
  9294. ' TArrayFlag = array of TSetOfFlag;',
  9295. 'procedure DoIt(const a: Tarrayflag);',
  9296. 'begin',
  9297. 'end;',
  9298. 'var',
  9299. ' f: TFlag;',
  9300. ' s: TSetOfFlag;',
  9301. ' Arr: TArrayFlag;',
  9302. ' i: longint;',
  9303. 'begin',
  9304. ' SetLength(arr,3);',
  9305. ' arr[0]:=s;',
  9306. ' arr[1]:=[big];',
  9307. ' arr[2]:=[big]+s;',
  9308. ' arr[3]:=s+[big];',
  9309. ' arr[4]:=arr[5];',
  9310. ' s:=arr[6];',
  9311. ' i:=low(arr);',
  9312. ' i:=high(arr);',
  9313. ' DoIt(arr);',
  9314. ' DoIt([s]);',
  9315. ' DoIt([[],s]);',
  9316. ' DoIt([s,[]]);',
  9317. '']);
  9318. ConvertProgram;
  9319. CheckSource('TestArrayOfSet',
  9320. LinesToStr([ // statements
  9321. 'this.TFlag = {',
  9322. ' "0": "big",',
  9323. ' big: 0,',
  9324. ' "1": "small",',
  9325. ' small: 1',
  9326. '};',
  9327. 'this.DoIt = function (a) {',
  9328. '};',
  9329. 'this.f = 0;',
  9330. 'this.s = {};',
  9331. 'this.Arr = [];',
  9332. 'this.i = 0;',
  9333. '']),
  9334. LinesToStr([ // $mod.$main
  9335. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9336. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9337. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9338. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9339. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9340. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9341. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9342. '$mod.i = 0;',
  9343. '$mod.i = rtl.length($mod.Arr) - 1;',
  9344. '$mod.DoIt($mod.Arr);',
  9345. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9346. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9347. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9348. '']));
  9349. end;
  9350. procedure TTestModule.TestArray_DynAsParam;
  9351. begin
  9352. StartProgram(false);
  9353. Add([
  9354. 'type integer = longint;',
  9355. 'type TArrInt = array of integer;',
  9356. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9357. 'var vJ: TArrInt;',
  9358. 'begin',
  9359. ' vg:=vg;',
  9360. ' vj:=vh;',
  9361. ' vi:=vi;',
  9362. ' doit(vg,vg,vg);',
  9363. ' doit(vh,vh,vj);',
  9364. ' doit(vi,vi,vi);',
  9365. ' doit(vj,vj,vj);',
  9366. 'end;',
  9367. 'var i: TArrInt;',
  9368. 'begin',
  9369. ' doit(i,i,i);']);
  9370. ConvertProgram;
  9371. CheckSource('TestArray_DynAsParams',
  9372. LinesToStr([ // statements
  9373. 'this.DoIt = function (vG,vH,vI) {',
  9374. ' var vJ = [];',
  9375. ' vG = rtl.arrayRef(vG);',
  9376. ' vJ = rtl.arrayRef(vH);',
  9377. ' vI.set(rtl.arrayRef(vI.get()));',
  9378. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9379. ' get: function () {',
  9380. ' return vG;',
  9381. ' },',
  9382. ' set: function (v) {',
  9383. ' vG = v;',
  9384. ' }',
  9385. ' });',
  9386. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9387. ' get: function () {',
  9388. ' return vJ;',
  9389. ' },',
  9390. ' set: function (v) {',
  9391. ' vJ = v;',
  9392. ' }',
  9393. ' });',
  9394. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9395. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9396. ' get: function () {',
  9397. ' return vJ;',
  9398. ' },',
  9399. ' set: function (v) {',
  9400. ' vJ = v;',
  9401. ' }',
  9402. ' });',
  9403. '};',
  9404. 'this.i = [];'
  9405. ]),
  9406. LinesToStr([
  9407. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9408. ' p: $mod,',
  9409. ' get: function () {',
  9410. ' return this.p.i;',
  9411. ' },',
  9412. ' set: function (v) {',
  9413. ' this.p.i = v;',
  9414. ' }',
  9415. '});'
  9416. ]));
  9417. end;
  9418. procedure TTestModule.TestArray_StaticAsParam;
  9419. begin
  9420. StartProgram(false);
  9421. Add([
  9422. 'type integer = longint;',
  9423. 'type TArrInt = array[1..2] of integer;',
  9424. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9425. 'var vJ: TArrInt;',
  9426. 'begin',
  9427. ' vg:=vg;',
  9428. ' vj:=vh;',
  9429. ' vi:=vi;',
  9430. ' doit(vg,vg,vg);',
  9431. ' doit(vh,vh,vj);',
  9432. ' doit(vi,vi,vi);',
  9433. ' doit(vj,vj,vj);',
  9434. 'end;',
  9435. 'var i: TArrInt;',
  9436. 'begin',
  9437. ' doit(i,i,i);']);
  9438. ConvertProgram;
  9439. CheckSource('TestArray_StaticAsParams',
  9440. LinesToStr([ // statements
  9441. 'this.DoIt = function (vG,vH,vI) {',
  9442. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9443. ' vG = vG.slice(0);',
  9444. ' vJ = vH.slice(0);',
  9445. ' vI.set(vI.get().slice(0));',
  9446. ' $mod.DoIt(vG.slice(0), vG, {',
  9447. ' get: function () {',
  9448. ' return vG;',
  9449. ' },',
  9450. ' set: function (v) {',
  9451. ' vG = v;',
  9452. ' }',
  9453. ' });',
  9454. ' $mod.DoIt(vH.slice(0), vH, {',
  9455. ' get: function () {',
  9456. ' return vJ;',
  9457. ' },',
  9458. ' set: function (v) {',
  9459. ' vJ = v;',
  9460. ' }',
  9461. ' });',
  9462. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9463. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9464. ' get: function () {',
  9465. ' return vJ;',
  9466. ' },',
  9467. ' set: function (v) {',
  9468. ' vJ = v;',
  9469. ' }',
  9470. ' });',
  9471. '};',
  9472. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9473. ]),
  9474. LinesToStr([
  9475. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9476. ' p: $mod,',
  9477. ' get: function () {',
  9478. ' return this.p.i;',
  9479. ' },',
  9480. ' set: function (v) {',
  9481. ' this.p.i = v;',
  9482. ' }',
  9483. '});'
  9484. ]));
  9485. end;
  9486. procedure TTestModule.TestArrayElement_AsParams;
  9487. begin
  9488. StartProgram(false);
  9489. Add('type integer = longint;');
  9490. Add('type TArrayInt = array of integer;');
  9491. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9492. Add('var vJ: tarrayint;');
  9493. Add('begin');
  9494. Add(' vi:=vi;');
  9495. Add(' doit(vi,vi,vi);');
  9496. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9497. Add('end;');
  9498. Add('var a: TArrayInt;');
  9499. Add('begin');
  9500. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9501. ConvertProgram;
  9502. CheckSource('TestArrayElement_AsParams',
  9503. LinesToStr([ // statements
  9504. 'this.DoIt = function (vG,vH,vI) {',
  9505. ' var vJ = [];',
  9506. ' vI.set(vI.get());',
  9507. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9508. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9509. ' a:1+3,',
  9510. ' p:vJ,',
  9511. ' get: function () {',
  9512. ' return this.p[this.a];',
  9513. ' },',
  9514. ' set: function (v) {',
  9515. ' this.p[this.a] = v;',
  9516. ' }',
  9517. ' });',
  9518. '};',
  9519. 'this.a = [];'
  9520. ]),
  9521. LinesToStr([
  9522. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9523. ' a: 1+6,',
  9524. ' p: $mod.a,',
  9525. ' get: function () {',
  9526. ' return this.p[this.a];',
  9527. ' },',
  9528. ' set: function (v) {',
  9529. ' this.p[this.a] = v;',
  9530. ' }',
  9531. '});'
  9532. ]));
  9533. end;
  9534. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9535. begin
  9536. StartProgram(false);
  9537. Add('type Integer = longint;');
  9538. Add('type TArrayInt = array of integer;');
  9539. Add('function GetArr(vB: integer = 0): tarrayint;');
  9540. Add('begin');
  9541. Add('end;');
  9542. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9543. Add('begin');
  9544. Add('end;');
  9545. Add('begin');
  9546. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9547. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9548. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9549. ConvertProgram;
  9550. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9551. LinesToStr([ // statements
  9552. 'this.GetArr = function (vB) {',
  9553. ' var Result = [];',
  9554. ' return Result;',
  9555. '};',
  9556. 'this.DoIt = function (vG,vH,vI) {',
  9557. '};'
  9558. ]),
  9559. LinesToStr([
  9560. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9561. ' a: 1+3,',
  9562. ' p: $mod.GetArr(0),',
  9563. ' get: function () {',
  9564. ' return this.p[this.a];',
  9565. ' },',
  9566. ' set: function (v) {',
  9567. ' this.p[this.a] = v;',
  9568. ' }',
  9569. '});',
  9570. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9571. ' a: 2+3,',
  9572. ' p: $mod.GetArr(0),',
  9573. ' get: function () {',
  9574. ' return this.p[this.a];',
  9575. ' },',
  9576. ' set: function (v) {',
  9577. ' this.p[this.a] = v;',
  9578. ' }',
  9579. '});',
  9580. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9581. ' a: 3+3,',
  9582. ' p: $mod.GetArr(9),',
  9583. ' get: function () {',
  9584. ' return this.p[this.a];',
  9585. ' },',
  9586. ' set: function (v) {',
  9587. ' this.p[this.a] = v;',
  9588. ' }',
  9589. '});',
  9590. '']));
  9591. end;
  9592. procedure TTestModule.TestArrayEnumTypeRange;
  9593. begin
  9594. StartProgram(false);
  9595. Add([
  9596. 'type',
  9597. ' TEnum = (red,blue);',
  9598. ' TEnumArray = array[TEnum] of longint;',
  9599. 'var',
  9600. ' e: TEnum;',
  9601. ' i: longint;',
  9602. ' a: TEnumArray;',
  9603. ' numbers: TEnumArray = (1,2);',
  9604. ' names: array[TEnum] of string = (''red'',''blue'');',
  9605. 'begin',
  9606. ' e:=low(a);',
  9607. ' e:=high(a);',
  9608. ' i:=a[red];',
  9609. ' a[e]:=a[e];']);
  9610. ConvertProgram;
  9611. CheckSource('TestArrayEnumTypeRange',
  9612. LinesToStr([ // statements
  9613. ' this.TEnum = {',
  9614. ' "0": "red",',
  9615. ' red: 0,',
  9616. ' "1": "blue",',
  9617. ' blue: 1',
  9618. '};',
  9619. 'this.e = 0;',
  9620. 'this.i = 0;',
  9621. 'this.a = rtl.arraySetLength(null,0,2);',
  9622. 'this.numbers = [1, 2];',
  9623. 'this.names = ["red", "blue"];',
  9624. '']),
  9625. LinesToStr([ // $mod.$main
  9626. '$mod.e = $mod.TEnum.red;',
  9627. '$mod.e = $mod.TEnum.blue;',
  9628. '$mod.i = $mod.a[$mod.TEnum.red];',
  9629. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9630. '']));
  9631. end;
  9632. procedure TTestModule.TestArray_SetLengthOutArg;
  9633. begin
  9634. StartProgram(false);
  9635. Add([
  9636. 'type TArrInt = array of longint;',
  9637. 'procedure DoIt(out a: TArrInt);',
  9638. 'begin',
  9639. ' SetLength(a,2);',
  9640. 'end;',
  9641. 'begin',
  9642. '']);
  9643. ConvertProgram;
  9644. CheckSource('TestArray_SetLengthOutArg',
  9645. LinesToStr([ // statements
  9646. 'this.DoIt = function (a) {',
  9647. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9648. '};',
  9649. '']),
  9650. LinesToStr([
  9651. '']));
  9652. end;
  9653. procedure TTestModule.TestArray_SetLengthProperty;
  9654. begin
  9655. StartProgram(false);
  9656. Add('type');
  9657. Add(' TArrInt = array of longint;');
  9658. Add(' TObject = class');
  9659. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9660. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9661. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9662. Add(' end;');
  9663. Add('var Obj: TObject;');
  9664. Add('begin');
  9665. Add(' SetLength(Obj.Colors,2);');
  9666. ConvertProgram;
  9667. CheckSource('TestArray_SetLengthProperty',
  9668. LinesToStr([ // statements
  9669. 'rtl.createClass(this, "TObject", null, function () {',
  9670. ' this.$init = function () {',
  9671. ' };',
  9672. ' this.$final = function () {',
  9673. ' };',
  9674. '});',
  9675. 'this.Obj = null;',
  9676. '']),
  9677. LinesToStr([
  9678. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9679. '']));
  9680. end;
  9681. procedure TTestModule.TestArray_SetLengthMultiDim;
  9682. begin
  9683. StartProgram(false);
  9684. Add([
  9685. 'type',
  9686. ' TArrArrInt = array of array of longint;',
  9687. ' TArrStaInt = array of array[1..2] of longint;',
  9688. 'var',
  9689. ' a: TArrArrInt;',
  9690. ' b: TArrStaInt;',
  9691. 'begin',
  9692. ' SetLength(a,2);',
  9693. ' SetLength(a,3,4);',
  9694. ' SetLength(b,5);',
  9695. '']);
  9696. ConvertProgram;
  9697. CheckSource('TestArray_SetLengthMultiDim',
  9698. LinesToStr([ // statements
  9699. 'this.a = [];',
  9700. 'this.b = [];',
  9701. '']),
  9702. LinesToStr([
  9703. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9704. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9705. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9706. '']));
  9707. end;
  9708. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9709. begin
  9710. StartProgram(false);
  9711. Add([
  9712. 'type',
  9713. ' TStaArr1 = array[1..3] of boolean;',
  9714. //' TStaArr2 = array[5..6] of TStaArr1;',
  9715. ' TDynArr1StaArr1 = array of TStaArr1;',
  9716. //' TDynArr1StaArr2 = array of TStaArr2;',
  9717. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9718. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9719. 'var',
  9720. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9721. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9722. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9723. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9724. 'begin',
  9725. ' SetLength(DynArr1StaArr1,11);',
  9726. ' SetLength(DynArr2StaArr1,12);',
  9727. ' SetLength(DynArr2StaArr1[13],14);',
  9728. ' SetLength(DynArr2StaArr1,15,16);',
  9729. //' SetLength(DynArr1StaArr2,21);',
  9730. //' SetLength(DynArr2StaArr2,22);',
  9731. //' SetLength(DynArr2StaArr2[23],24);',
  9732. //' SetLength(DynArr2StaArr2,25,26);',
  9733. '']);
  9734. ConvertProgram;
  9735. CheckSource('TestArray_DynOfStatic',
  9736. LinesToStr([ // statements
  9737. 'this.DynArr1StaArr1 = [];',
  9738. 'this.DynArr2StaArr1 = [];',
  9739. '']),
  9740. LinesToStr([ // $mod.$main
  9741. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9742. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9743. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9744. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9745. ' $mod.DynArr2StaArr1,',
  9746. ' false,',
  9747. ' 15,',
  9748. ' 16,',
  9749. ' "s",',
  9750. ' 3',
  9751. ');',
  9752. '']));
  9753. end;
  9754. procedure TTestModule.TestArray_OpenArrayOfString;
  9755. begin
  9756. StartProgram(false);
  9757. Add('procedure DoIt(const a: array of String);');
  9758. Add('var');
  9759. Add(' i: longint;');
  9760. Add(' s: string;');
  9761. Add('begin');
  9762. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9763. Add('end;');
  9764. Add('var s: string;');
  9765. Add('begin');
  9766. Add(' DoIt([]);');
  9767. Add(' DoIt([s,''foo'','''',s+s]);');
  9768. ConvertProgram;
  9769. CheckSource('TestArray_OpenArrayOfString',
  9770. LinesToStr([ // statements
  9771. 'this.DoIt = function (a) {',
  9772. ' var i = 0;',
  9773. ' var s = "";',
  9774. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9775. ' i = $l;',
  9776. ' s = a[rtl.length(a) - i - 1];',
  9777. ' };',
  9778. '};',
  9779. 'this.s = "";',
  9780. '']),
  9781. LinesToStr([
  9782. '$mod.DoIt([]);',
  9783. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9784. '']));
  9785. end;
  9786. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9787. begin
  9788. StartProgram(false);
  9789. Add([
  9790. 'type TArr = array of char;',
  9791. 'var',
  9792. ' c: char;',
  9793. ' s: string;',
  9794. ' a: TArr;',
  9795. 'procedure Run(const a: array of char);',
  9796. 'begin',
  9797. ' Run(c);',
  9798. ' Run(s);',
  9799. 'end;',
  9800. 'begin',
  9801. ' a:=c;',
  9802. ' a:=s;',
  9803. ' a:=#13;',
  9804. ' a:=''Foo'';',
  9805. ' Run(c);',
  9806. ' Run(s);',
  9807. '']);
  9808. ConvertProgram;
  9809. CheckSource('TestArray_ArrayOfCharAssignString',
  9810. LinesToStr([ // statements
  9811. 'this.c = "";',
  9812. 'this.s = "";',
  9813. 'this.a = [];',
  9814. 'this.Run = function (a) {',
  9815. ' $mod.Run($mod.c.split(""));',
  9816. ' $mod.Run($mod.s.split(""));',
  9817. '};',
  9818. '']),
  9819. LinesToStr([
  9820. '$mod.a = $mod.c.split("");',
  9821. '$mod.a = $mod.s.split("");',
  9822. '$mod.a = "\r".split("");',
  9823. '$mod.a = "Foo".split("");',
  9824. '$mod.Run($mod.c.split(""));',
  9825. '$mod.Run($mod.s.split(""));',
  9826. '']));
  9827. end;
  9828. procedure TTestModule.TestArray_ConstRef;
  9829. begin
  9830. StartProgram(false);
  9831. Add([
  9832. 'type TArr = array of word;',
  9833. 'procedure Run(constref a: TArr);',
  9834. 'begin',
  9835. 'end;',
  9836. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9837. 'var l: TArr;',
  9838. 'begin',
  9839. ' Run(l);',
  9840. ' Run(a);',
  9841. ' Run(b);',
  9842. ' Run(c);',
  9843. ' Run(d);',
  9844. ' Run(e);',
  9845. 'end;',
  9846. 'begin',
  9847. '']);
  9848. ConvertProgram;
  9849. CheckResolverUnexpectedHints();
  9850. CheckSource('TestArray_ConstRef',
  9851. LinesToStr([ // statements
  9852. 'this.Run = function (a) {',
  9853. '};',
  9854. 'this.Fly = function (a, b, c, d, e) {',
  9855. ' var l = [];',
  9856. ' $mod.Run(l);',
  9857. ' $mod.Run(a);',
  9858. ' $mod.Run(b.get());',
  9859. ' $mod.Run(c.get());',
  9860. ' $mod.Run(d);',
  9861. ' $mod.Run(e);',
  9862. '};',
  9863. '']),
  9864. LinesToStr([
  9865. '']));
  9866. end;
  9867. procedure TTestModule.TestArray_Concat;
  9868. begin
  9869. StartProgram(false);
  9870. Add([
  9871. 'type',
  9872. ' integer = longint;',
  9873. ' TFlag = (big,small);',
  9874. ' TFlags = set of TFlag;',
  9875. ' TRec = record',
  9876. ' i: integer;',
  9877. ' end;',
  9878. ' TArrInt = array of integer;',
  9879. ' TArrRec = array of TRec;',
  9880. ' TArrFlag = array of TFlag;',
  9881. ' TArrSet = array of TFlags;',
  9882. ' TArrJSValue = array of jsvalue;',
  9883. 'var',
  9884. ' ArrInt: tarrint;',
  9885. ' ArrRec: tarrrec;',
  9886. ' ArrFlag: tarrflag;',
  9887. ' ArrSet: tarrset;',
  9888. ' ArrJSValue: tarrjsvalue;',
  9889. 'begin',
  9890. ' arrint:=concat(arrint);',
  9891. ' arrint:=concat(arrint,arrint);',
  9892. ' arrint:=concat(arrint,arrint,arrint);',
  9893. ' arrrec:=concat(arrrec);',
  9894. ' arrrec:=concat(arrrec,arrrec);',
  9895. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9896. ' arrset:=concat(arrset);',
  9897. ' arrset:=concat(arrset,arrset);',
  9898. ' arrset:=concat(arrset,arrset,arrset);',
  9899. ' arrjsvalue:=concat(arrjsvalue);',
  9900. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9901. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9902. ' arrint:=concat([1],arrint);',
  9903. ' arrflag:=concat([big]);',
  9904. ' arrflag:=concat([big],arrflag);',
  9905. ' arrflag:=concat(arrflag,[small]);',
  9906. '']);
  9907. ConvertProgram;
  9908. CheckSource('TestArray_Concat',
  9909. LinesToStr([ // statements
  9910. 'this.TFlag = {',
  9911. ' "0": "big",',
  9912. ' big: 0,',
  9913. ' "1": "small",',
  9914. ' small: 1',
  9915. '};',
  9916. 'rtl.recNewT(this, "TRec", function () {',
  9917. ' this.i = 0;',
  9918. ' this.$eq = function (b) {',
  9919. ' return this.i === b.i;',
  9920. ' };',
  9921. ' this.$assign = function (s) {',
  9922. ' this.i = s.i;',
  9923. ' return this;',
  9924. ' };',
  9925. '});',
  9926. 'this.ArrInt = [];',
  9927. 'this.ArrRec = [];',
  9928. 'this.ArrFlag = [];',
  9929. 'this.ArrSet = [];',
  9930. 'this.ArrJSValue = [];',
  9931. '']),
  9932. LinesToStr([ // $mod.$main
  9933. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9934. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9935. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9936. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9937. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9938. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9939. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9940. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9941. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9942. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9943. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9944. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9945. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9946. '$mod.ArrFlag = [$mod.TFlag.big];',
  9947. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9948. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9949. '']));
  9950. end;
  9951. procedure TTestModule.TestArray_Copy;
  9952. begin
  9953. StartProgram(false);
  9954. Add([
  9955. 'type',
  9956. ' integer = longint;',
  9957. ' TFlag = (big,small);',
  9958. ' TFlags = set of TFlag;',
  9959. ' TRec = record',
  9960. ' i: integer;',
  9961. ' end;',
  9962. ' TArrInt = array of integer;',
  9963. ' TArrRec = array of TRec;',
  9964. ' TArrSet = array of TFlags;',
  9965. ' TArrJSValue = array of jsvalue;',
  9966. 'var',
  9967. ' ArrInt: tarrint;',
  9968. ' ArrRec: tarrrec;',
  9969. ' ArrSet: tarrset;',
  9970. ' ArrJSValue: tarrjsvalue;',
  9971. 'begin',
  9972. ' arrint:=copy(arrint);',
  9973. ' arrint:=copy(arrint,2);',
  9974. ' arrint:=copy(arrint,3,4);',
  9975. ' arrint:=copy([1,1],1,2);',
  9976. ' arrrec:=copy(arrrec);',
  9977. ' arrrec:=copy(arrrec,5);',
  9978. ' arrrec:=copy(arrrec,6,7);',
  9979. ' arrset:=copy(arrset);',
  9980. ' arrset:=copy(arrset,8);',
  9981. ' arrset:=copy(arrset,9,10);',
  9982. ' arrjsvalue:=copy(arrjsvalue);',
  9983. ' arrjsvalue:=copy(arrjsvalue,11);',
  9984. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9985. ' ']);
  9986. ConvertProgram;
  9987. CheckSource('TestArray_Copy',
  9988. LinesToStr([ // statements
  9989. 'this.TFlag = {',
  9990. ' "0": "big",',
  9991. ' big: 0,',
  9992. ' "1": "small",',
  9993. ' small: 1',
  9994. '};',
  9995. 'rtl.recNewT(this, "TRec", function () {',
  9996. ' this.i = 0;',
  9997. ' this.$eq = function (b) {',
  9998. ' return this.i === b.i;',
  9999. ' };',
  10000. ' this.$assign = function (s) {',
  10001. ' this.i = s.i;',
  10002. ' return this;',
  10003. ' };',
  10004. '});',
  10005. 'this.ArrInt = [];',
  10006. 'this.ArrRec = [];',
  10007. 'this.ArrSet = [];',
  10008. 'this.ArrJSValue = [];',
  10009. '']),
  10010. LinesToStr([ // $mod.$main
  10011. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10012. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10013. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10014. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10015. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10016. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10017. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10018. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10019. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10020. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10021. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10022. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10023. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10024. '']));
  10025. end;
  10026. procedure TTestModule.TestArray_InsertDelete;
  10027. begin
  10028. StartProgram(false);
  10029. Add([
  10030. 'type',
  10031. ' integer = longint;',
  10032. ' TFlag = (big,small);',
  10033. ' TFlags = set of TFlag;',
  10034. ' TRec = record',
  10035. ' i: integer;',
  10036. ' end;',
  10037. ' TArrInt = array of integer;',
  10038. ' TArrRec = array of TRec;',
  10039. ' TArrSet = array of TFlags;',
  10040. ' TArrJSValue = array of jsvalue;',
  10041. ' TArrArrInt = array of TArrInt;',
  10042. 'var',
  10043. ' ArrInt: tarrint;',
  10044. ' ArrRec: tarrrec;',
  10045. ' ArrSet: tarrset;',
  10046. ' ArrJSValue: tarrjsvalue;',
  10047. ' ArrArrInt: TArrArrInt;',
  10048. 'begin',
  10049. ' Insert(1,arrint,2);',
  10050. ' Insert(arrint[3],arrint,4);',
  10051. ' Insert(arrrec[5],arrrec,6);',
  10052. ' Insert(arrset[7],arrset,7);',
  10053. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10054. ' Insert(10,arrjsvalue,11);',
  10055. ' Insert([23],arrarrint,22);',
  10056. ' Delete(arrint,12,13);',
  10057. ' Delete(arrrec,14,15);',
  10058. ' Delete(arrset,17,18);',
  10059. ' Delete(arrjsvalue,19,10);']);
  10060. ConvertProgram;
  10061. CheckSource('TestArray_InsertDelete',
  10062. LinesToStr([ // statements
  10063. 'this.TFlag = {',
  10064. ' "0": "big",',
  10065. ' big: 0,',
  10066. ' "1": "small",',
  10067. ' small: 1',
  10068. '};',
  10069. 'rtl.recNewT(this, "TRec", function () {',
  10070. ' this.i = 0;',
  10071. ' this.$eq = function (b) {',
  10072. ' return this.i === b.i;',
  10073. ' };',
  10074. ' this.$assign = function (s) {',
  10075. ' this.i = s.i;',
  10076. ' return this;',
  10077. ' };',
  10078. '});',
  10079. 'this.ArrInt = [];',
  10080. 'this.ArrRec = [];',
  10081. 'this.ArrSet = [];',
  10082. 'this.ArrJSValue = [];',
  10083. 'this.ArrArrInt = [];',
  10084. '']),
  10085. LinesToStr([ // $mod.$main
  10086. '$mod.ArrInt.splice(2, 0, 1);',
  10087. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  10088. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  10089. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  10090. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  10091. '$mod.ArrJSValue.splice(11, 0, 10);',
  10092. '$mod.ArrArrInt.splice(22, 0, [23]);',
  10093. '$mod.ArrInt.splice(12, 13);',
  10094. '$mod.ArrRec.splice(14, 15);',
  10095. '$mod.ArrSet.splice(17, 18);',
  10096. '$mod.ArrJSValue.splice(19, 10);',
  10097. '']));
  10098. end;
  10099. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10100. begin
  10101. Parser.Options:=Parser.Options+[po_cassignments];
  10102. StartProgram(false);
  10103. Add([
  10104. '{$modeswitch arrayoperators}',
  10105. 'type',
  10106. ' integer = longint;',
  10107. ' TArrInt = array of integer;',
  10108. ' TArrStr = array of string;',
  10109. 'const',
  10110. ' Ints: TArrInt = (1,2,3);',
  10111. ' Aliases: TarrStr = (''foo'',''b'');',
  10112. ' OneInt: TArrInt = (7);',
  10113. ' OneStr: array of integer = (7);',
  10114. ' Chars: array of char = ''aoc'';',
  10115. ' Names: array of string = (''a'',''foo'');',
  10116. ' NameCount = low(Names)+high(Names)+length(Names);',
  10117. 'var i: integer;',
  10118. 'begin',
  10119. ' Ints:=[];',
  10120. ' Ints:=[1,1];',
  10121. ' Ints:=[1]+[2];',
  10122. ' Ints:=[2];',
  10123. ' Ints:=[]+ints;',
  10124. ' Ints:=Ints+[];',
  10125. ' Ints:=Ints+OneInt;',
  10126. ' Ints:=Ints+[1,1];',
  10127. ' Ints:=[i,i]+Ints;',
  10128. ' Ints:=[1]+[i]+[3];',
  10129. '']);
  10130. ConvertProgram;
  10131. CheckSource('TestArray_DynArrayConstObjFPC',
  10132. LinesToStr([ // statements
  10133. 'this.Ints = [1, 2, 3];',
  10134. 'this.Aliases = ["foo", "b"];',
  10135. 'this.OneInt = [7];',
  10136. 'this.OneStr = [7];',
  10137. 'this.Chars = ["a", "o", "c"];',
  10138. 'this.Names = ["a", "foo"];',
  10139. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10140. 'this.i = 0;',
  10141. '']),
  10142. LinesToStr([ // $mod.$main
  10143. '$mod.Ints = [];',
  10144. '$mod.Ints = [1, 1];',
  10145. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10146. '$mod.Ints = [2];',
  10147. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10148. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10149. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10150. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10151. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10152. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10153. '']));
  10154. end;
  10155. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10156. begin
  10157. StartProgram(false);
  10158. // Note: const c = [1,1]; defines a set!
  10159. Add([
  10160. '{$mode delphi}',
  10161. 'type',
  10162. ' integer = longint;',
  10163. ' TArrInt = array of integer;',
  10164. ' TArrStr = array of string;',
  10165. 'const',
  10166. ' Ints: TArrInt = [1,1,2];',
  10167. ' Aliases: TarrStr = [''foo'',''b''];',
  10168. ' OneInt: TArrInt = [7];',
  10169. ' OneStr: array of integer = [7]+[8];',
  10170. ' Chars: array of char = ''aoc'';',
  10171. ' Names: array of string = [''a'',''a''];',
  10172. ' NameCount = low(Names)+high(Names)+length(Names);',
  10173. 'begin',
  10174. '']);
  10175. ConvertProgram;
  10176. CheckSource('TestArray_DynArrayConstDelphi',
  10177. LinesToStr([ // statements
  10178. 'this.Ints = [1, 1, 2];',
  10179. 'this.Aliases = ["foo", "b"];',
  10180. 'this.OneInt = [7];',
  10181. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10182. 'this.Chars = ["a", "o", "c"];',
  10183. 'this.Names = ["a", "a"];',
  10184. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10185. '']),
  10186. LinesToStr([ // $mod.$main
  10187. '']));
  10188. end;
  10189. procedure TTestModule.TestArray_ArrayLitAsParam;
  10190. begin
  10191. StartProgram(false);
  10192. Add([
  10193. '{$modeswitch arrayoperators}',
  10194. 'type',
  10195. ' integer = longint;',
  10196. ' TArrInt = array of integer;',
  10197. ' TArrSet = array of (red,green,blue);',
  10198. 'procedure DoOpenInt(const a: array of integer); forward;',
  10199. 'procedure DoInt(const a: TArrInt);',
  10200. 'begin',
  10201. ' DoInt(a+[1]);',
  10202. ' DoInt([1]+a);',
  10203. ' DoOpenInt(a);',
  10204. ' DoOpenInt(a+[1]);',
  10205. ' DoOpenInt([1]+a);',
  10206. 'end;',
  10207. 'procedure DoOpenInt(const a: array of integer);',
  10208. 'begin',
  10209. ' DoOpenInt(a+[1]);',
  10210. ' DoOpenInt([1]+a);',
  10211. ' DoInt(a);',
  10212. ' DoInt(a+[1]);',
  10213. ' DoInt([1]+a);',
  10214. 'end;',
  10215. 'procedure DoSet(const a: TArrSet);',
  10216. 'begin',
  10217. ' DoSet(a+[red]);',
  10218. ' DoSet([blue]+a);',
  10219. 'end;',
  10220. 'var',
  10221. ' i: TArrInt;',
  10222. ' s: TArrSet;',
  10223. 'begin',
  10224. ' DoInt([1]);',
  10225. ' DoInt([1]+[2]);',
  10226. ' DoInt(i+[1]);',
  10227. ' DoInt([1]+i);',
  10228. ' DoOpenInt([1]);',
  10229. ' DoOpenInt([1]+[2]);',
  10230. ' DoOpenInt(i+[1]);',
  10231. ' DoOpenInt([1]+i);',
  10232. ' DoSet([red]);',
  10233. ' DoSet([blue]+[green]);',
  10234. ' DoSet(s+[blue]);',
  10235. ' DoSet([red]+s);',
  10236. '']);
  10237. ConvertProgram;
  10238. CheckSource('TestArray_ArrayLitAsParam',
  10239. LinesToStr([ // statements
  10240. 'this.TArrSet$a = {',
  10241. ' "0": "red",',
  10242. ' red: 0,',
  10243. ' "1": "green",',
  10244. ' green: 1,',
  10245. ' "2": "blue",',
  10246. ' blue: 2',
  10247. '};',
  10248. 'this.DoInt = function (a) {',
  10249. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10250. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10251. ' $mod.DoOpenInt(a);',
  10252. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10253. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10254. '};',
  10255. 'this.DoOpenInt = function (a) {',
  10256. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10257. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10258. ' $mod.DoInt(a);',
  10259. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10260. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10261. '};',
  10262. 'this.DoSet = function (a) {',
  10263. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10264. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10265. '};',
  10266. 'this.i = [];',
  10267. 'this.s = [];',
  10268. '']),
  10269. LinesToStr([ // $mod.$main
  10270. '$mod.DoInt([1]);',
  10271. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10272. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10273. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10274. '$mod.DoOpenInt([1]);',
  10275. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10276. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10277. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10278. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10279. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10280. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10281. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10282. '']));
  10283. end;
  10284. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10285. begin
  10286. StartProgram(false);
  10287. Add([
  10288. '{$modeswitch arrayoperators}',
  10289. 'type',
  10290. ' integer = longint;',
  10291. ' TArrInt = array of integer;',
  10292. ' TArrArrInt = array of TArrInt;',
  10293. 'procedure DoInt(const a: TArrArrInt);',
  10294. 'begin',
  10295. ' DoInt(a+[[1]]);',
  10296. ' DoInt([[1]]+a);',
  10297. ' DoInt(a);',
  10298. 'end;',
  10299. 'var',
  10300. ' i: TArrInt;',
  10301. ' a: TArrArrInt;',
  10302. 'begin',
  10303. ' a:=[[1]];',
  10304. ' a:=[i];',
  10305. ' a:=a+[i];',
  10306. ' a:=[i]+a;',
  10307. ' a:=[[1]+i];',
  10308. ' a:=[[1]+[2]];',
  10309. ' a:=[i+[2]];',
  10310. ' DoInt([[1]]);',
  10311. ' DoInt([[1]+[2],[3,4],[5]]);',
  10312. ' DoInt([i+[1]]+a);',
  10313. ' DoInt([i]+a);',
  10314. '']);
  10315. ConvertProgram;
  10316. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10317. LinesToStr([ // statements
  10318. 'this.DoInt = function (a) {',
  10319. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10320. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10321. ' $mod.DoInt(a);',
  10322. '};',
  10323. 'this.i = [];',
  10324. 'this.a = [];',
  10325. '']),
  10326. LinesToStr([ // $mod.$main
  10327. '$mod.a = [[1]];',
  10328. '$mod.a = [$mod.i];',
  10329. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10330. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10331. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10332. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10333. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10334. '$mod.DoInt([[1]]);',
  10335. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10336. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10337. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10338. '']));
  10339. end;
  10340. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10341. begin
  10342. StartProgram(false);
  10343. Add([
  10344. '{$modeswitch arrayoperators}',
  10345. 'type',
  10346. ' integer = longint;',
  10347. ' TArrInt = array[1..2] of integer;',
  10348. ' TArrArrInt = array of TArrInt;',
  10349. 'procedure DoInt(const a: TArrArrInt);',
  10350. 'begin',
  10351. ' DoInt(a+[[1,2]]);',
  10352. ' DoInt([[1,2]]+a);',
  10353. ' DoInt(a);',
  10354. 'end;',
  10355. 'var',
  10356. ' i: TArrInt;',
  10357. ' a: TArrArrInt;',
  10358. 'begin',
  10359. ' a:=[[1,1]];',
  10360. ' a:=[i];',
  10361. ' a:=a+[i];',
  10362. ' a:=[i]+a;',
  10363. ' DoInt([[1,1]]);',
  10364. ' DoInt([[1,2],[3,4]]);',
  10365. '']);
  10366. ConvertProgram;
  10367. CheckSource('TestArray_ArrayLitStaticAsParam',
  10368. LinesToStr([ // statements
  10369. 'this.DoInt = function (a) {',
  10370. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10371. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10372. ' $mod.DoInt(a);',
  10373. '};',
  10374. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10375. 'this.a = [];',
  10376. '']),
  10377. LinesToStr([ // $mod.$main
  10378. '$mod.a = [[1, 1]];',
  10379. '$mod.a = [$mod.i.slice(0)];',
  10380. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10381. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10382. '$mod.DoInt([[1, 1]]);',
  10383. '$mod.DoInt([[1, 2], [3, 4]]);',
  10384. '']));
  10385. end;
  10386. procedure TTestModule.TestArray_ForInArrOfString;
  10387. begin
  10388. StartProgram(false);
  10389. Add([
  10390. 'type',
  10391. 'type',
  10392. ' TMonthNameArray = array [1..12] of string;',
  10393. ' TMonthNames = TMonthNameArray;',
  10394. ' TObject = class',
  10395. ' private',
  10396. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10397. ' public',
  10398. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10399. ' end;',
  10400. 'var',
  10401. ' f: TObject;',
  10402. ' Month: string;',
  10403. ' Names: array of string = (''a'',''foo'',''bar'');',
  10404. ' i: longint;',
  10405. 'begin',
  10406. ' for Month in f.LongMonthNames do ;',
  10407. ' for Month in Names do ;',
  10408. ' for i:=low(Names) to high(Names) do ;',
  10409. '']);
  10410. ConvertProgram;
  10411. CheckSource('TestArray_ForInArrOfString',
  10412. LinesToStr([ // statements
  10413. 'rtl.createClass(this, "TObject", null, function () {',
  10414. ' this.$init = function () {',
  10415. ' };',
  10416. ' this.$final = function () {',
  10417. ' };',
  10418. '});',
  10419. 'this.f = null;',
  10420. 'this.Month = "";',
  10421. 'this.Names = ["a", "foo", "bar"];',
  10422. 'this.i = 0;',
  10423. '']),
  10424. LinesToStr([ // $mod.$main
  10425. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10426. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10427. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10428. '']));
  10429. end;
  10430. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10431. begin
  10432. StartProgram(false);
  10433. Add([
  10434. '{$modeswitch externalclass}',
  10435. 'type',
  10436. ' TJSObject = class external name ''Object''',
  10437. ' end;',
  10438. ' TJSArray = class external name ''Array''',
  10439. ' class function isArray(Value: JSValue) : boolean;',
  10440. ' function concat() : TJSArray; varargs;',
  10441. ' end;',
  10442. 'var',
  10443. ' aObj: TJSArray;',
  10444. ' a: array of longint;',
  10445. ' o: TJSObject;',
  10446. 'begin',
  10447. ' if TJSArray.isArray(65) then ;',
  10448. ' aObj:=TJSArray(a).concat(a);',
  10449. ' o:=TJSObject(a);']);
  10450. ConvertProgram;
  10451. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10452. LinesToStr([ // statements
  10453. 'this.aObj = null;',
  10454. 'this.a = [];',
  10455. 'this.o = null;',
  10456. '']),
  10457. LinesToStr([ // $mod.$main
  10458. 'if (Array.isArray(65)) ;',
  10459. '$mod.aObj = $mod.a.concat($mod.a);',
  10460. '$mod.o = $mod.a;',
  10461. '']));
  10462. end;
  10463. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10464. begin
  10465. StartProgram(false);
  10466. Add([
  10467. '{$modeswitch externalclass}',
  10468. 'type',
  10469. ' TArrStr = array of string;',
  10470. ' TJSArray = class external name ''Array''',
  10471. ' end;',
  10472. ' TJSObject = class external name ''Object''',
  10473. ' end;',
  10474. 'var',
  10475. ' aObj: TJSArray;',
  10476. ' a: TArrStr;',
  10477. ' jo: TJSObject;',
  10478. 'begin',
  10479. ' a:=TArrStr(aObj);',
  10480. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10481. ' a:=TarrStr(jo);',
  10482. '']);
  10483. ConvertProgram;
  10484. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10485. LinesToStr([ // statements
  10486. 'this.aObj = null;',
  10487. 'this.a = [];',
  10488. 'this.jo = null;',
  10489. '']),
  10490. LinesToStr([ // $mod.$main
  10491. '$mod.a = $mod.aObj;',
  10492. '$mod.aObj[1] = $mod.aObj[2];',
  10493. '$mod.a = $mod.jo;',
  10494. '']));
  10495. end;
  10496. procedure TTestModule.TestArrayOfConst_TVarRec;
  10497. begin
  10498. StartProgram(true,[supTVarRec]);
  10499. Add([
  10500. 'procedure Say(args: array of const);',
  10501. 'var',
  10502. ' i: longint;',
  10503. ' v: TVarRec;',
  10504. 'begin',
  10505. ' for i:=low(args) to high(args) do begin',
  10506. ' v:=args[i];',
  10507. ' case v.vtype of',
  10508. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10509. ' end;',
  10510. ' end;',
  10511. ' for v in args do ;',
  10512. ' args:=nil;',
  10513. ' SetLength(args,2);',
  10514. 'end;',
  10515. 'begin']);
  10516. ConvertProgram;
  10517. CheckSource('TestArrayOfConst_TVarRec',
  10518. LinesToStr([ // statements
  10519. 'this.Say = function (args) {',
  10520. ' var i = 0;',
  10521. ' var v = pas.system.TVarRec.$new();',
  10522. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10523. ' i = $l;',
  10524. ' v.$assign(args[i]);',
  10525. ' var $tmp = v.VType;',
  10526. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10527. ' };',
  10528. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10529. ' args = [];',
  10530. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10531. '};',
  10532. '']),
  10533. LinesToStr([ // $mod.$main
  10534. ]));
  10535. end;
  10536. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10537. begin
  10538. StartProgram(true,[supTVarRec]);
  10539. Add([
  10540. 'procedure Say(args: array of const);',
  10541. 'begin',
  10542. ' Say(args);',
  10543. 'end;',
  10544. 'var',
  10545. ' p: Pointer;',
  10546. ' j: jsvalue;',
  10547. ' c: currency;',
  10548. 'begin',
  10549. ' Say([]);',
  10550. ' Say([1]);',
  10551. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10552. '']);
  10553. ConvertProgram;
  10554. CheckSource('TestArrayOfConst_PassBaseTypes',
  10555. LinesToStr([ // statements
  10556. 'this.Say = function (args) {',
  10557. ' $mod.Say(args);',
  10558. '};',
  10559. 'this.p = null;',
  10560. 'this.j = undefined;',
  10561. 'this.c = 0;',
  10562. '']),
  10563. LinesToStr([ // $mod.$main
  10564. '$mod.Say([]);',
  10565. '$mod.Say(pas.system.VarRecs(0, 1));',
  10566. '$mod.Say(pas.system.VarRecs(',
  10567. ' 9,',
  10568. ' "c",',
  10569. ' 18,',
  10570. ' "foo",',
  10571. ' 5,',
  10572. ' null,',
  10573. ' 1,',
  10574. ' true,',
  10575. ' 3,',
  10576. ' 1.3,',
  10577. ' 5,',
  10578. ' $mod.p,',
  10579. ' 20,',
  10580. ' $mod.j,',
  10581. ' 12,',
  10582. ' $mod.c',
  10583. ' ));',
  10584. '']));
  10585. end;
  10586. procedure TTestModule.TestArrayOfConst_PassObj;
  10587. begin
  10588. StartProgram(true,[supTVarRec]);
  10589. Add([
  10590. '{$interfaces corba}',
  10591. 'type',
  10592. ' TObject = class',
  10593. ' end;',
  10594. ' TClass = class of TObject;',
  10595. ' IUnknown = interface',
  10596. ' end;',
  10597. 'procedure Say(args: array of const);',
  10598. 'begin',
  10599. 'end;',
  10600. 'var',
  10601. ' o: TObject;',
  10602. ' c: TClass;',
  10603. ' i: IUnknown;',
  10604. 'begin',
  10605. ' Say([o,c,TObject]);',
  10606. ' Say([nil,i]);',
  10607. '']);
  10608. ConvertProgram;
  10609. CheckSource('TestArrayOfConst_PassObj',
  10610. LinesToStr([ // statements
  10611. 'rtl.createClass(this, "TObject", null, function () {',
  10612. ' this.$init = function () {',
  10613. ' };',
  10614. ' this.$final = function () {',
  10615. ' };',
  10616. '});',
  10617. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10618. 'this.Say = function (args) {',
  10619. '};',
  10620. 'this.o = null;',
  10621. 'this.c = null;',
  10622. 'this.i = null;',
  10623. '']),
  10624. LinesToStr([ // $mod.$main
  10625. '$mod.Say(pas.system.VarRecs(',
  10626. ' 7,',
  10627. ' $mod.o,',
  10628. ' 8,',
  10629. ' $mod.c,',
  10630. ' 8,',
  10631. ' $mod.TObject',
  10632. '));',
  10633. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10634. '']));
  10635. end;
  10636. procedure TTestModule.TestRecord_Empty;
  10637. begin
  10638. StartProgram(false);
  10639. Add([
  10640. 'type',
  10641. ' TRecA = record',
  10642. ' end;',
  10643. 'var a,b: TRecA;',
  10644. 'begin',
  10645. ' if a=b then ;']);
  10646. ConvertProgram;
  10647. CheckSource('TestRecord_Empty',
  10648. LinesToStr([ // statements
  10649. 'rtl.recNewT(this, "TRecA", function () {',
  10650. ' this.$eq = function (b) {',
  10651. ' return true;',
  10652. ' };',
  10653. ' this.$assign = function (s) {',
  10654. ' return this;',
  10655. ' };',
  10656. '});',
  10657. 'this.a = this.TRecA.$new();',
  10658. 'this.b = this.TRecA.$new();',
  10659. '']),
  10660. LinesToStr([ // $mod.$main
  10661. 'if ($mod.a.$eq($mod.b)) ;'
  10662. ]));
  10663. end;
  10664. procedure TTestModule.TestRecord_Var;
  10665. begin
  10666. StartProgram(false);
  10667. Add('type');
  10668. Add(' TRecA = record');
  10669. Add(' Bold: longint;');
  10670. Add(' end;');
  10671. Add('var Rec: TRecA;');
  10672. Add('begin');
  10673. Add(' rec.bold:=123');
  10674. ConvertProgram;
  10675. CheckSource('TestRecord_Var',
  10676. LinesToStr([ // statements
  10677. 'rtl.recNewT(this, "TRecA", function () {',
  10678. ' this.Bold = 0;',
  10679. ' this.$eq = function (b) {',
  10680. ' return this.Bold === b.Bold;',
  10681. ' };',
  10682. ' this.$assign = function (s) {',
  10683. ' this.Bold = s.Bold;',
  10684. ' return this;',
  10685. ' };',
  10686. '});',
  10687. 'this.Rec = this.TRecA.$new();',
  10688. '']),
  10689. LinesToStr([ // $mod.$main
  10690. '$mod.Rec.Bold = 123;'
  10691. ]));
  10692. end;
  10693. procedure TTestModule.TestRecord_VarExternal;
  10694. begin
  10695. StartProgram(false);
  10696. Add([
  10697. '{$modeswitch externalclass}',
  10698. 'type',
  10699. ' TRecA = record',
  10700. ' i: byte;',
  10701. ' length_: longint external name ''length'';',
  10702. ' end;',
  10703. 'var Rec: TRecA;',
  10704. 'begin',
  10705. ' rec.length_ := rec.length_',
  10706. '']);
  10707. ConvertProgram;
  10708. CheckSource('TestRecord_VarExternal',
  10709. LinesToStr([ // statements
  10710. 'rtl.recNewT(this, "TRecA", function () {',
  10711. ' this.i = 0;',
  10712. ' this.$eq = function (b) {',
  10713. ' return (this.i === b.i) && (this.length === b.length);',
  10714. ' };',
  10715. ' this.$assign = function (s) {',
  10716. ' this.i = s.i;',
  10717. ' this.length = s.length;',
  10718. ' return this;',
  10719. ' };',
  10720. '});',
  10721. 'this.Rec = this.TRecA.$new();',
  10722. '']),
  10723. LinesToStr([ // $mod.$main
  10724. '$mod.Rec.length = $mod.Rec.length;'
  10725. ]));
  10726. end;
  10727. procedure TTestModule.TestRecord_WithDo;
  10728. begin
  10729. StartProgram(false);
  10730. Add('type');
  10731. Add(' TRec = record');
  10732. Add(' vI: longint;');
  10733. Add(' end;');
  10734. Add('var');
  10735. Add(' Int: longint;');
  10736. Add(' r: TRec;');
  10737. Add('begin');
  10738. Add(' with r do');
  10739. Add(' int:=vi;');
  10740. Add(' with r do begin');
  10741. Add(' int:=vi;');
  10742. Add(' vi:=int;');
  10743. Add(' end;');
  10744. ConvertProgram;
  10745. CheckSource('TestWithRecordDo',
  10746. LinesToStr([ // statements
  10747. 'rtl.recNewT(this, "TRec", function () {',
  10748. ' this.vI = 0;',
  10749. ' this.$eq = function (b) {',
  10750. ' return this.vI === b.vI;',
  10751. ' };',
  10752. ' this.$assign = function (s) {',
  10753. ' this.vI = s.vI;',
  10754. ' return this;',
  10755. ' };',
  10756. '});',
  10757. 'this.Int = 0;',
  10758. 'this.r = this.TRec.$new();',
  10759. '']),
  10760. LinesToStr([ // $mod.$main
  10761. 'var $with = $mod.r;',
  10762. '$mod.Int = $with.vI;',
  10763. 'var $with1 = $mod.r;',
  10764. '$mod.Int = $with1.vI;',
  10765. '$with1.vI = $mod.Int;'
  10766. ]));
  10767. end;
  10768. procedure TTestModule.TestRecord_Assign;
  10769. begin
  10770. StartProgram(false);
  10771. Add([
  10772. 'type',
  10773. ' TEnum = (red,green);',
  10774. ' TEnums = set of TEnum;',
  10775. ' TSmallRec = record',
  10776. ' N: longint;',
  10777. ' end;',
  10778. ' TBigRec = record',
  10779. ' Int: longint;',
  10780. ' D: double;',
  10781. ' Arr: array of longint;',
  10782. ' Arr2: array[1..2] of longint;',
  10783. ' Small: TSmallRec;',
  10784. ' Enums: TEnums;',
  10785. ' end;',
  10786. 'var',
  10787. ' r, s: TBigRec;',
  10788. 'begin',
  10789. ' r:=s;',
  10790. ' r:=default(TBigRec);',
  10791. ' r:=default(s);',
  10792. '']);
  10793. ConvertProgram;
  10794. CheckSource('TestRecord_Assign',
  10795. LinesToStr([ // statements
  10796. 'this.TEnum = {',
  10797. ' "0": "red",',
  10798. ' red: 0,',
  10799. ' "1": "green",',
  10800. ' green: 1',
  10801. '};',
  10802. 'rtl.recNewT(this, "TSmallRec", function () {',
  10803. ' this.N = 0;',
  10804. ' this.$eq = function (b) {',
  10805. ' return this.N === b.N;',
  10806. ' };',
  10807. ' this.$assign = function (s) {',
  10808. ' this.N = s.N;',
  10809. ' return this;',
  10810. ' };',
  10811. '});',
  10812. 'rtl.recNewT(this, "TBigRec", function () {',
  10813. ' this.Int = 0;',
  10814. ' this.D = 0.0;',
  10815. ' this.$new = function () {',
  10816. ' var r = Object.create(this);',
  10817. ' r.Arr = [];',
  10818. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10819. ' r.Small = $mod.TSmallRec.$new();',
  10820. ' r.Enums = {};',
  10821. ' return r;',
  10822. ' };',
  10823. ' this.$eq = function (b) {',
  10824. ' 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);',
  10825. ' };',
  10826. ' this.$assign = function (s) {',
  10827. ' this.Int = s.Int;',
  10828. ' this.D = s.D;',
  10829. ' this.Arr = rtl.arrayRef(s.Arr);',
  10830. ' this.Arr2 = s.Arr2.slice(0);',
  10831. ' this.Small.$assign(s.Small);',
  10832. ' this.Enums = rtl.refSet(s.Enums);',
  10833. ' return this;',
  10834. ' };',
  10835. '});',
  10836. 'this.r = this.TBigRec.$new();',
  10837. 'this.s = this.TBigRec.$new();',
  10838. '']),
  10839. LinesToStr([ // $mod.$main
  10840. '$mod.r.$assign($mod.s);',
  10841. '$mod.r.$assign($mod.TBigRec.$new());',
  10842. '$mod.r.$assign($mod.TBigRec.$new());',
  10843. '']));
  10844. end;
  10845. procedure TTestModule.TestRecord_AsParams;
  10846. begin
  10847. StartProgram(false);
  10848. Add([
  10849. 'type',
  10850. ' integer = longint;',
  10851. ' TRecord = record',
  10852. ' i: integer;',
  10853. ' end;',
  10854. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10855. 'var vL: TRecord;',
  10856. 'begin',
  10857. ' vd:=vd;',
  10858. ' vd.i:=vd.i;',
  10859. ' vl:=vc;',
  10860. ' vv:=vv;',
  10861. ' vv.i:=vv.i;',
  10862. ' U:=vl;',
  10863. ' U:=vd;',
  10864. ' U:=vc;',
  10865. ' U:=vv;',
  10866. ' vl:=TRecord(U);',
  10867. ' vd:=TRecord(U);',
  10868. ' vv:=TRecord(U);',
  10869. ' doit(vd,vd,vd,vd);',
  10870. ' doit(vc,vc,vl,vl);',
  10871. ' doit(vv,vv,vv,vv);',
  10872. ' doit(vl,vl,vl,vl);',
  10873. ' TRecord(U).i:=3;',
  10874. 'end;',
  10875. 'var i: TRecord;',
  10876. 'begin',
  10877. ' doit(i,i,i,i);',
  10878. '']);
  10879. ConvertProgram;
  10880. CheckSource('TestRecord_AsParams',
  10881. LinesToStr([ // statements
  10882. 'rtl.recNewT(this, "TRecord", function () {',
  10883. ' this.i = 0;',
  10884. ' this.$eq = function (b) {',
  10885. ' return this.i === b.i;',
  10886. ' };',
  10887. ' this.$assign = function (s) {',
  10888. ' this.i = s.i;',
  10889. ' return this;',
  10890. ' };',
  10891. '});',
  10892. 'this.DoIt = function (vD, vC, vV, U) {',
  10893. ' var vL = $mod.TRecord.$new();',
  10894. ' vD.$assign(vD);',
  10895. ' vD.i = vD.i;',
  10896. ' vL.$assign(vC);',
  10897. ' vV.$assign(vV);',
  10898. ' vV.i = vV.i;',
  10899. ' U.$assign(vL);',
  10900. ' U.$assign(vD);',
  10901. ' U.$assign(vC);',
  10902. ' U.$assign(vV);',
  10903. ' vL.$assign(U);',
  10904. ' vD.$assign(U);',
  10905. ' vV.$assign(U);',
  10906. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10907. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10908. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10909. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10910. ' U.i = 3;',
  10911. '};',
  10912. 'this.i = this.TRecord.$new();'
  10913. ]),
  10914. LinesToStr([
  10915. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10916. '']));
  10917. end;
  10918. procedure TTestModule.TestRecord_ConstRef;
  10919. begin
  10920. StartProgram(false);
  10921. Add([
  10922. 'type TRec = record i: word; end;',
  10923. 'procedure Run(constref a: TRec);',
  10924. 'begin',
  10925. 'end;',
  10926. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10927. 'var l: TRec;',
  10928. 'begin',
  10929. ' Run(l);',
  10930. ' Run(a);',
  10931. ' Run(b);',
  10932. ' Run(c);',
  10933. ' Run(d);',
  10934. ' Run(e);',
  10935. 'end;',
  10936. 'begin',
  10937. '']);
  10938. ConvertProgram;
  10939. CheckResolverUnexpectedHints();
  10940. CheckSource('TestRecord_ConstRef',
  10941. LinesToStr([ // statements
  10942. 'rtl.recNewT(this, "TRec", function () {',
  10943. ' this.i = 0;',
  10944. ' this.$eq = function (b) {',
  10945. ' return this.i === b.i;',
  10946. ' };',
  10947. ' this.$assign = function (s) {',
  10948. ' this.i = s.i;',
  10949. ' return this;',
  10950. ' };',
  10951. '});',
  10952. 'this.Run = function (a) {',
  10953. '};',
  10954. 'this.Fly = function (a, b, c, d, e) {',
  10955. ' var l = $mod.TRec.$new();',
  10956. ' $mod.Run(l);',
  10957. ' $mod.Run(a);',
  10958. ' $mod.Run(b);',
  10959. ' $mod.Run(c);',
  10960. ' $mod.Run(d);',
  10961. ' $mod.Run(e);',
  10962. '};',
  10963. '']),
  10964. LinesToStr([
  10965. '']));
  10966. end;
  10967. procedure TTestModule.TestRecordElement_AsParams;
  10968. begin
  10969. StartProgram(false);
  10970. Add('type');
  10971. Add(' integer = longint;');
  10972. Add(' TRecord = record');
  10973. Add(' i: integer;');
  10974. Add(' end;');
  10975. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10976. Add('var vJ: TRecord;');
  10977. Add('begin');
  10978. Add(' doit(vj.i,vj.i,vj.i);');
  10979. Add('end;');
  10980. Add('var r: TRecord;');
  10981. Add('begin');
  10982. Add(' doit(r.i,r.i,r.i);');
  10983. ConvertProgram;
  10984. CheckSource('TestRecordElement_AsParams',
  10985. LinesToStr([ // statements
  10986. 'rtl.recNewT(this, "TRecord", function () {',
  10987. ' this.i = 0;',
  10988. ' this.$eq = function (b) {',
  10989. ' return this.i === b.i;',
  10990. ' };',
  10991. ' this.$assign = function (s) {',
  10992. ' this.i = s.i;',
  10993. ' return this;',
  10994. ' };',
  10995. '});',
  10996. 'this.DoIt = function (vG,vH,vI) {',
  10997. ' var vJ = $mod.TRecord.$new();',
  10998. ' $mod.DoIt(vJ.i, vJ.i, {',
  10999. ' p: vJ,',
  11000. ' get: function () {',
  11001. ' return this.p.i;',
  11002. ' },',
  11003. ' set: function (v) {',
  11004. ' this.p.i = v;',
  11005. ' }',
  11006. ' });',
  11007. '};',
  11008. 'this.r = this.TRecord.$new();'
  11009. ]),
  11010. LinesToStr([
  11011. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11012. ' p: $mod.r,',
  11013. ' get: function () {',
  11014. ' return this.p.i;',
  11015. ' },',
  11016. ' set: function (v) {',
  11017. ' this.p.i = v;',
  11018. ' }',
  11019. '});'
  11020. ]));
  11021. end;
  11022. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11023. begin
  11024. StartProgram(false);
  11025. Add('type');
  11026. Add(' integer = longint;');
  11027. Add(' TRecord = record');
  11028. Add(' i: integer;');
  11029. Add(' end;');
  11030. Add('function GetRec(vB: integer = 0): TRecord;');
  11031. Add('begin');
  11032. Add('end;');
  11033. Add('procedure DoIt(vG: integer; const vH: integer);');
  11034. Add('begin');
  11035. Add('end;');
  11036. Add('begin');
  11037. Add(' doit(getrec.i,getrec.i);');
  11038. Add(' doit(getrec().i,getrec().i);');
  11039. Add(' doit(getrec(1).i,getrec(2).i);');
  11040. ConvertProgram;
  11041. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11042. LinesToStr([ // statements
  11043. 'rtl.recNewT(this, "TRecord", function () {',
  11044. ' this.i = 0;',
  11045. ' this.$eq = function (b) {',
  11046. ' return this.i === b.i;',
  11047. ' };',
  11048. ' this.$assign = function (s) {',
  11049. ' this.i = s.i;',
  11050. ' return this;',
  11051. ' };',
  11052. '});',
  11053. 'this.GetRec = function (vB) {',
  11054. ' var Result = $mod.TRecord.$new();',
  11055. ' return Result;',
  11056. '};',
  11057. 'this.DoIt = function (vG, vH) {',
  11058. '};',
  11059. '']),
  11060. LinesToStr([
  11061. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11062. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11063. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11064. '']));
  11065. end;
  11066. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11067. begin
  11068. StartProgram(false);
  11069. Add('type');
  11070. Add(' integer = longint;');
  11071. Add(' TRecord = record');
  11072. Add(' i: integer;');
  11073. Add(' end;');
  11074. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11075. Add('begin');
  11076. Add('end;');
  11077. Add('var r: trecord;');
  11078. Add('begin');
  11079. Add(' with r do ');
  11080. Add(' doit(i,i,i);');
  11081. ConvertProgram;
  11082. CheckSource('TestRecordElementFromWith_AsParams',
  11083. LinesToStr([ // statements
  11084. 'rtl.recNewT(this, "TRecord", function () {',
  11085. ' this.i = 0;',
  11086. ' this.$eq = function (b) {',
  11087. ' return this.i === b.i;',
  11088. ' };',
  11089. ' this.$assign = function (s) {',
  11090. ' this.i = s.i;',
  11091. ' return this;',
  11092. ' };',
  11093. '});',
  11094. 'this.DoIt = function (vG,vH,vI) {',
  11095. '};',
  11096. 'this.r = this.TRecord.$new();'
  11097. ]),
  11098. LinesToStr([
  11099. 'var $with = $mod.r;',
  11100. '$mod.DoIt($with.i,$with.i,{',
  11101. ' p: $with,',
  11102. ' get: function () {',
  11103. ' return this.p.i;',
  11104. ' },',
  11105. ' set: function (v) {',
  11106. ' this.p.i = v;',
  11107. ' }',
  11108. '});',
  11109. '']));
  11110. end;
  11111. procedure TTestModule.TestRecord_Equal;
  11112. begin
  11113. StartProgram(false);
  11114. Add('type');
  11115. Add(' integer = longint;');
  11116. Add(' TFlag = (red,blue);');
  11117. Add(' TFlags = set of TFlag;');
  11118. Add(' TProc = procedure;');
  11119. Add(' TRecord = record');
  11120. Add(' i: integer;');
  11121. Add(' Event: TProc;');
  11122. Add(' f: TFlags;');
  11123. Add(' end;');
  11124. Add(' TNested = record');
  11125. Add(' r: TRecord;');
  11126. Add(' end;');
  11127. Add('var');
  11128. Add(' b: boolean;');
  11129. Add(' r,s: trecord;');
  11130. Add('begin');
  11131. Add(' b:=r=s;');
  11132. Add(' b:=r<>s;');
  11133. ConvertProgram;
  11134. CheckSource('TestRecord_Equal',
  11135. LinesToStr([ // statements
  11136. 'this.TFlag = {',
  11137. ' "0": "red",',
  11138. ' red: 0,',
  11139. ' "1": "blue",',
  11140. ' blue: 1',
  11141. '};',
  11142. 'rtl.recNewT(this, "TRecord", function () {',
  11143. ' this.i = 0;',
  11144. ' this.Event = null;',
  11145. ' this.$new = function () {',
  11146. ' var r = Object.create(this);',
  11147. ' r.f = {};',
  11148. ' return r;',
  11149. ' };',
  11150. ' this.$eq = function (b) {',
  11151. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11152. ' };',
  11153. ' this.$assign = function (s) {',
  11154. ' this.i = s.i;',
  11155. ' this.Event = s.Event;',
  11156. ' this.f = rtl.refSet(s.f);',
  11157. ' return this;',
  11158. ' };',
  11159. '});',
  11160. 'rtl.recNewT(this, "TNested", function () {',
  11161. ' this.$new = function () {',
  11162. ' var r = Object.create(this);',
  11163. ' r.r = $mod.TRecord.$new();',
  11164. ' return r;',
  11165. ' };',
  11166. ' this.$eq = function (b) {',
  11167. ' return this.r.$eq(b.r);',
  11168. ' };',
  11169. ' this.$assign = function (s) {',
  11170. ' this.r.$assign(s.r);',
  11171. ' return this;',
  11172. ' };',
  11173. '});',
  11174. 'this.b = false;',
  11175. 'this.r = this.TRecord.$new();',
  11176. 'this.s = this.TRecord.$new();',
  11177. '']),
  11178. LinesToStr([
  11179. '$mod.b = $mod.r.$eq($mod.s);',
  11180. '$mod.b = !$mod.r.$eq($mod.s);',
  11181. '']));
  11182. end;
  11183. procedure TTestModule.TestRecord_JSValue;
  11184. begin
  11185. StartProgram(false);
  11186. Add([
  11187. 'type',
  11188. ' TRecord = record',
  11189. ' i: longint;',
  11190. ' end;',
  11191. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11192. 'begin',
  11193. 'end;',
  11194. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11195. 'begin',
  11196. ' if jsvalue(d) then ;',
  11197. ' if jsvalue(c) then ;',
  11198. ' if jsvalue(v) then ;',
  11199. 'end;',
  11200. 'var',
  11201. ' Jv: jsvalue;',
  11202. ' Rec: trecord;',
  11203. 'begin',
  11204. ' rec:=trecord(jv);',
  11205. ' jv:=rec;',
  11206. ' Fly(rec,rec);',
  11207. ' Fly(@rec,@rec);',
  11208. ' if jsvalue(Rec) then ;',
  11209. ' Run(trecord(jv),trecord(jv),rec);',
  11210. '']);
  11211. ConvertProgram;
  11212. CheckSource('TestRecord_JSValue',
  11213. LinesToStr([ // statements
  11214. 'rtl.recNewT(this, "TRecord", function () {',
  11215. ' this.i = 0;',
  11216. ' this.$eq = function (b) {',
  11217. ' return this.i === b.i;',
  11218. ' };',
  11219. ' this.$assign = function (s) {',
  11220. ' this.i = s.i;',
  11221. ' return this;',
  11222. ' };',
  11223. '});',
  11224. 'this.Fly = function (d, c) {',
  11225. '};',
  11226. 'this.Run = function (d, c, v) {',
  11227. ' if (d) ;',
  11228. ' if (c) ;',
  11229. ' if (v) ;',
  11230. '};',
  11231. 'this.Jv = undefined;',
  11232. 'this.Rec = this.TRecord.$new();',
  11233. '']),
  11234. LinesToStr([
  11235. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11236. '$mod.Jv = $mod.Rec;',
  11237. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11238. '$mod.Fly($mod.Rec, $mod.Rec);',
  11239. 'if ($mod.Rec) ;',
  11240. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11241. '']));
  11242. end;
  11243. procedure TTestModule.TestRecord_VariantFail;
  11244. begin
  11245. StartProgram(false);
  11246. Add([
  11247. 'type',
  11248. ' TRec = record',
  11249. ' case word of',
  11250. ' 0: (b0, b1: Byte);',
  11251. ' 1: (i: word);',
  11252. ' end;',
  11253. 'begin']);
  11254. SetExpectedPasResolverError('variant record is not supported',
  11255. nXIsNotSupported);
  11256. ConvertProgram;
  11257. end;
  11258. procedure TTestModule.TestRecord_FieldArray;
  11259. begin
  11260. StartProgram(false);
  11261. Add([
  11262. 'type',
  11263. ' TArrInt = array[3..4] of longint;',
  11264. ' TArrArrInt = array[3..4] of longint;',
  11265. ' TRec = record',
  11266. ' a: array of longint;',
  11267. ' s: array[1..2] of longint;',
  11268. ' m: array[1..2,3..4] of longint;',
  11269. ' o: TArrArrInt;',
  11270. ' end;',
  11271. 'begin']);
  11272. ConvertProgram;
  11273. CheckSource('TestRecord_FieldArray',
  11274. LinesToStr([ // statements
  11275. 'rtl.recNewT(this, "TRec", function () {',
  11276. ' this.$new = function () {',
  11277. ' var r = Object.create(this);',
  11278. ' r.a = [];',
  11279. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11280. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11281. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11282. ' return r;',
  11283. ' };',
  11284. ' this.$eq = function (b) {',
  11285. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11286. ' };',
  11287. ' this.$assign = function (s) {',
  11288. ' this.a = rtl.arrayRef(s.a);',
  11289. ' this.s = s.s.slice(0);',
  11290. ' this.m = s.m.slice(0);',
  11291. ' this.o = s.o.slice(0);',
  11292. ' return this;',
  11293. ' };',
  11294. '});',
  11295. '']),
  11296. LinesToStr([ // $mod.$main
  11297. '']));
  11298. end;
  11299. procedure TTestModule.TestRecord_Const;
  11300. begin
  11301. StartProgram(false);
  11302. Add([
  11303. 'type',
  11304. ' TArrInt = array[3..4] of longint;',
  11305. ' TPoint = record x,y: longint; end;',
  11306. ' TRec = record',
  11307. ' i: longint;',
  11308. ' a: array of longint;',
  11309. ' s: array[1..2] of longint;',
  11310. ' m: array[1..2,3..4] of longint;',
  11311. ' p: TPoint;',
  11312. ' end;',
  11313. ' TPoints = array of TPoint;',
  11314. 'const',
  11315. ' r: TRec = (',
  11316. ' i:1;',
  11317. ' a:(2,3);',
  11318. ' s:(4,5);',
  11319. ' m:( (11,12), (13,14) );',
  11320. ' p: (x:21; y:22)',
  11321. ' );',
  11322. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11323. 'begin']);
  11324. ConvertProgram;
  11325. CheckSource('TestRecord_Const',
  11326. LinesToStr([ // statements
  11327. 'rtl.recNewT(this, "TPoint", function () {',
  11328. ' this.x = 0;',
  11329. ' this.y = 0;',
  11330. ' this.$eq = function (b) {',
  11331. ' return (this.x === b.x) && (this.y === b.y);',
  11332. ' };',
  11333. ' this.$assign = function (s) {',
  11334. ' this.x = s.x;',
  11335. ' this.y = s.y;',
  11336. ' return this;',
  11337. ' };',
  11338. '});',
  11339. 'rtl.recNewT(this, "TRec", function () {',
  11340. ' this.i = 0;',
  11341. ' this.$new = function () {',
  11342. ' var r = Object.create(this);',
  11343. ' r.a = [];',
  11344. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11345. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11346. ' r.p = $mod.TPoint.$new();',
  11347. ' return r;',
  11348. ' };',
  11349. ' this.$eq = function (b) {',
  11350. ' 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);',
  11351. ' };',
  11352. ' this.$assign = function (s) {',
  11353. ' this.i = s.i;',
  11354. ' this.a = rtl.arrayRef(s.a);',
  11355. ' this.s = s.s.slice(0);',
  11356. ' this.m = s.m.slice(0);',
  11357. ' this.p.$assign(s.p);',
  11358. ' return this;',
  11359. ' };',
  11360. '});',
  11361. 'this.r = this.TRec.$clone({',
  11362. ' i: 1,',
  11363. ' a: [2, 3],',
  11364. ' s: [4, 5],',
  11365. ' m: [[11, 12], [13, 14]],',
  11366. ' p: this.TPoint.$clone({',
  11367. ' x: 21,',
  11368. ' y: 22',
  11369. ' })',
  11370. '});',
  11371. 'this.p = [this.TPoint.$clone({',
  11372. ' x: 1,',
  11373. ' y: 2',
  11374. '}), this.TPoint.$clone({',
  11375. ' x: 3,',
  11376. ' y: 4',
  11377. '})];',
  11378. '']),
  11379. LinesToStr([ // $mod.$main
  11380. '']));
  11381. end;
  11382. procedure TTestModule.TestRecord_TypecastFail;
  11383. begin
  11384. StartProgram(false);
  11385. Add([
  11386. 'type',
  11387. ' TPoint = record x,y: longint; end;',
  11388. ' TRec = record l: longint end;',
  11389. 'var p: TPoint;',
  11390. 'begin',
  11391. ' if TRec(p).l=2 then ;']);
  11392. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11393. nIllegalTypeConversionTo);
  11394. ConvertProgram;
  11395. end;
  11396. procedure TTestModule.TestRecord_InFunction;
  11397. begin
  11398. StartProgram(false);
  11399. Add([
  11400. 'var TPoint: longint = 3;',
  11401. 'procedure DoIt;',
  11402. 'type',
  11403. ' TPoint = record x,y: longint; end;',
  11404. ' TPoints = array of TPoint;',
  11405. 'var',
  11406. ' r: TPoint;',
  11407. ' p: TPoints;',
  11408. 'begin',
  11409. ' SetLength(p,2);',
  11410. 'end;',
  11411. 'begin']);
  11412. ConvertProgram;
  11413. CheckSource('TestRecord_InFunction',
  11414. LinesToStr([ // statements
  11415. 'this.TPoint = 3;',
  11416. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11417. ' this.x = 0;',
  11418. ' this.y = 0;',
  11419. ' this.$eq = function (b) {',
  11420. ' return (this.x === b.x) && (this.y === b.y);',
  11421. ' };',
  11422. ' this.$assign = function (s) {',
  11423. ' this.x = s.x;',
  11424. ' this.y = s.y;',
  11425. ' return this;',
  11426. ' };',
  11427. '});',
  11428. 'this.DoIt = function () {',
  11429. ' var r = TPoint$1.$new();',
  11430. ' var p = [];',
  11431. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11432. '};',
  11433. '']),
  11434. LinesToStr([ // $mod.$main
  11435. '']));
  11436. end;
  11437. procedure TTestModule.TestRecord_AnonymousFail;
  11438. begin
  11439. StartProgram(false);
  11440. Add([
  11441. 'var',
  11442. ' r: record x: word end;',
  11443. 'begin']);
  11444. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11445. nNotYetImplemented);
  11446. ConvertProgram;
  11447. end;
  11448. procedure TTestModule.TestAdvRecord_Function;
  11449. begin
  11450. StartProgram(false);
  11451. Parser.Options:=Parser.Options+[po_cassignments];
  11452. Add([
  11453. '{$modeswitch AdvancedRecords}',
  11454. 'type',
  11455. ' TPoint = record',
  11456. ' x,y: word;',
  11457. ' function Add(const apt: TPoint): TPoint;',
  11458. ' end;',
  11459. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11460. 'begin',
  11461. ' Result:=Self;',
  11462. ' Result.x+=apt.x;',
  11463. ' Result.y:=Result.y+apt.y;',
  11464. ' Self:=apt;',
  11465. 'end;',
  11466. 'var p,q: TPoint;',
  11467. 'begin',
  11468. ' p.add(q);',
  11469. ' p:=default(TPoint);',
  11470. ' p:=q;',
  11471. '']);
  11472. ConvertProgram;
  11473. CheckSource('TestAdvRecord_Function',
  11474. LinesToStr([ // statements
  11475. 'rtl.recNewT(this, "TPoint", function () {',
  11476. ' this.x = 0;',
  11477. ' this.y = 0;',
  11478. ' this.$eq = function (b) {',
  11479. ' return (this.x === b.x) && (this.y === b.y);',
  11480. ' };',
  11481. ' this.$assign = function (s) {',
  11482. ' this.x = s.x;',
  11483. ' this.y = s.y;',
  11484. ' return this;',
  11485. ' };',
  11486. ' this.Add = function (apt) {',
  11487. ' var Result = $mod.TPoint.$new();',
  11488. ' Result.$assign(this);',
  11489. ' Result.x += apt.x;',
  11490. ' Result.y = Result.y + apt.y;',
  11491. ' this.$assign(apt);',
  11492. ' return Result;',
  11493. ' };',
  11494. '});',
  11495. 'this.p = this.TPoint.$new();',
  11496. 'this.q = this.TPoint.$new();',
  11497. '']),
  11498. LinesToStr([ // $mod.$main
  11499. '$mod.p.Add($mod.q);',
  11500. '$mod.p.$assign($mod.TPoint.$new());',
  11501. '$mod.p.$assign($mod.q);',
  11502. '']));
  11503. end;
  11504. procedure TTestModule.TestAdvRecord_Property;
  11505. begin
  11506. StartProgram(false);
  11507. Add([
  11508. '{$modeswitch AdvancedRecords}',
  11509. 'type',
  11510. ' TPoint = record',
  11511. ' x,y: word;',
  11512. ' strict private',
  11513. ' function GetSize: longword;',
  11514. ' procedure SetSize(Value: longword);',
  11515. ' public',
  11516. ' property Size: longword read GetSize write SetSize;',
  11517. ' property Left: word read x write y;',
  11518. ' end;',
  11519. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11520. 'function TPoint.GetSize: longword;',
  11521. 'begin',
  11522. ' x:=y;',
  11523. ' Size:=Size;',
  11524. ' Left:=Left;',
  11525. 'end;',
  11526. 'procedure TPoint.SetSize(Value: longword);',
  11527. 'begin',
  11528. 'end;',
  11529. 'var p,q: TPoint;',
  11530. 'begin',
  11531. ' p.Size:=q.Size;',
  11532. ' p.Left:=q.Left;',
  11533. '']);
  11534. ConvertProgram;
  11535. CheckSource('TestAdvRecord_Property',
  11536. LinesToStr([ // statements
  11537. 'rtl.recNewT(this, "TPoint", function () {',
  11538. ' this.x = 0;',
  11539. ' this.y = 0;',
  11540. ' this.$eq = function (b) {',
  11541. ' return (this.x === b.x) && (this.y === b.y);',
  11542. ' };',
  11543. ' this.$assign = function (s) {',
  11544. ' this.x = s.x;',
  11545. ' this.y = s.y;',
  11546. ' return this;',
  11547. ' };',
  11548. ' this.GetSize = function () {',
  11549. ' var Result = 0;',
  11550. ' this.x = this.y;',
  11551. ' this.SetSize(this.GetSize());',
  11552. ' this.y = this.x;',
  11553. ' return Result;',
  11554. ' };',
  11555. ' this.SetSize = function (Value) {',
  11556. ' };',
  11557. '});',
  11558. 'this.SetSize = function (Value) {',
  11559. '};',
  11560. 'this.p = this.TPoint.$new();',
  11561. 'this.q = this.TPoint.$new();',
  11562. '']),
  11563. LinesToStr([ // $mod.$main
  11564. '$mod.p.SetSize($mod.q.GetSize());',
  11565. '$mod.p.y = $mod.q.x;',
  11566. '']));
  11567. end;
  11568. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11569. begin
  11570. StartProgram(false);
  11571. Add([
  11572. '{$modeswitch AdvancedRecords}',
  11573. 'type',
  11574. ' TPoint = record',
  11575. ' strict private',
  11576. ' function GetItems(Index: word): word;',
  11577. ' procedure SetItems(Index: word; Value: word);',
  11578. ' public',
  11579. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11580. ' end;',
  11581. 'function TPoint.GetItems(Index: word): word;',
  11582. 'begin',
  11583. ' Items[index]:=Items[index];',
  11584. ' self.Items[index]:=self.Items[index];',
  11585. 'end;',
  11586. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11587. 'begin',
  11588. 'end;',
  11589. 'var p: TPoint;',
  11590. 'begin',
  11591. ' p[1]:=p[2];',
  11592. ' p.Items[3]:=p.Items[4];',
  11593. '']);
  11594. ConvertProgram;
  11595. CheckSource('TestAdvRecord_PropertyDefault',
  11596. LinesToStr([ // statements
  11597. 'rtl.recNewT(this, "TPoint", function () {',
  11598. ' this.$eq = function (b) {',
  11599. ' return true;',
  11600. ' };',
  11601. ' this.$assign = function (s) {',
  11602. ' return this;',
  11603. ' };',
  11604. ' this.GetItems = function (Index) {',
  11605. ' var Result = 0;',
  11606. ' this.SetItems(Index, this.GetItems(Index));',
  11607. ' this.SetItems(Index, this.GetItems(Index));',
  11608. ' return Result;',
  11609. ' };',
  11610. ' this.SetItems = function (Index, Value) {',
  11611. ' };',
  11612. '});',
  11613. 'this.p = this.TPoint.$new();',
  11614. '']),
  11615. LinesToStr([ // $mod.$main
  11616. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11617. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11618. '']));
  11619. end;
  11620. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11621. begin
  11622. StartProgram(false);
  11623. Add([
  11624. '{$modeswitch AdvancedRecords}',
  11625. 'type',
  11626. ' TRec = record',
  11627. ' class var',
  11628. ' Fx: longint;',
  11629. ' Fy: longint;',
  11630. ' class function GetInt: longint; static;',
  11631. ' class procedure SetInt(Value: longint); static;',
  11632. ' class procedure DoIt; static;',
  11633. ' class property IntA: longint read Fx write Fy;',
  11634. ' class property IntB: longint read GetInt write SetInt;',
  11635. ' end;',
  11636. 'class function trec.getint: longint;',
  11637. 'begin',
  11638. ' result:=fx;',
  11639. 'end;',
  11640. 'class procedure trec.setint(value: longint);',
  11641. 'begin',
  11642. 'end;',
  11643. 'class procedure trec.doit;',
  11644. 'begin',
  11645. ' IntA:=IntA+1;',
  11646. ' IntB:=IntB+1;',
  11647. 'end;',
  11648. 'var r: trec;',
  11649. 'begin',
  11650. ' trec.inta:=trec.inta+1;',
  11651. ' if trec.intb=2 then;',
  11652. ' trec.intb:=trec.intb+2;',
  11653. ' trec.setint(trec.inta);',
  11654. ' r.inta:=r.inta+1;',
  11655. ' if r.intb=2 then;',
  11656. ' r.intb:=r.intb+2;',
  11657. ' r.setint(r.inta);']);
  11658. ConvertProgram;
  11659. CheckSource('TestAdvRecord_Property_ClassMethod',
  11660. LinesToStr([ // statements
  11661. 'rtl.recNewT(this, "TRec", function () {',
  11662. ' this.Fx = 0;',
  11663. ' this.Fy = 0;',
  11664. ' this.$eq = function (b) {',
  11665. ' return true;',
  11666. ' };',
  11667. ' this.$assign = function (s) {',
  11668. ' return this;',
  11669. ' };',
  11670. ' this.GetInt = function () {',
  11671. ' var Result = 0;',
  11672. ' Result = $mod.TRec.Fx;',
  11673. ' return Result;',
  11674. ' };',
  11675. ' this.SetInt = function (Value) {',
  11676. ' };',
  11677. ' this.DoIt = function () {',
  11678. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11679. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11680. ' };',
  11681. '}, true);',
  11682. 'this.r = this.TRec.$new();',
  11683. '']),
  11684. LinesToStr([ // $mod.$main
  11685. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11686. 'if ($mod.TRec.GetInt() === 2) ;',
  11687. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11688. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11689. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11690. 'if ($mod.TRec.GetInt() === 2) ;',
  11691. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11692. '$mod.TRec.SetInt($mod.r.Fx);',
  11693. '']));
  11694. end;
  11695. procedure TTestModule.TestAdvRecord_Const;
  11696. begin
  11697. StartProgram(false);
  11698. Add([
  11699. '{$modeswitch AdvancedRecords}',
  11700. 'type',
  11701. ' TArrInt = array[3..4] of longint;',
  11702. ' TPoint = record',
  11703. ' x,y: longint;',
  11704. ' class var Count: nativeint;',
  11705. ' end;',
  11706. ' TRec = record',
  11707. ' i: longint;',
  11708. ' a: array of longint;',
  11709. ' s: array[1..2] of longint;',
  11710. ' m: array[1..2,3..4] of longint;',
  11711. ' p: TPoint;',
  11712. ' end;',
  11713. ' TPoints = array of TPoint;',
  11714. 'const',
  11715. ' r: TRec = (',
  11716. ' i:1;',
  11717. ' a:(2,3);',
  11718. ' s:(4,5);',
  11719. ' m:( (11,12), (13,14) );',
  11720. ' p: (x:21)',
  11721. ' );',
  11722. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11723. 'begin']);
  11724. ConvertProgram;
  11725. CheckSource('TestAdvRecord_Const',
  11726. LinesToStr([ // statements
  11727. 'rtl.recNewT(this, "TPoint", function () {',
  11728. ' this.x = 0;',
  11729. ' this.y = 0;',
  11730. ' this.Count = 0;',
  11731. ' this.$eq = function (b) {',
  11732. ' return (this.x === b.x) && (this.y === b.y);',
  11733. ' };',
  11734. ' this.$assign = function (s) {',
  11735. ' this.x = s.x;',
  11736. ' this.y = s.y;',
  11737. ' return this;',
  11738. ' };',
  11739. '}, true);',
  11740. 'rtl.recNewT(this, "TRec", function () {',
  11741. ' this.i = 0;',
  11742. ' this.$new = function () {',
  11743. ' var r = Object.create(this);',
  11744. ' r.a = [];',
  11745. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11746. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11747. ' r.p = $mod.TPoint.$new();',
  11748. ' return r;',
  11749. ' };',
  11750. ' this.$eq = function (b) {',
  11751. ' 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);',
  11752. ' };',
  11753. ' this.$assign = function (s) {',
  11754. ' this.i = s.i;',
  11755. ' this.a = rtl.arrayRef(s.a);',
  11756. ' this.s = s.s.slice(0);',
  11757. ' this.m = s.m.slice(0);',
  11758. ' this.p.$assign(s.p);',
  11759. ' return this;',
  11760. ' };',
  11761. '});',
  11762. 'this.r = this.TRec.$clone({',
  11763. ' i: 1,',
  11764. ' a: [2, 3],',
  11765. ' s: [4, 5],',
  11766. ' m: [[11, 12], [13, 14]],',
  11767. ' p: this.TPoint.$clone({',
  11768. ' x: 21,',
  11769. ' y: 0',
  11770. ' })',
  11771. '});',
  11772. 'this.p = [this.TPoint.$clone({',
  11773. ' x: 1,',
  11774. ' y: 2',
  11775. '}), this.TPoint.$clone({',
  11776. ' x: 3,',
  11777. ' y: 4',
  11778. '})];',
  11779. '']),
  11780. LinesToStr([ // $mod.$main
  11781. '']));
  11782. end;
  11783. procedure TTestModule.TestAdvRecord_ExternalField;
  11784. begin
  11785. StartProgram(false);
  11786. Add([
  11787. '{$modeswitch AdvancedRecords}',
  11788. '{$modeswitch externalclass}',
  11789. 'type',
  11790. ' TCar = record',
  11791. ' public',
  11792. ' Intern: longint external name ''$Intern'';',
  11793. ' Intern2: longint external name ''$Intern2'';',
  11794. ' Bracket: longint external name ''["A B"]'';',
  11795. ' procedure DoIt;',
  11796. ' end;',
  11797. 'procedure tcar.doit;',
  11798. 'begin',
  11799. ' Intern:=Intern+1;',
  11800. ' Intern2:=Intern2+2;',
  11801. ' Bracket:=Bracket+3;',
  11802. 'end;',
  11803. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11804. 'begin',
  11805. ' Rec.intern:=Rec.intern+1;',
  11806. ' Rec.intern2:=Rec.intern2+2;',
  11807. ' Rec.Bracket:=Rec.Bracket+3;',
  11808. ' with Rec do begin',
  11809. ' intern:=intern+1;',
  11810. ' intern2:=intern2+2;',
  11811. ' Bracket:=Bracket+3;',
  11812. ' end;']);
  11813. ConvertProgram;
  11814. CheckSource('TestAdvRecord_ExternalField',
  11815. LinesToStr([ // statements
  11816. 'rtl.recNewT(this, "TCar", function () {',
  11817. ' this.$eq = function (b) {',
  11818. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11819. ' };',
  11820. ' this.$assign = function (s) {',
  11821. ' this.$Intern = s.$Intern;',
  11822. ' this.$Intern2 = s.$Intern2;',
  11823. ' this["A B"] = s["A B"];',
  11824. ' return this;',
  11825. ' };',
  11826. ' this.DoIt = function () {',
  11827. ' this.$Intern = this.$Intern + 1;',
  11828. ' this.$Intern2 = this.$Intern2 + 2;',
  11829. ' this["A B"] = this["A B"] + 3;',
  11830. ' };',
  11831. '});',
  11832. 'this.Rec = this.TCar.$clone({',
  11833. ' $Intern: 11,',
  11834. ' $Intern2: 12,',
  11835. ' "A B": 13',
  11836. '});',
  11837. '']),
  11838. LinesToStr([ // $mod.$main
  11839. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11840. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11841. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11842. 'var $with = $mod.Rec;',
  11843. '$with.$Intern = $with.$Intern + 1;',
  11844. '$with.$Intern2 = $with.$Intern2 + 2;',
  11845. '$with["A B"] = $with["A B"] + 3;',
  11846. '']));
  11847. end;
  11848. procedure TTestModule.TestAdvRecord_SubRecord;
  11849. begin
  11850. StartProgram(false);
  11851. Add([
  11852. '{$modeswitch AdvancedRecords}',
  11853. 'type',
  11854. ' TRec = record',
  11855. ' type',
  11856. ' TPoint = record',
  11857. ' x,y: longint;',
  11858. ' class var Count: nativeint;',
  11859. ' procedure DoIt;',
  11860. ' class procedure DoThat; static;',
  11861. ' end;',
  11862. ' var',
  11863. ' i: longint;',
  11864. ' p: TPoint;',
  11865. ' procedure DoSome;',
  11866. ' end;',
  11867. 'const',
  11868. ' r: TRec = (',
  11869. ' i:1;',
  11870. ' p: (x:21;y:22)',
  11871. ' );',
  11872. 'procedure TRec.DoSome;',
  11873. 'begin',
  11874. ' p.x:=p.y+1;',
  11875. ' p.Count:=p.Count+2;',
  11876. 'end;',
  11877. 'procedure TRec.TPoint.DoIt;',
  11878. 'begin',
  11879. ' Count:=Count+3;',
  11880. 'end;',
  11881. 'class procedure TRec.TPoint.DoThat;',
  11882. 'begin',
  11883. ' Count:=Count+4;',
  11884. 'end;',
  11885. 'begin']);
  11886. ConvertProgram;
  11887. CheckSource('TestAdvRecord_SubRecord',
  11888. LinesToStr([ // statements
  11889. 'rtl.recNewT(this, "TRec", function () {',
  11890. ' rtl.recNewT(this, "TPoint", function () {',
  11891. ' this.x = 0;',
  11892. ' this.y = 0;',
  11893. ' this.Count = 0;',
  11894. ' this.$eq = function (b) {',
  11895. ' return (this.x === b.x) && (this.y === b.y);',
  11896. ' };',
  11897. ' this.$assign = function (s) {',
  11898. ' this.x = s.x;',
  11899. ' this.y = s.y;',
  11900. ' return this;',
  11901. ' };',
  11902. ' this.DoIt = function () {',
  11903. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11904. ' };',
  11905. ' this.DoThat = function () {',
  11906. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11907. ' };',
  11908. ' }, true);',
  11909. ' this.i = 0;',
  11910. ' this.$new = function () {',
  11911. ' var r = Object.create(this);',
  11912. ' r.p = this.TPoint.$new();',
  11913. ' return r;',
  11914. ' };',
  11915. ' this.$eq = function (b) {',
  11916. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11917. ' };',
  11918. ' this.$assign = function (s) {',
  11919. ' this.i = s.i;',
  11920. ' this.p.$assign(s.p);',
  11921. ' return this;',
  11922. ' };',
  11923. ' this.DoSome = function () {',
  11924. ' this.p.x = this.p.y + 1;',
  11925. ' this.TPoint.Count = this.p.Count + 2;',
  11926. ' };',
  11927. '}, true);',
  11928. 'this.r = this.TRec.$clone({',
  11929. ' i: 1,',
  11930. ' p: this.TRec.TPoint.$clone({',
  11931. ' x: 21,',
  11932. ' y: 22',
  11933. ' })',
  11934. '});',
  11935. '']),
  11936. LinesToStr([ // $mod.$main
  11937. '']));
  11938. end;
  11939. procedure TTestModule.TestAdvRecord_SubClass;
  11940. begin
  11941. StartProgram(false);
  11942. Add([
  11943. '{$modeswitch AdvancedRecords}',
  11944. 'type',
  11945. ' TObject = class end;',
  11946. ' TPoint = record',
  11947. ' type',
  11948. ' TBird = class',
  11949. ' procedure DoIt;',
  11950. ' class procedure Glob;',
  11951. ' end;',
  11952. ' procedure DoIt(b: TBird);',
  11953. ' end;',
  11954. 'procedure TPoint.TBird.DoIt;',
  11955. 'begin',
  11956. ' doit;',
  11957. ' self.doit;',
  11958. ' glob;',
  11959. ' self.glob;',
  11960. 'end;',
  11961. 'class procedure TPoint.TBird.Glob;',
  11962. 'begin',
  11963. ' glob;',
  11964. ' self.glob;',
  11965. 'end;',
  11966. 'procedure TPoint.DoIt(b: TBird);',
  11967. 'begin',
  11968. ' b.doit;',
  11969. ' b.glob;',
  11970. ' TBird.glob;',
  11971. 'end;',
  11972. 'begin',
  11973. '']);
  11974. ConvertProgram;
  11975. CheckSource('TestAdvRecord_SubClass',
  11976. LinesToStr([ // statements
  11977. 'rtl.createClass(this, "TObject", null, function () {',
  11978. ' this.$init = function () {',
  11979. ' };',
  11980. ' this.$final = function () {',
  11981. ' };',
  11982. '});',
  11983. 'rtl.recNewT(this, "TPoint", function () {',
  11984. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11985. ' this.DoIt = function () {',
  11986. ' this.DoIt();',
  11987. ' this.DoIt();',
  11988. ' this.$class.Glob();',
  11989. ' this.$class.Glob();',
  11990. ' };',
  11991. ' this.Glob = function () {',
  11992. ' this.Glob();',
  11993. ' this.Glob();',
  11994. ' };',
  11995. ' }, "TPoint.TBird");',
  11996. ' this.$eq = function (b) {',
  11997. ' return true;',
  11998. ' };',
  11999. ' this.$assign = function (s) {',
  12000. ' return this;',
  12001. ' };',
  12002. ' this.DoIt = function (b) {',
  12003. ' b.DoIt();',
  12004. ' b.$class.Glob();',
  12005. ' this.TBird.Glob();',
  12006. ' };',
  12007. '}, true);',
  12008. '']),
  12009. LinesToStr([ // $mod.$main
  12010. '']));
  12011. end;
  12012. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12013. begin
  12014. StartProgram(false);
  12015. Add([
  12016. '{$modeswitch AdvancedRecords}',
  12017. 'type',
  12018. ' IUnknown = interface end;',
  12019. ' TPoint = record',
  12020. ' type IBird = interface end;',
  12021. ' end;',
  12022. 'begin',
  12023. '']);
  12024. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12025. nNotYetImplemented);
  12026. ParseProgram;
  12027. end;
  12028. procedure TTestModule.TestAdvRecord_Constructor;
  12029. begin
  12030. StartProgram(false);
  12031. Add([
  12032. '{$modeswitch AdvancedRecords}',
  12033. 'type',
  12034. ' TPoint = record',
  12035. ' x,y: longint;',
  12036. ' class procedure Run(w: longint = 13); static;',
  12037. ' constructor Create(ax: longint; ay: longint = -1);',
  12038. ' end;',
  12039. 'class procedure tpoint.run(w: longint);',
  12040. 'begin',
  12041. ' run;',
  12042. ' run();',
  12043. 'end;',
  12044. 'constructor tpoint.create(ax,ay: longint);',
  12045. 'begin',
  12046. ' x:=ax;',
  12047. ' self.y:=ay;',
  12048. ' run;',
  12049. ' run(ax);',
  12050. 'end;',
  12051. 'var r: TPoint;',
  12052. 'begin',
  12053. ' r:=TPoint.Create(1,2);',
  12054. ' with TPoint do r:=Create(1,2);',
  12055. ' r.Create(3);',
  12056. ' r:=r.Create(4);',
  12057. '']);
  12058. ConvertProgram;
  12059. CheckSource('TestAdvRecord_Constructor',
  12060. LinesToStr([ // statements
  12061. 'rtl.recNewT(this, "TPoint", function () {',
  12062. ' this.x = 0;',
  12063. ' this.y = 0;',
  12064. ' this.$eq = function (b) {',
  12065. ' return (this.x === b.x) && (this.y === b.y);',
  12066. ' };',
  12067. ' this.$assign = function (s) {',
  12068. ' this.x = s.x;',
  12069. ' this.y = s.y;',
  12070. ' return this;',
  12071. ' };',
  12072. ' this.Run = function (w) {',
  12073. ' $mod.TPoint.Run(13);',
  12074. ' $mod.TPoint.Run(13);',
  12075. ' };',
  12076. ' this.Create = function (ax, ay) {',
  12077. ' this.x = ax;',
  12078. ' this.y = ay;',
  12079. ' this.Run(13);',
  12080. ' this.Run(ax);',
  12081. ' return this;',
  12082. ' };',
  12083. '});',
  12084. 'this.r = this.TPoint.$new();',
  12085. '']),
  12086. LinesToStr([ // $mod.$main
  12087. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12088. 'var $with = $mod.TPoint;',
  12089. '$mod.r.$assign($with.$new().Create(1, 2));',
  12090. '$mod.r.Create(3, -1);',
  12091. '$mod.r.$assign($mod.r.Create(4, -1));',
  12092. '']));
  12093. end;
  12094. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12095. begin
  12096. StartProgram(false);
  12097. Add([
  12098. '{$modeswitch AdvancedRecords}',
  12099. 'type',
  12100. ' TPoint = record',
  12101. ' class var x: longint;',
  12102. ' class procedure Fly; static;',
  12103. ' class constructor Init;',
  12104. ' end;',
  12105. 'var count: word;',
  12106. 'class procedure Tpoint.Fly;',
  12107. 'begin',
  12108. 'end;',
  12109. 'class constructor tpoint.init;',
  12110. 'begin',
  12111. ' count:=count+1;',
  12112. ' x:=x+3;',
  12113. ' tpoint.x:=tpoint.x+4;',
  12114. ' fly;',
  12115. ' tpoint.fly;',
  12116. 'end;',
  12117. 'var r: TPoint;',
  12118. 'begin',
  12119. ' r.x:=r.x+10;',
  12120. ' r.Fly;',
  12121. ' r.Fly();',
  12122. '']);
  12123. ConvertProgram;
  12124. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12125. LinesToStr([ // statements
  12126. 'rtl.recNewT(this, "TPoint", function () {',
  12127. ' this.x = 0;',
  12128. ' this.$eq = function (b) {',
  12129. ' return true;',
  12130. ' };',
  12131. ' this.$assign = function (s) {',
  12132. ' return this;',
  12133. ' };',
  12134. ' this.Fly = function () {',
  12135. ' };',
  12136. '}, true);',
  12137. 'this.count = 0;',
  12138. 'this.r = this.TPoint.$new();',
  12139. '']),
  12140. LinesToStr([ // $mod.$main
  12141. '(function () {',
  12142. ' $mod.count = $mod.count + 1;',
  12143. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12144. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12145. ' $mod.TPoint.Fly();',
  12146. ' $mod.TPoint.Fly();',
  12147. '})();',
  12148. '$mod.TPoint.x = $mod.r.x + 10;',
  12149. '$mod.TPoint.Fly();',
  12150. '$mod.TPoint.Fly();',
  12151. '']));
  12152. end;
  12153. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12154. begin
  12155. StartUnit(false);
  12156. Add([
  12157. 'interface',
  12158. '{$modeswitch AdvancedRecords}',
  12159. 'type',
  12160. ' TPoint = record',
  12161. ' class var x: longint;',
  12162. ' class procedure Fly; static;',
  12163. ' class constructor Init;',
  12164. ' end;',
  12165. 'implementation',
  12166. 'var count: word;',
  12167. 'class procedure Tpoint.Fly;',
  12168. 'begin',
  12169. 'end;',
  12170. 'class constructor tpoint.init;',
  12171. 'begin',
  12172. ' count:=count+1;',
  12173. ' x:=3;',
  12174. ' tpoint.x:=4;',
  12175. ' fly;',
  12176. ' tpoint.fly;',
  12177. 'end;',
  12178. '']);
  12179. ConvertUnit;
  12180. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12181. LinesToStr([ // statements
  12182. 'var $impl = $mod.$impl;',
  12183. 'rtl.recNewT(this, "TPoint", function () {',
  12184. ' this.x = 0;',
  12185. ' this.$eq = function (b) {',
  12186. ' return true;',
  12187. ' };',
  12188. ' this.$assign = function (s) {',
  12189. ' return this;',
  12190. ' };',
  12191. ' this.Fly = function () {',
  12192. ' };',
  12193. '}, true);',
  12194. '']),
  12195. LinesToStr([ // $mod.$init
  12196. '(function () {',
  12197. ' $impl.count = $impl.count + 1;',
  12198. ' $mod.TPoint.x = 3;',
  12199. ' $mod.TPoint.x = 4;',
  12200. ' $mod.TPoint.Fly();',
  12201. ' $mod.TPoint.Fly();',
  12202. '})();',
  12203. '']),
  12204. LinesToStr([ // $mod.$main
  12205. '$impl.count = 0;',
  12206. '']));
  12207. end;
  12208. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12209. begin
  12210. StartProgram(false);
  12211. Add(['type',
  12212. ' TObject = class',
  12213. ' public',
  12214. ' constructor Create;',
  12215. ' destructor Destroy;',
  12216. ' end;',
  12217. ' TBird = TObject;',
  12218. 'constructor tobject.create;',
  12219. 'begin end;',
  12220. 'destructor tobject.destroy;',
  12221. 'begin end;',
  12222. 'var Obj: tobject;',
  12223. 'begin',
  12224. ' obj:=tobject.create;',
  12225. ' obj:=tobject.create();',
  12226. ' obj:=tbird.create;',
  12227. ' obj:=tbird.create();',
  12228. ' obj:=obj.create();',
  12229. ' obj.destroy;',
  12230. '']);
  12231. ConvertProgram;
  12232. CheckSource('TestClass_TObjectDefaultConstructor',
  12233. LinesToStr([ // statements
  12234. 'rtl.createClass(this,"TObject",null,function(){',
  12235. ' this.$init = function () {',
  12236. ' };',
  12237. ' this.$final = function () {',
  12238. ' };',
  12239. ' this.Create = function(){',
  12240. ' return this;',
  12241. ' };',
  12242. ' this.Destroy = function(){',
  12243. ' };',
  12244. '});',
  12245. 'this.Obj = null;'
  12246. ]),
  12247. LinesToStr([ // $mod.$main
  12248. '$mod.Obj = $mod.TObject.$create("Create");',
  12249. '$mod.Obj = $mod.TObject.$create("Create");',
  12250. '$mod.Obj = $mod.TObject.$create("Create");',
  12251. '$mod.Obj = $mod.TObject.$create("Create");',
  12252. '$mod.Obj = $mod.Obj.Create();',
  12253. '$mod.Obj.$destroy("Destroy");',
  12254. '']));
  12255. end;
  12256. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12257. begin
  12258. StartProgram(false);
  12259. Add('type');
  12260. Add(' TObject = class');
  12261. Add(' public');
  12262. Add(' constructor Create(Par: longint);');
  12263. Add(' end;');
  12264. Add('constructor tobject.create(par: longint);');
  12265. Add('begin end;');
  12266. Add('var Obj: tobject;');
  12267. Add('begin');
  12268. Add(' obj:=tobject.create(3);');
  12269. ConvertProgram;
  12270. CheckSource('TestClass_TObjectConstructorWithParams',
  12271. LinesToStr([ // statements
  12272. 'rtl.createClass(this,"TObject",null,function(){',
  12273. ' this.$init = function () {',
  12274. ' };',
  12275. ' this.$final = function () {',
  12276. ' };',
  12277. ' this.Create = function(Par){',
  12278. ' return this;',
  12279. ' };',
  12280. '});',
  12281. 'this.Obj = null;'
  12282. ]),
  12283. LinesToStr([ // $mod.$main
  12284. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12285. ]));
  12286. end;
  12287. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12288. begin
  12289. StartProgram(false);
  12290. Add('type');
  12291. Add(' TObject = class');
  12292. Add(' public');
  12293. Add(' constructor Create;');
  12294. Add(' end;');
  12295. Add(' TTest = class(TObject)');
  12296. Add(' public');
  12297. Add(' constructor Create(const Par: longint = 1);');
  12298. Add(' end;');
  12299. Add('constructor tobject.create;');
  12300. Add('begin end;');
  12301. Add('constructor ttest.create(const par: longint);');
  12302. Add('begin end;');
  12303. Add('var t: ttest;');
  12304. Add('begin');
  12305. Add(' t:=ttest.create;');
  12306. Add(' t:=ttest.create(2);');
  12307. ConvertProgram;
  12308. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12309. LinesToStr([ // statements
  12310. 'rtl.createClass(this,"TObject",null,function(){',
  12311. ' this.$init = function () {',
  12312. ' };',
  12313. ' this.$final = function () {',
  12314. ' };',
  12315. ' this.Create = function(){',
  12316. ' return this;',
  12317. ' };',
  12318. '});',
  12319. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12320. ' this.Create$1 = function (Par) {',
  12321. ' return this;',
  12322. ' };',
  12323. '});',
  12324. 'this.t = null;'
  12325. ]),
  12326. LinesToStr([ // $mod.$main
  12327. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12328. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12329. ]));
  12330. end;
  12331. procedure TTestModule.TestClass_Var;
  12332. begin
  12333. StartProgram(false);
  12334. Add([
  12335. 'type',
  12336. ' TObject = class',
  12337. ' public',
  12338. ' vI: longint;',
  12339. ' constructor Create(Par: longint);',
  12340. ' end;',
  12341. 'constructor tobject.create(par: longint);',
  12342. 'begin',
  12343. ' vi:=par+3',
  12344. 'end;',
  12345. 'var Obj: tobject;',
  12346. 'begin',
  12347. ' obj:=tobject.create(4);',
  12348. ' obj.vi:=obj.VI+5;']);
  12349. ConvertProgram;
  12350. CheckSource('TestClass_Var',
  12351. LinesToStr([ // statements
  12352. 'rtl.createClass(this,"TObject",null,function(){',
  12353. ' this.$init = function () {',
  12354. ' this.vI = 0;',
  12355. ' };',
  12356. ' this.$final = function () {',
  12357. ' };',
  12358. ' this.Create = function(Par){',
  12359. ' this.vI = Par+3;',
  12360. ' return this;',
  12361. ' };',
  12362. '});',
  12363. 'this.Obj = null;'
  12364. ]),
  12365. LinesToStr([ // $mod.$main
  12366. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12367. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12368. ]));
  12369. end;
  12370. procedure TTestModule.TestClass_Method;
  12371. begin
  12372. StartProgram(false);
  12373. Add('type');
  12374. Add(' TObject = class');
  12375. Add(' public');
  12376. Add(' vI: longint;');
  12377. Add(' Sub: TObject;');
  12378. Add(' constructor Create;');
  12379. Add(' function GetIt(Par: longint): tobject;');
  12380. Add(' end;');
  12381. Add('constructor tobject.create; begin end;');
  12382. Add('function tobject.getit(par: longint): tobject;');
  12383. Add('begin');
  12384. Add(' Self.vi:=par+3;');
  12385. Add(' Result:=self.sub;');
  12386. Add('end;');
  12387. Add('var Obj: tobject;');
  12388. Add('begin');
  12389. Add(' obj:=tobject.create;');
  12390. Add(' obj.getit(4);');
  12391. Add(' obj.sub.sub:=nil;');
  12392. Add(' obj.sub.getit(5);');
  12393. Add(' obj.sub.getit(6).SUB:=nil;');
  12394. Add(' obj.sub.getit(7).GETIT(8);');
  12395. Add(' obj.sub.getit(9).SuB.getit(10);');
  12396. ConvertProgram;
  12397. CheckSource('TestClass_Method',
  12398. LinesToStr([ // statements
  12399. 'rtl.createClass(this,"TObject",null,function(){',
  12400. ' this.$init = function () {',
  12401. ' this.vI = 0;',
  12402. ' this.Sub = null;',
  12403. ' };',
  12404. ' this.$final = function () {',
  12405. ' this.Sub = undefined;',
  12406. ' };',
  12407. ' this.Create = function(){',
  12408. ' return this;',
  12409. ' };',
  12410. ' this.GetIt = function(Par){',
  12411. ' var Result = null;',
  12412. ' this.vI = Par + 3;',
  12413. ' Result = this.Sub;',
  12414. ' return Result;',
  12415. ' };',
  12416. '});',
  12417. 'this.Obj = null;'
  12418. ]),
  12419. LinesToStr([ // $mod.$main
  12420. '$mod.Obj = $mod.TObject.$create("Create");',
  12421. '$mod.Obj.GetIt(4);',
  12422. '$mod.Obj.Sub.Sub=null;',
  12423. '$mod.Obj.Sub.GetIt(5);',
  12424. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12425. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12426. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12427. ]));
  12428. end;
  12429. procedure TTestModule.TestClass_Implementation;
  12430. begin
  12431. StartUnit(false);
  12432. Add([
  12433. 'interface',
  12434. 'type',
  12435. ' TObject = class',
  12436. ' constructor Create;',
  12437. ' end;',
  12438. 'implementation',
  12439. 'type',
  12440. ' TIntClass = class',
  12441. ' constructor Create; reintroduce;',
  12442. ' class procedure DoGlob;',
  12443. ' end;',
  12444. 'constructor tintclass.create;',
  12445. 'begin',
  12446. ' inherited;',
  12447. ' inherited create;',
  12448. ' doglob;',
  12449. 'end;',
  12450. 'class procedure tintclass.doglob;',
  12451. 'begin',
  12452. 'end;',
  12453. 'constructor tobject.create;',
  12454. 'var',
  12455. ' iC: tintclass;',
  12456. 'begin',
  12457. ' ic:=tintclass.create;',
  12458. ' tintclass.doglob;',
  12459. ' ic.doglob;',
  12460. 'end;',
  12461. 'initialization',
  12462. ' tintclass.doglob;',
  12463. '']);
  12464. ConvertUnit;
  12465. CheckSource('TestClass_Implementation',
  12466. LinesToStr([ // statements
  12467. 'var $impl = $mod.$impl;',
  12468. 'rtl.createClass(this, "TObject", null, function () {',
  12469. ' this.$init = function () {',
  12470. ' };',
  12471. ' this.$final = function () {',
  12472. ' };',
  12473. ' this.Create = function () {',
  12474. ' var iC = null;',
  12475. ' iC = $impl.TIntClass.$create("Create$1");',
  12476. ' $impl.TIntClass.DoGlob();',
  12477. ' iC.$class.DoGlob();',
  12478. ' return this;',
  12479. ' };',
  12480. '});',
  12481. '']),
  12482. LinesToStr([ // $mod.$main
  12483. '$impl.TIntClass.DoGlob();',
  12484. '']),
  12485. LinesToStr([
  12486. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12487. ' this.Create$1 = function () {',
  12488. ' $mod.TObject.Create.call(this);',
  12489. ' $mod.TObject.Create.call(this);',
  12490. ' this.$class.DoGlob();',
  12491. ' return this;',
  12492. ' };',
  12493. ' this.DoGlob = function () {',
  12494. ' };',
  12495. '});',
  12496. '']));
  12497. end;
  12498. procedure TTestModule.TestClass_Inheritance;
  12499. begin
  12500. StartProgram(false);
  12501. Add('type');
  12502. Add(' TObject = class');
  12503. Add(' public');
  12504. Add(' constructor Create;');
  12505. Add(' end;');
  12506. Add(' TClassA = class');
  12507. Add(' end;');
  12508. Add(' TClassB = class(TObject)');
  12509. Add(' procedure ProcB;');
  12510. Add(' end;');
  12511. Add('constructor tobject.create; begin end;');
  12512. Add('procedure tclassb.procb; begin end;');
  12513. Add('var');
  12514. Add(' oO: TObject;');
  12515. Add(' oA: TClassA;');
  12516. Add(' oB: TClassB;');
  12517. Add('begin');
  12518. Add(' oO:=tobject.Create;');
  12519. Add(' oA:=tclassa.Create;');
  12520. Add(' ob:=tclassb.Create;');
  12521. Add(' if oo is tclassa then ;');
  12522. Add(' ob:=oo as tclassb;');
  12523. Add(' (oo as tclassb).procb;');
  12524. ConvertProgram;
  12525. CheckSource('TestClass_Inheritance',
  12526. LinesToStr([ // statements
  12527. 'rtl.createClass(this,"TObject",null,function(){',
  12528. ' this.$init = function () {',
  12529. ' };',
  12530. ' this.$final = function () {',
  12531. ' };',
  12532. ' this.Create = function () {',
  12533. ' return this;',
  12534. ' };',
  12535. '});',
  12536. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12537. '});',
  12538. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12539. ' this.ProcB = function () {',
  12540. ' };',
  12541. '});',
  12542. 'this.oO = null;',
  12543. 'this.oA = null;',
  12544. 'this.oB = null;'
  12545. ]),
  12546. LinesToStr([ // $mod.$main
  12547. '$mod.oO = $mod.TObject.$create("Create");',
  12548. '$mod.oA = $mod.TClassA.$create("Create");',
  12549. '$mod.oB = $mod.TClassB.$create("Create");',
  12550. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12551. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12552. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12553. ]));
  12554. end;
  12555. procedure TTestModule.TestClass_TypeAlias;
  12556. begin
  12557. StartProgram(false);
  12558. Add([
  12559. '{$interfaces corba}',
  12560. 'type',
  12561. ' IObject = interface',
  12562. ' end;',
  12563. ' IBird = type IObject;',
  12564. ' TObject = class',
  12565. ' end;',
  12566. ' TBird = type TObject;',
  12567. 'var',
  12568. ' oObj: TObject;',
  12569. ' oBird: TBird;',
  12570. ' IntfObj: IObject;',
  12571. ' IntfBird: IBird;',
  12572. 'begin',
  12573. ' oObj:=oBird;',
  12574. '']);
  12575. ConvertProgram;
  12576. CheckSource('TestClass_TypeAlias',
  12577. LinesToStr([ // statements
  12578. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12579. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12580. 'rtl.createClass(this, "TObject", null, function () {',
  12581. ' this.$init = function () {',
  12582. ' };',
  12583. ' this.$final = function () {',
  12584. ' };',
  12585. '});',
  12586. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12587. '});',
  12588. 'this.oObj = null;',
  12589. 'this.oBird = null;',
  12590. 'this.IntfObj = null;',
  12591. 'this.IntfBird = null;',
  12592. '']),
  12593. LinesToStr([ // $mod.$main
  12594. '$mod.oObj = $mod.oBird;',
  12595. '']));
  12596. end;
  12597. procedure TTestModule.TestClass_AbstractMethod;
  12598. begin
  12599. StartProgram(false);
  12600. Add('type');
  12601. Add(' TObject = class');
  12602. Add(' public');
  12603. Add(' procedure DoIt; virtual; abstract;');
  12604. Add(' end;');
  12605. Add('begin');
  12606. ConvertProgram;
  12607. CheckSource('TestClass_AbstractMethod',
  12608. LinesToStr([ // statements
  12609. 'rtl.createClass(this,"TObject",null,function(){',
  12610. ' this.$init = function () {',
  12611. ' };',
  12612. ' this.$final = function () {',
  12613. ' };',
  12614. '});'
  12615. ]),
  12616. LinesToStr([ // this.$main
  12617. ''
  12618. ]));
  12619. end;
  12620. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12621. begin
  12622. StartProgram(false);
  12623. Add([
  12624. 'type',
  12625. ' TObject = class',
  12626. ' procedure DoAbstract; virtual; abstract;',
  12627. ' procedure DoVirtual; virtual;',
  12628. ' procedure DoIt;',
  12629. ' end;',
  12630. ' TA = class',
  12631. ' procedure doabstract; override;',
  12632. ' procedure dovirtual; override;',
  12633. ' procedure DoSome;',
  12634. ' end;',
  12635. 'procedure tobject.dovirtual;',
  12636. 'begin',
  12637. ' inherited; // call non existing ancestor -> ignore silently',
  12638. 'end;',
  12639. 'procedure tobject.doit;',
  12640. 'begin',
  12641. 'end;',
  12642. 'procedure ta.doabstract;',
  12643. 'begin',
  12644. ' inherited dovirtual; // call TObject.DoVirtual',
  12645. 'end;',
  12646. 'procedure ta.dovirtual;',
  12647. 'begin',
  12648. ' inherited; // call TObject.DoVirtual',
  12649. ' inherited dovirtual; // call TObject.DoVirtual',
  12650. ' inherited dovirtual(); // call TObject.DoVirtual',
  12651. ' doit;',
  12652. ' doit();',
  12653. 'end;',
  12654. 'procedure ta.dosome;',
  12655. 'begin',
  12656. ' inherited; // call non existing ancestor method -> silently ignore',
  12657. 'end;',
  12658. 'begin']);
  12659. ConvertProgram;
  12660. CheckSource('TestClass_CallInherited_ProcNoParams',
  12661. LinesToStr([ // statements
  12662. 'rtl.createClass(this,"TObject",null,function(){',
  12663. ' this.$init = function () {',
  12664. ' };',
  12665. ' this.$final = function () {',
  12666. ' };',
  12667. ' this.DoVirtual = function () {',
  12668. ' };',
  12669. ' this.DoIt = function () {',
  12670. ' };',
  12671. '});',
  12672. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12673. ' this.DoAbstract = function () {',
  12674. ' $mod.TObject.DoVirtual.call(this);',
  12675. ' };',
  12676. ' this.DoVirtual = function () {',
  12677. ' $mod.TObject.DoVirtual.call(this);',
  12678. ' $mod.TObject.DoVirtual.call(this);',
  12679. ' $mod.TObject.DoVirtual.call(this);',
  12680. ' this.DoIt();',
  12681. ' this.DoIt();',
  12682. ' };',
  12683. ' this.DoSome = function () {',
  12684. ' };',
  12685. '});'
  12686. ]),
  12687. LinesToStr([ // this.$main
  12688. ''
  12689. ]));
  12690. end;
  12691. procedure TTestModule.TestClass_CallInherited_WithParams;
  12692. begin
  12693. StartProgram(false);
  12694. Add([
  12695. 'type',
  12696. ' TObject = class',
  12697. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12698. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12699. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12700. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12701. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12702. ' end;',
  12703. ' TClassA = class',
  12704. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12705. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12706. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12707. ' end;',
  12708. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12709. 'begin',
  12710. 'end;',
  12711. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12712. 'begin',
  12713. 'end;',
  12714. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12715. 'begin',
  12716. 'end;',
  12717. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12718. 'begin',
  12719. 'end;',
  12720. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12721. 'begin',
  12722. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12723. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12724. 'end;',
  12725. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12726. 'begin',
  12727. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12728. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12729. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12730. ' doit(pa,pb);',
  12731. ' doit(pa);',
  12732. ' doit2(pa);',
  12733. ' doit2;',
  12734. 'end;',
  12735. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12736. 'begin',
  12737. ' pa:=inherited;',
  12738. 'end;',
  12739. 'begin']);
  12740. ConvertProgram;
  12741. CheckSource('TestClass_CallInherited_WithParams',
  12742. LinesToStr([ // statements
  12743. 'rtl.createClass(this,"TObject",null,function(){',
  12744. ' this.$init = function () {',
  12745. ' };',
  12746. ' this.$final = function () {',
  12747. ' };',
  12748. ' this.DoVirtual = function (pA,pB) {',
  12749. ' };',
  12750. ' this.DoIt = function (pA,pB) {',
  12751. ' };',
  12752. ' this.DoIt2 = function (pA,pB) {',
  12753. ' };',
  12754. ' this.GetIt = function (pA, pB) {',
  12755. ' var Result = 0;',
  12756. ' return Result;',
  12757. ' };',
  12758. '});',
  12759. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12760. ' this.DoAbstract = function (pA,pB) {',
  12761. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12762. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12763. ' };',
  12764. ' this.DoVirtual = function (pA,pB) {',
  12765. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12766. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12767. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12768. ' this.DoIt(pA,pB);',
  12769. ' this.DoIt(pA,0);',
  12770. ' this.DoIt2(pA,2);',
  12771. ' this.DoIt2(1,2);',
  12772. ' };',
  12773. ' this.GetIt$1 = function (pA, pB) {',
  12774. ' var Result = 0;',
  12775. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12776. ' return Result;',
  12777. ' };',
  12778. '});'
  12779. ]),
  12780. LinesToStr([ // this.$main
  12781. ''
  12782. ]));
  12783. end;
  12784. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12785. begin
  12786. StartProgram(false);
  12787. Add('type');
  12788. Add(' TObject = class');
  12789. Add(' constructor Create; virtual;');
  12790. Add(' constructor CreateWithB(b: boolean);');
  12791. Add(' end;');
  12792. Add(' TA = class');
  12793. Add(' constructor Create; override;');
  12794. Add(' constructor CreateWithC(c: char);');
  12795. Add(' procedure DoIt;');
  12796. Add(' class function DoSome: TObject;');
  12797. Add(' end;');
  12798. Add('constructor tobject.create;');
  12799. Add('begin');
  12800. Add(' inherited; // call non existing ancestor -> ignore silently');
  12801. Add('end;');
  12802. Add('constructor tobject.createwithb(b: boolean);');
  12803. Add('begin');
  12804. Add(' inherited; // call non existing ancestor -> ignore silently');
  12805. Add(' create; // normal call');
  12806. Add('end;');
  12807. Add('constructor ta.create;');
  12808. Add('begin');
  12809. Add(' inherited; // normal call TObject.Create');
  12810. Add(' inherited create; // normal call TObject.Create');
  12811. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12812. Add('end;');
  12813. Add('constructor ta.createwithc(c: char);');
  12814. Add('begin');
  12815. Add(' inherited create; // call TObject.Create');
  12816. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12817. Add(' doit;');
  12818. Add(' doit();');
  12819. Add(' dosome;');
  12820. Add('end;');
  12821. Add('procedure ta.doit;');
  12822. Add('begin');
  12823. Add(' create; // normal call');
  12824. Add(' createwithb(false); // normal call');
  12825. Add(' createwithc(''c''); // normal call');
  12826. Add('end;');
  12827. Add('class function ta.dosome: TObject;');
  12828. Add('begin');
  12829. Add(' Result:=create; // constructor');
  12830. Add(' Result:=createwithb(true); // constructor');
  12831. Add(' Result:=createwithc(''c''); // constructor');
  12832. Add('end;');
  12833. Add('begin');
  12834. ConvertProgram;
  12835. CheckSource('TestClass_CallInheritedConstructor',
  12836. LinesToStr([ // statements
  12837. 'rtl.createClass(this,"TObject",null,function(){',
  12838. ' this.$init = function () {',
  12839. ' };',
  12840. ' this.$final = function () {',
  12841. ' };',
  12842. ' this.Create = function () {',
  12843. ' return this;',
  12844. ' };',
  12845. ' this.CreateWithB = function (b) {',
  12846. ' this.Create();',
  12847. ' return this;',
  12848. ' };',
  12849. '});',
  12850. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12851. ' this.Create = function () {',
  12852. ' $mod.TObject.Create.call(this);',
  12853. ' $mod.TObject.Create.call(this);',
  12854. ' $mod.TObject.CreateWithB.call(this, false);',
  12855. ' return this;',
  12856. ' };',
  12857. ' this.CreateWithC = function (c) {',
  12858. ' $mod.TObject.Create.call(this);',
  12859. ' $mod.TObject.CreateWithB.call(this, true);',
  12860. ' this.DoIt();',
  12861. ' this.DoIt();',
  12862. ' this.$class.DoSome();',
  12863. ' return this;',
  12864. ' };',
  12865. ' this.DoIt = function () {',
  12866. ' this.Create();',
  12867. ' this.CreateWithB(false);',
  12868. ' this.CreateWithC("c");',
  12869. ' };',
  12870. ' this.DoSome = function () {',
  12871. ' var Result = null;',
  12872. ' Result = this.$create("Create");',
  12873. ' Result = this.$create("CreateWithB", [true]);',
  12874. ' Result = this.$create("CreateWithC", ["c"]);',
  12875. ' return Result;',
  12876. ' };',
  12877. '});'
  12878. ]),
  12879. LinesToStr([ // this.$main
  12880. ''
  12881. ]));
  12882. end;
  12883. procedure TTestModule.TestClass_ClassVar_Assign;
  12884. begin
  12885. StartProgram(false);
  12886. Add([
  12887. 'type',
  12888. ' TObject = class',
  12889. ' public',
  12890. ' class var vI: longint;',
  12891. ' class var Sub: TObject;',
  12892. ' constructor Create;',
  12893. ' class function GetIt(var Par: longint): tobject;',
  12894. ' end;',
  12895. 'constructor tobject.create;',
  12896. 'begin',
  12897. ' vi:=vi+1;',
  12898. ' Self.vi:=Self.vi+1;',
  12899. ' inc(vi);',
  12900. 'end;',
  12901. 'class function tobject.getit(var par: longint): tobject;',
  12902. 'begin',
  12903. ' vi:=vi+3;',
  12904. ' Self.vi:=Self.vi+4;',
  12905. ' inc(vi);',
  12906. ' Result:=self.sub;',
  12907. ' GetIt(vi);',
  12908. 'end;',
  12909. 'var Obj: tobject;',
  12910. 'begin',
  12911. ' obj:=tobject.create;',
  12912. ' tobject.vi:=3;',
  12913. ' if tobject.vi=4 then ;',
  12914. ' tobject.sub:=nil;',
  12915. ' obj.sub:=nil;',
  12916. ' obj.sub.sub:=nil;']);
  12917. ConvertProgram;
  12918. CheckSource('TestClass_ClassVar_Assign',
  12919. LinesToStr([ // statements
  12920. 'rtl.createClass(this,"TObject",null,function(){',
  12921. ' this.vI = 0;',
  12922. ' this.Sub = null;',
  12923. ' this.$init = function () {',
  12924. ' };',
  12925. ' this.$final = function () {',
  12926. ' };',
  12927. ' this.Create = function(){',
  12928. ' $mod.TObject.vI = this.vI+1;',
  12929. ' $mod.TObject.vI = this.vI+1;',
  12930. ' $mod.TObject.vI += 1;',
  12931. ' return this;',
  12932. ' };',
  12933. ' this.GetIt = function(Par){',
  12934. ' var Result = null;',
  12935. ' $mod.TObject.vI = this.vI + 3;',
  12936. ' $mod.TObject.vI = this.vI + 4;',
  12937. ' $mod.TObject.vI += 1;',
  12938. ' Result = this.Sub;',
  12939. ' this.GetIt({',
  12940. ' p: $mod.TObject,',
  12941. ' get: function () {',
  12942. ' return this.p.vI;',
  12943. ' },',
  12944. ' set: function (v) {',
  12945. ' this.p.vI = v;',
  12946. ' }',
  12947. ' });',
  12948. ' return Result;',
  12949. ' };',
  12950. '});',
  12951. 'this.Obj = null;'
  12952. ]),
  12953. LinesToStr([ // $mod.$main
  12954. '$mod.Obj = $mod.TObject.$create("Create");',
  12955. '$mod.TObject.vI = 3;',
  12956. 'if ($mod.TObject.vI === 4);',
  12957. '$mod.TObject.Sub=null;',
  12958. '$mod.TObject.Sub=null;',
  12959. '$mod.TObject.Sub=null;',
  12960. '']));
  12961. end;
  12962. procedure TTestModule.TestClass_CallClassMethod;
  12963. begin
  12964. StartProgram(false);
  12965. Add('type');
  12966. Add(' TObject = class');
  12967. Add(' public');
  12968. Add(' class var vI: longint;');
  12969. Add(' class var Sub: TObject;');
  12970. Add(' constructor Create;');
  12971. Add(' function GetMore(Par: longint): longint;');
  12972. Add(' class function GetIt(Par: longint): tobject;');
  12973. Add(' end;');
  12974. Add('constructor tobject.create;');
  12975. Add('begin');
  12976. Add(' sub:=getit(3);');
  12977. Add(' vi:=getmore(4);');
  12978. Add(' sub:=Self.getit(5);');
  12979. Add(' vi:=Self.getmore(6);');
  12980. Add('end;');
  12981. Add('function tobject.getmore(par: longint): longint;');
  12982. Add('begin');
  12983. Add(' sub:=getit(11);');
  12984. Add(' vi:=getmore(12);');
  12985. Add(' sub:=self.getit(13);');
  12986. Add(' vi:=self.getmore(14);');
  12987. Add('end;');
  12988. Add('class function tobject.getit(par: longint): tobject;');
  12989. Add('begin');
  12990. Add(' sub:=getit(21);');
  12991. Add(' vi:=sub.getmore(22);');
  12992. Add(' sub:=self.getit(23);');
  12993. Add(' vi:=self.sub.getmore(24);');
  12994. Add('end;');
  12995. Add('var Obj: tobject;');
  12996. Add('begin');
  12997. Add(' obj:=tobject.create;');
  12998. Add(' tobject.getit(5);');
  12999. Add(' obj.getit(6);');
  13000. Add(' obj.sub.getit(7);');
  13001. Add(' obj.sub.getit(8).SUB:=nil;');
  13002. Add(' obj.sub.getit(9).GETIT(10);');
  13003. Add(' obj.sub.getit(11).SuB.getit(12);');
  13004. ConvertProgram;
  13005. CheckSource('TestClass_CallClassMethod',
  13006. LinesToStr([ // statements
  13007. 'rtl.createClass(this,"TObject",null,function(){',
  13008. ' this.vI = 0;',
  13009. ' this.Sub = null;',
  13010. ' this.$init = function () {',
  13011. ' };',
  13012. ' this.$final = function () {',
  13013. ' };',
  13014. ' this.Create = function(){',
  13015. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13016. ' $mod.TObject.vI = this.GetMore(4);',
  13017. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13018. ' $mod.TObject.vI = this.GetMore(6);',
  13019. ' return this;',
  13020. ' };',
  13021. ' this.GetMore = function(Par){',
  13022. ' var Result = 0;',
  13023. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13024. ' $mod.TObject.vI = this.GetMore(12);',
  13025. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13026. ' $mod.TObject.vI = this.GetMore(14);',
  13027. ' return Result;',
  13028. ' };',
  13029. ' this.GetIt = function(Par){',
  13030. ' var Result = null;',
  13031. ' $mod.TObject.Sub = this.GetIt(21);',
  13032. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13033. ' $mod.TObject.Sub = this.GetIt(23);',
  13034. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13035. ' return Result;',
  13036. ' };',
  13037. '});',
  13038. 'this.Obj = null;'
  13039. ]),
  13040. LinesToStr([ // $mod.$main
  13041. '$mod.Obj = $mod.TObject.$create("Create");',
  13042. '$mod.TObject.GetIt(5);',
  13043. '$mod.Obj.$class.GetIt(6);',
  13044. '$mod.Obj.Sub.$class.GetIt(7);',
  13045. '$mod.TObject.Sub=null;',
  13046. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13047. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13048. '']));
  13049. end;
  13050. procedure TTestModule.TestClass_CallClassMethodStatic;
  13051. begin
  13052. StartProgram(false);
  13053. Add([
  13054. 'type',
  13055. ' TObject = class',
  13056. ' public',
  13057. ' class function Fly: tobject; static;',
  13058. ' end;',
  13059. 'class function tobject.Fly: tobject;',
  13060. 'begin',
  13061. ' Result.Fly;',
  13062. ' Result.Fly();',
  13063. ' Fly;',
  13064. ' Fly();',
  13065. ' Fly.Fly;',
  13066. ' Fly.Fly();',
  13067. 'end;',
  13068. 'var Obj: tobject;',
  13069. 'begin',
  13070. ' obj.Fly;',
  13071. ' obj.Fly();',
  13072. ' with obj do begin',
  13073. ' Fly;',
  13074. ' Fly();',
  13075. ' end;',
  13076. '']);
  13077. ConvertProgram;
  13078. CheckSource('TestClass_CallClassMethodStatic',
  13079. LinesToStr([ // statements
  13080. 'rtl.createClass(this, "TObject", null, function () {',
  13081. ' this.$init = function () {',
  13082. ' };',
  13083. ' this.$final = function () {',
  13084. ' };',
  13085. ' this.Fly = function () {',
  13086. ' var Result = null;',
  13087. ' $mod.TObject.Fly();',
  13088. ' $mod.TObject.Fly();',
  13089. ' $mod.TObject.Fly();',
  13090. ' $mod.TObject.Fly();',
  13091. ' $mod.TObject.Fly();',
  13092. ' $mod.TObject.Fly();',
  13093. ' return Result;',
  13094. ' };',
  13095. '});',
  13096. 'this.Obj = null;'
  13097. ]),
  13098. LinesToStr([ // $mod.$main
  13099. '$mod.TObject.Fly();',
  13100. '$mod.TObject.Fly();',
  13101. 'var $with = $mod.Obj;',
  13102. '$with.Fly();',
  13103. '$with.Fly();',
  13104. '']));
  13105. end;
  13106. procedure TTestModule.TestClass_Property;
  13107. begin
  13108. StartProgram(false);
  13109. Add('type');
  13110. Add(' TObject = class');
  13111. Add(' Fx: longint;');
  13112. Add(' Fy: longint;');
  13113. Add(' function GetInt: longint;');
  13114. Add(' procedure SetInt(Value: longint);');
  13115. Add(' procedure DoIt;');
  13116. Add(' property IntA: longint read Fx write Fy;');
  13117. Add(' property IntB: longint read GetInt write SetInt;');
  13118. Add(' end;');
  13119. Add('function tobject.getint: longint;');
  13120. Add('begin');
  13121. Add(' result:=fx;');
  13122. Add('end;');
  13123. Add('procedure tobject.setint(value: longint);');
  13124. Add('begin');
  13125. Add(' if value=fy then exit;');
  13126. Add(' fy:=value;');
  13127. Add('end;');
  13128. Add('procedure tobject.doit;');
  13129. Add('begin');
  13130. Add(' IntA:=IntA+1;');
  13131. Add(' Self.IntA:=Self.IntA+1;');
  13132. Add(' IntB:=IntB+1;');
  13133. Add(' Self.IntB:=Self.IntB+1;');
  13134. Add('end;');
  13135. Add('var Obj: tobject;');
  13136. Add('begin');
  13137. Add(' obj.inta:=obj.inta+1;');
  13138. Add(' if obj.intb=2 then;');
  13139. Add(' obj.intb:=obj.intb+2;');
  13140. Add(' obj.setint(obj.inta);');
  13141. ConvertProgram;
  13142. CheckSource('TestClass_Property',
  13143. LinesToStr([ // statements
  13144. 'rtl.createClass(this, "TObject", null, function () {',
  13145. ' this.$init = function () {',
  13146. ' this.Fx = 0;',
  13147. ' this.Fy = 0;',
  13148. ' };',
  13149. ' this.$final = function () {',
  13150. ' };',
  13151. ' this.GetInt = function () {',
  13152. ' var Result = 0;',
  13153. ' Result = this.Fx;',
  13154. ' return Result;',
  13155. ' };',
  13156. ' this.SetInt = function (Value) {',
  13157. ' if (Value === this.Fy) return;',
  13158. ' this.Fy = Value;',
  13159. ' };',
  13160. ' this.DoIt = function () {',
  13161. ' this.Fy = this.Fx + 1;',
  13162. ' this.Fy = this.Fx + 1;',
  13163. ' this.SetInt(this.GetInt() + 1);',
  13164. ' this.SetInt(this.GetInt() + 1);',
  13165. ' };',
  13166. '});',
  13167. 'this.Obj = null;'
  13168. ]),
  13169. LinesToStr([ // $mod.$main
  13170. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13171. 'if ($mod.Obj.GetInt() === 2);',
  13172. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13173. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13174. ]));
  13175. end;
  13176. procedure TTestModule.TestClass_Property_ClassMethod;
  13177. begin
  13178. StartProgram(false);
  13179. Add([
  13180. 'type',
  13181. ' TObject = class',
  13182. ' class var Fx: longint;',
  13183. ' class var Fy: longint;',
  13184. ' class function GetInt: longint;',
  13185. ' class procedure SetInt(Value: longint);',
  13186. ' end;',
  13187. ' TBird = class',
  13188. ' class procedure DoIt;',
  13189. ' class property IntA: longint read Fx write Fy;',
  13190. ' class property IntB: longint read GetInt write SetInt;',
  13191. ' end;',
  13192. 'class function tobject.getint: longint;',
  13193. 'begin',
  13194. ' result:=fx;',
  13195. 'end;',
  13196. 'class procedure tobject.setint(value: longint);',
  13197. 'begin',
  13198. 'end;',
  13199. 'class procedure tbird.doit;',
  13200. 'begin',
  13201. ' FX:=3;',
  13202. ' IntA:=IntA+1;',
  13203. ' Self.IntA:=Self.IntA+1;',
  13204. ' IntB:=IntB+1;',
  13205. ' Self.IntB:=Self.IntB+1;',
  13206. ' with Self do begin',
  13207. ' FX:=11;',
  13208. ' IntA:=IntA+12;',
  13209. ' IntB:=IntB+13;',
  13210. ' end;',
  13211. 'end;',
  13212. 'var Obj: tbird;',
  13213. 'begin',
  13214. ' tbird.fx:=tbird.fx+1;',
  13215. ' tbird.inta:=tbird.inta+1;',
  13216. ' if tbird.intb=2 then;',
  13217. ' tbird.intb:=tbird.intb+2;',
  13218. ' tbird.setint(tbird.inta);',
  13219. ' obj.inta:=obj.inta+1;',
  13220. ' if obj.intb=2 then;',
  13221. ' obj.intb:=obj.intb+2;',
  13222. ' obj.setint(obj.inta);',
  13223. ' with Tbird do begin',
  13224. ' FX:=FY+1;',
  13225. ' inta:=inta+2;',
  13226. ' intb:=intb+3;',
  13227. ' end;',
  13228. ' with Obj do begin',
  13229. ' FX:=FY+1;',
  13230. ' inta:=inta+2;',
  13231. ' intb:=intb+3;',
  13232. ' end;',
  13233. '']);
  13234. ConvertProgram;
  13235. CheckSource('TestClass_Property_ClassMethod',
  13236. LinesToStr([ // statements
  13237. 'rtl.createClass(this, "TObject", null, function () {',
  13238. ' this.Fx = 0;',
  13239. ' this.Fy = 0;',
  13240. ' this.$init = function () {',
  13241. ' };',
  13242. ' this.$final = function () {',
  13243. ' };',
  13244. ' this.GetInt = function () {',
  13245. ' var Result = 0;',
  13246. ' Result = this.Fx;',
  13247. ' return Result;',
  13248. ' };',
  13249. ' this.SetInt = function (Value) {',
  13250. ' };',
  13251. '});',
  13252. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13253. ' this.DoIt = function () {',
  13254. ' $mod.TObject.Fx = 3;',
  13255. ' $mod.TObject.Fy = this.Fx + 1;',
  13256. ' $mod.TObject.Fy = this.Fx + 1;',
  13257. ' this.SetInt(this.GetInt() + 1);',
  13258. ' this.SetInt(this.GetInt() + 1);',
  13259. ' $mod.TObject.Fx = 11;',
  13260. ' $mod.TObject.Fy = this.Fx + 12;',
  13261. ' this.SetInt(this.GetInt() + 13);',
  13262. ' };',
  13263. '});',
  13264. 'this.Obj = null;'
  13265. ]),
  13266. LinesToStr([ // $mod.$main
  13267. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13268. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13269. 'if ($mod.TBird.GetInt() === 2);',
  13270. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13271. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13272. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13273. 'if ($mod.Obj.$class.GetInt() === 2);',
  13274. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13275. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13276. 'var $with = $mod.TBird;',
  13277. '$mod.TObject.Fx = $with.Fy + 1;',
  13278. '$mod.TObject.Fy = $with.Fx + 2;',
  13279. '$with.SetInt($with.GetInt() + 3);',
  13280. 'var $with1 = $mod.Obj;',
  13281. '$mod.TObject.Fx = $with1.Fy + 1;',
  13282. '$mod.TObject.Fy = $with1.Fx + 2;',
  13283. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13284. '']));
  13285. end;
  13286. procedure TTestModule.TestClass_Property_Indexed;
  13287. begin
  13288. StartProgram(false);
  13289. Add([
  13290. 'type',
  13291. ' TObject = class',
  13292. ' FItems: array of longint;',
  13293. ' function GetItems(Index: longint): longint;',
  13294. ' procedure SetItems(Index: longint; Value: longint);',
  13295. ' procedure DoIt;',
  13296. ' property Items[Index: longint]: longint read getitems write setitems;',
  13297. ' end;',
  13298. 'function tobject.getitems(index: longint): longint;',
  13299. 'begin',
  13300. ' Result:=fitems[index];',
  13301. 'end;',
  13302. 'procedure tobject.setitems(index: longint; value: longint);',
  13303. 'begin',
  13304. ' fitems[index]:=value;',
  13305. 'end;',
  13306. 'procedure tobject.doit;',
  13307. 'begin',
  13308. ' items[1]:=2;',
  13309. ' items[3]:=items[4];',
  13310. ' self.items[5]:=self.items[6];',
  13311. ' items[items[7]]:=items[items[8]];',
  13312. 'end;',
  13313. 'var Obj: tobject;',
  13314. 'begin',
  13315. ' obj.Items[11]:=obj.Items[12];',
  13316. '']);
  13317. ConvertProgram;
  13318. CheckSource('TestClass_Property_Indexed',
  13319. LinesToStr([ // statements
  13320. 'rtl.createClass(this, "TObject", null, function () {',
  13321. ' this.$init = function () {',
  13322. ' this.FItems = [];',
  13323. ' };',
  13324. ' this.$final = function () {',
  13325. ' this.FItems = undefined;',
  13326. ' };',
  13327. ' this.GetItems = function (Index) {',
  13328. ' var Result = 0;',
  13329. ' Result = this.FItems[Index];',
  13330. ' return Result;',
  13331. ' };',
  13332. ' this.SetItems = function (Index, Value) {',
  13333. ' this.FItems[Index] = Value;',
  13334. ' };',
  13335. ' this.DoIt = function () {',
  13336. ' this.SetItems(1, 2);',
  13337. ' this.SetItems(3,this.GetItems(4));',
  13338. ' this.SetItems(5,this.GetItems(6));',
  13339. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13340. ' };',
  13341. '});',
  13342. 'this.Obj = null;'
  13343. ]),
  13344. LinesToStr([ // $mod.$main
  13345. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13346. ]));
  13347. end;
  13348. procedure TTestModule.TestClass_Property_IndexSpec;
  13349. begin
  13350. StartProgram(false);
  13351. Add([
  13352. 'type',
  13353. ' TEnum = (red, blue);',
  13354. ' TObject = class',
  13355. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13356. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13357. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13358. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13359. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13360. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13361. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13362. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13363. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13364. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13365. ' end;',
  13366. 'procedure DoIt(b: boolean); begin end;',
  13367. 'var',
  13368. ' o: TObject;',
  13369. 'begin',
  13370. ' o.B1:=o.B1;',
  13371. ' o.B2:=o.B2;',
  13372. ' o.B3:=o.B3;',
  13373. ' o.I1[''a'']:=o.I1[''b''];',
  13374. ' doit(o.b1);',
  13375. ' doit(o.b2);',
  13376. ' doit(o.i1[''c'']);',
  13377. '']);
  13378. ConvertProgram;
  13379. CheckSource('TestClass_Property_IndexSpec',
  13380. LinesToStr([ // statements
  13381. 'this.TEnum = {',
  13382. ' "0": "red",',
  13383. ' red: 0,',
  13384. ' "1": "blue",',
  13385. ' blue: 1',
  13386. '};',
  13387. 'rtl.createClass(this, "TObject", null, function () {',
  13388. ' this.$init = function () {',
  13389. ' };',
  13390. ' this.$final = function () {',
  13391. ' };',
  13392. '});',
  13393. 'this.DoIt = function (b) {',
  13394. '};',
  13395. 'this.o = null;',
  13396. '']),
  13397. LinesToStr([ // $mod.$main
  13398. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13399. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13400. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13401. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13402. '$mod.DoIt($mod.o.GetIntBool(1));',
  13403. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13404. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13405. '']));
  13406. end;
  13407. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13408. begin
  13409. StartProgram(false);
  13410. Add('type');
  13411. Add(' TArray = array of longint;');
  13412. Add(' TObject = class');
  13413. Add(' FItems: TArray;');
  13414. Add(' function GetItems: tarray;');
  13415. Add(' procedure SetItems(Value: tarray);');
  13416. Add(' property Items: tarray read getitems write setitems;');
  13417. Add(' procedure SetNumbers(const Value: tarray);');
  13418. Add(' property Numbers: tarray write setnumbers;');
  13419. Add(' end;');
  13420. Add('function tobject.getitems: tarray;');
  13421. Add('begin');
  13422. Add(' Result:=fitems;');
  13423. Add('end;');
  13424. Add('procedure tobject.setitems(value: tarray);');
  13425. Add('begin');
  13426. Add(' fitems:=value;');
  13427. Add(' fitems:=nil;');
  13428. Add(' Items:=nil;');
  13429. Add(' Items:=Items;');
  13430. Add(' Items[1]:=2;');
  13431. Add(' fitems[3]:=Items[4];');
  13432. Add(' Items[5]:=Items[6];');
  13433. Add(' Self.Items[7]:=8;');
  13434. Add(' Self.Items[9]:=Self.Items[10];');
  13435. Add(' Items[Items[11]]:=Items[Items[12]];');
  13436. Add('end;');
  13437. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13438. Add('begin;');
  13439. Add(' Numbers:=nil;');
  13440. Add(' Numbers:=Value;');
  13441. Add(' Self.Numbers:=Value;');
  13442. Add('end;');
  13443. Add('var Obj: tobject;');
  13444. Add('begin');
  13445. Add(' obj.items:=nil;');
  13446. Add(' obj.items:=obj.items;');
  13447. Add(' obj.items[11]:=obj.items[12];');
  13448. ConvertProgram;
  13449. CheckSource('TestClass_PropertyOfTypeArray',
  13450. LinesToStr([ // statements
  13451. 'rtl.createClass(this, "TObject", null, function () {',
  13452. ' this.$init = function () {',
  13453. ' this.FItems = [];',
  13454. ' };',
  13455. ' this.$final = function () {',
  13456. ' this.FItems = undefined;',
  13457. ' };',
  13458. ' this.GetItems = function () {',
  13459. ' var Result = [];',
  13460. ' Result = rtl.arrayRef(this.FItems);',
  13461. ' return Result;',
  13462. ' };',
  13463. ' this.SetItems = function (Value) {',
  13464. ' this.FItems = rtl.arrayRef(Value);',
  13465. ' this.FItems = [];',
  13466. ' this.SetItems([]);',
  13467. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13468. ' this.GetItems()[1] = 2;',
  13469. ' this.FItems[3] = this.GetItems()[4];',
  13470. ' this.GetItems()[5] = this.GetItems()[6];',
  13471. ' this.GetItems()[7] = 8;',
  13472. ' this.GetItems()[9] = this.GetItems()[10];',
  13473. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13474. ' };',
  13475. ' this.SetNumbers = function (Value) {',
  13476. ' this.SetNumbers([]);',
  13477. ' this.SetNumbers(Value);',
  13478. ' this.SetNumbers(Value);',
  13479. ' };',
  13480. '});',
  13481. 'this.Obj = null;'
  13482. ]),
  13483. LinesToStr([ // $mod.$main
  13484. '$mod.Obj.SetItems([]);',
  13485. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13486. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13487. ]));
  13488. end;
  13489. procedure TTestModule.TestClass_PropertyDefault;
  13490. begin
  13491. StartProgram(false);
  13492. Add([
  13493. 'type',
  13494. ' TArray = array of longint;',
  13495. ' TObject = class',
  13496. ' end;',
  13497. ' TBird = class',
  13498. ' FItems: TArray;',
  13499. ' function GetItems(Index: longint): longint;',
  13500. ' procedure SetItems(Index, Value: longint);',
  13501. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13502. ' end;',
  13503. 'function TBird.getitems(index: longint): longint;',
  13504. 'begin',
  13505. 'end;',
  13506. 'procedure TBird.setitems(index, value: longint);',
  13507. 'begin',
  13508. ' Self[1]:=2;',
  13509. ' Self[3]:=Self[index];',
  13510. ' Self[index]:=Self[Self[value]];',
  13511. ' Self[Self[4]]:=value;',
  13512. 'end;',
  13513. 'var',
  13514. ' Bird: TBird;',
  13515. ' Obj: TObject;',
  13516. 'begin',
  13517. ' bird[11]:=12;',
  13518. ' bird[13]:=bird[14];',
  13519. ' bird[Bird[15]]:=bird[Bird[15]];',
  13520. ' TBird(obj)[16]:=TBird(obj)[17];',
  13521. ' (obj as tbird)[18]:=19;',
  13522. '']);
  13523. ConvertProgram;
  13524. CheckSource('TestClass_PropertyDefault',
  13525. LinesToStr([ // statements
  13526. 'rtl.createClass(this, "TObject", null, function () {',
  13527. ' this.$init = function () {',
  13528. ' };',
  13529. ' this.$final = function () {',
  13530. ' };',
  13531. '});',
  13532. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13533. ' this.$init = function () {',
  13534. ' $mod.TObject.$init.call(this);',
  13535. ' this.FItems = [];',
  13536. ' };',
  13537. ' this.$final = function () {',
  13538. ' this.FItems = undefined;',
  13539. ' $mod.TObject.$final.call(this);',
  13540. ' };',
  13541. ' this.GetItems = function (Index) {',
  13542. ' var Result = 0;',
  13543. ' return Result;',
  13544. ' };',
  13545. ' this.SetItems = function (Index, Value) {',
  13546. ' this.SetItems(1, 2);',
  13547. ' this.SetItems(3, this.GetItems(Index));',
  13548. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13549. ' this.SetItems(this.GetItems(4), Value);',
  13550. ' };',
  13551. '});',
  13552. 'this.Bird = null;',
  13553. 'this.Obj = null;',
  13554. '']),
  13555. LinesToStr([ // $mod.$main
  13556. '$mod.Bird.SetItems(11, 12);',
  13557. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13558. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13559. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13560. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13561. '']));
  13562. end;
  13563. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13564. begin
  13565. StartProgram(false);
  13566. Add([
  13567. 'type',
  13568. ' TObject = class end;',
  13569. ' TAlphaList = class',
  13570. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13571. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13572. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13573. ' end;',
  13574. ' TBetaList = class',
  13575. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13576. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13577. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13578. ' end;',
  13579. ' TBird = class',
  13580. ' procedure DoIt;',
  13581. ' end;',
  13582. 'procedure TBird.DoIt;',
  13583. 'var',
  13584. ' List: TAlphaList;',
  13585. 'begin',
  13586. ' if TBetaList(List[true])[3]=nil then ;',
  13587. ' TBetaList(List[false])[5]:=nil;',
  13588. 'end;',
  13589. 'var',
  13590. ' List: TAlphaList;',
  13591. 'begin',
  13592. ' if TBetaList(List[true])[3]=nil then ;',
  13593. ' TBetaList(List[false])[5]:=nil;',
  13594. '']);
  13595. ConvertProgram;
  13596. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13597. LinesToStr([ // statements
  13598. 'rtl.createClass(this, "TObject", null, function () {',
  13599. ' this.$init = function () {',
  13600. ' };',
  13601. ' this.$final = function () {',
  13602. ' };',
  13603. '});',
  13604. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13605. '});',
  13606. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13607. '});',
  13608. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13609. ' this.DoIt = function () {',
  13610. ' var List = null;',
  13611. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13612. ' List.GetAlphas(false).SetBetas(5, null);',
  13613. ' };',
  13614. '});',
  13615. 'this.List = null;',
  13616. '']),
  13617. LinesToStr([ // $mod.$main
  13618. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13619. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13620. '']));
  13621. end;
  13622. procedure TTestModule.TestClass_PropertyOverride;
  13623. begin
  13624. StartProgram(false);
  13625. Add('type');
  13626. Add(' integer = longint;');
  13627. Add(' TObject = class');
  13628. Add(' FItem: integer;');
  13629. Add(' function GetItem: integer; external name ''GetItem'';');
  13630. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13631. Add(' property Item: integer read getitem write setitem;');
  13632. Add(' end;');
  13633. Add(' TCar = class');
  13634. Add(' FBag: integer;');
  13635. Add(' function GetBag: integer; external name ''GetBag'';');
  13636. Add(' property Item read getbag;');
  13637. Add(' end;');
  13638. Add('var');
  13639. Add(' Obj: tobject;');
  13640. Add(' Car: tcar;');
  13641. Add('begin');
  13642. Add(' Obj.Item:=Obj.Item;');
  13643. Add(' Car.Item:=Car.Item;');
  13644. ConvertProgram;
  13645. CheckSource('TestClass_PropertyOverride',
  13646. LinesToStr([ // statements
  13647. 'rtl.createClass(this, "TObject", null, function () {',
  13648. ' this.$init = function () {',
  13649. ' this.FItem = 0;',
  13650. ' };',
  13651. ' this.$final = function () {',
  13652. ' };',
  13653. '});',
  13654. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13655. ' this.$init = function () {',
  13656. ' $mod.TObject.$init.call(this);',
  13657. ' this.FBag = 0;',
  13658. ' };',
  13659. '});',
  13660. 'this.Obj = null;',
  13661. 'this.Car = null;',
  13662. '']),
  13663. LinesToStr([ // $mod.$main
  13664. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13665. '$mod.Car.SetItem($mod.Car.GetBag());',
  13666. '']));
  13667. end;
  13668. procedure TTestModule.TestClass_PropertyIncVisibility;
  13669. begin
  13670. AddModuleWithIntfImplSrc('unit1.pp',
  13671. LinesToStr([
  13672. 'type',
  13673. ' TNumber = longint;',
  13674. ' TInteger = longint;',
  13675. ' TObject = class',
  13676. ' private',
  13677. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13678. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13679. ' protected',
  13680. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13681. ' end;']),
  13682. LinesToStr([
  13683. '']));
  13684. StartProgram(true);
  13685. Add([
  13686. 'uses unit1;',
  13687. 'type',
  13688. ' TBird = class',
  13689. ' public',
  13690. ' property Items;',
  13691. ' end;',
  13692. 'procedure DoIt(i: TInteger);',
  13693. 'begin',
  13694. 'end;',
  13695. 'var b: TBird;',
  13696. 'begin',
  13697. ' b.Items[1]:=2;',
  13698. ' b.Items[3]:=b.Items[4];',
  13699. ' DoIt(b.Items[5]);',
  13700. '']);
  13701. ConvertProgram;
  13702. CheckSource('TestClass_PropertyIncVisibility',
  13703. LinesToStr([ // statements
  13704. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13705. '});',
  13706. 'this.DoIt = function (i) {',
  13707. '};',
  13708. 'this.b = null;'
  13709. ]),
  13710. LinesToStr([ // $mod.$main
  13711. '$mod.b.SetItems(1, 2);',
  13712. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13713. '$mod.DoIt($mod.b.GetItems(5));'
  13714. ]));
  13715. end;
  13716. procedure TTestModule.TestClass_Assigned;
  13717. begin
  13718. StartProgram(false);
  13719. Add('type');
  13720. Add(' TObject = class');
  13721. Add(' end;');
  13722. Add('var');
  13723. Add(' Obj: tobject;');
  13724. Add(' b: boolean;');
  13725. Add('begin');
  13726. Add(' if Assigned(obj) then ;');
  13727. Add(' b:=Assigned(obj) or false;');
  13728. ConvertProgram;
  13729. CheckSource('TestClass_Assigned',
  13730. LinesToStr([ // statements
  13731. 'rtl.createClass(this, "TObject", null, function () {',
  13732. ' this.$init = function () {',
  13733. ' };',
  13734. ' this.$final = function () {',
  13735. ' };',
  13736. '});',
  13737. 'this.Obj = null;',
  13738. 'this.b = false;'
  13739. ]),
  13740. LinesToStr([ // $mod.$main
  13741. 'if ($mod.Obj != null);',
  13742. '$mod.b = ($mod.Obj != null) || false;'
  13743. ]));
  13744. end;
  13745. procedure TTestModule.TestClass_WithClassDoCreate;
  13746. begin
  13747. StartProgram(false);
  13748. Add('type');
  13749. Add(' TObject = class');
  13750. Add(' aBool: boolean;');
  13751. Add(' Arr: array of boolean;');
  13752. Add(' constructor Create;');
  13753. Add(' end;');
  13754. Add('constructor TObject.Create; begin end;');
  13755. Add('var');
  13756. Add(' Obj: tobject;');
  13757. Add(' b: boolean;');
  13758. Add('begin');
  13759. Add(' with tobject.create do begin');
  13760. Add(' b:=abool;');
  13761. Add(' abool:=b;');
  13762. Add(' b:=arr[1];');
  13763. Add(' arr[2]:=b;');
  13764. Add(' end;');
  13765. Add(' with tobject do');
  13766. Add(' obj:=create;');
  13767. Add(' with obj do begin');
  13768. Add(' create;');
  13769. Add(' b:=abool;');
  13770. Add(' abool:=b;');
  13771. Add(' b:=arr[3];');
  13772. Add(' arr[4]:=b;');
  13773. Add(' end;');
  13774. ConvertProgram;
  13775. CheckSource('TestClass_WithClassDoCreate',
  13776. LinesToStr([ // statements
  13777. 'rtl.createClass(this, "TObject", null, function () {',
  13778. ' this.$init = function () {',
  13779. ' this.aBool = false;',
  13780. ' this.Arr = [];',
  13781. ' };',
  13782. ' this.$final = function () {',
  13783. ' this.Arr = undefined;',
  13784. ' };',
  13785. ' this.Create = function () {',
  13786. ' return this;',
  13787. ' };',
  13788. '});',
  13789. 'this.Obj = null;',
  13790. 'this.b = false;'
  13791. ]),
  13792. LinesToStr([ // $mod.$main
  13793. 'var $with = $mod.TObject.$create("Create");',
  13794. '$mod.b = $with.aBool;',
  13795. '$with.aBool = $mod.b;',
  13796. '$mod.b = $with.Arr[1];',
  13797. '$with.Arr[2] = $mod.b;',
  13798. 'var $with1 = $mod.TObject;',
  13799. '$mod.Obj = $with1.$create("Create");',
  13800. 'var $with2 = $mod.Obj;',
  13801. '$with2.Create();',
  13802. '$mod.b = $with2.aBool;',
  13803. '$with2.aBool = $mod.b;',
  13804. '$mod.b = $with2.Arr[3];',
  13805. '$with2.Arr[4] = $mod.b;',
  13806. '']));
  13807. end;
  13808. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13809. begin
  13810. StartProgram(false);
  13811. Add('type');
  13812. Add(' TObject = class');
  13813. Add(' FInt: longint;');
  13814. Add(' constructor Create;');
  13815. Add(' function GetSize: longint;');
  13816. Add(' procedure SetSize(Value: longint);');
  13817. Add(' property Int: longint read FInt write FInt;');
  13818. Add(' property Size: longint read GetSize write SetSize;');
  13819. Add(' end;');
  13820. Add('constructor TObject.Create; begin end;');
  13821. Add('function TObject.GetSize: longint; begin; end;');
  13822. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13823. Add('var');
  13824. Add(' Obj: tobject;');
  13825. Add(' i: longint;');
  13826. Add('begin');
  13827. Add(' with TObject.Create do begin');
  13828. Add(' i:=int;');
  13829. Add(' int:=i;');
  13830. Add(' i:=size;');
  13831. Add(' size:=i;');
  13832. Add(' end;');
  13833. Add(' with obj do begin');
  13834. Add(' i:=int;');
  13835. Add(' int:=i;');
  13836. Add(' i:=size;');
  13837. Add(' size:=i;');
  13838. Add(' end;');
  13839. ConvertProgram;
  13840. CheckSource('TestClass_WithClassInstDoProperty',
  13841. LinesToStr([ // statements
  13842. 'rtl.createClass(this, "TObject", null, function () {',
  13843. ' this.$init = function () {',
  13844. ' this.FInt = 0;',
  13845. ' };',
  13846. ' this.$final = function () {',
  13847. ' };',
  13848. ' this.Create = function () {',
  13849. ' return this;',
  13850. ' };',
  13851. ' this.GetSize = function () {',
  13852. ' var Result = 0;',
  13853. ' return Result;',
  13854. ' };',
  13855. ' this.SetSize = function (Value) {',
  13856. ' };',
  13857. '});',
  13858. 'this.Obj = null;',
  13859. 'this.i = 0;'
  13860. ]),
  13861. LinesToStr([ // $mod.$main
  13862. 'var $with = $mod.TObject.$create("Create");',
  13863. '$mod.i = $with.FInt;',
  13864. '$with.FInt = $mod.i;',
  13865. '$mod.i = $with.GetSize();',
  13866. '$with.SetSize($mod.i);',
  13867. 'var $with1 = $mod.Obj;',
  13868. '$mod.i = $with1.FInt;',
  13869. '$with1.FInt = $mod.i;',
  13870. '$mod.i = $with1.GetSize();',
  13871. '$with1.SetSize($mod.i);',
  13872. '']));
  13873. end;
  13874. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13875. begin
  13876. StartProgram(false);
  13877. Add('type');
  13878. Add(' TObject = class');
  13879. Add(' constructor Create;');
  13880. Add(' function GetItems(Index: longint): longint;');
  13881. Add(' procedure SetItems(Index, Value: longint);');
  13882. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13883. Add(' end;');
  13884. Add('constructor TObject.Create; begin end;');
  13885. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13886. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13887. Add('var');
  13888. Add(' Obj: tobject;');
  13889. Add(' i: longint;');
  13890. Add('begin');
  13891. Add(' with TObject.Create do begin');
  13892. Add(' i:=Items[1];');
  13893. Add(' Items[2]:=i;');
  13894. Add(' end;');
  13895. Add(' with obj do begin');
  13896. Add(' i:=Items[3];');
  13897. Add(' Items[4]:=i;');
  13898. Add(' end;');
  13899. ConvertProgram;
  13900. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13901. LinesToStr([ // statements
  13902. 'rtl.createClass(this, "TObject", null, function () {',
  13903. ' this.$init = function () {',
  13904. ' };',
  13905. ' this.$final = function () {',
  13906. ' };',
  13907. ' this.Create = function () {',
  13908. ' return this;',
  13909. ' };',
  13910. ' this.GetItems = function (Index) {',
  13911. ' var Result = 0;',
  13912. ' return Result;',
  13913. ' };',
  13914. ' this.SetItems = function (Index, Value) {',
  13915. ' };',
  13916. '});',
  13917. 'this.Obj = null;',
  13918. 'this.i = 0;'
  13919. ]),
  13920. LinesToStr([ // $mod.$main
  13921. 'var $with = $mod.TObject.$create("Create");',
  13922. '$mod.i = $with.GetItems(1);',
  13923. '$with.SetItems(2, $mod.i);',
  13924. 'var $with1 = $mod.Obj;',
  13925. '$mod.i = $with1.GetItems(3);',
  13926. '$with1.SetItems(4, $mod.i);',
  13927. '']));
  13928. end;
  13929. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13930. begin
  13931. StartProgram(false);
  13932. Add('type');
  13933. Add(' TObject = class');
  13934. Add(' constructor Create;');
  13935. Add(' function GetSize: longint;');
  13936. Add(' procedure SetSize(Value: longint);');
  13937. Add(' end;');
  13938. Add('constructor TObject.Create; begin end;');
  13939. Add('function TObject.GetSize: longint; begin; end;');
  13940. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13941. Add('var');
  13942. Add(' Obj: tobject;');
  13943. Add(' i: longint;');
  13944. Add('begin');
  13945. Add(' with TObject.Create do begin');
  13946. Add(' i:=GetSize;');
  13947. Add(' i:=GetSize();');
  13948. Add(' SetSize(i);');
  13949. Add(' end;');
  13950. Add(' with obj do begin');
  13951. Add(' i:=GetSize;');
  13952. Add(' i:=GetSize();');
  13953. Add(' SetSize(i);');
  13954. Add(' end;');
  13955. ConvertProgram;
  13956. CheckSource('TestClass_WithClassInstDoFunc',
  13957. LinesToStr([ // statements
  13958. 'rtl.createClass(this, "TObject", null, function () {',
  13959. ' this.$init = function () {',
  13960. ' };',
  13961. ' this.$final = function () {',
  13962. ' };',
  13963. ' this.Create = function () {',
  13964. ' return this;',
  13965. ' };',
  13966. ' this.GetSize = function () {',
  13967. ' var Result = 0;',
  13968. ' return Result;',
  13969. ' };',
  13970. ' this.SetSize = function (Value) {',
  13971. ' };',
  13972. '});',
  13973. 'this.Obj = null;',
  13974. 'this.i = 0;'
  13975. ]),
  13976. LinesToStr([ // $mod.$main
  13977. 'var $with = $mod.TObject.$create("Create");',
  13978. '$mod.i = $with.GetSize();',
  13979. '$mod.i = $with.GetSize();',
  13980. '$with.SetSize($mod.i);',
  13981. 'var $with1 = $mod.Obj;',
  13982. '$mod.i = $with1.GetSize();',
  13983. '$mod.i = $with1.GetSize();',
  13984. '$with1.SetSize($mod.i);',
  13985. '']));
  13986. end;
  13987. procedure TTestModule.TestClass_TypeCast;
  13988. begin
  13989. StartProgram(false);
  13990. Add('type');
  13991. Add(' TObject = class');
  13992. Add(' Next: TObject;');
  13993. Add(' constructor Create;');
  13994. Add(' end;');
  13995. Add(' TControl = class(TObject)');
  13996. Add(' Arr: array of TObject;');
  13997. Add(' function GetIt(vI: longint = 0): TObject;');
  13998. Add(' end;');
  13999. Add('constructor tobject.create; begin end;');
  14000. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14001. Add('var');
  14002. Add(' Obj: tobject;');
  14003. Add('begin');
  14004. Add(' obj:=tcontrol(obj).next;');
  14005. Add(' tcontrol(obj):=nil;');
  14006. Add(' obj:=tcontrol(obj);');
  14007. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14008. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14009. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14010. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14011. Add(' obj:=tcontrol(nil);');
  14012. ConvertProgram;
  14013. CheckSource('TestClass_TypeCast',
  14014. LinesToStr([ // statements
  14015. 'rtl.createClass(this, "TObject", null, function () {',
  14016. ' this.$init = function () {',
  14017. ' this.Next = null;',
  14018. ' };',
  14019. ' this.$final = function () {',
  14020. ' this.Next = undefined;',
  14021. ' };',
  14022. ' this.Create = function () {',
  14023. ' return this;',
  14024. ' };',
  14025. '});',
  14026. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14027. ' this.$init = function () {',
  14028. ' $mod.TObject.$init.call(this);',
  14029. ' this.Arr = [];',
  14030. ' };',
  14031. ' this.$final = function () {',
  14032. ' this.Arr = undefined;',
  14033. ' $mod.TObject.$final.call(this);',
  14034. ' };',
  14035. ' this.GetIt = function (vI) {',
  14036. ' var Result = null;',
  14037. ' return Result;',
  14038. ' };',
  14039. '});',
  14040. 'this.Obj = null;'
  14041. ]),
  14042. LinesToStr([ // $mod.$main
  14043. '$mod.Obj = $mod.Obj.Next;',
  14044. '$mod.Obj = null;',
  14045. '$mod.Obj = $mod.Obj;',
  14046. '$mod.Obj = $mod.Obj.GetIt(0);',
  14047. '$mod.Obj = $mod.Obj.GetIt(0);',
  14048. '$mod.Obj = $mod.Obj.GetIt(1);',
  14049. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14050. '$mod.Obj = null;',
  14051. '']));
  14052. end;
  14053. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14054. begin
  14055. StartProgram(false);
  14056. Add('type');
  14057. Add(' TObject = class end;');
  14058. Add('procedure ProcA(var A);');
  14059. Add('begin');
  14060. Add(' TObject(A):=nil;');
  14061. Add(' TObject(A):=TObject(A);');
  14062. Add(' if TObject(A)=nil then ;');
  14063. Add(' if nil=TObject(A) then ;');
  14064. Add('end;');
  14065. Add('procedure ProcB(out A);');
  14066. Add('begin');
  14067. Add(' TObject(A):=nil;');
  14068. Add(' TObject(A):=TObject(A);');
  14069. Add(' if TObject(A)=nil then ;');
  14070. Add(' if nil=TObject(A) then ;');
  14071. Add('end;');
  14072. Add('procedure ProcC(const A);');
  14073. Add('begin');
  14074. Add(' if TObject(A)=nil then ;');
  14075. Add(' if nil=TObject(A) then ;');
  14076. Add('end;');
  14077. Add('var o: TObject;');
  14078. Add('begin');
  14079. Add(' ProcA(o);');
  14080. Add(' ProcB(o);');
  14081. Add(' ProcC(o);');
  14082. ConvertProgram;
  14083. CheckSource('TestClass_TypeCastUntypedParam',
  14084. LinesToStr([ // statements
  14085. 'rtl.createClass(this, "TObject", null, function () {',
  14086. ' this.$init = function () {',
  14087. ' };',
  14088. ' this.$final = function () {',
  14089. ' };',
  14090. '});',
  14091. 'this.ProcA = function (A) {',
  14092. ' A.set(null);',
  14093. ' A.set(A.get());',
  14094. ' if (A.get() === null);',
  14095. ' if (null === A.get());',
  14096. '};',
  14097. 'this.ProcB = function (A) {',
  14098. ' A.set(null);',
  14099. ' A.set(A.get());',
  14100. ' if (A.get() === null);',
  14101. ' if (null === A.get());',
  14102. '};',
  14103. 'this.ProcC = function (A) {',
  14104. ' if (A === null);',
  14105. ' if (null === A);',
  14106. '};',
  14107. 'this.o = null;',
  14108. '']),
  14109. LinesToStr([ // $mod.$main
  14110. '$mod.ProcA({',
  14111. ' p: $mod,',
  14112. ' get: function () {',
  14113. ' return this.p.o;',
  14114. ' },',
  14115. ' set: function (v) {',
  14116. ' this.p.o = v;',
  14117. ' }',
  14118. '});',
  14119. '$mod.ProcB({',
  14120. ' p: $mod,',
  14121. ' get: function () {',
  14122. ' return this.p.o;',
  14123. ' },',
  14124. ' set: function (v) {',
  14125. ' this.p.o = v;',
  14126. ' }',
  14127. '});',
  14128. '$mod.ProcC($mod.o);',
  14129. '']));
  14130. end;
  14131. procedure TTestModule.TestClass_Overloads;
  14132. begin
  14133. StartProgram(false);
  14134. Add('type');
  14135. Add(' TObject = class');
  14136. Add(' procedure DoIt;');
  14137. Add(' procedure DoIt(vI: longint);');
  14138. Add(' end;');
  14139. Add('procedure TObject.DoIt;');
  14140. Add('begin');
  14141. Add(' DoIt;');
  14142. Add(' DoIt(1);');
  14143. Add('end;');
  14144. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14145. Add('begin');
  14146. ConvertProgram;
  14147. CheckSource('TestClass_Overloads',
  14148. LinesToStr([ // statements
  14149. 'rtl.createClass(this, "TObject", null, function () {',
  14150. ' this.$init = function () {',
  14151. ' };',
  14152. ' this.$final = function () {',
  14153. ' };',
  14154. ' this.DoIt = function () {',
  14155. ' this.DoIt();',
  14156. ' this.DoIt$1(1);',
  14157. ' };',
  14158. ' this.DoIt$1 = function (vI) {',
  14159. ' };',
  14160. '});',
  14161. '']),
  14162. LinesToStr([ // $mod.$main
  14163. '']));
  14164. end;
  14165. procedure TTestModule.TestClass_OverloadsAncestor;
  14166. begin
  14167. StartProgram(false);
  14168. Add('type');
  14169. Add(' TObject = class;');
  14170. Add(' TObject = class');
  14171. Add(' procedure DoIt(vA: longint);');
  14172. Add(' procedure DoIt(vA, vB: longint);');
  14173. Add(' end;');
  14174. Add(' TCar = class;');
  14175. Add(' TCar = class');
  14176. Add(' procedure DoIt(vA: longint);');
  14177. Add(' procedure DoIt(vA, vB: longint);');
  14178. Add(' end;');
  14179. Add('procedure tobject.doit(va: longint);');
  14180. Add('begin');
  14181. Add(' doit(1);');
  14182. Add(' doit(1,2);');
  14183. Add('end;');
  14184. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14185. Add('procedure tcar.doit(va: longint);');
  14186. Add('begin');
  14187. Add(' doit(1);');
  14188. Add(' doit(1,2);');
  14189. Add(' inherited doit(1);');
  14190. Add(' inherited doit(1,2);');
  14191. Add('end;');
  14192. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14193. Add('begin');
  14194. ConvertProgram;
  14195. CheckSource('TestClass_OverloadsAncestor',
  14196. LinesToStr([ // statements
  14197. 'rtl.createClass(this, "TObject", null, function () {',
  14198. ' this.$init = function () {',
  14199. ' };',
  14200. ' this.$final = function () {',
  14201. ' };',
  14202. ' this.DoIt = function (vA) {',
  14203. ' this.DoIt(1);',
  14204. ' this.DoIt$1(1,2);',
  14205. ' };',
  14206. ' this.DoIt$1 = function (vA, vB) {',
  14207. ' };',
  14208. '});',
  14209. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14210. ' this.DoIt$2 = function (vA) {',
  14211. ' this.DoIt$2(1);',
  14212. ' this.DoIt$3(1, 2);',
  14213. ' $mod.TObject.DoIt.call(this, 1);',
  14214. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14215. ' };',
  14216. ' this.DoIt$3 = function (vA, vB) {',
  14217. ' };',
  14218. '});',
  14219. '']),
  14220. LinesToStr([ // $mod.$main
  14221. '']));
  14222. end;
  14223. procedure TTestModule.TestClass_OverloadConstructor;
  14224. begin
  14225. StartProgram(false);
  14226. Add('type');
  14227. Add(' TObject = class');
  14228. Add(' constructor Create(vA: longint);');
  14229. Add(' constructor Create(vA, vB: longint);');
  14230. Add(' end;');
  14231. Add(' TCar = class');
  14232. Add(' constructor Create(vA: longint);');
  14233. Add(' constructor Create(vA, vB: longint);');
  14234. Add(' end;');
  14235. Add('constructor tobject.create(va: longint);');
  14236. Add('begin');
  14237. Add(' create(1);');
  14238. Add(' create(1,2);');
  14239. Add('end;');
  14240. Add('constructor tobject.create(va, vb: longint); begin end;');
  14241. Add('constructor tcar.create(va: longint);');
  14242. Add('begin');
  14243. Add(' create(1);');
  14244. Add(' create(1,2);');
  14245. Add(' inherited create(1);');
  14246. Add(' inherited create(1,2);');
  14247. Add('end;');
  14248. Add('constructor tcar.create(va, vb: longint); begin end;');
  14249. Add('begin');
  14250. Add(' tobject.create(1);');
  14251. Add(' tobject.create(1,2);');
  14252. Add(' tcar.create(1);');
  14253. Add(' tcar.create(1,2);');
  14254. ConvertProgram;
  14255. CheckSource('TestClass_OverloadConstructor',
  14256. LinesToStr([ // statements
  14257. 'rtl.createClass(this, "TObject", null, function () {',
  14258. ' this.$init = function () {',
  14259. ' };',
  14260. ' this.$final = function () {',
  14261. ' };',
  14262. ' this.Create = function (vA) {',
  14263. ' this.Create(1);',
  14264. ' this.Create$1(1,2);',
  14265. ' return this;',
  14266. ' };',
  14267. ' this.Create$1 = function (vA, vB) {',
  14268. ' return this;',
  14269. ' };',
  14270. '});',
  14271. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14272. ' this.Create$2 = function (vA) {',
  14273. ' this.Create$2(1);',
  14274. ' this.Create$3(1, 2);',
  14275. ' $mod.TObject.Create.call(this, 1);',
  14276. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14277. ' return this;',
  14278. ' };',
  14279. ' this.Create$3 = function (vA, vB) {',
  14280. ' return this;',
  14281. ' };',
  14282. '});',
  14283. '']),
  14284. LinesToStr([ // $mod.$main
  14285. '$mod.TObject.$create("Create", [1]);',
  14286. '$mod.TObject.$create("Create$1", [1, 2]);',
  14287. '$mod.TCar.$create("Create$2", [1]);',
  14288. '$mod.TCar.$create("Create$3", [1, 2]);',
  14289. '']));
  14290. end;
  14291. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14292. begin
  14293. StartProgram(false);
  14294. Add([
  14295. '{$mode delphi}',
  14296. 'type',
  14297. ' TObject = class end;',
  14298. ' TBird = class',
  14299. ' function {#a}GetValue: longint; overload; virtual;',
  14300. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14301. ' end;',
  14302. ' TEagle = class(TBird)',
  14303. ' function {#c}GetValue: longint; overload; override;',
  14304. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14305. ' end;',
  14306. 'function TBird.GetValue: longint;',
  14307. 'begin',
  14308. ' if 3={@a}GetValue then ;',
  14309. ' if 4={@b}GetValue(5) then ;',
  14310. 'end;',
  14311. 'function TBird.GetValue(AValue: longint): longint;',
  14312. 'begin',
  14313. 'end;',
  14314. 'function TEagle.GetValue: longint;',
  14315. 'begin',
  14316. ' if 13={@c}GetValue then ;',
  14317. ' if 14={@d}GetValue(15) then ;',
  14318. ' if 15=inherited {@a}GetValue then ;',
  14319. ' if 16=inherited {@b}GetValue(17) then ;',
  14320. 'end;',
  14321. 'function TEagle.GetValue(AValue: longint): longint;',
  14322. 'begin',
  14323. 'end;',
  14324. 'var',
  14325. ' e: TEagle;',
  14326. 'begin',
  14327. ' if 23=e.{@c}GetValue then ;',
  14328. ' if 24=e.{@d}GetValue(25) then ;']);
  14329. ConvertProgram;
  14330. CheckSource('TestClass_OverloadDelphiOverride',
  14331. LinesToStr([ // statements
  14332. 'rtl.createClass(this, "TObject", null, function () {',
  14333. ' this.$init = function () {',
  14334. ' };',
  14335. ' this.$final = function () {',
  14336. ' };',
  14337. '});',
  14338. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14339. ' this.GetValue = function () {',
  14340. ' var Result = 0;',
  14341. ' if (3 === this.GetValue()) ;',
  14342. ' if (4 === this.GetValue$1(5)) ;',
  14343. ' return Result;',
  14344. ' };',
  14345. ' this.GetValue$1 = function (AValue) {',
  14346. ' var Result = 0;',
  14347. ' return Result;',
  14348. ' };',
  14349. '});',
  14350. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14351. ' this.GetValue = function () {',
  14352. ' var Result = 0;',
  14353. ' if (13 === this.GetValue()) ;',
  14354. ' if (14 === this.GetValue$1(15)) ;',
  14355. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14356. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14357. ' return Result;',
  14358. ' };',
  14359. ' this.GetValue$1 = function (AValue) {',
  14360. ' var Result = 0;',
  14361. ' return Result;',
  14362. ' };',
  14363. '});',
  14364. 'this.e = null;',
  14365. '']),
  14366. LinesToStr([ // $mod.$main
  14367. 'if (23 === $mod.e.GetValue()) ;',
  14368. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14369. '']));
  14370. end;
  14371. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14372. begin
  14373. StartProgram(false);
  14374. Add([
  14375. '{$mode delphi}',
  14376. 'type',
  14377. ' TObject = class end;',
  14378. ' TAnimal = class',
  14379. ' public',
  14380. ' {#animal_a}A: longint;',
  14381. ' function {#animal_b}B: longint;',
  14382. ' end;',
  14383. ' TBird = class(TAnimal)',
  14384. ' public',
  14385. ' {#bird_a}A: double;',
  14386. ' {#bird_b}B: boolean;',
  14387. ' end;',
  14388. ' TEagle = class(TBird)',
  14389. ' public',
  14390. ' function {#eagle_a}A: boolean;',
  14391. ' {#eagle_b}B: double;',
  14392. ' end;',
  14393. 'function TAnimal.B: longint;',
  14394. 'begin',
  14395. 'end;',
  14396. 'function TEagle.A: boolean;',
  14397. 'begin',
  14398. ' {@eagle_b}B:=3.3;',
  14399. ' {@eagle_a}A();',
  14400. ' TBird(Self).{@bird_b}B:=true;',
  14401. ' TAnimal(Self).{@animal_a}A:=17;',
  14402. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14403. 'end;',
  14404. 'var',
  14405. ' e: TEagle;',
  14406. 'begin',
  14407. ' e.{@eagle_b}B:=5.3;',
  14408. ' if e.{@eagle_a}A then ;',
  14409. '']);
  14410. ConvertProgram;
  14411. CheckSource('TestClass_ReintroduceVarDelphi',
  14412. LinesToStr([ // statements
  14413. 'rtl.createClass(this, "TObject", null, function () {',
  14414. ' this.$init = function () {',
  14415. ' };',
  14416. ' this.$final = function () {',
  14417. ' };',
  14418. '});',
  14419. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14420. ' this.$init = function () {',
  14421. ' $mod.TObject.$init.call(this);',
  14422. ' this.A = 0;',
  14423. ' };',
  14424. ' this.B = function () {',
  14425. ' var Result = 0;',
  14426. ' return Result;',
  14427. ' };',
  14428. '});',
  14429. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14430. ' this.$init = function () {',
  14431. ' $mod.TAnimal.$init.call(this);',
  14432. ' this.A$1 = 0.0;',
  14433. ' this.B$1 = false;',
  14434. ' };',
  14435. '});',
  14436. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14437. ' this.$init = function () {',
  14438. ' $mod.TBird.$init.call(this);',
  14439. ' this.B$2 = 0.0;',
  14440. ' };',
  14441. ' this.A$2 = function () {',
  14442. ' var Result = false;',
  14443. ' this.B$2 = 3.3;',
  14444. ' this.A$2();',
  14445. ' this.B$1 = true;',
  14446. ' this.A = 17;',
  14447. ' this.B$1 = this.A$1 > 1;',
  14448. ' return Result;',
  14449. ' };',
  14450. '});',
  14451. 'this.e = null;',
  14452. '']),
  14453. LinesToStr([ // $mod.$main
  14454. '$mod.e.B$2 = 5.3;',
  14455. 'if ($mod.e.A$2()) ;',
  14456. '']));
  14457. end;
  14458. procedure TTestModule.TestClass_ReintroducedVar;
  14459. begin
  14460. StartProgram(false);
  14461. Add('type');
  14462. Add(' TObject = class');
  14463. Add(' strict private');
  14464. Add(' Some: longint;');
  14465. Add(' end;');
  14466. Add(' TMobile = class');
  14467. Add(' strict private');
  14468. Add(' Some: string;');
  14469. Add(' end;');
  14470. Add(' TCar = class(tmobile)');
  14471. Add(' procedure Some;');
  14472. Add(' procedure Some(vA: longint);');
  14473. Add(' end;');
  14474. Add('procedure tcar.some;');
  14475. Add('begin');
  14476. Add(' Some;');
  14477. Add(' Some(1);');
  14478. Add('end;');
  14479. Add('procedure tcar.some(va: longint); begin end;');
  14480. Add('begin');
  14481. ConvertProgram;
  14482. CheckSource('TestClass_ReintroducedVar',
  14483. LinesToStr([ // statements
  14484. 'rtl.createClass(this, "TObject", null, function () {',
  14485. ' this.$init = function () {',
  14486. ' this.Some = 0;',
  14487. ' };',
  14488. ' this.$final = function () {',
  14489. ' };',
  14490. '});',
  14491. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14492. ' this.$init = function () {',
  14493. ' $mod.TObject.$init.call(this);',
  14494. ' this.Some$1 = "";',
  14495. ' };',
  14496. '});',
  14497. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14498. ' this.Some$2 = function () {',
  14499. ' this.Some$2();',
  14500. ' this.Some$3(1);',
  14501. ' };',
  14502. ' this.Some$3 = function (vA) {',
  14503. ' };',
  14504. '});',
  14505. '']),
  14506. LinesToStr([ // $mod.$main
  14507. '']));
  14508. end;
  14509. procedure TTestModule.TestClass_RaiseDescendant;
  14510. begin
  14511. StartProgram(false);
  14512. Add([
  14513. 'type',
  14514. ' TObject = class',
  14515. ' constructor Create(Msg: string);',
  14516. ' end;',
  14517. ' Exception = class',
  14518. ' end;',
  14519. ' EConvertError = class(Exception)',
  14520. ' end;',
  14521. 'constructor TObject.Create(Msg: string); begin end;',
  14522. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14523. 'begin',
  14524. ' raise Exception.Create(''Bar1'');',
  14525. ' raise EConvertError.Create(''Bar2'');',
  14526. ' raise AssertConv(''Bar2'');',
  14527. ' raise AssertConv;',
  14528. '']);
  14529. ConvertProgram;
  14530. CheckSource('TestClass_RaiseDescendant',
  14531. LinesToStr([ // statements
  14532. 'rtl.createClass(this, "TObject", null, function () {',
  14533. ' this.$init = function () {',
  14534. ' };',
  14535. ' this.$final = function () {',
  14536. ' };',
  14537. ' this.Create = function (Msg) {',
  14538. ' return this;',
  14539. ' };',
  14540. '});',
  14541. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14542. '});',
  14543. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14544. '});',
  14545. 'this.AssertConv = function (Msg) {',
  14546. ' var Result = null;',
  14547. ' return Result;',
  14548. '};',
  14549. '']),
  14550. LinesToStr([ // $mod.$main
  14551. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14552. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14553. 'throw $mod.AssertConv("Bar2");',
  14554. 'throw $mod.AssertConv("def");',
  14555. '']));
  14556. end;
  14557. procedure TTestModule.TestClass_ExternalMethod;
  14558. begin
  14559. AddModuleWithIntfImplSrc('unit2.pas',
  14560. LinesToStr([
  14561. 'type',
  14562. ' TObject = class',
  14563. ' public',
  14564. ' procedure Intern; external name ''$DoIntern'';',
  14565. ' end;',
  14566. '']),
  14567. LinesToStr([
  14568. '']));
  14569. StartUnit(true);
  14570. Add('interface');
  14571. Add('uses unit2;');
  14572. Add('type');
  14573. Add(' TCar = class(TObject)');
  14574. Add(' public');
  14575. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14576. Add(' procedure DoIt;');
  14577. Add(' end;');
  14578. Add('implementation');
  14579. Add('procedure tcar.doit;');
  14580. Add('begin');
  14581. Add(' Intern;');
  14582. Add(' Intern();');
  14583. Add(' Intern2;');
  14584. Add(' Intern2();');
  14585. Add('end;');
  14586. Add('var Obj: TCar;');
  14587. Add('begin');
  14588. Add(' obj.intern;');
  14589. Add(' obj.intern();');
  14590. Add(' obj.intern2;');
  14591. Add(' obj.intern2();');
  14592. Add(' obj.doit;');
  14593. Add(' obj.doit();');
  14594. Add(' with obj do begin');
  14595. Add(' Intern;');
  14596. Add(' Intern();');
  14597. Add(' Intern2;');
  14598. Add(' Intern2();');
  14599. Add(' end;');
  14600. ConvertUnit;
  14601. CheckSource('TestClass_ExternalMethod',
  14602. LinesToStr([
  14603. 'var $impl = $mod.$impl;',
  14604. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14605. ' this.DoIt = function () {',
  14606. ' this.$DoIntern();',
  14607. ' this.$DoIntern();',
  14608. ' this.$DoIntern2();',
  14609. ' this.$DoIntern2();',
  14610. ' };',
  14611. ' });',
  14612. '']),
  14613. LinesToStr([ // this.$init
  14614. '$impl.Obj.$DoIntern();',
  14615. '$impl.Obj.$DoIntern();',
  14616. '$impl.Obj.$DoIntern2();',
  14617. '$impl.Obj.$DoIntern2();',
  14618. '$impl.Obj.DoIt();',
  14619. '$impl.Obj.DoIt();',
  14620. 'var $with = $impl.Obj;',
  14621. '$with.$DoIntern();',
  14622. '$with.$DoIntern();',
  14623. '$with.$DoIntern2();',
  14624. '$with.$DoIntern2();',
  14625. '']),
  14626. LinesToStr([ // implementation
  14627. '$impl.Obj = null;',
  14628. '']) );
  14629. end;
  14630. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14631. begin
  14632. StartProgram(false);
  14633. Add('type');
  14634. Add(' TObject = class');
  14635. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14636. Add(' end;');
  14637. Add('begin');
  14638. SetExpectedPasResolverError('Virtual method name must match external',
  14639. nVirtualMethodNameMustMatchExternal);
  14640. ConvertProgram;
  14641. end;
  14642. procedure TTestModule.TestClass_ExternalOverrideFail;
  14643. begin
  14644. StartProgram(false);
  14645. Add('type');
  14646. Add(' TObject = class');
  14647. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14648. Add(' end;');
  14649. Add(' TCar = class');
  14650. Add(' procedure DoIt; override; external name ''DoIt'';');
  14651. Add(' end;');
  14652. Add('begin');
  14653. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14654. nInvalidXModifierY);
  14655. ConvertProgram;
  14656. end;
  14657. procedure TTestModule.TestClass_ExternalVar;
  14658. begin
  14659. AddModuleWithIntfImplSrc('unit2.pas',
  14660. LinesToStr([
  14661. '{$modeswitch externalclass}',
  14662. 'type',
  14663. ' TObject = class',
  14664. ' public',
  14665. ' Intern: longint external name ''$Intern'';',
  14666. ' Bracket: longint external name ''["A B"]'';',
  14667. ' end;',
  14668. '']),
  14669. LinesToStr([
  14670. '']));
  14671. StartUnit(true);
  14672. Add([
  14673. 'interface',
  14674. 'uses unit2;',
  14675. '{$modeswitch externalclass}',
  14676. 'type',
  14677. ' TCar = class(tobject)',
  14678. ' public',
  14679. ' Intern2: longint external name ''$Intern2'';',
  14680. ' procedure DoIt;',
  14681. ' end;',
  14682. 'implementation',
  14683. 'procedure tcar.doit;',
  14684. 'begin',
  14685. ' Intern:=Intern+1;',
  14686. ' Intern2:=Intern2+2;',
  14687. ' Bracket:=Bracket+3;',
  14688. 'end;',
  14689. 'var Obj: TCar;',
  14690. 'begin',
  14691. ' obj.intern:=obj.intern+1;',
  14692. ' obj.intern2:=obj.intern2+2;',
  14693. ' obj.Bracket:=obj.Bracket+3;',
  14694. ' with obj do begin',
  14695. ' intern:=intern+1;',
  14696. ' intern2:=intern2+2;',
  14697. ' Bracket:=Bracket+3;',
  14698. ' end;']);
  14699. ConvertUnit;
  14700. CheckSource('TestClass_ExternalVar',
  14701. LinesToStr([
  14702. 'var $impl = $mod.$impl;',
  14703. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14704. ' this.DoIt = function () {',
  14705. ' this.$Intern = this.$Intern + 1;',
  14706. ' this.$Intern2 = this.$Intern2 + 2;',
  14707. ' this["A B"] = this["A B"] + 3;',
  14708. ' };',
  14709. ' });',
  14710. '']),
  14711. LinesToStr([
  14712. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14713. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14714. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14715. 'var $with = $impl.Obj;',
  14716. '$with.$Intern = $with.$Intern + 1;',
  14717. '$with.$Intern2 = $with.$Intern2 + 2;',
  14718. '$with["A B"] = $with["A B"] + 3;',
  14719. '']),
  14720. LinesToStr([ // implementation
  14721. '$impl.Obj = null;',
  14722. '']));
  14723. end;
  14724. procedure TTestModule.TestClass_Const;
  14725. begin
  14726. StartProgram(false);
  14727. Add([
  14728. 'type',
  14729. ' integer = longint;',
  14730. ' TClass = class of TObject;',
  14731. ' TObject = class',
  14732. ' public',
  14733. ' const cI: integer = 3;',
  14734. ' procedure DoIt;',
  14735. ' class procedure DoMore;',
  14736. ' end;',
  14737. 'procedure tobject.doit;',
  14738. 'begin',
  14739. ' if cI=4 then;',
  14740. ' if 5=cI then;',
  14741. ' if Self.cI=6 then;',
  14742. ' if 7=Self.cI then;',
  14743. ' with Self do begin',
  14744. ' if cI=11 then;',
  14745. ' if 12=cI then;',
  14746. ' end;',
  14747. 'end;',
  14748. 'class procedure tobject.domore;',
  14749. 'begin',
  14750. ' if cI=8 then;',
  14751. ' if Self.cI=9 then;',
  14752. ' if 10=cI then;',
  14753. ' if 11=Self.cI then;',
  14754. ' with Self do begin',
  14755. ' if cI=13 then;',
  14756. ' if 14=cI then;',
  14757. ' end;',
  14758. 'end;',
  14759. 'var',
  14760. ' Obj: TObject;',
  14761. ' Cla: TClass;',
  14762. 'begin',
  14763. ' if TObject.cI=21 then ;',
  14764. ' if Obj.cI=22 then ;',
  14765. ' if Cla.cI=23 then ;',
  14766. ' with obj do if ci=24 then;',
  14767. ' with TObject do if ci=25 then;',
  14768. ' with Cla do if ci=26 then;']);
  14769. ConvertProgram;
  14770. CheckSource('TestClass_Const',
  14771. LinesToStr([
  14772. 'rtl.createClass(this, "TObject", null, function () {',
  14773. ' this.cI = 3;',
  14774. ' this.$init = function () {',
  14775. ' };',
  14776. ' this.$final = function () {',
  14777. ' };',
  14778. ' this.DoIt = function () {',
  14779. ' if (this.cI === 4) ;',
  14780. ' if (5 === this.cI) ;',
  14781. ' if (this.cI === 6) ;',
  14782. ' if (7 === this.cI) ;',
  14783. ' if (this.cI === 11) ;',
  14784. ' if (12 === this.cI) ;',
  14785. ' };',
  14786. ' this.DoMore = function () {',
  14787. ' if (this.cI === 8) ;',
  14788. ' if (this.cI === 9) ;',
  14789. ' if (10 === this.cI) ;',
  14790. ' if (11 === this.cI) ;',
  14791. ' if (this.cI === 13) ;',
  14792. ' if (14 === this.cI) ;',
  14793. ' };',
  14794. '});',
  14795. 'this.Obj = null;',
  14796. 'this.Cla = null;',
  14797. '']),
  14798. LinesToStr([
  14799. 'if ($mod.TObject.cI === 21) ;',
  14800. 'if ($mod.Obj.cI === 22) ;',
  14801. 'if ($mod.Cla.cI === 23) ;',
  14802. 'var $with = $mod.Obj;',
  14803. 'if ($with.cI === 24) ;',
  14804. 'var $with1 = $mod.TObject;',
  14805. 'if ($with1.cI === 25) ;',
  14806. 'var $with2 = $mod.Cla;',
  14807. 'if ($with2.cI === 26) ;',
  14808. '']));
  14809. end;
  14810. procedure TTestModule.TestClass_ConstEnum;
  14811. begin
  14812. StartProgram(false);
  14813. Add([
  14814. 'type',
  14815. ' TEnum = (red,blue);',
  14816. ' TObject = class',
  14817. ' end;',
  14818. ' TAnimal = class',
  14819. ' public',
  14820. ' type TSubEnum = (light,dark);',
  14821. ' const a = high(TEnum);',
  14822. ' const b = high(TSubEnum);',
  14823. ' end;',
  14824. ' TBird = class(TAnimal)',
  14825. ' public',
  14826. ' const c = high(TEnum);',
  14827. ' const d = high(TSubEnum);',
  14828. ' end;',
  14829. ' TAnt = class',
  14830. ' public',
  14831. ' const e = high(TEnum);',
  14832. ' const f = high(TBird.TSubEnum);',
  14833. ' end;',
  14834. 'begin',
  14835. '']);
  14836. ConvertProgram;
  14837. CheckSource('TestClass_ConstEnum',
  14838. LinesToStr([
  14839. 'this.TEnum = {',
  14840. ' "0": "red",',
  14841. ' red: 0,',
  14842. ' "1": "blue",',
  14843. ' blue: 1',
  14844. '};',
  14845. 'rtl.createClass(this, "TObject", null, function () {',
  14846. ' this.$init = function () {',
  14847. ' };',
  14848. ' this.$final = function () {',
  14849. ' };',
  14850. '});',
  14851. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14852. ' this.TSubEnum = {',
  14853. ' "0": "light",',
  14854. ' light: 0,',
  14855. ' "1": "dark",',
  14856. ' dark: 1',
  14857. ' };',
  14858. ' this.a = $mod.TEnum.blue;',
  14859. ' this.b = this.TSubEnum.dark;',
  14860. '});',
  14861. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14862. ' this.c = $mod.TEnum.blue;',
  14863. ' this.d = this.TSubEnum.dark;',
  14864. '});',
  14865. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14866. ' this.e = $mod.TEnum.blue;',
  14867. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14868. '});',
  14869. '']),
  14870. LinesToStr([
  14871. '']));
  14872. end;
  14873. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14874. begin
  14875. StartProgram(false);
  14876. Add([
  14877. 'type',
  14878. ' TObject = class',
  14879. ' const cI: longint = 3;',
  14880. ' procedure Fly;',
  14881. ' procedure Run;',
  14882. ' end;',
  14883. ' TBird = class',
  14884. ' procedure Go;',
  14885. ' end;',
  14886. 'procedure tobject.fly;',
  14887. 'const cI: word = 4;',
  14888. 'begin',
  14889. ' if cI=Self.cI then ;',
  14890. 'end;',
  14891. 'procedure tobject.run;',
  14892. 'const cI: word = 5;',
  14893. 'begin',
  14894. ' if cI=Self.cI then ;',
  14895. 'end;',
  14896. 'procedure tbird.go;',
  14897. 'const cI: word = 6;',
  14898. 'begin',
  14899. ' if cI=Self.cI then ;',
  14900. 'end;',
  14901. 'begin',
  14902. '']);
  14903. ConvertProgram;
  14904. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14905. LinesToStr([
  14906. 'rtl.createClass(this, "TObject", null, function () {',
  14907. ' this.cI = 3;',
  14908. ' this.$init = function () {',
  14909. ' };',
  14910. ' this.$final = function () {',
  14911. ' };',
  14912. ' var cI$1 = 4;',
  14913. ' this.Fly = function () {',
  14914. ' if (cI$1 === this.cI) ;',
  14915. ' };',
  14916. ' var cI$2 = 5;',
  14917. ' this.Run = function () {',
  14918. ' if (cI$2 === this.cI) ;',
  14919. ' };',
  14920. '});',
  14921. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14922. ' var cI$3 = 6;',
  14923. ' this.Go = function () {',
  14924. ' if (cI$3 === this.cI) ;',
  14925. ' };',
  14926. '});',
  14927. '']),
  14928. LinesToStr([
  14929. '']));
  14930. end;
  14931. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14932. begin
  14933. StartUnit(false);
  14934. Add([
  14935. 'interface',
  14936. 'type',
  14937. ' TObject = class',
  14938. ' const cI: longint = 3;',
  14939. ' procedure Fly;',
  14940. ' procedure Run;',
  14941. ' end;',
  14942. ' TBird = class',
  14943. ' procedure Go;',
  14944. ' end;',
  14945. 'implementation',
  14946. 'procedure tobject.fly;',
  14947. 'const cI: word = 4;',
  14948. 'begin',
  14949. ' if cI=Self.cI then ;',
  14950. 'end;',
  14951. 'procedure tobject.run;',
  14952. 'const cI: word = 5;',
  14953. 'begin',
  14954. ' if cI=Self.cI then ;',
  14955. 'end;',
  14956. 'procedure tbird.go;',
  14957. 'const cI: word = 6;',
  14958. 'begin',
  14959. ' if cI=Self.cI then ;',
  14960. 'end;',
  14961. '']);
  14962. ConvertUnit;
  14963. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14964. LinesToStr([
  14965. 'rtl.createClass(this, "TObject", null, function () {',
  14966. ' this.cI = 3;',
  14967. ' this.$init = function () {',
  14968. ' };',
  14969. ' this.$final = function () {',
  14970. ' };',
  14971. ' var cI$1 = 4;',
  14972. ' this.Fly = function () {',
  14973. ' if (cI$1 === this.cI) ;',
  14974. ' };',
  14975. ' var cI$2 = 5;',
  14976. ' this.Run = function () {',
  14977. ' if (cI$2 === this.cI) ;',
  14978. ' };',
  14979. '});',
  14980. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14981. ' var cI$3 = 6;',
  14982. ' this.Go = function () {',
  14983. ' if (cI$3 === this.cI) ;',
  14984. ' };',
  14985. '});',
  14986. '']),
  14987. '',
  14988. '');
  14989. end;
  14990. procedure TTestModule.TestClass_LocalVarSelfFail;
  14991. begin
  14992. StartProgram(false);
  14993. Add([
  14994. 'type',
  14995. ' TObject = class',
  14996. ' constructor Create;',
  14997. ' end;',
  14998. 'constructor tobject.create;',
  14999. 'var self: longint;',
  15000. 'begin',
  15001. 'end',
  15002. 'begin',
  15003. '']);
  15004. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15005. ConvertProgram;
  15006. end;
  15007. procedure TTestModule.TestClass_ArgSelfFail;
  15008. begin
  15009. StartProgram(false);
  15010. Add([
  15011. 'type',
  15012. ' TObject = class',
  15013. ' procedure DoIt(Self: longint);',
  15014. ' end;',
  15015. 'procedure tobject.doit(self: longint);',
  15016. 'begin',
  15017. 'end',
  15018. 'begin',
  15019. '']);
  15020. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15021. ConvertProgram;
  15022. end;
  15023. procedure TTestModule.TestClass_NestedProcSelf;
  15024. begin
  15025. StartProgram(false);
  15026. Add([
  15027. 'type',
  15028. ' TObject = class',
  15029. ' Key: longint;',
  15030. ' class var State: longint;',
  15031. ' procedure DoIt;',
  15032. ' function GetSize: longint; virtual; abstract;',
  15033. ' procedure SetSize(Value: longint); virtual; abstract;',
  15034. ' property Size: longint read GetSize write SetSize;',
  15035. ' end;',
  15036. 'procedure tobject.doit;',
  15037. ' procedure Sub;',
  15038. ' begin',
  15039. ' key:=key+2;',
  15040. ' self.key:=self.key+3;',
  15041. ' state:=state+4;',
  15042. ' self.state:=self.state+5;',
  15043. ' tobject.state:=tobject.state+6;',
  15044. ' size:=size+7;',
  15045. ' self.size:=self.size+8;',
  15046. ' end;',
  15047. 'begin',
  15048. ' sub;',
  15049. ' key:=key+12;',
  15050. ' self.key:=self.key+13;',
  15051. ' state:=state+14;',
  15052. ' self.state:=self.state+15;',
  15053. ' tobject.state:=tobject.state+16;',
  15054. ' size:=size+17;',
  15055. ' self.size:=self.size+18;',
  15056. 'end;',
  15057. 'begin',
  15058. '']);
  15059. ConvertProgram;
  15060. CheckSource('TestClass_NestedProcSelf',
  15061. LinesToStr([ // statements
  15062. 'rtl.createClass(this, "TObject", null, function () {',
  15063. ' this.State = 0;',
  15064. ' this.$init = function () {',
  15065. ' this.Key = 0;',
  15066. ' };',
  15067. ' this.$final = function () {',
  15068. ' };',
  15069. ' this.DoIt = function () {',
  15070. ' var $Self = this;',
  15071. ' function Sub() {',
  15072. ' $Self.Key = $Self.Key + 2;',
  15073. ' $Self.Key = $Self.Key + 3;',
  15074. ' $mod.TObject.State = $Self.State + 4;',
  15075. ' $mod.TObject.State = $Self.State + 5;',
  15076. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15077. ' $Self.SetSize($Self.GetSize() + 7);',
  15078. ' $Self.SetSize($Self.GetSize() + 8);',
  15079. ' };',
  15080. ' Sub();',
  15081. ' this.Key = this.Key + 12;',
  15082. ' $Self.Key = $Self.Key + 13;',
  15083. ' $mod.TObject.State = this.State + 14;',
  15084. ' $mod.TObject.State = $Self.State + 15;',
  15085. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15086. ' this.SetSize(this.GetSize() + 17);',
  15087. ' $Self.SetSize($Self.GetSize() + 18);',
  15088. ' };',
  15089. '});',
  15090. '']),
  15091. LinesToStr([ // $mod.$main
  15092. '']));
  15093. end;
  15094. procedure TTestModule.TestClass_NestedProcSelf2;
  15095. begin
  15096. StartProgram(false);
  15097. Add([
  15098. 'type',
  15099. ' TObject = class',
  15100. ' Key: longint;',
  15101. ' class var State: longint;',
  15102. ' function GetSize: longint; virtual; abstract;',
  15103. ' procedure SetSize(Value: longint); virtual; abstract;',
  15104. ' property Size: longint read GetSize write SetSize;',
  15105. ' end;',
  15106. ' TBird = class',
  15107. ' procedure DoIt;',
  15108. ' end;',
  15109. 'procedure tbird.doit;',
  15110. ' procedure Sub;',
  15111. ' begin',
  15112. ' key:=key+2;',
  15113. ' self.key:=self.key+3;',
  15114. ' state:=state+4;',
  15115. ' self.state:=self.state+5;',
  15116. ' tobject.state:=tobject.state+6;',
  15117. ' size:=size+7;',
  15118. ' self.size:=self.size+8;',
  15119. ' end;',
  15120. 'begin',
  15121. ' sub;',
  15122. ' key:=key+12;',
  15123. ' self.key:=self.key+13;',
  15124. ' state:=state+14;',
  15125. ' self.state:=self.state+15;',
  15126. ' tobject.state:=tobject.state+16;',
  15127. ' size:=size+17;',
  15128. ' self.size:=self.size+18;',
  15129. 'end;',
  15130. 'begin',
  15131. '']);
  15132. ConvertProgram;
  15133. CheckSource('TestClass_NestedProcSelf2',
  15134. LinesToStr([ // statements
  15135. 'rtl.createClass(this, "TObject", null, function () {',
  15136. ' this.State = 0;',
  15137. ' this.$init = function () {',
  15138. ' this.Key = 0;',
  15139. ' };',
  15140. ' this.$final = function () {',
  15141. ' };',
  15142. '});',
  15143. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15144. ' this.DoIt = function () {',
  15145. ' var $Self = this;',
  15146. ' function Sub() {',
  15147. ' $Self.Key = $Self.Key + 2;',
  15148. ' $Self.Key = $Self.Key + 3;',
  15149. ' $mod.TObject.State = $Self.State + 4;',
  15150. ' $mod.TObject.State = $Self.State + 5;',
  15151. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15152. ' $Self.SetSize($Self.GetSize() + 7);',
  15153. ' $Self.SetSize($Self.GetSize() + 8);',
  15154. ' };',
  15155. ' Sub();',
  15156. ' this.Key = this.Key + 12;',
  15157. ' $Self.Key = $Self.Key + 13;',
  15158. ' $mod.TObject.State = this.State + 14;',
  15159. ' $mod.TObject.State = $Self.State + 15;',
  15160. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15161. ' this.SetSize(this.GetSize() + 17);',
  15162. ' $Self.SetSize($Self.GetSize() + 18);',
  15163. ' };',
  15164. '});',
  15165. '']),
  15166. LinesToStr([ // $mod.$main
  15167. '']));
  15168. end;
  15169. procedure TTestModule.TestClass_NestedProcClassSelf;
  15170. begin
  15171. StartProgram(false);
  15172. Add([
  15173. 'type',
  15174. ' TObject = class',
  15175. ' class var State: longint;',
  15176. ' class procedure DoIt;',
  15177. ' class function GetSize: longint; virtual; abstract;',
  15178. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15179. ' class property Size: longint read GetSize write SetSize;',
  15180. ' end;',
  15181. 'class procedure tobject.doit;',
  15182. ' procedure Sub;',
  15183. ' begin',
  15184. ' state:=state+2;',
  15185. ' self.state:=self.state+3;',
  15186. ' tobject.state:=tobject.state+4;',
  15187. ' size:=size+5;',
  15188. ' self.size:=self.size+6;',
  15189. ' tobject.size:=tobject.size+7;',
  15190. ' end;',
  15191. 'begin',
  15192. ' sub;',
  15193. ' state:=state+12;',
  15194. ' self.state:=self.state+13;',
  15195. ' tobject.state:=tobject.state+14;',
  15196. ' size:=size+15;',
  15197. ' self.size:=self.size+16;',
  15198. ' tobject.size:=tobject.size+17;',
  15199. 'end;',
  15200. 'begin',
  15201. '']);
  15202. ConvertProgram;
  15203. CheckSource('TestClass_NestedProcClassSelf',
  15204. LinesToStr([ // statements
  15205. 'rtl.createClass(this, "TObject", null, function () {',
  15206. ' this.State = 0;',
  15207. ' this.$init = function () {',
  15208. ' };',
  15209. ' this.$final = function () {',
  15210. ' };',
  15211. ' this.DoIt = function () {',
  15212. ' var $Self = this;',
  15213. ' function Sub() {',
  15214. ' $mod.TObject.State = $Self.State + 2;',
  15215. ' $mod.TObject.State = $Self.State + 3;',
  15216. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15217. ' $Self.SetSize($Self.GetSize() + 5);',
  15218. ' $Self.SetSize($Self.GetSize() + 6);',
  15219. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15220. ' };',
  15221. ' Sub();',
  15222. ' $mod.TObject.State = this.State + 12;',
  15223. ' $mod.TObject.State = $Self.State + 13;',
  15224. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15225. ' this.SetSize(this.GetSize() + 15);',
  15226. ' $Self.SetSize($Self.GetSize() + 16);',
  15227. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15228. ' };',
  15229. '});',
  15230. '']),
  15231. LinesToStr([ // $mod.$main
  15232. '']));
  15233. end;
  15234. procedure TTestModule.TestClass_NestedProcCallInherited;
  15235. begin
  15236. StartProgram(false);
  15237. Add([
  15238. 'type',
  15239. ' TObject = class',
  15240. ' function DoIt(k: boolean): longint; virtual;',
  15241. ' end;',
  15242. ' TBird = class',
  15243. ' function DoIt(k: boolean): longint; override;',
  15244. ' end;',
  15245. 'function tobject.doit(k: boolean): longint;',
  15246. 'begin',
  15247. 'end;',
  15248. 'function tbird.doit(k: boolean): longint;',
  15249. ' procedure Sub;',
  15250. ' begin',
  15251. ' inherited DoIt(true);',
  15252. //' if inherited DoIt(false)=4 then ;',
  15253. ' end;',
  15254. 'begin',
  15255. ' Sub;',
  15256. ' inherited;',
  15257. ' inherited DoIt(true);',
  15258. //' if inherited DoIt(false)=14 then ;',
  15259. 'end;',
  15260. 'begin',
  15261. '']);
  15262. ConvertProgram;
  15263. CheckSource('TestClass_NestedProcCallInherited',
  15264. LinesToStr([ // statements
  15265. 'rtl.createClass(this, "TObject", null, function () {',
  15266. ' this.$init = function () {',
  15267. ' };',
  15268. ' this.$final = function () {',
  15269. ' };',
  15270. ' this.DoIt = function (k) {',
  15271. ' var Result = 0;',
  15272. ' return Result;',
  15273. ' };',
  15274. '});',
  15275. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15276. ' this.DoIt = function (k) {',
  15277. ' var $Self = this;',
  15278. ' var Result = 0;',
  15279. ' function Sub() {',
  15280. ' $mod.TObject.DoIt.call($Self, true);',
  15281. ' };',
  15282. ' Sub();',
  15283. ' $mod.TObject.DoIt.apply(this, arguments);',
  15284. ' $mod.TObject.DoIt.call(this, true);',
  15285. ' return Result;',
  15286. ' };',
  15287. '});',
  15288. '']),
  15289. LinesToStr([ // $mod.$main
  15290. '']));
  15291. end;
  15292. procedure TTestModule.TestClass_TObjectFree;
  15293. begin
  15294. StartProgram(false);
  15295. Add([
  15296. 'type',
  15297. ' TObject = class',
  15298. ' Obj: tobject;',
  15299. ' procedure Free;',
  15300. ' procedure Release;',
  15301. ' end;',
  15302. 'procedure tobject.free;',
  15303. 'begin',
  15304. 'end;',
  15305. 'procedure tobject.release;',
  15306. 'begin',
  15307. ' free;',
  15308. ' if true then free;',
  15309. 'end;',
  15310. 'function DoIt(o: tobject): tobject;',
  15311. 'var l: tobject;',
  15312. 'begin',
  15313. ' o.free;',
  15314. ' o.free();',
  15315. ' l.free;',
  15316. ' l.free();',
  15317. ' o.obj.free;',
  15318. ' o.obj.free();',
  15319. ' with o do obj.free;',
  15320. ' with o do obj.free();',
  15321. ' result.Free;',
  15322. ' result.Free();',
  15323. 'end;',
  15324. 'var o: tobject;',
  15325. ' a: array of tobject;',
  15326. 'begin',
  15327. ' o.free;',
  15328. ' o.obj.free;',
  15329. ' a[1+2].free;',
  15330. '']);
  15331. ConvertProgram;
  15332. CheckSource('TestClass_TObjectFree',
  15333. LinesToStr([ // statements
  15334. 'rtl.createClass(this, "TObject", null, function () {',
  15335. ' this.$init = function () {',
  15336. ' this.Obj = null;',
  15337. ' };',
  15338. ' this.$final = function () {',
  15339. ' this.Obj = undefined;',
  15340. ' };',
  15341. ' this.Free = function () {',
  15342. ' };',
  15343. ' this.Release = function () {',
  15344. ' this.Free();',
  15345. ' if (true) this.Free();',
  15346. ' };',
  15347. '});',
  15348. 'this.DoIt = function (o) {',
  15349. ' var Result = null;',
  15350. ' var l = null;',
  15351. ' o = rtl.freeLoc(o);',
  15352. ' o = rtl.freeLoc(o);',
  15353. ' l = rtl.freeLoc(l);',
  15354. ' l = rtl.freeLoc(l);',
  15355. ' rtl.free(o, "Obj");',
  15356. ' rtl.free(o, "Obj");',
  15357. ' rtl.free(o, "Obj");',
  15358. ' rtl.free(o, "Obj");',
  15359. ' Result = rtl.freeLoc(Result);',
  15360. ' Result = rtl.freeLoc(Result);',
  15361. ' return Result;',
  15362. '};',
  15363. 'this.o = null;',
  15364. 'this.a = [];',
  15365. '']),
  15366. LinesToStr([ // $mod.$main
  15367. 'rtl.free($mod, "o");',
  15368. 'rtl.free($mod.o, "Obj");',
  15369. 'rtl.free($mod.a, 1 + 2);',
  15370. '']));
  15371. end;
  15372. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15373. begin
  15374. StartProgram(false);
  15375. Add([
  15376. 'type',
  15377. ' TObject = class',
  15378. ' Obj: tobject;',
  15379. ' procedure Free;',
  15380. ' end;',
  15381. 'procedure tobject.free;',
  15382. 'begin',
  15383. 'end;',
  15384. 'procedure DoIt(var o: tobject);',
  15385. 'begin',
  15386. ' o.free;',
  15387. ' o.free();',
  15388. 'end;',
  15389. 'begin',
  15390. '']);
  15391. ConvertProgram;
  15392. CheckSource('TestClass_TObjectFree_VarArg',
  15393. LinesToStr([ // statements
  15394. 'rtl.createClass(this, "TObject", null, function () {',
  15395. ' this.$init = function () {',
  15396. ' this.Obj = null;',
  15397. ' };',
  15398. ' this.$final = function () {',
  15399. ' this.Obj = undefined;',
  15400. ' };',
  15401. ' this.Free = function () {',
  15402. ' };',
  15403. '});',
  15404. 'this.DoIt = function (o) {',
  15405. ' o.set(rtl.freeLoc(o.get()));',
  15406. ' o.set(rtl.freeLoc(o.get()));',
  15407. '};',
  15408. '']),
  15409. LinesToStr([ // $mod.$main
  15410. '']));
  15411. end;
  15412. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15413. begin
  15414. StartProgram(false);
  15415. Add([
  15416. 'type',
  15417. ' TObject = class',
  15418. ' constructor Create;',
  15419. ' procedure Free;',
  15420. ' end;',
  15421. 'constructor TObject.Create; begin end;',
  15422. 'procedure tobject.free; begin end;',
  15423. 'begin',
  15424. ' with tobject.create do free;',
  15425. '']);
  15426. ConvertProgram;
  15427. CheckSource('TestClass_TObjectFreeNewInstance',
  15428. LinesToStr([ // statements
  15429. 'rtl.createClass(this, "TObject", null, function () {',
  15430. ' this.$init = function () {',
  15431. ' };',
  15432. ' this.$final = function () {',
  15433. ' };',
  15434. ' this.Create = function () {',
  15435. ' return this;',
  15436. ' };',
  15437. ' this.Free = function () {',
  15438. ' };',
  15439. '});',
  15440. '']),
  15441. LinesToStr([ // $mod.$main
  15442. 'var $with = $mod.TObject.$create("Create");',
  15443. '$with=rtl.freeLoc($with);',
  15444. '']));
  15445. end;
  15446. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15447. begin
  15448. StartProgram(false);
  15449. Add([
  15450. 'type',
  15451. ' TObject = class',
  15452. ' destructor Destroy;',
  15453. ' procedure Free;',
  15454. ' end;',
  15455. 'destructor TObject.Destroy; begin end;',
  15456. 'procedure tobject.free; begin end;',
  15457. 'var o: tobject;',
  15458. 'begin',
  15459. ' o.free;',
  15460. '']);
  15461. Converter.UseLowerCase:=true;
  15462. ConvertProgram;
  15463. CheckSource('TestClass_TObjectFreeLowerCase',
  15464. LinesToStr([ // statements
  15465. 'rtl.createClass(this, "tobject", null, function () {',
  15466. ' this.$init = function () {',
  15467. ' };',
  15468. ' this.$final = function () {',
  15469. ' };',
  15470. ' rtl.tObjectDestroy = "destroy";',
  15471. ' this.destroy = function () {',
  15472. ' };',
  15473. ' this.free = function () {',
  15474. ' };',
  15475. '});',
  15476. 'this.o = null;',
  15477. '']),
  15478. LinesToStr([ // $mod.$main
  15479. 'rtl.free($mod, "o");',
  15480. '']));
  15481. end;
  15482. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15483. begin
  15484. StartProgram(false);
  15485. Add([
  15486. 'type',
  15487. ' TObject = class',
  15488. ' procedure Free;',
  15489. ' function GetObj: tobject; virtual; abstract;',
  15490. ' end;',
  15491. 'procedure tobject.free;',
  15492. 'begin',
  15493. 'end;',
  15494. 'var o: tobject;',
  15495. 'begin',
  15496. ' o.getobj.free;',
  15497. '']);
  15498. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15499. ConvertProgram;
  15500. end;
  15501. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15502. begin
  15503. StartProgram(false);
  15504. Add([
  15505. 'type',
  15506. ' TObject = class',
  15507. ' procedure Free;',
  15508. ' FObj: TObject;',
  15509. ' property Obj: tobject read FObj write FObj;',
  15510. ' end;',
  15511. 'procedure tobject.free;',
  15512. 'begin',
  15513. 'end;',
  15514. 'var o: tobject;',
  15515. 'begin',
  15516. ' o.obj.free;',
  15517. '']);
  15518. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15519. ConvertProgram;
  15520. end;
  15521. procedure TTestModule.TestClass_ForIn;
  15522. begin
  15523. StartProgram(false);
  15524. Add([
  15525. 'type',
  15526. ' TObject = class end;',
  15527. ' TItem = TObject;',
  15528. ' TEnumerator = class',
  15529. ' FCurrent: TItem;',
  15530. ' property Current: TItem read FCurrent;',
  15531. ' function MoveNext: boolean;',
  15532. ' end;',
  15533. ' TBird = class',
  15534. ' function GetEnumerator: TEnumerator;',
  15535. ' end;',
  15536. 'function TEnumerator.MoveNext: boolean;',
  15537. 'begin',
  15538. 'end;',
  15539. 'function TBird.GetEnumerator: TEnumerator;',
  15540. 'begin',
  15541. 'end;',
  15542. 'var',
  15543. ' b: TBird;',
  15544. ' i, i2: TItem;',
  15545. 'begin',
  15546. ' for i in b do i2:=i;']);
  15547. ConvertProgram;
  15548. CheckSource('TestClass_ForIn',
  15549. LinesToStr([ // statements
  15550. 'rtl.createClass(this, "TObject", null, function () {',
  15551. ' this.$init = function () {',
  15552. ' };',
  15553. ' this.$final = function () {',
  15554. ' };',
  15555. '});',
  15556. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15557. ' this.$init = function () {',
  15558. ' $mod.TObject.$init.call(this);',
  15559. ' this.FCurrent = null;',
  15560. ' };',
  15561. ' this.$final = function () {',
  15562. ' this.FCurrent = undefined;',
  15563. ' $mod.TObject.$final.call(this);',
  15564. ' };',
  15565. ' this.MoveNext = function () {',
  15566. ' var Result = false;',
  15567. ' return Result;',
  15568. ' };',
  15569. '});',
  15570. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15571. ' this.GetEnumerator = function () {',
  15572. ' var Result = null;',
  15573. ' return Result;',
  15574. ' };',
  15575. '});',
  15576. 'this.b = null;',
  15577. 'this.i = null;',
  15578. 'this.i2 = null;'
  15579. ]),
  15580. LinesToStr([ // $mod.$main
  15581. 'var $in = $mod.b.GetEnumerator();',
  15582. 'try {',
  15583. ' while ($in.MoveNext()){',
  15584. ' $mod.i = $in.FCurrent;',
  15585. ' $mod.i2 = $mod.i;',
  15586. ' }',
  15587. '} finally {',
  15588. ' $in = rtl.freeLoc($in)',
  15589. '};',
  15590. '']));
  15591. end;
  15592. procedure TTestModule.TestClass_DispatchMessage;
  15593. begin
  15594. StartProgram(false);
  15595. Add([
  15596. 'type',
  15597. ' TObject = class',
  15598. ' {$DispatchField DispInt}',
  15599. ' procedure Dispatch(var Msg); virtual; abstract;',
  15600. ' {$DispatchStrField DispStr}',
  15601. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15602. ' end;',
  15603. ' THopMsg = record',
  15604. ' DispInt: longint;',
  15605. ' end;',
  15606. ' TPutMsg = record',
  15607. ' DispStr: string;',
  15608. ' end;',
  15609. ' TBird = class',
  15610. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15611. ' procedure Run; overload; virtual; abstract;',
  15612. ' procedure Run(var Msg); overload; message ''Fast'';',
  15613. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15614. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15615. ' end;',
  15616. 'procedure TBird.Run(var Msg);',
  15617. 'begin',
  15618. 'end;',
  15619. 'begin',
  15620. '']);
  15621. ConvertProgram;
  15622. CheckSource('TestClass_Message',
  15623. LinesToStr([ // statements
  15624. 'rtl.createClass(this, "TObject", null, function () {',
  15625. ' this.$init = function () {',
  15626. ' };',
  15627. ' this.$final = function () {',
  15628. ' };',
  15629. '});',
  15630. 'rtl.recNewT(this, "THopMsg", function () {',
  15631. ' this.DispInt = 0;',
  15632. ' this.$eq = function (b) {',
  15633. ' return this.DispInt === b.DispInt;',
  15634. ' };',
  15635. ' this.$assign = function (s) {',
  15636. ' this.DispInt = s.DispInt;',
  15637. ' return this;',
  15638. ' };',
  15639. '});',
  15640. 'rtl.recNewT(this, "TPutMsg", function () {',
  15641. ' this.DispStr = "";',
  15642. ' this.$eq = function (b) {',
  15643. ' return this.DispStr === b.DispStr;',
  15644. ' };',
  15645. ' this.$assign = function (s) {',
  15646. ' this.DispStr = s.DispStr;',
  15647. ' return this;',
  15648. ' };',
  15649. '});',
  15650. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15651. ' this.Run$1 = function (Msg) {',
  15652. ' };',
  15653. ' this.$msgint = {',
  15654. ' "2": "Fly",',
  15655. ' "3": "Hop"',
  15656. ' };',
  15657. ' this.$msgstr = {',
  15658. ' Fast: "Run$1",',
  15659. ' foo: "Put"',
  15660. ' };',
  15661. '});',
  15662. '']),
  15663. LinesToStr([ // $mod.$main
  15664. '']));
  15665. end;
  15666. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15667. begin
  15668. StartProgram(false);
  15669. Add([
  15670. 'type',
  15671. ' TObject = class',
  15672. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15673. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15674. ' end;',
  15675. 'begin',
  15676. '']);
  15677. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15678. ConvertProgram;
  15679. end;
  15680. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15681. begin
  15682. StartProgram(false);
  15683. Add([
  15684. 'type',
  15685. ' TObject = class',
  15686. ' {$dispatchfield Msg}',
  15687. ' procedure Dispatch(var Msg); virtual; abstract;',
  15688. ' end;',
  15689. ' TFlyMsg = record',
  15690. ' FlyId: longint;',
  15691. ' end;',
  15692. ' TBird = class',
  15693. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15694. ' end;',
  15695. 'begin',
  15696. '']);
  15697. ConvertProgram;
  15698. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15699. end;
  15700. procedure TTestModule.TestClassOf_Create;
  15701. begin
  15702. StartProgram(false);
  15703. Add('type');
  15704. Add(' TObject = class');
  15705. Add(' constructor Create;');
  15706. Add(' end;');
  15707. Add(' TClass = class of TObject;');
  15708. Add('constructor tobject.create; begin end;');
  15709. Add('var');
  15710. Add(' Obj: tobject;');
  15711. Add(' C: tclass;');
  15712. Add('begin');
  15713. Add(' obj:=C.create;');
  15714. Add(' with c do obj:=create;');
  15715. ConvertProgram;
  15716. CheckSource('TestClassOf_Create',
  15717. LinesToStr([ // statements
  15718. 'rtl.createClass(this, "TObject", null, function () {',
  15719. ' this.$init = function () {',
  15720. ' };',
  15721. ' this.$final = function () {',
  15722. ' };',
  15723. ' this.Create = function () {',
  15724. ' return this;',
  15725. ' };',
  15726. '});',
  15727. 'this.Obj = null;',
  15728. 'this.C = null;'
  15729. ]),
  15730. LinesToStr([ // $mod.$main
  15731. '$mod.Obj = $mod.C.$create("Create");',
  15732. 'var $with = $mod.C;',
  15733. '$mod.Obj = $with.$create("Create");',
  15734. '']));
  15735. end;
  15736. procedure TTestModule.TestClassOf_Call;
  15737. begin
  15738. StartProgram(false);
  15739. Add('type');
  15740. Add(' TObject = class');
  15741. Add(' class procedure DoIt;');
  15742. Add(' end;');
  15743. Add(' TClass = class of TObject;');
  15744. Add('class procedure tobject.doit; begin end;');
  15745. Add('var');
  15746. Add(' C: tclass;');
  15747. Add('begin');
  15748. Add(' c.doit;');
  15749. Add(' with c do doit;');
  15750. ConvertProgram;
  15751. CheckSource('TestClassOf_Call',
  15752. LinesToStr([ // statements
  15753. 'rtl.createClass(this, "TObject", null, function () {',
  15754. ' this.$init = function () {',
  15755. ' };',
  15756. ' this.$final = function () {',
  15757. ' };',
  15758. ' this.DoIt = function () {',
  15759. ' };',
  15760. '});',
  15761. 'this.C = null;'
  15762. ]),
  15763. LinesToStr([ // $mod.$main
  15764. '$mod.C.DoIt();',
  15765. 'var $with = $mod.C;',
  15766. '$with.DoIt();',
  15767. '']));
  15768. end;
  15769. procedure TTestModule.TestClassOf_Assign;
  15770. begin
  15771. StartProgram(false);
  15772. Add('type');
  15773. Add(' TClass = class of TObject;');
  15774. Add(' TObject = class');
  15775. Add(' ClassType: TClass; ');
  15776. Add(' end;');
  15777. Add('var');
  15778. Add(' Obj: tobject;');
  15779. Add(' C: tclass;');
  15780. Add('begin');
  15781. Add(' c:=nil;');
  15782. Add(' c:=obj.classtype;');
  15783. ConvertProgram;
  15784. CheckSource('TestClassOf_Assign',
  15785. LinesToStr([ // statements
  15786. 'rtl.createClass(this, "TObject", null, function () {',
  15787. ' this.$init = function () {',
  15788. ' this.ClassType = null;',
  15789. ' };',
  15790. ' this.$final = function () {',
  15791. ' this.ClassType = undefined;',
  15792. ' };',
  15793. '});',
  15794. 'this.Obj = null;',
  15795. 'this.C = null;'
  15796. ]),
  15797. LinesToStr([ // $mod.$main
  15798. '$mod.C = null;',
  15799. '$mod.C = $mod.Obj.ClassType;',
  15800. '']));
  15801. end;
  15802. procedure TTestModule.TestClassOf_Is;
  15803. begin
  15804. StartProgram(false);
  15805. Add('type');
  15806. Add(' TClass = class of TObject;');
  15807. Add(' TObject = class');
  15808. Add(' end;');
  15809. Add(' TCar = class');
  15810. Add(' end;');
  15811. Add(' TCars = class of TCar;');
  15812. Add('var');
  15813. Add(' Obj: tobject;');
  15814. Add(' C: tclass;');
  15815. Add(' Cars: tcars;');
  15816. Add('begin');
  15817. Add(' if c is tcar then ;');
  15818. Add(' if c is tcars then ;');
  15819. ConvertProgram;
  15820. CheckSource('TestClassOf_Is',
  15821. LinesToStr([ // statements
  15822. 'rtl.createClass(this, "TObject", null, function () {',
  15823. ' this.$init = function () {',
  15824. ' };',
  15825. ' this.$final = function () {',
  15826. ' };',
  15827. '});',
  15828. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15829. '});',
  15830. 'this.Obj = null;',
  15831. 'this.C = null;',
  15832. 'this.Cars = null;'
  15833. ]),
  15834. LinesToStr([ // $mod.$main
  15835. 'if(rtl.is($mod.C,$mod.TCar));',
  15836. 'if(rtl.is($mod.C,$mod.TCar));',
  15837. '']));
  15838. end;
  15839. procedure TTestModule.TestClassOf_Compare;
  15840. begin
  15841. StartProgram(false);
  15842. Add('type');
  15843. Add(' TClass = class of TObject;');
  15844. Add(' TObject = class');
  15845. Add(' ClassType: TClass; ');
  15846. Add(' end;');
  15847. Add('var');
  15848. Add(' b: boolean;');
  15849. Add(' Obj: tobject;');
  15850. Add(' C: tclass;');
  15851. Add('begin');
  15852. Add(' b:=c=nil;');
  15853. Add(' b:=nil=c;');
  15854. Add(' b:=c=obj.classtype;');
  15855. Add(' b:=obj.classtype=c;');
  15856. Add(' b:=c=TObject;');
  15857. Add(' b:=TObject=c;');
  15858. Add(' b:=c<>nil;');
  15859. Add(' b:=nil<>c;');
  15860. Add(' b:=c<>obj.classtype;');
  15861. Add(' b:=obj.classtype<>c;');
  15862. Add(' b:=c<>TObject;');
  15863. Add(' b:=TObject<>c;');
  15864. ConvertProgram;
  15865. CheckSource('TestClassOf_Compare',
  15866. LinesToStr([ // statements
  15867. 'rtl.createClass(this, "TObject", null, function () {',
  15868. ' this.$init = function () {',
  15869. ' this.ClassType = null;',
  15870. ' };',
  15871. ' this.$final = function () {',
  15872. ' this.ClassType = undefined;',
  15873. ' };',
  15874. '});',
  15875. 'this.b = false;',
  15876. 'this.Obj = null;',
  15877. 'this.C = null;'
  15878. ]),
  15879. LinesToStr([ // $mod.$main
  15880. '$mod.b = $mod.C === null;',
  15881. '$mod.b = null === $mod.C;',
  15882. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15883. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15884. '$mod.b = $mod.C === $mod.TObject;',
  15885. '$mod.b = $mod.TObject === $mod.C;',
  15886. '$mod.b = $mod.C !== null;',
  15887. '$mod.b = null !== $mod.C;',
  15888. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15889. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15890. '$mod.b = $mod.C !== $mod.TObject;',
  15891. '$mod.b = $mod.TObject !== $mod.C;',
  15892. '']));
  15893. end;
  15894. procedure TTestModule.TestClassOf_ClassVar;
  15895. begin
  15896. StartProgram(false);
  15897. Add('type');
  15898. Add(' TObject = class');
  15899. Add(' class var id: longint;');
  15900. Add(' end;');
  15901. Add(' TClass = class of TObject;');
  15902. Add('var');
  15903. Add(' C: tclass;');
  15904. Add('begin');
  15905. Add(' C.id:=C.id;');
  15906. ConvertProgram;
  15907. CheckSource('TestClassOf_ClassVar',
  15908. LinesToStr([ // statements
  15909. 'rtl.createClass(this, "TObject", null, function () {',
  15910. ' this.id = 0;',
  15911. ' this.$init = function () {',
  15912. ' };',
  15913. ' this.$final = function () {',
  15914. ' };',
  15915. '});',
  15916. 'this.C = null;'
  15917. ]),
  15918. LinesToStr([ // $mod.$main
  15919. '$mod.TObject.id = $mod.C.id;',
  15920. '']));
  15921. end;
  15922. procedure TTestModule.TestClassOf_ClassMethod;
  15923. begin
  15924. StartProgram(false);
  15925. Add('type');
  15926. Add(' TObject = class');
  15927. Add(' class function DoIt(i: longint = 0): longint;');
  15928. Add(' end;');
  15929. Add(' TClass = class of TObject;');
  15930. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15931. Add('var');
  15932. Add(' i: longint;');
  15933. Add(' C: tclass;');
  15934. Add('begin');
  15935. Add(' C.DoIt;');
  15936. Add(' C.DoIt();');
  15937. Add(' i:=C.DoIt;');
  15938. Add(' i:=C.DoIt();');
  15939. ConvertProgram;
  15940. CheckSource('TestClassOf_ClassMethod',
  15941. LinesToStr([ // statements
  15942. 'rtl.createClass(this, "TObject", null, function () {',
  15943. ' this.$init = function () {',
  15944. ' };',
  15945. ' this.$final = function () {',
  15946. ' };',
  15947. ' this.DoIt = function (i) {',
  15948. ' var Result = 0;',
  15949. ' return Result;',
  15950. ' };',
  15951. '});',
  15952. 'this.i = 0;',
  15953. 'this.C = null;'
  15954. ]),
  15955. LinesToStr([ // $mod.$main
  15956. '$mod.C.DoIt(0);',
  15957. '$mod.C.DoIt(0);',
  15958. '$mod.i = $mod.C.DoIt(0);',
  15959. '$mod.i = $mod.C.DoIt(0);',
  15960. '']));
  15961. end;
  15962. procedure TTestModule.TestClassOf_ClassProperty;
  15963. begin
  15964. StartProgram(false);
  15965. Add([
  15966. 'type',
  15967. ' TObject = class',
  15968. ' class var FA: longint;',
  15969. ' class function GetA: longint;',
  15970. ' class procedure SetA(Value: longint);',
  15971. ' class property pA: longint read fa write fa;',
  15972. ' class property pB: longint read geta write seta;',
  15973. ' end;',
  15974. ' TObjectClass = class of tobject;',
  15975. 'class function tobject.geta: longint; begin end;',
  15976. 'class procedure tobject.seta(value: longint); begin end;',
  15977. 'var',
  15978. ' b: boolean;',
  15979. ' Obj: tobject;',
  15980. ' Cla: tobjectclass;',
  15981. 'begin',
  15982. ' obj.pa:=obj.pa;',
  15983. ' obj.pb:=obj.pb;',
  15984. ' b:=obj.pa=4;',
  15985. ' b:=obj.pb=obj.pb;',
  15986. ' b:=5=obj.pa;',
  15987. ' cla.pa:=6;',
  15988. ' cla.pa:=cla.pa;',
  15989. ' cla.pb:=cla.pb;',
  15990. ' b:=cla.pa=7;',
  15991. ' b:=cla.pb=cla.pb;',
  15992. ' b:=8=cla.pa;',
  15993. ' tobject.pa:=9;',
  15994. ' tobject.pb:=tobject.pb;',
  15995. ' b:=tobject.pa=10;',
  15996. ' b:=11=tobject.pa;',
  15997. '']);
  15998. ConvertProgram;
  15999. CheckSource('TestClassOf_ClassProperty',
  16000. LinesToStr([ // statements
  16001. 'rtl.createClass(this, "TObject", null, function () {',
  16002. ' this.FA = 0;',
  16003. ' this.$init = function () {',
  16004. ' };',
  16005. ' this.$final = function () {',
  16006. ' };',
  16007. ' this.GetA = function () {',
  16008. ' var Result = 0;',
  16009. ' return Result;',
  16010. ' };',
  16011. ' this.SetA = function (Value) {',
  16012. ' };',
  16013. '});',
  16014. 'this.b = false;',
  16015. 'this.Obj = null;',
  16016. 'this.Cla = null;'
  16017. ]),
  16018. LinesToStr([ // $mod.$main
  16019. '$mod.TObject.FA = $mod.Obj.FA;',
  16020. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16021. '$mod.b = $mod.Obj.FA === 4;',
  16022. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16023. '$mod.b = 5 === $mod.Obj.FA;',
  16024. '$mod.TObject.FA = 6;',
  16025. '$mod.TObject.FA = $mod.Cla.FA;',
  16026. '$mod.Cla.SetA($mod.Cla.GetA());',
  16027. '$mod.b = $mod.Cla.FA === 7;',
  16028. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16029. '$mod.b = 8 === $mod.Cla.FA;',
  16030. '$mod.TObject.FA = 9;',
  16031. '$mod.TObject.SetA($mod.TObject.GetA());',
  16032. '$mod.b = $mod.TObject.FA === 10;',
  16033. '$mod.b = 11 === $mod.TObject.FA;',
  16034. '']));
  16035. end;
  16036. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16037. begin
  16038. StartProgram(false);
  16039. Add('type');
  16040. Add(' TObject = class');
  16041. Add(' class var GlobalId: longint;');
  16042. Add(' class procedure ProcA;');
  16043. Add(' end;');
  16044. Add('class procedure tobject.proca;');
  16045. Add('var b: boolean;');
  16046. Add('begin');
  16047. Add(' b:=self=nil;');
  16048. Add(' b:=self.globalid=3;');
  16049. Add(' b:=4=self.globalid;');
  16050. Add(' self.globalid:=5;');
  16051. Add(' self.proca;');
  16052. Add('end;');
  16053. Add('begin');
  16054. ConvertProgram;
  16055. CheckSource('TestClassOf_ClassMethodSelf',
  16056. LinesToStr([ // statements
  16057. 'rtl.createClass(this, "TObject", null, function () {',
  16058. ' this.GlobalId = 0;',
  16059. ' this.$init = function () {',
  16060. ' };',
  16061. ' this.$final = function () {',
  16062. ' };',
  16063. ' this.ProcA = function () {',
  16064. ' var b = false;',
  16065. ' b = this === null;',
  16066. ' b = this.GlobalId === 3;',
  16067. ' b = 4 === this.GlobalId;',
  16068. ' $mod.TObject.GlobalId = 5;',
  16069. ' this.ProcA();',
  16070. ' };',
  16071. '});'
  16072. ]),
  16073. LinesToStr([ // $mod.$main
  16074. '']));
  16075. end;
  16076. procedure TTestModule.TestClassOf_TypeCast;
  16077. begin
  16078. StartProgram(false);
  16079. Add('type');
  16080. Add(' TObject = class');
  16081. Add(' class procedure {#TObject_DoIt}DoIt;');
  16082. Add(' end;');
  16083. Add(' TClass = class of TObject;');
  16084. Add(' TMobile = class');
  16085. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16086. Add(' end;');
  16087. Add(' TMobileClass = class of TMobile;');
  16088. Add(' TCar = class(TMobile)');
  16089. Add(' class procedure {#TCar_DoIt}DoIt;');
  16090. Add(' end;');
  16091. Add(' TCarClass = class of TCar;');
  16092. Add('class procedure TObject.DoIt;');
  16093. Add('begin');
  16094. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16095. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16096. Add('end;');
  16097. Add('class procedure TMobile.DoIt;');
  16098. Add('begin');
  16099. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16100. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16101. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16102. Add('end;');
  16103. Add('class procedure TCar.DoIt; begin end;');
  16104. Add('var');
  16105. Add(' ObjC: TClass;');
  16106. Add(' MobileC: TMobileClass;');
  16107. Add(' CarC: TCarClass;');
  16108. Add('begin');
  16109. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16110. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16111. Add(' CarC.{@TCar_DoIt}DoIt;');
  16112. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16113. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16114. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16115. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16116. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16117. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16118. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16119. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16120. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16121. ConvertProgram;
  16122. CheckSource('TestClassOf_TypeCast',
  16123. LinesToStr([ // statements
  16124. 'rtl.createClass(this, "TObject", null, function () {',
  16125. ' this.$init = function () {',
  16126. ' };',
  16127. ' this.$final = function () {',
  16128. ' };',
  16129. ' this.DoIt = function () {',
  16130. ' this.DoIt();',
  16131. ' this.DoIt$1();',
  16132. ' };',
  16133. '});',
  16134. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16135. ' this.DoIt$1 = function () {',
  16136. ' this.DoIt();',
  16137. ' this.DoIt$1();',
  16138. ' this.DoIt$2();',
  16139. ' };',
  16140. '});',
  16141. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16142. ' this.DoIt$2 = function () {',
  16143. ' };',
  16144. '});',
  16145. 'this.ObjC = null;',
  16146. 'this.MobileC = null;',
  16147. 'this.CarC = null;',
  16148. '']),
  16149. LinesToStr([ // $mod.$main
  16150. '$mod.ObjC.DoIt();',
  16151. '$mod.MobileC.DoIt$1();',
  16152. '$mod.CarC.DoIt$2();',
  16153. '$mod.ObjC.DoIt();',
  16154. '$mod.ObjC.DoIt$1();',
  16155. '$mod.ObjC.DoIt$2();',
  16156. '$mod.MobileC.DoIt();',
  16157. '$mod.MobileC.DoIt$1();',
  16158. '$mod.MobileC.DoIt$2();',
  16159. '$mod.CarC.DoIt();',
  16160. '$mod.CarC.DoIt$1();',
  16161. '$mod.CarC.DoIt$2();',
  16162. '']));
  16163. end;
  16164. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16165. begin
  16166. StartProgram(false);
  16167. Add('type');
  16168. Add(' TObject = class');
  16169. Add(' function CurNow: longint; ');
  16170. Add(' class function Now: longint; ');
  16171. Add(' end;');
  16172. Add('function TObject.CurNow: longint; begin end;');
  16173. Add('class function TObject.Now: longint; begin end;');
  16174. Add('var');
  16175. Add(' Obj: tobject;');
  16176. Add(' vI: longint;');
  16177. Add('begin');
  16178. Add(' obj.curnow;');
  16179. Add(' vi:=obj.curnow;');
  16180. Add(' tobject.now;');
  16181. Add(' vi:=tobject.now;');
  16182. ConvertProgram;
  16183. CheckSource('TestClassOf_ImplicitFunctionCall',
  16184. LinesToStr([ // statements
  16185. 'rtl.createClass(this, "TObject", null, function () {',
  16186. ' this.$init = function () {',
  16187. ' };',
  16188. ' this.$final = function () {',
  16189. ' };',
  16190. ' this.CurNow = function () {',
  16191. ' var Result = 0;',
  16192. ' return Result;',
  16193. ' };',
  16194. ' this.Now = function () {',
  16195. ' var Result = 0;',
  16196. ' return Result;',
  16197. ' };',
  16198. '});',
  16199. 'this.Obj = null;',
  16200. 'this.vI = 0;',
  16201. '']),
  16202. LinesToStr([ // $mod.$main
  16203. '$mod.Obj.CurNow();',
  16204. '$mod.vI = $mod.Obj.CurNow();',
  16205. '$mod.TObject.Now();',
  16206. '$mod.vI = $mod.TObject.Now();',
  16207. '']));
  16208. end;
  16209. procedure TTestModule.TestClassOf_Const;
  16210. begin
  16211. StartProgram(false);
  16212. Add([
  16213. 'type',
  16214. ' TObject = class',
  16215. ' end;',
  16216. ' TBird = TObject;',
  16217. ' TBirds = class of TBird;',
  16218. ' TEagles = TBirds;',
  16219. ' THawk = class(TBird);',
  16220. 'const',
  16221. ' Hawk: TEagles = THawk;',
  16222. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16223. ' TBird,',
  16224. ' THawk',
  16225. ' );',
  16226. 'begin']);
  16227. ConvertProgram;
  16228. CheckSource('TestClassOf_Const',
  16229. LinesToStr([ // statements
  16230. 'rtl.createClass(this, "TObject", null, function () {',
  16231. ' this.$init = function () {',
  16232. ' };',
  16233. ' this.$final = function () {',
  16234. ' };',
  16235. '});',
  16236. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16237. '});',
  16238. 'this.Hawk = this.THawk;',
  16239. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16240. '']),
  16241. LinesToStr([ // $mod.$main
  16242. '']));
  16243. end;
  16244. procedure TTestModule.TestNestedClass_Alias;
  16245. begin
  16246. WithTypeInfo:=true;
  16247. StartProgram(false);
  16248. Add([
  16249. 'type',
  16250. ' TObject = class',
  16251. ' type TNested = type longint;',
  16252. ' end;',
  16253. 'type TAlias = type tobject.tnested;',
  16254. 'var i: tobject.tnested = 3;',
  16255. 'var j: TAlias = 4;',
  16256. 'begin',
  16257. ' if typeinfo(TAlias)=nil then ;',
  16258. ' if typeinfo(tobject.tnested)=nil then ;',
  16259. '']);
  16260. ConvertProgram;
  16261. CheckSource('TestNestedClass_Alias',
  16262. LinesToStr([ // statements
  16263. 'rtl.createClass(this, "TObject", null, function () {',
  16264. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16265. ' this.$init = function () {',
  16266. ' };',
  16267. ' this.$final = function () {',
  16268. ' };',
  16269. '});',
  16270. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16271. 'this.i = 3;',
  16272. 'this.j = 4;',
  16273. '']),
  16274. LinesToStr([ // $mod.$main
  16275. 'if ($mod.$rtti["TAlias"] === null) ;',
  16276. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16277. '']));
  16278. end;
  16279. procedure TTestModule.TestNestedClass_Record;
  16280. begin
  16281. WithTypeInfo:=true;
  16282. StartProgram(false);
  16283. Add([
  16284. 'type',
  16285. ' TObject = class',
  16286. ' type TPoint = record',
  16287. ' x,y: byte;',
  16288. ' end;',
  16289. ' procedure DoIt(t: TPoint);',
  16290. ' end;',
  16291. 'procedure tobject.DoIt(t: TPoint);',
  16292. 'var p: TPoint;',
  16293. 'begin',
  16294. ' t.x:=t.y;',
  16295. ' p:=t;',
  16296. 'end;',
  16297. 'var',
  16298. ' p: tobject.tpoint = (x:2; y:4);',
  16299. ' o: TObject;',
  16300. 'begin',
  16301. ' p:=p;',
  16302. ' o.doit(p);',
  16303. '']);
  16304. ConvertProgram;
  16305. CheckSource('TestNestedClass_Record',
  16306. LinesToStr([ // statements
  16307. 'rtl.createClass(this, "TObject", null, function () {',
  16308. ' rtl.recNewT(this, "TPoint", function () {',
  16309. ' this.x = 0;',
  16310. ' this.y = 0;',
  16311. ' this.$eq = function (b) {',
  16312. ' return (this.x === b.x) && (this.y === b.y);',
  16313. ' };',
  16314. ' this.$assign = function (s) {',
  16315. ' this.x = s.x;',
  16316. ' this.y = s.y;',
  16317. ' return this;',
  16318. ' };',
  16319. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16320. ' $r.addField("x", rtl.byte);',
  16321. ' $r.addField("y", rtl.byte);',
  16322. ' });',
  16323. ' this.$init = function () {',
  16324. ' };',
  16325. ' this.$final = function () {',
  16326. ' };',
  16327. ' this.DoIt = function (t) {',
  16328. ' var p = this.TPoint.$new();',
  16329. ' t.x = t.y;',
  16330. ' p.$assign(t);',
  16331. ' };',
  16332. '});',
  16333. 'this.p = this.TObject.TPoint.$clone({',
  16334. ' x: 2,',
  16335. ' y: 4',
  16336. '});',
  16337. 'this.o = null;',
  16338. '']),
  16339. LinesToStr([ // $mod.$main
  16340. '$mod.p.$assign($mod.p);',
  16341. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16342. '']));
  16343. end;
  16344. procedure TTestModule.TestNestedClass_Class;
  16345. begin
  16346. WithTypeInfo:=true;
  16347. StartProgram(false);
  16348. Add([
  16349. 'type',
  16350. ' TObject = class end;',
  16351. ' TBird = class',
  16352. ' type TLeg = class',
  16353. ' FId: longint;',
  16354. ' constructor Create;',
  16355. ' function Create(i: longint): TLeg;',
  16356. ' end;',
  16357. ' function DoIt(b: TBird): Tleg;',
  16358. ' end;',
  16359. 'constructor tbird.tleg.create;',
  16360. 'begin',
  16361. ' FId:=3;',
  16362. 'end;',
  16363. 'function tbird.tleg.Create(i: longint): TLeg;',
  16364. 'begin',
  16365. ' Create;',
  16366. ' Result:=TLeg.Create;',
  16367. ' Result:=TBird.TLeg.Create;',
  16368. ' Result:=Create(3);',
  16369. ' FId:=i;',
  16370. 'end;',
  16371. 'function tbird.DoIt(b: tbird): tleg;',
  16372. 'begin',
  16373. ' Result.Create;',
  16374. ' Result:=TLeg.Create;',
  16375. ' Result:=TBird.TLeg.Create;',
  16376. ' Result:=Result.Create(3);',
  16377. 'end;',
  16378. 'var',
  16379. ' b: Tbird.tleg;',
  16380. 'begin',
  16381. ' b.Create;',
  16382. ' b:=TBird.TLeg.Create;',
  16383. ' b:=b.Create(3);',
  16384. '']);
  16385. ConvertProgram;
  16386. CheckSource('TestNestedClass_Class',
  16387. LinesToStr([ // statements
  16388. 'rtl.createClass(this, "TObject", null, function () {',
  16389. ' this.$init = function () {',
  16390. ' };',
  16391. ' this.$final = function () {',
  16392. ' };',
  16393. '});',
  16394. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16395. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16396. ' this.$init = function () {',
  16397. ' $mod.TObject.$init.call(this);',
  16398. ' this.FId = 0;',
  16399. ' };',
  16400. ' this.Create = function () {',
  16401. ' this.FId = 3;',
  16402. ' return this;',
  16403. ' };',
  16404. ' this.Create$1 = function (i) {',
  16405. ' var Result = null;',
  16406. ' this.Create();',
  16407. ' Result = $mod.TBird.TLeg.$create("Create");',
  16408. ' Result = $mod.TBird.TLeg.$create("Create");',
  16409. ' Result = this.Create$1(3);',
  16410. ' this.FId = i;',
  16411. ' return Result;',
  16412. ' };',
  16413. ' }, "TBird.TLeg");',
  16414. ' this.DoIt = function (b) {',
  16415. ' var Result = null;',
  16416. ' Result.Create();',
  16417. ' Result = this.TLeg.$create("Create");',
  16418. ' Result = $mod.TBird.TLeg.$create("Create");',
  16419. ' Result = Result.Create$1(3);',
  16420. ' return Result;',
  16421. ' };',
  16422. '});',
  16423. 'this.b = null;',
  16424. '']),
  16425. LinesToStr([ // $mod.$main
  16426. '$mod.b.Create();',
  16427. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16428. '$mod.b = $mod.b.Create$1(3);',
  16429. '']));
  16430. end;
  16431. procedure TTestModule.TestExternalClass_Var;
  16432. begin
  16433. StartProgram(false);
  16434. Add([
  16435. '{$modeswitch externalclass}',
  16436. 'type',
  16437. ' TExtA = class external name ''ExtObj''',
  16438. ' Id: longint external name ''$Id'';',
  16439. ' B: longint;',
  16440. ' end;',
  16441. 'var Obj: TExtA;',
  16442. 'begin',
  16443. ' obj.id:=obj.id+1;',
  16444. ' obj.B:=obj.B+1;']);
  16445. ConvertProgram;
  16446. CheckSource('TestExternalClass_Var',
  16447. LinesToStr([ // statements
  16448. 'this.Obj = null;',
  16449. '']),
  16450. LinesToStr([ // $mod.$main
  16451. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16452. '$mod.Obj.B = $mod.Obj.B + 1;',
  16453. '']));
  16454. end;
  16455. procedure TTestModule.TestExternalClass_Const;
  16456. begin
  16457. StartProgram(false);
  16458. Add([
  16459. '{$modeswitch externalclass}',
  16460. 'type',
  16461. ' TExtA = class external name ''ExtObj''',
  16462. ' const Two: longint = 2;',
  16463. ' const Three = 3;',
  16464. ' const Id: longint;',
  16465. ' end;',
  16466. ' TExtB = class external name ''ExtB''',
  16467. ' A: TExtA;',
  16468. ' end;',
  16469. 'var',
  16470. ' A: texta;',
  16471. ' B: textb;',
  16472. ' i: longint;',
  16473. 'begin',
  16474. ' i:=a.two;',
  16475. ' i:=texta.two;',
  16476. ' i:=a.three;',
  16477. ' i:=texta.three;',
  16478. ' i:=a.id;',
  16479. ' i:=texta.id;',
  16480. '']);
  16481. ConvertProgram;
  16482. CheckSource('TestExternalClass_Const',
  16483. LinesToStr([ // statements
  16484. 'this.A = null;',
  16485. 'this.B = null;',
  16486. 'this.i = 0;',
  16487. '']),
  16488. LinesToStr([ // $mod.$main
  16489. '$mod.i = 2;',
  16490. '$mod.i = 2;',
  16491. '$mod.i = 3;',
  16492. '$mod.i = 3;',
  16493. '$mod.i = $mod.A.Id;',
  16494. '$mod.i = ExtObj.Id;',
  16495. '']));
  16496. end;
  16497. procedure TTestModule.TestExternalClass_Dollar;
  16498. begin
  16499. StartProgram(false);
  16500. Add([
  16501. '{$modeswitch externalclass}',
  16502. 'type',
  16503. ' TExtA = class external name ''$''',
  16504. ' Id: longint external name ''$'';',
  16505. ' function Bla(i: longint): longint; external name ''$'';',
  16506. ' end;',
  16507. 'function dollar(k: longint): longint; external name ''$'';',
  16508. 'var Obj: TExtA;',
  16509. 'begin',
  16510. ' dollar(1);',
  16511. ' obj.id:=obj.id+2;',
  16512. ' obj.Bla(3);',
  16513. '']);
  16514. ConvertProgram;
  16515. CheckSource('TestExternalClass_Dollar',
  16516. LinesToStr([ // statements
  16517. 'this.Obj = null;',
  16518. '']),
  16519. LinesToStr([ // $mod.$main
  16520. '$(1);',
  16521. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16522. '$mod.Obj.$(3);',
  16523. '']));
  16524. end;
  16525. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16526. begin
  16527. StartProgram(false);
  16528. Add('{$modeswitch externalclass}');
  16529. Add('type');
  16530. Add(' TExtA = class external name ''ExtA''');
  16531. Add(' Id: longint external name ''$Id'';');
  16532. Add(' end;');
  16533. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16534. Add(' Id: longint;');
  16535. Add(' end;');
  16536. Add('begin');
  16537. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16538. ConvertProgram;
  16539. end;
  16540. procedure TTestModule.TestExternalClass_Method;
  16541. begin
  16542. StartProgram(false);
  16543. Add(['{$modeswitch externalclass}',
  16544. 'type',
  16545. ' TExtA = class external name ''ExtObj''',
  16546. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16547. ' procedure DoSome(Id: longint = 1);',
  16548. ' end;',
  16549. 'var Obj: texta;',
  16550. 'begin',
  16551. ' obj.doit;',
  16552. ' obj.doit();',
  16553. ' obj.doit(2);',
  16554. ' with obj do begin',
  16555. ' doit;',
  16556. ' doit();',
  16557. ' doit(3);',
  16558. ' end;']);
  16559. ConvertProgram;
  16560. CheckSource('TestExternalClass_Method',
  16561. LinesToStr([ // statements
  16562. 'this.Obj = null;',
  16563. '']),
  16564. LinesToStr([ // $mod.$main
  16565. '$mod.Obj.$Execute(1);',
  16566. '$mod.Obj.$Execute(1);',
  16567. '$mod.Obj.$Execute(2);',
  16568. 'var $with = $mod.Obj;',
  16569. '$with.$Execute(1);',
  16570. '$with.$Execute(1);',
  16571. '$with.$Execute(3);',
  16572. '']));
  16573. end;
  16574. procedure TTestModule.TestExternalClass_ClassMethod;
  16575. begin
  16576. StartProgram(false);
  16577. Add([
  16578. '{$modeswitch externalclass}',
  16579. 'type',
  16580. ' TExtA = class external name ''ExtObj''',
  16581. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16582. ' end;',
  16583. ' TExtB = TExtA;',
  16584. 'var p: Pointer;',
  16585. 'begin',
  16586. ' texta.doit;',
  16587. ' texta.doit();',
  16588. ' texta.doit(2);',
  16589. ' p:[email protected];',
  16590. ' with texta do begin',
  16591. ' doit;',
  16592. ' doit();',
  16593. ' doit(3);',
  16594. ' p:=@DoIt;',
  16595. ' end;',
  16596. ' textb.doit;',
  16597. ' textb.doit();',
  16598. ' textb.doit(4);',
  16599. ' with textb do begin',
  16600. ' doit;',
  16601. ' doit();',
  16602. ' doit(5);',
  16603. ' end;',
  16604. '']);
  16605. ConvertProgram;
  16606. CheckSource('TestExternalClass_ClassMethod',
  16607. LinesToStr([ // statements
  16608. 'this.p = null;',
  16609. '']),
  16610. LinesToStr([ // $mod.$main
  16611. 'ExtObj.$Execute(1);',
  16612. 'ExtObj.$Execute(1);',
  16613. 'ExtObj.$Execute(2);',
  16614. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16615. 'ExtObj.$Execute(1);',
  16616. 'ExtObj.$Execute(1);',
  16617. 'ExtObj.$Execute(3);',
  16618. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16619. 'ExtObj.$Execute(1);',
  16620. 'ExtObj.$Execute(1);',
  16621. 'ExtObj.$Execute(4);',
  16622. 'ExtObj.$Execute(1);',
  16623. 'ExtObj.$Execute(1);',
  16624. 'ExtObj.$Execute(5);',
  16625. '']));
  16626. end;
  16627. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16628. begin
  16629. StartProgram(false);
  16630. Add([
  16631. '{$modeswitch externalclass}',
  16632. 'type',
  16633. ' TExtA = class external name ''ExtObj''',
  16634. ' class procedure DoIt(Id: longint = 1); static;',
  16635. ' end;',
  16636. 'var p: Pointer;',
  16637. 'begin',
  16638. ' texta.doit;',
  16639. ' texta.doit();',
  16640. ' texta.doit(2);',
  16641. ' p:[email protected];',
  16642. ' with texta do begin',
  16643. ' doit;',
  16644. ' doit();',
  16645. ' doit(3);',
  16646. ' p:=@DoIt;',
  16647. ' end;',
  16648. '']);
  16649. ConvertProgram;
  16650. CheckSource('TestExternalClass_ClassMethodStatic',
  16651. LinesToStr([ // statements
  16652. 'this.p = null;',
  16653. '']),
  16654. LinesToStr([ // $mod.$main
  16655. 'ExtObj.DoIt(1);',
  16656. 'ExtObj.DoIt(1);',
  16657. 'ExtObj.DoIt(2);',
  16658. '$mod.p = ExtObj.DoIt;',
  16659. 'ExtObj.DoIt(1);',
  16660. 'ExtObj.DoIt(1);',
  16661. 'ExtObj.DoIt(3);',
  16662. '$mod.p = ExtObj.DoIt;',
  16663. '']));
  16664. end;
  16665. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16666. begin
  16667. StartProgram(false);
  16668. Add([
  16669. '{$modeswitch externalclass}',
  16670. 'type',
  16671. ' TBird = class external name ''Array''',
  16672. ' end;',
  16673. 'function GetPtr: Pointer;',
  16674. 'begin',
  16675. 'end;',
  16676. 'procedure Write(const p);',
  16677. 'begin',
  16678. 'end;',
  16679. 'procedure WriteLn; varargs;',
  16680. 'begin',
  16681. 'end;',
  16682. 'begin',
  16683. ' if TBird(GetPtr)=nil then ;',
  16684. ' Write(GetPtr);',
  16685. ' WriteLn(GetPtr);',
  16686. ' Write(TBird(GetPtr));',
  16687. ' WriteLn(TBird(GetPtr));',
  16688. '']);
  16689. ConvertProgram;
  16690. CheckSource('TestFunctionResultInTypeCast',
  16691. LinesToStr([ // statements
  16692. 'this.GetPtr = function () {',
  16693. ' var Result = null;',
  16694. ' return Result;',
  16695. '};',
  16696. 'this.Write = function (p) {',
  16697. '};',
  16698. 'this.WriteLn = function () {',
  16699. '};',
  16700. '']),
  16701. LinesToStr([
  16702. 'if ($mod.GetPtr() === null) ;',
  16703. '$mod.Write($mod.GetPtr());',
  16704. '$mod.WriteLn($mod.GetPtr());',
  16705. '$mod.Write($mod.GetPtr());',
  16706. '$mod.WriteLn($mod.GetPtr());',
  16707. '']));
  16708. end;
  16709. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16710. begin
  16711. StartProgram(false);
  16712. Add([
  16713. '{$modeswitch externalclass}',
  16714. 'type',
  16715. ' TExtA = class external name ''ExtObjA''',
  16716. ' procedure ProcA; virtual;',
  16717. ' procedure ProcB; virtual;',
  16718. ' end;',
  16719. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16720. ' end;',
  16721. ' TExtC = class (TExtB)',
  16722. ' procedure ProcA; override;',
  16723. ' end;',
  16724. 'procedure TExtC.ProcA;',
  16725. 'begin',
  16726. ' ProcA;',
  16727. ' Self.ProcA;',
  16728. ' ProcB;',
  16729. ' Self.ProcB;',
  16730. 'end;',
  16731. 'var',
  16732. ' A: texta;',
  16733. ' B: textb;',
  16734. ' C: textc;',
  16735. 'begin',
  16736. ' a.proca;',
  16737. ' b.proca;',
  16738. ' c.proca;']);
  16739. ConvertProgram;
  16740. CheckSource('TestExternalClass_NonExternalOverride',
  16741. LinesToStr([ // statements
  16742. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16743. ' this.$init = function () {',
  16744. ' };',
  16745. ' this.$final = function () {',
  16746. ' };',
  16747. ' this.ProcA = function () {',
  16748. ' this.ProcA();',
  16749. ' this.ProcA();',
  16750. ' this.ProcB();',
  16751. ' this.ProcB();',
  16752. ' };',
  16753. '});',
  16754. 'this.A = null;',
  16755. 'this.B = null;',
  16756. 'this.C = null;',
  16757. '']),
  16758. LinesToStr([ // $mod.$main
  16759. '$mod.A.ProcA();',
  16760. '$mod.B.ProcA();',
  16761. '$mod.C.ProcA();',
  16762. '']));
  16763. end;
  16764. procedure TTestModule.TestExternalClass_OverloadHint;
  16765. begin
  16766. StartProgram(false);
  16767. Add([
  16768. '{$modeswitch externalclass}',
  16769. 'type',
  16770. ' TExtA = class external name ''ExtObjA''',
  16771. ' procedure DoIt;',
  16772. ' procedure DoIt(i: longint);',
  16773. ' end;',
  16774. 'begin',
  16775. '']);
  16776. ConvertProgram;
  16777. CheckResolverUnexpectedHints(true);
  16778. CheckSource('TestExternalClass_OverloadHint',
  16779. LinesToStr([ // statements
  16780. '']),
  16781. LinesToStr([ // $mod.$main
  16782. '']));
  16783. end;
  16784. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16785. begin
  16786. WithTypeInfo:=true;
  16787. StartProgram(false);
  16788. Add([
  16789. '{$modeswitch externalclass}',
  16790. 'type',
  16791. ' JSwiper = class external name ''Swiper''',
  16792. ' constructor New;',
  16793. ' end;',
  16794. ' TObject = class',
  16795. ' private',
  16796. ' FSwiper: JSwiper;',
  16797. ' published',
  16798. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16799. ' end;',
  16800. 'begin',
  16801. ' JSwiper.new;',
  16802. '']);
  16803. ConvertProgram;
  16804. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16805. LinesToStr([ // statements
  16806. 'this.$rtti.$ExtClass("JSwiper", {',
  16807. ' jsclass: "Swiper"',
  16808. '});',
  16809. 'rtl.createClass(this, "TObject", null, function () {',
  16810. ' this.$init = function () {',
  16811. ' this.FSwiper = null;',
  16812. ' };',
  16813. ' this.$final = function () {',
  16814. ' this.FSwiper = undefined;',
  16815. ' };',
  16816. ' var $r = this.$rtti;',
  16817. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16818. '});',
  16819. '']),
  16820. LinesToStr([ // $mod.$main
  16821. 'new Swiper();',
  16822. '']));
  16823. end;
  16824. procedure TTestModule.TestExternalClass_Property;
  16825. begin
  16826. StartProgram(false);
  16827. Add([
  16828. '{$modeswitch externalclass}',
  16829. 'type',
  16830. ' TExtA = class external name ''ExtA''',
  16831. ' function getYear: longint;',
  16832. ' procedure setYear(Value: longint);',
  16833. ' property Year: longint read getyear write setyear;',
  16834. ' end;',
  16835. ' TExtB = class (TExtA)',
  16836. ' procedure OtherSetYear(Value: longint);',
  16837. ' property year write othersetyear;',
  16838. ' end;',
  16839. 'procedure textb.othersetyear(value: longint);',
  16840. 'begin',
  16841. ' setYear(Value+4);',
  16842. 'end;',
  16843. 'var',
  16844. ' A: texta;',
  16845. ' B: textb;',
  16846. 'begin',
  16847. ' a.year:=a.year+1;',
  16848. ' b.year:=b.year+2;']);
  16849. ConvertProgram;
  16850. CheckSource('TestExternalClass_NonExternalOverride',
  16851. LinesToStr([ // statements
  16852. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16853. ' this.$init = function () {',
  16854. ' };',
  16855. ' this.$final = function () {',
  16856. ' };',
  16857. ' this.OtherSetYear = function (Value) {',
  16858. ' this.setYear(Value+4);',
  16859. ' };',
  16860. '});',
  16861. 'this.A = null;',
  16862. 'this.B = null;',
  16863. '']),
  16864. LinesToStr([ // $mod.$main
  16865. '$mod.A.setYear($mod.A.getYear()+1);',
  16866. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16867. '']));
  16868. end;
  16869. procedure TTestModule.TestExternalClass_PropertyDate;
  16870. begin
  16871. StartProgram(false);
  16872. Add([
  16873. '{$modeswitch externalclass}',
  16874. 'type',
  16875. ' TExtA = class external name ''ExtA''',
  16876. ' end;',
  16877. ' TExtB = class (TExtA)',
  16878. ' FDate: string;',
  16879. ' property Date: string read FDate write FDate;',
  16880. ' property ExtA: string read FDate write FDate;',
  16881. ' end;',
  16882. ' {$M+}',
  16883. ' TObject = class',
  16884. ' FDate: string;',
  16885. ' published',
  16886. ' property Date: string read FDate write FDate;',
  16887. ' property ExtA: string read FDate write FDate;',
  16888. ' end;',
  16889. 'var',
  16890. ' B: textb;',
  16891. ' o: TObject;',
  16892. 'begin',
  16893. ' b.date:=b.exta;',
  16894. ' o.date:=o.exta;']);
  16895. ConvertProgram;
  16896. CheckSource('TestExternalClass_PropertyDate',
  16897. LinesToStr([ // statements
  16898. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16899. ' this.$init = function () {',
  16900. ' this.FDate = "";',
  16901. ' };',
  16902. ' this.$final = function () {',
  16903. ' };',
  16904. '});',
  16905. 'rtl.createClass(this, "TObject", null, function () {',
  16906. ' this.$init = function () {',
  16907. ' this.FDate = "";',
  16908. ' };',
  16909. ' this.$final = function () {',
  16910. ' };',
  16911. ' var $r = this.$rtti;',
  16912. ' $r.addField("FDate", rtl.string);',
  16913. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16914. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16915. '});',
  16916. 'this.B = null;',
  16917. 'this.o = null;',
  16918. '']),
  16919. LinesToStr([ // $mod.$main
  16920. '$mod.B.FDate = $mod.B.FDate;',
  16921. '$mod.o.FDate = $mod.o.FDate;',
  16922. '']));
  16923. end;
  16924. procedure TTestModule.TestExternalClass_ClassProperty;
  16925. begin
  16926. StartProgram(false);
  16927. Add('{$modeswitch externalclass}');
  16928. Add('type');
  16929. Add(' TExtA = class external name ''ExtA''');
  16930. Add(' class function getYear: longint;');
  16931. Add(' class procedure setYear(Value: longint);');
  16932. Add(' class property Year: longint read getyear write setyear;');
  16933. Add(' end;');
  16934. Add(' TExtB = class (TExtA)');
  16935. Add(' class function GetCentury: longint;');
  16936. Add(' class procedure SetCentury(Value: longint);');
  16937. Add(' class property Century: longint read getcentury write setcentury;');
  16938. Add(' end;');
  16939. Add('class function textb.getcentury: longint;');
  16940. Add('begin');
  16941. Add('end;');
  16942. Add('class procedure textb.setcentury(value: longint);');
  16943. Add('begin');
  16944. Add(' setyear(value+11);');
  16945. Add(' texta.year:=texta.year+12;');
  16946. Add(' year:=year+13;');
  16947. Add(' textb.century:=textb.century+14;');
  16948. Add(' century:=century+15;');
  16949. Add('end;');
  16950. Add('var');
  16951. Add(' A: texta;');
  16952. Add(' B: textb;');
  16953. Add('begin');
  16954. Add(' texta.year:=texta.year+1;');
  16955. Add(' textb.year:=textb.year+2;');
  16956. Add(' TextA.year:=TextA.year+3;');
  16957. Add(' b.year:=b.year+4;');
  16958. Add(' textb.century:=textb.century+5;');
  16959. Add(' b.century:=b.century+6;');
  16960. ConvertProgram;
  16961. CheckSource('TestExternalClass_ClassProperty',
  16962. LinesToStr([ // statements
  16963. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16964. ' this.$init = function () {',
  16965. ' };',
  16966. ' this.$final = function () {',
  16967. ' };',
  16968. ' this.GetCentury = function () {',
  16969. ' var Result = 0;',
  16970. ' return Result;',
  16971. ' };',
  16972. ' this.SetCentury = function (Value) {',
  16973. ' this.setYear(Value + 11);',
  16974. ' ExtA.setYear(ExtA.getYear() + 12);',
  16975. ' this.setYear(this.getYear() + 13);',
  16976. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16977. ' this.SetCentury(this.GetCentury() + 15);',
  16978. ' };',
  16979. '});',
  16980. 'this.A = null;',
  16981. 'this.B = null;',
  16982. '']),
  16983. LinesToStr([ // $mod.$main
  16984. 'ExtA.setYear(ExtA.getYear() + 1);',
  16985. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16986. 'ExtA.setYear(ExtA.getYear() + 3);',
  16987. '$mod.B.setYear($mod.B.getYear() + 4);',
  16988. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16989. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16990. '']));
  16991. end;
  16992. procedure TTestModule.TestExternalClass_ClassOf;
  16993. begin
  16994. StartProgram(false);
  16995. Add('{$modeswitch externalclass}');
  16996. Add('type');
  16997. Add(' TExtA = class external name ''ExtA''');
  16998. Add(' procedure ProcA; virtual;');
  16999. Add(' procedure ProcB; virtual;');
  17000. Add(' end;');
  17001. Add(' TExtAClass = class of TExtA;');
  17002. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17003. Add(' end;');
  17004. Add(' TExtBClass = class of TExtB;');
  17005. Add(' TExtC = class (TExtB)');
  17006. Add(' procedure ProcA; override;');
  17007. Add(' end;');
  17008. Add(' TExtCClass = class of TExtC;');
  17009. Add('procedure TExtC.ProcA; begin end;');
  17010. Add('var');
  17011. Add(' A: texta; ClA: TExtAClass;');
  17012. Add(' B: textb; ClB: TExtBClass;');
  17013. Add(' C: textc; ClC: TExtCClass;');
  17014. Add('begin');
  17015. Add(' ClA:=texta;');
  17016. Add(' ClA:=textb;');
  17017. Add(' ClA:=textc;');
  17018. Add(' ClB:=textb;');
  17019. Add(' ClB:=textc;');
  17020. Add(' ClC:=textc;');
  17021. ConvertProgram;
  17022. CheckSource('TestExternalClass_ClassOf',
  17023. LinesToStr([ // statements
  17024. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17025. ' this.$init = function () {',
  17026. ' };',
  17027. ' this.$final = function () {',
  17028. ' };',
  17029. ' this.ProcA = function () {',
  17030. ' };',
  17031. '});',
  17032. 'this.A = null;',
  17033. 'this.ClA = null;',
  17034. 'this.B = null;',
  17035. 'this.ClB = null;',
  17036. 'this.C = null;',
  17037. 'this.ClC = null;',
  17038. '']),
  17039. LinesToStr([ // $mod.$main
  17040. '$mod.ClA = ExtA;',
  17041. '$mod.ClA = ExtB;',
  17042. '$mod.ClA = $mod.TExtC;',
  17043. '$mod.ClB = ExtB;',
  17044. '$mod.ClB = $mod.TExtC;',
  17045. '$mod.ClC = $mod.TExtC;',
  17046. '']));
  17047. end;
  17048. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17049. begin
  17050. AddModuleWithIntfImplSrc('unit2.pas',
  17051. LinesToStr([
  17052. '{$modeswitch externalclass}',
  17053. 'type',
  17054. ' TExtA = class external name ''ExtA''',
  17055. ' class var Id: longint;',
  17056. ' end;',
  17057. '']),
  17058. '');
  17059. StartUnit(true);
  17060. Add('interface');
  17061. Add('uses unit2;');
  17062. Add('implementation');
  17063. Add('begin');
  17064. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17065. ConvertUnit;
  17066. CheckSource('TestExternalClass_ClassOtherUnit',
  17067. LinesToStr([
  17068. '']),
  17069. LinesToStr([
  17070. 'ExtA.Id = ExtA.Id + 1;',
  17071. '']));
  17072. end;
  17073. procedure TTestModule.TestExternalClass_Is;
  17074. begin
  17075. StartProgram(false);
  17076. Add([
  17077. '{$modeswitch externalclass}',
  17078. 'type',
  17079. ' TExtA = class external name ''ExtA''',
  17080. ' end;',
  17081. ' TExtAClass = class of TExtA;',
  17082. ' TExtB = class external name ''ExtB'' (TExtA)',
  17083. ' end;',
  17084. ' TExtBClass = class of TExtB;',
  17085. ' TExtC = class (TExtB)',
  17086. ' end;',
  17087. ' TExtCClass = class of TExtC;',
  17088. 'var',
  17089. ' A: texta; ClA: TExtAClass;',
  17090. ' B: textb; ClB: TExtBClass;',
  17091. ' C: textc; ClC: TExtCClass;',
  17092. 'begin',
  17093. ' if a is textb then ;',
  17094. ' if a is textc then ;',
  17095. ' if b is textc then ;',
  17096. ' if cla is textb then ;',
  17097. ' if cla is textc then ;',
  17098. ' if clb is textc then ;',
  17099. ' try',
  17100. ' except',
  17101. ' on TExtA do ;',
  17102. ' on e: TExtB do ;',
  17103. ' end;',
  17104. '']);
  17105. ConvertProgram;
  17106. CheckSource('TestExternalClass_Is',
  17107. LinesToStr([ // statements
  17108. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17109. ' this.$init = function () {',
  17110. ' };',
  17111. ' this.$final = function () {',
  17112. ' };',
  17113. '});',
  17114. 'this.A = null;',
  17115. 'this.ClA = null;',
  17116. 'this.B = null;',
  17117. 'this.ClB = null;',
  17118. 'this.C = null;',
  17119. 'this.ClC = null;',
  17120. '']),
  17121. LinesToStr([ // $mod.$main
  17122. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17123. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17124. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17125. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17126. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17127. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17128. 'try {} catch ($e) {',
  17129. ' if (rtl.isExt($e,ExtA)) {}',
  17130. ' else if (rtl.isExt($e,ExtB)) {',
  17131. ' var e = $e;',
  17132. ' } else throw $e',
  17133. '};',
  17134. '']));
  17135. end;
  17136. procedure TTestModule.TestExternalClass_As;
  17137. begin
  17138. StartProgram(false);
  17139. Add('{$modeswitch externalclass}');
  17140. Add('type');
  17141. Add(' TExtA = class external name ''ExtA''');
  17142. Add(' end;');
  17143. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17144. Add(' end;');
  17145. Add(' TExtC = class (TExtB)');
  17146. Add(' end;');
  17147. Add('var');
  17148. Add(' A: texta;');
  17149. Add(' B: textb;');
  17150. Add(' C: textc;');
  17151. Add('begin');
  17152. Add(' b:=a as textb;');
  17153. Add(' c:=a as textc;');
  17154. Add(' c:=b as textc;');
  17155. ConvertProgram;
  17156. CheckSource('TestExternalClass_Is',
  17157. LinesToStr([ // statements
  17158. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17159. ' this.$init = function () {',
  17160. ' };',
  17161. ' this.$final = function () {',
  17162. ' };',
  17163. '});',
  17164. 'this.A = null;',
  17165. 'this.B = null;',
  17166. 'this.C = null;',
  17167. '']),
  17168. LinesToStr([ // $mod.$main
  17169. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17170. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17171. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17172. '']));
  17173. end;
  17174. procedure TTestModule.TestExternalClass_DestructorFail;
  17175. begin
  17176. StartProgram(false);
  17177. Add('{$modeswitch externalclass}');
  17178. Add('type');
  17179. Add(' TExtA = class external name ''ExtA''');
  17180. Add(' destructor Free;');
  17181. Add(' end;');
  17182. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17183. nPasElementNotSupported);
  17184. ConvertProgram;
  17185. end;
  17186. procedure TTestModule.TestExternalClass_New;
  17187. begin
  17188. StartProgram(false);
  17189. Add([
  17190. '{$modeswitch externalclass}',
  17191. 'type',
  17192. ' TExtA = class external name ''ExtA''',
  17193. ' constructor New;',
  17194. ' constructor New(i: longint; j: longint = 2);',
  17195. ' end;',
  17196. 'var',
  17197. ' A: texta;',
  17198. 'begin',
  17199. ' a:=texta.new;',
  17200. ' a:=texta(texta.new);',
  17201. ' a:=texta.new();',
  17202. ' a:=texta.new(1);',
  17203. ' with texta do begin',
  17204. ' a:=new;',
  17205. ' a:=new();',
  17206. ' a:=new(2);',
  17207. ' end;',
  17208. ' a:=test1.texta.new;',
  17209. ' a:=test1.texta.new();',
  17210. ' a:=test1.texta.new(3);',
  17211. '']);
  17212. ConvertProgram;
  17213. CheckSource('TestExternalClass_New',
  17214. LinesToStr([ // statements
  17215. 'this.A = null;',
  17216. '']),
  17217. LinesToStr([ // $mod.$main
  17218. '$mod.A = new ExtA();',
  17219. '$mod.A = new ExtA();',
  17220. '$mod.A = new ExtA();',
  17221. '$mod.A = new ExtA(1,2);',
  17222. '$mod.A = new ExtA();',
  17223. '$mod.A = new ExtA();',
  17224. '$mod.A = new ExtA(2,2);',
  17225. '$mod.A = new ExtA();',
  17226. '$mod.A = new ExtA();',
  17227. '$mod.A = new ExtA(3,2);',
  17228. '']));
  17229. end;
  17230. procedure TTestModule.TestExternalClass_ClassOf_New;
  17231. begin
  17232. StartProgram(false);
  17233. Add('{$modeswitch externalclass}');
  17234. Add('type');
  17235. Add(' TExtAClass = class of TExtA;');
  17236. Add(' TExtA = class external name ''ExtA''');
  17237. Add(' C: TExtAClass;');
  17238. Add(' constructor New;');
  17239. Add(' end;');
  17240. Add('var');
  17241. Add(' A: texta;');
  17242. Add(' C: textaclass;');
  17243. Add('begin');
  17244. Add(' a:=c.new;');
  17245. Add(' a:=c.new();');
  17246. Add(' with C do begin');
  17247. Add(' a:=new;');
  17248. Add(' a:=new();');
  17249. Add(' end;');
  17250. Add(' a:=test1.c.new;');
  17251. Add(' a:=test1.c.new();');
  17252. Add(' a:=A.c.new();');
  17253. ConvertProgram;
  17254. CheckSource('TestExternalClass_ClassOf_New',
  17255. LinesToStr([ // statements
  17256. 'this.A = null;',
  17257. 'this.C = null;',
  17258. '']),
  17259. LinesToStr([ // $mod.$main
  17260. '$mod.A = new $mod.C();',
  17261. '$mod.A = new $mod.C();',
  17262. 'var $with = $mod.C;',
  17263. '$mod.A = new $with();',
  17264. '$mod.A = new $with();',
  17265. '$mod.A = new $mod.C();',
  17266. '$mod.A = new $mod.C();',
  17267. '$mod.A = new $mod.A.C();',
  17268. '']));
  17269. end;
  17270. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17271. begin
  17272. StartProgram(false);
  17273. Add([
  17274. '{$modeswitch externalclass}',
  17275. 'type',
  17276. ' TExtAClass = class of TExtA;',
  17277. ' TExtA = class external name ''ExtA''',
  17278. ' constructor New;',
  17279. ' end;',
  17280. 'function GetCreator: TExtAClass;',
  17281. 'begin',
  17282. ' Result:=TExtA;',
  17283. 'end;',
  17284. 'var',
  17285. ' A: texta;',
  17286. 'begin',
  17287. ' a:=getcreator.new;',
  17288. ' a:=getcreator().new;',
  17289. ' a:=getcreator().new();',
  17290. ' a:=getcreator.new();',
  17291. ' with getcreator do begin',
  17292. ' a:=new;',
  17293. ' a:=new();',
  17294. ' end;']);
  17295. ConvertProgram;
  17296. CheckSource('TestExternalClass_FuncClassOf_New',
  17297. LinesToStr([ // statements
  17298. 'this.GetCreator = function () {',
  17299. ' var Result = null;',
  17300. ' Result = ExtA;',
  17301. ' return Result;',
  17302. '};',
  17303. 'this.A = null;',
  17304. '']),
  17305. LinesToStr([ // $mod.$main
  17306. '$mod.A = new ($mod.GetCreator())();',
  17307. '$mod.A = new ($mod.GetCreator())();',
  17308. '$mod.A = new ($mod.GetCreator())();',
  17309. '$mod.A = new ($mod.GetCreator())();',
  17310. 'var $with = $mod.GetCreator();',
  17311. '$mod.A = new $with();',
  17312. '$mod.A = new $with();',
  17313. '']));
  17314. end;
  17315. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17316. begin
  17317. StartProgram(false);
  17318. Add([
  17319. '{$modeswitch externalclass}',
  17320. 'type',
  17321. ' TExtA = class external name ''ExtA''',
  17322. ' constructor New;',
  17323. ' end;',
  17324. ' TBird = class(TExtA)',
  17325. ' end;',
  17326. 'begin',
  17327. ' TBird.new;',
  17328. '']);
  17329. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17330. ConvertProgram;
  17331. end;
  17332. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17333. begin
  17334. StartProgram(false);
  17335. Add([
  17336. '{$modeswitch externalclass}',
  17337. 'type',
  17338. ' TExtA = class external name ''ExtA''',
  17339. ' constructor New;',
  17340. ' end;',
  17341. ' TBird = class(TExtA)',
  17342. ' end;',
  17343. 'begin',
  17344. ' TBird.new();',
  17345. '']);
  17346. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17347. ConvertProgram;
  17348. end;
  17349. procedure TTestModule.TestExternalClass_NewExtName;
  17350. begin
  17351. StartProgram(false);
  17352. Add([
  17353. '{$modeswitch externalclass}',
  17354. 'type',
  17355. ' TExtA = class external name ''ExtA''',
  17356. ' constructor New; external name ''Other'';',
  17357. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17358. ' end;',
  17359. 'var',
  17360. ' A: texta;',
  17361. 'begin',
  17362. ' a:=texta.new;',
  17363. ' a:=texta(texta.new);',
  17364. ' a:=texta.new();',
  17365. ' a:=texta.new(1);',
  17366. ' with texta do begin',
  17367. ' a:=new;',
  17368. ' a:=new();',
  17369. ' a:=new(2);',
  17370. ' end;',
  17371. ' a:=test1.texta.new;',
  17372. ' a:=test1.texta.new();',
  17373. ' a:=test1.texta.new(3);',
  17374. '']);
  17375. ConvertProgram;
  17376. CheckSource('TestExternalClass_NewExtName',
  17377. LinesToStr([ // statements
  17378. 'this.A = null;',
  17379. '']),
  17380. LinesToStr([ // $mod.$main
  17381. '$mod.A = new Other();',
  17382. '$mod.A = new Other();',
  17383. '$mod.A = new Other();',
  17384. '$mod.A = new A.B(1,2);',
  17385. '$mod.A = new Other();',
  17386. '$mod.A = new Other();',
  17387. '$mod.A = new A.B(2,2);',
  17388. '$mod.A = new Other();',
  17389. '$mod.A = new Other();',
  17390. '$mod.A = new A.B(3,2);',
  17391. '']));
  17392. end;
  17393. procedure TTestModule.TestExternalClass_Constructor;
  17394. begin
  17395. StartProgram(false);
  17396. Add([
  17397. '{$modeswitch externalclass}',
  17398. 'type',
  17399. ' TExtA = class external name ''ExtA''',
  17400. ' constructor Create;',
  17401. ' constructor Create(i: longint; j: longint = 2);',
  17402. ' end;',
  17403. 'var',
  17404. ' A: texta;',
  17405. 'begin',
  17406. ' a:=texta.create;',
  17407. ' a:=texta(texta.create);',
  17408. ' a:=texta.create();',
  17409. ' a:=texta.create(1);',
  17410. ' with texta do begin',
  17411. ' a:=create;',
  17412. ' a:=create();',
  17413. ' a:=create(2);',
  17414. ' end;',
  17415. ' a:=test1.texta.create;',
  17416. ' a:=test1.texta.create();',
  17417. ' a:=test1.texta.create(3);',
  17418. '']);
  17419. ConvertProgram;
  17420. CheckSource('TestExternalClass_Constructor',
  17421. LinesToStr([ // statements
  17422. 'this.A = null;',
  17423. '']),
  17424. LinesToStr([ // $mod.$main
  17425. '$mod.A = new ExtA.Create();',
  17426. '$mod.A = new ExtA.Create();',
  17427. '$mod.A = new ExtA.Create();',
  17428. '$mod.A = new ExtA.Create(1,2);',
  17429. '$mod.A = new ExtA.Create();',
  17430. '$mod.A = new ExtA.Create();',
  17431. '$mod.A = new ExtA.Create(2,2);',
  17432. '$mod.A = new ExtA.Create();',
  17433. '$mod.A = new ExtA.Create();',
  17434. '$mod.A = new ExtA.Create(3,2);',
  17435. '']));
  17436. end;
  17437. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17438. begin
  17439. StartProgram(false);
  17440. Add([
  17441. '{$modeswitch externalclass}',
  17442. 'type',
  17443. ' TExtA = class external name ''ExtA''',
  17444. ' constructor Create; external name ''{}'';',
  17445. ' end;',
  17446. 'var',
  17447. ' A: texta;',
  17448. 'begin',
  17449. ' a:=texta.create;',
  17450. ' a:=texta(texta.create);',
  17451. ' a:=texta.create();',
  17452. ' with texta do begin',
  17453. ' a:=create;',
  17454. ' a:=create();',
  17455. ' end;',
  17456. ' a:=test1.texta.create;',
  17457. ' a:=test1.texta.create();',
  17458. '']);
  17459. ConvertProgram;
  17460. CheckSource('TestExternalClass_ConstructorBrackets',
  17461. LinesToStr([ // statements
  17462. 'this.A = null;',
  17463. '']),
  17464. LinesToStr([ // $mod.$main
  17465. '$mod.A = {};',
  17466. '$mod.A = {};',
  17467. '$mod.A = {};',
  17468. '$mod.A = {};',
  17469. '$mod.A = {};',
  17470. '$mod.A = {};',
  17471. '$mod.A = {};',
  17472. '']));
  17473. end;
  17474. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17475. begin
  17476. StartProgram(false);
  17477. Add('{$modeswitch externalclass}');
  17478. Add('type');
  17479. Add(' TExtA = class external name ''ExtA''');
  17480. Add(' constructor New;');
  17481. Add(' end;');
  17482. Add('function DoIt: longint;');
  17483. Add('const ExtA: longint = 3;');
  17484. Add('begin');
  17485. Add(' Result:=ExtA;');
  17486. Add('end;');
  17487. Add('var');
  17488. Add(' A: texta;');
  17489. Add('begin');
  17490. Add(' a:=texta.new;');
  17491. ConvertProgram;
  17492. CheckSource('TestExternalClass_LocalConstSameName',
  17493. LinesToStr([ // statements
  17494. 'var ExtA$1 = 3;',
  17495. 'this.DoIt = function () {',
  17496. ' var Result = 0;',
  17497. ' Result = ExtA$1;',
  17498. ' return Result;',
  17499. '};',
  17500. 'this.A = null;',
  17501. '']),
  17502. LinesToStr([ // $mod.$main
  17503. '$mod.A = new ExtA();',
  17504. '']));
  17505. end;
  17506. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17507. begin
  17508. StartProgram(false);
  17509. Add('{$modeswitch externalclass}');
  17510. Add('type');
  17511. Add(' TExtA = class external name ''ExtA''');
  17512. Add(' procedure DoIt;');
  17513. Add(' end;');
  17514. Add(' TMyA = class(TExtA)');
  17515. Add(' procedure DoIt;');
  17516. Add(' end;');
  17517. Add('procedure TMyA.DoIt; begin end;');
  17518. Add('begin');
  17519. ConvertProgram;
  17520. CheckSource('TestExternalClass_ReintroduceOverload',
  17521. LinesToStr([ // statements
  17522. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17523. ' this.$init = function () {',
  17524. ' };',
  17525. ' this.$final = function () {',
  17526. ' };',
  17527. ' this.DoIt$1 = function () {',
  17528. ' };',
  17529. '});',
  17530. '']),
  17531. LinesToStr([ // $mod.$main
  17532. '']));
  17533. end;
  17534. procedure TTestModule.TestExternalClass_Inherited;
  17535. begin
  17536. StartProgram(false);
  17537. Add('{$modeswitch externalclass}');
  17538. Add('type');
  17539. Add(' TExtA = class external name ''ExtA''');
  17540. Add(' procedure DoIt(i: longint = 1); virtual;');
  17541. Add(' procedure DoSome(j: longint = 2);');
  17542. Add(' end;');
  17543. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17544. Add(' end;');
  17545. Add(' TMyC = class(TExtB)');
  17546. Add(' procedure DoIt(i: longint = 1); override;');
  17547. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17548. Add(' end;');
  17549. Add('procedure TMyC.DoIt(i: longint);');
  17550. Add('begin');
  17551. Add(' inherited;');
  17552. Add(' inherited DoIt;');
  17553. Add(' inherited DoIt();');
  17554. Add(' inherited DoIt(3);');
  17555. Add(' inherited DoSome;');
  17556. Add(' inherited DoSome();');
  17557. Add(' inherited DoSome(4);');
  17558. Add('end;');
  17559. Add('procedure TMyC.DoSome(j: longint);');
  17560. Add('begin');
  17561. Add(' inherited;');
  17562. Add('end;');
  17563. Add('begin');
  17564. ConvertProgram;
  17565. CheckSource('TestExternalClass_ReintroduceOverload',
  17566. LinesToStr([ // statements
  17567. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17568. ' this.$init = function () {',
  17569. ' };',
  17570. ' this.$final = function () {',
  17571. ' };',
  17572. ' this.DoIt = function (i) {',
  17573. ' ExtB.DoIt.apply(this, arguments);',
  17574. ' ExtB.DoIt.call(this, 1);',
  17575. ' ExtB.DoIt.call(this, 1);',
  17576. ' ExtB.DoIt.call(this, 3);',
  17577. ' ExtB.DoSome.call(this, 2);',
  17578. ' ExtB.DoSome.call(this, 2);',
  17579. ' ExtB.DoSome.call(this, 4);',
  17580. ' };',
  17581. ' this.DoSome$1 = function (j) {',
  17582. ' ExtB.DoSome.apply(this, arguments);',
  17583. ' };',
  17584. '});',
  17585. '']),
  17586. LinesToStr([ // $mod.$main
  17587. '']));
  17588. end;
  17589. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17590. begin
  17591. StartProgram(false);
  17592. Add('{$modeswitch externalclass}');
  17593. Add('type');
  17594. Add(' TObject = class');
  17595. Add(' end;');
  17596. Add(' TExtA = class external name ''ExtA''(TObject)');
  17597. Add(' end;');
  17598. Add('begin');
  17599. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17600. ConvertProgram;
  17601. end;
  17602. procedure TTestModule.TestExternalClass_NewInstance;
  17603. begin
  17604. StartProgram(false);
  17605. Add('{$modeswitch externalclass}');
  17606. Add('type');
  17607. Add(' TExtA = class external name ''ExtA''');
  17608. Add(' end;');
  17609. Add(' TMyB = class(TExtA)');
  17610. Add(' protected');
  17611. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17612. Add(' end;');
  17613. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17614. Add('begin end;');
  17615. Add('begin');
  17616. ConvertProgram;
  17617. CheckSource('TestExternalClass_NewInstance',
  17618. LinesToStr([ // statements
  17619. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17620. ' this.$init = function () {',
  17621. ' };',
  17622. ' this.$final = function () {',
  17623. ' };',
  17624. ' this.NewInstance = function (fnname, paramarray) {',
  17625. ' var Result = null;',
  17626. ' return Result;',
  17627. ' };',
  17628. '});',
  17629. '']),
  17630. LinesToStr([ // $mod.$main
  17631. '']));
  17632. end;
  17633. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17634. begin
  17635. StartProgram(false);
  17636. Add('{$modeswitch externalclass}');
  17637. Add('type');
  17638. Add(' TExtA = class external name ''ExtA''');
  17639. Add(' end;');
  17640. Add(' TMyB = class(TExtA)');
  17641. Add(' protected');
  17642. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17643. Add(' end;');
  17644. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17645. Add('begin end;');
  17646. Add('begin');
  17647. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17648. ConvertProgram;
  17649. end;
  17650. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17651. begin
  17652. StartProgram(false);
  17653. Add('{$modeswitch externalclass}');
  17654. Add('type');
  17655. Add(' TExtA = class external name ''ExtA''');
  17656. Add(' end;');
  17657. Add(' TMyB = class(TExtA)');
  17658. Add(' protected');
  17659. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17660. Add(' end;');
  17661. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17662. Add('begin end;');
  17663. Add('begin');
  17664. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17665. nIncompatibleTypeArgNo);
  17666. ConvertProgram;
  17667. end;
  17668. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17669. begin
  17670. StartProgram(false);
  17671. Add('{$modeswitch externalclass}');
  17672. Add('type');
  17673. Add(' TExtA = class external name ''ExtA''');
  17674. Add(' end;');
  17675. Add(' TMyB = class(TExtA)');
  17676. Add(' protected');
  17677. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17678. Add(' end;');
  17679. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17680. Add('begin end;');
  17681. Add('begin');
  17682. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17683. nIncompatibleTypeArgNo);
  17684. ConvertProgram;
  17685. end;
  17686. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17687. begin
  17688. StartProgram(false);
  17689. Add([
  17690. '{$modeswitch externalclass}',
  17691. 'type',
  17692. ' TJSFunction = class external name ''Function''',
  17693. ' end;',
  17694. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17695. ' constructor New(w: word);',
  17696. ' end;',
  17697. ' TBird = class (TExtA)',
  17698. ' public',
  17699. ' Size: word;',
  17700. ' class var Legs: word;',
  17701. ' constructor Create(a: word);',
  17702. ' end;',
  17703. ' TEagle = class (TBird)',
  17704. ' public',
  17705. ' constructor Create(b: word); reintroduce;',
  17706. ' end;',
  17707. 'constructor TBird.Create(a: word);',
  17708. 'begin',
  17709. ' inherited;', // silently ignored
  17710. ' inherited New(a);', // this.$func(a)
  17711. 'end;',
  17712. 'constructor TEagle.Create(b: word);',
  17713. 'begin',
  17714. ' inherited Create(b);',
  17715. 'end;',
  17716. 'var',
  17717. ' Bird: TBird;',
  17718. ' Eagle: TEagle;',
  17719. 'begin',
  17720. ' Bird:=TBird.Create(3);',
  17721. ' Eagle:=TEagle.Create(4);',
  17722. ' Bird.Size:=Bird.Size+5;',
  17723. ' Bird.Legs:=Bird.Legs+6;',
  17724. ' Eagle.Size:=Eagle.Size+5;',
  17725. ' Eagle.Legs:=Eagle.Legs+6;',
  17726. '']);
  17727. ConvertProgram;
  17728. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17729. LinesToStr([ // statements
  17730. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17731. ' this.Legs = 0;',
  17732. ' this.$init = function () {',
  17733. ' this.Size = 0;',
  17734. ' };',
  17735. ' this.$final = function () {',
  17736. ' };',
  17737. ' this.Create = function (a) {',
  17738. ' this.$ancestorfunc(a);',
  17739. ' return this;',
  17740. ' };',
  17741. '});',
  17742. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17743. ' this.Create$1 = function (b) {',
  17744. ' $mod.TBird.Create.call(this, b);',
  17745. ' return this;',
  17746. ' };',
  17747. '});',
  17748. 'this.Bird = null;',
  17749. 'this.Eagle = null;',
  17750. '']),
  17751. LinesToStr([ // $mod.$main
  17752. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17753. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17754. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17755. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17756. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17757. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17758. '']));
  17759. end;
  17760. procedure TTestModule.TestExternalClass_PascalProperty;
  17761. begin
  17762. StartProgram(false);
  17763. Add('{$modeswitch externalclass}');
  17764. Add('type');
  17765. Add(' TJSElement = class;');
  17766. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17767. Add(' TJSElement = class external name ''ExtA''');
  17768. Add(' end;');
  17769. Add(' TControl = class(TJSElement)');
  17770. Add(' private');
  17771. Add(' FOnClick: TJSNotifyEvent;');
  17772. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17773. Add(' procedure Click(Sender: TJSElement);');
  17774. Add(' end;');
  17775. Add('procedure TControl.Click(Sender: TJSElement);');
  17776. Add('begin');
  17777. Add(' OnClick(Self);');
  17778. Add('end;');
  17779. Add('var');
  17780. Add(' Ctrl: TControl;');
  17781. Add('begin');
  17782. Add(' Ctrl.OnClick:[email protected];');
  17783. Add(' Ctrl.OnClick(Ctrl);');
  17784. ConvertProgram;
  17785. CheckSource('TestExternalClass_PascalProperty',
  17786. LinesToStr([ // statements
  17787. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17788. ' this.$init = function () {',
  17789. ' this.FOnClick = null;',
  17790. ' };',
  17791. ' this.$final = function () {',
  17792. ' this.FOnClick = undefined;',
  17793. ' };',
  17794. ' this.Click = function (Sender) {',
  17795. ' this.FOnClick(this);',
  17796. ' };',
  17797. '});',
  17798. 'this.Ctrl = null;',
  17799. '']),
  17800. LinesToStr([ // $mod.$main
  17801. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17802. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17803. '']));
  17804. end;
  17805. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17806. begin
  17807. StartProgram(false);
  17808. Add([
  17809. '{$modeswitch externalclass}',
  17810. 'type',
  17811. ' IUnknown = interface end;',
  17812. ' TObject = class',
  17813. ' end;',
  17814. ' TChild = class',
  17815. ' end;',
  17816. ' TExtRootA = class external name ''ExtRootA''',
  17817. ' end;',
  17818. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17819. ' end;',
  17820. ' TExtRootB = class external name ''ExtRootB''',
  17821. ' end;',
  17822. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17823. ' end;',
  17824. 'var',
  17825. ' Obj: TObject;',
  17826. ' Child: TChild;',
  17827. ' RootA: TExtRootA;',
  17828. ' ChildA: TExtChildA;',
  17829. ' RootB: TExtRootB;',
  17830. ' ChildB: TExtChildB;',
  17831. ' i: IUnknown;',
  17832. 'begin',
  17833. ' obj:=tobject(roota);',
  17834. ' obj:=tobject(childa);',
  17835. ' child:=tchild(tobject(roota));',
  17836. ' roota:=textroota(obj);',
  17837. ' roota:=textroota(child);',
  17838. ' roota:=textroota(rootb);',
  17839. ' roota:=textroota(childb);',
  17840. ' childa:=textchilda(textroota(obj));',
  17841. ' roota:=TExtRootA(i)',
  17842. '']);
  17843. ConvertProgram;
  17844. CheckSource('TestExternalClass_TypeCastToRootClass',
  17845. LinesToStr([ // statements
  17846. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17847. 'rtl.createClass(this, "TObject", null, function () {',
  17848. ' this.$init = function () {',
  17849. ' };',
  17850. ' this.$final = function () {',
  17851. ' };',
  17852. '});',
  17853. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17854. '});',
  17855. 'this.Obj = null;',
  17856. 'this.Child = null;',
  17857. 'this.RootA = null;',
  17858. 'this.ChildA = null;',
  17859. 'this.RootB = null;',
  17860. 'this.ChildB = null;',
  17861. 'this.i = null;',
  17862. '']),
  17863. LinesToStr([ // $mod.$main
  17864. '$mod.Obj = $mod.RootA;',
  17865. '$mod.Obj = $mod.ChildA;',
  17866. '$mod.Child = $mod.RootA;',
  17867. '$mod.RootA = $mod.Obj;',
  17868. '$mod.RootA = $mod.Child;',
  17869. '$mod.RootA = $mod.RootB;',
  17870. '$mod.RootA = $mod.ChildB;',
  17871. '$mod.ChildA = $mod.Obj;',
  17872. '$mod.RootA = $mod.i;',
  17873. '']));
  17874. end;
  17875. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17876. begin
  17877. StartProgram(false);
  17878. Add([
  17879. '{$modeswitch externalclass}',
  17880. 'type',
  17881. ' IUnknown = interface end;',
  17882. ' IBird = interface(IUnknown) end;',
  17883. ' TClass = class of TObject;',
  17884. ' TObject = class',
  17885. ' end;',
  17886. ' TChild = class',
  17887. ' end;',
  17888. ' TJSObject = class external name ''Object''',
  17889. ' end;',
  17890. ' TRec = record end;',
  17891. 'var',
  17892. ' Obj: TObject;',
  17893. ' Child: TChild;',
  17894. ' i: IUnknown;',
  17895. ' Bird: IBird;',
  17896. ' j: TJSObject;',
  17897. ' r: TRec;',
  17898. ' c: TClass;',
  17899. 'begin',
  17900. ' j:=tjsobject(IUnknown);',
  17901. ' j:=tjsobject(IBird);',
  17902. ' j:=tjsobject(TObject);',
  17903. ' j:=tjsobject(TChild);',
  17904. ' j:=tjsobject(TRec);',
  17905. ' j:=tjsobject(Obj);',
  17906. ' j:=tjsobject(Child);',
  17907. ' j:=tjsobject(i);',
  17908. ' j:=tjsobject(Bird);',
  17909. ' j:=tjsobject(r);',
  17910. ' j:=tjsobject(c);',
  17911. '']);
  17912. ConvertProgram;
  17913. CheckSource('TestExternalClass_TypeCastToJSObject',
  17914. LinesToStr([ // statements
  17915. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17916. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17917. 'rtl.createClass(this, "TObject", null, function () {',
  17918. ' this.$init = function () {',
  17919. ' };',
  17920. ' this.$final = function () {',
  17921. ' };',
  17922. '});',
  17923. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17924. '});',
  17925. 'rtl.recNewT(this, "TRec", function () {',
  17926. ' this.$eq = function (b) {',
  17927. ' return true;',
  17928. ' };',
  17929. ' this.$assign = function (s) {',
  17930. ' return this;',
  17931. ' };',
  17932. '});',
  17933. 'this.Obj = null;',
  17934. 'this.Child = null;',
  17935. 'this.i = null;',
  17936. 'this.Bird = null;',
  17937. 'this.j = null;',
  17938. 'this.r = this.TRec.$new();',
  17939. 'this.c = null;',
  17940. '']),
  17941. LinesToStr([ // $mod.$main
  17942. '$mod.j = $mod.IUnknown;',
  17943. '$mod.j = $mod.IBird;',
  17944. '$mod.j = $mod.TObject;',
  17945. '$mod.j = $mod.TChild;',
  17946. '$mod.j = $mod.TRec;',
  17947. '$mod.j = $mod.Obj;',
  17948. '$mod.j = $mod.Child;',
  17949. '$mod.j = $mod.i;',
  17950. '$mod.j = $mod.Bird;',
  17951. '$mod.j = $mod.r;',
  17952. '$mod.j = $mod.c;',
  17953. '']));
  17954. end;
  17955. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17956. begin
  17957. StartProgram(false);
  17958. Add('{$modeswitch externalclass}');
  17959. Add('type');
  17960. Add(' TJSString = class external name ''String''');
  17961. Add(' class function fromCharCode() : string; varargs;');
  17962. Add(' function anchor(const aName : string) : string;');
  17963. Add(' end;');
  17964. Add('var');
  17965. Add(' s: string;');
  17966. Add('begin');
  17967. Add(' s:=TJSString.fromCharCode(65,66);');
  17968. Add(' s:=TJSString(s).anchor(s);');
  17969. Add(' s:=TJSString(''foo'').anchor(s);');
  17970. ConvertProgram;
  17971. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17972. LinesToStr([ // statements
  17973. 'this.s = "";',
  17974. '']),
  17975. LinesToStr([ // $mod.$main
  17976. '$mod.s = String.fromCharCode(65, 66);',
  17977. '$mod.s = $mod.s.anchor($mod.s);',
  17978. '$mod.s = "foo".anchor($mod.s);',
  17979. '']));
  17980. end;
  17981. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17982. begin
  17983. StartProgram(false);
  17984. Add([
  17985. '{$modeswitch externalclass}',
  17986. 'type',
  17987. ' TJSObject = class external name ''Object'' end;',
  17988. ' TJSFunction = class external name ''Function''',
  17989. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17990. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17991. ' end;',
  17992. ' TObject = class',
  17993. ' procedure DoIt(i: longint);',
  17994. ' end;',
  17995. ' TFuncInt = function(o: TObject): longint;',
  17996. 'function GetIt(o: TObject): longint;',
  17997. ' procedure Sub; begin end;',
  17998. 'var',
  17999. ' f: TJSFunction;',
  18000. ' fi: TFuncInt;',
  18001. 'begin',
  18002. ' fi:=TFuncInt(f);',
  18003. ' f:=TJSFunction(fi);',
  18004. ' f:=TJSFunction(@GetIt);',
  18005. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18006. ' f:=TJSFunction(@Sub);',
  18007. ' f:=TJSFunction(@o.doit);',
  18008. ' f:=TJSFunction(fi).bind(nil,4)',
  18009. 'end;',
  18010. 'procedure TObject.DoIt(i: longint);',
  18011. ' procedure Sub; begin end;',
  18012. 'var f: TJSFunction;',
  18013. 'begin',
  18014. ' f:=TJSFunction(@DoIt);',
  18015. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18016. ' f:=TJSFunction(@Sub);',
  18017. ' f:=TJSFunction(@GetIt);',
  18018. 'end;',
  18019. 'begin']);
  18020. ConvertProgram;
  18021. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18022. LinesToStr([ // statements
  18023. 'rtl.createClass(this, "TObject", null, function () {',
  18024. ' this.$init = function () {',
  18025. ' };',
  18026. ' this.$final = function () {',
  18027. ' };',
  18028. ' this.DoIt = function (i) {',
  18029. ' var $Self = this;',
  18030. ' function Sub() {',
  18031. ' };',
  18032. ' var f = null;',
  18033. ' f = this.DoIt;',
  18034. ' f = this.DoIt.bind(null, 13);',
  18035. ' f = Sub;',
  18036. ' f = $mod.GetIt;',
  18037. ' };',
  18038. '});',
  18039. 'this.GetIt = function (o) {',
  18040. ' var Result = 0;',
  18041. ' function Sub() {',
  18042. ' };',
  18043. ' var f = null;',
  18044. ' var fi = null;',
  18045. ' fi = f;',
  18046. ' f = fi;',
  18047. ' f = $mod.GetIt;',
  18048. ' f = $mod.GetIt.bind(null, 3);',
  18049. ' f = Sub;',
  18050. ' f = $mod.TObject.DoIt;',
  18051. ' f = fi.bind(null, 4);',
  18052. ' return Result;',
  18053. '};',
  18054. '']),
  18055. LinesToStr([ // $mod.$main
  18056. '']));
  18057. end;
  18058. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18059. begin
  18060. StartProgram(false);
  18061. Add([
  18062. '{$mode delphi}',
  18063. '{$modeswitch externalclass}',
  18064. 'type',
  18065. ' TJSObject = class external name ''Object'' end;',
  18066. ' TJSWindow = class external name ''Window''(TJSObject)',
  18067. ' procedure Open;',
  18068. ' end;',
  18069. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18070. ' procedure Execute;',
  18071. ' end;',
  18072. 'procedure Fly;',
  18073. 'var',
  18074. ' w: TJSWindow;',
  18075. ' e: TJSEventTarget;',
  18076. 'begin',
  18077. ' w:=TJSWindow(e);',
  18078. ' e:=TJSEventTarget(w);',
  18079. 'end;',
  18080. 'begin']);
  18081. ConvertProgram;
  18082. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18083. LinesToStr([ // statements
  18084. 'this.Fly = function () {',
  18085. ' var w = null;',
  18086. ' var e = null;',
  18087. ' w = e;',
  18088. ' e = w;',
  18089. '};',
  18090. '']),
  18091. LinesToStr([ // $mod.$main
  18092. '']));
  18093. end;
  18094. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18095. begin
  18096. StartProgram(false);
  18097. Add('{$modeswitch externalclass}');
  18098. Add('type');
  18099. Add(' TJSString = class external name ''String''');
  18100. Add(' class function fromCharCode() : string; varargs;');
  18101. Add(' end;');
  18102. Add('var');
  18103. Add(' s: string;');
  18104. Add(' sObj: TJSString;');
  18105. Add('begin');
  18106. Add(' s:=sObj.fromCharCode(65,66);');
  18107. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18108. nExternalClassInstanceCannotAccessStaticX);
  18109. ConvertProgram;
  18110. end;
  18111. procedure TTestModule.TestExternalClass_BracketAccessor;
  18112. begin
  18113. StartProgram(false);
  18114. Add([
  18115. '{$modeswitch externalclass}',
  18116. 'type',
  18117. ' TJSArray = class external name ''Array2''',
  18118. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18119. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18120. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18121. ' end;',
  18122. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18123. 'begin end;',
  18124. 'var',
  18125. ' Arr: tjsarray;',
  18126. ' s: string;',
  18127. ' i: longint;',
  18128. ' v: jsvalue;',
  18129. 'begin',
  18130. ' v:=arr[0];',
  18131. ' v:=arr.items[1];',
  18132. ' arr[2]:=s;',
  18133. ' arr.items[3]:=s;',
  18134. ' arr[4]:=i;',
  18135. ' arr[5]:=arr[6];',
  18136. ' arr.items[7]:=arr.items[8];',
  18137. ' with arr do items[9]:=items[10];',
  18138. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18139. ' with arr do begin',
  18140. ' v:=GetItems(14);',
  18141. ' setitems(15,16);',
  18142. ' end;',
  18143. ' v:=test1.arr.items[17];',
  18144. ' test1.arr.items[18]:=v;',
  18145. '']);
  18146. ConvertProgram;
  18147. CheckSource('TestExternalClass_BracketAccessor',
  18148. LinesToStr([ // statements
  18149. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18150. '};',
  18151. 'this.Arr = null;',
  18152. 'this.s = "";',
  18153. 'this.i = 0;',
  18154. 'this.v = undefined;',
  18155. '']),
  18156. LinesToStr([ // $mod.$main
  18157. '$mod.v = $mod.Arr[0];',
  18158. '$mod.v = $mod.Arr[1];',
  18159. '$mod.Arr[2] = $mod.s;',
  18160. '$mod.Arr[3] = $mod.s;',
  18161. '$mod.Arr[4] = $mod.i;',
  18162. '$mod.Arr[5] = $mod.Arr[6];',
  18163. '$mod.Arr[7] = $mod.Arr[8];',
  18164. 'var $with = $mod.Arr;',
  18165. '$with[9] = $with[10];',
  18166. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18167. ' a: 9,',
  18168. ' p: $mod.Arr,',
  18169. ' get: function () {',
  18170. ' return this.p[this.a];',
  18171. ' },',
  18172. ' set: function (v) {',
  18173. ' this.p[this.a] = v;',
  18174. ' }',
  18175. '}, {',
  18176. ' a: 10,',
  18177. ' p: $mod.Arr,',
  18178. ' get: function () {',
  18179. ' return this.p[this.a];',
  18180. ' },',
  18181. ' set: function (v) {',
  18182. ' this.p[this.a] = v;',
  18183. ' }',
  18184. '});',
  18185. 'var $with1 = $mod.Arr;',
  18186. '$mod.v = $with1[14];',
  18187. '$with1[15] = 16;',
  18188. '$mod.v = $mod.Arr[17];',
  18189. '$mod.Arr[18] = $mod.v;',
  18190. '']));
  18191. end;
  18192. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18193. begin
  18194. StartProgram(false);
  18195. Add([
  18196. '{$modeswitch externalclass}',
  18197. 'type',
  18198. ' TJSArray = class external name ''Array2''',
  18199. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18200. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18201. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18202. ' end;',
  18203. ' TMyArr = class(TJSArray)',
  18204. ' procedure DoIt;',
  18205. ' end;',
  18206. 'procedure tmyarr.DoIt;',
  18207. 'begin',
  18208. ' Items[1]:=Items[2];',
  18209. ' SetItems(3,getItems(4));',
  18210. 'end;',
  18211. 'var',
  18212. ' Arr: tmyarr;',
  18213. ' s: string;',
  18214. ' i: longint;',
  18215. ' v: jsvalue;',
  18216. 'begin',
  18217. ' v:=arr[0];',
  18218. ' v:=arr.items[1];',
  18219. ' arr[2]:=s;',
  18220. ' arr.items[3]:=s;',
  18221. ' arr[4]:=i;',
  18222. ' arr[5]:=arr[6];',
  18223. ' arr.items[7]:=arr.items[8];',
  18224. ' with arr do items[9]:=items[10];',
  18225. ' with arr do begin',
  18226. ' v:=GetItems(14);',
  18227. ' setitems(15,16);',
  18228. ' end;',
  18229. '']);
  18230. ConvertProgram;
  18231. CheckSource('TestExternalClass_BracketAccessor_Call',
  18232. LinesToStr([ // statements
  18233. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18234. ' this.$init = function () {',
  18235. ' };',
  18236. ' this.$final = function () {',
  18237. ' };',
  18238. ' this.DoIt = function () {',
  18239. ' this[1] = this[2];',
  18240. ' this[3] = this[4];',
  18241. ' };',
  18242. '});',
  18243. 'this.Arr = null;',
  18244. 'this.s = "";',
  18245. 'this.i = 0;',
  18246. 'this.v = undefined;',
  18247. '']),
  18248. LinesToStr([ // $mod.$main
  18249. '$mod.v = $mod.Arr[0];',
  18250. '$mod.v = $mod.Arr[1];',
  18251. '$mod.Arr[2] = $mod.s;',
  18252. '$mod.Arr[3] = $mod.s;',
  18253. '$mod.Arr[4] = $mod.i;',
  18254. '$mod.Arr[5] = $mod.Arr[6];',
  18255. '$mod.Arr[7] = $mod.Arr[8];',
  18256. 'var $with = $mod.Arr;',
  18257. '$with[9] = $with[10];',
  18258. 'var $with1 = $mod.Arr;',
  18259. '$mod.v = $with1[14];',
  18260. '$with1[15] = 16;',
  18261. '']));
  18262. end;
  18263. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18264. begin
  18265. StartProgram(false);
  18266. Add('{$modeswitch externalclass}');
  18267. Add('type');
  18268. Add(' TJSArray = class external name ''Array2''');
  18269. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18270. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18271. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18272. Add(' end;');
  18273. Add('begin');
  18274. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18275. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18276. ConvertProgram;
  18277. end;
  18278. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18279. begin
  18280. StartProgram(false);
  18281. Add('{$modeswitch externalclass}');
  18282. Add('type');
  18283. Add(' TJSArray = class external name ''Array2''');
  18284. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18285. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18286. Add(' end;');
  18287. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18288. Add('begin end;');
  18289. Add('var');
  18290. Add(' Arr: tjsarray;');
  18291. Add(' v: jsvalue;');
  18292. Add('begin');
  18293. Add(' v:=arr[0];');
  18294. Add(' v:=arr.items[1];');
  18295. Add(' with arr do v:=items[2];');
  18296. Add(' doit(arr[3],arr[4]);');
  18297. ConvertProgram;
  18298. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18299. LinesToStr([ // statements
  18300. 'this.DoIt = function (vI, vJ) {',
  18301. '};',
  18302. 'this.Arr = null;',
  18303. 'this.v = undefined;',
  18304. '']),
  18305. LinesToStr([ // $mod.$main
  18306. '$mod.v = $mod.Arr[0];',
  18307. '$mod.v = $mod.Arr[1];',
  18308. 'var $with = $mod.Arr;',
  18309. '$mod.v = $with[2];',
  18310. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18311. '']));
  18312. end;
  18313. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18314. begin
  18315. StartProgram(false);
  18316. Add('{$modeswitch externalclass}');
  18317. Add('type');
  18318. Add(' TJSArray = class external name ''Array2''');
  18319. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18320. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18321. Add(' end;');
  18322. Add('var');
  18323. Add(' Arr: tjsarray;');
  18324. Add(' s: string;');
  18325. Add(' i: longint;');
  18326. Add(' v: jsvalue;');
  18327. Add('begin');
  18328. Add(' arr[2]:=s;');
  18329. Add(' arr.items[3]:=s;');
  18330. Add(' arr[4]:=i;');
  18331. Add(' with arr do items[5]:=i;');
  18332. ConvertProgram;
  18333. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18334. LinesToStr([ // statements
  18335. 'this.Arr = null;',
  18336. 'this.s = "";',
  18337. 'this.i = 0;',
  18338. 'this.v = undefined;',
  18339. '']),
  18340. LinesToStr([ // $mod.$main
  18341. '$mod.Arr[2] = $mod.s;',
  18342. '$mod.Arr[3] = $mod.s;',
  18343. '$mod.Arr[4] = $mod.i;',
  18344. 'var $with = $mod.Arr;',
  18345. '$with[5] = $mod.i;',
  18346. '']));
  18347. end;
  18348. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18349. begin
  18350. StartProgram(false);
  18351. Add('{$modeswitch externalclass}');
  18352. Add('type');
  18353. Add(' TJSArray = class external name ''Array2''');
  18354. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18355. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18356. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18357. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18358. Add(' end;');
  18359. Add('var');
  18360. Add(' Arr: tjsarray;');
  18361. Add(' s: string;');
  18362. Add(' i: longint;');
  18363. Add(' v: jsvalue;');
  18364. Add('begin');
  18365. Add(' arr[2]:=s;');
  18366. Add(' arr.items[3]:=s;');
  18367. Add(' arr.numbers[4]:=i;');
  18368. Add(' with arr do items[5]:=i;');
  18369. Add(' with arr do numbers[6]:=i;');
  18370. ConvertProgram;
  18371. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18372. LinesToStr([ // statements
  18373. 'this.Arr = null;',
  18374. 'this.s = "";',
  18375. 'this.i = 0;',
  18376. 'this.v = undefined;',
  18377. '']),
  18378. LinesToStr([ // $mod.$main
  18379. '$mod.Arr[2] = $mod.s;',
  18380. '$mod.Arr[3] = $mod.s;',
  18381. '$mod.Arr[4] = $mod.i;',
  18382. 'var $with = $mod.Arr;',
  18383. '$with[5] = $mod.i;',
  18384. 'var $with1 = $mod.Arr;',
  18385. '$with1[6] = $mod.i;',
  18386. '']));
  18387. end;
  18388. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18389. begin
  18390. StartProgram(false);
  18391. Add('{$modeswitch externalclass}');
  18392. Add('type');
  18393. Add(' TJSArray = class external name ''Array2''');
  18394. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18395. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18396. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18397. Add(' end;');
  18398. Add('var');
  18399. Add(' Arr: tjsarray;');
  18400. Add(' i: longint;');
  18401. Add(' IntArr: array of longint;');
  18402. Add(' v: jsvalue;');
  18403. Add('begin');
  18404. Add(' v:=arr.items[i];');
  18405. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18406. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18407. ConvertProgram;
  18408. CheckSource('TestExternalClass_BracketAccessor_Index',
  18409. LinesToStr([ // statements
  18410. 'this.Arr = null;',
  18411. 'this.i = 0;',
  18412. 'this.IntArr = [];',
  18413. 'this.v = undefined;',
  18414. '']),
  18415. LinesToStr([ // $mod.$main
  18416. '$mod.v = $mod.Arr[$mod.i];',
  18417. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18418. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18419. '']));
  18420. end;
  18421. procedure TTestModule.TestExternalClass_ForInJSObject;
  18422. begin
  18423. StartProgram(false);
  18424. Add([
  18425. '{$modeswitch externalclass}',
  18426. 'type',
  18427. ' TJSObject = class external name ''Object''',
  18428. ' end;',
  18429. 'var',
  18430. ' o: TJSObject;',
  18431. ' key: string;',
  18432. 'begin',
  18433. ' for key in o do',
  18434. ' if key=''abc'' then ;',
  18435. '']);
  18436. ConvertProgram;
  18437. CheckSource('TestExternalClass_ForInJSObject',
  18438. LinesToStr([ // statements
  18439. 'this.o = null;',
  18440. 'this.key = "";',
  18441. '']),
  18442. LinesToStr([ // $mod.$main
  18443. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18444. '']));
  18445. end;
  18446. procedure TTestModule.TestExternalClass_ForInJSArray;
  18447. begin
  18448. StartProgram(false);
  18449. Add([
  18450. '{$modeswitch externalclass}',
  18451. 'type',
  18452. ' TJSInt8Array = class external name ''Int8Array''',
  18453. ' private',
  18454. ' flength: NativeInt external name ''length'';',
  18455. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18456. ' public',
  18457. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18458. ' property Length: NativeInt read flength;',
  18459. ' end;',
  18460. 'var',
  18461. ' a: TJSInt8Array;',
  18462. ' value: shortint;',
  18463. 'begin',
  18464. ' for value in a do',
  18465. ' if value=3 then ;',
  18466. '']);
  18467. ConvertProgram;
  18468. CheckSource('TestExternalClass_ForInJSArray',
  18469. LinesToStr([ // statements
  18470. 'this.a = null;',
  18471. 'this.value = 0;',
  18472. '']),
  18473. LinesToStr([ // $mod.$main
  18474. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18475. ' $mod.value = $in[$l];',
  18476. ' if ($mod.value === 3) ;',
  18477. '};',
  18478. '']));
  18479. end;
  18480. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18481. begin
  18482. AddModuleWithIntfImplSrc('unit2.pas',
  18483. LinesToStr([
  18484. '{$modeswitch externalclass}',
  18485. 'type',
  18486. ' TJSBufferSource = class external name ''BufferSource''',
  18487. ' end;',
  18488. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18489. '']),
  18490. '');
  18491. AddModuleWithIntfImplSrc('unit3.pas',
  18492. LinesToStr([
  18493. '{$modeswitch externalclass}',
  18494. 'type',
  18495. ' TJSBufferSource = class external name ''BufferSource''',
  18496. ' end;',
  18497. '']),
  18498. '');
  18499. StartUnit(true);
  18500. Add([
  18501. 'interface',
  18502. 'uses unit2, unit3;',
  18503. 'procedure DoSome(s: TJSBufferSource);',
  18504. 'implementation',
  18505. 'procedure DoSome(s: TJSBufferSource);',
  18506. 'begin',
  18507. ' DoIt(s);',
  18508. 'end;',
  18509. '']);
  18510. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18511. nIncompatibleTypeArgNo);
  18512. ConvertUnit;
  18513. end;
  18514. procedure TTestModule.TestClassInterface_Corba;
  18515. begin
  18516. StartProgram(false);
  18517. Add([
  18518. '{$interfaces corba}',
  18519. 'type',
  18520. ' IUnknown = interface;',
  18521. ' IUnknown = interface',
  18522. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18523. ' end;',
  18524. ' IInterface = IUnknown;',
  18525. ' IBird = interface(IInterface)',
  18526. ' function GetSize: longint;',
  18527. ' procedure SetSize(i: longint);',
  18528. ' property Size: longint read GetSize write SetSize;',
  18529. ' procedure DoIt(i: longint);',
  18530. ' end;',
  18531. ' TObject = class',
  18532. ' end;',
  18533. ' TBird = class(TObject,IBird)',
  18534. ' function GetSize: longint; virtual; abstract;',
  18535. ' procedure SetSize(i: longint); virtual; abstract;',
  18536. ' procedure DoIt(i: longint); virtual; abstract;',
  18537. ' end;',
  18538. 'var',
  18539. ' BirdIntf: IBird;',
  18540. 'begin',
  18541. ' BirdIntf.Size:=BirdIntf.Size;',
  18542. '']);
  18543. ConvertProgram;
  18544. CheckSource('TestClassInterface_Corba',
  18545. LinesToStr([ // statements
  18546. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18547. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18548. 'rtl.createClass(this, "TObject", null, function () {',
  18549. ' this.$init = function () {',
  18550. ' };',
  18551. ' this.$final = function () {',
  18552. ' };',
  18553. '});',
  18554. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18555. ' rtl.addIntf(this, $mod.IBird);',
  18556. '});',
  18557. 'this.BirdIntf = null;',
  18558. '']),
  18559. LinesToStr([ // $mod.$main
  18560. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18561. '']));
  18562. end;
  18563. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18564. begin
  18565. StartProgram(false);
  18566. Add([
  18567. '{$interfaces corba}',
  18568. 'type',
  18569. ' IUnknown = interface',
  18570. ' procedure DoIt; external name ''foo'';',
  18571. ' end;',
  18572. 'begin']);
  18573. SetExpectedParserError(
  18574. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18575. nParserNoFieldsAllowed);
  18576. ConvertProgram;
  18577. end;
  18578. procedure TTestModule.TestClassInterface_Overloads;
  18579. begin
  18580. StartProgram(false);
  18581. Add([
  18582. '{$interfaces corba}',
  18583. 'type',
  18584. ' integer = longint;',
  18585. ' IUnknown = interface',
  18586. ' procedure DoIt(i: integer);',
  18587. ' procedure DoIt(s: string);',
  18588. ' end;',
  18589. ' IBird = interface(IUnknown)',
  18590. ' procedure DoIt(b: boolean); overload;',
  18591. ' end;',
  18592. ' TObject = class',
  18593. ' end;',
  18594. ' TBird = class(TObject,IBird)',
  18595. ' procedure DoIt(o: TObject);',
  18596. ' procedure DoIt(s: string);',
  18597. ' procedure DoIt(i: integer);',
  18598. ' procedure DoIt(b: boolean);',
  18599. ' end;',
  18600. 'procedure TBird.DoIt(o: TObject); begin end;',
  18601. 'procedure TBird.DoIt(s: string); begin end;',
  18602. 'procedure TBird.DoIt(i: integer); begin end;',
  18603. 'procedure TBird.DoIt(b: boolean); begin end;',
  18604. 'var',
  18605. ' BirdIntf: IBird;',
  18606. 'begin',
  18607. ' BirdIntf.DoIt(3);',
  18608. ' BirdIntf.DoIt(''abc'');',
  18609. ' BirdIntf.DoIt(true);',
  18610. '']);
  18611. ConvertProgram;
  18612. CheckSource('TestClassInterface_Overloads',
  18613. LinesToStr([ // statements
  18614. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18615. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18616. 'rtl.createClass(this, "TObject", null, function () {',
  18617. ' this.$init = function () {',
  18618. ' };',
  18619. ' this.$final = function () {',
  18620. ' };',
  18621. '});',
  18622. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18623. ' this.DoIt = function (o) {',
  18624. ' };',
  18625. ' this.DoIt$1 = function (s) {',
  18626. ' };',
  18627. ' this.DoIt$2 = function (i) {',
  18628. ' };',
  18629. ' this.DoIt$3 = function (b) {',
  18630. ' };',
  18631. ' rtl.addIntf(this, $mod.IBird, {',
  18632. ' DoIt$2: "DoIt$3",',
  18633. ' DoIt: "DoIt$2"',
  18634. ' });',
  18635. '});',
  18636. 'this.BirdIntf = null;',
  18637. '']),
  18638. LinesToStr([ // $mod.$main
  18639. '$mod.BirdIntf.DoIt(3);',
  18640. '$mod.BirdIntf.DoIt$1("abc");',
  18641. '$mod.BirdIntf.DoIt$2(true);',
  18642. '']));
  18643. end;
  18644. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18645. begin
  18646. StartProgram(false);
  18647. Add([
  18648. '{$interfaces corba}',
  18649. 'type',
  18650. ' IBird = interface',
  18651. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18652. ' end;',
  18653. ' IDog = interface',
  18654. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18655. ' end;',
  18656. ' TObject = class(IBird,IDog)',
  18657. ' end;',
  18658. 'begin']);
  18659. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18660. nDuplicateGUIDXInYZ);
  18661. ConvertProgram;
  18662. end;
  18663. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18664. begin
  18665. StartProgram(false);
  18666. Add([
  18667. '{$interfaces corba}',
  18668. 'type',
  18669. ' IAnimal = interface',
  18670. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18671. ' end;',
  18672. ' IBird = interface(IAnimal)',
  18673. ' end;',
  18674. ' IHawk = interface(IBird)',
  18675. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18676. ' end;',
  18677. 'begin']);
  18678. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18679. nDuplicateGUIDXInYZ);
  18680. ConvertProgram;
  18681. end;
  18682. procedure TTestModule.TestClassInterface_AncestorImpl;
  18683. begin
  18684. StartProgram(false);
  18685. Add([
  18686. '{$interfaces corba}',
  18687. 'type',
  18688. ' integer = longint;',
  18689. ' IUnknown = interface',
  18690. ' procedure DoIt(i: integer);',
  18691. ' end;',
  18692. ' IBird = interface',
  18693. ' procedure Fly(i: integer);',
  18694. ' end;',
  18695. ' TObject = class(IUnknown)',
  18696. ' procedure DoIt(i: integer);',
  18697. ' end;',
  18698. ' TBird = class(IBird)',
  18699. ' procedure Fly(i: integer);',
  18700. ' end;',
  18701. 'procedure TObject.DoIt(i: integer); begin end;',
  18702. 'procedure TBird.Fly(i: integer); begin end;',
  18703. 'begin',
  18704. '']);
  18705. ConvertProgram;
  18706. CheckSource('TestClassInterface_AncestorIntf',
  18707. LinesToStr([ // statements
  18708. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18709. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18710. 'rtl.createClass(this, "TObject", null, function () {',
  18711. ' this.$init = function () {',
  18712. ' };',
  18713. ' this.$final = function () {',
  18714. ' };',
  18715. ' this.DoIt = function (i) {',
  18716. ' };',
  18717. ' rtl.addIntf(this, $mod.IUnknown);',
  18718. '});',
  18719. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18720. ' this.Fly = function (i) {',
  18721. ' };',
  18722. ' rtl.addIntf(this, $mod.IBird);',
  18723. ' rtl.addIntf(this, $mod.IUnknown);',
  18724. '});',
  18725. '']),
  18726. LinesToStr([ // $mod.$main
  18727. '']));
  18728. end;
  18729. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18730. begin
  18731. StartProgram(false);
  18732. Add([
  18733. '{$interfaces corba}',
  18734. 'type',
  18735. ' integer = longint;',
  18736. ' IBird = interface',
  18737. ' procedure DoIt(i: integer);',
  18738. ' end;',
  18739. ' TObject = class',
  18740. ' procedure DoIt(i: integer);',
  18741. ' end;',
  18742. ' TBird = class(IBird)',
  18743. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18744. ' end;',
  18745. 'procedure TObject.DoIt(i: integer); begin end;',
  18746. 'procedure TBird.DoIt(i: integer); begin end;',
  18747. 'begin',
  18748. '']);
  18749. ConvertProgram;
  18750. CheckSource('TestClassInterface_ImplReintroduce',
  18751. LinesToStr([ // statements
  18752. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18753. 'rtl.createClass(this, "TObject", null, function () {',
  18754. ' this.$init = function () {',
  18755. ' };',
  18756. ' this.$final = function () {',
  18757. ' };',
  18758. ' this.DoIt = function (i) {',
  18759. ' };',
  18760. '});',
  18761. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18762. ' this.DoIt$1 = function (i) {',
  18763. ' };',
  18764. ' rtl.addIntf(this, $mod.IBird, {',
  18765. ' DoIt: "DoIt$1"',
  18766. ' });',
  18767. '});',
  18768. '']),
  18769. LinesToStr([ // $mod.$main
  18770. '']));
  18771. end;
  18772. procedure TTestModule.TestClassInterface_MethodResolution;
  18773. begin
  18774. StartProgram(false);
  18775. Add([
  18776. '{$interfaces corba}',
  18777. 'type',
  18778. ' IUnknown = interface',
  18779. ' procedure Walk(i: longint);',
  18780. ' end;',
  18781. ' IBird = interface(IUnknown)',
  18782. ' procedure Walk(b: boolean); overload;',
  18783. ' procedure Fly(s: string);',
  18784. ' end;',
  18785. ' TObject = class',
  18786. ' end;',
  18787. ' TBird = class(TObject,IBird)',
  18788. ' procedure IBird.Fly = Move;',
  18789. ' procedure IBird.Walk = Hop;',
  18790. ' procedure Hop(i: longint);',
  18791. ' procedure Move(s: string);',
  18792. ' procedure Hop(b: boolean);',
  18793. ' end;',
  18794. 'procedure TBird.Move(s: string); begin end;',
  18795. 'procedure TBird.Hop(i: longint); begin end;',
  18796. 'procedure TBird.Hop(b: boolean); begin end;',
  18797. 'var',
  18798. ' BirdIntf: IBird;',
  18799. 'begin',
  18800. ' BirdIntf.Walk(3);',
  18801. ' BirdIntf.Walk(true);',
  18802. ' BirdIntf.Fly(''abc'');',
  18803. '']);
  18804. ConvertProgram;
  18805. CheckSource('TestClassInterface_MethodResolution',
  18806. LinesToStr([ // statements
  18807. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18808. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18809. 'rtl.createClass(this, "TObject", null, function () {',
  18810. ' this.$init = function () {',
  18811. ' };',
  18812. ' this.$final = function () {',
  18813. ' };',
  18814. '});',
  18815. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18816. ' this.Hop = function (i) {',
  18817. ' };',
  18818. ' this.Move = function (s) {',
  18819. ' };',
  18820. ' this.Hop$1 = function (b) {',
  18821. ' };',
  18822. ' rtl.addIntf(this, $mod.IBird, {',
  18823. ' Walk$1: "Hop$1",',
  18824. ' Fly: "Move",',
  18825. ' Walk: "Hop"',
  18826. ' });',
  18827. '});',
  18828. 'this.BirdIntf = null;',
  18829. '']),
  18830. LinesToStr([ // $mod.$main
  18831. '$mod.BirdIntf.Walk(3);',
  18832. '$mod.BirdIntf.Walk$1(true);',
  18833. '$mod.BirdIntf.Fly("abc");',
  18834. '']));
  18835. end;
  18836. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18837. begin
  18838. StartProgram(false);
  18839. Add([
  18840. '{$interfaces com}',
  18841. 'type',
  18842. ' IUnknown = interface',
  18843. ' function _AddRef: longint;',
  18844. ' procedure Walk;',
  18845. ' end;',
  18846. ' IBird = interface end;',
  18847. ' IDog = interface end;',
  18848. ' TObject = class(IBird,IDog)',
  18849. ' function _AddRef: longint; virtual; abstract;',
  18850. ' procedure Walk; virtual; abstract;',
  18851. ' end;',
  18852. ' TBird = class(IUnknown)',
  18853. ' end;',
  18854. 'begin',
  18855. '']);
  18856. ConvertProgram;
  18857. CheckSource('TestClassInterface_COM_AncestorLess',
  18858. LinesToStr([ // statements
  18859. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18860. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18861. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18862. 'rtl.createClass(this, "TObject", null, function () {',
  18863. ' this.$init = function () {',
  18864. ' };',
  18865. ' this.$final = function () {',
  18866. ' };',
  18867. ' rtl.addIntf(this, $mod.IBird);',
  18868. ' rtl.addIntf(this, $mod.IDog);',
  18869. '});',
  18870. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18871. ' rtl.addIntf(this, $mod.IUnknown);',
  18872. ' rtl.addIntf(this, $mod.IBird);',
  18873. ' rtl.addIntf(this, $mod.IDog);',
  18874. '});',
  18875. '']),
  18876. LinesToStr([ // $mod.$main
  18877. '']));
  18878. end;
  18879. procedure TTestModule.TestClassInterface_MethodOverride;
  18880. begin
  18881. StartProgram(false);
  18882. Add([
  18883. '{$interfaces corba}',
  18884. 'type',
  18885. ' IUnknown = interface',
  18886. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18887. ' procedure Go;',
  18888. ' end;',
  18889. ' TObject = class(IUnknown)',
  18890. ' procedure Go; virtual; abstract;',
  18891. ' end;',
  18892. ' TBird = class',
  18893. ' procedure Go; override;',
  18894. ' end;',
  18895. ' TCat = class(TObject)',
  18896. ' procedure Go; override;',
  18897. ' end;',
  18898. ' TDog = class(TObject, IUnknown)',
  18899. ' procedure Go; override;',
  18900. ' end;',
  18901. 'procedure TBird.Go; begin end;',
  18902. 'procedure TCat.Go; begin end;',
  18903. 'procedure TDog.Go; begin end;',
  18904. 'begin',
  18905. '']);
  18906. ConvertProgram;
  18907. CheckSource('TestClassInterface_MethodOverride',
  18908. LinesToStr([ // statements
  18909. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18910. 'rtl.createClass(this, "TObject", null, function () {',
  18911. ' this.$init = function () {',
  18912. ' };',
  18913. ' this.$final = function () {',
  18914. ' };',
  18915. ' rtl.addIntf(this, $mod.IUnknown);',
  18916. '});',
  18917. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18918. ' this.Go = function () {',
  18919. ' };',
  18920. ' rtl.addIntf(this, $mod.IUnknown);',
  18921. '});',
  18922. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18923. ' this.Go = function () {',
  18924. ' };',
  18925. ' rtl.addIntf(this, $mod.IUnknown);',
  18926. '});',
  18927. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18928. ' this.Go = function () {',
  18929. ' };',
  18930. ' rtl.addIntf(this, $mod.IUnknown);',
  18931. '});',
  18932. '']),
  18933. LinesToStr([ // $mod.$main
  18934. '']));
  18935. end;
  18936. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18937. begin
  18938. StartProgram(false);
  18939. Add([
  18940. '{$interfaces corba}',
  18941. 'type',
  18942. ' IUnknown = interface',
  18943. ' end;',
  18944. ' IBird = interface(IUnknown)',
  18945. ' procedure Fly(s: string);',
  18946. ' end;',
  18947. ' IEagle = interface(IBird)',
  18948. ' end;',
  18949. ' IDove = interface(IBird)',
  18950. ' end;',
  18951. ' ISwallow = interface(IBird)',
  18952. ' end;',
  18953. ' TObject = class',
  18954. ' end;',
  18955. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18956. ' procedure Fly(s: string); virtual; abstract;',
  18957. ' end;',
  18958. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18959. ' FBirdIntf: IBird;',
  18960. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18961. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18962. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18963. ' FDoveObj: TBird;',
  18964. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18965. ' function GetSwallowObj: TBird; virtual; abstract;',
  18966. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18967. ' end;',
  18968. 'begin',
  18969. '']);
  18970. ConvertProgram;
  18971. CheckSource('TestClassInterface_Corba_Delegation',
  18972. LinesToStr([ // statements
  18973. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18974. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18975. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18976. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18977. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18978. 'rtl.createClass(this, "TObject", null, function () {',
  18979. ' this.$init = function () {',
  18980. ' };',
  18981. ' this.$final = function () {',
  18982. ' };',
  18983. '});',
  18984. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18985. ' rtl.addIntf(this, $mod.IBird);',
  18986. ' rtl.addIntf(this, $mod.IEagle);',
  18987. ' rtl.addIntf(this, $mod.IDove);',
  18988. ' rtl.addIntf(this, $mod.ISwallow);',
  18989. '});',
  18990. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18991. ' this.$init = function () {',
  18992. ' $mod.TObject.$init.call(this);',
  18993. ' this.FBirdIntf = null;',
  18994. ' this.FDoveObj = null;',
  18995. ' };',
  18996. ' this.$final = function () {',
  18997. ' this.FBirdIntf = undefined;',
  18998. ' this.FDoveObj = undefined;',
  18999. ' $mod.TObject.$final.call(this);',
  19000. ' };',
  19001. ' this.$intfmaps = {',
  19002. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19003. ' return this.FBirdIntf;',
  19004. ' },',
  19005. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19006. ' return this.GetEagleIntf();',
  19007. ' },',
  19008. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19009. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19010. ' },',
  19011. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19012. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19013. ' }',
  19014. ' };',
  19015. '});',
  19016. '']),
  19017. LinesToStr([ // $mod.$main
  19018. '']));
  19019. end;
  19020. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19021. begin
  19022. StartProgram(false);
  19023. Add([
  19024. '{$interfaces corba}',
  19025. 'type',
  19026. ' IUnknown = interface',
  19027. ' end;',
  19028. ' IBird = interface(IUnknown)',
  19029. ' procedure Fly(s: string);',
  19030. ' end;',
  19031. ' IEagle = interface(IBird)',
  19032. ' end;',
  19033. ' IDove = interface(IBird)',
  19034. ' end;',
  19035. ' ISwallow = interface(IBird)',
  19036. ' end;',
  19037. ' TObject = class',
  19038. ' end;',
  19039. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19040. ' procedure Fly(s: string); virtual; abstract;',
  19041. ' end;',
  19042. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19043. ' private',
  19044. ' class var FBirdIntf: IBird;',
  19045. ' class var FDoveObj: TBird;',
  19046. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19047. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19048. ' protected',
  19049. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19050. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19051. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19052. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19053. ' end;',
  19054. 'begin',
  19055. '']);
  19056. ConvertProgram;
  19057. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19058. LinesToStr([ // statements
  19059. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19060. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19061. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19062. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19063. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19064. 'rtl.createClass(this, "TObject", null, function () {',
  19065. ' this.$init = function () {',
  19066. ' };',
  19067. ' this.$final = function () {',
  19068. ' };',
  19069. '});',
  19070. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19071. ' rtl.addIntf(this, $mod.IBird);',
  19072. ' rtl.addIntf(this, $mod.IEagle);',
  19073. ' rtl.addIntf(this, $mod.IDove);',
  19074. ' rtl.addIntf(this, $mod.ISwallow);',
  19075. '});',
  19076. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19077. ' this.FBirdIntf = null;',
  19078. ' this.FDoveObj = null;',
  19079. ' this.$intfmaps = {',
  19080. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19081. ' return this.FBirdIntf;',
  19082. ' },',
  19083. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19084. ' return this.GetEagleIntf();',
  19085. ' },',
  19086. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19087. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19088. ' },',
  19089. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19090. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19091. ' }',
  19092. ' };',
  19093. '});',
  19094. '']),
  19095. LinesToStr([ // $mod.$main
  19096. '']));
  19097. end;
  19098. procedure TTestModule.TestClassInterface_Corba_Operators;
  19099. begin
  19100. StartProgram(false);
  19101. Add([
  19102. '{$interfaces corba}',
  19103. 'type',
  19104. ' IUnknown = interface',
  19105. ' end;',
  19106. ' IBird = interface(IUnknown)',
  19107. ' function GetItems(Index: longint): longint;',
  19108. ' procedure SetItems(Index: longint; Value: longint);',
  19109. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19110. ' end;',
  19111. ' TObject = class',
  19112. ' end;',
  19113. ' TBird = class(TObject,IBird)',
  19114. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19115. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19116. ' end;',
  19117. 'var',
  19118. ' IntfVar: IBird = nil;',
  19119. ' IntfVar2: IBird;',
  19120. ' ObjVar: TBird;',
  19121. ' v: JSValue;',
  19122. 'begin',
  19123. ' IntfVar:=nil;',
  19124. ' IntfVar[3]:=IntfVar[4];',
  19125. ' if Assigned(IntfVar) then ;',
  19126. ' IntfVar:=IntfVar2;',
  19127. ' IntfVar:=ObjVar;',
  19128. ' if IntfVar=IntfVar2 then ;',
  19129. ' if IntfVar<>IntfVar2 then ;',
  19130. ' if IntfVar is IBird then ;',
  19131. ' if IntfVar is TBird then ;',
  19132. ' if ObjVar is IBird then ;',
  19133. ' IntfVar:=IntfVar2 as IBird;',
  19134. ' ObjVar:=IntfVar2 as TBird;',
  19135. ' IntfVar:=ObjVar as IBird;',
  19136. ' IntfVar:=IBird(IntfVar2);',
  19137. ' ObjVar:=TBird(IntfVar);',
  19138. ' IntfVar:=IBird(ObjVar);',
  19139. ' v:=IntfVar;',
  19140. ' IntfVar:=IBird(v);',
  19141. ' if v is IBird then ;',
  19142. ' v:=JSValue(IntfVar);',
  19143. ' v:=IBird;',
  19144. '']);
  19145. ConvertProgram;
  19146. CheckSource('TestClassInterface_Corba_Operators',
  19147. LinesToStr([ // statements
  19148. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19149. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19150. 'rtl.createClass(this, "TObject", null, function () {',
  19151. ' this.$init = function () {',
  19152. ' };',
  19153. ' this.$final = function () {',
  19154. ' };',
  19155. '});',
  19156. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19157. ' rtl.addIntf(this, $mod.IBird);',
  19158. '});',
  19159. 'this.IntfVar = null;',
  19160. 'this.IntfVar2 = null;',
  19161. 'this.ObjVar = null;',
  19162. 'this.v = undefined;',
  19163. '']),
  19164. LinesToStr([ // $mod.$main
  19165. '$mod.IntfVar = null;',
  19166. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19167. 'if ($mod.IntfVar != null) ;',
  19168. '$mod.IntfVar = $mod.IntfVar2;',
  19169. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19170. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19171. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19172. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19173. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19174. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19175. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19176. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19177. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19178. '$mod.IntfVar = $mod.IntfVar2;',
  19179. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19180. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19181. '$mod.v = $mod.IntfVar;',
  19182. '$mod.IntfVar = rtl.getObject($mod.v);',
  19183. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19184. '$mod.v = $mod.IntfVar;',
  19185. '$mod.v = $mod.IBird;',
  19186. '']));
  19187. end;
  19188. procedure TTestModule.TestClassInterface_Corba_Args;
  19189. begin
  19190. StartProgram(false);
  19191. Add([
  19192. '{$interfaces corba}',
  19193. 'type',
  19194. ' IUnknown = interface',
  19195. ' end;',
  19196. ' IBird = interface(IUnknown)',
  19197. ' end;',
  19198. ' TObject = class',
  19199. ' end;',
  19200. ' TBird = class(TObject,IBird)',
  19201. ' end;',
  19202. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19203. 'begin',
  19204. ' DoIt(i,i,i);',
  19205. 'end;',
  19206. 'procedure Change(var i: IBird; out j: IBird);',
  19207. 'begin',
  19208. ' DoIt(i,i,i);',
  19209. ' Change(i,i);',
  19210. 'end;',
  19211. 'var',
  19212. ' i: IBird;',
  19213. ' o: TBird;',
  19214. 'begin',
  19215. ' DoIt(i,i,i);',
  19216. ' Change(i,i);',
  19217. ' DoIt(o,o,o);',
  19218. '']);
  19219. ConvertProgram;
  19220. CheckSource('TestClassInterface_Corba_Args',
  19221. LinesToStr([ // statements
  19222. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19223. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19224. 'rtl.createClass(this, "TObject", null, function () {',
  19225. ' this.$init = function () {',
  19226. ' };',
  19227. ' this.$final = function () {',
  19228. ' };',
  19229. '});',
  19230. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19231. ' rtl.addIntf(this, $mod.IBird);',
  19232. '});',
  19233. 'this.DoIt = function (u, i, j) {',
  19234. ' $mod.DoIt({',
  19235. ' get: function () {',
  19236. ' return i;',
  19237. ' },',
  19238. ' set: function (v) {',
  19239. ' i = v;',
  19240. ' }',
  19241. ' }, i, i);',
  19242. '};',
  19243. 'this.Change = function (i, j) {',
  19244. ' $mod.DoIt(i, i.get(), i.get());',
  19245. ' $mod.Change(i, i);',
  19246. '};',
  19247. 'this.i = null;',
  19248. 'this.o = null;',
  19249. '']),
  19250. LinesToStr([ // $mod.$main
  19251. '$mod.DoIt({',
  19252. ' p: $mod,',
  19253. ' get: function () {',
  19254. ' return this.p.i;',
  19255. ' },',
  19256. ' set: function (v) {',
  19257. ' this.p.i = v;',
  19258. ' }',
  19259. '}, $mod.i, $mod.i);',
  19260. '$mod.Change({',
  19261. ' p: $mod,',
  19262. ' get: function () {',
  19263. ' return this.p.i;',
  19264. ' },',
  19265. ' set: function (v) {',
  19266. ' this.p.i = v;',
  19267. ' }',
  19268. '}, {',
  19269. ' p: $mod,',
  19270. ' get: function () {',
  19271. ' return this.p.i;',
  19272. ' },',
  19273. ' set: function (v) {',
  19274. ' this.p.i = v;',
  19275. ' }',
  19276. '});',
  19277. '$mod.DoIt({',
  19278. ' p: $mod,',
  19279. ' get: function () {',
  19280. ' return this.p.o;',
  19281. ' },',
  19282. ' set: function (v) {',
  19283. ' this.p.o = v;',
  19284. ' }',
  19285. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19286. '']));
  19287. end;
  19288. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19289. begin
  19290. StartProgram(false);
  19291. Add([
  19292. '{$interfaces corba}',
  19293. 'type',
  19294. ' IUnknown = interface end;',
  19295. ' TObject = class',
  19296. ' Id: longint;',
  19297. ' end;',
  19298. ' IEnumerator = interface(IUnknown)',
  19299. ' function GetCurrent: TObject;',
  19300. ' function MoveNext: Boolean;',
  19301. ' property Current: TObject read GetCurrent;',
  19302. ' end;',
  19303. ' IEnumerable = interface(IUnknown)',
  19304. ' function GetEnumerator: IEnumerator;',
  19305. ' end;',
  19306. 'var',
  19307. ' o: TObject;',
  19308. ' i: IEnumerable;',
  19309. 'begin',
  19310. ' for o in i do o.Id:=3;',
  19311. '']);
  19312. ConvertProgram;
  19313. CheckSource('TestClassInterface_Corba_ForIn',
  19314. LinesToStr([ // statements
  19315. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19316. 'rtl.createClass(this, "TObject", null, function () {',
  19317. ' this.$init = function () {',
  19318. ' this.Id = 0;',
  19319. ' };',
  19320. ' this.$final = function () {',
  19321. ' };',
  19322. '});',
  19323. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19324. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19325. 'this.o = null;',
  19326. 'this.i = null;',
  19327. '']),
  19328. LinesToStr([ // $mod.$main
  19329. 'var $in = $mod.i.GetEnumerator();',
  19330. 'while ($in.MoveNext()) {',
  19331. ' $mod.o = $in.GetCurrent();',
  19332. ' $mod.o.Id = 3;',
  19333. '};',
  19334. '']));
  19335. end;
  19336. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19337. begin
  19338. StartProgram(false);
  19339. Add([
  19340. '{$interfaces com}',
  19341. 'type',
  19342. ' IUnknown = interface',
  19343. ' function _AddRef: longint;',
  19344. ' function _Release: longint;',
  19345. ' end;',
  19346. ' TObject = class(IUnknown)',
  19347. ' function _AddRef: longint; virtual; abstract;',
  19348. ' function _Release: longint; virtual; abstract;',
  19349. ' end;',
  19350. 'var',
  19351. ' i: IUnknown;',
  19352. 'procedure DoGlobal(o: TObject);',
  19353. 'begin',
  19354. ' i:=nil;',
  19355. ' i:=o;',
  19356. ' i:=i;',
  19357. 'end;',
  19358. 'procedure DoLocal(o: TObject);',
  19359. 'const k: IUnknown = nil;',
  19360. 'var j: IUnknown;',
  19361. 'begin',
  19362. ' k:=o;',
  19363. ' k:=i;',
  19364. ' j:=o;',
  19365. ' j:=i;',
  19366. 'end;',
  19367. 'var o: TObject;',
  19368. 'begin',
  19369. ' i:=nil;',
  19370. ' i:=o;',
  19371. '']);
  19372. ConvertProgram;
  19373. CheckSource('TestClassInterface_COM_AssignVar',
  19374. LinesToStr([ // statements
  19375. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19376. 'rtl.createClass(this, "TObject", null, function () {',
  19377. ' this.$init = function () {',
  19378. ' };',
  19379. ' this.$final = function () {',
  19380. ' };',
  19381. ' rtl.addIntf(this, $mod.IUnknown);',
  19382. '});',
  19383. 'this.i = null;',
  19384. 'this.DoGlobal = function (o) {',
  19385. ' rtl.setIntfP($mod, "i", null);',
  19386. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19387. ' rtl.setIntfP($mod, "i", $mod.i);',
  19388. '};',
  19389. 'var k = null;',
  19390. 'this.DoLocal = function (o) {',
  19391. ' var j = null;',
  19392. ' try{',
  19393. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19394. ' k = rtl.setIntfL(k, $mod.i);',
  19395. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19396. ' j = rtl.setIntfL(j, $mod.i);',
  19397. ' }finally{',
  19398. ' rtl._Release(j);',
  19399. ' };',
  19400. '};',
  19401. 'this.o = null;',
  19402. '']),
  19403. LinesToStr([ // $mod.$main
  19404. 'rtl.setIntfP($mod, "i", null);',
  19405. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19406. '']));
  19407. end;
  19408. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19409. begin
  19410. StartProgram(false);
  19411. Add([
  19412. '{$interfaces com}',
  19413. 'type',
  19414. ' IUnknown = interface',
  19415. ' function _AddRef: longint;',
  19416. ' function _Release: longint;',
  19417. ' end;',
  19418. ' TObject = class(IUnknown)',
  19419. ' function _AddRef: longint; virtual; abstract;',
  19420. ' function _Release: longint; virtual; abstract;',
  19421. ' end;',
  19422. 'procedure DoDefault(i, j: IUnknown);',
  19423. 'begin',
  19424. ' i:=nil;',
  19425. ' i:=j;',
  19426. 'end;',
  19427. 'begin',
  19428. '']);
  19429. ConvertProgram;
  19430. CheckSource('TestClassInterface_COM_AssignArg',
  19431. LinesToStr([ // statements
  19432. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19433. 'rtl.createClass(this, "TObject", null, function () {',
  19434. ' this.$init = function () {',
  19435. ' };',
  19436. ' this.$final = function () {',
  19437. ' };',
  19438. ' rtl.addIntf(this, $mod.IUnknown);',
  19439. '});',
  19440. 'this.DoDefault = function (i, j) {',
  19441. ' rtl._AddRef(i);',
  19442. ' try {',
  19443. ' i = rtl.setIntfL(i, null);',
  19444. ' i = rtl.setIntfL(i, j);',
  19445. ' } finally {',
  19446. ' rtl._Release(i);',
  19447. ' };',
  19448. '};',
  19449. '']),
  19450. LinesToStr([ // $mod.$main
  19451. '']));
  19452. end;
  19453. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19454. begin
  19455. StartProgram(false);
  19456. Add([
  19457. '{$interfaces com}',
  19458. 'type',
  19459. ' IUnknown = interface',
  19460. ' function _AddRef: longint;',
  19461. ' function _Release: longint;',
  19462. ' end;',
  19463. ' TObject = class(IUnknown)',
  19464. ' function _AddRef: longint; virtual; abstract;',
  19465. ' function _Release: longint; virtual; abstract;',
  19466. ' end;',
  19467. 'function DoDefault(i: IUnknown): IUnknown;',
  19468. 'begin',
  19469. ' Result:=i;',
  19470. ' if Result<>nil then exit;',
  19471. 'end;',
  19472. 'begin',
  19473. '']);
  19474. ConvertProgram;
  19475. CheckSource('TestClassInterface_COM_FunctionResult',
  19476. LinesToStr([ // statements
  19477. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19478. 'rtl.createClass(this, "TObject", null, function () {',
  19479. ' this.$init = function () {',
  19480. ' };',
  19481. ' this.$final = function () {',
  19482. ' };',
  19483. ' rtl.addIntf(this, $mod.IUnknown);',
  19484. '});',
  19485. 'this.DoDefault = function (i) {',
  19486. ' var Result = null;',
  19487. ' var $ok = false;',
  19488. ' try {',
  19489. ' Result = rtl.setIntfL(Result, i);',
  19490. ' if(Result !== null){',
  19491. ' $ok = true;',
  19492. ' return Result;',
  19493. ' };',
  19494. ' $ok = true;',
  19495. ' } finally {',
  19496. ' if(!$ok) rtl._Release(Result);',
  19497. ' };',
  19498. ' return Result;',
  19499. '};',
  19500. '']),
  19501. LinesToStr([ // $mod.$main
  19502. '']));
  19503. end;
  19504. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19505. begin
  19506. StartProgram(false);
  19507. Add([
  19508. '{$interfaces com}',
  19509. 'type',
  19510. ' IUnknown = interface',
  19511. ' function _AddRef: longint;',
  19512. ' function _Release: longint;',
  19513. ' end;',
  19514. ' TObject = class(IUnknown)',
  19515. ' function _AddRef: longint; virtual; abstract;',
  19516. ' function _Release: longint; virtual; abstract;',
  19517. ' function GetIntf: IUnknown; virtual;',
  19518. ' end;',
  19519. ' TMouse = class',
  19520. ' function GetIntf: IUnknown; override;',
  19521. ' end;',
  19522. 'function TObject.GetIntf: IUnknown; begin end;',
  19523. 'function TMouse.GetIntf: IUnknown;',
  19524. 'var i: IUnknown;',
  19525. 'begin',
  19526. ' inherited;',
  19527. ' inherited GetIntf;',
  19528. ' inherited GetIntf();',
  19529. ' Result:=inherited GetIntf;',
  19530. ' Result:=inherited GetIntf();',
  19531. ' i:=inherited GetIntf;',
  19532. ' i:=inherited GetIntf();',
  19533. 'end;',
  19534. 'begin',
  19535. '']);
  19536. ConvertProgram;
  19537. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19538. LinesToStr([ // statements
  19539. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19540. 'rtl.createClass(this, "TObject", null, function () {',
  19541. ' this.$init = function () {',
  19542. ' };',
  19543. ' this.$final = function () {',
  19544. ' };',
  19545. ' this.GetIntf = function () {',
  19546. ' var Result = null;',
  19547. ' return Result;',
  19548. ' };',
  19549. ' rtl.addIntf(this, $mod.IUnknown);',
  19550. '});',
  19551. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19552. ' this.GetIntf = function () {',
  19553. ' var Result = null;',
  19554. ' var i = null;',
  19555. ' var $ir = rtl.createIntfRefs();',
  19556. ' var $ok = false;',
  19557. ' try {',
  19558. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19559. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19560. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19561. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19562. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19563. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19564. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19565. ' $ok = true;',
  19566. ' } finally {',
  19567. ' $ir.free();',
  19568. ' rtl._Release(i);',
  19569. ' if (!$ok) rtl._Release(Result);',
  19570. ' };',
  19571. ' return Result;',
  19572. ' };',
  19573. ' rtl.addIntf(this, $mod.IUnknown);',
  19574. '});',
  19575. '']),
  19576. LinesToStr([ // $mod.$main
  19577. '']));
  19578. end;
  19579. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19580. begin
  19581. StartProgram(false);
  19582. Add([
  19583. '{$interfaces com}',
  19584. 'type',
  19585. ' IUnknown = interface',
  19586. ' function _AddRef: longint;',
  19587. ' function _Release: longint;',
  19588. ' end;',
  19589. ' TObject = class(IUnknown)',
  19590. ' function _AddRef: longint; virtual; abstract;',
  19591. ' function _Release: longint; virtual; abstract;',
  19592. ' end;',
  19593. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19594. 'begin',
  19595. ' if i is IUnknown then ;',
  19596. ' if o is IUnknown then ;',
  19597. ' if i is TObject then ;',
  19598. ' i:=j as IUnknown;',
  19599. ' i:=o as IUnknown;',
  19600. ' o:=j as TObject;',
  19601. ' i:=IUnknown(j);',
  19602. ' i:=IUnknown(o);',
  19603. ' o:=TObject(i);',
  19604. 'end;',
  19605. 'begin',
  19606. '']);
  19607. ConvertProgram;
  19608. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19609. LinesToStr([ // statements
  19610. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19611. 'rtl.createClass(this, "TObject", null, function () {',
  19612. ' this.$init = function () {',
  19613. ' };',
  19614. ' this.$final = function () {',
  19615. ' };',
  19616. ' rtl.addIntf(this, $mod.IUnknown);',
  19617. '});',
  19618. 'this.DoDefault = function (i, j, o) {',
  19619. ' rtl._AddRef(i);',
  19620. ' try {',
  19621. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19622. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19623. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19624. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19625. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19626. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19627. ' i = rtl.setIntfL(i, j);',
  19628. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19629. ' o = rtl.intfToClass(i, $mod.TObject);',
  19630. ' } finally {',
  19631. ' rtl._Release(i);',
  19632. ' };',
  19633. '};',
  19634. '']),
  19635. LinesToStr([ // $mod.$main
  19636. '']));
  19637. end;
  19638. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19639. begin
  19640. StartProgram(false);
  19641. Add([
  19642. '{$interfaces com}',
  19643. 'type',
  19644. ' IUnknown = interface',
  19645. ' function _AddRef: longint;',
  19646. ' function _Release: longint;',
  19647. ' end;',
  19648. ' TObject = class(IUnknown)',
  19649. ' function _AddRef: longint; virtual; abstract;',
  19650. ' function _Release: longint; virtual; abstract;',
  19651. ' end;',
  19652. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19653. 'var o: TObject;',
  19654. 'begin',
  19655. ' DoIt(v,v,v,v);',
  19656. ' DoIt(o,o,k,k);',
  19657. 'end;',
  19658. 'procedure DoSome;',
  19659. 'var v: IUnknown;',
  19660. 'begin',
  19661. ' DoIt(v,v,v,v);',
  19662. 'end;',
  19663. 'var i: IUnknown;',
  19664. 'begin',
  19665. ' DoIt(i,i,i,i);',
  19666. '']);
  19667. ConvertProgram;
  19668. CheckSource('TestClassInterface_COM_PassAsArg',
  19669. LinesToStr([ // statements
  19670. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19671. 'rtl.createClass(this, "TObject", null, function () {',
  19672. ' this.$init = function () {',
  19673. ' };',
  19674. ' this.$final = function () {',
  19675. ' };',
  19676. ' rtl.addIntf(this, $mod.IUnknown);',
  19677. '});',
  19678. 'this.DoIt = function (v, j, k, l) {',
  19679. ' var o = null;',
  19680. ' var $ir = rtl.createIntfRefs();',
  19681. ' rtl._AddRef(v);',
  19682. ' try {',
  19683. ' $mod.DoIt(v, v, {',
  19684. ' get: function () {',
  19685. ' return v;',
  19686. ' },',
  19687. ' set: function (w) {',
  19688. ' v = rtl.setIntfL(v, w);',
  19689. ' }',
  19690. ' }, {',
  19691. ' get: function () {',
  19692. ' return v;',
  19693. ' },',
  19694. ' set: function (w) {',
  19695. ' v = rtl.setIntfL(v, w);',
  19696. ' }',
  19697. ' });',
  19698. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19699. ' } finally {',
  19700. ' $ir.free();',
  19701. ' rtl._Release(v);',
  19702. ' };',
  19703. '};',
  19704. 'this.DoSome = function () {',
  19705. ' var v = null;',
  19706. ' try {',
  19707. ' $mod.DoIt(v, v, {',
  19708. ' get: function () {',
  19709. ' return v;',
  19710. ' },',
  19711. ' set: function (w) {',
  19712. ' v = rtl.setIntfL(v, w);',
  19713. ' }',
  19714. ' }, {',
  19715. ' get: function () {',
  19716. ' return v;',
  19717. ' },',
  19718. ' set: function (w) {',
  19719. ' v = rtl.setIntfL(v, w);',
  19720. ' }',
  19721. ' });',
  19722. ' } finally {',
  19723. ' rtl._Release(v);',
  19724. ' };',
  19725. '};',
  19726. 'this.i = null;',
  19727. '']),
  19728. LinesToStr([ // $mod.$main
  19729. '$mod.DoIt($mod.i, $mod.i, {',
  19730. ' p: $mod,',
  19731. ' get: function () {',
  19732. ' return this.p.i;',
  19733. ' },',
  19734. ' set: function (v) {',
  19735. ' rtl.setIntfP(this.p, "i", v);',
  19736. ' }',
  19737. '}, {',
  19738. ' p: $mod,',
  19739. ' get: function () {',
  19740. ' return this.p.i;',
  19741. ' },',
  19742. ' set: function (v) {',
  19743. ' rtl.setIntfP(this.p, "i", v);',
  19744. ' }',
  19745. '});',
  19746. '']));
  19747. end;
  19748. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19749. begin
  19750. StartProgram(false);
  19751. Add([
  19752. '{$interfaces com}',
  19753. 'type',
  19754. ' IUnknown = interface',
  19755. ' function _AddRef: longint;',
  19756. ' function _Release: longint;',
  19757. ' end;',
  19758. ' TObject = class(IUnknown)',
  19759. ' function _AddRef: longint; virtual; abstract;',
  19760. ' function _Release: longint; virtual; abstract;',
  19761. ' end;',
  19762. 'procedure DoIt(out i);',
  19763. 'begin end;',
  19764. 'procedure DoSome;',
  19765. 'var v: IUnknown;',
  19766. 'begin',
  19767. ' DoIt(v);',
  19768. 'end;',
  19769. 'function GetIt: IUnknown;',
  19770. 'begin',
  19771. ' DoIt(Result);',
  19772. 'end;',
  19773. 'var i: IUnknown;',
  19774. 'begin',
  19775. ' DoIt(i);',
  19776. '']);
  19777. ConvertProgram;
  19778. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19779. LinesToStr([ // statements
  19780. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19781. 'rtl.createClass(this, "TObject", null, function () {',
  19782. ' this.$init = function () {',
  19783. ' };',
  19784. ' this.$final = function () {',
  19785. ' };',
  19786. ' rtl.addIntf(this, $mod.IUnknown);',
  19787. '});',
  19788. 'this.DoIt = function (i) {',
  19789. '};',
  19790. 'this.DoSome = function () {',
  19791. ' var v = null;',
  19792. ' try {',
  19793. ' $mod.DoIt({',
  19794. ' get: function () {',
  19795. ' return v;',
  19796. ' },',
  19797. ' set: function (w) {',
  19798. ' v = w;',
  19799. ' }',
  19800. ' });',
  19801. ' } finally {',
  19802. ' rtl._Release(v);',
  19803. ' };',
  19804. '};',
  19805. 'this.GetIt = function () {',
  19806. ' var Result = null;',
  19807. ' var $ok = false;',
  19808. ' try {',
  19809. ' $mod.DoIt({',
  19810. ' get: function () {',
  19811. ' return Result;',
  19812. ' },',
  19813. ' set: function (v) {',
  19814. ' Result = v;',
  19815. ' }',
  19816. ' });',
  19817. ' $ok = true;',
  19818. ' } finally {',
  19819. ' if (!$ok) rtl._Release(Result);',
  19820. ' };',
  19821. ' return Result;',
  19822. '};',
  19823. 'this.i = null;',
  19824. '']),
  19825. LinesToStr([ // $mod.$main
  19826. 'try {',
  19827. ' $mod.DoIt({',
  19828. ' p: $mod,',
  19829. ' get: function () {',
  19830. ' return this.p.i;',
  19831. ' },',
  19832. ' set: function (v) {',
  19833. ' this.p.i = v;',
  19834. ' }',
  19835. ' });',
  19836. '} finally {',
  19837. ' rtl._Release($mod.i);',
  19838. '};',
  19839. '']));
  19840. end;
  19841. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19842. begin
  19843. StartProgram(false);
  19844. Add([
  19845. '{$interfaces com}',
  19846. 'type',
  19847. ' IUnknown = interface',
  19848. ' function _AddRef: longint;',
  19849. ' function _Release: longint;',
  19850. ' end;',
  19851. ' TObject = class(IUnknown)',
  19852. ' function _AddRef: longint; virtual; abstract;',
  19853. ' function _Release: longint; virtual; abstract;',
  19854. ' end;',
  19855. 'function GetIt: IUnknown;',
  19856. 'begin',
  19857. 'end;',
  19858. 'procedure DoSome;',
  19859. 'var v: IUnknown;',
  19860. ' i: longint;',
  19861. 'begin',
  19862. ' v:=GetIt;',
  19863. ' v:=GetIt();',
  19864. ' GetIt()._AddRef;',
  19865. ' i:=GetIt()._AddRef;',
  19866. 'end;',
  19867. 'var v: IUnknown;',
  19868. ' i: longint;',
  19869. 'begin',
  19870. ' v:=GetIt;',
  19871. ' v:=GetIt();',
  19872. ' GetIt()._AddRef;',
  19873. ' i:=GetIt()._AddRef;',
  19874. '']);
  19875. ConvertProgram;
  19876. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19877. LinesToStr([ // statements
  19878. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19879. 'rtl.createClass(this, "TObject", null, function () {',
  19880. ' this.$init = function () {',
  19881. ' };',
  19882. ' this.$final = function () {',
  19883. ' };',
  19884. ' rtl.addIntf(this, $mod.IUnknown);',
  19885. '});',
  19886. 'this.GetIt = function () {',
  19887. ' var Result = null;',
  19888. ' return Result;',
  19889. '};',
  19890. 'this.DoSome = function () {',
  19891. ' var v = null;',
  19892. ' var i = 0;',
  19893. ' var $ir = rtl.createIntfRefs();',
  19894. ' try {',
  19895. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19896. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19897. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19898. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19899. ' } finally {',
  19900. ' $ir.free();',
  19901. ' rtl._Release(v);',
  19902. ' };',
  19903. '};',
  19904. 'this.v = null;',
  19905. 'this.i = 0;',
  19906. '']),
  19907. LinesToStr([ // $mod.$main
  19908. 'var $ir = rtl.createIntfRefs();',
  19909. 'try {',
  19910. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19911. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19912. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19913. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19914. '} finally {',
  19915. ' $ir.free();',
  19916. '};',
  19917. '']));
  19918. end;
  19919. procedure TTestModule.TestClassInterface_COM_Property;
  19920. begin
  19921. StartProgram(false);
  19922. Add([
  19923. '{$interfaces com}',
  19924. 'type',
  19925. ' IUnknown = interface',
  19926. ' function _AddRef: longint;',
  19927. ' function _Release: longint;',
  19928. ' end;',
  19929. ' TObject = class(IUnknown)',
  19930. ' FAnt: IUnknown;',
  19931. ' function _AddRef: longint; virtual; abstract;',
  19932. ' function _Release: longint; virtual; abstract;',
  19933. ' function GetBird: IUnknown; virtual; abstract;',
  19934. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19935. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19936. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19937. ' property Ant: IUnknown read FAnt write FAnt;',
  19938. ' property Bird: IUnknown read GetBird write SetBird;',
  19939. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19940. ' end;',
  19941. 'procedure DoIt;',
  19942. 'var',
  19943. ' o: TObject;',
  19944. ' v: IUnknown;',
  19945. 'begin',
  19946. ' v:=o.Ant;',
  19947. ' o.Ant:=v;',
  19948. ' o.Ant:=o.Ant;',
  19949. ' v:=o.Bird;',
  19950. ' o.Bird:=v;',
  19951. ' o.Bird:=o.Bird;',
  19952. ' v:=o.Items[1];',
  19953. ' o.Items[2]:=v;',
  19954. ' o.Items[3]:=o.Items[4];',
  19955. ' v:=o[5];',
  19956. ' o[6]:=v;',
  19957. ' o[7]:=o[8];',
  19958. 'end;',
  19959. 'begin',
  19960. '']);
  19961. ConvertProgram;
  19962. CheckSource('TestClassInterface_COM_Property',
  19963. LinesToStr([ // statements
  19964. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19965. 'rtl.createClass(this, "TObject", null, function () {',
  19966. ' this.$init = function () {',
  19967. ' this.FAnt = null;',
  19968. ' };',
  19969. ' this.$final = function () {',
  19970. ' this.FAnt = undefined;',
  19971. ' };',
  19972. ' rtl.addIntf(this, $mod.IUnknown);',
  19973. '});',
  19974. 'this.DoIt = function () {',
  19975. ' var o = null;',
  19976. ' var v = null;',
  19977. ' var $ir = rtl.createIntfRefs();',
  19978. ' try {',
  19979. ' v = rtl.setIntfL(v, o.FAnt);',
  19980. ' rtl.setIntfP(o, "FAnt", v);',
  19981. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19982. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19983. ' o.SetBird(v);',
  19984. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19985. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19986. ' o.SetItems(2, v);',
  19987. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19988. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19989. ' o.SetItems(6, v);',
  19990. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19991. ' } finally {',
  19992. ' $ir.free();',
  19993. ' rtl._Release(v);',
  19994. ' };',
  19995. '};',
  19996. '']),
  19997. LinesToStr([ // $mod.$main
  19998. '']));
  19999. end;
  20000. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20001. begin
  20002. StartProgram(false);
  20003. Add([
  20004. '{$interfaces com}',
  20005. 'type',
  20006. ' IUnknown = interface',
  20007. ' function _AddRef: longint;',
  20008. ' function _Release: longint;',
  20009. ' function GetBird: IUnknown;',
  20010. ' procedure SetBird(Value: IUnknown);',
  20011. ' function GetItems(Index: longint): IUnknown;',
  20012. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20013. ' property Bird: IUnknown read GetBird write SetBird;',
  20014. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20015. ' end;',
  20016. ' TObject = class(IUnknown)',
  20017. ' function _AddRef: longint; virtual; abstract;',
  20018. ' function _Release: longint; virtual; abstract;',
  20019. ' function GetBird: IUnknown; virtual; abstract;',
  20020. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20021. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20022. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20023. ' end;',
  20024. 'procedure DoIt;',
  20025. 'var',
  20026. ' o: TObject;',
  20027. ' v: IUnknown;',
  20028. 'begin',
  20029. ' v:=v.Items[1];',
  20030. ' v.Items[2]:=v;',
  20031. ' v.Items[3]:=v.Items[4];',
  20032. ' v:=v[5];',
  20033. ' v[6]:=v;',
  20034. ' v[7]:=v[8];',
  20035. ' v[9].Bird.Bird:=v;',
  20036. ' v:=v.Bird[10].Bird',
  20037. 'end;',
  20038. 'begin',
  20039. '']);
  20040. ConvertProgram;
  20041. CheckSource('TestClassInterface_COM_IntfProperty',
  20042. LinesToStr([ // statements
  20043. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20044. ' "_AddRef",',
  20045. ' "_Release",',
  20046. ' "GetBird",',
  20047. ' "SetBird",',
  20048. ' "GetItems",',
  20049. ' "SetItems"',
  20050. '], null);',
  20051. 'rtl.createClass(this, "TObject", null, function () {',
  20052. ' this.$init = function () {',
  20053. ' };',
  20054. ' this.$final = function () {',
  20055. ' };',
  20056. ' rtl.addIntf(this, $mod.IUnknown);',
  20057. '});',
  20058. 'this.DoIt = function () {',
  20059. ' var o = null;',
  20060. ' var v = null;',
  20061. ' var $ir = rtl.createIntfRefs();',
  20062. ' try {',
  20063. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20064. ' v.SetItems(2, v);',
  20065. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20066. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20067. ' v.SetItems(6, v);',
  20068. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20069. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20070. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20071. ' } finally {',
  20072. ' $ir.free();',
  20073. ' rtl._Release(v);',
  20074. ' };',
  20075. '};',
  20076. '']),
  20077. LinesToStr([ // $mod.$main
  20078. '']));
  20079. end;
  20080. procedure TTestModule.TestClassInterface_COM_Delegation;
  20081. begin
  20082. StartProgram(false);
  20083. Add([
  20084. '{$interfaces com}',
  20085. 'type',
  20086. ' IUnknown = interface',
  20087. ' function _AddRef: longint;',
  20088. ' function _Release: longint;',
  20089. ' end;',
  20090. ' IBird = interface(IUnknown)',
  20091. ' procedure Fly(s: string);',
  20092. ' end;',
  20093. ' IEagle = interface(IBird) end;',
  20094. ' IDove = interface(IBird) end;',
  20095. ' ISwallow = interface(IBird) end;',
  20096. ' TObject = class',
  20097. ' end;',
  20098. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20099. ' function _AddRef: longint; virtual; abstract;',
  20100. ' function _Release: longint; virtual; abstract;',
  20101. ' procedure Fly(s: string); virtual; abstract;',
  20102. ' end;',
  20103. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20104. ' function _AddRef: longint; virtual; abstract;',
  20105. ' function _Release: longint; virtual; abstract;',
  20106. ' FBirdIntf: IBird;',
  20107. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20108. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20109. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20110. ' FDoveObj: TBird;',
  20111. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20112. ' function GetSwallowObj: TBird; virtual; abstract;',
  20113. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20114. ' end;',
  20115. 'begin',
  20116. '']);
  20117. ConvertProgram;
  20118. CheckSource('TestClassInterface_COM_Delegation',
  20119. LinesToStr([ // statements
  20120. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20121. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20122. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20123. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20124. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20125. 'rtl.createClass(this, "TObject", null, function () {',
  20126. ' this.$init = function () {',
  20127. ' };',
  20128. ' this.$final = function () {',
  20129. ' };',
  20130. '});',
  20131. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20132. ' rtl.addIntf(this, $mod.IBird);',
  20133. ' rtl.addIntf(this, $mod.IEagle);',
  20134. ' rtl.addIntf(this, $mod.IDove);',
  20135. ' rtl.addIntf(this, $mod.ISwallow);',
  20136. '});',
  20137. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20138. ' this.$init = function () {',
  20139. ' $mod.TObject.$init.call(this);',
  20140. ' this.FBirdIntf = null;',
  20141. ' this.FDoveObj = null;',
  20142. ' };',
  20143. ' this.$final = function () {',
  20144. ' this.FBirdIntf = undefined;',
  20145. ' this.FDoveObj = undefined;',
  20146. ' $mod.TObject.$final.call(this);',
  20147. ' };',
  20148. ' this.$intfmaps = {',
  20149. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20150. ' return rtl._AddRef(this.FBirdIntf);',
  20151. ' },',
  20152. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20153. ' return this.GetEagleIntf();',
  20154. ' },',
  20155. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20156. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20157. ' },',
  20158. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20159. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20160. ' }',
  20161. ' };',
  20162. '});',
  20163. '']),
  20164. LinesToStr([ // $mod.$main
  20165. '']));
  20166. end;
  20167. procedure TTestModule.TestClassInterface_COM_With;
  20168. begin
  20169. StartProgram(false);
  20170. Add([
  20171. '{$interfaces com}',
  20172. 'type',
  20173. ' IUnknown = interface',
  20174. ' function _AddRef: longint;',
  20175. ' function _Release: longint;',
  20176. ' function GetAnt: IUnknown;',
  20177. ' property Ant: IUnknown read GetAnt;',
  20178. ' end;',
  20179. ' TObject = class(IUnknown)',
  20180. ' function _AddRef: longint; virtual; abstract;',
  20181. ' function _Release: longint; virtual; abstract;',
  20182. ' function GetAnt: IUnknown; virtual; abstract;',
  20183. ' property Ant: IUnknown read GetAnt;',
  20184. ' end;',
  20185. 'procedure DoIt;',
  20186. 'var',
  20187. ' i: IUnknown;',
  20188. 'begin',
  20189. ' with i do ',
  20190. ' GetAnt;',
  20191. ' with i.Ant, Ant do ',
  20192. ' GetAnt;',
  20193. 'end;',
  20194. 'begin',
  20195. '']);
  20196. ConvertProgram;
  20197. CheckSource('TestClassInterface_COM_With',
  20198. LinesToStr([ // statements
  20199. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20200. 'rtl.createClass(this, "TObject", null, function () {',
  20201. ' this.$init = function () {',
  20202. ' };',
  20203. ' this.$final = function () {',
  20204. ' };',
  20205. ' rtl.addIntf(this, $mod.IUnknown);',
  20206. '});',
  20207. 'this.DoIt = function () {',
  20208. ' var i = null;',
  20209. ' var $ir = rtl.createIntfRefs();',
  20210. ' try {',
  20211. ' $ir.ref(1, i.GetAnt());',
  20212. ' var $with = $ir.ref(2, i.GetAnt());',
  20213. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20214. ' $ir.ref(4, $with1.GetAnt());',
  20215. ' } finally {',
  20216. ' $ir.free();',
  20217. ' };',
  20218. '};',
  20219. '']),
  20220. LinesToStr([ // $mod.$main
  20221. '']));
  20222. end;
  20223. procedure TTestModule.TestClassInterface_COM_ForIn;
  20224. begin
  20225. StartProgram(false);
  20226. Add([
  20227. '{$interfaces com}',
  20228. 'type',
  20229. ' IUnknown = interface end;',
  20230. ' TObject = class',
  20231. ' Id: longint;',
  20232. ' end;',
  20233. ' IEnumerator = interface(IUnknown)',
  20234. ' function GetCurrent: TObject;',
  20235. ' function MoveNext: Boolean;',
  20236. ' property Current: TObject read GetCurrent;',
  20237. ' end;',
  20238. ' IEnumerable = interface(IUnknown)',
  20239. ' function GetEnumerator: IEnumerator;',
  20240. ' end;',
  20241. 'var',
  20242. ' o: TObject;',
  20243. ' i: IEnumerable;',
  20244. 'begin',
  20245. ' for o in i do o.Id:=3;',
  20246. '']);
  20247. ConvertProgram;
  20248. CheckSource('TestClassInterface_COM_ForIn',
  20249. LinesToStr([ // statements
  20250. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20251. 'rtl.createClass(this, "TObject", null, function () {',
  20252. ' this.$init = function () {',
  20253. ' this.Id = 0;',
  20254. ' };',
  20255. ' this.$final = function () {',
  20256. ' };',
  20257. '});',
  20258. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20259. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20260. 'this.o = null;',
  20261. 'this.i = null;',
  20262. '']),
  20263. LinesToStr([ // $mod.$main
  20264. 'var $in = $mod.i.GetEnumerator();',
  20265. 'try {',
  20266. ' while ($in.MoveNext()) {',
  20267. ' $mod.o = $in.GetCurrent();',
  20268. ' $mod.o.Id = 3;',
  20269. ' }',
  20270. '} finally {',
  20271. ' rtl._Release($in)',
  20272. '};',
  20273. '']));
  20274. end;
  20275. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20276. begin
  20277. StartProgram(false);
  20278. Add([
  20279. '{$interfaces com}',
  20280. 'type',
  20281. ' IUnknown = interface',
  20282. ' function _AddRef: longint;',
  20283. ' function _Release: longint;',
  20284. ' end;',
  20285. ' TObject = class',
  20286. ' end;',
  20287. ' TArrOfIntf = array of IUnknown;',
  20288. 'begin',
  20289. '']);
  20290. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20291. ConvertProgram;
  20292. end;
  20293. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20294. begin
  20295. StartProgram(false);
  20296. Add([
  20297. '{$interfaces com}',
  20298. 'type',
  20299. ' IUnknown = interface',
  20300. ' function _AddRef: longint;',
  20301. ' function _Release: longint;',
  20302. ' end;',
  20303. ' TRec = record',
  20304. ' i: IUnknown;',
  20305. ' end;',
  20306. 'begin',
  20307. '']);
  20308. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20309. ConvertProgram;
  20310. end;
  20311. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20312. begin
  20313. StartUnit(false);
  20314. Add([
  20315. '{$interfaces com}',
  20316. 'interface',
  20317. 'implementation',
  20318. 'type',
  20319. ' IUnknown = interface',
  20320. ' function _AddRef: longint;',
  20321. ' end;',
  20322. ' TObject = class(IUnknown)',
  20323. ' function _AddRef: longint;',
  20324. ' end;',
  20325. 'function TObject._AddRef: longint; begin end;',
  20326. 'var i: IUnknown;',
  20327. ' o: TObject;',
  20328. 'initialization',
  20329. ' i:=nil;',
  20330. ' i:=i;',
  20331. ' i:=o;',
  20332. ' if (o as IUnknown)=nil then ;',
  20333. '']);
  20334. ConvertUnit;
  20335. CheckSource('TestClassInterface_COM_UnitInitialization',
  20336. LinesToStr([ // statements
  20337. 'var $impl = $mod.$impl;',
  20338. '']),
  20339. LinesToStr([ // this.$init
  20340. 'var $ir = rtl.createIntfRefs();',
  20341. 'try {',
  20342. ' rtl.setIntfP($impl, "i", null);',
  20343. ' rtl.setIntfP($impl, "i", $impl.i);',
  20344. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20345. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20346. '} finally {',
  20347. ' $ir.free();',
  20348. '};',
  20349. '']),
  20350. LinesToStr([ // implementation
  20351. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20352. 'rtl.createClass($impl, "TObject", null, function () {',
  20353. ' this.$init = function () {',
  20354. ' };',
  20355. ' this.$final = function () {',
  20356. ' };',
  20357. ' this._AddRef = function () {',
  20358. ' var Result = 0;',
  20359. ' return Result;',
  20360. ' };',
  20361. ' rtl.addIntf(this, $impl.IUnknown);',
  20362. '});',
  20363. '$impl.i = null;',
  20364. '$impl.o = null;',
  20365. ''])
  20366. );
  20367. end;
  20368. procedure TTestModule.TestClassInterface_GUID;
  20369. begin
  20370. StartProgram(false);
  20371. Add([
  20372. '{$interfaces corba}',
  20373. 'type',
  20374. ' IUnknown = interface',
  20375. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20376. ' end;',
  20377. ' TObject = class end;',
  20378. ' TGUID = record D1, D2, D3, D4: word; end;',
  20379. ' TAliasGUID = TGUID;',
  20380. ' TGUIDString = type string;',
  20381. ' TAliasGUIDString = TGUIDString;',
  20382. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20383. 'begin end;',
  20384. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20385. 'begin end;',
  20386. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20387. 'begin end;',
  20388. 'var',
  20389. ' i: IUnknown;',
  20390. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20391. ' s: TAliasGUIDString;',
  20392. 'begin',
  20393. ' DoConstGUIDIt(IUnknown);',
  20394. ' DoDefGUID(IUnknown);',
  20395. ' DoStr(IUnknown);',
  20396. ' DoConstGUIDIt(i);',
  20397. ' DoDefGUID(i);',
  20398. ' DoStr(i);',
  20399. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20400. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20401. ' DoStr(g);',
  20402. ' g:=i;',
  20403. ' g:=IUnknown;',
  20404. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20405. ' s:=i;',
  20406. ' s:=IUnknown;',
  20407. ' s:=g;',
  20408. ' if g=i then ;',
  20409. ' if i=g then ;',
  20410. ' if g=IUnknown then ;',
  20411. ' if IUnknown=g then ;',
  20412. ' if s=i then ;',
  20413. ' if i=s then ;',
  20414. ' if s=IUnknown then ;',
  20415. ' if IUnknown=s then ;',
  20416. ' if s=g then ;',
  20417. ' if g=s then ;',
  20418. '']);
  20419. ConvertProgram;
  20420. CheckSource('TestClassInterface_GUID',
  20421. LinesToStr([ // statements
  20422. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20423. 'rtl.createClass(this, "TObject", null, function () {',
  20424. ' this.$init = function () {',
  20425. ' };',
  20426. ' this.$final = function () {',
  20427. ' };',
  20428. '});',
  20429. 'rtl.recNewT(this, "TGUID", function () {',
  20430. ' this.D1 = 0;',
  20431. ' this.D2 = 0;',
  20432. ' this.D3 = 0;',
  20433. ' this.D4 = 0;',
  20434. ' this.$eq = function (b) {',
  20435. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20436. ' };',
  20437. ' this.$assign = function (s) {',
  20438. ' this.D1 = s.D1;',
  20439. ' this.D2 = s.D2;',
  20440. ' this.D3 = s.D3;',
  20441. ' this.D4 = s.D4;',
  20442. ' return this;',
  20443. ' };',
  20444. '});',
  20445. 'this.DoConstGUIDIt = function (g) {',
  20446. '};',
  20447. 'this.DoDefGUID = function (g) {',
  20448. '};',
  20449. 'this.DoStr = function (s) {',
  20450. '};',
  20451. 'this.i = null;',
  20452. 'this.g = this.TGUID.$clone({',
  20453. ' D1: 0xD91C9AF4,',
  20454. ' D2: 0x3C93,',
  20455. ' D3: 0x420F,',
  20456. ' D4: [',
  20457. ' 0xA3,',
  20458. ' 0x03,',
  20459. ' 0xBF,',
  20460. ' 0x5B,',
  20461. ' 0xA8,',
  20462. ' 0x2B,',
  20463. ' 0xFD,',
  20464. ' 0x23',
  20465. ' ]',
  20466. '});',
  20467. 'this.s = "";',
  20468. '']),
  20469. LinesToStr([ // $mod.$main
  20470. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20471. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20472. '$mod.DoStr($mod.IUnknown.$guid);',
  20473. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20474. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20475. '$mod.DoStr($mod.i.$guid);',
  20476. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20477. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20478. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20479. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20480. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20481. '$mod.g.$assign({',
  20482. ' D1: 0xD91C9AF4,',
  20483. ' D2: 0x3C93,',
  20484. ' D3: 0x420F,',
  20485. ' D4: [',
  20486. ' 0xA3,',
  20487. ' 0x03,',
  20488. ' 0xBF,',
  20489. ' 0x5B,',
  20490. ' 0xA8,',
  20491. ' 0x2B,',
  20492. ' 0xFD,',
  20493. ' 0x23',
  20494. ' ]',
  20495. '});',
  20496. '$mod.s = $mod.i.$guid;',
  20497. '$mod.s = $mod.IUnknown.$guid;',
  20498. '$mod.s = rtl.guidrToStr($mod.g);',
  20499. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20500. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20501. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20502. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20503. 'if ($mod.s === $mod.i.$guid) ;',
  20504. 'if ($mod.i.$guid === $mod.s) ;',
  20505. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20506. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20507. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20508. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20509. '']));
  20510. end;
  20511. procedure TTestModule.TestClassInterface_GUIDProperty;
  20512. begin
  20513. StartProgram(false);
  20514. Add([
  20515. '{$interfaces corba}',
  20516. 'type',
  20517. ' IUnknown = interface',
  20518. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20519. ' end;',
  20520. ' TGUID = record D1, D2, D3, D4: word; end;',
  20521. ' TAliasGUID = TGUID;',
  20522. ' TGUIDString = type string;',
  20523. ' TAliasGUIDString = TGUIDString;',
  20524. ' TObject = class',
  20525. ' function GetG: TAliasGUID; virtual; abstract;',
  20526. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20527. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20528. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20529. ' property g: TAliasGUID read GetG write SetG;',
  20530. ' property s: TAliasGUIDString read GetS write SetS;',
  20531. ' end;',
  20532. 'var o: TObject;',
  20533. 'begin',
  20534. ' o.g:=IUnknown;',
  20535. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20536. ' o.s:=IUnknown;',
  20537. ' o.s:=o.g;',
  20538. '']);
  20539. ConvertProgram;
  20540. CheckSource('TestClassInterface_GUIDProperty',
  20541. LinesToStr([ // statements
  20542. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20543. 'rtl.recNewT(this, "TGUID", function () {',
  20544. ' this.D1 = 0;',
  20545. ' this.D2 = 0;',
  20546. ' this.D3 = 0;',
  20547. ' this.D4 = 0;',
  20548. ' this.$eq = function (b) {',
  20549. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20550. ' };',
  20551. ' this.$assign = function (s) {',
  20552. ' this.D1 = s.D1;',
  20553. ' this.D2 = s.D2;',
  20554. ' this.D3 = s.D3;',
  20555. ' this.D4 = s.D4;',
  20556. ' return this;',
  20557. ' };',
  20558. '});',
  20559. 'rtl.createClass(this, "TObject", null, function () {',
  20560. ' this.$init = function () {',
  20561. ' };',
  20562. ' this.$final = function () {',
  20563. ' };',
  20564. '});',
  20565. 'this.o = null;',
  20566. '']),
  20567. LinesToStr([ // $mod.$main
  20568. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20569. '$mod.o.SetG({',
  20570. ' D1: 0xD91C9AF4,',
  20571. ' D2: 0x3C93,',
  20572. ' D3: 0x420F,',
  20573. ' D4: [',
  20574. ' 0xA3,',
  20575. ' 0x03,',
  20576. ' 0xBF,',
  20577. ' 0x5B,',
  20578. ' 0xA8,',
  20579. ' 0x2B,',
  20580. ' 0xFD,',
  20581. ' 0x23',
  20582. ' ]',
  20583. '});',
  20584. '$mod.o.SetS($mod.IUnknown.$guid);',
  20585. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20586. '']));
  20587. end;
  20588. procedure TTestModule.TestClassHelper_ClassVar;
  20589. begin
  20590. StartProgram(false);
  20591. Add([
  20592. 'type',
  20593. ' TObject = class',
  20594. ' end;',
  20595. ' THelper = class helper for TObject',
  20596. ' const',
  20597. ' One = 1;',
  20598. ' Two: word = 2;',
  20599. ' class var',
  20600. ' Glob: word;',
  20601. ' function Foo(w: word): word;',
  20602. ' class function Bar(w: word): word;',
  20603. ' end;',
  20604. 'function THelper.foo(w: word): word;',
  20605. 'begin',
  20606. ' Result:=w;',
  20607. ' Two:=One+w;',
  20608. ' Glob:=Glob;',
  20609. ' Result:=Self.Glob;',
  20610. ' Self.Glob:=Self.Glob;',
  20611. ' with Self do Glob:=Glob;',
  20612. 'end;',
  20613. 'class function THelper.bar(w: word): word;',
  20614. 'begin',
  20615. ' Result:=w;',
  20616. ' Two:=One;',
  20617. ' Glob:=Glob;',
  20618. ' Self.Glob:=Self.Glob;',
  20619. ' with Self do Glob:=Glob;',
  20620. 'end;',
  20621. 'var o: TObject;',
  20622. 'begin',
  20623. ' tobject.two:=tobject.one;',
  20624. ' tobject.Glob:=tobject.Glob;',
  20625. ' with tobject do begin',
  20626. ' two:=one;',
  20627. ' Glob:=Glob;',
  20628. ' end;',
  20629. ' o.two:=o.one;',
  20630. ' o.Glob:=o.Glob;',
  20631. ' with o do begin',
  20632. ' two:=one;',
  20633. ' Glob:=Glob;',
  20634. ' end;',
  20635. '']);
  20636. ConvertProgram;
  20637. CheckSource('TestClassHelper_ClassVar',
  20638. LinesToStr([ // statements
  20639. 'rtl.createClass(this, "TObject", null, function () {',
  20640. ' this.$init = function () {',
  20641. ' };',
  20642. ' this.$final = function () {',
  20643. ' };',
  20644. '});',
  20645. 'rtl.createHelper(this, "THelper", null, function () {',
  20646. ' this.One = 1;',
  20647. ' this.Two = 2;',
  20648. ' this.Glob = 0;',
  20649. ' this.Foo = function (w) {',
  20650. ' var Result = 0;',
  20651. ' Result = w;',
  20652. ' $mod.THelper.Two = 1 + w;',
  20653. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20654. ' Result = $mod.THelper.Glob;',
  20655. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20656. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20657. ' return Result;',
  20658. ' };',
  20659. ' this.Bar = function (w) {',
  20660. ' var Result = 0;',
  20661. ' Result = w;',
  20662. ' $mod.THelper.Two = 1;',
  20663. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20664. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20665. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20666. ' return Result;',
  20667. ' };',
  20668. '});',
  20669. 'this.o = null;',
  20670. '']),
  20671. LinesToStr([ // $mod.$main
  20672. '$mod.THelper.Two = 1;',
  20673. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20674. 'var $with = $mod.TObject;',
  20675. '$mod.THelper.Two = 1;',
  20676. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20677. '$mod.THelper.Two = 1;',
  20678. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20679. 'var $with1 = $mod.o;',
  20680. '$mod.THelper.Two = 1;',
  20681. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20682. '']));
  20683. end;
  20684. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20685. begin
  20686. StartProgram(false);
  20687. Add([
  20688. 'type',
  20689. ' TObject = class',
  20690. ' FSize: word;',
  20691. ' property Size: word read FSize write FSize;',
  20692. ' end;',
  20693. ' THelper = class helper for TObject',
  20694. ' function Foo(w: word = 1): word;',
  20695. ' end;',
  20696. 'function THelper.foo(w: word): word;',
  20697. 'begin',
  20698. ' Result:=Size;',
  20699. ' Size:=Size+2;',
  20700. ' Self.Size:=Self.Size+3;',
  20701. ' FSize:=FSize+4;',
  20702. ' Self.FSize:=Self.FSize+5;',
  20703. ' with Self do begin',
  20704. ' Size:=Size+6;',
  20705. ' FSize:=FSize+7;',
  20706. ' FSize:=FSize+8;',
  20707. ' end;',
  20708. 'end;',
  20709. 'begin',
  20710. '']);
  20711. ConvertProgram;
  20712. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20713. LinesToStr([ // statements
  20714. 'rtl.createClass(this, "TObject", null, function () {',
  20715. ' this.$init = function () {',
  20716. ' this.FSize = 0;',
  20717. ' };',
  20718. ' this.$final = function () {',
  20719. ' };',
  20720. '});',
  20721. 'rtl.createHelper(this, "THelper", null, function () {',
  20722. ' this.Foo = function (w) {',
  20723. ' var Result = 0;',
  20724. ' Result = this.FSize;',
  20725. ' this.FSize = this.FSize + 2;',
  20726. ' this.FSize = this.FSize + 3;',
  20727. ' this.FSize = this.FSize + 4;',
  20728. ' this.FSize = this.FSize + 5;',
  20729. ' this.FSize = this.FSize + 6;',
  20730. ' this.FSize = this.FSize + 7;',
  20731. ' this.FSize = this.FSize + 8;',
  20732. ' return Result;',
  20733. ' };',
  20734. '});',
  20735. '']),
  20736. LinesToStr([ // $mod.$main
  20737. '']));
  20738. end;
  20739. procedure TTestModule.TestClassHelper_Method_Call;
  20740. begin
  20741. StartProgram(false);
  20742. Add([
  20743. 'type',
  20744. ' TObject = class',
  20745. ' procedure Run(w: word = 10);',
  20746. ' end;',
  20747. ' THelper = class helper for TObject',
  20748. ' function Foo(w: word = 1): word;',
  20749. ' end;',
  20750. 'procedure TObject.Run(w: word);',
  20751. 'var o: TObject;',
  20752. 'begin',
  20753. ' Foo;',
  20754. ' Foo();',
  20755. ' Foo(2);',
  20756. ' Self.Foo;',
  20757. ' Self.Foo();',
  20758. ' Self.Foo(3);',
  20759. ' with Self do begin',
  20760. ' Foo;',
  20761. ' Foo();',
  20762. ' Foo(4);',
  20763. ' end;',
  20764. ' with o do Foo(5);',
  20765. 'end;',
  20766. 'function THelper.foo(w: word): word;',
  20767. 'begin',
  20768. ' Run;',
  20769. ' Run();',
  20770. ' Run(11);',
  20771. ' Foo;',
  20772. ' Foo();',
  20773. ' Foo(12);',
  20774. ' Self.Foo;',
  20775. ' Self.Foo();',
  20776. ' Self.Foo(13);',
  20777. ' with Self do begin',
  20778. ' Foo;',
  20779. ' Foo();',
  20780. ' Foo(14);',
  20781. ' end;',
  20782. 'end;',
  20783. 'var Obj: TObject;',
  20784. 'begin',
  20785. ' obj.Foo;',
  20786. ' obj.Foo();',
  20787. ' obj.Foo(21);',
  20788. ' with obj do begin',
  20789. ' Foo;',
  20790. ' Foo();',
  20791. ' Foo(22);',
  20792. ' end;',
  20793. '']);
  20794. ConvertProgram;
  20795. CheckSource('TestClassHelper_Method_Call',
  20796. LinesToStr([ // statements
  20797. 'rtl.createClass(this, "TObject", null, function () {',
  20798. ' this.$init = function () {',
  20799. ' };',
  20800. ' this.$final = function () {',
  20801. ' };',
  20802. ' this.Run = function (w) {',
  20803. ' var o = null;',
  20804. ' $mod.THelper.Foo.call(this, 1);',
  20805. ' $mod.THelper.Foo.call(this, 1);',
  20806. ' $mod.THelper.Foo.call(this, 2);',
  20807. ' $mod.THelper.Foo.call(this, 1);',
  20808. ' $mod.THelper.Foo.call(this, 1);',
  20809. ' $mod.THelper.Foo.call(this, 3);',
  20810. ' $mod.THelper.Foo.call(this, 1);',
  20811. ' $mod.THelper.Foo.call(this, 1);',
  20812. ' $mod.THelper.Foo.call(this, 4);',
  20813. ' $mod.THelper.Foo.call(o, 5);',
  20814. ' };',
  20815. '});',
  20816. 'rtl.createHelper(this, "THelper", null, function () {',
  20817. ' this.Foo = function (w) {',
  20818. ' var Result = 0;',
  20819. ' this.Run(10);',
  20820. ' this.Run(10);',
  20821. ' this.Run(11);',
  20822. ' $mod.THelper.Foo.call(this, 1);',
  20823. ' $mod.THelper.Foo.call(this, 1);',
  20824. ' $mod.THelper.Foo.call(this, 12);',
  20825. ' $mod.THelper.Foo.call(this, 1);',
  20826. ' $mod.THelper.Foo.call(this, 1);',
  20827. ' $mod.THelper.Foo.call(this, 13);',
  20828. ' $mod.THelper.Foo.call(this, 1);',
  20829. ' $mod.THelper.Foo.call(this, 1);',
  20830. ' $mod.THelper.Foo.call(this, 14);',
  20831. ' return Result;',
  20832. ' };',
  20833. '});',
  20834. 'this.Obj = null;',
  20835. '']),
  20836. LinesToStr([ // $mod.$main
  20837. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20838. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20839. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20840. 'var $with = $mod.Obj;',
  20841. '$mod.THelper.Foo.call($with, 1);',
  20842. '$mod.THelper.Foo.call($with, 1);',
  20843. '$mod.THelper.Foo.call($with, 22);',
  20844. '']));
  20845. end;
  20846. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20847. begin
  20848. StartProgram(false);
  20849. Add([
  20850. 'type',
  20851. ' TObject = class',
  20852. ' procedure Run(w: word = 10);',
  20853. ' end;',
  20854. ' THelper = class helper for TObject',
  20855. ' function Foo(w: word = 1): word;',
  20856. ' end;',
  20857. 'procedure TObject.Run(w: word);',
  20858. ' procedure Sub(Self: TObject);',
  20859. ' begin',
  20860. ' Foo;',
  20861. ' Foo();',
  20862. ' Self.Foo;',
  20863. ' Self.Foo();',
  20864. ' with Self do begin',
  20865. ' Foo;',
  20866. ' Foo();',
  20867. ' end;',
  20868. ' end;',
  20869. 'begin',
  20870. 'end;',
  20871. 'function THelper.foo(w: word): word;',
  20872. ' procedure Sub(Self: TObject);',
  20873. ' begin',
  20874. ' Run;',
  20875. ' Run();',
  20876. ' Foo;',
  20877. ' Foo();',
  20878. ' Self.Foo;',
  20879. ' Self.Foo();',
  20880. ' with Self do begin',
  20881. ' Foo;',
  20882. ' Foo();',
  20883. ' end;',
  20884. ' end;',
  20885. 'begin',
  20886. 'end;',
  20887. 'begin',
  20888. '']);
  20889. ConvertProgram;
  20890. CheckSource('TestClassHelper_Method_Nested_Call',
  20891. LinesToStr([ // statements
  20892. 'rtl.createClass(this, "TObject", null, function () {',
  20893. ' this.$init = function () {',
  20894. ' };',
  20895. ' this.$final = function () {',
  20896. ' };',
  20897. ' this.Run = function (w) {',
  20898. ' var $Self = this;',
  20899. ' function Sub(Self) {',
  20900. ' $mod.THelper.Foo.call($Self, 1);',
  20901. ' $mod.THelper.Foo.call($Self, 1);',
  20902. ' $mod.THelper.Foo.call(Self, 1);',
  20903. ' $mod.THelper.Foo.call(Self, 1);',
  20904. ' $mod.THelper.Foo.call(Self, 1);',
  20905. ' $mod.THelper.Foo.call(Self, 1);',
  20906. ' };',
  20907. ' };',
  20908. '});',
  20909. 'rtl.createHelper(this, "THelper", null, function () {',
  20910. ' this.Foo = function (w) {',
  20911. ' var $Self = this;',
  20912. ' var Result = 0;',
  20913. ' function Sub(Self) {',
  20914. ' $Self.Run(10);',
  20915. ' $Self.Run(10);',
  20916. ' $mod.THelper.Foo.call($Self, 1);',
  20917. ' $mod.THelper.Foo.call($Self, 1);',
  20918. ' $mod.THelper.Foo.call(Self, 1);',
  20919. ' $mod.THelper.Foo.call(Self, 1);',
  20920. ' $mod.THelper.Foo.call(Self, 1);',
  20921. ' $mod.THelper.Foo.call(Self, 1);',
  20922. ' };',
  20923. ' return Result;',
  20924. ' };',
  20925. '});',
  20926. '']),
  20927. LinesToStr([ // $mod.$main
  20928. '']));
  20929. end;
  20930. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20931. begin
  20932. StartProgram(false);
  20933. Add([
  20934. 'type',
  20935. ' TObject = class',
  20936. ' class procedure Run(w: word = 10);',
  20937. ' end;',
  20938. ' THelper = class helper for TObject',
  20939. ' class function Foo(w: word = 1): word;',
  20940. ' end;',
  20941. 'class procedure TObject.Run(w: word);',
  20942. 'begin',
  20943. ' Foo;',
  20944. ' Foo();',
  20945. ' Self.Foo;',
  20946. ' Self.Foo();',
  20947. ' with Self do begin',
  20948. ' Foo;',
  20949. ' Foo();',
  20950. ' end;',
  20951. 'end;',
  20952. 'class function THelper.foo(w: word): word;',
  20953. 'begin',
  20954. ' Run;',
  20955. ' Run();',
  20956. ' Foo;',
  20957. ' Foo();',
  20958. ' Self.Foo;',
  20959. ' Self.Foo();',
  20960. ' with Self do begin',
  20961. ' Foo;',
  20962. ' Foo();',
  20963. ' end;',
  20964. 'end;',
  20965. 'var',
  20966. ' Obj: TObject;',
  20967. 'begin',
  20968. ' obj.Foo;',
  20969. ' obj.Foo();',
  20970. ' with obj do begin',
  20971. ' Foo;',
  20972. ' Foo();',
  20973. ' end;',
  20974. ' tobject.Foo;',
  20975. ' tobject.Foo();',
  20976. ' with tobject do begin',
  20977. ' Foo;',
  20978. ' Foo();',
  20979. ' end;',
  20980. '']);
  20981. ConvertProgram;
  20982. CheckSource('TestClassHelper_ClassMethod_Call',
  20983. LinesToStr([ // statements
  20984. 'rtl.createClass(this, "TObject", null, function () {',
  20985. ' this.$init = function () {',
  20986. ' };',
  20987. ' this.$final = function () {',
  20988. ' };',
  20989. ' this.Run = function (w) {',
  20990. ' $mod.THelper.Foo.call(this, 1);',
  20991. ' $mod.THelper.Foo.call(this, 1);',
  20992. ' $mod.THelper.Foo.call(this, 1);',
  20993. ' $mod.THelper.Foo.call(this, 1);',
  20994. ' $mod.THelper.Foo.call(this, 1);',
  20995. ' $mod.THelper.Foo.call(this, 1);',
  20996. ' };',
  20997. '});',
  20998. 'rtl.createHelper(this, "THelper", null, function () {',
  20999. ' this.Foo = function (w) {',
  21000. ' var Result = 0;',
  21001. ' this.Run(10);',
  21002. ' this.Run(10);',
  21003. ' $mod.THelper.Foo.call(this, 1);',
  21004. ' $mod.THelper.Foo.call(this, 1);',
  21005. ' $mod.THelper.Foo.call(this, 1);',
  21006. ' $mod.THelper.Foo.call(this, 1);',
  21007. ' $mod.THelper.Foo.call(this, 1);',
  21008. ' $mod.THelper.Foo.call(this, 1);',
  21009. ' return Result;',
  21010. ' };',
  21011. '});',
  21012. 'this.Obj = null;',
  21013. '']),
  21014. LinesToStr([ // $mod.$main
  21015. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21016. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21017. 'var $with = $mod.Obj;',
  21018. '$mod.THelper.Foo.call($with.$class, 1);',
  21019. '$mod.THelper.Foo.call($with.$class, 1);',
  21020. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21021. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21022. 'var $with1 = $mod.TObject;',
  21023. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21024. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21025. '']));
  21026. end;
  21027. procedure TTestModule.TestClassHelper_ClassOf;
  21028. begin
  21029. StartProgram(false);
  21030. Add([
  21031. 'type',
  21032. ' TObject = class',
  21033. ' end;',
  21034. ' TClass = class of TObject;',
  21035. ' THelper = class helper for TObject',
  21036. ' class function Foo(w: word = 1): word;',
  21037. ' end;',
  21038. 'class function THelper.foo(w: word): word;',
  21039. 'begin',
  21040. 'end;',
  21041. 'var',
  21042. ' c: TClass;',
  21043. 'begin',
  21044. ' c.Foo;',
  21045. ' c.Foo();',
  21046. ' with c do begin',
  21047. ' Foo;',
  21048. ' Foo();',
  21049. ' end;',
  21050. '']);
  21051. ConvertProgram;
  21052. CheckSource('TestClassHelper_ClassOf',
  21053. LinesToStr([ // statements
  21054. 'rtl.createClass(this, "TObject", null, function () {',
  21055. ' this.$init = function () {',
  21056. ' };',
  21057. ' this.$final = function () {',
  21058. ' };',
  21059. '});',
  21060. 'rtl.createHelper(this, "THelper", null, function () {',
  21061. ' this.Foo = function (w) {',
  21062. ' var Result = 0;',
  21063. ' return Result;',
  21064. ' };',
  21065. '});',
  21066. 'this.c = null;',
  21067. '']),
  21068. LinesToStr([ // $mod.$main
  21069. '$mod.THelper.Foo.call($mod.c, 1);',
  21070. '$mod.THelper.Foo.call($mod.c, 1);',
  21071. 'var $with = $mod.c;',
  21072. '$mod.THelper.Foo.call($with, 1);',
  21073. '$mod.THelper.Foo.call($with, 1);',
  21074. '']));
  21075. end;
  21076. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21077. begin
  21078. StartProgram(false);
  21079. Add([
  21080. '{$mode objfpc}',
  21081. 'type',
  21082. ' TObject = class',
  21083. ' procedure DoIt;',
  21084. ' end;',
  21085. ' THelper = class helper for TObject',
  21086. ' procedure Fly(w: word = 1);',
  21087. ' class procedure Glide(w: word = 1);',
  21088. ' class procedure Run(w: word = 1); static;',
  21089. ' end;',
  21090. ' TFly = procedure(w: word) of object;',
  21091. ' TGlide = TFly;',
  21092. ' TRun = procedure(w: word);',
  21093. 'var',
  21094. ' f: TFly;',
  21095. ' g: TGlide;',
  21096. ' r: TRun;',
  21097. 'procedure TObject.DoIt;',
  21098. 'begin',
  21099. ' f:=@fly;',
  21100. ' g:=@glide;',
  21101. ' r:=@run;',
  21102. ' f:[email protected];',
  21103. ' g:[email protected];',
  21104. ' r:[email protected];',
  21105. ' with self do begin',
  21106. ' f:=@fly;',
  21107. ' g:=@glide;',
  21108. ' r:=@run;',
  21109. ' end;',
  21110. 'end;',
  21111. 'procedure THelper.fly(w: word);',
  21112. 'begin',
  21113. ' f:=@fly;',
  21114. ' g:=@glide;',
  21115. ' r:=@run;',
  21116. 'end;',
  21117. 'class procedure THelper.glide(w: word);',
  21118. 'begin',
  21119. ' g:=@glide;',
  21120. ' r:=@run;',
  21121. 'end;',
  21122. 'class procedure THelper.run(w: word);',
  21123. 'begin',
  21124. ' g:=@glide;',
  21125. ' r:=@run;',
  21126. 'end;',
  21127. 'var',
  21128. ' Obj: TObject;',
  21129. 'begin',
  21130. ' f:[email protected];',
  21131. ' g:[email protected];',
  21132. ' r:[email protected];',
  21133. ' with obj do begin',
  21134. ' f:=@fly;',
  21135. ' g:=@glide;',
  21136. ' r:=@run;',
  21137. ' end;',
  21138. ' g:[email protected];',
  21139. ' r:[email protected];',
  21140. ' with tobject do begin',
  21141. ' g:=@glide;',
  21142. ' r:=@run;',
  21143. ' end;',
  21144. '']);
  21145. ConvertProgram;
  21146. CheckSource('TestClassHelper_MethodRefObjFPC',
  21147. LinesToStr([ // statements
  21148. 'rtl.createClass(this, "TObject", null, function () {',
  21149. ' this.$init = function () {',
  21150. ' };',
  21151. ' this.$final = function () {',
  21152. ' };',
  21153. ' this.DoIt = function () {',
  21154. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21155. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21156. ' $mod.r = $mod.THelper.Run;',
  21157. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21158. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21159. ' $mod.r = $mod.THelper.Run;',
  21160. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21161. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21162. ' $mod.r = $mod.THelper.Run;',
  21163. ' };',
  21164. '});',
  21165. 'rtl.createHelper(this, "THelper", null, function () {',
  21166. ' this.Fly = function (w) {',
  21167. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21168. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21169. ' $mod.r = $mod.THelper.Run;',
  21170. ' };',
  21171. ' this.Glide = function (w) {',
  21172. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21173. ' $mod.r = $mod.THelper.Run;',
  21174. ' };',
  21175. ' this.Run = function (w) {',
  21176. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21177. ' $mod.r = $mod.THelper.Run;',
  21178. ' };',
  21179. '});',
  21180. 'this.f = null;',
  21181. 'this.g = null;',
  21182. 'this.r = null;',
  21183. 'this.Obj = null;',
  21184. '']),
  21185. LinesToStr([ // $mod.$main
  21186. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21187. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21188. '$mod.r = $mod.THelper.Run;',
  21189. 'var $with = $mod.Obj;',
  21190. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21191. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21192. '$mod.r = $mod.THelper.Run;',
  21193. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21194. '$mod.r = $mod.THelper.Run;',
  21195. 'var $with1 = $mod.TObject;',
  21196. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21197. '$mod.r = $mod.THelper.Run;',
  21198. '']));
  21199. end;
  21200. procedure TTestModule.TestClassHelper_Constructor;
  21201. begin
  21202. StartProgram(false);
  21203. Add([
  21204. 'type',
  21205. ' TObject = class',
  21206. ' constructor Create;',
  21207. ' end;',
  21208. ' TClass = class of TObject;',
  21209. ' THelper = class helper for TObject',
  21210. ' constructor NewHlp(w: word);',
  21211. ' end;',
  21212. 'var',
  21213. ' obj: TObject;',
  21214. ' c: TClass;',
  21215. 'constructor TObject.Create;',
  21216. 'begin',
  21217. ' NewHlp(2);', // normal call
  21218. ' tobject.NewHlp(3);', // new instance
  21219. ' c.newhlp(4);', // new instance
  21220. 'end;',
  21221. 'constructor THelper.NewHlp(w: word);',
  21222. 'begin',
  21223. ' create;', // normal call
  21224. ' tobject.create;', // new instance
  21225. ' NewHlp(2);', // normal call
  21226. ' tobject.NewHlp(3);', // new instance
  21227. ' c.newhlp(4);', // new instance
  21228. 'end;',
  21229. 'begin',
  21230. ' obj.newhlp(2);', // normal call
  21231. ' with Obj do newhlp(12);', // normal call
  21232. ' tobject.newhlp(3);', // new instance
  21233. ' with tobject do newhlp(13);', // new instance
  21234. ' c.newhlp(4);', // new instance
  21235. ' with c do newhlp(14);', // new instance
  21236. '']);
  21237. ConvertProgram;
  21238. CheckSource('TestClassHelper_Constructor',
  21239. LinesToStr([ // statements
  21240. 'rtl.createClass(this, "TObject", null, function () {',
  21241. ' this.$init = function () {',
  21242. ' };',
  21243. ' this.$final = function () {',
  21244. ' };',
  21245. ' this.Create = function () {',
  21246. ' $mod.THelper.NewHlp.call(this, 2);',
  21247. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21248. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21249. ' return this;',
  21250. ' };',
  21251. '});',
  21252. 'rtl.createHelper(this, "THelper", null, function () {',
  21253. ' this.NewHlp = function (w) {',
  21254. ' this.Create();',
  21255. ' $mod.TObject.$create("Create");',
  21256. ' $mod.THelper.NewHlp.call(this, 2);',
  21257. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21258. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21259. ' return this;',
  21260. ' };',
  21261. '});',
  21262. 'this.obj = null;',
  21263. 'this.c = null;',
  21264. '']),
  21265. LinesToStr([ // $mod.$main
  21266. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21267. 'var $with = $mod.obj;',
  21268. '$mod.THelper.NewHlp.call($with, 12);',
  21269. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21270. 'var $with1 = $mod.TObject;',
  21271. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21272. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21273. 'var $with2 = $mod.c;',
  21274. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21275. '']));
  21276. end;
  21277. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21278. begin
  21279. StartProgram(false);
  21280. Add([
  21281. 'type',
  21282. ' TObject = class',
  21283. ' procedure Fly;',
  21284. ' end;',
  21285. ' TObjHelper = class helper for TObject',
  21286. ' procedure Fly;',
  21287. ' end;',
  21288. ' TBird = class',
  21289. ' procedure Fly;',
  21290. ' end;',
  21291. ' TBirdHelper = class helper for TBird',
  21292. ' procedure Fly;',
  21293. ' procedure Walk(w: word);',
  21294. ' end;',
  21295. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21296. ' procedure Fly;',
  21297. ' procedure Walk(w: word);',
  21298. ' end;',
  21299. 'procedure Tobject.fly;',
  21300. 'begin',
  21301. ' inherited;', // ignore
  21302. 'end;',
  21303. 'procedure Tobjhelper.fly;',
  21304. 'begin',
  21305. ' {@TObject_Fly}inherited;',
  21306. ' inherited {@TObject_Fly}Fly;',
  21307. 'end;',
  21308. 'procedure Tbird.fly;',
  21309. 'begin',
  21310. ' {@TObjHelper_Fly}inherited;',
  21311. ' inherited {@TObjHelper_Fly}Fly;',
  21312. 'end;',
  21313. 'procedure Tbirdhelper.fly;',
  21314. 'begin',
  21315. ' {@TBird_Fly}inherited;',
  21316. ' inherited {@TBird_Fly}Fly;',
  21317. 'end;',
  21318. 'procedure Tbirdhelper.walk(w: word);',
  21319. 'begin',
  21320. 'end;',
  21321. 'procedure teagleHelper.fly;',
  21322. 'begin',
  21323. ' {@TBird_Fly}inherited;',
  21324. ' inherited {@TBird_Fly}Fly;',
  21325. 'end;',
  21326. 'procedure teagleHelper.walk(w: word);',
  21327. 'begin',
  21328. ' {@TBirdHelper_Walk}inherited;',
  21329. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21330. 'end;',
  21331. 'begin',
  21332. '']);
  21333. ConvertProgram;
  21334. CheckSource('TestClassHelper_InheritedObjFPC',
  21335. LinesToStr([ // statements
  21336. 'rtl.createClass(this, "TObject", null, function () {',
  21337. ' this.$init = function () {',
  21338. ' };',
  21339. ' this.$final = function () {',
  21340. ' };',
  21341. ' this.Fly = function () {',
  21342. ' };',
  21343. '});',
  21344. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21345. ' this.Fly = function () {',
  21346. ' $mod.TObject.Fly.call(this);',
  21347. ' $mod.TObject.Fly.call(this);',
  21348. ' };',
  21349. '});',
  21350. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21351. ' this.Fly$1 = function () {',
  21352. ' $mod.TObjHelper.Fly.call(this);',
  21353. ' $mod.TObjHelper.Fly.call(this);',
  21354. ' };',
  21355. '});',
  21356. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21357. ' this.Fly = function () {',
  21358. ' $mod.TBird.Fly$1.call(this);',
  21359. ' $mod.TBird.Fly$1.call(this);',
  21360. ' };',
  21361. ' this.Walk = function (w) {',
  21362. ' };',
  21363. '});',
  21364. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21365. ' this.Fly$1 = function () {',
  21366. ' $mod.TBird.Fly$1.call(this);',
  21367. ' $mod.TBird.Fly$1.call(this);',
  21368. ' };',
  21369. ' this.Walk$1 = function (w) {',
  21370. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21371. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21372. ' };',
  21373. '});',
  21374. '']),
  21375. LinesToStr([ // $mod.$main
  21376. '']));
  21377. end;
  21378. procedure TTestModule.TestClassHelper_Property;
  21379. begin
  21380. StartProgram(false);
  21381. Add([
  21382. 'type',
  21383. ' TObject = class',
  21384. ' FSize: word;',
  21385. ' function GetSpeed: word;',
  21386. ' procedure SetSpeed(Value: word);',
  21387. ' end;',
  21388. ' TObjHelper = class helper for TObject',
  21389. ' function GetLeft: word;',
  21390. ' procedure SetLeft(Value: word);',
  21391. ' property Size: word read FSize write FSize;',
  21392. ' property Speed: word read GetSpeed write SetSpeed;',
  21393. ' property Left: word read GetLeft write SetLeft;',
  21394. ' end;',
  21395. ' TBird = class',
  21396. ' property NotRight: word read GetLeft write SetLeft;',
  21397. ' procedure DoIt;',
  21398. ' end;',
  21399. 'var',
  21400. ' b: TBird;',
  21401. 'function Tobject.GetSpeed: word;',
  21402. 'begin',
  21403. ' Size:=Size+11;',
  21404. ' Speed:=Speed+12;',
  21405. ' Result:=Left+13;',
  21406. ' Left:=13;',
  21407. ' Left:=Left+13;',
  21408. ' Self.Size:=Self.Size+21;',
  21409. ' Self.Speed:=Self.Speed+22;',
  21410. ' Self.Left:=Self.Left+23;',
  21411. ' with Self do begin',
  21412. ' Size:=Size+31;',
  21413. ' Speed:=Speed+32;',
  21414. ' Left:=Left+33;',
  21415. ' end;',
  21416. 'end;',
  21417. 'procedure Tobject.SetSpeed(Value: word);',
  21418. 'begin',
  21419. 'end;',
  21420. 'function TObjHelper.GetLeft: word;',
  21421. 'begin',
  21422. ' Size:=Size+11;',
  21423. ' Speed:=Speed+12;',
  21424. ' Left:=Left+13;',
  21425. ' Self.Size:=Self.Size+21;',
  21426. ' Self.Speed:=Self.Speed+22;',
  21427. ' Self.Left:=Self.Left+23;',
  21428. ' with Self do begin',
  21429. ' Size:=Size+31;',
  21430. ' Speed:=Speed+32;',
  21431. ' Left:=Left+33;',
  21432. ' end;',
  21433. 'end;',
  21434. 'procedure TObjHelper.SetLeft(Value: word);',
  21435. 'begin',
  21436. 'end;',
  21437. 'procedure TBird.DoIt;',
  21438. 'begin',
  21439. ' NotRight:=NotRight+11;',
  21440. ' Self.NotRight:=Self.NotRight+21;',
  21441. ' with Self do begin',
  21442. ' NotRight:=NotRight+31;',
  21443. ' end;',
  21444. 'end;',
  21445. 'begin',
  21446. ' b.Size:=b.Size+11;',
  21447. ' b.Speed:=b.Speed+12;',
  21448. ' b.Left:=b.Left+13;',
  21449. ' b.NotRight:=b.NotRight+14;',
  21450. ' with b do begin',
  21451. ' Size:=Size+31;',
  21452. ' Speed:=Speed+32;',
  21453. ' Left:=Left+33;',
  21454. ' NotRight:=NotRight+34;',
  21455. ' end;',
  21456. '']);
  21457. ConvertProgram;
  21458. CheckSource('TestClassHelper_Property',
  21459. LinesToStr([ // statements
  21460. 'rtl.createClass(this, "TObject", null, function () {',
  21461. ' this.$init = function () {',
  21462. ' this.FSize = 0;',
  21463. ' };',
  21464. ' this.$final = function () {',
  21465. ' };',
  21466. ' this.GetSpeed = function () {',
  21467. ' var Result = 0;',
  21468. ' this.FSize = this.FSize + 11;',
  21469. ' this.SetSpeed(this.GetSpeed() + 12);',
  21470. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21471. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21472. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21473. ' this.FSize = this.FSize + 21;',
  21474. ' this.SetSpeed(this.GetSpeed() + 22);',
  21475. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21476. ' this.FSize = this.FSize + 31;',
  21477. ' this.SetSpeed(this.GetSpeed() + 32);',
  21478. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21479. ' return Result;',
  21480. ' };',
  21481. ' this.SetSpeed = function (Value) {',
  21482. ' };',
  21483. '});',
  21484. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21485. ' this.GetLeft = function () {',
  21486. ' var Result = 0;',
  21487. ' this.FSize = this.FSize + 11;',
  21488. ' this.SetSpeed(this.GetSpeed() + 12);',
  21489. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21490. ' this.FSize = this.FSize + 21;',
  21491. ' this.SetSpeed(this.GetSpeed() + 22);',
  21492. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21493. ' this.FSize = this.FSize + 31;',
  21494. ' this.SetSpeed(this.GetSpeed() + 32);',
  21495. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21496. ' return Result;',
  21497. ' };',
  21498. ' this.SetLeft = function (Value) {',
  21499. ' };',
  21500. '});',
  21501. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21502. ' this.DoIt = function () {',
  21503. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21504. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21505. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21506. ' };',
  21507. '});',
  21508. 'this.b = null;',
  21509. '']),
  21510. LinesToStr([ // $mod.$main
  21511. '$mod.b.FSize = $mod.b.FSize + 11;',
  21512. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21513. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21514. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21515. 'var $with = $mod.b;',
  21516. '$with.FSize = $with.FSize + 31;',
  21517. '$with.SetSpeed($with.GetSpeed() + 32);',
  21518. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21519. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21520. '']));
  21521. end;
  21522. procedure TTestModule.TestClassHelper_Property_Array;
  21523. begin
  21524. StartProgram(false);
  21525. Add([
  21526. 'type',
  21527. ' TObject = class',
  21528. ' function GetSpeed(Index: boolean): word;',
  21529. ' procedure SetSpeed(Index: boolean; Value: word);',
  21530. ' end;',
  21531. ' TObjHelper = class helper for TObject',
  21532. ' function GetSize(Index: boolean): word;',
  21533. ' procedure SetSize(Index: boolean; Value: word);',
  21534. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21535. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21536. ' end;',
  21537. ' TBird = class',
  21538. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21539. ' procedure DoIt;',
  21540. ' end;',
  21541. 'var',
  21542. ' b: TBird;',
  21543. 'function Tobject.GetSpeed(Index: boolean): word;',
  21544. 'begin',
  21545. ' Result:=Size[false];',
  21546. ' Size[true]:=Size[false]+11;',
  21547. ' Speed[true]:=Speed[false]+12;',
  21548. ' Self.Size[true]:=Self.Size[false]+21;',
  21549. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21550. ' with Self do begin',
  21551. ' Size[true]:=Size[false]+31;',
  21552. ' Speed[true]:=Speed[false]+32;',
  21553. ' end;',
  21554. 'end;',
  21555. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21556. 'begin',
  21557. 'end;',
  21558. 'function TObjHelper.GetSize(Index: boolean): word;',
  21559. 'begin',
  21560. ' Size[true]:=Size[false]+11;',
  21561. ' Speed[true]:=Speed[false]+12;',
  21562. ' Self.Size[true]:=Self.Size[false]+21;',
  21563. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21564. ' with Self do begin',
  21565. ' Size[true]:=Size[false]+31;',
  21566. ' Speed[true]:=Speed[false]+32;',
  21567. ' end;',
  21568. 'end;',
  21569. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21570. 'begin',
  21571. 'end;',
  21572. 'procedure TBird.DoIt;',
  21573. 'begin',
  21574. ' Items[true]:=Items[false]+11;',
  21575. ' Self.Items[true]:=Self.Items[false]+21;',
  21576. ' with Self do Items[true]:=Items[false]+31;',
  21577. 'end;',
  21578. 'begin',
  21579. ' b.Size[true]:=b.Size[false]+11;',
  21580. ' b.Speed[true]:=b.Speed[false]+12;',
  21581. ' b.Items[true]:=b.Items[false]+13;',
  21582. ' with b do begin',
  21583. ' Size[true]:=Size[false]+21;',
  21584. ' Speed[true]:=Speed[false]+22;',
  21585. ' Items[true]:=Items[false]+23;',
  21586. ' end;',
  21587. '']);
  21588. ConvertProgram;
  21589. CheckSource('TestClassHelper_Property_Array',
  21590. LinesToStr([ // statements
  21591. 'rtl.createClass(this, "TObject", null, function () {',
  21592. ' this.$init = function () {',
  21593. ' };',
  21594. ' this.$final = function () {',
  21595. ' };',
  21596. ' this.GetSpeed = function (Index) {',
  21597. ' var Result = 0;',
  21598. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21599. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21600. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21601. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21602. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21603. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21604. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21605. ' return Result;',
  21606. ' };',
  21607. ' this.SetSpeed = function (Index, Value) {',
  21608. ' };',
  21609. '});',
  21610. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21611. ' this.GetSize = function (Index) {',
  21612. ' var Result = 0;',
  21613. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21614. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21615. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21616. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21617. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21618. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21619. ' return Result;',
  21620. ' };',
  21621. ' this.SetSize = function (Index, Value) {',
  21622. ' };',
  21623. '});',
  21624. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21625. ' this.DoIt = function () {',
  21626. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21627. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21628. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21629. ' };',
  21630. '});',
  21631. 'this.b = null;',
  21632. '']),
  21633. LinesToStr([ // $mod.$main
  21634. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21635. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21636. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21637. 'var $with = $mod.b;',
  21638. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21639. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21640. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21641. '']));
  21642. end;
  21643. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21644. begin
  21645. StartProgram(false);
  21646. Add([
  21647. 'type',
  21648. ' TObject = class',
  21649. ' function GetSpeed(Index: boolean): word;',
  21650. ' procedure SetSpeed(Index: boolean; Value: word);',
  21651. ' end;',
  21652. ' TObjHelper = class helper for TObject',
  21653. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21654. ' end;',
  21655. ' TBird = class',
  21656. ' end;',
  21657. ' TBirdHelper = class helper for TBird',
  21658. ' function GetSize(Index: word): boolean;',
  21659. ' procedure SetSize(Index: word; Value: boolean);',
  21660. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21661. ' end;',
  21662. 'function Tobject.GetSpeed(Index: boolean): word;',
  21663. 'begin',
  21664. ' Self[true]:=Self[false]+1;',
  21665. 'end;',
  21666. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21667. 'begin',
  21668. 'end;',
  21669. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21670. 'begin',
  21671. ' Self[1]:=not Self[2];',
  21672. 'end;',
  21673. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21674. 'begin',
  21675. 'end;',
  21676. 'var',
  21677. ' o: TObject;',
  21678. ' b: TBird;',
  21679. 'begin',
  21680. ' o[true]:=o[false]+1;',
  21681. ' b[3]:=not b[4];',
  21682. '']);
  21683. ConvertProgram;
  21684. CheckSource('TestClassHelper_Property_Array_Default',
  21685. LinesToStr([ // statements
  21686. 'rtl.createClass(this, "TObject", null, function () {',
  21687. ' this.$init = function () {',
  21688. ' };',
  21689. ' this.$final = function () {',
  21690. ' };',
  21691. ' this.GetSpeed = function (Index) {',
  21692. ' var Result = 0;',
  21693. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21694. ' return Result;',
  21695. ' };',
  21696. ' this.SetSpeed = function (Index, Value) {',
  21697. ' };',
  21698. '});',
  21699. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21700. '});',
  21701. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21702. '});',
  21703. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21704. ' this.GetSize = function (Index) {',
  21705. ' var Result = false;',
  21706. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21707. ' return Result;',
  21708. ' };',
  21709. ' this.SetSize = function (Index, Value) {',
  21710. ' };',
  21711. '});',
  21712. 'this.o = null;',
  21713. 'this.b = null;',
  21714. '']),
  21715. LinesToStr([ // $mod.$main
  21716. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21717. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21718. '']));
  21719. end;
  21720. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21721. begin
  21722. StartProgram(false);
  21723. Add([
  21724. 'type',
  21725. ' TObject = class',
  21726. ' end;',
  21727. ' TObjHelper = class helper for TObject',
  21728. ' function GetItems(Index: word): TObject;',
  21729. ' procedure SetItems(Index: word; Value: TObject);',
  21730. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21731. ' end;',
  21732. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21733. 'begin',
  21734. ' Self[1][2]:=Self[3][4];',
  21735. 'end;',
  21736. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21737. 'begin',
  21738. 'end;',
  21739. 'var',
  21740. ' o: TObject;',
  21741. 'begin',
  21742. ' o[1][2]:=o[3][4];',
  21743. '']);
  21744. ConvertProgram;
  21745. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21746. LinesToStr([ // statements
  21747. 'rtl.createClass(this, "TObject", null, function () {',
  21748. ' this.$init = function () {',
  21749. ' };',
  21750. ' this.$final = function () {',
  21751. ' };',
  21752. '});',
  21753. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21754. ' this.GetItems = function (Index) {',
  21755. ' var Result = null;',
  21756. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21757. ' return Result;',
  21758. ' };',
  21759. ' this.SetItems = function (Index, Value) {',
  21760. ' };',
  21761. '});',
  21762. 'this.o = null;',
  21763. '']),
  21764. LinesToStr([ // $mod.$main
  21765. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21766. '']));
  21767. end;
  21768. procedure TTestModule.TestClassHelper_ClassProperty;
  21769. begin
  21770. StartProgram(false);
  21771. Add([
  21772. 'type',
  21773. ' TObject = class',
  21774. ' class var FSize: word;',
  21775. ' class function GetSpeed: word;',
  21776. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21777. ' end;',
  21778. ' TObjHelper = class helper for TObject',
  21779. ' class function GetLeft: word;',
  21780. ' class procedure SetLeft(Value: word);',
  21781. ' class property Size: word read FSize write FSize;',
  21782. ' class property Speed: word read GetSpeed write SetSpeed;',
  21783. ' class property Left: word read GetLeft write SetLeft;',
  21784. ' end;',
  21785. ' TBird = class',
  21786. ' class property NotRight: word read GetLeft write SetLeft;',
  21787. ' class procedure DoIt;',
  21788. ' end;',
  21789. ' TBirdClass = class of TBird;',
  21790. 'class function Tobject.GetSpeed: word;',
  21791. 'begin',
  21792. ' Size:=Size+11;',
  21793. ' Speed:=Speed+12;',
  21794. ' Left:=Left+13;',
  21795. ' Self.Size:=Self.Size+21;',
  21796. ' Self.Speed:=Self.Speed+22;',
  21797. ' Self.Left:=Self.Left+23;',
  21798. ' with Self do begin',
  21799. ' Size:=Size+31;',
  21800. ' Speed:=Speed+32;',
  21801. ' Left:=Left+33;',
  21802. ' end;',
  21803. 'end;',
  21804. 'class function TObjHelper.GetLeft: word;',
  21805. 'begin',
  21806. ' Size:=Size+11;',
  21807. ' Speed:=Speed+12;',
  21808. ' Left:=Left+13;',
  21809. ' Self.Size:=Self.Size+21;',
  21810. ' Self.Speed:=Self.Speed+22;',
  21811. ' Self.Left:=Self.Left+23;',
  21812. ' with Self do begin',
  21813. ' Size:=Size+31;',
  21814. ' Speed:=Speed+32;',
  21815. ' Left:=Left+33;',
  21816. ' end;',
  21817. 'end;',
  21818. 'class procedure TObjHelper.SetLeft(Value: word);',
  21819. 'begin',
  21820. 'end;',
  21821. 'class procedure TBird.DoIt;',
  21822. 'begin',
  21823. ' NotRight:=NotRight+11;',
  21824. ' Self.NotRight:=Self.NotRight+21;',
  21825. ' with Self do NotRight:=NotRight+31;',
  21826. 'end;',
  21827. 'var',
  21828. ' b: TBird;',
  21829. ' c: TBirdClass;',
  21830. 'begin',
  21831. ' b.Size:=b.Size+11;',
  21832. ' b.Speed:=b.Speed+12;',
  21833. ' b.Left:=b.Left+13;',
  21834. ' b.NotRight:=b.NotRight+14;',
  21835. ' with b do begin',
  21836. ' Size:=Size+31;',
  21837. ' Speed:=Speed+32;',
  21838. ' Left:=Left+33;',
  21839. ' NotRight:=NotRight+34;',
  21840. ' end;',
  21841. ' c.Size:=c.Size+11;',
  21842. ' c.Speed:=c.Speed+12;',
  21843. ' c.Left:=c.Left+13;',
  21844. ' c.NotRight:=c.NotRight+14;',
  21845. ' with c do begin',
  21846. ' Size:=Size+31;',
  21847. ' Speed:=Speed+32;',
  21848. ' Left:=Left+33;',
  21849. ' NotRight:=NotRight+34;',
  21850. ' end;',
  21851. ' tbird.Size:=tbird.Size+11;',
  21852. ' tbird.Speed:=tbird.Speed+12;',
  21853. ' tbird.Left:=tbird.Left+13;',
  21854. ' tbird.NotRight:=tbird.NotRight+14;',
  21855. ' with tbird do begin',
  21856. ' Size:=Size+31;',
  21857. ' Speed:=Speed+32;',
  21858. ' Left:=Left+33;',
  21859. ' NotRight:=NotRight+34;',
  21860. ' end;',
  21861. '']);
  21862. ConvertProgram;
  21863. CheckSource('TestClassHelper_ClassProperty',
  21864. LinesToStr([ // statements
  21865. 'rtl.createClass(this, "TObject", null, function () {',
  21866. ' this.FSize = 0;',
  21867. ' this.$init = function () {',
  21868. ' };',
  21869. ' this.$final = function () {',
  21870. ' };',
  21871. ' this.GetSpeed = function () {',
  21872. ' var Result = 0;',
  21873. ' $mod.TObject.FSize = this.FSize + 11;',
  21874. ' this.SetSpeed(this.GetSpeed() + 12);',
  21875. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21876. ' $mod.TObject.FSize = this.FSize + 21;',
  21877. ' this.SetSpeed(this.GetSpeed() + 22);',
  21878. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21879. ' $mod.TObject.FSize = this.FSize + 31;',
  21880. ' this.SetSpeed(this.GetSpeed() + 32);',
  21881. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21882. ' return Result;',
  21883. ' };',
  21884. '});',
  21885. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21886. ' this.GetLeft = function () {',
  21887. ' var Result = 0;',
  21888. ' $mod.TObject.FSize = this.FSize + 11;',
  21889. ' this.SetSpeed(this.GetSpeed() + 12);',
  21890. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21891. ' $mod.TObject.FSize = this.FSize + 21;',
  21892. ' this.SetSpeed(this.GetSpeed() + 22);',
  21893. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21894. ' $mod.TObject.FSize = this.FSize + 31;',
  21895. ' this.SetSpeed(this.GetSpeed() + 32);',
  21896. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21897. ' return Result;',
  21898. ' };',
  21899. ' this.SetLeft = function (Value) {',
  21900. ' };',
  21901. '});',
  21902. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21903. ' this.DoIt = function () {',
  21904. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21905. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21906. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21907. ' };',
  21908. '});',
  21909. 'this.b = null;',
  21910. 'this.c = null;',
  21911. '']),
  21912. LinesToStr([ // $mod.$main
  21913. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21914. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21915. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21916. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21917. 'var $with = $mod.b;',
  21918. '$mod.TObject.FSize = $with.FSize + 31;',
  21919. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21920. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21921. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21922. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21923. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21924. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21925. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21926. 'var $with1 = $mod.c;',
  21927. '$mod.TObject.FSize = $with1.FSize + 31;',
  21928. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21929. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21930. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21931. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21932. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21933. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21934. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21935. 'var $with2 = $mod.TBird;',
  21936. '$mod.TObject.FSize = $with2.FSize + 31;',
  21937. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21938. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21939. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21940. '']));
  21941. end;
  21942. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21943. begin
  21944. StartProgram(false);
  21945. Add([
  21946. 'type',
  21947. ' TObject = class',
  21948. ' class function GetSpeed: word; static;',
  21949. ' class procedure SetSpeed(Value: word); static;',
  21950. ' end;',
  21951. ' TObjHelper = class helper for TObject',
  21952. ' class function GetLeft: word; static;',
  21953. ' class procedure SetLeft(Value: word); static;',
  21954. ' class property Speed: word read GetSpeed write SetSpeed;',
  21955. ' class property Left: word read GetLeft write SetLeft;',
  21956. ' end;',
  21957. ' TBird = class',
  21958. ' class property NotRight: word read GetLeft write SetLeft;',
  21959. ' class procedure DoIt; static;',
  21960. ' class procedure DoSome;',
  21961. ' end;',
  21962. ' TBirdClass = class of TBird;',
  21963. 'class function Tobject.GetSpeed: word;',
  21964. 'begin',
  21965. ' Speed:=Speed+12;',
  21966. ' Left:=Left+13;',
  21967. 'end;',
  21968. 'class procedure TObject.SetSpeed(Value: word);',
  21969. 'begin',
  21970. 'end;',
  21971. 'class function TObjHelper.GetLeft: word;',
  21972. 'begin',
  21973. ' Speed:=Speed+12;',
  21974. ' Left:=Left+13;',
  21975. 'end;',
  21976. 'class procedure TObjHelper.SetLeft(Value: word);',
  21977. 'begin',
  21978. 'end;',
  21979. 'class procedure TBird.DoIt;',
  21980. 'begin',
  21981. ' NotRight:=NotRight+11;',
  21982. 'end;',
  21983. 'class procedure TBird.DoSome;',
  21984. 'begin',
  21985. ' Speed:=Speed+12;',
  21986. ' Left:=Left+13;',
  21987. ' Self.Speed:=Self.Speed+22;',
  21988. ' Self.Left:=Self.Left+23;',
  21989. ' with Self do begin',
  21990. ' Speed:=Speed+32;',
  21991. ' Left:=Left+33;',
  21992. ' end;',
  21993. ' NotRight:=NotRight+11;',
  21994. ' Self.NotRight:=Self.NotRight+21;',
  21995. ' with Self do NotRight:=NotRight+31;',
  21996. 'end;',
  21997. 'var',
  21998. ' b: TBird;',
  21999. ' c: TBirdClass;',
  22000. 'begin',
  22001. ' b.Speed:=b.Speed+12;',
  22002. ' b.Left:=b.Left+13;',
  22003. ' b.NotRight:=b.NotRight+14;',
  22004. ' with b do begin',
  22005. ' Speed:=Speed+32;',
  22006. ' Left:=Left+33;',
  22007. ' NotRight:=NotRight+34;',
  22008. ' end;',
  22009. ' c.Speed:=c.Speed+12;',
  22010. ' c.Left:=c.Left+13;',
  22011. ' c.NotRight:=c.NotRight+14;',
  22012. ' with c do begin',
  22013. ' Speed:=Speed+32;',
  22014. ' Left:=Left+33;',
  22015. ' NotRight:=NotRight+34;',
  22016. ' end;',
  22017. ' tbird.Speed:=tbird.Speed+12;',
  22018. ' tbird.Left:=tbird.Left+13;',
  22019. ' tbird.NotRight:=tbird.NotRight+14;',
  22020. ' with tbird do begin',
  22021. ' Speed:=Speed+32;',
  22022. ' Left:=Left+33;',
  22023. ' NotRight:=NotRight+34;',
  22024. ' end;',
  22025. '']);
  22026. ConvertProgram;
  22027. CheckSource('TestClassHelper_ClassPropertyStatic',
  22028. LinesToStr([ // statements
  22029. 'rtl.createClass(this, "TObject", null, function () {',
  22030. ' this.$init = function () {',
  22031. ' };',
  22032. ' this.$final = function () {',
  22033. ' };',
  22034. ' this.GetSpeed = function () {',
  22035. ' var Result = 0;',
  22036. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22037. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22038. ' return Result;',
  22039. ' };',
  22040. ' this.SetSpeed = function (Value) {',
  22041. ' };',
  22042. '});',
  22043. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22044. ' this.GetLeft = function () {',
  22045. ' var Result = 0;',
  22046. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22047. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22048. ' return Result;',
  22049. ' };',
  22050. ' this.SetLeft = function (Value) {',
  22051. ' };',
  22052. '});',
  22053. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22054. ' this.DoIt = function () {',
  22055. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22056. ' };',
  22057. ' this.DoSome = function () {',
  22058. ' this.SetSpeed(this.GetSpeed() + 12);',
  22059. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22060. ' this.SetSpeed(this.GetSpeed() + 22);',
  22061. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22062. ' this.SetSpeed(this.GetSpeed() + 32);',
  22063. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22064. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22065. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22066. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22067. ' };',
  22068. '});',
  22069. 'this.b = null;',
  22070. 'this.c = null;',
  22071. '']),
  22072. LinesToStr([ // $mod.$main
  22073. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22074. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22075. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22076. 'var $with = $mod.b;',
  22077. '$with.SetSpeed($with.GetSpeed() + 32);',
  22078. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22079. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22080. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22081. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22082. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22083. 'var $with1 = $mod.c;',
  22084. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22085. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22086. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22087. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22088. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22089. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22090. 'var $with2 = $mod.TBird;',
  22091. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22092. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22093. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22094. '']));
  22095. end;
  22096. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22097. begin
  22098. StartProgram(false);
  22099. Add([
  22100. 'type',
  22101. ' TObject = class',
  22102. ' class function GetSpeed(Index: boolean): word;',
  22103. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22104. ' end;',
  22105. ' TObjHelper = class helper for TObject',
  22106. ' class function GetSize(Index: boolean): word;',
  22107. ' class procedure SetSize(Index: boolean; Value: word);',
  22108. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22109. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22110. ' end;',
  22111. ' TBird = class',
  22112. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22113. ' class procedure DoIt;',
  22114. ' end;',
  22115. ' TBirdClass = class of TBird;',
  22116. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22117. 'begin',
  22118. ' Size[true]:=Size[false]+11;',
  22119. ' Speed[true]:=Speed[false]+12;',
  22120. ' Self.Size[true]:=Self.Size[false]+21;',
  22121. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22122. ' with Self do begin',
  22123. ' Size[true]:=Size[false]+31;',
  22124. ' Speed[true]:=Speed[false]+32;',
  22125. ' end;',
  22126. 'end;',
  22127. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22128. 'begin',
  22129. ' Size[true]:=Size[false]+11;',
  22130. ' Speed[true]:=Speed[false]+12;',
  22131. ' Self.Size[true]:=Self.Size[false]+21;',
  22132. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22133. ' with Self do begin',
  22134. ' Size[true]:=Size[false]+31;',
  22135. ' Speed[true]:=Speed[false]+32;',
  22136. ' end;',
  22137. 'end;',
  22138. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22139. 'begin',
  22140. 'end;',
  22141. 'class procedure TBird.DoIt;',
  22142. 'begin',
  22143. ' Items[true]:=Items[false]+11;',
  22144. ' Self.Items[true]:=Self.Items[false]+21;',
  22145. ' with Self do Items[true]:=Items[false]+31;',
  22146. 'end;',
  22147. 'var',
  22148. ' b: TBird;',
  22149. ' c: TBirdClass;',
  22150. 'begin',
  22151. ' b.Size[true]:=b.Size[false]+11;',
  22152. ' b.Speed[true]:=b.Speed[false]+12;',
  22153. ' b.Items[true]:=b.Items[false]+13;',
  22154. ' with b do begin',
  22155. ' Size[true]:=Size[false]+21;',
  22156. ' Speed[true]:=Speed[false]+22;',
  22157. ' Items[true]:=Items[false]+23;',
  22158. ' end;',
  22159. ' c.Size[true]:=c.Size[false]+11;',
  22160. ' c.Speed[true]:=c.Speed[false]+12;',
  22161. ' c.Items[true]:=c.Items[false]+13;',
  22162. ' with c do begin',
  22163. ' Size[true]:=Size[false]+21;',
  22164. ' Speed[true]:=Speed[false]+22;',
  22165. ' Items[true]:=Items[false]+23;',
  22166. ' end;',
  22167. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22168. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22169. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22170. ' with TBird do begin',
  22171. ' Size[true]:=Size[false]+21;',
  22172. ' Speed[true]:=Speed[false]+22;',
  22173. ' Items[true]:=Items[false]+23;',
  22174. ' end;',
  22175. '']);
  22176. ConvertProgram;
  22177. CheckSource('TestClassHelper_ClassProperty_Array',
  22178. LinesToStr([ // statements
  22179. 'rtl.createClass(this, "TObject", null, function () {',
  22180. ' this.$init = function () {',
  22181. ' };',
  22182. ' this.$final = function () {',
  22183. ' };',
  22184. ' this.GetSpeed = function (Index) {',
  22185. ' var Result = 0;',
  22186. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22187. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22188. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22189. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22190. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22191. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22192. ' return Result;',
  22193. ' };',
  22194. '});',
  22195. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22196. ' this.GetSize = function (Index) {',
  22197. ' var Result = 0;',
  22198. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22199. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22200. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22201. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22202. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22203. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22204. ' return Result;',
  22205. ' };',
  22206. ' this.SetSize = function (Index, Value) {',
  22207. ' };',
  22208. '});',
  22209. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22210. ' this.DoIt = function () {',
  22211. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22212. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22213. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22214. ' };',
  22215. '});',
  22216. 'this.b = null;',
  22217. 'this.c = null;',
  22218. '']),
  22219. LinesToStr([ // $mod.$main
  22220. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22221. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22222. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22223. 'var $with = $mod.b;',
  22224. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22225. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22226. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22227. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22228. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22229. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22230. 'var $with1 = $mod.c;',
  22231. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22232. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22233. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22234. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22235. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22236. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22237. 'var $with2 = $mod.TBird;',
  22238. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22239. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22240. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22241. '']));
  22242. end;
  22243. procedure TTestModule.TestClassHelper_ForIn;
  22244. begin
  22245. StartProgram(false);
  22246. Add([
  22247. 'type',
  22248. ' TObject = class end;',
  22249. ' TItem = TObject;',
  22250. ' TEnumerator = class',
  22251. ' FCurrent: TItem;',
  22252. ' property Current: TItem read FCurrent;',
  22253. ' function MoveNext: boolean;',
  22254. ' end;',
  22255. ' TBird = class',
  22256. ' end;',
  22257. ' TBirdHelper = class helper for TBird',
  22258. ' function GetEnumerator: TEnumerator;',
  22259. ' end;',
  22260. 'function TEnumerator.MoveNext: boolean;',
  22261. 'begin',
  22262. 'end;',
  22263. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22264. 'begin',
  22265. 'end;',
  22266. 'var',
  22267. ' b: TBird;',
  22268. ' i, i2: TItem;',
  22269. 'begin',
  22270. ' for i in b do i2:=i;']);
  22271. ConvertProgram;
  22272. CheckSource('TestClassHelper_ForIn',
  22273. LinesToStr([ // statements
  22274. 'rtl.createClass(this, "TObject", null, function () {',
  22275. ' this.$init = function () {',
  22276. ' };',
  22277. ' this.$final = function () {',
  22278. ' };',
  22279. '});',
  22280. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22281. ' this.$init = function () {',
  22282. ' $mod.TObject.$init.call(this);',
  22283. ' this.FCurrent = null;',
  22284. ' };',
  22285. ' this.$final = function () {',
  22286. ' this.FCurrent = undefined;',
  22287. ' $mod.TObject.$final.call(this);',
  22288. ' };',
  22289. ' this.MoveNext = function () {',
  22290. ' var Result = false;',
  22291. ' return Result;',
  22292. ' };',
  22293. '});',
  22294. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22295. '});',
  22296. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22297. ' this.GetEnumerator = function () {',
  22298. ' var Result = null;',
  22299. ' return Result;',
  22300. ' };',
  22301. '});',
  22302. 'this.b = null;',
  22303. 'this.i = null;',
  22304. 'this.i2 = null;'
  22305. ]),
  22306. LinesToStr([ // $mod.$main
  22307. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22308. 'try {',
  22309. ' while ($in.MoveNext()){',
  22310. ' $mod.i = $in.FCurrent;',
  22311. ' $mod.i2 = $mod.i;',
  22312. ' }',
  22313. '} finally {',
  22314. ' $in = rtl.freeLoc($in)',
  22315. '};',
  22316. '']));
  22317. end;
  22318. procedure TTestModule.TestClassHelper_PassProperty;
  22319. begin
  22320. StartProgram(false);
  22321. Add([
  22322. 'type',
  22323. ' TObject = class',
  22324. ' FField: TObject;',
  22325. ' property Field: TObject read FField write FField;',
  22326. ' end;',
  22327. ' THelper = class helper for TObject',
  22328. ' procedure Fly;',
  22329. ' class procedure Run;',
  22330. ' class procedure Jump; static;',
  22331. ' end;',
  22332. 'procedure THelper.Fly;',
  22333. 'begin',
  22334. ' Field.Fly;',
  22335. ' Field.Run;',
  22336. ' Field.Jump;',
  22337. ' with Field do begin',
  22338. ' Fly;',
  22339. ' Run;',
  22340. ' Jump;',
  22341. ' end;',
  22342. 'end;',
  22343. 'class procedure THelper.Run;',
  22344. 'begin',
  22345. 'end;',
  22346. 'class procedure THelper.Jump;',
  22347. 'begin',
  22348. 'end;',
  22349. 'var',
  22350. ' b: TObject;',
  22351. 'begin',
  22352. ' b.Field.Fly;',
  22353. ' b.Field.Run;',
  22354. ' b.Field.Jump;',
  22355. ' with b do begin',
  22356. ' Field.Run;',
  22357. ' Field.Fly;',
  22358. ' Field.Jump;',
  22359. ' end;',
  22360. ' with b.Field do begin',
  22361. ' Run;',
  22362. ' Fly;',
  22363. ' Jump;',
  22364. ' end;',
  22365. '']);
  22366. ConvertProgram;
  22367. CheckSource('TestClassHelper_PassProperty',
  22368. LinesToStr([ // statements
  22369. 'rtl.createClass(this, "TObject", null, function () {',
  22370. ' this.$init = function () {',
  22371. ' this.FField = null;',
  22372. ' };',
  22373. ' this.$final = function () {',
  22374. ' this.FField = undefined;',
  22375. ' };',
  22376. '});',
  22377. 'rtl.createHelper(this, "THelper", null, function () {',
  22378. ' this.Fly = function () {',
  22379. ' $mod.THelper.Fly.call(this.FField);',
  22380. ' $mod.THelper.Run.call(this.FField.$class);',
  22381. ' $mod.THelper.Jump();',
  22382. ' var $with = this.FField;',
  22383. ' $mod.THelper.Fly.call($with);',
  22384. ' $mod.THelper.Run.call($with.$class);',
  22385. ' $mod.THelper.Jump();',
  22386. ' };',
  22387. ' this.Run = function () {',
  22388. ' };',
  22389. ' this.Jump = function () {',
  22390. ' };',
  22391. '});',
  22392. 'this.b = null;',
  22393. '']),
  22394. LinesToStr([ // $mod.$main
  22395. '$mod.THelper.Fly.call($mod.b.FField);',
  22396. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22397. '$mod.THelper.Jump();',
  22398. 'var $with = $mod.b;',
  22399. '$mod.THelper.Run.call($with.FField.$class);',
  22400. '$mod.THelper.Fly.call($with.FField);',
  22401. '$mod.THelper.Jump();',
  22402. 'var $with1 = $mod.b.FField;',
  22403. '$mod.THelper.Run.call($with1.$class);',
  22404. '$mod.THelper.Fly.call($with1);',
  22405. '$mod.THelper.Jump();',
  22406. '']));
  22407. end;
  22408. procedure TTestModule.TestExtClassHelper_ClassVar;
  22409. begin
  22410. StartProgram(false);
  22411. Add([
  22412. '{$modeswitch externalclass}',
  22413. 'type',
  22414. ' TExtA = class external name ''ExtObj''',
  22415. ' end;',
  22416. ' THelper = class helper for TExtA',
  22417. ' const',
  22418. ' One = 1;',
  22419. ' Two: word = 2;',
  22420. ' class var',
  22421. ' Glob: word;',
  22422. ' function Foo(w: word): word;',
  22423. ' class function Bar(w: word): word; static;',
  22424. ' end;',
  22425. 'function THelper.foo(w: word): word;',
  22426. 'begin',
  22427. ' Result:=w;',
  22428. ' Two:=One+w;',
  22429. ' Glob:=Glob;',
  22430. ' Result:=Self.Glob;',
  22431. ' Self.Glob:=Self.Glob;',
  22432. ' with Self do Glob:=Glob;',
  22433. 'end;',
  22434. 'class function THelper.bar(w: word): word;',
  22435. 'begin',
  22436. ' Result:=w;',
  22437. ' Two:=One;',
  22438. ' Glob:=Glob;',
  22439. 'end;',
  22440. 'var o: TExtA;',
  22441. 'begin',
  22442. ' texta.two:=texta.one;',
  22443. ' texta.Glob:=texta.Glob;',
  22444. ' with texta do begin',
  22445. ' two:=one;',
  22446. ' Glob:=Glob;',
  22447. ' end;',
  22448. ' o.two:=o.one;',
  22449. ' o.Glob:=o.Glob;',
  22450. ' with o do begin',
  22451. ' two:=one;',
  22452. ' Glob:=Glob;',
  22453. ' end;',
  22454. '']);
  22455. ConvertProgram;
  22456. CheckSource('TestExtClassHelper_ClassVar',
  22457. LinesToStr([ // statements
  22458. 'rtl.createHelper(this, "THelper", null, function () {',
  22459. ' this.One = 1;',
  22460. ' this.Two = 2;',
  22461. ' this.Glob = 0;',
  22462. ' this.Foo = function (w) {',
  22463. ' var Result = 0;',
  22464. ' Result = w;',
  22465. ' $mod.THelper.Two = 1 + w;',
  22466. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22467. ' Result = $mod.THelper.Glob;',
  22468. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22469. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22470. ' return Result;',
  22471. ' };',
  22472. ' this.Bar = function (w) {',
  22473. ' var Result = 0;',
  22474. ' Result = w;',
  22475. ' $mod.THelper.Two = 1;',
  22476. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22477. ' return Result;',
  22478. ' };',
  22479. '});',
  22480. 'this.o = null;',
  22481. '']),
  22482. LinesToStr([ // $mod.$main
  22483. '$mod.THelper.Two = 1;',
  22484. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22485. '$mod.THelper.Two = 1;',
  22486. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22487. '$mod.THelper.Two = 1;',
  22488. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22489. 'var $with = $mod.o;',
  22490. '$mod.THelper.Two = 1;',
  22491. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22492. '']));
  22493. end;
  22494. procedure TTestModule.TestExtClassHelper_Method_Call;
  22495. begin
  22496. StartProgram(false);
  22497. Add([
  22498. '{$modeswitch externalclass}',
  22499. 'type',
  22500. ' TFly = function(w: word): word of object;',
  22501. ' TExtA = class external name ''ExtObj''',
  22502. ' procedure Run(w: word = 10);',
  22503. ' end;',
  22504. ' THelper = class helper for TExtA',
  22505. ' function Foo(w: word = 1): word;',
  22506. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22507. ' end;',
  22508. 'var p: TFly;',
  22509. 'function THelper.foo(w: word): word;',
  22510. 'begin',
  22511. ' Run;',
  22512. ' Run();',
  22513. ' Run(11);',
  22514. ' Foo;',
  22515. ' Foo();',
  22516. ' Foo(12);',
  22517. ' Self.Foo;',
  22518. ' Self.Foo();',
  22519. ' Self.Foo(13);',
  22520. ' Fly;',
  22521. ' Fly();',
  22522. ' with Self do begin',
  22523. ' Foo;',
  22524. ' Foo();',
  22525. ' Foo(14);',
  22526. ' Fly;',
  22527. ' Fly();',
  22528. ' end;',
  22529. ' p:=@Fly;',
  22530. 'end;',
  22531. 'var Obj: TExtA;',
  22532. 'begin',
  22533. ' obj.Foo;',
  22534. ' obj.Foo();',
  22535. ' obj.Foo(21);',
  22536. ' obj.Fly;',
  22537. ' obj.Fly();',
  22538. ' with obj do begin',
  22539. ' Foo;',
  22540. ' Foo();',
  22541. ' Foo(22);',
  22542. ' Fly;',
  22543. ' Fly();',
  22544. ' end;',
  22545. ' p:[email protected];',
  22546. '']);
  22547. ConvertProgram;
  22548. CheckSource('TestExtClassHelper_Method_Call',
  22549. LinesToStr([ // statements
  22550. 'rtl.createHelper(this, "THelper", null, function () {',
  22551. ' this.Foo = function (w) {',
  22552. ' var Result = 0;',
  22553. ' this.Run(10);',
  22554. ' this.Run(10);',
  22555. ' this.Run(11);',
  22556. ' $mod.THelper.Foo.call(this, 1);',
  22557. ' $mod.THelper.Foo.call(this, 1);',
  22558. ' $mod.THelper.Foo.call(this, 12);',
  22559. ' $mod.THelper.Foo.call(this, 1);',
  22560. ' $mod.THelper.Foo.call(this, 1);',
  22561. ' $mod.THelper.Foo.call(this, 13);',
  22562. ' this.Fly(2);',
  22563. ' this.Fly(2);',
  22564. ' $mod.THelper.Foo.call(this, 1);',
  22565. ' $mod.THelper.Foo.call(this, 1);',
  22566. ' $mod.THelper.Foo.call(this, 14);',
  22567. ' this.Fly(2);',
  22568. ' this.Fly(2);',
  22569. ' $mod.p = rtl.createCallback(this, "Fly");',
  22570. ' return Result;',
  22571. ' };',
  22572. '});',
  22573. 'this.p = null;',
  22574. 'this.Obj = null;',
  22575. '']),
  22576. LinesToStr([ // $mod.$main
  22577. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22578. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22579. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22580. '$mod.Obj.Fly(2);',
  22581. '$mod.Obj.Fly(2);',
  22582. 'var $with = $mod.Obj;',
  22583. '$mod.THelper.Foo.call($with, 1);',
  22584. '$mod.THelper.Foo.call($with, 1);',
  22585. '$mod.THelper.Foo.call($with, 22);',
  22586. '$with.Fly(2);',
  22587. '$with.Fly(2);',
  22588. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22589. '']));
  22590. end;
  22591. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22592. begin
  22593. StartProgram(false);
  22594. Add([
  22595. '{$modeswitch externalclass}',
  22596. 'type',
  22597. ' TExtA = class external name ''ExtObj''',
  22598. ' procedure Run(w: word = 10);',
  22599. ' end;',
  22600. ' THelper = class helper for TExtA',
  22601. ' class procedure Fly;',
  22602. ' end;',
  22603. 'class procedure THelper.Fly;',
  22604. 'begin end;',
  22605. 'begin',
  22606. '']);
  22607. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22608. nHelperClassMethodForExtClassMustBeStatic);
  22609. ConvertProgram;
  22610. end;
  22611. procedure TTestModule.TestRecordHelper_ClassVar;
  22612. begin
  22613. StartProgram(false);
  22614. Add([
  22615. 'type',
  22616. ' TRec = record',
  22617. ' end;',
  22618. ' THelper = record helper for TRec',
  22619. ' const',
  22620. ' One = 1;',
  22621. ' Two: word = 2;',
  22622. ' class var',
  22623. ' Glob: word;',
  22624. ' function Foo(w: word): word;',
  22625. ' class function Bar(w: word): word; static;',
  22626. ' end;',
  22627. 'function THelper.foo(w: word): word;',
  22628. 'begin',
  22629. ' Result:=w;',
  22630. ' Two:=One+w;',
  22631. ' Glob:=Glob;',
  22632. ' Result:=Self.Glob;',
  22633. ' Self.Glob:=Self.Glob;',
  22634. ' with Self do Glob:=Glob;',
  22635. ' Self:=Self;',
  22636. 'end;',
  22637. 'class function THelper.bar(w: word): word;',
  22638. 'begin',
  22639. ' Result:=w;',
  22640. ' Two:=One;',
  22641. ' Glob:=Glob;',
  22642. 'end;',
  22643. 'var r: TRec;',
  22644. 'begin',
  22645. ' trec.two:=trec.one;',
  22646. ' trec.Glob:=trec.Glob;',
  22647. ' with trec do begin',
  22648. ' two:=one;',
  22649. ' Glob:=Glob;',
  22650. ' end;',
  22651. ' r.two:=r.one;',
  22652. ' r.Glob:=r.Glob;',
  22653. ' with r do begin',
  22654. ' two:=one;',
  22655. ' Glob:=Glob;',
  22656. ' end;',
  22657. '']);
  22658. ConvertProgram;
  22659. CheckSource('TestRecordHelper_ClassVar',
  22660. LinesToStr([ // statements
  22661. 'rtl.recNewT(this, "TRec", function () {',
  22662. ' this.$eq = function (b) {',
  22663. ' return true;',
  22664. ' };',
  22665. ' this.$assign = function (s) {',
  22666. ' return this;',
  22667. ' };',
  22668. '});',
  22669. 'rtl.createHelper(this, "THelper", null, function () {',
  22670. ' this.One = 1;',
  22671. ' this.Two = 2;',
  22672. ' this.Glob = 0;',
  22673. ' this.Foo = function (w) {',
  22674. ' var Result = 0;',
  22675. ' Result = w;',
  22676. ' $mod.THelper.Two = 1 + w;',
  22677. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22678. ' Result = $mod.THelper.Glob;',
  22679. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22680. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22681. ' this.$assign(this);',
  22682. ' return Result;',
  22683. ' };',
  22684. ' this.Bar = function (w) {',
  22685. ' var Result = 0;',
  22686. ' Result = w;',
  22687. ' $mod.THelper.Two = 1;',
  22688. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22689. ' return Result;',
  22690. ' };',
  22691. '});',
  22692. 'this.r = this.TRec.$new();',
  22693. '']),
  22694. LinesToStr([ // $mod.$main
  22695. '$mod.THelper.Two = 1;',
  22696. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22697. 'var $with = $mod.TRec;',
  22698. '$mod.THelper.Two = 1;',
  22699. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22700. '$mod.THelper.Two = 1;',
  22701. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22702. 'var $with1 = $mod.r;',
  22703. '$mod.THelper.Two = 1;',
  22704. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22705. '']));
  22706. end;
  22707. procedure TTestModule.TestRecordHelper_Method_Call;
  22708. begin
  22709. StartProgram(false);
  22710. Add([
  22711. '{$modeswitch AdvancedRecords}',
  22712. 'type',
  22713. ' TRec = record',
  22714. ' procedure Run(w: word = 10);',
  22715. ' end;',
  22716. ' THelper = record helper for TRec',
  22717. ' function Foo(w: word = 1): word;',
  22718. ' end;',
  22719. 'procedure TRec.Run(w: word);',
  22720. 'begin',
  22721. ' Foo;',
  22722. ' Foo();',
  22723. ' Foo(2);',
  22724. ' Self.Foo;',
  22725. ' Self.Foo();',
  22726. ' Self.Foo(3);',
  22727. ' with Self do begin',
  22728. ' Foo;',
  22729. ' Foo();',
  22730. ' Foo(4);',
  22731. ' end;',
  22732. 'end;',
  22733. 'function THelper.foo(w: word): word;',
  22734. 'begin',
  22735. ' Run;',
  22736. ' Run();',
  22737. ' Run(11);',
  22738. ' Foo;',
  22739. ' Foo();',
  22740. ' Foo(12);',
  22741. ' Self.Foo;',
  22742. ' Self.Foo();',
  22743. ' Self.Foo(13);',
  22744. ' with Self do begin',
  22745. ' Foo;',
  22746. ' Foo();',
  22747. ' Foo(14);',
  22748. ' end;',
  22749. 'end;',
  22750. 'var Rec: TRec;',
  22751. 'begin',
  22752. ' Rec.Foo;',
  22753. ' Rec.Foo();',
  22754. ' Rec.Foo(21);',
  22755. ' with Rec do begin',
  22756. ' Foo;',
  22757. ' Foo();',
  22758. ' Foo(22);',
  22759. ' end;',
  22760. '']);
  22761. ConvertProgram;
  22762. CheckSource('TestRecordHelper_Method_Call',
  22763. LinesToStr([ // statements
  22764. 'rtl.recNewT(this, "TRec", function () {',
  22765. ' this.$eq = function (b) {',
  22766. ' return true;',
  22767. ' };',
  22768. ' this.$assign = function (s) {',
  22769. ' return this;',
  22770. ' };',
  22771. ' this.Run = function (w) {',
  22772. ' $mod.THelper.Foo.call(this, 1);',
  22773. ' $mod.THelper.Foo.call(this, 1);',
  22774. ' $mod.THelper.Foo.call(this, 2);',
  22775. ' $mod.THelper.Foo.call(this, 1);',
  22776. ' $mod.THelper.Foo.call(this, 1);',
  22777. ' $mod.THelper.Foo.call(this, 3);',
  22778. ' $mod.THelper.Foo.call(this, 1);',
  22779. ' $mod.THelper.Foo.call(this, 1);',
  22780. ' $mod.THelper.Foo.call(this, 4);',
  22781. ' };',
  22782. '});',
  22783. 'rtl.createHelper(this, "THelper", null, function () {',
  22784. ' this.Foo = function (w) {',
  22785. ' var Result = 0;',
  22786. ' this.Run(10);',
  22787. ' this.Run(10);',
  22788. ' this.Run(11);',
  22789. ' $mod.THelper.Foo.call(this, 1);',
  22790. ' $mod.THelper.Foo.call(this, 1);',
  22791. ' $mod.THelper.Foo.call(this, 12);',
  22792. ' $mod.THelper.Foo.call(this, 1);',
  22793. ' $mod.THelper.Foo.call(this, 1);',
  22794. ' $mod.THelper.Foo.call(this, 13);',
  22795. ' $mod.THelper.Foo.call(this, 1);',
  22796. ' $mod.THelper.Foo.call(this, 1);',
  22797. ' $mod.THelper.Foo.call(this, 14);',
  22798. ' return Result;',
  22799. ' };',
  22800. '});',
  22801. 'this.Rec = this.TRec.$new();',
  22802. '']),
  22803. LinesToStr([ // $mod.$main
  22804. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22805. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22806. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22807. 'var $with = $mod.Rec;',
  22808. '$mod.THelper.Foo.call($with, 1);',
  22809. '$mod.THelper.Foo.call($with, 1);',
  22810. '$mod.THelper.Foo.call($with, 22);',
  22811. '']));
  22812. end;
  22813. procedure TTestModule.TestRecordHelper_Constructor;
  22814. begin
  22815. StartProgram(false);
  22816. Add([
  22817. '{$modeswitch AdvancedRecords}',
  22818. 'type',
  22819. ' TRec = record',
  22820. ' constructor Create(w: word);',
  22821. ' end;',
  22822. ' THelper = record helper for TRec',
  22823. ' constructor NewHlp(w: word);',
  22824. ' end;',
  22825. 'var',
  22826. ' Rec: TRec;',
  22827. 'constructor TRec.Create(w: word);',
  22828. 'begin',
  22829. ' NewHlp(2);', // normal call
  22830. ' trec.NewHlp(3);', // new instance
  22831. 'end;',
  22832. 'constructor THelper.NewHlp(w: word);',
  22833. 'begin',
  22834. ' create(2);', // normal call
  22835. ' trec.create(3);', // new instance
  22836. ' NewHlp(4);', // normal call
  22837. ' trec.NewHlp(5);', // new instance
  22838. 'end;',
  22839. 'begin',
  22840. ' rec.newhlp(2);', // normal call
  22841. ' with rec do newhlp(12);', // normal call
  22842. ' trec.newhlp(3);', // new instance
  22843. ' with trec do newhlp(13);', // new instance
  22844. '']);
  22845. ConvertProgram;
  22846. CheckSource('TestRecordHelper_Constructor',
  22847. LinesToStr([ // statements
  22848. 'rtl.recNewT(this, "TRec", function () {',
  22849. ' this.$eq = function (b) {',
  22850. ' return true;',
  22851. ' };',
  22852. ' this.$assign = function (s) {',
  22853. ' return this;',
  22854. ' };',
  22855. ' this.Create = function (w) {',
  22856. ' $mod.THelper.NewHlp.call(this, 2);',
  22857. ' $mod.THelper.$new("NewHlp", [3]);',
  22858. ' return this;',
  22859. ' };',
  22860. '});',
  22861. 'rtl.createHelper(this, "THelper", null, function () {',
  22862. ' this.NewHlp = function (w) {',
  22863. ' this.Create(2);',
  22864. ' $mod.TRec.$new().Create(3);',
  22865. ' $mod.THelper.NewHlp.call(this, 4);',
  22866. ' $mod.THelper.$new("NewHlp", [5]);',
  22867. ' return this;',
  22868. ' };',
  22869. ' this.$new = function (fn, args) {',
  22870. ' return this[fn].apply($mod.TRec.$new(), args);',
  22871. ' };',
  22872. '});',
  22873. 'this.Rec = this.TRec.$new();',
  22874. '']),
  22875. LinesToStr([ // $mod.$main
  22876. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22877. 'var $with = $mod.Rec;',
  22878. '$mod.THelper.NewHlp.call($with, 12);',
  22879. '$mod.THelper.$new("NewHlp", [3]);',
  22880. 'var $with1 = $mod.TRec;',
  22881. '$mod.THelper.$new("NewHlp", [13]);',
  22882. '']));
  22883. end;
  22884. procedure TTestModule.TestTypeHelper_ClassVar;
  22885. begin
  22886. StartProgram(false);
  22887. Add([
  22888. '{$modeswitch typehelpers}',
  22889. 'type',
  22890. ' THelper = type helper for byte',
  22891. ' const',
  22892. ' One = 1;',
  22893. ' Two: word = 2;',
  22894. ' class var',
  22895. ' Glob: word;',
  22896. ' function Foo(w: word): word;',
  22897. ' class function Bar(w: word): word; static;',
  22898. ' end;',
  22899. 'function THelper.foo(w: word): word;',
  22900. 'begin',
  22901. ' Result:=w;',
  22902. ' Two:=One+w;',
  22903. ' Glob:=Glob;',
  22904. ' Result:=Self.Glob;',
  22905. ' Self.Glob:=Self.Glob;',
  22906. ' with Self do Glob:=Glob;',
  22907. 'end;',
  22908. 'class function THelper.bar(w: word): word;',
  22909. 'begin',
  22910. ' Result:=w;',
  22911. ' Two:=One;',
  22912. ' Glob:=Glob;',
  22913. 'end;',
  22914. 'var b: byte;',
  22915. 'begin',
  22916. ' byte.two:=byte.one;',
  22917. ' byte.Glob:=byte.Glob;',
  22918. ' with byte do begin',
  22919. ' two:=one;',
  22920. ' Glob:=Glob;',
  22921. ' end;',
  22922. ' b.two:=b.one;',
  22923. ' b.Glob:=b.Glob;',
  22924. ' with b do begin',
  22925. ' two:=one;',
  22926. ' Glob:=Glob;',
  22927. ' end;',
  22928. '']);
  22929. ConvertProgram;
  22930. CheckSource('TestTypeHelper_ClassVar',
  22931. LinesToStr([ // statements
  22932. 'rtl.createHelper(this, "THelper", null, function () {',
  22933. ' this.One = 1;',
  22934. ' this.Two = 2;',
  22935. ' this.Glob = 0;',
  22936. ' this.Foo = function (w) {',
  22937. ' var Result = 0;',
  22938. ' Result = w;',
  22939. ' $mod.THelper.Two = 1 + w;',
  22940. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22941. ' Result = $mod.THelper.Glob;',
  22942. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22943. ' var $with = this.get();',
  22944. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22945. ' return Result;',
  22946. ' };',
  22947. ' this.Bar = function (w) {',
  22948. ' var Result = 0;',
  22949. ' Result = w;',
  22950. ' $mod.THelper.Two = 1;',
  22951. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22952. ' return Result;',
  22953. ' };',
  22954. '});',
  22955. 'this.b = 0;',
  22956. '']),
  22957. LinesToStr([ // $mod.$main
  22958. '$mod.THelper.Two = 1;',
  22959. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22960. '$mod.THelper.Two = 1;',
  22961. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22962. '$mod.THelper.Two = 1;',
  22963. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22964. 'var $with = $mod.b;',
  22965. '$mod.THelper.Two = 1;',
  22966. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22967. '']));
  22968. end;
  22969. procedure TTestModule.TestTypeHelper_PassResultElement;
  22970. begin
  22971. StartProgram(false);
  22972. Add([
  22973. '{$modeswitch typehelpers}',
  22974. 'type',
  22975. ' THelper = type helper for word',
  22976. ' procedure DoIt(e: byte = 123);',
  22977. ' class procedure DoSome(e: byte = 456); static;',
  22978. ' end;',
  22979. 'procedure THelper.DoIt(e: byte);',
  22980. 'begin',
  22981. 'end;',
  22982. 'class procedure THelper.DoSome(e: byte);',
  22983. 'begin',
  22984. 'end;',
  22985. 'function Foo(w: word): word;',
  22986. 'begin',
  22987. ' Result.DoIt;',
  22988. ' Result.DoIt();',
  22989. ' Result.DoSome;',
  22990. ' Result.DoSome();',
  22991. ' with Result do begin',
  22992. ' DoIt;',
  22993. ' DoIt();',
  22994. ' DoSome;',
  22995. ' DoSome();',
  22996. ' end;',
  22997. 'end;',
  22998. 'begin',
  22999. '']);
  23000. ConvertProgram;
  23001. CheckSource('TestTypeHelper_PassResultElement',
  23002. LinesToStr([ // statements
  23003. 'rtl.createHelper(this, "THelper", null, function () {',
  23004. ' this.DoIt = function (e) {',
  23005. ' };',
  23006. ' this.DoSome = function (e) {',
  23007. ' };',
  23008. '});',
  23009. 'this.Foo = function (w) {',
  23010. ' var Result = 0;',
  23011. ' $mod.THelper.DoIt.call({',
  23012. ' get: function () {',
  23013. ' return Result;',
  23014. ' },',
  23015. ' set: function (v) {',
  23016. ' Result = v;',
  23017. ' }',
  23018. ' }, 123);',
  23019. ' $mod.THelper.DoIt.call({',
  23020. ' get: function () {',
  23021. ' return Result;',
  23022. ' },',
  23023. ' set: function (v) {',
  23024. ' Result = v;',
  23025. ' }',
  23026. ' }, 123);',
  23027. ' $mod.THelper.DoSome(456);',
  23028. ' $mod.THelper.DoSome(456);',
  23029. ' $mod.THelper.DoIt.call({',
  23030. ' get: function () {',
  23031. ' return Result;',
  23032. ' },',
  23033. ' set: function (v) {',
  23034. ' Result = v;',
  23035. ' }',
  23036. ' }, 123);',
  23037. ' $mod.THelper.DoIt.call({',
  23038. ' get: function () {',
  23039. ' return Result;',
  23040. ' },',
  23041. ' set: function (v) {',
  23042. ' Result = v;',
  23043. ' }',
  23044. ' }, 123);',
  23045. ' $mod.THelper.DoSome(456);',
  23046. ' $mod.THelper.DoSome(456);',
  23047. ' return Result;',
  23048. '};',
  23049. '']),
  23050. LinesToStr([ // $mod.$main
  23051. '']));
  23052. end;
  23053. procedure TTestModule.TestTypeHelper_PassArgs;
  23054. begin
  23055. StartProgram(false);
  23056. Add([
  23057. '{$modeswitch typehelpers}',
  23058. 'type',
  23059. ' THelper = type helper for word',
  23060. ' procedure DoIt(e: byte = 123);',
  23061. ' end;',
  23062. 'procedure THelper.DoIt(e: byte);',
  23063. 'begin',
  23064. 'end;',
  23065. 'procedure FooDefault(a: word);',
  23066. 'begin',
  23067. ' a.DoIt;',
  23068. ' with a do DoIt;',
  23069. 'end;',
  23070. 'procedure FooConst(const a: word);',
  23071. 'begin',
  23072. ' a.DoIt;',
  23073. ' with a do DoIt;',
  23074. 'end;',
  23075. 'procedure FooVar(var a: word);',
  23076. 'begin',
  23077. ' a.DoIt;',
  23078. ' with a do DoIt;',
  23079. 'end;',
  23080. 'begin',
  23081. '']);
  23082. ConvertProgram;
  23083. CheckSource('TestTypeHelper_PassArgs',
  23084. LinesToStr([ // statements
  23085. 'rtl.createHelper(this, "THelper", null, function () {',
  23086. ' this.DoIt = function (e) {',
  23087. ' };',
  23088. '});',
  23089. 'this.FooDefault = function (a) {',
  23090. ' $mod.THelper.DoIt.call({',
  23091. ' get: function () {',
  23092. ' return a;',
  23093. ' },',
  23094. ' set: function (v) {',
  23095. ' a = v;',
  23096. ' }',
  23097. ' }, 123);',
  23098. ' $mod.THelper.DoIt.call({',
  23099. ' get: function () {',
  23100. ' return a;',
  23101. ' },',
  23102. ' set: function (v) {',
  23103. ' a = v;',
  23104. ' }',
  23105. ' }, 123);',
  23106. '};',
  23107. 'this.FooConst = function (a) {',
  23108. ' $mod.THelper.DoIt.call({',
  23109. ' get: function () {',
  23110. ' return a;',
  23111. ' },',
  23112. ' set: function (v) {',
  23113. ' rtl.raiseE("EPropReadOnly");',
  23114. ' }',
  23115. ' }, 123);',
  23116. ' $mod.THelper.DoIt.call({',
  23117. ' get: function () {',
  23118. ' return a;',
  23119. ' },',
  23120. ' set: function () {',
  23121. ' rtl.raiseE("EPropReadOnly");',
  23122. ' }',
  23123. ' }, 123);',
  23124. '};',
  23125. 'this.FooVar = function (a) {',
  23126. ' $mod.THelper.DoIt.call(a, 123);',
  23127. ' var $with = a.get();',
  23128. ' $mod.THelper.DoIt.call(a, 123);',
  23129. '};',
  23130. '']),
  23131. LinesToStr([ // $mod.$main
  23132. '']));
  23133. end;
  23134. procedure TTestModule.TestTypeHelper_PassVarConst;
  23135. begin
  23136. StartProgram(false);
  23137. Add([
  23138. '{$modeswitch typehelpers}',
  23139. 'type',
  23140. ' THelper = type helper for word',
  23141. ' procedure DoIt(e: byte = 123);',
  23142. ' end;',
  23143. 'procedure THelper.DoIt(e: byte);',
  23144. 'begin',
  23145. 'end;',
  23146. 'var a: word;',
  23147. 'const c: word = 2;',
  23148. '{$writeableconst off}',
  23149. 'const r: word = 3;',
  23150. 'begin',
  23151. ' a.DoIt;',
  23152. ' with a do DoIt;',
  23153. ' c.DoIt;',
  23154. ' with c do DoIt;',
  23155. ' r.DoIt;',
  23156. ' with r do DoIt;',
  23157. '']);
  23158. ConvertProgram;
  23159. CheckSource('TestTypeHelper_PassVarConst',
  23160. LinesToStr([ // statements
  23161. 'rtl.createHelper(this, "THelper", null, function () {',
  23162. ' this.DoIt = function (e) {',
  23163. ' };',
  23164. '});',
  23165. 'this.a = 0;',
  23166. 'this.c = 2;',
  23167. 'this.r = 3;',
  23168. '']),
  23169. LinesToStr([ // $mod.$main
  23170. '$mod.THelper.DoIt.call({',
  23171. ' p: $mod,',
  23172. ' get: function () {',
  23173. ' return this.p.a;',
  23174. ' },',
  23175. ' set: function (v) {',
  23176. ' this.p.a = v;',
  23177. ' }',
  23178. '}, 123);',
  23179. 'var $with = $mod.a;',
  23180. '$mod.THelper.DoIt.call({',
  23181. ' get: function () {',
  23182. ' return $with;',
  23183. ' },',
  23184. ' set: function (v) {',
  23185. ' $with = v;',
  23186. ' }',
  23187. '}, 123);',
  23188. '$mod.THelper.DoIt.call({',
  23189. ' p: $mod,',
  23190. ' get: function () {',
  23191. ' return this.p.c;',
  23192. ' },',
  23193. ' set: function (v) {',
  23194. ' this.p.c = v;',
  23195. ' }',
  23196. '}, 123);',
  23197. 'var $with1 = $mod.c;',
  23198. '$mod.THelper.DoIt.call({',
  23199. ' get: function () {',
  23200. ' return $with1;',
  23201. ' },',
  23202. ' set: function (v) {',
  23203. ' $with1 = v;',
  23204. ' }',
  23205. '}, 123);',
  23206. '$mod.THelper.DoIt.call({',
  23207. ' get: function () {',
  23208. ' return 3;',
  23209. ' },',
  23210. ' set: function (v) {',
  23211. ' rtl.raiseE("EPropReadOnly");',
  23212. ' }',
  23213. '}, 123);',
  23214. 'var $with2 = 3;',
  23215. ' $mod.THelper.DoIt.call({',
  23216. ' get: function () {',
  23217. ' return $with2;',
  23218. ' },',
  23219. ' set: function () {',
  23220. ' rtl.raiseE("EPropReadOnly");',
  23221. ' }',
  23222. ' }, 123);',
  23223. '']));
  23224. end;
  23225. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23226. begin
  23227. StartProgram(false);
  23228. Add([
  23229. '{$modeswitch typehelpers}',
  23230. 'type',
  23231. ' THelper = type helper for word',
  23232. ' procedure DoIt(e: byte = 123);',
  23233. ' end;',
  23234. 'procedure THelper.DoIt(e: byte);',
  23235. 'begin',
  23236. 'end;',
  23237. 'function Foo(b: byte = 1): word;',
  23238. 'begin',
  23239. 'end;',
  23240. 'begin',
  23241. ' Foo.DoIt;',
  23242. ' Foo().DoIt;',
  23243. ' with Foo do DoIt;',
  23244. ' with Foo() do DoIt;',
  23245. '']);
  23246. ConvertProgram;
  23247. CheckSource('TestTypeHelper_PassFuncResult',
  23248. LinesToStr([ // statements
  23249. 'rtl.createHelper(this, "THelper", null, function () {',
  23250. ' this.DoIt = function (e) {',
  23251. ' };',
  23252. '});',
  23253. 'this.Foo = function (b) {',
  23254. ' var Result = 0;',
  23255. ' return Result;',
  23256. '};',
  23257. '']),
  23258. LinesToStr([ // $mod.$main
  23259. '$mod.THelper.DoIt.call({',
  23260. ' a: $mod.Foo(1),',
  23261. ' get: function () {',
  23262. ' return this.a;',
  23263. ' },',
  23264. ' set: function (v) {',
  23265. ' this.a = v;',
  23266. ' }',
  23267. '}, 123);',
  23268. '$mod.THelper.DoIt.call({',
  23269. ' a: $mod.Foo(1),',
  23270. ' get: function () {',
  23271. ' return this.a;',
  23272. ' },',
  23273. ' set: function (v) {',
  23274. ' this.a = v;',
  23275. ' }',
  23276. '}, 123);',
  23277. 'var $with = $mod.Foo(1);',
  23278. '$mod.THelper.DoIt.call({',
  23279. ' get: function () {',
  23280. ' return $with;',
  23281. ' },',
  23282. ' set: function (v) {',
  23283. ' $with = v;',
  23284. ' }',
  23285. '}, 123);',
  23286. 'var $with1 = $mod.Foo(1);',
  23287. '$mod.THelper.DoIt.call({',
  23288. ' get: function () {',
  23289. ' return $with1;',
  23290. ' },',
  23291. ' set: function (v) {',
  23292. ' $with1 = v;',
  23293. ' }',
  23294. '}, 123);',
  23295. '']));
  23296. end;
  23297. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23298. begin
  23299. StartProgram(false);
  23300. Add([
  23301. '{$modeswitch typehelpers}',
  23302. 'type',
  23303. ' TObject = class',
  23304. ' FField: word;',
  23305. ' procedure SetField(Value: word);',
  23306. ' property Field: word read FField write SetField;',
  23307. ' end;',
  23308. ' THelper = type helper for word',
  23309. ' procedure Fly;',
  23310. ' class procedure Run; static;',
  23311. ' end;',
  23312. 'procedure TObject.SetField(Value: word);',
  23313. 'begin',
  23314. ' Field.Fly;',
  23315. ' Field.Run;',
  23316. ' Self.Field.Fly;',
  23317. ' Self.Field.Run;',
  23318. ' with Self do begin',
  23319. ' Field.Fly;',
  23320. ' Field.Run;',
  23321. ' end;',
  23322. ' with Self.Field do begin',
  23323. ' Fly;',
  23324. ' Run;',
  23325. ' end;',
  23326. 'end;',
  23327. 'procedure THelper.Fly;',
  23328. 'begin',
  23329. 'end;',
  23330. 'class procedure THelper.Run;',
  23331. 'begin',
  23332. 'end;',
  23333. 'var',
  23334. ' o: TObject;',
  23335. 'begin',
  23336. ' o.Field.Fly;',
  23337. ' o.Field.Run;',
  23338. ' with o do begin',
  23339. ' Field.Fly;',
  23340. ' Field.Run;',
  23341. ' end;',
  23342. ' with o.Field do begin',
  23343. ' Fly;',
  23344. ' Run;',
  23345. ' end;',
  23346. '']);
  23347. ConvertProgram;
  23348. CheckSource('TestTypeHelper_PassPropertyField',
  23349. LinesToStr([ // statements
  23350. 'rtl.createClass(this, "TObject", null, function () {',
  23351. ' this.$init = function () {',
  23352. ' this.FField = 0;',
  23353. ' };',
  23354. ' this.$final = function () {',
  23355. ' };',
  23356. ' this.SetField = function (Value) {',
  23357. ' $mod.THelper.Fly.call({',
  23358. ' p: this,',
  23359. ' get: function () {',
  23360. ' return this.p.FField;',
  23361. ' },',
  23362. ' set: function (v) {',
  23363. ' this.p.FField = v;',
  23364. ' }',
  23365. ' });',
  23366. ' $mod.THelper.Run();',
  23367. ' $mod.THelper.Fly.call({',
  23368. ' p: this,',
  23369. ' get: function () {',
  23370. ' return this.p.FField;',
  23371. ' },',
  23372. ' set: function (v) {',
  23373. ' this.p.FField = v;',
  23374. ' }',
  23375. ' });',
  23376. ' $mod.THelper.Run();',
  23377. ' $mod.THelper.Fly.call({',
  23378. ' p: this,',
  23379. ' get: function () {',
  23380. ' return this.p.FField;',
  23381. ' },',
  23382. ' set: function (v) {',
  23383. ' this.p.FField = v;',
  23384. ' }',
  23385. ' });',
  23386. ' $mod.THelper.Run();',
  23387. ' var $with = this.FField;',
  23388. ' $mod.THelper.Fly.call({',
  23389. ' get: function () {',
  23390. ' return $with;',
  23391. ' },',
  23392. ' set: function (v) {',
  23393. ' $with = v;',
  23394. ' }',
  23395. ' });',
  23396. ' $mod.THelper.Run();',
  23397. ' };',
  23398. '});',
  23399. 'rtl.createHelper(this, "THelper", null, function () {',
  23400. ' this.Fly = function () {',
  23401. ' };',
  23402. ' this.Run = function () {',
  23403. ' };',
  23404. '});',
  23405. 'this.o = null;',
  23406. '']),
  23407. LinesToStr([ // $mod.$main
  23408. '$mod.THelper.Fly.call({',
  23409. ' p: $mod.o,',
  23410. ' get: function () {',
  23411. ' return this.p.FField;',
  23412. ' },',
  23413. ' set: function (v) {',
  23414. ' this.p.FField = v;',
  23415. ' }',
  23416. '});',
  23417. '$mod.THelper.Run();',
  23418. 'var $with = $mod.o;',
  23419. '$mod.THelper.Fly.call({',
  23420. ' p: $with,',
  23421. ' get: function () {',
  23422. ' return this.p.FField;',
  23423. ' },',
  23424. ' set: function (v) {',
  23425. ' this.p.FField = v;',
  23426. ' }',
  23427. '});',
  23428. '$mod.THelper.Run();',
  23429. 'var $with1 = $mod.o.FField;',
  23430. '$mod.THelper.Fly.call({',
  23431. ' get: function () {',
  23432. ' return $with1;',
  23433. ' },',
  23434. ' set: function (v) {',
  23435. ' $with1 = v;',
  23436. ' }',
  23437. '});',
  23438. '$mod.THelper.Run();',
  23439. '']));
  23440. end;
  23441. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23442. begin
  23443. StartProgram(false);
  23444. Add([
  23445. '{$modeswitch typehelpers}',
  23446. 'type',
  23447. ' TObject = class',
  23448. ' FField: word;',
  23449. ' function GetField: word;',
  23450. ' property Field: word read GetField write FField;',
  23451. ' end;',
  23452. ' THelper = type helper for word',
  23453. ' procedure Fly;',
  23454. ' class procedure Run; static;',
  23455. ' end;',
  23456. 'function TObject.GetField: word;',
  23457. 'begin',
  23458. ' Field.Fly;',
  23459. ' Field.Run;',
  23460. ' Self.Field.Fly;',
  23461. ' Self.Field.Run;',
  23462. ' with Self do begin',
  23463. ' Field.Fly;',
  23464. ' Field.Run;',
  23465. ' end;',
  23466. ' with Self.Field do begin',
  23467. ' Fly;',
  23468. ' Run;',
  23469. ' end;',
  23470. 'end;',
  23471. 'procedure THelper.Fly;',
  23472. 'begin',
  23473. 'end;',
  23474. 'class procedure THelper.Run;',
  23475. 'begin',
  23476. 'end;',
  23477. 'var',
  23478. ' o: TObject;',
  23479. 'begin',
  23480. ' o.Field.Fly;',
  23481. ' o.Field.Run;',
  23482. ' with o do begin',
  23483. ' Field.Fly;',
  23484. ' Field.Run;',
  23485. ' end;',
  23486. ' with o.Field do begin',
  23487. ' Fly;',
  23488. ' Run;',
  23489. ' end;',
  23490. '']);
  23491. ConvertProgram;
  23492. CheckSource('TestTypeHelper_PassPropertyGetter',
  23493. LinesToStr([ // statements
  23494. 'rtl.createClass(this, "TObject", null, function () {',
  23495. ' this.$init = function () {',
  23496. ' this.FField = 0;',
  23497. ' };',
  23498. ' this.$final = function () {',
  23499. ' };',
  23500. ' this.GetField = function () {',
  23501. ' var Result = 0;',
  23502. ' $mod.THelper.Fly.call({',
  23503. ' p: this.GetField(),',
  23504. ' get: function () {',
  23505. ' return this.p;',
  23506. ' },',
  23507. ' set: function (v) {',
  23508. ' this.p = v;',
  23509. ' }',
  23510. ' });',
  23511. ' $mod.THelper.Run();',
  23512. ' $mod.THelper.Fly.call({',
  23513. ' p: this.GetField(),',
  23514. ' get: function () {',
  23515. ' return this.p;',
  23516. ' },',
  23517. ' set: function (v) {',
  23518. ' this.p = v;',
  23519. ' }',
  23520. ' });',
  23521. ' $mod.THelper.Run();',
  23522. ' $mod.THelper.Fly.call({',
  23523. ' p: this.GetField(),',
  23524. ' get: function () {',
  23525. ' return this.p;',
  23526. ' },',
  23527. ' set: function (v) {',
  23528. ' this.p = v;',
  23529. ' }',
  23530. ' });',
  23531. ' $mod.THelper.Run();',
  23532. ' var $with = this.GetField();',
  23533. ' $mod.THelper.Fly.call({',
  23534. ' get: function () {',
  23535. ' return $with;',
  23536. ' },',
  23537. ' set: function (v) {',
  23538. ' $with = v;',
  23539. ' }',
  23540. ' });',
  23541. ' $mod.THelper.Run();',
  23542. ' return Result;',
  23543. ' };',
  23544. '});',
  23545. 'rtl.createHelper(this, "THelper", null, function () {',
  23546. ' this.Fly = function () {',
  23547. ' };',
  23548. ' this.Run = function () {',
  23549. ' };',
  23550. '});',
  23551. 'this.o = null;',
  23552. '']),
  23553. LinesToStr([ // $mod.$main
  23554. '$mod.THelper.Fly.call({',
  23555. ' p: $mod.o.GetField(),',
  23556. ' get: function () {',
  23557. ' return this.p;',
  23558. ' },',
  23559. ' set: function (v) {',
  23560. ' this.p = v;',
  23561. ' }',
  23562. '});',
  23563. '$mod.THelper.Run();',
  23564. 'var $with = $mod.o;',
  23565. '$mod.THelper.Fly.call({',
  23566. ' p: $with.GetField(),',
  23567. ' get: function () {',
  23568. ' return this.p;',
  23569. ' },',
  23570. ' set: function (v) {',
  23571. ' this.p = v;',
  23572. ' }',
  23573. '});',
  23574. '$mod.THelper.Run();',
  23575. 'var $with1 = $mod.o.GetField();',
  23576. '$mod.THelper.Fly.call({',
  23577. ' get: function () {',
  23578. ' return $with1;',
  23579. ' },',
  23580. ' set: function (v) {',
  23581. ' $with1 = v;',
  23582. ' }',
  23583. '});',
  23584. '$mod.THelper.Run();',
  23585. '']));
  23586. end;
  23587. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23588. begin
  23589. StartProgram(false);
  23590. Add([
  23591. '{$modeswitch typehelpers}',
  23592. 'type',
  23593. ' TObject = class',
  23594. ' class var FField: word;',
  23595. ' class procedure SetField(Value: word);',
  23596. ' class property Field: word read FField write SetField;',
  23597. ' end;',
  23598. ' THelper = type helper for word',
  23599. ' procedure Fly(n: byte);',
  23600. ' end;',
  23601. 'class procedure TObject.SetField(Value: word);',
  23602. 'begin',
  23603. ' Field.Fly(1);',
  23604. ' Self.Field.Fly(2);',
  23605. ' with Self do Field.Fly(3);',
  23606. ' with Self.Field do Fly(4);',
  23607. ' TObject.Field.Fly(5);',
  23608. ' with TObject do Field.Fly(6);',
  23609. ' with TObject.Field do Fly(7);',
  23610. 'end;',
  23611. 'procedure THelper.Fly(n: byte);',
  23612. 'begin',
  23613. 'end;',
  23614. 'var',
  23615. ' o: TObject;',
  23616. 'begin',
  23617. ' o.Field.Fly(11);',
  23618. ' with o do Field.Fly(12);',
  23619. ' with o.Field do Fly(13);',
  23620. ' TObject.Field.Fly(14);',
  23621. ' with TObject do Field.Fly(15);',
  23622. ' with TObject.Field do Fly(16);',
  23623. '']);
  23624. ConvertProgram;
  23625. CheckSource('TestTypeHelper_PassClassPropertyField',
  23626. LinesToStr([ // statements
  23627. 'rtl.createClass(this, "TObject", null, function () {',
  23628. ' this.FField = 0;',
  23629. ' this.$init = function () {',
  23630. ' };',
  23631. ' this.$final = function () {',
  23632. ' };',
  23633. ' this.SetField = function (Value) {',
  23634. ' $mod.THelper.Fly.call({',
  23635. ' p: this,',
  23636. ' get: function () {',
  23637. ' return this.p.FField;',
  23638. ' },',
  23639. ' set: function (v) {',
  23640. ' $mod.TObject.FField = v;',
  23641. ' }',
  23642. ' }, 1);',
  23643. ' $mod.THelper.Fly.call({',
  23644. ' p: this,',
  23645. ' get: function () {',
  23646. ' return this.p.FField;',
  23647. ' },',
  23648. ' set: function (v) {',
  23649. ' $mod.TObject.FField = v;',
  23650. ' }',
  23651. ' }, 2);',
  23652. ' $mod.THelper.Fly.call({',
  23653. ' p: this,',
  23654. ' get: function () {',
  23655. ' return this.p.FField;',
  23656. ' },',
  23657. ' set: function (v) {',
  23658. ' $mod.TObject.FField = v;',
  23659. ' }',
  23660. ' }, 3);',
  23661. ' var $with = this.FField;',
  23662. ' $mod.THelper.Fly.call({',
  23663. ' get: function () {',
  23664. ' return $with;',
  23665. ' },',
  23666. ' set: function (v) {',
  23667. ' $with = v;',
  23668. ' }',
  23669. ' }, 4);',
  23670. ' $mod.THelper.Fly.call({',
  23671. ' p: $mod.TObject,',
  23672. ' get: function () {',
  23673. ' return this.p.FField;',
  23674. ' },',
  23675. ' set: function (v) {',
  23676. ' $mod.TObject.FField = v;',
  23677. ' }',
  23678. ' }, 5);',
  23679. ' var $with1 = $mod.TObject;',
  23680. ' $mod.THelper.Fly.call({',
  23681. ' p: $with1,',
  23682. ' get: function () {',
  23683. ' return this.p.FField;',
  23684. ' },',
  23685. ' set: function (v) {',
  23686. ' $mod.TObject.FField = v;',
  23687. ' }',
  23688. ' }, 6);',
  23689. ' var $with2 = $mod.TObject.FField;',
  23690. ' $mod.THelper.Fly.call({',
  23691. ' get: function () {',
  23692. ' return $with2;',
  23693. ' },',
  23694. ' set: function (v) {',
  23695. ' $with2 = v;',
  23696. ' }',
  23697. ' }, 7);',
  23698. ' };',
  23699. '});',
  23700. 'rtl.createHelper(this, "THelper", null, function () {',
  23701. ' this.Fly = function (n) {',
  23702. ' };',
  23703. '});',
  23704. 'this.o = null;',
  23705. '']),
  23706. LinesToStr([ // $mod.$main
  23707. '$mod.THelper.Fly.call({',
  23708. ' p: $mod.o,',
  23709. ' get: function () {',
  23710. ' return this.p.FField;',
  23711. ' },',
  23712. ' set: function (v) {',
  23713. ' $mod.TObject.FField = v;',
  23714. ' }',
  23715. '}, 11);',
  23716. 'var $with = $mod.o;',
  23717. '$mod.THelper.Fly.call({',
  23718. ' p: $with,',
  23719. ' get: function () {',
  23720. ' return this.p.FField;',
  23721. ' },',
  23722. ' set: function (v) {',
  23723. ' $mod.TObject.FField = v;',
  23724. ' }',
  23725. '}, 12);',
  23726. 'var $with1 = $mod.o.FField;',
  23727. '$mod.THelper.Fly.call({',
  23728. ' get: function () {',
  23729. ' return $with1;',
  23730. ' },',
  23731. ' set: function (v) {',
  23732. ' $with1 = v;',
  23733. ' }',
  23734. '}, 13);',
  23735. '$mod.THelper.Fly.call({',
  23736. ' p: $mod.TObject,',
  23737. ' get: function () {',
  23738. ' return this.p.FField;',
  23739. ' },',
  23740. ' set: function (v) {',
  23741. ' $mod.TObject.FField = v;',
  23742. ' }',
  23743. '}, 14);',
  23744. 'var $with2 = $mod.TObject;',
  23745. '$mod.THelper.Fly.call({',
  23746. ' p: $with2,',
  23747. ' get: function () {',
  23748. ' return this.p.FField;',
  23749. ' },',
  23750. ' set: function (v) {',
  23751. ' $mod.TObject.FField = v;',
  23752. ' }',
  23753. '}, 15);',
  23754. 'var $with3 = $mod.TObject.FField;',
  23755. '$mod.THelper.Fly.call({',
  23756. ' get: function () {',
  23757. ' return $with3;',
  23758. ' },',
  23759. ' set: function (v) {',
  23760. ' $with3 = v;',
  23761. ' }',
  23762. '}, 16);',
  23763. '']));
  23764. end;
  23765. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23766. begin
  23767. StartProgram(false);
  23768. Add([
  23769. '{$modeswitch typehelpers}',
  23770. 'type',
  23771. ' TObject = class',
  23772. ' class var FField: word;',
  23773. ' class function GetField: word; static;',
  23774. ' class property Field: word read GetField write FField;',
  23775. ' end;',
  23776. ' THelper = type helper for word',
  23777. ' procedure Fly(n: byte);',
  23778. ' end;',
  23779. 'class function TObject.GetField: word;',
  23780. 'begin',
  23781. ' Field.Fly(1);',
  23782. ' TObject.Field.Fly(5);',
  23783. ' with TObject do Field.Fly(6);',
  23784. ' with TObject.Field do Fly(7);',
  23785. 'end;',
  23786. 'procedure THelper.Fly(n: byte);',
  23787. 'begin',
  23788. 'end;',
  23789. 'var',
  23790. ' o: TObject;',
  23791. 'begin',
  23792. ' o.Field.Fly(11);',
  23793. ' with o do Field.Fly(12);',
  23794. ' with o.Field do Fly(13);',
  23795. '']);
  23796. ConvertProgram;
  23797. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23798. LinesToStr([ // statements
  23799. 'rtl.createClass(this, "TObject", null, function () {',
  23800. ' this.FField = 0;',
  23801. ' this.$init = function () {',
  23802. ' };',
  23803. ' this.$final = function () {',
  23804. ' };',
  23805. ' this.GetField = function () {',
  23806. ' var Result = 0;',
  23807. ' $mod.THelper.Fly.call({',
  23808. ' p: $mod.TObject.GetField(),',
  23809. ' get: function () {',
  23810. ' return this.p;',
  23811. ' },',
  23812. ' set: function (v) {',
  23813. ' this.p = v;',
  23814. ' }',
  23815. ' }, 1);',
  23816. ' $mod.THelper.Fly.call({',
  23817. ' p: $mod.TObject.GetField(),',
  23818. ' get: function () {',
  23819. ' return this.p;',
  23820. ' },',
  23821. ' set: function (v) {',
  23822. ' this.p = v;',
  23823. ' }',
  23824. ' }, 5);',
  23825. ' var $with = $mod.TObject;',
  23826. ' $mod.THelper.Fly.call({',
  23827. ' p: $with.GetField(),',
  23828. ' get: function () {',
  23829. ' return this.p;',
  23830. ' },',
  23831. ' set: function (v) {',
  23832. ' this.p = v;',
  23833. ' }',
  23834. ' }, 6);',
  23835. ' var $with1 = $mod.TObject.GetField();',
  23836. ' $mod.THelper.Fly.call({',
  23837. ' get: function () {',
  23838. ' return $with1;',
  23839. ' },',
  23840. ' set: function (v) {',
  23841. ' $with1 = v;',
  23842. ' }',
  23843. ' }, 7);',
  23844. ' return Result;',
  23845. ' };',
  23846. '});',
  23847. 'rtl.createHelper(this, "THelper", null, function () {',
  23848. ' this.Fly = function (n) {',
  23849. ' };',
  23850. '});',
  23851. 'this.o = null;',
  23852. '']),
  23853. LinesToStr([ // $mod.$main
  23854. '$mod.THelper.Fly.call({',
  23855. ' p: $mod.TObject.GetField(),',
  23856. ' get: function () {',
  23857. ' return this.p;',
  23858. ' },',
  23859. ' set: function (v) {',
  23860. ' this.p = v;',
  23861. ' }',
  23862. '}, 11);',
  23863. 'var $with = $mod.o;',
  23864. '$mod.THelper.Fly.call({',
  23865. ' p: $with.GetField(),',
  23866. ' get: function () {',
  23867. ' return this.p;',
  23868. ' },',
  23869. ' set: function (v) {',
  23870. ' this.p = v;',
  23871. ' }',
  23872. '}, 12);',
  23873. 'var $with1 = $mod.TObject.GetField();',
  23874. '$mod.THelper.Fly.call({',
  23875. ' get: function () {',
  23876. ' return $with1;',
  23877. ' },',
  23878. ' set: function (v) {',
  23879. ' $with1 = v;',
  23880. ' }',
  23881. '}, 13);',
  23882. '']));
  23883. end;
  23884. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23885. begin
  23886. StartProgram(false);
  23887. Add([
  23888. '{$modeswitch typehelpers}',
  23889. 'type',
  23890. ' TObject = class',
  23891. ' class var FField: word;',
  23892. ' class function GetField: word;',
  23893. ' class property Field: word read GetField write FField;',
  23894. ' end;',
  23895. ' TClass = class of TObject;',
  23896. ' THelper = type helper for word',
  23897. ' procedure Fly(n: byte);',
  23898. ' end;',
  23899. 'class function TObject.GetField: word;',
  23900. 'begin',
  23901. ' Field.Fly(1);',
  23902. ' Self.Field.Fly(5);',
  23903. ' with Self do Field.Fly(6);',
  23904. ' with Self.Field do Fly(7);',
  23905. 'end;',
  23906. 'procedure THelper.Fly(n: byte);',
  23907. 'begin',
  23908. 'end;',
  23909. 'var',
  23910. ' o: TObject;',
  23911. ' c: TClass;',
  23912. 'begin',
  23913. ' o.Field.Fly(11);',
  23914. ' with o do Field.Fly(12);',
  23915. ' with o.Field do Fly(13);',
  23916. ' c.Field.Fly(14);',
  23917. ' with c do Field.Fly(15);',
  23918. ' with c.Field do Fly(16);',
  23919. '']);
  23920. ConvertProgram;
  23921. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23922. LinesToStr([ // statements
  23923. 'rtl.createClass(this, "TObject", null, function () {',
  23924. ' this.FField = 0;',
  23925. ' this.$init = function () {',
  23926. ' };',
  23927. ' this.$final = function () {',
  23928. ' };',
  23929. ' this.GetField = function () {',
  23930. ' var Result = 0;',
  23931. ' $mod.THelper.Fly.call({',
  23932. ' p: this.GetField(),',
  23933. ' get: function () {',
  23934. ' return this.p;',
  23935. ' },',
  23936. ' set: function (v) {',
  23937. ' this.p = v;',
  23938. ' }',
  23939. ' }, 1);',
  23940. ' $mod.THelper.Fly.call({',
  23941. ' p: this.GetField(),',
  23942. ' get: function () {',
  23943. ' return this.p;',
  23944. ' },',
  23945. ' set: function (v) {',
  23946. ' this.p = v;',
  23947. ' }',
  23948. ' }, 5);',
  23949. ' $mod.THelper.Fly.call({',
  23950. ' p: this.GetField(),',
  23951. ' get: function () {',
  23952. ' return this.p;',
  23953. ' },',
  23954. ' set: function (v) {',
  23955. ' this.p = v;',
  23956. ' }',
  23957. ' }, 6);',
  23958. ' var $with = this.GetField();',
  23959. ' $mod.THelper.Fly.call({',
  23960. ' get: function () {',
  23961. ' return $with;',
  23962. ' },',
  23963. ' set: function (v) {',
  23964. ' $with = v;',
  23965. ' }',
  23966. ' }, 7);',
  23967. ' return Result;',
  23968. ' };',
  23969. '});',
  23970. 'rtl.createHelper(this, "THelper", null, function () {',
  23971. ' this.Fly = function (n) {',
  23972. ' };',
  23973. '});',
  23974. 'this.o = null;',
  23975. 'this.c = null;',
  23976. '']),
  23977. LinesToStr([ // $mod.$main
  23978. '$mod.THelper.Fly.call({',
  23979. ' p: $mod.o.$class.GetField(),',
  23980. ' get: function () {',
  23981. ' return this.p;',
  23982. ' },',
  23983. ' set: function (v) {',
  23984. ' this.p = v;',
  23985. ' }',
  23986. '}, 11);',
  23987. 'var $with = $mod.o;',
  23988. '$mod.THelper.Fly.call({',
  23989. ' p: $with.$class.GetField(),',
  23990. ' get: function () {',
  23991. ' return this.p;',
  23992. ' },',
  23993. ' set: function (v) {',
  23994. ' this.p = v;',
  23995. ' }',
  23996. '}, 12);',
  23997. 'var $with1 = $mod.o.$class.GetField();',
  23998. '$mod.THelper.Fly.call({',
  23999. ' get: function () {',
  24000. ' return $with1;',
  24001. ' },',
  24002. ' set: function (v) {',
  24003. ' $with1 = v;',
  24004. ' }',
  24005. '}, 13);',
  24006. '$mod.THelper.Fly.call({',
  24007. ' p: $mod.c.GetField(),',
  24008. ' get: function () {',
  24009. ' return this.p;',
  24010. ' },',
  24011. ' set: function (v) {',
  24012. ' this.p = v;',
  24013. ' }',
  24014. '}, 14);',
  24015. 'var $with2 = $mod.c;',
  24016. '$mod.THelper.Fly.call({',
  24017. ' p: $with2.GetField(),',
  24018. ' get: function () {',
  24019. ' return this.p;',
  24020. ' },',
  24021. ' set: function (v) {',
  24022. ' this.p = v;',
  24023. ' }',
  24024. '}, 15);',
  24025. 'var $with3 = $mod.c.GetField();',
  24026. '$mod.THelper.Fly.call({',
  24027. ' get: function () {',
  24028. ' return $with3;',
  24029. ' },',
  24030. ' set: function (v) {',
  24031. ' $with3 = v;',
  24032. ' }',
  24033. '}, 16);',
  24034. '']));
  24035. end;
  24036. procedure TTestModule.TestTypeHelper_Property;
  24037. begin
  24038. StartProgram(false);
  24039. Add([
  24040. '{$modeswitch typehelpers}',
  24041. 'type',
  24042. ' THelper = type helper for word',
  24043. ' function GetSize: longint;',
  24044. ' procedure SetSize(Value: longint);',
  24045. ' property Size: longint read GetSize write SetSize;',
  24046. ' end;',
  24047. 'function THelper.GetSize: longint;',
  24048. 'begin',
  24049. ' Result:=Size+1;',
  24050. ' Size:=2;',
  24051. ' Result:=Self.Size+3;',
  24052. ' Self.Size:=4;',
  24053. ' with Self do begin',
  24054. ' Result:=Size+5;',
  24055. ' Size:=6;',
  24056. ' end;',
  24057. 'end;',
  24058. 'procedure THelper.SetSize(Value: longint);',
  24059. 'begin',
  24060. 'end;',
  24061. 'var w: word;',
  24062. 'begin',
  24063. ' w:=w.Size+7;',
  24064. ' w.Size:=w+8;',
  24065. ' with w do begin',
  24066. ' w:=Size+9;',
  24067. ' Size:=w+10;',
  24068. ' end;',
  24069. '']);
  24070. ConvertProgram;
  24071. CheckSource('TestTypeHelper_Property',
  24072. LinesToStr([ // statements
  24073. 'rtl.createHelper(this, "THelper", null, function () {',
  24074. ' this.GetSize = function () {',
  24075. ' var Result = 0;',
  24076. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24077. ' $mod.THelper.SetSize.call(this, 2);',
  24078. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24079. ' $mod.THelper.SetSize.call(this, 4);',
  24080. ' var $with = this.get();',
  24081. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24082. ' $mod.THelper.SetSize.call(this, 6);',
  24083. ' return Result;',
  24084. ' };',
  24085. ' this.SetSize = function (Value) {',
  24086. ' };',
  24087. '});',
  24088. 'this.w = 0;',
  24089. '']),
  24090. LinesToStr([ // $mod.$main
  24091. '$mod.w = $mod.THelper.GetSize.call({',
  24092. ' p: $mod,',
  24093. ' get: function () {',
  24094. ' return this.p.w;',
  24095. ' },',
  24096. ' set: function (v) {',
  24097. ' this.p.w = v;',
  24098. ' }',
  24099. '}) + 7;',
  24100. '$mod.THelper.SetSize.call({',
  24101. ' p: $mod,',
  24102. ' get: function () {',
  24103. ' return this.p.w;',
  24104. ' },',
  24105. ' set: function (v) {',
  24106. ' this.p.w = v;',
  24107. ' }',
  24108. '}, $mod.w + 8);',
  24109. 'var $with = $mod.w;',
  24110. '$mod.w = $mod.THelper.GetSize.call({',
  24111. ' get: function () {',
  24112. ' return $with;',
  24113. ' },',
  24114. ' set: function (v) {',
  24115. ' $with = v;',
  24116. ' }',
  24117. '}) + 9;',
  24118. '$mod.THelper.SetSize.call({',
  24119. ' get: function () {',
  24120. ' return $with;',
  24121. ' },',
  24122. ' set: function (v) {',
  24123. ' $with = v;',
  24124. ' }',
  24125. '}, $mod.w + 10);',
  24126. '']));
  24127. end;
  24128. procedure TTestModule.TestTypeHelper_Property_Array;
  24129. begin
  24130. StartProgram(false);
  24131. Add([
  24132. '{$modeswitch typehelpers}',
  24133. 'type',
  24134. ' THelper = type helper for word',
  24135. ' function GetItems(Index: byte): boolean;',
  24136. ' procedure SetItems(Index: byte; Value: boolean);',
  24137. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24138. ' end;',
  24139. 'function THelper.GetItems(Index: byte): boolean;',
  24140. 'begin',
  24141. ' Result:=Items[1];',
  24142. ' Items[2]:=false;',
  24143. ' Result:=Self.Items[3];',
  24144. ' Self.Items[4]:=true;',
  24145. ' with Self do begin',
  24146. ' Result:=Items[5];',
  24147. ' Items[6]:=false;',
  24148. ' end;',
  24149. 'end;',
  24150. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24151. 'begin',
  24152. 'end;',
  24153. 'var',
  24154. ' w: word;',
  24155. ' b: boolean;',
  24156. 'begin',
  24157. ' b:=w.Items[1];',
  24158. ' w.Items[2]:=b;',
  24159. ' with w do begin',
  24160. ' b:=Items[3];',
  24161. ' Items[4]:=b;',
  24162. ' end;',
  24163. '']);
  24164. ConvertProgram;
  24165. CheckSource('TestTypeHelper_Property_Array',
  24166. LinesToStr([ // statements
  24167. 'rtl.createHelper(this, "THelper", null, function () {',
  24168. ' this.GetItems = function (Index) {',
  24169. ' var Result = false;',
  24170. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24171. ' $mod.THelper.SetItems.call(this, 2, false);',
  24172. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24173. ' $mod.THelper.SetItems.call(this, 4, true);',
  24174. ' var $with = this.get();',
  24175. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24176. ' $mod.THelper.SetItems.call(this, 6, false);',
  24177. ' return Result;',
  24178. ' };',
  24179. ' this.SetItems = function (Index, Value) {',
  24180. ' };',
  24181. '});',
  24182. 'this.w = 0;',
  24183. 'this.b = false;',
  24184. '']),
  24185. LinesToStr([ // $mod.$main
  24186. '$mod.b = $mod.THelper.GetItems.call({',
  24187. ' p: $mod,',
  24188. ' get: function () {',
  24189. ' return this.p.w;',
  24190. ' },',
  24191. ' set: function (v) {',
  24192. ' this.p.w = v;',
  24193. ' }',
  24194. '}, 1);',
  24195. '$mod.THelper.SetItems.call({',
  24196. ' p: $mod,',
  24197. ' get: function () {',
  24198. ' return this.p.w;',
  24199. ' },',
  24200. ' set: function (v) {',
  24201. ' this.p.w = v;',
  24202. ' }',
  24203. '}, 2, $mod.b);',
  24204. 'var $with = $mod.w;',
  24205. '$mod.b = $mod.THelper.GetItems.call({',
  24206. ' get: function () {',
  24207. ' return $with;',
  24208. ' },',
  24209. ' set: function (v) {',
  24210. ' $with = v;',
  24211. ' }',
  24212. '}, 3);',
  24213. '$mod.THelper.SetItems.call({',
  24214. ' get: function () {',
  24215. ' return $with;',
  24216. ' },',
  24217. ' set: function (v) {',
  24218. ' $with = v;',
  24219. ' }',
  24220. '}, 4, $mod.b);',
  24221. '']));
  24222. end;
  24223. procedure TTestModule.TestTypeHelper_ClassProperty;
  24224. begin
  24225. StartProgram(false);
  24226. Add([
  24227. '{$modeswitch typehelpers}',
  24228. 'type',
  24229. ' THelper = type helper for word',
  24230. ' class function GetSize: longint; static;',
  24231. ' class procedure SetSize(Value: longint); static;',
  24232. ' class property Size: longint read GetSize write SetSize;',
  24233. ' end;',
  24234. 'class function THelper.GetSize: longint;',
  24235. 'begin',
  24236. ' Result:=Size+1;',
  24237. ' Size:=2;',
  24238. 'end;',
  24239. 'class procedure THelper.SetSize(Value: longint);',
  24240. 'begin',
  24241. 'end;',
  24242. 'begin',
  24243. '']);
  24244. ConvertProgram;
  24245. CheckSource('TestTypeHelper_ClassProperty',
  24246. LinesToStr([ // statements
  24247. 'rtl.createHelper(this, "THelper", null, function () {',
  24248. ' this.GetSize = function () {',
  24249. ' var Result = 0;',
  24250. ' Result = $mod.THelper.GetSize() + 1;',
  24251. ' $mod.THelper.SetSize(2);',
  24252. ' return Result;',
  24253. ' };',
  24254. ' this.SetSize = function (Value) {',
  24255. ' };',
  24256. '});',
  24257. '']),
  24258. LinesToStr([ // $mod.$main
  24259. '']));
  24260. end;
  24261. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24262. begin
  24263. StartProgram(false);
  24264. Add([
  24265. '{$modeswitch typehelpers}',
  24266. 'type',
  24267. ' THelper = type helper for word',
  24268. ' class function GetItems(Index: byte): boolean; static;',
  24269. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24270. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24271. ' end;',
  24272. 'class function THelper.GetItems(Index: byte): boolean;',
  24273. 'begin',
  24274. ' Result:=Items[1];',
  24275. ' Items[2]:=false;',
  24276. 'end;',
  24277. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24278. 'begin',
  24279. 'end;',
  24280. 'var',
  24281. ' w: word;',
  24282. ' b: boolean;',
  24283. 'begin',
  24284. ' b:=w.Items[1];',
  24285. ' w.Items[2]:=b;',
  24286. ' with w do begin',
  24287. ' b:=Items[3];',
  24288. ' Items[4]:=b;',
  24289. ' end;',
  24290. '']);
  24291. ConvertProgram;
  24292. CheckSource('TestTypeHelper_ClassProperty_Array',
  24293. LinesToStr([ // statements
  24294. 'rtl.createHelper(this, "THelper", null, function () {',
  24295. ' this.GetItems = function (Index) {',
  24296. ' var Result = false;',
  24297. ' Result = $mod.THelper.GetItems(1);',
  24298. ' $mod.THelper.SetItems(2, false);',
  24299. ' return Result;',
  24300. ' };',
  24301. ' this.SetItems = function (Index, Value) {',
  24302. ' };',
  24303. '});',
  24304. 'this.w = 0;',
  24305. 'this.b = false;',
  24306. '']),
  24307. LinesToStr([ // $mod.$main
  24308. '$mod.b = $mod.THelper.GetItems(1);',
  24309. '$mod.THelper.SetItems(2, $mod.b);',
  24310. 'var $with = $mod.w;',
  24311. '$mod.b = $mod.THelper.GetItems(3);',
  24312. '$mod.THelper.SetItems(4, $mod.b);',
  24313. '']));
  24314. end;
  24315. procedure TTestModule.TestTypeHelper_ClassMethod;
  24316. begin
  24317. StartProgram(false);
  24318. Add([
  24319. '{$modeswitch typehelpers}',
  24320. 'type',
  24321. ' THelper = type helper for word',
  24322. ' class procedure DoStatic; static;',
  24323. ' end;',
  24324. 'class procedure THelper.DoStatic;',
  24325. 'begin',
  24326. ' DoStatic;',
  24327. ' DoStatic();',
  24328. 'end;',
  24329. 'var w: word;',
  24330. 'begin',
  24331. ' w.DoStatic;',
  24332. ' w.DoStatic();',
  24333. '']);
  24334. ConvertProgram;
  24335. CheckSource('TestTypeHelper_ClassMethod',
  24336. LinesToStr([ // statements
  24337. 'rtl.createHelper(this, "THelper", null, function () {',
  24338. ' this.DoStatic = function () {',
  24339. ' $mod.THelper.DoStatic();',
  24340. ' $mod.THelper.DoStatic();',
  24341. ' };',
  24342. '});',
  24343. 'this.w = 0;',
  24344. '']),
  24345. LinesToStr([ // $mod.$main
  24346. '$mod.THelper.DoStatic();',
  24347. '$mod.THelper.DoStatic();',
  24348. '']));
  24349. end;
  24350. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24351. begin
  24352. StartProgram(false);
  24353. Add([
  24354. '{$modeswitch typehelpers}',
  24355. 'type',
  24356. ' THelper = type helper for word',
  24357. ' procedure Run; external name ''Run'';',
  24358. ' end;',
  24359. 'var w: word;',
  24360. 'begin',
  24361. ' w.Run;',
  24362. '']);
  24363. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24364. ConvertProgram;
  24365. end;
  24366. procedure TTestModule.TestTypeHelper_Constructor;
  24367. begin
  24368. StartProgram(false);
  24369. Add([
  24370. '{$modeswitch typehelpers}',
  24371. 'type',
  24372. ' THelper = type helper for word',
  24373. ' constructor Init(e: longint);',
  24374. ' end;',
  24375. 'constructor THelper.Init(e: longint);',
  24376. 'begin',
  24377. ' Self:=e;',
  24378. ' Init(e+1);',
  24379. 'end;',
  24380. 'var w: word;',
  24381. 'begin',
  24382. ' w:=word.Init(2);',
  24383. ' w:=w.Init(3);',
  24384. ' with word do w:=Init(4);',
  24385. ' with w do w:=Init(5);',
  24386. '']);
  24387. ConvertProgram;
  24388. CheckSource('TestTypeHelper_Constructor',
  24389. LinesToStr([ // statements
  24390. 'rtl.createHelper(this, "THelper", null, function () {',
  24391. ' this.Init = function (e) {',
  24392. ' this.set(e);',
  24393. ' $mod.THelper.Init.call(this, e + 1);',
  24394. ' return this.get();',
  24395. ' };',
  24396. ' this.$new = function (fn, args) {',
  24397. ' return this[fn].apply({',
  24398. ' p: 0,',
  24399. ' get: function () {',
  24400. ' return this.p;',
  24401. ' },',
  24402. ' set: function (v) {',
  24403. ' this.p = v;',
  24404. ' }',
  24405. ' }, args);',
  24406. ' };',
  24407. '});',
  24408. 'this.w = 0;',
  24409. '']),
  24410. LinesToStr([ // $mod.$main
  24411. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24412. '$mod.w = $mod.THelper.Init.call({',
  24413. ' p: $mod,',
  24414. ' get: function () {',
  24415. ' return this.p.w;',
  24416. ' },',
  24417. ' set: function (v) {',
  24418. ' this.p.w = v;',
  24419. ' }',
  24420. '}, 3);',
  24421. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24422. 'var $with = $mod.w;',
  24423. '$mod.w = $mod.THelper.Init.call({',
  24424. ' get: function () {',
  24425. ' return $with;',
  24426. ' },',
  24427. ' set: function (v) {',
  24428. ' $with = v;',
  24429. ' }',
  24430. '}, 5);',
  24431. '']));
  24432. end;
  24433. procedure TTestModule.TestTypeHelper_Word;
  24434. begin
  24435. StartProgram(false);
  24436. Add([
  24437. '{$modeswitch typehelpers}',
  24438. 'type',
  24439. ' THelper = type helper for word',
  24440. ' procedure DoIt(e: byte = 123);',
  24441. ' end;',
  24442. 'procedure THelper.DoIt(e: byte);',
  24443. 'begin',
  24444. ' Self:=e;',
  24445. ' Self:=Self+1;',
  24446. ' with Self do Doit;',
  24447. 'end;',
  24448. 'begin',
  24449. ' word(3).DoIt;',
  24450. '']);
  24451. ConvertProgram;
  24452. CheckSource('TestTypeHelper_Word',
  24453. LinesToStr([ // statements
  24454. 'rtl.createHelper(this, "THelper", null, function () {',
  24455. ' this.DoIt = function (e) {',
  24456. ' this.set(e);',
  24457. ' this.set(this.get() + 1);',
  24458. ' var $with = this.get();',
  24459. ' $mod.THelper.DoIt.call(this, 123);',
  24460. ' };',
  24461. '});',
  24462. '']),
  24463. LinesToStr([ // $mod.$main
  24464. '$mod.THelper.DoIt.call({',
  24465. ' get: function () {',
  24466. ' return 3;',
  24467. ' },',
  24468. ' set: function (v) {',
  24469. ' rtl.raiseE("EPropReadOnly");',
  24470. ' }',
  24471. '}, 123);',
  24472. '']));
  24473. end;
  24474. procedure TTestModule.TestTypeHelper_Boolean;
  24475. begin
  24476. StartProgram(false);
  24477. Add([
  24478. '{$modeswitch typehelpers}',
  24479. 'type',
  24480. ' Integer = longint;',
  24481. ' THelper = type helper for boolean',
  24482. ' procedure Run(e: wordbool = true);',
  24483. ' end;',
  24484. 'procedure THelper.Run(e: wordbool);',
  24485. 'begin',
  24486. ' Self:=e;',
  24487. ' Self:=not Self;',
  24488. ' with Self do Run;',
  24489. ' if Integer(Self)=0 then ;',
  24490. 'end;',
  24491. 'begin',
  24492. ' boolean(3).Run;',
  24493. '']);
  24494. ConvertProgram;
  24495. CheckSource('TestTypeHelper_Boolean',
  24496. LinesToStr([ // statements
  24497. 'rtl.createHelper(this, "THelper", null, function () {',
  24498. ' this.Run = function (e) {',
  24499. ' this.set(e);',
  24500. ' this.set(!this.get());',
  24501. ' var $with = this.get();',
  24502. ' $mod.THelper.Run.call(this, true);',
  24503. ' if ((this.get() ? 1 : 0) === 0) ;',
  24504. ' };',
  24505. '});',
  24506. '']),
  24507. LinesToStr([ // $mod.$main
  24508. '$mod.THelper.Run.call({',
  24509. ' a: 3 != 0,',
  24510. ' get: function () {',
  24511. ' return this.a;',
  24512. ' },',
  24513. ' set: function (v) {',
  24514. ' rtl.raiseE("EPropReadOnly");',
  24515. ' }',
  24516. '}, true);',
  24517. '']));
  24518. end;
  24519. procedure TTestModule.TestTypeHelper_WordBool;
  24520. begin
  24521. StartProgram(false);
  24522. Add([
  24523. '{$modeswitch typehelpers}',
  24524. 'type',
  24525. ' Integer = longint;',
  24526. ' THelper = type helper for WordBool',
  24527. ' procedure Run(e: wordbool = true);',
  24528. ' end;',
  24529. 'procedure THelper.Run(e: wordbool);',
  24530. 'var i: integer;',
  24531. 'begin',
  24532. ' i:=Integer(Self);',
  24533. 'end;',
  24534. 'var w: wordbool;',
  24535. 'begin',
  24536. ' w.Run;',
  24537. ' wordbool(3).Run;',
  24538. '']);
  24539. ConvertProgram;
  24540. CheckSource('TestTypeHelper_WordBool',
  24541. LinesToStr([ // statements
  24542. 'rtl.createHelper(this, "THelper", null, function () {',
  24543. ' this.Run = function (e) {',
  24544. ' var i = 0;',
  24545. ' i = (this.get() ? 1 : 0);',
  24546. ' };',
  24547. '});',
  24548. 'this.w = false;',
  24549. '']),
  24550. LinesToStr([ // $mod.$main
  24551. '$mod.THelper.Run.call({',
  24552. ' p: $mod,',
  24553. ' get: function () {',
  24554. ' return this.p.w;',
  24555. ' },',
  24556. ' set: function (v) {',
  24557. ' this.p.w = v;',
  24558. ' }',
  24559. '}, true);',
  24560. '$mod.THelper.Run.call({',
  24561. ' a: 3 != 0,',
  24562. ' get: function () {',
  24563. ' return this.a;',
  24564. ' },',
  24565. ' set: function (v) {',
  24566. ' rtl.raiseE("EPropReadOnly");',
  24567. ' }',
  24568. '}, true);',
  24569. '']));
  24570. end;
  24571. procedure TTestModule.TestTypeHelper_Double;
  24572. begin
  24573. StartProgram(false);
  24574. Add([
  24575. '{$modeswitch typehelpers}',
  24576. 'type',
  24577. ' Float = type double;',
  24578. ' THelper = type helper for Float',
  24579. ' const NPI = 3.141592;',
  24580. ' function ToStr: String;',
  24581. ' end;',
  24582. 'function THelper.ToStr: String;',
  24583. 'begin',
  24584. 'end;',
  24585. 'procedure DoIt(s: string);',
  24586. 'begin',
  24587. 'end;',
  24588. 'var f: Float;',
  24589. 'begin',
  24590. ' DoIt(f.toStr);',
  24591. ' DoIt(f.toStr());',
  24592. ' (f*f).toStr;',
  24593. ' DoIt((f*f).toStr);',
  24594. '']);
  24595. ConvertProgram;
  24596. CheckSource('TestTypeHelper_Double',
  24597. LinesToStr([ // statements
  24598. 'rtl.createHelper(this, "THelper", null, function () {',
  24599. ' this.NPI = 3.141592;',
  24600. ' this.ToStr = function () {',
  24601. ' var Result = "";',
  24602. ' return Result;',
  24603. ' };',
  24604. '});',
  24605. 'this.DoIt = function (s) {',
  24606. '};',
  24607. 'this.f = 0.0;',
  24608. '']),
  24609. LinesToStr([ // $mod.$main
  24610. '$mod.DoIt($mod.THelper.ToStr.call({',
  24611. ' p: $mod,',
  24612. ' get: function () {',
  24613. ' return this.p.f;',
  24614. ' },',
  24615. ' set: function (v) {',
  24616. ' this.p.f = v;',
  24617. ' }',
  24618. '}));',
  24619. '$mod.DoIt($mod.THelper.ToStr.call({',
  24620. ' p: $mod,',
  24621. ' get: function () {',
  24622. ' return this.p.f;',
  24623. ' },',
  24624. ' set: function (v) {',
  24625. ' this.p.f = v;',
  24626. ' }',
  24627. '}));',
  24628. '$mod.THelper.ToStr.call({',
  24629. ' a: $mod.f * $mod.f,',
  24630. ' get: function () {',
  24631. ' return this.a;',
  24632. ' },',
  24633. ' set: function (v) {',
  24634. ' rtl.raiseE("EPropReadOnly");',
  24635. ' }',
  24636. '});',
  24637. '$mod.DoIt($mod.THelper.ToStr.call({',
  24638. ' a: $mod.f * $mod.f,',
  24639. ' get: function () {',
  24640. ' return this.a;',
  24641. ' },',
  24642. ' set: function (v) {',
  24643. ' rtl.raiseE("EPropReadOnly");',
  24644. ' }',
  24645. '}));',
  24646. '']));
  24647. end;
  24648. procedure TTestModule.TestTypeHelper_NativeInt;
  24649. begin
  24650. StartProgram(false);
  24651. Add([
  24652. '{$modeswitch typehelpers}',
  24653. 'type',
  24654. ' MaxInt = type nativeint;',
  24655. ' THelperI = type helper for MaxInt',
  24656. ' function ToStr: String;',
  24657. ' end;',
  24658. ' MaxUInt = type nativeuint;',
  24659. ' THelperU = type helper for MaxUInt',
  24660. ' function ToStr: String;',
  24661. ' end;',
  24662. 'function THelperI.ToStr: String;',
  24663. 'begin',
  24664. ' Result:=str(Self);',
  24665. 'end;',
  24666. 'function THelperU.ToStr: String;',
  24667. 'begin',
  24668. ' Result:=str(Self);',
  24669. 'end;',
  24670. 'procedure DoIt(s: string);',
  24671. 'begin',
  24672. 'end;',
  24673. 'var i: MaxInt;',
  24674. 'begin',
  24675. ' DoIt(i.toStr);',
  24676. ' DoIt(i.toStr());',
  24677. ' (i*i).toStr;',
  24678. ' DoIt((i*i).toStr);',
  24679. '']);
  24680. ConvertProgram;
  24681. CheckSource('TestTypeHelper_NativeInt',
  24682. LinesToStr([ // statements
  24683. 'rtl.createHelper(this, "THelperI", null, function () {',
  24684. ' this.ToStr = function () {',
  24685. ' var Result = "";',
  24686. ' Result = "" + this.get();',
  24687. ' return Result;',
  24688. ' };',
  24689. '});',
  24690. 'rtl.createHelper(this, "THelperU", null, function () {',
  24691. ' this.ToStr = function () {',
  24692. ' var Result = "";',
  24693. ' Result = "" + this.get();',
  24694. ' return Result;',
  24695. ' };',
  24696. '});',
  24697. 'this.DoIt = function (s) {',
  24698. '};',
  24699. 'this.i = 0;',
  24700. '']),
  24701. LinesToStr([ // $mod.$main
  24702. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24703. ' p: $mod,',
  24704. ' get: function () {',
  24705. ' return this.p.i;',
  24706. ' },',
  24707. ' set: function (v) {',
  24708. ' this.p.i = v;',
  24709. ' }',
  24710. '}));',
  24711. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24712. ' p: $mod,',
  24713. ' get: function () {',
  24714. ' return this.p.i;',
  24715. ' },',
  24716. ' set: function (v) {',
  24717. ' this.p.i = v;',
  24718. ' }',
  24719. '}));',
  24720. '$mod.THelperI.ToStr.call({',
  24721. ' a: $mod.i * $mod.i,',
  24722. ' get: function () {',
  24723. ' return this.a;',
  24724. ' },',
  24725. ' set: function (v) {',
  24726. ' rtl.raiseE("EPropReadOnly");',
  24727. ' }',
  24728. '});',
  24729. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24730. ' a: $mod.i * $mod.i,',
  24731. ' get: function () {',
  24732. ' return this.a;',
  24733. ' },',
  24734. ' set: function (v) {',
  24735. ' rtl.raiseE("EPropReadOnly");',
  24736. ' }',
  24737. '}));',
  24738. '']));
  24739. end;
  24740. procedure TTestModule.TestTypeHelper_StringChar;
  24741. begin
  24742. StartProgram(false);
  24743. Add([
  24744. '{$modeswitch typehelpers}',
  24745. 'type',
  24746. ' TStringHelper = type helper for string',
  24747. ' procedure DoIt(e: byte = 123);',
  24748. ' end;',
  24749. ' TCharHelper = type helper for char',
  24750. ' procedure Fly;',
  24751. ' end;',
  24752. 'procedure TStringHelper.DoIt(e: byte);',
  24753. 'begin',
  24754. ' Self[1]:=''c'';',
  24755. ' Self[2]:=Self[3];',
  24756. 'end;',
  24757. 'procedure TCharHelper.Fly;',
  24758. 'begin',
  24759. ' Self:=''c'';',
  24760. 'end;',
  24761. 'begin',
  24762. ' ''abc''.DoIt;',
  24763. ' ''xyz''.DoIt();',
  24764. ' ''c''.Fly();',
  24765. '']);
  24766. ConvertProgram;
  24767. CheckSource('TestTypeHelper_StringChar',
  24768. LinesToStr([ // statements
  24769. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24770. ' this.DoIt = function (e) {',
  24771. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24772. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24773. ' };',
  24774. '});',
  24775. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24776. ' this.Fly = function () {',
  24777. ' this.set("c");',
  24778. ' };',
  24779. '});',
  24780. '']),
  24781. LinesToStr([ // $mod.$main
  24782. '$mod.TStringHelper.DoIt.call({',
  24783. ' get: function () {',
  24784. ' return "abc";',
  24785. ' },',
  24786. ' set: function (v) {',
  24787. ' rtl.raiseE("EPropReadOnly");',
  24788. ' }',
  24789. '}, 123);',
  24790. '$mod.TStringHelper.DoIt.call({',
  24791. ' get: function () {',
  24792. ' return "xyz";',
  24793. ' },',
  24794. ' set: function (v) {',
  24795. ' rtl.raiseE("EPropReadOnly");',
  24796. ' }',
  24797. '}, 123);',
  24798. '$mod.TCharHelper.Fly.call({',
  24799. ' get: function () {',
  24800. ' return "c";',
  24801. ' },',
  24802. ' set: function (v) {',
  24803. ' rtl.raiseE("EPropReadOnly");',
  24804. ' }',
  24805. '});',
  24806. '']));
  24807. end;
  24808. procedure TTestModule.TestTypeHelper_JSValue;
  24809. begin
  24810. StartProgram(false);
  24811. Add([
  24812. '{$modeswitch typehelpers}',
  24813. 'type',
  24814. ' TExtValue = type jsvalue;',
  24815. ' THelper = type helper for TExtValue',
  24816. ' function ToStr: String;',
  24817. ' end;',
  24818. 'function THelper.ToStr: String;',
  24819. 'begin',
  24820. 'end;',
  24821. 'var',
  24822. ' s: string;',
  24823. ' v: TExtValue;',
  24824. 'begin',
  24825. ' s:=v.toStr;',
  24826. ' s:=v.toStr();',
  24827. ' TExtValue(s).toStr;',
  24828. '']);
  24829. ConvertProgram;
  24830. CheckSource('TestTypeHelper_JSValue',
  24831. LinesToStr([ // statements
  24832. 'rtl.createHelper(this, "THelper", null, function () {',
  24833. ' this.ToStr = function () {',
  24834. ' var Result = "";',
  24835. ' return Result;',
  24836. ' };',
  24837. '});',
  24838. 'this.s = "";',
  24839. 'this.v = undefined;',
  24840. '']),
  24841. LinesToStr([ // $mod.$main
  24842. '$mod.s = $mod.THelper.ToStr.call({',
  24843. ' p: $mod,',
  24844. ' get: function () {',
  24845. ' return this.p.v;',
  24846. ' },',
  24847. ' set: function (v) {',
  24848. ' this.p.v = v;',
  24849. ' }',
  24850. '});',
  24851. '$mod.s = $mod.THelper.ToStr.call({',
  24852. ' p: $mod,',
  24853. ' get: function () {',
  24854. ' return this.p.v;',
  24855. ' },',
  24856. ' set: function (v) {',
  24857. ' this.p.v = v;',
  24858. ' }',
  24859. '});',
  24860. '$mod.THelper.ToStr.call({',
  24861. ' p: $mod,',
  24862. ' get: function () {',
  24863. ' return this.p.s;',
  24864. ' },',
  24865. ' set: function (v) {',
  24866. ' rtl.raiseE("EPropReadOnly");',
  24867. ' }',
  24868. '});',
  24869. '']));
  24870. end;
  24871. procedure TTestModule.TestTypeHelper_Array;
  24872. begin
  24873. StartProgram(false);
  24874. Add([
  24875. '{$modeswitch typehelpers}',
  24876. 'type',
  24877. ' TArrOfBool = array of boolean;',
  24878. ' TArrOfJS = array of jsvalue;',
  24879. ' THelper = type helper for TArrOfBool',
  24880. ' procedure DoIt(e: byte = 123);',
  24881. ' end;',
  24882. 'procedure THelper.DoIt(e: byte);',
  24883. 'begin',
  24884. ' Self[1]:=true;',
  24885. ' Self[2]:=not Self[3];',
  24886. ' SetLength(Self,4);',
  24887. 'end;',
  24888. 'var',
  24889. ' b: TArrOfBool;',
  24890. ' j: TArrOfJS;',
  24891. 'begin',
  24892. ' b.DoIt;',
  24893. ' TArrOfBool(j).DoIt();',
  24894. '']);
  24895. ConvertProgram;
  24896. CheckSource('TestTypeHelper_Array',
  24897. LinesToStr([ // statements
  24898. 'rtl.createHelper(this, "THelper", null, function () {',
  24899. ' this.DoIt = function (e) {',
  24900. ' this.get()[1] = true;',
  24901. ' this.get()[2] = !this.get()[3];',
  24902. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24903. ' };',
  24904. '});',
  24905. 'this.b = [];',
  24906. 'this.j = [];',
  24907. '']),
  24908. LinesToStr([ // $mod.$main
  24909. '$mod.THelper.DoIt.call({',
  24910. ' p: $mod,',
  24911. ' get: function () {',
  24912. ' return this.p.b;',
  24913. ' },',
  24914. ' set: function (v) {',
  24915. ' this.p.b = v;',
  24916. ' }',
  24917. '}, 123);',
  24918. '$mod.THelper.DoIt.call({',
  24919. ' p: $mod,',
  24920. ' get: function () {',
  24921. ' return this.p.j;',
  24922. ' },',
  24923. ' set: function (v) {',
  24924. ' this.p.j = v;',
  24925. ' }',
  24926. '}, 123);',
  24927. '']));
  24928. end;
  24929. procedure TTestModule.TestTypeHelper_EnumType;
  24930. begin
  24931. StartProgram(false);
  24932. Add([
  24933. '{$modeswitch typehelpers}',
  24934. 'type',
  24935. ' TEnum = (red,blue);',
  24936. ' THelper = type helper for TEnum',
  24937. ' procedure DoIt(e: byte = 123);',
  24938. ' class procedure Swing(w: word); static;',
  24939. ' end;',
  24940. 'procedure THelper.DoIt(e: byte);',
  24941. 'begin',
  24942. ' Self:=red;',
  24943. ' Self:=succ(Self);',
  24944. ' with Self do Doit;',
  24945. 'end;',
  24946. 'class procedure THelper.Swing(w: word);',
  24947. 'begin',
  24948. 'end;',
  24949. 'var e: TEnum;',
  24950. 'begin',
  24951. ' e.DoIt;',
  24952. ' red.DoIt;',
  24953. ' TEnum.blue.DoIt;',
  24954. ' TEnum(1).DoIt;',
  24955. ' TEnum.Swing(3);',
  24956. '']);
  24957. ConvertProgram;
  24958. CheckSource('TestTypeHelper_EnumType',
  24959. LinesToStr([ // statements
  24960. 'this.TEnum = {',
  24961. ' "0": "red",',
  24962. ' red: 0,',
  24963. ' "1": "blue",',
  24964. ' blue: 1',
  24965. '};',
  24966. 'rtl.createHelper(this, "THelper", null, function () {',
  24967. ' this.DoIt = function (e) {',
  24968. ' this.set($mod.TEnum.red);',
  24969. ' this.set(this.get() + 1);',
  24970. ' var $with = this.get();',
  24971. ' $mod.THelper.DoIt.call(this, 123);',
  24972. ' };',
  24973. ' this.Swing = function (w) {',
  24974. ' };',
  24975. '});',
  24976. 'this.e = 0;',
  24977. '']),
  24978. LinesToStr([ // $mod.$main
  24979. '$mod.THelper.DoIt.call({',
  24980. ' p: $mod,',
  24981. ' get: function () {',
  24982. ' return this.p.e;',
  24983. ' },',
  24984. ' set: function (v) {',
  24985. ' this.p.e = v;',
  24986. ' }',
  24987. '}, 123);',
  24988. '$mod.THelper.DoIt.call({',
  24989. ' p: $mod.TEnum,',
  24990. ' get: function () {',
  24991. ' return this.p.red;',
  24992. ' },',
  24993. ' set: function (v) {',
  24994. ' rtl.raiseE("EPropReadOnly");',
  24995. ' }',
  24996. '}, 123);',
  24997. '$mod.THelper.DoIt.call({',
  24998. ' p: $mod.TEnum,',
  24999. ' get: function () {',
  25000. ' return this.p.blue;',
  25001. ' },',
  25002. ' set: function (v) {',
  25003. ' rtl.raiseE("EPropReadOnly");',
  25004. ' }',
  25005. '}, 123);',
  25006. '$mod.THelper.DoIt.call({',
  25007. ' get: function () {',
  25008. ' return 1;',
  25009. ' },',
  25010. ' set: function (v) {',
  25011. ' rtl.raiseE("EPropReadOnly");',
  25012. ' }',
  25013. '}, 123);',
  25014. '$mod.THelper.Swing(3);',
  25015. '']));
  25016. end;
  25017. procedure TTestModule.TestTypeHelper_SetType;
  25018. begin
  25019. StartProgram(false);
  25020. Add([
  25021. '{$modeswitch typehelpers}',
  25022. 'type',
  25023. ' TEnum = (red,blue);',
  25024. ' TSetOfEnum = set of TEnum;',
  25025. ' THelper = type helper for TSetOfEnum',
  25026. ' procedure DoIt(e: byte = 123);',
  25027. ' constructor Init(e: TEnum);',
  25028. ' constructor InitEmpty;',
  25029. ' end;',
  25030. 'procedure THelper.DoIt(e: byte);',
  25031. 'begin',
  25032. ' Self:=[];',
  25033. ' Self:=[red];',
  25034. ' Include(Self,blue);',
  25035. 'end;',
  25036. 'constructor THelper.Init(e: TEnum);',
  25037. 'begin',
  25038. ' Self:=[];',
  25039. ' Self:=[e];',
  25040. ' Include(Self,blue);',
  25041. 'end;',
  25042. 'constructor THelper.InitEmpty;',
  25043. 'begin',
  25044. 'end;',
  25045. 'var s: TSetOfEnum;',
  25046. 'begin',
  25047. ' s.DoIt;',
  25048. //' [red].DoIt;',
  25049. //' with s do DoIt;',
  25050. //' with [red,blue] do DoIt;',
  25051. ' s:=TSetOfEnum.Init(blue);',
  25052. ' s:=s.Init(blue);',
  25053. '']);
  25054. ConvertProgram;
  25055. CheckSource('TestTypeHelper_SetType',
  25056. LinesToStr([ // statements
  25057. 'this.TEnum = {',
  25058. ' "0": "red",',
  25059. ' red: 0,',
  25060. ' "1": "blue",',
  25061. ' blue: 1',
  25062. '};',
  25063. 'rtl.createHelper(this, "THelper", null, function () {',
  25064. ' this.DoIt = function (e) {',
  25065. ' this.set({});',
  25066. ' this.set(rtl.createSet($mod.TEnum.red));',
  25067. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25068. ' };',
  25069. ' this.Init = function (e) {',
  25070. ' this.set({});',
  25071. ' this.set(rtl.createSet(e));',
  25072. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25073. ' return this.get();',
  25074. ' };',
  25075. ' this.InitEmpty = function () {',
  25076. ' return this.get();',
  25077. ' };',
  25078. ' this.$new = function (fn, args) {',
  25079. ' return this[fn].apply({',
  25080. ' p: {},',
  25081. ' get: function () {',
  25082. ' return this.p;',
  25083. ' },',
  25084. ' set: function (v) {',
  25085. ' this.p = v;',
  25086. ' }',
  25087. ' }, args);',
  25088. ' };',
  25089. '});',
  25090. 'this.s = {};',
  25091. '']),
  25092. LinesToStr([ // $mod.$main
  25093. '$mod.THelper.DoIt.call({',
  25094. ' p: $mod,',
  25095. ' get: function () {',
  25096. ' return this.p.s;',
  25097. ' },',
  25098. ' set: function (v) {',
  25099. ' this.p.s = v;',
  25100. ' }',
  25101. '}, 123);',
  25102. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25103. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25104. ' p: $mod,',
  25105. ' get: function () {',
  25106. ' return this.p.s;',
  25107. ' },',
  25108. ' set: function (v) {',
  25109. ' this.p.s = v;',
  25110. ' }',
  25111. '}, $mod.TEnum.blue));',
  25112. '']));
  25113. end;
  25114. procedure TTestModule.TestTypeHelper_InterfaceType;
  25115. begin
  25116. StartProgram(false);
  25117. Add([
  25118. '{$interfaces com}',
  25119. '{$modeswitch typehelpers}',
  25120. 'type',
  25121. ' IUnknown = interface',
  25122. ' function _AddRef: longint;',
  25123. ' function _Release: longint;',
  25124. ' end;',
  25125. ' TObject = class(IUnknown)',
  25126. ' function _AddRef: longint; virtual; abstract;',
  25127. ' function _Release: longint; virtual; abstract;',
  25128. ' end;',
  25129. ' THelper = type helper for IUnknown',
  25130. ' procedure Fly(e: byte = 123);',
  25131. ' class procedure Run; static;',
  25132. ' end;',
  25133. 'var',
  25134. ' i: IUnknown;',
  25135. ' o: TObject;',
  25136. 'procedure THelper.Fly(e: byte);',
  25137. 'begin',
  25138. ' i:=Self;',
  25139. ' o:=Self as TObject;',
  25140. ' Self:=nil;',
  25141. ' Self:=i;',
  25142. ' Self:=o;',
  25143. ' with Self do begin',
  25144. ' Fly;',
  25145. ' Fly();',
  25146. ' end;',
  25147. 'end;',
  25148. 'class procedure THelper.Run;',
  25149. 'var l: IUnknown;',
  25150. 'begin',
  25151. ' l.Fly;',
  25152. ' l.Fly();',
  25153. 'end;',
  25154. 'begin',
  25155. ' i.Fly;',
  25156. ' i.Fly();',
  25157. ' i.Run;',
  25158. ' i.Run();',
  25159. ' IUnknown.Run;',
  25160. ' IUnknown.Run();',
  25161. '']);
  25162. ConvertProgram;
  25163. CheckSource('TestTypeHelper_InterfaceType',
  25164. LinesToStr([ // statements
  25165. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25166. 'rtl.createClass(this, "TObject", null, function () {',
  25167. ' this.$init = function () {',
  25168. ' };',
  25169. ' this.$final = function () {',
  25170. ' };',
  25171. ' rtl.addIntf(this, $mod.IUnknown);',
  25172. '});',
  25173. 'rtl.createHelper(this, "THelper", null, function () {',
  25174. ' this.Fly = function (e) {',
  25175. ' var $ir = rtl.createIntfRefs();',
  25176. ' try {',
  25177. ' rtl.setIntfP($mod, "i", this.get());',
  25178. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25179. ' this.set(null);',
  25180. ' this.set($mod.i);',
  25181. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25182. ' var $with = this.get();',
  25183. ' $mod.THelper.Fly.call(this, 123);',
  25184. ' $mod.THelper.Fly.call(this, 123);',
  25185. ' } finally {',
  25186. ' $ir.free();',
  25187. ' };',
  25188. ' };',
  25189. ' this.Run = function () {',
  25190. ' var l = null;',
  25191. ' try {',
  25192. ' $mod.THelper.Fly.call({',
  25193. ' get: function () {',
  25194. ' return l;',
  25195. ' },',
  25196. ' set: function (v) {',
  25197. ' l = rtl.setIntfL(l, v);',
  25198. ' }',
  25199. ' }, 123);',
  25200. ' $mod.THelper.Fly.call({',
  25201. ' get: function () {',
  25202. ' return l;',
  25203. ' },',
  25204. ' set: function (v) {',
  25205. ' l = rtl.setIntfL(l, v);',
  25206. ' }',
  25207. ' }, 123);',
  25208. ' } finally {',
  25209. ' rtl._Release(l);',
  25210. ' };',
  25211. ' };',
  25212. '});',
  25213. 'this.i = null;',
  25214. 'this.o = null;',
  25215. '']),
  25216. LinesToStr([ // $mod.$main
  25217. '$mod.THelper.Fly.call({',
  25218. ' p: $mod,',
  25219. ' get: function () {',
  25220. ' return this.p.i;',
  25221. ' },',
  25222. ' set: function (v) {',
  25223. ' rtl.setIntfP(this.p, "i", v);',
  25224. ' }',
  25225. '}, 123);',
  25226. '$mod.THelper.Fly.call({',
  25227. ' p: $mod,',
  25228. ' get: function () {',
  25229. ' return this.p.i;',
  25230. ' },',
  25231. ' set: function (v) {',
  25232. ' rtl.setIntfP(this.p, "i", v);',
  25233. ' }',
  25234. '}, 123);',
  25235. '$mod.THelper.Run();',
  25236. '$mod.THelper.Run();',
  25237. '$mod.THelper.Run();',
  25238. '$mod.THelper.Run();',
  25239. '']));
  25240. end;
  25241. procedure TTestModule.TestTypeHelper_NestedSelf;
  25242. begin
  25243. StartProgram(false);
  25244. Add([
  25245. '{$modeswitch typehelpers}',
  25246. 'type',
  25247. ' THelper = type helper for string',
  25248. ' procedure Run(Value: string);',
  25249. ' end;',
  25250. 'procedure THelper.Run(Value: string);',
  25251. ' function Sub(i: nativeint): boolean;',
  25252. ' begin',
  25253. ' Result:=Self[i+1]=Value[i];',
  25254. ' end;',
  25255. 'begin',
  25256. ' if Self[3]=Value[4] then ;',
  25257. 'end;',
  25258. 'begin',
  25259. '']);
  25260. ConvertProgram;
  25261. CheckSource('TestTypeHelper_NestedSelf',
  25262. LinesToStr([ // statements
  25263. 'rtl.createHelper(this, "THelper", null, function () {',
  25264. ' this.Run = function (Value) {',
  25265. ' var $Self = this;',
  25266. ' function Sub(i) {',
  25267. ' var Result = false;',
  25268. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25269. ' return Result;',
  25270. ' };',
  25271. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25272. ' };',
  25273. '});',
  25274. '']),
  25275. LinesToStr([ // $mod.$main
  25276. '']));
  25277. end;
  25278. procedure TTestModule.TestProcType;
  25279. begin
  25280. StartProgram(false);
  25281. Add([
  25282. 'type',
  25283. ' TProcInt = procedure(vI: longint = 1);',
  25284. 'procedure DoIt(vJ: longint);',
  25285. 'begin end;',
  25286. 'var',
  25287. ' b: boolean;',
  25288. ' vP, vQ: tprocint;',
  25289. 'begin',
  25290. ' vp:=nil;',
  25291. ' vp:=vp;',
  25292. ' vp:=@doit;',
  25293. ' vp;',
  25294. ' vp();',
  25295. ' vp(2);',
  25296. ' b:=vp=nil;',
  25297. ' b:=nil=vp;',
  25298. ' b:=vp=vq;',
  25299. ' b:=vp=@doit;',
  25300. ' b:=@doit=vp;',
  25301. ' b:=vp<>nil;',
  25302. ' b:=nil<>vp;',
  25303. ' b:=vp<>vq;',
  25304. ' b:=vp<>@doit;',
  25305. ' b:=@doit<>vp;',
  25306. ' b:=Assigned(vp);',
  25307. ' if Assigned(vp) then ;']);
  25308. ConvertProgram;
  25309. CheckSource('TestProcType',
  25310. LinesToStr([ // statements
  25311. 'this.DoIt = function(vJ) {',
  25312. '};',
  25313. 'this.b = false;',
  25314. 'this.vP = null;',
  25315. 'this.vQ = null;'
  25316. ]),
  25317. LinesToStr([ // $mod.$main
  25318. '$mod.vP = null;',
  25319. '$mod.vP = $mod.vP;',
  25320. '$mod.vP = $mod.DoIt;',
  25321. '$mod.vP(1);',
  25322. '$mod.vP(1);',
  25323. '$mod.vP(2);',
  25324. '$mod.b = $mod.vP === null;',
  25325. '$mod.b = null === $mod.vP;',
  25326. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25327. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25328. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25329. '$mod.b = $mod.vP !== null;',
  25330. '$mod.b = null !== $mod.vP;',
  25331. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25332. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25333. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25334. '$mod.b = $mod.vP != null;',
  25335. 'if ($mod.vP != null) ;',
  25336. '']));
  25337. end;
  25338. procedure TTestModule.TestProcType_Arg;
  25339. begin
  25340. StartProgram(false);
  25341. Add([
  25342. 'type',
  25343. ' TProcInt = procedure(vI: longint = 1);',
  25344. 'procedure DoIt(vJ: longint); begin end;',
  25345. 'procedure DoSome(vP, vQ: TProcInt);',
  25346. 'var',
  25347. ' b: boolean;',
  25348. 'begin',
  25349. ' vp:=nil;',
  25350. ' vp:=vp;',
  25351. ' vp:=@doit;',
  25352. ' vp;',
  25353. ' vp();',
  25354. ' vp(2);',
  25355. ' b:=vp=nil;',
  25356. ' b:=nil=vp;',
  25357. ' b:=vp=vq;',
  25358. ' b:=vp=@doit;',
  25359. ' b:=@doit=vp;',
  25360. ' b:=vp<>nil;',
  25361. ' b:=nil<>vp;',
  25362. ' b:=vp<>vq;',
  25363. ' b:=vp<>@doit;',
  25364. ' b:=@doit<>vp;',
  25365. ' b:=Assigned(vp);',
  25366. ' if Assigned(vp) then ;',
  25367. 'end;',
  25368. 'begin',
  25369. ' DoSome(@DoIt,nil);']);
  25370. ConvertProgram;
  25371. CheckSource('TestProcType_Arg',
  25372. LinesToStr([ // statements
  25373. 'this.DoIt = function(vJ) {',
  25374. '};',
  25375. 'this.DoSome = function(vP, vQ) {',
  25376. ' var b = false;',
  25377. ' vP = null;',
  25378. ' vP = vP;',
  25379. ' vP = $mod.DoIt;',
  25380. ' vP(1);',
  25381. ' vP(1);',
  25382. ' vP(2);',
  25383. ' b = vP === null;',
  25384. ' b = null === vP;',
  25385. ' b = rtl.eqCallback(vP,vQ);',
  25386. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25387. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25388. ' b = vP !== null;',
  25389. ' b = null !== vP;',
  25390. ' b = !rtl.eqCallback(vP, vQ);',
  25391. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25392. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25393. ' b = vP != null;',
  25394. ' if (vP != null) ;',
  25395. '};',
  25396. '']),
  25397. LinesToStr([ // $mod.$main
  25398. '$mod.DoSome($mod.DoIt,null);',
  25399. '']));
  25400. end;
  25401. procedure TTestModule.TestProcType_FunctionFPC;
  25402. begin
  25403. StartProgram(false);
  25404. Add('type');
  25405. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25406. Add('function DoIt(vI: longint): longint;');
  25407. Add('begin end;');
  25408. Add('var');
  25409. Add(' b: boolean;');
  25410. Add(' vP, vQ: tfuncint;');
  25411. Add('begin');
  25412. Add(' vp:=nil;');
  25413. Add(' vp:=vp;');
  25414. Add(' vp:=@doit;'); // ok in fpc and delphi
  25415. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25416. Add(' vp;'); // ok in fpc and delphi
  25417. Add(' vp();');
  25418. Add(' vp(2);');
  25419. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25420. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25421. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25422. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25423. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25424. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25425. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25426. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25427. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25428. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25429. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25430. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25431. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25432. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25433. Add(' b:=Assigned(vp);');
  25434. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25435. Add(' doit(vp());'); // ok in fpc and delphi
  25436. Add(' doit(vp(2));'); // ok in fpc and delphi
  25437. ConvertProgram;
  25438. CheckSource('TestProcType_FunctionFPC',
  25439. LinesToStr([ // statements
  25440. 'this.DoIt = function(vI) {',
  25441. ' var Result = 0;',
  25442. ' return Result;',
  25443. '};',
  25444. 'this.b = false;',
  25445. 'this.vP = null;',
  25446. 'this.vQ = null;'
  25447. ]),
  25448. LinesToStr([ // $mod.$main
  25449. '$mod.vP = null;',
  25450. '$mod.vP = $mod.vP;',
  25451. '$mod.vP = $mod.DoIt;',
  25452. '$mod.vP(1);',
  25453. '$mod.vP(1);',
  25454. '$mod.vP(2);',
  25455. '$mod.b = $mod.vP === null;',
  25456. '$mod.b = null === $mod.vP;',
  25457. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25458. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25459. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25460. '$mod.b = 4 === $mod.vP(1);',
  25461. '$mod.b = $mod.vP !== null;',
  25462. '$mod.b = null !== $mod.vP;',
  25463. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25464. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25465. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25466. '$mod.b = 6 !== $mod.vP(1);',
  25467. '$mod.b = $mod.vP != null;',
  25468. '$mod.DoIt($mod.vP(1));',
  25469. '$mod.DoIt($mod.vP(2));',
  25470. '']));
  25471. end;
  25472. procedure TTestModule.TestProcType_FunctionDelphi;
  25473. begin
  25474. StartProgram(false);
  25475. Add('{$mode Delphi}');
  25476. Add('type');
  25477. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25478. Add('function DoIt(vI: longint): longint;');
  25479. Add('begin end;');
  25480. Add('var');
  25481. Add(' b: boolean;');
  25482. Add(' vP, vQ: tfuncint;');
  25483. Add('begin');
  25484. Add(' vp:=nil;');
  25485. Add(' vp:=vp;');
  25486. Add(' vp:=@doit;'); // ok in fpc and delphi
  25487. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25488. Add(' vp;'); // ok in fpc and delphi
  25489. Add(' vp();');
  25490. Add(' vp(2);');
  25491. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25492. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25493. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25494. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25495. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25496. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25497. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25498. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25499. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25500. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25501. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25502. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25503. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25504. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25505. Add(' b:=Assigned(vp);');
  25506. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25507. Add(' doit(vp());'); // ok in fpc and delphi
  25508. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25509. ConvertProgram;
  25510. CheckSource('TestProcType_FunctionDelphi',
  25511. LinesToStr([ // statements
  25512. 'this.DoIt = function(vI) {',
  25513. ' var Result = 0;',
  25514. ' return Result;',
  25515. '};',
  25516. 'this.b = false;',
  25517. 'this.vP = null;',
  25518. 'this.vQ = null;'
  25519. ]),
  25520. LinesToStr([ // $mod.$main
  25521. '$mod.vP = null;',
  25522. '$mod.vP = $mod.vP;',
  25523. '$mod.vP = $mod.DoIt;',
  25524. '$mod.vP = $mod.DoIt;',
  25525. '$mod.vP(1);',
  25526. '$mod.vP(1);',
  25527. '$mod.vP(2);',
  25528. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25529. '$mod.b = $mod.vP(1) === 3;',
  25530. '$mod.b = 4 === $mod.vP(1);',
  25531. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25532. '$mod.b = $mod.vP(1) !== 5;',
  25533. '$mod.b = 6 !== $mod.vP(1);',
  25534. '$mod.b = $mod.vP != null;',
  25535. '$mod.DoIt($mod.vP(1));',
  25536. '$mod.DoIt($mod.vP(1));',
  25537. '$mod.DoIt($mod.vP(2));',
  25538. '']));
  25539. end;
  25540. procedure TTestModule.TestProcType_ProcedureDelphi;
  25541. begin
  25542. StartProgram(false);
  25543. Add('{$mode Delphi}');
  25544. Add('type');
  25545. Add(' TProc = procedure;');
  25546. Add('procedure DoIt;');
  25547. Add('begin end;');
  25548. Add('var');
  25549. Add(' b: boolean;');
  25550. Add(' vP, vQ: tproc;');
  25551. Add('begin');
  25552. Add(' vp:=nil;');
  25553. Add(' vp:=vp;');
  25554. Add(' vp:=vq;');
  25555. 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
  25556. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25557. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25558. Add(' vp;'); // ok in fpc and delphi
  25559. Add(' vp();');
  25560. // equal
  25561. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25562. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25563. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25564. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25565. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25566. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25567. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25568. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25569. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25570. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25571. // unequal
  25572. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25573. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25574. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25575. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25576. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25577. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25578. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25579. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25580. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25581. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25582. Add(' b:=Assigned(vp);');
  25583. ConvertProgram;
  25584. CheckSource('TestProcType_ProcedureDelphi',
  25585. LinesToStr([ // statements
  25586. 'this.DoIt = function() {',
  25587. '};',
  25588. 'this.b = false;',
  25589. 'this.vP = null;',
  25590. 'this.vQ = null;'
  25591. ]),
  25592. LinesToStr([ // $mod.$main
  25593. '$mod.vP = null;',
  25594. '$mod.vP = $mod.vP;',
  25595. '$mod.vP = $mod.vQ;',
  25596. '$mod.vP = $mod.DoIt;',
  25597. '$mod.vP = $mod.DoIt;',
  25598. '$mod.vP();',
  25599. '$mod.vP();',
  25600. '$mod.b = $mod.vP === null;',
  25601. '$mod.b = null === $mod.vP;',
  25602. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25603. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25604. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25605. '$mod.b = $mod.vP !== null;',
  25606. '$mod.b = null !== $mod.vP;',
  25607. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25608. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25609. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25610. '$mod.b = $mod.vP != null;',
  25611. '']));
  25612. end;
  25613. procedure TTestModule.TestProcType_AsParam;
  25614. begin
  25615. StartProgram(false);
  25616. Add('type');
  25617. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25618. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25619. Add('var vJ: tfuncint;');
  25620. Add('begin');
  25621. Add(' vg:=vg;');
  25622. Add(' vj:=vh;');
  25623. Add(' vi:=vi;');
  25624. Add(' doit(vg,vg,vg);');
  25625. Add(' doit(vh,vh,vj);');
  25626. Add(' doit(vi,vi,vi);');
  25627. Add(' doit(vj,vj,vj);');
  25628. Add('end;');
  25629. Add('var i: tfuncint;');
  25630. Add('begin');
  25631. Add(' doit(i,i,i);');
  25632. ConvertProgram;
  25633. CheckSource('TestProcType_AsParam',
  25634. LinesToStr([ // statements
  25635. 'this.DoIt = function (vG,vH,vI) {',
  25636. ' var vJ = null;',
  25637. ' vG = vG;',
  25638. ' vJ = vH;',
  25639. ' vI.set(vI.get());',
  25640. ' $mod.DoIt(vG, vG, {',
  25641. ' get: function () {',
  25642. ' return vG;',
  25643. ' },',
  25644. ' set: function (v) {',
  25645. ' vG = v;',
  25646. ' }',
  25647. ' });',
  25648. ' $mod.DoIt(vH, vH, {',
  25649. ' get: function () {',
  25650. ' return vJ;',
  25651. ' },',
  25652. ' set: function (v) {',
  25653. ' vJ = v;',
  25654. ' }',
  25655. ' });',
  25656. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25657. ' $mod.DoIt(vJ, vJ, {',
  25658. ' get: function () {',
  25659. ' return vJ;',
  25660. ' },',
  25661. ' set: function (v) {',
  25662. ' vJ = v;',
  25663. ' }',
  25664. ' });',
  25665. '};',
  25666. 'this.i = null;'
  25667. ]),
  25668. LinesToStr([
  25669. '$mod.DoIt($mod.i,$mod.i,{',
  25670. ' p: $mod,',
  25671. ' get: function () {',
  25672. ' return this.p.i;',
  25673. ' },',
  25674. ' set: function (v) {',
  25675. ' this.p.i = v;',
  25676. ' }',
  25677. '});'
  25678. ]));
  25679. end;
  25680. procedure TTestModule.TestProcType_MethodFPC;
  25681. begin
  25682. StartProgram(false);
  25683. Add('type');
  25684. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25685. Add(' TObject = class');
  25686. Add(' function DoIt(vA: longint = 1): longint;');
  25687. Add(' end;');
  25688. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25689. Add('begin');
  25690. Add('end;');
  25691. Add('var');
  25692. Add(' Obj: TObject;');
  25693. Add(' vP: tfuncint;');
  25694. Add(' b: boolean;');
  25695. Add('begin');
  25696. Add(' vp:[email protected];'); // ok in fpc and delphi
  25697. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25698. Add(' vp;'); // ok in fpc and delphi
  25699. Add(' vp();');
  25700. Add(' vp(2);');
  25701. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25702. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25703. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25704. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25705. ConvertProgram;
  25706. CheckSource('TestProcType_MethodFPC',
  25707. LinesToStr([ // statements
  25708. 'rtl.createClass(this, "TObject", null, function () {',
  25709. ' this.$init = function () {',
  25710. ' };',
  25711. ' this.$final = function () {',
  25712. ' };',
  25713. ' this.DoIt = function (vA) {',
  25714. ' var Result = 0;',
  25715. ' return Result;',
  25716. ' };',
  25717. '});',
  25718. 'this.Obj = null;',
  25719. 'this.vP = null;',
  25720. 'this.b = false;'
  25721. ]),
  25722. LinesToStr([
  25723. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25724. '$mod.vP(1);',
  25725. '$mod.vP(1);',
  25726. '$mod.vP(2);',
  25727. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25728. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25729. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25730. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25731. '']));
  25732. end;
  25733. procedure TTestModule.TestProcType_MethodDelphi;
  25734. begin
  25735. StartProgram(false);
  25736. Add([
  25737. '{$mode delphi}',
  25738. 'type',
  25739. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25740. ' TObject = class',
  25741. ' function DoIt(vA: longint = 1): longint;',
  25742. ' end;',
  25743. 'function TObject.DoIt(vA: longint = 1): longint;',
  25744. 'begin',
  25745. 'end;',
  25746. 'var',
  25747. ' Obj: TObject;',
  25748. ' vP: tfuncint;',
  25749. ' b: boolean;',
  25750. 'begin',
  25751. ' vp:[email protected];', // ok in fpc and delphi
  25752. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25753. ' vp;', // ok in fpc and delphi
  25754. ' vp();',
  25755. ' vp(2);',
  25756. //' b:[email protected];', // ok in fpc, illegal in delphi
  25757. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25758. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25759. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25760. '']);
  25761. ConvertProgram;
  25762. CheckSource('TestProcType_MethodDelphi',
  25763. LinesToStr([ // statements
  25764. 'rtl.createClass(this, "TObject", null, function () {',
  25765. ' this.$init = function () {',
  25766. ' };',
  25767. ' this.$final = function () {',
  25768. ' };',
  25769. ' this.DoIt = function (vA) {',
  25770. ' var Result = 0;',
  25771. ' return Result;',
  25772. ' };',
  25773. '});',
  25774. 'this.Obj = null;',
  25775. 'this.vP = null;',
  25776. 'this.b = false;'
  25777. ]),
  25778. LinesToStr([
  25779. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25780. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25781. '$mod.vP(1);',
  25782. '$mod.vP(1);',
  25783. '$mod.vP(2);',
  25784. '']));
  25785. end;
  25786. procedure TTestModule.TestProcType_PropertyFPC;
  25787. begin
  25788. StartProgram(false);
  25789. Add('type');
  25790. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25791. Add(' TObject = class');
  25792. Add(' FOnFoo: TFuncInt;');
  25793. Add(' function DoIt(vA: longint = 1): longint;');
  25794. Add(' function GetFoo: TFuncInt;');
  25795. Add(' procedure SetFoo(const Value: TFuncInt);');
  25796. Add(' function GetEvents(Index: longint): TFuncInt;');
  25797. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25798. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25799. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25800. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25801. Add(' end;');
  25802. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25803. Add('function tobject.getfoo: tfuncint; begin end;');
  25804. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25805. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25806. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25807. Add('var');
  25808. Add(' Obj: TObject;');
  25809. Add(' vP: tfuncint;');
  25810. Add(' b: boolean;');
  25811. Add('begin');
  25812. Add(' obj.onfoo:=nil;');
  25813. Add(' obj.onbar:=nil;');
  25814. Add(' obj.events[1]:=nil;');
  25815. Add(' obj.onfoo:=obj.onfoo;');
  25816. Add(' obj.onbar:=obj.onbar;');
  25817. Add(' obj.events[2]:=obj.events[3];');
  25818. Add(' obj.onfoo:[email protected];');
  25819. Add(' obj.onbar:[email protected];');
  25820. Add(' obj.events[4]:[email protected];');
  25821. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25822. //Add(' obj.onbar:=obj.doit;'); // delphi
  25823. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25824. Add(' obj.onfoo;');
  25825. Add(' obj.onbar;');
  25826. //Add(' obj.events[5];'); ToDo in pasresolver
  25827. Add(' obj.onfoo();');
  25828. Add(' obj.onbar();');
  25829. Add(' obj.events[6]();');
  25830. Add(' b:=obj.onfoo=nil;');
  25831. Add(' b:=obj.onbar=nil;');
  25832. Add(' b:=obj.events[7]=nil;');
  25833. Add(' b:=obj.onfoo<>nil;');
  25834. Add(' b:=obj.onbar<>nil;');
  25835. Add(' b:=obj.events[8]<>nil;');
  25836. Add(' b:=obj.onfoo=vp;');
  25837. Add(' b:=obj.onbar=vp;');
  25838. Add(' b:=obj.events[9]=vp;');
  25839. Add(' b:=obj.onfoo=obj.onfoo;');
  25840. Add(' b:=obj.onbar=obj.onfoo;');
  25841. Add(' b:=obj.events[10]=obj.onfoo;');
  25842. Add(' b:=obj.onfoo<>obj.onfoo;');
  25843. Add(' b:=obj.onbar<>obj.onfoo;');
  25844. Add(' b:=obj.events[11]<>obj.onfoo;');
  25845. Add(' b:[email protected];');
  25846. Add(' b:[email protected];');
  25847. Add(' b:=obj.events[12][email protected];');
  25848. Add(' b:=obj.onfoo<>@obj.doit;');
  25849. Add(' b:=obj.onbar<>@obj.doit;');
  25850. Add(' b:=obj.events[12]<>@obj.doit;');
  25851. Add(' b:=Assigned(obj.onfoo);');
  25852. Add(' b:=Assigned(obj.onbar);');
  25853. Add(' b:=Assigned(obj.events[13]);');
  25854. ConvertProgram;
  25855. CheckSource('TestProcType_PropertyFPC',
  25856. LinesToStr([ // statements
  25857. 'rtl.createClass(this, "TObject", null, function () {',
  25858. ' this.$init = function () {',
  25859. ' this.FOnFoo = null;',
  25860. ' };',
  25861. ' this.$final = function () {',
  25862. ' this.FOnFoo = undefined;',
  25863. ' };',
  25864. ' this.DoIt = function (vA) {',
  25865. ' var Result = 0;',
  25866. ' return Result;',
  25867. ' };',
  25868. 'this.GetFoo = function () {',
  25869. ' var Result = null;',
  25870. ' return Result;',
  25871. '};',
  25872. 'this.SetFoo = function (Value) {',
  25873. '};',
  25874. 'this.GetEvents = function (Index) {',
  25875. ' var Result = null;',
  25876. ' return Result;',
  25877. '};',
  25878. 'this.SetEvents = function (Index, Value) {',
  25879. '};',
  25880. '});',
  25881. 'this.Obj = null;',
  25882. 'this.vP = null;',
  25883. 'this.b = false;'
  25884. ]),
  25885. LinesToStr([
  25886. '$mod.Obj.FOnFoo = null;',
  25887. '$mod.Obj.SetFoo(null);',
  25888. '$mod.Obj.SetEvents(1, null);',
  25889. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25890. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25891. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25892. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25893. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25894. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25895. '$mod.Obj.FOnFoo(1);',
  25896. '$mod.Obj.GetFoo();',
  25897. '$mod.Obj.FOnFoo(1);',
  25898. '$mod.Obj.GetFoo()(1);',
  25899. '$mod.Obj.GetEvents(6)(1);',
  25900. '$mod.b = $mod.Obj.FOnFoo === null;',
  25901. '$mod.b = $mod.Obj.GetFoo() === null;',
  25902. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25903. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25904. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25905. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25906. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25907. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25908. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25909. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25910. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25911. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25912. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25913. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25914. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25915. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25916. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25917. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25918. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25919. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25920. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25921. '$mod.b = $mod.Obj.FOnFoo != null;',
  25922. '$mod.b = $mod.Obj.GetFoo() != null;',
  25923. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25924. '']));
  25925. end;
  25926. procedure TTestModule.TestProcType_PropertyDelphi;
  25927. begin
  25928. StartProgram(false);
  25929. Add('{$mode delphi}');
  25930. Add('type');
  25931. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25932. Add(' TObject = class');
  25933. Add(' FOnFoo: TFuncInt;');
  25934. Add(' function DoIt(vA: longint = 1): longint;');
  25935. Add(' function GetFoo: TFuncInt;');
  25936. Add(' procedure SetFoo(const Value: TFuncInt);');
  25937. Add(' function GetEvents(Index: longint): TFuncInt;');
  25938. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25939. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25940. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25941. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25942. Add(' end;');
  25943. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25944. Add('function tobject.getfoo: tfuncint; begin end;');
  25945. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25946. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25947. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25948. Add('var');
  25949. Add(' Obj: TObject;');
  25950. Add(' vP: tfuncint;');
  25951. Add(' b: boolean;');
  25952. Add('begin');
  25953. Add(' obj.onfoo:=nil;');
  25954. Add(' obj.onbar:=nil;');
  25955. Add(' obj.events[1]:=nil;');
  25956. Add(' obj.onfoo:=obj.onfoo;');
  25957. Add(' obj.onbar:=obj.onbar;');
  25958. Add(' obj.events[2]:=obj.events[3];');
  25959. Add(' obj.onfoo:[email protected];');
  25960. Add(' obj.onbar:[email protected];');
  25961. Add(' obj.events[4]:[email protected];');
  25962. Add(' obj.onfoo:=obj.doit;'); // delphi
  25963. Add(' obj.onbar:=obj.doit;'); // delphi
  25964. Add(' obj.events[4]:=obj.doit;'); // delphi
  25965. Add(' obj.onfoo;');
  25966. Add(' obj.onbar;');
  25967. //Add(' obj.events[5];'); ToDo in pasresolver
  25968. Add(' obj.onfoo();');
  25969. Add(' obj.onbar();');
  25970. Add(' obj.events[6]();');
  25971. //Add(' b:=obj.onfoo=nil;'); // fpc
  25972. //Add(' b:=obj.onbar=nil;'); // fpc
  25973. //Add(' b:=obj.events[7]=nil;'); // fpc
  25974. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25975. //Add(' b:=obj.onbar<>nil;'); // fpc
  25976. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25977. Add(' b:=obj.onfoo=vp;');
  25978. Add(' b:=obj.onbar=vp;');
  25979. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25980. Add(' b:=obj.onfoo=obj.onfoo;');
  25981. Add(' b:=obj.onbar=obj.onfoo;');
  25982. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25983. Add(' b:=obj.onfoo<>obj.onfoo;');
  25984. Add(' b:=obj.onbar<>obj.onfoo;');
  25985. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25986. //Add(' b:[email protected];'); // fpc
  25987. //Add(' b:[email protected];'); // fpc
  25988. //Add(' b:=obj.events[12][email protected];'); // fpc
  25989. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25990. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25991. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25992. Add(' b:=Assigned(obj.onfoo);');
  25993. Add(' b:=Assigned(obj.onbar);');
  25994. Add(' b:=Assigned(obj.events[13]);');
  25995. ConvertProgram;
  25996. CheckSource('TestProcType_PropertyDelphi',
  25997. LinesToStr([ // statements
  25998. 'rtl.createClass(this, "TObject", null, function () {',
  25999. ' this.$init = function () {',
  26000. ' this.FOnFoo = null;',
  26001. ' };',
  26002. ' this.$final = function () {',
  26003. ' this.FOnFoo = undefined;',
  26004. ' };',
  26005. ' this.DoIt = function (vA) {',
  26006. ' var Result = 0;',
  26007. ' return Result;',
  26008. ' };',
  26009. 'this.GetFoo = function () {',
  26010. ' var Result = null;',
  26011. ' return Result;',
  26012. '};',
  26013. 'this.SetFoo = function (Value) {',
  26014. '};',
  26015. 'this.GetEvents = function (Index) {',
  26016. ' var Result = null;',
  26017. ' return Result;',
  26018. '};',
  26019. 'this.SetEvents = function (Index, Value) {',
  26020. '};',
  26021. '});',
  26022. 'this.Obj = null;',
  26023. 'this.vP = null;',
  26024. 'this.b = false;'
  26025. ]),
  26026. LinesToStr([
  26027. '$mod.Obj.FOnFoo = null;',
  26028. '$mod.Obj.SetFoo(null);',
  26029. '$mod.Obj.SetEvents(1, null);',
  26030. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26031. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26032. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26033. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26034. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26035. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26036. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26037. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26038. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26039. '$mod.Obj.FOnFoo(1);',
  26040. '$mod.Obj.GetFoo();',
  26041. '$mod.Obj.FOnFoo(1);',
  26042. '$mod.Obj.GetFoo()(1);',
  26043. '$mod.Obj.GetEvents(6)(1);',
  26044. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26045. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26046. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26047. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26048. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26049. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26050. '$mod.b = $mod.Obj.FOnFoo != null;',
  26051. '$mod.b = $mod.Obj.GetFoo() != null;',
  26052. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26053. '']));
  26054. end;
  26055. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26056. begin
  26057. StartProgram(false);
  26058. Add('type');
  26059. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26060. Add(' TObject = class');
  26061. Add(' FOnFoo: TFuncInt;');
  26062. Add(' function DoIt(vA: longint = 1): longint;');
  26063. Add(' function GetFoo: TFuncInt;');
  26064. Add(' procedure SetFoo(const Value: TFuncInt);');
  26065. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26066. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26067. Add(' end;');
  26068. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26069. Add('function tobject.getfoo: tfuncint; begin end;');
  26070. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26071. Add('var');
  26072. Add(' Obj: TObject;');
  26073. Add(' vP: tfuncint;');
  26074. Add(' b: boolean;');
  26075. Add('begin');
  26076. Add('with obj do begin');
  26077. Add(' fonfoo:=nil;');
  26078. Add(' onfoo:=nil;');
  26079. Add(' onbar:=nil;');
  26080. Add(' fonfoo:=fonfoo;');
  26081. Add(' onfoo:=onfoo;');
  26082. Add(' onbar:=onbar;');
  26083. Add(' fonfoo:=@doit;');
  26084. Add(' onfoo:=@doit;');
  26085. Add(' onbar:=@doit;');
  26086. //Add(' fonfoo:=doit;'); // delphi
  26087. //Add(' onfoo:=doit;'); // delphi
  26088. //Add(' onbar:=doit;'); // delphi
  26089. Add(' fonfoo;');
  26090. Add(' onfoo;');
  26091. Add(' onbar;');
  26092. Add(' fonfoo();');
  26093. Add(' onfoo();');
  26094. Add(' onbar();');
  26095. Add(' b:=fonfoo=nil;');
  26096. Add(' b:=onfoo=nil;');
  26097. Add(' b:=onbar=nil;');
  26098. Add(' b:=fonfoo<>nil;');
  26099. Add(' b:=onfoo<>nil;');
  26100. Add(' b:=onbar<>nil;');
  26101. Add(' b:=fonfoo=vp;');
  26102. Add(' b:=onfoo=vp;');
  26103. Add(' b:=onbar=vp;');
  26104. Add(' b:=fonfoo=fonfoo;');
  26105. Add(' b:=onfoo=onfoo;');
  26106. Add(' b:=onbar=onfoo;');
  26107. Add(' b:=fonfoo<>fonfoo;');
  26108. Add(' b:=onfoo<>onfoo;');
  26109. Add(' b:=onbar<>onfoo;');
  26110. Add(' b:=fonfoo=@doit;');
  26111. Add(' b:=onfoo=@doit;');
  26112. Add(' b:=onbar=@doit;');
  26113. Add(' b:=fonfoo<>@doit;');
  26114. Add(' b:=onfoo<>@doit;');
  26115. Add(' b:=onbar<>@doit;');
  26116. Add(' b:=Assigned(fonfoo);');
  26117. Add(' b:=Assigned(onfoo);');
  26118. Add(' b:=Assigned(onbar);');
  26119. Add('end;');
  26120. ConvertProgram;
  26121. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26122. LinesToStr([ // statements
  26123. 'rtl.createClass(this, "TObject", null, function () {',
  26124. ' this.$init = function () {',
  26125. ' this.FOnFoo = null;',
  26126. ' };',
  26127. ' this.$final = function () {',
  26128. ' this.FOnFoo = undefined;',
  26129. ' };',
  26130. ' this.DoIt = function (vA) {',
  26131. ' var Result = 0;',
  26132. ' return Result;',
  26133. ' };',
  26134. ' this.GetFoo = function () {',
  26135. ' var Result = null;',
  26136. ' return Result;',
  26137. ' };',
  26138. ' this.SetFoo = function (Value) {',
  26139. ' };',
  26140. '});',
  26141. 'this.Obj = null;',
  26142. 'this.vP = null;',
  26143. 'this.b = false;'
  26144. ]),
  26145. LinesToStr([
  26146. 'var $with = $mod.Obj;',
  26147. '$with.FOnFoo = null;',
  26148. '$with.FOnFoo = null;',
  26149. '$with.SetFoo(null);',
  26150. '$with.FOnFoo = $with.FOnFoo;',
  26151. '$with.FOnFoo = $with.FOnFoo;',
  26152. '$with.SetFoo($with.GetFoo());',
  26153. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26154. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26155. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26156. '$with.FOnFoo(1);',
  26157. '$with.FOnFoo(1);',
  26158. '$with.GetFoo();',
  26159. '$with.FOnFoo(1);',
  26160. '$with.FOnFoo(1);',
  26161. '$with.GetFoo()(1);',
  26162. '$mod.b = $with.FOnFoo === null;',
  26163. '$mod.b = $with.FOnFoo === null;',
  26164. '$mod.b = $with.GetFoo() === null;',
  26165. '$mod.b = $with.FOnFoo !== null;',
  26166. '$mod.b = $with.FOnFoo !== null;',
  26167. '$mod.b = $with.GetFoo() !== null;',
  26168. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26169. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26170. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26171. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26172. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26173. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26174. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26175. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26176. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26177. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26178. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26179. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26180. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26181. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26182. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26183. '$mod.b = $with.FOnFoo != null;',
  26184. '$mod.b = $with.FOnFoo != null;',
  26185. '$mod.b = $with.GetFoo() != null;',
  26186. '']));
  26187. end;
  26188. procedure TTestModule.TestProcType_Nested;
  26189. begin
  26190. StartProgram(false);
  26191. Add([
  26192. 'type',
  26193. ' TProcInt = procedure(vI: longint = 1);',
  26194. 'procedure DoIt(vJ: longint);',
  26195. 'var aProc: TProcInt;',
  26196. ' b: boolean;',
  26197. ' procedure Sub(vK: longint);',
  26198. ' var aSub: TProcInt;',
  26199. ' procedure SubSub(vK: longint);',
  26200. ' var aSubSub: TProcInt;',
  26201. ' begin;',
  26202. ' aProc:=@DoIt;',
  26203. ' aSub:=@DoIt;',
  26204. ' aSubSub:=@DoIt;',
  26205. ' aProc:=@Sub;',
  26206. ' aSub:=@Sub;',
  26207. ' aSubSub:=@Sub;',
  26208. ' aProc:=@SubSub;',
  26209. ' aSub:=@SubSub;',
  26210. ' aSubSub:=@SubSub;',
  26211. ' end;',
  26212. ' begin;',
  26213. ' end;',
  26214. 'begin;',
  26215. ' aProc:=@Sub;',
  26216. ' b:=aProc=@Sub;',
  26217. ' b:=@Sub=aProc;',
  26218. 'end;',
  26219. 'begin',
  26220. '']);
  26221. ConvertProgram;
  26222. CheckSource('TestProcType_Nested',
  26223. LinesToStr([ // statements
  26224. 'this.DoIt = function (vJ) {',
  26225. ' var aProc = null;',
  26226. ' var b = false;',
  26227. ' function Sub(vK) {',
  26228. ' var aSub = null;',
  26229. ' function SubSub(vK) {',
  26230. ' var aSubSub = null;',
  26231. ' aProc = $mod.DoIt;',
  26232. ' aSub = $mod.DoIt;',
  26233. ' aSubSub = $mod.DoIt;',
  26234. ' aProc = Sub;',
  26235. ' aSub = Sub;',
  26236. ' aSubSub = Sub;',
  26237. ' aProc = SubSub;',
  26238. ' aSub = SubSub;',
  26239. ' aSubSub = SubSub;',
  26240. ' };',
  26241. ' };',
  26242. ' aProc = Sub;',
  26243. ' b = rtl.eqCallback(aProc, Sub);',
  26244. ' b = rtl.eqCallback(Sub, aProc);',
  26245. '};',
  26246. '']),
  26247. LinesToStr([ // $mod.$main
  26248. '']));
  26249. end;
  26250. procedure TTestModule.TestProcType_NestedOfObject;
  26251. begin
  26252. StartProgram(false);
  26253. Add([
  26254. 'type',
  26255. ' TProcInt = procedure(vI: longint = 1) of object;',
  26256. ' TObject = class',
  26257. ' procedure DoIt(vJ: longint);',
  26258. ' end;',
  26259. 'procedure TObject.DoIt(vJ: longint);',
  26260. 'var aProc: TProcInt;',
  26261. ' b: boolean;',
  26262. ' procedure Sub(vK: longint);',
  26263. ' var aSub: TProcInt;',
  26264. ' procedure SubSub(vK: longint);',
  26265. ' var aSubSub: TProcInt;',
  26266. ' begin;',
  26267. ' aProc:=@DoIt;',
  26268. ' aSub:=@DoIt;',
  26269. ' aSubSub:=@DoIt;',
  26270. ' aProc:=@Sub;',
  26271. ' aSub:=@Sub;',
  26272. ' aSubSub:=@Sub;',
  26273. ' aProc:=@SubSub;',
  26274. ' aSub:=@SubSub;',
  26275. ' aSubSub:=@SubSub;',
  26276. ' end;',
  26277. ' begin;',
  26278. ' end;',
  26279. 'begin;',
  26280. ' aProc:=@Sub;',
  26281. ' b:=aProc=@Sub;',
  26282. ' b:=@Sub=aProc;',
  26283. 'end;',
  26284. 'begin',
  26285. '']);
  26286. ConvertProgram;
  26287. CheckSource('TestProcType_Nested',
  26288. LinesToStr([ // statements
  26289. 'rtl.createClass(this, "TObject", null, function () {',
  26290. ' this.$init = function () {',
  26291. ' };',
  26292. ' this.$final = function () {',
  26293. ' };',
  26294. ' this.DoIt = function (vJ) {',
  26295. ' var $Self = this;',
  26296. ' var aProc = null;',
  26297. ' var b = false;',
  26298. ' function Sub(vK) {',
  26299. ' var aSub = null;',
  26300. ' function SubSub(vK) {',
  26301. ' var aSubSub = null;',
  26302. ' aProc = rtl.createCallback($Self, "DoIt");',
  26303. ' aSub = rtl.createCallback($Self, "DoIt");',
  26304. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26305. ' aProc = Sub;',
  26306. ' aSub = Sub;',
  26307. ' aSubSub = Sub;',
  26308. ' aProc = SubSub;',
  26309. ' aSub = SubSub;',
  26310. ' aSubSub = SubSub;',
  26311. ' };',
  26312. ' };',
  26313. ' aProc = Sub;',
  26314. ' b = rtl.eqCallback(aProc, Sub);',
  26315. ' b = rtl.eqCallback(Sub, aProc);',
  26316. ' };',
  26317. '});',
  26318. '']),
  26319. LinesToStr([ // $mod.$main
  26320. '']));
  26321. end;
  26322. procedure TTestModule.TestProcType_ReferenceToProc;
  26323. begin
  26324. StartProgram(false);
  26325. Add([
  26326. 'type',
  26327. ' TProcRef = reference to procedure(i: longint = 0);',
  26328. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26329. 'var',
  26330. ' p: TProcRef;',
  26331. ' f: TFuncRef;',
  26332. 'procedure DoIt(i: longint);',
  26333. 'begin',
  26334. 'end;',
  26335. 'function GetIt(i: longint): longint;',
  26336. 'begin',
  26337. ' p:=@DoIt;',
  26338. ' f:=@GetIt;',
  26339. ' f;',
  26340. ' f();',
  26341. ' f(1);',
  26342. 'end;',
  26343. 'begin',
  26344. ' p:=@DoIt;',
  26345. ' f:=@GetIt;',
  26346. ' f;',
  26347. ' f();',
  26348. ' f(1);',
  26349. ' p:=TProcRef(f);',
  26350. '']);
  26351. ConvertProgram;
  26352. CheckSource('TestProcType_ReferenceToProc',
  26353. LinesToStr([ // statements
  26354. 'this.p = null;',
  26355. 'this.f = null;',
  26356. 'this.DoIt = function (i) {',
  26357. '};',
  26358. 'this.GetIt = function (i) {',
  26359. ' var Result = 0;',
  26360. ' $mod.p = $mod.DoIt;',
  26361. ' $mod.f = $mod.GetIt;',
  26362. ' $mod.f(0);',
  26363. ' $mod.f(0);',
  26364. ' $mod.f(1);',
  26365. ' return Result;',
  26366. '};',
  26367. '']),
  26368. LinesToStr([ // $mod.$main
  26369. '$mod.p = $mod.DoIt;',
  26370. '$mod.f = $mod.GetIt;',
  26371. '$mod.f(0);',
  26372. '$mod.f(0);',
  26373. '$mod.f(1);',
  26374. '$mod.p = $mod.f;',
  26375. '']));
  26376. end;
  26377. procedure TTestModule.TestProcType_ReferenceToMethod;
  26378. begin
  26379. StartProgram(false);
  26380. Add([
  26381. 'type',
  26382. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26383. ' TObject = class',
  26384. ' function Grow(s: longint): longint;',
  26385. ' end;',
  26386. 'var',
  26387. ' f: tfuncref;',
  26388. 'function tobject.grow(s: longint): longint;',
  26389. ' function GrowSub(i: longint): longint;',
  26390. ' begin',
  26391. ' f:=@grow;',
  26392. ' f:=@growsub;',
  26393. ' end;',
  26394. 'begin',
  26395. ' f:=@grow;',
  26396. ' f:=@growsub;',
  26397. 'end;',
  26398. 'begin',
  26399. '']);
  26400. ConvertProgram;
  26401. CheckSource('TestProcType_ReferenceToMethod',
  26402. LinesToStr([ // statements
  26403. 'rtl.createClass(this, "TObject", null, function () {',
  26404. ' this.$init = function () {',
  26405. ' };',
  26406. ' this.$final = function () {',
  26407. ' };',
  26408. ' this.Grow = function (s) {',
  26409. ' var $Self = this;',
  26410. ' var Result = 0;',
  26411. ' function GrowSub(i) {',
  26412. ' var Result = 0;',
  26413. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26414. ' $mod.f = GrowSub;',
  26415. ' return Result;',
  26416. ' };',
  26417. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26418. ' $mod.f = GrowSub;',
  26419. ' return Result;',
  26420. ' };',
  26421. '});',
  26422. 'this.f = null;',
  26423. '']),
  26424. LinesToStr([ // $mod.$main
  26425. '']));
  26426. end;
  26427. procedure TTestModule.TestProcType_Typecast;
  26428. begin
  26429. StartProgram(false);
  26430. Add([
  26431. 'type',
  26432. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26433. ' TEvent = procedure of object;',
  26434. ' TGetter = function:longint of object;',
  26435. ' TProcA = procedure(i: longint);',
  26436. ' TFuncB = function(i, j: longint): longint;',
  26437. 'procedure DoIt(); varargs; begin end;',
  26438. 'var',
  26439. ' Notify: tnotifyevent;',
  26440. ' Event: tevent;',
  26441. ' Getter: tgetter;',
  26442. ' ProcA: tproca;',
  26443. ' FuncB: tfuncb;',
  26444. ' p: pointer;',
  26445. 'begin',
  26446. ' notify:=tnotifyevent(event);',
  26447. ' event:=tevent(event);',
  26448. ' event:=tevent(notify);',
  26449. ' event:=tevent(getter);',
  26450. ' event:=tevent(proca);',
  26451. ' proca:=tproca(funcb);',
  26452. ' funcb:=tfuncb(funcb);',
  26453. ' funcb:=tfuncb(proca);',
  26454. ' funcb:=tfuncb(getter);',
  26455. ' proca:=tproca(p);',
  26456. ' funcb:=tfuncb(p);',
  26457. ' getter:=tgetter(p);',
  26458. ' p:=pointer(notify);',
  26459. ' p:=notify;',
  26460. ' p:=pointer(proca);',
  26461. ' p:=proca;',
  26462. ' p:=pointer(funcb);',
  26463. ' p:=funcb;',
  26464. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26465. '']);
  26466. ConvertProgram;
  26467. CheckSource('TestProcType_Typecast',
  26468. LinesToStr([ // statements
  26469. 'this.DoIt = function () {',
  26470. '};',
  26471. 'this.Notify = null;',
  26472. 'this.Event = null;',
  26473. 'this.Getter = null;',
  26474. 'this.ProcA = null;',
  26475. 'this.FuncB = null;',
  26476. 'this.p = null;',
  26477. '']),
  26478. LinesToStr([ // $mod.$main
  26479. '$mod.Notify = $mod.Event;',
  26480. '$mod.Event = $mod.Event;',
  26481. '$mod.Event = $mod.Notify;',
  26482. '$mod.Event = $mod.Getter;',
  26483. '$mod.Event = $mod.ProcA;',
  26484. '$mod.ProcA = $mod.FuncB;',
  26485. '$mod.FuncB = $mod.FuncB;',
  26486. '$mod.FuncB = $mod.ProcA;',
  26487. '$mod.FuncB = $mod.Getter;',
  26488. '$mod.ProcA = $mod.p;',
  26489. '$mod.FuncB = $mod.p;',
  26490. '$mod.Getter = $mod.p;',
  26491. '$mod.p = $mod.Notify;',
  26492. '$mod.p = $mod.Notify;',
  26493. '$mod.p = $mod.ProcA;',
  26494. '$mod.p = $mod.ProcA;',
  26495. '$mod.p = $mod.FuncB;',
  26496. '$mod.p = $mod.FuncB;',
  26497. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26498. '']));
  26499. end;
  26500. procedure TTestModule.TestProcType_PassProcToUntyped;
  26501. begin
  26502. StartProgram(false);
  26503. Add([
  26504. 'type',
  26505. ' TEvent = procedure of object;',
  26506. ' TFunc = function: longint;',
  26507. 'procedure DoIt(); varargs; begin end;',
  26508. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26509. 'var',
  26510. ' Event: tevent;',
  26511. ' Func: TFunc;',
  26512. 'begin',
  26513. ' doit(event,func);',
  26514. ' dosome(event,event,event);',
  26515. ' dosome(func,func,func);',
  26516. '']);
  26517. ConvertProgram;
  26518. CheckSource('TestProcType_PassProcToUntyped',
  26519. LinesToStr([ // statements
  26520. 'this.DoIt = function () {',
  26521. '};',
  26522. 'this.DoSome = function (a, b, p) {',
  26523. '};',
  26524. 'this.Event = null;',
  26525. 'this.Func = null;',
  26526. '']),
  26527. LinesToStr([ // $mod.$main
  26528. '$mod.DoIt($mod.Event, $mod.Func);',
  26529. '$mod.DoSome($mod.Event, {',
  26530. ' p: $mod,',
  26531. ' get: function () {',
  26532. ' return this.p.Event;',
  26533. ' },',
  26534. ' set: function (v) {',
  26535. ' this.p.Event = v;',
  26536. ' }',
  26537. '}, $mod.Event);',
  26538. '$mod.DoSome($mod.Func, {',
  26539. ' p: $mod,',
  26540. ' get: function () {',
  26541. ' return this.p.Func;',
  26542. ' },',
  26543. ' set: function (v) {',
  26544. ' this.p.Func = v;',
  26545. ' }',
  26546. '}, $mod.Func);',
  26547. '']));
  26548. end;
  26549. procedure TTestModule.TestProcType_PassProcToArray;
  26550. begin
  26551. StartProgram(false);
  26552. Add([
  26553. 'type',
  26554. ' TFunc = function: longint;',
  26555. ' TArrFunc = array of TFunc;',
  26556. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26557. 'function GetIt: longint; begin end;',
  26558. 'var',
  26559. ' Func: tfunc;',
  26560. 'begin',
  26561. ' doit([]);',
  26562. ' doit([@GetIt]);',
  26563. ' doit([Func]);',
  26564. '']);
  26565. ConvertProgram;
  26566. CheckSource('TestProcType_PassProcToArray',
  26567. LinesToStr([ // statements
  26568. 'this.DoIt = function (Arr) {',
  26569. '};',
  26570. 'this.GetIt = function () {',
  26571. ' var Result = 0;',
  26572. ' return Result;',
  26573. '};',
  26574. 'this.Func = null;',
  26575. '']),
  26576. LinesToStr([ // $mod.$main
  26577. '$mod.DoIt([]);',
  26578. '$mod.DoIt([$mod.GetIt]);',
  26579. '$mod.DoIt([$mod.Func]);',
  26580. '']));
  26581. end;
  26582. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26583. begin
  26584. StartProgram(false);
  26585. Add([
  26586. '{$modeswitch externalclass}',
  26587. 'type',
  26588. ' TProc = reference to procedure(i: longint); safecall;',
  26589. ' TEvent = procedure(i: longint) of object; safecall;',
  26590. ' TExtA = class external name ''ExtObj''',
  26591. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26592. ' procedure DoSome(Id: longint = 1);',
  26593. ' procedure SetOnClick(const e: TEvent);',
  26594. ' property OnClick: TEvent write SetOnClick;',
  26595. ' class procedure Fly(Id: longint = 1); static;',
  26596. ' procedure SetOnShow(const p: TProc);',
  26597. ' property OnShow: TProc write SetOnShow;',
  26598. ' end;',
  26599. 'procedure Run(i: longint = 1);',
  26600. 'begin',
  26601. 'end;',
  26602. 'var',
  26603. ' Obj: texta;',
  26604. ' e: TEvent;',
  26605. ' p: TProc;',
  26606. 'begin',
  26607. ' e:=e;',
  26608. ' e:[email protected];',
  26609. ' e:[email protected];',
  26610. ' e:=TEvent(@obj.dosome);', // no safecall
  26611. ' obj.OnClick:[email protected];',
  26612. ' obj.OnClick:[email protected];',
  26613. ' obj.setonclick(@obj.doit);',
  26614. ' obj.setonclick(@obj.dosome);',
  26615. ' p:=@Run;',
  26616. ' p:[email protected];',
  26617. ' obj.OnShow:=@Run;',
  26618. ' obj.OnShow:[email protected];',
  26619. ' obj.setOnShow(@Run);',
  26620. ' obj.setOnShow(@TExtA.Fly);',
  26621. ' with obj do begin',
  26622. ' e:=@doit;',
  26623. ' e:=@dosome;',
  26624. ' OnClick:=@doit;',
  26625. ' OnClick:=@dosome;',
  26626. ' setonclick(@doit);',
  26627. ' setonclick(@dosome);',
  26628. ' OnShow:=@Run;',
  26629. ' setOnShow(@Run);',
  26630. ' end;']);
  26631. ConvertProgram;
  26632. CheckSource('TestProcType_SafeCallObjFPC',
  26633. LinesToStr([ // statements
  26634. 'this.Run = function (i) {',
  26635. '};',
  26636. 'this.Obj = null;',
  26637. 'this.e = null;',
  26638. 'this.p = null;',
  26639. '']),
  26640. LinesToStr([ // $mod.$main
  26641. '$mod.e = $mod.e;',
  26642. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26643. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26644. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26645. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26646. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26647. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26648. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26649. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26650. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26651. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26652. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26653. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26654. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26655. 'var $with = $mod.Obj;',
  26656. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26657. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26658. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26659. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26660. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26661. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26662. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26663. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26664. '']));
  26665. end;
  26666. procedure TTestModule.TestProcType_SafeCallDelphi;
  26667. begin
  26668. StartProgram(false);
  26669. Add([
  26670. '{$mode delphi}',
  26671. '{$modeswitch externalclass}',
  26672. 'type',
  26673. ' TProc = reference to procedure(i: longint); safecall;',
  26674. ' TEvent = procedure(i: longint) of object; safecall;',
  26675. ' TExtA = class external name ''ExtObj''',
  26676. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26677. ' procedure DoSome(Id: longint = 1);',
  26678. ' procedure SetOnClick(const e: TEvent);',
  26679. ' property OnClick: TEvent write SetOnClick;',
  26680. ' class procedure Fly(Id: longint = 1); static;',
  26681. ' procedure SetOnShow(const p: TProc);',
  26682. ' property OnShow: TProc write SetOnShow;',
  26683. ' end;',
  26684. 'procedure Run(i: longint = 1);',
  26685. 'begin',
  26686. 'end;',
  26687. 'var',
  26688. ' Obj: texta;',
  26689. ' e: TEvent;',
  26690. ' p: TProc;',
  26691. 'begin',
  26692. ' e:=e;',
  26693. ' e:=obj.doit;',
  26694. ' e:=obj.dosome;',
  26695. ' e:=TEvent(@obj.dosome);', // no safecall
  26696. ' obj.OnClick:=obj.doit;',
  26697. ' obj.OnClick:=obj.dosome;',
  26698. ' obj.setonclick(obj.doit);',
  26699. ' obj.setonclick(obj.dosome);',
  26700. ' p:=Run;',
  26701. ' p:=TExtA.Fly;',
  26702. ' obj.OnShow:=Run;',
  26703. ' obj.OnShow:=TExtA.Fly;',
  26704. ' obj.setOnShow(Run);',
  26705. ' obj.setOnShow(TExtA.Fly);',
  26706. ' with obj do begin',
  26707. ' e:=doit;',
  26708. ' e:=dosome;',
  26709. ' OnClick:=doit;',
  26710. ' OnClick:=dosome;',
  26711. ' setonclick(doit);',
  26712. ' setonclick(dosome);',
  26713. ' OnShow:=@Run;',
  26714. ' setOnShow(@Run);',
  26715. ' end;']);
  26716. ConvertProgram;
  26717. CheckSource('TestProcType_SafeCallDelphi',
  26718. LinesToStr([ // statements
  26719. 'this.Run = function (i) {',
  26720. '};',
  26721. 'this.Obj = null;',
  26722. 'this.e = null;',
  26723. 'this.p = null;',
  26724. '']),
  26725. LinesToStr([ // $mod.$main
  26726. '$mod.e = $mod.e;',
  26727. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26728. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26729. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26730. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26731. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26732. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26733. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26734. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26735. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26736. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26737. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26738. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26739. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26740. 'var $with = $mod.Obj;',
  26741. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26742. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26743. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26744. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26745. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26746. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26747. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26748. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26749. '']));
  26750. end;
  26751. procedure TTestModule.TestPointer;
  26752. begin
  26753. StartProgram(false);
  26754. Add(['type',
  26755. ' TObject = class end;',
  26756. ' TClass = class of TObject;',
  26757. ' TArrInt = array of longint;',
  26758. 'const',
  26759. ' n = nil;',
  26760. 'var',
  26761. ' v: jsvalue;',
  26762. ' Obj: tobject;',
  26763. ' C: tclass;',
  26764. ' a: tarrint;',
  26765. ' p: Pointer = nil;',
  26766. ' s: string;',
  26767. 'begin',
  26768. ' p:=p;',
  26769. ' p:=nil;',
  26770. ' if p=nil then;',
  26771. ' if nil=p then;',
  26772. ' if Assigned(p) then;',
  26773. ' p:=Pointer(v);',
  26774. ' p:=obj;',
  26775. ' p:=c;',
  26776. ' p:=a;',
  26777. ' p:=tobject;',
  26778. ' obj:=TObject(p);',
  26779. ' c:=TClass(p);',
  26780. ' a:=TArrInt(p);',
  26781. ' p:=n;',
  26782. ' p:=Pointer(a);',
  26783. ' p:=pointer(s);',
  26784. ' s:=string(p);',
  26785. '']);
  26786. ConvertProgram;
  26787. CheckSource('TestPointer',
  26788. LinesToStr([ // statements
  26789. 'rtl.createClass(this, "TObject", null, function () {',
  26790. ' this.$init = function () {',
  26791. ' };',
  26792. ' this.$final = function () {',
  26793. ' };',
  26794. '});',
  26795. 'this.n = null;',
  26796. 'this.v = undefined;',
  26797. 'this.Obj = null;',
  26798. 'this.C = null;',
  26799. 'this.a = [];',
  26800. 'this.p = null;',
  26801. 'this.s = "";',
  26802. '']),
  26803. LinesToStr([ // $mod.$main
  26804. '$mod.p = $mod.p;',
  26805. '$mod.p = null;',
  26806. 'if ($mod.p === null) ;',
  26807. 'if (null === $mod.p) ;',
  26808. 'if ($mod.p != null) ;',
  26809. '$mod.p = $mod.v;',
  26810. '$mod.p = $mod.Obj;',
  26811. '$mod.p = $mod.C;',
  26812. '$mod.p = $mod.a;',
  26813. '$mod.p = $mod.TObject;',
  26814. '$mod.Obj = $mod.p;',
  26815. '$mod.C = $mod.p;',
  26816. '$mod.a = $mod.p;',
  26817. '$mod.p = null;',
  26818. '$mod.p = $mod.a;',
  26819. '$mod.p = $mod.s;',
  26820. '$mod.s = $mod.p;',
  26821. '']));
  26822. end;
  26823. procedure TTestModule.TestPointer_Proc;
  26824. begin
  26825. StartProgram(false);
  26826. Add('type');
  26827. Add(' TObject = class');
  26828. Add(' procedure DoIt; virtual; abstract;');
  26829. Add(' end;');
  26830. Add('procedure DoSome; begin end;');
  26831. Add('var');
  26832. Add(' o: TObject;');
  26833. Add(' p: Pointer;');
  26834. Add('begin');
  26835. Add(' p:=@DoSome;');
  26836. Add(' p:[email protected];');
  26837. ConvertProgram;
  26838. CheckSource('TestPointer_Proc',
  26839. LinesToStr([ // statements
  26840. 'rtl.createClass(this, "TObject", null, function () {',
  26841. ' this.$init = function () {',
  26842. ' };',
  26843. ' this.$final = function () {',
  26844. ' };',
  26845. '});',
  26846. 'this.DoSome = function () {',
  26847. '};',
  26848. 'this.o = null;',
  26849. 'this.p = null;',
  26850. '']),
  26851. LinesToStr([ // $mod.$main
  26852. '$mod.p = $mod.DoSome;',
  26853. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26854. '']));
  26855. end;
  26856. procedure TTestModule.TestPointer_AssignRecordFail;
  26857. begin
  26858. StartProgram(false);
  26859. Add('type');
  26860. Add(' TRec = record end;');
  26861. Add('var');
  26862. Add(' p: Pointer;');
  26863. Add(' r: TRec;');
  26864. Add('begin');
  26865. Add(' p:=r;');
  26866. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26867. nIncompatibleTypesGotExpected);
  26868. ConvertProgram;
  26869. end;
  26870. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26871. begin
  26872. StartProgram(false);
  26873. Add('type');
  26874. Add(' TArr = array[boolean] of longint;');
  26875. Add('var');
  26876. Add(' p: Pointer;');
  26877. Add(' a: TArr;');
  26878. Add('begin');
  26879. Add(' p:=a;');
  26880. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26881. nIncompatibleTypesGotExpected);
  26882. ConvertProgram;
  26883. end;
  26884. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26885. begin
  26886. StartProgram(false);
  26887. Add([
  26888. 'procedure DoIt(args: array of jsvalue); begin end;',
  26889. 'procedure DoAll; varargs; begin end;',
  26890. 'var',
  26891. ' v: jsvalue;',
  26892. 'begin',
  26893. ' DoIt([pointer(v)]);',
  26894. ' DoAll(pointer(v));',
  26895. '']);
  26896. ConvertProgram;
  26897. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26898. LinesToStr([ // statements
  26899. 'this.DoIt = function (args) {',
  26900. '};',
  26901. 'this.DoAll = function () {',
  26902. '};',
  26903. 'this.v = undefined;',
  26904. '']),
  26905. LinesToStr([ // $mod.$main
  26906. '$mod.DoIt([$mod.v]);',
  26907. '$mod.DoAll($mod.v);',
  26908. '']));
  26909. end;
  26910. procedure TTestModule.TestPointer_NonRecordFail;
  26911. begin
  26912. StartProgram(false);
  26913. Add([
  26914. 'type',
  26915. ' p = ^longint;',
  26916. 'begin',
  26917. '']);
  26918. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26919. ConvertProgram;
  26920. end;
  26921. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26922. begin
  26923. StartProgram(false);
  26924. Add([
  26925. 'procedure DoIt(p: ^longint); begin end;',
  26926. 'begin',
  26927. '']);
  26928. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26929. ConvertProgram;
  26930. end;
  26931. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26932. begin
  26933. StartProgram(false);
  26934. Add([
  26935. 'var p: ^longint;',
  26936. 'begin',
  26937. '']);
  26938. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26939. ConvertProgram;
  26940. end;
  26941. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26942. begin
  26943. StartProgram(false);
  26944. Add([
  26945. 'function DoIt: ^longint; begin end;',
  26946. 'begin',
  26947. '']);
  26948. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26949. ConvertProgram;
  26950. end;
  26951. procedure TTestModule.TestPointer_AddrOperatorFail;
  26952. begin
  26953. StartProgram(false);
  26954. Add([
  26955. 'var i: longint;',
  26956. 'begin',
  26957. ' if @i=nil then ;',
  26958. '']);
  26959. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26960. ConvertProgram;
  26961. end;
  26962. procedure TTestModule.TestPointer_ArrayParamsFail;
  26963. begin
  26964. StartProgram(false);
  26965. Add([
  26966. 'var',
  26967. ' p: Pointer;',
  26968. 'begin',
  26969. ' p:=p[1];',
  26970. '']);
  26971. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26972. ConvertProgram;
  26973. end;
  26974. procedure TTestModule.TestPointer_PointerAddFail;
  26975. begin
  26976. StartProgram(false);
  26977. Add([
  26978. 'var',
  26979. ' p: Pointer;',
  26980. 'begin',
  26981. ' p:=p+1;',
  26982. '']);
  26983. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26984. ConvertProgram;
  26985. end;
  26986. procedure TTestModule.TestPointer_IncPointerFail;
  26987. begin
  26988. StartProgram(false);
  26989. Add([
  26990. 'var',
  26991. ' p: Pointer;',
  26992. 'begin',
  26993. ' inc(p,1);',
  26994. '']);
  26995. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26996. nIncompatibleTypeArgNo);
  26997. ConvertProgram;
  26998. end;
  26999. procedure TTestModule.TestPointer_Record;
  27000. begin
  27001. StartProgram(false);
  27002. Add([
  27003. 'type',
  27004. ' TRec = record x: longint; end;',
  27005. ' PRec = ^TRec;',
  27006. 'var',
  27007. ' r: TRec;',
  27008. ' p: PRec;',
  27009. ' q: ^TRec;',
  27010. ' Ptr: pointer;',
  27011. 'begin',
  27012. ' new(p);',
  27013. ' p:=@r;',
  27014. ' r:=p^;',
  27015. ' r.x:=p^.x;',
  27016. ' p^.x:=r.x;',
  27017. ' if p^.x=3 then ;',
  27018. ' if 4=p^.x then ;',
  27019. ' dispose(p);',
  27020. ' new(q);',
  27021. ' dispose(q);',
  27022. ' Ptr:=p;',
  27023. ' p:=PRec(ptr);',
  27024. '']);
  27025. ConvertProgram;
  27026. CheckSource('TestPointer_Record',
  27027. LinesToStr([ // statements
  27028. 'rtl.recNewT(this, "TRec", function () {',
  27029. ' this.x = 0;',
  27030. ' this.$eq = function (b) {',
  27031. ' return this.x === b.x;',
  27032. ' };',
  27033. ' this.$assign = function (s) {',
  27034. ' this.x = s.x;',
  27035. ' return this;',
  27036. ' };',
  27037. '});',
  27038. 'this.r = this.TRec.$new();',
  27039. 'this.p = null;',
  27040. 'this.q = null;',
  27041. 'this.Ptr = null;',
  27042. '']),
  27043. LinesToStr([ // $mod.$main
  27044. '$mod.p = $mod.TRec.$new();',
  27045. '$mod.p = $mod.r;',
  27046. '$mod.r.$assign($mod.p);',
  27047. '$mod.r.x = $mod.p.x;',
  27048. '$mod.p.x = $mod.r.x;',
  27049. 'if ($mod.p.x === 3) ;',
  27050. 'if (4 === $mod.p.x) ;',
  27051. '$mod.p = null;',
  27052. '$mod.q = $mod.TRec.$new();',
  27053. '$mod.q = null;',
  27054. '$mod.Ptr = $mod.p;',
  27055. '$mod.p = $mod.Ptr;',
  27056. '']));
  27057. end;
  27058. procedure TTestModule.TestPointer_RecordArg;
  27059. begin
  27060. StartProgram(false);
  27061. Add([
  27062. '{$modeswitch autoderef}',
  27063. 'type',
  27064. ' TRec = record x: longint; end;',
  27065. ' PRec = ^TRec;',
  27066. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27067. 'begin',
  27068. ' a.x:=a.x;',
  27069. ' a^.x:=a^.x;',
  27070. ' with a^ do',
  27071. ' x:=x;',
  27072. 'end;',
  27073. 'function GetIt(p: PRec): PRec;',
  27074. 'begin',
  27075. ' p.x:=p.x;',
  27076. ' p^.x:=p^.x;',
  27077. ' with p^ do',
  27078. ' x:=x;',
  27079. 'end;',
  27080. 'var',
  27081. ' r: TRec;',
  27082. ' p: PRec;',
  27083. 'begin',
  27084. ' p:=GetIt(p);',
  27085. ' p^:=GetIt(@r)^;',
  27086. ' DoIt(p,p,p);',
  27087. ' DoIt(@r,p,p);',
  27088. '']);
  27089. ConvertProgram;
  27090. CheckSource('TestPointer_RecordArg',
  27091. LinesToStr([ // statements
  27092. 'rtl.recNewT(this, "TRec", function () {',
  27093. ' this.x = 0;',
  27094. ' this.$eq = function (b) {',
  27095. ' return this.x === b.x;',
  27096. ' };',
  27097. ' this.$assign = function (s) {',
  27098. ' this.x = s.x;',
  27099. ' return this;',
  27100. ' };',
  27101. '});',
  27102. 'this.DoIt = function (a, b, c) {',
  27103. ' var Result = $mod.TRec.$new();',
  27104. ' a.x = a.x;',
  27105. ' a.x = a.x;',
  27106. ' a.x = a.x;',
  27107. ' return Result;',
  27108. '};',
  27109. 'this.GetIt = function (p) {',
  27110. ' var Result = null;',
  27111. ' p.x = p.x;',
  27112. ' p.x = p.x;',
  27113. ' p.x = p.x;',
  27114. ' return Result;',
  27115. '};',
  27116. 'this.r = this.TRec.$new();',
  27117. 'this.p = null;',
  27118. '']),
  27119. LinesToStr([ // $mod.$main
  27120. '$mod.p = $mod.GetIt($mod.p);',
  27121. '$mod.p.$assign($mod.GetIt($mod.r));',
  27122. '$mod.DoIt($mod.p, {',
  27123. ' p: $mod,',
  27124. ' get: function () {',
  27125. ' return this.p.p;',
  27126. ' },',
  27127. ' set: function (v) {',
  27128. ' this.p.p = v;',
  27129. ' }',
  27130. '}, {',
  27131. ' p: $mod,',
  27132. ' get: function () {',
  27133. ' return this.p.p;',
  27134. ' },',
  27135. ' set: function (v) {',
  27136. ' this.p.p = v;',
  27137. ' }',
  27138. '});',
  27139. '$mod.DoIt($mod.r, {',
  27140. ' p: $mod,',
  27141. ' get: function () {',
  27142. ' return this.p.p;',
  27143. ' },',
  27144. ' set: function (v) {',
  27145. ' this.p.p = v;',
  27146. ' }',
  27147. '}, {',
  27148. ' p: $mod,',
  27149. ' get: function () {',
  27150. ' return this.p.p;',
  27151. ' },',
  27152. ' set: function (v) {',
  27153. ' this.p.p = v;',
  27154. ' }',
  27155. '});',
  27156. '']));
  27157. end;
  27158. procedure TTestModule.TestJSValue_AssignToJSValue;
  27159. begin
  27160. StartProgram(false);
  27161. Add('var');
  27162. Add(' v: jsvalue;');
  27163. Add(' i: longint;');
  27164. Add(' s: string;');
  27165. Add(' b: boolean;');
  27166. Add(' d: double;');
  27167. Add(' p: pointer;');
  27168. Add('begin');
  27169. Add(' v:=v;');
  27170. Add(' v:=1;');
  27171. Add(' v:=i;');
  27172. Add(' v:='''';');
  27173. Add(' v:=''c'';');
  27174. Add(' v:=''foo'';');
  27175. Add(' v:=s;');
  27176. Add(' v:=false;');
  27177. Add(' v:=true;');
  27178. Add(' v:=b;');
  27179. Add(' v:=0.1;');
  27180. Add(' v:=d;');
  27181. Add(' v:=nil;');
  27182. Add(' v:=p;');
  27183. ConvertProgram;
  27184. CheckSource('TestJSValue_AssignToJSValue',
  27185. LinesToStr([ // statements
  27186. 'this.v = undefined;',
  27187. 'this.i = 0;',
  27188. 'this.s = "";',
  27189. 'this.b = false;',
  27190. 'this.d = 0.0;',
  27191. 'this.p = null;',
  27192. '']),
  27193. LinesToStr([ // $mod.$main
  27194. '$mod.v = $mod.v;',
  27195. '$mod.v = 1;',
  27196. '$mod.v = $mod.i;',
  27197. '$mod.v = "";',
  27198. '$mod.v = "c";',
  27199. '$mod.v = "foo";',
  27200. '$mod.v = $mod.s;',
  27201. '$mod.v = false;',
  27202. '$mod.v = true;',
  27203. '$mod.v = $mod.b;',
  27204. '$mod.v = 0.1;',
  27205. '$mod.v = $mod.d;',
  27206. '$mod.v = null;',
  27207. '$mod.v = $mod.p;',
  27208. '']));
  27209. end;
  27210. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27211. begin
  27212. StartProgram(false);
  27213. Add('type');
  27214. Add(' integer = longint;');
  27215. Add(' TYesNo = boolean;');
  27216. Add(' TFloat = double;');
  27217. Add(' TCaption = string;');
  27218. Add(' TChar = char;');
  27219. Add('var');
  27220. Add(' v: jsvalue;');
  27221. Add(' i: integer;');
  27222. Add(' s: TCaption;');
  27223. Add(' b: TYesNo;');
  27224. Add(' d: TFloat;');
  27225. Add(' c: char;');
  27226. Add('begin');
  27227. Add(' i:=longint(v);');
  27228. Add(' i:=integer(v);');
  27229. Add(' s:=string(v);');
  27230. Add(' s:=TCaption(v);');
  27231. Add(' b:=boolean(v);');
  27232. Add(' b:=TYesNo(v);');
  27233. Add(' d:=double(v);');
  27234. Add(' d:=TFloat(v);');
  27235. Add(' c:=char(v);');
  27236. Add(' c:=TChar(v);');
  27237. ConvertProgram;
  27238. CheckSource('TestJSValue_TypeCastToBaseType',
  27239. LinesToStr([ // statements
  27240. 'this.v = undefined;',
  27241. 'this.i = 0;',
  27242. 'this.s = "";',
  27243. 'this.b = false;',
  27244. 'this.d = 0.0;',
  27245. 'this.c = "";',
  27246. '']),
  27247. LinesToStr([ // $mod.$main
  27248. '$mod.i = rtl.trunc($mod.v);',
  27249. '$mod.i = rtl.trunc($mod.v);',
  27250. '$mod.s = "" + $mod.v;',
  27251. '$mod.s = "" + $mod.v;',
  27252. '$mod.b = !($mod.v == false);',
  27253. '$mod.b = !($mod.v == false);',
  27254. '$mod.d = rtl.getNumber($mod.v);',
  27255. '$mod.d = rtl.getNumber($mod.v);',
  27256. '$mod.c = rtl.getChar($mod.v);',
  27257. '$mod.c = rtl.getChar($mod.v);',
  27258. '']));
  27259. end;
  27260. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27261. begin
  27262. StartProgram(false);
  27263. Add([
  27264. 'type',
  27265. ' TArr = array of word;',
  27266. ' TRec = record end;',
  27267. ' TSet = set of boolean;',
  27268. 'procedure Fly(v: jsvalue);',
  27269. 'begin',
  27270. 'end;',
  27271. 'var',
  27272. ' a: TArr;',
  27273. ' r: TRec;',
  27274. ' s: TSet;',
  27275. 'begin',
  27276. ' Fly(jsvalue(a));',
  27277. ' Fly(jsvalue(r));',
  27278. ' Fly(jsvalue(s));',
  27279. '']);
  27280. ConvertProgram;
  27281. CheckSource('TestJSValue_TypecastToJSValue',
  27282. LinesToStr([ // statements
  27283. 'rtl.recNewT(this, "TRec", function () {',
  27284. ' this.$eq = function (b) {',
  27285. ' return true;',
  27286. ' };',
  27287. ' this.$assign = function (s) {',
  27288. ' return this;',
  27289. ' };',
  27290. '});',
  27291. 'this.Fly = function (v) {',
  27292. '};',
  27293. 'this.a = [];',
  27294. 'this.r = this.TRec.$new();',
  27295. 'this.s = {};',
  27296. '']),
  27297. LinesToStr([ // $mod.$main
  27298. '$mod.Fly($mod.a);',
  27299. '$mod.Fly($mod.r);',
  27300. '$mod.Fly($mod.s);',
  27301. '']));
  27302. end;
  27303. procedure TTestModule.TestJSValue_Equal;
  27304. begin
  27305. StartProgram(false);
  27306. Add('type');
  27307. Add(' integer = longint;');
  27308. Add(' TYesNo = boolean;');
  27309. Add(' TFloat = double;');
  27310. Add(' TCaption = string;');
  27311. Add(' TChar = char;');
  27312. Add(' TMulti = JSValue;');
  27313. Add('var');
  27314. Add(' v: jsvalue;');
  27315. Add(' i: integer;');
  27316. Add(' s: TCaption;');
  27317. Add(' b: TYesNo;');
  27318. Add(' d: TFloat;');
  27319. Add(' c: char;');
  27320. Add(' m: TMulti;');
  27321. Add('begin');
  27322. Add(' b:=v=v;');
  27323. Add(' b:=v<>v;');
  27324. Add(' b:=v=1;');
  27325. Add(' b:=v<>1;');
  27326. Add(' b:=2=v;');
  27327. Add(' b:=2<>v;');
  27328. Add(' b:=v=i;');
  27329. Add(' b:=i=v;');
  27330. Add(' b:=v=nil;');
  27331. Add(' b:=nil=v;');
  27332. Add(' b:=v=false;');
  27333. Add(' b:=true=v;');
  27334. Add(' b:=v=b;');
  27335. Add(' b:=b=v;');
  27336. Add(' b:=v=s;');
  27337. Add(' b:=s=v;');
  27338. Add(' b:=v=''foo'';');
  27339. Add(' b:=''''=v;');
  27340. Add(' b:=v=d;');
  27341. Add(' b:=d=v;');
  27342. Add(' b:=v=3.4;');
  27343. Add(' b:=5.6=v;');
  27344. Add(' b:=v=c;');
  27345. Add(' b:=c=v;');
  27346. Add(' b:=m=m;');
  27347. Add(' b:=v=m;');
  27348. Add(' b:=m=v;');
  27349. ConvertProgram;
  27350. CheckSource('TestJSValue_Equal',
  27351. LinesToStr([ // statements
  27352. 'this.v = undefined;',
  27353. 'this.i = 0;',
  27354. 'this.s = "";',
  27355. 'this.b = false;',
  27356. 'this.d = 0.0;',
  27357. 'this.c = "";',
  27358. 'this.m = undefined;',
  27359. '']),
  27360. LinesToStr([ // $mod.$main
  27361. '$mod.b = $mod.v == $mod.v;',
  27362. '$mod.b = $mod.v != $mod.v;',
  27363. '$mod.b = $mod.v == 1;',
  27364. '$mod.b = $mod.v != 1;',
  27365. '$mod.b = 2 == $mod.v;',
  27366. '$mod.b = 2 != $mod.v;',
  27367. '$mod.b = $mod.v == $mod.i;',
  27368. '$mod.b = $mod.i == $mod.v;',
  27369. '$mod.b = $mod.v == null;',
  27370. '$mod.b = null == $mod.v;',
  27371. '$mod.b = $mod.v == false;',
  27372. '$mod.b = true == $mod.v;',
  27373. '$mod.b = $mod.v == $mod.b;',
  27374. '$mod.b = $mod.b == $mod.v;',
  27375. '$mod.b = $mod.v == $mod.s;',
  27376. '$mod.b = $mod.s == $mod.v;',
  27377. '$mod.b = $mod.v == "foo";',
  27378. '$mod.b = "" == $mod.v;',
  27379. '$mod.b = $mod.v == $mod.d;',
  27380. '$mod.b = $mod.d == $mod.v;',
  27381. '$mod.b = $mod.v == 3.4;',
  27382. '$mod.b = 5.6 == $mod.v;',
  27383. '$mod.b = $mod.v == $mod.c;',
  27384. '$mod.b = $mod.c == $mod.v;',
  27385. '$mod.b = $mod.m == $mod.m;',
  27386. '$mod.b = $mod.v == $mod.m;',
  27387. '$mod.b = $mod.m == $mod.v;',
  27388. '']));
  27389. end;
  27390. procedure TTestModule.TestJSValue_If;
  27391. begin
  27392. StartProgram(false);
  27393. Add([
  27394. 'procedure Fly(var u);',
  27395. 'begin',
  27396. ' if jsvalue(u) then ;',
  27397. 'end;',
  27398. 'var',
  27399. ' v: jsvalue;',
  27400. 'begin',
  27401. ' if v then ;',
  27402. ' while v do ;',
  27403. ' repeat until v;',
  27404. '']);
  27405. ConvertProgram;
  27406. CheckSource('TestJSValue_If',
  27407. LinesToStr([ // statements
  27408. 'this.Fly = function (u) {',
  27409. ' if (u.get()) ;',
  27410. '};',
  27411. 'this.v = undefined;',
  27412. '']),
  27413. LinesToStr([ // $mod.$main
  27414. 'if ($mod.v) ;',
  27415. 'while($mod.v){',
  27416. '};',
  27417. 'do{',
  27418. '} while(!$mod.v);',
  27419. '']));
  27420. end;
  27421. procedure TTestModule.TestJSValue_Not;
  27422. begin
  27423. StartProgram(false);
  27424. Add([
  27425. 'var',
  27426. ' v: jsvalue;',
  27427. ' b: boolean;',
  27428. 'begin',
  27429. ' b:=not v;',
  27430. ' if not v then ;',
  27431. ' while not v do ;',
  27432. ' repeat until not v;',
  27433. '']);
  27434. ConvertProgram;
  27435. CheckSource('TestJSValue_If',
  27436. LinesToStr([ // statements
  27437. 'this.v = undefined;',
  27438. 'this.b = false;',
  27439. '']),
  27440. LinesToStr([ // $mod.$main
  27441. '$mod.b=!$mod.v;',
  27442. 'if (!$mod.v) ;',
  27443. 'while(!$mod.v){',
  27444. '};',
  27445. 'do{',
  27446. '} while($mod.v);',
  27447. '']));
  27448. end;
  27449. procedure TTestModule.TestJSValue_Enum;
  27450. begin
  27451. StartProgram(false);
  27452. Add('type');
  27453. Add(' TColor = (red, blue);');
  27454. Add(' TRedBlue = TColor;');
  27455. Add('var');
  27456. Add(' v: jsvalue;');
  27457. Add(' e: TColor;');
  27458. Add('begin');
  27459. Add(' v:=e;');
  27460. Add(' v:=TColor(e);');
  27461. Add(' v:=TRedBlue(e);');
  27462. Add(' e:=TColor(v);');
  27463. Add(' e:=TRedBlue(v);');
  27464. ConvertProgram;
  27465. CheckSource('TestJSValue_Enum',
  27466. LinesToStr([ // statements
  27467. 'this.TColor = {',
  27468. ' "0": "red",',
  27469. ' red: 0,',
  27470. ' "1": "blue",',
  27471. ' blue: 1',
  27472. '};',
  27473. 'this.v = undefined;',
  27474. 'this.e = 0;',
  27475. '']),
  27476. LinesToStr([ // $mod.$main
  27477. '$mod.v = $mod.e;',
  27478. '$mod.v = $mod.e;',
  27479. '$mod.v = $mod.e;',
  27480. '$mod.e = $mod.v;',
  27481. '$mod.e = $mod.v;',
  27482. '']));
  27483. end;
  27484. procedure TTestModule.TestJSValue_ClassInstance;
  27485. begin
  27486. StartProgram(false);
  27487. Add([
  27488. 'type',
  27489. ' TObject = class',
  27490. ' end;',
  27491. ' TBirdObject = TObject;',
  27492. 'var',
  27493. ' v: jsvalue;',
  27494. ' o: TObject;',
  27495. 'begin',
  27496. ' v:=o;',
  27497. ' v:=TObject(o);',
  27498. ' v:=TBirdObject(o);',
  27499. ' o:=TObject(v);',
  27500. ' o:=TBirdObject(v);',
  27501. ' if v is TObject then ;',
  27502. '']);
  27503. ConvertProgram;
  27504. CheckSource('TestJSValue_ClassInstance',
  27505. LinesToStr([ // statements
  27506. 'rtl.createClass(this, "TObject", null, function () {',
  27507. ' this.$init = function () {',
  27508. ' };',
  27509. ' this.$final = function () {',
  27510. ' };',
  27511. '});',
  27512. 'this.v = undefined;',
  27513. 'this.o = null;',
  27514. '']),
  27515. LinesToStr([ // $mod.$main
  27516. '$mod.v = $mod.o;',
  27517. '$mod.v = $mod.o;',
  27518. '$mod.v = $mod.o;',
  27519. '$mod.o = rtl.getObject($mod.v);',
  27520. '$mod.o = rtl.getObject($mod.v);',
  27521. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27522. '']));
  27523. end;
  27524. procedure TTestModule.TestJSValue_ClassOf;
  27525. begin
  27526. StartProgram(false);
  27527. Add([
  27528. 'type',
  27529. ' TClass = class of TObject;',
  27530. ' TObject = class',
  27531. ' end;',
  27532. ' TBirds = class of TBird;',
  27533. ' TBird = class(TObject) end;',
  27534. 'var',
  27535. ' v: jsvalue;',
  27536. ' c: TClass;',
  27537. 'begin',
  27538. ' v:=c;',
  27539. ' v:=TObject;',
  27540. ' v:=TClass(c);',
  27541. ' v:=TBirds(c);',
  27542. ' c:=TClass(v);',
  27543. ' c:=TBirds(v);',
  27544. ' if v is TClass then ;',
  27545. '']);
  27546. ConvertProgram;
  27547. CheckSource('TestJSValue_ClassOf',
  27548. LinesToStr([ // statements
  27549. 'rtl.createClass(this, "TObject", null, function () {',
  27550. ' this.$init = function () {',
  27551. ' };',
  27552. ' this.$final = function () {',
  27553. ' };',
  27554. '});',
  27555. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27556. '});',
  27557. 'this.v = undefined;',
  27558. 'this.c = null;',
  27559. '']),
  27560. LinesToStr([ // $mod.$main
  27561. '$mod.v = $mod.c;',
  27562. '$mod.v = $mod.TObject;',
  27563. '$mod.v = $mod.c;',
  27564. '$mod.v = $mod.c;',
  27565. '$mod.c = rtl.getObject($mod.v);',
  27566. '$mod.c = rtl.getObject($mod.v);',
  27567. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27568. '']));
  27569. end;
  27570. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27571. begin
  27572. StartProgram(false);
  27573. Add([
  27574. 'type',
  27575. ' integer = longint;',
  27576. ' TArray = array of JSValue;',
  27577. ' TArrgh = tarray;',
  27578. ' TArrInt = array of integer;',
  27579. 'var',
  27580. ' v: jsvalue;',
  27581. ' TheArray: tarray = (1,''2'');',
  27582. ' Arr: tarrgh;',
  27583. ' i: integer;',
  27584. ' ArrInt: tarrint;',
  27585. 'begin',
  27586. ' arr:=thearray;',
  27587. ' thearray:=arr;',
  27588. ' setlength(arr,2);',
  27589. ' setlength(thearray,3);',
  27590. ' arr[4]:=v;',
  27591. ' arr[5]:=length(thearray);',
  27592. ' arr[6]:=nil;',
  27593. ' arr[7]:=thearray[8];',
  27594. ' arr[low(arr)]:=high(thearray);',
  27595. ' arr:=arrint;',
  27596. ' arrInt:=tarrint(arr);',
  27597. ' if TheArray = nil then ;',
  27598. ' if nil = TheArray then ;',
  27599. ' if TheArray <> nil then ;',
  27600. ' if nil <> TheArray then ;',
  27601. '']);
  27602. ConvertProgram;
  27603. CheckSource('TestJSValue_ArrayOfJSValue',
  27604. LinesToStr([ // statements
  27605. 'this.v = undefined;',
  27606. 'this.TheArray = [1, "2"];',
  27607. 'this.Arr = [];',
  27608. 'this.i = 0;',
  27609. 'this.ArrInt = [];',
  27610. '']),
  27611. LinesToStr([ // $mod.$main
  27612. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27613. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27614. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27615. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27616. '$mod.Arr[4] = $mod.v;',
  27617. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27618. '$mod.Arr[6] = null;',
  27619. '$mod.Arr[7] = $mod.TheArray[8];',
  27620. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27621. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27622. '$mod.ArrInt = $mod.Arr;',
  27623. 'if (rtl.length($mod.TheArray) === 0) ;',
  27624. 'if (rtl.length($mod.TheArray) === 0) ;',
  27625. 'if (rtl.length($mod.TheArray) > 0) ;',
  27626. 'if (rtl.length($mod.TheArray) > 0) ;',
  27627. '']));
  27628. end;
  27629. procedure TTestModule.TestJSValue_ArrayLit;
  27630. begin
  27631. StartProgram(false);
  27632. Add([
  27633. 'type',
  27634. ' TFlag = (big,small);',
  27635. ' TArray = array of JSValue;',
  27636. ' TObject = class end;',
  27637. ' TClass = class of TObject;',
  27638. 'var',
  27639. ' v: jsvalue;',
  27640. ' a: TArray;',
  27641. ' o: TObject;',
  27642. 'begin',
  27643. ' a:=[];',
  27644. ' a:=[1];',
  27645. ' a:=[1,2];',
  27646. ' a:=[big];',
  27647. ' a:=[1,big];',
  27648. ' a:=[o,nil];',
  27649. '']);
  27650. ConvertProgram;
  27651. CheckSource('TestJSValue_ArrayLit',
  27652. LinesToStr([ // statements
  27653. 'this.TFlag = {',
  27654. ' "0": "big",',
  27655. ' big: 0,',
  27656. ' "1": "small",',
  27657. ' small: 1',
  27658. '};',
  27659. 'rtl.createClass(this, "TObject", null, function () {',
  27660. ' this.$init = function () {',
  27661. ' };',
  27662. ' this.$final = function () {',
  27663. ' };',
  27664. '});',
  27665. 'this.v = undefined;',
  27666. 'this.a = [];',
  27667. 'this.o = null;',
  27668. '']),
  27669. LinesToStr([ // $mod.$main
  27670. '$mod.a = [];',
  27671. '$mod.a = [1];',
  27672. '$mod.a = [1, 2];',
  27673. '$mod.a = [$mod.TFlag.big];',
  27674. '$mod.a = [1, $mod.TFlag.big];',
  27675. '$mod.a = [$mod.o, null];',
  27676. '']));
  27677. end;
  27678. procedure TTestModule.TestJSValue_Params;
  27679. begin
  27680. StartProgram(false);
  27681. Add('type');
  27682. Add(' integer = longint;');
  27683. Add(' TYesNo = boolean;');
  27684. Add(' TFloat = double;');
  27685. Add(' TCaption = string;');
  27686. Add(' TChar = char;');
  27687. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27688. Add('var');
  27689. Add(' l: jsvalue;');
  27690. Add('begin');
  27691. Add(' a:=a;');
  27692. Add(' l:=b;');
  27693. Add(' c:=c;');
  27694. Add(' d:=d;');
  27695. Add(' Result:=l;');
  27696. Add('end;');
  27697. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27698. Add('var');
  27699. Add(' v: jsvalue;');
  27700. Add(' i: integer;');
  27701. Add(' b: TYesNo;');
  27702. Add(' d: TFloat;');
  27703. Add(' s: TCaption;');
  27704. Add(' c: TChar;');
  27705. Add('begin');
  27706. Add(' v:=doit(v,v,v,v);');
  27707. Add(' i:=integer(dosome(i,i));');
  27708. Add(' b:=TYesNo(dosome(b,b));');
  27709. Add(' d:=TFloat(dosome(d,d));');
  27710. Add(' s:=TCaption(dosome(s,s));');
  27711. Add(' c:=TChar(dosome(c,c));');
  27712. ConvertProgram;
  27713. CheckSource('TestJSValue_Params',
  27714. LinesToStr([ // statements
  27715. 'this.DoIt = function (a, b, c, d) {',
  27716. ' var Result = undefined;',
  27717. ' var l = undefined;',
  27718. ' a = a;',
  27719. ' l = b;',
  27720. ' c.set(c.get());',
  27721. ' d.set(d.get());',
  27722. ' Result = l;',
  27723. ' return Result;',
  27724. '};',
  27725. 'this.DoSome = function (a, b) {',
  27726. ' var Result = undefined;',
  27727. ' return Result;',
  27728. '};',
  27729. 'this.v = undefined;',
  27730. 'this.i = 0;',
  27731. 'this.b = false;',
  27732. 'this.d = 0.0;',
  27733. 'this.s = "";',
  27734. 'this.c = "";',
  27735. '']),
  27736. LinesToStr([ // $mod.$main
  27737. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27738. ' p: $mod,',
  27739. ' get: function () {',
  27740. ' return this.p.v;',
  27741. ' },',
  27742. ' set: function (v) {',
  27743. ' this.p.v = v;',
  27744. ' }',
  27745. '}, {',
  27746. ' p: $mod,',
  27747. ' get: function () {',
  27748. ' return this.p.v;',
  27749. ' },',
  27750. ' set: function (v) {',
  27751. ' this.p.v = v;',
  27752. ' }',
  27753. '});',
  27754. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27755. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27756. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27757. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27758. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27759. '']));
  27760. end;
  27761. procedure TTestModule.TestJSValue_UntypedParam;
  27762. begin
  27763. StartProgram(false);
  27764. Add('function DoIt(const a; var b; out c): jsvalue;');
  27765. Add('begin');
  27766. Add(' Result:=a;');
  27767. Add(' Result:=b;');
  27768. Add(' Result:=c;');
  27769. Add(' b:=Result;');
  27770. Add(' c:=Result;');
  27771. Add('end;');
  27772. Add('var i: longint;');
  27773. Add('begin');
  27774. Add(' doit(i,i,i);');
  27775. ConvertProgram;
  27776. CheckSource('TestJSValue_UntypedParam',
  27777. LinesToStr([ // statements
  27778. 'this.DoIt = function (a, b, c) {',
  27779. ' var Result = undefined;',
  27780. ' Result = a;',
  27781. ' Result = b.get();',
  27782. ' Result = c.get();',
  27783. ' b.set(Result);',
  27784. ' c.set(Result);',
  27785. ' return Result;',
  27786. '};',
  27787. 'this.i = 0;',
  27788. '']),
  27789. LinesToStr([ // $mod.$main
  27790. '$mod.DoIt($mod.i, {',
  27791. ' p: $mod,',
  27792. ' get: function () {',
  27793. ' return this.p.i;',
  27794. ' },',
  27795. ' set: function (v) {',
  27796. ' this.p.i = v;',
  27797. ' }',
  27798. '}, {',
  27799. ' p: $mod,',
  27800. ' get: function () {',
  27801. ' return this.p.i;',
  27802. ' },',
  27803. ' set: function (v) {',
  27804. ' this.p.i = v;',
  27805. ' }',
  27806. '});',
  27807. '']));
  27808. end;
  27809. procedure TTestModule.TestJSValue_FuncResultType;
  27810. begin
  27811. StartProgram(false);
  27812. Add('type');
  27813. Add(' integer = longint;');
  27814. Add(' TJSValueArray = array of JSValue;');
  27815. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27816. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27817. Add('begin');
  27818. Add(' while Compare(P,aList[0])>0 do ;');
  27819. Add('end;');
  27820. Add('var');
  27821. Add(' Compare: TListSortCompare;');
  27822. Add(' V: JSValue;');
  27823. Add(' i: integer;');
  27824. Add('begin');
  27825. Add(' if Compare(V,V)>0 then ;');
  27826. Add(' if Compare(i,i)>1 then ;');
  27827. Add(' if Compare(nil,false)>2 then ;');
  27828. Add(' if Compare(1,true)>3 then ;');
  27829. ConvertProgram;
  27830. CheckSource('TestJSValue_UntypedParam',
  27831. LinesToStr([ // statements
  27832. 'this.Sort = function (P, aList, Compare) {',
  27833. ' while (Compare(P, aList[0]) > 0) {',
  27834. ' };',
  27835. '};',
  27836. 'this.Compare = null;',
  27837. 'this.V = undefined;',
  27838. 'this.i = 0;',
  27839. '']),
  27840. LinesToStr([ // $mod.$main
  27841. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27842. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27843. 'if ($mod.Compare(null, false) > 2) ;',
  27844. 'if ($mod.Compare(1, true) > 3) ;',
  27845. '']));
  27846. end;
  27847. procedure TTestModule.TestJSValue_ProcType_Assign;
  27848. begin
  27849. StartProgram(false);
  27850. Add('type');
  27851. Add(' integer = longint;');
  27852. Add(' TObject = class');
  27853. Add(' class function GetGlob: integer;');
  27854. Add(' function Getter: integer;');
  27855. Add(' end;');
  27856. Add('class function TObject.GetGlob: integer;');
  27857. Add('var v1: jsvalue;');
  27858. Add('begin');
  27859. Add(' v1:=@GetGlob;');
  27860. Add(' v1:[email protected];');
  27861. Add('end;');
  27862. Add('function TObject.Getter: integer;');
  27863. Add('var v2: jsvalue;');
  27864. Add('begin');
  27865. Add(' v2:=@Getter;');
  27866. Add(' v2:[email protected];');
  27867. Add(' v2:=@GetGlob;');
  27868. Add(' v2:[email protected];');
  27869. Add('end;');
  27870. Add('function GetIt(i: integer): integer;');
  27871. Add('var v3: jsvalue;');
  27872. Add('begin');
  27873. Add(' v3:=@GetIt;');
  27874. Add('end;');
  27875. Add('var');
  27876. Add(' V: JSValue;');
  27877. Add(' o: TObject;');
  27878. Add('begin');
  27879. Add(' v:=@GetIt;');
  27880. Add(' v:[email protected];');
  27881. Add(' v:[email protected];');
  27882. ConvertProgram;
  27883. CheckSource('TestJSValue_ProcType_Assign',
  27884. LinesToStr([ // statements
  27885. 'rtl.createClass(this, "TObject", null, function () {',
  27886. ' this.$init = function () {',
  27887. ' };',
  27888. ' this.$final = function () {',
  27889. ' };',
  27890. ' this.GetGlob = function () {',
  27891. ' var Result = 0;',
  27892. ' var v1 = undefined;',
  27893. ' v1 = rtl.createCallback(this, "GetGlob");',
  27894. ' v1 = rtl.createCallback(this, "GetGlob");',
  27895. ' return Result;',
  27896. ' };',
  27897. ' this.Getter = function () {',
  27898. ' var Result = 0;',
  27899. ' var v2 = undefined;',
  27900. ' v2 = rtl.createCallback(this, "Getter");',
  27901. ' v2 = rtl.createCallback(this, "Getter");',
  27902. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27903. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27904. ' return Result;',
  27905. ' };',
  27906. '});',
  27907. 'this.GetIt = function (i) {',
  27908. ' var Result = 0;',
  27909. ' var v3 = undefined;',
  27910. ' v3 = $mod.GetIt;',
  27911. ' return Result;',
  27912. '};',
  27913. 'this.V = undefined;',
  27914. 'this.o = null;',
  27915. '']),
  27916. LinesToStr([ // $mod.$main
  27917. '$mod.V = $mod.GetIt;',
  27918. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27919. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27920. '']));
  27921. end;
  27922. procedure TTestModule.TestJSValue_ProcType_Equal;
  27923. begin
  27924. StartProgram(false);
  27925. Add('type');
  27926. Add(' integer = longint;');
  27927. Add(' TObject = class');
  27928. Add(' class function GetGlob: integer;');
  27929. Add(' function Getter: integer;');
  27930. Add(' end;');
  27931. Add('class function TObject.GetGlob: integer;');
  27932. Add('var v1: jsvalue;');
  27933. Add('begin');
  27934. Add(' if v1=@GetGlob then;');
  27935. Add(' if [email protected] then ;');
  27936. Add('end;');
  27937. Add('function TObject.Getter: integer;');
  27938. Add('var v2: jsvalue;');
  27939. Add('begin');
  27940. Add(' if v2=@Getter then;');
  27941. Add(' if [email protected] then ;');
  27942. Add(' if v2=@GetGlob then;');
  27943. Add(' if [email protected] then;');
  27944. Add('end;');
  27945. Add('function GetIt(i: integer): integer;');
  27946. Add('var v3: jsvalue;');
  27947. Add('begin');
  27948. Add(' if v3=@GetIt then;');
  27949. Add('end;');
  27950. Add('var');
  27951. Add(' V: JSValue;');
  27952. Add(' o: TObject;');
  27953. Add('begin');
  27954. Add(' if v=@GetIt then;');
  27955. Add(' if [email protected] then;');
  27956. Add(' if [email protected] then;');
  27957. Add(' if @GetIt=v then;');
  27958. Add(' if @o.Getter=v then;');
  27959. Add(' if @o.GetGlob=v then;');
  27960. ConvertProgram;
  27961. CheckSource('TestJSValue_ProcType_Equal',
  27962. LinesToStr([ // statements
  27963. 'rtl.createClass(this, "TObject", null, function () {',
  27964. ' this.$init = function () {',
  27965. ' };',
  27966. ' this.$final = function () {',
  27967. ' };',
  27968. ' this.GetGlob = function () {',
  27969. ' var Result = 0;',
  27970. ' var v1 = undefined;',
  27971. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27972. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27973. ' return Result;',
  27974. ' };',
  27975. ' this.Getter = function () {',
  27976. ' var Result = 0;',
  27977. ' var v2 = undefined;',
  27978. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27979. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27980. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27981. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27982. ' return Result;',
  27983. ' };',
  27984. '});',
  27985. 'this.GetIt = function (i) {',
  27986. ' var Result = 0;',
  27987. ' var v3 = undefined;',
  27988. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27989. ' return Result;',
  27990. '};',
  27991. 'this.V = undefined;',
  27992. 'this.o = null;',
  27993. '']),
  27994. LinesToStr([ // $mod.$main
  27995. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27996. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27997. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27998. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27999. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28000. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28001. '']));
  28002. end;
  28003. procedure TTestModule.TestJSValue_ProcType_Param;
  28004. begin
  28005. StartProgram(false);
  28006. Add([
  28007. 'type',
  28008. ' variant = jsvalue;',
  28009. ' TArrVariant = array of variant;',
  28010. ' TArrVar2 = TArrVariant;',
  28011. ' TFuncInt = function: longint;',
  28012. 'function GetIt: longint;',
  28013. 'begin',
  28014. 'end;',
  28015. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28016. 'var v: variant;',
  28017. 'begin',
  28018. ' v:=arr[1];',
  28019. 'end;',
  28020. 'var s: string;',
  28021. 'begin',
  28022. ' DoIt(GetIt,[]);',
  28023. ' DoIt(@GetIt,[]);',
  28024. ' DoIt(1,[s,GetIt]);',
  28025. ' DoIt(1,[s,@GetIt]);',
  28026. '']);
  28027. ConvertProgram;
  28028. CheckSource('TestJSValue_ProcType_Param',
  28029. LinesToStr([ // statements
  28030. 'this.GetIt = function () {',
  28031. ' var Result = 0;',
  28032. ' return Result;',
  28033. '};',
  28034. 'this.DoIt = function (p, Arr) {',
  28035. ' var v = undefined;',
  28036. ' v = Arr[1];',
  28037. '};',
  28038. 'this.s = "";',
  28039. '']),
  28040. LinesToStr([ // $mod.$main
  28041. '$mod.DoIt($mod.GetIt(), []);',
  28042. '$mod.DoIt($mod.GetIt, []);',
  28043. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28044. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28045. '']));
  28046. end;
  28047. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28048. begin
  28049. StartProgram(false);
  28050. Add([
  28051. 'var',
  28052. ' v: JSValue;',
  28053. ' p: Pointer;',
  28054. 'begin',
  28055. ' p:=v;',
  28056. '']);
  28057. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28058. nIncompatibleTypesGotExpected);
  28059. ConvertProgram;
  28060. end;
  28061. procedure TTestModule.TestJSValue_OverloadDouble;
  28062. begin
  28063. StartProgram(false);
  28064. Add([
  28065. 'type',
  28066. ' integer = longint;',
  28067. ' tdatetime = double;',
  28068. 'procedure DoIt(d: double); begin end;',
  28069. 'procedure DoIt(v: jsvalue); begin end;',
  28070. 'var',
  28071. ' d: double;',
  28072. ' dt: tdatetime;',
  28073. ' i: integer;',
  28074. ' b: byte;',
  28075. ' shi: shortint;',
  28076. ' w: word;',
  28077. ' smi: smallint;',
  28078. ' lw: longword;',
  28079. ' li: longint;',
  28080. ' ni: nativeint;',
  28081. ' nu: nativeuint;',
  28082. 'begin',
  28083. ' DoIt(d);',
  28084. ' DoIt(dt);',
  28085. ' DoIt(i);',
  28086. ' DoIt(b);',
  28087. ' DoIt(shi);',
  28088. ' DoIt(w);',
  28089. ' DoIt(smi);',
  28090. ' DoIt(lw);',
  28091. ' DoIt(li);',
  28092. ' DoIt(ni);',
  28093. ' DoIt(nu);',
  28094. '']);
  28095. ConvertProgram;
  28096. CheckSource('TestJSValue_OverloadDouble',
  28097. LinesToStr([ // statements
  28098. 'this.DoIt = function (d) {',
  28099. '};',
  28100. 'this.DoIt$1 = function (v) {',
  28101. '};',
  28102. 'this.d = 0.0;',
  28103. 'this.dt = 0.0;',
  28104. 'this.i = 0;',
  28105. 'this.b = 0;',
  28106. 'this.shi = 0;',
  28107. 'this.w = 0;',
  28108. 'this.smi = 0;',
  28109. 'this.lw = 0;',
  28110. 'this.li = 0;',
  28111. 'this.ni = 0;',
  28112. 'this.nu = 0;',
  28113. '']),
  28114. LinesToStr([ // $mod.$main
  28115. '$mod.DoIt($mod.d);',
  28116. '$mod.DoIt($mod.dt);',
  28117. '$mod.DoIt$1($mod.i);',
  28118. '$mod.DoIt$1($mod.b);',
  28119. '$mod.DoIt$1($mod.shi);',
  28120. '$mod.DoIt$1($mod.w);',
  28121. '$mod.DoIt$1($mod.smi);',
  28122. '$mod.DoIt$1($mod.lw);',
  28123. '$mod.DoIt$1($mod.li);',
  28124. '$mod.DoIt$1($mod.ni);',
  28125. '$mod.DoIt$1($mod.nu);',
  28126. '']));
  28127. end;
  28128. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28129. begin
  28130. StartProgram(false);
  28131. Add([
  28132. 'type',
  28133. ' integer = longint;',
  28134. ' int53 = nativeint;',
  28135. ' tdatetime = double;',
  28136. 'procedure DoIt(n: nativeint); begin end;',
  28137. 'procedure DoIt(v: jsvalue); begin end;',
  28138. 'var',
  28139. ' d: double;',
  28140. ' dt: tdatetime;',
  28141. ' i: integer;',
  28142. ' b: byte;',
  28143. ' shi: shortint;',
  28144. ' w: word;',
  28145. ' smi: smallint;',
  28146. ' lw: longword;',
  28147. ' li: longint;',
  28148. ' ni: nativeint;',
  28149. ' nu: nativeuint;',
  28150. 'begin',
  28151. ' DoIt(d);',
  28152. ' DoIt(dt);',
  28153. ' DoIt(i);',
  28154. ' DoIt(b);',
  28155. ' DoIt(shi);',
  28156. ' DoIt(w);',
  28157. ' DoIt(smi);',
  28158. ' DoIt(lw);',
  28159. ' DoIt(li);',
  28160. ' DoIt(ni);',
  28161. ' DoIt(nu);',
  28162. '']);
  28163. ConvertProgram;
  28164. CheckSource('TestJSValue_OverloadNativeInt',
  28165. LinesToStr([ // statements
  28166. 'this.DoIt = function (n) {',
  28167. '};',
  28168. 'this.DoIt$1 = function (v) {',
  28169. '};',
  28170. 'this.d = 0.0;',
  28171. 'this.dt = 0.0;',
  28172. 'this.i = 0;',
  28173. 'this.b = 0;',
  28174. 'this.shi = 0;',
  28175. 'this.w = 0;',
  28176. 'this.smi = 0;',
  28177. 'this.lw = 0;',
  28178. 'this.li = 0;',
  28179. 'this.ni = 0;',
  28180. 'this.nu = 0;',
  28181. '']),
  28182. LinesToStr([ // $mod.$main
  28183. '$mod.DoIt$1($mod.d);',
  28184. '$mod.DoIt$1($mod.dt);',
  28185. '$mod.DoIt($mod.i);',
  28186. '$mod.DoIt($mod.b);',
  28187. '$mod.DoIt($mod.shi);',
  28188. '$mod.DoIt($mod.w);',
  28189. '$mod.DoIt($mod.smi);',
  28190. '$mod.DoIt($mod.lw);',
  28191. '$mod.DoIt($mod.li);',
  28192. '$mod.DoIt($mod.ni);',
  28193. '$mod.DoIt($mod.nu);',
  28194. '']));
  28195. end;
  28196. procedure TTestModule.TestJSValue_OverloadWord;
  28197. begin
  28198. StartProgram(false);
  28199. Add([
  28200. 'type',
  28201. ' integer = longint;',
  28202. ' int53 = nativeint;',
  28203. ' tdatetime = double;',
  28204. 'procedure DoIt(w: word); begin end;',
  28205. 'procedure DoIt(v: jsvalue); begin end;',
  28206. 'var',
  28207. ' d: double;',
  28208. ' dt: tdatetime;',
  28209. ' i: integer;',
  28210. ' b: byte;',
  28211. ' shi: shortint;',
  28212. ' w: word;',
  28213. ' smi: smallint;',
  28214. ' lw: longword;',
  28215. ' li: longint;',
  28216. ' ni: nativeint;',
  28217. ' nu: nativeuint;',
  28218. 'begin',
  28219. ' DoIt(d);',
  28220. ' DoIt(dt);',
  28221. ' DoIt(i);',
  28222. ' DoIt(b);',
  28223. ' DoIt(shi);',
  28224. ' DoIt(w);',
  28225. ' DoIt(smi);',
  28226. ' DoIt(lw);',
  28227. ' DoIt(li);',
  28228. ' DoIt(ni);',
  28229. ' DoIt(nu);',
  28230. '']);
  28231. ConvertProgram;
  28232. CheckSource('TestJSValue_OverloadWord',
  28233. LinesToStr([ // statements
  28234. 'this.DoIt = function (w) {',
  28235. '};',
  28236. 'this.DoIt$1 = function (v) {',
  28237. '};',
  28238. 'this.d = 0.0;',
  28239. 'this.dt = 0.0;',
  28240. 'this.i = 0;',
  28241. 'this.b = 0;',
  28242. 'this.shi = 0;',
  28243. 'this.w = 0;',
  28244. 'this.smi = 0;',
  28245. 'this.lw = 0;',
  28246. 'this.li = 0;',
  28247. 'this.ni = 0;',
  28248. 'this.nu = 0;',
  28249. '']),
  28250. LinesToStr([ // $mod.$main
  28251. '$mod.DoIt$1($mod.d);',
  28252. '$mod.DoIt$1($mod.dt);',
  28253. '$mod.DoIt$1($mod.i);',
  28254. '$mod.DoIt($mod.b);',
  28255. '$mod.DoIt($mod.shi);',
  28256. '$mod.DoIt($mod.w);',
  28257. '$mod.DoIt$1($mod.smi);',
  28258. '$mod.DoIt$1($mod.lw);',
  28259. '$mod.DoIt$1($mod.li);',
  28260. '$mod.DoIt$1($mod.ni);',
  28261. '$mod.DoIt$1($mod.nu);',
  28262. '']));
  28263. end;
  28264. procedure TTestModule.TestJSValue_OverloadString;
  28265. begin
  28266. StartProgram(false);
  28267. Add([
  28268. 'type',
  28269. ' uni = string;',
  28270. ' WChar = char;',
  28271. 'procedure DoIt(s: string); begin end;',
  28272. 'procedure DoIt(v: jsvalue); begin end;',
  28273. 'var',
  28274. ' s: string;',
  28275. ' c: char;',
  28276. ' u: uni;',
  28277. 'begin',
  28278. ' DoIt(s);',
  28279. ' DoIt(c);',
  28280. ' DoIt(u);',
  28281. '']);
  28282. ConvertProgram;
  28283. CheckSource('TestJSValue_OverloadString',
  28284. LinesToStr([ // statements
  28285. 'this.DoIt = function (s) {',
  28286. '};',
  28287. 'this.DoIt$1 = function (v) {',
  28288. '};',
  28289. 'this.s = "";',
  28290. 'this.c = "";',
  28291. 'this.u = "";',
  28292. '']),
  28293. LinesToStr([ // $mod.$main
  28294. '$mod.DoIt($mod.s);',
  28295. '$mod.DoIt($mod.c);',
  28296. '$mod.DoIt($mod.u);',
  28297. '']));
  28298. end;
  28299. procedure TTestModule.TestJSValue_OverloadChar;
  28300. begin
  28301. StartProgram(false);
  28302. Add([
  28303. 'type',
  28304. ' uni = string;',
  28305. ' WChar = char;',
  28306. 'procedure DoIt(c: char); begin end;',
  28307. 'procedure DoIt(v: jsvalue); begin end;',
  28308. 'var',
  28309. ' s: string;',
  28310. ' c: char;',
  28311. ' u: uni;',
  28312. 'begin',
  28313. ' DoIt(s);',
  28314. ' DoIt(c);',
  28315. ' DoIt(u);',
  28316. '']);
  28317. ConvertProgram;
  28318. CheckSource('TestJSValue_OverloadChar',
  28319. LinesToStr([ // statements
  28320. 'this.DoIt = function (c) {',
  28321. '};',
  28322. 'this.DoIt$1 = function (v) {',
  28323. '};',
  28324. 'this.s = "";',
  28325. 'this.c = "";',
  28326. 'this.u = "";',
  28327. '']),
  28328. LinesToStr([ // $mod.$main
  28329. '$mod.DoIt$1($mod.s);',
  28330. '$mod.DoIt($mod.c);',
  28331. '$mod.DoIt$1($mod.u);',
  28332. '']));
  28333. end;
  28334. procedure TTestModule.TestJSValue_OverloadPointer;
  28335. begin
  28336. StartProgram(false);
  28337. Add([
  28338. 'type',
  28339. ' TObject = class end;',
  28340. 'procedure DoIt(p: pointer); begin end;',
  28341. 'procedure DoIt(v: jsvalue); begin end;',
  28342. 'var',
  28343. ' o: TObject;',
  28344. 'begin',
  28345. ' DoIt(o);',
  28346. '']);
  28347. ConvertProgram;
  28348. CheckSource('TestJSValue_OverloadPointer',
  28349. LinesToStr([ // statements
  28350. 'rtl.createClass(this, "TObject", null, function () {',
  28351. ' this.$init = function () {',
  28352. ' };',
  28353. ' this.$final = function () {',
  28354. ' };',
  28355. '});',
  28356. 'this.DoIt = function (p) {',
  28357. '};',
  28358. 'this.DoIt$1 = function (v) {',
  28359. '};',
  28360. 'this.o = null;',
  28361. '']),
  28362. LinesToStr([ // $mod.$main
  28363. '$mod.DoIt($mod.o);',
  28364. '']));
  28365. end;
  28366. procedure TTestModule.TestJSValue_ForIn;
  28367. begin
  28368. StartProgram(false);
  28369. Add([
  28370. 'var',
  28371. ' v: JSValue;',
  28372. ' key: string;',
  28373. 'begin',
  28374. ' for key in v do begin',
  28375. ' if key=''abc'' then ;',
  28376. ' end;',
  28377. '']);
  28378. ConvertProgram;
  28379. CheckSource('TestJSValue_ForIn',
  28380. LinesToStr([ // statements
  28381. 'this.v = undefined;',
  28382. 'this.key = "";',
  28383. '']),
  28384. LinesToStr([ // $mod.$main
  28385. 'for ($mod.key in $mod.v) {',
  28386. ' if ($mod.key === "abc") ;',
  28387. '};',
  28388. '']));
  28389. end;
  28390. procedure TTestModule.TestRTTI_IntRange;
  28391. begin
  28392. WithTypeInfo:=true;
  28393. StartProgram(true,[supTypeInfo]);
  28394. Add([
  28395. '{$modeswitch externalclass}',
  28396. 'type',
  28397. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28398. ' TColor = type TGraphicsColor;',
  28399. 'var',
  28400. ' p: TTypeInfo;',
  28401. ' k: TTypeKind;',
  28402. 'begin',
  28403. ' p:=typeinfo(TGraphicsColor);',
  28404. ' p:=typeinfo(TColor);',
  28405. ' k:=GetTypeKind(TGraphicsColor);',
  28406. ' k:=GetTypeKind(TColor);',
  28407. '']);
  28408. ConvertProgram;
  28409. CheckSource('TestRTTI_IntRange',
  28410. LinesToStr([ // statements
  28411. 'this.$rtti.$Int("TGraphicsColor", {',
  28412. ' minvalue: -2147483648,',
  28413. ' maxvalue: 2147483647,',
  28414. ' ordtype: 4',
  28415. '});',
  28416. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28417. 'this.p = null;',
  28418. 'this.k = 0;',
  28419. '']),
  28420. LinesToStr([ // $mod.$main
  28421. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28422. '$mod.p = $mod.$rtti["TColor"];',
  28423. '$mod.k = 1;',
  28424. '$mod.k = 1;',
  28425. '']));
  28426. end;
  28427. procedure TTestModule.TestRTTI_Double;
  28428. begin
  28429. WithTypeInfo:=true;
  28430. StartProgram(true,[supTypeInfo]);
  28431. Add([
  28432. '{$modeswitch externalclass}',
  28433. 'type',
  28434. ' TFloat = type double;',
  28435. 'var',
  28436. ' p: TTypeInfo;',
  28437. 'begin',
  28438. ' p:=typeinfo(double);',
  28439. ' p:=typeinfo(TFloat);',
  28440. '']);
  28441. ConvertProgram;
  28442. CheckSource('TestRTTI_Double',
  28443. LinesToStr([ // statements
  28444. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28445. 'this.p = null;',
  28446. '']),
  28447. LinesToStr([ // $mod.$main
  28448. '$mod.p = rtl.double;',
  28449. '$mod.p = $mod.$rtti["TFloat"];',
  28450. '']));
  28451. end;
  28452. procedure TTestModule.TestRTTI_ProcType;
  28453. begin
  28454. WithTypeInfo:=true;
  28455. StartProgram(false);
  28456. Add('type');
  28457. Add(' TProcA = procedure;');
  28458. Add(' TMethodB = procedure of object;');
  28459. Add(' TProcC = procedure; varargs;');
  28460. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28461. Add(' TProcE = function: nativeint;');
  28462. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28463. Add('var p: pointer;');
  28464. Add('begin');
  28465. Add(' p:=typeinfo(tproca);');
  28466. ConvertProgram;
  28467. CheckSource('TestRTTI_ProcType',
  28468. LinesToStr([ // statements
  28469. 'this.$rtti.$ProcVar("TProcA", {',
  28470. ' procsig: rtl.newTIProcSig(null)',
  28471. '});',
  28472. 'this.$rtti.$MethodVar("TMethodB", {',
  28473. ' procsig: rtl.newTIProcSig(null),',
  28474. ' methodkind: 0',
  28475. '});',
  28476. 'this.$rtti.$ProcVar("TProcC", {',
  28477. ' procsig: rtl.newTIProcSig(null, 2)',
  28478. '});',
  28479. 'this.$rtti.$ProcVar("TProcD", {',
  28480. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28481. '});',
  28482. 'this.$rtti.$ProcVar("TProcE", {',
  28483. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28484. '});',
  28485. 'this.$rtti.$ProcVar("TProcF", {',
  28486. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28487. '});',
  28488. 'this.p = null;',
  28489. '']),
  28490. LinesToStr([ // $mod.$main
  28491. '$mod.p = $mod.$rtti["TProcA"];',
  28492. '']));
  28493. end;
  28494. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28495. begin
  28496. WithTypeInfo:=true;
  28497. AddModuleWithIntfImplSrc('unit2.pas',
  28498. LinesToStr([
  28499. 'type',
  28500. ' TObject = class end;'
  28501. ]),
  28502. '');
  28503. StartUnit(true);
  28504. Add('interface');
  28505. Add('uses unit2;');
  28506. Add('type');
  28507. Add(' TProcA = function(o: tobject): tobject;');
  28508. Add('implementation');
  28509. Add('type');
  28510. Add(' TProcB = function(o: tobject): tobject;');
  28511. Add('var p: Pointer;');
  28512. Add('initialization');
  28513. Add(' p:=typeinfo(tproca);');
  28514. Add(' p:=typeinfo(tprocb);');
  28515. ConvertUnit;
  28516. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28517. LinesToStr([ // statements
  28518. 'var $impl = $mod.$impl;',
  28519. 'this.$rtti.$ProcVar("TProcA", {',
  28520. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28521. '});',
  28522. '']),
  28523. LinesToStr([ // this.$init
  28524. '$impl.p = $mod.$rtti["TProcA"];',
  28525. '$impl.p = $mod.$rtti["TProcB"];',
  28526. '']),
  28527. LinesToStr([ // implementation
  28528. '$mod.$rtti.$ProcVar("TProcB", {',
  28529. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28530. '});',
  28531. '$impl.p = null;',
  28532. '']) );
  28533. end;
  28534. procedure TTestModule.TestRTTI_EnumAndSetType;
  28535. begin
  28536. WithTypeInfo:=true;
  28537. StartProgram(false);
  28538. Add('type');
  28539. Add(' TFlag = (light,dark);');
  28540. Add(' TFlags = set of TFlag;');
  28541. Add(' TProc = function(f: TFlags): TFlag;');
  28542. Add('var p: pointer;');
  28543. Add('begin');
  28544. Add(' p:=typeinfo(tflag);');
  28545. Add(' p:=typeinfo(tflags);');
  28546. ConvertProgram;
  28547. CheckSource('TestRTTI_EnumAndType',
  28548. LinesToStr([ // statements
  28549. 'this.TFlag = {',
  28550. ' "0": "light",',
  28551. ' light: 0,',
  28552. ' "1": "dark",',
  28553. ' dark: 1',
  28554. '};',
  28555. 'this.$rtti.$Enum("TFlag", {',
  28556. ' minvalue: 0,',
  28557. ' maxvalue: 1,',
  28558. ' ordtype: 1,',
  28559. ' enumtype: this.TFlag',
  28560. '});',
  28561. 'this.$rtti.$Set("TFlags", {',
  28562. ' comptype: this.$rtti["TFlag"]',
  28563. '});',
  28564. 'this.$rtti.$ProcVar("TProc", {',
  28565. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28566. '});',
  28567. 'this.p = null;',
  28568. '']),
  28569. LinesToStr([ // $mod.$main
  28570. '$mod.p = $mod.$rtti["TFlag"];',
  28571. '$mod.p = $mod.$rtti["TFlags"];',
  28572. '']));
  28573. end;
  28574. procedure TTestModule.TestRTTI_EnumRange;
  28575. begin
  28576. WithTypeInfo:=true;
  28577. StartProgram(false);
  28578. Add([
  28579. 'type',
  28580. ' TCol = (red,green,blue);',
  28581. ' TColRg = green..blue;',
  28582. ' TSetOfColRg = set of TColRg;',
  28583. 'var p: pointer;',
  28584. 'begin',
  28585. ' p:=typeinfo(tcolrg);',
  28586. ' p:=typeinfo(tsetofcolrg);',
  28587. '']);
  28588. ConvertProgram;
  28589. end;
  28590. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28591. begin
  28592. WithTypeInfo:=true;
  28593. StartProgram(false);
  28594. Add('type');
  28595. Add(' TFlags = set of (red, green);');
  28596. Add('var');
  28597. Add(' f: TFlags;');
  28598. Add('begin');
  28599. Add(' Include(f,red);');
  28600. ConvertProgram;
  28601. CheckSource('TestRTTI_AnonymousEnumType',
  28602. LinesToStr([ // statements
  28603. 'this.TFlags$a = {',
  28604. ' "0": "red",',
  28605. ' red: 0,',
  28606. ' "1": "green",',
  28607. ' green: 1',
  28608. '};',
  28609. 'this.$rtti.$Enum("TFlags$a", {',
  28610. ' minvalue: 0,',
  28611. ' maxvalue: 1,',
  28612. ' ordtype: 1,',
  28613. ' enumtype: this.TFlags$a',
  28614. '});',
  28615. 'this.$rtti.$Set("TFlags", {',
  28616. ' comptype: this.$rtti["TFlags$a"]',
  28617. '});',
  28618. 'this.f = {};',
  28619. '']),
  28620. LinesToStr([
  28621. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28622. '']));
  28623. end;
  28624. procedure TTestModule.TestRTTI_StaticArray;
  28625. begin
  28626. WithTypeInfo:=true;
  28627. StartProgram(false);
  28628. Add('type');
  28629. Add(' TFlag = (light,dark);');
  28630. Add(' TFlagNames = array[TFlag] of string;');
  28631. Add(' TBoolNames = array[boolean] of string;');
  28632. Add(' TByteArray = array[1..32768] of byte;');
  28633. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28634. Add('var p: pointer;');
  28635. Add('begin');
  28636. Add(' p:=typeinfo(TFlagNames);');
  28637. Add(' p:=typeinfo(TBoolNames);');
  28638. ConvertProgram;
  28639. CheckSource('TestRTTI_StaticArray',
  28640. LinesToStr([ // statements
  28641. 'this.TFlag = {',
  28642. ' "0": "light",',
  28643. ' light: 0,',
  28644. ' "1": "dark",',
  28645. ' dark: 1',
  28646. '};',
  28647. 'this.$rtti.$Enum("TFlag", {',
  28648. ' minvalue: 0,',
  28649. ' maxvalue: 1,',
  28650. ' ordtype: 1,',
  28651. ' enumtype: this.TFlag',
  28652. '});',
  28653. 'this.$rtti.$StaticArray("TFlagNames", {',
  28654. ' dims: [2],',
  28655. ' eltype: rtl.string',
  28656. '});',
  28657. 'this.$rtti.$StaticArray("TBoolNames", {',
  28658. ' dims: [2],',
  28659. ' eltype: rtl.string',
  28660. '});',
  28661. 'this.$rtti.$StaticArray("TByteArray", {',
  28662. ' dims: [32768],',
  28663. ' eltype: rtl.byte',
  28664. '});',
  28665. 'this.$rtti.$ProcVar("TProc", {',
  28666. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28667. '});',
  28668. 'this.p = null;',
  28669. '']),
  28670. LinesToStr([ // $mod.$main
  28671. '$mod.p = $mod.$rtti["TFlagNames"];',
  28672. '$mod.p = $mod.$rtti["TBoolNames"];',
  28673. '']));
  28674. end;
  28675. procedure TTestModule.TestRTTI_DynArray;
  28676. begin
  28677. WithTypeInfo:=true;
  28678. StartProgram(false);
  28679. Add('type');
  28680. Add(' TArrStr = array of string;');
  28681. Add(' TArr2Dim = array of tarrstr;');
  28682. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28683. Add('var p: pointer;');
  28684. Add('begin');
  28685. Add(' p:=typeinfo(tarrstr);');
  28686. Add(' p:=typeinfo(tarr2dim);');
  28687. ConvertProgram;
  28688. CheckSource('TestRTTI_DynArray',
  28689. LinesToStr([ // statements
  28690. 'this.$rtti.$DynArray("TArrStr", {',
  28691. ' eltype: rtl.string',
  28692. '});',
  28693. 'this.$rtti.$DynArray("TArr2Dim", {',
  28694. ' eltype: this.$rtti["TArrStr"]',
  28695. '});',
  28696. 'this.$rtti.$ProcVar("TProc", {',
  28697. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28698. '});',
  28699. 'this.p = null;',
  28700. '']),
  28701. LinesToStr([ // $mod.$main
  28702. '$mod.p = $mod.$rtti["TArrStr"];',
  28703. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28704. '']));
  28705. end;
  28706. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28707. begin
  28708. WithTypeInfo:=true;
  28709. StartProgram(false);
  28710. Add('type');
  28711. Add(' TArr = array of array of longint;');
  28712. Add('var a: TArr;');
  28713. Add('begin');
  28714. ConvertProgram;
  28715. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28716. LinesToStr([ // statements
  28717. 'this.$rtti.$DynArray("TArr$a", {',
  28718. ' eltype: rtl.longint',
  28719. '});',
  28720. 'this.$rtti.$DynArray("TArr", {',
  28721. ' eltype: this.$rtti["TArr$a"]',
  28722. '});',
  28723. 'this.a = [];',
  28724. '']),
  28725. LinesToStr([ // $mod.$main
  28726. ]));
  28727. end;
  28728. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28729. begin
  28730. WithTypeInfo:=true;
  28731. StartProgram(false);
  28732. Add('type');
  28733. Add(' TObject = class');
  28734. Add(' published');
  28735. Add(' procedure Proc; virtual; abstract;');
  28736. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28737. Add(' end;');
  28738. Add('begin');
  28739. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28740. nDuplicatePublishedMethodXAtY);
  28741. ConvertProgram;
  28742. end;
  28743. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28744. begin
  28745. WithTypeInfo:=true;
  28746. StartProgram(false);
  28747. Add('type');
  28748. Add(' TObject = class');
  28749. Add(' published');
  28750. Add(' procedure Proc; external name ''foo'';');
  28751. Add(' end;');
  28752. Add('begin');
  28753. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28754. nPublishedNameMustMatchExternal);
  28755. ConvertProgram;
  28756. end;
  28757. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28758. begin
  28759. WithTypeInfo:=true;
  28760. StartProgram(false);
  28761. Add('type');
  28762. Add(' TObject = class');
  28763. Add(' class var FA: longint;');
  28764. Add(' published');
  28765. Add(' class property A: longint read FA;');
  28766. Add(' end;');
  28767. Add('begin');
  28768. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28769. nInvalidXModifierY);
  28770. ConvertProgram;
  28771. end;
  28772. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28773. begin
  28774. WithTypeInfo:=true;
  28775. StartProgram(false);
  28776. Add('type');
  28777. Add(' TObject = class');
  28778. Add(' published');
  28779. Add(' class var FA: longint;');
  28780. Add(' end;');
  28781. Add('begin');
  28782. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28783. nSymbolCannotBePublished);
  28784. ConvertProgram;
  28785. end;
  28786. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28787. begin
  28788. WithTypeInfo:=true;
  28789. StartProgram(false);
  28790. Add('{$modeswitch externalclass}');
  28791. Add('type');
  28792. Add(' TObject = class');
  28793. Add(' published');
  28794. Add(' V: longint; external name ''foo'';');
  28795. Add(' end;');
  28796. Add('begin');
  28797. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28798. nPublishedNameMustMatchExternal);
  28799. ConvertProgram;
  28800. end;
  28801. procedure TTestModule.TestRTTI_Class_Field;
  28802. begin
  28803. WithTypeInfo:=true;
  28804. StartProgram(false);
  28805. Add('{$modeswitch externalclass}');
  28806. Add('type');
  28807. Add(' TObject = class');
  28808. Add(' private');
  28809. Add(' FPropA: string;');
  28810. Add(' published');
  28811. Add(' VarLI: longint;');
  28812. Add(' VarC: char;');
  28813. Add(' VarS: string;');
  28814. Add(' VarD: double;');
  28815. Add(' VarB: boolean;');
  28816. Add(' VarLW: longword;');
  28817. Add(' VarSmI: smallint;');
  28818. Add(' VarW: word;');
  28819. Add(' VarShI: shortint;');
  28820. Add(' VarBy: byte;');
  28821. Add(' VarExt: longint external name ''VarExt'';');
  28822. Add(' ArrA, ArrB: array of byte;');
  28823. Add(' end;');
  28824. Add('var p: pointer;');
  28825. Add(' Obj: tobject;');
  28826. Add('begin');
  28827. Add(' p:=typeinfo(tobject);');
  28828. Add(' p:=typeinfo(p);');
  28829. Add(' p:=typeinfo(obj);');
  28830. ConvertProgram;
  28831. CheckSource('TestRTTI_Class_Field',
  28832. LinesToStr([ // statements
  28833. 'rtl.createClass(this, "TObject", null, function () {',
  28834. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28835. ' eltype: rtl.byte',
  28836. ' });',
  28837. ' this.$init = function () {',
  28838. ' this.FPropA = "";',
  28839. ' this.VarLI = 0;',
  28840. ' this.VarC = "";',
  28841. ' this.VarS = "";',
  28842. ' this.VarD = 0.0;',
  28843. ' this.VarB = false;',
  28844. ' this.VarLW = 0;',
  28845. ' this.VarSmI = 0;',
  28846. ' this.VarW = 0;',
  28847. ' this.VarShI = 0;',
  28848. ' this.VarBy = 0;',
  28849. ' this.ArrA = [];',
  28850. ' this.ArrB = [];',
  28851. ' };',
  28852. ' this.$final = function () {',
  28853. ' this.ArrA = undefined;',
  28854. ' this.ArrB = undefined;',
  28855. ' };',
  28856. ' var $r = this.$rtti;',
  28857. ' $r.addField("VarLI", rtl.longint);',
  28858. ' $r.addField("VarC", rtl.char);',
  28859. ' $r.addField("VarS", rtl.string);',
  28860. ' $r.addField("VarD", rtl.double);',
  28861. ' $r.addField("VarB", rtl.boolean);',
  28862. ' $r.addField("VarLW", rtl.longword);',
  28863. ' $r.addField("VarSmI", rtl.smallint);',
  28864. ' $r.addField("VarW", rtl.word);',
  28865. ' $r.addField("VarShI", rtl.shortint);',
  28866. ' $r.addField("VarBy", rtl.byte);',
  28867. ' $r.addField("VarExt", rtl.longint);',
  28868. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28869. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28870. '});',
  28871. 'this.p = null;',
  28872. 'this.Obj = null;',
  28873. '']),
  28874. LinesToStr([ // $mod.$main
  28875. '$mod.p = $mod.$rtti["TObject"];',
  28876. '$mod.p = rtl.pointer;',
  28877. '$mod.p = $mod.Obj.$rtti;',
  28878. '']));
  28879. end;
  28880. procedure TTestModule.TestRTTI_Class_Method;
  28881. begin
  28882. WithTypeInfo:=true;
  28883. StartProgram(false);
  28884. Add('type');
  28885. Add(' TObject = class');
  28886. Add(' private');
  28887. Add(' procedure Internal; external name ''$intern'';');
  28888. Add(' published');
  28889. Add(' procedure Click; virtual; abstract;');
  28890. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28891. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28892. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28893. Add(' end;');
  28894. Add('begin');
  28895. ConvertProgram;
  28896. CheckSource('TestRTTI_Class_Method',
  28897. LinesToStr([ // statements
  28898. 'rtl.createClass(this, "TObject", null, function () {',
  28899. ' this.$init = function () {',
  28900. ' };',
  28901. ' this.$final = function () {',
  28902. ' };',
  28903. ' var $r = this.$rtti;',
  28904. ' $r.addMethod("Click", 0, null);',
  28905. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28906. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28907. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28908. ' flags: 2',
  28909. ' });',
  28910. '});',
  28911. '']),
  28912. LinesToStr([ // $mod.$main
  28913. '']));
  28914. end;
  28915. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28916. begin
  28917. WithTypeInfo:=true;
  28918. StartProgram(false);
  28919. Add('type');
  28920. Add(' TObject = class');
  28921. Add(' published');
  28922. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28923. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28924. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28925. Add(' end;');
  28926. Add('begin');
  28927. ConvertProgram;
  28928. CheckSource('TestRTTI_Class_MethodOpenArray',
  28929. LinesToStr([ // statements
  28930. 'rtl.createClass(this, "TObject", null, function () {',
  28931. ' this.$init = function () {',
  28932. ' };',
  28933. ' this.$final = function () {',
  28934. ' };',
  28935. ' var $r = this.$rtti;',
  28936. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28937. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28938. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28939. '});',
  28940. '']),
  28941. LinesToStr([ // $mod.$main
  28942. '']));
  28943. end;
  28944. procedure TTestModule.TestRTTI_Class_Property;
  28945. begin
  28946. WithTypeInfo:=true;
  28947. StartProgram(false);
  28948. Add('{$modeswitch externalclass}');
  28949. Add('type');
  28950. Add(' TObject = class');
  28951. Add(' private');
  28952. Add(' FColor: longint;');
  28953. Add(' FColorStored: boolean;');
  28954. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28955. Add(' function GetColor: longint; virtual; abstract;');
  28956. Add(' function GetColorStored: boolean; virtual; abstract;');
  28957. Add(' FExtSize: longint external name ''$extSize'';');
  28958. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28959. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28960. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28961. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28962. Add(' published');
  28963. Add(' property ColorA: longint read FColor;');
  28964. Add(' property ColorB: longint write FColor;');
  28965. Add(' property ColorC: longint read GetColor write SetColor;');
  28966. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28967. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28968. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28969. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28970. Add(' end;');
  28971. Add('begin');
  28972. ConvertProgram;
  28973. CheckSource('TestRTTI_Class_Property',
  28974. LinesToStr([ // statements
  28975. 'rtl.createClass(this, "TObject", null, function () {',
  28976. ' this.$init = function () {',
  28977. ' this.FColor = 0;',
  28978. ' this.FColorStored = false;',
  28979. ' };',
  28980. ' this.$final = function () {',
  28981. ' };',
  28982. ' var $r = this.$rtti;',
  28983. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28984. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28985. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28986. ' $r.addProperty(',
  28987. ' "ColorD",',
  28988. ' 8,',
  28989. ' rtl.longint,',
  28990. ' "FColor",',
  28991. ' "FColor",',
  28992. ' {',
  28993. ' stored: "FColorStored"',
  28994. ' }',
  28995. ' );',
  28996. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28997. ' $r.addProperty(',
  28998. ' "ExtSizeB",',
  28999. ' 11,',
  29000. ' rtl.longint,',
  29001. ' "$getSize",',
  29002. ' "$setSize",',
  29003. ' {',
  29004. ' stored: "$extSizeStored"',
  29005. ' }',
  29006. ' );',
  29007. ' $r.addProperty(',
  29008. ' "ExtSizeC",',
  29009. ' 12,',
  29010. ' rtl.longint,',
  29011. ' "$extSize",',
  29012. ' "$extSize",',
  29013. ' {',
  29014. ' stored: "$getExtSizeStored"',
  29015. ' }',
  29016. ' );',
  29017. '});',
  29018. '']),
  29019. LinesToStr([ // $mod.$main
  29020. '']));
  29021. end;
  29022. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29023. begin
  29024. WithTypeInfo:=true;
  29025. StartProgram(false);
  29026. Add('{$modeswitch externalclass}');
  29027. Add('type');
  29028. Add(' integer = longint;');
  29029. Add(' TObject = class');
  29030. Add(' private');
  29031. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29032. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29033. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29034. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29035. Add(' published');
  29036. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29037. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29038. Add(' end;');
  29039. Add('begin');
  29040. ConvertProgram;
  29041. CheckSource('TestRTTI_Class_PropertyParams',
  29042. LinesToStr([ // statements
  29043. 'rtl.createClass(this, "TObject", null, function () {',
  29044. ' this.$init = function () {',
  29045. ' };',
  29046. ' this.$final = function () {',
  29047. ' };',
  29048. ' var $r = this.$rtti;',
  29049. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29050. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29051. '});',
  29052. '']),
  29053. LinesToStr([ // $mod.$main
  29054. '']));
  29055. end;
  29056. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29057. begin
  29058. WithTypeInfo:=true;
  29059. AddModuleWithIntfImplSrc('unit1.pas',
  29060. 'type TColor = -5..5;',
  29061. '');
  29062. StartProgram(true);
  29063. Add([
  29064. 'uses unit1;',
  29065. 'type',
  29066. ' TColorAlias = TColor;',
  29067. ' TColorTypeAlias = type TColor;',
  29068. ' TObject = class',
  29069. ' private',
  29070. ' fColor: TColor;',
  29071. ' fAlias: TColorAlias;',
  29072. ' fTypeAlias: TColorTypeAlias;',
  29073. ' published',
  29074. ' property Color: TColor read fcolor;',
  29075. ' property Alias: TColorAlias read falias;',
  29076. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29077. ' end;',
  29078. 'begin',
  29079. '']);
  29080. ConvertProgram;
  29081. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29082. LinesToStr([ // statements
  29083. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29084. 'rtl.createClass(this, "TObject", null, function () {',
  29085. ' this.$init = function () {',
  29086. ' this.fColor = 0;',
  29087. ' this.fAlias = 0;',
  29088. ' this.fTypeAlias = 0;',
  29089. ' };',
  29090. ' this.$final = function () {',
  29091. ' };',
  29092. ' var $r = this.$rtti;',
  29093. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29094. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29095. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29096. '});',
  29097. '']),
  29098. LinesToStr([ // $mod.$main
  29099. '']));
  29100. end;
  29101. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29102. begin
  29103. WithTypeInfo:=true;
  29104. StartProgram(false);
  29105. Add([
  29106. '{$modeswitch omitrtti}',
  29107. 'type',
  29108. ' TObject = class',
  29109. ' private',
  29110. ' FA: byte;',
  29111. ' published',
  29112. ' property A: byte read FA write FA;',
  29113. ' end;',
  29114. 'begin']);
  29115. ConvertProgram;
  29116. CheckSource('TestRTTI_Class_OmitRTTI',
  29117. LinesToStr([ // statements
  29118. 'rtl.createClass(this, "TObject", null, function () {',
  29119. ' this.$init = function () {',
  29120. ' this.FA = 0;',
  29121. ' };',
  29122. ' this.$final = function () {',
  29123. ' };',
  29124. '});',
  29125. '']),
  29126. LinesToStr([ // $mod.$main
  29127. '']));
  29128. end;
  29129. procedure TTestModule.TestRTTI_IndexModifier;
  29130. begin
  29131. WithTypeInfo:=true;
  29132. StartProgram(false);
  29133. Add([
  29134. 'type',
  29135. ' TEnum = (red, blue);',
  29136. ' TObject = class',
  29137. ' FB: boolean;',
  29138. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29139. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29140. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29141. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29142. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29143. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29144. ' published',
  29145. ' property B1: boolean index 1 read FB write SetIntBool;',
  29146. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29147. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29148. ' end;',
  29149. 'begin']);
  29150. ConvertProgram;
  29151. CheckSource('TestRTTI_IndexModifier',
  29152. LinesToStr([ // statements
  29153. 'this.TEnum = {',
  29154. ' "0": "red",',
  29155. ' red: 0,',
  29156. ' "1": "blue",',
  29157. ' blue: 1',
  29158. '};',
  29159. 'this.$rtti.$Enum("TEnum", {',
  29160. ' minvalue: 0,',
  29161. ' maxvalue: 1,',
  29162. ' ordtype: 1,',
  29163. ' enumtype: this.TEnum',
  29164. '});',
  29165. 'rtl.createClass(this, "TObject", null, function () {',
  29166. ' this.$init = function () {',
  29167. ' this.FB = false;',
  29168. ' };',
  29169. ' this.$final = function () {',
  29170. ' };',
  29171. ' var $r = this.$rtti;',
  29172. ' $r.addProperty(',
  29173. ' "B1",',
  29174. ' 18,',
  29175. ' rtl.boolean,',
  29176. ' "FB",',
  29177. ' "SetIntBool",',
  29178. ' {',
  29179. ' index: 1',
  29180. ' }',
  29181. ' );',
  29182. ' $r.addProperty(',
  29183. ' "B2",',
  29184. ' 17,',
  29185. ' rtl.boolean,',
  29186. ' "GetEnumBool",',
  29187. ' "FB",',
  29188. ' {',
  29189. ' index: $mod.TEnum.blue',
  29190. ' }',
  29191. ' );',
  29192. ' $r.addProperty(',
  29193. ' "I1",',
  29194. ' 19,',
  29195. ' rtl.boolean,',
  29196. ' "GetStrIntBool",',
  29197. ' "SetStrIntBool",',
  29198. ' {',
  29199. ' index: 2',
  29200. ' }',
  29201. ' );',
  29202. '});',
  29203. '']),
  29204. LinesToStr([ // $mod.$main
  29205. '']));
  29206. end;
  29207. procedure TTestModule.TestRTTI_StoredModifier;
  29208. begin
  29209. WithTypeInfo:=true;
  29210. StartProgram(false);
  29211. Add([
  29212. 'const',
  29213. ' ConstB = true;',
  29214. 'type',
  29215. ' TObject = class',
  29216. ' private',
  29217. ' FB: boolean;',
  29218. ' function IsBStored: boolean; virtual; abstract;',
  29219. ' published',
  29220. ' property BoolA: boolean read FB stored true;',
  29221. ' property BoolB: boolean read FB stored false;',
  29222. ' property BoolC: boolean read FB stored FB;',
  29223. ' property BoolD: boolean read FB stored ConstB;',
  29224. ' property BoolE: boolean read FB stored IsBStored;',
  29225. ' end;',
  29226. 'begin']);
  29227. ConvertProgram;
  29228. CheckSource('TestRTTI_StoredModifier',
  29229. LinesToStr([ // statements
  29230. 'this.ConstB = true;',
  29231. 'rtl.createClass(this, "TObject", null, function () {',
  29232. ' this.$init = function () {',
  29233. ' this.FB = false;',
  29234. ' };',
  29235. ' this.$final = function () {',
  29236. ' };',
  29237. ' var $r = this.$rtti;',
  29238. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29239. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29240. ' $r.addProperty(',
  29241. ' "BoolC",',
  29242. ' 8,',
  29243. ' rtl.boolean,',
  29244. ' "FB",',
  29245. ' "",',
  29246. ' {',
  29247. ' stored: "FB"',
  29248. ' }',
  29249. ' );',
  29250. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29251. ' $r.addProperty(',
  29252. ' "BoolE",',
  29253. ' 12,',
  29254. ' rtl.boolean,',
  29255. ' "FB",',
  29256. ' "",',
  29257. ' {',
  29258. ' stored: "IsBStored"',
  29259. ' }',
  29260. ' );',
  29261. '});',
  29262. '']),
  29263. LinesToStr([ // $mod.$main
  29264. '']));
  29265. end;
  29266. procedure TTestModule.TestRTTI_DefaultValue;
  29267. begin
  29268. WithTypeInfo:=true;
  29269. StartProgram(false);
  29270. Add([
  29271. 'type',
  29272. ' TEnum = (red, blue);',
  29273. 'const',
  29274. ' CB = true or false;',
  29275. ' CI = 1+2;',
  29276. 'type',
  29277. ' TObject = class',
  29278. ' FB: boolean;',
  29279. ' FI: longint;',
  29280. ' FE: TEnum;',
  29281. ' published',
  29282. ' property B1: boolean read FB default true;',
  29283. ' property B2: boolean read FB default CB;',
  29284. ' property B3: boolean read FB default test1.cb;',
  29285. ' property I1: longint read FI default 2;',
  29286. ' property I2: longint read FI default CI;',
  29287. ' property E1: TEnum read FE default red;',
  29288. ' property E2: TEnum read FE default TEnum.blue;',
  29289. ' end;',
  29290. 'begin']);
  29291. ConvertProgram;
  29292. CheckSource('TestRTTI_DefaultValue',
  29293. LinesToStr([ // statements
  29294. 'this.TEnum = {',
  29295. ' "0": "red",',
  29296. ' red: 0,',
  29297. ' "1": "blue",',
  29298. ' blue: 1',
  29299. '};',
  29300. 'this.$rtti.$Enum("TEnum", {',
  29301. ' minvalue: 0,',
  29302. ' maxvalue: 1,',
  29303. ' ordtype: 1,',
  29304. ' enumtype: this.TEnum',
  29305. '});',
  29306. 'this.CB = true || false;',
  29307. 'this.CI = 1 + 2;',
  29308. 'rtl.createClass(this, "TObject", null, function () {',
  29309. ' this.$init = function () {',
  29310. ' this.FB = false;',
  29311. ' this.FI = 0;',
  29312. ' this.FE = 0;',
  29313. ' };',
  29314. ' this.$final = function () {',
  29315. ' };',
  29316. ' var $r = this.$rtti;',
  29317. ' $r.addProperty(',
  29318. ' "B1",',
  29319. ' 0,',
  29320. ' rtl.boolean,',
  29321. ' "FB",',
  29322. ' "",',
  29323. ' {',
  29324. ' Default: true',
  29325. ' }',
  29326. ' );',
  29327. ' $r.addProperty(',
  29328. ' "B2",',
  29329. ' 0,',
  29330. ' rtl.boolean,',
  29331. ' "FB",',
  29332. ' "",',
  29333. ' {',
  29334. ' Default: true',
  29335. ' }',
  29336. ' );',
  29337. ' $r.addProperty(',
  29338. ' "B3",',
  29339. ' 0,',
  29340. ' rtl.boolean,',
  29341. ' "FB",',
  29342. ' "",',
  29343. ' {',
  29344. ' Default: true',
  29345. ' }',
  29346. ' );',
  29347. ' $r.addProperty(',
  29348. ' "I1",',
  29349. ' 0,',
  29350. ' rtl.longint,',
  29351. ' "FI",',
  29352. ' "",',
  29353. ' {',
  29354. ' Default: 2',
  29355. ' }',
  29356. ' );',
  29357. ' $r.addProperty(',
  29358. ' "I2",',
  29359. ' 0,',
  29360. ' rtl.longint,',
  29361. ' "FI",',
  29362. ' "",',
  29363. ' {',
  29364. ' Default: 3',
  29365. ' }',
  29366. ' );',
  29367. ' $r.addProperty(',
  29368. ' "E1",',
  29369. ' 0,',
  29370. ' $mod.$rtti["TEnum"],',
  29371. ' "FE",',
  29372. ' "",',
  29373. ' {',
  29374. ' Default: $mod.TEnum.red',
  29375. ' }',
  29376. ' );',
  29377. ' $r.addProperty(',
  29378. ' "E2",',
  29379. ' 0,',
  29380. ' $mod.$rtti["TEnum"],',
  29381. ' "FE",',
  29382. ' "",',
  29383. ' {',
  29384. ' Default: $mod.TEnum.blue',
  29385. ' }',
  29386. ' );',
  29387. '});',
  29388. '']),
  29389. LinesToStr([ // $mod.$main
  29390. '']));
  29391. end;
  29392. procedure TTestModule.TestRTTI_DefaultValueSet;
  29393. begin
  29394. WithTypeInfo:=true;
  29395. StartProgram(false);
  29396. Add([
  29397. 'type',
  29398. ' TEnum = (red, blue);',
  29399. ' TSet = set of TEnum;',
  29400. 'const',
  29401. ' CSet = [red,blue];',
  29402. 'type',
  29403. ' TObject = class',
  29404. ' FSet: TSet;',
  29405. ' published',
  29406. ' property Set1: TSet read FSet default [];',
  29407. ' property Set2: TSet read FSet default [red];',
  29408. ' property Set3: TSet read FSet default [red,blue];',
  29409. ' property Set4: TSet read FSet default CSet;',
  29410. ' end;',
  29411. 'begin']);
  29412. ConvertProgram;
  29413. CheckSource('TestRTTI_DefaultValueSet',
  29414. LinesToStr([ // statements
  29415. 'this.TEnum = {',
  29416. ' "0": "red",',
  29417. ' red: 0,',
  29418. ' "1": "blue",',
  29419. ' blue: 1',
  29420. '};',
  29421. 'this.$rtti.$Enum("TEnum", {',
  29422. ' minvalue: 0,',
  29423. ' maxvalue: 1,',
  29424. ' ordtype: 1,',
  29425. ' enumtype: this.TEnum',
  29426. '});',
  29427. 'this.$rtti.$Set("TSet", {',
  29428. ' comptype: this.$rtti["TEnum"]',
  29429. '});',
  29430. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29431. 'rtl.createClass(this, "TObject", null, function () {',
  29432. ' this.$init = function () {',
  29433. ' this.FSet = {};',
  29434. ' };',
  29435. ' this.$final = function () {',
  29436. ' this.FSet = undefined;',
  29437. ' };',
  29438. ' var $r = this.$rtti;',
  29439. ' $r.addProperty(',
  29440. ' "Set1",',
  29441. ' 0,',
  29442. ' $mod.$rtti["TSet"],',
  29443. ' "FSet",',
  29444. ' "",',
  29445. ' {',
  29446. ' Default: {}',
  29447. ' }',
  29448. ' );',
  29449. ' $r.addProperty(',
  29450. ' "Set2",',
  29451. ' 0,',
  29452. ' $mod.$rtti["TSet"],',
  29453. ' "FSet",',
  29454. ' "",',
  29455. ' {',
  29456. ' Default: rtl.createSet($mod.TEnum.red)',
  29457. ' }',
  29458. ' );',
  29459. ' $r.addProperty(',
  29460. ' "Set3",',
  29461. ' 0,',
  29462. ' $mod.$rtti["TSet"],',
  29463. ' "FSet",',
  29464. ' "",',
  29465. ' {',
  29466. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29467. ' }',
  29468. ' );',
  29469. ' $r.addProperty(',
  29470. ' "Set4",',
  29471. ' 0,',
  29472. ' $mod.$rtti["TSet"],',
  29473. ' "FSet",',
  29474. ' "",',
  29475. ' {',
  29476. ' Default: $mod.CSet',
  29477. ' }',
  29478. ' );',
  29479. '});',
  29480. '']),
  29481. LinesToStr([ // $mod.$main
  29482. '']));
  29483. end;
  29484. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29485. begin
  29486. WithTypeInfo:=true;
  29487. StartProgram(false);
  29488. Add([
  29489. 'type',
  29490. ' TRg = -1..1;',
  29491. 'const',
  29492. ' l = low(TRg);',
  29493. ' h = high(TRg);',
  29494. 'type',
  29495. ' TObject = class',
  29496. ' FV: TRg;',
  29497. ' published',
  29498. ' property V1: TRg read FV default -1;',
  29499. ' end;',
  29500. 'begin']);
  29501. ConvertProgram;
  29502. CheckSource('TestRTTI_DefaultValueRangeType',
  29503. LinesToStr([ // statements
  29504. 'this.$rtti.$Int("TRg", {',
  29505. ' minvalue: -1,',
  29506. ' maxvalue: 1,',
  29507. ' ordtype: 0',
  29508. '});',
  29509. 'this.l = -1;',
  29510. 'this.h = 1;',
  29511. 'rtl.createClass(this, "TObject", null, function () {',
  29512. ' this.$init = function () {',
  29513. ' this.FV = 0;',
  29514. ' };',
  29515. ' this.$final = function () {',
  29516. ' };',
  29517. ' var $r = this.$rtti;',
  29518. ' $r.addProperty(',
  29519. ' "V1",',
  29520. ' 0,',
  29521. ' $mod.$rtti["TRg"],',
  29522. ' "FV",',
  29523. ' "",',
  29524. ' {',
  29525. ' Default: -1',
  29526. ' }',
  29527. ' );',
  29528. '});',
  29529. '']),
  29530. LinesToStr([ // $mod.$main
  29531. '']));
  29532. end;
  29533. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29534. begin
  29535. WithTypeInfo:=true;
  29536. StartProgram(false);
  29537. Add([
  29538. 'type',
  29539. ' TObject = class',
  29540. ' FA, FB: byte;',
  29541. ' property A: byte read FA default 1;',
  29542. ' property B: byte read FB default 2;',
  29543. ' end;',
  29544. ' TBird = class',
  29545. ' published',
  29546. ' property A;',
  29547. ' property B nodefault;',
  29548. ' end;',
  29549. 'begin']);
  29550. ConvertProgram;
  29551. CheckSource('TestRTTI_DefaultValueInherit',
  29552. LinesToStr([ // statements
  29553. 'rtl.createClass(this, "TObject", null, function () {',
  29554. ' this.$init = function () {',
  29555. ' this.FA = 0;',
  29556. ' this.FB = 0;',
  29557. ' };',
  29558. ' this.$final = function () {',
  29559. ' };',
  29560. '});',
  29561. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29562. ' var $r = this.$rtti;',
  29563. ' $r.addProperty(',
  29564. ' "A",',
  29565. ' 0,',
  29566. ' rtl.byte,',
  29567. ' "FA",',
  29568. ' "",',
  29569. ' {',
  29570. ' Default: 1',
  29571. ' }',
  29572. ' );',
  29573. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29574. '});',
  29575. '']),
  29576. LinesToStr([ // $mod.$main
  29577. '']));
  29578. end;
  29579. procedure TTestModule.TestRTTI_OverrideMethod;
  29580. begin
  29581. WithTypeInfo:=true;
  29582. StartProgram(false);
  29583. Add('type');
  29584. Add(' TObject = class');
  29585. Add(' published');
  29586. Add(' procedure DoIt; virtual; abstract;');
  29587. Add(' end;');
  29588. Add(' TSky = class');
  29589. Add(' published');
  29590. Add(' procedure DoIt; override;');
  29591. Add(' end;');
  29592. Add('procedure TSky.DoIt; begin end;');
  29593. Add('begin');
  29594. ConvertProgram;
  29595. CheckSource('TestRTTI_OverrideMethod',
  29596. LinesToStr([ // statements
  29597. 'rtl.createClass(this, "TObject", null, function () {',
  29598. ' this.$init = function () {',
  29599. ' };',
  29600. ' this.$final = function () {',
  29601. ' };',
  29602. ' var $r = this.$rtti;',
  29603. ' $r.addMethod("DoIt", 0, null);',
  29604. '});',
  29605. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29606. ' this.DoIt = function () {',
  29607. ' };',
  29608. '});',
  29609. '']),
  29610. LinesToStr([ // $mod.$main
  29611. '']));
  29612. end;
  29613. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29614. begin
  29615. WithTypeInfo:=true;
  29616. StartProgram(false);
  29617. Add([
  29618. 'type',
  29619. ' TObject = class',
  29620. ' published',
  29621. ' procedure DoIt;',
  29622. ' end;',
  29623. ' TSky = class',
  29624. ' published',
  29625. ' procedure DoIt; reintroduce;',
  29626. ' end;',
  29627. 'procedure TObject.DoIt; begin end;',
  29628. 'procedure TSky.DoIt;',
  29629. 'begin',
  29630. ' inherited DoIt;',
  29631. 'end;',
  29632. 'begin']);
  29633. ConvertProgram;
  29634. CheckSource('TestRTTI_ReintroduceMethod',
  29635. LinesToStr([ // statements
  29636. 'rtl.createClass(this, "TObject", null, function () {',
  29637. ' this.$init = function () {',
  29638. ' };',
  29639. ' this.$final = function () {',
  29640. ' };',
  29641. ' this.DoIt = function () {',
  29642. ' };',
  29643. ' var $r = this.$rtti;',
  29644. ' $r.addMethod("DoIt", 0, null);',
  29645. '});',
  29646. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29647. ' this.DoIt = function () {',
  29648. ' $mod.TObject.DoIt.call(this);',
  29649. ' };',
  29650. ' var $r = this.$rtti;',
  29651. ' $r.addMethod("DoIt", 0, null);',
  29652. '});',
  29653. '']),
  29654. LinesToStr([ // $mod.$main
  29655. '']));
  29656. end;
  29657. procedure TTestModule.TestRTTI_OverloadProperty;
  29658. begin
  29659. WithTypeInfo:=true;
  29660. StartProgram(false);
  29661. Add('type');
  29662. Add(' TObject = class');
  29663. Add(' protected');
  29664. Add(' FFlag: longint;');
  29665. Add(' published');
  29666. Add(' property Flag: longint read fflag;');
  29667. Add(' end;');
  29668. Add(' TSky = class');
  29669. Add(' published');
  29670. Add(' property FLAG: longint write fflag;');
  29671. Add(' end;');
  29672. Add('begin');
  29673. ConvertProgram;
  29674. CheckSource('TestRTTI_OverrideMethod',
  29675. LinesToStr([ // statements
  29676. 'rtl.createClass(this, "TObject", null, function () {',
  29677. ' this.$init = function () {',
  29678. ' this.FFlag = 0;',
  29679. ' };',
  29680. ' this.$final = function () {',
  29681. ' };',
  29682. ' var $r = this.$rtti;',
  29683. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29684. '});',
  29685. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29686. ' var $r = this.$rtti;',
  29687. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29688. '});',
  29689. '']),
  29690. LinesToStr([ // $mod.$main
  29691. '']));
  29692. end;
  29693. procedure TTestModule.TestRTTI_ClassForward;
  29694. begin
  29695. WithTypeInfo:=true;
  29696. StartProgram(false);
  29697. Add('type');
  29698. Add(' TObject = class end;');
  29699. Add(' tbridge = class;');
  29700. Add(' TProc = function: tbridge;');
  29701. Add(' TOger = class');
  29702. Add(' published');
  29703. Add(' FBridge: tbridge;');
  29704. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29705. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29706. Add(' end;');
  29707. Add(' TBridge = class');
  29708. Add(' FOger: toger;');
  29709. Add(' end;');
  29710. Add('var p: Pointer;');
  29711. Add(' b: tbridge;');
  29712. Add('begin');
  29713. Add(' p:=typeinfo(tbridge);');
  29714. Add(' p:=typeinfo(b);');
  29715. ConvertProgram;
  29716. CheckSource('TestRTTI_ClassForward',
  29717. LinesToStr([ // statements
  29718. 'rtl.createClass(this, "TObject", null, function () {',
  29719. ' this.$init = function () {',
  29720. ' };',
  29721. ' this.$final = function () {',
  29722. ' };',
  29723. '});',
  29724. 'this.$rtti.$Class("TBridge");',
  29725. 'this.$rtti.$ProcVar("TProc", {',
  29726. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29727. '});',
  29728. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29729. ' this.$init = function () {',
  29730. ' $mod.TObject.$init.call(this);',
  29731. ' this.FBridge = null;',
  29732. ' };',
  29733. ' this.$final = function () {',
  29734. ' this.FBridge = undefined;',
  29735. ' $mod.TObject.$final.call(this);',
  29736. ' };',
  29737. ' var $r = this.$rtti;',
  29738. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29739. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29740. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29741. '});',
  29742. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29743. ' this.$init = function () {',
  29744. ' $mod.TObject.$init.call(this);',
  29745. ' this.FOger = null;',
  29746. ' };',
  29747. ' this.$final = function () {',
  29748. ' this.FOger = undefined;',
  29749. ' $mod.TObject.$final.call(this);',
  29750. ' };',
  29751. '});',
  29752. 'this.p = null;',
  29753. 'this.b = null;',
  29754. '']),
  29755. LinesToStr([ // $mod.$main
  29756. '$mod.p = $mod.$rtti["TBridge"];',
  29757. '$mod.p = $mod.b.$rtti;',
  29758. '']));
  29759. end;
  29760. procedure TTestModule.TestRTTI_ClassOf;
  29761. begin
  29762. WithTypeInfo:=true;
  29763. StartProgram(false);
  29764. Add('type');
  29765. Add(' TClass = class of tobject;');
  29766. Add(' TProcA = function: TClass;');
  29767. Add(' TObject = class');
  29768. Add(' published');
  29769. Add(' C: tclass;');
  29770. Add(' end;');
  29771. Add(' tfox = class;');
  29772. Add(' TBird = class end;');
  29773. Add(' TBirds = class of tbird;');
  29774. Add(' TFox = class end;');
  29775. Add(' TFoxes = class of tfox;');
  29776. Add(' TCows = class of TCow;');
  29777. Add(' TCow = class;');
  29778. Add(' TCow = class end;');
  29779. Add('begin');
  29780. ConvertProgram;
  29781. CheckSource('TestRTTI_ClassOf',
  29782. LinesToStr([ // statements
  29783. 'this.$rtti.$Class("TObject");',
  29784. 'this.$rtti.$ClassRef("TClass", {',
  29785. ' instancetype: this.$rtti["TObject"]',
  29786. '});',
  29787. 'this.$rtti.$ProcVar("TProcA", {',
  29788. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29789. '});',
  29790. 'rtl.createClass(this, "TObject", null, function () {',
  29791. ' this.$init = function () {',
  29792. ' this.C = null;',
  29793. ' };',
  29794. ' this.$final = function () {',
  29795. ' this.C = undefined;',
  29796. ' };',
  29797. ' var $r = this.$rtti;',
  29798. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29799. '});',
  29800. 'this.$rtti.$Class("TFox");',
  29801. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29802. '});',
  29803. 'this.$rtti.$ClassRef("TBirds", {',
  29804. ' instancetype: this.$rtti["TBird"]',
  29805. '});',
  29806. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29807. '});',
  29808. 'this.$rtti.$ClassRef("TFoxes", {',
  29809. ' instancetype: this.$rtti["TFox"]',
  29810. '});',
  29811. 'this.$rtti.$Class("TCow");',
  29812. 'this.$rtti.$ClassRef("TCows", {',
  29813. ' instancetype: this.$rtti["TCow"]',
  29814. '});',
  29815. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29816. '});',
  29817. '']),
  29818. LinesToStr([ // $mod.$main
  29819. '']));
  29820. end;
  29821. procedure TTestModule.TestRTTI_Record;
  29822. begin
  29823. WithTypeInfo:=true;
  29824. StartProgram(false);
  29825. Add('type');
  29826. Add(' integer = longint;');
  29827. Add(' TPoint = record');
  29828. Add(' x,y: integer;');
  29829. Add(' end;');
  29830. Add('var p: pointer;');
  29831. Add(' r: tpoint;');
  29832. Add('begin');
  29833. Add(' p:=typeinfo(tpoint);');
  29834. Add(' p:=typeinfo(r);');
  29835. Add(' p:=typeinfo(r.x);');
  29836. ConvertProgram;
  29837. CheckSource('TestRTTI_Record',
  29838. LinesToStr([ // statements
  29839. 'rtl.recNewT(this, "TPoint", function () {',
  29840. ' this.x = 0;',
  29841. ' this.y = 0;',
  29842. ' this.$eq = function (b) {',
  29843. ' return (this.x === b.x) && (this.y === b.y);',
  29844. ' };',
  29845. ' this.$assign = function (s) {',
  29846. ' this.x = s.x;',
  29847. ' this.y = s.y;',
  29848. ' return this;',
  29849. ' };',
  29850. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29851. ' $r.addField("x", rtl.longint);',
  29852. ' $r.addField("y", rtl.longint);',
  29853. '});',
  29854. 'this.p = null;',
  29855. 'this.r = this.TPoint.$new();',
  29856. '']),
  29857. LinesToStr([ // $mod.$main
  29858. '$mod.p = $mod.$rtti["TPoint"];',
  29859. '$mod.p = $mod.$rtti["TPoint"];',
  29860. '$mod.p = rtl.longint;',
  29861. '']));
  29862. end;
  29863. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29864. begin
  29865. WithTypeInfo:=true;
  29866. StartProgram(false);
  29867. Add('type');
  29868. Add(' TFloatRec = record');
  29869. Add(' c,d: array of char;');
  29870. // Add(' i: array of array of longint;');
  29871. Add(' end;');
  29872. Add('var p: pointer;');
  29873. Add(' r: tfloatrec;');
  29874. Add('begin');
  29875. Add(' p:=typeinfo(tfloatrec);');
  29876. Add(' p:=typeinfo(r);');
  29877. Add(' p:=typeinfo(r.d);');
  29878. ConvertProgram;
  29879. CheckSource('TestRTTI_Record',
  29880. LinesToStr([ // statements
  29881. 'rtl.recNewT(this, "TFloatRec", function () {',
  29882. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29883. ' eltype: rtl.char',
  29884. ' });',
  29885. ' this.$new = function () {',
  29886. ' var r = Object.create(this);',
  29887. ' r.c = [];',
  29888. ' r.d = [];',
  29889. ' return r;',
  29890. ' };',
  29891. ' this.$eq = function (b) {',
  29892. ' return (this.c === b.c) && (this.d === b.d);',
  29893. ' };',
  29894. ' this.$assign = function (s) {',
  29895. ' this.c = rtl.arrayRef(s.c);',
  29896. ' this.d = rtl.arrayRef(s.d);',
  29897. ' return this;',
  29898. ' };',
  29899. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29900. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29901. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29902. '});',
  29903. 'this.p = null;',
  29904. 'this.r = this.TFloatRec.$new();',
  29905. '']),
  29906. LinesToStr([ // $mod.$main
  29907. '$mod.p = $mod.$rtti["TFloatRec"];',
  29908. '$mod.p = $mod.$rtti["TFloatRec"];',
  29909. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29910. '']));
  29911. end;
  29912. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29913. begin
  29914. WithTypeInfo:=true;
  29915. StartProgram(false);
  29916. Add([
  29917. '{$modeswitch AdvancedRecords}',
  29918. 'type',
  29919. ' TPoint = record',
  29920. ' type TProc = procedure(w: word);',
  29921. ' class var p: TProc;',
  29922. ' end;',
  29923. 'begin',
  29924. '']);
  29925. ConvertProgram;
  29926. CheckSource('TestRTTI_Record_ClassVarType',
  29927. LinesToStr([ // statements
  29928. 'rtl.recNewT(this, "TPoint", function () {',
  29929. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  29930. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  29931. ' });',
  29932. ' this.p = null;',
  29933. ' this.$eq = function (b) {',
  29934. ' return true;',
  29935. ' };',
  29936. ' this.$assign = function (s) {',
  29937. ' return this;',
  29938. ' };',
  29939. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29940. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  29941. '}, true);',
  29942. '']),
  29943. LinesToStr([ // $mod.$main
  29944. '']));
  29945. end;
  29946. procedure TTestModule.TestRTTI_LocalTypes;
  29947. begin
  29948. WithTypeInfo:=true;
  29949. StartProgram(false);
  29950. Add([
  29951. 'procedure DoIt;',
  29952. 'type',
  29953. ' integer = longint;',
  29954. ' TPoint = record',
  29955. ' x,y: integer;',
  29956. ' end;',
  29957. 'var p: TPoint;',
  29958. 'begin',
  29959. 'end;',
  29960. 'begin']);
  29961. ConvertProgram;
  29962. CheckSource('TestRTTI_LocalTypes',
  29963. LinesToStr([ // statements
  29964. 'var TPoint = rtl.recNewT(null, "", function () {',
  29965. ' this.x = 0;',
  29966. ' this.y = 0;',
  29967. ' this.$eq = function (b) {',
  29968. ' return (this.x === b.x) && (this.y === b.y);',
  29969. ' };',
  29970. ' this.$assign = function (s) {',
  29971. ' this.x = s.x;',
  29972. ' this.y = s.y;',
  29973. ' return this;',
  29974. ' };',
  29975. '});',
  29976. 'this.DoIt = function () {',
  29977. ' var p = TPoint.$new();',
  29978. '};',
  29979. '']),
  29980. LinesToStr([ // $mod.$main
  29981. '']));
  29982. end;
  29983. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29984. begin
  29985. WithTypeInfo:=true;
  29986. StartProgram(false);
  29987. Add([
  29988. 'type',
  29989. ' TCaption = string;',
  29990. ' TYesNo = boolean;',
  29991. ' TLetter = char;',
  29992. ' TFloat = double;',
  29993. ' TPtr = pointer;',
  29994. ' TShortInt = shortint;',
  29995. ' TByte = byte;',
  29996. ' TSmallInt = smallint;',
  29997. ' TWord = word;',
  29998. ' TInt32 = longint;',
  29999. ' TDWord = longword;',
  30000. ' TValue = jsvalue;',
  30001. 'var p: TPtr;',
  30002. 'begin',
  30003. ' p:=typeinfo(string);',
  30004. ' p:=typeinfo(tcaption);',
  30005. ' p:=typeinfo(boolean);',
  30006. ' p:=typeinfo(tyesno);',
  30007. ' p:=typeinfo(char);',
  30008. ' p:=typeinfo(tletter);',
  30009. ' p:=typeinfo(double);',
  30010. ' p:=typeinfo(tfloat);',
  30011. ' p:=typeinfo(pointer);',
  30012. ' p:=typeinfo(tptr);',
  30013. ' p:=typeinfo(shortint);',
  30014. ' p:=typeinfo(tshortint);',
  30015. ' p:=typeinfo(byte);',
  30016. ' p:=typeinfo(tbyte);',
  30017. ' p:=typeinfo(smallint);',
  30018. ' p:=typeinfo(tsmallint);',
  30019. ' p:=typeinfo(word);',
  30020. ' p:=typeinfo(tword);',
  30021. ' p:=typeinfo(longword);',
  30022. ' p:=typeinfo(tdword);',
  30023. ' p:=typeinfo(jsvalue);',
  30024. ' p:=typeinfo(tvalue);',
  30025. '']);
  30026. ConvertProgram;
  30027. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30028. LinesToStr([ // statements
  30029. 'this.p = null;',
  30030. '']),
  30031. LinesToStr([ // $mod.$main
  30032. '$mod.p = rtl.string;',
  30033. '$mod.p = rtl.string;',
  30034. '$mod.p = rtl.boolean;',
  30035. '$mod.p = rtl.boolean;',
  30036. '$mod.p = rtl.char;',
  30037. '$mod.p = rtl.char;',
  30038. '$mod.p = rtl.double;',
  30039. '$mod.p = rtl.double;',
  30040. '$mod.p = rtl.pointer;',
  30041. '$mod.p = rtl.pointer;',
  30042. '$mod.p = rtl.shortint;',
  30043. '$mod.p = rtl.shortint;',
  30044. '$mod.p = rtl.byte;',
  30045. '$mod.p = rtl.byte;',
  30046. '$mod.p = rtl.smallint;',
  30047. '$mod.p = rtl.smallint;',
  30048. '$mod.p = rtl.word;',
  30049. '$mod.p = rtl.word;',
  30050. '$mod.p = rtl.longword;',
  30051. '$mod.p = rtl.longword;',
  30052. '$mod.p = rtl.jsvalue;',
  30053. '$mod.p = rtl.jsvalue;',
  30054. '']));
  30055. end;
  30056. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30057. begin
  30058. WithTypeInfo:=true;
  30059. StartProgram(false);
  30060. Add([
  30061. 'type',
  30062. ' TCaption = type string;',
  30063. ' TYesNo = type boolean;',
  30064. ' TLetter = type char;',
  30065. ' TFloat = type double;',
  30066. ' TPtr = type pointer;',
  30067. ' TShortInt = type shortint;',
  30068. ' TByte = type byte;',
  30069. ' TSmallInt = type smallint;',
  30070. ' TWord = type word;',
  30071. ' TInt32 = type longint;',
  30072. ' TDWord = type longword;',
  30073. ' TValue = type jsvalue;',
  30074. ' TAliasValue = type TValue;',
  30075. 'var',
  30076. ' p: TPtr;',
  30077. ' a: TAliasValue;',
  30078. 'begin',
  30079. ' p:=typeinfo(tcaption);',
  30080. ' p:=typeinfo(tyesno);',
  30081. ' p:=typeinfo(tletter);',
  30082. ' p:=typeinfo(tfloat);',
  30083. ' p:=typeinfo(tptr);',
  30084. ' p:=typeinfo(tshortint);',
  30085. ' p:=typeinfo(tbyte);',
  30086. ' p:=typeinfo(tsmallint);',
  30087. ' p:=typeinfo(tword);',
  30088. ' p:=typeinfo(tdword);',
  30089. ' p:=typeinfo(tvalue);',
  30090. ' p:=typeinfo(taliasvalue);',
  30091. ' p:=typeinfo(a);',
  30092. '']);
  30093. ConvertProgram;
  30094. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30095. LinesToStr([ // statements
  30096. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30097. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30098. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30099. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30100. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30101. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30102. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30103. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30104. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30105. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30106. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30107. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30108. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30109. 'this.p = null;',
  30110. 'this.a = undefined;',
  30111. '']),
  30112. LinesToStr([ // $mod.$main
  30113. '$mod.p = $mod.$rtti["TCaption"];',
  30114. '$mod.p = $mod.$rtti["TYesNo"];',
  30115. '$mod.p = $mod.$rtti["TLetter"];',
  30116. '$mod.p = $mod.$rtti["TFloat"];',
  30117. '$mod.p = $mod.$rtti["TPtr"];',
  30118. '$mod.p = $mod.$rtti["TShortInt"];',
  30119. '$mod.p = $mod.$rtti["TByte"];',
  30120. '$mod.p = $mod.$rtti["TSmallInt"];',
  30121. '$mod.p = $mod.$rtti["TWord"];',
  30122. '$mod.p = $mod.$rtti["TDWord"];',
  30123. '$mod.p = $mod.$rtti["TValue"];',
  30124. '$mod.p = $mod.$rtti["TAliasValue"];',
  30125. '$mod.p = $mod.$rtti["TAliasValue"];',
  30126. '']));
  30127. end;
  30128. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30129. begin
  30130. WithTypeInfo:=true;
  30131. StartProgram(false);
  30132. Add('procedure DoIt;');
  30133. Add('type');
  30134. Add(' integer = longint;');
  30135. Add(' TPoint = record');
  30136. Add(' x,y: integer;');
  30137. Add(' end;');
  30138. Add('var p: pointer;');
  30139. Add('begin');
  30140. Add(' p:=typeinfo(tpoint);');
  30141. Add('end;');
  30142. Add('begin');
  30143. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30144. ConvertProgram;
  30145. end;
  30146. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30147. begin
  30148. WithTypeInfo:=true;
  30149. StartProgram(true,[supTypeInfo]);
  30150. Add([
  30151. '{$modeswitch externalclass}',
  30152. 'type',
  30153. ' TFlag = (up,down);',
  30154. ' TFlags = set of TFlag;',
  30155. 'var',
  30156. ' ti: TTypeInfo;',
  30157. ' tiInt: TTypeInfoInteger;',
  30158. ' tiEnum: TTypeInfoEnum;',
  30159. ' tiSet: TTypeInfoSet;',
  30160. 'begin',
  30161. ' ti:=typeinfo(string);',
  30162. ' ti:=typeinfo(boolean);',
  30163. ' ti:=typeinfo(char);',
  30164. ' ti:=typeinfo(double);',
  30165. ' tiInt:=typeinfo(shortint);',
  30166. ' tiInt:=typeinfo(byte);',
  30167. ' tiInt:=typeinfo(smallint);',
  30168. ' tiInt:=typeinfo(word);',
  30169. ' tiInt:=typeinfo(longint);',
  30170. ' tiInt:=typeinfo(longword);',
  30171. ' ti:=typeinfo(jsvalue);',
  30172. ' tiEnum:=typeinfo(tflag);',
  30173. ' tiSet:=typeinfo(tflags);']);
  30174. ConvertProgram;
  30175. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30176. LinesToStr([ // statements
  30177. 'this.TFlag = {',
  30178. ' "0": "up",',
  30179. ' up: 0,',
  30180. ' "1": "down",',
  30181. ' down: 1',
  30182. '};',
  30183. 'this.$rtti.$Enum("TFlag", {',
  30184. ' minvalue: 0,',
  30185. ' maxvalue: 1,',
  30186. ' ordtype: 1,',
  30187. ' enumtype: this.TFlag',
  30188. '});',
  30189. 'this.$rtti.$Set("TFlags", {',
  30190. ' comptype: this.$rtti["TFlag"]',
  30191. '});',
  30192. 'this.ti = null;',
  30193. 'this.tiInt = null;',
  30194. 'this.tiEnum = null;',
  30195. 'this.tiSet = null;',
  30196. '']),
  30197. LinesToStr([ // $mod.$main
  30198. '$mod.ti = rtl.string;',
  30199. '$mod.ti = rtl.boolean;',
  30200. '$mod.ti = rtl.char;',
  30201. '$mod.ti = rtl.double;',
  30202. '$mod.tiInt = rtl.shortint;',
  30203. '$mod.tiInt = rtl.byte;',
  30204. '$mod.tiInt = rtl.smallint;',
  30205. '$mod.tiInt = rtl.word;',
  30206. '$mod.tiInt = rtl.longint;',
  30207. '$mod.tiInt = rtl.longword;',
  30208. '$mod.ti = rtl.jsvalue;',
  30209. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30210. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30211. '']));
  30212. end;
  30213. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30214. begin
  30215. WithTypeInfo:=true;
  30216. StartProgram(true,[supTypeInfo]);
  30217. Add('{$modeswitch externalclass}');
  30218. Add('type');
  30219. Add(' TStaticArr = array[boolean] of string;');
  30220. Add(' TDynArr = array of string;');
  30221. Add(' TProc = procedure;');
  30222. Add(' TMethod = procedure of object;');
  30223. Add('var');
  30224. Add(' StaticArray: TStaticArr;');
  30225. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30226. Add(' DynArray: TDynArr;');
  30227. Add(' tiDynArray: TTypeInfoDynArray;');
  30228. Add(' ProcVar: TProc;');
  30229. Add(' tiProcVar: TTypeInfoProcVar;');
  30230. Add(' MethodVar: TMethod;');
  30231. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30232. Add('begin');
  30233. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30234. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30235. Add(' tiDynArray:=typeinfo(DynArray);');
  30236. Add(' tiDynArray:=typeinfo(TDynArr);');
  30237. Add(' tiProcVar:=typeinfo(ProcVar);');
  30238. Add(' tiProcVar:=typeinfo(TProc);');
  30239. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30240. Add(' tiMethodVar:=typeinfo(TMethod);');
  30241. ConvertProgram;
  30242. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30243. LinesToStr([ // statements
  30244. 'this.$rtti.$StaticArray("TStaticArr", {',
  30245. ' dims: [2],',
  30246. ' eltype: rtl.string',
  30247. '});',
  30248. 'this.$rtti.$DynArray("TDynArr", {',
  30249. ' eltype: rtl.string',
  30250. '});',
  30251. 'this.$rtti.$ProcVar("TProc", {',
  30252. ' procsig: rtl.newTIProcSig(null)',
  30253. '});',
  30254. 'this.$rtti.$MethodVar("TMethod", {',
  30255. ' procsig: rtl.newTIProcSig(null),',
  30256. ' methodkind: 0',
  30257. '});',
  30258. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30259. 'this.tiStaticArray = null;',
  30260. 'this.DynArray = [];',
  30261. 'this.tiDynArray = null;',
  30262. 'this.ProcVar = null;',
  30263. 'this.tiProcVar = null;',
  30264. 'this.MethodVar = null;',
  30265. 'this.tiMethodVar = null;',
  30266. '']),
  30267. LinesToStr([ // $mod.$main
  30268. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30269. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30270. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30271. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30272. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30273. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30274. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30275. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30276. '']));
  30277. end;
  30278. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30279. begin
  30280. WithTypeInfo:=true;
  30281. StartProgram(true,[supTypeInfo]);
  30282. Add('{$modeswitch externalclass}');
  30283. Add('type');
  30284. Add(' TRec = record end;');
  30285. // ToDo: ^TRec
  30286. Add(' TObject = class end;');
  30287. Add(' TClass = class of tobject;');
  30288. Add('var');
  30289. Add(' Rec: trec;');
  30290. Add(' tiRecord: ttypeinforecord;');
  30291. Add(' Obj: tobject;');
  30292. Add(' tiClass: ttypeinfoclass;');
  30293. Add(' aClass: tclass;');
  30294. Add(' tiClassRef: ttypeinfoclassref;');
  30295. // ToDo: ^TRec
  30296. Add(' tiPointer: ttypeinfopointer;');
  30297. Add('begin');
  30298. Add(' tirecord:=typeinfo(trec);');
  30299. Add(' tirecord:=typeinfo(trec);');
  30300. Add(' ticlass:=typeinfo(obj);');
  30301. Add(' ticlass:=typeinfo(tobject);');
  30302. Add(' ticlass:=typeinfo(aclass);');
  30303. Add(' ticlassref:=typeinfo(tclass);');
  30304. ConvertProgram;
  30305. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30306. LinesToStr([ // statements
  30307. 'rtl.recNewT(this, "TRec", function () {',
  30308. ' this.$eq = function (b) {',
  30309. ' return true;',
  30310. ' };',
  30311. ' this.$assign = function (s) {',
  30312. ' return this;',
  30313. ' };',
  30314. ' $mod.$rtti.$Record("TRec", {});',
  30315. '});',
  30316. 'rtl.createClass(this, "TObject", null, function () {',
  30317. ' this.$init = function () {',
  30318. ' };',
  30319. ' this.$final = function () {',
  30320. ' };',
  30321. '});',
  30322. 'this.$rtti.$ClassRef("TClass", {',
  30323. ' instancetype: this.$rtti["TObject"]',
  30324. '});',
  30325. 'this.Rec = this.TRec.$new();',
  30326. 'this.tiRecord = null;',
  30327. 'this.Obj = null;',
  30328. 'this.tiClass = null;',
  30329. 'this.aClass = null;',
  30330. 'this.tiClassRef = null;',
  30331. 'this.tiPointer = null;',
  30332. '']),
  30333. LinesToStr([ // $mod.$main
  30334. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30335. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30336. '$mod.tiClass = $mod.Obj.$rtti;',
  30337. '$mod.tiClass = $mod.$rtti["TObject"];',
  30338. '$mod.tiClass = $mod.aClass.$rtti;',
  30339. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30340. '']));
  30341. end;
  30342. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30343. begin
  30344. WithTypeInfo:=true;
  30345. StartProgram(true,[supTypeInfo]);
  30346. Add([
  30347. '{$modeswitch externalclass}',
  30348. 'type',
  30349. ' TClass = class of tobject;',
  30350. ' TObject = class',
  30351. ' function MyClass: TClass;',
  30352. ' class function ClassType: TClass;',
  30353. ' end;',
  30354. 'function TObject.MyClass: TClass;',
  30355. 'var t: TTypeInfoClass;',
  30356. 'begin',
  30357. ' t:=TypeInfo(Self);',
  30358. ' t:=TypeInfo(Result);',
  30359. ' t:=TypeInfo(TObject);',
  30360. 'end;',
  30361. 'class function TObject.ClassType: TClass;',
  30362. 'var t: TTypeInfoClass;',
  30363. 'begin',
  30364. ' t:=TypeInfo(Self);',
  30365. ' t:=TypeInfo(Result);',
  30366. 'end;',
  30367. 'var',
  30368. ' Obj: TObject;',
  30369. ' t: TTypeInfoClass;',
  30370. 'begin',
  30371. ' t:=TypeInfo(TObject.ClassType);',
  30372. ' t:=TypeInfo(Obj.ClassType);',
  30373. ' t:=TypeInfo(Obj.MyClass);',
  30374. '']);
  30375. ConvertProgram;
  30376. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30377. LinesToStr([ // statements
  30378. 'this.$rtti.$Class("TObject");',
  30379. 'this.$rtti.$ClassRef("TClass", {',
  30380. ' instancetype: this.$rtti["TObject"]',
  30381. '});',
  30382. 'rtl.createClass(this, "TObject", null, function () {',
  30383. ' this.$init = function () {',
  30384. ' };',
  30385. ' this.$final = function () {',
  30386. ' };',
  30387. ' this.MyClass = function () {',
  30388. ' var Result = null;',
  30389. ' var t = null;',
  30390. ' t = this.$rtti;',
  30391. ' t = Result.$rtti;',
  30392. ' t = $mod.$rtti["TObject"];',
  30393. ' return Result;',
  30394. ' };',
  30395. ' this.ClassType = function () {',
  30396. ' var Result = null;',
  30397. ' var t = null;',
  30398. ' t = this.$rtti;',
  30399. ' t = Result.$rtti;',
  30400. ' return Result;',
  30401. ' };',
  30402. '});',
  30403. 'this.Obj = null;',
  30404. 'this.t = null;',
  30405. '']),
  30406. LinesToStr([ // $mod.$main
  30407. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30408. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30409. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30410. '']));
  30411. end;
  30412. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30413. begin
  30414. WithTypeInfo:=true;
  30415. AddModuleWithIntfImplSrc('typinfo.pas',
  30416. LinesToStr([
  30417. '{$modeswitch externalclass}',
  30418. 'type',
  30419. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30420. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30421. '']),
  30422. '');
  30423. AddModuleWithIntfImplSrc('unit2.pas',
  30424. LinesToStr([
  30425. 'uses typinfo;',
  30426. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30427. 'procedure DoPtr(p: PTypeInfo);',
  30428. 'procedure DoInfo(t: TTypeInfo);',
  30429. 'procedure DoInt(t: TTypeInfoInteger);',
  30430. '']),
  30431. LinesToStr([
  30432. 'procedure DoPtr(p: PTypeInfo);',
  30433. 'begin end;',
  30434. 'procedure DoInfo(t: TTypeInfo);',
  30435. 'begin end;',
  30436. 'procedure DoInt(t: TTypeInfoInteger);',
  30437. 'begin end;',
  30438. '']));
  30439. StartUnit(true);
  30440. Add([
  30441. 'interface',
  30442. 'uses unit2;', // does not use unit typinfo
  30443. 'implementation',
  30444. 'var',
  30445. ' i: byte;',
  30446. ' p: pointer;',
  30447. ' t: PTypeInfo;',
  30448. 'initialization',
  30449. ' p:=typeinfo(i);',
  30450. ' t:=typeinfo(i);',
  30451. ' if p=t then ;',
  30452. ' if p=typeinfo(i) then ;',
  30453. ' if typeinfo(i)=p then ;',
  30454. ' if t=typeinfo(i) then ;',
  30455. ' if typeinfo(i)=t then ;',
  30456. ' DoPtr(p);',
  30457. ' DoPtr(t);',
  30458. ' DoPtr(typeinfo(i));',
  30459. ' DoInfo(p);',
  30460. ' DoInfo(t);',
  30461. ' DoInfo(typeinfo(i));',
  30462. ' DoInt(typeinfo(i));',
  30463. '']);
  30464. ConvertUnit;
  30465. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30466. LinesToStr([ // statements
  30467. 'var $impl = $mod.$impl;',
  30468. '']),
  30469. LinesToStr([ // this.$init
  30470. '$impl.p = rtl.byte;',
  30471. '$impl.t = rtl.byte;',
  30472. 'if ($impl.p === $impl.t) ;',
  30473. 'if ($impl.p === rtl.byte) ;',
  30474. 'if (rtl.byte === $impl.p) ;',
  30475. 'if ($impl.t === rtl.byte) ;',
  30476. 'if (rtl.byte === $impl.t) ;',
  30477. 'pas.unit2.DoPtr($impl.p);',
  30478. 'pas.unit2.DoPtr($impl.t);',
  30479. 'pas.unit2.DoPtr(rtl.byte);',
  30480. 'pas.unit2.DoInfo($impl.p);',
  30481. 'pas.unit2.DoInfo($impl.t);',
  30482. 'pas.unit2.DoInfo(rtl.byte);',
  30483. 'pas.unit2.DoInt(rtl.byte);',
  30484. '']),
  30485. LinesToStr([ // implementation
  30486. '$impl.i = 0;',
  30487. '$impl.p = null;',
  30488. '$impl.t = null;',
  30489. '']) );
  30490. end;
  30491. procedure TTestModule.TestRTTI_Interface_Corba;
  30492. begin
  30493. WithTypeInfo:=true;
  30494. StartProgram(true,[supTypeInfo]);
  30495. Add([
  30496. '{$interfaces corba}',
  30497. '{$modeswitch externalclass}',
  30498. 'type',
  30499. ' IUnknown = interface',
  30500. ' end;',
  30501. ' IBird = interface',
  30502. ' function GetItem: longint;',
  30503. ' procedure SetItem(Value: longint);',
  30504. ' property Item: longint read GetItem write SetItem;',
  30505. ' end;',
  30506. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30507. 'var',
  30508. ' i: IBird;',
  30509. ' t: TTypeInfoInterface;',
  30510. 'begin',
  30511. ' t:=TypeInfo(IBird);',
  30512. ' t:=TypeInfo(i);',
  30513. ' DoIt(t);',
  30514. ' DoIt(TypeInfo(IBird));',
  30515. '']);
  30516. ConvertProgram;
  30517. CheckSource('TestRTTI_Interface_Corba',
  30518. LinesToStr([ // statements
  30519. 'rtl.createInterface(',
  30520. ' this,',
  30521. ' "IUnknown",',
  30522. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30523. ' [],',
  30524. ' null,',
  30525. ' function () {',
  30526. ' }',
  30527. ');',
  30528. 'rtl.createInterface(',
  30529. ' this,',
  30530. ' "IBird",',
  30531. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30532. ' ["GetItem", "SetItem"],',
  30533. ' null,',
  30534. ' function () {',
  30535. ' var $r = this.$rtti;',
  30536. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30537. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30538. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30539. ' }',
  30540. ');',
  30541. 'this.DoIt = function (t) {',
  30542. '}; ',
  30543. 'this.i = null;',
  30544. 'this.t = null;',
  30545. '']),
  30546. LinesToStr([ // $mod.$main
  30547. '$mod.t = $mod.$rtti["IBird"];',
  30548. '$mod.t = $mod.i.$rtti;',
  30549. '$mod.DoIt($mod.t);',
  30550. '$mod.DoIt($mod.$rtti["IBird"]);',
  30551. '']));
  30552. end;
  30553. procedure TTestModule.TestRTTI_Interface_COM;
  30554. begin
  30555. WithTypeInfo:=true;
  30556. StartProgram(true,[supTypeInfo]);
  30557. Add([
  30558. '{$interfaces com}',
  30559. '{$modeswitch externalclass}',
  30560. 'type',
  30561. ' TGuid = record end;',
  30562. ' integer = longint;',
  30563. ' IUnknown = interface',
  30564. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30565. ' function _AddRef: Integer;',
  30566. ' function _Release: Integer;',
  30567. ' end;',
  30568. ' IBird = interface',
  30569. ' function GetItem: longint;',
  30570. ' procedure SetItem(Value: longint);',
  30571. ' property Item: longint read GetItem write SetItem;',
  30572. ' end;',
  30573. 'var',
  30574. ' i: IBird;',
  30575. ' t: TTypeInfoInterface;',
  30576. 'begin',
  30577. ' t:=TypeInfo(IBird);',
  30578. ' t:=TypeInfo(i);',
  30579. '']);
  30580. ConvertProgram;
  30581. CheckSource('TestRTTI_Interface_COM',
  30582. LinesToStr([ // statements
  30583. 'rtl.recNewT(this, "TGuid", function () {',
  30584. ' this.$eq = function (b) {',
  30585. ' return true;',
  30586. ' };',
  30587. ' this.$assign = function (s) {',
  30588. ' return this;',
  30589. ' };',
  30590. ' $mod.$rtti.$Record("TGuid", {});',
  30591. '});',
  30592. 'rtl.createInterface(',
  30593. ' this,',
  30594. ' "IUnknown",',
  30595. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30596. ' ["QueryInterface", "_AddRef", "_Release"],',
  30597. ' null,',
  30598. ' function () {',
  30599. ' this.$kind = "com";',
  30600. ' var $r = this.$rtti;',
  30601. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30602. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30603. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30604. ' }',
  30605. ');',
  30606. 'rtl.createInterface(',
  30607. ' this,',
  30608. ' "IBird",',
  30609. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30610. ' ["GetItem", "SetItem"],',
  30611. ' this.IUnknown,',
  30612. ' function () {',
  30613. ' var $r = this.$rtti;',
  30614. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30615. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30616. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30617. ' }',
  30618. ');',
  30619. 'this.i = null;',
  30620. 'this.t = null;',
  30621. '']),
  30622. LinesToStr([ // $mod.$main
  30623. '$mod.t = $mod.$rtti["IBird"];',
  30624. '$mod.t = $mod.i.$rtti;',
  30625. '']));
  30626. end;
  30627. procedure TTestModule.TestRTTI_ClassHelper;
  30628. begin
  30629. WithTypeInfo:=true;
  30630. StartProgram(true,[supTypeInfo]);
  30631. Add([
  30632. '{$interfaces com}',
  30633. '{$modeswitch externalclass}',
  30634. 'type',
  30635. ' TObject = class',
  30636. ' end;',
  30637. ' THelper = class helper for TObject',
  30638. ' published',
  30639. ' function GetItem: longint;',
  30640. ' property Item: longint read GetItem;',
  30641. ' end;',
  30642. 'function THelper.GetItem: longint;',
  30643. 'begin',
  30644. 'end;',
  30645. 'var',
  30646. ' t: TTypeInfoHelper;',
  30647. 'begin',
  30648. ' t:=TypeInfo(THelper);',
  30649. '']);
  30650. ConvertProgram;
  30651. CheckSource('TestRTTI_ClassHelper',
  30652. LinesToStr([ // statements
  30653. 'rtl.createClass(this, "TObject", null, function () {',
  30654. ' this.$init = function () {',
  30655. ' };',
  30656. ' this.$final = function () {',
  30657. ' };',
  30658. '});',
  30659. 'rtl.createHelper(this, "THelper", null, function () {',
  30660. ' this.GetItem = function () {',
  30661. ' var Result = 0;',
  30662. ' return Result;',
  30663. ' };',
  30664. ' var $r = this.$rtti;',
  30665. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30666. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30667. '});',
  30668. 'this.t = null;',
  30669. '']),
  30670. LinesToStr([ // $mod.$main
  30671. '$mod.t = $mod.$rtti["THelper"];',
  30672. '']));
  30673. end;
  30674. procedure TTestModule.TestRTTI_ExternalClass;
  30675. begin
  30676. WithTypeInfo:=true;
  30677. StartProgram(true,[supTypeInfo]);
  30678. Add([
  30679. '{$modeswitch externalclass}',
  30680. 'type',
  30681. ' TJSObject = class external name ''Object''',
  30682. ' end;',
  30683. ' TJSArray = class external name ''Array'' (TJSObject)',
  30684. ' end;',
  30685. 'var',
  30686. ' p: Pointer;',
  30687. ' tc: TTypeInfoExtClass;',
  30688. 'begin',
  30689. ' p:=typeinfo(TJSArray);']);
  30690. ConvertProgram;
  30691. CheckSource('TestRTTI_ExternalClass',
  30692. LinesToStr([ // statements
  30693. 'this.$rtti.$ExtClass("TJSObject", {',
  30694. ' jsclass: "Object"',
  30695. '});',
  30696. 'this.$rtti.$ExtClass("TJSArray", {',
  30697. ' ancestor: this.$rtti["TJSObject"],',
  30698. ' jsclass: "Array"',
  30699. '});',
  30700. 'this.p = null;',
  30701. 'this.tc = null;',
  30702. '']),
  30703. LinesToStr([ // $mod.$main
  30704. '$mod.p = $mod.$rtti["TJSArray"];',
  30705. '']));
  30706. end;
  30707. procedure TTestModule.TestRTTI_Unit;
  30708. begin
  30709. WithTypeInfo:=true;
  30710. AddModuleWithIntfImplSrc('unit2.pas',
  30711. LinesToStr([
  30712. '{$mode delphi}',
  30713. 'type',
  30714. ' TWordArray = array of word;',
  30715. ' TArray<T> = array of T;',
  30716. '']),
  30717. '');
  30718. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30719. Add([
  30720. '{$mode delphi}',
  30721. 'interface',
  30722. 'uses unit2;',
  30723. 'type',
  30724. ' IBird = interface',
  30725. ' function Swoop: TWordArray;',
  30726. ' function Glide: TArray<word>;',
  30727. ' end;',
  30728. 'procedure Fly;',
  30729. 'implementation',
  30730. 'procedure Fly;',
  30731. 'var',
  30732. ' ta: tTypeInfoDynArray;',
  30733. ' ti: tTypeInfoInterface;',
  30734. 'begin',
  30735. ' ta:=typeinfo(TWordArray);',
  30736. ' ta:=typeinfo(TArray<word>);',
  30737. ' ti:=typeinfo(IBird);',
  30738. 'end;',
  30739. '']);
  30740. ConvertUnit;
  30741. CheckSource('TestRTTI_ExternalClass',
  30742. LinesToStr([ // statements
  30743. 'rtl.createInterface(',
  30744. ' this,',
  30745. ' "IBird",',
  30746. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30747. ' ["Swoop", "Glide"],',
  30748. ' pas.system.IUnknown,',
  30749. ' function () {',
  30750. ' var $r = this.$rtti;',
  30751. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30752. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30753. ' }',
  30754. ');',
  30755. 'this.Fly = function () {',
  30756. ' var ta = null;',
  30757. ' var ti = null;',
  30758. ' ta = pas.unit2.$rtti["TWordArray"];',
  30759. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30760. ' ti = $mod.$rtti["IBird"];',
  30761. '};',
  30762. '']),
  30763. LinesToStr([ // $mod.$main
  30764. '']));
  30765. end;
  30766. procedure TTestModule.TestResourcestringProgram;
  30767. begin
  30768. AddModuleWithIntfImplSrc('unit2.pas',
  30769. LinesToStr([
  30770. 'resourcestring Title = ''Nice'';',
  30771. '']),
  30772. '');
  30773. StartProgram(true);
  30774. Add([
  30775. 'uses unit2;',
  30776. 'const Bar = ''bar'';',
  30777. 'resourcestring',
  30778. ' Red = ''red'';',
  30779. ' Foobar = ''fOo''+bar;',
  30780. 'var s: string;',
  30781. ' c: char;',
  30782. 'begin',
  30783. ' s:=red;',
  30784. ' s:=test1.red;',
  30785. ' s:=Title;',
  30786. ' c:=red[1];',
  30787. ' c:=test1.red[2];',
  30788. ' if red=foobar then ;',
  30789. ' if red[3]=red[4] then ;']);
  30790. ConvertProgram;
  30791. CheckSource('TestResourcestringProgram',
  30792. LinesToStr([ // statements
  30793. 'this.Bar = "bar";',
  30794. 'this.s = "";',
  30795. 'this.c = "";',
  30796. '$mod.$resourcestrings = {',
  30797. ' Red: {',
  30798. ' org: "red"',
  30799. ' },',
  30800. ' Foobar: {',
  30801. ' org: "fOobar"',
  30802. ' }',
  30803. '};',
  30804. '']),
  30805. LinesToStr([ // $mod.$main
  30806. '$mod.s = rtl.getResStr($mod, "Red");',
  30807. '$mod.s = rtl.getResStr($mod, "Red");',
  30808. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30809. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30810. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30811. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30812. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30813. '']));
  30814. end;
  30815. procedure TTestModule.TestResourcestringUnit;
  30816. begin
  30817. AddModuleWithIntfImplSrc('unit2.pas',
  30818. LinesToStr([
  30819. 'resourcestring Title = ''Nice'';',
  30820. '']),
  30821. '');
  30822. StartUnit(true);
  30823. Add([
  30824. 'interface',
  30825. 'uses unit2;',
  30826. 'const Red = ''rEd'';',
  30827. 'resourcestring',
  30828. ' Blue = ''blue'';',
  30829. ' NotRed = ''not''+Red;',
  30830. 'var s: string;',
  30831. 'implementation',
  30832. 'resourcestring',
  30833. ' ImplGreen = ''green'';',
  30834. 'initialization',
  30835. ' s:=blue+ImplGreen;',
  30836. ' s:=test1.blue+test1.implgreen;',
  30837. ' s:=blue[1]+implgreen[2];',
  30838. ' s:=Title;',
  30839. '']);
  30840. ConvertUnit;
  30841. CheckSource('TestResourcestringUnit',
  30842. LinesToStr([ // statements
  30843. 'this.Red = "rEd";',
  30844. 'this.s = "";',
  30845. '$mod.$resourcestrings = {',
  30846. ' Blue: {',
  30847. ' org: "blue"',
  30848. ' },',
  30849. ' NotRed: {',
  30850. ' org: "notrEd"',
  30851. ' },',
  30852. ' ImplGreen: {',
  30853. ' org: "green"',
  30854. ' }',
  30855. '};',
  30856. '']),
  30857. LinesToStr([ // $mod.$main
  30858. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30859. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30860. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30861. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30862. '']));
  30863. end;
  30864. procedure TTestModule.TestResourcestringImplementation;
  30865. begin
  30866. StartUnit(false);
  30867. Add([
  30868. 'interface',
  30869. 'implementation',
  30870. 'resourcestring',
  30871. ' ImplRed = ''red'';']);
  30872. ConvertUnit;
  30873. CheckSource('TestResourcestringImplementation',
  30874. LinesToStr([ // intf statements
  30875. 'var $impl = $mod.$impl;']),
  30876. LinesToStr([ // $mod.$init
  30877. '']),
  30878. LinesToStr([ // impl statements
  30879. '$mod.$resourcestrings = {',
  30880. ' ImplRed: {',
  30881. ' org: "red"',
  30882. ' }',
  30883. '};',
  30884. '']));
  30885. end;
  30886. procedure TTestModule.TestAttributes_Members;
  30887. begin
  30888. WithTypeInfo:=true;
  30889. StartProgram(false);
  30890. Add([
  30891. '{$modeswitch PrefixedAttributes}',
  30892. 'type',
  30893. ' TObject = class',
  30894. ' constructor Create;',
  30895. ' end;',
  30896. ' TCustomAttribute = class',
  30897. ' constructor Create(Id: word);',
  30898. ' end;',
  30899. ' [Missing]',
  30900. ' TBird = class',
  30901. ' published',
  30902. ' [Tcustom]',
  30903. ' FField: word;',
  30904. ' [tcustom(14)]',
  30905. ' property Size: word read FField;',
  30906. ' [Tcustom(15)]',
  30907. ' procedure Fly; virtual; abstract;',
  30908. ' end;',
  30909. ' TRec = record',
  30910. ' [Tcustom,tcustom(14)]',
  30911. ' Size: word;',
  30912. ' end;',
  30913. 'constructor TObject.Create; begin end;',
  30914. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30915. 'begin',
  30916. '']);
  30917. ConvertProgram;
  30918. CheckSource('TestAttributes_Members',
  30919. LinesToStr([ // statements
  30920. 'rtl.createClass(this, "TObject", null, function () {',
  30921. ' this.$init = function () {',
  30922. ' };',
  30923. ' this.$final = function () {',
  30924. ' };',
  30925. ' this.Create = function () {',
  30926. ' return this;',
  30927. ' };',
  30928. '});',
  30929. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30930. ' this.Create$1 = function (Id) {',
  30931. ' return this;',
  30932. ' };',
  30933. '});',
  30934. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30935. ' this.$init = function () {',
  30936. ' $mod.TObject.$init.call(this);',
  30937. ' this.FField = 0;',
  30938. ' };',
  30939. ' var $r = this.$rtti;',
  30940. ' $r.addField("FField", rtl.word, {',
  30941. ' attr: [$mod.TCustomAttribute, "Create"]',
  30942. ' });',
  30943. ' $r.addProperty(',
  30944. ' "Size",',
  30945. ' 0,',
  30946. ' rtl.word,',
  30947. ' "FField",',
  30948. ' "",',
  30949. ' {',
  30950. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30951. ' }',
  30952. ' );',
  30953. ' $r.addMethod("Fly", 0, null, null, {',
  30954. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30955. ' });',
  30956. '});',
  30957. 'rtl.recNewT(this, "TRec", function () {',
  30958. ' this.Size = 0;',
  30959. ' this.$eq = function (b) {',
  30960. ' return this.Size === b.Size;',
  30961. ' };',
  30962. ' this.$assign = function (s) {',
  30963. ' this.Size = s.Size;',
  30964. ' return this;',
  30965. ' };',
  30966. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30967. ' $r.addField("Size", rtl.word, {',
  30968. ' attr: [',
  30969. ' $mod.TCustomAttribute,',
  30970. ' "Create",',
  30971. ' $mod.TCustomAttribute,',
  30972. ' "Create$1",',
  30973. ' [14]',
  30974. ' ]',
  30975. ' });',
  30976. '});',
  30977. '']),
  30978. LinesToStr([ // $mod.$main
  30979. '']));
  30980. end;
  30981. procedure TTestModule.TestAttributes_Types;
  30982. begin
  30983. WithTypeInfo:=true;
  30984. StartProgram(false);
  30985. Add([
  30986. '{$modeswitch PrefixedAttributes}',
  30987. 'type',
  30988. ' TObject = class',
  30989. ' constructor Create(Id: word);',
  30990. ' end;',
  30991. ' TCustomAttribute = class',
  30992. ' end;',
  30993. ' [TCustom(1)]',
  30994. ' TMyClass = class',
  30995. ' end;',
  30996. ' [TCustom(11)]',
  30997. ' TMyDescendant = class(TMyClass)',
  30998. ' end;',
  30999. ' [TCustom(2)]',
  31000. ' TRec = record',
  31001. ' end;',
  31002. ' [TCustom(3)]',
  31003. ' TInt = type word;',
  31004. 'constructor TObject.Create(Id: word);',
  31005. 'begin',
  31006. 'end;',
  31007. 'var p: pointer;',
  31008. 'begin',
  31009. ' p:=typeinfo(TMyClass);',
  31010. ' p:=typeinfo(TRec);',
  31011. ' p:=typeinfo(TInt);',
  31012. '']);
  31013. ConvertProgram;
  31014. CheckSource('TestAttributes_Types',
  31015. LinesToStr([ // statements
  31016. 'rtl.createClass(this, "TObject", null, function () {',
  31017. ' this.$init = function () {',
  31018. ' };',
  31019. ' this.$final = function () {',
  31020. ' };',
  31021. ' this.Create = function (Id) {',
  31022. ' return this;',
  31023. ' };',
  31024. '});',
  31025. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31026. '});',
  31027. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31028. ' var $r = this.$rtti;',
  31029. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31030. '});',
  31031. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31032. ' var $r = this.$rtti;',
  31033. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31034. '});',
  31035. 'rtl.recNewT(this, "TRec", function () {',
  31036. ' this.$eq = function (b) {',
  31037. ' return true;',
  31038. ' };',
  31039. ' this.$assign = function (s) {',
  31040. ' return this;',
  31041. ' };',
  31042. ' $mod.$rtti.$Record("TRec", {',
  31043. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31044. ' });',
  31045. '});',
  31046. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31047. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31048. '});',
  31049. 'this.p = null;',
  31050. '']),
  31051. LinesToStr([ // $mod.$main
  31052. '$mod.p = $mod.$rtti["TMyClass"];',
  31053. '$mod.p = $mod.$rtti["TRec"];',
  31054. '$mod.p = $mod.$rtti["TInt"];',
  31055. '']));
  31056. end;
  31057. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31058. begin
  31059. WithTypeInfo:=true;
  31060. StartProgram(false);
  31061. Add([
  31062. '{$modeswitch PrefixedAttributes}',
  31063. 'type',
  31064. ' TObject = class',
  31065. ' constructor Create;',
  31066. ' end;',
  31067. ' TCustomAttribute = class',
  31068. ' end;',
  31069. ' THelper = class helper for TCustomAttribute',
  31070. ' constructor Create(Id: word);',
  31071. ' end;',
  31072. ' [TCustom(3)]',
  31073. ' TMyInt = word;',
  31074. 'constructor TObject.Create; begin end;',
  31075. 'constructor THelper.Create(Id: word); begin end;',
  31076. 'begin',
  31077. ' if typeinfo(TMyInt)=nil then ;']);
  31078. ConvertProgram;
  31079. end;
  31080. procedure TTestModule.TestAssert;
  31081. begin
  31082. StartProgram(false);
  31083. Add([
  31084. 'procedure DoIt;',
  31085. 'var',
  31086. ' b: boolean;',
  31087. ' s: string;',
  31088. 'begin',
  31089. ' {$Assertions on}',
  31090. ' Assert(b);',
  31091. 'end;',
  31092. 'begin',
  31093. ' DoIt;',
  31094. '']);
  31095. ConvertProgram;
  31096. CheckSource('TestAssert',
  31097. LinesToStr([ // statements
  31098. 'this.DoIt = function () {',
  31099. ' var b = false;',
  31100. ' var s = "";',
  31101. ' if (!b) throw "assert failed";',
  31102. '};',
  31103. '']),
  31104. LinesToStr([ // $mod.$main
  31105. '$mod.DoIt();',
  31106. '']));
  31107. end;
  31108. procedure TTestModule.TestAssert_SysUtils;
  31109. begin
  31110. AddModuleWithIntfImplSrc('SysUtils.pas',
  31111. LinesToStr([
  31112. 'type',
  31113. ' TObject = class',
  31114. ' constructor Create;',
  31115. ' end;',
  31116. ' EAssertionFailed = class',
  31117. ' constructor Create(s: string);',
  31118. ' end;',
  31119. '']),
  31120. LinesToStr([
  31121. 'constructor TObject.Create;',
  31122. 'begin end;',
  31123. 'constructor EAssertionFailed.Create(s: string);',
  31124. 'begin end;',
  31125. '']) );
  31126. StartProgram(true);
  31127. Add([
  31128. 'uses sysutils;',
  31129. 'procedure DoIt;',
  31130. 'var',
  31131. ' b: boolean;',
  31132. ' s: string;',
  31133. 'begin',
  31134. ' {$Assertions on}',
  31135. ' Assert(b);',
  31136. ' Assert(b,''msg'');',
  31137. 'end;',
  31138. 'begin',
  31139. ' DoIt;',
  31140. '']);
  31141. ConvertProgram;
  31142. CheckSource('TestAssert_SysUtils',
  31143. LinesToStr([ // statements
  31144. 'this.DoIt = function () {',
  31145. ' var b = false;',
  31146. ' var s = "";',
  31147. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31148. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31149. '};',
  31150. '']),
  31151. LinesToStr([ // $mod.$main
  31152. '$mod.DoIt();',
  31153. '']));
  31154. end;
  31155. procedure TTestModule.TestObjectChecks;
  31156. begin
  31157. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31158. StartProgram(false);
  31159. Add([
  31160. 'type',
  31161. ' TObject = class',
  31162. ' procedure DoIt;',
  31163. ' end;',
  31164. ' TClass = class of tobject;',
  31165. ' TBird = class',
  31166. ' end;',
  31167. ' TBirdClass = class of TBird;',
  31168. 'var',
  31169. ' o : TObject;',
  31170. ' c: TClass;',
  31171. ' b: TBird;',
  31172. ' bc: TBirdClass;',
  31173. 'procedure TObject.DoIt;',
  31174. 'begin',
  31175. ' b:=TBird(o);',
  31176. 'end;',
  31177. 'begin',
  31178. ' o.DoIt;',
  31179. ' b:=TBird(o);',
  31180. ' bc:=TBirdClass(c);',
  31181. '']);
  31182. ConvertProgram;
  31183. CheckSource('TestCheckMethodCall',
  31184. LinesToStr([ // statements
  31185. 'rtl.createClass(this, "TObject", null, function () {',
  31186. ' this.$init = function () {',
  31187. ' };',
  31188. ' this.$final = function () {',
  31189. ' };',
  31190. ' this.DoIt = function () {',
  31191. ' rtl.checkMethodCall(this,$mod.TObject);',
  31192. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31193. ' };',
  31194. '});',
  31195. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31196. '});',
  31197. 'this.o = null;',
  31198. 'this.c = null;',
  31199. 'this.b = null;',
  31200. 'this.bc = null;',
  31201. '']),
  31202. LinesToStr([ // $mod.$main
  31203. '$mod.o.DoIt();',
  31204. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31205. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31206. '']));
  31207. end;
  31208. procedure TTestModule.TestOverflowChecks_Int;
  31209. begin
  31210. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31211. StartProgram(false);
  31212. Add([
  31213. 'procedure DoIt;',
  31214. 'var',
  31215. ' b: byte;',
  31216. ' n: nativeint;',
  31217. ' u: nativeuint;',
  31218. ' c: currency;',
  31219. 'begin',
  31220. ' n:=n+n;',
  31221. ' n:=n-n;',
  31222. ' n:=n+b;',
  31223. ' n:=b-n;',
  31224. ' n:=n*n;',
  31225. ' n:=n*u;',
  31226. ' c:=c+b;',
  31227. ' c:=b+c;',
  31228. ' c:=c*b;',
  31229. ' c:=b*c;',
  31230. 'end;',
  31231. 'begin',
  31232. '']);
  31233. ConvertProgram;
  31234. CheckSource('TestOverflowChecks_Int',
  31235. LinesToStr([ // statements
  31236. 'this.DoIt = function () {',
  31237. ' var b = 0;',
  31238. ' var n = 0;',
  31239. ' var u = 0;',
  31240. ' var c = 0;',
  31241. ' n = rtl.oc(n + n);',
  31242. ' n = rtl.oc(n - n);',
  31243. ' n = rtl.oc(n + b);',
  31244. ' n = rtl.oc(b - n);',
  31245. ' n = rtl.oc(n * n);',
  31246. ' n = rtl.oc(n * u);',
  31247. ' c = rtl.oc(c + (b * 10000));',
  31248. ' c = rtl.oc((b * 10000) + c);',
  31249. ' c = rtl.oc(c * b);',
  31250. ' c = rtl.oc(b * c);',
  31251. '};',
  31252. '']),
  31253. LinesToStr([ // $mod.$main
  31254. '']));
  31255. end;
  31256. procedure TTestModule.TestRangeChecks_AssignInt;
  31257. begin
  31258. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31259. StartProgram(false);
  31260. Add([
  31261. '{$R+}',
  31262. 'var',
  31263. ' b: byte = 2;',
  31264. ' w: word = 3;',
  31265. 'procedure DoIt(p: byte);',
  31266. 'begin',
  31267. ' b:=w;',
  31268. ' b+=w;',
  31269. ' b:=1;',
  31270. 'end;',
  31271. '{$R-}',
  31272. 'procedure DoSome;',
  31273. 'begin',
  31274. ' DoIt(w);',
  31275. ' b:=w;',
  31276. ' b:=2;',
  31277. 'end;',
  31278. 'begin',
  31279. '{$R+}',
  31280. '']);
  31281. ConvertProgram;
  31282. CheckSource('TestRangeChecks_AssignInt',
  31283. LinesToStr([ // statements
  31284. 'this.b = 2;',
  31285. 'this.w = 3;',
  31286. 'this.DoIt = function (p) {',
  31287. ' rtl.rc(p, 0, 255);',
  31288. ' $mod.b = rtl.rc($mod.w,0,255);',
  31289. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31290. ' $mod.b = 1;',
  31291. '};',
  31292. 'this.DoSome = function () {',
  31293. ' $mod.DoIt($mod.w);',
  31294. ' $mod.b = $mod.w;',
  31295. ' $mod.b = 2;',
  31296. '};',
  31297. '']),
  31298. LinesToStr([ // $mod.$main
  31299. '']));
  31300. end;
  31301. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31302. begin
  31303. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31304. StartProgram(false);
  31305. Add([
  31306. '{$R+}',
  31307. 'type Ten = 1..10;',
  31308. 'var',
  31309. ' b: Ten = 2;',
  31310. ' w: Ten = 3;',
  31311. 'procedure DoIt(p: Ten);',
  31312. 'begin',
  31313. ' b:=w;',
  31314. ' b+=w;',
  31315. ' b:=1;',
  31316. 'end;',
  31317. '{$R-}',
  31318. 'procedure DoSome;',
  31319. 'begin',
  31320. ' DoIt(w);',
  31321. ' b:=w;',
  31322. ' b:=2;',
  31323. 'end;',
  31324. 'begin',
  31325. '{$R+}',
  31326. '']);
  31327. ConvertProgram;
  31328. CheckSource('TestRangeChecks_AssignIntRange',
  31329. LinesToStr([ // statements
  31330. 'this.b = 2;',
  31331. 'this.w = 3;',
  31332. 'this.DoIt = function (p) {',
  31333. ' rtl.rc(p, 1, 10);',
  31334. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31335. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31336. ' $mod.b = 1;',
  31337. '};',
  31338. 'this.DoSome = function () {',
  31339. ' $mod.DoIt($mod.w);',
  31340. ' $mod.b = $mod.w;',
  31341. ' $mod.b = 2;',
  31342. '};',
  31343. '']),
  31344. LinesToStr([ // $mod.$main
  31345. '']));
  31346. end;
  31347. procedure TTestModule.TestRangeChecks_AssignEnum;
  31348. begin
  31349. StartProgram(false);
  31350. Add([
  31351. '{$R+}',
  31352. 'type TEnum = (red,green);',
  31353. 'var',
  31354. ' e: TEnum = red;',
  31355. 'procedure DoIt(p: TEnum);',
  31356. 'begin',
  31357. ' e:=p;',
  31358. ' p:=TEnum(0);',
  31359. ' p:=succ(e);',
  31360. 'end;',
  31361. '{$R-}',
  31362. 'procedure DoSome;',
  31363. 'begin',
  31364. ' DoIt(e);',
  31365. ' e:=TEnum(1);',
  31366. ' e:=pred(e);',
  31367. 'end;',
  31368. 'begin',
  31369. '{$R+}',
  31370. '']);
  31371. ConvertProgram;
  31372. CheckSource('TestRangeChecks_AssignEnum',
  31373. LinesToStr([ // statements
  31374. 'this.TEnum = {',
  31375. ' "0": "red",',
  31376. ' red: 0,',
  31377. ' "1": "green",',
  31378. ' green: 1',
  31379. '};',
  31380. 'this.e = this.TEnum.red;',
  31381. 'this.DoIt = function (p) {',
  31382. ' rtl.rc(p, 0, 1);',
  31383. ' $mod.e = rtl.rc(p, 0, 1);',
  31384. ' p = 0;',
  31385. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31386. '};',
  31387. 'this.DoSome = function () {',
  31388. ' $mod.DoIt($mod.e);',
  31389. ' $mod.e = 1;',
  31390. ' $mod.e = $mod.e - 1;',
  31391. '};',
  31392. '']),
  31393. LinesToStr([ // $mod.$main
  31394. '']));
  31395. end;
  31396. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31397. begin
  31398. StartProgram(false);
  31399. Add([
  31400. '{$R+}',
  31401. 'type',
  31402. ' TEnum = (red,green);',
  31403. ' TEnumRg = red..green;',
  31404. 'var',
  31405. ' e: TEnumRg = red;',
  31406. 'procedure DoIt(p: TEnumRg);',
  31407. 'begin',
  31408. ' e:=p;',
  31409. ' p:=TEnumRg(0);',
  31410. ' p:=succ(e);',
  31411. 'end;',
  31412. '{$R-}',
  31413. 'procedure DoSome;',
  31414. 'begin',
  31415. ' DoIt(e);',
  31416. ' e:=TEnum(1);',
  31417. ' e:=pred(e);',
  31418. 'end;',
  31419. 'begin',
  31420. '{$R+}',
  31421. '']);
  31422. ConvertProgram;
  31423. CheckSource('TestRangeChecks_AssignEnumRange',
  31424. LinesToStr([ // statements
  31425. 'this.TEnum = {',
  31426. ' "0": "red",',
  31427. ' red: 0,',
  31428. ' "1": "green",',
  31429. ' green: 1',
  31430. '};',
  31431. 'this.e = this.TEnum.red;',
  31432. 'this.DoIt = function (p) {',
  31433. ' rtl.rc(p, 0, 1);',
  31434. ' $mod.e = rtl.rc(p, 0, 1);',
  31435. ' p = 0;',
  31436. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31437. '};',
  31438. 'this.DoSome = function () {',
  31439. ' $mod.DoIt($mod.e);',
  31440. ' $mod.e = 1;',
  31441. ' $mod.e = $mod.e - 1;',
  31442. '};',
  31443. '']),
  31444. LinesToStr([ // $mod.$main
  31445. '']));
  31446. end;
  31447. procedure TTestModule.TestRangeChecks_AssignChar;
  31448. begin
  31449. StartProgram(false);
  31450. Add([
  31451. '{$R+}',
  31452. 'type',
  31453. ' TLetter = char;',
  31454. 'var',
  31455. ' b: TLetter = ''2'';',
  31456. ' w: TLetter = ''3'';',
  31457. 'procedure DoIt(p: TLetter);',
  31458. 'begin',
  31459. ' b:=w;',
  31460. ' b:=''1'';',
  31461. 'end;',
  31462. '{$R-}',
  31463. 'procedure DoSome;',
  31464. 'begin',
  31465. ' DoIt(w);',
  31466. ' b:=w;',
  31467. ' b:=''2'';',
  31468. 'end;',
  31469. 'begin',
  31470. '{$R+}',
  31471. '']);
  31472. ConvertProgram;
  31473. CheckSource('TestRangeChecks_AssignChar',
  31474. LinesToStr([ // statements
  31475. 'this.b = "2";',
  31476. 'this.w = "3";',
  31477. 'this.DoIt = function (p) {',
  31478. ' rtl.rcc(p, 0, 65535);',
  31479. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31480. ' $mod.b = "1";',
  31481. '};',
  31482. 'this.DoSome = function () {',
  31483. ' $mod.DoIt($mod.w);',
  31484. ' $mod.b = $mod.w;',
  31485. ' $mod.b = "2";',
  31486. '};',
  31487. '']),
  31488. LinesToStr([ // $mod.$main
  31489. '']));
  31490. end;
  31491. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31492. begin
  31493. StartProgram(false);
  31494. Add([
  31495. '{$R+}',
  31496. 'type TDigit = ''0''..''9'';',
  31497. 'var',
  31498. ' b: TDigit = ''2'';',
  31499. ' w: TDigit = ''3'';',
  31500. 'procedure DoIt(p: TDigit);',
  31501. 'begin',
  31502. ' b:=w;',
  31503. ' b:=''1'';',
  31504. 'end;',
  31505. '{$R-}',
  31506. 'procedure DoSome;',
  31507. 'begin',
  31508. ' DoIt(w);',
  31509. ' b:=w;',
  31510. ' b:=''2'';',
  31511. 'end;',
  31512. 'begin',
  31513. '{$R+}',
  31514. '']);
  31515. ConvertProgram;
  31516. CheckSource('TestRangeChecks_AssignCharRange',
  31517. LinesToStr([ // statements
  31518. 'this.b = "2";',
  31519. 'this.w = "3";',
  31520. 'this.DoIt = function (p) {',
  31521. ' rtl.rcc(p, 48, 57);',
  31522. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31523. ' $mod.b = "1";',
  31524. '};',
  31525. 'this.DoSome = function () {',
  31526. ' $mod.DoIt($mod.w);',
  31527. ' $mod.b = $mod.w;',
  31528. ' $mod.b = "2";',
  31529. '};',
  31530. '']),
  31531. LinesToStr([ // $mod.$main
  31532. '']));
  31533. end;
  31534. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31535. begin
  31536. StartProgram(false);
  31537. Add([
  31538. '{$R+}',
  31539. 'type',
  31540. ' Ten = 1..10;',
  31541. ' TArr = array of Ten;',
  31542. ' TArrArr = array of TArr;',
  31543. ' TArrByte = array[byte] of Ten;',
  31544. ' TArrChar = array[''0''..''9''] of Ten;',
  31545. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31546. ' TObject = class',
  31547. ' A: TArr;',
  31548. ' end;',
  31549. 'procedure DoIt;',
  31550. 'var',
  31551. ' Arr: TArr;',
  31552. ' ArrArr: TArrArr;',
  31553. ' ArrByte: TArrByte;',
  31554. ' ArrChar: TArrChar;',
  31555. ' ArrByteChar: TArrByteChar;',
  31556. ' i: Ten;',
  31557. ' c: char;',
  31558. ' o: tobject;',
  31559. 'begin',
  31560. ' i:=Arr[1];',
  31561. ' i:=ArrByteChar[1,''2''];',
  31562. ' Arr[1]:=Arr[1];',
  31563. ' Arr[i]:=Arr[i];',
  31564. ' ArrByte[3]:=ArrByte[3];',
  31565. ' ArrByte[i]:=ArrByte[i];',
  31566. ' ArrChar[''5'']:=ArrChar[''5''];',
  31567. ' ArrChar[c]:=ArrChar[c];',
  31568. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31569. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31570. ' o.a[i]:=o.a[i];',
  31571. 'end;',
  31572. 'begin',
  31573. '']);
  31574. ConvertProgram;
  31575. CheckSource('TestRangeChecks_ArrayIndex',
  31576. LinesToStr([ // statements
  31577. 'rtl.createClass(this, "TObject", null, function () {',
  31578. ' this.$init = function () {',
  31579. ' this.A = [];',
  31580. ' };',
  31581. ' this.$final = function () {',
  31582. ' this.A = undefined;',
  31583. ' };',
  31584. '});',
  31585. 'this.DoIt = function () {',
  31586. ' var Arr = [];',
  31587. ' var ArrArr = [];',
  31588. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31589. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31590. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31591. ' var i = 0;',
  31592. ' var c = "";',
  31593. ' var o = null;',
  31594. ' i = rtl.rc(Arr[1], 1, 10);',
  31595. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31596. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31597. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31598. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31599. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31600. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31601. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31602. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31603. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31604. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31605. '};',
  31606. '']),
  31607. LinesToStr([ // $mod.$main
  31608. '']));
  31609. end;
  31610. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31611. begin
  31612. StartProgram(false);
  31613. Add([
  31614. '{$R+}',
  31615. 'type',
  31616. ' Ten = 1..10;',
  31617. ' TRec = record x: Ten end;',
  31618. ' TArr = array of TRec;',
  31619. ' TArrArr = array of TArr;',
  31620. ' TObject = class',
  31621. ' A: TArr;',
  31622. ' end;',
  31623. 'procedure DoIt;',
  31624. 'var',
  31625. ' Arr: TArr;',
  31626. ' ArrArr: TArrArr;',
  31627. ' i: Ten;',
  31628. ' o: tobject;',
  31629. 'begin',
  31630. ' Arr[1]:=Arr[1];',
  31631. ' Arr[i]:=Arr[i+1];',
  31632. ' o.a[i]:=o.a[i+2];',
  31633. 'end;',
  31634. 'begin',
  31635. '']);
  31636. ConvertProgram;
  31637. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31638. LinesToStr([ // statements
  31639. 'rtl.recNewT(this, "TRec", function () {',
  31640. ' this.x = 0;',
  31641. ' this.$eq = function (b) {',
  31642. ' return this.x === b.x;',
  31643. ' };',
  31644. ' this.$assign = function (s) {',
  31645. ' this.x = s.x;',
  31646. ' return this;',
  31647. ' };',
  31648. '});',
  31649. 'rtl.createClass(this, "TObject", null, function () {',
  31650. ' this.$init = function () {',
  31651. ' this.A = [];',
  31652. ' };',
  31653. ' this.$final = function () {',
  31654. ' this.A = undefined;',
  31655. ' };',
  31656. '});',
  31657. 'this.DoIt = function () {',
  31658. ' var Arr = [];',
  31659. ' var ArrArr = [];',
  31660. ' var i = 0;',
  31661. ' var o = null;',
  31662. ' Arr[1].$assign(Arr[1]);',
  31663. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31664. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31665. '};',
  31666. '']),
  31667. LinesToStr([ // $mod.$main
  31668. '']));
  31669. end;
  31670. procedure TTestModule.TestRangeChecks_StringIndex;
  31671. begin
  31672. StartProgram(false);
  31673. Add([
  31674. 'type',
  31675. ' TObject = class',
  31676. ' S: string;',
  31677. ' end;',
  31678. '{$R+}',
  31679. 'procedure DoIt(var h: string);',
  31680. 'var',
  31681. ' s: string;',
  31682. ' i: longint;',
  31683. ' c: char;',
  31684. ' o: tobject;',
  31685. 'begin',
  31686. ' c:=s[1];',
  31687. ' s[i]:=s[i];',
  31688. ' h[i]:=h[i];',
  31689. ' c:=o.s[i];',
  31690. ' o.s[i]:=c;',
  31691. 'end;',
  31692. 'begin',
  31693. '']);
  31694. ConvertProgram;
  31695. CheckSource('TestRangeChecks_StringIndex',
  31696. LinesToStr([ // statements
  31697. 'rtl.createClass(this, "TObject", null, function () {',
  31698. ' this.$init = function () {',
  31699. ' this.S = "";',
  31700. ' };',
  31701. ' this.$final = function () {',
  31702. ' };',
  31703. '});',
  31704. 'this.DoIt = function (h) {',
  31705. ' var s = "";',
  31706. ' var i = 0;',
  31707. ' var c = "";',
  31708. ' var o = null;',
  31709. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31710. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31711. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31712. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31713. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31714. '};',
  31715. '']),
  31716. LinesToStr([ // $mod.$main
  31717. '']));
  31718. end;
  31719. procedure TTestModule.TestRangeChecks_TypecastInt;
  31720. begin
  31721. StartProgram(false);
  31722. Add([
  31723. '{$R+}',
  31724. 'var',
  31725. ' i: nativeint;',
  31726. ' b: byte;',
  31727. ' sh: shortint;',
  31728. ' w: word;',
  31729. ' sm: smallint;',
  31730. ' lw: longword;',
  31731. ' li: longint;',
  31732. 'begin',
  31733. ' b:=12+byte(i);',
  31734. ' sh:=12+shortint(i);',
  31735. ' w:=12+word(i);',
  31736. ' sm:=12+smallint(i);',
  31737. ' lw:=12+longword(i);',
  31738. ' li:=12+longint(i);',
  31739. '']);
  31740. ConvertProgram;
  31741. CheckSource('TestRangeChecks_TypecastInt',
  31742. LinesToStr([
  31743. 'this.i = 0;',
  31744. 'this.b = 0;',
  31745. 'this.sh = 0;',
  31746. 'this.w = 0;',
  31747. 'this.sm = 0;',
  31748. 'this.lw = 0;',
  31749. 'this.li = 0;',
  31750. '']),
  31751. LinesToStr([
  31752. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31753. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31754. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31755. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31756. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31757. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31758. '']));
  31759. end;
  31760. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31761. begin
  31762. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31763. StartProgram(false);
  31764. Add([
  31765. '{$modeswitch typehelpers}',
  31766. '{$R+}',
  31767. 'type',
  31768. ' TObject = class',
  31769. ' FSize: byte;',
  31770. ' property Size: byte read FSize;',
  31771. ' end;',
  31772. ' THelper = type helper for byte',
  31773. ' procedure SetIt(w: word);',
  31774. ' end;',
  31775. 'procedure THelper.SetIt(w: word);',
  31776. 'begin',
  31777. ' Self:=w;',
  31778. 'end;',
  31779. 'function GetIt: byte;',
  31780. 'begin',
  31781. ' Result.SetIt(2);',
  31782. 'end;',
  31783. 'var',
  31784. ' b: byte = 3;',
  31785. ' o: TObject;',
  31786. 'begin',
  31787. ' b.SetIt(14);',
  31788. ' with b do SetIt(15);',
  31789. ' o.Size.SetIt(16);',
  31790. '']);
  31791. ConvertProgram;
  31792. CheckSource('TestRangeChecks_AssignInt',
  31793. LinesToStr([ // statements
  31794. 'rtl.createClass(this, "TObject", null, function () {',
  31795. ' this.$init = function () {',
  31796. ' this.FSize = 0;',
  31797. ' };',
  31798. ' this.$final = function () {',
  31799. ' };',
  31800. '});',
  31801. 'rtl.createHelper(this, "THelper", null, function () {',
  31802. ' this.SetIt = function (w) {',
  31803. ' rtl.rc(w, 0, 65535);',
  31804. ' this.set(w);',
  31805. ' };',
  31806. '});',
  31807. 'this.GetIt = function () {',
  31808. ' var Result = 0;',
  31809. ' $mod.THelper.SetIt.call({',
  31810. ' get: function () {',
  31811. ' return Result;',
  31812. ' },',
  31813. ' set: function (v) {',
  31814. ' rtl.rc(v, 0, 255);',
  31815. ' Result = v;',
  31816. ' }',
  31817. ' }, 2);',
  31818. ' return Result;',
  31819. '};',
  31820. 'this.b = 3;',
  31821. 'this.o = null;',
  31822. '']),
  31823. LinesToStr([ // $mod.$main
  31824. '$mod.THelper.SetIt.call({',
  31825. ' p: $mod,',
  31826. ' get: function () {',
  31827. ' return this.p.b;',
  31828. ' },',
  31829. ' set: function (v) {',
  31830. ' rtl.rc(v, 0, 255);',
  31831. ' this.p.b = v;',
  31832. ' }',
  31833. '}, 14);',
  31834. 'var $with = $mod.b;',
  31835. '$mod.THelper.SetIt.call({',
  31836. ' get: function () {',
  31837. ' return $with;',
  31838. ' },',
  31839. ' set: function (v) {',
  31840. ' rtl.rc(v, 0, 255);',
  31841. ' $with = v;',
  31842. ' }',
  31843. '}, 15);',
  31844. '$mod.THelper.SetIt.call({',
  31845. ' p: $mod.o,',
  31846. ' get: function () {',
  31847. ' return this.p.FSize;',
  31848. ' },',
  31849. ' set: function (v) {',
  31850. ' rtl.rc(v, 0, 255);',
  31851. ' this.p.FSize = v;',
  31852. ' }',
  31853. '}, 16);',
  31854. '']));
  31855. end;
  31856. procedure TTestModule.TestAsync_Proc;
  31857. begin
  31858. StartProgram(false);
  31859. Add([
  31860. 'procedure Fly(w: word = 1); async; forward;',
  31861. 'procedure Run(w: word = 2); async;',
  31862. 'begin',
  31863. ' Fly(w);',
  31864. ' Fly;',
  31865. ' await(Fly(w));',
  31866. ' await(Fly);',
  31867. 'end;',
  31868. 'procedure Fly(w: word); ',
  31869. 'begin',
  31870. 'end;',
  31871. 'begin',
  31872. ' Run;',
  31873. ' Run(3);',
  31874. '']);
  31875. CheckResolverUnexpectedHints();
  31876. ConvertProgram;
  31877. CheckSource('TestAsync_Proc',
  31878. LinesToStr([ // statements
  31879. 'this.Run = async function (w) {',
  31880. ' $mod.Fly(w);',
  31881. ' $mod.Fly(1);',
  31882. ' await $mod.Fly(w);',
  31883. ' await $mod.Fly(1);',
  31884. '};',
  31885. 'this.Fly = async function (w) {',
  31886. '};',
  31887. '']),
  31888. LinesToStr([
  31889. '$mod.Run(2);',
  31890. '$mod.Run(3);',
  31891. '']));
  31892. end;
  31893. procedure TTestModule.TestAsync_CallResultIsPromise;
  31894. begin
  31895. StartProgram(false);
  31896. Add([
  31897. '{$modeswitch externalclass}',
  31898. 'type',
  31899. ' TObject = class',
  31900. ' end;',
  31901. ' TJSPromise = class external name ''Promise''',
  31902. ' end;',
  31903. ' TBird = class',
  31904. ' function Fly: word; async; ',
  31905. ' end;',
  31906. 'function TBird.Fly: word; async; ',
  31907. 'begin',
  31908. ' Result:=3;',
  31909. ' Fly:=4+Result;',
  31910. ' if Result=5 then ;',
  31911. ' exit(6);',
  31912. 'end;',
  31913. 'function Run: word; async;',
  31914. 'begin',
  31915. ' Result:=11+Result;',
  31916. ' inc(Result);',
  31917. 'end;',
  31918. 'var',
  31919. ' p: TJSPromise;',
  31920. ' o: TBird;',
  31921. 'begin',
  31922. ' p:=Run;',
  31923. ' p:=Run();',
  31924. ' if Run=p then ;',
  31925. ' if p=Run then ;',
  31926. ' if Run()=p then ;',
  31927. ' if p=Run() then ;',
  31928. ' p:=o.Fly;',
  31929. ' p:=o.Fly();',
  31930. ' if o.Fly=p then ;',
  31931. ' if o.Fly()=p then ;',
  31932. ' with o do begin',
  31933. ' p:=Fly;',
  31934. ' p:=Fly();',
  31935. ' if Fly=p then ;',
  31936. ' if Fly()=p then ;',
  31937. ' end;',
  31938. '']);
  31939. CheckResolverUnexpectedHints();
  31940. ConvertProgram;
  31941. CheckSource('TestAsync_CallResultIsPromise',
  31942. LinesToStr([ // statements
  31943. 'rtl.createClass(this, "TObject", null, function () {',
  31944. ' this.$init = function () {',
  31945. ' };',
  31946. ' this.$final = function () {',
  31947. ' };',
  31948. '});',
  31949. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31950. ' this.Fly = async function () {',
  31951. ' var Result = 0;',
  31952. ' Result = 3;',
  31953. ' Result = 4 + Result;',
  31954. ' if (Result === 5) ;',
  31955. ' return 6;',
  31956. ' return Result;',
  31957. ' };',
  31958. '});',
  31959. 'this.Run = async function () {',
  31960. ' var Result = 0;',
  31961. ' Result = 11 + Result;',
  31962. ' Result += 1;',
  31963. ' return Result;',
  31964. '};',
  31965. 'this.p = null;',
  31966. 'this.o = null;',
  31967. '']),
  31968. LinesToStr([
  31969. '$mod.p = $mod.Run();',
  31970. '$mod.p = $mod.Run();',
  31971. 'if ($mod.Run() === $mod.p) ;',
  31972. 'if ($mod.p === $mod.Run()) ;',
  31973. 'if ($mod.Run() === $mod.p) ;',
  31974. 'if ($mod.p === $mod.Run()) ;',
  31975. '$mod.p = $mod.o.Fly();',
  31976. '$mod.p = $mod.o.Fly();',
  31977. 'if ($mod.o.Fly() === $mod.p) ;',
  31978. 'if ($mod.o.Fly() === $mod.p) ;',
  31979. 'var $with = $mod.o;',
  31980. '$mod.p = $with.Fly();',
  31981. '$mod.p = $with.Fly();',
  31982. 'if ($with.Fly() === $mod.p) ;',
  31983. 'if ($with.Fly() === $mod.p) ;',
  31984. '']));
  31985. end;
  31986. procedure TTestModule.TestAsync_ConstructorFail;
  31987. begin
  31988. StartProgram(false);
  31989. Add([
  31990. 'type',
  31991. ' TObject = class',
  31992. ' end;',
  31993. ' TBird = class',
  31994. ' constructor Create; async;',
  31995. ' end;',
  31996. 'constructor TBird.Create; async;',
  31997. 'begin',
  31998. 'end;',
  31999. 'begin',
  32000. '']);
  32001. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32002. ConvertProgram;
  32003. end;
  32004. procedure TTestModule.TestAsync_PropertyGetterFail;
  32005. begin
  32006. StartProgram(false);
  32007. Add([
  32008. 'type',
  32009. ' TObject = class',
  32010. ' end;',
  32011. ' TBird = class',
  32012. ' function GetSize: word; async;',
  32013. ' property Size: word read GetSize;',
  32014. ' end;',
  32015. 'function TBird.GetSize: word; async;',
  32016. 'begin',
  32017. 'end;',
  32018. 'begin',
  32019. '']);
  32020. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32021. ConvertProgram;
  32022. end;
  32023. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32024. begin
  32025. StartProgram(false);
  32026. Add([
  32027. 'procedure Run; async;',
  32028. 'begin',
  32029. ' await(word,1);',
  32030. 'end;',
  32031. 'begin',
  32032. '']);
  32033. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32034. ConvertProgram;
  32035. end;
  32036. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32037. begin
  32038. StartProgram(false);
  32039. Add([
  32040. 'type',
  32041. ' TObject = class',
  32042. ' end;',
  32043. ' TBird = class',
  32044. ' end;',
  32045. 'function Fly: TObject; async;',
  32046. 'begin',
  32047. 'end;',
  32048. 'procedure Run; async;',
  32049. 'begin',
  32050. ' await(TBird,Fly);',
  32051. 'end;',
  32052. 'begin',
  32053. '']);
  32054. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32055. ConvertProgram;
  32056. end;
  32057. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32058. begin
  32059. StartProgram(false);
  32060. Add([
  32061. 'procedure Crawl(w: double); ',
  32062. 'begin',
  32063. 'end;',
  32064. 'procedure Run(w: double);',
  32065. 'begin',
  32066. ' await(Crawl(w));',
  32067. 'end;',
  32068. 'begin',
  32069. ' Run(1);']);
  32070. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32071. ConvertProgram;
  32072. end;
  32073. procedure TTestModule.TestAWait_IntegerFail;
  32074. begin
  32075. StartProgram(false);
  32076. Add([
  32077. 'function Run: word;',
  32078. 'begin',
  32079. 'end;',
  32080. 'procedure Fly(w: word); async;',
  32081. 'begin',
  32082. ' await(Run());',
  32083. 'end;',
  32084. 'begin',
  32085. ' Fly(1);']);
  32086. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32087. ConvertProgram;
  32088. end;
  32089. procedure TTestModule.TestAWait_ExternalClassPromise;
  32090. begin
  32091. StartProgram(false);
  32092. Add([
  32093. '{$modeswitch externalclass}',
  32094. 'type',
  32095. ' TJSPromise = class external name ''Promise''',
  32096. ' end;',
  32097. ' TJSThenable = class external name ''Thenable''',
  32098. ' end;',
  32099. 'function Fly(w: word): TJSPromise;',
  32100. 'begin',
  32101. 'end;',
  32102. 'function Jump(w: word): word; async;',
  32103. 'begin',
  32104. 'end;',
  32105. 'function Eat(w: word): TJSPromise; async;',
  32106. 'begin',
  32107. 'end;',
  32108. 'function Run(d: double): word; async;',
  32109. 'var',
  32110. ' p: TJSPromise;',
  32111. 'begin',
  32112. ' Result:=await(word,p);', // promise needs type
  32113. ' Result:=await(word,Fly(3));', // promise needs type
  32114. ' Result:=await(Jump(4));', // async non promise must omit the type
  32115. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32116. ' Result:=await(word,Eat(6));', // promise needs type
  32117. 'end;',
  32118. 'begin',
  32119. '']);
  32120. ConvertProgram;
  32121. CheckSource('TestAWait_ExternalClassPromise',
  32122. LinesToStr([ // statements
  32123. 'this.Fly = function (w) {',
  32124. ' var Result = null;',
  32125. ' return Result;',
  32126. '};',
  32127. 'this.Jump = async function (w) {',
  32128. ' var Result = 0;',
  32129. ' return Result;',
  32130. '};',
  32131. 'this.Eat = async function (w) {',
  32132. ' var Result = null;',
  32133. ' return Result;',
  32134. '};',
  32135. 'this.Run = async function (d) {',
  32136. ' var Result = 0;',
  32137. ' var p = null;',
  32138. ' Result = await p;',
  32139. ' Result = await $mod.Fly(3);',
  32140. ' Result = await $mod.Jump(4);',
  32141. ' Result = await $mod.Jump(5);',
  32142. ' Result = await $mod.Eat(6);',
  32143. ' return Result;',
  32144. '};',
  32145. '']),
  32146. LinesToStr([
  32147. ]));
  32148. CheckResolverUnexpectedHints();
  32149. end;
  32150. procedure TTestModule.TestAWait_JSValue;
  32151. begin
  32152. StartProgram(false);
  32153. Add([
  32154. '{$modeswitch externalclass}',
  32155. 'type',
  32156. ' TJSPromise = class external name ''Promise''',
  32157. ' end;',
  32158. 'function Fly(w: word): jsvalue; async;',
  32159. 'begin',
  32160. 'end;',
  32161. 'function Run(d: jsvalue; var e): word; async;',
  32162. 'begin',
  32163. ' Result:=await(word,d);', // promise needs type
  32164. ' d:=await(Fly(4));', // async non promise must omit the type
  32165. ' Result:=await(word,e);', // promise needs type
  32166. 'end;',
  32167. 'begin',
  32168. '']);
  32169. ConvertProgram;
  32170. CheckSource('TestAWait_JSValue',
  32171. LinesToStr([ // statements
  32172. 'this.Fly = async function (w) {',
  32173. ' var Result = undefined;',
  32174. ' return Result;',
  32175. '};',
  32176. 'this.Run = async function (d, e) {',
  32177. ' var Result = 0;',
  32178. ' Result = await d;',
  32179. ' d = await $mod.Fly(4);',
  32180. ' Result = await e.get();',
  32181. ' return Result;',
  32182. '};',
  32183. '']),
  32184. LinesToStr([
  32185. ]));
  32186. CheckResolverUnexpectedHints();
  32187. end;
  32188. procedure TTestModule.TestAWait_Result;
  32189. begin
  32190. StartProgram(false);
  32191. Add([
  32192. '{$modeswitch externalclass}',
  32193. 'type',
  32194. ' TJSPromise = class external name ''Promise''',
  32195. ' end;',
  32196. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32197. 'begin',
  32198. 'end;',
  32199. 'function Run(d: double = 1.6): word; async;',
  32200. 'begin',
  32201. ' Result:=await(word,Crawl);',
  32202. ' Result:=await(word,Crawl(4.5));',
  32203. ' Result:=await(Run);',
  32204. ' Result:=await(Run(6.7));',
  32205. 'end;',
  32206. 'begin',
  32207. ' Run(1);']);
  32208. ConvertProgram;
  32209. CheckSource('TestAWait_Result',
  32210. LinesToStr([ // statements
  32211. 'this.Crawl = function (d) {',
  32212. ' var Result = null;',
  32213. ' return Result;',
  32214. '};',
  32215. 'this.Run = async function (d) {',
  32216. ' var Result = 0;',
  32217. ' Result = await $mod.Crawl(1.3);',
  32218. ' Result = await $mod.Crawl(4.5);',
  32219. ' Result = await $mod.Run(1.6);',
  32220. ' Result = await $mod.Run(6.7);',
  32221. ' return Result;',
  32222. '};',
  32223. '']),
  32224. LinesToStr([
  32225. '$mod.Run(1);'
  32226. ]));
  32227. CheckResolverUnexpectedHints();
  32228. end;
  32229. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32230. begin
  32231. StartProgram(false);
  32232. Add([
  32233. '{$mode objfpc}',
  32234. '{$modeswitch externalclass}',
  32235. 'type',
  32236. ' TJSPromise = class external name ''Promise''',
  32237. ' end;',
  32238. 'function Run: TJSPromise; async;',
  32239. 'begin',
  32240. 'end;',
  32241. 'procedure Fly(w: word); async;',
  32242. 'begin',
  32243. ' await(Run());',
  32244. 'end;',
  32245. 'begin',
  32246. ' Fly(1);']);
  32247. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32248. nWrongNumberOfParametersForCallTo);
  32249. ConvertProgram;
  32250. end;
  32251. procedure TTestModule.TestAsync_AnonymousProc;
  32252. begin
  32253. StartProgram(false);
  32254. Add([
  32255. '{$mode objfpc}',
  32256. '{$modeswitch externalclass}',
  32257. 'type',
  32258. ' TJSPromise = class external name ''Promise''',
  32259. ' end;',
  32260. 'type',
  32261. ' TFunc = reference to function(x: double): word; async;',
  32262. 'function Crawl(d: double = 1.3): word; async;',
  32263. 'begin',
  32264. 'end;',
  32265. 'var Func: TFunc;',
  32266. 'begin',
  32267. ' Func:=function(c:double):word async begin',
  32268. ' Result:=await(Crawl(c));',
  32269. ' end;',
  32270. ' Func:=function(c:double):word async assembler asm',
  32271. ' end;',
  32272. ' ']);
  32273. ConvertProgram;
  32274. CheckSource('TestAsync_AnonymousProc',
  32275. LinesToStr([ // statements
  32276. 'this.Crawl = async function (d) {',
  32277. ' var Result = 0;',
  32278. ' return Result;',
  32279. '};',
  32280. 'this.Func = null;',
  32281. '']),
  32282. LinesToStr([
  32283. '$mod.Func = async function (c) {',
  32284. ' var Result = 0;',
  32285. ' Result = await $mod.Crawl(c);',
  32286. ' return Result;',
  32287. '};',
  32288. '$mod.Func = async function (c) {',
  32289. '};',
  32290. '']));
  32291. CheckResolverUnexpectedHints();
  32292. end;
  32293. procedure TTestModule.TestAsync_ProcType;
  32294. begin
  32295. StartProgram(false);
  32296. Add([
  32297. '{$mode objfpc}',
  32298. 'type',
  32299. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32300. ' TFunc = function(x: double = 1.1): word; async;',
  32301. ' TProc = procedure(x: longint = 7); async;',
  32302. 'function Crawl(d: double): word; async;',
  32303. 'begin',
  32304. 'end;',
  32305. 'procedure Run(e:longint); async;',
  32306. 'begin',
  32307. 'end;',
  32308. 'procedure Fly(p: TProc); async;',
  32309. 'begin',
  32310. ' await(p);',
  32311. ' await(p());',
  32312. 'end;',
  32313. 'var',
  32314. ' RefFunc: TRefFunc;',
  32315. ' Func: TFunc;',
  32316. ' Proc, ProcB: TProc;',
  32317. 'begin',
  32318. ' Func:=@Crawl;',
  32319. ' RefFunc:=@Crawl;',
  32320. ' RefFunc:=function(c:double):word async begin',
  32321. ' Result:=await(RefFunc);',
  32322. ' Result:=await(RefFunc());',
  32323. ' Result:=await(Func);',
  32324. ' Result:=await(Func());',
  32325. ' await(Proc);',
  32326. ' await(Proc());',
  32327. ' await(Proc(13));',
  32328. ' end;',
  32329. ' Proc:=@Run;',
  32330. ' if Proc=ProcB then ;',
  32331. ' ']);
  32332. ConvertProgram;
  32333. CheckResolverUnexpectedHints();
  32334. CheckSource('TestAsync_ProcType',
  32335. LinesToStr([ // statements
  32336. 'this.Crawl = async function (d) {',
  32337. ' var Result = 0;',
  32338. ' return Result;',
  32339. '};',
  32340. 'this.Run = async function (e) {',
  32341. '};',
  32342. 'this.Fly = async function (p) {',
  32343. ' await p(7);',
  32344. ' await p(7);',
  32345. '};',
  32346. 'this.RefFunc = null;',
  32347. 'this.Func = null;',
  32348. 'this.Proc = null;',
  32349. 'this.ProcB = null;',
  32350. '']),
  32351. LinesToStr([
  32352. '$mod.Func = $mod.Crawl;',
  32353. '$mod.RefFunc = $mod.Crawl;',
  32354. '$mod.RefFunc = async function (c) {',
  32355. ' var Result = 0;',
  32356. ' Result = await $mod.RefFunc(1.3);',
  32357. ' Result = await $mod.RefFunc(1.3);',
  32358. ' Result = await $mod.Func(1.1);',
  32359. ' Result = await $mod.Func(1.1);',
  32360. ' await $mod.Proc(7);',
  32361. ' await $mod.Proc(7);',
  32362. ' await $mod.Proc(13);',
  32363. ' return Result;',
  32364. '};',
  32365. '$mod.Proc = $mod.Run;',
  32366. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32367. '']));
  32368. end;
  32369. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32370. begin
  32371. StartProgram(false);
  32372. Add([
  32373. '{$mode objfpc}',
  32374. 'type',
  32375. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32376. 'function Crawl(d: double): word; async;',
  32377. 'begin',
  32378. 'end;',
  32379. 'var',
  32380. ' RefFunc: TRefFunc;',
  32381. 'begin',
  32382. ' RefFunc:=@Crawl;',
  32383. ' ']);
  32384. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32385. ConvertProgram;
  32386. end;
  32387. procedure TTestModule.TestAsync_Inherited;
  32388. begin
  32389. StartProgram(false);
  32390. Add([
  32391. '{$mode objfpc}',
  32392. '{$modeswitch externalclass}',
  32393. 'type',
  32394. ' TJSPromise = class external name ''Promise''',
  32395. ' end;',
  32396. ' TObject = class',
  32397. ' function Run(w: word = 3): word; async; virtual;',
  32398. ' end;',
  32399. ' TBird = class',
  32400. ' function Run(w: word = 3): word; async; override;',
  32401. ' end;',
  32402. 'function TObject.Run(w: word = 3): word; async;',
  32403. 'begin',
  32404. 'end;',
  32405. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32406. 'var p: TJSPromise;',
  32407. 'begin',
  32408. ' p:=inherited;',
  32409. ' p:=inherited Run;',
  32410. ' p:=inherited Run();',
  32411. ' p:=inherited Run(4);',
  32412. ' exit(p);',
  32413. ' exit(inherited);',
  32414. ' exit(inherited Run);',
  32415. ' exit(inherited Run(5));',
  32416. ' exit(6);',
  32417. 'end;',
  32418. 'begin',
  32419. ' ']);
  32420. ConvertProgram;
  32421. CheckSource('TestAsync_Inherited',
  32422. LinesToStr([ // statements
  32423. 'rtl.createClass(this, "TObject", null, function () {',
  32424. ' this.$init = function () {',
  32425. ' };',
  32426. ' this.$final = function () {',
  32427. ' };',
  32428. ' this.Run = async function (w) {',
  32429. ' var Result = 0;',
  32430. ' return Result;',
  32431. ' };',
  32432. '});',
  32433. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32434. ' this.Run = async function (w) {',
  32435. ' var Result = 0;',
  32436. ' var p = null;',
  32437. ' p = $mod.TObject.Run.apply(this, arguments);',
  32438. ' p = $mod.TObject.Run.call(this, 3);',
  32439. ' p = $mod.TObject.Run.call(this, 3);',
  32440. ' p = $mod.TObject.Run.call(this, 4);',
  32441. ' return p;',
  32442. ' return $mod.TObject.Run.apply(this, arguments);',
  32443. ' return $mod.TObject.Run.call(this, 3);',
  32444. ' return $mod.TObject.Run.call(this, 5);',
  32445. ' return 6;',
  32446. ' return Result;',
  32447. ' };',
  32448. '});',
  32449. '']),
  32450. LinesToStr([
  32451. '']));
  32452. CheckResolverUnexpectedHints();
  32453. end;
  32454. procedure TTestModule.TestAsync_ClassInterface;
  32455. begin
  32456. StartProgram(false);
  32457. Add([
  32458. '{$mode objfpc}',
  32459. '{$modeswitch externalclass}',
  32460. 'type',
  32461. ' TJSPromise = class external name ''Promise''',
  32462. ' end;',
  32463. ' IUnknown = interface',
  32464. ' function _AddRef: longint;',
  32465. ' function _Release: longint;',
  32466. ' end;',
  32467. 'function Say(i: IUnknown): IUnknown; async;',
  32468. 'begin',
  32469. 'end;',
  32470. 'function Run: IUnknown; async;',
  32471. 'begin',
  32472. ' Result:=await(Run);',
  32473. ' Result:=await(Run());',
  32474. ' Result:=await(Run) as IUnknown;',
  32475. ' Result:=await(Say(nil));',
  32476. ' Result:=await(Say(await(Run())));',
  32477. ' Result:=await(Say(await(Run()) as IUnknown));',
  32478. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32479. 'end;',
  32480. 'procedure Fly;',
  32481. 'var p: TJSPromise;',
  32482. 'begin',
  32483. ' Run;',
  32484. ' Run();',
  32485. ' p:=Run;',
  32486. ' p:=Run();',
  32487. 'end;',
  32488. 'begin',
  32489. ' ']);
  32490. ConvertProgram;
  32491. CheckSource('TestAsync_ClassInterface',
  32492. LinesToStr([ // statements
  32493. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32494. 'this.Say = async function (i) {',
  32495. ' var Result = null;',
  32496. ' return Result;',
  32497. '};',
  32498. 'this.Run = async function () {',
  32499. ' var Result = null;',
  32500. ' var $ok = false;',
  32501. ' try {',
  32502. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32503. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32504. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32505. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32506. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32507. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32508. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32509. ' $ok = true;',
  32510. ' } finally {',
  32511. ' if (!$ok) rtl._Release(Result);',
  32512. ' };',
  32513. ' return Result;',
  32514. '};',
  32515. 'this.Fly = function () {',
  32516. ' var p = null;',
  32517. ' $mod.Run();',
  32518. ' $mod.Run();',
  32519. ' p = $mod.Run();',
  32520. ' p = $mod.Run();',
  32521. '};',
  32522. '']),
  32523. LinesToStr([
  32524. '']));
  32525. CheckResolverUnexpectedHints();
  32526. end;
  32527. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32528. begin
  32529. StartProgram(true,[supTInterfacedObject]);
  32530. Add([
  32531. '{$mode objfpc}',
  32532. '{$modeswitch externalclass}',
  32533. 'type',
  32534. ' TJSPromise = class external name ''Promise''',
  32535. ' end;',
  32536. ' IBird = interface',
  32537. ' procedure Run;',
  32538. ' end;',
  32539. ' TBird = class(TInterfacedObject,IBird)',
  32540. ' procedure Run; async;',
  32541. ' end;',
  32542. 'procedure TBird.Run;',
  32543. 'begin',
  32544. 'end;',
  32545. 'begin',
  32546. ' ']);
  32547. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32548. ConvertProgram;
  32549. end;
  32550. Initialization
  32551. RegisterTests([TTestModule]);
  32552. end.