tcmodules.pas 897 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208
  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. {$Optimization }
  18. interface
  19. uses
  20. Classes, SysUtils, fpcunit, testregistry, contnrs,
  21. jstree, jswriter, jsbase,
  22. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  23. FPPas2Js;
  24. const
  25. // default parser+scanner options
  26. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  27. co_tcmodules = [];
  28. type
  29. TSrcMarkerKind = (
  30. mkLabel,
  31. mkResolverReference,
  32. mkDirectReference
  33. );
  34. PSrcMarker = ^TSrcMarker;
  35. TSrcMarker = record
  36. Kind: TSrcMarkerKind;
  37. Filename: string;
  38. Row: integer;
  39. StartCol, EndCol: integer; // token start, end column
  40. Identifier: string;
  41. Next: PSrcMarker;
  42. end;
  43. TSystemUnitPart = (
  44. supTObject,
  45. supTVarRec,
  46. supTypeInfo,
  47. supTInterfacedObject,
  48. supWriteln
  49. );
  50. TSystemUnitParts = set of TSystemUnitPart;
  51. { TTestHintMessage }
  52. TTestHintMessage = class
  53. public
  54. Id: int64;
  55. MsgType: TMessageType;
  56. MsgNumber: integer;
  57. Msg: string;
  58. SourcePos: TPasSourcePos;
  59. end;
  60. { TTestPasParser }
  61. TTestPasParser = Class(TPasParser)
  62. end;
  63. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  64. { TTestEnginePasResolver }
  65. TTestEnginePasResolver = class(TPas2JsResolver)
  66. private
  67. FFilename: string;
  68. FModule: TPasModule;
  69. FOnFindUnit: TOnFindUnit;
  70. FParser: TTestPasParser;
  71. FStreamResolver: TStreamResolver;
  72. FScanner: TPas2jsPasScanner;
  73. FSource: string;
  74. public
  75. destructor Destroy; override;
  76. function FindUnit(const AName, InFilename: String; NameExpr,
  77. InFileExpr: TPasExpr): TPasModule; override;
  78. procedure UsedInterfacesFinished(Section: TPasSection); override;
  79. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  80. property Filename: string read FFilename write FFilename;
  81. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  82. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  83. property Parser: TTestPasParser read FParser write FParser;
  84. property Source: string read FSource write FSource;
  85. property Module: TPasModule read FModule;
  86. end;
  87. { TCustomTestModule }
  88. TCustomTestModule = Class(TTestCase)
  89. private
  90. FConverter: TPasToJSConverter;
  91. FEngine: TTestEnginePasResolver;
  92. FExpectedErrorClass: ExceptClass;
  93. FExpectedErrorMsg: string;
  94. FExpectedErrorNumber: integer;
  95. FFilename: string;
  96. FFileResolver: TStreamResolver;
  97. FHub: TPas2JSResolverHub;
  98. FJSImplementationUses: TJSArrayLiteral;
  99. FJSInitBody: TJSFunctionBody;
  100. FJSImplentationUses: TJSArrayLiteral;
  101. FJSInterfaceUses: TJSArrayLiteral;
  102. FJSModule: TJSSourceElements;
  103. FJSModuleSrc: TJSSourceElements;
  104. FJSSource: TStringList;
  105. FModule: TPasModule;
  106. FJSModuleCallArgs: TJSArguments;
  107. FModules: TObjectList;// list of TTestEnginePasResolver
  108. FParser: TTestPasParser;
  109. FPasProgram: TPasProgram;
  110. FPasLibrary: TPasLibrary;
  111. FHintMsgs: TObjectList; // list of TTestHintMessage
  112. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  113. FJSRegModuleCall: TJSCallExpression;
  114. FScanner: TPas2jsPasScanner;
  115. FSkipTests: boolean;
  116. FSource: TStringList;
  117. FFirstPasStatement: TPasImplBlock;
  118. FWithTypeInfo: boolean;
  119. {$IFDEF EnablePasTreeGlobalRefCount}
  120. FElementRefCountAtSetup: int64;
  121. {$ENDIF}
  122. function GetMsgCount: integer;
  123. function GetMsgs(Index: integer): TTestHintMessage;
  124. function GetResolverCount: integer;
  125. function GetResolvers(Index: integer): TTestEnginePasResolver;
  126. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  127. procedure OnParserLog(Sender: TObject; const Msg: String);
  128. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  129. procedure OnScannerLog(Sender: TObject; const Msg: String);
  130. procedure SetWithTypeInfo(const AValue: boolean);
  131. protected
  132. procedure SetUp; override;
  133. function CreateConverter: TPasToJSConverter; virtual;
  134. function LoadUnit(const aUnitName: String): TPasModule;
  135. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  136. procedure TearDown; override;
  137. Procedure Add(Line: string); virtual;
  138. Procedure Add(const Lines: array of string);
  139. Procedure StartParsing; virtual;
  140. procedure ParseModuleQueue; virtual;
  141. procedure ParseModule; virtual;
  142. procedure ParseProgram; virtual;
  143. procedure ParseLibrary; virtual;
  144. procedure ParseUnit; virtual;
  145. protected
  146. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  149. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  150. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  151. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  152. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  155. procedure ConvertModule; virtual;
  156. procedure ConvertProgram; virtual;
  157. procedure ConvertLibrary; virtual;
  158. procedure ConvertUnit; virtual;
  159. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  160. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  161. function GetDottedIdentifier(El: TJSElement): string;
  162. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  163. ImplStatements: string = ''); virtual;
  164. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  165. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  166. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  167. Msg: string; Marker: PSrcMarker = nil); virtual;
  168. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  169. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  172. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  173. function IsErrorExpected(E: Exception): boolean;
  174. procedure HandleScannerError(E: EScannerError);
  175. procedure HandleParserError(E: EParserError);
  176. procedure HandlePasResolveError(E: EPasResolve);
  177. procedure HandlePas2JSError(E: EPas2JS);
  178. procedure HandleException(E: Exception);
  179. procedure FailException(E: Exception);
  180. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  181. function IndexOfResolver(const Filename: string): integer;
  182. function GetResolver(const Filename: string): TTestEnginePasResolver;
  183. function GetDefaultNamespace: string;
  184. property PasProgram: TPasProgram Read FPasProgram;
  185. property PasLibrary: TPasLibrary Read FPasLibrary;
  186. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  187. property ResolverCount: integer read GetResolverCount;
  188. property Engine: TTestEnginePasResolver read FEngine;
  189. property Filename: string read FFilename;
  190. Property Module: TPasModule Read FModule;
  191. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  192. property Converter: TPasToJSConverter read FConverter;
  193. property JSSource: TStringList read FJSSource;
  194. property JSModule: TJSSourceElements read FJSModule;
  195. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  196. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  197. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  198. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  199. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  200. property JSInitBody: TJSFunctionBody read FJSInitBody;
  201. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  202. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  203. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  204. property SkipTests: boolean read FSkipTests write FSkipTests;
  205. public
  206. constructor Create; override;
  207. destructor Destroy; override;
  208. property Hub: TPas2JSResolverHub read FHub;
  209. property Source: TStringList read FSource;
  210. property FileResolver: TStreamResolver read FFileResolver;
  211. property Scanner: TPas2jsPasScanner read FScanner;
  212. property Parser: TTestPasParser read FParser;
  213. property MsgCount: integer read GetMsgCount;
  214. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  215. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  216. end;
  217. { TTestModule }
  218. TTestModule = class(TCustomTestModule)
  219. Published
  220. Procedure TestReservedWords;
  221. // program, units, includes
  222. Procedure TestEmptyProgram;
  223. Procedure TestEmptyProgramUseStrict;
  224. Procedure TestEmptyUnit;
  225. Procedure TestEmptyUnitUseStrict;
  226. Procedure TestDottedUnitNames;
  227. Procedure TestDottedUnitNameImpl;
  228. Procedure TestDottedUnitExpr;
  229. Procedure Test_ModeFPCFail;
  230. Procedure Test_ModeSwitchCBlocksFail;
  231. Procedure TestUnit_UseSystem;
  232. Procedure TestUnit_Intf1Impl2Intf1;
  233. Procedure TestIncludeVersion;
  234. // vars/const
  235. Procedure TestVarInt;
  236. Procedure TestVarBaseTypes;
  237. Procedure TestBaseTypeSingleFail;
  238. Procedure TestBaseTypeExtendedFail;
  239. Procedure TestConstBaseTypes;
  240. Procedure TestUnitImplVars;
  241. Procedure TestUnitImplConsts;
  242. Procedure TestUnitImplRecord;
  243. Procedure TestRenameJSNameConflict;
  244. Procedure TestLocalConst;
  245. Procedure TestVarExternal;
  246. Procedure TestVarExternalOtherUnit;
  247. Procedure TestVarAbsoluteFail;
  248. Procedure TestConstExternal;
  249. // numbers
  250. Procedure TestDouble;
  251. Procedure TestInteger;
  252. Procedure TestIntegerRange;
  253. Procedure TestIntegerTypecasts;
  254. Procedure TestInteger_BitwiseShrNativeInt;
  255. Procedure TestInteger_BitwiseShlNativeInt;
  256. Procedure TestInteger_SystemFunc;
  257. Procedure TestInteger_AssignOutsideConst;
  258. Procedure TestCurrency;
  259. Procedure TestForBoolDo;
  260. Procedure TestForIntDo;
  261. Procedure TestForIntInDo;
  262. // strings
  263. Procedure TestCharConst;
  264. Procedure TestChar_Compare;
  265. Procedure TestChar_BuiltInProcs;
  266. Procedure TestStringConst;
  267. Procedure TestStringConst_InvalidUTF16;
  268. Procedure TestStringConstSurrogate;
  269. Procedure TestString_Length;
  270. Procedure TestString_Compare;
  271. Procedure TestString_SetLength;
  272. Procedure TestString_CharAt;
  273. Procedure TestStringHMinusFail;
  274. Procedure TestStr;
  275. Procedure TestBaseType_AnsiStringFail;
  276. Procedure TestBaseType_WideStringFail;
  277. Procedure TestBaseType_ShortStringFail;
  278. Procedure TestBaseType_RawByteStringFail;
  279. Procedure TestTypeShortstring_Fail;
  280. Procedure TestCharSet_Custom;
  281. Procedure TestWideChar;
  282. Procedure TestForCharDo;
  283. Procedure TestForCharInDo;
  284. // alias types
  285. Procedure TestAliasTypeRef;
  286. Procedure TestTypeCast_BaseTypes;
  287. Procedure TestTypeCast_AliasBaseTypes;
  288. // functions
  289. Procedure TestEmptyProc;
  290. Procedure TestProcOneParam;
  291. Procedure TestFunctionWithoutParams;
  292. Procedure TestProcedureWithoutParams;
  293. Procedure TestPrgProcVar;
  294. Procedure TestProcTwoArgs;
  295. Procedure TestProc_DefaultValue;
  296. Procedure TestUnitProcVar;
  297. Procedure TestImplProc;
  298. Procedure TestFunctionResult;
  299. Procedure TestNestedProc;
  300. Procedure TestNestedProc_ResultString;
  301. Procedure TestForwardProc;
  302. Procedure TestNestedForwardProc;
  303. Procedure TestAssignFunctionResult;
  304. Procedure TestFunctionResultInCondition;
  305. Procedure TestFunctionResultInForLoop;
  306. Procedure TestFunctionResultInTypeCast;
  307. Procedure TestExit;
  308. Procedure TestExit_ResultInFinally;
  309. Procedure TestBreak;
  310. Procedure TestBreakAsVar;
  311. Procedure TestContinue;
  312. Procedure TestProc_External;
  313. Procedure TestProc_ExternalOtherUnit;
  314. Procedure TestProc_Asm;
  315. Procedure TestProc_AsmSubBlock;
  316. Procedure TestProc_Assembler;
  317. Procedure TestProc_VarParam;
  318. Procedure TestProc_VarParamString;
  319. Procedure TestProc_VarParamV;
  320. Procedure TestProc_Overload;
  321. Procedure TestProc_OverloadForward;
  322. Procedure TestProc_OverloadIntfImpl;
  323. Procedure TestProc_OverloadNested;
  324. Procedure TestProc_OverloadNestedForward;
  325. Procedure TestProc_OverloadUnitCycle;
  326. Procedure TestProc_Varargs;
  327. Procedure TestProc_ConstOrder;
  328. Procedure TestProc_DuplicateConst;
  329. Procedure TestProc_LocalVarAbsolute;
  330. Procedure TestProc_LocalVarInit;
  331. Procedure TestProc_ReservedWords;
  332. Procedure TestProc_ConstRefWord;
  333. // anonymous functions
  334. Procedure TestAnonymousProc_Assign_ObjFPC;
  335. Procedure TestAnonymousProc_Assign_Delphi;
  336. Procedure TestAnonymousProc_Arg;
  337. Procedure TestAnonymousProc_Typecast;
  338. Procedure TestAnonymousProc_With;
  339. Procedure TestAnonymousProc_ExceptOn;
  340. Procedure TestAnonymousProc_Nested;
  341. Procedure TestAnonymousProc_NestedAssignResult;
  342. Procedure TestAnonymousProc_Class;
  343. Procedure TestAnonymousProc_ForLoop;
  344. Procedure TestAnonymousProc_AsmDelphi;
  345. // enums, sets
  346. Procedure TestEnum_Name;
  347. Procedure TestEnum_Number;
  348. Procedure TestEnum_ConstFail;
  349. Procedure TestEnum_Functions;
  350. Procedure TestEnumRg_Functions;
  351. Procedure TestEnum_AsParams;
  352. Procedure TestEnumRange_Array;
  353. Procedure TestEnum_ForIn;
  354. Procedure TestEnum_ScopedNumber;
  355. Procedure TestEnum_InFunction;
  356. Procedure TestEnum_Name_Anonymous_Unit;
  357. Procedure TestSet_Enum;
  358. Procedure TestSet_Operators;
  359. Procedure TestSet_Operator_In;
  360. Procedure TestSet_Functions;
  361. Procedure TestSet_PassAsArgClone;
  362. Procedure TestSet_AsParams;
  363. Procedure TestSet_Property;
  364. Procedure TestSet_EnumConst;
  365. Procedure TestSet_IntConst;
  366. Procedure TestSet_IntRange;
  367. Procedure TestSet_AnonymousEnumType;
  368. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  369. Procedure TestSet_ConstEnum;
  370. Procedure TestSet_ConstChar;
  371. Procedure TestSet_ConstInt;
  372. Procedure TestSet_InFunction;
  373. Procedure TestSet_ForIn;
  374. // statements
  375. Procedure TestNestBegin;
  376. Procedure TestIncDec;
  377. Procedure TestLoHiFpcMode;
  378. Procedure TestLoHiDelphiMode;
  379. Procedure TestAssignments;
  380. Procedure TestArithmeticOperators1;
  381. Procedure TestMultiAdd;
  382. Procedure TestLogicalOperators;
  383. Procedure TestBitwiseOperators;
  384. Procedure TestBitwiseOperatorsLongword;
  385. Procedure TestFunctionInt;
  386. Procedure TestFunctionString;
  387. Procedure TestIfThen;
  388. Procedure TestForLoop;
  389. Procedure TestForLoopInsideFunction;
  390. Procedure TestForLoop_ReadVarAfter;
  391. Procedure TestForLoop_Nested;
  392. Procedure TestRepeatUntil;
  393. Procedure TestAsmBlock;
  394. Procedure TestAsmPas_Impl; // ToDo
  395. Procedure TestTryFinally;
  396. Procedure TestTryExcept;
  397. Procedure TestTryExcept_ReservedWords;
  398. Procedure TestIfThenRaiseElse;
  399. Procedure TestCaseOf;
  400. Procedure TestCaseOf_UseSwitch;
  401. Procedure TestCaseOfNoElse;
  402. Procedure TestCaseOfNoElse_UseSwitch;
  403. Procedure TestCaseOfRange;
  404. Procedure TestCaseOfString;
  405. Procedure TestCaseOfChar;
  406. Procedure TestCaseOfExternalClassConst;
  407. Procedure TestDebugger;
  408. // arrays
  409. Procedure TestArray_Dynamic;
  410. Procedure TestArray_Dynamic_Nil;
  411. Procedure TestArray_DynMultiDimensional;
  412. Procedure TestArray_DynamicAssign;
  413. Procedure TestArray_StaticInt;
  414. Procedure TestArray_StaticBool;
  415. Procedure TestArray_StaticChar;
  416. Procedure TestArray_StaticMultiDim;
  417. Procedure TestArray_StaticInFunction;
  418. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  419. Procedure TestArrayOfRecord;
  420. Procedure TestArray_StaticRecord;
  421. Procedure TestArrayOfSet;
  422. Procedure TestArray_DynAsParam;
  423. Procedure TestArray_StaticAsParam;
  424. Procedure TestArrayElement_AsParams;
  425. Procedure TestArrayElementFromFuncResult_AsParams;
  426. Procedure TestArrayEnumTypeRange;
  427. Procedure TestArray_SetLengthOutArg;
  428. Procedure TestArray_SetLengthProperty;
  429. Procedure TestArray_SetLengthMultiDim;
  430. Procedure TestArray_SetLengthDynOfStatic;
  431. Procedure TestArray_OpenArrayOfString;
  432. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  433. Procedure TestArray_ConstRef;
  434. Procedure TestArray_Concat;
  435. Procedure TestArray_Copy;
  436. Procedure TestArray_InsertDelete;
  437. Procedure TestArray_DynArrayConstObjFPC;
  438. Procedure TestArray_DynArrayConstDelphi;
  439. Procedure TestArray_ArrayLitAsParam;
  440. Procedure TestArray_ArrayLitMultiDimAsParam;
  441. Procedure TestArray_ArrayLitStaticAsParam;
  442. Procedure TestArray_ForInArrOfString;
  443. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  444. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  445. Procedure TestArrayOfConst_TVarRec;
  446. Procedure TestArrayOfConst_PassBaseTypes;
  447. Procedure TestArrayOfConst_PassObj;
  448. // record
  449. Procedure TestRecord_Empty;
  450. Procedure TestRecord_Var;
  451. Procedure TestRecord_VarExternal;
  452. Procedure TestRecord_WithDo;
  453. Procedure TestRecord_Assign;
  454. Procedure TestRecord_AsParams;
  455. Procedure TestRecord_ConstRef;
  456. Procedure TestRecordElement_AsParams;
  457. Procedure TestRecordElementFromFuncResult_AsParams;
  458. Procedure TestRecordElementFromWith_AsParams;
  459. Procedure TestRecord_Equal;
  460. Procedure TestRecord_JSValue;
  461. Procedure TestRecord_VariantFail;
  462. Procedure TestRecord_FieldArray;
  463. Procedure TestRecord_Const;
  464. Procedure TestRecord_TypecastFail;
  465. Procedure TestRecord_InFunction;
  466. Procedure TestRecord_AnonymousFail;
  467. // advanced record
  468. Procedure TestAdvRecord_Function;
  469. Procedure TestAdvRecord_Property;
  470. Procedure TestAdvRecord_PropertyDefault;
  471. Procedure TestAdvRecord_Property_ClassMethod;
  472. Procedure TestAdvRecord_Const;
  473. Procedure TestAdvRecord_ExternalField;
  474. Procedure TestAdvRecord_SubRecord;
  475. Procedure TestAdvRecord_SubClass;
  476. Procedure TestAdvRecord_SubInterfaceFail;
  477. Procedure TestAdvRecord_Constructor;
  478. Procedure TestAdvRecord_ClassConstructor_Program;
  479. Procedure TestAdvRecord_ClassConstructor_Unit;
  480. // classes
  481. Procedure TestClass_TObjectDefaultConstructor;
  482. Procedure TestClass_TObjectConstructorWithParams;
  483. Procedure TestClass_TObjectConstructorWithDefaultParam;
  484. Procedure TestClass_Var;
  485. Procedure TestClass_Method;
  486. Procedure TestClass_Implementation;
  487. Procedure TestClass_Inheritance;
  488. Procedure TestClass_TypeAlias;
  489. Procedure TestClass_AbstractMethod;
  490. Procedure TestClass_CallInherited_ProcNoParams;
  491. Procedure TestClass_CallInherited_WithParams;
  492. Procedure TestClasS_CallInheritedConstructor;
  493. Procedure TestClass_ClassVar_Assign;
  494. Procedure TestClass_CallClassMethod;
  495. Procedure TestClass_CallClassMethodStatic;
  496. Procedure TestClass_Property;
  497. Procedure TestClass_Property_ClassMethod;
  498. Procedure TestClass_Property_ClassMethodStatic;
  499. Procedure TestClass_Property_Indexed;
  500. Procedure TestClass_Property_IndexSpec;
  501. Procedure TestClass_PropertyOfTypeArray;
  502. Procedure TestClass_PropertyDefault;
  503. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  504. //Procedure TestClass_PropertyDefault;
  505. Procedure TestClass_PropertyOverride;
  506. Procedure TestClass_PropertyIncVisibility;
  507. Procedure TestClass_Assigned;
  508. Procedure TestClass_WithClassDoCreate;
  509. Procedure TestClass_WithClassInstDoProperty;
  510. Procedure TestClass_WithClassInstDoPropertyWithParams;
  511. Procedure TestClass_WithClassInstDoFunc;
  512. Procedure TestClass_TypeCast;
  513. Procedure TestClass_TypeCastUntypedParam;
  514. Procedure TestClass_Overloads;
  515. Procedure TestClass_OverloadsAncestor;
  516. Procedure TestClass_OverloadConstructor;
  517. Procedure TestClass_OverloadDelphiOverride;
  518. Procedure TestClass_ReintroduceVarDelphi;
  519. Procedure TestClass_ReintroducedVar;
  520. Procedure TestClass_RaiseDescendant;
  521. Procedure TestClass_ExternalMethod;
  522. Procedure TestClass_ExternalVirtualNameMismatchFail;
  523. Procedure TestClass_ExternalOverrideFail;
  524. Procedure TestClass_ExternalVar;
  525. Procedure TestClass_Const;
  526. Procedure TestClass_ConstEnum;
  527. Procedure TestClass_LocalConstDuplicate_Prg;
  528. Procedure TestClass_LocalConstDuplicate_Unit;
  529. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  530. Procedure TestClass_LocalVarSelfFail;
  531. Procedure TestClass_ArgSelfFail;
  532. Procedure TestClass_NestedProcSelf;
  533. Procedure TestClass_NestedProcSelf2;
  534. Procedure TestClass_NestedProcClassSelf;
  535. Procedure TestClass_NestedProcCallInherited;
  536. Procedure TestClass_TObjectFree;
  537. Procedure TestClass_TObjectFree_VarArg;
  538. Procedure TestClass_TObjectFreeNewInstance;
  539. Procedure TestClass_TObjectFreeLowerCase;
  540. Procedure TestClass_TObjectFreeFunctionFail;
  541. Procedure TestClass_TObjectFreePropertyFail;
  542. Procedure TestClass_ForIn;
  543. Procedure TestClass_DispatchMessage;
  544. Procedure TestClass_Message_DuplicateIntFail;
  545. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  546. // class of
  547. Procedure TestClassOf_Create;
  548. Procedure TestClassOf_Call;
  549. Procedure TestClassOf_Assign;
  550. Procedure TestClassOf_Is;
  551. Procedure TestClassOf_Compare;
  552. Procedure TestClassOf_ClassVar;
  553. Procedure TestClassOf_ClassMethod;
  554. Procedure TestClassOf_ClassProperty;
  555. Procedure TestClassOf_ClassMethodSelf;
  556. Procedure TestClassOf_TypeCast;
  557. Procedure TestClassOf_ImplicitFunctionCall;
  558. Procedure TestClassOf_Const;
  559. // nested class
  560. Procedure TestNestedClass_Alias;
  561. Procedure TestNestedClass_Record;
  562. Procedure TestNestedClass_Class;
  563. // external class
  564. Procedure TestExternalClass_Var;
  565. Procedure TestExternalClass_Const;
  566. Procedure TestExternalClass_Dollar;
  567. Procedure TestExternalClass_DuplicateVarFail;
  568. Procedure TestExternalClass_Method;
  569. Procedure TestExternalClass_ClassMethod;
  570. Procedure TestExternalClass_ClassMethodStatic;
  571. Procedure TestExternalClass_FunctionResultInTypeCast;
  572. Procedure TestExternalClass_NonExternalOverride;
  573. Procedure TestExternalClass_OverloadHint;
  574. Procedure TestExternalClass_SameNamePublishedProperty;
  575. Procedure TestExternalClass_Property;
  576. Procedure TestExternalClass_PropertyDate;
  577. Procedure TestExternalClass_ClassProperty;
  578. Procedure TestExternalClass_ClassOf;
  579. Procedure TestExternalClass_ClassOtherUnit;
  580. Procedure TestExternalClass_Is;
  581. Procedure TestExternalClass_As;
  582. Procedure TestExternalClass_DestructorFail;
  583. Procedure TestExternalClass_New;
  584. Procedure TestExternalClass_ClassOf_New;
  585. Procedure TestExternalClass_FuncClassOf_New;
  586. Procedure TestExternalClass_New_PasClassFail;
  587. Procedure TestExternalClass_New_PasClassBracketsFail;
  588. Procedure TestExternalClass_NewExtName;
  589. Procedure TestExternalClass_Constructor;
  590. Procedure TestExternalClass_ConstructorBrackets;
  591. Procedure TestExternalClass_LocalConstSameName;
  592. Procedure TestExternalClass_ReintroduceOverload;
  593. Procedure TestExternalClass_Inherited;
  594. Procedure TestExternalClass_PascalAncestorFail;
  595. Procedure TestExternalClass_NewInstance;
  596. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  597. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  598. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  599. Procedure TestExternalClass_JSFunctionPasDescendant;
  600. Procedure TestExternalClass_PascalProperty;
  601. Procedure TestExternalClass_TypeCastToRootClass;
  602. Procedure TestExternalClass_TypeCastToJSObject;
  603. Procedure TestExternalClass_TypeCastStringToExternalString;
  604. Procedure TestExternalClass_TypeCastToJSFunction;
  605. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  606. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  607. Procedure TestExternalClass_BracketAccessor;
  608. Procedure TestExternalClass_BracketAccessor_Call;
  609. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  610. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  611. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  612. Procedure TestExternalClass_BracketAccessor_MultiType;
  613. Procedure TestExternalClass_BracketAccessor_Index;
  614. Procedure TestExternalClass_ForInJSObject;
  615. Procedure TestExternalClass_ForInJSArray;
  616. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  617. Procedure TestExternalClass_NestedConstructor;
  618. // class interfaces
  619. Procedure TestClassInterface_Corba;
  620. Procedure TestClassInterface_ProcExternalFail;
  621. Procedure TestClassInterface_Overloads;
  622. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  623. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  624. Procedure TestClassInterface_AncestorImpl;
  625. Procedure TestClassInterface_ImplReintroduce;
  626. Procedure TestClassInterface_MethodResolution;
  627. Procedure TestClassInterface_AncestorMoreInterfaces;
  628. Procedure TestClassInterface_MethodOverride;
  629. Procedure TestClassInterface_Corba_Delegation;
  630. Procedure TestClassInterface_Corba_DelegationStatic;
  631. Procedure TestClassInterface_Corba_Operators;
  632. Procedure TestClassInterface_Corba_Args;
  633. Procedure TestClassInterface_Corba_ForIn;
  634. Procedure TestClassInterface_COM_AssignVar;
  635. Procedure TestClassInterface_COM_AssignArg;
  636. Procedure TestClassInterface_COM_FunctionResult;
  637. Procedure TestClassInterface_COM_InheritedFuncResult;
  638. Procedure TestClassInterface_COM_IsAsTypeCasts;
  639. Procedure TestClassInterface_COM_PassAsArg;
  640. Procedure TestClassInterface_COM_PassToUntypedParam;
  641. Procedure TestClassInterface_COM_FunctionInExpr;
  642. Procedure TestClassInterface_COM_Property;
  643. Procedure TestClassInterface_COM_IntfProperty;
  644. Procedure TestClassInterface_COM_Delegation;
  645. Procedure TestClassInterface_COM_With;
  646. Procedure TestClassInterface_COM_ForIn;
  647. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  648. Procedure TestClassInterface_COM_RecordIntfFail;
  649. Procedure TestClassInterface_COM_UnitInitialization;
  650. Procedure TestClassInterface_GUID;
  651. Procedure TestClassInterface_GUIDProperty;
  652. // helpers
  653. Procedure TestClassHelper_ClassVar;
  654. Procedure TestClassHelper_Method_AccessInstanceFields;
  655. Procedure TestClassHelper_Method_Call;
  656. Procedure TestClassHelper_Method_Nested_Call;
  657. Procedure TestClassHelper_ClassMethod_Call;
  658. Procedure TestClassHelper_ClassOf;
  659. Procedure TestClassHelper_MethodRefObjFPC;
  660. Procedure TestClassHelper_Constructor;
  661. Procedure TestClassHelper_InheritedObjFPC;
  662. Procedure TestClassHelper_Property;
  663. Procedure TestClassHelper_Property_Array;
  664. Procedure TestClassHelper_Property_Array_Default;
  665. Procedure TestClassHelper_Property_Array_DefaultDefault;
  666. Procedure TestClassHelper_ClassProperty;
  667. Procedure TestClassHelper_ClassPropertyStatic;
  668. Procedure TestClassHelper_ClassProperty_Array;
  669. Procedure TestClassHelper_ForIn;
  670. Procedure TestClassHelper_PassProperty;
  671. Procedure TestExtClassHelper_ClassVar;
  672. Procedure TestExtClassHelper_Method_Call;
  673. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  674. Procedure TestRecordHelper_ClassVar;
  675. Procedure TestRecordHelper_Method_Call;
  676. Procedure TestRecordHelper_Constructor;
  677. Procedure TestTypeHelper_ClassVar;
  678. Procedure TestTypeHelper_PassResultElement;
  679. Procedure TestTypeHelper_PassArgs;
  680. Procedure TestTypeHelper_PassVarConst;
  681. Procedure TestTypeHelper_PassFuncResult;
  682. Procedure TestTypeHelper_PassPropertyField;
  683. Procedure TestTypeHelper_PassPropertyGetter;
  684. Procedure TestTypeHelper_PassClassPropertyField;
  685. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  686. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  687. Procedure TestTypeHelper_Property;
  688. Procedure TestTypeHelper_Property_Array;
  689. Procedure TestTypeHelper_ClassProperty;
  690. Procedure TestTypeHelper_ClassProperty_Array;
  691. Procedure TestTypeHelper_ClassMethod;
  692. Procedure TestTypeHelper_ExtClassMethodFail;
  693. Procedure TestTypeHelper_Constructor;
  694. Procedure TestTypeHelper_Word;
  695. Procedure TestTypeHelper_Boolean;
  696. Procedure TestTypeHelper_WordBool;
  697. Procedure TestTypeHelper_Double;
  698. Procedure TestTypeHelper_NativeInt;
  699. Procedure TestTypeHelper_StringChar;
  700. Procedure TestTypeHelper_JSValue;
  701. Procedure TestTypeHelper_Array;
  702. Procedure TestTypeHelper_EnumType;
  703. Procedure TestTypeHelper_SetType;
  704. Procedure TestTypeHelper_InterfaceType;
  705. Procedure TestTypeHelper_NestedSelf;
  706. // proc types
  707. Procedure TestProcType;
  708. Procedure TestProcType_Arg;
  709. Procedure TestProcType_FunctionFPC;
  710. Procedure TestProcType_FunctionDelphi;
  711. Procedure TestProcType_ProcedureDelphi;
  712. Procedure TestProcType_AsParam;
  713. Procedure TestProcType_MethodFPC;
  714. Procedure TestProcType_MethodDelphi;
  715. Procedure TestProcType_PropertyFPC;
  716. Procedure TestProcType_PropertyDelphi;
  717. Procedure TestProcType_WithClassInstDoPropertyFPC;
  718. Procedure TestProcType_Nested;
  719. Procedure TestProcType_NestedOfObject;
  720. Procedure TestProcType_ReferenceToProc;
  721. Procedure TestProcType_ReferenceToMethod;
  722. Procedure TestProcType_Typecast;
  723. Procedure TestProcType_PassProcToUntyped;
  724. Procedure TestProcType_PassProcToArray;
  725. Procedure TestProcType_SafeCallObjFPC;
  726. Procedure TestProcType_SafeCallDelphi;
  727. // pointer
  728. Procedure TestPointer;
  729. Procedure TestPointer_Proc;
  730. Procedure TestPointer_AssignRecordFail;
  731. Procedure TestPointer_AssignStaticArrayFail;
  732. Procedure TestPointer_TypeCastJSValueToPointer;
  733. Procedure TestPointer_NonRecordFail;
  734. Procedure TestPointer_AnonymousArgTypeFail;
  735. Procedure TestPointer_AnonymousVarTypeFail;
  736. Procedure TestPointer_AnonymousResultTypeFail;
  737. Procedure TestPointer_AddrOperatorFail;
  738. Procedure TestPointer_ArrayParamsFail;
  739. Procedure TestPointer_PointerAddFail;
  740. Procedure TestPointer_IncPointerFail;
  741. Procedure TestPointer_Record;
  742. Procedure TestPointer_RecordArg;
  743. // jsvalue
  744. Procedure TestJSValue_AssignToJSValue;
  745. Procedure TestJSValue_TypeCastToBaseType;
  746. Procedure TestJSValue_TypecastToJSValue;
  747. Procedure TestJSValue_Equal;
  748. Procedure TestJSValue_If;
  749. Procedure TestJSValue_Not;
  750. Procedure TestJSValue_Enum;
  751. Procedure TestJSValue_ClassInstance;
  752. Procedure TestJSValue_ClassOf;
  753. Procedure TestJSValue_ArrayOfJSValue;
  754. Procedure TestJSValue_ArrayLit;
  755. Procedure TestJSValue_Params;
  756. Procedure TestJSValue_UntypedParam;
  757. Procedure TestJSValue_FuncResultType;
  758. Procedure TestJSValue_ProcType_Assign;
  759. Procedure TestJSValue_ProcType_Equal;
  760. Procedure TestJSValue_ProcType_Param;
  761. Procedure TestJSValue_AssignToPointerFail;
  762. Procedure TestJSValue_OverloadDouble;
  763. Procedure TestJSValue_OverloadNativeInt;
  764. Procedure TestJSValue_OverloadWord;
  765. Procedure TestJSValue_OverloadString;
  766. Procedure TestJSValue_OverloadChar;
  767. Procedure TestJSValue_OverloadPointer;
  768. Procedure TestJSValue_ForIn;
  769. // RTTI
  770. Procedure TestRTTI_IntRange;
  771. Procedure TestRTTI_Double;
  772. Procedure TestRTTI_ProcType;
  773. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  774. Procedure TestRTTI_EnumAndSetType;
  775. Procedure TestRTTI_EnumRange;
  776. Procedure TestRTTI_AnonymousEnumType;
  777. Procedure TestRTTI_StaticArray;
  778. Procedure TestRTTI_DynArray;
  779. Procedure TestRTTI_ArrayNestedAnonymous;
  780. Procedure TestRTTI_PublishedMethodOverloadFail;
  781. Procedure TestRTTI_PublishedMethodHideNoHint;
  782. Procedure TestRTTI_PublishedMethodExternalFail;
  783. Procedure TestRTTI_PublishedClassPropertyFail;
  784. Procedure TestRTTI_PublishedClassFieldFail;
  785. Procedure TestRTTI_PublishedFieldExternalFail;
  786. Procedure TestRTTI_Class_Field;
  787. Procedure TestRTTI_Class_Method;
  788. Procedure TestRTTI_Class_MethodArgFlags;
  789. Procedure TestRTTI_Class_Property;
  790. Procedure TestRTTI_Class_PropertyParams;
  791. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  792. Procedure TestRTTI_Class_OmitRTTI;
  793. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  794. Procedure TestRTTI_IndexModifier;
  795. Procedure TestRTTI_StoredModifier;
  796. Procedure TestRTTI_DefaultValue;
  797. Procedure TestRTTI_DefaultValueSet;
  798. Procedure TestRTTI_DefaultValueRangeType;
  799. Procedure TestRTTI_DefaultValueInherit;
  800. Procedure TestRTTI_OverrideMethod;
  801. Procedure TestRTTI_ReintroduceMethod;
  802. Procedure TestRTTI_OverloadProperty;
  803. // ToDo: array argument
  804. Procedure TestRTTI_ClassForward;
  805. Procedure TestRTTI_ClassOf;
  806. Procedure TestRTTI_Record;
  807. Procedure TestRTTI_RecordAnonymousArray;
  808. Procedure TestRTTI_Record_ClassVarType;
  809. Procedure TestRTTI_LocalTypes;
  810. Procedure TestRTTI_TypeInfo_BaseTypes;
  811. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  812. Procedure TestRTTI_TypeInfo_LocalFail;
  813. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  814. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  815. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  816. Procedure TestRTTI_TypeInfo_FunctionClassType;
  817. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  818. Procedure TestRTTI_Interface_Corba;
  819. Procedure TestRTTI_Interface_COM;
  820. Procedure TestRTTI_ClassHelper;
  821. Procedure TestRTTI_ExternalClass;
  822. Procedure TestRTTI_Unit;
  823. // Resourcestring
  824. Procedure TestResourcestringProgram;
  825. Procedure TestResourcestringUnit;
  826. Procedure TestResourcestringImplementation;
  827. // Attributes
  828. Procedure TestAttributes_Members;
  829. Procedure TestAttributes_Types;
  830. Procedure TestAttributes_HelperConstructor_Fail;
  831. Procedure TestAttributes_InterfacesList;
  832. // Assertions, checks
  833. procedure TestAssert;
  834. procedure TestAssert_SysUtils;
  835. procedure TestObjectChecks;
  836. procedure TestOverflowChecks_Int;
  837. procedure TestRangeChecks_AssignInt;
  838. procedure TestRangeChecks_AssignIntRange;
  839. procedure TestRangeChecks_AssignEnum;
  840. procedure TestRangeChecks_AssignEnumRange;
  841. procedure TestRangeChecks_AssignChar;
  842. procedure TestRangeChecks_AssignCharRange;
  843. procedure TestRangeChecks_ArrayIndex;
  844. procedure TestRangeChecks_ArrayOfRecIndex;
  845. procedure TestRangeChecks_StringIndex;
  846. procedure TestRangeChecks_TypecastInt;
  847. procedure TestRangeChecks_TypeHelperInt;
  848. // Async/AWait
  849. Procedure TestAsync_Proc;
  850. Procedure TestAsync_CallResultIsPromise;
  851. Procedure TestAsync_ConstructorFail;
  852. Procedure TestAsync_PropertyGetterFail;
  853. Procedure TestAwait_NonPromiseWithTypeFail;
  854. Procedure TestAwait_AsyncCallTypeMismatch;
  855. Procedure TestAWait_OutsideAsyncFail;
  856. Procedure TestAWait_IntegerFail;
  857. Procedure TestAWait_ExternalClassPromise;
  858. Procedure TestAWait_JSValue;
  859. Procedure TestAWait_Result;
  860. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  861. Procedure TestAsync_AnonymousProc;
  862. Procedure TestAsync_ProcType;
  863. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  864. Procedure TestAsync_Inherited;
  865. Procedure TestAsync_ClassInterface;
  866. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  867. Procedure TestAWait_ClassAs;
  868. // Library
  869. Procedure TestLibrary_Empty;
  870. Procedure TestLibrary_ExportFunc;
  871. Procedure TestLibrary_Export_Index_Fail;
  872. Procedure TestLibrary_ExportVar;
  873. Procedure TestLibrary_ExportUnitFunc;
  874. // ToDo: test delayed specialization init
  875. // ToDo: analyzer
  876. end;
  877. function LinesToStr(Args: array of const): string;
  878. function ExtractFileUnitName(aFilename: string): string;
  879. function JSToStr(El: TJSElement): string;
  880. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  881. implementation
  882. function LinesToStr(Args: array of const): string;
  883. var
  884. s: String;
  885. i: Integer;
  886. begin
  887. s:='';
  888. for i:=Low(Args) to High(Args) do
  889. case Args[i].VType of
  890. vtChar: s += Args[i].VChar+LineEnding;
  891. vtString: s += Args[i].VString^+LineEnding;
  892. vtPChar: s += Args[i].VPChar+LineEnding;
  893. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  894. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  895. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  896. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  897. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  898. end;
  899. Result:=s;
  900. end;
  901. function ExtractFileUnitName(aFilename: string): string;
  902. var
  903. p: Integer;
  904. begin
  905. Result:=ExtractFileName(aFilename);
  906. if Result='' then exit;
  907. for p:=length(Result) downto 1 do
  908. case Result[p] of
  909. '/','\': exit;
  910. '.':
  911. begin
  912. Delete(Result,p,length(Result));
  913. exit;
  914. end;
  915. end;
  916. end;
  917. function JSToStr(El: TJSElement): string;
  918. var
  919. aWriter: TBufferWriter;
  920. aJSWriter: TJSWriter;
  921. begin
  922. aJSWriter:=nil;
  923. aWriter:=TBufferWriter.Create(1000);
  924. try
  925. aJSWriter:=TJSWriter.Create(aWriter);
  926. aJSWriter.IndentSize:=2;
  927. aJSWriter.WriteJS(El);
  928. Result:=aWriter.AsString;
  929. finally
  930. aJSWriter.Free;
  931. aWriter.Free;
  932. end;
  933. end;
  934. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  935. // search diff, ignore changes in spaces
  936. const
  937. SpaceChars = [#9,#10,#13,' '];
  938. var
  939. ExpectedP, ActualP: PChar;
  940. function FindLineEnd(p: PChar): PChar;
  941. begin
  942. Result:=p;
  943. while not (Result^ in [#0,#10,#13]) do inc(Result);
  944. end;
  945. function FindLineStart(p, MinP: PChar): PChar;
  946. begin
  947. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  948. Result:=p;
  949. end;
  950. procedure SkipLineEnd(var p: PChar);
  951. begin
  952. if p^ in [#10,#13] then
  953. begin
  954. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  955. inc(p,2)
  956. else
  957. inc(p);
  958. end;
  959. end;
  960. function HasSpecialChar(s: string): boolean;
  961. var
  962. i: Integer;
  963. begin
  964. for i:=1 to length(s) do
  965. if s[i] in [#0..#31,#127..#255] then
  966. exit(true);
  967. Result:=false;
  968. end;
  969. function HashSpecialChars(s: string): string;
  970. var
  971. i: Integer;
  972. begin
  973. Result:='';
  974. for i:=1 to length(s) do
  975. if s[i] in [#0..#31,#127..#255] then
  976. Result:=Result+'#'+hexstr(ord(s[i]),2)
  977. else
  978. Result:=Result+s[i];
  979. end;
  980. procedure DiffFound;
  981. var
  982. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  983. ExpLine, ActLine: String;
  984. i, LineNo, DiffLineNo: Integer;
  985. begin
  986. writeln('Diff found "',Msg,'". Lines:');
  987. // write correct lines
  988. p:=PChar(Expected);
  989. LineNo:=0;
  990. DiffLineNo:=0;
  991. repeat
  992. StartPos:=p;
  993. while not (p^ in [#0,#10,#13]) do inc(p);
  994. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  995. SkipLineEnd(p);
  996. inc(LineNo);
  997. if (p<=ExpectedP) and (p^<>#0) then
  998. begin
  999. writeln('= ',ExpLine);
  1000. end else begin
  1001. // diff line
  1002. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1003. // write actual line
  1004. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1005. ActLineEndP:=FindLineEnd(ActualP);
  1006. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1007. writeln('- ',ActLine);
  1008. if HasSpecialChar(ActLine) then
  1009. writeln('- ',HashSpecialChars(ActLine));
  1010. // write expected line
  1011. writeln('+ ',ExpLine);
  1012. if HasSpecialChar(ExpLine) then
  1013. writeln('- ',HashSpecialChars(ExpLine));
  1014. // write empty line with pointer ^
  1015. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1016. writeln('^');
  1017. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1018. CheckSrcDiff:=false;
  1019. // write up to three following actual lines to get some context
  1020. for i:=1 to 3 do begin
  1021. ActLineStartP:=ActLineEndP;
  1022. SkipLineEnd(ActLineStartP);
  1023. if ActLineStartP^=#0 then break;
  1024. ActLineEndP:=FindLineEnd(ActLineStartP);
  1025. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1026. writeln('~ ',ActLine);
  1027. end;
  1028. exit;
  1029. end;
  1030. until p^=#0;
  1031. writeln('DiffFound Actual:-----------------------');
  1032. writeln(Actual);
  1033. writeln('DiffFound Expected:---------------------');
  1034. writeln(Expected);
  1035. writeln('DiffFound ------------------------------');
  1036. Msg:='diff found, but lines are the same, internal error';
  1037. CheckSrcDiff:=false;
  1038. end;
  1039. var
  1040. IsSpaceNeeded: Boolean;
  1041. LastChar, Quote: Char;
  1042. begin
  1043. Result:=true;
  1044. Msg:='';
  1045. if Expected='' then Expected:=' ';
  1046. if Actual='' then Actual:=' ';
  1047. ExpectedP:=PChar(Expected);
  1048. ActualP:=PChar(Actual);
  1049. repeat
  1050. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1051. case ExpectedP^ of
  1052. #0:
  1053. begin
  1054. // check that rest of Actual has only spaces
  1055. while ActualP^ in SpaceChars do inc(ActualP);
  1056. if ActualP^<>#0 then
  1057. begin
  1058. DiffFound;
  1059. exit;
  1060. end;
  1061. exit(true);
  1062. end;
  1063. ' ',#9,#10,#13:
  1064. begin
  1065. // skip space in Expected
  1066. IsSpaceNeeded:=false;
  1067. if ExpectedP>PChar(Expected) then
  1068. LastChar:=ExpectedP[-1]
  1069. else
  1070. LastChar:=#0;
  1071. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1072. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1073. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1074. IsSpaceNeeded:=true;
  1075. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1076. begin
  1077. DiffFound;
  1078. exit;
  1079. end;
  1080. while ActualP^ in SpaceChars do inc(ActualP);
  1081. end;
  1082. '''','"':
  1083. begin
  1084. while ActualP^ in SpaceChars do inc(ActualP);
  1085. if ExpectedP^<>ActualP^ then
  1086. begin
  1087. DiffFound;
  1088. exit;
  1089. end;
  1090. Quote:=ExpectedP^;
  1091. repeat
  1092. inc(ExpectedP);
  1093. inc(ActualP);
  1094. if ExpectedP^<>ActualP^ then
  1095. begin
  1096. DiffFound;
  1097. exit;
  1098. end;
  1099. if (ExpectedP^ in [#0,#10,#13]) then
  1100. break
  1101. else if (ExpectedP^=Quote) then
  1102. begin
  1103. inc(ExpectedP);
  1104. inc(ActualP);
  1105. break;
  1106. end;
  1107. until false;
  1108. end;
  1109. else
  1110. while ActualP^ in SpaceChars do inc(ActualP);
  1111. if ExpectedP^<>ActualP^ then
  1112. begin
  1113. DiffFound;
  1114. exit;
  1115. end;
  1116. inc(ExpectedP);
  1117. inc(ActualP);
  1118. end;
  1119. until false;
  1120. end;
  1121. { TTestEnginePasResolver }
  1122. destructor TTestEnginePasResolver.Destroy;
  1123. begin
  1124. FreeAndNil(FStreamResolver);
  1125. FreeAndNil(FParser);
  1126. FreeAndNil(FScanner);
  1127. FreeAndNil(FStreamResolver);
  1128. if Module<>nil then
  1129. begin
  1130. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1131. FModule:=nil;
  1132. end;
  1133. inherited Destroy;
  1134. end;
  1135. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1136. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1137. begin
  1138. Result:=nil;
  1139. if InFilename<>'' then
  1140. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1141. if Assigned(OnFindUnit) then
  1142. Result:=OnFindUnit(AName);
  1143. if NameExpr=nil then ;
  1144. end;
  1145. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1146. begin
  1147. // do not parse recursively
  1148. // parse via the queue
  1149. if Section=nil then ;
  1150. end;
  1151. { TCustomTestModule }
  1152. function TCustomTestModule.GetMsgCount: integer;
  1153. begin
  1154. Result:=FHintMsgs.Count;
  1155. end;
  1156. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1157. begin
  1158. Result:=TTestHintMessage(FHintMsgs[Index]);
  1159. end;
  1160. function TCustomTestModule.GetResolverCount: integer;
  1161. begin
  1162. Result:=FModules.Count;
  1163. end;
  1164. function TCustomTestModule.GetResolvers(Index: integer
  1165. ): TTestEnginePasResolver;
  1166. begin
  1167. Result:=TTestEnginePasResolver(FModules[Index]);
  1168. end;
  1169. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1170. ): TPasModule;
  1171. var
  1172. DefNamespace: String;
  1173. begin
  1174. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1175. if (Pos('.',aUnitName)<1) then
  1176. begin
  1177. DefNamespace:=GetDefaultNamespace;
  1178. if DefNamespace<>'' then
  1179. begin
  1180. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1181. if Result<>nil then exit;
  1182. end;
  1183. end;
  1184. Result:=LoadUnit(aUnitName);
  1185. if Result<>nil then exit;
  1186. {$IFDEF VerbosePas2JS}
  1187. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1188. {$ENDIF}
  1189. Fail('can''t find unit "'+aUnitName+'"');
  1190. end;
  1191. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1192. var
  1193. aParser: TPasParser;
  1194. Item: TTestHintMessage;
  1195. begin
  1196. aParser:=Sender as TPasParser;
  1197. Item:=TTestHintMessage.Create;
  1198. Item.Id:=aParser.LastMsgNumber;
  1199. Item.MsgType:=aParser.LastMsgType;
  1200. Item.MsgNumber:=aParser.LastMsgNumber;
  1201. Item.Msg:=Msg;
  1202. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1203. {$IFDEF VerbosePas2JS}
  1204. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1205. {$ENDIF}
  1206. FHintMsgs.Add(Item);
  1207. end;
  1208. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1209. );
  1210. var
  1211. aResolver: TTestEnginePasResolver;
  1212. Item: TTestHintMessage;
  1213. begin
  1214. aResolver:=Sender as TTestEnginePasResolver;
  1215. Item:=TTestHintMessage.Create;
  1216. Item.Id:=aResolver.LastMsgId;
  1217. Item.MsgType:=aResolver.LastMsgType;
  1218. Item.MsgNumber:=aResolver.LastMsgNumber;
  1219. Item.Msg:=Msg;
  1220. Item.SourcePos:=aResolver.LastSourcePos;
  1221. {$IFDEF VerbosePas2JS}
  1222. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1223. {$ENDIF}
  1224. FHintMsgs.Add(Item);
  1225. end;
  1226. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1227. var
  1228. Item: TTestHintMessage;
  1229. aScanner: TPas2jsPasScanner;
  1230. begin
  1231. aScanner:=Sender as TPas2jsPasScanner;
  1232. Item:=TTestHintMessage.Create;
  1233. Item.Id:=aScanner.LastMsgNumber;
  1234. Item.MsgType:=aScanner.LastMsgType;
  1235. Item.MsgNumber:=aScanner.LastMsgNumber;
  1236. Item.Msg:=Msg;
  1237. Item.SourcePos:=aScanner.CurSourcePos;
  1238. {$IFDEF VerbosePas2JS}
  1239. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1240. {$ENDIF}
  1241. FHintMsgs.Add(Item);
  1242. end;
  1243. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1244. begin
  1245. if FWithTypeInfo=AValue then Exit;
  1246. FWithTypeInfo:=AValue;
  1247. if AValue then
  1248. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1249. else
  1250. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1251. end;
  1252. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1253. var
  1254. i: Integer;
  1255. CurEngine: TTestEnginePasResolver;
  1256. CurUnitName: String;
  1257. begin
  1258. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1259. Result:=nil;
  1260. if (Module.ClassType=TPasModule)
  1261. and (CompareText(Module.Name,aUnitName)=0) then
  1262. exit(Module);
  1263. for i:=0 to ResolverCount-1 do
  1264. begin
  1265. CurEngine:=Resolvers[i];
  1266. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1267. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1268. if CompareText(aUnitName,CurUnitName)=0 then
  1269. begin
  1270. Result:=CurEngine.Module;
  1271. if Result<>nil then exit;
  1272. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1273. FileResolver.FindSourceFile(aUnitName);
  1274. CurEngine.StreamResolver:=TStreamResolver.Create;
  1275. CurEngine.StreamResolver.OwnsStreams:=True;
  1276. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1277. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1278. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1279. InitScanner(CurEngine.Scanner);
  1280. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1281. CurEngine.Parser.Options:=po_tcmodules;
  1282. if CompareText(CurUnitName,'System')=0 then
  1283. CurEngine.Parser.ImplicitUses.Clear;
  1284. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1285. try
  1286. CurEngine.Parser.NextToken;
  1287. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1288. except
  1289. on E: Exception do
  1290. HandleException(E);
  1291. end;
  1292. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1293. Result:=CurEngine.Module;
  1294. exit;
  1295. end;
  1296. end;
  1297. end;
  1298. procedure TCustomTestModule.SetUp;
  1299. begin
  1300. {$IFDEF EnablePasTreeGlobalRefCount}
  1301. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1302. {$ENDIF}
  1303. if FModules<>nil then
  1304. begin
  1305. writeln('TCustomTestModule.SetUp FModules<>nil');
  1306. Halt;
  1307. end;
  1308. inherited SetUp;
  1309. FSkipTests:=false;
  1310. FWithTypeInfo:=false;
  1311. FSource:=TStringList.Create;
  1312. FHub:=TPas2JSResolverHub.Create(Self);
  1313. FModules:=TObjectList.Create(true);
  1314. FFilename:='test1.pp';
  1315. FFileResolver:=TStreamResolver.Create;
  1316. FFileResolver.OwnsStreams:=True;
  1317. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1318. InitScanner(FScanner);
  1319. FEngine:=AddModule(Filename);
  1320. FEngine.Scanner:=FScanner;
  1321. FScanner.Resolver:=FEngine;
  1322. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1323. FParser.OnLog:=@OnParserLog;
  1324. FEngine.Parser:=FParser;
  1325. Parser.Options:=po_tcmodules;
  1326. FModule:=Nil;
  1327. FConverter:=CreateConverter;
  1328. FExpectedErrorClass:=nil;
  1329. end;
  1330. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1331. var
  1332. Options: TPasToJsConverterOptions;
  1333. begin
  1334. Result:=TPasToJSConverter.Create;
  1335. Options:=co_tcmodules;
  1336. if WithTypeInfo then
  1337. Exclude(Options,coNoTypeInfo)
  1338. else
  1339. Include(Options,coNoTypeInfo);
  1340. Result.Options:=Options;
  1341. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1342. end;
  1343. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1344. begin
  1345. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1346. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1347. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1348. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1349. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1350. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1351. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1352. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1353. aScanner.OnLog:=@OnScannerLog;
  1354. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1355. end;
  1356. procedure TCustomTestModule.TearDown;
  1357. {$IFDEF CheckPasTreeRefCount}
  1358. var
  1359. El: TPasElement;
  1360. {$ENDIF}
  1361. var
  1362. i: Integer;
  1363. CurModule: TPasModule;
  1364. begin
  1365. FHintMsgs.Clear;
  1366. FHintMsgsGood.Clear;
  1367. FSkipTests:=false;
  1368. FWithTypeInfo:=false;
  1369. FJSRegModuleCall:=nil;
  1370. FJSModuleCallArgs:=nil;
  1371. FJSImplentationUses:=nil;
  1372. FJSInterfaceUses:=nil;
  1373. FJSModuleSrc:=nil;
  1374. FJSInitBody:=nil;
  1375. FreeAndNil(FJSSource);
  1376. FreeAndNil(FJSModule);
  1377. FreeAndNil(FConverter);
  1378. Engine.Clear;
  1379. FreeAndNil(FSource);
  1380. FreeAndNil(FFileResolver);
  1381. if FModules<>nil then
  1382. begin
  1383. for i:=0 to FModules.Count-1 do
  1384. begin
  1385. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1386. if CurModule=nil then continue;
  1387. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1388. CurModule.ReleaseUsedUnits;
  1389. end;
  1390. if FModule<>nil then
  1391. FModule.ReleaseUsedUnits;
  1392. for i:=0 to FModules.Count-1 do
  1393. begin
  1394. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1395. if CurModule=nil then continue;
  1396. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1397. end;
  1398. FreeAndNil(FModules);
  1399. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1400. FEngine:=nil;
  1401. end;
  1402. FreeAndNil(FHub);
  1403. inherited TearDown;
  1404. {$IFDEF EnablePasTreeGlobalRefCount}
  1405. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1406. begin
  1407. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1408. {$IFDEF CheckPasTreeRefCount}
  1409. El:=TPasElement.FirstRefEl;
  1410. while El<>nil do
  1411. begin
  1412. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1413. for i:=0 to El.RefIds.Count-1 do
  1414. writeln(' ',El.RefIds[i]);
  1415. El:=El.NextRefEl;
  1416. end;
  1417. {$ENDIF}
  1418. Halt;
  1419. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1420. end;
  1421. {$ENDIF}
  1422. end;
  1423. procedure TCustomTestModule.Add(Line: string);
  1424. begin
  1425. Source.Add(Line);
  1426. end;
  1427. procedure TCustomTestModule.Add(const Lines: array of string);
  1428. var
  1429. i: Integer;
  1430. begin
  1431. for i:=low(Lines) to high(Lines) do
  1432. Add(Lines[i]);
  1433. end;
  1434. procedure TCustomTestModule.StartParsing;
  1435. var
  1436. Src: String;
  1437. begin
  1438. Src:=Source.Text;
  1439. FEngine.Source:=Src;
  1440. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1441. Scanner.OpenFile(FileName);
  1442. Writeln('// Test : ',Self.TestName);
  1443. Writeln(Src);
  1444. end;
  1445. procedure TCustomTestModule.ParseModuleQueue;
  1446. var
  1447. i: Integer;
  1448. CurResolver: TTestEnginePasResolver;
  1449. Found: Boolean;
  1450. Section: TPasSection;
  1451. begin
  1452. // parse til exception or all modules finished
  1453. while not SkipTests do
  1454. begin
  1455. Found:=false;
  1456. for i:=0 to ResolverCount-1 do
  1457. begin
  1458. CurResolver:=Resolvers[i];
  1459. if CurResolver.CurrentParser=nil then continue;
  1460. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1461. continue;
  1462. CurResolver.Parser.ParseContinue;
  1463. Found:=true;
  1464. break;
  1465. end;
  1466. if not Found then break;
  1467. end;
  1468. for i:=0 to ResolverCount-1 do
  1469. begin
  1470. CurResolver:=Resolvers[i];
  1471. if CurResolver.Parser=nil then
  1472. begin
  1473. if CurResolver.CurrentParser<>nil then
  1474. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1475. continue;
  1476. end;
  1477. if CurResolver.Parser.CurModule<>nil then
  1478. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1479. end;
  1480. end;
  1481. procedure TCustomTestModule.ParseModule;
  1482. begin
  1483. if SkipTests then exit;
  1484. FFirstPasStatement:=nil;
  1485. try
  1486. StartParsing;
  1487. Parser.ParseMain(FModule);
  1488. ParseModuleQueue;
  1489. except
  1490. on E: Exception do
  1491. HandleException(E);
  1492. end;
  1493. if SkipTests then exit;
  1494. AssertNotNull('Module resulted in Module',Module);
  1495. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1496. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1497. end;
  1498. procedure TCustomTestModule.ParseProgram;
  1499. begin
  1500. if SkipTests then exit;
  1501. ParseModule;
  1502. if SkipTests then exit;
  1503. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1504. FPasProgram:=TPasProgram(Module);
  1505. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1506. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1507. if (PasProgram.InitializationSection.Elements.Count>0) then
  1508. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1509. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1510. end;
  1511. procedure TCustomTestModule.ParseLibrary;
  1512. var
  1513. Init: TInitializationSection;
  1514. begin
  1515. if SkipTests then exit;
  1516. ParseModule;
  1517. if SkipTests then exit;
  1518. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1519. FPasLibrary:=TPasLibrary(Module);
  1520. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1521. Init:=PasLibrary.InitializationSection;
  1522. if (Init<>nil) and (Init.Elements.Count>0) then
  1523. if TObject(Init.Elements[0]) is TPasImplBlock then
  1524. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1525. end;
  1526. procedure TCustomTestModule.ParseUnit;
  1527. begin
  1528. if SkipTests then exit;
  1529. ParseModule;
  1530. if SkipTests then exit;
  1531. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1532. AssertNotNull('Has interface section',Module.InterfaceSection);
  1533. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1534. if (Module.InitializationSection<>nil)
  1535. and (Module.InitializationSection.Elements.Count>0)
  1536. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1537. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1538. end;
  1539. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1540. ): TTestEnginePasResolver;
  1541. var
  1542. i: Integer;
  1543. begin
  1544. for i:=0 to ResolverCount-1 do
  1545. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1546. exit(Resolvers[i]);
  1547. Result:=nil;
  1548. end;
  1549. function TCustomTestModule.AddModule(aFilename: string
  1550. ): TTestEnginePasResolver;
  1551. begin
  1552. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1553. if FindModuleWithFilename(aFilename)<>nil then
  1554. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1555. Result:=TTestEnginePasResolver.Create;
  1556. Result.Filename:=aFilename;
  1557. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1558. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1559. Result.OnLog:=@OnPasResolverLog;
  1560. Result.Hub:=Hub;
  1561. FModules.Add(Result);
  1562. end;
  1563. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1564. ): TTestEnginePasResolver;
  1565. begin
  1566. Result:=AddModule(aFilename);
  1567. Result.Source:=Src;
  1568. end;
  1569. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1570. ImplementationSrc: string): TTestEnginePasResolver;
  1571. var
  1572. Src: String;
  1573. begin
  1574. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1575. Src+=LineEnding;
  1576. Src+='interface'+LineEnding;
  1577. Src+=LineEnding;
  1578. Src+=InterfaceSrc;
  1579. Src+='implementation'+LineEnding;
  1580. Src+=LineEnding;
  1581. Src+=ImplementationSrc;
  1582. Src+='end.'+LineEnding;
  1583. Result:=AddModuleWithSrc(aFilename,Src);
  1584. end;
  1585. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1586. var
  1587. Intf, Impl: TStringList;
  1588. begin
  1589. Intf:=TStringList.Create;
  1590. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1591. // unit interface
  1592. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1593. Intf.Add('{$modeswitch externalclass}');
  1594. Intf.Add('type');
  1595. Intf.Add(' integer=longint;');
  1596. Intf.Add(' sizeint=nativeint;');
  1597. //'const',
  1598. //' LineEnding = #10;',
  1599. //' DirectorySeparator = ''/'';',
  1600. //' DriveSeparator = '''';',
  1601. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1602. //' AllowDriveSeparators : set of char = [];',
  1603. if supTObject in Parts then
  1604. Intf.AddStrings([
  1605. 'type',
  1606. ' TClass = class of TObject;',
  1607. ' TObject = class',
  1608. ' constructor Create;',
  1609. ' destructor Destroy; virtual;',
  1610. ' class function ClassType: TClass; assembler;',
  1611. ' class function ClassName: String; assembler;',
  1612. ' class function ClassNameIs(const Name: string): boolean;',
  1613. ' class function ClassParent: TClass; assembler;',
  1614. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1615. ' class function UnitName: String; assembler;',
  1616. ' procedure AfterConstruction; virtual;',
  1617. ' procedure BeforeDestruction;virtual;',
  1618. ' function Equals(Obj: TObject): boolean; virtual;',
  1619. ' function ToString: String; virtual;',
  1620. ' end;']);
  1621. if supTInterfacedObject in Parts then
  1622. Intf.AddStrings([
  1623. ' {$Interfaces COM}',
  1624. ' IUnknown = interface',
  1625. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1626. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1627. ' function _AddRef: Integer;',
  1628. ' function _Release: Integer;',
  1629. ' end;',
  1630. ' IInterface = IUnknown;',
  1631. ' TInterfacedObject = class(TObject,IUnknown)',
  1632. ' protected',
  1633. ' fRefCount: Integer;',
  1634. ' { implement methods of IUnknown }',
  1635. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1636. ' function _AddRef: Integer; virtual;',
  1637. ' function _Release: Integer; virtual;',
  1638. ' end;',
  1639. ' TInterfacedClass = class of TInterfacedObject;',
  1640. '',
  1641. '']);
  1642. if supTVarRec in Parts then
  1643. Intf.AddStrings([
  1644. 'const',
  1645. ' vtInteger = 0;',
  1646. ' vtBoolean = 1;',
  1647. ' vtJSValue = 19;',
  1648. 'type',
  1649. ' PVarRec = ^TVarRec;',
  1650. ' TVarRec = record',
  1651. ' VType : byte;',
  1652. ' VJSValue: JSValue;',
  1653. ' vInteger: longint external name ''VJSValue'';',
  1654. ' vBoolean: boolean external name ''VJSValue'';',
  1655. ' end;',
  1656. ' TVarRecArray = array of TVarRec;',
  1657. 'function VarRecs: TVarRecArray; varargs;',
  1658. '']);
  1659. if supTypeInfo in Parts then
  1660. begin
  1661. Intf.AddStrings([
  1662. 'type',
  1663. ' TTypeKind = (',
  1664. ' tkUnknown, // 0',
  1665. ' tkInteger, // 1',
  1666. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1667. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1668. ' tkEnumeration, // 4',
  1669. ' tkSet, // 5',
  1670. ' tkDouble, // 6',
  1671. ' tkBool, // 7',
  1672. ' tkProcVar, // 8 function or procedure',
  1673. ' tkMethod, // 9 proc var of object',
  1674. ' tkArray, // 10 static array',
  1675. ' tkDynArray, // 11',
  1676. ' tkRecord, // 12',
  1677. ' tkClass, // 13',
  1678. ' tkClassRef, // 14',
  1679. ' tkPointer, // 15',
  1680. ' tkJSValue, // 16',
  1681. ' tkRefToProcVar, // 17 variable of procedure type',
  1682. ' tkInterface, // 18',
  1683. ' //tkObject,',
  1684. ' //tkSString,tkLString,tkAString,tkWString,',
  1685. ' //tkVariant,',
  1686. ' //tkWChar,',
  1687. ' //tkInt64,',
  1688. ' //tkQWord,',
  1689. ' //tkInterfaceRaw,',
  1690. ' //tkUString,tkUChar,',
  1691. ' tkHelper, // 19',
  1692. ' //tkFile,',
  1693. ' tkExtClass // 20',
  1694. ' );',
  1695. ' TTypeKinds = set of TTypeKind;',
  1696. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1697. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1698. ' end;',
  1699. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1700. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1701. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1702. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1703. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1704. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1705. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1706. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1707. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1708. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1709. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1710. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1711. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1712. '']);
  1713. end;
  1714. if supWriteln in Parts then
  1715. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1716. Intf.Add('var');
  1717. Intf.Add(' ExitCode: Longint = 0;');
  1718. // unit implementation
  1719. Impl:=TStringList.Create;
  1720. if supTObject in Parts then
  1721. Impl.AddStrings([
  1722. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1723. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1724. 'asm',
  1725. 'end;',
  1726. 'constructor TObject.Create; begin end;',
  1727. 'destructor TObject.Destroy; begin end;',
  1728. 'class function TObject.ClassType: TClass; assembler;',
  1729. 'asm',
  1730. 'end;',
  1731. 'class function TObject.ClassName: String; assembler;',
  1732. 'asm',
  1733. 'end;',
  1734. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1735. 'begin',
  1736. ' Result:=SameText(Name,ClassName);',
  1737. 'end;',
  1738. 'class function TObject.ClassParent: TClass; assembler;',
  1739. 'asm',
  1740. 'end;',
  1741. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1742. 'asm',
  1743. 'end;',
  1744. 'class function TObject.UnitName: String; assembler;',
  1745. 'asm',
  1746. 'end;',
  1747. 'procedure TObject.AfterConstruction; begin end;',
  1748. 'procedure TObject.BeforeDestruction; begin end;',
  1749. 'function TObject.Equals(Obj: TObject): boolean;',
  1750. 'begin',
  1751. ' Result:=Obj=Self;',
  1752. 'end;',
  1753. 'function TObject.ToString: String;',
  1754. 'begin',
  1755. ' Result:=ClassName;',
  1756. 'end;'
  1757. ]);
  1758. if supTInterfacedObject in Parts then
  1759. Impl.AddStrings([
  1760. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1761. //'begin',
  1762. //'end;',
  1763. 'function TInterfacedObject._AddRef: Integer;',
  1764. 'begin',
  1765. 'end;',
  1766. 'function TInterfacedObject._Release: Integer;',
  1767. 'begin',
  1768. 'end;',
  1769. '']);
  1770. if supTVarRec in Parts then
  1771. Impl.AddStrings([
  1772. 'function VarRecs: TVarRecArray; varargs;',
  1773. 'var',
  1774. ' v: PVarRec;',
  1775. 'begin',
  1776. ' v^.VType:=1;',
  1777. ' v^.VJSValue:=2;',
  1778. 'end;',
  1779. '']);
  1780. try
  1781. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1782. finally
  1783. Intf.Free;
  1784. Impl.Free;
  1785. end;
  1786. end;
  1787. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1788. SystemUnitParts: TSystemUnitParts);
  1789. begin
  1790. if NeedSystemUnit then
  1791. AddSystemUnit(SystemUnitParts)
  1792. else
  1793. Parser.ImplicitUses.Clear;
  1794. Add('program '+ExtractFileUnitName(Filename)+';');
  1795. Add('');
  1796. end;
  1797. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1798. SystemUnitParts: TSystemUnitParts);
  1799. begin
  1800. if NeedSystemUnit then
  1801. AddSystemUnit(SystemUnitParts)
  1802. else
  1803. Parser.ImplicitUses.Clear;
  1804. Add('library '+ExtractFileUnitName(Filename)+';');
  1805. Add('');
  1806. end;
  1807. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1808. SystemUnitParts: TSystemUnitParts);
  1809. begin
  1810. if NeedSystemUnit then
  1811. AddSystemUnit(SystemUnitParts)
  1812. else
  1813. Parser.ImplicitUses.Clear;
  1814. Add('unit Test1;');
  1815. Add('');
  1816. end;
  1817. procedure TCustomTestModule.ConvertModule;
  1818. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1819. out UsesLit: TJSArrayLiteral);
  1820. var
  1821. i: Integer;
  1822. Item: TJSElement;
  1823. Lit: TJSLiteral;
  1824. begin
  1825. UsesLit:=nil;
  1826. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1827. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1828. exit; // null is ok
  1829. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1830. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1831. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1832. begin
  1833. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1834. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1835. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1836. Lit:=TJSLiteral(Item);
  1837. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1838. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1839. end;
  1840. end;
  1841. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1842. out Src: TJSSourceElements);
  1843. var
  1844. FunDecl: TJSFunctionDeclarationStatement;
  1845. FunDef: TJSFuncDef;
  1846. FunBody: TJSFunctionBody;
  1847. begin
  1848. Src:=nil;
  1849. AssertNotNull(ParamName,Arg.Expr);
  1850. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1851. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1852. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1853. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1854. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1855. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1856. AssertNotNull(ParamName+' body',FunDef.Body);
  1857. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1858. FunBody:=FunDef.Body as TJSFunctionBody;
  1859. AssertNotNull(ParamName+' body.A',FunBody.A);
  1860. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1861. Src:=FunBody.A as TJSSourceElements;
  1862. end;
  1863. var
  1864. ModuleNameExpr: TJSLiteral;
  1865. InitFunction: TJSFunctionDeclarationStatement;
  1866. InitAssign: TJSSimpleAssignStatement;
  1867. InitName: String;
  1868. LastNode: TJSElement;
  1869. Arg: TJSArrayLiteralElement;
  1870. IsProg, IsLib: Boolean;
  1871. begin
  1872. if SkipTests then exit;
  1873. IsProg:=false;
  1874. IsLib:=false;
  1875. if Module is TPasProgram then
  1876. IsProg:=true
  1877. else if Module is TPasLibrary then
  1878. IsLib:=true;
  1879. try
  1880. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1881. except
  1882. on E: Exception do
  1883. HandleException(E);
  1884. end;
  1885. if SkipTests then exit;
  1886. if ExpectedErrorClass<>nil then
  1887. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1888. FJSSource:=TStringList.Create;
  1889. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1890. {$IFDEF VerbosePas2JS}
  1891. writeln('TTestModule.ConvertModule JS:');
  1892. write(FJSSource.Text);
  1893. {$ENDIF}
  1894. // rtl.module(...
  1895. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1896. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1897. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1898. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1899. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1900. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1901. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1902. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1903. // parameter 'unitname'
  1904. if JSModuleCallArgs.Elements.Count<1 then
  1905. Fail('rtl.module first param unit missing');
  1906. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1907. AssertNotNull('module name param',Arg.Expr);
  1908. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1909. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1910. if IsProg then
  1911. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1912. else if IsLib then
  1913. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1914. else
  1915. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1916. // main uses section
  1917. if JSModuleCallArgs.Elements.Count<2 then
  1918. Fail('rtl.module second param main uses missing');
  1919. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1920. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1921. // program/library/interface function()
  1922. if JSModuleCallArgs.Elements.Count<3 then
  1923. Fail('rtl.module third param intf-function missing');
  1924. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1925. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1926. // search for $mod.$init or $mod.$main - the last statement
  1927. if IsProg or IsLib then
  1928. begin
  1929. InitName:='$main';
  1930. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1931. end
  1932. else
  1933. InitName:='$init';
  1934. InitAssign:=nil;
  1935. InitFunction:=nil;
  1936. FJSInitBody:=nil;
  1937. if JSModuleSrc.Statements.Count>0 then
  1938. begin
  1939. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1940. if LastNode is TJSSimpleAssignStatement then
  1941. begin
  1942. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1943. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1944. begin
  1945. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1946. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1947. end
  1948. else if IsProg or IsLib then
  1949. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1950. end;
  1951. end;
  1952. // optional: implementation uses section
  1953. if JSModuleCallArgs.Elements.Count<4 then
  1954. exit;
  1955. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1956. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1957. end;
  1958. procedure TCustomTestModule.ConvertProgram;
  1959. begin
  1960. Add('end.');
  1961. ParseProgram;
  1962. ConvertModule;
  1963. end;
  1964. procedure TCustomTestModule.ConvertLibrary;
  1965. begin
  1966. Add('end.');
  1967. ParseLibrary;
  1968. ConvertModule;
  1969. end;
  1970. procedure TCustomTestModule.ConvertUnit;
  1971. begin
  1972. Add('end.');
  1973. ParseUnit;
  1974. ConvertModule;
  1975. end;
  1976. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1977. begin
  1978. Result:=tcmodules.JSToStr(El);
  1979. end;
  1980. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1981. DottedName: string);
  1982. begin
  1983. if DottedName='' then
  1984. begin
  1985. AssertNull(Msg,El);
  1986. end
  1987. else
  1988. begin
  1989. AssertNotNull(Msg,El);
  1990. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1991. end;
  1992. end;
  1993. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1994. begin
  1995. if El=nil then
  1996. Result:=''
  1997. else if El is TJSPrimaryExpressionIdent then
  1998. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1999. else if El is TJSDotMemberExpression then
  2000. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2001. else
  2002. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2003. end;
  2004. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2005. InitStatements: string; ImplStatements: string);
  2006. var
  2007. ActualSrc, ExpectedSrc, InitName: String;
  2008. IsProg, IsLib: Boolean;
  2009. begin
  2010. ActualSrc:=JSToStr(JSModuleSrc);
  2011. if coUseStrict in Converter.Options then
  2012. ExpectedSrc:='"use strict";'+LineEnding
  2013. else
  2014. ExpectedSrc:='';
  2015. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2016. ExpectedSrc:=ExpectedSrc+Statements;
  2017. // unit implementation
  2018. if (Trim(ImplStatements)<>'') then
  2019. ExpectedSrc:=ExpectedSrc+LineEnding
  2020. +'$mod.$implcode = function () {'+LineEnding
  2021. +ImplStatements
  2022. +'};'+LineEnding;
  2023. // program main or unit initialization
  2024. IsProg:=false;
  2025. IsLib:=false;
  2026. if Module is TPasProgram then
  2027. IsProg:=true
  2028. else if Module is TPasLibrary then
  2029. IsLib:=true;
  2030. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2031. begin
  2032. if IsProg or IsLib then
  2033. InitName:='$main'
  2034. else
  2035. InitName:='$init';
  2036. ExpectedSrc:=ExpectedSrc+LineEnding
  2037. +'$mod.'+InitName+' = function () {'+LineEnding
  2038. +InitStatements
  2039. +'};'+LineEnding;
  2040. end;
  2041. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2042. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2043. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2044. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2045. end;
  2046. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2047. // search diff, ignore changes in spaces
  2048. var
  2049. s: string;
  2050. begin
  2051. if CheckSrcDiff(Expected,Actual,s) then exit;
  2052. Fail(Msg+': '+s);
  2053. end;
  2054. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2055. var
  2056. aResolver: TTestEnginePasResolver;
  2057. aConverter: TPasToJSConverter;
  2058. aJSModule: TJSSourceElements;
  2059. ActualSrc: String;
  2060. begin
  2061. aResolver:=GetResolver(Filename);
  2062. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2063. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2064. {$IFDEF VerbosePas2JS}
  2065. writeln('CheckUnit '+Filename+' converting ...');
  2066. {$ENDIF}
  2067. aConverter:=CreateConverter;
  2068. aJSModule:=nil;
  2069. try
  2070. try
  2071. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2072. except
  2073. on E: Exception do
  2074. HandleException(E);
  2075. end;
  2076. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2077. {$IFDEF VerbosePas2JS}
  2078. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2079. write(aResolver.Source);
  2080. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2081. write(ActualSrc);
  2082. {$ENDIF}
  2083. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2084. finally
  2085. aJSModule.Free;
  2086. aConverter.Free;
  2087. end;
  2088. end;
  2089. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2090. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2091. var
  2092. i: Integer;
  2093. Item: TTestHintMessage;
  2094. Expected,Actual: string;
  2095. begin
  2096. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2097. for i:=0 to MsgCount-1 do
  2098. begin
  2099. Item:=Msgs[i];
  2100. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2101. if (Marker<>nil) then
  2102. begin
  2103. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2104. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2105. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2106. end;
  2107. // found
  2108. FHintMsgsGood.Add(Item);
  2109. str(Item.MsgType,Actual);
  2110. str(MsgType,Expected);
  2111. AssertEquals('MsgType',Expected,Actual);
  2112. exit;
  2113. end;
  2114. // needed message missing -> show emitted messages
  2115. WriteSources('',0,0);
  2116. for i:=0 to MsgCount-1 do
  2117. begin
  2118. Item:=Msgs[i];
  2119. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2120. ' ('+IntToStr(Item.MsgNumber),')');
  2121. if Marker<>nil then
  2122. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2123. writeln(' {',Item.Msg,'}');
  2124. end;
  2125. str(MsgType,Expected);
  2126. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2127. if Marker<>nil then
  2128. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2129. Actual:=Actual+' '+Msg;
  2130. Fail(Actual);
  2131. end;
  2132. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2133. );
  2134. var
  2135. i: Integer;
  2136. s, Txt: String;
  2137. Msg: TTestHintMessage;
  2138. begin
  2139. for i:=0 to MsgCount-1 do
  2140. begin
  2141. Msg:=Msgs[i];
  2142. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2143. s:='';
  2144. str(Msg.MsgType,s);
  2145. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2146. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2147. if WithSourcePos then
  2148. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2149. Txt:=Txt+' {'+Msg.Msg+'}';
  2150. Fail(Txt);
  2151. end;
  2152. end;
  2153. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2154. MsgNumber: integer);
  2155. begin
  2156. ExpectedErrorClass:=EScannerError;
  2157. ExpectedErrorMsg:=Msg;
  2158. ExpectedErrorNumber:=MsgNumber;
  2159. end;
  2160. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2161. MsgNumber: integer);
  2162. begin
  2163. ExpectedErrorClass:=EParserError;
  2164. ExpectedErrorMsg:=Msg;
  2165. ExpectedErrorNumber:=MsgNumber;
  2166. end;
  2167. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2168. MsgNumber: integer);
  2169. begin
  2170. ExpectedErrorClass:=EPasResolve;
  2171. ExpectedErrorMsg:=Msg;
  2172. ExpectedErrorNumber:=MsgNumber;
  2173. end;
  2174. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2175. MsgNumber: integer);
  2176. begin
  2177. ExpectedErrorClass:=EPas2JS;
  2178. ExpectedErrorMsg:=Msg;
  2179. ExpectedErrorNumber:=MsgNumber;
  2180. end;
  2181. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2182. var
  2183. MsgNumber: Integer;
  2184. Msg: String;
  2185. begin
  2186. Result:=false;
  2187. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2188. Msg:=E.Message;
  2189. if E is EPas2JS then
  2190. MsgNumber:=EPas2JS(E).MsgNumber
  2191. else if E is EPasResolve then
  2192. MsgNumber:=EPasResolve(E).MsgNumber
  2193. else if E is EParserError then
  2194. MsgNumber:=Parser.LastMsgNumber
  2195. else if E is EScannerError then
  2196. begin
  2197. MsgNumber:=Scanner.LastMsgNumber;
  2198. Msg:=Scanner.LastMsg;
  2199. end
  2200. else
  2201. MsgNumber:=0;
  2202. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2203. if Result then
  2204. SkipTests:=true;
  2205. end;
  2206. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2207. begin
  2208. if IsErrorExpected(E) then exit;
  2209. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2210. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2211. +' '+Scanner.CurFilename
  2212. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2213. FailException(E);
  2214. end;
  2215. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2216. begin
  2217. if IsErrorExpected(E) then exit;
  2218. WriteSources(E.Filename,E.Row,E.Column);
  2219. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2220. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2221. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2222. );
  2223. FailException(E);
  2224. end;
  2225. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2226. var
  2227. P: TPasSourcePos;
  2228. begin
  2229. if IsErrorExpected(E) then exit;
  2230. P:=E.SourcePos;
  2231. WriteSources(P.FileName,P.Row,P.Column);
  2232. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2233. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2234. FailException(E);
  2235. end;
  2236. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2237. var
  2238. Row, Col: integer;
  2239. begin
  2240. if IsErrorExpected(E) then exit;
  2241. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2242. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2243. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2244. +' '+E.PasElement.SourceFilename
  2245. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2246. FailException(E);
  2247. end;
  2248. procedure TCustomTestModule.HandleException(E: Exception);
  2249. begin
  2250. if E is EScannerError then
  2251. HandleScannerError(EScannerError(E))
  2252. else if E is EParserError then
  2253. HandleParserError(EParserError(E))
  2254. else if E is EPasResolve then
  2255. HandlePasResolveError(EPasResolve(E))
  2256. else if E is EPas2JS then
  2257. HandlePas2JSError(EPas2JS(E))
  2258. else
  2259. begin
  2260. if IsErrorExpected(E) then exit;
  2261. if not (E is EAssertionFailedError) then
  2262. begin
  2263. WriteSources('',0,0);
  2264. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2265. end;
  2266. FailException(E);
  2267. end;
  2268. end;
  2269. procedure TCustomTestModule.FailException(E: Exception);
  2270. var
  2271. MsgNumber: Integer;
  2272. begin
  2273. if ExpectedErrorClass<>nil then
  2274. begin
  2275. if FExpectedErrorClass=E.ClassType then
  2276. begin
  2277. if E is EPas2JS then
  2278. MsgNumber:=EPas2JS(E).MsgNumber
  2279. else if E is EPasResolve then
  2280. MsgNumber:=EPasResolve(E).MsgNumber
  2281. else if E is EParserError then
  2282. MsgNumber:=Parser.LastMsgNumber
  2283. else if E is EScannerError then
  2284. MsgNumber:=Scanner.LastMsgNumber
  2285. else
  2286. MsgNumber:=0;
  2287. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2288. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2289. ExpectedErrorNumber,MsgNumber);
  2290. end else begin
  2291. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2292. end;
  2293. end;
  2294. Fail(E.Message);
  2295. end;
  2296. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2297. aCol: integer);
  2298. var
  2299. IsSrc: Boolean;
  2300. i, j: Integer;
  2301. SrcLines: TStringList;
  2302. Line: string;
  2303. aModule: TTestEnginePasResolver;
  2304. begin
  2305. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2306. for i:=0 to ResolverCount-1 do
  2307. begin
  2308. aModule:=Resolvers[i];
  2309. SrcLines:=TStringList.Create;
  2310. try
  2311. SrcLines.Text:=aModule.Source;
  2312. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2313. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2314. for j:=1 to SrcLines.Count do
  2315. begin
  2316. Line:=SrcLines[j-1];
  2317. if IsSrc and (j=aRow) then
  2318. begin
  2319. write('*');
  2320. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2321. end;
  2322. writeln(Format('%:4d: ',[j]),Line);
  2323. end;
  2324. finally
  2325. SrcLines.Free;
  2326. end;
  2327. end;
  2328. end;
  2329. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2330. var
  2331. i: Integer;
  2332. begin
  2333. for i:=0 to ResolverCount-1 do
  2334. if Filename=Resolvers[i].Filename then exit(i);
  2335. Result:=-1;
  2336. end;
  2337. function TCustomTestModule.GetResolver(const Filename: string
  2338. ): TTestEnginePasResolver;
  2339. var
  2340. i: Integer;
  2341. begin
  2342. i:=IndexOfResolver(Filename);
  2343. if i<0 then exit(nil);
  2344. Result:=Resolvers[i];
  2345. end;
  2346. function TCustomTestModule.GetDefaultNamespace: string;
  2347. var
  2348. C: TClass;
  2349. begin
  2350. Result:='';
  2351. if FModule=nil then exit;
  2352. C:=FModule.ClassType;
  2353. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2354. Result:=Engine.DefaultNameSpace;
  2355. end;
  2356. constructor TCustomTestModule.Create;
  2357. begin
  2358. inherited Create;
  2359. FHintMsgs:=TObjectList.Create(true);
  2360. FHintMsgsGood:=TFPList.Create;
  2361. end;
  2362. destructor TCustomTestModule.Destroy;
  2363. begin
  2364. FreeAndNil(FHintMsgs);
  2365. FreeAndNil(FHintMsgsGood);
  2366. inherited Destroy;
  2367. end;
  2368. { TTestModule }
  2369. procedure TTestModule.TestReservedWords;
  2370. var
  2371. i: integer;
  2372. begin
  2373. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2374. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2375. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2376. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2377. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2378. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2379. end;
  2380. procedure TTestModule.TestEmptyProgram;
  2381. begin
  2382. StartProgram(false);
  2383. Add('begin');
  2384. ConvertProgram;
  2385. CheckSource('TestEmptyProgram','','');
  2386. end;
  2387. procedure TTestModule.TestEmptyProgramUseStrict;
  2388. begin
  2389. Converter.Options:=Converter.Options+[coUseStrict];
  2390. StartProgram(false);
  2391. Add('begin');
  2392. ConvertProgram;
  2393. CheckSource('TestEmptyProgramUseStrict','','');
  2394. end;
  2395. procedure TTestModule.TestEmptyUnit;
  2396. begin
  2397. StartUnit(false);
  2398. Add('interface');
  2399. Add('implementation');
  2400. ConvertUnit;
  2401. CheckSource('TestEmptyUnit',
  2402. LinesToStr([
  2403. ]),
  2404. '');
  2405. end;
  2406. procedure TTestModule.TestEmptyUnitUseStrict;
  2407. begin
  2408. Converter.Options:=Converter.Options+[coUseStrict];
  2409. StartUnit(false);
  2410. Add('interface');
  2411. Add('implementation');
  2412. ConvertUnit;
  2413. CheckSource('TestEmptyUnitUseStrict',
  2414. LinesToStr([
  2415. ''
  2416. ]),
  2417. '');
  2418. end;
  2419. procedure TTestModule.TestDottedUnitNames;
  2420. begin
  2421. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2422. LinesToStr([
  2423. 'var iV: longint;'
  2424. ]),
  2425. '');
  2426. FFilename:='ns1.test1.pp';
  2427. StartProgram(true);
  2428. Add('uses unIt2;');
  2429. Add('var');
  2430. Add(' i: longint;');
  2431. Add('begin');
  2432. Add(' i:=iv;');
  2433. Add(' i:=uNit2.iv;');
  2434. Add(' i:=Ns1.TEst1.i;');
  2435. ConvertProgram;
  2436. CheckSource('TestDottedUnitNames',
  2437. LinesToStr([
  2438. 'this.i = 0;',
  2439. '']),
  2440. LinesToStr([ // this.$init
  2441. '$mod.i = pas["NS1.Unit2"].iV;',
  2442. '$mod.i = pas["NS1.Unit2"].iV;',
  2443. '$mod.i = $mod.i;',
  2444. '']) );
  2445. end;
  2446. procedure TTestModule.TestDottedUnitNameImpl;
  2447. begin
  2448. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2449. LinesToStr([
  2450. 'type',
  2451. ' TObject = class end;',
  2452. ' TTestA = class',
  2453. ' end;'
  2454. ]),
  2455. LinesToStr(['uses TEST.UnitB;'])
  2456. );
  2457. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2458. LinesToStr([
  2459. 'uses TEST.UnitA;',
  2460. 'type TTestB = class(TTestA);'
  2461. ]),
  2462. ''
  2463. );
  2464. StartProgram(true);
  2465. Add('uses TEST.UnitA;');
  2466. Add('begin');
  2467. ConvertProgram;
  2468. CheckSource('TestDottedUnitNameImpl',
  2469. LinesToStr([
  2470. '']),
  2471. LinesToStr([ // this.$init
  2472. '']) );
  2473. CheckUnit('TEST.UnitA.pas',
  2474. LinesToStr([
  2475. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2476. ' var $mod = this;',
  2477. ' rtl.createClass(this, "TObject", null, function () {',
  2478. ' this.$init = function () {',
  2479. ' };',
  2480. ' this.$final = function () {',
  2481. ' };',
  2482. ' });',
  2483. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2484. ' });',
  2485. '}, ["TEST.UnitB"]);'
  2486. ]));
  2487. CheckUnit('TEST.UnitB.pas',
  2488. LinesToStr([
  2489. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2490. ' var $mod = this;',
  2491. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2492. ' });',
  2493. '});'
  2494. ]));
  2495. end;
  2496. procedure TTestModule.TestDottedUnitExpr;
  2497. begin
  2498. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2499. LinesToStr([
  2500. 'procedure DoIt;'
  2501. ]),
  2502. 'procedure DoIt; begin end;');
  2503. FFilename:='Ns1.SubNs1.Test1.pp';
  2504. StartProgram(true);
  2505. Add('uses Ns2.sUbnS2.unIt2;');
  2506. Add('var');
  2507. Add(' i: longint;');
  2508. Add('begin');
  2509. Add(' ns2.subns2.unit2.doit;');
  2510. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2511. ConvertProgram;
  2512. CheckSource('TestDottedUnitExpr',
  2513. LinesToStr([
  2514. 'this.i = 0;',
  2515. '']),
  2516. LinesToStr([ // this.$init
  2517. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2518. '$mod.i = $mod.i;',
  2519. '']) );
  2520. end;
  2521. procedure TTestModule.Test_ModeFPCFail;
  2522. begin
  2523. StartProgram(false);
  2524. Add('{$mode FPC}');
  2525. Add('begin');
  2526. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2527. ConvertProgram;
  2528. end;
  2529. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2530. begin
  2531. StartProgram(false);
  2532. Add('{$modeswitch cblocks-}');
  2533. Add('begin');
  2534. ConvertProgram;
  2535. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2536. CheckResolverUnexpectedHints();
  2537. end;
  2538. procedure TTestModule.TestUnit_UseSystem;
  2539. begin
  2540. StartUnit(true);
  2541. Add([
  2542. 'interface',
  2543. 'var i: integer;',
  2544. 'implementation']);
  2545. ConvertUnit;
  2546. CheckSource('TestUnit_UseSystem',
  2547. LinesToStr([
  2548. 'this.i = 0;',
  2549. '']),
  2550. LinesToStr([
  2551. '']) );
  2552. end;
  2553. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2554. begin
  2555. AddModuleWithIntfImplSrc('unit1.pp',
  2556. LinesToStr([
  2557. 'type number = longint;']),
  2558. LinesToStr([
  2559. 'uses test1;',
  2560. 'procedure DoIt;',
  2561. 'begin',
  2562. ' i:=3;',
  2563. 'end;']));
  2564. StartUnit(true);
  2565. Add([
  2566. 'interface',
  2567. 'uses unit1;',
  2568. 'var i: number;',
  2569. 'implementation']);
  2570. ConvertUnit;
  2571. CheckSource('TestUnit_Intf1Impl2Intf1',
  2572. LinesToStr([
  2573. 'this.i = 0;',
  2574. '']),
  2575. LinesToStr([
  2576. '']) );
  2577. end;
  2578. procedure TTestModule.TestIncludeVersion;
  2579. begin
  2580. StartProgram(false);
  2581. Add([
  2582. 'var',
  2583. ' s: string;',
  2584. ' i: word;',
  2585. 'begin',
  2586. ' s:={$I %line%};',
  2587. ' i:={$I %linenum%};',
  2588. ' s:={$I %currentroutine%};',
  2589. ' s:={$I %pas2jsversion%};',
  2590. ' s:={$I %pas2jstarget%};',
  2591. ' s:={$I %pas2jstargetos%};',
  2592. ' s:={$I %pas2jstargetcpu%};',
  2593. ' s:={$I %file%};',
  2594. '']);
  2595. ConvertProgram;
  2596. CheckSource('TestIncludeVersion',
  2597. LinesToStr([
  2598. 'this.s="";',
  2599. 'this.i = 0;']),
  2600. LinesToStr([
  2601. '$mod.s = "7";',
  2602. '$mod.i = 8;',
  2603. '$mod.s = "<anonymous>";',
  2604. '$mod.s = "Comp.Ver.tcmodules";',
  2605. '$mod.s = "Browser";',
  2606. '$mod.s = "Browser";',
  2607. '$mod.s = "ECMAScript5";',
  2608. '$mod.s = "test1.pp";',
  2609. '']));
  2610. end;
  2611. procedure TTestModule.TestVarInt;
  2612. begin
  2613. StartProgram(false);
  2614. Add('var MyI: longint;');
  2615. Add('begin');
  2616. ConvertProgram;
  2617. CheckSource('TestVarInt','this.MyI=0;','');
  2618. end;
  2619. procedure TTestModule.TestVarBaseTypes;
  2620. begin
  2621. StartProgram(false);
  2622. Add('var');
  2623. Add(' i: longint;');
  2624. Add(' s: string;');
  2625. Add(' c: char;');
  2626. Add(' b: boolean;');
  2627. Add(' d: double;');
  2628. Add(' i2: longint = 3;');
  2629. Add(' s2: string = ''foo'';');
  2630. Add(' c2: char = ''4'';');
  2631. Add(' b2: boolean = true;');
  2632. Add(' d2: double = 5.6;');
  2633. Add(' i3: longint = $707;');
  2634. Add(' i4: nativeint = 9007199254740991;');
  2635. Add(' i5: nativeint = -9007199254740991-1;');
  2636. Add(' i6: nativeint = $fffffffffffff;');
  2637. Add(' i7: nativeint = -$fffffffffffff-1;');
  2638. Add(' i8: byte = 00;');
  2639. Add(' u8: nativeuint = $fffffffffffff;');
  2640. Add(' u9: nativeuint = $0000000000000;');
  2641. Add(' u10: nativeuint = $00ff00;');
  2642. Add('begin');
  2643. ConvertProgram;
  2644. CheckSource('TestVarBaseTypes',
  2645. LinesToStr([
  2646. 'this.i = 0;',
  2647. 'this.s = "";',
  2648. 'this.c = "";',
  2649. 'this.b = false;',
  2650. 'this.d = 0.0;',
  2651. 'this.i2 = 3;',
  2652. 'this.s2 = "foo";',
  2653. 'this.c2 = "4";',
  2654. 'this.b2 = true;',
  2655. 'this.d2 = 5.6;',
  2656. 'this.i3 = 0x707;',
  2657. 'this.i4 = 9007199254740991;',
  2658. 'this.i5 = -9007199254740991-1;',
  2659. 'this.i6 = 0xfffffffffffff;',
  2660. 'this.i7 =-0xfffffffffffff-1;',
  2661. 'this.i8 = 0;',
  2662. 'this.u8 = 0xfffffffffffff;',
  2663. 'this.u9 = 0x0;',
  2664. 'this.u10 = 0xff00;'
  2665. ]),
  2666. '');
  2667. end;
  2668. procedure TTestModule.TestBaseTypeSingleFail;
  2669. begin
  2670. StartProgram(false);
  2671. Add('var s: single;');
  2672. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2673. ConvertProgram;
  2674. end;
  2675. procedure TTestModule.TestBaseTypeExtendedFail;
  2676. begin
  2677. StartProgram(false);
  2678. Add('var e: extended;');
  2679. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2680. ConvertProgram;
  2681. end;
  2682. procedure TTestModule.TestConstBaseTypes;
  2683. begin
  2684. StartProgram(false);
  2685. Add('const');
  2686. Add(' i: longint = 3;');
  2687. Add(' s: string = ''foo'';');
  2688. Add(' c: char = ''4'';');
  2689. Add(' b: boolean = true;');
  2690. Add(' d: double = 5.6;');
  2691. Add(' e = low(word);');
  2692. Add(' f = high(word);');
  2693. Add('begin');
  2694. ConvertProgram;
  2695. CheckSource('TestVarBaseTypes',
  2696. LinesToStr([
  2697. 'this.i=3;',
  2698. 'this.s="foo";',
  2699. 'this.c="4";',
  2700. 'this.b=true;',
  2701. 'this.d=5.6;',
  2702. 'this.e = 0;',
  2703. 'this.f = 65535;'
  2704. ]),
  2705. '');
  2706. end;
  2707. procedure TTestModule.TestAliasTypeRef;
  2708. begin
  2709. StartProgram(false);
  2710. Add('type');
  2711. Add(' a=longint;');
  2712. Add(' b=a;');
  2713. Add('var');
  2714. Add(' c: A;');
  2715. Add(' d: B;');
  2716. Add('begin');
  2717. ConvertProgram;
  2718. CheckSource('TestAliasTypeRef',
  2719. LinesToStr([ // statements
  2720. 'this.c = 0;',
  2721. 'this.d = 0;'
  2722. ]),
  2723. LinesToStr([ // this.$main
  2724. ''
  2725. ]));
  2726. end;
  2727. procedure TTestModule.TestTypeCast_BaseTypes;
  2728. begin
  2729. StartProgram(false);
  2730. Add([
  2731. 'var',
  2732. ' i: longint;',
  2733. ' b: boolean;',
  2734. ' d: double;',
  2735. ' s: string;',
  2736. ' c: char;',
  2737. 'begin',
  2738. ' i:=longint(i);',
  2739. ' i:=longint(b);',
  2740. ' b:=boolean(b);',
  2741. ' b:=boolean(i);',
  2742. ' d:=double(d);',
  2743. ' d:=double(i);',
  2744. ' s:=string(s);',
  2745. ' s:=string(c);',
  2746. ' c:=char(c);',
  2747. ' c:=char(i);',
  2748. ' c:=char(65);',
  2749. ' c:=char(#10);',
  2750. ' c:=char(#$E000);',
  2751. '']);
  2752. ConvertProgram;
  2753. CheckSource('TestAliasTypeRef',
  2754. LinesToStr([ // statements
  2755. 'this.i = 0;',
  2756. 'this.b = false;',
  2757. 'this.d = 0.0;',
  2758. 'this.s = "";',
  2759. 'this.c = "";',
  2760. '']),
  2761. LinesToStr([ // this.$main
  2762. '$mod.i = $mod.i;',
  2763. '$mod.i = ($mod.b ? 1 : 0);',
  2764. '$mod.b = $mod.b;',
  2765. '$mod.b = $mod.i != 0;',
  2766. '$mod.d = $mod.d;',
  2767. '$mod.d = $mod.i;',
  2768. '$mod.s = $mod.s;',
  2769. '$mod.s = $mod.c;',
  2770. '$mod.c = $mod.c;',
  2771. '$mod.c = String.fromCharCode($mod.i);',
  2772. '$mod.c = "A";',
  2773. '$mod.c = "\n";',
  2774. '$mod.c = "";',
  2775. '']));
  2776. end;
  2777. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2778. begin
  2779. StartProgram(false);
  2780. Add('type');
  2781. Add(' integer = longint;');
  2782. Add(' TYesNo = boolean;');
  2783. Add(' TFloat = double;');
  2784. Add(' TCaption = string;');
  2785. Add(' TChar = char;');
  2786. Add('var');
  2787. Add(' i: integer;');
  2788. Add(' b: TYesNo;');
  2789. Add(' d: TFloat;');
  2790. Add(' s: TCaption;');
  2791. Add(' c: TChar;');
  2792. Add('begin');
  2793. Add(' i:=integer(i);');
  2794. Add(' i:=integer(b);');
  2795. Add(' b:=TYesNo(b);');
  2796. Add(' b:=TYesNo(i);');
  2797. Add(' d:=TFloat(d);');
  2798. Add(' d:=TFloat(i);');
  2799. Add(' s:=TCaption(s);');
  2800. Add(' s:=TCaption(c);');
  2801. Add(' c:=TChar(c);');
  2802. ConvertProgram;
  2803. CheckSource('TestAliasTypeRef',
  2804. LinesToStr([ // statements
  2805. 'this.i = 0;',
  2806. 'this.b = false;',
  2807. 'this.d = 0.0;',
  2808. 'this.s = "";',
  2809. 'this.c = "";',
  2810. '']),
  2811. LinesToStr([ // this.$main
  2812. '$mod.i = $mod.i;',
  2813. '$mod.i = ($mod.b ? 1 : 0);',
  2814. '$mod.b = $mod.b;',
  2815. '$mod.b = $mod.i != 0;',
  2816. '$mod.d = $mod.d;',
  2817. '$mod.d = $mod.i;',
  2818. '$mod.s = $mod.s;',
  2819. '$mod.s = $mod.c;',
  2820. '$mod.c = $mod.c;',
  2821. '']));
  2822. end;
  2823. procedure TTestModule.TestEmptyProc;
  2824. begin
  2825. StartProgram(false);
  2826. Add('procedure Test;');
  2827. Add('begin');
  2828. Add('end;');
  2829. Add('begin');
  2830. ConvertProgram;
  2831. CheckSource('TestEmptyProc',
  2832. LinesToStr([ // statements
  2833. 'this.Test = function () {',
  2834. '};'
  2835. ]),
  2836. LinesToStr([ // this.$main
  2837. ''
  2838. ]));
  2839. end;
  2840. procedure TTestModule.TestProcOneParam;
  2841. begin
  2842. StartProgram(false);
  2843. Add('procedure ProcA(i: longint);');
  2844. Add('begin');
  2845. Add('end;');
  2846. Add('begin');
  2847. Add(' PROCA(3);');
  2848. ConvertProgram;
  2849. CheckSource('TestProcOneParam',
  2850. LinesToStr([ // statements
  2851. 'this.ProcA = function (i) {',
  2852. '};'
  2853. ]),
  2854. LinesToStr([ // this.$main
  2855. '$mod.ProcA(3);'
  2856. ]));
  2857. end;
  2858. procedure TTestModule.TestFunctionWithoutParams;
  2859. begin
  2860. StartProgram(false);
  2861. Add('function FuncA: longint;');
  2862. Add('begin');
  2863. Add('end;');
  2864. Add('var i: longint;');
  2865. Add('begin');
  2866. Add(' I:=FUNCA();');
  2867. Add(' I:=FUNCA;');
  2868. Add(' FUNCA();');
  2869. Add(' FUNCA;');
  2870. ConvertProgram;
  2871. CheckSource('TestProcWithoutParams',
  2872. LinesToStr([ // statements
  2873. 'this.FuncA = function () {',
  2874. ' var Result = 0;',
  2875. ' return Result;',
  2876. '};',
  2877. 'this.i=0;'
  2878. ]),
  2879. LinesToStr([ // this.$main
  2880. '$mod.i=$mod.FuncA();',
  2881. '$mod.i=$mod.FuncA();',
  2882. '$mod.FuncA();',
  2883. '$mod.FuncA();'
  2884. ]));
  2885. end;
  2886. procedure TTestModule.TestProcedureWithoutParams;
  2887. begin
  2888. StartProgram(false);
  2889. Add('procedure ProcA;');
  2890. Add('begin');
  2891. Add('end;');
  2892. Add('begin');
  2893. Add(' PROCA();');
  2894. Add(' PROCA;');
  2895. ConvertProgram;
  2896. CheckSource('TestProcWithoutParams',
  2897. LinesToStr([ // statements
  2898. 'this.ProcA = function () {',
  2899. '};'
  2900. ]),
  2901. LinesToStr([ // this.$main
  2902. '$mod.ProcA();',
  2903. '$mod.ProcA();'
  2904. ]));
  2905. end;
  2906. procedure TTestModule.TestIncDec;
  2907. begin
  2908. StartProgram(false);
  2909. Add([
  2910. 'procedure DoIt(var i: longint);',
  2911. 'begin',
  2912. ' inc(i);',
  2913. ' inc(i,2);',
  2914. 'end;',
  2915. 'var',
  2916. ' Bar: longint;',
  2917. 'begin',
  2918. ' inc(bar);',
  2919. ' inc(bar,2);',
  2920. ' dec(bar);',
  2921. ' dec(bar,3);',
  2922. '']);
  2923. ConvertProgram;
  2924. CheckSource('TestIncDec',
  2925. LinesToStr([ // statements
  2926. 'this.DoIt = function (i) {',
  2927. ' i.set(i.get()+1);',
  2928. ' i.set(i.get()+2);',
  2929. '};',
  2930. 'this.Bar = 0;'
  2931. ]),
  2932. LinesToStr([ // this.$main
  2933. '$mod.Bar+=1;',
  2934. '$mod.Bar+=2;',
  2935. '$mod.Bar-=1;',
  2936. '$mod.Bar-=3;'
  2937. ]));
  2938. end;
  2939. procedure TTestModule.TestLoHiFpcMode;
  2940. begin
  2941. StartProgram(false);
  2942. Add([
  2943. '{$mode objfpc}',
  2944. 'const',
  2945. ' LoByte1 = Lo(Word($1234));',
  2946. ' HiByte1 = Hi(Word($1234));',
  2947. ' LoByte2 = Lo(SmallInt($1234));',
  2948. ' HiByte2 = Hi(SmallInt($1234));',
  2949. ' LoWord1 = Lo($1234CDEF);',
  2950. ' HiWord1 = Hi($1234CDEF);',
  2951. ' LoWord2 = Lo(-$1234CDEF);',
  2952. ' HiWord2 = Hi(-$1234CDEF);',
  2953. ' lo4:byte=lo(byte($34));',
  2954. ' hi4:byte=hi(byte($34));',
  2955. ' lo5:byte=lo(shortint(-$34));',
  2956. ' hi5:byte=hi(shortint(-$34));',
  2957. ' lo6:longword=lo($123456789ABCD);',
  2958. ' hi6:longword=hi($123456789ABCD);',
  2959. ' lo7:longword=lo(-$123456789ABCD);',
  2960. ' hi7:longword=hi(-$123456789ABCD);',
  2961. 'var',
  2962. ' b: Byte;',
  2963. ' ss: shortint;',
  2964. ' w: Word;',
  2965. ' si: SmallInt;',
  2966. ' lw: LongWord;',
  2967. ' li: LongInt;',
  2968. ' b2: Byte;',
  2969. ' ni: nativeint;',
  2970. 'begin',
  2971. ' w := $1234;',
  2972. ' ss := -$12;',
  2973. ' b := lo(ss);',
  2974. ' b := HI(ss);',
  2975. ' b := lo(w);',
  2976. ' b := HI(w);',
  2977. ' b2 := lo(b);',
  2978. ' b2 := hi(b);',
  2979. ' lw := $1234CDEF;',
  2980. ' w := lo(lw);',
  2981. ' w := hi(lw);',
  2982. ' ni := $123456789ABCD;',
  2983. ' lw := lo(ni);',
  2984. ' lw := hi(ni);',
  2985. '']);
  2986. ConvertProgram;
  2987. CheckSource('TestLoHiFpcMode',
  2988. LinesToStr([ // statements
  2989. 'this.LoByte1 = 0x1234 & 0xFF;',
  2990. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2991. 'this.LoByte2 = 0x1234 & 0xFF;',
  2992. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2993. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2994. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2995. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  2996. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  2997. 'this.lo4 = 0x34 & 0xF;',
  2998. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2999. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3000. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3001. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3002. 'this.hi6 = 74565 >>> 0;',
  3003. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3004. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3005. 'this.b = 0;',
  3006. 'this.ss = 0;',
  3007. 'this.w = 0;',
  3008. 'this.si = 0;',
  3009. 'this.lw = 0;',
  3010. 'this.li = 0;',
  3011. 'this.b2 = 0;',
  3012. 'this.ni = 0;',
  3013. '']),
  3014. LinesToStr([ // this.$main
  3015. '$mod.w = 0x1234;',
  3016. '$mod.ss = -0x12;',
  3017. '$mod.b = $mod.ss & 0xFF;',
  3018. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3019. '$mod.b = $mod.w & 0xFF;',
  3020. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3021. '$mod.b2 = $mod.b & 0xF;',
  3022. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3023. '$mod.lw = 0x1234CDEF;',
  3024. '$mod.w = $mod.lw & 0xFFFF;',
  3025. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3026. '$mod.ni = 0x123456789ABCD;',
  3027. '$mod.lw = $mod.ni >>> 0;',
  3028. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3029. '']));
  3030. end;
  3031. procedure TTestModule.TestLoHiDelphiMode;
  3032. begin
  3033. StartProgram(false);
  3034. Add([
  3035. '{$mode delphi}',
  3036. 'const',
  3037. ' LoByte1 = Lo(Word($1234));',
  3038. ' HiByte1 = Hi(Word($1234));',
  3039. ' LoByte2 = Lo(SmallInt($1234));',
  3040. ' HiByte2 = Hi(SmallInt($1234));',
  3041. ' LoByte3 = Lo($1234CDEF);',
  3042. ' HiByte3 = Hi($1234CDEF);',
  3043. ' LoByte4 = Lo(-$1234CDEF);',
  3044. ' HiByte4 = Hi(-$1234CDEF);',
  3045. 'var',
  3046. ' b: Byte;',
  3047. ' w: Word;',
  3048. ' si: SmallInt;',
  3049. ' lw: LongWord;',
  3050. ' li: LongInt;',
  3051. 'begin',
  3052. ' w := $1234;',
  3053. ' b := lo(w);',
  3054. ' b := HI(w);',
  3055. ' lw := $1234CDEF;',
  3056. ' b := lo(lw);',
  3057. ' b := hi(lw);',
  3058. '']);
  3059. ConvertProgram;
  3060. CheckSource('TestLoHiDelphiMode',
  3061. LinesToStr([ // statements
  3062. 'this.LoByte1 = 0x1234 & 0xFF;',
  3063. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3064. 'this.LoByte2 = 0x1234 & 0xFF;',
  3065. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3066. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3067. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3068. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3069. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3070. 'this.b = 0;',
  3071. 'this.w = 0;',
  3072. 'this.si = 0;',
  3073. 'this.lw = 0;',
  3074. 'this.li = 0;'
  3075. ]),
  3076. LinesToStr([ // this.$main
  3077. '$mod.w = 0x1234;',
  3078. '$mod.b = $mod.w & 0xFF;',
  3079. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3080. '$mod.lw = 0x1234CDEF;',
  3081. '$mod.b = $mod.lw & 0xFF;',
  3082. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3083. ]));
  3084. end;
  3085. procedure TTestModule.TestAssignments;
  3086. begin
  3087. StartProgram(false);
  3088. Parser.Options:=Parser.Options+[po_cassignments];
  3089. Add('var');
  3090. Add(' Bar:longint;');
  3091. Add('begin');
  3092. Add(' bar:=3;');
  3093. Add(' bar+=4;');
  3094. Add(' bar-=5;');
  3095. Add(' bar*=6;');
  3096. ConvertProgram;
  3097. CheckSource('TestAssignments',
  3098. LinesToStr([ // statements
  3099. 'this.Bar = 0;'
  3100. ]),
  3101. LinesToStr([ // this.$main
  3102. '$mod.Bar=3;',
  3103. '$mod.Bar+=4;',
  3104. '$mod.Bar-=5;',
  3105. '$mod.Bar*=6;'
  3106. ]));
  3107. end;
  3108. procedure TTestModule.TestArithmeticOperators1;
  3109. begin
  3110. StartProgram(false);
  3111. Add('var');
  3112. Add(' vA,vB,vC:longint;');
  3113. Add('begin');
  3114. Add(' va:=1;');
  3115. Add(' vb:=va+va;');
  3116. Add(' vb:=va div vb;');
  3117. Add(' vb:=va mod vb;');
  3118. Add(' vb:=va+va*vb+va div vb;');
  3119. Add(' vc:=-va;');
  3120. Add(' va:=va-vb;');
  3121. Add(' vb:=va;');
  3122. Add(' if va<vb then vc:=va else vc:=vb;');
  3123. ConvertProgram;
  3124. CheckSource('TestArithmeticOperators1',
  3125. LinesToStr([ // statements
  3126. 'this.vA = 0;',
  3127. 'this.vB = 0;',
  3128. 'this.vC = 0;'
  3129. ]),
  3130. LinesToStr([ // this.$main
  3131. '$mod.vA = 1;',
  3132. '$mod.vB = $mod.vA + $mod.vA;',
  3133. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3134. '$mod.vB = $mod.vA % $mod.vB;',
  3135. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3136. '$mod.vC = -$mod.vA;',
  3137. '$mod.vA = $mod.vA - $mod.vB;',
  3138. '$mod.vB = $mod.vA;',
  3139. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3140. ]));
  3141. end;
  3142. procedure TTestModule.TestMultiAdd;
  3143. begin
  3144. StartProgram(false);
  3145. Add([
  3146. 'function Fly: string; external name ''fly'';',
  3147. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3148. 'var',
  3149. ' Date: double;',
  3150. 'begin',
  3151. ' Result:=(Year>0) and (Year<10000) and',
  3152. ' (Month >= 1) and (Month<=12) and',
  3153. ' (Day>0) and (Day<=31);',
  3154. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3155. 'end;',
  3156. 'var s: string;',
  3157. 'begin',
  3158. ' s:=''a''+''b''+''c''+''d'';',
  3159. ' s:=s+Fly+''e'';',
  3160. ' s:=Fly+Fly+Fly;',
  3161. '']);
  3162. ConvertProgram;
  3163. CheckSource('TestMultiAdd',
  3164. LinesToStr([ // statements
  3165. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3166. ' var Result = false;',
  3167. ' var date = 0.0;',
  3168. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3169. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3170. ' return Result;',
  3171. '};',
  3172. 'this.s = "";',
  3173. '']),
  3174. LinesToStr([ // this.$main
  3175. '$mod.s = "a" + "b" + "c" + "d";',
  3176. '$mod.s = $mod.s + fly() + "e";',
  3177. '$mod.s = fly() + fly() + fly();',
  3178. '']));
  3179. end;
  3180. procedure TTestModule.TestLogicalOperators;
  3181. begin
  3182. StartProgram(false);
  3183. Add('var');
  3184. Add(' vA,vB,vC:boolean;');
  3185. Add('begin');
  3186. Add(' va:=vb and vc;');
  3187. Add(' va:=vb or vc;');
  3188. Add(' va:=vb xor vc;');
  3189. Add(' va:=true and vc;');
  3190. Add(' va:=(vb and vc) or (va and vb);');
  3191. Add(' va:=not vb;');
  3192. ConvertProgram;
  3193. CheckSource('TestLogicalOperators',
  3194. LinesToStr([ // statements
  3195. 'this.vA = false;',
  3196. 'this.vB = false;',
  3197. 'this.vC = false;'
  3198. ]),
  3199. LinesToStr([ // this.$main
  3200. '$mod.vA = $mod.vB && $mod.vC;',
  3201. '$mod.vA = $mod.vB || $mod.vC;',
  3202. '$mod.vA = $mod.vB ^ $mod.vC;',
  3203. '$mod.vA = true && $mod.vC;',
  3204. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3205. '$mod.vA = !$mod.vB;'
  3206. ]));
  3207. end;
  3208. procedure TTestModule.TestBitwiseOperators;
  3209. begin
  3210. StartProgram(false);
  3211. Add([
  3212. 'var',
  3213. ' vA,vB,vC:longint;',
  3214. ' X,Y,Z: nativeint;',
  3215. 'begin',
  3216. ' va:=vb and vc;',
  3217. ' va:=vb or vc;',
  3218. ' va:=vb xor vc;',
  3219. ' va:=vb shl vc;',
  3220. ' va:=vb shr vc;',
  3221. ' va:=3 and vc;',
  3222. ' va:=(vb and vc) or (va and vb);',
  3223. ' va:=not vb;',
  3224. ' X:=Y and Z;',
  3225. ' X:=Y and va;',
  3226. ' X:=Y or Z;',
  3227. ' X:=Y or va;',
  3228. ' X:=Y xor Z;',
  3229. ' X:=Y xor va;',
  3230. '']);
  3231. ConvertProgram;
  3232. CheckSource('TestBitwiseOperators',
  3233. LinesToStr([ // statements
  3234. 'this.vA = 0;',
  3235. 'this.vB = 0;',
  3236. 'this.vC = 0;',
  3237. 'this.X = 0;',
  3238. 'this.Y = 0;',
  3239. 'this.Z = 0;',
  3240. '']),
  3241. LinesToStr([ // this.$main
  3242. '$mod.vA = $mod.vB & $mod.vC;',
  3243. '$mod.vA = $mod.vB | $mod.vC;',
  3244. '$mod.vA = $mod.vB ^ $mod.vC;',
  3245. '$mod.vA = $mod.vB << $mod.vC;',
  3246. '$mod.vA = $mod.vB >>> $mod.vC;',
  3247. '$mod.vA = 3 & $mod.vC;',
  3248. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3249. '$mod.vA = ~$mod.vB;',
  3250. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3251. '$mod.X = $mod.Y & $mod.vA;',
  3252. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3253. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3254. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3255. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3256. '']));
  3257. end;
  3258. procedure TTestModule.TestBitwiseOperatorsLongword;
  3259. begin
  3260. StartProgram(false);
  3261. Add([
  3262. 'var',
  3263. ' a,b,c:longword;',
  3264. ' i: longint;',
  3265. 'begin',
  3266. ' a:=$12345678;',
  3267. ' b:=$EDCBA987;',
  3268. ' c:=not a;',
  3269. ' c:=a and b;',
  3270. ' c:=a and $ffff0000;',
  3271. ' c:=a or b;',
  3272. ' c:=a or $ff00ff00;',
  3273. ' c:=a xor b;',
  3274. ' c:=a xor $f0f0f0f0;',
  3275. ' c:=a shl 1;',
  3276. ' c:=a shl 16;',
  3277. ' c:=a shl 24;',
  3278. ' c:=a shl b;',
  3279. ' c:=a shr 1;',
  3280. ' c:=a shr 16;',
  3281. ' c:=a shr 24;',
  3282. ' c:=a shr b;',
  3283. ' c:=(b and c) or (a and b);',
  3284. ' c:=i and a;',
  3285. ' c:=i or a;',
  3286. ' c:=i xor a;',
  3287. '']);
  3288. ConvertProgram;
  3289. CheckSource('TestBitwiseOperatorsLongword',
  3290. LinesToStr([ // statements
  3291. 'this.a = 0;',
  3292. 'this.b = 0;',
  3293. 'this.c = 0;',
  3294. 'this.i = 0;',
  3295. '']),
  3296. LinesToStr([ // this.$main
  3297. '$mod.a = 0x12345678;',
  3298. '$mod.b = 0xEDCBA987;',
  3299. '$mod.c = rtl.lw(~$mod.a);',
  3300. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3301. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3302. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3303. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3304. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3305. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3306. '$mod.c = rtl.lw($mod.a << 1);',
  3307. '$mod.c = rtl.lw($mod.a << 16);',
  3308. '$mod.c = rtl.lw($mod.a << 24);',
  3309. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3310. '$mod.c = rtl.lw($mod.a >>> 1);',
  3311. '$mod.c = rtl.lw($mod.a >>> 16);',
  3312. '$mod.c = rtl.lw($mod.a >>> 24);',
  3313. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3314. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3315. '$mod.c = $mod.i & $mod.a;',
  3316. '$mod.c = $mod.i | $mod.a;',
  3317. '$mod.c = $mod.i ^ $mod.a;',
  3318. '']));
  3319. end;
  3320. procedure TTestModule.TestPrgProcVar;
  3321. begin
  3322. StartProgram(false);
  3323. Add('procedure Proc1;');
  3324. Add('type');
  3325. Add(' t1=longint;');
  3326. Add('var');
  3327. Add(' vA:t1;');
  3328. Add('begin');
  3329. Add('end;');
  3330. Add('begin');
  3331. ConvertProgram;
  3332. CheckSource('TestPrgProcVar',
  3333. LinesToStr([ // statements
  3334. 'this.Proc1 = function () {',
  3335. ' var vA=0;',
  3336. '};'
  3337. ]),
  3338. LinesToStr([ // this.$main
  3339. ''
  3340. ]));
  3341. end;
  3342. procedure TTestModule.TestUnitProcVar;
  3343. begin
  3344. StartUnit(false);
  3345. Add('interface');
  3346. Add('');
  3347. Add('type tA=string; // unit scope');
  3348. Add('procedure Proc1;');
  3349. Add('');
  3350. Add('implementation');
  3351. Add('');
  3352. Add('procedure Proc1;');
  3353. Add('type tA=longint; // local proc scope');
  3354. Add('var v1:tA; // using local tA');
  3355. Add('begin');
  3356. Add('end;');
  3357. Add('var v2:tA; // using interface tA');
  3358. ConvertUnit;
  3359. CheckSource('TestUnitProcVar',
  3360. LinesToStr([ // statements
  3361. 'var $impl = $mod.$impl;',
  3362. 'this.Proc1 = function () {',
  3363. ' var v1 = 0;',
  3364. '};',
  3365. '']),
  3366. // this.$init
  3367. '',
  3368. // implementation
  3369. LinesToStr([
  3370. '$impl.v2 = "";',
  3371. '']));
  3372. end;
  3373. procedure TTestModule.TestImplProc;
  3374. begin
  3375. StartUnit(false);
  3376. Add('interface');
  3377. Add('');
  3378. Add('procedure Proc1;');
  3379. Add('');
  3380. Add('implementation');
  3381. Add('');
  3382. Add('procedure Proc1; begin end;');
  3383. Add('procedure Proc2; begin end;');
  3384. Add('initialization');
  3385. Add(' Proc1;');
  3386. Add(' Proc2;');
  3387. ConvertUnit;
  3388. CheckSource('TestImplProc',
  3389. LinesToStr([ // statements
  3390. 'var $impl = $mod.$impl;',
  3391. 'this.Proc1 = function () {',
  3392. '};',
  3393. '']),
  3394. LinesToStr([ // this.$init
  3395. '$mod.Proc1();',
  3396. '$impl.Proc2();',
  3397. '']),
  3398. LinesToStr([ // implementation
  3399. '$impl.Proc2 = function () {',
  3400. '};',
  3401. ''])
  3402. );
  3403. end;
  3404. procedure TTestModule.TestFunctionResult;
  3405. begin
  3406. StartProgram(false);
  3407. Add('function Func1: longint;');
  3408. Add('begin');
  3409. Add(' Result:=3;');
  3410. Add(' Func1:=4;');
  3411. Add('end;');
  3412. Add('begin');
  3413. ConvertProgram;
  3414. CheckSource('TestFunctionResult',
  3415. LinesToStr([ // statements
  3416. 'this.Func1 = function () {',
  3417. ' var Result = 0;',
  3418. ' Result = 3;',
  3419. ' Result = 4;',
  3420. ' return Result;',
  3421. '};'
  3422. ]),
  3423. '');
  3424. end;
  3425. procedure TTestModule.TestNestedProc;
  3426. begin
  3427. StartProgram(false);
  3428. Add([
  3429. 'var vInUnit: longint;',
  3430. 'function DoIt(pA,pD: longint): longint;',
  3431. 'var',
  3432. ' vB: longint;',
  3433. ' vC: longint;',
  3434. ' function Nesty(pA: longint): longint; ',
  3435. ' var vB: longint;',
  3436. ' begin',
  3437. ' Result:=pa+vb+vc+pd+vInUnit;',
  3438. ' nesty:=3;',
  3439. ' doit:=4;',
  3440. ' exit;',
  3441. ' end;',
  3442. 'begin',
  3443. ' Result:=pa+vb+vc;',
  3444. ' doit:=6;',
  3445. ' exit;',
  3446. 'end;',
  3447. 'begin']);
  3448. ConvertProgram;
  3449. CheckSource('TestNestedProc',
  3450. LinesToStr([ // statements
  3451. 'this.vInUnit = 0;',
  3452. 'this.DoIt = function (pA, pD) {',
  3453. ' var Result = 0;',
  3454. ' var vB = 0;',
  3455. ' var vC = 0;',
  3456. ' function Nesty(pA) {',
  3457. ' var Result$1 = 0;',
  3458. ' var vB = 0;',
  3459. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3460. ' Result$1 = 3;',
  3461. ' Result = 4;',
  3462. ' return Result$1;',
  3463. ' return Result$1;',
  3464. ' };',
  3465. ' Result = pA + vB + vC;',
  3466. ' Result = 6;',
  3467. ' return Result;',
  3468. ' return Result;',
  3469. '};'
  3470. ]),
  3471. '');
  3472. end;
  3473. procedure TTestModule.TestNestedProc_ResultString;
  3474. begin
  3475. StartProgram(false);
  3476. Add([
  3477. 'function DoIt: string;',
  3478. ' function Nesty: string; ',
  3479. ' begin',
  3480. ' nesty:=#65#66;',
  3481. ' nesty[1]:=#67;',
  3482. ' doit:=#68;',
  3483. ' doit[2]:=#69;',
  3484. ' end;',
  3485. 'begin',
  3486. ' doit:=#70;',
  3487. ' doit[3]:=#71;',
  3488. 'end;',
  3489. 'begin']);
  3490. ConvertProgram;
  3491. CheckSource('TestNestedProc_ResultString',
  3492. LinesToStr([ // statements
  3493. 'this.DoIt = function () {',
  3494. ' var Result = "";',
  3495. ' function Nesty() {',
  3496. ' var Result$1 = "";',
  3497. ' Result$1 = "AB";',
  3498. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3499. ' Result = "D";',
  3500. ' Result = rtl.setCharAt(Result, 1, "E");',
  3501. ' return Result$1;',
  3502. ' };',
  3503. ' Result = "F";',
  3504. ' Result = rtl.setCharAt(Result, 2, "G");',
  3505. ' return Result;',
  3506. '};'
  3507. ]),
  3508. '');
  3509. end;
  3510. procedure TTestModule.TestForwardProc;
  3511. begin
  3512. StartProgram(false);
  3513. Add('procedure FuncA(Bar: longint); forward;');
  3514. Add('procedure FuncB(Bar: longint);');
  3515. Add('begin');
  3516. Add(' funca(bar);');
  3517. Add('end;');
  3518. Add('procedure funca(bar: longint);');
  3519. Add('begin');
  3520. Add(' if bar=3 then ;');
  3521. Add('end;');
  3522. Add('begin');
  3523. Add(' funca(4);');
  3524. Add(' funcb(5);');
  3525. ConvertProgram;
  3526. CheckSource('TestForwardProc',
  3527. LinesToStr([ // statements'
  3528. 'this.FuncB = function (Bar) {',
  3529. ' $mod.FuncA(Bar);',
  3530. '};',
  3531. 'this.FuncA = function (Bar) {',
  3532. ' if (Bar === 3);',
  3533. '};'
  3534. ]),
  3535. LinesToStr([
  3536. '$mod.FuncA(4);',
  3537. '$mod.FuncB(5);'
  3538. ])
  3539. );
  3540. end;
  3541. procedure TTestModule.TestNestedForwardProc;
  3542. begin
  3543. StartProgram(false);
  3544. Add('procedure FuncA;');
  3545. Add(' procedure FuncB(i: longint); forward;');
  3546. Add(' procedure FuncC(i: longint);');
  3547. Add(' begin');
  3548. Add(' funcb(i);');
  3549. Add(' end;');
  3550. Add(' procedure FuncB(i: longint);');
  3551. Add(' begin');
  3552. Add(' if i=3 then ;');
  3553. Add(' end;');
  3554. Add('begin');
  3555. Add(' funcc(4)');
  3556. Add('end;');
  3557. Add('begin');
  3558. Add(' funca;');
  3559. ConvertProgram;
  3560. CheckSource('TestNestedForwardProc',
  3561. LinesToStr([ // statements'
  3562. 'this.FuncA = function () {',
  3563. ' function FuncC(i) {',
  3564. ' FuncB(i);',
  3565. ' };',
  3566. ' function FuncB(i) {',
  3567. ' if (i === 3);',
  3568. ' };',
  3569. ' FuncC(4);',
  3570. '};'
  3571. ]),
  3572. LinesToStr([
  3573. '$mod.FuncA();'
  3574. ])
  3575. );
  3576. end;
  3577. procedure TTestModule.TestAssignFunctionResult;
  3578. begin
  3579. StartProgram(false);
  3580. Add('function Func1: longint;');
  3581. Add('begin');
  3582. Add('end;');
  3583. Add('var i: longint;');
  3584. Add('begin');
  3585. Add(' i:=func1();');
  3586. Add(' i:=func1()+func1();');
  3587. ConvertProgram;
  3588. CheckSource('TestAssignFunctionResult',
  3589. LinesToStr([ // statements
  3590. 'this.Func1 = function () {',
  3591. ' var Result = 0;',
  3592. ' return Result;',
  3593. '};',
  3594. 'this.i = 0;'
  3595. ]),
  3596. LinesToStr([
  3597. '$mod.i = $mod.Func1();',
  3598. '$mod.i = $mod.Func1() + $mod.Func1();'
  3599. ]));
  3600. end;
  3601. procedure TTestModule.TestFunctionResultInCondition;
  3602. begin
  3603. StartProgram(false);
  3604. Add('function Func1: longint;');
  3605. Add('begin');
  3606. Add('end;');
  3607. Add('function Func2: boolean;');
  3608. Add('begin');
  3609. Add('end;');
  3610. Add('var i: longint;');
  3611. Add('begin');
  3612. Add(' if func2 then ;');
  3613. Add(' if i=func1() then ;');
  3614. Add(' if i=func1 then ;');
  3615. ConvertProgram;
  3616. CheckSource('TestFunctionResultInCondition',
  3617. LinesToStr([ // statements
  3618. 'this.Func1 = function () {',
  3619. ' var Result = 0;',
  3620. ' return Result;',
  3621. '};',
  3622. 'this.Func2 = function () {',
  3623. ' var Result = false;',
  3624. ' return Result;',
  3625. '};',
  3626. 'this.i = 0;'
  3627. ]),
  3628. LinesToStr([
  3629. 'if ($mod.Func2());',
  3630. 'if ($mod.i === $mod.Func1());',
  3631. 'if ($mod.i === $mod.Func1());'
  3632. ]));
  3633. end;
  3634. procedure TTestModule.TestFunctionResultInForLoop;
  3635. begin
  3636. StartProgram(false);
  3637. Add([
  3638. 'function Func1(a: array of longint): longint;',
  3639. 'begin',
  3640. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3641. ' for Result in a do if a[Result]=0 then exit;',
  3642. 'end;',
  3643. 'begin',
  3644. ' Func1([1,2,3])']);
  3645. ConvertProgram;
  3646. CheckSource('TestFunctionResultInForLoop',
  3647. LinesToStr([ // statements
  3648. 'this.Func1 = function (a) {',
  3649. ' var Result = 0;',
  3650. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3651. ' Result = $l;',
  3652. ' if (a[Result] === 0) return Result;',
  3653. ' };',
  3654. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3655. ' Result = $in[$l1];',
  3656. ' if (a[Result] === 0) return Result;',
  3657. ' };',
  3658. ' return Result;',
  3659. '};',
  3660. '']),
  3661. LinesToStr([
  3662. '$mod.Func1([1, 2, 3]);'
  3663. ]));
  3664. end;
  3665. procedure TTestModule.TestFunctionResultInTypeCast;
  3666. begin
  3667. StartProgram(false);
  3668. Add([
  3669. 'function GetInt: longint;',
  3670. 'begin',
  3671. 'end;',
  3672. 'begin',
  3673. ' if Byte(GetInt)=0 then ;',
  3674. '']);
  3675. ConvertProgram;
  3676. CheckSource('TestFunctionResultInTypeCast',
  3677. LinesToStr([ // statements
  3678. 'this.GetInt = function () {',
  3679. ' var Result = 0;',
  3680. ' return Result;',
  3681. '};',
  3682. '']),
  3683. LinesToStr([
  3684. 'if (($mod.GetInt() & 255) === 0) ;'
  3685. ]));
  3686. end;
  3687. procedure TTestModule.TestExit;
  3688. begin
  3689. StartProgram(false);
  3690. Add('procedure ProcA;');
  3691. Add('begin');
  3692. Add(' exit;');
  3693. Add('end;');
  3694. Add('function FuncB: longint;');
  3695. Add('begin');
  3696. Add(' exit;');
  3697. Add(' exit(3);');
  3698. Add('end;');
  3699. Add('function FuncC: string;');
  3700. Add('begin');
  3701. Add(' exit;');
  3702. Add(' exit(''a'');');
  3703. Add(' exit(''abc'');');
  3704. Add('end;');
  3705. Add('begin');
  3706. Add(' exit;');
  3707. Add(' exit(1);');
  3708. ConvertProgram;
  3709. CheckSource('TestExit',
  3710. LinesToStr([ // statements
  3711. 'this.ProcA = function () {',
  3712. ' return;',
  3713. '};',
  3714. 'this.FuncB = function () {',
  3715. ' var Result = 0;',
  3716. ' return Result;',
  3717. ' return 3;',
  3718. ' return Result;',
  3719. '};',
  3720. 'this.FuncC = function () {',
  3721. ' var Result = "";',
  3722. ' return Result;',
  3723. ' return "a";',
  3724. ' return "abc";',
  3725. ' return Result;',
  3726. '};'
  3727. ]),
  3728. LinesToStr([
  3729. 'return;',
  3730. 'return 1;',
  3731. '']));
  3732. end;
  3733. procedure TTestModule.TestExit_ResultInFinally;
  3734. begin
  3735. StartProgram(false);
  3736. Add([
  3737. 'function Run: word;',
  3738. 'begin',
  3739. ' try',
  3740. ' exit(3);', // no Result in finally -> use return 3
  3741. ' finally',
  3742. ' end;',
  3743. 'end;',
  3744. 'function Fly: word;',
  3745. 'begin',
  3746. ' try',
  3747. ' exit(3);',
  3748. ' finally',
  3749. ' if Result>0 then ;',
  3750. ' end;',
  3751. 'end;',
  3752. 'function Jump: word;',
  3753. 'begin',
  3754. ' try',
  3755. ' try',
  3756. ' exit(4);',
  3757. ' finally',
  3758. ' end;',
  3759. ' finally',
  3760. ' if Result>0 then ;',
  3761. ' end;',
  3762. 'end;',
  3763. 'begin',
  3764. '']);
  3765. ConvertProgram;
  3766. CheckSource('TestExit_ResultInFinally',
  3767. LinesToStr([ // statements
  3768. 'this.Run = function () {',
  3769. ' var Result = 0;',
  3770. ' try {',
  3771. ' return 3;',
  3772. ' } finally {',
  3773. ' };',
  3774. ' return Result;',
  3775. '};',
  3776. 'this.Fly = function () {',
  3777. ' var Result = 0;',
  3778. ' try {',
  3779. ' Result = 3;',
  3780. ' return Result;',
  3781. ' } finally {',
  3782. ' if (Result > 0) ;',
  3783. ' };',
  3784. ' return Result;',
  3785. '};',
  3786. 'this.Jump = function () {',
  3787. ' var Result = 0;',
  3788. ' try {',
  3789. ' try {',
  3790. ' Result = 4;',
  3791. ' return Result;',
  3792. ' } finally {',
  3793. ' };',
  3794. ' } finally {',
  3795. ' if (Result > 0) ;',
  3796. ' };',
  3797. ' return Result;',
  3798. '};',
  3799. '']),
  3800. LinesToStr([
  3801. '']));
  3802. end;
  3803. procedure TTestModule.TestBreak;
  3804. begin
  3805. StartProgram(false);
  3806. Add([
  3807. 'var',
  3808. ' i: longint;',
  3809. 'begin',
  3810. ' repeat',
  3811. ' break;',
  3812. ' until true;',
  3813. ' while true do',
  3814. ' break;',
  3815. ' for i:=1 to 2 do',
  3816. ' break;']);
  3817. ConvertProgram;
  3818. CheckSource('TestBreak',
  3819. LinesToStr([ // statements
  3820. 'this.i = 0;'
  3821. ]),
  3822. LinesToStr([
  3823. 'do {',
  3824. ' break;',
  3825. '} while (!true);',
  3826. 'while (true) break;',
  3827. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3828. '']));
  3829. end;
  3830. procedure TTestModule.TestBreakAsVar;
  3831. begin
  3832. StartProgram(false);
  3833. Add([
  3834. 'procedure DoIt(break: boolean);',
  3835. 'begin',
  3836. ' if break then ;',
  3837. 'end;',
  3838. 'var',
  3839. ' break: boolean;',
  3840. 'begin',
  3841. ' if break then ;']);
  3842. ConvertProgram;
  3843. CheckSource('TestBreakAsVar',
  3844. LinesToStr([ // statements
  3845. 'this.DoIt = function (Break) {',
  3846. ' if (Break) ;',
  3847. '};',
  3848. 'this.Break = false;',
  3849. '']),
  3850. LinesToStr([
  3851. 'if($mod.Break) ;',
  3852. '']));
  3853. end;
  3854. procedure TTestModule.TestContinue;
  3855. begin
  3856. StartProgram(false);
  3857. Add('var i: longint;');
  3858. Add('begin');
  3859. Add(' repeat');
  3860. Add(' continue;');
  3861. Add(' until true;');
  3862. Add(' while true do');
  3863. Add(' continue;');
  3864. Add(' for i:=1 to 2 do');
  3865. Add(' continue;');
  3866. ConvertProgram;
  3867. CheckSource('TestContinue',
  3868. LinesToStr([ // statements
  3869. 'this.i = 0;'
  3870. ]),
  3871. LinesToStr([
  3872. 'do {',
  3873. ' continue;',
  3874. '} while (!true);',
  3875. 'while (true) continue;',
  3876. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3877. '']));
  3878. end;
  3879. procedure TTestModule.TestProc_External;
  3880. begin
  3881. StartProgram(false);
  3882. Add('procedure Foo; external name ''console.log'';');
  3883. Add('function Bar: longint; external name ''get.item'';');
  3884. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3885. Add('var');
  3886. Add(' i: longint;');
  3887. Add('begin');
  3888. Add(' Foo;');
  3889. Add(' i:=Bar;');
  3890. Add(' i:=Bla(''abc'');');
  3891. ConvertProgram;
  3892. CheckSource('TestProc_External',
  3893. LinesToStr([ // statements
  3894. 'this.i = 0;'
  3895. ]),
  3896. LinesToStr([
  3897. 'console.log();',
  3898. '$mod.i = get.item();',
  3899. '$mod.i = apply.something("abc");'
  3900. ]));
  3901. end;
  3902. procedure TTestModule.TestProc_ExternalOtherUnit;
  3903. begin
  3904. AddModuleWithIntfImplSrc('unit2.pas',
  3905. LinesToStr([
  3906. 'procedure Now; external name ''Date.now'';',
  3907. 'procedure DoIt;'
  3908. ]),
  3909. 'procedure doit; begin end;');
  3910. StartUnit(true);
  3911. Add('interface');
  3912. Add('uses unit2;');
  3913. Add('implementation');
  3914. Add('begin');
  3915. Add(' now;');
  3916. Add(' now();');
  3917. Add(' uNit2.now;');
  3918. Add(' uNit2.now();');
  3919. Add(' doit;');
  3920. Add(' uNit2.doit;');
  3921. ConvertUnit;
  3922. CheckSource('TestProc_ExternalOtherUnit',
  3923. LinesToStr([
  3924. '']),
  3925. LinesToStr([
  3926. 'Date.now();',
  3927. 'Date.now();',
  3928. 'Date.now();',
  3929. 'Date.now();',
  3930. 'pas.unit2.DoIt();',
  3931. 'pas.unit2.DoIt();',
  3932. '']));
  3933. end;
  3934. procedure TTestModule.TestProc_Asm;
  3935. begin
  3936. StartProgram(false);
  3937. Add([
  3938. '{$mode delphi}',
  3939. 'function DoIt: longint;',
  3940. 'begin;',
  3941. ' asm',
  3942. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3943. ' end;',
  3944. ' asm console.log(); end;',
  3945. ' asm',
  3946. ' s = "'' ";',
  3947. ' s = ''" '';',
  3948. ' s = s + "world" + "''";',
  3949. ' // end',
  3950. ' s = ''end'';',
  3951. ' s = "end";',
  3952. ' s = "foo\"bar";',
  3953. ' s = ''a\''b'';',
  3954. ' s = `${expr}\`-"-''-`;',
  3955. ' s = `multi',
  3956. 'line`;',
  3957. ' end;',
  3958. 'end;',
  3959. 'procedure Fly;',
  3960. 'asm',
  3961. ' return;',
  3962. 'end;',
  3963. 'begin']);
  3964. ConvertProgram;
  3965. CheckSource('TestProc_Asm',
  3966. LinesToStr([ // statements
  3967. 'this.DoIt = function () {',
  3968. ' var Result = 0;',
  3969. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3970. ' console.log();',
  3971. ' s = "'' ";',
  3972. ' s = ''" '';',
  3973. ' s = s + "world" + "''";',
  3974. ' // end',
  3975. ' s = ''end'';',
  3976. ' s = "end";',
  3977. ' s = "foo\"bar";',
  3978. ' s = ''a\''b'';',
  3979. ' s = `${expr}\`-"-''-`;',
  3980. ' s = `multi',
  3981. 'line`;',
  3982. ' return Result;',
  3983. '};',
  3984. 'this.Fly = function () {',
  3985. ' return;',
  3986. '};',
  3987. '']),
  3988. LinesToStr([
  3989. ''
  3990. ]));
  3991. end;
  3992. procedure TTestModule.TestProc_AsmSubBlock;
  3993. begin
  3994. StartProgram(true,[supTObject]);
  3995. Add([
  3996. '{$mode delphi}',
  3997. 'type',
  3998. ' TBird = class end;',
  3999. 'procedure Run(w: word);',
  4000. 'begin;',
  4001. ' if true then asm console.log(); end;',
  4002. ' if w>3 then asm',
  4003. ' var a = w+1;',
  4004. ' w = a+3;',
  4005. ' end;',
  4006. ' while (w>7) do asm',
  4007. ' w+=3; w*=2;',
  4008. ' end;',
  4009. ' try',
  4010. ' except',
  4011. ' on E: TBird do',
  4012. ' asm console.log(E); end;',
  4013. ' on E: TObject do',
  4014. ' asm var i=3; i--; end;',
  4015. ' else asm Fly; High; end;',
  4016. ' end;',
  4017. 'end;',
  4018. 'begin']);
  4019. ConvertProgram;
  4020. CheckSource('TestProc_AsmSubBlock',
  4021. LinesToStr([ // statements
  4022. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4023. '});',
  4024. 'this.Run = function (w) {',
  4025. ' if (true) console.log();',
  4026. ' if (w > 3) {',
  4027. ' var a = w+1;',
  4028. ' w = a+3;',
  4029. ' };',
  4030. ' while (w > 7) {',
  4031. ' w+=3; w*=2;',
  4032. ' };',
  4033. ' try {} catch ($e) {',
  4034. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4035. ' var E = $e;',
  4036. ' console.log(E);',
  4037. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4038. ' var E = $e;',
  4039. ' var i=3; i--;',
  4040. ' } else {',
  4041. ' Fly; High;',
  4042. ' }',
  4043. ' };',
  4044. '};',
  4045. '']),
  4046. LinesToStr([
  4047. ''
  4048. ]));
  4049. end;
  4050. procedure TTestModule.TestProc_Assembler;
  4051. begin
  4052. StartProgram(false);
  4053. Add('function DoIt: longint; assembler;');
  4054. Add('asm');
  4055. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4056. Add('end;');
  4057. Add('begin');
  4058. ConvertProgram;
  4059. CheckSource('TestProc_Assembler',
  4060. LinesToStr([ // statements
  4061. 'this.DoIt = function () {',
  4062. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4063. '};'
  4064. ]),
  4065. LinesToStr([
  4066. ''
  4067. ]));
  4068. end;
  4069. procedure TTestModule.TestProc_VarParam;
  4070. begin
  4071. StartProgram(false);
  4072. Add('type integer = longint;');
  4073. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4074. Add('var vJ: integer;');
  4075. Add('begin');
  4076. Add(' vg:=vg+1;');
  4077. Add(' vj:=vh+2;');
  4078. Add(' vi:=vi+3;');
  4079. Add(' doit(vg,vg,vg);');
  4080. Add(' doit(vh,vh,vj);');
  4081. Add(' doit(vi,vi,vi);');
  4082. Add(' doit(vj,vj,vj);');
  4083. Add('end;');
  4084. Add('var i: integer;');
  4085. Add('begin');
  4086. Add(' doit(i,i,i);');
  4087. ConvertProgram;
  4088. CheckSource('TestProc_VarParam',
  4089. LinesToStr([ // statements
  4090. 'this.DoIt = function (vG,vH,vI) {',
  4091. ' var vJ = 0;',
  4092. ' vG = vG + 1;',
  4093. ' vJ = vH + 2;',
  4094. ' vI.set(vI.get()+3);',
  4095. ' $mod.DoIt(vG, vG, {',
  4096. ' get: function () {',
  4097. ' return vG;',
  4098. ' },',
  4099. ' set: function (v) {',
  4100. ' vG = v;',
  4101. ' }',
  4102. ' });',
  4103. ' $mod.DoIt(vH, vH, {',
  4104. ' get: function () {',
  4105. ' return vJ;',
  4106. ' },',
  4107. ' set: function (v) {',
  4108. ' vJ = v;',
  4109. ' }',
  4110. ' });',
  4111. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4112. ' $mod.DoIt(vJ, vJ, {',
  4113. ' get: function () {',
  4114. ' return vJ;',
  4115. ' },',
  4116. ' set: function (v) {',
  4117. ' vJ = v;',
  4118. ' }',
  4119. ' });',
  4120. '};',
  4121. 'this.i = 0;'
  4122. ]),
  4123. LinesToStr([
  4124. '$mod.DoIt($mod.i,$mod.i,{',
  4125. ' p: $mod,',
  4126. ' get: function () {',
  4127. ' return this.p.i;',
  4128. ' },',
  4129. ' set: function (v) {',
  4130. ' this.p.i = v;',
  4131. ' }',
  4132. '});'
  4133. ]));
  4134. end;
  4135. procedure TTestModule.TestProc_VarParamString;
  4136. begin
  4137. StartProgram(false);
  4138. Add(['type TCaption = string;',
  4139. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4140. 'var c: char;',
  4141. 'begin',
  4142. ' va[1]:=c;',
  4143. ' vb[2]:=c;',
  4144. ' vc[3]:=c;',
  4145. 'end;',
  4146. 'begin']);
  4147. ConvertProgram;
  4148. CheckSource('TestProc_VarParamString',
  4149. LinesToStr([ // statements
  4150. 'this.DoIt = function (vA,vB,vC) {',
  4151. ' var c = "";',
  4152. ' vA = rtl.setCharAt(vA, 0, c);',
  4153. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4154. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4155. '};',
  4156. '']),
  4157. LinesToStr([
  4158. ]));
  4159. end;
  4160. procedure TTestModule.TestProc_VarParamV;
  4161. begin
  4162. StartProgram(false);
  4163. Add([
  4164. 'procedure Inc2(var i: longint);',
  4165. 'begin',
  4166. ' i:=i+2;',
  4167. 'end;',
  4168. 'procedure DoIt(v: longint);',
  4169. 'var p: array of longint;',
  4170. 'begin',
  4171. ' Inc2(v);',
  4172. ' Inc2(p[v]);',
  4173. 'end;',
  4174. 'begin']);
  4175. ConvertProgram;
  4176. CheckSource('TestProc_VarParamV',
  4177. LinesToStr([ // statements
  4178. 'this.Inc2 = function (i) {',
  4179. ' i.set(i.get()+2);',
  4180. '};',
  4181. 'this.DoIt = function (v) {',
  4182. ' var p = [];',
  4183. ' $mod.Inc2({get: function () {',
  4184. ' return v;',
  4185. ' }, set: function (w) {',
  4186. ' v = w;',
  4187. ' }});',
  4188. ' $mod.Inc2({',
  4189. ' a: v,',
  4190. ' p: p,',
  4191. ' get: function () {',
  4192. ' return this.p[this.a];',
  4193. ' },',
  4194. ' set: function (v) {',
  4195. ' this.p[this.a] = v;',
  4196. ' }',
  4197. ' });',
  4198. '};',
  4199. '']),
  4200. LinesToStr([
  4201. '']));
  4202. end;
  4203. procedure TTestModule.TestProc_Overload;
  4204. begin
  4205. StartProgram(false);
  4206. Add('procedure DoIt(vI: longint); begin end;');
  4207. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4208. Add('procedure DoIt(vD: double); begin end;');
  4209. Add('begin');
  4210. Add(' DoIt(1);');
  4211. Add(' DoIt(2,3);');
  4212. Add(' DoIt(4.5);');
  4213. ConvertProgram;
  4214. CheckSource('TestProcedureOverload',
  4215. LinesToStr([ // statements
  4216. 'this.DoIt = function (vI) {',
  4217. '};',
  4218. 'this.DoIt$1 = function (vI, vJ) {',
  4219. '};',
  4220. 'this.DoIt$2 = function (vD) {',
  4221. '};',
  4222. '']),
  4223. LinesToStr([
  4224. '$mod.DoIt(1);',
  4225. '$mod.DoIt$1(2, 3);',
  4226. '$mod.DoIt$2(4.5);',
  4227. '']));
  4228. end;
  4229. procedure TTestModule.TestProc_OverloadForward;
  4230. begin
  4231. StartProgram(false);
  4232. Add('procedure DoIt(vI: longint); forward;');
  4233. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4234. Add('procedure doit(vi: longint); begin end;');
  4235. Add('begin');
  4236. Add(' doit(1);');
  4237. Add(' doit(2,3);');
  4238. ConvertProgram;
  4239. CheckSource('TestProcedureOverloadForward',
  4240. LinesToStr([ // statements
  4241. 'this.DoIt$1 = function (vI, vJ) {',
  4242. '};',
  4243. 'this.DoIt = function (vI) {',
  4244. '};',
  4245. '']),
  4246. LinesToStr([
  4247. '$mod.DoIt(1);',
  4248. '$mod.DoIt$1(2, 3);',
  4249. '']));
  4250. end;
  4251. procedure TTestModule.TestProc_OverloadIntfImpl;
  4252. begin
  4253. StartUnit(false);
  4254. Add('interface');
  4255. Add('procedure DoIt(vI: longint);');
  4256. Add('procedure DoIt(vI, vJ: longint);');
  4257. Add('implementation');
  4258. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4259. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4260. Add('procedure DoIt(vi: longint); begin end;');
  4261. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4262. Add('procedure DoIt(vi, vj: longint); begin end;');
  4263. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4264. Add('begin');
  4265. Add(' doit(1);');
  4266. Add(' doit(2,3);');
  4267. Add(' doit(4,5,6);');
  4268. Add(' doit(7,8,9,10);');
  4269. Add(' doit(11,12,13,14,15);');
  4270. ConvertUnit;
  4271. CheckSource('TestProcedureOverloadUnit',
  4272. LinesToStr([ // statements
  4273. 'var $impl = $mod.$impl;',
  4274. 'this.DoIt = function (vI) {',
  4275. '};',
  4276. 'this.DoIt$1 = function (vI, vJ) {',
  4277. '};',
  4278. '']),
  4279. LinesToStr([ // this.$init
  4280. '$mod.DoIt(1);',
  4281. '$mod.DoIt$1(2, 3);',
  4282. '$impl.DoIt$3(4,5,6);',
  4283. '$impl.DoIt$4(7,8,9,10);',
  4284. '$impl.DoIt$2(11,12,13,14,15);',
  4285. '']),
  4286. LinesToStr([ // implementation
  4287. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4288. '};',
  4289. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4290. '};',
  4291. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4292. '};',
  4293. '']));
  4294. end;
  4295. procedure TTestModule.TestProc_OverloadNested;
  4296. begin
  4297. StartProgram(false);
  4298. Add([
  4299. 'procedure doit(vA: longint);',
  4300. ' procedure DoIt(vA, vB: longint); overload;',
  4301. ' begin',
  4302. ' doit(1);',
  4303. ' doit(1,2);',
  4304. ' end;',
  4305. ' procedure doit(vA, vB, vC: longint);',
  4306. ' begin',
  4307. ' doit(1);',
  4308. ' doit(1,2);',
  4309. ' doit(1,2,3);',
  4310. ' end;',
  4311. 'begin',
  4312. ' doit(1);',
  4313. ' doit(1,2);',
  4314. ' doit(1,2,3);',
  4315. 'end;',
  4316. 'begin // main',
  4317. ' doit(1);']);
  4318. ConvertProgram;
  4319. CheckSource('TestProcedureOverloadNested',
  4320. LinesToStr([ // statements
  4321. 'this.doit = function (vA) {',
  4322. ' function DoIt$1(vA, vB) {',
  4323. ' $mod.doit(1);',
  4324. ' DoIt$1(1, 2);',
  4325. ' };',
  4326. ' function doit$2(vA, vB, vC) {',
  4327. ' $mod.doit(1);',
  4328. ' DoIt$1(1, 2);',
  4329. ' doit$2(1, 2, 3);',
  4330. ' };',
  4331. ' $mod.doit(1);',
  4332. ' DoIt$1(1, 2);',
  4333. ' doit$2(1, 2, 3);',
  4334. '};',
  4335. '']),
  4336. LinesToStr([
  4337. '$mod.doit(1);',
  4338. '']));
  4339. end;
  4340. procedure TTestModule.TestProc_OverloadNestedForward;
  4341. begin
  4342. StartProgram(false);
  4343. Add([
  4344. 'procedure DoIt(vA: longint); overload; forward;',
  4345. 'procedure DoIt(vB, vC: longint); overload;',
  4346. 'begin // 2 param overload',
  4347. ' doit(1);',
  4348. ' doit(1,2);',
  4349. 'end;',
  4350. 'procedure doit(vA: longint);',
  4351. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4352. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4353. ' begin // 4 param overload',
  4354. ' doit(1);',
  4355. ' doit(1,2);',
  4356. ' doit(1,2,3);',
  4357. ' doit(1,2,3,4);',
  4358. ' end;',
  4359. ' procedure doit(vA, vB, vC: longint);',
  4360. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4361. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4362. ' begin // 6 param overload',
  4363. ' doit(1);',
  4364. ' doit(1,2);',
  4365. ' doit(1,2,3);',
  4366. ' doit(1,2,3,4);',
  4367. ' doit(1,2,3,4,5);',
  4368. ' doit(1,2,3,4,5,6);',
  4369. ' end;',
  4370. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4371. ' begin // 5 param overload',
  4372. ' doit(1);',
  4373. ' doit(1,2);',
  4374. ' doit(1,2,3);',
  4375. ' doit(1,2,3,4);',
  4376. ' doit(1,2,3,4,5);',
  4377. ' doit(1,2,3,4,5,6);',
  4378. ' end;',
  4379. ' begin // 3 param overload',
  4380. ' doit(1);',
  4381. ' doit(1,2);',
  4382. ' doit(1,2,3);',
  4383. ' doit(1,2,3,4);',
  4384. ' doit(1,2,3,4,5);',
  4385. ' doit(1,2,3,4,5,6);',
  4386. ' end;',
  4387. 'begin // 1 param overload',
  4388. ' doit(1);',
  4389. ' doit(1,2);',
  4390. ' doit(1,2,3);',
  4391. ' doit(1,2,3,4);',
  4392. 'end;',
  4393. 'begin // main',
  4394. ' doit(1);',
  4395. ' doit(1,2);']);
  4396. ConvertProgram;
  4397. CheckSource('TestProc_OverloadNestedForward',
  4398. LinesToStr([ // statements
  4399. 'this.DoIt$1 = function (vB, vC) {',
  4400. ' $mod.DoIt(1);',
  4401. ' $mod.DoIt$1(1, 2);',
  4402. '};',
  4403. 'this.DoIt = function (vA) {',
  4404. ' function DoIt$3(vA, vB, vC, vD) {',
  4405. ' $mod.DoIt(1);',
  4406. ' $mod.DoIt$1(1, 2);',
  4407. ' DoIt$2(1, 2, 3);',
  4408. ' DoIt$3(1, 2, 3, 4);',
  4409. ' };',
  4410. ' function DoIt$2(vA, vB, vC) {',
  4411. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4412. ' $mod.DoIt(1);',
  4413. ' $mod.DoIt$1(1, 2);',
  4414. ' DoIt$2(1, 2, 3);',
  4415. ' DoIt$3(1, 2, 3, 4);',
  4416. ' DoIt$4(1, 2, 3, 4, 5);',
  4417. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4418. ' };',
  4419. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4420. ' $mod.DoIt(1);',
  4421. ' $mod.DoIt$1(1, 2);',
  4422. ' DoIt$2(1, 2, 3);',
  4423. ' DoIt$3(1, 2, 3, 4);',
  4424. ' DoIt$4(1, 2, 3, 4, 5);',
  4425. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4426. ' };',
  4427. ' $mod.DoIt(1);',
  4428. ' $mod.DoIt$1(1, 2);',
  4429. ' DoIt$2(1, 2, 3);',
  4430. ' DoIt$3(1, 2, 3, 4);',
  4431. ' DoIt$4(1, 2, 3, 4, 5);',
  4432. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4433. ' };',
  4434. ' $mod.DoIt(1);',
  4435. ' $mod.DoIt$1(1, 2);',
  4436. ' DoIt$2(1, 2, 3);',
  4437. ' DoIt$3(1, 2, 3, 4);',
  4438. '};',
  4439. '']),
  4440. LinesToStr([
  4441. '$mod.DoIt(1);',
  4442. '$mod.DoIt$1(1, 2);',
  4443. '']));
  4444. end;
  4445. procedure TTestModule.TestProc_OverloadUnitCycle;
  4446. begin
  4447. AddModuleWithIntfImplSrc('Unit2.pas',
  4448. LinesToStr([
  4449. 'type',
  4450. ' TObject = class',
  4451. ' procedure DoIt(b: boolean); virtual; abstract;',
  4452. ' procedure DoIt(i: longint); virtual; abstract;',
  4453. ' end;',
  4454. '']),
  4455. 'uses test1;');
  4456. StartUnit(true);
  4457. Add([
  4458. 'interface',
  4459. 'uses unit2;',
  4460. 'type',
  4461. ' TEagle = class(TObject)',
  4462. ' procedure DoIt(b: boolean); override;',
  4463. ' procedure DoIt(i: longint); override;',
  4464. ' end;',
  4465. 'implementation',
  4466. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4467. 'procedure TEagle.DoIt(i: longint); begin end;',
  4468. '']);
  4469. ConvertUnit;
  4470. CheckSource('TestProc_OverloadUnitCycle',
  4471. LinesToStr([ // statements
  4472. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4473. ' this.DoIt = function (b) {',
  4474. ' };',
  4475. ' this.DoIt$1 = function (i) {',
  4476. ' };',
  4477. '});',
  4478. '']),
  4479. '',
  4480. LinesToStr([
  4481. '']));
  4482. end;
  4483. procedure TTestModule.TestProc_Varargs;
  4484. begin
  4485. StartProgram(false);
  4486. Add([
  4487. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4488. 'procedure ProcB; varargs; external name ''ProcB'';',
  4489. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4490. 'function GetIt: longint; begin end;',
  4491. 'begin',
  4492. ' ProcA(1);',
  4493. ' ProcA(1,2);',
  4494. ' ProcA(1,2.0);',
  4495. ' ProcA(1,2,3);',
  4496. ' ProcA(1,''2'');',
  4497. ' ProcA(2,'''');',
  4498. ' ProcA(3,false);',
  4499. ' ProcB;',
  4500. ' ProcB();',
  4501. ' ProcB(4);',
  4502. ' ProcB(''foo'');',
  4503. ' ProcC;',
  4504. ' ProcC();',
  4505. ' ProcC(4);',
  4506. ' ProcC(5,''foo'');',
  4507. ' ProcB(GetIt);',
  4508. ' ProcB(GetIt());',
  4509. ' ProcB(GetIt,GetIt());']);
  4510. ConvertProgram;
  4511. CheckSource('TestProc_Varargs',
  4512. LinesToStr([ // statements
  4513. 'this.GetIt = function () {',
  4514. ' var Result = 0;',
  4515. ' return Result;',
  4516. '};',
  4517. '']),
  4518. LinesToStr([
  4519. 'ProcA(1);',
  4520. 'ProcA(1, 2);',
  4521. 'ProcA(1, 2.0);',
  4522. 'ProcA(1, 2, 3);',
  4523. 'ProcA(1, "2");',
  4524. 'ProcA(2, "");',
  4525. 'ProcA(3, false);',
  4526. 'ProcB();',
  4527. 'ProcB();',
  4528. 'ProcB(4);',
  4529. 'ProcB("foo");',
  4530. 'ProcC(17);',
  4531. 'ProcC(17);',
  4532. 'ProcC(4);',
  4533. 'ProcC(5, "foo");',
  4534. 'ProcB($mod.GetIt());',
  4535. 'ProcB($mod.GetIt());',
  4536. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4537. '']));
  4538. end;
  4539. procedure TTestModule.TestProc_ConstOrder;
  4540. begin
  4541. StartProgram(false);
  4542. Add([
  4543. 'const A = 3;',
  4544. 'const B = A+1;',
  4545. 'procedure DoIt;',
  4546. 'const C = A+1;',
  4547. 'const D = B+1;',
  4548. 'const E = D+C+B+A;',
  4549. 'begin',
  4550. 'end;',
  4551. 'begin'
  4552. ]);
  4553. ConvertProgram;
  4554. CheckSource('TestProc_ConstOrder',
  4555. LinesToStr([ // statements
  4556. 'this.A = 3;',
  4557. 'this.B = 3 + 1;',
  4558. 'var C = 3 + 1;',
  4559. 'var D = 4 + 1;',
  4560. 'var E = 5 + 4 + 4 + 3;',
  4561. 'this.DoIt = function () {',
  4562. '};',
  4563. '']),
  4564. LinesToStr([
  4565. ''
  4566. ]));
  4567. end;
  4568. procedure TTestModule.TestProc_DuplicateConst;
  4569. begin
  4570. StartProgram(false);
  4571. Add([
  4572. 'const A = 1;',
  4573. 'procedure DoIt;',
  4574. 'const A = 2;',
  4575. ' procedure SubIt;',
  4576. ' const A = 21;',
  4577. ' begin',
  4578. ' end;',
  4579. 'begin',
  4580. 'end;',
  4581. 'procedure DoSome;',
  4582. 'const A = 3;',
  4583. 'begin',
  4584. 'end;',
  4585. 'begin'
  4586. ]);
  4587. ConvertProgram;
  4588. CheckSource('TestProc_DuplicateConst',
  4589. LinesToStr([ // statements
  4590. 'this.A = 1;',
  4591. 'var A$1 = 2;',
  4592. 'var A$2 = 21;',
  4593. 'this.DoIt = function () {',
  4594. ' function SubIt() {',
  4595. ' };',
  4596. '};',
  4597. 'var A$3 = 3;',
  4598. 'this.DoSome = function () {',
  4599. '};',
  4600. '']),
  4601. LinesToStr([
  4602. ''
  4603. ]));
  4604. end;
  4605. procedure TTestModule.TestProc_LocalVarAbsolute;
  4606. begin
  4607. StartProgram(false);
  4608. Add([
  4609. 'type',
  4610. ' TObject = class',
  4611. ' Index: longint;',
  4612. ' procedure DoAbs(Item: pointer);',
  4613. ' end;',
  4614. 'procedure TObject.DoAbs(Item: pointer);',
  4615. 'var',
  4616. ' o: TObject absolute Item;',
  4617. 'begin',
  4618. ' if o.Index<o.Index then o.Index:=o.Index;',
  4619. 'end;',
  4620. 'procedure DoIt(i: longint; p: pointer);',
  4621. 'var',
  4622. ' d: double absolute i;',
  4623. ' s: string absolute d;',
  4624. ' oi: TObject absolute i;',
  4625. ' op: TObject absolute p;',
  4626. 'begin',
  4627. ' if d=d then d:=d;',
  4628. ' if s=s then s:=s;',
  4629. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4630. ' if op.Index=op.Index then op.Index:=op.Index;',
  4631. 'end;',
  4632. 'begin']);
  4633. ConvertProgram;
  4634. CheckSource('TestProc_LocalVarAbsolute',
  4635. LinesToStr([ // statements
  4636. 'rtl.createClass(this, "TObject", null, function () {',
  4637. ' this.$init = function () {',
  4638. ' this.Index = 0;',
  4639. ' };',
  4640. ' this.$final = function () {',
  4641. ' };',
  4642. ' this.DoAbs = function (Item) {',
  4643. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4644. ' };',
  4645. '});',
  4646. 'this.DoIt = function (i, p) {',
  4647. ' if (i === i) i = i;',
  4648. ' if (i === i) i = i;',
  4649. ' if (i.Index < i.Index) i.Index = i.Index;',
  4650. ' if (p.Index === p.Index) p.Index = p.Index;',
  4651. '};'
  4652. ]),
  4653. LinesToStr([
  4654. ]));
  4655. end;
  4656. procedure TTestModule.TestProc_LocalVarInit;
  4657. begin
  4658. StartProgram(false);
  4659. Add([
  4660. 'type TBytes = array of byte;',
  4661. 'procedure DoIt;',
  4662. 'const c = 4;',
  4663. 'var',
  4664. ' b: byte = 1;',
  4665. ' w: word = 2+c;',
  4666. ' p: pointer = nil;',
  4667. ' Buffer: TBytes = nil;',
  4668. 'begin',
  4669. 'end;',
  4670. 'begin']);
  4671. ConvertProgram;
  4672. CheckSource('TestProc_LocalVarInit',
  4673. LinesToStr([ // statements
  4674. 'var c = 4;',
  4675. 'this.DoIt = function () {',
  4676. ' var b = 1;',
  4677. ' var w = 2 + 4;',
  4678. ' var p = null;',
  4679. ' var Buffer = [];',
  4680. '};',
  4681. '']),
  4682. LinesToStr([
  4683. ]));
  4684. end;
  4685. procedure TTestModule.TestProc_ReservedWords;
  4686. begin
  4687. StartProgram(false);
  4688. Add([
  4689. 'procedure Date(ArrayBuffer: longint);',
  4690. 'const',
  4691. ' NaN: longint = 3;',
  4692. 'var',
  4693. ' &Boolean: longint;',
  4694. ' procedure Error(ArrayBuffer: longint);',
  4695. ' begin',
  4696. ' end;',
  4697. 'begin',
  4698. ' Nan:=&bOolean;',
  4699. 'end;',
  4700. 'begin',
  4701. ' Date(1);']);
  4702. ConvertProgram;
  4703. CheckSource('TestProc_ReservedWords',
  4704. LinesToStr([ // statements
  4705. 'var naN = 3;',
  4706. 'this.Date = function (arrayBuffer) {',
  4707. ' var boolean = 0;',
  4708. ' function error(arrayBuffer) {',
  4709. ' };',
  4710. ' naN = boolean;',
  4711. '};',
  4712. '']),
  4713. LinesToStr([
  4714. ' $mod.Date(1);'
  4715. ]));
  4716. end;
  4717. procedure TTestModule.TestProc_ConstRefWord;
  4718. begin
  4719. StartProgram(false);
  4720. Add([
  4721. 'procedure Run(constref w: word);',
  4722. 'var l: word;',
  4723. 'begin',
  4724. ' l:=w;',
  4725. ' Run(w);',
  4726. ' Run(l);',
  4727. 'end;',
  4728. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4729. 'begin',
  4730. ' Run(a);',
  4731. ' Run(b);',
  4732. ' Run(c);',
  4733. ' Run(d);',
  4734. ' Run(e);',
  4735. 'end;',
  4736. 'begin',
  4737. ' Run(1);']);
  4738. ConvertProgram;
  4739. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4740. CheckSource('TestProc_ConstRefWord',
  4741. LinesToStr([ // statements
  4742. 'this.Run = function (w) {',
  4743. ' var l = 0;',
  4744. ' l = w;',
  4745. ' $mod.Run(w);',
  4746. ' $mod.Run(l);',
  4747. '};',
  4748. 'this.Fly = function (a, b, c, d, e) {',
  4749. ' $mod.Run(a);',
  4750. ' $mod.Run(b.get());',
  4751. ' $mod.Run(c.get());',
  4752. ' $mod.Run(d);',
  4753. ' $mod.Run(e);',
  4754. '};',
  4755. '']),
  4756. LinesToStr([
  4757. '$mod.Run(1);'
  4758. ]));
  4759. end;
  4760. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4761. begin
  4762. StartProgram(false);
  4763. Add([
  4764. '{$mode objfpc}',
  4765. 'type',
  4766. ' TFunc = reference to function(x: word): word;',
  4767. 'var Func: TFunc;',
  4768. 'procedure DoIt(a: word);',
  4769. 'begin',
  4770. ' Func:=function(b:word): word',
  4771. ' begin',
  4772. ' Result:=a+b;',
  4773. ' exit(b);',
  4774. ' exit(Result);',
  4775. ' end;',// test semicolon
  4776. ' a:=3;',
  4777. 'end;',
  4778. 'begin',
  4779. ' Func:=function(c:word):word begin',
  4780. ' Result:=3+c;',
  4781. ' exit(c);',
  4782. ' exit(Result);',
  4783. ' end;']);
  4784. ConvertProgram;
  4785. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4786. LinesToStr([ // statements
  4787. 'this.Func = null;',
  4788. 'this.DoIt = function (a) {',
  4789. ' $mod.Func = function (b) {',
  4790. ' var Result = 0;',
  4791. ' Result = a + b;',
  4792. ' return b;',
  4793. ' return Result;',
  4794. ' return Result;',
  4795. ' };',
  4796. ' a = 3;',
  4797. '};',
  4798. '']),
  4799. LinesToStr([
  4800. '$mod.Func = function (c) {',
  4801. ' var Result = 0;',
  4802. ' Result = 3 + c;',
  4803. ' return c;',
  4804. ' return Result;',
  4805. ' return Result;',
  4806. '};',
  4807. '']));
  4808. end;
  4809. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4810. begin
  4811. StartProgram(false);
  4812. Add([
  4813. '{$mode delphi}',
  4814. 'type',
  4815. ' TProc = reference to procedure(x: word);',
  4816. 'procedure DoIt(a: word);',
  4817. 'var Proc: TProc;',
  4818. 'begin',
  4819. ' Proc:=procedure(b:word) begin end;',
  4820. 'end;',
  4821. 'var Proc: TProc;',
  4822. 'begin',
  4823. ' Proc:=procedure(c:word) begin end;',
  4824. '']);
  4825. ConvertProgram;
  4826. CheckSource('TestAnonymousProc_Assign_Delphi',
  4827. LinesToStr([ // statements
  4828. 'this.DoIt = function (a) {',
  4829. ' var Proc = null;',
  4830. ' Proc = function (b) {',
  4831. ' };',
  4832. '};',
  4833. 'this.Proc = null;',
  4834. '']),
  4835. LinesToStr([
  4836. '$mod.Proc = function (c) {',
  4837. '};',
  4838. '']));
  4839. end;
  4840. procedure TTestModule.TestAnonymousProc_Arg;
  4841. begin
  4842. StartProgram(false);
  4843. Add([
  4844. 'type',
  4845. ' TProc = reference to procedure;',
  4846. ' TFunc = reference to function(x: word): word;',
  4847. 'procedure DoMore(f,g: TProc);',
  4848. 'begin',
  4849. 'end;',
  4850. 'procedure DoOdd(v: jsvalue);',
  4851. 'begin',
  4852. 'end;',
  4853. 'procedure DoIt(f: TFunc);',
  4854. 'begin',
  4855. ' DoIt(function(b:word): word',
  4856. ' begin',
  4857. ' Result:=1+b;',
  4858. ' end);',
  4859. ' DoMore(procedure begin end, procedure begin end);',
  4860. ' DoOdd(procedure begin end);',
  4861. 'end;',
  4862. 'begin',
  4863. ' DoMore(procedure begin end,',
  4864. ' procedure assembler asm',
  4865. ' console.log("c");',
  4866. ' end);',
  4867. '']);
  4868. ConvertProgram;
  4869. CheckSource('TestAnonymousProc_Arg',
  4870. LinesToStr([ // statements
  4871. 'this.DoMore = function (f, g) {',
  4872. '};',
  4873. 'this.DoOdd = function (v) {',
  4874. '};',
  4875. 'this.DoIt = function (f) {',
  4876. ' $mod.DoIt(function (b) {',
  4877. ' var Result = 0;',
  4878. ' Result = 1 + b;',
  4879. ' return Result;',
  4880. ' });',
  4881. ' $mod.DoMore(function () {',
  4882. ' }, function () {',
  4883. ' });',
  4884. ' $mod.DoOdd(function () {',
  4885. ' });',
  4886. '};',
  4887. '']),
  4888. LinesToStr([
  4889. '$mod.DoMore(function () {',
  4890. '}, function () {',
  4891. ' console.log("c");',
  4892. '});',
  4893. '']));
  4894. end;
  4895. procedure TTestModule.TestAnonymousProc_Typecast;
  4896. begin
  4897. StartProgram(false);
  4898. Add([
  4899. 'type',
  4900. ' TProc = reference to procedure(w: word);',
  4901. ' TArr = array of word;',
  4902. ' TFuncArr = reference to function: TArr;',
  4903. 'procedure DoIt(p: TProc);',
  4904. 'var',
  4905. ' w: word;',
  4906. ' a: TArr;',
  4907. 'begin',
  4908. ' p:=TProc(procedure(b: smallint) begin end);',
  4909. ' a:=TFuncArr(function: TArr begin end)();',
  4910. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4911. 'end;',
  4912. 'begin']);
  4913. ConvertProgram;
  4914. CheckSource('TestAnonymousProc_Typecast',
  4915. LinesToStr([ // statements
  4916. 'this.DoIt = function (p) {',
  4917. ' var w = 0;',
  4918. ' var a = [];',
  4919. ' p = function (b) {',
  4920. ' };',
  4921. ' a = function () {',
  4922. ' var Result = [];',
  4923. ' return Result;',
  4924. ' }();',
  4925. ' w = function () {',
  4926. ' var Result = [];',
  4927. ' return Result;',
  4928. ' }()[3];',
  4929. '};',
  4930. '']),
  4931. LinesToStr([
  4932. '']));
  4933. end;
  4934. procedure TTestModule.TestAnonymousProc_With;
  4935. begin
  4936. StartProgram(false);
  4937. Add([
  4938. 'type',
  4939. ' TProc = reference to procedure(w: word);',
  4940. ' TObject = class',
  4941. ' b: boolean;',
  4942. ' end;',
  4943. 'var',
  4944. ' p: TProc;',
  4945. ' bird: TObject;',
  4946. 'begin',
  4947. ' with bird do',
  4948. ' p:=procedure(w: word)',
  4949. ' begin',
  4950. ' b:=w>2;',
  4951. ' end;',
  4952. '']);
  4953. ConvertProgram;
  4954. CheckSource('TestAnonymousProc_With',
  4955. LinesToStr([ // statements
  4956. 'rtl.createClass(this, "TObject", null, function () {',
  4957. ' this.$init = function () {',
  4958. ' this.b = false;',
  4959. ' };',
  4960. ' this.$final = function () {',
  4961. ' };',
  4962. '});',
  4963. 'this.p = null;',
  4964. 'this.bird = null;',
  4965. '']),
  4966. LinesToStr([
  4967. 'var $with = $mod.bird;',
  4968. '$mod.p = function (w) {',
  4969. ' $with.b = w > 2;',
  4970. '};',
  4971. '']));
  4972. end;
  4973. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4974. begin
  4975. StartProgram(false);
  4976. Add([
  4977. 'type',
  4978. ' TProc = reference to procedure;',
  4979. ' TObject = class',
  4980. ' b: boolean;',
  4981. ' end;',
  4982. 'procedure DoIt;',
  4983. 'var',
  4984. ' p: TProc;',
  4985. 'begin',
  4986. ' try',
  4987. ' except',
  4988. ' on E: TObject do',
  4989. ' p:=procedure',
  4990. ' begin',
  4991. ' E.b:=true;',
  4992. ' end;',
  4993. ' end;',
  4994. 'end;',
  4995. 'begin']);
  4996. ConvertProgram;
  4997. CheckSource('TestAnonymousProc_ExceptOn',
  4998. LinesToStr([ // statements
  4999. 'rtl.createClass(this, "TObject", null, function () {',
  5000. ' this.$init = function () {',
  5001. ' this.b = false;',
  5002. ' };',
  5003. ' this.$final = function () {',
  5004. ' };',
  5005. '});',
  5006. 'this.DoIt = function () {',
  5007. ' var p = null;',
  5008. ' try {} catch ($e) {',
  5009. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5010. ' var E = $e;',
  5011. ' p = function () {',
  5012. ' E.b = true;',
  5013. ' };',
  5014. ' } else throw $e',
  5015. ' };',
  5016. '};',
  5017. '']),
  5018. LinesToStr([
  5019. '']));
  5020. end;
  5021. procedure TTestModule.TestAnonymousProc_Nested;
  5022. begin
  5023. StartProgram(false);
  5024. Add([
  5025. 'type',
  5026. ' TProc = reference to procedure;',
  5027. ' TObject = class',
  5028. ' i: byte;',
  5029. ' procedure DoIt;',
  5030. ' end;',
  5031. 'procedure TObject.DoIt;',
  5032. 'var',
  5033. ' p: TProc;',
  5034. ' procedure Sub;',
  5035. ' begin',
  5036. ' p:=procedure',
  5037. ' begin',
  5038. ' i:=3;',
  5039. ' Self.i:=4;',
  5040. ' p:=procedure',
  5041. ' procedure SubSub;',
  5042. ' begin',
  5043. ' i:=13;',
  5044. ' Self.i:=14;',
  5045. ' end;',
  5046. ' begin',
  5047. ' i:=13;',
  5048. ' Self.i:=14;',
  5049. ' end;',
  5050. ' end;',
  5051. ' end;',
  5052. 'begin',
  5053. 'end;',
  5054. 'begin']);
  5055. ConvertProgram;
  5056. CheckSource('TestAnonymousProc_Nested',
  5057. LinesToStr([ // statements
  5058. 'rtl.createClass(this, "TObject", null, function () {',
  5059. ' this.$init = function () {',
  5060. ' this.i = 0;',
  5061. ' };',
  5062. ' this.$final = function () {',
  5063. ' };',
  5064. ' this.DoIt = function () {',
  5065. ' var $Self = this;',
  5066. ' var p = null;',
  5067. ' function Sub() {',
  5068. ' p = function () {',
  5069. ' $Self.i = 3;',
  5070. ' $Self.i = 4;',
  5071. ' p = function () {',
  5072. ' function SubSub() {',
  5073. ' $Self.i = 13;',
  5074. ' $Self.i = 14;',
  5075. ' };',
  5076. ' $Self.i = 13;',
  5077. ' $Self.i = 14;',
  5078. ' };',
  5079. ' };',
  5080. ' };',
  5081. ' };',
  5082. '});',
  5083. '']),
  5084. LinesToStr([
  5085. '']));
  5086. end;
  5087. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5088. begin
  5089. StartProgram(false);
  5090. Add([
  5091. 'type',
  5092. ' TProc = reference to procedure;',
  5093. 'function DoIt: TProc;',
  5094. ' function Sub: TProc;',
  5095. ' begin',
  5096. ' Result:=procedure',
  5097. ' begin',
  5098. ' Sub:=procedure',
  5099. ' procedure SubSub;',
  5100. ' begin',
  5101. ' Result:=nil;',
  5102. ' Sub:=nil;',
  5103. ' DoIt:=nil;',
  5104. ' end;',
  5105. ' begin',
  5106. ' Result:=nil;',
  5107. ' Sub:=nil;',
  5108. ' DoIt:=nil;',
  5109. ' end;',
  5110. ' end;',
  5111. ' end;',
  5112. 'begin',
  5113. 'end;',
  5114. 'begin']);
  5115. ConvertProgram;
  5116. CheckSource('TestAnonymousProc_NestedAssignResult',
  5117. LinesToStr([ // statements
  5118. 'this.DoIt = function () {',
  5119. ' var Result = null;',
  5120. ' function Sub() {',
  5121. ' var Result$1 = null;',
  5122. ' Result$1 = function () {',
  5123. ' Result$1 = function () {',
  5124. ' function SubSub() {',
  5125. ' Result$1 = null;',
  5126. ' Result$1 = null;',
  5127. ' Result = null;',
  5128. ' };',
  5129. ' Result$1 = null;',
  5130. ' Result$1 = null;',
  5131. ' Result = null;',
  5132. ' };',
  5133. ' };',
  5134. ' return Result$1;',
  5135. ' };',
  5136. ' return Result;',
  5137. '};',
  5138. '']),
  5139. LinesToStr([
  5140. '']));
  5141. end;
  5142. procedure TTestModule.TestAnonymousProc_Class;
  5143. begin
  5144. StartProgram(false);
  5145. Add([
  5146. 'type',
  5147. ' TProc = reference to procedure;',
  5148. ' TEvent = procedure of object;',
  5149. ' TObject = class',
  5150. ' Size: word;',
  5151. ' function GetIt: TProc;',
  5152. ' procedure DoIt; virtual; abstract;',
  5153. ' end;',
  5154. 'function TObject.GetIt: TProc;',
  5155. 'begin',
  5156. ' Result:=procedure',
  5157. ' var p: TEvent;',
  5158. ' begin',
  5159. ' Size:=Size;',
  5160. ' Size:=Self.Size;',
  5161. ' p:=@DoIt;',
  5162. ' p:[email protected];',
  5163. ' end;',
  5164. 'end;',
  5165. 'begin']);
  5166. ConvertProgram;
  5167. CheckSource('TestAnonymousProc_Class',
  5168. LinesToStr([ // statements
  5169. 'rtl.createClass(this, "TObject", null, function () {',
  5170. ' this.$init = function () {',
  5171. ' this.Size = 0;',
  5172. ' };',
  5173. ' this.$final = function () {',
  5174. ' };',
  5175. ' this.GetIt = function () {',
  5176. ' var $Self = this;',
  5177. ' var Result = null;',
  5178. ' Result = function () {',
  5179. ' var p = null;',
  5180. ' $Self.Size = $Self.Size;',
  5181. ' $Self.Size = $Self.Size;',
  5182. ' p = rtl.createCallback($Self, "DoIt");',
  5183. ' p = rtl.createCallback($Self, "DoIt");',
  5184. ' };',
  5185. ' return Result;',
  5186. ' };',
  5187. '});',
  5188. '']),
  5189. LinesToStr([
  5190. '']));
  5191. end;
  5192. procedure TTestModule.TestAnonymousProc_ForLoop;
  5193. begin
  5194. StartProgram(false);
  5195. Add([
  5196. 'type TProc = reference to procedure;',
  5197. 'procedure Foo(p: TProc);',
  5198. 'begin',
  5199. 'end;',
  5200. 'procedure DoIt;',
  5201. 'var i: word;',
  5202. ' a: word;',
  5203. 'begin',
  5204. ' for i:=1 to 10 do begin',
  5205. ' Foo(procedure begin a:=3; end);',
  5206. ' end;',
  5207. 'end;',
  5208. 'begin',
  5209. ' DoIt;']);
  5210. ConvertProgram;
  5211. CheckSource('TestAnonymousProc_ForLoop',
  5212. LinesToStr([ // statements
  5213. 'this.Foo = function (p) {',
  5214. '};',
  5215. 'this.DoIt = function () {',
  5216. ' var i = 0;',
  5217. ' var a = 0;',
  5218. ' for (i = 1; i <= 10; i++) {',
  5219. ' $mod.Foo(function () {',
  5220. ' a = 3;',
  5221. ' });',
  5222. ' };',
  5223. '};',
  5224. '']),
  5225. LinesToStr([
  5226. '$mod.DoIt();'
  5227. ]));
  5228. end;
  5229. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5230. begin
  5231. StartProgram(false);
  5232. Add([
  5233. '{$mode delphi}',
  5234. 'type',
  5235. ' TProc = reference to procedure;',
  5236. ' TFunc = reference to function(x: word): word;',
  5237. 'procedure Run;',
  5238. 'asm',
  5239. 'end;',
  5240. 'procedure Walk(p: TProc; f: TFunc);',
  5241. 'begin',
  5242. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5243. 'end;',
  5244. 'begin',
  5245. ' Walk(procedure',
  5246. ' asm',
  5247. ' console.log("a");',
  5248. ' end,',
  5249. ' function(x: word): word asm',
  5250. ' console.log("c");',
  5251. ' end);',
  5252. '']);
  5253. ConvertProgram;
  5254. CheckSource('TestAnonymousProc_AsmDelphi',
  5255. LinesToStr([ // statements
  5256. 'this.Run = function () {',
  5257. '};',
  5258. 'this.Walk = function (p, f) {',
  5259. ' $mod.Walk(function () {',
  5260. ' }, function (b) {',
  5261. ' return 1+b;',
  5262. ' });',
  5263. '};',
  5264. '']),
  5265. LinesToStr([
  5266. '$mod.Walk(function () {',
  5267. ' console.log("a");',
  5268. '}, function (x) {',
  5269. ' console.log("c");',
  5270. '});',
  5271. '']));
  5272. end;
  5273. procedure TTestModule.TestEnum_Name;
  5274. begin
  5275. StartProgram(false);
  5276. Add('type TMyEnum = (Red, Green, Blue);');
  5277. Add('var e: TMyEnum;');
  5278. Add('var f: TMyEnum = Blue;');
  5279. Add('begin');
  5280. Add(' e:=green;');
  5281. Add(' e:=default(TMyEnum);');
  5282. ConvertProgram;
  5283. CheckSource('TestEnum_Name',
  5284. LinesToStr([ // statements
  5285. 'this.TMyEnum = {',
  5286. ' "0":"Red",',
  5287. ' Red:0,',
  5288. ' "1":"Green",',
  5289. ' Green:1,',
  5290. ' "2":"Blue",',
  5291. ' Blue:2',
  5292. ' };',
  5293. 'this.e = 0;',
  5294. 'this.f = this.TMyEnum.Blue;'
  5295. ]),
  5296. LinesToStr([
  5297. '$mod.e=$mod.TMyEnum.Green;',
  5298. '$mod.e=$mod.TMyEnum.Red;'
  5299. ]));
  5300. end;
  5301. procedure TTestModule.TestEnum_Number;
  5302. begin
  5303. Converter.Options:=Converter.Options+[coEnumNumbers];
  5304. StartProgram(false);
  5305. Add('type TMyEnum = (Red, Green);');
  5306. Add('var');
  5307. Add(' e: TMyEnum;');
  5308. Add(' f: TMyEnum = Green;');
  5309. Add(' i: longint;');
  5310. Add('begin');
  5311. Add(' e:=green;');
  5312. Add(' i:=longint(e);');
  5313. ConvertProgram;
  5314. CheckSource('TestEnumNumber',
  5315. LinesToStr([ // statements
  5316. 'this.TMyEnum = {',
  5317. ' "0":"Red",',
  5318. ' Red:0,',
  5319. ' "1":"Green",',
  5320. ' Green:1',
  5321. ' };',
  5322. 'this.e = 0;',
  5323. 'this.f = 1;',
  5324. 'this.i = 0;'
  5325. ]),
  5326. LinesToStr([
  5327. '$mod.e=1;',
  5328. '$mod.i=$mod.e;'
  5329. ]));
  5330. end;
  5331. procedure TTestModule.TestEnum_ConstFail;
  5332. begin
  5333. StartProgram(false);
  5334. Add([
  5335. 'type TMyEnum = (Red = 100, Green = 101);',
  5336. 'var',
  5337. ' e: TMyEnum;',
  5338. ' f: TMyEnum = Green;',
  5339. 'begin',
  5340. ' e:=green;']);
  5341. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5342. ConvertProgram;
  5343. end;
  5344. procedure TTestModule.TestEnum_Functions;
  5345. begin
  5346. StartProgram(false);
  5347. Add([
  5348. 'type TMyEnum = (Red, Green);',
  5349. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5350. 'var',
  5351. ' v: longint;',
  5352. ' s: string;',
  5353. 'begin',
  5354. ' val(s,e,v);',
  5355. ' val(s,e,i);',
  5356. 'end;',
  5357. 'var',
  5358. ' e: TMyEnum;',
  5359. ' i: longint;',
  5360. ' s: string;',
  5361. ' b: boolean;',
  5362. 'begin',
  5363. ' i:=ord(red);',
  5364. ' i:=ord(green);',
  5365. ' i:=ord(e);',
  5366. ' i:=ord(b);',
  5367. ' e:=low(tmyenum);',
  5368. ' e:=low(e);',
  5369. ' b:=low(boolean);',
  5370. ' e:=high(tmyenum);',
  5371. ' e:=high(e);',
  5372. ' b:=high(boolean);',
  5373. ' e:=pred(green);',
  5374. ' e:=pred(e);',
  5375. ' b:=pred(b);',
  5376. ' e:=succ(red);',
  5377. ' e:=succ(e);',
  5378. ' b:=succ(b);',
  5379. ' e:=tmyenum(1);',
  5380. ' e:=tmyenum(i);',
  5381. ' s:=str(e);',
  5382. ' str(e,s);',
  5383. ' str(red,s);',
  5384. ' s:=str(e:3);',
  5385. ' writestr(s,e:3,red);',
  5386. ' val(s,e,i);',
  5387. ' i:=longint(e);']);
  5388. ConvertProgram;
  5389. CheckSource('TestEnum_Functions',
  5390. LinesToStr([ // statements
  5391. 'this.TMyEnum = {',
  5392. ' "0":"Red",',
  5393. ' Red:0,',
  5394. ' "1":"Green",',
  5395. ' Green:1',
  5396. ' };',
  5397. 'this.DoIt = function (e, i) {',
  5398. ' var v = 0;',
  5399. ' var s = "";',
  5400. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5401. ' v = w;',
  5402. ' }));',
  5403. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5404. '};',
  5405. 'this.e = 0;',
  5406. 'this.i = 0;',
  5407. 'this.s = "";',
  5408. 'this.b = false;',
  5409. '']),
  5410. LinesToStr([
  5411. '$mod.i=$mod.TMyEnum.Red;',
  5412. '$mod.i=$mod.TMyEnum.Green;',
  5413. '$mod.i=$mod.e;',
  5414. '$mod.i=$mod.b+0;',
  5415. '$mod.e=$mod.TMyEnum.Red;',
  5416. '$mod.e=$mod.TMyEnum.Red;',
  5417. '$mod.b=false;',
  5418. '$mod.e=$mod.TMyEnum.Green;',
  5419. '$mod.e=$mod.TMyEnum.Green;',
  5420. '$mod.b=true;',
  5421. '$mod.e=$mod.TMyEnum.Green-1;',
  5422. '$mod.e=$mod.e-1;',
  5423. '$mod.b=false;',
  5424. '$mod.e=$mod.TMyEnum.Red+1;',
  5425. '$mod.e=$mod.e+1;',
  5426. '$mod.b=true;',
  5427. '$mod.e=1;',
  5428. '$mod.e=$mod.i;',
  5429. '$mod.s = $mod.TMyEnum[$mod.e];',
  5430. '$mod.s = $mod.TMyEnum[$mod.e];',
  5431. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5432. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5433. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5434. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5435. ' $mod.i = v;',
  5436. '});',
  5437. '$mod.i=$mod.e;',
  5438. '']));
  5439. end;
  5440. procedure TTestModule.TestEnumRg_Functions;
  5441. begin
  5442. StartProgram(false);
  5443. Add([
  5444. 'type',
  5445. ' TEnum = (Red, Green, Blue);',
  5446. ' TEnumRg = Green..Blue;',
  5447. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5448. 'var',
  5449. ' v: longint;',
  5450. ' s: string;',
  5451. 'begin',
  5452. ' val(s,e,v);',
  5453. ' val(s,e,i);',
  5454. 'end;',
  5455. 'var',
  5456. ' e: TEnumRg;',
  5457. ' i: longint;',
  5458. ' s: string;',
  5459. 'begin',
  5460. ' i:=ord(green);',
  5461. ' i:=ord(e);',
  5462. ' e:=low(tenumrg);',
  5463. ' e:=low(e);',
  5464. ' e:=high(tenumrg);',
  5465. ' e:=high(e);',
  5466. ' e:=pred(blue);',
  5467. ' e:=pred(e);',
  5468. ' e:=succ(green);',
  5469. ' e:=succ(e);',
  5470. ' e:=tenumrg(1);',
  5471. ' e:=tenumrg(i);',
  5472. ' s:=str(e);',
  5473. ' str(e,s);',
  5474. ' str(red,s);',
  5475. ' s:=str(e:3);',
  5476. ' writestr(s,e:3,blue);',
  5477. ' val(s,e,i);',
  5478. ' i:=longint(e);']);
  5479. ConvertProgram;
  5480. CheckSource('TestEnumRg_Functions',
  5481. LinesToStr([ // statements
  5482. 'this.TEnum = {',
  5483. ' "0":"Red",',
  5484. ' Red:0,',
  5485. ' "1":"Green",',
  5486. ' Green:1,',
  5487. ' "2":"Blue",',
  5488. ' Blue:2',
  5489. ' };',
  5490. 'this.DoIt = function (e, i) {',
  5491. ' var v = 0;',
  5492. ' var s = "";',
  5493. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5494. ' v = w;',
  5495. ' }));',
  5496. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5497. '};',
  5498. 'this.e = this.TEnum.Green;',
  5499. 'this.i = 0;',
  5500. 'this.s = "";',
  5501. '']),
  5502. LinesToStr([
  5503. '$mod.i=$mod.TEnum.Green;',
  5504. '$mod.i=$mod.e;',
  5505. '$mod.e=$mod.TEnum.Green;',
  5506. '$mod.e=$mod.TEnum.Green;',
  5507. '$mod.e=$mod.TEnum.Blue;',
  5508. '$mod.e=$mod.TEnum.Blue;',
  5509. '$mod.e=$mod.TEnum.Blue-1;',
  5510. '$mod.e=$mod.e-1;',
  5511. '$mod.e=$mod.TEnum.Green+1;',
  5512. '$mod.e=$mod.e+1;',
  5513. '$mod.e=1;',
  5514. '$mod.e=$mod.i;',
  5515. '$mod.s = $mod.TEnum[$mod.e];',
  5516. '$mod.s = $mod.TEnum[$mod.e];',
  5517. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5518. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5519. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5520. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5521. ' $mod.i = v;',
  5522. '});',
  5523. '$mod.i=$mod.e;',
  5524. '']));
  5525. end;
  5526. procedure TTestModule.TestEnum_AsParams;
  5527. begin
  5528. StartProgram(false);
  5529. Add('type TEnum = (Red,Blue);');
  5530. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5531. Add('var vJ: TEnum;');
  5532. Add('begin');
  5533. Add(' vg:=vg;');
  5534. Add(' vj:=vh;');
  5535. Add(' vi:=vi;');
  5536. Add(' doit(vg,vg,vg);');
  5537. Add(' doit(vh,vh,vj);');
  5538. Add(' doit(vi,vi,vi);');
  5539. Add(' doit(vj,vj,vj);');
  5540. Add('end;');
  5541. Add('var i: TEnum;');
  5542. Add('begin');
  5543. Add(' doit(i,i,i);');
  5544. ConvertProgram;
  5545. CheckSource('TestEnum_AsParams',
  5546. LinesToStr([ // statements
  5547. 'this.TEnum = {',
  5548. ' "0": "Red",',
  5549. ' Red: 0,',
  5550. ' "1": "Blue",',
  5551. ' Blue: 1',
  5552. '};',
  5553. 'this.DoIt = function (vG,vH,vI) {',
  5554. ' var vJ = 0;',
  5555. ' vG = vG;',
  5556. ' vJ = vH;',
  5557. ' vI.set(vI.get());',
  5558. ' $mod.DoIt(vG, vG, {',
  5559. ' get: function () {',
  5560. ' return vG;',
  5561. ' },',
  5562. ' set: function (v) {',
  5563. ' vG = v;',
  5564. ' }',
  5565. ' });',
  5566. ' $mod.DoIt(vH, vH, {',
  5567. ' get: function () {',
  5568. ' return vJ;',
  5569. ' },',
  5570. ' set: function (v) {',
  5571. ' vJ = v;',
  5572. ' }',
  5573. ' });',
  5574. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5575. ' $mod.DoIt(vJ, vJ, {',
  5576. ' get: function () {',
  5577. ' return vJ;',
  5578. ' },',
  5579. ' set: function (v) {',
  5580. ' vJ = v;',
  5581. ' }',
  5582. ' });',
  5583. '};',
  5584. 'this.i = 0;'
  5585. ]),
  5586. LinesToStr([
  5587. '$mod.DoIt($mod.i,$mod.i,{',
  5588. ' p: $mod,',
  5589. ' get: function () {',
  5590. ' return this.p.i;',
  5591. ' },',
  5592. ' set: function (v) {',
  5593. ' this.p.i = v;',
  5594. ' }',
  5595. '});'
  5596. ]));
  5597. end;
  5598. procedure TTestModule.TestEnumRange_Array;
  5599. begin
  5600. StartProgram(false);
  5601. Add([
  5602. 'type',
  5603. ' TEnum = (Red, Green, Blue);',
  5604. ' TEnumRg = green..blue;',
  5605. ' TArr = array[TEnumRg] of byte;',
  5606. ' TArr2 = array[green..blue] of byte;',
  5607. 'var',
  5608. ' a: TArr;',
  5609. ' b: TArr = (3,4);',
  5610. ' c: TArr2 = (5,6);',
  5611. 'begin',
  5612. ' a[green] := b[blue];',
  5613. ' c[green] := c[blue];',
  5614. '']);
  5615. ConvertProgram;
  5616. CheckSource('TestEnumRange_Array',
  5617. LinesToStr([ // statements
  5618. 'this.TEnum = {',
  5619. ' "0": "Red",',
  5620. ' Red: 0,',
  5621. ' "1": "Green",',
  5622. ' Green: 1,',
  5623. ' "2": "Blue",',
  5624. ' Blue: 2',
  5625. '};',
  5626. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5627. 'this.b = [3, 4];',
  5628. 'this.c = [5, 6];',
  5629. '']),
  5630. LinesToStr([
  5631. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5632. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5633. '']));
  5634. end;
  5635. procedure TTestModule.TestEnum_ForIn;
  5636. begin
  5637. StartProgram(false);
  5638. Add([
  5639. 'type',
  5640. ' TEnum = (Red, Green, Blue);',
  5641. ' TEnumRg = green..blue;',
  5642. ' TArr = array[TEnum] of byte;',
  5643. ' TArrRg = array[TEnumRg] of byte;',
  5644. 'var',
  5645. ' e: TEnum;',
  5646. ' a1: TArr = (3,4,5);',
  5647. ' a2: TArrRg = (11,12);',
  5648. ' b: byte;',
  5649. 'begin',
  5650. ' for e in TEnum do ;',
  5651. ' for e in TEnumRg do ;',
  5652. ' for e in TArr do ;',
  5653. ' for e in TArrRg do ;',
  5654. ' for b in a1 do ;',
  5655. ' for b in a2 do ;',
  5656. '']);
  5657. ConvertProgram;
  5658. CheckSource('TestEnum_ForIn',
  5659. LinesToStr([ // statements
  5660. 'this.TEnum = {',
  5661. ' "0": "Red",',
  5662. ' Red: 0,',
  5663. ' "1": "Green",',
  5664. ' Green: 1,',
  5665. ' "2": "Blue",',
  5666. ' Blue: 2',
  5667. '};',
  5668. 'this.e = 0;',
  5669. 'this.a1 = [3, 4, 5];',
  5670. 'this.a2 = [11, 12];',
  5671. 'this.b = 0;',
  5672. '']),
  5673. LinesToStr([
  5674. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5675. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5676. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5677. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5678. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5679. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5680. '']));
  5681. end;
  5682. procedure TTestModule.TestEnum_ScopedNumber;
  5683. begin
  5684. Converter.Options:=Converter.Options+[coEnumNumbers];
  5685. StartProgram(false);
  5686. Add([
  5687. 'type',
  5688. ' TEnum = (Red, Green);',
  5689. 'var',
  5690. ' e: TEnum;',
  5691. 'begin',
  5692. ' e:=TEnum.Green;',
  5693. '']);
  5694. ConvertProgram;
  5695. CheckSource('TestEnum_ScopedNumber',
  5696. LinesToStr([ // statements
  5697. 'this.TEnum = {',
  5698. ' "0": "Red",',
  5699. ' Red: 0,',
  5700. ' "1": "Green",',
  5701. ' Green: 1',
  5702. '};',
  5703. 'this.e = 0;',
  5704. '']),
  5705. LinesToStr([
  5706. '$mod.e = 1;']));
  5707. end;
  5708. procedure TTestModule.TestEnum_InFunction;
  5709. begin
  5710. StartProgram(false);
  5711. Add([
  5712. 'const TEnum = 3;',
  5713. 'procedure DoIt;',
  5714. 'type',
  5715. ' TEnum = (Red, Green, Blue);',
  5716. ' procedure Sub;',
  5717. ' type',
  5718. ' TEnumSub = (Left, Right);',
  5719. ' var',
  5720. ' es: TEnumSub;',
  5721. ' begin',
  5722. ' es:=Left;',
  5723. ' end;',
  5724. 'var',
  5725. ' e, e2: TEnum;',
  5726. 'begin',
  5727. ' if e in [red,blue] then e2:=e;',
  5728. 'end;',
  5729. 'begin']);
  5730. ConvertProgram;
  5731. CheckSource('TestEnum_InFunction',
  5732. LinesToStr([ // statements
  5733. 'this.TEnum = 3;',
  5734. 'var TEnum$1 = {',
  5735. ' "0":"Red",',
  5736. ' Red:0,',
  5737. ' "1":"Green",',
  5738. ' Green:1,',
  5739. ' "2":"Blue",',
  5740. ' Blue:2',
  5741. ' };',
  5742. 'var TEnumSub = {',
  5743. ' "0": "Left",',
  5744. ' Left: 0,',
  5745. ' "1": "Right",',
  5746. ' Right: 1',
  5747. '};',
  5748. 'this.DoIt = function () {',
  5749. ' function Sub() {',
  5750. ' var es = 0;',
  5751. ' es = TEnumSub.Left;',
  5752. ' };',
  5753. ' var e = 0;',
  5754. ' var e2 = 0;',
  5755. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5756. '};',
  5757. '']),
  5758. LinesToStr([
  5759. '']));
  5760. end;
  5761. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5762. begin
  5763. StartUnit(true);
  5764. Add([
  5765. 'interface',
  5766. 'var color: (red, green);',
  5767. 'implementation',
  5768. 'initialization',
  5769. ' color:=green;',
  5770. '']);
  5771. ConvertUnit;
  5772. CheckSource('TestEnum_Name_Anonymous_Unit',
  5773. LinesToStr([
  5774. 'this.color$a = {',
  5775. ' "0": "red",',
  5776. ' red: 0,',
  5777. ' "1": "green",',
  5778. ' green: 1',
  5779. '};',
  5780. 'this.color = 0;',
  5781. '']),
  5782. LinesToStr([ // this.$init
  5783. '$mod.color = $mod.color$a.green;',
  5784. '']),
  5785. LinesToStr([ // implementation
  5786. '']) );
  5787. end;
  5788. procedure TTestModule.TestSet_Enum;
  5789. begin
  5790. StartProgram(false);
  5791. Add([
  5792. 'type',
  5793. ' TColor = (Red, Green, Blue);',
  5794. ' TColors = set of TColor;',
  5795. 'var',
  5796. ' c: TColor;',
  5797. ' s: TColors;',
  5798. ' t: TColors = [];',
  5799. ' u: TColors = [Red];',
  5800. 'begin',
  5801. ' s:=[];',
  5802. ' s:=[Green];',
  5803. ' s:=[Green,Blue];',
  5804. ' s:=[Red..Blue];',
  5805. ' s:=[Red,Green..Blue];',
  5806. ' s:=[Red,c];',
  5807. ' s:=t;',
  5808. ' s:=default(TColors);',
  5809. '']);
  5810. ConvertProgram;
  5811. CheckSource('TestSet',
  5812. LinesToStr([ // statements
  5813. 'this.TColor = {',
  5814. ' "0":"Red",',
  5815. ' Red:0,',
  5816. ' "1":"Green",',
  5817. ' Green:1,',
  5818. ' "2":"Blue",',
  5819. ' Blue:2',
  5820. ' };',
  5821. 'this.c = 0;',
  5822. 'this.s = {};',
  5823. 'this.t = {};',
  5824. 'this.u = rtl.createSet(this.TColor.Red);'
  5825. ]),
  5826. LinesToStr([
  5827. '$mod.s={};',
  5828. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5829. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5830. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5831. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5832. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5833. '$mod.s=rtl.refSet($mod.t);',
  5834. '$mod.s={};',
  5835. '']));
  5836. end;
  5837. procedure TTestModule.TestSet_Operators;
  5838. begin
  5839. StartProgram(false);
  5840. Add('type');
  5841. Add(' TColor = (Red, Green, Blue);');
  5842. Add(' TColors = set of tcolor;');
  5843. Add('var');
  5844. Add(' vC: TColor;');
  5845. Add(' vS: TColors;');
  5846. Add(' vT: TColors;');
  5847. Add(' vU: TColors;');
  5848. Add(' B: boolean;');
  5849. Add('begin');
  5850. Add(' include(vs,green);');
  5851. Add(' exclude(vs,vc);');
  5852. Add(' vs:=vt+vu;');
  5853. Add(' vs:=vt+[red];');
  5854. Add(' vs:=[red]+vt;');
  5855. Add(' vs:=[red]+[green];');
  5856. Add(' vs:=vt-vu;');
  5857. Add(' vs:=vt-[red];');
  5858. Add(' vs:=[red]-vt;');
  5859. Add(' vs:=[red]-[green];');
  5860. Add(' vs:=vt*vu;');
  5861. Add(' vs:=vt*[red];');
  5862. Add(' vs:=[red]*vt;');
  5863. Add(' vs:=[red]*[green];');
  5864. Add(' vs:=vt><vu;');
  5865. Add(' vs:=vt><[red];');
  5866. Add(' vs:=[red]><vt;');
  5867. Add(' vs:=[red]><[green];');
  5868. Add(' b:=vt=vu;');
  5869. Add(' b:=vt=[red];');
  5870. Add(' b:=[red]=vt;');
  5871. Add(' b:=[red]=[green];');
  5872. Add(' b:=vt<>vu;');
  5873. Add(' b:=vt<>[red];');
  5874. Add(' b:=[red]<>vt;');
  5875. Add(' b:=[red]<>[green];');
  5876. Add(' b:=vt<=vu;');
  5877. Add(' b:=vt<=[red];');
  5878. Add(' b:=[red]<=vt;');
  5879. Add(' b:=[red]<=[green];');
  5880. Add(' b:=vt>=vu;');
  5881. Add(' b:=vt>=[red];');
  5882. Add(' b:=[red]>=vt;');
  5883. Add(' b:=[red]>=[green];');
  5884. ConvertProgram;
  5885. CheckSource('TestSet_Operators',
  5886. LinesToStr([ // statements
  5887. 'this.TColor = {',
  5888. ' "0":"Red",',
  5889. ' Red:0,',
  5890. ' "1":"Green",',
  5891. ' Green:1,',
  5892. ' "2":"Blue",',
  5893. ' Blue:2',
  5894. ' };',
  5895. 'this.vC = 0;',
  5896. 'this.vS = {};',
  5897. 'this.vT = {};',
  5898. 'this.vU = {};',
  5899. 'this.B = false;'
  5900. ]),
  5901. LinesToStr([
  5902. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5903. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5904. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5905. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5906. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5907. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5908. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5909. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5910. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5911. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5912. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5913. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5914. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5915. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5916. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5917. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5918. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5919. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5920. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5921. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5922. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5923. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5924. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5925. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5926. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5927. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5928. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5929. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5930. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5931. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5932. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5933. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5934. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5935. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5936. '']));
  5937. end;
  5938. procedure TTestModule.TestSet_Operator_In;
  5939. begin
  5940. StartProgram(false);
  5941. Add([
  5942. 'type',
  5943. ' TColor = (Red, Green, Blue);',
  5944. ' TColors = set of tcolor;',
  5945. ' TColorRg = green..blue;',
  5946. 'var',
  5947. ' vC: tcolor;',
  5948. ' vT: tcolors;',
  5949. ' B: boolean;',
  5950. ' rg: TColorRg;',
  5951. 'begin',
  5952. ' b:=red in vt;',
  5953. ' b:=vc in vt;',
  5954. ' b:=green in [red..blue];',
  5955. ' b:=vc in [red..blue];',
  5956. ' ',
  5957. ' if red in vt then ;',
  5958. ' while vC in vt do ;',
  5959. ' repeat',
  5960. ' until vC in vt;',
  5961. ' if rg in [green..blue] then ;',
  5962. '']);
  5963. ConvertProgram;
  5964. CheckSource('TestSet_Operator_In',
  5965. LinesToStr([ // statements
  5966. 'this.TColor = {',
  5967. ' "0":"Red",',
  5968. ' Red:0,',
  5969. ' "1":"Green",',
  5970. ' Green:1,',
  5971. ' "2":"Blue",',
  5972. ' Blue:2',
  5973. ' };',
  5974. 'this.vC = 0;',
  5975. 'this.vT = {};',
  5976. 'this.B = false;',
  5977. 'this.rg = this.TColor.Green;',
  5978. '']),
  5979. LinesToStr([
  5980. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5981. '$mod.B = $mod.vC in $mod.vT;',
  5982. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5983. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5984. 'if ($mod.TColor.Red in $mod.vT) ;',
  5985. 'while ($mod.vC in $mod.vT) {',
  5986. '};',
  5987. 'do {',
  5988. '} while (!($mod.vC in $mod.vT));',
  5989. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5990. '']));
  5991. end;
  5992. procedure TTestModule.TestSet_Functions;
  5993. begin
  5994. StartProgram(false);
  5995. Add('type');
  5996. Add(' TMyEnum = (Red, Green);');
  5997. Add(' TMyEnums = set of TMyEnum;');
  5998. Add('var');
  5999. Add(' e: TMyEnum;');
  6000. Add(' s: TMyEnums;');
  6001. Add('begin');
  6002. Add(' e:=Low(TMyEnums);');
  6003. Add(' e:=Low(s);');
  6004. Add(' e:=High(TMyEnums);');
  6005. Add(' e:=High(s);');
  6006. ConvertProgram;
  6007. CheckSource('TestSetFunctions',
  6008. LinesToStr([ // statements
  6009. 'this.TMyEnum = {',
  6010. ' "0":"Red",',
  6011. ' Red:0,',
  6012. ' "1":"Green",',
  6013. ' Green:1',
  6014. ' };',
  6015. 'this.e = 0;',
  6016. 'this.s = {};'
  6017. ]),
  6018. LinesToStr([
  6019. '$mod.e=$mod.TMyEnum.Red;',
  6020. '$mod.e=$mod.TMyEnum.Red;',
  6021. '$mod.e=$mod.TMyEnum.Green;',
  6022. '$mod.e=$mod.TMyEnum.Green;',
  6023. '']));
  6024. end;
  6025. procedure TTestModule.TestSet_PassAsArgClone;
  6026. begin
  6027. StartProgram(false);
  6028. Add('type');
  6029. Add(' TMyEnum = (Red, Green);');
  6030. Add(' TMyEnums = set of TMyEnum;');
  6031. Add('procedure DoDefault(s: tmyenums); begin end;');
  6032. Add('procedure DoConst(const s: tmyenums); begin end;');
  6033. Add('var');
  6034. Add(' aSet: tmyenums;');
  6035. Add('begin');
  6036. Add(' dodefault(aset);');
  6037. Add(' doconst(aset);');
  6038. ConvertProgram;
  6039. CheckSource('TestSetFunctions',
  6040. LinesToStr([ // statements
  6041. 'this.TMyEnum = {',
  6042. ' "0":"Red",',
  6043. ' Red:0,',
  6044. ' "1":"Green",',
  6045. ' Green:1',
  6046. ' };',
  6047. 'this.DoDefault = function (s) {',
  6048. '};',
  6049. 'this.DoConst = function (s) {',
  6050. '};',
  6051. 'this.aSet = {};'
  6052. ]),
  6053. LinesToStr([
  6054. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6055. '$mod.DoConst($mod.aSet);',
  6056. '']));
  6057. end;
  6058. procedure TTestModule.TestSet_AsParams;
  6059. begin
  6060. StartProgram(false);
  6061. Add([
  6062. 'type TEnum = (Red,Blue);',
  6063. 'type TEnums = set of TEnum;',
  6064. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6065. 'var vJ: TEnums;',
  6066. 'begin',
  6067. ' Include(vg,red);',
  6068. ' Include(result,blue);',
  6069. ' vg:=vg;',
  6070. ' vj:=vh;',
  6071. ' vi:=vi;',
  6072. ' doit(vg,vg,vg);',
  6073. ' doit(vh,vh,vj);',
  6074. ' doit(vi,vi,vi);',
  6075. ' doit(vj,vj,vj);',
  6076. 'end;',
  6077. 'var i: TEnums;',
  6078. 'begin',
  6079. ' doit(i,i,i);']);
  6080. ConvertProgram;
  6081. CheckSource('TestSet_AsParams',
  6082. LinesToStr([ // statements
  6083. 'this.TEnum = {',
  6084. ' "0": "Red",',
  6085. ' Red: 0,',
  6086. ' "1": "Blue",',
  6087. ' Blue: 1',
  6088. '};',
  6089. 'this.DoIt = function (vG,vH,vI) {',
  6090. ' var Result = {};',
  6091. ' var vJ = {};',
  6092. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6093. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6094. ' vG = rtl.refSet(vG);',
  6095. ' vJ = rtl.refSet(vH);',
  6096. ' vI.set(rtl.refSet(vI.get()));',
  6097. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6098. ' get: function () {',
  6099. ' return vG;',
  6100. ' },',
  6101. ' set: function (v) {',
  6102. ' vG = v;',
  6103. ' }',
  6104. ' });',
  6105. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6106. ' get: function () {',
  6107. ' return vJ;',
  6108. ' },',
  6109. ' set: function (v) {',
  6110. ' vJ = v;',
  6111. ' }',
  6112. ' });',
  6113. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6114. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6115. ' get: function () {',
  6116. ' return vJ;',
  6117. ' },',
  6118. ' set: function (v) {',
  6119. ' vJ = v;',
  6120. ' }',
  6121. ' });',
  6122. ' return Result;',
  6123. '};',
  6124. 'this.i = {};'
  6125. ]),
  6126. LinesToStr([
  6127. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6128. ' p: $mod,',
  6129. ' get: function () {',
  6130. ' return this.p.i;',
  6131. ' },',
  6132. ' set: function (v) {',
  6133. ' this.p.i = v;',
  6134. ' }',
  6135. '});'
  6136. ]));
  6137. end;
  6138. procedure TTestModule.TestSet_Property;
  6139. begin
  6140. StartProgram(false);
  6141. Add('type');
  6142. Add(' TEnum = (Red,Blue);');
  6143. Add(' TEnums = set of TEnum;');
  6144. Add(' TObject = class');
  6145. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6146. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6147. Add(' property Colors: TEnums read GetColors write SetColors;');
  6148. Add(' end;');
  6149. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6150. Add('begin end;');
  6151. Add('var Obj: TObject;');
  6152. Add('begin');
  6153. Add(' Include(Obj.Colors,Red);');
  6154. Add(' Exclude(Obj.Colors,Red);');
  6155. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6156. ConvertProgram;
  6157. CheckSource('TestSet_Property',
  6158. LinesToStr([ // statements
  6159. 'this.TEnum = {',
  6160. ' "0": "Red",',
  6161. ' Red: 0,',
  6162. ' "1": "Blue",',
  6163. ' Blue: 1',
  6164. '};',
  6165. 'rtl.createClass(this, "TObject", null, function () {',
  6166. ' this.$init = function () {',
  6167. ' };',
  6168. ' this.$final = function () {',
  6169. ' };',
  6170. '});',
  6171. 'this.DoIt = function (i, j, k, l) {',
  6172. '};',
  6173. 'this.Obj = null;',
  6174. '']),
  6175. LinesToStr([
  6176. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6177. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6178. '']));
  6179. end;
  6180. procedure TTestModule.TestSet_EnumConst;
  6181. begin
  6182. StartProgram(false);
  6183. Add([
  6184. 'type',
  6185. ' TEnum = (Red,Blue);',
  6186. ' TEnums = set of TEnum;',
  6187. 'const',
  6188. ' Orange = red;',
  6189. 'var',
  6190. ' Enum: tenum;',
  6191. ' Enums: tenums;',
  6192. 'begin',
  6193. ' Include(enums,orange);',
  6194. ' Exclude(enums,orange);',
  6195. ' if orange in enums then;',
  6196. ' if orange in [orange,red] then;']);
  6197. ConvertProgram;
  6198. CheckSource('TestSet_EnumConst',
  6199. LinesToStr([ // statements
  6200. 'this.TEnum = {',
  6201. ' "0": "Red",',
  6202. ' Red: 0,',
  6203. ' "1": "Blue",',
  6204. ' Blue: 1',
  6205. '};',
  6206. 'this.Orange = this.TEnum.Red;',
  6207. 'this.Enum = 0;',
  6208. 'this.Enums = {};',
  6209. '']),
  6210. LinesToStr([
  6211. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6212. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6213. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6214. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6215. '']));
  6216. end;
  6217. procedure TTestModule.TestSet_IntConst;
  6218. begin
  6219. StartProgram(false);
  6220. Add([
  6221. 'type',
  6222. ' TEnums = set of Byte;',
  6223. 'const',
  6224. ' Orange = 0;',
  6225. 'var',
  6226. ' Enum: byte;',
  6227. ' Enums: tenums;',
  6228. 'begin',
  6229. ' Enums:=[];',
  6230. ' Enums:=[0];',
  6231. ' Enums:=[1..2];',
  6232. //' Include(enums,orange);',
  6233. //' Exclude(enums,orange);',
  6234. ' if orange in enums then;',
  6235. ' if orange in [orange,1] then;']);
  6236. ConvertProgram;
  6237. CheckSource('TestSet_IntConst',
  6238. LinesToStr([ // statements
  6239. 'this.Orange = 0;',
  6240. 'this.Enum = 0;',
  6241. 'this.Enums = {};',
  6242. '']),
  6243. LinesToStr([
  6244. '$mod.Enums = {};',
  6245. '$mod.Enums = rtl.createSet(0);',
  6246. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6247. 'if (0 in $mod.Enums) ;',
  6248. 'if (0 in rtl.createSet(0, 1)) ;',
  6249. '']));
  6250. end;
  6251. procedure TTestModule.TestSet_IntRange;
  6252. begin
  6253. StartProgram(false);
  6254. Add([
  6255. 'type',
  6256. ' TRange = 1..3;',
  6257. ' TEnums = set of TRange;',
  6258. 'const',
  6259. ' Orange = 2;',
  6260. 'var',
  6261. ' Enum: byte;',
  6262. ' Enums: TEnums;',
  6263. 'begin',
  6264. ' Enums:=[];',
  6265. ' Enums:=[1];',
  6266. ' Enums:=[2..3];',
  6267. ' Include(enums,orange);',
  6268. ' Exclude(enums,orange);',
  6269. ' if orange in enums then;',
  6270. ' if orange in [orange,1] then;']);
  6271. ConvertProgram;
  6272. CheckSource('TestSet_IntRange',
  6273. LinesToStr([ // statements
  6274. 'this.Orange = 2;',
  6275. 'this.Enum = 0;',
  6276. 'this.Enums = {};',
  6277. '']),
  6278. LinesToStr([
  6279. '$mod.Enums = {};',
  6280. '$mod.Enums = rtl.createSet(1);',
  6281. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6282. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6283. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6284. 'if (2 in $mod.Enums) ;',
  6285. 'if (2 in rtl.createSet(2, 1)) ;',
  6286. '']));
  6287. end;
  6288. procedure TTestModule.TestSet_AnonymousEnumType;
  6289. begin
  6290. StartProgram(false);
  6291. Add('type');
  6292. Add(' TFlags = set of (red, green);');
  6293. Add('const');
  6294. Add(' favorite = red;');
  6295. Add('var');
  6296. Add(' f: TFlags;');
  6297. Add(' i: longint;');
  6298. Add('begin');
  6299. Add(' Include(f,red);');
  6300. Add(' Include(f,favorite);');
  6301. Add(' i:=ord(red);');
  6302. Add(' i:=ord(favorite);');
  6303. Add(' i:=ord(low(TFlags));');
  6304. Add(' i:=ord(low(f));');
  6305. Add(' i:=ord(low(favorite));');
  6306. Add(' i:=ord(high(TFlags));');
  6307. Add(' i:=ord(high(f));');
  6308. Add(' i:=ord(high(favorite));');
  6309. Add(' f:=[green,favorite];');
  6310. ConvertProgram;
  6311. CheckSource('TestSet_AnonymousEnumType',
  6312. LinesToStr([ // statements
  6313. 'this.TFlags$a = {',
  6314. ' "0": "red",',
  6315. ' red: 0,',
  6316. ' "1": "green",',
  6317. ' green: 1',
  6318. '};',
  6319. 'this.favorite = this.TFlags$a.red;',
  6320. 'this.f = {};',
  6321. 'this.i = 0;',
  6322. '']),
  6323. LinesToStr([
  6324. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6325. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6326. '$mod.i = $mod.TFlags$a.red;',
  6327. '$mod.i = $mod.TFlags$a.red;',
  6328. '$mod.i = $mod.TFlags$a.red;',
  6329. '$mod.i = $mod.TFlags$a.red;',
  6330. '$mod.i = $mod.TFlags$a.red;',
  6331. '$mod.i = $mod.TFlags$a.green;',
  6332. '$mod.i = $mod.TFlags$a.green;',
  6333. '$mod.i = $mod.TFlags$a.green;',
  6334. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6335. '']));
  6336. end;
  6337. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6338. begin
  6339. exit;
  6340. StartProgram(false);
  6341. Add([
  6342. 'type',
  6343. ' TAtoZ = ''A''..''Z'';',
  6344. ' TSetOfAZ = set of TAtoZ;',
  6345. 'var',
  6346. ' c: char;',
  6347. ' a: TAtoZ;',
  6348. ' s: TSetOfAZ = [''P'',''A''];',
  6349. ' i: longint;',
  6350. 'begin',
  6351. ' Include(s,''S'');',
  6352. ' Include(s,c);',
  6353. ' Include(s,a);',
  6354. ' c:=low(TAtoZ);',
  6355. ' i:=ord(low(TAtoZ));',
  6356. ' a:=high(TAtoZ);',
  6357. ' a:=high(TSetOfAtoZ);',
  6358. ' s:=[a,c,''M''];',
  6359. '']);
  6360. ConvertProgram;
  6361. CheckSource('TestSet_AnonymousEnumTypeChar',
  6362. LinesToStr([ // statements
  6363. '']),
  6364. LinesToStr([
  6365. '']));
  6366. end;
  6367. procedure TTestModule.TestSet_ConstEnum;
  6368. begin
  6369. StartProgram(false);
  6370. Add([
  6371. 'type',
  6372. ' TEnum = (red,blue,green);',
  6373. ' TEnums = set of TEnum;',
  6374. 'const',
  6375. ' teAny = [low(TEnum)..high(TEnum)];',
  6376. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6377. 'var',
  6378. ' e: TEnum;',
  6379. ' s: TEnums;',
  6380. 'begin',
  6381. ' if blue in teAny then;',
  6382. ' if blue in teAny+[e] then;',
  6383. ' if blue in teAny+teRedBlue then;',
  6384. ' if e in [red,blue] then;',
  6385. ' s:=teAny;',
  6386. ' s:=teAny+[e];',
  6387. ' s:=[e]+teAny;',
  6388. ' s:=teAny+teRedBlue;',
  6389. ' s:=teAny+teRedBlue+[e];',
  6390. '']);
  6391. ConvertProgram;
  6392. CheckSource('TestSet_ConstEnum',
  6393. LinesToStr([ // statements
  6394. 'this.TEnum = {',
  6395. ' "0": "red",',
  6396. ' red: 0,',
  6397. ' "1": "blue",',
  6398. ' blue: 1,',
  6399. ' "2": "green",',
  6400. ' green: 2',
  6401. '};',
  6402. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6403. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6404. 'this.e = 0;',
  6405. 'this.s = {};',
  6406. '']),
  6407. LinesToStr([
  6408. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6409. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6410. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6411. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6412. '$mod.s = rtl.refSet($mod.teAny);',
  6413. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6414. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6415. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6416. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6417. '']));
  6418. end;
  6419. procedure TTestModule.TestSet_ConstChar;
  6420. begin
  6421. StartProgram(false);
  6422. Add([
  6423. 'const',
  6424. ' LowChars = [''a''..''z''];',
  6425. ' Chars = LowChars+[''A''..''Z''];',
  6426. ' sc = [''А'', ''Я''];',
  6427. 'var',
  6428. ' c: char;',
  6429. ' s: string;',
  6430. 'begin',
  6431. ' if c in lowchars then ;',
  6432. ' if ''a'' in lowchars then ;',
  6433. ' if s[1] in lowchars then ;',
  6434. ' if c in chars then ;',
  6435. ' if c in [''a''..''z'',''_''] then ;',
  6436. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6437. ' if ''Я'' in sc then ;',
  6438. ' if 3=ord('' '') then ;',
  6439. '']);
  6440. ConvertProgram;
  6441. CheckSource('TestSet_ConstChar',
  6442. LinesToStr([ // statements
  6443. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6444. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6445. 'this.sc = rtl.createSet(1040, 1071);',
  6446. 'this.c = "";',
  6447. 'this.s = "";',
  6448. '']),
  6449. LinesToStr([
  6450. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6451. 'if (97 in $mod.LowChars) ;',
  6452. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6453. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6454. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6455. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6456. 'if (1071 in $mod.sc) ;',
  6457. 'if (3 === 32) ;',
  6458. '']));
  6459. end;
  6460. procedure TTestModule.TestSet_ConstInt;
  6461. begin
  6462. StartProgram(false);
  6463. Add([
  6464. 'const',
  6465. ' Months = [1..12];',
  6466. ' Mirror = [-12..-1]+Months;',
  6467. 'var',
  6468. ' i: smallint;',
  6469. 'begin',
  6470. ' if 3 in Months then;',
  6471. ' if i in Months+[i] then;',
  6472. ' if i in Months+Mirror then;',
  6473. ' if i in [4..6,8] then;',
  6474. '']);
  6475. ConvertProgram;
  6476. CheckSource('TestSet_ConstInt',
  6477. LinesToStr([ // statements
  6478. 'this.Months = rtl.createSet(null, 1, 12);',
  6479. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6480. 'this.i = 0;',
  6481. '']),
  6482. LinesToStr([
  6483. 'if (3 in $mod.Months) ;',
  6484. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6485. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6486. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6487. '']));
  6488. end;
  6489. procedure TTestModule.TestSet_InFunction;
  6490. begin
  6491. StartProgram(false);
  6492. Add([
  6493. 'const',
  6494. ' TEnum = 3;',
  6495. ' TSetOfEnum = 4;',
  6496. ' TSetOfAno = 5;',
  6497. 'procedure DoIt;',
  6498. 'type',
  6499. ' TEnum = (red, blue);',
  6500. ' TSetOfEnum = set of TEnum;',
  6501. ' TSetOfAno = set of (up,down);',
  6502. 'var',
  6503. ' e: TEnum;',
  6504. ' se: TSetOfEnum;',
  6505. ' sa: TSetOfAno;',
  6506. 'begin',
  6507. ' se:=[e];',
  6508. ' sa:=[up];',
  6509. 'end;',
  6510. 'begin',
  6511. '']);
  6512. ConvertProgram;
  6513. CheckSource('TestSet_InFunction',
  6514. LinesToStr([ // statements
  6515. 'this.TEnum = 3;',
  6516. 'this.TSetOfEnum = 4;',
  6517. 'this.TSetOfAno = 5;',
  6518. 'var TEnum$1 = {',
  6519. ' "0": "red",',
  6520. ' red: 0,',
  6521. ' "1": "blue",',
  6522. ' blue: 1',
  6523. '};',
  6524. 'var TSetOfAno$a = {',
  6525. ' "0": "up",',
  6526. ' up: 0,',
  6527. ' "1": "down",',
  6528. ' down: 1',
  6529. '};',
  6530. 'this.DoIt = function () {',
  6531. ' var e = 0;',
  6532. ' var se = {};',
  6533. ' var sa = {};',
  6534. ' se = rtl.createSet(e);',
  6535. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6536. '};',
  6537. '']),
  6538. LinesToStr([
  6539. '']));
  6540. end;
  6541. procedure TTestModule.TestSet_ForIn;
  6542. begin
  6543. StartProgram(false);
  6544. Add([
  6545. 'type',
  6546. ' TEnum = (Red, Green, Blue);',
  6547. ' TEnumRg = green..blue;',
  6548. ' TSetOfEnum = set of TEnum;',
  6549. ' TSetOfEnumRg = set of TEnumRg;',
  6550. 'var',
  6551. ' e, e2: TEnum;',
  6552. ' er: TEnum;',
  6553. ' s: TSetOfEnum;',
  6554. 'begin',
  6555. ' for e in TSetOfEnum do ;',
  6556. ' for e in TSetOfEnumRg do ;',
  6557. ' for e in [] do e2:=e;',
  6558. ' for e in [red..green] do e2:=e;',
  6559. ' for e in [green,blue] do e2:=e;',
  6560. ' for e in [red,blue] do e2:=e;',
  6561. ' for e in s do e2:=e;',
  6562. ' for er in TSetOfEnumRg do ;',
  6563. '']);
  6564. ConvertProgram;
  6565. CheckSource('TestSet_ForIn',
  6566. LinesToStr([ // statements
  6567. 'this.TEnum = {',
  6568. ' "0":"Red",',
  6569. ' Red:0,',
  6570. ' "1":"Green",',
  6571. ' Green:1,',
  6572. ' "2":"Blue",',
  6573. ' Blue:2',
  6574. ' };',
  6575. 'this.e = 0;',
  6576. 'this.e2 = 0;',
  6577. 'this.er = 0;',
  6578. 'this.s = {};',
  6579. '']),
  6580. LinesToStr([
  6581. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6582. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6583. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6584. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6585. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6586. 'for (var $l in $mod.s){',
  6587. ' $mod.e = +$l;',
  6588. ' $mod.e2 = $mod.e;',
  6589. '};',
  6590. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6591. '']));
  6592. end;
  6593. procedure TTestModule.TestNestBegin;
  6594. begin
  6595. StartProgram(false);
  6596. Add('begin');
  6597. Add(' begin');
  6598. Add(' begin');
  6599. Add(' end;');
  6600. Add(' begin');
  6601. Add(' if true then ;');
  6602. Add(' end;');
  6603. Add(' end;');
  6604. ConvertProgram;
  6605. CheckSource('TestNestBegin',
  6606. '',
  6607. 'if (true) ;');
  6608. end;
  6609. procedure TTestModule.TestUnitImplVars;
  6610. begin
  6611. StartUnit(false);
  6612. Add('interface');
  6613. Add('implementation');
  6614. Add('var');
  6615. Add(' V1:longint;');
  6616. Add(' V2:longint = 3;');
  6617. Add(' V3:string = ''abc'';');
  6618. ConvertUnit;
  6619. CheckSource('TestUnitImplVars',
  6620. LinesToStr([ // statements
  6621. 'var $impl = $mod.$impl;',
  6622. '']),
  6623. '', // this.$init
  6624. LinesToStr([ // implementation
  6625. '$impl.V1 = 0;',
  6626. '$impl.V2 = 3;',
  6627. '$impl.V3 = "abc";',
  6628. '']) );
  6629. end;
  6630. procedure TTestModule.TestUnitImplConsts;
  6631. begin
  6632. StartUnit(false);
  6633. Add('interface');
  6634. Add('implementation');
  6635. Add('const');
  6636. Add(' v1 = 3;');
  6637. Add(' v2:longint = 4;');
  6638. Add(' v3:string = ''abc'';');
  6639. ConvertUnit;
  6640. CheckSource('TestUnitImplConsts',
  6641. LinesToStr([ // statements
  6642. 'var $impl = $mod.$impl;',
  6643. '']),
  6644. '', // this.$init
  6645. LinesToStr([ // implementation
  6646. '$impl.v1 = 3;',
  6647. '$impl.v2 = 4;',
  6648. '$impl.v3 = "abc";',
  6649. '']) );
  6650. end;
  6651. procedure TTestModule.TestUnitImplRecord;
  6652. begin
  6653. StartUnit(false);
  6654. Add('interface');
  6655. Add('implementation');
  6656. Add('type');
  6657. Add(' TMyRecord = record');
  6658. Add(' i: longint;');
  6659. Add(' end;');
  6660. Add('var aRec: TMyRecord;');
  6661. Add('initialization');
  6662. Add(' arec.i:=3;');
  6663. ConvertUnit;
  6664. CheckSource('TestUnitImplRecord',
  6665. LinesToStr([ // statements
  6666. 'var $impl = $mod.$impl;',
  6667. '']),
  6668. // this.$init
  6669. '$impl.aRec.i = 3;',
  6670. LinesToStr([ // implementation
  6671. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6672. ' this.i = 0;',
  6673. ' this.$eq = function (b) {',
  6674. ' return this.i === b.i;',
  6675. ' };',
  6676. ' this.$assign = function (s) {',
  6677. ' this.i = s.i;',
  6678. ' return this;',
  6679. ' };',
  6680. '});',
  6681. '$impl.aRec = $impl.TMyRecord.$new();',
  6682. '']) );
  6683. end;
  6684. procedure TTestModule.TestRenameJSNameConflict;
  6685. begin
  6686. StartProgram(false);
  6687. Add('var apply: longint;');
  6688. Add('var bind: longint;');
  6689. Add('var call: longint;');
  6690. Add('begin');
  6691. ConvertProgram;
  6692. CheckSource('TestRenameJSNameConflict',
  6693. LinesToStr([ // statements
  6694. 'this.Apply = 0;',
  6695. 'this.Bind = 0;',
  6696. 'this.Call = 0;'
  6697. ]),
  6698. LinesToStr([ // this.$main
  6699. ''
  6700. ]));
  6701. end;
  6702. procedure TTestModule.TestLocalConst;
  6703. begin
  6704. StartProgram(false);
  6705. Add('procedure DoIt;');
  6706. Add('const');
  6707. Add(' cA: longint = 1;');
  6708. Add(' cB = 2;');
  6709. Add(' procedure Sub;');
  6710. Add(' const');
  6711. Add(' csA = 3;');
  6712. Add(' cB: double = 4;');
  6713. Add(' begin');
  6714. Add(' cb:=cb+csa;');
  6715. Add(' ca:=ca+csa+5;');
  6716. Add(' end;');
  6717. Add('begin');
  6718. Add(' ca:=ca+cb+6;');
  6719. Add('end;');
  6720. Add('begin');
  6721. ConvertProgram;
  6722. CheckSource('TestLocalConst',
  6723. LinesToStr([
  6724. 'var cA = 1;',
  6725. 'var cB = 2;',
  6726. 'var csA = 3;',
  6727. 'var cB$1 = 4;',
  6728. 'this.DoIt = function () {',
  6729. ' function Sub() {',
  6730. ' cB$1 = cB$1 + 3;',
  6731. ' cA = cA + 3 + 5;',
  6732. ' };',
  6733. ' cA = cA + 2 + 6;',
  6734. '};'
  6735. ]),
  6736. LinesToStr([
  6737. ]));
  6738. end;
  6739. procedure TTestModule.TestVarExternal;
  6740. begin
  6741. StartProgram(false);
  6742. Add('var');
  6743. Add(' NaN: double; external name ''Global.NaN'';');
  6744. Add(' d: double;');
  6745. Add('begin');
  6746. Add(' d:=NaN;');
  6747. ConvertProgram;
  6748. CheckSource('TestVarExternal',
  6749. LinesToStr([
  6750. 'this.d = 0.0;'
  6751. ]),
  6752. LinesToStr([
  6753. '$mod.d = Global.NaN;'
  6754. ]));
  6755. end;
  6756. procedure TTestModule.TestVarExternalOtherUnit;
  6757. begin
  6758. AddModuleWithIntfImplSrc('unit2.pas',
  6759. LinesToStr([
  6760. 'var NaN: double; external name ''Global.NaN'';',
  6761. 'var iV: longint;'
  6762. ]),
  6763. '');
  6764. StartUnit(true);
  6765. Add('interface');
  6766. Add('uses unit2;');
  6767. Add('implementation');
  6768. Add('var');
  6769. Add(' d: double;');
  6770. Add(' i: longint; external name ''$i'';');
  6771. Add('begin');
  6772. Add(' d:=nan;');
  6773. Add(' d:=uNit2.nan;');
  6774. Add(' d:=test1.d;');
  6775. Add(' i:=iv;');
  6776. Add(' i:=uNit2.iv;');
  6777. Add(' i:=test1.i;');
  6778. ConvertUnit;
  6779. CheckSource('TestVarExternalOtherUnit',
  6780. LinesToStr([
  6781. 'var $impl = $mod.$impl;',
  6782. '']),
  6783. LinesToStr([ // this.$init
  6784. '$impl.d = Global.NaN;',
  6785. '$impl.d = Global.NaN;',
  6786. '$impl.d = $impl.d;',
  6787. '$i = pas.unit2.iV;',
  6788. '$i = pas.unit2.iV;',
  6789. '$i = $i;',
  6790. '']),
  6791. LinesToStr([ // implementation
  6792. '$impl.d = 0.0;',
  6793. '']) );
  6794. end;
  6795. procedure TTestModule.TestVarAbsoluteFail;
  6796. begin
  6797. StartProgram(false);
  6798. Add([
  6799. 'var',
  6800. ' a: longint;',
  6801. ' b: longword absolute a;',
  6802. 'begin']);
  6803. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6804. ConvertProgram;
  6805. end;
  6806. procedure TTestModule.TestConstExternal;
  6807. begin
  6808. StartProgram(false);
  6809. Add([
  6810. 'const',
  6811. ' PI: double; external name ''Global.PI'';',
  6812. ' Tau = 2*pi;',
  6813. 'var d: double;',
  6814. 'begin',
  6815. ' d:=pi;',
  6816. ' d:=tau+pi;']);
  6817. ConvertProgram;
  6818. CheckSource('TestConstExternal',
  6819. LinesToStr([
  6820. 'this.Tau = 2*Global.PI;',
  6821. 'this.d = 0.0;'
  6822. ]),
  6823. LinesToStr([
  6824. '$mod.d = Global.PI;',
  6825. '$mod.d = $mod.Tau + Global.PI;'
  6826. ]));
  6827. end;
  6828. procedure TTestModule.TestDouble;
  6829. begin
  6830. StartProgram(false);
  6831. Add([
  6832. 'type',
  6833. ' TDateTime = double;',
  6834. 'const',
  6835. ' a = TDateTime(2.7);',
  6836. ' b = a + TDateTime(1.7);',
  6837. ' c = 0.9 + 0.1;',
  6838. ' f0_1 = 0.1;',
  6839. ' f0_3 = 0.3;',
  6840. ' fn0_1 = -0.1;',
  6841. ' fn0_3 = -0.3;',
  6842. ' fn0_003 = -0.003;',
  6843. ' fn0_123456789 = -0.123456789;',
  6844. ' fn300_0 = -300.0;',
  6845. ' fn123456_0 = -123456.0;',
  6846. ' fn1234567_8 = -1234567.8;',
  6847. ' fn12345678_9 = -12345678.9;',
  6848. ' f1_0En12 = 1E-12;',
  6849. ' fn1_0En12 = -1E-12;',
  6850. ' maxdouble = 1.7e+308;',
  6851. ' mindouble = -1.7e+308;',
  6852. ' MinSafeIntDouble = -$1fffffffffffff;',
  6853. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6854. ' MaxSafeIntDouble = $1fffffffffffff;',
  6855. ' DZeroResolution = 1E-12;',
  6856. ' Minus1 = -1E-12;',
  6857. ' EPS = 1E-9;',
  6858. ' DELTA = 0.001;',
  6859. ' Big = 129.789E+100;',
  6860. ' Test0_15 = 0.15;',
  6861. ' Test999 = 2.9999999999999;',
  6862. ' Test111999 = 211199999999999000.0;',
  6863. ' TestMinus111999 = -211199999999999000.0;',
  6864. ' Inf = 1.0 / 0.0;',
  6865. ' NegInf = -1.0 / 0.0;',
  6866. 'procedure Run(d: double); external name ''Run'';',
  6867. 'var',
  6868. ' d: double = b;',
  6869. 'begin',
  6870. ' d:=1.0;',
  6871. ' d:=1.0/3.0;',
  6872. ' d:=1.0/(3-2-1);',
  6873. ' d:=1/3;',
  6874. ' d:=5.0E-324;',
  6875. ' d:=1.7E308;',
  6876. ' d:=001.00E00;',
  6877. ' d:=002.00E001;',
  6878. ' d:=003.000E000;',
  6879. ' d:=-004.00E-00;',
  6880. ' d:=-005.00E-001;',
  6881. ' d:=10**3;',
  6882. ' d:=10 mod 3;',
  6883. ' d:=10 div 3;',
  6884. ' d:=c;',
  6885. ' d:=f0_1;',
  6886. ' d:=f0_3;',
  6887. ' d:=fn0_1;',
  6888. ' d:=fn0_3;',
  6889. ' d:=fn0_003;',
  6890. ' d:=fn0_123456789;',
  6891. ' d:=fn300_0;',
  6892. ' d:=fn123456_0;',
  6893. ' d:=fn1234567_8;',
  6894. ' d:=fn12345678_9;',
  6895. ' d:=f1_0En12;',
  6896. ' d:=fn1_0En12;',
  6897. ' d:=maxdouble;',
  6898. ' d:=mindouble;',
  6899. ' d:=MinSafeIntDouble;',
  6900. ' d:=double(MinSafeIntDouble);',
  6901. ' d:=MinSafeIntDouble2;',
  6902. ' d:=double(MinSafeIntDouble2);',
  6903. ' d:=MaxSafeIntDouble;',
  6904. ' d:=default(double);',
  6905. ' Run(Inf);',
  6906. ' Run(NegInf);',
  6907. '']);
  6908. ConvertProgram;
  6909. CheckSource('TestDouble',
  6910. LinesToStr([
  6911. 'this.a = 2.7;',
  6912. 'this.b = 2.7 + 1.7;',
  6913. 'this.c = 0.9 + 0.1;',
  6914. 'this.f0_1 = 0.1;',
  6915. 'this.f0_3 = 0.3;',
  6916. 'this.fn0_1 = -0.1;',
  6917. 'this.fn0_3 = -0.3;',
  6918. 'this.fn0_003 = -0.003;',
  6919. 'this.fn0_123456789 = -0.123456789;',
  6920. 'this.fn300_0 = -300.0;',
  6921. 'this.fn123456_0 = -123456.0;',
  6922. 'this.fn1234567_8 = -1234567.8;',
  6923. 'this.fn12345678_9 = -12345678.9;',
  6924. 'this.f1_0En12 = 1E-12;',
  6925. 'this.fn1_0En12 = -1E-12;',
  6926. 'this.maxdouble = 1.7e+308;',
  6927. 'this.mindouble = -1.7e+308;',
  6928. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6929. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6930. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6931. 'this.DZeroResolution = 1E-12;',
  6932. 'this.Minus1 = -1E-12;',
  6933. 'this.EPS = 1E-9;',
  6934. 'this.DELTA = 0.001;',
  6935. 'this.Big = 129.789E+100;',
  6936. 'this.Test0_15 = 0.15;',
  6937. 'this.Test999 = 2.9999999999999;',
  6938. 'this.Test111999 = 211199999999999000.0;',
  6939. 'this.TestMinus111999 = -211199999999999000.0;',
  6940. 'this.Inf = 1.0 / 0.0;',
  6941. 'this.NegInf = -1.0 / 0.0;',
  6942. 'this.d = 4.4;',
  6943. '']),
  6944. LinesToStr([
  6945. '$mod.d = 1.0;',
  6946. '$mod.d = 1.0 / 3.0;',
  6947. '$mod.d = 1.0 / (3 - 2 - 1);',
  6948. '$mod.d = 1 / 3;',
  6949. '$mod.d = 5.0E-324;',
  6950. '$mod.d = 1.7E308;',
  6951. '$mod.d = 1.00E0;',
  6952. '$mod.d = 2.00E1;',
  6953. '$mod.d = 3.000E0;',
  6954. '$mod.d = -4.00E-0;',
  6955. '$mod.d = -5.00E-1;',
  6956. '$mod.d = Math.pow(10, 3);',
  6957. '$mod.d = 10 % 3;',
  6958. '$mod.d = rtl.trunc(10 / 3);',
  6959. '$mod.d = 1;',
  6960. '$mod.d = 0.1;',
  6961. '$mod.d = 0.3;',
  6962. '$mod.d = -0.1;',
  6963. '$mod.d = -0.3;',
  6964. '$mod.d = -0.003;',
  6965. '$mod.d = -0.123456789;',
  6966. '$mod.d = -300;',
  6967. '$mod.d = -123456;',
  6968. '$mod.d = -1234567.8;',
  6969. '$mod.d = -1.23456789E7;',
  6970. '$mod.d = 1E-12;',
  6971. '$mod.d = -1E-12;',
  6972. '$mod.d = 1.7E308;',
  6973. '$mod.d = -1.7E308;',
  6974. '$mod.d = -9007199254740991;',
  6975. '$mod.d = -9007199254740991;',
  6976. '$mod.d = -9.007199254740992E15;',
  6977. '$mod.d = -9.007199254740992E15;',
  6978. '$mod.d = 9007199254740991;',
  6979. '$mod.d = 0.0;',
  6980. 'Run(1 / 0);',
  6981. 'Run(-1 / 0);',
  6982. '']));
  6983. end;
  6984. procedure TTestModule.TestInteger;
  6985. begin
  6986. StartProgram(false);
  6987. Add([
  6988. 'const',
  6989. ' MinInt = low(NativeInt);',
  6990. ' MaxInt = high(NativeInt);',
  6991. 'type',
  6992. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6993. 'const',
  6994. ' a = low(TMyInt)+High(TMyInt);',
  6995. 'var',
  6996. ' i: TMyInt;',
  6997. 'begin',
  6998. ' i:=-MinInt;',
  6999. ' i:=default(TMyInt);',
  7000. ' i:=low(i)+high(i);',
  7001. '']);
  7002. ConvertProgram;
  7003. CheckSource('TestIntegerRange',
  7004. LinesToStr([
  7005. 'this.MinInt = -9007199254740991;',
  7006. 'this.MaxInt = 9007199254740991;',
  7007. 'this.a = -9007199254740991 + 9007199254740991;',
  7008. 'this.i = 0;',
  7009. '']),
  7010. LinesToStr([
  7011. '$mod.i = - -9007199254740991;',
  7012. '$mod.i = -9007199254740991;',
  7013. '$mod.i = -9007199254740991 + 9007199254740991;',
  7014. '']));
  7015. end;
  7016. procedure TTestModule.TestIntegerRange;
  7017. begin
  7018. StartProgram(false);
  7019. Add([
  7020. 'const',
  7021. ' MinInt = -1;',
  7022. ' MaxInt = +1;',
  7023. 'type',
  7024. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7025. ' TInt2 = 1..3;',
  7026. 'const',
  7027. ' a = low(TMyInt)+High(TMyInt);',
  7028. ' b = low(TInt2)+High(TInt2);',
  7029. ' s1 = [1];',
  7030. ' s2 = [1,2];',
  7031. ' s3 = [1..3];',
  7032. ' s4 = [low(shortint)..high(shortint)];',
  7033. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7034. ' s6 = 1 in s2;',
  7035. 'var',
  7036. ' i: TMyInt;',
  7037. ' i2: TInt2;',
  7038. 'begin',
  7039. ' i:=i2;',
  7040. ' i:=default(TMyInt);',
  7041. ' if i=i2 then ;',
  7042. ' i:=ord(i2);',
  7043. '']);
  7044. ConvertProgram;
  7045. CheckSource('TestIntegerRange',
  7046. LinesToStr([
  7047. 'this.MinInt = -1;',
  7048. 'this.MaxInt = +1;',
  7049. 'this.a = -1 + 1;',
  7050. 'this.b = 1 + 3;',
  7051. 'this.s1 = rtl.createSet(1);',
  7052. 'this.s2 = rtl.createSet(1, 2);',
  7053. 'this.s3 = rtl.createSet(null, 1, 3);',
  7054. 'this.s4 = rtl.createSet(null, -128, 127);',
  7055. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7056. 'this.s6 = 1 in this.s2;',
  7057. 'this.i = 0;',
  7058. 'this.i2 = 0;',
  7059. '']),
  7060. LinesToStr([
  7061. '$mod.i = $mod.i2;',
  7062. '$mod.i = -1;',
  7063. 'if ($mod.i === $mod.i2) ;',
  7064. '$mod.i = $mod.i2;',
  7065. '']));
  7066. end;
  7067. procedure TTestModule.TestIntegerTypecasts;
  7068. begin
  7069. StartProgram(false);
  7070. Add([
  7071. 'var',
  7072. ' i: nativeint;',
  7073. ' b: byte;',
  7074. ' sh: shortint;',
  7075. ' w: word;',
  7076. ' sm: smallint;',
  7077. ' lw: longword;',
  7078. ' li: longint;',
  7079. 'begin',
  7080. ' b:=byte(i);',
  7081. ' sh:=shortint(i);',
  7082. ' w:=word(i);',
  7083. ' sm:=smallint(i);',
  7084. ' lw:=longword(i);',
  7085. ' li:=longint(i);',
  7086. '']);
  7087. ConvertProgram;
  7088. CheckSource('TestIntegerTypecasts',
  7089. LinesToStr([
  7090. 'this.i = 0;',
  7091. 'this.b = 0;',
  7092. 'this.sh = 0;',
  7093. 'this.w = 0;',
  7094. 'this.sm = 0;',
  7095. 'this.lw = 0;',
  7096. 'this.li = 0;',
  7097. '']),
  7098. LinesToStr([
  7099. '$mod.b = $mod.i & 255;',
  7100. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7101. '$mod.w = $mod.i & 65535;',
  7102. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7103. '$mod.lw = $mod.i >>> 0;',
  7104. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7105. '']));
  7106. end;
  7107. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7108. begin
  7109. StartProgram(false);
  7110. Add([
  7111. 'var',
  7112. ' i,j: nativeint;',
  7113. 'begin',
  7114. ' i:=i shr 0;',
  7115. ' i:=i shr 1;',
  7116. ' i:=i shr 3;',
  7117. ' i:=i shr 54;',
  7118. ' i:=j shr i;',
  7119. '']);
  7120. ConvertProgram;
  7121. CheckResolverUnexpectedHints;
  7122. CheckSource('TestInteger_BitwiseShrNativeInt',
  7123. LinesToStr([
  7124. 'this.i = 0;',
  7125. 'this.j = 0;',
  7126. '']),
  7127. LinesToStr([
  7128. '$mod.i = $mod.i;',
  7129. '$mod.i = Math.floor($mod.i / 2);',
  7130. '$mod.i = Math.floor($mod.i / 8);',
  7131. '$mod.i = 0;',
  7132. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7133. '']));
  7134. end;
  7135. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7136. begin
  7137. StartProgram(false);
  7138. Add([
  7139. 'var',
  7140. ' i: nativeint;',
  7141. 'begin',
  7142. ' i:=i shl 0;',
  7143. ' i:=i shl 54;',
  7144. ' i:=123456789012 shl 1;',
  7145. ' i:=i shl 1;',
  7146. '']);
  7147. ConvertProgram;
  7148. CheckResolverUnexpectedHints;
  7149. CheckSource('TestInteger_BitwiseShrNativeInt',
  7150. LinesToStr([
  7151. 'this.i = 0;',
  7152. '']),
  7153. LinesToStr([
  7154. '$mod.i = $mod.i;',
  7155. '$mod.i = 0;',
  7156. '$mod.i = 246913578024;',
  7157. '$mod.i = rtl.shl($mod.i, 1);',
  7158. '']));
  7159. end;
  7160. procedure TTestModule.TestInteger_SystemFunc;
  7161. begin
  7162. StartProgram(true);
  7163. Add([
  7164. 'var',
  7165. ' i: byte;',
  7166. ' s: string;',
  7167. 'begin',
  7168. ' system.inc(i);',
  7169. ' system.str(i,s);',
  7170. ' s:=system.str(i);',
  7171. ' i:=system.low(i);',
  7172. ' i:=system.high(i);',
  7173. ' i:=system.pred(i);',
  7174. ' i:=system.succ(i);',
  7175. ' i:=system.ord(i);',
  7176. '']);
  7177. ConvertProgram;
  7178. CheckResolverUnexpectedHints;
  7179. CheckSource('TestInteger_SystemFunc',
  7180. LinesToStr([
  7181. 'this.i = 0;',
  7182. 'this.s = "";',
  7183. '']),
  7184. LinesToStr([
  7185. '$mod.i += 1;',
  7186. '$mod.s = "" + $mod.i;',
  7187. '$mod.s = "" + $mod.i;',
  7188. '$mod.i = 0;',
  7189. '$mod.i = 255;',
  7190. '$mod.i = $mod.i - 1;',
  7191. '$mod.i = $mod.i + 1;',
  7192. '$mod.i = $mod.i;',
  7193. '']));
  7194. end;
  7195. procedure TTestModule.TestInteger_AssignOutsideConst;
  7196. begin
  7197. StartProgram(false);
  7198. Add([
  7199. 'const',
  7200. ' MinInt = low(longint);',
  7201. ' MaxInt = high(longint);',
  7202. 'type',
  7203. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7204. 'var',
  7205. ' i: TMyInt;',
  7206. ' aByte: byte;',
  7207. ' aShortInt: shortint;',
  7208. ' aWord: word;',
  7209. ' aSmallInt: smallint;',
  7210. ' aLongWord: longword;',
  7211. ' aLongInt: longint;',
  7212. ' aNativeInt: nativeint;',
  7213. ' aNativeUInt: nativeuint;',
  7214. 'begin',
  7215. ' aByte:=$FF;',
  7216. ' aByte:=$100;',
  7217. ' aByte:=-1;',
  7218. ' aByte:=-127;',
  7219. ' aByte:=-128;',
  7220. ' aByte:=-254;',
  7221. ' aByte:=-255;',
  7222. ' aByte:=-256;',
  7223. ' aShortInt:=127;',
  7224. ' aShortInt:=128;',
  7225. ' aShortInt:=-128;',
  7226. ' aShortInt:=-129;',
  7227. ' aWord:=$ffff;',
  7228. ' aWord:=$10000;',
  7229. ' aWord:=-1;',
  7230. ' aWord:=-$ffff;',
  7231. ' aWord:=-$10000;',
  7232. ' aWord:=-$10001;',
  7233. ' aSmallInt:=$7fff;',
  7234. ' aSmallInt:=$8000;',
  7235. ' aSmallInt:=-$8000;',
  7236. ' aSmallInt:=-$8001;',
  7237. ' aLongWord:=$ffffffff;',
  7238. ' aLongWord:=$100000000;',
  7239. ' aLongWord:=-1;',
  7240. ' aLongWord:=-$ffffffff;',
  7241. ' aNativeInt:=$1fffffffffffff;',
  7242. ' aNativeInt:=-$1fffffffffffff;',
  7243. ' aNativeUInt:=$1fffffffffffff;',
  7244. ' aNativeUInt:=-$1fffffffffffff;',
  7245. '']);
  7246. ConvertProgram;
  7247. CheckSource('TestInteger_AssignOutsideConst',
  7248. LinesToStr([
  7249. 'this.MinInt = -2147483648;',
  7250. 'this.MaxInt = 2147483647;',
  7251. 'this.i = 0;',
  7252. 'this.aByte = 0;',
  7253. 'this.aShortInt = 0;',
  7254. 'this.aWord = 0;',
  7255. 'this.aSmallInt = 0;',
  7256. 'this.aLongWord = 0;',
  7257. 'this.aLongInt = 0;',
  7258. 'this.aNativeInt = 0;',
  7259. 'this.aNativeUInt = 0;',
  7260. '']),
  7261. LinesToStr([
  7262. '$mod.aByte = 0xFF;',
  7263. '$mod.aByte = 0;',
  7264. '$mod.aByte = 255;',
  7265. '$mod.aByte = 129;',
  7266. '$mod.aByte = 128;',
  7267. '$mod.aByte = 2;',
  7268. '$mod.aByte = 1;',
  7269. '$mod.aByte = 0;',
  7270. '$mod.aShortInt = 127;',
  7271. '$mod.aShortInt = -128;',
  7272. '$mod.aShortInt = -128;',
  7273. '$mod.aShortInt = 127;',
  7274. '$mod.aWord = 0xffff;',
  7275. '$mod.aWord = 0;',
  7276. '$mod.aWord = 65535;',
  7277. '$mod.aWord = 1;',
  7278. '$mod.aWord = 0;',
  7279. '$mod.aWord = 65535;',
  7280. '$mod.aSmallInt = 0x7fff;',
  7281. '$mod.aSmallInt = -32768;',
  7282. '$mod.aSmallInt = -0x8000;',
  7283. '$mod.aSmallInt = 32767;',
  7284. '$mod.aLongWord = 0xffffffff;',
  7285. '$mod.aLongWord = 0;',
  7286. '$mod.aLongWord = 4294967295;',
  7287. '$mod.aLongWord = 1;',
  7288. '$mod.aNativeInt = 0x1fffffffffffff;',
  7289. '$mod.aNativeInt = -0x1fffffffffffff;',
  7290. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7291. '$mod.aNativeUInt = 1;',
  7292. '']));
  7293. end;
  7294. procedure TTestModule.TestCurrency;
  7295. begin
  7296. StartProgram(false);
  7297. Add([
  7298. 'type',
  7299. ' TCoin = currency;',
  7300. 'const',
  7301. ' a = TCoin(2.7);',
  7302. ' b = a + TCoin(1.7);',
  7303. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7304. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7305. 'var',
  7306. ' c: TCoin = b;',
  7307. ' i: nativeint;',
  7308. ' d: double;',
  7309. ' j: jsvalue;',
  7310. 'function DoIt(c: currency): currency; begin end;',
  7311. 'function GetIt(d: double): double; begin end;',
  7312. 'procedure Write(v: jsvalue); begin end;',
  7313. 'begin',
  7314. ' c:=1.0;',
  7315. ' c:=0.1;',
  7316. ' c:=1.0/3.0;',
  7317. ' c:=1/3;',
  7318. ' c:=a;',
  7319. ' d:=c;',
  7320. ' c:=d;',
  7321. ' c:=currency(c);',
  7322. ' c:=currency(d);',
  7323. ' d:=double(c);',
  7324. ' c:=i;',
  7325. ' c:=currency(i);',
  7326. //' i:=c;', not allowed
  7327. ' i:=nativeint(c);',
  7328. ' c:=c+a;',
  7329. ' c:=-c-a;',
  7330. ' c:=d+c;',
  7331. ' c:=c+d;',
  7332. ' c:=d-c;',
  7333. ' c:=c-d;',
  7334. ' c:=c*a;',
  7335. ' c:=a*c;',
  7336. ' c:=d*c;',
  7337. ' c:=c*d;',
  7338. ' c:=c/a;',
  7339. ' c:=a/c;',
  7340. ' c:=d/c;',
  7341. ' c:=c/d;',
  7342. ' c:=c**a;',
  7343. ' c:=a**c;',
  7344. ' c:=d**c;',
  7345. ' c:=c**d;',
  7346. ' if c=c then ;',
  7347. ' if c=a then ;',
  7348. ' if a=c then ;',
  7349. ' if d=c then ;',
  7350. ' if c=d then ;',
  7351. ' c:=DoIt(c);',
  7352. ' c:=DoIt(i);',
  7353. ' c:=DoIt(d);',
  7354. ' c:=GetIt(c);',
  7355. ' j:=c;',
  7356. ' Write(c);',
  7357. ' c:=default(currency);',
  7358. ' j:=str(c);',
  7359. ' j:=str(c:0:3);',
  7360. '']);
  7361. ConvertProgram;
  7362. CheckSource('TestCurrency',
  7363. LinesToStr([
  7364. 'this.a = 27000;',
  7365. 'this.b = this.a + 17000;',
  7366. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7367. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7368. 'this.c = this.b;',
  7369. 'this.i = 0;',
  7370. 'this.d = 0.0;',
  7371. 'this.j = undefined;',
  7372. 'this.DoIt = function (c) {',
  7373. ' var Result = 0;',
  7374. ' return Result;',
  7375. '};',
  7376. 'this.GetIt = function (d) {',
  7377. ' var Result = 0.0;',
  7378. ' return Result;',
  7379. '};',
  7380. 'this.Write = function (v) {',
  7381. '};',
  7382. '']),
  7383. LinesToStr([
  7384. '$mod.c = 10000;',
  7385. '$mod.c = 1000;',
  7386. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7387. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7388. '$mod.c = $mod.a;',
  7389. '$mod.d = $mod.c / 10000;',
  7390. '$mod.c = rtl.trunc($mod.d * 10000);',
  7391. '$mod.c = $mod.c;',
  7392. '$mod.c = $mod.d * 10000;',
  7393. '$mod.d = $mod.c / 10000;',
  7394. '$mod.c = $mod.i * 10000;',
  7395. '$mod.c = $mod.i * 10000;',
  7396. '$mod.i = rtl.trunc($mod.c / 10000);',
  7397. '$mod.c = $mod.c + $mod.a;',
  7398. '$mod.c = -$mod.c - $mod.a;',
  7399. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7400. '$mod.c = $mod.c + ($mod.d * 10000);',
  7401. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7402. '$mod.c = $mod.c - ($mod.d * 10000);',
  7403. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7404. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7405. '$mod.c = $mod.d * $mod.c;',
  7406. '$mod.c = $mod.c * $mod.d;',
  7407. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7408. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7409. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7410. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7411. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7412. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7413. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7414. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7415. 'if ($mod.c === $mod.c) ;',
  7416. 'if ($mod.c === $mod.a) ;',
  7417. 'if ($mod.a === $mod.c) ;',
  7418. 'if (($mod.d * 10000) === $mod.c) ;',
  7419. 'if ($mod.c === ($mod.d * 10000)) ;',
  7420. '$mod.c = $mod.DoIt($mod.c);',
  7421. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7422. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7423. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7424. '$mod.j = $mod.c / 10000;',
  7425. '$mod.Write($mod.c / 10000);',
  7426. '$mod.c = 0;',
  7427. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7428. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7429. '']));
  7430. end;
  7431. procedure TTestModule.TestForBoolDo;
  7432. begin
  7433. StartProgram(false);
  7434. Add([
  7435. 'var b: boolean;',
  7436. 'begin',
  7437. ' for b:=false to true do ;',
  7438. ' for b:=b downto false do ;',
  7439. ' for b in boolean do ;',
  7440. '']);
  7441. ConvertProgram;
  7442. CheckSource('TestForBoolDo',
  7443. LinesToStr([ // statements
  7444. 'this.b = false;']),
  7445. LinesToStr([ // this.$main
  7446. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7447. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7448. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7449. '']));
  7450. end;
  7451. procedure TTestModule.TestForIntDo;
  7452. begin
  7453. StartProgram(false);
  7454. Add([
  7455. 'var i: longint;',
  7456. 'begin',
  7457. ' for i:=3 to 5 do ;',
  7458. ' for i:=i downto 2 do ;',
  7459. ' for i in byte do ;',
  7460. '']);
  7461. ConvertProgram;
  7462. CheckSource('TestForIntDo',
  7463. LinesToStr([ // statements
  7464. 'this.i = 0;']),
  7465. LinesToStr([ // this.$main
  7466. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7467. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7468. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7469. '']));
  7470. end;
  7471. procedure TTestModule.TestForIntInDo;
  7472. begin
  7473. StartProgram(false);
  7474. Add([
  7475. 'type',
  7476. ' TSetOfInt = set of byte;',
  7477. ' TIntRg = 3..7;',
  7478. ' TSetOfIntRg = set of TIntRg;',
  7479. 'var',
  7480. ' i,i2: longint;',
  7481. ' a1: array of byte;',
  7482. ' a2: array[1..3] of byte;',
  7483. ' soi: TSetOfInt;',
  7484. ' soir: TSetOfIntRg;',
  7485. ' ir: TIntRg;',
  7486. 'begin',
  7487. ' for i in byte do ;',
  7488. ' for i in a1 do ;',
  7489. ' for i in a2 do ;',
  7490. ' for i in [11..13] do ;',
  7491. ' for i in TSetOfInt do ;',
  7492. ' for i in TIntRg do ;',
  7493. ' for i in soi do i2:=i;',
  7494. ' for i in TSetOfIntRg do ;',
  7495. ' for i in soir do ;',
  7496. ' for ir in TIntRg do ;',
  7497. ' for ir in TSetOfIntRg do ;',
  7498. ' for ir in soir do ;',
  7499. '']);
  7500. ConvertProgram;
  7501. CheckSource('TestForIntInDo',
  7502. LinesToStr([ // statements
  7503. 'this.i = 0;',
  7504. 'this.i2 = 0;',
  7505. 'this.a1 = [];',
  7506. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7507. 'this.soi = {};',
  7508. 'this.soir = {};',
  7509. 'this.ir = 0;',
  7510. '']),
  7511. LinesToStr([ // this.$main
  7512. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7513. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7514. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7515. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7516. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7517. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7518. 'for (var $l6 in $mod.soi) {',
  7519. ' $mod.i = +$l6;',
  7520. ' $mod.i2 = $mod.i;',
  7521. '};',
  7522. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7523. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7524. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7525. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7526. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7527. '']));
  7528. end;
  7529. procedure TTestModule.TestCharConst;
  7530. begin
  7531. StartProgram(false);
  7532. Add([
  7533. 'const',
  7534. ' a = #$00F3;',
  7535. ' c: char = ''1'';',
  7536. ' wc: widechar = ''ä'';',
  7537. 'begin',
  7538. ' c:=#0;',
  7539. ' c:=#1;',
  7540. ' c:=#9;',
  7541. ' c:=#10;',
  7542. ' c:=#13;',
  7543. ' c:=#31;',
  7544. ' c:=#32;',
  7545. ' c:=#$A;',
  7546. ' c:=#$0A;',
  7547. ' c:=#$b;',
  7548. ' c:=#$0b;',
  7549. ' c:=^A;',
  7550. ' c:=''"'';',
  7551. ' c:=default(char);',
  7552. ' c:=#$00E4;', // ä
  7553. ' c:=''ä'';',
  7554. ' c:=#$E4;', // ä
  7555. ' c:=#$D800;', // invalid UTF-16
  7556. ' c:=#$DFFF;', // invalid UTF-16
  7557. ' c:=#$FFFF;', // last UCS-2
  7558. ' c:=high(c);', // last UCS-2
  7559. ' c:=#269;',
  7560. '']);
  7561. ConvertProgram;
  7562. CheckSource('TestCharConst',
  7563. LinesToStr([
  7564. 'this.a="ó";',
  7565. 'this.c="1";',
  7566. 'this.wc="ä";'
  7567. ]),
  7568. LinesToStr([
  7569. '$mod.c="\x00";',
  7570. '$mod.c="\x01";',
  7571. '$mod.c="\t";',
  7572. '$mod.c="\n";',
  7573. '$mod.c="\r";',
  7574. '$mod.c="\x1F";',
  7575. '$mod.c=" ";',
  7576. '$mod.c="\n";',
  7577. '$mod.c="\n";',
  7578. '$mod.c="\x0B";',
  7579. '$mod.c="\x0B";',
  7580. '$mod.c="\x01";',
  7581. '$mod.c=''"'';',
  7582. '$mod.c="\x00";',
  7583. '$mod.c = "ä";',
  7584. '$mod.c = "ä";',
  7585. '$mod.c = "ä";',
  7586. '$mod.c="\uD800";',
  7587. '$mod.c="\uDFFF";',
  7588. '$mod.c="\uFFFF";',
  7589. '$mod.c="\uFFFF";',
  7590. '$mod.c = "č";',
  7591. '']));
  7592. end;
  7593. procedure TTestModule.TestChar_Compare;
  7594. begin
  7595. StartProgram(false);
  7596. Add('var');
  7597. Add(' c: char;');
  7598. Add(' b: boolean;');
  7599. Add('begin');
  7600. Add(' b:=c=''1'';');
  7601. Add(' b:=''2''=c;');
  7602. Add(' b:=''3''=''4'';');
  7603. Add(' b:=c<>''5'';');
  7604. Add(' b:=''6''<>c;');
  7605. Add(' b:=c>''7'';');
  7606. Add(' b:=''8''>c;');
  7607. Add(' b:=c>=''9'';');
  7608. Add(' b:=''A''>=c;');
  7609. Add(' b:=c<''B'';');
  7610. Add(' b:=''C''<c;');
  7611. Add(' b:=c<=''D'';');
  7612. Add(' b:=''E''<=c;');
  7613. ConvertProgram;
  7614. CheckSource('TestChar_Compare',
  7615. LinesToStr([
  7616. 'this.c="";',
  7617. 'this.b = false;'
  7618. ]),
  7619. LinesToStr([
  7620. '$mod.b = $mod.c === "1";',
  7621. '$mod.b = "2" === $mod.c;',
  7622. '$mod.b = "3" === "4";',
  7623. '$mod.b = $mod.c !== "5";',
  7624. '$mod.b = "6" !== $mod.c;',
  7625. '$mod.b = $mod.c > "7";',
  7626. '$mod.b = "8" > $mod.c;',
  7627. '$mod.b = $mod.c >= "9";',
  7628. '$mod.b = "A" >= $mod.c;',
  7629. '$mod.b = $mod.c < "B";',
  7630. '$mod.b = "C" < $mod.c;',
  7631. '$mod.b = $mod.c <= "D";',
  7632. '$mod.b = "E" <= $mod.c;',
  7633. '']));
  7634. end;
  7635. procedure TTestModule.TestChar_BuiltInProcs;
  7636. begin
  7637. StartProgram(false);
  7638. Add([
  7639. 'var',
  7640. ' c: char;',
  7641. ' i: longint;',
  7642. ' s: string;',
  7643. 'begin',
  7644. ' i:=ord(c);',
  7645. ' i:=ord(s[i]);',
  7646. ' c:=chr(i);',
  7647. ' c:=pred(c);',
  7648. ' c:=succ(c);',
  7649. ' c:=low(c);',
  7650. ' c:=high(c);',
  7651. ' i:=byte(c);',
  7652. ' i:=word(c);',
  7653. ' i:=longint(c);',
  7654. '']);
  7655. ConvertProgram;
  7656. CheckSource('TestChar_BuiltInProcs',
  7657. LinesToStr([
  7658. 'this.c = "";',
  7659. 'this.i = 0;',
  7660. 'this.s = "";'
  7661. ]),
  7662. LinesToStr([
  7663. '$mod.i = $mod.c.charCodeAt();',
  7664. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7665. '$mod.c = String.fromCharCode($mod.i);',
  7666. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7667. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7668. '$mod.c = "\x00";',
  7669. '$mod.c = "\uFFFF";',
  7670. '$mod.i = $mod.c.charCodeAt() & 255;',
  7671. '$mod.i = $mod.c.charCodeAt();',
  7672. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7673. '']));
  7674. end;
  7675. procedure TTestModule.TestStringConst;
  7676. begin
  7677. StartProgram(false);
  7678. Add([
  7679. '{$H+}',
  7680. 'const',
  7681. ' a = #$00F3#$017C;', // first <256, then >=256
  7682. ' b = string(''a'');',
  7683. ' c = string(''ä'');',
  7684. ' d = UnicodeString(''b'');',
  7685. ' e = UnicodeString(''ö'');',
  7686. ' f = low(a)+high(b);',
  7687. ' g: word = low(a);',
  7688. 'var',
  7689. ' s: string = ''abc'';',
  7690. ' i: longint;',
  7691. 'begin',
  7692. ' s:='''';',
  7693. ' s:=#13#10;',
  7694. ' s:=#9''foo'';',
  7695. ' s:=#$A9;',
  7696. ' s:=''foo''#13''bar'';',
  7697. ' s:=''"'';',
  7698. ' s:=''"''''"'';',
  7699. ' s:=#$20AC;', // euro
  7700. ' s:=#$10437;', // outside BMP
  7701. ' s:=''abc''#$20AC;', // ascii,#
  7702. ' s:=''ä''#$20AC;', // non ascii,#
  7703. ' s:=#$20AC''abc'';', // #, ascii
  7704. ' s:=#$20AC''ä'';', // #, non ascii
  7705. ' s:=default(string);',
  7706. ' s:=concat(s);',
  7707. ' s:=concat(s,''a'',s);',
  7708. ' s:=#250#269;',
  7709. ' i:=low(s)+high(a);',
  7710. //' s:=#$2F804;',
  7711. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7712. '']);
  7713. ConvertProgram;
  7714. CheckSource('TestStringConst',
  7715. LinesToStr([
  7716. 'this.a = "óż";',
  7717. 'this.b = "a";',
  7718. 'this.c = "ä";',
  7719. 'this.d = "b";',
  7720. 'this.e = "ö";',
  7721. 'this.f = 1 + this.b.length;',
  7722. 'this.g = 1;',
  7723. 'this.s="abc";',
  7724. 'this.i = 0;',
  7725. '']),
  7726. LinesToStr([
  7727. '$mod.s="";',
  7728. '$mod.s="\r\n";',
  7729. '$mod.s="\tfoo";',
  7730. '$mod.s="©";',
  7731. '$mod.s="foo\rbar";',
  7732. '$mod.s=''"'';',
  7733. '$mod.s=''"\''"'';',
  7734. '$mod.s="€";',
  7735. '$mod.s="'#$F0#$90#$90#$B7'";',
  7736. '$mod.s = "abc€";',
  7737. '$mod.s = "ä€";',
  7738. '$mod.s = "€abc";',
  7739. '$mod.s = "ۊ";',
  7740. '$mod.s="";',
  7741. '$mod.s = $mod.s;',
  7742. '$mod.s = $mod.s.concat("a", $mod.s);',
  7743. '$mod.s = "úč";',
  7744. '$mod.i = 1 + $mod.a.length;',
  7745. '']));
  7746. end;
  7747. procedure TTestModule.TestStringConst_InvalidUTF16;
  7748. begin
  7749. StartProgram(false);
  7750. Add([
  7751. 'const',
  7752. ' a: char = #$D87E;',
  7753. ' b: string = #$D87E;',
  7754. ' c: string = #$D87E#43;',
  7755. 'begin',
  7756. ' c:=''abc''#$D87E;',
  7757. ' c:=#0#1#2;',
  7758. ' c:=#127;',
  7759. ' c:=#128;',
  7760. ' c:=#255;',
  7761. ' c:=#256;',
  7762. '']);
  7763. ConvertProgram;
  7764. CheckSource('TestStringConst',
  7765. LinesToStr([
  7766. 'this.a = "\uD87E";',
  7767. 'this.b = "\uD87E";',
  7768. 'this.c = "\uD87E+";',
  7769. '']),
  7770. LinesToStr([
  7771. '$mod.c = "abc\uD87E";',
  7772. '$mod.c = "\x00\x01\x02";',
  7773. '$mod.c = "'#127'";',
  7774. '$mod.c = "'#$c2#$80'";',
  7775. '$mod.c = "'#$c3#$BF'";',
  7776. '$mod.c = "'#$c4#$80'";',
  7777. '']));
  7778. end;
  7779. procedure TTestModule.TestStringConstSurrogate;
  7780. begin
  7781. StartProgram(false);
  7782. Add([
  7783. 'var',
  7784. ' s: string;',
  7785. 'begin',
  7786. ' s:=''😊'';', // 1F60A
  7787. '']);
  7788. ConvertProgram;
  7789. CheckSource('TestStringConstSurrogate',
  7790. LinesToStr([
  7791. 'this.s="";'
  7792. ]),
  7793. LinesToStr([
  7794. '$mod.s="😊";'
  7795. ]));
  7796. end;
  7797. procedure TTestModule.TestString_Length;
  7798. begin
  7799. StartProgram(false);
  7800. Add('const c = ''foo'';');
  7801. Add('var');
  7802. Add(' s: string;');
  7803. Add(' i: longint;');
  7804. Add('begin');
  7805. Add(' i:=length(s);');
  7806. Add(' i:=length(s+s);');
  7807. Add(' i:=length(''abc'');');
  7808. Add(' i:=length(c);');
  7809. ConvertProgram;
  7810. CheckSource('TestString_Length',
  7811. LinesToStr([
  7812. 'this.c = "foo";',
  7813. 'this.s = "";',
  7814. 'this.i = 0;',
  7815. '']),
  7816. LinesToStr([
  7817. '$mod.i = $mod.s.length;',
  7818. '$mod.i = ($mod.s+$mod.s).length;',
  7819. '$mod.i = "abc".length;',
  7820. '$mod.i = $mod.c.length;',
  7821. '']));
  7822. end;
  7823. procedure TTestModule.TestString_Compare;
  7824. begin
  7825. StartProgram(false);
  7826. Add('var');
  7827. Add(' s, t: string;');
  7828. Add(' b: boolean;');
  7829. Add('begin');
  7830. Add(' b:=s=t;');
  7831. Add(' b:=s<>t;');
  7832. Add(' b:=s>t;');
  7833. Add(' b:=s>=t;');
  7834. Add(' b:=s<t;');
  7835. Add(' b:=s<=t;');
  7836. ConvertProgram;
  7837. CheckSource('TestString_Compare',
  7838. LinesToStr([ // statements
  7839. 'this.s = "";',
  7840. 'this.t = "";',
  7841. 'this.b =false;'
  7842. ]),
  7843. LinesToStr([ // this.$main
  7844. '$mod.b = $mod.s === $mod.t;',
  7845. '$mod.b = $mod.s !== $mod.t;',
  7846. '$mod.b = $mod.s > $mod.t;',
  7847. '$mod.b = $mod.s >= $mod.t;',
  7848. '$mod.b = $mod.s < $mod.t;',
  7849. '$mod.b = $mod.s <= $mod.t;',
  7850. '']));
  7851. end;
  7852. procedure TTestModule.TestString_SetLength;
  7853. begin
  7854. StartProgram(false);
  7855. Add([
  7856. 'procedure Fly(var s: string);',
  7857. 'begin',
  7858. ' SetLength(s,1);',
  7859. 'end;',
  7860. 'procedure Run(var s: unicodestring);',
  7861. 'begin',
  7862. ' SetLength(s,2);',
  7863. 'end;',
  7864. 'var s: string;',
  7865. ' u: unicodestring;',
  7866. 'begin',
  7867. ' SetLength(s,3);',
  7868. ' SetLength(u,4);',
  7869. '']);
  7870. ConvertProgram;
  7871. CheckSource('TestString_SetLength',
  7872. LinesToStr([ // statements
  7873. 'this.Fly = function (s) {',
  7874. ' s.set(rtl.strSetLength(s.get(), 1));',
  7875. '};',
  7876. 'this.Run = function (s) {',
  7877. ' s.set(rtl.strSetLength(s.get(), 2));',
  7878. '};',
  7879. 'this.s = "";',
  7880. 'this.u = "";',
  7881. '']),
  7882. LinesToStr([ // this.$main
  7883. '$mod.s = rtl.strSetLength($mod.s, 3);',
  7884. '$mod.u = rtl.strSetLength($mod.u, 4);'
  7885. ]));
  7886. end;
  7887. procedure TTestModule.TestString_CharAt;
  7888. begin
  7889. StartProgram(false);
  7890. Add([
  7891. 'var',
  7892. ' s: string;',
  7893. ' c: char;',
  7894. ' b: boolean;',
  7895. 'begin',
  7896. ' b:= s[1] = c;',
  7897. ' b:= c = s[1];',
  7898. ' b:= c <> s[1];',
  7899. ' b:= c > s[1];',
  7900. ' b:= c >= s[1];',
  7901. ' b:= c < s[2];',
  7902. ' b:= c <= s[1];',
  7903. ' s[1] := c;',
  7904. ' s[2+3] := c;']);
  7905. ConvertProgram;
  7906. CheckSource('TestString_CharAt',
  7907. LinesToStr([ // statements
  7908. 'this.s = "";',
  7909. 'this.c = "";',
  7910. 'this.b = false;'
  7911. ]),
  7912. LinesToStr([ // this.$main
  7913. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7914. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7915. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7916. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7917. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7918. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7919. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7920. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7921. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7922. '']));
  7923. end;
  7924. procedure TTestModule.TestStringHMinusFail;
  7925. begin
  7926. StartProgram(false);
  7927. Add([
  7928. '{$H-}',
  7929. 'var s: string;',
  7930. 'begin']);
  7931. ConvertProgram;
  7932. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7933. end;
  7934. procedure TTestModule.TestStr;
  7935. begin
  7936. StartProgram(false);
  7937. Add('var');
  7938. Add(' b: boolean;');
  7939. Add(' i: longint;');
  7940. Add(' d: double;');
  7941. Add(' s: string;');
  7942. Add('begin');
  7943. Add(' str(b,s);');
  7944. Add(' str(i,s);');
  7945. Add(' str(d,s);');
  7946. Add(' str(i:3,s);');
  7947. Add(' str(d:3:2,s);');
  7948. Add(' Str(12.456:12:1,s);');
  7949. Add(' Str(12.456:12,s);');
  7950. Add(' s:=str(b);');
  7951. Add(' s:=str(i);');
  7952. Add(' s:=str(d);');
  7953. Add(' s:=str(i,i);');
  7954. Add(' s:=str(i:3);');
  7955. Add(' s:=str(d:3:2);');
  7956. Add(' s:=str(i:4,i);');
  7957. Add(' s:=str(i,i:5);');
  7958. Add(' s:=str(i:4,i:5);');
  7959. Add(' s:=str(s,s);');
  7960. Add(' s:=str(s,''foo'');');
  7961. ConvertProgram;
  7962. CheckSource('TestStr',
  7963. LinesToStr([ // statements
  7964. 'this.b = false;',
  7965. 'this.i = 0;',
  7966. 'this.d = 0.0;',
  7967. 'this.s = "";',
  7968. '']),
  7969. LinesToStr([ // this.$main
  7970. '$mod.s = ""+$mod.b;',
  7971. '$mod.s = ""+$mod.i;',
  7972. '$mod.s = rtl.floatToStr($mod.d);',
  7973. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7974. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7975. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7976. '$mod.s = rtl.floatToStr(12.456,12);',
  7977. '$mod.s = ""+$mod.b;',
  7978. '$mod.s = ""+$mod.i;',
  7979. '$mod.s = rtl.floatToStr($mod.d);',
  7980. '$mod.s = ""+$mod.i+$mod.i;',
  7981. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7982. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7983. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7984. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7985. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7986. '$mod.s = $mod.s + $mod.s;',
  7987. '$mod.s = $mod.s + "foo";',
  7988. '']));
  7989. end;
  7990. procedure TTestModule.TestBaseType_AnsiStringFail;
  7991. begin
  7992. StartProgram(false);
  7993. Add('var s: AnsiString');
  7994. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7995. ConvertProgram;
  7996. end;
  7997. procedure TTestModule.TestBaseType_WideStringFail;
  7998. begin
  7999. StartProgram(false);
  8000. Add('var s: WideString');
  8001. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8002. ConvertProgram;
  8003. end;
  8004. procedure TTestModule.TestBaseType_ShortStringFail;
  8005. begin
  8006. StartProgram(false);
  8007. Add('var s: ShortString');
  8008. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8009. ConvertProgram;
  8010. end;
  8011. procedure TTestModule.TestBaseType_RawByteStringFail;
  8012. begin
  8013. StartProgram(false);
  8014. Add('var s: RawByteString');
  8015. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8016. ConvertProgram;
  8017. end;
  8018. procedure TTestModule.TestTypeShortstring_Fail;
  8019. begin
  8020. StartProgram(false);
  8021. Add('type t = string[12];');
  8022. Add('var s: t;');
  8023. Add('begin');
  8024. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8025. ConvertProgram;
  8026. end;
  8027. procedure TTestModule.TestCharSet_Custom;
  8028. begin
  8029. StartProgram(false);
  8030. Add([
  8031. 'type',
  8032. ' TCharRg = ''a''..''z'';',
  8033. ' TSetOfCharRg = set of TCharRg;',
  8034. ' TCharRg2 = ''m''..''p'';',
  8035. 'const',
  8036. ' crg: TCharRg = ''b'';',
  8037. 'var',
  8038. ' c: char;',
  8039. ' crg2: TCharRg2;',
  8040. ' s: TSetOfCharRg;',
  8041. 'begin',
  8042. ' c:=crg;',
  8043. ' crg:=c;',
  8044. ' crg2:=crg;',
  8045. ' if c=crg then ;',
  8046. ' if crg=c then ;',
  8047. ' if crg=crg2 then ;',
  8048. ' if c in s then ;',
  8049. ' if crg2 in s then ;',
  8050. ' c:=default(TCharRg);',
  8051. '']);
  8052. ConvertProgram;
  8053. CheckSource('TestCharSet_Custom',
  8054. LinesToStr([ // statements
  8055. 'this.crg = "b";',
  8056. 'this.c = "";',
  8057. 'this.crg2 = "m";',
  8058. 'this.s = {};',
  8059. '']),
  8060. LinesToStr([ // this.$main
  8061. '$mod.c = $mod.crg;',
  8062. '$mod.crg = $mod.c;',
  8063. '$mod.crg2 = $mod.crg;',
  8064. 'if ($mod.c === $mod.crg) ;',
  8065. 'if ($mod.crg === $mod.c) ;',
  8066. 'if ($mod.crg === $mod.crg2) ;',
  8067. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8068. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8069. '$mod.c = "a";',
  8070. '']));
  8071. end;
  8072. procedure TTestModule.TestWideChar;
  8073. begin
  8074. StartProgram(false);
  8075. Add([
  8076. 'procedure Fly(var c: char);',
  8077. 'begin',
  8078. 'end;',
  8079. 'procedure Run(var c: widechar);',
  8080. 'begin',
  8081. 'end;',
  8082. 'var',
  8083. ' c: char;',
  8084. ' wc: widechar;',
  8085. ' w: word;',
  8086. 'begin',
  8087. ' Fly(wc);',
  8088. ' Run(c);',
  8089. ' wc:=WideChar(w);',
  8090. ' w:=ord(wc);',
  8091. '']);
  8092. ConvertProgram;
  8093. CheckSource('TestWideChar_VarArg',
  8094. LinesToStr([ // statements
  8095. 'this.Fly = function (c) {',
  8096. '};',
  8097. 'this.Run = function (c) {',
  8098. '};',
  8099. 'this.c = "";',
  8100. 'this.wc = "";',
  8101. 'this.w = 0;',
  8102. '']),
  8103. LinesToStr([ // this.$main
  8104. '$mod.Fly({',
  8105. ' p: $mod,',
  8106. ' get: function () {',
  8107. ' return this.p.wc;',
  8108. ' },',
  8109. ' set: function (v) {',
  8110. ' this.p.wc = v;',
  8111. ' }',
  8112. '});',
  8113. '$mod.Run({',
  8114. ' p: $mod,',
  8115. ' get: function () {',
  8116. ' return this.p.c;',
  8117. ' },',
  8118. ' set: function (v) {',
  8119. ' this.p.c = v;',
  8120. ' }',
  8121. '});',
  8122. '$mod.wc = String.fromCharCode($mod.w);',
  8123. '$mod.w = $mod.wc.charCodeAt();',
  8124. '',
  8125. '']));
  8126. end;
  8127. procedure TTestModule.TestForCharDo;
  8128. begin
  8129. StartProgram(false);
  8130. Add([
  8131. 'var c: char;',
  8132. 'begin',
  8133. ' for c:=''a'' to ''c'' do ;',
  8134. ' for c:=c downto ''a'' do ;',
  8135. ' for c:=''Б'' to ''Я'' do ;',
  8136. '']);
  8137. ConvertProgram;
  8138. CheckSource('TestForCharDo',
  8139. LinesToStr([ // statements
  8140. 'this.c = "";']),
  8141. LinesToStr([ // this.$main
  8142. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8143. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8144. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8145. '']));
  8146. end;
  8147. procedure TTestModule.TestForCharInDo;
  8148. begin
  8149. StartProgram(false);
  8150. Add([
  8151. 'type',
  8152. ' TSetOfChar = set of char;',
  8153. ' TCharRg = ''a''..''z'';',
  8154. ' TSetOfCharRg = set of TCharRg;',
  8155. 'const Foo = ''foo'';',
  8156. 'var',
  8157. ' c,c2: char;',
  8158. ' s: string;',
  8159. ' a1: array of char;',
  8160. ' a2: array[1..3] of char;',
  8161. ' soc: TSetOfChar;',
  8162. ' socr: TSetOfCharRg;',
  8163. ' cr: TCharRg;',
  8164. 'begin',
  8165. ' for c in foo do ;',
  8166. ' for c in s do ;',
  8167. ' for c in char do ;',
  8168. ' for c in a1 do ;',
  8169. ' for c in a2 do ;',
  8170. ' for c in [''1''..''3''] do ;',
  8171. ' for c in TSetOfChar do ;',
  8172. ' for c in TCharRg do ;',
  8173. ' for c in soc do c2:=c;',
  8174. ' for c in TSetOfCharRg do ;',
  8175. ' for c in socr do ;',
  8176. ' for cr in TCharRg do ;',
  8177. ' for cr in TSetOfCharRg do ;',
  8178. ' for cr in socr do ;',
  8179. '']);
  8180. ConvertProgram;
  8181. CheckSource('TestForCharInDo',
  8182. LinesToStr([ // statements
  8183. 'this.Foo = "foo";',
  8184. 'this.c = "";',
  8185. 'this.c2 = "";',
  8186. 'this.s = "";',
  8187. 'this.a1 = [];',
  8188. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8189. 'this.soc = {};',
  8190. 'this.socr = {};',
  8191. 'this.cr = "a";',
  8192. '']),
  8193. LinesToStr([ // this.$main
  8194. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8195. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8196. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8197. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8198. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8199. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8200. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8201. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8202. 'for (var $l8 in $mod.soc) {',
  8203. ' $mod.c = String.fromCharCode($l8);',
  8204. ' $mod.c2 = $mod.c;',
  8205. '};',
  8206. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8207. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8208. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8209. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8210. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8211. '']));
  8212. end;
  8213. procedure TTestModule.TestProcTwoArgs;
  8214. begin
  8215. StartProgram(false);
  8216. Add('procedure Test(a,b: longint);');
  8217. Add('begin');
  8218. Add('end;');
  8219. Add('begin');
  8220. ConvertProgram;
  8221. CheckSource('TestProcTwoArgs',
  8222. LinesToStr([ // statements
  8223. 'this.Test = function (a,b) {',
  8224. '};'
  8225. ]),
  8226. LinesToStr([ // this.$main
  8227. ''
  8228. ]));
  8229. end;
  8230. procedure TTestModule.TestProc_DefaultValue;
  8231. begin
  8232. StartProgram(false);
  8233. Add('procedure p1(i: longint = 1);');
  8234. Add('begin');
  8235. Add('end;');
  8236. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8237. Add('begin');
  8238. Add('end;');
  8239. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8240. Add('begin');
  8241. Add('end;');
  8242. Add('begin');
  8243. Add(' p1;');
  8244. Add(' p1();');
  8245. Add(' p1(11);');
  8246. Add(' p2;');
  8247. Add(' p2();');
  8248. Add(' p2(12);');
  8249. Add(' p2(13,''b'');');
  8250. Add(' p3();');
  8251. ConvertProgram;
  8252. CheckSource('TestProc_DefaultValue',
  8253. LinesToStr([ // statements
  8254. 'this.p1 = function (i) {',
  8255. '};',
  8256. 'this.p2 = function (i,c) {',
  8257. '};',
  8258. 'this.p3 = function (d,b,s) {',
  8259. '};'
  8260. ]),
  8261. LinesToStr([ // this.$main
  8262. ' $mod.p1(1);',
  8263. ' $mod.p1(1);',
  8264. ' $mod.p1(11);',
  8265. ' $mod.p2(1,"a");',
  8266. ' $mod.p2(1,"a");',
  8267. ' $mod.p2(12,"a");',
  8268. ' $mod.p2(13,"b");',
  8269. ' $mod.p3(1.0,false,"abc");'
  8270. ]));
  8271. end;
  8272. procedure TTestModule.TestFunctionInt;
  8273. begin
  8274. StartProgram(false);
  8275. Add('function MyTest(Bar: longint): longint;');
  8276. Add('begin');
  8277. Add(' Result:=2*bar');
  8278. Add('end;');
  8279. Add('begin');
  8280. ConvertProgram;
  8281. CheckSource('TestFunctionInt',
  8282. LinesToStr([ // statements
  8283. 'this.MyTest = function (Bar) {',
  8284. ' var Result = 0;',
  8285. ' Result = 2*Bar;',
  8286. ' return Result;',
  8287. '};'
  8288. ]),
  8289. LinesToStr([ // this.$main
  8290. ''
  8291. ]));
  8292. end;
  8293. procedure TTestModule.TestFunctionString;
  8294. begin
  8295. StartProgram(false);
  8296. Add('function Test(Bar: string): string;');
  8297. Add('begin');
  8298. Add(' Result:=bar+BAR');
  8299. Add('end;');
  8300. Add('begin');
  8301. ConvertProgram;
  8302. CheckSource('TestFunctionString',
  8303. LinesToStr([ // statements
  8304. 'this.Test = function (Bar) {',
  8305. ' var Result = "";',
  8306. ' Result = Bar+Bar;',
  8307. ' return Result;',
  8308. '};'
  8309. ]),
  8310. LinesToStr([ // this.$main
  8311. ''
  8312. ]));
  8313. end;
  8314. procedure TTestModule.TestIfThen;
  8315. begin
  8316. StartProgram(false);
  8317. Add([
  8318. 'var b: boolean;',
  8319. 'begin',
  8320. ' if b then ;',
  8321. ' if b then else ;']);
  8322. ConvertProgram;
  8323. CheckSource('TestIfThen',
  8324. LinesToStr([ // statements
  8325. 'this.b = false;',
  8326. '']),
  8327. LinesToStr([ // this.$main
  8328. 'if ($mod.b) ;',
  8329. 'if ($mod.b) ;',
  8330. '']));
  8331. end;
  8332. procedure TTestModule.TestForLoop;
  8333. begin
  8334. StartProgram(false);
  8335. Add('var');
  8336. Add(' vI, vJ, vN: longint;');
  8337. Add('begin');
  8338. Add(' VJ:=0;');
  8339. Add(' VN:=3;');
  8340. Add(' for VI:=1 to VN do');
  8341. Add(' begin');
  8342. Add(' VJ:=VJ+VI;');
  8343. Add(' end;');
  8344. ConvertProgram;
  8345. CheckSource('TestForLoop',
  8346. LinesToStr([ // statements
  8347. 'this.vI = 0;',
  8348. 'this.vJ = 0;',
  8349. 'this.vN = 0;'
  8350. ]),
  8351. LinesToStr([ // this.$main
  8352. ' $mod.vJ = 0;',
  8353. ' $mod.vN = 3;',
  8354. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8355. ' $mod.vI = $l;',
  8356. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8357. ' };',
  8358. '']));
  8359. end;
  8360. procedure TTestModule.TestForLoopInsideFunction;
  8361. begin
  8362. StartProgram(false);
  8363. Add('function SumNumbers(Count: longint): longint;');
  8364. Add('var');
  8365. Add(' vI, vJ: longint;');
  8366. Add('begin');
  8367. Add(' vj:=0;');
  8368. Add(' for vi:=1 to count do');
  8369. Add(' begin');
  8370. Add(' vj:=vj+vi;');
  8371. Add(' end;');
  8372. Add('end;');
  8373. Add('begin');
  8374. Add(' sumnumbers(3);');
  8375. ConvertProgram;
  8376. CheckSource('TestForLoopInsideFunction',
  8377. LinesToStr([ // statements
  8378. 'this.SumNumbers = function (Count) {',
  8379. ' var Result = 0;',
  8380. ' var vI = 0;',
  8381. ' var vJ = 0;',
  8382. ' vJ = 0;',
  8383. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8384. ' vI = $l;',
  8385. ' vJ = vJ + vI;',
  8386. ' };',
  8387. ' return Result;',
  8388. '};'
  8389. ]),
  8390. LinesToStr([ // $mod.$main
  8391. ' $mod.SumNumbers(3);'
  8392. ]));
  8393. end;
  8394. procedure TTestModule.TestForLoop_ReadVarAfter;
  8395. begin
  8396. StartProgram(false);
  8397. Add('var');
  8398. Add(' vI: longint;');
  8399. Add('begin');
  8400. Add(' for vi:=1 to 2 do ;');
  8401. Add(' if vi=3 then ;');
  8402. ConvertProgram;
  8403. CheckSource('TestForLoop',
  8404. LinesToStr([ // statements
  8405. 'this.vI = 0;'
  8406. ]),
  8407. LinesToStr([ // this.$main
  8408. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8409. ' if ($mod.vI===3) ;'
  8410. ]));
  8411. end;
  8412. procedure TTestModule.TestForLoop_Nested;
  8413. begin
  8414. StartProgram(false);
  8415. Add('function SumNumbers(Count: longint): longint;');
  8416. Add('var');
  8417. Add(' vI, vJ, vK: longint;');
  8418. Add('begin');
  8419. Add(' VK:=0;');
  8420. Add(' for VI:=1 to count do');
  8421. Add(' begin');
  8422. Add(' for vj:=1 to vi do');
  8423. Add(' begin');
  8424. Add(' vk:=VK+VI;');
  8425. Add(' end;');
  8426. Add(' end;');
  8427. Add('end;');
  8428. Add('begin');
  8429. Add(' sumnumbers(3);');
  8430. ConvertProgram;
  8431. CheckSource('TestForLoopInFunction',
  8432. LinesToStr([ // statements
  8433. 'this.SumNumbers = function (Count) {',
  8434. ' var Result = 0;',
  8435. ' var vI = 0;',
  8436. ' var vJ = 0;',
  8437. ' var vK = 0;',
  8438. ' vK = 0;',
  8439. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8440. ' vI = $l;',
  8441. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8442. ' vJ = $l1;',
  8443. ' vK = vK + vI;',
  8444. ' };',
  8445. ' };',
  8446. ' return Result;',
  8447. '};'
  8448. ]),
  8449. LinesToStr([ // $mod.$main
  8450. ' $mod.SumNumbers(3);'
  8451. ]));
  8452. end;
  8453. procedure TTestModule.TestRepeatUntil;
  8454. begin
  8455. StartProgram(false);
  8456. Add('var');
  8457. Add(' vI, vJ, vN: longint;');
  8458. Add('begin');
  8459. Add(' vn:=3;');
  8460. Add(' vj:=0;');
  8461. Add(' VI:=0;');
  8462. Add(' repeat');
  8463. Add(' VI:=vi+1;');
  8464. Add(' vj:=VJ+vI;');
  8465. Add(' until vi>=vn');
  8466. ConvertProgram;
  8467. CheckSource('TestRepeatUntil',
  8468. LinesToStr([ // statements
  8469. 'this.vI = 0;',
  8470. 'this.vJ = 0;',
  8471. 'this.vN = 0;'
  8472. ]),
  8473. LinesToStr([ // $mod.$main
  8474. ' $mod.vN = 3;',
  8475. ' $mod.vJ = 0;',
  8476. ' $mod.vI = 0;',
  8477. ' do{',
  8478. ' $mod.vI = $mod.vI + 1;',
  8479. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8480. ' }while(!($mod.vI>=$mod.vN));'
  8481. ]));
  8482. end;
  8483. procedure TTestModule.TestAsmBlock;
  8484. begin
  8485. StartProgram(false);
  8486. Add([
  8487. 'var',
  8488. ' vI: longint;',
  8489. 'begin',
  8490. ' vi:=1;',
  8491. ' asm',
  8492. ' if (vI===1) {',
  8493. ' vI=2;',
  8494. //' console.log(''end;'');', ToDo
  8495. ' }',
  8496. ' if (vI===2){ vI=3; }',
  8497. ' end;',
  8498. ' VI:=4;']);
  8499. ConvertProgram;
  8500. CheckSource('TestAsmBlock',
  8501. LinesToStr([ // statements
  8502. 'this.vI = 0;'
  8503. ]),
  8504. LinesToStr([ // $mod.$main
  8505. '$mod.vI = 1;',
  8506. 'if (vI===1) {',
  8507. ' vI=2;',
  8508. '}',
  8509. 'if (vI===2){ vI=3; }',
  8510. ';',
  8511. '$mod.vI = 4;'
  8512. ]));
  8513. end;
  8514. procedure TTestModule.TestAsmPas_Impl;
  8515. begin
  8516. StartUnit(false);
  8517. Add('interface');
  8518. Add('const cIntf: longint = 1;');
  8519. Add('var vIntf: longint;');
  8520. Add('implementation');
  8521. Add('const cImpl: longint = 2;');
  8522. Add('var vImpl: longint;');
  8523. Add('procedure DoIt;');
  8524. Add('const cLoc: longint = 3;');
  8525. Add('var vLoc: longint;');
  8526. Add('begin;');
  8527. Add(' asm');
  8528. //Add(' pas(vIntf)=pas(cIntf);');
  8529. //Add(' pas(vImpl)=pas(cImpl);');
  8530. //Add(' pas(vLoc)=pas(cLoc);');
  8531. Add(' end;');
  8532. Add('end;');
  8533. ConvertUnit;
  8534. CheckSource('TestAsmPas_Impl',
  8535. LinesToStr([
  8536. 'var $impl = $mod.$impl;',
  8537. 'this.cIntf = 1;',
  8538. 'this.vIntf = 0;',
  8539. '']),
  8540. '', // this.$init
  8541. LinesToStr([ // implementation
  8542. '$impl.cImpl = 2;',
  8543. '$impl.vImpl = 0;',
  8544. 'var cLoc = 3;',
  8545. '$impl.DoIt = function () {',
  8546. ' var vLoc = 0;',
  8547. '};',
  8548. '']) );
  8549. end;
  8550. procedure TTestModule.TestTryFinally;
  8551. begin
  8552. StartProgram(false);
  8553. Add('var i: longint;');
  8554. Add('begin');
  8555. Add(' try');
  8556. Add(' i:=0; i:=2 div i;');
  8557. Add(' finally');
  8558. Add(' i:=3');
  8559. Add(' end;');
  8560. ConvertProgram;
  8561. CheckSource('TestTryFinally',
  8562. LinesToStr([ // statements
  8563. 'this.i = 0;'
  8564. ]),
  8565. LinesToStr([ // $mod.$main
  8566. 'try {',
  8567. ' $mod.i = 0;',
  8568. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8569. '} finally {',
  8570. ' $mod.i = 3;',
  8571. '};'
  8572. ]));
  8573. end;
  8574. procedure TTestModule.TestTryExcept;
  8575. begin
  8576. StartProgram(false);
  8577. Add([
  8578. 'type',
  8579. ' TObject = class end;',
  8580. ' Exception = class Msg: string; end;',
  8581. ' EInvalidCast = class(Exception) end;',
  8582. 'var vI: longint;',
  8583. 'begin',
  8584. ' try',
  8585. ' vi:=1;',
  8586. ' except',
  8587. ' vi:=2',
  8588. ' end;',
  8589. ' try',
  8590. ' vi:=3;',
  8591. ' except',
  8592. ' raise;',
  8593. ' end;',
  8594. ' try',
  8595. ' VI:=4;',
  8596. ' except',
  8597. ' on einvalidcast do',
  8598. ' raise;',
  8599. ' on E: exception do',
  8600. ' if e.msg='''' then',
  8601. ' raise e;',
  8602. ' else',
  8603. ' vi:=5',
  8604. ' end;',
  8605. ' try',
  8606. ' VI:=6;',
  8607. ' except',
  8608. ' on einvalidcast do ;',
  8609. ' end;',
  8610. '']);
  8611. ConvertProgram;
  8612. CheckSource('TestTryExcept',
  8613. LinesToStr([ // statements
  8614. 'rtl.createClass(this, "TObject", null, function () {',
  8615. ' this.$init = function () {',
  8616. ' };',
  8617. ' this.$final = function () {',
  8618. ' };',
  8619. '});',
  8620. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8621. ' this.$init = function () {',
  8622. ' $mod.TObject.$init.call(this);',
  8623. ' this.Msg = "";',
  8624. ' };',
  8625. '});',
  8626. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8627. '});',
  8628. 'this.vI = 0;'
  8629. ]),
  8630. LinesToStr([ // $mod.$main
  8631. 'try {',
  8632. ' $mod.vI = 1;',
  8633. '} catch ($e) {',
  8634. ' $mod.vI = 2;',
  8635. '};',
  8636. 'try {',
  8637. ' $mod.vI = 3;',
  8638. '} catch ($e) {',
  8639. ' throw $e;',
  8640. '};',
  8641. 'try {',
  8642. ' $mod.vI = 4;',
  8643. '} catch ($e) {',
  8644. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8645. ' throw $e',
  8646. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8647. ' var E = $e;',
  8648. ' if (E.Msg === "") throw E;',
  8649. ' } else {',
  8650. ' $mod.vI = 5;',
  8651. ' }',
  8652. '};',
  8653. 'try {',
  8654. ' $mod.vI = 6;',
  8655. '} catch ($e) {',
  8656. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8657. ' } else throw $e',
  8658. '};',
  8659. '']));
  8660. end;
  8661. procedure TTestModule.TestTryExcept_ReservedWords;
  8662. begin
  8663. StartProgram(false);
  8664. Add([
  8665. 'type',
  8666. ' TObject = class end;',
  8667. ' Exception = class',
  8668. ' Symbol: string;',
  8669. ' end;',
  8670. 'var &try: longint;',
  8671. 'begin',
  8672. ' try',
  8673. ' &try:=4;',
  8674. ' except',
  8675. ' on Error: exception do',
  8676. ' if errOR.symBol='''' then',
  8677. ' raise ERRor;',
  8678. ' end;',
  8679. '']);
  8680. ConvertProgram;
  8681. CheckSource('TestTryExcept_ReservedWords',
  8682. LinesToStr([ // statements
  8683. 'rtl.createClass(this, "TObject", null, function () {',
  8684. ' this.$init = function () {',
  8685. ' };',
  8686. ' this.$final = function () {',
  8687. ' };',
  8688. '});',
  8689. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8690. ' this.$init = function () {',
  8691. ' $mod.TObject.$init.call(this);',
  8692. ' this.Symbol = "";',
  8693. ' };',
  8694. '});',
  8695. 'this.Try = 0;',
  8696. '']),
  8697. LinesToStr([ // $mod.$main
  8698. 'try {',
  8699. ' $mod.Try = 4;',
  8700. '} catch ($e) {',
  8701. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8702. ' var error = $e;',
  8703. ' if (error.Symbol === "") throw error;',
  8704. ' } else throw $e',
  8705. '};',
  8706. '']));
  8707. end;
  8708. procedure TTestModule.TestIfThenRaiseElse;
  8709. begin
  8710. StartProgram(false);
  8711. Add([
  8712. 'type',
  8713. ' TObject = class',
  8714. ' constructor Create;',
  8715. ' end;',
  8716. 'constructor TObject.Create;',
  8717. 'begin',
  8718. 'end;',
  8719. 'var b: boolean;',
  8720. 'begin',
  8721. ' if b then',
  8722. ' raise TObject.Create',
  8723. ' else',
  8724. ' b:=false;',
  8725. '']);
  8726. ConvertProgram;
  8727. CheckSource('TestIfThenRaiseElse',
  8728. LinesToStr([ // statements
  8729. 'rtl.createClass(this, "TObject", null, function () {',
  8730. ' this.$init = function () {',
  8731. ' };',
  8732. ' this.$final = function () {',
  8733. ' };',
  8734. ' this.Create = function () {',
  8735. ' return this;',
  8736. ' };',
  8737. '});',
  8738. 'this.b = false;',
  8739. '']),
  8740. LinesToStr([ // $mod.$main
  8741. 'if ($mod.b) {',
  8742. ' throw $mod.TObject.$create("Create")}',
  8743. ' else $mod.b = false;',
  8744. '']));
  8745. end;
  8746. procedure TTestModule.TestCaseOf;
  8747. begin
  8748. StartProgram(false);
  8749. Add([
  8750. 'const e: longint; external name ''$e'';',
  8751. 'var vI: longint;',
  8752. 'begin',
  8753. ' case vi of',
  8754. ' 1: ;',
  8755. ' 2: vi:=3;',
  8756. ' e: ;',
  8757. ' else',
  8758. ' VI:=4',
  8759. ' end;']);
  8760. ConvertProgram;
  8761. CheckSource('TestCaseOf',
  8762. LinesToStr([ // statements
  8763. 'this.vI = 0;'
  8764. ]),
  8765. LinesToStr([ // $mod.$main
  8766. 'var $tmp = $mod.vI;',
  8767. 'if ($tmp === 1) {}',
  8768. 'else if ($tmp === 2) {',
  8769. ' $mod.vI = 3}',
  8770. ' else if ($tmp === $e) {}',
  8771. 'else {',
  8772. ' $mod.vI = 4;',
  8773. '};'
  8774. ]));
  8775. end;
  8776. procedure TTestModule.TestCaseOf_UseSwitch;
  8777. begin
  8778. StartProgram(false);
  8779. Converter.UseSwitchStatement:=true;
  8780. Add('var Vi: longint;');
  8781. Add('begin');
  8782. Add(' case vi of');
  8783. Add(' 1: ;');
  8784. Add(' 2: VI:=3;');
  8785. Add(' else');
  8786. Add(' vi:=4');
  8787. Add(' end;');
  8788. ConvertProgram;
  8789. CheckSource('TestCaseOf_UseSwitch',
  8790. LinesToStr([ // statements
  8791. 'this.Vi = 0;'
  8792. ]),
  8793. LinesToStr([ // $mod.$main
  8794. 'switch ($mod.Vi) {',
  8795. 'case 1:',
  8796. ' break;',
  8797. 'case 2:',
  8798. ' $mod.Vi = 3;',
  8799. ' break;',
  8800. 'default:',
  8801. ' $mod.Vi = 4;',
  8802. '};'
  8803. ]));
  8804. end;
  8805. procedure TTestModule.TestCaseOfNoElse;
  8806. begin
  8807. StartProgram(false);
  8808. Add('var Vi: longint;');
  8809. Add('begin');
  8810. Add(' case vi of');
  8811. Add(' 1: begin vi:=2; VI:=3; end;');
  8812. Add(' end;');
  8813. ConvertProgram;
  8814. CheckSource('TestCaseOfNoElse',
  8815. LinesToStr([ // statements
  8816. 'this.Vi = 0;'
  8817. ]),
  8818. LinesToStr([ // $mod.$main
  8819. 'var $tmp = $mod.Vi;',
  8820. 'if ($tmp === 1) {',
  8821. ' $mod.Vi = 2;',
  8822. ' $mod.Vi = 3;',
  8823. '};'
  8824. ]));
  8825. end;
  8826. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8827. begin
  8828. StartProgram(false);
  8829. Converter.UseSwitchStatement:=true;
  8830. Add('var vI: longint;');
  8831. Add('begin');
  8832. Add(' case vi of');
  8833. Add(' 1: begin VI:=2; vi:=3; end;');
  8834. Add(' end;');
  8835. ConvertProgram;
  8836. CheckSource('TestCaseOfNoElse_UseSwitch',
  8837. LinesToStr([ // statements
  8838. 'this.vI = 0;'
  8839. ]),
  8840. LinesToStr([ // $mod.$main
  8841. 'switch ($mod.vI) {',
  8842. 'case 1:',
  8843. ' $mod.vI = 2;',
  8844. ' $mod.vI = 3;',
  8845. ' break;',
  8846. '};'
  8847. ]));
  8848. end;
  8849. procedure TTestModule.TestCaseOfRange;
  8850. begin
  8851. StartProgram(false);
  8852. Add('var vI: longint;');
  8853. Add('begin');
  8854. Add(' case vi of');
  8855. Add(' 1..3: vi:=14;');
  8856. Add(' 4,5: vi:=16;');
  8857. Add(' 6..7,9..10: ;');
  8858. Add(' else ;');
  8859. Add(' end;');
  8860. ConvertProgram;
  8861. CheckSource('TestCaseOfRange',
  8862. LinesToStr([ // statements
  8863. 'this.vI = 0;'
  8864. ]),
  8865. LinesToStr([ // $mod.$main
  8866. 'var $tmp = $mod.vI;',
  8867. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8868. ' $mod.vI = 14',
  8869. '} else if (($tmp === 4) || ($tmp === 5)){',
  8870. ' $mod.vI = 16',
  8871. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8872. ]));
  8873. end;
  8874. procedure TTestModule.TestCaseOfString;
  8875. begin
  8876. StartProgram(false);
  8877. Add([
  8878. 'var s,h: string;',
  8879. 'begin',
  8880. ' case s of',
  8881. ' ''foo'': s:=h;',
  8882. ' ''a''..''z'': h:=s;',
  8883. ' ''ў'', ''ё'': ;',
  8884. ' ''Б''..''Я'': ;',
  8885. ' end;',
  8886. '']);
  8887. ConvertProgram;
  8888. CheckSource('TestCaseOfString',
  8889. LinesToStr([ // statements
  8890. 'this.s = "";',
  8891. 'this.h = "";',
  8892. '']),
  8893. LinesToStr([ // $mod.$main
  8894. 'var $tmp = $mod.s;',
  8895. 'if ($tmp === "foo") {',
  8896. ' $mod.s = $mod.h}',
  8897. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8898. ' $mod.h = $mod.s}',
  8899. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8900. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8901. '']));
  8902. end;
  8903. procedure TTestModule.TestCaseOfChar;
  8904. begin
  8905. StartProgram(false);
  8906. Add([
  8907. 'var s,h: char;',
  8908. 'begin',
  8909. ' case s of',
  8910. ' ''a''..''z'': h:=s;',
  8911. ' ''ä'': ;',
  8912. ' ''ў'', ''ё'': ;',
  8913. ' ''Б''..''Я'': ;',
  8914. ' end;',
  8915. '']);
  8916. ConvertProgram;
  8917. CheckSource('TestCaseOfString',
  8918. LinesToStr([ // statements
  8919. 'this.s = "";',
  8920. 'this.h = "";',
  8921. '']),
  8922. LinesToStr([ // $mod.$main
  8923. 'var $tmp = $mod.s;',
  8924. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8925. ' $mod.h = $mod.s}',
  8926. ' else if ($tmp === "ä") {}',
  8927. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8928. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8929. '']));
  8930. end;
  8931. procedure TTestModule.TestCaseOfExternalClassConst;
  8932. begin
  8933. StartProgram(false);
  8934. Add([
  8935. '{$modeswitch externalclass}',
  8936. 'type',
  8937. ' TBird = class external name ''Bird''',
  8938. ' const e: longint;',
  8939. ' end;',
  8940. 'var vI: longint;',
  8941. 'begin',
  8942. ' case vi of',
  8943. ' 1: vi:=3;',
  8944. ' TBird.e: ;',
  8945. ' end;']);
  8946. ConvertProgram;
  8947. CheckSource('TestCaseOfExternalClassConst',
  8948. LinesToStr([ // statements
  8949. 'this.vI = 0;'
  8950. ]),
  8951. LinesToStr([ // $mod.$main
  8952. 'var $tmp = $mod.vI;',
  8953. 'if ($tmp === 1) {',
  8954. ' $mod.vI = 3}',
  8955. ' else if ($tmp === Bird.e) ;'
  8956. ]));
  8957. end;
  8958. procedure TTestModule.TestDebugger;
  8959. begin
  8960. StartProgram(false);
  8961. Add([
  8962. 'procedure DoIt;',
  8963. 'begin',
  8964. ' deBugger;',
  8965. ' DeBugger();',
  8966. 'end;',
  8967. 'begin',
  8968. ' Debugger;']);
  8969. ConvertProgram;
  8970. CheckSource('TestDebugger',
  8971. LinesToStr([ // statements
  8972. 'this.DoIt = function () {',
  8973. ' debugger;',
  8974. ' debugger;',
  8975. '};',
  8976. '']),
  8977. LinesToStr([ // $mod.$main
  8978. 'debugger;',
  8979. '']));
  8980. end;
  8981. procedure TTestModule.TestArray_Dynamic;
  8982. begin
  8983. StartProgram(false);
  8984. Add([
  8985. 'type',
  8986. ' TArrayInt = array of longint;',
  8987. 'var',
  8988. ' Arr: TArrayInt;',
  8989. ' i: longint;',
  8990. ' b: boolean;',
  8991. 'begin',
  8992. ' SetLength(arr,3);',
  8993. ' arr[0]:=4;',
  8994. ' arr[1]:=length(arr)+arr[0];',
  8995. ' arr[i]:=5;',
  8996. ' arr[arr[i]]:=arr[6];',
  8997. ' i:=low(arr);',
  8998. ' i:=high(arr);',
  8999. ' b:=Assigned(arr);',
  9000. ' Arr:=default(TArrayInt);']);
  9001. ConvertProgram;
  9002. CheckSource('TestArray_Dynamic',
  9003. LinesToStr([ // statements
  9004. 'this.Arr = [];',
  9005. 'this.i = 0;',
  9006. 'this.b = false;'
  9007. ]),
  9008. LinesToStr([ // $mod.$main
  9009. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9010. '$mod.Arr[0] = 4;',
  9011. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9012. '$mod.Arr[$mod.i] = 5;',
  9013. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9014. '$mod.i = 0;',
  9015. '$mod.i = rtl.length($mod.Arr) - 1;',
  9016. '$mod.b = rtl.length($mod.Arr) > 0;',
  9017. '$mod.Arr = [];',
  9018. '']));
  9019. end;
  9020. procedure TTestModule.TestArray_Dynamic_Nil;
  9021. begin
  9022. StartProgram(false);
  9023. Add('type');
  9024. Add(' TArrayInt = array of longint;');
  9025. Add('var');
  9026. Add(' Arr: TArrayInt;');
  9027. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9028. Add('begin');
  9029. Add(' arr:=nil;');
  9030. Add(' if arr=nil then;');
  9031. Add(' if nil=arr then;');
  9032. Add(' if arr<>nil then;');
  9033. Add(' if nil<>arr then;');
  9034. Add(' DoIt(nil,nil);');
  9035. ConvertProgram;
  9036. CheckSource('TestArray_Dynamic',
  9037. LinesToStr([ // statements
  9038. 'this.Arr = [];',
  9039. 'this.DoIt = function(i,j){',
  9040. '};'
  9041. ]),
  9042. LinesToStr([ // $mod.$main
  9043. '$mod.Arr = [];',
  9044. 'if (rtl.length($mod.Arr) === 0) ;',
  9045. 'if (rtl.length($mod.Arr) === 0) ;',
  9046. 'if (rtl.length($mod.Arr) > 0) ;',
  9047. 'if (rtl.length($mod.Arr) > 0) ;',
  9048. '$mod.DoIt([],[]);',
  9049. '']));
  9050. end;
  9051. procedure TTestModule.TestArray_DynMultiDimensional;
  9052. begin
  9053. StartProgram(false);
  9054. Add([
  9055. 'type',
  9056. ' TArrayInt = array of longint;',
  9057. ' TArrayArrayInt = array of TArrayInt;',
  9058. 'var',
  9059. ' Arr: TArrayInt;',
  9060. ' Arr2: TArrayArrayInt;',
  9061. ' i: longint;',
  9062. 'begin',
  9063. ' arr2:=nil;',
  9064. ' if arr2=nil then;',
  9065. ' if nil=arr2 then;',
  9066. ' i:=low(arr2);',
  9067. ' i:=low(arr2[1]);',
  9068. ' i:=high(arr2);',
  9069. ' i:=high(arr2[2]);',
  9070. ' arr2[3]:=arr;',
  9071. ' arr2[4][5]:=i;',
  9072. ' i:=arr2[6][7];',
  9073. ' arr2[8,9]:=i;',
  9074. ' i:=arr2[10,11];',
  9075. ' SetLength(arr2,14);',
  9076. ' SetLength(arr2[15],16);']);
  9077. ConvertProgram;
  9078. CheckSource('TestArray_Dynamic',
  9079. LinesToStr([ // statements
  9080. 'this.Arr = [];',
  9081. 'this.Arr2 = [];',
  9082. 'this.i = 0;'
  9083. ]),
  9084. LinesToStr([ // $mod.$main
  9085. '$mod.Arr2 = [];',
  9086. 'if (rtl.length($mod.Arr2) === 0) ;',
  9087. 'if (rtl.length($mod.Arr2) === 0) ;',
  9088. '$mod.i = 0;',
  9089. '$mod.i = 0;',
  9090. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9091. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9092. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9093. '$mod.Arr2[4][5] = $mod.i;',
  9094. '$mod.i = $mod.Arr2[6][7];',
  9095. '$mod.Arr2[8][9] = $mod.i;',
  9096. '$mod.i = $mod.Arr2[10][11];',
  9097. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9098. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9099. '']));
  9100. end;
  9101. procedure TTestModule.TestArray_DynamicAssign;
  9102. begin
  9103. StartProgram(false);
  9104. Add([
  9105. 'type',
  9106. ' TArrayInt = array of longint;',
  9107. ' TArrayArrayInt = array of TArrayInt;',
  9108. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9109. 'begin',
  9110. 'end;',
  9111. 'procedure Fly(var a: TArrayInt);',
  9112. 'begin',
  9113. 'end;',
  9114. 'var',
  9115. ' Arr: TArrayInt;',
  9116. ' Arr2: TArrayArrayInt;',
  9117. 'begin',
  9118. ' arr:=nil;',
  9119. ' arr2:=nil;',
  9120. ' arr2[1]:=nil;',
  9121. ' arr2[2]:=arr;',
  9122. ' Run(arr,arr,arr);',
  9123. ' Fly(arr);',
  9124. ' Run(arr2[4],arr2[5],arr2[6]);',
  9125. ' Fly(arr2[7]);',
  9126. '']);
  9127. ConvertProgram;
  9128. CheckSource('TestArray_DynamicAssign',
  9129. LinesToStr([ // statements
  9130. 'this.Run = function (a, b, c) {',
  9131. '};',
  9132. 'this.Fly = function (a) {',
  9133. '};',
  9134. 'this.Arr = [];',
  9135. 'this.Arr2 = [];',
  9136. '']),
  9137. LinesToStr([ // $mod.$main
  9138. '$mod.Arr = [];',
  9139. '$mod.Arr2 = [];',
  9140. '$mod.Arr2[1] = [];',
  9141. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9142. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9143. '$mod.Fly({',
  9144. ' p: $mod,',
  9145. ' get: function () {',
  9146. ' return this.p.Arr;',
  9147. ' },',
  9148. ' set: function (v) {',
  9149. ' this.p.Arr = v;',
  9150. ' }',
  9151. '});',
  9152. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9153. '$mod.Fly({',
  9154. ' a: 7,',
  9155. ' p: $mod.Arr2,',
  9156. ' get: function () {',
  9157. ' return this.p[this.a];',
  9158. ' },',
  9159. ' set: function (v) {',
  9160. ' this.p[this.a] = v;',
  9161. ' }',
  9162. '});',
  9163. '']));
  9164. end;
  9165. procedure TTestModule.TestArray_StaticInt;
  9166. begin
  9167. StartProgram(false);
  9168. Add('type');
  9169. Add(' TArrayInt = array[2..4] of longint;');
  9170. Add('var');
  9171. Add(' Arr: TArrayInt;');
  9172. Add(' Arr2: TArrayInt = (5,6,7);');
  9173. Add(' i: longint;');
  9174. Add(' b: boolean;');
  9175. Add('begin');
  9176. Add(' arr[2]:=4;');
  9177. Add(' arr[3]:=arr[2]+arr[3];');
  9178. Add(' arr[i]:=5;');
  9179. Add(' arr[arr[i]]:=arr[high(arr)];');
  9180. Add(' i:=low(arr);');
  9181. Add(' i:=high(arr);');
  9182. Add(' b:=arr[2]=arr[3];');
  9183. Add(' arr:=default(TArrayInt);');
  9184. ConvertProgram;
  9185. CheckSource('TestArray_StaticInt',
  9186. LinesToStr([ // statements
  9187. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9188. 'this.Arr2 = [5, 6, 7];',
  9189. 'this.i = 0;',
  9190. 'this.b = false;'
  9191. ]),
  9192. LinesToStr([ // $mod.$main
  9193. '$mod.Arr[0] = 4;',
  9194. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9195. '$mod.Arr[$mod.i-2] = 5;',
  9196. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9197. '$mod.i = 2;',
  9198. '$mod.i = 4;',
  9199. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9200. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9201. '']));
  9202. end;
  9203. procedure TTestModule.TestArray_StaticBool;
  9204. begin
  9205. StartProgram(false);
  9206. Add('type');
  9207. Add(' TBools = array[boolean] of boolean;');
  9208. Add(' TBool2 = array[true..true] of boolean;');
  9209. Add('var');
  9210. Add(' Arr: TBools;');
  9211. Add(' Arr2: TBool2;');
  9212. Add(' Arr3: TBools = (true,false);');
  9213. Add(' b: boolean;');
  9214. Add('begin');
  9215. Add(' b:=low(arr);');
  9216. Add(' b:=high(arr);');
  9217. Add(' arr[true]:=false;');
  9218. Add(' arr[false]:=arr[b] or arr[true];');
  9219. Add(' arr[b]:=true;');
  9220. Add(' arr[arr[b]]:=arr[high(arr)];');
  9221. Add(' b:=arr[false]=arr[true];');
  9222. Add(' b:=low(arr2);');
  9223. Add(' b:=high(arr2);');
  9224. Add(' arr2[true]:=true;');
  9225. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9226. Add(' arr2[b]:=false;');
  9227. ConvertProgram;
  9228. CheckSource('TestArray_StaticBool',
  9229. LinesToStr([ // statements
  9230. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9231. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9232. 'this.Arr3 = [true, false];',
  9233. 'this.b = false;'
  9234. ]),
  9235. LinesToStr([ // $mod.$main
  9236. '$mod.b = false;',
  9237. '$mod.b = true;',
  9238. '$mod.Arr[1] = false;',
  9239. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9240. '$mod.Arr[+$mod.b] = true;',
  9241. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9242. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9243. '$mod.b = true;',
  9244. '$mod.b = true;',
  9245. '$mod.Arr2[0] = true;',
  9246. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9247. '$mod.Arr2[1-$mod.b] = false;',
  9248. '']));
  9249. end;
  9250. procedure TTestModule.TestArray_StaticChar;
  9251. begin
  9252. StartProgram(false);
  9253. Add([
  9254. 'type',
  9255. ' TChars = array[char] of char;',
  9256. ' TChars2 = array[''a''..''z''] of char;',
  9257. 'var',
  9258. ' Arr: TChars;',
  9259. ' Arr2: TChars2;',
  9260. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9261. ' Arr4: array[11..13] of char = ''pas'';',
  9262. ' Arr5: array[21..22] of char = ''äö'';',
  9263. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9264. ' c: char;',
  9265. ' b: boolean;',
  9266. 'begin',
  9267. ' c:=low(arr);',
  9268. ' c:=high(arr);',
  9269. ' arr[''B'']:=''a'';',
  9270. ' arr[''D'']:=arr[c];',
  9271. ' arr[c]:=arr[''d''];',
  9272. ' arr[arr[c]]:=arr[high(arr)];',
  9273. ' b:=arr[low(arr)]=arr[''e''];',
  9274. ' c:=low(arr2);',
  9275. ' c:=high(arr2);',
  9276. ' arr2[''b'']:=''f'';',
  9277. ' arr2[''a'']:=arr2[c];',
  9278. ' arr2[c]:=arr2[''g''];']);
  9279. ConvertProgram;
  9280. CheckSource('TestArray_StaticChar',
  9281. LinesToStr([ // statements
  9282. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9283. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9284. 'this.Arr3 = ["p", "a", "s"];',
  9285. 'this.Arr4 = ["p", "a", "s"];',
  9286. 'this.Arr5 = ["ä", "ö"];',
  9287. 'this.Arr6 = ["ä", "ö"];',
  9288. 'this.c = "";',
  9289. 'this.b = false;',
  9290. '']),
  9291. LinesToStr([ // $mod.$main
  9292. '$mod.c = "\x00";',
  9293. '$mod.c = "\uFFFF";',
  9294. '$mod.Arr[66] = "a";',
  9295. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9296. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9297. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9298. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9299. '$mod.c = "a";',
  9300. '$mod.c = "z";',
  9301. '$mod.Arr2[1] = "f";',
  9302. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9303. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9304. '']));
  9305. end;
  9306. procedure TTestModule.TestArray_StaticMultiDim;
  9307. begin
  9308. StartProgram(false);
  9309. Add([
  9310. 'type',
  9311. ' TArrayInt = array[1..3] of longint;',
  9312. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9313. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9314. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9315. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9316. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9317. 'var',
  9318. ' Arr: TArrayInt;',
  9319. ' Arr2: TArrayArrayInt;',
  9320. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9321. ' Arr4: TArrayArrayInt;',
  9322. ' ArrDim2: TArrayDim2Int;',
  9323. ' ArrDim3: TArrayDim3Int;',
  9324. ' ArrDim4: TArrayDim4Int;',
  9325. ' i: longint;',
  9326. 'begin',
  9327. ' i:=low(arr);',
  9328. ' i:=low(arr2);',
  9329. ' i:=low(arr2[5]);',
  9330. ' i:=high(arr);',
  9331. ' i:=high(arr2);',
  9332. ' i:=high(arr2[6]);',
  9333. ' arr2[5]:=arr;',
  9334. ' arr2[6][2]:=i;',
  9335. ' i:=arr2[6][3];',
  9336. ' arr2[6,3]:=i;',
  9337. ' i:=arr2[5,2];',
  9338. ' arr2:=arr2;',// clone multi dim static array
  9339. ' arr3:=arr3;',// clone anonymous multi dim static array
  9340. ' arr4:=arr4;',
  9341. ' Arr:=Arr;',
  9342. ' ArrDim2:=ArrDim2;',
  9343. ' ArrDim3:=ArrDim3;',
  9344. ' ArrDim4:=ArrDim4;',
  9345. '']);
  9346. ConvertProgram;
  9347. CheckSource('TestArray_StaticMultiDim',
  9348. LinesToStr([ // statements
  9349. 'this.TArrayArrayInt$clone = function (a) {',
  9350. ' var b = [];',
  9351. ' b.length = 2;',
  9352. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9353. ' return b;',
  9354. '};',
  9355. 'this.TArrayArrayArrayInt$clone = function (a) {',
  9356. ' var b = [];',
  9357. ' b.length = 2;',
  9358. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  9359. ' return b;',
  9360. '};',
  9361. 'this.TArrayDim2Int$clone = function (a) {',
  9362. ' var b = [];',
  9363. ' b.length = 2;',
  9364. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9365. ' return b;',
  9366. '};',
  9367. 'this.TArrayDim3Int$clone = function (a) {',
  9368. ' var b = [];',
  9369. ' b.length = 2;',
  9370. ' for (var c = 0; c < 2; c++) {',
  9371. ' var d = b[c] = [];',
  9372. ' d.length = 3;',
  9373. ' var e = a[c];',
  9374. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  9375. ' };',
  9376. ' return b;',
  9377. '};',
  9378. 'this.TArrayDim4Int$clone = function (a) {',
  9379. ' var b = [];',
  9380. ' b.length = 2;',
  9381. ' for (var c = 0; c < 2; c++) {',
  9382. ' var d = b[c] = [];',
  9383. ' d.length = 3;',
  9384. ' var e = a[c];',
  9385. ' for (var f = 0; f < 3; f++) {',
  9386. ' var g = d[f] = [];',
  9387. ' g.length = 4;',
  9388. ' var h = e[f];',
  9389. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  9390. ' };',
  9391. ' };',
  9392. ' return b;',
  9393. '};',
  9394. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9395. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9396. 'this.Arr3$a$clone = function (a) {',
  9397. ' var b = [];',
  9398. ' b.length = 2;',
  9399. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9400. ' return b;',
  9401. '};',
  9402. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9403. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  9404. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  9405. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  9406. 'this.ArrDim4 = rtl.arraySetLength(',
  9407. ' null,',
  9408. ' 0,',
  9409. ' 2,',
  9410. ' 3,',
  9411. ' 4,',
  9412. ' 5',
  9413. ');',
  9414. 'this.i = 0;'
  9415. ]),
  9416. LinesToStr([ // $mod.$main
  9417. '$mod.i = 1;',
  9418. '$mod.i = 5;',
  9419. '$mod.i = 1;',
  9420. '$mod.i = 3;',
  9421. '$mod.i = 6;',
  9422. '$mod.i = 3;',
  9423. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9424. '$mod.Arr2[1][1] = $mod.i;',
  9425. '$mod.i = $mod.Arr2[1][2];',
  9426. '$mod.Arr2[1][2] = $mod.i;',
  9427. '$mod.i = $mod.Arr2[0][1];',
  9428. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9429. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9430. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  9431. '$mod.Arr = $mod.Arr.slice(0);',
  9432. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  9433. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  9434. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  9435. '']));
  9436. end;
  9437. procedure TTestModule.TestArray_StaticInFunction;
  9438. begin
  9439. StartProgram(false);
  9440. Add([
  9441. 'const TArrayInt = 3;',
  9442. 'const TArrayArrayInt = 4;',
  9443. 'procedure DoIt;',
  9444. 'type',
  9445. ' TArrayInt = array[1..3] of longint;',
  9446. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9447. 'var',
  9448. ' Arr: TArrayInt;',
  9449. ' Arr2: TArrayArrayInt;',
  9450. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9451. ' i: longint;',
  9452. 'begin',
  9453. ' arr2[5]:=arr;',
  9454. ' arr2:=arr2;',// clone multi dim static array
  9455. ' arr3:=arr3;',// clone multi dim anonymous static array
  9456. 'end;',
  9457. 'begin',
  9458. '']);
  9459. ConvertProgram;
  9460. CheckSource('TestArray_StaticInFunction',
  9461. LinesToStr([ // statements
  9462. 'this.TArrayInt = 3;',
  9463. 'this.TArrayArrayInt = 4;',
  9464. 'var TArrayArrayInt$1$clone = function (a) {',
  9465. ' var b = [];',
  9466. ' b.length = 2;',
  9467. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9468. ' return b;',
  9469. '};',
  9470. 'var Arr3$a$clone = function (a) {',
  9471. ' var b = [];',
  9472. ' b.length = 2;',
  9473. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9474. ' return b;',
  9475. '};',
  9476. 'this.DoIt = function () {',
  9477. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9478. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9479. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9480. ' var i = 0;',
  9481. ' Arr2[0] = Arr.slice(0);',
  9482. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9483. ' Arr3 = Arr3$a$clone(Arr3);',
  9484. '};',
  9485. '']),
  9486. LinesToStr([ // $mod.$main
  9487. '']));
  9488. end;
  9489. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9490. begin
  9491. StartProgram(false);
  9492. Add([
  9493. 'type',
  9494. ' TArrayInt = array[1..3,1..2] of longint;',
  9495. 'var',
  9496. ' a,b: TArrayInt;',
  9497. 'begin',
  9498. ' if a=b then ;',
  9499. '']);
  9500. SetExpectedPasResolverError('compare static array is not supported',
  9501. nXIsNotSupported);
  9502. ConvertProgram;
  9503. end;
  9504. procedure TTestModule.TestArrayOfRecord;
  9505. begin
  9506. StartProgram(false);
  9507. Add([
  9508. 'type',
  9509. ' TRec = record',
  9510. ' Int: longint;',
  9511. ' end;',
  9512. ' TArrayRec = array of TRec;',
  9513. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9514. 'begin',
  9515. 'end;',
  9516. 'var',
  9517. ' Arr: TArrayRec;',
  9518. ' r: TRec;',
  9519. ' i: longint;',
  9520. 'begin',
  9521. ' SetLength(arr,3);',
  9522. ' arr[0].int:=4;',
  9523. ' arr[1].int:=length(arr)+arr[2].int;',
  9524. ' arr[arr[i].int].int:=arr[5].int;',
  9525. ' arr[7]:=r;',
  9526. ' r:=arr[8];',
  9527. ' i:=low(arr);',
  9528. ' i:=high(arr);',
  9529. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9530. ConvertProgram;
  9531. CheckSource('TestArrayOfRecord',
  9532. LinesToStr([ // statements
  9533. 'rtl.recNewT(this, "TRec", function () {',
  9534. ' this.Int = 0;',
  9535. ' this.$eq = function (b) {',
  9536. ' return this.Int === b.Int;',
  9537. ' };',
  9538. ' this.$assign = function (s) {',
  9539. ' this.Int = s.Int;',
  9540. ' return this;',
  9541. ' };',
  9542. '});',
  9543. 'this.DoIt = function (vd, vc, vv) {',
  9544. '};',
  9545. 'this.Arr = [];',
  9546. 'this.r = this.TRec.$new();',
  9547. 'this.i = 0;'
  9548. ]),
  9549. LinesToStr([ // $mod.$main
  9550. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9551. '$mod.Arr[0].Int = 4;',
  9552. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9553. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9554. '$mod.Arr[7].$assign($mod.r);',
  9555. '$mod.r.$assign($mod.Arr[8]);',
  9556. '$mod.i = 0;',
  9557. '$mod.i = rtl.length($mod.Arr)-1;',
  9558. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9559. '']));
  9560. end;
  9561. procedure TTestModule.TestArray_StaticRecord;
  9562. begin
  9563. StartProgram(false);
  9564. Add([
  9565. 'type',
  9566. ' TRec = record',
  9567. ' Int: longint;',
  9568. ' end;',
  9569. ' TArrayRec = array[1..2] of TRec;',
  9570. 'var',
  9571. ' Arr: TArrayRec;',
  9572. 'begin',
  9573. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9574. '']);
  9575. ConvertProgram;
  9576. CheckSource('TestArray_StaticRecord',
  9577. LinesToStr([ // statements
  9578. 'rtl.recNewT(this, "TRec", function () {',
  9579. ' this.Int = 0;',
  9580. ' this.$eq = function (b) {',
  9581. ' return this.Int === b.Int;',
  9582. ' };',
  9583. ' this.$assign = function (s) {',
  9584. ' this.Int = s.Int;',
  9585. ' return this;',
  9586. ' };',
  9587. '});',
  9588. 'this.TArrayRec$clone = function (a) {',
  9589. ' var b = [];',
  9590. ' b.length = 2;',
  9591. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  9592. ' return b;',
  9593. '};',
  9594. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9595. '']),
  9596. LinesToStr([ // $mod.$main
  9597. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9598. end;
  9599. procedure TTestModule.TestArrayOfSet;
  9600. begin
  9601. StartProgram(false);
  9602. Add([
  9603. 'type',
  9604. ' TFlag = (big,small);',
  9605. ' TSetOfFlag = set of tflag;',
  9606. ' TArrayFlag = array of TSetOfFlag;',
  9607. 'procedure DoIt(const a: Tarrayflag);',
  9608. 'begin',
  9609. 'end;',
  9610. 'var',
  9611. ' f: TFlag;',
  9612. ' s: TSetOfFlag;',
  9613. ' Arr: TArrayFlag;',
  9614. ' i: longint;',
  9615. 'begin',
  9616. ' SetLength(arr,3);',
  9617. ' arr[0]:=s;',
  9618. ' arr[1]:=[big];',
  9619. ' arr[2]:=[big]+s;',
  9620. ' arr[3]:=s+[big];',
  9621. ' arr[4]:=arr[5];',
  9622. ' s:=arr[6];',
  9623. ' i:=low(arr);',
  9624. ' i:=high(arr);',
  9625. ' DoIt(arr);',
  9626. ' DoIt([s]);',
  9627. ' DoIt([[],s]);',
  9628. ' DoIt([s,[]]);',
  9629. '']);
  9630. ConvertProgram;
  9631. CheckSource('TestArrayOfSet',
  9632. LinesToStr([ // statements
  9633. 'this.TFlag = {',
  9634. ' "0": "big",',
  9635. ' big: 0,',
  9636. ' "1": "small",',
  9637. ' small: 1',
  9638. '};',
  9639. 'this.DoIt = function (a) {',
  9640. '};',
  9641. 'this.f = 0;',
  9642. 'this.s = {};',
  9643. 'this.Arr = [];',
  9644. 'this.i = 0;',
  9645. '']),
  9646. LinesToStr([ // $mod.$main
  9647. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9648. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9649. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9650. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9651. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9652. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9653. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9654. '$mod.i = 0;',
  9655. '$mod.i = rtl.length($mod.Arr) - 1;',
  9656. '$mod.DoIt($mod.Arr);',
  9657. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9658. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9659. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9660. '']));
  9661. end;
  9662. procedure TTestModule.TestArray_DynAsParam;
  9663. begin
  9664. StartProgram(false);
  9665. Add([
  9666. 'type integer = longint;',
  9667. 'type TArrInt = array of integer;',
  9668. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9669. 'var vJ: TArrInt;',
  9670. 'begin',
  9671. ' vg:=vg;',
  9672. ' vj:=vh;',
  9673. ' vi:=vi;',
  9674. ' doit(vg,vg,vg);',
  9675. ' doit(vh,vh,vj);',
  9676. ' doit(vi,vi,vi);',
  9677. ' doit(vj,vj,vj);',
  9678. 'end;',
  9679. 'var i: TArrInt;',
  9680. 'begin',
  9681. ' doit(i,i,i);']);
  9682. ConvertProgram;
  9683. CheckSource('TestArray_DynAsParams',
  9684. LinesToStr([ // statements
  9685. 'this.DoIt = function (vG,vH,vI) {',
  9686. ' var vJ = [];',
  9687. ' vG = rtl.arrayRef(vG);',
  9688. ' vJ = rtl.arrayRef(vH);',
  9689. ' vI.set(rtl.arrayRef(vI.get()));',
  9690. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9691. ' get: function () {',
  9692. ' return vG;',
  9693. ' },',
  9694. ' set: function (v) {',
  9695. ' vG = v;',
  9696. ' }',
  9697. ' });',
  9698. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9699. ' get: function () {',
  9700. ' return vJ;',
  9701. ' },',
  9702. ' set: function (v) {',
  9703. ' vJ = v;',
  9704. ' }',
  9705. ' });',
  9706. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9707. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9708. ' get: function () {',
  9709. ' return vJ;',
  9710. ' },',
  9711. ' set: function (v) {',
  9712. ' vJ = v;',
  9713. ' }',
  9714. ' });',
  9715. '};',
  9716. 'this.i = [];'
  9717. ]),
  9718. LinesToStr([
  9719. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9720. ' p: $mod,',
  9721. ' get: function () {',
  9722. ' return this.p.i;',
  9723. ' },',
  9724. ' set: function (v) {',
  9725. ' this.p.i = v;',
  9726. ' }',
  9727. '});'
  9728. ]));
  9729. end;
  9730. procedure TTestModule.TestArray_StaticAsParam;
  9731. begin
  9732. StartProgram(false);
  9733. Add([
  9734. 'type integer = longint;',
  9735. 'type TArrInt = array[1..2] of integer;',
  9736. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9737. 'var vJ: TArrInt;',
  9738. 'begin',
  9739. ' vg:=vg;',
  9740. ' vj:=vh;',
  9741. ' vi:=vi;',
  9742. ' doit(vg,vg,vg);',
  9743. ' doit(vh,vh,vj);',
  9744. ' doit(vi,vi,vi);',
  9745. ' doit(vj,vj,vj);',
  9746. 'end;',
  9747. 'var i: TArrInt;',
  9748. 'begin',
  9749. ' doit(i,i,i);']);
  9750. ConvertProgram;
  9751. CheckSource('TestArray_StaticAsParams',
  9752. LinesToStr([ // statements
  9753. 'this.DoIt = function (vG,vH,vI) {',
  9754. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9755. ' vG = vG.slice(0);',
  9756. ' vJ = vH.slice(0);',
  9757. ' vI.set(vI.get().slice(0));',
  9758. ' $mod.DoIt(vG.slice(0), vG, {',
  9759. ' get: function () {',
  9760. ' return vG;',
  9761. ' },',
  9762. ' set: function (v) {',
  9763. ' vG = v;',
  9764. ' }',
  9765. ' });',
  9766. ' $mod.DoIt(vH.slice(0), vH, {',
  9767. ' get: function () {',
  9768. ' return vJ;',
  9769. ' },',
  9770. ' set: function (v) {',
  9771. ' vJ = v;',
  9772. ' }',
  9773. ' });',
  9774. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9775. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9776. ' get: function () {',
  9777. ' return vJ;',
  9778. ' },',
  9779. ' set: function (v) {',
  9780. ' vJ = v;',
  9781. ' }',
  9782. ' });',
  9783. '};',
  9784. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9785. ]),
  9786. LinesToStr([
  9787. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9788. ' p: $mod,',
  9789. ' get: function () {',
  9790. ' return this.p.i;',
  9791. ' },',
  9792. ' set: function (v) {',
  9793. ' this.p.i = v;',
  9794. ' }',
  9795. '});'
  9796. ]));
  9797. end;
  9798. procedure TTestModule.TestArrayElement_AsParams;
  9799. begin
  9800. StartProgram(false);
  9801. Add('type integer = longint;');
  9802. Add('type TArrayInt = array of integer;');
  9803. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9804. Add('var vJ: tarrayint;');
  9805. Add('begin');
  9806. Add(' vi:=vi;');
  9807. Add(' doit(vi,vi,vi);');
  9808. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9809. Add('end;');
  9810. Add('var a: TArrayInt;');
  9811. Add('begin');
  9812. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9813. ConvertProgram;
  9814. CheckSource('TestArrayElement_AsParams',
  9815. LinesToStr([ // statements
  9816. 'this.DoIt = function (vG,vH,vI) {',
  9817. ' var vJ = [];',
  9818. ' vI.set(vI.get());',
  9819. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9820. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9821. ' a:1+3,',
  9822. ' p:vJ,',
  9823. ' get: function () {',
  9824. ' return this.p[this.a];',
  9825. ' },',
  9826. ' set: function (v) {',
  9827. ' this.p[this.a] = v;',
  9828. ' }',
  9829. ' });',
  9830. '};',
  9831. 'this.a = [];'
  9832. ]),
  9833. LinesToStr([
  9834. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9835. ' a: 1+6,',
  9836. ' p: $mod.a,',
  9837. ' get: function () {',
  9838. ' return this.p[this.a];',
  9839. ' },',
  9840. ' set: function (v) {',
  9841. ' this.p[this.a] = v;',
  9842. ' }',
  9843. '});'
  9844. ]));
  9845. end;
  9846. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9847. begin
  9848. StartProgram(false);
  9849. Add('type Integer = longint;');
  9850. Add('type TArrayInt = array of integer;');
  9851. Add('function GetArr(vB: integer = 0): tarrayint;');
  9852. Add('begin');
  9853. Add('end;');
  9854. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9855. Add('begin');
  9856. Add('end;');
  9857. Add('begin');
  9858. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9859. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9860. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9861. ConvertProgram;
  9862. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9863. LinesToStr([ // statements
  9864. 'this.GetArr = function (vB) {',
  9865. ' var Result = [];',
  9866. ' return Result;',
  9867. '};',
  9868. 'this.DoIt = function (vG,vH,vI) {',
  9869. '};'
  9870. ]),
  9871. LinesToStr([
  9872. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9873. ' a: 1+3,',
  9874. ' p: $mod.GetArr(0),',
  9875. ' get: function () {',
  9876. ' return this.p[this.a];',
  9877. ' },',
  9878. ' set: function (v) {',
  9879. ' this.p[this.a] = v;',
  9880. ' }',
  9881. '});',
  9882. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9883. ' a: 2+3,',
  9884. ' p: $mod.GetArr(0),',
  9885. ' get: function () {',
  9886. ' return this.p[this.a];',
  9887. ' },',
  9888. ' set: function (v) {',
  9889. ' this.p[this.a] = v;',
  9890. ' }',
  9891. '});',
  9892. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9893. ' a: 3+3,',
  9894. ' p: $mod.GetArr(9),',
  9895. ' get: function () {',
  9896. ' return this.p[this.a];',
  9897. ' },',
  9898. ' set: function (v) {',
  9899. ' this.p[this.a] = v;',
  9900. ' }',
  9901. '});',
  9902. '']));
  9903. end;
  9904. procedure TTestModule.TestArrayEnumTypeRange;
  9905. begin
  9906. StartProgram(false);
  9907. Add([
  9908. 'type',
  9909. ' TEnum = (red,blue);',
  9910. ' TEnumArray = array[TEnum] of longint;',
  9911. 'var',
  9912. ' e: TEnum;',
  9913. ' i: longint;',
  9914. ' a: TEnumArray;',
  9915. ' numbers: TEnumArray = (1,2);',
  9916. ' names: array[TEnum] of string = (''red'',''blue'');',
  9917. 'begin',
  9918. ' e:=low(a);',
  9919. ' e:=high(a);',
  9920. ' i:=a[red];',
  9921. ' a[e]:=a[e];']);
  9922. ConvertProgram;
  9923. CheckSource('TestArrayEnumTypeRange',
  9924. LinesToStr([ // statements
  9925. ' this.TEnum = {',
  9926. ' "0": "red",',
  9927. ' red: 0,',
  9928. ' "1": "blue",',
  9929. ' blue: 1',
  9930. '};',
  9931. 'this.e = 0;',
  9932. 'this.i = 0;',
  9933. 'this.a = rtl.arraySetLength(null,0,2);',
  9934. 'this.numbers = [1, 2];',
  9935. 'this.names = ["red", "blue"];',
  9936. '']),
  9937. LinesToStr([ // $mod.$main
  9938. '$mod.e = $mod.TEnum.red;',
  9939. '$mod.e = $mod.TEnum.blue;',
  9940. '$mod.i = $mod.a[$mod.TEnum.red];',
  9941. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9942. '']));
  9943. end;
  9944. procedure TTestModule.TestArray_SetLengthOutArg;
  9945. begin
  9946. StartProgram(false);
  9947. Add([
  9948. 'type TArrInt = array of longint;',
  9949. 'procedure DoIt(out a: TArrInt);',
  9950. 'begin',
  9951. ' SetLength(a,2);',
  9952. 'end;',
  9953. 'begin',
  9954. '']);
  9955. ConvertProgram;
  9956. CheckSource('TestArray_SetLengthOutArg',
  9957. LinesToStr([ // statements
  9958. 'this.DoIt = function (a) {',
  9959. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9960. '};',
  9961. '']),
  9962. LinesToStr([
  9963. '']));
  9964. end;
  9965. procedure TTestModule.TestArray_SetLengthProperty;
  9966. begin
  9967. StartProgram(false);
  9968. Add('type');
  9969. Add(' TArrInt = array of longint;');
  9970. Add(' TObject = class');
  9971. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9972. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9973. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9974. Add(' end;');
  9975. Add('var Obj: TObject;');
  9976. Add('begin');
  9977. Add(' SetLength(Obj.Colors,2);');
  9978. ConvertProgram;
  9979. CheckSource('TestArray_SetLengthProperty',
  9980. LinesToStr([ // statements
  9981. 'rtl.createClass(this, "TObject", null, function () {',
  9982. ' this.$init = function () {',
  9983. ' };',
  9984. ' this.$final = function () {',
  9985. ' };',
  9986. '});',
  9987. 'this.Obj = null;',
  9988. '']),
  9989. LinesToStr([
  9990. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9991. '']));
  9992. end;
  9993. procedure TTestModule.TestArray_SetLengthMultiDim;
  9994. begin
  9995. StartProgram(false);
  9996. Add([
  9997. 'type',
  9998. ' TArrArrInt = array of array of longint;',
  9999. ' TArrStaInt = array of array[1..2] of longint;',
  10000. 'var',
  10001. ' a: TArrArrInt;',
  10002. ' b: TArrStaInt;',
  10003. 'begin',
  10004. ' SetLength(a,2);',
  10005. ' SetLength(a,3,4);',
  10006. ' SetLength(b,5);',
  10007. '']);
  10008. ConvertProgram;
  10009. CheckSource('TestArray_SetLengthMultiDim',
  10010. LinesToStr([ // statements
  10011. 'this.a = [];',
  10012. 'this.b = [];',
  10013. '']),
  10014. LinesToStr([
  10015. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10016. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10017. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10018. '']));
  10019. end;
  10020. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10021. begin
  10022. StartProgram(false);
  10023. Add([
  10024. 'type',
  10025. ' TStaArr1 = array[1..3] of boolean;',
  10026. //' TStaArr2 = array[5..6] of TStaArr1;',
  10027. ' TDynArr1StaArr1 = array of TStaArr1;',
  10028. //' TDynArr1StaArr2 = array of TStaArr2;',
  10029. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10030. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10031. 'var',
  10032. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10033. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10034. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10035. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10036. 'begin',
  10037. ' SetLength(DynArr1StaArr1,11);',
  10038. ' SetLength(DynArr2StaArr1,12);',
  10039. ' SetLength(DynArr2StaArr1[13],14);',
  10040. ' SetLength(DynArr2StaArr1,15,16);',
  10041. //' SetLength(DynArr1StaArr2,21);',
  10042. //' SetLength(DynArr2StaArr2,22);',
  10043. //' SetLength(DynArr2StaArr2[23],24);',
  10044. //' SetLength(DynArr2StaArr2,25,26);',
  10045. '']);
  10046. ConvertProgram;
  10047. CheckSource('TestArray_DynOfStatic',
  10048. LinesToStr([ // statements
  10049. 'this.DynArr1StaArr1 = [];',
  10050. 'this.DynArr2StaArr1 = [];',
  10051. '']),
  10052. LinesToStr([ // $mod.$main
  10053. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10054. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10055. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10056. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10057. ' $mod.DynArr2StaArr1,',
  10058. ' false,',
  10059. ' 15,',
  10060. ' 16,',
  10061. ' "s",',
  10062. ' 3',
  10063. ');',
  10064. '']));
  10065. end;
  10066. procedure TTestModule.TestArray_OpenArrayOfString;
  10067. begin
  10068. StartProgram(false);
  10069. Add('procedure DoIt(const a: array of String);');
  10070. Add('var');
  10071. Add(' i: longint;');
  10072. Add(' s: string;');
  10073. Add('begin');
  10074. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10075. Add('end;');
  10076. Add('var s: string;');
  10077. Add('begin');
  10078. Add(' DoIt([]);');
  10079. Add(' DoIt([s,''foo'','''',s+s]);');
  10080. ConvertProgram;
  10081. CheckSource('TestArray_OpenArrayOfString',
  10082. LinesToStr([ // statements
  10083. 'this.DoIt = function (a) {',
  10084. ' var i = 0;',
  10085. ' var s = "";',
  10086. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10087. ' i = $l;',
  10088. ' s = a[rtl.length(a) - i - 1];',
  10089. ' };',
  10090. '};',
  10091. 'this.s = "";',
  10092. '']),
  10093. LinesToStr([
  10094. '$mod.DoIt([]);',
  10095. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10096. '']));
  10097. end;
  10098. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10099. begin
  10100. StartProgram(false);
  10101. Add([
  10102. 'type TArr = array of char;',
  10103. 'var',
  10104. ' c: char;',
  10105. ' s: string;',
  10106. ' a: TArr;',
  10107. 'procedure Run(const a: array of char);',
  10108. 'begin',
  10109. ' Run(c);',
  10110. ' Run(s);',
  10111. 'end;',
  10112. 'begin',
  10113. ' a:=c;',
  10114. ' a:=s;',
  10115. ' a:=#13;',
  10116. ' a:=''Foo'';',
  10117. ' Run(c);',
  10118. ' Run(s);',
  10119. '']);
  10120. ConvertProgram;
  10121. CheckSource('TestArray_ArrayOfCharAssignString',
  10122. LinesToStr([ // statements
  10123. 'this.c = "";',
  10124. 'this.s = "";',
  10125. 'this.a = [];',
  10126. 'this.Run = function (a) {',
  10127. ' $mod.Run($mod.c.split(""));',
  10128. ' $mod.Run($mod.s.split(""));',
  10129. '};',
  10130. '']),
  10131. LinesToStr([
  10132. '$mod.a = $mod.c.split("");',
  10133. '$mod.a = $mod.s.split("");',
  10134. '$mod.a = "\r".split("");',
  10135. '$mod.a = "Foo".split("");',
  10136. '$mod.Run($mod.c.split(""));',
  10137. '$mod.Run($mod.s.split(""));',
  10138. '']));
  10139. end;
  10140. procedure TTestModule.TestArray_ConstRef;
  10141. begin
  10142. StartProgram(false);
  10143. Add([
  10144. 'type TArr = array of word;',
  10145. 'procedure Run(constref a: TArr);',
  10146. 'begin',
  10147. 'end;',
  10148. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10149. 'var l: TArr;',
  10150. 'begin',
  10151. ' Run(l);',
  10152. ' Run(a);',
  10153. ' Run(b);',
  10154. ' Run(c);',
  10155. ' Run(d);',
  10156. ' Run(e);',
  10157. 'end;',
  10158. 'begin',
  10159. '']);
  10160. ConvertProgram;
  10161. CheckResolverUnexpectedHints();
  10162. CheckSource('TestArray_ConstRef',
  10163. LinesToStr([ // statements
  10164. 'this.Run = function (a) {',
  10165. '};',
  10166. 'this.Fly = function (a, b, c, d, e) {',
  10167. ' var l = [];',
  10168. ' $mod.Run(l);',
  10169. ' $mod.Run(a);',
  10170. ' $mod.Run(b.get());',
  10171. ' $mod.Run(c.get());',
  10172. ' $mod.Run(d);',
  10173. ' $mod.Run(e);',
  10174. '};',
  10175. '']),
  10176. LinesToStr([
  10177. '']));
  10178. end;
  10179. procedure TTestModule.TestArray_Concat;
  10180. begin
  10181. StartProgram(false);
  10182. Add([
  10183. 'type',
  10184. ' integer = longint;',
  10185. ' TFlag = (big,small);',
  10186. ' TFlags = set of TFlag;',
  10187. ' TRec = record',
  10188. ' i: integer;',
  10189. ' end;',
  10190. ' TArrInt = array of integer;',
  10191. ' TArrRec = array of TRec;',
  10192. ' TArrFlag = array of TFlag;',
  10193. ' TArrSet = array of TFlags;',
  10194. ' TArrJSValue = array of jsvalue;',
  10195. 'var',
  10196. ' ArrInt: tarrint;',
  10197. ' ArrRec: tarrrec;',
  10198. ' ArrFlag: tarrflag;',
  10199. ' ArrSet: tarrset;',
  10200. ' ArrJSValue: tarrjsvalue;',
  10201. 'begin',
  10202. ' arrint:=concat(arrint);',
  10203. ' arrint:=concat(arrint,arrint);',
  10204. ' arrint:=concat(arrint,arrint,arrint);',
  10205. ' arrrec:=concat(arrrec);',
  10206. ' arrrec:=concat(arrrec,arrrec);',
  10207. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10208. ' arrset:=concat(arrset);',
  10209. ' arrset:=concat(arrset,arrset);',
  10210. ' arrset:=concat(arrset,arrset,arrset);',
  10211. ' arrjsvalue:=concat(arrjsvalue);',
  10212. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10213. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10214. ' arrint:=concat([1],arrint);',
  10215. ' arrflag:=concat([big]);',
  10216. ' arrflag:=concat([big],arrflag);',
  10217. ' arrflag:=concat(arrflag,[small]);',
  10218. '']);
  10219. ConvertProgram;
  10220. CheckSource('TestArray_Concat',
  10221. LinesToStr([ // statements
  10222. 'this.TFlag = {',
  10223. ' "0": "big",',
  10224. ' big: 0,',
  10225. ' "1": "small",',
  10226. ' small: 1',
  10227. '};',
  10228. 'rtl.recNewT(this, "TRec", function () {',
  10229. ' this.i = 0;',
  10230. ' this.$eq = function (b) {',
  10231. ' return this.i === b.i;',
  10232. ' };',
  10233. ' this.$assign = function (s) {',
  10234. ' this.i = s.i;',
  10235. ' return this;',
  10236. ' };',
  10237. '});',
  10238. 'this.ArrInt = [];',
  10239. 'this.ArrRec = [];',
  10240. 'this.ArrFlag = [];',
  10241. 'this.ArrSet = [];',
  10242. 'this.ArrJSValue = [];',
  10243. '']),
  10244. LinesToStr([ // $mod.$main
  10245. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10246. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10247. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10248. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10249. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10250. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10251. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10252. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10253. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10254. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10255. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10256. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10257. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10258. '$mod.ArrFlag = [$mod.TFlag.big];',
  10259. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10260. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10261. '']));
  10262. end;
  10263. procedure TTestModule.TestArray_Copy;
  10264. begin
  10265. StartProgram(false);
  10266. Add([
  10267. 'type',
  10268. ' integer = longint;',
  10269. ' TFlag = (big,small);',
  10270. ' TFlags = set of TFlag;',
  10271. ' TRec = record',
  10272. ' i: integer;',
  10273. ' end;',
  10274. ' TArrInt = array of integer;',
  10275. ' TArrRec = array of TRec;',
  10276. ' TArrSet = array of TFlags;',
  10277. ' TArrJSValue = array of jsvalue;',
  10278. 'var',
  10279. ' ArrInt: tarrint;',
  10280. ' ArrRec: tarrrec;',
  10281. ' ArrSet: tarrset;',
  10282. ' ArrJSValue: tarrjsvalue;',
  10283. 'begin',
  10284. ' arrint:=copy(arrint);',
  10285. ' arrint:=copy(arrint,2);',
  10286. ' arrint:=copy(arrint,3,4);',
  10287. ' arrint:=copy([1,1],1,2);',
  10288. ' arrrec:=copy(arrrec);',
  10289. ' arrrec:=copy(arrrec,5);',
  10290. ' arrrec:=copy(arrrec,6,7);',
  10291. ' arrset:=copy(arrset);',
  10292. ' arrset:=copy(arrset,8);',
  10293. ' arrset:=copy(arrset,9,10);',
  10294. ' arrjsvalue:=copy(arrjsvalue);',
  10295. ' arrjsvalue:=copy(arrjsvalue,11);',
  10296. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10297. ' ']);
  10298. ConvertProgram;
  10299. CheckSource('TestArray_Copy',
  10300. LinesToStr([ // statements
  10301. 'this.TFlag = {',
  10302. ' "0": "big",',
  10303. ' big: 0,',
  10304. ' "1": "small",',
  10305. ' small: 1',
  10306. '};',
  10307. 'rtl.recNewT(this, "TRec", function () {',
  10308. ' this.i = 0;',
  10309. ' this.$eq = function (b) {',
  10310. ' return this.i === b.i;',
  10311. ' };',
  10312. ' this.$assign = function (s) {',
  10313. ' this.i = s.i;',
  10314. ' return this;',
  10315. ' };',
  10316. '});',
  10317. 'this.ArrInt = [];',
  10318. 'this.ArrRec = [];',
  10319. 'this.ArrSet = [];',
  10320. 'this.ArrJSValue = [];',
  10321. '']),
  10322. LinesToStr([ // $mod.$main
  10323. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10324. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10325. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10326. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10327. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10328. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10329. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10330. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10331. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10332. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10333. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10334. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10335. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10336. '']));
  10337. end;
  10338. procedure TTestModule.TestArray_InsertDelete;
  10339. begin
  10340. StartProgram(false);
  10341. Add([
  10342. 'type',
  10343. ' integer = longint;',
  10344. ' TFlag = (big,small);',
  10345. ' TFlags = set of TFlag;',
  10346. ' TRec = record',
  10347. ' i: integer;',
  10348. ' end;',
  10349. ' TArrInt = array of integer;',
  10350. ' TArrRec = array of TRec;',
  10351. ' TArrSet = array of TFlags;',
  10352. ' TArrJSValue = array of jsvalue;',
  10353. ' TArrArrInt = array of TArrInt;',
  10354. 'var',
  10355. ' ArrInt: tarrint;',
  10356. ' ArrRec: tarrrec;',
  10357. ' ArrSet: tarrset;',
  10358. ' ArrJSValue: tarrjsvalue;',
  10359. ' ArrArrInt: TArrArrInt;',
  10360. 'begin',
  10361. ' Insert(1,arrint,2);',
  10362. ' Insert(arrint[3],arrint,4);',
  10363. ' Insert(arrrec[5],arrrec,6);',
  10364. ' Insert(arrset[7],arrset,7);',
  10365. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10366. ' Insert(10,arrjsvalue,11);',
  10367. ' Insert([23],arrarrint,22);',
  10368. ' Delete(arrint,12,13);',
  10369. ' Delete(arrrec,14,15);',
  10370. ' Delete(arrset,17,18);',
  10371. ' Delete(arrjsvalue,19,10);']);
  10372. ConvertProgram;
  10373. CheckSource('TestArray_InsertDelete',
  10374. LinesToStr([ // statements
  10375. 'this.TFlag = {',
  10376. ' "0": "big",',
  10377. ' big: 0,',
  10378. ' "1": "small",',
  10379. ' small: 1',
  10380. '};',
  10381. 'rtl.recNewT(this, "TRec", function () {',
  10382. ' this.i = 0;',
  10383. ' this.$eq = function (b) {',
  10384. ' return this.i === b.i;',
  10385. ' };',
  10386. ' this.$assign = function (s) {',
  10387. ' this.i = s.i;',
  10388. ' return this;',
  10389. ' };',
  10390. '});',
  10391. 'this.ArrInt = [];',
  10392. 'this.ArrRec = [];',
  10393. 'this.ArrSet = [];',
  10394. 'this.ArrJSValue = [];',
  10395. 'this.ArrArrInt = [];',
  10396. '']),
  10397. LinesToStr([ // $mod.$main
  10398. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10399. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10400. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10401. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10402. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10403. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10404. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10405. '$mod.ArrInt.splice(12, 13);',
  10406. '$mod.ArrRec.splice(14, 15);',
  10407. '$mod.ArrSet.splice(17, 18);',
  10408. '$mod.ArrJSValue.splice(19, 10);',
  10409. '']));
  10410. end;
  10411. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10412. begin
  10413. Parser.Options:=Parser.Options+[po_cassignments];
  10414. StartProgram(false);
  10415. Add([
  10416. '{$modeswitch arrayoperators}',
  10417. 'type',
  10418. ' integer = longint;',
  10419. ' TArrInt = array of integer;',
  10420. ' TArrStr = array of string;',
  10421. 'const',
  10422. ' Ints: TArrInt = (1,2,3);',
  10423. ' Aliases: TarrStr = (''foo'',''b'');',
  10424. ' OneInt: TArrInt = (7);',
  10425. ' OneStr: array of integer = (7);',
  10426. ' Chars: array of char = ''aoc'';',
  10427. ' Names: array of string = (''a'',''foo'');',
  10428. ' NameCount = low(Names)+high(Names)+length(Names);',
  10429. 'var i: integer;',
  10430. 'begin',
  10431. ' Ints:=[];',
  10432. ' Ints:=[1,1];',
  10433. ' Ints:=[1]+[2];',
  10434. ' Ints:=[2];',
  10435. ' Ints:=[]+ints;',
  10436. ' Ints:=Ints+[];',
  10437. ' Ints:=Ints+OneInt;',
  10438. ' Ints:=Ints+[1,1];',
  10439. ' Ints:=[i,i]+Ints;',
  10440. ' Ints:=[1]+[i]+[3];',
  10441. '']);
  10442. ConvertProgram;
  10443. CheckSource('TestArray_DynArrayConstObjFPC',
  10444. LinesToStr([ // statements
  10445. 'this.Ints = [1, 2, 3];',
  10446. 'this.Aliases = ["foo", "b"];',
  10447. 'this.OneInt = [7];',
  10448. 'this.OneStr = [7];',
  10449. 'this.Chars = ["a", "o", "c"];',
  10450. 'this.Names = ["a", "foo"];',
  10451. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10452. 'this.i = 0;',
  10453. '']),
  10454. LinesToStr([ // $mod.$main
  10455. '$mod.Ints = [];',
  10456. '$mod.Ints = [1, 1];',
  10457. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10458. '$mod.Ints = [2];',
  10459. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10460. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10461. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10462. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10463. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10464. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10465. '']));
  10466. end;
  10467. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10468. begin
  10469. StartProgram(false);
  10470. // Note: const c = [1,1]; defines a set!
  10471. Add([
  10472. '{$mode delphi}',
  10473. 'type',
  10474. ' integer = longint;',
  10475. ' TArrInt = array of integer;',
  10476. ' TArrStr = array of string;',
  10477. 'const',
  10478. ' Ints: TArrInt = [1,1,2];',
  10479. ' Aliases: TarrStr = [''foo'',''b''];',
  10480. ' OneInt: TArrInt = [7];',
  10481. ' OneStr: array of integer = [7]+[8];',
  10482. ' Chars: array of char = ''aoc'';',
  10483. ' Names: array of string = [''a'',''a''];',
  10484. ' NameCount = low(Names)+high(Names)+length(Names);',
  10485. 'begin',
  10486. '']);
  10487. ConvertProgram;
  10488. CheckSource('TestArray_DynArrayConstDelphi',
  10489. LinesToStr([ // statements
  10490. 'this.Ints = [1, 1, 2];',
  10491. 'this.Aliases = ["foo", "b"];',
  10492. 'this.OneInt = [7];',
  10493. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10494. 'this.Chars = ["a", "o", "c"];',
  10495. 'this.Names = ["a", "a"];',
  10496. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10497. '']),
  10498. LinesToStr([ // $mod.$main
  10499. '']));
  10500. end;
  10501. procedure TTestModule.TestArray_ArrayLitAsParam;
  10502. begin
  10503. StartProgram(false);
  10504. Add([
  10505. '{$modeswitch arrayoperators}',
  10506. 'type',
  10507. ' integer = longint;',
  10508. ' TArrInt = array of integer;',
  10509. ' TArrSet = array of (red,green,blue);',
  10510. 'procedure DoOpenInt(const a: array of integer); forward;',
  10511. 'procedure DoInt(const a: TArrInt);',
  10512. 'begin',
  10513. ' DoInt(a+[1]);',
  10514. ' DoInt([1]+a);',
  10515. ' DoOpenInt(a);',
  10516. ' DoOpenInt(a+[1]);',
  10517. ' DoOpenInt([1]+a);',
  10518. 'end;',
  10519. 'procedure DoOpenInt(const a: array of integer);',
  10520. 'begin',
  10521. ' DoOpenInt(a+[1]);',
  10522. ' DoOpenInt([1]+a);',
  10523. ' DoInt(a);',
  10524. ' DoInt(a+[1]);',
  10525. ' DoInt([1]+a);',
  10526. 'end;',
  10527. 'procedure DoSet(const a: TArrSet);',
  10528. 'begin',
  10529. ' DoSet(a+[red]);',
  10530. ' DoSet([blue]+a);',
  10531. 'end;',
  10532. 'var',
  10533. ' i: TArrInt;',
  10534. ' s: TArrSet;',
  10535. 'begin',
  10536. ' DoInt([1]);',
  10537. ' DoInt([1]+[2]);',
  10538. ' DoInt(i+[1]);',
  10539. ' DoInt([1]+i);',
  10540. ' DoOpenInt([1]);',
  10541. ' DoOpenInt([1]+[2]);',
  10542. ' DoOpenInt(i+[1]);',
  10543. ' DoOpenInt([1]+i);',
  10544. ' DoSet([red]);',
  10545. ' DoSet([blue]+[green]);',
  10546. ' DoSet(s+[blue]);',
  10547. ' DoSet([red]+s);',
  10548. '']);
  10549. ConvertProgram;
  10550. CheckSource('TestArray_ArrayLitAsParam',
  10551. LinesToStr([ // statements
  10552. 'this.TArrSet$a = {',
  10553. ' "0": "red",',
  10554. ' red: 0,',
  10555. ' "1": "green",',
  10556. ' green: 1,',
  10557. ' "2": "blue",',
  10558. ' blue: 2',
  10559. '};',
  10560. 'this.DoInt = function (a) {',
  10561. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10562. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10563. ' $mod.DoOpenInt(a);',
  10564. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10565. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10566. '};',
  10567. 'this.DoOpenInt = function (a) {',
  10568. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10569. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10570. ' $mod.DoInt(a);',
  10571. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10572. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10573. '};',
  10574. 'this.DoSet = function (a) {',
  10575. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10576. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10577. '};',
  10578. 'this.i = [];',
  10579. 'this.s = [];',
  10580. '']),
  10581. LinesToStr([ // $mod.$main
  10582. '$mod.DoInt([1]);',
  10583. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10584. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10585. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10586. '$mod.DoOpenInt([1]);',
  10587. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10588. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10589. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10590. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10591. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10592. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10593. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10594. '']));
  10595. end;
  10596. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10597. begin
  10598. StartProgram(false);
  10599. Add([
  10600. '{$modeswitch arrayoperators}',
  10601. 'type',
  10602. ' integer = longint;',
  10603. ' TArrInt = array of integer;',
  10604. ' TArrArrInt = array of TArrInt;',
  10605. 'procedure DoInt(const a: TArrArrInt);',
  10606. 'begin',
  10607. ' DoInt(a+[[1]]);',
  10608. ' DoInt([[1]]+a);',
  10609. ' DoInt(a);',
  10610. 'end;',
  10611. 'var',
  10612. ' i: TArrInt;',
  10613. ' a: TArrArrInt;',
  10614. 'begin',
  10615. ' a:=[[1]];',
  10616. ' a:=[i];',
  10617. ' a:=a+[i];',
  10618. ' a:=[i]+a;',
  10619. ' a:=[[1]+i];',
  10620. ' a:=[[1]+[2]];',
  10621. ' a:=[i+[2]];',
  10622. ' DoInt([[1]]);',
  10623. ' DoInt([[1]+[2],[3,4],[5]]);',
  10624. ' DoInt([i+[1]]+a);',
  10625. ' DoInt([i]+a);',
  10626. '']);
  10627. ConvertProgram;
  10628. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10629. LinesToStr([ // statements
  10630. 'this.DoInt = function (a) {',
  10631. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10632. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10633. ' $mod.DoInt(a);',
  10634. '};',
  10635. 'this.i = [];',
  10636. 'this.a = [];',
  10637. '']),
  10638. LinesToStr([ // $mod.$main
  10639. '$mod.a = [[1]];',
  10640. '$mod.a = [$mod.i];',
  10641. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10642. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10643. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10644. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10645. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10646. '$mod.DoInt([[1]]);',
  10647. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10648. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10649. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10650. '']));
  10651. end;
  10652. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10653. begin
  10654. StartProgram(false);
  10655. Add([
  10656. '{$modeswitch arrayoperators}',
  10657. 'type',
  10658. ' integer = longint;',
  10659. ' TArrInt = array[1..2] of integer;',
  10660. ' TArrArrInt = array of TArrInt;',
  10661. 'procedure DoInt(const a: TArrArrInt);',
  10662. 'begin',
  10663. ' DoInt(a+[[1,2]]);',
  10664. ' DoInt([[1,2]]+a);',
  10665. ' DoInt(a);',
  10666. 'end;',
  10667. 'var',
  10668. ' i: TArrInt;',
  10669. ' a: TArrArrInt;',
  10670. 'begin',
  10671. ' a:=[[1,1]];',
  10672. ' a:=[i];',
  10673. ' a:=a+[i];',
  10674. ' a:=[i]+a;',
  10675. ' DoInt([[1,1]]);',
  10676. ' DoInt([[1,2],[3,4]]);',
  10677. '']);
  10678. ConvertProgram;
  10679. CheckSource('TestArray_ArrayLitStaticAsParam',
  10680. LinesToStr([ // statements
  10681. 'this.DoInt = function (a) {',
  10682. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10683. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10684. ' $mod.DoInt(a);',
  10685. '};',
  10686. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10687. 'this.a = [];',
  10688. '']),
  10689. LinesToStr([ // $mod.$main
  10690. '$mod.a = [[1, 1]];',
  10691. '$mod.a = [$mod.i.slice(0)];',
  10692. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10693. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10694. '$mod.DoInt([[1, 1]]);',
  10695. '$mod.DoInt([[1, 2], [3, 4]]);',
  10696. '']));
  10697. end;
  10698. procedure TTestModule.TestArray_ForInArrOfString;
  10699. begin
  10700. StartProgram(false);
  10701. Add([
  10702. 'type',
  10703. 'type',
  10704. ' TMonthNameArray = array [1..12] of string;',
  10705. ' TMonthNames = TMonthNameArray;',
  10706. ' TObject = class',
  10707. ' private',
  10708. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10709. ' public',
  10710. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10711. ' end;',
  10712. 'var',
  10713. ' f: TObject;',
  10714. ' Month: string;',
  10715. ' Names: array of string = (''a'',''foo'',''bar'');',
  10716. ' i: longint;',
  10717. 'begin',
  10718. ' for Month in f.LongMonthNames do ;',
  10719. ' for Month in Names do ;',
  10720. ' for i:=low(Names) to high(Names) do ;',
  10721. '']);
  10722. ConvertProgram;
  10723. CheckSource('TestArray_ForInArrOfString',
  10724. LinesToStr([ // statements
  10725. 'rtl.createClass(this, "TObject", null, function () {',
  10726. ' this.$init = function () {',
  10727. ' };',
  10728. ' this.$final = function () {',
  10729. ' };',
  10730. '});',
  10731. 'this.f = null;',
  10732. 'this.Month = "";',
  10733. 'this.Names = ["a", "foo", "bar"];',
  10734. 'this.i = 0;',
  10735. '']),
  10736. LinesToStr([ // $mod.$main
  10737. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10738. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10739. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10740. '']));
  10741. end;
  10742. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10743. begin
  10744. StartProgram(false);
  10745. Add([
  10746. '{$modeswitch externalclass}',
  10747. 'type',
  10748. ' TJSObject = class external name ''Object''',
  10749. ' end;',
  10750. ' TJSArray = class external name ''Array''',
  10751. ' class function isArray(Value: JSValue) : boolean;',
  10752. ' function concat() : TJSArray; varargs;',
  10753. ' end;',
  10754. 'var',
  10755. ' aObj: TJSArray;',
  10756. ' a: array of longint;',
  10757. ' o: TJSObject;',
  10758. 'begin',
  10759. ' if TJSArray.isArray(65) then ;',
  10760. ' aObj:=TJSArray(a).concat(a);',
  10761. ' o:=TJSObject(a);']);
  10762. ConvertProgram;
  10763. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10764. LinesToStr([ // statements
  10765. 'this.aObj = null;',
  10766. 'this.a = [];',
  10767. 'this.o = null;',
  10768. '']),
  10769. LinesToStr([ // $mod.$main
  10770. 'if (Array.isArray(65)) ;',
  10771. '$mod.aObj = $mod.a.concat($mod.a);',
  10772. '$mod.o = $mod.a;',
  10773. '']));
  10774. end;
  10775. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10776. begin
  10777. StartProgram(false);
  10778. Add([
  10779. '{$modeswitch externalclass}',
  10780. 'type',
  10781. ' TArrStr = array of string;',
  10782. ' TJSArray = class external name ''Array''',
  10783. ' end;',
  10784. ' TJSObject = class external name ''Object''',
  10785. ' end;',
  10786. 'var',
  10787. ' aObj: TJSArray;',
  10788. ' a: TArrStr;',
  10789. ' jo: TJSObject;',
  10790. 'begin',
  10791. ' a:=TArrStr(aObj);',
  10792. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10793. ' a:=TarrStr(jo);',
  10794. '']);
  10795. ConvertProgram;
  10796. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10797. LinesToStr([ // statements
  10798. 'this.aObj = null;',
  10799. 'this.a = [];',
  10800. 'this.jo = null;',
  10801. '']),
  10802. LinesToStr([ // $mod.$main
  10803. '$mod.a = $mod.aObj;',
  10804. '$mod.aObj[1] = $mod.aObj[2];',
  10805. '$mod.a = $mod.jo;',
  10806. '']));
  10807. end;
  10808. procedure TTestModule.TestArrayOfConst_TVarRec;
  10809. begin
  10810. StartProgram(true,[supTVarRec]);
  10811. Add([
  10812. 'procedure Say(args: array of const);',
  10813. 'var',
  10814. ' i: longint;',
  10815. ' v: TVarRec;',
  10816. 'begin',
  10817. ' for i:=low(args) to high(args) do begin',
  10818. ' v:=args[i];',
  10819. ' case v.vtype of',
  10820. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10821. ' end;',
  10822. ' end;',
  10823. ' for v in args do ;',
  10824. ' args:=nil;',
  10825. ' SetLength(args,2);',
  10826. 'end;',
  10827. 'begin']);
  10828. ConvertProgram;
  10829. CheckSource('TestArrayOfConst_TVarRec',
  10830. LinesToStr([ // statements
  10831. 'this.Say = function (args) {',
  10832. ' var i = 0;',
  10833. ' var v = pas.system.TVarRec.$new();',
  10834. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10835. ' i = $l;',
  10836. ' v.$assign(args[i]);',
  10837. ' var $tmp = v.VType;',
  10838. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10839. ' };',
  10840. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10841. ' args = [];',
  10842. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10843. '};',
  10844. '']),
  10845. LinesToStr([ // $mod.$main
  10846. ]));
  10847. end;
  10848. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10849. begin
  10850. StartProgram(true,[supTVarRec]);
  10851. Add([
  10852. 'procedure Say(args: array of const);',
  10853. 'begin',
  10854. ' Say(args);',
  10855. 'end;',
  10856. 'var',
  10857. ' p: Pointer;',
  10858. ' j: jsvalue;',
  10859. ' c: currency;',
  10860. 'begin',
  10861. ' Say([]);',
  10862. ' Say([1]);',
  10863. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10864. '']);
  10865. ConvertProgram;
  10866. CheckSource('TestArrayOfConst_PassBaseTypes',
  10867. LinesToStr([ // statements
  10868. 'this.Say = function (args) {',
  10869. ' $mod.Say(args);',
  10870. '};',
  10871. 'this.p = null;',
  10872. 'this.j = undefined;',
  10873. 'this.c = 0;',
  10874. '']),
  10875. LinesToStr([ // $mod.$main
  10876. '$mod.Say([]);',
  10877. '$mod.Say(pas.system.VarRecs(0, 1));',
  10878. '$mod.Say(pas.system.VarRecs(',
  10879. ' 9,',
  10880. ' "c",',
  10881. ' 18,',
  10882. ' "foo",',
  10883. ' 5,',
  10884. ' null,',
  10885. ' 1,',
  10886. ' true,',
  10887. ' 3,',
  10888. ' 1.3,',
  10889. ' 5,',
  10890. ' $mod.p,',
  10891. ' 20,',
  10892. ' $mod.j,',
  10893. ' 12,',
  10894. ' $mod.c',
  10895. ' ));',
  10896. '']));
  10897. end;
  10898. procedure TTestModule.TestArrayOfConst_PassObj;
  10899. begin
  10900. StartProgram(true,[supTVarRec]);
  10901. Add([
  10902. '{$interfaces corba}',
  10903. 'type',
  10904. ' TObject = class',
  10905. ' end;',
  10906. ' TClass = class of TObject;',
  10907. ' IUnknown = interface',
  10908. ' end;',
  10909. 'procedure Say(args: array of const);',
  10910. 'begin',
  10911. 'end;',
  10912. 'var',
  10913. ' o: TObject;',
  10914. ' c: TClass;',
  10915. ' i: IUnknown;',
  10916. 'begin',
  10917. ' Say([o,c,TObject]);',
  10918. ' Say([nil,i]);',
  10919. '']);
  10920. ConvertProgram;
  10921. CheckSource('TestArrayOfConst_PassObj',
  10922. LinesToStr([ // statements
  10923. 'rtl.createClass(this, "TObject", null, function () {',
  10924. ' this.$init = function () {',
  10925. ' };',
  10926. ' this.$final = function () {',
  10927. ' };',
  10928. '});',
  10929. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10930. 'this.Say = function (args) {',
  10931. '};',
  10932. 'this.o = null;',
  10933. 'this.c = null;',
  10934. 'this.i = null;',
  10935. '']),
  10936. LinesToStr([ // $mod.$main
  10937. '$mod.Say(pas.system.VarRecs(',
  10938. ' 7,',
  10939. ' $mod.o,',
  10940. ' 8,',
  10941. ' $mod.c,',
  10942. ' 8,',
  10943. ' $mod.TObject',
  10944. '));',
  10945. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10946. '']));
  10947. end;
  10948. procedure TTestModule.TestRecord_Empty;
  10949. begin
  10950. StartProgram(false);
  10951. Add([
  10952. 'type',
  10953. ' TRecA = record',
  10954. ' end;',
  10955. 'var a,b: TRecA;',
  10956. 'begin',
  10957. ' if a=b then ;']);
  10958. ConvertProgram;
  10959. CheckSource('TestRecord_Empty',
  10960. LinesToStr([ // statements
  10961. 'rtl.recNewT(this, "TRecA", function () {',
  10962. ' this.$eq = function (b) {',
  10963. ' return true;',
  10964. ' };',
  10965. ' this.$assign = function (s) {',
  10966. ' return this;',
  10967. ' };',
  10968. '});',
  10969. 'this.a = this.TRecA.$new();',
  10970. 'this.b = this.TRecA.$new();',
  10971. '']),
  10972. LinesToStr([ // $mod.$main
  10973. 'if ($mod.a.$eq($mod.b)) ;'
  10974. ]));
  10975. end;
  10976. procedure TTestModule.TestRecord_Var;
  10977. begin
  10978. StartProgram(false);
  10979. Add('type');
  10980. Add(' TRecA = record');
  10981. Add(' Bold: longint;');
  10982. Add(' end;');
  10983. Add('var Rec: TRecA;');
  10984. Add('begin');
  10985. Add(' rec.bold:=123');
  10986. ConvertProgram;
  10987. CheckSource('TestRecord_Var',
  10988. LinesToStr([ // statements
  10989. 'rtl.recNewT(this, "TRecA", function () {',
  10990. ' this.Bold = 0;',
  10991. ' this.$eq = function (b) {',
  10992. ' return this.Bold === b.Bold;',
  10993. ' };',
  10994. ' this.$assign = function (s) {',
  10995. ' this.Bold = s.Bold;',
  10996. ' return this;',
  10997. ' };',
  10998. '});',
  10999. 'this.Rec = this.TRecA.$new();',
  11000. '']),
  11001. LinesToStr([ // $mod.$main
  11002. '$mod.Rec.Bold = 123;'
  11003. ]));
  11004. end;
  11005. procedure TTestModule.TestRecord_VarExternal;
  11006. begin
  11007. StartProgram(false);
  11008. Add([
  11009. '{$modeswitch externalclass}',
  11010. 'type',
  11011. ' TRecA = record',
  11012. ' i: byte;',
  11013. ' length_: longint external name ''length'';',
  11014. ' end;',
  11015. 'var Rec: TRecA;',
  11016. 'begin',
  11017. ' rec.length_ := rec.length_',
  11018. '']);
  11019. ConvertProgram;
  11020. CheckSource('TestRecord_VarExternal',
  11021. LinesToStr([ // statements
  11022. 'rtl.recNewT(this, "TRecA", function () {',
  11023. ' this.i = 0;',
  11024. ' this.$eq = function (b) {',
  11025. ' return (this.i === b.i) && (this.length === b.length);',
  11026. ' };',
  11027. ' this.$assign = function (s) {',
  11028. ' this.i = s.i;',
  11029. ' this.length = s.length;',
  11030. ' return this;',
  11031. ' };',
  11032. '});',
  11033. 'this.Rec = this.TRecA.$new();',
  11034. '']),
  11035. LinesToStr([ // $mod.$main
  11036. '$mod.Rec.length = $mod.Rec.length;'
  11037. ]));
  11038. end;
  11039. procedure TTestModule.TestRecord_WithDo;
  11040. begin
  11041. StartProgram(false);
  11042. Add('type');
  11043. Add(' TRec = record');
  11044. Add(' vI: longint;');
  11045. Add(' end;');
  11046. Add('var');
  11047. Add(' Int: longint;');
  11048. Add(' r: TRec;');
  11049. Add('begin');
  11050. Add(' with r do');
  11051. Add(' int:=vi;');
  11052. Add(' with r do begin');
  11053. Add(' int:=vi;');
  11054. Add(' vi:=int;');
  11055. Add(' end;');
  11056. ConvertProgram;
  11057. CheckSource('TestWithRecordDo',
  11058. LinesToStr([ // statements
  11059. 'rtl.recNewT(this, "TRec", function () {',
  11060. ' this.vI = 0;',
  11061. ' this.$eq = function (b) {',
  11062. ' return this.vI === b.vI;',
  11063. ' };',
  11064. ' this.$assign = function (s) {',
  11065. ' this.vI = s.vI;',
  11066. ' return this;',
  11067. ' };',
  11068. '});',
  11069. 'this.Int = 0;',
  11070. 'this.r = this.TRec.$new();',
  11071. '']),
  11072. LinesToStr([ // $mod.$main
  11073. 'var $with = $mod.r;',
  11074. '$mod.Int = $with.vI;',
  11075. 'var $with1 = $mod.r;',
  11076. '$mod.Int = $with1.vI;',
  11077. '$with1.vI = $mod.Int;'
  11078. ]));
  11079. end;
  11080. procedure TTestModule.TestRecord_Assign;
  11081. begin
  11082. StartProgram(false);
  11083. Add([
  11084. 'type',
  11085. ' TEnum = (red,green);',
  11086. ' TEnums = set of TEnum;',
  11087. ' TSmallRec = record',
  11088. ' N: longint;',
  11089. ' end;',
  11090. ' TBigRec = record',
  11091. ' Int: longint;',
  11092. ' D: double;',
  11093. ' Arr: array of longint;',
  11094. ' Arr2: array[1..2] of longint;',
  11095. ' Small: TSmallRec;',
  11096. ' Enums: TEnums;',
  11097. ' end;',
  11098. 'var',
  11099. ' r, s: TBigRec;',
  11100. 'begin',
  11101. ' r:=s;',
  11102. ' r:=default(TBigRec);',
  11103. ' r:=default(s);',
  11104. '']);
  11105. ConvertProgram;
  11106. CheckSource('TestRecord_Assign',
  11107. LinesToStr([ // statements
  11108. 'this.TEnum = {',
  11109. ' "0": "red",',
  11110. ' red: 0,',
  11111. ' "1": "green",',
  11112. ' green: 1',
  11113. '};',
  11114. 'rtl.recNewT(this, "TSmallRec", function () {',
  11115. ' this.N = 0;',
  11116. ' this.$eq = function (b) {',
  11117. ' return this.N === b.N;',
  11118. ' };',
  11119. ' this.$assign = function (s) {',
  11120. ' this.N = s.N;',
  11121. ' return this;',
  11122. ' };',
  11123. '});',
  11124. 'rtl.recNewT(this, "TBigRec", function () {',
  11125. ' this.Int = 0;',
  11126. ' this.D = 0.0;',
  11127. ' this.$new = function () {',
  11128. ' var r = Object.create(this);',
  11129. ' r.Arr = [];',
  11130. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11131. ' r.Small = $mod.TSmallRec.$new();',
  11132. ' r.Enums = {};',
  11133. ' return r;',
  11134. ' };',
  11135. ' this.$eq = function (b) {',
  11136. ' 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);',
  11137. ' };',
  11138. ' this.$assign = function (s) {',
  11139. ' this.Int = s.Int;',
  11140. ' this.D = s.D;',
  11141. ' this.Arr = rtl.arrayRef(s.Arr);',
  11142. ' this.Arr2 = s.Arr2.slice(0);',
  11143. ' this.Small.$assign(s.Small);',
  11144. ' this.Enums = rtl.refSet(s.Enums);',
  11145. ' return this;',
  11146. ' };',
  11147. '});',
  11148. 'this.r = this.TBigRec.$new();',
  11149. 'this.s = this.TBigRec.$new();',
  11150. '']),
  11151. LinesToStr([ // $mod.$main
  11152. '$mod.r.$assign($mod.s);',
  11153. '$mod.r.$assign($mod.TBigRec.$new());',
  11154. '$mod.r.$assign($mod.TBigRec.$new());',
  11155. '']));
  11156. end;
  11157. procedure TTestModule.TestRecord_AsParams;
  11158. begin
  11159. StartProgram(false);
  11160. Add([
  11161. 'type',
  11162. ' integer = longint;',
  11163. ' TRecord = record',
  11164. ' i: integer;',
  11165. ' end;',
  11166. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11167. 'var vL: TRecord;',
  11168. 'begin',
  11169. ' vd:=vd;',
  11170. ' vd.i:=vd.i;',
  11171. ' vl:=vc;',
  11172. ' vv:=vv;',
  11173. ' vv.i:=vv.i;',
  11174. ' U:=vl;',
  11175. ' U:=vd;',
  11176. ' U:=vc;',
  11177. ' U:=vv;',
  11178. ' vl:=TRecord(U);',
  11179. ' vd:=TRecord(U);',
  11180. ' vv:=TRecord(U);',
  11181. ' doit(vd,vd,vd,vd);',
  11182. ' doit(vc,vc,vl,vl);',
  11183. ' doit(vv,vv,vv,vv);',
  11184. ' doit(vl,vl,vl,vl);',
  11185. ' TRecord(U).i:=3;',
  11186. 'end;',
  11187. 'var i: TRecord;',
  11188. 'begin',
  11189. ' doit(i,i,i,i);',
  11190. '']);
  11191. ConvertProgram;
  11192. CheckSource('TestRecord_AsParams',
  11193. LinesToStr([ // statements
  11194. 'rtl.recNewT(this, "TRecord", function () {',
  11195. ' this.i = 0;',
  11196. ' this.$eq = function (b) {',
  11197. ' return this.i === b.i;',
  11198. ' };',
  11199. ' this.$assign = function (s) {',
  11200. ' this.i = s.i;',
  11201. ' return this;',
  11202. ' };',
  11203. '});',
  11204. 'this.DoIt = function (vD, vC, vV, U) {',
  11205. ' var vL = $mod.TRecord.$new();',
  11206. ' vD.$assign(vD);',
  11207. ' vD.i = vD.i;',
  11208. ' vL.$assign(vC);',
  11209. ' vV.$assign(vV);',
  11210. ' vV.i = vV.i;',
  11211. ' U.$assign(vL);',
  11212. ' U.$assign(vD);',
  11213. ' U.$assign(vC);',
  11214. ' U.$assign(vV);',
  11215. ' vL.$assign(U);',
  11216. ' vD.$assign(U);',
  11217. ' vV.$assign(U);',
  11218. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11219. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11220. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11221. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11222. ' U.i = 3;',
  11223. '};',
  11224. 'this.i = this.TRecord.$new();'
  11225. ]),
  11226. LinesToStr([
  11227. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11228. '']));
  11229. end;
  11230. procedure TTestModule.TestRecord_ConstRef;
  11231. begin
  11232. StartProgram(false);
  11233. Add([
  11234. 'type TRec = record i: word; end;',
  11235. 'procedure Run(constref a: TRec);',
  11236. 'begin',
  11237. 'end;',
  11238. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11239. 'var l: TRec;',
  11240. 'begin',
  11241. ' Run(l);',
  11242. ' Run(a);',
  11243. ' Run(b);',
  11244. ' Run(c);',
  11245. ' Run(d);',
  11246. ' Run(e);',
  11247. 'end;',
  11248. 'begin',
  11249. '']);
  11250. ConvertProgram;
  11251. CheckResolverUnexpectedHints();
  11252. CheckSource('TestRecord_ConstRef',
  11253. LinesToStr([ // statements
  11254. 'rtl.recNewT(this, "TRec", function () {',
  11255. ' this.i = 0;',
  11256. ' this.$eq = function (b) {',
  11257. ' return this.i === b.i;',
  11258. ' };',
  11259. ' this.$assign = function (s) {',
  11260. ' this.i = s.i;',
  11261. ' return this;',
  11262. ' };',
  11263. '});',
  11264. 'this.Run = function (a) {',
  11265. '};',
  11266. 'this.Fly = function (a, b, c, d, e) {',
  11267. ' var l = $mod.TRec.$new();',
  11268. ' $mod.Run(l);',
  11269. ' $mod.Run(a);',
  11270. ' $mod.Run(b);',
  11271. ' $mod.Run(c);',
  11272. ' $mod.Run(d);',
  11273. ' $mod.Run(e);',
  11274. '};',
  11275. '']),
  11276. LinesToStr([
  11277. '']));
  11278. end;
  11279. procedure TTestModule.TestRecordElement_AsParams;
  11280. begin
  11281. StartProgram(false);
  11282. Add('type');
  11283. Add(' integer = longint;');
  11284. Add(' TRecord = record');
  11285. Add(' i: integer;');
  11286. Add(' end;');
  11287. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11288. Add('var vJ: TRecord;');
  11289. Add('begin');
  11290. Add(' doit(vj.i,vj.i,vj.i);');
  11291. Add('end;');
  11292. Add('var r: TRecord;');
  11293. Add('begin');
  11294. Add(' doit(r.i,r.i,r.i);');
  11295. ConvertProgram;
  11296. CheckSource('TestRecordElement_AsParams',
  11297. LinesToStr([ // statements
  11298. 'rtl.recNewT(this, "TRecord", function () {',
  11299. ' this.i = 0;',
  11300. ' this.$eq = function (b) {',
  11301. ' return this.i === b.i;',
  11302. ' };',
  11303. ' this.$assign = function (s) {',
  11304. ' this.i = s.i;',
  11305. ' return this;',
  11306. ' };',
  11307. '});',
  11308. 'this.DoIt = function (vG,vH,vI) {',
  11309. ' var vJ = $mod.TRecord.$new();',
  11310. ' $mod.DoIt(vJ.i, vJ.i, {',
  11311. ' p: vJ,',
  11312. ' get: function () {',
  11313. ' return this.p.i;',
  11314. ' },',
  11315. ' set: function (v) {',
  11316. ' this.p.i = v;',
  11317. ' }',
  11318. ' });',
  11319. '};',
  11320. 'this.r = this.TRecord.$new();'
  11321. ]),
  11322. LinesToStr([
  11323. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11324. ' p: $mod.r,',
  11325. ' get: function () {',
  11326. ' return this.p.i;',
  11327. ' },',
  11328. ' set: function (v) {',
  11329. ' this.p.i = v;',
  11330. ' }',
  11331. '});'
  11332. ]));
  11333. end;
  11334. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11335. begin
  11336. StartProgram(false);
  11337. Add('type');
  11338. Add(' integer = longint;');
  11339. Add(' TRecord = record');
  11340. Add(' i: integer;');
  11341. Add(' end;');
  11342. Add('function GetRec(vB: integer = 0): TRecord;');
  11343. Add('begin');
  11344. Add('end;');
  11345. Add('procedure DoIt(vG: integer; const vH: integer);');
  11346. Add('begin');
  11347. Add('end;');
  11348. Add('begin');
  11349. Add(' doit(getrec.i,getrec.i);');
  11350. Add(' doit(getrec().i,getrec().i);');
  11351. Add(' doit(getrec(1).i,getrec(2).i);');
  11352. ConvertProgram;
  11353. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11354. LinesToStr([ // statements
  11355. 'rtl.recNewT(this, "TRecord", function () {',
  11356. ' this.i = 0;',
  11357. ' this.$eq = function (b) {',
  11358. ' return this.i === b.i;',
  11359. ' };',
  11360. ' this.$assign = function (s) {',
  11361. ' this.i = s.i;',
  11362. ' return this;',
  11363. ' };',
  11364. '});',
  11365. 'this.GetRec = function (vB) {',
  11366. ' var Result = $mod.TRecord.$new();',
  11367. ' return Result;',
  11368. '};',
  11369. 'this.DoIt = function (vG, vH) {',
  11370. '};',
  11371. '']),
  11372. LinesToStr([
  11373. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11374. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11375. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11376. '']));
  11377. end;
  11378. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11379. begin
  11380. StartProgram(false);
  11381. Add('type');
  11382. Add(' integer = longint;');
  11383. Add(' TRecord = record');
  11384. Add(' i: integer;');
  11385. Add(' end;');
  11386. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11387. Add('begin');
  11388. Add('end;');
  11389. Add('var r: trecord;');
  11390. Add('begin');
  11391. Add(' with r do ');
  11392. Add(' doit(i,i,i);');
  11393. ConvertProgram;
  11394. CheckSource('TestRecordElementFromWith_AsParams',
  11395. LinesToStr([ // statements
  11396. 'rtl.recNewT(this, "TRecord", function () {',
  11397. ' this.i = 0;',
  11398. ' this.$eq = function (b) {',
  11399. ' return this.i === b.i;',
  11400. ' };',
  11401. ' this.$assign = function (s) {',
  11402. ' this.i = s.i;',
  11403. ' return this;',
  11404. ' };',
  11405. '});',
  11406. 'this.DoIt = function (vG,vH,vI) {',
  11407. '};',
  11408. 'this.r = this.TRecord.$new();'
  11409. ]),
  11410. LinesToStr([
  11411. 'var $with = $mod.r;',
  11412. '$mod.DoIt($with.i,$with.i,{',
  11413. ' p: $with,',
  11414. ' get: function () {',
  11415. ' return this.p.i;',
  11416. ' },',
  11417. ' set: function (v) {',
  11418. ' this.p.i = v;',
  11419. ' }',
  11420. '});',
  11421. '']));
  11422. end;
  11423. procedure TTestModule.TestRecord_Equal;
  11424. begin
  11425. StartProgram(false);
  11426. Add('type');
  11427. Add(' integer = longint;');
  11428. Add(' TFlag = (red,blue);');
  11429. Add(' TFlags = set of TFlag;');
  11430. Add(' TProc = procedure;');
  11431. Add(' TRecord = record');
  11432. Add(' i: integer;');
  11433. Add(' Event: TProc;');
  11434. Add(' f: TFlags;');
  11435. Add(' end;');
  11436. Add(' TNested = record');
  11437. Add(' r: TRecord;');
  11438. Add(' end;');
  11439. Add('var');
  11440. Add(' b: boolean;');
  11441. Add(' r,s: trecord;');
  11442. Add('begin');
  11443. Add(' b:=r=s;');
  11444. Add(' b:=r<>s;');
  11445. ConvertProgram;
  11446. CheckSource('TestRecord_Equal',
  11447. LinesToStr([ // statements
  11448. 'this.TFlag = {',
  11449. ' "0": "red",',
  11450. ' red: 0,',
  11451. ' "1": "blue",',
  11452. ' blue: 1',
  11453. '};',
  11454. 'rtl.recNewT(this, "TRecord", function () {',
  11455. ' this.i = 0;',
  11456. ' this.Event = null;',
  11457. ' this.$new = function () {',
  11458. ' var r = Object.create(this);',
  11459. ' r.f = {};',
  11460. ' return r;',
  11461. ' };',
  11462. ' this.$eq = function (b) {',
  11463. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11464. ' };',
  11465. ' this.$assign = function (s) {',
  11466. ' this.i = s.i;',
  11467. ' this.Event = s.Event;',
  11468. ' this.f = rtl.refSet(s.f);',
  11469. ' return this;',
  11470. ' };',
  11471. '});',
  11472. 'rtl.recNewT(this, "TNested", function () {',
  11473. ' this.$new = function () {',
  11474. ' var r = Object.create(this);',
  11475. ' r.r = $mod.TRecord.$new();',
  11476. ' return r;',
  11477. ' };',
  11478. ' this.$eq = function (b) {',
  11479. ' return this.r.$eq(b.r);',
  11480. ' };',
  11481. ' this.$assign = function (s) {',
  11482. ' this.r.$assign(s.r);',
  11483. ' return this;',
  11484. ' };',
  11485. '});',
  11486. 'this.b = false;',
  11487. 'this.r = this.TRecord.$new();',
  11488. 'this.s = this.TRecord.$new();',
  11489. '']),
  11490. LinesToStr([
  11491. '$mod.b = $mod.r.$eq($mod.s);',
  11492. '$mod.b = !$mod.r.$eq($mod.s);',
  11493. '']));
  11494. end;
  11495. procedure TTestModule.TestRecord_JSValue;
  11496. begin
  11497. StartProgram(false);
  11498. Add([
  11499. 'type',
  11500. ' TRecord = record',
  11501. ' i: longint;',
  11502. ' end;',
  11503. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11504. 'begin',
  11505. 'end;',
  11506. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11507. 'begin',
  11508. ' if jsvalue(d) then ;',
  11509. ' if jsvalue(c) then ;',
  11510. ' if jsvalue(v) then ;',
  11511. 'end;',
  11512. 'var',
  11513. ' Jv: jsvalue;',
  11514. ' Rec: trecord;',
  11515. 'begin',
  11516. ' rec:=trecord(jv);',
  11517. ' jv:=rec;',
  11518. ' Fly(rec,rec);',
  11519. ' Fly(@rec,@rec);',
  11520. ' if jsvalue(Rec) then ;',
  11521. ' Run(trecord(jv),trecord(jv),rec);',
  11522. '']);
  11523. ConvertProgram;
  11524. CheckSource('TestRecord_JSValue',
  11525. LinesToStr([ // statements
  11526. 'rtl.recNewT(this, "TRecord", function () {',
  11527. ' this.i = 0;',
  11528. ' this.$eq = function (b) {',
  11529. ' return this.i === b.i;',
  11530. ' };',
  11531. ' this.$assign = function (s) {',
  11532. ' this.i = s.i;',
  11533. ' return this;',
  11534. ' };',
  11535. '});',
  11536. 'this.Fly = function (d, c) {',
  11537. '};',
  11538. 'this.Run = function (d, c, v) {',
  11539. ' if (d) ;',
  11540. ' if (c) ;',
  11541. ' if (v) ;',
  11542. '};',
  11543. 'this.Jv = undefined;',
  11544. 'this.Rec = this.TRecord.$new();',
  11545. '']),
  11546. LinesToStr([
  11547. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11548. '$mod.Jv = $mod.Rec;',
  11549. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11550. '$mod.Fly($mod.Rec, $mod.Rec);',
  11551. 'if ($mod.Rec) ;',
  11552. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11553. '']));
  11554. end;
  11555. procedure TTestModule.TestRecord_VariantFail;
  11556. begin
  11557. StartProgram(false);
  11558. Add([
  11559. 'type',
  11560. ' TRec = record',
  11561. ' case word of',
  11562. ' 0: (b0, b1: Byte);',
  11563. ' 1: (i: word);',
  11564. ' end;',
  11565. 'begin']);
  11566. SetExpectedPasResolverError('variant record is not supported',
  11567. nXIsNotSupported);
  11568. ConvertProgram;
  11569. end;
  11570. procedure TTestModule.TestRecord_FieldArray;
  11571. begin
  11572. StartProgram(false);
  11573. Add([
  11574. 'type',
  11575. ' TArrInt = array[3..4] of longint;',
  11576. ' TArrArrInt = array[3..4] of longint;',
  11577. ' TRec = record',
  11578. ' a: array of longint;',
  11579. ' s: array[1..2] of longint;',
  11580. ' m: array[1..2,3..4] of longint;',
  11581. ' o: TArrArrInt;',
  11582. ' end;',
  11583. 'begin']);
  11584. ConvertProgram;
  11585. CheckSource('TestRecord_FieldArray',
  11586. LinesToStr([ // statements
  11587. 'rtl.recNewT(this, "TRec", function () {',
  11588. ' this.m$a$clone = function (a) {',
  11589. ' var b = [];',
  11590. ' b.length = 2;',
  11591. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11592. ' return b;',
  11593. ' };',
  11594. ' this.$new = function () {',
  11595. ' var r = Object.create(this);',
  11596. ' r.a = [];',
  11597. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11598. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11599. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11600. ' return r;',
  11601. ' };',
  11602. ' this.$eq = function (b) {',
  11603. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11604. ' };',
  11605. ' this.$assign = function (s) {',
  11606. ' this.a = rtl.arrayRef(s.a);',
  11607. ' this.s = s.s.slice(0);',
  11608. ' this.m = this.m$a$clone(s.m);',
  11609. ' this.o = s.o.slice(0);',
  11610. ' return this;',
  11611. ' };',
  11612. '});',
  11613. '']),
  11614. LinesToStr([ // $mod.$main
  11615. '']));
  11616. end;
  11617. procedure TTestModule.TestRecord_Const;
  11618. begin
  11619. StartProgram(false);
  11620. Add([
  11621. 'type',
  11622. ' TArrInt = array[3..4] of longint;',
  11623. ' TPoint = record x,y: longint; end;',
  11624. ' TRec = record',
  11625. ' i: longint;',
  11626. ' a: array of longint;',
  11627. ' s: array[1..2] of longint;',
  11628. ' m: array[1..2,3..4] of longint;',
  11629. ' p: TPoint;',
  11630. ' end;',
  11631. ' TPoints = array of TPoint;',
  11632. 'const',
  11633. ' r: TRec = (',
  11634. ' i:1;',
  11635. ' a:(2,3);',
  11636. ' s:(4,5);',
  11637. ' m:( (11,12), (13,14) );',
  11638. ' p: (x:21; y:22)',
  11639. ' );',
  11640. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11641. 'begin']);
  11642. ConvertProgram;
  11643. CheckSource('TestRecord_Const',
  11644. LinesToStr([ // statements
  11645. 'rtl.recNewT(this, "TPoint", function () {',
  11646. ' this.x = 0;',
  11647. ' this.y = 0;',
  11648. ' this.$eq = function (b) {',
  11649. ' return (this.x === b.x) && (this.y === b.y);',
  11650. ' };',
  11651. ' this.$assign = function (s) {',
  11652. ' this.x = s.x;',
  11653. ' this.y = s.y;',
  11654. ' return this;',
  11655. ' };',
  11656. '});',
  11657. 'rtl.recNewT(this, "TRec", function () {',
  11658. ' this.i = 0;',
  11659. ' this.m$a$clone = function (a) {',
  11660. ' var b = [];',
  11661. ' b.length = 2;',
  11662. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11663. ' return b;',
  11664. ' };',
  11665. ' this.$new = function () {',
  11666. ' var r = Object.create(this);',
  11667. ' r.a = [];',
  11668. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11669. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11670. ' r.p = $mod.TPoint.$new();',
  11671. ' return r;',
  11672. ' };',
  11673. ' this.$eq = function (b) {',
  11674. ' 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);',
  11675. ' };',
  11676. ' this.$assign = function (s) {',
  11677. ' this.i = s.i;',
  11678. ' this.a = rtl.arrayRef(s.a);',
  11679. ' this.s = s.s.slice(0);',
  11680. ' this.m = this.m$a$clone(s.m);',
  11681. ' this.p.$assign(s.p);',
  11682. ' return this;',
  11683. ' };',
  11684. '});',
  11685. 'this.r = this.TRec.$clone({',
  11686. ' i: 1,',
  11687. ' a: [2, 3],',
  11688. ' s: [4, 5],',
  11689. ' m: [[11, 12], [13, 14]],',
  11690. ' p: this.TPoint.$clone({',
  11691. ' x: 21,',
  11692. ' y: 22',
  11693. ' })',
  11694. '});',
  11695. 'this.p = [this.TPoint.$clone({',
  11696. ' x: 1,',
  11697. ' y: 2',
  11698. '}), this.TPoint.$clone({',
  11699. ' x: 3,',
  11700. ' y: 4',
  11701. '})];',
  11702. '']),
  11703. LinesToStr([ // $mod.$main
  11704. '']));
  11705. end;
  11706. procedure TTestModule.TestRecord_TypecastFail;
  11707. begin
  11708. StartProgram(false);
  11709. Add([
  11710. 'type',
  11711. ' TPoint = record x,y: longint; end;',
  11712. ' TRec = record l: longint end;',
  11713. 'var p: TPoint;',
  11714. 'begin',
  11715. ' if TRec(p).l=2 then ;']);
  11716. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11717. nIllegalTypeConversionTo);
  11718. ConvertProgram;
  11719. end;
  11720. procedure TTestModule.TestRecord_InFunction;
  11721. begin
  11722. StartProgram(false);
  11723. Add([
  11724. 'var TPoint: longint = 3;',
  11725. 'procedure DoIt;',
  11726. 'type',
  11727. ' TPoint = record x,y: longint; end;',
  11728. ' TPoints = array of TPoint;',
  11729. 'var',
  11730. ' r: TPoint;',
  11731. ' p: TPoints;',
  11732. 'begin',
  11733. ' SetLength(p,2);',
  11734. 'end;',
  11735. 'begin']);
  11736. ConvertProgram;
  11737. CheckSource('TestRecord_InFunction',
  11738. LinesToStr([ // statements
  11739. 'this.TPoint = 3;',
  11740. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11741. ' this.x = 0;',
  11742. ' this.y = 0;',
  11743. ' this.$eq = function (b) {',
  11744. ' return (this.x === b.x) && (this.y === b.y);',
  11745. ' };',
  11746. ' this.$assign = function (s) {',
  11747. ' this.x = s.x;',
  11748. ' this.y = s.y;',
  11749. ' return this;',
  11750. ' };',
  11751. '});',
  11752. 'this.DoIt = function () {',
  11753. ' var r = TPoint$1.$new();',
  11754. ' var p = [];',
  11755. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11756. '};',
  11757. '']),
  11758. LinesToStr([ // $mod.$main
  11759. '']));
  11760. end;
  11761. procedure TTestModule.TestRecord_AnonymousFail;
  11762. begin
  11763. StartProgram(false);
  11764. Add([
  11765. 'var',
  11766. ' r: record x: word end;',
  11767. 'begin']);
  11768. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11769. nNotYetImplemented);
  11770. ConvertProgram;
  11771. end;
  11772. procedure TTestModule.TestAdvRecord_Function;
  11773. begin
  11774. StartProgram(false);
  11775. Parser.Options:=Parser.Options+[po_cassignments];
  11776. Add([
  11777. '{$modeswitch AdvancedRecords}',
  11778. 'type',
  11779. ' TPoint = record',
  11780. ' x,y: word;',
  11781. ' function Add(const apt: TPoint): TPoint;',
  11782. ' end;',
  11783. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11784. 'begin',
  11785. ' Result:=Self;',
  11786. ' Result.x+=apt.x;',
  11787. ' Result.y:=Result.y+apt.y;',
  11788. ' Self:=apt;',
  11789. 'end;',
  11790. 'var p,q: TPoint;',
  11791. 'begin',
  11792. ' p.add(q);',
  11793. ' p:=default(TPoint);',
  11794. ' p:=q;',
  11795. '']);
  11796. ConvertProgram;
  11797. CheckSource('TestAdvRecord_Function',
  11798. LinesToStr([ // statements
  11799. 'rtl.recNewT(this, "TPoint", function () {',
  11800. ' this.x = 0;',
  11801. ' this.y = 0;',
  11802. ' this.$eq = function (b) {',
  11803. ' return (this.x === b.x) && (this.y === b.y);',
  11804. ' };',
  11805. ' this.$assign = function (s) {',
  11806. ' this.x = s.x;',
  11807. ' this.y = s.y;',
  11808. ' return this;',
  11809. ' };',
  11810. ' this.Add = function (apt) {',
  11811. ' var Result = $mod.TPoint.$new();',
  11812. ' Result.$assign(this);',
  11813. ' Result.x += apt.x;',
  11814. ' Result.y = Result.y + apt.y;',
  11815. ' this.$assign(apt);',
  11816. ' return Result;',
  11817. ' };',
  11818. '});',
  11819. 'this.p = this.TPoint.$new();',
  11820. 'this.q = this.TPoint.$new();',
  11821. '']),
  11822. LinesToStr([ // $mod.$main
  11823. '$mod.p.Add($mod.q);',
  11824. '$mod.p.$assign($mod.TPoint.$new());',
  11825. '$mod.p.$assign($mod.q);',
  11826. '']));
  11827. end;
  11828. procedure TTestModule.TestAdvRecord_Property;
  11829. begin
  11830. StartProgram(false);
  11831. Add([
  11832. '{$modeswitch AdvancedRecords}',
  11833. 'type',
  11834. ' TPoint = record',
  11835. ' x,y: word;',
  11836. ' strict private',
  11837. ' function GetSize: longword;',
  11838. ' procedure SetSize(Value: longword);',
  11839. ' public',
  11840. ' property Size: longword read GetSize write SetSize;',
  11841. ' property Left: word read x write y;',
  11842. ' end;',
  11843. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11844. 'function TPoint.GetSize: longword;',
  11845. 'begin',
  11846. ' x:=y;',
  11847. ' Size:=Size;',
  11848. ' Left:=Left;',
  11849. 'end;',
  11850. 'procedure TPoint.SetSize(Value: longword);',
  11851. 'begin',
  11852. 'end;',
  11853. 'var p,q: TPoint;',
  11854. 'begin',
  11855. ' p.Size:=q.Size;',
  11856. ' p.Left:=q.Left;',
  11857. '']);
  11858. ConvertProgram;
  11859. CheckSource('TestAdvRecord_Property',
  11860. LinesToStr([ // statements
  11861. 'rtl.recNewT(this, "TPoint", function () {',
  11862. ' this.x = 0;',
  11863. ' this.y = 0;',
  11864. ' this.$eq = function (b) {',
  11865. ' return (this.x === b.x) && (this.y === b.y);',
  11866. ' };',
  11867. ' this.$assign = function (s) {',
  11868. ' this.x = s.x;',
  11869. ' this.y = s.y;',
  11870. ' return this;',
  11871. ' };',
  11872. ' this.GetSize = function () {',
  11873. ' var Result = 0;',
  11874. ' this.x = this.y;',
  11875. ' this.SetSize(this.GetSize());',
  11876. ' this.y = this.x;',
  11877. ' return Result;',
  11878. ' };',
  11879. ' this.SetSize = function (Value) {',
  11880. ' };',
  11881. '});',
  11882. 'this.SetSize = function (Value) {',
  11883. '};',
  11884. 'this.p = this.TPoint.$new();',
  11885. 'this.q = this.TPoint.$new();',
  11886. '']),
  11887. LinesToStr([ // $mod.$main
  11888. '$mod.p.SetSize($mod.q.GetSize());',
  11889. '$mod.p.y = $mod.q.x;',
  11890. '']));
  11891. end;
  11892. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11893. begin
  11894. StartProgram(false);
  11895. Add([
  11896. '{$modeswitch AdvancedRecords}',
  11897. 'type',
  11898. ' TPoint = record',
  11899. ' strict private',
  11900. ' function GetItems(Index: word): word;',
  11901. ' procedure SetItems(Index: word; Value: word);',
  11902. ' public',
  11903. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11904. ' end;',
  11905. 'function TPoint.GetItems(Index: word): word;',
  11906. 'begin',
  11907. ' Items[index]:=Items[index];',
  11908. ' self.Items[index]:=self.Items[index];',
  11909. 'end;',
  11910. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11911. 'begin',
  11912. 'end;',
  11913. 'var p: TPoint;',
  11914. 'begin',
  11915. ' p[1]:=p[2];',
  11916. ' p.Items[3]:=p.Items[4];',
  11917. '']);
  11918. ConvertProgram;
  11919. CheckSource('TestAdvRecord_PropertyDefault',
  11920. LinesToStr([ // statements
  11921. 'rtl.recNewT(this, "TPoint", function () {',
  11922. ' this.$eq = function (b) {',
  11923. ' return true;',
  11924. ' };',
  11925. ' this.$assign = function (s) {',
  11926. ' return this;',
  11927. ' };',
  11928. ' this.GetItems = function (Index) {',
  11929. ' var Result = 0;',
  11930. ' this.SetItems(Index, this.GetItems(Index));',
  11931. ' this.SetItems(Index, this.GetItems(Index));',
  11932. ' return Result;',
  11933. ' };',
  11934. ' this.SetItems = function (Index, Value) {',
  11935. ' };',
  11936. '});',
  11937. 'this.p = this.TPoint.$new();',
  11938. '']),
  11939. LinesToStr([ // $mod.$main
  11940. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11941. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11942. '']));
  11943. end;
  11944. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11945. begin
  11946. StartProgram(false);
  11947. Add([
  11948. '{$modeswitch AdvancedRecords}',
  11949. 'type',
  11950. ' TRec = record',
  11951. ' class var',
  11952. ' Fx: longint;',
  11953. ' Fy: longint;',
  11954. ' class function GetInt: longint; static;',
  11955. ' class procedure SetInt(Value: longint); static;',
  11956. ' class procedure DoIt; static;',
  11957. ' class property IntA: longint read Fx write Fy;',
  11958. ' class property IntB: longint read GetInt write SetInt;',
  11959. ' end;',
  11960. 'class function trec.getint: longint;',
  11961. 'begin',
  11962. ' result:=fx;',
  11963. 'end;',
  11964. 'class procedure trec.setint(value: longint);',
  11965. 'begin',
  11966. 'end;',
  11967. 'class procedure trec.doit;',
  11968. 'begin',
  11969. ' IntA:=IntA+1;',
  11970. ' IntB:=IntB+1;',
  11971. 'end;',
  11972. 'var r: trec;',
  11973. 'begin',
  11974. ' trec.inta:=trec.inta+1;',
  11975. ' if trec.intb=2 then;',
  11976. ' trec.intb:=trec.intb+2;',
  11977. ' trec.setint(trec.inta);',
  11978. ' r.inta:=r.inta+1;',
  11979. ' if r.intb=2 then;',
  11980. ' r.intb:=r.intb+2;',
  11981. ' r.setint(r.inta);']);
  11982. ConvertProgram;
  11983. CheckSource('TestAdvRecord_Property_ClassMethod',
  11984. LinesToStr([ // statements
  11985. 'rtl.recNewT(this, "TRec", function () {',
  11986. ' this.Fx = 0;',
  11987. ' this.Fy = 0;',
  11988. ' this.$eq = function (b) {',
  11989. ' return true;',
  11990. ' };',
  11991. ' this.$assign = function (s) {',
  11992. ' return this;',
  11993. ' };',
  11994. ' this.GetInt = function () {',
  11995. ' var Result = 0;',
  11996. ' Result = $mod.TRec.Fx;',
  11997. ' return Result;',
  11998. ' };',
  11999. ' this.SetInt = function (Value) {',
  12000. ' };',
  12001. ' this.DoIt = function () {',
  12002. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12003. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  12004. ' };',
  12005. '}, true);',
  12006. 'this.r = this.TRec.$new();',
  12007. '']),
  12008. LinesToStr([ // $mod.$main
  12009. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12010. 'if ($mod.TRec.GetInt() === 2) ;',
  12011. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12012. '$mod.TRec.SetInt($mod.TRec.Fx);',
  12013. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12014. 'if ($mod.TRec.GetInt() === 2) ;',
  12015. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12016. '$mod.TRec.SetInt($mod.r.Fx);',
  12017. '']));
  12018. end;
  12019. procedure TTestModule.TestAdvRecord_Const;
  12020. begin
  12021. StartProgram(false);
  12022. Add([
  12023. '{$modeswitch AdvancedRecords}',
  12024. 'type',
  12025. ' TArrInt = array[3..4] of longint;',
  12026. ' TPoint = record',
  12027. ' x,y: longint;',
  12028. ' class var Count: nativeint;',
  12029. ' end;',
  12030. ' TRec = record',
  12031. ' i: longint;',
  12032. ' a: array of longint;',
  12033. ' s: array[1..2] of longint;',
  12034. ' m: array[1..2,3..4] of longint;',
  12035. ' p: TPoint;',
  12036. ' end;',
  12037. ' TPoints = array of TPoint;',
  12038. 'const',
  12039. ' r: TRec = (',
  12040. ' i:1;',
  12041. ' a:(2,3);',
  12042. ' s:(4,5);',
  12043. ' m:( (11,12), (13,14) );',
  12044. ' p: (x:21)',
  12045. ' );',
  12046. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12047. 'begin']);
  12048. ConvertProgram;
  12049. CheckSource('TestAdvRecord_Const',
  12050. LinesToStr([ // statements
  12051. 'rtl.recNewT(this, "TPoint", function () {',
  12052. ' this.x = 0;',
  12053. ' this.y = 0;',
  12054. ' this.Count = 0;',
  12055. ' this.$eq = function (b) {',
  12056. ' return (this.x === b.x) && (this.y === b.y);',
  12057. ' };',
  12058. ' this.$assign = function (s) {',
  12059. ' this.x = s.x;',
  12060. ' this.y = s.y;',
  12061. ' return this;',
  12062. ' };',
  12063. '}, true);',
  12064. 'rtl.recNewT(this, "TRec", function () {',
  12065. ' this.i = 0;',
  12066. ' this.m$a$clone = function (a) {',
  12067. ' var b = [];',
  12068. ' b.length = 2;',
  12069. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12070. ' return b;',
  12071. ' };',
  12072. ' this.$new = function () {',
  12073. ' var r = Object.create(this);',
  12074. ' r.a = [];',
  12075. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12076. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12077. ' r.p = $mod.TPoint.$new();',
  12078. ' return r;',
  12079. ' };',
  12080. ' this.$eq = function (b) {',
  12081. ' 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);',
  12082. ' };',
  12083. ' this.$assign = function (s) {',
  12084. ' this.i = s.i;',
  12085. ' this.a = rtl.arrayRef(s.a);',
  12086. ' this.s = s.s.slice(0);',
  12087. ' this.m = this.m$a$clone(s.m);',
  12088. ' this.p.$assign(s.p);',
  12089. ' return this;',
  12090. ' };',
  12091. '});',
  12092. 'this.r = this.TRec.$clone({',
  12093. ' i: 1,',
  12094. ' a: [2, 3],',
  12095. ' s: [4, 5],',
  12096. ' m: [[11, 12], [13, 14]],',
  12097. ' p: this.TPoint.$clone({',
  12098. ' x: 21,',
  12099. ' y: 0',
  12100. ' })',
  12101. '});',
  12102. 'this.p = [this.TPoint.$clone({',
  12103. ' x: 1,',
  12104. ' y: 2',
  12105. '}), this.TPoint.$clone({',
  12106. ' x: 3,',
  12107. ' y: 4',
  12108. '})];',
  12109. '']),
  12110. LinesToStr([ // $mod.$main
  12111. '']));
  12112. end;
  12113. procedure TTestModule.TestAdvRecord_ExternalField;
  12114. begin
  12115. StartProgram(false);
  12116. Add([
  12117. '{$modeswitch AdvancedRecords}',
  12118. '{$modeswitch externalclass}',
  12119. 'type',
  12120. ' TCar = record',
  12121. ' public',
  12122. ' Intern: longint external name ''$Intern'';',
  12123. ' Intern2: longint external name ''$Intern2'';',
  12124. ' Bracket: longint external name ''["A B"]'';',
  12125. ' procedure DoIt;',
  12126. ' end;',
  12127. 'procedure tcar.doit;',
  12128. 'begin',
  12129. ' Intern:=Intern+1;',
  12130. ' Intern2:=Intern2+2;',
  12131. ' Bracket:=Bracket+3;',
  12132. 'end;',
  12133. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  12134. 'begin',
  12135. ' Rec.intern:=Rec.intern+1;',
  12136. ' Rec.intern2:=Rec.intern2+2;',
  12137. ' Rec.Bracket:=Rec.Bracket+3;',
  12138. ' with Rec do begin',
  12139. ' intern:=intern+1;',
  12140. ' intern2:=intern2+2;',
  12141. ' Bracket:=Bracket+3;',
  12142. ' end;']);
  12143. ConvertProgram;
  12144. CheckSource('TestAdvRecord_ExternalField',
  12145. LinesToStr([ // statements
  12146. 'rtl.recNewT(this, "TCar", function () {',
  12147. ' this.$eq = function (b) {',
  12148. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  12149. ' };',
  12150. ' this.$assign = function (s) {',
  12151. ' this.$Intern = s.$Intern;',
  12152. ' this.$Intern2 = s.$Intern2;',
  12153. ' this["A B"] = s["A B"];',
  12154. ' return this;',
  12155. ' };',
  12156. ' this.DoIt = function () {',
  12157. ' this.$Intern = this.$Intern + 1;',
  12158. ' this.$Intern2 = this.$Intern2 + 2;',
  12159. ' this["A B"] = this["A B"] + 3;',
  12160. ' };',
  12161. '});',
  12162. 'this.Rec = this.TCar.$clone({',
  12163. ' $Intern: 11,',
  12164. ' $Intern2: 12,',
  12165. ' "A B": 13',
  12166. '});',
  12167. '']),
  12168. LinesToStr([ // $mod.$main
  12169. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  12170. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  12171. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  12172. 'var $with = $mod.Rec;',
  12173. '$with.$Intern = $with.$Intern + 1;',
  12174. '$with.$Intern2 = $with.$Intern2 + 2;',
  12175. '$with["A B"] = $with["A B"] + 3;',
  12176. '']));
  12177. end;
  12178. procedure TTestModule.TestAdvRecord_SubRecord;
  12179. begin
  12180. StartProgram(false);
  12181. Add([
  12182. '{$modeswitch AdvancedRecords}',
  12183. 'type',
  12184. ' TRec = record',
  12185. ' type',
  12186. ' TPoint = record',
  12187. ' x,y: longint;',
  12188. ' class var Count: nativeint;',
  12189. ' procedure DoIt;',
  12190. ' class procedure DoThat; static;',
  12191. ' end;',
  12192. ' var',
  12193. ' i: longint;',
  12194. ' p: TPoint;',
  12195. ' procedure DoSome;',
  12196. ' end;',
  12197. 'const',
  12198. ' r: TRec = (',
  12199. ' i:1;',
  12200. ' p: (x:21;y:22)',
  12201. ' );',
  12202. 'procedure TRec.DoSome;',
  12203. 'begin',
  12204. ' p.x:=p.y+1;',
  12205. ' p.Count:=p.Count+2;',
  12206. 'end;',
  12207. 'procedure TRec.TPoint.DoIt;',
  12208. 'begin',
  12209. ' Count:=Count+3;',
  12210. 'end;',
  12211. 'class procedure TRec.TPoint.DoThat;',
  12212. 'begin',
  12213. ' Count:=Count+4;',
  12214. 'end;',
  12215. 'begin']);
  12216. ConvertProgram;
  12217. CheckSource('TestAdvRecord_SubRecord',
  12218. LinesToStr([ // statements
  12219. 'rtl.recNewT(this, "TRec", function () {',
  12220. ' rtl.recNewT(this, "TPoint", function () {',
  12221. ' this.x = 0;',
  12222. ' this.y = 0;',
  12223. ' this.Count = 0;',
  12224. ' this.$eq = function (b) {',
  12225. ' return (this.x === b.x) && (this.y === b.y);',
  12226. ' };',
  12227. ' this.$assign = function (s) {',
  12228. ' this.x = s.x;',
  12229. ' this.y = s.y;',
  12230. ' return this;',
  12231. ' };',
  12232. ' this.DoIt = function () {',
  12233. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  12234. ' };',
  12235. ' this.DoThat = function () {',
  12236. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  12237. ' };',
  12238. ' }, true);',
  12239. ' this.i = 0;',
  12240. ' this.$new = function () {',
  12241. ' var r = Object.create(this);',
  12242. ' r.p = this.TPoint.$new();',
  12243. ' return r;',
  12244. ' };',
  12245. ' this.$eq = function (b) {',
  12246. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12247. ' };',
  12248. ' this.$assign = function (s) {',
  12249. ' this.i = s.i;',
  12250. ' this.p.$assign(s.p);',
  12251. ' return this;',
  12252. ' };',
  12253. ' this.DoSome = function () {',
  12254. ' this.p.x = this.p.y + 1;',
  12255. ' this.TPoint.Count = this.p.Count + 2;',
  12256. ' };',
  12257. '}, true);',
  12258. 'this.r = this.TRec.$clone({',
  12259. ' i: 1,',
  12260. ' p: this.TRec.TPoint.$clone({',
  12261. ' x: 21,',
  12262. ' y: 22',
  12263. ' })',
  12264. '});',
  12265. '']),
  12266. LinesToStr([ // $mod.$main
  12267. '']));
  12268. end;
  12269. procedure TTestModule.TestAdvRecord_SubClass;
  12270. begin
  12271. StartProgram(false);
  12272. Add([
  12273. '{$modeswitch AdvancedRecords}',
  12274. 'type',
  12275. ' TObject = class end;',
  12276. ' TPoint = record',
  12277. ' type',
  12278. ' TBird = class',
  12279. ' procedure DoIt;',
  12280. ' class procedure Glob;',
  12281. ' end;',
  12282. ' procedure DoIt(b: TBird);',
  12283. ' end;',
  12284. 'procedure TPoint.TBird.DoIt;',
  12285. 'begin',
  12286. ' doit;',
  12287. ' self.doit;',
  12288. ' glob;',
  12289. ' self.glob;',
  12290. 'end;',
  12291. 'class procedure TPoint.TBird.Glob;',
  12292. 'begin',
  12293. ' glob;',
  12294. ' self.glob;',
  12295. 'end;',
  12296. 'procedure TPoint.DoIt(b: TBird);',
  12297. 'begin',
  12298. ' b.doit;',
  12299. ' b.glob;',
  12300. ' TBird.glob;',
  12301. 'end;',
  12302. 'begin',
  12303. '']);
  12304. ConvertProgram;
  12305. CheckSource('TestAdvRecord_SubClass',
  12306. LinesToStr([ // statements
  12307. 'rtl.createClass(this, "TObject", null, function () {',
  12308. ' this.$init = function () {',
  12309. ' };',
  12310. ' this.$final = function () {',
  12311. ' };',
  12312. '});',
  12313. 'rtl.recNewT(this, "TPoint", function () {',
  12314. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12315. ' this.DoIt = function () {',
  12316. ' this.DoIt();',
  12317. ' this.DoIt();',
  12318. ' this.$class.Glob();',
  12319. ' this.$class.Glob();',
  12320. ' };',
  12321. ' this.Glob = function () {',
  12322. ' this.Glob();',
  12323. ' this.Glob();',
  12324. ' };',
  12325. ' }, "TPoint.TBird");',
  12326. ' this.$eq = function (b) {',
  12327. ' return true;',
  12328. ' };',
  12329. ' this.$assign = function (s) {',
  12330. ' return this;',
  12331. ' };',
  12332. ' this.DoIt = function (b) {',
  12333. ' b.DoIt();',
  12334. ' b.$class.Glob();',
  12335. ' this.TBird.Glob();',
  12336. ' };',
  12337. '}, true);',
  12338. '']),
  12339. LinesToStr([ // $mod.$main
  12340. '']));
  12341. end;
  12342. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12343. begin
  12344. StartProgram(false);
  12345. Add([
  12346. '{$modeswitch AdvancedRecords}',
  12347. 'type',
  12348. ' IUnknown = interface end;',
  12349. ' TPoint = record',
  12350. ' type IBird = interface end;',
  12351. ' end;',
  12352. 'begin',
  12353. '']);
  12354. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12355. nNotYetImplemented);
  12356. ParseProgram;
  12357. end;
  12358. procedure TTestModule.TestAdvRecord_Constructor;
  12359. begin
  12360. StartProgram(false);
  12361. Add([
  12362. '{$modeswitch AdvancedRecords}',
  12363. 'type',
  12364. ' TPoint = record',
  12365. ' x,y: longint;',
  12366. ' class procedure Run(w: longint = 13); static;',
  12367. ' constructor Create(ax: longint; ay: longint = -1);',
  12368. ' end;',
  12369. 'class procedure tpoint.run(w: longint);',
  12370. 'begin',
  12371. ' run;',
  12372. ' run();',
  12373. 'end;',
  12374. 'constructor tpoint.create(ax,ay: longint);',
  12375. 'begin',
  12376. ' x:=ax;',
  12377. ' self.y:=ay;',
  12378. ' run;',
  12379. ' run(ax);',
  12380. 'end;',
  12381. 'var r: TPoint;',
  12382. 'begin',
  12383. ' r:=TPoint.Create(1,2);',
  12384. ' with TPoint do r:=Create(1,2);',
  12385. ' r.Create(3);',
  12386. ' r:=r.Create(4);',
  12387. '']);
  12388. ConvertProgram;
  12389. CheckSource('TestAdvRecord_Constructor',
  12390. LinesToStr([ // statements
  12391. 'rtl.recNewT(this, "TPoint", function () {',
  12392. ' this.x = 0;',
  12393. ' this.y = 0;',
  12394. ' this.$eq = function (b) {',
  12395. ' return (this.x === b.x) && (this.y === b.y);',
  12396. ' };',
  12397. ' this.$assign = function (s) {',
  12398. ' this.x = s.x;',
  12399. ' this.y = s.y;',
  12400. ' return this;',
  12401. ' };',
  12402. ' this.Run = function (w) {',
  12403. ' $mod.TPoint.Run(13);',
  12404. ' $mod.TPoint.Run(13);',
  12405. ' };',
  12406. ' this.Create = function (ax, ay) {',
  12407. ' this.x = ax;',
  12408. ' this.y = ay;',
  12409. ' this.Run(13);',
  12410. ' this.Run(ax);',
  12411. ' return this;',
  12412. ' };',
  12413. '});',
  12414. 'this.r = this.TPoint.$new();',
  12415. '']),
  12416. LinesToStr([ // $mod.$main
  12417. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12418. 'var $with = $mod.TPoint;',
  12419. '$mod.r.$assign($with.$new().Create(1, 2));',
  12420. '$mod.r.Create(3, -1);',
  12421. '$mod.r.$assign($mod.r.Create(4, -1));',
  12422. '']));
  12423. end;
  12424. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12425. begin
  12426. StartProgram(false);
  12427. Add([
  12428. '{$modeswitch AdvancedRecords}',
  12429. 'type',
  12430. ' TPoint = record',
  12431. ' class var x: longint;',
  12432. ' class procedure Fly; static;',
  12433. ' class constructor Init;',
  12434. ' end;',
  12435. 'var count: word;',
  12436. 'class procedure Tpoint.Fly;',
  12437. 'begin',
  12438. 'end;',
  12439. 'class constructor tpoint.init;',
  12440. 'begin',
  12441. ' count:=count+1;',
  12442. ' x:=x+3;',
  12443. ' tpoint.x:=tpoint.x+4;',
  12444. ' fly;',
  12445. ' tpoint.fly;',
  12446. 'end;',
  12447. 'var r: TPoint;',
  12448. 'begin',
  12449. ' r.x:=r.x+10;',
  12450. ' r.Fly;',
  12451. ' r.Fly();',
  12452. '']);
  12453. ConvertProgram;
  12454. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12455. LinesToStr([ // statements
  12456. 'rtl.recNewT(this, "TPoint", function () {',
  12457. ' this.x = 0;',
  12458. ' this.$eq = function (b) {',
  12459. ' return true;',
  12460. ' };',
  12461. ' this.$assign = function (s) {',
  12462. ' return this;',
  12463. ' };',
  12464. ' this.Fly = function () {',
  12465. ' };',
  12466. '}, true);',
  12467. 'this.count = 0;',
  12468. 'this.r = this.TPoint.$new();',
  12469. '']),
  12470. LinesToStr([ // $mod.$main
  12471. '(function () {',
  12472. ' $mod.count = $mod.count + 1;',
  12473. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12474. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12475. ' $mod.TPoint.Fly();',
  12476. ' $mod.TPoint.Fly();',
  12477. '})();',
  12478. '$mod.TPoint.x = $mod.r.x + 10;',
  12479. '$mod.TPoint.Fly();',
  12480. '$mod.TPoint.Fly();',
  12481. '']));
  12482. end;
  12483. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12484. begin
  12485. StartUnit(false);
  12486. Add([
  12487. 'interface',
  12488. '{$modeswitch AdvancedRecords}',
  12489. 'type',
  12490. ' TPoint = record',
  12491. ' class var x: longint;',
  12492. ' class procedure Fly; static;',
  12493. ' class constructor Init;',
  12494. ' end;',
  12495. 'implementation',
  12496. 'var count: word;',
  12497. 'class procedure Tpoint.Fly;',
  12498. 'begin',
  12499. 'end;',
  12500. 'class constructor tpoint.init;',
  12501. 'begin',
  12502. ' count:=count+1;',
  12503. ' x:=3;',
  12504. ' tpoint.x:=4;',
  12505. ' fly;',
  12506. ' tpoint.fly;',
  12507. 'end;',
  12508. '']);
  12509. ConvertUnit;
  12510. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12511. LinesToStr([ // statements
  12512. 'var $impl = $mod.$impl;',
  12513. 'rtl.recNewT(this, "TPoint", function () {',
  12514. ' this.x = 0;',
  12515. ' this.$eq = function (b) {',
  12516. ' return true;',
  12517. ' };',
  12518. ' this.$assign = function (s) {',
  12519. ' return this;',
  12520. ' };',
  12521. ' this.Fly = function () {',
  12522. ' };',
  12523. '}, true);',
  12524. '']),
  12525. LinesToStr([ // $mod.$init
  12526. '(function () {',
  12527. ' $impl.count = $impl.count + 1;',
  12528. ' $mod.TPoint.x = 3;',
  12529. ' $mod.TPoint.x = 4;',
  12530. ' $mod.TPoint.Fly();',
  12531. ' $mod.TPoint.Fly();',
  12532. '})();',
  12533. '']),
  12534. LinesToStr([ // $mod.$main
  12535. '$impl.count = 0;',
  12536. '']));
  12537. end;
  12538. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12539. begin
  12540. StartProgram(false);
  12541. Add(['type',
  12542. ' TObject = class',
  12543. ' public',
  12544. ' constructor Create;',
  12545. ' destructor Destroy;',
  12546. ' end;',
  12547. ' TBird = TObject;',
  12548. 'constructor tobject.create;',
  12549. 'begin end;',
  12550. 'destructor tobject.destroy;',
  12551. 'begin end;',
  12552. 'var Obj: tobject;',
  12553. 'begin',
  12554. ' obj:=tobject.create;',
  12555. ' obj:=tobject.create();',
  12556. ' obj:=tbird.create;',
  12557. ' obj:=tbird.create();',
  12558. ' obj:=obj.create();',
  12559. ' obj.destroy;',
  12560. '']);
  12561. ConvertProgram;
  12562. CheckSource('TestClass_TObjectDefaultConstructor',
  12563. LinesToStr([ // statements
  12564. 'rtl.createClass(this,"TObject",null,function(){',
  12565. ' this.$init = function () {',
  12566. ' };',
  12567. ' this.$final = function () {',
  12568. ' };',
  12569. ' this.Create = function(){',
  12570. ' return this;',
  12571. ' };',
  12572. ' this.Destroy = function(){',
  12573. ' };',
  12574. '});',
  12575. 'this.Obj = null;'
  12576. ]),
  12577. LinesToStr([ // $mod.$main
  12578. '$mod.Obj = $mod.TObject.$create("Create");',
  12579. '$mod.Obj = $mod.TObject.$create("Create");',
  12580. '$mod.Obj = $mod.TObject.$create("Create");',
  12581. '$mod.Obj = $mod.TObject.$create("Create");',
  12582. '$mod.Obj = $mod.Obj.Create();',
  12583. '$mod.Obj.$destroy("Destroy");',
  12584. '']));
  12585. end;
  12586. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12587. begin
  12588. StartProgram(false);
  12589. Add('type');
  12590. Add(' TObject = class');
  12591. Add(' public');
  12592. Add(' constructor Create(Par: longint);');
  12593. Add(' end;');
  12594. Add('constructor tobject.create(par: longint);');
  12595. Add('begin end;');
  12596. Add('var Obj: tobject;');
  12597. Add('begin');
  12598. Add(' obj:=tobject.create(3);');
  12599. ConvertProgram;
  12600. CheckSource('TestClass_TObjectConstructorWithParams',
  12601. LinesToStr([ // statements
  12602. 'rtl.createClass(this,"TObject",null,function(){',
  12603. ' this.$init = function () {',
  12604. ' };',
  12605. ' this.$final = function () {',
  12606. ' };',
  12607. ' this.Create = function(Par){',
  12608. ' return this;',
  12609. ' };',
  12610. '});',
  12611. 'this.Obj = null;'
  12612. ]),
  12613. LinesToStr([ // $mod.$main
  12614. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12615. ]));
  12616. end;
  12617. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12618. begin
  12619. StartProgram(false);
  12620. Add('type');
  12621. Add(' TObject = class');
  12622. Add(' public');
  12623. Add(' constructor Create;');
  12624. Add(' end;');
  12625. Add(' TTest = class(TObject)');
  12626. Add(' public');
  12627. Add(' constructor Create(const Par: longint = 1);');
  12628. Add(' end;');
  12629. Add('constructor tobject.create;');
  12630. Add('begin end;');
  12631. Add('constructor ttest.create(const par: longint);');
  12632. Add('begin end;');
  12633. Add('var t: ttest;');
  12634. Add('begin');
  12635. Add(' t:=ttest.create;');
  12636. Add(' t:=ttest.create(2);');
  12637. ConvertProgram;
  12638. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12639. LinesToStr([ // statements
  12640. 'rtl.createClass(this,"TObject",null,function(){',
  12641. ' this.$init = function () {',
  12642. ' };',
  12643. ' this.$final = function () {',
  12644. ' };',
  12645. ' this.Create = function(){',
  12646. ' return this;',
  12647. ' };',
  12648. '});',
  12649. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12650. ' this.Create$1 = function (Par) {',
  12651. ' return this;',
  12652. ' };',
  12653. '});',
  12654. 'this.t = null;'
  12655. ]),
  12656. LinesToStr([ // $mod.$main
  12657. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12658. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12659. ]));
  12660. end;
  12661. procedure TTestModule.TestClass_Var;
  12662. begin
  12663. StartProgram(false);
  12664. Add([
  12665. 'type',
  12666. ' TObject = class',
  12667. ' public',
  12668. ' vI: longint;',
  12669. ' constructor Create(Par: longint);',
  12670. ' end;',
  12671. 'constructor tobject.create(par: longint);',
  12672. 'begin',
  12673. ' vi:=par+3',
  12674. 'end;',
  12675. 'var Obj: tobject;',
  12676. 'begin',
  12677. ' obj:=tobject.create(4);',
  12678. ' obj.vi:=obj.VI+5;']);
  12679. ConvertProgram;
  12680. CheckSource('TestClass_Var',
  12681. LinesToStr([ // statements
  12682. 'rtl.createClass(this,"TObject",null,function(){',
  12683. ' this.$init = function () {',
  12684. ' this.vI = 0;',
  12685. ' };',
  12686. ' this.$final = function () {',
  12687. ' };',
  12688. ' this.Create = function(Par){',
  12689. ' this.vI = Par+3;',
  12690. ' return this;',
  12691. ' };',
  12692. '});',
  12693. 'this.Obj = null;'
  12694. ]),
  12695. LinesToStr([ // $mod.$main
  12696. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12697. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12698. ]));
  12699. end;
  12700. procedure TTestModule.TestClass_Method;
  12701. begin
  12702. StartProgram(false);
  12703. Add('type');
  12704. Add(' TObject = class');
  12705. Add(' public');
  12706. Add(' vI: longint;');
  12707. Add(' Sub: TObject;');
  12708. Add(' constructor Create;');
  12709. Add(' function GetIt(Par: longint): tobject;');
  12710. Add(' end;');
  12711. Add('constructor tobject.create; begin end;');
  12712. Add('function tobject.getit(par: longint): tobject;');
  12713. Add('begin');
  12714. Add(' Self.vi:=par+3;');
  12715. Add(' Result:=self.sub;');
  12716. Add('end;');
  12717. Add('var Obj: tobject;');
  12718. Add('begin');
  12719. Add(' obj:=tobject.create;');
  12720. Add(' obj.getit(4);');
  12721. Add(' obj.sub.sub:=nil;');
  12722. Add(' obj.sub.getit(5);');
  12723. Add(' obj.sub.getit(6).SUB:=nil;');
  12724. Add(' obj.sub.getit(7).GETIT(8);');
  12725. Add(' obj.sub.getit(9).SuB.getit(10);');
  12726. ConvertProgram;
  12727. CheckSource('TestClass_Method',
  12728. LinesToStr([ // statements
  12729. 'rtl.createClass(this,"TObject",null,function(){',
  12730. ' this.$init = function () {',
  12731. ' this.vI = 0;',
  12732. ' this.Sub = null;',
  12733. ' };',
  12734. ' this.$final = function () {',
  12735. ' this.Sub = undefined;',
  12736. ' };',
  12737. ' this.Create = function(){',
  12738. ' return this;',
  12739. ' };',
  12740. ' this.GetIt = function(Par){',
  12741. ' var Result = null;',
  12742. ' this.vI = Par + 3;',
  12743. ' Result = this.Sub;',
  12744. ' return Result;',
  12745. ' };',
  12746. '});',
  12747. 'this.Obj = null;'
  12748. ]),
  12749. LinesToStr([ // $mod.$main
  12750. '$mod.Obj = $mod.TObject.$create("Create");',
  12751. '$mod.Obj.GetIt(4);',
  12752. '$mod.Obj.Sub.Sub=null;',
  12753. '$mod.Obj.Sub.GetIt(5);',
  12754. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12755. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12756. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12757. ]));
  12758. end;
  12759. procedure TTestModule.TestClass_Implementation;
  12760. begin
  12761. StartUnit(false);
  12762. Add([
  12763. 'interface',
  12764. 'type',
  12765. ' TObject = class',
  12766. ' constructor Create;',
  12767. ' end;',
  12768. 'implementation',
  12769. 'type',
  12770. ' TIntClass = class',
  12771. ' constructor Create; reintroduce;',
  12772. ' class procedure DoGlob;',
  12773. ' end;',
  12774. 'constructor tintclass.create;',
  12775. 'begin',
  12776. ' inherited;',
  12777. ' inherited create;',
  12778. ' doglob;',
  12779. 'end;',
  12780. 'class procedure tintclass.doglob;',
  12781. 'begin',
  12782. 'end;',
  12783. 'constructor tobject.create;',
  12784. 'var',
  12785. ' iC: tintclass;',
  12786. 'begin',
  12787. ' ic:=tintclass.create;',
  12788. ' tintclass.doglob;',
  12789. ' ic.doglob;',
  12790. 'end;',
  12791. 'initialization',
  12792. ' tintclass.doglob;',
  12793. '']);
  12794. ConvertUnit;
  12795. CheckSource('TestClass_Implementation',
  12796. LinesToStr([ // statements
  12797. 'var $impl = $mod.$impl;',
  12798. 'rtl.createClass(this, "TObject", null, function () {',
  12799. ' this.$init = function () {',
  12800. ' };',
  12801. ' this.$final = function () {',
  12802. ' };',
  12803. ' this.Create = function () {',
  12804. ' var iC = null;',
  12805. ' iC = $impl.TIntClass.$create("Create$1");',
  12806. ' $impl.TIntClass.DoGlob();',
  12807. ' iC.$class.DoGlob();',
  12808. ' return this;',
  12809. ' };',
  12810. '});',
  12811. '']),
  12812. LinesToStr([ // $mod.$main
  12813. '$impl.TIntClass.DoGlob();',
  12814. '']),
  12815. LinesToStr([
  12816. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12817. ' this.Create$1 = function () {',
  12818. ' $mod.TObject.Create.call(this);',
  12819. ' $mod.TObject.Create.call(this);',
  12820. ' this.$class.DoGlob();',
  12821. ' return this;',
  12822. ' };',
  12823. ' this.DoGlob = function () {',
  12824. ' };',
  12825. '});',
  12826. '']));
  12827. end;
  12828. procedure TTestModule.TestClass_Inheritance;
  12829. begin
  12830. StartProgram(false);
  12831. Add('type');
  12832. Add(' TObject = class');
  12833. Add(' public');
  12834. Add(' constructor Create;');
  12835. Add(' end;');
  12836. Add(' TClassA = class');
  12837. Add(' end;');
  12838. Add(' TClassB = class(TObject)');
  12839. Add(' procedure ProcB;');
  12840. Add(' end;');
  12841. Add('constructor tobject.create; begin end;');
  12842. Add('procedure tclassb.procb; begin end;');
  12843. Add('var');
  12844. Add(' oO: TObject;');
  12845. Add(' oA: TClassA;');
  12846. Add(' oB: TClassB;');
  12847. Add('begin');
  12848. Add(' oO:=tobject.Create;');
  12849. Add(' oA:=tclassa.Create;');
  12850. Add(' ob:=tclassb.Create;');
  12851. Add(' if oo is tclassa then ;');
  12852. Add(' ob:=oo as tclassb;');
  12853. Add(' (oo as tclassb).procb;');
  12854. ConvertProgram;
  12855. CheckSource('TestClass_Inheritance',
  12856. LinesToStr([ // statements
  12857. 'rtl.createClass(this,"TObject",null,function(){',
  12858. ' this.$init = function () {',
  12859. ' };',
  12860. ' this.$final = function () {',
  12861. ' };',
  12862. ' this.Create = function () {',
  12863. ' return this;',
  12864. ' };',
  12865. '});',
  12866. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12867. '});',
  12868. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12869. ' this.ProcB = function () {',
  12870. ' };',
  12871. '});',
  12872. 'this.oO = null;',
  12873. 'this.oA = null;',
  12874. 'this.oB = null;'
  12875. ]),
  12876. LinesToStr([ // $mod.$main
  12877. '$mod.oO = $mod.TObject.$create("Create");',
  12878. '$mod.oA = $mod.TClassA.$create("Create");',
  12879. '$mod.oB = $mod.TClassB.$create("Create");',
  12880. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12881. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12882. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12883. ]));
  12884. end;
  12885. procedure TTestModule.TestClass_TypeAlias;
  12886. begin
  12887. StartProgram(false);
  12888. Add([
  12889. '{$interfaces corba}',
  12890. 'type',
  12891. ' IObject = interface',
  12892. ' end;',
  12893. ' IBird = type IObject;',
  12894. ' TObject = class',
  12895. ' end;',
  12896. ' TBird = type TObject;',
  12897. 'var',
  12898. ' oObj: TObject;',
  12899. ' oBird: TBird;',
  12900. ' IntfObj: IObject;',
  12901. ' IntfBird: IBird;',
  12902. 'begin',
  12903. ' oObj:=oBird;',
  12904. '']);
  12905. ConvertProgram;
  12906. CheckSource('TestClass_TypeAlias',
  12907. LinesToStr([ // statements
  12908. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12909. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12910. 'rtl.createClass(this, "TObject", null, function () {',
  12911. ' this.$init = function () {',
  12912. ' };',
  12913. ' this.$final = function () {',
  12914. ' };',
  12915. '});',
  12916. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12917. '});',
  12918. 'this.oObj = null;',
  12919. 'this.oBird = null;',
  12920. 'this.IntfObj = null;',
  12921. 'this.IntfBird = null;',
  12922. '']),
  12923. LinesToStr([ // $mod.$main
  12924. '$mod.oObj = $mod.oBird;',
  12925. '']));
  12926. end;
  12927. procedure TTestModule.TestClass_AbstractMethod;
  12928. begin
  12929. StartProgram(false);
  12930. Add('type');
  12931. Add(' TObject = class');
  12932. Add(' public');
  12933. Add(' procedure DoIt; virtual; abstract;');
  12934. Add(' end;');
  12935. Add('begin');
  12936. ConvertProgram;
  12937. CheckSource('TestClass_AbstractMethod',
  12938. LinesToStr([ // statements
  12939. 'rtl.createClass(this,"TObject",null,function(){',
  12940. ' this.$init = function () {',
  12941. ' };',
  12942. ' this.$final = function () {',
  12943. ' };',
  12944. '});'
  12945. ]),
  12946. LinesToStr([ // this.$main
  12947. ''
  12948. ]));
  12949. end;
  12950. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12951. begin
  12952. StartProgram(false);
  12953. Add([
  12954. 'type',
  12955. ' TObject = class',
  12956. ' procedure DoAbstract; virtual; abstract;',
  12957. ' procedure DoVirtual; virtual;',
  12958. ' procedure DoIt;',
  12959. ' end;',
  12960. ' TA = class',
  12961. ' procedure doabstract; override;',
  12962. ' procedure dovirtual; override;',
  12963. ' procedure DoSome;',
  12964. ' end;',
  12965. 'procedure tobject.dovirtual;',
  12966. 'begin',
  12967. ' inherited; // call non existing ancestor -> ignore silently',
  12968. 'end;',
  12969. 'procedure tobject.doit;',
  12970. 'begin',
  12971. 'end;',
  12972. 'procedure ta.doabstract;',
  12973. 'begin',
  12974. ' inherited dovirtual; // call TObject.DoVirtual',
  12975. 'end;',
  12976. 'procedure ta.dovirtual;',
  12977. 'begin',
  12978. ' inherited; // call TObject.DoVirtual',
  12979. ' inherited dovirtual; // call TObject.DoVirtual',
  12980. ' inherited dovirtual(); // call TObject.DoVirtual',
  12981. ' doit;',
  12982. ' doit();',
  12983. 'end;',
  12984. 'procedure ta.dosome;',
  12985. 'begin',
  12986. ' inherited; // call non existing ancestor method -> silently ignore',
  12987. 'end;',
  12988. 'begin']);
  12989. ConvertProgram;
  12990. CheckSource('TestClass_CallInherited_ProcNoParams',
  12991. LinesToStr([ // statements
  12992. 'rtl.createClass(this,"TObject",null,function(){',
  12993. ' this.$init = function () {',
  12994. ' };',
  12995. ' this.$final = function () {',
  12996. ' };',
  12997. ' this.DoVirtual = function () {',
  12998. ' };',
  12999. ' this.DoIt = function () {',
  13000. ' };',
  13001. '});',
  13002. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13003. ' this.DoAbstract = function () {',
  13004. ' $mod.TObject.DoVirtual.call(this);',
  13005. ' };',
  13006. ' this.DoVirtual = function () {',
  13007. ' $mod.TObject.DoVirtual.call(this);',
  13008. ' $mod.TObject.DoVirtual.call(this);',
  13009. ' $mod.TObject.DoVirtual.call(this);',
  13010. ' this.DoIt();',
  13011. ' this.DoIt();',
  13012. ' };',
  13013. ' this.DoSome = function () {',
  13014. ' };',
  13015. '});'
  13016. ]),
  13017. LinesToStr([ // this.$main
  13018. ''
  13019. ]));
  13020. end;
  13021. procedure TTestModule.TestClass_CallInherited_WithParams;
  13022. begin
  13023. StartProgram(false);
  13024. Add([
  13025. 'type',
  13026. ' TObject = class',
  13027. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13028. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13029. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13030. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13031. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13032. ' end;',
  13033. ' TClassA = class',
  13034. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13035. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13036. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13037. ' end;',
  13038. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13039. 'begin',
  13040. 'end;',
  13041. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13042. 'begin',
  13043. 'end;',
  13044. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13045. 'begin',
  13046. 'end;',
  13047. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13048. 'begin',
  13049. 'end;',
  13050. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13051. 'begin',
  13052. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13053. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13054. 'end;',
  13055. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13056. 'begin',
  13057. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13058. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13059. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13060. ' doit(pa,pb);',
  13061. ' doit(pa);',
  13062. ' doit2(pa);',
  13063. ' doit2;',
  13064. 'end;',
  13065. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13066. 'begin',
  13067. ' pa:=inherited;',
  13068. 'end;',
  13069. 'begin']);
  13070. ConvertProgram;
  13071. CheckSource('TestClass_CallInherited_WithParams',
  13072. LinesToStr([ // statements
  13073. 'rtl.createClass(this,"TObject",null,function(){',
  13074. ' this.$init = function () {',
  13075. ' };',
  13076. ' this.$final = function () {',
  13077. ' };',
  13078. ' this.DoVirtual = function (pA,pB) {',
  13079. ' };',
  13080. ' this.DoIt = function (pA,pB) {',
  13081. ' };',
  13082. ' this.DoIt2 = function (pA,pB) {',
  13083. ' };',
  13084. ' this.GetIt = function (pA, pB) {',
  13085. ' var Result = 0;',
  13086. ' return Result;',
  13087. ' };',
  13088. '});',
  13089. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13090. ' this.DoAbstract = function (pA,pB) {',
  13091. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13092. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13093. ' };',
  13094. ' this.DoVirtual = function (pA,pB) {',
  13095. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  13096. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13097. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13098. ' this.DoIt(pA,pB);',
  13099. ' this.DoIt(pA,0);',
  13100. ' this.DoIt2(pA,2);',
  13101. ' this.DoIt2(1,2);',
  13102. ' };',
  13103. ' this.GetIt$1 = function (pA, pB) {',
  13104. ' var Result = 0;',
  13105. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  13106. ' return Result;',
  13107. ' };',
  13108. '});'
  13109. ]),
  13110. LinesToStr([ // this.$main
  13111. ''
  13112. ]));
  13113. end;
  13114. procedure TTestModule.TestClasS_CallInheritedConstructor;
  13115. begin
  13116. StartProgram(false);
  13117. Add('type');
  13118. Add(' TObject = class');
  13119. Add(' constructor Create; virtual;');
  13120. Add(' constructor CreateWithB(b: boolean);');
  13121. Add(' end;');
  13122. Add(' TA = class');
  13123. Add(' constructor Create; override;');
  13124. Add(' constructor CreateWithC(c: char);');
  13125. Add(' procedure DoIt;');
  13126. Add(' class function DoSome: TObject;');
  13127. Add(' end;');
  13128. Add('constructor tobject.create;');
  13129. Add('begin');
  13130. Add(' inherited; // call non existing ancestor -> ignore silently');
  13131. Add('end;');
  13132. Add('constructor tobject.createwithb(b: boolean);');
  13133. Add('begin');
  13134. Add(' inherited; // call non existing ancestor -> ignore silently');
  13135. Add(' create; // normal call');
  13136. Add('end;');
  13137. Add('constructor ta.create;');
  13138. Add('begin');
  13139. Add(' inherited; // normal call TObject.Create');
  13140. Add(' inherited create; // normal call TObject.Create');
  13141. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  13142. Add('end;');
  13143. Add('constructor ta.createwithc(c: char);');
  13144. Add('begin');
  13145. Add(' inherited create; // call TObject.Create');
  13146. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  13147. Add(' doit;');
  13148. Add(' doit();');
  13149. Add(' dosome;');
  13150. Add('end;');
  13151. Add('procedure ta.doit;');
  13152. Add('begin');
  13153. Add(' create; // normal call');
  13154. Add(' createwithb(false); // normal call');
  13155. Add(' createwithc(''c''); // normal call');
  13156. Add('end;');
  13157. Add('class function ta.dosome: TObject;');
  13158. Add('begin');
  13159. Add(' Result:=create; // constructor');
  13160. Add(' Result:=createwithb(true); // constructor');
  13161. Add(' Result:=createwithc(''c''); // constructor');
  13162. Add('end;');
  13163. Add('begin');
  13164. ConvertProgram;
  13165. CheckSource('TestClass_CallInheritedConstructor',
  13166. LinesToStr([ // statements
  13167. 'rtl.createClass(this,"TObject",null,function(){',
  13168. ' this.$init = function () {',
  13169. ' };',
  13170. ' this.$final = function () {',
  13171. ' };',
  13172. ' this.Create = function () {',
  13173. ' return this;',
  13174. ' };',
  13175. ' this.CreateWithB = function (b) {',
  13176. ' this.Create();',
  13177. ' return this;',
  13178. ' };',
  13179. '});',
  13180. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13181. ' this.Create = function () {',
  13182. ' $mod.TObject.Create.call(this);',
  13183. ' $mod.TObject.Create.call(this);',
  13184. ' $mod.TObject.CreateWithB.call(this, false);',
  13185. ' return this;',
  13186. ' };',
  13187. ' this.CreateWithC = function (c) {',
  13188. ' $mod.TObject.Create.call(this);',
  13189. ' $mod.TObject.CreateWithB.call(this, true);',
  13190. ' this.DoIt();',
  13191. ' this.DoIt();',
  13192. ' this.$class.DoSome();',
  13193. ' return this;',
  13194. ' };',
  13195. ' this.DoIt = function () {',
  13196. ' this.Create();',
  13197. ' this.CreateWithB(false);',
  13198. ' this.CreateWithC("c");',
  13199. ' };',
  13200. ' this.DoSome = function () {',
  13201. ' var Result = null;',
  13202. ' Result = this.$create("Create");',
  13203. ' Result = this.$create("CreateWithB", [true]);',
  13204. ' Result = this.$create("CreateWithC", ["c"]);',
  13205. ' return Result;',
  13206. ' };',
  13207. '});'
  13208. ]),
  13209. LinesToStr([ // this.$main
  13210. ''
  13211. ]));
  13212. end;
  13213. procedure TTestModule.TestClass_ClassVar_Assign;
  13214. begin
  13215. StartProgram(false);
  13216. Add([
  13217. 'type',
  13218. ' TObject = class',
  13219. ' public',
  13220. ' class var vI: longint;',
  13221. ' class var Sub: TObject;',
  13222. ' constructor Create;',
  13223. ' class function GetIt(var Par: longint): tobject;',
  13224. ' end;',
  13225. 'constructor tobject.create;',
  13226. 'begin',
  13227. ' vi:=vi+1;',
  13228. ' Self.vi:=Self.vi+1;',
  13229. ' inc(vi);',
  13230. 'end;',
  13231. 'class function tobject.getit(var par: longint): tobject;',
  13232. 'begin',
  13233. ' vi:=vi+3;',
  13234. ' Self.vi:=Self.vi+4;',
  13235. ' inc(vi);',
  13236. ' Result:=self.sub;',
  13237. ' GetIt(vi);',
  13238. 'end;',
  13239. 'var Obj: tobject;',
  13240. 'begin',
  13241. ' obj:=tobject.create;',
  13242. ' tobject.vi:=3;',
  13243. ' if tobject.vi=4 then ;',
  13244. ' tobject.sub:=nil;',
  13245. ' obj.sub:=nil;',
  13246. ' obj.sub.sub:=nil;']);
  13247. ConvertProgram;
  13248. CheckSource('TestClass_ClassVar_Assign',
  13249. LinesToStr([ // statements
  13250. 'rtl.createClass(this,"TObject",null,function(){',
  13251. ' this.vI = 0;',
  13252. ' this.Sub = null;',
  13253. ' this.$init = function () {',
  13254. ' };',
  13255. ' this.$final = function () {',
  13256. ' };',
  13257. ' this.Create = function(){',
  13258. ' $mod.TObject.vI = this.vI+1;',
  13259. ' $mod.TObject.vI = this.vI+1;',
  13260. ' $mod.TObject.vI += 1;',
  13261. ' return this;',
  13262. ' };',
  13263. ' this.GetIt = function(Par){',
  13264. ' var Result = null;',
  13265. ' $mod.TObject.vI = this.vI + 3;',
  13266. ' $mod.TObject.vI = this.vI + 4;',
  13267. ' $mod.TObject.vI += 1;',
  13268. ' Result = this.Sub;',
  13269. ' this.GetIt({',
  13270. ' p: $mod.TObject,',
  13271. ' get: function () {',
  13272. ' return this.p.vI;',
  13273. ' },',
  13274. ' set: function (v) {',
  13275. ' this.p.vI = v;',
  13276. ' }',
  13277. ' });',
  13278. ' return Result;',
  13279. ' };',
  13280. '});',
  13281. 'this.Obj = null;'
  13282. ]),
  13283. LinesToStr([ // $mod.$main
  13284. '$mod.Obj = $mod.TObject.$create("Create");',
  13285. '$mod.TObject.vI = 3;',
  13286. 'if ($mod.TObject.vI === 4);',
  13287. '$mod.TObject.Sub=null;',
  13288. '$mod.TObject.Sub=null;',
  13289. '$mod.TObject.Sub=null;',
  13290. '']));
  13291. end;
  13292. procedure TTestModule.TestClass_CallClassMethod;
  13293. begin
  13294. StartProgram(false);
  13295. Add('type');
  13296. Add(' TObject = class');
  13297. Add(' public');
  13298. Add(' class var vI: longint;');
  13299. Add(' class var Sub: TObject;');
  13300. Add(' constructor Create;');
  13301. Add(' function GetMore(Par: longint): longint;');
  13302. Add(' class function GetIt(Par: longint): tobject;');
  13303. Add(' end;');
  13304. Add('constructor tobject.create;');
  13305. Add('begin');
  13306. Add(' sub:=getit(3);');
  13307. Add(' vi:=getmore(4);');
  13308. Add(' sub:=Self.getit(5);');
  13309. Add(' vi:=Self.getmore(6);');
  13310. Add('end;');
  13311. Add('function tobject.getmore(par: longint): longint;');
  13312. Add('begin');
  13313. Add(' sub:=getit(11);');
  13314. Add(' vi:=getmore(12);');
  13315. Add(' sub:=self.getit(13);');
  13316. Add(' vi:=self.getmore(14);');
  13317. Add('end;');
  13318. Add('class function tobject.getit(par: longint): tobject;');
  13319. Add('begin');
  13320. Add(' sub:=getit(21);');
  13321. Add(' vi:=sub.getmore(22);');
  13322. Add(' sub:=self.getit(23);');
  13323. Add(' vi:=self.sub.getmore(24);');
  13324. Add('end;');
  13325. Add('var Obj: tobject;');
  13326. Add('begin');
  13327. Add(' obj:=tobject.create;');
  13328. Add(' tobject.getit(5);');
  13329. Add(' obj.getit(6);');
  13330. Add(' obj.sub.getit(7);');
  13331. Add(' obj.sub.getit(8).SUB:=nil;');
  13332. Add(' obj.sub.getit(9).GETIT(10);');
  13333. Add(' obj.sub.getit(11).SuB.getit(12);');
  13334. ConvertProgram;
  13335. CheckSource('TestClass_CallClassMethod',
  13336. LinesToStr([ // statements
  13337. 'rtl.createClass(this,"TObject",null,function(){',
  13338. ' this.vI = 0;',
  13339. ' this.Sub = null;',
  13340. ' this.$init = function () {',
  13341. ' };',
  13342. ' this.$final = function () {',
  13343. ' };',
  13344. ' this.Create = function(){',
  13345. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13346. ' $mod.TObject.vI = this.GetMore(4);',
  13347. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13348. ' $mod.TObject.vI = this.GetMore(6);',
  13349. ' return this;',
  13350. ' };',
  13351. ' this.GetMore = function(Par){',
  13352. ' var Result = 0;',
  13353. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13354. ' $mod.TObject.vI = this.GetMore(12);',
  13355. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13356. ' $mod.TObject.vI = this.GetMore(14);',
  13357. ' return Result;',
  13358. ' };',
  13359. ' this.GetIt = function(Par){',
  13360. ' var Result = null;',
  13361. ' $mod.TObject.Sub = this.GetIt(21);',
  13362. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13363. ' $mod.TObject.Sub = this.GetIt(23);',
  13364. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13365. ' return Result;',
  13366. ' };',
  13367. '});',
  13368. 'this.Obj = null;'
  13369. ]),
  13370. LinesToStr([ // $mod.$main
  13371. '$mod.Obj = $mod.TObject.$create("Create");',
  13372. '$mod.TObject.GetIt(5);',
  13373. '$mod.Obj.$class.GetIt(6);',
  13374. '$mod.Obj.Sub.$class.GetIt(7);',
  13375. '$mod.TObject.Sub=null;',
  13376. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13377. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13378. '']));
  13379. end;
  13380. procedure TTestModule.TestClass_CallClassMethodStatic;
  13381. begin
  13382. StartProgram(false);
  13383. Add([
  13384. 'type',
  13385. ' TObject = class',
  13386. ' public',
  13387. ' class function Fly: tobject; static;',
  13388. ' end;',
  13389. 'class function tobject.Fly: tobject;',
  13390. 'begin',
  13391. ' Result.Fly;',
  13392. ' Result.Fly();',
  13393. ' Fly;',
  13394. ' Fly();',
  13395. ' Fly.Fly;',
  13396. ' Fly.Fly();',
  13397. 'end;',
  13398. 'var Obj: tobject;',
  13399. 'begin',
  13400. ' obj.Fly;',
  13401. ' obj.Fly();',
  13402. ' with obj do begin',
  13403. ' Fly;',
  13404. ' Fly();',
  13405. ' end;',
  13406. '']);
  13407. ConvertProgram;
  13408. CheckSource('TestClass_CallClassMethodStatic',
  13409. LinesToStr([ // statements
  13410. 'rtl.createClass(this, "TObject", null, function () {',
  13411. ' this.$init = function () {',
  13412. ' };',
  13413. ' this.$final = function () {',
  13414. ' };',
  13415. ' this.Fly = function () {',
  13416. ' var Result = null;',
  13417. ' $mod.TObject.Fly();',
  13418. ' $mod.TObject.Fly();',
  13419. ' $mod.TObject.Fly();',
  13420. ' $mod.TObject.Fly();',
  13421. ' $mod.TObject.Fly();',
  13422. ' $mod.TObject.Fly();',
  13423. ' return Result;',
  13424. ' };',
  13425. '});',
  13426. 'this.Obj = null;'
  13427. ]),
  13428. LinesToStr([ // $mod.$main
  13429. '$mod.TObject.Fly();',
  13430. '$mod.TObject.Fly();',
  13431. 'var $with = $mod.Obj;',
  13432. '$with.Fly();',
  13433. '$with.Fly();',
  13434. '']));
  13435. end;
  13436. procedure TTestModule.TestClass_Property;
  13437. begin
  13438. StartProgram(false);
  13439. Add('type');
  13440. Add(' TObject = class');
  13441. Add(' Fx: longint;');
  13442. Add(' Fy: longint;');
  13443. Add(' function GetInt: longint;');
  13444. Add(' procedure SetInt(Value: longint);');
  13445. Add(' procedure DoIt;');
  13446. Add(' property IntA: longint read Fx write Fy;');
  13447. Add(' property IntB: longint read GetInt write SetInt;');
  13448. Add(' end;');
  13449. Add('function tobject.getint: longint;');
  13450. Add('begin');
  13451. Add(' result:=fx;');
  13452. Add('end;');
  13453. Add('procedure tobject.setint(value: longint);');
  13454. Add('begin');
  13455. Add(' if value=fy then exit;');
  13456. Add(' fy:=value;');
  13457. Add('end;');
  13458. Add('procedure tobject.doit;');
  13459. Add('begin');
  13460. Add(' IntA:=IntA+1;');
  13461. Add(' Self.IntA:=Self.IntA+1;');
  13462. Add(' IntB:=IntB+1;');
  13463. Add(' Self.IntB:=Self.IntB+1;');
  13464. Add('end;');
  13465. Add('var Obj: tobject;');
  13466. Add('begin');
  13467. Add(' obj.inta:=obj.inta+1;');
  13468. Add(' if obj.intb=2 then;');
  13469. Add(' obj.intb:=obj.intb+2;');
  13470. Add(' obj.setint(obj.inta);');
  13471. ConvertProgram;
  13472. CheckSource('TestClass_Property',
  13473. LinesToStr([ // statements
  13474. 'rtl.createClass(this, "TObject", null, function () {',
  13475. ' this.$init = function () {',
  13476. ' this.Fx = 0;',
  13477. ' this.Fy = 0;',
  13478. ' };',
  13479. ' this.$final = function () {',
  13480. ' };',
  13481. ' this.GetInt = function () {',
  13482. ' var Result = 0;',
  13483. ' Result = this.Fx;',
  13484. ' return Result;',
  13485. ' };',
  13486. ' this.SetInt = function (Value) {',
  13487. ' if (Value === this.Fy) return;',
  13488. ' this.Fy = Value;',
  13489. ' };',
  13490. ' this.DoIt = function () {',
  13491. ' this.Fy = this.Fx + 1;',
  13492. ' this.Fy = this.Fx + 1;',
  13493. ' this.SetInt(this.GetInt() + 1);',
  13494. ' this.SetInt(this.GetInt() + 1);',
  13495. ' };',
  13496. '});',
  13497. 'this.Obj = null;'
  13498. ]),
  13499. LinesToStr([ // $mod.$main
  13500. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13501. 'if ($mod.Obj.GetInt() === 2);',
  13502. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13503. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13504. ]));
  13505. end;
  13506. procedure TTestModule.TestClass_Property_ClassMethod;
  13507. begin
  13508. StartProgram(false);
  13509. Add([
  13510. 'type',
  13511. ' TObject = class',
  13512. ' class var Fx: longint;',
  13513. ' class var Fy: longint;',
  13514. ' class function GetInt: longint;',
  13515. ' class procedure SetInt(Value: longint);',
  13516. ' end;',
  13517. ' TBird = class',
  13518. ' class procedure DoIt;',
  13519. ' class property IntA: longint read Fx write Fy;',
  13520. ' class property IntB: longint read GetInt write SetInt;',
  13521. ' end;',
  13522. 'class function tobject.getint: longint;',
  13523. 'begin',
  13524. ' result:=fx;',
  13525. 'end;',
  13526. 'class procedure tobject.setint(value: longint);',
  13527. 'begin',
  13528. 'end;',
  13529. 'class procedure tbird.doit;',
  13530. 'begin',
  13531. ' FX:=3;',
  13532. ' IntA:=IntA+1;',
  13533. ' Self.IntA:=Self.IntA+1;',
  13534. ' IntB:=IntB+1;',
  13535. ' Self.IntB:=Self.IntB+1;',
  13536. ' with Self do begin',
  13537. ' FX:=11;',
  13538. ' IntA:=IntA+12;',
  13539. ' IntB:=IntB+13;',
  13540. ' end;',
  13541. 'end;',
  13542. 'var Obj: tbird;',
  13543. 'begin',
  13544. ' tbird.fx:=tbird.fx+1;',
  13545. ' tbird.inta:=tbird.inta+1;',
  13546. ' if tbird.intb=2 then;',
  13547. ' tbird.intb:=tbird.intb+2;',
  13548. ' tbird.setint(tbird.inta);',
  13549. ' obj.inta:=obj.inta+1;',
  13550. ' if obj.intb=2 then;',
  13551. ' obj.intb:=obj.intb+2;',
  13552. ' obj.setint(obj.inta);',
  13553. ' with Tbird do begin',
  13554. ' FX:=FY+1;',
  13555. ' inta:=inta+2;',
  13556. ' intb:=intb+3;',
  13557. ' end;',
  13558. ' with Obj do begin',
  13559. ' FX:=FY+1;',
  13560. ' inta:=inta+2;',
  13561. ' intb:=intb+3;',
  13562. ' end;',
  13563. '']);
  13564. ConvertProgram;
  13565. CheckSource('TestClass_Property_ClassMethod',
  13566. LinesToStr([ // statements
  13567. 'rtl.createClass(this, "TObject", null, function () {',
  13568. ' this.Fx = 0;',
  13569. ' this.Fy = 0;',
  13570. ' this.$init = function () {',
  13571. ' };',
  13572. ' this.$final = function () {',
  13573. ' };',
  13574. ' this.GetInt = function () {',
  13575. ' var Result = 0;',
  13576. ' Result = this.Fx;',
  13577. ' return Result;',
  13578. ' };',
  13579. ' this.SetInt = function (Value) {',
  13580. ' };',
  13581. '});',
  13582. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13583. ' this.DoIt = function () {',
  13584. ' $mod.TObject.Fx = 3;',
  13585. ' $mod.TObject.Fy = this.Fx + 1;',
  13586. ' $mod.TObject.Fy = this.Fx + 1;',
  13587. ' this.SetInt(this.GetInt() + 1);',
  13588. ' this.SetInt(this.GetInt() + 1);',
  13589. ' $mod.TObject.Fx = 11;',
  13590. ' $mod.TObject.Fy = this.Fx + 12;',
  13591. ' this.SetInt(this.GetInt() + 13);',
  13592. ' };',
  13593. '});',
  13594. 'this.Obj = null;'
  13595. ]),
  13596. LinesToStr([ // $mod.$main
  13597. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13598. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13599. 'if ($mod.TBird.GetInt() === 2);',
  13600. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13601. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13602. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13603. 'if ($mod.Obj.$class.GetInt() === 2);',
  13604. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13605. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13606. 'var $with = $mod.TBird;',
  13607. '$mod.TObject.Fx = $with.Fy + 1;',
  13608. '$mod.TObject.Fy = $with.Fx + 2;',
  13609. '$with.SetInt($with.GetInt() + 3);',
  13610. 'var $with1 = $mod.Obj;',
  13611. '$mod.TObject.Fx = $with1.Fy + 1;',
  13612. '$mod.TObject.Fy = $with1.Fx + 2;',
  13613. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13614. '']));
  13615. end;
  13616. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  13617. begin
  13618. StartProgram(false);
  13619. Add([
  13620. 'type',
  13621. ' TObject = class',
  13622. ' class function GetInt: longint; static;',
  13623. ' class procedure SetInt(Value: longint); static;',
  13624. ' class function GetItems(Index: word): longint; static;',
  13625. ' class procedure SetItems(Index: word; const Value: longint); static;',
  13626. ' end;',
  13627. ' TBird = class',
  13628. ' class procedure Fly;',
  13629. ' class property IntA: longint read GetInt write SetInt;',
  13630. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  13631. ' end;',
  13632. 'class function tobject.getint: longint;',
  13633. 'begin',
  13634. 'end;',
  13635. 'class procedure tobject.setint(value: longint);',
  13636. 'begin',
  13637. 'end;',
  13638. 'class function tobject.GetItems(Index: word): longint;',
  13639. 'begin',
  13640. 'end;',
  13641. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  13642. 'begin',
  13643. 'end;',
  13644. 'class procedure tbird.fly;',
  13645. 'var w: longint;',
  13646. 'begin',
  13647. ' inta:=inta+51;',
  13648. ' w:=items[52];',
  13649. ' items[53]:=54;',
  13650. 'end;',
  13651. 'var Obj: tbird;',
  13652. ' i: longint;',
  13653. 'begin',
  13654. ' tbird.inta:=tbird.inta+1;',
  13655. ' i:=tbird.items[2];',
  13656. ' tbird.items[3]:=4;',
  13657. ' obj.inta:=obj.inta+11;',
  13658. ' i:=obj.items[12];',
  13659. ' obj.items[13]:=14;',
  13660. ' with Tbird do begin',
  13661. ' inta:=inta+21;',
  13662. ' i:=items[22];',
  13663. ' items[23]:=24;',
  13664. ' end;',
  13665. ' with Obj do begin',
  13666. ' inta:=inta+31;',
  13667. ' i:=items[32];',
  13668. ' items[33]:=34;',
  13669. ' end;',
  13670. '']);
  13671. ConvertProgram;
  13672. CheckSource('TestClass_Property_ClassMethod',
  13673. LinesToStr([ // statements
  13674. 'rtl.createClass(this, "TObject", null, function () {',
  13675. ' this.$init = function () {',
  13676. ' };',
  13677. ' this.$final = function () {',
  13678. ' };',
  13679. ' this.GetInt = function () {',
  13680. ' var Result = 0;',
  13681. ' return Result;',
  13682. ' };',
  13683. ' this.SetInt = function (Value) {',
  13684. ' };',
  13685. ' this.GetItems = function (Index) {',
  13686. ' var Result = 0;',
  13687. ' return Result;',
  13688. ' };',
  13689. ' this.SetItems = function (Index, Value) {',
  13690. ' };',
  13691. '});',
  13692. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13693. ' this.Fly = function () {',
  13694. ' var w = 0;',
  13695. ' this.SetInt(this.GetInt() + 51);',
  13696. ' w = this.GetItems(52);',
  13697. ' this.SetItems(53, 54);',
  13698. ' };',
  13699. '});',
  13700. 'this.Obj = null;',
  13701. 'this.i = 0;',
  13702. '']),
  13703. LinesToStr([ // $mod.$main
  13704. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  13705. '$mod.i = $mod.TObject.GetItems(2);',
  13706. '$mod.TObject.SetItems(3, 4);',
  13707. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  13708. '$mod.i = $mod.TObject.GetItems(12);',
  13709. '$mod.TObject.SetItems(13, 14);',
  13710. 'var $with = $mod.TBird;',
  13711. '$with.SetInt($with.GetInt() + 21);',
  13712. '$mod.i = $with.GetItems(22);',
  13713. '$with.SetItems(23, 24);',
  13714. 'var $with1 = $mod.Obj;',
  13715. '$with1.SetInt($with1.GetInt() + 31);',
  13716. '$mod.i = $with1.GetItems(32);',
  13717. '$with1.SetItems(33, 34);',
  13718. '']));
  13719. end;
  13720. procedure TTestModule.TestClass_Property_Indexed;
  13721. begin
  13722. StartProgram(false);
  13723. Add([
  13724. 'type',
  13725. ' TObject = class',
  13726. ' FItems: array of longint;',
  13727. ' function GetItems(Index: longint): longint;',
  13728. ' procedure SetItems(Index: longint; Value: longint);',
  13729. ' procedure DoIt;',
  13730. ' property Items[Index: longint]: longint read getitems write setitems;',
  13731. ' end;',
  13732. 'function tobject.getitems(index: longint): longint;',
  13733. 'begin',
  13734. ' Result:=fitems[index];',
  13735. 'end;',
  13736. 'procedure tobject.setitems(index: longint; value: longint);',
  13737. 'begin',
  13738. ' fitems[index]:=value;',
  13739. 'end;',
  13740. 'procedure tobject.doit;',
  13741. 'begin',
  13742. ' items[1]:=2;',
  13743. ' items[3]:=items[4];',
  13744. ' self.items[5]:=self.items[6];',
  13745. ' items[items[7]]:=items[items[8]];',
  13746. 'end;',
  13747. 'var Obj: tobject;',
  13748. 'begin',
  13749. ' obj.Items[11]:=obj.Items[12];',
  13750. '']);
  13751. ConvertProgram;
  13752. CheckSource('TestClass_Property_Indexed',
  13753. LinesToStr([ // statements
  13754. 'rtl.createClass(this, "TObject", null, function () {',
  13755. ' this.$init = function () {',
  13756. ' this.FItems = [];',
  13757. ' };',
  13758. ' this.$final = function () {',
  13759. ' this.FItems = undefined;',
  13760. ' };',
  13761. ' this.GetItems = function (Index) {',
  13762. ' var Result = 0;',
  13763. ' Result = this.FItems[Index];',
  13764. ' return Result;',
  13765. ' };',
  13766. ' this.SetItems = function (Index, Value) {',
  13767. ' this.FItems[Index] = Value;',
  13768. ' };',
  13769. ' this.DoIt = function () {',
  13770. ' this.SetItems(1, 2);',
  13771. ' this.SetItems(3,this.GetItems(4));',
  13772. ' this.SetItems(5,this.GetItems(6));',
  13773. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13774. ' };',
  13775. '});',
  13776. 'this.Obj = null;'
  13777. ]),
  13778. LinesToStr([ // $mod.$main
  13779. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13780. ]));
  13781. end;
  13782. procedure TTestModule.TestClass_Property_IndexSpec;
  13783. begin
  13784. StartProgram(false);
  13785. Add([
  13786. 'type',
  13787. ' TEnum = (red, blue);',
  13788. ' TObject = class',
  13789. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13790. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13791. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13792. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13793. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13794. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13795. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13796. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13797. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13798. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13799. ' end;',
  13800. 'procedure DoIt(b: boolean); begin end;',
  13801. 'var',
  13802. ' o: TObject;',
  13803. 'begin',
  13804. ' o.B1:=o.B1;',
  13805. ' o.B2:=o.B2;',
  13806. ' o.B3:=o.B3;',
  13807. ' o.I1[''a'']:=o.I1[''b''];',
  13808. ' doit(o.b1);',
  13809. ' doit(o.b2);',
  13810. ' doit(o.i1[''c'']);',
  13811. '']);
  13812. ConvertProgram;
  13813. CheckSource('TestClass_Property_IndexSpec',
  13814. LinesToStr([ // statements
  13815. 'this.TEnum = {',
  13816. ' "0": "red",',
  13817. ' red: 0,',
  13818. ' "1": "blue",',
  13819. ' blue: 1',
  13820. '};',
  13821. 'rtl.createClass(this, "TObject", null, function () {',
  13822. ' this.$init = function () {',
  13823. ' };',
  13824. ' this.$final = function () {',
  13825. ' };',
  13826. '});',
  13827. 'this.DoIt = function (b) {',
  13828. '};',
  13829. 'this.o = null;',
  13830. '']),
  13831. LinesToStr([ // $mod.$main
  13832. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13833. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13834. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13835. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13836. '$mod.DoIt($mod.o.GetIntBool(1));',
  13837. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13838. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13839. '']));
  13840. end;
  13841. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13842. begin
  13843. StartProgram(false);
  13844. Add('type');
  13845. Add(' TArray = array of longint;');
  13846. Add(' TObject = class');
  13847. Add(' FItems: TArray;');
  13848. Add(' function GetItems: tarray;');
  13849. Add(' procedure SetItems(Value: tarray);');
  13850. Add(' property Items: tarray read getitems write setitems;');
  13851. Add(' procedure SetNumbers(const Value: tarray);');
  13852. Add(' property Numbers: tarray write setnumbers;');
  13853. Add(' end;');
  13854. Add('function tobject.getitems: tarray;');
  13855. Add('begin');
  13856. Add(' Result:=fitems;');
  13857. Add('end;');
  13858. Add('procedure tobject.setitems(value: tarray);');
  13859. Add('begin');
  13860. Add(' fitems:=value;');
  13861. Add(' fitems:=nil;');
  13862. Add(' Items:=nil;');
  13863. Add(' Items:=Items;');
  13864. Add(' Items[1]:=2;');
  13865. Add(' fitems[3]:=Items[4];');
  13866. Add(' Items[5]:=Items[6];');
  13867. Add(' Self.Items[7]:=8;');
  13868. Add(' Self.Items[9]:=Self.Items[10];');
  13869. Add(' Items[Items[11]]:=Items[Items[12]];');
  13870. Add('end;');
  13871. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13872. Add('begin;');
  13873. Add(' Numbers:=nil;');
  13874. Add(' Numbers:=Value;');
  13875. Add(' Self.Numbers:=Value;');
  13876. Add('end;');
  13877. Add('var Obj: tobject;');
  13878. Add('begin');
  13879. Add(' obj.items:=nil;');
  13880. Add(' obj.items:=obj.items;');
  13881. Add(' obj.items[11]:=obj.items[12];');
  13882. ConvertProgram;
  13883. CheckSource('TestClass_PropertyOfTypeArray',
  13884. LinesToStr([ // statements
  13885. 'rtl.createClass(this, "TObject", null, function () {',
  13886. ' this.$init = function () {',
  13887. ' this.FItems = [];',
  13888. ' };',
  13889. ' this.$final = function () {',
  13890. ' this.FItems = undefined;',
  13891. ' };',
  13892. ' this.GetItems = function () {',
  13893. ' var Result = [];',
  13894. ' Result = rtl.arrayRef(this.FItems);',
  13895. ' return Result;',
  13896. ' };',
  13897. ' this.SetItems = function (Value) {',
  13898. ' this.FItems = rtl.arrayRef(Value);',
  13899. ' this.FItems = [];',
  13900. ' this.SetItems([]);',
  13901. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13902. ' this.GetItems()[1] = 2;',
  13903. ' this.FItems[3] = this.GetItems()[4];',
  13904. ' this.GetItems()[5] = this.GetItems()[6];',
  13905. ' this.GetItems()[7] = 8;',
  13906. ' this.GetItems()[9] = this.GetItems()[10];',
  13907. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13908. ' };',
  13909. ' this.SetNumbers = function (Value) {',
  13910. ' this.SetNumbers([]);',
  13911. ' this.SetNumbers(Value);',
  13912. ' this.SetNumbers(Value);',
  13913. ' };',
  13914. '});',
  13915. 'this.Obj = null;'
  13916. ]),
  13917. LinesToStr([ // $mod.$main
  13918. '$mod.Obj.SetItems([]);',
  13919. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13920. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13921. ]));
  13922. end;
  13923. procedure TTestModule.TestClass_PropertyDefault;
  13924. begin
  13925. StartProgram(false);
  13926. Add([
  13927. 'type',
  13928. ' TArray = array of longint;',
  13929. ' TObject = class',
  13930. ' end;',
  13931. ' TBird = class',
  13932. ' FItems: TArray;',
  13933. ' function GetItems(Index: longint): longint;',
  13934. ' procedure SetItems(Index, Value: longint);',
  13935. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13936. ' end;',
  13937. 'function TBird.getitems(index: longint): longint;',
  13938. 'begin',
  13939. 'end;',
  13940. 'procedure TBird.setitems(index, value: longint);',
  13941. 'begin',
  13942. ' Self[1]:=2;',
  13943. ' Self[3]:=Self[index];',
  13944. ' Self[index]:=Self[Self[value]];',
  13945. ' Self[Self[4]]:=value;',
  13946. 'end;',
  13947. 'var',
  13948. ' Bird: TBird;',
  13949. ' Obj: TObject;',
  13950. 'begin',
  13951. ' bird[11]:=12;',
  13952. ' bird[13]:=bird[14];',
  13953. ' bird[Bird[15]]:=bird[Bird[15]];',
  13954. ' TBird(obj)[16]:=TBird(obj)[17];',
  13955. ' (obj as tbird)[18]:=19;',
  13956. '']);
  13957. ConvertProgram;
  13958. CheckSource('TestClass_PropertyDefault',
  13959. LinesToStr([ // statements
  13960. 'rtl.createClass(this, "TObject", null, function () {',
  13961. ' this.$init = function () {',
  13962. ' };',
  13963. ' this.$final = function () {',
  13964. ' };',
  13965. '});',
  13966. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13967. ' this.$init = function () {',
  13968. ' $mod.TObject.$init.call(this);',
  13969. ' this.FItems = [];',
  13970. ' };',
  13971. ' this.$final = function () {',
  13972. ' this.FItems = undefined;',
  13973. ' $mod.TObject.$final.call(this);',
  13974. ' };',
  13975. ' this.GetItems = function (Index) {',
  13976. ' var Result = 0;',
  13977. ' return Result;',
  13978. ' };',
  13979. ' this.SetItems = function (Index, Value) {',
  13980. ' this.SetItems(1, 2);',
  13981. ' this.SetItems(3, this.GetItems(Index));',
  13982. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13983. ' this.SetItems(this.GetItems(4), Value);',
  13984. ' };',
  13985. '});',
  13986. 'this.Bird = null;',
  13987. 'this.Obj = null;',
  13988. '']),
  13989. LinesToStr([ // $mod.$main
  13990. '$mod.Bird.SetItems(11, 12);',
  13991. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13992. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13993. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13994. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13995. '']));
  13996. end;
  13997. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13998. begin
  13999. StartProgram(false);
  14000. Add([
  14001. 'type',
  14002. ' TObject = class end;',
  14003. ' TAlphaList = class',
  14004. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  14005. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  14006. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  14007. ' end;',
  14008. ' TBetaList = class',
  14009. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  14010. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  14011. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  14012. ' end;',
  14013. ' TBird = class',
  14014. ' procedure DoIt;',
  14015. ' end;',
  14016. 'procedure TBird.DoIt;',
  14017. 'var',
  14018. ' List: TAlphaList;',
  14019. 'begin',
  14020. ' if TBetaList(List[true])[3]=nil then ;',
  14021. ' TBetaList(List[false])[5]:=nil;',
  14022. 'end;',
  14023. 'var',
  14024. ' List: TAlphaList;',
  14025. 'begin',
  14026. ' if TBetaList(List[true])[3]=nil then ;',
  14027. ' TBetaList(List[false])[5]:=nil;',
  14028. '']);
  14029. ConvertProgram;
  14030. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14031. LinesToStr([ // statements
  14032. 'rtl.createClass(this, "TObject", null, function () {',
  14033. ' this.$init = function () {',
  14034. ' };',
  14035. ' this.$final = function () {',
  14036. ' };',
  14037. '});',
  14038. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14039. '});',
  14040. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14041. '});',
  14042. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14043. ' this.DoIt = function () {',
  14044. ' var List = null;',
  14045. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14046. ' List.GetAlphas(false).SetBetas(5, null);',
  14047. ' };',
  14048. '});',
  14049. 'this.List = null;',
  14050. '']),
  14051. LinesToStr([ // $mod.$main
  14052. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14053. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14054. '']));
  14055. end;
  14056. procedure TTestModule.TestClass_PropertyOverride;
  14057. begin
  14058. StartProgram(false);
  14059. Add('type');
  14060. Add(' integer = longint;');
  14061. Add(' TObject = class');
  14062. Add(' FItem: integer;');
  14063. Add(' function GetItem: integer; external name ''GetItem'';');
  14064. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14065. Add(' property Item: integer read getitem write setitem;');
  14066. Add(' end;');
  14067. Add(' TCar = class');
  14068. Add(' FBag: integer;');
  14069. Add(' function GetBag: integer; external name ''GetBag'';');
  14070. Add(' property Item read getbag;');
  14071. Add(' end;');
  14072. Add('var');
  14073. Add(' Obj: tobject;');
  14074. Add(' Car: tcar;');
  14075. Add('begin');
  14076. Add(' Obj.Item:=Obj.Item;');
  14077. Add(' Car.Item:=Car.Item;');
  14078. ConvertProgram;
  14079. CheckSource('TestClass_PropertyOverride',
  14080. LinesToStr([ // statements
  14081. 'rtl.createClass(this, "TObject", null, function () {',
  14082. ' this.$init = function () {',
  14083. ' this.FItem = 0;',
  14084. ' };',
  14085. ' this.$final = function () {',
  14086. ' };',
  14087. '});',
  14088. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14089. ' this.$init = function () {',
  14090. ' $mod.TObject.$init.call(this);',
  14091. ' this.FBag = 0;',
  14092. ' };',
  14093. '});',
  14094. 'this.Obj = null;',
  14095. 'this.Car = null;',
  14096. '']),
  14097. LinesToStr([ // $mod.$main
  14098. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  14099. '$mod.Car.SetItem($mod.Car.GetBag());',
  14100. '']));
  14101. end;
  14102. procedure TTestModule.TestClass_PropertyIncVisibility;
  14103. begin
  14104. AddModuleWithIntfImplSrc('unit1.pp',
  14105. LinesToStr([
  14106. 'type',
  14107. ' TNumber = longint;',
  14108. ' TInteger = longint;',
  14109. ' TObject = class',
  14110. ' private',
  14111. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  14112. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  14113. ' protected',
  14114. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  14115. ' end;']),
  14116. LinesToStr([
  14117. '']));
  14118. StartProgram(true);
  14119. Add([
  14120. 'uses unit1;',
  14121. 'type',
  14122. ' TBird = class',
  14123. ' public',
  14124. ' property Items;',
  14125. ' end;',
  14126. 'procedure DoIt(i: TInteger);',
  14127. 'begin',
  14128. 'end;',
  14129. 'var b: TBird;',
  14130. 'begin',
  14131. ' b.Items[1]:=2;',
  14132. ' b.Items[3]:=b.Items[4];',
  14133. ' DoIt(b.Items[5]);',
  14134. '']);
  14135. ConvertProgram;
  14136. CheckSource('TestClass_PropertyIncVisibility',
  14137. LinesToStr([ // statements
  14138. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  14139. '});',
  14140. 'this.DoIt = function (i) {',
  14141. '};',
  14142. 'this.b = null;'
  14143. ]),
  14144. LinesToStr([ // $mod.$main
  14145. '$mod.b.SetItems(1, 2);',
  14146. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  14147. '$mod.DoIt($mod.b.GetItems(5));'
  14148. ]));
  14149. end;
  14150. procedure TTestModule.TestClass_Assigned;
  14151. begin
  14152. StartProgram(false);
  14153. Add('type');
  14154. Add(' TObject = class');
  14155. Add(' end;');
  14156. Add('var');
  14157. Add(' Obj: tobject;');
  14158. Add(' b: boolean;');
  14159. Add('begin');
  14160. Add(' if Assigned(obj) then ;');
  14161. Add(' b:=Assigned(obj) or false;');
  14162. ConvertProgram;
  14163. CheckSource('TestClass_Assigned',
  14164. LinesToStr([ // statements
  14165. 'rtl.createClass(this, "TObject", null, function () {',
  14166. ' this.$init = function () {',
  14167. ' };',
  14168. ' this.$final = function () {',
  14169. ' };',
  14170. '});',
  14171. 'this.Obj = null;',
  14172. 'this.b = false;'
  14173. ]),
  14174. LinesToStr([ // $mod.$main
  14175. 'if ($mod.Obj != null);',
  14176. '$mod.b = ($mod.Obj != null) || false;'
  14177. ]));
  14178. end;
  14179. procedure TTestModule.TestClass_WithClassDoCreate;
  14180. begin
  14181. StartProgram(false);
  14182. Add('type');
  14183. Add(' TObject = class');
  14184. Add(' aBool: boolean;');
  14185. Add(' Arr: array of boolean;');
  14186. Add(' constructor Create;');
  14187. Add(' end;');
  14188. Add('constructor TObject.Create; begin end;');
  14189. Add('var');
  14190. Add(' Obj: tobject;');
  14191. Add(' b: boolean;');
  14192. Add('begin');
  14193. Add(' with tobject.create do begin');
  14194. Add(' b:=abool;');
  14195. Add(' abool:=b;');
  14196. Add(' b:=arr[1];');
  14197. Add(' arr[2]:=b;');
  14198. Add(' end;');
  14199. Add(' with tobject do');
  14200. Add(' obj:=create;');
  14201. Add(' with obj do begin');
  14202. Add(' create;');
  14203. Add(' b:=abool;');
  14204. Add(' abool:=b;');
  14205. Add(' b:=arr[3];');
  14206. Add(' arr[4]:=b;');
  14207. Add(' end;');
  14208. ConvertProgram;
  14209. CheckSource('TestClass_WithClassDoCreate',
  14210. LinesToStr([ // statements
  14211. 'rtl.createClass(this, "TObject", null, function () {',
  14212. ' this.$init = function () {',
  14213. ' this.aBool = false;',
  14214. ' this.Arr = [];',
  14215. ' };',
  14216. ' this.$final = function () {',
  14217. ' this.Arr = undefined;',
  14218. ' };',
  14219. ' this.Create = function () {',
  14220. ' return this;',
  14221. ' };',
  14222. '});',
  14223. 'this.Obj = null;',
  14224. 'this.b = false;'
  14225. ]),
  14226. LinesToStr([ // $mod.$main
  14227. 'var $with = $mod.TObject.$create("Create");',
  14228. '$mod.b = $with.aBool;',
  14229. '$with.aBool = $mod.b;',
  14230. '$mod.b = $with.Arr[1];',
  14231. '$with.Arr[2] = $mod.b;',
  14232. 'var $with1 = $mod.TObject;',
  14233. '$mod.Obj = $with1.$create("Create");',
  14234. 'var $with2 = $mod.Obj;',
  14235. '$with2.Create();',
  14236. '$mod.b = $with2.aBool;',
  14237. '$with2.aBool = $mod.b;',
  14238. '$mod.b = $with2.Arr[3];',
  14239. '$with2.Arr[4] = $mod.b;',
  14240. '']));
  14241. end;
  14242. procedure TTestModule.TestClass_WithClassInstDoProperty;
  14243. begin
  14244. StartProgram(false);
  14245. Add('type');
  14246. Add(' TObject = class');
  14247. Add(' FInt: longint;');
  14248. Add(' constructor Create;');
  14249. Add(' function GetSize: longint;');
  14250. Add(' procedure SetSize(Value: longint);');
  14251. Add(' property Int: longint read FInt write FInt;');
  14252. Add(' property Size: longint read GetSize write SetSize;');
  14253. Add(' end;');
  14254. Add('constructor TObject.Create; begin end;');
  14255. Add('function TObject.GetSize: longint; begin; end;');
  14256. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14257. Add('var');
  14258. Add(' Obj: tobject;');
  14259. Add(' i: longint;');
  14260. Add('begin');
  14261. Add(' with TObject.Create do begin');
  14262. Add(' i:=int;');
  14263. Add(' int:=i;');
  14264. Add(' i:=size;');
  14265. Add(' size:=i;');
  14266. Add(' end;');
  14267. Add(' with obj do begin');
  14268. Add(' i:=int;');
  14269. Add(' int:=i;');
  14270. Add(' i:=size;');
  14271. Add(' size:=i;');
  14272. Add(' end;');
  14273. ConvertProgram;
  14274. CheckSource('TestClass_WithClassInstDoProperty',
  14275. LinesToStr([ // statements
  14276. 'rtl.createClass(this, "TObject", null, function () {',
  14277. ' this.$init = function () {',
  14278. ' this.FInt = 0;',
  14279. ' };',
  14280. ' this.$final = function () {',
  14281. ' };',
  14282. ' this.Create = function () {',
  14283. ' return this;',
  14284. ' };',
  14285. ' this.GetSize = function () {',
  14286. ' var Result = 0;',
  14287. ' return Result;',
  14288. ' };',
  14289. ' this.SetSize = function (Value) {',
  14290. ' };',
  14291. '});',
  14292. 'this.Obj = null;',
  14293. 'this.i = 0;'
  14294. ]),
  14295. LinesToStr([ // $mod.$main
  14296. 'var $with = $mod.TObject.$create("Create");',
  14297. '$mod.i = $with.FInt;',
  14298. '$with.FInt = $mod.i;',
  14299. '$mod.i = $with.GetSize();',
  14300. '$with.SetSize($mod.i);',
  14301. 'var $with1 = $mod.Obj;',
  14302. '$mod.i = $with1.FInt;',
  14303. '$with1.FInt = $mod.i;',
  14304. '$mod.i = $with1.GetSize();',
  14305. '$with1.SetSize($mod.i);',
  14306. '']));
  14307. end;
  14308. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  14309. begin
  14310. StartProgram(false);
  14311. Add('type');
  14312. Add(' TObject = class');
  14313. Add(' constructor Create;');
  14314. Add(' function GetItems(Index: longint): longint;');
  14315. Add(' procedure SetItems(Index, Value: longint);');
  14316. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  14317. Add(' end;');
  14318. Add('constructor TObject.Create; begin end;');
  14319. Add('function tobject.getitems(index: longint): longint; begin; end;');
  14320. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  14321. Add('var');
  14322. Add(' Obj: tobject;');
  14323. Add(' i: longint;');
  14324. Add('begin');
  14325. Add(' with TObject.Create do begin');
  14326. Add(' i:=Items[1];');
  14327. Add(' Items[2]:=i;');
  14328. Add(' end;');
  14329. Add(' with obj do begin');
  14330. Add(' i:=Items[3];');
  14331. Add(' Items[4]:=i;');
  14332. Add(' end;');
  14333. ConvertProgram;
  14334. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  14335. LinesToStr([ // statements
  14336. 'rtl.createClass(this, "TObject", null, function () {',
  14337. ' this.$init = function () {',
  14338. ' };',
  14339. ' this.$final = function () {',
  14340. ' };',
  14341. ' this.Create = function () {',
  14342. ' return this;',
  14343. ' };',
  14344. ' this.GetItems = function (Index) {',
  14345. ' var Result = 0;',
  14346. ' return Result;',
  14347. ' };',
  14348. ' this.SetItems = function (Index, Value) {',
  14349. ' };',
  14350. '});',
  14351. 'this.Obj = null;',
  14352. 'this.i = 0;'
  14353. ]),
  14354. LinesToStr([ // $mod.$main
  14355. 'var $with = $mod.TObject.$create("Create");',
  14356. '$mod.i = $with.GetItems(1);',
  14357. '$with.SetItems(2, $mod.i);',
  14358. 'var $with1 = $mod.Obj;',
  14359. '$mod.i = $with1.GetItems(3);',
  14360. '$with1.SetItems(4, $mod.i);',
  14361. '']));
  14362. end;
  14363. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14364. begin
  14365. StartProgram(false);
  14366. Add('type');
  14367. Add(' TObject = class');
  14368. Add(' constructor Create;');
  14369. Add(' function GetSize: longint;');
  14370. Add(' procedure SetSize(Value: longint);');
  14371. Add(' end;');
  14372. Add('constructor TObject.Create; begin end;');
  14373. Add('function TObject.GetSize: longint; begin; end;');
  14374. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14375. Add('var');
  14376. Add(' Obj: tobject;');
  14377. Add(' i: longint;');
  14378. Add('begin');
  14379. Add(' with TObject.Create do begin');
  14380. Add(' i:=GetSize;');
  14381. Add(' i:=GetSize();');
  14382. Add(' SetSize(i);');
  14383. Add(' end;');
  14384. Add(' with obj do begin');
  14385. Add(' i:=GetSize;');
  14386. Add(' i:=GetSize();');
  14387. Add(' SetSize(i);');
  14388. Add(' end;');
  14389. ConvertProgram;
  14390. CheckSource('TestClass_WithClassInstDoFunc',
  14391. LinesToStr([ // statements
  14392. 'rtl.createClass(this, "TObject", null, function () {',
  14393. ' this.$init = function () {',
  14394. ' };',
  14395. ' this.$final = function () {',
  14396. ' };',
  14397. ' this.Create = function () {',
  14398. ' return this;',
  14399. ' };',
  14400. ' this.GetSize = function () {',
  14401. ' var Result = 0;',
  14402. ' return Result;',
  14403. ' };',
  14404. ' this.SetSize = function (Value) {',
  14405. ' };',
  14406. '});',
  14407. 'this.Obj = null;',
  14408. 'this.i = 0;'
  14409. ]),
  14410. LinesToStr([ // $mod.$main
  14411. 'var $with = $mod.TObject.$create("Create");',
  14412. '$mod.i = $with.GetSize();',
  14413. '$mod.i = $with.GetSize();',
  14414. '$with.SetSize($mod.i);',
  14415. 'var $with1 = $mod.Obj;',
  14416. '$mod.i = $with1.GetSize();',
  14417. '$mod.i = $with1.GetSize();',
  14418. '$with1.SetSize($mod.i);',
  14419. '']));
  14420. end;
  14421. procedure TTestModule.TestClass_TypeCast;
  14422. begin
  14423. StartProgram(false);
  14424. Add('type');
  14425. Add(' TObject = class');
  14426. Add(' Next: TObject;');
  14427. Add(' constructor Create;');
  14428. Add(' end;');
  14429. Add(' TControl = class(TObject)');
  14430. Add(' Arr: array of TObject;');
  14431. Add(' function GetIt(vI: longint = 0): TObject;');
  14432. Add(' end;');
  14433. Add('constructor tobject.create; begin end;');
  14434. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14435. Add('var');
  14436. Add(' Obj: tobject;');
  14437. Add('begin');
  14438. Add(' obj:=tcontrol(obj).next;');
  14439. Add(' tcontrol(obj):=nil;');
  14440. Add(' obj:=tcontrol(obj);');
  14441. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14442. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14443. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14444. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14445. Add(' obj:=tcontrol(nil);');
  14446. ConvertProgram;
  14447. CheckSource('TestClass_TypeCast',
  14448. LinesToStr([ // statements
  14449. 'rtl.createClass(this, "TObject", null, function () {',
  14450. ' this.$init = function () {',
  14451. ' this.Next = null;',
  14452. ' };',
  14453. ' this.$final = function () {',
  14454. ' this.Next = undefined;',
  14455. ' };',
  14456. ' this.Create = function () {',
  14457. ' return this;',
  14458. ' };',
  14459. '});',
  14460. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14461. ' this.$init = function () {',
  14462. ' $mod.TObject.$init.call(this);',
  14463. ' this.Arr = [];',
  14464. ' };',
  14465. ' this.$final = function () {',
  14466. ' this.Arr = undefined;',
  14467. ' $mod.TObject.$final.call(this);',
  14468. ' };',
  14469. ' this.GetIt = function (vI) {',
  14470. ' var Result = null;',
  14471. ' return Result;',
  14472. ' };',
  14473. '});',
  14474. 'this.Obj = null;'
  14475. ]),
  14476. LinesToStr([ // $mod.$main
  14477. '$mod.Obj = $mod.Obj.Next;',
  14478. '$mod.Obj = null;',
  14479. '$mod.Obj = $mod.Obj;',
  14480. '$mod.Obj = $mod.Obj.GetIt(0);',
  14481. '$mod.Obj = $mod.Obj.GetIt(0);',
  14482. '$mod.Obj = $mod.Obj.GetIt(1);',
  14483. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14484. '$mod.Obj = null;',
  14485. '']));
  14486. end;
  14487. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14488. begin
  14489. StartProgram(false);
  14490. Add('type');
  14491. Add(' TObject = class end;');
  14492. Add('procedure ProcA(var A);');
  14493. Add('begin');
  14494. Add(' TObject(A):=nil;');
  14495. Add(' TObject(A):=TObject(A);');
  14496. Add(' if TObject(A)=nil then ;');
  14497. Add(' if nil=TObject(A) then ;');
  14498. Add('end;');
  14499. Add('procedure ProcB(out A);');
  14500. Add('begin');
  14501. Add(' TObject(A):=nil;');
  14502. Add(' TObject(A):=TObject(A);');
  14503. Add(' if TObject(A)=nil then ;');
  14504. Add(' if nil=TObject(A) then ;');
  14505. Add('end;');
  14506. Add('procedure ProcC(const A);');
  14507. Add('begin');
  14508. Add(' if TObject(A)=nil then ;');
  14509. Add(' if nil=TObject(A) then ;');
  14510. Add('end;');
  14511. Add('var o: TObject;');
  14512. Add('begin');
  14513. Add(' ProcA(o);');
  14514. Add(' ProcB(o);');
  14515. Add(' ProcC(o);');
  14516. ConvertProgram;
  14517. CheckSource('TestClass_TypeCastUntypedParam',
  14518. LinesToStr([ // statements
  14519. 'rtl.createClass(this, "TObject", null, function () {',
  14520. ' this.$init = function () {',
  14521. ' };',
  14522. ' this.$final = function () {',
  14523. ' };',
  14524. '});',
  14525. 'this.ProcA = function (A) {',
  14526. ' A.set(null);',
  14527. ' A.set(A.get());',
  14528. ' if (A.get() === null);',
  14529. ' if (null === A.get());',
  14530. '};',
  14531. 'this.ProcB = function (A) {',
  14532. ' A.set(null);',
  14533. ' A.set(A.get());',
  14534. ' if (A.get() === null);',
  14535. ' if (null === A.get());',
  14536. '};',
  14537. 'this.ProcC = function (A) {',
  14538. ' if (A === null);',
  14539. ' if (null === A);',
  14540. '};',
  14541. 'this.o = null;',
  14542. '']),
  14543. LinesToStr([ // $mod.$main
  14544. '$mod.ProcA({',
  14545. ' p: $mod,',
  14546. ' get: function () {',
  14547. ' return this.p.o;',
  14548. ' },',
  14549. ' set: function (v) {',
  14550. ' this.p.o = v;',
  14551. ' }',
  14552. '});',
  14553. '$mod.ProcB({',
  14554. ' p: $mod,',
  14555. ' get: function () {',
  14556. ' return this.p.o;',
  14557. ' },',
  14558. ' set: function (v) {',
  14559. ' this.p.o = v;',
  14560. ' }',
  14561. '});',
  14562. '$mod.ProcC($mod.o);',
  14563. '']));
  14564. end;
  14565. procedure TTestModule.TestClass_Overloads;
  14566. begin
  14567. StartProgram(false);
  14568. Add('type');
  14569. Add(' TObject = class');
  14570. Add(' procedure DoIt;');
  14571. Add(' procedure DoIt(vI: longint);');
  14572. Add(' end;');
  14573. Add('procedure TObject.DoIt;');
  14574. Add('begin');
  14575. Add(' DoIt;');
  14576. Add(' DoIt(1);');
  14577. Add('end;');
  14578. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14579. Add('begin');
  14580. ConvertProgram;
  14581. CheckSource('TestClass_Overloads',
  14582. LinesToStr([ // statements
  14583. 'rtl.createClass(this, "TObject", null, function () {',
  14584. ' this.$init = function () {',
  14585. ' };',
  14586. ' this.$final = function () {',
  14587. ' };',
  14588. ' this.DoIt = function () {',
  14589. ' this.DoIt();',
  14590. ' this.DoIt$1(1);',
  14591. ' };',
  14592. ' this.DoIt$1 = function (vI) {',
  14593. ' };',
  14594. '});',
  14595. '']),
  14596. LinesToStr([ // $mod.$main
  14597. '']));
  14598. end;
  14599. procedure TTestModule.TestClass_OverloadsAncestor;
  14600. begin
  14601. StartProgram(false);
  14602. Add('type');
  14603. Add(' TObject = class;');
  14604. Add(' TObject = class');
  14605. Add(' procedure DoIt(vA: longint);');
  14606. Add(' procedure DoIt(vA, vB: longint);');
  14607. Add(' end;');
  14608. Add(' TCar = class;');
  14609. Add(' TCar = class');
  14610. Add(' procedure DoIt(vA: longint);');
  14611. Add(' procedure DoIt(vA, vB: longint);');
  14612. Add(' end;');
  14613. Add('procedure tobject.doit(va: longint);');
  14614. Add('begin');
  14615. Add(' doit(1);');
  14616. Add(' doit(1,2);');
  14617. Add('end;');
  14618. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14619. Add('procedure tcar.doit(va: longint);');
  14620. Add('begin');
  14621. Add(' doit(1);');
  14622. Add(' doit(1,2);');
  14623. Add(' inherited doit(1);');
  14624. Add(' inherited doit(1,2);');
  14625. Add('end;');
  14626. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14627. Add('begin');
  14628. ConvertProgram;
  14629. CheckSource('TestClass_OverloadsAncestor',
  14630. LinesToStr([ // statements
  14631. 'rtl.createClass(this, "TObject", null, function () {',
  14632. ' this.$init = function () {',
  14633. ' };',
  14634. ' this.$final = function () {',
  14635. ' };',
  14636. ' this.DoIt = function (vA) {',
  14637. ' this.DoIt(1);',
  14638. ' this.DoIt$1(1,2);',
  14639. ' };',
  14640. ' this.DoIt$1 = function (vA, vB) {',
  14641. ' };',
  14642. '});',
  14643. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14644. ' this.DoIt$2 = function (vA) {',
  14645. ' this.DoIt$2(1);',
  14646. ' this.DoIt$3(1, 2);',
  14647. ' $mod.TObject.DoIt.call(this, 1);',
  14648. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14649. ' };',
  14650. ' this.DoIt$3 = function (vA, vB) {',
  14651. ' };',
  14652. '});',
  14653. '']),
  14654. LinesToStr([ // $mod.$main
  14655. '']));
  14656. end;
  14657. procedure TTestModule.TestClass_OverloadConstructor;
  14658. begin
  14659. StartProgram(false);
  14660. Add('type');
  14661. Add(' TObject = class');
  14662. Add(' constructor Create(vA: longint);');
  14663. Add(' constructor Create(vA, vB: longint);');
  14664. Add(' end;');
  14665. Add(' TCar = class');
  14666. Add(' constructor Create(vA: longint);');
  14667. Add(' constructor Create(vA, vB: longint);');
  14668. Add(' end;');
  14669. Add('constructor tobject.create(va: longint);');
  14670. Add('begin');
  14671. Add(' create(1);');
  14672. Add(' create(1,2);');
  14673. Add('end;');
  14674. Add('constructor tobject.create(va, vb: longint); begin end;');
  14675. Add('constructor tcar.create(va: longint);');
  14676. Add('begin');
  14677. Add(' create(1);');
  14678. Add(' create(1,2);');
  14679. Add(' inherited create(1);');
  14680. Add(' inherited create(1,2);');
  14681. Add('end;');
  14682. Add('constructor tcar.create(va, vb: longint); begin end;');
  14683. Add('begin');
  14684. Add(' tobject.create(1);');
  14685. Add(' tobject.create(1,2);');
  14686. Add(' tcar.create(1);');
  14687. Add(' tcar.create(1,2);');
  14688. ConvertProgram;
  14689. CheckSource('TestClass_OverloadConstructor',
  14690. LinesToStr([ // statements
  14691. 'rtl.createClass(this, "TObject", null, function () {',
  14692. ' this.$init = function () {',
  14693. ' };',
  14694. ' this.$final = function () {',
  14695. ' };',
  14696. ' this.Create = function (vA) {',
  14697. ' this.Create(1);',
  14698. ' this.Create$1(1,2);',
  14699. ' return this;',
  14700. ' };',
  14701. ' this.Create$1 = function (vA, vB) {',
  14702. ' return this;',
  14703. ' };',
  14704. '});',
  14705. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14706. ' this.Create$2 = function (vA) {',
  14707. ' this.Create$2(1);',
  14708. ' this.Create$3(1, 2);',
  14709. ' $mod.TObject.Create.call(this, 1);',
  14710. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14711. ' return this;',
  14712. ' };',
  14713. ' this.Create$3 = function (vA, vB) {',
  14714. ' return this;',
  14715. ' };',
  14716. '});',
  14717. '']),
  14718. LinesToStr([ // $mod.$main
  14719. '$mod.TObject.$create("Create", [1]);',
  14720. '$mod.TObject.$create("Create$1", [1, 2]);',
  14721. '$mod.TCar.$create("Create$2", [1]);',
  14722. '$mod.TCar.$create("Create$3", [1, 2]);',
  14723. '']));
  14724. end;
  14725. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14726. begin
  14727. StartProgram(false);
  14728. Add([
  14729. '{$mode delphi}',
  14730. 'type',
  14731. ' TObject = class end;',
  14732. ' TBird = class',
  14733. ' function {#a}GetValue: longint; overload; virtual;',
  14734. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14735. ' end;',
  14736. ' TEagle = class(TBird)',
  14737. ' function {#c}GetValue: longint; overload; override;',
  14738. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14739. ' end;',
  14740. 'function TBird.GetValue: longint;',
  14741. 'begin',
  14742. ' if 3={@a}GetValue then ;',
  14743. ' if 4={@b}GetValue(5) then ;',
  14744. 'end;',
  14745. 'function TBird.GetValue(AValue: longint): longint;',
  14746. 'begin',
  14747. 'end;',
  14748. 'function TEagle.GetValue: longint;',
  14749. 'begin',
  14750. ' if 13={@c}GetValue then ;',
  14751. ' if 14={@d}GetValue(15) then ;',
  14752. ' if 15=inherited {@a}GetValue then ;',
  14753. ' if 16=inherited {@b}GetValue(17) then ;',
  14754. 'end;',
  14755. 'function TEagle.GetValue(AValue: longint): longint;',
  14756. 'begin',
  14757. 'end;',
  14758. 'var',
  14759. ' e: TEagle;',
  14760. 'begin',
  14761. ' if 23=e.{@c}GetValue then ;',
  14762. ' if 24=e.{@d}GetValue(25) then ;']);
  14763. ConvertProgram;
  14764. CheckSource('TestClass_OverloadDelphiOverride',
  14765. LinesToStr([ // statements
  14766. 'rtl.createClass(this, "TObject", null, function () {',
  14767. ' this.$init = function () {',
  14768. ' };',
  14769. ' this.$final = function () {',
  14770. ' };',
  14771. '});',
  14772. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14773. ' this.GetValue = function () {',
  14774. ' var Result = 0;',
  14775. ' if (3 === this.GetValue()) ;',
  14776. ' if (4 === this.GetValue$1(5)) ;',
  14777. ' return Result;',
  14778. ' };',
  14779. ' this.GetValue$1 = function (AValue) {',
  14780. ' var Result = 0;',
  14781. ' return Result;',
  14782. ' };',
  14783. '});',
  14784. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14785. ' this.GetValue = function () {',
  14786. ' var Result = 0;',
  14787. ' if (13 === this.GetValue()) ;',
  14788. ' if (14 === this.GetValue$1(15)) ;',
  14789. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14790. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14791. ' return Result;',
  14792. ' };',
  14793. ' this.GetValue$1 = function (AValue) {',
  14794. ' var Result = 0;',
  14795. ' return Result;',
  14796. ' };',
  14797. '});',
  14798. 'this.e = null;',
  14799. '']),
  14800. LinesToStr([ // $mod.$main
  14801. 'if (23 === $mod.e.GetValue()) ;',
  14802. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14803. '']));
  14804. end;
  14805. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14806. begin
  14807. StartProgram(false);
  14808. Add([
  14809. '{$mode delphi}',
  14810. 'type',
  14811. ' TObject = class end;',
  14812. ' TAnimal = class',
  14813. ' public',
  14814. ' {#animal_a}A: longint;',
  14815. ' function {#animal_b}B: longint;',
  14816. ' end;',
  14817. ' TBird = class(TAnimal)',
  14818. ' public',
  14819. ' {#bird_a}A: double;',
  14820. ' {#bird_b}B: boolean;',
  14821. ' end;',
  14822. ' TEagle = class(TBird)',
  14823. ' public',
  14824. ' function {#eagle_a}A: boolean;',
  14825. ' {#eagle_b}B: double;',
  14826. ' end;',
  14827. 'function TAnimal.B: longint;',
  14828. 'begin',
  14829. 'end;',
  14830. 'function TEagle.A: boolean;',
  14831. 'begin',
  14832. ' {@eagle_b}B:=3.3;',
  14833. ' {@eagle_a}A();',
  14834. ' TBird(Self).{@bird_b}B:=true;',
  14835. ' TAnimal(Self).{@animal_a}A:=17;',
  14836. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14837. 'end;',
  14838. 'var',
  14839. ' e: TEagle;',
  14840. 'begin',
  14841. ' e.{@eagle_b}B:=5.3;',
  14842. ' if e.{@eagle_a}A then ;',
  14843. '']);
  14844. ConvertProgram;
  14845. CheckSource('TestClass_ReintroduceVarDelphi',
  14846. LinesToStr([ // statements
  14847. 'rtl.createClass(this, "TObject", null, function () {',
  14848. ' this.$init = function () {',
  14849. ' };',
  14850. ' this.$final = function () {',
  14851. ' };',
  14852. '});',
  14853. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14854. ' this.$init = function () {',
  14855. ' $mod.TObject.$init.call(this);',
  14856. ' this.A = 0;',
  14857. ' };',
  14858. ' this.B = function () {',
  14859. ' var Result = 0;',
  14860. ' return Result;',
  14861. ' };',
  14862. '});',
  14863. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14864. ' this.$init = function () {',
  14865. ' $mod.TAnimal.$init.call(this);',
  14866. ' this.A$1 = 0.0;',
  14867. ' this.B$1 = false;',
  14868. ' };',
  14869. '});',
  14870. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14871. ' this.$init = function () {',
  14872. ' $mod.TBird.$init.call(this);',
  14873. ' this.B$2 = 0.0;',
  14874. ' };',
  14875. ' this.A$2 = function () {',
  14876. ' var Result = false;',
  14877. ' this.B$2 = 3.3;',
  14878. ' this.A$2();',
  14879. ' this.B$1 = true;',
  14880. ' this.A = 17;',
  14881. ' this.B$1 = this.A$1 > 1;',
  14882. ' return Result;',
  14883. ' };',
  14884. '});',
  14885. 'this.e = null;',
  14886. '']),
  14887. LinesToStr([ // $mod.$main
  14888. '$mod.e.B$2 = 5.3;',
  14889. 'if ($mod.e.A$2()) ;',
  14890. '']));
  14891. end;
  14892. procedure TTestModule.TestClass_ReintroducedVar;
  14893. begin
  14894. StartProgram(false);
  14895. Add('type');
  14896. Add(' TObject = class');
  14897. Add(' strict private');
  14898. Add(' Some: longint;');
  14899. Add(' end;');
  14900. Add(' TMobile = class');
  14901. Add(' strict private');
  14902. Add(' Some: string;');
  14903. Add(' end;');
  14904. Add(' TCar = class(tmobile)');
  14905. Add(' procedure Some;');
  14906. Add(' procedure Some(vA: longint);');
  14907. Add(' end;');
  14908. Add('procedure tcar.some;');
  14909. Add('begin');
  14910. Add(' Some;');
  14911. Add(' Some(1);');
  14912. Add('end;');
  14913. Add('procedure tcar.some(va: longint); begin end;');
  14914. Add('begin');
  14915. ConvertProgram;
  14916. CheckSource('TestClass_ReintroducedVar',
  14917. LinesToStr([ // statements
  14918. 'rtl.createClass(this, "TObject", null, function () {',
  14919. ' this.$init = function () {',
  14920. ' this.Some = 0;',
  14921. ' };',
  14922. ' this.$final = function () {',
  14923. ' };',
  14924. '});',
  14925. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14926. ' this.$init = function () {',
  14927. ' $mod.TObject.$init.call(this);',
  14928. ' this.Some$1 = "";',
  14929. ' };',
  14930. '});',
  14931. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14932. ' this.Some$2 = function () {',
  14933. ' this.Some$2();',
  14934. ' this.Some$3(1);',
  14935. ' };',
  14936. ' this.Some$3 = function (vA) {',
  14937. ' };',
  14938. '});',
  14939. '']),
  14940. LinesToStr([ // $mod.$main
  14941. '']));
  14942. end;
  14943. procedure TTestModule.TestClass_RaiseDescendant;
  14944. begin
  14945. StartProgram(false);
  14946. Add([
  14947. 'type',
  14948. ' TObject = class',
  14949. ' constructor Create(Msg: string);',
  14950. ' end;',
  14951. ' Exception = class',
  14952. ' end;',
  14953. ' EConvertError = class(Exception)',
  14954. ' end;',
  14955. 'constructor TObject.Create(Msg: string); begin end;',
  14956. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14957. 'begin',
  14958. ' raise Exception.Create(''Bar1'');',
  14959. ' raise EConvertError.Create(''Bar2'');',
  14960. ' raise AssertConv(''Bar2'');',
  14961. ' raise AssertConv;',
  14962. '']);
  14963. ConvertProgram;
  14964. CheckSource('TestClass_RaiseDescendant',
  14965. LinesToStr([ // statements
  14966. 'rtl.createClass(this, "TObject", null, function () {',
  14967. ' this.$init = function () {',
  14968. ' };',
  14969. ' this.$final = function () {',
  14970. ' };',
  14971. ' this.Create = function (Msg) {',
  14972. ' return this;',
  14973. ' };',
  14974. '});',
  14975. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14976. '});',
  14977. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14978. '});',
  14979. 'this.AssertConv = function (Msg) {',
  14980. ' var Result = null;',
  14981. ' return Result;',
  14982. '};',
  14983. '']),
  14984. LinesToStr([ // $mod.$main
  14985. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14986. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14987. 'throw $mod.AssertConv("Bar2");',
  14988. 'throw $mod.AssertConv("def");',
  14989. '']));
  14990. end;
  14991. procedure TTestModule.TestClass_ExternalMethod;
  14992. begin
  14993. AddModuleWithIntfImplSrc('unit2.pas',
  14994. LinesToStr([
  14995. 'type',
  14996. ' TObject = class',
  14997. ' public',
  14998. ' procedure Intern; external name ''$DoIntern'';',
  14999. ' end;',
  15000. '']),
  15001. LinesToStr([
  15002. '']));
  15003. StartUnit(true);
  15004. Add('interface');
  15005. Add('uses unit2;');
  15006. Add('type');
  15007. Add(' TCar = class(TObject)');
  15008. Add(' public');
  15009. Add(' procedure Intern2; external name ''$DoIntern2'';');
  15010. Add(' procedure DoIt;');
  15011. Add(' end;');
  15012. Add('implementation');
  15013. Add('procedure tcar.doit;');
  15014. Add('begin');
  15015. Add(' Intern;');
  15016. Add(' Intern();');
  15017. Add(' Intern2;');
  15018. Add(' Intern2();');
  15019. Add('end;');
  15020. Add('var Obj: TCar;');
  15021. Add('begin');
  15022. Add(' obj.intern;');
  15023. Add(' obj.intern();');
  15024. Add(' obj.intern2;');
  15025. Add(' obj.intern2();');
  15026. Add(' obj.doit;');
  15027. Add(' obj.doit();');
  15028. Add(' with obj do begin');
  15029. Add(' Intern;');
  15030. Add(' Intern();');
  15031. Add(' Intern2;');
  15032. Add(' Intern2();');
  15033. Add(' end;');
  15034. ConvertUnit;
  15035. CheckSource('TestClass_ExternalMethod',
  15036. LinesToStr([
  15037. 'var $impl = $mod.$impl;',
  15038. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15039. ' this.DoIt = function () {',
  15040. ' this.$DoIntern();',
  15041. ' this.$DoIntern();',
  15042. ' this.$DoIntern2();',
  15043. ' this.$DoIntern2();',
  15044. ' };',
  15045. ' });',
  15046. '']),
  15047. LinesToStr([ // this.$init
  15048. '$impl.Obj.$DoIntern();',
  15049. '$impl.Obj.$DoIntern();',
  15050. '$impl.Obj.$DoIntern2();',
  15051. '$impl.Obj.$DoIntern2();',
  15052. '$impl.Obj.DoIt();',
  15053. '$impl.Obj.DoIt();',
  15054. 'var $with = $impl.Obj;',
  15055. '$with.$DoIntern();',
  15056. '$with.$DoIntern();',
  15057. '$with.$DoIntern2();',
  15058. '$with.$DoIntern2();',
  15059. '']),
  15060. LinesToStr([ // implementation
  15061. '$impl.Obj = null;',
  15062. '']) );
  15063. end;
  15064. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15065. begin
  15066. StartProgram(false);
  15067. Add('type');
  15068. Add(' TObject = class');
  15069. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15070. Add(' end;');
  15071. Add('begin');
  15072. SetExpectedPasResolverError('Virtual method name must match external',
  15073. nVirtualMethodNameMustMatchExternal);
  15074. ConvertProgram;
  15075. end;
  15076. procedure TTestModule.TestClass_ExternalOverrideFail;
  15077. begin
  15078. StartProgram(false);
  15079. Add('type');
  15080. Add(' TObject = class');
  15081. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15082. Add(' end;');
  15083. Add(' TCar = class');
  15084. Add(' procedure DoIt; override; external name ''DoIt'';');
  15085. Add(' end;');
  15086. Add('begin');
  15087. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15088. nInvalidXModifierY);
  15089. ConvertProgram;
  15090. end;
  15091. procedure TTestModule.TestClass_ExternalVar;
  15092. begin
  15093. AddModuleWithIntfImplSrc('unit2.pas',
  15094. LinesToStr([
  15095. '{$modeswitch externalclass}',
  15096. 'type',
  15097. ' TObject = class',
  15098. ' public',
  15099. ' Intern: longint external name ''$Intern'';',
  15100. ' Bracket: longint external name ''["A B"]'';',
  15101. ' end;',
  15102. '']),
  15103. LinesToStr([
  15104. '']));
  15105. StartUnit(true);
  15106. Add([
  15107. 'interface',
  15108. 'uses unit2;',
  15109. '{$modeswitch externalclass}',
  15110. 'type',
  15111. ' TCar = class(tobject)',
  15112. ' public',
  15113. ' Intern2: longint external name ''$Intern2'';',
  15114. ' procedure DoIt;',
  15115. ' end;',
  15116. 'implementation',
  15117. 'procedure tcar.doit;',
  15118. 'begin',
  15119. ' Intern:=Intern+1;',
  15120. ' Intern2:=Intern2+2;',
  15121. ' Bracket:=Bracket+3;',
  15122. 'end;',
  15123. 'var Obj: TCar;',
  15124. 'begin',
  15125. ' obj.intern:=obj.intern+1;',
  15126. ' obj.intern2:=obj.intern2+2;',
  15127. ' obj.Bracket:=obj.Bracket+3;',
  15128. ' with obj do begin',
  15129. ' intern:=intern+1;',
  15130. ' intern2:=intern2+2;',
  15131. ' Bracket:=Bracket+3;',
  15132. ' end;']);
  15133. ConvertUnit;
  15134. CheckSource('TestClass_ExternalVar',
  15135. LinesToStr([
  15136. 'var $impl = $mod.$impl;',
  15137. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15138. ' this.DoIt = function () {',
  15139. ' this.$Intern = this.$Intern + 1;',
  15140. ' this.$Intern2 = this.$Intern2 + 2;',
  15141. ' this["A B"] = this["A B"] + 3;',
  15142. ' };',
  15143. ' });',
  15144. '']),
  15145. LinesToStr([
  15146. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  15147. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  15148. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  15149. 'var $with = $impl.Obj;',
  15150. '$with.$Intern = $with.$Intern + 1;',
  15151. '$with.$Intern2 = $with.$Intern2 + 2;',
  15152. '$with["A B"] = $with["A B"] + 3;',
  15153. '']),
  15154. LinesToStr([ // implementation
  15155. '$impl.Obj = null;',
  15156. '']));
  15157. end;
  15158. procedure TTestModule.TestClass_Const;
  15159. begin
  15160. StartProgram(false);
  15161. Add([
  15162. 'type',
  15163. ' integer = longint;',
  15164. ' TClass = class of TObject;',
  15165. ' TObject = class',
  15166. ' public',
  15167. ' const cI: integer = 3;',
  15168. ' procedure DoIt;',
  15169. ' class procedure DoMore;',
  15170. ' end;',
  15171. 'procedure tobject.doit;',
  15172. 'begin',
  15173. ' if cI=4 then;',
  15174. ' if 5=cI then;',
  15175. ' if Self.cI=6 then;',
  15176. ' if 7=Self.cI then;',
  15177. ' with Self do begin',
  15178. ' if cI=11 then;',
  15179. ' if 12=cI then;',
  15180. ' end;',
  15181. 'end;',
  15182. 'class procedure tobject.domore;',
  15183. 'begin',
  15184. ' if cI=8 then;',
  15185. ' if Self.cI=9 then;',
  15186. ' if 10=cI then;',
  15187. ' if 11=Self.cI then;',
  15188. ' with Self do begin',
  15189. ' if cI=13 then;',
  15190. ' if 14=cI then;',
  15191. ' end;',
  15192. 'end;',
  15193. 'var',
  15194. ' Obj: TObject;',
  15195. ' Cla: TClass;',
  15196. 'begin',
  15197. ' if TObject.cI=21 then ;',
  15198. ' if Obj.cI=22 then ;',
  15199. ' if Cla.cI=23 then ;',
  15200. ' with obj do if ci=24 then;',
  15201. ' with TObject do if ci=25 then;',
  15202. ' with Cla do if ci=26 then;']);
  15203. ConvertProgram;
  15204. CheckSource('TestClass_Const',
  15205. LinesToStr([
  15206. 'rtl.createClass(this, "TObject", null, function () {',
  15207. ' this.cI = 3;',
  15208. ' this.$init = function () {',
  15209. ' };',
  15210. ' this.$final = function () {',
  15211. ' };',
  15212. ' this.DoIt = function () {',
  15213. ' if (this.cI === 4) ;',
  15214. ' if (5 === this.cI) ;',
  15215. ' if (this.cI === 6) ;',
  15216. ' if (7 === this.cI) ;',
  15217. ' if (this.cI === 11) ;',
  15218. ' if (12 === this.cI) ;',
  15219. ' };',
  15220. ' this.DoMore = function () {',
  15221. ' if (this.cI === 8) ;',
  15222. ' if (this.cI === 9) ;',
  15223. ' if (10 === this.cI) ;',
  15224. ' if (11 === this.cI) ;',
  15225. ' if (this.cI === 13) ;',
  15226. ' if (14 === this.cI) ;',
  15227. ' };',
  15228. '});',
  15229. 'this.Obj = null;',
  15230. 'this.Cla = null;',
  15231. '']),
  15232. LinesToStr([
  15233. 'if ($mod.TObject.cI === 21) ;',
  15234. 'if ($mod.Obj.cI === 22) ;',
  15235. 'if ($mod.Cla.cI === 23) ;',
  15236. 'var $with = $mod.Obj;',
  15237. 'if ($with.cI === 24) ;',
  15238. 'var $with1 = $mod.TObject;',
  15239. 'if ($with1.cI === 25) ;',
  15240. 'var $with2 = $mod.Cla;',
  15241. 'if ($with2.cI === 26) ;',
  15242. '']));
  15243. end;
  15244. procedure TTestModule.TestClass_ConstEnum;
  15245. begin
  15246. StartProgram(false);
  15247. Add([
  15248. 'type',
  15249. ' TEnum = (red,blue);',
  15250. ' TObject = class',
  15251. ' end;',
  15252. ' TAnimal = class',
  15253. ' public',
  15254. ' type TSubEnum = (light,dark);',
  15255. ' const a = high(TEnum);',
  15256. ' const b = high(TSubEnum);',
  15257. ' end;',
  15258. ' TBird = class(TAnimal)',
  15259. ' public',
  15260. ' const c = high(TEnum);',
  15261. ' const d = high(TSubEnum);',
  15262. ' end;',
  15263. ' TAnt = class',
  15264. ' public',
  15265. ' const e = high(TEnum);',
  15266. ' const f = high(TBird.TSubEnum);',
  15267. ' end;',
  15268. 'begin',
  15269. '']);
  15270. ConvertProgram;
  15271. CheckSource('TestClass_ConstEnum',
  15272. LinesToStr([
  15273. 'this.TEnum = {',
  15274. ' "0": "red",',
  15275. ' red: 0,',
  15276. ' "1": "blue",',
  15277. ' blue: 1',
  15278. '};',
  15279. 'rtl.createClass(this, "TObject", null, function () {',
  15280. ' this.$init = function () {',
  15281. ' };',
  15282. ' this.$final = function () {',
  15283. ' };',
  15284. '});',
  15285. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15286. ' this.TSubEnum = {',
  15287. ' "0": "light",',
  15288. ' light: 0,',
  15289. ' "1": "dark",',
  15290. ' dark: 1',
  15291. ' };',
  15292. ' this.a = $mod.TEnum.blue;',
  15293. ' this.b = this.TSubEnum.dark;',
  15294. '});',
  15295. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15296. ' this.c = $mod.TEnum.blue;',
  15297. ' this.d = this.TSubEnum.dark;',
  15298. '});',
  15299. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  15300. ' this.e = $mod.TEnum.blue;',
  15301. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  15302. '});',
  15303. '']),
  15304. LinesToStr([
  15305. '']));
  15306. end;
  15307. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  15308. begin
  15309. StartProgram(false);
  15310. Add([
  15311. 'type',
  15312. ' TObject = class',
  15313. ' const cI: longint = 3;',
  15314. ' procedure Fly;',
  15315. ' procedure Run;',
  15316. ' end;',
  15317. ' TBird = class',
  15318. ' procedure Go;',
  15319. ' end;',
  15320. 'procedure tobject.fly;',
  15321. 'const cI: word = 4;',
  15322. 'begin',
  15323. ' if cI=Self.cI then ;',
  15324. 'end;',
  15325. 'procedure tobject.run;',
  15326. 'const cI: word = 5;',
  15327. 'begin',
  15328. ' if cI=Self.cI then ;',
  15329. 'end;',
  15330. 'procedure tbird.go;',
  15331. 'const cI: word = 6;',
  15332. 'begin',
  15333. ' if cI=Self.cI then ;',
  15334. 'end;',
  15335. 'begin',
  15336. '']);
  15337. ConvertProgram;
  15338. CheckSource('TestClass_LocalConstDuplicate_Prg',
  15339. LinesToStr([
  15340. 'rtl.createClass(this, "TObject", null, function () {',
  15341. ' this.cI = 3;',
  15342. ' this.$init = function () {',
  15343. ' };',
  15344. ' this.$final = function () {',
  15345. ' };',
  15346. ' var cI$1 = 4;',
  15347. ' this.Fly = function () {',
  15348. ' if (cI$1 === this.cI) ;',
  15349. ' };',
  15350. ' var cI$2 = 5;',
  15351. ' this.Run = function () {',
  15352. ' if (cI$2 === this.cI) ;',
  15353. ' };',
  15354. '});',
  15355. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15356. ' var cI$3 = 6;',
  15357. ' this.Go = function () {',
  15358. ' if (cI$3 === this.cI) ;',
  15359. ' };',
  15360. '});',
  15361. '']),
  15362. LinesToStr([
  15363. '']));
  15364. end;
  15365. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15366. begin
  15367. StartUnit(false);
  15368. Add([
  15369. 'interface',
  15370. 'type',
  15371. ' TObject = class',
  15372. ' const cI: longint = 3;',
  15373. ' procedure Fly;',
  15374. ' procedure Run;',
  15375. ' end;',
  15376. ' TBird = class',
  15377. ' procedure Go;',
  15378. ' end;',
  15379. 'implementation',
  15380. 'procedure tobject.fly;',
  15381. 'const cI: word = 4;',
  15382. 'begin',
  15383. ' if cI=Self.cI then ;',
  15384. 'end;',
  15385. 'procedure tobject.run;',
  15386. 'const cI: word = 5;',
  15387. 'begin',
  15388. ' if cI=Self.cI then ;',
  15389. 'end;',
  15390. 'procedure tbird.go;',
  15391. 'const cI: word = 6;',
  15392. 'begin',
  15393. ' if cI=Self.cI then ;',
  15394. 'end;',
  15395. '']);
  15396. ConvertUnit;
  15397. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15398. LinesToStr([
  15399. 'rtl.createClass(this, "TObject", null, function () {',
  15400. ' this.cI = 3;',
  15401. ' this.$init = function () {',
  15402. ' };',
  15403. ' this.$final = function () {',
  15404. ' };',
  15405. ' var cI$1 = 4;',
  15406. ' this.Fly = function () {',
  15407. ' if (cI$1 === this.cI) ;',
  15408. ' };',
  15409. ' var cI$2 = 5;',
  15410. ' this.Run = function () {',
  15411. ' if (cI$2 === this.cI) ;',
  15412. ' };',
  15413. '});',
  15414. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15415. ' var cI$3 = 6;',
  15416. ' this.Go = function () {',
  15417. ' if (cI$3 === this.cI) ;',
  15418. ' };',
  15419. '});',
  15420. '']),
  15421. '',
  15422. '');
  15423. end;
  15424. procedure TTestModule.TestClass_LocalVarSelfFail;
  15425. begin
  15426. StartProgram(false);
  15427. Add([
  15428. 'type',
  15429. ' TObject = class',
  15430. ' constructor Create;',
  15431. ' end;',
  15432. 'constructor tobject.create;',
  15433. 'var self: longint;',
  15434. 'begin',
  15435. 'end',
  15436. 'begin',
  15437. '']);
  15438. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15439. ConvertProgram;
  15440. end;
  15441. procedure TTestModule.TestClass_ArgSelfFail;
  15442. begin
  15443. StartProgram(false);
  15444. Add([
  15445. 'type',
  15446. ' TObject = class',
  15447. ' procedure DoIt(Self: longint);',
  15448. ' end;',
  15449. 'procedure tobject.doit(self: longint);',
  15450. 'begin',
  15451. 'end',
  15452. 'begin',
  15453. '']);
  15454. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15455. ConvertProgram;
  15456. end;
  15457. procedure TTestModule.TestClass_NestedProcSelf;
  15458. begin
  15459. StartProgram(false);
  15460. Add([
  15461. 'type',
  15462. ' TObject = class',
  15463. ' Key: longint;',
  15464. ' class var State: longint;',
  15465. ' procedure DoIt;',
  15466. ' function GetSize: longint; virtual; abstract;',
  15467. ' procedure SetSize(Value: longint); virtual; abstract;',
  15468. ' property Size: longint read GetSize write SetSize;',
  15469. ' end;',
  15470. 'procedure tobject.doit;',
  15471. ' procedure Sub;',
  15472. ' begin',
  15473. ' key:=key+2;',
  15474. ' self.key:=self.key+3;',
  15475. ' state:=state+4;',
  15476. ' self.state:=self.state+5;',
  15477. ' tobject.state:=tobject.state+6;',
  15478. ' size:=size+7;',
  15479. ' self.size:=self.size+8;',
  15480. ' end;',
  15481. 'begin',
  15482. ' sub;',
  15483. ' key:=key+12;',
  15484. ' self.key:=self.key+13;',
  15485. ' state:=state+14;',
  15486. ' self.state:=self.state+15;',
  15487. ' tobject.state:=tobject.state+16;',
  15488. ' size:=size+17;',
  15489. ' self.size:=self.size+18;',
  15490. 'end;',
  15491. 'begin',
  15492. '']);
  15493. ConvertProgram;
  15494. CheckSource('TestClass_NestedProcSelf',
  15495. LinesToStr([ // statements
  15496. 'rtl.createClass(this, "TObject", null, function () {',
  15497. ' this.State = 0;',
  15498. ' this.$init = function () {',
  15499. ' this.Key = 0;',
  15500. ' };',
  15501. ' this.$final = function () {',
  15502. ' };',
  15503. ' this.DoIt = function () {',
  15504. ' var $Self = this;',
  15505. ' function Sub() {',
  15506. ' $Self.Key = $Self.Key + 2;',
  15507. ' $Self.Key = $Self.Key + 3;',
  15508. ' $mod.TObject.State = $Self.State + 4;',
  15509. ' $mod.TObject.State = $Self.State + 5;',
  15510. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15511. ' $Self.SetSize($Self.GetSize() + 7);',
  15512. ' $Self.SetSize($Self.GetSize() + 8);',
  15513. ' };',
  15514. ' Sub();',
  15515. ' this.Key = this.Key + 12;',
  15516. ' $Self.Key = $Self.Key + 13;',
  15517. ' $mod.TObject.State = this.State + 14;',
  15518. ' $mod.TObject.State = $Self.State + 15;',
  15519. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15520. ' this.SetSize(this.GetSize() + 17);',
  15521. ' $Self.SetSize($Self.GetSize() + 18);',
  15522. ' };',
  15523. '});',
  15524. '']),
  15525. LinesToStr([ // $mod.$main
  15526. '']));
  15527. end;
  15528. procedure TTestModule.TestClass_NestedProcSelf2;
  15529. begin
  15530. StartProgram(false);
  15531. Add([
  15532. 'type',
  15533. ' TObject = class',
  15534. ' Key: longint;',
  15535. ' class var State: longint;',
  15536. ' function GetSize: longint; virtual; abstract;',
  15537. ' procedure SetSize(Value: longint); virtual; abstract;',
  15538. ' property Size: longint read GetSize write SetSize;',
  15539. ' end;',
  15540. ' TBird = class',
  15541. ' procedure DoIt;',
  15542. ' end;',
  15543. 'procedure tbird.doit;',
  15544. ' procedure Sub;',
  15545. ' begin',
  15546. ' key:=key+2;',
  15547. ' self.key:=self.key+3;',
  15548. ' state:=state+4;',
  15549. ' self.state:=self.state+5;',
  15550. ' tobject.state:=tobject.state+6;',
  15551. ' size:=size+7;',
  15552. ' self.size:=self.size+8;',
  15553. ' end;',
  15554. 'begin',
  15555. ' sub;',
  15556. ' key:=key+12;',
  15557. ' self.key:=self.key+13;',
  15558. ' state:=state+14;',
  15559. ' self.state:=self.state+15;',
  15560. ' tobject.state:=tobject.state+16;',
  15561. ' size:=size+17;',
  15562. ' self.size:=self.size+18;',
  15563. 'end;',
  15564. 'begin',
  15565. '']);
  15566. ConvertProgram;
  15567. CheckSource('TestClass_NestedProcSelf2',
  15568. LinesToStr([ // statements
  15569. 'rtl.createClass(this, "TObject", null, function () {',
  15570. ' this.State = 0;',
  15571. ' this.$init = function () {',
  15572. ' this.Key = 0;',
  15573. ' };',
  15574. ' this.$final = function () {',
  15575. ' };',
  15576. '});',
  15577. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15578. ' this.DoIt = function () {',
  15579. ' var $Self = this;',
  15580. ' function Sub() {',
  15581. ' $Self.Key = $Self.Key + 2;',
  15582. ' $Self.Key = $Self.Key + 3;',
  15583. ' $mod.TObject.State = $Self.State + 4;',
  15584. ' $mod.TObject.State = $Self.State + 5;',
  15585. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15586. ' $Self.SetSize($Self.GetSize() + 7);',
  15587. ' $Self.SetSize($Self.GetSize() + 8);',
  15588. ' };',
  15589. ' Sub();',
  15590. ' this.Key = this.Key + 12;',
  15591. ' $Self.Key = $Self.Key + 13;',
  15592. ' $mod.TObject.State = this.State + 14;',
  15593. ' $mod.TObject.State = $Self.State + 15;',
  15594. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15595. ' this.SetSize(this.GetSize() + 17);',
  15596. ' $Self.SetSize($Self.GetSize() + 18);',
  15597. ' };',
  15598. '});',
  15599. '']),
  15600. LinesToStr([ // $mod.$main
  15601. '']));
  15602. end;
  15603. procedure TTestModule.TestClass_NestedProcClassSelf;
  15604. begin
  15605. StartProgram(false);
  15606. Add([
  15607. 'type',
  15608. ' TObject = class',
  15609. ' class var State: longint;',
  15610. ' class procedure DoIt;',
  15611. ' class function GetSize: longint; virtual; abstract;',
  15612. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15613. ' class property Size: longint read GetSize write SetSize;',
  15614. ' end;',
  15615. 'class procedure tobject.doit;',
  15616. ' procedure Sub;',
  15617. ' begin',
  15618. ' state:=state+2;',
  15619. ' self.state:=self.state+3;',
  15620. ' tobject.state:=tobject.state+4;',
  15621. ' size:=size+5;',
  15622. ' self.size:=self.size+6;',
  15623. ' tobject.size:=tobject.size+7;',
  15624. ' end;',
  15625. 'begin',
  15626. ' sub;',
  15627. ' state:=state+12;',
  15628. ' self.state:=self.state+13;',
  15629. ' tobject.state:=tobject.state+14;',
  15630. ' size:=size+15;',
  15631. ' self.size:=self.size+16;',
  15632. ' tobject.size:=tobject.size+17;',
  15633. 'end;',
  15634. 'begin',
  15635. '']);
  15636. ConvertProgram;
  15637. CheckSource('TestClass_NestedProcClassSelf',
  15638. LinesToStr([ // statements
  15639. 'rtl.createClass(this, "TObject", null, function () {',
  15640. ' this.State = 0;',
  15641. ' this.$init = function () {',
  15642. ' };',
  15643. ' this.$final = function () {',
  15644. ' };',
  15645. ' this.DoIt = function () {',
  15646. ' var $Self = this;',
  15647. ' function Sub() {',
  15648. ' $mod.TObject.State = $Self.State + 2;',
  15649. ' $mod.TObject.State = $Self.State + 3;',
  15650. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15651. ' $Self.SetSize($Self.GetSize() + 5);',
  15652. ' $Self.SetSize($Self.GetSize() + 6);',
  15653. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15654. ' };',
  15655. ' Sub();',
  15656. ' $mod.TObject.State = this.State + 12;',
  15657. ' $mod.TObject.State = $Self.State + 13;',
  15658. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15659. ' this.SetSize(this.GetSize() + 15);',
  15660. ' $Self.SetSize($Self.GetSize() + 16);',
  15661. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15662. ' };',
  15663. '});',
  15664. '']),
  15665. LinesToStr([ // $mod.$main
  15666. '']));
  15667. end;
  15668. procedure TTestModule.TestClass_NestedProcCallInherited;
  15669. begin
  15670. StartProgram(false);
  15671. Add([
  15672. 'type',
  15673. ' TObject = class',
  15674. ' function DoIt(k: boolean): longint; virtual;',
  15675. ' end;',
  15676. ' TBird = class',
  15677. ' function DoIt(k: boolean): longint; override;',
  15678. ' end;',
  15679. 'function tobject.doit(k: boolean): longint;',
  15680. 'begin',
  15681. 'end;',
  15682. 'function tbird.doit(k: boolean): longint;',
  15683. ' procedure Sub;',
  15684. ' begin',
  15685. ' inherited DoIt(true);',
  15686. //' if inherited DoIt(false)=4 then ;',
  15687. ' end;',
  15688. 'begin',
  15689. ' Sub;',
  15690. ' inherited;',
  15691. ' inherited DoIt(true);',
  15692. //' if inherited DoIt(false)=14 then ;',
  15693. 'end;',
  15694. 'begin',
  15695. '']);
  15696. ConvertProgram;
  15697. CheckSource('TestClass_NestedProcCallInherited',
  15698. LinesToStr([ // statements
  15699. 'rtl.createClass(this, "TObject", null, function () {',
  15700. ' this.$init = function () {',
  15701. ' };',
  15702. ' this.$final = function () {',
  15703. ' };',
  15704. ' this.DoIt = function (k) {',
  15705. ' var Result = 0;',
  15706. ' return Result;',
  15707. ' };',
  15708. '});',
  15709. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15710. ' this.DoIt = function (k) {',
  15711. ' var $Self = this;',
  15712. ' var Result = 0;',
  15713. ' function Sub() {',
  15714. ' $mod.TObject.DoIt.call($Self, true);',
  15715. ' };',
  15716. ' Sub();',
  15717. ' $mod.TObject.DoIt.apply(this, arguments);',
  15718. ' $mod.TObject.DoIt.call(this, true);',
  15719. ' return Result;',
  15720. ' };',
  15721. '});',
  15722. '']),
  15723. LinesToStr([ // $mod.$main
  15724. '']));
  15725. end;
  15726. procedure TTestModule.TestClass_TObjectFree;
  15727. begin
  15728. StartProgram(false);
  15729. Add([
  15730. 'type',
  15731. ' TObject = class',
  15732. ' Obj: tobject;',
  15733. ' procedure Free;',
  15734. ' procedure Release;',
  15735. ' end;',
  15736. 'procedure tobject.free;',
  15737. 'begin',
  15738. 'end;',
  15739. 'procedure tobject.release;',
  15740. 'begin',
  15741. ' free;',
  15742. ' if true then free;',
  15743. 'end;',
  15744. 'function DoIt(o: tobject): tobject;',
  15745. 'var l: tobject;',
  15746. 'begin',
  15747. ' o.free;',
  15748. ' o.free();',
  15749. ' l.free;',
  15750. ' l.free();',
  15751. ' o.obj.free;',
  15752. ' o.obj.free();',
  15753. ' with o do obj.free;',
  15754. ' with o do obj.free();',
  15755. ' result.Free;',
  15756. ' result.Free();',
  15757. 'end;',
  15758. 'var o: tobject;',
  15759. ' a: array of tobject;',
  15760. 'begin',
  15761. ' o.free;',
  15762. ' o.obj.free;',
  15763. ' a[1+2].free;',
  15764. '']);
  15765. ConvertProgram;
  15766. CheckSource('TestClass_TObjectFree',
  15767. LinesToStr([ // statements
  15768. 'rtl.createClass(this, "TObject", null, function () {',
  15769. ' this.$init = function () {',
  15770. ' this.Obj = null;',
  15771. ' };',
  15772. ' this.$final = function () {',
  15773. ' this.Obj = undefined;',
  15774. ' };',
  15775. ' this.Free = function () {',
  15776. ' };',
  15777. ' this.Release = function () {',
  15778. ' this.Free();',
  15779. ' if (true) this.Free();',
  15780. ' };',
  15781. '});',
  15782. 'this.DoIt = function (o) {',
  15783. ' var Result = null;',
  15784. ' var l = null;',
  15785. ' o = rtl.freeLoc(o);',
  15786. ' o = rtl.freeLoc(o);',
  15787. ' l = rtl.freeLoc(l);',
  15788. ' l = rtl.freeLoc(l);',
  15789. ' rtl.free(o, "Obj");',
  15790. ' rtl.free(o, "Obj");',
  15791. ' rtl.free(o, "Obj");',
  15792. ' rtl.free(o, "Obj");',
  15793. ' Result = rtl.freeLoc(Result);',
  15794. ' Result = rtl.freeLoc(Result);',
  15795. ' return Result;',
  15796. '};',
  15797. 'this.o = null;',
  15798. 'this.a = [];',
  15799. '']),
  15800. LinesToStr([ // $mod.$main
  15801. 'rtl.free($mod, "o");',
  15802. 'rtl.free($mod.o, "Obj");',
  15803. 'rtl.free($mod.a, 1 + 2);',
  15804. '']));
  15805. end;
  15806. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15807. begin
  15808. StartProgram(false);
  15809. Add([
  15810. 'type',
  15811. ' TObject = class',
  15812. ' Obj: tobject;',
  15813. ' procedure Free;',
  15814. ' end;',
  15815. 'procedure tobject.free;',
  15816. 'begin',
  15817. 'end;',
  15818. 'procedure DoIt(var o: tobject);',
  15819. 'begin',
  15820. ' o.free;',
  15821. ' o.free();',
  15822. 'end;',
  15823. 'begin',
  15824. '']);
  15825. ConvertProgram;
  15826. CheckSource('TestClass_TObjectFree_VarArg',
  15827. LinesToStr([ // statements
  15828. 'rtl.createClass(this, "TObject", null, function () {',
  15829. ' this.$init = function () {',
  15830. ' this.Obj = null;',
  15831. ' };',
  15832. ' this.$final = function () {',
  15833. ' this.Obj = undefined;',
  15834. ' };',
  15835. ' this.Free = function () {',
  15836. ' };',
  15837. '});',
  15838. 'this.DoIt = function (o) {',
  15839. ' o.set(rtl.freeLoc(o.get()));',
  15840. ' o.set(rtl.freeLoc(o.get()));',
  15841. '};',
  15842. '']),
  15843. LinesToStr([ // $mod.$main
  15844. '']));
  15845. end;
  15846. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15847. begin
  15848. StartProgram(false);
  15849. Add([
  15850. 'type',
  15851. ' TObject = class',
  15852. ' constructor Create;',
  15853. ' procedure Free;',
  15854. ' end;',
  15855. 'constructor TObject.Create; begin end;',
  15856. 'procedure tobject.free; begin end;',
  15857. 'begin',
  15858. ' with tobject.create do free;',
  15859. '']);
  15860. ConvertProgram;
  15861. CheckSource('TestClass_TObjectFreeNewInstance',
  15862. LinesToStr([ // statements
  15863. 'rtl.createClass(this, "TObject", null, function () {',
  15864. ' this.$init = function () {',
  15865. ' };',
  15866. ' this.$final = function () {',
  15867. ' };',
  15868. ' this.Create = function () {',
  15869. ' return this;',
  15870. ' };',
  15871. ' this.Free = function () {',
  15872. ' };',
  15873. '});',
  15874. '']),
  15875. LinesToStr([ // $mod.$main
  15876. 'var $with = $mod.TObject.$create("Create");',
  15877. '$with=rtl.freeLoc($with);',
  15878. '']));
  15879. end;
  15880. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15881. begin
  15882. StartProgram(false);
  15883. Add([
  15884. 'type',
  15885. ' TObject = class',
  15886. ' destructor Destroy;',
  15887. ' procedure Free;',
  15888. ' end;',
  15889. 'destructor TObject.Destroy; begin end;',
  15890. 'procedure tobject.free; begin end;',
  15891. 'var o: tobject;',
  15892. 'begin',
  15893. ' o.free;',
  15894. '']);
  15895. Converter.UseLowerCase:=true;
  15896. ConvertProgram;
  15897. CheckSource('TestClass_TObjectFreeLowerCase',
  15898. LinesToStr([ // statements
  15899. 'rtl.createClass(this, "tobject", null, function () {',
  15900. ' this.$init = function () {',
  15901. ' };',
  15902. ' this.$final = function () {',
  15903. ' };',
  15904. ' rtl.tObjectDestroy = "destroy";',
  15905. ' this.destroy = function () {',
  15906. ' };',
  15907. ' this.free = function () {',
  15908. ' };',
  15909. '});',
  15910. 'this.o = null;',
  15911. '']),
  15912. LinesToStr([ // $mod.$main
  15913. 'rtl.free($mod, "o");',
  15914. '']));
  15915. end;
  15916. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15917. begin
  15918. StartProgram(false);
  15919. Add([
  15920. 'type',
  15921. ' TObject = class',
  15922. ' procedure Free;',
  15923. ' function GetObj: tobject; virtual; abstract;',
  15924. ' end;',
  15925. 'procedure tobject.free;',
  15926. 'begin',
  15927. 'end;',
  15928. 'var o: tobject;',
  15929. 'begin',
  15930. ' o.getobj.free;',
  15931. '']);
  15932. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15933. ConvertProgram;
  15934. end;
  15935. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15936. begin
  15937. StartProgram(false);
  15938. Add([
  15939. 'type',
  15940. ' TObject = class',
  15941. ' procedure Free;',
  15942. ' FObj: TObject;',
  15943. ' property Obj: tobject read FObj write FObj;',
  15944. ' end;',
  15945. 'procedure tobject.free;',
  15946. 'begin',
  15947. 'end;',
  15948. 'var o: tobject;',
  15949. 'begin',
  15950. ' o.obj.free;',
  15951. '']);
  15952. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15953. ConvertProgram;
  15954. end;
  15955. procedure TTestModule.TestClass_ForIn;
  15956. begin
  15957. StartProgram(false);
  15958. Add([
  15959. 'type',
  15960. ' TObject = class end;',
  15961. ' TItem = TObject;',
  15962. ' TEnumerator = class',
  15963. ' FCurrent: TItem;',
  15964. ' property Current: TItem read FCurrent;',
  15965. ' function MoveNext: boolean;',
  15966. ' end;',
  15967. ' TBird = class',
  15968. ' function GetEnumerator: TEnumerator;',
  15969. ' end;',
  15970. 'function TEnumerator.MoveNext: boolean;',
  15971. 'begin',
  15972. 'end;',
  15973. 'function TBird.GetEnumerator: TEnumerator;',
  15974. 'begin',
  15975. 'end;',
  15976. 'var',
  15977. ' b: TBird;',
  15978. ' i, i2: TItem;',
  15979. 'begin',
  15980. ' for i in b do i2:=i;']);
  15981. ConvertProgram;
  15982. CheckSource('TestClass_ForIn',
  15983. LinesToStr([ // statements
  15984. 'rtl.createClass(this, "TObject", null, function () {',
  15985. ' this.$init = function () {',
  15986. ' };',
  15987. ' this.$final = function () {',
  15988. ' };',
  15989. '});',
  15990. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15991. ' this.$init = function () {',
  15992. ' $mod.TObject.$init.call(this);',
  15993. ' this.FCurrent = null;',
  15994. ' };',
  15995. ' this.$final = function () {',
  15996. ' this.FCurrent = undefined;',
  15997. ' $mod.TObject.$final.call(this);',
  15998. ' };',
  15999. ' this.MoveNext = function () {',
  16000. ' var Result = false;',
  16001. ' return Result;',
  16002. ' };',
  16003. '});',
  16004. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16005. ' this.GetEnumerator = function () {',
  16006. ' var Result = null;',
  16007. ' return Result;',
  16008. ' };',
  16009. '});',
  16010. 'this.b = null;',
  16011. 'this.i = null;',
  16012. 'this.i2 = null;'
  16013. ]),
  16014. LinesToStr([ // $mod.$main
  16015. 'var $in = $mod.b.GetEnumerator();',
  16016. 'try {',
  16017. ' while ($in.MoveNext()){',
  16018. ' $mod.i = $in.FCurrent;',
  16019. ' $mod.i2 = $mod.i;',
  16020. ' }',
  16021. '} finally {',
  16022. ' $in = rtl.freeLoc($in)',
  16023. '};',
  16024. '']));
  16025. end;
  16026. procedure TTestModule.TestClass_DispatchMessage;
  16027. begin
  16028. StartProgram(false);
  16029. Add([
  16030. 'type',
  16031. ' TObject = class',
  16032. ' {$DispatchField DispInt}',
  16033. ' procedure Dispatch(var Msg); virtual; abstract;',
  16034. ' {$DispatchStrField DispStr}',
  16035. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16036. ' end;',
  16037. ' THopMsg = record',
  16038. ' DispInt: longint;',
  16039. ' end;',
  16040. ' TPutMsg = record',
  16041. ' DispStr: string;',
  16042. ' end;',
  16043. ' TBird = class',
  16044. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16045. ' procedure Run; overload; virtual; abstract;',
  16046. ' procedure Run(var Msg); overload; message ''Fast'';',
  16047. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16048. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16049. ' end;',
  16050. 'procedure TBird.Run(var Msg);',
  16051. 'begin',
  16052. 'end;',
  16053. 'begin',
  16054. '']);
  16055. ConvertProgram;
  16056. CheckSource('TestClass_Message',
  16057. LinesToStr([ // statements
  16058. 'rtl.createClass(this, "TObject", null, function () {',
  16059. ' this.$init = function () {',
  16060. ' };',
  16061. ' this.$final = function () {',
  16062. ' };',
  16063. '});',
  16064. 'rtl.recNewT(this, "THopMsg", function () {',
  16065. ' this.DispInt = 0;',
  16066. ' this.$eq = function (b) {',
  16067. ' return this.DispInt === b.DispInt;',
  16068. ' };',
  16069. ' this.$assign = function (s) {',
  16070. ' this.DispInt = s.DispInt;',
  16071. ' return this;',
  16072. ' };',
  16073. '});',
  16074. 'rtl.recNewT(this, "TPutMsg", function () {',
  16075. ' this.DispStr = "";',
  16076. ' this.$eq = function (b) {',
  16077. ' return this.DispStr === b.DispStr;',
  16078. ' };',
  16079. ' this.$assign = function (s) {',
  16080. ' this.DispStr = s.DispStr;',
  16081. ' return this;',
  16082. ' };',
  16083. '});',
  16084. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16085. ' this.Run$1 = function (Msg) {',
  16086. ' };',
  16087. ' this.$msgint = {',
  16088. ' "2": "Fly",',
  16089. ' "3": "Hop"',
  16090. ' };',
  16091. ' this.$msgstr = {',
  16092. ' Fast: "Run$1",',
  16093. ' foo: "Put"',
  16094. ' };',
  16095. '});',
  16096. '']),
  16097. LinesToStr([ // $mod.$main
  16098. '']));
  16099. end;
  16100. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  16101. begin
  16102. StartProgram(false);
  16103. Add([
  16104. 'type',
  16105. ' TObject = class',
  16106. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  16107. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  16108. ' end;',
  16109. 'begin',
  16110. '']);
  16111. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  16112. ConvertProgram;
  16113. end;
  16114. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  16115. begin
  16116. StartProgram(false);
  16117. Add([
  16118. 'type',
  16119. ' TObject = class',
  16120. ' {$dispatchfield Msg}',
  16121. ' procedure Dispatch(var Msg); virtual; abstract;',
  16122. ' end;',
  16123. ' TFlyMsg = record',
  16124. ' FlyId: longint;',
  16125. ' end;',
  16126. ' TBird = class',
  16127. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  16128. ' end;',
  16129. 'begin',
  16130. '']);
  16131. ConvertProgram;
  16132. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  16133. end;
  16134. procedure TTestModule.TestClassOf_Create;
  16135. begin
  16136. StartProgram(false);
  16137. Add('type');
  16138. Add(' TObject = class');
  16139. Add(' constructor Create;');
  16140. Add(' end;');
  16141. Add(' TClass = class of TObject;');
  16142. Add('constructor tobject.create; begin end;');
  16143. Add('var');
  16144. Add(' Obj: tobject;');
  16145. Add(' C: tclass;');
  16146. Add('begin');
  16147. Add(' obj:=C.create;');
  16148. Add(' with c do obj:=create;');
  16149. ConvertProgram;
  16150. CheckSource('TestClassOf_Create',
  16151. LinesToStr([ // statements
  16152. 'rtl.createClass(this, "TObject", null, function () {',
  16153. ' this.$init = function () {',
  16154. ' };',
  16155. ' this.$final = function () {',
  16156. ' };',
  16157. ' this.Create = function () {',
  16158. ' return this;',
  16159. ' };',
  16160. '});',
  16161. 'this.Obj = null;',
  16162. 'this.C = null;'
  16163. ]),
  16164. LinesToStr([ // $mod.$main
  16165. '$mod.Obj = $mod.C.$create("Create");',
  16166. 'var $with = $mod.C;',
  16167. '$mod.Obj = $with.$create("Create");',
  16168. '']));
  16169. end;
  16170. procedure TTestModule.TestClassOf_Call;
  16171. begin
  16172. StartProgram(false);
  16173. Add('type');
  16174. Add(' TObject = class');
  16175. Add(' class procedure DoIt;');
  16176. Add(' end;');
  16177. Add(' TClass = class of TObject;');
  16178. Add('class procedure tobject.doit; begin end;');
  16179. Add('var');
  16180. Add(' C: tclass;');
  16181. Add('begin');
  16182. Add(' c.doit;');
  16183. Add(' with c do doit;');
  16184. ConvertProgram;
  16185. CheckSource('TestClassOf_Call',
  16186. LinesToStr([ // statements
  16187. 'rtl.createClass(this, "TObject", null, function () {',
  16188. ' this.$init = function () {',
  16189. ' };',
  16190. ' this.$final = function () {',
  16191. ' };',
  16192. ' this.DoIt = function () {',
  16193. ' };',
  16194. '});',
  16195. 'this.C = null;'
  16196. ]),
  16197. LinesToStr([ // $mod.$main
  16198. '$mod.C.DoIt();',
  16199. 'var $with = $mod.C;',
  16200. '$with.DoIt();',
  16201. '']));
  16202. end;
  16203. procedure TTestModule.TestClassOf_Assign;
  16204. begin
  16205. StartProgram(false);
  16206. Add('type');
  16207. Add(' TClass = class of TObject;');
  16208. Add(' TObject = class');
  16209. Add(' ClassType: TClass; ');
  16210. Add(' end;');
  16211. Add('var');
  16212. Add(' Obj: tobject;');
  16213. Add(' C: tclass;');
  16214. Add('begin');
  16215. Add(' c:=nil;');
  16216. Add(' c:=obj.classtype;');
  16217. ConvertProgram;
  16218. CheckSource('TestClassOf_Assign',
  16219. LinesToStr([ // statements
  16220. 'rtl.createClass(this, "TObject", null, function () {',
  16221. ' this.$init = function () {',
  16222. ' this.ClassType = null;',
  16223. ' };',
  16224. ' this.$final = function () {',
  16225. ' this.ClassType = undefined;',
  16226. ' };',
  16227. '});',
  16228. 'this.Obj = null;',
  16229. 'this.C = null;'
  16230. ]),
  16231. LinesToStr([ // $mod.$main
  16232. '$mod.C = null;',
  16233. '$mod.C = $mod.Obj.ClassType;',
  16234. '']));
  16235. end;
  16236. procedure TTestModule.TestClassOf_Is;
  16237. begin
  16238. StartProgram(false);
  16239. Add('type');
  16240. Add(' TClass = class of TObject;');
  16241. Add(' TObject = class');
  16242. Add(' end;');
  16243. Add(' TCar = class');
  16244. Add(' end;');
  16245. Add(' TCars = class of TCar;');
  16246. Add('var');
  16247. Add(' Obj: tobject;');
  16248. Add(' C: tclass;');
  16249. Add(' Cars: tcars;');
  16250. Add('begin');
  16251. Add(' if c is tcar then ;');
  16252. Add(' if c is tcars then ;');
  16253. ConvertProgram;
  16254. CheckSource('TestClassOf_Is',
  16255. LinesToStr([ // statements
  16256. 'rtl.createClass(this, "TObject", null, function () {',
  16257. ' this.$init = function () {',
  16258. ' };',
  16259. ' this.$final = function () {',
  16260. ' };',
  16261. '});',
  16262. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  16263. '});',
  16264. 'this.Obj = null;',
  16265. 'this.C = null;',
  16266. 'this.Cars = null;'
  16267. ]),
  16268. LinesToStr([ // $mod.$main
  16269. 'if(rtl.is($mod.C,$mod.TCar));',
  16270. 'if(rtl.is($mod.C,$mod.TCar));',
  16271. '']));
  16272. end;
  16273. procedure TTestModule.TestClassOf_Compare;
  16274. begin
  16275. StartProgram(false);
  16276. Add('type');
  16277. Add(' TClass = class of TObject;');
  16278. Add(' TObject = class');
  16279. Add(' ClassType: TClass; ');
  16280. Add(' end;');
  16281. Add('var');
  16282. Add(' b: boolean;');
  16283. Add(' Obj: tobject;');
  16284. Add(' C: tclass;');
  16285. Add('begin');
  16286. Add(' b:=c=nil;');
  16287. Add(' b:=nil=c;');
  16288. Add(' b:=c=obj.classtype;');
  16289. Add(' b:=obj.classtype=c;');
  16290. Add(' b:=c=TObject;');
  16291. Add(' b:=TObject=c;');
  16292. Add(' b:=c<>nil;');
  16293. Add(' b:=nil<>c;');
  16294. Add(' b:=c<>obj.classtype;');
  16295. Add(' b:=obj.classtype<>c;');
  16296. Add(' b:=c<>TObject;');
  16297. Add(' b:=TObject<>c;');
  16298. ConvertProgram;
  16299. CheckSource('TestClassOf_Compare',
  16300. LinesToStr([ // statements
  16301. 'rtl.createClass(this, "TObject", null, function () {',
  16302. ' this.$init = function () {',
  16303. ' this.ClassType = null;',
  16304. ' };',
  16305. ' this.$final = function () {',
  16306. ' this.ClassType = undefined;',
  16307. ' };',
  16308. '});',
  16309. 'this.b = false;',
  16310. 'this.Obj = null;',
  16311. 'this.C = null;'
  16312. ]),
  16313. LinesToStr([ // $mod.$main
  16314. '$mod.b = $mod.C === null;',
  16315. '$mod.b = null === $mod.C;',
  16316. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  16317. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  16318. '$mod.b = $mod.C === $mod.TObject;',
  16319. '$mod.b = $mod.TObject === $mod.C;',
  16320. '$mod.b = $mod.C !== null;',
  16321. '$mod.b = null !== $mod.C;',
  16322. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  16323. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  16324. '$mod.b = $mod.C !== $mod.TObject;',
  16325. '$mod.b = $mod.TObject !== $mod.C;',
  16326. '']));
  16327. end;
  16328. procedure TTestModule.TestClassOf_ClassVar;
  16329. begin
  16330. StartProgram(false);
  16331. Add('type');
  16332. Add(' TObject = class');
  16333. Add(' class var id: longint;');
  16334. Add(' end;');
  16335. Add(' TClass = class of TObject;');
  16336. Add('var');
  16337. Add(' C: tclass;');
  16338. Add('begin');
  16339. Add(' C.id:=C.id;');
  16340. ConvertProgram;
  16341. CheckSource('TestClassOf_ClassVar',
  16342. LinesToStr([ // statements
  16343. 'rtl.createClass(this, "TObject", null, function () {',
  16344. ' this.id = 0;',
  16345. ' this.$init = function () {',
  16346. ' };',
  16347. ' this.$final = function () {',
  16348. ' };',
  16349. '});',
  16350. 'this.C = null;'
  16351. ]),
  16352. LinesToStr([ // $mod.$main
  16353. '$mod.TObject.id = $mod.C.id;',
  16354. '']));
  16355. end;
  16356. procedure TTestModule.TestClassOf_ClassMethod;
  16357. begin
  16358. StartProgram(false);
  16359. Add('type');
  16360. Add(' TObject = class');
  16361. Add(' class function DoIt(i: longint = 0): longint;');
  16362. Add(' end;');
  16363. Add(' TClass = class of TObject;');
  16364. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16365. Add('var');
  16366. Add(' i: longint;');
  16367. Add(' C: tclass;');
  16368. Add('begin');
  16369. Add(' C.DoIt;');
  16370. Add(' C.DoIt();');
  16371. Add(' i:=C.DoIt;');
  16372. Add(' i:=C.DoIt();');
  16373. ConvertProgram;
  16374. CheckSource('TestClassOf_ClassMethod',
  16375. LinesToStr([ // statements
  16376. 'rtl.createClass(this, "TObject", null, function () {',
  16377. ' this.$init = function () {',
  16378. ' };',
  16379. ' this.$final = function () {',
  16380. ' };',
  16381. ' this.DoIt = function (i) {',
  16382. ' var Result = 0;',
  16383. ' return Result;',
  16384. ' };',
  16385. '});',
  16386. 'this.i = 0;',
  16387. 'this.C = null;'
  16388. ]),
  16389. LinesToStr([ // $mod.$main
  16390. '$mod.C.DoIt(0);',
  16391. '$mod.C.DoIt(0);',
  16392. '$mod.i = $mod.C.DoIt(0);',
  16393. '$mod.i = $mod.C.DoIt(0);',
  16394. '']));
  16395. end;
  16396. procedure TTestModule.TestClassOf_ClassProperty;
  16397. begin
  16398. StartProgram(false);
  16399. Add([
  16400. 'type',
  16401. ' TObject = class',
  16402. ' class var FA: longint;',
  16403. ' class function GetA: longint;',
  16404. ' class procedure SetA(Value: longint);',
  16405. ' class property pA: longint read fa write fa;',
  16406. ' class property pB: longint read geta write seta;',
  16407. ' end;',
  16408. ' TObjectClass = class of tobject;',
  16409. 'class function tobject.geta: longint; begin end;',
  16410. 'class procedure tobject.seta(value: longint); begin end;',
  16411. 'var',
  16412. ' b: boolean;',
  16413. ' Obj: tobject;',
  16414. ' Cla: tobjectclass;',
  16415. 'begin',
  16416. ' obj.pa:=obj.pa;',
  16417. ' obj.pb:=obj.pb;',
  16418. ' b:=obj.pa=4;',
  16419. ' b:=obj.pb=obj.pb;',
  16420. ' b:=5=obj.pa;',
  16421. ' cla.pa:=6;',
  16422. ' cla.pa:=cla.pa;',
  16423. ' cla.pb:=cla.pb;',
  16424. ' b:=cla.pa=7;',
  16425. ' b:=cla.pb=cla.pb;',
  16426. ' b:=8=cla.pa;',
  16427. ' tobject.pa:=9;',
  16428. ' tobject.pb:=tobject.pb;',
  16429. ' b:=tobject.pa=10;',
  16430. ' b:=11=tobject.pa;',
  16431. '']);
  16432. ConvertProgram;
  16433. CheckSource('TestClassOf_ClassProperty',
  16434. LinesToStr([ // statements
  16435. 'rtl.createClass(this, "TObject", null, function () {',
  16436. ' this.FA = 0;',
  16437. ' this.$init = function () {',
  16438. ' };',
  16439. ' this.$final = function () {',
  16440. ' };',
  16441. ' this.GetA = function () {',
  16442. ' var Result = 0;',
  16443. ' return Result;',
  16444. ' };',
  16445. ' this.SetA = function (Value) {',
  16446. ' };',
  16447. '});',
  16448. 'this.b = false;',
  16449. 'this.Obj = null;',
  16450. 'this.Cla = null;'
  16451. ]),
  16452. LinesToStr([ // $mod.$main
  16453. '$mod.TObject.FA = $mod.Obj.FA;',
  16454. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16455. '$mod.b = $mod.Obj.FA === 4;',
  16456. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16457. '$mod.b = 5 === $mod.Obj.FA;',
  16458. '$mod.TObject.FA = 6;',
  16459. '$mod.TObject.FA = $mod.Cla.FA;',
  16460. '$mod.Cla.SetA($mod.Cla.GetA());',
  16461. '$mod.b = $mod.Cla.FA === 7;',
  16462. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16463. '$mod.b = 8 === $mod.Cla.FA;',
  16464. '$mod.TObject.FA = 9;',
  16465. '$mod.TObject.SetA($mod.TObject.GetA());',
  16466. '$mod.b = $mod.TObject.FA === 10;',
  16467. '$mod.b = 11 === $mod.TObject.FA;',
  16468. '']));
  16469. end;
  16470. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16471. begin
  16472. StartProgram(false);
  16473. Add('type');
  16474. Add(' TObject = class');
  16475. Add(' class var GlobalId: longint;');
  16476. Add(' class procedure ProcA;');
  16477. Add(' end;');
  16478. Add('class procedure tobject.proca;');
  16479. Add('var b: boolean;');
  16480. Add('begin');
  16481. Add(' b:=self=nil;');
  16482. Add(' b:=self.globalid=3;');
  16483. Add(' b:=4=self.globalid;');
  16484. Add(' self.globalid:=5;');
  16485. Add(' self.proca;');
  16486. Add('end;');
  16487. Add('begin');
  16488. ConvertProgram;
  16489. CheckSource('TestClassOf_ClassMethodSelf',
  16490. LinesToStr([ // statements
  16491. 'rtl.createClass(this, "TObject", null, function () {',
  16492. ' this.GlobalId = 0;',
  16493. ' this.$init = function () {',
  16494. ' };',
  16495. ' this.$final = function () {',
  16496. ' };',
  16497. ' this.ProcA = function () {',
  16498. ' var b = false;',
  16499. ' b = this === null;',
  16500. ' b = this.GlobalId === 3;',
  16501. ' b = 4 === this.GlobalId;',
  16502. ' $mod.TObject.GlobalId = 5;',
  16503. ' this.ProcA();',
  16504. ' };',
  16505. '});'
  16506. ]),
  16507. LinesToStr([ // $mod.$main
  16508. '']));
  16509. end;
  16510. procedure TTestModule.TestClassOf_TypeCast;
  16511. begin
  16512. StartProgram(false);
  16513. Add('type');
  16514. Add(' TObject = class');
  16515. Add(' class procedure {#TObject_DoIt}DoIt;');
  16516. Add(' end;');
  16517. Add(' TClass = class of TObject;');
  16518. Add(' TMobile = class');
  16519. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16520. Add(' end;');
  16521. Add(' TMobileClass = class of TMobile;');
  16522. Add(' TCar = class(TMobile)');
  16523. Add(' class procedure {#TCar_DoIt}DoIt;');
  16524. Add(' end;');
  16525. Add(' TCarClass = class of TCar;');
  16526. Add('class procedure TObject.DoIt;');
  16527. Add('begin');
  16528. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16529. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16530. Add('end;');
  16531. Add('class procedure TMobile.DoIt;');
  16532. Add('begin');
  16533. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16534. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16535. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16536. Add('end;');
  16537. Add('class procedure TCar.DoIt; begin end;');
  16538. Add('var');
  16539. Add(' ObjC: TClass;');
  16540. Add(' MobileC: TMobileClass;');
  16541. Add(' CarC: TCarClass;');
  16542. Add('begin');
  16543. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16544. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16545. Add(' CarC.{@TCar_DoIt}DoIt;');
  16546. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16547. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16548. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16549. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16550. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16551. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16552. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16553. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16554. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16555. ConvertProgram;
  16556. CheckSource('TestClassOf_TypeCast',
  16557. LinesToStr([ // statements
  16558. 'rtl.createClass(this, "TObject", null, function () {',
  16559. ' this.$init = function () {',
  16560. ' };',
  16561. ' this.$final = function () {',
  16562. ' };',
  16563. ' this.DoIt = function () {',
  16564. ' this.DoIt();',
  16565. ' this.DoIt$1();',
  16566. ' };',
  16567. '});',
  16568. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16569. ' this.DoIt$1 = function () {',
  16570. ' this.DoIt();',
  16571. ' this.DoIt$1();',
  16572. ' this.DoIt$2();',
  16573. ' };',
  16574. '});',
  16575. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16576. ' this.DoIt$2 = function () {',
  16577. ' };',
  16578. '});',
  16579. 'this.ObjC = null;',
  16580. 'this.MobileC = null;',
  16581. 'this.CarC = null;',
  16582. '']),
  16583. LinesToStr([ // $mod.$main
  16584. '$mod.ObjC.DoIt();',
  16585. '$mod.MobileC.DoIt$1();',
  16586. '$mod.CarC.DoIt$2();',
  16587. '$mod.ObjC.DoIt();',
  16588. '$mod.ObjC.DoIt$1();',
  16589. '$mod.ObjC.DoIt$2();',
  16590. '$mod.MobileC.DoIt();',
  16591. '$mod.MobileC.DoIt$1();',
  16592. '$mod.MobileC.DoIt$2();',
  16593. '$mod.CarC.DoIt();',
  16594. '$mod.CarC.DoIt$1();',
  16595. '$mod.CarC.DoIt$2();',
  16596. '']));
  16597. end;
  16598. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16599. begin
  16600. StartProgram(false);
  16601. Add('type');
  16602. Add(' TObject = class');
  16603. Add(' function CurNow: longint; ');
  16604. Add(' class function Now: longint; ');
  16605. Add(' end;');
  16606. Add('function TObject.CurNow: longint; begin end;');
  16607. Add('class function TObject.Now: longint; begin end;');
  16608. Add('var');
  16609. Add(' Obj: tobject;');
  16610. Add(' vI: longint;');
  16611. Add('begin');
  16612. Add(' obj.curnow;');
  16613. Add(' vi:=obj.curnow;');
  16614. Add(' tobject.now;');
  16615. Add(' vi:=tobject.now;');
  16616. ConvertProgram;
  16617. CheckSource('TestClassOf_ImplicitFunctionCall',
  16618. LinesToStr([ // statements
  16619. 'rtl.createClass(this, "TObject", null, function () {',
  16620. ' this.$init = function () {',
  16621. ' };',
  16622. ' this.$final = function () {',
  16623. ' };',
  16624. ' this.CurNow = function () {',
  16625. ' var Result = 0;',
  16626. ' return Result;',
  16627. ' };',
  16628. ' this.Now = function () {',
  16629. ' var Result = 0;',
  16630. ' return Result;',
  16631. ' };',
  16632. '});',
  16633. 'this.Obj = null;',
  16634. 'this.vI = 0;',
  16635. '']),
  16636. LinesToStr([ // $mod.$main
  16637. '$mod.Obj.CurNow();',
  16638. '$mod.vI = $mod.Obj.CurNow();',
  16639. '$mod.TObject.Now();',
  16640. '$mod.vI = $mod.TObject.Now();',
  16641. '']));
  16642. end;
  16643. procedure TTestModule.TestClassOf_Const;
  16644. begin
  16645. StartProgram(false);
  16646. Add([
  16647. 'type',
  16648. ' TObject = class',
  16649. ' end;',
  16650. ' TBird = TObject;',
  16651. ' TBirds = class of TBird;',
  16652. ' TEagles = TBirds;',
  16653. ' THawk = class(TBird);',
  16654. 'const',
  16655. ' Hawk: TEagles = THawk;',
  16656. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16657. ' TBird,',
  16658. ' THawk',
  16659. ' );',
  16660. 'begin']);
  16661. ConvertProgram;
  16662. CheckSource('TestClassOf_Const',
  16663. LinesToStr([ // statements
  16664. 'rtl.createClass(this, "TObject", null, function () {',
  16665. ' this.$init = function () {',
  16666. ' };',
  16667. ' this.$final = function () {',
  16668. ' };',
  16669. '});',
  16670. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16671. '});',
  16672. 'this.Hawk = this.THawk;',
  16673. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16674. '']),
  16675. LinesToStr([ // $mod.$main
  16676. '']));
  16677. end;
  16678. procedure TTestModule.TestNestedClass_Alias;
  16679. begin
  16680. WithTypeInfo:=true;
  16681. StartProgram(false);
  16682. Add([
  16683. 'type',
  16684. ' TObject = class',
  16685. ' type TNested = type longint;',
  16686. ' end;',
  16687. 'type TAlias = type tobject.tnested;',
  16688. 'var i: tobject.tnested = 3;',
  16689. 'var j: TAlias = 4;',
  16690. 'begin',
  16691. ' if typeinfo(TAlias)=nil then ;',
  16692. ' if typeinfo(tobject.tnested)=nil then ;',
  16693. '']);
  16694. ConvertProgram;
  16695. CheckSource('TestNestedClass_Alias',
  16696. LinesToStr([ // statements
  16697. 'rtl.createClass(this, "TObject", null, function () {',
  16698. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16699. ' this.$init = function () {',
  16700. ' };',
  16701. ' this.$final = function () {',
  16702. ' };',
  16703. '});',
  16704. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16705. 'this.i = 3;',
  16706. 'this.j = 4;',
  16707. '']),
  16708. LinesToStr([ // $mod.$main
  16709. 'if ($mod.$rtti["TAlias"] === null) ;',
  16710. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16711. '']));
  16712. end;
  16713. procedure TTestModule.TestNestedClass_Record;
  16714. begin
  16715. WithTypeInfo:=true;
  16716. StartProgram(false);
  16717. Add([
  16718. 'type',
  16719. ' TObject = class',
  16720. ' type TPoint = record',
  16721. ' x,y: byte;',
  16722. ' end;',
  16723. ' procedure DoIt(t: TPoint);',
  16724. ' end;',
  16725. 'procedure tobject.DoIt(t: TPoint);',
  16726. 'var p: TPoint;',
  16727. 'begin',
  16728. ' t.x:=t.y;',
  16729. ' p:=t;',
  16730. 'end;',
  16731. 'var',
  16732. ' p: tobject.tpoint = (x:2; y:4);',
  16733. ' o: TObject;',
  16734. 'begin',
  16735. ' p:=p;',
  16736. ' o.doit(p);',
  16737. '']);
  16738. ConvertProgram;
  16739. CheckSource('TestNestedClass_Record',
  16740. LinesToStr([ // statements
  16741. 'rtl.createClass(this, "TObject", null, function () {',
  16742. ' rtl.recNewT(this, "TPoint", function () {',
  16743. ' this.x = 0;',
  16744. ' this.y = 0;',
  16745. ' this.$eq = function (b) {',
  16746. ' return (this.x === b.x) && (this.y === b.y);',
  16747. ' };',
  16748. ' this.$assign = function (s) {',
  16749. ' this.x = s.x;',
  16750. ' this.y = s.y;',
  16751. ' return this;',
  16752. ' };',
  16753. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16754. ' $r.addField("x", rtl.byte);',
  16755. ' $r.addField("y", rtl.byte);',
  16756. ' });',
  16757. ' this.$init = function () {',
  16758. ' };',
  16759. ' this.$final = function () {',
  16760. ' };',
  16761. ' this.DoIt = function (t) {',
  16762. ' var p = this.TPoint.$new();',
  16763. ' t.x = t.y;',
  16764. ' p.$assign(t);',
  16765. ' };',
  16766. '});',
  16767. 'this.p = this.TObject.TPoint.$clone({',
  16768. ' x: 2,',
  16769. ' y: 4',
  16770. '});',
  16771. 'this.o = null;',
  16772. '']),
  16773. LinesToStr([ // $mod.$main
  16774. '$mod.p.$assign($mod.p);',
  16775. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16776. '']));
  16777. end;
  16778. procedure TTestModule.TestNestedClass_Class;
  16779. begin
  16780. WithTypeInfo:=true;
  16781. StartProgram(false);
  16782. Add([
  16783. 'type',
  16784. ' TObject = class end;',
  16785. ' TBird = class',
  16786. ' type TLeg = class',
  16787. ' FId: longint;',
  16788. ' constructor Create;',
  16789. ' function Create(i: longint): TLeg;',
  16790. ' end;',
  16791. ' function DoIt(b: TBird): Tleg;',
  16792. ' end;',
  16793. 'constructor tbird.tleg.create;',
  16794. 'begin',
  16795. ' FId:=3;',
  16796. 'end;',
  16797. 'function tbird.tleg.Create(i: longint): TLeg;',
  16798. 'begin',
  16799. ' Create;',
  16800. ' Result:=TLeg.Create;',
  16801. ' Result:=TBird.TLeg.Create;',
  16802. ' Result:=Create(3);',
  16803. ' FId:=i;',
  16804. 'end;',
  16805. 'function tbird.DoIt(b: tbird): tleg;',
  16806. 'begin',
  16807. ' Result.Create;',
  16808. ' Result:=TLeg.Create;',
  16809. ' Result:=TBird.TLeg.Create;',
  16810. ' Result:=Result.Create(3);',
  16811. 'end;',
  16812. 'var',
  16813. ' b: Tbird.tleg;',
  16814. 'begin',
  16815. ' b.Create;',
  16816. ' b:=TBird.TLeg.Create;',
  16817. ' b:=b.Create(3);',
  16818. '']);
  16819. ConvertProgram;
  16820. CheckSource('TestNestedClass_Class',
  16821. LinesToStr([ // statements
  16822. 'rtl.createClass(this, "TObject", null, function () {',
  16823. ' this.$init = function () {',
  16824. ' };',
  16825. ' this.$final = function () {',
  16826. ' };',
  16827. '});',
  16828. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16829. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16830. ' this.$init = function () {',
  16831. ' $mod.TObject.$init.call(this);',
  16832. ' this.FId = 0;',
  16833. ' };',
  16834. ' this.Create = function () {',
  16835. ' this.FId = 3;',
  16836. ' return this;',
  16837. ' };',
  16838. ' this.Create$1 = function (i) {',
  16839. ' var Result = null;',
  16840. ' this.Create();',
  16841. ' Result = $mod.TBird.TLeg.$create("Create");',
  16842. ' Result = $mod.TBird.TLeg.$create("Create");',
  16843. ' Result = this.Create$1(3);',
  16844. ' this.FId = i;',
  16845. ' return Result;',
  16846. ' };',
  16847. ' }, "TBird.TLeg");',
  16848. ' this.DoIt = function (b) {',
  16849. ' var Result = null;',
  16850. ' Result.Create();',
  16851. ' Result = this.TLeg.$create("Create");',
  16852. ' Result = $mod.TBird.TLeg.$create("Create");',
  16853. ' Result = Result.Create$1(3);',
  16854. ' return Result;',
  16855. ' };',
  16856. '});',
  16857. 'this.b = null;',
  16858. '']),
  16859. LinesToStr([ // $mod.$main
  16860. '$mod.b.Create();',
  16861. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16862. '$mod.b = $mod.b.Create$1(3);',
  16863. '']));
  16864. end;
  16865. procedure TTestModule.TestExternalClass_Var;
  16866. begin
  16867. StartProgram(false);
  16868. Add([
  16869. '{$modeswitch externalclass}',
  16870. 'type',
  16871. ' TExtA = class external name ''ExtObj''',
  16872. ' Id: longint external name ''$Id'';',
  16873. ' B: longint;',
  16874. ' end;',
  16875. 'var Obj: TExtA;',
  16876. 'begin',
  16877. ' obj.id:=obj.id+1;',
  16878. ' obj.B:=obj.B+1;']);
  16879. ConvertProgram;
  16880. CheckSource('TestExternalClass_Var',
  16881. LinesToStr([ // statements
  16882. 'this.Obj = null;',
  16883. '']),
  16884. LinesToStr([ // $mod.$main
  16885. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16886. '$mod.Obj.B = $mod.Obj.B + 1;',
  16887. '']));
  16888. end;
  16889. procedure TTestModule.TestExternalClass_Const;
  16890. begin
  16891. StartProgram(false);
  16892. Add([
  16893. '{$modeswitch externalclass}',
  16894. 'type',
  16895. ' TExtA = class external name ''ExtObj''',
  16896. ' const Two: longint = 2;',
  16897. ' const Three = 3;',
  16898. ' const Id: longint;',
  16899. ' end;',
  16900. ' TExtB = class external name ''ExtB''',
  16901. ' A: TExtA;',
  16902. ' end;',
  16903. 'var',
  16904. ' A: texta;',
  16905. ' B: textb;',
  16906. ' i: longint;',
  16907. 'begin',
  16908. ' i:=a.two;',
  16909. ' i:=texta.two;',
  16910. ' i:=a.three;',
  16911. ' i:=texta.three;',
  16912. ' i:=a.id;',
  16913. ' i:=texta.id;',
  16914. '']);
  16915. ConvertProgram;
  16916. CheckSource('TestExternalClass_Const',
  16917. LinesToStr([ // statements
  16918. 'this.A = null;',
  16919. 'this.B = null;',
  16920. 'this.i = 0;',
  16921. '']),
  16922. LinesToStr([ // $mod.$main
  16923. '$mod.i = 2;',
  16924. '$mod.i = 2;',
  16925. '$mod.i = 3;',
  16926. '$mod.i = 3;',
  16927. '$mod.i = $mod.A.Id;',
  16928. '$mod.i = ExtObj.Id;',
  16929. '']));
  16930. end;
  16931. procedure TTestModule.TestExternalClass_Dollar;
  16932. begin
  16933. StartProgram(false);
  16934. Add([
  16935. '{$modeswitch externalclass}',
  16936. 'type',
  16937. ' TExtA = class external name ''$''',
  16938. ' Id: longint external name ''$'';',
  16939. ' function Bla(i: longint): longint; external name ''$'';',
  16940. ' end;',
  16941. 'function dollar(k: longint): longint; external name ''$'';',
  16942. 'var Obj: TExtA;',
  16943. 'begin',
  16944. ' dollar(1);',
  16945. ' obj.id:=obj.id+2;',
  16946. ' obj.Bla(3);',
  16947. '']);
  16948. ConvertProgram;
  16949. CheckSource('TestExternalClass_Dollar',
  16950. LinesToStr([ // statements
  16951. 'this.Obj = null;',
  16952. '']),
  16953. LinesToStr([ // $mod.$main
  16954. '$(1);',
  16955. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16956. '$mod.Obj.$(3);',
  16957. '']));
  16958. end;
  16959. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16960. begin
  16961. StartProgram(false);
  16962. Add('{$modeswitch externalclass}');
  16963. Add('type');
  16964. Add(' TExtA = class external name ''ExtA''');
  16965. Add(' Id: longint external name ''$Id'';');
  16966. Add(' end;');
  16967. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16968. Add(' Id: longint;');
  16969. Add(' end;');
  16970. Add('begin');
  16971. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16972. ConvertProgram;
  16973. end;
  16974. procedure TTestModule.TestExternalClass_Method;
  16975. begin
  16976. StartProgram(false);
  16977. Add(['{$modeswitch externalclass}',
  16978. 'type',
  16979. ' TExtA = class external name ''ExtObj''',
  16980. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16981. ' procedure DoSome(Id: longint = 1);',
  16982. ' end;',
  16983. 'var Obj: texta;',
  16984. 'begin',
  16985. ' obj.doit;',
  16986. ' obj.doit();',
  16987. ' obj.doit(2);',
  16988. ' with obj do begin',
  16989. ' doit;',
  16990. ' doit();',
  16991. ' doit(3);',
  16992. ' end;']);
  16993. ConvertProgram;
  16994. CheckSource('TestExternalClass_Method',
  16995. LinesToStr([ // statements
  16996. 'this.Obj = null;',
  16997. '']),
  16998. LinesToStr([ // $mod.$main
  16999. '$mod.Obj.$Execute(1);',
  17000. '$mod.Obj.$Execute(1);',
  17001. '$mod.Obj.$Execute(2);',
  17002. 'var $with = $mod.Obj;',
  17003. '$with.$Execute(1);',
  17004. '$with.$Execute(1);',
  17005. '$with.$Execute(3);',
  17006. '']));
  17007. end;
  17008. procedure TTestModule.TestExternalClass_ClassMethod;
  17009. begin
  17010. StartProgram(false);
  17011. Add([
  17012. '{$modeswitch externalclass}',
  17013. 'type',
  17014. ' TExtA = class external name ''ExtObj''',
  17015. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17016. ' end;',
  17017. ' TExtB = TExtA;',
  17018. 'var p: Pointer;',
  17019. 'begin',
  17020. ' texta.doit;',
  17021. ' texta.doit();',
  17022. ' texta.doit(2);',
  17023. ' p:[email protected];',
  17024. ' with texta do begin',
  17025. ' doit;',
  17026. ' doit();',
  17027. ' doit(3);',
  17028. ' p:=@DoIt;',
  17029. ' end;',
  17030. ' textb.doit;',
  17031. ' textb.doit();',
  17032. ' textb.doit(4);',
  17033. ' with textb do begin',
  17034. ' doit;',
  17035. ' doit();',
  17036. ' doit(5);',
  17037. ' end;',
  17038. '']);
  17039. ConvertProgram;
  17040. CheckSource('TestExternalClass_ClassMethod',
  17041. LinesToStr([ // statements
  17042. 'this.p = null;',
  17043. '']),
  17044. LinesToStr([ // $mod.$main
  17045. 'ExtObj.$Execute(1);',
  17046. 'ExtObj.$Execute(1);',
  17047. 'ExtObj.$Execute(2);',
  17048. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17049. 'ExtObj.$Execute(1);',
  17050. 'ExtObj.$Execute(1);',
  17051. 'ExtObj.$Execute(3);',
  17052. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17053. 'ExtObj.$Execute(1);',
  17054. 'ExtObj.$Execute(1);',
  17055. 'ExtObj.$Execute(4);',
  17056. 'ExtObj.$Execute(1);',
  17057. 'ExtObj.$Execute(1);',
  17058. 'ExtObj.$Execute(5);',
  17059. '']));
  17060. end;
  17061. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  17062. begin
  17063. StartProgram(false);
  17064. Add([
  17065. '{$modeswitch externalclass}',
  17066. 'type',
  17067. ' TExtA = class external name ''ExtObj''',
  17068. ' class procedure DoIt(Id: longint = 1); static;',
  17069. ' end;',
  17070. 'var p: Pointer;',
  17071. 'begin',
  17072. ' texta.doit;',
  17073. ' texta.doit();',
  17074. ' texta.doit(2);',
  17075. ' p:[email protected];',
  17076. ' with texta do begin',
  17077. ' doit;',
  17078. ' doit();',
  17079. ' doit(3);',
  17080. ' p:=@DoIt;',
  17081. ' end;',
  17082. '']);
  17083. ConvertProgram;
  17084. CheckSource('TestExternalClass_ClassMethodStatic',
  17085. LinesToStr([ // statements
  17086. 'this.p = null;',
  17087. '']),
  17088. LinesToStr([ // $mod.$main
  17089. 'ExtObj.DoIt(1);',
  17090. 'ExtObj.DoIt(1);',
  17091. 'ExtObj.DoIt(2);',
  17092. '$mod.p = ExtObj.DoIt;',
  17093. 'ExtObj.DoIt(1);',
  17094. 'ExtObj.DoIt(1);',
  17095. 'ExtObj.DoIt(3);',
  17096. '$mod.p = ExtObj.DoIt;',
  17097. '']));
  17098. end;
  17099. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  17100. begin
  17101. StartProgram(false);
  17102. Add([
  17103. '{$modeswitch externalclass}',
  17104. 'type',
  17105. ' TBird = class external name ''Array''',
  17106. ' end;',
  17107. 'function GetPtr: Pointer;',
  17108. 'begin',
  17109. 'end;',
  17110. 'procedure Write(const p);',
  17111. 'begin',
  17112. 'end;',
  17113. 'procedure WriteLn; varargs;',
  17114. 'begin',
  17115. 'end;',
  17116. 'begin',
  17117. ' if TBird(GetPtr)=nil then ;',
  17118. ' Write(GetPtr);',
  17119. ' WriteLn(GetPtr);',
  17120. ' Write(TBird(GetPtr));',
  17121. ' WriteLn(TBird(GetPtr));',
  17122. '']);
  17123. ConvertProgram;
  17124. CheckSource('TestFunctionResultInTypeCast',
  17125. LinesToStr([ // statements
  17126. 'this.GetPtr = function () {',
  17127. ' var Result = null;',
  17128. ' return Result;',
  17129. '};',
  17130. 'this.Write = function (p) {',
  17131. '};',
  17132. 'this.WriteLn = function () {',
  17133. '};',
  17134. '']),
  17135. LinesToStr([
  17136. 'if ($mod.GetPtr() === null) ;',
  17137. '$mod.Write($mod.GetPtr());',
  17138. '$mod.WriteLn($mod.GetPtr());',
  17139. '$mod.Write($mod.GetPtr());',
  17140. '$mod.WriteLn($mod.GetPtr());',
  17141. '']));
  17142. end;
  17143. procedure TTestModule.TestExternalClass_NonExternalOverride;
  17144. begin
  17145. StartProgram(false);
  17146. Add([
  17147. '{$modeswitch externalclass}',
  17148. 'type',
  17149. ' TExtA = class external name ''ExtObjA''',
  17150. ' procedure ProcA; virtual;',
  17151. ' procedure ProcB; virtual;',
  17152. ' end;',
  17153. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  17154. ' end;',
  17155. ' TExtC = class (TExtB)',
  17156. ' procedure ProcA; override;',
  17157. ' end;',
  17158. 'procedure TExtC.ProcA;',
  17159. 'begin',
  17160. ' ProcA;',
  17161. ' Self.ProcA;',
  17162. ' ProcB;',
  17163. ' Self.ProcB;',
  17164. 'end;',
  17165. 'var',
  17166. ' A: texta;',
  17167. ' B: textb;',
  17168. ' C: textc;',
  17169. 'begin',
  17170. ' a.proca;',
  17171. ' b.proca;',
  17172. ' c.proca;']);
  17173. ConvertProgram;
  17174. CheckSource('TestExternalClass_NonExternalOverride',
  17175. LinesToStr([ // statements
  17176. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  17177. ' this.$init = function () {',
  17178. ' };',
  17179. ' this.$final = function () {',
  17180. ' };',
  17181. ' this.ProcA = function () {',
  17182. ' this.ProcA();',
  17183. ' this.ProcA();',
  17184. ' this.ProcB();',
  17185. ' this.ProcB();',
  17186. ' };',
  17187. '});',
  17188. 'this.A = null;',
  17189. 'this.B = null;',
  17190. 'this.C = null;',
  17191. '']),
  17192. LinesToStr([ // $mod.$main
  17193. '$mod.A.ProcA();',
  17194. '$mod.B.ProcA();',
  17195. '$mod.C.ProcA();',
  17196. '']));
  17197. end;
  17198. procedure TTestModule.TestExternalClass_OverloadHint;
  17199. begin
  17200. StartProgram(false);
  17201. Add([
  17202. '{$modeswitch externalclass}',
  17203. 'type',
  17204. ' TExtA = class external name ''ExtObjA''',
  17205. ' procedure DoIt;',
  17206. ' procedure DoIt(i: longint);',
  17207. ' end;',
  17208. 'begin',
  17209. '']);
  17210. ConvertProgram;
  17211. CheckResolverUnexpectedHints(true);
  17212. CheckSource('TestExternalClass_OverloadHint',
  17213. LinesToStr([ // statements
  17214. '']),
  17215. LinesToStr([ // $mod.$main
  17216. '']));
  17217. end;
  17218. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  17219. begin
  17220. WithTypeInfo:=true;
  17221. StartProgram(false);
  17222. Add([
  17223. '{$modeswitch externalclass}',
  17224. 'type',
  17225. ' JSwiper = class external name ''Swiper''',
  17226. ' constructor New;',
  17227. ' end;',
  17228. ' TObject = class',
  17229. ' private',
  17230. ' FSwiper: JSwiper;',
  17231. ' published',
  17232. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  17233. ' end;',
  17234. 'begin',
  17235. ' JSwiper.new;',
  17236. '']);
  17237. ConvertProgram;
  17238. CheckSource('TestExternalClass_SameNamePublishedProperty',
  17239. LinesToStr([ // statements
  17240. 'this.$rtti.$ExtClass("JSwiper", {',
  17241. ' jsclass: "Swiper"',
  17242. '});',
  17243. 'rtl.createClass(this, "TObject", null, function () {',
  17244. ' this.$init = function () {',
  17245. ' this.FSwiper = null;',
  17246. ' };',
  17247. ' this.$final = function () {',
  17248. ' this.FSwiper = undefined;',
  17249. ' };',
  17250. ' var $r = this.$rtti;',
  17251. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  17252. '});',
  17253. '']),
  17254. LinesToStr([ // $mod.$main
  17255. 'new Swiper();',
  17256. '']));
  17257. end;
  17258. procedure TTestModule.TestExternalClass_Property;
  17259. begin
  17260. StartProgram(false);
  17261. Add([
  17262. '{$modeswitch externalclass}',
  17263. 'type',
  17264. ' TExtA = class external name ''ExtA''',
  17265. ' function getYear: longint;',
  17266. ' procedure setYear(Value: longint);',
  17267. ' property Year: longint read getyear write setyear;',
  17268. ' end;',
  17269. ' TExtB = class (TExtA)',
  17270. ' procedure OtherSetYear(Value: longint);',
  17271. ' property year write othersetyear;',
  17272. ' end;',
  17273. 'procedure textb.othersetyear(value: longint);',
  17274. 'begin',
  17275. ' setYear(Value+4);',
  17276. 'end;',
  17277. 'var',
  17278. ' A: texta;',
  17279. ' B: textb;',
  17280. 'begin',
  17281. ' a.year:=a.year+1;',
  17282. ' b.year:=b.year+2;']);
  17283. ConvertProgram;
  17284. CheckSource('TestExternalClass_NonExternalOverride',
  17285. LinesToStr([ // statements
  17286. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17287. ' this.$init = function () {',
  17288. ' };',
  17289. ' this.$final = function () {',
  17290. ' };',
  17291. ' this.OtherSetYear = function (Value) {',
  17292. ' this.setYear(Value+4);',
  17293. ' };',
  17294. '});',
  17295. 'this.A = null;',
  17296. 'this.B = null;',
  17297. '']),
  17298. LinesToStr([ // $mod.$main
  17299. '$mod.A.setYear($mod.A.getYear()+1);',
  17300. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  17301. '']));
  17302. end;
  17303. procedure TTestModule.TestExternalClass_PropertyDate;
  17304. begin
  17305. StartProgram(false);
  17306. Add([
  17307. '{$modeswitch externalclass}',
  17308. 'type',
  17309. ' TExtA = class external name ''ExtA''',
  17310. ' end;',
  17311. ' TExtB = class (TExtA)',
  17312. ' FDate: string;',
  17313. ' property Date: string read FDate write FDate;',
  17314. ' property ExtA: string read FDate write FDate;',
  17315. ' end;',
  17316. ' {$M+}',
  17317. ' TObject = class',
  17318. ' FDate: string;',
  17319. ' published',
  17320. ' property Date: string read FDate write FDate;',
  17321. ' property ExtA: string read FDate write FDate;',
  17322. ' end;',
  17323. 'var',
  17324. ' B: textb;',
  17325. ' o: TObject;',
  17326. 'begin',
  17327. ' b.date:=b.exta;',
  17328. ' o.date:=o.exta;']);
  17329. ConvertProgram;
  17330. CheckSource('TestExternalClass_PropertyDate',
  17331. LinesToStr([ // statements
  17332. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17333. ' this.$init = function () {',
  17334. ' this.FDate = "";',
  17335. ' };',
  17336. ' this.$final = function () {',
  17337. ' };',
  17338. '});',
  17339. 'rtl.createClass(this, "TObject", null, function () {',
  17340. ' this.$init = function () {',
  17341. ' this.FDate = "";',
  17342. ' };',
  17343. ' this.$final = function () {',
  17344. ' };',
  17345. ' var $r = this.$rtti;',
  17346. ' $r.addField("FDate", rtl.string);',
  17347. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  17348. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  17349. '});',
  17350. 'this.B = null;',
  17351. 'this.o = null;',
  17352. '']),
  17353. LinesToStr([ // $mod.$main
  17354. '$mod.B.FDate = $mod.B.FDate;',
  17355. '$mod.o.FDate = $mod.o.FDate;',
  17356. '']));
  17357. end;
  17358. procedure TTestModule.TestExternalClass_ClassProperty;
  17359. begin
  17360. StartProgram(false);
  17361. Add('{$modeswitch externalclass}');
  17362. Add('type');
  17363. Add(' TExtA = class external name ''ExtA''');
  17364. Add(' class function getYear: longint;');
  17365. Add(' class procedure setYear(Value: longint);');
  17366. Add(' class property Year: longint read getyear write setyear;');
  17367. Add(' end;');
  17368. Add(' TExtB = class (TExtA)');
  17369. Add(' class function GetCentury: longint;');
  17370. Add(' class procedure SetCentury(Value: longint);');
  17371. Add(' class property Century: longint read getcentury write setcentury;');
  17372. Add(' end;');
  17373. Add('class function textb.getcentury: longint;');
  17374. Add('begin');
  17375. Add('end;');
  17376. Add('class procedure textb.setcentury(value: longint);');
  17377. Add('begin');
  17378. Add(' setyear(value+11);');
  17379. Add(' texta.year:=texta.year+12;');
  17380. Add(' year:=year+13;');
  17381. Add(' textb.century:=textb.century+14;');
  17382. Add(' century:=century+15;');
  17383. Add('end;');
  17384. Add('var');
  17385. Add(' A: texta;');
  17386. Add(' B: textb;');
  17387. Add('begin');
  17388. Add(' texta.year:=texta.year+1;');
  17389. Add(' textb.year:=textb.year+2;');
  17390. Add(' TextA.year:=TextA.year+3;');
  17391. Add(' b.year:=b.year+4;');
  17392. Add(' textb.century:=textb.century+5;');
  17393. Add(' b.century:=b.century+6;');
  17394. ConvertProgram;
  17395. CheckSource('TestExternalClass_ClassProperty',
  17396. LinesToStr([ // statements
  17397. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17398. ' this.$init = function () {',
  17399. ' };',
  17400. ' this.$final = function () {',
  17401. ' };',
  17402. ' this.GetCentury = function () {',
  17403. ' var Result = 0;',
  17404. ' return Result;',
  17405. ' };',
  17406. ' this.SetCentury = function (Value) {',
  17407. ' this.setYear(Value + 11);',
  17408. ' ExtA.setYear(ExtA.getYear() + 12);',
  17409. ' this.setYear(this.getYear() + 13);',
  17410. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17411. ' this.SetCentury(this.GetCentury() + 15);',
  17412. ' };',
  17413. '});',
  17414. 'this.A = null;',
  17415. 'this.B = null;',
  17416. '']),
  17417. LinesToStr([ // $mod.$main
  17418. 'ExtA.setYear(ExtA.getYear() + 1);',
  17419. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17420. 'ExtA.setYear(ExtA.getYear() + 3);',
  17421. '$mod.B.setYear($mod.B.getYear() + 4);',
  17422. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17423. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17424. '']));
  17425. end;
  17426. procedure TTestModule.TestExternalClass_ClassOf;
  17427. begin
  17428. StartProgram(false);
  17429. Add('{$modeswitch externalclass}');
  17430. Add('type');
  17431. Add(' TExtA = class external name ''ExtA''');
  17432. Add(' procedure ProcA; virtual;');
  17433. Add(' procedure ProcB; virtual;');
  17434. Add(' end;');
  17435. Add(' TExtAClass = class of TExtA;');
  17436. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17437. Add(' end;');
  17438. Add(' TExtBClass = class of TExtB;');
  17439. Add(' TExtC = class (TExtB)');
  17440. Add(' procedure ProcA; override;');
  17441. Add(' end;');
  17442. Add(' TExtCClass = class of TExtC;');
  17443. Add('procedure TExtC.ProcA; begin end;');
  17444. Add('var');
  17445. Add(' A: texta; ClA: TExtAClass;');
  17446. Add(' B: textb; ClB: TExtBClass;');
  17447. Add(' C: textc; ClC: TExtCClass;');
  17448. Add('begin');
  17449. Add(' ClA:=texta;');
  17450. Add(' ClA:=textb;');
  17451. Add(' ClA:=textc;');
  17452. Add(' ClB:=textb;');
  17453. Add(' ClB:=textc;');
  17454. Add(' ClC:=textc;');
  17455. ConvertProgram;
  17456. CheckSource('TestExternalClass_ClassOf',
  17457. LinesToStr([ // statements
  17458. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17459. ' this.$init = function () {',
  17460. ' };',
  17461. ' this.$final = function () {',
  17462. ' };',
  17463. ' this.ProcA = function () {',
  17464. ' };',
  17465. '});',
  17466. 'this.A = null;',
  17467. 'this.ClA = null;',
  17468. 'this.B = null;',
  17469. 'this.ClB = null;',
  17470. 'this.C = null;',
  17471. 'this.ClC = null;',
  17472. '']),
  17473. LinesToStr([ // $mod.$main
  17474. '$mod.ClA = ExtA;',
  17475. '$mod.ClA = ExtB;',
  17476. '$mod.ClA = $mod.TExtC;',
  17477. '$mod.ClB = ExtB;',
  17478. '$mod.ClB = $mod.TExtC;',
  17479. '$mod.ClC = $mod.TExtC;',
  17480. '']));
  17481. end;
  17482. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17483. begin
  17484. AddModuleWithIntfImplSrc('unit2.pas',
  17485. LinesToStr([
  17486. '{$modeswitch externalclass}',
  17487. 'type',
  17488. ' TExtA = class external name ''ExtA''',
  17489. ' class var Id: longint;',
  17490. ' end;',
  17491. '']),
  17492. '');
  17493. StartUnit(true);
  17494. Add('interface');
  17495. Add('uses unit2;');
  17496. Add('implementation');
  17497. Add('begin');
  17498. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17499. ConvertUnit;
  17500. CheckSource('TestExternalClass_ClassOtherUnit',
  17501. LinesToStr([
  17502. '']),
  17503. LinesToStr([
  17504. 'ExtA.Id = ExtA.Id + 1;',
  17505. '']));
  17506. end;
  17507. procedure TTestModule.TestExternalClass_Is;
  17508. begin
  17509. StartProgram(false);
  17510. Add([
  17511. '{$modeswitch externalclass}',
  17512. 'type',
  17513. ' TExtA = class external name ''ExtA''',
  17514. ' end;',
  17515. ' TExtAClass = class of TExtA;',
  17516. ' TExtB = class external name ''ExtB'' (TExtA)',
  17517. ' end;',
  17518. ' TExtBClass = class of TExtB;',
  17519. ' TExtC = class (TExtB)',
  17520. ' end;',
  17521. ' TExtCClass = class of TExtC;',
  17522. 'var',
  17523. ' A: texta; ClA: TExtAClass;',
  17524. ' B: textb; ClB: TExtBClass;',
  17525. ' C: textc; ClC: TExtCClass;',
  17526. 'begin',
  17527. ' if a is textb then ;',
  17528. ' if a is textc then ;',
  17529. ' if b is textc then ;',
  17530. ' if cla is textb then ;',
  17531. ' if cla is textc then ;',
  17532. ' if clb is textc then ;',
  17533. ' try',
  17534. ' except',
  17535. ' on TExtA do ;',
  17536. ' on e: TExtB do ;',
  17537. ' end;',
  17538. '']);
  17539. ConvertProgram;
  17540. CheckSource('TestExternalClass_Is',
  17541. LinesToStr([ // statements
  17542. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17543. ' this.$init = function () {',
  17544. ' };',
  17545. ' this.$final = function () {',
  17546. ' };',
  17547. '});',
  17548. 'this.A = null;',
  17549. 'this.ClA = null;',
  17550. 'this.B = null;',
  17551. 'this.ClB = null;',
  17552. 'this.C = null;',
  17553. 'this.ClC = null;',
  17554. '']),
  17555. LinesToStr([ // $mod.$main
  17556. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17557. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17558. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17559. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17560. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17561. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17562. 'try {} catch ($e) {',
  17563. ' if (rtl.isExt($e,ExtA)) {}',
  17564. ' else if (rtl.isExt($e,ExtB)) {',
  17565. ' var e = $e;',
  17566. ' } else throw $e',
  17567. '};',
  17568. '']));
  17569. end;
  17570. procedure TTestModule.TestExternalClass_As;
  17571. begin
  17572. StartProgram(false);
  17573. Add('{$modeswitch externalclass}');
  17574. Add('type');
  17575. Add(' TExtA = class external name ''ExtA''');
  17576. Add(' end;');
  17577. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17578. Add(' end;');
  17579. Add(' TExtC = class (TExtB)');
  17580. Add(' end;');
  17581. Add('var');
  17582. Add(' A: texta;');
  17583. Add(' B: textb;');
  17584. Add(' C: textc;');
  17585. Add('begin');
  17586. Add(' b:=a as textb;');
  17587. Add(' c:=a as textc;');
  17588. Add(' c:=b as textc;');
  17589. ConvertProgram;
  17590. CheckSource('TestExternalClass_Is',
  17591. LinesToStr([ // statements
  17592. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17593. ' this.$init = function () {',
  17594. ' };',
  17595. ' this.$final = function () {',
  17596. ' };',
  17597. '});',
  17598. 'this.A = null;',
  17599. 'this.B = null;',
  17600. 'this.C = null;',
  17601. '']),
  17602. LinesToStr([ // $mod.$main
  17603. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17604. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17605. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17606. '']));
  17607. end;
  17608. procedure TTestModule.TestExternalClass_DestructorFail;
  17609. begin
  17610. StartProgram(false);
  17611. Add('{$modeswitch externalclass}');
  17612. Add('type');
  17613. Add(' TExtA = class external name ''ExtA''');
  17614. Add(' destructor Free;');
  17615. Add(' end;');
  17616. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17617. nPasElementNotSupported);
  17618. ConvertProgram;
  17619. end;
  17620. procedure TTestModule.TestExternalClass_New;
  17621. begin
  17622. StartProgram(false);
  17623. Add([
  17624. '{$modeswitch externalclass}',
  17625. 'type',
  17626. ' TExtA = class external name ''ExtA''',
  17627. ' constructor New;',
  17628. ' constructor New(i: longint; j: longint = 2);',
  17629. ' end;',
  17630. 'var',
  17631. ' A: texta;',
  17632. 'begin',
  17633. ' a:=texta.new;',
  17634. ' a:=texta(texta.new);',
  17635. ' a:=texta.new();',
  17636. ' a:=texta.new(1);',
  17637. ' with texta do begin',
  17638. ' a:=new;',
  17639. ' a:=new();',
  17640. ' a:=new(2);',
  17641. ' end;',
  17642. ' a:=test1.texta.new;',
  17643. ' a:=test1.texta.new();',
  17644. ' a:=test1.texta.new(3);',
  17645. '']);
  17646. ConvertProgram;
  17647. CheckSource('TestExternalClass_New',
  17648. LinesToStr([ // statements
  17649. 'this.A = null;',
  17650. '']),
  17651. LinesToStr([ // $mod.$main
  17652. '$mod.A = new ExtA();',
  17653. '$mod.A = new ExtA();',
  17654. '$mod.A = new ExtA();',
  17655. '$mod.A = new ExtA(1,2);',
  17656. '$mod.A = new ExtA();',
  17657. '$mod.A = new ExtA();',
  17658. '$mod.A = new ExtA(2,2);',
  17659. '$mod.A = new ExtA();',
  17660. '$mod.A = new ExtA();',
  17661. '$mod.A = new ExtA(3,2);',
  17662. '']));
  17663. end;
  17664. procedure TTestModule.TestExternalClass_ClassOf_New;
  17665. begin
  17666. StartProgram(false);
  17667. Add('{$modeswitch externalclass}');
  17668. Add('type');
  17669. Add(' TExtAClass = class of TExtA;');
  17670. Add(' TExtA = class external name ''ExtA''');
  17671. Add(' C: TExtAClass;');
  17672. Add(' constructor New;');
  17673. Add(' end;');
  17674. Add('var');
  17675. Add(' A: texta;');
  17676. Add(' C: textaclass;');
  17677. Add('begin');
  17678. Add(' a:=c.new;');
  17679. Add(' a:=c.new();');
  17680. Add(' with C do begin');
  17681. Add(' a:=new;');
  17682. Add(' a:=new();');
  17683. Add(' end;');
  17684. Add(' a:=test1.c.new;');
  17685. Add(' a:=test1.c.new();');
  17686. Add(' a:=A.c.new();');
  17687. ConvertProgram;
  17688. CheckSource('TestExternalClass_ClassOf_New',
  17689. LinesToStr([ // statements
  17690. 'this.A = null;',
  17691. 'this.C = null;',
  17692. '']),
  17693. LinesToStr([ // $mod.$main
  17694. '$mod.A = new $mod.C();',
  17695. '$mod.A = new $mod.C();',
  17696. 'var $with = $mod.C;',
  17697. '$mod.A = new $with();',
  17698. '$mod.A = new $with();',
  17699. '$mod.A = new $mod.C();',
  17700. '$mod.A = new $mod.C();',
  17701. '$mod.A = new $mod.A.C();',
  17702. '']));
  17703. end;
  17704. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17705. begin
  17706. StartProgram(false);
  17707. Add([
  17708. '{$modeswitch externalclass}',
  17709. 'type',
  17710. ' TExtAClass = class of TExtA;',
  17711. ' TExtA = class external name ''ExtA''',
  17712. ' constructor New;',
  17713. ' end;',
  17714. 'function GetCreator: TExtAClass;',
  17715. 'begin',
  17716. ' Result:=TExtA;',
  17717. 'end;',
  17718. 'var',
  17719. ' A: texta;',
  17720. 'begin',
  17721. ' a:=getcreator.new;',
  17722. ' a:=getcreator().new;',
  17723. ' a:=getcreator().new();',
  17724. ' a:=getcreator.new();',
  17725. ' with getcreator do begin',
  17726. ' a:=new;',
  17727. ' a:=new();',
  17728. ' end;']);
  17729. ConvertProgram;
  17730. CheckSource('TestExternalClass_FuncClassOf_New',
  17731. LinesToStr([ // statements
  17732. 'this.GetCreator = function () {',
  17733. ' var Result = null;',
  17734. ' Result = ExtA;',
  17735. ' return Result;',
  17736. '};',
  17737. 'this.A = null;',
  17738. '']),
  17739. LinesToStr([ // $mod.$main
  17740. '$mod.A = new ($mod.GetCreator())();',
  17741. '$mod.A = new ($mod.GetCreator())();',
  17742. '$mod.A = new ($mod.GetCreator())();',
  17743. '$mod.A = new ($mod.GetCreator())();',
  17744. 'var $with = $mod.GetCreator();',
  17745. '$mod.A = new $with();',
  17746. '$mod.A = new $with();',
  17747. '']));
  17748. end;
  17749. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17750. begin
  17751. StartProgram(false);
  17752. Add([
  17753. '{$modeswitch externalclass}',
  17754. 'type',
  17755. ' TExtA = class external name ''ExtA''',
  17756. ' constructor New;',
  17757. ' end;',
  17758. ' TBird = class(TExtA)',
  17759. ' end;',
  17760. 'begin',
  17761. ' TBird.new;',
  17762. '']);
  17763. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17764. ConvertProgram;
  17765. end;
  17766. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17767. begin
  17768. StartProgram(false);
  17769. Add([
  17770. '{$modeswitch externalclass}',
  17771. 'type',
  17772. ' TExtA = class external name ''ExtA''',
  17773. ' constructor New;',
  17774. ' end;',
  17775. ' TBird = class(TExtA)',
  17776. ' end;',
  17777. 'begin',
  17778. ' TBird.new();',
  17779. '']);
  17780. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17781. ConvertProgram;
  17782. end;
  17783. procedure TTestModule.TestExternalClass_NewExtName;
  17784. begin
  17785. StartProgram(false);
  17786. Add([
  17787. '{$modeswitch externalclass}',
  17788. 'type',
  17789. ' TExtA = class external name ''ExtA''',
  17790. ' constructor New; external name ''Other'';',
  17791. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17792. ' end;',
  17793. 'var',
  17794. ' A: texta;',
  17795. 'begin',
  17796. ' a:=texta.new;',
  17797. ' a:=texta(texta.new);',
  17798. ' a:=texta.new();',
  17799. ' a:=texta.new(1);',
  17800. ' with texta do begin',
  17801. ' a:=new;',
  17802. ' a:=new();',
  17803. ' a:=new(2);',
  17804. ' end;',
  17805. ' a:=test1.texta.new;',
  17806. ' a:=test1.texta.new();',
  17807. ' a:=test1.texta.new(3);',
  17808. '']);
  17809. ConvertProgram;
  17810. CheckSource('TestExternalClass_NewExtName',
  17811. LinesToStr([ // statements
  17812. 'this.A = null;',
  17813. '']),
  17814. LinesToStr([ // $mod.$main
  17815. '$mod.A = new Other();',
  17816. '$mod.A = new Other();',
  17817. '$mod.A = new Other();',
  17818. '$mod.A = new A.B(1,2);',
  17819. '$mod.A = new Other();',
  17820. '$mod.A = new Other();',
  17821. '$mod.A = new A.B(2,2);',
  17822. '$mod.A = new Other();',
  17823. '$mod.A = new Other();',
  17824. '$mod.A = new A.B(3,2);',
  17825. '']));
  17826. end;
  17827. procedure TTestModule.TestExternalClass_Constructor;
  17828. begin
  17829. StartProgram(false);
  17830. Add([
  17831. '{$modeswitch externalclass}',
  17832. 'type',
  17833. ' TExtA = class external name ''ExtA''',
  17834. ' public type',
  17835. ' TExtB = class external name ''ExtB''',
  17836. ' public type',
  17837. ' TExtC = class external name ''ExtC''',
  17838. ' constructor New;',
  17839. ' constructor New(i: word);',
  17840. ' end;',
  17841. ' end;',
  17842. ' constructor Create;',
  17843. ' constructor Create(i: longint; j: longint = 2);',
  17844. ' end;',
  17845. 'var',
  17846. ' A: texta;',
  17847. ' C: texta.textb.textc;',
  17848. 'begin',
  17849. ' a:=texta.create;',
  17850. ' a:=texta(texta.create);',
  17851. ' a:=texta.create();',
  17852. ' a:=texta.create(1);',
  17853. ' with texta do begin',
  17854. ' a:=create;',
  17855. ' a:=create();',
  17856. ' a:=create(2);',
  17857. ' end;',
  17858. ' a:=test1.texta.create;',
  17859. ' a:=test1.texta.create();',
  17860. ' a:=test1.texta.create(3);',
  17861. ' c:=texta.textb.textc.new;',
  17862. ' c:=texta.textb.textc.new();',
  17863. ' c:=texta.textb.textc.new(4);',
  17864. '']);
  17865. ConvertProgram;
  17866. CheckSource('TestExternalClass_Constructor',
  17867. LinesToStr([ // statements
  17868. 'this.A = null;',
  17869. 'this.C = null;',
  17870. '']),
  17871. LinesToStr([ // $mod.$main
  17872. '$mod.A = new ExtA.Create();',
  17873. '$mod.A = new ExtA.Create();',
  17874. '$mod.A = new ExtA.Create();',
  17875. '$mod.A = new ExtA.Create(1,2);',
  17876. '$mod.A = new ExtA.Create();',
  17877. '$mod.A = new ExtA.Create();',
  17878. '$mod.A = new ExtA.Create(2,2);',
  17879. '$mod.A = new ExtA.Create();',
  17880. '$mod.A = new ExtA.Create();',
  17881. '$mod.A = new ExtA.Create(3,2);',
  17882. '$mod.C = new ExtA.ExtB.ExtC();',
  17883. '$mod.C = new ExtA.ExtB.ExtC();',
  17884. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17885. '']));
  17886. end;
  17887. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17888. begin
  17889. StartProgram(false);
  17890. Add([
  17891. '{$modeswitch externalclass}',
  17892. 'type',
  17893. ' TExtA = class external name ''ExtA''',
  17894. ' constructor Create; external name ''{}'';',
  17895. ' end;',
  17896. 'var',
  17897. ' A: texta;',
  17898. 'begin',
  17899. ' a:=texta.create;',
  17900. ' a:=texta(texta.create);',
  17901. ' a:=texta.create();',
  17902. ' with texta do begin',
  17903. ' a:=create;',
  17904. ' a:=create();',
  17905. ' end;',
  17906. ' a:=test1.texta.create;',
  17907. ' a:=test1.texta.create();',
  17908. '']);
  17909. ConvertProgram;
  17910. CheckSource('TestExternalClass_ConstructorBrackets',
  17911. LinesToStr([ // statements
  17912. 'this.A = null;',
  17913. '']),
  17914. LinesToStr([ // $mod.$main
  17915. '$mod.A = {};',
  17916. '$mod.A = {};',
  17917. '$mod.A = {};',
  17918. '$mod.A = {};',
  17919. '$mod.A = {};',
  17920. '$mod.A = {};',
  17921. '$mod.A = {};',
  17922. '']));
  17923. end;
  17924. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17925. begin
  17926. StartProgram(false);
  17927. Add('{$modeswitch externalclass}');
  17928. Add('type');
  17929. Add(' TExtA = class external name ''ExtA''');
  17930. Add(' constructor New;');
  17931. Add(' end;');
  17932. Add('function DoIt: longint;');
  17933. Add('const ExtA: longint = 3;');
  17934. Add('begin');
  17935. Add(' Result:=ExtA;');
  17936. Add('end;');
  17937. Add('var');
  17938. Add(' A: texta;');
  17939. Add('begin');
  17940. Add(' a:=texta.new;');
  17941. ConvertProgram;
  17942. CheckSource('TestExternalClass_LocalConstSameName',
  17943. LinesToStr([ // statements
  17944. 'var ExtA$1 = 3;',
  17945. 'this.DoIt = function () {',
  17946. ' var Result = 0;',
  17947. ' Result = ExtA$1;',
  17948. ' return Result;',
  17949. '};',
  17950. 'this.A = null;',
  17951. '']),
  17952. LinesToStr([ // $mod.$main
  17953. '$mod.A = new ExtA();',
  17954. '']));
  17955. end;
  17956. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17957. begin
  17958. StartProgram(false);
  17959. Add('{$modeswitch externalclass}');
  17960. Add('type');
  17961. Add(' TExtA = class external name ''ExtA''');
  17962. Add(' procedure DoIt;');
  17963. Add(' end;');
  17964. Add(' TMyA = class(TExtA)');
  17965. Add(' procedure DoIt;');
  17966. Add(' end;');
  17967. Add('procedure TMyA.DoIt; begin end;');
  17968. Add('begin');
  17969. ConvertProgram;
  17970. CheckSource('TestExternalClass_ReintroduceOverload',
  17971. LinesToStr([ // statements
  17972. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17973. ' this.$init = function () {',
  17974. ' };',
  17975. ' this.$final = function () {',
  17976. ' };',
  17977. ' this.DoIt$1 = function () {',
  17978. ' };',
  17979. '});',
  17980. '']),
  17981. LinesToStr([ // $mod.$main
  17982. '']));
  17983. end;
  17984. procedure TTestModule.TestExternalClass_Inherited;
  17985. begin
  17986. StartProgram(false);
  17987. Add('{$modeswitch externalclass}');
  17988. Add('type');
  17989. Add(' TExtA = class external name ''ExtA''');
  17990. Add(' procedure DoIt(i: longint = 1); virtual;');
  17991. Add(' procedure DoSome(j: longint = 2);');
  17992. Add(' end;');
  17993. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17994. Add(' end;');
  17995. Add(' TMyC = class(TExtB)');
  17996. Add(' procedure DoIt(i: longint = 1); override;');
  17997. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17998. Add(' end;');
  17999. Add('procedure TMyC.DoIt(i: longint);');
  18000. Add('begin');
  18001. Add(' inherited;');
  18002. Add(' inherited DoIt;');
  18003. Add(' inherited DoIt();');
  18004. Add(' inherited DoIt(3);');
  18005. Add(' inherited DoSome;');
  18006. Add(' inherited DoSome();');
  18007. Add(' inherited DoSome(4);');
  18008. Add('end;');
  18009. Add('procedure TMyC.DoSome(j: longint);');
  18010. Add('begin');
  18011. Add(' inherited;');
  18012. Add('end;');
  18013. Add('begin');
  18014. ConvertProgram;
  18015. CheckSource('TestExternalClass_ReintroduceOverload',
  18016. LinesToStr([ // statements
  18017. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18018. ' this.$init = function () {',
  18019. ' };',
  18020. ' this.$final = function () {',
  18021. ' };',
  18022. ' this.DoIt = function (i) {',
  18023. ' ExtB.DoIt.apply(this, arguments);',
  18024. ' ExtB.DoIt.call(this, 1);',
  18025. ' ExtB.DoIt.call(this, 1);',
  18026. ' ExtB.DoIt.call(this, 3);',
  18027. ' ExtB.DoSome.call(this, 2);',
  18028. ' ExtB.DoSome.call(this, 2);',
  18029. ' ExtB.DoSome.call(this, 4);',
  18030. ' };',
  18031. ' this.DoSome$1 = function (j) {',
  18032. ' ExtB.DoSome.apply(this, arguments);',
  18033. ' };',
  18034. '});',
  18035. '']),
  18036. LinesToStr([ // $mod.$main
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  18040. begin
  18041. StartProgram(false);
  18042. Add('{$modeswitch externalclass}');
  18043. Add('type');
  18044. Add(' TObject = class');
  18045. Add(' end;');
  18046. Add(' TExtA = class external name ''ExtA''(TObject)');
  18047. Add(' end;');
  18048. Add('begin');
  18049. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  18050. ConvertProgram;
  18051. end;
  18052. procedure TTestModule.TestExternalClass_NewInstance;
  18053. begin
  18054. StartProgram(false);
  18055. Add('{$modeswitch externalclass}');
  18056. Add('type');
  18057. Add(' TExtA = class external name ''ExtA''');
  18058. Add(' end;');
  18059. Add(' TMyB = class(TExtA)');
  18060. Add(' protected');
  18061. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  18062. Add(' end;');
  18063. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18064. Add('begin end;');
  18065. Add('begin');
  18066. ConvertProgram;
  18067. CheckSource('TestExternalClass_NewInstance',
  18068. LinesToStr([ // statements
  18069. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  18070. ' this.$init = function () {',
  18071. ' };',
  18072. ' this.$final = function () {',
  18073. ' };',
  18074. ' this.NewInstance = function (fnname, paramarray) {',
  18075. ' var Result = null;',
  18076. ' return Result;',
  18077. ' };',
  18078. '});',
  18079. '']),
  18080. LinesToStr([ // $mod.$main
  18081. '']));
  18082. end;
  18083. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  18084. begin
  18085. StartProgram(false);
  18086. Add('{$modeswitch externalclass}');
  18087. Add('type');
  18088. Add(' TExtA = class external name ''ExtA''');
  18089. Add(' end;');
  18090. Add(' TMyB = class(TExtA)');
  18091. Add(' protected');
  18092. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  18093. Add(' end;');
  18094. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18095. Add('begin end;');
  18096. Add('begin');
  18097. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  18098. ConvertProgram;
  18099. end;
  18100. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  18101. begin
  18102. StartProgram(false);
  18103. Add('{$modeswitch externalclass}');
  18104. Add('type');
  18105. Add(' TExtA = class external name ''ExtA''');
  18106. Add(' end;');
  18107. Add(' TMyB = class(TExtA)');
  18108. Add(' protected');
  18109. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  18110. Add(' end;');
  18111. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  18112. Add('begin end;');
  18113. Add('begin');
  18114. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  18115. nIncompatibleTypeArgNo);
  18116. ConvertProgram;
  18117. end;
  18118. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  18119. begin
  18120. StartProgram(false);
  18121. Add('{$modeswitch externalclass}');
  18122. Add('type');
  18123. Add(' TExtA = class external name ''ExtA''');
  18124. Add(' end;');
  18125. Add(' TMyB = class(TExtA)');
  18126. Add(' protected');
  18127. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  18128. Add(' end;');
  18129. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  18130. Add('begin end;');
  18131. Add('begin');
  18132. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  18133. nIncompatibleTypeArgNo);
  18134. ConvertProgram;
  18135. end;
  18136. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  18137. begin
  18138. StartProgram(false);
  18139. Add([
  18140. '{$modeswitch externalclass}',
  18141. 'type',
  18142. ' TJSFunction = class external name ''Function''',
  18143. ' end;',
  18144. ' TExtA = class external name ''ExtA''(TJSFunction)',
  18145. ' constructor New(w: word);',
  18146. ' end;',
  18147. ' TBird = class (TExtA)',
  18148. ' public',
  18149. ' Size: word;',
  18150. ' class var Legs: word;',
  18151. ' constructor Create(a: word);',
  18152. ' end;',
  18153. ' TEagle = class (TBird)',
  18154. ' public',
  18155. ' constructor Create(b: word); reintroduce;',
  18156. ' end;',
  18157. 'constructor TBird.Create(a: word);',
  18158. 'begin',
  18159. ' inherited;', // silently ignored
  18160. ' inherited New(a);', // this.$func(a)
  18161. 'end;',
  18162. 'constructor TEagle.Create(b: word);',
  18163. 'begin',
  18164. ' inherited Create(b);',
  18165. 'end;',
  18166. 'var',
  18167. ' Bird: TBird;',
  18168. ' Eagle: TEagle;',
  18169. 'begin',
  18170. ' Bird:=TBird.Create(3);',
  18171. ' Eagle:=TEagle.Create(4);',
  18172. ' Bird.Size:=Bird.Size+5;',
  18173. ' Bird.Legs:=Bird.Legs+6;',
  18174. ' Eagle.Size:=Eagle.Size+5;',
  18175. ' Eagle.Legs:=Eagle.Legs+6;',
  18176. '']);
  18177. ConvertProgram;
  18178. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  18179. LinesToStr([ // statements
  18180. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  18181. ' this.Legs = 0;',
  18182. ' this.$init = function () {',
  18183. ' this.Size = 0;',
  18184. ' };',
  18185. ' this.$final = function () {',
  18186. ' };',
  18187. ' this.Create = function (a) {',
  18188. ' this.$ancestorfunc(a);',
  18189. ' return this;',
  18190. ' };',
  18191. '});',
  18192. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  18193. ' this.Create$1 = function (b) {',
  18194. ' $mod.TBird.Create.call(this, b);',
  18195. ' return this;',
  18196. ' };',
  18197. '});',
  18198. 'this.Bird = null;',
  18199. 'this.Eagle = null;',
  18200. '']),
  18201. LinesToStr([ // $mod.$main
  18202. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  18203. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  18204. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  18205. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  18206. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  18207. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  18208. '']));
  18209. end;
  18210. procedure TTestModule.TestExternalClass_PascalProperty;
  18211. begin
  18212. StartProgram(false);
  18213. Add('{$modeswitch externalclass}');
  18214. Add('type');
  18215. Add(' TJSElement = class;');
  18216. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  18217. Add(' TJSElement = class external name ''ExtA''');
  18218. Add(' end;');
  18219. Add(' TControl = class(TJSElement)');
  18220. Add(' private');
  18221. Add(' FOnClick: TJSNotifyEvent;');
  18222. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  18223. Add(' procedure Click(Sender: TJSElement);');
  18224. Add(' end;');
  18225. Add('procedure TControl.Click(Sender: TJSElement);');
  18226. Add('begin');
  18227. Add(' OnClick(Self);');
  18228. Add('end;');
  18229. Add('var');
  18230. Add(' Ctrl: TControl;');
  18231. Add('begin');
  18232. Add(' Ctrl.OnClick:[email protected];');
  18233. Add(' Ctrl.OnClick(Ctrl);');
  18234. ConvertProgram;
  18235. CheckSource('TestExternalClass_PascalProperty',
  18236. LinesToStr([ // statements
  18237. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  18238. ' this.$init = function () {',
  18239. ' this.FOnClick = null;',
  18240. ' };',
  18241. ' this.$final = function () {',
  18242. ' this.FOnClick = undefined;',
  18243. ' };',
  18244. ' this.Click = function (Sender) {',
  18245. ' this.FOnClick(this);',
  18246. ' };',
  18247. '});',
  18248. 'this.Ctrl = null;',
  18249. '']),
  18250. LinesToStr([ // $mod.$main
  18251. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  18252. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  18253. '']));
  18254. end;
  18255. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  18256. begin
  18257. StartProgram(false);
  18258. Add([
  18259. '{$modeswitch externalclass}',
  18260. 'type',
  18261. ' IUnknown = interface end;',
  18262. ' TObject = class',
  18263. ' end;',
  18264. ' TChild = class',
  18265. ' end;',
  18266. ' TExtRootA = class external name ''ExtRootA''',
  18267. ' end;',
  18268. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  18269. ' end;',
  18270. ' TExtRootB = class external name ''ExtRootB''',
  18271. ' end;',
  18272. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  18273. ' end;',
  18274. ' TExtString = class external name ''String''',
  18275. ' function charAt(aIndex : NativeInt) : string;',
  18276. ' end;',
  18277. 'var',
  18278. ' Obj: TObject;',
  18279. ' Child: TChild;',
  18280. ' RootA: TExtRootA;',
  18281. ' ChildA: TExtChildA;',
  18282. ' RootB: TExtRootB;',
  18283. ' ChildB: TExtChildB;',
  18284. ' i: IUnknown;',
  18285. ' s: string;',
  18286. ' v: jsvalue;',
  18287. 'begin',
  18288. ' obj:=tobject(roota);',
  18289. ' obj:=tobject(childa);',
  18290. ' child:=tchild(tobject(roota));',
  18291. ' roota:=textroota(obj);',
  18292. ' roota:=textroota(child);',
  18293. ' roota:=textroota(rootb);',
  18294. ' roota:=textroota(childb);',
  18295. ' childa:=textchilda(textroota(obj));',
  18296. ' roota:=TExtRootA(i);',
  18297. ' s:=TExtString(s).charAt(7);',
  18298. ' s:=TExtString(v).charAt(8);',
  18299. '']);
  18300. ConvertProgram;
  18301. CheckSource('TestExternalClass_TypeCastToRootClass',
  18302. LinesToStr([ // statements
  18303. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18304. 'rtl.createClass(this, "TObject", null, function () {',
  18305. ' this.$init = function () {',
  18306. ' };',
  18307. ' this.$final = function () {',
  18308. ' };',
  18309. '});',
  18310. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18311. '});',
  18312. 'this.Obj = null;',
  18313. 'this.Child = null;',
  18314. 'this.RootA = null;',
  18315. 'this.ChildA = null;',
  18316. 'this.RootB = null;',
  18317. 'this.ChildB = null;',
  18318. 'this.i = null;',
  18319. 'this.s = "";',
  18320. 'this.v = undefined;',
  18321. '']),
  18322. LinesToStr([ // $mod.$main
  18323. '$mod.Obj = $mod.RootA;',
  18324. '$mod.Obj = $mod.ChildA;',
  18325. '$mod.Child = $mod.RootA;',
  18326. '$mod.RootA = $mod.Obj;',
  18327. '$mod.RootA = $mod.Child;',
  18328. '$mod.RootA = $mod.RootB;',
  18329. '$mod.RootA = $mod.ChildB;',
  18330. '$mod.ChildA = $mod.Obj;',
  18331. '$mod.RootA = $mod.i;',
  18332. '$mod.s = $mod.s.charAt(7);',
  18333. '$mod.s = $mod.v.charAt(8);',
  18334. '']));
  18335. end;
  18336. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  18337. begin
  18338. StartProgram(false);
  18339. Add([
  18340. '{$modeswitch externalclass}',
  18341. 'type',
  18342. ' IUnknown = interface end;',
  18343. ' IBird = interface(IUnknown) end;',
  18344. ' TClass = class of TObject;',
  18345. ' TObject = class',
  18346. ' end;',
  18347. ' TChild = class',
  18348. ' end;',
  18349. ' TJSObject = class external name ''Object''',
  18350. ' end;',
  18351. ' TRec = record end;',
  18352. 'var',
  18353. ' Obj: TObject;',
  18354. ' Child: TChild;',
  18355. ' i: IUnknown;',
  18356. ' Bird: IBird;',
  18357. ' j: TJSObject;',
  18358. ' r: TRec;',
  18359. ' c: TClass;',
  18360. 'begin',
  18361. ' j:=tjsobject(IUnknown);',
  18362. ' j:=tjsobject(IBird);',
  18363. ' j:=tjsobject(TObject);',
  18364. ' j:=tjsobject(TChild);',
  18365. ' j:=tjsobject(TRec);',
  18366. ' j:=tjsobject(Obj);',
  18367. ' j:=tjsobject(Child);',
  18368. ' j:=tjsobject(i);',
  18369. ' j:=tjsobject(Bird);',
  18370. ' j:=tjsobject(r);',
  18371. ' j:=tjsobject(c);',
  18372. '']);
  18373. ConvertProgram;
  18374. CheckSource('TestExternalClass_TypeCastToJSObject',
  18375. LinesToStr([ // statements
  18376. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18377. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18378. 'rtl.createClass(this, "TObject", null, function () {',
  18379. ' this.$init = function () {',
  18380. ' };',
  18381. ' this.$final = function () {',
  18382. ' };',
  18383. '});',
  18384. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18385. '});',
  18386. 'rtl.recNewT(this, "TRec", function () {',
  18387. ' this.$eq = function (b) {',
  18388. ' return true;',
  18389. ' };',
  18390. ' this.$assign = function (s) {',
  18391. ' return this;',
  18392. ' };',
  18393. '});',
  18394. 'this.Obj = null;',
  18395. 'this.Child = null;',
  18396. 'this.i = null;',
  18397. 'this.Bird = null;',
  18398. 'this.j = null;',
  18399. 'this.r = this.TRec.$new();',
  18400. 'this.c = null;',
  18401. '']),
  18402. LinesToStr([ // $mod.$main
  18403. '$mod.j = $mod.IUnknown;',
  18404. '$mod.j = $mod.IBird;',
  18405. '$mod.j = $mod.TObject;',
  18406. '$mod.j = $mod.TChild;',
  18407. '$mod.j = $mod.TRec;',
  18408. '$mod.j = $mod.Obj;',
  18409. '$mod.j = $mod.Child;',
  18410. '$mod.j = $mod.i;',
  18411. '$mod.j = $mod.Bird;',
  18412. '$mod.j = $mod.r;',
  18413. '$mod.j = $mod.c;',
  18414. '']));
  18415. end;
  18416. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18417. begin
  18418. StartProgram(false);
  18419. Add('{$modeswitch externalclass}');
  18420. Add('type');
  18421. Add(' TJSString = class external name ''String''');
  18422. Add(' class function fromCharCode() : string; varargs;');
  18423. Add(' function anchor(const aName : string) : string;');
  18424. Add(' end;');
  18425. Add('var');
  18426. Add(' s: string;');
  18427. Add('begin');
  18428. Add(' s:=TJSString.fromCharCode(65,66);');
  18429. Add(' s:=TJSString(s).anchor(s);');
  18430. Add(' s:=TJSString(''foo'').anchor(s);');
  18431. ConvertProgram;
  18432. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18433. LinesToStr([ // statements
  18434. 'this.s = "";',
  18435. '']),
  18436. LinesToStr([ // $mod.$main
  18437. '$mod.s = String.fromCharCode(65, 66);',
  18438. '$mod.s = $mod.s.anchor($mod.s);',
  18439. '$mod.s = "foo".anchor($mod.s);',
  18440. '']));
  18441. end;
  18442. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18443. begin
  18444. StartProgram(false);
  18445. Add([
  18446. '{$modeswitch externalclass}',
  18447. 'type',
  18448. ' TJSObject = class external name ''Object'' end;',
  18449. ' TJSFunction = class external name ''Function''',
  18450. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18451. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18452. ' end;',
  18453. ' TObject = class',
  18454. ' procedure DoIt(i: longint);',
  18455. ' end;',
  18456. ' TFuncInt = function(o: TObject): longint;',
  18457. 'function GetIt(o: TObject): longint;',
  18458. ' procedure Sub; begin end;',
  18459. 'var',
  18460. ' f: TJSFunction;',
  18461. ' fi: TFuncInt;',
  18462. 'begin',
  18463. ' fi:=TFuncInt(f);',
  18464. ' f:=TJSFunction(fi);',
  18465. ' f:=TJSFunction(@GetIt);',
  18466. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18467. ' f:=TJSFunction(@Sub);',
  18468. ' f:=TJSFunction(@o.doit);',
  18469. ' f:=TJSFunction(fi).bind(nil,4)',
  18470. 'end;',
  18471. 'procedure TObject.DoIt(i: longint);',
  18472. ' procedure Sub; begin end;',
  18473. 'var f: TJSFunction;',
  18474. 'begin',
  18475. ' f:=TJSFunction(@DoIt);',
  18476. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18477. ' f:=TJSFunction(@Sub);',
  18478. ' f:=TJSFunction(@GetIt);',
  18479. 'end;',
  18480. 'begin']);
  18481. ConvertProgram;
  18482. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18483. LinesToStr([ // statements
  18484. 'rtl.createClass(this, "TObject", null, function () {',
  18485. ' this.$init = function () {',
  18486. ' };',
  18487. ' this.$final = function () {',
  18488. ' };',
  18489. ' this.DoIt = function (i) {',
  18490. ' var $Self = this;',
  18491. ' function Sub() {',
  18492. ' };',
  18493. ' var f = null;',
  18494. ' f = this.DoIt;',
  18495. ' f = this.DoIt.bind(null, 13);',
  18496. ' f = Sub;',
  18497. ' f = $mod.GetIt;',
  18498. ' };',
  18499. '});',
  18500. 'this.GetIt = function (o) {',
  18501. ' var Result = 0;',
  18502. ' function Sub() {',
  18503. ' };',
  18504. ' var f = null;',
  18505. ' var fi = null;',
  18506. ' fi = f;',
  18507. ' f = fi;',
  18508. ' f = $mod.GetIt;',
  18509. ' f = $mod.GetIt.bind(null, 3);',
  18510. ' f = Sub;',
  18511. ' f = $mod.TObject.DoIt;',
  18512. ' f = fi.bind(null, 4);',
  18513. ' return Result;',
  18514. '};',
  18515. '']),
  18516. LinesToStr([ // $mod.$main
  18517. '']));
  18518. end;
  18519. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18520. begin
  18521. StartProgram(false);
  18522. Add([
  18523. '{$mode delphi}',
  18524. '{$modeswitch externalclass}',
  18525. 'type',
  18526. ' TJSObject = class external name ''Object'' end;',
  18527. ' TJSWindow = class external name ''Window''(TJSObject)',
  18528. ' procedure Open;',
  18529. ' end;',
  18530. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18531. ' procedure Execute;',
  18532. ' end;',
  18533. 'procedure Fly;',
  18534. 'var',
  18535. ' w: TJSWindow;',
  18536. ' e: TJSEventTarget;',
  18537. 'begin',
  18538. ' w:=TJSWindow(e);',
  18539. ' e:=TJSEventTarget(w);',
  18540. 'end;',
  18541. 'begin']);
  18542. ConvertProgram;
  18543. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18544. LinesToStr([ // statements
  18545. 'this.Fly = function () {',
  18546. ' var w = null;',
  18547. ' var e = null;',
  18548. ' w = e;',
  18549. ' e = w;',
  18550. '};',
  18551. '']),
  18552. LinesToStr([ // $mod.$main
  18553. '']));
  18554. end;
  18555. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18556. begin
  18557. StartProgram(false);
  18558. Add('{$modeswitch externalclass}');
  18559. Add('type');
  18560. Add(' TJSString = class external name ''String''');
  18561. Add(' class function fromCharCode() : string; varargs;');
  18562. Add(' end;');
  18563. Add('var');
  18564. Add(' s: string;');
  18565. Add(' sObj: TJSString;');
  18566. Add('begin');
  18567. Add(' s:=sObj.fromCharCode(65,66);');
  18568. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18569. nExternalClassInstanceCannotAccessStaticX);
  18570. ConvertProgram;
  18571. end;
  18572. procedure TTestModule.TestExternalClass_BracketAccessor;
  18573. begin
  18574. StartProgram(false);
  18575. Add([
  18576. '{$modeswitch externalclass}',
  18577. 'type',
  18578. ' TJSArray = class external name ''Array2''',
  18579. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18580. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18581. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18582. ' end;',
  18583. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18584. 'begin end;',
  18585. 'var',
  18586. ' Arr: tjsarray;',
  18587. ' s: string;',
  18588. ' i: longint;',
  18589. ' v: jsvalue;',
  18590. 'begin',
  18591. ' v:=arr[0];',
  18592. ' v:=arr.items[1];',
  18593. ' arr[2]:=s;',
  18594. ' arr.items[3]:=s;',
  18595. ' arr[4]:=i;',
  18596. ' arr[5]:=arr[6];',
  18597. ' arr.items[7]:=arr.items[8];',
  18598. ' with arr do items[9]:=items[10];',
  18599. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18600. ' with arr do begin',
  18601. ' v:=GetItems(14);',
  18602. ' setitems(15,16);',
  18603. ' end;',
  18604. ' v:=test1.arr.items[17];',
  18605. ' test1.arr.items[18]:=v;',
  18606. '']);
  18607. ConvertProgram;
  18608. CheckSource('TestExternalClass_BracketAccessor',
  18609. LinesToStr([ // statements
  18610. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18611. '};',
  18612. 'this.Arr = null;',
  18613. 'this.s = "";',
  18614. 'this.i = 0;',
  18615. 'this.v = undefined;',
  18616. '']),
  18617. LinesToStr([ // $mod.$main
  18618. '$mod.v = $mod.Arr[0];',
  18619. '$mod.v = $mod.Arr[1];',
  18620. '$mod.Arr[2] = $mod.s;',
  18621. '$mod.Arr[3] = $mod.s;',
  18622. '$mod.Arr[4] = $mod.i;',
  18623. '$mod.Arr[5] = $mod.Arr[6];',
  18624. '$mod.Arr[7] = $mod.Arr[8];',
  18625. 'var $with = $mod.Arr;',
  18626. '$with[9] = $with[10];',
  18627. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18628. ' a: 9,',
  18629. ' p: $mod.Arr,',
  18630. ' get: function () {',
  18631. ' return this.p[this.a];',
  18632. ' },',
  18633. ' set: function (v) {',
  18634. ' this.p[this.a] = v;',
  18635. ' }',
  18636. '}, {',
  18637. ' a: 10,',
  18638. ' p: $mod.Arr,',
  18639. ' get: function () {',
  18640. ' return this.p[this.a];',
  18641. ' },',
  18642. ' set: function (v) {',
  18643. ' this.p[this.a] = v;',
  18644. ' }',
  18645. '});',
  18646. 'var $with1 = $mod.Arr;',
  18647. '$mod.v = $with1[14];',
  18648. '$with1[15] = 16;',
  18649. '$mod.v = $mod.Arr[17];',
  18650. '$mod.Arr[18] = $mod.v;',
  18651. '']));
  18652. end;
  18653. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18654. begin
  18655. StartProgram(false);
  18656. Add([
  18657. '{$modeswitch externalclass}',
  18658. 'type',
  18659. ' TJSArray = class external name ''Array2''',
  18660. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18661. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18662. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18663. ' end;',
  18664. ' TMyArr = class(TJSArray)',
  18665. ' procedure DoIt;',
  18666. ' end;',
  18667. 'procedure tmyarr.DoIt;',
  18668. 'begin',
  18669. ' Items[1]:=Items[2];',
  18670. ' SetItems(3,getItems(4));',
  18671. 'end;',
  18672. 'var',
  18673. ' Arr: tmyarr;',
  18674. ' s: string;',
  18675. ' i: longint;',
  18676. ' v: jsvalue;',
  18677. 'begin',
  18678. ' v:=arr[0];',
  18679. ' v:=arr.items[1];',
  18680. ' arr[2]:=s;',
  18681. ' arr.items[3]:=s;',
  18682. ' arr[4]:=i;',
  18683. ' arr[5]:=arr[6];',
  18684. ' arr.items[7]:=arr.items[8];',
  18685. ' with arr do items[9]:=items[10];',
  18686. ' with arr do begin',
  18687. ' v:=GetItems(14);',
  18688. ' setitems(15,16);',
  18689. ' end;',
  18690. '']);
  18691. ConvertProgram;
  18692. CheckSource('TestExternalClass_BracketAccessor_Call',
  18693. LinesToStr([ // statements
  18694. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18695. ' this.$init = function () {',
  18696. ' };',
  18697. ' this.$final = function () {',
  18698. ' };',
  18699. ' this.DoIt = function () {',
  18700. ' this[1] = this[2];',
  18701. ' this[3] = this[4];',
  18702. ' };',
  18703. '});',
  18704. 'this.Arr = null;',
  18705. 'this.s = "";',
  18706. 'this.i = 0;',
  18707. 'this.v = undefined;',
  18708. '']),
  18709. LinesToStr([ // $mod.$main
  18710. '$mod.v = $mod.Arr[0];',
  18711. '$mod.v = $mod.Arr[1];',
  18712. '$mod.Arr[2] = $mod.s;',
  18713. '$mod.Arr[3] = $mod.s;',
  18714. '$mod.Arr[4] = $mod.i;',
  18715. '$mod.Arr[5] = $mod.Arr[6];',
  18716. '$mod.Arr[7] = $mod.Arr[8];',
  18717. 'var $with = $mod.Arr;',
  18718. '$with[9] = $with[10];',
  18719. 'var $with1 = $mod.Arr;',
  18720. '$mod.v = $with1[14];',
  18721. '$with1[15] = 16;',
  18722. '']));
  18723. end;
  18724. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18725. begin
  18726. StartProgram(false);
  18727. Add('{$modeswitch externalclass}');
  18728. Add('type');
  18729. Add(' TJSArray = class external name ''Array2''');
  18730. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18731. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18732. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18733. Add(' end;');
  18734. Add('begin');
  18735. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18736. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18737. ConvertProgram;
  18738. end;
  18739. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18740. begin
  18741. StartProgram(false);
  18742. Add('{$modeswitch externalclass}');
  18743. Add('type');
  18744. Add(' TJSArray = class external name ''Array2''');
  18745. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18746. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18747. Add(' end;');
  18748. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18749. Add('begin end;');
  18750. Add('var');
  18751. Add(' Arr: tjsarray;');
  18752. Add(' v: jsvalue;');
  18753. Add('begin');
  18754. Add(' v:=arr[0];');
  18755. Add(' v:=arr.items[1];');
  18756. Add(' with arr do v:=items[2];');
  18757. Add(' doit(arr[3],arr[4]);');
  18758. ConvertProgram;
  18759. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18760. LinesToStr([ // statements
  18761. 'this.DoIt = function (vI, vJ) {',
  18762. '};',
  18763. 'this.Arr = null;',
  18764. 'this.v = undefined;',
  18765. '']),
  18766. LinesToStr([ // $mod.$main
  18767. '$mod.v = $mod.Arr[0];',
  18768. '$mod.v = $mod.Arr[1];',
  18769. 'var $with = $mod.Arr;',
  18770. '$mod.v = $with[2];',
  18771. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18772. '']));
  18773. end;
  18774. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18775. begin
  18776. StartProgram(false);
  18777. Add('{$modeswitch externalclass}');
  18778. Add('type');
  18779. Add(' TJSArray = class external name ''Array2''');
  18780. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18781. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18782. Add(' end;');
  18783. Add('var');
  18784. Add(' Arr: tjsarray;');
  18785. Add(' s: string;');
  18786. Add(' i: longint;');
  18787. Add(' v: jsvalue;');
  18788. Add('begin');
  18789. Add(' arr[2]:=s;');
  18790. Add(' arr.items[3]:=s;');
  18791. Add(' arr[4]:=i;');
  18792. Add(' with arr do items[5]:=i;');
  18793. ConvertProgram;
  18794. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18795. LinesToStr([ // statements
  18796. 'this.Arr = null;',
  18797. 'this.s = "";',
  18798. 'this.i = 0;',
  18799. 'this.v = undefined;',
  18800. '']),
  18801. LinesToStr([ // $mod.$main
  18802. '$mod.Arr[2] = $mod.s;',
  18803. '$mod.Arr[3] = $mod.s;',
  18804. '$mod.Arr[4] = $mod.i;',
  18805. 'var $with = $mod.Arr;',
  18806. '$with[5] = $mod.i;',
  18807. '']));
  18808. end;
  18809. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18810. begin
  18811. StartProgram(false);
  18812. Add('{$modeswitch externalclass}');
  18813. Add('type');
  18814. Add(' TJSArray = class external name ''Array2''');
  18815. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18816. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18817. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18818. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18819. Add(' end;');
  18820. Add('var');
  18821. Add(' Arr: tjsarray;');
  18822. Add(' s: string;');
  18823. Add(' i: longint;');
  18824. Add(' v: jsvalue;');
  18825. Add('begin');
  18826. Add(' arr[2]:=s;');
  18827. Add(' arr.items[3]:=s;');
  18828. Add(' arr.numbers[4]:=i;');
  18829. Add(' with arr do items[5]:=i;');
  18830. Add(' with arr do numbers[6]:=i;');
  18831. ConvertProgram;
  18832. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18833. LinesToStr([ // statements
  18834. 'this.Arr = null;',
  18835. 'this.s = "";',
  18836. 'this.i = 0;',
  18837. 'this.v = undefined;',
  18838. '']),
  18839. LinesToStr([ // $mod.$main
  18840. '$mod.Arr[2] = $mod.s;',
  18841. '$mod.Arr[3] = $mod.s;',
  18842. '$mod.Arr[4] = $mod.i;',
  18843. 'var $with = $mod.Arr;',
  18844. '$with[5] = $mod.i;',
  18845. 'var $with1 = $mod.Arr;',
  18846. '$with1[6] = $mod.i;',
  18847. '']));
  18848. end;
  18849. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18850. begin
  18851. StartProgram(false);
  18852. Add('{$modeswitch externalclass}');
  18853. Add('type');
  18854. Add(' TJSArray = class external name ''Array2''');
  18855. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18856. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18857. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18858. Add(' end;');
  18859. Add('var');
  18860. Add(' Arr: tjsarray;');
  18861. Add(' i: longint;');
  18862. Add(' IntArr: array of longint;');
  18863. Add(' v: jsvalue;');
  18864. Add('begin');
  18865. Add(' v:=arr.items[i];');
  18866. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18867. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18868. ConvertProgram;
  18869. CheckSource('TestExternalClass_BracketAccessor_Index',
  18870. LinesToStr([ // statements
  18871. 'this.Arr = null;',
  18872. 'this.i = 0;',
  18873. 'this.IntArr = [];',
  18874. 'this.v = undefined;',
  18875. '']),
  18876. LinesToStr([ // $mod.$main
  18877. '$mod.v = $mod.Arr[$mod.i];',
  18878. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18879. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18880. '']));
  18881. end;
  18882. procedure TTestModule.TestExternalClass_ForInJSObject;
  18883. begin
  18884. StartProgram(false);
  18885. Add([
  18886. '{$modeswitch externalclass}',
  18887. 'type',
  18888. ' TJSObject = class external name ''Object''',
  18889. ' end;',
  18890. 'var',
  18891. ' o: TJSObject;',
  18892. ' key: string;',
  18893. 'begin',
  18894. ' for key in o do',
  18895. ' if key=''abc'' then ;',
  18896. '']);
  18897. ConvertProgram;
  18898. CheckSource('TestExternalClass_ForInJSObject',
  18899. LinesToStr([ // statements
  18900. 'this.o = null;',
  18901. 'this.key = "";',
  18902. '']),
  18903. LinesToStr([ // $mod.$main
  18904. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18905. '']));
  18906. end;
  18907. procedure TTestModule.TestExternalClass_ForInJSArray;
  18908. begin
  18909. StartProgram(false);
  18910. Add([
  18911. '{$modeswitch externalclass}',
  18912. 'type',
  18913. ' TJSInt8Array = class external name ''Int8Array''',
  18914. ' private',
  18915. ' flength: NativeInt external name ''length'';',
  18916. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18917. ' public',
  18918. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18919. ' property Length: NativeInt read flength;',
  18920. ' end;',
  18921. 'var',
  18922. ' a: TJSInt8Array;',
  18923. ' value: shortint;',
  18924. 'begin',
  18925. ' for value in a do',
  18926. ' if value=3 then ;',
  18927. '']);
  18928. ConvertProgram;
  18929. CheckSource('TestExternalClass_ForInJSArray',
  18930. LinesToStr([ // statements
  18931. 'this.a = null;',
  18932. 'this.value = 0;',
  18933. '']),
  18934. LinesToStr([ // $mod.$main
  18935. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18936. ' $mod.value = $in[$l];',
  18937. ' if ($mod.value === 3) ;',
  18938. '};',
  18939. '']));
  18940. end;
  18941. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18942. begin
  18943. AddModuleWithIntfImplSrc('unit2.pas',
  18944. LinesToStr([
  18945. '{$modeswitch externalclass}',
  18946. 'type',
  18947. ' TJSBufferSource = class external name ''BufferSource''',
  18948. ' end;',
  18949. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18950. '']),
  18951. '');
  18952. AddModuleWithIntfImplSrc('unit3.pas',
  18953. LinesToStr([
  18954. '{$modeswitch externalclass}',
  18955. 'type',
  18956. ' TJSBufferSource = class external name ''BufferSource''',
  18957. ' end;',
  18958. '']),
  18959. '');
  18960. StartUnit(true);
  18961. Add([
  18962. 'interface',
  18963. 'uses unit2, unit3;',
  18964. 'procedure DoSome(s: TJSBufferSource);',
  18965. 'implementation',
  18966. 'procedure DoSome(s: TJSBufferSource);',
  18967. 'begin',
  18968. ' DoIt(s);',
  18969. 'end;',
  18970. '']);
  18971. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18972. nIncompatibleTypeArgNo);
  18973. ConvertUnit;
  18974. end;
  18975. procedure TTestModule.TestExternalClass_NestedConstructor;
  18976. begin
  18977. StartProgram(false);
  18978. Add([
  18979. '{$modeswitch externalclass}',
  18980. 'type',
  18981. ' TJSObject = class external name ''Object''',
  18982. ' type TBird = class external name ''Bird''',
  18983. ' type TWing = class external name ''Wing''',
  18984. ' constructor New;',
  18985. ' constructor Create(w: word = 3);',
  18986. ' end;',
  18987. ' end;',
  18988. ' end;',
  18989. 'var',
  18990. ' w: TJSObject.TBird.TWing;',
  18991. 'begin',
  18992. ' w:=tjsobject.tbird.twing.new;',
  18993. ' w:=tjsobject.tbird.twing.new();',
  18994. ' w:=tjsobject.tbird.twing.create;',
  18995. ' w:=tjsobject.tbird.twing.create(4);',
  18996. ' with tjsobject do begin',
  18997. ' w:=tbird.twing.new;',
  18998. ' w:=tbird.twing.new();',
  18999. ' w:=tbird.twing.create;',
  19000. ' w:=tbird.twing.create(11);',
  19001. ' end;',
  19002. ' with tjsobject.tbird do begin',
  19003. ' w:=twing.new;',
  19004. ' w:=twing.new();',
  19005. ' w:=twing.create;',
  19006. ' w:=twing.create(21);',
  19007. ' end;',
  19008. ' with tjsobject.tbird.twing do begin',
  19009. ' w:=new;',
  19010. ' w:=new();',
  19011. ' w:=create;',
  19012. ' w:=create(31);',
  19013. ' end;',
  19014. '']);
  19015. ConvertProgram;
  19016. CheckSource('TestExternalClass_NestedConstructor',
  19017. LinesToStr([ // statements
  19018. 'this.w = null;',
  19019. '']),
  19020. LinesToStr([ // $mod.$main
  19021. '$mod.w = new Object.Bird.Wing();',
  19022. '$mod.w = new Object.Bird.Wing();',
  19023. '$mod.w = new Object.Bird.Wing.Create();',
  19024. '$mod.w = new Object.Bird.Wing.Create(4);',
  19025. '$mod.w = new Object.Bird.Wing();',
  19026. '$mod.w = new Object.Bird.Wing();',
  19027. '$mod.w = new Object.Bird.Wing.Create();',
  19028. '$mod.w = new Object.Bird.Wing.Create(11);',
  19029. 'var $with = Object.Bird;',
  19030. '$mod.w = new Object.Bird.Wing();',
  19031. '$mod.w = new Object.Bird.Wing();',
  19032. '$mod.w = new Object.Bird.Wing.Create();',
  19033. '$mod.w = new Object.Bird.Wing.Create(21);',
  19034. 'var $with1 = Object.Bird.Wing;',
  19035. '$mod.w = new $with1();',
  19036. '$mod.w = new $with1();',
  19037. '$mod.w = new Object.Bird.Wing.Create();',
  19038. '$mod.w = new Object.Bird.Wing.Create(31);',
  19039. '']));
  19040. end;
  19041. procedure TTestModule.TestClassInterface_Corba;
  19042. begin
  19043. StartProgram(false);
  19044. Add([
  19045. '{$interfaces corba}',
  19046. 'type',
  19047. ' IUnknown = interface;',
  19048. ' IUnknown = interface',
  19049. ' [''{00000000-0000-0000-C000-000000000046}'']',
  19050. ' end;',
  19051. ' IInterface = IUnknown;',
  19052. ' IBird = interface(IInterface)',
  19053. ' function GetSize: longint;',
  19054. ' procedure SetSize(i: longint);',
  19055. ' property Size: longint read GetSize write SetSize;',
  19056. ' procedure DoIt(i: longint);',
  19057. ' end;',
  19058. ' TObject = class',
  19059. ' end;',
  19060. ' TBird = class(TObject,IBird)',
  19061. ' function GetSize: longint; virtual; abstract;',
  19062. ' procedure SetSize(i: longint); virtual; abstract;',
  19063. ' procedure DoIt(i: longint); virtual; abstract;',
  19064. ' end;',
  19065. 'var',
  19066. ' BirdIntf: IBird;',
  19067. 'begin',
  19068. ' BirdIntf.Size:=BirdIntf.Size;',
  19069. '']);
  19070. ConvertProgram;
  19071. CheckSource('TestClassInterface_Corba',
  19072. LinesToStr([ // statements
  19073. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  19074. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  19075. 'rtl.createClass(this, "TObject", null, function () {',
  19076. ' this.$init = function () {',
  19077. ' };',
  19078. ' this.$final = function () {',
  19079. ' };',
  19080. '});',
  19081. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19082. ' rtl.addIntf(this, $mod.IBird);',
  19083. '});',
  19084. 'this.BirdIntf = null;',
  19085. '']),
  19086. LinesToStr([ // $mod.$main
  19087. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  19088. '']));
  19089. end;
  19090. procedure TTestModule.TestClassInterface_ProcExternalFail;
  19091. begin
  19092. StartProgram(false);
  19093. Add([
  19094. '{$interfaces corba}',
  19095. 'type',
  19096. ' IUnknown = interface',
  19097. ' procedure DoIt; external name ''foo'';',
  19098. ' end;',
  19099. 'begin']);
  19100. SetExpectedParserError(
  19101. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  19102. nParserNoFieldsAllowed);
  19103. ConvertProgram;
  19104. end;
  19105. procedure TTestModule.TestClassInterface_Overloads;
  19106. begin
  19107. StartProgram(false);
  19108. Add([
  19109. '{$interfaces corba}',
  19110. 'type',
  19111. ' integer = longint;',
  19112. ' IUnknown = interface',
  19113. ' procedure DoIt(i: integer);',
  19114. ' procedure DoIt(s: string);',
  19115. ' end;',
  19116. ' IBird = interface(IUnknown)',
  19117. ' procedure DoIt(b: boolean); overload;',
  19118. ' end;',
  19119. ' TObject = class',
  19120. ' end;',
  19121. ' TBird = class(TObject,IBird)',
  19122. ' procedure DoIt(o: TObject);',
  19123. ' procedure DoIt(s: string);',
  19124. ' procedure DoIt(i: integer);',
  19125. ' procedure DoIt(b: boolean);',
  19126. ' end;',
  19127. 'procedure TBird.DoIt(o: TObject); begin end;',
  19128. 'procedure TBird.DoIt(s: string); begin end;',
  19129. 'procedure TBird.DoIt(i: integer); begin end;',
  19130. 'procedure TBird.DoIt(b: boolean); begin end;',
  19131. 'var',
  19132. ' BirdIntf: IBird;',
  19133. 'begin',
  19134. ' BirdIntf.DoIt(3);',
  19135. ' BirdIntf.DoIt(''abc'');',
  19136. ' BirdIntf.DoIt(true);',
  19137. '']);
  19138. ConvertProgram;
  19139. CheckSource('TestClassInterface_Overloads',
  19140. LinesToStr([ // statements
  19141. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  19142. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  19143. 'rtl.createClass(this, "TObject", null, function () {',
  19144. ' this.$init = function () {',
  19145. ' };',
  19146. ' this.$final = function () {',
  19147. ' };',
  19148. '});',
  19149. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19150. ' this.DoIt = function (o) {',
  19151. ' };',
  19152. ' this.DoIt$1 = function (s) {',
  19153. ' };',
  19154. ' this.DoIt$2 = function (i) {',
  19155. ' };',
  19156. ' this.DoIt$3 = function (b) {',
  19157. ' };',
  19158. ' rtl.addIntf(this, $mod.IBird, {',
  19159. ' DoIt$2: "DoIt$3",',
  19160. ' DoIt: "DoIt$2"',
  19161. ' });',
  19162. '});',
  19163. 'this.BirdIntf = null;',
  19164. '']),
  19165. LinesToStr([ // $mod.$main
  19166. '$mod.BirdIntf.DoIt(3);',
  19167. '$mod.BirdIntf.DoIt$1("abc");',
  19168. '$mod.BirdIntf.DoIt$2(true);',
  19169. '']));
  19170. end;
  19171. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  19172. begin
  19173. StartProgram(false);
  19174. Add([
  19175. '{$interfaces corba}',
  19176. 'type',
  19177. ' IBird = interface',
  19178. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19179. ' end;',
  19180. ' IDog = interface',
  19181. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19182. ' end;',
  19183. ' TObject = class(IBird,IDog)',
  19184. ' end;',
  19185. 'begin']);
  19186. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  19187. nDuplicateGUIDXInYZ);
  19188. ConvertProgram;
  19189. end;
  19190. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  19191. begin
  19192. StartProgram(false);
  19193. Add([
  19194. '{$interfaces corba}',
  19195. 'type',
  19196. ' IAnimal = interface',
  19197. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19198. ' end;',
  19199. ' IBird = interface(IAnimal)',
  19200. ' end;',
  19201. ' IHawk = interface(IBird)',
  19202. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19203. ' end;',
  19204. 'begin']);
  19205. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  19206. nDuplicateGUIDXInYZ);
  19207. ConvertProgram;
  19208. end;
  19209. procedure TTestModule.TestClassInterface_AncestorImpl;
  19210. begin
  19211. StartProgram(false);
  19212. Add([
  19213. '{$interfaces corba}',
  19214. 'type',
  19215. ' integer = longint;',
  19216. ' IUnknown = interface',
  19217. ' procedure DoIt(i: integer);',
  19218. ' end;',
  19219. ' IBird = interface',
  19220. ' procedure Fly(i: integer);',
  19221. ' end;',
  19222. ' TObject = class(IUnknown)',
  19223. ' procedure DoIt(i: integer);',
  19224. ' end;',
  19225. ' TBird = class(IBird)',
  19226. ' procedure Fly(i: integer);',
  19227. ' end;',
  19228. 'procedure TObject.DoIt(i: integer); begin end;',
  19229. 'procedure TBird.Fly(i: integer); begin end;',
  19230. 'begin',
  19231. '']);
  19232. ConvertProgram;
  19233. CheckSource('TestClassInterface_AncestorIntf',
  19234. LinesToStr([ // statements
  19235. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  19236. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  19237. 'rtl.createClass(this, "TObject", null, function () {',
  19238. ' this.$init = function () {',
  19239. ' };',
  19240. ' this.$final = function () {',
  19241. ' };',
  19242. ' this.DoIt = function (i) {',
  19243. ' };',
  19244. ' rtl.addIntf(this, $mod.IUnknown);',
  19245. '});',
  19246. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19247. ' this.Fly = function (i) {',
  19248. ' };',
  19249. ' rtl.addIntf(this, $mod.IBird);',
  19250. ' rtl.addIntf(this, $mod.IUnknown);',
  19251. '});',
  19252. '']),
  19253. LinesToStr([ // $mod.$main
  19254. '']));
  19255. end;
  19256. procedure TTestModule.TestClassInterface_ImplReintroduce;
  19257. begin
  19258. StartProgram(false);
  19259. Add([
  19260. '{$interfaces corba}',
  19261. 'type',
  19262. ' integer = longint;',
  19263. ' IBird = interface',
  19264. ' procedure DoIt(i: integer);',
  19265. ' end;',
  19266. ' TObject = class',
  19267. ' procedure DoIt(i: integer);',
  19268. ' end;',
  19269. ' TBird = class(IBird)',
  19270. ' procedure DoIt(i: integer); virtual; reintroduce;',
  19271. ' end;',
  19272. 'procedure TObject.DoIt(i: integer); begin end;',
  19273. 'procedure TBird.DoIt(i: integer); begin end;',
  19274. 'begin',
  19275. '']);
  19276. ConvertProgram;
  19277. CheckSource('TestClassInterface_ImplReintroduce',
  19278. LinesToStr([ // statements
  19279. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  19280. 'rtl.createClass(this, "TObject", null, function () {',
  19281. ' this.$init = function () {',
  19282. ' };',
  19283. ' this.$final = function () {',
  19284. ' };',
  19285. ' this.DoIt = function (i) {',
  19286. ' };',
  19287. '});',
  19288. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19289. ' this.DoIt$1 = function (i) {',
  19290. ' };',
  19291. ' rtl.addIntf(this, $mod.IBird, {',
  19292. ' DoIt: "DoIt$1"',
  19293. ' });',
  19294. '});',
  19295. '']),
  19296. LinesToStr([ // $mod.$main
  19297. '']));
  19298. end;
  19299. procedure TTestModule.TestClassInterface_MethodResolution;
  19300. begin
  19301. StartProgram(false);
  19302. Add([
  19303. '{$interfaces corba}',
  19304. 'type',
  19305. ' IUnknown = interface',
  19306. ' procedure Walk(i: longint);',
  19307. ' end;',
  19308. ' IBird = interface(IUnknown)',
  19309. ' procedure Walk(b: boolean); overload;',
  19310. ' procedure Fly(s: string);',
  19311. ' end;',
  19312. ' TObject = class',
  19313. ' end;',
  19314. ' TBird = class(TObject,IBird)',
  19315. ' procedure IBird.Fly = Move;',
  19316. ' procedure IBird.Walk = Hop;',
  19317. ' procedure Hop(i: longint);',
  19318. ' procedure Move(s: string);',
  19319. ' procedure Hop(b: boolean);',
  19320. ' end;',
  19321. 'procedure TBird.Move(s: string); begin end;',
  19322. 'procedure TBird.Hop(i: longint); begin end;',
  19323. 'procedure TBird.Hop(b: boolean); begin end;',
  19324. 'var',
  19325. ' BirdIntf: IBird;',
  19326. 'begin',
  19327. ' BirdIntf.Walk(3);',
  19328. ' BirdIntf.Walk(true);',
  19329. ' BirdIntf.Fly(''abc'');',
  19330. '']);
  19331. ConvertProgram;
  19332. CheckSource('TestClassInterface_MethodResolution',
  19333. LinesToStr([ // statements
  19334. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  19335. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  19336. 'rtl.createClass(this, "TObject", null, function () {',
  19337. ' this.$init = function () {',
  19338. ' };',
  19339. ' this.$final = function () {',
  19340. ' };',
  19341. '});',
  19342. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19343. ' this.Hop = function (i) {',
  19344. ' };',
  19345. ' this.Move = function (s) {',
  19346. ' };',
  19347. ' this.Hop$1 = function (b) {',
  19348. ' };',
  19349. ' rtl.addIntf(this, $mod.IBird, {',
  19350. ' Walk$1: "Hop$1",',
  19351. ' Fly: "Move",',
  19352. ' Walk: "Hop"',
  19353. ' });',
  19354. '});',
  19355. 'this.BirdIntf = null;',
  19356. '']),
  19357. LinesToStr([ // $mod.$main
  19358. '$mod.BirdIntf.Walk(3);',
  19359. '$mod.BirdIntf.Walk$1(true);',
  19360. '$mod.BirdIntf.Fly("abc");',
  19361. '']));
  19362. end;
  19363. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  19364. begin
  19365. StartProgram(false);
  19366. Add([
  19367. '{$interfaces com}',
  19368. 'type',
  19369. ' IUnknown = interface',
  19370. ' function _AddRef: longint;',
  19371. ' procedure Walk;',
  19372. ' end;',
  19373. ' IBird = interface end;',
  19374. ' IDog = interface end;',
  19375. ' TObject = class(IBird,IDog)',
  19376. ' function _AddRef: longint; virtual; abstract;',
  19377. ' procedure Walk; virtual; abstract;',
  19378. ' end;',
  19379. ' TBird = class(IUnknown)',
  19380. ' end;',
  19381. 'begin',
  19382. '']);
  19383. ConvertProgram;
  19384. CheckSource('TestClassInterface_COM_AncestorLess',
  19385. LinesToStr([ // statements
  19386. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  19387. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  19388. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  19389. 'rtl.createClass(this, "TObject", null, function () {',
  19390. ' this.$init = function () {',
  19391. ' };',
  19392. ' this.$final = function () {',
  19393. ' };',
  19394. ' rtl.addIntf(this, $mod.IBird);',
  19395. ' rtl.addIntf(this, $mod.IDog);',
  19396. '});',
  19397. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19398. ' rtl.addIntf(this, $mod.IUnknown);',
  19399. ' rtl.addIntf(this, $mod.IBird);',
  19400. ' rtl.addIntf(this, $mod.IDog);',
  19401. '});',
  19402. '']),
  19403. LinesToStr([ // $mod.$main
  19404. '']));
  19405. end;
  19406. procedure TTestModule.TestClassInterface_MethodOverride;
  19407. begin
  19408. StartProgram(false);
  19409. Add([
  19410. '{$interfaces corba}',
  19411. 'type',
  19412. ' IUnknown = interface',
  19413. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  19414. ' procedure Go;',
  19415. ' end;',
  19416. ' TObject = class(IUnknown)',
  19417. ' procedure Go; virtual; abstract;',
  19418. ' end;',
  19419. ' TBird = class',
  19420. ' procedure Go; override;',
  19421. ' end;',
  19422. ' TCat = class(TObject)',
  19423. ' procedure Go; override;',
  19424. ' end;',
  19425. ' TDog = class(TObject, IUnknown)',
  19426. ' procedure Go; override;',
  19427. ' end;',
  19428. 'procedure TBird.Go; begin end;',
  19429. 'procedure TCat.Go; begin end;',
  19430. 'procedure TDog.Go; begin end;',
  19431. 'begin',
  19432. '']);
  19433. ConvertProgram;
  19434. CheckSource('TestClassInterface_MethodOverride',
  19435. LinesToStr([ // statements
  19436. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  19437. 'rtl.createClass(this, "TObject", null, function () {',
  19438. ' this.$init = function () {',
  19439. ' };',
  19440. ' this.$final = function () {',
  19441. ' };',
  19442. ' rtl.addIntf(this, $mod.IUnknown);',
  19443. '});',
  19444. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19445. ' this.Go = function () {',
  19446. ' };',
  19447. ' rtl.addIntf(this, $mod.IUnknown);',
  19448. '});',
  19449. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19450. ' this.Go = function () {',
  19451. ' };',
  19452. ' rtl.addIntf(this, $mod.IUnknown);',
  19453. '});',
  19454. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19455. ' this.Go = function () {',
  19456. ' };',
  19457. ' rtl.addIntf(this, $mod.IUnknown);',
  19458. '});',
  19459. '']),
  19460. LinesToStr([ // $mod.$main
  19461. '']));
  19462. end;
  19463. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19464. begin
  19465. StartProgram(false);
  19466. Add([
  19467. '{$interfaces corba}',
  19468. 'type',
  19469. ' IUnknown = interface',
  19470. ' end;',
  19471. ' IBird = interface(IUnknown)',
  19472. ' procedure Fly(s: string);',
  19473. ' end;',
  19474. ' IEagle = interface(IBird)',
  19475. ' end;',
  19476. ' IDove = interface(IBird)',
  19477. ' end;',
  19478. ' ISwallow = interface(IBird)',
  19479. ' end;',
  19480. ' TObject = class',
  19481. ' end;',
  19482. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19483. ' procedure Fly(s: string); virtual; abstract;',
  19484. ' end;',
  19485. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19486. ' FBirdIntf: IBird;',
  19487. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19488. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19489. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19490. ' FDoveObj: TBird;',
  19491. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19492. ' function GetSwallowObj: TBird; virtual; abstract;',
  19493. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19494. ' end;',
  19495. 'begin',
  19496. '']);
  19497. ConvertProgram;
  19498. CheckSource('TestClassInterface_Corba_Delegation',
  19499. LinesToStr([ // statements
  19500. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19501. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19502. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19503. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19504. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19505. 'rtl.createClass(this, "TObject", null, function () {',
  19506. ' this.$init = function () {',
  19507. ' };',
  19508. ' this.$final = function () {',
  19509. ' };',
  19510. '});',
  19511. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19512. ' rtl.addIntf(this, $mod.IBird);',
  19513. ' rtl.addIntf(this, $mod.IEagle);',
  19514. ' rtl.addIntf(this, $mod.IDove);',
  19515. ' rtl.addIntf(this, $mod.ISwallow);',
  19516. '});',
  19517. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19518. ' this.$init = function () {',
  19519. ' $mod.TObject.$init.call(this);',
  19520. ' this.FBirdIntf = null;',
  19521. ' this.FDoveObj = null;',
  19522. ' };',
  19523. ' this.$final = function () {',
  19524. ' this.FBirdIntf = undefined;',
  19525. ' this.FDoveObj = undefined;',
  19526. ' $mod.TObject.$final.call(this);',
  19527. ' };',
  19528. ' this.$intfmaps = {',
  19529. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19530. ' return this.FBirdIntf;',
  19531. ' },',
  19532. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19533. ' return this.GetEagleIntf();',
  19534. ' },',
  19535. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19536. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19537. ' },',
  19538. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19539. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19540. ' }',
  19541. ' };',
  19542. '});',
  19543. '']),
  19544. LinesToStr([ // $mod.$main
  19545. '']));
  19546. end;
  19547. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19548. begin
  19549. StartProgram(false);
  19550. Add([
  19551. '{$interfaces corba}',
  19552. 'type',
  19553. ' IUnknown = interface',
  19554. ' end;',
  19555. ' IBird = interface(IUnknown)',
  19556. ' procedure Fly(s: string);',
  19557. ' end;',
  19558. ' IEagle = interface(IBird)',
  19559. ' end;',
  19560. ' IDove = interface(IBird)',
  19561. ' end;',
  19562. ' ISwallow = interface(IBird)',
  19563. ' end;',
  19564. ' TObject = class',
  19565. ' end;',
  19566. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19567. ' procedure Fly(s: string); virtual; abstract;',
  19568. ' end;',
  19569. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19570. ' private',
  19571. ' class var FBirdIntf: IBird;',
  19572. ' class var FDoveObj: TBird;',
  19573. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19574. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19575. ' protected',
  19576. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19577. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19578. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19579. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19580. ' end;',
  19581. 'begin',
  19582. '']);
  19583. ConvertProgram;
  19584. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19585. LinesToStr([ // statements
  19586. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19587. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19588. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19589. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19590. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19591. 'rtl.createClass(this, "TObject", null, function () {',
  19592. ' this.$init = function () {',
  19593. ' };',
  19594. ' this.$final = function () {',
  19595. ' };',
  19596. '});',
  19597. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19598. ' rtl.addIntf(this, $mod.IBird);',
  19599. ' rtl.addIntf(this, $mod.IEagle);',
  19600. ' rtl.addIntf(this, $mod.IDove);',
  19601. ' rtl.addIntf(this, $mod.ISwallow);',
  19602. '});',
  19603. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19604. ' this.FBirdIntf = null;',
  19605. ' this.FDoveObj = null;',
  19606. ' this.$intfmaps = {',
  19607. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19608. ' return this.FBirdIntf;',
  19609. ' },',
  19610. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19611. ' return this.GetEagleIntf();',
  19612. ' },',
  19613. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19614. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19615. ' },',
  19616. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19617. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19618. ' }',
  19619. ' };',
  19620. '});',
  19621. '']),
  19622. LinesToStr([ // $mod.$main
  19623. '']));
  19624. end;
  19625. procedure TTestModule.TestClassInterface_Corba_Operators;
  19626. begin
  19627. StartProgram(false);
  19628. Add([
  19629. '{$interfaces corba}',
  19630. 'type',
  19631. ' IUnknown = interface',
  19632. ' end;',
  19633. ' IBird = interface(IUnknown)',
  19634. ' function GetItems(Index: longint): longint;',
  19635. ' procedure SetItems(Index: longint; Value: longint);',
  19636. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19637. ' end;',
  19638. ' TObject = class',
  19639. ' end;',
  19640. ' TBird = class(TObject,IBird)',
  19641. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19642. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19643. ' end;',
  19644. 'var',
  19645. ' IntfVar: IBird = nil;',
  19646. ' IntfVar2: IBird;',
  19647. ' ObjVar: TBird;',
  19648. ' v: JSValue;',
  19649. 'begin',
  19650. ' IntfVar:=nil;',
  19651. ' IntfVar[3]:=IntfVar[4];',
  19652. ' if Assigned(IntfVar) then ;',
  19653. ' IntfVar:=IntfVar2;',
  19654. ' IntfVar:=ObjVar;',
  19655. ' if IntfVar=IntfVar2 then ;',
  19656. ' if IntfVar<>IntfVar2 then ;',
  19657. ' if IntfVar is IBird then ;',
  19658. ' if IntfVar is TBird then ;',
  19659. ' if ObjVar is IBird then ;',
  19660. ' IntfVar:=IntfVar2 as IBird;',
  19661. ' ObjVar:=IntfVar2 as TBird;',
  19662. ' IntfVar:=ObjVar as IBird;',
  19663. ' IntfVar:=IBird(IntfVar2);',
  19664. ' ObjVar:=TBird(IntfVar);',
  19665. ' IntfVar:=IBird(ObjVar);',
  19666. ' v:=IntfVar;',
  19667. ' IntfVar:=IBird(v);',
  19668. ' if v is IBird then ;',
  19669. ' v:=JSValue(IntfVar);',
  19670. ' v:=IBird;',
  19671. '']);
  19672. ConvertProgram;
  19673. CheckSource('TestClassInterface_Corba_Operators',
  19674. LinesToStr([ // statements
  19675. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19676. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19677. 'rtl.createClass(this, "TObject", null, function () {',
  19678. ' this.$init = function () {',
  19679. ' };',
  19680. ' this.$final = function () {',
  19681. ' };',
  19682. '});',
  19683. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19684. ' rtl.addIntf(this, $mod.IBird);',
  19685. '});',
  19686. 'this.IntfVar = null;',
  19687. 'this.IntfVar2 = null;',
  19688. 'this.ObjVar = null;',
  19689. 'this.v = undefined;',
  19690. '']),
  19691. LinesToStr([ // $mod.$main
  19692. '$mod.IntfVar = null;',
  19693. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19694. 'if ($mod.IntfVar != null) ;',
  19695. '$mod.IntfVar = $mod.IntfVar2;',
  19696. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19697. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19698. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19699. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19700. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19701. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19702. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19703. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19704. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19705. '$mod.IntfVar = $mod.IntfVar2;',
  19706. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19707. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19708. '$mod.v = $mod.IntfVar;',
  19709. '$mod.IntfVar = rtl.getObject($mod.v);',
  19710. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19711. '$mod.v = $mod.IntfVar;',
  19712. '$mod.v = $mod.IBird;',
  19713. '']));
  19714. end;
  19715. procedure TTestModule.TestClassInterface_Corba_Args;
  19716. begin
  19717. StartProgram(false);
  19718. Add([
  19719. '{$interfaces corba}',
  19720. 'type',
  19721. ' IUnknown = interface',
  19722. ' end;',
  19723. ' IBird = interface(IUnknown)',
  19724. ' end;',
  19725. ' TObject = class',
  19726. ' end;',
  19727. ' TBird = class(TObject,IBird)',
  19728. ' end;',
  19729. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19730. 'begin',
  19731. ' DoIt(i,i,i);',
  19732. 'end;',
  19733. 'procedure Change(var i: IBird; out j: IBird);',
  19734. 'begin',
  19735. ' DoIt(i,i,i);',
  19736. ' Change(i,i);',
  19737. 'end;',
  19738. 'var',
  19739. ' i: IBird;',
  19740. ' o: TBird;',
  19741. 'begin',
  19742. ' DoIt(i,i,i);',
  19743. ' Change(i,i);',
  19744. ' DoIt(o,o,o);',
  19745. '']);
  19746. ConvertProgram;
  19747. CheckSource('TestClassInterface_Corba_Args',
  19748. LinesToStr([ // statements
  19749. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19750. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19751. 'rtl.createClass(this, "TObject", null, function () {',
  19752. ' this.$init = function () {',
  19753. ' };',
  19754. ' this.$final = function () {',
  19755. ' };',
  19756. '});',
  19757. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19758. ' rtl.addIntf(this, $mod.IBird);',
  19759. '});',
  19760. 'this.DoIt = function (u, i, j) {',
  19761. ' $mod.DoIt({',
  19762. ' get: function () {',
  19763. ' return i;',
  19764. ' },',
  19765. ' set: function (v) {',
  19766. ' i = v;',
  19767. ' }',
  19768. ' }, i, i);',
  19769. '};',
  19770. 'this.Change = function (i, j) {',
  19771. ' $mod.DoIt(i, i.get(), i.get());',
  19772. ' $mod.Change(i, i);',
  19773. '};',
  19774. 'this.i = null;',
  19775. 'this.o = null;',
  19776. '']),
  19777. LinesToStr([ // $mod.$main
  19778. '$mod.DoIt({',
  19779. ' p: $mod,',
  19780. ' get: function () {',
  19781. ' return this.p.i;',
  19782. ' },',
  19783. ' set: function (v) {',
  19784. ' this.p.i = v;',
  19785. ' }',
  19786. '}, $mod.i, $mod.i);',
  19787. '$mod.Change({',
  19788. ' p: $mod,',
  19789. ' get: function () {',
  19790. ' return this.p.i;',
  19791. ' },',
  19792. ' set: function (v) {',
  19793. ' this.p.i = v;',
  19794. ' }',
  19795. '}, {',
  19796. ' p: $mod,',
  19797. ' get: function () {',
  19798. ' return this.p.i;',
  19799. ' },',
  19800. ' set: function (v) {',
  19801. ' this.p.i = v;',
  19802. ' }',
  19803. '});',
  19804. '$mod.DoIt({',
  19805. ' p: $mod,',
  19806. ' get: function () {',
  19807. ' return this.p.o;',
  19808. ' },',
  19809. ' set: function (v) {',
  19810. ' this.p.o = v;',
  19811. ' }',
  19812. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19813. '']));
  19814. end;
  19815. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19816. begin
  19817. StartProgram(false);
  19818. Add([
  19819. '{$interfaces corba}',
  19820. 'type',
  19821. ' IUnknown = interface end;',
  19822. ' TObject = class',
  19823. ' Id: longint;',
  19824. ' end;',
  19825. ' IEnumerator = interface(IUnknown)',
  19826. ' function GetCurrent: TObject;',
  19827. ' function MoveNext: Boolean;',
  19828. ' property Current: TObject read GetCurrent;',
  19829. ' end;',
  19830. ' IEnumerable = interface(IUnknown)',
  19831. ' function GetEnumerator: IEnumerator;',
  19832. ' end;',
  19833. 'var',
  19834. ' o: TObject;',
  19835. ' i: IEnumerable;',
  19836. 'begin',
  19837. ' for o in i do o.Id:=3;',
  19838. '']);
  19839. ConvertProgram;
  19840. CheckSource('TestClassInterface_Corba_ForIn',
  19841. LinesToStr([ // statements
  19842. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19843. 'rtl.createClass(this, "TObject", null, function () {',
  19844. ' this.$init = function () {',
  19845. ' this.Id = 0;',
  19846. ' };',
  19847. ' this.$final = function () {',
  19848. ' };',
  19849. '});',
  19850. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19851. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19852. 'this.o = null;',
  19853. 'this.i = null;',
  19854. '']),
  19855. LinesToStr([ // $mod.$main
  19856. 'var $in = $mod.i.GetEnumerator();',
  19857. 'while ($in.MoveNext()) {',
  19858. ' $mod.o = $in.GetCurrent();',
  19859. ' $mod.o.Id = 3;',
  19860. '};',
  19861. '']));
  19862. end;
  19863. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19864. begin
  19865. StartProgram(false);
  19866. Add([
  19867. '{$interfaces com}',
  19868. 'type',
  19869. ' IUnknown = interface',
  19870. ' function _AddRef: longint;',
  19871. ' function _Release: longint;',
  19872. ' end;',
  19873. ' TObject = class(IUnknown)',
  19874. ' function _AddRef: longint; virtual; abstract;',
  19875. ' function _Release: longint; virtual; abstract;',
  19876. ' end;',
  19877. 'var',
  19878. ' i: IUnknown;',
  19879. 'procedure DoGlobal(o: TObject);',
  19880. 'begin',
  19881. ' i:=nil;',
  19882. ' i:=o;',
  19883. ' i:=i;',
  19884. 'end;',
  19885. 'procedure DoLocal(o: TObject);',
  19886. 'const k: IUnknown = nil;',
  19887. 'var j: IUnknown;',
  19888. 'begin',
  19889. ' k:=o;',
  19890. ' k:=i;',
  19891. ' j:=o;',
  19892. ' j:=i;',
  19893. 'end;',
  19894. 'var o: TObject;',
  19895. 'begin',
  19896. ' i:=nil;',
  19897. ' i:=o;',
  19898. '']);
  19899. ConvertProgram;
  19900. CheckSource('TestClassInterface_COM_AssignVar',
  19901. LinesToStr([ // statements
  19902. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19903. 'rtl.createClass(this, "TObject", null, function () {',
  19904. ' this.$init = function () {',
  19905. ' };',
  19906. ' this.$final = function () {',
  19907. ' };',
  19908. ' rtl.addIntf(this, $mod.IUnknown);',
  19909. '});',
  19910. 'this.i = null;',
  19911. 'this.DoGlobal = function (o) {',
  19912. ' rtl.setIntfP($mod, "i", null);',
  19913. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19914. ' rtl.setIntfP($mod, "i", $mod.i);',
  19915. '};',
  19916. 'var k = null;',
  19917. 'this.DoLocal = function (o) {',
  19918. ' var j = null;',
  19919. ' try{',
  19920. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19921. ' k = rtl.setIntfL(k, $mod.i);',
  19922. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19923. ' j = rtl.setIntfL(j, $mod.i);',
  19924. ' }finally{',
  19925. ' rtl._Release(j);',
  19926. ' };',
  19927. '};',
  19928. 'this.o = null;',
  19929. '']),
  19930. LinesToStr([ // $mod.$main
  19931. 'rtl.setIntfP($mod, "i", null);',
  19932. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19933. '']));
  19934. end;
  19935. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19936. begin
  19937. StartProgram(false);
  19938. Add([
  19939. '{$interfaces com}',
  19940. 'type',
  19941. ' IUnknown = interface',
  19942. ' function _AddRef: longint;',
  19943. ' function _Release: longint;',
  19944. ' end;',
  19945. ' TObject = class(IUnknown)',
  19946. ' function _AddRef: longint; virtual; abstract;',
  19947. ' function _Release: longint; virtual; abstract;',
  19948. ' end;',
  19949. 'procedure DoDefault(i, j: IUnknown);',
  19950. 'begin',
  19951. ' i:=nil;',
  19952. ' i:=j;',
  19953. 'end;',
  19954. 'begin',
  19955. '']);
  19956. ConvertProgram;
  19957. CheckSource('TestClassInterface_COM_AssignArg',
  19958. LinesToStr([ // statements
  19959. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19960. 'rtl.createClass(this, "TObject", null, function () {',
  19961. ' this.$init = function () {',
  19962. ' };',
  19963. ' this.$final = function () {',
  19964. ' };',
  19965. ' rtl.addIntf(this, $mod.IUnknown);',
  19966. '});',
  19967. 'this.DoDefault = function (i, j) {',
  19968. ' rtl._AddRef(i);',
  19969. ' try {',
  19970. ' i = rtl.setIntfL(i, null);',
  19971. ' i = rtl.setIntfL(i, j);',
  19972. ' } finally {',
  19973. ' rtl._Release(i);',
  19974. ' };',
  19975. '};',
  19976. '']),
  19977. LinesToStr([ // $mod.$main
  19978. '']));
  19979. end;
  19980. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19981. begin
  19982. StartProgram(false);
  19983. Add([
  19984. '{$interfaces com}',
  19985. 'type',
  19986. ' IUnknown = interface',
  19987. ' function _AddRef: longint;',
  19988. ' function _Release: longint;',
  19989. ' end;',
  19990. ' TObject = class(IUnknown)',
  19991. ' function _AddRef: longint; virtual; abstract;',
  19992. ' function _Release: longint; virtual; abstract;',
  19993. ' end;',
  19994. 'function DoDefault(i: IUnknown): IUnknown;',
  19995. 'begin',
  19996. ' Result:=i;',
  19997. ' if Result<>nil then exit;',
  19998. 'end;',
  19999. 'begin',
  20000. '']);
  20001. ConvertProgram;
  20002. CheckSource('TestClassInterface_COM_FunctionResult',
  20003. LinesToStr([ // statements
  20004. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20005. 'rtl.createClass(this, "TObject", null, function () {',
  20006. ' this.$init = function () {',
  20007. ' };',
  20008. ' this.$final = function () {',
  20009. ' };',
  20010. ' rtl.addIntf(this, $mod.IUnknown);',
  20011. '});',
  20012. 'this.DoDefault = function (i) {',
  20013. ' var Result = null;',
  20014. ' var $ok = false;',
  20015. ' try {',
  20016. ' Result = rtl.setIntfL(Result, i);',
  20017. ' if(Result !== null){',
  20018. ' $ok = true;',
  20019. ' return Result;',
  20020. ' };',
  20021. ' $ok = true;',
  20022. ' } finally {',
  20023. ' if(!$ok) rtl._Release(Result);',
  20024. ' };',
  20025. ' return Result;',
  20026. '};',
  20027. '']),
  20028. LinesToStr([ // $mod.$main
  20029. '']));
  20030. end;
  20031. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  20032. begin
  20033. StartProgram(false);
  20034. Add([
  20035. '{$interfaces com}',
  20036. 'type',
  20037. ' IUnknown = interface',
  20038. ' function _AddRef: longint;',
  20039. ' function _Release: longint;',
  20040. ' end;',
  20041. ' TObject = class(IUnknown)',
  20042. ' function _AddRef: longint; virtual; abstract;',
  20043. ' function _Release: longint; virtual; abstract;',
  20044. ' function GetIntf: IUnknown; virtual;',
  20045. ' end;',
  20046. ' TMouse = class',
  20047. ' function GetIntf: IUnknown; override;',
  20048. ' end;',
  20049. 'function TObject.GetIntf: IUnknown; begin end;',
  20050. 'function TMouse.GetIntf: IUnknown;',
  20051. 'var i: IUnknown;',
  20052. 'begin',
  20053. ' inherited;',
  20054. ' inherited GetIntf;',
  20055. ' inherited GetIntf();',
  20056. ' Result:=inherited GetIntf;',
  20057. ' Result:=inherited GetIntf();',
  20058. ' i:=inherited GetIntf;',
  20059. ' i:=inherited GetIntf();',
  20060. 'end;',
  20061. 'begin',
  20062. '']);
  20063. ConvertProgram;
  20064. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  20065. LinesToStr([ // statements
  20066. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20067. 'rtl.createClass(this, "TObject", null, function () {',
  20068. ' this.$init = function () {',
  20069. ' };',
  20070. ' this.$final = function () {',
  20071. ' };',
  20072. ' this.GetIntf = function () {',
  20073. ' var Result = null;',
  20074. ' return Result;',
  20075. ' };',
  20076. ' rtl.addIntf(this, $mod.IUnknown);',
  20077. '});',
  20078. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  20079. ' this.GetIntf = function () {',
  20080. ' var Result = null;',
  20081. ' var i = null;',
  20082. ' var $ir = rtl.createIntfRefs();',
  20083. ' var $ok = false;',
  20084. ' try {',
  20085. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  20086. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  20087. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  20088. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20089. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20090. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20091. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20092. ' $ok = true;',
  20093. ' } finally {',
  20094. ' $ir.free();',
  20095. ' rtl._Release(i);',
  20096. ' if (!$ok) rtl._Release(Result);',
  20097. ' };',
  20098. ' return Result;',
  20099. ' };',
  20100. ' rtl.addIntf(this, $mod.IUnknown);',
  20101. '});',
  20102. '']),
  20103. LinesToStr([ // $mod.$main
  20104. '']));
  20105. end;
  20106. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  20107. begin
  20108. StartProgram(false);
  20109. Add([
  20110. '{$interfaces com}',
  20111. 'type',
  20112. ' IUnknown = interface',
  20113. ' function _AddRef: longint;',
  20114. ' function _Release: longint;',
  20115. ' end;',
  20116. ' TObject = class(IUnknown)',
  20117. ' function _AddRef: longint; virtual; abstract;',
  20118. ' function _Release: longint; virtual; abstract;',
  20119. ' end;',
  20120. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  20121. 'begin',
  20122. ' if i is IUnknown then ;',
  20123. ' if o is IUnknown then ;',
  20124. ' if i is TObject then ;',
  20125. ' i:=j as IUnknown;',
  20126. ' i:=o as IUnknown;',
  20127. ' o:=j as TObject;',
  20128. ' i:=IUnknown(j);',
  20129. ' i:=IUnknown(o);',
  20130. ' o:=TObject(i);',
  20131. 'end;',
  20132. 'begin',
  20133. '']);
  20134. ConvertProgram;
  20135. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  20136. LinesToStr([ // statements
  20137. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20138. 'rtl.createClass(this, "TObject", null, function () {',
  20139. ' this.$init = function () {',
  20140. ' };',
  20141. ' this.$final = function () {',
  20142. ' };',
  20143. ' rtl.addIntf(this, $mod.IUnknown);',
  20144. '});',
  20145. 'this.DoDefault = function (i, j, o) {',
  20146. ' rtl._AddRef(i);',
  20147. ' try {',
  20148. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  20149. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  20150. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  20151. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  20152. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20153. ' o = rtl.intfAsClass(j, $mod.TObject);',
  20154. ' i = rtl.setIntfL(i, j);',
  20155. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20156. ' o = rtl.intfToClass(i, $mod.TObject);',
  20157. ' } finally {',
  20158. ' rtl._Release(i);',
  20159. ' };',
  20160. '};',
  20161. '']),
  20162. LinesToStr([ // $mod.$main
  20163. '']));
  20164. end;
  20165. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  20166. begin
  20167. StartProgram(false);
  20168. Add([
  20169. '{$interfaces com}',
  20170. 'type',
  20171. ' IUnknown = interface',
  20172. ' function _AddRef: longint;',
  20173. ' function _Release: longint;',
  20174. ' end;',
  20175. ' TObject = class(IUnknown)',
  20176. ' function _AddRef: longint; virtual; abstract;',
  20177. ' function _Release: longint; virtual; abstract;',
  20178. ' end;',
  20179. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  20180. 'var o: TObject;',
  20181. 'begin',
  20182. ' DoIt(v,v,v,v);',
  20183. ' DoIt(o,o,k,k);',
  20184. 'end;',
  20185. 'procedure DoSome;',
  20186. 'var v: IUnknown;',
  20187. 'begin',
  20188. ' DoIt(v,v,v,v);',
  20189. 'end;',
  20190. 'var i: IUnknown;',
  20191. 'begin',
  20192. ' DoIt(i,i,i,i);',
  20193. '']);
  20194. ConvertProgram;
  20195. CheckSource('TestClassInterface_COM_PassAsArg',
  20196. LinesToStr([ // statements
  20197. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20198. 'rtl.createClass(this, "TObject", null, function () {',
  20199. ' this.$init = function () {',
  20200. ' };',
  20201. ' this.$final = function () {',
  20202. ' };',
  20203. ' rtl.addIntf(this, $mod.IUnknown);',
  20204. '});',
  20205. 'this.DoIt = function (v, j, k, l) {',
  20206. ' var o = null;',
  20207. ' var $ir = rtl.createIntfRefs();',
  20208. ' rtl._AddRef(v);',
  20209. ' try {',
  20210. ' $mod.DoIt(v, v, {',
  20211. ' get: function () {',
  20212. ' return v;',
  20213. ' },',
  20214. ' set: function (w) {',
  20215. ' v = rtl.setIntfL(v, w);',
  20216. ' }',
  20217. ' }, {',
  20218. ' get: function () {',
  20219. ' return v;',
  20220. ' },',
  20221. ' set: function (w) {',
  20222. ' v = rtl.setIntfL(v, w);',
  20223. ' }',
  20224. ' });',
  20225. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  20226. ' } finally {',
  20227. ' $ir.free();',
  20228. ' rtl._Release(v);',
  20229. ' };',
  20230. '};',
  20231. 'this.DoSome = function () {',
  20232. ' var v = null;',
  20233. ' try {',
  20234. ' $mod.DoIt(v, v, {',
  20235. ' get: function () {',
  20236. ' return v;',
  20237. ' },',
  20238. ' set: function (w) {',
  20239. ' v = rtl.setIntfL(v, w);',
  20240. ' }',
  20241. ' }, {',
  20242. ' get: function () {',
  20243. ' return v;',
  20244. ' },',
  20245. ' set: function (w) {',
  20246. ' v = rtl.setIntfL(v, w);',
  20247. ' }',
  20248. ' });',
  20249. ' } finally {',
  20250. ' rtl._Release(v);',
  20251. ' };',
  20252. '};',
  20253. 'this.i = null;',
  20254. '']),
  20255. LinesToStr([ // $mod.$main
  20256. '$mod.DoIt($mod.i, $mod.i, {',
  20257. ' p: $mod,',
  20258. ' get: function () {',
  20259. ' return this.p.i;',
  20260. ' },',
  20261. ' set: function (v) {',
  20262. ' rtl.setIntfP(this.p, "i", v);',
  20263. ' }',
  20264. '}, {',
  20265. ' p: $mod,',
  20266. ' get: function () {',
  20267. ' return this.p.i;',
  20268. ' },',
  20269. ' set: function (v) {',
  20270. ' rtl.setIntfP(this.p, "i", v);',
  20271. ' }',
  20272. '});',
  20273. '']));
  20274. end;
  20275. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  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(IUnknown)',
  20286. ' function _AddRef: longint; virtual; abstract;',
  20287. ' function _Release: longint; virtual; abstract;',
  20288. ' end;',
  20289. 'procedure DoIt(out i);',
  20290. 'begin end;',
  20291. 'procedure DoSome;',
  20292. 'var v: IUnknown;',
  20293. 'begin',
  20294. ' DoIt(v);',
  20295. 'end;',
  20296. 'function GetIt: IUnknown;',
  20297. 'begin',
  20298. ' DoIt(Result);',
  20299. 'end;',
  20300. 'var i: IUnknown;',
  20301. 'begin',
  20302. ' DoIt(i);',
  20303. '']);
  20304. ConvertProgram;
  20305. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  20306. LinesToStr([ // statements
  20307. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20308. 'rtl.createClass(this, "TObject", null, function () {',
  20309. ' this.$init = function () {',
  20310. ' };',
  20311. ' this.$final = function () {',
  20312. ' };',
  20313. ' rtl.addIntf(this, $mod.IUnknown);',
  20314. '});',
  20315. 'this.DoIt = function (i) {',
  20316. '};',
  20317. 'this.DoSome = function () {',
  20318. ' var v = null;',
  20319. ' try {',
  20320. ' $mod.DoIt({',
  20321. ' get: function () {',
  20322. ' return v;',
  20323. ' },',
  20324. ' set: function (w) {',
  20325. ' v = w;',
  20326. ' }',
  20327. ' });',
  20328. ' } finally {',
  20329. ' rtl._Release(v);',
  20330. ' };',
  20331. '};',
  20332. 'this.GetIt = function () {',
  20333. ' var Result = null;',
  20334. ' var $ok = false;',
  20335. ' try {',
  20336. ' $mod.DoIt({',
  20337. ' get: function () {',
  20338. ' return Result;',
  20339. ' },',
  20340. ' set: function (v) {',
  20341. ' Result = v;',
  20342. ' }',
  20343. ' });',
  20344. ' $ok = true;',
  20345. ' } finally {',
  20346. ' if (!$ok) rtl._Release(Result);',
  20347. ' };',
  20348. ' return Result;',
  20349. '};',
  20350. 'this.i = null;',
  20351. '']),
  20352. LinesToStr([ // $mod.$main
  20353. 'try {',
  20354. ' $mod.DoIt({',
  20355. ' p: $mod,',
  20356. ' get: function () {',
  20357. ' return this.p.i;',
  20358. ' },',
  20359. ' set: function (v) {',
  20360. ' this.p.i = v;',
  20361. ' }',
  20362. ' });',
  20363. '} finally {',
  20364. ' rtl._Release($mod.i);',
  20365. '};',
  20366. '']));
  20367. end;
  20368. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  20369. begin
  20370. StartProgram(false);
  20371. Add([
  20372. '{$interfaces com}',
  20373. 'type',
  20374. ' IUnknown = interface',
  20375. ' function _AddRef: longint;',
  20376. ' function _Release: longint;',
  20377. ' end;',
  20378. ' TObject = class(IUnknown)',
  20379. ' function _AddRef: longint; virtual; abstract;',
  20380. ' function _Release: longint; virtual; abstract;',
  20381. ' end;',
  20382. 'function GetIt: IUnknown;',
  20383. 'begin',
  20384. 'end;',
  20385. 'procedure DoSome;',
  20386. 'var v: IUnknown;',
  20387. ' i: longint;',
  20388. 'begin',
  20389. ' v:=GetIt;',
  20390. ' v:=GetIt();',
  20391. ' GetIt()._AddRef;',
  20392. ' i:=GetIt()._AddRef;',
  20393. 'end;',
  20394. 'var v: IUnknown;',
  20395. ' i: longint;',
  20396. 'begin',
  20397. ' v:=GetIt;',
  20398. ' v:=GetIt();',
  20399. ' GetIt()._AddRef;',
  20400. ' i:=GetIt()._AddRef;',
  20401. '']);
  20402. ConvertProgram;
  20403. CheckSource('TestClassInterface_COM_FunctionInExpr',
  20404. LinesToStr([ // statements
  20405. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20406. 'rtl.createClass(this, "TObject", null, function () {',
  20407. ' this.$init = function () {',
  20408. ' };',
  20409. ' this.$final = function () {',
  20410. ' };',
  20411. ' rtl.addIntf(this, $mod.IUnknown);',
  20412. '});',
  20413. 'this.GetIt = function () {',
  20414. ' var Result = null;',
  20415. ' return Result;',
  20416. '};',
  20417. 'this.DoSome = function () {',
  20418. ' var v = null;',
  20419. ' var i = 0;',
  20420. ' var $ir = rtl.createIntfRefs();',
  20421. ' try {',
  20422. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20423. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20424. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20425. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20426. ' } finally {',
  20427. ' $ir.free();',
  20428. ' rtl._Release(v);',
  20429. ' };',
  20430. '};',
  20431. 'this.v = null;',
  20432. 'this.i = 0;',
  20433. '']),
  20434. LinesToStr([ // $mod.$main
  20435. 'var $ir = rtl.createIntfRefs();',
  20436. 'try {',
  20437. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20438. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20439. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20440. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20441. '} finally {',
  20442. ' $ir.free();',
  20443. '};',
  20444. '']));
  20445. end;
  20446. procedure TTestModule.TestClassInterface_COM_Property;
  20447. begin
  20448. StartProgram(false);
  20449. Add([
  20450. '{$interfaces com}',
  20451. 'type',
  20452. ' IUnknown = interface',
  20453. ' function _AddRef: longint;',
  20454. ' function _Release: longint;',
  20455. ' end;',
  20456. ' TObject = class(IUnknown)',
  20457. ' FAnt: IUnknown;',
  20458. ' function _AddRef: longint; virtual; abstract;',
  20459. ' function _Release: longint; virtual; abstract;',
  20460. ' function GetBird: IUnknown; virtual; abstract;',
  20461. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20462. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20463. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20464. ' property Ant: IUnknown read FAnt write FAnt;',
  20465. ' property Bird: IUnknown read GetBird write SetBird;',
  20466. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20467. ' end;',
  20468. 'procedure DoIt;',
  20469. 'var',
  20470. ' o: TObject;',
  20471. ' v: IUnknown;',
  20472. 'begin',
  20473. ' v:=o.Ant;',
  20474. ' o.Ant:=v;',
  20475. ' o.Ant:=o.Ant;',
  20476. ' v:=o.Bird;',
  20477. ' o.Bird:=v;',
  20478. ' o.Bird:=o.Bird;',
  20479. ' v:=o.Items[1];',
  20480. ' o.Items[2]:=v;',
  20481. ' o.Items[3]:=o.Items[4];',
  20482. ' v:=o[5];',
  20483. ' o[6]:=v;',
  20484. ' o[7]:=o[8];',
  20485. 'end;',
  20486. 'begin',
  20487. '']);
  20488. ConvertProgram;
  20489. CheckSource('TestClassInterface_COM_Property',
  20490. LinesToStr([ // statements
  20491. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20492. 'rtl.createClass(this, "TObject", null, function () {',
  20493. ' this.$init = function () {',
  20494. ' this.FAnt = null;',
  20495. ' };',
  20496. ' this.$final = function () {',
  20497. ' rtl.setIntfP(this, "FAnt", null);',
  20498. ' };',
  20499. ' rtl.addIntf(this, $mod.IUnknown);',
  20500. '});',
  20501. 'this.DoIt = function () {',
  20502. ' var o = null;',
  20503. ' var v = null;',
  20504. ' var $ir = rtl.createIntfRefs();',
  20505. ' try {',
  20506. ' v = rtl.setIntfL(v, o.FAnt);',
  20507. ' rtl.setIntfP(o, "FAnt", v);',
  20508. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20509. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20510. ' o.SetBird(v);',
  20511. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20512. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20513. ' o.SetItems(2, v);',
  20514. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20515. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20516. ' o.SetItems(6, v);',
  20517. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20518. ' } finally {',
  20519. ' $ir.free();',
  20520. ' rtl._Release(v);',
  20521. ' };',
  20522. '};',
  20523. '']),
  20524. LinesToStr([ // $mod.$main
  20525. '']));
  20526. end;
  20527. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20528. begin
  20529. StartProgram(false);
  20530. Add([
  20531. '{$interfaces com}',
  20532. 'type',
  20533. ' IUnknown = interface',
  20534. ' function _AddRef: longint;',
  20535. ' function _Release: longint;',
  20536. ' function GetBird: IUnknown;',
  20537. ' procedure SetBird(Value: IUnknown);',
  20538. ' function GetItems(Index: longint): IUnknown;',
  20539. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20540. ' property Bird: IUnknown read GetBird write SetBird;',
  20541. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20542. ' end;',
  20543. ' TObject = class(IUnknown)',
  20544. ' function _AddRef: longint; virtual; abstract;',
  20545. ' function _Release: longint; virtual; abstract;',
  20546. ' function GetBird: IUnknown; virtual; abstract;',
  20547. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20548. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20549. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20550. ' end;',
  20551. 'procedure DoIt;',
  20552. 'var',
  20553. ' o: TObject;',
  20554. ' v: IUnknown;',
  20555. 'begin',
  20556. ' v:=v.Items[1];',
  20557. ' v.Items[2]:=v;',
  20558. ' v.Items[3]:=v.Items[4];',
  20559. ' v:=v[5];',
  20560. ' v[6]:=v;',
  20561. ' v[7]:=v[8];',
  20562. ' v[9].Bird.Bird:=v;',
  20563. ' v:=v.Bird[10].Bird',
  20564. 'end;',
  20565. 'begin',
  20566. '']);
  20567. ConvertProgram;
  20568. CheckSource('TestClassInterface_COM_IntfProperty',
  20569. LinesToStr([ // statements
  20570. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20571. ' "_AddRef",',
  20572. ' "_Release",',
  20573. ' "GetBird",',
  20574. ' "SetBird",',
  20575. ' "GetItems",',
  20576. ' "SetItems"',
  20577. '], null);',
  20578. 'rtl.createClass(this, "TObject", null, function () {',
  20579. ' this.$init = function () {',
  20580. ' };',
  20581. ' this.$final = function () {',
  20582. ' };',
  20583. ' rtl.addIntf(this, $mod.IUnknown);',
  20584. '});',
  20585. 'this.DoIt = function () {',
  20586. ' var o = null;',
  20587. ' var v = null;',
  20588. ' var $ir = rtl.createIntfRefs();',
  20589. ' try {',
  20590. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20591. ' v.SetItems(2, v);',
  20592. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20593. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20594. ' v.SetItems(6, v);',
  20595. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20596. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20597. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20598. ' } finally {',
  20599. ' $ir.free();',
  20600. ' rtl._Release(v);',
  20601. ' };',
  20602. '};',
  20603. '']),
  20604. LinesToStr([ // $mod.$main
  20605. '']));
  20606. end;
  20607. procedure TTestModule.TestClassInterface_COM_Delegation;
  20608. begin
  20609. StartProgram(false);
  20610. Add([
  20611. '{$interfaces com}',
  20612. 'type',
  20613. ' IUnknown = interface',
  20614. ' function _AddRef: longint;',
  20615. ' function _Release: longint;',
  20616. ' end;',
  20617. ' IBird = interface(IUnknown)',
  20618. ' procedure Fly(s: string);',
  20619. ' end;',
  20620. ' IEagle = interface(IBird) end;',
  20621. ' IDove = interface(IBird) end;',
  20622. ' ISwallow = interface(IBird) end;',
  20623. ' TObject = class',
  20624. ' end;',
  20625. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20626. ' function _AddRef: longint; virtual; abstract;',
  20627. ' function _Release: longint; virtual; abstract;',
  20628. ' procedure Fly(s: string); virtual; abstract;',
  20629. ' end;',
  20630. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20631. ' function _AddRef: longint; virtual; abstract;',
  20632. ' function _Release: longint; virtual; abstract;',
  20633. ' FBirdIntf: IBird;',
  20634. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20635. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20636. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20637. ' FDoveObj: TBird;',
  20638. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20639. ' function GetSwallowObj: TBird; virtual; abstract;',
  20640. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20641. ' end;',
  20642. 'begin',
  20643. '']);
  20644. ConvertProgram;
  20645. CheckSource('TestClassInterface_COM_Delegation',
  20646. LinesToStr([ // statements
  20647. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20648. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20649. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20650. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20651. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20652. 'rtl.createClass(this, "TObject", null, function () {',
  20653. ' this.$init = function () {',
  20654. ' };',
  20655. ' this.$final = function () {',
  20656. ' };',
  20657. '});',
  20658. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20659. ' rtl.addIntf(this, $mod.IBird);',
  20660. ' rtl.addIntf(this, $mod.IEagle);',
  20661. ' rtl.addIntf(this, $mod.IDove);',
  20662. ' rtl.addIntf(this, $mod.ISwallow);',
  20663. '});',
  20664. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20665. ' this.$init = function () {',
  20666. ' $mod.TObject.$init.call(this);',
  20667. ' this.FBirdIntf = null;',
  20668. ' this.FDoveObj = null;',
  20669. ' };',
  20670. ' this.$final = function () {',
  20671. ' rtl.setIntfP(this, "FBirdIntf", null);',
  20672. ' this.FDoveObj = undefined;',
  20673. ' $mod.TObject.$final.call(this);',
  20674. ' };',
  20675. ' this.$intfmaps = {',
  20676. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20677. ' return rtl._AddRef(this.FBirdIntf);',
  20678. ' },',
  20679. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20680. ' return this.GetEagleIntf();',
  20681. ' },',
  20682. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20683. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20684. ' },',
  20685. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20686. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20687. ' }',
  20688. ' };',
  20689. '});',
  20690. '']),
  20691. LinesToStr([ // $mod.$main
  20692. '']));
  20693. end;
  20694. procedure TTestModule.TestClassInterface_COM_With;
  20695. begin
  20696. StartProgram(false);
  20697. Add([
  20698. '{$interfaces com}',
  20699. 'type',
  20700. ' IUnknown = interface',
  20701. ' function _AddRef: longint;',
  20702. ' function _Release: longint;',
  20703. ' function GetAnt: IUnknown;',
  20704. ' property Ant: IUnknown read GetAnt;',
  20705. ' end;',
  20706. ' TObject = class(IUnknown)',
  20707. ' function _AddRef: longint; virtual; abstract;',
  20708. ' function _Release: longint; virtual; abstract;',
  20709. ' function GetAnt: IUnknown; virtual; abstract;',
  20710. ' property Ant: IUnknown read GetAnt;',
  20711. ' end;',
  20712. 'procedure DoIt;',
  20713. 'var',
  20714. ' i: IUnknown;',
  20715. 'begin',
  20716. ' with i do ',
  20717. ' GetAnt;',
  20718. ' with i.Ant, Ant do ',
  20719. ' GetAnt;',
  20720. 'end;',
  20721. 'begin',
  20722. '']);
  20723. ConvertProgram;
  20724. CheckSource('TestClassInterface_COM_With',
  20725. LinesToStr([ // statements
  20726. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20727. 'rtl.createClass(this, "TObject", null, function () {',
  20728. ' this.$init = function () {',
  20729. ' };',
  20730. ' this.$final = function () {',
  20731. ' };',
  20732. ' rtl.addIntf(this, $mod.IUnknown);',
  20733. '});',
  20734. 'this.DoIt = function () {',
  20735. ' var i = null;',
  20736. ' var $ir = rtl.createIntfRefs();',
  20737. ' try {',
  20738. ' $ir.ref(1, i.GetAnt());',
  20739. ' var $with = $ir.ref(2, i.GetAnt());',
  20740. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20741. ' $ir.ref(4, $with1.GetAnt());',
  20742. ' } finally {',
  20743. ' $ir.free();',
  20744. ' };',
  20745. '};',
  20746. '']),
  20747. LinesToStr([ // $mod.$main
  20748. '']));
  20749. end;
  20750. procedure TTestModule.TestClassInterface_COM_ForIn;
  20751. begin
  20752. StartProgram(false);
  20753. Add([
  20754. '{$interfaces com}',
  20755. 'type',
  20756. ' IUnknown = interface end;',
  20757. ' TObject = class',
  20758. ' Id: longint;',
  20759. ' end;',
  20760. ' IEnumerator = interface(IUnknown)',
  20761. ' function GetCurrent: TObject;',
  20762. ' function MoveNext: Boolean;',
  20763. ' property Current: TObject read GetCurrent;',
  20764. ' end;',
  20765. ' IEnumerable = interface(IUnknown)',
  20766. ' function GetEnumerator: IEnumerator;',
  20767. ' end;',
  20768. 'var',
  20769. ' o: TObject;',
  20770. ' i: IEnumerable;',
  20771. 'begin',
  20772. ' for o in i do o.Id:=3;',
  20773. '']);
  20774. ConvertProgram;
  20775. CheckSource('TestClassInterface_COM_ForIn',
  20776. LinesToStr([ // statements
  20777. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20778. 'rtl.createClass(this, "TObject", null, function () {',
  20779. ' this.$init = function () {',
  20780. ' this.Id = 0;',
  20781. ' };',
  20782. ' this.$final = function () {',
  20783. ' };',
  20784. '});',
  20785. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20786. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20787. 'this.o = null;',
  20788. 'this.i = null;',
  20789. '']),
  20790. LinesToStr([ // $mod.$main
  20791. 'var $in = $mod.i.GetEnumerator();',
  20792. 'try {',
  20793. ' while ($in.MoveNext()) {',
  20794. ' $mod.o = $in.GetCurrent();',
  20795. ' $mod.o.Id = 3;',
  20796. ' }',
  20797. '} finally {',
  20798. ' rtl._Release($in)',
  20799. '};',
  20800. '']));
  20801. end;
  20802. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20803. begin
  20804. StartProgram(false);
  20805. Add([
  20806. '{$interfaces com}',
  20807. 'type',
  20808. ' IUnknown = interface',
  20809. ' function _AddRef: longint;',
  20810. ' function _Release: longint;',
  20811. ' end;',
  20812. ' TObject = class',
  20813. ' end;',
  20814. ' TArrOfIntf = array of IUnknown;',
  20815. 'begin',
  20816. '']);
  20817. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20818. ConvertProgram;
  20819. end;
  20820. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20821. begin
  20822. StartProgram(false);
  20823. Add([
  20824. '{$interfaces com}',
  20825. 'type',
  20826. ' IUnknown = interface',
  20827. ' function _AddRef: longint;',
  20828. ' function _Release: longint;',
  20829. ' end;',
  20830. ' TRec = record',
  20831. ' i: IUnknown;',
  20832. ' end;',
  20833. 'begin',
  20834. '']);
  20835. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20836. ConvertProgram;
  20837. end;
  20838. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20839. begin
  20840. StartUnit(false);
  20841. Add([
  20842. '{$interfaces com}',
  20843. 'interface',
  20844. 'implementation',
  20845. 'type',
  20846. ' IUnknown = interface',
  20847. ' function _AddRef: longint;',
  20848. ' end;',
  20849. ' TObject = class(IUnknown)',
  20850. ' function _AddRef: longint;',
  20851. ' end;',
  20852. 'function TObject._AddRef: longint; begin end;',
  20853. 'var i: IUnknown;',
  20854. ' o: TObject;',
  20855. 'initialization',
  20856. ' i:=nil;',
  20857. ' i:=i;',
  20858. ' i:=o;',
  20859. ' if (o as IUnknown)=nil then ;',
  20860. '']);
  20861. ConvertUnit;
  20862. CheckSource('TestClassInterface_COM_UnitInitialization',
  20863. LinesToStr([ // statements
  20864. 'var $impl = $mod.$impl;',
  20865. '']),
  20866. LinesToStr([ // this.$init
  20867. 'var $ir = rtl.createIntfRefs();',
  20868. 'try {',
  20869. ' rtl.setIntfP($impl, "i", null);',
  20870. ' rtl.setIntfP($impl, "i", $impl.i);',
  20871. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20872. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20873. '} finally {',
  20874. ' $ir.free();',
  20875. '};',
  20876. '']),
  20877. LinesToStr([ // implementation
  20878. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20879. 'rtl.createClass($impl, "TObject", null, function () {',
  20880. ' this.$init = function () {',
  20881. ' };',
  20882. ' this.$final = function () {',
  20883. ' };',
  20884. ' this._AddRef = function () {',
  20885. ' var Result = 0;',
  20886. ' return Result;',
  20887. ' };',
  20888. ' rtl.addIntf(this, $impl.IUnknown);',
  20889. '});',
  20890. '$impl.i = null;',
  20891. '$impl.o = null;',
  20892. ''])
  20893. );
  20894. end;
  20895. procedure TTestModule.TestClassInterface_GUID;
  20896. begin
  20897. StartProgram(false);
  20898. Add([
  20899. '{$interfaces corba}',
  20900. 'type',
  20901. ' IUnknown = interface',
  20902. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20903. ' end;',
  20904. ' TObject = class end;',
  20905. ' TGUID = record D1, D2, D3, D4: word; end;',
  20906. ' TAliasGUID = TGUID;',
  20907. ' TGUIDString = type string;',
  20908. ' TAliasGUIDString = TGUIDString;',
  20909. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20910. 'begin end;',
  20911. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20912. 'begin end;',
  20913. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20914. 'begin end;',
  20915. 'var',
  20916. ' i: IUnknown;',
  20917. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20918. ' s: TAliasGUIDString;',
  20919. 'begin',
  20920. ' DoConstGUIDIt(IUnknown);',
  20921. ' DoDefGUID(IUnknown);',
  20922. ' DoStr(IUnknown);',
  20923. ' DoConstGUIDIt(i);',
  20924. ' DoDefGUID(i);',
  20925. ' DoStr(i);',
  20926. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20927. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20928. ' DoStr(g);',
  20929. ' g:=i;',
  20930. ' g:=IUnknown;',
  20931. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20932. ' s:=i;',
  20933. ' s:=IUnknown;',
  20934. ' s:=g;',
  20935. ' if g=i then ;',
  20936. ' if i=g then ;',
  20937. ' if g=IUnknown then ;',
  20938. ' if IUnknown=g then ;',
  20939. ' if s=i then ;',
  20940. ' if i=s then ;',
  20941. ' if s=IUnknown then ;',
  20942. ' if IUnknown=s then ;',
  20943. ' if s=g then ;',
  20944. ' if g=s then ;',
  20945. '']);
  20946. ConvertProgram;
  20947. CheckSource('TestClassInterface_GUID',
  20948. LinesToStr([ // statements
  20949. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20950. 'rtl.createClass(this, "TObject", null, function () {',
  20951. ' this.$init = function () {',
  20952. ' };',
  20953. ' this.$final = function () {',
  20954. ' };',
  20955. '});',
  20956. 'rtl.recNewT(this, "TGUID", function () {',
  20957. ' this.D1 = 0;',
  20958. ' this.D2 = 0;',
  20959. ' this.D3 = 0;',
  20960. ' this.D4 = 0;',
  20961. ' this.$eq = function (b) {',
  20962. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20963. ' };',
  20964. ' this.$assign = function (s) {',
  20965. ' this.D1 = s.D1;',
  20966. ' this.D2 = s.D2;',
  20967. ' this.D3 = s.D3;',
  20968. ' this.D4 = s.D4;',
  20969. ' return this;',
  20970. ' };',
  20971. '});',
  20972. 'this.DoConstGUIDIt = function (g) {',
  20973. '};',
  20974. 'this.DoDefGUID = function (g) {',
  20975. '};',
  20976. 'this.DoStr = function (s) {',
  20977. '};',
  20978. 'this.i = null;',
  20979. 'this.g = this.TGUID.$clone({',
  20980. ' D1: 0xD91C9AF4,',
  20981. ' D2: 0x3C93,',
  20982. ' D3: 0x420F,',
  20983. ' D4: [',
  20984. ' 0xA3,',
  20985. ' 0x03,',
  20986. ' 0xBF,',
  20987. ' 0x5B,',
  20988. ' 0xA8,',
  20989. ' 0x2B,',
  20990. ' 0xFD,',
  20991. ' 0x23',
  20992. ' ]',
  20993. '});',
  20994. 'this.s = "";',
  20995. '']),
  20996. LinesToStr([ // $mod.$main
  20997. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20998. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20999. '$mod.DoStr($mod.IUnknown.$guid);',
  21000. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  21001. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  21002. '$mod.DoStr($mod.i.$guid);',
  21003. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21004. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21005. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  21006. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  21007. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  21008. '$mod.g.$assign({',
  21009. ' D1: 0xD91C9AF4,',
  21010. ' D2: 0x3C93,',
  21011. ' D3: 0x420F,',
  21012. ' D4: [',
  21013. ' 0xA3,',
  21014. ' 0x03,',
  21015. ' 0xBF,',
  21016. ' 0x5B,',
  21017. ' 0xA8,',
  21018. ' 0x2B,',
  21019. ' 0xFD,',
  21020. ' 0x23',
  21021. ' ]',
  21022. '});',
  21023. '$mod.s = $mod.i.$guid;',
  21024. '$mod.s = $mod.IUnknown.$guid;',
  21025. '$mod.s = rtl.guidrToStr($mod.g);',
  21026. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21027. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21028. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21029. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21030. 'if ($mod.s === $mod.i.$guid) ;',
  21031. 'if ($mod.i.$guid === $mod.s) ;',
  21032. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  21033. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  21034. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21035. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21036. '']));
  21037. end;
  21038. procedure TTestModule.TestClassInterface_GUIDProperty;
  21039. begin
  21040. StartProgram(false);
  21041. Add([
  21042. '{$interfaces corba}',
  21043. 'type',
  21044. ' IUnknown = interface',
  21045. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21046. ' end;',
  21047. ' TGUID = record D1, D2, D3, D4: word; end;',
  21048. ' TAliasGUID = TGUID;',
  21049. ' TGUIDString = type string;',
  21050. ' TAliasGUIDString = TGUIDString;',
  21051. ' TObject = class',
  21052. ' function GetG: TAliasGUID; virtual; abstract;',
  21053. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  21054. ' function GetS: TAliasGUIDString; virtual; abstract;',
  21055. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  21056. ' property g: TAliasGUID read GetG write SetG;',
  21057. ' property s: TAliasGUIDString read GetS write SetS;',
  21058. ' end;',
  21059. 'var o: TObject;',
  21060. 'begin',
  21061. ' o.g:=IUnknown;',
  21062. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21063. ' o.s:=IUnknown;',
  21064. ' o.s:=o.g;',
  21065. '']);
  21066. ConvertProgram;
  21067. CheckSource('TestClassInterface_GUIDProperty',
  21068. LinesToStr([ // statements
  21069. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21070. 'rtl.recNewT(this, "TGUID", function () {',
  21071. ' this.D1 = 0;',
  21072. ' this.D2 = 0;',
  21073. ' this.D3 = 0;',
  21074. ' this.D4 = 0;',
  21075. ' this.$eq = function (b) {',
  21076. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21077. ' };',
  21078. ' this.$assign = function (s) {',
  21079. ' this.D1 = s.D1;',
  21080. ' this.D2 = s.D2;',
  21081. ' this.D3 = s.D3;',
  21082. ' this.D4 = s.D4;',
  21083. ' return this;',
  21084. ' };',
  21085. '});',
  21086. 'rtl.createClass(this, "TObject", null, function () {',
  21087. ' this.$init = function () {',
  21088. ' };',
  21089. ' this.$final = function () {',
  21090. ' };',
  21091. '});',
  21092. 'this.o = null;',
  21093. '']),
  21094. LinesToStr([ // $mod.$main
  21095. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  21096. '$mod.o.SetG({',
  21097. ' D1: 0xD91C9AF4,',
  21098. ' D2: 0x3C93,',
  21099. ' D3: 0x420F,',
  21100. ' D4: [',
  21101. ' 0xA3,',
  21102. ' 0x03,',
  21103. ' 0xBF,',
  21104. ' 0x5B,',
  21105. ' 0xA8,',
  21106. ' 0x2B,',
  21107. ' 0xFD,',
  21108. ' 0x23',
  21109. ' ]',
  21110. '});',
  21111. '$mod.o.SetS($mod.IUnknown.$guid);',
  21112. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  21113. '']));
  21114. end;
  21115. procedure TTestModule.TestClassHelper_ClassVar;
  21116. begin
  21117. StartProgram(false);
  21118. Add([
  21119. 'type',
  21120. ' TObject = class',
  21121. ' end;',
  21122. ' THelper = class helper for TObject',
  21123. ' const',
  21124. ' One = 1;',
  21125. ' Two: word = 2;',
  21126. ' class var',
  21127. ' Glob: word;',
  21128. ' function Foo(w: word): word;',
  21129. ' class function Bar(w: word): word;',
  21130. ' end;',
  21131. 'function THelper.foo(w: word): word;',
  21132. 'begin',
  21133. ' Result:=w;',
  21134. ' Two:=One+w;',
  21135. ' Glob:=Glob;',
  21136. ' Result:=Self.Glob;',
  21137. ' Self.Glob:=Self.Glob;',
  21138. ' with Self do Glob:=Glob;',
  21139. 'end;',
  21140. 'class function THelper.bar(w: word): word;',
  21141. 'begin',
  21142. ' Result:=w;',
  21143. ' Two:=One;',
  21144. ' Glob:=Glob;',
  21145. ' Self.Glob:=Self.Glob;',
  21146. ' with Self do Glob:=Glob;',
  21147. 'end;',
  21148. 'var o: TObject;',
  21149. 'begin',
  21150. ' tobject.two:=tobject.one;',
  21151. ' tobject.Glob:=tobject.Glob;',
  21152. ' with tobject do begin',
  21153. ' two:=one;',
  21154. ' Glob:=Glob;',
  21155. ' end;',
  21156. ' o.two:=o.one;',
  21157. ' o.Glob:=o.Glob;',
  21158. ' with o do begin',
  21159. ' two:=one;',
  21160. ' Glob:=Glob;',
  21161. ' end;',
  21162. '']);
  21163. ConvertProgram;
  21164. CheckSource('TestClassHelper_ClassVar',
  21165. LinesToStr([ // statements
  21166. 'rtl.createClass(this, "TObject", null, function () {',
  21167. ' this.$init = function () {',
  21168. ' };',
  21169. ' this.$final = function () {',
  21170. ' };',
  21171. '});',
  21172. 'rtl.createHelper(this, "THelper", null, function () {',
  21173. ' this.One = 1;',
  21174. ' this.Two = 2;',
  21175. ' this.Glob = 0;',
  21176. ' this.Foo = function (w) {',
  21177. ' var Result = 0;',
  21178. ' Result = w;',
  21179. ' $mod.THelper.Two = 1 + w;',
  21180. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21181. ' Result = $mod.THelper.Glob;',
  21182. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21183. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21184. ' return Result;',
  21185. ' };',
  21186. ' this.Bar = function (w) {',
  21187. ' var Result = 0;',
  21188. ' Result = w;',
  21189. ' $mod.THelper.Two = 1;',
  21190. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21191. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21192. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21193. ' return Result;',
  21194. ' };',
  21195. '});',
  21196. 'this.o = null;',
  21197. '']),
  21198. LinesToStr([ // $mod.$main
  21199. '$mod.THelper.Two = 1;',
  21200. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21201. 'var $with = $mod.TObject;',
  21202. '$mod.THelper.Two = 1;',
  21203. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21204. '$mod.THelper.Two = 1;',
  21205. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21206. 'var $with1 = $mod.o;',
  21207. '$mod.THelper.Two = 1;',
  21208. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21209. '']));
  21210. end;
  21211. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  21212. begin
  21213. StartProgram(false);
  21214. Add([
  21215. 'type',
  21216. ' TObject = class',
  21217. ' FSize: word;',
  21218. ' property Size: word read FSize write FSize;',
  21219. ' end;',
  21220. ' THelper = class helper for TObject',
  21221. ' function Foo(w: word = 1): word;',
  21222. ' end;',
  21223. 'function THelper.foo(w: word): word;',
  21224. 'begin',
  21225. ' Result:=Size;',
  21226. ' Size:=Size+2;',
  21227. ' Self.Size:=Self.Size+3;',
  21228. ' FSize:=FSize+4;',
  21229. ' Self.FSize:=Self.FSize+5;',
  21230. ' with Self do begin',
  21231. ' Size:=Size+6;',
  21232. ' FSize:=FSize+7;',
  21233. ' FSize:=FSize+8;',
  21234. ' end;',
  21235. 'end;',
  21236. 'begin',
  21237. '']);
  21238. ConvertProgram;
  21239. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  21240. LinesToStr([ // statements
  21241. 'rtl.createClass(this, "TObject", null, function () {',
  21242. ' this.$init = function () {',
  21243. ' this.FSize = 0;',
  21244. ' };',
  21245. ' this.$final = function () {',
  21246. ' };',
  21247. '});',
  21248. 'rtl.createHelper(this, "THelper", null, function () {',
  21249. ' this.Foo = function (w) {',
  21250. ' var Result = 0;',
  21251. ' Result = this.FSize;',
  21252. ' this.FSize = this.FSize + 2;',
  21253. ' this.FSize = this.FSize + 3;',
  21254. ' this.FSize = this.FSize + 4;',
  21255. ' this.FSize = this.FSize + 5;',
  21256. ' this.FSize = this.FSize + 6;',
  21257. ' this.FSize = this.FSize + 7;',
  21258. ' this.FSize = this.FSize + 8;',
  21259. ' return Result;',
  21260. ' };',
  21261. '});',
  21262. '']),
  21263. LinesToStr([ // $mod.$main
  21264. '']));
  21265. end;
  21266. procedure TTestModule.TestClassHelper_Method_Call;
  21267. begin
  21268. StartProgram(false);
  21269. Add([
  21270. 'type',
  21271. ' TObject = class',
  21272. ' procedure Run(w: word = 10);',
  21273. ' end;',
  21274. ' THelper = class helper for TObject',
  21275. ' function Foo(w: word = 1): word;',
  21276. ' end;',
  21277. 'procedure TObject.Run(w: word);',
  21278. 'var o: TObject;',
  21279. 'begin',
  21280. ' Foo;',
  21281. ' Foo();',
  21282. ' Foo(2);',
  21283. ' Self.Foo;',
  21284. ' Self.Foo();',
  21285. ' Self.Foo(3);',
  21286. ' with Self do begin',
  21287. ' Foo;',
  21288. ' Foo();',
  21289. ' Foo(4);',
  21290. ' end;',
  21291. ' with o do Foo(5);',
  21292. 'end;',
  21293. 'function THelper.foo(w: word): word;',
  21294. 'begin',
  21295. ' Run;',
  21296. ' Run();',
  21297. ' Run(11);',
  21298. ' Foo;',
  21299. ' Foo();',
  21300. ' Foo(12);',
  21301. ' Self.Foo;',
  21302. ' Self.Foo();',
  21303. ' Self.Foo(13);',
  21304. ' with Self do begin',
  21305. ' Foo;',
  21306. ' Foo();',
  21307. ' Foo(14);',
  21308. ' end;',
  21309. 'end;',
  21310. 'var Obj: TObject;',
  21311. 'begin',
  21312. ' obj.Foo;',
  21313. ' obj.Foo();',
  21314. ' obj.Foo(21);',
  21315. ' with obj do begin',
  21316. ' Foo;',
  21317. ' Foo();',
  21318. ' Foo(22);',
  21319. ' end;',
  21320. '']);
  21321. ConvertProgram;
  21322. CheckSource('TestClassHelper_Method_Call',
  21323. LinesToStr([ // statements
  21324. 'rtl.createClass(this, "TObject", null, function () {',
  21325. ' this.$init = function () {',
  21326. ' };',
  21327. ' this.$final = function () {',
  21328. ' };',
  21329. ' this.Run = function (w) {',
  21330. ' var o = null;',
  21331. ' $mod.THelper.Foo.call(this, 1);',
  21332. ' $mod.THelper.Foo.call(this, 1);',
  21333. ' $mod.THelper.Foo.call(this, 2);',
  21334. ' $mod.THelper.Foo.call(this, 1);',
  21335. ' $mod.THelper.Foo.call(this, 1);',
  21336. ' $mod.THelper.Foo.call(this, 3);',
  21337. ' $mod.THelper.Foo.call(this, 1);',
  21338. ' $mod.THelper.Foo.call(this, 1);',
  21339. ' $mod.THelper.Foo.call(this, 4);',
  21340. ' $mod.THelper.Foo.call(o, 5);',
  21341. ' };',
  21342. '});',
  21343. 'rtl.createHelper(this, "THelper", null, function () {',
  21344. ' this.Foo = function (w) {',
  21345. ' var Result = 0;',
  21346. ' this.Run(10);',
  21347. ' this.Run(10);',
  21348. ' this.Run(11);',
  21349. ' $mod.THelper.Foo.call(this, 1);',
  21350. ' $mod.THelper.Foo.call(this, 1);',
  21351. ' $mod.THelper.Foo.call(this, 12);',
  21352. ' $mod.THelper.Foo.call(this, 1);',
  21353. ' $mod.THelper.Foo.call(this, 1);',
  21354. ' $mod.THelper.Foo.call(this, 13);',
  21355. ' $mod.THelper.Foo.call(this, 1);',
  21356. ' $mod.THelper.Foo.call(this, 1);',
  21357. ' $mod.THelper.Foo.call(this, 14);',
  21358. ' return Result;',
  21359. ' };',
  21360. '});',
  21361. 'this.Obj = null;',
  21362. '']),
  21363. LinesToStr([ // $mod.$main
  21364. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21365. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21366. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21367. 'var $with = $mod.Obj;',
  21368. '$mod.THelper.Foo.call($with, 1);',
  21369. '$mod.THelper.Foo.call($with, 1);',
  21370. '$mod.THelper.Foo.call($with, 22);',
  21371. '']));
  21372. end;
  21373. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  21374. begin
  21375. StartProgram(false);
  21376. Add([
  21377. 'type',
  21378. ' TObject = class',
  21379. ' procedure Run(w: word = 10);',
  21380. ' end;',
  21381. ' THelper = class helper for TObject',
  21382. ' function Foo(w: word = 1): word;',
  21383. ' end;',
  21384. 'procedure TObject.Run(w: word);',
  21385. ' procedure Sub(Self: TObject);',
  21386. ' begin',
  21387. ' Foo;',
  21388. ' Foo();',
  21389. ' Self.Foo;',
  21390. ' Self.Foo();',
  21391. ' with Self do begin',
  21392. ' Foo;',
  21393. ' Foo();',
  21394. ' end;',
  21395. ' end;',
  21396. 'begin',
  21397. 'end;',
  21398. 'function THelper.foo(w: word): word;',
  21399. ' procedure Sub(Self: TObject);',
  21400. ' begin',
  21401. ' Run;',
  21402. ' Run();',
  21403. ' Foo;',
  21404. ' Foo();',
  21405. ' Self.Foo;',
  21406. ' Self.Foo();',
  21407. ' with Self do begin',
  21408. ' Foo;',
  21409. ' Foo();',
  21410. ' end;',
  21411. ' end;',
  21412. 'begin',
  21413. 'end;',
  21414. 'begin',
  21415. '']);
  21416. ConvertProgram;
  21417. CheckSource('TestClassHelper_Method_Nested_Call',
  21418. LinesToStr([ // statements
  21419. 'rtl.createClass(this, "TObject", null, function () {',
  21420. ' this.$init = function () {',
  21421. ' };',
  21422. ' this.$final = function () {',
  21423. ' };',
  21424. ' this.Run = function (w) {',
  21425. ' var $Self = this;',
  21426. ' function Sub(Self) {',
  21427. ' $mod.THelper.Foo.call($Self, 1);',
  21428. ' $mod.THelper.Foo.call($Self, 1);',
  21429. ' $mod.THelper.Foo.call(Self, 1);',
  21430. ' $mod.THelper.Foo.call(Self, 1);',
  21431. ' $mod.THelper.Foo.call(Self, 1);',
  21432. ' $mod.THelper.Foo.call(Self, 1);',
  21433. ' };',
  21434. ' };',
  21435. '});',
  21436. 'rtl.createHelper(this, "THelper", null, function () {',
  21437. ' this.Foo = function (w) {',
  21438. ' var $Self = this;',
  21439. ' var Result = 0;',
  21440. ' function Sub(Self) {',
  21441. ' $Self.Run(10);',
  21442. ' $Self.Run(10);',
  21443. ' $mod.THelper.Foo.call($Self, 1);',
  21444. ' $mod.THelper.Foo.call($Self, 1);',
  21445. ' $mod.THelper.Foo.call(Self, 1);',
  21446. ' $mod.THelper.Foo.call(Self, 1);',
  21447. ' $mod.THelper.Foo.call(Self, 1);',
  21448. ' $mod.THelper.Foo.call(Self, 1);',
  21449. ' };',
  21450. ' return Result;',
  21451. ' };',
  21452. '});',
  21453. '']),
  21454. LinesToStr([ // $mod.$main
  21455. '']));
  21456. end;
  21457. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21458. begin
  21459. StartProgram(false);
  21460. Add([
  21461. 'type',
  21462. ' TObject = class',
  21463. ' class procedure Run(w: word = 10);',
  21464. ' end;',
  21465. ' THelper = class helper for TObject',
  21466. ' class function Foo(w: word = 1): word;',
  21467. ' end;',
  21468. 'class procedure TObject.Run(w: word);',
  21469. 'begin',
  21470. ' Foo;',
  21471. ' Foo();',
  21472. ' Self.Foo;',
  21473. ' Self.Foo();',
  21474. ' with Self do begin',
  21475. ' Foo;',
  21476. ' Foo();',
  21477. ' end;',
  21478. 'end;',
  21479. 'class function THelper.foo(w: word): word;',
  21480. 'begin',
  21481. ' Run;',
  21482. ' Run();',
  21483. ' Foo;',
  21484. ' Foo();',
  21485. ' Self.Foo;',
  21486. ' Self.Foo();',
  21487. ' with Self do begin',
  21488. ' Foo;',
  21489. ' Foo();',
  21490. ' end;',
  21491. 'end;',
  21492. 'var',
  21493. ' Obj: TObject;',
  21494. 'begin',
  21495. ' obj.Foo;',
  21496. ' obj.Foo();',
  21497. ' with obj do begin',
  21498. ' Foo;',
  21499. ' Foo();',
  21500. ' end;',
  21501. ' tobject.Foo;',
  21502. ' tobject.Foo();',
  21503. ' with tobject do begin',
  21504. ' Foo;',
  21505. ' Foo();',
  21506. ' end;',
  21507. '']);
  21508. ConvertProgram;
  21509. CheckSource('TestClassHelper_ClassMethod_Call',
  21510. LinesToStr([ // statements
  21511. 'rtl.createClass(this, "TObject", null, function () {',
  21512. ' this.$init = function () {',
  21513. ' };',
  21514. ' this.$final = function () {',
  21515. ' };',
  21516. ' this.Run = function (w) {',
  21517. ' $mod.THelper.Foo.call(this, 1);',
  21518. ' $mod.THelper.Foo.call(this, 1);',
  21519. ' $mod.THelper.Foo.call(this, 1);',
  21520. ' $mod.THelper.Foo.call(this, 1);',
  21521. ' $mod.THelper.Foo.call(this, 1);',
  21522. ' $mod.THelper.Foo.call(this, 1);',
  21523. ' };',
  21524. '});',
  21525. 'rtl.createHelper(this, "THelper", null, function () {',
  21526. ' this.Foo = function (w) {',
  21527. ' var Result = 0;',
  21528. ' this.Run(10);',
  21529. ' this.Run(10);',
  21530. ' $mod.THelper.Foo.call(this, 1);',
  21531. ' $mod.THelper.Foo.call(this, 1);',
  21532. ' $mod.THelper.Foo.call(this, 1);',
  21533. ' $mod.THelper.Foo.call(this, 1);',
  21534. ' $mod.THelper.Foo.call(this, 1);',
  21535. ' $mod.THelper.Foo.call(this, 1);',
  21536. ' return Result;',
  21537. ' };',
  21538. '});',
  21539. 'this.Obj = null;',
  21540. '']),
  21541. LinesToStr([ // $mod.$main
  21542. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21543. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21544. 'var $with = $mod.Obj;',
  21545. '$mod.THelper.Foo.call($with.$class, 1);',
  21546. '$mod.THelper.Foo.call($with.$class, 1);',
  21547. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21548. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21549. 'var $with1 = $mod.TObject;',
  21550. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21551. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21552. '']));
  21553. end;
  21554. procedure TTestModule.TestClassHelper_ClassOf;
  21555. begin
  21556. StartProgram(false);
  21557. Add([
  21558. 'type',
  21559. ' TObject = class',
  21560. ' end;',
  21561. ' TClass = class of TObject;',
  21562. ' THelper = class helper for TObject',
  21563. ' class function Foo(w: word = 1): word;',
  21564. ' end;',
  21565. 'class function THelper.foo(w: word): word;',
  21566. 'begin',
  21567. 'end;',
  21568. 'var',
  21569. ' c: TClass;',
  21570. 'begin',
  21571. ' c.Foo;',
  21572. ' c.Foo();',
  21573. ' with c do begin',
  21574. ' Foo;',
  21575. ' Foo();',
  21576. ' end;',
  21577. '']);
  21578. ConvertProgram;
  21579. CheckSource('TestClassHelper_ClassOf',
  21580. LinesToStr([ // statements
  21581. 'rtl.createClass(this, "TObject", null, function () {',
  21582. ' this.$init = function () {',
  21583. ' };',
  21584. ' this.$final = function () {',
  21585. ' };',
  21586. '});',
  21587. 'rtl.createHelper(this, "THelper", null, function () {',
  21588. ' this.Foo = function (w) {',
  21589. ' var Result = 0;',
  21590. ' return Result;',
  21591. ' };',
  21592. '});',
  21593. 'this.c = null;',
  21594. '']),
  21595. LinesToStr([ // $mod.$main
  21596. '$mod.THelper.Foo.call($mod.c, 1);',
  21597. '$mod.THelper.Foo.call($mod.c, 1);',
  21598. 'var $with = $mod.c;',
  21599. '$mod.THelper.Foo.call($with, 1);',
  21600. '$mod.THelper.Foo.call($with, 1);',
  21601. '']));
  21602. end;
  21603. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21604. begin
  21605. StartProgram(false);
  21606. Add([
  21607. '{$mode objfpc}',
  21608. 'type',
  21609. ' TObject = class',
  21610. ' procedure DoIt;',
  21611. ' end;',
  21612. ' THelper = class helper for TObject',
  21613. ' procedure Fly(w: word = 1);',
  21614. ' class procedure Glide(w: word = 1);',
  21615. ' class procedure Run(w: word = 1); static;',
  21616. ' end;',
  21617. ' TFly = procedure(w: word) of object;',
  21618. ' TGlide = TFly;',
  21619. ' TRun = procedure(w: word);',
  21620. 'var',
  21621. ' f: TFly;',
  21622. ' g: TGlide;',
  21623. ' r: TRun;',
  21624. 'procedure TObject.DoIt;',
  21625. 'begin',
  21626. ' f:=@fly;',
  21627. ' g:=@glide;',
  21628. ' r:=@run;',
  21629. ' f:[email protected];',
  21630. ' g:[email protected];',
  21631. ' r:[email protected];',
  21632. ' with self do begin',
  21633. ' f:=@fly;',
  21634. ' g:=@glide;',
  21635. ' r:=@run;',
  21636. ' end;',
  21637. 'end;',
  21638. 'procedure THelper.fly(w: word);',
  21639. 'begin',
  21640. ' f:=@fly;',
  21641. ' g:=@glide;',
  21642. ' r:=@run;',
  21643. 'end;',
  21644. 'class procedure THelper.glide(w: word);',
  21645. 'begin',
  21646. ' g:=@glide;',
  21647. ' r:=@run;',
  21648. 'end;',
  21649. 'class procedure THelper.run(w: word);',
  21650. 'begin',
  21651. ' g:=@glide;',
  21652. ' r:=@run;',
  21653. 'end;',
  21654. 'var',
  21655. ' Obj: TObject;',
  21656. 'begin',
  21657. ' f:[email protected];',
  21658. ' g:[email protected];',
  21659. ' r:[email protected];',
  21660. ' with obj do begin',
  21661. ' f:=@fly;',
  21662. ' g:=@glide;',
  21663. ' r:=@run;',
  21664. ' end;',
  21665. ' g:[email protected];',
  21666. ' r:[email protected];',
  21667. ' with tobject do begin',
  21668. ' g:=@glide;',
  21669. ' r:=@run;',
  21670. ' end;',
  21671. '']);
  21672. ConvertProgram;
  21673. CheckSource('TestClassHelper_MethodRefObjFPC',
  21674. LinesToStr([ // statements
  21675. 'rtl.createClass(this, "TObject", null, function () {',
  21676. ' this.$init = function () {',
  21677. ' };',
  21678. ' this.$final = function () {',
  21679. ' };',
  21680. ' this.DoIt = function () {',
  21681. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21682. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21683. ' $mod.r = $mod.THelper.Run;',
  21684. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21685. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21686. ' $mod.r = $mod.THelper.Run;',
  21687. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21688. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21689. ' $mod.r = $mod.THelper.Run;',
  21690. ' };',
  21691. '});',
  21692. 'rtl.createHelper(this, "THelper", null, function () {',
  21693. ' this.Fly = function (w) {',
  21694. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21695. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21696. ' $mod.r = $mod.THelper.Run;',
  21697. ' };',
  21698. ' this.Glide = function (w) {',
  21699. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21700. ' $mod.r = $mod.THelper.Run;',
  21701. ' };',
  21702. ' this.Run = function (w) {',
  21703. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21704. ' $mod.r = $mod.THelper.Run;',
  21705. ' };',
  21706. '});',
  21707. 'this.f = null;',
  21708. 'this.g = null;',
  21709. 'this.r = null;',
  21710. 'this.Obj = null;',
  21711. '']),
  21712. LinesToStr([ // $mod.$main
  21713. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21714. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21715. '$mod.r = $mod.THelper.Run;',
  21716. 'var $with = $mod.Obj;',
  21717. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21718. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21719. '$mod.r = $mod.THelper.Run;',
  21720. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21721. '$mod.r = $mod.THelper.Run;',
  21722. 'var $with1 = $mod.TObject;',
  21723. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21724. '$mod.r = $mod.THelper.Run;',
  21725. '']));
  21726. end;
  21727. procedure TTestModule.TestClassHelper_Constructor;
  21728. begin
  21729. StartProgram(false);
  21730. Add([
  21731. 'type',
  21732. ' TObject = class',
  21733. ' constructor Create;',
  21734. ' end;',
  21735. ' TClass = class of TObject;',
  21736. ' THelper = class helper for TObject',
  21737. ' constructor NewHlp(w: word);',
  21738. ' end;',
  21739. 'var',
  21740. ' obj: TObject;',
  21741. ' c: TClass;',
  21742. 'constructor TObject.Create;',
  21743. 'begin',
  21744. ' NewHlp(2);', // normal call
  21745. ' tobject.NewHlp(3);', // new instance
  21746. ' c.newhlp(4);', // new instance
  21747. 'end;',
  21748. 'constructor THelper.NewHlp(w: word);',
  21749. 'begin',
  21750. ' create;', // normal call
  21751. ' tobject.create;', // new instance
  21752. ' NewHlp(2);', // normal call
  21753. ' tobject.NewHlp(3);', // new instance
  21754. ' c.newhlp(4);', // new instance
  21755. 'end;',
  21756. 'begin',
  21757. ' obj.newhlp(2);', // normal call
  21758. ' with Obj do newhlp(12);', // normal call
  21759. ' tobject.newhlp(3);', // new instance
  21760. ' with tobject do newhlp(13);', // new instance
  21761. ' c.newhlp(4);', // new instance
  21762. ' with c do newhlp(14);', // new instance
  21763. '']);
  21764. ConvertProgram;
  21765. CheckSource('TestClassHelper_Constructor',
  21766. LinesToStr([ // statements
  21767. 'rtl.createClass(this, "TObject", null, function () {',
  21768. ' this.$init = function () {',
  21769. ' };',
  21770. ' this.$final = function () {',
  21771. ' };',
  21772. ' this.Create = function () {',
  21773. ' $mod.THelper.NewHlp.call(this, 2);',
  21774. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21775. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21776. ' return this;',
  21777. ' };',
  21778. '});',
  21779. 'rtl.createHelper(this, "THelper", null, function () {',
  21780. ' this.NewHlp = function (w) {',
  21781. ' this.Create();',
  21782. ' $mod.TObject.$create("Create");',
  21783. ' $mod.THelper.NewHlp.call(this, 2);',
  21784. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21785. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21786. ' return this;',
  21787. ' };',
  21788. '});',
  21789. 'this.obj = null;',
  21790. 'this.c = null;',
  21791. '']),
  21792. LinesToStr([ // $mod.$main
  21793. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21794. 'var $with = $mod.obj;',
  21795. '$mod.THelper.NewHlp.call($with, 12);',
  21796. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21797. 'var $with1 = $mod.TObject;',
  21798. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21799. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21800. 'var $with2 = $mod.c;',
  21801. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21802. '']));
  21803. end;
  21804. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21805. begin
  21806. StartProgram(false);
  21807. Add([
  21808. 'type',
  21809. ' TObject = class',
  21810. ' procedure Fly;',
  21811. ' end;',
  21812. ' TObjHelper = class helper for TObject',
  21813. ' procedure Fly;',
  21814. ' end;',
  21815. ' TBird = class',
  21816. ' procedure Fly;',
  21817. ' end;',
  21818. ' TBirdHelper = class helper for TBird',
  21819. ' procedure Fly;',
  21820. ' procedure Walk(w: word);',
  21821. ' end;',
  21822. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21823. ' procedure Fly;',
  21824. ' procedure Walk(w: word);',
  21825. ' end;',
  21826. 'procedure Tobject.fly;',
  21827. 'begin',
  21828. ' inherited;', // ignore
  21829. 'end;',
  21830. 'procedure Tobjhelper.fly;',
  21831. 'begin',
  21832. ' {@TObject_Fly}inherited;',
  21833. ' inherited {@TObject_Fly}Fly;',
  21834. 'end;',
  21835. 'procedure Tbird.fly;',
  21836. 'begin',
  21837. ' {@TObjHelper_Fly}inherited;',
  21838. ' inherited {@TObjHelper_Fly}Fly;',
  21839. 'end;',
  21840. 'procedure Tbirdhelper.fly;',
  21841. 'begin',
  21842. ' {@TBird_Fly}inherited;',
  21843. ' inherited {@TBird_Fly}Fly;',
  21844. 'end;',
  21845. 'procedure Tbirdhelper.walk(w: word);',
  21846. 'begin',
  21847. 'end;',
  21848. 'procedure teagleHelper.fly;',
  21849. 'begin',
  21850. ' {@TBird_Fly}inherited;',
  21851. ' inherited {@TBird_Fly}Fly;',
  21852. 'end;',
  21853. 'procedure teagleHelper.walk(w: word);',
  21854. 'begin',
  21855. ' {@TBirdHelper_Walk}inherited;',
  21856. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21857. 'end;',
  21858. 'begin',
  21859. '']);
  21860. ConvertProgram;
  21861. CheckSource('TestClassHelper_InheritedObjFPC',
  21862. LinesToStr([ // statements
  21863. 'rtl.createClass(this, "TObject", null, function () {',
  21864. ' this.$init = function () {',
  21865. ' };',
  21866. ' this.$final = function () {',
  21867. ' };',
  21868. ' this.Fly = function () {',
  21869. ' };',
  21870. '});',
  21871. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21872. ' this.Fly = function () {',
  21873. ' $mod.TObject.Fly.call(this);',
  21874. ' $mod.TObject.Fly.call(this);',
  21875. ' };',
  21876. '});',
  21877. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21878. ' this.Fly$1 = function () {',
  21879. ' $mod.TObjHelper.Fly.call(this);',
  21880. ' $mod.TObjHelper.Fly.call(this);',
  21881. ' };',
  21882. '});',
  21883. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21884. ' this.Fly = function () {',
  21885. ' $mod.TBird.Fly$1.call(this);',
  21886. ' $mod.TBird.Fly$1.call(this);',
  21887. ' };',
  21888. ' this.Walk = function (w) {',
  21889. ' };',
  21890. '});',
  21891. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21892. ' this.Fly$1 = function () {',
  21893. ' $mod.TBird.Fly$1.call(this);',
  21894. ' $mod.TBird.Fly$1.call(this);',
  21895. ' };',
  21896. ' this.Walk$1 = function (w) {',
  21897. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21898. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21899. ' };',
  21900. '});',
  21901. '']),
  21902. LinesToStr([ // $mod.$main
  21903. '']));
  21904. end;
  21905. procedure TTestModule.TestClassHelper_Property;
  21906. begin
  21907. StartProgram(false);
  21908. Add([
  21909. 'type',
  21910. ' TObject = class',
  21911. ' FSize: word;',
  21912. ' function GetSpeed: word;',
  21913. ' procedure SetSpeed(Value: word);',
  21914. ' end;',
  21915. ' TObjHelper = class helper for TObject',
  21916. ' function GetLeft: word;',
  21917. ' procedure SetLeft(Value: word);',
  21918. ' property Size: word read FSize write FSize;',
  21919. ' property Speed: word read GetSpeed write SetSpeed;',
  21920. ' property Left: word read GetLeft write SetLeft;',
  21921. ' end;',
  21922. ' TBird = class',
  21923. ' property NotRight: word read GetLeft write SetLeft;',
  21924. ' procedure DoIt;',
  21925. ' end;',
  21926. 'var',
  21927. ' b: TBird;',
  21928. 'function Tobject.GetSpeed: word;',
  21929. 'begin',
  21930. ' Size:=Size+11;',
  21931. ' Speed:=Speed+12;',
  21932. ' Result:=Left+13;',
  21933. ' Left:=13;',
  21934. ' Left:=Left+13;',
  21935. ' Self.Size:=Self.Size+21;',
  21936. ' Self.Speed:=Self.Speed+22;',
  21937. ' Self.Left:=Self.Left+23;',
  21938. ' with Self do begin',
  21939. ' Size:=Size+31;',
  21940. ' Speed:=Speed+32;',
  21941. ' Left:=Left+33;',
  21942. ' end;',
  21943. 'end;',
  21944. 'procedure Tobject.SetSpeed(Value: word);',
  21945. 'begin',
  21946. 'end;',
  21947. 'function TObjHelper.GetLeft: word;',
  21948. 'begin',
  21949. ' Size:=Size+11;',
  21950. ' Speed:=Speed+12;',
  21951. ' Left:=Left+13;',
  21952. ' Self.Size:=Self.Size+21;',
  21953. ' Self.Speed:=Self.Speed+22;',
  21954. ' Self.Left:=Self.Left+23;',
  21955. ' with Self do begin',
  21956. ' Size:=Size+31;',
  21957. ' Speed:=Speed+32;',
  21958. ' Left:=Left+33;',
  21959. ' end;',
  21960. 'end;',
  21961. 'procedure TObjHelper.SetLeft(Value: word);',
  21962. 'begin',
  21963. 'end;',
  21964. 'procedure TBird.DoIt;',
  21965. 'begin',
  21966. ' NotRight:=NotRight+11;',
  21967. ' Self.NotRight:=Self.NotRight+21;',
  21968. ' with Self do begin',
  21969. ' NotRight:=NotRight+31;',
  21970. ' end;',
  21971. 'end;',
  21972. 'begin',
  21973. ' b.Size:=b.Size+11;',
  21974. ' b.Speed:=b.Speed+12;',
  21975. ' b.Left:=b.Left+13;',
  21976. ' b.NotRight:=b.NotRight+14;',
  21977. ' with b do begin',
  21978. ' Size:=Size+31;',
  21979. ' Speed:=Speed+32;',
  21980. ' Left:=Left+33;',
  21981. ' NotRight:=NotRight+34;',
  21982. ' end;',
  21983. '']);
  21984. ConvertProgram;
  21985. CheckSource('TestClassHelper_Property',
  21986. LinesToStr([ // statements
  21987. 'rtl.createClass(this, "TObject", null, function () {',
  21988. ' this.$init = function () {',
  21989. ' this.FSize = 0;',
  21990. ' };',
  21991. ' this.$final = function () {',
  21992. ' };',
  21993. ' this.GetSpeed = function () {',
  21994. ' var Result = 0;',
  21995. ' this.FSize = this.FSize + 11;',
  21996. ' this.SetSpeed(this.GetSpeed() + 12);',
  21997. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21998. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21999. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22000. ' this.FSize = this.FSize + 21;',
  22001. ' this.SetSpeed(this.GetSpeed() + 22);',
  22002. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22003. ' this.FSize = this.FSize + 31;',
  22004. ' this.SetSpeed(this.GetSpeed() + 32);',
  22005. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22006. ' return Result;',
  22007. ' };',
  22008. ' this.SetSpeed = function (Value) {',
  22009. ' };',
  22010. '});',
  22011. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22012. ' this.GetLeft = function () {',
  22013. ' var Result = 0;',
  22014. ' this.FSize = this.FSize + 11;',
  22015. ' this.SetSpeed(this.GetSpeed() + 12);',
  22016. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22017. ' this.FSize = this.FSize + 21;',
  22018. ' this.SetSpeed(this.GetSpeed() + 22);',
  22019. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22020. ' this.FSize = this.FSize + 31;',
  22021. ' this.SetSpeed(this.GetSpeed() + 32);',
  22022. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22023. ' return Result;',
  22024. ' };',
  22025. ' this.SetLeft = function (Value) {',
  22026. ' };',
  22027. '});',
  22028. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22029. ' this.DoIt = function () {',
  22030. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22031. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22032. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22033. ' };',
  22034. '});',
  22035. 'this.b = null;',
  22036. '']),
  22037. LinesToStr([ // $mod.$main
  22038. '$mod.b.FSize = $mod.b.FSize + 11;',
  22039. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  22040. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  22041. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  22042. 'var $with = $mod.b;',
  22043. '$with.FSize = $with.FSize + 31;',
  22044. '$with.SetSpeed($with.GetSpeed() + 32);',
  22045. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  22046. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  22047. '']));
  22048. end;
  22049. procedure TTestModule.TestClassHelper_Property_Array;
  22050. begin
  22051. StartProgram(false);
  22052. Add([
  22053. 'type',
  22054. ' TObject = class',
  22055. ' function GetSpeed(Index: boolean): word;',
  22056. ' procedure SetSpeed(Index: boolean; Value: word);',
  22057. ' end;',
  22058. ' TObjHelper = class helper for TObject',
  22059. ' function GetSize(Index: boolean): word;',
  22060. ' procedure SetSize(Index: boolean; Value: word);',
  22061. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  22062. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22063. ' end;',
  22064. ' TBird = class',
  22065. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  22066. ' procedure DoIt;',
  22067. ' end;',
  22068. 'var',
  22069. ' b: TBird;',
  22070. 'function Tobject.GetSpeed(Index: boolean): word;',
  22071. 'begin',
  22072. ' Result:=Size[false];',
  22073. ' Size[true]:=Size[false]+11;',
  22074. ' Speed[true]:=Speed[false]+12;',
  22075. ' Self.Size[true]:=Self.Size[false]+21;',
  22076. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22077. ' with Self do begin',
  22078. ' Size[true]:=Size[false]+31;',
  22079. ' Speed[true]:=Speed[false]+32;',
  22080. ' end;',
  22081. 'end;',
  22082. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22083. 'begin',
  22084. 'end;',
  22085. 'function TObjHelper.GetSize(Index: boolean): word;',
  22086. 'begin',
  22087. ' Size[true]:=Size[false]+11;',
  22088. ' Speed[true]:=Speed[false]+12;',
  22089. ' Self.Size[true]:=Self.Size[false]+21;',
  22090. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22091. ' with Self do begin',
  22092. ' Size[true]:=Size[false]+31;',
  22093. ' Speed[true]:=Speed[false]+32;',
  22094. ' end;',
  22095. 'end;',
  22096. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22097. 'begin',
  22098. 'end;',
  22099. 'procedure TBird.DoIt;',
  22100. 'begin',
  22101. ' Items[true]:=Items[false]+11;',
  22102. ' Self.Items[true]:=Self.Items[false]+21;',
  22103. ' with Self do Items[true]:=Items[false]+31;',
  22104. 'end;',
  22105. 'begin',
  22106. ' b.Size[true]:=b.Size[false]+11;',
  22107. ' b.Speed[true]:=b.Speed[false]+12;',
  22108. ' b.Items[true]:=b.Items[false]+13;',
  22109. ' with b do begin',
  22110. ' Size[true]:=Size[false]+21;',
  22111. ' Speed[true]:=Speed[false]+22;',
  22112. ' Items[true]:=Items[false]+23;',
  22113. ' end;',
  22114. '']);
  22115. ConvertProgram;
  22116. CheckSource('TestClassHelper_Property_Array',
  22117. LinesToStr([ // statements
  22118. 'rtl.createClass(this, "TObject", null, function () {',
  22119. ' this.$init = function () {',
  22120. ' };',
  22121. ' this.$final = function () {',
  22122. ' };',
  22123. ' this.GetSpeed = function (Index) {',
  22124. ' var Result = 0;',
  22125. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  22126. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22127. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22128. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22129. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22130. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22131. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22132. ' return Result;',
  22133. ' };',
  22134. ' this.SetSpeed = function (Index, Value) {',
  22135. ' };',
  22136. '});',
  22137. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22138. ' this.GetSize = function (Index) {',
  22139. ' var Result = 0;',
  22140. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22141. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22142. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22143. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22144. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22145. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22146. ' return Result;',
  22147. ' };',
  22148. ' this.SetSize = function (Index, Value) {',
  22149. ' };',
  22150. '});',
  22151. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22152. ' this.DoIt = function () {',
  22153. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22154. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22155. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22156. ' };',
  22157. '});',
  22158. 'this.b = null;',
  22159. '']),
  22160. LinesToStr([ // $mod.$main
  22161. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  22162. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  22163. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  22164. 'var $with = $mod.b;',
  22165. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  22166. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  22167. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  22168. '']));
  22169. end;
  22170. procedure TTestModule.TestClassHelper_Property_Array_Default;
  22171. begin
  22172. StartProgram(false);
  22173. Add([
  22174. 'type',
  22175. ' TObject = class',
  22176. ' function GetSpeed(Index: boolean): word;',
  22177. ' procedure SetSpeed(Index: boolean; Value: word);',
  22178. ' end;',
  22179. ' TObjHelper = class helper for TObject',
  22180. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  22181. ' end;',
  22182. ' TBird = class',
  22183. ' end;',
  22184. ' TBirdHelper = class helper for TBird',
  22185. ' function GetSize(Index: word): boolean;',
  22186. ' procedure SetSize(Index: word; Value: boolean);',
  22187. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  22188. ' end;',
  22189. 'function Tobject.GetSpeed(Index: boolean): word;',
  22190. 'begin',
  22191. ' Self[true]:=Self[false]+1;',
  22192. 'end;',
  22193. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22194. 'begin',
  22195. 'end;',
  22196. 'function TBirdHelper.GetSize(Index: word): boolean;',
  22197. 'begin',
  22198. ' Self[1]:=not Self[2];',
  22199. 'end;',
  22200. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  22201. 'begin',
  22202. 'end;',
  22203. 'var',
  22204. ' o: TObject;',
  22205. ' b: TBird;',
  22206. 'begin',
  22207. ' o[true]:=o[false]+1;',
  22208. ' b[3]:=not b[4];',
  22209. '']);
  22210. ConvertProgram;
  22211. CheckSource('TestClassHelper_Property_Array_Default',
  22212. LinesToStr([ // statements
  22213. 'rtl.createClass(this, "TObject", null, function () {',
  22214. ' this.$init = function () {',
  22215. ' };',
  22216. ' this.$final = function () {',
  22217. ' };',
  22218. ' this.GetSpeed = function (Index) {',
  22219. ' var Result = 0;',
  22220. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  22221. ' return Result;',
  22222. ' };',
  22223. ' this.SetSpeed = function (Index, Value) {',
  22224. ' };',
  22225. '});',
  22226. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22227. '});',
  22228. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22229. '});',
  22230. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22231. ' this.GetSize = function (Index) {',
  22232. ' var Result = false;',
  22233. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  22234. ' return Result;',
  22235. ' };',
  22236. ' this.SetSize = function (Index, Value) {',
  22237. ' };',
  22238. '});',
  22239. 'this.o = null;',
  22240. 'this.b = null;',
  22241. '']),
  22242. LinesToStr([ // $mod.$main
  22243. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  22244. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  22245. '']));
  22246. end;
  22247. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  22248. begin
  22249. StartProgram(false);
  22250. Add([
  22251. 'type',
  22252. ' TObject = class',
  22253. ' end;',
  22254. ' TObjHelper = class helper for TObject',
  22255. ' function GetItems(Index: word): TObject;',
  22256. ' procedure SetItems(Index: word; Value: TObject);',
  22257. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  22258. ' end;',
  22259. 'function Tobjhelper.GetItems(Index: word): TObject;',
  22260. 'begin',
  22261. ' Self[1][2]:=Self[3][4];',
  22262. 'end;',
  22263. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  22264. 'begin',
  22265. 'end;',
  22266. 'var',
  22267. ' o: TObject;',
  22268. 'begin',
  22269. ' o[1][2]:=o[3][4];',
  22270. '']);
  22271. ConvertProgram;
  22272. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  22273. LinesToStr([ // statements
  22274. 'rtl.createClass(this, "TObject", null, function () {',
  22275. ' this.$init = function () {',
  22276. ' };',
  22277. ' this.$final = function () {',
  22278. ' };',
  22279. '});',
  22280. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22281. ' this.GetItems = function (Index) {',
  22282. ' var Result = null;',
  22283. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  22284. ' return Result;',
  22285. ' };',
  22286. ' this.SetItems = function (Index, Value) {',
  22287. ' };',
  22288. '});',
  22289. 'this.o = null;',
  22290. '']),
  22291. LinesToStr([ // $mod.$main
  22292. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  22293. '']));
  22294. end;
  22295. procedure TTestModule.TestClassHelper_ClassProperty;
  22296. begin
  22297. StartProgram(false);
  22298. Add([
  22299. 'type',
  22300. ' TObject = class',
  22301. ' class var FSize: word;',
  22302. ' class function GetSpeed: word;',
  22303. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  22304. ' end;',
  22305. ' TObjHelper = class helper for TObject',
  22306. ' class function GetLeft: word;',
  22307. ' class procedure SetLeft(Value: word);',
  22308. ' class property Size: word read FSize write FSize;',
  22309. ' class property Speed: word read GetSpeed write SetSpeed;',
  22310. ' class property Left: word read GetLeft write SetLeft;',
  22311. ' end;',
  22312. ' TBird = class',
  22313. ' class property NotRight: word read GetLeft write SetLeft;',
  22314. ' class procedure DoIt;',
  22315. ' end;',
  22316. ' TBirdClass = class of TBird;',
  22317. 'class function Tobject.GetSpeed: word;',
  22318. 'begin',
  22319. ' Size:=Size+11;',
  22320. ' Speed:=Speed+12;',
  22321. ' Left:=Left+13;',
  22322. ' Self.Size:=Self.Size+21;',
  22323. ' Self.Speed:=Self.Speed+22;',
  22324. ' Self.Left:=Self.Left+23;',
  22325. ' with Self do begin',
  22326. ' Size:=Size+31;',
  22327. ' Speed:=Speed+32;',
  22328. ' Left:=Left+33;',
  22329. ' end;',
  22330. 'end;',
  22331. 'class function TObjHelper.GetLeft: word;',
  22332. 'begin',
  22333. ' Size:=Size+11;',
  22334. ' Speed:=Speed+12;',
  22335. ' Left:=Left+13;',
  22336. ' Self.Size:=Self.Size+21;',
  22337. ' Self.Speed:=Self.Speed+22;',
  22338. ' Self.Left:=Self.Left+23;',
  22339. ' with Self do begin',
  22340. ' Size:=Size+31;',
  22341. ' Speed:=Speed+32;',
  22342. ' Left:=Left+33;',
  22343. ' end;',
  22344. 'end;',
  22345. 'class procedure TObjHelper.SetLeft(Value: word);',
  22346. 'begin',
  22347. 'end;',
  22348. 'class procedure TBird.DoIt;',
  22349. 'begin',
  22350. ' NotRight:=NotRight+11;',
  22351. ' Self.NotRight:=Self.NotRight+21;',
  22352. ' with Self do NotRight:=NotRight+31;',
  22353. 'end;',
  22354. 'var',
  22355. ' b: TBird;',
  22356. ' c: TBirdClass;',
  22357. 'begin',
  22358. ' b.Size:=b.Size+11;',
  22359. ' b.Speed:=b.Speed+12;',
  22360. ' b.Left:=b.Left+13;',
  22361. ' b.NotRight:=b.NotRight+14;',
  22362. ' with b do begin',
  22363. ' Size:=Size+31;',
  22364. ' Speed:=Speed+32;',
  22365. ' Left:=Left+33;',
  22366. ' NotRight:=NotRight+34;',
  22367. ' end;',
  22368. ' c.Size:=c.Size+11;',
  22369. ' c.Speed:=c.Speed+12;',
  22370. ' c.Left:=c.Left+13;',
  22371. ' c.NotRight:=c.NotRight+14;',
  22372. ' with c do begin',
  22373. ' Size:=Size+31;',
  22374. ' Speed:=Speed+32;',
  22375. ' Left:=Left+33;',
  22376. ' NotRight:=NotRight+34;',
  22377. ' end;',
  22378. ' tbird.Size:=tbird.Size+11;',
  22379. ' tbird.Speed:=tbird.Speed+12;',
  22380. ' tbird.Left:=tbird.Left+13;',
  22381. ' tbird.NotRight:=tbird.NotRight+14;',
  22382. ' with tbird do begin',
  22383. ' Size:=Size+31;',
  22384. ' Speed:=Speed+32;',
  22385. ' Left:=Left+33;',
  22386. ' NotRight:=NotRight+34;',
  22387. ' end;',
  22388. '']);
  22389. ConvertProgram;
  22390. CheckSource('TestClassHelper_ClassProperty',
  22391. LinesToStr([ // statements
  22392. 'rtl.createClass(this, "TObject", null, function () {',
  22393. ' this.FSize = 0;',
  22394. ' this.$init = function () {',
  22395. ' };',
  22396. ' this.$final = function () {',
  22397. ' };',
  22398. ' this.GetSpeed = function () {',
  22399. ' var Result = 0;',
  22400. ' $mod.TObject.FSize = this.FSize + 11;',
  22401. ' this.SetSpeed(this.GetSpeed() + 12);',
  22402. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22403. ' $mod.TObject.FSize = this.FSize + 21;',
  22404. ' this.SetSpeed(this.GetSpeed() + 22);',
  22405. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22406. ' $mod.TObject.FSize = this.FSize + 31;',
  22407. ' this.SetSpeed(this.GetSpeed() + 32);',
  22408. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22409. ' return Result;',
  22410. ' };',
  22411. '});',
  22412. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22413. ' this.GetLeft = function () {',
  22414. ' var Result = 0;',
  22415. ' $mod.TObject.FSize = this.FSize + 11;',
  22416. ' this.SetSpeed(this.GetSpeed() + 12);',
  22417. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22418. ' $mod.TObject.FSize = this.FSize + 21;',
  22419. ' this.SetSpeed(this.GetSpeed() + 22);',
  22420. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22421. ' $mod.TObject.FSize = this.FSize + 31;',
  22422. ' this.SetSpeed(this.GetSpeed() + 32);',
  22423. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22424. ' return Result;',
  22425. ' };',
  22426. ' this.SetLeft = function (Value) {',
  22427. ' };',
  22428. '});',
  22429. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22430. ' this.DoIt = function () {',
  22431. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22432. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22433. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22434. ' };',
  22435. '});',
  22436. 'this.b = null;',
  22437. 'this.c = null;',
  22438. '']),
  22439. LinesToStr([ // $mod.$main
  22440. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  22441. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  22442. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22443. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22444. 'var $with = $mod.b;',
  22445. '$mod.TObject.FSize = $with.FSize + 31;',
  22446. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22447. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22448. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22449. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22450. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22451. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22452. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22453. 'var $with1 = $mod.c;',
  22454. '$mod.TObject.FSize = $with1.FSize + 31;',
  22455. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22456. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22457. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22458. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22459. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22460. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22461. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22462. 'var $with2 = $mod.TBird;',
  22463. '$mod.TObject.FSize = $with2.FSize + 31;',
  22464. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22465. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22466. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22467. '']));
  22468. end;
  22469. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22470. begin
  22471. StartProgram(false);
  22472. Add([
  22473. 'type',
  22474. ' TObject = class',
  22475. ' class function GetSpeed: word; static;',
  22476. ' class procedure SetSpeed(Value: word); static;',
  22477. ' end;',
  22478. ' TObjHelper = class helper for TObject',
  22479. ' class function GetLeft: word; static;',
  22480. ' class procedure SetLeft(Value: word); static;',
  22481. ' class property Speed: word read GetSpeed write SetSpeed;',
  22482. ' class property Left: word read GetLeft write SetLeft;',
  22483. ' end;',
  22484. ' TBird = class',
  22485. ' class property NotRight: word read GetLeft write SetLeft;',
  22486. ' class procedure DoIt; static;',
  22487. ' class procedure DoSome;',
  22488. ' end;',
  22489. ' TBirdClass = class of TBird;',
  22490. 'class function Tobject.GetSpeed: word;',
  22491. 'begin',
  22492. ' Speed:=Speed+12;',
  22493. ' Left:=Left+13;',
  22494. 'end;',
  22495. 'class procedure TObject.SetSpeed(Value: word);',
  22496. 'begin',
  22497. 'end;',
  22498. 'class function TObjHelper.GetLeft: word;',
  22499. 'begin',
  22500. ' Speed:=Speed+12;',
  22501. ' Left:=Left+13;',
  22502. 'end;',
  22503. 'class procedure TObjHelper.SetLeft(Value: word);',
  22504. 'begin',
  22505. 'end;',
  22506. 'class procedure TBird.DoIt;',
  22507. 'begin',
  22508. ' NotRight:=NotRight+11;',
  22509. 'end;',
  22510. 'class procedure TBird.DoSome;',
  22511. 'begin',
  22512. ' Speed:=Speed+12;',
  22513. ' Left:=Left+13;',
  22514. ' Self.Speed:=Self.Speed+22;',
  22515. ' Self.Left:=Self.Left+23;',
  22516. ' with Self do begin',
  22517. ' Speed:=Speed+32;',
  22518. ' Left:=Left+33;',
  22519. ' end;',
  22520. ' NotRight:=NotRight+11;',
  22521. ' Self.NotRight:=Self.NotRight+21;',
  22522. ' with Self do NotRight:=NotRight+31;',
  22523. 'end;',
  22524. 'var',
  22525. ' b: TBird;',
  22526. ' c: TBirdClass;',
  22527. 'begin',
  22528. ' b.Speed:=b.Speed+12;',
  22529. ' b.Left:=b.Left+13;',
  22530. ' b.NotRight:=b.NotRight+14;',
  22531. ' with b do begin',
  22532. ' Speed:=Speed+32;',
  22533. ' Left:=Left+33;',
  22534. ' NotRight:=NotRight+34;',
  22535. ' end;',
  22536. ' c.Speed:=c.Speed+12;',
  22537. ' c.Left:=c.Left+13;',
  22538. ' c.NotRight:=c.NotRight+14;',
  22539. ' with c do begin',
  22540. ' Speed:=Speed+32;',
  22541. ' Left:=Left+33;',
  22542. ' NotRight:=NotRight+34;',
  22543. ' end;',
  22544. ' tbird.Speed:=tbird.Speed+12;',
  22545. ' tbird.Left:=tbird.Left+13;',
  22546. ' tbird.NotRight:=tbird.NotRight+14;',
  22547. ' with tbird do begin',
  22548. ' Speed:=Speed+32;',
  22549. ' Left:=Left+33;',
  22550. ' NotRight:=NotRight+34;',
  22551. ' end;',
  22552. '']);
  22553. ConvertProgram;
  22554. CheckSource('TestClassHelper_ClassPropertyStatic',
  22555. LinesToStr([ // statements
  22556. 'rtl.createClass(this, "TObject", null, function () {',
  22557. ' this.$init = function () {',
  22558. ' };',
  22559. ' this.$final = function () {',
  22560. ' };',
  22561. ' this.GetSpeed = function () {',
  22562. ' var Result = 0;',
  22563. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22564. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22565. ' return Result;',
  22566. ' };',
  22567. ' this.SetSpeed = function (Value) {',
  22568. ' };',
  22569. '});',
  22570. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22571. ' this.GetLeft = function () {',
  22572. ' var Result = 0;',
  22573. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22574. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22575. ' return Result;',
  22576. ' };',
  22577. ' this.SetLeft = function (Value) {',
  22578. ' };',
  22579. '});',
  22580. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22581. ' this.DoIt = function () {',
  22582. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22583. ' };',
  22584. ' this.DoSome = function () {',
  22585. ' this.SetSpeed(this.GetSpeed() + 12);',
  22586. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22587. ' this.SetSpeed(this.GetSpeed() + 22);',
  22588. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22589. ' this.SetSpeed(this.GetSpeed() + 32);',
  22590. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22591. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22592. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22593. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22594. ' };',
  22595. '});',
  22596. 'this.b = null;',
  22597. 'this.c = null;',
  22598. '']),
  22599. LinesToStr([ // $mod.$main
  22600. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22601. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22602. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22603. 'var $with = $mod.b;',
  22604. '$with.SetSpeed($with.GetSpeed() + 32);',
  22605. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22606. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22607. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22608. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22609. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22610. 'var $with1 = $mod.c;',
  22611. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22612. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22613. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22614. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22615. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22616. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22617. 'var $with2 = $mod.TBird;',
  22618. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22619. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22620. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22621. '']));
  22622. end;
  22623. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22624. begin
  22625. StartProgram(false);
  22626. Add([
  22627. 'type',
  22628. ' TObject = class',
  22629. ' class function GetSpeed(Index: boolean): word;',
  22630. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22631. ' end;',
  22632. ' TObjHelper = class helper for TObject',
  22633. ' class function GetSize(Index: boolean): word;',
  22634. ' class procedure SetSize(Index: boolean; Value: word);',
  22635. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22636. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22637. ' end;',
  22638. ' TBird = class',
  22639. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22640. ' class procedure DoIt;',
  22641. ' end;',
  22642. ' TBirdClass = class of TBird;',
  22643. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22644. 'begin',
  22645. ' Size[true]:=Size[false]+11;',
  22646. ' Speed[true]:=Speed[false]+12;',
  22647. ' Self.Size[true]:=Self.Size[false]+21;',
  22648. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22649. ' with Self do begin',
  22650. ' Size[true]:=Size[false]+31;',
  22651. ' Speed[true]:=Speed[false]+32;',
  22652. ' end;',
  22653. 'end;',
  22654. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22655. 'begin',
  22656. ' Size[true]:=Size[false]+11;',
  22657. ' Speed[true]:=Speed[false]+12;',
  22658. ' Self.Size[true]:=Self.Size[false]+21;',
  22659. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22660. ' with Self do begin',
  22661. ' Size[true]:=Size[false]+31;',
  22662. ' Speed[true]:=Speed[false]+32;',
  22663. ' end;',
  22664. 'end;',
  22665. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22666. 'begin',
  22667. 'end;',
  22668. 'class procedure TBird.DoIt;',
  22669. 'begin',
  22670. ' Items[true]:=Items[false]+11;',
  22671. ' Self.Items[true]:=Self.Items[false]+21;',
  22672. ' with Self do Items[true]:=Items[false]+31;',
  22673. 'end;',
  22674. 'var',
  22675. ' b: TBird;',
  22676. ' c: TBirdClass;',
  22677. 'begin',
  22678. ' b.Size[true]:=b.Size[false]+11;',
  22679. ' b.Speed[true]:=b.Speed[false]+12;',
  22680. ' b.Items[true]:=b.Items[false]+13;',
  22681. ' with b do begin',
  22682. ' Size[true]:=Size[false]+21;',
  22683. ' Speed[true]:=Speed[false]+22;',
  22684. ' Items[true]:=Items[false]+23;',
  22685. ' end;',
  22686. ' c.Size[true]:=c.Size[false]+11;',
  22687. ' c.Speed[true]:=c.Speed[false]+12;',
  22688. ' c.Items[true]:=c.Items[false]+13;',
  22689. ' with c do begin',
  22690. ' Size[true]:=Size[false]+21;',
  22691. ' Speed[true]:=Speed[false]+22;',
  22692. ' Items[true]:=Items[false]+23;',
  22693. ' end;',
  22694. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22695. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22696. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22697. ' with TBird do begin',
  22698. ' Size[true]:=Size[false]+21;',
  22699. ' Speed[true]:=Speed[false]+22;',
  22700. ' Items[true]:=Items[false]+23;',
  22701. ' end;',
  22702. '']);
  22703. ConvertProgram;
  22704. CheckSource('TestClassHelper_ClassProperty_Array',
  22705. LinesToStr([ // statements
  22706. 'rtl.createClass(this, "TObject", null, function () {',
  22707. ' this.$init = function () {',
  22708. ' };',
  22709. ' this.$final = function () {',
  22710. ' };',
  22711. ' this.GetSpeed = function (Index) {',
  22712. ' var Result = 0;',
  22713. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22714. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22715. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22716. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22717. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22718. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22719. ' return Result;',
  22720. ' };',
  22721. '});',
  22722. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22723. ' this.GetSize = function (Index) {',
  22724. ' var Result = 0;',
  22725. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22726. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22727. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22728. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22729. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22730. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22731. ' return Result;',
  22732. ' };',
  22733. ' this.SetSize = function (Index, Value) {',
  22734. ' };',
  22735. '});',
  22736. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22737. ' this.DoIt = function () {',
  22738. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22739. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22740. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22741. ' };',
  22742. '});',
  22743. 'this.b = null;',
  22744. 'this.c = null;',
  22745. '']),
  22746. LinesToStr([ // $mod.$main
  22747. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22748. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22749. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22750. 'var $with = $mod.b;',
  22751. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22752. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22753. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22754. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22755. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22756. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22757. 'var $with1 = $mod.c;',
  22758. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22759. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22760. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22761. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22762. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22763. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22764. 'var $with2 = $mod.TBird;',
  22765. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22766. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22767. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22768. '']));
  22769. end;
  22770. procedure TTestModule.TestClassHelper_ForIn;
  22771. begin
  22772. StartProgram(false);
  22773. Add([
  22774. 'type',
  22775. ' TObject = class end;',
  22776. ' TItem = TObject;',
  22777. ' TEnumerator = class',
  22778. ' FCurrent: TItem;',
  22779. ' property Current: TItem read FCurrent;',
  22780. ' function MoveNext: boolean;',
  22781. ' end;',
  22782. ' TBird = class',
  22783. ' end;',
  22784. ' TBirdHelper = class helper for TBird',
  22785. ' function GetEnumerator: TEnumerator;',
  22786. ' end;',
  22787. 'function TEnumerator.MoveNext: boolean;',
  22788. 'begin',
  22789. 'end;',
  22790. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22791. 'begin',
  22792. 'end;',
  22793. 'var',
  22794. ' b: TBird;',
  22795. ' i, i2: TItem;',
  22796. 'begin',
  22797. ' for i in b do i2:=i;']);
  22798. ConvertProgram;
  22799. CheckSource('TestClassHelper_ForIn',
  22800. LinesToStr([ // statements
  22801. 'rtl.createClass(this, "TObject", null, function () {',
  22802. ' this.$init = function () {',
  22803. ' };',
  22804. ' this.$final = function () {',
  22805. ' };',
  22806. '});',
  22807. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22808. ' this.$init = function () {',
  22809. ' $mod.TObject.$init.call(this);',
  22810. ' this.FCurrent = null;',
  22811. ' };',
  22812. ' this.$final = function () {',
  22813. ' this.FCurrent = undefined;',
  22814. ' $mod.TObject.$final.call(this);',
  22815. ' };',
  22816. ' this.MoveNext = function () {',
  22817. ' var Result = false;',
  22818. ' return Result;',
  22819. ' };',
  22820. '});',
  22821. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22822. '});',
  22823. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22824. ' this.GetEnumerator = function () {',
  22825. ' var Result = null;',
  22826. ' return Result;',
  22827. ' };',
  22828. '});',
  22829. 'this.b = null;',
  22830. 'this.i = null;',
  22831. 'this.i2 = null;'
  22832. ]),
  22833. LinesToStr([ // $mod.$main
  22834. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22835. 'try {',
  22836. ' while ($in.MoveNext()){',
  22837. ' $mod.i = $in.FCurrent;',
  22838. ' $mod.i2 = $mod.i;',
  22839. ' }',
  22840. '} finally {',
  22841. ' $in = rtl.freeLoc($in)',
  22842. '};',
  22843. '']));
  22844. end;
  22845. procedure TTestModule.TestClassHelper_PassProperty;
  22846. begin
  22847. StartProgram(false);
  22848. Add([
  22849. 'type',
  22850. ' TObject = class',
  22851. ' FField: TObject;',
  22852. ' property Field: TObject read FField write FField;',
  22853. ' end;',
  22854. ' THelper = class helper for TObject',
  22855. ' procedure Fly;',
  22856. ' class procedure Run;',
  22857. ' class procedure Jump; static;',
  22858. ' end;',
  22859. 'procedure THelper.Fly;',
  22860. 'begin',
  22861. ' Field.Fly;',
  22862. ' Field.Run;',
  22863. ' Field.Jump;',
  22864. ' with Field do begin',
  22865. ' Fly;',
  22866. ' Run;',
  22867. ' Jump;',
  22868. ' end;',
  22869. 'end;',
  22870. 'class procedure THelper.Run;',
  22871. 'begin',
  22872. 'end;',
  22873. 'class procedure THelper.Jump;',
  22874. 'begin',
  22875. 'end;',
  22876. 'var',
  22877. ' b: TObject;',
  22878. 'begin',
  22879. ' b.Field.Fly;',
  22880. ' b.Field.Run;',
  22881. ' b.Field.Jump;',
  22882. ' with b do begin',
  22883. ' Field.Run;',
  22884. ' Field.Fly;',
  22885. ' Field.Jump;',
  22886. ' end;',
  22887. ' with b.Field do begin',
  22888. ' Run;',
  22889. ' Fly;',
  22890. ' Jump;',
  22891. ' end;',
  22892. '']);
  22893. ConvertProgram;
  22894. CheckSource('TestClassHelper_PassProperty',
  22895. LinesToStr([ // statements
  22896. 'rtl.createClass(this, "TObject", null, function () {',
  22897. ' this.$init = function () {',
  22898. ' this.FField = null;',
  22899. ' };',
  22900. ' this.$final = function () {',
  22901. ' this.FField = undefined;',
  22902. ' };',
  22903. '});',
  22904. 'rtl.createHelper(this, "THelper", null, function () {',
  22905. ' this.Fly = function () {',
  22906. ' $mod.THelper.Fly.call(this.FField);',
  22907. ' $mod.THelper.Run.call(this.FField.$class);',
  22908. ' $mod.THelper.Jump();',
  22909. ' var $with = this.FField;',
  22910. ' $mod.THelper.Fly.call($with);',
  22911. ' $mod.THelper.Run.call($with.$class);',
  22912. ' $mod.THelper.Jump();',
  22913. ' };',
  22914. ' this.Run = function () {',
  22915. ' };',
  22916. ' this.Jump = function () {',
  22917. ' };',
  22918. '});',
  22919. 'this.b = null;',
  22920. '']),
  22921. LinesToStr([ // $mod.$main
  22922. '$mod.THelper.Fly.call($mod.b.FField);',
  22923. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22924. '$mod.THelper.Jump();',
  22925. 'var $with = $mod.b;',
  22926. '$mod.THelper.Run.call($with.FField.$class);',
  22927. '$mod.THelper.Fly.call($with.FField);',
  22928. '$mod.THelper.Jump();',
  22929. 'var $with1 = $mod.b.FField;',
  22930. '$mod.THelper.Run.call($with1.$class);',
  22931. '$mod.THelper.Fly.call($with1);',
  22932. '$mod.THelper.Jump();',
  22933. '']));
  22934. end;
  22935. procedure TTestModule.TestExtClassHelper_ClassVar;
  22936. begin
  22937. StartProgram(false);
  22938. Add([
  22939. '{$modeswitch externalclass}',
  22940. 'type',
  22941. ' TExtA = class external name ''ExtObj''',
  22942. ' end;',
  22943. ' THelper = class helper for TExtA',
  22944. ' const',
  22945. ' One = 1;',
  22946. ' Two: word = 2;',
  22947. ' class var',
  22948. ' Glob: word;',
  22949. ' function Foo(w: word): word;',
  22950. ' class function Bar(w: word): word; static;',
  22951. ' end;',
  22952. 'function THelper.foo(w: word): word;',
  22953. 'begin',
  22954. ' Result:=w;',
  22955. ' Two:=One+w;',
  22956. ' Glob:=Glob;',
  22957. ' Result:=Self.Glob;',
  22958. ' Self.Glob:=Self.Glob;',
  22959. ' with Self do Glob:=Glob;',
  22960. 'end;',
  22961. 'class function THelper.bar(w: word): word;',
  22962. 'begin',
  22963. ' Result:=w;',
  22964. ' Two:=One;',
  22965. ' Glob:=Glob;',
  22966. 'end;',
  22967. 'var o: TExtA;',
  22968. 'begin',
  22969. ' texta.two:=texta.one;',
  22970. ' texta.Glob:=texta.Glob;',
  22971. ' with texta do begin',
  22972. ' two:=one;',
  22973. ' Glob:=Glob;',
  22974. ' end;',
  22975. ' o.two:=o.one;',
  22976. ' o.Glob:=o.Glob;',
  22977. ' with o do begin',
  22978. ' two:=one;',
  22979. ' Glob:=Glob;',
  22980. ' end;',
  22981. '']);
  22982. ConvertProgram;
  22983. CheckSource('TestExtClassHelper_ClassVar',
  22984. LinesToStr([ // statements
  22985. 'rtl.createHelper(this, "THelper", null, function () {',
  22986. ' this.One = 1;',
  22987. ' this.Two = 2;',
  22988. ' this.Glob = 0;',
  22989. ' this.Foo = function (w) {',
  22990. ' var Result = 0;',
  22991. ' Result = w;',
  22992. ' $mod.THelper.Two = 1 + w;',
  22993. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22994. ' Result = $mod.THelper.Glob;',
  22995. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22996. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22997. ' return Result;',
  22998. ' };',
  22999. ' this.Bar = function (w) {',
  23000. ' var Result = 0;',
  23001. ' Result = w;',
  23002. ' $mod.THelper.Two = 1;',
  23003. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23004. ' return Result;',
  23005. ' };',
  23006. '});',
  23007. 'this.o = null;',
  23008. '']),
  23009. LinesToStr([ // $mod.$main
  23010. '$mod.THelper.Two = 1;',
  23011. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23012. '$mod.THelper.Two = 1;',
  23013. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23014. '$mod.THelper.Two = 1;',
  23015. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23016. 'var $with = $mod.o;',
  23017. '$mod.THelper.Two = 1;',
  23018. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23019. '']));
  23020. end;
  23021. procedure TTestModule.TestExtClassHelper_Method_Call;
  23022. begin
  23023. StartProgram(false);
  23024. Add([
  23025. '{$modeswitch externalclass}',
  23026. 'type',
  23027. ' TFly = function(w: word): word of object;',
  23028. ' TExtA = class external name ''ExtObj''',
  23029. ' procedure Run(w: word = 10);',
  23030. ' end;',
  23031. ' THelper = class helper for TExtA',
  23032. ' function Foo(w: word = 1): word;',
  23033. ' function Fly(w: word = 2): word; external name ''Fly'';',
  23034. ' end;',
  23035. 'var p: TFly;',
  23036. 'function THelper.foo(w: word): word;',
  23037. 'begin',
  23038. ' Run;',
  23039. ' Run();',
  23040. ' Run(11);',
  23041. ' Foo;',
  23042. ' Foo();',
  23043. ' Foo(12);',
  23044. ' Self.Foo;',
  23045. ' Self.Foo();',
  23046. ' Self.Foo(13);',
  23047. ' Fly;',
  23048. ' Fly();',
  23049. ' with Self do begin',
  23050. ' Foo;',
  23051. ' Foo();',
  23052. ' Foo(14);',
  23053. ' Fly;',
  23054. ' Fly();',
  23055. ' end;',
  23056. ' p:=@Fly;',
  23057. 'end;',
  23058. 'var Obj: TExtA;',
  23059. 'begin',
  23060. ' obj.Foo;',
  23061. ' obj.Foo();',
  23062. ' obj.Foo(21);',
  23063. ' obj.Fly;',
  23064. ' obj.Fly();',
  23065. ' with obj do begin',
  23066. ' Foo;',
  23067. ' Foo();',
  23068. ' Foo(22);',
  23069. ' Fly;',
  23070. ' Fly();',
  23071. ' end;',
  23072. ' p:[email protected];',
  23073. '']);
  23074. ConvertProgram;
  23075. CheckSource('TestExtClassHelper_Method_Call',
  23076. LinesToStr([ // statements
  23077. 'rtl.createHelper(this, "THelper", null, function () {',
  23078. ' this.Foo = function (w) {',
  23079. ' var Result = 0;',
  23080. ' this.Run(10);',
  23081. ' this.Run(10);',
  23082. ' this.Run(11);',
  23083. ' $mod.THelper.Foo.call(this, 1);',
  23084. ' $mod.THelper.Foo.call(this, 1);',
  23085. ' $mod.THelper.Foo.call(this, 12);',
  23086. ' $mod.THelper.Foo.call(this, 1);',
  23087. ' $mod.THelper.Foo.call(this, 1);',
  23088. ' $mod.THelper.Foo.call(this, 13);',
  23089. ' this.Fly(2);',
  23090. ' this.Fly(2);',
  23091. ' $mod.THelper.Foo.call(this, 1);',
  23092. ' $mod.THelper.Foo.call(this, 1);',
  23093. ' $mod.THelper.Foo.call(this, 14);',
  23094. ' this.Fly(2);',
  23095. ' this.Fly(2);',
  23096. ' $mod.p = rtl.createCallback(this, "Fly");',
  23097. ' return Result;',
  23098. ' };',
  23099. '});',
  23100. 'this.p = null;',
  23101. 'this.Obj = null;',
  23102. '']),
  23103. LinesToStr([ // $mod.$main
  23104. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23105. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23106. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23107. '$mod.Obj.Fly(2);',
  23108. '$mod.Obj.Fly(2);',
  23109. 'var $with = $mod.Obj;',
  23110. '$mod.THelper.Foo.call($with, 1);',
  23111. '$mod.THelper.Foo.call($with, 1);',
  23112. '$mod.THelper.Foo.call($with, 22);',
  23113. '$with.Fly(2);',
  23114. '$with.Fly(2);',
  23115. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  23116. '']));
  23117. end;
  23118. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  23119. begin
  23120. StartProgram(false);
  23121. Add([
  23122. '{$modeswitch externalclass}',
  23123. 'type',
  23124. ' TExtA = class external name ''ExtObj''',
  23125. ' procedure Run(w: word = 10);',
  23126. ' end;',
  23127. ' THelper = class helper for TExtA',
  23128. ' class procedure Fly;',
  23129. ' end;',
  23130. 'class procedure THelper.Fly;',
  23131. 'begin end;',
  23132. 'begin',
  23133. '']);
  23134. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  23135. nHelperClassMethodForExtClassMustBeStatic);
  23136. ConvertProgram;
  23137. end;
  23138. procedure TTestModule.TestRecordHelper_ClassVar;
  23139. begin
  23140. StartProgram(false);
  23141. Add([
  23142. 'type',
  23143. ' TRec = record',
  23144. ' end;',
  23145. ' THelper = record helper for TRec',
  23146. ' const',
  23147. ' One = 1;',
  23148. ' Two: word = 2;',
  23149. ' class var',
  23150. ' Glob: word;',
  23151. ' function Foo(w: word): word;',
  23152. ' class function Bar(w: word): word; static;',
  23153. ' end;',
  23154. 'function THelper.foo(w: word): word;',
  23155. 'begin',
  23156. ' Result:=w;',
  23157. ' Two:=One+w;',
  23158. ' Glob:=Glob;',
  23159. ' Result:=Self.Glob;',
  23160. ' Self.Glob:=Self.Glob;',
  23161. ' with Self do Glob:=Glob;',
  23162. ' Self:=Self;',
  23163. 'end;',
  23164. 'class function THelper.bar(w: word): word;',
  23165. 'begin',
  23166. ' Result:=w;',
  23167. ' Two:=One;',
  23168. ' Glob:=Glob;',
  23169. 'end;',
  23170. 'var r: TRec;',
  23171. 'begin',
  23172. ' trec.two:=trec.one;',
  23173. ' trec.Glob:=trec.Glob;',
  23174. ' with trec do begin',
  23175. ' two:=one;',
  23176. ' Glob:=Glob;',
  23177. ' end;',
  23178. ' r.two:=r.one;',
  23179. ' r.Glob:=r.Glob;',
  23180. ' with r do begin',
  23181. ' two:=one;',
  23182. ' Glob:=Glob;',
  23183. ' end;',
  23184. '']);
  23185. ConvertProgram;
  23186. CheckSource('TestRecordHelper_ClassVar',
  23187. LinesToStr([ // statements
  23188. 'rtl.recNewT(this, "TRec", function () {',
  23189. ' this.$eq = function (b) {',
  23190. ' return true;',
  23191. ' };',
  23192. ' this.$assign = function (s) {',
  23193. ' return this;',
  23194. ' };',
  23195. '});',
  23196. 'rtl.createHelper(this, "THelper", null, function () {',
  23197. ' this.One = 1;',
  23198. ' this.Two = 2;',
  23199. ' this.Glob = 0;',
  23200. ' this.Foo = function (w) {',
  23201. ' var Result = 0;',
  23202. ' Result = w;',
  23203. ' $mod.THelper.Two = 1 + w;',
  23204. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23205. ' Result = $mod.THelper.Glob;',
  23206. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23207. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23208. ' this.$assign(this);',
  23209. ' return Result;',
  23210. ' };',
  23211. ' this.Bar = function (w) {',
  23212. ' var Result = 0;',
  23213. ' Result = w;',
  23214. ' $mod.THelper.Two = 1;',
  23215. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23216. ' return Result;',
  23217. ' };',
  23218. '});',
  23219. 'this.r = this.TRec.$new();',
  23220. '']),
  23221. LinesToStr([ // $mod.$main
  23222. '$mod.THelper.Two = 1;',
  23223. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23224. 'var $with = $mod.TRec;',
  23225. '$mod.THelper.Two = 1;',
  23226. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23227. '$mod.THelper.Two = 1;',
  23228. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23229. 'var $with1 = $mod.r;',
  23230. '$mod.THelper.Two = 1;',
  23231. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23232. '']));
  23233. end;
  23234. procedure TTestModule.TestRecordHelper_Method_Call;
  23235. begin
  23236. StartProgram(false);
  23237. Add([
  23238. '{$modeswitch AdvancedRecords}',
  23239. 'type',
  23240. ' TRec = record',
  23241. ' procedure Run(w: word = 10);',
  23242. ' end;',
  23243. ' THelper = record helper for TRec',
  23244. ' function Foo(w: word = 1): word;',
  23245. ' end;',
  23246. 'procedure TRec.Run(w: word);',
  23247. 'begin',
  23248. ' Foo;',
  23249. ' Foo();',
  23250. ' Foo(2);',
  23251. ' Self.Foo;',
  23252. ' Self.Foo();',
  23253. ' Self.Foo(3);',
  23254. ' with Self do begin',
  23255. ' Foo;',
  23256. ' Foo();',
  23257. ' Foo(4);',
  23258. ' end;',
  23259. 'end;',
  23260. 'function THelper.foo(w: word): word;',
  23261. 'begin',
  23262. ' Run;',
  23263. ' Run();',
  23264. ' Run(11);',
  23265. ' Foo;',
  23266. ' Foo();',
  23267. ' Foo(12);',
  23268. ' Self.Foo;',
  23269. ' Self.Foo();',
  23270. ' Self.Foo(13);',
  23271. ' with Self do begin',
  23272. ' Foo;',
  23273. ' Foo();',
  23274. ' Foo(14);',
  23275. ' end;',
  23276. 'end;',
  23277. 'var Rec: TRec;',
  23278. 'begin',
  23279. ' Rec.Foo;',
  23280. ' Rec.Foo();',
  23281. ' Rec.Foo(21);',
  23282. ' with Rec do begin',
  23283. ' Foo;',
  23284. ' Foo();',
  23285. ' Foo(22);',
  23286. ' end;',
  23287. '']);
  23288. ConvertProgram;
  23289. CheckSource('TestRecordHelper_Method_Call',
  23290. LinesToStr([ // statements
  23291. 'rtl.recNewT(this, "TRec", function () {',
  23292. ' this.$eq = function (b) {',
  23293. ' return true;',
  23294. ' };',
  23295. ' this.$assign = function (s) {',
  23296. ' return this;',
  23297. ' };',
  23298. ' this.Run = function (w) {',
  23299. ' $mod.THelper.Foo.call(this, 1);',
  23300. ' $mod.THelper.Foo.call(this, 1);',
  23301. ' $mod.THelper.Foo.call(this, 2);',
  23302. ' $mod.THelper.Foo.call(this, 1);',
  23303. ' $mod.THelper.Foo.call(this, 1);',
  23304. ' $mod.THelper.Foo.call(this, 3);',
  23305. ' $mod.THelper.Foo.call(this, 1);',
  23306. ' $mod.THelper.Foo.call(this, 1);',
  23307. ' $mod.THelper.Foo.call(this, 4);',
  23308. ' };',
  23309. '});',
  23310. 'rtl.createHelper(this, "THelper", null, function () {',
  23311. ' this.Foo = function (w) {',
  23312. ' var Result = 0;',
  23313. ' this.Run(10);',
  23314. ' this.Run(10);',
  23315. ' this.Run(11);',
  23316. ' $mod.THelper.Foo.call(this, 1);',
  23317. ' $mod.THelper.Foo.call(this, 1);',
  23318. ' $mod.THelper.Foo.call(this, 12);',
  23319. ' $mod.THelper.Foo.call(this, 1);',
  23320. ' $mod.THelper.Foo.call(this, 1);',
  23321. ' $mod.THelper.Foo.call(this, 13);',
  23322. ' $mod.THelper.Foo.call(this, 1);',
  23323. ' $mod.THelper.Foo.call(this, 1);',
  23324. ' $mod.THelper.Foo.call(this, 14);',
  23325. ' return Result;',
  23326. ' };',
  23327. '});',
  23328. 'this.Rec = this.TRec.$new();',
  23329. '']),
  23330. LinesToStr([ // $mod.$main
  23331. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23332. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23333. '$mod.THelper.Foo.call($mod.Rec, 21);',
  23334. 'var $with = $mod.Rec;',
  23335. '$mod.THelper.Foo.call($with, 1);',
  23336. '$mod.THelper.Foo.call($with, 1);',
  23337. '$mod.THelper.Foo.call($with, 22);',
  23338. '']));
  23339. end;
  23340. procedure TTestModule.TestRecordHelper_Constructor;
  23341. begin
  23342. StartProgram(false);
  23343. Add([
  23344. '{$modeswitch AdvancedRecords}',
  23345. 'type',
  23346. ' TRec = record',
  23347. ' constructor Create(w: word);',
  23348. ' end;',
  23349. ' THelper = record helper for TRec',
  23350. ' constructor NewHlp(w: word);',
  23351. ' end;',
  23352. 'var',
  23353. ' Rec: TRec;',
  23354. 'constructor TRec.Create(w: word);',
  23355. 'begin',
  23356. ' NewHlp(2);', // normal call
  23357. ' trec.NewHlp(3);', // new instance
  23358. 'end;',
  23359. 'constructor THelper.NewHlp(w: word);',
  23360. 'begin',
  23361. ' create(2);', // normal call
  23362. ' trec.create(3);', // new instance
  23363. ' NewHlp(4);', // normal call
  23364. ' trec.NewHlp(5);', // new instance
  23365. 'end;',
  23366. 'begin',
  23367. ' rec.newhlp(2);', // normal call
  23368. ' with rec do newhlp(12);', // normal call
  23369. ' trec.newhlp(3);', // new instance
  23370. ' with trec do newhlp(13);', // new instance
  23371. '']);
  23372. ConvertProgram;
  23373. CheckSource('TestRecordHelper_Constructor',
  23374. LinesToStr([ // statements
  23375. 'rtl.recNewT(this, "TRec", function () {',
  23376. ' this.$eq = function (b) {',
  23377. ' return true;',
  23378. ' };',
  23379. ' this.$assign = function (s) {',
  23380. ' return this;',
  23381. ' };',
  23382. ' this.Create = function (w) {',
  23383. ' $mod.THelper.NewHlp.call(this, 2);',
  23384. ' $mod.THelper.$new("NewHlp", [3]);',
  23385. ' return this;',
  23386. ' };',
  23387. '});',
  23388. 'rtl.createHelper(this, "THelper", null, function () {',
  23389. ' this.NewHlp = function (w) {',
  23390. ' this.Create(2);',
  23391. ' $mod.TRec.$new().Create(3);',
  23392. ' $mod.THelper.NewHlp.call(this, 4);',
  23393. ' $mod.THelper.$new("NewHlp", [5]);',
  23394. ' return this;',
  23395. ' };',
  23396. ' this.$new = function (fn, args) {',
  23397. ' return this[fn].apply($mod.TRec.$new(), args);',
  23398. ' };',
  23399. '});',
  23400. 'this.Rec = this.TRec.$new();',
  23401. '']),
  23402. LinesToStr([ // $mod.$main
  23403. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  23404. 'var $with = $mod.Rec;',
  23405. '$mod.THelper.NewHlp.call($with, 12);',
  23406. '$mod.THelper.$new("NewHlp", [3]);',
  23407. 'var $with1 = $mod.TRec;',
  23408. '$mod.THelper.$new("NewHlp", [13]);',
  23409. '']));
  23410. end;
  23411. procedure TTestModule.TestTypeHelper_ClassVar;
  23412. begin
  23413. StartProgram(false);
  23414. Add([
  23415. '{$modeswitch typehelpers}',
  23416. 'type',
  23417. ' THelper = type helper for byte',
  23418. ' const',
  23419. ' One = 1;',
  23420. ' Two: word = 2;',
  23421. ' class var',
  23422. ' Glob: word;',
  23423. ' function Foo(w: word): word;',
  23424. ' class function Bar(w: word): word; static;',
  23425. ' end;',
  23426. 'function THelper.foo(w: word): word;',
  23427. 'begin',
  23428. ' Result:=w;',
  23429. ' Two:=One+w;',
  23430. ' Glob:=Glob;',
  23431. ' Result:=Self.Glob;',
  23432. ' Self.Glob:=Self.Glob;',
  23433. ' with Self do Glob:=Glob;',
  23434. 'end;',
  23435. 'class function THelper.bar(w: word): word;',
  23436. 'begin',
  23437. ' Result:=w;',
  23438. ' Two:=One;',
  23439. ' Glob:=Glob;',
  23440. 'end;',
  23441. 'var b: byte;',
  23442. 'begin',
  23443. ' byte.two:=byte.one;',
  23444. ' byte.Glob:=byte.Glob;',
  23445. ' with byte do begin',
  23446. ' two:=one;',
  23447. ' Glob:=Glob;',
  23448. ' end;',
  23449. ' b.two:=b.one;',
  23450. ' b.Glob:=b.Glob;',
  23451. ' with b do begin',
  23452. ' two:=one;',
  23453. ' Glob:=Glob;',
  23454. ' end;',
  23455. '']);
  23456. ConvertProgram;
  23457. CheckSource('TestTypeHelper_ClassVar',
  23458. LinesToStr([ // statements
  23459. 'rtl.createHelper(this, "THelper", null, function () {',
  23460. ' this.One = 1;',
  23461. ' this.Two = 2;',
  23462. ' this.Glob = 0;',
  23463. ' this.Foo = function (w) {',
  23464. ' var Result = 0;',
  23465. ' Result = w;',
  23466. ' $mod.THelper.Two = 1 + w;',
  23467. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23468. ' Result = $mod.THelper.Glob;',
  23469. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23470. ' var $with = this.get();',
  23471. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23472. ' return Result;',
  23473. ' };',
  23474. ' this.Bar = function (w) {',
  23475. ' var Result = 0;',
  23476. ' Result = w;',
  23477. ' $mod.THelper.Two = 1;',
  23478. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23479. ' return Result;',
  23480. ' };',
  23481. '});',
  23482. 'this.b = 0;',
  23483. '']),
  23484. LinesToStr([ // $mod.$main
  23485. '$mod.THelper.Two = 1;',
  23486. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23487. '$mod.THelper.Two = 1;',
  23488. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23489. '$mod.THelper.Two = 1;',
  23490. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23491. 'var $with = $mod.b;',
  23492. '$mod.THelper.Two = 1;',
  23493. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23494. '']));
  23495. end;
  23496. procedure TTestModule.TestTypeHelper_PassResultElement;
  23497. begin
  23498. StartProgram(false);
  23499. Add([
  23500. '{$modeswitch typehelpers}',
  23501. 'type',
  23502. ' THelper = type helper for word',
  23503. ' procedure DoIt(e: byte = 123);',
  23504. ' class procedure DoSome(e: byte = 456); static;',
  23505. ' end;',
  23506. 'procedure THelper.DoIt(e: byte);',
  23507. 'begin',
  23508. 'end;',
  23509. 'class procedure THelper.DoSome(e: byte);',
  23510. 'begin',
  23511. 'end;',
  23512. 'function Foo(w: word): word;',
  23513. 'begin',
  23514. ' Result.DoIt;',
  23515. ' Result.DoIt();',
  23516. ' Result.DoSome;',
  23517. ' Result.DoSome();',
  23518. ' with Result do begin',
  23519. ' DoIt;',
  23520. ' DoIt();',
  23521. ' DoSome;',
  23522. ' DoSome();',
  23523. ' end;',
  23524. 'end;',
  23525. 'begin',
  23526. '']);
  23527. ConvertProgram;
  23528. CheckSource('TestTypeHelper_PassResultElement',
  23529. LinesToStr([ // statements
  23530. 'rtl.createHelper(this, "THelper", null, function () {',
  23531. ' this.DoIt = function (e) {',
  23532. ' };',
  23533. ' this.DoSome = function (e) {',
  23534. ' };',
  23535. '});',
  23536. 'this.Foo = function (w) {',
  23537. ' var Result = 0;',
  23538. ' $mod.THelper.DoIt.call({',
  23539. ' get: function () {',
  23540. ' return Result;',
  23541. ' },',
  23542. ' set: function (v) {',
  23543. ' Result = v;',
  23544. ' }',
  23545. ' }, 123);',
  23546. ' $mod.THelper.DoIt.call({',
  23547. ' get: function () {',
  23548. ' return Result;',
  23549. ' },',
  23550. ' set: function (v) {',
  23551. ' Result = v;',
  23552. ' }',
  23553. ' }, 123);',
  23554. ' $mod.THelper.DoSome(456);',
  23555. ' $mod.THelper.DoSome(456);',
  23556. ' $mod.THelper.DoIt.call({',
  23557. ' get: function () {',
  23558. ' return Result;',
  23559. ' },',
  23560. ' set: function (v) {',
  23561. ' Result = v;',
  23562. ' }',
  23563. ' }, 123);',
  23564. ' $mod.THelper.DoIt.call({',
  23565. ' get: function () {',
  23566. ' return Result;',
  23567. ' },',
  23568. ' set: function (v) {',
  23569. ' Result = v;',
  23570. ' }',
  23571. ' }, 123);',
  23572. ' $mod.THelper.DoSome(456);',
  23573. ' $mod.THelper.DoSome(456);',
  23574. ' return Result;',
  23575. '};',
  23576. '']),
  23577. LinesToStr([ // $mod.$main
  23578. '']));
  23579. end;
  23580. procedure TTestModule.TestTypeHelper_PassArgs;
  23581. begin
  23582. StartProgram(false);
  23583. Add([
  23584. '{$modeswitch typehelpers}',
  23585. 'type',
  23586. ' THelper = type helper for word',
  23587. ' procedure DoIt(e: byte = 123);',
  23588. ' end;',
  23589. 'procedure THelper.DoIt(e: byte);',
  23590. 'begin',
  23591. 'end;',
  23592. 'procedure FooDefault(a: word);',
  23593. 'begin',
  23594. ' a.DoIt;',
  23595. ' with a do DoIt;',
  23596. 'end;',
  23597. 'procedure FooConst(const a: word);',
  23598. 'begin',
  23599. ' a.DoIt;',
  23600. ' with a do DoIt;',
  23601. 'end;',
  23602. 'procedure FooVar(var a: word);',
  23603. 'begin',
  23604. ' a.DoIt;',
  23605. ' with a do DoIt;',
  23606. 'end;',
  23607. 'begin',
  23608. '']);
  23609. ConvertProgram;
  23610. CheckSource('TestTypeHelper_PassArgs',
  23611. LinesToStr([ // statements
  23612. 'rtl.createHelper(this, "THelper", null, function () {',
  23613. ' this.DoIt = function (e) {',
  23614. ' };',
  23615. '});',
  23616. 'this.FooDefault = function (a) {',
  23617. ' $mod.THelper.DoIt.call({',
  23618. ' get: function () {',
  23619. ' return a;',
  23620. ' },',
  23621. ' set: function (v) {',
  23622. ' a = v;',
  23623. ' }',
  23624. ' }, 123);',
  23625. ' $mod.THelper.DoIt.call({',
  23626. ' get: function () {',
  23627. ' return a;',
  23628. ' },',
  23629. ' set: function (v) {',
  23630. ' a = v;',
  23631. ' }',
  23632. ' }, 123);',
  23633. '};',
  23634. 'this.FooConst = function (a) {',
  23635. ' $mod.THelper.DoIt.call({',
  23636. ' get: function () {',
  23637. ' return a;',
  23638. ' },',
  23639. ' set: function (v) {',
  23640. ' rtl.raiseE("EPropReadOnly");',
  23641. ' }',
  23642. ' }, 123);',
  23643. ' $mod.THelper.DoIt.call({',
  23644. ' get: function () {',
  23645. ' return a;',
  23646. ' },',
  23647. ' set: function () {',
  23648. ' rtl.raiseE("EPropReadOnly");',
  23649. ' }',
  23650. ' }, 123);',
  23651. '};',
  23652. 'this.FooVar = function (a) {',
  23653. ' $mod.THelper.DoIt.call(a, 123);',
  23654. ' var $with = a.get();',
  23655. ' $mod.THelper.DoIt.call(a, 123);',
  23656. '};',
  23657. '']),
  23658. LinesToStr([ // $mod.$main
  23659. '']));
  23660. end;
  23661. procedure TTestModule.TestTypeHelper_PassVarConst;
  23662. begin
  23663. StartProgram(false);
  23664. Add([
  23665. '{$modeswitch typehelpers}',
  23666. 'type',
  23667. ' THelper = type helper for word',
  23668. ' procedure DoIt(e: byte = 123);',
  23669. ' end;',
  23670. 'procedure THelper.DoIt(e: byte);',
  23671. 'begin',
  23672. 'end;',
  23673. 'var a: word;',
  23674. 'const c: word = 2;',
  23675. '{$writeableconst off}',
  23676. 'const r: word = 3;',
  23677. 'begin',
  23678. ' a.DoIt;',
  23679. ' with a do DoIt;',
  23680. ' c.DoIt;',
  23681. ' with c do DoIt;',
  23682. ' r.DoIt;',
  23683. ' with r do DoIt;',
  23684. '']);
  23685. ConvertProgram;
  23686. CheckSource('TestTypeHelper_PassVarConst',
  23687. LinesToStr([ // statements
  23688. 'rtl.createHelper(this, "THelper", null, function () {',
  23689. ' this.DoIt = function (e) {',
  23690. ' };',
  23691. '});',
  23692. 'this.a = 0;',
  23693. 'this.c = 2;',
  23694. 'this.r = 3;',
  23695. '']),
  23696. LinesToStr([ // $mod.$main
  23697. '$mod.THelper.DoIt.call({',
  23698. ' p: $mod,',
  23699. ' get: function () {',
  23700. ' return this.p.a;',
  23701. ' },',
  23702. ' set: function (v) {',
  23703. ' this.p.a = v;',
  23704. ' }',
  23705. '}, 123);',
  23706. 'var $with = $mod.a;',
  23707. '$mod.THelper.DoIt.call({',
  23708. ' get: function () {',
  23709. ' return $with;',
  23710. ' },',
  23711. ' set: function (v) {',
  23712. ' $with = v;',
  23713. ' }',
  23714. '}, 123);',
  23715. '$mod.THelper.DoIt.call({',
  23716. ' p: $mod,',
  23717. ' get: function () {',
  23718. ' return this.p.c;',
  23719. ' },',
  23720. ' set: function (v) {',
  23721. ' this.p.c = v;',
  23722. ' }',
  23723. '}, 123);',
  23724. 'var $with1 = $mod.c;',
  23725. '$mod.THelper.DoIt.call({',
  23726. ' get: function () {',
  23727. ' return $with1;',
  23728. ' },',
  23729. ' set: function (v) {',
  23730. ' $with1 = v;',
  23731. ' }',
  23732. '}, 123);',
  23733. '$mod.THelper.DoIt.call({',
  23734. ' get: function () {',
  23735. ' return 3;',
  23736. ' },',
  23737. ' set: function (v) {',
  23738. ' rtl.raiseE("EPropReadOnly");',
  23739. ' }',
  23740. '}, 123);',
  23741. 'var $with2 = 3;',
  23742. ' $mod.THelper.DoIt.call({',
  23743. ' get: function () {',
  23744. ' return $with2;',
  23745. ' },',
  23746. ' set: function () {',
  23747. ' rtl.raiseE("EPropReadOnly");',
  23748. ' }',
  23749. ' }, 123);',
  23750. '']));
  23751. end;
  23752. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23753. begin
  23754. StartProgram(false);
  23755. Add([
  23756. '{$modeswitch typehelpers}',
  23757. 'type',
  23758. ' THelper = type helper for word',
  23759. ' procedure DoIt(e: byte = 123);',
  23760. ' end;',
  23761. 'procedure THelper.DoIt(e: byte);',
  23762. 'begin',
  23763. 'end;',
  23764. 'function Foo(b: byte = 1): word;',
  23765. 'begin',
  23766. 'end;',
  23767. 'begin',
  23768. ' Foo.DoIt;',
  23769. ' Foo().DoIt;',
  23770. ' with Foo do DoIt;',
  23771. ' with Foo() do DoIt;',
  23772. '']);
  23773. ConvertProgram;
  23774. CheckSource('TestTypeHelper_PassFuncResult',
  23775. LinesToStr([ // statements
  23776. 'rtl.createHelper(this, "THelper", null, function () {',
  23777. ' this.DoIt = function (e) {',
  23778. ' };',
  23779. '});',
  23780. 'this.Foo = function (b) {',
  23781. ' var Result = 0;',
  23782. ' return Result;',
  23783. '};',
  23784. '']),
  23785. LinesToStr([ // $mod.$main
  23786. '$mod.THelper.DoIt.call({',
  23787. ' a: $mod.Foo(1),',
  23788. ' get: function () {',
  23789. ' return this.a;',
  23790. ' },',
  23791. ' set: function (v) {',
  23792. ' this.a = v;',
  23793. ' }',
  23794. '}, 123);',
  23795. '$mod.THelper.DoIt.call({',
  23796. ' a: $mod.Foo(1),',
  23797. ' get: function () {',
  23798. ' return this.a;',
  23799. ' },',
  23800. ' set: function (v) {',
  23801. ' this.a = v;',
  23802. ' }',
  23803. '}, 123);',
  23804. 'var $with = $mod.Foo(1);',
  23805. '$mod.THelper.DoIt.call({',
  23806. ' get: function () {',
  23807. ' return $with;',
  23808. ' },',
  23809. ' set: function (v) {',
  23810. ' $with = v;',
  23811. ' }',
  23812. '}, 123);',
  23813. 'var $with1 = $mod.Foo(1);',
  23814. '$mod.THelper.DoIt.call({',
  23815. ' get: function () {',
  23816. ' return $with1;',
  23817. ' },',
  23818. ' set: function (v) {',
  23819. ' $with1 = v;',
  23820. ' }',
  23821. '}, 123);',
  23822. '']));
  23823. end;
  23824. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23825. begin
  23826. StartProgram(false);
  23827. Add([
  23828. '{$modeswitch typehelpers}',
  23829. 'type',
  23830. ' TObject = class',
  23831. ' FField: word;',
  23832. ' procedure SetField(Value: word);',
  23833. ' property Field: word read FField write SetField;',
  23834. ' end;',
  23835. ' THelper = type helper for word',
  23836. ' procedure Fly;',
  23837. ' class procedure Run; static;',
  23838. ' end;',
  23839. 'procedure TObject.SetField(Value: word);',
  23840. 'begin',
  23841. ' Field.Fly;',
  23842. ' Field.Run;',
  23843. ' Self.Field.Fly;',
  23844. ' Self.Field.Run;',
  23845. ' with Self do begin',
  23846. ' Field.Fly;',
  23847. ' Field.Run;',
  23848. ' end;',
  23849. ' with Self.Field do begin',
  23850. ' Fly;',
  23851. ' Run;',
  23852. ' end;',
  23853. 'end;',
  23854. 'procedure THelper.Fly;',
  23855. 'begin',
  23856. 'end;',
  23857. 'class procedure THelper.Run;',
  23858. 'begin',
  23859. 'end;',
  23860. 'var',
  23861. ' o: TObject;',
  23862. 'begin',
  23863. ' o.Field.Fly;',
  23864. ' o.Field.Run;',
  23865. ' with o do begin',
  23866. ' Field.Fly;',
  23867. ' Field.Run;',
  23868. ' end;',
  23869. ' with o.Field do begin',
  23870. ' Fly;',
  23871. ' Run;',
  23872. ' end;',
  23873. '']);
  23874. ConvertProgram;
  23875. CheckSource('TestTypeHelper_PassPropertyField',
  23876. LinesToStr([ // statements
  23877. 'rtl.createClass(this, "TObject", null, function () {',
  23878. ' this.$init = function () {',
  23879. ' this.FField = 0;',
  23880. ' };',
  23881. ' this.$final = function () {',
  23882. ' };',
  23883. ' this.SetField = function (Value) {',
  23884. ' $mod.THelper.Fly.call({',
  23885. ' p: this,',
  23886. ' get: function () {',
  23887. ' return this.p.FField;',
  23888. ' },',
  23889. ' set: function (v) {',
  23890. ' this.p.FField = v;',
  23891. ' }',
  23892. ' });',
  23893. ' $mod.THelper.Run();',
  23894. ' $mod.THelper.Fly.call({',
  23895. ' p: this,',
  23896. ' get: function () {',
  23897. ' return this.p.FField;',
  23898. ' },',
  23899. ' set: function (v) {',
  23900. ' this.p.FField = v;',
  23901. ' }',
  23902. ' });',
  23903. ' $mod.THelper.Run();',
  23904. ' $mod.THelper.Fly.call({',
  23905. ' p: this,',
  23906. ' get: function () {',
  23907. ' return this.p.FField;',
  23908. ' },',
  23909. ' set: function (v) {',
  23910. ' this.p.FField = v;',
  23911. ' }',
  23912. ' });',
  23913. ' $mod.THelper.Run();',
  23914. ' var $with = this.FField;',
  23915. ' $mod.THelper.Fly.call({',
  23916. ' get: function () {',
  23917. ' return $with;',
  23918. ' },',
  23919. ' set: function (v) {',
  23920. ' $with = v;',
  23921. ' }',
  23922. ' });',
  23923. ' $mod.THelper.Run();',
  23924. ' };',
  23925. '});',
  23926. 'rtl.createHelper(this, "THelper", null, function () {',
  23927. ' this.Fly = function () {',
  23928. ' };',
  23929. ' this.Run = function () {',
  23930. ' };',
  23931. '});',
  23932. 'this.o = null;',
  23933. '']),
  23934. LinesToStr([ // $mod.$main
  23935. '$mod.THelper.Fly.call({',
  23936. ' p: $mod.o,',
  23937. ' get: function () {',
  23938. ' return this.p.FField;',
  23939. ' },',
  23940. ' set: function (v) {',
  23941. ' this.p.FField = v;',
  23942. ' }',
  23943. '});',
  23944. '$mod.THelper.Run();',
  23945. 'var $with = $mod.o;',
  23946. '$mod.THelper.Fly.call({',
  23947. ' p: $with,',
  23948. ' get: function () {',
  23949. ' return this.p.FField;',
  23950. ' },',
  23951. ' set: function (v) {',
  23952. ' this.p.FField = v;',
  23953. ' }',
  23954. '});',
  23955. '$mod.THelper.Run();',
  23956. 'var $with1 = $mod.o.FField;',
  23957. '$mod.THelper.Fly.call({',
  23958. ' get: function () {',
  23959. ' return $with1;',
  23960. ' },',
  23961. ' set: function (v) {',
  23962. ' $with1 = v;',
  23963. ' }',
  23964. '});',
  23965. '$mod.THelper.Run();',
  23966. '']));
  23967. end;
  23968. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23969. begin
  23970. StartProgram(false);
  23971. Add([
  23972. '{$modeswitch typehelpers}',
  23973. 'type',
  23974. ' TObject = class',
  23975. ' FField: word;',
  23976. ' function GetField: word;',
  23977. ' property Field: word read GetField write FField;',
  23978. ' end;',
  23979. ' THelper = type helper for word',
  23980. ' procedure Fly;',
  23981. ' class procedure Run; static;',
  23982. ' end;',
  23983. 'function TObject.GetField: word;',
  23984. 'begin',
  23985. ' Field.Fly;',
  23986. ' Field.Run;',
  23987. ' Self.Field.Fly;',
  23988. ' Self.Field.Run;',
  23989. ' with Self do begin',
  23990. ' Field.Fly;',
  23991. ' Field.Run;',
  23992. ' end;',
  23993. ' with Self.Field do begin',
  23994. ' Fly;',
  23995. ' Run;',
  23996. ' end;',
  23997. 'end;',
  23998. 'procedure THelper.Fly;',
  23999. 'begin',
  24000. 'end;',
  24001. 'class procedure THelper.Run;',
  24002. 'begin',
  24003. 'end;',
  24004. 'var',
  24005. ' o: TObject;',
  24006. 'begin',
  24007. ' o.Field.Fly;',
  24008. ' o.Field.Run;',
  24009. ' with o do begin',
  24010. ' Field.Fly;',
  24011. ' Field.Run;',
  24012. ' end;',
  24013. ' with o.Field do begin',
  24014. ' Fly;',
  24015. ' Run;',
  24016. ' end;',
  24017. '']);
  24018. ConvertProgram;
  24019. CheckSource('TestTypeHelper_PassPropertyGetter',
  24020. LinesToStr([ // statements
  24021. 'rtl.createClass(this, "TObject", null, function () {',
  24022. ' this.$init = function () {',
  24023. ' this.FField = 0;',
  24024. ' };',
  24025. ' this.$final = function () {',
  24026. ' };',
  24027. ' this.GetField = function () {',
  24028. ' var Result = 0;',
  24029. ' $mod.THelper.Fly.call({',
  24030. ' p: this.GetField(),',
  24031. ' get: function () {',
  24032. ' return this.p;',
  24033. ' },',
  24034. ' set: function (v) {',
  24035. ' this.p = v;',
  24036. ' }',
  24037. ' });',
  24038. ' $mod.THelper.Run();',
  24039. ' $mod.THelper.Fly.call({',
  24040. ' p: this.GetField(),',
  24041. ' get: function () {',
  24042. ' return this.p;',
  24043. ' },',
  24044. ' set: function (v) {',
  24045. ' this.p = v;',
  24046. ' }',
  24047. ' });',
  24048. ' $mod.THelper.Run();',
  24049. ' $mod.THelper.Fly.call({',
  24050. ' p: this.GetField(),',
  24051. ' get: function () {',
  24052. ' return this.p;',
  24053. ' },',
  24054. ' set: function (v) {',
  24055. ' this.p = v;',
  24056. ' }',
  24057. ' });',
  24058. ' $mod.THelper.Run();',
  24059. ' var $with = this.GetField();',
  24060. ' $mod.THelper.Fly.call({',
  24061. ' get: function () {',
  24062. ' return $with;',
  24063. ' },',
  24064. ' set: function (v) {',
  24065. ' $with = v;',
  24066. ' }',
  24067. ' });',
  24068. ' $mod.THelper.Run();',
  24069. ' return Result;',
  24070. ' };',
  24071. '});',
  24072. 'rtl.createHelper(this, "THelper", null, function () {',
  24073. ' this.Fly = function () {',
  24074. ' };',
  24075. ' this.Run = function () {',
  24076. ' };',
  24077. '});',
  24078. 'this.o = null;',
  24079. '']),
  24080. LinesToStr([ // $mod.$main
  24081. '$mod.THelper.Fly.call({',
  24082. ' p: $mod.o.GetField(),',
  24083. ' get: function () {',
  24084. ' return this.p;',
  24085. ' },',
  24086. ' set: function (v) {',
  24087. ' this.p = v;',
  24088. ' }',
  24089. '});',
  24090. '$mod.THelper.Run();',
  24091. 'var $with = $mod.o;',
  24092. '$mod.THelper.Fly.call({',
  24093. ' p: $with.GetField(),',
  24094. ' get: function () {',
  24095. ' return this.p;',
  24096. ' },',
  24097. ' set: function (v) {',
  24098. ' this.p = v;',
  24099. ' }',
  24100. '});',
  24101. '$mod.THelper.Run();',
  24102. 'var $with1 = $mod.o.GetField();',
  24103. '$mod.THelper.Fly.call({',
  24104. ' get: function () {',
  24105. ' return $with1;',
  24106. ' },',
  24107. ' set: function (v) {',
  24108. ' $with1 = v;',
  24109. ' }',
  24110. '});',
  24111. '$mod.THelper.Run();',
  24112. '']));
  24113. end;
  24114. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  24115. begin
  24116. StartProgram(false);
  24117. Add([
  24118. '{$modeswitch typehelpers}',
  24119. 'type',
  24120. ' TObject = class',
  24121. ' class var FField: word;',
  24122. ' class procedure SetField(Value: word);',
  24123. ' class property Field: word read FField write SetField;',
  24124. ' end;',
  24125. ' THelper = type helper for word',
  24126. ' procedure Fly(n: byte);',
  24127. ' end;',
  24128. 'class procedure TObject.SetField(Value: word);',
  24129. 'begin',
  24130. ' Field.Fly(1);',
  24131. ' Self.Field.Fly(2);',
  24132. ' with Self do Field.Fly(3);',
  24133. ' with Self.Field do Fly(4);',
  24134. ' TObject.Field.Fly(5);',
  24135. ' with TObject do Field.Fly(6);',
  24136. ' with TObject.Field do Fly(7);',
  24137. 'end;',
  24138. 'procedure THelper.Fly(n: byte);',
  24139. 'begin',
  24140. 'end;',
  24141. 'var',
  24142. ' o: TObject;',
  24143. 'begin',
  24144. ' o.Field.Fly(11);',
  24145. ' with o do Field.Fly(12);',
  24146. ' with o.Field do Fly(13);',
  24147. ' TObject.Field.Fly(14);',
  24148. ' with TObject do Field.Fly(15);',
  24149. ' with TObject.Field do Fly(16);',
  24150. '']);
  24151. ConvertProgram;
  24152. CheckSource('TestTypeHelper_PassClassPropertyField',
  24153. LinesToStr([ // statements
  24154. 'rtl.createClass(this, "TObject", null, function () {',
  24155. ' this.FField = 0;',
  24156. ' this.$init = function () {',
  24157. ' };',
  24158. ' this.$final = function () {',
  24159. ' };',
  24160. ' this.SetField = function (Value) {',
  24161. ' $mod.THelper.Fly.call({',
  24162. ' p: this,',
  24163. ' get: function () {',
  24164. ' return this.p.FField;',
  24165. ' },',
  24166. ' set: function (v) {',
  24167. ' $mod.TObject.FField = v;',
  24168. ' }',
  24169. ' }, 1);',
  24170. ' $mod.THelper.Fly.call({',
  24171. ' p: this,',
  24172. ' get: function () {',
  24173. ' return this.p.FField;',
  24174. ' },',
  24175. ' set: function (v) {',
  24176. ' $mod.TObject.FField = v;',
  24177. ' }',
  24178. ' }, 2);',
  24179. ' $mod.THelper.Fly.call({',
  24180. ' p: this,',
  24181. ' get: function () {',
  24182. ' return this.p.FField;',
  24183. ' },',
  24184. ' set: function (v) {',
  24185. ' $mod.TObject.FField = v;',
  24186. ' }',
  24187. ' }, 3);',
  24188. ' var $with = this.FField;',
  24189. ' $mod.THelper.Fly.call({',
  24190. ' get: function () {',
  24191. ' return $with;',
  24192. ' },',
  24193. ' set: function (v) {',
  24194. ' $with = v;',
  24195. ' }',
  24196. ' }, 4);',
  24197. ' $mod.THelper.Fly.call({',
  24198. ' p: $mod.TObject,',
  24199. ' get: function () {',
  24200. ' return this.p.FField;',
  24201. ' },',
  24202. ' set: function (v) {',
  24203. ' $mod.TObject.FField = v;',
  24204. ' }',
  24205. ' }, 5);',
  24206. ' var $with1 = $mod.TObject;',
  24207. ' $mod.THelper.Fly.call({',
  24208. ' p: $with1,',
  24209. ' get: function () {',
  24210. ' return this.p.FField;',
  24211. ' },',
  24212. ' set: function (v) {',
  24213. ' $mod.TObject.FField = v;',
  24214. ' }',
  24215. ' }, 6);',
  24216. ' var $with2 = $mod.TObject.FField;',
  24217. ' $mod.THelper.Fly.call({',
  24218. ' get: function () {',
  24219. ' return $with2;',
  24220. ' },',
  24221. ' set: function (v) {',
  24222. ' $with2 = v;',
  24223. ' }',
  24224. ' }, 7);',
  24225. ' };',
  24226. '});',
  24227. 'rtl.createHelper(this, "THelper", null, function () {',
  24228. ' this.Fly = function (n) {',
  24229. ' };',
  24230. '});',
  24231. 'this.o = null;',
  24232. '']),
  24233. LinesToStr([ // $mod.$main
  24234. '$mod.THelper.Fly.call({',
  24235. ' p: $mod.o,',
  24236. ' get: function () {',
  24237. ' return this.p.FField;',
  24238. ' },',
  24239. ' set: function (v) {',
  24240. ' $mod.TObject.FField = v;',
  24241. ' }',
  24242. '}, 11);',
  24243. 'var $with = $mod.o;',
  24244. '$mod.THelper.Fly.call({',
  24245. ' p: $with,',
  24246. ' get: function () {',
  24247. ' return this.p.FField;',
  24248. ' },',
  24249. ' set: function (v) {',
  24250. ' $mod.TObject.FField = v;',
  24251. ' }',
  24252. '}, 12);',
  24253. 'var $with1 = $mod.o.FField;',
  24254. '$mod.THelper.Fly.call({',
  24255. ' get: function () {',
  24256. ' return $with1;',
  24257. ' },',
  24258. ' set: function (v) {',
  24259. ' $with1 = v;',
  24260. ' }',
  24261. '}, 13);',
  24262. '$mod.THelper.Fly.call({',
  24263. ' p: $mod.TObject,',
  24264. ' get: function () {',
  24265. ' return this.p.FField;',
  24266. ' },',
  24267. ' set: function (v) {',
  24268. ' $mod.TObject.FField = v;',
  24269. ' }',
  24270. '}, 14);',
  24271. 'var $with2 = $mod.TObject;',
  24272. '$mod.THelper.Fly.call({',
  24273. ' p: $with2,',
  24274. ' get: function () {',
  24275. ' return this.p.FField;',
  24276. ' },',
  24277. ' set: function (v) {',
  24278. ' $mod.TObject.FField = v;',
  24279. ' }',
  24280. '}, 15);',
  24281. 'var $with3 = $mod.TObject.FField;',
  24282. '$mod.THelper.Fly.call({',
  24283. ' get: function () {',
  24284. ' return $with3;',
  24285. ' },',
  24286. ' set: function (v) {',
  24287. ' $with3 = v;',
  24288. ' }',
  24289. '}, 16);',
  24290. '']));
  24291. end;
  24292. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  24293. begin
  24294. StartProgram(false);
  24295. Add([
  24296. '{$modeswitch typehelpers}',
  24297. 'type',
  24298. ' TObject = class',
  24299. ' class var FField: word;',
  24300. ' class function GetField: word; static;',
  24301. ' class property Field: word read GetField write FField;',
  24302. ' end;',
  24303. ' THelper = type helper for word',
  24304. ' procedure Fly(n: byte);',
  24305. ' end;',
  24306. 'class function TObject.GetField: word;',
  24307. 'begin',
  24308. ' Field.Fly(1);',
  24309. ' TObject.Field.Fly(5);',
  24310. ' with TObject do Field.Fly(6);',
  24311. ' with TObject.Field do Fly(7);',
  24312. 'end;',
  24313. 'procedure THelper.Fly(n: byte);',
  24314. 'begin',
  24315. 'end;',
  24316. 'var',
  24317. ' o: TObject;',
  24318. 'begin',
  24319. ' o.Field.Fly(11);',
  24320. ' with o do Field.Fly(12);',
  24321. ' with o.Field do Fly(13);',
  24322. '']);
  24323. ConvertProgram;
  24324. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  24325. LinesToStr([ // statements
  24326. 'rtl.createClass(this, "TObject", null, function () {',
  24327. ' this.FField = 0;',
  24328. ' this.$init = function () {',
  24329. ' };',
  24330. ' this.$final = function () {',
  24331. ' };',
  24332. ' this.GetField = function () {',
  24333. ' var Result = 0;',
  24334. ' $mod.THelper.Fly.call({',
  24335. ' p: $mod.TObject.GetField(),',
  24336. ' get: function () {',
  24337. ' return this.p;',
  24338. ' },',
  24339. ' set: function (v) {',
  24340. ' this.p = v;',
  24341. ' }',
  24342. ' }, 1);',
  24343. ' $mod.THelper.Fly.call({',
  24344. ' p: $mod.TObject.GetField(),',
  24345. ' get: function () {',
  24346. ' return this.p;',
  24347. ' },',
  24348. ' set: function (v) {',
  24349. ' this.p = v;',
  24350. ' }',
  24351. ' }, 5);',
  24352. ' var $with = $mod.TObject;',
  24353. ' $mod.THelper.Fly.call({',
  24354. ' p: $with.GetField(),',
  24355. ' get: function () {',
  24356. ' return this.p;',
  24357. ' },',
  24358. ' set: function (v) {',
  24359. ' this.p = v;',
  24360. ' }',
  24361. ' }, 6);',
  24362. ' var $with1 = $mod.TObject.GetField();',
  24363. ' $mod.THelper.Fly.call({',
  24364. ' get: function () {',
  24365. ' return $with1;',
  24366. ' },',
  24367. ' set: function (v) {',
  24368. ' $with1 = v;',
  24369. ' }',
  24370. ' }, 7);',
  24371. ' return Result;',
  24372. ' };',
  24373. '});',
  24374. 'rtl.createHelper(this, "THelper", null, function () {',
  24375. ' this.Fly = function (n) {',
  24376. ' };',
  24377. '});',
  24378. 'this.o = null;',
  24379. '']),
  24380. LinesToStr([ // $mod.$main
  24381. '$mod.THelper.Fly.call({',
  24382. ' p: $mod.TObject.GetField(),',
  24383. ' get: function () {',
  24384. ' return this.p;',
  24385. ' },',
  24386. ' set: function (v) {',
  24387. ' this.p = v;',
  24388. ' }',
  24389. '}, 11);',
  24390. 'var $with = $mod.o;',
  24391. '$mod.THelper.Fly.call({',
  24392. ' p: $with.GetField(),',
  24393. ' get: function () {',
  24394. ' return this.p;',
  24395. ' },',
  24396. ' set: function (v) {',
  24397. ' this.p = v;',
  24398. ' }',
  24399. '}, 12);',
  24400. 'var $with1 = $mod.TObject.GetField();',
  24401. '$mod.THelper.Fly.call({',
  24402. ' get: function () {',
  24403. ' return $with1;',
  24404. ' },',
  24405. ' set: function (v) {',
  24406. ' $with1 = v;',
  24407. ' }',
  24408. '}, 13);',
  24409. '']));
  24410. end;
  24411. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  24412. begin
  24413. StartProgram(false);
  24414. Add([
  24415. '{$modeswitch typehelpers}',
  24416. 'type',
  24417. ' TObject = class',
  24418. ' class var FField: word;',
  24419. ' class function GetField: word;',
  24420. ' class property Field: word read GetField write FField;',
  24421. ' end;',
  24422. ' TClass = class of TObject;',
  24423. ' THelper = type helper for word',
  24424. ' procedure Fly(n: byte);',
  24425. ' end;',
  24426. 'class function TObject.GetField: word;',
  24427. 'begin',
  24428. ' Field.Fly(1);',
  24429. ' Self.Field.Fly(5);',
  24430. ' with Self do Field.Fly(6);',
  24431. ' with Self.Field do Fly(7);',
  24432. 'end;',
  24433. 'procedure THelper.Fly(n: byte);',
  24434. 'begin',
  24435. 'end;',
  24436. 'var',
  24437. ' o: TObject;',
  24438. ' c: TClass;',
  24439. 'begin',
  24440. ' o.Field.Fly(11);',
  24441. ' with o do Field.Fly(12);',
  24442. ' with o.Field do Fly(13);',
  24443. ' c.Field.Fly(14);',
  24444. ' with c do Field.Fly(15);',
  24445. ' with c.Field do Fly(16);',
  24446. '']);
  24447. ConvertProgram;
  24448. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24449. LinesToStr([ // statements
  24450. 'rtl.createClass(this, "TObject", null, function () {',
  24451. ' this.FField = 0;',
  24452. ' this.$init = function () {',
  24453. ' };',
  24454. ' this.$final = function () {',
  24455. ' };',
  24456. ' this.GetField = function () {',
  24457. ' var Result = 0;',
  24458. ' $mod.THelper.Fly.call({',
  24459. ' p: this.GetField(),',
  24460. ' get: function () {',
  24461. ' return this.p;',
  24462. ' },',
  24463. ' set: function (v) {',
  24464. ' this.p = v;',
  24465. ' }',
  24466. ' }, 1);',
  24467. ' $mod.THelper.Fly.call({',
  24468. ' p: this.GetField(),',
  24469. ' get: function () {',
  24470. ' return this.p;',
  24471. ' },',
  24472. ' set: function (v) {',
  24473. ' this.p = v;',
  24474. ' }',
  24475. ' }, 5);',
  24476. ' $mod.THelper.Fly.call({',
  24477. ' p: this.GetField(),',
  24478. ' get: function () {',
  24479. ' return this.p;',
  24480. ' },',
  24481. ' set: function (v) {',
  24482. ' this.p = v;',
  24483. ' }',
  24484. ' }, 6);',
  24485. ' var $with = this.GetField();',
  24486. ' $mod.THelper.Fly.call({',
  24487. ' get: function () {',
  24488. ' return $with;',
  24489. ' },',
  24490. ' set: function (v) {',
  24491. ' $with = v;',
  24492. ' }',
  24493. ' }, 7);',
  24494. ' return Result;',
  24495. ' };',
  24496. '});',
  24497. 'rtl.createHelper(this, "THelper", null, function () {',
  24498. ' this.Fly = function (n) {',
  24499. ' };',
  24500. '});',
  24501. 'this.o = null;',
  24502. 'this.c = null;',
  24503. '']),
  24504. LinesToStr([ // $mod.$main
  24505. '$mod.THelper.Fly.call({',
  24506. ' p: $mod.o.$class.GetField(),',
  24507. ' get: function () {',
  24508. ' return this.p;',
  24509. ' },',
  24510. ' set: function (v) {',
  24511. ' this.p = v;',
  24512. ' }',
  24513. '}, 11);',
  24514. 'var $with = $mod.o;',
  24515. '$mod.THelper.Fly.call({',
  24516. ' p: $with.$class.GetField(),',
  24517. ' get: function () {',
  24518. ' return this.p;',
  24519. ' },',
  24520. ' set: function (v) {',
  24521. ' this.p = v;',
  24522. ' }',
  24523. '}, 12);',
  24524. 'var $with1 = $mod.o.$class.GetField();',
  24525. '$mod.THelper.Fly.call({',
  24526. ' get: function () {',
  24527. ' return $with1;',
  24528. ' },',
  24529. ' set: function (v) {',
  24530. ' $with1 = v;',
  24531. ' }',
  24532. '}, 13);',
  24533. '$mod.THelper.Fly.call({',
  24534. ' p: $mod.c.GetField(),',
  24535. ' get: function () {',
  24536. ' return this.p;',
  24537. ' },',
  24538. ' set: function (v) {',
  24539. ' this.p = v;',
  24540. ' }',
  24541. '}, 14);',
  24542. 'var $with2 = $mod.c;',
  24543. '$mod.THelper.Fly.call({',
  24544. ' p: $with2.GetField(),',
  24545. ' get: function () {',
  24546. ' return this.p;',
  24547. ' },',
  24548. ' set: function (v) {',
  24549. ' this.p = v;',
  24550. ' }',
  24551. '}, 15);',
  24552. 'var $with3 = $mod.c.GetField();',
  24553. '$mod.THelper.Fly.call({',
  24554. ' get: function () {',
  24555. ' return $with3;',
  24556. ' },',
  24557. ' set: function (v) {',
  24558. ' $with3 = v;',
  24559. ' }',
  24560. '}, 16);',
  24561. '']));
  24562. end;
  24563. procedure TTestModule.TestTypeHelper_Property;
  24564. begin
  24565. StartProgram(false);
  24566. Add([
  24567. '{$modeswitch typehelpers}',
  24568. 'type',
  24569. ' THelper = type helper for word',
  24570. ' function GetSize: longint;',
  24571. ' procedure SetSize(Value: longint);',
  24572. ' property Size: longint read GetSize write SetSize;',
  24573. ' end;',
  24574. 'function THelper.GetSize: longint;',
  24575. 'begin',
  24576. ' Result:=Size+1;',
  24577. ' Size:=2;',
  24578. ' Result:=Self.Size+3;',
  24579. ' Self.Size:=4;',
  24580. ' with Self do begin',
  24581. ' Result:=Size+5;',
  24582. ' Size:=6;',
  24583. ' end;',
  24584. 'end;',
  24585. 'procedure THelper.SetSize(Value: longint);',
  24586. 'begin',
  24587. 'end;',
  24588. 'var w: word;',
  24589. 'begin',
  24590. ' w:=w.Size+7;',
  24591. ' w.Size:=w+8;',
  24592. ' with w do begin',
  24593. ' w:=Size+9;',
  24594. ' Size:=w+10;',
  24595. ' end;',
  24596. '']);
  24597. ConvertProgram;
  24598. CheckSource('TestTypeHelper_Property',
  24599. LinesToStr([ // statements
  24600. 'rtl.createHelper(this, "THelper", null, function () {',
  24601. ' this.GetSize = function () {',
  24602. ' var Result = 0;',
  24603. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24604. ' $mod.THelper.SetSize.call(this, 2);',
  24605. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24606. ' $mod.THelper.SetSize.call(this, 4);',
  24607. ' var $with = this.get();',
  24608. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24609. ' $mod.THelper.SetSize.call(this, 6);',
  24610. ' return Result;',
  24611. ' };',
  24612. ' this.SetSize = function (Value) {',
  24613. ' };',
  24614. '});',
  24615. 'this.w = 0;',
  24616. '']),
  24617. LinesToStr([ // $mod.$main
  24618. '$mod.w = $mod.THelper.GetSize.call({',
  24619. ' p: $mod,',
  24620. ' get: function () {',
  24621. ' return this.p.w;',
  24622. ' },',
  24623. ' set: function (v) {',
  24624. ' this.p.w = v;',
  24625. ' }',
  24626. '}) + 7;',
  24627. '$mod.THelper.SetSize.call({',
  24628. ' p: $mod,',
  24629. ' get: function () {',
  24630. ' return this.p.w;',
  24631. ' },',
  24632. ' set: function (v) {',
  24633. ' this.p.w = v;',
  24634. ' }',
  24635. '}, $mod.w + 8);',
  24636. 'var $with = $mod.w;',
  24637. '$mod.w = $mod.THelper.GetSize.call({',
  24638. ' get: function () {',
  24639. ' return $with;',
  24640. ' },',
  24641. ' set: function (v) {',
  24642. ' $with = v;',
  24643. ' }',
  24644. '}) + 9;',
  24645. '$mod.THelper.SetSize.call({',
  24646. ' get: function () {',
  24647. ' return $with;',
  24648. ' },',
  24649. ' set: function (v) {',
  24650. ' $with = v;',
  24651. ' }',
  24652. '}, $mod.w + 10);',
  24653. '']));
  24654. end;
  24655. procedure TTestModule.TestTypeHelper_Property_Array;
  24656. begin
  24657. StartProgram(false);
  24658. Add([
  24659. '{$modeswitch typehelpers}',
  24660. 'type',
  24661. ' THelper = type helper for word',
  24662. ' function GetItems(Index: byte): boolean;',
  24663. ' procedure SetItems(Index: byte; Value: boolean);',
  24664. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24665. ' end;',
  24666. 'function THelper.GetItems(Index: byte): boolean;',
  24667. 'begin',
  24668. ' Result:=Items[1];',
  24669. ' Items[2]:=false;',
  24670. ' Result:=Self.Items[3];',
  24671. ' Self.Items[4]:=true;',
  24672. ' with Self do begin',
  24673. ' Result:=Items[5];',
  24674. ' Items[6]:=false;',
  24675. ' end;',
  24676. 'end;',
  24677. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24678. 'begin',
  24679. 'end;',
  24680. 'var',
  24681. ' w: word;',
  24682. ' b: boolean;',
  24683. 'begin',
  24684. ' b:=w.Items[1];',
  24685. ' w.Items[2]:=b;',
  24686. ' with w do begin',
  24687. ' b:=Items[3];',
  24688. ' Items[4]:=b;',
  24689. ' end;',
  24690. '']);
  24691. ConvertProgram;
  24692. CheckSource('TestTypeHelper_Property_Array',
  24693. LinesToStr([ // statements
  24694. 'rtl.createHelper(this, "THelper", null, function () {',
  24695. ' this.GetItems = function (Index) {',
  24696. ' var Result = false;',
  24697. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24698. ' $mod.THelper.SetItems.call(this, 2, false);',
  24699. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24700. ' $mod.THelper.SetItems.call(this, 4, true);',
  24701. ' var $with = this.get();',
  24702. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24703. ' $mod.THelper.SetItems.call(this, 6, false);',
  24704. ' return Result;',
  24705. ' };',
  24706. ' this.SetItems = function (Index, Value) {',
  24707. ' };',
  24708. '});',
  24709. 'this.w = 0;',
  24710. 'this.b = false;',
  24711. '']),
  24712. LinesToStr([ // $mod.$main
  24713. '$mod.b = $mod.THelper.GetItems.call({',
  24714. ' p: $mod,',
  24715. ' get: function () {',
  24716. ' return this.p.w;',
  24717. ' },',
  24718. ' set: function (v) {',
  24719. ' this.p.w = v;',
  24720. ' }',
  24721. '}, 1);',
  24722. '$mod.THelper.SetItems.call({',
  24723. ' p: $mod,',
  24724. ' get: function () {',
  24725. ' return this.p.w;',
  24726. ' },',
  24727. ' set: function (v) {',
  24728. ' this.p.w = v;',
  24729. ' }',
  24730. '}, 2, $mod.b);',
  24731. 'var $with = $mod.w;',
  24732. '$mod.b = $mod.THelper.GetItems.call({',
  24733. ' get: function () {',
  24734. ' return $with;',
  24735. ' },',
  24736. ' set: function (v) {',
  24737. ' $with = v;',
  24738. ' }',
  24739. '}, 3);',
  24740. '$mod.THelper.SetItems.call({',
  24741. ' get: function () {',
  24742. ' return $with;',
  24743. ' },',
  24744. ' set: function (v) {',
  24745. ' $with = v;',
  24746. ' }',
  24747. '}, 4, $mod.b);',
  24748. '']));
  24749. end;
  24750. procedure TTestModule.TestTypeHelper_ClassProperty;
  24751. begin
  24752. StartProgram(false);
  24753. Add([
  24754. '{$modeswitch typehelpers}',
  24755. 'type',
  24756. ' THelper = type helper for word',
  24757. ' class function GetSize: longint; static;',
  24758. ' class procedure SetSize(Value: longint); static;',
  24759. ' class property Size: longint read GetSize write SetSize;',
  24760. ' end;',
  24761. 'class function THelper.GetSize: longint;',
  24762. 'begin',
  24763. ' Result:=Size+1;',
  24764. ' Size:=2;',
  24765. 'end;',
  24766. 'class procedure THelper.SetSize(Value: longint);',
  24767. 'begin',
  24768. 'end;',
  24769. 'begin',
  24770. '']);
  24771. ConvertProgram;
  24772. CheckSource('TestTypeHelper_ClassProperty',
  24773. LinesToStr([ // statements
  24774. 'rtl.createHelper(this, "THelper", null, function () {',
  24775. ' this.GetSize = function () {',
  24776. ' var Result = 0;',
  24777. ' Result = $mod.THelper.GetSize() + 1;',
  24778. ' $mod.THelper.SetSize(2);',
  24779. ' return Result;',
  24780. ' };',
  24781. ' this.SetSize = function (Value) {',
  24782. ' };',
  24783. '});',
  24784. '']),
  24785. LinesToStr([ // $mod.$main
  24786. '']));
  24787. end;
  24788. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24789. begin
  24790. StartProgram(false);
  24791. Add([
  24792. '{$modeswitch typehelpers}',
  24793. 'type',
  24794. ' THelper = type helper for word',
  24795. ' class function GetItems(Index: byte): boolean; static;',
  24796. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24797. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24798. ' end;',
  24799. 'class function THelper.GetItems(Index: byte): boolean;',
  24800. 'begin',
  24801. ' Result:=Items[1];',
  24802. ' Items[2]:=false;',
  24803. 'end;',
  24804. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24805. 'begin',
  24806. 'end;',
  24807. 'var',
  24808. ' w: word;',
  24809. ' b: boolean;',
  24810. 'begin',
  24811. ' b:=w.Items[1];',
  24812. ' w.Items[2]:=b;',
  24813. ' with w do begin',
  24814. ' b:=Items[3];',
  24815. ' Items[4]:=b;',
  24816. ' end;',
  24817. '']);
  24818. ConvertProgram;
  24819. CheckSource('TestTypeHelper_ClassProperty_Array',
  24820. LinesToStr([ // statements
  24821. 'rtl.createHelper(this, "THelper", null, function () {',
  24822. ' this.GetItems = function (Index) {',
  24823. ' var Result = false;',
  24824. ' Result = $mod.THelper.GetItems(1);',
  24825. ' $mod.THelper.SetItems(2, false);',
  24826. ' return Result;',
  24827. ' };',
  24828. ' this.SetItems = function (Index, Value) {',
  24829. ' };',
  24830. '});',
  24831. 'this.w = 0;',
  24832. 'this.b = false;',
  24833. '']),
  24834. LinesToStr([ // $mod.$main
  24835. '$mod.b = $mod.THelper.GetItems(1);',
  24836. '$mod.THelper.SetItems(2, $mod.b);',
  24837. 'var $with = $mod.w;',
  24838. '$mod.b = $mod.THelper.GetItems(3);',
  24839. '$mod.THelper.SetItems(4, $mod.b);',
  24840. '']));
  24841. end;
  24842. procedure TTestModule.TestTypeHelper_ClassMethod;
  24843. begin
  24844. StartProgram(false);
  24845. Add([
  24846. '{$modeswitch typehelpers}',
  24847. 'type',
  24848. ' THelper = type helper for word',
  24849. ' class procedure DoStatic; static;',
  24850. ' end;',
  24851. 'class procedure THelper.DoStatic;',
  24852. 'begin',
  24853. ' DoStatic;',
  24854. ' DoStatic();',
  24855. 'end;',
  24856. 'var w: word;',
  24857. 'begin',
  24858. ' w.DoStatic;',
  24859. ' w.DoStatic();',
  24860. '']);
  24861. ConvertProgram;
  24862. CheckSource('TestTypeHelper_ClassMethod',
  24863. LinesToStr([ // statements
  24864. 'rtl.createHelper(this, "THelper", null, function () {',
  24865. ' this.DoStatic = function () {',
  24866. ' $mod.THelper.DoStatic();',
  24867. ' $mod.THelper.DoStatic();',
  24868. ' };',
  24869. '});',
  24870. 'this.w = 0;',
  24871. '']),
  24872. LinesToStr([ // $mod.$main
  24873. '$mod.THelper.DoStatic();',
  24874. '$mod.THelper.DoStatic();',
  24875. '']));
  24876. end;
  24877. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24878. begin
  24879. StartProgram(false);
  24880. Add([
  24881. '{$modeswitch typehelpers}',
  24882. 'type',
  24883. ' THelper = type helper for word',
  24884. ' procedure Run; external name ''Run'';',
  24885. ' end;',
  24886. 'var w: word;',
  24887. 'begin',
  24888. ' w.Run;',
  24889. '']);
  24890. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24891. ConvertProgram;
  24892. end;
  24893. procedure TTestModule.TestTypeHelper_Constructor;
  24894. begin
  24895. StartProgram(false);
  24896. Add([
  24897. '{$modeswitch typehelpers}',
  24898. 'type',
  24899. ' THelper = type helper for word',
  24900. ' constructor Init(e: longint);',
  24901. ' end;',
  24902. 'constructor THelper.Init(e: longint);',
  24903. 'begin',
  24904. ' Self:=e;',
  24905. ' Init(e+1);',
  24906. 'end;',
  24907. 'var w: word;',
  24908. 'begin',
  24909. ' w:=word.Init(2);',
  24910. ' w:=w.Init(3);',
  24911. ' with word do w:=Init(4);',
  24912. ' with w do w:=Init(5);',
  24913. '']);
  24914. ConvertProgram;
  24915. CheckSource('TestTypeHelper_Constructor',
  24916. LinesToStr([ // statements
  24917. 'rtl.createHelper(this, "THelper", null, function () {',
  24918. ' this.Init = function (e) {',
  24919. ' this.set(e);',
  24920. ' $mod.THelper.Init.call(this, e + 1);',
  24921. ' return this.get();',
  24922. ' };',
  24923. ' this.$new = function (fn, args) {',
  24924. ' return this[fn].apply({',
  24925. ' p: 0,',
  24926. ' get: function () {',
  24927. ' return this.p;',
  24928. ' },',
  24929. ' set: function (v) {',
  24930. ' this.p = v;',
  24931. ' }',
  24932. ' }, args);',
  24933. ' };',
  24934. '});',
  24935. 'this.w = 0;',
  24936. '']),
  24937. LinesToStr([ // $mod.$main
  24938. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24939. '$mod.w = $mod.THelper.Init.call({',
  24940. ' p: $mod,',
  24941. ' get: function () {',
  24942. ' return this.p.w;',
  24943. ' },',
  24944. ' set: function (v) {',
  24945. ' this.p.w = v;',
  24946. ' }',
  24947. '}, 3);',
  24948. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24949. 'var $with = $mod.w;',
  24950. '$mod.w = $mod.THelper.Init.call({',
  24951. ' get: function () {',
  24952. ' return $with;',
  24953. ' },',
  24954. ' set: function (v) {',
  24955. ' $with = v;',
  24956. ' }',
  24957. '}, 5);',
  24958. '']));
  24959. end;
  24960. procedure TTestModule.TestTypeHelper_Word;
  24961. begin
  24962. StartProgram(false);
  24963. Add([
  24964. '{$modeswitch typehelpers}',
  24965. 'type',
  24966. ' THelper = type helper for word',
  24967. ' procedure DoIt(e: byte = 123);',
  24968. ' end;',
  24969. 'procedure THelper.DoIt(e: byte);',
  24970. 'begin',
  24971. ' Self:=e;',
  24972. ' Self:=Self+1;',
  24973. ' with Self do Doit;',
  24974. 'end;',
  24975. 'begin',
  24976. ' word(3).DoIt;',
  24977. '']);
  24978. ConvertProgram;
  24979. CheckSource('TestTypeHelper_Word',
  24980. LinesToStr([ // statements
  24981. 'rtl.createHelper(this, "THelper", null, function () {',
  24982. ' this.DoIt = function (e) {',
  24983. ' this.set(e);',
  24984. ' this.set(this.get() + 1);',
  24985. ' var $with = this.get();',
  24986. ' $mod.THelper.DoIt.call(this, 123);',
  24987. ' };',
  24988. '});',
  24989. '']),
  24990. LinesToStr([ // $mod.$main
  24991. '$mod.THelper.DoIt.call({',
  24992. ' get: function () {',
  24993. ' return 3;',
  24994. ' },',
  24995. ' set: function (v) {',
  24996. ' rtl.raiseE("EPropReadOnly");',
  24997. ' }',
  24998. '}, 123);',
  24999. '']));
  25000. end;
  25001. procedure TTestModule.TestTypeHelper_Boolean;
  25002. begin
  25003. StartProgram(false);
  25004. Add([
  25005. '{$modeswitch typehelpers}',
  25006. 'type',
  25007. ' Integer = longint;',
  25008. ' THelper = type helper for boolean',
  25009. ' procedure Run(e: wordbool = true);',
  25010. ' end;',
  25011. 'procedure THelper.Run(e: wordbool);',
  25012. 'begin',
  25013. ' Self:=e;',
  25014. ' Self:=not Self;',
  25015. ' with Self do Run;',
  25016. ' if Integer(Self)=0 then ;',
  25017. 'end;',
  25018. 'begin',
  25019. ' boolean(3).Run;',
  25020. '']);
  25021. ConvertProgram;
  25022. CheckSource('TestTypeHelper_Boolean',
  25023. LinesToStr([ // statements
  25024. 'rtl.createHelper(this, "THelper", null, function () {',
  25025. ' this.Run = function (e) {',
  25026. ' this.set(e);',
  25027. ' this.set(!this.get());',
  25028. ' var $with = this.get();',
  25029. ' $mod.THelper.Run.call(this, true);',
  25030. ' if ((this.get() ? 1 : 0) === 0) ;',
  25031. ' };',
  25032. '});',
  25033. '']),
  25034. LinesToStr([ // $mod.$main
  25035. '$mod.THelper.Run.call({',
  25036. ' a: 3 != 0,',
  25037. ' get: function () {',
  25038. ' return this.a;',
  25039. ' },',
  25040. ' set: function (v) {',
  25041. ' rtl.raiseE("EPropReadOnly");',
  25042. ' }',
  25043. '}, true);',
  25044. '']));
  25045. end;
  25046. procedure TTestModule.TestTypeHelper_WordBool;
  25047. begin
  25048. StartProgram(false);
  25049. Add([
  25050. '{$modeswitch typehelpers}',
  25051. 'type',
  25052. ' Integer = longint;',
  25053. ' THelper = type helper for WordBool',
  25054. ' procedure Run(e: wordbool = true);',
  25055. ' end;',
  25056. 'procedure THelper.Run(e: wordbool);',
  25057. 'var i: integer;',
  25058. 'begin',
  25059. ' i:=Integer(Self);',
  25060. 'end;',
  25061. 'var w: wordbool;',
  25062. 'begin',
  25063. ' w.Run;',
  25064. ' wordbool(3).Run;',
  25065. '']);
  25066. ConvertProgram;
  25067. CheckSource('TestTypeHelper_WordBool',
  25068. LinesToStr([ // statements
  25069. 'rtl.createHelper(this, "THelper", null, function () {',
  25070. ' this.Run = function (e) {',
  25071. ' var i = 0;',
  25072. ' i = (this.get() ? 1 : 0);',
  25073. ' };',
  25074. '});',
  25075. 'this.w = false;',
  25076. '']),
  25077. LinesToStr([ // $mod.$main
  25078. '$mod.THelper.Run.call({',
  25079. ' p: $mod,',
  25080. ' get: function () {',
  25081. ' return this.p.w;',
  25082. ' },',
  25083. ' set: function (v) {',
  25084. ' this.p.w = v;',
  25085. ' }',
  25086. '}, true);',
  25087. '$mod.THelper.Run.call({',
  25088. ' a: 3 != 0,',
  25089. ' get: function () {',
  25090. ' return this.a;',
  25091. ' },',
  25092. ' set: function (v) {',
  25093. ' rtl.raiseE("EPropReadOnly");',
  25094. ' }',
  25095. '}, true);',
  25096. '']));
  25097. end;
  25098. procedure TTestModule.TestTypeHelper_Double;
  25099. begin
  25100. StartProgram(false);
  25101. Add([
  25102. '{$modeswitch typehelpers}',
  25103. 'type',
  25104. ' Float = type double;',
  25105. ' THelper = type helper for Float',
  25106. ' const NPI = 3.141592;',
  25107. ' function ToStr: String;',
  25108. ' end;',
  25109. 'function THelper.ToStr: String;',
  25110. 'begin',
  25111. 'end;',
  25112. 'procedure DoIt(s: string);',
  25113. 'begin',
  25114. 'end;',
  25115. 'var f: Float;',
  25116. 'begin',
  25117. ' DoIt(f.toStr);',
  25118. ' DoIt(f.toStr());',
  25119. ' (f*f).toStr;',
  25120. ' DoIt((f*f).toStr);',
  25121. '']);
  25122. ConvertProgram;
  25123. CheckSource('TestTypeHelper_Double',
  25124. LinesToStr([ // statements
  25125. 'rtl.createHelper(this, "THelper", null, function () {',
  25126. ' this.NPI = 3.141592;',
  25127. ' this.ToStr = function () {',
  25128. ' var Result = "";',
  25129. ' return Result;',
  25130. ' };',
  25131. '});',
  25132. 'this.DoIt = function (s) {',
  25133. '};',
  25134. 'this.f = 0.0;',
  25135. '']),
  25136. LinesToStr([ // $mod.$main
  25137. '$mod.DoIt($mod.THelper.ToStr.call({',
  25138. ' p: $mod,',
  25139. ' get: function () {',
  25140. ' return this.p.f;',
  25141. ' },',
  25142. ' set: function (v) {',
  25143. ' this.p.f = v;',
  25144. ' }',
  25145. '}));',
  25146. '$mod.DoIt($mod.THelper.ToStr.call({',
  25147. ' p: $mod,',
  25148. ' get: function () {',
  25149. ' return this.p.f;',
  25150. ' },',
  25151. ' set: function (v) {',
  25152. ' this.p.f = v;',
  25153. ' }',
  25154. '}));',
  25155. '$mod.THelper.ToStr.call({',
  25156. ' a: $mod.f * $mod.f,',
  25157. ' get: function () {',
  25158. ' return this.a;',
  25159. ' },',
  25160. ' set: function (v) {',
  25161. ' rtl.raiseE("EPropReadOnly");',
  25162. ' }',
  25163. '});',
  25164. '$mod.DoIt($mod.THelper.ToStr.call({',
  25165. ' a: $mod.f * $mod.f,',
  25166. ' get: function () {',
  25167. ' return this.a;',
  25168. ' },',
  25169. ' set: function (v) {',
  25170. ' rtl.raiseE("EPropReadOnly");',
  25171. ' }',
  25172. '}));',
  25173. '']));
  25174. end;
  25175. procedure TTestModule.TestTypeHelper_NativeInt;
  25176. begin
  25177. StartProgram(false);
  25178. Add([
  25179. '{$modeswitch typehelpers}',
  25180. 'type',
  25181. ' MaxInt = type nativeint;',
  25182. ' THelperI = type helper for MaxInt',
  25183. ' function ToStr: String;',
  25184. ' end;',
  25185. ' MaxUInt = type nativeuint;',
  25186. ' THelperU = type helper for MaxUInt',
  25187. ' function ToStr: String;',
  25188. ' end;',
  25189. 'function THelperI.ToStr: String;',
  25190. 'begin',
  25191. ' Result:=str(Self);',
  25192. 'end;',
  25193. 'function THelperU.ToStr: String;',
  25194. 'begin',
  25195. ' Result:=str(Self);',
  25196. 'end;',
  25197. 'procedure DoIt(s: string);',
  25198. 'begin',
  25199. 'end;',
  25200. 'var i: MaxInt;',
  25201. 'begin',
  25202. ' DoIt(i.toStr);',
  25203. ' DoIt(i.toStr());',
  25204. ' (i*i).toStr;',
  25205. ' DoIt((i*i).toStr);',
  25206. '']);
  25207. ConvertProgram;
  25208. CheckSource('TestTypeHelper_NativeInt',
  25209. LinesToStr([ // statements
  25210. 'rtl.createHelper(this, "THelperI", null, function () {',
  25211. ' this.ToStr = function () {',
  25212. ' var Result = "";',
  25213. ' Result = "" + this.get();',
  25214. ' return Result;',
  25215. ' };',
  25216. '});',
  25217. 'rtl.createHelper(this, "THelperU", null, function () {',
  25218. ' this.ToStr = function () {',
  25219. ' var Result = "";',
  25220. ' Result = "" + this.get();',
  25221. ' return Result;',
  25222. ' };',
  25223. '});',
  25224. 'this.DoIt = function (s) {',
  25225. '};',
  25226. 'this.i = 0;',
  25227. '']),
  25228. LinesToStr([ // $mod.$main
  25229. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25230. ' p: $mod,',
  25231. ' get: function () {',
  25232. ' return this.p.i;',
  25233. ' },',
  25234. ' set: function (v) {',
  25235. ' this.p.i = v;',
  25236. ' }',
  25237. '}));',
  25238. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25239. ' p: $mod,',
  25240. ' get: function () {',
  25241. ' return this.p.i;',
  25242. ' },',
  25243. ' set: function (v) {',
  25244. ' this.p.i = v;',
  25245. ' }',
  25246. '}));',
  25247. '$mod.THelperI.ToStr.call({',
  25248. ' a: $mod.i * $mod.i,',
  25249. ' get: function () {',
  25250. ' return this.a;',
  25251. ' },',
  25252. ' set: function (v) {',
  25253. ' rtl.raiseE("EPropReadOnly");',
  25254. ' }',
  25255. '});',
  25256. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25257. ' a: $mod.i * $mod.i,',
  25258. ' get: function () {',
  25259. ' return this.a;',
  25260. ' },',
  25261. ' set: function (v) {',
  25262. ' rtl.raiseE("EPropReadOnly");',
  25263. ' }',
  25264. '}));',
  25265. '']));
  25266. end;
  25267. procedure TTestModule.TestTypeHelper_StringChar;
  25268. begin
  25269. StartProgram(false);
  25270. Add([
  25271. '{$modeswitch typehelpers}',
  25272. 'type',
  25273. ' TStringHelper = type helper for string',
  25274. ' procedure DoIt(e: byte = 123);',
  25275. ' end;',
  25276. ' TCharHelper = type helper for char',
  25277. ' procedure Fly;',
  25278. ' end;',
  25279. 'procedure TStringHelper.DoIt(e: byte);',
  25280. 'begin',
  25281. ' Self[1]:=''c'';',
  25282. ' Self[2]:=Self[3];',
  25283. 'end;',
  25284. 'procedure TCharHelper.Fly;',
  25285. 'begin',
  25286. ' Self:=''c'';',
  25287. 'end;',
  25288. 'begin',
  25289. ' ''abc''.DoIt;',
  25290. ' ''xyz''.DoIt();',
  25291. ' ''c''.Fly();',
  25292. '']);
  25293. ConvertProgram;
  25294. CheckSource('TestTypeHelper_StringChar',
  25295. LinesToStr([ // statements
  25296. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  25297. ' this.DoIt = function (e) {',
  25298. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  25299. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  25300. ' };',
  25301. '});',
  25302. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  25303. ' this.Fly = function () {',
  25304. ' this.set("c");',
  25305. ' };',
  25306. '});',
  25307. '']),
  25308. LinesToStr([ // $mod.$main
  25309. '$mod.TStringHelper.DoIt.call({',
  25310. ' get: function () {',
  25311. ' return "abc";',
  25312. ' },',
  25313. ' set: function (v) {',
  25314. ' rtl.raiseE("EPropReadOnly");',
  25315. ' }',
  25316. '}, 123);',
  25317. '$mod.TStringHelper.DoIt.call({',
  25318. ' get: function () {',
  25319. ' return "xyz";',
  25320. ' },',
  25321. ' set: function (v) {',
  25322. ' rtl.raiseE("EPropReadOnly");',
  25323. ' }',
  25324. '}, 123);',
  25325. '$mod.TCharHelper.Fly.call({',
  25326. ' get: function () {',
  25327. ' return "c";',
  25328. ' },',
  25329. ' set: function (v) {',
  25330. ' rtl.raiseE("EPropReadOnly");',
  25331. ' }',
  25332. '});',
  25333. '']));
  25334. end;
  25335. procedure TTestModule.TestTypeHelper_JSValue;
  25336. begin
  25337. StartProgram(false);
  25338. Add([
  25339. '{$modeswitch typehelpers}',
  25340. 'type',
  25341. ' TExtValue = type jsvalue;',
  25342. ' THelper = type helper for TExtValue',
  25343. ' function ToStr: String;',
  25344. ' end;',
  25345. 'function THelper.ToStr: String;',
  25346. 'begin',
  25347. 'end;',
  25348. 'var',
  25349. ' s: string;',
  25350. ' v: TExtValue;',
  25351. 'begin',
  25352. ' s:=v.toStr;',
  25353. ' s:=v.toStr();',
  25354. ' TExtValue(s).toStr;',
  25355. '']);
  25356. ConvertProgram;
  25357. CheckSource('TestTypeHelper_JSValue',
  25358. LinesToStr([ // statements
  25359. 'rtl.createHelper(this, "THelper", null, function () {',
  25360. ' this.ToStr = function () {',
  25361. ' var Result = "";',
  25362. ' return Result;',
  25363. ' };',
  25364. '});',
  25365. 'this.s = "";',
  25366. 'this.v = undefined;',
  25367. '']),
  25368. LinesToStr([ // $mod.$main
  25369. '$mod.s = $mod.THelper.ToStr.call({',
  25370. ' p: $mod,',
  25371. ' get: function () {',
  25372. ' return this.p.v;',
  25373. ' },',
  25374. ' set: function (v) {',
  25375. ' this.p.v = v;',
  25376. ' }',
  25377. '});',
  25378. '$mod.s = $mod.THelper.ToStr.call({',
  25379. ' p: $mod,',
  25380. ' get: function () {',
  25381. ' return this.p.v;',
  25382. ' },',
  25383. ' set: function (v) {',
  25384. ' this.p.v = v;',
  25385. ' }',
  25386. '});',
  25387. '$mod.THelper.ToStr.call({',
  25388. ' p: $mod,',
  25389. ' get: function () {',
  25390. ' return this.p.s;',
  25391. ' },',
  25392. ' set: function (v) {',
  25393. ' rtl.raiseE("EPropReadOnly");',
  25394. ' }',
  25395. '});',
  25396. '']));
  25397. end;
  25398. procedure TTestModule.TestTypeHelper_Array;
  25399. begin
  25400. StartProgram(false);
  25401. Add([
  25402. '{$modeswitch typehelpers}',
  25403. 'type',
  25404. ' TArrOfBool = array of boolean;',
  25405. ' TArrOfJS = array of jsvalue;',
  25406. ' THelper = type helper for TArrOfBool',
  25407. ' procedure DoIt(e: byte = 123);',
  25408. ' end;',
  25409. 'procedure THelper.DoIt(e: byte);',
  25410. 'begin',
  25411. ' Self[1]:=true;',
  25412. ' Self[2]:=not Self[3];',
  25413. ' SetLength(Self,4);',
  25414. 'end;',
  25415. 'var',
  25416. ' b: TArrOfBool;',
  25417. ' j: TArrOfJS;',
  25418. 'begin',
  25419. ' b.DoIt;',
  25420. ' TArrOfBool(j).DoIt();',
  25421. '']);
  25422. ConvertProgram;
  25423. CheckSource('TestTypeHelper_Array',
  25424. LinesToStr([ // statements
  25425. 'rtl.createHelper(this, "THelper", null, function () {',
  25426. ' this.DoIt = function (e) {',
  25427. ' this.get()[1] = true;',
  25428. ' this.get()[2] = !this.get()[3];',
  25429. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  25430. ' };',
  25431. '});',
  25432. 'this.b = [];',
  25433. 'this.j = [];',
  25434. '']),
  25435. LinesToStr([ // $mod.$main
  25436. '$mod.THelper.DoIt.call({',
  25437. ' p: $mod,',
  25438. ' get: function () {',
  25439. ' return this.p.b;',
  25440. ' },',
  25441. ' set: function (v) {',
  25442. ' this.p.b = v;',
  25443. ' }',
  25444. '}, 123);',
  25445. '$mod.THelper.DoIt.call({',
  25446. ' p: $mod,',
  25447. ' get: function () {',
  25448. ' return this.p.j;',
  25449. ' },',
  25450. ' set: function (v) {',
  25451. ' this.p.j = v;',
  25452. ' }',
  25453. '}, 123);',
  25454. '']));
  25455. end;
  25456. procedure TTestModule.TestTypeHelper_EnumType;
  25457. begin
  25458. StartProgram(false);
  25459. Add([
  25460. '{$modeswitch typehelpers}',
  25461. 'type',
  25462. ' TEnum = (red,blue);',
  25463. ' THelper = type helper for TEnum',
  25464. ' procedure DoIt(e: byte = 123);',
  25465. ' class procedure Swing(w: word); static;',
  25466. ' end;',
  25467. 'procedure THelper.DoIt(e: byte);',
  25468. 'begin',
  25469. ' Self:=red;',
  25470. ' Self:=succ(Self);',
  25471. ' with Self do Doit;',
  25472. 'end;',
  25473. 'class procedure THelper.Swing(w: word);',
  25474. 'begin',
  25475. 'end;',
  25476. 'var e: TEnum;',
  25477. 'begin',
  25478. ' e.DoIt;',
  25479. ' red.DoIt;',
  25480. ' TEnum.blue.DoIt;',
  25481. ' TEnum(1).DoIt;',
  25482. ' TEnum.Swing(3);',
  25483. '']);
  25484. ConvertProgram;
  25485. CheckSource('TestTypeHelper_EnumType',
  25486. LinesToStr([ // statements
  25487. 'this.TEnum = {',
  25488. ' "0": "red",',
  25489. ' red: 0,',
  25490. ' "1": "blue",',
  25491. ' blue: 1',
  25492. '};',
  25493. 'rtl.createHelper(this, "THelper", null, function () {',
  25494. ' this.DoIt = function (e) {',
  25495. ' this.set($mod.TEnum.red);',
  25496. ' this.set(this.get() + 1);',
  25497. ' var $with = this.get();',
  25498. ' $mod.THelper.DoIt.call(this, 123);',
  25499. ' };',
  25500. ' this.Swing = function (w) {',
  25501. ' };',
  25502. '});',
  25503. 'this.e = 0;',
  25504. '']),
  25505. LinesToStr([ // $mod.$main
  25506. '$mod.THelper.DoIt.call({',
  25507. ' p: $mod,',
  25508. ' get: function () {',
  25509. ' return this.p.e;',
  25510. ' },',
  25511. ' set: function (v) {',
  25512. ' this.p.e = v;',
  25513. ' }',
  25514. '}, 123);',
  25515. '$mod.THelper.DoIt.call({',
  25516. ' p: $mod.TEnum,',
  25517. ' get: function () {',
  25518. ' return this.p.red;',
  25519. ' },',
  25520. ' set: function (v) {',
  25521. ' rtl.raiseE("EPropReadOnly");',
  25522. ' }',
  25523. '}, 123);',
  25524. '$mod.THelper.DoIt.call({',
  25525. ' p: $mod.TEnum,',
  25526. ' get: function () {',
  25527. ' return this.p.blue;',
  25528. ' },',
  25529. ' set: function (v) {',
  25530. ' rtl.raiseE("EPropReadOnly");',
  25531. ' }',
  25532. '}, 123);',
  25533. '$mod.THelper.DoIt.call({',
  25534. ' get: function () {',
  25535. ' return 1;',
  25536. ' },',
  25537. ' set: function (v) {',
  25538. ' rtl.raiseE("EPropReadOnly");',
  25539. ' }',
  25540. '}, 123);',
  25541. '$mod.THelper.Swing(3);',
  25542. '']));
  25543. end;
  25544. procedure TTestModule.TestTypeHelper_SetType;
  25545. begin
  25546. StartProgram(false);
  25547. Add([
  25548. '{$modeswitch typehelpers}',
  25549. 'type',
  25550. ' TEnum = (red,blue);',
  25551. ' TSetOfEnum = set of TEnum;',
  25552. ' THelper = type helper for TSetOfEnum',
  25553. ' procedure DoIt(e: byte = 123);',
  25554. ' constructor Init(e: TEnum);',
  25555. ' constructor InitEmpty;',
  25556. ' end;',
  25557. 'procedure THelper.DoIt(e: byte);',
  25558. 'begin',
  25559. ' Self:=[];',
  25560. ' Self:=[red];',
  25561. ' Include(Self,blue);',
  25562. 'end;',
  25563. 'constructor THelper.Init(e: TEnum);',
  25564. 'begin',
  25565. ' Self:=[];',
  25566. ' Self:=[e];',
  25567. ' Include(Self,blue);',
  25568. 'end;',
  25569. 'constructor THelper.InitEmpty;',
  25570. 'begin',
  25571. 'end;',
  25572. 'var s: TSetOfEnum;',
  25573. 'begin',
  25574. ' s.DoIt;',
  25575. //' [red].DoIt;',
  25576. //' with s do DoIt;',
  25577. //' with [red,blue] do DoIt;',
  25578. ' s:=TSetOfEnum.Init(blue);',
  25579. ' s:=s.Init(blue);',
  25580. '']);
  25581. ConvertProgram;
  25582. CheckSource('TestTypeHelper_SetType',
  25583. LinesToStr([ // statements
  25584. 'this.TEnum = {',
  25585. ' "0": "red",',
  25586. ' red: 0,',
  25587. ' "1": "blue",',
  25588. ' blue: 1',
  25589. '};',
  25590. 'rtl.createHelper(this, "THelper", null, function () {',
  25591. ' this.DoIt = function (e) {',
  25592. ' this.set({});',
  25593. ' this.set(rtl.createSet($mod.TEnum.red));',
  25594. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25595. ' };',
  25596. ' this.Init = function (e) {',
  25597. ' this.set({});',
  25598. ' this.set(rtl.createSet(e));',
  25599. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25600. ' return this.get();',
  25601. ' };',
  25602. ' this.InitEmpty = function () {',
  25603. ' return this.get();',
  25604. ' };',
  25605. ' this.$new = function (fn, args) {',
  25606. ' return this[fn].apply({',
  25607. ' p: {},',
  25608. ' get: function () {',
  25609. ' return this.p;',
  25610. ' },',
  25611. ' set: function (v) {',
  25612. ' this.p = v;',
  25613. ' }',
  25614. ' }, args);',
  25615. ' };',
  25616. '});',
  25617. 'this.s = {};',
  25618. '']),
  25619. LinesToStr([ // $mod.$main
  25620. '$mod.THelper.DoIt.call({',
  25621. ' p: $mod,',
  25622. ' get: function () {',
  25623. ' return this.p.s;',
  25624. ' },',
  25625. ' set: function (v) {',
  25626. ' this.p.s = v;',
  25627. ' }',
  25628. '}, 123);',
  25629. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25630. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25631. ' p: $mod,',
  25632. ' get: function () {',
  25633. ' return this.p.s;',
  25634. ' },',
  25635. ' set: function (v) {',
  25636. ' this.p.s = v;',
  25637. ' }',
  25638. '}, $mod.TEnum.blue));',
  25639. '']));
  25640. end;
  25641. procedure TTestModule.TestTypeHelper_InterfaceType;
  25642. begin
  25643. StartProgram(false);
  25644. Add([
  25645. '{$interfaces com}',
  25646. '{$modeswitch typehelpers}',
  25647. 'type',
  25648. ' IUnknown = interface',
  25649. ' function _AddRef: longint;',
  25650. ' function _Release: longint;',
  25651. ' end;',
  25652. ' TObject = class(IUnknown)',
  25653. ' function _AddRef: longint; virtual; abstract;',
  25654. ' function _Release: longint; virtual; abstract;',
  25655. ' end;',
  25656. ' THelper = type helper for IUnknown',
  25657. ' procedure Fly(e: byte = 123);',
  25658. ' class procedure Run; static;',
  25659. ' end;',
  25660. 'var',
  25661. ' i: IUnknown;',
  25662. ' o: TObject;',
  25663. 'procedure THelper.Fly(e: byte);',
  25664. 'begin',
  25665. ' i:=Self;',
  25666. ' o:=Self as TObject;',
  25667. ' Self:=nil;',
  25668. ' Self:=i;',
  25669. ' Self:=o;',
  25670. ' with Self do begin',
  25671. ' Fly;',
  25672. ' Fly();',
  25673. ' end;',
  25674. 'end;',
  25675. 'class procedure THelper.Run;',
  25676. 'var l: IUnknown;',
  25677. 'begin',
  25678. ' l.Fly;',
  25679. ' l.Fly();',
  25680. 'end;',
  25681. 'begin',
  25682. ' i.Fly;',
  25683. ' i.Fly();',
  25684. ' i.Run;',
  25685. ' i.Run();',
  25686. ' IUnknown.Run;',
  25687. ' IUnknown.Run();',
  25688. '']);
  25689. ConvertProgram;
  25690. CheckSource('TestTypeHelper_InterfaceType',
  25691. LinesToStr([ // statements
  25692. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25693. 'rtl.createClass(this, "TObject", null, function () {',
  25694. ' this.$init = function () {',
  25695. ' };',
  25696. ' this.$final = function () {',
  25697. ' };',
  25698. ' rtl.addIntf(this, $mod.IUnknown);',
  25699. '});',
  25700. 'rtl.createHelper(this, "THelper", null, function () {',
  25701. ' this.Fly = function (e) {',
  25702. ' var $ir = rtl.createIntfRefs();',
  25703. ' try {',
  25704. ' rtl.setIntfP($mod, "i", this.get());',
  25705. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25706. ' this.set(null);',
  25707. ' this.set($mod.i);',
  25708. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25709. ' var $with = this.get();',
  25710. ' $mod.THelper.Fly.call(this, 123);',
  25711. ' $mod.THelper.Fly.call(this, 123);',
  25712. ' } finally {',
  25713. ' $ir.free();',
  25714. ' };',
  25715. ' };',
  25716. ' this.Run = function () {',
  25717. ' var l = null;',
  25718. ' try {',
  25719. ' $mod.THelper.Fly.call({',
  25720. ' get: function () {',
  25721. ' return l;',
  25722. ' },',
  25723. ' set: function (v) {',
  25724. ' l = rtl.setIntfL(l, v);',
  25725. ' }',
  25726. ' }, 123);',
  25727. ' $mod.THelper.Fly.call({',
  25728. ' get: function () {',
  25729. ' return l;',
  25730. ' },',
  25731. ' set: function (v) {',
  25732. ' l = rtl.setIntfL(l, v);',
  25733. ' }',
  25734. ' }, 123);',
  25735. ' } finally {',
  25736. ' rtl._Release(l);',
  25737. ' };',
  25738. ' };',
  25739. '});',
  25740. 'this.i = null;',
  25741. 'this.o = null;',
  25742. '']),
  25743. LinesToStr([ // $mod.$main
  25744. '$mod.THelper.Fly.call({',
  25745. ' p: $mod,',
  25746. ' get: function () {',
  25747. ' return this.p.i;',
  25748. ' },',
  25749. ' set: function (v) {',
  25750. ' rtl.setIntfP(this.p, "i", v);',
  25751. ' }',
  25752. '}, 123);',
  25753. '$mod.THelper.Fly.call({',
  25754. ' p: $mod,',
  25755. ' get: function () {',
  25756. ' return this.p.i;',
  25757. ' },',
  25758. ' set: function (v) {',
  25759. ' rtl.setIntfP(this.p, "i", v);',
  25760. ' }',
  25761. '}, 123);',
  25762. '$mod.THelper.Run();',
  25763. '$mod.THelper.Run();',
  25764. '$mod.THelper.Run();',
  25765. '$mod.THelper.Run();',
  25766. '']));
  25767. end;
  25768. procedure TTestModule.TestTypeHelper_NestedSelf;
  25769. begin
  25770. StartProgram(false);
  25771. Add([
  25772. '{$modeswitch typehelpers}',
  25773. 'type',
  25774. ' THelper = type helper for string',
  25775. ' procedure Run(Value: string);',
  25776. ' end;',
  25777. 'procedure THelper.Run(Value: string);',
  25778. ' function Sub(i: nativeint): boolean;',
  25779. ' begin',
  25780. ' Result:=Self[i+1]=Value[i];',
  25781. ' end;',
  25782. 'begin',
  25783. ' if Self[3]=Value[4] then ;',
  25784. 'end;',
  25785. 'begin',
  25786. '']);
  25787. ConvertProgram;
  25788. CheckSource('TestTypeHelper_NestedSelf',
  25789. LinesToStr([ // statements
  25790. 'rtl.createHelper(this, "THelper", null, function () {',
  25791. ' this.Run = function (Value) {',
  25792. ' var $Self = this;',
  25793. ' function Sub(i) {',
  25794. ' var Result = false;',
  25795. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25796. ' return Result;',
  25797. ' };',
  25798. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25799. ' };',
  25800. '});',
  25801. '']),
  25802. LinesToStr([ // $mod.$main
  25803. '']));
  25804. end;
  25805. procedure TTestModule.TestProcType;
  25806. begin
  25807. StartProgram(false);
  25808. Add([
  25809. 'type',
  25810. ' TProcInt = procedure(vI: longint = 1);',
  25811. 'procedure DoIt(vJ: longint);',
  25812. 'begin end;',
  25813. 'var',
  25814. ' b: boolean;',
  25815. ' vP, vQ: tprocint;',
  25816. 'begin',
  25817. ' vp:=nil;',
  25818. ' vp:=vp;',
  25819. ' vp:=@doit;',
  25820. ' vp;',
  25821. ' vp();',
  25822. ' vp(2);',
  25823. ' b:=vp=nil;',
  25824. ' b:=nil=vp;',
  25825. ' b:=vp=vq;',
  25826. ' b:=vp=@doit;',
  25827. ' b:=@doit=vp;',
  25828. ' b:=vp<>nil;',
  25829. ' b:=nil<>vp;',
  25830. ' b:=vp<>vq;',
  25831. ' b:=vp<>@doit;',
  25832. ' b:=@doit<>vp;',
  25833. ' b:=Assigned(vp);',
  25834. ' if Assigned(vp) then ;']);
  25835. ConvertProgram;
  25836. CheckSource('TestProcType',
  25837. LinesToStr([ // statements
  25838. 'this.DoIt = function(vJ) {',
  25839. '};',
  25840. 'this.b = false;',
  25841. 'this.vP = null;',
  25842. 'this.vQ = null;'
  25843. ]),
  25844. LinesToStr([ // $mod.$main
  25845. '$mod.vP = null;',
  25846. '$mod.vP = $mod.vP;',
  25847. '$mod.vP = $mod.DoIt;',
  25848. '$mod.vP(1);',
  25849. '$mod.vP(1);',
  25850. '$mod.vP(2);',
  25851. '$mod.b = $mod.vP === null;',
  25852. '$mod.b = null === $mod.vP;',
  25853. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25854. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25855. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25856. '$mod.b = $mod.vP !== null;',
  25857. '$mod.b = null !== $mod.vP;',
  25858. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25859. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25860. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25861. '$mod.b = $mod.vP != null;',
  25862. 'if ($mod.vP != null) ;',
  25863. '']));
  25864. end;
  25865. procedure TTestModule.TestProcType_Arg;
  25866. begin
  25867. StartProgram(false);
  25868. Add([
  25869. 'type',
  25870. ' TProcInt = procedure(vI: longint = 1);',
  25871. 'procedure DoIt(vJ: longint); begin end;',
  25872. 'procedure DoSome(vP, vQ: TProcInt);',
  25873. 'var',
  25874. ' b: boolean;',
  25875. 'begin',
  25876. ' vp:=nil;',
  25877. ' vp:=vp;',
  25878. ' vp:=@doit;',
  25879. ' vp;',
  25880. ' vp();',
  25881. ' vp(2);',
  25882. ' b:=vp=nil;',
  25883. ' b:=nil=vp;',
  25884. ' b:=vp=vq;',
  25885. ' b:=vp=@doit;',
  25886. ' b:=@doit=vp;',
  25887. ' b:=vp<>nil;',
  25888. ' b:=nil<>vp;',
  25889. ' b:=vp<>vq;',
  25890. ' b:=vp<>@doit;',
  25891. ' b:=@doit<>vp;',
  25892. ' b:=Assigned(vp);',
  25893. ' if Assigned(vp) then ;',
  25894. 'end;',
  25895. 'begin',
  25896. ' DoSome(@DoIt,nil);']);
  25897. ConvertProgram;
  25898. CheckSource('TestProcType_Arg',
  25899. LinesToStr([ // statements
  25900. 'this.DoIt = function(vJ) {',
  25901. '};',
  25902. 'this.DoSome = function(vP, vQ) {',
  25903. ' var b = false;',
  25904. ' vP = null;',
  25905. ' vP = vP;',
  25906. ' vP = $mod.DoIt;',
  25907. ' vP(1);',
  25908. ' vP(1);',
  25909. ' vP(2);',
  25910. ' b = vP === null;',
  25911. ' b = null === vP;',
  25912. ' b = rtl.eqCallback(vP,vQ);',
  25913. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25914. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25915. ' b = vP !== null;',
  25916. ' b = null !== vP;',
  25917. ' b = !rtl.eqCallback(vP, vQ);',
  25918. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25919. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25920. ' b = vP != null;',
  25921. ' if (vP != null) ;',
  25922. '};',
  25923. '']),
  25924. LinesToStr([ // $mod.$main
  25925. '$mod.DoSome($mod.DoIt,null);',
  25926. '']));
  25927. end;
  25928. procedure TTestModule.TestProcType_FunctionFPC;
  25929. begin
  25930. StartProgram(false);
  25931. Add('type');
  25932. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25933. Add('function DoIt(vI: longint): longint;');
  25934. Add('begin end;');
  25935. Add('var');
  25936. Add(' b: boolean;');
  25937. Add(' vP, vQ: tfuncint;');
  25938. Add('begin');
  25939. Add(' vp:=nil;');
  25940. Add(' vp:=vp;');
  25941. Add(' vp:=@doit;'); // ok in fpc and delphi
  25942. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25943. Add(' vp;'); // ok in fpc and delphi
  25944. Add(' vp();');
  25945. Add(' vp(2);');
  25946. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25947. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25948. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25949. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25950. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25951. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25952. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25953. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25954. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25955. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25956. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25957. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25958. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25959. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25960. Add(' b:=Assigned(vp);');
  25961. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25962. Add(' doit(vp());'); // ok in fpc and delphi
  25963. Add(' doit(vp(2));'); // ok in fpc and delphi
  25964. ConvertProgram;
  25965. CheckSource('TestProcType_FunctionFPC',
  25966. LinesToStr([ // statements
  25967. 'this.DoIt = function(vI) {',
  25968. ' var Result = 0;',
  25969. ' return Result;',
  25970. '};',
  25971. 'this.b = false;',
  25972. 'this.vP = null;',
  25973. 'this.vQ = null;'
  25974. ]),
  25975. LinesToStr([ // $mod.$main
  25976. '$mod.vP = null;',
  25977. '$mod.vP = $mod.vP;',
  25978. '$mod.vP = $mod.DoIt;',
  25979. '$mod.vP(1);',
  25980. '$mod.vP(1);',
  25981. '$mod.vP(2);',
  25982. '$mod.b = $mod.vP === null;',
  25983. '$mod.b = null === $mod.vP;',
  25984. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25985. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25986. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25987. '$mod.b = 4 === $mod.vP(1);',
  25988. '$mod.b = $mod.vP !== null;',
  25989. '$mod.b = null !== $mod.vP;',
  25990. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25991. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25992. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25993. '$mod.b = 6 !== $mod.vP(1);',
  25994. '$mod.b = $mod.vP != null;',
  25995. '$mod.DoIt($mod.vP(1));',
  25996. '$mod.DoIt($mod.vP(2));',
  25997. '']));
  25998. end;
  25999. procedure TTestModule.TestProcType_FunctionDelphi;
  26000. begin
  26001. StartProgram(false);
  26002. Add('{$mode Delphi}');
  26003. Add('type');
  26004. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26005. Add('function DoIt(vI: longint): longint;');
  26006. Add('begin end;');
  26007. Add('var');
  26008. Add(' b: boolean;');
  26009. Add(' vP, vQ: tfuncint;');
  26010. Add('begin');
  26011. Add(' vp:=nil;');
  26012. Add(' vp:=vp;');
  26013. Add(' vp:=@doit;'); // ok in fpc and delphi
  26014. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26015. Add(' vp;'); // ok in fpc and delphi
  26016. Add(' vp();');
  26017. Add(' vp(2);');
  26018. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26019. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26020. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26021. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26022. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26023. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26024. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26025. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26026. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26027. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26028. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26029. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26030. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26031. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26032. Add(' b:=Assigned(vp);');
  26033. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26034. Add(' doit(vp());'); // ok in fpc and delphi
  26035. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  26036. ConvertProgram;
  26037. CheckSource('TestProcType_FunctionDelphi',
  26038. LinesToStr([ // statements
  26039. 'this.DoIt = function(vI) {',
  26040. ' var Result = 0;',
  26041. ' return Result;',
  26042. '};',
  26043. 'this.b = false;',
  26044. 'this.vP = null;',
  26045. 'this.vQ = null;'
  26046. ]),
  26047. LinesToStr([ // $mod.$main
  26048. '$mod.vP = null;',
  26049. '$mod.vP = $mod.vP;',
  26050. '$mod.vP = $mod.DoIt;',
  26051. '$mod.vP = $mod.DoIt;',
  26052. '$mod.vP(1);',
  26053. '$mod.vP(1);',
  26054. '$mod.vP(2);',
  26055. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  26056. '$mod.b = $mod.vP(1) === 3;',
  26057. '$mod.b = 4 === $mod.vP(1);',
  26058. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  26059. '$mod.b = $mod.vP(1) !== 5;',
  26060. '$mod.b = 6 !== $mod.vP(1);',
  26061. '$mod.b = $mod.vP != null;',
  26062. '$mod.DoIt($mod.vP(1));',
  26063. '$mod.DoIt($mod.vP(1));',
  26064. '$mod.DoIt($mod.vP(2));',
  26065. '']));
  26066. end;
  26067. procedure TTestModule.TestProcType_ProcedureDelphi;
  26068. begin
  26069. StartProgram(false);
  26070. Add('{$mode Delphi}');
  26071. Add('type');
  26072. Add(' TProc = procedure;');
  26073. Add('procedure DoIt;');
  26074. Add('begin end;');
  26075. Add('var');
  26076. Add(' b: boolean;');
  26077. Add(' vP, vQ: tproc;');
  26078. Add('begin');
  26079. Add(' vp:=nil;');
  26080. Add(' vp:=vp;');
  26081. Add(' vp:=vq;');
  26082. 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
  26083. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26084. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  26085. Add(' vp;'); // ok in fpc and delphi
  26086. Add(' vp();');
  26087. // equal
  26088. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26089. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  26090. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26091. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26092. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  26093. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26094. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26095. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  26096. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26097. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26098. // unequal
  26099. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26100. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  26101. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26102. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  26103. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26104. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  26105. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26106. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  26107. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26108. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  26109. Add(' b:=Assigned(vp);');
  26110. ConvertProgram;
  26111. CheckSource('TestProcType_ProcedureDelphi',
  26112. LinesToStr([ // statements
  26113. 'this.DoIt = function() {',
  26114. '};',
  26115. 'this.b = false;',
  26116. 'this.vP = null;',
  26117. 'this.vQ = null;'
  26118. ]),
  26119. LinesToStr([ // $mod.$main
  26120. '$mod.vP = null;',
  26121. '$mod.vP = $mod.vP;',
  26122. '$mod.vP = $mod.vQ;',
  26123. '$mod.vP = $mod.DoIt;',
  26124. '$mod.vP = $mod.DoIt;',
  26125. '$mod.vP();',
  26126. '$mod.vP();',
  26127. '$mod.b = $mod.vP === null;',
  26128. '$mod.b = null === $mod.vP;',
  26129. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  26130. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26131. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26132. '$mod.b = $mod.vP !== null;',
  26133. '$mod.b = null !== $mod.vP;',
  26134. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  26135. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26136. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26137. '$mod.b = $mod.vP != null;',
  26138. '']));
  26139. end;
  26140. procedure TTestModule.TestProcType_AsParam;
  26141. begin
  26142. StartProgram(false);
  26143. Add('type');
  26144. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26145. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  26146. Add('var vJ: tfuncint;');
  26147. Add('begin');
  26148. Add(' vg:=vg;');
  26149. Add(' vj:=vh;');
  26150. Add(' vi:=vi;');
  26151. Add(' doit(vg,vg,vg);');
  26152. Add(' doit(vh,vh,vj);');
  26153. Add(' doit(vi,vi,vi);');
  26154. Add(' doit(vj,vj,vj);');
  26155. Add('end;');
  26156. Add('var i: tfuncint;');
  26157. Add('begin');
  26158. Add(' doit(i,i,i);');
  26159. ConvertProgram;
  26160. CheckSource('TestProcType_AsParam',
  26161. LinesToStr([ // statements
  26162. 'this.DoIt = function (vG,vH,vI) {',
  26163. ' var vJ = null;',
  26164. ' vG = vG;',
  26165. ' vJ = vH;',
  26166. ' vI.set(vI.get());',
  26167. ' $mod.DoIt(vG, vG, {',
  26168. ' get: function () {',
  26169. ' return vG;',
  26170. ' },',
  26171. ' set: function (v) {',
  26172. ' vG = v;',
  26173. ' }',
  26174. ' });',
  26175. ' $mod.DoIt(vH, vH, {',
  26176. ' get: function () {',
  26177. ' return vJ;',
  26178. ' },',
  26179. ' set: function (v) {',
  26180. ' vJ = v;',
  26181. ' }',
  26182. ' });',
  26183. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  26184. ' $mod.DoIt(vJ, vJ, {',
  26185. ' get: function () {',
  26186. ' return vJ;',
  26187. ' },',
  26188. ' set: function (v) {',
  26189. ' vJ = v;',
  26190. ' }',
  26191. ' });',
  26192. '};',
  26193. 'this.i = null;'
  26194. ]),
  26195. LinesToStr([
  26196. '$mod.DoIt($mod.i,$mod.i,{',
  26197. ' p: $mod,',
  26198. ' get: function () {',
  26199. ' return this.p.i;',
  26200. ' },',
  26201. ' set: function (v) {',
  26202. ' this.p.i = v;',
  26203. ' }',
  26204. '});'
  26205. ]));
  26206. end;
  26207. procedure TTestModule.TestProcType_MethodFPC;
  26208. begin
  26209. StartProgram(false);
  26210. Add('type');
  26211. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26212. Add(' TObject = class');
  26213. Add(' function DoIt(vA: longint = 1): longint;');
  26214. Add(' end;');
  26215. Add('function TObject.DoIt(vA: longint = 1): longint;');
  26216. Add('begin');
  26217. Add('end;');
  26218. Add('var');
  26219. Add(' Obj: TObject;');
  26220. Add(' vP: tfuncint;');
  26221. Add(' b: boolean;');
  26222. Add('begin');
  26223. Add(' vp:[email protected];'); // ok in fpc and delphi
  26224. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  26225. Add(' vp;'); // ok in fpc and delphi
  26226. Add(' vp();');
  26227. Add(' vp(2);');
  26228. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  26229. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  26230. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  26231. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26232. ConvertProgram;
  26233. CheckSource('TestProcType_MethodFPC',
  26234. LinesToStr([ // statements
  26235. 'rtl.createClass(this, "TObject", null, function () {',
  26236. ' this.$init = function () {',
  26237. ' };',
  26238. ' this.$final = function () {',
  26239. ' };',
  26240. ' this.DoIt = function (vA) {',
  26241. ' var Result = 0;',
  26242. ' return Result;',
  26243. ' };',
  26244. '});',
  26245. 'this.Obj = null;',
  26246. 'this.vP = null;',
  26247. 'this.b = false;'
  26248. ]),
  26249. LinesToStr([
  26250. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26251. '$mod.vP(1);',
  26252. '$mod.vP(1);',
  26253. '$mod.vP(2);',
  26254. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26255. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26256. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26257. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26258. '']));
  26259. end;
  26260. procedure TTestModule.TestProcType_MethodDelphi;
  26261. begin
  26262. StartProgram(false);
  26263. Add([
  26264. '{$mode delphi}',
  26265. 'type',
  26266. ' TFuncInt = function(vA: longint = 1): longint of object;',
  26267. ' TObject = class',
  26268. ' function DoIt(vA: longint = 1): longint;',
  26269. ' end;',
  26270. 'function TObject.DoIt(vA: longint = 1): longint;',
  26271. 'begin',
  26272. 'end;',
  26273. 'var',
  26274. ' Obj: TObject;',
  26275. ' vP: tfuncint;',
  26276. ' b: boolean;',
  26277. 'begin',
  26278. ' vp:[email protected];', // ok in fpc and delphi
  26279. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  26280. ' vp;', // ok in fpc and delphi
  26281. ' vp();',
  26282. ' vp(2);',
  26283. //' b:[email protected];', // ok in fpc, illegal in delphi
  26284. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  26285. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  26286. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26287. '']);
  26288. ConvertProgram;
  26289. CheckSource('TestProcType_MethodDelphi',
  26290. LinesToStr([ // statements
  26291. 'rtl.createClass(this, "TObject", null, function () {',
  26292. ' this.$init = function () {',
  26293. ' };',
  26294. ' this.$final = function () {',
  26295. ' };',
  26296. ' this.DoIt = function (vA) {',
  26297. ' var Result = 0;',
  26298. ' return Result;',
  26299. ' };',
  26300. '});',
  26301. 'this.Obj = null;',
  26302. 'this.vP = null;',
  26303. 'this.b = false;'
  26304. ]),
  26305. LinesToStr([
  26306. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26307. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26308. '$mod.vP(1);',
  26309. '$mod.vP(1);',
  26310. '$mod.vP(2);',
  26311. '']));
  26312. end;
  26313. procedure TTestModule.TestProcType_PropertyFPC;
  26314. begin
  26315. StartProgram(false);
  26316. Add('type');
  26317. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26318. Add(' TObject = class');
  26319. Add(' FOnFoo: TFuncInt;');
  26320. Add(' function DoIt(vA: longint = 1): longint;');
  26321. Add(' function GetFoo: TFuncInt;');
  26322. Add(' procedure SetFoo(const Value: TFuncInt);');
  26323. Add(' function GetEvents(Index: longint): TFuncInt;');
  26324. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26325. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26326. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26327. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26328. Add(' end;');
  26329. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26330. Add('function tobject.getfoo: tfuncint; begin end;');
  26331. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26332. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26333. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26334. Add('var');
  26335. Add(' Obj: TObject;');
  26336. Add(' vP: tfuncint;');
  26337. Add(' b: boolean;');
  26338. Add('begin');
  26339. Add(' obj.onfoo:=nil;');
  26340. Add(' obj.onbar:=nil;');
  26341. Add(' obj.events[1]:=nil;');
  26342. Add(' obj.onfoo:=obj.onfoo;');
  26343. Add(' obj.onbar:=obj.onbar;');
  26344. Add(' obj.events[2]:=obj.events[3];');
  26345. Add(' obj.onfoo:[email protected];');
  26346. Add(' obj.onbar:[email protected];');
  26347. Add(' obj.events[4]:[email protected];');
  26348. //Add(' obj.onfoo:=obj.doit;'); // delphi
  26349. //Add(' obj.onbar:=obj.doit;'); // delphi
  26350. //Add(' obj.events[4]:=obj.doit;'); // delphi
  26351. Add(' obj.onfoo;');
  26352. Add(' obj.onbar;');
  26353. //Add(' obj.events[5];'); ToDo in pasresolver
  26354. Add(' obj.onfoo();');
  26355. Add(' obj.onbar();');
  26356. Add(' obj.events[6]();');
  26357. Add(' b:=obj.onfoo=nil;');
  26358. Add(' b:=obj.onbar=nil;');
  26359. Add(' b:=obj.events[7]=nil;');
  26360. Add(' b:=obj.onfoo<>nil;');
  26361. Add(' b:=obj.onbar<>nil;');
  26362. Add(' b:=obj.events[8]<>nil;');
  26363. Add(' b:=obj.onfoo=vp;');
  26364. Add(' b:=obj.onbar=vp;');
  26365. Add(' b:=obj.events[9]=vp;');
  26366. Add(' b:=obj.onfoo=obj.onfoo;');
  26367. Add(' b:=obj.onbar=obj.onfoo;');
  26368. Add(' b:=obj.events[10]=obj.onfoo;');
  26369. Add(' b:=obj.onfoo<>obj.onfoo;');
  26370. Add(' b:=obj.onbar<>obj.onfoo;');
  26371. Add(' b:=obj.events[11]<>obj.onfoo;');
  26372. Add(' b:[email protected];');
  26373. Add(' b:[email protected];');
  26374. Add(' b:=obj.events[12][email protected];');
  26375. Add(' b:=obj.onfoo<>@obj.doit;');
  26376. Add(' b:=obj.onbar<>@obj.doit;');
  26377. Add(' b:=obj.events[12]<>@obj.doit;');
  26378. Add(' b:=Assigned(obj.onfoo);');
  26379. Add(' b:=Assigned(obj.onbar);');
  26380. Add(' b:=Assigned(obj.events[13]);');
  26381. ConvertProgram;
  26382. CheckSource('TestProcType_PropertyFPC',
  26383. LinesToStr([ // statements
  26384. 'rtl.createClass(this, "TObject", null, function () {',
  26385. ' this.$init = function () {',
  26386. ' this.FOnFoo = null;',
  26387. ' };',
  26388. ' this.$final = function () {',
  26389. ' this.FOnFoo = undefined;',
  26390. ' };',
  26391. ' this.DoIt = function (vA) {',
  26392. ' var Result = 0;',
  26393. ' return Result;',
  26394. ' };',
  26395. 'this.GetFoo = function () {',
  26396. ' var Result = null;',
  26397. ' return Result;',
  26398. '};',
  26399. 'this.SetFoo = function (Value) {',
  26400. '};',
  26401. 'this.GetEvents = function (Index) {',
  26402. ' var Result = null;',
  26403. ' return Result;',
  26404. '};',
  26405. 'this.SetEvents = function (Index, Value) {',
  26406. '};',
  26407. '});',
  26408. 'this.Obj = null;',
  26409. 'this.vP = null;',
  26410. 'this.b = false;'
  26411. ]),
  26412. LinesToStr([
  26413. '$mod.Obj.FOnFoo = null;',
  26414. '$mod.Obj.SetFoo(null);',
  26415. '$mod.Obj.SetEvents(1, null);',
  26416. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26417. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26418. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26419. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26420. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26421. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26422. '$mod.Obj.FOnFoo(1);',
  26423. '$mod.Obj.GetFoo();',
  26424. '$mod.Obj.FOnFoo(1);',
  26425. '$mod.Obj.GetFoo()(1);',
  26426. '$mod.Obj.GetEvents(6)(1);',
  26427. '$mod.b = $mod.Obj.FOnFoo === null;',
  26428. '$mod.b = $mod.Obj.GetFoo() === null;',
  26429. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  26430. '$mod.b = $mod.Obj.FOnFoo !== null;',
  26431. '$mod.b = $mod.Obj.GetFoo() !== null;',
  26432. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  26433. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  26434. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  26435. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  26436. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26437. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26438. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  26439. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26440. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26441. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  26442. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26443. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26444. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26445. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26446. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26447. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26448. '$mod.b = $mod.Obj.FOnFoo != null;',
  26449. '$mod.b = $mod.Obj.GetFoo() != null;',
  26450. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26451. '']));
  26452. end;
  26453. procedure TTestModule.TestProcType_PropertyDelphi;
  26454. begin
  26455. StartProgram(false);
  26456. Add('{$mode delphi}');
  26457. Add('type');
  26458. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26459. Add(' TObject = class');
  26460. Add(' FOnFoo: TFuncInt;');
  26461. Add(' function DoIt(vA: longint = 1): longint;');
  26462. Add(' function GetFoo: TFuncInt;');
  26463. Add(' procedure SetFoo(const Value: TFuncInt);');
  26464. Add(' function GetEvents(Index: longint): TFuncInt;');
  26465. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26466. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26467. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26468. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26469. Add(' end;');
  26470. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26471. Add('function tobject.getfoo: tfuncint; begin end;');
  26472. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26473. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26474. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26475. Add('var');
  26476. Add(' Obj: TObject;');
  26477. Add(' vP: tfuncint;');
  26478. Add(' b: boolean;');
  26479. Add('begin');
  26480. Add(' obj.onfoo:=nil;');
  26481. Add(' obj.onbar:=nil;');
  26482. Add(' obj.events[1]:=nil;');
  26483. Add(' obj.onfoo:=obj.onfoo;');
  26484. Add(' obj.onbar:=obj.onbar;');
  26485. Add(' obj.events[2]:=obj.events[3];');
  26486. Add(' obj.onfoo:[email protected];');
  26487. Add(' obj.onbar:[email protected];');
  26488. Add(' obj.events[4]:[email protected];');
  26489. Add(' obj.onfoo:=obj.doit;'); // delphi
  26490. Add(' obj.onbar:=obj.doit;'); // delphi
  26491. Add(' obj.events[4]:=obj.doit;'); // delphi
  26492. Add(' obj.onfoo;');
  26493. Add(' obj.onbar;');
  26494. //Add(' obj.events[5];'); ToDo in pasresolver
  26495. Add(' obj.onfoo();');
  26496. Add(' obj.onbar();');
  26497. Add(' obj.events[6]();');
  26498. //Add(' b:=obj.onfoo=nil;'); // fpc
  26499. //Add(' b:=obj.onbar=nil;'); // fpc
  26500. //Add(' b:=obj.events[7]=nil;'); // fpc
  26501. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26502. //Add(' b:=obj.onbar<>nil;'); // fpc
  26503. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26504. Add(' b:=obj.onfoo=vp;');
  26505. Add(' b:=obj.onbar=vp;');
  26506. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26507. Add(' b:=obj.onfoo=obj.onfoo;');
  26508. Add(' b:=obj.onbar=obj.onfoo;');
  26509. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26510. Add(' b:=obj.onfoo<>obj.onfoo;');
  26511. Add(' b:=obj.onbar<>obj.onfoo;');
  26512. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26513. //Add(' b:[email protected];'); // fpc
  26514. //Add(' b:[email protected];'); // fpc
  26515. //Add(' b:=obj.events[12][email protected];'); // fpc
  26516. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26517. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26518. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26519. Add(' b:=Assigned(obj.onfoo);');
  26520. Add(' b:=Assigned(obj.onbar);');
  26521. Add(' b:=Assigned(obj.events[13]);');
  26522. ConvertProgram;
  26523. CheckSource('TestProcType_PropertyDelphi',
  26524. LinesToStr([ // statements
  26525. 'rtl.createClass(this, "TObject", null, function () {',
  26526. ' this.$init = function () {',
  26527. ' this.FOnFoo = null;',
  26528. ' };',
  26529. ' this.$final = function () {',
  26530. ' this.FOnFoo = undefined;',
  26531. ' };',
  26532. ' this.DoIt = function (vA) {',
  26533. ' var Result = 0;',
  26534. ' return Result;',
  26535. ' };',
  26536. 'this.GetFoo = function () {',
  26537. ' var Result = null;',
  26538. ' return Result;',
  26539. '};',
  26540. 'this.SetFoo = function (Value) {',
  26541. '};',
  26542. 'this.GetEvents = function (Index) {',
  26543. ' var Result = null;',
  26544. ' return Result;',
  26545. '};',
  26546. 'this.SetEvents = function (Index, Value) {',
  26547. '};',
  26548. '});',
  26549. 'this.Obj = null;',
  26550. 'this.vP = null;',
  26551. 'this.b = false;'
  26552. ]),
  26553. LinesToStr([
  26554. '$mod.Obj.FOnFoo = null;',
  26555. '$mod.Obj.SetFoo(null);',
  26556. '$mod.Obj.SetEvents(1, null);',
  26557. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26558. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26559. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26560. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26561. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26562. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26563. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26564. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26565. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26566. '$mod.Obj.FOnFoo(1);',
  26567. '$mod.Obj.GetFoo();',
  26568. '$mod.Obj.FOnFoo(1);',
  26569. '$mod.Obj.GetFoo()(1);',
  26570. '$mod.Obj.GetEvents(6)(1);',
  26571. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26572. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26573. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26574. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26575. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26576. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26577. '$mod.b = $mod.Obj.FOnFoo != null;',
  26578. '$mod.b = $mod.Obj.GetFoo() != null;',
  26579. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26580. '']));
  26581. end;
  26582. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26583. begin
  26584. StartProgram(false);
  26585. Add('type');
  26586. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26587. Add(' TObject = class');
  26588. Add(' FOnFoo: TFuncInt;');
  26589. Add(' function DoIt(vA: longint = 1): longint;');
  26590. Add(' function GetFoo: TFuncInt;');
  26591. Add(' procedure SetFoo(const Value: TFuncInt);');
  26592. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26593. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26594. Add(' end;');
  26595. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26596. Add('function tobject.getfoo: tfuncint; begin end;');
  26597. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26598. Add('var');
  26599. Add(' Obj: TObject;');
  26600. Add(' vP: tfuncint;');
  26601. Add(' b: boolean;');
  26602. Add('begin');
  26603. Add('with obj do begin');
  26604. Add(' fonfoo:=nil;');
  26605. Add(' onfoo:=nil;');
  26606. Add(' onbar:=nil;');
  26607. Add(' fonfoo:=fonfoo;');
  26608. Add(' onfoo:=onfoo;');
  26609. Add(' onbar:=onbar;');
  26610. Add(' fonfoo:=@doit;');
  26611. Add(' onfoo:=@doit;');
  26612. Add(' onbar:=@doit;');
  26613. //Add(' fonfoo:=doit;'); // delphi
  26614. //Add(' onfoo:=doit;'); // delphi
  26615. //Add(' onbar:=doit;'); // delphi
  26616. Add(' fonfoo;');
  26617. Add(' onfoo;');
  26618. Add(' onbar;');
  26619. Add(' fonfoo();');
  26620. Add(' onfoo();');
  26621. Add(' onbar();');
  26622. Add(' b:=fonfoo=nil;');
  26623. Add(' b:=onfoo=nil;');
  26624. Add(' b:=onbar=nil;');
  26625. Add(' b:=fonfoo<>nil;');
  26626. Add(' b:=onfoo<>nil;');
  26627. Add(' b:=onbar<>nil;');
  26628. Add(' b:=fonfoo=vp;');
  26629. Add(' b:=onfoo=vp;');
  26630. Add(' b:=onbar=vp;');
  26631. Add(' b:=fonfoo=fonfoo;');
  26632. Add(' b:=onfoo=onfoo;');
  26633. Add(' b:=onbar=onfoo;');
  26634. Add(' b:=fonfoo<>fonfoo;');
  26635. Add(' b:=onfoo<>onfoo;');
  26636. Add(' b:=onbar<>onfoo;');
  26637. Add(' b:=fonfoo=@doit;');
  26638. Add(' b:=onfoo=@doit;');
  26639. Add(' b:=onbar=@doit;');
  26640. Add(' b:=fonfoo<>@doit;');
  26641. Add(' b:=onfoo<>@doit;');
  26642. Add(' b:=onbar<>@doit;');
  26643. Add(' b:=Assigned(fonfoo);');
  26644. Add(' b:=Assigned(onfoo);');
  26645. Add(' b:=Assigned(onbar);');
  26646. Add('end;');
  26647. ConvertProgram;
  26648. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26649. LinesToStr([ // statements
  26650. 'rtl.createClass(this, "TObject", null, function () {',
  26651. ' this.$init = function () {',
  26652. ' this.FOnFoo = null;',
  26653. ' };',
  26654. ' this.$final = function () {',
  26655. ' this.FOnFoo = undefined;',
  26656. ' };',
  26657. ' this.DoIt = function (vA) {',
  26658. ' var Result = 0;',
  26659. ' return Result;',
  26660. ' };',
  26661. ' this.GetFoo = function () {',
  26662. ' var Result = null;',
  26663. ' return Result;',
  26664. ' };',
  26665. ' this.SetFoo = function (Value) {',
  26666. ' };',
  26667. '});',
  26668. 'this.Obj = null;',
  26669. 'this.vP = null;',
  26670. 'this.b = false;'
  26671. ]),
  26672. LinesToStr([
  26673. 'var $with = $mod.Obj;',
  26674. '$with.FOnFoo = null;',
  26675. '$with.FOnFoo = null;',
  26676. '$with.SetFoo(null);',
  26677. '$with.FOnFoo = $with.FOnFoo;',
  26678. '$with.FOnFoo = $with.FOnFoo;',
  26679. '$with.SetFoo($with.GetFoo());',
  26680. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26681. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26682. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26683. '$with.FOnFoo(1);',
  26684. '$with.FOnFoo(1);',
  26685. '$with.GetFoo();',
  26686. '$with.FOnFoo(1);',
  26687. '$with.FOnFoo(1);',
  26688. '$with.GetFoo()(1);',
  26689. '$mod.b = $with.FOnFoo === null;',
  26690. '$mod.b = $with.FOnFoo === null;',
  26691. '$mod.b = $with.GetFoo() === null;',
  26692. '$mod.b = $with.FOnFoo !== null;',
  26693. '$mod.b = $with.FOnFoo !== null;',
  26694. '$mod.b = $with.GetFoo() !== null;',
  26695. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26696. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26697. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26698. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26699. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26700. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26701. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26702. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26703. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26704. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26705. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26706. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26707. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26708. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26709. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26710. '$mod.b = $with.FOnFoo != null;',
  26711. '$mod.b = $with.FOnFoo != null;',
  26712. '$mod.b = $with.GetFoo() != null;',
  26713. '']));
  26714. end;
  26715. procedure TTestModule.TestProcType_Nested;
  26716. begin
  26717. StartProgram(false);
  26718. Add([
  26719. 'type',
  26720. ' TProcInt = procedure(vI: longint = 1);',
  26721. 'procedure DoIt(vJ: longint);',
  26722. 'var aProc: TProcInt;',
  26723. ' b: boolean;',
  26724. ' procedure Sub(vK: longint);',
  26725. ' var aSub: TProcInt;',
  26726. ' procedure SubSub(vK: longint);',
  26727. ' var aSubSub: TProcInt;',
  26728. ' begin;',
  26729. ' aProc:=@DoIt;',
  26730. ' aSub:=@DoIt;',
  26731. ' aSubSub:=@DoIt;',
  26732. ' aProc:=@Sub;',
  26733. ' aSub:=@Sub;',
  26734. ' aSubSub:=@Sub;',
  26735. ' aProc:=@SubSub;',
  26736. ' aSub:=@SubSub;',
  26737. ' aSubSub:=@SubSub;',
  26738. ' end;',
  26739. ' begin;',
  26740. ' end;',
  26741. 'begin;',
  26742. ' aProc:=@Sub;',
  26743. ' b:=aProc=@Sub;',
  26744. ' b:=@Sub=aProc;',
  26745. 'end;',
  26746. 'begin',
  26747. '']);
  26748. ConvertProgram;
  26749. CheckSource('TestProcType_Nested',
  26750. LinesToStr([ // statements
  26751. 'this.DoIt = function (vJ) {',
  26752. ' var aProc = null;',
  26753. ' var b = false;',
  26754. ' function Sub(vK) {',
  26755. ' var aSub = null;',
  26756. ' function SubSub(vK) {',
  26757. ' var aSubSub = null;',
  26758. ' aProc = $mod.DoIt;',
  26759. ' aSub = $mod.DoIt;',
  26760. ' aSubSub = $mod.DoIt;',
  26761. ' aProc = Sub;',
  26762. ' aSub = Sub;',
  26763. ' aSubSub = Sub;',
  26764. ' aProc = SubSub;',
  26765. ' aSub = SubSub;',
  26766. ' aSubSub = SubSub;',
  26767. ' };',
  26768. ' };',
  26769. ' aProc = Sub;',
  26770. ' b = rtl.eqCallback(aProc, Sub);',
  26771. ' b = rtl.eqCallback(Sub, aProc);',
  26772. '};',
  26773. '']),
  26774. LinesToStr([ // $mod.$main
  26775. '']));
  26776. end;
  26777. procedure TTestModule.TestProcType_NestedOfObject;
  26778. begin
  26779. StartProgram(false);
  26780. Add([
  26781. 'type',
  26782. ' TProcInt = procedure(vI: longint = 1) of object;',
  26783. ' TObject = class',
  26784. ' procedure DoIt(vJ: longint);',
  26785. ' end;',
  26786. 'procedure TObject.DoIt(vJ: longint);',
  26787. 'var aProc: TProcInt;',
  26788. ' b: boolean;',
  26789. ' procedure Sub(vK: longint);',
  26790. ' var aSub: TProcInt;',
  26791. ' procedure SubSub(vK: longint);',
  26792. ' var aSubSub: TProcInt;',
  26793. ' begin;',
  26794. ' aProc:=@DoIt;',
  26795. ' aSub:=@DoIt;',
  26796. ' aSubSub:=@DoIt;',
  26797. ' aProc:=@Sub;',
  26798. ' aSub:=@Sub;',
  26799. ' aSubSub:=@Sub;',
  26800. ' aProc:=@SubSub;',
  26801. ' aSub:=@SubSub;',
  26802. ' aSubSub:=@SubSub;',
  26803. ' end;',
  26804. ' begin;',
  26805. ' end;',
  26806. 'begin;',
  26807. ' aProc:=@Sub;',
  26808. ' b:=aProc=@Sub;',
  26809. ' b:=@Sub=aProc;',
  26810. 'end;',
  26811. 'begin',
  26812. '']);
  26813. ConvertProgram;
  26814. CheckSource('TestProcType_Nested',
  26815. LinesToStr([ // statements
  26816. 'rtl.createClass(this, "TObject", null, function () {',
  26817. ' this.$init = function () {',
  26818. ' };',
  26819. ' this.$final = function () {',
  26820. ' };',
  26821. ' this.DoIt = function (vJ) {',
  26822. ' var $Self = this;',
  26823. ' var aProc = null;',
  26824. ' var b = false;',
  26825. ' function Sub(vK) {',
  26826. ' var aSub = null;',
  26827. ' function SubSub(vK) {',
  26828. ' var aSubSub = null;',
  26829. ' aProc = rtl.createCallback($Self, "DoIt");',
  26830. ' aSub = rtl.createCallback($Self, "DoIt");',
  26831. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26832. ' aProc = Sub;',
  26833. ' aSub = Sub;',
  26834. ' aSubSub = Sub;',
  26835. ' aProc = SubSub;',
  26836. ' aSub = SubSub;',
  26837. ' aSubSub = SubSub;',
  26838. ' };',
  26839. ' };',
  26840. ' aProc = Sub;',
  26841. ' b = rtl.eqCallback(aProc, Sub);',
  26842. ' b = rtl.eqCallback(Sub, aProc);',
  26843. ' };',
  26844. '});',
  26845. '']),
  26846. LinesToStr([ // $mod.$main
  26847. '']));
  26848. end;
  26849. procedure TTestModule.TestProcType_ReferenceToProc;
  26850. begin
  26851. StartProgram(false);
  26852. Add([
  26853. 'type',
  26854. ' TProcRef = reference to procedure(i: longint = 0);',
  26855. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26856. 'var',
  26857. ' p: TProcRef;',
  26858. ' f: TFuncRef;',
  26859. 'procedure DoIt(i: longint);',
  26860. 'begin',
  26861. 'end;',
  26862. 'function GetIt(i: longint): longint;',
  26863. 'begin',
  26864. ' p:=@DoIt;',
  26865. ' f:=@GetIt;',
  26866. ' f;',
  26867. ' f();',
  26868. ' f(1);',
  26869. 'end;',
  26870. 'begin',
  26871. ' p:=@DoIt;',
  26872. ' f:=@GetIt;',
  26873. ' f;',
  26874. ' f();',
  26875. ' f(1);',
  26876. ' p:=TProcRef(f);',
  26877. '']);
  26878. ConvertProgram;
  26879. CheckSource('TestProcType_ReferenceToProc',
  26880. LinesToStr([ // statements
  26881. 'this.p = null;',
  26882. 'this.f = null;',
  26883. 'this.DoIt = function (i) {',
  26884. '};',
  26885. 'this.GetIt = function (i) {',
  26886. ' var Result = 0;',
  26887. ' $mod.p = $mod.DoIt;',
  26888. ' $mod.f = $mod.GetIt;',
  26889. ' $mod.f(0);',
  26890. ' $mod.f(0);',
  26891. ' $mod.f(1);',
  26892. ' return Result;',
  26893. '};',
  26894. '']),
  26895. LinesToStr([ // $mod.$main
  26896. '$mod.p = $mod.DoIt;',
  26897. '$mod.f = $mod.GetIt;',
  26898. '$mod.f(0);',
  26899. '$mod.f(0);',
  26900. '$mod.f(1);',
  26901. '$mod.p = $mod.f;',
  26902. '']));
  26903. end;
  26904. procedure TTestModule.TestProcType_ReferenceToMethod;
  26905. begin
  26906. StartProgram(false);
  26907. Add([
  26908. 'type',
  26909. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26910. ' TObject = class',
  26911. ' function Grow(s: longint): longint;',
  26912. ' end;',
  26913. 'var',
  26914. ' f: tfuncref;',
  26915. 'function tobject.grow(s: longint): longint;',
  26916. ' function GrowSub(i: longint): longint;',
  26917. ' begin',
  26918. ' f:=@grow;',
  26919. ' f:=@growsub;',
  26920. ' end;',
  26921. 'begin',
  26922. ' f:=@grow;',
  26923. ' f:=@growsub;',
  26924. 'end;',
  26925. 'begin',
  26926. '']);
  26927. ConvertProgram;
  26928. CheckSource('TestProcType_ReferenceToMethod',
  26929. LinesToStr([ // statements
  26930. 'rtl.createClass(this, "TObject", null, function () {',
  26931. ' this.$init = function () {',
  26932. ' };',
  26933. ' this.$final = function () {',
  26934. ' };',
  26935. ' this.Grow = function (s) {',
  26936. ' var $Self = this;',
  26937. ' var Result = 0;',
  26938. ' function GrowSub(i) {',
  26939. ' var Result = 0;',
  26940. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26941. ' $mod.f = GrowSub;',
  26942. ' return Result;',
  26943. ' };',
  26944. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26945. ' $mod.f = GrowSub;',
  26946. ' return Result;',
  26947. ' };',
  26948. '});',
  26949. 'this.f = null;',
  26950. '']),
  26951. LinesToStr([ // $mod.$main
  26952. '']));
  26953. end;
  26954. procedure TTestModule.TestProcType_Typecast;
  26955. begin
  26956. StartProgram(false);
  26957. Add([
  26958. 'type',
  26959. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26960. ' TEvent = procedure of object;',
  26961. ' TGetter = function:longint of object;',
  26962. ' TProcA = procedure(i: longint);',
  26963. ' TFuncB = function(i, j: longint): longint;',
  26964. 'procedure DoIt(); varargs; begin end;',
  26965. 'var',
  26966. ' Notify: tnotifyevent;',
  26967. ' Event: tevent;',
  26968. ' Getter: tgetter;',
  26969. ' ProcA: tproca;',
  26970. ' FuncB: tfuncb;',
  26971. ' p: pointer;',
  26972. 'begin',
  26973. ' notify:=tnotifyevent(event);',
  26974. ' event:=tevent(event);',
  26975. ' event:=tevent(notify);',
  26976. ' event:=tevent(getter);',
  26977. ' event:=tevent(proca);',
  26978. ' proca:=tproca(funcb);',
  26979. ' funcb:=tfuncb(funcb);',
  26980. ' funcb:=tfuncb(proca);',
  26981. ' funcb:=tfuncb(getter);',
  26982. ' proca:=tproca(p);',
  26983. ' funcb:=tfuncb(p);',
  26984. ' getter:=tgetter(p);',
  26985. ' p:=pointer(notify);',
  26986. ' p:=notify;',
  26987. ' p:=pointer(proca);',
  26988. ' p:=proca;',
  26989. ' p:=pointer(funcb);',
  26990. ' p:=funcb;',
  26991. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26992. '']);
  26993. ConvertProgram;
  26994. CheckSource('TestProcType_Typecast',
  26995. LinesToStr([ // statements
  26996. 'this.DoIt = function () {',
  26997. '};',
  26998. 'this.Notify = null;',
  26999. 'this.Event = null;',
  27000. 'this.Getter = null;',
  27001. 'this.ProcA = null;',
  27002. 'this.FuncB = null;',
  27003. 'this.p = null;',
  27004. '']),
  27005. LinesToStr([ // $mod.$main
  27006. '$mod.Notify = $mod.Event;',
  27007. '$mod.Event = $mod.Event;',
  27008. '$mod.Event = $mod.Notify;',
  27009. '$mod.Event = $mod.Getter;',
  27010. '$mod.Event = $mod.ProcA;',
  27011. '$mod.ProcA = $mod.FuncB;',
  27012. '$mod.FuncB = $mod.FuncB;',
  27013. '$mod.FuncB = $mod.ProcA;',
  27014. '$mod.FuncB = $mod.Getter;',
  27015. '$mod.ProcA = $mod.p;',
  27016. '$mod.FuncB = $mod.p;',
  27017. '$mod.Getter = $mod.p;',
  27018. '$mod.p = $mod.Notify;',
  27019. '$mod.p = $mod.Notify;',
  27020. '$mod.p = $mod.ProcA;',
  27021. '$mod.p = $mod.ProcA;',
  27022. '$mod.p = $mod.FuncB;',
  27023. '$mod.p = $mod.FuncB;',
  27024. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  27025. '']));
  27026. end;
  27027. procedure TTestModule.TestProcType_PassProcToUntyped;
  27028. begin
  27029. StartProgram(false);
  27030. Add([
  27031. 'type',
  27032. ' TEvent = procedure of object;',
  27033. ' TFunc = function: longint;',
  27034. 'procedure DoIt(); varargs; begin end;',
  27035. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  27036. 'var',
  27037. ' Event: tevent;',
  27038. ' Func: TFunc;',
  27039. 'begin',
  27040. ' doit(event,func);',
  27041. ' dosome(event,event,event);',
  27042. ' dosome(func,func,func);',
  27043. '']);
  27044. ConvertProgram;
  27045. CheckSource('TestProcType_PassProcToUntyped',
  27046. LinesToStr([ // statements
  27047. 'this.DoIt = function () {',
  27048. '};',
  27049. 'this.DoSome = function (a, b, p) {',
  27050. '};',
  27051. 'this.Event = null;',
  27052. 'this.Func = null;',
  27053. '']),
  27054. LinesToStr([ // $mod.$main
  27055. '$mod.DoIt($mod.Event, $mod.Func);',
  27056. '$mod.DoSome($mod.Event, {',
  27057. ' p: $mod,',
  27058. ' get: function () {',
  27059. ' return this.p.Event;',
  27060. ' },',
  27061. ' set: function (v) {',
  27062. ' this.p.Event = v;',
  27063. ' }',
  27064. '}, $mod.Event);',
  27065. '$mod.DoSome($mod.Func, {',
  27066. ' p: $mod,',
  27067. ' get: function () {',
  27068. ' return this.p.Func;',
  27069. ' },',
  27070. ' set: function (v) {',
  27071. ' this.p.Func = v;',
  27072. ' }',
  27073. '}, $mod.Func);',
  27074. '']));
  27075. end;
  27076. procedure TTestModule.TestProcType_PassProcToArray;
  27077. begin
  27078. StartProgram(false);
  27079. Add([
  27080. 'type',
  27081. ' TFunc = function: longint;',
  27082. ' TArrFunc = array of TFunc;',
  27083. 'procedure DoIt(Arr: TArrFunc); begin end;',
  27084. 'function GetIt: longint; begin end;',
  27085. 'var',
  27086. ' Func: tfunc;',
  27087. 'begin',
  27088. ' doit([]);',
  27089. ' doit([@GetIt]);',
  27090. ' doit([Func]);',
  27091. '']);
  27092. ConvertProgram;
  27093. CheckSource('TestProcType_PassProcToArray',
  27094. LinesToStr([ // statements
  27095. 'this.DoIt = function (Arr) {',
  27096. '};',
  27097. 'this.GetIt = function () {',
  27098. ' var Result = 0;',
  27099. ' return Result;',
  27100. '};',
  27101. 'this.Func = null;',
  27102. '']),
  27103. LinesToStr([ // $mod.$main
  27104. '$mod.DoIt([]);',
  27105. '$mod.DoIt([$mod.GetIt]);',
  27106. '$mod.DoIt([$mod.Func]);',
  27107. '']));
  27108. end;
  27109. procedure TTestModule.TestProcType_SafeCallObjFPC;
  27110. begin
  27111. StartProgram(false);
  27112. Add([
  27113. '{$modeswitch externalclass}',
  27114. 'type',
  27115. ' TProc = reference to procedure(i: longint); safecall;',
  27116. ' TEvent = procedure(i: longint) of object; safecall;',
  27117. ' TExtA = class external name ''ExtObj''',
  27118. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27119. ' procedure DoSome(Id: longint = 1);',
  27120. ' procedure SetOnClick(const e: TEvent);',
  27121. ' property OnClick: TEvent write SetOnClick;',
  27122. ' class procedure Fly(Id: longint = 1); static;',
  27123. ' procedure SetOnShow(const p: TProc);',
  27124. ' property OnShow: TProc write SetOnShow;',
  27125. ' end;',
  27126. 'procedure Run(i: longint = 1);',
  27127. 'begin',
  27128. 'end;',
  27129. 'var',
  27130. ' Obj: texta;',
  27131. ' e: TEvent;',
  27132. ' p: TProc;',
  27133. 'begin',
  27134. ' e:=e;',
  27135. ' e:[email protected];',
  27136. ' e:[email protected];',
  27137. ' e:=TEvent(@obj.dosome);', // no safecall
  27138. ' obj.OnClick:[email protected];',
  27139. ' obj.OnClick:[email protected];',
  27140. ' obj.setonclick(@obj.doit);',
  27141. ' obj.setonclick(@obj.dosome);',
  27142. ' p:=@Run;',
  27143. ' p:[email protected];',
  27144. ' obj.OnShow:=@Run;',
  27145. ' obj.OnShow:[email protected];',
  27146. ' obj.setOnShow(@Run);',
  27147. ' obj.setOnShow(@TExtA.Fly);',
  27148. ' with obj do begin',
  27149. ' e:=@doit;',
  27150. ' e:=@dosome;',
  27151. ' OnClick:=@doit;',
  27152. ' OnClick:=@dosome;',
  27153. ' setonclick(@doit);',
  27154. ' setonclick(@dosome);',
  27155. ' OnShow:=@Run;',
  27156. ' setOnShow(@Run);',
  27157. ' end;']);
  27158. ConvertProgram;
  27159. CheckSource('TestProcType_SafeCallObjFPC',
  27160. LinesToStr([ // statements
  27161. 'this.Run = function (i) {',
  27162. '};',
  27163. 'this.Obj = null;',
  27164. 'this.e = null;',
  27165. 'this.p = null;',
  27166. '']),
  27167. LinesToStr([ // $mod.$main
  27168. '$mod.e = $mod.e;',
  27169. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27170. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27171. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27172. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27173. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27174. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27175. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27176. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27177. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27178. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27179. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27180. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27181. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27182. 'var $with = $mod.Obj;',
  27183. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27184. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27185. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27186. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27187. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27188. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27189. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27190. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27191. '']));
  27192. end;
  27193. procedure TTestModule.TestProcType_SafeCallDelphi;
  27194. begin
  27195. StartProgram(false);
  27196. Add([
  27197. '{$mode delphi}',
  27198. '{$modeswitch externalclass}',
  27199. 'type',
  27200. ' TProc = reference to procedure(i: longint); safecall;',
  27201. ' TEvent = procedure(i: longint) of object; safecall;',
  27202. ' TExtA = class external name ''ExtObj''',
  27203. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27204. ' procedure DoSome(Id: longint = 1);',
  27205. ' procedure SetOnClick(const e: TEvent);',
  27206. ' property OnClick: TEvent write SetOnClick;',
  27207. ' class procedure Fly(Id: longint = 1); static;',
  27208. ' procedure SetOnShow(const p: TProc);',
  27209. ' property OnShow: TProc write SetOnShow;',
  27210. ' end;',
  27211. 'procedure Run(i: longint = 1);',
  27212. 'begin',
  27213. 'end;',
  27214. 'var',
  27215. ' Obj: texta;',
  27216. ' e: TEvent;',
  27217. ' p: TProc;',
  27218. 'begin',
  27219. ' e:=e;',
  27220. ' e:=obj.doit;',
  27221. ' e:=obj.dosome;',
  27222. ' e:=TEvent(@obj.dosome);', // no safecall
  27223. ' obj.OnClick:=obj.doit;',
  27224. ' obj.OnClick:=obj.dosome;',
  27225. ' obj.setonclick(obj.doit);',
  27226. ' obj.setonclick(obj.dosome);',
  27227. ' p:=Run;',
  27228. ' p:=TExtA.Fly;',
  27229. ' obj.OnShow:=Run;',
  27230. ' obj.OnShow:=TExtA.Fly;',
  27231. ' obj.setOnShow(Run);',
  27232. ' obj.setOnShow(TExtA.Fly);',
  27233. ' with obj do begin',
  27234. ' e:=doit;',
  27235. ' e:=dosome;',
  27236. ' OnClick:=doit;',
  27237. ' OnClick:=dosome;',
  27238. ' setonclick(doit);',
  27239. ' setonclick(dosome);',
  27240. ' OnShow:=@Run;',
  27241. ' setOnShow(@Run);',
  27242. ' end;']);
  27243. ConvertProgram;
  27244. CheckSource('TestProcType_SafeCallDelphi',
  27245. LinesToStr([ // statements
  27246. 'this.Run = function (i) {',
  27247. '};',
  27248. 'this.Obj = null;',
  27249. 'this.e = null;',
  27250. 'this.p = null;',
  27251. '']),
  27252. LinesToStr([ // $mod.$main
  27253. '$mod.e = $mod.e;',
  27254. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27255. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27256. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27257. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27258. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27259. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27260. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27261. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27262. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27263. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27264. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27265. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27266. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27267. 'var $with = $mod.Obj;',
  27268. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27269. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27270. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27271. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27272. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27273. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27274. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27275. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27276. '']));
  27277. end;
  27278. procedure TTestModule.TestPointer;
  27279. begin
  27280. StartProgram(false);
  27281. Add(['type',
  27282. ' TObject = class end;',
  27283. ' TClass = class of TObject;',
  27284. ' TArrInt = array of longint;',
  27285. 'const',
  27286. ' n = nil;',
  27287. 'var',
  27288. ' v: jsvalue;',
  27289. ' Obj: tobject;',
  27290. ' C: tclass;',
  27291. ' a: tarrint;',
  27292. ' p: Pointer = nil;',
  27293. ' s: string;',
  27294. 'begin',
  27295. ' p:=p;',
  27296. ' p:=nil;',
  27297. ' if p=nil then;',
  27298. ' if nil=p then;',
  27299. ' if Assigned(p) then;',
  27300. ' p:=Pointer(v);',
  27301. ' p:=obj;',
  27302. ' p:=c;',
  27303. ' p:=a;',
  27304. ' p:=tobject;',
  27305. ' obj:=TObject(p);',
  27306. ' c:=TClass(p);',
  27307. ' a:=TArrInt(p);',
  27308. ' p:=n;',
  27309. ' p:=Pointer(a);',
  27310. ' p:=pointer(s);',
  27311. ' s:=string(p);',
  27312. '']);
  27313. ConvertProgram;
  27314. CheckSource('TestPointer',
  27315. LinesToStr([ // statements
  27316. 'rtl.createClass(this, "TObject", null, function () {',
  27317. ' this.$init = function () {',
  27318. ' };',
  27319. ' this.$final = function () {',
  27320. ' };',
  27321. '});',
  27322. 'this.n = null;',
  27323. 'this.v = undefined;',
  27324. 'this.Obj = null;',
  27325. 'this.C = null;',
  27326. 'this.a = [];',
  27327. 'this.p = null;',
  27328. 'this.s = "";',
  27329. '']),
  27330. LinesToStr([ // $mod.$main
  27331. '$mod.p = $mod.p;',
  27332. '$mod.p = null;',
  27333. 'if ($mod.p === null) ;',
  27334. 'if (null === $mod.p) ;',
  27335. 'if ($mod.p != null) ;',
  27336. '$mod.p = $mod.v;',
  27337. '$mod.p = $mod.Obj;',
  27338. '$mod.p = $mod.C;',
  27339. '$mod.p = $mod.a;',
  27340. '$mod.p = $mod.TObject;',
  27341. '$mod.Obj = $mod.p;',
  27342. '$mod.C = $mod.p;',
  27343. '$mod.a = $mod.p;',
  27344. '$mod.p = null;',
  27345. '$mod.p = $mod.a;',
  27346. '$mod.p = $mod.s;',
  27347. '$mod.s = $mod.p;',
  27348. '']));
  27349. end;
  27350. procedure TTestModule.TestPointer_Proc;
  27351. begin
  27352. StartProgram(false);
  27353. Add('type');
  27354. Add(' TObject = class');
  27355. Add(' procedure DoIt; virtual; abstract;');
  27356. Add(' end;');
  27357. Add('procedure DoSome; begin end;');
  27358. Add('var');
  27359. Add(' o: TObject;');
  27360. Add(' p: Pointer;');
  27361. Add('begin');
  27362. Add(' p:=@DoSome;');
  27363. Add(' p:[email protected];');
  27364. ConvertProgram;
  27365. CheckSource('TestPointer_Proc',
  27366. LinesToStr([ // statements
  27367. 'rtl.createClass(this, "TObject", null, function () {',
  27368. ' this.$init = function () {',
  27369. ' };',
  27370. ' this.$final = function () {',
  27371. ' };',
  27372. '});',
  27373. 'this.DoSome = function () {',
  27374. '};',
  27375. 'this.o = null;',
  27376. 'this.p = null;',
  27377. '']),
  27378. LinesToStr([ // $mod.$main
  27379. '$mod.p = $mod.DoSome;',
  27380. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  27381. '']));
  27382. end;
  27383. procedure TTestModule.TestPointer_AssignRecordFail;
  27384. begin
  27385. StartProgram(false);
  27386. Add('type');
  27387. Add(' TRec = record end;');
  27388. Add('var');
  27389. Add(' p: Pointer;');
  27390. Add(' r: TRec;');
  27391. Add('begin');
  27392. Add(' p:=r;');
  27393. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  27394. nIncompatibleTypesGotExpected);
  27395. ConvertProgram;
  27396. end;
  27397. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  27398. begin
  27399. StartProgram(false);
  27400. Add('type');
  27401. Add(' TArr = array[boolean] of longint;');
  27402. Add('var');
  27403. Add(' p: Pointer;');
  27404. Add(' a: TArr;');
  27405. Add('begin');
  27406. Add(' p:=a;');
  27407. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  27408. nIncompatibleTypesGotExpected);
  27409. ConvertProgram;
  27410. end;
  27411. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  27412. begin
  27413. StartProgram(false);
  27414. Add([
  27415. 'procedure DoIt(args: array of jsvalue); begin end;',
  27416. 'procedure DoAll; varargs; begin end;',
  27417. 'var',
  27418. ' v: jsvalue;',
  27419. 'begin',
  27420. ' DoIt([pointer(v)]);',
  27421. ' DoAll(pointer(v));',
  27422. '']);
  27423. ConvertProgram;
  27424. CheckSource('TestPointer_TypeCastJSValueToPointer',
  27425. LinesToStr([ // statements
  27426. 'this.DoIt = function (args) {',
  27427. '};',
  27428. 'this.DoAll = function () {',
  27429. '};',
  27430. 'this.v = undefined;',
  27431. '']),
  27432. LinesToStr([ // $mod.$main
  27433. '$mod.DoIt([$mod.v]);',
  27434. '$mod.DoAll($mod.v);',
  27435. '']));
  27436. end;
  27437. procedure TTestModule.TestPointer_NonRecordFail;
  27438. begin
  27439. StartProgram(false);
  27440. Add([
  27441. 'type',
  27442. ' p = ^longint;',
  27443. 'begin',
  27444. '']);
  27445. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27446. ConvertProgram;
  27447. end;
  27448. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27449. begin
  27450. StartProgram(false);
  27451. Add([
  27452. 'procedure DoIt(p: ^longint); begin end;',
  27453. 'begin',
  27454. '']);
  27455. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27456. ConvertProgram;
  27457. end;
  27458. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27459. begin
  27460. StartProgram(false);
  27461. Add([
  27462. 'var p: ^longint;',
  27463. 'begin',
  27464. '']);
  27465. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27466. ConvertProgram;
  27467. end;
  27468. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27469. begin
  27470. StartProgram(false);
  27471. Add([
  27472. 'function DoIt: ^longint; begin end;',
  27473. 'begin',
  27474. '']);
  27475. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27476. ConvertProgram;
  27477. end;
  27478. procedure TTestModule.TestPointer_AddrOperatorFail;
  27479. begin
  27480. StartProgram(false);
  27481. Add([
  27482. 'var i: longint;',
  27483. 'begin',
  27484. ' if @i=nil then ;',
  27485. '']);
  27486. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27487. ConvertProgram;
  27488. end;
  27489. procedure TTestModule.TestPointer_ArrayParamsFail;
  27490. begin
  27491. StartProgram(false);
  27492. Add([
  27493. 'var',
  27494. ' p: Pointer;',
  27495. 'begin',
  27496. ' p:=p[1];',
  27497. '']);
  27498. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27499. ConvertProgram;
  27500. end;
  27501. procedure TTestModule.TestPointer_PointerAddFail;
  27502. begin
  27503. StartProgram(false);
  27504. Add([
  27505. 'var',
  27506. ' p: Pointer;',
  27507. 'begin',
  27508. ' p:=p+1;',
  27509. '']);
  27510. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27511. ConvertProgram;
  27512. end;
  27513. procedure TTestModule.TestPointer_IncPointerFail;
  27514. begin
  27515. StartProgram(false);
  27516. Add([
  27517. 'var',
  27518. ' p: Pointer;',
  27519. 'begin',
  27520. ' inc(p,1);',
  27521. '']);
  27522. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27523. nIncompatibleTypeArgNo);
  27524. ConvertProgram;
  27525. end;
  27526. procedure TTestModule.TestPointer_Record;
  27527. begin
  27528. StartProgram(false);
  27529. Add([
  27530. 'type',
  27531. ' TRec = record x: longint; end;',
  27532. ' PRec = ^TRec;',
  27533. 'var',
  27534. ' r: TRec;',
  27535. ' p: PRec;',
  27536. ' q: ^TRec;',
  27537. ' Ptr: pointer;',
  27538. 'begin',
  27539. ' new(p);',
  27540. ' p:=@r;',
  27541. ' r:=p^;',
  27542. ' r.x:=p^.x;',
  27543. ' p^.x:=r.x;',
  27544. ' if p^.x=3 then ;',
  27545. ' if 4=p^.x then ;',
  27546. ' dispose(p);',
  27547. ' new(q);',
  27548. ' dispose(q);',
  27549. ' Ptr:=p;',
  27550. ' p:=PRec(ptr);',
  27551. '']);
  27552. ConvertProgram;
  27553. CheckSource('TestPointer_Record',
  27554. LinesToStr([ // statements
  27555. 'rtl.recNewT(this, "TRec", function () {',
  27556. ' this.x = 0;',
  27557. ' this.$eq = function (b) {',
  27558. ' return this.x === b.x;',
  27559. ' };',
  27560. ' this.$assign = function (s) {',
  27561. ' this.x = s.x;',
  27562. ' return this;',
  27563. ' };',
  27564. '});',
  27565. 'this.r = this.TRec.$new();',
  27566. 'this.p = null;',
  27567. 'this.q = null;',
  27568. 'this.Ptr = null;',
  27569. '']),
  27570. LinesToStr([ // $mod.$main
  27571. '$mod.p = $mod.TRec.$new();',
  27572. '$mod.p = $mod.r;',
  27573. '$mod.r.$assign($mod.p);',
  27574. '$mod.r.x = $mod.p.x;',
  27575. '$mod.p.x = $mod.r.x;',
  27576. 'if ($mod.p.x === 3) ;',
  27577. 'if (4 === $mod.p.x) ;',
  27578. '$mod.p = null;',
  27579. '$mod.q = $mod.TRec.$new();',
  27580. '$mod.q = null;',
  27581. '$mod.Ptr = $mod.p;',
  27582. '$mod.p = $mod.Ptr;',
  27583. '']));
  27584. end;
  27585. procedure TTestModule.TestPointer_RecordArg;
  27586. begin
  27587. StartProgram(false);
  27588. Add([
  27589. '{$modeswitch autoderef}',
  27590. 'type',
  27591. ' TRec = record x: longint; end;',
  27592. ' PRec = ^TRec;',
  27593. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27594. 'begin',
  27595. ' a.x:=a.x;',
  27596. ' a^.x:=a^.x;',
  27597. ' with a^ do',
  27598. ' x:=x;',
  27599. 'end;',
  27600. 'function GetIt(p: PRec): PRec;',
  27601. 'begin',
  27602. ' p.x:=p.x;',
  27603. ' p^.x:=p^.x;',
  27604. ' with p^ do',
  27605. ' x:=x;',
  27606. 'end;',
  27607. 'var',
  27608. ' r: TRec;',
  27609. ' p: PRec;',
  27610. 'begin',
  27611. ' p:=GetIt(p);',
  27612. ' p^:=GetIt(@r)^;',
  27613. ' DoIt(p,p,p);',
  27614. ' DoIt(@r,p,p);',
  27615. '']);
  27616. ConvertProgram;
  27617. CheckSource('TestPointer_RecordArg',
  27618. LinesToStr([ // statements
  27619. 'rtl.recNewT(this, "TRec", function () {',
  27620. ' this.x = 0;',
  27621. ' this.$eq = function (b) {',
  27622. ' return this.x === b.x;',
  27623. ' };',
  27624. ' this.$assign = function (s) {',
  27625. ' this.x = s.x;',
  27626. ' return this;',
  27627. ' };',
  27628. '});',
  27629. 'this.DoIt = function (a, b, c) {',
  27630. ' var Result = $mod.TRec.$new();',
  27631. ' a.x = a.x;',
  27632. ' a.x = a.x;',
  27633. ' a.x = a.x;',
  27634. ' return Result;',
  27635. '};',
  27636. 'this.GetIt = function (p) {',
  27637. ' var Result = null;',
  27638. ' p.x = p.x;',
  27639. ' p.x = p.x;',
  27640. ' p.x = p.x;',
  27641. ' return Result;',
  27642. '};',
  27643. 'this.r = this.TRec.$new();',
  27644. 'this.p = null;',
  27645. '']),
  27646. LinesToStr([ // $mod.$main
  27647. '$mod.p = $mod.GetIt($mod.p);',
  27648. '$mod.p.$assign($mod.GetIt($mod.r));',
  27649. '$mod.DoIt($mod.p, {',
  27650. ' p: $mod,',
  27651. ' get: function () {',
  27652. ' return this.p.p;',
  27653. ' },',
  27654. ' set: function (v) {',
  27655. ' this.p.p = v;',
  27656. ' }',
  27657. '}, {',
  27658. ' p: $mod,',
  27659. ' get: function () {',
  27660. ' return this.p.p;',
  27661. ' },',
  27662. ' set: function (v) {',
  27663. ' this.p.p = v;',
  27664. ' }',
  27665. '});',
  27666. '$mod.DoIt($mod.r, {',
  27667. ' p: $mod,',
  27668. ' get: function () {',
  27669. ' return this.p.p;',
  27670. ' },',
  27671. ' set: function (v) {',
  27672. ' this.p.p = v;',
  27673. ' }',
  27674. '}, {',
  27675. ' p: $mod,',
  27676. ' get: function () {',
  27677. ' return this.p.p;',
  27678. ' },',
  27679. ' set: function (v) {',
  27680. ' this.p.p = v;',
  27681. ' }',
  27682. '});',
  27683. '']));
  27684. end;
  27685. procedure TTestModule.TestJSValue_AssignToJSValue;
  27686. begin
  27687. StartProgram(false);
  27688. Add('var');
  27689. Add(' v: jsvalue;');
  27690. Add(' i: longint;');
  27691. Add(' s: string;');
  27692. Add(' b: boolean;');
  27693. Add(' d: double;');
  27694. Add(' p: pointer;');
  27695. Add('begin');
  27696. Add(' v:=v;');
  27697. Add(' v:=1;');
  27698. Add(' v:=i;');
  27699. Add(' v:='''';');
  27700. Add(' v:=''c'';');
  27701. Add(' v:=''foo'';');
  27702. Add(' v:=s;');
  27703. Add(' v:=false;');
  27704. Add(' v:=true;');
  27705. Add(' v:=b;');
  27706. Add(' v:=0.1;');
  27707. Add(' v:=d;');
  27708. Add(' v:=nil;');
  27709. Add(' v:=p;');
  27710. ConvertProgram;
  27711. CheckSource('TestJSValue_AssignToJSValue',
  27712. LinesToStr([ // statements
  27713. 'this.v = undefined;',
  27714. 'this.i = 0;',
  27715. 'this.s = "";',
  27716. 'this.b = false;',
  27717. 'this.d = 0.0;',
  27718. 'this.p = null;',
  27719. '']),
  27720. LinesToStr([ // $mod.$main
  27721. '$mod.v = $mod.v;',
  27722. '$mod.v = 1;',
  27723. '$mod.v = $mod.i;',
  27724. '$mod.v = "";',
  27725. '$mod.v = "c";',
  27726. '$mod.v = "foo";',
  27727. '$mod.v = $mod.s;',
  27728. '$mod.v = false;',
  27729. '$mod.v = true;',
  27730. '$mod.v = $mod.b;',
  27731. '$mod.v = 0.1;',
  27732. '$mod.v = $mod.d;',
  27733. '$mod.v = null;',
  27734. '$mod.v = $mod.p;',
  27735. '']));
  27736. end;
  27737. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27738. begin
  27739. StartProgram(false);
  27740. Add('type');
  27741. Add(' integer = longint;');
  27742. Add(' TYesNo = boolean;');
  27743. Add(' TFloat = double;');
  27744. Add(' TCaption = string;');
  27745. Add(' TChar = char;');
  27746. Add('var');
  27747. Add(' v: jsvalue;');
  27748. Add(' i: integer;');
  27749. Add(' s: TCaption;');
  27750. Add(' b: TYesNo;');
  27751. Add(' d: TFloat;');
  27752. Add(' c: char;');
  27753. Add('begin');
  27754. Add(' i:=longint(v);');
  27755. Add(' i:=integer(v);');
  27756. Add(' s:=string(v);');
  27757. Add(' s:=TCaption(v);');
  27758. Add(' b:=boolean(v);');
  27759. Add(' b:=TYesNo(v);');
  27760. Add(' d:=double(v);');
  27761. Add(' d:=TFloat(v);');
  27762. Add(' c:=char(v);');
  27763. Add(' c:=TChar(v);');
  27764. ConvertProgram;
  27765. CheckSource('TestJSValue_TypeCastToBaseType',
  27766. LinesToStr([ // statements
  27767. 'this.v = undefined;',
  27768. 'this.i = 0;',
  27769. 'this.s = "";',
  27770. 'this.b = false;',
  27771. 'this.d = 0.0;',
  27772. 'this.c = "";',
  27773. '']),
  27774. LinesToStr([ // $mod.$main
  27775. '$mod.i = rtl.trunc($mod.v);',
  27776. '$mod.i = rtl.trunc($mod.v);',
  27777. '$mod.s = "" + $mod.v;',
  27778. '$mod.s = "" + $mod.v;',
  27779. '$mod.b = !($mod.v == false);',
  27780. '$mod.b = !($mod.v == false);',
  27781. '$mod.d = rtl.getNumber($mod.v);',
  27782. '$mod.d = rtl.getNumber($mod.v);',
  27783. '$mod.c = rtl.getChar($mod.v);',
  27784. '$mod.c = rtl.getChar($mod.v);',
  27785. '']));
  27786. end;
  27787. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27788. begin
  27789. StartProgram(false);
  27790. Add([
  27791. 'type',
  27792. ' TArr = array of word;',
  27793. ' TRec = record end;',
  27794. ' TSet = set of boolean;',
  27795. 'procedure Fly(v: jsvalue);',
  27796. 'begin',
  27797. 'end;',
  27798. 'var',
  27799. ' a: TArr;',
  27800. ' r: TRec;',
  27801. ' s: TSet;',
  27802. 'begin',
  27803. ' Fly(jsvalue(a));',
  27804. ' Fly(jsvalue(r));',
  27805. ' Fly(jsvalue(s));',
  27806. '']);
  27807. ConvertProgram;
  27808. CheckSource('TestJSValue_TypecastToJSValue',
  27809. LinesToStr([ // statements
  27810. 'rtl.recNewT(this, "TRec", function () {',
  27811. ' this.$eq = function (b) {',
  27812. ' return true;',
  27813. ' };',
  27814. ' this.$assign = function (s) {',
  27815. ' return this;',
  27816. ' };',
  27817. '});',
  27818. 'this.Fly = function (v) {',
  27819. '};',
  27820. 'this.a = [];',
  27821. 'this.r = this.TRec.$new();',
  27822. 'this.s = {};',
  27823. '']),
  27824. LinesToStr([ // $mod.$main
  27825. '$mod.Fly($mod.a);',
  27826. '$mod.Fly($mod.r);',
  27827. '$mod.Fly($mod.s);',
  27828. '']));
  27829. end;
  27830. procedure TTestModule.TestJSValue_Equal;
  27831. begin
  27832. StartProgram(false);
  27833. Add('type');
  27834. Add(' integer = longint;');
  27835. Add(' TYesNo = boolean;');
  27836. Add(' TFloat = double;');
  27837. Add(' TCaption = string;');
  27838. Add(' TChar = char;');
  27839. Add(' TMulti = JSValue;');
  27840. Add('var');
  27841. Add(' v: jsvalue;');
  27842. Add(' i: integer;');
  27843. Add(' s: TCaption;');
  27844. Add(' b: TYesNo;');
  27845. Add(' d: TFloat;');
  27846. Add(' c: char;');
  27847. Add(' m: TMulti;');
  27848. Add('begin');
  27849. Add(' b:=v=v;');
  27850. Add(' b:=v<>v;');
  27851. Add(' b:=v=1;');
  27852. Add(' b:=v<>1;');
  27853. Add(' b:=2=v;');
  27854. Add(' b:=2<>v;');
  27855. Add(' b:=v=i;');
  27856. Add(' b:=i=v;');
  27857. Add(' b:=v=nil;');
  27858. Add(' b:=nil=v;');
  27859. Add(' b:=v=false;');
  27860. Add(' b:=true=v;');
  27861. Add(' b:=v=b;');
  27862. Add(' b:=b=v;');
  27863. Add(' b:=v=s;');
  27864. Add(' b:=s=v;');
  27865. Add(' b:=v=''foo'';');
  27866. Add(' b:=''''=v;');
  27867. Add(' b:=v=d;');
  27868. Add(' b:=d=v;');
  27869. Add(' b:=v=3.4;');
  27870. Add(' b:=5.6=v;');
  27871. Add(' b:=v=c;');
  27872. Add(' b:=c=v;');
  27873. Add(' b:=m=m;');
  27874. Add(' b:=v=m;');
  27875. Add(' b:=m=v;');
  27876. ConvertProgram;
  27877. CheckSource('TestJSValue_Equal',
  27878. LinesToStr([ // statements
  27879. 'this.v = undefined;',
  27880. 'this.i = 0;',
  27881. 'this.s = "";',
  27882. 'this.b = false;',
  27883. 'this.d = 0.0;',
  27884. 'this.c = "";',
  27885. 'this.m = undefined;',
  27886. '']),
  27887. LinesToStr([ // $mod.$main
  27888. '$mod.b = $mod.v == $mod.v;',
  27889. '$mod.b = $mod.v != $mod.v;',
  27890. '$mod.b = $mod.v == 1;',
  27891. '$mod.b = $mod.v != 1;',
  27892. '$mod.b = 2 == $mod.v;',
  27893. '$mod.b = 2 != $mod.v;',
  27894. '$mod.b = $mod.v == $mod.i;',
  27895. '$mod.b = $mod.i == $mod.v;',
  27896. '$mod.b = $mod.v == null;',
  27897. '$mod.b = null == $mod.v;',
  27898. '$mod.b = $mod.v == false;',
  27899. '$mod.b = true == $mod.v;',
  27900. '$mod.b = $mod.v == $mod.b;',
  27901. '$mod.b = $mod.b == $mod.v;',
  27902. '$mod.b = $mod.v == $mod.s;',
  27903. '$mod.b = $mod.s == $mod.v;',
  27904. '$mod.b = $mod.v == "foo";',
  27905. '$mod.b = "" == $mod.v;',
  27906. '$mod.b = $mod.v == $mod.d;',
  27907. '$mod.b = $mod.d == $mod.v;',
  27908. '$mod.b = $mod.v == 3.4;',
  27909. '$mod.b = 5.6 == $mod.v;',
  27910. '$mod.b = $mod.v == $mod.c;',
  27911. '$mod.b = $mod.c == $mod.v;',
  27912. '$mod.b = $mod.m == $mod.m;',
  27913. '$mod.b = $mod.v == $mod.m;',
  27914. '$mod.b = $mod.m == $mod.v;',
  27915. '']));
  27916. end;
  27917. procedure TTestModule.TestJSValue_If;
  27918. begin
  27919. StartProgram(false);
  27920. Add([
  27921. 'procedure Fly(var u);',
  27922. 'begin',
  27923. ' if jsvalue(u) then ;',
  27924. 'end;',
  27925. 'var',
  27926. ' v: jsvalue;',
  27927. 'begin',
  27928. ' if v then ;',
  27929. ' while v do ;',
  27930. ' repeat until v;',
  27931. '']);
  27932. ConvertProgram;
  27933. CheckSource('TestJSValue_If',
  27934. LinesToStr([ // statements
  27935. 'this.Fly = function (u) {',
  27936. ' if (u.get()) ;',
  27937. '};',
  27938. 'this.v = undefined;',
  27939. '']),
  27940. LinesToStr([ // $mod.$main
  27941. 'if ($mod.v) ;',
  27942. 'while($mod.v){',
  27943. '};',
  27944. 'do{',
  27945. '} while(!$mod.v);',
  27946. '']));
  27947. end;
  27948. procedure TTestModule.TestJSValue_Not;
  27949. begin
  27950. StartProgram(false);
  27951. Add([
  27952. 'var',
  27953. ' v: jsvalue;',
  27954. ' b: boolean;',
  27955. 'begin',
  27956. ' b:=not v;',
  27957. ' if not v then ;',
  27958. ' while not v do ;',
  27959. ' repeat until not v;',
  27960. '']);
  27961. ConvertProgram;
  27962. CheckSource('TestJSValue_If',
  27963. LinesToStr([ // statements
  27964. 'this.v = undefined;',
  27965. 'this.b = false;',
  27966. '']),
  27967. LinesToStr([ // $mod.$main
  27968. '$mod.b=!$mod.v;',
  27969. 'if (!$mod.v) ;',
  27970. 'while(!$mod.v){',
  27971. '};',
  27972. 'do{',
  27973. '} while($mod.v);',
  27974. '']));
  27975. end;
  27976. procedure TTestModule.TestJSValue_Enum;
  27977. begin
  27978. StartProgram(false);
  27979. Add('type');
  27980. Add(' TColor = (red, blue);');
  27981. Add(' TRedBlue = TColor;');
  27982. Add('var');
  27983. Add(' v: jsvalue;');
  27984. Add(' e: TColor;');
  27985. Add('begin');
  27986. Add(' v:=e;');
  27987. Add(' v:=TColor(e);');
  27988. Add(' v:=TRedBlue(e);');
  27989. Add(' e:=TColor(v);');
  27990. Add(' e:=TRedBlue(v);');
  27991. ConvertProgram;
  27992. CheckSource('TestJSValue_Enum',
  27993. LinesToStr([ // statements
  27994. 'this.TColor = {',
  27995. ' "0": "red",',
  27996. ' red: 0,',
  27997. ' "1": "blue",',
  27998. ' blue: 1',
  27999. '};',
  28000. 'this.v = undefined;',
  28001. 'this.e = 0;',
  28002. '']),
  28003. LinesToStr([ // $mod.$main
  28004. '$mod.v = $mod.e;',
  28005. '$mod.v = $mod.e;',
  28006. '$mod.v = $mod.e;',
  28007. '$mod.e = $mod.v;',
  28008. '$mod.e = $mod.v;',
  28009. '']));
  28010. end;
  28011. procedure TTestModule.TestJSValue_ClassInstance;
  28012. begin
  28013. StartProgram(false);
  28014. Add([
  28015. 'type',
  28016. ' TObject = class',
  28017. ' end;',
  28018. ' TBirdObject = TObject;',
  28019. 'var',
  28020. ' v: jsvalue;',
  28021. ' o: TObject;',
  28022. 'begin',
  28023. ' v:=o;',
  28024. ' v:=TObject(o);',
  28025. ' v:=TBirdObject(o);',
  28026. ' o:=TObject(v);',
  28027. ' o:=TBirdObject(v);',
  28028. ' if v is TObject then ;',
  28029. '']);
  28030. ConvertProgram;
  28031. CheckSource('TestJSValue_ClassInstance',
  28032. LinesToStr([ // statements
  28033. 'rtl.createClass(this, "TObject", null, function () {',
  28034. ' this.$init = function () {',
  28035. ' };',
  28036. ' this.$final = function () {',
  28037. ' };',
  28038. '});',
  28039. 'this.v = undefined;',
  28040. 'this.o = null;',
  28041. '']),
  28042. LinesToStr([ // $mod.$main
  28043. '$mod.v = $mod.o;',
  28044. '$mod.v = $mod.o;',
  28045. '$mod.v = $mod.o;',
  28046. '$mod.o = rtl.getObject($mod.v);',
  28047. '$mod.o = rtl.getObject($mod.v);',
  28048. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  28049. '']));
  28050. end;
  28051. procedure TTestModule.TestJSValue_ClassOf;
  28052. begin
  28053. StartProgram(false);
  28054. Add([
  28055. 'type',
  28056. ' TClass = class of TObject;',
  28057. ' TObject = class',
  28058. ' end;',
  28059. ' TBirds = class of TBird;',
  28060. ' TBird = class(TObject) end;',
  28061. 'var',
  28062. ' v: jsvalue;',
  28063. ' c: TClass;',
  28064. 'begin',
  28065. ' v:=c;',
  28066. ' v:=TObject;',
  28067. ' v:=TClass(c);',
  28068. ' v:=TBirds(c);',
  28069. ' c:=TClass(v);',
  28070. ' c:=TBirds(v);',
  28071. ' if v is TClass then ;',
  28072. '']);
  28073. ConvertProgram;
  28074. CheckSource('TestJSValue_ClassOf',
  28075. LinesToStr([ // statements
  28076. 'rtl.createClass(this, "TObject", null, function () {',
  28077. ' this.$init = function () {',
  28078. ' };',
  28079. ' this.$final = function () {',
  28080. ' };',
  28081. '});',
  28082. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28083. '});',
  28084. 'this.v = undefined;',
  28085. 'this.c = null;',
  28086. '']),
  28087. LinesToStr([ // $mod.$main
  28088. '$mod.v = $mod.c;',
  28089. '$mod.v = $mod.TObject;',
  28090. '$mod.v = $mod.c;',
  28091. '$mod.v = $mod.c;',
  28092. '$mod.c = rtl.getObject($mod.v);',
  28093. '$mod.c = rtl.getObject($mod.v);',
  28094. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  28095. '']));
  28096. end;
  28097. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  28098. begin
  28099. StartProgram(false);
  28100. Add([
  28101. 'type',
  28102. ' integer = longint;',
  28103. ' TArray = array of JSValue;',
  28104. ' TArrgh = tarray;',
  28105. ' TArrInt = array of integer;',
  28106. 'var',
  28107. ' v: jsvalue;',
  28108. ' TheArray: tarray = (1,''2'');',
  28109. ' Arr: tarrgh;',
  28110. ' i: integer;',
  28111. ' ArrInt: tarrint;',
  28112. 'begin',
  28113. ' arr:=thearray;',
  28114. ' thearray:=arr;',
  28115. ' setlength(arr,2);',
  28116. ' setlength(thearray,3);',
  28117. ' arr[4]:=v;',
  28118. ' arr[5]:=length(thearray);',
  28119. ' arr[6]:=nil;',
  28120. ' arr[7]:=thearray[8];',
  28121. ' arr[low(arr)]:=high(thearray);',
  28122. ' arr:=arrint;',
  28123. ' arrInt:=tarrint(arr);',
  28124. ' if TheArray = nil then ;',
  28125. ' if nil = TheArray then ;',
  28126. ' if TheArray <> nil then ;',
  28127. ' if nil <> TheArray then ;',
  28128. '']);
  28129. ConvertProgram;
  28130. CheckSource('TestJSValue_ArrayOfJSValue',
  28131. LinesToStr([ // statements
  28132. 'this.v = undefined;',
  28133. 'this.TheArray = [1, "2"];',
  28134. 'this.Arr = [];',
  28135. 'this.i = 0;',
  28136. 'this.ArrInt = [];',
  28137. '']),
  28138. LinesToStr([ // $mod.$main
  28139. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  28140. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  28141. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  28142. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  28143. '$mod.Arr[4] = $mod.v;',
  28144. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  28145. '$mod.Arr[6] = null;',
  28146. '$mod.Arr[7] = $mod.TheArray[8];',
  28147. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  28148. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  28149. '$mod.ArrInt = $mod.Arr;',
  28150. 'if (rtl.length($mod.TheArray) === 0) ;',
  28151. 'if (rtl.length($mod.TheArray) === 0) ;',
  28152. 'if (rtl.length($mod.TheArray) > 0) ;',
  28153. 'if (rtl.length($mod.TheArray) > 0) ;',
  28154. '']));
  28155. end;
  28156. procedure TTestModule.TestJSValue_ArrayLit;
  28157. begin
  28158. StartProgram(false);
  28159. Add([
  28160. 'type',
  28161. ' TFlag = (big,small);',
  28162. ' TArray = array of JSValue;',
  28163. ' TObject = class end;',
  28164. ' TClass = class of TObject;',
  28165. 'var',
  28166. ' v: jsvalue;',
  28167. ' a: TArray;',
  28168. ' o: TObject;',
  28169. 'begin',
  28170. ' a:=[];',
  28171. ' a:=[1];',
  28172. ' a:=[1,2];',
  28173. ' a:=[big];',
  28174. ' a:=[1,big];',
  28175. ' a:=[o,nil];',
  28176. '']);
  28177. ConvertProgram;
  28178. CheckSource('TestJSValue_ArrayLit',
  28179. LinesToStr([ // statements
  28180. 'this.TFlag = {',
  28181. ' "0": "big",',
  28182. ' big: 0,',
  28183. ' "1": "small",',
  28184. ' small: 1',
  28185. '};',
  28186. 'rtl.createClass(this, "TObject", null, function () {',
  28187. ' this.$init = function () {',
  28188. ' };',
  28189. ' this.$final = function () {',
  28190. ' };',
  28191. '});',
  28192. 'this.v = undefined;',
  28193. 'this.a = [];',
  28194. 'this.o = null;',
  28195. '']),
  28196. LinesToStr([ // $mod.$main
  28197. '$mod.a = [];',
  28198. '$mod.a = [1];',
  28199. '$mod.a = [1, 2];',
  28200. '$mod.a = [$mod.TFlag.big];',
  28201. '$mod.a = [1, $mod.TFlag.big];',
  28202. '$mod.a = [$mod.o, null];',
  28203. '']));
  28204. end;
  28205. procedure TTestModule.TestJSValue_Params;
  28206. begin
  28207. StartProgram(false);
  28208. Add('type');
  28209. Add(' integer = longint;');
  28210. Add(' TYesNo = boolean;');
  28211. Add(' TFloat = double;');
  28212. Add(' TCaption = string;');
  28213. Add(' TChar = char;');
  28214. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  28215. Add('var');
  28216. Add(' l: jsvalue;');
  28217. Add('begin');
  28218. Add(' a:=a;');
  28219. Add(' l:=b;');
  28220. Add(' c:=c;');
  28221. Add(' d:=d;');
  28222. Add(' Result:=l;');
  28223. Add('end;');
  28224. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  28225. Add('var');
  28226. Add(' v: jsvalue;');
  28227. Add(' i: integer;');
  28228. Add(' b: TYesNo;');
  28229. Add(' d: TFloat;');
  28230. Add(' s: TCaption;');
  28231. Add(' c: TChar;');
  28232. Add('begin');
  28233. Add(' v:=doit(v,v,v,v);');
  28234. Add(' i:=integer(dosome(i,i));');
  28235. Add(' b:=TYesNo(dosome(b,b));');
  28236. Add(' d:=TFloat(dosome(d,d));');
  28237. Add(' s:=TCaption(dosome(s,s));');
  28238. Add(' c:=TChar(dosome(c,c));');
  28239. ConvertProgram;
  28240. CheckSource('TestJSValue_Params',
  28241. LinesToStr([ // statements
  28242. 'this.DoIt = function (a, b, c, d) {',
  28243. ' var Result = undefined;',
  28244. ' var l = undefined;',
  28245. ' a = a;',
  28246. ' l = b;',
  28247. ' c.set(c.get());',
  28248. ' d.set(d.get());',
  28249. ' Result = l;',
  28250. ' return Result;',
  28251. '};',
  28252. 'this.DoSome = function (a, b) {',
  28253. ' var Result = undefined;',
  28254. ' return Result;',
  28255. '};',
  28256. 'this.v = undefined;',
  28257. 'this.i = 0;',
  28258. 'this.b = false;',
  28259. 'this.d = 0.0;',
  28260. 'this.s = "";',
  28261. 'this.c = "";',
  28262. '']),
  28263. LinesToStr([ // $mod.$main
  28264. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  28265. ' p: $mod,',
  28266. ' get: function () {',
  28267. ' return this.p.v;',
  28268. ' },',
  28269. ' set: function (v) {',
  28270. ' this.p.v = v;',
  28271. ' }',
  28272. '}, {',
  28273. ' p: $mod,',
  28274. ' get: function () {',
  28275. ' return this.p.v;',
  28276. ' },',
  28277. ' set: function (v) {',
  28278. ' this.p.v = v;',
  28279. ' }',
  28280. '});',
  28281. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  28282. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  28283. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  28284. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  28285. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  28286. '']));
  28287. end;
  28288. procedure TTestModule.TestJSValue_UntypedParam;
  28289. begin
  28290. StartProgram(false);
  28291. Add('function DoIt(const a; var b; out c): jsvalue;');
  28292. Add('begin');
  28293. Add(' Result:=a;');
  28294. Add(' Result:=b;');
  28295. Add(' Result:=c;');
  28296. Add(' b:=Result;');
  28297. Add(' c:=Result;');
  28298. Add('end;');
  28299. Add('var i: longint;');
  28300. Add('begin');
  28301. Add(' doit(i,i,i);');
  28302. ConvertProgram;
  28303. CheckSource('TestJSValue_UntypedParam',
  28304. LinesToStr([ // statements
  28305. 'this.DoIt = function (a, b, c) {',
  28306. ' var Result = undefined;',
  28307. ' Result = a;',
  28308. ' Result = b.get();',
  28309. ' Result = c.get();',
  28310. ' b.set(Result);',
  28311. ' c.set(Result);',
  28312. ' return Result;',
  28313. '};',
  28314. 'this.i = 0;',
  28315. '']),
  28316. LinesToStr([ // $mod.$main
  28317. '$mod.DoIt($mod.i, {',
  28318. ' p: $mod,',
  28319. ' get: function () {',
  28320. ' return this.p.i;',
  28321. ' },',
  28322. ' set: function (v) {',
  28323. ' this.p.i = v;',
  28324. ' }',
  28325. '}, {',
  28326. ' p: $mod,',
  28327. ' get: function () {',
  28328. ' return this.p.i;',
  28329. ' },',
  28330. ' set: function (v) {',
  28331. ' this.p.i = v;',
  28332. ' }',
  28333. '});',
  28334. '']));
  28335. end;
  28336. procedure TTestModule.TestJSValue_FuncResultType;
  28337. begin
  28338. StartProgram(false);
  28339. Add('type');
  28340. Add(' integer = longint;');
  28341. Add(' TJSValueArray = array of JSValue;');
  28342. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  28343. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  28344. Add('begin');
  28345. Add(' while Compare(P,aList[0])>0 do ;');
  28346. Add('end;');
  28347. Add('var');
  28348. Add(' Compare: TListSortCompare;');
  28349. Add(' V: JSValue;');
  28350. Add(' i: integer;');
  28351. Add('begin');
  28352. Add(' if Compare(V,V)>0 then ;');
  28353. Add(' if Compare(i,i)>1 then ;');
  28354. Add(' if Compare(nil,false)>2 then ;');
  28355. Add(' if Compare(1,true)>3 then ;');
  28356. ConvertProgram;
  28357. CheckSource('TestJSValue_UntypedParam',
  28358. LinesToStr([ // statements
  28359. 'this.Sort = function (P, aList, Compare) {',
  28360. ' while (Compare(P, aList[0]) > 0) {',
  28361. ' };',
  28362. '};',
  28363. 'this.Compare = null;',
  28364. 'this.V = undefined;',
  28365. 'this.i = 0;',
  28366. '']),
  28367. LinesToStr([ // $mod.$main
  28368. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  28369. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  28370. 'if ($mod.Compare(null, false) > 2) ;',
  28371. 'if ($mod.Compare(1, true) > 3) ;',
  28372. '']));
  28373. end;
  28374. procedure TTestModule.TestJSValue_ProcType_Assign;
  28375. begin
  28376. StartProgram(false);
  28377. Add('type');
  28378. Add(' integer = longint;');
  28379. Add(' TObject = class');
  28380. Add(' class function GetGlob: integer;');
  28381. Add(' function Getter: integer;');
  28382. Add(' end;');
  28383. Add('class function TObject.GetGlob: integer;');
  28384. Add('var v1: jsvalue;');
  28385. Add('begin');
  28386. Add(' v1:=@GetGlob;');
  28387. Add(' v1:[email protected];');
  28388. Add('end;');
  28389. Add('function TObject.Getter: integer;');
  28390. Add('var v2: jsvalue;');
  28391. Add('begin');
  28392. Add(' v2:=@Getter;');
  28393. Add(' v2:[email protected];');
  28394. Add(' v2:=@GetGlob;');
  28395. Add(' v2:[email protected];');
  28396. Add('end;');
  28397. Add('function GetIt(i: integer): integer;');
  28398. Add('var v3: jsvalue;');
  28399. Add('begin');
  28400. Add(' v3:=@GetIt;');
  28401. Add('end;');
  28402. Add('var');
  28403. Add(' V: JSValue;');
  28404. Add(' o: TObject;');
  28405. Add('begin');
  28406. Add(' v:=@GetIt;');
  28407. Add(' v:[email protected];');
  28408. Add(' v:[email protected];');
  28409. ConvertProgram;
  28410. CheckSource('TestJSValue_ProcType_Assign',
  28411. LinesToStr([ // statements
  28412. 'rtl.createClass(this, "TObject", null, function () {',
  28413. ' this.$init = function () {',
  28414. ' };',
  28415. ' this.$final = function () {',
  28416. ' };',
  28417. ' this.GetGlob = function () {',
  28418. ' var Result = 0;',
  28419. ' var v1 = undefined;',
  28420. ' v1 = rtl.createCallback(this, "GetGlob");',
  28421. ' v1 = rtl.createCallback(this, "GetGlob");',
  28422. ' return Result;',
  28423. ' };',
  28424. ' this.Getter = function () {',
  28425. ' var Result = 0;',
  28426. ' var v2 = undefined;',
  28427. ' v2 = rtl.createCallback(this, "Getter");',
  28428. ' v2 = rtl.createCallback(this, "Getter");',
  28429. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28430. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28431. ' return Result;',
  28432. ' };',
  28433. '});',
  28434. 'this.GetIt = function (i) {',
  28435. ' var Result = 0;',
  28436. ' var v3 = undefined;',
  28437. ' v3 = $mod.GetIt;',
  28438. ' return Result;',
  28439. '};',
  28440. 'this.V = undefined;',
  28441. 'this.o = null;',
  28442. '']),
  28443. LinesToStr([ // $mod.$main
  28444. '$mod.V = $mod.GetIt;',
  28445. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28446. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28447. '']));
  28448. end;
  28449. procedure TTestModule.TestJSValue_ProcType_Equal;
  28450. begin
  28451. StartProgram(false);
  28452. Add('type');
  28453. Add(' integer = longint;');
  28454. Add(' TObject = class');
  28455. Add(' class function GetGlob: integer;');
  28456. Add(' function Getter: integer;');
  28457. Add(' end;');
  28458. Add('class function TObject.GetGlob: integer;');
  28459. Add('var v1: jsvalue;');
  28460. Add('begin');
  28461. Add(' if v1=@GetGlob then;');
  28462. Add(' if [email protected] then ;');
  28463. Add('end;');
  28464. Add('function TObject.Getter: integer;');
  28465. Add('var v2: jsvalue;');
  28466. Add('begin');
  28467. Add(' if v2=@Getter then;');
  28468. Add(' if [email protected] then ;');
  28469. Add(' if v2=@GetGlob then;');
  28470. Add(' if [email protected] then;');
  28471. Add('end;');
  28472. Add('function GetIt(i: integer): integer;');
  28473. Add('var v3: jsvalue;');
  28474. Add('begin');
  28475. Add(' if v3=@GetIt then;');
  28476. Add('end;');
  28477. Add('var');
  28478. Add(' V: JSValue;');
  28479. Add(' o: TObject;');
  28480. Add('begin');
  28481. Add(' if v=@GetIt then;');
  28482. Add(' if [email protected] then;');
  28483. Add(' if [email protected] then;');
  28484. Add(' if @GetIt=v then;');
  28485. Add(' if @o.Getter=v then;');
  28486. Add(' if @o.GetGlob=v then;');
  28487. ConvertProgram;
  28488. CheckSource('TestJSValue_ProcType_Equal',
  28489. LinesToStr([ // statements
  28490. 'rtl.createClass(this, "TObject", null, function () {',
  28491. ' this.$init = function () {',
  28492. ' };',
  28493. ' this.$final = function () {',
  28494. ' };',
  28495. ' this.GetGlob = function () {',
  28496. ' var Result = 0;',
  28497. ' var v1 = undefined;',
  28498. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28499. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28500. ' return Result;',
  28501. ' };',
  28502. ' this.Getter = function () {',
  28503. ' var Result = 0;',
  28504. ' var v2 = undefined;',
  28505. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28506. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28507. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28508. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28509. ' return Result;',
  28510. ' };',
  28511. '});',
  28512. 'this.GetIt = function (i) {',
  28513. ' var Result = 0;',
  28514. ' var v3 = undefined;',
  28515. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28516. ' return Result;',
  28517. '};',
  28518. 'this.V = undefined;',
  28519. 'this.o = null;',
  28520. '']),
  28521. LinesToStr([ // $mod.$main
  28522. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28523. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28524. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28525. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28526. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28527. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28528. '']));
  28529. end;
  28530. procedure TTestModule.TestJSValue_ProcType_Param;
  28531. begin
  28532. StartProgram(false);
  28533. Add([
  28534. 'type',
  28535. ' variant = jsvalue;',
  28536. ' TArrVariant = array of variant;',
  28537. ' TArrVar2 = TArrVariant;',
  28538. ' TFuncInt = function: longint;',
  28539. 'function GetIt: longint;',
  28540. 'begin',
  28541. 'end;',
  28542. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28543. 'var v: variant;',
  28544. 'begin',
  28545. ' v:=arr[1];',
  28546. 'end;',
  28547. 'var s: string;',
  28548. 'begin',
  28549. ' DoIt(GetIt,[]);',
  28550. ' DoIt(@GetIt,[]);',
  28551. ' DoIt(1,[s,GetIt]);',
  28552. ' DoIt(1,[s,@GetIt]);',
  28553. '']);
  28554. ConvertProgram;
  28555. CheckSource('TestJSValue_ProcType_Param',
  28556. LinesToStr([ // statements
  28557. 'this.GetIt = function () {',
  28558. ' var Result = 0;',
  28559. ' return Result;',
  28560. '};',
  28561. 'this.DoIt = function (p, Arr) {',
  28562. ' var v = undefined;',
  28563. ' v = Arr[1];',
  28564. '};',
  28565. 'this.s = "";',
  28566. '']),
  28567. LinesToStr([ // $mod.$main
  28568. '$mod.DoIt($mod.GetIt(), []);',
  28569. '$mod.DoIt($mod.GetIt, []);',
  28570. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28571. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28572. '']));
  28573. end;
  28574. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28575. begin
  28576. StartProgram(false);
  28577. Add([
  28578. 'var',
  28579. ' v: JSValue;',
  28580. ' p: Pointer;',
  28581. 'begin',
  28582. ' p:=v;',
  28583. '']);
  28584. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28585. nIncompatibleTypesGotExpected);
  28586. ConvertProgram;
  28587. end;
  28588. procedure TTestModule.TestJSValue_OverloadDouble;
  28589. begin
  28590. StartProgram(false);
  28591. Add([
  28592. 'type',
  28593. ' integer = longint;',
  28594. ' tdatetime = double;',
  28595. 'procedure DoIt(d: double); begin end;',
  28596. 'procedure DoIt(v: jsvalue); begin end;',
  28597. 'var',
  28598. ' d: double;',
  28599. ' dt: tdatetime;',
  28600. ' i: integer;',
  28601. ' b: byte;',
  28602. ' shi: shortint;',
  28603. ' w: word;',
  28604. ' smi: smallint;',
  28605. ' lw: longword;',
  28606. ' li: longint;',
  28607. ' ni: nativeint;',
  28608. ' nu: nativeuint;',
  28609. 'begin',
  28610. ' DoIt(d);',
  28611. ' DoIt(dt);',
  28612. ' DoIt(i);',
  28613. ' DoIt(b);',
  28614. ' DoIt(shi);',
  28615. ' DoIt(w);',
  28616. ' DoIt(smi);',
  28617. ' DoIt(lw);',
  28618. ' DoIt(li);',
  28619. ' DoIt(ni);',
  28620. ' DoIt(nu);',
  28621. '']);
  28622. ConvertProgram;
  28623. CheckSource('TestJSValue_OverloadDouble',
  28624. LinesToStr([ // statements
  28625. 'this.DoIt = function (d) {',
  28626. '};',
  28627. 'this.DoIt$1 = function (v) {',
  28628. '};',
  28629. 'this.d = 0.0;',
  28630. 'this.dt = 0.0;',
  28631. 'this.i = 0;',
  28632. 'this.b = 0;',
  28633. 'this.shi = 0;',
  28634. 'this.w = 0;',
  28635. 'this.smi = 0;',
  28636. 'this.lw = 0;',
  28637. 'this.li = 0;',
  28638. 'this.ni = 0;',
  28639. 'this.nu = 0;',
  28640. '']),
  28641. LinesToStr([ // $mod.$main
  28642. '$mod.DoIt($mod.d);',
  28643. '$mod.DoIt($mod.dt);',
  28644. '$mod.DoIt$1($mod.i);',
  28645. '$mod.DoIt$1($mod.b);',
  28646. '$mod.DoIt$1($mod.shi);',
  28647. '$mod.DoIt$1($mod.w);',
  28648. '$mod.DoIt$1($mod.smi);',
  28649. '$mod.DoIt$1($mod.lw);',
  28650. '$mod.DoIt$1($mod.li);',
  28651. '$mod.DoIt$1($mod.ni);',
  28652. '$mod.DoIt$1($mod.nu);',
  28653. '']));
  28654. end;
  28655. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28656. begin
  28657. StartProgram(false);
  28658. Add([
  28659. 'type',
  28660. ' integer = longint;',
  28661. ' int53 = nativeint;',
  28662. ' tdatetime = double;',
  28663. 'procedure DoIt(n: nativeint); begin end;',
  28664. 'procedure DoIt(v: jsvalue); begin end;',
  28665. 'var',
  28666. ' d: double;',
  28667. ' dt: tdatetime;',
  28668. ' i: integer;',
  28669. ' b: byte;',
  28670. ' shi: shortint;',
  28671. ' w: word;',
  28672. ' smi: smallint;',
  28673. ' lw: longword;',
  28674. ' li: longint;',
  28675. ' ni: nativeint;',
  28676. ' nu: nativeuint;',
  28677. 'begin',
  28678. ' DoIt(d);',
  28679. ' DoIt(dt);',
  28680. ' DoIt(i);',
  28681. ' DoIt(b);',
  28682. ' DoIt(shi);',
  28683. ' DoIt(w);',
  28684. ' DoIt(smi);',
  28685. ' DoIt(lw);',
  28686. ' DoIt(li);',
  28687. ' DoIt(ni);',
  28688. ' DoIt(nu);',
  28689. '']);
  28690. ConvertProgram;
  28691. CheckSource('TestJSValue_OverloadNativeInt',
  28692. LinesToStr([ // statements
  28693. 'this.DoIt = function (n) {',
  28694. '};',
  28695. 'this.DoIt$1 = function (v) {',
  28696. '};',
  28697. 'this.d = 0.0;',
  28698. 'this.dt = 0.0;',
  28699. 'this.i = 0;',
  28700. 'this.b = 0;',
  28701. 'this.shi = 0;',
  28702. 'this.w = 0;',
  28703. 'this.smi = 0;',
  28704. 'this.lw = 0;',
  28705. 'this.li = 0;',
  28706. 'this.ni = 0;',
  28707. 'this.nu = 0;',
  28708. '']),
  28709. LinesToStr([ // $mod.$main
  28710. '$mod.DoIt$1($mod.d);',
  28711. '$mod.DoIt$1($mod.dt);',
  28712. '$mod.DoIt($mod.i);',
  28713. '$mod.DoIt($mod.b);',
  28714. '$mod.DoIt($mod.shi);',
  28715. '$mod.DoIt($mod.w);',
  28716. '$mod.DoIt($mod.smi);',
  28717. '$mod.DoIt($mod.lw);',
  28718. '$mod.DoIt($mod.li);',
  28719. '$mod.DoIt($mod.ni);',
  28720. '$mod.DoIt($mod.nu);',
  28721. '']));
  28722. end;
  28723. procedure TTestModule.TestJSValue_OverloadWord;
  28724. begin
  28725. StartProgram(false);
  28726. Add([
  28727. 'type',
  28728. ' integer = longint;',
  28729. ' int53 = nativeint;',
  28730. ' tdatetime = double;',
  28731. 'procedure DoIt(w: word); begin end;',
  28732. 'procedure DoIt(v: jsvalue); begin end;',
  28733. 'var',
  28734. ' d: double;',
  28735. ' dt: tdatetime;',
  28736. ' i: integer;',
  28737. ' b: byte;',
  28738. ' shi: shortint;',
  28739. ' w: word;',
  28740. ' smi: smallint;',
  28741. ' lw: longword;',
  28742. ' li: longint;',
  28743. ' ni: nativeint;',
  28744. ' nu: nativeuint;',
  28745. 'begin',
  28746. ' DoIt(d);',
  28747. ' DoIt(dt);',
  28748. ' DoIt(i);',
  28749. ' DoIt(b);',
  28750. ' DoIt(shi);',
  28751. ' DoIt(w);',
  28752. ' DoIt(smi);',
  28753. ' DoIt(lw);',
  28754. ' DoIt(li);',
  28755. ' DoIt(ni);',
  28756. ' DoIt(nu);',
  28757. '']);
  28758. ConvertProgram;
  28759. CheckSource('TestJSValue_OverloadWord',
  28760. LinesToStr([ // statements
  28761. 'this.DoIt = function (w) {',
  28762. '};',
  28763. 'this.DoIt$1 = function (v) {',
  28764. '};',
  28765. 'this.d = 0.0;',
  28766. 'this.dt = 0.0;',
  28767. 'this.i = 0;',
  28768. 'this.b = 0;',
  28769. 'this.shi = 0;',
  28770. 'this.w = 0;',
  28771. 'this.smi = 0;',
  28772. 'this.lw = 0;',
  28773. 'this.li = 0;',
  28774. 'this.ni = 0;',
  28775. 'this.nu = 0;',
  28776. '']),
  28777. LinesToStr([ // $mod.$main
  28778. '$mod.DoIt$1($mod.d);',
  28779. '$mod.DoIt$1($mod.dt);',
  28780. '$mod.DoIt$1($mod.i);',
  28781. '$mod.DoIt($mod.b);',
  28782. '$mod.DoIt($mod.shi);',
  28783. '$mod.DoIt($mod.w);',
  28784. '$mod.DoIt$1($mod.smi);',
  28785. '$mod.DoIt$1($mod.lw);',
  28786. '$mod.DoIt$1($mod.li);',
  28787. '$mod.DoIt$1($mod.ni);',
  28788. '$mod.DoIt$1($mod.nu);',
  28789. '']));
  28790. end;
  28791. procedure TTestModule.TestJSValue_OverloadString;
  28792. begin
  28793. StartProgram(false);
  28794. Add([
  28795. 'type',
  28796. ' uni = string;',
  28797. ' WChar = char;',
  28798. 'procedure DoIt(s: string); begin end;',
  28799. 'procedure DoIt(v: jsvalue); begin end;',
  28800. 'var',
  28801. ' s: string;',
  28802. ' c: char;',
  28803. ' u: uni;',
  28804. 'begin',
  28805. ' DoIt(s);',
  28806. ' DoIt(c);',
  28807. ' DoIt(u);',
  28808. '']);
  28809. ConvertProgram;
  28810. CheckSource('TestJSValue_OverloadString',
  28811. LinesToStr([ // statements
  28812. 'this.DoIt = function (s) {',
  28813. '};',
  28814. 'this.DoIt$1 = function (v) {',
  28815. '};',
  28816. 'this.s = "";',
  28817. 'this.c = "";',
  28818. 'this.u = "";',
  28819. '']),
  28820. LinesToStr([ // $mod.$main
  28821. '$mod.DoIt($mod.s);',
  28822. '$mod.DoIt($mod.c);',
  28823. '$mod.DoIt($mod.u);',
  28824. '']));
  28825. end;
  28826. procedure TTestModule.TestJSValue_OverloadChar;
  28827. begin
  28828. StartProgram(false);
  28829. Add([
  28830. 'type',
  28831. ' uni = string;',
  28832. ' WChar = char;',
  28833. 'procedure DoIt(c: char); begin end;',
  28834. 'procedure DoIt(v: jsvalue); begin end;',
  28835. 'var',
  28836. ' s: string;',
  28837. ' c: char;',
  28838. ' u: uni;',
  28839. 'begin',
  28840. ' DoIt(s);',
  28841. ' DoIt(c);',
  28842. ' DoIt(u);',
  28843. '']);
  28844. ConvertProgram;
  28845. CheckSource('TestJSValue_OverloadChar',
  28846. LinesToStr([ // statements
  28847. 'this.DoIt = function (c) {',
  28848. '};',
  28849. 'this.DoIt$1 = function (v) {',
  28850. '};',
  28851. 'this.s = "";',
  28852. 'this.c = "";',
  28853. 'this.u = "";',
  28854. '']),
  28855. LinesToStr([ // $mod.$main
  28856. '$mod.DoIt$1($mod.s);',
  28857. '$mod.DoIt($mod.c);',
  28858. '$mod.DoIt$1($mod.u);',
  28859. '']));
  28860. end;
  28861. procedure TTestModule.TestJSValue_OverloadPointer;
  28862. begin
  28863. StartProgram(false);
  28864. Add([
  28865. 'type',
  28866. ' TObject = class end;',
  28867. 'procedure DoIt(p: pointer); begin end;',
  28868. 'procedure DoIt(v: jsvalue); begin end;',
  28869. 'var',
  28870. ' o: TObject;',
  28871. 'begin',
  28872. ' DoIt(o);',
  28873. '']);
  28874. ConvertProgram;
  28875. CheckSource('TestJSValue_OverloadPointer',
  28876. LinesToStr([ // statements
  28877. 'rtl.createClass(this, "TObject", null, function () {',
  28878. ' this.$init = function () {',
  28879. ' };',
  28880. ' this.$final = function () {',
  28881. ' };',
  28882. '});',
  28883. 'this.DoIt = function (p) {',
  28884. '};',
  28885. 'this.DoIt$1 = function (v) {',
  28886. '};',
  28887. 'this.o = null;',
  28888. '']),
  28889. LinesToStr([ // $mod.$main
  28890. '$mod.DoIt($mod.o);',
  28891. '']));
  28892. end;
  28893. procedure TTestModule.TestJSValue_ForIn;
  28894. begin
  28895. StartProgram(false);
  28896. Add([
  28897. 'var',
  28898. ' v: JSValue;',
  28899. ' key: string;',
  28900. 'begin',
  28901. ' for key in v do begin',
  28902. ' if key=''abc'' then ;',
  28903. ' end;',
  28904. '']);
  28905. ConvertProgram;
  28906. CheckSource('TestJSValue_ForIn',
  28907. LinesToStr([ // statements
  28908. 'this.v = undefined;',
  28909. 'this.key = "";',
  28910. '']),
  28911. LinesToStr([ // $mod.$main
  28912. 'for ($mod.key in $mod.v) {',
  28913. ' if ($mod.key === "abc") ;',
  28914. '};',
  28915. '']));
  28916. end;
  28917. procedure TTestModule.TestRTTI_IntRange;
  28918. begin
  28919. WithTypeInfo:=true;
  28920. StartProgram(true,[supTypeInfo]);
  28921. Add([
  28922. '{$modeswitch externalclass}',
  28923. 'type',
  28924. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28925. ' TColor = type TGraphicsColor;',
  28926. 'var',
  28927. ' p: TTypeInfo;',
  28928. ' k: TTypeKind;',
  28929. 'begin',
  28930. ' p:=typeinfo(TGraphicsColor);',
  28931. ' p:=typeinfo(TColor);',
  28932. ' k:=GetTypeKind(TGraphicsColor);',
  28933. ' k:=GetTypeKind(TColor);',
  28934. '']);
  28935. ConvertProgram;
  28936. CheckSource('TestRTTI_IntRange',
  28937. LinesToStr([ // statements
  28938. 'this.$rtti.$Int("TGraphicsColor", {',
  28939. ' minvalue: -2147483648,',
  28940. ' maxvalue: 2147483647,',
  28941. ' ordtype: 4',
  28942. '});',
  28943. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28944. 'this.p = null;',
  28945. 'this.k = 0;',
  28946. '']),
  28947. LinesToStr([ // $mod.$main
  28948. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28949. '$mod.p = $mod.$rtti["TColor"];',
  28950. '$mod.k = 1;',
  28951. '$mod.k = 1;',
  28952. '']));
  28953. end;
  28954. procedure TTestModule.TestRTTI_Double;
  28955. begin
  28956. WithTypeInfo:=true;
  28957. StartProgram(true,[supTypeInfo]);
  28958. Add([
  28959. '{$modeswitch externalclass}',
  28960. 'type',
  28961. ' TFloat = type double;',
  28962. 'var',
  28963. ' p: TTypeInfo;',
  28964. 'begin',
  28965. ' p:=typeinfo(double);',
  28966. ' p:=typeinfo(TFloat);',
  28967. '']);
  28968. ConvertProgram;
  28969. CheckSource('TestRTTI_Double',
  28970. LinesToStr([ // statements
  28971. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28972. 'this.p = null;',
  28973. '']),
  28974. LinesToStr([ // $mod.$main
  28975. '$mod.p = rtl.double;',
  28976. '$mod.p = $mod.$rtti["TFloat"];',
  28977. '']));
  28978. end;
  28979. procedure TTestModule.TestRTTI_ProcType;
  28980. begin
  28981. WithTypeInfo:=true;
  28982. StartProgram(false);
  28983. Add('type');
  28984. Add(' TProcA = procedure;');
  28985. Add(' TMethodB = procedure of object;');
  28986. Add(' TProcC = procedure; varargs;');
  28987. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28988. Add(' TProcE = function: nativeint;');
  28989. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28990. Add('var p: pointer;');
  28991. Add('begin');
  28992. Add(' p:=typeinfo(tproca);');
  28993. ConvertProgram;
  28994. CheckSource('TestRTTI_ProcType',
  28995. LinesToStr([ // statements
  28996. 'this.$rtti.$ProcVar("TProcA", {',
  28997. ' procsig: rtl.newTIProcSig([])',
  28998. '});',
  28999. 'this.$rtti.$MethodVar("TMethodB", {',
  29000. ' procsig: rtl.newTIProcSig([]),',
  29001. ' methodkind: 0',
  29002. '});',
  29003. 'this.$rtti.$ProcVar("TProcC", {',
  29004. ' procsig: rtl.newTIProcSig([], null, 2)',
  29005. '});',
  29006. 'this.$rtti.$ProcVar("TProcD", {',
  29007. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  29008. '});',
  29009. 'this.$rtti.$ProcVar("TProcE", {',
  29010. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  29011. '});',
  29012. 'this.$rtti.$ProcVar("TProcF", {',
  29013. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  29014. '});',
  29015. 'this.p = null;',
  29016. '']),
  29017. LinesToStr([ // $mod.$main
  29018. '$mod.p = $mod.$rtti["TProcA"];',
  29019. '']));
  29020. end;
  29021. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  29022. begin
  29023. WithTypeInfo:=true;
  29024. AddModuleWithIntfImplSrc('unit2.pas',
  29025. LinesToStr([
  29026. 'type',
  29027. ' TObject = class end;'
  29028. ]),
  29029. '');
  29030. StartUnit(true);
  29031. Add('interface');
  29032. Add('uses unit2;');
  29033. Add('type');
  29034. Add(' TProcA = function(o: tobject): tobject;');
  29035. Add('implementation');
  29036. Add('type');
  29037. Add(' TProcB = function(o: tobject): tobject;');
  29038. Add('var p: Pointer;');
  29039. Add('initialization');
  29040. Add(' p:=typeinfo(tproca);');
  29041. Add(' p:=typeinfo(tprocb);');
  29042. ConvertUnit;
  29043. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  29044. LinesToStr([ // statements
  29045. 'var $impl = $mod.$impl;',
  29046. 'this.$rtti.$ProcVar("TProcA", {',
  29047. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29048. '});',
  29049. '']),
  29050. LinesToStr([ // this.$init
  29051. '$impl.p = $mod.$rtti["TProcA"];',
  29052. '$impl.p = $mod.$rtti["TProcB"];',
  29053. '']),
  29054. LinesToStr([ // implementation
  29055. '$mod.$rtti.$ProcVar("TProcB", {',
  29056. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29057. '});',
  29058. '$impl.p = null;',
  29059. '']) );
  29060. end;
  29061. procedure TTestModule.TestRTTI_EnumAndSetType;
  29062. begin
  29063. WithTypeInfo:=true;
  29064. StartProgram(false);
  29065. Add('type');
  29066. Add(' TFlag = (light,dark);');
  29067. Add(' TFlags = set of TFlag;');
  29068. Add(' TProc = function(f: TFlags): TFlag;');
  29069. Add('var p: pointer;');
  29070. Add('begin');
  29071. Add(' p:=typeinfo(tflag);');
  29072. Add(' p:=typeinfo(tflags);');
  29073. ConvertProgram;
  29074. CheckSource('TestRTTI_EnumAndType',
  29075. LinesToStr([ // statements
  29076. 'this.TFlag = {',
  29077. ' "0": "light",',
  29078. ' light: 0,',
  29079. ' "1": "dark",',
  29080. ' dark: 1',
  29081. '};',
  29082. 'this.$rtti.$Enum("TFlag", {',
  29083. ' minvalue: 0,',
  29084. ' maxvalue: 1,',
  29085. ' ordtype: 1,',
  29086. ' enumtype: this.TFlag',
  29087. '});',
  29088. 'this.$rtti.$Set("TFlags", {',
  29089. ' comptype: this.$rtti["TFlag"]',
  29090. '});',
  29091. 'this.$rtti.$ProcVar("TProc", {',
  29092. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  29093. '});',
  29094. 'this.p = null;',
  29095. '']),
  29096. LinesToStr([ // $mod.$main
  29097. '$mod.p = $mod.$rtti["TFlag"];',
  29098. '$mod.p = $mod.$rtti["TFlags"];',
  29099. '']));
  29100. end;
  29101. procedure TTestModule.TestRTTI_EnumRange;
  29102. begin
  29103. WithTypeInfo:=true;
  29104. StartProgram(false);
  29105. Add([
  29106. 'type',
  29107. ' TCol = (red,green,blue);',
  29108. ' TColRg = green..blue;',
  29109. ' TSetOfColRg = set of TColRg;',
  29110. 'var p: pointer;',
  29111. 'begin',
  29112. ' p:=typeinfo(tcolrg);',
  29113. ' p:=typeinfo(tsetofcolrg);',
  29114. '']);
  29115. ConvertProgram;
  29116. end;
  29117. procedure TTestModule.TestRTTI_AnonymousEnumType;
  29118. begin
  29119. WithTypeInfo:=true;
  29120. StartProgram(false);
  29121. Add('type');
  29122. Add(' TFlags = set of (red, green);');
  29123. Add('var');
  29124. Add(' f: TFlags;');
  29125. Add('begin');
  29126. Add(' Include(f,red);');
  29127. ConvertProgram;
  29128. CheckSource('TestRTTI_AnonymousEnumType',
  29129. LinesToStr([ // statements
  29130. 'this.TFlags$a = {',
  29131. ' "0": "red",',
  29132. ' red: 0,',
  29133. ' "1": "green",',
  29134. ' green: 1',
  29135. '};',
  29136. 'this.$rtti.$Enum("TFlags$a", {',
  29137. ' minvalue: 0,',
  29138. ' maxvalue: 1,',
  29139. ' ordtype: 1,',
  29140. ' enumtype: this.TFlags$a',
  29141. '});',
  29142. 'this.$rtti.$Set("TFlags", {',
  29143. ' comptype: this.$rtti["TFlags$a"]',
  29144. '});',
  29145. 'this.f = {};',
  29146. '']),
  29147. LinesToStr([
  29148. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  29149. '']));
  29150. end;
  29151. procedure TTestModule.TestRTTI_StaticArray;
  29152. begin
  29153. WithTypeInfo:=true;
  29154. StartProgram(false);
  29155. Add('type');
  29156. Add(' TFlag = (light,dark);');
  29157. Add(' TFlagNames = array[TFlag] of string;');
  29158. Add(' TBoolNames = array[boolean] of string;');
  29159. Add(' TByteArray = array[1..32768] of byte;');
  29160. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  29161. Add('var p: pointer;');
  29162. Add('begin');
  29163. Add(' p:=typeinfo(TFlagNames);');
  29164. Add(' p:=typeinfo(TBoolNames);');
  29165. ConvertProgram;
  29166. CheckSource('TestRTTI_StaticArray',
  29167. LinesToStr([ // statements
  29168. 'this.TFlag = {',
  29169. ' "0": "light",',
  29170. ' light: 0,',
  29171. ' "1": "dark",',
  29172. ' dark: 1',
  29173. '};',
  29174. 'this.$rtti.$Enum("TFlag", {',
  29175. ' minvalue: 0,',
  29176. ' maxvalue: 1,',
  29177. ' ordtype: 1,',
  29178. ' enumtype: this.TFlag',
  29179. '});',
  29180. 'this.$rtti.$StaticArray("TFlagNames", {',
  29181. ' dims: [2],',
  29182. ' eltype: rtl.string',
  29183. '});',
  29184. 'this.$rtti.$StaticArray("TBoolNames", {',
  29185. ' dims: [2],',
  29186. ' eltype: rtl.string',
  29187. '});',
  29188. 'this.$rtti.$StaticArray("TByteArray", {',
  29189. ' dims: [32768],',
  29190. ' eltype: rtl.byte',
  29191. '});',
  29192. 'this.$rtti.$ProcVar("TProc", {',
  29193. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  29194. '});',
  29195. 'this.p = null;',
  29196. '']),
  29197. LinesToStr([ // $mod.$main
  29198. '$mod.p = $mod.$rtti["TFlagNames"];',
  29199. '$mod.p = $mod.$rtti["TBoolNames"];',
  29200. '']));
  29201. end;
  29202. procedure TTestModule.TestRTTI_DynArray;
  29203. begin
  29204. WithTypeInfo:=true;
  29205. StartProgram(false);
  29206. Add('type');
  29207. Add(' TArrStr = array of string;');
  29208. Add(' TArr2Dim = array of tarrstr;');
  29209. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  29210. Add('var p: pointer;');
  29211. Add('begin');
  29212. Add(' p:=typeinfo(tarrstr);');
  29213. Add(' p:=typeinfo(tarr2dim);');
  29214. ConvertProgram;
  29215. CheckSource('TestRTTI_DynArray',
  29216. LinesToStr([ // statements
  29217. 'this.$rtti.$DynArray("TArrStr", {',
  29218. ' eltype: rtl.string',
  29219. '});',
  29220. 'this.$rtti.$DynArray("TArr2Dim", {',
  29221. ' eltype: this.$rtti["TArrStr"]',
  29222. '});',
  29223. 'this.$rtti.$ProcVar("TProc", {',
  29224. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  29225. '});',
  29226. 'this.p = null;',
  29227. '']),
  29228. LinesToStr([ // $mod.$main
  29229. '$mod.p = $mod.$rtti["TArrStr"];',
  29230. '$mod.p = $mod.$rtti["TArr2Dim"];',
  29231. '']));
  29232. end;
  29233. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  29234. begin
  29235. WithTypeInfo:=true;
  29236. StartProgram(false);
  29237. Add('type');
  29238. Add(' TArr = array of array of longint;');
  29239. Add('var a: TArr;');
  29240. Add('begin');
  29241. ConvertProgram;
  29242. CheckSource('TestRTTI_ArrayNestedAnonymous',
  29243. LinesToStr([ // statements
  29244. 'this.$rtti.$DynArray("TArr$a", {',
  29245. ' eltype: rtl.longint',
  29246. '});',
  29247. 'this.$rtti.$DynArray("TArr", {',
  29248. ' eltype: this.$rtti["TArr$a"]',
  29249. '});',
  29250. 'this.a = [];',
  29251. '']),
  29252. LinesToStr([ // $mod.$main
  29253. ]));
  29254. end;
  29255. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  29256. begin
  29257. WithTypeInfo:=true;
  29258. StartProgram(false);
  29259. Add('type');
  29260. Add(' TObject = class');
  29261. Add(' published');
  29262. Add(' procedure Proc; virtual; abstract;');
  29263. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  29264. Add(' end;');
  29265. Add('begin');
  29266. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  29267. nDuplicatePublishedMethodXAtY);
  29268. ConvertProgram;
  29269. end;
  29270. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  29271. begin
  29272. WithTypeInfo:=true;
  29273. StartUnit(false);
  29274. Add([
  29275. 'interface',
  29276. 'type',
  29277. ' TObject = class',
  29278. ' end;',
  29279. ' {$M+}',
  29280. ' TBird = class',
  29281. ' procedure Fly;',
  29282. ' end;',
  29283. ' {$M-}',
  29284. 'type',
  29285. ' TEagle = class(TBird)',
  29286. ' procedure Fly;',
  29287. ' end;',
  29288. 'implementation',
  29289. 'procedure TBird.Fly;',
  29290. 'begin',
  29291. 'end;',
  29292. 'procedure TEagle.Fly;',
  29293. 'begin',
  29294. 'end;',
  29295. '']);
  29296. ConvertUnit;
  29297. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  29298. LinesToStr([ // statements
  29299. 'rtl.createClass(this, "TObject", null, function () {',
  29300. ' this.$init = function () {',
  29301. ' };',
  29302. ' this.$final = function () {',
  29303. ' };',
  29304. '});',
  29305. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29306. ' this.Fly = function () {',
  29307. ' };',
  29308. ' var $r = this.$rtti;',
  29309. ' $r.addMethod("Fly", 0, []);',
  29310. '});',
  29311. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  29312. ' this.Fly = function () {',
  29313. ' };',
  29314. ' var $r = this.$rtti;',
  29315. ' $r.addMethod("Fly", 0, []);',
  29316. '});',
  29317. '']),
  29318. LinesToStr([ // $mod.$main
  29319. ]));
  29320. CheckResolverUnexpectedHints(true);
  29321. end;
  29322. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  29323. begin
  29324. WithTypeInfo:=true;
  29325. StartProgram(false);
  29326. Add('type');
  29327. Add(' TObject = class');
  29328. Add(' published');
  29329. Add(' procedure Proc; external name ''foo'';');
  29330. Add(' end;');
  29331. Add('begin');
  29332. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29333. nPublishedNameMustMatchExternal);
  29334. ConvertProgram;
  29335. end;
  29336. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  29337. begin
  29338. WithTypeInfo:=true;
  29339. StartProgram(false);
  29340. Add('type');
  29341. Add(' TObject = class');
  29342. Add(' class var FA: longint;');
  29343. Add(' published');
  29344. Add(' class property A: longint read FA;');
  29345. Add(' end;');
  29346. Add('begin');
  29347. SetExpectedPasResolverError('Invalid published property modifier "class"',
  29348. nInvalidXModifierY);
  29349. ConvertProgram;
  29350. end;
  29351. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  29352. begin
  29353. WithTypeInfo:=true;
  29354. StartProgram(false);
  29355. Add('type');
  29356. Add(' TObject = class');
  29357. Add(' published');
  29358. Add(' class var FA: longint;');
  29359. Add(' end;');
  29360. Add('begin');
  29361. SetExpectedPasResolverError(sSymbolCannotBePublished,
  29362. nSymbolCannotBePublished);
  29363. ConvertProgram;
  29364. end;
  29365. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  29366. begin
  29367. WithTypeInfo:=true;
  29368. StartProgram(false);
  29369. Add('{$modeswitch externalclass}');
  29370. Add('type');
  29371. Add(' TObject = class');
  29372. Add(' published');
  29373. Add(' V: longint; external name ''foo'';');
  29374. Add(' end;');
  29375. Add('begin');
  29376. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29377. nPublishedNameMustMatchExternal);
  29378. ConvertProgram;
  29379. end;
  29380. procedure TTestModule.TestRTTI_Class_Field;
  29381. begin
  29382. WithTypeInfo:=true;
  29383. StartProgram(false);
  29384. Add('{$modeswitch externalclass}');
  29385. Add('type');
  29386. Add(' TObject = class');
  29387. Add(' private');
  29388. Add(' FPropA: string;');
  29389. Add(' published');
  29390. Add(' VarLI: longint;');
  29391. Add(' VarC: char;');
  29392. Add(' VarS: string;');
  29393. Add(' VarD: double;');
  29394. Add(' VarB: boolean;');
  29395. Add(' VarLW: longword;');
  29396. Add(' VarSmI: smallint;');
  29397. Add(' VarW: word;');
  29398. Add(' VarShI: shortint;');
  29399. Add(' VarBy: byte;');
  29400. Add(' VarExt: longint external name ''VarExt'';');
  29401. Add(' ArrA, ArrB: array of byte;');
  29402. Add(' end;');
  29403. Add('var p: pointer;');
  29404. Add(' Obj: tobject;');
  29405. Add('begin');
  29406. Add(' p:=typeinfo(tobject);');
  29407. Add(' p:=typeinfo(p);');
  29408. Add(' p:=typeinfo(obj);');
  29409. ConvertProgram;
  29410. CheckSource('TestRTTI_Class_Field',
  29411. LinesToStr([ // statements
  29412. 'rtl.createClass(this, "TObject", null, function () {',
  29413. ' this.$init = function () {',
  29414. ' this.FPropA = "";',
  29415. ' this.VarLI = 0;',
  29416. ' this.VarC = "";',
  29417. ' this.VarS = "";',
  29418. ' this.VarD = 0.0;',
  29419. ' this.VarB = false;',
  29420. ' this.VarLW = 0;',
  29421. ' this.VarSmI = 0;',
  29422. ' this.VarW = 0;',
  29423. ' this.VarShI = 0;',
  29424. ' this.VarBy = 0;',
  29425. ' this.ArrA = [];',
  29426. ' this.ArrB = [];',
  29427. ' };',
  29428. ' this.$final = function () {',
  29429. ' this.ArrA = undefined;',
  29430. ' this.ArrB = undefined;',
  29431. ' };',
  29432. ' var $r = this.$rtti;',
  29433. ' $r.addField("VarLI", rtl.longint);',
  29434. ' $r.addField("VarC", rtl.char);',
  29435. ' $r.addField("VarS", rtl.string);',
  29436. ' $r.addField("VarD", rtl.double);',
  29437. ' $r.addField("VarB", rtl.boolean);',
  29438. ' $r.addField("VarLW", rtl.longword);',
  29439. ' $r.addField("VarSmI", rtl.smallint);',
  29440. ' $r.addField("VarW", rtl.word);',
  29441. ' $r.addField("VarShI", rtl.shortint);',
  29442. ' $r.addField("VarBy", rtl.byte);',
  29443. ' $r.addField("VarExt", rtl.longint);',
  29444. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29445. ' eltype: rtl.byte',
  29446. ' });',
  29447. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29448. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29449. '});',
  29450. 'this.p = null;',
  29451. 'this.Obj = null;',
  29452. '']),
  29453. LinesToStr([ // $mod.$main
  29454. '$mod.p = $mod.$rtti["TObject"];',
  29455. '$mod.p = rtl.pointer;',
  29456. '$mod.p = $mod.Obj.$rtti;',
  29457. '']));
  29458. end;
  29459. procedure TTestModule.TestRTTI_Class_Method;
  29460. begin
  29461. WithTypeInfo:=true;
  29462. StartProgram(false);
  29463. Add([
  29464. 'type',
  29465. ' TObject = class',
  29466. ' private',
  29467. ' procedure Internal; external name ''$intern'';',
  29468. ' published',
  29469. ' procedure Click; virtual; abstract;',
  29470. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29471. ' function GetNotify: boolean; external name ''GetNotify'';',
  29472. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29473. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29474. ' end;',
  29475. 'begin']);
  29476. ConvertProgram;
  29477. CheckSource('TestRTTI_Class_Method',
  29478. LinesToStr([ // statements
  29479. 'rtl.createClass(this, "TObject", null, function () {',
  29480. ' this.$init = function () {',
  29481. ' };',
  29482. ' this.$final = function () {',
  29483. ' };',
  29484. ' var $r = this.$rtti;',
  29485. ' $r.addMethod("Click", 0, []);',
  29486. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29487. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29488. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29489. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29490. '});',
  29491. '']),
  29492. LinesToStr([ // $mod.$main
  29493. '']));
  29494. end;
  29495. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29496. begin
  29497. WithTypeInfo:=true;
  29498. StartProgram(false);
  29499. Add('type');
  29500. Add(' TObject = class');
  29501. Add(' published');
  29502. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29503. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29504. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29505. Add(' end;');
  29506. Add('begin');
  29507. ConvertProgram;
  29508. CheckSource('TestRTTI_Class_MethodOpenArray',
  29509. LinesToStr([ // statements
  29510. 'rtl.createClass(this, "TObject", null, function () {',
  29511. ' this.$init = function () {',
  29512. ' };',
  29513. ' this.$final = function () {',
  29514. ' };',
  29515. ' var $r = this.$rtti;',
  29516. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29517. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29518. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29519. '});',
  29520. '']),
  29521. LinesToStr([ // $mod.$main
  29522. '']));
  29523. end;
  29524. procedure TTestModule.TestRTTI_Class_Property;
  29525. begin
  29526. WithTypeInfo:=true;
  29527. StartProgram(false);
  29528. Add('{$modeswitch externalclass}');
  29529. Add('type');
  29530. Add(' TObject = class');
  29531. Add(' private');
  29532. Add(' FColor: longint;');
  29533. Add(' FColorStored: boolean;');
  29534. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29535. Add(' function GetColor: longint; virtual; abstract;');
  29536. Add(' function GetColorStored: boolean; virtual; abstract;');
  29537. Add(' FExtSize: longint external name ''$extSize'';');
  29538. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29539. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29540. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29541. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29542. Add(' published');
  29543. Add(' property ColorA: longint read FColor;');
  29544. Add(' property ColorB: longint write FColor;');
  29545. Add(' property ColorC: longint read GetColor write SetColor;');
  29546. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29547. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29548. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29549. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29550. Add(' end;');
  29551. Add('begin');
  29552. ConvertProgram;
  29553. CheckSource('TestRTTI_Class_Property',
  29554. LinesToStr([ // statements
  29555. 'rtl.createClass(this, "TObject", null, function () {',
  29556. ' this.$init = function () {',
  29557. ' this.FColor = 0;',
  29558. ' this.FColorStored = false;',
  29559. ' };',
  29560. ' this.$final = function () {',
  29561. ' };',
  29562. ' var $r = this.$rtti;',
  29563. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29564. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29565. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29566. ' $r.addProperty(',
  29567. ' "ColorD",',
  29568. ' 8,',
  29569. ' rtl.longint,',
  29570. ' "FColor",',
  29571. ' "FColor",',
  29572. ' {',
  29573. ' stored: "FColorStored"',
  29574. ' }',
  29575. ' );',
  29576. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29577. ' $r.addProperty(',
  29578. ' "ExtSizeB",',
  29579. ' 11,',
  29580. ' rtl.longint,',
  29581. ' "$getSize",',
  29582. ' "$setSize",',
  29583. ' {',
  29584. ' stored: "$extSizeStored"',
  29585. ' }',
  29586. ' );',
  29587. ' $r.addProperty(',
  29588. ' "ExtSizeC",',
  29589. ' 12,',
  29590. ' rtl.longint,',
  29591. ' "$extSize",',
  29592. ' "$extSize",',
  29593. ' {',
  29594. ' stored: "$getExtSizeStored"',
  29595. ' }',
  29596. ' );',
  29597. '});',
  29598. '']),
  29599. LinesToStr([ // $mod.$main
  29600. '']));
  29601. end;
  29602. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29603. begin
  29604. WithTypeInfo:=true;
  29605. StartProgram(false);
  29606. Add('{$modeswitch externalclass}');
  29607. Add('type');
  29608. Add(' integer = longint;');
  29609. Add(' TObject = class');
  29610. Add(' private');
  29611. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29612. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29613. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29614. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29615. Add(' published');
  29616. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29617. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29618. Add(' end;');
  29619. Add('begin');
  29620. ConvertProgram;
  29621. CheckSource('TestRTTI_Class_PropertyParams',
  29622. LinesToStr([ // statements
  29623. 'rtl.createClass(this, "TObject", null, function () {',
  29624. ' this.$init = function () {',
  29625. ' };',
  29626. ' this.$final = function () {',
  29627. ' };',
  29628. ' var $r = this.$rtti;',
  29629. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29630. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29631. '});',
  29632. '']),
  29633. LinesToStr([ // $mod.$main
  29634. '']));
  29635. end;
  29636. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29637. begin
  29638. WithTypeInfo:=true;
  29639. AddModuleWithIntfImplSrc('unit1.pas',
  29640. 'type TColor = -5..5;',
  29641. '');
  29642. StartProgram(true);
  29643. Add([
  29644. 'uses unit1;',
  29645. 'type',
  29646. ' TColorAlias = TColor;',
  29647. ' TColorTypeAlias = type TColor;',
  29648. ' TObject = class',
  29649. ' private',
  29650. ' fColor: TColor;',
  29651. ' fAlias: TColorAlias;',
  29652. ' fTypeAlias: TColorTypeAlias;',
  29653. ' published',
  29654. ' property Color: TColor read fcolor;',
  29655. ' property Alias: TColorAlias read falias;',
  29656. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29657. ' end;',
  29658. 'begin',
  29659. '']);
  29660. ConvertProgram;
  29661. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29662. LinesToStr([ // statements
  29663. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29664. 'rtl.createClass(this, "TObject", null, function () {',
  29665. ' this.$init = function () {',
  29666. ' this.fColor = 0;',
  29667. ' this.fAlias = 0;',
  29668. ' this.fTypeAlias = 0;',
  29669. ' };',
  29670. ' this.$final = function () {',
  29671. ' };',
  29672. ' var $r = this.$rtti;',
  29673. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29674. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29675. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29676. '});',
  29677. '']),
  29678. LinesToStr([ // $mod.$main
  29679. '']));
  29680. end;
  29681. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29682. begin
  29683. WithTypeInfo:=true;
  29684. StartProgram(false);
  29685. Add([
  29686. '{$modeswitch omitrtti}',
  29687. 'type',
  29688. ' TObject = class',
  29689. ' private',
  29690. ' FA: byte;',
  29691. ' published',
  29692. ' property A: byte read FA write FA;',
  29693. ' end;',
  29694. 'begin']);
  29695. ConvertProgram;
  29696. CheckSource('TestRTTI_Class_OmitRTTI',
  29697. LinesToStr([ // statements
  29698. 'rtl.createClass(this, "TObject", null, function () {',
  29699. ' this.$init = function () {',
  29700. ' this.FA = 0;',
  29701. ' };',
  29702. ' this.$final = function () {',
  29703. ' };',
  29704. '});',
  29705. '']),
  29706. LinesToStr([ // $mod.$main
  29707. '']));
  29708. end;
  29709. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29710. begin
  29711. WithTypeInfo:=true;
  29712. StartUnit(true,[supTObject]);
  29713. Add([
  29714. 'interface',
  29715. 'type',
  29716. ' {$M+}',
  29717. ' TBird = class',
  29718. ' published',
  29719. ' Swarm: array of TBird;',
  29720. ' end;',
  29721. 'implementation',
  29722. '']);
  29723. ConvertUnit;
  29724. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29725. LinesToStr([ // statements
  29726. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29727. ' this.$init = function () {',
  29728. ' pas.system.TObject.$init.call(this);',
  29729. ' this.Swarm = [];',
  29730. ' };',
  29731. ' this.$final = function () {',
  29732. ' this.Swarm = undefined;',
  29733. ' pas.system.TObject.$final.call(this);',
  29734. ' };',
  29735. ' var $r = this.$rtti;',
  29736. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29737. ' eltype: $r',
  29738. ' });',
  29739. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29740. '});',
  29741. '']),
  29742. LinesToStr([ // $mod.$main
  29743. '']));
  29744. end;
  29745. procedure TTestModule.TestRTTI_IndexModifier;
  29746. begin
  29747. WithTypeInfo:=true;
  29748. StartProgram(false);
  29749. Add([
  29750. 'type',
  29751. ' TEnum = (red, blue);',
  29752. ' TObject = class',
  29753. ' FB: boolean;',
  29754. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29755. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29756. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29757. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29758. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29759. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29760. ' published',
  29761. ' property B1: boolean index 1 read FB write SetIntBool;',
  29762. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29763. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29764. ' end;',
  29765. 'begin']);
  29766. ConvertProgram;
  29767. CheckSource('TestRTTI_IndexModifier',
  29768. LinesToStr([ // statements
  29769. 'this.TEnum = {',
  29770. ' "0": "red",',
  29771. ' red: 0,',
  29772. ' "1": "blue",',
  29773. ' blue: 1',
  29774. '};',
  29775. 'this.$rtti.$Enum("TEnum", {',
  29776. ' minvalue: 0,',
  29777. ' maxvalue: 1,',
  29778. ' ordtype: 1,',
  29779. ' enumtype: this.TEnum',
  29780. '});',
  29781. 'rtl.createClass(this, "TObject", null, function () {',
  29782. ' this.$init = function () {',
  29783. ' this.FB = false;',
  29784. ' };',
  29785. ' this.$final = function () {',
  29786. ' };',
  29787. ' var $r = this.$rtti;',
  29788. ' $r.addProperty(',
  29789. ' "B1",',
  29790. ' 18,',
  29791. ' rtl.boolean,',
  29792. ' "FB",',
  29793. ' "SetIntBool",',
  29794. ' {',
  29795. ' index: 1',
  29796. ' }',
  29797. ' );',
  29798. ' $r.addProperty(',
  29799. ' "B2",',
  29800. ' 17,',
  29801. ' rtl.boolean,',
  29802. ' "GetEnumBool",',
  29803. ' "FB",',
  29804. ' {',
  29805. ' index: $mod.TEnum.blue',
  29806. ' }',
  29807. ' );',
  29808. ' $r.addProperty(',
  29809. ' "I1",',
  29810. ' 19,',
  29811. ' rtl.boolean,',
  29812. ' "GetStrIntBool",',
  29813. ' "SetStrIntBool",',
  29814. ' {',
  29815. ' index: 2',
  29816. ' }',
  29817. ' );',
  29818. '});',
  29819. '']),
  29820. LinesToStr([ // $mod.$main
  29821. '']));
  29822. end;
  29823. procedure TTestModule.TestRTTI_StoredModifier;
  29824. begin
  29825. WithTypeInfo:=true;
  29826. StartProgram(false);
  29827. Add([
  29828. 'const',
  29829. ' ConstB = true;',
  29830. 'type',
  29831. ' TObject = class',
  29832. ' private',
  29833. ' FB: boolean;',
  29834. ' function IsBStored: boolean; virtual; abstract;',
  29835. ' published',
  29836. ' property BoolA: boolean read FB stored true;',
  29837. ' property BoolB: boolean read FB stored false;',
  29838. ' property BoolC: boolean read FB stored FB;',
  29839. ' property BoolD: boolean read FB stored ConstB;',
  29840. ' property BoolE: boolean read FB stored IsBStored;',
  29841. ' end;',
  29842. 'begin']);
  29843. ConvertProgram;
  29844. CheckSource('TestRTTI_StoredModifier',
  29845. LinesToStr([ // statements
  29846. 'this.ConstB = true;',
  29847. 'rtl.createClass(this, "TObject", null, function () {',
  29848. ' this.$init = function () {',
  29849. ' this.FB = false;',
  29850. ' };',
  29851. ' this.$final = function () {',
  29852. ' };',
  29853. ' var $r = this.$rtti;',
  29854. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29855. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29856. ' $r.addProperty(',
  29857. ' "BoolC",',
  29858. ' 8,',
  29859. ' rtl.boolean,',
  29860. ' "FB",',
  29861. ' "",',
  29862. ' {',
  29863. ' stored: "FB"',
  29864. ' }',
  29865. ' );',
  29866. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29867. ' $r.addProperty(',
  29868. ' "BoolE",',
  29869. ' 12,',
  29870. ' rtl.boolean,',
  29871. ' "FB",',
  29872. ' "",',
  29873. ' {',
  29874. ' stored: "IsBStored"',
  29875. ' }',
  29876. ' );',
  29877. '});',
  29878. '']),
  29879. LinesToStr([ // $mod.$main
  29880. '']));
  29881. end;
  29882. procedure TTestModule.TestRTTI_DefaultValue;
  29883. begin
  29884. WithTypeInfo:=true;
  29885. StartProgram(false);
  29886. Add([
  29887. 'type',
  29888. ' TEnum = (red, blue);',
  29889. 'const',
  29890. ' CB = true or false;',
  29891. ' CI = 1+2;',
  29892. 'type',
  29893. ' TObject = class',
  29894. ' FB: boolean;',
  29895. ' FI: longint;',
  29896. ' FE: TEnum;',
  29897. ' published',
  29898. ' property B1: boolean read FB default true;',
  29899. ' property B2: boolean read FB default CB;',
  29900. ' property B3: boolean read FB default test1.cb;',
  29901. ' property I1: longint read FI default 2;',
  29902. ' property I2: longint read FI default CI;',
  29903. ' property E1: TEnum read FE default red;',
  29904. ' property E2: TEnum read FE default TEnum.blue;',
  29905. ' end;',
  29906. 'begin']);
  29907. ConvertProgram;
  29908. CheckSource('TestRTTI_DefaultValue',
  29909. LinesToStr([ // statements
  29910. 'this.TEnum = {',
  29911. ' "0": "red",',
  29912. ' red: 0,',
  29913. ' "1": "blue",',
  29914. ' blue: 1',
  29915. '};',
  29916. 'this.$rtti.$Enum("TEnum", {',
  29917. ' minvalue: 0,',
  29918. ' maxvalue: 1,',
  29919. ' ordtype: 1,',
  29920. ' enumtype: this.TEnum',
  29921. '});',
  29922. 'this.CB = true || false;',
  29923. 'this.CI = 1 + 2;',
  29924. 'rtl.createClass(this, "TObject", null, function () {',
  29925. ' this.$init = function () {',
  29926. ' this.FB = false;',
  29927. ' this.FI = 0;',
  29928. ' this.FE = 0;',
  29929. ' };',
  29930. ' this.$final = function () {',
  29931. ' };',
  29932. ' var $r = this.$rtti;',
  29933. ' $r.addProperty(',
  29934. ' "B1",',
  29935. ' 0,',
  29936. ' rtl.boolean,',
  29937. ' "FB",',
  29938. ' "",',
  29939. ' {',
  29940. ' Default: true',
  29941. ' }',
  29942. ' );',
  29943. ' $r.addProperty(',
  29944. ' "B2",',
  29945. ' 0,',
  29946. ' rtl.boolean,',
  29947. ' "FB",',
  29948. ' "",',
  29949. ' {',
  29950. ' Default: true',
  29951. ' }',
  29952. ' );',
  29953. ' $r.addProperty(',
  29954. ' "B3",',
  29955. ' 0,',
  29956. ' rtl.boolean,',
  29957. ' "FB",',
  29958. ' "",',
  29959. ' {',
  29960. ' Default: true',
  29961. ' }',
  29962. ' );',
  29963. ' $r.addProperty(',
  29964. ' "I1",',
  29965. ' 0,',
  29966. ' rtl.longint,',
  29967. ' "FI",',
  29968. ' "",',
  29969. ' {',
  29970. ' Default: 2',
  29971. ' }',
  29972. ' );',
  29973. ' $r.addProperty(',
  29974. ' "I2",',
  29975. ' 0,',
  29976. ' rtl.longint,',
  29977. ' "FI",',
  29978. ' "",',
  29979. ' {',
  29980. ' Default: 3',
  29981. ' }',
  29982. ' );',
  29983. ' $r.addProperty(',
  29984. ' "E1",',
  29985. ' 0,',
  29986. ' $mod.$rtti["TEnum"],',
  29987. ' "FE",',
  29988. ' "",',
  29989. ' {',
  29990. ' Default: $mod.TEnum.red',
  29991. ' }',
  29992. ' );',
  29993. ' $r.addProperty(',
  29994. ' "E2",',
  29995. ' 0,',
  29996. ' $mod.$rtti["TEnum"],',
  29997. ' "FE",',
  29998. ' "",',
  29999. ' {',
  30000. ' Default: $mod.TEnum.blue',
  30001. ' }',
  30002. ' );',
  30003. '});',
  30004. '']),
  30005. LinesToStr([ // $mod.$main
  30006. '']));
  30007. end;
  30008. procedure TTestModule.TestRTTI_DefaultValueSet;
  30009. begin
  30010. WithTypeInfo:=true;
  30011. StartProgram(false);
  30012. Add([
  30013. 'type',
  30014. ' TEnum = (red, blue);',
  30015. ' TSet = set of TEnum;',
  30016. 'const',
  30017. ' CSet = [red,blue];',
  30018. 'type',
  30019. ' TObject = class',
  30020. ' FSet: TSet;',
  30021. ' published',
  30022. ' property Set1: TSet read FSet default [];',
  30023. ' property Set2: TSet read FSet default [red];',
  30024. ' property Set3: TSet read FSet default [red,blue];',
  30025. ' property Set4: TSet read FSet default CSet;',
  30026. ' end;',
  30027. 'begin']);
  30028. ConvertProgram;
  30029. CheckSource('TestRTTI_DefaultValueSet',
  30030. LinesToStr([ // statements
  30031. 'this.TEnum = {',
  30032. ' "0": "red",',
  30033. ' red: 0,',
  30034. ' "1": "blue",',
  30035. ' blue: 1',
  30036. '};',
  30037. 'this.$rtti.$Enum("TEnum", {',
  30038. ' minvalue: 0,',
  30039. ' maxvalue: 1,',
  30040. ' ordtype: 1,',
  30041. ' enumtype: this.TEnum',
  30042. '});',
  30043. 'this.$rtti.$Set("TSet", {',
  30044. ' comptype: this.$rtti["TEnum"]',
  30045. '});',
  30046. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  30047. 'rtl.createClass(this, "TObject", null, function () {',
  30048. ' this.$init = function () {',
  30049. ' this.FSet = {};',
  30050. ' };',
  30051. ' this.$final = function () {',
  30052. ' this.FSet = undefined;',
  30053. ' };',
  30054. ' var $r = this.$rtti;',
  30055. ' $r.addProperty(',
  30056. ' "Set1",',
  30057. ' 0,',
  30058. ' $mod.$rtti["TSet"],',
  30059. ' "FSet",',
  30060. ' "",',
  30061. ' {',
  30062. ' Default: {}',
  30063. ' }',
  30064. ' );',
  30065. ' $r.addProperty(',
  30066. ' "Set2",',
  30067. ' 0,',
  30068. ' $mod.$rtti["TSet"],',
  30069. ' "FSet",',
  30070. ' "",',
  30071. ' {',
  30072. ' Default: rtl.createSet($mod.TEnum.red)',
  30073. ' }',
  30074. ' );',
  30075. ' $r.addProperty(',
  30076. ' "Set3",',
  30077. ' 0,',
  30078. ' $mod.$rtti["TSet"],',
  30079. ' "FSet",',
  30080. ' "",',
  30081. ' {',
  30082. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  30083. ' }',
  30084. ' );',
  30085. ' $r.addProperty(',
  30086. ' "Set4",',
  30087. ' 0,',
  30088. ' $mod.$rtti["TSet"],',
  30089. ' "FSet",',
  30090. ' "",',
  30091. ' {',
  30092. ' Default: $mod.CSet',
  30093. ' }',
  30094. ' );',
  30095. '});',
  30096. '']),
  30097. LinesToStr([ // $mod.$main
  30098. '']));
  30099. end;
  30100. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  30101. begin
  30102. WithTypeInfo:=true;
  30103. StartProgram(false);
  30104. Add([
  30105. 'type',
  30106. ' TRg = -1..1;',
  30107. 'const',
  30108. ' l = low(TRg);',
  30109. ' h = high(TRg);',
  30110. 'type',
  30111. ' TObject = class',
  30112. ' FV: TRg;',
  30113. ' published',
  30114. ' property V1: TRg read FV default -1;',
  30115. ' end;',
  30116. 'begin']);
  30117. ConvertProgram;
  30118. CheckSource('TestRTTI_DefaultValueRangeType',
  30119. LinesToStr([ // statements
  30120. 'this.$rtti.$Int("TRg", {',
  30121. ' minvalue: -1,',
  30122. ' maxvalue: 1,',
  30123. ' ordtype: 0',
  30124. '});',
  30125. 'this.l = -1;',
  30126. 'this.h = 1;',
  30127. 'rtl.createClass(this, "TObject", null, function () {',
  30128. ' this.$init = function () {',
  30129. ' this.FV = 0;',
  30130. ' };',
  30131. ' this.$final = function () {',
  30132. ' };',
  30133. ' var $r = this.$rtti;',
  30134. ' $r.addProperty(',
  30135. ' "V1",',
  30136. ' 0,',
  30137. ' $mod.$rtti["TRg"],',
  30138. ' "FV",',
  30139. ' "",',
  30140. ' {',
  30141. ' Default: -1',
  30142. ' }',
  30143. ' );',
  30144. '});',
  30145. '']),
  30146. LinesToStr([ // $mod.$main
  30147. '']));
  30148. end;
  30149. procedure TTestModule.TestRTTI_DefaultValueInherit;
  30150. begin
  30151. WithTypeInfo:=true;
  30152. StartProgram(false);
  30153. Add([
  30154. 'type',
  30155. ' TObject = class',
  30156. ' FA, FB: byte;',
  30157. ' property A: byte read FA default 1;',
  30158. ' property B: byte read FB default 2;',
  30159. ' end;',
  30160. ' TBird = class',
  30161. ' published',
  30162. ' property A;',
  30163. ' property B nodefault;',
  30164. ' end;',
  30165. 'begin']);
  30166. ConvertProgram;
  30167. CheckSource('TestRTTI_DefaultValueInherit',
  30168. LinesToStr([ // statements
  30169. 'rtl.createClass(this, "TObject", null, function () {',
  30170. ' this.$init = function () {',
  30171. ' this.FA = 0;',
  30172. ' this.FB = 0;',
  30173. ' };',
  30174. ' this.$final = function () {',
  30175. ' };',
  30176. '});',
  30177. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30178. ' var $r = this.$rtti;',
  30179. ' $r.addProperty(',
  30180. ' "A",',
  30181. ' 0,',
  30182. ' rtl.byte,',
  30183. ' "FA",',
  30184. ' "",',
  30185. ' {',
  30186. ' Default: 1',
  30187. ' }',
  30188. ' );',
  30189. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  30190. '});',
  30191. '']),
  30192. LinesToStr([ // $mod.$main
  30193. '']));
  30194. end;
  30195. procedure TTestModule.TestRTTI_OverrideMethod;
  30196. begin
  30197. WithTypeInfo:=true;
  30198. StartProgram(false);
  30199. Add('type');
  30200. Add(' TObject = class');
  30201. Add(' published');
  30202. Add(' procedure DoIt; virtual; abstract;');
  30203. Add(' end;');
  30204. Add(' TSky = class');
  30205. Add(' published');
  30206. Add(' procedure DoIt; override;');
  30207. Add(' end;');
  30208. Add('procedure TSky.DoIt; begin end;');
  30209. Add('begin');
  30210. ConvertProgram;
  30211. CheckSource('TestRTTI_OverrideMethod',
  30212. LinesToStr([ // statements
  30213. 'rtl.createClass(this, "TObject", null, function () {',
  30214. ' this.$init = function () {',
  30215. ' };',
  30216. ' this.$final = function () {',
  30217. ' };',
  30218. ' var $r = this.$rtti;',
  30219. ' $r.addMethod("DoIt", 0, []);',
  30220. '});',
  30221. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30222. ' this.DoIt = function () {',
  30223. ' };',
  30224. '});',
  30225. '']),
  30226. LinesToStr([ // $mod.$main
  30227. '']));
  30228. end;
  30229. procedure TTestModule.TestRTTI_ReintroduceMethod;
  30230. begin
  30231. WithTypeInfo:=true;
  30232. StartProgram(false);
  30233. Add([
  30234. 'type',
  30235. ' TObject = class',
  30236. ' published',
  30237. ' procedure DoIt;',
  30238. ' end;',
  30239. ' TSky = class',
  30240. ' published',
  30241. ' procedure DoIt; reintroduce;',
  30242. ' end;',
  30243. 'procedure TObject.DoIt; begin end;',
  30244. 'procedure TSky.DoIt;',
  30245. 'begin',
  30246. ' inherited DoIt;',
  30247. 'end;',
  30248. 'begin']);
  30249. ConvertProgram;
  30250. CheckSource('TestRTTI_ReintroduceMethod',
  30251. LinesToStr([ // statements
  30252. 'rtl.createClass(this, "TObject", null, function () {',
  30253. ' this.$init = function () {',
  30254. ' };',
  30255. ' this.$final = function () {',
  30256. ' };',
  30257. ' this.DoIt = function () {',
  30258. ' };',
  30259. ' var $r = this.$rtti;',
  30260. ' $r.addMethod("DoIt", 0, []);',
  30261. '});',
  30262. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30263. ' this.DoIt = function () {',
  30264. ' $mod.TObject.DoIt.call(this);',
  30265. ' };',
  30266. ' var $r = this.$rtti;',
  30267. ' $r.addMethod("DoIt", 0, []);',
  30268. '});',
  30269. '']),
  30270. LinesToStr([ // $mod.$main
  30271. '']));
  30272. end;
  30273. procedure TTestModule.TestRTTI_OverloadProperty;
  30274. begin
  30275. WithTypeInfo:=true;
  30276. StartProgram(false);
  30277. Add('type');
  30278. Add(' TObject = class');
  30279. Add(' protected');
  30280. Add(' FFlag: longint;');
  30281. Add(' published');
  30282. Add(' property Flag: longint read fflag;');
  30283. Add(' end;');
  30284. Add(' TSky = class');
  30285. Add(' published');
  30286. Add(' property FLAG: longint write fflag;');
  30287. Add(' end;');
  30288. Add('begin');
  30289. ConvertProgram;
  30290. CheckSource('TestRTTI_OverrideMethod',
  30291. LinesToStr([ // statements
  30292. 'rtl.createClass(this, "TObject", null, function () {',
  30293. ' this.$init = function () {',
  30294. ' this.FFlag = 0;',
  30295. ' };',
  30296. ' this.$final = function () {',
  30297. ' };',
  30298. ' var $r = this.$rtti;',
  30299. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  30300. '});',
  30301. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30302. ' var $r = this.$rtti;',
  30303. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  30304. '});',
  30305. '']),
  30306. LinesToStr([ // $mod.$main
  30307. '']));
  30308. end;
  30309. procedure TTestModule.TestRTTI_ClassForward;
  30310. begin
  30311. WithTypeInfo:=true;
  30312. StartProgram(false);
  30313. Add('type');
  30314. Add(' TObject = class end;');
  30315. Add(' tbridge = class;');
  30316. Add(' TProc = function: tbridge;');
  30317. Add(' TOger = class');
  30318. Add(' published');
  30319. Add(' FBridge: tbridge;');
  30320. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  30321. Add(' property Bridge: tbridge read fbridge write setbridge;');
  30322. Add(' end;');
  30323. Add(' TBridge = class');
  30324. Add(' FOger: toger;');
  30325. Add(' end;');
  30326. Add('var p: Pointer;');
  30327. Add(' b: tbridge;');
  30328. Add('begin');
  30329. Add(' p:=typeinfo(tbridge);');
  30330. Add(' p:=typeinfo(b);');
  30331. ConvertProgram;
  30332. CheckSource('TestRTTI_ClassForward',
  30333. LinesToStr([ // statements
  30334. 'rtl.createClass(this, "TObject", null, function () {',
  30335. ' this.$init = function () {',
  30336. ' };',
  30337. ' this.$final = function () {',
  30338. ' };',
  30339. '});',
  30340. 'this.$rtti.$Class("TBridge");',
  30341. 'this.$rtti.$ProcVar("TProc", {',
  30342. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  30343. '});',
  30344. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  30345. ' this.$init = function () {',
  30346. ' $mod.TObject.$init.call(this);',
  30347. ' this.FBridge = null;',
  30348. ' };',
  30349. ' this.$final = function () {',
  30350. ' this.FBridge = undefined;',
  30351. ' $mod.TObject.$final.call(this);',
  30352. ' };',
  30353. ' var $r = this.$rtti;',
  30354. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  30355. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  30356. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  30357. '});',
  30358. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  30359. ' this.$init = function () {',
  30360. ' $mod.TObject.$init.call(this);',
  30361. ' this.FOger = null;',
  30362. ' };',
  30363. ' this.$final = function () {',
  30364. ' this.FOger = undefined;',
  30365. ' $mod.TObject.$final.call(this);',
  30366. ' };',
  30367. '});',
  30368. 'this.p = null;',
  30369. 'this.b = null;',
  30370. '']),
  30371. LinesToStr([ // $mod.$main
  30372. '$mod.p = $mod.$rtti["TBridge"];',
  30373. '$mod.p = $mod.b.$rtti;',
  30374. '']));
  30375. end;
  30376. procedure TTestModule.TestRTTI_ClassOf;
  30377. begin
  30378. WithTypeInfo:=true;
  30379. StartProgram(false);
  30380. Add('type');
  30381. Add(' TClass = class of tobject;');
  30382. Add(' TProcA = function: TClass;');
  30383. Add(' TObject = class');
  30384. Add(' published');
  30385. Add(' C: tclass;');
  30386. Add(' end;');
  30387. Add(' tfox = class;');
  30388. Add(' TBird = class end;');
  30389. Add(' TBirds = class of tbird;');
  30390. Add(' TFox = class end;');
  30391. Add(' TFoxes = class of tfox;');
  30392. Add(' TCows = class of TCow;');
  30393. Add(' TCow = class;');
  30394. Add(' TCow = class end;');
  30395. Add('begin');
  30396. ConvertProgram;
  30397. CheckSource('TestRTTI_ClassOf',
  30398. LinesToStr([ // statements
  30399. 'this.$rtti.$Class("TObject");',
  30400. 'this.$rtti.$ClassRef("TClass", {',
  30401. ' instancetype: this.$rtti["TObject"]',
  30402. '});',
  30403. 'this.$rtti.$ProcVar("TProcA", {',
  30404. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  30405. '});',
  30406. 'rtl.createClass(this, "TObject", null, function () {',
  30407. ' this.$init = function () {',
  30408. ' this.C = null;',
  30409. ' };',
  30410. ' this.$final = function () {',
  30411. ' this.C = undefined;',
  30412. ' };',
  30413. ' var $r = this.$rtti;',
  30414. ' $r.addField("C", $mod.$rtti["TClass"]);',
  30415. '});',
  30416. 'this.$rtti.$Class("TFox");',
  30417. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30418. '});',
  30419. 'this.$rtti.$ClassRef("TBirds", {',
  30420. ' instancetype: this.$rtti["TBird"]',
  30421. '});',
  30422. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  30423. '});',
  30424. 'this.$rtti.$ClassRef("TFoxes", {',
  30425. ' instancetype: this.$rtti["TFox"]',
  30426. '});',
  30427. 'this.$rtti.$Class("TCow");',
  30428. 'this.$rtti.$ClassRef("TCows", {',
  30429. ' instancetype: this.$rtti["TCow"]',
  30430. '});',
  30431. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  30432. '});',
  30433. '']),
  30434. LinesToStr([ // $mod.$main
  30435. '']));
  30436. end;
  30437. procedure TTestModule.TestRTTI_Record;
  30438. begin
  30439. WithTypeInfo:=true;
  30440. StartProgram(false);
  30441. Add('type');
  30442. Add(' integer = longint;');
  30443. Add(' TPoint = record');
  30444. Add(' x,y: integer;');
  30445. Add(' end;');
  30446. Add('var p: pointer;');
  30447. Add(' r: tpoint;');
  30448. Add('begin');
  30449. Add(' p:=typeinfo(tpoint);');
  30450. Add(' p:=typeinfo(r);');
  30451. Add(' p:=typeinfo(r.x);');
  30452. ConvertProgram;
  30453. CheckSource('TestRTTI_Record',
  30454. LinesToStr([ // statements
  30455. 'rtl.recNewT(this, "TPoint", function () {',
  30456. ' this.x = 0;',
  30457. ' this.y = 0;',
  30458. ' this.$eq = function (b) {',
  30459. ' return (this.x === b.x) && (this.y === b.y);',
  30460. ' };',
  30461. ' this.$assign = function (s) {',
  30462. ' this.x = s.x;',
  30463. ' this.y = s.y;',
  30464. ' return this;',
  30465. ' };',
  30466. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30467. ' $r.addField("x", rtl.longint);',
  30468. ' $r.addField("y", rtl.longint);',
  30469. '});',
  30470. 'this.p = null;',
  30471. 'this.r = this.TPoint.$new();',
  30472. '']),
  30473. LinesToStr([ // $mod.$main
  30474. '$mod.p = $mod.$rtti["TPoint"];',
  30475. '$mod.p = $mod.$rtti["TPoint"];',
  30476. '$mod.p = rtl.longint;',
  30477. '']));
  30478. end;
  30479. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30480. begin
  30481. WithTypeInfo:=true;
  30482. StartProgram(false);
  30483. Add('type');
  30484. Add(' TFloatRec = record');
  30485. Add(' c,d: array of char;');
  30486. // Add(' i: array of array of longint;');
  30487. Add(' end;');
  30488. Add('var p: pointer;');
  30489. Add(' r: tfloatrec;');
  30490. Add('begin');
  30491. Add(' p:=typeinfo(tfloatrec);');
  30492. Add(' p:=typeinfo(r);');
  30493. Add(' p:=typeinfo(r.d);');
  30494. ConvertProgram;
  30495. CheckSource('TestRTTI_Record',
  30496. LinesToStr([ // statements
  30497. 'rtl.recNewT(this, "TFloatRec", function () {',
  30498. ' this.$new = function () {',
  30499. ' var r = Object.create(this);',
  30500. ' r.c = [];',
  30501. ' r.d = [];',
  30502. ' return r;',
  30503. ' };',
  30504. ' this.$eq = function (b) {',
  30505. ' return (this.c === b.c) && (this.d === b.d);',
  30506. ' };',
  30507. ' this.$assign = function (s) {',
  30508. ' this.c = rtl.arrayRef(s.c);',
  30509. ' this.d = rtl.arrayRef(s.d);',
  30510. ' return this;',
  30511. ' };',
  30512. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30513. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30514. ' eltype: rtl.char',
  30515. ' });',
  30516. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30517. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30518. '});',
  30519. 'this.p = null;',
  30520. 'this.r = this.TFloatRec.$new();',
  30521. '']),
  30522. LinesToStr([ // $mod.$main
  30523. '$mod.p = $mod.$rtti["TFloatRec"];',
  30524. '$mod.p = $mod.$rtti["TFloatRec"];',
  30525. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30526. '']));
  30527. end;
  30528. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30529. begin
  30530. WithTypeInfo:=true;
  30531. StartProgram(false);
  30532. Add([
  30533. '{$modeswitch AdvancedRecords}',
  30534. 'type',
  30535. ' TPoint = record',
  30536. ' type TProc = procedure(w: word);',
  30537. ' class var p: TProc;',
  30538. ' end;',
  30539. 'begin',
  30540. '']);
  30541. ConvertProgram;
  30542. CheckSource('TestRTTI_Record_ClassVarType',
  30543. LinesToStr([ // statements
  30544. 'rtl.recNewT(this, "TPoint", function () {',
  30545. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30546. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30547. ' });',
  30548. ' this.p = null;',
  30549. ' this.$eq = function (b) {',
  30550. ' return true;',
  30551. ' };',
  30552. ' this.$assign = function (s) {',
  30553. ' return this;',
  30554. ' };',
  30555. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30556. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30557. '}, true);',
  30558. '']),
  30559. LinesToStr([ // $mod.$main
  30560. '']));
  30561. end;
  30562. procedure TTestModule.TestRTTI_LocalTypes;
  30563. begin
  30564. WithTypeInfo:=true;
  30565. StartProgram(false);
  30566. Add([
  30567. 'procedure DoIt;',
  30568. 'type',
  30569. ' integer = longint;',
  30570. ' TPoint = record',
  30571. ' x,y: integer;',
  30572. ' end;',
  30573. 'var p: TPoint;',
  30574. 'begin',
  30575. 'end;',
  30576. 'begin']);
  30577. ConvertProgram;
  30578. CheckSource('TestRTTI_LocalTypes',
  30579. LinesToStr([ // statements
  30580. 'var TPoint = rtl.recNewT(null, "", function () {',
  30581. ' this.x = 0;',
  30582. ' this.y = 0;',
  30583. ' this.$eq = function (b) {',
  30584. ' return (this.x === b.x) && (this.y === b.y);',
  30585. ' };',
  30586. ' this.$assign = function (s) {',
  30587. ' this.x = s.x;',
  30588. ' this.y = s.y;',
  30589. ' return this;',
  30590. ' };',
  30591. '});',
  30592. 'this.DoIt = function () {',
  30593. ' var p = TPoint.$new();',
  30594. '};',
  30595. '']),
  30596. LinesToStr([ // $mod.$main
  30597. '']));
  30598. end;
  30599. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30600. begin
  30601. WithTypeInfo:=true;
  30602. StartProgram(false);
  30603. Add([
  30604. 'type',
  30605. ' TCaption = string;',
  30606. ' TYesNo = boolean;',
  30607. ' TLetter = char;',
  30608. ' TFloat = double;',
  30609. ' TPtr = pointer;',
  30610. ' TShortInt = shortint;',
  30611. ' TByte = byte;',
  30612. ' TSmallInt = smallint;',
  30613. ' TWord = word;',
  30614. ' TInt32 = longint;',
  30615. ' TDWord = longword;',
  30616. ' TValue = jsvalue;',
  30617. 'var p: TPtr;',
  30618. 'begin',
  30619. ' p:=typeinfo(string);',
  30620. ' p:=typeinfo(tcaption);',
  30621. ' p:=typeinfo(boolean);',
  30622. ' p:=typeinfo(tyesno);',
  30623. ' p:=typeinfo(char);',
  30624. ' p:=typeinfo(tletter);',
  30625. ' p:=typeinfo(double);',
  30626. ' p:=typeinfo(tfloat);',
  30627. ' p:=typeinfo(pointer);',
  30628. ' p:=typeinfo(tptr);',
  30629. ' p:=typeinfo(shortint);',
  30630. ' p:=typeinfo(tshortint);',
  30631. ' p:=typeinfo(byte);',
  30632. ' p:=typeinfo(tbyte);',
  30633. ' p:=typeinfo(smallint);',
  30634. ' p:=typeinfo(tsmallint);',
  30635. ' p:=typeinfo(word);',
  30636. ' p:=typeinfo(tword);',
  30637. ' p:=typeinfo(longword);',
  30638. ' p:=typeinfo(tdword);',
  30639. ' p:=typeinfo(jsvalue);',
  30640. ' p:=typeinfo(tvalue);',
  30641. '']);
  30642. ConvertProgram;
  30643. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30644. LinesToStr([ // statements
  30645. 'this.p = null;',
  30646. '']),
  30647. LinesToStr([ // $mod.$main
  30648. '$mod.p = rtl.string;',
  30649. '$mod.p = rtl.string;',
  30650. '$mod.p = rtl.boolean;',
  30651. '$mod.p = rtl.boolean;',
  30652. '$mod.p = rtl.char;',
  30653. '$mod.p = rtl.char;',
  30654. '$mod.p = rtl.double;',
  30655. '$mod.p = rtl.double;',
  30656. '$mod.p = rtl.pointer;',
  30657. '$mod.p = rtl.pointer;',
  30658. '$mod.p = rtl.shortint;',
  30659. '$mod.p = rtl.shortint;',
  30660. '$mod.p = rtl.byte;',
  30661. '$mod.p = rtl.byte;',
  30662. '$mod.p = rtl.smallint;',
  30663. '$mod.p = rtl.smallint;',
  30664. '$mod.p = rtl.word;',
  30665. '$mod.p = rtl.word;',
  30666. '$mod.p = rtl.longword;',
  30667. '$mod.p = rtl.longword;',
  30668. '$mod.p = rtl.jsvalue;',
  30669. '$mod.p = rtl.jsvalue;',
  30670. '']));
  30671. end;
  30672. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30673. begin
  30674. WithTypeInfo:=true;
  30675. StartProgram(false);
  30676. Add([
  30677. 'type',
  30678. ' TCaption = type string;',
  30679. ' TYesNo = type boolean;',
  30680. ' TLetter = type char;',
  30681. ' TFloat = type double;',
  30682. ' TPtr = type pointer;',
  30683. ' TShortInt = type shortint;',
  30684. ' TByte = type byte;',
  30685. ' TSmallInt = type smallint;',
  30686. ' TWord = type word;',
  30687. ' TInt32 = type longint;',
  30688. ' TDWord = type longword;',
  30689. ' TValue = type jsvalue;',
  30690. ' TAliasValue = type TValue;',
  30691. 'var',
  30692. ' p: TPtr;',
  30693. ' a: TAliasValue;',
  30694. 'begin',
  30695. ' p:=typeinfo(tcaption);',
  30696. ' p:=typeinfo(tyesno);',
  30697. ' p:=typeinfo(tletter);',
  30698. ' p:=typeinfo(tfloat);',
  30699. ' p:=typeinfo(tptr);',
  30700. ' p:=typeinfo(tshortint);',
  30701. ' p:=typeinfo(tbyte);',
  30702. ' p:=typeinfo(tsmallint);',
  30703. ' p:=typeinfo(tword);',
  30704. ' p:=typeinfo(tdword);',
  30705. ' p:=typeinfo(tvalue);',
  30706. ' p:=typeinfo(taliasvalue);',
  30707. ' p:=typeinfo(a);',
  30708. '']);
  30709. ConvertProgram;
  30710. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30711. LinesToStr([ // statements
  30712. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30713. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30714. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30715. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30716. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30717. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30718. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30719. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30720. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30721. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30722. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30723. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30724. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30725. 'this.p = null;',
  30726. 'this.a = undefined;',
  30727. '']),
  30728. LinesToStr([ // $mod.$main
  30729. '$mod.p = $mod.$rtti["TCaption"];',
  30730. '$mod.p = $mod.$rtti["TYesNo"];',
  30731. '$mod.p = $mod.$rtti["TLetter"];',
  30732. '$mod.p = $mod.$rtti["TFloat"];',
  30733. '$mod.p = $mod.$rtti["TPtr"];',
  30734. '$mod.p = $mod.$rtti["TShortInt"];',
  30735. '$mod.p = $mod.$rtti["TByte"];',
  30736. '$mod.p = $mod.$rtti["TSmallInt"];',
  30737. '$mod.p = $mod.$rtti["TWord"];',
  30738. '$mod.p = $mod.$rtti["TDWord"];',
  30739. '$mod.p = $mod.$rtti["TValue"];',
  30740. '$mod.p = $mod.$rtti["TAliasValue"];',
  30741. '$mod.p = $mod.$rtti["TAliasValue"];',
  30742. '']));
  30743. end;
  30744. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30745. begin
  30746. WithTypeInfo:=true;
  30747. StartProgram(false);
  30748. Add('procedure DoIt;');
  30749. Add('type');
  30750. Add(' integer = longint;');
  30751. Add(' TPoint = record');
  30752. Add(' x,y: integer;');
  30753. Add(' end;');
  30754. Add('var p: pointer;');
  30755. Add('begin');
  30756. Add(' p:=typeinfo(tpoint);');
  30757. Add('end;');
  30758. Add('begin');
  30759. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30760. ConvertProgram;
  30761. end;
  30762. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30763. begin
  30764. WithTypeInfo:=true;
  30765. StartProgram(true,[supTypeInfo]);
  30766. Add([
  30767. '{$modeswitch externalclass}',
  30768. 'type',
  30769. ' TFlag = (up,down);',
  30770. ' TFlags = set of TFlag;',
  30771. 'var',
  30772. ' ti: TTypeInfo;',
  30773. ' tiInt: TTypeInfoInteger;',
  30774. ' tiEnum: TTypeInfoEnum;',
  30775. ' tiSet: TTypeInfoSet;',
  30776. 'begin',
  30777. ' ti:=typeinfo(string);',
  30778. ' ti:=typeinfo(boolean);',
  30779. ' ti:=typeinfo(char);',
  30780. ' ti:=typeinfo(double);',
  30781. ' tiInt:=typeinfo(shortint);',
  30782. ' tiInt:=typeinfo(byte);',
  30783. ' tiInt:=typeinfo(smallint);',
  30784. ' tiInt:=typeinfo(word);',
  30785. ' tiInt:=typeinfo(longint);',
  30786. ' tiInt:=typeinfo(longword);',
  30787. ' ti:=typeinfo(jsvalue);',
  30788. ' tiEnum:=typeinfo(tflag);',
  30789. ' tiSet:=typeinfo(tflags);']);
  30790. ConvertProgram;
  30791. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30792. LinesToStr([ // statements
  30793. 'this.TFlag = {',
  30794. ' "0": "up",',
  30795. ' up: 0,',
  30796. ' "1": "down",',
  30797. ' down: 1',
  30798. '};',
  30799. 'this.$rtti.$Enum("TFlag", {',
  30800. ' minvalue: 0,',
  30801. ' maxvalue: 1,',
  30802. ' ordtype: 1,',
  30803. ' enumtype: this.TFlag',
  30804. '});',
  30805. 'this.$rtti.$Set("TFlags", {',
  30806. ' comptype: this.$rtti["TFlag"]',
  30807. '});',
  30808. 'this.ti = null;',
  30809. 'this.tiInt = null;',
  30810. 'this.tiEnum = null;',
  30811. 'this.tiSet = null;',
  30812. '']),
  30813. LinesToStr([ // $mod.$main
  30814. '$mod.ti = rtl.string;',
  30815. '$mod.ti = rtl.boolean;',
  30816. '$mod.ti = rtl.char;',
  30817. '$mod.ti = rtl.double;',
  30818. '$mod.tiInt = rtl.shortint;',
  30819. '$mod.tiInt = rtl.byte;',
  30820. '$mod.tiInt = rtl.smallint;',
  30821. '$mod.tiInt = rtl.word;',
  30822. '$mod.tiInt = rtl.longint;',
  30823. '$mod.tiInt = rtl.longword;',
  30824. '$mod.ti = rtl.jsvalue;',
  30825. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30826. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30827. '']));
  30828. end;
  30829. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30830. begin
  30831. WithTypeInfo:=true;
  30832. StartProgram(true,[supTypeInfo]);
  30833. Add('{$modeswitch externalclass}');
  30834. Add('type');
  30835. Add(' TStaticArr = array[boolean] of string;');
  30836. Add(' TDynArr = array of string;');
  30837. Add(' TProc = procedure;');
  30838. Add(' TMethod = procedure of object;');
  30839. Add('var');
  30840. Add(' StaticArray: TStaticArr;');
  30841. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30842. Add(' DynArray: TDynArr;');
  30843. Add(' tiDynArray: TTypeInfoDynArray;');
  30844. Add(' ProcVar: TProc;');
  30845. Add(' tiProcVar: TTypeInfoProcVar;');
  30846. Add(' MethodVar: TMethod;');
  30847. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30848. Add('begin');
  30849. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30850. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30851. Add(' tiDynArray:=typeinfo(DynArray);');
  30852. Add(' tiDynArray:=typeinfo(TDynArr);');
  30853. Add(' tiProcVar:=typeinfo(ProcVar);');
  30854. Add(' tiProcVar:=typeinfo(TProc);');
  30855. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30856. Add(' tiMethodVar:=typeinfo(TMethod);');
  30857. ConvertProgram;
  30858. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30859. LinesToStr([ // statements
  30860. 'this.$rtti.$StaticArray("TStaticArr", {',
  30861. ' dims: [2],',
  30862. ' eltype: rtl.string',
  30863. '});',
  30864. 'this.$rtti.$DynArray("TDynArr", {',
  30865. ' eltype: rtl.string',
  30866. '});',
  30867. 'this.$rtti.$ProcVar("TProc", {',
  30868. ' procsig: rtl.newTIProcSig([])',
  30869. '});',
  30870. 'this.$rtti.$MethodVar("TMethod", {',
  30871. ' procsig: rtl.newTIProcSig([]),',
  30872. ' methodkind: 0',
  30873. '});',
  30874. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30875. 'this.tiStaticArray = null;',
  30876. 'this.DynArray = [];',
  30877. 'this.tiDynArray = null;',
  30878. 'this.ProcVar = null;',
  30879. 'this.tiProcVar = null;',
  30880. 'this.MethodVar = null;',
  30881. 'this.tiMethodVar = null;',
  30882. '']),
  30883. LinesToStr([ // $mod.$main
  30884. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30885. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30886. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30887. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30888. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30889. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30890. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30891. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30892. '']));
  30893. end;
  30894. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30895. begin
  30896. WithTypeInfo:=true;
  30897. StartProgram(true,[supTypeInfo]);
  30898. Add('{$modeswitch externalclass}');
  30899. Add('type');
  30900. Add(' TRec = record end;');
  30901. // ToDo: ^TRec
  30902. Add(' TObject = class end;');
  30903. Add(' TClass = class of tobject;');
  30904. Add('var');
  30905. Add(' Rec: trec;');
  30906. Add(' tiRecord: ttypeinforecord;');
  30907. Add(' Obj: tobject;');
  30908. Add(' tiClass: ttypeinfoclass;');
  30909. Add(' aClass: tclass;');
  30910. Add(' tiClassRef: ttypeinfoclassref;');
  30911. // ToDo: ^TRec
  30912. Add(' tiPointer: ttypeinfopointer;');
  30913. Add('begin');
  30914. Add(' tirecord:=typeinfo(trec);');
  30915. Add(' tirecord:=typeinfo(trec);');
  30916. Add(' ticlass:=typeinfo(obj);');
  30917. Add(' ticlass:=typeinfo(tobject);');
  30918. Add(' ticlass:=typeinfo(aclass);');
  30919. Add(' ticlassref:=typeinfo(tclass);');
  30920. ConvertProgram;
  30921. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30922. LinesToStr([ // statements
  30923. 'rtl.recNewT(this, "TRec", function () {',
  30924. ' this.$eq = function (b) {',
  30925. ' return true;',
  30926. ' };',
  30927. ' this.$assign = function (s) {',
  30928. ' return this;',
  30929. ' };',
  30930. ' $mod.$rtti.$Record("TRec", {});',
  30931. '});',
  30932. 'rtl.createClass(this, "TObject", null, function () {',
  30933. ' this.$init = function () {',
  30934. ' };',
  30935. ' this.$final = function () {',
  30936. ' };',
  30937. '});',
  30938. 'this.$rtti.$ClassRef("TClass", {',
  30939. ' instancetype: this.$rtti["TObject"]',
  30940. '});',
  30941. 'this.Rec = this.TRec.$new();',
  30942. 'this.tiRecord = null;',
  30943. 'this.Obj = null;',
  30944. 'this.tiClass = null;',
  30945. 'this.aClass = null;',
  30946. 'this.tiClassRef = null;',
  30947. 'this.tiPointer = null;',
  30948. '']),
  30949. LinesToStr([ // $mod.$main
  30950. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30951. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30952. '$mod.tiClass = $mod.Obj.$rtti;',
  30953. '$mod.tiClass = $mod.$rtti["TObject"];',
  30954. '$mod.tiClass = $mod.aClass.$rtti;',
  30955. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30956. '']));
  30957. end;
  30958. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30959. begin
  30960. WithTypeInfo:=true;
  30961. StartProgram(true,[supTypeInfo]);
  30962. Add([
  30963. '{$modeswitch externalclass}',
  30964. 'type',
  30965. ' TClass = class of tobject;',
  30966. ' TObject = class',
  30967. ' function MyClass: TClass;',
  30968. ' class function ClassType: TClass;',
  30969. ' end;',
  30970. 'function TObject.MyClass: TClass;',
  30971. 'var t: TTypeInfoClass;',
  30972. 'begin',
  30973. ' t:=TypeInfo(Self);',
  30974. ' t:=TypeInfo(Result);',
  30975. ' t:=TypeInfo(TObject);',
  30976. 'end;',
  30977. 'class function TObject.ClassType: TClass;',
  30978. 'var t: TTypeInfoClass;',
  30979. 'begin',
  30980. ' t:=TypeInfo(Self);',
  30981. ' t:=TypeInfo(Result);',
  30982. 'end;',
  30983. 'var',
  30984. ' Obj: TObject;',
  30985. ' t: TTypeInfoClass;',
  30986. 'begin',
  30987. ' t:=TypeInfo(TObject.ClassType);',
  30988. ' t:=TypeInfo(Obj.ClassType);',
  30989. ' t:=TypeInfo(Obj.MyClass);',
  30990. '']);
  30991. ConvertProgram;
  30992. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30993. LinesToStr([ // statements
  30994. 'this.$rtti.$Class("TObject");',
  30995. 'this.$rtti.$ClassRef("TClass", {',
  30996. ' instancetype: this.$rtti["TObject"]',
  30997. '});',
  30998. 'rtl.createClass(this, "TObject", null, function () {',
  30999. ' this.$init = function () {',
  31000. ' };',
  31001. ' this.$final = function () {',
  31002. ' };',
  31003. ' this.MyClass = function () {',
  31004. ' var Result = null;',
  31005. ' var t = null;',
  31006. ' t = this.$rtti;',
  31007. ' t = Result.$rtti;',
  31008. ' t = $mod.$rtti["TObject"];',
  31009. ' return Result;',
  31010. ' };',
  31011. ' this.ClassType = function () {',
  31012. ' var Result = null;',
  31013. ' var t = null;',
  31014. ' t = this.$rtti;',
  31015. ' t = Result.$rtti;',
  31016. ' return Result;',
  31017. ' };',
  31018. '});',
  31019. 'this.Obj = null;',
  31020. 'this.t = null;',
  31021. '']),
  31022. LinesToStr([ // $mod.$main
  31023. '$mod.t = $mod.TObject.ClassType().$rtti;',
  31024. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  31025. '$mod.t = $mod.Obj.MyClass().$rtti;',
  31026. '']));
  31027. end;
  31028. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  31029. begin
  31030. WithTypeInfo:=true;
  31031. AddModuleWithIntfImplSrc('typinfo.pas',
  31032. LinesToStr([
  31033. '{$modeswitch externalclass}',
  31034. 'type',
  31035. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  31036. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  31037. '']),
  31038. '');
  31039. AddModuleWithIntfImplSrc('unit2.pas',
  31040. LinesToStr([
  31041. 'uses typinfo;',
  31042. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  31043. 'procedure DoPtr(p: PTypeInfo);',
  31044. 'procedure DoInfo(t: TTypeInfo);',
  31045. 'procedure DoInt(t: TTypeInfoInteger);',
  31046. '']),
  31047. LinesToStr([
  31048. 'procedure DoPtr(p: PTypeInfo);',
  31049. 'begin end;',
  31050. 'procedure DoInfo(t: TTypeInfo);',
  31051. 'begin end;',
  31052. 'procedure DoInt(t: TTypeInfoInteger);',
  31053. 'begin end;',
  31054. '']));
  31055. StartUnit(true);
  31056. Add([
  31057. 'interface',
  31058. 'uses unit2;', // does not use unit typinfo
  31059. 'implementation',
  31060. 'var',
  31061. ' i: byte;',
  31062. ' p: pointer;',
  31063. ' t: PTypeInfo;',
  31064. 'initialization',
  31065. ' p:=typeinfo(i);',
  31066. ' t:=typeinfo(i);',
  31067. ' if p=t then ;',
  31068. ' if p=typeinfo(i) then ;',
  31069. ' if typeinfo(i)=p then ;',
  31070. ' if t=typeinfo(i) then ;',
  31071. ' if typeinfo(i)=t then ;',
  31072. ' DoPtr(p);',
  31073. ' DoPtr(t);',
  31074. ' DoPtr(typeinfo(i));',
  31075. ' DoInfo(p);',
  31076. ' DoInfo(t);',
  31077. ' DoInfo(typeinfo(i));',
  31078. ' DoInt(typeinfo(i));',
  31079. '']);
  31080. ConvertUnit;
  31081. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  31082. LinesToStr([ // statements
  31083. 'var $impl = $mod.$impl;',
  31084. '']),
  31085. LinesToStr([ // this.$init
  31086. '$impl.p = rtl.byte;',
  31087. '$impl.t = rtl.byte;',
  31088. 'if ($impl.p === $impl.t) ;',
  31089. 'if ($impl.p === rtl.byte) ;',
  31090. 'if (rtl.byte === $impl.p) ;',
  31091. 'if ($impl.t === rtl.byte) ;',
  31092. 'if (rtl.byte === $impl.t) ;',
  31093. 'pas.unit2.DoPtr($impl.p);',
  31094. 'pas.unit2.DoPtr($impl.t);',
  31095. 'pas.unit2.DoPtr(rtl.byte);',
  31096. 'pas.unit2.DoInfo($impl.p);',
  31097. 'pas.unit2.DoInfo($impl.t);',
  31098. 'pas.unit2.DoInfo(rtl.byte);',
  31099. 'pas.unit2.DoInt(rtl.byte);',
  31100. '']),
  31101. LinesToStr([ // implementation
  31102. '$impl.i = 0;',
  31103. '$impl.p = null;',
  31104. '$impl.t = null;',
  31105. '']) );
  31106. end;
  31107. procedure TTestModule.TestRTTI_Interface_Corba;
  31108. begin
  31109. WithTypeInfo:=true;
  31110. StartProgram(true,[supTypeInfo]);
  31111. Add([
  31112. '{$interfaces corba}',
  31113. '{$modeswitch externalclass}',
  31114. 'type',
  31115. ' IUnknown = interface',
  31116. ' end;',
  31117. ' IBird = interface',
  31118. ' function GetItem: longint;',
  31119. ' procedure SetItem(Value: longint);',
  31120. ' property Item: longint read GetItem write SetItem;',
  31121. ' end;',
  31122. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  31123. 'var',
  31124. ' i: IBird;',
  31125. ' t: TTypeInfoInterface;',
  31126. 'begin',
  31127. ' t:=TypeInfo(IBird);',
  31128. ' t:=TypeInfo(i);',
  31129. ' DoIt(t);',
  31130. ' DoIt(TypeInfo(IBird));',
  31131. '']);
  31132. ConvertProgram;
  31133. CheckSource('TestRTTI_Interface_Corba',
  31134. LinesToStr([ // statements
  31135. 'rtl.createInterface(',
  31136. ' this,',
  31137. ' "IUnknown",',
  31138. ' "{B92D5841-758A-322B-B800-000000000000}",',
  31139. ' [],',
  31140. ' null,',
  31141. ' function () {',
  31142. ' }',
  31143. ');',
  31144. 'rtl.createInterface(',
  31145. ' this,',
  31146. ' "IBird",',
  31147. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  31148. ' ["GetItem", "SetItem"],',
  31149. ' null,',
  31150. ' function () {',
  31151. ' var $r = this.$rtti;',
  31152. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31153. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31154. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31155. ' }',
  31156. ');',
  31157. 'this.DoIt = function (t) {',
  31158. '}; ',
  31159. 'this.i = null;',
  31160. 'this.t = null;',
  31161. '']),
  31162. LinesToStr([ // $mod.$main
  31163. '$mod.t = $mod.$rtti["IBird"];',
  31164. '$mod.t = $mod.i.$rtti;',
  31165. '$mod.DoIt($mod.t);',
  31166. '$mod.DoIt($mod.$rtti["IBird"]);',
  31167. '']));
  31168. end;
  31169. procedure TTestModule.TestRTTI_Interface_COM;
  31170. begin
  31171. WithTypeInfo:=true;
  31172. StartProgram(true,[supTypeInfo]);
  31173. Add([
  31174. '{$interfaces com}',
  31175. '{$modeswitch externalclass}',
  31176. 'type',
  31177. ' TGuid = record end;',
  31178. ' integer = longint;',
  31179. ' IUnknown = interface',
  31180. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  31181. ' function _AddRef: Integer;',
  31182. ' function _Release: Integer;',
  31183. ' end;',
  31184. ' IBird = interface',
  31185. ' function GetItem: longint;',
  31186. ' procedure SetItem(Value: longint);',
  31187. ' property Item: longint read GetItem write SetItem;',
  31188. ' end;',
  31189. 'var',
  31190. ' i: IBird;',
  31191. ' t: TTypeInfoInterface;',
  31192. 'begin',
  31193. ' t:=TypeInfo(IBird);',
  31194. ' t:=TypeInfo(i);',
  31195. '']);
  31196. ConvertProgram;
  31197. CheckSource('TestRTTI_Interface_COM',
  31198. LinesToStr([ // statements
  31199. 'rtl.recNewT(this, "TGuid", function () {',
  31200. ' this.$eq = function (b) {',
  31201. ' return true;',
  31202. ' };',
  31203. ' this.$assign = function (s) {',
  31204. ' return this;',
  31205. ' };',
  31206. ' $mod.$rtti.$Record("TGuid", {});',
  31207. '});',
  31208. 'rtl.createInterface(',
  31209. ' this,',
  31210. ' "IUnknown",',
  31211. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  31212. ' ["QueryInterface", "_AddRef", "_Release"],',
  31213. ' null,',
  31214. ' function () {',
  31215. ' this.$kind = "com";',
  31216. ' var $r = this.$rtti;',
  31217. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  31218. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  31219. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  31220. ' }',
  31221. ');',
  31222. 'rtl.createInterface(',
  31223. ' this,',
  31224. ' "IBird",',
  31225. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  31226. ' ["GetItem", "SetItem"],',
  31227. ' this.IUnknown,',
  31228. ' function () {',
  31229. ' var $r = this.$rtti;',
  31230. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31231. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31232. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31233. ' }',
  31234. ');',
  31235. 'this.i = null;',
  31236. 'this.t = null;',
  31237. '']),
  31238. LinesToStr([ // $mod.$main
  31239. '$mod.t = $mod.$rtti["IBird"];',
  31240. '$mod.t = $mod.i.$rtti;',
  31241. '']));
  31242. end;
  31243. procedure TTestModule.TestRTTI_ClassHelper;
  31244. begin
  31245. WithTypeInfo:=true;
  31246. StartProgram(true,[supTypeInfo]);
  31247. Add([
  31248. '{$interfaces com}',
  31249. '{$modeswitch externalclass}',
  31250. 'type',
  31251. ' TObject = class',
  31252. ' end;',
  31253. ' THelper = class helper for TObject',
  31254. ' published',
  31255. ' function GetItem: longint;',
  31256. ' property Item: longint read GetItem;',
  31257. ' end;',
  31258. 'function THelper.GetItem: longint;',
  31259. 'begin',
  31260. 'end;',
  31261. 'var',
  31262. ' t: TTypeInfoHelper;',
  31263. 'begin',
  31264. ' t:=TypeInfo(THelper);',
  31265. '']);
  31266. ConvertProgram;
  31267. CheckSource('TestRTTI_ClassHelper',
  31268. LinesToStr([ // statements
  31269. 'rtl.createClass(this, "TObject", null, function () {',
  31270. ' this.$init = function () {',
  31271. ' };',
  31272. ' this.$final = function () {',
  31273. ' };',
  31274. '});',
  31275. 'rtl.createHelper(this, "THelper", null, function () {',
  31276. ' this.GetItem = function () {',
  31277. ' var Result = 0;',
  31278. ' return Result;',
  31279. ' };',
  31280. ' var $r = this.$rtti;',
  31281. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31282. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  31283. '});',
  31284. 'this.t = null;',
  31285. '']),
  31286. LinesToStr([ // $mod.$main
  31287. '$mod.t = $mod.$rtti["THelper"];',
  31288. '']));
  31289. end;
  31290. procedure TTestModule.TestRTTI_ExternalClass;
  31291. begin
  31292. WithTypeInfo:=true;
  31293. StartProgram(true,[supTypeInfo]);
  31294. Add([
  31295. '{$modeswitch externalclass}',
  31296. 'type',
  31297. ' TJSObject = class external name ''Object''',
  31298. ' end;',
  31299. ' TJSArray = class external name ''Array'' (TJSObject)',
  31300. ' end;',
  31301. 'var',
  31302. ' p: Pointer;',
  31303. ' tc: TTypeInfoExtClass;',
  31304. 'begin',
  31305. ' p:=typeinfo(TJSArray);']);
  31306. ConvertProgram;
  31307. CheckSource('TestRTTI_ExternalClass',
  31308. LinesToStr([ // statements
  31309. 'this.$rtti.$ExtClass("TJSObject", {',
  31310. ' jsclass: "Object"',
  31311. '});',
  31312. 'this.$rtti.$ExtClass("TJSArray", {',
  31313. ' ancestor: this.$rtti["TJSObject"],',
  31314. ' jsclass: "Array"',
  31315. '});',
  31316. 'this.p = null;',
  31317. 'this.tc = null;',
  31318. '']),
  31319. LinesToStr([ // $mod.$main
  31320. '$mod.p = $mod.$rtti["TJSArray"];',
  31321. '']));
  31322. end;
  31323. procedure TTestModule.TestRTTI_Unit;
  31324. begin
  31325. WithTypeInfo:=true;
  31326. AddModuleWithIntfImplSrc('unit2.pas',
  31327. LinesToStr([
  31328. '{$mode delphi}',
  31329. 'type',
  31330. ' TWordArray = array of word;',
  31331. ' TArray<T> = array of T;',
  31332. '']),
  31333. '');
  31334. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  31335. Add([
  31336. '{$mode delphi}',
  31337. 'interface',
  31338. 'uses unit2;',
  31339. 'type',
  31340. ' IBird = interface',
  31341. ' function Swoop: TWordArray;',
  31342. ' function Glide: TArray<word>;',
  31343. ' end;',
  31344. 'procedure Fly;',
  31345. 'implementation',
  31346. 'procedure Fly;',
  31347. 'var',
  31348. ' ta: tTypeInfoDynArray;',
  31349. ' ti: tTypeInfoInterface;',
  31350. 'begin',
  31351. ' ta:=typeinfo(TWordArray);',
  31352. ' ta:=typeinfo(TArray<word>);',
  31353. ' ti:=typeinfo(IBird);',
  31354. 'end;',
  31355. '']);
  31356. ConvertUnit;
  31357. CheckSource('TestRTTI_ExternalClass',
  31358. LinesToStr([ // statements
  31359. 'rtl.createInterface(',
  31360. ' this,',
  31361. ' "IBird",',
  31362. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  31363. ' ["Swoop", "Glide"],',
  31364. ' pas.system.IUnknown,',
  31365. ' function () {',
  31366. ' var $r = this.$rtti;',
  31367. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  31368. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  31369. ' }',
  31370. ');',
  31371. 'this.Fly = function () {',
  31372. ' var ta = null;',
  31373. ' var ti = null;',
  31374. ' ta = pas.unit2.$rtti["TWordArray"];',
  31375. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  31376. ' ti = $mod.$rtti["IBird"];',
  31377. '};',
  31378. '']),
  31379. LinesToStr([ // $mod.$main
  31380. '']));
  31381. end;
  31382. procedure TTestModule.TestResourcestringProgram;
  31383. begin
  31384. AddModuleWithIntfImplSrc('unit2.pas',
  31385. LinesToStr([
  31386. 'resourcestring Title = ''Nice'';',
  31387. '']),
  31388. '');
  31389. StartProgram(true);
  31390. Add([
  31391. 'uses unit2;',
  31392. 'const Bar = ''bar'';',
  31393. 'resourcestring',
  31394. ' Red = ''red'';',
  31395. ' Foobar = ''fOo''+bar;',
  31396. 'var s: string;',
  31397. ' c: char;',
  31398. 'begin',
  31399. ' s:=red;',
  31400. ' s:=test1.red;',
  31401. ' s:=Title;',
  31402. ' c:=red[1];',
  31403. ' c:=test1.red[2];',
  31404. ' if red=foobar then ;',
  31405. ' if red[3]=red[4] then ;']);
  31406. ConvertProgram;
  31407. CheckSource('TestResourcestringProgram',
  31408. LinesToStr([ // statements
  31409. 'this.Bar = "bar";',
  31410. 'this.s = "";',
  31411. 'this.c = "";',
  31412. '$mod.$resourcestrings = {',
  31413. ' Red: {',
  31414. ' org: "red"',
  31415. ' },',
  31416. ' Foobar: {',
  31417. ' org: "fOobar"',
  31418. ' }',
  31419. '};',
  31420. '']),
  31421. LinesToStr([ // $mod.$main
  31422. '$mod.s = rtl.getResStr($mod, "Red");',
  31423. '$mod.s = rtl.getResStr($mod, "Red");',
  31424. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31425. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  31426. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  31427. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  31428. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  31429. '']));
  31430. end;
  31431. procedure TTestModule.TestResourcestringUnit;
  31432. begin
  31433. AddModuleWithIntfImplSrc('unit2.pas',
  31434. LinesToStr([
  31435. 'resourcestring Title = ''Nice'';',
  31436. '']),
  31437. '');
  31438. StartUnit(true);
  31439. Add([
  31440. 'interface',
  31441. 'uses unit2;',
  31442. 'const Red = ''rEd'';',
  31443. 'resourcestring',
  31444. ' Blue = ''blue'';',
  31445. ' NotRed = ''not''+Red;',
  31446. 'var s: string;',
  31447. 'implementation',
  31448. 'resourcestring',
  31449. ' ImplGreen = ''green'';',
  31450. 'initialization',
  31451. ' s:=blue+ImplGreen;',
  31452. ' s:=test1.blue+test1.implgreen;',
  31453. ' s:=blue[1]+implgreen[2];',
  31454. ' s:=Title;',
  31455. '']);
  31456. ConvertUnit;
  31457. CheckSource('TestResourcestringUnit',
  31458. LinesToStr([ // statements
  31459. 'this.Red = "rEd";',
  31460. 'this.s = "";',
  31461. '$mod.$resourcestrings = {',
  31462. ' Blue: {',
  31463. ' org: "blue"',
  31464. ' },',
  31465. ' NotRed: {',
  31466. ' org: "notrEd"',
  31467. ' },',
  31468. ' ImplGreen: {',
  31469. ' org: "green"',
  31470. ' }',
  31471. '};',
  31472. '']),
  31473. LinesToStr([ // $mod.$main
  31474. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31475. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31476. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31477. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31478. '']));
  31479. end;
  31480. procedure TTestModule.TestResourcestringImplementation;
  31481. begin
  31482. StartUnit(false);
  31483. Add([
  31484. 'interface',
  31485. 'implementation',
  31486. 'resourcestring',
  31487. ' ImplRed = ''red'';']);
  31488. ConvertUnit;
  31489. CheckSource('TestResourcestringImplementation',
  31490. LinesToStr([ // intf statements
  31491. 'var $impl = $mod.$impl;']),
  31492. LinesToStr([ // $mod.$init
  31493. '']),
  31494. LinesToStr([ // impl statements
  31495. '$mod.$resourcestrings = {',
  31496. ' ImplRed: {',
  31497. ' org: "red"',
  31498. ' }',
  31499. '};',
  31500. '']));
  31501. end;
  31502. procedure TTestModule.TestAttributes_Members;
  31503. begin
  31504. WithTypeInfo:=true;
  31505. StartProgram(false);
  31506. Add([
  31507. '{$modeswitch PrefixedAttributes}',
  31508. 'type',
  31509. ' TObject = class',
  31510. ' constructor Create;',
  31511. ' end;',
  31512. ' TCustomAttribute = class',
  31513. ' constructor Create(Id: word);',
  31514. ' end;',
  31515. ' [Missing]',
  31516. ' TBird = class',
  31517. ' published',
  31518. ' [Tcustom]',
  31519. ' FField: word;',
  31520. ' [tcustom(14)]',
  31521. ' property Size: word read FField;',
  31522. ' [Tcustom(15)]',
  31523. ' procedure Fly; virtual; abstract;',
  31524. ' end;',
  31525. ' TRec = record',
  31526. ' [Tcustom,tcustom(14)]',
  31527. ' Size: word;',
  31528. ' end;',
  31529. 'constructor TObject.Create; begin end;',
  31530. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31531. 'begin',
  31532. '']);
  31533. ConvertProgram;
  31534. CheckSource('TestAttributes_Members',
  31535. LinesToStr([ // statements
  31536. 'rtl.createClass(this, "TObject", null, function () {',
  31537. ' this.$init = function () {',
  31538. ' };',
  31539. ' this.$final = function () {',
  31540. ' };',
  31541. ' this.Create = function () {',
  31542. ' return this;',
  31543. ' };',
  31544. '});',
  31545. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31546. ' this.Create$1 = function (Id) {',
  31547. ' return this;',
  31548. ' };',
  31549. '});',
  31550. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31551. ' this.$init = function () {',
  31552. ' $mod.TObject.$init.call(this);',
  31553. ' this.FField = 0;',
  31554. ' };',
  31555. ' var $r = this.$rtti;',
  31556. ' $r.addField("FField", rtl.word, {',
  31557. ' attr: [$mod.TCustomAttribute, "Create"]',
  31558. ' });',
  31559. ' $r.addProperty(',
  31560. ' "Size",',
  31561. ' 0,',
  31562. ' rtl.word,',
  31563. ' "FField",',
  31564. ' "",',
  31565. ' {',
  31566. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31567. ' }',
  31568. ' );',
  31569. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31570. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31571. ' });',
  31572. '});',
  31573. 'rtl.recNewT(this, "TRec", function () {',
  31574. ' this.Size = 0;',
  31575. ' this.$eq = function (b) {',
  31576. ' return this.Size === b.Size;',
  31577. ' };',
  31578. ' this.$assign = function (s) {',
  31579. ' this.Size = s.Size;',
  31580. ' return this;',
  31581. ' };',
  31582. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31583. ' $r.addField("Size", rtl.word, {',
  31584. ' attr: [',
  31585. ' $mod.TCustomAttribute,',
  31586. ' "Create",',
  31587. ' $mod.TCustomAttribute,',
  31588. ' "Create$1",',
  31589. ' [14]',
  31590. ' ]',
  31591. ' });',
  31592. '});',
  31593. '']),
  31594. LinesToStr([ // $mod.$main
  31595. '']));
  31596. end;
  31597. procedure TTestModule.TestAttributes_Types;
  31598. begin
  31599. WithTypeInfo:=true;
  31600. StartProgram(false);
  31601. Add([
  31602. '{$modeswitch PrefixedAttributes}',
  31603. 'type',
  31604. ' TObject = class',
  31605. ' constructor Create(Id: word);',
  31606. ' end;',
  31607. ' TCustomAttribute = class',
  31608. ' end;',
  31609. ' [TCustom(1)]',
  31610. ' TMyClass = class',
  31611. ' end;',
  31612. ' [TCustom(11)]',
  31613. ' TMyDescendant = class(TMyClass)',
  31614. ' end;',
  31615. ' [TCustom(2)]',
  31616. ' TRec = record',
  31617. ' end;',
  31618. ' [TCustom(3)]',
  31619. ' TInt = type word;',
  31620. 'constructor TObject.Create(Id: word);',
  31621. 'begin',
  31622. 'end;',
  31623. 'var p: pointer;',
  31624. 'begin',
  31625. ' p:=typeinfo(TMyClass);',
  31626. ' p:=typeinfo(TRec);',
  31627. ' p:=typeinfo(TInt);',
  31628. '']);
  31629. ConvertProgram;
  31630. CheckSource('TestAttributes_Types',
  31631. LinesToStr([ // statements
  31632. 'rtl.createClass(this, "TObject", null, function () {',
  31633. ' this.$init = function () {',
  31634. ' };',
  31635. ' this.$final = function () {',
  31636. ' };',
  31637. ' this.Create = function (Id) {',
  31638. ' return this;',
  31639. ' };',
  31640. '});',
  31641. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31642. '});',
  31643. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31644. ' var $r = this.$rtti;',
  31645. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31646. '});',
  31647. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31648. ' var $r = this.$rtti;',
  31649. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31650. '});',
  31651. 'rtl.recNewT(this, "TRec", function () {',
  31652. ' this.$eq = function (b) {',
  31653. ' return true;',
  31654. ' };',
  31655. ' this.$assign = function (s) {',
  31656. ' return this;',
  31657. ' };',
  31658. ' $mod.$rtti.$Record("TRec", {',
  31659. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31660. ' });',
  31661. '});',
  31662. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31663. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31664. '});',
  31665. 'this.p = null;',
  31666. '']),
  31667. LinesToStr([ // $mod.$main
  31668. '$mod.p = $mod.$rtti["TMyClass"];',
  31669. '$mod.p = $mod.$rtti["TRec"];',
  31670. '$mod.p = $mod.$rtti["TInt"];',
  31671. '']));
  31672. end;
  31673. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31674. begin
  31675. WithTypeInfo:=true;
  31676. StartProgram(false);
  31677. Add([
  31678. '{$modeswitch PrefixedAttributes}',
  31679. 'type',
  31680. ' TObject = class',
  31681. ' constructor Create;',
  31682. ' end;',
  31683. ' TCustomAttribute = class',
  31684. ' end;',
  31685. ' THelper = class helper for TCustomAttribute',
  31686. ' constructor Create(Id: word);',
  31687. ' end;',
  31688. ' [TCustom(3)]',
  31689. ' TMyInt = word;',
  31690. 'constructor TObject.Create; begin end;',
  31691. 'constructor THelper.Create(Id: word); begin end;',
  31692. 'begin',
  31693. ' if typeinfo(TMyInt)=nil then ;']);
  31694. ConvertProgram;
  31695. end;
  31696. procedure TTestModule.TestAttributes_InterfacesList;
  31697. begin
  31698. WithTypeInfo:=true;
  31699. StartProgram(false);
  31700. Add([
  31701. '{$mode Delphi}',
  31702. 'type',
  31703. ' TObject = class',
  31704. ' constructor Create;',
  31705. ' end;',
  31706. ' IInterface = interface end;',
  31707. ' TCustomAttribute = class',
  31708. ' end;',
  31709. ' Red = class(TCustomAttribute);',
  31710. ' Blue = class(TCustomAttribute);',
  31711. ' [Red]',
  31712. ' IBird<T> = interface',
  31713. ' procedure Fly;',
  31714. ' end;',
  31715. ' [Blue]',
  31716. ' IEagle = interface(IBird<Word>)',
  31717. ' procedure Dive;',
  31718. ' end;',
  31719. ' TAnt = class(TObject, IEagle)',
  31720. ' procedure Fly; virtual; abstract;',
  31721. ' procedure Dive; virtual; abstract;',
  31722. ' end;',
  31723. 'constructor TObject.Create;',
  31724. 'begin',
  31725. 'end;',
  31726. 'begin',
  31727. '']);
  31728. ConvertProgram;
  31729. CheckSource('TestAttributes_InterfacesList',
  31730. LinesToStr([ // statements
  31731. '$mod.$rtti.$Interface("IBird<System.Word>");',
  31732. 'rtl.createClass(this, "TObject", null, function () {',
  31733. ' this.$init = function () {',
  31734. ' };',
  31735. ' this.$final = function () {',
  31736. ' };',
  31737. ' this.Create = function () {',
  31738. ' return this;',
  31739. ' };',
  31740. '});',
  31741. 'rtl.createInterface(',
  31742. ' this,',
  31743. ' "IInterface",',
  31744. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  31745. ' [],',
  31746. ' null,',
  31747. ' function () {',
  31748. ' this.$kind = "com";',
  31749. ' }',
  31750. ');',
  31751. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31752. '});',
  31753. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  31754. '});',
  31755. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  31756. '});',
  31757. 'rtl.createInterface(',
  31758. ' this,',
  31759. ' "IBird$G1",',
  31760. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  31761. ' ["Fly"],',
  31762. ' this.IInterface,',
  31763. ' function () {',
  31764. ' var $r = this.$rtti;',
  31765. ' $r.addMethod("Fly", 0, []);',
  31766. ' $r.attr = [$mod.Red, "Create"];',
  31767. ' },',
  31768. ' "IBird<System.Word>"',
  31769. ');',
  31770. 'rtl.createInterface(',
  31771. ' this,',
  31772. ' "IEagle",',
  31773. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  31774. ' ["Dive"],',
  31775. ' this.IBird$G1,',
  31776. ' function () {',
  31777. ' var $r = this.$rtti;',
  31778. ' $r.addMethod("Dive", 0, []);',
  31779. ' $r.attr = [$mod.Blue, "Create"];',
  31780. ' }',
  31781. ');',
  31782. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  31783. ' rtl.addIntf(this, $mod.IEagle);',
  31784. '});',
  31785. '']),
  31786. LinesToStr([ // $mod.$main
  31787. '']));
  31788. end;
  31789. procedure TTestModule.TestAssert;
  31790. begin
  31791. StartProgram(false);
  31792. Add([
  31793. 'procedure DoIt;',
  31794. 'var',
  31795. ' b: boolean;',
  31796. ' s: string;',
  31797. 'begin',
  31798. ' {$Assertions on}',
  31799. ' Assert(b);',
  31800. 'end;',
  31801. 'begin',
  31802. ' DoIt;',
  31803. '']);
  31804. ConvertProgram;
  31805. CheckSource('TestAssert',
  31806. LinesToStr([ // statements
  31807. 'this.DoIt = function () {',
  31808. ' var b = false;',
  31809. ' var s = "";',
  31810. ' if (!b) throw "assert failed";',
  31811. '};',
  31812. '']),
  31813. LinesToStr([ // $mod.$main
  31814. '$mod.DoIt();',
  31815. '']));
  31816. end;
  31817. procedure TTestModule.TestAssert_SysUtils;
  31818. begin
  31819. AddModuleWithIntfImplSrc('SysUtils.pas',
  31820. LinesToStr([
  31821. 'type',
  31822. ' TObject = class',
  31823. ' constructor Create;',
  31824. ' end;',
  31825. ' EAssertionFailed = class',
  31826. ' constructor Create(s: string);',
  31827. ' end;',
  31828. '']),
  31829. LinesToStr([
  31830. 'constructor TObject.Create;',
  31831. 'begin end;',
  31832. 'constructor EAssertionFailed.Create(s: string);',
  31833. 'begin end;',
  31834. '']) );
  31835. StartProgram(true);
  31836. Add([
  31837. 'uses sysutils;',
  31838. 'procedure DoIt;',
  31839. 'var',
  31840. ' b: boolean;',
  31841. ' s: string;',
  31842. 'begin',
  31843. ' {$Assertions on}',
  31844. ' Assert(b);',
  31845. ' Assert(b,''msg'');',
  31846. 'end;',
  31847. 'begin',
  31848. ' DoIt;',
  31849. '']);
  31850. ConvertProgram;
  31851. CheckSource('TestAssert_SysUtils',
  31852. LinesToStr([ // statements
  31853. 'this.DoIt = function () {',
  31854. ' var b = false;',
  31855. ' var s = "";',
  31856. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31857. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31858. '};',
  31859. '']),
  31860. LinesToStr([ // $mod.$main
  31861. '$mod.DoIt();',
  31862. '']));
  31863. end;
  31864. procedure TTestModule.TestObjectChecks;
  31865. begin
  31866. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31867. StartProgram(false);
  31868. Add([
  31869. 'type',
  31870. ' TObject = class',
  31871. ' procedure DoIt;',
  31872. ' end;',
  31873. ' TClass = class of tobject;',
  31874. ' TBird = class',
  31875. ' end;',
  31876. ' TBirdClass = class of TBird;',
  31877. 'var',
  31878. ' o : TObject;',
  31879. ' c: TClass;',
  31880. ' b: TBird;',
  31881. ' bc: TBirdClass;',
  31882. 'procedure TObject.DoIt;',
  31883. 'begin',
  31884. ' b:=TBird(o);',
  31885. 'end;',
  31886. 'begin',
  31887. ' o.DoIt;',
  31888. ' b:=TBird(o);',
  31889. ' bc:=TBirdClass(c);',
  31890. '']);
  31891. ConvertProgram;
  31892. CheckSource('TestCheckMethodCall',
  31893. LinesToStr([ // statements
  31894. 'rtl.createClass(this, "TObject", null, function () {',
  31895. ' this.$init = function () {',
  31896. ' };',
  31897. ' this.$final = function () {',
  31898. ' };',
  31899. ' this.DoIt = function () {',
  31900. ' rtl.checkMethodCall(this,$mod.TObject);',
  31901. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31902. ' };',
  31903. '});',
  31904. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31905. '});',
  31906. 'this.o = null;',
  31907. 'this.c = null;',
  31908. 'this.b = null;',
  31909. 'this.bc = null;',
  31910. '']),
  31911. LinesToStr([ // $mod.$main
  31912. '$mod.o.DoIt();',
  31913. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31914. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31915. '']));
  31916. end;
  31917. procedure TTestModule.TestOverflowChecks_Int;
  31918. begin
  31919. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31920. StartProgram(false);
  31921. Add([
  31922. 'procedure DoIt;',
  31923. 'var',
  31924. ' b: byte;',
  31925. ' n: nativeint;',
  31926. ' u: nativeuint;',
  31927. ' c: currency;',
  31928. 'begin',
  31929. ' n:=n+n;',
  31930. ' n:=n-n;',
  31931. ' n:=n+b;',
  31932. ' n:=b-n;',
  31933. ' n:=n*n;',
  31934. ' n:=n*u;',
  31935. ' c:=c+b;',
  31936. ' c:=b+c;',
  31937. ' c:=c*b;',
  31938. ' c:=b*c;',
  31939. 'end;',
  31940. 'begin',
  31941. '']);
  31942. ConvertProgram;
  31943. CheckSource('TestOverflowChecks_Int',
  31944. LinesToStr([ // statements
  31945. 'this.DoIt = function () {',
  31946. ' var b = 0;',
  31947. ' var n = 0;',
  31948. ' var u = 0;',
  31949. ' var c = 0;',
  31950. ' n = rtl.oc(n + n);',
  31951. ' n = rtl.oc(n - n);',
  31952. ' n = rtl.oc(n + b);',
  31953. ' n = rtl.oc(b - n);',
  31954. ' n = rtl.oc(n * n);',
  31955. ' n = rtl.oc(n * u);',
  31956. ' c = rtl.oc(c + (b * 10000));',
  31957. ' c = rtl.oc((b * 10000) + c);',
  31958. ' c = rtl.oc(c * b);',
  31959. ' c = rtl.oc(b * c);',
  31960. '};',
  31961. '']),
  31962. LinesToStr([ // $mod.$main
  31963. '']));
  31964. end;
  31965. procedure TTestModule.TestRangeChecks_AssignInt;
  31966. begin
  31967. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31968. StartProgram(false);
  31969. Add([
  31970. '{$R+}',
  31971. 'var',
  31972. ' b: byte = 2;',
  31973. ' w: word = 3;',
  31974. 'procedure DoIt(p: byte);',
  31975. 'begin',
  31976. ' b:=w;',
  31977. ' b+=w;',
  31978. ' b:=1;',
  31979. 'end;',
  31980. '{$R-}',
  31981. 'procedure DoSome;',
  31982. 'begin',
  31983. ' DoIt(w);',
  31984. ' b:=w;',
  31985. ' b:=2;',
  31986. 'end;',
  31987. 'begin',
  31988. '{$R+}',
  31989. '']);
  31990. ConvertProgram;
  31991. CheckSource('TestRangeChecks_AssignInt',
  31992. LinesToStr([ // statements
  31993. 'this.b = 2;',
  31994. 'this.w = 3;',
  31995. 'this.DoIt = function (p) {',
  31996. ' rtl.rc(p, 0, 255);',
  31997. ' $mod.b = rtl.rc($mod.w,0,255);',
  31998. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31999. ' $mod.b = 1;',
  32000. '};',
  32001. 'this.DoSome = function () {',
  32002. ' $mod.DoIt($mod.w);',
  32003. ' $mod.b = $mod.w;',
  32004. ' $mod.b = 2;',
  32005. '};',
  32006. '']),
  32007. LinesToStr([ // $mod.$main
  32008. '']));
  32009. end;
  32010. procedure TTestModule.TestRangeChecks_AssignIntRange;
  32011. begin
  32012. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32013. StartProgram(false);
  32014. Add([
  32015. '{$R+}',
  32016. 'type Ten = 1..10;',
  32017. 'var',
  32018. ' b: Ten = 2;',
  32019. ' w: Ten = 3;',
  32020. 'procedure DoIt(p: Ten);',
  32021. 'begin',
  32022. ' b:=w;',
  32023. ' b+=w;',
  32024. ' b:=1;',
  32025. 'end;',
  32026. '{$R-}',
  32027. 'procedure DoSome;',
  32028. 'begin',
  32029. ' DoIt(w);',
  32030. ' b:=w;',
  32031. ' b:=2;',
  32032. 'end;',
  32033. 'begin',
  32034. '{$R+}',
  32035. '']);
  32036. ConvertProgram;
  32037. CheckSource('TestRangeChecks_AssignIntRange',
  32038. LinesToStr([ // statements
  32039. 'this.b = 2;',
  32040. 'this.w = 3;',
  32041. 'this.DoIt = function (p) {',
  32042. ' rtl.rc(p, 1, 10);',
  32043. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  32044. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  32045. ' $mod.b = 1;',
  32046. '};',
  32047. 'this.DoSome = function () {',
  32048. ' $mod.DoIt($mod.w);',
  32049. ' $mod.b = $mod.w;',
  32050. ' $mod.b = 2;',
  32051. '};',
  32052. '']),
  32053. LinesToStr([ // $mod.$main
  32054. '']));
  32055. end;
  32056. procedure TTestModule.TestRangeChecks_AssignEnum;
  32057. begin
  32058. StartProgram(false);
  32059. Add([
  32060. '{$R+}',
  32061. 'type TEnum = (red,green);',
  32062. 'var',
  32063. ' e: TEnum = red;',
  32064. 'procedure DoIt(p: TEnum);',
  32065. 'begin',
  32066. ' e:=p;',
  32067. ' p:=TEnum(0);',
  32068. ' p:=succ(e);',
  32069. 'end;',
  32070. '{$R-}',
  32071. 'procedure DoSome;',
  32072. 'begin',
  32073. ' DoIt(e);',
  32074. ' e:=TEnum(1);',
  32075. ' e:=pred(e);',
  32076. 'end;',
  32077. 'begin',
  32078. '{$R+}',
  32079. '']);
  32080. ConvertProgram;
  32081. CheckSource('TestRangeChecks_AssignEnum',
  32082. LinesToStr([ // statements
  32083. 'this.TEnum = {',
  32084. ' "0": "red",',
  32085. ' red: 0,',
  32086. ' "1": "green",',
  32087. ' green: 1',
  32088. '};',
  32089. 'this.e = this.TEnum.red;',
  32090. 'this.DoIt = function (p) {',
  32091. ' rtl.rc(p, 0, 1);',
  32092. ' $mod.e = rtl.rc(p, 0, 1);',
  32093. ' p = 0;',
  32094. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32095. '};',
  32096. 'this.DoSome = function () {',
  32097. ' $mod.DoIt($mod.e);',
  32098. ' $mod.e = 1;',
  32099. ' $mod.e = $mod.e - 1;',
  32100. '};',
  32101. '']),
  32102. LinesToStr([ // $mod.$main
  32103. '']));
  32104. end;
  32105. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  32106. begin
  32107. StartProgram(false);
  32108. Add([
  32109. '{$R+}',
  32110. 'type',
  32111. ' TEnum = (red,green);',
  32112. ' TEnumRg = red..green;',
  32113. 'var',
  32114. ' e: TEnumRg = red;',
  32115. 'procedure DoIt(p: TEnumRg);',
  32116. 'begin',
  32117. ' e:=p;',
  32118. ' p:=TEnumRg(0);',
  32119. ' p:=succ(e);',
  32120. 'end;',
  32121. '{$R-}',
  32122. 'procedure DoSome;',
  32123. 'begin',
  32124. ' DoIt(e);',
  32125. ' e:=TEnum(1);',
  32126. ' e:=pred(e);',
  32127. 'end;',
  32128. 'begin',
  32129. '{$R+}',
  32130. '']);
  32131. ConvertProgram;
  32132. CheckSource('TestRangeChecks_AssignEnumRange',
  32133. LinesToStr([ // statements
  32134. 'this.TEnum = {',
  32135. ' "0": "red",',
  32136. ' red: 0,',
  32137. ' "1": "green",',
  32138. ' green: 1',
  32139. '};',
  32140. 'this.e = this.TEnum.red;',
  32141. 'this.DoIt = function (p) {',
  32142. ' rtl.rc(p, 0, 1);',
  32143. ' $mod.e = rtl.rc(p, 0, 1);',
  32144. ' p = 0;',
  32145. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32146. '};',
  32147. 'this.DoSome = function () {',
  32148. ' $mod.DoIt($mod.e);',
  32149. ' $mod.e = 1;',
  32150. ' $mod.e = $mod.e - 1;',
  32151. '};',
  32152. '']),
  32153. LinesToStr([ // $mod.$main
  32154. '']));
  32155. end;
  32156. procedure TTestModule.TestRangeChecks_AssignChar;
  32157. begin
  32158. StartProgram(false);
  32159. Add([
  32160. '{$R+}',
  32161. 'type',
  32162. ' TLetter = char;',
  32163. 'var',
  32164. ' b: TLetter = ''2'';',
  32165. ' w: TLetter = ''3'';',
  32166. 'procedure DoIt(p: TLetter);',
  32167. 'begin',
  32168. ' b:=w;',
  32169. ' b:=''1'';',
  32170. 'end;',
  32171. '{$R-}',
  32172. 'procedure DoSome;',
  32173. 'begin',
  32174. ' DoIt(w);',
  32175. ' b:=w;',
  32176. ' b:=''2'';',
  32177. 'end;',
  32178. 'begin',
  32179. '{$R+}',
  32180. '']);
  32181. ConvertProgram;
  32182. CheckSource('TestRangeChecks_AssignChar',
  32183. LinesToStr([ // statements
  32184. 'this.b = "2";',
  32185. 'this.w = "3";',
  32186. 'this.DoIt = function (p) {',
  32187. ' rtl.rcc(p, 0, 65535);',
  32188. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  32189. ' $mod.b = "1";',
  32190. '};',
  32191. 'this.DoSome = function () {',
  32192. ' $mod.DoIt($mod.w);',
  32193. ' $mod.b = $mod.w;',
  32194. ' $mod.b = "2";',
  32195. '};',
  32196. '']),
  32197. LinesToStr([ // $mod.$main
  32198. '']));
  32199. end;
  32200. procedure TTestModule.TestRangeChecks_AssignCharRange;
  32201. begin
  32202. StartProgram(false);
  32203. Add([
  32204. '{$R+}',
  32205. 'type TDigit = ''0''..''9'';',
  32206. 'var',
  32207. ' b: TDigit = ''2'';',
  32208. ' w: TDigit = ''3'';',
  32209. 'procedure DoIt(p: TDigit);',
  32210. 'begin',
  32211. ' b:=w;',
  32212. ' b:=''1'';',
  32213. 'end;',
  32214. '{$R-}',
  32215. 'procedure DoSome;',
  32216. 'begin',
  32217. ' DoIt(w);',
  32218. ' b:=w;',
  32219. ' b:=''2'';',
  32220. 'end;',
  32221. 'begin',
  32222. '{$R+}',
  32223. '']);
  32224. ConvertProgram;
  32225. CheckSource('TestRangeChecks_AssignCharRange',
  32226. LinesToStr([ // statements
  32227. 'this.b = "2";',
  32228. 'this.w = "3";',
  32229. 'this.DoIt = function (p) {',
  32230. ' rtl.rcc(p, 48, 57);',
  32231. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  32232. ' $mod.b = "1";',
  32233. '};',
  32234. 'this.DoSome = function () {',
  32235. ' $mod.DoIt($mod.w);',
  32236. ' $mod.b = $mod.w;',
  32237. ' $mod.b = "2";',
  32238. '};',
  32239. '']),
  32240. LinesToStr([ // $mod.$main
  32241. '']));
  32242. end;
  32243. procedure TTestModule.TestRangeChecks_ArrayIndex;
  32244. begin
  32245. StartProgram(false);
  32246. Add([
  32247. '{$R+}',
  32248. 'type',
  32249. ' Ten = 1..10;',
  32250. ' TArr = array of Ten;',
  32251. ' TArrArr = array of TArr;',
  32252. ' TArrByte = array[byte] of Ten;',
  32253. ' TArrChar = array[''0''..''9''] of Ten;',
  32254. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  32255. ' TObject = class',
  32256. ' A: TArr;',
  32257. ' end;',
  32258. 'procedure DoIt;',
  32259. 'var',
  32260. ' Arr: TArr;',
  32261. ' ArrArr: TArrArr;',
  32262. ' ArrByte: TArrByte;',
  32263. ' ArrChar: TArrChar;',
  32264. ' ArrByteChar: TArrByteChar;',
  32265. ' i: Ten;',
  32266. ' c: char;',
  32267. ' o: tobject;',
  32268. 'begin',
  32269. ' i:=Arr[1];',
  32270. ' i:=ArrByteChar[1,''2''];',
  32271. ' Arr[1]:=Arr[1];',
  32272. ' Arr[i]:=Arr[i];',
  32273. ' ArrByte[3]:=ArrByte[3];',
  32274. ' ArrByte[i]:=ArrByte[i];',
  32275. ' ArrChar[''5'']:=ArrChar[''5''];',
  32276. ' ArrChar[c]:=ArrChar[c];',
  32277. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  32278. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  32279. ' o.a[i]:=o.a[i];',
  32280. 'end;',
  32281. 'begin',
  32282. '']);
  32283. ConvertProgram;
  32284. CheckSource('TestRangeChecks_ArrayIndex',
  32285. LinesToStr([ // statements
  32286. 'this.TArrByteChar$clone = function (a) {',
  32287. ' var b = [];',
  32288. ' b.length = 256;',
  32289. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  32290. ' return b;',
  32291. '};',
  32292. 'rtl.createClass(this, "TObject", null, function () {',
  32293. ' this.$init = function () {',
  32294. ' this.A = [];',
  32295. ' };',
  32296. ' this.$final = function () {',
  32297. ' this.A = undefined;',
  32298. ' };',
  32299. '});',
  32300. 'this.DoIt = function () {',
  32301. ' var Arr = [];',
  32302. ' var ArrArr = [];',
  32303. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  32304. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  32305. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  32306. ' var i = 0;',
  32307. ' var c = "";',
  32308. ' var o = null;',
  32309. ' i = rtl.rc(Arr[1], 1, 10);',
  32310. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  32311. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  32312. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  32313. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  32314. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  32315. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  32316. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  32317. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  32318. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  32319. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  32320. '};',
  32321. '']),
  32322. LinesToStr([ // $mod.$main
  32323. '']));
  32324. end;
  32325. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  32326. begin
  32327. StartProgram(false);
  32328. Add([
  32329. '{$R+}',
  32330. 'type',
  32331. ' Ten = 1..10;',
  32332. ' TRec = record x: Ten end;',
  32333. ' TArr = array of TRec;',
  32334. ' TArrArr = array of TArr;',
  32335. ' TObject = class',
  32336. ' A: TArr;',
  32337. ' end;',
  32338. 'procedure DoIt;',
  32339. 'var',
  32340. ' Arr: TArr;',
  32341. ' ArrArr: TArrArr;',
  32342. ' i: Ten;',
  32343. ' o: tobject;',
  32344. 'begin',
  32345. ' Arr[1]:=Arr[1];',
  32346. ' Arr[i]:=Arr[i+1];',
  32347. ' o.a[i]:=o.a[i+2];',
  32348. 'end;',
  32349. 'begin',
  32350. '']);
  32351. ConvertProgram;
  32352. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  32353. LinesToStr([ // statements
  32354. 'rtl.recNewT(this, "TRec", function () {',
  32355. ' this.x = 0;',
  32356. ' this.$eq = function (b) {',
  32357. ' return this.x === b.x;',
  32358. ' };',
  32359. ' this.$assign = function (s) {',
  32360. ' this.x = s.x;',
  32361. ' return this;',
  32362. ' };',
  32363. '});',
  32364. 'rtl.createClass(this, "TObject", null, function () {',
  32365. ' this.$init = function () {',
  32366. ' this.A = [];',
  32367. ' };',
  32368. ' this.$final = function () {',
  32369. ' this.A = undefined;',
  32370. ' };',
  32371. '});',
  32372. 'this.DoIt = function () {',
  32373. ' var Arr = [];',
  32374. ' var ArrArr = [];',
  32375. ' var i = 0;',
  32376. ' var o = null;',
  32377. ' Arr[1].$assign(Arr[1]);',
  32378. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  32379. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  32380. '};',
  32381. '']),
  32382. LinesToStr([ // $mod.$main
  32383. '']));
  32384. end;
  32385. procedure TTestModule.TestRangeChecks_StringIndex;
  32386. begin
  32387. StartProgram(false);
  32388. Add([
  32389. 'type',
  32390. ' TObject = class',
  32391. ' S: string;',
  32392. ' end;',
  32393. '{$R+}',
  32394. 'procedure DoIt(var h: string);',
  32395. 'var',
  32396. ' s: string;',
  32397. ' i: longint;',
  32398. ' c: char;',
  32399. ' o: tobject;',
  32400. 'begin',
  32401. ' c:=s[1];',
  32402. ' s[i]:=s[i];',
  32403. ' h[i]:=h[i];',
  32404. ' c:=o.s[i];',
  32405. ' o.s[i]:=c;',
  32406. 'end;',
  32407. 'begin',
  32408. '']);
  32409. ConvertProgram;
  32410. CheckSource('TestRangeChecks_StringIndex',
  32411. LinesToStr([ // statements
  32412. 'rtl.createClass(this, "TObject", null, function () {',
  32413. ' this.$init = function () {',
  32414. ' this.S = "";',
  32415. ' };',
  32416. ' this.$final = function () {',
  32417. ' };',
  32418. '});',
  32419. 'this.DoIt = function (h) {',
  32420. ' var s = "";',
  32421. ' var i = 0;',
  32422. ' var c = "";',
  32423. ' var o = null;',
  32424. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  32425. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  32426. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  32427. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  32428. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  32429. '};',
  32430. '']),
  32431. LinesToStr([ // $mod.$main
  32432. '']));
  32433. end;
  32434. procedure TTestModule.TestRangeChecks_TypecastInt;
  32435. begin
  32436. StartProgram(false);
  32437. Add([
  32438. '{$R+}',
  32439. 'var',
  32440. ' i: nativeint;',
  32441. ' b: byte;',
  32442. ' sh: shortint;',
  32443. ' w: word;',
  32444. ' sm: smallint;',
  32445. ' lw: longword;',
  32446. ' li: longint;',
  32447. 'begin',
  32448. ' b:=12+byte(i);',
  32449. ' sh:=12+shortint(i);',
  32450. ' w:=12+word(i);',
  32451. ' sm:=12+smallint(i);',
  32452. ' lw:=12+longword(i);',
  32453. ' li:=12+longint(i);',
  32454. '']);
  32455. ConvertProgram;
  32456. CheckSource('TestRangeChecks_TypecastInt',
  32457. LinesToStr([
  32458. 'this.i = 0;',
  32459. 'this.b = 0;',
  32460. 'this.sh = 0;',
  32461. 'this.w = 0;',
  32462. 'this.sm = 0;',
  32463. 'this.lw = 0;',
  32464. 'this.li = 0;',
  32465. '']),
  32466. LinesToStr([
  32467. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  32468. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  32469. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  32470. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  32471. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  32472. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  32473. '']));
  32474. end;
  32475. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  32476. begin
  32477. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32478. StartProgram(false);
  32479. Add([
  32480. '{$modeswitch typehelpers}',
  32481. '{$R+}',
  32482. 'type',
  32483. ' TObject = class',
  32484. ' FSize: byte;',
  32485. ' property Size: byte read FSize;',
  32486. ' end;',
  32487. ' THelper = type helper for byte',
  32488. ' procedure SetIt(w: word);',
  32489. ' end;',
  32490. 'procedure THelper.SetIt(w: word);',
  32491. 'begin',
  32492. ' Self:=w;',
  32493. 'end;',
  32494. 'function GetIt: byte;',
  32495. 'begin',
  32496. ' Result.SetIt(2);',
  32497. 'end;',
  32498. 'var',
  32499. ' b: byte = 3;',
  32500. ' o: TObject;',
  32501. 'begin',
  32502. ' b.SetIt(14);',
  32503. ' with b do SetIt(15);',
  32504. ' o.Size.SetIt(16);',
  32505. '']);
  32506. ConvertProgram;
  32507. CheckSource('TestRangeChecks_AssignInt',
  32508. LinesToStr([ // statements
  32509. 'rtl.createClass(this, "TObject", null, function () {',
  32510. ' this.$init = function () {',
  32511. ' this.FSize = 0;',
  32512. ' };',
  32513. ' this.$final = function () {',
  32514. ' };',
  32515. '});',
  32516. 'rtl.createHelper(this, "THelper", null, function () {',
  32517. ' this.SetIt = function (w) {',
  32518. ' rtl.rc(w, 0, 65535);',
  32519. ' this.set(w);',
  32520. ' };',
  32521. '});',
  32522. 'this.GetIt = function () {',
  32523. ' var Result = 0;',
  32524. ' $mod.THelper.SetIt.call({',
  32525. ' get: function () {',
  32526. ' return Result;',
  32527. ' },',
  32528. ' set: function (v) {',
  32529. ' rtl.rc(v, 0, 255);',
  32530. ' Result = v;',
  32531. ' }',
  32532. ' }, 2);',
  32533. ' return Result;',
  32534. '};',
  32535. 'this.b = 3;',
  32536. 'this.o = null;',
  32537. '']),
  32538. LinesToStr([ // $mod.$main
  32539. '$mod.THelper.SetIt.call({',
  32540. ' p: $mod,',
  32541. ' get: function () {',
  32542. ' return this.p.b;',
  32543. ' },',
  32544. ' set: function (v) {',
  32545. ' rtl.rc(v, 0, 255);',
  32546. ' this.p.b = v;',
  32547. ' }',
  32548. '}, 14);',
  32549. 'var $with = $mod.b;',
  32550. '$mod.THelper.SetIt.call({',
  32551. ' get: function () {',
  32552. ' return $with;',
  32553. ' },',
  32554. ' set: function (v) {',
  32555. ' rtl.rc(v, 0, 255);',
  32556. ' $with = v;',
  32557. ' }',
  32558. '}, 15);',
  32559. '$mod.THelper.SetIt.call({',
  32560. ' p: $mod.o,',
  32561. ' get: function () {',
  32562. ' return this.p.FSize;',
  32563. ' },',
  32564. ' set: function (v) {',
  32565. ' rtl.rc(v, 0, 255);',
  32566. ' this.p.FSize = v;',
  32567. ' }',
  32568. '}, 16);',
  32569. '']));
  32570. end;
  32571. procedure TTestModule.TestAsync_Proc;
  32572. begin
  32573. StartProgram(false);
  32574. Add([
  32575. 'procedure Fly(w: word = 1); async; forward;',
  32576. 'procedure Run(w: word = 2); async;',
  32577. 'begin',
  32578. ' Fly(w);',
  32579. ' Fly;',
  32580. ' await(Fly(w));',
  32581. ' await(Fly);',
  32582. 'end;',
  32583. 'procedure Fly(w: word); ',
  32584. 'begin',
  32585. 'end;',
  32586. 'begin',
  32587. ' Run;',
  32588. ' Run(3);',
  32589. '']);
  32590. CheckResolverUnexpectedHints();
  32591. ConvertProgram;
  32592. CheckSource('TestAsync_Proc',
  32593. LinesToStr([ // statements
  32594. 'this.Run = async function (w) {',
  32595. ' $mod.Fly(w);',
  32596. ' $mod.Fly(1);',
  32597. ' await $mod.Fly(w);',
  32598. ' await $mod.Fly(1);',
  32599. '};',
  32600. 'this.Fly = async function (w) {',
  32601. '};',
  32602. '']),
  32603. LinesToStr([
  32604. '$mod.Run(2);',
  32605. '$mod.Run(3);',
  32606. '']));
  32607. end;
  32608. procedure TTestModule.TestAsync_CallResultIsPromise;
  32609. begin
  32610. StartProgram(false);
  32611. Add([
  32612. '{$modeswitch externalclass}',
  32613. 'type',
  32614. ' TObject = class',
  32615. ' end;',
  32616. ' TJSPromise = class external name ''Promise''',
  32617. ' end;',
  32618. ' TBird = class',
  32619. ' function Fly: word; async; ',
  32620. ' end;',
  32621. 'function TBird.Fly: word; async; ',
  32622. 'begin',
  32623. ' Result:=3;',
  32624. ' Fly:=4+Result;',
  32625. ' if Result=5 then ;',
  32626. ' exit(6);',
  32627. 'end;',
  32628. 'function Run: word; async;',
  32629. 'begin',
  32630. ' Result:=11+Result;',
  32631. ' inc(Result);',
  32632. 'end;',
  32633. 'var',
  32634. ' p: TJSPromise;',
  32635. ' o: TBird;',
  32636. 'begin',
  32637. ' p:=Run;',
  32638. ' p:=Run();',
  32639. ' if Run=p then ;',
  32640. ' if p=Run then ;',
  32641. ' if Run()=p then ;',
  32642. ' if p=Run() then ;',
  32643. ' p:=o.Fly;',
  32644. ' p:=o.Fly();',
  32645. ' if o.Fly=p then ;',
  32646. ' if o.Fly()=p then ;',
  32647. ' with o do begin',
  32648. ' p:=Fly;',
  32649. ' p:=Fly();',
  32650. ' if Fly=p then ;',
  32651. ' if Fly()=p then ;',
  32652. ' end;',
  32653. '']);
  32654. CheckResolverUnexpectedHints();
  32655. ConvertProgram;
  32656. CheckSource('TestAsync_CallResultIsPromise',
  32657. LinesToStr([ // statements
  32658. 'rtl.createClass(this, "TObject", null, function () {',
  32659. ' this.$init = function () {',
  32660. ' };',
  32661. ' this.$final = function () {',
  32662. ' };',
  32663. '});',
  32664. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32665. ' this.Fly = async function () {',
  32666. ' var Result = 0;',
  32667. ' Result = 3;',
  32668. ' Result = 4 + Result;',
  32669. ' if (Result === 5) ;',
  32670. ' return 6;',
  32671. ' return Result;',
  32672. ' };',
  32673. '});',
  32674. 'this.Run = async function () {',
  32675. ' var Result = 0;',
  32676. ' Result = 11 + Result;',
  32677. ' Result += 1;',
  32678. ' return Result;',
  32679. '};',
  32680. 'this.p = null;',
  32681. 'this.o = null;',
  32682. '']),
  32683. LinesToStr([
  32684. '$mod.p = $mod.Run();',
  32685. '$mod.p = $mod.Run();',
  32686. 'if ($mod.Run() === $mod.p) ;',
  32687. 'if ($mod.p === $mod.Run()) ;',
  32688. 'if ($mod.Run() === $mod.p) ;',
  32689. 'if ($mod.p === $mod.Run()) ;',
  32690. '$mod.p = $mod.o.Fly();',
  32691. '$mod.p = $mod.o.Fly();',
  32692. 'if ($mod.o.Fly() === $mod.p) ;',
  32693. 'if ($mod.o.Fly() === $mod.p) ;',
  32694. 'var $with = $mod.o;',
  32695. '$mod.p = $with.Fly();',
  32696. '$mod.p = $with.Fly();',
  32697. 'if ($with.Fly() === $mod.p) ;',
  32698. 'if ($with.Fly() === $mod.p) ;',
  32699. '']));
  32700. end;
  32701. procedure TTestModule.TestAsync_ConstructorFail;
  32702. begin
  32703. StartProgram(false);
  32704. Add([
  32705. 'type',
  32706. ' TObject = class',
  32707. ' end;',
  32708. ' TBird = class',
  32709. ' constructor Create; async;',
  32710. ' end;',
  32711. 'constructor TBird.Create; async;',
  32712. 'begin',
  32713. 'end;',
  32714. 'begin',
  32715. '']);
  32716. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32717. ConvertProgram;
  32718. end;
  32719. procedure TTestModule.TestAsync_PropertyGetterFail;
  32720. begin
  32721. StartProgram(false);
  32722. Add([
  32723. 'type',
  32724. ' TObject = class',
  32725. ' end;',
  32726. ' TBird = class',
  32727. ' function GetSize: word; async;',
  32728. ' property Size: word read GetSize;',
  32729. ' end;',
  32730. 'function TBird.GetSize: word; async;',
  32731. 'begin',
  32732. 'end;',
  32733. 'begin',
  32734. '']);
  32735. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32736. ConvertProgram;
  32737. end;
  32738. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32739. begin
  32740. StartProgram(false);
  32741. Add([
  32742. 'procedure Run; async;',
  32743. 'begin',
  32744. ' await(word,1);',
  32745. 'end;',
  32746. 'begin',
  32747. '']);
  32748. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32749. ConvertProgram;
  32750. end;
  32751. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32752. begin
  32753. StartProgram(false);
  32754. Add([
  32755. 'type',
  32756. ' TObject = class',
  32757. ' end;',
  32758. ' TBird = class',
  32759. ' end;',
  32760. 'function Fly: TObject; async;',
  32761. 'begin',
  32762. 'end;',
  32763. 'procedure Run; async;',
  32764. 'begin',
  32765. ' await(TBird,Fly);',
  32766. 'end;',
  32767. 'begin',
  32768. '']);
  32769. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32770. ConvertProgram;
  32771. end;
  32772. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32773. begin
  32774. StartProgram(false);
  32775. Add([
  32776. 'procedure Crawl(w: double); ',
  32777. 'begin',
  32778. 'end;',
  32779. 'procedure Run(w: double);',
  32780. 'begin',
  32781. ' await(Crawl(w));',
  32782. 'end;',
  32783. 'begin',
  32784. ' Run(1);']);
  32785. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32786. ConvertProgram;
  32787. end;
  32788. procedure TTestModule.TestAWait_IntegerFail;
  32789. begin
  32790. StartProgram(false);
  32791. Add([
  32792. 'function Run: word;',
  32793. 'begin',
  32794. 'end;',
  32795. 'procedure Fly(w: word); async;',
  32796. 'begin',
  32797. ' await(Run());',
  32798. 'end;',
  32799. 'begin',
  32800. ' Fly(1);']);
  32801. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32802. ConvertProgram;
  32803. end;
  32804. procedure TTestModule.TestAWait_ExternalClassPromise;
  32805. begin
  32806. StartProgram(false);
  32807. Add([
  32808. '{$modeswitch externalclass}',
  32809. 'type',
  32810. ' TJSPromise = class external name ''Promise''',
  32811. ' end;',
  32812. ' TJSThenable = class external name ''Thenable''',
  32813. ' end;',
  32814. 'function Fly(w: word): TJSPromise;',
  32815. 'begin',
  32816. 'end;',
  32817. 'function Jump(w: word): word; async;',
  32818. 'begin',
  32819. 'end;',
  32820. 'function Eat(w: word): TJSPromise; async;',
  32821. 'begin',
  32822. 'end;',
  32823. 'function Run(d: double): word; async;',
  32824. 'var',
  32825. ' p: TJSPromise;',
  32826. 'begin',
  32827. ' Result:=await(word,p);', // promise needs type
  32828. ' Result:=await(word,Fly(3));', // promise needs type
  32829. ' Result:=await(Jump(4));', // async non promise must omit the type
  32830. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32831. ' Result:=await(word,Eat(6));', // promise needs type
  32832. 'end;',
  32833. 'begin',
  32834. '']);
  32835. ConvertProgram;
  32836. CheckSource('TestAWait_ExternalClassPromise',
  32837. LinesToStr([ // statements
  32838. 'this.Fly = function (w) {',
  32839. ' var Result = null;',
  32840. ' return Result;',
  32841. '};',
  32842. 'this.Jump = async function (w) {',
  32843. ' var Result = 0;',
  32844. ' return Result;',
  32845. '};',
  32846. 'this.Eat = async function (w) {',
  32847. ' var Result = null;',
  32848. ' return Result;',
  32849. '};',
  32850. 'this.Run = async function (d) {',
  32851. ' var Result = 0;',
  32852. ' var p = null;',
  32853. ' Result = await p;',
  32854. ' Result = await $mod.Fly(3);',
  32855. ' Result = await $mod.Jump(4);',
  32856. ' Result = await $mod.Jump(5);',
  32857. ' Result = await $mod.Eat(6);',
  32858. ' return Result;',
  32859. '};',
  32860. '']),
  32861. LinesToStr([
  32862. ]));
  32863. CheckResolverUnexpectedHints();
  32864. end;
  32865. procedure TTestModule.TestAWait_JSValue;
  32866. begin
  32867. StartProgram(false);
  32868. Add([
  32869. '{$modeswitch externalclass}',
  32870. 'type',
  32871. ' TJSPromise = class external name ''Promise''',
  32872. ' end;',
  32873. 'function Fly(w: word): jsvalue; async;',
  32874. 'begin',
  32875. 'end;',
  32876. 'function Run(d: jsvalue; var e): word; async;',
  32877. 'begin',
  32878. ' Result:=await(word,d);', // promise needs type
  32879. ' d:=await(Fly(4));', // async non promise must omit the type
  32880. ' Result:=await(word,e);', // promise needs type
  32881. 'end;',
  32882. 'begin',
  32883. '']);
  32884. ConvertProgram;
  32885. CheckSource('TestAWait_JSValue',
  32886. LinesToStr([ // statements
  32887. 'this.Fly = async function (w) {',
  32888. ' var Result = undefined;',
  32889. ' return Result;',
  32890. '};',
  32891. 'this.Run = async function (d, e) {',
  32892. ' var Result = 0;',
  32893. ' Result = await d;',
  32894. ' d = await $mod.Fly(4);',
  32895. ' Result = await e.get();',
  32896. ' return Result;',
  32897. '};',
  32898. '']),
  32899. LinesToStr([
  32900. ]));
  32901. CheckResolverUnexpectedHints();
  32902. end;
  32903. procedure TTestModule.TestAWait_Result;
  32904. begin
  32905. StartProgram(false);
  32906. Add([
  32907. '{$modeswitch externalclass}',
  32908. 'type',
  32909. ' TJSPromise = class external name ''Promise''',
  32910. ' end;',
  32911. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32912. 'begin',
  32913. 'end;',
  32914. 'function Run(d: double = 1.6): word; async;',
  32915. 'begin',
  32916. ' Result:=await(word,Crawl);',
  32917. ' Result:=await(word,Crawl(4.5));',
  32918. ' Result:=await(Run);',
  32919. ' Result:=await(Run(6.7));',
  32920. 'end;',
  32921. 'begin',
  32922. ' Run(1);']);
  32923. ConvertProgram;
  32924. CheckSource('TestAWait_Result',
  32925. LinesToStr([ // statements
  32926. 'this.Crawl = function (d) {',
  32927. ' var Result = null;',
  32928. ' return Result;',
  32929. '};',
  32930. 'this.Run = async function (d) {',
  32931. ' var Result = 0;',
  32932. ' Result = await $mod.Crawl(1.3);',
  32933. ' Result = await $mod.Crawl(4.5);',
  32934. ' Result = await $mod.Run(1.6);',
  32935. ' Result = await $mod.Run(6.7);',
  32936. ' return Result;',
  32937. '};',
  32938. '']),
  32939. LinesToStr([
  32940. '$mod.Run(1);'
  32941. ]));
  32942. CheckResolverUnexpectedHints();
  32943. end;
  32944. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32945. begin
  32946. StartProgram(false);
  32947. Add([
  32948. '{$mode objfpc}',
  32949. '{$modeswitch externalclass}',
  32950. 'type',
  32951. ' TJSPromise = class external name ''Promise''',
  32952. ' end;',
  32953. 'function Run: TJSPromise; async;',
  32954. 'begin',
  32955. 'end;',
  32956. 'procedure Fly(w: word); async;',
  32957. 'begin',
  32958. ' await(Run());',
  32959. 'end;',
  32960. 'begin',
  32961. ' Fly(1);']);
  32962. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32963. nWrongNumberOfParametersForCallTo);
  32964. ConvertProgram;
  32965. end;
  32966. procedure TTestModule.TestAsync_AnonymousProc;
  32967. begin
  32968. StartProgram(false);
  32969. Add([
  32970. '{$mode objfpc}',
  32971. '{$modeswitch externalclass}',
  32972. 'type',
  32973. ' TJSPromise = class external name ''Promise''',
  32974. ' end;',
  32975. 'type',
  32976. ' TFunc = reference to function(x: double): word; async;',
  32977. 'function Crawl(d: double = 1.3): word; async;',
  32978. 'begin',
  32979. 'end;',
  32980. 'var Func: TFunc;',
  32981. 'begin',
  32982. ' Func:=function(c:double):word async begin',
  32983. ' Result:=await(Crawl(c));',
  32984. ' end;',
  32985. ' Func:=function(c:double):word async assembler asm',
  32986. ' end;',
  32987. ' ']);
  32988. ConvertProgram;
  32989. CheckSource('TestAsync_AnonymousProc',
  32990. LinesToStr([ // statements
  32991. 'this.Crawl = async function (d) {',
  32992. ' var Result = 0;',
  32993. ' return Result;',
  32994. '};',
  32995. 'this.Func = null;',
  32996. '']),
  32997. LinesToStr([
  32998. '$mod.Func = async function (c) {',
  32999. ' var Result = 0;',
  33000. ' Result = await $mod.Crawl(c);',
  33001. ' return Result;',
  33002. '};',
  33003. '$mod.Func = async function (c) {',
  33004. '};',
  33005. '']));
  33006. CheckResolverUnexpectedHints();
  33007. end;
  33008. procedure TTestModule.TestAsync_ProcType;
  33009. begin
  33010. StartProgram(false);
  33011. Add([
  33012. '{$mode objfpc}',
  33013. 'type',
  33014. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  33015. ' TFunc = function(x: double = 1.1): word; async;',
  33016. ' TProc = procedure(x: longint = 7); async;',
  33017. 'function Crawl(d: double): word; async;',
  33018. 'begin',
  33019. 'end;',
  33020. 'procedure Run(e:longint); async;',
  33021. 'begin',
  33022. 'end;',
  33023. 'procedure Fly(p: TProc); async;',
  33024. 'begin',
  33025. ' await(p);',
  33026. ' await(p());',
  33027. 'end;',
  33028. 'var',
  33029. ' RefFunc: TRefFunc;',
  33030. ' Func: TFunc;',
  33031. ' Proc, ProcB: TProc;',
  33032. 'begin',
  33033. ' Func:=@Crawl;',
  33034. ' RefFunc:=@Crawl;',
  33035. ' RefFunc:=function(c:double):word async begin',
  33036. ' Result:=await(RefFunc);',
  33037. ' Result:=await(RefFunc());',
  33038. ' Result:=await(Func);',
  33039. ' Result:=await(Func());',
  33040. ' await(Proc);',
  33041. ' await(Proc());',
  33042. ' await(Proc(13));',
  33043. ' end;',
  33044. ' Proc:=@Run;',
  33045. ' if Proc=ProcB then ;',
  33046. ' ']);
  33047. ConvertProgram;
  33048. CheckResolverUnexpectedHints();
  33049. CheckSource('TestAsync_ProcType',
  33050. LinesToStr([ // statements
  33051. 'this.Crawl = async function (d) {',
  33052. ' var Result = 0;',
  33053. ' return Result;',
  33054. '};',
  33055. 'this.Run = async function (e) {',
  33056. '};',
  33057. 'this.Fly = async function (p) {',
  33058. ' await p(7);',
  33059. ' await p(7);',
  33060. '};',
  33061. 'this.RefFunc = null;',
  33062. 'this.Func = null;',
  33063. 'this.Proc = null;',
  33064. 'this.ProcB = null;',
  33065. '']),
  33066. LinesToStr([
  33067. '$mod.Func = $mod.Crawl;',
  33068. '$mod.RefFunc = $mod.Crawl;',
  33069. '$mod.RefFunc = async function (c) {',
  33070. ' var Result = 0;',
  33071. ' Result = await $mod.RefFunc(1.3);',
  33072. ' Result = await $mod.RefFunc(1.3);',
  33073. ' Result = await $mod.Func(1.1);',
  33074. ' Result = await $mod.Func(1.1);',
  33075. ' await $mod.Proc(7);',
  33076. ' await $mod.Proc(7);',
  33077. ' await $mod.Proc(13);',
  33078. ' return Result;',
  33079. '};',
  33080. '$mod.Proc = $mod.Run;',
  33081. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  33082. '']));
  33083. end;
  33084. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  33085. begin
  33086. StartProgram(false);
  33087. Add([
  33088. '{$mode objfpc}',
  33089. 'type',
  33090. ' TRefFunc = reference to function(x: double = 1.3): word;',
  33091. 'function Crawl(d: double): word; async;',
  33092. 'begin',
  33093. 'end;',
  33094. 'var',
  33095. ' RefFunc: TRefFunc;',
  33096. 'begin',
  33097. ' RefFunc:=@Crawl;',
  33098. ' ']);
  33099. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33100. ConvertProgram;
  33101. end;
  33102. procedure TTestModule.TestAsync_Inherited;
  33103. begin
  33104. StartProgram(false);
  33105. Add([
  33106. '{$mode objfpc}',
  33107. '{$modeswitch externalclass}',
  33108. 'type',
  33109. ' TJSPromise = class external name ''Promise''',
  33110. ' end;',
  33111. ' TObject = class',
  33112. ' function Run(w: word = 3): word; async; virtual;',
  33113. ' end;',
  33114. ' TBird = class',
  33115. ' function Run(w: word = 3): word; async; override;',
  33116. ' end;',
  33117. 'function TObject.Run(w: word = 3): word; async;',
  33118. 'begin',
  33119. 'end;',
  33120. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  33121. 'var p: TJSPromise;',
  33122. 'begin',
  33123. ' p:=inherited;',
  33124. ' p:=inherited Run;',
  33125. ' p:=inherited Run();',
  33126. ' p:=inherited Run(4);',
  33127. ' exit(p);',
  33128. ' exit(inherited);',
  33129. ' exit(inherited Run);',
  33130. ' exit(inherited Run(5));',
  33131. ' exit(6);',
  33132. 'end;',
  33133. 'begin',
  33134. ' ']);
  33135. ConvertProgram;
  33136. CheckSource('TestAsync_Inherited',
  33137. LinesToStr([ // statements
  33138. 'rtl.createClass(this, "TObject", null, function () {',
  33139. ' this.$init = function () {',
  33140. ' };',
  33141. ' this.$final = function () {',
  33142. ' };',
  33143. ' this.Run = async function (w) {',
  33144. ' var Result = 0;',
  33145. ' return Result;',
  33146. ' };',
  33147. '});',
  33148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33149. ' this.Run = async function (w) {',
  33150. ' var Result = 0;',
  33151. ' var p = null;',
  33152. ' p = $mod.TObject.Run.apply(this, arguments);',
  33153. ' p = $mod.TObject.Run.call(this, 3);',
  33154. ' p = $mod.TObject.Run.call(this, 3);',
  33155. ' p = $mod.TObject.Run.call(this, 4);',
  33156. ' return p;',
  33157. ' return $mod.TObject.Run.apply(this, arguments);',
  33158. ' return $mod.TObject.Run.call(this, 3);',
  33159. ' return $mod.TObject.Run.call(this, 5);',
  33160. ' return 6;',
  33161. ' return Result;',
  33162. ' };',
  33163. '});',
  33164. '']),
  33165. LinesToStr([
  33166. '']));
  33167. CheckResolverUnexpectedHints();
  33168. end;
  33169. procedure TTestModule.TestAsync_ClassInterface;
  33170. begin
  33171. StartProgram(false);
  33172. Add([
  33173. '{$mode objfpc}',
  33174. '{$modeswitch externalclass}',
  33175. 'type',
  33176. ' TJSPromise = class external name ''Promise''',
  33177. ' end;',
  33178. ' IUnknown = interface',
  33179. ' function _AddRef: longint;',
  33180. ' function _Release: longint;',
  33181. ' end;',
  33182. 'function Say(i: IUnknown): IUnknown; async;',
  33183. 'begin',
  33184. 'end;',
  33185. 'function Run: IUnknown; async;',
  33186. 'begin',
  33187. ' Result:=await(Run);',
  33188. ' Result:=await(Run());',
  33189. ' Result:=await(Run) as IUnknown;',
  33190. ' Result:=await(Say(nil));',
  33191. ' Result:=await(Say(await(Run())));',
  33192. ' Result:=await(Say(await(Run()) as IUnknown));',
  33193. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  33194. 'end;',
  33195. 'procedure Fly;',
  33196. 'var p: TJSPromise;',
  33197. 'begin',
  33198. ' Run;',
  33199. ' Run();',
  33200. ' p:=Run;',
  33201. ' p:=Run();',
  33202. 'end;',
  33203. 'begin',
  33204. ' ']);
  33205. ConvertProgram;
  33206. CheckSource('TestAsync_ClassInterface',
  33207. LinesToStr([ // statements
  33208. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  33209. 'this.Say = async function (i) {',
  33210. ' var Result = null;',
  33211. ' return Result;',
  33212. '};',
  33213. 'this.Run = async function () {',
  33214. ' var Result = null;',
  33215. ' var $ok = false;',
  33216. ' try {',
  33217. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33218. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33219. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  33220. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  33221. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  33222. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  33223. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  33224. ' $ok = true;',
  33225. ' } finally {',
  33226. ' if (!$ok) rtl._Release(Result);',
  33227. ' };',
  33228. ' return Result;',
  33229. '};',
  33230. 'this.Fly = function () {',
  33231. ' var p = null;',
  33232. ' $mod.Run();',
  33233. ' $mod.Run();',
  33234. ' p = $mod.Run();',
  33235. ' p = $mod.Run();',
  33236. '};',
  33237. '']),
  33238. LinesToStr([
  33239. '']));
  33240. CheckResolverUnexpectedHints();
  33241. end;
  33242. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  33243. begin
  33244. StartProgram(true,[supTInterfacedObject]);
  33245. Add([
  33246. '{$mode objfpc}',
  33247. '{$modeswitch externalclass}',
  33248. 'type',
  33249. ' TJSPromise = class external name ''Promise''',
  33250. ' end;',
  33251. ' IBird = interface',
  33252. ' procedure Run;',
  33253. ' end;',
  33254. ' TBird = class(TInterfacedObject,IBird)',
  33255. ' procedure Run; async;',
  33256. ' end;',
  33257. 'procedure TBird.Run;',
  33258. 'begin',
  33259. 'end;',
  33260. 'begin',
  33261. ' ']);
  33262. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33263. ConvertProgram;
  33264. end;
  33265. procedure TTestModule.TestAWait_ClassAs;
  33266. begin
  33267. StartProgram(false);
  33268. Add([
  33269. '{$mode objfpc}',
  33270. '{$modeswitch externalclass}',
  33271. 'type',
  33272. ' TJSPromise = class external name ''Promise''',
  33273. ' end;',
  33274. ' TObject = class',
  33275. ' function Run: TObject; async;',
  33276. ' end;',
  33277. ' TBird = class',
  33278. ' function Fly: TBird; async;',
  33279. ' end;',
  33280. 'function TObject.Run: TObject; async;',
  33281. 'begin',
  33282. 'end;',
  33283. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  33284. 'var o: TObject;',
  33285. 'begin',
  33286. ' o:=await(TObject,Run);',
  33287. ' o:=await(TObject,Fly);',
  33288. ' o:=await(TBird,Fly);',
  33289. ' o:=await(TObject,inherited Run);',
  33290. ' o:=await(TObject,inherited Run) as TBird;',
  33291. 'end;',
  33292. 'begin',
  33293. ' ']);
  33294. ConvertProgram;
  33295. CheckSource('TestAWait_ClassAs',
  33296. LinesToStr([ // statements
  33297. 'rtl.createClass(this, "TObject", null, function () {',
  33298. ' this.$init = function () {',
  33299. ' };',
  33300. ' this.$final = function () {',
  33301. ' };',
  33302. ' this.Run = async function () {',
  33303. ' var Result = null;',
  33304. ' return Result;',
  33305. ' };',
  33306. '});',
  33307. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33308. ' this.Fly = async function () {',
  33309. ' var Result = null;',
  33310. ' var o = null;',
  33311. ' o = await this.Run();',
  33312. ' o = await this.Fly();',
  33313. ' o = await this.Fly();',
  33314. ' o = await $mod.TObject.Run.call(this);',
  33315. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  33316. ' return Result;',
  33317. ' };',
  33318. '});',
  33319. '']),
  33320. LinesToStr([
  33321. '']));
  33322. CheckResolverUnexpectedHints();
  33323. end;
  33324. procedure TTestModule.TestLibrary_Empty;
  33325. begin
  33326. StartLibrary(false);
  33327. Add([
  33328. '']);
  33329. ConvertLibrary;
  33330. CheckSource('TestLibrary_Empty',
  33331. LinesToStr([ // statements
  33332. '']),
  33333. LinesToStr([
  33334. '']));
  33335. CheckResolverUnexpectedHints();
  33336. end;
  33337. procedure TTestModule.TestLibrary_ExportFunc;
  33338. begin
  33339. StartLibrary(false);
  33340. Add([
  33341. 'procedure Run(w: word);',
  33342. 'begin',
  33343. 'end;',
  33344. 'exports',
  33345. ' Run;',
  33346. ' run name ''Foo'';',
  33347. ' test1.run name ''Test1Run'';',
  33348. '']);
  33349. ConvertLibrary;
  33350. CheckSource('TestLibrary_ExportFunc',
  33351. LinesToStr([ // statements
  33352. 'this.Run = function (w) {',
  33353. '};',
  33354. 'export { this.Run as Run, this.Run as Foo, this.Run as Test1Run };',
  33355. '']),
  33356. LinesToStr([
  33357. '']));
  33358. CheckResolverUnexpectedHints();
  33359. end;
  33360. procedure TTestModule.TestLibrary_Export_Index_Fail;
  33361. begin
  33362. StartLibrary(false);
  33363. Add([
  33364. 'procedure Run(w: word);',
  33365. 'begin',
  33366. 'end;',
  33367. 'exports',
  33368. ' Run index 3;',
  33369. '']);
  33370. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  33371. ConvertLibrary;
  33372. end;
  33373. procedure TTestModule.TestLibrary_ExportVar;
  33374. begin
  33375. StartLibrary(false);
  33376. Add([
  33377. 'var Wing: word;',
  33378. 'exports',
  33379. ' Wing;',
  33380. '']);
  33381. ConvertLibrary;
  33382. CheckSource('TestLibrary_ExportVar',
  33383. LinesToStr([ // statements
  33384. 'this.Wing = 0;',
  33385. 'export { this.Wing as Wing };',
  33386. '']),
  33387. LinesToStr([
  33388. '']));
  33389. CheckResolverUnexpectedHints();
  33390. end;
  33391. procedure TTestModule.TestLibrary_ExportUnitFunc;
  33392. begin
  33393. end;
  33394. Initialization
  33395. RegisterTests([TTestModule]);
  33396. end.